]>
Commit | Line | Data |
---|---|---|
1 | /* /////////////////////////////////////////////////////////////////////////// | |
2 | // Name: src/gtk1/win_gtk.c | |
3 | // Purpose: Native GTK+ widget for wxWidgets, based on GtkLayout and | |
4 | // GtkFixed. It makes use of the gravity window property and | |
5 | // therefore does not work with GTK 1.0. | |
6 | // Author: Robert Roebling | |
7 | // Id: $Id$ | |
8 | // Copyright: (c) 1998 Robert Roebling | |
9 | // Licence: wxWindows licence | |
10 | /////////////////////////////////////////////////////////////////////////// */ | |
11 | ||
12 | #ifdef VMS | |
13 | #define XCheckIfEvent XCHECKIFEVENT | |
14 | #endif | |
15 | ||
16 | #include "wx/platform.h" | |
17 | #include "wx/gtk1/win_gtk.h" | |
18 | #include "gtk/gtksignal.h" | |
19 | #include "gtk/gtkprivate.h" | |
20 | #include "gdk/gdkx.h" | |
21 | ||
22 | #ifdef __cplusplus | |
23 | extern "C" { | |
24 | #endif /* __cplusplus */ | |
25 | ||
26 | #include <X11/Xlib.h> | |
27 | #include <X11/Xutil.h> | |
28 | #include <X11/Xatom.h> | |
29 | ||
30 | #define IS_ONSCREEN(x,y) ((x >= G_MINSHORT) && (x <= G_MAXSHORT) && \ | |
31 | (y >= G_MINSHORT) && (y <= G_MAXSHORT)) | |
32 | ||
33 | ||
34 | typedef struct _GtkPizzaAdjData GtkPizzaAdjData; | |
35 | ||
36 | struct _GtkPizzaAdjData | |
37 | { | |
38 | gint dx; | |
39 | gint dy; | |
40 | }; | |
41 | ||
42 | static void gtk_pizza_class_init (GtkPizzaClass *klass); | |
43 | static void gtk_pizza_init (GtkPizza *pizza); | |
44 | ||
45 | static void gtk_pizza_realize (GtkWidget *widget); | |
46 | static void gtk_pizza_unrealize (GtkWidget *widget); | |
47 | ||
48 | static void gtk_pizza_map (GtkWidget *widget); | |
49 | ||
50 | static void gtk_pizza_size_request (GtkWidget *widget, | |
51 | GtkRequisition *requisition); | |
52 | static void gtk_pizza_size_allocate (GtkWidget *widget, | |
53 | GtkAllocation *allocation); | |
54 | static void gtk_pizza_draw (GtkWidget *widget, | |
55 | GdkRectangle *area); | |
56 | static gint gtk_pizza_expose (GtkWidget *widget, | |
57 | GdkEventExpose *event); | |
58 | static void gtk_pizza_style_set (GtkWidget *widget, | |
59 | GtkStyle *previous_style); | |
60 | static void gtk_pizza_add (GtkContainer *container, | |
61 | GtkWidget *widget); | |
62 | static void gtk_pizza_remove (GtkContainer *container, | |
63 | GtkWidget *widget); | |
64 | static void gtk_pizza_forall (GtkContainer *container, | |
65 | gboolean include_internals, | |
66 | GtkCallback callback, | |
67 | gpointer callback_data); | |
68 | ||
69 | static void gtk_pizza_allocate_child (GtkPizza *pizza, | |
70 | GtkPizzaChild *child); | |
71 | static void gtk_pizza_adjust_allocations_recurse (GtkWidget *widget, | |
72 | gpointer cb_data); | |
73 | ||
74 | static void gtk_pizza_position_child (GtkPizza *pizza, | |
75 | GtkPizzaChild *child); | |
76 | static void gtk_pizza_position_children (GtkPizza *pizza); | |
77 | ||
78 | static GdkFilterReturn gtk_pizza_filter (GdkXEvent *gdk_xevent, | |
79 | GdkEvent *event, | |
80 | gpointer data); | |
81 | static GdkFilterReturn gtk_pizza_main_filter (GdkXEvent *gdk_xevent, | |
82 | GdkEvent *event, | |
83 | gpointer data); | |
84 | ||
85 | static GtkType gtk_pizza_child_type (GtkContainer *container); | |
86 | ||
87 | static void gtk_pizza_scroll_set_adjustments (GtkPizza *pizza, | |
88 | GtkAdjustment *hadj, | |
89 | GtkAdjustment *vadj); | |
90 | ||
91 | ||
92 | static GtkContainerClass *pizza_parent_class = NULL; | |
93 | ||
94 | static gboolean gravity_works; | |
95 | ||
96 | GtkType | |
97 | gtk_pizza_get_type () | |
98 | { | |
99 | static GtkType pizza_type = 0; | |
100 | ||
101 | if (!pizza_type) | |
102 | { | |
103 | GtkTypeInfo pizza_info = | |
104 | { | |
105 | "GtkPizza", | |
106 | sizeof (GtkPizza), | |
107 | sizeof (GtkPizzaClass), | |
108 | (GtkClassInitFunc) gtk_pizza_class_init, | |
109 | (GtkObjectInitFunc) gtk_pizza_init, | |
110 | /* reserved_1 */ NULL, | |
111 | /* reserved_2 */ NULL, | |
112 | (GtkClassInitFunc) NULL, | |
113 | }; | |
114 | pizza_type = gtk_type_unique (gtk_container_get_type (), &pizza_info); | |
115 | } | |
116 | ||
117 | return pizza_type; | |
118 | } | |
119 | ||
120 | static void | |
121 | gtk_pizza_class_init (GtkPizzaClass *klass) | |
122 | { | |
123 | GtkObjectClass *object_class; | |
124 | GtkWidgetClass *widget_class; | |
125 | GtkContainerClass *container_class; | |
126 | ||
127 | object_class = (GtkObjectClass*) klass; | |
128 | widget_class = (GtkWidgetClass*) klass; | |
129 | container_class = (GtkContainerClass*) klass; | |
130 | pizza_parent_class = gtk_type_class (GTK_TYPE_CONTAINER); | |
131 | ||
132 | widget_class->map = gtk_pizza_map; | |
133 | widget_class->realize = gtk_pizza_realize; | |
134 | widget_class->unrealize = gtk_pizza_unrealize; | |
135 | widget_class->size_request = gtk_pizza_size_request; | |
136 | widget_class->size_allocate = gtk_pizza_size_allocate; | |
137 | widget_class->draw = gtk_pizza_draw; | |
138 | widget_class->expose_event = gtk_pizza_expose; | |
139 | widget_class->style_set = gtk_pizza_style_set; | |
140 | ||
141 | container_class->add = gtk_pizza_add; | |
142 | container_class->remove = gtk_pizza_remove; | |
143 | container_class->forall = gtk_pizza_forall; | |
144 | ||
145 | container_class->child_type = gtk_pizza_child_type; | |
146 | ||
147 | klass->set_scroll_adjustments = gtk_pizza_scroll_set_adjustments; | |
148 | ||
149 | widget_class->set_scroll_adjustments_signal = | |
150 | gtk_signal_new ("set_scroll_adjustments", | |
151 | GTK_RUN_LAST, | |
152 | object_class->type, | |
153 | GTK_SIGNAL_OFFSET (GtkPizzaClass, set_scroll_adjustments), | |
154 | gtk_marshal_NONE__POINTER_POINTER, | |
155 | GTK_TYPE_NONE, 2, GTK_TYPE_ADJUSTMENT, GTK_TYPE_ADJUSTMENT); | |
156 | } | |
157 | ||
158 | static GtkType | |
159 | gtk_pizza_child_type (GtkContainer *container) | |
160 | { | |
161 | return GTK_TYPE_WIDGET; | |
162 | } | |
163 | ||
164 | static void | |
165 | gtk_pizza_init (GtkPizza *pizza) | |
166 | { | |
167 | GTK_WIDGET_UNSET_FLAGS (pizza, GTK_NO_WINDOW); | |
168 | ||
169 | pizza->shadow_type = GTK_MYSHADOW_NONE; | |
170 | ||
171 | pizza->children = NULL; | |
172 | ||
173 | pizza->width = 20; | |
174 | pizza->height = 20; | |
175 | ||
176 | pizza->bin_window = NULL; | |
177 | ||
178 | pizza->xoffset = 0; | |
179 | pizza->yoffset = 0; | |
180 | ||
181 | pizza->configure_serial = 0; | |
182 | pizza->scroll_x = 0; | |
183 | pizza->scroll_y = 0; | |
184 | pizza->visibility = GDK_VISIBILITY_PARTIAL; | |
185 | ||
186 | pizza->clear_on_draw = TRUE; | |
187 | pizza->use_filter = TRUE; | |
188 | pizza->external_expose = FALSE; | |
189 | } | |
190 | ||
191 | GtkWidget* | |
192 | gtk_pizza_new () | |
193 | { | |
194 | GtkPizza *pizza; | |
195 | ||
196 | pizza = gtk_type_new (gtk_pizza_get_type ()); | |
197 | ||
198 | return GTK_WIDGET (pizza); | |
199 | } | |
200 | ||
201 | static void | |
202 | gtk_pizza_scroll_set_adjustments (GtkPizza *pizza, | |
203 | GtkAdjustment *hadj, | |
204 | GtkAdjustment *vadj) | |
205 | { | |
206 | /* We handle scrolling in the wxScrolledWindow, not here. */ | |
207 | } | |
208 | ||
209 | void | |
210 | gtk_pizza_set_shadow_type (GtkPizza *pizza, | |
211 | GtkMyShadowType type) | |
212 | { | |
213 | g_return_if_fail (pizza != NULL); | |
214 | g_return_if_fail (GTK_IS_PIZZA (pizza)); | |
215 | ||
216 | if ((GtkMyShadowType) pizza->shadow_type != type) | |
217 | { | |
218 | pizza->shadow_type = type; | |
219 | ||
220 | if (GTK_WIDGET_VISIBLE (pizza)) | |
221 | { | |
222 | gtk_widget_size_allocate (GTK_WIDGET (pizza), &(GTK_WIDGET (pizza)->allocation)); | |
223 | gtk_widget_queue_draw (GTK_WIDGET (pizza)); | |
224 | } | |
225 | } | |
226 | } | |
227 | ||
228 | void | |
229 | gtk_pizza_set_clear (GtkPizza *pizza, | |
230 | gboolean clear) | |
231 | { | |
232 | g_return_if_fail (pizza != NULL); | |
233 | g_return_if_fail (GTK_IS_PIZZA (pizza)); | |
234 | ||
235 | pizza->clear_on_draw = clear; | |
236 | } | |
237 | ||
238 | void | |
239 | gtk_pizza_set_filter (GtkPizza *pizza, | |
240 | gboolean use) | |
241 | { | |
242 | g_return_if_fail (pizza != NULL); | |
243 | g_return_if_fail (GTK_IS_PIZZA (pizza)); | |
244 | ||
245 | pizza->use_filter = use; | |
246 | } | |
247 | ||
248 | void | |
249 | gtk_pizza_set_external (GtkPizza *pizza, | |
250 | gboolean expose) | |
251 | { | |
252 | g_return_if_fail (pizza != NULL); | |
253 | g_return_if_fail (GTK_IS_PIZZA (pizza)); | |
254 | ||
255 | pizza->external_expose = expose; | |
256 | } | |
257 | ||
258 | void | |
259 | gtk_pizza_put (GtkPizza *pizza, | |
260 | GtkWidget *widget, | |
261 | gint x, | |
262 | gint y, | |
263 | gint width, | |
264 | gint height) | |
265 | { | |
266 | GtkPizzaChild *child_info; | |
267 | ||
268 | g_return_if_fail (pizza != NULL); | |
269 | g_return_if_fail (GTK_IS_PIZZA (pizza)); | |
270 | g_return_if_fail (widget != NULL); | |
271 | ||
272 | child_info = g_new (GtkPizzaChild, 1); | |
273 | ||
274 | child_info->widget = widget; | |
275 | child_info->x = x; | |
276 | child_info->y = y; | |
277 | child_info->width = width; | |
278 | child_info->height = height; | |
279 | ||
280 | pizza->children = g_list_append (pizza->children, child_info); | |
281 | ||
282 | if (GTK_WIDGET_REALIZED (pizza)) | |
283 | gtk_widget_set_parent_window (widget, pizza->bin_window); | |
284 | ||
285 | gtk_widget_set_parent (widget, GTK_WIDGET (pizza)); | |
286 | ||
287 | if (!IS_ONSCREEN (x, y)) | |
288 | GTK_PRIVATE_SET_FLAG (widget, GTK_IS_OFFSCREEN); | |
289 | ||
290 | gtk_widget_set_usize (widget, width, height); | |
291 | } | |
292 | ||
293 | void | |
294 | gtk_pizza_move (GtkPizza *pizza, | |
295 | GtkWidget *widget, | |
296 | gint x, | |
297 | gint y) | |
298 | { | |
299 | GtkPizzaChild *child; | |
300 | GList *children; | |
301 | ||
302 | g_return_if_fail (pizza != NULL); | |
303 | g_return_if_fail (GTK_IS_PIZZA (pizza)); | |
304 | g_return_if_fail (widget != NULL); | |
305 | ||
306 | children = pizza->children; | |
307 | while (children) | |
308 | { | |
309 | child = children->data; | |
310 | children = children->next; | |
311 | ||
312 | if (child->widget == widget) | |
313 | { | |
314 | if ((child->x == x) && (child->y == y)) | |
315 | break; | |
316 | ||
317 | child->x = x; | |
318 | child->y = y; | |
319 | ||
320 | if (GTK_WIDGET_VISIBLE (widget) && GTK_WIDGET_VISIBLE (pizza)) | |
321 | gtk_widget_queue_resize (widget); | |
322 | break; | |
323 | } | |
324 | } | |
325 | } | |
326 | ||
327 | void | |
328 | gtk_pizza_resize (GtkPizza *pizza, | |
329 | GtkWidget *widget, | |
330 | gint width, | |
331 | gint height) | |
332 | { | |
333 | GtkPizzaChild *child; | |
334 | GList *children; | |
335 | ||
336 | g_return_if_fail (pizza != NULL); | |
337 | g_return_if_fail (GTK_IS_PIZZA (pizza)); | |
338 | g_return_if_fail (widget != NULL); | |
339 | ||
340 | children = pizza->children; | |
341 | while (children) | |
342 | { | |
343 | child = children->data; | |
344 | children = children->next; | |
345 | ||
346 | if (child->widget == widget) | |
347 | { | |
348 | if ((child->width == width) && (child->height == height)) | |
349 | break; | |
350 | ||
351 | child->width = width; | |
352 | child->height = height; | |
353 | ||
354 | gtk_widget_set_usize (widget, width, height); | |
355 | ||
356 | if (GTK_WIDGET_VISIBLE (widget) && GTK_WIDGET_VISIBLE (pizza)) | |
357 | gtk_widget_queue_resize (widget); | |
358 | break; | |
359 | } | |
360 | } | |
361 | } | |
362 | ||
363 | void | |
364 | gtk_pizza_set_size (GtkPizza *pizza, | |
365 | GtkWidget *widget, | |
366 | gint x, | |
367 | gint y, | |
368 | gint width, | |
369 | gint height) | |
370 | { | |
371 | GtkPizzaChild *child; | |
372 | GList *children; | |
373 | ||
374 | g_return_if_fail (pizza != NULL); | |
375 | g_return_if_fail (GTK_IS_PIZZA (pizza)); | |
376 | g_return_if_fail (widget != NULL); | |
377 | ||
378 | children = pizza->children; | |
379 | while (children) | |
380 | { | |
381 | child = children->data; | |
382 | children = children->next; | |
383 | ||
384 | if (child->widget == widget) | |
385 | { | |
386 | if ((child->x == x) && | |
387 | (child->y == y) && | |
388 | (child->width == width) && | |
389 | (child->height == height)) return; | |
390 | ||
391 | child->x = x; | |
392 | child->y = y; | |
393 | child->width = width; | |
394 | child->height = height; | |
395 | ||
396 | gtk_widget_set_usize (widget, width, height); | |
397 | ||
398 | if (GTK_WIDGET_VISIBLE (widget) && GTK_WIDGET_VISIBLE (pizza)) | |
399 | gtk_widget_queue_resize (widget); | |
400 | ||
401 | return; | |
402 | } | |
403 | } | |
404 | } | |
405 | ||
406 | gint | |
407 | gtk_pizza_child_resized (GtkPizza *pizza, | |
408 | GtkWidget *widget) | |
409 | { | |
410 | GtkPizzaChild *child; | |
411 | GList *children; | |
412 | ||
413 | g_return_val_if_fail (pizza != NULL, FALSE); | |
414 | g_return_val_if_fail (GTK_IS_PIZZA (pizza), FALSE); | |
415 | g_return_val_if_fail (widget != NULL, FALSE); | |
416 | ||
417 | children = pizza->children; | |
418 | while (children) | |
419 | { | |
420 | child = children->data; | |
421 | children = children->next; | |
422 | ||
423 | if (child->widget == widget) | |
424 | { | |
425 | return ((child->width == widget->allocation.width) && | |
426 | (child->height == widget->allocation.height)); | |
427 | } | |
428 | } | |
429 | ||
430 | return FALSE; | |
431 | } | |
432 | ||
433 | static void | |
434 | gtk_pizza_map (GtkWidget *widget) | |
435 | { | |
436 | GtkPizza *pizza; | |
437 | GtkPizzaChild *child; | |
438 | GList *children; | |
439 | ||
440 | g_return_if_fail (widget != NULL); | |
441 | g_return_if_fail (GTK_IS_PIZZA (widget)); | |
442 | ||
443 | GTK_WIDGET_SET_FLAGS (widget, GTK_MAPPED); | |
444 | pizza = GTK_PIZZA (widget); | |
445 | ||
446 | children = pizza->children; | |
447 | while (children) | |
448 | { | |
449 | child = children->data; | |
450 | children = children->next; | |
451 | ||
452 | if ( GTK_WIDGET_VISIBLE (child->widget) && | |
453 | !GTK_WIDGET_MAPPED (child->widget) && | |
454 | !GTK_WIDGET_IS_OFFSCREEN (child->widget)) | |
455 | { | |
456 | gtk_widget_map (child->widget); | |
457 | } | |
458 | } | |
459 | ||
460 | gdk_window_show (widget->window); | |
461 | gdk_window_show (pizza->bin_window); | |
462 | } | |
463 | ||
464 | static void | |
465 | gtk_pizza_realize (GtkWidget *widget) | |
466 | { | |
467 | GtkPizza *pizza; | |
468 | GdkWindowAttr attributes; | |
469 | gint attributes_mask; | |
470 | GtkPizzaChild *child; | |
471 | GList *children; | |
472 | ||
473 | g_return_if_fail (widget != NULL); | |
474 | g_return_if_fail (GTK_IS_PIZZA (widget)); | |
475 | ||
476 | pizza = GTK_PIZZA (widget); | |
477 | GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED); | |
478 | ||
479 | attributes.window_type = GDK_WINDOW_CHILD; | |
480 | ||
481 | attributes.x = widget->allocation.x; | |
482 | attributes.y = widget->allocation.y; | |
483 | attributes.width = widget->allocation.width; | |
484 | attributes.height = widget->allocation.height; | |
485 | ||
486 | #ifndef __WXUNIVERSAL__ | |
487 | if (pizza->shadow_type == GTK_MYSHADOW_NONE) | |
488 | { | |
489 | /* no border, no changes to sizes */ | |
490 | } | |
491 | else if (pizza->shadow_type == GTK_MYSHADOW_THIN) | |
492 | { | |
493 | /* GTK_MYSHADOW_THIN == wxSIMPLE_BORDER */ | |
494 | attributes.x += 1; | |
495 | attributes.y += 1; | |
496 | attributes.width -= 2; | |
497 | attributes.height -= 2; | |
498 | } | |
499 | else | |
500 | { | |
501 | /* GTK_MYSHADOW_IN == wxSUNKEN_BORDER */ | |
502 | /* GTK_MYSHADOW_OUT == wxRAISED_BORDER */ | |
503 | attributes.x += 2; | |
504 | attributes.y += 2; | |
505 | attributes.width -= 4; | |
506 | attributes.height -= 4; | |
507 | } | |
508 | #endif /* __WXUNIVERSAL__ */ | |
509 | ||
510 | /* minimal size */ | |
511 | if (attributes.width < 2) attributes.width = 2; | |
512 | if (attributes.height < 2) attributes.height = 2; | |
513 | ||
514 | attributes.wclass = GDK_INPUT_OUTPUT; | |
515 | attributes.visual = gtk_widget_get_visual (widget); | |
516 | attributes.colormap = gtk_widget_get_colormap (widget); | |
517 | attributes.event_mask = GDK_VISIBILITY_NOTIFY_MASK; | |
518 | attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP; | |
519 | ||
520 | widget->window = gdk_window_new(gtk_widget_get_parent_window (widget), | |
521 | &attributes, attributes_mask); | |
522 | gdk_window_set_user_data (widget->window, widget); | |
523 | ||
524 | attributes.x = 0; | |
525 | attributes.y = 0; | |
526 | ||
527 | attributes.event_mask = gtk_widget_get_events (widget); | |
528 | attributes.event_mask |= GDK_EXPOSURE_MASK | | |
529 | GDK_POINTER_MOTION_MASK | | |
530 | GDK_POINTER_MOTION_HINT_MASK | | |
531 | GDK_BUTTON_MOTION_MASK | | |
532 | GDK_BUTTON1_MOTION_MASK | | |
533 | GDK_BUTTON2_MOTION_MASK | | |
534 | GDK_BUTTON3_MOTION_MASK | | |
535 | GDK_BUTTON_PRESS_MASK | | |
536 | GDK_BUTTON_RELEASE_MASK | | |
537 | GDK_KEY_PRESS_MASK | | |
538 | GDK_KEY_RELEASE_MASK | | |
539 | GDK_ENTER_NOTIFY_MASK | | |
540 | GDK_LEAVE_NOTIFY_MASK | | |
541 | GDK_FOCUS_CHANGE_MASK; | |
542 | ||
543 | pizza->bin_window = gdk_window_new(widget->window, | |
544 | &attributes, attributes_mask); | |
545 | gdk_window_set_user_data (pizza->bin_window, widget); | |
546 | ||
547 | widget->style = gtk_style_attach (widget->style, widget->window); | |
548 | gtk_style_set_background (widget->style, widget->window, GTK_STATE_NORMAL); | |
549 | gtk_style_set_background (widget->style, pizza->bin_window, GTK_STATE_NORMAL ); | |
550 | ||
551 | /* | |
552 | gdk_window_set_back_pixmap( widget->window, NULL, FALSE ); | |
553 | gdk_window_set_back_pixmap( pizza->bin_window, NULL, FALSE ); | |
554 | */ | |
555 | ||
556 | /* add filters for intercepting visibility and expose events */ | |
557 | gdk_window_add_filter (widget->window, gtk_pizza_main_filter, pizza); | |
558 | gdk_window_add_filter (pizza->bin_window, gtk_pizza_filter, pizza); | |
559 | ||
560 | /* we NEED gravity or we'll give up */ | |
561 | gravity_works = gdk_window_set_static_gravities (pizza->bin_window, TRUE); | |
562 | ||
563 | /* cannot be done before realisation */ | |
564 | children = pizza->children; | |
565 | while (children) | |
566 | { | |
567 | child = children->data; | |
568 | children = children->next; | |
569 | ||
570 | gtk_widget_set_parent_window (child->widget, pizza->bin_window); | |
571 | } | |
572 | } | |
573 | ||
574 | static void | |
575 | gtk_pizza_unrealize (GtkWidget *widget) | |
576 | { | |
577 | GtkPizza *pizza; | |
578 | ||
579 | g_return_if_fail (widget != NULL); | |
580 | g_return_if_fail (GTK_IS_PIZZA (widget)); | |
581 | ||
582 | pizza = GTK_PIZZA (widget); | |
583 | ||
584 | gdk_window_set_user_data (pizza->bin_window, NULL); | |
585 | gdk_window_destroy (pizza->bin_window); | |
586 | pizza->bin_window = NULL; | |
587 | ||
588 | if (GTK_WIDGET_CLASS (pizza_parent_class)->unrealize) | |
589 | (* GTK_WIDGET_CLASS (pizza_parent_class)->unrealize) (widget); | |
590 | } | |
591 | ||
592 | static void | |
593 | gtk_pizza_size_request (GtkWidget *widget, | |
594 | GtkRequisition *requisition) | |
595 | { | |
596 | GtkPizza *pizza; | |
597 | GtkPizzaChild *child; | |
598 | GList *children; | |
599 | GtkRequisition child_requisition; | |
600 | ||
601 | g_return_if_fail (widget != NULL); | |
602 | g_return_if_fail (GTK_IS_PIZZA (widget)); | |
603 | g_return_if_fail (requisition != NULL); | |
604 | ||
605 | pizza = GTK_PIZZA (widget); | |
606 | ||
607 | children = pizza->children; | |
608 | while (children) | |
609 | { | |
610 | child = children->data; | |
611 | children = children->next; | |
612 | ||
613 | if (GTK_WIDGET_VISIBLE (child->widget)) | |
614 | { | |
615 | gtk_widget_size_request (child->widget, &child_requisition); | |
616 | } | |
617 | } | |
618 | ||
619 | /* request very little, I'm not sure if requesting nothing | |
620 | will always have positive effects on stability... */ | |
621 | requisition->width = 2; | |
622 | requisition->height = 2; | |
623 | } | |
624 | ||
625 | static void | |
626 | gtk_pizza_size_allocate (GtkWidget *widget, | |
627 | GtkAllocation *allocation) | |
628 | { | |
629 | GtkPizza *pizza; | |
630 | gint border; | |
631 | gint x,y,w,h; | |
632 | GtkPizzaChild *child; | |
633 | GList *children; | |
634 | ||
635 | g_return_if_fail (widget != NULL); | |
636 | g_return_if_fail (GTK_IS_PIZZA(widget)); | |
637 | g_return_if_fail (allocation != NULL); | |
638 | ||
639 | pizza = GTK_PIZZA (widget); | |
640 | ||
641 | widget->allocation = *allocation; | |
642 | ||
643 | if (pizza->shadow_type == GTK_MYSHADOW_NONE) | |
644 | border = 0; | |
645 | else | |
646 | if (pizza->shadow_type == GTK_MYSHADOW_THIN) | |
647 | border = 1; | |
648 | else | |
649 | border = 2; | |
650 | ||
651 | x = allocation->x + border; | |
652 | y = allocation->y + border; | |
653 | w = allocation->width - border*2; | |
654 | h = allocation->height - border*2; | |
655 | ||
656 | if (GTK_WIDGET_REALIZED (widget)) | |
657 | { | |
658 | gdk_window_move_resize( widget->window, x, y, w, h ); | |
659 | gdk_window_move_resize( pizza->bin_window, 0, 0, w, h ); | |
660 | } | |
661 | ||
662 | children = pizza->children; | |
663 | while (children) | |
664 | { | |
665 | child = children->data; | |
666 | children = children->next; | |
667 | ||
668 | gtk_pizza_position_child (pizza, child); | |
669 | gtk_pizza_allocate_child (pizza, child); | |
670 | } | |
671 | } | |
672 | ||
673 | static void | |
674 | gtk_pizza_draw (GtkWidget *widget, | |
675 | GdkRectangle *area) | |
676 | { | |
677 | GtkPizza *pizza; | |
678 | GtkPizzaChild *child; | |
679 | GdkRectangle child_area; | |
680 | GList *children; | |
681 | ||
682 | g_return_if_fail (widget != NULL); | |
683 | g_return_if_fail (GTK_IS_PIZZA (widget)); | |
684 | ||
685 | pizza = GTK_PIZZA (widget); | |
686 | ||
687 | /* Sometimes, We handle all expose events in window.cpp now. */ | |
688 | if (pizza->external_expose) | |
689 | return; | |
690 | ||
691 | children = pizza->children; | |
692 | if ( !(GTK_WIDGET_APP_PAINTABLE (widget)) && | |
693 | (pizza->clear_on_draw)) | |
694 | { | |
695 | gdk_window_clear_area( pizza->bin_window, | |
696 | area->x, area->y, area->width, area->height); | |
697 | } | |
698 | ||
699 | while (children) | |
700 | { | |
701 | child = children->data; | |
702 | children = children->next; | |
703 | ||
704 | if (gtk_widget_intersect (child->widget, area, &child_area)) | |
705 | gtk_widget_draw (child->widget, &child_area); | |
706 | } | |
707 | } | |
708 | ||
709 | static gint | |
710 | gtk_pizza_expose (GtkWidget *widget, | |
711 | GdkEventExpose *event) | |
712 | { | |
713 | GtkPizza *pizza; | |
714 | GtkPizzaChild *child; | |
715 | GdkEventExpose child_event; | |
716 | GList *children; | |
717 | ||
718 | g_return_val_if_fail (widget != NULL, FALSE); | |
719 | g_return_val_if_fail (GTK_IS_PIZZA (widget), FALSE); | |
720 | g_return_val_if_fail (event != NULL, FALSE); | |
721 | ||
722 | pizza = GTK_PIZZA (widget); | |
723 | ||
724 | if (event->window != pizza->bin_window) | |
725 | return FALSE; | |
726 | ||
727 | /* We handle all expose events in window.cpp now. */ | |
728 | if (pizza->external_expose) | |
729 | return FALSE; | |
730 | ||
731 | children = pizza->children; | |
732 | while (children) | |
733 | { | |
734 | child = children->data; | |
735 | children = children->next; | |
736 | ||
737 | child_event = *event; | |
738 | ||
739 | if (GTK_WIDGET_NO_WINDOW (child->widget) && | |
740 | GTK_WIDGET_DRAWABLE (child->widget) && | |
741 | gtk_widget_intersect (child->widget, &event->area, &child_event.area)) | |
742 | { | |
743 | gtk_widget_event (child->widget, (GdkEvent*) &child_event); | |
744 | } | |
745 | } | |
746 | ||
747 | return TRUE; | |
748 | } | |
749 | ||
750 | static void | |
751 | gtk_pizza_style_set(GtkWidget *widget, GtkStyle *previous_style) | |
752 | { | |
753 | if (GTK_WIDGET_REALIZED(widget)) | |
754 | { | |
755 | gtk_style_set_background(widget->style, widget->window, GTK_STATE_NORMAL); | |
756 | gtk_style_set_background(widget->style, GTK_PIZZA(widget)->bin_window, GTK_STATE_NORMAL ); | |
757 | } | |
758 | ||
759 | (* GTK_WIDGET_CLASS (pizza_parent_class)->style_set) (widget, previous_style); | |
760 | } | |
761 | ||
762 | static void | |
763 | gtk_pizza_add (GtkContainer *container, | |
764 | GtkWidget *widget) | |
765 | { | |
766 | g_return_if_fail (container != NULL); | |
767 | g_return_if_fail (GTK_IS_PIZZA (container)); | |
768 | g_return_if_fail (widget != NULL); | |
769 | ||
770 | gtk_pizza_put (GTK_PIZZA (container), widget, 0, 0, 20, 20 ); | |
771 | } | |
772 | ||
773 | static void | |
774 | gtk_pizza_remove (GtkContainer *container, | |
775 | GtkWidget *widget) | |
776 | { | |
777 | GtkPizza *pizza; | |
778 | GtkPizzaChild *child; | |
779 | GList *children; | |
780 | ||
781 | g_return_if_fail (container != NULL); | |
782 | g_return_if_fail (GTK_IS_PIZZA (container)); | |
783 | g_return_if_fail (widget != NULL); | |
784 | ||
785 | pizza = GTK_PIZZA (container); | |
786 | ||
787 | children = pizza->children; | |
788 | while (children) | |
789 | { | |
790 | child = children->data; | |
791 | ||
792 | if (child->widget == widget) | |
793 | { | |
794 | gtk_widget_unparent (widget); | |
795 | ||
796 | /* security checks */ | |
797 | g_return_if_fail (GTK_IS_WIDGET (widget)); | |
798 | ||
799 | pizza->children = g_list_remove_link (pizza->children, children); | |
800 | g_list_free (children); | |
801 | g_free (child); | |
802 | ||
803 | /* security checks */ | |
804 | g_return_if_fail (GTK_IS_WIDGET (widget)); | |
805 | ||
806 | GTK_PRIVATE_UNSET_FLAG (widget, GTK_IS_OFFSCREEN); | |
807 | ||
808 | break; | |
809 | } | |
810 | ||
811 | children = children->next; | |
812 | } | |
813 | } | |
814 | ||
815 | static void | |
816 | gtk_pizza_forall (GtkContainer *container, | |
817 | gboolean include_internals, | |
818 | GtkCallback callback, | |
819 | gpointer callback_data) | |
820 | { | |
821 | GtkPizza *pizza; | |
822 | GtkPizzaChild *child; | |
823 | GList *children; | |
824 | ||
825 | g_return_if_fail (container != NULL); | |
826 | g_return_if_fail (GTK_IS_PIZZA (container)); | |
827 | g_return_if_fail (callback != (GtkCallback)NULL); | |
828 | ||
829 | pizza = GTK_PIZZA (container); | |
830 | ||
831 | children = pizza->children; | |
832 | while (children) | |
833 | { | |
834 | child = children->data; | |
835 | children = children->next; | |
836 | ||
837 | (* callback) (child->widget, callback_data); | |
838 | } | |
839 | } | |
840 | ||
841 | static void | |
842 | gtk_pizza_allocate_child (GtkPizza *pizza, | |
843 | GtkPizzaChild *child) | |
844 | { | |
845 | GtkAllocation allocation; | |
846 | GtkRequisition requisition; | |
847 | ||
848 | allocation.x = child->x - pizza->xoffset; | |
849 | allocation.y = child->y - pizza->yoffset; | |
850 | gtk_widget_get_child_requisition (child->widget, &requisition); | |
851 | allocation.width = requisition.width; | |
852 | allocation.height = requisition.height; | |
853 | ||
854 | gtk_widget_size_allocate (child->widget, &allocation); | |
855 | } | |
856 | ||
857 | static void | |
858 | gtk_pizza_adjust_allocations_recurse (GtkWidget *widget, | |
859 | gpointer cb_data) | |
860 | { | |
861 | GtkPizzaAdjData *data = cb_data; | |
862 | ||
863 | widget->allocation.x += data->dx; | |
864 | widget->allocation.y += data->dy; | |
865 | ||
866 | if (GTK_WIDGET_NO_WINDOW (widget) && GTK_IS_CONTAINER (widget)) | |
867 | { | |
868 | gtk_container_forall (GTK_CONTAINER (widget), | |
869 | gtk_pizza_adjust_allocations_recurse, | |
870 | cb_data); | |
871 | } | |
872 | } | |
873 | ||
874 | static void | |
875 | gtk_pizza_adjust_allocations (GtkPizza *pizza, | |
876 | gint dx, | |
877 | gint dy) | |
878 | { | |
879 | GList *tmp_list; | |
880 | GtkPizzaAdjData data; | |
881 | ||
882 | data.dx = dx; | |
883 | data.dy = dy; | |
884 | ||
885 | tmp_list = pizza->children; | |
886 | while (tmp_list) | |
887 | { | |
888 | GtkPizzaChild *child = tmp_list->data; | |
889 | tmp_list = tmp_list->next; | |
890 | ||
891 | child->widget->allocation.x += dx; | |
892 | child->widget->allocation.y += dy; | |
893 | ||
894 | if (GTK_WIDGET_NO_WINDOW (child->widget) && | |
895 | GTK_IS_CONTAINER (child->widget)) | |
896 | { | |
897 | gtk_container_forall (GTK_CONTAINER (child->widget), | |
898 | gtk_pizza_adjust_allocations_recurse, | |
899 | &data); | |
900 | } | |
901 | } | |
902 | } | |
903 | ||
904 | static void | |
905 | gtk_pizza_position_child (GtkPizza *pizza, | |
906 | GtkPizzaChild *child) | |
907 | { | |
908 | gint x; | |
909 | gint y; | |
910 | ||
911 | x = child->x - pizza->xoffset; | |
912 | y = child->y - pizza->yoffset; | |
913 | ||
914 | if (IS_ONSCREEN (x,y)) | |
915 | { | |
916 | if (GTK_WIDGET_MAPPED (pizza) && | |
917 | GTK_WIDGET_VISIBLE (child->widget)) | |
918 | { | |
919 | if (!GTK_WIDGET_MAPPED (child->widget)) | |
920 | gtk_widget_map (child->widget); | |
921 | } | |
922 | ||
923 | if (GTK_WIDGET_IS_OFFSCREEN (child->widget)) | |
924 | GTK_PRIVATE_UNSET_FLAG (child->widget, GTK_IS_OFFSCREEN); | |
925 | } | |
926 | else | |
927 | { | |
928 | if (!GTK_WIDGET_IS_OFFSCREEN (child->widget)) | |
929 | GTK_PRIVATE_SET_FLAG (child->widget, GTK_IS_OFFSCREEN); | |
930 | ||
931 | if (GTK_WIDGET_MAPPED (child->widget)) | |
932 | gtk_widget_unmap (child->widget); | |
933 | } | |
934 | } | |
935 | ||
936 | static void | |
937 | gtk_pizza_position_children (GtkPizza *pizza) | |
938 | { | |
939 | GList *tmp_list; | |
940 | ||
941 | tmp_list = pizza->children; | |
942 | while (tmp_list) | |
943 | { | |
944 | GtkPizzaChild *child = tmp_list->data; | |
945 | tmp_list = tmp_list->next; | |
946 | ||
947 | gtk_pizza_position_child (pizza, child); | |
948 | } | |
949 | } | |
950 | ||
951 | /* This function is used to find events to process while scrolling */ | |
952 | static Bool | |
953 | gtk_pizza_expose_predicate (Display *display, | |
954 | XEvent *xevent, | |
955 | XPointer arg) | |
956 | { | |
957 | if ((xevent->type == Expose) || | |
958 | ((xevent->xany.window == *(Window *)arg) && | |
959 | (xevent->type == ConfigureNotify))) | |
960 | return True; | |
961 | else | |
962 | return False; | |
963 | } | |
964 | ||
965 | /* This is the main routine to do the scrolling. Scrolling is | |
966 | * done by "Guffaw" scrolling, as in the Mozilla XFE, with | |
967 | * a few modifications. | |
968 | * | |
969 | * The main improvement is that we keep track of whether we | |
970 | * are obscured or not. If not, we ignore the generated expose | |
971 | * events and instead do the exposes ourself, without having | |
972 | * to wait for a roundtrip to the server. This also provides | |
973 | * a limited form of expose-event compression, since we do | |
974 | * the affected area as one big chunk. | |
975 | */ | |
976 | ||
977 | void | |
978 | gtk_pizza_scroll (GtkPizza *pizza, gint dx, gint dy) | |
979 | { | |
980 | GtkWidget *widget; | |
981 | XEvent xevent; | |
982 | XID win; | |
983 | ||
984 | gint x,y,w,h,border; | |
985 | ||
986 | widget = GTK_WIDGET (pizza); | |
987 | ||
988 | pizza->xoffset += dx; | |
989 | pizza->yoffset += dy; | |
990 | ||
991 | if (!GTK_WIDGET_MAPPED (pizza)) | |
992 | { | |
993 | gtk_pizza_position_children (pizza); | |
994 | return; | |
995 | } | |
996 | ||
997 | gtk_pizza_adjust_allocations (pizza, -dx, -dy); | |
998 | ||
999 | if (pizza->shadow_type == GTK_MYSHADOW_NONE) | |
1000 | border = 0; | |
1001 | else | |
1002 | if (pizza->shadow_type == GTK_MYSHADOW_THIN) | |
1003 | border = 1; | |
1004 | else | |
1005 | border = 2; | |
1006 | ||
1007 | x = 0; | |
1008 | y = 0; | |
1009 | w = widget->allocation.width - 2*border; | |
1010 | h = widget->allocation.height - 2*border; | |
1011 | ||
1012 | if (dx > 0) | |
1013 | { | |
1014 | if (gravity_works) | |
1015 | { | |
1016 | gdk_window_resize (pizza->bin_window, | |
1017 | w + dx, | |
1018 | h); | |
1019 | gdk_window_move (pizza->bin_window, x-dx, y); | |
1020 | gdk_window_move_resize (pizza->bin_window, x, y, w, h ); | |
1021 | } | |
1022 | else | |
1023 | { | |
1024 | /* FIXME */ | |
1025 | } | |
1026 | } | |
1027 | else if (dx < 0) | |
1028 | { | |
1029 | if (gravity_works) | |
1030 | { | |
1031 | gdk_window_move_resize (pizza->bin_window, | |
1032 | x + dx, | |
1033 | y, | |
1034 | w - dx, | |
1035 | h); | |
1036 | gdk_window_move (pizza->bin_window, x, y); | |
1037 | gdk_window_resize (pizza->bin_window, w, h ); | |
1038 | } | |
1039 | else | |
1040 | { | |
1041 | /* FIXME */ | |
1042 | } | |
1043 | } | |
1044 | ||
1045 | if (dy > 0) | |
1046 | { | |
1047 | if (gravity_works) | |
1048 | { | |
1049 | gdk_window_resize (pizza->bin_window, w, h + dy); | |
1050 | gdk_window_move (pizza->bin_window, x, y-dy); | |
1051 | gdk_window_move_resize (pizza->bin_window, | |
1052 | x, y, w, h ); | |
1053 | } | |
1054 | else | |
1055 | { | |
1056 | /* FIXME */ | |
1057 | } | |
1058 | } | |
1059 | else if (dy < 0) | |
1060 | { | |
1061 | if (gravity_works) | |
1062 | { | |
1063 | gdk_window_move_resize (pizza->bin_window, | |
1064 | x, y+dy, w, h - dy ); | |
1065 | gdk_window_move (pizza->bin_window, x, y); | |
1066 | gdk_window_resize (pizza->bin_window, w, h ); | |
1067 | } | |
1068 | else | |
1069 | { | |
1070 | /* FIXME */ | |
1071 | } | |
1072 | } | |
1073 | ||
1074 | gtk_pizza_position_children (pizza); | |
1075 | ||
1076 | gdk_flush(); | |
1077 | ||
1078 | win = GDK_WINDOW_XWINDOW (pizza->bin_window); | |
1079 | while (XCheckIfEvent(GDK_WINDOW_XDISPLAY (pizza->bin_window), | |
1080 | &xevent, | |
1081 | gtk_pizza_expose_predicate, | |
1082 | (XPointer)&win)) | |
1083 | { | |
1084 | GdkEvent event; | |
1085 | GtkWidget *event_widget; | |
1086 | ||
1087 | if ((xevent.xany.window == GDK_WINDOW_XWINDOW (pizza->bin_window)) ) | |
1088 | gtk_pizza_filter (&xevent, &event, pizza); | |
1089 | ||
1090 | if (xevent.type == Expose) | |
1091 | { | |
1092 | event.expose.window = gdk_window_lookup (xevent.xany.window); | |
1093 | gdk_window_get_user_data (event.expose.window, | |
1094 | (gpointer *)&event_widget); | |
1095 | ||
1096 | if (event_widget) | |
1097 | { | |
1098 | event.expose.type = GDK_EXPOSE; | |
1099 | event.expose.area.x = xevent.xexpose.x; | |
1100 | event.expose.area.y = xevent.xexpose.y; | |
1101 | event.expose.area.width = xevent.xexpose.width; | |
1102 | event.expose.area.height = xevent.xexpose.height; | |
1103 | event.expose.count = xevent.xexpose.count; | |
1104 | ||
1105 | gdk_window_ref (event.expose.window); | |
1106 | gtk_widget_event (event_widget, &event); | |
1107 | gdk_window_unref (event.expose.window); | |
1108 | } | |
1109 | } | |
1110 | } | |
1111 | } | |
1112 | ||
1113 | ||
1114 | /* The main event filter. Actually, we probably don't really need | |
1115 | * to install this as a filter at all, since we are calling it | |
1116 | * directly above in the expose-handling hack. But in case scrollbars | |
1117 | * are fixed up in some manner... | |
1118 | * | |
1119 | * This routine identifies expose events that are generated when | |
1120 | * we've temporarily moved the bin_window_origin, and translates | |
1121 | * them or discards them, depending on whether we are obscured | |
1122 | * or not. | |
1123 | */ | |
1124 | static GdkFilterReturn | |
1125 | gtk_pizza_filter (GdkXEvent *gdk_xevent, | |
1126 | GdkEvent *event, | |
1127 | gpointer data) | |
1128 | { | |
1129 | XEvent *xevent; | |
1130 | GtkPizza *pizza; | |
1131 | ||
1132 | xevent = (XEvent *)gdk_xevent; | |
1133 | ||
1134 | pizza = GTK_PIZZA (data); | |
1135 | ||
1136 | if (!pizza->use_filter) | |
1137 | return GDK_FILTER_CONTINUE; | |
1138 | ||
1139 | switch (xevent->type) | |
1140 | { | |
1141 | case Expose: | |
1142 | if (xevent->xexpose.serial == pizza->configure_serial) | |
1143 | { | |
1144 | xevent->xexpose.x += pizza->scroll_x; | |
1145 | xevent->xexpose.y += pizza->scroll_y; | |
1146 | } | |
1147 | break; | |
1148 | ||
1149 | case ConfigureNotify: | |
1150 | { | |
1151 | pizza->configure_serial = xevent->xconfigure.serial; | |
1152 | pizza->scroll_x = xevent->xconfigure.x; | |
1153 | pizza->scroll_y = xevent->xconfigure.y; | |
1154 | } | |
1155 | break; | |
1156 | } | |
1157 | ||
1158 | return GDK_FILTER_CONTINUE; | |
1159 | } | |
1160 | ||
1161 | /* Although GDK does have a GDK_VISIBILITY_NOTIFY event, | |
1162 | * there is no corresponding event in GTK, so we have | |
1163 | * to get the events from a filter | |
1164 | */ | |
1165 | static GdkFilterReturn | |
1166 | gtk_pizza_main_filter (GdkXEvent *gdk_xevent, | |
1167 | GdkEvent *event, | |
1168 | gpointer data) | |
1169 | { | |
1170 | XEvent *xevent; | |
1171 | GtkPizza *pizza; | |
1172 | ||
1173 | xevent = (XEvent *)gdk_xevent; | |
1174 | pizza = GTK_PIZZA (data); | |
1175 | ||
1176 | if (!pizza->use_filter) | |
1177 | return GDK_FILTER_CONTINUE; | |
1178 | ||
1179 | if (xevent->type == VisibilityNotify) | |
1180 | { | |
1181 | switch (xevent->xvisibility.state) | |
1182 | { | |
1183 | case VisibilityFullyObscured: | |
1184 | pizza->visibility = GDK_VISIBILITY_FULLY_OBSCURED; | |
1185 | break; | |
1186 | ||
1187 | case VisibilityPartiallyObscured: | |
1188 | pizza->visibility = GDK_VISIBILITY_PARTIAL; | |
1189 | break; | |
1190 | ||
1191 | case VisibilityUnobscured: | |
1192 | pizza->visibility = GDK_VISIBILITY_UNOBSCURED; | |
1193 | break; | |
1194 | } | |
1195 | ||
1196 | return GDK_FILTER_REMOVE; | |
1197 | } | |
1198 | ||
1199 | return GDK_FILTER_CONTINUE; | |
1200 | } | |
1201 | ||
1202 | #ifdef __cplusplus | |
1203 | } | |
1204 | #endif /* __cplusplus */ |