1 /* ///////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/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
8 // Copyright: (c) 1998 Robert Roebling
9 // Licence: wxWidgets licence
10 /////////////////////////////////////////////////////////////////////////// */
13 #define XCheckIfEvent XCHECKIFEVENT
16 #include "wx/platform.h"
17 #include "wx/gtk/win_gtk.h"
21 #endif /* __cplusplus */
23 typedef struct _GtkPizzaAdjData GtkPizzaAdjData
;
25 struct _GtkPizzaAdjData
31 static void gtk_pizza_class_init (GtkPizzaClass
*klass
);
32 static void gtk_pizza_init (GtkPizza
*pizza
);
34 static void gtk_pizza_realize (GtkWidget
*widget
);
35 static void gtk_pizza_unrealize (GtkWidget
*widget
);
37 static void gtk_pizza_map (GtkWidget
*widget
);
39 static void gtk_pizza_size_request (GtkWidget
*widget
,
40 GtkRequisition
*requisition
);
41 static void gtk_pizza_size_allocate (GtkWidget
*widget
,
42 GtkAllocation
*allocation
);
43 static gint
gtk_pizza_expose (GtkWidget
*widget
,
44 GdkEventExpose
*event
);
45 static void gtk_pizza_style_set (GtkWidget
*widget
,
46 GtkStyle
*previous_style
);
47 static void gtk_pizza_add (GtkContainer
*container
,
49 static void gtk_pizza_remove (GtkContainer
*container
,
51 static void gtk_pizza_forall (GtkContainer
*container
,
52 gboolean include_internals
,
54 gpointer callback_data
);
56 static void gtk_pizza_allocate_child (GtkPizza
*pizza
,
57 GtkPizzaChild
*child
);
58 static void gtk_pizza_adjust_allocations_recurse (GtkWidget
*widget
,
61 static GtkType
gtk_pizza_child_type (GtkContainer
*container
);
63 static void gtk_pizza_scroll_set_adjustments (GtkPizza
*pizza
,
68 GtkContainerClass
*pizza_parent_class
= NULL
;
73 static GtkType pizza_type
= 0;
77 static const GTypeInfo pizza_info
=
79 sizeof (GtkPizzaClass
),
81 NULL
, /* base_finalize */
82 (GClassInitFunc
) gtk_pizza_class_init
,
83 NULL
, /* class_finalize */
84 NULL
, /* class_data */
87 (GInstanceInitFunc
) gtk_pizza_init
,
89 pizza_type
= g_type_register_static (GTK_TYPE_CONTAINER
, "GtkPizza", &pizza_info
, (GTypeFlags
)0);
95 /* Marshaller needed for set_scroll_adjustments signal,
96 generated with GLib-2.4.6 glib-genmarshal */
97 #define g_marshal_value_peek_object(v) g_value_get_object (v)
99 g_cclosure_user_marshal_VOID__OBJECT_OBJECT (GClosure
*closure
,
100 GValue
*return_value
,
101 guint n_param_values
,
102 const GValue
*param_values
,
103 gpointer invocation_hint
,
104 gpointer marshal_data
)
106 typedef void (*GMarshalFunc_VOID__OBJECT_OBJECT
) (gpointer data1
,
110 register GMarshalFunc_VOID__OBJECT_OBJECT callback
;
111 register GCClosure
*cc
= (GCClosure
*) closure
;
112 register gpointer data1
, data2
;
114 g_return_if_fail (n_param_values
== 3);
116 if (G_CCLOSURE_SWAP_DATA (closure
))
118 data1
= closure
->data
;
119 data2
= g_value_peek_pointer (param_values
+ 0);
123 data1
= g_value_peek_pointer (param_values
+ 0);
124 data2
= closure
->data
;
126 callback
= (GMarshalFunc_VOID__OBJECT_OBJECT
) (marshal_data
? marshal_data
: cc
->callback
);
129 g_marshal_value_peek_object (param_values
+ 1),
130 g_marshal_value_peek_object (param_values
+ 2),
135 gtk_pizza_class_init (GtkPizzaClass
*klass
)
137 GtkObjectClass
*object_class
;
138 GtkWidgetClass
*widget_class
;
139 GtkContainerClass
*container_class
;
141 object_class
= (GtkObjectClass
*) klass
;
142 widget_class
= (GtkWidgetClass
*) klass
;
143 container_class
= (GtkContainerClass
*) klass
;
144 pizza_parent_class
= gtk_type_class (GTK_TYPE_CONTAINER
);
146 widget_class
->map
= gtk_pizza_map
;
147 widget_class
->realize
= gtk_pizza_realize
;
148 widget_class
->unrealize
= gtk_pizza_unrealize
;
149 widget_class
->size_request
= gtk_pizza_size_request
;
150 widget_class
->size_allocate
= gtk_pizza_size_allocate
;
151 widget_class
->expose_event
= gtk_pizza_expose
;
152 widget_class
->style_set
= gtk_pizza_style_set
;
154 container_class
->add
= gtk_pizza_add
;
155 container_class
->remove
= gtk_pizza_remove
;
156 container_class
->forall
= gtk_pizza_forall
;
158 container_class
->child_type
= gtk_pizza_child_type
;
160 klass
->set_scroll_adjustments
= gtk_pizza_scroll_set_adjustments
;
162 widget_class
->set_scroll_adjustments_signal
=
164 "set_scroll_adjustments",
165 G_TYPE_FROM_CLASS(object_class
),
167 G_STRUCT_OFFSET(GtkPizzaClass
, set_scroll_adjustments
),
170 g_cclosure_user_marshal_VOID__OBJECT_OBJECT
,
174 GTK_TYPE_ADJUSTMENT
);
178 gtk_pizza_child_type (GtkContainer
*container
)
180 return GTK_TYPE_WIDGET
;
184 gtk_pizza_init (GtkPizza
*pizza
)
186 GTK_WIDGET_UNSET_FLAGS (pizza
, GTK_NO_WINDOW
);
188 pizza
->shadow_type
= GTK_MYSHADOW_NONE
;
190 pizza
->children
= NULL
;
195 pizza
->bin_window
= NULL
;
203 pizza
->external_expose
= FALSE
;
211 pizza
= g_object_new (gtk_pizza_get_type (), NULL
);
213 return GTK_WIDGET (pizza
);
217 gtk_pizza_scroll_set_adjustments (GtkPizza
*pizza
,
221 /* We handle scrolling in the wxScrolledWindow, not here. */
225 gtk_pizza_set_shadow_type (GtkPizza
*pizza
,
226 GtkMyShadowType type
)
228 g_return_if_fail (pizza
!= NULL
);
229 g_return_if_fail (GTK_IS_PIZZA (pizza
));
231 if (pizza
->shadow_type
!= type
)
233 pizza
->shadow_type
= type
;
235 if (GTK_WIDGET_VISIBLE (pizza
))
237 gtk_widget_size_allocate (GTK_WIDGET (pizza
), &(GTK_WIDGET (pizza
)->allocation
));
238 gtk_widget_queue_draw (GTK_WIDGET (pizza
));
244 gtk_pizza_set_external (GtkPizza
*pizza
,
247 g_return_if_fail (pizza
!= NULL
);
248 g_return_if_fail (GTK_IS_PIZZA (pizza
));
250 pizza
->external_expose
= expose
;
254 gtk_pizza_put (GtkPizza
*pizza
,
261 GtkPizzaChild
*child_info
;
263 g_return_if_fail (pizza
!= NULL
);
264 g_return_if_fail (GTK_IS_PIZZA (pizza
));
265 g_return_if_fail (widget
!= NULL
);
267 child_info
= g_new (GtkPizzaChild
, 1);
269 child_info
->widget
= widget
;
272 child_info
->width
= width
;
273 child_info
->height
= height
;
275 pizza
->children
= g_list_append (pizza
->children
, child_info
);
277 if (GTK_WIDGET_REALIZED (pizza
))
278 gtk_widget_set_parent_window (widget
, pizza
->bin_window
);
280 gtk_widget_set_parent (widget
, GTK_WIDGET (pizza
));
282 gtk_widget_set_size_request (widget
, width
, height
);
286 gtk_pizza_set_size (GtkPizza
*pizza
,
293 GtkPizzaChild
*child
;
296 g_return_if_fail (pizza
!= NULL
);
297 g_return_if_fail (GTK_IS_PIZZA (pizza
));
298 g_return_if_fail (widget
!= NULL
);
300 #ifndef WX_WARN_ILLEGAL_SETSIZE
301 /* this really shouldn't happen -- but it does, a lot, right now and we
302 can't pass negative values to gtk_widget_set_size_request() without getting
303 a warning printed out, so filter them out here */
310 children
= pizza
->children
;
313 child
= children
->data
;
314 children
= children
->next
;
316 if (child
->widget
== widget
)
318 if ((child
->x
== x
) &&
320 (child
->width
== width
) &&
321 (child
->height
== height
)) return;
325 child
->width
= width
;
326 child
->height
= height
;
328 gtk_widget_set_size_request (widget
, width
, height
);
336 gtk_pizza_map (GtkWidget
*widget
)
339 GtkPizzaChild
*child
;
342 g_return_if_fail (widget
!= NULL
);
343 g_return_if_fail (GTK_IS_PIZZA (widget
));
345 GTK_WIDGET_SET_FLAGS (widget
, GTK_MAPPED
);
346 pizza
= GTK_PIZZA (widget
);
348 children
= pizza
->children
;
351 child
= children
->data
;
352 children
= children
->next
;
354 if ( GTK_WIDGET_VISIBLE (child
->widget
) &&
355 !GTK_WIDGET_MAPPED (child
->widget
) )
357 gtk_widget_map (child
->widget
);
361 gdk_window_show (widget
->window
);
362 gdk_window_show (pizza
->bin_window
);
366 gtk_pizza_realize (GtkWidget
*widget
)
369 GdkWindowAttr attributes
;
370 gint attributes_mask
;
371 GtkPizzaChild
*child
;
374 g_return_if_fail (widget
!= NULL
);
375 g_return_if_fail (GTK_IS_PIZZA (widget
));
377 pizza
= GTK_PIZZA (widget
);
378 GTK_WIDGET_SET_FLAGS (widget
, GTK_REALIZED
);
380 attributes
.window_type
= GDK_WINDOW_CHILD
;
382 attributes
.x
= widget
->allocation
.x
;
383 attributes
.y
= widget
->allocation
.y
;
384 attributes
.width
= widget
->allocation
.width
;
385 attributes
.height
= widget
->allocation
.height
;
387 #ifndef __WXUNIVERSAL__
388 if (pizza
->shadow_type
== GTK_MYSHADOW_NONE
)
390 /* no border, no changes to sizes */
392 else if (pizza
->shadow_type
== GTK_MYSHADOW_THIN
)
394 /* GTK_MYSHADOW_THIN == wxSIMPLE_BORDER */
397 attributes
.width
-= 2;
398 attributes
.height
-= 2;
402 /* GTK_MYSHADOW_IN == wxSUNKEN_BORDER */
403 /* GTK_MYSHADOW_OUT == wxRAISED_BORDER */
406 attributes
.width
-= 4;
407 attributes
.height
-= 4;
409 #endif /* __WXUNIVERSAL__ */
412 if (attributes
.width
< 2) attributes
.width
= 2;
413 if (attributes
.height
< 2) attributes
.height
= 2;
415 attributes
.wclass
= GDK_INPUT_OUTPUT
;
416 attributes
.visual
= gtk_widget_get_visual (widget
);
417 attributes
.colormap
= gtk_widget_get_colormap (widget
);
418 attributes
.event_mask
= GDK_VISIBILITY_NOTIFY_MASK
;
419 attributes_mask
= GDK_WA_X
| GDK_WA_Y
| GDK_WA_VISUAL
| GDK_WA_COLORMAP
;
421 widget
->window
= gdk_window_new(gtk_widget_get_parent_window (widget
),
422 &attributes
, attributes_mask
);
423 gdk_window_set_user_data (widget
->window
, widget
);
428 attributes
.event_mask
= gtk_widget_get_events (widget
);
429 attributes
.event_mask
|= GDK_EXPOSURE_MASK
|
431 GDK_POINTER_MOTION_MASK
|
432 GDK_POINTER_MOTION_HINT_MASK
|
433 GDK_BUTTON_MOTION_MASK
|
434 GDK_BUTTON1_MOTION_MASK
|
435 GDK_BUTTON2_MOTION_MASK
|
436 GDK_BUTTON3_MOTION_MASK
|
437 GDK_BUTTON_PRESS_MASK
|
438 GDK_BUTTON_RELEASE_MASK
|
440 GDK_KEY_RELEASE_MASK
|
441 GDK_ENTER_NOTIFY_MASK
|
442 GDK_LEAVE_NOTIFY_MASK
|
443 GDK_FOCUS_CHANGE_MASK
;
445 pizza
->bin_window
= gdk_window_new(widget
->window
,
446 &attributes
, attributes_mask
);
447 gdk_window_set_user_data (pizza
->bin_window
, widget
);
449 widget
->style
= gtk_style_attach (widget
->style
, widget
->window
);
450 gtk_style_set_background (widget
->style
, widget
->window
, GTK_STATE_NORMAL
);
451 gtk_style_set_background (widget
->style
, pizza
->bin_window
, GTK_STATE_NORMAL
);
454 gdk_window_set_back_pixmap( widget->window, NULL, FALSE );
455 gdk_window_set_back_pixmap( pizza->bin_window, NULL, FALSE );
458 /* cannot be done before realisation */
459 children
= pizza
->children
;
462 child
= children
->data
;
463 children
= children
->next
;
465 gtk_widget_set_parent_window (child
->widget
, pizza
->bin_window
);
470 gtk_pizza_unrealize (GtkWidget
*widget
)
474 g_return_if_fail (widget
!= NULL
);
475 g_return_if_fail (GTK_IS_PIZZA (widget
));
477 pizza
= GTK_PIZZA (widget
);
479 gdk_window_set_user_data (pizza
->bin_window
, NULL
);
480 gdk_window_destroy (pizza
->bin_window
);
481 pizza
->bin_window
= NULL
;
483 if (GTK_WIDGET_CLASS (pizza_parent_class
)->unrealize
)
484 (* GTK_WIDGET_CLASS (pizza_parent_class
)->unrealize
) (widget
);
488 gtk_pizza_size_request (GtkWidget
*widget
,
489 GtkRequisition
*requisition
)
492 GtkPizzaChild
*child
;
494 GtkRequisition child_requisition
;
496 g_return_if_fail (widget
!= NULL
);
497 g_return_if_fail (GTK_IS_PIZZA (widget
));
498 g_return_if_fail (requisition
!= NULL
);
500 pizza
= GTK_PIZZA (widget
);
502 children
= pizza
->children
;
505 child
= children
->data
;
506 children
= children
->next
;
508 if (GTK_WIDGET_VISIBLE (child
->widget
))
510 gtk_widget_size_request (child
->widget
, &child_requisition
);
514 /* request very little, I'm not sure if requesting nothing
515 will always have positive effects on stability... */
516 requisition
->width
= 2;
517 requisition
->height
= 2;
521 gtk_pizza_size_allocate (GtkWidget
*widget
,
522 GtkAllocation
*allocation
)
527 GtkPizzaChild
*child
;
530 g_return_if_fail (widget
!= NULL
);
531 g_return_if_fail (GTK_IS_PIZZA(widget
));
532 g_return_if_fail (allocation
!= NULL
);
534 pizza
= GTK_PIZZA (widget
);
536 widget
->allocation
= *allocation
;
538 if (pizza
->shadow_type
== GTK_MYSHADOW_NONE
)
541 if (pizza
->shadow_type
== GTK_MYSHADOW_THIN
)
546 x
= allocation
->x
+ border
;
547 y
= allocation
->y
+ border
;
548 w
= allocation
->width
- border
*2;
549 h
= allocation
->height
- border
*2;
551 if (GTK_WIDGET_REALIZED (widget
))
553 gdk_window_move_resize( widget
->window
, x
, y
, w
, h
);
554 gdk_window_move_resize( pizza
->bin_window
, 0, 0, w
, h
);
557 children
= pizza
->children
;
560 child
= children
->data
;
561 children
= children
->next
;
563 gtk_pizza_allocate_child (pizza
, child
);
568 gtk_pizza_expose (GtkWidget
*widget
,
569 GdkEventExpose
*event
)
573 g_return_val_if_fail (widget
!= NULL
, FALSE
);
574 g_return_val_if_fail (GTK_IS_PIZZA (widget
), FALSE
);
575 g_return_val_if_fail (event
!= NULL
, FALSE
);
577 pizza
= GTK_PIZZA (widget
);
579 if (event
->window
!= pizza
->bin_window
)
582 /* We handle all expose events in window.cpp now. */
583 if (pizza
->external_expose
)
586 (* GTK_WIDGET_CLASS (pizza_parent_class
)->expose_event
) (widget
, event
);
592 gtk_pizza_style_set(GtkWidget
*widget
, GtkStyle
*previous_style
)
594 if (GTK_WIDGET_REALIZED(widget
))
596 gtk_style_set_background(widget
->style
, widget
->window
, GTK_STATE_NORMAL
);
597 gtk_style_set_background(widget
->style
, GTK_PIZZA(widget
)->bin_window
, GTK_STATE_NORMAL
);
600 (* GTK_WIDGET_CLASS (pizza_parent_class
)->style_set
) (widget
, previous_style
);
604 gtk_pizza_add (GtkContainer
*container
,
607 g_return_if_fail (container
!= NULL
);
608 g_return_if_fail (GTK_IS_PIZZA (container
));
609 g_return_if_fail (widget
!= NULL
);
611 gtk_pizza_put (GTK_PIZZA (container
), widget
, 0, 0, 20, 20 );
615 gtk_pizza_remove (GtkContainer
*container
,
619 GtkPizzaChild
*child
;
622 g_return_if_fail (container
!= NULL
);
623 g_return_if_fail (GTK_IS_PIZZA (container
));
624 g_return_if_fail (widget
!= NULL
);
626 pizza
= GTK_PIZZA (container
);
628 children
= pizza
->children
;
631 child
= children
->data
;
633 if (child
->widget
== widget
)
635 gtk_widget_unparent (widget
);
637 /* security checks */
638 g_return_if_fail (GTK_IS_WIDGET (widget
));
640 pizza
->children
= g_list_remove_link (pizza
->children
, children
);
641 g_list_free (children
);
644 /* security checks */
645 g_return_if_fail (GTK_IS_WIDGET (widget
));
650 children
= children
->next
;
655 gtk_pizza_forall (GtkContainer
*container
,
656 gboolean include_internals
,
657 GtkCallback callback
,
658 gpointer callback_data
)
661 GtkPizzaChild
*child
;
664 g_return_if_fail (container
!= NULL
);
665 g_return_if_fail (GTK_IS_PIZZA (container
));
666 g_return_if_fail (callback
!= (GtkCallback
)NULL
);
668 pizza
= GTK_PIZZA (container
);
670 children
= pizza
->children
;
673 child
= children
->data
;
674 children
= children
->next
;
676 (* callback
) (child
->widget
, callback_data
);
681 gtk_pizza_allocate_child (GtkPizza
*pizza
,
682 GtkPizzaChild
*child
)
684 GtkAllocation allocation
;
685 GtkRequisition requisition
;
687 allocation
.x
= child
->x
- pizza
->xoffset
;
688 allocation
.y
= child
->y
- pizza
->yoffset
;
689 gtk_widget_get_child_requisition (child
->widget
, &requisition
);
690 allocation
.width
= requisition
.width
;
691 allocation
.height
= requisition
.height
;
693 gtk_widget_size_allocate (child
->widget
, &allocation
);
697 gtk_pizza_adjust_allocations_recurse (GtkWidget
*widget
,
700 GtkPizzaAdjData
*data
= cb_data
;
702 widget
->allocation
.x
+= data
->dx
;
703 widget
->allocation
.y
+= data
->dy
;
705 if (GTK_WIDGET_NO_WINDOW (widget
) && GTK_IS_CONTAINER (widget
))
707 gtk_container_forall (GTK_CONTAINER (widget
),
708 gtk_pizza_adjust_allocations_recurse
,
714 gtk_pizza_adjust_allocations (GtkPizza
*pizza
,
719 GtkPizzaAdjData data
;
724 tmp_list
= pizza
->children
;
727 GtkPizzaChild
*child
= tmp_list
->data
;
728 tmp_list
= tmp_list
->next
;
730 child
->widget
->allocation
.x
+= dx
;
731 child
->widget
->allocation
.y
+= dy
;
733 if (GTK_WIDGET_NO_WINDOW (child
->widget
) &&
734 GTK_IS_CONTAINER (child
->widget
))
736 gtk_container_forall (GTK_CONTAINER (child
->widget
),
737 gtk_pizza_adjust_allocations_recurse
,
744 gtk_pizza_scroll (GtkPizza
*pizza
, gint dx
, gint dy
)
746 pizza
->xoffset
+= dx
;
747 pizza
->yoffset
+= dy
;
749 gtk_pizza_adjust_allocations (pizza
, -dx
, -dy
);
751 if (pizza
->bin_window
)
752 gdk_window_scroll( pizza
->bin_window
, -dx
, -dy
);
757 #endif /* __cplusplus */