]> git.street.me.uk Git - andy/viking.git/blame - src/ui_util.c
Save bfilter values used for subsequent reuse in a session.
[andy/viking.git] / src / ui_util.c
CommitLineData
bc34c059
RN
1/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
2/*
3 * Viking - GPS data editor
4 * Copyright (C) 2007, Guilhem Bonnefille <guilhem.bonnefille@gmail.com>
5 * Copyright (C) 2014, Rob Norris <rw_norris@hotmail.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, version 2.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20 /*
21 * Ideally dependencies should just be on Gtk,
22 * see vikutils for things that further depend on other Viking types
23 * see utils for things only depend on Glib
24 */
25#ifdef HAVE_CONFIG_H
26#include "config.h"
27#endif
28
29#include <glib/gstdio.h>
30#include <glib/gi18n.h>
31#include <glib/gprintf.h>
32
33#include "util.h"
34#include "dialog.h"
35
36#ifdef WINDOWS
37#include <windows.h>
38#endif
39
40/*
41#ifndef WINDOWS
42static gboolean spawn_command_line_async(const gchar * cmd,
43 const gchar * arg)
44{
45 gchar *cmdline = NULL;
46 gboolean status;
47
48 cmdline = g_strdup_printf("%s '%s'", cmd, arg);
49 g_debug("Running: %s", cmdline);
50
51 status = g_spawn_command_line_async(cmdline, NULL);
52
53 g_free(cmdline);
54
55 return status;
56}
57#endif
58*/
59
60// Annoyingly gtk_show_uri() doesn't work so resort to ShellExecute method
61// (non working at least in our Windows build with GTK+2.24.10 on Windows 7)
62
63void open_url(GtkWindow *parent, const gchar * url)
64{
65#ifdef WINDOWS
66 ShellExecute(NULL, NULL, (char *) url, NULL, ".\\", 0);
67#else
68 GError *error = NULL;
69 gtk_show_uri ( gtk_widget_get_screen (GTK_WIDGET(parent)), url, GDK_CURRENT_TIME, &error );
70 if ( error ) {
71 a_dialog_error_msg_extra ( parent, _("Could not launch web browser. %s"), error->message );
72 g_error_free ( error );
73 }
74#endif
75}
76
77void new_email(GtkWindow *parent, const gchar * address)
78{
79 gchar *uri = g_strdup_printf("mailto:%s", address);
80 GError *error = NULL;
81 gtk_show_uri ( gtk_widget_get_screen (GTK_WIDGET(parent)), uri, GDK_CURRENT_TIME, &error );
82 if ( error ) {
83 a_dialog_error_msg_extra ( parent, _("Could not create new email. %s"), error->message );
84 g_error_free ( error );
85 }
86 /*
87#ifdef WINDOWS
88 ShellExecute(NULL, NULL, (char *) uri, NULL, ".\\", 0);
89#else
90 if (!spawn_command_line_async("xdg-email", uri))
91 a_dialog_error_msg ( parent, _("Could not create new email.") );
92#endif
93 */
94 g_free(uri);
95 uri = NULL;
96}
ec77010a
RN
97
98/** Creates a @c GtkButton with custom text and a stock image similar to
99 * @c gtk_button_new_from_stock().
100 * @param stock_id A @c GTK_STOCK_NAME string.
101 * @param text Button label text, can include mnemonics.
102 * @return The new @c GtkButton.
103 */
104GtkWidget *ui_button_new_with_image(const gchar *stock_id, const gchar *text)
105{
106 GtkWidget *image, *button;
107
108 button = gtk_button_new_with_mnemonic(text);
109 gtk_widget_show(button);
110 image = gtk_image_new_from_stock(stock_id, GTK_ICON_SIZE_BUTTON);
111 gtk_button_set_image(GTK_BUTTON(button), image);
112 // note: image is shown by gtk
113 return button;
114}
115
116/** Reads an integer from the GTK default settings registry
117 * (see http://library.gnome.org/devel/gtk/stable/GtkSettings.html).
118 * @param property_name The property to read.
119 * @param default_value The default value in case the value could not be read.
120 * @return The value for the property if it exists, otherwise the @a default_value.
121 */
122gint ui_get_gtk_settings_integer(const gchar *property_name, gint default_value)
123{
124 if (g_object_class_find_property(G_OBJECT_GET_CLASS(G_OBJECT(
125 gtk_settings_get_default())), property_name))
126 {
127 gint value;
128 g_object_get(G_OBJECT(gtk_settings_get_default()), property_name, &value, NULL);
129 return value;
130 }
131 else
132 return default_value;
133}
134
135
136/** Returns a widget from a name in a component, usually created by Glade.
137 * Call it with the toplevel widget in the component (i.e. a window/dialog),
138 * or alternatively any widget in the component, and the name of the widget
139 * you want returned.
140 * @param widget Widget with the @a widget_name property set.
141 * @param widget_name Name to lookup.
142 * @return The widget found.
143 * @see ui_hookup_widget().
144 *
145 */
146GtkWidget *ui_lookup_widget(GtkWidget *widget, const gchar *widget_name)
147{
148 GtkWidget *parent, *found_widget;
149
150 g_return_val_if_fail(widget != NULL, NULL);
151 g_return_val_if_fail(widget_name != NULL, NULL);
152
153 for (;;)
154 {
155 if (GTK_IS_MENU(widget))
156 parent = gtk_menu_get_attach_widget(GTK_MENU(widget));
157 else
158 parent = gtk_widget_get_parent(widget);
159 if (parent == NULL)
160 parent = (GtkWidget*) g_object_get_data(G_OBJECT(widget), "GladeParentKey");
161 if (parent == NULL)
162 break;
163 widget = parent;
164 }
165
166 found_widget = (GtkWidget*) g_object_get_data(G_OBJECT(widget), widget_name);
167 if (G_UNLIKELY(found_widget == NULL))
168 g_warning("Widget not found: %s", widget_name);
169 return found_widget;
170}
5cca6c53 171
491f06f2
RN
172/**
173 * Returns a label widget that is made selectable (i.e. the user can copy the text)
174 * @param text String to display - maybe NULL
175 * @return The label widget
176 */
177GtkWidget* ui_label_new_selectable ( const gchar* text )
178{
179 GtkWidget *widget = gtk_label_new ( text );
180 gtk_label_set_selectable ( GTK_LABEL(widget), TRUE );
181 return widget;
182}
183
5cca6c53
RN
184/**
185 * Apply the alpha value to the specified pixbuf
186 */
187GdkPixbuf *ui_pixbuf_set_alpha ( GdkPixbuf *pixbuf, guint8 alpha )
188{
189 guchar *pixels;
190 gint width, height, iii, jjj;
191
192 if ( ! gdk_pixbuf_get_has_alpha ( pixbuf ) )
193 {
194 GdkPixbuf *tmp = gdk_pixbuf_add_alpha(pixbuf,FALSE,0,0,0);
195 g_object_unref(G_OBJECT(pixbuf));
196 pixbuf = tmp;
197 if ( !pixbuf )
198 return NULL;
199 }
200
201 pixels = gdk_pixbuf_get_pixels(pixbuf);
202 width = gdk_pixbuf_get_width(pixbuf);
203 height = gdk_pixbuf_get_height(pixbuf);
204
205 /* r,g,b,a,r,g,b,a.... */
206 for (iii = 0; iii < width; iii++) for (jjj = 0; jjj < height; jjj++)
207 {
208 pixels += 3;
209 *pixels++ = alpha;
210 }
211 return pixbuf;
212}
a4b5fc11 213
53991e79
MH
214
215/**
216 * Reduce the alpha value of the specified pixbuf by alpha / 255
217 */
218GdkPixbuf *ui_pixbuf_scale_alpha ( GdkPixbuf *pixbuf, guint8 alpha )
219{
220 guchar *pixels;
221 gint width, height, iii, jjj;
222
223 if ( ! gdk_pixbuf_get_has_alpha ( pixbuf ) )
224 {
225 GdkPixbuf *tmp = gdk_pixbuf_add_alpha(pixbuf,FALSE,0,0,0);
226 g_object_unref(G_OBJECT(pixbuf));
227 pixbuf = tmp;
228 if ( !pixbuf )
229 return NULL;
230 }
231
232 pixels = gdk_pixbuf_get_pixels(pixbuf);
233 width = gdk_pixbuf_get_width(pixbuf);
234 height = gdk_pixbuf_get_height(pixbuf);
235
236 /* r,g,b,a,r,g,b,a.... */
237 for (iii = 0; iii < width; iii++) for (jjj = 0; jjj < height; jjj++)
238 {
239 pixels += 3;
240 *pixels = (guint8)(((guint16)*pixels * (guint16)alpha) / 255);
241 pixels++;
242 }
243 return pixbuf;
244}
245
246
247
a4b5fc11
RN
248/**
249 *
250 */
251void ui_add_recent_file ( const gchar *filename )
252{
253 if ( filename ) {
254 GtkRecentManager *manager = gtk_recent_manager_get_default();
255 GFile *file = g_file_new_for_commandline_arg ( filename );
256 gchar *uri = g_file_get_uri ( file );
257 if ( uri && manager )
258 gtk_recent_manager_add_item ( manager, uri );
259 g_object_unref( file );
260 g_free (uri);
261 }
262}