From Alejandro Vaquero
[obnox/wireshark/wip.git] / gtk / graph_analysis.c
1 /* graph_analysis.c
2  * Graphic Analysis addition for ethereal
3  *
4  * $Id$
5  *
6  * Copyright 2004, Verso Technologies Inc.
7  * By Alejandro Vaquero <alejandrovaquero@yahoo.com>
8  *
9  * based on rtp_analysis.c and io_stat
10  *
11  *
12  * Ethereal - Network traffic analyzer
13  * By Gerald Combs <gerald@ethereal.com>
14  * Copyright 1998 Gerald Combs
15  *
16  * This program is free software; you can redistribute it and/or
17  * modify it under the terms of the GNU General Public License
18  * as published by the Free Software Foundation; either version 2
19  * of the License, or (at your option) any later version.
20  *
21  * This program is distributed in the hope that it will be useful,
22  * but WITHOUT ANY WARRANTY; without even the implied warranty of
23  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24  * GNU General Public License for more details.
25  *
26  * You should have received a copy of the GNU General Public License
27  * along with this program; if not, write to the Free Software
28  * Foundation,  Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
29  */
30
31 #ifdef HAVE_CONFIG_H
32 #include "config.h"
33 #endif
34
35 #include "graph_analysis.h"
36
37 #include <epan/epan_dissect.h>
38
39 /* in /gtk ... */
40 #include <gtk/gtk.h>
41 #include <gdk/gdkkeysyms.h>
42 #include "gtkglobals.h"
43
44 #include "dlg_utils.h"
45 #include "ui_util.h"
46 #include "main.h"
47 #include "compat_macros.h"
48 #include "../color.h"
49
50 #include <string.h>
51
52 #ifdef HAVE_UNISTD_H
53 #include <unistd.h>
54 #endif
55
56 /****************************************************************************/
57
58
59 #define OK_TEXT "[ Ok ]"
60 #define PT_UNDEFINED -1
61
62
63 #if GTK_MAJOR_VERSION < 2
64 GtkRcStyle *rc_style;
65 GdkColormap *colormap;
66 #endif
67
68 /****************************************************************************/
69 /* Reset the user_data structure */
70 static void graph_analysis_reset(graph_analysis_data_t* user_data)
71 {
72         int i;
73
74         user_data->num_nodes = 0;
75         user_data->num_items = 0;
76         for (i=0; i<MAX_NUM_NODES; i++){
77                 user_data->nodes[i] = 0;
78         }
79         
80         user_data->dlg.first_node=0;
81         user_data->dlg.first_item=0;
82         user_data->dlg.left_x_border=0;
83         user_data->dlg.selected_item=0xFFFFFFFF;    /*not item selected */
84 }
85
86 /****************************************************************************/
87 /* Reset the user_data structure */
88 static void graph_analysis_init_dlg(graph_analysis_data_t* user_data)
89 {
90     /* init dialog_graph */
91     user_data->dlg.needs_redraw=TRUE;
92     user_data->dlg.draw_area=NULL;
93     user_data->dlg.pixmap=NULL;
94     user_data->dlg.h_scrollbar=NULL;
95     user_data->dlg.h_scrollbar_adjustment=NULL;
96     user_data->dlg.v_scrollbar=NULL;
97     user_data->dlg.v_scrollbar_adjustment=NULL;
98     user_data->dlg.pixmap_width=600;
99     user_data->dlg.pixmap_height=400;
100         user_data->dlg.first_node=0;
101         user_data->dlg.first_item=0;
102         user_data->dlg.left_x_border=0;
103         user_data->dlg.selected_item=0xFFFFFFFF;    /*not item selected */
104         user_data->dlg.window=NULL;
105 }
106
107 /****************************************************************************/
108 /* CALLBACKS */
109
110
111 /****************************************************************************/
112 /* close the dialog window and remove the tap listener */
113 static void on_destroy(GtkWidget *win _U_, graph_analysis_data_t *user_data _U_)
114 {
115
116         g_free(user_data);
117 }
118
119
120 /****************************************************************************/
121 static void dialog_graph_set_title(graph_analysis_data_t* user_data)
122 {
123         char            *title;
124         if (!user_data->dlg.window){
125                 return;
126         }
127         title = g_strdup_printf("Ale");
128
129         gtk_window_set_title(GTK_WINDOW(user_data->dlg.window), title);
130         g_free(title);  
131 }
132
133 #define RIGHT_ARROW 1
134 #define LEFT_ARROW 0
135 #define WIDTH_ARROW 8
136 #define HEIGHT_ARROW 6
137
138 /****************************************************************************/
139 static void draw_arrow(GdkDrawable *pixmap, GdkGC *gc, gint x, gint y, gboolean direction)
140 {
141         GdkPoint arrow_point[3];
142
143         arrow_point[0].x = x;
144         arrow_point[0].y = y-HEIGHT_ARROW/2;
145         if (direction == RIGHT_ARROW)
146                 arrow_point[1].x = x+WIDTH_ARROW;
147         else
148                 arrow_point[1].x = x-WIDTH_ARROW;
149         arrow_point[1].y = y;
150         arrow_point[2].x = x;
151         arrow_point[2].y = y+HEIGHT_ARROW/2;;
152
153         gdk_draw_polygon(pixmap, gc, TRUE, 
154                 arrow_point, 3);
155 }
156
157 #define MAX_LABEL 50
158 #define MAX_COMMENT 60
159 #define ITEM_HEIGHT 20
160 #define NODE_WIDTH 100
161 #define TOP_Y_BORDER 40
162 #define BOTTOM_Y_BORDER 0
163 #define COMMENT_WIDTH 250
164
165 /****************************************************************************/
166 static void dialog_graph_draw(graph_analysis_data_t* user_data)
167 {
168         guint32 i, last_item, first_item, last_node, first_node, display_items, display_nodes;
169                 guint32 start_arrow, end_arrow, label_x, src_port_x, dst_port_x, arrow_width;
170         guint32 current_item;
171         guint32 left_x_border;
172         guint32 right_x_border;
173         guint32 top_y_border;
174         guint32 bottom_y_border;
175                 graph_analysis_item_t *gai;
176                 gboolean display_label;
177
178 #if GTK_MAJOR_VERSION < 2
179         GdkFont *font;
180                 FONT_TYPE *big_font;
181                 FONT_TYPE *small_font;
182 #else
183         PangoLayout  *layout;
184         PangoLayout  *big_layout;
185         PangoLayout  *small_layout;
186 #endif
187         guint32 label_width, label_height;
188         guint32 draw_width, draw_height;
189         char label_string[MAX_COMMENT];
190                 GList* list;
191
192         /* new variables */
193
194 #if GTK_MAJOR_VERSION <2
195         font = user_data->dlg.draw_area->style->font;
196                 big_font = gdk_font_load("-adobe-helvetica-bold-r-normal--12-120-75-75-p-70-iso8859-1");
197                 small_font = gdk_font_load("-adobe-helvetica-bold-r-normal--10-120-75-75-p-70-iso8859-1");
198 #endif
199         if(!user_data->dlg.needs_redraw){
200                 return;
201         }
202         user_data->dlg.needs_redraw=FALSE;
203
204         /*
205          * Clear out old plot
206          */
207         gdk_draw_rectangle(user_data->dlg.pixmap,
208                            user_data->dlg.draw_area->style->white_gc,
209                            TRUE,
210                            0, 0,
211                            user_data->dlg.draw_area->allocation.width,
212                            user_data->dlg.draw_area->allocation.height);
213
214                 /* Calculate the y border */
215         top_y_border=TOP_Y_BORDER;      /* to display the node IP address */
216         bottom_y_border=BOTTOM_Y_BORDER;
217
218         draw_height=user_data->dlg.pixmap_height-top_y_border-bottom_y_border;
219
220                 first_item = user_data->dlg.first_item;
221                 display_items = draw_height/ITEM_HEIGHT;
222                 last_item = first_item+display_items-1;
223
224                 /* get the items to display and fill the matrix array */
225                 list = g_list_first(user_data->graph_info->list);
226                 current_item = 0;
227                 i = 0;
228                 while (list)
229                 {
230                         gai = list->data;
231                         if (gai->display){
232                                 if (current_item>=display_items) break;         /* the item is outside the display */
233                                 if (i>=first_item){
234                                         user_data->dlg.items[current_item].frame_num = gai->frame_num;
235                                         user_data->dlg.items[current_item].time = gai->time;
236                                         user_data->dlg.items[current_item].port_src = gai->port_src;
237                                         user_data->dlg.items[current_item].port_dst = gai->port_dst;
238                                         /* Add "..." if the length is 50 characters */
239                                         if (strlen(gai->frame_label) > 48) {
240                                                 gai->frame_label[48] = '.';
241                                                 gai->frame_label[47] = '.';
242                                                 gai->frame_label[46] = '.';
243                                         }
244                                         user_data->dlg.items[current_item].frame_label = gai->frame_label;
245                                         user_data->dlg.items[current_item].comment = gai->comment;
246                                         user_data->dlg.items[current_item].conv_num = gai->conv_num;
247                                         user_data->dlg.items[current_item].src_node = gai->src_node;
248                                         user_data->dlg.items[current_item].dst_node = gai->dst_node;
249                                         user_data->dlg.items[current_item].line_style = gai->line_style;
250                                         current_item++;
251                                 }
252                                 i++;
253                         }
254
255                         list = g_list_next(list);
256                 }
257                 /* in case the windows is resized so we have to move the top item */
258                 if ((first_item + display_items) > user_data->num_items){
259                         if (display_items>user_data->num_items)
260                                 first_item=0;
261                         else
262                                 first_item = user_data->num_items - display_items;
263                 }
264                 
265                 /* in case there are less items than possible displayed */
266                 display_items = current_item;
267                 last_item = first_item+display_items-1;
268
269                 /* if not items to display */
270                 if (display_items == 0) return;                         
271
272
273                 /* Calculate the x borders */
274                 /* We use time from the last display item to calcultate the x left border */
275                 g_snprintf(label_string, MAX_LABEL, "%.3f", user_data->dlg.items[display_items-1].time);
276 #if GTK_MAJOR_VERSION < 2
277         label_width=gdk_string_width(font, label_string);
278         label_height=gdk_string_height(font, label_string);
279 #else
280         layout = gtk_widget_create_pango_layout(user_data->dlg.draw_area, label_string);
281         big_layout = gtk_widget_create_pango_layout(user_data->dlg.draw_area, label_string);
282         small_layout = gtk_widget_create_pango_layout(user_data->dlg.draw_area, label_string);
283
284                 pango_layout_set_font_description(big_layout, pango_font_description_from_string("Helvetica-Bold 8"));
285                 pango_layout_set_font_description(small_layout, pango_font_description_from_string("Helvetica-Bold 7"));
286
287         pango_layout_get_pixel_size(layout, &label_width, &label_height);
288 #endif
289         left_x_border=label_width+10;
290                 user_data->dlg.left_x_border = left_x_border;
291
292         right_x_border=COMMENT_WIDTH;
293
294                 /* Calculate the number of nodes to display */
295         draw_width=user_data->dlg.pixmap_width-right_x_border-left_x_border;
296                 display_nodes = draw_width/NODE_WIDTH;
297                 first_node = user_data->dlg.first_node;
298
299                 /* in case the windows is resized so we have to move the left node */
300                 if ((first_node + display_nodes) > user_data->num_nodes){
301                         if (display_nodes>user_data->num_nodes) 
302                                 first_node=0;
303                         else
304                                 first_node=user_data->num_nodes - display_nodes;
305                 }
306
307                 /* in case there are less nodes than possible displayed */
308                 if (display_nodes>user_data->num_nodes) display_nodes=user_data->num_nodes;
309
310                 last_node = first_node + display_nodes-1;
311
312                 /* Paint the background items */ 
313                 for (current_item=0; current_item<display_items; current_item++){
314                         /* Paint background */
315                 gdk_draw_rectangle(user_data->dlg.pixmap,
316                            user_data->dlg.bg_gc[user_data->dlg.items[current_item].conv_num%MAX_NUM_COL_CONV],
317                            TRUE,
318                            left_x_border, 
319                                                    top_y_border+current_item*ITEM_HEIGHT,
320                            draw_width,
321                            ITEM_HEIGHT);
322                 }
323
324
325                 /* Draw the node names on top and the division lines */
326                 for (i=0; i<display_nodes; i++){
327                         /* draw the node IPs */
328                         g_snprintf(label_string, MAX_LABEL, "%s",
329                                 ip_to_str((guint8 *)&(user_data->nodes[i+first_node])));
330 #if GTK_MAJOR_VERSION < 2
331                 label_width=gdk_string_width(font, label_string);
332                 label_height=gdk_string_height(font, label_string);
333                         gdk_draw_string(user_data->dlg.pixmap,
334                 font,
335                 user_data->dlg.draw_area->style->black_gc,
336                 left_x_border+NODE_WIDTH/2-label_width/2+NODE_WIDTH*i,
337                 top_y_border/2-label_height/2,
338                 label_string);
339 #else
340                         pango_layout_set_text(layout, label_string, -1);
341                 pango_layout_get_pixel_size(layout, &label_width, &label_height);
342                 gdk_draw_layout(user_data->dlg.pixmap,
343                 user_data->dlg.draw_area->style->black_gc,
344                 left_x_border+NODE_WIDTH/2-label_width/2+NODE_WIDTH*i,
345                 top_y_border/2-label_height/2,
346                 layout);
347 #endif          
348
349                         /* draw the node division lines */
350                         gdk_draw_line(user_data->dlg.pixmap, user_data->dlg.div_line_gc,
351                                 left_x_border+NODE_WIDTH/2+NODE_WIDTH*i,
352                                 top_y_border,
353                                 left_x_border+NODE_WIDTH/2+NODE_WIDTH*i,
354                                 user_data->dlg.pixmap_height-bottom_y_border);
355
356                 }
357
358                 /*
359                  * Draw the items 
360                  */
361
362
363                 for (current_item=0; current_item<display_items; current_item++){
364                         /* draw the time */
365                         g_snprintf(label_string, MAX_LABEL, "%.3f", user_data->dlg.items[current_item].time);
366 #if GTK_MAJOR_VERSION < 2
367                 label_width=gdk_string_width(font, label_string);
368                 label_height=gdk_string_height(font, label_string);
369                         gdk_draw_string(user_data->dlg.pixmap,
370                 font,
371                 user_data->dlg.draw_area->style->black_gc,
372                 left_x_border-label_width-4,
373                 top_y_border+current_item*ITEM_HEIGHT+ITEM_HEIGHT/2+label_height/4,
374                 label_string);
375 #else
376                         pango_layout_set_text(layout, label_string, -1);
377                 pango_layout_get_pixel_size(layout, &label_width, &label_height);
378                 gdk_draw_layout(user_data->dlg.pixmap,
379                 user_data->dlg.draw_area->style->black_gc,
380                 left_x_border-label_width-4,
381                 top_y_border+current_item*ITEM_HEIGHT+ITEM_HEIGHT/2-label_height/2,
382                 layout);
383 #endif
384
385                         /*draw the comments */
386                         g_snprintf(label_string, MAX_COMMENT, "%s", user_data->dlg.items[current_item].comment);
387 #if GTK_MAJOR_VERSION < 2
388                         label_width=gdk_string_width(small_font, label_string);
389                         label_height=gdk_string_height(small_font, label_string);
390                         gdk_draw_string(user_data->dlg.pixmap,
391                 small_font,
392                 user_data->dlg.draw_area->style->black_gc,
393                 user_data->dlg.pixmap_width-right_x_border+3,
394                 top_y_border+current_item*ITEM_HEIGHT+ITEM_HEIGHT/2+label_height/4,
395                 label_string);
396 #else
397                         pango_layout_set_text(small_layout, label_string, -1);
398                         pango_layout_get_pixel_size(small_layout, &label_width, &label_height);
399                 gdk_draw_layout(user_data->dlg.pixmap,
400                 user_data->dlg.draw_area->style->black_gc,
401                 user_data->dlg.pixmap_width-right_x_border+3,
402                 top_y_border+current_item*ITEM_HEIGHT+ITEM_HEIGHT/2-label_height/2,
403                 small_layout);
404 #endif
405                         
406                         /* draw the arrow an frame label*/
407                         display_label = FALSE;
408                         if (user_data->dlg.items[current_item].src_node>=first_node){
409                                 if (user_data->dlg.items[current_item].src_node<=last_node){
410                                         start_arrow = left_x_border+(user_data->dlg.items[current_item].src_node-first_node)*NODE_WIDTH+NODE_WIDTH/2;
411                                         display_label = TRUE;
412                                 } else {
413                                         start_arrow = user_data->dlg.pixmap_width - right_x_border;
414                                 }
415                         } else {
416                                 start_arrow = left_x_border;
417                         }
418
419                         if (user_data->dlg.items[current_item].dst_node>=first_node){
420                                 if (user_data->dlg.items[current_item].dst_node<=last_node){
421                                         end_arrow = left_x_border+(user_data->dlg.items[current_item].dst_node-first_node)*NODE_WIDTH+NODE_WIDTH/2;
422                                         display_label = TRUE;
423                                 } else {
424                                         end_arrow = user_data->dlg.pixmap_width - right_x_border;
425                                 }
426                         } else {
427                                 end_arrow = left_x_border;
428                         }
429
430                         if (start_arrow != end_arrow){
431                                 /* draw the arrow line */
432                                 gdk_draw_line(user_data->dlg.pixmap, user_data->dlg.draw_area->style->black_gc,
433                                         start_arrow,
434                                         top_y_border+current_item*ITEM_HEIGHT+ITEM_HEIGHT-7,
435                                         end_arrow,
436                                         top_y_border+current_item*ITEM_HEIGHT+ITEM_HEIGHT-7);
437
438                                 /* draw the additional line when line style is 2 pixels width */
439                                 if (user_data->dlg.items[current_item].line_style == 2){
440                                         gdk_draw_line(user_data->dlg.pixmap, user_data->dlg.draw_area->style->black_gc,
441                                                 start_arrow,
442                                                 top_y_border+current_item*ITEM_HEIGHT+ITEM_HEIGHT-6,
443                                                 end_arrow,
444                                                 top_y_border+current_item*ITEM_HEIGHT+ITEM_HEIGHT-6);
445                                 }                                       
446
447                                 /* draw the arrow */
448                                 if (start_arrow<end_arrow)
449                                         draw_arrow(user_data->dlg.pixmap, user_data->dlg.draw_area->style->black_gc, end_arrow-WIDTH_ARROW,top_y_border+current_item*ITEM_HEIGHT+ITEM_HEIGHT-7, RIGHT_ARROW);
450                                 else
451                                         draw_arrow(user_data->dlg.pixmap, user_data->dlg.draw_area->style->black_gc, end_arrow+WIDTH_ARROW,top_y_border+current_item*ITEM_HEIGHT+ITEM_HEIGHT-7, LEFT_ARROW);
452                         }
453
454                         /* draw the frame comment */
455                         if (display_label){
456                                 g_snprintf(label_string, MAX_LABEL, "%s", user_data->dlg.items[current_item].frame_label);
457 #if GTK_MAJOR_VERSION < 2
458                                 label_width=gdk_string_width(big_font, label_string);
459                                 label_height=gdk_string_height(big_font, label_string);
460 #else
461                                 pango_layout_set_text(big_layout, label_string, -1);
462                                 pango_layout_get_pixel_size(big_layout, &label_width, &label_height);
463 #endif
464
465                                 if (start_arrow<end_arrow){
466                                         arrow_width = end_arrow-start_arrow;
467                                         label_x = arrow_width/2+start_arrow;
468                                 }
469                                 else {
470                                         arrow_width = start_arrow-end_arrow;
471                                         label_x = arrow_width/2+end_arrow;
472                                 }
473
474                                 if (label_width>arrow_width) arrow_width = label_width;
475
476                                 if (left_x_border > (label_x-label_width/2)) label_x = left_x_border + label_width/2;
477
478                                 if ((user_data->dlg.pixmap_width - right_x_border) < (label_x+label_width/2)) label_x = user_data->dlg.pixmap_width - right_x_border - label_width/2;
479
480 #if GTK_MAJOR_VERSION < 2
481                                 gdk_draw_string(user_data->dlg.pixmap,
482                                         big_font,
483                                         user_data->dlg.draw_area->style->black_gc,
484                                         label_x - label_width/2,
485                                         top_y_border+current_item*ITEM_HEIGHT+ITEM_HEIGHT/2+label_height/4-3,
486                                         label_string);
487 #else
488                                 gdk_draw_layout(user_data->dlg.pixmap,
489                                         user_data->dlg.draw_area->style->black_gc,
490                                         label_x - label_width/2,
491                                         top_y_border+current_item*ITEM_HEIGHT+ITEM_HEIGHT/2-label_height/2-3,
492                                         big_layout);
493 #endif
494
495                                 /* draw the source port number */
496                                 if ((start_arrow != left_x_border) && (start_arrow != (user_data->dlg.pixmap_width - right_x_border))){ 
497                                         g_snprintf(label_string, MAX_LABEL, "(%i)", user_data->dlg.items[current_item].port_src);
498 #if GTK_MAJOR_VERSION < 2
499                                         label_width=gdk_string_width(small_font, label_string);
500                                         label_height=gdk_string_height(small_font, label_string);
501 #else
502                                         pango_layout_set_text(small_layout, label_string, -1);
503                                         pango_layout_get_pixel_size(small_layout, &label_width, &label_height);
504 #endif
505                                         if (start_arrow<end_arrow){
506                                                 src_port_x = start_arrow - label_width - 2;
507                                         }
508                                         else {
509                                                 src_port_x = start_arrow + 2;
510                                         }
511 #if GTK_MAJOR_VERSION < 2
512                                         gdk_draw_string(user_data->dlg.pixmap,
513                                                 small_font,
514                                                 user_data->dlg.div_line_gc,
515                                                 src_port_x,
516                                                 top_y_border+current_item*ITEM_HEIGHT+ITEM_HEIGHT-2+label_height/4-2,
517                                                 label_string);
518 #else
519                                         gdk_draw_layout(user_data->dlg.pixmap,
520                                                 user_data->dlg.div_line_gc,
521                                                 src_port_x,
522                                                 top_y_border+current_item*ITEM_HEIGHT+ITEM_HEIGHT-2-label_height/2-2,
523                                                 small_layout);
524 #endif
525                                 }
526
527                                 /* draw the destination port number */
528                                 if ((end_arrow != left_x_border) && (end_arrow != (user_data->dlg.pixmap_width - right_x_border))){ 
529                                         g_snprintf(label_string, MAX_LABEL, "(%i)", user_data->dlg.items[current_item].port_dst);
530 #if GTK_MAJOR_VERSION < 2
531                                         label_width=gdk_string_width(small_font, label_string);
532                                         label_height=gdk_string_height(small_font, label_string);
533 #else
534                                         pango_layout_set_text(small_layout, label_string, -1);
535                                         pango_layout_get_pixel_size(small_layout, &label_width, &label_height);
536 #endif
537                                         if (start_arrow<end_arrow){
538                                                 dst_port_x = end_arrow + 2;
539                                         }
540                                         else {
541                                                 dst_port_x = end_arrow - label_width - 2;
542                                         }
543 #if GTK_MAJOR_VERSION < 2
544                                         gdk_draw_string(user_data->dlg.pixmap,
545                                                 small_font,
546                                                 user_data->dlg.div_line_gc,
547                                                 dst_port_x,
548                                                 top_y_border+current_item*ITEM_HEIGHT+ITEM_HEIGHT-2+label_height/4-2,
549                                                 label_string);
550 #else
551                                         gdk_draw_layout(user_data->dlg.pixmap,
552                                                 user_data->dlg.div_line_gc,
553                                                 dst_port_x,
554                                                 top_y_border+current_item*ITEM_HEIGHT+ITEM_HEIGHT-2-label_height/2-2,
555                                                 small_layout);
556 #endif
557                                 }
558
559                         }
560                 }
561
562 #if GTK_MAJOR_VERSION >= 2
563         g_object_unref(G_OBJECT(layout));
564 #endif
565
566                 /* draw the border on the selected item */
567                 if ( (user_data->dlg.selected_item != 0xFFFFFFFF) && ( (user_data->dlg.selected_item>=first_item) && (user_data->dlg.selected_item<=last_item) )){
568                         gdk_draw_rectangle(user_data->dlg.pixmap, user_data->dlg.draw_area->style->black_gc,
569                                 FALSE,
570                                 left_x_border-1,
571                                 (user_data->dlg.selected_item-first_item)*ITEM_HEIGHT+TOP_Y_BORDER,
572                                 user_data->dlg.pixmap_width-COMMENT_WIDTH-left_x_border+1,
573                                 ITEM_HEIGHT);
574                 }
575
576
577
578         gdk_draw_pixmap(user_data->dlg.draw_area->window,
579                         user_data->dlg.draw_area->style->fg_gc[GTK_WIDGET_STATE(user_data->dlg.draw_area)],
580                         user_data->dlg.pixmap,
581                         0, 0,
582                         0, 0,
583                         user_data->dlg.pixmap_width, user_data->dlg.pixmap_height);
584
585
586         /* update the h_scrollbar */
587         user_data->dlg.h_scrollbar_adjustment->upper=(gfloat) user_data->num_nodes-1;
588         user_data->dlg.h_scrollbar_adjustment->step_increment=1;
589         user_data->dlg.h_scrollbar_adjustment->page_increment=(gfloat) (last_node-first_node);
590         user_data->dlg.h_scrollbar_adjustment->page_size=(gfloat) (last_node-first_node);
591         user_data->dlg.h_scrollbar_adjustment->value=(gfloat) first_node;
592
593                 gtk_adjustment_changed(user_data->dlg.h_scrollbar_adjustment);
594         gtk_adjustment_value_changed(user_data->dlg.h_scrollbar_adjustment);
595
596         /* update the v_scrollbar */
597         user_data->dlg.v_scrollbar_adjustment->upper=(gfloat) user_data->num_items-1;
598         user_data->dlg.v_scrollbar_adjustment->step_increment=1;
599         user_data->dlg.v_scrollbar_adjustment->page_increment=(gfloat) (last_item-first_item);
600         user_data->dlg.v_scrollbar_adjustment->page_size=(gfloat) (last_item-first_item);
601         user_data->dlg.v_scrollbar_adjustment->value=(gfloat) first_item;
602
603                 gtk_adjustment_changed(user_data->dlg.v_scrollbar_adjustment);
604         gtk_adjustment_value_changed(user_data->dlg.v_scrollbar_adjustment);
605
606 }
607
608 /****************************************************************************/
609 static void dialog_graph_redraw(graph_analysis_data_t* user_data)
610 {
611         user_data->dlg.needs_redraw=TRUE;
612         dialog_graph_draw(user_data); 
613 }
614
615 /****************************************************************************/
616 static gint quit(GtkWidget *widget, GdkEventExpose *event _U_)
617 {
618         graph_analysis_data_t *user_data;
619
620         user_data=(graph_analysis_data_t *)OBJECT_GET_DATA(widget, "graph_analysis_data_t");
621
622                 user_data->dlg.window = NULL;
623
624                 user_data = NULL;
625         return TRUE;
626 }
627
628 /****************************************************************************/
629 static gint button_press_event(GtkWidget *widget, GdkEventButton *event _U_)
630 {
631         graph_analysis_data_t *user_data;
632                 guint32 item;
633
634         user_data=(graph_analysis_data_t *)OBJECT_GET_DATA(widget, "graph_analysis_data_t");
635
636                 if (event->type != GDK_BUTTON_PRESS) return TRUE;
637
638                 if (event->y<TOP_Y_BORDER) return TRUE;
639
640                 /* get the item clicked */
641                 item = ((guint32)event->y - TOP_Y_BORDER) / ITEM_HEIGHT;
642                 user_data->dlg.selected_item = item + user_data->dlg.first_item;
643
644                 user_data->dlg.needs_redraw=TRUE;
645                 dialog_graph_draw(user_data);
646
647                 cf_goto_frame(&cfile, user_data->dlg.items[item].frame_num);
648
649         return TRUE;
650 }
651
652 /****************************************************************************/
653 static gint scroll_event(GtkWidget *widget, GdkEventButton *event _U_)
654 {
655         graph_analysis_data_t *user_data;
656         
657         user_data=(graph_analysis_data_t *)OBJECT_GET_DATA(widget, "graph_analysis_data_t");
658         
659         /* Up scroll */
660         if (event->state == 0){
661                 if (user_data->dlg.first_item == 0) return TRUE;
662                 user_data->dlg.first_item--;
663                 
664                 /* Down scroll */
665         } else {
666                 if ((user_data->dlg.first_item+user_data->dlg.v_scrollbar_adjustment->page_size+1 == user_data->num_items)) return TRUE;
667                 user_data->dlg.first_item++;
668         }
669         dialog_graph_redraw(user_data);
670         
671         return TRUE;
672 }
673
674 /****************************************************************************/
675 static gint key_press_event(GtkWidget *widget, GdkEventKey *event _U_)
676 {
677         graph_analysis_data_t *user_data;
678         
679         user_data=(graph_analysis_data_t *)OBJECT_GET_DATA(widget, "graph_analysis_data_t");
680         
681         /* Up arrow */
682         if (event->keyval == GDK_Up){
683                 if (user_data->dlg.selected_item == 0) return TRUE;
684                 user_data->dlg.selected_item--;
685                 if ( (user_data->dlg.selected_item<user_data->dlg.first_item) || (user_data->dlg.selected_item>user_data->dlg.first_item+user_data->dlg.v_scrollbar_adjustment->page_size) )
686                         user_data->dlg.first_item = user_data->dlg.selected_item;
687                 /* Down arrow */
688         } else if (event->keyval == GDK_Down){
689                 if (user_data->dlg.selected_item == user_data->num_items-1) return TRUE;
690                 user_data->dlg.selected_item++;
691                 if ( (user_data->dlg.selected_item<user_data->dlg.first_item) || (user_data->dlg.selected_item>user_data->dlg.first_item+user_data->dlg.v_scrollbar_adjustment->page_size) )
692                         user_data->dlg.first_item = (guint32)user_data->dlg.selected_item-(guint32)user_data->dlg.v_scrollbar_adjustment->page_size;
693         } else if (event->keyval == GDK_Left){
694                 if (user_data->dlg.first_node == 0) return TRUE;
695                 user_data->dlg.first_node--;
696         } else if (event->keyval == GDK_Right){
697                 if ((user_data->dlg.first_node+user_data->dlg.h_scrollbar_adjustment->page_size+1 == user_data->num_nodes)) return TRUE;
698                 user_data->dlg.first_node++;
699         }
700         
701         user_data->dlg.needs_redraw=TRUE;
702         dialog_graph_draw(user_data);
703         
704         cf_goto_frame(&cfile, user_data->dlg.items[user_data->dlg.selected_item-user_data->dlg.first_item].frame_num);
705         
706         return TRUE;
707 }
708
709 /****************************************************************************/
710 static gint expose_event(GtkWidget *widget, GdkEventExpose *event)
711 {
712         graph_analysis_data_t *user_data;
713
714         user_data=(graph_analysis_data_t *)OBJECT_GET_DATA(widget, "graph_analysis_data_t");
715         if(!user_data){
716                 exit(10);
717         }
718
719
720         gdk_draw_pixmap(widget->window,
721                         widget->style->fg_gc[GTK_WIDGET_STATE(widget)],
722                         user_data->dlg.pixmap,
723                         event->area.x, event->area.y,
724                         event->area.x, event->area.y,
725                         event->area.width, event->area.height);
726
727         return FALSE;
728 }
729
730 static const GdkColor COLOR_GRAY = {0, 0x7fff, 0x7fff, 0x7fff};
731
732 /****************************************************************************/
733 static gint configure_event(GtkWidget *widget, GdkEventConfigure *event _U_)
734 {
735         graph_analysis_data_t *user_data;
736                 int i;
737                 GdkColor color_div_line = COLOR_GRAY;
738
739                 static GdkColor col[MAX_NUM_COL_CONV] = {
740                 {0,     0x00FF, 0xFFFF, 0x00FF},
741                 {0,     0xFFFF, 0xFFFF, 0x00FF},
742                 {0,     0xFFFF, 0x00FF, 0x00FF},
743                 {0,     0xFFFF, 0x00FF, 0xFFFF},
744                         {0,     0x00FF, 0x00FF, 0xFFFF},
745                         {0,     0x00FF, 0xFFFF, 0xFFFF},
746                         {0,     0xFFFF, 0x80FF, 0x00FF},
747                         {0,     0x80FF, 0x00FF, 0xFFFF},
748                         {0,     0x00FF, 0x80FF, 0xFFFF},
749                         {0,     0xFFFF, 0x00FF, 0x80FF}
750                 };
751
752         user_data=(graph_analysis_data_t *)OBJECT_GET_DATA(widget, "graph_analysis_data_t");
753
754         if(!user_data){
755                 exit(10);
756         }
757
758         if(user_data->dlg.pixmap){
759                 gdk_pixmap_unref(user_data->dlg.pixmap);
760                 user_data->dlg.pixmap=NULL;
761         }
762
763         user_data->dlg.pixmap=gdk_pixmap_new(widget->window,
764                         widget->allocation.width,
765                         widget->allocation.height,
766                         -1);
767         user_data->dlg.pixmap_width=widget->allocation.width;
768         user_data->dlg.pixmap_height=widget->allocation.height;
769
770         gdk_draw_rectangle(user_data->dlg.pixmap,
771                         widget->style->white_gc,
772                         TRUE,
773                         0, 0,
774                         widget->allocation.width,
775                         widget->allocation.height);
776
777                 /* create gc for division lines and set the line stype to dash*/
778                 user_data->dlg.div_line_gc=gdk_gc_new(user_data->dlg.pixmap);
779                 gdk_gc_set_line_attributes(user_data->dlg.div_line_gc, 1, GDK_LINE_ON_OFF_DASH, 0, 0);
780 #if GTK_MAJOR_VERSION < 2
781         colormap = gtk_widget_get_colormap (widget);
782         if (!gdk_color_alloc (colormap, &color_div_line)){
783                      g_warning ("Couldn't allocate color");
784         }
785         gdk_gc_set_foreground(user_data->dlg.div_line_gc, &color_div_line);
786 #else
787         gdk_gc_set_rgb_fg_color(user_data->dlg.div_line_gc, &color_div_line);
788 #endif
789         
790                 /* create gcs for the background items */
791
792                 for (i=0; i<MAX_NUM_COL_CONV; i++){
793                         user_data->dlg.bg_gc[i]=gdk_gc_new(user_data->dlg.pixmap);
794 #if GTK_MAJOR_VERSION < 2
795                         colormap = gtk_widget_get_colormap (widget);
796                         if (!gdk_color_alloc (colormap, &col[i])){
797                                                  g_warning ("Couldn't allocate color");
798                         }
799                         gdk_gc_set_foreground(user_data->dlg.bg_gc[i], &col[i]);
800 #else
801                 gdk_gc_set_rgb_fg_color(user_data->dlg.bg_gc[i], &col[i]);
802 #endif
803                 }
804
805         dialog_graph_redraw(user_data);
806         return TRUE;
807 }
808
809 /****************************************************************************/
810 static gint h_scrollbar_changed(GtkWidget *widget _U_, gpointer data)
811 {
812     graph_analysis_data_t *user_data=(graph_analysis_data_t *)data;
813
814         if ((user_data->dlg.first_node+user_data->dlg.h_scrollbar_adjustment->page_size+1 == user_data->num_nodes) 
815                 && (user_data->dlg.h_scrollbar_adjustment->value >= user_data->dlg.first_node ))
816                 return TRUE;
817
818         if (user_data->dlg.first_node == (guint16) user_data->dlg.h_scrollbar_adjustment->value)
819                 return TRUE;
820
821     user_data->dlg.first_node = (guint16) user_data->dlg.h_scrollbar_adjustment->value;
822
823         dialog_graph_redraw(user_data);
824     return TRUE;
825 }
826
827 /****************************************************************************/
828 static gint v_scrollbar_changed(GtkWidget *widget _U_, gpointer data)
829 {
830     graph_analysis_data_t *user_data=(graph_analysis_data_t *)data;
831         if ((user_data->dlg.first_item+user_data->dlg.v_scrollbar_adjustment->page_size+1 == user_data->num_items) 
832                 && (user_data->dlg.v_scrollbar_adjustment->value >= user_data->dlg.first_item ))
833                 return TRUE;
834
835         if (user_data->dlg.first_item == user_data->dlg.v_scrollbar_adjustment->value)
836                 return TRUE;
837                 
838     user_data->dlg.first_item = (guint32) user_data->dlg.v_scrollbar_adjustment->value;
839
840         dialog_graph_redraw(user_data);
841     return TRUE;
842 }
843
844 /****************************************************************************/
845 static void create_draw_area(graph_analysis_data_t* user_data, GtkWidget *box)
846 {
847             GtkWidget *vbox;
848         GtkWidget *hbox;
849
850         hbox=gtk_hbox_new(FALSE, 0);
851         gtk_widget_show(hbox);
852
853         vbox=gtk_vbox_new(FALSE, 0);
854         gtk_widget_show(vbox);
855
856
857         user_data->dlg.draw_area=gtk_drawing_area_new();
858                 GTK_WIDGET_SET_FLAGS(user_data->dlg.draw_area, GTK_CAN_FOCUS);
859                 gtk_widget_grab_focus(user_data->dlg.draw_area);
860
861         SIGNAL_CONNECT(user_data->dlg.draw_area, "destroy", quit, user_data);
862         OBJECT_SET_DATA(user_data->dlg.draw_area, "graph_analysis_data_t", user_data);
863
864         WIDGET_SET_SIZE(user_data->dlg.draw_area, user_data->dlg.pixmap_width, user_data->dlg.pixmap_height);
865
866         /* signals needed to handle backing pixmap */
867         SIGNAL_CONNECT(user_data->dlg.draw_area, "expose_event", expose_event, NULL);
868         SIGNAL_CONNECT(user_data->dlg.draw_area, "configure_event", configure_event, user_data);
869
870                 gtk_widget_add_events (user_data->dlg.draw_area,
871                          GDK_BUTTON_PRESS_MASK);
872                 SIGNAL_CONNECT(user_data->dlg.draw_area, "button_press_event", button_press_event, user_data);
873                 SIGNAL_CONNECT(user_data->dlg.draw_area, "scroll_event",  scroll_event, user_data);
874                 SIGNAL_CONNECT(user_data->dlg.draw_area, "key_press_event",  key_press_event, user_data);
875                 
876         gtk_widget_show(user_data->dlg.draw_area);
877         gtk_box_pack_start(GTK_BOX(vbox), user_data->dlg.draw_area, TRUE, TRUE, 0);
878
879         /* create the associated h_scrollbar */
880         user_data->dlg.h_scrollbar_adjustment=(GtkAdjustment *)gtk_adjustment_new(0,0,0,0,0,0);
881         user_data->dlg.h_scrollbar=gtk_hscrollbar_new(user_data->dlg.h_scrollbar_adjustment);
882         gtk_widget_show(user_data->dlg.h_scrollbar);
883         gtk_box_pack_end(GTK_BOX(vbox), user_data->dlg.h_scrollbar, FALSE, FALSE, 0);
884         SIGNAL_CONNECT(user_data->dlg.h_scrollbar_adjustment, "value_changed", h_scrollbar_changed, user_data);
885
886         gtk_box_pack_start(GTK_BOX(hbox), vbox, TRUE, TRUE, 0);
887
888        /* create the associated v_scrollbar */
889         user_data->dlg.v_scrollbar_adjustment=(GtkAdjustment *)gtk_adjustment_new(0,0,0,0,0,0);
890         user_data->dlg.v_scrollbar=gtk_vscrollbar_new(user_data->dlg.v_scrollbar_adjustment);
891         gtk_widget_show(user_data->dlg.v_scrollbar);
892         gtk_box_pack_end(GTK_BOX(hbox), user_data->dlg.v_scrollbar, FALSE, FALSE, 0);
893                 SIGNAL_CONNECT(user_data->dlg.v_scrollbar_adjustment, "value_changed", v_scrollbar_changed, user_data);
894
895         gtk_box_pack_start(GTK_BOX(box), hbox, TRUE, TRUE, 0);
896 }
897
898
899 /****************************************************************************/
900 static void dialog_graph_create_window(graph_analysis_data_t* user_data)
901 {
902         GtkWidget *vbox;
903         GtkWidget *hbox;
904         GtkWidget *bt_close;
905
906         /* create the main window */
907         user_data->dlg.window=window_new(GTK_WINDOW_TOPLEVEL, "Graph Analysis");
908
909
910         vbox=gtk_vbox_new(FALSE, 0);
911         gtk_container_add(GTK_CONTAINER(user_data->dlg.window), vbox);
912         gtk_widget_show(vbox);
913
914         create_draw_area(user_data, vbox);
915
916         hbox = dlg_button_row_new(GTK_STOCK_CLOSE, NULL);
917         gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
918         gtk_widget_show(hbox);
919
920         bt_close = OBJECT_GET_DATA(hbox, GTK_STOCK_CLOSE);
921         window_set_cancel_button(user_data->dlg.window, bt_close, window_cancel_button_cb);
922
923         SIGNAL_CONNECT(user_data->dlg.window, "delete_event", window_delete_event_cb, NULL);
924
925         gtk_widget_show(user_data->dlg.window);
926         window_present(user_data->dlg.window);
927 }
928
929 /* Return the index array if the node is in the array. Return -1 if there is room in the array
930  * and Return -2 if the array is full
931  */
932 /****************************************************************************/
933 gint is_node_array(graph_analysis_data_t* user_data, guint32 node)
934 {
935         int i;
936         for (i=0; i<MAX_NUM_NODES; i++){
937                 if (user_data->nodes[i] == 0)   return -1;      /* it is not in the array */
938                 if (user_data->nodes[i] == node) return i;      /* it is in the array */
939         }
940         return -2;              /* array full */
941 }
942
943
944 /* Get the nodes (IPs) from the list */
945 /****************************************************************************/
946 void get_nodes(graph_analysis_data_t* user_data)
947 {
948         GList* list;
949         graph_analysis_item_t *gai;
950         gint index;
951
952         /* fill the node array */
953         list = g_list_first(user_data->graph_info->list);
954         while (list)
955         {
956                 gai = list->data;
957                 if (gai->display){
958                         user_data->num_items++;
959                         /* check source IP node */
960                         index = is_node_array(user_data, gai->ip_src);
961                         switch(index){
962                                 case -2: /* array full */
963                                         gai->src_node = NODE_OVERFLOW;
964                                         break;
965                                 case -1: /* not in array */
966                                         user_data->nodes[user_data->num_nodes] = gai->ip_src;
967                                         gai->src_node = user_data->num_nodes;
968                                         user_data->num_nodes++;
969                                         break;
970                                 default: /* it is in the array, just update the src_node */
971                                         gai->src_node = (guint16)index;
972                         }
973
974                         /* check destination  IP node */
975                         index = is_node_array(user_data, gai->ip_dst);
976                         switch(index){
977                                 case -2: /* array full */
978                                         gai->dst_node = NODE_OVERFLOW;
979                                         break;
980                                 case -1: /* not in array */
981                                         user_data->nodes[user_data->num_nodes] = gai->ip_dst;
982                                         gai->dst_node = user_data->num_nodes;
983                                         user_data->num_nodes++;
984                                         break;
985                                 default: /* it is in the array, just update the dst_node */
986                                         gai->dst_node = (guint16)index;
987                         }
988                 }
989
990                 list = g_list_next(list);
991         }
992 }
993
994 /****************************************************************************/
995 /* XXX only handles IPv4, should add IPv6 support */
996 graph_analysis_data_t* graph_analysis_init(void)
997 {
998         graph_analysis_data_t* user_data;
999         /* init */
1000         user_data = g_malloc(sizeof(graph_analysis_data_t));
1001
1002         /* init user_data */
1003         graph_analysis_init_dlg(user_data);
1004
1005         return user_data;
1006 }
1007
1008 /****************************************************************************/
1009 void graph_analysis_create(graph_analysis_data_t* user_data)
1010 {
1011         /* reset the data */
1012         graph_analysis_reset(user_data);
1013
1014         /* get nodes (each node is an IP address) */
1015         get_nodes(user_data);
1016
1017         /* create the graph windows */
1018         dialog_graph_create_window(user_data);
1019
1020         /* redraw the graph */
1021         dialog_graph_redraw(user_data); 
1022
1023         return;
1024 }
1025
1026 /****************************************************************************/
1027 void graph_analysis_update(graph_analysis_data_t* user_data)
1028 {
1029         /* reset the data */
1030         graph_analysis_reset(user_data);
1031
1032         /* get nodes (each node is an IP address) */
1033         get_nodes(user_data);
1034
1035         /* redraw the graph */
1036         dialog_graph_redraw(user_data); 
1037
1038     window_present(user_data->dlg.window);
1039         return;
1040 }