Detect clang and llvm-gcc.
[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, *fmt_cmb;
53 static gulong column_menu_changed_handler_id;
54 static gulong column_field_changed_handler_id;
55 static gulong column_row_deleted_handler_id;
56
57 static void column_list_new_cb(GtkWidget *, gpointer);
58 static void column_list_delete_cb(GtkWidget *, gpointer);
59 static void column_list_select_cb(GtkTreeSelection *, gpointer);
60 static void column_menu_changed_cb(GtkWidget *, gpointer);
61 static void column_field_changed_cb(GtkEditable *, gpointer);
62 static void column_dnd_row_deleted_cb(GtkTreeModel *, GtkTreePath *, gpointer);
63 static gboolean column_title_changed_cb(GtkCellRendererText *, const gchar *, const gchar *, gpointer);
64
65 /*
66  * Create and display the column selection widgets.
67  * Called as part of the creation of the Preferences notebook ( Edit ! Preferences )
68  */
69 GtkWidget *
70 column_prefs_show(GtkWidget *prefs_window) {
71     GtkWidget         *main_vb, *bottom_hb, *column_l, *add_bt, *tb, *lb;
72     GtkWidget         *list_vb, *list_lb, *list_sc;
73     GtkWidget         *add_remove_vb;
74     GtkWidget         *props_fr, *props_hb;
75     GList             *clp;
76     fmt_data          *cfmt;
77     gint               i;
78     gchar             *fmt;
79     gint               cur_fmt;
80     const gchar       *column_titles[] = {"Title", "Field type"};
81     GtkListStore      *store;
82     GtkCellRenderer   *renderer;
83     GtkTreeViewColumn *column;
84     GtkTreeSelection  *sel;
85     GtkTreeIter        iter;
86     GtkTreeIter        first_iter;
87     gint               first_row = TRUE;
88
89     GtkTooltips   *tooltips = gtk_tooltips_new();
90
91     /* Container for each row of widgets */
92     main_vb = gtk_vbox_new(FALSE, 5);
93     gtk_container_set_border_width(GTK_CONTAINER(main_vb), 5);
94     gtk_widget_show(main_vb);
95
96     list_vb = gtk_vbox_new (FALSE, 0);
97     gtk_container_set_border_width  (GTK_CONTAINER (list_vb), 5);
98     gtk_widget_show (list_vb);
99     gtk_box_pack_start (GTK_BOX (main_vb), list_vb, TRUE, TRUE, 0);
100
101     list_lb = gtk_label_new (("[The first list entry will be displayed as the leftmost column - Drag and drop entries to change column order]"));
102     gtk_widget_show (list_lb);
103     gtk_box_pack_start (GTK_BOX (list_vb), list_lb, FALSE, FALSE, 0);
104
105     list_sc = scrolled_window_new(NULL, NULL);
106     gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(list_sc), GTK_SHADOW_IN);
107     gtk_container_add(GTK_CONTAINER(list_vb), list_sc);
108     gtk_widget_show(list_sc);
109
110     store = gtk_list_store_new(3, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_POINTER);
111     column_row_deleted_handler_id = 
112         g_signal_connect(GTK_TREE_MODEL(store), "row-deleted", G_CALLBACK(column_dnd_row_deleted_cb), NULL);
113
114     column_l = tree_view_new(GTK_TREE_MODEL(store));
115     gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(column_l), TRUE);
116     gtk_tree_view_set_headers_clickable(GTK_TREE_VIEW(column_l), FALSE);
117     gtk_tree_view_set_reorderable(GTK_TREE_VIEW(column_l), TRUE);
118     gtk_tooltips_set_tip (tooltips, column_l, 
119         "Click on a title to change its name.\nDrag an item to change its order.", NULL);
120
121     renderer = gtk_cell_renderer_text_new();
122     g_object_set(G_OBJECT(renderer), "editable", TRUE, NULL);
123     g_signal_connect (renderer, "edited", G_CALLBACK(column_title_changed_cb), GTK_TREE_MODEL(store));
124     column = gtk_tree_view_column_new_with_attributes(column_titles[0], renderer, "text", 0, NULL);
125     gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_AUTOSIZE);
126     gtk_tree_view_append_column(GTK_TREE_VIEW(column_l), column);
127
128     renderer = gtk_cell_renderer_text_new();
129     column = gtk_tree_view_column_new_with_attributes(column_titles[1], renderer, "text", 1, NULL);
130     gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_AUTOSIZE);
131     gtk_tree_view_append_column(GTK_TREE_VIEW(column_l), column);
132
133     /* XXX - make this match the packet list prefs? */
134     sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(column_l));
135     gtk_tree_selection_set_mode(sel, GTK_SELECTION_SINGLE);
136     g_signal_connect(sel, "changed", G_CALLBACK(column_list_select_cb), NULL);
137
138     gtk_container_add(GTK_CONTAINER(list_sc), column_l);
139     gtk_widget_show(column_l);
140
141     clp = g_list_first(prefs.col_list);
142     while (clp) {
143         cfmt    = (fmt_data *) clp->data;
144         cur_fmt = get_column_format_from_str(cfmt->fmt);
145         if (cur_fmt == COL_CUSTOM) {
146             fmt = g_strdup_printf("%s (%s)", col_format_desc(cur_fmt), cfmt->custom_field);
147         } else {
148             if (cfmt->custom_field) {
149                 /* Delete custom_field from previous changes */
150                 g_free (cfmt->custom_field);
151                 cfmt->custom_field = NULL;
152             }
153             fmt = g_strdup_printf("%s", col_format_desc(cur_fmt));
154         }
155         gtk_list_store_append(store, &iter);
156         gtk_list_store_set(store, &iter, 0, cfmt->title, 1, fmt, 2, clp, -1);
157         if (first_row) {
158             first_iter = iter;
159             first_row = FALSE;
160         }
161         clp = clp->next;
162         g_free (fmt);
163     }
164     g_object_unref(G_OBJECT(store));
165
166     /* Bottom row: Add/remove buttons and properties */
167     bottom_hb = gtk_hbox_new(FALSE, 5);
168     gtk_box_pack_start (GTK_BOX (main_vb), bottom_hb, FALSE, TRUE, 0);
169     gtk_widget_show(bottom_hb);
170
171     /* Add / remove buttons */
172     add_remove_vb = gtk_vbox_new (TRUE, 0);
173     gtk_container_set_border_width (GTK_CONTAINER (add_remove_vb), 5);
174     gtk_box_pack_start (GTK_BOX (bottom_hb), add_remove_vb, FALSE, FALSE, 0);
175     gtk_widget_show(add_remove_vb);
176
177     add_bt = gtk_button_new_from_stock(GTK_STOCK_ADD);
178     g_signal_connect(add_bt, "clicked", G_CALLBACK(column_list_new_cb), column_l);
179     gtk_box_pack_start (GTK_BOX (add_remove_vb), add_bt, FALSE, FALSE, 0);
180     gtk_tooltips_set_tip (tooltips, add_bt,
181                           "Add a new column at the end of the list.", NULL);
182     gtk_widget_show(add_bt);
183
184     remove_bt = gtk_button_new_from_stock(GTK_STOCK_REMOVE);
185     gtk_widget_set_sensitive(remove_bt, FALSE);
186     g_signal_connect(remove_bt, "clicked", G_CALLBACK(column_list_delete_cb), column_l);
187     gtk_box_pack_start (GTK_BOX (add_remove_vb), remove_bt, FALSE, FALSE, 0);
188     gtk_tooltips_set_tip (tooltips, remove_bt,
189                           "Remove the selected column.", NULL);
190     gtk_widget_show(remove_bt);
191   
192     /* properties frame */
193     props_fr = gtk_frame_new("Properties");
194     gtk_box_pack_start (GTK_BOX (bottom_hb), props_fr, TRUE, TRUE, 0);
195     gtk_widget_show(props_fr);
196
197     /* Column name entry and format selection */
198     tb = gtk_table_new(2, 2, FALSE);
199     gtk_container_set_border_width(GTK_CONTAINER(tb), 5);
200     gtk_container_add(GTK_CONTAINER(props_fr), tb);
201     gtk_table_set_row_spacings(GTK_TABLE(tb), 10);
202     gtk_table_set_col_spacings(GTK_TABLE(tb), 15);
203     gtk_widget_show(tb);
204
205     lb = gtk_label_new("Field type:");
206     gtk_misc_set_alignment(GTK_MISC(lb), 1.0f, 0.5f);
207     gtk_table_attach_defaults(GTK_TABLE(tb), lb, 0, 1, 0, 1);
208     gtk_tooltips_set_tip (tooltips, lb,
209                           "Select which packet information to present in the column.", NULL);
210     gtk_widget_show(lb);
211
212     props_hb = gtk_hbox_new(FALSE, 5);
213     gtk_table_attach(GTK_TABLE(tb), props_hb, 1, 2, 0, 1, GTK_FILL, GTK_SHRINK, 0, 0);
214     gtk_tooltips_set_tip (tooltips, props_hb,
215                           "Select which packet information to present in the column.", NULL);
216     gtk_widget_show(props_hb);
217
218     field_lb = gtk_label_new("Field name:");
219     gtk_misc_set_alignment(GTK_MISC(field_lb), 1.0f, 0.5f);
220     gtk_table_attach_defaults(GTK_TABLE(tb), field_lb, 0, 1, 1, 2);
221     gtk_widget_set_sensitive(field_lb, FALSE);
222     gtk_tooltips_set_tip (tooltips, field_lb,
223                           "Field name used when field type is \"Custom\". "
224                           "This string has the same syntax as a display filter string.", NULL);
225     gtk_widget_show(field_lb);
226
227     field_te = gtk_entry_new();
228     g_object_set_data (G_OBJECT(field_te), E_FILT_FIELD_NAME_ONLY_KEY, "");
229     g_signal_connect(field_te, "changed", G_CALLBACK(filter_te_syntax_check_cb), NULL);
230
231     /* XXX: column_field_changed_cb will be called for every character entered in the entry box.      */
232     /*       Consider Changing logic so that the field is "accepted" only when a return is entered ?? */
233     /*       Also: entry shouldn't be accepted if it's not a valid filter ?                           */
234     column_field_changed_handler_id = 
235         g_signal_connect(field_te, "changed", G_CALLBACK(column_field_changed_cb), column_l);
236
237     g_object_set_data(G_OBJECT(main_vb), E_FILT_AUTOCOMP_PTR_KEY, NULL);
238     g_signal_connect(field_te, "key-press-event", G_CALLBACK (filter_string_te_key_pressed_cb), NULL);
239     g_signal_connect(prefs_window, "key-press-event", G_CALLBACK (filter_parent_dlg_key_pressed_cb), NULL);
240     colorize_filter_te_as_empty(field_te);
241     gtk_table_attach_defaults(GTK_TABLE(tb), field_te, 1, 2, 1, 2);
242     gtk_widget_set_sensitive(field_te, FALSE);
243     gtk_tooltips_set_tip (tooltips, field_te,
244                           "Field name used when field type is \"Custom\". "
245                           "This string has the same syntax as a display filter string.", NULL);
246     gtk_widget_show(field_te);
247
248     fmt_cmb = gtk_combo_box_new_text();
249
250     for (i = 0; i < NUM_COL_FMTS; i++)
251         gtk_combo_box_append_text(GTK_COMBO_BOX(fmt_cmb), col_format_desc(i));
252
253     column_menu_changed_handler_id = g_signal_connect(fmt_cmb, "changed", G_CALLBACK(column_menu_changed_cb), column_l);
254
255     gtk_widget_set_sensitive(fmt_cmb, FALSE);
256     gtk_box_pack_start(GTK_BOX(props_hb), fmt_cmb, FALSE, FALSE, 0);
257     gtk_widget_show(fmt_cmb);
258
259     /* select the first menu list row.             */
260     /*  Triggers call to column_list_select_cb().  */
261     gtk_tree_selection_select_iter(sel, &first_iter);
262
263     return(main_vb);
264 }
265
266 void
267 column_prefs_add_custom(gint fmt, const gchar *title, const gchar *custom_field)
268 {
269   GList *clp;
270   fmt_data *cfmt, *last_cfmt;
271
272   cfmt = (fmt_data *) g_malloc(sizeof(fmt_data));
273   /*
274    * Because a single underscore is interpreted as a signal that the next character
275    * is going to be marked as accelerator for this header (i.e. is going to be
276    * shown underlined), escape it be inserting a second consecutive underscore.
277    */
278   cfmt->title = g_strdup(title);
279   cfmt->fmt = g_strdup(col_format_to_string(fmt));
280   cfmt->custom_field = g_strdup(custom_field);
281
282   if (custom_field) {
283     clp = g_list_last(prefs.col_list);
284     last_cfmt = (fmt_data *) clp->data;
285     if (strcmp(last_cfmt->fmt, "%i") == 0) {
286       /* Last column is COL_INFO, add custom column before this */
287       prefs.col_list = g_list_insert(prefs.col_list, cfmt, g_list_length(prefs.col_list)-1);
288     } else {
289       prefs.col_list = g_list_append(prefs.col_list, cfmt);
290     }
291   } else {
292     prefs.col_list = g_list_append(prefs.col_list, cfmt);
293   }
294 }
295
296 void
297 column_prefs_rename(gint col, const gchar *title)
298 {
299   GList    *clp = g_list_nth(prefs.col_list, col);
300   fmt_data *cfmt = (fmt_data *) clp->data;
301
302   g_free (cfmt->title);
303   cfmt->title = g_strdup(title);
304 }
305
306 void
307 column_prefs_remove(gint col)
308 {
309   GList    *clp = g_list_nth(prefs.col_list, col);
310   fmt_data *cfmt = (fmt_data *) clp->data;
311
312   g_free(cfmt->title);
313   g_free(cfmt->fmt);
314   g_free(cfmt->custom_field);
315   g_free(cfmt);
316   prefs.col_list = g_list_remove_link(prefs.col_list, clp);
317 }
318
319 /* To do: add input checking to each of these callbacks */
320
321 static void
322 column_list_new_cb(GtkWidget *w _U_, gpointer data) {
323     gint               cur_fmt;
324     const gchar       *title = "New Column";
325     GtkTreeView       *column_l = GTK_TREE_VIEW(data);
326     GtkTreeModel      *model;
327     GtkTreeIter        iter;
328     GtkTreePath       *path;
329     GtkTreeViewColumn *title_column;
330
331     cur_fmt = COL_NUMBER;    /*  Set the default new column type */
332     column_prefs_add_custom (cur_fmt, title, NULL);
333
334     model = gtk_tree_view_get_model(column_l);
335     gtk_list_store_append(GTK_LIST_STORE(model), &iter);
336     gtk_list_store_set(GTK_LIST_STORE(model), &iter, 
337                        0, title,
338                        1, col_format_desc(cur_fmt),
339                        2, g_list_last(prefs.col_list),
340                        -1);
341
342     /* Triggers call to column_list_select_cb()   */
343     gtk_tree_selection_select_iter(gtk_tree_view_get_selection(column_l), &iter);
344
345     /* Set the cursor to the 'Title' column of the newly added row and enable editing */
346     /* XXX: If displaying the new title ["New column"] widens the title column of the */
347     /*      treeview, then the set_cursor below doesn't properly generate an entry    */
348     /*      box around the title text. The width of the box appears to be the column  */
349     /*      width before the treeview title column was widened.  Seems like a bug...  */
350     /*      I haven't found a work-around.                                            */
351     path = gtk_tree_model_get_path(model, &iter);
352     title_column = gtk_tree_view_get_column(column_l, 0);
353     gtk_tree_view_set_cursor(column_l, path, title_column, TRUE);
354     gtk_tree_path_free(path);
355
356     cfile.cinfo.columns_changed = TRUE;
357 }
358
359
360 static void
361 column_list_delete_cb(GtkWidget *w _U_, gpointer data) {
362     GtkTreeView      *column_l = GTK_TREE_VIEW(data);
363     GList            *clp;
364     fmt_data         *cfmt;
365     GtkTreeSelection *sel;
366     GtkTreeModel     *model;
367     GtkTreeIter       iter;
368     GtkTreeIter       new_iter;
369
370     sel = gtk_tree_view_get_selection(column_l);
371     if (gtk_tree_selection_get_selected(sel, &model, &iter))
372     {
373         gtk_tree_model_get(model, &iter, 2, &clp, -1);
374
375         cfmt = (fmt_data *) clp->data;
376         g_free(cfmt->title);
377         g_free(cfmt->fmt);
378         g_free(cfmt->custom_field);
379         g_free(cfmt);
380         prefs.col_list = g_list_remove_link(prefs.col_list, clp);
381
382         /* Change the row selection to the next row (if available) or    */
383         /*  the previous row (if available). If there's only one row     */
384         /*  in the store (no previous and no next), then the selection   */
385         /*  will not be changed.                                         */
386
387         /* Note that gtk_tree_selection_select_iter() will trigger a     */
388         /* call to column_list_select_cb().                              */
389
390         new_iter = iter;
391         if ( gtk_tree_model_iter_next(model, &new_iter)) {
392             gtk_tree_selection_select_iter(sel, &new_iter);
393         } else { /* "gtk_tree_model_iter_prev" */
394             GtkTreePath *path = gtk_tree_model_get_path(model, &iter);
395             if (gtk_tree_path_prev(path)) {
396                 gtk_tree_model_get_iter(model, &new_iter, path);
397                 gtk_tree_selection_select_iter(sel, &new_iter);
398             }
399             gtk_tree_path_free(path);
400         }
401
402         /* Remove the row from the list store.                           */
403         /*  We prevent triggering call to column_row_deleted_cb() since  */
404         /*  the entry has already been removed from prefs.col_list and   */
405         /*  since rebuilding the list is not needed because the order    */
406         /*  of the list hasn't changed.                                  */
407
408         g_signal_handler_block(model, column_row_deleted_handler_id);
409
410         /* list_store_remove below will trigger a call to                */
411         /* column_list_select_cb() only when deleting the last entry in  */
412         /* the column list.                                              */
413         /* (This is because the selection in this case changes to        */
414         /*  "nothing selected" when the last row is removed.             */
415
416         gtk_list_store_remove(GTK_LIST_STORE(model), &iter);
417
418         g_signal_handler_unblock  (model, column_row_deleted_handler_id);
419
420         cfile.cinfo.columns_changed = TRUE;
421     }
422 }
423
424
425 static gboolean
426 column_title_changed_cb(GtkCellRendererText *cell _U_, const gchar *str_path, const gchar *new_title, gpointer data) {
427     fmt_data     *cfmt;
428     GList        *clp;
429     GtkTreeModel *model = (GtkTreeModel *)data;
430     GtkTreePath  *path = gtk_tree_path_new_from_string (str_path);
431     GtkTreeIter   iter;
432
433     gtk_tree_model_get_iter(model, &iter, path); 
434   
435     gtk_list_store_set(GTK_LIST_STORE(model), &iter, 0, new_title, -1);
436
437     gtk_tree_model_get(model, &iter, 2, &clp, -1);
438     if (clp) {    
439         cfmt  = (fmt_data *) clp->data;
440         g_free(cfmt->title);
441         cfmt->title = g_strdup(new_title);
442     }
443
444     gtk_tree_path_free (path);
445     cfile.cinfo.columns_changed = TRUE; 
446     return TRUE;  
447 }
448
449 /*
450  * column list row selection changed.
451  *  Set the "Properties" widgets to match the currently selected column row item.
452  */
453
454 static void
455 column_list_select_cb(GtkTreeSelection *sel, gpointer data _U_)
456 {
457     fmt_data     *cfmt;
458     gint          cur_fmt;
459     GList        *clp;
460     GtkTreeModel *model;
461     GtkTreeIter   iter;
462
463     /* if something was selected */
464     if (gtk_tree_selection_get_selected(sel, &model, &iter))
465     {
466         gtk_tree_model_get(model, &iter, 2, &clp, -1);
467         g_assert(clp != NULL);
468         cfmt    = (fmt_data *) clp->data;
469         cur_fmt = get_column_format_from_str(cfmt->fmt);
470         g_assert(cur_fmt != -1);     /* It should always be valid */
471
472         g_signal_handler_block  (fmt_cmb, column_menu_changed_handler_id);
473         gtk_combo_box_set_active(GTK_COMBO_BOX(fmt_cmb), cur_fmt);
474         g_signal_handler_unblock(fmt_cmb, column_menu_changed_handler_id);
475
476         g_signal_handler_block  (field_te, column_field_changed_handler_id);
477         if (cur_fmt == COL_CUSTOM) {
478             gtk_entry_set_text(GTK_ENTRY(field_te), cfmt->custom_field);
479             gtk_widget_set_sensitive(field_lb, TRUE);
480             gtk_widget_set_sensitive(field_te, TRUE);
481         } else {
482             gtk_editable_delete_text(GTK_EDITABLE(field_te), 0, -1);
483             gtk_widget_set_sensitive(field_lb, FALSE);
484             gtk_widget_set_sensitive(field_te, FALSE);
485         }
486         g_signal_handler_unblock(field_te, column_field_changed_handler_id);
487
488         gtk_widget_set_sensitive(remove_bt, TRUE);
489         gtk_widget_set_sensitive(fmt_cmb, TRUE);
490     }
491     else
492     {
493         gtk_editable_delete_text(GTK_EDITABLE(field_te), 0, -1);
494
495         gtk_widget_set_sensitive(remove_bt, FALSE);
496         gtk_widget_set_sensitive(field_te, FALSE);
497         gtk_widget_set_sensitive(fmt_cmb, FALSE);
498     }
499 }
500
501
502 /*
503  * The user selected a new entry in the format combo-box;
504  *       Note: column_menu_changed_cb is expected to be called only
505  *         when the user changes the format combo-box.
506  * Action:
507  * If no selection active in the column list:
508  *    Hide 'field"; desensitize buttons.
509  *    XXX: Can this happen ?
510  * If a column list selection is active:
511  *    1. Update the display of "field" as req'd (depending upon
512  *       whether the active combo-box entry is the "custom"
513  *       format type).
514  *    2. Update the column_list and prefs.col_formats.
515  *       Set columns_changed = TRUE.
516  */
517
518 static void
519 column_menu_changed_cb(GtkWidget *w, gpointer data) {
520     GtkTreeView      *column_l = GTK_TREE_VIEW(data);
521     fmt_data         *cfmt;
522     gint              cur_fmt;
523     gint              cur_cb_fmt;
524     GList            *clp;
525     gchar            *fmt;
526     GtkTreeSelection *sel;
527     GtkTreeModel     *model;
528     GtkTreeIter       iter;
529
530     sel = gtk_tree_view_get_selection(column_l);
531     if (! (gtk_tree_selection_get_selected(sel, &model, &iter)))
532         return;  /* no column list selection [Can this happen ?]: ignore callback */
533
534     cur_cb_fmt = gtk_combo_box_get_active(GTK_COMBO_BOX(w));
535     gtk_tree_model_get(model, &iter, 2, &clp, -1);
536     cfmt    = (fmt_data *) clp->data;
537     cur_fmt = get_column_format_from_str(cfmt->fmt);
538
539     g_assert(cur_cb_fmt != cur_fmt);
540
541     /* The User has selected a new format in the combo-box    */
542     /*  (IE: combo-box format != current selected row format) */
543     /* Update field widgets, list_store, column format array  */
544     /*  entry as appropriate.                                 */
545     g_signal_handler_block  (field_te, column_field_changed_handler_id);
546     if (cur_fmt == COL_CUSTOM) {
547         /* Changing from custom to non-custom   */
548         gtk_editable_delete_text(GTK_EDITABLE(field_te), 0, -1);
549         fmt = g_strdup_printf("%s", col_format_desc(cur_cb_fmt));
550         gtk_widget_set_sensitive(field_lb, FALSE);
551         gtk_widget_set_sensitive(field_te, FALSE);
552
553     } else if (cur_cb_fmt == COL_CUSTOM) {
554         /* Changing from non-custom to custom   */
555         if (cfmt->custom_field == NULL)
556             cfmt->custom_field = g_strdup("");
557         /* The following doesn't trigger a call to menu_field_changed_cb()    */
558         gtk_entry_set_text(GTK_ENTRY(field_te), cfmt->custom_field);
559         fmt = g_strdup_printf("%s (%s)", col_format_desc(cur_cb_fmt), cfmt->custom_field);
560         gtk_widget_set_sensitive(field_lb, TRUE);
561         gtk_widget_set_sensitive(field_te, TRUE);
562
563     } else {
564         /* Changing from non-custom to non-custom */
565         fmt = g_strdup_printf("%s", col_format_desc(cur_cb_fmt));
566     }
567     g_signal_handler_unblock(field_te, column_field_changed_handler_id);
568
569     gtk_list_store_set(GTK_LIST_STORE(model), &iter, 1, fmt, -1);
570     g_free(fmt);
571     g_free(cfmt->fmt);
572     cfmt->fmt = g_strdup(col_format_to_string(cur_cb_fmt));
573     cfile.cinfo.columns_changed = TRUE;
574 }
575
576
577 /*
578  * The user changed the custom field entry box or
579  *  the field entry box has been updated because a new
580  *  column row with custom format has been selected.
581  * If the current field entry matches that of the current
582  *  column row, this is just an update because a new
583  *  column row has been selected. Do nothing.
584  * If the two are different, then update the column row & etc.
585  */
586 static void
587 column_field_changed_cb(GtkEditable *te, gpointer data) {
588     fmt_data         *cfmt;
589     gint              cur_fmt;
590     GList            *clp;
591     gchar            *field, *fmt;
592     GtkTreeView      *tree = (GtkTreeView *)data;
593     GtkTreeSelection *sel;
594     GtkTreeModel     *model;
595     GtkTreeIter       iter;
596
597     sel = gtk_tree_view_get_selection(tree);
598     if ( ! (gtk_tree_selection_get_selected(sel, &model, &iter))) {
599         return;
600     }
601
602     field = gtk_editable_get_chars(te, 0, -1);
603     gtk_tree_model_get(model, &iter, 2, &clp, -1);
604     cfmt  = (fmt_data *) clp->data;
605     if (strcmp(cfmt->custom_field, field) == 0) {
606         return; /* no action req'd */
607     }
608
609     /* The user has entered a new value in the field entry box: make the req'd changes */ 
610     cur_fmt = get_column_format_from_str(cfmt->fmt);
611     fmt = g_strdup_printf("%s (%s)", col_format_desc(cur_fmt), field);
612
613     gtk_list_store_set(GTK_LIST_STORE(model), &iter, 1, fmt, -1);
614     g_free(fmt);
615     g_free(cfmt->custom_field);
616     cfmt->custom_field = field;
617     cfile.cinfo.columns_changed = TRUE;
618 }
619
620
621 /*
622  * Callback for the "row-deleted" signal emitted when a list item is dragged.
623  * http://library.gnome.org/devel/gtk/stable/GtkTreeModel.html#GtkTreeModel-rows-reordered
624  * says that DND deletes, THEN inserts the row. 
625  *
626  * XXX: For the record: For Gtk+ 2.16.0 testing shows the actual sequence for drag-and-drop to be as follows:
627  *      1. Insert a new, empty row at the destination;
628  *      2. Emit a "row-inserted" signal on the model; invoke any row-inserted callbacks & etc;
629  *      3. Copy the source row data to the new (empty) destination row;
630  *      4. Delete the source row;
631  *      5. Emit a "row-deleted" signal; invoke any row-deleted callbacks & etc.
632  *
633  *  The code below (invoked as a consequence of a "row-deleted" signal) rebuilds 
634  *  prefs.col_list by iterating over the (re-ordered) tree model.
635  */
636 static void
637 column_dnd_row_deleted_cb(GtkTreeModel *model, GtkTreePath *path _U_, gpointer data _U_) {
638     GtkTreeIter   iter;
639     GList        *clp, *new_col_list = NULL;
640     gboolean      items_left;
641
642     /*
643      * XXX - This rebuilds prefs.col_list based on the current model. We
644      * might just want to do this when the prefs are applied
645      */
646     for (items_left = gtk_tree_model_get_iter_first (model, &iter);
647          items_left;
648          items_left = gtk_tree_model_iter_next (model, &iter)) {
649
650         gtk_tree_model_get(model, &iter, 2, &clp, -1);
651         if (clp) {
652             prefs.col_list = g_list_remove_link(prefs.col_list, clp);
653             new_col_list = g_list_concat(new_col_list, clp);
654         }
655     }
656     if (prefs.col_list) {
657         g_warning("column_dnd_row_deleted_cb: prefs.col_list has %d leftover data",
658                   g_list_length(prefs.col_list));
659         g_list_free(prefs.col_list);
660     }
661
662     prefs.col_list = new_col_list;
663     cfile.cinfo.columns_changed = TRUE;
664 }
665
666
667 void
668 column_prefs_fetch(GtkWidget *w _U_) {
669 }
670
671
672 void
673 column_prefs_apply(GtkWidget *w _U_)
674 {
675     /* Redraw the packet list if the columns were changed */
676     if(cfile.cinfo.columns_changed) {
677 #ifdef NEW_PACKET_LIST
678         new_packet_list_recreate();
679 #else
680         packet_list_recreate();
681 #endif
682         cfile.cinfo.columns_changed = FALSE; /* Reset value */
683     }
684 }
685
686
687 void
688 column_prefs_destroy(GtkWidget *w _U_) {
689 }