]> git.street.me.uk Git - andy/viking.git/blame - src/uibuilder.c
Minimum GTK+2.14 required for the build.
[andy/viking.git] / src / uibuilder.c
CommitLineData
f33aa4e0
EB
1/*
2 * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
3 *
4 * Copyright (C) 2003-2007, Evan Battaglia <gtoevan@gmx.net>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 */
d9d1084e
GB
21#ifdef HAVE_CONFIG_H
22#include "config.h"
23#endif
24
f33aa4e0 25#include <gtk/gtk.h>
d9d1084e 26#include <glib/gi18n.h>
f33aa4e0
EB
27#include "uibuilder.h"
28#include "vikradiogroup.h"
29#include "vikfileentry.h"
30#include "vikfilelist.h"
31
a7023a1b
RN
32VikLayerParamData vik_lpd_true_default ( void ) { return VIK_LPD_BOOLEAN ( TRUE ); }
33VikLayerParamData vik_lpd_false_default ( void ) { return VIK_LPD_BOOLEAN ( FALSE ); }
34
f33aa4e0
EB
35GtkWidget *a_uibuilder_new_widget ( VikLayerParam *param, VikLayerParamData data )
36{
37 GtkWidget *rv = NULL;
38 switch ( param->widget_type )
39 {
40 case VIK_LAYER_WIDGET_COLOR:
41 if ( param->type == VIK_LAYER_PARAM_COLOR )
42 rv = gtk_color_button_new_with_color ( &(data.c) );
43 break;
44 case VIK_LAYER_WIDGET_CHECKBUTTON:
45 if ( param->type == VIK_LAYER_PARAM_BOOLEAN )
46 {
47 //rv = gtk_check_button_new_with_label ( //param->title );
48 rv = gtk_check_button_new ();
49 if ( data.b )
50 gtk_toggle_button_set_active ( GTK_TOGGLE_BUTTON(rv), TRUE );
51 }
52 break;
53 case VIK_LAYER_WIDGET_COMBOBOX:
54#ifndef GTK_2_2
55 if ( param->type == VIK_LAYER_PARAM_UINT && param->widget_data )
56 {
57 gchar **pstr = param->widget_data;
58 rv = gtk_combo_box_new_text ();
59 while ( *pstr )
60 gtk_combo_box_append_text ( GTK_COMBO_BOX ( rv ), *(pstr++) );
61 if ( param->extra_widget_data ) /* map of alternate uint values for options */
62 {
63 int i;
64 for ( i = 0; ((const char **)param->widget_data)[i]; i++ )
65 if ( ((guint *)param->extra_widget_data)[i] == data.u )
66 {
67 gtk_combo_box_set_active ( GTK_COMBO_BOX(rv), i );
68 break;
69 }
70 }
a8876892
GB
71 else
72 gtk_combo_box_set_active ( GTK_COMBO_BOX ( rv ), data.u );
f33aa4e0 73 }
ce37ab9b
GB
74 else if ( param->type == VIK_LAYER_PARAM_STRING && param->widget_data )
75 {
76 gchar **pstr = param->widget_data;
a8876892 77 rv = GTK_WIDGET ( gtk_combo_box_entry_new_text () );
ce37ab9b
GB
78 if ( data.s )
79 gtk_combo_box_append_text ( GTK_COMBO_BOX ( rv ), data.s );
80 while ( *pstr )
81 gtk_combo_box_append_text ( GTK_COMBO_BOX ( rv ), *(pstr++) );
82 if ( data.s )
83 gtk_combo_box_set_active ( GTK_COMBO_BOX ( rv ), 0 );
84 }
f33aa4e0
EB
85 break;
86#endif
87 case VIK_LAYER_WIDGET_RADIOGROUP:
88 /* widget_data and extra_widget_data are GList */
89 if ( param->type == VIK_LAYER_PARAM_UINT && param->widget_data )
90 {
91 rv = vik_radio_group_new ( param->widget_data );
92 if ( param->extra_widget_data ) /* map of alternate uint values for options */
93 {
94 int i;
a8876892 95 int nb_elem = g_list_length(param->widget_data);
f33aa4e0 96 for ( i = 0; i < nb_elem; i++ )
dc2c040e 97 if ( GPOINTER_TO_UINT ( g_list_nth_data(param->extra_widget_data, i) ) == data.u )
f33aa4e0
EB
98 {
99 vik_radio_group_set_selected ( VIK_RADIO_GROUP(rv), i );
100 break;
101 }
102 }
103 else if ( data.u ) /* zero is already default */
104 vik_radio_group_set_selected ( VIK_RADIO_GROUP(rv), data.u );
105 }
106 break;
107 case VIK_LAYER_WIDGET_RADIOGROUP_STATIC:
108 if ( param->type == VIK_LAYER_PARAM_UINT && param->widget_data )
109 {
110 rv = vik_radio_group_new_static ( (const gchar **) param->widget_data );
111 if ( param->extra_widget_data ) /* map of alternate uint values for options */
112 {
113 int i;
114 for ( i = 0; ((const char **)param->widget_data)[i]; i++ )
115 if ( ((guint *)param->extra_widget_data)[i] == data.u )
116 {
117 vik_radio_group_set_selected ( VIK_RADIO_GROUP(rv), i );
118 break;
119 }
120 }
121 else if ( data.u ) /* zero is already default */
122 vik_radio_group_set_selected ( VIK_RADIO_GROUP(rv), data.u );
123 }
124 break;
125 case VIK_LAYER_WIDGET_SPINBUTTON:
126 if ( (param->type == VIK_LAYER_PARAM_DOUBLE || param->type == VIK_LAYER_PARAM_UINT
127 || param->type == VIK_LAYER_PARAM_INT) && param->widget_data )
128 {
129 gdouble init_val = (param->type == VIK_LAYER_PARAM_DOUBLE) ? data.d : (param->type == VIK_LAYER_PARAM_UINT ? data.u : data.i);
130 VikLayerParamScale *scale = (VikLayerParamScale *) param->widget_data;
ac33062d 131 rv = gtk_spin_button_new ( GTK_ADJUSTMENT(gtk_adjustment_new( init_val, scale->min, scale->max, scale->step, scale->step, 0 )), scale->step, scale->digits );
f33aa4e0
EB
132 }
133 break;
134 case VIK_LAYER_WIDGET_ENTRY:
135 if ( param->type == VIK_LAYER_PARAM_STRING )
136 {
137 rv = gtk_entry_new ();
2f9b9d48
GS
138 if (data.s)
139 gtk_entry_set_text ( GTK_ENTRY(rv), data.s );
f33aa4e0
EB
140 }
141 break;
d9d1084e
GB
142 case VIK_LAYER_WIDGET_PASSWORD:
143 if ( param->type == VIK_LAYER_PARAM_STRING )
144 {
145 rv = gtk_entry_new ();
146 gtk_entry_set_visibility ( GTK_ENTRY(rv), FALSE );
2f9b9d48
GS
147 if (data.s)
148 gtk_entry_set_text ( GTK_ENTRY(rv), data.s );
e6994d8d
RN
149 gtk_widget_set_tooltip_text ( GTK_WIDGET(rv),
150 _("Take care that this password will be stored clearly in a plain file.") );
d9d1084e
GB
151 }
152 break;
f33aa4e0
EB
153 case VIK_LAYER_WIDGET_FILEENTRY:
154 if ( param->type == VIK_LAYER_PARAM_STRING )
155 {
79672b85 156 rv = vik_file_entry_new (GTK_FILE_CHOOSER_ACTION_OPEN);
f33aa4e0
EB
157 vik_file_entry_set_filename ( VIK_FILE_ENTRY(rv), data.s );
158 }
159 break;
79672b85
JJ
160 case VIK_LAYER_WIDGET_FOLDERENTRY:
161 if ( param->type == VIK_LAYER_PARAM_STRING )
162 {
163 rv = vik_file_entry_new (GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER);
164 vik_file_entry_set_filename ( VIK_FILE_ENTRY(rv), data.s );
165 }
166 break;
167
f33aa4e0
EB
168 case VIK_LAYER_WIDGET_FILELIST:
169 if ( param->type == VIK_LAYER_PARAM_STRING_LIST )
170 {
0d109127 171 rv = vik_file_list_new ( _(param->title) );
f33aa4e0
EB
172 vik_file_list_set_files ( VIK_FILE_LIST(rv), data.sl );
173 }
174 break;
175 case VIK_LAYER_WIDGET_HSCALE:
176 if ( (param->type == VIK_LAYER_PARAM_DOUBLE || param->type == VIK_LAYER_PARAM_UINT
177 || param->type == VIK_LAYER_PARAM_INT) && param->widget_data )
178 {
179 gdouble init_val = (param->type == VIK_LAYER_PARAM_DOUBLE) ? data.d : (param->type == VIK_LAYER_PARAM_UINT ? data.u : data.i);
180 VikLayerParamScale *scale = (VikLayerParamScale *) param->widget_data;
181 rv = gtk_hscale_new_with_range ( scale->min, scale->max, scale->step );
182 gtk_scale_set_digits ( GTK_SCALE(rv), scale->digits );
183 gtk_range_set_value ( GTK_RANGE(rv), init_val );
184 }
185 }
e6994d8d
RN
186 if ( rv && !gtk_widget_get_tooltip_text ( rv ) ) {
187 if ( param->tooltip )
188 gtk_widget_set_tooltip_text ( rv, _(param->tooltip) );
189 }
f33aa4e0
EB
190 return rv;
191}
192
193VikLayerParamData a_uibuilder_widget_get_value ( GtkWidget *widget, VikLayerParam *param )
194{
195 VikLayerParamData rv;
196 switch ( param->widget_type )
197 {
198 case VIK_LAYER_WIDGET_COLOR:
199 gtk_color_button_get_color ( GTK_COLOR_BUTTON(widget), &(rv.c) );
200 break;
201 case VIK_LAYER_WIDGET_CHECKBUTTON:
202 rv.b = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget));
203 break;
204 case VIK_LAYER_WIDGET_COMBOBOX:
205#ifndef GTK_2_2
ce37ab9b
GB
206 if ( param->type == VIK_LAYER_PARAM_UINT )
207 {
208 rv.i = gtk_combo_box_get_active ( GTK_COMBO_BOX(widget) );
209 if ( rv.i == -1 ) rv.i = 0;
210 rv.u = rv.i;
211 if ( param->extra_widget_data )
212 rv.u = ((guint *)param->extra_widget_data)[rv.u];
213 }
214 if ( param->type == VIK_LAYER_PARAM_STRING)
215 {
216 rv.s = gtk_combo_box_get_active_text ( GTK_COMBO_BOX(widget) );
217 g_debug("%s: %s", __FUNCTION__, rv.s);
218 }
f33aa4e0
EB
219 break;
220#endif
221 case VIK_LAYER_WIDGET_RADIOGROUP:
222 case VIK_LAYER_WIDGET_RADIOGROUP_STATIC:
223 rv.u = vik_radio_group_get_selected(VIK_RADIO_GROUP(widget));
224 if ( param->extra_widget_data )
dc2c040e 225 rv.u = GPOINTER_TO_UINT ( g_list_nth_data(param->extra_widget_data, rv.u) );
f33aa4e0
EB
226 break;
227 case VIK_LAYER_WIDGET_SPINBUTTON:
228 if ( param->type == VIK_LAYER_PARAM_UINT )
229 rv.u = gtk_spin_button_get_value_as_int ( GTK_SPIN_BUTTON(widget) );
230 else if ( param->type == VIK_LAYER_PARAM_INT )
231 rv.i = gtk_spin_button_get_value_as_int ( GTK_SPIN_BUTTON(widget) );
232 else
233 rv.d = gtk_spin_button_get_value ( GTK_SPIN_BUTTON(widget) );
234 break;
235 case VIK_LAYER_WIDGET_ENTRY:
d9d1084e 236 case VIK_LAYER_WIDGET_PASSWORD:
f33aa4e0
EB
237 rv.s = gtk_entry_get_text ( GTK_ENTRY(widget) );
238 break;
239 case VIK_LAYER_WIDGET_FILEENTRY:
79672b85 240 case VIK_LAYER_WIDGET_FOLDERENTRY:
f33aa4e0
EB
241 rv.s = vik_file_entry_get_filename ( VIK_FILE_ENTRY(widget) );
242 break;
243 case VIK_LAYER_WIDGET_FILELIST:
244 rv.sl = vik_file_list_get_files ( VIK_FILE_LIST(widget) );
245 break;
246 case VIK_LAYER_WIDGET_HSCALE:
247 if ( param->type == VIK_LAYER_PARAM_UINT )
248 rv.u = (guint32) gtk_range_get_value ( GTK_RANGE(widget) );
249 else if ( param->type == VIK_LAYER_PARAM_INT )
250 rv.i = (gint32) gtk_range_get_value ( GTK_RANGE(widget) );
251 else
252 rv.d = gtk_range_get_value ( GTK_RANGE(widget) );
253 break;
254 }
255 return rv;
256}
257
13fde155 258gint a_uibuilder_properties_factory ( const gchar *dialog_name, GtkWindow *parent, VikLayerParam *params,
158b3642
RN
259 guint16 params_count, gchar **groups, guint8 groups_count,
260 gboolean (*setparam) (gpointer,guint16,VikLayerParamData,gpointer,gboolean),
261 gpointer pass_along1, gpointer pass_along2,
262 VikLayerParamData (*getparam) (gpointer,guint16,gboolean),
263 gpointer pass_along_getparam )
264 /* pass_along1 and pass_along2 are for set_param first and last params */
f33aa4e0
EB
265{
266 guint16 i, j, widget_count = 0;
267 gboolean must_redraw = FALSE;
268
269 if ( ! params )
270 return 1; /* no params == no options, so all is good */
271
272 for ( i = 0; i < params_count; i++ )
273 if ( params[i].group != VIK_LAYER_NOT_IN_PROPERTIES )
274 widget_count++;
275
276 if ( widget_count == 0)
277 return 0; /* TODO -- should be one? */
278 else
279 {
280 /* create widgets and titles; place in table */
13fde155 281 GtkWidget *dialog = gtk_dialog_new_with_buttons ( dialog_name,
9be0449f
RN
282 parent,
283 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
284 GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
285 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT, NULL );
f9d4faa9
RN
286 gtk_dialog_set_default_response ( GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT );
287 GtkWidget *response_w = NULL;
288#if GTK_CHECK_VERSION (2, 20, 0)
289 response_w = gtk_dialog_get_widget_for_response ( GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT );
290#endif
f33aa4e0
EB
291 gint resp;
292
f33aa4e0
EB
293 GtkWidget *table = NULL;
294 GtkWidget **tables = NULL; /* for more than one group */
295
296 GtkWidget *notebook = NULL;
297 GtkWidget **widgets = g_malloc ( sizeof(GtkWidget *) * widget_count );
298
299 if ( groups && groups_count > 1 )
300 {
301 guint8 current_group;
302 guint16 tab_widget_count;
303 notebook = gtk_notebook_new ();
9b082b39 304 gtk_box_pack_start (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), notebook, FALSE, FALSE, 0);
f33aa4e0
EB
305 tables = g_malloc ( sizeof(GtkWidget *) * groups_count );
306 for ( current_group = 0; current_group < groups_count; current_group++ )
307 {
308 tab_widget_count = 0;
309 for ( j = 0; j < params_count; j ++ )
310 if ( params[j].group == current_group )
311 tab_widget_count++;
312
313 if ( tab_widget_count )
314 {
315 tables[current_group] = gtk_table_new ( tab_widget_count, 1, FALSE );
316 gtk_notebook_append_page ( GTK_NOTEBOOK(notebook), tables[current_group], gtk_label_new(groups[current_group]) );
317 }
318 }
319 }
320 else
321 {
322 table = gtk_table_new( widget_count, 1, FALSE );
9b082b39 323 gtk_box_pack_start (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), table, FALSE, FALSE, 0);
f33aa4e0
EB
324 }
325
326 for ( i = 0, j = 0; i < params_count; i++ )
327 {
328 if ( params[i].group != VIK_LAYER_NOT_IN_PROPERTIES )
329 {
330 if ( tables )
331 table = tables[MAX(0, params[i].group)]; /* round up NOT_IN_GROUP, that's not reasonable here */
332
158b3642 333 widgets[j] = a_uibuilder_new_widget ( &(params[i]), getparam ( pass_along_getparam, i, FALSE ) );
f33aa4e0 334
a7023a1b
RN
335 if ( widgets[j] ) {
336 gtk_table_attach ( GTK_TABLE(table), gtk_label_new(_(params[i].title)), 0, 1, j, j+1, 0, 0, 0, 0 );
337 gtk_table_attach ( GTK_TABLE(table), widgets[j], 1, 2, j, j+1, GTK_EXPAND | GTK_FILL, 0, 2, 2 );
338 }
f33aa4e0
EB
339 j++;
340 }
341 }
342
f9d4faa9
RN
343 if ( response_w )
344 gtk_widget_grab_focus ( response_w );
345
f33aa4e0
EB
346 gtk_widget_show_all ( dialog );
347
348 resp = gtk_dialog_run (GTK_DIALOG (dialog));
349 if ( resp == GTK_RESPONSE_ACCEPT )
350 {
351 for ( i = 0, j = 0; i < params_count; i++ )
352 {
353 if ( params[i].group != VIK_LAYER_NOT_IN_PROPERTIES )
354 {
158b3642
RN
355 if ( setparam ( pass_along1,
356 i,
357 a_uibuilder_widget_get_value ( widgets[j], &(params[i]) ),
358 pass_along2,
359 FALSE ) )
f33aa4e0
EB
360 must_redraw = TRUE;
361 j++;
362 }
363 }
364
365 gtk_widget_destroy ( dialog ); /* hide before redrawing. */
366 g_free ( widgets );
74ace2b1
RN
367 if ( tables )
368 g_free ( tables );
f33aa4e0
EB
369
370 return must_redraw ? 2 : 3; /* user clicked OK */
371 }
372
373 if ( tables )
374 g_free ( tables );
375 gtk_widget_destroy ( dialog );
376 g_free ( widgets );
377 return 0;
378 }
379}
380
381
382static void uibuilder_run_setparam ( VikLayerParamData *paramdatas, guint16 i, VikLayerParamData data, VikLayerParam *params )
383{
384 /* could have to copy it if it's a string! */
385 switch ( params[i].type ) {
386 case VIK_LAYER_PARAM_STRING:
387 paramdatas[i].s = g_strdup ( data.s );
388 break;
389 default:
390 paramdatas[i] = data; /* string list will have to be freed by layer. anything else not freed */
391 }
392}
393
394static VikLayerParamData uibuilder_run_getparam ( VikLayerParamData *params_defaults, guint16 i )
395{
396 return params_defaults[i];
397}
398
399
13fde155 400VikLayerParamData *a_uibuilder_run_dialog ( const gchar *dialog_name, GtkWindow *parent, VikLayerParam *params,
f33aa4e0
EB
401 guint16 params_count, gchar **groups, guint8 groups_count,
402 VikLayerParamData *params_defaults )
403{
404 VikLayerParamData *paramdatas = g_new(VikLayerParamData, params_count);
13fde155
RN
405 if ( a_uibuilder_properties_factory ( dialog_name,
406 parent,
dc2c040e
RN
407 params,
408 params_count,
409 groups,
410 groups_count,
411 (gpointer) uibuilder_run_setparam,
412 paramdatas,
413 params,
414 (gpointer) uibuilder_run_getparam,
415 params_defaults ) > 0 ) {
f33aa4e0
EB
416
417 return paramdatas;
418 }
419 g_free ( paramdatas );
420 return NULL;
421}
422
423/* frees data from last (if ness) */
424void a_uibuilder_free_paramdatas ( VikLayerParamData *paramdatas, VikLayerParam *params, guint16 params_count )
425{
426 int i;
427 /* may have to free strings, etc. */
428 for ( i = 0; i < params_count; i++ ) {
429 switch ( params[i].type ) {
430 case VIK_LAYER_PARAM_STRING:
431 g_free ( (gchar *) paramdatas[i].s );
432 break;
433 case VIK_LAYER_PARAM_STRING_LIST: {
434 /* should make a util function out of this */
435 GList *iter = paramdatas[i].sl;
436 while ( iter ) {
437 g_free ( iter->data );
438 iter = iter->next;
439 }
440 g_list_free ( paramdatas[i].sl );
441 break;
b5926b35
RN
442 default:
443 break;
f33aa4e0
EB
444 }
445 }
446 }
447 g_free ( paramdatas );
448}