c2a1e3386e141eb5addd26312a31f8e0141a1f89
[metze/wireshark/wip.git] / ui / gtk / extcap_gtk.c
1 /* extcap_gtk.c
2  *
3  * Wireshark - Network traffic analyzer
4  * By Gerald Combs <gerald@wireshark.org>
5  * Copyright 1998 Gerald Combs
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  */
21
22 #include "config.h"
23
24 #include <string.h>
25
26 #include <gtk/gtk.h>
27
28 #include <ui/gtk/gui_utils.h>
29 #include <wsutil/filesystem.h>
30
31 #include "extcap_gtk.h"
32
33 #include "log.h"
34
35 static gboolean extcap_gtk_count_tree_elements(GtkTreeModel *model, GtkTreePath *path, GtkTreeIter *iter, gpointer data)
36 {
37     int *ptr_count = (int*)data;
38     gboolean multi_enabled;
39     (void)path;
40
41     g_assert(ptr_count != NULL);
42
43     gtk_tree_model_get(model, iter,
44             EXTCAP_GTK_MULTI_COL_CHECK, &multi_enabled, -1);
45
46     if (multi_enabled)
47     {
48         ++(*ptr_count);
49     }
50
51     return FALSE; /* Continue iteration. */
52 }
53
54 typedef struct _extcap_gtk_multi_fill_cb_data
55 {
56     gchar **list;
57     int num;
58     int max;
59 } extcap_gtk_multi_fill_cb_data;
60
61 static gboolean extcap_gtk_fill_multi_list(GtkTreeModel *model, GtkTreePath *path, GtkTreeIter *iter, gpointer data)
62 {
63     extcap_gtk_multi_fill_cb_data *ptr_data = (extcap_gtk_multi_fill_cb_data*)data;
64     gboolean multi_enabled;
65     extcap_value *value;
66     (void)path;
67
68     g_assert(ptr_data != NULL);
69
70     gtk_tree_model_get(model, iter,
71             EXTCAP_GTK_MULTI_COL_CHECK, &multi_enabled,
72             EXTCAP_GTK_MULTI_COL_VALUE, &value, -1);
73
74     if (multi_enabled)
75     {
76         g_assert(ptr_data->num < ptr_data->max);
77
78         if (ptr_data->num < ptr_data->max)
79         {
80             ptr_data->list[ptr_data->num] = g_strdup(value->call);
81             ptr_data->num++;
82         }
83     }
84
85     return FALSE; /* Continue iteration. */
86 }
87
88 typedef struct _extcap_gtk_multi_find_cb_data
89 {
90     gchar *parent;
91     GtkTreeIter *parent_iter;
92 } extcap_gtk_multi_find_cb_data;
93
94 static gboolean extcap_gtk_find_parent_in_multi_list(GtkTreeModel *model, GtkTreePath *path, GtkTreeIter *iter, gpointer data)
95 {
96     extcap_gtk_multi_find_cb_data *ptr_data = (extcap_gtk_multi_find_cb_data*)data;
97     extcap_value *value;
98     (void)path;
99
100     g_assert(ptr_data != NULL);
101
102     gtk_tree_model_get(model, iter,
103             EXTCAP_GTK_MULTI_COL_VALUE, &value, -1);
104
105     if (0 == g_strcmp0(ptr_data->parent, value->call))
106     {
107         ptr_data->parent_iter = gtk_tree_iter_copy(iter);
108         return TRUE; /* Stop iteration. */
109     }
110
111     return FALSE; /* Continue iteration. */
112 }
113
114 GHashTable *extcap_gtk_get_state(GtkWidget *widget) {
115     GSList *widget_list, *widget_iter;
116     GSList *radio_list = NULL, *radio_iter = NULL;
117
118     GtkWidget *list_widget, *radio_widget, *tree_widget, *entry_widget;
119
120     extcap_arg *arg = NULL;
121     extcap_value *value = NULL;
122     extcap_complex *parsed_complex = NULL;
123
124     GtkTreeSelection *treeselection;
125     GtkTreeModel *treemodel;
126     GtkTreeIter treeiter;
127
128     GHashTable *ret_hash;
129
130     gchar *call_string = NULL;
131
132     extcap_gtk_multi_fill_cb_data multi_data = { NULL, 0, 0 };
133
134     int multi_num = 0;
135
136     widget_list = (GSList *) g_object_get_data(G_OBJECT(widget),
137     EXTCAP_GTK_DATA_KEY_WIDGETLIST);
138
139     if (widget_list == NULL)
140         return NULL ;
141
142     /* String hash */
143     ret_hash = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
144
145     for (widget_iter = widget_list; widget_iter; widget_iter =
146             widget_iter->next) {
147         list_widget = (GtkWidget *) widget_iter->data;
148
149         if ((arg = (extcap_arg *) g_object_get_data(G_OBJECT(list_widget),
150         EXTCAP_GTK_DATA_KEY_ARGPTR)) == NULL) {
151             continue;
152         }
153
154         switch (arg->arg_type) {
155         case EXTCAP_ARG_INTEGER:
156         case EXTCAP_ARG_UNSIGNED:
157         case EXTCAP_ARG_LONG:
158         case EXTCAP_ARG_DOUBLE:
159         case EXTCAP_ARG_STRING:
160             parsed_complex = extcap_parse_complex(arg->arg_type,
161                     gtk_entry_get_text(GTK_ENTRY(list_widget)));
162             if (parsed_complex == NULL) {
163                 continue;
164             }
165             break;
166         case EXTCAP_ARG_BOOLEAN:
167         case EXTCAP_ARG_BOOLFLAG:
168             parsed_complex = extcap_parse_complex(arg->arg_type,
169                     gtk_toggle_button_get_active(
170                             GTK_TOGGLE_BUTTON(list_widget)) ? "true" : "false");
171             break;
172         case EXTCAP_ARG_FILESELECT:
173             if ((entry_widget =
174                     (GtkWidget *) g_object_get_data(G_OBJECT(list_widget),
175                             EXTCAP_GTK_DATA_KEY_FILENAME)) == NULL) {
176                 continue;
177             }
178             parsed_complex = extcap_parse_complex(arg->arg_type,
179                     gtk_entry_get_text(GTK_ENTRY(entry_widget)));
180             if (parsed_complex == NULL) {
181                 continue;
182             }
183             break;
184         case EXTCAP_ARG_RADIO:
185             if ((radio_widget = (GtkWidget *) g_object_get_data(
186                     G_OBJECT(list_widget),
187                     EXTCAP_GTK_DATA_KEY_FIRSTRADIO)) == NULL) {
188                 continue;
189             }
190
191             if ((radio_list = gtk_radio_button_get_group(
192                     GTK_RADIO_BUTTON(radio_widget))) == NULL) {
193                 continue;
194             }
195
196             for (radio_iter = radio_list; radio_iter;
197                     radio_iter = radio_iter->next) {
198                 GtkWidget *cur_radio = (GtkWidget *) radio_iter->data;
199
200                 if (gtk_toggle_button_get_active(
201                         GTK_TOGGLE_BUTTON(cur_radio))) {
202                     if ((value = (extcap_value *) g_object_get_data(
203                             G_OBJECT(cur_radio),
204                             EXTCAP_GTK_DATA_KEY_VALPTR)) == NULL) {
205                         continue;
206                     }
207
208                     if (value->is_default)
209                         continue;
210
211                     call_string = g_strdup(value->call);
212                     break;
213                 }
214             }
215
216             break;
217         case EXTCAP_ARG_SELECTOR:
218             if ((tree_widget = (GtkWidget *) g_object_get_data(
219                     G_OBJECT(list_widget),
220                     EXTCAP_GTK_DATA_KEY_TREEVIEW)) == NULL) {
221                 continue;
222             }
223
224             treeselection = gtk_tree_view_get_selection(
225                     GTK_TREE_VIEW(tree_widget));
226             treemodel = gtk_tree_view_get_model(GTK_TREE_VIEW(tree_widget));
227             if (gtk_tree_selection_get_selected(treeselection, &treemodel,
228                     &treeiter)) {
229                 gtk_tree_model_get(treemodel, &treeiter, EXTCAP_GTK_COL_VALUE,
230                         &value, -1);
231
232                 if (value->is_default)
233                     continue;
234
235                 call_string = g_strdup(value->call);
236             }
237
238             break;
239         case EXTCAP_ARG_MULTICHECK:
240             if ((tree_widget = (GtkWidget *) g_object_get_data(
241                     G_OBJECT(list_widget),
242                     EXTCAP_GTK_DATA_KEY_TREEVIEW)) == NULL) {
243                 continue;
244             }
245
246             gtk_tree_view_get_selection(GTK_TREE_VIEW(tree_widget));
247             treemodel = gtk_tree_view_get_model(GTK_TREE_VIEW(tree_widget));
248
249             multi_num = 0;
250
251             /* Count the # of items enabled */
252             gtk_tree_model_foreach(treemodel, extcap_gtk_count_tree_elements,
253                     &multi_num);
254
255             if (multi_num > 0)
256             {
257                 multi_data.list = g_new(gchar *, multi_num + 1);
258                 multi_data.num = 0;
259                 multi_data.max = multi_num;
260
261                 multi_num = 0;
262
263                 /* Get values list of items enabled */
264                 gtk_tree_model_foreach(treemodel, extcap_gtk_fill_multi_list,
265                         &multi_data);
266
267                 multi_data.list[multi_data.max] = NULL;
268
269                 call_string = g_strjoinv(",", multi_data.list);
270
271                 g_strfreev(multi_data.list);
272             }
273             else
274             {
275                 /* There are no enabled items. Skip this argument from command line. */
276                 continue;
277             }
278
279             break;
280         default:
281             break;
282         }
283
284         if (parsed_complex == NULL && call_string == NULL)
285             continue;
286
287
288         /* Flags are set as is, and have not true/false switch */
289         if (arg->arg_type == EXTCAP_ARG_BOOLFLAG)
290         {
291             if (extcap_complex_get_bool(parsed_complex) == TRUE)
292             {
293                 /* Include boolflag call in list.
294                  * Only arg->call should appear in commandline arguments.
295                  * Setting call_string to NULL here makes it perform as desired.
296                  */
297                 call_string = NULL;
298             }
299             else
300             {
301                  /* Omit boolflag call from list. */
302                  g_free(parsed_complex);
303                  parsed_complex = NULL;
304                  continue;
305             }
306         }
307         else
308         {
309             /* Comparing if the user has changed the value at all, and ignoring it if so.
310              * This does not apply to EXTCAP_ARG_BOOLFLAG.
311              */
312             if (extcap_compare_is_default(arg, parsed_complex))
313                 continue;
314
315             if (parsed_complex != NULL && call_string == NULL)
316                 call_string = extcap_get_complex_as_string(parsed_complex);
317         }
318
319         g_hash_table_insert(ret_hash, g_strdup(arg->call),
320                 g_strdup(call_string));
321
322         g_free(call_string);
323         call_string = NULL;
324
325         g_free(parsed_complex);
326         parsed_complex = NULL;
327     }
328
329     return ret_hash;
330 }
331
332 static void extcap_gtk_treeview_vscroll_map_handler(GtkTreeView *treeView,
333         gpointer data) {
334     GtkWidget *padBox = (GtkWidget*) data;
335     gint x, y;
336
337     g_assert(GTK_IS_BOX(padBox));
338
339     /* Set the padding above the scrollbar to the height of the tree header window */
340     gtk_tree_view_convert_bin_window_to_widget_coords(GTK_TREE_VIEW(treeView),
341             0, 0, &x, &y);
342     gtk_widget_set_size_request(padBox, -1, y);
343 }
344
345 static GtkWidget *extcap_gtk_wrap_scroll_treeview(GtkWidget *view) {
346     GtkWidget *vscroll, *padbox, *hbox, *vbox;
347     GtkAdjustment *padj;
348
349 #if GTK_CHECK_VERSION(3, 0, 0)
350     padj = gtk_scrollable_get_vadjustment(GTK_SCROLLABLE(view));
351 #if GTK_CHECK_VERSION(3, 2, 0)
352     vscroll = gtk_scrollbar_new(GTK_ORIENTATION_VERTICAL, padj);
353 #else
354     vscroll = gtk_vscrollbar_new(padj);
355 #endif
356 #else
357     padj = gtk_tree_view_get_vadjustment(GTK_TREE_VIEW(view));
358     vscroll = gtk_vscrollbar_new(padj);
359 #endif
360
361     hbox = ws_gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0, FALSE);
362
363     /* First insert the tree view */
364     gtk_box_pack_start(GTK_BOX(hbox), view, TRUE, TRUE, 0);
365     gtk_widget_show(view);
366
367     /* Pack to the right a vbox containing a box for padding at top and scrollbar */
368     vbox = ws_gtk_box_new(GTK_ORIENTATION_VERTICAL, 0, FALSE);
369     gtk_box_pack_start(GTK_BOX(hbox), vbox, FALSE, FALSE, 0);
370     gtk_widget_show(vbox);
371
372     padbox = ws_gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0, FALSE);
373     gtk_box_pack_start(GTK_BOX(vbox), padbox, FALSE, FALSE, 0);
374     gtk_widget_show(padbox);
375
376     gtk_box_pack_start(GTK_BOX(vbox), vscroll, TRUE, TRUE, 0);
377     gtk_widget_show(vscroll);
378
379     g_object_set_data(G_OBJECT(hbox), EXTCAP_GTK_DATA_KEY_TREEVIEW, view);
380
381     g_signal_connect(view, "map",
382             G_CALLBACK(extcap_gtk_treeview_vscroll_map_handler), padbox);
383
384     return hbox;
385 }
386
387 GtkWidget *extcap_create_gtk_listwidget(extcap_arg *argument,
388         GHashTable *prev_map) {
389     GtkCellRenderer *renderer;
390     GtkTreeModel *model;
391     GtkWidget *view, *retview;
392     GtkListStore *store;
393     GtkTreeIter iter;
394     GtkTreeSelection *selection;
395     extcap_value *v = NULL;
396     GList * walker = NULL;
397     gchar *prev_item = NULL;
398
399     if (g_list_length(argument->values) == 0)
400         return NULL ;
401
402     view = gtk_tree_view_new();
403
404     selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(view));
405
406     store = gtk_list_store_new(EXTCAP_GTK_NUM_COLS, G_TYPE_STRING,
407             G_TYPE_POINTER);
408
409     model = GTK_TREE_MODEL(store);
410     gtk_tree_view_set_model(GTK_TREE_VIEW(view), model);
411     gtk_tree_selection_set_mode(selection, GTK_SELECTION_SINGLE);
412
413     if (prev_map != NULL)
414         prev_item = (gchar *) g_hash_table_lookup(prev_map, argument->call);
415
416     for (walker = g_list_first(argument->values); walker != NULL ; walker =
417             walker->next) {
418         v = (extcap_value *) walker->data;
419         if (v->display == NULL)
420             break;
421
422         gtk_list_store_append(store, &iter);
423         gtk_list_store_set(store, &iter, EXTCAP_GTK_COL_DISPLAY, v->display,
424                 EXTCAP_GTK_COL_VALUE, v, -1);
425
426         if (prev_item != NULL) {
427             if (g_ascii_strcasecmp(prev_item, v->call) == 0) {
428                 gtk_tree_selection_select_iter(selection, &iter);
429             }
430         } else if (v->is_default) {
431             gtk_tree_selection_select_iter(selection, &iter);
432         }
433     }
434
435     renderer = gtk_cell_renderer_text_new();
436     gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(view), -1, "Name",
437             renderer, "text", EXTCAP_GTK_COL_DISPLAY, NULL);
438
439     gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(view), FALSE);
440
441     retview = extcap_gtk_wrap_scroll_treeview(view);
442
443     if (gtk_tree_model_iter_n_children(model, NULL) > 3)
444         gtk_widget_set_size_request(retview, 0, 100);
445
446     /* Tree view has own reference */
447     g_object_unref(model);
448
449     return retview;
450 }
451
452 GtkWidget *extcap_create_gtk_radiowidget(extcap_arg *argument,
453         GHashTable *prev_map) {
454     GtkWidget *radiobox = NULL, *last_radio = NULL;
455     extcap_value *v = NULL;
456     GList * walker = NULL;
457     gchar *prev_item = NULL;
458
459     if (g_list_length(argument->values) == 0)
460         return NULL ;
461
462     if (prev_map != NULL)
463         prev_item = (gchar *) g_hash_table_lookup(prev_map, argument->call);
464
465     radiobox = ws_gtk_box_new(GTK_ORIENTATION_VERTICAL, 1, FALSE);
466
467     for (walker = g_list_first(argument->values); walker != NULL ; walker =
468             walker->next) {
469         v = (extcap_value *) walker->data;
470
471         if (last_radio == NULL) {
472             last_radio = gtk_radio_button_new_with_label(NULL, v->display);
473             /* Set a pointer to the first radio button */
474             g_object_set_data(G_OBJECT(radiobox),
475                     EXTCAP_GTK_DATA_KEY_FIRSTRADIO, last_radio);
476         } else {
477             last_radio = gtk_radio_button_new_with_label_from_widget(
478                     GTK_RADIO_BUTTON(last_radio), v->display);
479         }
480
481         /* Set a pointer to the value used in this radio */
482         g_object_set_data(G_OBJECT(last_radio), EXTCAP_GTK_DATA_KEY_VALPTR, v);
483
484         if (prev_item != NULL) {
485             if (g_ascii_strcasecmp(prev_item, v->call) == 0) {
486                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(last_radio),
487                         TRUE);
488             }
489         } else if (v->is_default) {
490             gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(last_radio), TRUE);
491         }
492
493         gtk_box_pack_start(GTK_BOX(radiobox), last_radio, TRUE, TRUE, 0);
494         gtk_widget_show(last_radio);
495     }
496
497     return radiobox;
498 }
499
500 static void extcap_gtk_multicheck_toggled(GtkCellRendererToggle *cell _U_,
501         gchar *path_str, gpointer data) {
502     GtkTreeModel *model = (GtkTreeModel *) data;
503     GtkTreeIter iter;
504     GtkTreePath *path = gtk_tree_path_new_from_string(path_str);
505     gboolean enabled;
506
507     gtk_tree_model_get_iter(model, &iter, path);
508     gtk_tree_model_get(model, &iter, EXTCAP_GTK_MULTI_COL_CHECK, &enabled, -1);
509
510     enabled ^= 1;
511
512     gtk_tree_store_set(GTK_TREE_STORE(model), &iter, EXTCAP_GTK_MULTI_COL_CHECK,
513             enabled, -1);
514
515     gtk_tree_path_free(path);
516 }
517
518 GtkWidget *extcap_create_gtk_rangewidget(extcap_arg *argument,
519         GHashTable *prev_map _U_) {
520     GtkWidget *spinButton;
521     GtkAdjustment *adjustment;
522
523     gfloat def = 0.0f, min = 0.0f, max = 0.0f;
524
525     switch (argument->arg_type) {
526     case EXTCAP_ARG_INTEGER:
527         def = (gfloat) extcap_complex_get_int(argument->default_complex);
528         min = (gfloat) extcap_complex_get_int(argument->range_start);
529         max = (gfloat) extcap_complex_get_int(argument->range_end);
530         break;
531     case EXTCAP_ARG_UNSIGNED:
532         def = (gfloat) extcap_complex_get_uint(argument->default_complex);
533         min = (gfloat) extcap_complex_get_uint(argument->range_start);
534         max = (gfloat) extcap_complex_get_uint(argument->range_end);
535         break;
536     case EXTCAP_ARG_LONG:
537         def = (gfloat) extcap_complex_get_long(argument->default_complex);
538         min = (gfloat) extcap_complex_get_long(argument->range_start);
539         max = (gfloat) extcap_complex_get_long(argument->range_end);
540         break;
541     case EXTCAP_ARG_DOUBLE:
542         def = (gfloat) extcap_complex_get_double(argument->default_complex);
543         min = (gfloat) extcap_complex_get_double(argument->range_start);
544         max = (gfloat) extcap_complex_get_double(argument->range_end);
545         break;
546     default:
547         return NULL ;
548         break;
549     }
550
551     adjustment = (GtkAdjustment *)gtk_adjustment_new(def, min, max, 1.0, 10.0, 0.0);
552
553     spinButton = gtk_spin_button_new(adjustment, 0, 0);
554     gtk_spin_button_set_wrap(GTK_SPIN_BUTTON(spinButton), TRUE);
555     gtk_widget_set_size_request(spinButton, 80, -1);
556
557     return spinButton;
558 }
559
560 static void extcap_file_selectiondialog( GtkWidget *widget _U_, gpointer data )
561 {
562     GtkWidget * filechooser = NULL;
563     GtkFileChooserAction action = GTK_FILE_CHOOSER_ACTION_SAVE;
564     gchar *filename = NULL;
565     gint res = 0;
566     extcap_arg *argument = NULL;
567
568     if ( GTK_ENTRY(data) == NULL )
569         return;
570
571     argument = (extcap_arg *)g_object_get_data(G_OBJECT(data), EXTCAP_GTK_DATA_KEY_ARGUMENT);
572     if ( argument != NULL && argument->fileexists == TRUE )
573         action = GTK_FILE_CHOOSER_ACTION_OPEN;
574
575     filechooser = gtk_file_chooser_dialog_new("Select file path", NULL, action,
576             "_Cancel", GTK_RESPONSE_CANCEL, "_Open", GTK_RESPONSE_ACCEPT, NULL);
577
578     res = gtk_dialog_run (GTK_DIALOG (filechooser));
579     if (res == GTK_RESPONSE_ACCEPT)
580     {
581         GtkFileChooser *chooser = GTK_FILE_CHOOSER (filechooser);
582         filename = gtk_file_chooser_get_filename (chooser);
583
584         /* this check might not be necessary, but just to be on the safe side */
585         if ( action == GTK_FILE_CHOOSER_ACTION_OPEN && ! file_exists ( filename ) )
586             filename = g_strdup ( " " );
587
588         gtk_entry_set_text(GTK_ENTRY(data), filename);
589     }
590
591     gtk_widget_destroy (filechooser);
592 }
593
594 static GtkWidget *extcap_create_gtk_fileselect(extcap_arg *argument,
595         GHashTable *prev_map _U_, gchar * file _U_) {
596     GtkWidget * entry = NULL;
597     GtkWidget * button = NULL;
598     GtkWidget * ret_box = NULL;
599
600     button = gtk_button_new_with_label ("...");
601     entry = gtk_entry_new();
602     if (file != NULL)
603         gtk_entry_set_text(GTK_ENTRY(entry), file);
604     gtk_editable_set_editable (GTK_EDITABLE (entry), FALSE);
605     g_object_set_data(G_OBJECT(entry), EXTCAP_GTK_DATA_KEY_ARGUMENT, argument);
606
607 #if GTK_CHECK_VERSION(3, 0, 0)
608     ret_box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 3);
609 #else
610     ret_box = gtk_hbox_new(FALSE, 3);
611 #endif
612
613     gtk_box_pack_start ( GTK_BOX(ret_box), entry, TRUE, TRUE, 5 );
614     gtk_widget_show(entry);
615     gtk_box_pack_end ( GTK_BOX(ret_box), button, FALSE, FALSE, 5 );
616     gtk_widget_show(button);
617
618     g_signal_connect (button, "clicked",
619                   G_CALLBACK (extcap_file_selectiondialog), (gpointer) entry);
620
621     g_object_set_data(G_OBJECT(ret_box), EXTCAP_GTK_DATA_KEY_FILENAME, entry);
622
623     return ret_box;
624 }
625
626 GtkWidget *extcap_create_gtk_multicheckwidget(extcap_arg *argument,
627         GHashTable *prev_map) {
628     GtkCellRenderer *renderer, *togglerenderer;
629     GtkTreeModel *model;
630     GtkWidget *view, *retview;
631     GtkTreeStore *store;
632     GtkTreeIter iter;
633     GtkTreeSelection *selection;
634     extcap_value *v = NULL;
635     GList * walker = NULL;
636     gchar *prev_item = NULL;
637     gchar **prev_list = NULL, **prev_iter = NULL;
638     gboolean prev_value, prev_matched;
639     extcap_gtk_multi_find_cb_data find_data;
640
641     if (g_list_length(argument->values) == 0)
642         return NULL ;
643
644     view = gtk_tree_view_new();
645
646     selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(view));
647
648     store = gtk_tree_store_new(EXTCAP_GTK_MULTI_NUM_COLS, G_TYPE_BOOLEAN,
649             G_TYPE_STRING, G_TYPE_POINTER, G_TYPE_BOOLEAN);
650
651     model = GTK_TREE_MODEL(store);
652     gtk_tree_view_set_model(GTK_TREE_VIEW(view), model);
653     gtk_tree_selection_set_mode(selection, GTK_SELECTION_NONE);
654
655     if (prev_map != NULL)
656         prev_item = (gchar *) g_hash_table_lookup(prev_map, argument->call);
657
658     if (prev_item != NULL)
659         prev_list = g_strsplit(prev_item, ",", 0);
660
661     for (walker = g_list_first(argument->values); walker != NULL ; walker =
662             walker->next) {
663         v = (extcap_value *) walker->data;
664         if (v->display == NULL)
665             break;
666
667         find_data.parent = v->parent;
668         find_data.parent_iter = NULL;
669
670         if (find_data.parent != NULL)
671         {
672             gtk_tree_model_foreach(model, extcap_gtk_find_parent_in_multi_list,
673                     &find_data);
674             if (find_data.parent_iter == NULL)
675             {
676                 g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG,
677                     "Extcap parent %s not found for value %s (%s)",
678                     v->parent, v->call, argument->call);
679             }
680         }
681
682         prev_value = FALSE;
683         gtk_tree_store_append(store, &iter, find_data.parent_iter);
684
685         if (find_data.parent_iter != NULL)
686         {
687             gtk_tree_iter_free(find_data.parent_iter);
688             find_data.parent_iter = NULL;
689         }
690
691         if (prev_list != NULL) {
692             prev_matched = FALSE;
693             prev_iter = prev_list;
694
695             while (*prev_iter != NULL ) {
696                 if (g_strcmp0(*prev_iter, v->call) == 0) {
697                     prev_matched = TRUE;
698                     prev_value = TRUE;
699                     break;
700                 }
701
702                 prev_iter++;
703             }
704
705             if (prev_matched == FALSE)
706                 prev_value = v->enabled;
707         }
708         else
709         {
710             /* Use default value if there is no information about previously selected items. */
711             prev_value = v->is_default;
712         }
713
714
715         /* v->is_default is set when there was {default=true} for this value. */
716         /* v->enabled is false for non-clickable tree items ({enabled=false}). */
717         gtk_tree_store_set(store, &iter, EXTCAP_GTK_MULTI_COL_CHECK, prev_value,
718                 EXTCAP_GTK_MULTI_COL_DISPLAY, v->display,
719                 EXTCAP_GTK_MULTI_COL_VALUE, v,
720                 EXTCAP_GTK_MULTI_COL_ACTIVATABLE, v->enabled, -1);
721     }
722
723     if (prev_list != NULL)
724         g_strfreev(prev_list);
725
726     renderer = gtk_cell_renderer_text_new();
727     togglerenderer = gtk_cell_renderer_toggle_new();
728     g_signal_connect(togglerenderer, "toggled",
729             G_CALLBACK(extcap_gtk_multicheck_toggled), model);
730     gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(view), -1,
731             "Enabled", togglerenderer, "active", EXTCAP_GTK_MULTI_COL_CHECK,
732             "activatable", EXTCAP_GTK_MULTI_COL_ACTIVATABLE,
733             "visible", EXTCAP_GTK_MULTI_COL_ACTIVATABLE,
734             NULL);
735     gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(view), -1, "Name",
736             renderer, "text", EXTCAP_GTK_MULTI_COL_DISPLAY,
737             NULL);
738
739     gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(view), FALSE);
740
741     retview = extcap_gtk_wrap_scroll_treeview(view);
742
743     if (gtk_tree_model_iter_n_children(model, NULL) > 3)
744         gtk_widget_set_size_request(retview, 0, 100);
745
746     /* Tree view has own reference */
747     g_object_unref(model);
748
749     return retview;
750 }
751
752 void extcap_gtk_free_args(GtkWidget *vbox) {
753     GList *arguments = (GList *) g_object_get_data(G_OBJECT(vbox),
754             EXTCAP_GTK_DATA_KEY_ARGPTR);
755     extcap_free_arg_list(arguments);
756     g_object_set_data(G_OBJECT(vbox), EXTCAP_GTK_DATA_KEY_ARGPTR, NULL);
757 }
758
759 GSList *extcap_populate_gtk_vbox(GList *arguments, GtkWidget *vbox,
760         GHashTable *prev_map) {
761     GSList *widget_toplist = NULL;
762
763     extcap_arg *arg_iter = NULL;
764
765     extcap_complex *prev_complex = NULL;
766     gchar *prev_call, *default_str;
767
768     GList * arg_list = g_list_first(arguments);
769     if ( arg_list == NULL )
770         return NULL;
771
772     g_object_set_data(G_OBJECT(vbox), EXTCAP_GTK_DATA_KEY_ARGPTR, arguments);
773
774     while (arg_list != NULL ) {
775         GtkWidget *hbox = NULL, *label = NULL, *item = NULL;
776
777         arg_iter = (extcap_arg*) (arg_list->data);
778
779         /* A new storage box for label + element */
780
781         hbox = ws_gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 5, FALSE);
782
783         if (prev_map != NULL
784                 && (prev_call = (gchar *) g_hash_table_lookup(prev_map,
785                         arg_iter->call)) != NULL) {
786             prev_complex = extcap_parse_complex(arg_iter->arg_type, prev_call);
787         } else {
788             prev_complex = NULL;
789         }
790
791         switch (arg_iter->arg_type) {
792         case EXTCAP_ARG_INTEGER:
793         case EXTCAP_ARG_UNSIGNED:
794         case EXTCAP_ARG_LONG:
795         case EXTCAP_ARG_DOUBLE:
796             label = gtk_label_new(arg_iter->display);
797             gtk_misc_set_alignment(GTK_MISC(label), 0.0f, 0.1f);
798             item = extcap_create_gtk_rangewidget(arg_iter, prev_map);
799             if (item == NULL) {
800                 item = gtk_entry_new();
801
802                 if (prev_complex != NULL) {
803                     default_str = extcap_get_complex_as_string(prev_complex);
804                     gtk_entry_set_text(GTK_ENTRY(item), default_str);
805                     g_free(default_str);
806                 } else if (arg_iter->default_complex != NULL) {
807                     default_str = extcap_get_complex_as_string(
808                             arg_iter->default_complex);
809                     gtk_entry_set_text(GTK_ENTRY(item), default_str);
810                     g_free(default_str);
811                 }
812             }
813             break;
814         case EXTCAP_ARG_STRING:
815             label = gtk_label_new(arg_iter->display);
816
817             item = gtk_entry_new();
818             default_str = NULL;
819
820             if (prev_complex != NULL)
821                 default_str = extcap_get_complex_as_string(prev_complex);
822             else if (arg_iter->default_complex != NULL)
823                 default_str = extcap_get_complex_as_string(
824                         arg_iter->default_complex);
825
826             if (default_str != NULL) {
827                 gtk_entry_set_text(GTK_ENTRY(item), default_str);
828                 g_free(default_str);
829             }
830
831             break;
832         case EXTCAP_ARG_FILESELECT:
833             label = gtk_label_new(arg_iter->display);
834             default_str = NULL;
835
836             if (prev_complex != NULL)
837                 default_str = extcap_get_complex_as_string(prev_complex);
838             else if (arg_iter->default_complex != NULL)
839                 default_str = extcap_get_complex_as_string(
840                         arg_iter->default_complex);
841
842             gtk_misc_set_alignment(GTK_MISC(label), 0.0f, 0.1f);
843             item = extcap_create_gtk_fileselect(arg_iter, prev_map, default_str);
844             if (default_str != NULL)
845                 g_free(default_str);
846             break;
847         case EXTCAP_ARG_BOOLEAN:
848         case EXTCAP_ARG_BOOLFLAG:
849             item = gtk_check_button_new_with_label(arg_iter->display);
850
851             if (prev_complex != NULL) {
852                 if (extcap_complex_get_bool(prev_complex))
853                     gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(item), TRUE);
854             } else if (arg_iter->default_complex != NULL
855                     && extcap_complex_get_bool(arg_iter->default_complex)) {
856                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(item), TRUE);
857             }
858
859             break;
860         case EXTCAP_ARG_RADIO:
861             label = gtk_label_new(arg_iter->display);
862             gtk_misc_set_alignment(GTK_MISC(label), 0.0f, 0.1f);
863             item = extcap_create_gtk_radiowidget(arg_iter, prev_map);
864             break;
865         case EXTCAP_ARG_SELECTOR:
866             label = gtk_label_new(arg_iter->display);
867             gtk_misc_set_alignment(GTK_MISC(label), 0.0f, 0.1f);
868             item = extcap_create_gtk_listwidget(arg_iter, prev_map);
869             break;
870         case EXTCAP_ARG_MULTICHECK:
871             label = gtk_label_new(arg_iter->display);
872             gtk_misc_set_alignment(GTK_MISC(label), 0.0f, 0.1f);
873             item = extcap_create_gtk_multicheckwidget(arg_iter, prev_map);
874             break;
875         default:
876             break;
877         }
878
879         if (prev_complex != NULL)
880             extcap_free_complex(prev_complex);
881
882         if (label != NULL) {
883             gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 5);
884             gtk_widget_show(label);
885         }
886
887         if (item != NULL) {
888             gtk_box_pack_start(GTK_BOX(hbox), item, TRUE, TRUE, 0);
889             gtk_widget_show(item);
890             g_object_set_data(G_OBJECT(item), EXTCAP_GTK_DATA_KEY_ARGPTR,
891                     arg_iter);
892
893             if (arg_iter->tooltip != NULL) {
894                 gtk_widget_set_tooltip_text(item, arg_iter->tooltip);
895             }
896
897             widget_toplist = g_slist_append(widget_toplist, item);
898         }
899
900         gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 1);
901
902         gtk_widget_show(hbox);
903
904         arg_list = arg_list->next;
905     }
906
907     return widget_toplist;
908 }
909
910 /*
911  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
912  *
913  * Local variables:
914  * c-basic-offset: 4
915  * tab-width: 8
916  * indent-tabs-mode: nil
917  * End:
918  *
919  * vi: set shiftwidth=4 tabstop=8 expandtab:
920  * :indentSize=4:tabSize=8:noTabs=true:
921  */