Delete extra_split if we're not using it. This keeps its handle from
[metze/wireshark/wip.git] / ui / 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25  */
26
27 #include "config.h"
28 #include <string.h>
29 #include <stdio.h>
30 #include <errno.h>
31
32 #include <gtk/gtk.h>
33
34 #include "epan/filesystem.h"
35
36 #include "ui/simple_dialog.h"
37 #include <wsutil/file_util.h>
38
39 #include "ui/gtk/text_page_utils.h"
40 #include "ui/gtk/gui_utils.h"
41 #include "ui/gtk/font_utils.h"
42
43
44 #define TEXT_KEY        "txt_key"
45
46 static void text_page_insert(GtkWidget *page, const char *buffer, int nchars);
47 static void text_page_set_text(GtkWidget *page, const char *absolute_path);
48
49
50 /*
51  * Construct a simple text page widget from a file.
52  */
53 GtkWidget * text_page_new(const char *absolute_path)
54 {
55   GtkWidget *page_vb, *txt_scrollw, *txt;
56
57   page_vb =ws_gtk_box_new(GTK_ORIENTATION_VERTICAL, 0, FALSE);
58   gtk_container_set_border_width(GTK_CONTAINER(page_vb), 1);
59   txt_scrollw = scrolled_window_new(NULL, NULL);
60     gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(txt_scrollw),
61                                    GTK_SHADOW_IN);
62   gtk_box_pack_start(GTK_BOX(page_vb), txt_scrollw, TRUE, TRUE, 0);
63
64   gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(txt_scrollw),
65                                  GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
66   txt = gtk_text_view_new();
67   gtk_text_view_set_editable(GTK_TEXT_VIEW(txt), FALSE);
68   gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(txt), GTK_WRAP_WORD);
69   gtk_text_view_set_cursor_visible(GTK_TEXT_VIEW(txt), FALSE);
70   /* XXX: there seems to be no way to add a small border *around* the whole text,
71    * so the text will be "bump" against the edges.
72    * the following is only working for left and right edges,
73    * there is no such thing for top and bottom :-( */
74   /* gtk_text_view_set_left_margin(GTK_TEXT_VIEW(txt), 3); */
75   /* gtk_text_view_set_right_margin(GTK_TEXT_VIEW(txt), 3); */
76
77   g_object_set_data(G_OBJECT(page_vb), TEXT_KEY, txt);
78
79   text_page_set_text(page_vb, absolute_path);
80   gtk_container_add(GTK_CONTAINER(txt_scrollw), txt);
81   gtk_widget_show(txt_scrollw);
82   gtk_widget_show(txt);
83
84   return page_vb;
85 }
86
87
88 /*
89  * Insert some text to a text page.
90  */
91 static void text_page_insert(GtkWidget *page, const char *buffer, int nchars)
92 {
93     GtkWidget *txt = (GtkWidget *)g_object_get_data(G_OBJECT(page), TEXT_KEY);
94
95     GtkTextBuffer *buf= gtk_text_view_get_buffer(GTK_TEXT_VIEW(txt));
96     GtkTextIter    iter;
97
98     gtk_text_buffer_get_end_iter(buf, &iter);
99 #if GTK_CHECK_VERSION(3,0,0)
100     gtk_widget_override_font(GTK_WIDGET(txt), user_font_get_regular());
101 #else
102     gtk_widget_modify_font(GTK_WIDGET(txt), user_font_get_regular());
103 #endif
104     if (!g_utf8_validate(buffer, -1, NULL))
105         printf("Invalid utf8 encoding: %s\n", buffer);
106     gtk_text_buffer_insert(buf, &iter, buffer, nchars);
107 }
108
109
110 /*
111  * Put the complete text file into a text page.
112  */
113 static void text_page_set_text(GtkWidget *page, const char *absolute_path)
114 {
115   FILE *text_file;
116   char line[4096+1];    /* XXX - size? */
117
118   text_file = ws_fopen(absolute_path, "r");
119   if (text_file != NULL) {
120     while (fgets(line, sizeof line, text_file) != NULL) {
121       text_page_insert(page, line, (int) strlen(line));
122     }
123     if(ferror(text_file)) {
124       simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "Error reading file \"%s\": %s",
125                     absolute_path, g_strerror(errno));
126     }
127     fclose(text_file);
128   } else {
129       simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "Could not open file \"%s\": %s",
130                     absolute_path, g_strerror(errno));
131   }
132 }
133
134
135 /**
136  * Clear the text from the text page.
137  */
138 static void text_page_clear(GtkWidget *page)
139 {
140   GtkTextBuffer *buf = gtk_text_view_get_buffer(GTK_TEXT_VIEW(g_object_get_data(G_OBJECT(page), TEXT_KEY)));
141
142   gtk_text_buffer_set_text(buf, "", 0);
143 }
144
145
146 /**
147  * Redraw a single text page, e.g. to use a new font.
148  */
149 void text_page_redraw(GtkWidget *page, const char *absolute_path)
150 {
151   text_page_clear(page);
152   text_page_set_text(page, absolute_path);
153 }