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 )
38 switch ( param->widget_type )
40 case VIK_LAYER_WIDGET_COLOR:
41 if ( param->type == VIK_LAYER_PARAM_COLOR )
42 rv = gtk_color_button_new_with_color ( &(data.c) );
44 case VIK_LAYER_WIDGET_CHECKBUTTON:
45 if ( param->type == VIK_LAYER_PARAM_BOOLEAN )
47 //rv = gtk_check_button_new_with_label ( //param->title );
48 rv = gtk_check_button_new ();
50 gtk_toggle_button_set_active ( GTK_TOGGLE_BUTTON(rv), TRUE );
53 case VIK_LAYER_WIDGET_COMBOBOX:
55 if ( param->type == VIK_LAYER_PARAM_UINT && param->widget_data )
57 gchar **pstr = param->widget_data;
58 rv = gtk_combo_box_new_text ();
60 gtk_combo_box_append_text ( GTK_COMBO_BOX ( rv ), *(pstr++) );
61 if ( param->extra_widget_data ) /* map of alternate uint values for options */
64 for ( i = 0; ((const char **)param->widget_data)[i]; i++ )
65 if ( ((guint *)param->extra_widget_data)[i] == data.u )
67 gtk_combo_box_set_active ( GTK_COMBO_BOX(rv), i );
72 gtk_combo_box_set_active ( GTK_COMBO_BOX ( rv ), data.u );
74 else if ( param->type == VIK_LAYER_PARAM_STRING && param->widget_data )
76 gchar **pstr = param->widget_data;
77 rv = GTK_WIDGET ( gtk_combo_box_entry_new_text () );
79 gtk_combo_box_append_text ( GTK_COMBO_BOX ( rv ), data.s );
81 gtk_combo_box_append_text ( GTK_COMBO_BOX ( rv ), *(pstr++) );
83 gtk_combo_box_set_active ( GTK_COMBO_BOX ( rv ), 0 );
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 )
91 rv = vik_radio_group_new ( param->widget_data );
92 if ( param->extra_widget_data ) /* map of alternate uint values for options */
95 int nb_elem = g_list_length(param->widget_data);
96 for ( i = 0; i < nb_elem; i++ )
97 if ( GPOINTER_TO_UINT ( g_list_nth_data(param->extra_widget_data, i) ) == data.u )
99 vik_radio_group_set_selected ( VIK_RADIO_GROUP(rv), i );
103 else if ( data.u ) /* zero is already default */
104 vik_radio_group_set_selected ( VIK_RADIO_GROUP(rv), data.u );
107 case VIK_LAYER_WIDGET_RADIOGROUP_STATIC:
108 if ( param->type == VIK_LAYER_PARAM_UINT && param->widget_data )
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 */
114 for ( i = 0; ((const char **)param->widget_data)[i]; i++ )
115 if ( ((guint *)param->extra_widget_data)[i] == data.u )
117 vik_radio_group_set_selected ( VIK_RADIO_GROUP(rv), i );
121 else if ( data.u ) /* zero is already default */
122 vik_radio_group_set_selected ( VIK_RADIO_GROUP(rv), data.u );
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 )
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;
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 );
134 case VIK_LAYER_WIDGET_ENTRY:
135 if ( param->type == VIK_LAYER_PARAM_STRING )
137 rv = gtk_entry_new ();
139 gtk_entry_set_text ( GTK_ENTRY(rv), data.s );
142 case VIK_LAYER_WIDGET_PASSWORD:
143 if ( param->type == VIK_LAYER_PARAM_STRING )
145 rv = gtk_entry_new ();
146 gtk_entry_set_visibility ( GTK_ENTRY(rv), FALSE );
148 gtk_entry_set_text ( GTK_ENTRY(rv), data.s );
149 gtk_widget_set_tooltip_text ( GTK_WIDGET(rv),
150 _("Take care that this password will be stored clearly in a plain file.") );
153 case VIK_LAYER_WIDGET_FILEENTRY:
154 if ( param->type == VIK_LAYER_PARAM_STRING )
156 rv = vik_file_entry_new (GTK_FILE_CHOOSER_ACTION_OPEN);
157 vik_file_entry_set_filename ( VIK_FILE_ENTRY(rv), data.s );
160 case VIK_LAYER_WIDGET_FOLDERENTRY:
161 if ( param->type == VIK_LAYER_PARAM_STRING )
163 rv = vik_file_entry_new (GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER);
164 vik_file_entry_set_filename ( VIK_FILE_ENTRY(rv), data.s );
168 case VIK_LAYER_WIDGET_FILELIST:
169 if ( param->type == VIK_LAYER_PARAM_STRING_LIST )
171 rv = vik_file_list_new ( _(param->title) );
172 vik_file_list_set_files ( VIK_FILE_LIST(rv), data.sl );
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 )
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 );
186 if ( rv && !gtk_widget_get_tooltip_text ( rv ) ) {
187 if ( param->tooltip )
188 gtk_widget_set_tooltip_text ( rv, _(param->tooltip) );
193 VikLayerParamData a_uibuilder_widget_get_value ( GtkWidget *widget, VikLayerParam *param )
195 VikLayerParamData rv;
196 switch ( param->widget_type )
198 case VIK_LAYER_WIDGET_COLOR:
199 gtk_color_button_get_color ( GTK_COLOR_BUTTON(widget), &(rv.c) );
201 case VIK_LAYER_WIDGET_CHECKBUTTON:
202 rv.b = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget));
204 case VIK_LAYER_WIDGET_COMBOBOX:
206 if ( param->type == VIK_LAYER_PARAM_UINT )
208 rv.i = gtk_combo_box_get_active ( GTK_COMBO_BOX(widget) );
209 if ( rv.i == -1 ) rv.i = 0;
211 if ( param->extra_widget_data )
212 rv.u = ((guint *)param->extra_widget_data)[rv.u];
214 if ( param->type == VIK_LAYER_PARAM_STRING)
216 rv.s = gtk_combo_box_get_active_text ( GTK_COMBO_BOX(widget) );
217 g_debug("%s: %s", __FUNCTION__, rv.s);
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 )
225 rv.u = GPOINTER_TO_UINT ( g_list_nth_data(param->extra_widget_data, rv.u) );
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) );
233 rv.d = gtk_spin_button_get_value ( GTK_SPIN_BUTTON(widget) );
235 case VIK_LAYER_WIDGET_ENTRY:
236 case VIK_LAYER_WIDGET_PASSWORD:
237 rv.s = gtk_entry_get_text ( GTK_ENTRY(widget) );
239 case VIK_LAYER_WIDGET_FILEENTRY:
240 case VIK_LAYER_WIDGET_FOLDERENTRY:
241 rv.s = vik_file_entry_get_filename ( VIK_FILE_ENTRY(widget) );
243 case VIK_LAYER_WIDGET_FILELIST:
244 rv.sl = vik_file_list_get_files ( VIK_FILE_LIST(widget) );
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) );
252 rv.d = gtk_range_get_value ( GTK_RANGE(widget) );
258 gint a_uibuilder_properties_factory ( const gchar *dialog_name, GtkWindow *parent, VikLayerParam *params,
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 */
266 guint16 i, j, widget_count = 0;
267 gboolean must_redraw = FALSE;
270 return 1; /* no params == no options, so all is good */
272 for ( i = 0; i < params_count; i++ )
273 if ( params[i].group != VIK_LAYER_NOT_IN_PROPERTIES )
276 if ( widget_count == 0)
277 return 0; /* TODO -- should be one? */
280 /* create widgets and titles; place in table */
281 GtkWidget *dialog = gtk_dialog_new_with_buttons ( dialog_name,
283 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
284 GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
285 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT, NULL );
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 );
293 GtkWidget *table = NULL;
294 GtkWidget **tables = NULL; /* for more than one group */
296 GtkWidget *notebook = NULL;
297 GtkWidget **widgets = g_malloc ( sizeof(GtkWidget *) * widget_count );
299 if ( groups && groups_count > 1 )
301 guint8 current_group;
302 guint16 tab_widget_count;
303 notebook = gtk_notebook_new ();
304 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), notebook, FALSE, FALSE, 0);
305 tables = g_malloc ( sizeof(GtkWidget *) * groups_count );
306 for ( current_group = 0; current_group < groups_count; current_group++ )
308 tab_widget_count = 0;
309 for ( j = 0; j < params_count; j ++ )
310 if ( params[j].group == current_group )
313 if ( tab_widget_count )
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]) );
322 table = gtk_table_new( widget_count, 1, FALSE );
323 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), table, FALSE, FALSE, 0);
326 for ( i = 0, j = 0; i < params_count; i++ )
328 if ( params[i].group != VIK_LAYER_NOT_IN_PROPERTIES )
331 table = tables[MAX(0, params[i].group)]; /* round up NOT_IN_GROUP, that's not reasonable here */
333 widgets[j] = a_uibuilder_new_widget ( &(params[i]), getparam ( pass_along_getparam, i, FALSE ) );
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 );
344 gtk_widget_grab_focus ( response_w );
346 gtk_widget_show_all ( dialog );
348 resp = gtk_dialog_run (GTK_DIALOG (dialog));
349 if ( resp == GTK_RESPONSE_ACCEPT )
351 for ( i = 0, j = 0; i < params_count; i++ )
353 if ( params[i].group != VIK_LAYER_NOT_IN_PROPERTIES )
355 if ( setparam ( pass_along1,
357 a_uibuilder_widget_get_value ( widgets[j], &(params[i]) ),
365 gtk_widget_destroy ( dialog ); /* hide before redrawing. */
370 return must_redraw ? 2 : 3; /* user clicked OK */
375 gtk_widget_destroy ( dialog );
382 static void uibuilder_run_setparam ( VikLayerParamData *paramdatas, guint16 i, VikLayerParamData data, VikLayerParam *params )
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 );
390 paramdatas[i] = data; /* string list will have to be freed by layer. anything else not freed */
394 static VikLayerParamData uibuilder_run_getparam ( VikLayerParamData *params_defaults, guint16 i )
396 return params_defaults[i];
400 VikLayerParamData *a_uibuilder_run_dialog ( const gchar *dialog_name, GtkWindow *parent, VikLayerParam *params,
401 guint16 params_count, gchar **groups, guint8 groups_count,
402 VikLayerParamData *params_defaults )
404 VikLayerParamData *paramdatas = g_new(VikLayerParamData, params_count);
405 if ( a_uibuilder_properties_factory ( dialog_name,
411 (gpointer) uibuilder_run_setparam,
414 (gpointer) uibuilder_run_getparam,
415 params_defaults ) > 0 ) {
419 g_free ( paramdatas );
423 /* frees data from last (if ness) */
424 void a_uibuilder_free_paramdatas ( VikLayerParamData *paramdatas, VikLayerParam *params, guint16 params_count )
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 );
433 case VIK_LAYER_PARAM_STRING_LIST: {
434 /* should make a util function out of this */
435 GList *iter = paramdatas[i].sl;
437 g_free ( iter->data );
440 g_list_free ( paramdatas[i].sl );
447 g_free ( paramdatas );