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