b2769cc8816b03cf8dbef5d0a257a18ad6006b19
[metze/wireshark/wip.git] / gtk / text_page_utils.c
1 /* text_page_utils.c
2  *  Construct a simple text page widget from a file.
3  *
4  * $Id$
5  *
6  * Ulf Lamping
7  *
8  * Wireshark - Network traffic analyzer
9  * By Gerald Combs <gerald@wireshark.org>
10  * Copyright 2000 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 #include <string.h>
31 #include <stdio.h>
32 #include <errno.h>
33
34 #include <gtk/gtk.h>
35
36 #include "epan/filesystem.h"
37
38 #include "../simple_dialog.h"
39 #include <wsutil/file_util.h>
40
41 #include "gtk/text_page_utils.h"
42 #include "gtk/gui_utils.h"
43 #include "gtk/font_utils.h"
44
45
46 #define TEXT_KEY        "txt_key"
47
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);
50
51
52 /*
53  * Construct a simple text page widget from a file.
54  */
55 GtkWidget * text_page_new(const char *absolute_path)
56 {
57   GtkWidget *page_vb, *txt_scrollw, *txt;
58
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),
63                                    GTK_SHADOW_IN);
64   gtk_box_pack_start(GTK_BOX(page_vb), txt_scrollw, TRUE, TRUE, 0);
65
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); */
78
79   g_object_set_data(G_OBJECT(page_vb), TEXT_KEY, txt);
80
81   text_page_set_text(page_vb, absolute_path);
82   gtk_container_add(GTK_CONTAINER(txt_scrollw), txt);
83   gtk_widget_show(txt_scrollw);
84   gtk_widget_show(txt);
85
86   return page_vb;
87 }
88
89
90 /*
91  * Insert some text to a text page.
92  */
93 static void text_page_insert(GtkWidget *page, const char *buffer, int nchars)
94 {
95     GtkWidget *txt = g_object_get_data(G_OBJECT(page), TEXT_KEY);
96
97     GtkTextBuffer *buf= gtk_text_view_get_buffer(GTK_TEXT_VIEW(txt));
98     GtkTextIter    iter;
99
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);
105 }
106
107
108 /*
109  * Put the complete text file into a text page.
110  */
111 static void text_page_set_text(GtkWidget *page, const char *absolute_path)
112 {
113   FILE *text_file;
114   char line[4096+1];    /* XXX - size? */
115
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));
120     }
121     if(ferror(text_file)) {
122       simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "Error reading file \"%s\": %s",
123                     absolute_path, strerror(errno));
124     }
125     fclose(text_file);
126   } else {
127       simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "Could not open file \"%s\": %s",
128                     absolute_path, strerror(errno));
129   }
130 }
131
132
133 /**
134  * Clear the text from the text page.
135  */
136 static void text_page_clear(GtkWidget *page)
137 {
138   GtkTextBuffer *buf = gtk_text_view_get_buffer(GTK_TEXT_VIEW(g_object_get_data(G_OBJECT(page), TEXT_KEY)));
139
140   gtk_text_buffer_set_text(buf, "", 0);
141 }
142
143
144 /**
145  * Redraw a single text page, e.g. to use a new font.
146  */
147 void text_page_redraw(GtkWidget *page, const char *absolute_path)
148 {
149   text_page_clear(page);
150   text_page_set_text(page, absolute_path);
151 }