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