2 * Construct a simple text page widget from a file.
8 * Wireshark - Network traffic analyzer
9 * By Gerald Combs <gerald@wireshark.org>
10 * Copyright 2000 Gerald Combs
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.
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.
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.
36 #include "epan/filesystem.h"
38 #include "../simple_dialog.h"
39 #include <wsutil/file_util.h>
41 #include "gtk/text_page_utils.h"
42 #include "gtk/gui_utils.h"
43 #include "gtk/font_utils.h"
46 #define TEXT_KEY "txt_key"
48 static void text_page_insert(GtkWidget *page, const char *buffer, int nchars);
49 static void text_page_set_text(GtkWidget *page, const char *absolute_path);
53 * Construct a simple text page widget from a file.
55 GtkWidget * text_page_new(const char *absolute_path)
57 GtkWidget *page_vb, *txt_scrollw, *txt;
59 page_vb = gtk_vbox_new(FALSE, 0);
60 gtk_container_set_border_width(GTK_CONTAINER(page_vb), 1);
61 txt_scrollw = scrolled_window_new(NULL, NULL);
62 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(txt_scrollw),
64 gtk_box_pack_start(GTK_BOX(page_vb), txt_scrollw, TRUE, TRUE, 0);
66 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(txt_scrollw),
67 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
68 txt = gtk_text_view_new();
69 gtk_text_view_set_editable(GTK_TEXT_VIEW(txt), FALSE);
70 gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(txt), GTK_WRAP_WORD);
71 gtk_text_view_set_cursor_visible(GTK_TEXT_VIEW(txt), FALSE);
72 /* XXX: there seems to be no way to add a small border *around* the whole text,
73 * so the text will be "bump" against the edges.
74 * the following is only working for left and right edges,
75 * there is no such thing for top and bottom :-( */
76 /* gtk_text_view_set_left_margin(GTK_TEXT_VIEW(txt), 3); */
77 /* gtk_text_view_set_right_margin(GTK_TEXT_VIEW(txt), 3); */
79 g_object_set_data(G_OBJECT(page_vb), TEXT_KEY, txt);
81 text_page_set_text(page_vb, absolute_path);
82 gtk_container_add(GTK_CONTAINER(txt_scrollw), txt);
83 gtk_widget_show(txt_scrollw);
91 * Insert some text to a text page.
93 static void text_page_insert(GtkWidget *page, const char *buffer, int nchars)
95 GtkWidget *txt = g_object_get_data(G_OBJECT(page), TEXT_KEY);
97 GtkTextBuffer *buf= gtk_text_view_get_buffer(GTK_TEXT_VIEW(txt));
100 gtk_text_buffer_get_end_iter(buf, &iter);
101 gtk_widget_modify_font(GTK_WIDGET(txt), user_font_get_regular());
102 if (!g_utf8_validate(buffer, -1, NULL))
103 printf("Invalid utf8 encoding: %s\n", buffer);
104 gtk_text_buffer_insert(buf, &iter, buffer, nchars);
109 * Put the complete text file into a text page.
111 static void text_page_set_text(GtkWidget *page, const char *absolute_path)
114 char line[4096+1]; /* XXX - size? */
116 text_file = ws_fopen(absolute_path, "r");
117 if (text_file != NULL) {
118 while (fgets(line, sizeof line, text_file) != NULL) {
119 text_page_insert(page, line, (int) strlen(line));
121 if(ferror(text_file)) {
122 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "Error reading file \"%s\": %s",
123 absolute_path, strerror(errno));
127 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "Could not open file \"%s\": %s",
128 absolute_path, strerror(errno));
134 * Clear the text from the text page.
136 static void text_page_clear(GtkWidget *page)
138 GtkTextBuffer *buf = gtk_text_view_get_buffer(GTK_TEXT_VIEW(g_object_get_data(G_OBJECT(page), TEXT_KEY)));
140 gtk_text_buffer_set_text(buf, "", 0);
145 * Redraw a single text page, e.g. to use a new font.
147 void text_page_redraw(GtkWidget *page, const char *absolute_path)
149 text_page_clear(page);
150 text_page_set_text(page, absolute_path);