]> git.street.me.uk Git - andy/viking.git/blame - src/uibuilder.c
[DOC] Fix some incorrect docbook markup.
[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:
f33aa4e0
EB
54 if ( param->type == VIK_LAYER_PARAM_UINT && param->widget_data )
55 {
77a62382 56 /* Build a simple combobox */
f33aa4e0 57 gchar **pstr = param->widget_data;
1bc1c05b 58 rv = vik_combo_box_text_new ();
f33aa4e0 59 while ( *pstr )
1bc1c05b 60 vik_combo_box_text_append ( rv, *(pstr++) );
f33aa4e0
EB
61 if ( param->extra_widget_data ) /* map of alternate uint values for options */
62 {
77a62382 63 /* Set the effective default value */
f33aa4e0
EB
64 int i;
65 for ( i = 0; ((const char **)param->widget_data)[i]; i++ )
66 if ( ((guint *)param->extra_widget_data)[i] == data.u )
67 {
77a62382 68 /* Match default value */
f33aa4e0
EB
69 gtk_combo_box_set_active ( GTK_COMBO_BOX(rv), i );
70 break;
71 }
72 }
a8876892
GB
73 else
74 gtk_combo_box_set_active ( GTK_COMBO_BOX ( rv ), data.u );
f33aa4e0 75 }
77a62382 76 else if ( param->type == VIK_LAYER_PARAM_STRING && param->widget_data && !param->extra_widget_data )
ce37ab9b 77 {
77a62382 78 /* Build a combobox with editable text */
ce37ab9b 79 gchar **pstr = param->widget_data;
1bc1c05b
RN
80#if GTK_CHECK_VERSION (2, 24, 0)
81 rv = gtk_combo_box_text_new_with_entry ();
82#else
83 rv = gtk_combo_box_entry_new_text ();
84#endif
ce37ab9b 85 if ( data.s )
1bc1c05b 86 vik_combo_box_text_append ( rv, data.s );
ce37ab9b 87 while ( *pstr )
1bc1c05b 88 vik_combo_box_text_append ( rv, *(pstr++) );
ce37ab9b
GB
89 if ( data.s )
90 gtk_combo_box_set_active ( GTK_COMBO_BOX ( rv ), 0 );
91 }
77a62382
GB
92 else if ( param->type == VIK_LAYER_PARAM_STRING && param->widget_data && param->extra_widget_data)
93 {
94 /* Build a combobox with fixed selections without editable text */
95 gchar **pstr = param->widget_data;
96 rv = GTK_WIDGET ( vik_combo_box_text_new () );
97 while ( *pstr )
98 vik_combo_box_text_append ( rv, *(pstr++) );
99 if ( data.s )
100 {
101 /* Set the effective default value */
102 /* In case of value does not exist, set the first value */
103 gtk_combo_box_set_active ( GTK_COMBO_BOX ( rv ), 0 );
104 int i;
105 for ( i = 0; ((const char **)param->widget_data)[i]; i++ )
106 if ( strcmp(((const char **)param->extra_widget_data)[i], data.s) == 0 )
107 {
108 /* Match default value */
109 gtk_combo_box_set_active ( GTK_COMBO_BOX ( rv ), i );
110 break;
111 }
112 }
113 else
114 gtk_combo_box_set_active ( GTK_COMBO_BOX ( rv ), 0 );
115 }
f33aa4e0 116 break;
f33aa4e0
EB
117 case VIK_LAYER_WIDGET_RADIOGROUP:
118 /* widget_data and extra_widget_data are GList */
119 if ( param->type == VIK_LAYER_PARAM_UINT && param->widget_data )
120 {
121 rv = vik_radio_group_new ( param->widget_data );
122 if ( param->extra_widget_data ) /* map of alternate uint values for options */
123 {
124 int i;
a8876892 125 int nb_elem = g_list_length(param->widget_data);
f33aa4e0 126 for ( i = 0; i < nb_elem; i++ )
dc2c040e 127 if ( GPOINTER_TO_UINT ( g_list_nth_data(param->extra_widget_data, i) ) == data.u )
f33aa4e0
EB
128 {
129 vik_radio_group_set_selected ( VIK_RADIO_GROUP(rv), i );
130 break;
131 }
132 }
133 else if ( data.u ) /* zero is already default */
134 vik_radio_group_set_selected ( VIK_RADIO_GROUP(rv), data.u );
135 }
136 break;
137 case VIK_LAYER_WIDGET_RADIOGROUP_STATIC:
138 if ( param->type == VIK_LAYER_PARAM_UINT && param->widget_data )
139 {
140 rv = vik_radio_group_new_static ( (const gchar **) param->widget_data );
141 if ( param->extra_widget_data ) /* map of alternate uint values for options */
142 {
143 int i;
144 for ( i = 0; ((const char **)param->widget_data)[i]; i++ )
145 if ( ((guint *)param->extra_widget_data)[i] == data.u )
146 {
147 vik_radio_group_set_selected ( VIK_RADIO_GROUP(rv), i );
148 break;
149 }
150 }
151 else if ( data.u ) /* zero is already default */
152 vik_radio_group_set_selected ( VIK_RADIO_GROUP(rv), data.u );
153 }
154 break;
155 case VIK_LAYER_WIDGET_SPINBUTTON:
156 if ( (param->type == VIK_LAYER_PARAM_DOUBLE || param->type == VIK_LAYER_PARAM_UINT
157 || param->type == VIK_LAYER_PARAM_INT) && param->widget_data )
158 {
159 gdouble init_val = (param->type == VIK_LAYER_PARAM_DOUBLE) ? data.d : (param->type == VIK_LAYER_PARAM_UINT ? data.u : data.i);
160 VikLayerParamScale *scale = (VikLayerParamScale *) param->widget_data;
ac33062d 161 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
162 }
163 break;
164 case VIK_LAYER_WIDGET_ENTRY:
165 if ( param->type == VIK_LAYER_PARAM_STRING )
166 {
167 rv = gtk_entry_new ();
2f9b9d48
GS
168 if (data.s)
169 gtk_entry_set_text ( GTK_ENTRY(rv), data.s );
f33aa4e0
EB
170 }
171 break;
d9d1084e
GB
172 case VIK_LAYER_WIDGET_PASSWORD:
173 if ( param->type == VIK_LAYER_PARAM_STRING )
174 {
175 rv = gtk_entry_new ();
176 gtk_entry_set_visibility ( GTK_ENTRY(rv), FALSE );
2f9b9d48
GS
177 if (data.s)
178 gtk_entry_set_text ( GTK_ENTRY(rv), data.s );
e6994d8d
RN
179 gtk_widget_set_tooltip_text ( GTK_WIDGET(rv),
180 _("Take care that this password will be stored clearly in a plain file.") );
d9d1084e
GB
181 }
182 break;
f33aa4e0
EB
183 case VIK_LAYER_WIDGET_FILEENTRY:
184 if ( param->type == VIK_LAYER_PARAM_STRING )
185 {
79672b85 186 rv = vik_file_entry_new (GTK_FILE_CHOOSER_ACTION_OPEN);
fd74cdb9
RN
187 if ( data.s )
188 vik_file_entry_set_filename ( VIK_FILE_ENTRY(rv), data.s );
f33aa4e0
EB
189 }
190 break;
79672b85
JJ
191 case VIK_LAYER_WIDGET_FOLDERENTRY:
192 if ( param->type == VIK_LAYER_PARAM_STRING )
193 {
194 rv = vik_file_entry_new (GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER);
fd74cdb9
RN
195 if ( data.s )
196 vik_file_entry_set_filename ( VIK_FILE_ENTRY(rv), data.s );
79672b85
JJ
197 }
198 break;
199
f33aa4e0
EB
200 case VIK_LAYER_WIDGET_FILELIST:
201 if ( param->type == VIK_LAYER_PARAM_STRING_LIST )
202 {
0d109127 203 rv = vik_file_list_new ( _(param->title) );
f33aa4e0
EB
204 vik_file_list_set_files ( VIK_FILE_LIST(rv), data.sl );
205 }
206 break;
207 case VIK_LAYER_WIDGET_HSCALE:
208 if ( (param->type == VIK_LAYER_PARAM_DOUBLE || param->type == VIK_LAYER_PARAM_UINT
209 || param->type == VIK_LAYER_PARAM_INT) && param->widget_data )
210 {
211 gdouble init_val = (param->type == VIK_LAYER_PARAM_DOUBLE) ? data.d : (param->type == VIK_LAYER_PARAM_UINT ? data.u : data.i);
212 VikLayerParamScale *scale = (VikLayerParamScale *) param->widget_data;
213 rv = gtk_hscale_new_with_range ( scale->min, scale->max, scale->step );
214 gtk_scale_set_digits ( GTK_SCALE(rv), scale->digits );
215 gtk_range_set_value ( GTK_RANGE(rv), init_val );
216 }
217 }
e6994d8d
RN
218 if ( rv && !gtk_widget_get_tooltip_text ( rv ) ) {
219 if ( param->tooltip )
220 gtk_widget_set_tooltip_text ( rv, _(param->tooltip) );
221 }
f33aa4e0
EB
222 return rv;
223}
224
225VikLayerParamData a_uibuilder_widget_get_value ( GtkWidget *widget, VikLayerParam *param )
226{
227 VikLayerParamData rv;
228 switch ( param->widget_type )
229 {
230 case VIK_LAYER_WIDGET_COLOR:
231 gtk_color_button_get_color ( GTK_COLOR_BUTTON(widget), &(rv.c) );
232 break;
233 case VIK_LAYER_WIDGET_CHECKBUTTON:
234 rv.b = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget));
235 break;
236 case VIK_LAYER_WIDGET_COMBOBOX:
ce37ab9b
GB
237 if ( param->type == VIK_LAYER_PARAM_UINT )
238 {
239 rv.i = gtk_combo_box_get_active ( GTK_COMBO_BOX(widget) );
240 if ( rv.i == -1 ) rv.i = 0;
241 rv.u = rv.i;
242 if ( param->extra_widget_data )
243 rv.u = ((guint *)param->extra_widget_data)[rv.u];
244 }
245 if ( param->type == VIK_LAYER_PARAM_STRING)
246 {
77a62382
GB
247 if ( param->extra_widget_data )
248 {
249 /* Combobox displays labels and we want values from extra */
250 int pos = gtk_combo_box_get_active ( GTK_COMBO_BOX(widget) );
251 rv.s = ((const char **)param->extra_widget_data)[pos];
252 }
253 else
254 {
255 /* Return raw value */
1bc1c05b 256#if GTK_CHECK_VERSION (2, 24, 0)
77a62382 257 rv.s = gtk_entry_get_text (GTK_ENTRY (gtk_bin_get_child (GTK_BIN (widget))));
1bc1c05b 258#else
77a62382 259 rv.s = gtk_combo_box_get_active_text ( GTK_COMBO_BOX(widget) );
1bc1c05b 260#endif
77a62382 261 }
ce37ab9b
GB
262 g_debug("%s: %s", __FUNCTION__, rv.s);
263 }
f33aa4e0 264 break;
f33aa4e0
EB
265 case VIK_LAYER_WIDGET_RADIOGROUP:
266 case VIK_LAYER_WIDGET_RADIOGROUP_STATIC:
267 rv.u = vik_radio_group_get_selected(VIK_RADIO_GROUP(widget));
268 if ( param->extra_widget_data )
dc2c040e 269 rv.u = GPOINTER_TO_UINT ( g_list_nth_data(param->extra_widget_data, rv.u) );
f33aa4e0
EB
270 break;
271 case VIK_LAYER_WIDGET_SPINBUTTON:
272 if ( param->type == VIK_LAYER_PARAM_UINT )
273 rv.u = gtk_spin_button_get_value_as_int ( GTK_SPIN_BUTTON(widget) );
274 else if ( param->type == VIK_LAYER_PARAM_INT )
275 rv.i = gtk_spin_button_get_value_as_int ( GTK_SPIN_BUTTON(widget) );
276 else
277 rv.d = gtk_spin_button_get_value ( GTK_SPIN_BUTTON(widget) );
278 break;
279 case VIK_LAYER_WIDGET_ENTRY:
d9d1084e 280 case VIK_LAYER_WIDGET_PASSWORD:
f33aa4e0
EB
281 rv.s = gtk_entry_get_text ( GTK_ENTRY(widget) );
282 break;
283 case VIK_LAYER_WIDGET_FILEENTRY:
79672b85 284 case VIK_LAYER_WIDGET_FOLDERENTRY:
f33aa4e0
EB
285 rv.s = vik_file_entry_get_filename ( VIK_FILE_ENTRY(widget) );
286 break;
287 case VIK_LAYER_WIDGET_FILELIST:
288 rv.sl = vik_file_list_get_files ( VIK_FILE_LIST(widget) );
289 break;
290 case VIK_LAYER_WIDGET_HSCALE:
291 if ( param->type == VIK_LAYER_PARAM_UINT )
292 rv.u = (guint32) gtk_range_get_value ( GTK_RANGE(widget) );
293 else if ( param->type == VIK_LAYER_PARAM_INT )
294 rv.i = (gint32) gtk_range_get_value ( GTK_RANGE(widget) );
295 else
296 rv.d = gtk_range_get_value ( GTK_RANGE(widget) );
297 break;
298 }
299 return rv;
300}
301
13fde155 302gint a_uibuilder_properties_factory ( const gchar *dialog_name, GtkWindow *parent, VikLayerParam *params,
158b3642
RN
303 guint16 params_count, gchar **groups, guint8 groups_count,
304 gboolean (*setparam) (gpointer,guint16,VikLayerParamData,gpointer,gboolean),
305 gpointer pass_along1, gpointer pass_along2,
306 VikLayerParamData (*getparam) (gpointer,guint16,gboolean),
307 gpointer pass_along_getparam )
308 /* pass_along1 and pass_along2 are for set_param first and last params */
f33aa4e0
EB
309{
310 guint16 i, j, widget_count = 0;
311 gboolean must_redraw = FALSE;
312
313 if ( ! params )
314 return 1; /* no params == no options, so all is good */
315
316 for ( i = 0; i < params_count; i++ )
317 if ( params[i].group != VIK_LAYER_NOT_IN_PROPERTIES )
318 widget_count++;
319
320 if ( widget_count == 0)
321 return 0; /* TODO -- should be one? */
322 else
323 {
324 /* create widgets and titles; place in table */
13fde155 325 GtkWidget *dialog = gtk_dialog_new_with_buttons ( dialog_name,
9be0449f
RN
326 parent,
327 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
328 GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
329 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT, NULL );
f9d4faa9
RN
330 gtk_dialog_set_default_response ( GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT );
331 GtkWidget *response_w = NULL;
332#if GTK_CHECK_VERSION (2, 20, 0)
333 response_w = gtk_dialog_get_widget_for_response ( GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT );
334#endif
f33aa4e0
EB
335 gint resp;
336
f33aa4e0
EB
337 GtkWidget *table = NULL;
338 GtkWidget **tables = NULL; /* for more than one group */
339
340 GtkWidget *notebook = NULL;
341 GtkWidget **widgets = g_malloc ( sizeof(GtkWidget *) * widget_count );
342
343 if ( groups && groups_count > 1 )
344 {
345 guint8 current_group;
346 guint16 tab_widget_count;
347 notebook = gtk_notebook_new ();
0ae518e4
RN
348 // Switch to vertical notebook mode when many groups
349 if ( groups_count > 4 )
350 gtk_notebook_set_tab_pos ( GTK_NOTEBOOK(notebook), GTK_POS_LEFT );
9b082b39 351 gtk_box_pack_start (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), notebook, FALSE, FALSE, 0);
f33aa4e0
EB
352 tables = g_malloc ( sizeof(GtkWidget *) * groups_count );
353 for ( current_group = 0; current_group < groups_count; current_group++ )
354 {
355 tab_widget_count = 0;
356 for ( j = 0; j < params_count; j ++ )
357 if ( params[j].group == current_group )
358 tab_widget_count++;
359
360 if ( tab_widget_count )
361 {
362 tables[current_group] = gtk_table_new ( tab_widget_count, 1, FALSE );
363 gtk_notebook_append_page ( GTK_NOTEBOOK(notebook), tables[current_group], gtk_label_new(groups[current_group]) );
364 }
365 }
366 }
367 else
368 {
369 table = gtk_table_new( widget_count, 1, FALSE );
9b082b39 370 gtk_box_pack_start (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), table, FALSE, FALSE, 0);
f33aa4e0
EB
371 }
372
373 for ( i = 0, j = 0; i < params_count; i++ )
374 {
375 if ( params[i].group != VIK_LAYER_NOT_IN_PROPERTIES )
376 {
377 if ( tables )
378 table = tables[MAX(0, params[i].group)]; /* round up NOT_IN_GROUP, that's not reasonable here */
379
158b3642 380 widgets[j] = a_uibuilder_new_widget ( &(params[i]), getparam ( pass_along_getparam, i, FALSE ) );
f33aa4e0 381
a7023a1b
RN
382 if ( widgets[j] ) {
383 gtk_table_attach ( GTK_TABLE(table), gtk_label_new(_(params[i].title)), 0, 1, j, j+1, 0, 0, 0, 0 );
384 gtk_table_attach ( GTK_TABLE(table), widgets[j], 1, 2, j, j+1, GTK_EXPAND | GTK_FILL, 0, 2, 2 );
385 }
f33aa4e0
EB
386 j++;
387 }
388 }
389
f9d4faa9
RN
390 if ( response_w )
391 gtk_widget_grab_focus ( response_w );
392
f33aa4e0
EB
393 gtk_widget_show_all ( dialog );
394
395 resp = gtk_dialog_run (GTK_DIALOG (dialog));
396 if ( resp == GTK_RESPONSE_ACCEPT )
397 {
398 for ( i = 0, j = 0; i < params_count; i++ )
399 {
400 if ( params[i].group != VIK_LAYER_NOT_IN_PROPERTIES )
401 {
158b3642
RN
402 if ( setparam ( pass_along1,
403 i,
404 a_uibuilder_widget_get_value ( widgets[j], &(params[i]) ),
405 pass_along2,
406 FALSE ) )
f33aa4e0
EB
407 must_redraw = TRUE;
408 j++;
409 }
410 }
411
412 gtk_widget_destroy ( dialog ); /* hide before redrawing. */
413 g_free ( widgets );
74ace2b1
RN
414 if ( tables )
415 g_free ( tables );
f33aa4e0
EB
416
417 return must_redraw ? 2 : 3; /* user clicked OK */
418 }
419
420 if ( tables )
421 g_free ( tables );
422 gtk_widget_destroy ( dialog );
423 g_free ( widgets );
424 return 0;
425 }
426}
427
428
429static void uibuilder_run_setparam ( VikLayerParamData *paramdatas, guint16 i, VikLayerParamData data, VikLayerParam *params )
430{
431 /* could have to copy it if it's a string! */
432 switch ( params[i].type ) {
433 case VIK_LAYER_PARAM_STRING:
434 paramdatas[i].s = g_strdup ( data.s );
435 break;
436 default:
437 paramdatas[i] = data; /* string list will have to be freed by layer. anything else not freed */
438 }
439}
440
441static VikLayerParamData uibuilder_run_getparam ( VikLayerParamData *params_defaults, guint16 i )
442{
443 return params_defaults[i];
444}
445
446
13fde155 447VikLayerParamData *a_uibuilder_run_dialog ( const gchar *dialog_name, GtkWindow *parent, VikLayerParam *params,
f33aa4e0
EB
448 guint16 params_count, gchar **groups, guint8 groups_count,
449 VikLayerParamData *params_defaults )
450{
451 VikLayerParamData *paramdatas = g_new(VikLayerParamData, params_count);
13fde155
RN
452 if ( a_uibuilder_properties_factory ( dialog_name,
453 parent,
dc2c040e
RN
454 params,
455 params_count,
456 groups,
457 groups_count,
458 (gpointer) uibuilder_run_setparam,
459 paramdatas,
460 params,
461 (gpointer) uibuilder_run_getparam,
462 params_defaults ) > 0 ) {
f33aa4e0
EB
463
464 return paramdatas;
465 }
466 g_free ( paramdatas );
467 return NULL;
468}
469
470/* frees data from last (if ness) */
471void a_uibuilder_free_paramdatas ( VikLayerParamData *paramdatas, VikLayerParam *params, guint16 params_count )
472{
473 int i;
474 /* may have to free strings, etc. */
475 for ( i = 0; i < params_count; i++ ) {
476 switch ( params[i].type ) {
477 case VIK_LAYER_PARAM_STRING:
478 g_free ( (gchar *) paramdatas[i].s );
479 break;
480 case VIK_LAYER_PARAM_STRING_LIST: {
481 /* should make a util function out of this */
482 GList *iter = paramdatas[i].sl;
483 while ( iter ) {
484 g_free ( iter->data );
485 iter = iter->next;
486 }
487 g_list_free ( paramdatas[i].sl );
488 break;
b5926b35
RN
489 default:
490 break;
f33aa4e0
EB
491 }
492 }
493 }
494 g_free ( paramdatas );
495}