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