replace *a lot* of file related calls by their GLib counterparts. This is necessary...
[obnox/wireshark/wip.git] / gtk / text_page.c
1 /* text_page.c
2  *
3  * $Id$
4  *
5  * Ulf Lamping
6  *
7  * Ethereal - Network traffic analyzer
8  * By Gerald Combs <gerald@ethereal.com>
9  * Copyright 2000 Gerald Combs
10  *
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.
15  *
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.
20  *
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.
24  */
25
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
29
30 #include <gtk/gtk.h>
31 #include <string.h>
32 #include <stdio.h>
33 #include <errno.h>
34
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"
42
43 #define TEXT_KEY        "txt_key"
44
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);
47
48
49 /*
50  * Construct a simple text page widget from a file.
51  */
52 GtkWidget * text_page_new(const char *absolute_path)
53 {
54   GtkWidget *page_vb, *txt_scrollw, *txt;
55
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), 
61                                    GTK_SHADOW_IN);
62 #endif
63   gtk_box_pack_start(GTK_BOX(page_vb), txt_scrollw, TRUE, TRUE, 0);
64
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);
72 #else
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); */
85 #endif
86
87   OBJECT_SET_DATA(page_vb, TEXT_KEY, txt);
88
89   text_page_set_text(page_vb, absolute_path);
90   gtk_container_add(GTK_CONTAINER(txt_scrollw), txt);
91   gtk_widget_show(txt_scrollw);
92   gtk_widget_show(txt);
93
94   return page_vb;
95 }
96
97
98 /*
99  * Insert some text to a text page.
100  */
101 static void text_page_insert(GtkWidget *page, const char *buffer, int nchars)
102 {
103     GtkWidget *txt = OBJECT_GET_DATA(page, TEXT_KEY);
104
105 #if GTK_MAJOR_VERSION < 2
106     gtk_text_insert(GTK_TEXT(txt), user_font_get_regular(), NULL, NULL, buffer, nchars);
107 #else
108     GtkTextBuffer *buf= gtk_text_view_get_buffer(GTK_TEXT_VIEW(txt));
109     GtkTextIter    iter;
110
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);
116 #endif
117 }
118
119
120 /*
121  * Put the complete text file into a text page.
122  */
123 static void text_page_set_text(GtkWidget *page, const char *absolute_path)
124 {
125   FILE *text_file;
126   char line[4096+1];    /* XXX - size? */
127
128 #if GTK_MAJOR_VERSION < 2
129   GtkText *txt = GTK_TEXT(OBJECT_GET_DATA(page, TEXT_KEY));
130   gtk_text_freeze(txt);
131 #endif
132
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));
137     }
138     if(ferror(text_file)) {
139       simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "Error reading file \"%s\": %s",
140                     absolute_path, strerror(errno));
141     }
142     fclose(text_file);
143   } else {
144       simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "Could not open file \"%s\": %s",
145                     absolute_path, strerror(errno));
146   }
147 #if GTK_MAJOR_VERSION < 2
148   gtk_text_thaw(txt);
149 #endif
150 }
151
152
153 /**
154  * Clear the text from the text page.
155  */
156 static void text_page_clear(GtkWidget *page)
157 {
158 #if GTK_MAJOR_VERSION < 2
159   GtkText *txt = GTK_TEXT(OBJECT_GET_DATA(page, TEXT_KEY));
160
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));
168 #else
169   GtkTextBuffer *buf = gtk_text_view_get_buffer(GTK_TEXT_VIEW(OBJECT_GET_DATA(page, TEXT_KEY)));
170
171   gtk_text_buffer_set_text(buf, "", 0);
172 #endif
173 }
174
175
176 /**
177  * Redraw a single text page, e.g. to use a new font.
178  */
179 void text_page_redraw(GtkWidget *page, const char *absolute_path)
180 {
181 #if GTK_MAJOR_VERSION < 2
182   GtkWidget *txt = OBJECT_GET_DATA(page, TEXT_KEY);
183 #endif
184
185 #if GTK_MAJOR_VERSION < 2
186   gtk_text_freeze(GTK_TEXT(txt));
187 #endif
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));
192 #endif
193 }