Still bit fiddling in wxImage.
[wxWidgets.git] / src / gtk1 / win_gtk.c
1 /* ///////////////////////////////////////////////////////////////////////////
2 // Name: win_gtk.c
3 // Purpose: native GTK+ widget for wxWindows
4 // Author: Robert Roebling
5 // Id: $Id$
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////// */
9
10 #include "wx/gtk/win_gtk.h"
11 #include <gtk/gtkfeatures.h>
12
13 #ifdef __cplusplus
14 extern "C" {
15 #endif /* __cplusplus */
16
17 static void gtk_myfixed_class_init (GtkMyFixedClass *klass);
18 static void gtk_myfixed_init (GtkMyFixed *myfixed);
19 static void gtk_myfixed_map (GtkWidget *widget);
20 static void gtk_myfixed_unmap (GtkWidget *widget);
21 static void gtk_myfixed_realize (GtkWidget *widget);
22 static void gtk_myfixed_size_request (GtkWidget *widget,
23 GtkRequisition *requisition);
24 static void gtk_myfixed_size_allocate (GtkWidget *widget,
25 GtkAllocation *allocation);
26 static void gtk_myfixed_paint (GtkWidget *widget,
27 GdkRectangle *area);
28 static void gtk_myfixed_draw (GtkWidget *widget,
29 GdkRectangle *area);
30 static gint gtk_myfixed_expose (GtkWidget *widget,
31 GdkEventExpose *event);
32 static void gtk_myfixed_add (GtkContainer *container,
33 GtkWidget *widget);
34 static void gtk_myfixed_remove (GtkContainer *container,
35 GtkWidget *widget);
36 static void gtk_myfixed_foreach (GtkContainer *container,
37 #if (GTK_MINOR_VERSION == 1)
38 gboolean include_internals,
39 #endif
40 GtkCallback callback,
41 gpointer callback_data);
42
43
44 static GtkContainerClass *parent_class = NULL;
45
46
47 guint
48 gtk_myfixed_get_type ()
49 {
50 static guint myfixed_type = 0;
51
52 if (!myfixed_type)
53 {
54 GtkTypeInfo myfixed_info =
55 {
56 "GtkMyFixed",
57 sizeof (GtkMyFixed),
58 sizeof (GtkMyFixedClass),
59 (GtkClassInitFunc) gtk_myfixed_class_init,
60 (GtkObjectInitFunc) gtk_myfixed_init,
61 (GtkArgSetFunc) NULL,
62 (GtkArgGetFunc) NULL,
63 };
64
65 myfixed_type = gtk_type_unique (gtk_container_get_type (), &myfixed_info);
66 }
67
68 return myfixed_type;
69 }
70
71 static void
72 gtk_myfixed_class_init (GtkMyFixedClass *klass)
73 {
74 GtkObjectClass *object_class;
75 GtkWidgetClass *widget_class;
76 GtkContainerClass *container_class;
77
78 object_class = (GtkObjectClass*) klass;
79 widget_class = (GtkWidgetClass*) klass;
80 container_class = (GtkContainerClass*) klass;
81
82 parent_class = gtk_type_class (gtk_container_get_type ());
83
84 widget_class->map = gtk_myfixed_map;
85 widget_class->unmap = gtk_myfixed_unmap;
86 widget_class->realize = gtk_myfixed_realize;
87 widget_class->size_request = gtk_myfixed_size_request;
88 widget_class->size_allocate = gtk_myfixed_size_allocate;
89 widget_class->draw = gtk_myfixed_draw;
90 widget_class->expose_event = gtk_myfixed_expose;
91
92 container_class->add = gtk_myfixed_add;
93 container_class->remove = gtk_myfixed_remove;
94 #if (GTK_MINOR_VERSION == 1)
95 container_class->forall = gtk_myfixed_foreach;
96 #else
97 container_class->foreach = gtk_myfixed_foreach;
98 #endif
99 }
100
101 static void
102 gtk_myfixed_init (GtkMyFixed *myfixed)
103 {
104 GTK_WIDGET_UNSET_FLAGS (myfixed, GTK_NO_WINDOW);
105 GTK_WIDGET_SET_FLAGS (myfixed, GTK_BASIC);
106
107 myfixed->children = NULL;
108 }
109
110 GtkWidget*
111 gtk_myfixed_new ()
112 {
113 GtkMyFixed *myfixed;
114
115 myfixed = gtk_type_new (gtk_myfixed_get_type ());
116
117 return GTK_WIDGET (myfixed);
118 }
119
120 void
121 gtk_myfixed_put (GtkMyFixed *myfixed,
122 GtkWidget *widget,
123 gint16 x,
124 gint16 y)
125 {
126 GtkMyFixedChild *child_info;
127
128 g_return_if_fail (myfixed != NULL);
129 g_return_if_fail (GTK_IS_MYFIXED (myfixed));
130 g_return_if_fail (widget != NULL);
131
132 child_info = g_new (GtkMyFixedChild, 1);
133 child_info->widget = widget;
134 child_info->x = x;
135 child_info->y = y;
136
137 gtk_widget_set_parent (widget, GTK_WIDGET (myfixed));
138
139 myfixed->children = g_list_append (myfixed->children, child_info);
140
141 if (GTK_WIDGET_REALIZED (myfixed) && !GTK_WIDGET_REALIZED (widget))
142 gtk_widget_realize (widget);
143
144 if (GTK_WIDGET_MAPPED (myfixed) && !GTK_WIDGET_MAPPED (widget))
145 gtk_widget_map (widget);
146
147 if (GTK_WIDGET_VISIBLE (widget) && GTK_WIDGET_VISIBLE (myfixed))
148 gtk_widget_queue_resize (GTK_WIDGET (myfixed));
149 }
150
151 void
152 gtk_myfixed_move (GtkMyFixed *myfixed,
153 GtkWidget *widget,
154 gint16 x,
155 gint16 y)
156 {
157 GtkMyFixedChild *child;
158 GList *children;
159
160 g_return_if_fail (myfixed != NULL);
161 g_return_if_fail (GTK_IS_MYFIXED (myfixed));
162 g_return_if_fail (widget != NULL);
163
164 children = myfixed->children;
165 while (children)
166 {
167 child = children->data;
168 children = children->next;
169
170 if (child->widget == widget)
171 {
172 if ((child->x == x) && (child->y == y)) return;
173
174 child->x = x;
175 child->y = y;
176
177 if (GTK_WIDGET_VISIBLE (widget) && GTK_WIDGET_VISIBLE (myfixed))
178 gtk_widget_queue_resize (GTK_WIDGET (myfixed));
179
180 break;
181 }
182 }
183 }
184
185 static void
186 gtk_myfixed_map (GtkWidget *widget)
187 {
188 GtkMyFixed *myfixed;
189 GtkMyFixedChild *child;
190 GList *children;
191
192 g_return_if_fail (widget != NULL);
193 g_return_if_fail (GTK_IS_MYFIXED (widget));
194
195 GTK_WIDGET_SET_FLAGS (widget, GTK_MAPPED);
196 myfixed = GTK_MYFIXED (widget);
197
198 gdk_window_show (widget->window);
199
200 children = myfixed->children;
201 while (children)
202 {
203 child = children->data;
204 children = children->next;
205
206 if (GTK_WIDGET_VISIBLE (child->widget) &&
207 !GTK_WIDGET_MAPPED (child->widget))
208 gtk_widget_map (child->widget);
209 }
210 }
211
212 static void
213 gtk_myfixed_unmap (GtkWidget *widget)
214 {
215 g_return_if_fail (widget != NULL);
216 g_return_if_fail (GTK_IS_MYFIXED (widget));
217
218 GTK_WIDGET_UNSET_FLAGS (widget, GTK_MAPPED);
219 }
220
221 static void
222 gtk_myfixed_realize (GtkWidget *widget)
223 {
224 GtkMyFixed *myfixed;
225 GdkWindowAttr attributes;
226 gint attributes_mask;
227
228 g_return_if_fail (widget != NULL);
229 g_return_if_fail (GTK_IS_MYFIXED (widget));
230
231 myfixed = GTK_MYFIXED (widget);
232
233 GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
234
235 attributes.window_type = GDK_WINDOW_CHILD;
236 attributes.x = widget->allocation.x;
237 attributes.y = widget->allocation.y;
238 attributes.width = 32000;
239 attributes.height = 32000;
240 attributes.wclass = GDK_INPUT_OUTPUT;
241 attributes.visual = gtk_widget_get_visual (widget);
242 attributes.colormap = gtk_widget_get_colormap (widget);
243 attributes.event_mask = gtk_widget_get_events (widget);
244 attributes.event_mask |=
245 GDK_EXPOSURE_MASK |
246 GDK_POINTER_MOTION_MASK |
247 GDK_BUTTON_MOTION_MASK |
248 GDK_BUTTON1_MOTION_MASK |
249 GDK_BUTTON2_MOTION_MASK |
250 GDK_BUTTON3_MOTION_MASK |
251 GDK_BUTTON_PRESS_MASK |
252 GDK_BUTTON_RELEASE_MASK |
253 GDK_KEY_PRESS_MASK |
254 GDK_KEY_RELEASE_MASK |
255 GDK_ENTER_NOTIFY_MASK |
256 GDK_LEAVE_NOTIFY_MASK |
257 GDK_FOCUS_CHANGE_MASK;
258
259 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
260
261 widget->window = gdk_window_new (gtk_widget_get_parent_window (widget), &attributes,
262 attributes_mask);
263 gdk_window_set_user_data (widget->window, widget);
264
265 widget->style = gtk_style_attach (widget->style, widget->window);
266 gtk_style_set_background (widget->style, widget->window, GTK_STATE_NORMAL);
267 }
268
269 static void
270 gtk_myfixed_size_request (GtkWidget *widget,
271 GtkRequisition *requisition)
272 {
273 GtkMyFixed *myfixed;
274 GtkMyFixedChild *child;
275 GList *children;
276
277 g_return_if_fail (widget != NULL);
278 g_return_if_fail (GTK_IS_MYFIXED (widget));
279 g_return_if_fail (requisition != NULL);
280
281 myfixed = GTK_MYFIXED (widget);
282
283 requisition->width = 0;
284 requisition->height = 0;
285
286 children = myfixed->children;
287 while (children)
288 {
289 child = children->data;
290 children = children->next;
291
292 if (GTK_WIDGET_VISIBLE (child->widget))
293 {
294 gtk_widget_size_request (child->widget, &child->widget->requisition);
295 }
296 }
297 }
298
299 static void
300 gtk_myfixed_size_allocate (GtkWidget *widget,
301 GtkAllocation *allocation)
302 {
303 GtkMyFixed *myfixed;
304 GtkMyFixedChild *child;
305 GtkAllocation child_allocation;
306 GList *children;
307 guint16 border_width;
308
309 g_return_if_fail (widget != NULL);
310 g_return_if_fail (GTK_IS_MYFIXED(widget));
311 g_return_if_fail (allocation != NULL);
312
313 myfixed = GTK_MYFIXED (widget);
314
315 widget->allocation = *allocation;
316 if (GTK_WIDGET_REALIZED (widget))
317 gdk_window_move_resize (widget->window, allocation->x, allocation->y, 32000, 32000 );
318
319 border_width = GTK_CONTAINER (myfixed)->border_width;
320
321 children = myfixed->children;
322 while (children)
323 {
324 child = children->data;
325 children = children->next;
326
327 if (GTK_WIDGET_VISIBLE (child->widget))
328 {
329 child_allocation.x = child->x + border_width;
330 child_allocation.y = child->y + border_width;
331 child_allocation.width = child->widget->requisition.width;
332 child_allocation.height = child->widget->requisition.height;
333 gtk_widget_size_allocate (child->widget, &child_allocation);
334 }
335 }
336 }
337
338 static void
339 gtk_myfixed_paint (GtkWidget *widget,
340 GdkRectangle *area)
341 {
342 g_return_if_fail (widget != NULL);
343 g_return_if_fail (GTK_IS_MYFIXED (widget));
344 g_return_if_fail (area != NULL);
345
346 if (GTK_WIDGET_DRAWABLE (widget))
347 gdk_window_clear_area (widget->window,
348 area->x, area->y,
349 area->width, area->height);
350 }
351
352 static void
353 gtk_myfixed_draw (GtkWidget *widget,
354 GdkRectangle *area)
355 {
356 GtkMyFixed *myfixed;
357 GtkMyFixedChild *child;
358 GdkRectangle child_area;
359 GList *children;
360
361 g_return_if_fail (widget != NULL);
362 g_return_if_fail (GTK_IS_MYFIXED (widget));
363
364 if (GTK_WIDGET_DRAWABLE (widget))
365 {
366 myfixed = GTK_MYFIXED (widget);
367 gtk_myfixed_paint (widget, area);
368
369 children = myfixed->children;
370 while (children)
371 {
372 child = children->data;
373 children = children->next;
374
375 if (gtk_widget_intersect (child->widget, area, &child_area))
376 gtk_widget_draw (child->widget, &child_area);
377 }
378 }
379 }
380
381 static gint
382 gtk_myfixed_expose (GtkWidget *widget,
383 GdkEventExpose *event)
384 {
385 GtkMyFixed *myfixed;
386 GtkMyFixedChild *child;
387 GdkEventExpose child_event;
388 GList *children;
389
390 g_return_val_if_fail (widget != NULL, FALSE);
391 g_return_val_if_fail (GTK_IS_MYFIXED (widget), FALSE);
392 g_return_val_if_fail (event != NULL, FALSE);
393
394 if (GTK_WIDGET_DRAWABLE (widget))
395 {
396 myfixed = GTK_MYFIXED (widget);
397
398 child_event = *event;
399
400 children = myfixed->children;
401 while (children)
402 {
403 child = children->data;
404 children = children->next;
405
406 if (GTK_WIDGET_NO_WINDOW (child->widget) &&
407 gtk_widget_intersect (child->widget, &event->area,
408 &child_event.area))
409 gtk_widget_event (child->widget, (GdkEvent*) &child_event);
410 }
411 }
412
413 return FALSE;
414 }
415
416 static void
417 gtk_myfixed_add (GtkContainer *container,
418 GtkWidget *widget)
419 {
420 g_return_if_fail (container != NULL);
421 g_return_if_fail (GTK_IS_MYFIXED (container));
422 g_return_if_fail (widget != NULL);
423
424 gtk_myfixed_put (GTK_MYFIXED (container), widget, 0, 0);
425 }
426
427 static void
428 gtk_myfixed_remove (GtkContainer *container,
429 GtkWidget *widget)
430 {
431 GtkMyFixed *myfixed;
432 GtkMyFixedChild *child;
433 GList *children;
434
435 g_return_if_fail (container != NULL);
436 g_return_if_fail (GTK_IS_MYFIXED (container));
437 g_return_if_fail (widget != NULL);
438
439 myfixed = GTK_MYFIXED (container);
440
441 children = myfixed->children;
442 while (children)
443 {
444 child = children->data;
445
446 if (child->widget == widget)
447 {
448 gtk_widget_unparent (widget);
449
450 myfixed->children = g_list_remove_link (myfixed->children, children);
451 g_list_free (children);
452 g_free (child);
453
454 if (GTK_WIDGET_VISIBLE (widget) && GTK_WIDGET_VISIBLE (container))
455 gtk_widget_queue_resize (GTK_WIDGET (container));
456
457 break;
458 }
459
460 children = children->next;
461 }
462 }
463
464 static void
465 gtk_myfixed_foreach (GtkContainer *container,
466 #if (GTK_MINOR_VERSION == 1)
467 gboolean include_internals,
468 #endif
469 GtkCallback callback,
470 gpointer callback_data)
471 {
472 GtkMyFixed *myfixed;
473 GtkMyFixedChild *child;
474 GList *children;
475
476 g_return_if_fail (container != NULL);
477 g_return_if_fail (GTK_IS_MYFIXED (container));
478 g_return_if_fail (callback != NULL);
479
480 myfixed = GTK_MYFIXED (container);
481
482 children = myfixed->children;
483 while (children)
484 {
485 child = children->data;
486 children = children->next;
487
488 (* callback) (child->widget, callback_data);
489 }
490 }
491
492
493 #ifdef __cplusplus
494 }
495 #endif /* __cplusplus */
496