From Jakub Zawadzki:
[obnox/wireshark/wip.git] / gtk / tcp_graph.c
1 /* tcp_graph.c
2  * TCP graph drawing code
3  * By Pavel Mores <pvl@uh.cz>
4  * Win32 port:  rwh@unifiedtech.com
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 <stdio.h>
32 #include <math.h>
33 #include <string.h>
34
35 #include <gtk/gtk.h>
36 #include <gdk/gdkkeysyms.h>
37
38 #include <epan/packet.h>
39 #include <epan/ipproto.h>
40 #include <epan/etypes.h>
41 #include <epan/ppptypes.h>
42 #include <epan/epan_dissect.h>
43 #include <epan/dissectors/packet-tcp.h>
44 #include <epan/address.h>
45 #include <epan/tap.h>
46
47 #include "../globals.h"
48 #include "../simple_dialog.h"
49 #include "../color.h"
50 #include "../stat_menu.h"
51
52 #include "gtk/gui_utils.h"
53 #include "gtk/dlg_utils.h"
54 #include "gtk/gui_stat_menu.h"
55
56
57 #define TH_FIN    0x01
58 #define TH_SYN    0x02
59 #define TH_RST    0x04
60 #define TH_PUSH   0x08
61 #define TH_ACK    0x10
62 #define TH_URG    0x20
63
64 #define TCP_SYN(flags)          ( flags & TH_SYN )
65 #define TCP_ACK(flags)          ( flags & TH_ACK )
66 #define TCP_FIN(flags)          ( flags & TH_FIN )
67
68 #define TXT_WIDTH       850
69 #define TXT_HEIGHT      550
70
71 /* for compare_headers() */
72 /* segment went the same direction as the currently selected one */
73 #define COMPARE_CURR_DIR        0
74 #define COMPARE_ANY_DIR         1
75
76 /* initalize_axis() */
77 #define AXIS_HORIZONTAL         0
78 #define AXIS_VERTICAL           1
79
80 #define WINDOW_TITLE_LENGTH 256
81
82 #define MOUSE_BUTTON_LEFT       1
83 #define MOUSE_BUTTON_MIDDLE     2
84 #define MOUSE_BUTTON_RIGHT      3
85
86 struct segment {
87         struct segment *next;
88         guint32 num;
89         guint32 rel_secs;
90         guint32 rel_usecs;
91         guint32 abs_secs;
92         guint32 abs_usecs;
93
94         guint32 th_seq;
95         guint32 th_ack;
96         guint8  th_flags;
97         guint32 th_win;   /* make it 32 bits so we can handle some scaling */
98         guint32 th_seglen;
99         guint16 th_sport;
100         guint16 th_dport;
101         address ip_src;
102         address ip_dst;
103 };
104
105 struct rect {
106         double x, y, width, height;
107 };
108
109 struct line {
110         double x1, y1, x2, y2;
111 };
112
113 struct irect {
114         int x, y, width, height;
115 };
116
117 struct ipoint {
118         int x, y;
119 };
120
121 typedef enum {
122         ELMT_NONE=0,
123         ELMT_RECT=1,
124         ELMT_LINE=2,
125         ELMT_ARC=3
126 } ElementType;
127
128 struct rect_params {
129         struct rect dim;
130         gint filled;
131 };
132
133 struct line_params {
134         struct line dim;
135 };
136
137 struct arc_params {
138         struct rect dim;
139         gint filled;
140         gint angle1, angle2;
141 };
142
143 struct element {
144         ElementType type;
145         GdkGC *gc;
146         struct segment *parent;
147         union {
148                 struct arc_params arc;
149                 struct rect_params rect;
150                 struct line_params line;
151         } p;
152 };
153
154 struct element_list {
155         struct element_list *next;
156         struct element *elements;
157 };
158
159 struct axis {
160         struct graph *g;                        /* which graph we belong to */
161         GtkWidget *drawing_area;
162         GdkPixmap *pixmap[2];
163         int displayed;
164 #define AXIS_ORIENTATION        1 << 0
165         int flags;
166         /* dim and orig (relative to origin of window) of axis' pixmap */
167         struct irect p;
168         /* dim and orig (relative to origin of axis' pixmap) of scale itself */
169         struct irect s;
170         gdouble min, max;
171         gdouble major, minor;           /* major and minor ticks */
172         const char **label;
173 };
174
175 #define HAXIS_INIT_HEIGHT       70
176 #define VAXIS_INIT_WIDTH        100
177 #define TITLEBAR_HEIGHT         50
178 #define RMARGIN_WIDTH   30
179
180 struct style_tseq_tcptrace {
181         GdkGC *gc_seq;
182         GdkGC *gc_ack[2];
183         int flags;
184 };
185
186 struct style_tseq_stevens {
187         int seq_width;
188         int seq_height;
189         int flags;
190 };
191
192 struct style_tput {
193         int width, height;
194         int nsegs;
195         int flags;
196 };
197
198 struct style_rtt {
199         int width, height;
200         int flags;
201 };
202
203 /* style flags */
204 #define SEQ_ORIGIN                      0x1
205 /* show absolute sequence numbers (not differences from isn) */
206 #define SEQ_ORIGIN_ZERO         0x1
207 #define SEQ_ORIGIN_ISN          0x0
208 #define TIME_ORIGIN                     0x10
209 /* show time from beginning of capture as opposed to time from beginning
210  * of the connection */
211 #define TIME_ORIGIN_CAP         0x10
212 #define TIME_ORIGIN_CONN        0x0
213
214 /* this is used by rtt module only */
215 struct unack {
216         struct unack *next;
217         double time;
218         unsigned int seqno;
219 };
220
221 struct cross {
222         int x, y;
223         int draw;                       /* indicates whether we should draw cross at all */
224         int erase_needed;
225         GtkToggleButton *on_toggle;
226         GtkToggleButton *off_toggle;
227 };
228
229 struct bounds {
230         double x0, y0, width, height;
231 };
232
233 struct zoom {
234         double x, y;
235 };
236
237 struct zooms {
238         double x, y;
239         double step_x, step_y;
240         struct zoom initial;
241 #define ZOOM_OUT                                (1 << 0)
242 #define ZOOM_HLOCK                              (1 << 1)
243 #define ZOOM_VLOCK                              (1 << 2)
244 #define ZOOM_STEPS_SAME                 (1 << 3)
245 #define ZOOM_STEPS_KEEP_RATIO   (1 << 4)
246         int flags;
247         /* unfortunately, we need them both because gtk_toggle_button_set_active ()
248          * with second argument FALSE doesn't do anything, somehow */
249         struct {
250                 GtkToggleButton *in_toggle;
251                 GtkToggleButton *out_toggle;
252                 GtkEntry *h_zoom;
253                 GtkEntry *v_zoom;
254                 GtkSpinButton *h_step;
255                 GtkSpinButton *v_step;
256         } widget;
257 };
258
259 struct grab {
260         int grabbed;
261         int x, y;
262 };
263
264 struct magnify {
265         int active;
266         int x, y;
267         struct ipoint offset;
268         int width, height;
269         struct zoom zoom;
270         struct graph *g;
271 #define MAGZOOMS_SAME           (1U << 0)
272 #define MAGZOOMS_SAME_RATIO     (1U << 1)
273 #define MAGZOOMS_IGNORE         (1U << 31)
274         guint flags;
275         struct {
276                 GtkSpinButton *h_zoom, *v_zoom;
277         } widget;
278 };
279
280 struct graph {
281         struct graph *next;
282 #define GRAPH_TSEQ_STEVENS      0
283 #define GRAPH_TSEQ_TCPTRACE     1
284 #define GRAPH_THROUGHPUT        2
285 #define GRAPH_RTT                       3
286         int type;
287 #define GRAPH_DESTROYED                         (1 << 0)
288 #define GRAPH_INIT_ON_TYPE_CHANGE       (1 << 1)
289         int flags;
290         GtkWidget *toplevel;    /* keypress handler needs this */
291         GtkWidget *drawing_area;
292         GtkWidget *text;        /* text widget for seg list - probably
293                                  * temporary */
294         PangoFontDescription *font;     /* font used for annotations etc. */
295         GdkGC *fg_gc;
296         GdkGC *bg_gc;
297         GdkPixmap *title_pixmap;
298         GdkPixmap *pixmap[2];
299         int displayed;                  /* which of both pixmaps is on screen right now */
300         struct {
301                 GtkWidget *control_panel;
302                 /* this belongs to style structs of graph types that make use of it */
303                 GtkToggleButton *time_orig_conn, *seq_orig_isn;
304         } gui;
305         const char **title;
306         /* Next 4 attribs describe the graph in natural units, before any scaling.
307          * For example, if we want to display graph of TCP conversation that
308          * started 112.309845 s after beginning of the capture and ran until
309          * 479.093582 s, 237019 B went through the connection (in one direction)
310          * starting with isn 31934022, then (bounds.x0, bounds.y0)=(112.309845,
311          * 31934022) and (bounds.width, bounds.height)=(366.783737, 237019). */
312         struct bounds bounds;
313         /* dimensions and position of the graph, both expressed already in pixels.
314          * x and y give the position of upper left corner of the graph relative
315          * to origin of the graph window, size is basically bounds*zoom */
316         struct irect geom;
317         /* viewport (=graph window area which is reserved for graph itself), its
318          * size and position relative to origin of the graph window */
319         struct irect wp;
320         struct grab grab;
321         /* If we need to display 237019 sequence numbers (=bytes) onto say 500
322          * pixels, we have to scale the graph down by factor of 0.002109. This
323          * number would be zoom.y. Obviously, both directions have separate zooms.*/
324         struct zooms zoom;
325         struct cross cross;
326         struct magnify magnify;
327         struct axis *x_axis, *y_axis;
328         struct segment *segments;
329         struct segment *current;
330         struct element_list *elists;            /* element lists */
331         union {
332                 struct style_tseq_stevens tseq_stevens;
333                 struct style_tseq_tcptrace tseq_tcptrace;
334                 struct style_tput tput;
335                 struct style_rtt rtt;
336         } s;
337         /* This allows keyboard to set the radio button */
338         struct {
339                 GtkToggleButton *graph_rtt, *graph_tput, *graph_tseqstevens, *graph_tseqttrace;
340         } gt;
341 };
342
343 static GdkGC *xor_gc = NULL;
344 static int refnum=0;
345
346 #define debug(section) if (debugging & section)
347 /* print function entry points */
348 #define DBS_FENTRY                      (1 << 0)
349 #define DBS_AXES_TICKS          (1 << 1)
350 #define DBS_AXES_DRAWING        (1 << 2)
351 #define DBS_GRAPH_DRAWING       (1 << 3)
352 #define DBS_TPUT_ELMTS          (1 << 4)
353 /*int debugging = DBS_FENTRY;*/
354 static int debugging = 0;
355 /*int debugging = DBS_AXES_TICKS;*/
356 /*int debugging = DBS_AXES_DRAWING;*/
357 /*int debugging = DBS_GRAPH_DRAWING;*/
358 /*int debugging = DBS_TPUT_ELMTS;*/
359
360 static void create_gui (struct graph * );
361 #if 0
362 static void create_text_widget (struct graph * );
363 static void display_text (struct graph * );
364 #endif
365 static void create_drawing_area (struct graph * );
366 static void control_panel_create (struct graph * );
367 static GtkWidget *control_panel_create_zoom_group (struct graph * );
368 static GtkWidget *control_panel_create_magnify_group (struct graph * );
369 static GtkWidget *control_panel_create_cross_group (struct graph * );
370 static GtkWidget *control_panel_create_zoomlock_group (struct graph * );
371 static GtkWidget *control_panel_create_graph_type_group (struct graph * );
372 static void control_panel_add_zoom_page (struct graph * , GtkWidget * );
373 static void control_panel_add_magnify_page (struct graph * , GtkWidget * );
374 static void control_panel_add_origin_page (struct graph * , GtkWidget * );
375 static void control_panel_add_cross_page (struct graph * , GtkWidget * );
376 static void control_panel_add_graph_type_page (struct graph * , GtkWidget * );
377 static void callback_toplevel_destroy (GtkWidget * , gpointer );
378 static gboolean callback_delete_event(GtkWidget * , GdkEvent * , gpointer);
379 static void callback_close (GtkWidget * , gpointer );
380 static void callback_time_origin (GtkWidget * , gpointer );
381 static void callback_seq_origin (GtkWidget * , gpointer );
382 static void callback_zoomlock_h (GtkWidget * , gpointer );
383 static void callback_zoomlock_v (GtkWidget * , gpointer );
384 static void callback_zoom_inout (GtkWidget * , gpointer );
385 static void callback_zoom_step (GtkWidget * , gpointer );
386 static void callback_zoom_flags (GtkWidget * , gpointer );
387 static void callback_cross_on_off (GtkWidget * , gpointer );
388 static void callback_mag_width (GtkWidget * , gpointer );
389 static void callback_mag_height (GtkWidget * , gpointer );
390 static void callback_mag_x (GtkWidget * , gpointer );
391 static void callback_mag_y (GtkWidget * , gpointer );
392 static void callback_mag_zoom (GtkWidget * , gpointer );
393 static void callback_mag_flags (GtkWidget * , gpointer );
394 static void callback_graph_type (GtkWidget * , gpointer );
395 static void callback_graph_init_on_typechg (GtkWidget * , gpointer );
396 static void callback_create_help (GtkWidget * , gpointer );
397 static void update_zoom_spins (struct graph * );
398 static struct tcpheader *select_tcpip_session (capture_file *, struct segment * );
399 static int compare_headers (address *saddr1, address *daddr1, guint16 sport1, guint16 dport1, address *saddr2, address *daddr2, guint16 sport2, guint16 dport2, int dir);
400 static int get_num_dsegs (struct graph * );
401 static int get_num_acks (struct graph * );
402 static void graph_type_dependent_initialize (struct graph * );
403 static struct graph *graph_new (void);
404 static void graph_destroy (struct graph * );
405 static void graph_initialize_values (struct graph * );
406 static void graph_init_sequence (struct graph * );
407 static void draw_element_line (struct graph * , struct element * );
408 static void draw_element_arc (struct graph * , struct element * );
409 static void graph_display (struct graph * );
410 static void graph_pixmaps_create (struct graph * );
411 static void graph_pixmaps_switch (struct graph * );
412 static void graph_pixmap_draw (struct graph * );
413 static void graph_pixmap_display (struct graph * );
414 static void graph_element_lists_make (struct graph * );
415 static void graph_element_lists_free (struct graph * );
416 static void graph_element_lists_initialize (struct graph * );
417 static void graph_title_pixmap_create (struct graph * );
418 static void graph_title_pixmap_draw (struct graph * );
419 static void graph_title_pixmap_display (struct graph * );
420 static void graph_segment_list_get (struct graph * );
421 static void graph_segment_list_free (struct graph * );
422 static void graph_select_segment (struct graph * , int , int );
423 static int line_detect_collision (struct element * , int , int );
424 static int arc_detect_collision (struct element * , int , int );
425 static void axis_pixmaps_create (struct axis * );
426 static void axis_pixmaps_switch (struct axis * );
427 static void axis_display (struct axis * );
428 static void v_axis_pixmap_draw (struct axis * );
429 static void h_axis_pixmap_draw (struct axis * );
430 static void axis_pixmap_display (struct axis * );
431 static void axis_compute_ticks (struct axis * , double , double , int );
432 static double axis_zoom_get (struct axis * , int );
433 static void axis_ticks_up (int * , int * );
434 static void axis_ticks_down (int * , int * );
435 static void axis_destroy (struct axis * );
436 static int get_label_dim (struct axis * , int , double );
437 static void toggle_crosshairs (struct graph *g);
438 static void toggle_time_origin (struct graph * );
439 static void toggle_seq_origin (struct graph * );
440 static void restore_initial_graph_view (struct graph *g);
441 static void cross_xor (struct graph * , int , int );
442 static void cross_draw (struct graph * , int , int );
443 static void cross_erase (struct graph * );
444 static void magnify_create (struct graph * , int , int );
445 static void magnify_move (struct graph * , int , int );
446 static void magnify_destroy (struct graph * );
447 static void magnify_draw (struct graph * );
448 static void magnify_get_geom (struct graph * , int , int );
449 static gint configure_event (GtkWidget * , GdkEventConfigure * );
450 static gint expose_event (GtkWidget * , GdkEventExpose * );
451 static gint button_press_event (GtkWidget * , GdkEventButton * );
452 static gint button_release_event (GtkWidget * , GdkEventButton * );
453 static gint motion_notify_event (GtkWidget * , GdkEventMotion * );
454 static gint key_press_event (GtkWidget * , GdkEventKey * );
455 static gint key_release_event (GtkWidget * , GdkEventKey * );
456 static gint leave_notify_event (GtkWidget * , GdkEventCrossing * );
457 static gint enter_notify_event (GtkWidget * , GdkEventCrossing * );
458 static void tseq_initialize (struct graph * );
459 static void tseq_get_bounds (struct graph * );
460 static void tseq_stevens_read_config (struct graph * );
461 static void tseq_stevens_make_elmtlist (struct graph * );
462 static void tseq_stevens_toggle_seq_origin (struct graph * );
463 static void tseq_stevens_toggle_time_origin (struct graph * );
464 static void tseq_tcptrace_read_config (struct graph * );
465 static void tseq_tcptrace_make_elmtlist (struct graph * );
466 static void tseq_tcptrace_toggle_seq_origin (struct graph * );
467 static void tseq_tcptrace_toggle_time_origin (struct graph * );
468 static void tput_initialize (struct graph * );
469 static void tput_read_config (struct graph * );
470 static void tput_make_elmtlist (struct graph * );
471 static void tput_toggle_time_origin (struct graph * );
472 static void rtt_read_config (struct graph * );
473 static void rtt_initialize (struct graph * );
474 static int rtt_is_retrans (struct unack * , unsigned int );
475 static struct unack *rtt_get_new_unack (double , unsigned int );
476 static void rtt_put_unack_on_list (struct unack ** , struct unack * );
477 static void rtt_delete_unack_from_list (struct unack ** , struct unack * );
478 static void rtt_make_elmtlist (struct graph * );
479 static void rtt_toggle_seq_origin (struct graph * );
480 #if defined(_WIN32) && !defined(__MINGW32__)
481 static int rint (double );      /* compiler template for Windows */
482 #endif
483
484 /* 
485  * Uncomment the following define to revert WIN32 to 
486  * use original mouse button controls 
487  */
488
489 /* #define ORIGINAL_WIN32_BUTTONS 1 */
490
491 /* XXX - what about OS X? */
492 static char helptext[] =
493         "Here's what you can do:\n"
494         "\n"
495 #ifdef ORIGINAL_WIN32_BUTTONS
496         "   <Ctrl>-Left Mouse Button            selects segment under cursor in Wireshark's packet list\n"
497         "\n"
498         "   Left Mouse Button                   zooms in (towards area under mouse pointer)\n"
499         "   <Shift>-Left Mouse Button           zooms out\n"
500         "\n"
501         "   Right Mouse Button                  moves the graph (if zoomed in)\n"
502         "   <Ctrl>-Right Mouse Button           displays a portion of graph under cursor magnified\n"
503 #else /* !ORIGINAL_WIN32_BUTTONS */
504         "   Left Mouse Button                   selects segment under cursor in Wireshark's packet list\n"
505         "\n"
506         "   Middle Mouse Button                 zooms in (towards area under cousor)\n"
507         "   <Shift>-Middle Mouse Button zooms out\n"
508         "\n"
509         "   Right Mouse Button                  moves the graph (if zoomed in)\n"
510         "   <Ctrl>-Right Mouse Button           displays a portion of graph under cursor magnified\n"
511 #endif
512         "\n"
513         "\n"
514         "   '1'                         display Round Trip Time Graph\n"
515         "   '2'                         display Throughput Graph\n"
516         "   '3'                         display Time/Sequence Graph (Stevens)\n"
517         "   '4'                         display Time/Sequence Graph (tcptrace)\n"
518         "\n"
519         "   <Space bar> toggles crosshairs on/off\n"
520         "\n"
521         "   'i' or '+'                  zoom in (towards area under mouse pointer)\n"
522         "   'o' or '-'                  zoom out\n"
523         "   'r' or <Home>       restore graph to initial state (zoom out max)\n"
524         "   's'                         toggles relative/absolute sequence numbers\n"
525         "   't'                         toggles time origin\n"
526         "   'g'                         go to frame under cursor in Wireshark's packet list (if possible)\n"
527         "\n"
528         "   <Left>                      move view left by 100 pixels (if zoomed in)\n"
529         "   <Right>             move view right 100 pixels (if zoomed in)\n"
530         "   <Up>                        move view up by 100 pixels (if zoomed in)\n"
531         "   <Down>              move view down by 100 pixels (if zoomed in)\n"
532         "\n"
533         "   <Shift><Left>       move view left by 10 pixels (if zoomed in)\n"
534         "   <Shift><Right>      move view right 10 pixels (if zoomed in)\n"
535         "   <Shift><Up> move view up by 10 pixels (if zoomed in)\n"
536         "   <Shift><Down>       move view down by 10 pixels (if zoomed in)\n"
537         "\n"
538         "   <Ctrl><Left>        move view left by 1 pixel (if zoomed in)\n"
539         "   <Ctrl><Right>       move view right 1 pixel (if zoomed in)\n"
540         "   <Ctrl><Up>  move view up by 1 pixel (if zoomed in)\n"
541         "   <Ctrl><Down>        move view down by 1 pixel (if zoomed in)\n"
542 ;
543
544 #if 0
545 static void debug_coord (struct graph *g, const char *c)
546 {
547         static unsigned count = 0;
548         
549         count++;
550         printf("%u: %s\n", count, c);
551         printf("%u:  g->geom.width %d\n", count, g->geom.width);
552         printf("%u: g->geom.height %d\n", count, g->geom.height);
553         printf("%u:      g->geom.x %d\n", count, g->geom.x);
554         printf("%u:      g->geom.y %d\n", count, g->geom.y);
555
556         printf("%u:    g->wp.width %d\n", count, g->wp.width);
557         printf("%u:   g->wp.height %d\n", count, g->wp.height);
558         printf("%u:        g->wp.x %d\n", count, g->wp.x);
559         printf("%u:        g->wp.y %d\n", count, g->wp.y);
560         printf("---------------\n");
561 }
562 #endif
563
564 static void set_busy_cursor(GdkWindow *w)
565 {
566         GdkCursor* cursor;
567
568         cursor = gdk_cursor_new(GDK_WATCH);
569         gdk_window_set_cursor(w, cursor);
570         gdk_flush(); 
571         gdk_cursor_destroy(cursor);
572 }
573
574 static void unset_busy_cursor(GdkWindow *w)
575 {
576         gdk_window_set_cursor(w, NULL);
577         gdk_flush(); 
578 }
579
580 static void tcp_graph_cb (GtkWidget *w _U_, gpointer data, guint callback_action /*graph_type*/ _U_)
581 {
582         struct segment current;
583         struct graph *g;
584
585         guint graph_type = GPOINTER_TO_INT(data);
586
587         debug(DBS_FENTRY) puts ("tcp_graph_cb()");
588
589         if (! (g = graph_new()))
590                 return;
591
592         refnum++;
593         graph_initialize_values (g);
594
595         g->type = graph_type;
596         if (!select_tcpip_session (&cfile, &current)) {
597                 return;
598         }
599
600         graph_segment_list_get(g);
601         create_gui(g);
602         /* display_text(g); */
603         graph_init_sequence(g);
604 }
605
606 static void create_gui (struct graph *g)
607 {
608         debug(DBS_FENTRY) puts ("create_gui()");
609         /* create_text_widget(g); */
610         control_panel_create (g);
611         create_drawing_area(g);
612 }
613
614
615
616 static void create_drawing_area (struct graph *g)
617 {
618         GdkColormap *colormap;
619         GdkColor color;
620         char window_title[WINDOW_TITLE_LENGTH];
621         struct segment current;
622         struct tcpheader *thdr;
623
624         debug(DBS_FENTRY) puts ("create_drawing_area()");
625 #if 0
626         g->font = gdk_font_load ("-sony-fixed-medium-r-normal--16-150-75-75"
627                                                         "-c-80-iso8859-2");
628         g->font = gdk_font_load ("-biznet-fotinostypewriter-medium-r-normal-*-*-120"
629                                                         "-*-*-m-*-iso8859-2");
630 #endif
631         thdr=select_tcpip_session (&cfile, &current);
632         g_snprintf (window_title, WINDOW_TITLE_LENGTH, "TCP Graph %d: %s %s:%d -> %s:%d",
633                         refnum,
634                         cf_get_display_name(&cfile),
635                         ep_address_to_str(&(thdr->ip_src)),
636                         thdr->th_sport,
637                         ep_address_to_str(&(thdr->ip_dst)),
638                         thdr->th_dport
639         );
640         g->toplevel = dlg_window_new ("Tcp Graph");
641         gtk_window_set_title(GTK_WINDOW(g->toplevel), window_title);
642         gtk_widget_set_name (g->toplevel, "Test Graph");
643         g_object_set_data(G_OBJECT(g->toplevel), "graph", g);
644
645         /* Create the drawing area */
646         g->drawing_area = gtk_drawing_area_new ();
647         g_object_set_data(G_OBJECT(g->drawing_area), "graph", g);
648         g->x_axis->drawing_area = g->y_axis->drawing_area = g->drawing_area;
649         gtk_widget_set_size_request (g->drawing_area,
650                                         g->wp.width + g->wp.x + RMARGIN_WIDTH,
651                                         g->wp.height + g->wp.y + g->x_axis->s.height);
652         gtk_widget_show (g->drawing_area);
653
654         g_signal_connect(g->drawing_area, "expose_event", G_CALLBACK(expose_event), NULL);
655         /* this has to be done later, after the widget has been shown */
656         /*
657         g_signal_connect(g->drawing_area,"configure_event", G_CALLBACK(configure_event),
658         NULL);
659          */
660         g_signal_connect(g->drawing_area, "motion_notify_event",
661                        G_CALLBACK(motion_notify_event), NULL);
662         g_signal_connect(g->drawing_area, "button_press_event",
663                        G_CALLBACK(button_press_event), NULL);
664         g_signal_connect(g->drawing_area, "button_release_event",
665                        G_CALLBACK(button_release_event), NULL);
666         g_signal_connect(g->drawing_area, "leave_notify_event",
667                        G_CALLBACK(leave_notify_event), NULL);
668         g_signal_connect(g->drawing_area, "enter_notify_event",
669                        G_CALLBACK(enter_notify_event), NULL);
670         g_signal_connect(g->toplevel, "destroy", G_CALLBACK(callback_toplevel_destroy), g);
671         /* why doesn't drawing area send key_press_signals? */
672         g_signal_connect(g->toplevel, "key_press_event", G_CALLBACK(key_press_event), NULL);
673         g_signal_connect(g->toplevel, "key_release_event", G_CALLBACK(key_release_event),
674                        NULL);
675         gtk_widget_set_events(g->toplevel,
676                               GDK_KEY_PRESS_MASK|GDK_KEY_RELEASE_MASK);
677
678         gtk_widget_set_events (g->drawing_area,
679                                GDK_EXPOSURE_MASK
680                                | GDK_LEAVE_NOTIFY_MASK
681                                | GDK_ENTER_NOTIFY_MASK
682                                | GDK_BUTTON_PRESS_MASK
683                                | GDK_BUTTON_RELEASE_MASK
684                                | GDK_POINTER_MOTION_MASK
685                                | GDK_POINTER_MOTION_HINT_MASK);
686
687 #if 0
688         frame = gtk_frame_new (NULL);
689         gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_ETCHED_IN);
690         gtk_container_add (GTK_CONTAINER (frame), g->drawing_area);
691
692         box = gtk_hbox_new (FALSE, 0);
693         gtk_box_pack_start (GTK_BOX (box), g->gui.control_panel, FALSE, FALSE, 0);
694         gtk_box_pack_start (GTK_BOX (box), frame, TRUE, TRUE, 0);
695         gtk_container_add (GTK_CONTAINER (g->toplevel), box);
696         gtk_container_set_border_width (GTK_CONTAINER (g->toplevel), 5);
697         gtk_widget_show (frame);
698         gtk_widget_show (box);
699 #endif
700
701         gtk_container_add (GTK_CONTAINER (g->toplevel), g->drawing_area);
702         gtk_widget_show (g->toplevel);
703
704         /* in case we didn't get what we asked for */
705         g->wp.width = GTK_WIDGET (g->drawing_area)->allocation.width -
706                                                 g->wp.x - RMARGIN_WIDTH;
707         g->wp.height = GTK_WIDGET (g->drawing_area)->allocation.height -
708                                                 g->wp.y - g->x_axis->s.height;
709
710         g->font = g->drawing_area->style->font_desc;
711
712         colormap = gdk_window_get_colormap (g->drawing_area->window);
713         if (!xor_gc) {
714                 xor_gc = gdk_gc_new (g->drawing_area->window);
715                 gdk_gc_set_function (xor_gc, GDK_XOR);
716                 if (!gdk_color_parse ("gray15", &color)) {
717                         /*
718                          * XXX - do more than just warn.
719                          */
720                         simple_dialog(ESD_TYPE_WARN, ESD_BTN_OK,
721                             "Could not parse color gray15.");
722                 }
723                 if (!gdk_colormap_alloc_color (colormap, &color, FALSE, TRUE)) {
724                         /*
725                          * XXX - do more than just warn.
726                          */
727                         simple_dialog(ESD_TYPE_WARN, ESD_BTN_OK,
728                             "Could not allocate color gray15.");
729                 }
730                 gdk_gc_set_foreground (xor_gc, &color);
731         }
732         g->fg_gc = gdk_gc_new (g->drawing_area->window);
733         g->bg_gc = gdk_gc_new (g->drawing_area->window);
734         if (!gdk_color_parse ("white", &color)) {
735                 /*
736                  * XXX - do more than just warn.
737                  */
738                 simple_dialog(ESD_TYPE_WARN, ESD_BTN_OK,
739                     "Could not parse color white.");
740         }
741         if (!gdk_colormap_alloc_color (colormap, &color, FALSE, TRUE)) {
742                 /*
743                  * XXX - do more than just warn.
744                  */
745                 simple_dialog(ESD_TYPE_WARN, ESD_BTN_OK,
746                     "Could not allocate color white.");
747         }
748         gdk_gc_set_foreground (g->bg_gc, &color);
749
750         /* this is probably quite an ugly way to get rid of the first configure
751          * event
752          * immediatelly after gtk_widget_show (window) drawing_area gets a configure
753          * event which is handled during the next return to gtk_main which is
754          * probably the gdk_gc_new() call. configure handler calls
755          * graph_element_lists_make() which is not good because the graph struct is
756          * not fully set up yet - namely we're not sure about actual geometry
757          * and we don't have the GC's at all. so we just postpone installation
758          * of configure handler until we're ready to deal with it.
759          *
760          * !!! NEMLLO BY TO BYT NA KONCI graph_init_sequence()? !!!
761          *
762          */
763         g_signal_connect(g->drawing_area,"configure_event", G_CALLBACK(configure_event),
764                        NULL);
765
766         /* puts ("exiting create_drawing_area()"); */
767 }
768
769 static void callback_toplevel_destroy (GtkWidget *widget _U_, gpointer data)
770 {
771         struct graph *g = (struct graph * )data;
772
773         if (!(g->flags & GRAPH_DESTROYED)) {
774                 g->flags |= GRAPH_DESTROYED;
775                 graph_destroy ((struct graph * )data);
776         }
777 }
778
779 static void control_panel_create (struct graph *g)
780 {
781     GtkWidget *toplevel, *notebook;
782     GtkWidget *table;
783     GtkWidget *help_bt, *close_bt, *bbox;
784     char window_title[WINDOW_TITLE_LENGTH];
785
786     debug(DBS_FENTRY) puts ("control_panel_create()");
787
788     notebook = gtk_notebook_new ();
789     control_panel_add_zoom_page (g, notebook);
790     control_panel_add_magnify_page (g, notebook);
791     control_panel_add_origin_page (g, notebook);
792     control_panel_add_cross_page (g, notebook);
793     control_panel_add_graph_type_page (g, notebook);
794
795     g_snprintf (window_title, WINDOW_TITLE_LENGTH,
796                 "Graph %d - Control - Wireshark", refnum);
797     toplevel = dlg_window_new ("tcp-graph-control");
798     gtk_window_set_title(GTK_WINDOW(toplevel), window_title);
799
800     table = gtk_table_new (2, 1,  FALSE);
801     gtk_container_add (GTK_CONTAINER (toplevel), table);
802
803     gtk_table_attach (GTK_TABLE (table), notebook, 0, 1, 0, 1,
804                       GTK_FILL|GTK_EXPAND, GTK_FILL, 5, 5);
805
806     /* Button row. */
807     bbox = dlg_button_row_new(GTK_STOCK_HELP, GTK_STOCK_CLOSE, NULL);
808     gtk_table_attach (GTK_TABLE (table), bbox, 0, 1, 1, 2,
809                       GTK_FILL|GTK_EXPAND, GTK_FILL, 5, 5);
810
811     help_bt = g_object_get_data(G_OBJECT(bbox), GTK_STOCK_HELP);
812     g_signal_connect(help_bt, "clicked", G_CALLBACK(callback_create_help), g);
813
814     close_bt = g_object_get_data(G_OBJECT(bbox), GTK_STOCK_CLOSE);
815     window_set_cancel_button(toplevel, close_bt, NULL);
816     g_signal_connect(close_bt, "clicked", G_CALLBACK(callback_close), g);
817
818     g_signal_connect(toplevel, "delete_event", G_CALLBACK(callback_delete_event), g);
819     g_signal_connect(toplevel, "destroy", G_CALLBACK(callback_toplevel_destroy), g);
820
821     /* gtk_widget_show_all (table); */
822     /* g->gui.control_panel = table; */
823     gtk_widget_show_all (toplevel);
824     window_present(toplevel);
825
826     g->gui.control_panel = toplevel;
827 }
828
829 static void control_panel_add_zoom_page (struct graph *g, GtkWidget *n)
830 {
831         GtkWidget *zoom_frame;
832         GtkWidget *zoom_lock_frame;
833         GtkWidget *label;
834         GtkWidget *box;
835
836         zoom_frame = control_panel_create_zoom_group (g);
837         gtk_container_set_border_width (GTK_CONTAINER (zoom_frame), 5);
838         zoom_lock_frame = control_panel_create_zoomlock_group (g);
839         gtk_container_set_border_width (GTK_CONTAINER (zoom_lock_frame), 5);
840         box = gtk_vbox_new (FALSE, 0);
841         gtk_box_pack_start (GTK_BOX (box), zoom_frame, TRUE, TRUE, 0);
842         gtk_box_pack_start (GTK_BOX (box), zoom_lock_frame, TRUE, TRUE, 0);
843         gtk_widget_show (box);
844         label = gtk_label_new ("Zoom");
845         gtk_notebook_append_page (GTK_NOTEBOOK (n), box, label);
846 }
847
848 static void control_panel_add_magnify_page (struct graph *g, GtkWidget *n)
849 {
850         GtkWidget *mag_frame, *label;
851
852         mag_frame = control_panel_create_magnify_group (g);
853         gtk_container_set_border_width (GTK_CONTAINER (mag_frame), 5);
854         label = gtk_label_new ("Magnify");
855         gtk_notebook_append_page (GTK_NOTEBOOK (n), mag_frame, label);
856 }
857
858 static void control_panel_add_origin_page (struct graph *g, GtkWidget *n)
859 {
860         GtkWidget *time_orig_cap, *time_orig_conn, *time_orig_box, *time_orig_frame;
861         GtkWidget *seq_orig_isn, *seq_orig_zero, *seq_orig_box, *seq_orig_frame;
862         GtkWidget *box, *label;
863
864         /* time origin box */
865         time_orig_cap =
866                         gtk_radio_button_new_with_label (NULL, "beginning of capture");
867         time_orig_conn = gtk_radio_button_new_with_label (
868                         gtk_radio_button_get_group (GTK_RADIO_BUTTON (time_orig_cap)),
869                         "beginning of this TCP connection");
870         gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (time_orig_conn), TRUE);
871         time_orig_box = gtk_vbox_new (TRUE, 0);
872         gtk_box_pack_start (GTK_BOX (time_orig_box), time_orig_conn, TRUE, TRUE, 0);
873         gtk_box_pack_start (GTK_BOX (time_orig_box), time_orig_cap, TRUE, TRUE, 0);
874         time_orig_frame = gtk_frame_new ("Time origin");
875         gtk_container_set_border_width (GTK_CONTAINER (time_orig_frame), 5);
876         gtk_container_add (GTK_CONTAINER (time_orig_frame), time_orig_box);
877
878         /* sequence number origin group */
879         seq_orig_isn =
880                         gtk_radio_button_new_with_label (NULL, "initial sequence number");
881         seq_orig_zero = gtk_radio_button_new_with_label (gtk_radio_button_get_group (
882                         GTK_RADIO_BUTTON (seq_orig_isn)), "0 (=absolute)");
883         gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (seq_orig_isn), TRUE);
884         seq_orig_box = gtk_vbox_new (TRUE, 0);
885         gtk_box_pack_start (GTK_BOX (seq_orig_box), seq_orig_isn, TRUE, TRUE, 0);
886         gtk_box_pack_start (GTK_BOX (seq_orig_box), seq_orig_zero, TRUE, TRUE, 0);
887         seq_orig_frame = gtk_frame_new ("Sequence number origin");
888         gtk_container_set_border_width (GTK_CONTAINER (seq_orig_frame), 5);
889         gtk_container_add (GTK_CONTAINER (seq_orig_frame), seq_orig_box);
890
891         g->gui.time_orig_conn = (GtkToggleButton * )time_orig_conn;
892         g->gui.seq_orig_isn = (GtkToggleButton * )seq_orig_isn;
893
894         g_signal_connect(time_orig_conn, "toggled", G_CALLBACK(callback_time_origin), g);
895         g_signal_connect(seq_orig_isn, "toggled", G_CALLBACK(callback_seq_origin), g);
896
897         box = gtk_vbox_new (FALSE, 0);
898         gtk_container_set_border_width (GTK_CONTAINER (box), 5);
899         gtk_box_pack_start (GTK_BOX (box), time_orig_frame, TRUE, TRUE, 0);
900         gtk_box_pack_start (GTK_BOX (box), seq_orig_frame, TRUE, TRUE, 0);
901         gtk_widget_show (box);
902         label = gtk_label_new ("Origin");
903         gtk_notebook_append_page (GTK_NOTEBOOK (n), box, label);
904 }
905
906 static void control_panel_add_cross_page (struct graph *g, GtkWidget *n)
907 {
908         GtkWidget *cross_frame, *label;
909
910         cross_frame = control_panel_create_cross_group (g);
911         gtk_container_set_border_width (GTK_CONTAINER (cross_frame), 5);
912         label = gtk_label_new ("Cross");
913         gtk_notebook_append_page (GTK_NOTEBOOK (n), cross_frame, label);
914 }
915
916 static void control_panel_add_graph_type_page (struct graph *g, GtkWidget *n)
917 {
918         GtkWidget *frame, *label;
919
920         frame = control_panel_create_graph_type_group (g);
921         gtk_container_set_border_width (GTK_CONTAINER (frame), 5);
922         label = gtk_label_new ("Graph type");
923         gtk_notebook_append_page (GTK_NOTEBOOK (n), frame, label);
924 }
925
926 /* Treat this as a cancel, by calling "callback_close()" */
927 static gboolean
928 callback_delete_event(GtkWidget *widget _U_, GdkEvent *event _U_,
929                       gpointer data)
930 {
931         callback_close(NULL, data);
932         return FALSE;
933 }
934
935 static void callback_close (GtkWidget *widget _U_, gpointer data)
936 {
937         struct graph *g = (struct graph * )data;
938
939         if (!(g->flags & GRAPH_DESTROYED)) {
940                 g->flags |= GRAPH_DESTROYED;
941                 graph_destroy ((struct graph * )data);
942         }
943 }
944
945 static void callback_create_help(GtkWidget *widget _U_, gpointer data _U_)
946 {
947         GtkWidget *toplevel, *vbox, *text, *scroll, *bbox, *close_bt;
948         GtkTextBuffer *buf;
949
950         toplevel = dlg_window_new ("Help for TCP graphing");
951         gtk_window_set_default_size(GTK_WINDOW(toplevel), 500, 400);
952
953         vbox = gtk_vbox_new (FALSE, 3);
954         gtk_container_set_border_width(GTK_CONTAINER(vbox), 12);
955         gtk_container_add (GTK_CONTAINER (toplevel), vbox);
956
957         scroll = scrolled_window_new (NULL, NULL);
958         gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scroll),
959                                    GTK_SHADOW_IN);
960         gtk_box_pack_start (GTK_BOX (vbox), scroll, TRUE, TRUE, 0);
961         text = gtk_text_view_new();
962         gtk_text_view_set_editable(GTK_TEXT_VIEW(text), FALSE);
963         buf = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text));
964         gtk_text_buffer_set_text(buf, helptext, -1);
965         gtk_container_add (GTK_CONTAINER (scroll), text);
966
967         /* Button row. */
968         bbox = dlg_button_row_new(GTK_STOCK_CLOSE, NULL);
969         gtk_box_pack_start (GTK_BOX (vbox), bbox, FALSE, FALSE, 0);
970         gtk_widget_show(bbox);
971
972         close_bt = g_object_get_data(G_OBJECT(bbox), GTK_STOCK_CLOSE);
973         window_set_cancel_button(toplevel, close_bt, window_cancel_button_cb);
974
975         g_signal_connect(toplevel, "delete_event", G_CALLBACK(window_delete_event_cb), NULL);
976
977         gtk_widget_show_all (toplevel);
978         window_present(toplevel);
979 }
980
981 static void callback_time_origin (GtkWidget *toggle _U_, gpointer data)
982 {
983         toggle_time_origin ((struct graph * )data);
984 }
985
986 static void callback_seq_origin (GtkWidget *toggle _U_, gpointer data)
987 {
988         toggle_seq_origin ((struct graph * )data);
989 }
990
991 static GtkWidget *control_panel_create_zoom_group (struct graph *g)
992 {
993         GtkWidget *zoom_in, *zoom_out, *zoom_box, *zoom_frame;
994         GtkAdjustment *zoom_h_adj, *zoom_v_adj;
995         GtkWidget *zoom_inout_box, *zoom_h_step_label, *zoom_h_step;
996         GtkWidget *zoom_v_step_label, *zoom_v_step;
997         GtkWidget *zoom_separator1, *zoom_separator2, *zoom_step_table, *zoom_table;
998         GtkWidget *zoom_ratio_toggle, *zoom_same_toggle;
999         GtkWidget *zoom_h_entry, *zoom_v_entry;
1000         GtkWidget *zoom_h_label, *zoom_v_label;
1001
1002         zoom_in = gtk_radio_button_new_with_label (NULL, "in");
1003         zoom_out = gtk_radio_button_new_with_label (
1004                                         gtk_radio_button_get_group (GTK_RADIO_BUTTON (zoom_in)), "out");
1005         gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (zoom_in), TRUE);
1006         zoom_inout_box = gtk_hbox_new (FALSE, 0);
1007         gtk_box_pack_start (GTK_BOX (zoom_inout_box), zoom_in, FALSE, FALSE, 10);
1008         gtk_box_pack_start (GTK_BOX (zoom_inout_box), zoom_out, FALSE, FALSE, 0);
1009
1010         zoom_separator1 = gtk_hseparator_new ();
1011
1012         zoom_h_entry = gtk_entry_new ();
1013         gtk_entry_set_text (GTK_ENTRY (zoom_h_entry), "1.000");
1014         gtk_editable_set_editable (GTK_EDITABLE (zoom_h_entry), FALSE);
1015         zoom_h_label = gtk_label_new ("Horizontal:");
1016
1017         zoom_v_entry = gtk_entry_new ();
1018         gtk_entry_set_text (GTK_ENTRY (zoom_v_entry), "1.000");
1019         gtk_editable_set_editable (GTK_EDITABLE (zoom_v_entry), FALSE);
1020         zoom_v_label = gtk_label_new ("Vertical:");
1021
1022         g->zoom.widget.h_zoom = (GtkEntry * )zoom_h_entry;
1023         g->zoom.widget.v_zoom = (GtkEntry * )zoom_v_entry;
1024
1025         zoom_table = gtk_table_new (2, 2,  FALSE);
1026         gtk_table_attach (GTK_TABLE (zoom_table), zoom_h_label, 0,1,0,1,
1027                                 GTK_FILL|GTK_EXPAND, GTK_FILL|GTK_EXPAND, 5, 0);
1028         gtk_table_attach (GTK_TABLE (zoom_table), zoom_h_entry, 1, 2, 0, 1,
1029                                 GTK_FILL|GTK_EXPAND, GTK_FILL|GTK_EXPAND, 5, 0);
1030         gtk_table_attach (GTK_TABLE (zoom_table), zoom_v_label, 0,1,1,2,
1031                                 GTK_FILL|GTK_EXPAND, GTK_FILL|GTK_EXPAND, 5, 0);
1032         gtk_table_attach (GTK_TABLE (zoom_table), zoom_v_entry, 1, 2, 1, 2,
1033                                 GTK_FILL|GTK_EXPAND, GTK_FILL|GTK_EXPAND, 5, 0);
1034
1035         zoom_separator2 = gtk_hseparator_new ();
1036
1037         zoom_h_adj = (GtkAdjustment * )gtk_adjustment_new ((gfloat)1.2, 1.0, 5, (gfloat)0.1, 1, 0);
1038         zoom_h_step = gtk_spin_button_new (zoom_h_adj, 0, 1);
1039         gtk_spin_button_set_numeric (GTK_SPIN_BUTTON (zoom_h_step), TRUE);
1040         zoom_h_step_label = gtk_label_new ("Horizontal step:");
1041
1042         zoom_v_adj = (GtkAdjustment * )gtk_adjustment_new ((gfloat)1.2, 1.0, 5, (gfloat)0.1, 1, 0);
1043         zoom_v_step = gtk_spin_button_new (zoom_v_adj, 0, 1);
1044         gtk_spin_button_set_numeric (GTK_SPIN_BUTTON (zoom_v_step), TRUE);
1045         zoom_v_step_label = gtk_label_new ("Vertical step:");
1046
1047         g->zoom.widget.h_step = (GtkSpinButton * )zoom_h_step;
1048         g->zoom.widget.v_step = (GtkSpinButton * )zoom_v_step;
1049
1050         zoom_same_toggle = gtk_check_button_new_with_label("Keep them the same");
1051         zoom_ratio_toggle = gtk_check_button_new_with_label("Preserve their ratio");
1052         g_object_set_data(G_OBJECT(zoom_same_toggle), "flag", (gpointer)ZOOM_STEPS_SAME);
1053         g_object_set_data(G_OBJECT(zoom_ratio_toggle), "flag",
1054                         (gpointer)ZOOM_STEPS_KEEP_RATIO);
1055         g_signal_connect(zoom_same_toggle, "clicked", G_CALLBACK(callback_zoom_flags), g);
1056         g_signal_connect(zoom_ratio_toggle, "clicked", G_CALLBACK(callback_zoom_flags), g);
1057
1058         zoom_step_table = gtk_table_new (4, 2,  FALSE);
1059         gtk_table_attach (GTK_TABLE (zoom_step_table), zoom_h_step_label, 0,1,0,1,
1060                                 GTK_FILL|GTK_EXPAND, GTK_FILL|GTK_EXPAND, 5, 0);
1061         gtk_table_attach (GTK_TABLE (zoom_step_table), zoom_h_step, 1, 2, 0, 1,
1062                                 GTK_FILL|GTK_EXPAND, GTK_FILL|GTK_EXPAND, 5, 0);
1063         gtk_table_attach (GTK_TABLE (zoom_step_table), zoom_v_step_label, 0,1,1,2,
1064                                 GTK_FILL|GTK_EXPAND, GTK_FILL|GTK_EXPAND, 5, 0);
1065         gtk_table_attach (GTK_TABLE (zoom_step_table), zoom_v_step, 1, 2, 1, 2,
1066                                 GTK_FILL|GTK_EXPAND, GTK_FILL|GTK_EXPAND, 5, 0);
1067         gtk_table_attach (GTK_TABLE (zoom_step_table), zoom_same_toggle, 0,2,2,3,
1068                                 GTK_FILL|GTK_EXPAND, GTK_FILL|GTK_EXPAND, 5, 0);
1069         gtk_table_attach (GTK_TABLE (zoom_step_table), zoom_ratio_toggle, 0,2,3,4,
1070                                 GTK_FILL|GTK_EXPAND, GTK_FILL|GTK_EXPAND, 5, 0);
1071
1072         zoom_box = gtk_vbox_new (FALSE, 0);
1073         gtk_box_pack_start (GTK_BOX (zoom_box), zoom_inout_box, TRUE, TRUE, 0);
1074         gtk_box_pack_start (GTK_BOX (zoom_box), zoom_separator1, TRUE, TRUE, 0);
1075         gtk_box_pack_start (GTK_BOX (zoom_box), zoom_table, TRUE, TRUE, 0);
1076         gtk_box_pack_start (GTK_BOX (zoom_box), zoom_separator2, TRUE, TRUE, 0);
1077         gtk_box_pack_start (GTK_BOX (zoom_box), zoom_step_table, TRUE, TRUE, 0);
1078         zoom_frame = gtk_frame_new ("Zoom");
1079         gtk_container_add (GTK_CONTAINER (zoom_frame), zoom_box);
1080
1081         g_object_set_data(G_OBJECT(zoom_h_step), "direction", GINT_TO_POINTER(0));
1082         g_object_set_data(G_OBJECT(zoom_v_step), "direction", GINT_TO_POINTER(1));
1083
1084         g_signal_connect(zoom_in, "toggled", G_CALLBACK(callback_zoom_inout), g);
1085         g_signal_connect(zoom_h_step, "changed", G_CALLBACK(callback_zoom_step), g);
1086         g_signal_connect(zoom_v_step, "changed", G_CALLBACK(callback_zoom_step), g);
1087
1088         g->zoom.widget.in_toggle = (GtkToggleButton * )zoom_in;
1089         g->zoom.widget.out_toggle = (GtkToggleButton * )zoom_out;
1090         return zoom_frame;
1091 }
1092
1093 static void callback_zoom_inout (GtkWidget *toggle, gpointer data)
1094 {
1095         struct graph *g = (struct graph * )data;
1096
1097         if (GTK_TOGGLE_BUTTON (toggle)->active)
1098                 g->zoom.flags &= ~ZOOM_OUT;
1099         else
1100                 g->zoom.flags |= ZOOM_OUT;
1101 }
1102
1103 static void callback_zoom_step (GtkWidget *spin, gpointer data)
1104 {
1105         struct graph *g = (struct graph * )data;
1106         double value;
1107         int direction;
1108         double *zoom_this, *zoom_other;
1109         GtkSpinButton *widget_this, *widget_other;
1110         double old_this;
1111
1112         direction = (long)g_object_get_data(G_OBJECT(spin), "direction");
1113         value = gtk_spin_button_get_value (GTK_SPIN_BUTTON (spin));
1114
1115         if (direction) {
1116                 zoom_this = &g->zoom.step_y;
1117                 zoom_other = &g->zoom.step_x;
1118                 widget_this = g->zoom.widget.v_step;
1119                 widget_other = g->zoom.widget.h_step;
1120         } else {
1121                 zoom_this = &g->zoom.step_x;
1122                 zoom_other = &g->zoom.step_y;
1123                 widget_this = g->zoom.widget.h_step;
1124                 widget_other = g->zoom.widget.v_step;
1125         }
1126
1127         old_this = *zoom_this;
1128         *zoom_this = value;
1129         if (g->zoom.flags & ZOOM_STEPS_SAME) {
1130                 *zoom_other = value;
1131                 gtk_spin_button_set_value (widget_other, (gfloat) *zoom_other);
1132         } else if (g->zoom.flags & ZOOM_STEPS_KEEP_RATIO) {
1133                 double old_other = *zoom_other;
1134                 *zoom_other *= value / old_this;
1135                 if (*zoom_other < 1.0) {
1136                         *zoom_other = 1.0;
1137                         *zoom_this = old_this * 1.0 / old_other;
1138                         gtk_spin_button_set_value (widget_this, (gfloat) *zoom_this);
1139                 } else if (*zoom_other > 5.0) {
1140                         *zoom_other = 5.0;
1141                         *zoom_this = old_this * 5.0 / old_other;
1142                         gtk_spin_button_set_value (widget_this, (gfloat) *zoom_this);
1143                 }
1144                 gtk_spin_button_set_value (widget_other, (gfloat) *zoom_other);
1145         }
1146 }
1147
1148 static void callback_zoom_flags (GtkWidget *toggle, gpointer data)
1149 {
1150         struct graph *g = (struct graph * )data;
1151         int flag = (long)g_object_get_data(G_OBJECT(toggle), "flag");
1152
1153         if (GTK_TOGGLE_BUTTON (toggle)->active)
1154                 g->zoom.flags |= flag;
1155         else
1156                 g->zoom.flags &= ~flag;
1157 }
1158
1159 static void update_zoom_spins (struct graph *g)
1160 {
1161         char s[32];
1162
1163         g_snprintf (s, sizeof(s), "%.3f", g->zoom.x / g->zoom.initial.x);
1164         gtk_entry_set_text (g->zoom.widget.h_zoom, s);
1165         g_snprintf (s, sizeof(s), "%.3f", g->zoom.y / g->zoom.initial.y);
1166         gtk_entry_set_text (g->zoom.widget.v_zoom, s);
1167 }
1168
1169 static GtkWidget *control_panel_create_magnify_group (struct graph *g)
1170 {
1171         GtkWidget *mag_width_label, *mag_width;
1172         GtkWidget *mag_height_label, *mag_height;
1173         GtkWidget *mag_x_label, *mag_x;
1174         GtkWidget *mag_y_label, *mag_y;
1175         GtkWidget *mag_wh_table, *mag_zoom_frame, *mag_zoom_table;
1176         GtkWidget *mag_h_zoom_label, *mag_h_zoom;
1177         GtkWidget *mag_v_zoom_label, *mag_v_zoom;
1178         GtkWidget *mag_zoom_same, *mag_zoom_ratio;
1179         GtkAdjustment *mag_width_adj, *mag_height_adj, *mag_x_adj, *mag_y_adj;
1180         GtkAdjustment *mag_h_zoom_adj, *mag_v_zoom_adj;
1181         GtkWidget *mag_box, *mag_frame;
1182
1183         mag_width_label = gtk_label_new ("Width:");
1184         mag_width_adj = (GtkAdjustment * )gtk_adjustment_new (250,100,600,1,10,0);
1185         mag_width = gtk_spin_button_new (mag_width_adj, 0, 0);
1186
1187         mag_height_label = gtk_label_new ("Height:");
1188         mag_height_adj = (GtkAdjustment * )gtk_adjustment_new (250,100,600,1,10,0);
1189         mag_height = gtk_spin_button_new (mag_height_adj, 0, 0);
1190
1191         mag_x_label = gtk_label_new ("X:");
1192         mag_x_adj = (GtkAdjustment * )gtk_adjustment_new (0,-1000,1000,1,10,0);
1193         mag_x = gtk_spin_button_new (mag_x_adj, 0, 0);
1194
1195         mag_y_label = gtk_label_new ("Y:");
1196         mag_y_adj = (GtkAdjustment * )gtk_adjustment_new (0,-1000,1000,1,10,0);
1197         mag_y = gtk_spin_button_new (mag_y_adj, 0, 0);
1198
1199         mag_wh_table = gtk_table_new (4, 2, FALSE);
1200         gtk_table_attach (GTK_TABLE (mag_wh_table), mag_width_label, 0,1,0,1,
1201                                 GTK_FILL|GTK_EXPAND, GTK_FILL|GTK_EXPAND, 5, 0);
1202         gtk_table_attach (GTK_TABLE (mag_wh_table), mag_width, 1,2,0,1,
1203                                 GTK_FILL|GTK_EXPAND, GTK_FILL|GTK_EXPAND, 5, 0);
1204         gtk_table_attach (GTK_TABLE (mag_wh_table), mag_height_label, 0,1,1,2,
1205                                 GTK_FILL|GTK_EXPAND, GTK_FILL|GTK_EXPAND, 5, 0);
1206         gtk_table_attach (GTK_TABLE (mag_wh_table), mag_height, 1,2,1,2,
1207                                 GTK_FILL|GTK_EXPAND, GTK_FILL|GTK_EXPAND, 5, 0);
1208         gtk_table_attach (GTK_TABLE (mag_wh_table), mag_x_label, 0,1,2,3,
1209                                 GTK_FILL|GTK_EXPAND, GTK_FILL|GTK_EXPAND, 5, 0);
1210         gtk_table_attach (GTK_TABLE (mag_wh_table), mag_x, 1,2,2,3,
1211                                 GTK_FILL|GTK_EXPAND, GTK_FILL|GTK_EXPAND, 5, 0);
1212         gtk_table_attach (GTK_TABLE (mag_wh_table), mag_y_label, 0,1,3,4,
1213                                 GTK_FILL|GTK_EXPAND, GTK_FILL|GTK_EXPAND, 5, 0);
1214         gtk_table_attach (GTK_TABLE (mag_wh_table), mag_y, 1,2,3,4,
1215                                 GTK_FILL|GTK_EXPAND, GTK_FILL|GTK_EXPAND, 5, 0);
1216
1217         mag_h_zoom_label = gtk_label_new ("Horizontal:");
1218         mag_h_zoom_adj = (GtkAdjustment *)gtk_adjustment_new(10.0, 1.0, 25.0, (gfloat)0.1, 1, 0);
1219         mag_h_zoom = gtk_spin_button_new (mag_h_zoom_adj, 0, 1);
1220
1221         mag_v_zoom_label = gtk_label_new ("Vertical:");
1222         mag_v_zoom_adj = (GtkAdjustment *)gtk_adjustment_new(10.0, 1.0, 25.0, (gfloat)0.1, 1, 0);
1223         mag_v_zoom = gtk_spin_button_new (mag_v_zoom_adj, 0, 1);
1224
1225         mag_zoom_same = gtk_check_button_new_with_label ("Keep them the same");
1226         mag_zoom_ratio = gtk_check_button_new_with_label("Preserve their ratio");
1227
1228         mag_zoom_table = gtk_table_new (4, 2, FALSE);
1229         gtk_table_attach (GTK_TABLE (mag_zoom_table), mag_h_zoom_label, 0,1,0,1,
1230                                 GTK_FILL|GTK_EXPAND, GTK_FILL|GTK_EXPAND, 0, 0);
1231         gtk_table_attach (GTK_TABLE (mag_zoom_table), mag_h_zoom, 1,2,0,1,
1232                                 GTK_FILL|GTK_EXPAND, GTK_FILL|GTK_EXPAND, 0, 0);
1233         gtk_table_attach (GTK_TABLE (mag_zoom_table), mag_v_zoom_label, 0,1,1,2,
1234                                 GTK_FILL|GTK_EXPAND, GTK_FILL|GTK_EXPAND, 0, 0);
1235         gtk_table_attach (GTK_TABLE (mag_zoom_table), mag_v_zoom, 1,2,1,2,
1236                                 GTK_FILL|GTK_EXPAND, GTK_FILL|GTK_EXPAND, 0, 0);
1237         gtk_table_attach (GTK_TABLE (mag_zoom_table), mag_zoom_same, 0,2,2,3,
1238                                 GTK_FILL|GTK_EXPAND, GTK_FILL|GTK_EXPAND, 0, 0);
1239         gtk_table_attach (GTK_TABLE (mag_zoom_table), mag_zoom_ratio, 0,2,3,4,
1240                                 GTK_FILL|GTK_EXPAND, GTK_FILL|GTK_EXPAND, 0, 0);
1241
1242         mag_zoom_frame = gtk_frame_new ("Magnify zoom");
1243         gtk_container_add (GTK_CONTAINER (mag_zoom_frame), mag_zoom_table);
1244         gtk_container_set_border_width (GTK_CONTAINER (mag_zoom_frame), 3);
1245
1246         mag_box = gtk_vbox_new (FALSE, 0);
1247         gtk_box_pack_start (GTK_BOX (mag_box), mag_wh_table, TRUE, TRUE, 0);
1248         gtk_box_pack_start (GTK_BOX (mag_box), mag_zoom_frame, TRUE, TRUE, 0);
1249         mag_frame = gtk_frame_new ("Magnify");
1250         gtk_container_add (GTK_CONTAINER (mag_frame), mag_box);
1251
1252         g->magnify.widget.h_zoom = (GtkSpinButton * )mag_h_zoom;
1253         g->magnify.widget.v_zoom = (GtkSpinButton * )mag_v_zoom;
1254         g_object_set_data(G_OBJECT(mag_h_zoom), "direction", GINT_TO_POINTER(0));
1255         g_object_set_data(G_OBJECT(mag_v_zoom), "direction", GINT_TO_POINTER(1));
1256         g_object_set_data(G_OBJECT(mag_zoom_same), "flag", (gpointer)MAGZOOMS_SAME);
1257         g_object_set_data(G_OBJECT(mag_zoom_ratio), "flag", (gpointer)MAGZOOMS_SAME_RATIO);
1258
1259         g_signal_connect(mag_width, "changed", G_CALLBACK(callback_mag_width), g);
1260         g_signal_connect(mag_height, "changed", G_CALLBACK(callback_mag_height), g);
1261         g_signal_connect(mag_x, "changed", G_CALLBACK(callback_mag_x), g);
1262         g_signal_connect(mag_y, "changed", G_CALLBACK(callback_mag_y), g);
1263         g_signal_connect(mag_h_zoom, "changed", G_CALLBACK(callback_mag_zoom), g);
1264         g_signal_connect(mag_v_zoom, "changed", G_CALLBACK(callback_mag_zoom), g);
1265         g_signal_connect(mag_zoom_same, "clicked", G_CALLBACK(callback_mag_flags), g);
1266         g_signal_connect(mag_zoom_ratio, "clicked", G_CALLBACK(callback_mag_flags), g);
1267
1268         return mag_frame;
1269 }
1270
1271 static void callback_mag_width (GtkWidget *spin, gpointer data)
1272 {
1273         struct graph *g = (struct graph * )data;
1274
1275         g->magnify.width = gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON(spin));
1276 }
1277
1278 static void callback_mag_height (GtkWidget *spin, gpointer data)
1279 {
1280         struct graph *g = (struct graph * )data;
1281
1282         g->magnify.height = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(spin));
1283 }
1284
1285 static void callback_mag_x (GtkWidget *spin, gpointer data)
1286 {
1287         struct graph *g = (struct graph * )data;
1288
1289         g->magnify.offset.x=gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(spin));
1290 }
1291
1292 static void callback_mag_y (GtkWidget *spin, gpointer data)
1293 {
1294         struct graph *g = (struct graph * )data;
1295
1296         g->magnify.offset.y=gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(spin));
1297 }
1298
1299 static void callback_mag_zoom (GtkWidget *spin, gpointer data)
1300 {
1301         struct graph *g = (struct graph * )data;
1302         double value;
1303         int direction;
1304         double *zoom_this, *zoom_other;
1305         GtkSpinButton *widget_this, *widget_other;
1306         double old_this;
1307
1308         if (g->magnify.flags & MAGZOOMS_IGNORE) {
1309                 printf ("refusing callback for %s zoom widget.\n", (GtkSpinButton * )spin==g->magnify.widget.h_zoom ? "horizontal" : "vertical");
1310                 g->magnify.flags &= ~MAGZOOMS_IGNORE;
1311                 return;
1312         }
1313         direction = (long)g_object_get_data(G_OBJECT(spin), "direction");
1314         value = gtk_spin_button_get_value (GTK_SPIN_BUTTON (spin));
1315
1316         if (direction) {
1317                 zoom_this = &g->magnify.zoom.y;
1318                 zoom_other = &g->magnify.zoom.x;
1319                 widget_this = g->magnify.widget.v_zoom;
1320                 widget_other = g->magnify.widget.h_zoom;
1321         } else {
1322                 zoom_this = &g->magnify.zoom.x;
1323                 zoom_other = &g->magnify.zoom.y;
1324                 widget_this = g->magnify.widget.h_zoom;
1325                 widget_other = g->magnify.widget.v_zoom;
1326         }
1327
1328         old_this = *zoom_this;
1329         *zoom_this = value;
1330         if (g->magnify.flags & MAGZOOMS_SAME) {
1331                 *zoom_other = value;
1332                 /* g->magnify.flags |= MAGZOOMS_IGNORE; */
1333                 gtk_spin_button_set_value (widget_other, (gfloat) *zoom_other);
1334         } else if (g->magnify.flags & MAGZOOMS_SAME_RATIO) {
1335                 double old_other = *zoom_other;
1336                 *zoom_other *= value / old_this;
1337                 if (*zoom_other < 1.0) {
1338                         *zoom_other = 1.0;
1339                         *zoom_this = old_this * 1.0 / old_other;
1340                         /* g->magnify.flags |= MAGZOOMS_IGNORE; */
1341                         gtk_spin_button_set_value (widget_this, (gfloat) *zoom_this);
1342                 } else if (*zoom_other > 25.0) {
1343                         *zoom_other = 25.0;
1344                         *zoom_this = old_this * 25.0 / old_other;
1345                         /* g->magnify.flags |= MAGZOOMS_IGNORE; */
1346                         gtk_spin_button_set_value (widget_this, (gfloat) *zoom_this);
1347                 }
1348                 /* g->magnify.flags |= MAGZOOMS_IGNORE; */
1349                 gtk_spin_button_set_value (widget_other, (gfloat) *zoom_other);
1350         }
1351 }
1352
1353 static void callback_mag_flags (GtkWidget *toggle, gpointer data)
1354 {
1355         struct graph *g = (struct graph * )data;
1356         int flag = (long)g_object_get_data(G_OBJECT(toggle), "flag");
1357
1358         if (GTK_TOGGLE_BUTTON (toggle)->active)
1359                 g->magnify.flags |= flag;
1360         else
1361                 g->magnify.flags &= ~flag;
1362 }
1363
1364 static GtkWidget *control_panel_create_zoomlock_group (struct graph *g)
1365 {
1366         GtkWidget *zoom_lock_h, *zoom_lock_v, *zoom_lock_none, *zoom_lock_box;
1367         GtkWidget *zoom_lock_frame;
1368
1369         zoom_lock_none = gtk_radio_button_new_with_label (NULL, "none");
1370         zoom_lock_h = gtk_radio_button_new_with_label (
1371                                         gtk_radio_button_get_group (GTK_RADIO_BUTTON (zoom_lock_none)),
1372                                         "horizontal");
1373         zoom_lock_v = gtk_radio_button_new_with_label (
1374                                         gtk_radio_button_get_group (GTK_RADIO_BUTTON (zoom_lock_none)),
1375                                         "vertical");
1376         gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (zoom_lock_none), TRUE);
1377         zoom_lock_box = gtk_hbox_new (FALSE, 0);
1378         gtk_box_pack_start(GTK_BOX(zoom_lock_box), zoom_lock_none,
1379                            TRUE, TRUE, 0);
1380         gtk_box_pack_start(GTK_BOX(zoom_lock_box), zoom_lock_h, TRUE, TRUE, 0);
1381         gtk_box_pack_start(GTK_BOX(zoom_lock_box), zoom_lock_v, TRUE, TRUE, 0);
1382         zoom_lock_frame = gtk_frame_new ("Zoom lock:");
1383         gtk_container_add (GTK_CONTAINER (zoom_lock_frame), zoom_lock_box);
1384
1385         g_signal_connect(zoom_lock_h, "toggled", G_CALLBACK(callback_zoomlock_h), g);
1386         g_signal_connect(zoom_lock_v, "toggled", G_CALLBACK(callback_zoomlock_v), g);
1387
1388         return zoom_lock_frame;
1389 }
1390
1391 static void callback_zoomlock_h (GtkWidget *toggle, gpointer data)
1392 {
1393         struct graph *g = (struct graph * )data;
1394
1395         if (GTK_TOGGLE_BUTTON (toggle)->active)
1396                 g->zoom.flags |= ZOOM_HLOCK;
1397         else
1398                 g->zoom.flags &= ~ZOOM_HLOCK;
1399 }
1400
1401 static void callback_zoomlock_v (GtkWidget *toggle, gpointer data)
1402 {
1403         struct graph *g = (struct graph * )data;
1404
1405         if (GTK_TOGGLE_BUTTON (toggle)->active)
1406                 g->zoom.flags |= ZOOM_VLOCK;
1407         else
1408                 g->zoom.flags &= ~ZOOM_VLOCK;
1409 }
1410
1411 static GtkWidget *control_panel_create_cross_group (struct graph *g)
1412 {
1413         GtkWidget *on, *off, *box, *frame, *vbox, *label;
1414
1415         label = gtk_label_new ("Crosshairs:");
1416         off = gtk_radio_button_new_with_label (NULL, "off");
1417         on = gtk_radio_button_new_with_label (
1418                                 gtk_radio_button_get_group (GTK_RADIO_BUTTON (off)), "on");
1419         gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (off), TRUE);
1420         box = gtk_hbox_new (FALSE, 0);
1421         gtk_box_pack_start (GTK_BOX (box), label, FALSE, FALSE, 10);
1422         gtk_box_pack_start (GTK_BOX (box), off, FALSE, FALSE, 10);
1423         gtk_box_pack_start (GTK_BOX (box), on, FALSE, FALSE, 0);
1424         vbox = gtk_vbox_new (FALSE, 0);
1425         gtk_box_pack_start (GTK_BOX (vbox), box, FALSE, FALSE, 15);
1426         /* frame = gtk_frame_new ("Cross:"); */
1427         frame = gtk_frame_new (NULL);
1428         gtk_container_add (GTK_CONTAINER (frame), vbox);
1429
1430         g_signal_connect(on, "toggled", G_CALLBACK(callback_cross_on_off), g);
1431
1432         g->cross.on_toggle = (GtkToggleButton * )on;
1433         g->cross.off_toggle = (GtkToggleButton * )off;
1434
1435         return frame;
1436 }
1437
1438 static void callback_cross_on_off (GtkWidget *toggle, gpointer data)
1439 {
1440         struct graph *g = (struct graph * )data;
1441
1442         if (GTK_TOGGLE_BUTTON (toggle)->active) {
1443                 int x, y;
1444                 g->cross.draw = TRUE;
1445                 gdk_window_get_pointer (g->drawing_area->window, &x, &y, 0);
1446                 cross_draw (g, x, y);
1447         } else {
1448                 g->cross.draw = FALSE;
1449                 cross_erase (g);
1450         }
1451 }
1452
1453 static GtkWidget *control_panel_create_graph_type_group (struct graph *g)
1454 {
1455         GtkWidget *graph_tseqttrace, *graph_tseqstevens;
1456         GtkWidget *graph_tput, *graph_rtt, *graph_sep, *graph_init, *graph_box;
1457         GtkWidget *graph_frame;
1458
1459         graph_tput = gtk_radio_button_new_with_label (NULL, "Throughput");
1460         graph_tseqttrace = gtk_radio_button_new_with_label (
1461                                         gtk_radio_button_get_group (GTK_RADIO_BUTTON (graph_tput)),
1462                                         "Time/Sequence (tcptrace-style)");
1463         graph_tseqstevens = gtk_radio_button_new_with_label (
1464                                         gtk_radio_button_get_group (GTK_RADIO_BUTTON (graph_tput)),
1465                                         "Time/Sequence (Stevens'-style)");
1466         graph_rtt = gtk_radio_button_new_with_label (
1467                                         gtk_radio_button_get_group (GTK_RADIO_BUTTON (graph_tput)),
1468                                         "Round-trip Time");
1469         switch (g->type) {
1470         case GRAPH_TSEQ_STEVENS:
1471                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(graph_tseqstevens),TRUE);
1472                 break;
1473         case GRAPH_TSEQ_TCPTRACE:
1474                 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON(graph_tseqttrace),TRUE);
1475                 break;
1476         case GRAPH_THROUGHPUT:
1477                 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (graph_tput), TRUE);
1478                 break;
1479         case GRAPH_RTT:
1480                 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (graph_rtt), TRUE);
1481                 break;
1482         }
1483         graph_init = gtk_check_button_new_with_label ("Init on change");
1484         graph_sep = gtk_hseparator_new ();
1485         graph_box = gtk_vbox_new (FALSE, 0);
1486         gtk_box_pack_start (GTK_BOX (graph_box), graph_rtt, TRUE, TRUE, 0);
1487         gtk_box_pack_start (GTK_BOX (graph_box), graph_tput, TRUE, TRUE, 0);
1488         gtk_box_pack_start (GTK_BOX (graph_box), graph_tseqstevens, TRUE, TRUE, 0);
1489         gtk_box_pack_start (GTK_BOX (graph_box), graph_tseqttrace, TRUE, TRUE, 0);
1490         gtk_box_pack_start (GTK_BOX (graph_box), graph_sep, TRUE, TRUE, 0);
1491         gtk_box_pack_start (GTK_BOX (graph_box), graph_init, TRUE, TRUE, 0);
1492         graph_frame = gtk_frame_new ("Graph type:");
1493         gtk_container_add (GTK_CONTAINER (graph_frame), graph_box);
1494
1495         g_object_set_data(G_OBJECT(graph_tseqstevens), "new-graph-type",
1496                         GINT_TO_POINTER(0));
1497         g_object_set_data(G_OBJECT(graph_tseqttrace), "new-graph-type", GINT_TO_POINTER(1));
1498         g_object_set_data(G_OBJECT(graph_tput), "new-graph-type", GINT_TO_POINTER(2));
1499         g_object_set_data(G_OBJECT(graph_rtt), "new-graph-type", GINT_TO_POINTER(3));
1500
1501         g->gt.graph_rtt = (GtkToggleButton * )graph_rtt;
1502         g->gt.graph_tput = (GtkToggleButton * )graph_tput;
1503         g->gt.graph_tseqstevens = (GtkToggleButton * )graph_tseqstevens;
1504         g->gt.graph_tseqttrace = (GtkToggleButton * )graph_tseqttrace;
1505
1506         g_signal_connect(graph_tseqttrace, "toggled", G_CALLBACK(callback_graph_type), g);
1507         g_signal_connect(graph_tseqstevens, "toggled", G_CALLBACK(callback_graph_type), g);
1508         g_signal_connect(graph_tput, "toggled", G_CALLBACK(callback_graph_type), g);
1509         g_signal_connect(graph_rtt, "toggled", G_CALLBACK(callback_graph_type), g);
1510         g_signal_connect(graph_init, "toggled", G_CALLBACK(callback_graph_init_on_typechg), g);
1511
1512         return graph_frame;
1513 }
1514
1515 static void callback_graph_type (GtkWidget *toggle, gpointer data)
1516 {
1517         int old_type, new_type;
1518         struct graph *g = (struct graph * )data;
1519
1520         new_type = (long)g_object_get_data(G_OBJECT(toggle),"new-graph-type");
1521
1522         if (!GTK_TOGGLE_BUTTON (toggle)->active)
1523                 return;
1524
1525         old_type = g->type;
1526         g->type = new_type;
1527
1528         graph_element_lists_free (g);
1529         graph_element_lists_initialize (g);
1530
1531         if (old_type == GRAPH_THROUGHPUT || new_type == GRAPH_THROUGHPUT) {
1532                 /* throughput graph uses differently constructed segment list so we
1533                  * need to recreate it */
1534                 graph_segment_list_free (g);
1535                 graph_segment_list_get (g);
1536         }
1537
1538         if (g->flags & GRAPH_INIT_ON_TYPE_CHANGE) {
1539                 g->geom.width = g->wp.width;
1540                 g->geom.height = g->wp.height;
1541                 g->geom.x = g->wp.x;
1542                 g->geom.y = g->wp.y;
1543         }
1544         g->x_axis->min = g->y_axis->min = 0;
1545         gtk_toggle_button_set_active (g->gui.time_orig_conn, TRUE);
1546         gtk_toggle_button_set_active (g->gui.seq_orig_isn, TRUE);
1547         graph_init_sequence (g);
1548 }
1549
1550 static void callback_graph_init_on_typechg (GtkWidget *toggle _U_, gpointer data)
1551 {
1552         ((struct graph * )data)->flags ^= GRAPH_INIT_ON_TYPE_CHANGE;
1553 }
1554
1555 static struct graph *graph_new (void)
1556 {
1557         struct graph *g;
1558
1559         g = (struct graph * )g_malloc0 (sizeof (struct graph));
1560         graph_element_lists_initialize (g);
1561
1562         g->x_axis = (struct axis * )g_malloc0 (sizeof (struct axis));
1563         g->y_axis = (struct axis * )g_malloc0 (sizeof (struct axis));
1564         g->x_axis->g = g;
1565         g->x_axis->flags = 0;
1566         g->x_axis->flags |= AXIS_ORIENTATION;
1567         g->x_axis->s.x = g->x_axis->s.y = 0;
1568         g->x_axis->s.height = HAXIS_INIT_HEIGHT;
1569         g->x_axis->p.x = VAXIS_INIT_WIDTH;
1570         g->x_axis->p.height = HAXIS_INIT_HEIGHT;
1571         g->y_axis->g = g;
1572         g->y_axis->flags = 0;
1573         g->y_axis->flags &= ~AXIS_ORIENTATION;
1574         g->y_axis->p.x = g->y_axis->p.y = 0;
1575         g->y_axis->p.width = VAXIS_INIT_WIDTH;
1576         g->y_axis->s.x = 0;
1577         g->y_axis->s.y = TITLEBAR_HEIGHT;
1578         g->y_axis->s.width = VAXIS_INIT_WIDTH;
1579
1580         return g;
1581 }
1582
1583 static void graph_initialize_values (struct graph *g)
1584 {
1585         g->geom.width = g->wp.width = 750;
1586         g->geom.height = g->wp.height = 550;
1587         g->geom.x = g->wp.x = VAXIS_INIT_WIDTH;
1588         g->geom.y = g->wp.y = TITLEBAR_HEIGHT;
1589         g->flags = 0;
1590         /* g->zoom.x = g->zoom.y = 1.0; */
1591         g->zoom.step_x = g->zoom.step_y = 1.2;
1592         g->zoom.flags = 0;
1593         g->cross.draw = g->cross.erase_needed = 0;
1594         g->grab.grabbed = 0;
1595         g->magnify.active = 0;
1596         g->magnify.offset.x = g->magnify.offset.y = 0;
1597         g->magnify.width = g->magnify.height = 250;
1598         g->magnify.zoom.x = g->magnify.zoom.y = 10.0;
1599         g->magnify.flags = 0;
1600 }
1601
1602 static void graph_init_sequence (struct graph *g)
1603 {
1604         debug(DBS_FENTRY) puts ("graph_init_sequence()");
1605
1606         graph_type_dependent_initialize (g);
1607         g->zoom.initial.x = g->zoom.x;
1608         g->zoom.initial.y = g->zoom.y;
1609         graph_element_lists_make (g);
1610         g->x_axis->s.width = g->wp.width;
1611         g->x_axis->p.width = g->x_axis->s.width + RMARGIN_WIDTH;
1612         g->x_axis->p.y = TITLEBAR_HEIGHT + g->wp.height;
1613         g->x_axis->s.height = g->x_axis->p.height = HAXIS_INIT_HEIGHT;
1614         g->y_axis->s.height = g->wp.height;
1615         g->y_axis->p.height = g->wp.height + TITLEBAR_HEIGHT;
1616         graph_pixmaps_create (g);
1617         axis_pixmaps_create (g->y_axis);
1618         axis_pixmaps_create (g->x_axis);
1619         graph_title_pixmap_create (g);
1620         graph_title_pixmap_draw (g);
1621         graph_title_pixmap_display (g);
1622         graph_display (g);
1623         axis_display (g->y_axis);
1624         axis_display (g->x_axis);
1625 }
1626
1627 static void graph_type_dependent_initialize (struct graph *g)
1628 {
1629         switch (g->type) {
1630         case GRAPH_TSEQ_STEVENS:
1631         case GRAPH_TSEQ_TCPTRACE:
1632                 tseq_initialize (g);
1633                 break;
1634         case GRAPH_THROUGHPUT:
1635                 tput_initialize (g);
1636                 break;
1637         case GRAPH_RTT:
1638                 rtt_initialize (g);
1639                 break;
1640         default:
1641                 break;
1642         }
1643 }
1644
1645 static void graph_destroy (struct graph *g)
1646 {
1647         debug(DBS_FENTRY) puts ("graph_destroy()");
1648
1649         axis_destroy (g->x_axis);
1650         axis_destroy (g->y_axis);
1651         /* window_destroy (g->drawing_area); */
1652         window_destroy (g->gui.control_panel);
1653         window_destroy (g->toplevel);
1654         /* window_destroy (g->text); */
1655         gdk_gc_unref (g->fg_gc);
1656         gdk_gc_unref (g->bg_gc);
1657         gdk_pixmap_unref (g->pixmap[0]);
1658         gdk_pixmap_unref (g->pixmap[1]);
1659         g_free (g->x_axis);
1660         g_free (g->y_axis);
1661         g_free ( (gpointer) (g->title) );
1662         graph_segment_list_free (g);
1663         graph_element_lists_free (g);
1664
1665         g_free (g);
1666 }
1667
1668
1669 typedef struct _tcp_scan_t {
1670         struct segment *current;
1671         int direction;
1672         struct graph *g;
1673         struct segment *last;
1674 } tcp_scan_t;
1675
1676 static int
1677 tapall_tcpip_packet(void *pct, packet_info *pinfo, epan_dissect_t *edt _U_, const void *vip)
1678 {
1679         static struct segment *segment=NULL;
1680         tcp_scan_t *ts=(tcp_scan_t *)pct;
1681         struct tcpheader *tcphdr=(struct tcpheader *)vip;
1682
1683         if(!segment){
1684                 segment=g_malloc(sizeof (struct segment));
1685         }
1686
1687
1688         if (compare_headers(&ts->current->ip_src, &ts->current->ip_dst,
1689                             ts->current->th_sport, ts->current->th_dport,
1690                             &tcphdr->ip_src, &tcphdr->ip_dst,
1691                             tcphdr->th_sport, tcphdr->th_dport,
1692                             ts->direction)) {
1693                 segment->next = NULL;
1694                 segment->num = pinfo->fd->num;
1695                 segment->rel_secs = (guint32) pinfo->fd->rel_ts.secs;
1696                 segment->rel_usecs = pinfo->fd->rel_ts.nsecs/1000;
1697                 segment->abs_secs = (guint32) pinfo->fd->abs_ts.secs;
1698                 segment->abs_usecs = pinfo->fd->abs_ts.nsecs/1000;
1699                 segment->th_seq=tcphdr->th_seq;
1700                 segment->th_ack=tcphdr->th_ack;
1701                 segment->th_win=tcphdr->th_win;
1702                 segment->th_flags=tcphdr->th_flags;
1703                 segment->th_sport=tcphdr->th_sport;
1704                 segment->th_dport=tcphdr->th_dport;
1705                 segment->th_seglen=tcphdr->th_seglen;
1706                 COPY_ADDRESS(&segment->ip_src, &tcphdr->ip_src);
1707                 COPY_ADDRESS(&segment->ip_dst, &tcphdr->ip_dst);
1708                 if (ts->g->segments) {
1709                         ts->last->next = segment;
1710                 } else {
1711                         ts->g->segments = segment;
1712                 }
1713                 ts->last = segment;
1714                 if(pinfo->fd->num==ts->current->num){
1715                         ts->g->current = segment;
1716                 }
1717
1718                 segment=NULL;
1719         }
1720
1721         return 0;
1722 }
1723
1724
1725
1726 /* here we collect all the external data we will ever need */
1727 static void graph_segment_list_get (struct graph *g)
1728 {
1729         struct segment current;
1730         GString *error_string;
1731         tcp_scan_t ts;
1732
1733
1734         debug(DBS_FENTRY) puts ("graph_segment_list_get()");
1735         select_tcpip_session (&cfile, &current);
1736         if (g->type == GRAPH_THROUGHPUT)
1737                 ts.direction = COMPARE_CURR_DIR;
1738         else
1739                 ts.direction = COMPARE_ANY_DIR;
1740
1741         /* rescan all the packets and pick up all interesting tcp headers.
1742          * we only filter for TCP here for speed and do the actual compare
1743          * in the tap listener
1744          */
1745         ts.current=&current;
1746         ts.g=g;
1747         ts.last=NULL;
1748         error_string=register_tap_listener("tcp", &ts, "tcp", 0, NULL, tapall_tcpip_packet, NULL);
1749         if(error_string){
1750                 fprintf(stderr, "wireshark: Couldn't register tcp_graph tap: %s\n",
1751                     error_string->str);
1752                 g_string_free(error_string, TRUE);
1753                 exit(1);
1754         }
1755         cf_retap_packets(&cfile);
1756         remove_tap_listener(&ts);
1757 }
1758
1759
1760 typedef struct _th_t {
1761         int num_hdrs;
1762         struct tcpheader *tcphdr;
1763 } th_t;
1764
1765 static int
1766 tap_tcpip_packet(void *pct, packet_info *pinfo _U_, epan_dissect_t *edt _U_, const void *vip)
1767 {
1768         th_t *th=pct;
1769
1770         th->num_hdrs++;
1771         th->tcphdr=(struct tcpheader *)vip;
1772
1773         return 0;
1774 }
1775
1776
1777
1778 /* XXX should be enhanced so that if we have multiple TCP layers in the trace
1779  * then present the user with a dialog where the user can select WHICH tcp
1780  * session to graph.
1781  */
1782 static struct tcpheader *select_tcpip_session (capture_file *cf, struct segment *hdrs)
1783 {
1784         frame_data *fdata;
1785         gint err;
1786         gchar *err_info;
1787         epan_dissect_t edt;
1788         dfilter_t *sfcode;
1789         GString *error_string;
1790         th_t th = {0, NULL};
1791
1792         fdata = cf->current_frame;
1793
1794         /* no real filter yet */
1795         if (!dfilter_compile("tcp", &sfcode)) {
1796                 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "%s", dfilter_error_msg);
1797                 return NULL;
1798         }
1799
1800         /* dissect the current frame */
1801         if (!cf_read_frame(cf, fdata, &err, &err_info)) {
1802                 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
1803                         cf_read_error_message(err, err_info), cf->filename);
1804                 return NULL;
1805         }
1806
1807
1808         error_string=register_tap_listener("tcp", &th, NULL, 0, NULL, tap_tcpip_packet, NULL);
1809         if(error_string){
1810                 fprintf(stderr, "wireshark: Couldn't register tcp_graph tap: %s\n",
1811                     error_string->str);
1812                 g_string_free(error_string, TRUE);
1813                 exit(1);
1814         }
1815
1816         epan_dissect_init(&edt, TRUE, FALSE);
1817         epan_dissect_prime_dfilter(&edt, sfcode);
1818         tap_queue_init(&edt);
1819         epan_dissect_run(&edt, &cf->pseudo_header, cf->pd, fdata, NULL);
1820         tap_push_tapped_queue(&edt);
1821         epan_dissect_cleanup(&edt);
1822         remove_tap_listener(&th);
1823
1824         if(th.num_hdrs==0){
1825                 /* This "shouldn't happen", as our menu items shouldn't
1826                  * even be enabled if the selected packet isn't a TCP
1827                  * segment, as tcp_graph_selected_packet_enabled() is used
1828                  * to determine whether to enable any of our menu items. */
1829                 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
1830                     "Selected packet isn't a TCP segment");
1831                 return NULL;
1832         }
1833         /* XXX fix this later, we should show a dialog allowing the user
1834            to select which session he wants here
1835          */
1836         if(th.num_hdrs>1){
1837                 /* can only handle a single tcp layer yet */
1838                 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
1839                     "The selected packet has more than one TCP"
1840                     "header in it.");
1841                 return NULL;
1842         }
1843
1844         hdrs->num = fdata->num;
1845         hdrs->rel_secs = (guint32) fdata->rel_ts.secs;
1846         hdrs->rel_usecs = fdata->rel_ts.nsecs/1000;
1847         hdrs->abs_secs = (guint32) fdata->abs_ts.secs;
1848         hdrs->abs_usecs = fdata->abs_ts.nsecs/1000;
1849         hdrs->th_seq=th.tcphdr->th_seq;
1850         hdrs->th_ack=th.tcphdr->th_ack;
1851         hdrs->th_win=th.tcphdr->th_win;
1852         hdrs->th_flags=th.tcphdr->th_flags;
1853         hdrs->th_sport=th.tcphdr->th_sport;
1854         hdrs->th_dport=th.tcphdr->th_dport;
1855         hdrs->th_seglen=th.tcphdr->th_seglen;
1856         COPY_ADDRESS(&hdrs->ip_src, &th.tcphdr->ip_src);
1857         COPY_ADDRESS(&hdrs->ip_dst, &th.tcphdr->ip_dst);
1858         return th.tcphdr;
1859
1860 }
1861
1862 static int compare_headers (address *saddr1, address *daddr1, guint16 sport1, guint16 dport1, address *saddr2, address *daddr2, guint16 sport2, guint16 dport2, int dir)
1863 {
1864         int dir1, dir2;
1865
1866         dir1 = ((!(CMP_ADDRESS(saddr1, saddr2))) &&
1867                 (!(CMP_ADDRESS(daddr1, daddr2))) &&
1868                 (sport1==sport2) &&
1869                 (dport1==dport2));
1870
1871         if(dir==COMPARE_CURR_DIR){
1872                 return dir1;
1873         } else {
1874                 dir2 = ((!(CMP_ADDRESS(saddr1, daddr2))) &&
1875                         (!(CMP_ADDRESS(daddr1, saddr2))) &&
1876                         (sport1==dport2) &&
1877                         (dport1==sport2));
1878                 return dir1 || dir2;
1879         }
1880 }
1881
1882 static void graph_segment_list_free (struct graph *g)
1883 {
1884         struct segment *segment;
1885
1886         while (g->segments) {
1887                 segment = g->segments->next;
1888                 g_free (g->segments);
1889                 g->segments = segment;
1890         }
1891         g->segments = NULL;
1892 }
1893
1894 static void graph_element_lists_initialize (struct graph *g)
1895 {
1896         g->elists = (struct element_list *)g_malloc0 (sizeof (struct element_list));
1897 }
1898
1899 static void graph_element_lists_make (struct graph *g)
1900 {
1901         debug(DBS_FENTRY) puts ("graph_element_lists_make()");
1902
1903         switch (g->type) {
1904         case GRAPH_TSEQ_STEVENS:
1905                 tseq_stevens_make_elmtlist (g);
1906                 break;
1907         case GRAPH_TSEQ_TCPTRACE:
1908                 tseq_tcptrace_make_elmtlist (g);
1909                 break;
1910         case GRAPH_THROUGHPUT:
1911                 tput_make_elmtlist (g);
1912                 break;
1913         case GRAPH_RTT:
1914                 rtt_make_elmtlist (g);
1915                 break;
1916         default:
1917                 printf ("graph_element_lists_make: unknown graph type: %d\n", g->type);
1918                 break;
1919         }
1920 }
1921
1922 static void graph_element_lists_free (struct graph *g)
1923 {
1924         struct element_list *list, *next_list;
1925
1926 #if 0
1927         for (list=g->elists; list; list=list->next)
1928                 g_free (list->elements);
1929         while (g->elists->next) {
1930                 list = g->elists->next->next;
1931                 g_free (g->elists->next);
1932                 g->elists->next = list;
1933         }
1934 #endif
1935
1936         for (list=g->elists; list; list=next_list) {
1937                 g_free (list->elements);
1938                 next_list = list->next;
1939                 g_free (list);
1940         }
1941         g->elists = NULL;       /* just to make debugging easier */
1942 }
1943
1944 static void graph_title_pixmap_create (struct graph *g)
1945 {
1946         if (g->title_pixmap)
1947                 gdk_pixmap_unref (g->title_pixmap);
1948
1949         g->title_pixmap = gdk_pixmap_new (g->drawing_area->window,
1950                                                         g->x_axis->p.width, g->wp.y, -1);
1951 }
1952
1953 static void graph_title_pixmap_draw (struct graph *g)
1954 {
1955         int i;
1956
1957         gdk_draw_rectangle(g->title_pixmap, g->bg_gc, TRUE, 0, 0,
1958                            g->x_axis->p.width, g->wp.y);
1959         for (i=0; g->title[i]; i++) {
1960                 gint w, h;
1961                 PangoLayout *layout;
1962                 layout = gtk_widget_create_pango_layout(g->drawing_area,
1963                                                         g->title[i]);
1964                 pango_layout_get_pixel_size(layout, &w, &h);
1965                 gdk_draw_layout(g->title_pixmap, g->fg_gc,
1966                                 g->wp.width/2 - w/2, 20 + i*(h+3), layout);
1967                 g_object_unref(G_OBJECT(layout));
1968         }
1969 }
1970
1971 static void graph_title_pixmap_display (struct graph *g)
1972 {
1973         gdk_draw_pixmap (g->drawing_area->window, g->fg_gc, g->title_pixmap,
1974                          0, 0, g->wp.x, 0, g->x_axis->p.width, g->wp.y);
1975 }
1976
1977 static void graph_pixmaps_create (struct graph *g)
1978 {
1979         debug(DBS_FENTRY) puts ("graph_pixmaps_create()");
1980
1981         if (g->pixmap[0])
1982                 gdk_pixmap_unref (g->pixmap[0]);
1983         if (g->pixmap[1])
1984                 gdk_pixmap_unref (g->pixmap[1]);
1985
1986         g->pixmap[0] = gdk_pixmap_new (g->drawing_area->window,
1987                                                                         g->wp.width, g->wp.height, -1);
1988         g->pixmap[1] = gdk_pixmap_new (g->drawing_area->window,
1989                                                                         g->wp.width, g->wp.height, -1);
1990
1991         g->displayed = 0;
1992 }
1993
1994 static void graph_display (struct graph *g)
1995 {
1996         set_busy_cursor (g->drawing_area->window);
1997         graph_pixmap_draw (g);
1998         unset_busy_cursor (g->drawing_area->window);
1999         graph_pixmaps_switch (g);
2000         graph_pixmap_display (g);
2001 }
2002
2003 static void graph_pixmap_display (struct graph *g)
2004 {
2005     gdk_draw_pixmap (g->drawing_area->window, g->fg_gc,
2006                                         g->pixmap[g->displayed], 0, 0, g->wp.x, g->wp.y,
2007                                         g->wp.width, g->wp.height);
2008     if (g->cross.erase_needed) {
2009        cross_xor(g, g->cross.x, g->cross.y);
2010     }
2011 }
2012
2013 static void graph_pixmaps_switch (struct graph *g)
2014 {
2015         g->displayed = 1 ^ g->displayed;
2016 }
2017
2018 static void graph_pixmap_draw (struct graph *g)
2019 {
2020         struct element_list *list;
2021         struct element *e;
2022         int not_disp;
2023
2024         debug(DBS_FENTRY) puts ("graph_display()");
2025         not_disp = 1 ^ g->displayed;
2026
2027         gdk_draw_rectangle (g->pixmap[not_disp], g->bg_gc, TRUE,
2028                                                         0, 0, g->wp.width, g->wp.height);
2029         for (list=g->elists; list; list=list->next)
2030                 for (e=list->elements; e->type != ELMT_NONE; e++) {
2031                         switch (e->type) {
2032                         case ELMT_RECT:
2033                                 break;
2034                         case ELMT_LINE:
2035                                 draw_element_line (g, e);
2036                                 break;
2037                         case ELMT_ARC:
2038                                 draw_element_arc (g, e);
2039                                 break;
2040                         default:
2041                                 break;
2042                         }
2043                 }
2044 }
2045
2046 static void draw_element_line (struct graph *g, struct element *e)
2047 {
2048         int xx1, xx2, yy1, yy2;
2049
2050         debug(DBS_GRAPH_DRAWING) printf ("line element: (%.2f,%.2f)->(%.2f,%.2f), "
2051                                 "seg %d ... ", e->p.line.dim.x1, e->p.line.dim.y1,
2052                                 e->p.line.dim.x2, e->p.line.dim.y2, e->parent->num);
2053         xx1 = (int )rint (e->p.line.dim.x1 + g->geom.x - g->wp.x);
2054         xx2 = (int )rint (e->p.line.dim.x2 + g->geom.x - g->wp.x);
2055         yy1 = (int )rint ((g->geom.height-1-e->p.line.dim.y1) + g->geom.y-g->wp.y);
2056         yy2 = (int )rint ((g->geom.height-1-e->p.line.dim.y2) + g->geom.y-g->wp.y);
2057         if (xx1 > xx2) {
2058                 int tmp=xx2;
2059                 xx2=xx1;
2060                 xx1=tmp;
2061         }
2062         if (yy1 > yy2) {
2063                 int tmp=yy2;
2064                 yy2=yy1;
2065                 yy1=tmp;
2066         }
2067         if ((xx1<0 && xx2<0) || (xx1>=g->wp.width && xx2>=g->wp.width) ||
2068                                 (yy1<0 && yy2<0) || (yy1>=g->wp.height && yy2>=g->wp.height)) {
2069                 debug(DBS_GRAPH_DRAWING) printf (" refusing: (%d,%d)->(%d,%d)\n",
2070                                                                         xx1, yy1, xx2, yy2);
2071                 return;
2072         }
2073         if (xx2 > g->wp.width-1)
2074                 xx2 = g->wp.width-1;
2075         if (xx1 < 0)
2076                 xx1 = 0;
2077         if (yy2 > g->wp.height-1)
2078                 yy2 = g->wp.height-1;
2079         if (yy1 < 0)
2080                 yy1 = 0;
2081         debug(DBS_GRAPH_DRAWING) printf ("line: (%d,%d)->(%d,%d)\n", xx1, yy1, xx2,yy2);
2082         gdk_draw_line (g->pixmap[1^g->displayed], e->gc, xx1, yy1, xx2, yy2);
2083 }
2084
2085 static void draw_element_arc (struct graph *g, struct element *e)
2086 {
2087         int xx1, xx2, yy1, yy2;
2088
2089         xx1 = (int )rint (e->p.arc.dim.x + g->geom.x - g->wp.x);
2090         xx2 = (int )e->p.arc.dim.width;
2091         yy1 = (int )rint (g->geom.height-1 - e->p.arc.dim.y + g->geom.y - g->wp.y);
2092         yy2 = (int )e->p.arc.dim.height;
2093         if (xx1<-xx2 || xx1>=g->wp.width || yy1<-yy2 || yy1>=g->wp.height)
2094                 return;
2095         debug(DBS_GRAPH_DRAWING) printf ("arc: (%d,%d)->(%d,%d)\n", xx1, yy1, xx2, yy2);
2096         gdk_draw_arc (g->pixmap[1^g->displayed], e->gc, e->p.arc.filled, xx1,
2097                                         yy1, xx2, yy2, e->p.arc.angle1, e->p.arc.angle2);
2098 }
2099
2100 static void axis_pixmaps_create (struct axis *axis)
2101 {
2102         debug(DBS_FENTRY) puts ("axis_pixmaps_create()");
2103         if (axis->pixmap[0])
2104                 gdk_pixmap_unref (axis->pixmap[0]);
2105         if (axis->pixmap[1])
2106                 gdk_pixmap_unref (axis->pixmap[1]);
2107
2108         axis->pixmap[0] = gdk_pixmap_new (axis->drawing_area->window,
2109                                                         axis->p.width, axis->p.height, -1);
2110         axis->pixmap[1] = gdk_pixmap_new (axis->drawing_area->window,
2111                                                         axis->p.width, axis->p.height, -1);
2112
2113         axis->displayed = 0;
2114 }
2115
2116 static void axis_destroy (struct axis *axis)
2117 {
2118         gdk_pixmap_unref (axis->pixmap[0]);
2119         gdk_pixmap_unref (axis->pixmap[1]);
2120         g_free ( (gpointer) (axis->label) );
2121 }
2122
2123 static void axis_display (struct axis *axis)
2124 {
2125         if (axis->flags & AXIS_ORIENTATION)
2126                 h_axis_pixmap_draw (axis);
2127         else
2128                 v_axis_pixmap_draw (axis);
2129         axis_pixmaps_switch (axis);
2130         axis_pixmap_display (axis);
2131 }
2132
2133 static void v_axis_pixmap_draw (struct axis *axis)
2134 {
2135         struct graph *g = axis->g;
2136         int i;
2137         double major_tick;
2138         int not_disp, rdigits, offset, imin, imax;
2139         double bottom, top, j, fl, corr;
2140         PangoLayout *layout;
2141
2142         debug(DBS_FENTRY) puts ("v_axis_pixmap_draw()");
2143         bottom = (g->geom.height - (g->wp.height + g->wp.y + (-g->geom.y))) /
2144                                         (double )g->geom.height * g->bounds.height;
2145         bottom += axis->min;
2146         top = (g->geom.height - (g->wp.y + (-g->geom.y))) /
2147                                         (double )g->geom.height * g->bounds.height;
2148         top += axis->min;
2149         axis_compute_ticks (axis, bottom, top, AXIS_VERTICAL);
2150
2151         j = axis->major - floor (axis->major);
2152         for (rdigits=0; rdigits<=6; rdigits++) {
2153                 j *= 10;
2154                 if (j<=0.000001)
2155                         break;
2156                 j = j - floor (j);
2157         }
2158
2159         not_disp = 1 ^ axis->displayed;
2160         gdk_draw_rectangle (axis->pixmap[not_disp], g->bg_gc, TRUE, 0, 0,
2161                                         axis->p.width, axis->p.height);
2162         /* axis */
2163         gdk_draw_line (axis->pixmap[not_disp], g->fg_gc, axis->p.width - 1,
2164                         (gint) ((axis->p.height-axis->s.height)/2.0), axis->s.width - 1,
2165                         axis->p.height);
2166
2167         offset = g->wp.y + (-g->geom.y);
2168         fl = floor (axis->min / axis->major) * axis->major;
2169         corr = rint ((axis->min - fl) * g->zoom.y);
2170
2171         /* major ticks */
2172         major_tick = axis->major * g->zoom.y;
2173         imin = (int) ((g->geom.height - offset + corr - g->wp.height) / major_tick + 1);
2174         imax = (int) ((g->geom.height - offset + corr) / major_tick);
2175         for (i=imin; i <= imax; i++) {
2176                 gint w, h;
2177                 char desc[32];
2178                 int y = (int) (g->geom.height-1 - (int )rint (i * major_tick) -
2179                                                 offset + corr + axis->s.y);
2180
2181                 debug(DBS_AXES_DRAWING) printf("%f @ %d\n",
2182                                                i*axis->major + fl, y);
2183                 if (y < 0 || y > axis->p.height)
2184                         continue;
2185                 gdk_draw_line (axis->pixmap[not_disp], g->fg_gc,
2186                                axis->s.width - 15, y, axis->s.width - 1, y);
2187                 g_snprintf (desc, sizeof(desc), "%.*f", rdigits, i*axis->major + fl);
2188                 layout = gtk_widget_create_pango_layout(g->drawing_area, desc);
2189                 pango_layout_get_pixel_size(layout, &w, &h);
2190                 gdk_draw_layout(axis->pixmap[not_disp], g->fg_gc,
2191                                 axis->s.width-14-4-w, y - h/2, layout);
2192                 g_object_unref(G_OBJECT(layout));
2193         }
2194         /* minor ticks */
2195         if (axis->minor) {
2196                 double minor_tick = axis->minor * g->zoom.y;
2197                 imin = (int) ((g->geom.height - offset + corr - g->wp.height)/minor_tick + 1);
2198                 imax = (int) ((g->geom.height - offset + corr) / minor_tick);
2199                 for (i=imin; i <= imax; i++) {
2200                         int y = (int) (g->geom.height-1 - (int )rint (i*minor_tick) -
2201                                                         offset + corr + axis->s.y);
2202
2203                         debug (DBS_AXES_DRAWING) printf ("%f @ %d\n", i*axis->minor+fl, y);
2204                         if (y > 0 && y < axis->p.height)
2205                                 gdk_draw_line (axis->pixmap[not_disp], g->fg_gc,
2206                                                axis->s.width - 8, y,
2207                                                axis->s.width - 1, y);
2208                 }
2209         }
2210         for (i=0; axis->label[i]; i++) {
2211                 gint w, h;
2212                 layout = gtk_widget_create_pango_layout(g->drawing_area,
2213                                                         axis->label[i]);
2214                 pango_layout_get_pixel_size(layout, &w, &h);
2215                 gdk_draw_layout(axis->pixmap[not_disp], g->fg_gc,
2216                                 (axis->p.width - w)/2,
2217                                 TITLEBAR_HEIGHT-10 - i*(h+3) - h,
2218                                 layout);
2219                 g_object_unref(G_OBJECT(layout));
2220         }
2221 }
2222
2223 static void h_axis_pixmap_draw (struct axis *axis)
2224 {
2225         struct graph *g = axis->g;
2226         int i;
2227         double major_tick, minor_tick;
2228         int not_disp, rdigits, offset, imin, imax;
2229         double left, right, j, fl, corr;
2230         PangoLayout *layout;
2231
2232         debug(DBS_FENTRY) puts ("h_axis_pixmap_draw()");
2233         left = (g->wp.x-g->geom.x) /
2234                                         (double )g->geom.width * g->bounds.width;
2235         left += axis->min;
2236         right = (g->wp.x-g->geom.x+g->wp.width) /
2237                                         (double )g->geom.width * g->bounds.width;
2238         right += axis->min;
2239         axis_compute_ticks (axis, left, right, AXIS_HORIZONTAL);
2240
2241         j = axis->major - floor (axis->major);
2242         for (rdigits=0; rdigits<=6; rdigits++) {
2243                 j *= 10;
2244                 if (j<=0.000001)
2245                         break;
2246                 j = j - floor (j);
2247         }
2248
2249         not_disp = 1 ^ axis->displayed;
2250         gdk_draw_rectangle (axis->pixmap[not_disp], g->bg_gc, TRUE, 0, 0,
2251                                         axis->p.width, axis->p.height);
2252         /* axis */
2253         gdk_draw_line (axis->pixmap[not_disp], g->fg_gc, 0, 0,
2254                                                 (gint) (axis->s.width + (axis->p.width-axis->s.width)/2.0), 0);
2255         offset = g->wp.x - g->geom.x;
2256
2257         fl = floor (axis->min / axis->major) * axis->major;
2258         corr = rint ((axis->min - fl) * g->zoom.x);
2259
2260         /* major ticks */
2261         major_tick = axis->major*g->zoom.x;
2262         imin = (int) ((offset + corr) / major_tick + 1);
2263         imax = (int) ((offset + corr + axis->s.width) / major_tick);
2264         for (i=imin; i <= imax; i++) {
2265                 char desc[32];
2266                 int w, h;
2267                 int x = (int ) (rint (i * major_tick) - offset - corr);
2268
2269                 /* printf ("%f @ %d\n", i*axis->major + fl, x); */
2270                 if (x < 0 || x > axis->s.width)
2271                         continue;
2272                 gdk_draw_line (axis->pixmap[not_disp], g->fg_gc, x, 0, x, 15);
2273                 g_snprintf (desc, sizeof(desc), "%.*f", rdigits, i*axis->major + fl);
2274                 layout = gtk_widget_create_pango_layout(g->drawing_area, desc);
2275                 pango_layout_get_pixel_size(layout, &w, &h);
2276                 gdk_draw_layout(axis->pixmap[not_disp], g->fg_gc,
2277                                 x - w/2, 15+4, layout);
2278                 g_object_unref(G_OBJECT(layout));
2279         }
2280         if (axis->minor > 0) {
2281                 /* minor ticks */
2282                 minor_tick = axis->minor*g->zoom.x;
2283                 imin = (int) ((offset + corr) / minor_tick + 1);
2284                 imax = (int) ((offset + corr + g->wp.width) / minor_tick);
2285                 for (i=imin; i <= imax; i++) {
2286                         int x = (int) (rint (i * minor_tick) - offset - corr);
2287                         if (x > 0 && x < axis->s.width)
2288                                 gdk_draw_line (axis->pixmap[not_disp], g->fg_gc, x, 0, x, 8);
2289                 }
2290         }
2291         for (i=0; axis->label[i]; i++) {
2292                 gint w, h;
2293                 layout = gtk_widget_create_pango_layout(g->drawing_area,
2294                                                         axis->label[i]);
2295                 pango_layout_get_pixel_size(layout, &w, &h);
2296                 gdk_draw_layout(axis->pixmap[not_disp], g->fg_gc,
2297                                 axis->s.width - w - 50, 15+h+15 + i*(h+3),
2298                                 layout);
2299                 g_object_unref(G_OBJECT(layout));
2300         }
2301 }
2302
2303 static void axis_pixmaps_switch (struct axis *axis)
2304 {
2305         axis->displayed = 1 ^ axis->displayed;
2306 }
2307
2308 static void axis_pixmap_display (struct axis *axis)
2309 {
2310         gdk_draw_pixmap (axis->drawing_area->window, axis->g->fg_gc,
2311                         axis->pixmap[axis->displayed], 0, 0, axis->p.x, axis->p.y,
2312                         axis->p.width, axis->p.height);
2313 }
2314
2315 static void axis_compute_ticks (struct axis *axis, double x0, double xmax, int dir)
2316 {
2317         int i, j, ii, jj, ms;
2318         double zoom, x, steps[3]={ 0.1, 0.5 };
2319         int dim, check_needed, diminished;
2320         double majthresh[2]={2.0, 3.0};
2321
2322         debug((DBS_FENTRY | DBS_AXES_TICKS)) puts ("axis_compute_ticks()");
2323         debug(DBS_AXES_TICKS)
2324                 printf ("x0=%f xmax=%f dir=%s\n", x0,xmax, dir?"VERTICAL":"HORIZONTAL");
2325
2326         zoom = axis_zoom_get (axis, dir);
2327         x = xmax-x0;
2328         for (i=-9; i<=12; i++) {
2329                 if (x / pow (10, i) < 1)
2330                         break;
2331         }
2332         --i;
2333         ms = (int )(x / pow (10, i));
2334
2335         if (ms > 5) {
2336                 j = 0;
2337                 ++i;
2338         } else if (ms > 2)
2339                 j = 1;
2340         else
2341                 j = 0;
2342
2343         axis->major = steps[j] * pow (10, i);
2344
2345         debug(DBS_AXES_TICKS) printf ("zoom=%.1f, x=%f -> i=%d -> ms=%d -> j=%d ->"
2346                         " axis->major=%f\n", zoom, x, i, ms, j, axis->major);
2347
2348         /* let's compute minor ticks */
2349         jj = j;
2350         ii = i;
2351         axis_ticks_down (&ii, &jj);
2352         axis->minor = steps[jj] * pow (10, ii);
2353         /* we don't want minors if they would be less than 10 pixels apart */
2354         if (axis->minor*zoom < 10) {
2355                 debug(DBS_AXES_TICKS) printf ("refusing axis->minor of %f: "
2356                                         "axis->minor*zoom == %f\n", axis->minor, axis->minor*zoom);
2357                 axis->minor = 0;
2358         }
2359
2360         check_needed = TRUE;
2361         diminished = FALSE;
2362         while (check_needed) {
2363                 check_needed = FALSE;
2364                 dim = get_label_dim (axis, dir, xmax);
2365                 debug(DBS_AXES_TICKS) printf ("axis->major==%.1f, axis->minor==%.1f =>"
2366                                 " axis->major*zoom/dim==%f, axis->minor*zoom/dim==%f\n",
2367                                 axis->major, axis->minor, axis->major*zoom/dim,
2368                                 axis->minor*zoom/dim);
2369
2370                 /* corrections: if majors are less than majthresh[dir] times label
2371                 * dimension apart, we need to use bigger ones */
2372                 if (axis->major*zoom / dim < majthresh[dir]) {
2373                         axis_ticks_up (&ii, &jj);
2374                         axis->minor = axis->major;
2375                         axis_ticks_up (&i, &j);
2376                         axis->major = steps[j] * pow (10, i);
2377                         check_needed = TRUE;
2378                         debug(DBS_AXES_TICKS) printf ("axis->major enlarged to %.1f\n",
2379                                                                                 axis->major);
2380                 }
2381                 /* if minor ticks are bigger than majthresh[dir] times label dimension,
2382                  * we could  promote them to majors as well */
2383                 if (axis->minor*zoom / dim > majthresh[dir] && !diminished) {
2384                         axis_ticks_down (&i, &j);
2385                         axis->major = axis->minor;
2386                         axis_ticks_down (&ii, &jj);
2387                         axis->minor = steps[jj] * pow (10, ii);
2388                         check_needed = TRUE;
2389                         diminished = TRUE;
2390
2391                         debug(DBS_AXES_TICKS) printf ("axis->minor diminished to %.1f\n",
2392                                                                                 axis->minor);
2393
2394                         if (axis->minor*zoom < 10) {
2395                                 debug(DBS_AXES_TICKS) printf ("refusing axis->minor of %f: "
2396                                         "axis->minor*zoom == %f\n", axis->minor, axis->minor*zoom);
2397                                 axis->minor = 0;
2398                         }
2399                 }
2400         }
2401
2402         debug(DBS_AXES_TICKS) printf ("corrected: axis->major == %.1f -> "
2403                                                         "axis->minor == %.1f\n", axis->major, axis->minor);
2404 }
2405
2406 static void axis_ticks_up (int *i, int *j)
2407 {
2408         (*j)++;
2409         if (*j>1) {
2410                 (*i)++;
2411                 *j=0;
2412         }
2413 }
2414
2415 static void axis_ticks_down (int *i, int *j)
2416 {
2417         (*j)--;
2418         if (*j<0) {
2419                 (*i)--;
2420                 *j=1;
2421         }
2422 }
2423
2424 static int get_label_dim (struct axis *axis, int dir, double label)
2425 {
2426         double y;
2427         char str[32];
2428         int rdigits, dim;
2429         PangoLayout *layout;
2430
2431          /* First, let's compute how many digits to the right of radix
2432          * we need to print */
2433         y = axis->major - floor (axis->major);
2434         for (rdigits=0; rdigits<=6; rdigits++) {
2435                 y *= 10;
2436                 if (y<=0.000001)
2437                         break;
2438                 y = y - floor (y);
2439         }
2440         g_snprintf (str, sizeof(str), "%.*f", rdigits, label);
2441         switch (dir) {
2442         case AXIS_HORIZONTAL:
2443                 layout = gtk_widget_create_pango_layout(axis->g->drawing_area,
2444                                                         str);
2445                 pango_layout_get_pixel_size(layout, &dim, NULL);
2446                 g_object_unref(G_OBJECT(layout));
2447                 break;
2448         case AXIS_VERTICAL:
2449                 layout = gtk_widget_create_pango_layout(axis->g->drawing_area,
2450                                                         str);
2451                 pango_layout_get_pixel_size(layout, NULL, &dim);
2452                 g_object_unref(G_OBJECT(layout));
2453                 break;
2454         default:
2455                 puts ("initialize axis: an axis must be either horizontal or vertical");
2456                 return -1;
2457         }
2458         return dim;
2459 }
2460
2461 static double axis_zoom_get (struct axis *axis, int dir)
2462 {
2463         switch (dir) {
2464         case AXIS_HORIZONTAL:
2465                 return axis->g->zoom.x;
2466         case AXIS_VERTICAL:
2467                 return axis->g->zoom.y;
2468         default:
2469                 return -1;
2470         }
2471 }
2472
2473 static void graph_select_segment (struct graph *g, int x, int y)
2474 {
2475         struct element_list *list;
2476         struct element *e;
2477         guint num = 0;
2478
2479         debug(DBS_FENTRY) puts ("graph_select_segment()");
2480
2481         x -= g->geom.x;
2482         y = g->geom.height-1 - (y - g->geom.y);
2483
2484         set_busy_cursor (g->drawing_area->window);
2485
2486         for (list=g->elists; list; list=list->next)
2487                 for (e=list->elements; e->type != ELMT_NONE; e++) {
2488                         switch (e->type) {
2489                         case ELMT_RECT:
2490                                 break;
2491                         case ELMT_LINE:
2492                                 if (line_detect_collision (e, x, y)) {
2493                                         num = e->parent->num;
2494                                 }
2495                         case ELMT_ARC:
2496                                 if (arc_detect_collision (e, x, y)) {
2497                                         num = e->parent->num;
2498                                 }
2499                                 break;
2500                         default:
2501                                 break;
2502                         }
2503                 }
2504
2505
2506         if (num) {
2507                 cf_goto_frame(&cfile, num);
2508         }
2509         unset_busy_cursor (g->drawing_area->window);
2510 }
2511
2512 static int line_detect_collision (struct element *e, int x, int y)
2513 {
2514         int xx1, yy1, xx2, yy2;
2515
2516         if (e->p.line.dim.x1 < e->p.line.dim.x2) {
2517                 xx1 = (int )rint (e->p.line.dim.x1);
2518                 xx2 = (int )rint (e->p.line.dim.x2);
2519         } else {
2520                 xx1 = (int )rint (e->p.line.dim.x2);
2521                 xx2 = (int )rint (e->p.line.dim.x1);
2522         }
2523         if (e->p.line.dim.y1 < e->p.line.dim.y2) {
2524                 yy1 = (int )rint (e->p.line.dim.y1);
2525                 yy2 = (int )rint (e->p.line.dim.y2);
2526         } else {
2527                 yy1 = (int )rint (e->p.line.dim.y2);
2528                 yy2 = (int )rint (e->p.line.dim.y1);
2529         }
2530         /*
2531         printf ("line: (%d,%d)->(%d,%d), clicked: (%d,%d)\n", xx1, yy1, xx2, yy2, x, y);
2532          */
2533         if ((xx1==x && xx2==x && yy1<=y && y<=yy2)||(yy1==y && yy2==y && xx1<=x && x<=xx2))
2534                 return TRUE;
2535         else
2536                 return FALSE;
2537 }
2538
2539 static int arc_detect_collision (struct element *e, int x, int y)
2540 {
2541         int xx1, yy1, xx2, yy2;
2542
2543         xx1 = (int )rint (e->p.arc.dim.x);
2544         xx2 = (int )rint (e->p.arc.dim.x + e->p.arc.dim.width);
2545         yy1 = (int )rint (e->p.arc.dim.y - e->p.arc.dim.height);
2546         yy2 = (int )rint (e->p.arc.dim.y);
2547         /*
2548         printf ("arc: (%d,%d)->(%d,%d), clicked: (%d,%d)\n", xx1, yy1, xx2, yy2, x, y);
2549          */
2550         if (xx1<=x && x<=xx2 && yy1<=y && y<=yy2)
2551                 return TRUE;
2552         else
2553                 return FALSE;
2554 }
2555
2556 static void cross_xor (struct graph *g, int x, int y)
2557 {
2558         if (x > g->wp.x && x < g->wp.x+g->wp.width &&
2559                                 y >= g->wp.y && y < g->wp.y+g->wp.height) {
2560                 gdk_draw_line (g->drawing_area->window, xor_gc, g->wp.x,
2561                                                 y, g->wp.x + g->wp.width, y);
2562                 gdk_draw_line (g->drawing_area->window, xor_gc, x,
2563                                                 g->wp.y, x, g->wp.y + g->wp.height);
2564         }
2565 }
2566
2567 static void cross_draw (struct graph *g, int x, int y)
2568 {
2569         cross_xor (g, x, y);
2570         g->cross.x = x;
2571         g->cross.y = y;
2572         g->cross.erase_needed = 1;
2573 }
2574
2575 static void cross_erase (struct graph *g)
2576 {
2577         cross_xor (g, g->cross.x, g->cross.y);
2578         g->cross.erase_needed = 0;
2579 }
2580
2581 static void magnify_create (struct graph *g, int x, int y)
2582 {
2583         struct graph *mg;
2584         struct element_list *list, *new_list;
2585         struct ipoint pos, offsetpos;
2586         GdkEvent *e=NULL;
2587
2588         mg = g->magnify.g = (struct graph * )g_malloc (sizeof (struct graph));
2589         memcpy ((void * )mg, (void * )g, sizeof (struct graph));
2590
2591         mg->toplevel = dlg_window_new("tcp graph magnify");
2592         mg->drawing_area = mg->toplevel;
2593         gtk_window_set_default_size(GTK_WINDOW(mg->toplevel), g->magnify.width, g->magnify.height);
2594         gtk_widget_set_events (mg->drawing_area, GDK_EXPOSURE_MASK
2595                         /*              | GDK_ENTER_NOTIFY_MASK */
2596                         /*              | GDK_ALL_EVENTS_MASK   */
2597                                         );
2598
2599         mg->wp.x = 0;
2600         mg->wp.y = 0;
2601         mg->wp.width = g->magnify.width;
2602         mg->wp.height = g->magnify.height;
2603         mg->geom.width = (int )rint (g->geom.width * g->magnify.zoom.x);
2604         mg->geom.height = (int )rint (g->geom.height * g->magnify.zoom.y);
2605         mg->zoom.x = (mg->geom.width - 1) / g->bounds.width;
2606         mg->zoom.y = (mg->geom.height- 1) / g->bounds.height;
2607
2608         /* in order to keep original element lists intact we need our own */
2609         graph_element_lists_initialize (mg);
2610         list = g->elists->next;
2611         new_list = mg->elists;
2612         for ( ; list; list=list->next) {
2613                 new_list->next =
2614                                 (struct element_list * )g_malloc (sizeof (struct element_list));
2615                 new_list = new_list->next;
2616                 new_list->next = NULL;
2617                 new_list->elements = NULL;
2618         }
2619         graph_element_lists_make (mg);
2620
2621         gdk_window_get_position (GTK_WIDGET (g->toplevel)->window, &pos.x, &pos.y);
2622         g->magnify.x = pos.x + x - g->magnify.width/2;
2623         g->magnify.y = pos.y + y - g->magnify.height/2;
2624         offsetpos.x = g->magnify.x + g->magnify.offset.x;
2625         offsetpos.x = offsetpos.x >= 0 ? offsetpos.x : 0;
2626         offsetpos.y = g->magnify.y + g->magnify.offset.y;
2627         offsetpos.y = offsetpos.y >= 0 ? offsetpos.y : 0;
2628         gtk_window_set_position (GTK_WINDOW(mg->drawing_area), GTK_WIN_POS_NONE);       
2629         magnify_get_geom (g, x, y);
2630
2631         gtk_widget_show (mg->drawing_area);
2632
2633         /* we need to wait for the first expose event before we start drawing */
2634         while (!gdk_events_pending ());
2635         do {
2636                 e = gdk_event_get ();
2637                 if (e) {
2638                         if (e->any.type == GDK_EXPOSE) {
2639                                 gdk_event_free (e);
2640                                 break;
2641                         }
2642                         gdk_event_free (e);
2643                 }
2644         } while (e);
2645
2646         mg->pixmap[0] = mg->pixmap[1] = NULL;
2647         graph_pixmaps_create (mg);
2648         magnify_draw (g);
2649         g->magnify.active = 1;
2650 }
2651
2652 static void magnify_move (struct graph *g, int x, int y)
2653 {
2654         struct ipoint pos, offsetpos;
2655
2656         gdk_window_get_position (GTK_WIDGET (g->toplevel)->window, &pos.x, &pos.y);
2657         g->magnify.x = pos.x + x - g->magnify.width/2;
2658         g->magnify.y = pos.y + y - g->magnify.height/2;
2659         offsetpos.x = g->magnify.x + g->magnify.offset.x;
2660         offsetpos.x = offsetpos.x >= 0 ? offsetpos.x : 0;
2661         offsetpos.y = g->magnify.y + g->magnify.offset.y;
2662         offsetpos.y = offsetpos.y >= 0 ? offsetpos.y : 0;
2663         magnify_get_geom (g, x, y);
2664         magnify_draw (g);
2665 }
2666
2667 static void magnify_destroy (struct graph *g)
2668 {
2669         struct element_list *list;
2670         struct graph *mg = g->magnify.g;
2671
2672         window_destroy (GTK_WIDGET (mg->drawing_area));
2673         gdk_pixmap_unref (mg->pixmap[0]);
2674         gdk_pixmap_unref (mg->pixmap[1]);
2675         for (list=mg->elists; list; list=list->next)
2676                 g_free (list->elements);
2677
2678         if (mg->elists) {
2679     while (mg->elists->next) {
2680       list = mg->elists->next->next;
2681       g_free (mg->elists->next);
2682       mg->elists->next = list;
2683     }
2684         }
2685         g_free (g->magnify.g);
2686         g->magnify.active = 0;
2687 }
2688
2689 static void magnify_get_geom (struct graph *g, int x, int y)
2690 {
2691         int posx, posy;
2692
2693         gdk_window_get_position (GTK_WIDGET (g->toplevel)->window, &posx, &posy);
2694
2695         g->magnify.g->geom.x = g->geom.x;
2696         g->magnify.g->geom.y = g->geom.y;
2697
2698         g->magnify.g->geom.x -=
2699                                 (int )rint ((g->magnify.g->geom.width - g->geom.width) *
2700                                 ((x-g->geom.x)/(double )g->geom.width));
2701         g->magnify.g->geom.y -=
2702                                 (int )rint ((g->magnify.g->geom.height - g->geom.height) *
2703                                 ((y-g->geom.y)/(double )g->geom.height));
2704
2705         /* we have coords of origin of graph relative to origin of g->toplevel.
2706          * now we need them to relate to origin of magnify window */
2707         g->magnify.g->geom.x -= (g->magnify.x - posx);
2708         g->magnify.g->geom.y -= (g->magnify.y - posy);
2709 }
2710
2711 static void magnify_draw (struct graph *g)
2712 {
2713         int not_disp = 1 ^ g->magnify.g->displayed;
2714
2715         graph_pixmap_draw (g->magnify.g);
2716         /* graph pixmap is almost ready, just add border */
2717         gdk_draw_line (g->magnify.g->pixmap[not_disp], g->fg_gc, 0, 0,
2718                                                 g->magnify.width - 1, 0);
2719         gdk_draw_line (g->magnify.g->pixmap[not_disp], g->fg_gc,
2720                         g->magnify.width - 1, 0, g->magnify.width - 1, g->magnify.height);
2721         gdk_draw_line (g->magnify.g->pixmap[not_disp], g->fg_gc, 0, 0,
2722                                                 0, g->magnify.height - 1);
2723         gdk_draw_line (g->magnify.g->pixmap[not_disp], g->fg_gc, 0,
2724                         g->magnify.height - 1, g->magnify.width - 1, g->magnify.height - 1);
2725
2726         graph_pixmaps_switch (g->magnify.g);
2727         graph_pixmap_display (g->magnify.g);
2728
2729 }
2730
2731 static gint configure_event (GtkWidget *widget, GdkEventConfigure *event)
2732 {
2733         struct graph *g = (struct graph *) g_object_get_data(G_OBJECT(widget), "graph");
2734         struct {
2735                 double x, y;
2736         } zoom;
2737         int cur_g_width, cur_g_height;
2738         int cur_wp_width, cur_wp_height;
2739
2740         debug(DBS_FENTRY) puts ("configure_event()");
2741
2742         cur_wp_width = g->wp.width;
2743         cur_wp_height = g->wp.height;
2744         g->wp.width = event->width - g->y_axis->p.width - RMARGIN_WIDTH;
2745         g->wp.height = event->height - g->x_axis->p.height - g->wp.y;
2746         g->x_axis->s.width = g->wp.width;
2747         g->x_axis->p.width = g->wp.width + RMARGIN_WIDTH;
2748         g->y_axis->p.height = g->wp.height + g->wp.y;
2749         g->y_axis->s.height = g->wp.height;
2750         g->x_axis->p.y = g->y_axis->p.height;
2751         zoom.x = (double )g->wp.width / cur_wp_width;
2752         zoom.y = (double )g->wp.height / cur_wp_height;
2753         cur_g_width = g->geom.width;
2754         cur_g_height = g->geom.height;
2755         g->geom.width = (int )rint (g->geom.width * zoom.x);
2756         g->geom.height = (int )rint (g->geom.height * zoom.y);
2757         g->zoom.x = (double )(g->geom.width - 1) / g->bounds.width;
2758         g->zoom.y = (double )(g->geom.height -1) / g->bounds.height;
2759         /* g->zoom.initial.x = g->zoom.x; */
2760         /* g->zoom.initial.y = g->zoom.y; */
2761
2762         g->geom.x = (int) (g->wp.x - (double )g->geom.width/cur_g_width *
2763                                                         (g->wp.x - g->geom.x));
2764         g->geom.y = (int) (g->wp.y - (double )g->geom.height/cur_g_height *
2765                                                         (g->wp.y - g->geom.y));
2766 #if 0
2767         printf ("configure: graph: (%d,%d), (%d,%d); viewport: (%d,%d), (%d,%d); "
2768                                 "zooms: (%f,%f)\n", g->geom.x, g->geom.y, g->geom.width,
2769                                 g->geom.height, g->wp.x, g->wp.y, g->wp.width, g->wp.height,
2770                                 g->zoom.x, g->zoom.y);
2771 #endif
2772
2773         update_zoom_spins (g);
2774         graph_element_lists_make (g);
2775         graph_pixmaps_create (g);
2776         graph_title_pixmap_create (g);
2777         axis_pixmaps_create (g->y_axis);
2778         axis_pixmaps_create (g->x_axis);
2779         /* we don't do actual drawing here; we leave it to expose handler */
2780         graph_pixmap_draw (g);
2781         graph_pixmaps_switch (g);
2782         graph_title_pixmap_draw (g);
2783         h_axis_pixmap_draw (g->x_axis);
2784         axis_pixmaps_switch (g->x_axis);
2785         v_axis_pixmap_draw (g->y_axis);
2786         axis_pixmaps_switch (g->y_axis);
2787         return TRUE;
2788 }
2789
2790 static gint expose_event (GtkWidget *widget, GdkEventExpose *event)
2791 {
2792         struct graph *g = (struct graph *) g_object_get_data(G_OBJECT(widget), "graph");
2793
2794         debug(DBS_FENTRY) puts ("expose_event()");
2795
2796         if (event->count)
2797                 return TRUE;
2798
2799         /* lower left corner */
2800         gdk_draw_rectangle (g->drawing_area->window, g->bg_gc, TRUE, 0,
2801                         g->wp.y + g->wp.height, g->y_axis->p.width, g->x_axis->p.height);
2802         /* right margin */
2803         gdk_draw_rectangle (g->drawing_area->window, g->bg_gc, TRUE,
2804                         g->wp.x + g->wp.width, g->wp.y, RMARGIN_WIDTH, g->wp.height);
2805
2806         graph_pixmap_display (g);
2807         graph_title_pixmap_display (g);
2808         axis_pixmap_display (g->x_axis);
2809         axis_pixmap_display (g->y_axis);
2810
2811         return TRUE;
2812 }
2813
2814 static void do_zoom_mouse (struct graph *g, GdkEventButton *event)
2815 {
2816         int cur_width = g->geom.width, cur_height = g->geom.height;
2817         struct { double x, y; } factor;
2818
2819         if (g->zoom.flags & ZOOM_OUT) {
2820                 if (g->zoom.flags & ZOOM_HLOCK)
2821                         factor.x = 1.0;
2822                 else
2823                         factor.x = 1 / g->zoom.step_x;
2824                 if (g->zoom.flags & ZOOM_VLOCK)
2825                         factor.y = 1.0;
2826                 else
2827                         factor.y = 1 / g->zoom.step_y;
2828         } else {
2829                 if (g->zoom.flags & ZOOM_HLOCK)
2830                         factor.x = 1.0;
2831                 else
2832                         factor.x = g->zoom.step_x;
2833                 if (g->zoom.flags & ZOOM_VLOCK)
2834                         factor.y = 1.0;
2835                 else
2836                         factor.y = g->zoom.step_y;
2837         }
2838
2839         g->geom.width = (int )rint (g->geom.width * factor.x);
2840         g->geom.height = (int )rint (g->geom.height * factor.y);
2841         if (g->geom.width < g->wp.width)
2842                 g->geom.width = g->wp.width;
2843         if (g->geom.height < g->wp.height)
2844                 g->geom.height = g->wp.height;
2845         g->zoom.x = (g->geom.width - 1) / g->bounds.width;
2846         g->zoom.y = (g->geom.height- 1) / g->bounds.height;
2847
2848         g->geom.x -= (int )rint ((g->geom.width - cur_width) *
2849                         ((event->x-g->geom.x)/(double )cur_width));
2850         g->geom.y -= (int )rint ((g->geom.height - cur_height) *
2851                         ((event->y-g->geom.y)/(double )cur_height));
2852
2853         if (g->geom.x > g->wp.x)
2854                 g->geom.x = g->wp.x;
2855         if (g->geom.y > g->wp.y)
2856                 g->geom.y = g->wp.y;
2857         if (g->wp.x + g->wp.width > g->geom.x + g->geom.width)
2858                 g->geom.x = g->wp.width + g->wp.x - g->geom.width;
2859         if (g->wp.y + g->wp.height > g->geom.y + g->geom.height)
2860                 g->geom.y = g->wp.height + g->wp.y - g->geom.height;
2861 #if 0
2862         printf ("button press: graph: (%d,%d), (%d,%d); viewport: (%d,%d), "
2863                         "(%d,%d); zooms: (%f,%f)\n", g->geom.x, g->geom.y,
2864                         g->geom.width, g->geom.height, g->wp.x, g->wp.y, g->wp.width,
2865                         g->wp.height, g->zoom.x, g->zoom.y);
2866 #endif
2867         graph_element_lists_make (g);
2868         g->cross.erase_needed = 0;
2869         graph_display (g);
2870         axis_display (g->y_axis);
2871         axis_display (g->x_axis);
2872         update_zoom_spins (g);
2873         if (g->cross.draw)
2874                 cross_draw (g, (int) event->x, (int) event->y);
2875 }
2876
2877 static void do_zoom_keyboard (struct graph *g)
2878 {
2879         int cur_width = g->geom.width, cur_height = g->geom.height;
2880         struct { double x, y; } factor;
2881         int pointer_x, pointer_y;
2882
2883         gdk_window_get_pointer (g->drawing_area->window, &pointer_x, &pointer_y, 0);
2884
2885         if (g->zoom.flags & ZOOM_OUT) {
2886                 if (g->zoom.flags & ZOOM_HLOCK)
2887                         factor.x = 1.0;
2888                 else
2889                         factor.x = 1 / g->zoom.step_x;
2890                 if (g->zoom.flags & ZOOM_VLOCK)
2891                         factor.y = 1.0;
2892                 else
2893                         factor.y = 1 / g->zoom.step_y;
2894         } else {
2895                 if (g->zoom.flags & ZOOM_HLOCK)
2896                         factor.x = 1.0;
2897                 else
2898                         factor.x = g->zoom.step_x;
2899                 if (g->zoom.flags & ZOOM_VLOCK)
2900                         factor.y = 1.0;
2901                 else
2902                         factor.y = g->zoom.step_y;
2903         }
2904
2905         g->geom.width = (int )rint (g->geom.width * factor.x);
2906         g->geom.height = (int )rint (g->geom.height * factor.y);
2907         if (g->geom.width < g->wp.width)
2908                 g->geom.width = g->wp.width;
2909         if (g->geom.height < g->wp.height)
2910                 g->geom.height = g->wp.height;
2911         g->zoom.x = (g->geom.width - 1) / g->bounds.width;
2912         g->zoom.y = (g->geom.height- 1) / g->bounds.height;
2913
2914         g->geom.x -= (int )rint ((g->geom.width - cur_width) *
2915                         ((pointer_x - g->geom.x)/(double )cur_width));
2916         g->geom.y -= (int )rint ((g->geom.height - cur_height) *
2917                         ((pointer_y - g->geom.y)/(double )cur_height));
2918
2919         if (g->geom.x > g->wp.x)
2920                 g->geom.x = g->wp.x;
2921         if (g->geom.y > g->wp.y)
2922                 g->geom.y = g->wp.y;
2923         if (g->wp.x + g->wp.width > g->geom.x + g->geom.width)
2924                 g->geom.x = g->wp.width + g->wp.x - g->geom.width;
2925         if (g->wp.y + g->wp.height > g->geom.y + g->geom.height)
2926                 g->geom.y = g->wp.height + g->wp.y - g->geom.height;
2927 #if 0
2928         printf ("key press: graph: (%d,%d), (%d,%d); viewport: (%d,%d), "
2929                         "(%d,%d); zooms: (%f,%f)\n", g->geom.x, g->geom.y,
2930                         g->geom.width, g->geom.height, g->wp.x, g->wp.y, g->wp.width,
2931                         g->wp.height, g->zoom.x, g->zoom.y);
2932 #endif
2933
2934         graph_element_lists_make (g);
2935         g->cross.erase_needed = 0;
2936         graph_display (g);
2937         axis_display (g->y_axis);
2938         axis_display (g->x_axis);
2939         update_zoom_spins (g);
2940         if (g->cross.draw)
2941                 cross_draw (g, pointer_x, pointer_y);
2942 }
2943
2944 static void do_zoom_in_keyboard (struct graph *g)
2945 {
2946         gtk_toggle_button_set_active (g->zoom.widget.in_toggle, TRUE);
2947         do_zoom_keyboard (g);
2948 }
2949
2950 static void do_zoom_out_keyboard (struct graph *g)
2951 {
2952         gtk_toggle_button_set_active (g->zoom.widget.out_toggle, TRUE);
2953         do_zoom_keyboard (g);
2954         gtk_toggle_button_set_active (g->zoom.widget.in_toggle, TRUE);
2955 }
2956
2957 static void do_select_segment (struct graph *g)
2958 {
2959         int pointer_x, pointer_y;
2960
2961         gdk_window_get_pointer (g->drawing_area->window, &pointer_x, &pointer_y, 0);
2962         graph_select_segment (g, pointer_x, pointer_y);
2963 }
2964
2965 static void do_rtt_graph (struct graph *g)
2966 {
2967         gtk_toggle_button_set_active (g->gt.graph_rtt, TRUE); 
2968 }
2969
2970 static void do_throughput_graph (struct graph *g)
2971 {
2972         gtk_toggle_button_set_active (g->gt.graph_tput, TRUE);
2973 }
2974
2975 static void do_ts_graph_stevens (struct graph *g)
2976 {
2977         gtk_toggle_button_set_active (g->gt.graph_tseqstevens, TRUE);
2978 }
2979
2980 static void do_ts_graph_tcptrace (struct graph *g)
2981 {
2982         gtk_toggle_button_set_active (g->gt.graph_tseqttrace, TRUE);
2983 }
2984
2985 static void do_magnify_create (struct graph *g)
2986 {
2987         int pointer_x, pointer_y;
2988
2989         gdk_window_get_pointer (g->drawing_area->window, &pointer_x, &pointer_y, 0);
2990
2991         magnify_create (g, (int )rint (pointer_x), (int )rint (pointer_y));
2992 }
2993
2994 static void do_key_motion (struct graph *g)
2995 {
2996         if (g->geom.x > g->wp.x)
2997                 g->geom.x = g->wp.x;
2998         if (g->geom.y > g->wp.y)
2999                 g->geom.y = g->wp.y;
3000         if (g->wp.x + g->wp.width > g->geom.x + g->geom.width)
3001                 g->geom.x = g->wp.width + g->wp.x - g->geom.width;
3002         if (g->wp.y + g->wp.height > g->geom.y + g->geom.height)
3003                 g->geom.y = g->wp.height + g->wp.y - g->geom.height;
3004         g->cross.erase_needed = 0;
3005         graph_display (g);
3006         axis_display (g->y_axis);
3007         axis_display (g->x_axis);
3008         if (g->cross.draw) {
3009                 int pointer_x, pointer_y;
3010                 gdk_window_get_pointer (g->drawing_area->window, &pointer_x, &pointer_y, 0);
3011                 cross_draw (g, pointer_x, pointer_y);
3012         }
3013 }
3014
3015 static void do_key_motion_up (struct graph *g, int step)
3016 {
3017         g->geom.y += step;
3018         do_key_motion (g);
3019 }
3020
3021 static void do_key_motion_down (struct graph *g, int step)
3022 {
3023         g->geom.y -= step;
3024         do_key_motion (g);
3025 }
3026
3027 static void do_key_motion_left (struct graph *g, int step)
3028 {
3029         g->geom.x += step;
3030         do_key_motion (g);
3031 }
3032
3033 static void do_key_motion_right (struct graph *g, int step)
3034 {
3035         g->geom.x -= step;
3036         do_key_motion (g);
3037 }
3038
3039 static gint button_press_event (GtkWidget *widget, GdkEventButton *event)
3040 {
3041         struct graph *g = (struct graph *) g_object_get_data(G_OBJECT(widget), "graph");
3042
3043         debug(DBS_FENTRY) puts ("button_press_event()");
3044
3045         if (event->button == MOUSE_BUTTON_RIGHT) {
3046                 if (event->state & GDK_CONTROL_MASK) {
3047                         magnify_create (g, (int )rint (event->x), (int )rint (event->y));
3048                 } else {
3049                         g->grab.x = (int )rint (event->x) - g->geom.x;
3050                         g->grab.y = (int )rint (event->y) - g->geom.y;
3051                         g->grab.grabbed = TRUE;
3052                 }
3053 #ifdef ORIGINAL_WIN32_BUTTONS
3054                                 /* Windows mouse control:        */
3055                                 /* [<ctrl>-left] - select packet */
3056                                 /* [left] - zoom in              */
3057                                 /* [<shift>-left] - zoom out     */
3058         } else if (event->button == MOUSE_BUTTON_LEFT) {
3059                 if (event->state & GDK_CONTROL_MASK) {
3060                         graph_select_segment (g, (int)event->x, (int)event->y);
3061                 } else {
3062 #else /* !ORIGINAL_WIN32_BUTTONS */
3063         } else if (event->button == MOUSE_BUTTON_MIDDLE) {
3064 #endif
3065                 do_zoom_mouse(g, event);
3066 #ifndef ORIGINAL_WIN32_BUTTONS
3067         } else if (event->button == MOUSE_BUTTON_LEFT) {
3068                 graph_select_segment (g, (int )event->x, (int )event->y);
3069 #else /* ORIGINAL_WIN32_BUTTONS*/
3070                 }
3071 #endif
3072         }
3073         return TRUE;
3074 }
3075
3076 static gint motion_notify_event (GtkWidget *widget, GdkEventMotion *event)
3077 {
3078         struct graph *g = (struct graph *) g_object_get_data(G_OBJECT(widget), "graph");
3079         int x, y;
3080         GdkModifierType state;
3081
3082         /* debug(DBS_FENTRY) puts ("motion_notify_event()"); */
3083
3084         if (event->is_hint)
3085                 gdk_window_get_pointer (event->window, &x, &y, &state);
3086         else {
3087                 x = (int) event->x;
3088                 y = (int) event->y;
3089                 state = event->state;
3090         }
3091
3092         /* Testing just (state & GDK_BUTTON1_MASK) is not enough since when button1
3093          * is pressed while pointer is in motion, we will receive one more motion
3094          * notify *before* we get the button press. This last motion notify works
3095          * with stale grab coordinates */
3096         if (state & GDK_BUTTON3_MASK) {
3097                 if (g->grab.grabbed) {
3098                         g->geom.x = x-g->grab.x;
3099                         g->geom.y = y-g->grab.y;
3100
3101                         if (g->geom.x > g->wp.x)
3102                                 g->geom.x = g->wp.x;
3103                         if (g->geom.y > g->wp.y)
3104                                 g->geom.y = g->wp.y;
3105                         if (g->wp.x + g->wp.width > g->geom.x + g->geom.width)
3106                                 g->geom.x = g->wp.width + g->wp.x - g->geom.width;
3107                         if (g->wp.y + g->wp.height > g->geom.y + g->geom.height)
3108                                 g->geom.y = g->wp.height + g->wp.y - g->geom.height;
3109                         g->cross.erase_needed = 0;
3110                         graph_display (g);
3111                         axis_display (g->y_axis);
3112                         axis_display (g->x_axis);
3113                         if (g->cross.draw)
3114                                 cross_draw (g, x, y);
3115                 } else if (g->magnify.active)
3116                         magnify_move (g, x, y);
3117         } else if (state & GDK_BUTTON1_MASK) {
3118                 graph_select_segment (g, x, y);
3119                 if (g->cross.erase_needed)
3120                         cross_erase (g);
3121                 if (g->cross.draw)
3122                         cross_draw (g, x, y);
3123         } else {
3124                 if (g->cross.erase_needed)
3125                         cross_erase (g);
3126                 if (g->cross.draw)
3127                         cross_draw (g, x, y);
3128         }
3129
3130         return TRUE;
3131 }
3132
3133 static gint button_release_event (GtkWidget *widget, GdkEventButton *event)
3134 {
3135         struct graph *g = (struct graph *) g_object_get_data(G_OBJECT(widget), "graph");
3136
3137         debug(DBS_FENTRY) puts ("button_release_event()");
3138
3139         if (event->button == MOUSE_BUTTON_RIGHT)
3140                 g->grab.grabbed = FALSE;
3141
3142         if (g->magnify.active)
3143                 magnify_destroy (g);
3144         return TRUE;
3145 }
3146
3147 static gint key_press_event (GtkWidget *widget, GdkEventKey *event)
3148 {
3149         struct graph *g = (struct graph *) g_object_get_data(G_OBJECT(widget), "graph");
3150         int step;
3151
3152         debug(DBS_FENTRY) puts ("key_press_event()");
3153
3154         if((event->state & GDK_CONTROL_MASK) && (event->state & GDK_SHIFT_MASK))
3155                 step = 0;
3156         else if (event->state & GDK_CONTROL_MASK)
3157                 step = 1;
3158         else if (event->state & GDK_SHIFT_MASK)
3159                 step = 10;
3160         else
3161                 step = 100;
3162
3163         switch (event->keyval) {
3164         case ' ':
3165                 toggle_crosshairs (g);
3166                 break;
3167         case 't':
3168                 toggle_time_origin (g);
3169                 break;
3170         case 's':
3171                 toggle_seq_origin (g);
3172                 break;
3173         case 'r':
3174         case GDK_Home:
3175                 restore_initial_graph_view (g);
3176                 break;
3177         case 'i':
3178         case '+':
3179                 do_zoom_in_keyboard (g);
3180                 break;
3181         case 'o':
3182         case '-':
3183                 do_zoom_out_keyboard (g);
3184                 break;
3185         case 'm':
3186                 do_magnify_create (g);
3187                 break;
3188         case 'g':
3189                 do_select_segment (g);
3190                 break;
3191         case '1':
3192                 do_rtt_graph (g);
3193                 break;
3194         case '2':
3195                 do_throughput_graph (g);
3196                 break;
3197         case '3':
3198                 do_ts_graph_stevens (g);
3199                 break;
3200         case '4':
3201                 do_ts_graph_tcptrace (g);
3202                 break;
3203         case GDK_Left:
3204                 do_key_motion_left (g, step);
3205                 break;
3206         case GDK_Up:
3207                 do_key_motion_up (g, step);
3208                 break;
3209         case GDK_Right:
3210                 do_key_motion_right (g, step);
3211                 break;
3212         case GDK_Down:
3213                 do_key_motion_down (g, step);
3214                 break;
3215         case GDK_F1:
3216                 callback_create_help (NULL, NULL);      
3217                 break;
3218         default:
3219                 break;
3220         }
3221         return TRUE;
3222 }
3223
3224 static gint key_release_event (GtkWidget *widget, GdkEventKey *event)
3225 {
3226         struct graph *g = (struct graph *) g_object_get_data(G_OBJECT(widget), "graph");
3227
3228         debug(DBS_FENTRY) puts ("key_release_event()");
3229
3230         if (event->keyval == GDK_Shift_L || event->keyval == GDK_ISO_Prev_Group) {
3231                 /* g->zoom.flags &= ~ZOOM_OUT; */
3232                 gtk_toggle_button_set_active (g->zoom.widget.in_toggle, TRUE);
3233         }
3234         return TRUE;
3235 }
3236
3237 static gint leave_notify_event (GtkWidget *widget, GdkEventCrossing *event _U_)
3238 {
3239         struct graph *g = (struct graph *) g_object_get_data(G_OBJECT(widget), "graph");
3240
3241         if (g->cross.erase_needed)
3242                 cross_erase (g);
3243
3244         return TRUE;
3245 }
3246
3247 static gint enter_notify_event (GtkWidget *widget, GdkEventCrossing *event _U_)
3248 {
3249         struct graph *g = (struct graph *) g_object_get_data(G_OBJECT(widget), "graph");
3250
3251         /* graph_pixmap_display (g); */
3252         if (g->cross.draw) {
3253                 int x, y;
3254                 gdk_window_get_pointer (g->drawing_area->window, &x, &y, 0);
3255                 cross_draw (g, x, y);
3256         }
3257         return TRUE;
3258 }
3259
3260 static void toggle_crosshairs (struct graph *g)
3261 {
3262         g->cross.draw ^= 1;
3263 #if 0
3264         if (g->cross.draw) {
3265                 int x, y;
3266                 gdk_window_get_pointer (g->drawing_area->window, &x, &y, 0);
3267                 cross_draw (g);
3268         } else if (g->cross.erase_needed) {
3269                 cross_erase (g);
3270         }
3271 #endif
3272         /* toggle buttons emit their "toggled" signals so don't bother doing
3273          * any real work here, it will be done in signal handlers */
3274         if (g->cross.draw)
3275                 gtk_toggle_button_set_active (g->cross.on_toggle, TRUE);
3276         else
3277                 gtk_toggle_button_set_active (g->cross.off_toggle, TRUE);
3278 }
3279
3280 static void toggle_time_origin (struct graph *g)
3281 {
3282         switch (g->type) {
3283         case GRAPH_TSEQ_STEVENS:
3284                 tseq_stevens_toggle_time_origin (g);
3285                 break;
3286         case GRAPH_TSEQ_TCPTRACE:
3287                 tseq_tcptrace_toggle_time_origin (g);
3288                 break;
3289         case GRAPH_THROUGHPUT:
3290                 tput_toggle_time_origin (g);
3291                 break;
3292         default:
3293                 break;
3294         }
3295         axis_display (g->x_axis);
3296 }
3297
3298 static void toggle_seq_origin (struct graph *g)
3299 {
3300         switch (g->type) {
3301         case GRAPH_TSEQ_STEVENS:
3302                 tseq_stevens_toggle_seq_origin (g);
3303                 axis_display (g->y_axis);
3304                 break;
3305         case GRAPH_TSEQ_TCPTRACE:
3306                 tseq_tcptrace_toggle_seq_origin (g);
3307                 axis_display (g->y_axis);
3308                 break;
3309         case GRAPH_RTT:
3310                 rtt_toggle_seq_origin (g);
3311                 axis_display (g->x_axis);
3312                 break;
3313         default:
3314                 break;
3315         }
3316 }
3317
3318 static void restore_initial_graph_view (struct graph *g)
3319 {
3320         g->geom.width = g->wp.width;
3321         g->geom.height = g->wp.height;
3322         g->geom.x = g->wp.x;
3323         g->geom.y = g->wp.y;
3324         graph_init_sequence (g);
3325 }
3326
3327 static int get_num_dsegs (struct graph *g)
3328 {
3329         int count;
3330         struct segment *tmp;
3331
3332         for (tmp=g->segments, count=0; tmp; tmp=tmp->next) {
3333                 if(compare_headers(&g->current->ip_src, &g->current->ip_dst,
3334                                    g->current->th_sport, g->current->th_dport,
3335                                    &tmp->ip_src, &tmp->ip_dst,
3336                                    tmp->th_sport, tmp->th_dport,
3337                                    COMPARE_CURR_DIR)) {
3338                         count++;
3339                 }
3340         }
3341         return count;
3342 }
3343
3344 static int get_num_acks (struct graph *g)
3345 {
3346         int count;
3347         struct segment *tmp;
3348
3349         for (tmp=g->segments, count=0; tmp; tmp=tmp->next) {
3350                 if(!compare_headers(&g->current->ip_src, &g->current->ip_dst,
3351                                    g->current->th_sport, g->current->th_dport,
3352                                    &tmp->ip_src, &tmp->ip_dst,
3353                                    tmp->th_sport, tmp->th_dport,
3354                                    COMPARE_CURR_DIR)) {
3355                         count++;
3356                 }
3357         }
3358         return count;
3359 }
3360
3361 /*
3362  * Stevens-style time-sequence grapH
3363  */
3364
3365 static void tseq_stevens_read_config (struct graph *g)
3366 {
3367         debug(DBS_FENTRY) puts ("tseq_stevens_read_config()");
3368
3369         g->s.tseq_stevens.seq_width = 4;
3370         g->s.tseq_stevens.seq_height = 4;
3371         g->s.tseq_stevens.flags = 0;
3372
3373         g->title = (const char ** )g_malloc (2 * sizeof (char *));
3374         g->title[0] = "Time/Sequence Graph (Stevens)";
3375         g->title[1] = NULL;
3376         g->y_axis->label = (const char ** )g_malloc (3 * sizeof (char * ));
3377         g->y_axis->label[0] = "number[B]";
3378         g->y_axis->label[1] = "Sequence";
3379         g->y_axis->label[2] = NULL;
3380         g->x_axis->label = (const char ** )g_malloc (2 * sizeof (char * ));
3381         g->x_axis->label[0] = "Time[s]";
3382         g->x_axis->label[1] = NULL;
3383 }
3384
3385 /* Used by both 'stevens' and 'tcptrace' */
3386 static void tseq_initialize (struct graph *g)
3387 {
3388         debug(DBS_FENTRY) puts ("tseq_initialize()");
3389         tseq_get_bounds (g);
3390
3391         g->x_axis->min = 0;
3392         g->y_axis->min = 0;
3393
3394         switch (g->type) {
3395         case GRAPH_TSEQ_STEVENS:
3396                 tseq_stevens_read_config(g);
3397                 break;
3398         case GRAPH_TSEQ_TCPTRACE:
3399                 tseq_tcptrace_read_config(g);
3400                 break;
3401         }
3402 }
3403
3404
3405 /* Determine "bounds"
3406  *  Essentially: look for lowest/highest time and seq in the list of segments
3407  *  Note that for tcptrace the "(ack + window) sequence number" would normally be expected
3408  *   to be the upper bound; However, just to be safe, include the data seg sequence numbers
3409  *   in the comparison for tcptrace
3410  *   (e.g. to handle the case of only data segments).
3411  */
3412
3413 /* ToDo: worry about handling cases such as trying to plot seq of just 1 frame  */
3414
3415 static void tseq_get_bounds (struct graph *g)
3416 {
3417         struct segment *tmp;
3418         double   tim;
3419         gboolean data_frame_seen=FALSE;
3420         double   data_tim_low=0;
3421         double   data_tim_high=0;
3422         guint32  data_seq_cur;
3423         guint32  data_seq_nxt;
3424         guint32  data_seq_low=0;
3425         guint32  data_seq_high=0;
3426         gboolean ack_frame_seen=FALSE;
3427         double   ack_tim_low=0;
3428         double   ack_tim_high=0;
3429         guint32  ack_seq_cur;
3430         guint32  ack_seq_low=0;
3431         guint32  win_seq_cur;
3432         guint32  win_seq_high=0;
3433
3434         /* go thru all segments to determine "bounds" */
3435         for (tmp=g->segments; tmp; tmp=tmp->next) {
3436                 if(compare_headers(&g->current->ip_src, &g->current->ip_dst,
3437                                    g->current->th_sport, g->current->th_dport,
3438                                    &tmp->ip_src, &tmp->ip_dst,
3439                                    tmp->th_sport, tmp->th_dport,
3440                                    COMPARE_CURR_DIR)) {
3441
3442                         /* "data" seg */
3443                         tim = tmp->rel_secs + tmp->rel_usecs / 1000000.0;
3444                         data_seq_cur = tmp->th_seq;
3445                         data_seq_nxt = data_seq_cur + tmp->th_seglen;
3446                         if (! data_frame_seen) {
3447                                 data_tim_low    = data_tim_high = tim;
3448                                 data_seq_low    = data_seq_cur;
3449                                 data_seq_high   = data_seq_nxt;
3450                                 data_frame_seen = TRUE;
3451                         }
3452                         if (tim          < data_tim_low)  data_tim_low  = tim;
3453                         if (tim          > data_tim_high) data_tim_high = tim;
3454                         if (data_seq_cur < data_seq_low)  data_seq_low  = data_seq_cur;
3455                         if (data_seq_nxt > data_seq_high) data_seq_high = data_seq_nxt;
3456                 }
3457                 else { /* ack seg */
3458                         /* skip ack processing if no ACK (e.g. in RST) */
3459                         if (TCP_ACK (tmp->th_flags)) {
3460                                 tim = tmp->rel_secs + tmp->rel_usecs / 1000000.0;
3461                                 ack_seq_cur = tmp->th_ack;
3462                                 win_seq_cur = ack_seq_cur + tmp->th_win;
3463                                 if (! ack_frame_seen) {
3464                                         ack_tim_low  = ack_tim_high = tim;
3465                                         ack_seq_low  = ack_seq_cur;
3466                                         win_seq_high = win_seq_cur;
3467                                         ack_frame_seen = TRUE;
3468                                 }
3469                                 if (tim         < ack_tim_low)  ack_tim_low  = tim;
3470                                 if (tim         > ack_tim_high) ack_tim_high = tim;
3471                                 if (ack_seq_cur < ack_seq_low)  ack_seq_low  = ack_seq_cur;
3472                                 if (win_seq_cur > win_seq_high) win_seq_high = win_seq_cur;
3473                         }
3474                 }
3475         }
3476
3477         /* if 'stevens':  use only data segments to determine bounds         */
3478         /* if 'tcptrace': use both data and ack segments to determine bounds */
3479         switch (g->type) {
3480         case GRAPH_TSEQ_STEVENS:
3481                 g->bounds.x0     = data_tim_low;
3482                 g->bounds.width  = data_tim_high - data_tim_low;
3483                 g->bounds.y0     = data_seq_low;
3484                 g->bounds.height = data_seq_high - data_seq_low;
3485                 break;
3486         case GRAPH_TSEQ_TCPTRACE:
3487                 /* If (ack_frame_seen == false) -> use 'data' segments.
3488                  * Else If (data_frame_seen == false) -> use 'ack' segments.
3489                  * Else -> use both data and ack segments.
3490                  */
3491                 g->bounds.x0     =  ((data_tim_low <= ack_tim_low   && data_frame_seen) || (! ack_frame_seen)) ? data_tim_low  : ack_tim_low;
3492                 g->bounds.width  = (((data_tim_high >= ack_tim_high && data_frame_seen) || (! ack_frame_seen)) ? data_tim_high : ack_tim_high) - g->bounds.x0;
3493                 g->bounds.y0     =  ((data_seq_low <= ack_seq_low   && data_frame_seen) || (! ack_frame_seen)) ? data_seq_low  : ack_seq_low;
3494                 g->bounds.height = (((data_seq_high >= win_seq_high && data_frame_seen) || (! ack_frame_seen)) ? data_seq_high : win_seq_high) - g->bounds.y0;
3495                 break;
3496         }
3497
3498         g->zoom.x = (g->geom.width - 1) / g->bounds.width;
3499         g->zoom.y = (g->geom.height -1) / g->bounds.height;
3500 }
3501
3502
3503 static void tseq_stevens_make_elmtlist (struct graph *g)
3504 {
3505         struct segment *tmp;
3506         struct element *elements, *e;
3507         double xx0 = g->bounds.x0, yy0 = g->bounds.y0;
3508         guint32 seq_base = (guint32) yy0;
3509         guint32 seq_cur;
3510
3511         debug(DBS_FENTRY) puts ("tseq_stevens_make_elmtlist()");
3512         if (g->elists->elements == NULL) {
3513                 int n = 1 + get_num_dsegs (g);
3514                 e = elements = (struct element * )g_malloc (n*sizeof (struct element));
3515         } else
3516                 e = elements = g->elists->elements;
3517
3518         for (tmp=g->segments; tmp; tmp=tmp->next) {
3519                 double secs, seqno;
3520
3521                 if(!compare_headers(&g->current->ip_src, &g->current->ip_dst,
3522                                    g->current->th_sport, g->current->th_dport,
3523                                    &tmp->ip_src, &tmp->ip_dst,
3524                                    tmp->th_sport, tmp->th_dport,
3525                                    COMPARE_CURR_DIR)) {
3526                         continue;
3527                 }
3528                 /* data seg */
3529                 seq_cur = tmp->th_seq - seq_base;
3530                 secs = g->zoom.x * (tmp->rel_secs + tmp->rel_usecs / 1000000.0 - xx0);
3531                 seqno = g->zoom.y * seq_cur;
3532
3533                 e->type = ELMT_ARC;
3534                 e->parent = tmp;
3535                 e->gc = g->fg_gc;
3536                 e->p.arc.dim.width = g->s.tseq_stevens.seq_width;
3537                 e->p.arc.dim.height = g->s.tseq_stevens.seq_height;
3538                 e->p.arc.dim.x = secs - g->s.tseq_stevens.seq_width/2.0;
3539                 e->p.arc.dim.y = seqno + g->s.tseq_stevens.seq_height/2.0;
3540                 e->p.arc.filled = TRUE;
3541                 e->p.arc.angle1 = 0;
3542                 e->p.arc.angle2 = 23040;
3543                 e++;
3544         }
3545         e->type = ELMT_NONE;
3546         g->elists->elements = elements;
3547 }
3548
3549 static void tseq_stevens_toggle_seq_origin (struct graph *g)
3550 {
3551         g->s.tseq_stevens.flags ^= SEQ_ORIGIN;
3552
3553         if ((g->s.tseq_stevens.flags & SEQ_ORIGIN) == SEQ_ORIGIN_ZERO)
3554                 g->y_axis->min = g->bounds.y0;
3555         else            /* g->tseq_stevens.flags & SEQ_ORIGIN == SEQ_ORIGIN_ISN */
3556                 g->y_axis->min = 0;
3557 }
3558
3559 static void tseq_stevens_toggle_time_origin (struct graph *g)
3560 {
3561         g->s.tseq_stevens.flags ^= TIME_ORIGIN;
3562
3563         if ((g->s.tseq_stevens.flags & TIME_ORIGIN) == TIME_ORIGIN_CAP)
3564                 g->x_axis->min = g->bounds.x0;
3565         else            /* g->tseq_stevens.flags & TIME_ORIGIN == TIME_ORIGIN_CONN */
3566                 g->x_axis->min = 0;
3567 }
3568
3569 /*
3570  * tcptrace-style time-sequence graph
3571  */
3572
3573 static void tseq_tcptrace_read_config (struct graph *g)
3574 {
3575         GdkColormap *colormap;
3576         GdkColor color;
3577
3578         g->s.tseq_tcptrace.flags = 0;
3579         g->s.tseq_tcptrace.gc_seq = gdk_gc_new (g->drawing_area->window);
3580         g->s.tseq_tcptrace.gc_ack[0] = gdk_gc_new (g->drawing_area->window);
3581         g->s.tseq_tcptrace.gc_ack[1] = gdk_gc_new (g->drawing_area->window);
3582         colormap = gdk_window_get_colormap (g->drawing_area->window);
3583         if (!gdk_color_parse ("black", &color)) {
3584                 /*
3585                  * XXX - do more than just warn.
3586                  */
3587                 simple_dialog(ESD_TYPE_WARN, ESD_BTN_OK,
3588                     "Could not parse color black.");
3589         }
3590         if (!gdk_colormap_alloc_color (colormap, &color, FALSE, TRUE)) {
3591                 /*
3592                  * XXX - do more than just warn.
3593                  */
3594                 simple_dialog(ESD_TYPE_WARN, ESD_BTN_OK,
3595                     "Could not allocate color black.");
3596         }
3597         gdk_gc_set_foreground (g->s.tseq_tcptrace.gc_seq, &color);
3598         if (!gdk_color_parse ("LightSlateGray", &color)) {
3599                 /*
3600                  * XXX - do more than just warn.
3601                  */
3602                 simple_dialog(ESD_TYPE_WARN, ESD_BTN_OK,
3603                     "Could not parse color LightSlateGray.");
3604         }
3605         if (!gdk_colormap_alloc_color (colormap, &color, FALSE, TRUE)) {
3606                 /*
3607                  * XXX - do more than just warn.
3608                  */
3609                 simple_dialog(ESD_TYPE_WARN, ESD_BTN_OK,
3610                     "Could not allocate color LightSlateGray.");
3611         }
3612         gdk_gc_set_foreground (g->s.tseq_tcptrace.gc_ack[0], &color);
3613         if (!gdk_color_parse ("LightGray", &color)) {
3614                 /*
3615                  * XXX - do more than just warn.
3616                  */
3617                 simple_dialog(ESD_TYPE_WARN, ESD_BTN_OK,
3618                     "Could not parse color LightGray.");
3619         }
3620         if (!gdk_colormap_alloc_color (colormap, &color, FALSE, TRUE)) {
3621                 /*
3622                  * XXX - do more than just warn.
3623                  */
3624                 simple_dialog(ESD_TYPE_WARN, ESD_BTN_OK,
3625                     "Could not allocate color LightGray.");
3626         }
3627         gdk_gc_set_foreground (g->s.tseq_tcptrace.gc_ack[1], &color);
3628
3629         g->elists->next = (struct element_list * )
3630                                 g_malloc (sizeof (struct element_list));
3631         g->elists->next->next = NULL;
3632         g->elists->next->elements = NULL;
3633
3634         g->title = (const char ** )g_malloc (2 * sizeof (char *));
3635         g->title[0] = "Time/Sequence Graph (tcptrace)";
3636         g->title[1] = NULL;
3637         g->y_axis->label = (const char ** )g_malloc (3 * sizeof (char * ));
3638         g->y_axis->label[0] = "number[B]";
3639         g->y_axis->label[1] = "Sequence";
3640         g->y_axis->label[2] = NULL;
3641         g->x_axis->label = (const char ** )g_malloc (2 * sizeof (char * ));
3642         g->x_axis->label[0] = "Time[s]";
3643         g->x_axis->label[1] = NULL;
3644 }
3645
3646 static void tseq_tcptrace_make_elmtlist (struct graph *g)
3647 {
3648         struct segment *tmp;
3649         struct element *elements0, *e0;         /* list of elmts with prio 0 */
3650         struct element *elements1, *e1;         /* list of elmts with prio 1 */
3651         double xx0, yy0;
3652         double p_t = 0; /* ackno, window and time of previous segment */
3653         double p_ackno = 0, p_win = 0;
3654         gboolean ack_seen=FALSE;
3655         int toggle=0;
3656         guint32 seq_base;
3657         guint32 seq_cur;
3658
3659         debug(DBS_FENTRY) puts ("tseq_tcptrace_make_elmtlist()");
3660
3661         if (g->elists->elements == NULL) {
3662                 int n = 1 + 4*get_num_acks(g);
3663                 e0 = elements0 = (struct element * )g_malloc (n*sizeof (struct element));
3664         } else
3665                 e0 = elements0 = g->elists->elements;
3666
3667         if (g->elists->next->elements == NULL ) {
3668                 int n = 1 + 3*get_num_dsegs(g);
3669                 e1 = elements1 = (struct element * )g_malloc (n*sizeof (struct element));
3670         } else
3671                 e1 = elements1 = g->elists->next->elements;
3672
3673         xx0 = g->bounds.x0;
3674         yy0 = g->bounds.y0;
3675         seq_base = (guint32) yy0;
3676
3677         for (tmp=g->segments; tmp; tmp=tmp->next) {
3678                 double secs, data;
3679                 double x;
3680
3681                 secs = tmp->rel_secs + tmp->rel_usecs / 1000000.0;
3682                 x = secs - xx0;
3683                 x *= g->zoom.x;
3684                 if(compare_headers(&g->current->ip_src, &g->current->ip_dst,
3685                                    g->current->th_sport, g->current->th_dport,
3686                                    &tmp->ip_src, &tmp->ip_dst,
3687                                    tmp->th_sport, tmp->th_dport,
3688                                    COMPARE_CURR_DIR)) {
3689                         /* forward direction -> we need seqno and amount of data */
3690                         double yy1, yy2;
3691
3692                         seq_cur = tmp->th_seq - seq_base;
3693                         if (TCP_SYN (tmp->th_flags) || TCP_FIN (tmp->th_flags))
3694                                 data = 1;
3695                         else
3696                                 data = tmp->th_seglen;
3697
3698                         yy1 = g->zoom.y * (seq_cur);
3699                         yy2 = g->zoom.y * (seq_cur + data);
3700                         e1->type = ELMT_LINE;
3701                         e1->parent = tmp;
3702                         e1->gc = g->s.tseq_tcptrace.gc_seq;
3703                         e1->p.line.dim.x1 = e1->p.line.dim.x2 = x;
3704                         e1->p.line.dim.y1 = yy1;
3705                         e1->p.line.dim.y2 = yy2;
3706                         e1++;
3707                         e1->type = ELMT_LINE;
3708                         e1->parent = tmp;
3709                         e1->gc = g->s.tseq_tcptrace.gc_seq;
3710                         e1->p.line.dim.x1 = x - 1;
3711                         e1->p.line.dim.x2 = x + 1;
3712                         e1->p.line.dim.y1 = e1->p.line.dim.y2 = yy1;
3713                         e1++;
3714                         e1->type = ELMT_LINE;
3715                         e1->parent = tmp;
3716                         e1->gc = g->s.tseq_tcptrace.gc_seq;
3717                         e1->p.line.dim.x1 = x + 1;
3718                         e1->p.line.dim.x2 = x - 1;
3719                         e1->p.line.dim.y1 = e1->p.line.dim.y2 = yy2;
3720                         e1++;
3721                 } else {
3722                         double ackno, win;
3723                         if (! TCP_ACK (tmp->th_flags))
3724                                 /* SYN's and RST's do not necessarily have ACK's*/
3725                                 continue;
3726                         /* backward direction -> we need ackno and window */
3727                         seq_cur = tmp->th_ack - seq_base;
3728                         ackno = seq_cur * g->zoom.y;
3729                         win = tmp->th_win * g->zoom.y;
3730
3731                         /* ack line */
3732                         if (ack_seen == TRUE) { /* don't plot the first ack */
3733                                 e0->type = ELMT_LINE;
3734                                 e0->parent = tmp;
3735                                 e0->gc = g->s.tseq_tcptrace.gc_ack[toggle];
3736                                 e0->p.line.dim.x1 = p_t;
3737                                 e0->p.line.dim.y1 = p_ackno;
3738                                 e0->p.line.dim.x2 = x;
3739                                 e0->p.line.dim.y2 = p_ackno;
3740                                 e0++;
3741                                 e0->type = ELMT_LINE;
3742                                 e0->parent = tmp;
3743                                 e0->gc = g->s.tseq_tcptrace.gc_ack[toggle];
3744                                 e0->p.line.dim.x1 = x;
3745                                 e0->p.line.dim.y1 = p_ackno;
3746                                 e0->p.line.dim.x2 = x;
3747                                 e0->p.line.dim.y2 = ackno!=p_ackno || ackno<4 ? ackno : ackno-4;
3748                                 e0++;
3749                                 /* window line */
3750                                 e0->type = ELMT_LINE;
3751                                 e0->parent = tmp;
3752                                 e0->gc = g->s.tseq_tcptrace.gc_ack[toggle];
3753                                 e0->p.line.dim.x1 = p_t;
3754                                 e0->p.line.dim.y1 = p_win + p_ackno;
3755                                 e0->p.line.dim.x2 = x;
3756                                 e0->p.line.dim.y2 = p_win + p_ackno;
3757                                 e0++;
3758                                 e0->type = ELMT_LINE;
3759                                 e0->parent = tmp;
3760                                 e0->gc = g->s.tseq_tcptrace.gc_ack[toggle];
3761                                 e0->p.line.dim.x1 = x;
3762                                 e0->p.line.dim.y1 = p_win + p_ackno;
3763                                 e0->p.line.dim.x2 = x;
3764                                 e0->p.line.dim.y2 = win + ackno;
3765                                 e0++;
3766                                 toggle = 1^toggle;
3767                         }
3768                         ack_seen = TRUE;
3769                         p_ackno = ackno;
3770                         p_win = win;
3771                         p_t = x;
3772                 }
3773         }
3774         e0->type = ELMT_NONE;
3775         e1->type = ELMT_NONE;
3776         g->elists->elements = elements0;
3777         g->elists->next->elements = elements1;
3778 }
3779
3780 static void tseq_tcptrace_toggle_seq_origin (struct graph *g)
3781 {
3782         g->s.tseq_tcptrace.flags ^= SEQ_ORIGIN;
3783
3784         if ((g->s.tseq_tcptrace.flags & SEQ_ORIGIN) == SEQ_ORIGIN_ZERO)
3785                 g->y_axis->min = g->bounds.y0;
3786         else    /* g->tseq_stevens.flags & SEQ_ORIGIN == SEQ_ORIGIN_ISN */
3787                 g->y_axis->min = 0;
3788 }
3789
3790 static void tseq_tcptrace_toggle_time_origin (struct graph *g)
3791 {
3792         g->s.tseq_tcptrace.flags ^= TIME_ORIGIN;
3793
3794         if ((g->s.tseq_tcptrace.flags & TIME_ORIGIN) == TIME_ORIGIN_CAP)
3795                 g->x_axis->min = g->bounds.x0;
3796         else    /* g->tseq_stevens.flags & TIME_ORIGIN == TIME_ORIGIN_CONN */
3797                 g->x_axis->min = 0;
3798 }
3799
3800 /*
3801  * throughput graph
3802  */
3803
3804 static void tput_make_elmtlist (struct graph *g)
3805 {
3806         struct segment *tmp, *oldest;
3807         struct element *elements, *e;
3808         int i, sum=0;
3809         double dtime, tput;
3810
3811         if (g->elists->elements == NULL) {
3812                 int n = 1 + get_num_dsegs (g);
3813                 e = elements = (struct element * )g_malloc (n*sizeof (struct element));
3814         } else
3815                 e = elements = g->elists->elements;
3816
3817         for (oldest=g->segments,tmp=g->segments->next,i=0; tmp; tmp=tmp->next,i++) {
3818                 double time_val = tmp->rel_secs + tmp->rel_usecs/1000000.0;
3819                 dtime = time_val - (oldest->rel_secs + oldest->rel_usecs/1000000.0);
3820                 if (i>g->s.tput.nsegs) {
3821                         sum -= oldest->th_seglen;
3822                         oldest=oldest->next;
3823                 }
3824                 sum += tmp->th_seglen;
3825                 tput = sum / dtime;
3826                 /* debug(DBS_TPUT_ELMTS) printf ("tput=%f\n", tput); */
3827
3828                 e->type = ELMT_ARC;
3829                 e->parent = tmp;
3830                 e->gc = g->fg_gc;
3831                 e->p.arc.dim.width = g->s.tput.width;
3832                 e->p.arc.dim.height = g->s.tput.height;
3833                 e->p.arc.dim.x = g->zoom.x*(time_val - g->bounds.x0) - g->s.tput.width/2.0;
3834                 e->p.arc.dim.y = g->zoom.y*tput + g->s.tput.height/2.0;
3835                 e->p.arc.filled = TRUE;
3836                 e->p.arc.angle1 = 0;
3837                 e->p.arc.angle2 = 23040;
3838                 e++;
3839         }
3840         e->type = ELMT_NONE;
3841         g->elists->elements = elements;
3842 }
3843
3844 /* Purpose of <graph_type>_initialize functions:
3845  * - find maximum and minimum for both axes
3846  * - call setup routine for style struct */
3847 static void tput_initialize (struct graph *g)
3848 {
3849         struct segment *tmp, *oldest, *last;
3850         int i, sum=0;
3851         double dtime, tput, tputmax=0;
3852         double t, t0, tmax = 0, yy0, ymax;
3853
3854         debug(DBS_FENTRY) puts ("tput_initialize()");
3855
3856         tput_read_config(g);
3857
3858         for (last=g->segments; last->next; last=last->next);
3859         for (oldest=g->segments,tmp=g->segments->next,i=0; tmp; tmp=tmp->next,i++) {
3860                 dtime = tmp->rel_secs + tmp->rel_usecs/1000000.0 -
3861                                                 (oldest->rel_secs + oldest->rel_usecs/1000000.0);
3862                 if (i>g->s.tput.nsegs) {
3863                         sum -= oldest->th_seglen;
3864                         oldest=oldest->next;
3865                 }
3866                 sum += tmp->th_seglen;
3867                 tput = sum / dtime;
3868                 debug(DBS_TPUT_ELMTS) printf ("tput=%f\n", tput);
3869                 if (tput > tputmax)
3870                         tputmax = tput;
3871                 t = tmp->rel_secs + tmp->rel_usecs / 1000000.0;
3872                 if (t > tmax)
3873                         tmax = t;
3874         }
3875
3876         t0 = g->segments->rel_secs + g->segments->rel_usecs / 1000000.0;
3877         yy0 = 0;
3878         ymax = tputmax;
3879
3880         g->bounds.x0 = t0;
3881         g->bounds.y0 = yy0;
3882         g->bounds.width = tmax - t0;
3883         g->bounds.height = ymax - yy0;
3884         g->zoom.x = (g->geom.width - 1) / g->bounds.width;
3885         g->zoom.y = (g->geom.height -1) / g->bounds.height;
3886 }
3887
3888 static void tput_read_config (struct graph *g)
3889 {
3890         debug(DBS_FENTRY) puts ("tput_read_config()");
3891
3892         g->s.tput.width = 4;
3893         g->s.tput.height = 4;
3894         g->s.tput.nsegs = 20;
3895
3896         g->title = (const char ** )g_malloc (2 * sizeof (char *));
3897         g->title[0] = "Throughput Graph";
3898         g->title[1] = NULL;
3899         g->y_axis->label = (const char ** )g_malloc (3 * sizeof (char * ));
3900         g->y_axis->label[0] = "[B/s]";
3901         g->y_axis->label[1] = "Throughput";
3902         g->y_axis->label[2] = NULL;
3903         g->x_axis->label = (const char ** )g_malloc (2 * sizeof (char * ));
3904         g->x_axis->label[0] = "Time[s]";
3905         g->x_axis->label[1] = NULL;
3906         g->s.tput.flags = 0;
3907 }
3908
3909 static void tput_toggle_time_origin (struct graph *g)
3910 {
3911         g->s.tput.flags ^= TIME_ORIGIN;
3912
3913         if ((g->s.tput.flags & TIME_ORIGIN) == TIME_ORIGIN_CAP)
3914                 g->x_axis->min = g->bounds.x0;
3915         else    /* g->s.tput.flags & TIME_ORIGIN == TIME_ORIGIN_CONN */
3916                 g->x_axis->min = 0;
3917 }
3918
3919 /* RTT graph */
3920
3921 static void rtt_read_config (struct graph *g)
3922 {
3923         debug(DBS_FENTRY) puts ("rtt_read_config()");
3924
3925         g->s.rtt.width = 4;
3926         g->s.rtt.height = 4;
3927         g->s.rtt.flags = 0;
3928
3929         g->title = (const char ** )g_malloc (2 * sizeof (char *));
3930         g->title[0] = "Round Trip Time Graph";
3931         g->title[1] = NULL;
3932         g->y_axis->label = (const char ** )g_malloc (3 * sizeof (char * ));
3933         g->y_axis->label[0] = "RTT [s]";
3934         g->y_axis->label[1] = NULL;
3935         g->x_axis->label = (const char ** )g_malloc (2 * sizeof (char * ));
3936         g->x_axis->label[0] = "Sequence Number[B]";
3937         g->x_axis->label[1] = NULL;
3938 }
3939
3940 static void rtt_initialize (struct graph *g)
3941 {
3942         struct segment *tmp, *first=NULL;
3943         struct unack *unack = NULL, *u;
3944         double rttmax=0;
3945         double xx0, yy0, ymax;
3946         guint32 xmax = 0;
3947         guint32 seq_base = 0;
3948
3949         debug(DBS_FENTRY) puts ("rtt_initialize()");
3950
3951         rtt_read_config (g);
3952
3953         for (tmp=g->segments; tmp; tmp=tmp->next) {
3954                 if(compare_headers(&g->current->ip_src, &g->current->ip_dst,
3955                                    g->current->th_sport, g->current->th_dport,
3956                                    &tmp->ip_src, &tmp->ip_dst,
3957                                    tmp->th_sport, tmp->th_dport,
3958                                    COMPARE_CURR_DIR)) {
3959                         guint32 seqno = tmp->th_seq;
3960
3961                         if (!first) {
3962                                 first= tmp;
3963                                 seq_base = seqno;
3964                         }
3965                         seqno -= seq_base;
3966                         if (tmp->th_seglen && !rtt_is_retrans (unack, seqno)) {
3967                                 double time_val = tmp->rel_secs + tmp->rel_usecs / 1000000.0;
3968                                 u = rtt_get_new_unack (time_val, seqno);
3969                                 if (!u) return;
3970                                 rtt_put_unack_on_list (&unack, u);
3971                         }
3972
3973                         if (seqno + tmp->th_seglen > xmax)
3974                                 xmax = seqno + tmp->th_seglen;
3975                 } else if (first) {
3976                         guint32 ackno = tmp->th_ack -seq_base;
3977                         double time_val = tmp->rel_secs + tmp->rel_usecs / 1000000.0;
3978                         struct unack *v;
3979
3980                         for (u=unack; u; u=v)
3981                                 if (ackno > u->seqno) {
3982                                         double rtt = time_val - u->time;
3983                                         if (rtt > rttmax)
3984                                                 rttmax = rtt;
3985                                         v=u->next;
3986                                         rtt_delete_unack_from_list (&unack, u);
3987                                 } else
3988                                         v=u->next;
3989                 }
3990         }
3991
3992         xx0 = seq_base;
3993         yy0 = 0;
3994         ymax = rttmax;
3995
3996         g->bounds.x0 = xx0;
3997         g->bounds.y0 = yy0;
3998         g->bounds.width = xmax;
3999         g->bounds.height = ymax - yy0;
4000         g->zoom.x = g->geom.width / g->bounds.width;
4001         g->zoom.y = g->geom.height / g->bounds.height;
4002 }
4003
4004 static int rtt_is_retrans (struct unack *list, unsigned int seqno)
4005 {
4006         struct unack *u;
4007
4008         for (u=list; u; u=u->next)
4009                 if (u->seqno== seqno)
4010                         return TRUE;
4011
4012         return FALSE;
4013 }
4014
4015 static struct unack *rtt_get_new_unack (double time_val, unsigned int seqno)
4016 {
4017         struct unack *u;
4018
4019         u = (struct unack * )g_malloc (sizeof (struct unack));
4020         if (!u)
4021                 return NULL;
4022         u->next = NULL;
4023         u->time = time_val;
4024         u->seqno = seqno;
4025         return u;
4026 }
4027
4028 static void rtt_put_unack_on_list (struct unack **l, struct unack *new)
4029 {
4030         struct unack *u, *list = *l;
4031
4032         for (u=list; u; u=u->next)
4033                 if (!u->next)
4034                         break;
4035
4036         if (u)
4037                 u->next = new;
4038         else
4039                 *l = new;
4040 }
4041
4042 static void rtt_delete_unack_from_list (struct unack **l, struct unack *dead)
4043 {
4044         struct unack *u, *list = *l;
4045
4046         if (!dead || !list)
4047                 return;
4048
4049         if (dead==list) {
4050                 *l = list->next;
4051                 g_free (list);
4052         } else
4053                 for (u=list; u; u=u->next)
4054                         if (u->next == dead) {
4055                                 u->next = u->next->next;
4056                                 g_free (dead);
4057                                 break;
4058                         }
4059 }
4060
4061 static void rtt_make_elmtlist (struct graph *g)
4062 {
4063         struct segment *tmp;
4064         struct unack *unack = NULL, *u;
4065         struct element *elements, *e;
4066         guint32 seq_base = (guint32) g->bounds.x0;
4067
4068         debug(DBS_FENTRY) puts ("rtt_make_elmtlist()");
4069
4070         if (g->elists->elements == NULL) {
4071                 int n = 1 + get_num_dsegs (g);
4072                 e = elements = (struct element * )g_malloc (n*sizeof (struct element));
4073         } else {
4074                 e = elements = g->elists->elements;
4075         }
4076
4077         for (tmp=g->segments; tmp; tmp=tmp->next) {
4078                 if(compare_headers(&g->current->ip_src, &g->current->ip_dst,
4079                                    g->current->th_sport, g->current->th_dport,
4080                                    &tmp->ip_src, &tmp->ip_dst,
4081                                    tmp->th_sport, tmp->th_dport,
4082                                    COMPARE_CURR_DIR)) {
4083                         guint32 seqno = tmp->th_seq -seq_base;
4084
4085                         if (tmp->th_seglen && !rtt_is_retrans (unack, seqno)) {
4086                                 double time_val = tmp->rel_secs + tmp->rel_usecs / 1000000.0;
4087                                 u = rtt_get_new_unack (time_val, seqno);
4088                                 if (!u) return;
4089                                 rtt_put_unack_on_list (&unack, u);
4090                         }
4091                 } else {
4092                         guint32 ackno = tmp->th_ack -seq_base;
4093                         double time_val = tmp->rel_secs + tmp->rel_usecs / 1000000.0;
4094                         struct unack *v;
4095
4096                         for (u=unack; u; u=v)
4097                                 if (ackno > u->seqno) {
4098                                         double rtt = time_val - u->time;
4099
4100                                         e->type = ELMT_ARC;
4101                                         e->parent = tmp;
4102                                         e->gc = g->fg_gc;
4103                                         e->p.arc.dim.width = g->s.rtt.width;
4104                                         e->p.arc.dim.height = g->s.rtt.height;
4105                                         e->p.arc.dim.x = g->zoom.x * u->seqno - g->s.rtt.width/2.0;
4106                                         e->p.arc.dim.y = g->zoom.y * rtt + g->s.rtt.height/2.0;
4107                                         e->p.arc.filled = TRUE;
4108                                         e->p.arc.angle1 = 0;
4109                                         e->p.arc.angle2 = 23040;
4110                                         e++;
4111
4112                                         v=u->next;
4113                                         rtt_delete_unack_from_list (&unack, u);
4114                                 } else
4115                                         v=u->next;
4116                 }
4117         }
4118         e->type = ELMT_NONE;
4119         g->elists->elements = elements;
4120 }
4121
4122 static void rtt_toggle_seq_origin (struct graph *g)
4123 {
4124         g->s.rtt.flags ^= SEQ_ORIGIN;
4125
4126         if ((g->s.rtt.flags & SEQ_ORIGIN) == SEQ_ORIGIN_ZERO)
4127                 g->x_axis->min = g->bounds.x0;
4128         else
4129                 g->x_axis->min = 0;
4130 }
4131
4132 #if defined(_WIN32) && !defined(__MINGW32__)
4133 /* replacement of Unix rint() for Windows */
4134 static int rint (double x)
4135 {
4136         char *buf;
4137         int i,dec,sig;
4138
4139         buf = _fcvt(x, 0, &dec, &sig);
4140         i = atoi(buf);
4141         if(sig == 1) {
4142                 i = i * -1;
4143         }
4144         return(i);
4145 }
4146 #endif
4147
4148
4149 static gboolean tcp_graph_selected_packet_enabled(frame_data *current_frame, epan_dissect_t *edt, gpointer callback_data _U_)
4150 {
4151     return current_frame != NULL ? (edt->pi.ipproto == IP_PROTO_TCP) : FALSE;
4152 }
4153
4154
4155 void
4156 register_tap_listener_tcp_graph(void)
4157 {
4158     register_stat_menu_item("TCP Stream Graph/Time-Sequence Graph (Stevens)", REGISTER_STAT_GROUP_UNSORTED,
4159         tcp_graph_cb, tcp_graph_selected_packet_enabled, NULL, GINT_TO_POINTER(0));
4160     register_stat_menu_item("TCP Stream Graph/Time-Sequence Graph (tcptrace)", REGISTER_STAT_GROUP_UNSORTED,
4161         tcp_graph_cb, tcp_graph_selected_packet_enabled, NULL, GINT_TO_POINTER(1));
4162     register_stat_menu_item("TCP Stream Graph/Throughput Graph", REGISTER_STAT_GROUP_UNSORTED,
4163         tcp_graph_cb, tcp_graph_selected_packet_enabled, NULL, GINT_TO_POINTER(2));
4164     register_stat_menu_item("TCP Stream Graph/Round Trip Time Graph", REGISTER_STAT_GROUP_UNSORTED,
4165         tcp_graph_cb, tcp_graph_selected_packet_enabled, NULL, GINT_TO_POINTER(3));
4166 }