2 * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
4 * Copyright (C) 2003-2007, Evan Battaglia <gtoevan@gmx.net>
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.
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.
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
26 #include <glib/gi18n.h>
27 #include "uibuilder.h"
28 #include "vikradiogroup.h"
29 #include "vikfileentry.h"
30 #include "vikfilelist.h"
32 VikLayerParamData vik_lpd_true_default ( void ) { return VIK_LPD_BOOLEAN ( TRUE ); }
33 VikLayerParamData vik_lpd_false_default ( void ) { return VIK_LPD_BOOLEAN ( FALSE ); }
35 GtkWidget *a_uibuilder_new_widget ( VikLayerParam *param, VikLayerParamData data )
37 // Perform pre conversion if necessary
38 VikLayerParamData vlpd = data;
39 if ( param->convert_to_display )
40 vlpd = param->convert_to_display ( data );
43 switch ( param->widget_type )
45 case VIK_LAYER_WIDGET_COLOR:
46 if ( param->type == VIK_LAYER_PARAM_COLOR )
47 rv = gtk_color_button_new_with_color ( &(vlpd.c) );
49 case VIK_LAYER_WIDGET_CHECKBUTTON:
50 if ( param->type == VIK_LAYER_PARAM_BOOLEAN )
52 //rv = gtk_check_button_new_with_label ( //param->title );
53 rv = gtk_check_button_new ();
55 gtk_toggle_button_set_active ( GTK_TOGGLE_BUTTON(rv), TRUE );
58 case VIK_LAYER_WIDGET_COMBOBOX:
59 if ( param->type == VIK_LAYER_PARAM_UINT && param->widget_data )
61 /* Build a simple combobox */
62 gchar **pstr = param->widget_data;
63 rv = vik_combo_box_text_new ();
65 vik_combo_box_text_append ( rv, *(pstr++) );
66 if ( param->extra_widget_data ) /* map of alternate uint values for options */
68 /* Set the effective default value */
70 for ( i = 0; ((const char **)param->widget_data)[i]; i++ )
71 if ( ((guint *)param->extra_widget_data)[i] == vlpd.u )
73 /* Match default value */
74 gtk_combo_box_set_active ( GTK_COMBO_BOX(rv), i );
79 gtk_combo_box_set_active ( GTK_COMBO_BOX ( rv ), vlpd.u );
81 else if ( param->type == VIK_LAYER_PARAM_STRING && param->widget_data && !param->extra_widget_data )
83 /* Build a combobox with editable text */
84 gchar **pstr = param->widget_data;
85 #if GTK_CHECK_VERSION (2, 24, 0)
86 rv = gtk_combo_box_text_new_with_entry ();
88 rv = gtk_combo_box_entry_new_text ();
91 vik_combo_box_text_append ( rv, vlpd.s );
93 vik_combo_box_text_append ( rv, *(pstr++) );
95 gtk_combo_box_set_active ( GTK_COMBO_BOX ( rv ), 0 );
97 else if ( param->type == VIK_LAYER_PARAM_STRING && param->widget_data && param->extra_widget_data)
99 /* Build a combobox with fixed selections without editable text */
100 gchar **pstr = param->widget_data;
101 rv = GTK_WIDGET ( vik_combo_box_text_new () );
103 vik_combo_box_text_append ( rv, *(pstr++) );
106 /* Set the effective default value */
107 /* In case of value does not exist, set the first value */
108 gtk_combo_box_set_active ( GTK_COMBO_BOX ( rv ), 0 );
110 for ( i = 0; ((const char **)param->widget_data)[i]; i++ )
111 if ( strcmp(((const char **)param->extra_widget_data)[i], vlpd.s) == 0 )
113 /* Match default value */
114 gtk_combo_box_set_active ( GTK_COMBO_BOX ( rv ), i );
119 gtk_combo_box_set_active ( GTK_COMBO_BOX ( rv ), 0 );
122 case VIK_LAYER_WIDGET_RADIOGROUP:
123 /* widget_data and extra_widget_data are GList */
124 if ( param->type == VIK_LAYER_PARAM_UINT && param->widget_data )
126 rv = vik_radio_group_new ( param->widget_data );
127 if ( param->extra_widget_data ) /* map of alternate uint values for options */
130 int nb_elem = g_list_length(param->widget_data);
131 for ( i = 0; i < nb_elem; i++ )
132 if ( GPOINTER_TO_UINT ( g_list_nth_data(param->extra_widget_data, i) ) == vlpd.u )
134 vik_radio_group_set_selected ( VIK_RADIO_GROUP(rv), i );
138 else if ( vlpd.u ) /* zero is already default */
139 vik_radio_group_set_selected ( VIK_RADIO_GROUP(rv), vlpd.u );
142 case VIK_LAYER_WIDGET_RADIOGROUP_STATIC:
143 if ( param->type == VIK_LAYER_PARAM_UINT && param->widget_data )
145 rv = vik_radio_group_new_static ( (const gchar **) param->widget_data );
146 if ( param->extra_widget_data ) /* map of alternate uint values for options */
149 for ( i = 0; ((const char **)param->widget_data)[i]; i++ )
150 if ( ((guint *)param->extra_widget_data)[i] == vlpd.u )
152 vik_radio_group_set_selected ( VIK_RADIO_GROUP(rv), i );
156 else if ( vlpd.u ) /* zero is already default */
157 vik_radio_group_set_selected ( VIK_RADIO_GROUP(rv), vlpd.u );
160 case VIK_LAYER_WIDGET_SPINBUTTON:
161 if ( (param->type == VIK_LAYER_PARAM_DOUBLE || param->type == VIK_LAYER_PARAM_UINT
162 || param->type == VIK_LAYER_PARAM_INT) && param->widget_data )
164 gdouble init_val = (param->type == VIK_LAYER_PARAM_DOUBLE) ? vlpd.d : (param->type == VIK_LAYER_PARAM_UINT ? vlpd.u : vlpd.i);
165 VikLayerParamScale *scale = (VikLayerParamScale *) param->widget_data;
166 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 );
169 case VIK_LAYER_WIDGET_ENTRY:
170 if ( param->type == VIK_LAYER_PARAM_STRING )
172 rv = gtk_entry_new ();
174 gtk_entry_set_text ( GTK_ENTRY(rv), vlpd.s );
177 case VIK_LAYER_WIDGET_PASSWORD:
178 if ( param->type == VIK_LAYER_PARAM_STRING )
180 rv = gtk_entry_new ();
181 gtk_entry_set_visibility ( GTK_ENTRY(rv), FALSE );
183 gtk_entry_set_text ( GTK_ENTRY(rv), vlpd.s );
184 gtk_widget_set_tooltip_text ( GTK_WIDGET(rv),
185 _("Take care that this password will be stored clearly in a plain file.") );
188 case VIK_LAYER_WIDGET_FILEENTRY:
189 if ( param->type == VIK_LAYER_PARAM_STRING )
191 rv = vik_file_entry_new (GTK_FILE_CHOOSER_ACTION_OPEN);
193 vik_file_entry_set_filename ( VIK_FILE_ENTRY(rv), vlpd.s );
196 case VIK_LAYER_WIDGET_FOLDERENTRY:
197 if ( param->type == VIK_LAYER_PARAM_STRING )
199 rv = vik_file_entry_new (GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER);
201 vik_file_entry_set_filename ( VIK_FILE_ENTRY(rv), vlpd.s );
205 case VIK_LAYER_WIDGET_FILELIST:
206 if ( param->type == VIK_LAYER_PARAM_STRING_LIST )
208 rv = vik_file_list_new ( _(param->title), NULL );
209 vik_file_list_set_files ( VIK_FILE_LIST(rv), vlpd.sl );
212 case VIK_LAYER_WIDGET_HSCALE:
213 if ( (param->type == VIK_LAYER_PARAM_DOUBLE || param->type == VIK_LAYER_PARAM_UINT
214 || param->type == VIK_LAYER_PARAM_INT) && param->widget_data )
216 gdouble init_val = (param->type == VIK_LAYER_PARAM_DOUBLE) ? vlpd.d : (param->type == VIK_LAYER_PARAM_UINT ? vlpd.u : vlpd.i);
217 VikLayerParamScale *scale = (VikLayerParamScale *) param->widget_data;
218 rv = gtk_hscale_new_with_range ( scale->min, scale->max, scale->step );
219 gtk_scale_set_digits ( GTK_SCALE(rv), scale->digits );
220 gtk_range_set_value ( GTK_RANGE(rv), init_val );
223 if ( rv && !gtk_widget_get_tooltip_text ( rv ) ) {
224 if ( param->tooltip )
225 gtk_widget_set_tooltip_text ( rv, _(param->tooltip) );
230 VikLayerParamData a_uibuilder_widget_get_value ( GtkWidget *widget, VikLayerParam *param )
232 VikLayerParamData rv;
233 switch ( param->widget_type )
235 case VIK_LAYER_WIDGET_COLOR:
236 gtk_color_button_get_color ( GTK_COLOR_BUTTON(widget), &(rv.c) );
238 case VIK_LAYER_WIDGET_CHECKBUTTON:
239 rv.b = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget));
241 case VIK_LAYER_WIDGET_COMBOBOX:
242 if ( param->type == VIK_LAYER_PARAM_UINT )
244 rv.i = gtk_combo_box_get_active ( GTK_COMBO_BOX(widget) );
245 if ( rv.i == -1 ) rv.i = 0;
247 if ( param->extra_widget_data )
248 rv.u = ((guint *)param->extra_widget_data)[rv.u];
250 if ( param->type == VIK_LAYER_PARAM_STRING)
252 if ( param->extra_widget_data )
254 /* Combobox displays labels and we want values from extra */
255 int pos = gtk_combo_box_get_active ( GTK_COMBO_BOX(widget) );
256 rv.s = ((const char **)param->extra_widget_data)[pos];
260 /* Return raw value */
261 #if GTK_CHECK_VERSION (2, 24, 0)
262 rv.s = gtk_entry_get_text (GTK_ENTRY (gtk_bin_get_child (GTK_BIN (widget))));
264 rv.s = gtk_combo_box_get_active_text ( GTK_COMBO_BOX(widget) );
267 g_debug("%s: %s", __FUNCTION__, rv.s);
270 case VIK_LAYER_WIDGET_RADIOGROUP:
271 case VIK_LAYER_WIDGET_RADIOGROUP_STATIC:
272 rv.u = vik_radio_group_get_selected(VIK_RADIO_GROUP(widget));
273 if ( param->extra_widget_data )
274 rv.u = GPOINTER_TO_UINT ( g_list_nth_data(param->extra_widget_data, rv.u) );
276 case VIK_LAYER_WIDGET_SPINBUTTON:
277 if ( param->type == VIK_LAYER_PARAM_UINT )
278 rv.u = gtk_spin_button_get_value_as_int ( GTK_SPIN_BUTTON(widget) );
279 else if ( param->type == VIK_LAYER_PARAM_INT )
280 rv.i = gtk_spin_button_get_value_as_int ( GTK_SPIN_BUTTON(widget) );
282 rv.d = gtk_spin_button_get_value ( GTK_SPIN_BUTTON(widget) );
284 case VIK_LAYER_WIDGET_ENTRY:
285 case VIK_LAYER_WIDGET_PASSWORD:
286 rv.s = gtk_entry_get_text ( GTK_ENTRY(widget) );
288 case VIK_LAYER_WIDGET_FILEENTRY:
289 case VIK_LAYER_WIDGET_FOLDERENTRY:
290 rv.s = vik_file_entry_get_filename ( VIK_FILE_ENTRY(widget) );
292 case VIK_LAYER_WIDGET_FILELIST:
293 rv.sl = vik_file_list_get_files ( VIK_FILE_LIST(widget) );
295 case VIK_LAYER_WIDGET_HSCALE:
296 if ( param->type == VIK_LAYER_PARAM_UINT )
297 rv.u = (guint32) gtk_range_get_value ( GTK_RANGE(widget) );
298 else if ( param->type == VIK_LAYER_PARAM_INT )
299 rv.i = (gint32) gtk_range_get_value ( GTK_RANGE(widget) );
301 rv.d = gtk_range_get_value ( GTK_RANGE(widget) );
305 // Perform conversion if necessary
306 if ( param->convert_to_internal )
307 rv = param->convert_to_internal ( rv );
312 //static void draw_to_image_file_total_area_cb (GtkSpinButton *spinbutton, gpointer *pass_along)
313 gint a_uibuilder_properties_factory ( const gchar *dialog_name,
315 VikLayerParam *params,
316 guint16 params_count,
319 gboolean (*setparam) (gpointer,guint16,VikLayerParamData,gpointer,gboolean),
320 gpointer pass_along1,
321 gpointer pass_along2,
322 VikLayerParamData (*getparam) (gpointer,guint16,gboolean),
323 gpointer pass_along_getparam,
324 void (*changeparam) (GtkWidget*, ui_change_values) )
325 /* pass_along1 and pass_along2 are for set_param first and last params */
327 guint16 i, j, widget_count = 0;
328 gboolean must_redraw = FALSE;
331 return 1; /* no params == no options, so all is good */
333 for ( i = 0; i < params_count; i++ )
334 if ( params[i].group != VIK_LAYER_NOT_IN_PROPERTIES )
337 if ( widget_count == 0)
338 return 0; /* TODO -- should be one? */
341 /* create widgets and titles; place in table */
342 GtkWidget *dialog = gtk_dialog_new_with_buttons ( dialog_name,
344 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
345 GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
346 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT, NULL );
347 gtk_dialog_set_default_response ( GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT );
348 GtkWidget *response_w = NULL;
349 #if GTK_CHECK_VERSION (2, 20, 0)
350 response_w = gtk_dialog_get_widget_for_response ( GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT );
354 GtkWidget *table = NULL;
355 GtkWidget **tables = NULL; /* for more than one group */
357 GtkWidget *notebook = NULL;
358 GtkWidget **labels = g_malloc ( sizeof(GtkWidget *) * widget_count );
359 GtkWidget **widgets = g_malloc ( sizeof(GtkWidget *) * widget_count );
360 ui_change_values *change_values = g_malloc ( sizeof(ui_change_values) * widget_count );
362 if ( groups && groups_count > 1 )
364 guint8 current_group;
365 guint16 tab_widget_count;
366 notebook = gtk_notebook_new ();
367 // Switch to vertical notebook mode when many groups
368 if ( groups_count > 4 )
369 gtk_notebook_set_tab_pos ( GTK_NOTEBOOK(notebook), GTK_POS_LEFT );
370 gtk_box_pack_start (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), notebook, FALSE, FALSE, 0);
371 tables = g_malloc ( sizeof(GtkWidget *) * groups_count );
372 for ( current_group = 0; current_group < groups_count; current_group++ )
374 tab_widget_count = 0;
375 for ( j = 0; j < params_count; j ++ )
376 if ( params[j].group == current_group )
379 if ( tab_widget_count )
381 tables[current_group] = gtk_table_new ( tab_widget_count, 1, FALSE );
382 gtk_notebook_append_page ( GTK_NOTEBOOK(notebook), tables[current_group], gtk_label_new(groups[current_group]) );
388 table = gtk_table_new( widget_count, 1, FALSE );
389 gtk_box_pack_start (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), table, FALSE, FALSE, 0);
392 for ( i = 0, j = 0; i < params_count; i++ )
394 if ( params[i].group != VIK_LAYER_NOT_IN_PROPERTIES )
397 table = tables[MAX(0, params[i].group)]; /* round up NOT_IN_GROUP, that's not reasonable here */
399 widgets[j] = a_uibuilder_new_widget ( &(params[i]), getparam ( pass_along_getparam, i, FALSE ) );
402 labels[j] = gtk_label_new(_(params[i].title));
403 gtk_table_attach ( GTK_TABLE(table), labels[j], 0, 1, j, j+1, 0, 0, 0, 0 );
404 gtk_table_attach ( GTK_TABLE(table), widgets[j], 1, 2, j, j+1, GTK_EXPAND | GTK_FILL, 0, 2, 2 );
408 change_values[j][UI_CHG_LAYER] = pass_along1;
409 change_values[j][UI_CHG_PARAM] = ¶ms[i];
410 change_values[j][UI_CHG_PARAM_ID] = GINT_TO_POINTER((gint)i);
411 change_values[j][UI_CHG_WIDGETS] = widgets;
412 change_values[j][UI_CHG_LABELS] = labels;
414 switch ( params[i].widget_type )
416 // Change conditions for other widget types can be added when needed
417 case VIK_LAYER_WIDGET_COMBOBOX:
418 g_signal_connect ( G_OBJECT(widgets[j]), "changed", G_CALLBACK(changeparam), change_values[j] );
420 case VIK_LAYER_WIDGET_CHECKBUTTON:
421 g_signal_connect ( G_OBJECT(widgets[j]), "toggled", G_CALLBACK(changeparam), change_values[j] );
431 // Repeat run through to force changeparam callbacks now that the widgets have been created
432 // This primarily so the widget sensitivities get set up
434 for ( i = 0, j = 0; i < params_count; i++ ) {
435 if ( params[i].group != VIK_LAYER_NOT_IN_PROPERTIES ) {
437 changeparam ( widgets[j], change_values[j] );
445 gtk_widget_grab_focus ( response_w );
447 gtk_widget_show_all ( dialog );
449 resp = gtk_dialog_run (GTK_DIALOG (dialog));
450 if ( resp == GTK_RESPONSE_ACCEPT )
452 for ( i = 0, j = 0; i < params_count; i++ )
454 if ( params[i].group != VIK_LAYER_NOT_IN_PROPERTIES )
456 if ( setparam ( pass_along1,
458 a_uibuilder_widget_get_value ( widgets[j], &(params[i]) ),
466 gtk_widget_destroy ( dialog ); /* hide before redrawing. */
469 g_free ( change_values );
473 return must_redraw ? 2 : 3; /* user clicked OK */
478 gtk_widget_destroy ( dialog );
485 static void uibuilder_run_setparam ( VikLayerParamData *paramdatas, guint16 i, VikLayerParamData data, VikLayerParam *params )
487 /* could have to copy it if it's a string! */
488 switch ( params[i].type ) {
489 case VIK_LAYER_PARAM_STRING:
490 paramdatas[i].s = g_strdup ( data.s );
493 paramdatas[i] = data; /* string list will have to be freed by layer. anything else not freed */
497 static VikLayerParamData uibuilder_run_getparam ( VikLayerParamData *params_defaults, guint16 i )
499 return params_defaults[i];
503 VikLayerParamData *a_uibuilder_run_dialog ( const gchar *dialog_name, GtkWindow *parent, VikLayerParam *params,
504 guint16 params_count, gchar **groups, guint8 groups_count,
505 VikLayerParamData *params_defaults )
507 VikLayerParamData *paramdatas = g_new(VikLayerParamData, params_count);
508 if ( a_uibuilder_properties_factory ( dialog_name,
514 (gpointer) uibuilder_run_setparam,
517 (gpointer) uibuilder_run_getparam,
523 g_free ( paramdatas );
527 /* frees data from last (if ness) */
528 void a_uibuilder_free_paramdatas ( VikLayerParamData *paramdatas, VikLayerParam *params, guint16 params_count )
531 /* may have to free strings, etc. */
532 for ( i = 0; i < params_count; i++ ) {
533 switch ( params[i].type ) {
534 case VIK_LAYER_PARAM_STRING:
535 g_free ( (gchar *) paramdatas[i].s );
537 case VIK_LAYER_PARAM_STRING_LIST: {
538 /* should make a util function out of this */
539 GList *iter = paramdatas[i].sl;
541 g_free ( iter->data );
544 g_list_free ( paramdatas[i].sl );
551 g_free ( paramdatas );