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