Fix various typos and spelling errors.
[obnox/wireshark/wip.git] / gtk / service_response_time_table.c
1 /* service_response_time_table.c
2  * service_response_time_table   2003 Ronnie Sahlberg
3  * Helper routines common to all service response time statistics
4  * tap.
5  *
6  * $Id$
7  *
8  * Wireshark - Network traffic analyzer
9  * By Gerald Combs <gerald@wireshark.org>
10  * Copyright 1998 Gerald Combs
11  *
12  * This program is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU General Public License
14  * as published by the Free Software Foundation; either version 2
15  * of the License, or (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
25  */
26
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #include <gtk/gtk.h>
32 #include <stdio.h>
33
34 #include "epan/packet_info.h"
35
36 #include "../simple_dialog.h"
37
38 #include "gtk/service_response_time_table.h"
39 #include "gtk/filter_utils.h"
40 #include "gtk/gui_utils.h"
41 #include "gtk/utf8_entities.h"
42
43 #define NANOSECS_PER_SEC 1000000000
44
45 enum
46 {
47         INDEX_COLUMN,
48         PROCEDURE_COLUMN,
49         CALLS_COLUMN,
50         MIN_SRT_COLUMN,
51         MAX_SRT_COLUMN,
52         AVG_SRT_COLUMN,
53         N_COLUMNS
54 };
55
56
57 static void
58 srt_select_filter_cb(GtkWidget *widget _U_, gpointer callback_data, guint callback_action)
59 {
60         srt_stat_table *rst = (srt_stat_table *)callback_data;
61         char *str = NULL;
62         GtkTreeIter iter;
63         GtkTreeModel *model;
64         GtkTreeSelection  *sel;
65         int selection;
66
67         if(rst->filter_string==NULL){
68                 return;
69         }
70
71         sel = gtk_tree_view_get_selection (GTK_TREE_VIEW(rst->table));
72
73         if (!gtk_tree_selection_get_selected(sel, &model, &iter))
74                 return;
75
76         gtk_tree_model_get (model, &iter, INDEX_COLUMN, &selection, -1);
77         if(selection>=(int)rst->num_procs){
78                 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "No procedure selected");
79                 return;
80         }
81
82         str = g_strdup_printf("%s==%d", rst->filter_string, selection);
83
84         apply_selected_filter (callback_action, str);
85
86         g_free(str);
87 }
88
89 static gboolean
90 srt_show_popup_menu_cb(void *widg _U_, GdkEvent *event, srt_stat_table *rst)
91 {
92         GdkEventButton *bevent = (GdkEventButton *)event;
93
94         if(event->type==GDK_BUTTON_PRESS && bevent->button==3){
95                 gtk_menu_popup(GTK_MENU(rst->menu), NULL, NULL, NULL, NULL,
96                         bevent->button, bevent->time);
97         }
98
99         return FALSE;
100 }
101
102
103 /* Action callbacks */
104 static void
105 apply_as_selected_cb(GtkWidget *widget, gpointer user_data)
106 {
107         srt_select_filter_cb( widget , user_data, CALLBACK_MATCH(ACTYPE_SELECTED, 0));
108 }
109 static void
110 apply_as_not_selected_cb(GtkWidget *widget, gpointer user_data)
111 {
112         srt_select_filter_cb( widget , user_data, CALLBACK_MATCH(ACTYPE_NOT_SELECTED, 0));
113 }
114 static void
115 apply_as_and_selected_cb(GtkWidget *widget, gpointer user_data)
116 {
117         srt_select_filter_cb( widget , user_data, CALLBACK_MATCH(ACTYPE_AND_SELECTED, 0));
118 }
119 static void
120 apply_as_or_selected_cb(GtkWidget *widget, gpointer user_data)
121 {
122         srt_select_filter_cb( widget , user_data, CALLBACK_MATCH(ACTYPE_OR_SELECTED, 0));
123 }
124 static void
125 apply_as_and_not_selected_cb(GtkWidget *widget, gpointer user_data)
126 {
127         srt_select_filter_cb( widget , user_data, CALLBACK_MATCH(ACTYPE_AND_NOT_SELECTED, 0));
128 }
129 static void
130 apply_as_or_not_selected_cb(GtkWidget *widget, gpointer user_data)
131 {
132         srt_select_filter_cb( widget , user_data, CALLBACK_MATCH(ACTYPE_OR_NOT_SELECTED, 0));
133 }
134
135 static void
136 prep_as_selected_cb(GtkWidget *widget, gpointer user_data)
137 {
138         srt_select_filter_cb( widget , user_data, CALLBACK_PREPARE(ACTYPE_SELECTED, 0));
139 }
140 static void
141 prep_as_not_selected_cb(GtkWidget *widget, gpointer user_data)
142 {
143         srt_select_filter_cb( widget , user_data, CALLBACK_PREPARE(ACTYPE_NOT_SELECTED, 0));
144 }
145 static void
146 prep_as_and_selected_cb(GtkWidget *widget, gpointer user_data)
147 {
148         srt_select_filter_cb( widget , user_data, CALLBACK_PREPARE(ACTYPE_AND_SELECTED, 0));
149 }
150 static void
151 prep_as_or_selected_cb(GtkWidget *widget, gpointer user_data)
152 {
153         srt_select_filter_cb( widget , user_data, CALLBACK_PREPARE(ACTYPE_OR_SELECTED, 0));
154 }
155 static void
156 prep_as_and_not_selected_cb(GtkWidget *widget, gpointer user_data)
157 {
158         srt_select_filter_cb( widget , user_data, CALLBACK_PREPARE(ACTYPE_AND_NOT_SELECTED, 0));
159 }
160 static void
161 prep_as_or_not_selected_cb(GtkWidget *widget, gpointer user_data)
162 {
163         srt_select_filter_cb( widget , user_data, CALLBACK_PREPARE(ACTYPE_OR_NOT_SELECTED, 0));
164 }
165
166 static void
167 find_selected_cb(GtkWidget *widget, gpointer user_data)
168 {
169         srt_select_filter_cb( widget , user_data, CALLBACK_FIND_FRAME(ACTYPE_SELECTED, 0));
170 }
171 static void
172 find_not_selected_cb(GtkWidget *widget, gpointer user_data)
173 {
174         srt_select_filter_cb( widget , user_data, CALLBACK_FIND_FRAME(ACTYPE_NOT_SELECTED, 0));
175 }
176 static void
177 find_prev_selected_cb(GtkWidget *widget, gpointer user_data)
178 {
179         srt_select_filter_cb( widget , user_data, CALLBACK_FIND_PREVIOUS(ACTYPE_SELECTED, 0));
180 }
181 static void
182 find_prev_not_selected_cb(GtkWidget *widget, gpointer user_data)
183 {
184         srt_select_filter_cb( widget , user_data, CALLBACK_FIND_PREVIOUS(ACTYPE_NOT_SELECTED, 0));
185 }
186 static void
187 find_next_selected_cb(GtkWidget *widget, gpointer user_data)
188 {
189         srt_select_filter_cb( widget , user_data, CALLBACK_FIND_NEXT(ACTYPE_SELECTED, 0));
190 }
191 static void
192 find_next_not_selected_cb(GtkWidget *widget, gpointer user_data)
193 {
194         srt_select_filter_cb( widget , user_data, CALLBACK_FIND_NEXT(ACTYPE_NOT_SELECTED, 0));
195 }
196 static void
197 color_selected_cb(GtkWidget *widget, gpointer user_data)
198 {
199         srt_select_filter_cb( widget , user_data, CALLBACK_COLORIZE(ACTYPE_SELECTED, 0));
200 }
201 static void
202 color_not_selected_cb(GtkWidget *widget, gpointer user_data)
203 {
204         srt_select_filter_cb( widget , user_data, CALLBACK_COLORIZE(ACTYPE_SELECTED, 0));
205 }
206
207 static const char *ui_desc_service_resp_t_filter_popup =
208 "<ui>\n"
209 "  <popup name='ServiceRespTFilterPopup'>\n"
210 "    <menu action='/Apply as Filter'>\n"
211 "      <menuitem action='/Apply as Filter/Selected'/>\n"
212 "      <menuitem action='/Apply as Filter/" UTF8_HORIZONTAL_ELLIPSIS " not Selected'/>\n"
213 "      <menuitem action='/Apply as Filter/" UTF8_HORIZONTAL_ELLIPSIS " and Selected'/>\n"
214 "      <menuitem action='/Apply as Filter/" UTF8_HORIZONTAL_ELLIPSIS " or Selected'/>\n"
215 "      <menuitem action='/Apply as Filter/" UTF8_HORIZONTAL_ELLIPSIS " and not Selected'/>\n"
216 "      <menuitem action='/Apply as Filter/" UTF8_HORIZONTAL_ELLIPSIS " or not Selected'/>\n"
217 "    </menu>\n"
218 "    <menu action='/Prepare a Filter'>\n"
219 "      <menuitem action='/Prepare a Filter/Selected'/>\n"
220 "      <menuitem action='/Prepare a Filter/" UTF8_HORIZONTAL_ELLIPSIS " not Selected'/>\n"
221 "      <menuitem action='/Prepare a Filter/" UTF8_HORIZONTAL_ELLIPSIS " and Selected'/>\n"
222 "      <menuitem action='/Prepare a Filter/" UTF8_HORIZONTAL_ELLIPSIS " or Selected'/>\n"
223 "      <menuitem action='/Prepare a Filter/" UTF8_HORIZONTAL_ELLIPSIS " and not Selected'/>\n"
224 "      <menuitem action='/Prepare a Filter/" UTF8_HORIZONTAL_ELLIPSIS " or not Selected'/>\n"
225 "    </menu>\n"
226 "    <menu action='/Find Frame'>\n"
227 "      <menu action='/Find Frame/Find Frame'>\n"
228 "        <menuitem action='/Find Frame/Selected'/>\n"
229 "        <menuitem action='/Find Frame/Not Selected'/>\n"
230 "      </menu>\n"
231 "      <menu action='/Find Frame/Find Next'>\n"
232 "        <menuitem action='/Find Next/Selected'/>\n"
233 "        <menuitem action='/Find Next/Not Selected'/>\n"
234 "      </menu>\n"
235 "      <menu action='/Find Frame/Find Previous'>\n"
236 "        <menuitem action='/Find Previous/Selected'/>\n"
237 "        <menuitem action='/Find Previous/Not Selected'/>\n"
238 "      </menu>\n"
239 "    </menu>\n"
240 "    <menu action='/Colorize Procedure'>\n"
241 "     <menuitem action='/Colorize Procedure/Selected'/>\n"
242 "     <menuitem action='/Colorize Procedure/Not Selected'/>\n"
243 "    </menu>\n"
244 "  </popup>\n"
245 "</ui>\n";
246
247 /*
248  * GtkActionEntry
249  * typedef struct {
250  *   const gchar     *name;
251  *   const gchar     *stock_id;
252  *   const gchar     *label;
253  *   const gchar     *accelerator;
254  *   const gchar     *tooltip;
255  *   GCallback  callback;
256  * } GtkActionEntry;
257  * const gchar *name;                   The name of the action.
258  * const gchar *stock_id;               The stock id for the action, or the name of an icon from the icon theme.
259  * const gchar *label;                  The label for the action. This field should typically be marked for translation,
260  *                                                              see gtk_action_group_set_translation_domain().
261  *                                                              If label is NULL, the label of the stock item with id stock_id is used.
262  * const gchar *accelerator;    The accelerator for the action, in the format understood by gtk_accelerator_parse().
263  * const gchar *tooltip;                The tooltip for the action. This field should typically be marked for translation,
264  *                              see gtk_action_group_set_translation_domain().
265  * GCallback callback;                  The function to call when the action is activated.
266  *
267  */
268 static const GtkActionEntry service_resp_t__popup_entries[] = {
269   { "/Apply as Filter",                                                 NULL, "Apply as Filter",                NULL, NULL,                                             NULL },
270   { "/Prepare a Filter",                                                NULL, "Prepare a Filter",               NULL, NULL,                                             NULL },
271   { "/Find Frame",                                                              NULL, "Find Frame",                             NULL, NULL,                                             NULL },
272   { "/Find Frame/Find Frame",                                   NULL, "Find Frame",                             NULL, NULL,                                             NULL },
273   { "/Find Frame/Find Next",                                    NULL, "Find Next" ,                             NULL, NULL,                                             NULL },
274   { "/Find Frame/Find Previous",                                NULL, "Find Previous",                  NULL, NULL,                                             NULL },
275   { "/Colorize Procedure",                                              NULL, "Colorize Procedure",             NULL, NULL,                                             NULL },
276   { "/Apply as Filter/Selected",                                NULL, "Selected",                               NULL, "Selected",                               G_CALLBACK(apply_as_selected_cb) },
277   { "/Apply as Filter/" UTF8_HORIZONTAL_ELLIPSIS " not Selected",               NULL, UTF8_HORIZONTAL_ELLIPSIS " not Selected",         NULL, UTF8_HORIZONTAL_ELLIPSIS " not Selected",         G_CALLBACK(apply_as_not_selected_cb) },
278   { "/Apply as Filter/" UTF8_HORIZONTAL_ELLIPSIS " and Selected",               NULL, UTF8_HORIZONTAL_ELLIPSIS " and Selected",         NULL, UTF8_HORIZONTAL_ELLIPSIS " and Selected",         G_CALLBACK(apply_as_and_selected_cb) },
279   { "/Apply as Filter/" UTF8_HORIZONTAL_ELLIPSIS " or Selected",                        NULL, UTF8_HORIZONTAL_ELLIPSIS " or Selected",          NULL, UTF8_HORIZONTAL_ELLIPSIS " or Selected",          G_CALLBACK(apply_as_or_selected_cb) },
280   { "/Apply as Filter/" UTF8_HORIZONTAL_ELLIPSIS " and not Selected",   NULL, UTF8_HORIZONTAL_ELLIPSIS " and not Selected",     NULL, UTF8_HORIZONTAL_ELLIPSIS " and not Selected",     G_CALLBACK(apply_as_and_not_selected_cb) },
281   { "/Apply as Filter/" UTF8_HORIZONTAL_ELLIPSIS " or not Selected",            NULL, UTF8_HORIZONTAL_ELLIPSIS " or not Selected",      NULL, UTF8_HORIZONTAL_ELLIPSIS " or not Selected",      G_CALLBACK(apply_as_or_not_selected_cb) },
282   { "/Prepare a Filter/Selected",                               NULL, "Selected",                               NULL, "selcted",                                G_CALLBACK(prep_as_selected_cb) },
283   { "/Prepare a Filter/" UTF8_HORIZONTAL_ELLIPSIS " not Selected",              NULL, UTF8_HORIZONTAL_ELLIPSIS " not Selected",         NULL, UTF8_HORIZONTAL_ELLIPSIS " not Selected",         G_CALLBACK(prep_as_not_selected_cb) },
284   { "/Prepare a Filter/" UTF8_HORIZONTAL_ELLIPSIS " and Selected",              NULL, UTF8_HORIZONTAL_ELLIPSIS " and Selected",         NULL, UTF8_HORIZONTAL_ELLIPSIS " and Selected",         G_CALLBACK(prep_as_and_selected_cb) },
285   { "/Prepare a Filter/" UTF8_HORIZONTAL_ELLIPSIS " or Selected",               NULL, UTF8_HORIZONTAL_ELLIPSIS " or Selected",          NULL, UTF8_HORIZONTAL_ELLIPSIS " or Selected",          G_CALLBACK(prep_as_or_selected_cb) },
286   { "/Prepare a Filter/" UTF8_HORIZONTAL_ELLIPSIS " and not Selected",  NULL, UTF8_HORIZONTAL_ELLIPSIS " and not Selected",     NULL, UTF8_HORIZONTAL_ELLIPSIS " and not Selected",     G_CALLBACK(prep_as_and_not_selected_cb) },
287   { "/Prepare a Filter/" UTF8_HORIZONTAL_ELLIPSIS " or not Selected",   NULL, UTF8_HORIZONTAL_ELLIPSIS " or not Selected",      NULL, UTF8_HORIZONTAL_ELLIPSIS " or not Selected",      G_CALLBACK(prep_as_or_not_selected_cb) },
288   { "/Find Frame/Selected",                                             NULL, "Selected",                               NULL, "Selected",                               G_CALLBACK(find_selected_cb) },
289   { "/Find Frame/Not Selected",                                 NULL, "Not Selected",                   NULL, "Not Selected",                   G_CALLBACK(find_not_selected_cb) },
290   { "/Find Previous/Selected",                                  NULL, "Selected",                               NULL, "Selected",                               G_CALLBACK(find_prev_selected_cb) },
291   { "/Find Previous/Not Selected",                              NULL, "Not Selected",                   NULL, "Not Selected",                   G_CALLBACK(find_prev_not_selected_cb) },
292   { "/Find Next/Selected",                                              NULL, "Selected",                               NULL, "Selected",                               G_CALLBACK(find_next_selected_cb) },
293   { "/Find Next/Not Selected",                                  NULL, "Not Selected",                   NULL, "Not Selected",                   G_CALLBACK(find_next_not_selected_cb) },
294   { "/Colorize Procedure/Selected",                             NULL, "Selected",                               NULL, "Selected",                               G_CALLBACK(color_selected_cb) },
295   { "/Colorize Procedure/Not Selected",                 NULL, "Not Selected",                   NULL, "Not Selected",                   G_CALLBACK(color_not_selected_cb) },
296 };
297
298 static void
299 srt_create_popup_menu(srt_stat_table *rst)
300 {
301         GtkUIManager *ui_manager;
302         GtkActionGroup *action_group;
303         GError *error = NULL;
304
305         action_group = gtk_action_group_new ("ServiceRespTFilterPopupActionGroup");
306         gtk_action_group_add_actions (action_group,                                     /* the action group */
307                                       service_resp_t__popup_entries,                    /* an array of action descriptions */
308                                       G_N_ELEMENTS(service_resp_t__popup_entries),      /* the number of entries */
309                                       rst);                                             /* data to pass to the action callbacks */
310
311         ui_manager = gtk_ui_manager_new ();
312         gtk_ui_manager_insert_action_group (ui_manager,
313                 action_group,
314                 0); /* the position at which the group will be inserted */
315         gtk_ui_manager_add_ui_from_string (ui_manager,ui_desc_service_resp_t_filter_popup, -1, &error);
316         if (error != NULL)
317     {
318         fprintf (stderr, "Warning: building service response time filter popup failed: %s\n",
319                 error->message);
320         g_error_free (error);
321         error = NULL;
322     }
323         rst->menu = gtk_ui_manager_get_widget(ui_manager, "/ServiceRespTFilterPopup");
324         g_signal_connect(rst->table, "button_press_event", G_CALLBACK(srt_show_popup_menu_cb), rst);
325
326 }
327
328 /* ---------------- */
329 static void
330 srt_time_func (GtkTreeViewColumn *column _U_,
331                GtkCellRenderer   *renderer,
332                GtkTreeModel      *model,
333                GtkTreeIter       *iter,
334                gpointer           user_data)
335 {
336          gchar *str;
337          nstime_t *data;
338
339          /* The col to get data from is in userdata */
340          gint data_column = GPOINTER_TO_INT(user_data);
341
342          gtk_tree_model_get(model, iter, data_column, &data, -1);
343          if (!data) {
344                  g_object_set(renderer, "text", "", NULL);
345                  return;
346          }
347          str = g_strdup_printf("%3d.%06d", (int)data->secs, (data->nsecs+500)/1000);
348          g_object_set(renderer, "text", str, NULL);
349          g_free(str);
350 }
351
352 static void
353 srt_avg_func (GtkTreeViewColumn *column _U_,
354               GtkCellRenderer   *renderer,
355               GtkTreeModel      *model,
356               GtkTreeIter       *iter,
357               gpointer           user_data)
358 {
359         gchar *str;
360         guint64 td;
361         gint data_column = GPOINTER_TO_INT(user_data);
362
363         gtk_tree_model_get(model, iter, data_column, &td, -1);
364         str=g_strdup_printf("%3d.%06d",
365                             (int)(td/1000000), (int)(td%1000000));
366         g_object_set(renderer, "text", str, NULL);
367         g_free(str);
368 }
369
370 static gint
371 srt_time_sort_func(GtkTreeModel *model,
372                    GtkTreeIter *a,
373                    GtkTreeIter *b,
374                    gpointer user_data)
375 {
376          nstime_t *ns_a;
377          nstime_t *ns_b;
378          gint ret = 0;
379          gint data_column = GPOINTER_TO_INT(user_data);
380
381          gtk_tree_model_get(model, a, data_column, &ns_a, -1);
382          gtk_tree_model_get(model, b, data_column, &ns_b, -1);
383
384         if (ns_a == ns_b) {
385                 ret = 0;
386         }
387         else if (ns_a == NULL || ns_b == NULL) {
388                 ret = (ns_a == NULL) ? -1 : 1;
389         }
390         else {
391                 ret = nstime_cmp(ns_a,ns_b);
392         }
393         return ret;
394 }
395
396 /*
397     XXX Resizable columns are ugly when there's more than on table cf. SMB
398 */
399 void
400 init_srt_table(srt_stat_table *rst, int num_procs, GtkWidget *vbox, const char *filter_string)
401 {
402         int i;
403         GtkListStore *store;
404         GtkWidget *tree;
405         GtkTreeViewColumn *column;
406         GtkCellRenderer *renderer;
407         GtkTreeSortable *sortable;
408         GtkTreeSelection  *sel;
409
410         const char *default_titles[] = { "Index", "Procedure", "Calls", "Min SRT", "Max SRT", "Avg SRT" };
411
412         /* Create the store */
413         store = gtk_list_store_new (N_COLUMNS,  /* Total number of columns */
414                                     G_TYPE_INT,         /* Index     */
415                                     G_TYPE_STRING,   /* Procedure */
416                                     G_TYPE_UINT,        /* Calls     */
417                                     G_TYPE_POINTER,  /* Min SRT   */
418                                     G_TYPE_POINTER,  /* Max SRT   */
419                                     G_TYPE_UINT64);  /* Avg SRT   */
420
421         /* Create a view */
422         tree = gtk_tree_view_new_with_model (GTK_TREE_MODEL (store));
423         rst->table = GTK_TREE_VIEW(tree);
424         sortable = GTK_TREE_SORTABLE(store);
425
426         /* The view now holds a reference.  We can get rid of our own reference */
427         g_object_unref (G_OBJECT (store));
428
429         if(filter_string){
430                 rst->filter_string=g_strdup(filter_string);
431         } else {
432                 rst->filter_string=NULL;
433         }
434         for (i = 0; i < N_COLUMNS; i++) {
435                 renderer = gtk_cell_renderer_text_new ();
436                 if (i != PROCEDURE_COLUMN) {
437                         /* right align numbers */
438                         g_object_set(G_OBJECT(renderer), "xalign", 1.0, NULL);
439                 }
440                 g_object_set(renderer, "ypad", 0, NULL);
441                 switch (i) {
442                 case MIN_SRT_COLUMN:
443                 case MAX_SRT_COLUMN:
444                         column = gtk_tree_view_column_new_with_attributes (default_titles[i], renderer, NULL);
445                         gtk_tree_view_column_set_cell_data_func(column, renderer, srt_time_func,  GINT_TO_POINTER(i), NULL);
446                         gtk_tree_sortable_set_sort_func(sortable, i, srt_time_sort_func, GINT_TO_POINTER(i), NULL);
447                         break;
448                 case AVG_SRT_COLUMN:
449                         column = gtk_tree_view_column_new_with_attributes (default_titles[i], renderer, NULL);
450                         gtk_tree_view_column_set_cell_data_func(column, renderer, srt_avg_func,  GINT_TO_POINTER(i), NULL);
451                         break;
452                 default:
453                         column = gtk_tree_view_column_new_with_attributes (default_titles[i], renderer, "text",
454                                         i, NULL);
455                         break;
456                 }
457
458                 gtk_tree_view_column_set_sort_column_id(column, i);
459                 gtk_tree_view_column_set_resizable(column, TRUE);
460                 gtk_tree_view_append_column (rst->table, column);
461                 if (i == CALLS_COLUMN) {
462                         /* XXX revert order sort */
463                         gtk_tree_view_column_clicked(column);
464                         gtk_tree_view_column_clicked(column);
465                 }
466         }
467
468         rst->scrolled_window=scrolled_window_new(NULL, NULL);
469         gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(rst->scrolled_window),
470                                             GTK_SHADOW_IN);
471         gtk_container_add(GTK_CONTAINER(rst->scrolled_window), GTK_WIDGET (rst->table));
472         gtk_box_pack_start(GTK_BOX(vbox), rst->scrolled_window, TRUE, TRUE, 0);
473
474         gtk_tree_view_set_reorderable (rst->table, FALSE);
475         /* Now enable the sorting of each column */
476         gtk_tree_view_set_rules_hint(rst->table, TRUE);
477         gtk_tree_view_set_headers_clickable(rst->table, TRUE);
478
479         gtk_widget_show(rst->scrolled_window);
480
481         rst->num_procs=num_procs;
482         rst->procedures=g_malloc(sizeof(srt_procedure_t)*num_procs);
483         for(i=0;i<num_procs;i++){
484                 time_stat_init(&rst->procedures[i].stats);
485                 rst->procedures[i].index = 0;
486                 rst->procedures[i].procedure = NULL;
487         }
488
489         sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(rst->table));
490         gtk_tree_selection_set_mode(sel, GTK_SELECTION_SINGLE);
491         /* create popup menu for this table */
492         if(rst->filter_string){
493                 srt_create_popup_menu(rst);
494         }
495 }
496
497 void
498 init_srt_table_row(srt_stat_table *rst, int index, const char *procedure)
499 {
500         /* we have discovered a new procedure. Extend the table accordingly */
501         if(index>=rst->num_procs){
502                 int old_num_procs=rst->num_procs;
503                 int i;
504
505                 rst->num_procs=index+1;
506                 rst->procedures=g_realloc(rst->procedures, sizeof(srt_procedure_t)*(rst->num_procs));
507                 for(i=old_num_procs;i<rst->num_procs;i++){
508                         time_stat_init(&rst->procedures[i].stats);
509                         rst->procedures[i].index = i;
510                         rst->procedures[i].procedure=NULL;
511                 }
512         }
513         rst->procedures[index].index = index;
514         rst->procedures[index].procedure=g_strdup(procedure);
515 }
516
517 void
518 add_srt_table_data(srt_stat_table *rst, int index, const nstime_t *req_time, packet_info *pinfo)
519 {
520         srt_procedure_t *rp;
521         nstime_t t, delta;
522
523         g_assert(index >= 0 && index < rst->num_procs);
524         rp=&rst->procedures[index];
525
526         /*
527          * If the count of calls for this procedure is currently zero, it's
528          * going to become non-zero, so add a row for it (we don't want
529          * rows for procedures that have no calls - especially if the
530          * procedure has no calls because the index doesn't correspond
531          * to a procedure, but is an unused/reserved value).
532          *
533          * (Yes, this means that the rows aren't in order by anything
534          * interesting.  That's why we have the table sorted by a column.)
535          */
536
537         if (rp->stats.num==0){
538                 GtkListStore *store = GTK_LIST_STORE(gtk_tree_view_get_model(rst->table));
539                 gtk_list_store_append(store, &rp->iter);
540                 gtk_list_store_set(store, &rp->iter,
541                                    INDEX_COLUMN,     rp->index,
542                                    PROCEDURE_COLUMN, rp->procedure,
543                                    CALLS_COLUMN,     rp->stats.num,
544                                    MIN_SRT_COLUMN,   NULL,
545                                    MAX_SRT_COLUMN,   NULL,
546                                    AVG_SRT_COLUMN,   (guint64)0,
547                                    -1);
548         }
549
550         /* calculate time delta between request and reply */
551         t=pinfo->fd->abs_ts;
552         nstime_delta(&delta, &t, req_time);
553
554         time_stat_update(&rp->stats, &delta, pinfo);
555 }
556
557 void
558 draw_srt_table_data(srt_stat_table *rst)
559 {
560         int i;
561         guint64 td;
562         GtkListStore *store = GTK_LIST_STORE(gtk_tree_view_get_model(rst->table));
563
564         for(i=0;i<rst->num_procs;i++){
565                 /* ignore procedures with no calls (they don't have rows) */
566                 if(rst->procedures[i].stats.num==0){
567                         continue;
568                 }
569                 /* Scale the average SRT in units of 1us and round to the nearest us.
570                    tot.secs is a time_t which may be 32 or 64 bits (or even floating)
571                    depending uon the platform.  After casting tot.secs to 64 bits, it
572                    would take a capture with a duration of over 136 *years* to
573                    overflow the secs portion of td. */
574                 td = ((guint64)(rst->procedures[i].stats.tot.secs))*NANOSECS_PER_SEC + rst->procedures[i].stats.tot.nsecs;
575                 td = ((td / rst->procedures[i].stats.num) + 500) / 1000;
576
577                 gtk_list_store_set(store, &rst->procedures[i].iter,
578                                    CALLS_COLUMN,     rst->procedures[i].stats.num,
579                                    MIN_SRT_COLUMN,   &rst->procedures[i].stats.min,
580                                    MAX_SRT_COLUMN,   &rst->procedures[i].stats.max,
581                                    AVG_SRT_COLUMN,   td,
582                                    -1);
583         }
584 }
585
586
587 void
588 reset_srt_table_data(srt_stat_table *rst)
589 {
590         int i;
591         GtkListStore *store;
592
593         for(i=0;i<rst->num_procs;i++){
594                 time_stat_init(&rst->procedures[i].stats);
595         }
596         store = GTK_LIST_STORE(gtk_tree_view_get_model(rst->table));
597         gtk_list_store_clear(store);
598 }
599
600 void
601 free_srt_table_data(srt_stat_table *rst)
602 {
603         int i;
604
605         for(i=0;i<rst->num_procs;i++){
606                 g_free(rst->procedures[i].procedure);
607                 rst->procedures[i].procedure=NULL;
608         }
609         g_free(rst->filter_string);
610         rst->filter_string=NULL;
611         g_free(rst->procedures);
612         rst->procedures=NULL;
613         rst->num_procs=0;
614 }
615