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 <gnome.h> |
---|
29 | #include "gtkhtml.h" |
---|
30 | #include "gtkhtml-stream.h" |
---|
31 | |
---|
32 | #define BUTTON_INDEX 6 |
---|
33 | |
---|
34 | GtkWidget *html; |
---|
35 | |
---|
36 | gchar *html_files [] = {"test1.html", "test2.html", "test9.html", "test11.html", "test6.html"}; |
---|
37 | |
---|
38 | static void |
---|
39 | url_requested (GtkHTML *html, const char *url, GtkHTMLStream *stream, gpointer data) |
---|
40 | { |
---|
41 | int fd; |
---|
42 | gchar *filename; |
---|
43 | |
---|
44 | filename = g_strconcat ("tests/", url, NULL); |
---|
45 | fd = open (filename, O_RDONLY); |
---|
46 | |
---|
47 | if (fd != -1) { |
---|
48 | #define MY_BUF_SIZE 32768 |
---|
49 | gchar *buf; |
---|
50 | size_t size; |
---|
51 | |
---|
52 | buf = alloca (MY_BUF_SIZE); |
---|
53 | while ((size = read (fd, buf, MY_BUF_SIZE)) > 0) { |
---|
54 | gtk_html_stream_write (stream, buf, size); |
---|
55 | } |
---|
56 | gtk_html_stream_close (stream, size == -1 ? GTK_HTML_STREAM_ERROR : GTK_HTML_STREAM_OK); |
---|
57 | close (fd); |
---|
58 | } else |
---|
59 | gtk_html_stream_close (stream, GTK_HTML_STREAM_ERROR); |
---|
60 | g_free (filename); |
---|
61 | } |
---|
62 | |
---|
63 | static void |
---|
64 | read_html (GtkWidget *html, gint k) |
---|
65 | { |
---|
66 | int fd, n; |
---|
67 | gchar *html_file = g_strconcat ("tests/", html_files [k], NULL); |
---|
68 | char ostr [BUFSIZ]; |
---|
69 | fd = open (html_file, O_RDONLY); |
---|
70 | |
---|
71 | if (fd != -1) { |
---|
72 | n = read (fd, ostr, BUFSIZ); |
---|
73 | gtk_html_load_from_string (GTK_HTML (html), ostr, n); |
---|
74 | |
---|
75 | close (fd); |
---|
76 | } |
---|
77 | g_free (html_file); |
---|
78 | } |
---|
79 | |
---|
80 | static void |
---|
81 | button_cb (GtkWidget *button, gpointer data) |
---|
82 | { |
---|
83 | read_html (html, GPOINTER_TO_INT (data)); |
---|
84 | } |
---|
85 | |
---|
86 | static void |
---|
87 | quit_cb (GtkWidget *button) |
---|
88 | { |
---|
89 | gtk_main_quit (); |
---|
90 | } |
---|
91 | |
---|
92 | int |
---|
93 | main (int argc, char **argv) |
---|
94 | { |
---|
95 | GtkWidget *window; |
---|
96 | GtkWidget *vbox; |
---|
97 | GtkWidget *hbox; |
---|
98 | GtkWidget *button [BUTTON_INDEX]; |
---|
99 | GtkWidget *swindow; |
---|
100 | gchar *str []= {"Example 1", "Example 2", "Example 3", "Example 4", "Example 5", "Quit"}; |
---|
101 | int i = 0; |
---|
102 | |
---|
103 | gtk_init (&argc, &argv); |
---|
104 | gdk_rgb_init (); |
---|
105 | |
---|
106 | window = gtk_window_new (GTK_WINDOW_TOPLEVEL); |
---|
107 | vbox = gtk_vbox_new (FALSE, 0); |
---|
108 | hbox = gtk_hbox_new (FALSE, 0); |
---|
109 | swindow = gtk_scrolled_window_new (NULL, NULL); |
---|
110 | html = gtk_html_new (); |
---|
111 | gtk_signal_connect (GTK_OBJECT (html), "url_requested", GTK_SIGNAL_FUNC (url_requested), NULL); |
---|
112 | |
---|
113 | for (; i < BUTTON_INDEX; i++) |
---|
114 | button [i] = gtk_button_new_with_label (str [i]); |
---|
115 | |
---|
116 | gtk_container_add (GTK_CONTAINER (window), vbox); |
---|
117 | gtk_container_add (GTK_CONTAINER (swindow), html); |
---|
118 | gtk_box_pack_start (GTK_BOX (vbox), swindow, FALSE, FALSE, 0); |
---|
119 | gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0); |
---|
120 | for (i = 0; i < BUTTON_INDEX; i++) |
---|
121 | gtk_box_pack_start (GTK_BOX (hbox), button [i], FALSE, FALSE, 0); |
---|
122 | |
---|
123 | gtk_window_set_title (GTK_WINDOW (window), "GtkHTML Test"); |
---|
124 | gtk_widget_set_usize (GTK_WIDGET (swindow), 500, 500); |
---|
125 | |
---|
126 | for (i = 0; i < BUTTON_INDEX -1; i++) |
---|
127 | gtk_signal_connect (GTK_OBJECT (button [i]), "clicked", button_cb, GINT_TO_POINTER (i)); |
---|
128 | gtk_signal_connect (GTK_OBJECT (button [BUTTON_INDEX - 1]), "clicked", quit_cb, NULL); |
---|
129 | |
---|
130 | gtk_widget_show_all (window); |
---|
131 | |
---|
132 | gtk_main (); |
---|
133 | |
---|
134 | return 0; |
---|
135 | |
---|
136 | } |
---|
137 | |
---|
138 | |
---|