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