]> git.street.me.uk Git - andy/viking.git/blob - src/ui_util.c
Merge pull request #20 from huobos/zh_CN
[andy/viking.git] / src / ui_util.c
1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
2 /*
3  *    Viking - GPS data editor
4  *    Copyright (C) 2014, Rob Norris <rw_norris@hotmail.com>
5  *    Copyright 2006-2012 Enrico Tröger <enrico(dot)troeger(at)uvena(dot)de>
6  *    Copyright 2006-2012 Nick Treleaven <nick(dot)treleaven(at)btinternet(dot)com>
7  *    Copyright 2011-2012 Matthew Brush <mbrush(at)codebrainz(dot)ca>
8  *
9  *    This program is free software; you can redistribute it and/or modify
10  *    it under the terms of the GNU General Public License as published by
11  *    the Free Software Foundation; either version 2 of the License, or
12  *    (at your option) any later version.
13  *
14  *    This program is distributed in the hope that it will be useful,
15  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *    GNU General Public License for more details.
18  *
19  *    You should have received a copy of the GNU General Public License
20  *    along with this program; if not, write to the Free Software
21  *    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
22  */
23  /*
24   * Ideally dependencies should just be on Gtk,
25   * see vikutils for things that further depend on other Viking types
26   * see utils for things only depend on Glib
27   */
28 #ifdef HAVE_CONFIG_H
29 #include "config.h"
30 #endif
31
32 #include <glib/gstdio.h>
33 #include <glib/gi18n.h>
34 #include <glib/gprintf.h>
35
36 #include "util.h"
37 #include "dialog.h"
38 #include "settings.h"
39
40 #ifdef WINDOWS
41 #include <windows.h>
42 #endif
43
44
45 #ifndef WINDOWS
46 static gboolean spawn_command_line_async(const gchar * cmd,
47                                          const gchar * arg)
48 {
49   gchar *cmdline = NULL;
50   gboolean status;
51
52   cmdline = g_strdup_printf("%s '%s'", cmd, arg);
53   g_debug("Running: %s", cmdline);
54     
55   status = g_spawn_command_line_async(cmdline, NULL);
56
57   g_free(cmdline);
58  
59   return status;
60 }
61 #endif
62
63
64 // Annoyingly gtk_show_uri() doesn't work so resort to ShellExecute method
65 //   (non working at least in our Windows build with GTK+2.24.10 on Windows 7)
66
67 void open_url(GtkWindow *parent, const gchar * url)
68 {
69 #ifdef WINDOWS
70   ShellExecute(NULL, NULL, (char *) url, NULL, ".\\", 0);
71 #else
72   gboolean use_browser = FALSE;
73   if ( a_settings_get_boolean ( "use_env_browser", &use_browser ) ) {
74     const gchar *browser = g_getenv("BROWSER");
75     if (browser == NULL || browser[0] == '\0') {
76       browser = "firefox";
77     }
78     if (spawn_command_line_async(browser, url)) {
79       return;
80     }
81     else
82       g_warning("Failed to run: %s on %s", browser, url);
83   }
84   else {
85     GError *error = NULL;
86     gtk_show_uri ( gtk_widget_get_screen (GTK_WIDGET(parent)), url, GDK_CURRENT_TIME, &error );
87     if ( error ) {
88       a_dialog_error_msg_extra ( parent, _("Could not launch web browser. %s"), error->message );
89       g_error_free ( error );
90     }
91   }
92 #endif
93 }
94
95 void new_email(GtkWindow *parent, const gchar * address)
96 {
97   gchar *uri = g_strdup_printf("mailto:%s", address);
98   GError *error = NULL;
99   gtk_show_uri ( gtk_widget_get_screen (GTK_WIDGET(parent)), uri, GDK_CURRENT_TIME, &error );
100   if ( error ) {
101     a_dialog_error_msg_extra ( parent, _("Could not create new email. %s"), error->message );
102     g_error_free ( error );
103   }
104   /*
105 #ifdef WINDOWS
106   ShellExecute(NULL, NULL, (char *) uri, NULL, ".\\", 0);
107 #else
108   if (!spawn_command_line_async("xdg-email", uri))
109     a_dialog_error_msg ( parent, _("Could not create new email.") );
110 #endif
111   */
112   g_free(uri);
113   uri = NULL;
114 }
115
116 /** Creates a @c GtkButton with custom text and a stock image similar to
117  * @c gtk_button_new_from_stock().
118  * @param stock_id A @c GTK_STOCK_NAME string.
119  * @param text Button label text, can include mnemonics.
120  * @return The new @c GtkButton.
121  */
122 GtkWidget *ui_button_new_with_image(const gchar *stock_id, const gchar *text)
123 {
124         GtkWidget *image, *button;
125
126         button = gtk_button_new_with_mnemonic(text);
127         gtk_widget_show(button);
128         image = gtk_image_new_from_stock(stock_id, GTK_ICON_SIZE_BUTTON);
129         gtk_button_set_image(GTK_BUTTON(button), image);
130         // note: image is shown by gtk
131         return button;
132 }
133
134 /** Reads an integer from the GTK default settings registry
135  * (see http://library.gnome.org/devel/gtk/stable/GtkSettings.html).
136  * @param property_name The property to read.
137  * @param default_value The default value in case the value could not be read.
138  * @return The value for the property if it exists, otherwise the @a default_value.
139  */
140 gint ui_get_gtk_settings_integer(const gchar *property_name, gint default_value)
141 {
142         if (g_object_class_find_property(G_OBJECT_GET_CLASS(G_OBJECT(
143                 gtk_settings_get_default())), property_name))
144         {
145                 gint value;
146                 g_object_get(G_OBJECT(gtk_settings_get_default()), property_name, &value, NULL);
147                 return value;
148         }
149         else
150                 return default_value;
151 }
152
153
154 /** Returns a widget from a name in a component, usually created by Glade.
155  * Call it with the toplevel widget in the component (i.e. a window/dialog),
156  * or alternatively any widget in the component, and the name of the widget
157  * you want returned.
158  * @param widget Widget with the @a widget_name property set.
159  * @param widget_name Name to lookup.
160  * @return The widget found.
161  * @see ui_hookup_widget().
162  *
163  */
164 GtkWidget *ui_lookup_widget(GtkWidget *widget, const gchar *widget_name)
165 {
166         GtkWidget *parent, *found_widget;
167
168         g_return_val_if_fail(widget != NULL, NULL);
169         g_return_val_if_fail(widget_name != NULL, NULL);
170
171         for (;;)
172         {
173                 if (GTK_IS_MENU(widget))
174                         parent = gtk_menu_get_attach_widget(GTK_MENU(widget));
175                 else
176                         parent = gtk_widget_get_parent(widget);
177                 if (parent == NULL)
178                         parent = (GtkWidget*) g_object_get_data(G_OBJECT(widget), "GladeParentKey");
179                 if (parent == NULL)
180                         break;
181                 widget = parent;
182         }
183
184         found_widget = (GtkWidget*) g_object_get_data(G_OBJECT(widget), widget_name);
185         if (G_UNLIKELY(found_widget == NULL))
186                 g_warning("Widget not found: %s", widget_name);
187         return found_widget;
188 }
189
190 /**
191  * Returns a label widget that is made selectable (i.e. the user can copy the text)
192  * @param text String to display - maybe NULL
193  * @return The label widget
194  */
195 GtkWidget* ui_label_new_selectable ( const gchar* text )
196 {
197         GtkWidget *widget = gtk_label_new ( text );
198         gtk_label_set_selectable ( GTK_LABEL(widget), TRUE );
199         return widget;
200 }
201
202 /**
203  * Apply the alpha value to the specified pixbuf
204  */
205 GdkPixbuf *ui_pixbuf_set_alpha ( GdkPixbuf *pixbuf, guint8 alpha )
206 {
207   guchar *pixels;
208   gint width, height, iii, jjj;
209
210   if ( ! gdk_pixbuf_get_has_alpha ( pixbuf ) )
211   {
212     GdkPixbuf *tmp = gdk_pixbuf_add_alpha(pixbuf,FALSE,0,0,0);
213     g_object_unref(G_OBJECT(pixbuf));
214     pixbuf = tmp;
215     if ( !pixbuf )
216       return NULL;
217   }
218
219   pixels = gdk_pixbuf_get_pixels(pixbuf);
220   width = gdk_pixbuf_get_width(pixbuf);
221   height = gdk_pixbuf_get_height(pixbuf);
222
223   /* r,g,b,a,r,g,b,a.... */
224   for (iii = 0; iii < width; iii++) for (jjj = 0; jjj < height; jjj++)
225   {
226     pixels += 3;
227     if ( *pixels != 0 )
228       *pixels = alpha;
229     pixels++;
230   }
231   return pixbuf;
232 }
233
234
235 /**
236  * Reduce the alpha value of the specified pixbuf by alpha / 255
237  */
238 GdkPixbuf *ui_pixbuf_scale_alpha ( GdkPixbuf *pixbuf, guint8 alpha )
239 {
240   guchar *pixels;
241   gint width, height, iii, jjj;
242
243   if ( ! gdk_pixbuf_get_has_alpha ( pixbuf ) )
244   {
245     GdkPixbuf *tmp = gdk_pixbuf_add_alpha(pixbuf,FALSE,0,0,0);
246     g_object_unref(G_OBJECT(pixbuf));
247     pixbuf = tmp;
248     if ( !pixbuf )
249       return NULL;
250   }
251
252   pixels = gdk_pixbuf_get_pixels(pixbuf);
253   width = gdk_pixbuf_get_width(pixbuf);
254   height = gdk_pixbuf_get_height(pixbuf);
255
256   /* r,g,b,a,r,g,b,a.... */
257   for (iii = 0; iii < width; iii++) for (jjj = 0; jjj < height; jjj++)
258   {
259     pixels += 3;
260     if ( *pixels != 0 )
261       *pixels = (guint8)(((guint16)*pixels * (guint16)alpha) / 255);
262     pixels++;
263   }
264   return pixbuf;
265 }
266
267
268
269 /**
270  *
271  */
272 void ui_add_recent_file ( const gchar *filename )
273 {
274         if ( filename ) {
275                 GtkRecentManager *manager = gtk_recent_manager_get_default();
276                 GFile *file = g_file_new_for_commandline_arg ( filename );
277                 gchar *uri = g_file_get_uri ( file );
278                 if ( uri && manager )
279                         gtk_recent_manager_add_item ( manager, uri );
280                 g_object_unref( file );
281                 g_free (uri);
282         }
283 }
284
285 /**
286  * Clear the entry text if the specified icon is pressed
287  */
288 static void ui_icon_clear_entry ( GtkEntry             *entry,
289                                   GtkEntryIconPosition position,
290                                   GdkEventButton       *event,
291                                   gpointer             data )
292 {
293         if ( position == GPOINTER_TO_INT(data) )
294                 gtk_entry_set_text ( entry, "" );
295 }
296
297 static void
298 text_changed_cb (GtkEntry   *entry,
299                  GParamSpec *pspec,
300                  gpointer   data)
301 {
302         if ( data ) {
303                 gboolean has_text = gtk_entry_get_text_length(entry) > 0;
304                 gtk_entry_set_icon_sensitive ( entry, GPOINTER_TO_INT(data), has_text );
305         }
306 }
307
308 /**
309  * Create an entry field with an icon to clear the entry
310  *
311  * Ideal for entries used for getting user entered transitory data,
312  *  so it is easy to delete the text and start again.
313  */
314 GtkWidget *ui_entry_new ( const gchar *str, GtkEntryIconPosition position )
315 {
316         GtkWidget *entry = gtk_entry_new();
317         if ( str )
318                 gtk_entry_set_text ( GTK_ENTRY(entry), str );
319         gtk_entry_set_icon_from_stock ( GTK_ENTRY(entry), position, GTK_STOCK_CLEAR );
320 #if GTK_CHECK_VERSION (2,20,0)
321         text_changed_cb ( GTK_ENTRY(entry), NULL, GINT_TO_POINTER(position) );
322         g_signal_connect ( entry, "notify::text", G_CALLBACK(text_changed_cb), GINT_TO_POINTER(position) );
323 #endif
324         g_signal_connect ( entry, "icon-release", G_CALLBACK(ui_icon_clear_entry), GINT_TO_POINTER(position) );
325         return entry;
326 }
327
328 /**
329  * Create a spinbutton with an icon to clear the entry
330  *
331  * Ideal for entries used for getting user entered transitory data,
332  *  so it is easy to delete the number and start again.
333  */
334 GtkWidget *ui_spin_button_new ( GtkAdjustment *adjustment,
335                                 gdouble climb_rate,
336                                 guint digits )
337 {
338         GtkWidget *spin = gtk_spin_button_new ( adjustment, climb_rate, digits );
339         gtk_entry_set_icon_from_stock ( GTK_ENTRY(spin), GTK_ENTRY_ICON_PRIMARY, GTK_STOCK_CLEAR );
340         g_signal_connect ( spin, "icon-release", G_CALLBACK(ui_icon_clear_entry), GINT_TO_POINTER(GTK_ENTRY_ICON_PRIMARY) );
341         return spin;
342 }