1 | /* -*- mode: c; c-basic-offset: 8 -*- */ |
---|
2 | |
---|
3 | /* |
---|
4 | This file is part of the GuileRepl library |
---|
5 | |
---|
6 | Copyright 2001 Ariel Rios <ariel@linuxppc.org> |
---|
7 | |
---|
8 | This library is free software; you can redistribute it and/or |
---|
9 | modify it under the terms of the GNU Library General Public |
---|
10 | License as published by the Free Software Foundation; either |
---|
11 | version 2 of the License, or (at your option) any later version. |
---|
12 | |
---|
13 | This library is distributed in the hope that it will be useful, |
---|
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
16 | Library General Public License for more details. |
---|
17 | |
---|
18 | You should have received a copy of the GNU Library General Public License |
---|
19 | along with this library; see the file COPYING.LIB. If not, write to |
---|
20 | the Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
---|
21 | Boston, MA 02111-1307, USA. |
---|
22 | |
---|
23 | */ |
---|
24 | #include <sys/types.h> |
---|
25 | #include <sys/stat.h> |
---|
26 | #include <fcntl.h> |
---|
27 | #include <stdio.h> |
---|
28 | #include <unistd.h> |
---|
29 | #include <string.h> |
---|
30 | |
---|
31 | #include <gtk/gtkwindow.h> |
---|
32 | #include <gtk/gtkhbox.h> |
---|
33 | #include <gtk/gtkscrolledwindow.h> |
---|
34 | #include <gtk/gtkvbox.h> |
---|
35 | #include <gtk/gtkmain.h> |
---|
36 | #include <gtk/gtkbutton.h> |
---|
37 | #include <gtk/gtksignal.h> |
---|
38 | |
---|
39 | #include <libgnome/gnome-i18n.h> |
---|
40 | #include <libgnomeui/gnome-ui-init.h> |
---|
41 | |
---|
42 | #include "gtkhtml.h" |
---|
43 | #include "gtkhtmldebug.h" |
---|
44 | #include "gtkhtml-stream.h" |
---|
45 | |
---|
46 | #include "htmlengine.h" |
---|
47 | |
---|
48 | #define BUTTON_INDEX 6 |
---|
49 | |
---|
50 | GtkWidget *html; |
---|
51 | |
---|
52 | gchar *html_files [] = {"test1.html", "test2.html", "test9.html", "test11.html", "test6.html"}; |
---|
53 | |
---|
54 | static gchar *welcome = |
---|
55 | "Czech (Čeština) Čau, Ahoj, Dobrý den<BR>" |
---|
56 | "French (Français) Bonjour, Salut<BR>" |
---|
57 | "Korean (한글) 안녕하세요, 안녕하십니까<BR>" |
---|
58 | "Russian (Русский) Здравствуйте!<BR>" |
---|
59 | "Chinese (Simplified) <span lang=\"zh-cn\">元气 开发</span><BR>" |
---|
60 | "Chinese (Traditional) <span lang=\"zh-tw\">元氣 開發</span><BR>" |
---|
61 | "Japanese <span lang=\"ja\">元気 開発<BR></FONT>"; |
---|
62 | |
---|
63 | static void |
---|
64 | url_requested (GtkHTML *html, const char *url, GtkHTMLStream *stream, gpointer data) |
---|
65 | { |
---|
66 | int fd; |
---|
67 | gchar *filename; |
---|
68 | |
---|
69 | filename = g_strconcat ("tests/", url, NULL); |
---|
70 | fd = open (filename, O_RDONLY); |
---|
71 | |
---|
72 | if (fd != -1) { |
---|
73 | #define MY_BUF_SIZE 32768 |
---|
74 | gchar *buf; |
---|
75 | size_t size; |
---|
76 | |
---|
77 | buf = alloca (MY_BUF_SIZE); |
---|
78 | while ((size = read (fd, buf, MY_BUF_SIZE)) > 0) { |
---|
79 | gtk_html_stream_write (stream, buf, size); |
---|
80 | } |
---|
81 | gtk_html_stream_close (stream, size == -1 ? GTK_HTML_STREAM_ERROR : GTK_HTML_STREAM_OK); |
---|
82 | close (fd); |
---|
83 | } else |
---|
84 | gtk_html_stream_close (stream, GTK_HTML_STREAM_ERROR); |
---|
85 | g_free (filename); |
---|
86 | } |
---|
87 | |
---|
88 | static void |
---|
89 | read_html (GtkWidget *html, gint k) |
---|
90 | { |
---|
91 | GtkHTMLStream *stream; |
---|
92 | |
---|
93 | stream = gtk_html_begin (GTK_HTML (html)); |
---|
94 | url_requested (GTK_HTML (html), html_files [k], stream, NULL); |
---|
95 | } |
---|
96 | |
---|
97 | static void |
---|
98 | button_cb (GtkWidget *button, gpointer data) |
---|
99 | { |
---|
100 | read_html (html, GPOINTER_TO_INT (data)); |
---|
101 | } |
---|
102 | |
---|
103 | static void |
---|
104 | quit_cb (GtkWidget *button) |
---|
105 | { |
---|
106 | gtk_main_quit (); |
---|
107 | } |
---|
108 | |
---|
109 | static gchar * |
---|
110 | encode_html (gchar *txt) |
---|
111 | { |
---|
112 | GString *str; |
---|
113 | gchar *rv; |
---|
114 | |
---|
115 | str = g_string_new (NULL); |
---|
116 | |
---|
117 | do { |
---|
118 | gunichar uc; |
---|
119 | |
---|
120 | uc = g_utf8_get_char (txt); |
---|
121 | if (uc > 160) { |
---|
122 | g_string_append_printf (str, "&#%u;", uc); |
---|
123 | } else { |
---|
124 | g_string_append_c (str, uc); |
---|
125 | } |
---|
126 | } while ((txt = g_utf8_next_char (txt)) && *txt); |
---|
127 | |
---|
128 | rv = str->str; |
---|
129 | g_string_free (str, FALSE); |
---|
130 | |
---|
131 | return rv; |
---|
132 | } |
---|
133 | |
---|
134 | static void |
---|
135 | dump_cb (GtkWidget *widget, gpointer data) |
---|
136 | { |
---|
137 | g_print ("Object Tree\n"); |
---|
138 | g_print ("-----------\n"); |
---|
139 | |
---|
140 | gtk_html_debug_dump_tree (GTK_HTML (html)->engine->clue, 0); |
---|
141 | } |
---|
142 | |
---|
143 | static void |
---|
144 | dump_simple_cb (GtkWidget *widget, gpointer data) |
---|
145 | { |
---|
146 | g_print ("Simple Object Tree\n"); |
---|
147 | g_print ("-----------\n"); |
---|
148 | |
---|
149 | gtk_html_debug_dump_tree_simple (GTK_HTML (html)->engine->clue, 0); |
---|
150 | } |
---|
151 | |
---|
152 | int |
---|
153 | main (int argc, char **argv) |
---|
154 | { |
---|
155 | GtkWidget *window; |
---|
156 | GtkWidget *vbox; |
---|
157 | GtkWidget *hbox; |
---|
158 | GtkWidget *button [BUTTON_INDEX]; |
---|
159 | GtkWidget *swindow; |
---|
160 | GtkWidget *debug; |
---|
161 | gchar *str []= {"Example 1", "Example 2", "Example 3", "Example 4", "Example 5", "Quit"}; |
---|
162 | int i = 0; |
---|
163 | |
---|
164 | gnome_program_init ("libgtkhtml test", "0.0", LIBGNOMEUI_MODULE, argc, argv, NULL); |
---|
165 | |
---|
166 | window = gtk_window_new (GTK_WINDOW_TOPLEVEL); |
---|
167 | vbox = gtk_vbox_new (FALSE, 0); |
---|
168 | hbox = gtk_hbox_new (FALSE, 0); |
---|
169 | swindow = gtk_scrolled_window_new (NULL, NULL); |
---|
170 | gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (swindow), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC); |
---|
171 | html = gtk_html_new_from_string (encode_html (welcome), -1); |
---|
172 | g_signal_connect (html, "url_requested", G_CALLBACK (url_requested), NULL); |
---|
173 | |
---|
174 | for (; i < BUTTON_INDEX; i++) |
---|
175 | button [i] = gtk_button_new_with_label (str [i]); |
---|
176 | |
---|
177 | gtk_container_add (GTK_CONTAINER (window), vbox); |
---|
178 | gtk_container_add (GTK_CONTAINER (swindow), html); |
---|
179 | gtk_box_pack_start (GTK_BOX (vbox), swindow, TRUE, TRUE, 0); |
---|
180 | gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0); |
---|
181 | for (i = 0; i < BUTTON_INDEX; i++) |
---|
182 | gtk_box_pack_start (GTK_BOX (hbox), button [i], FALSE, FALSE, 0); |
---|
183 | |
---|
184 | debug = gtk_button_new_with_label ("Dump"); |
---|
185 | gtk_box_pack_end (GTK_BOX (hbox), debug, FALSE, FALSE, 0); |
---|
186 | g_signal_connect (debug, "clicked", G_CALLBACK (dump_cb), NULL); |
---|
187 | debug = gtk_button_new_with_label ("Dump simple"); |
---|
188 | gtk_box_pack_end (GTK_BOX (hbox), debug, FALSE, FALSE, 0); |
---|
189 | g_signal_connect (debug, "clicked", G_CALLBACK (dump_simple_cb), NULL); |
---|
190 | |
---|
191 | gtk_window_set_title (GTK_WINDOW (window), _("GtkHTML Test")); |
---|
192 | gtk_window_set_default_size (GTK_WINDOW (window), 500, 500); |
---|
193 | |
---|
194 | for (i = 0; i < BUTTON_INDEX -1; i++) |
---|
195 | g_signal_connect (button [i], "clicked", G_CALLBACK (button_cb), GINT_TO_POINTER (i)); |
---|
196 | g_signal_connect (button [BUTTON_INDEX - 1], "clicked", G_CALLBACK (quit_cb), NULL); |
---|
197 | |
---|
198 | gtk_widget_show_all (window); |
---|
199 | |
---|
200 | gtk_main (); |
---|
201 | |
---|
202 | return 0; |
---|
203 | |
---|
204 | } |
---|