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"
18 #include "gtk/gtksignal.h"
19 #include "gtk/gtkprivate.h"
24 #endif /* __cplusplus */
26 typedef struct _GtkPizzaAdjData GtkPizzaAdjData
;
28 struct _GtkPizzaAdjData
34 static void gtk_pizza_class_init (GtkPizzaClass
*klass
);
35 static void gtk_pizza_init (GtkPizza
*pizza
);
37 static void gtk_pizza_realize (GtkWidget
*widget
);
38 static void gtk_pizza_unrealize (GtkWidget
*widget
);
40 static void gtk_pizza_map (GtkWidget
*widget
);
42 static void gtk_pizza_size_request (GtkWidget
*widget
,
43 GtkRequisition
*requisition
);
44 static void gtk_pizza_size_allocate (GtkWidget
*widget
,
45 GtkAllocation
*allocation
);
46 static gint
gtk_pizza_expose (GtkWidget
*widget
,
47 GdkEventExpose
*event
);
48 static void gtk_pizza_style_set (GtkWidget
*widget
,
49 GtkStyle
*previous_style
);
50 static void gtk_pizza_add (GtkContainer
*container
,
52 static void gtk_pizza_remove (GtkContainer
*container
,
54 static void gtk_pizza_forall (GtkContainer
*container
,
55 gboolean include_internals
,
57 gpointer callback_data
);
59 static void gtk_pizza_allocate_child (GtkPizza
*pizza
,
60 GtkPizzaChild
*child
);
61 static void gtk_pizza_adjust_allocations_recurse (GtkWidget
*widget
,
64 static GtkType
gtk_pizza_child_type (GtkContainer
*container
);
66 static void gtk_pizza_scroll_set_adjustments (GtkPizza
*pizza
,
71 GtkContainerClass
*pizza_parent_class
= NULL
;
76 static GtkType pizza_type
= 0;
80 static const GTypeInfo pizza_info
=
82 sizeof (GtkPizzaClass
),
84 NULL
, /* base_finalize */
85 (GClassInitFunc
) gtk_pizza_class_init
,
86 NULL
, /* class_finalize */
87 NULL
, /* class_data */
90 (GInstanceInitFunc
) gtk_pizza_init
,
92 pizza_type
= g_type_register_static (GTK_TYPE_CONTAINER
, "GtkPizza", &pizza_info
, (GTypeFlags
)0);
98 /* Marshaller needed for set_scroll_adjustments signal,
99 generated with GLib-2.4.6 glib-genmarshal */
100 #define g_marshal_value_peek_object(v) g_value_get_object (v)
102 g_cclosure_user_marshal_VOID__OBJECT_OBJECT (GClosure
*closure
,
103 GValue
*return_value
,
104 guint n_param_values
,
105 const GValue
*param_values
,
106 gpointer invocation_hint
,
107 gpointer marshal_data
)
109 typedef void (*GMarshalFunc_VOID__OBJECT_OBJECT
) (gpointer data1
,
113 register GMarshalFunc_VOID__OBJECT_OBJECT callback
;
114 register GCClosure
*cc
= (GCClosure
*) closure
;
115 register gpointer data1
, data2
;
117 g_return_if_fail (n_param_values
== 3);
119 if (G_CCLOSURE_SWAP_DATA (closure
))
121 data1
= closure
->data
;
122 data2
= g_value_peek_pointer (param_values
+ 0);
126 data1
= g_value_peek_pointer (param_values
+ 0);
127 data2
= closure
->data
;
129 callback
= (GMarshalFunc_VOID__OBJECT_OBJECT
) (marshal_data
? marshal_data
: cc
->callback
);
132 g_marshal_value_peek_object (param_values
+ 1),
133 g_marshal_value_peek_object (param_values
+ 2),
138 gtk_pizza_class_init (GtkPizzaClass
*klass
)
140 GtkObjectClass
*object_class
;
141 GtkWidgetClass
*widget_class
;
142 GtkContainerClass
*container_class
;
144 object_class
= (GtkObjectClass
*) klass
;
145 widget_class
= (GtkWidgetClass
*) klass
;
146 container_class
= (GtkContainerClass
*) klass
;
147 pizza_parent_class
= gtk_type_class (GTK_TYPE_CONTAINER
);
149 widget_class
->map
= gtk_pizza_map
;
150 widget_class
->realize
= gtk_pizza_realize
;
151 widget_class
->unrealize
= gtk_pizza_unrealize
;
152 widget_class
->size_request
= gtk_pizza_size_request
;
153 widget_class
->size_allocate
= gtk_pizza_size_allocate
;
154 widget_class
->expose_event
= gtk_pizza_expose
;
155 widget_class
->style_set
= gtk_pizza_style_set
;
157 container_class
->add
= gtk_pizza_add
;
158 container_class
->remove
= gtk_pizza_remove
;
159 container_class
->forall
= gtk_pizza_forall
;
161 container_class
->child_type
= gtk_pizza_child_type
;
163 klass
->set_scroll_adjustments
= gtk_pizza_scroll_set_adjustments
;
165 widget_class
->set_scroll_adjustments_signal
=
167 "set_scroll_adjustments",
168 G_TYPE_FROM_CLASS(object_class
),
170 G_STRUCT_OFFSET(GtkPizzaClass
, set_scroll_adjustments
),
173 g_cclosure_user_marshal_VOID__OBJECT_OBJECT
,
177 GTK_TYPE_ADJUSTMENT
);
181 gtk_pizza_child_type (GtkContainer
*container
)
183 return GTK_TYPE_WIDGET
;
187 gtk_pizza_init (GtkPizza
*pizza
)
189 GTK_WIDGET_UNSET_FLAGS (pizza
, GTK_NO_WINDOW
);
191 pizza
->shadow_type
= GTK_MYSHADOW_NONE
;
193 pizza
->children
= NULL
;
198 pizza
->bin_window
= NULL
;
206 pizza
->external_expose
= FALSE
;
214 pizza
= gtk_type_new (gtk_pizza_get_type ());
216 return GTK_WIDGET (pizza
);
220 gtk_pizza_scroll_set_adjustments (GtkPizza
*pizza
,
224 /* We handle scrolling in the wxScrolledWindow, not here. */
228 gtk_pizza_set_shadow_type (GtkPizza
*pizza
,
229 GtkMyShadowType type
)
231 g_return_if_fail (pizza
!= NULL
);
232 g_return_if_fail (GTK_IS_PIZZA (pizza
));
234 if ((GtkMyShadowType
) pizza
->shadow_type
!= type
)
236 pizza
->shadow_type
= type
;
238 if (GTK_WIDGET_VISIBLE (pizza
))
240 gtk_widget_size_allocate (GTK_WIDGET (pizza
), &(GTK_WIDGET (pizza
)->allocation
));
241 gtk_widget_queue_draw (GTK_WIDGET (pizza
));
247 gtk_pizza_set_external (GtkPizza
*pizza
,
250 g_return_if_fail (pizza
!= NULL
);
251 g_return_if_fail (GTK_IS_PIZZA (pizza
));
253 pizza
->external_expose
= expose
;
257 gtk_pizza_put (GtkPizza
*pizza
,
264 GtkPizzaChild
*child_info
;
266 g_return_if_fail (pizza
!= NULL
);
267 g_return_if_fail (GTK_IS_PIZZA (pizza
));
268 g_return_if_fail (widget
!= NULL
);
270 child_info
= g_new (GtkPizzaChild
, 1);
272 child_info
->widget
= widget
;
275 child_info
->width
= width
;
276 child_info
->height
= height
;
278 pizza
->children
= g_list_append (pizza
->children
, child_info
);
280 if (GTK_WIDGET_REALIZED (pizza
))
281 gtk_widget_set_parent_window (widget
, pizza
->bin_window
);
283 gtk_widget_set_parent (widget
, GTK_WIDGET (pizza
));
285 gtk_widget_set_size_request (widget
, width
, height
);
289 gtk_pizza_set_size (GtkPizza
*pizza
,
296 GtkPizzaChild
*child
;
299 g_return_if_fail (pizza
!= NULL
);
300 g_return_if_fail (GTK_IS_PIZZA (pizza
));
301 g_return_if_fail (widget
!= NULL
);
303 #ifndef WX_WARN_ILLEGAL_SETSIZE
304 /* this really shouldn't happen -- but it does, a lot, right now and we
305 can't pass negative values to gtk_widget_set_size_request() without getting
306 a warning printed out, so filter them out here */
313 children
= pizza
->children
;
316 child
= children
->data
;
317 children
= children
->next
;
319 if (child
->widget
== widget
)
321 if ((child
->x
== x
) &&
323 (child
->width
== width
) &&
324 (child
->height
== height
)) return;
328 child
->width
= width
;
329 child
->height
= height
;
331 gtk_widget_set_size_request (widget
, width
, height
);
333 if (GTK_WIDGET_VISIBLE (widget
) && GTK_WIDGET_VISIBLE (pizza
))
334 gtk_widget_queue_resize (widget
);
342 gtk_pizza_map (GtkWidget
*widget
)
345 GtkPizzaChild
*child
;
348 g_return_if_fail (widget
!= NULL
);
349 g_return_if_fail (GTK_IS_PIZZA (widget
));
351 GTK_WIDGET_SET_FLAGS (widget
, GTK_MAPPED
);
352 pizza
= GTK_PIZZA (widget
);
354 children
= pizza
->children
;
357 child
= children
->data
;
358 children
= children
->next
;
360 if ( GTK_WIDGET_VISIBLE (child
->widget
) &&
361 !GTK_WIDGET_MAPPED (child
->widget
) &&
364 gtk_widget_map (child
->widget
);
368 gdk_window_show (widget
->window
);
369 gdk_window_show (pizza
->bin_window
);
373 gtk_pizza_realize (GtkWidget
*widget
)
376 GdkWindowAttr attributes
;
377 gint attributes_mask
;
378 GtkPizzaChild
*child
;
381 g_return_if_fail (widget
!= NULL
);
382 g_return_if_fail (GTK_IS_PIZZA (widget
));
384 pizza
= GTK_PIZZA (widget
);
385 GTK_WIDGET_SET_FLAGS (widget
, GTK_REALIZED
);
387 attributes
.window_type
= GDK_WINDOW_CHILD
;
389 attributes
.x
= widget
->allocation
.x
;
390 attributes
.y
= widget
->allocation
.y
;
391 attributes
.width
= widget
->allocation
.width
;
392 attributes
.height
= widget
->allocation
.height
;
394 #ifndef __WXUNIVERSAL__
395 if (pizza
->shadow_type
== GTK_MYSHADOW_NONE
)
397 /* no border, no changes to sizes */
399 else if (pizza
->shadow_type
== GTK_MYSHADOW_THIN
)
401 /* GTK_MYSHADOW_THIN == wxSIMPLE_BORDER */
404 attributes
.width
-= 2;
405 attributes
.height
-= 2;
409 /* GTK_MYSHADOW_IN == wxSUNKEN_BORDER */
410 /* GTK_MYSHADOW_OUT == wxRAISED_BORDER */
413 attributes
.width
-= 4;
414 attributes
.height
-= 4;
416 #endif /* __WXUNIVERSAL__ */
419 if (attributes
.width
< 2) attributes
.width
= 2;
420 if (attributes
.height
< 2) attributes
.height
= 2;
422 attributes
.wclass
= GDK_INPUT_OUTPUT
;
423 attributes
.visual
= gtk_widget_get_visual (widget
);
424 attributes
.colormap
= gtk_widget_get_colormap (widget
);
425 attributes
.event_mask
= GDK_VISIBILITY_NOTIFY_MASK
;
426 attributes_mask
= GDK_WA_X
| GDK_WA_Y
| GDK_WA_VISUAL
| GDK_WA_COLORMAP
;
428 widget
->window
= gdk_window_new(gtk_widget_get_parent_window (widget
),
429 &attributes
, attributes_mask
);
430 gdk_window_set_user_data (widget
->window
, widget
);
435 attributes
.event_mask
= gtk_widget_get_events (widget
);
436 attributes
.event_mask
|= GDK_EXPOSURE_MASK
|
438 GDK_POINTER_MOTION_MASK
|
439 GDK_POINTER_MOTION_HINT_MASK
|
440 GDK_BUTTON_MOTION_MASK
|
441 GDK_BUTTON1_MOTION_MASK
|
442 GDK_BUTTON2_MOTION_MASK
|
443 GDK_BUTTON3_MOTION_MASK
|
444 GDK_BUTTON_PRESS_MASK
|
445 GDK_BUTTON_RELEASE_MASK
|
447 GDK_KEY_RELEASE_MASK
|
448 GDK_ENTER_NOTIFY_MASK
|
449 GDK_LEAVE_NOTIFY_MASK
|
450 GDK_FOCUS_CHANGE_MASK
;
452 pizza
->bin_window
= gdk_window_new(widget
->window
,
453 &attributes
, attributes_mask
);
454 gdk_window_set_user_data (pizza
->bin_window
, widget
);
456 widget
->style
= gtk_style_attach (widget
->style
, widget
->window
);
457 gtk_style_set_background (widget
->style
, widget
->window
, GTK_STATE_NORMAL
);
458 gtk_style_set_background (widget
->style
, pizza
->bin_window
, GTK_STATE_NORMAL
);
461 gdk_window_set_back_pixmap( widget->window, NULL, FALSE );
462 gdk_window_set_back_pixmap( pizza->bin_window, NULL, FALSE );
465 /* cannot be done before realisation */
466 children
= pizza
->children
;
469 child
= children
->data
;
470 children
= children
->next
;
472 gtk_widget_set_parent_window (child
->widget
, pizza
->bin_window
);
477 gtk_pizza_unrealize (GtkWidget
*widget
)
481 g_return_if_fail (widget
!= NULL
);
482 g_return_if_fail (GTK_IS_PIZZA (widget
));
484 pizza
= GTK_PIZZA (widget
);
486 gdk_window_set_user_data (pizza
->bin_window
, NULL
);
487 gdk_window_destroy (pizza
->bin_window
);
488 pizza
->bin_window
= NULL
;
490 if (GTK_WIDGET_CLASS (pizza_parent_class
)->unrealize
)
491 (* GTK_WIDGET_CLASS (pizza_parent_class
)->unrealize
) (widget
);
495 gtk_pizza_size_request (GtkWidget
*widget
,
496 GtkRequisition
*requisition
)
499 GtkPizzaChild
*child
;
501 GtkRequisition child_requisition
;
503 g_return_if_fail (widget
!= NULL
);
504 g_return_if_fail (GTK_IS_PIZZA (widget
));
505 g_return_if_fail (requisition
!= NULL
);
507 pizza
= GTK_PIZZA (widget
);
509 children
= pizza
->children
;
512 child
= children
->data
;
513 children
= children
->next
;
515 if (GTK_WIDGET_VISIBLE (child
->widget
))
517 gtk_widget_size_request (child
->widget
, &child_requisition
);
521 /* request very little, I'm not sure if requesting nothing
522 will always have positive effects on stability... */
523 requisition
->width
= 2;
524 requisition
->height
= 2;
528 gtk_pizza_size_allocate (GtkWidget
*widget
,
529 GtkAllocation
*allocation
)
534 GtkPizzaChild
*child
;
537 g_return_if_fail (widget
!= NULL
);
538 g_return_if_fail (GTK_IS_PIZZA(widget
));
539 g_return_if_fail (allocation
!= NULL
);
541 pizza
= GTK_PIZZA (widget
);
543 widget
->allocation
= *allocation
;
545 if (pizza
->shadow_type
== GTK_MYSHADOW_NONE
)
548 if (pizza
->shadow_type
== GTK_MYSHADOW_THIN
)
553 x
= allocation
->x
+ border
;
554 y
= allocation
->y
+ border
;
555 w
= allocation
->width
- border
*2;
556 h
= allocation
->height
- border
*2;
558 if (GTK_WIDGET_REALIZED (widget
))
560 gdk_window_move_resize( widget
->window
, x
, y
, w
, h
);
561 gdk_window_move_resize( pizza
->bin_window
, 0, 0, w
, h
);
564 children
= pizza
->children
;
567 child
= children
->data
;
568 children
= children
->next
;
570 gtk_pizza_allocate_child (pizza
, child
);
575 gtk_pizza_expose (GtkWidget
*widget
,
576 GdkEventExpose
*event
)
580 g_return_val_if_fail (widget
!= NULL
, FALSE
);
581 g_return_val_if_fail (GTK_IS_PIZZA (widget
), FALSE
);
582 g_return_val_if_fail (event
!= NULL
, FALSE
);
584 pizza
= GTK_PIZZA (widget
);
586 if (event
->window
!= pizza
->bin_window
)
589 /* We handle all expose events in window.cpp now. */
590 if (pizza
->external_expose
)
593 (* GTK_WIDGET_CLASS (pizza_parent_class
)->expose_event
) (widget
, event
);
599 gtk_pizza_style_set(GtkWidget
*widget
, GtkStyle
*previous_style
)
601 if (GTK_WIDGET_REALIZED(widget
))
603 gtk_style_set_background(widget
->style
, widget
->window
, GTK_STATE_NORMAL
);
604 gtk_style_set_background(widget
->style
, GTK_PIZZA(widget
)->bin_window
, GTK_STATE_NORMAL
);
607 (* GTK_WIDGET_CLASS (pizza_parent_class
)->style_set
) (widget
, previous_style
);
611 gtk_pizza_add (GtkContainer
*container
,
614 g_return_if_fail (container
!= NULL
);
615 g_return_if_fail (GTK_IS_PIZZA (container
));
616 g_return_if_fail (widget
!= NULL
);
618 gtk_pizza_put (GTK_PIZZA (container
), widget
, 0, 0, 20, 20 );
622 gtk_pizza_remove (GtkContainer
*container
,
626 GtkPizzaChild
*child
;
629 g_return_if_fail (container
!= NULL
);
630 g_return_if_fail (GTK_IS_PIZZA (container
));
631 g_return_if_fail (widget
!= NULL
);
633 pizza
= GTK_PIZZA (container
);
635 children
= pizza
->children
;
638 child
= children
->data
;
640 if (child
->widget
== widget
)
642 gtk_widget_unparent (widget
);
644 /* security checks */
645 g_return_if_fail (GTK_IS_WIDGET (widget
));
647 pizza
->children
= g_list_remove_link (pizza
->children
, children
);
648 g_list_free (children
);
651 /* security checks */
652 g_return_if_fail (GTK_IS_WIDGET (widget
));
657 children
= children
->next
;
662 gtk_pizza_forall (GtkContainer
*container
,
663 gboolean include_internals
,
664 GtkCallback callback
,
665 gpointer callback_data
)
668 GtkPizzaChild
*child
;
671 g_return_if_fail (container
!= NULL
);
672 g_return_if_fail (GTK_IS_PIZZA (container
));
673 g_return_if_fail (callback
!= (GtkCallback
)NULL
);
675 pizza
= GTK_PIZZA (container
);
677 children
= pizza
->children
;
680 child
= children
->data
;
681 children
= children
->next
;
683 (* callback
) (child
->widget
, callback_data
);
688 gtk_pizza_allocate_child (GtkPizza
*pizza
,
689 GtkPizzaChild
*child
)
691 GtkAllocation allocation
;
692 GtkRequisition requisition
;
694 allocation
.x
= child
->x
- pizza
->xoffset
;
695 allocation
.y
= child
->y
- pizza
->yoffset
;
696 gtk_widget_get_child_requisition (child
->widget
, &requisition
);
697 allocation
.width
= requisition
.width
;
698 allocation
.height
= requisition
.height
;
700 gtk_widget_size_allocate (child
->widget
, &allocation
);
704 gtk_pizza_adjust_allocations_recurse (GtkWidget
*widget
,
707 GtkPizzaAdjData
*data
= cb_data
;
709 widget
->allocation
.x
+= data
->dx
;
710 widget
->allocation
.y
+= data
->dy
;
712 if (GTK_WIDGET_NO_WINDOW (widget
) && GTK_IS_CONTAINER (widget
))
714 gtk_container_forall (GTK_CONTAINER (widget
),
715 gtk_pizza_adjust_allocations_recurse
,
721 gtk_pizza_adjust_allocations (GtkPizza
*pizza
,
726 GtkPizzaAdjData data
;
731 tmp_list
= pizza
->children
;
734 GtkPizzaChild
*child
= tmp_list
->data
;
735 tmp_list
= tmp_list
->next
;
737 child
->widget
->allocation
.x
+= dx
;
738 child
->widget
->allocation
.y
+= dy
;
740 if (GTK_WIDGET_NO_WINDOW (child
->widget
) &&
741 GTK_IS_CONTAINER (child
->widget
))
743 gtk_container_forall (GTK_CONTAINER (child
->widget
),
744 gtk_pizza_adjust_allocations_recurse
,
751 gtk_pizza_scroll (GtkPizza
*pizza
, gint dx
, gint dy
)
753 pizza
->xoffset
+= dx
;
754 pizza
->yoffset
+= dy
;
756 gtk_pizza_adjust_allocations (pizza
, -dx
, -dy
);
758 if (pizza
->bin_window
)
759 gdk_window_scroll( pizza
->bin_window
, -dx
, -dy
);
764 #endif /* __cplusplus */