Added "Edit Column Details" functions to the packet list heading popup.
[obnox/wireshark/wip.git] / gtk / prefs_column.c
1 /* column_prefs.c
2  * Dialog box for column preferences
3  *
4  * $Id$
5  *
6  * Wireshark - Network traffic analyzer
7  * By Gerald Combs <gerald@wireshark.org>
8  * Copyright 1998 Gerald Combs
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
23  */
24
25 #ifdef HAVE_CONFIG_H
26 #include "config.h"
27 #endif
28
29 #include <string.h>
30
31 #include <gtk/gtk.h>
32
33 #include <epan/prefs.h>
34 #include <epan/column_info.h>
35 #include <epan/column.h>
36 #include <epan/strutil.h>
37
38 #include "../globals.h"
39
40 #include "gtk/prefs_column.h"
41 #include "gtk/gtkglobals.h"
42 #include "gtk/gui_utils.h"
43 #ifdef NEW_PACKET_LIST
44 #include "gtk/new_packet_list.h"
45 #else
46 #include "gtk/main_packet_list.h"
47 #endif
48 #include "gtk/filter_dlg.h"
49 #include "gtk/filter_autocomplete.h"
50
51
52 static GtkWidget *remove_bt, *field_te, *field_lb, *occurrence_te, *occurrence_lb, *fmt_cmb;
53 static gulong column_menu_changed_handler_id;
54 static gulong column_field_changed_handler_id;
55 static gulong column_occurrence_changed_handler_id;
56 static gulong column_row_deleted_handler_id;
57
58 static void column_list_new_cb(GtkWidget *, gpointer);
59 static void column_list_delete_cb(GtkWidget *, gpointer);
60 static void column_list_select_cb(GtkTreeSelection *, gpointer);
61 static void column_menu_changed_cb(GtkWidget *, gpointer);
62 static void column_field_changed_cb(GtkEditable *, gpointer);
63 static void column_occurrence_changed_cb(GtkEditable *, gpointer);
64 static void column_dnd_row_deleted_cb(GtkTreeModel *, GtkTreePath *, gpointer);
65 static gboolean column_title_changed_cb(GtkCellRendererText *, const gchar *, const gchar *, gpointer);
66
67 static char custom_occurrence_str[8] = "";
68
69 enum {
70 #ifdef NEW_PACKET_LIST
71   VISIBLE_COLUMN,
72 #endif
73   TITLE_COLUMN,
74   FORMAT_COLUMN,
75   DATA_COLUMN,
76   N_COLUMN /* The number of columns */
77 };
78
79 #ifdef NEW_PACKET_LIST
80 /* Visible toggled */
81 static void
82 visible_toggled(GtkCellRendererToggle *cell _U_, gchar *path_str, gpointer data)
83 {
84   GtkTreeModel    *model = (GtkTreeModel *)data;
85   GtkTreeIter      iter;
86   GtkTreePath     *path = gtk_tree_path_new_from_string(path_str);
87   GList           *clp;
88   fmt_data        *cfmt;
89
90   gtk_tree_model_get_iter(model, &iter, path);
91   gtk_tree_model_get(model, &iter, DATA_COLUMN, &clp, -1);
92
93   cfmt = (fmt_data *) clp->data;
94   if (cfmt->visible)
95     cfmt->visible = FALSE;
96   else
97     cfmt->visible = TRUE;
98
99   gtk_list_store_set(GTK_LIST_STORE(model), &iter, VISIBLE_COLUMN, cfmt->visible, -1);
100   cfile.cinfo.columns_changed = TRUE;
101
102   gtk_tree_path_free(path);
103 } /* visible_toggled */
104 #endif
105
106 /*
107  * Create and display the column selection widgets.
108  * Called as part of the creation of the Preferences notebook ( Edit ! Preferences )
109  */
110 GtkWidget *
111 column_prefs_show(GtkWidget *prefs_window) {
112     GtkWidget         *main_vb, *bottom_hb, *column_l, *add_bt, *tb, *lb;
113     GtkWidget         *list_vb, *list_lb, *list_sc;
114     GtkWidget         *add_remove_vb;
115     GtkWidget         *props_fr, *props_hb;
116     GList             *clp;
117     fmt_data          *cfmt;
118     gint               i;
119     gchar             *fmt;
120     gint               cur_fmt;
121 #ifdef NEW_PACKET_LIST
122     const gchar       *column_titles[] = {"Displayed", "Title", "Field type"};
123 #else
124     const gchar       *column_titles[] = {"Title", "Field type"};
125 #endif
126     GtkListStore      *store;
127     GtkCellRenderer   *renderer;
128     GtkTreeViewColumn *column;
129     GtkTreeSelection  *sel;
130     GtkTreeIter        iter;
131     GtkTreeIter        first_iter;
132     gint               first_row = TRUE;
133
134     GtkTooltips   *tooltips = gtk_tooltips_new();
135
136     /* Container for each row of widgets */
137     main_vb = gtk_vbox_new(FALSE, 5);
138     gtk_container_set_border_width(GTK_CONTAINER(main_vb), 5);
139     gtk_widget_show(main_vb);
140
141     list_vb = gtk_vbox_new (FALSE, 0);
142     gtk_container_set_border_width  (GTK_CONTAINER (list_vb), 5);
143     gtk_widget_show (list_vb);
144     gtk_box_pack_start (GTK_BOX (main_vb), list_vb, TRUE, TRUE, 0);
145
146     list_lb = gtk_label_new (("[The first list entry will be displayed as the leftmost column - Drag and drop entries to change column order]"));
147     gtk_widget_show (list_lb);
148     gtk_box_pack_start (GTK_BOX (list_vb), list_lb, FALSE, FALSE, 0);
149
150     list_sc = scrolled_window_new(NULL, NULL);
151     gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(list_sc), GTK_SHADOW_IN);
152     gtk_container_add(GTK_CONTAINER(list_vb), list_sc);
153     gtk_widget_show(list_sc);
154
155     store = gtk_list_store_new(N_COLUMN,
156 #ifdef NEW_PACKET_LIST
157                                G_TYPE_BOOLEAN,
158 #endif
159                                G_TYPE_STRING, G_TYPE_STRING, G_TYPE_POINTER);
160     column_row_deleted_handler_id = 
161         g_signal_connect(GTK_TREE_MODEL(store), "row-deleted", G_CALLBACK(column_dnd_row_deleted_cb), NULL);
162
163     column_l = tree_view_new(GTK_TREE_MODEL(store));
164     gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(column_l), TRUE);
165     gtk_tree_view_set_headers_clickable(GTK_TREE_VIEW(column_l), FALSE);
166     gtk_tree_view_set_reorderable(GTK_TREE_VIEW(column_l), TRUE);
167     gtk_tooltips_set_tip (tooltips, column_l, 
168         "Click on a title to change its name.\nDrag an item to change its order.", NULL);
169
170 #ifdef NEW_PACKET_LIST
171     renderer = gtk_cell_renderer_toggle_new();
172     g_signal_connect(renderer, "toggled", G_CALLBACK(visible_toggled), store);
173     column = gtk_tree_view_column_new_with_attributes(column_titles[VISIBLE_COLUMN], renderer, "active", VISIBLE_COLUMN, NULL);
174     gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_AUTOSIZE);
175     gtk_tree_view_append_column(GTK_TREE_VIEW(column_l), column);
176 #endif
177
178     renderer = gtk_cell_renderer_text_new();
179     g_object_set(G_OBJECT(renderer), "editable", TRUE, NULL);
180     g_signal_connect (renderer, "edited", G_CALLBACK(column_title_changed_cb), GTK_TREE_MODEL(store));
181     column = gtk_tree_view_column_new_with_attributes(column_titles[TITLE_COLUMN], renderer, "text", TITLE_COLUMN, NULL);
182     gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_AUTOSIZE);
183     gtk_tree_view_append_column(GTK_TREE_VIEW(column_l), column);
184
185     renderer = gtk_cell_renderer_text_new();
186     column = gtk_tree_view_column_new_with_attributes(column_titles[FORMAT_COLUMN], renderer, "text", FORMAT_COLUMN, NULL);
187     gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_AUTOSIZE);
188     gtk_tree_view_append_column(GTK_TREE_VIEW(column_l), column);
189
190     /* XXX - make this match the packet list prefs? */
191     sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(column_l));
192     gtk_tree_selection_set_mode(sel, GTK_SELECTION_SINGLE);
193     g_signal_connect(sel, "changed", G_CALLBACK(column_list_select_cb), NULL);
194
195     gtk_container_add(GTK_CONTAINER(list_sc), column_l);
196     gtk_widget_show(column_l);
197
198     clp = g_list_first(prefs.col_list);
199     while (clp) {
200         cfmt    = (fmt_data *) clp->data;
201         cur_fmt = get_column_format_from_str(cfmt->fmt);
202         if (cur_fmt == COL_CUSTOM) {
203             if (cfmt->custom_occurrence) {
204                 fmt = g_strdup_printf("%s (%s#%d)", col_format_desc(cur_fmt), cfmt->custom_field, cfmt->custom_occurrence);
205             } else {
206                 fmt = g_strdup_printf("%s (%s)", col_format_desc(cur_fmt), cfmt->custom_field);
207             }
208         } else {
209             if (cfmt->custom_field) {
210                 /* Delete custom_field from previous changes */
211                 g_free (cfmt->custom_field);
212                 cfmt->custom_field = NULL;
213                 cfmt->custom_occurrence = 0;
214             }
215             fmt = g_strdup_printf("%s", col_format_desc(cur_fmt));
216         }
217 #if GTK_CHECK_VERSION(2,6,0)
218         gtk_list_store_insert_with_values(store, &iter, G_MAXINT,
219 #else
220         gtk_list_store_append(store, &iter);
221         gtk_list_store_set(store, &iter,
222 #endif
223 #ifdef NEW_PACKET_LIST
224                            VISIBLE_COLUMN, cfmt->visible,
225 #endif
226                            TITLE_COLUMN, cfmt->title, FORMAT_COLUMN, fmt, DATA_COLUMN, clp, -1);
227
228         if (first_row) {
229             first_iter = iter;
230             first_row = FALSE;
231         }
232         clp = clp->next;
233         g_free (fmt);
234     }
235     g_object_unref(G_OBJECT(store));
236
237     /* Bottom row: Add/remove buttons and properties */
238     bottom_hb = gtk_hbox_new(FALSE, 5);
239     gtk_box_pack_start (GTK_BOX (main_vb), bottom_hb, FALSE, TRUE, 0);
240     gtk_widget_show(bottom_hb);
241
242     /* Add / remove buttons */
243     add_remove_vb = gtk_vbox_new (TRUE, 0);
244     gtk_container_set_border_width (GTK_CONTAINER (add_remove_vb), 5);
245     gtk_box_pack_start (GTK_BOX (bottom_hb), add_remove_vb, FALSE, FALSE, 0);
246     gtk_widget_show(add_remove_vb);
247
248     add_bt = gtk_button_new_from_stock(GTK_STOCK_ADD);
249     g_signal_connect(add_bt, "clicked", G_CALLBACK(column_list_new_cb), column_l);
250     gtk_box_pack_start (GTK_BOX (add_remove_vb), add_bt, FALSE, FALSE, 0);
251     gtk_tooltips_set_tip (tooltips, add_bt,
252                           "Add a new column at the end of the list.", NULL);
253     gtk_widget_show(add_bt);
254
255     remove_bt = gtk_button_new_from_stock(GTK_STOCK_REMOVE);
256     gtk_widget_set_sensitive(remove_bt, FALSE);
257     g_signal_connect(remove_bt, "clicked", G_CALLBACK(column_list_delete_cb), column_l);
258     gtk_box_pack_start (GTK_BOX (add_remove_vb), remove_bt, FALSE, FALSE, 0);
259     gtk_tooltips_set_tip (tooltips, remove_bt,
260                           "Remove the selected column.", NULL);
261     gtk_widget_show(remove_bt);
262   
263     /* properties frame */
264     props_fr = gtk_frame_new("Properties");
265     gtk_box_pack_start (GTK_BOX (bottom_hb), props_fr, TRUE, TRUE, 0);
266     gtk_widget_show(props_fr);
267
268     /* Column name entry and format selection */
269     tb = gtk_table_new(2, 4, FALSE);
270     gtk_container_set_border_width(GTK_CONTAINER(tb), 5);
271     gtk_container_add(GTK_CONTAINER(props_fr), tb);
272     gtk_table_set_row_spacings(GTK_TABLE(tb), 10);
273     gtk_table_set_col_spacings(GTK_TABLE(tb), 15);
274     gtk_widget_show(tb);
275
276     lb = gtk_label_new("Field type:");
277     gtk_misc_set_alignment(GTK_MISC(lb), 0.0f, 0.5f);
278     gtk_table_attach_defaults(GTK_TABLE(tb), lb, 0, 1, 0, 1);
279     gtk_tooltips_set_tip (tooltips, lb,
280                           "Select which packet information to present in the column.", NULL);
281     gtk_widget_show(lb);
282
283     props_hb = gtk_hbox_new(FALSE, 5);
284     gtk_table_attach(GTK_TABLE(tb), props_hb, 1, 2, 0, 1, GTK_FILL, GTK_SHRINK, 0, 0);
285     gtk_tooltips_set_tip (tooltips, props_hb,
286                           "Select which packet information to present in the column.", NULL);
287     gtk_widget_show(props_hb);
288
289     field_lb = gtk_label_new("Field name:");
290     gtk_misc_set_alignment(GTK_MISC(field_lb), 0.0f, 0.5f);
291     gtk_table_attach_defaults(GTK_TABLE(tb), field_lb, 0, 1, 1, 2);
292     gtk_widget_set_sensitive(field_lb, FALSE);
293     gtk_tooltips_set_tip (tooltips, field_lb,
294                           "Field name used when field type is \"Custom\". "
295                           "This string has the same syntax as a display filter string.", NULL);
296     gtk_widget_show(field_lb);
297
298     field_te = gtk_entry_new();
299     g_object_set_data (G_OBJECT(field_te), E_FILT_FIELD_NAME_ONLY_KEY, "");
300     g_signal_connect(field_te, "changed", G_CALLBACK(filter_te_syntax_check_cb), NULL);
301
302     /* XXX: column_field_changed_cb will be called for every character entered in the entry box.      */
303     /*       Consider Changing logic so that the field is "accepted" only when a return is entered ?? */
304     /*       Also: entry shouldn't be accepted if it's not a valid filter ?                           */
305     column_field_changed_handler_id = 
306         g_signal_connect(field_te, "changed", G_CALLBACK(column_field_changed_cb), column_l);
307
308     g_object_set_data(G_OBJECT(main_vb), E_FILT_AUTOCOMP_PTR_KEY, NULL);
309     g_signal_connect(field_te, "key-press-event", G_CALLBACK (filter_string_te_key_pressed_cb), NULL);
310     g_signal_connect(prefs_window, "key-press-event", G_CALLBACK (filter_parent_dlg_key_pressed_cb), NULL);
311     colorize_filter_te_as_empty(field_te);
312     gtk_table_attach_defaults(GTK_TABLE(tb), field_te, 1, 2, 1, 2);
313     gtk_widget_set_sensitive(field_te, FALSE);
314     gtk_tooltips_set_tip (tooltips, field_te,
315                           "Field name used when field type is \"Custom\". "
316                           "This string has the same syntax as a display filter string.", NULL);
317     gtk_widget_show(field_te);
318
319     occurrence_lb = gtk_label_new("Field occurrence:");
320     gtk_misc_set_alignment(GTK_MISC(occurrence_lb), 0.0f, 0.5f);
321     gtk_table_attach_defaults(GTK_TABLE(tb), occurrence_lb, 2, 3, 1, 2);
322     gtk_widget_set_sensitive(occurrence_lb, FALSE);
323     gtk_tooltips_set_tip (tooltips, occurrence_lb,
324                           "Field occurence to use. "
325                           "0=all (default), 1=first, 2=second, ..., -1=last.", NULL);
326     gtk_widget_show(occurrence_lb);
327
328     occurrence_te = gtk_entry_new();
329     gtk_entry_set_max_length (GTK_ENTRY(occurrence_te),4);
330     g_object_set_data (G_OBJECT(occurrence_te), "occurrence", "");
331
332     /* XXX: column_occurrence_changed_cb will be called for every character entered in the entry box.      */
333     /*       Consider Changing logic so that the field is "accepted" only when a return is entered ?? */
334     column_occurrence_changed_handler_id = 
335         g_signal_connect(occurrence_te, "changed", G_CALLBACK(column_occurrence_changed_cb), column_l);
336
337     gtk_table_attach_defaults(GTK_TABLE(tb), occurrence_te, 3, 4, 1, 2);
338     gtk_widget_set_sensitive(occurrence_te, FALSE);
339     gtk_tooltips_set_tip (tooltips, occurrence_te,
340                           "Field occurence to use. "
341                           "0=all (default), 1=first, 2=second, ..., -1=last.", NULL);
342     gtk_widget_show(occurrence_te);
343
344     fmt_cmb = gtk_combo_box_new_text();
345
346     for (i = 0; i < NUM_COL_FMTS; i++)
347         gtk_combo_box_append_text(GTK_COMBO_BOX(fmt_cmb), col_format_desc(i));
348
349     column_menu_changed_handler_id = g_signal_connect(fmt_cmb, "changed", G_CALLBACK(column_menu_changed_cb), column_l);
350
351     gtk_widget_set_sensitive(fmt_cmb, FALSE);
352     gtk_box_pack_start(GTK_BOX(props_hb), fmt_cmb, FALSE, FALSE, 0);
353     gtk_widget_show(fmt_cmb);
354
355     /* select the first menu list row.             */
356     /*  Triggers call to column_list_select_cb().  */
357     gtk_tree_selection_select_iter(sel, &first_iter);
358
359     return(main_vb);
360 }
361
362 void
363 column_prefs_add_custom(gint fmt, const gchar *title, const gchar *custom_field, gint custom_occurrence)
364 {
365   GList *clp;
366   fmt_data *cfmt, *last_cfmt;
367
368   cfmt = (fmt_data *) g_malloc(sizeof(fmt_data));
369   /*
370    * Because a single underscore is interpreted as a signal that the next character
371    * is going to be marked as accelerator for this header (i.e. is going to be
372    * shown underlined), escape it be inserting a second consecutive underscore.
373    */
374   cfmt->title = g_strdup(title);
375   cfmt->fmt = g_strdup(col_format_to_string(fmt));
376   cfmt->custom_field = g_strdup(custom_field);
377   cfmt->custom_occurrence = custom_occurrence;
378   cfmt->resolved = TRUE;
379
380   if (custom_field) {
381     cfmt->visible = TRUE;
382     clp = g_list_last(prefs.col_list);
383     last_cfmt = (fmt_data *) clp->data;
384     if (strcmp(last_cfmt->fmt, "%i") == 0) {
385       /* Last column is COL_INFO, add custom column before this */
386       prefs.col_list = g_list_insert(prefs.col_list, cfmt, g_list_length(prefs.col_list)-1);
387     } else {
388       prefs.col_list = g_list_append(prefs.col_list, cfmt);
389     }
390   } else {
391     cfmt->visible = FALSE;  /* Will be set to TRUE in visible_toggled() when added to list */
392     prefs.col_list = g_list_append(prefs.col_list, cfmt);
393   }
394 }
395
396 void
397 column_prefs_remove(gint col)
398 {
399   GList    *clp = g_list_nth(prefs.col_list, col);
400   fmt_data *cfmt = (fmt_data *) clp->data;
401
402   g_free(cfmt->title);
403   g_free(cfmt->fmt);
404   g_free(cfmt->custom_field);
405   g_free(cfmt);
406   prefs.col_list = g_list_remove_link(prefs.col_list, clp);
407 }
408
409 /* To do: add input checking to each of these callbacks */
410
411 static void
412 column_list_new_cb(GtkWidget *w _U_, gpointer data) {
413     gint               cur_fmt;
414     const gchar       *title = "New Column";
415     GtkTreeView       *column_l = GTK_TREE_VIEW(data);
416     GtkTreeModel      *model;
417     GtkTreeIter        iter;
418     GtkTreePath       *path;
419     GtkTreeViewColumn *title_column;
420
421     cur_fmt = COL_NUMBER;    /*  Set the default new column type */
422     column_prefs_add_custom (cur_fmt, title, NULL, 0);
423
424     model = gtk_tree_view_get_model(column_l);
425 #if GTK_CHECK_VERSION(2,6,0)
426     gtk_list_store_insert_with_values(GTK_LIST_STORE(model), &iter, G_MAXINT,
427 #else
428     gtk_list_store_append(GTK_LIST_STORE(model), &iter);
429     gtk_list_store_set(GTK_LIST_STORE(model), &iter, 
430 #endif
431 #ifdef NEW_PACKET_LIST
432                        VISIBLE_COLUMN, TRUE,
433 #endif
434                        TITLE_COLUMN, title,
435                        FORMAT_COLUMN, col_format_desc(cur_fmt),
436                        DATA_COLUMN, g_list_last(prefs.col_list),
437                        -1);
438
439     /* Triggers call to column_list_select_cb()   */
440     gtk_tree_selection_select_iter(gtk_tree_view_get_selection(column_l), &iter);
441
442     /* Set the cursor to the 'Title' column of the newly added row and enable editing */
443     /* XXX: If displaying the new title ["New column"] widens the title column of the */
444     /*      treeview, then the set_cursor below doesn't properly generate an entry    */
445     /*      box around the title text. The width of the box appears to be the column  */
446     /*      width before the treeview title column was widened.  Seems like a bug...  */
447     /*      I haven't found a work-around.                                            */
448     path = gtk_tree_model_get_path(model, &iter);
449     title_column = gtk_tree_view_get_column(column_l, 0);
450     gtk_tree_view_set_cursor(column_l, path, title_column, TRUE);
451     gtk_tree_path_free(path);
452
453     cfile.cinfo.columns_changed = TRUE;
454 }
455
456
457 static void
458 column_list_delete_cb(GtkWidget *w _U_, gpointer data) {
459     GtkTreeView      *column_l = GTK_TREE_VIEW(data);
460     GList            *clp;
461     fmt_data         *cfmt;
462     GtkTreeSelection *sel;
463     GtkTreeModel     *model;
464     GtkTreeIter       iter;
465     GtkTreeIter       new_iter;
466
467     sel = gtk_tree_view_get_selection(column_l);
468     if (gtk_tree_selection_get_selected(sel, &model, &iter))
469     {
470         gtk_tree_model_get(model, &iter, DATA_COLUMN, &clp, -1);
471
472         cfmt = (fmt_data *) clp->data;
473         g_free(cfmt->title);
474         g_free(cfmt->fmt);
475         g_free(cfmt->custom_field);
476         g_free(cfmt);
477         prefs.col_list = g_list_remove_link(prefs.col_list, clp);
478
479         /* Change the row selection to the next row (if available) or    */
480         /*  the previous row (if available). If there's only one row     */
481         /*  in the store (no previous and no next), then the selection   */
482         /*  will not be changed.                                         */
483
484         /* Note that gtk_tree_selection_select_iter() will trigger a     */
485         /* call to column_list_select_cb().                              */
486
487         new_iter = iter;
488         if ( gtk_tree_model_iter_next(model, &new_iter)) {
489             gtk_tree_selection_select_iter(sel, &new_iter);
490         } else { /* "gtk_tree_model_iter_prev" */
491             GtkTreePath *path = gtk_tree_model_get_path(model, &iter);
492             if (gtk_tree_path_prev(path)) {
493                 gtk_tree_model_get_iter(model, &new_iter, path);
494                 gtk_tree_selection_select_iter(sel, &new_iter);
495             }
496             gtk_tree_path_free(path);
497         }
498
499         /* Remove the row from the list store.                           */
500         /*  We prevent triggering call to column_row_deleted_cb() since  */
501         /*  the entry has already been removed from prefs.col_list and   */
502         /*  since rebuilding the list is not needed because the order    */
503         /*  of the list hasn't changed.                                  */
504
505         g_signal_handler_block(model, column_row_deleted_handler_id);
506
507         /* list_store_remove below will trigger a call to                */
508         /* column_list_select_cb() only when deleting the last entry in  */
509         /* the column list.                                              */
510         /* (This is because the selection in this case changes to        */
511         /*  "nothing selected" when the last row is removed.             */
512
513         gtk_list_store_remove(GTK_LIST_STORE(model), &iter);
514
515         g_signal_handler_unblock  (model, column_row_deleted_handler_id);
516
517         cfile.cinfo.columns_changed = TRUE;
518     }
519 }
520
521
522 static gboolean
523 column_title_changed_cb(GtkCellRendererText *cell _U_, const gchar *str_path, const gchar *new_title, gpointer data) {
524     fmt_data     *cfmt;
525     GList        *clp;
526     GtkTreeModel *model = (GtkTreeModel *)data;
527     GtkTreePath  *path = gtk_tree_path_new_from_string (str_path);
528     GtkTreeIter   iter;
529
530     gtk_tree_model_get_iter(model, &iter, path); 
531   
532     gtk_list_store_set(GTK_LIST_STORE(model), &iter, TITLE_COLUMN, new_title, -1);
533
534     gtk_tree_model_get(model, &iter, DATA_COLUMN, &clp, -1);
535     if (clp) {    
536         cfmt  = (fmt_data *) clp->data;
537         g_free(cfmt->title);
538         cfmt->title = g_strdup(new_title);
539     }
540
541     gtk_tree_path_free (path);
542     cfile.cinfo.columns_changed = TRUE; 
543     return TRUE;  
544 }
545
546 /*
547  * column list row selection changed.
548  *  Set the "Properties" widgets to match the currently selected column row item.
549  */
550
551 static void
552 column_list_select_cb(GtkTreeSelection *sel, gpointer data _U_)
553 {
554     fmt_data     *cfmt;
555     gint          cur_fmt;
556     GList        *clp;
557     GtkTreeModel *model;
558     GtkTreeIter   iter;
559
560     /* if something was selected */
561     if (gtk_tree_selection_get_selected(sel, &model, &iter))
562     {
563         gtk_tree_model_get(model, &iter, DATA_COLUMN, &clp, -1);
564         g_assert(clp != NULL);
565         cfmt    = (fmt_data *) clp->data;
566         cur_fmt = get_column_format_from_str(cfmt->fmt);
567         g_assert(cur_fmt != -1);     /* It should always be valid */
568
569         g_signal_handler_block  (fmt_cmb, column_menu_changed_handler_id);
570         gtk_combo_box_set_active(GTK_COMBO_BOX(fmt_cmb), cur_fmt);
571         g_signal_handler_unblock(fmt_cmb, column_menu_changed_handler_id);
572
573         g_signal_handler_block  (field_te, column_field_changed_handler_id);
574         g_signal_handler_block  (occurrence_te, column_occurrence_changed_handler_id);
575         if (cur_fmt == COL_CUSTOM) {
576             gtk_entry_set_text(GTK_ENTRY(field_te), cfmt->custom_field);
577             gtk_widget_set_sensitive(field_lb, TRUE);
578             gtk_widget_set_sensitive(field_te, TRUE);
579             g_snprintf(custom_occurrence_str, sizeof(custom_occurrence_str), "%d", cfmt->custom_occurrence);
580             gtk_entry_set_text(GTK_ENTRY(occurrence_te), custom_occurrence_str);
581             gtk_widget_set_sensitive(occurrence_lb, TRUE);
582             gtk_widget_set_sensitive(occurrence_te, TRUE);
583         } else {
584             gtk_editable_delete_text(GTK_EDITABLE(field_te), 0, -1);
585             gtk_widget_set_sensitive(field_lb, FALSE);
586             gtk_widget_set_sensitive(field_te, FALSE);
587             gtk_editable_delete_text(GTK_EDITABLE(occurrence_te), 0, -1);
588             gtk_widget_set_sensitive(occurrence_lb, FALSE);
589             gtk_widget_set_sensitive(occurrence_te, FALSE);
590         }
591         g_signal_handler_unblock(occurrence_te, column_occurrence_changed_handler_id);
592         g_signal_handler_unblock(field_te, column_field_changed_handler_id);
593
594         gtk_widget_set_sensitive(remove_bt, TRUE);
595         gtk_widget_set_sensitive(fmt_cmb, TRUE);
596     }
597     else
598     {
599         gtk_editable_delete_text(GTK_EDITABLE(field_te), 0, -1);
600         gtk_editable_delete_text(GTK_EDITABLE(occurrence_te), 0, -1);
601
602         gtk_widget_set_sensitive(remove_bt, FALSE);
603         gtk_widget_set_sensitive(field_te, FALSE);
604         gtk_widget_set_sensitive(occurrence_te, FALSE);
605         gtk_widget_set_sensitive(fmt_cmb, FALSE);
606     }
607 }
608
609
610 /*
611  * The user selected a new entry in the format combo-box;
612  *       Note: column_menu_changed_cb is expected to be called only
613  *         when the user changes the format combo-box.
614  * Action:
615  * If no selection active in the column list:
616  *    Hide 'field"; desensitize buttons.
617  *    XXX: Can this happen ?
618  * If a column list selection is active:
619  *    1. Update the display of "field" as req'd (depending upon
620  *       whether the active combo-box entry is the "custom"
621  *       format type).
622  *    2. Update the column_list and prefs.col_formats.
623  *       Set columns_changed = TRUE.
624  */
625
626 static void
627 column_menu_changed_cb(GtkWidget *w, gpointer data) {
628     GtkTreeView      *column_l = GTK_TREE_VIEW(data);
629     fmt_data         *cfmt;
630     gint              cur_fmt;
631     gint              cur_cb_fmt;
632     GList            *clp;
633     gchar            *fmt;
634     GtkTreeSelection *sel;
635     GtkTreeModel     *model;
636     GtkTreeIter       iter;
637
638     sel = gtk_tree_view_get_selection(column_l);
639     if (! (gtk_tree_selection_get_selected(sel, &model, &iter)))
640         return;  /* no column list selection [Can this happen ?]: ignore callback */
641
642     cur_cb_fmt = gtk_combo_box_get_active(GTK_COMBO_BOX(w));
643     gtk_tree_model_get(model, &iter, DATA_COLUMN, &clp, -1);
644     cfmt    = (fmt_data *) clp->data;
645     cur_fmt = get_column_format_from_str(cfmt->fmt);
646
647     g_assert(cur_cb_fmt != cur_fmt);
648
649     /* The User has selected a new format in the combo-box    */
650     /*  (IE: combo-box format != current selected row format) */
651     /* Update field widgets, list_store, column format array  */
652     /*  entry as appropriate.                                 */
653     g_signal_handler_block  (field_te, column_field_changed_handler_id);
654     g_signal_handler_block  (occurrence_te, column_occurrence_changed_handler_id);
655     if (cur_fmt == COL_CUSTOM) {
656         /* Changing from custom to non-custom   */
657         gtk_editable_delete_text(GTK_EDITABLE(field_te), 0, -1);
658         gtk_editable_delete_text(GTK_EDITABLE(occurrence_te), 0, -1);
659         fmt = g_strdup_printf("%s", col_format_desc(cur_cb_fmt));
660         gtk_widget_set_sensitive(field_lb, FALSE);
661         gtk_widget_set_sensitive(field_te, FALSE);
662         gtk_widget_set_sensitive(occurrence_lb, FALSE);
663         gtk_widget_set_sensitive(occurrence_te, FALSE);
664
665     } else if (cur_cb_fmt == COL_CUSTOM) {
666         /* Changing from non-custom to custom   */
667         if (cfmt->custom_field == NULL)
668             cfmt->custom_field = g_strdup("");
669         /* The following doesn't trigger a call to menu_field_changed_cb()    */
670         gtk_entry_set_text(GTK_ENTRY(field_te), cfmt->custom_field);
671         g_snprintf(custom_occurrence_str, sizeof(custom_occurrence_str), "%d", cfmt->custom_occurrence);
672         gtk_entry_set_text(GTK_ENTRY(occurrence_te), custom_occurrence_str);
673
674         if (cfmt->custom_occurrence) {
675             fmt = g_strdup_printf("%s (%s#%d)", col_format_desc(cur_cb_fmt), cfmt->custom_field, cfmt->custom_occurrence);
676         } else {
677             fmt = g_strdup_printf("%s (%s)", col_format_desc(cur_cb_fmt), cfmt->custom_field);
678         }
679         gtk_widget_set_sensitive(field_lb, TRUE);
680         gtk_widget_set_sensitive(field_te, TRUE);
681         gtk_widget_set_sensitive(occurrence_lb, TRUE);
682         gtk_widget_set_sensitive(occurrence_te, TRUE);
683
684     } else {
685         /* Changing from non-custom to non-custom */
686         fmt = g_strdup_printf("%s", col_format_desc(cur_cb_fmt));
687     }
688     g_signal_handler_unblock(occurrence_te, column_occurrence_changed_handler_id);
689     g_signal_handler_unblock(field_te, column_field_changed_handler_id);
690
691     gtk_list_store_set(GTK_LIST_STORE(model), &iter, FORMAT_COLUMN, fmt, -1);
692     g_free(fmt);
693     g_free(cfmt->fmt);
694     cfmt->fmt = g_strdup(col_format_to_string(cur_cb_fmt));
695     cfile.cinfo.columns_changed = TRUE;
696 }
697
698
699 /*
700  * The user changed the custom field entry box or
701  *  the field entry box has been updated because a new
702  *  column row with custom format has been selected.
703  * If the current field entry matches that of the current
704  *  column row, this is just an update because a new
705  *  column row has been selected. Do nothing.
706  * If the two are different, then update the column row & etc.
707  */
708 static void
709 column_field_changed_cb(GtkEditable *te, gpointer data) {
710     fmt_data         *cfmt;
711     gint              cur_fmt;
712     GList            *clp;
713     gchar            *field, *fmt;
714     GtkTreeView      *tree = (GtkTreeView *)data;
715     GtkTreeSelection *sel;
716     GtkTreeModel     *model;
717     GtkTreeIter       iter;
718
719     sel = gtk_tree_view_get_selection(tree);
720     if ( ! (gtk_tree_selection_get_selected(sel, &model, &iter))) {
721         return;
722     }
723
724     field = gtk_editable_get_chars(te, 0, -1);
725     gtk_tree_model_get(model, &iter, DATA_COLUMN, &clp, -1);
726     cfmt  = (fmt_data *) clp->data;
727     if (strcmp(cfmt->custom_field, field) == 0) {
728         return; /* no action req'd */
729     }
730
731     /* The user has entered a new value in the field entry box: make the req'd changes */ 
732     cur_fmt = get_column_format_from_str(cfmt->fmt);
733     if (cfmt->custom_occurrence) {
734         fmt = g_strdup_printf("%s (%s#%d)", col_format_desc(cur_fmt), field, cfmt->custom_occurrence);
735     } else {
736         fmt = g_strdup_printf("%s (%s)", col_format_desc(cur_fmt), field);
737     }
738
739     gtk_list_store_set(GTK_LIST_STORE(model), &iter, FORMAT_COLUMN, fmt, -1);
740     g_free(fmt);
741     g_free(cfmt->custom_field);
742     cfmt->custom_field = field;
743     cfile.cinfo.columns_changed = TRUE;
744 }
745
746
747 /*
748  * The user changed the custom field occurrence entry box or
749  *  the field occurrece entry box has been updated because a new
750  *  column row with custom format has been selected.
751  * If the current field entry matches that of the current
752  *  column row, this is just an update because a new
753  *  column row has been selected. Do nothing.
754  * If the two are different, then update the column row & etc.
755  */
756 static void
757 column_occurrence_changed_cb(GtkEditable *te, gpointer data) {
758     fmt_data         *cfmt;
759     gint              cur_fmt;
760     gint              occurrence;
761     GList            *clp;
762     gchar            *fmt;
763     GtkTreeView      *tree = (GtkTreeView *)data;
764     GtkTreeSelection *sel;
765     GtkTreeModel     *model;
766     GtkTreeIter       iter;
767
768     sel = gtk_tree_view_get_selection(tree);
769     if ( ! (gtk_tree_selection_get_selected(sel, &model, &iter))) {
770         return;
771     }
772
773     occurrence = (gint)strtol(gtk_editable_get_chars(te, 0, -1), NULL, 10);
774     gtk_tree_model_get(model, &iter, DATA_COLUMN, &clp, -1);
775     cfmt  = (fmt_data *) clp->data;
776     if (cfmt->custom_occurrence == occurrence) {
777         return; /* no action req'd */
778     }
779
780     /* The user has entered a new value in the field occurrence entry box: make the req'd changes */ 
781     cur_fmt = get_column_format_from_str(cfmt->fmt);
782     if (occurrence) {
783         fmt = g_strdup_printf("%s (%s#%d)", col_format_desc(cur_fmt), cfmt->custom_field, occurrence);
784     } else {
785         fmt = g_strdup_printf("%s (%s)", col_format_desc(cur_fmt), cfmt->custom_field);
786     }
787
788     gtk_list_store_set(GTK_LIST_STORE(model), &iter, FORMAT_COLUMN, fmt, -1);
789     g_free(fmt);
790     cfmt->custom_occurrence = occurrence;
791     cfile.cinfo.columns_changed = TRUE;
792 }
793
794
795 /*
796  * Callback for the "row-deleted" signal emitted when a list item is dragged.
797  * http://library.gnome.org/devel/gtk/stable/GtkTreeModel.html#GtkTreeModel-rows-reordered
798  * says that DND deletes, THEN inserts the row. 
799  *
800  * XXX: For the record: For Gtk+ 2.16.0 testing shows the actual sequence for drag-and-drop to be as follows:
801  *      1. Insert a new, empty row at the destination;
802  *      2. Emit a "row-inserted" signal on the model; invoke any row-inserted callbacks & etc;
803  *      3. Copy the source row data to the new (empty) destination row;
804  *      4. Delete the source row;
805  *      5. Emit a "row-deleted" signal; invoke any row-deleted callbacks & etc.
806  *
807  *  The code below (invoked as a consequence of a "row-deleted" signal) rebuilds 
808  *  prefs.col_list by iterating over the (re-ordered) tree model.
809  */
810 static void
811 column_dnd_row_deleted_cb(GtkTreeModel *model, GtkTreePath *path _U_, gpointer data _U_) {
812     GtkTreeIter   iter;
813     GList        *clp, *new_col_list = NULL;
814     gboolean      items_left;
815
816     /*
817      * XXX - This rebuilds prefs.col_list based on the current model. We
818      * might just want to do this when the prefs are applied
819      */
820     for (items_left = gtk_tree_model_get_iter_first (model, &iter);
821          items_left;
822          items_left = gtk_tree_model_iter_next (model, &iter)) {
823
824         gtk_tree_model_get(model, &iter, DATA_COLUMN, &clp, -1);
825         if (clp) {
826             prefs.col_list = g_list_remove_link(prefs.col_list, clp);
827             new_col_list = g_list_concat(new_col_list, clp);
828         }
829     }
830     if (prefs.col_list) {
831         g_warning("column_dnd_row_deleted_cb: prefs.col_list has %d leftover data",
832                   g_list_length(prefs.col_list));
833         g_list_free(prefs.col_list);
834     }
835
836     prefs.col_list = new_col_list;
837     cfile.cinfo.columns_changed = TRUE;
838 }
839
840
841 void
842 column_prefs_fetch(GtkWidget *w _U_) {
843 }
844
845
846 void
847 column_prefs_apply(GtkWidget *w _U_)
848 {
849     /* Redraw the packet list if the columns were changed */
850     if(cfile.cinfo.columns_changed) {
851 #ifdef NEW_PACKET_LIST
852         new_packet_list_recreate();
853 #else
854         packet_list_recreate();
855 #endif
856         cfile.cinfo.columns_changed = FALSE; /* Reset value */
857     }
858 }
859
860
861 void
862 column_prefs_destroy(GtkWidget *w _U_) {
863 }