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