3b4838dfd832f7294137224b4f1ffb969fb17237
[obnox/wireshark/wip.git] / gtk / sctp_error_dlg.c
1 /* 
2  * Copyright 2004, Irene Ruengeler <i.ruengeler [AT] fh-muenster.de>
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 <gtk/gtk.h>
30
31 #include "globals.h"
32 #include "epan/filesystem.h"
33 #include "simple_dialog.h"
34 #include "dlg_utils.h"
35 #include "gui_utils.h"
36 #include "main.h"
37 #include "compat_macros.h"
38
39 #include "sctp_stat.h"
40 /*#include "sctp_assoc_analyse.h"*/
41
42
43 static GtkWidget *sctp_error_dlg=NULL;
44 static GtkWidget *clist = NULL;
45 static GList *last_list = NULL;
46 static sctp_error_info_t* selected_packet = NULL;/* current selection */
47 /*static sctp_assoc_info_t* selected_assoc = NULL; */
48
49 #define NUM_COLS 3
50
51 typedef struct column_arrows {
52         GtkWidget *table;
53         GtkWidget *ascend_pm;
54         GtkWidget *descend_pm;
55 } column_arrows;
56
57
58 static void
59 dlg_destroy(void)
60 {
61         sctp_error_dlg=NULL;
62 }
63
64 static void add_to_clist(sctp_error_info_t* errinfo)
65 {
66         gint added_row, i;
67         gchar *data[NUM_COLS];
68         gchar field[NUM_COLS][30];
69
70         for (i=0; i<NUM_COLS; i++)
71                 data[i]=&field[i][0];
72
73                 /*printf("errinfo=%s\n",errinfo->chunk_info);*/
74
75         g_snprintf(field[0], 20, "%u", errinfo->frame_number);
76         g_snprintf(field[1], 20, "%s", errinfo->chunk_info);
77         g_snprintf(field[2], 20, "%s", errinfo->info_text);
78
79         added_row = gtk_clist_append(GTK_CLIST(clist), data);
80
81         /* set data pointer of last row to point to user data for that row */
82         gtk_clist_set_row_data(GTK_CLIST(clist), added_row, errinfo);
83 }
84
85 static void
86 sctp_error_on_unselect(GtkButton *button _U_, gpointer user_data _U_)
87 {
88         gtk_clist_unselect_all(GTK_CLIST(clist));
89 }
90
91 static void sctp_error_dlg_update(GList *list)
92 {
93         GList *ilist=NULL;
94
95         if (sctp_error_dlg != NULL) 
96         {
97                 gtk_clist_clear(GTK_CLIST(clist));
98                 ilist=list;
99
100                 while (ilist)
101                 {
102                         add_to_clist((sctp_error_info_t*)(ilist->data));
103                         ilist = g_list_next(ilist);
104                 }
105
106                 sctp_error_on_unselect(NULL, NULL);
107         }
108         last_list = ilist;
109 }
110
111 static void
112 sctp_error_on_select_row(GtkCList *clist, gint row,gint column _U_, GdkEventButton *event _U_, gpointer user_data _U_)
113 {
114         selected_packet = gtk_clist_get_row_data(GTK_CLIST(clist), row);
115 }
116
117
118
119 static void
120 sctp_error_on_frame (GtkButton *button _U_, gpointer user_data _U_)
121 {
122
123         if (selected_packet==NULL)
124                 return;
125
126         if (selected_packet)
127                 cf_goto_frame(&cfile, selected_packet->frame_number);
128 }
129
130
131 static void
132 sctp_error_on_close (GtkButton *button _U_, gpointer user_data _U_)
133 {
134         gtk_grab_remove(sctp_error_dlg);
135         gtk_widget_destroy(sctp_error_dlg);
136 }
137
138 static void
139 gtk_sctperror_dlg(void)
140 {
141         GtkWidget *sctp_error_dlg_w;
142         GtkWidget *vbox1;
143         GtkWidget *scrolledwindow1;
144         GtkWidget *hbuttonbox2;
145         GtkWidget *bt_unselect;
146         GtkWidget *bt_frame;
147         GtkWidget *bt_close;
148
149         const gchar *titles[NUM_COLS] =  {"Framenumber","Chunk Types", "Info"};
150         column_arrows *col_arrows;
151         GtkStyle *win_style;
152         GtkWidget *column_lb;
153         int i;
154
155         sctp_error_dlg_w = window_new (GTK_WINDOW_TOPLEVEL, "Wireshark: SCTP Associations");
156         gtk_window_set_position (GTK_WINDOW (sctp_error_dlg_w), GTK_WIN_POS_CENTER);
157         g_signal_connect(sctp_error_dlg_w, "destroy", G_CALLBACK(dlg_destroy), NULL);
158
159         /* Container for each row of widgets */
160         vbox1 = gtk_vbox_new(FALSE, 2);
161         gtk_container_border_width(GTK_CONTAINER(vbox1), 8);
162         gtk_container_add(GTK_CONTAINER(sctp_error_dlg_w), vbox1);
163         gtk_widget_show(vbox1);
164
165         scrolledwindow1 = scrolled_window_new (NULL, NULL);
166         gtk_widget_show (scrolledwindow1);
167         gtk_box_pack_start (GTK_BOX (vbox1), scrolledwindow1, TRUE, TRUE, 0);
168
169         clist = gtk_clist_new (NUM_COLS);
170         gtk_widget_show (clist);
171         gtk_container_add (GTK_CONTAINER (scrolledwindow1), clist);
172         gtk_widget_set_size_request(clist, 500, 200);
173
174         gtk_clist_set_column_width (GTK_CLIST (clist), 0, 100);
175         gtk_clist_set_column_width (GTK_CLIST (clist), 1, 200);
176         gtk_clist_set_column_width (GTK_CLIST (clist), 2, 200);
177
178         gtk_clist_set_column_justification(GTK_CLIST(clist), 0, GTK_JUSTIFY_CENTER);
179         gtk_clist_set_column_justification(GTK_CLIST(clist), 1, GTK_JUSTIFY_LEFT);
180         gtk_clist_set_column_justification(GTK_CLIST(clist), 2, GTK_JUSTIFY_LEFT);
181
182         gtk_clist_column_titles_show (GTK_CLIST (clist));
183
184         gtk_clist_set_sort_column(GTK_CLIST(clist), 0);
185         gtk_clist_set_sort_type(GTK_CLIST(clist), GTK_SORT_ASCENDING);
186
187         gtk_widget_show(sctp_error_dlg_w);
188
189         col_arrows = (column_arrows *) g_malloc(sizeof(column_arrows) * NUM_COLS);
190         win_style = gtk_widget_get_style(scrolledwindow1);
191
192         for (i=0; i<NUM_COLS; i++) {
193                 col_arrows[i].table = gtk_table_new(2, 2, FALSE);
194                 gtk_table_set_col_spacings(GTK_TABLE(col_arrows[i].table), 5);
195                 column_lb = gtk_label_new(titles[i]);
196                 gtk_table_attach(GTK_TABLE(col_arrows[i].table), column_lb, 0, 1, 0, 2, GTK_SHRINK, GTK_SHRINK, 0, 0);
197                 gtk_widget_show(column_lb);
198
199                 gtk_clist_set_column_widget(GTK_CLIST(clist), i, col_arrows[i].table);
200                 gtk_widget_show(col_arrows[i].table);
201         }
202
203
204         hbuttonbox2 = gtk_hbutton_box_new ();
205         gtk_widget_show (hbuttonbox2);
206         gtk_box_pack_start (GTK_BOX (vbox1), hbuttonbox2, FALSE, FALSE, 0);
207         gtk_button_box_set_spacing (GTK_BUTTON_BOX (hbuttonbox2), 5);
208         gtk_button_box_set_layout (GTK_BUTTON_BOX (hbuttonbox2), GTK_BUTTONBOX_SPREAD);
209
210
211         bt_unselect = gtk_button_new_with_label ("Unselect");
212         gtk_widget_show (bt_unselect);
213         gtk_container_add (GTK_CONTAINER (hbuttonbox2), bt_unselect);
214
215         bt_frame = gtk_button_new_with_label ("Go to Frame");
216         gtk_widget_show (bt_frame);
217         gtk_container_add (GTK_CONTAINER (hbuttonbox2), bt_frame);
218
219         bt_close = gtk_button_new_from_stock(GTK_STOCK_CLOSE);
220         gtk_widget_show (bt_close);
221         gtk_container_add (GTK_CONTAINER (hbuttonbox2), bt_close);
222
223         g_signal_connect(sctp_error_dlg_w, "destroy", G_CALLBACK(dlg_destroy), NULL);
224         g_signal_connect(clist, "select_row", G_CALLBACK(sctp_error_on_select_row), NULL);
225         g_signal_connect(bt_unselect, "clicked", G_CALLBACK(sctp_error_on_unselect), NULL);
226         g_signal_connect(bt_frame, "clicked", G_CALLBACK(sctp_error_on_frame), NULL);
227         g_signal_connect(bt_close, "clicked", G_CALLBACK(sctp_error_on_close), NULL);
228
229         sctp_error_dlg = sctp_error_dlg_w;
230 }
231
232
233 void sctp_error_dlg_show(sctp_assoc_info_t* assoc)
234 {
235         GList *list;
236
237         list =assoc->error_info_list;
238         if (list != NULL)
239         {
240                 if (sctp_error_dlg != NULL) {
241                         /* There's already a dialog box; reactivate it. */
242                         reactivate_window(sctp_error_dlg);
243                         /* Another list since last call? */
244                         if (list != last_list) {
245                                 sctp_error_dlg_update(list);
246                         }
247                 }
248                 else {
249                         /* Create and show the dialog box */
250                         gtk_sctperror_dlg();
251                         sctp_error_dlg_update(list);
252                 }
253         }
254         else
255                 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
256                     "No errors found!");
257 }
258