]> git.street.me.uk Git - andy/viking.git/blame_incremental - src/uibuilder.c
Fix sensitivity of the track properties window split marker to be disabled when the...
[andy/viking.git] / src / uibuilder.c
... / ...
CommitLineData
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 */
21#ifdef HAVE_CONFIG_H
22#include "config.h"
23#endif
24
25#include <gtk/gtk.h>
26#include <glib/gi18n.h>
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 }
68 else
69 gtk_combo_box_set_active ( GTK_COMBO_BOX ( rv ), data.u );
70 }
71 else if ( param->type == VIK_LAYER_PARAM_STRING && param->widget_data )
72 {
73 gchar **pstr = param->widget_data;
74 rv = GTK_WIDGET ( gtk_combo_box_entry_new_text () );
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 }
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;
92 int nb_elem = g_list_length(param->widget_data);
93 for ( i = 0; i < nb_elem; i++ )
94 if ( GPOINTER_TO_UINT ( g_list_nth_data(param->extra_widget_data, i) ) == data.u )
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;
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 );
129 }
130 break;
131 case VIK_LAYER_WIDGET_ENTRY:
132 if ( param->type == VIK_LAYER_PARAM_STRING )
133 {
134 rv = gtk_entry_new ();
135 if (data.s)
136 gtk_entry_set_text ( GTK_ENTRY(rv), data.s );
137 }
138 break;
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 );
144 if (data.s)
145 gtk_entry_set_text ( GTK_ENTRY(rv), data.s );
146#if GTK_CHECK_VERSION(2,12,0)
147 gtk_widget_set_tooltip_text ( GTK_WIDGET(rv),
148 _("Take care that this password will be stored clearly in a plain file.") );
149#endif
150 }
151 break;
152 case VIK_LAYER_WIDGET_FILEENTRY:
153 if ( param->type == VIK_LAYER_PARAM_STRING )
154 {
155 rv = vik_file_entry_new (GTK_FILE_CHOOSER_ACTION_OPEN);
156 vik_file_entry_set_filename ( VIK_FILE_ENTRY(rv), data.s );
157 }
158 break;
159 case VIK_LAYER_WIDGET_FOLDERENTRY:
160 if ( param->type == VIK_LAYER_PARAM_STRING )
161 {
162 rv = vik_file_entry_new (GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER);
163 vik_file_entry_set_filename ( VIK_FILE_ENTRY(rv), data.s );
164 }
165 break;
166
167 case VIK_LAYER_WIDGET_FILELIST:
168 if ( param->type == VIK_LAYER_PARAM_STRING_LIST )
169 {
170 rv = vik_file_list_new ( _(param->title) );
171 vik_file_list_set_files ( VIK_FILE_LIST(rv), data.sl );
172 }
173 break;
174 case VIK_LAYER_WIDGET_HSCALE:
175 if ( (param->type == VIK_LAYER_PARAM_DOUBLE || param->type == VIK_LAYER_PARAM_UINT
176 || param->type == VIK_LAYER_PARAM_INT) && param->widget_data )
177 {
178 gdouble init_val = (param->type == VIK_LAYER_PARAM_DOUBLE) ? data.d : (param->type == VIK_LAYER_PARAM_UINT ? data.u : data.i);
179 VikLayerParamScale *scale = (VikLayerParamScale *) param->widget_data;
180 rv = gtk_hscale_new_with_range ( scale->min, scale->max, scale->step );
181 gtk_scale_set_digits ( GTK_SCALE(rv), scale->digits );
182 gtk_range_set_value ( GTK_RANGE(rv), init_val );
183 }
184 }
185 return rv;
186}
187
188VikLayerParamData a_uibuilder_widget_get_value ( GtkWidget *widget, VikLayerParam *param )
189{
190 VikLayerParamData rv;
191 switch ( param->widget_type )
192 {
193 case VIK_LAYER_WIDGET_COLOR:
194 gtk_color_button_get_color ( GTK_COLOR_BUTTON(widget), &(rv.c) );
195 break;
196 case VIK_LAYER_WIDGET_CHECKBUTTON:
197 rv.b = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget));
198 break;
199 case VIK_LAYER_WIDGET_COMBOBOX:
200#ifndef GTK_2_2
201 if ( param->type == VIK_LAYER_PARAM_UINT )
202 {
203 rv.i = gtk_combo_box_get_active ( GTK_COMBO_BOX(widget) );
204 if ( rv.i == -1 ) rv.i = 0;
205 rv.u = rv.i;
206 if ( param->extra_widget_data )
207 rv.u = ((guint *)param->extra_widget_data)[rv.u];
208 }
209 if ( param->type == VIK_LAYER_PARAM_STRING)
210 {
211 rv.s = gtk_combo_box_get_active_text ( GTK_COMBO_BOX(widget) );
212 g_debug("%s: %s", __FUNCTION__, rv.s);
213 }
214 break;
215#endif
216 case VIK_LAYER_WIDGET_RADIOGROUP:
217 case VIK_LAYER_WIDGET_RADIOGROUP_STATIC:
218 rv.u = vik_radio_group_get_selected(VIK_RADIO_GROUP(widget));
219 if ( param->extra_widget_data )
220 rv.u = GPOINTER_TO_UINT ( g_list_nth_data(param->extra_widget_data, rv.u) );
221 break;
222 case VIK_LAYER_WIDGET_SPINBUTTON:
223 if ( param->type == VIK_LAYER_PARAM_UINT )
224 rv.u = gtk_spin_button_get_value_as_int ( GTK_SPIN_BUTTON(widget) );
225 else if ( param->type == VIK_LAYER_PARAM_INT )
226 rv.i = gtk_spin_button_get_value_as_int ( GTK_SPIN_BUTTON(widget) );
227 else
228 rv.d = gtk_spin_button_get_value ( GTK_SPIN_BUTTON(widget) );
229 break;
230 case VIK_LAYER_WIDGET_ENTRY:
231 case VIK_LAYER_WIDGET_PASSWORD:
232 rv.s = gtk_entry_get_text ( GTK_ENTRY(widget) );
233 break;
234 case VIK_LAYER_WIDGET_FILEENTRY:
235 case VIK_LAYER_WIDGET_FOLDERENTRY:
236 rv.s = vik_file_entry_get_filename ( VIK_FILE_ENTRY(widget) );
237 break;
238 case VIK_LAYER_WIDGET_FILELIST:
239 rv.sl = vik_file_list_get_files ( VIK_FILE_LIST(widget) );
240 break;
241 case VIK_LAYER_WIDGET_HSCALE:
242 if ( param->type == VIK_LAYER_PARAM_UINT )
243 rv.u = (guint32) gtk_range_get_value ( GTK_RANGE(widget) );
244 else if ( param->type == VIK_LAYER_PARAM_INT )
245 rv.i = (gint32) gtk_range_get_value ( GTK_RANGE(widget) );
246 else
247 rv.d = gtk_range_get_value ( GTK_RANGE(widget) );
248 break;
249 }
250 return rv;
251}
252
253
254gint a_uibuilder_properties_factory ( const gchar *dialog_name, GtkWindow *parent, VikLayerParam *params,
255 guint16 params_count, gchar **groups, guint8 groups_count,
256 gboolean (*setparam) (gpointer,guint16,VikLayerParamData,gpointer,gboolean),
257 gpointer pass_along1, gpointer pass_along2,
258 VikLayerParamData (*getparam) (gpointer,guint16,gboolean),
259 gpointer pass_along_getparam )
260 /* pass_along1 and pass_along2 are for set_param first and last params */
261{
262 guint16 i, j, widget_count = 0;
263 gboolean must_redraw = FALSE;
264
265 if ( ! params )
266 return 1; /* no params == no options, so all is good */
267
268 for ( i = 0; i < params_count; i++ )
269 if ( params[i].group != VIK_LAYER_NOT_IN_PROPERTIES )
270 widget_count++;
271
272 if ( widget_count == 0)
273 return 0; /* TODO -- should be one? */
274 else
275 {
276 /* create widgets and titles; place in table */
277 GtkWidget *dialog = gtk_dialog_new_with_buttons ( dialog_name,
278 parent,
279 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
280 GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
281 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT, NULL );
282 gtk_dialog_set_default_response ( GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT );
283 GtkWidget *response_w = NULL;
284#if GTK_CHECK_VERSION (2, 20, 0)
285 response_w = gtk_dialog_get_widget_for_response ( GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT );
286#endif
287 gint resp;
288
289 GtkWidget *table = NULL;
290 GtkWidget **tables = NULL; /* for more than one group */
291
292 GtkWidget *notebook = NULL;
293 GtkWidget **widgets = g_malloc ( sizeof(GtkWidget *) * widget_count );
294
295 if ( groups && groups_count > 1 )
296 {
297 guint8 current_group;
298 guint16 tab_widget_count;
299 notebook = gtk_notebook_new ();
300 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), notebook, FALSE, FALSE, 0);
301 tables = g_malloc ( sizeof(GtkWidget *) * groups_count );
302 for ( current_group = 0; current_group < groups_count; current_group++ )
303 {
304 tab_widget_count = 0;
305 for ( j = 0; j < params_count; j ++ )
306 if ( params[j].group == current_group )
307 tab_widget_count++;
308
309 if ( tab_widget_count )
310 {
311 tables[current_group] = gtk_table_new ( tab_widget_count, 1, FALSE );
312 gtk_notebook_append_page ( GTK_NOTEBOOK(notebook), tables[current_group], gtk_label_new(groups[current_group]) );
313 }
314 }
315 }
316 else
317 {
318 table = gtk_table_new( widget_count, 1, FALSE );
319 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), table, FALSE, FALSE, 0);
320 }
321
322 for ( i = 0, j = 0; i < params_count; i++ )
323 {
324 if ( params[i].group != VIK_LAYER_NOT_IN_PROPERTIES )
325 {
326 if ( tables )
327 table = tables[MAX(0, params[i].group)]; /* round up NOT_IN_GROUP, that's not reasonable here */
328
329 widgets[j] = a_uibuilder_new_widget ( &(params[i]), getparam ( pass_along_getparam, i, FALSE ) );
330
331 g_assert ( widgets[j] != NULL );
332
333 gtk_table_attach ( GTK_TABLE(table), gtk_label_new(_(params[i].title)), 0, 1, j, j+1, 0, 0, 0, 0 );
334 gtk_table_attach ( GTK_TABLE(table), widgets[j], 1, 2, j, j+1, GTK_EXPAND | GTK_FILL, 0, 2, 2 );
335 j++;
336 }
337 }
338
339 if ( response_w )
340 gtk_widget_grab_focus ( response_w );
341
342 gtk_widget_show_all ( dialog );
343
344 resp = gtk_dialog_run (GTK_DIALOG (dialog));
345 if ( resp == GTK_RESPONSE_ACCEPT )
346 {
347 for ( i = 0, j = 0; i < params_count; i++ )
348 {
349 if ( params[i].group != VIK_LAYER_NOT_IN_PROPERTIES )
350 {
351 if ( setparam ( pass_along1,
352 i,
353 a_uibuilder_widget_get_value ( widgets[j], &(params[i]) ),
354 pass_along2,
355 FALSE ) )
356 must_redraw = TRUE;
357 j++;
358 }
359 }
360
361 gtk_widget_destroy ( dialog ); /* hide before redrawing. */
362 g_free ( widgets );
363
364 return must_redraw ? 2 : 3; /* user clicked OK */
365 }
366
367 if ( tables )
368 g_free ( tables );
369 gtk_widget_destroy ( dialog );
370 g_free ( widgets );
371 return 0;
372 }
373}
374
375
376static void uibuilder_run_setparam ( VikLayerParamData *paramdatas, guint16 i, VikLayerParamData data, VikLayerParam *params )
377{
378 /* could have to copy it if it's a string! */
379 switch ( params[i].type ) {
380 case VIK_LAYER_PARAM_STRING:
381 paramdatas[i].s = g_strdup ( data.s );
382 break;
383 default:
384 paramdatas[i] = data; /* string list will have to be freed by layer. anything else not freed */
385 }
386}
387
388static VikLayerParamData uibuilder_run_getparam ( VikLayerParamData *params_defaults, guint16 i )
389{
390 return params_defaults[i];
391}
392
393
394VikLayerParamData *a_uibuilder_run_dialog ( const gchar *dialog_name, GtkWindow *parent, VikLayerParam *params,
395 guint16 params_count, gchar **groups, guint8 groups_count,
396 VikLayerParamData *params_defaults )
397{
398 VikLayerParamData *paramdatas = g_new(VikLayerParamData, params_count);
399 if ( a_uibuilder_properties_factory ( dialog_name,
400 parent,
401 params,
402 params_count,
403 groups,
404 groups_count,
405 (gpointer) uibuilder_run_setparam,
406 paramdatas,
407 params,
408 (gpointer) uibuilder_run_getparam,
409 params_defaults ) > 0 ) {
410
411 return paramdatas;
412 }
413 g_free ( paramdatas );
414 return NULL;
415}
416
417/* frees data from last (if ness) */
418void a_uibuilder_free_paramdatas ( VikLayerParamData *paramdatas, VikLayerParam *params, guint16 params_count )
419{
420 int i;
421 /* may have to free strings, etc. */
422 for ( i = 0; i < params_count; i++ ) {
423 switch ( params[i].type ) {
424 case VIK_LAYER_PARAM_STRING:
425 g_free ( (gchar *) paramdatas[i].s );
426 break;
427 case VIK_LAYER_PARAM_STRING_LIST: {
428 /* should make a util function out of this */
429 GList *iter = paramdatas[i].sl;
430 while ( iter ) {
431 g_free ( iter->data );
432 iter = iter->next;
433 }
434 g_list_free ( paramdatas[i].sl );
435 break;
436 }
437 }
438 }
439 g_free ( paramdatas );
440}