7 * Ethereal - Network traffic analyzer
8 * By Gerald Combs <gerald@ethereal.com>
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"
38 #include "compat_macros.h"
39 #include "simple_dialog.h"
40 #include "font_utils.h"
42 #define TEXT_KEY "txt_key"
44 static void text_page_insert(GtkWidget *page, const char *buffer, int nchars);
45 static void text_page_set_text(GtkWidget *page, const char *absolute_path);
49 * Construct a simple text page widget from a file.
51 GtkWidget * text_page_new(const char *absolute_path)
53 GtkWidget *page_vb, *txt_scrollw, *txt;
55 page_vb = gtk_vbox_new(FALSE, 0);
56 gtk_container_border_width(GTK_CONTAINER(page_vb), 1);
57 txt_scrollw = scrolled_window_new(NULL, NULL);
58 #if GTK_MAJOR_VERSION >= 2
59 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(txt_scrollw),
62 gtk_box_pack_start(GTK_BOX(page_vb), txt_scrollw, TRUE, TRUE, 0);
64 #if GTK_MAJOR_VERSION < 2
65 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(txt_scrollw),
66 GTK_POLICY_NEVER, GTK_POLICY_ALWAYS);
67 txt = gtk_text_new(NULL, NULL);
68 gtk_text_set_editable(GTK_TEXT(txt), FALSE);
69 gtk_text_set_word_wrap(GTK_TEXT(txt), TRUE);
70 gtk_text_set_line_wrap(GTK_TEXT(txt), TRUE);
72 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(txt_scrollw),
73 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
74 txt = gtk_text_view_new();
75 gtk_text_view_set_editable(GTK_TEXT_VIEW(txt), FALSE);
76 gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(txt), GTK_WRAP_WORD);
77 gtk_text_view_set_cursor_visible(GTK_TEXT_VIEW(txt), FALSE);
78 /* XXX: there seems to be no way to add a small border *around* the whole text,
79 * so the text will be "bump" against the edges.
80 * the following is only working for left and right edges,
81 * there is no such thing for top and bottom :-( */
82 /* gtk_text_view_set_left_margin(GTK_TEXT_VIEW(txt), 3); */
83 /* gtk_text_view_set_right_margin(GTK_TEXT_VIEW(txt), 3); */
86 OBJECT_SET_DATA(page_vb, TEXT_KEY, txt);
88 text_page_set_text(page_vb, absolute_path);
89 gtk_container_add(GTK_CONTAINER(txt_scrollw), txt);
90 gtk_widget_show(txt_scrollw);
98 * Insert some text to a text page.
100 static void text_page_insert(GtkWidget *page, const char *buffer, int nchars)
102 GtkWidget *txt = OBJECT_GET_DATA(page, TEXT_KEY);
104 #if GTK_MAJOR_VERSION < 2
105 gtk_text_insert(GTK_TEXT(txt), user_font_get_regular(), NULL, NULL, buffer, nchars);
107 GtkTextBuffer *buf= gtk_text_view_get_buffer(GTK_TEXT_VIEW(txt));
110 gtk_text_buffer_get_end_iter(buf, &iter);
111 gtk_widget_modify_font(GTK_WIDGET(txt), user_font_get_regular());
112 if (!g_utf8_validate(buffer, -1, NULL))
113 printf("Invalid utf8 encoding: %s\n", buffer);
114 gtk_text_buffer_insert(buf, &iter, buffer, nchars);
120 * Put the complete text file into a text page.
122 static void text_page_set_text(GtkWidget *page, const char *absolute_path)
125 char line[4096+1]; /* XXX - size? */
127 #if GTK_MAJOR_VERSION < 2
128 GtkText *txt = GTK_TEXT(OBJECT_GET_DATA(page, TEXT_KEY));
129 gtk_text_freeze(txt);
132 text_file = fopen(absolute_path, "r");
133 if (text_file != NULL) {
134 while (fgets(line, sizeof line, text_file) != NULL) {
135 text_page_insert(page, line, strlen(line));
137 if(ferror(text_file)) {
138 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "Error reading file \"%s\": %s",
139 absolute_path, strerror(errno));
143 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "Could not open file \"%s\": %s",
144 absolute_path, strerror(errno));
146 #if GTK_MAJOR_VERSION < 2
153 * Clear the text from the text page.
155 static void text_page_clear(GtkWidget *page)
157 #if GTK_MAJOR_VERSION < 2
158 GtkText *txt = GTK_TEXT(OBJECT_GET_DATA(page, TEXT_KEY));
160 gtk_text_set_point(txt, 0);
161 /* Keep GTK+ 1.2.3 through 1.2.6 from dumping core - see
162 http://www.ethereal.com/lists/ethereal-dev/199912/msg00312.html and
163 http://www.gnome.org/mailing-lists/archives/gtk-devel-list/1999-October/0051.shtml
164 for more information */
165 gtk_adjustment_set_value(txt->vadj, 0.0);
166 gtk_text_forward_delete(txt, gtk_text_get_length(txt));
168 GtkTextBuffer *buf = gtk_text_view_get_buffer(GTK_TEXT_VIEW(OBJECT_GET_DATA(page, TEXT_KEY)));
170 gtk_text_buffer_set_text(buf, "", 0);
176 * Redraw a single text page, e.g. to use a new font.
178 void text_page_redraw(GtkWidget *page, const char *absolute_path)
180 #if GTK_MAJOR_VERSION < 2
181 GtkWidget *txt = OBJECT_GET_DATA(page, TEXT_KEY);
184 #if GTK_MAJOR_VERSION < 2
185 gtk_text_freeze(GTK_TEXT(txt));
187 text_page_clear(page);
188 text_page_set_text(page, absolute_path);
189 #if GTK_MAJOR_VERSION < 2
190 gtk_text_thaw(GTK_TEXT(txt));