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