7 * Wireshark - Network traffic analyzer
8 * By Gerald Combs <gerald@wireshark.org>
9 * Copyright 2000 Gerald Combs
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version 2
14 * of the License, or (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
35 #include "epan/filesystem.h"
36 #include "text_page.h"
37 #include "gui_utils.h"
38 #include "compat_macros.h"
39 #include "simple_dialog.h"
40 #include "font_utils.h"
41 #include "file_util.h"
43 #define TEXT_KEY "txt_key"
45 static void text_page_insert(GtkWidget *page, const char *buffer, int nchars);
46 static void text_page_set_text(GtkWidget *page, const char *absolute_path);
50 * Construct a simple text page widget from a file.
52 GtkWidget * text_page_new(const char *absolute_path)
54 GtkWidget *page_vb, *txt_scrollw, *txt;
56 page_vb = gtk_vbox_new(FALSE, 0);
57 gtk_container_border_width(GTK_CONTAINER(page_vb), 1);
58 txt_scrollw = scrolled_window_new(NULL, NULL);
59 #if GTK_MAJOR_VERSION >= 2
60 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(txt_scrollw),
63 gtk_box_pack_start(GTK_BOX(page_vb), txt_scrollw, TRUE, TRUE, 0);
65 #if GTK_MAJOR_VERSION < 2
66 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(txt_scrollw),
67 GTK_POLICY_NEVER, GTK_POLICY_ALWAYS);
68 txt = gtk_text_new(NULL, NULL);
69 gtk_text_set_editable(GTK_TEXT(txt), FALSE);
70 gtk_text_set_word_wrap(GTK_TEXT(txt), TRUE);
71 gtk_text_set_line_wrap(GTK_TEXT(txt), TRUE);
73 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(txt_scrollw),
74 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
75 txt = gtk_text_view_new();
76 gtk_text_view_set_editable(GTK_TEXT_VIEW(txt), FALSE);
77 gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(txt), GTK_WRAP_WORD);
78 gtk_text_view_set_cursor_visible(GTK_TEXT_VIEW(txt), FALSE);
79 /* XXX: there seems to be no way to add a small border *around* the whole text,
80 * so the text will be "bump" against the edges.
81 * the following is only working for left and right edges,
82 * there is no such thing for top and bottom :-( */
83 /* gtk_text_view_set_left_margin(GTK_TEXT_VIEW(txt), 3); */
84 /* gtk_text_view_set_right_margin(GTK_TEXT_VIEW(txt), 3); */
87 OBJECT_SET_DATA(page_vb, TEXT_KEY, txt);
89 text_page_set_text(page_vb, absolute_path);
90 gtk_container_add(GTK_CONTAINER(txt_scrollw), txt);
91 gtk_widget_show(txt_scrollw);
99 * Insert some text to a text page.
101 static void text_page_insert(GtkWidget *page, const char *buffer, int nchars)
103 GtkWidget *txt = OBJECT_GET_DATA(page, TEXT_KEY);
105 #if GTK_MAJOR_VERSION < 2
106 gtk_text_insert(GTK_TEXT(txt), user_font_get_regular(), NULL, NULL, buffer, nchars);
108 GtkTextBuffer *buf= gtk_text_view_get_buffer(GTK_TEXT_VIEW(txt));
111 gtk_text_buffer_get_end_iter(buf, &iter);
112 gtk_widget_modify_font(GTK_WIDGET(txt), user_font_get_regular());
113 if (!g_utf8_validate(buffer, -1, NULL))
114 printf("Invalid utf8 encoding: %s\n", buffer);
115 gtk_text_buffer_insert(buf, &iter, buffer, nchars);
121 * Put the complete text file into a text page.
123 static void text_page_set_text(GtkWidget *page, const char *absolute_path)
126 char line[4096+1]; /* XXX - size? */
128 #if GTK_MAJOR_VERSION < 2
129 GtkText *txt = GTK_TEXT(OBJECT_GET_DATA(page, TEXT_KEY));
130 gtk_text_freeze(txt);
133 text_file = eth_fopen(absolute_path, "r");
134 if (text_file != NULL) {
135 while (fgets(line, sizeof line, text_file) != NULL) {
136 text_page_insert(page, line, strlen(line));
138 if(ferror(text_file)) {
139 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "Error reading file \"%s\": %s",
140 absolute_path, strerror(errno));
144 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "Could not open file \"%s\": %s",
145 absolute_path, strerror(errno));
147 #if GTK_MAJOR_VERSION < 2
154 * Clear the text from the text page.
156 static void text_page_clear(GtkWidget *page)
158 #if GTK_MAJOR_VERSION < 2
159 GtkText *txt = GTK_TEXT(OBJECT_GET_DATA(page, TEXT_KEY));
161 gtk_text_set_point(txt, 0);
162 /* Keep GTK+ 1.2.3 through 1.2.6 from dumping core - see
163 http://www.ethereal.com/lists/ethereal-dev/199912/msg00312.html and
164 http://www.gnome.org/mailing-lists/archives/gtk-devel-list/1999-October/0051.shtml
165 for more information */
166 gtk_adjustment_set_value(txt->vadj, 0.0);
167 gtk_text_forward_delete(txt, gtk_text_get_length(txt));
169 GtkTextBuffer *buf = gtk_text_view_get_buffer(GTK_TEXT_VIEW(OBJECT_GET_DATA(page, TEXT_KEY)));
171 gtk_text_buffer_set_text(buf, "", 0);
177 * Redraw a single text page, e.g. to use a new font.
179 void text_page_redraw(GtkWidget *page, const char *absolute_path)
181 #if GTK_MAJOR_VERSION < 2
182 GtkWidget *txt = OBJECT_GET_DATA(page, TEXT_KEY);
185 #if GTK_MAJOR_VERSION < 2
186 gtk_text_freeze(GTK_TEXT(txt));
188 text_page_clear(page);
189 text_page_set_text(page, absolute_path);
190 #if GTK_MAJOR_VERSION < 2
191 gtk_text_thaw(GTK_TEXT(txt));