1 | /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */ |
---|
2 | /* |
---|
3 | * Copyright (C) 2000 Helix Code Inc. |
---|
4 | * |
---|
5 | * Authors: Michael Zucchi <notzed@helixcode.com> |
---|
6 | * |
---|
7 | * An embeddable html widget. |
---|
8 | * |
---|
9 | * This program is free software; you can redistribute it and/or |
---|
10 | * modify it under the terms of the GNU Library General Public License |
---|
11 | * as published by the Free Software Foundation; either version 2 of |
---|
12 | * the License, or (at your option) any later version. |
---|
13 | * |
---|
14 | * This program is distributed in the hope that it will be useful, |
---|
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
17 | * GNU Library General Public License for more details. |
---|
18 | * |
---|
19 | * You should have received a copy of the GNU Library General Public |
---|
20 | * License along with this program; if not, write to the Free Software |
---|
21 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
---|
22 | */ |
---|
23 | |
---|
24 | #include <config.h> |
---|
25 | #include <gnome.h> |
---|
26 | |
---|
27 | #include "gtkhtml-embedded.h" |
---|
28 | #include "htmlengine.h" |
---|
29 | |
---|
30 | static void gtk_html_embedded_class_init (GtkHTMLEmbeddedClass *class); |
---|
31 | static void gtk_html_embedded_init (GtkHTMLEmbedded *gspaper); |
---|
32 | |
---|
33 | static void gtk_html_embedded_size_request (GtkWidget *widget, GtkRequisition *requisition); |
---|
34 | static void gtk_html_embedded_size_allocate (GtkWidget *widget, GtkAllocation *allocation); |
---|
35 | |
---|
36 | /* saved parent calls */ |
---|
37 | static void (*old_add)(GtkContainer *container, GtkWidget *child); |
---|
38 | static void (*old_remove)(GtkContainer *container, GtkWidget *child); |
---|
39 | |
---|
40 | static GtkBin *parent_class; |
---|
41 | |
---|
42 | enum { |
---|
43 | DRAW_GDK, |
---|
44 | DRAW_PRINT, |
---|
45 | CHANGED, |
---|
46 | LAST_SIGNAL |
---|
47 | }; |
---|
48 | |
---|
49 | static guint signals [LAST_SIGNAL] = { 0 }; |
---|
50 | |
---|
51 | guint |
---|
52 | gtk_html_embedded_get_type (void) |
---|
53 | { |
---|
54 | static guint select_paper_type = 0; |
---|
55 | |
---|
56 | if (!select_paper_type) { |
---|
57 | GtkTypeInfo html_embedded_info = { |
---|
58 | "GtkHTMLEmbedded", |
---|
59 | sizeof (GtkHTMLEmbedded), |
---|
60 | sizeof (GtkHTMLEmbeddedClass), |
---|
61 | (GtkClassInitFunc) gtk_html_embedded_class_init, |
---|
62 | (GtkObjectInitFunc) gtk_html_embedded_init, |
---|
63 | NULL, |
---|
64 | NULL |
---|
65 | }; |
---|
66 | |
---|
67 | select_paper_type = gtk_type_unique (gtk_bin_get_type (), &html_embedded_info); |
---|
68 | } |
---|
69 | |
---|
70 | return select_paper_type; |
---|
71 | } |
---|
72 | |
---|
73 | static void |
---|
74 | free_param(void *key, void *value, void *data) |
---|
75 | { |
---|
76 | g_free (key); |
---|
77 | g_free (value); |
---|
78 | } |
---|
79 | |
---|
80 | static void |
---|
81 | gtk_html_embedded_finalize (GtkObject *object) |
---|
82 | { |
---|
83 | GtkHTMLEmbedded *eb = GTK_HTML_EMBEDDED(object); |
---|
84 | |
---|
85 | g_hash_table_foreach (eb->params, free_param, 0); |
---|
86 | g_hash_table_destroy (eb->params); |
---|
87 | g_free(eb->classid); |
---|
88 | g_free(eb->type); |
---|
89 | |
---|
90 | GTK_OBJECT_CLASS(parent_class)->finalize (object); |
---|
91 | } |
---|
92 | |
---|
93 | static void |
---|
94 | gtk_html_embedded_changed (GtkHTMLEmbedded *ge) |
---|
95 | { |
---|
96 | gtk_signal_emit (GTK_OBJECT (ge), signals[CHANGED]); |
---|
97 | } |
---|
98 | |
---|
99 | static void gtk_html_embedded_add (GtkContainer *container, GtkWidget *child) |
---|
100 | { |
---|
101 | g_return_if_fail (container != NULL); |
---|
102 | |
---|
103 | /* can't add something twice */ |
---|
104 | g_return_if_fail( GTK_BIN(container)->child == NULL ); |
---|
105 | |
---|
106 | old_add(container, child); |
---|
107 | gtk_html_embedded_changed(GTK_HTML_EMBEDDED(container)); |
---|
108 | } |
---|
109 | |
---|
110 | static void gtk_html_embedded_remove (GtkContainer *container, GtkWidget *child) |
---|
111 | { |
---|
112 | g_return_if_fail (container != NULL); |
---|
113 | g_return_if_fail( GTK_BIN(container)->child != NULL ); |
---|
114 | |
---|
115 | old_remove(container, child); |
---|
116 | |
---|
117 | gtk_html_embedded_changed(GTK_HTML_EMBEDDED(container)); |
---|
118 | } |
---|
119 | |
---|
120 | typedef void (*draw_print_signal)(GtkObject *, gpointer, gpointer); |
---|
121 | typedef void (*draw_gdk_signal)(GtkObject *, gpointer, gpointer, gint, gint, gpointer); |
---|
122 | |
---|
123 | static void |
---|
124 | draw_gdk_signal_marshaller(GtkObject * object, GtkSignalFunc func, |
---|
125 | gpointer func_data, GtkArg * args) { |
---|
126 | draw_gdk_signal ff = (draw_gdk_signal)func; |
---|
127 | (*ff)(object, |
---|
128 | GTK_VALUE_POINTER(args[0]), |
---|
129 | GTK_VALUE_POINTER(args[1]), |
---|
130 | GTK_VALUE_INT(args[2]), |
---|
131 | GTK_VALUE_INT(args[3]), |
---|
132 | func_data); |
---|
133 | } |
---|
134 | |
---|
135 | static void |
---|
136 | gtk_html_embedded_class_init (GtkHTMLEmbeddedClass *class) |
---|
137 | { |
---|
138 | GtkObjectClass *object_class; |
---|
139 | GtkWidgetClass *widget_class; |
---|
140 | GtkContainerClass *container_class; |
---|
141 | |
---|
142 | object_class = (GtkObjectClass *) class; |
---|
143 | widget_class = (GtkWidgetClass*) class; |
---|
144 | container_class = (GtkContainerClass*) class; |
---|
145 | |
---|
146 | parent_class = gtk_type_class (gtk_bin_get_type ()); |
---|
147 | |
---|
148 | signals [CHANGED] = |
---|
149 | gtk_signal_new ("changed", |
---|
150 | GTK_RUN_FIRST, |
---|
151 | object_class->type, |
---|
152 | GTK_SIGNAL_OFFSET (GtkHTMLEmbeddedClass, changed), |
---|
153 | gtk_marshal_NONE__NONE, |
---|
154 | GTK_TYPE_NONE, 0); |
---|
155 | signals[DRAW_GDK] = |
---|
156 | gtk_signal_new("draw_gdk", GTK_RUN_FIRST, |
---|
157 | object_class->type, |
---|
158 | GTK_SIGNAL_OFFSET(GtkHTMLEmbeddedClass, draw_gdk), |
---|
159 | draw_gdk_signal_marshaller, GTK_TYPE_NONE, 4, |
---|
160 | GTK_TYPE_POINTER, GTK_TYPE_POINTER, |
---|
161 | GTK_TYPE_INT, GTK_TYPE_INT); |
---|
162 | |
---|
163 | signals[DRAW_PRINT] = |
---|
164 | gtk_signal_new("draw_print", GTK_RUN_FIRST, |
---|
165 | object_class->type, |
---|
166 | GTK_SIGNAL_OFFSET(GtkHTMLEmbeddedClass, draw_print), |
---|
167 | gtk_marshal_NONE__POINTER, |
---|
168 | GTK_TYPE_NONE, 1, |
---|
169 | GTK_TYPE_POINTER); |
---|
170 | |
---|
171 | |
---|
172 | gtk_object_class_add_signals (object_class, signals, LAST_SIGNAL); |
---|
173 | |
---|
174 | object_class->finalize = gtk_html_embedded_finalize; |
---|
175 | |
---|
176 | widget_class->size_request = gtk_html_embedded_size_request; |
---|
177 | widget_class->size_allocate = gtk_html_embedded_size_allocate; |
---|
178 | |
---|
179 | old_add = container_class->add; |
---|
180 | container_class->add = gtk_html_embedded_add; |
---|
181 | old_remove = container_class->remove; |
---|
182 | container_class->remove = gtk_html_embedded_remove; |
---|
183 | } |
---|
184 | |
---|
185 | static void |
---|
186 | gtk_html_embedded_size_request (GtkWidget *widget, GtkRequisition *requisition) |
---|
187 | { |
---|
188 | GtkBin *bin; |
---|
189 | |
---|
190 | g_return_if_fail (widget != NULL); |
---|
191 | g_return_if_fail (requisition != NULL); |
---|
192 | |
---|
193 | bin = GTK_BIN (widget); |
---|
194 | |
---|
195 | if (bin->child) { |
---|
196 | gtk_widget_size_request (bin->child, requisition); |
---|
197 | } else { |
---|
198 | requisition->width = widget->requisition.width; |
---|
199 | requisition->height = widget->requisition.height; |
---|
200 | } |
---|
201 | } |
---|
202 | |
---|
203 | static void |
---|
204 | gtk_html_embedded_size_allocate (GtkWidget *widget, GtkAllocation *allocation) |
---|
205 | { |
---|
206 | GtkBin *bin; |
---|
207 | |
---|
208 | g_return_if_fail (widget != NULL); |
---|
209 | g_return_if_fail (allocation != NULL); |
---|
210 | |
---|
211 | bin = GTK_BIN (widget); |
---|
212 | |
---|
213 | if (bin->child && GTK_WIDGET_VISIBLE (bin->child)) { |
---|
214 | gtk_widget_size_allocate(bin->child, allocation); |
---|
215 | } |
---|
216 | widget->allocation = *allocation; |
---|
217 | } |
---|
218 | |
---|
219 | static void |
---|
220 | gtk_html_embedded_init (GtkHTMLEmbedded *ge) |
---|
221 | { |
---|
222 | ge->descent = 0; |
---|
223 | ge->params = g_hash_table_new (g_str_hash, g_str_equal); |
---|
224 | } |
---|
225 | |
---|
226 | /** |
---|
227 | * gtk_html_embedded_new: |
---|
228 | * |
---|
229 | * Create a new GtkHTMLEmbedded widget. |
---|
230 | * Note that this function should never be called outside of gtkhtml. |
---|
231 | * |
---|
232 | * Return value: A new GtkHTMLEmbedded widget. |
---|
233 | **/ |
---|
234 | GtkWidget * |
---|
235 | gtk_html_embedded_new (char *classid, char *name, char *type, char *data, int width, int height) |
---|
236 | { |
---|
237 | GtkHTMLEmbedded *em; |
---|
238 | |
---|
239 | em = (GtkHTMLEmbedded *)( gtk_type_new (gtk_html_embedded_get_type ())); |
---|
240 | |
---|
241 | if (width != -1 || height != -1) |
---|
242 | gtk_widget_set_usize (GTK_WIDGET (em), width, height); |
---|
243 | |
---|
244 | em->width = width; |
---|
245 | em->height = height; |
---|
246 | em->type = type ? g_strdup(type) : NULL; |
---|
247 | em->classid = g_strdup(classid); |
---|
248 | em->name = g_strdup(name); |
---|
249 | em->data = g_strdup(data); |
---|
250 | |
---|
251 | return (GtkWidget *)em; |
---|
252 | } |
---|
253 | |
---|
254 | /** |
---|
255 | * gtk_html_embedded_get_parameter: |
---|
256 | * @ge: the #GtkHTMLEmbedded widget. |
---|
257 | * @param: the parameter to examine. |
---|
258 | * |
---|
259 | * Returns: the value of the parameter. |
---|
260 | */ |
---|
261 | char * |
---|
262 | gtk_html_embedded_get_parameter (GtkHTMLEmbedded *ge, char *param) |
---|
263 | { |
---|
264 | return g_hash_table_lookup (ge->params, param); |
---|
265 | } |
---|
266 | |
---|
267 | /** |
---|
268 | * gtk_html_embedded_set_parameter: |
---|
269 | * @ge: The #GtkHTMLEmbedded widget. |
---|
270 | * @param: the name of the parameter to set. |
---|
271 | * @value: the value of the parameter. |
---|
272 | * |
---|
273 | * The parameter named @name to the @value. |
---|
274 | */ |
---|
275 | void |
---|
276 | gtk_html_embedded_set_parameter (GtkHTMLEmbedded *ge, char *param, char *value) |
---|
277 | { |
---|
278 | gchar *lookup; |
---|
279 | |
---|
280 | if (!param) |
---|
281 | return; |
---|
282 | lookup = (gchar *) g_hash_table_lookup (ge->params, param); |
---|
283 | if (lookup) |
---|
284 | g_free (lookup); |
---|
285 | g_hash_table_insert (ge->params, |
---|
286 | lookup ? param : g_strdup (param), |
---|
287 | value ? g_strdup(value) : NULL); |
---|
288 | } |
---|
289 | |
---|
290 | /** |
---|
291 | * gtk_html_embedded_set_descent: |
---|
292 | * @ge: the GtkHTMLEmbedded widget. |
---|
293 | * @descent: the value of the new descent. |
---|
294 | * |
---|
295 | * Set the descent of the widget beneath the baseline. |
---|
296 | */ |
---|
297 | void |
---|
298 | gtk_html_embedded_set_descent (GtkHTMLEmbedded *ge, int descent) |
---|
299 | { |
---|
300 | if (ge->descent == descent) |
---|
301 | return; |
---|
302 | |
---|
303 | ge->descent = descent; |
---|
304 | gtk_html_embedded_changed (ge); |
---|
305 | } |
---|
306 | |
---|