]> git.street.me.uk Git - andy/viking.git/blob - src/uibuilder.c
[QA] Better usage of enumeration type to identify layers.
[andy/viking.git] / src / uibuilder.c
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
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         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         gtk_widget_set_tooltip_text ( GTK_WIDGET(rv),
147                                      _("Take care that this password will be stored clearly in a plain file.") );
148       }
149       break;
150     case VIK_LAYER_WIDGET_FILEENTRY:
151       if ( param->type == VIK_LAYER_PARAM_STRING )
152       {
153         rv = vik_file_entry_new (GTK_FILE_CHOOSER_ACTION_OPEN);
154         vik_file_entry_set_filename ( VIK_FILE_ENTRY(rv), data.s );
155       }
156       break;
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
165     case VIK_LAYER_WIDGET_FILELIST:
166       if ( param->type == VIK_LAYER_PARAM_STRING_LIST )
167       {
168         rv = vik_file_list_new ( _(param->title) );
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   }
183   if ( rv && !gtk_widget_get_tooltip_text ( rv ) ) {
184     if ( param->tooltip )
185       gtk_widget_set_tooltip_text ( rv, _(param->tooltip) );
186   }
187   return rv;
188 }
189
190 VikLayerParamData 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
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       }
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 )
222         rv.u = GPOINTER_TO_UINT ( g_list_nth_data(param->extra_widget_data, rv.u) );
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:
233     case VIK_LAYER_WIDGET_PASSWORD:
234       rv.s = gtk_entry_get_text ( GTK_ENTRY(widget) );
235       break;
236     case VIK_LAYER_WIDGET_FILEENTRY:
237     case VIK_LAYER_WIDGET_FOLDERENTRY:
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
256 gint a_uibuilder_properties_factory ( const gchar *dialog_name, GtkWindow *parent, VikLayerParam *params,
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 */
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 */
279     GtkWidget *dialog = gtk_dialog_new_with_buttons ( dialog_name,
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 );
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
289     gint resp;
290
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
331         widgets[j] = a_uibuilder_new_widget ( &(params[i]), getparam ( pass_along_getparam, i, FALSE ) );
332
333         g_assert ( widgets[j] != NULL );
334
335         gtk_table_attach ( GTK_TABLE(table), gtk_label_new(_(params[i].title)), 0, 1, j, j+1, 0, 0, 0, 0 );
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
341     if ( response_w )
342       gtk_widget_grab_focus ( response_w );
343
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         {
353           if ( setparam ( pass_along1,
354                           i,
355                           a_uibuilder_widget_get_value ( widgets[j], &(params[i]) ),
356                           pass_along2,
357                           FALSE ) )
358             must_redraw = TRUE;
359           j++;
360         }
361       }
362
363       gtk_widget_destroy ( dialog ); /* hide before redrawing. */
364       g_free ( widgets );
365       if ( tables )
366         g_free ( tables );
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
380 static 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
392 static VikLayerParamData uibuilder_run_getparam ( VikLayerParamData *params_defaults, guint16 i )
393 {
394   return params_defaults[i];
395 }
396
397
398 VikLayerParamData *a_uibuilder_run_dialog (  const gchar *dialog_name, GtkWindow *parent, VikLayerParam *params,
399                         guint16 params_count, gchar **groups, guint8 groups_count,
400                         VikLayerParamData *params_defaults )
401 {
402     VikLayerParamData *paramdatas = g_new(VikLayerParamData, params_count);
403     if ( a_uibuilder_properties_factory ( dialog_name,
404                                           parent,
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 ) {
414
415       return paramdatas;
416     }
417     g_free ( paramdatas );
418     return NULL;
419 }
420
421 /* frees data from last (if ness) */
422 void 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       default:
441         break;
442       }
443     }
444   }
445   g_free ( paramdatas );
446 }