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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
34 #include "epan/filesystem.h"
36 #include "ui/simple_dialog.h"
37 #include <wsutil/file_util.h>
39 #include "ui/gtk/text_page_utils.h"
40 #include "ui/gtk/gui_utils.h"
41 #include "ui/gtk/font_utils.h"
44 #define TEXT_KEY "txt_key"
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
);
51 * Construct a simple text page widget from a file.
53 GtkWidget
* text_page_new(const char *absolute_path
)
55 GtkWidget
*page_vb
, *txt_scrollw
, *txt
;
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
),
62 gtk_box_pack_start(GTK_BOX(page_vb
), txt_scrollw
, TRUE
, TRUE
, 0);
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); */
77 g_object_set_data(G_OBJECT(page_vb
), TEXT_KEY
, txt
);
79 text_page_set_text(page_vb
, absolute_path
);
80 gtk_container_add(GTK_CONTAINER(txt_scrollw
), txt
);
81 gtk_widget_show(txt_scrollw
);
89 * Insert some text to a text page.
91 static void text_page_insert(GtkWidget
*page
, const char *buffer
, int nchars
)
93 GtkWidget
*txt
= (GtkWidget
*)g_object_get_data(G_OBJECT(page
), TEXT_KEY
);
95 GtkTextBuffer
*buf
= gtk_text_view_get_buffer(GTK_TEXT_VIEW(txt
));
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());
102 gtk_widget_modify_font(GTK_WIDGET(txt
), user_font_get_regular());
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
);
111 * Put the complete text file into a text page.
113 static void text_page_set_text(GtkWidget
*page
, const char *absolute_path
)
116 char line
[4096+1]; /* XXX - size? */
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
));
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
));
129 simple_dialog(ESD_TYPE_ERROR
, ESD_BTN_OK
, "Could not open file \"%s\": %s",
130 absolute_path
, g_strerror(errno
));
136 * Clear the text from the text page.
138 static void text_page_clear(GtkWidget
*page
)
140 GtkTextBuffer
*buf
= gtk_text_view_get_buffer(GTK_TEXT_VIEW(g_object_get_data(G_OBJECT(page
), TEXT_KEY
)));
142 gtk_text_buffer_set_text(buf
, "", 0);
147 * Redraw a single text page, e.g. to use a new font.
149 void text_page_redraw(GtkWidget
*page
, const char *absolute_path
)
151 text_page_clear(page
);
152 text_page_set_text(page
, absolute_path
);