And finally (I hope) the last part from the patch
[obnox/wireshark/wip.git] / gtk / packet_history.c
1 /* packet_history.c
2  * packet history related functions   2004 Ulf Lamping
3  *
4  * $Id$
5  *
6  * Ethereal - Network traffic analyzer
7  * By Gerald Combs <gerald@ethereal.com>
8  * Copyright 1998 Gerald Combs
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
23  */
24
25 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
28
29 #include <gtk/gtk.h>
30
31 #include <stdio.h>
32
33 #include "file.h"
34 #include "globals.h"
35
36 #include "menu.h"
37 #include "packet_history.h"
38
39
40 GList *history_current = NULL;
41 GList *history_list = NULL;
42 gboolean ignore_jump = FALSE;
43
44
45 #if 0
46 /* print the complete packet history to console */
47 static void history_print(void) {
48     GList *current = g_list_first(history_list);
49
50     printf(" List:\n");
51
52     while(current) {
53         if(current == history_current) {
54             printf(" Row: %u *\n", GPOINTER_TO_INT(current->data));
55         } else {
56             printf(" Row: %u\n", GPOINTER_TO_INT(current->data));
57         }
58         current = g_list_next(current);
59     }
60 }
61 #endif
62
63
64 /* adjust menu and toolbar sensitivity depending on the history entries */
65 static void adjust_menus(void) {
66
67     if(history_current) {
68         set_menus_for_packet_history(
69             (g_list_previous(history_current) != NULL),
70             (g_list_next(history_current) != NULL));
71     } else {
72         /* we don't have any history */
73         set_menus_for_packet_history(FALSE, FALSE);
74     }
75
76     /* history_print(); */
77 }
78
79
80 /* clear the history list from the given entry to the end of the list */
81 static void clear_list(GList *current) {
82     GList *next_packet;
83
84
85     while(current) {
86         next_packet = g_list_next(current);
87         history_list = g_list_remove(history_list, current->data);
88         current = next_packet;
89     }
90 }
91
92
93 /* add an entry to the history list */
94 void packet_history_add(gint row) {
95
96     if(ignore_jump) {
97         /* we jumping back and forward in history, so don't change list */
98         return;
99     }
100
101     if (history_current) {
102         /* clear list behind current position */
103         clear_list(g_list_next(history_current));
104
105         /* ignore duplicates */
106         if(GPOINTER_TO_INT(history_current->data) == row) {
107             adjust_menus();
108             return;
109         }
110     }
111
112     /* add row */
113     history_list = g_list_append(history_list, GINT_TO_POINTER(row));
114     history_current = g_list_last(history_list);
115
116     adjust_menus();
117 }
118
119
120 void packet_history_clear(void) {
121     
122     /* clear "old" list */
123     clear_list(history_list);
124     history_current = NULL;
125
126     /* add the currently selected first row */
127     packet_history_add(0);
128
129     adjust_menus();
130 }
131
132
133 void packet_history_back(void) {
134     GList *previous;
135
136     if(history_current) {
137         previous = g_list_previous(history_current);
138
139         /* do we have a previous entry */
140         if(previous) {
141             history_current = previous;
142
143             /* goto that packet but don't change history */
144             ignore_jump = TRUE;
145             cf_goto_frame(&cfile, GPOINTER_TO_INT(previous->data) +1);
146             ignore_jump = FALSE;
147         }
148     }
149
150     adjust_menus();
151 }
152
153
154 void packet_history_forward(void) {
155     GList *next;
156
157     if(history_current) {
158         next = g_list_next(history_current);
159
160         /* do we have a forward entry? */
161         if(next) {
162             history_current = next;
163
164             /* goto that packet but don't change history */
165             ignore_jump = TRUE;
166             cf_goto_frame(&cfile, GPOINTER_TO_INT(next->data) +1);
167             ignore_jump = FALSE;
168         }
169     }
170
171     adjust_menus();
172 }
173
174
175 void history_forward_cb(GtkWidget *widget _U_, gpointer data _U_) {
176     packet_history_forward();
177 }
178
179
180 void history_back_cb(GtkWidget *widget _U_, gpointer data _U_) {
181     packet_history_back();
182 }
183