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