When using a custom column, make it possible to select which occurrence to show if...
[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     g_object_set_data (G_OBJECT(occurrence_te), "occurrence", "");
330
331     /* XXX: column_occurrence_changed_cb will be called for every character entered in the entry box.      */
332     /*       Consider Changing logic so that the field is "accepted" only when a return is entered ?? */
333     column_occurrence_changed_handler_id = 
334         g_signal_connect(occurrence_te, "changed", G_CALLBACK(column_occurrence_changed_cb), column_l);
335
336     gtk_table_attach_defaults(GTK_TABLE(tb), occurrence_te, 3, 4, 1, 2);
337     gtk_widget_set_sensitive(occurrence_te, FALSE);
338     gtk_tooltips_set_tip (tooltips, occurrence_te,
339                           "Field occurence to use. "
340                           "0=all (default), 1=first, 2=second, ..., -1=last.", NULL);
341     gtk_widget_show(occurrence_te);
342
343     fmt_cmb = gtk_combo_box_new_text();
344
345     for (i = 0; i < NUM_COL_FMTS; i++)
346         gtk_combo_box_append_text(GTK_COMBO_BOX(fmt_cmb), col_format_desc(i));
347
348     column_menu_changed_handler_id = g_signal_connect(fmt_cmb, "changed", G_CALLBACK(column_menu_changed_cb), column_l);
349
350     gtk_widget_set_sensitive(fmt_cmb, FALSE);
351     gtk_box_pack_start(GTK_BOX(props_hb), fmt_cmb, FALSE, FALSE, 0);
352     gtk_widget_show(fmt_cmb);
353
354     /* select the first menu list row.             */
355     /*  Triggers call to column_list_select_cb().  */
356     gtk_tree_selection_select_iter(sel, &first_iter);
357
358     return(main_vb);
359 }
360
361 void
362 column_prefs_add_custom(gint fmt, const gchar *title, const gchar *custom_field, gint custom_occurrence)
363 {
364   GList *clp;
365   fmt_data *cfmt, *last_cfmt;
366
367   cfmt = (fmt_data *) g_malloc(sizeof(fmt_data));
368   /*
369    * Because a single underscore is interpreted as a signal that the next character
370    * is going to be marked as accelerator for this header (i.e. is going to be
371    * shown underlined), escape it be inserting a second consecutive underscore.
372    */
373   cfmt->title = g_strdup(title);
374   cfmt->fmt = g_strdup(col_format_to_string(fmt));
375   cfmt->custom_field = g_strdup(custom_field);
376   cfmt->custom_occurrence = custom_occurrence;
377   cfmt->resolved = TRUE;
378
379   if (custom_field) {
380     cfmt->visible = TRUE;
381     clp = g_list_last(prefs.col_list);
382     last_cfmt = (fmt_data *) clp->data;
383     if (strcmp(last_cfmt->fmt, "%i") == 0) {
384       /* Last column is COL_INFO, add custom column before this */
385       prefs.col_list = g_list_insert(prefs.col_list, cfmt, g_list_length(prefs.col_list)-1);
386     } else {
387       prefs.col_list = g_list_append(prefs.col_list, cfmt);
388     }
389   } else {
390     cfmt->visible = FALSE;  /* Will be set to TRUE in visible_toggled() when added to list */
391     prefs.col_list = g_list_append(prefs.col_list, cfmt);
392   }
393 }
394
395 void
396 column_prefs_rename(gint col, const gchar *title)
397 {
398   GList    *clp = g_list_nth(prefs.col_list, col);
399   fmt_data *cfmt = (fmt_data *) clp->data;
400
401   g_free (cfmt->title);
402   cfmt->title = g_strdup(title);
403 }
404
405 void
406 column_prefs_remove(gint col)
407 {
408   GList    *clp = g_list_nth(prefs.col_list, col);
409   fmt_data *cfmt = (fmt_data *) clp->data;
410
411   g_free(cfmt->title);
412   g_free(cfmt->fmt);
413   g_free(cfmt->custom_field);
414   g_free(cfmt);
415   prefs.col_list = g_list_remove_link(prefs.col_list, clp);
416 }
417
418 /* To do: add input checking to each of these callbacks */
419
420 static void
421 column_list_new_cb(GtkWidget *w _U_, gpointer data) {
422     gint               cur_fmt;
423     const gchar       *title = "New Column";
424     GtkTreeView       *column_l = GTK_TREE_VIEW(data);
425     GtkTreeModel      *model;
426     GtkTreeIter        iter;
427     GtkTreePath       *path;
428     GtkTreeViewColumn *title_column;
429
430     cur_fmt = COL_NUMBER;    /*  Set the default new column type */
431     column_prefs_add_custom (cur_fmt, title, NULL, 0);
432
433     model = gtk_tree_view_get_model(column_l);
434 #if GTK_CHECK_VERSION(2,6,0)
435     gtk_list_store_insert_with_values(GTK_LIST_STORE(model), &iter, G_MAXINT,
436 #else
437     gtk_list_store_append(GTK_LIST_STORE(model), &iter);
438     gtk_list_store_set(GTK_LIST_STORE(model), &iter, 
439 #endif
440 #ifdef NEW_PACKET_LIST
441                        VISIBLE_COLUMN, TRUE,
442 #endif
443                        TITLE_COLUMN, title,
444                        FORMAT_COLUMN, col_format_desc(cur_fmt),
445                        DATA_COLUMN, g_list_last(prefs.col_list),
446                        -1);
447
448     /* Triggers call to column_list_select_cb()   */
449     gtk_tree_selection_select_iter(gtk_tree_view_get_selection(column_l), &iter);
450
451     /* Set the cursor to the 'Title' column of the newly added row and enable editing */
452     /* XXX: If displaying the new title ["New column"] widens the title column of the */
453     /*      treeview, then the set_cursor below doesn't properly generate an entry    */
454     /*      box around the title text. The width of the box appears to be the column  */
455     /*      width before the treeview title column was widened.  Seems like a bug...  */
456     /*      I haven't found a work-around.                                            */
457     path = gtk_tree_model_get_path(model, &iter);
458     title_column = gtk_tree_view_get_column(column_l, 0);
459     gtk_tree_view_set_cursor(column_l, path, title_column, TRUE);
460     gtk_tree_path_free(path);
461
462     cfile.cinfo.columns_changed = TRUE;
463 }
464
465
466 static void
467 column_list_delete_cb(GtkWidget *w _U_, gpointer data) {
468     GtkTreeView      *column_l = GTK_TREE_VIEW(data);
469     GList            *clp;
470     fmt_data         *cfmt;
471     GtkTreeSelection *sel;
472     GtkTreeModel     *model;
473     GtkTreeIter       iter;
474     GtkTreeIter       new_iter;
475
476     sel = gtk_tree_view_get_selection(column_l);
477     if (gtk_tree_selection_get_selected(sel, &model, &iter))
478     {
479         gtk_tree_model_get(model, &iter, DATA_COLUMN, &clp, -1);
480
481         cfmt = (fmt_data *) clp->data;
482         g_free(cfmt->title);
483         g_free(cfmt->fmt);
484         g_free(cfmt->custom_field);
485         g_free(cfmt);
486         prefs.col_list = g_list_remove_link(prefs.col_list, clp);
487
488         /* Change the row selection to the next row (if available) or    */
489         /*  the previous row (if available). If there's only one row     */
490         /*  in the store (no previous and no next), then the selection   */
491         /*  will not be changed.                                         */
492
493         /* Note that gtk_tree_selection_select_iter() will trigger a     */
494         /* call to column_list_select_cb().                              */
495
496         new_iter = iter;
497         if ( gtk_tree_model_iter_next(model, &new_iter)) {
498             gtk_tree_selection_select_iter(sel, &new_iter);
499         } else { /* "gtk_tree_model_iter_prev" */
500             GtkTreePath *path = gtk_tree_model_get_path(model, &iter);
501             if (gtk_tree_path_prev(path)) {
502                 gtk_tree_model_get_iter(model, &new_iter, path);
503                 gtk_tree_selection_select_iter(sel, &new_iter);
504             }
505             gtk_tree_path_free(path);
506         }
507
508         /* Remove the row from the list store.                           */
509         /*  We prevent triggering call to column_row_deleted_cb() since  */
510         /*  the entry has already been removed from prefs.col_list and   */
511         /*  since rebuilding the list is not needed because the order    */
512         /*  of the list hasn't changed.                                  */
513
514         g_signal_handler_block(model, column_row_deleted_handler_id);
515
516         /* list_store_remove below will trigger a call to                */
517         /* column_list_select_cb() only when deleting the last entry in  */
518         /* the column list.                                              */
519         /* (This is because the selection in this case changes to        */
520         /*  "nothing selected" when the last row is removed.             */
521
522         gtk_list_store_remove(GTK_LIST_STORE(model), &iter);
523
524         g_signal_handler_unblock  (model, column_row_deleted_handler_id);
525
526         cfile.cinfo.columns_changed = TRUE;
527     }
528 }
529
530
531 static gboolean
532 column_title_changed_cb(GtkCellRendererText *cell _U_, const gchar *str_path, const gchar *new_title, gpointer data) {
533     fmt_data     *cfmt;
534     GList        *clp;
535     GtkTreeModel *model = (GtkTreeModel *)data;
536     GtkTreePath  *path = gtk_tree_path_new_from_string (str_path);
537     GtkTreeIter   iter;
538
539     gtk_tree_model_get_iter(model, &iter, path); 
540   
541     gtk_list_store_set(GTK_LIST_STORE(model), &iter, TITLE_COLUMN, new_title, -1);
542
543     gtk_tree_model_get(model, &iter, DATA_COLUMN, &clp, -1);
544     if (clp) {    
545         cfmt  = (fmt_data *) clp->data;
546         g_free(cfmt->title);
547         cfmt->title = g_strdup(new_title);
548     }
549
550     gtk_tree_path_free (path);
551     cfile.cinfo.columns_changed = TRUE; 
552     return TRUE;  
553 }
554
555 /*
556  * column list row selection changed.
557  *  Set the "Properties" widgets to match the currently selected column row item.
558  */
559
560 static void
561 column_list_select_cb(GtkTreeSelection *sel, gpointer data _U_)
562 {
563     fmt_data     *cfmt;
564     gint          cur_fmt;
565     GList        *clp;
566     GtkTreeModel *model;
567     GtkTreeIter   iter;
568
569     /* if something was selected */
570     if (gtk_tree_selection_get_selected(sel, &model, &iter))
571     {
572         gtk_tree_model_get(model, &iter, DATA_COLUMN, &clp, -1);
573         g_assert(clp != NULL);
574         cfmt    = (fmt_data *) clp->data;
575         cur_fmt = get_column_format_from_str(cfmt->fmt);
576         g_assert(cur_fmt != -1);     /* It should always be valid */
577
578         g_signal_handler_block  (fmt_cmb, column_menu_changed_handler_id);
579         gtk_combo_box_set_active(GTK_COMBO_BOX(fmt_cmb), cur_fmt);
580         g_signal_handler_unblock(fmt_cmb, column_menu_changed_handler_id);
581
582         g_signal_handler_block  (field_te, column_field_changed_handler_id);
583         g_signal_handler_block  (occurrence_te, column_occurrence_changed_handler_id);
584         if (cur_fmt == COL_CUSTOM) {
585             gtk_entry_set_text(GTK_ENTRY(field_te), cfmt->custom_field);
586             gtk_widget_set_sensitive(field_lb, TRUE);
587             gtk_widget_set_sensitive(field_te, TRUE);
588             g_snprintf(custom_occurrence_str, sizeof(custom_occurrence_str), "%d", cfmt->custom_occurrence);
589             gtk_entry_set_text(GTK_ENTRY(occurrence_te), custom_occurrence_str);
590             gtk_widget_set_sensitive(occurrence_lb, TRUE);
591             gtk_widget_set_sensitive(occurrence_te, TRUE);
592         } else {
593             gtk_editable_delete_text(GTK_EDITABLE(field_te), 0, -1);
594             gtk_widget_set_sensitive(field_lb, FALSE);
595             gtk_widget_set_sensitive(field_te, FALSE);
596             gtk_editable_delete_text(GTK_EDITABLE(occurrence_te), 0, -1);
597             gtk_widget_set_sensitive(occurrence_lb, FALSE);
598             gtk_widget_set_sensitive(occurrence_te, FALSE);
599         }
600         g_signal_handler_unblock(occurrence_te, column_occurrence_changed_handler_id);
601         g_signal_handler_unblock(field_te, column_field_changed_handler_id);
602
603         gtk_widget_set_sensitive(remove_bt, TRUE);
604         gtk_widget_set_sensitive(fmt_cmb, TRUE);
605     }
606     else
607     {
608         gtk_editable_delete_text(GTK_EDITABLE(field_te), 0, -1);
609         gtk_editable_delete_text(GTK_EDITABLE(occurrence_te), 0, -1);
610
611         gtk_widget_set_sensitive(remove_bt, FALSE);
612         gtk_widget_set_sensitive(field_te, FALSE);
613         gtk_widget_set_sensitive(occurrence_te, FALSE);
614         gtk_widget_set_sensitive(fmt_cmb, FALSE);
615     }
616 }
617
618
619 /*
620  * The user selected a new entry in the format combo-box;
621  *       Note: column_menu_changed_cb is expected to be called only
622  *         when the user changes the format combo-box.
623  * Action:
624  * If no selection active in the column list:
625  *    Hide 'field"; desensitize buttons.
626  *    XXX: Can this happen ?
627  * If a column list selection is active:
628  *    1. Update the display of "field" as req'd (depending upon
629  *       whether the active combo-box entry is the "custom"
630  *       format type).
631  *    2. Update the column_list and prefs.col_formats.
632  *       Set columns_changed = TRUE.
633  */
634
635 static void
636 column_menu_changed_cb(GtkWidget *w, gpointer data) {
637     GtkTreeView      *column_l = GTK_TREE_VIEW(data);
638     fmt_data         *cfmt;
639     gint              cur_fmt;
640     gint              cur_cb_fmt;
641     GList            *clp;
642     gchar            *fmt;
643     GtkTreeSelection *sel;
644     GtkTreeModel     *model;
645     GtkTreeIter       iter;
646
647     sel = gtk_tree_view_get_selection(column_l);
648     if (! (gtk_tree_selection_get_selected(sel, &model, &iter)))
649         return;  /* no column list selection [Can this happen ?]: ignore callback */
650
651     cur_cb_fmt = gtk_combo_box_get_active(GTK_COMBO_BOX(w));
652     gtk_tree_model_get(model, &iter, DATA_COLUMN, &clp, -1);
653     cfmt    = (fmt_data *) clp->data;
654     cur_fmt = get_column_format_from_str(cfmt->fmt);
655
656     g_assert(cur_cb_fmt != cur_fmt);
657
658     /* The User has selected a new format in the combo-box    */
659     /*  (IE: combo-box format != current selected row format) */
660     /* Update field widgets, list_store, column format array  */
661     /*  entry as appropriate.                                 */
662     g_signal_handler_block  (field_te, column_field_changed_handler_id);
663     g_signal_handler_block  (occurrence_te, column_occurrence_changed_handler_id);
664     if (cur_fmt == COL_CUSTOM) {
665         /* Changing from custom to non-custom   */
666         gtk_editable_delete_text(GTK_EDITABLE(field_te), 0, -1);
667         gtk_editable_delete_text(GTK_EDITABLE(occurrence_te), 0, -1);
668         fmt = g_strdup_printf("%s", col_format_desc(cur_cb_fmt));
669         gtk_widget_set_sensitive(field_lb, FALSE);
670         gtk_widget_set_sensitive(field_te, FALSE);
671         gtk_widget_set_sensitive(occurrence_lb, FALSE);
672         gtk_widget_set_sensitive(occurrence_te, FALSE);
673
674     } else if (cur_cb_fmt == COL_CUSTOM) {
675         /* Changing from non-custom to custom   */
676         if (cfmt->custom_field == NULL)
677             cfmt->custom_field = g_strdup("");
678         /* The following doesn't trigger a call to menu_field_changed_cb()    */
679         gtk_entry_set_text(GTK_ENTRY(field_te), cfmt->custom_field);
680         g_snprintf(custom_occurrence_str, sizeof(custom_occurrence_str), "%d", cfmt->custom_occurrence);
681         gtk_entry_set_text(GTK_ENTRY(occurrence_te), custom_occurrence_str);
682
683         if (cfmt->custom_occurrence) {
684             fmt = g_strdup_printf("%s (%s#%d)", col_format_desc(cur_cb_fmt), cfmt->custom_field, cfmt->custom_occurrence);
685         } else {
686             fmt = g_strdup_printf("%s (%s)", col_format_desc(cur_cb_fmt), cfmt->custom_field);
687         }
688         gtk_widget_set_sensitive(field_lb, TRUE);
689         gtk_widget_set_sensitive(field_te, TRUE);
690         gtk_widget_set_sensitive(occurrence_lb, TRUE);
691         gtk_widget_set_sensitive(occurrence_te, TRUE);
692
693     } else {
694         /* Changing from non-custom to non-custom */
695         fmt = g_strdup_printf("%s", col_format_desc(cur_cb_fmt));
696     }
697     g_signal_handler_unblock(occurrence_te, column_occurrence_changed_handler_id);
698     g_signal_handler_unblock(field_te, column_field_changed_handler_id);
699
700     gtk_list_store_set(GTK_LIST_STORE(model), &iter, FORMAT_COLUMN, fmt, -1);
701     g_free(fmt);
702     g_free(cfmt->fmt);
703     cfmt->fmt = g_strdup(col_format_to_string(cur_cb_fmt));
704     cfile.cinfo.columns_changed = TRUE;
705 }
706
707
708 /*
709  * The user changed the custom field entry box or
710  *  the field entry box has been updated because a new
711  *  column row with custom format has been selected.
712  * If the current field entry matches that of the current
713  *  column row, this is just an update because a new
714  *  column row has been selected. Do nothing.
715  * If the two are different, then update the column row & etc.
716  */
717 static void
718 column_field_changed_cb(GtkEditable *te, gpointer data) {
719     fmt_data         *cfmt;
720     gint              cur_fmt;
721     GList            *clp;
722     gchar            *field, *fmt;
723     GtkTreeView      *tree = (GtkTreeView *)data;
724     GtkTreeSelection *sel;
725     GtkTreeModel     *model;
726     GtkTreeIter       iter;
727
728     sel = gtk_tree_view_get_selection(tree);
729     if ( ! (gtk_tree_selection_get_selected(sel, &model, &iter))) {
730         return;
731     }
732
733     field = gtk_editable_get_chars(te, 0, -1);
734     gtk_tree_model_get(model, &iter, DATA_COLUMN, &clp, -1);
735     cfmt  = (fmt_data *) clp->data;
736     if (strcmp(cfmt->custom_field, field) == 0) {
737         return; /* no action req'd */
738     }
739
740     /* The user has entered a new value in the field entry box: make the req'd changes */ 
741     cur_fmt = get_column_format_from_str(cfmt->fmt);
742     if (cfmt->custom_occurrence) {
743         fmt = g_strdup_printf("%s (%s#%d)", col_format_desc(cur_fmt), field, cfmt->custom_occurrence);
744     } else {
745         fmt = g_strdup_printf("%s (%s)", col_format_desc(cur_fmt), field);
746     }
747
748     gtk_list_store_set(GTK_LIST_STORE(model), &iter, FORMAT_COLUMN, fmt, -1);
749     g_free(fmt);
750     g_free(cfmt->custom_field);
751     cfmt->custom_field = field;
752     cfile.cinfo.columns_changed = TRUE;
753 }
754
755
756 /*
757  * The user changed the custom field occurrence entry box or
758  *  the field occurrece entry box has been updated because a new
759  *  column row with custom format has been selected.
760  * If the current field entry matches that of the current
761  *  column row, this is just an update because a new
762  *  column row has been selected. Do nothing.
763  * If the two are different, then update the column row & etc.
764  */
765 static void
766 column_occurrence_changed_cb(GtkEditable *te, gpointer data) {
767     fmt_data         *cfmt;
768     gint              cur_fmt;
769     gint              occurrence;
770     GList            *clp;
771     gchar            *fmt;
772     GtkTreeView      *tree = (GtkTreeView *)data;
773     GtkTreeSelection *sel;
774     GtkTreeModel     *model;
775     GtkTreeIter       iter;
776
777     sel = gtk_tree_view_get_selection(tree);
778     if ( ! (gtk_tree_selection_get_selected(sel, &model, &iter))) {
779         return;
780     }
781
782     occurrence = (gint)strtol(gtk_editable_get_chars(te, 0, -1), NULL, 10);
783     gtk_tree_model_get(model, &iter, DATA_COLUMN, &clp, -1);
784     cfmt  = (fmt_data *) clp->data;
785     if (cfmt->custom_occurrence == occurrence) {
786         return; /* no action req'd */
787     }
788
789     /* The user has entered a new value in the field occurrence entry box: make the req'd changes */ 
790     cur_fmt = get_column_format_from_str(cfmt->fmt);
791     if (occurrence) {
792         fmt = g_strdup_printf("%s (%s#%d)", col_format_desc(cur_fmt), cfmt->custom_field, occurrence);
793     } else {
794         fmt = g_strdup_printf("%s (%s)", col_format_desc(cur_fmt), cfmt->custom_field);
795     }
796
797     gtk_list_store_set(GTK_LIST_STORE(model), &iter, FORMAT_COLUMN, fmt, -1);
798     g_free(fmt);
799     cfmt->custom_occurrence = occurrence;
800     cfile.cinfo.columns_changed = TRUE;
801 }
802
803
804 /*
805  * Callback for the "row-deleted" signal emitted when a list item is dragged.
806  * http://library.gnome.org/devel/gtk/stable/GtkTreeModel.html#GtkTreeModel-rows-reordered
807  * says that DND deletes, THEN inserts the row. 
808  *
809  * XXX: For the record: For Gtk+ 2.16.0 testing shows the actual sequence for drag-and-drop to be as follows:
810  *      1. Insert a new, empty row at the destination;
811  *      2. Emit a "row-inserted" signal on the model; invoke any row-inserted callbacks & etc;
812  *      3. Copy the source row data to the new (empty) destination row;
813  *      4. Delete the source row;
814  *      5. Emit a "row-deleted" signal; invoke any row-deleted callbacks & etc.
815  *
816  *  The code below (invoked as a consequence of a "row-deleted" signal) rebuilds 
817  *  prefs.col_list by iterating over the (re-ordered) tree model.
818  */
819 static void
820 column_dnd_row_deleted_cb(GtkTreeModel *model, GtkTreePath *path _U_, gpointer data _U_) {
821     GtkTreeIter   iter;
822     GList        *clp, *new_col_list = NULL;
823     gboolean      items_left;
824
825     /*
826      * XXX - This rebuilds prefs.col_list based on the current model. We
827      * might just want to do this when the prefs are applied
828      */
829     for (items_left = gtk_tree_model_get_iter_first (model, &iter);
830          items_left;
831          items_left = gtk_tree_model_iter_next (model, &iter)) {
832
833         gtk_tree_model_get(model, &iter, DATA_COLUMN, &clp, -1);
834         if (clp) {
835             prefs.col_list = g_list_remove_link(prefs.col_list, clp);
836             new_col_list = g_list_concat(new_col_list, clp);
837         }
838     }
839     if (prefs.col_list) {
840         g_warning("column_dnd_row_deleted_cb: prefs.col_list has %d leftover data",
841                   g_list_length(prefs.col_list));
842         g_list_free(prefs.col_list);
843     }
844
845     prefs.col_list = new_col_list;
846     cfile.cinfo.columns_changed = TRUE;
847 }
848
849
850 void
851 column_prefs_fetch(GtkWidget *w _U_) {
852 }
853
854
855 void
856 column_prefs_apply(GtkWidget *w _U_)
857 {
858     /* Redraw the packet list if the columns were changed */
859     if(cfile.cinfo.columns_changed) {
860 #ifdef NEW_PACKET_LIST
861         new_packet_list_recreate();
862 #else
863         packet_list_recreate();
864 #endif
865         cfile.cinfo.columns_changed = FALSE; /* Reset value */
866     }
867 }
868
869
870 void
871 column_prefs_destroy(GtkWidget *w _U_) {
872 }