[go: up one dir, main page]

Menu

[1f9cef]: / src / ui_util.c  Maximize  Restore  History

Download this file

597 lines (538 with data), 18.0 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
/*
* Viking - GPS data editor
* Copyright (C) 2014, Rob Norris <rw_norris@hotmail.com>
* Copyright 2006-2012 Enrico Tröger <enrico(dot)troeger(at)uvena(dot)de>
* Copyright 2006-2012 Nick Treleaven <nick(dot)treleaven(at)btinternet(dot)com>
* Copyright 2011-2012 Matthew Brush <mbrush(at)codebrainz(dot)ca>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
/*
* Ideally dependencies should just be on Gtk,
* see vikutils for things that further depend on other Viking types
* see utils for things only depend on Glib
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <glib/gstdio.h>
#include <glib/gi18n.h>
#include <glib/gprintf.h>
#include "ui_util.h"
#include "util.h"
#include "dialog.h"
#include "settings.h"
#include "icons/icons.h"
#ifdef WINDOWS
#include <windows.h>
#endif
#ifndef WINDOWS
static gboolean spawn_command_line_async(const gchar * cmd,
const gchar * arg)
{
gchar *cmdline = NULL;
gboolean status;
cmdline = g_strdup_printf("%s '%s'", cmd, arg);
g_debug("Running: %s", cmdline);
status = g_spawn_command_line_async(cmdline, NULL);
g_free(cmdline);
return status;
}
#endif
// Annoyingly gtk_show_uri() doesn't work so resort to ShellExecute method
// (non working at least in our Windows build with GTK+2.24.10 on Windows 7)
// @parent: Maybe NULL
//
void open_url(GtkWindow *parent, const gchar * url)
{
#ifdef WINDOWS
ShellExecute(NULL, NULL, (char *) url, NULL, ".\\", 0);
#else
gboolean use_browser = FALSE;
if ( a_settings_get_boolean ( "use_env_browser", &use_browser ) ) {
const gchar *browser = g_getenv("BROWSER");
if (browser == NULL || browser[0] == '\0') {
browser = "firefox";
}
if (spawn_command_line_async(browser, url)) {
return;
}
else
g_warning("Failed to run: %s on %s", browser, url);
}
else {
GError *error = NULL;
#if GTK_CHECK_VERSION (3,22,0)
(void)gtk_show_uri_on_window ( parent, url, GDK_CURRENT_TIME, &error );
#else
(void)gtk_show_uri ( parent ? gtk_widget_get_screen(GTK_WIDGET(parent)) : NULL, url, GDK_CURRENT_TIME, &error );
#endif
if ( error ) {
a_dialog_error_msg_extra ( parent, _("Could not launch web browser. %s"), error->message );
g_error_free ( error );
}
}
#endif
}
void new_email(GtkWindow *parent, const gchar * address)
{
gchar *uri = g_strdup_printf("mailto:%s", address);
GError *error = NULL;
#if GTK_CHECK_VERSION (3,22,0)
(void)gtk_show_uri_on_window ( parent, uri, GDK_CURRENT_TIME, &error );
#else
(void)gtk_show_uri ( gtk_widget_get_screen (GTK_WIDGET(parent)), uri, GDK_CURRENT_TIME, &error );
#endif
if ( error ) {
a_dialog_error_msg_extra ( parent, _("Could not create new email. %s"), error->message );
g_error_free ( error );
}
/*
#ifdef WINDOWS
ShellExecute(NULL, NULL, (char *) uri, NULL, ".\\", 0);
#else
if (!spawn_command_line_async("xdg-email", uri))
a_dialog_error_msg ( parent, _("Could not create new email.") );
#endif
*/
g_free(uri);
uri = NULL;
}
/** Creates a @c GtkButton with custom text and a stock image similar to
* @c gtk_button_new_from_stock().
* @param stock_id A @c GTK_STOCK_NAME string.
* @param text Button label text, can include mnemonics.
* @return The new @c GtkButton.
*/
GtkWidget *ui_button_new_with_image(const gchar *stock_id, const gchar *text)
{
GtkWidget *image, *button;
button = gtk_button_new_with_mnemonic(text);
gtk_widget_show(button);
image = gtk_image_new_from_stock(stock_id, GTK_ICON_SIZE_BUTTON);
gtk_button_set_image(GTK_BUTTON(button), image);
// note: image is shown by gtk
return button;
}
/** Reads an integer from the GTK default settings registry
* (see http://library.gnome.org/devel/gtk/stable/GtkSettings.html).
* @param property_name The property to read.
* @param default_value The default value in case the value could not be read.
* @return The value for the property if it exists, otherwise the @a default_value.
*/
gint ui_get_gtk_settings_integer(const gchar *property_name, gint default_value)
{
if (g_object_class_find_property(G_OBJECT_GET_CLASS(G_OBJECT(
gtk_settings_get_default())), property_name))
{
gint value;
g_object_get(G_OBJECT(gtk_settings_get_default()), property_name, &value, NULL);
return value;
}
else
return default_value;
}
/** Returns a widget from a name in a component, usually created by Glade.
* Call it with the toplevel widget in the component (i.e. a window/dialog),
* or alternatively any widget in the component, and the name of the widget
* you want returned.
* @param widget Widget with the @a widget_name property set.
* @param widget_name Name to lookup.
* @return The widget found.
* @see ui_hookup_widget().
*
*/
GtkWidget *ui_lookup_widget(GtkWidget *widget, const gchar *widget_name)
{
GtkWidget *parent, *found_widget;
g_return_val_if_fail(widget != NULL, NULL);
g_return_val_if_fail(widget_name != NULL, NULL);
for (;;)
{
if (GTK_IS_MENU(widget))
parent = gtk_menu_get_attach_widget(GTK_MENU(widget));
else
parent = gtk_widget_get_parent(widget);
if (parent == NULL)
parent = (GtkWidget*) g_object_get_data(G_OBJECT(widget), "GladeParentKey");
if (parent == NULL)
break;
widget = parent;
}
found_widget = (GtkWidget*) g_object_get_data(G_OBJECT(widget), widget_name);
if (G_UNLIKELY(found_widget == NULL))
g_warning("Widget not found: %s", widget_name);
return found_widget;
}
/**
* Returns a label widget that is made selectable (i.e. the user can copy the text)
* @param text String to display - maybe NULL
* @return The label widget
*/
GtkWidget* ui_label_new_selectable ( const gchar* text )
{
GtkWidget *widget = gtk_label_new ( text );
gtk_label_set_selectable ( GTK_LABEL(widget), TRUE );
return widget;
}
/**
* Create a new pixbuf of the specified color and size
*/
GdkPixbuf *ui_pixbuf_new ( GdkColor *color, guint width, guint height )
{
GdkPixbuf *pixbuf = gdk_pixbuf_new ( GDK_COLORSPACE_RGB, FALSE, 8, width, height );
// Annoyingly the GdkColor.pixel does not give the correct color when passed to gdk_pixbuf_fill (even when alloc'ed)
// Here is some magic found to do the conversion
// http://www.cs.binghamton.edu/~sgreene/cs360-2011s/topics/gtk+-2.20.1/gtk/gtkcolorbutton.c
if ( color ) {
guint32 pixel = ((color->red & 0xff00) << 16) |
((color->green & 0xff00) << 8) |
(color->blue & 0xff00);
gdk_pixbuf_fill ( pixbuf, pixel );
}
return pixbuf;
}
/**
* Apply the alpha value to the specified pixbuf
*/
GdkPixbuf *ui_pixbuf_set_alpha ( GdkPixbuf *pixbuf, guint8 alpha )
{
guchar *pixels;
gint width, height, iii, jjj;
if ( ! gdk_pixbuf_get_has_alpha ( pixbuf ) )
{
GdkPixbuf *tmp = gdk_pixbuf_add_alpha(pixbuf,FALSE,0,0,0);
g_object_unref(G_OBJECT(pixbuf));
pixbuf = tmp;
if ( !pixbuf )
return NULL;
}
pixels = gdk_pixbuf_get_pixels(pixbuf);
width = gdk_pixbuf_get_width(pixbuf);
height = gdk_pixbuf_get_height(pixbuf);
/* r,g,b,a,r,g,b,a.... */
for (iii = 0; iii < width; iii++) for (jjj = 0; jjj < height; jjj++)
{
pixels += 3;
if ( *pixels != 0 )
*pixels = alpha;
pixels++;
}
return pixbuf;
}
/**
* Reduce the alpha value of the specified pixbuf by alpha / 255
*/
GdkPixbuf *ui_pixbuf_scale_alpha ( GdkPixbuf *pixbuf, guint8 alpha )
{
guchar *pixels;
gint width, height, iii, jjj;
if ( ! gdk_pixbuf_get_has_alpha ( pixbuf ) )
{
GdkPixbuf *tmp = gdk_pixbuf_add_alpha(pixbuf,FALSE,0,0,0);
g_object_unref(G_OBJECT(pixbuf));
pixbuf = tmp;
if ( !pixbuf )
return NULL;
}
pixels = gdk_pixbuf_get_pixels(pixbuf);
width = gdk_pixbuf_get_width(pixbuf);
height = gdk_pixbuf_get_height(pixbuf);
/* r,g,b,a,r,g,b,a.... */
for (iii = 0; iii < width; iii++) for (jjj = 0; jjj < height; jjj++)
{
pixels += 3;
if ( *pixels != 0 )
*pixels = (guint8)(((guint16)*pixels * (guint16)alpha) / 255);
pixels++;
}
return pixbuf;
}
/**
*
*/
void ui_add_recent_file ( const gchar *filename )
{
if ( filename ) {
GtkRecentManager *manager = gtk_recent_manager_get_default();
GFile *file = g_file_new_for_commandline_arg ( filename );
gchar *uri = g_file_get_uri ( file );
if ( uri && manager )
gtk_recent_manager_add_item ( manager, uri );
g_object_unref( file );
g_free (uri);
}
}
/**
* Clear the entry text if the specified icon is pressed
*/
static void ui_icon_clear_entry ( GtkEntry *entry,
GtkEntryIconPosition position,
GdkEventButton *event,
gpointer data )
{
if ( position == GPOINTER_TO_INT(data) )
gtk_entry_set_text ( entry, "" );
}
static void
text_changed_cb (GtkEntry *entry,
GParamSpec *pspec,
gpointer data)
{
if ( data ) {
gboolean has_text = gtk_entry_get_text_length(entry) > 0;
gtk_entry_set_icon_sensitive ( entry, GPOINTER_TO_INT(data), has_text );
}
}
/**
* Create an entry field with an icon to clear the entry
*
* Ideal for entries used for getting user entered transitory data,
* so it is easy to delete the text and start again.
*/
GtkWidget *ui_entry_new ( const gchar *str, GtkEntryIconPosition position )
{
GtkWidget *entry = gtk_entry_new();
if ( str )
gtk_entry_set_text ( GTK_ENTRY(entry), str );
gtk_entry_set_icon_from_stock ( GTK_ENTRY(entry), position, GTK_STOCK_CLEAR );
#if GTK_CHECK_VERSION (2,20,0)
text_changed_cb ( GTK_ENTRY(entry), NULL, GINT_TO_POINTER(position) );
g_signal_connect ( entry, "notify::text", G_CALLBACK(text_changed_cb), GINT_TO_POINTER(position) );
#endif
g_signal_connect ( entry, "icon-release", G_CALLBACK(ui_icon_clear_entry), GINT_TO_POINTER(position) );
return entry;
}
/**
* Set the entry field text
*
* Handles NULL, unlike gtk_entry_set_text() which complains
*/
void ui_entry_set_text ( GtkWidget *widget, const gchar *str )
{
if ( str )
gtk_entry_set_text ( GTK_ENTRY(widget), str );
else
gtk_entry_set_text ( GTK_ENTRY(widget), "" );
}
/**
* Create a spinbutton with an icon to clear the entry
*
* Ideal for entries used for getting user entered transitory data,
* so it is easy to delete the number and start again.
*/
GtkWidget *ui_spin_button_new ( GtkAdjustment *adjustment,
gdouble climb_rate,
guint digits )
{
GtkWidget *spin = gtk_spin_button_new ( adjustment, climb_rate, digits );
gtk_entry_set_icon_from_stock ( GTK_ENTRY(spin), GTK_ENTRY_ICON_PRIMARY, GTK_STOCK_CLEAR );
g_signal_connect ( spin, "icon-release", G_CALLBACK(ui_icon_clear_entry), GINT_TO_POINTER(GTK_ENTRY_ICON_PRIMARY) );
return spin;
}
/**
* ui_format_1f_cell_data_func:
*
* General purpose column double formatting
*
*/
void ui_format_1f_cell_data_func ( GtkTreeViewColumn *col,
GtkCellRenderer *renderer,
GtkTreeModel *model,
GtkTreeIter *iter,
gpointer user_data )
{
gdouble value;
gchar buf[20];
gint column = GPOINTER_TO_INT (user_data);
gtk_tree_model_get ( model, iter, column, &value, -1 );
g_snprintf ( buf, sizeof(buf), "%.1f", value );
g_object_set ( renderer, "text", buf, NULL );
}
/**
* ui_new_column_text:
*
* Standard adding of a text column
*
*/
GtkTreeViewColumn *ui_new_column_text ( const gchar *title, GtkCellRenderer *renderer, GtkWidget *view, gint column_runner )
{
GtkTreeViewColumn *column = gtk_tree_view_column_new_with_attributes ( title, renderer, "text", column_runner, NULL );
gtk_tree_view_column_set_sort_column_id ( column, column_runner );
gtk_tree_view_append_column ( GTK_TREE_VIEW(view), column );
gtk_tree_view_column_set_resizable ( column, TRUE );
return column;
}
/**
* ui_tree_model_number_tooltip_cb:
*
* A simple tooltip on a widget for showing number of items displayed in the tree model
*
*/
gboolean ui_tree_model_number_tooltip_cb ( GtkWidget *widget,
gint x,
gint y,
gboolean keyboard_mode,
GtkTooltip *tooltip,
GtkTreeModel *tree_model )
{
gint num = gtk_tree_model_iter_n_children ( tree_model, NULL );
if ( num ) {
gchar *text = g_strdup_printf ( _("Items: %d"), num );
gtk_tooltip_set_text ( tooltip, text );
g_free ( text );
}
return (num != 0);
}
/**
* Load Icons using GResource (see icons/icons.gresource.xml)
*/
void ui_load_icons ( void )
{
const gchar *vikpath = "/org/viking-gps/viking/icons";
// Much easier in GTK3
#if GTK_CHECK_VERSION(3, 14, 0)
gtk_icon_theme_add_resource_path ( gtk_icon_theme_get_default(), vikpath );
#else
GResource *icon_res = vik_icons_get_resource();
char ** children = g_resource_enumerate_children ( icon_res, vikpath, 0, NULL );
if ( children == NULL ) {
g_critical ( "No icons in resources!" );
return;
}
for ( int ii = 0; children[ii] != NULL; ii++ ) {
char *path = g_strdup_printf ( "%s/%s", vikpath, children[ii] );
GError *error = NULL;
GdkPixbuf *pb = gdk_pixbuf_new_from_resource ( path, &error );
if ( error ) {
g_warning ( "%s: %s, %s", __FUNCTION__, children[ii], error->message );
g_error_free ( error );
break;
}
int size = gdk_pixbuf_get_height ( pb );
// Use a normalized name (i.e. skip the '.png')
char *name = g_strdup ( children[ii] );
char *pos = g_strstr_len ( name, -1, "." );
if ( pos != NULL )
*pos = '\0';
gtk_icon_theme_add_builtin_icon ( name, size, pb );
g_object_unref ( pb );
g_free ( name );
}
g_strfreev ( children );
#endif
}
GdkPixbuf *ui_get_icon ( const gchar *name, guint size )
{
GError *error = NULL;
GdkPixbuf *icon = gtk_icon_theme_load_icon ( gtk_icon_theme_get_default(), name, size, GTK_ICON_LOOKUP_FORCE_SIZE, &error );
if ( error ) {
// Own icons should always be defined
g_critical ( "%s: icon '%s' - %s", __FUNCTION__, name, error->message );
g_error_free ( error );
}
return icon;
}
/**
* replacement for gdk_draw_layout()
* note that x,y are now doubles and may need to be offset by 0.5
*/
void ui_cr_draw_layout ( cairo_t *cr, gdouble xx, gdouble yy, PangoLayout *layout )
{
cairo_move_to ( cr, xx, yy );
pango_cairo_show_layout ( cr, layout );
}
/**
* replacement for gdk_draw_line()
* note that as x,y are now doubles and describe the center of the line
* ATM no clipping is performed and unknown what happens if ones uses
* very large numbers for x2, y2
*
* cairo_stroke() should be called after using this
* (or after consecutive calls to this are done as a group)
*/
void ui_cr_draw_line ( cairo_t *cr, gdouble x1, gdouble y1, gdouble x2, gdouble y2 )
{
cairo_move_to ( cr, x1, y1 );
cairo_line_to ( cr, x2, y2 );
}
/**
* replacement for gdk_draw_rectangle()
* note that as x,y are now doubles and describe the center of the line
* ATM no clipping is performed and unknown what happens if ones uses very large numbers
* cairo_stroke() should be called after using this
* (or after consecutive calls to this are done as a group)
*/
void ui_cr_draw_rectangle ( cairo_t *cr, gboolean fill, gdouble xx, gdouble yy, gdouble ww, gdouble hh )
{
cairo_rectangle ( cr, xx, yy, ww, hh );
if ( fill )
cairo_fill ( cr );
}
void ui_cr_set_color ( cairo_t *cr, const gchar *name )
{
GdkColor color;
if ( gdk_color_parse(name, &color) ) {
gdk_cairo_set_source_color ( cr, &color );
}
}
// Generic dashed line
void ui_cr_set_dash ( cairo_t *cr )
{
// ATM assume this doesn't need to be multiplied by the vvp->scale
static const double dashed[] = {4.0};
cairo_set_dash ( cr, dashed, 1, 0 );
}
void ui_cr_clear ( cairo_t *cr )
{
g_return_if_fail ( cr != NULL );
cairo_operator_t operator = cairo_get_operator ( cr );
// Clear the buffer
cairo_set_operator ( cr, CAIRO_OPERATOR_CLEAR );
cairo_paint ( cr );
// Return to the previous mode
// typically will be the default operator CAIRO_OPERATOR_OVER, for the normal drawing on top mode
cairo_set_operator ( cr, operator );
}
// Typically should be used in the 'draw' event
// Draw the specified surface (whole screen)
// which would be overlayed on any other surface
void ui_cr_surface_paint ( cairo_t *cr, cairo_surface_t *surface )
{
g_return_if_fail ( surface != NULL );
cairo_set_source_surface ( cr, surface, 0.0, 0.0 );
cairo_paint ( cr );
}
// Label with solid a background and a border
void ui_cr_label_with_bg (cairo_t *cr, gint xd, gint yd, gint wd, gint hd, PangoLayout *pl)
{
g_return_if_fail ( cr != NULL );
ui_cr_set_color ( cr, "#cccccc" );
ui_cr_draw_rectangle ( cr, TRUE, xd-2, yd-1, wd+4, hd+1 );
cairo_stroke ( cr );
ui_cr_set_color ( cr, "#000000" );
ui_cr_draw_rectangle ( cr, FALSE, xd-2, yd-1, wd+4, hd+1 );
cairo_stroke ( cr );
ui_cr_draw_layout ( cr, xd, yd, pl );
}
void ui_gc_unref ( GdkGC *gc )
{
#if GTK_CHECK_VERSION (3,0,0)
cairo_destroy ( gc );
#else
g_object_unref ( G_OBJECT(gc) );
#endif
}