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
;
193 pizza
->m_height
= 20;
195 pizza
->bin_window
= NULL
;
197 pizza
->m_xoffset
= 0;
198 pizza
->m_yoffset
= 0;
200 pizza
->external_expose
= FALSE
;
208 pizza
= g_object_new (gtk_pizza_get_type (), NULL
);
210 return GTK_WIDGET (pizza
);
213 gint
gtk_pizza_get_width (GtkPizza
*pizza
)
215 g_return_val_if_fail ( (pizza
!= NULL
), -1 );
216 g_return_val_if_fail ( (GTK_IS_PIZZA (pizza
)), -1 );
218 return pizza
->m_width
;
221 gint
gtk_pizza_get_height (GtkPizza
*pizza
)
223 g_return_val_if_fail ( (pizza
!= NULL
), -1 );
224 g_return_val_if_fail ( (GTK_IS_PIZZA (pizza
)), -1 );
226 return pizza
->m_height
;
229 gint
gtk_pizza_get_xoffset (GtkPizza
*pizza
)
231 g_return_val_if_fail ( (pizza
!= NULL
), -1 );
232 g_return_val_if_fail ( (GTK_IS_PIZZA (pizza
)), -1 );
234 return pizza
->m_xoffset
;
237 gint
gtk_pizza_get_yoffset (GtkPizza
*pizza
)
239 g_return_val_if_fail ( (pizza
!= NULL
), -1 );
240 g_return_val_if_fail ( (GTK_IS_PIZZA (pizza
)), -1 );
242 return pizza
->m_yoffset
;
245 void gtk_pizza_set_xoffset (GtkPizza
*pizza
, gint xoffset
)
247 g_return_if_fail (pizza
!= NULL
);
248 g_return_if_fail (GTK_IS_PIZZA (pizza
));
250 pizza
->m_xoffset
= xoffset
;
254 void gtk_pizza_set_yoffset (GtkPizza
*pizza
, gint yoffset
)
256 g_return_if_fail (pizza
!= NULL
);
257 g_return_if_fail (GTK_IS_PIZZA (pizza
));
259 pizza
->m_xoffset
= yoffset
;
264 gtk_pizza_scroll_set_adjustments (GtkPizza
*pizza
,
268 /* We handle scrolling in the wxScrolledWindow, not here. */
272 gtk_pizza_set_shadow_type (GtkPizza
*pizza
,
273 GtkMyShadowType type
)
275 g_return_if_fail (pizza
!= NULL
);
276 g_return_if_fail (GTK_IS_PIZZA (pizza
));
278 if (pizza
->shadow_type
!= type
)
280 pizza
->shadow_type
= type
;
282 if (GTK_WIDGET_VISIBLE (pizza
))
284 gtk_widget_size_allocate (GTK_WIDGET (pizza
), &(GTK_WIDGET (pizza
)->allocation
));
285 gtk_widget_queue_draw (GTK_WIDGET (pizza
));
291 gtk_pizza_set_external (GtkPizza
*pizza
,
294 g_return_if_fail (pizza
!= NULL
);
295 g_return_if_fail (GTK_IS_PIZZA (pizza
));
297 pizza
->external_expose
= expose
;
301 gtk_pizza_put (GtkPizza
*pizza
,
308 GtkPizzaChild
*child_info
;
310 g_return_if_fail (pizza
!= NULL
);
311 g_return_if_fail (GTK_IS_PIZZA (pizza
));
312 g_return_if_fail (widget
!= NULL
);
314 child_info
= g_new (GtkPizzaChild
, 1);
316 child_info
->widget
= widget
;
319 child_info
->width
= width
;
320 child_info
->height
= height
;
322 pizza
->children
= g_list_append (pizza
->children
, child_info
);
324 if (GTK_WIDGET_REALIZED (pizza
))
325 gtk_widget_set_parent_window (widget
, pizza
->bin_window
);
327 gtk_widget_set_parent (widget
, GTK_WIDGET (pizza
));
329 gtk_widget_set_size_request (widget
, width
, height
);
333 gtk_pizza_set_size (GtkPizza
*pizza
,
340 GtkPizzaChild
*child
;
343 g_return_if_fail (pizza
!= NULL
);
344 g_return_if_fail (GTK_IS_PIZZA (pizza
));
345 g_return_if_fail (widget
!= NULL
);
347 #ifndef WX_WARN_ILLEGAL_SETSIZE
348 /* this really shouldn't happen -- but it does, a lot, right now and we
349 can't pass negative values to gtk_widget_set_size_request() without getting
350 a warning printed out, so filter them out here */
357 children
= pizza
->children
;
360 child
= children
->data
;
361 children
= children
->next
;
363 if (child
->widget
== widget
)
365 if ((child
->x
== x
) &&
367 (child
->width
== width
) &&
368 (child
->height
== height
)) return;
372 child
->width
= width
;
373 child
->height
= height
;
375 gtk_widget_set_size_request (widget
, width
, height
);
383 gtk_pizza_map (GtkWidget
*widget
)
386 GtkPizzaChild
*child
;
389 g_return_if_fail (widget
!= NULL
);
390 g_return_if_fail (GTK_IS_PIZZA (widget
));
392 GTK_WIDGET_SET_FLAGS (widget
, GTK_MAPPED
);
393 pizza
= GTK_PIZZA (widget
);
395 children
= pizza
->children
;
398 child
= children
->data
;
399 children
= children
->next
;
401 if ( GTK_WIDGET_VISIBLE (child
->widget
) &&
402 !GTK_WIDGET_MAPPED (child
->widget
) )
404 gtk_widget_map (child
->widget
);
408 gdk_window_show (widget
->window
);
409 gdk_window_show (pizza
->bin_window
);
413 gtk_pizza_realize (GtkWidget
*widget
)
416 GdkWindowAttr attributes
;
417 gint attributes_mask
;
418 GtkPizzaChild
*child
;
421 g_return_if_fail (widget
!= NULL
);
422 g_return_if_fail (GTK_IS_PIZZA (widget
));
424 pizza
= GTK_PIZZA (widget
);
425 GTK_WIDGET_SET_FLAGS (widget
, GTK_REALIZED
);
427 attributes
.window_type
= GDK_WINDOW_CHILD
;
429 attributes
.x
= widget
->allocation
.x
;
430 attributes
.y
= widget
->allocation
.y
;
431 attributes
.width
= widget
->allocation
.width
;
432 attributes
.height
= widget
->allocation
.height
;
434 #ifndef __WXUNIVERSAL__
435 if (pizza
->shadow_type
== GTK_MYSHADOW_NONE
)
437 /* no border, no changes to sizes */
439 else if (pizza
->shadow_type
== GTK_MYSHADOW_THIN
)
441 /* GTK_MYSHADOW_THIN == wxSIMPLE_BORDER */
444 attributes
.width
-= 2;
445 attributes
.height
-= 2;
449 /* GTK_MYSHADOW_IN == wxSUNKEN_BORDER */
450 /* GTK_MYSHADOW_OUT == wxRAISED_BORDER */
453 attributes
.width
-= 4;
454 attributes
.height
-= 4;
456 #endif /* __WXUNIVERSAL__ */
459 if (attributes
.width
< 2) attributes
.width
= 2;
460 if (attributes
.height
< 2) attributes
.height
= 2;
462 attributes
.wclass
= GDK_INPUT_OUTPUT
;
463 attributes
.visual
= gtk_widget_get_visual (widget
);
464 attributes
.colormap
= gtk_widget_get_colormap (widget
);
465 attributes
.event_mask
= GDK_VISIBILITY_NOTIFY_MASK
;
466 attributes_mask
= GDK_WA_X
| GDK_WA_Y
| GDK_WA_VISUAL
| GDK_WA_COLORMAP
;
468 widget
->window
= gdk_window_new(gtk_widget_get_parent_window (widget
),
469 &attributes
, attributes_mask
);
470 gdk_window_set_user_data (widget
->window
, widget
);
475 attributes
.event_mask
= gtk_widget_get_events (widget
);
476 attributes
.event_mask
|= GDK_EXPOSURE_MASK
|
478 GDK_POINTER_MOTION_MASK
|
479 GDK_POINTER_MOTION_HINT_MASK
|
480 GDK_BUTTON_MOTION_MASK
|
481 GDK_BUTTON1_MOTION_MASK
|
482 GDK_BUTTON2_MOTION_MASK
|
483 GDK_BUTTON3_MOTION_MASK
|
484 GDK_BUTTON_PRESS_MASK
|
485 GDK_BUTTON_RELEASE_MASK
|
487 GDK_KEY_RELEASE_MASK
|
488 GDK_ENTER_NOTIFY_MASK
|
489 GDK_LEAVE_NOTIFY_MASK
|
490 GDK_FOCUS_CHANGE_MASK
;
492 pizza
->bin_window
= gdk_window_new(widget
->window
,
493 &attributes
, attributes_mask
);
494 gdk_window_set_user_data (pizza
->bin_window
, widget
);
496 widget
->style
= gtk_style_attach (widget
->style
, widget
->window
);
497 gtk_style_set_background (widget
->style
, widget
->window
, GTK_STATE_NORMAL
);
498 gtk_style_set_background (widget
->style
, pizza
->bin_window
, GTK_STATE_NORMAL
);
501 gdk_window_set_back_pixmap( widget->window, NULL, FALSE );
502 gdk_window_set_back_pixmap( pizza->bin_window, NULL, FALSE );
505 /* cannot be done before realisation */
506 children
= pizza
->children
;
509 child
= children
->data
;
510 children
= children
->next
;
512 gtk_widget_set_parent_window (child
->widget
, pizza
->bin_window
);
517 gtk_pizza_unrealize (GtkWidget
*widget
)
521 g_return_if_fail (widget
!= NULL
);
522 g_return_if_fail (GTK_IS_PIZZA (widget
));
524 pizza
= GTK_PIZZA (widget
);
526 gdk_window_set_user_data (pizza
->bin_window
, NULL
);
527 gdk_window_destroy (pizza
->bin_window
);
528 pizza
->bin_window
= NULL
;
530 if (GTK_WIDGET_CLASS (pizza_parent_class
)->unrealize
)
531 (* GTK_WIDGET_CLASS (pizza_parent_class
)->unrealize
) (widget
);
535 gtk_pizza_size_request (GtkWidget
*widget
,
536 GtkRequisition
*requisition
)
539 GtkPizzaChild
*child
;
541 GtkRequisition child_requisition
;
543 g_return_if_fail (widget
!= NULL
);
544 g_return_if_fail (GTK_IS_PIZZA (widget
));
545 g_return_if_fail (requisition
!= NULL
);
547 pizza
= GTK_PIZZA (widget
);
549 children
= pizza
->children
;
552 child
= children
->data
;
553 children
= children
->next
;
555 if (GTK_WIDGET_VISIBLE (child
->widget
))
557 gtk_widget_size_request (child
->widget
, &child_requisition
);
561 /* request very little, I'm not sure if requesting nothing
562 will always have positive effects on stability... */
563 requisition
->width
= 2;
564 requisition
->height
= 2;
568 gtk_pizza_size_allocate (GtkWidget
*widget
,
569 GtkAllocation
*allocation
)
574 GtkPizzaChild
*child
;
577 g_return_if_fail (widget
!= NULL
);
578 g_return_if_fail (GTK_IS_PIZZA(widget
));
579 g_return_if_fail (allocation
!= NULL
);
581 pizza
= GTK_PIZZA (widget
);
583 widget
->allocation
= *allocation
;
585 if (pizza
->shadow_type
== GTK_MYSHADOW_NONE
)
588 if (pizza
->shadow_type
== GTK_MYSHADOW_THIN
)
593 x
= allocation
->x
+ border
;
594 y
= allocation
->y
+ border
;
595 w
= allocation
->width
- border
*2;
596 h
= allocation
->height
- border
*2;
602 if (GTK_WIDGET_REALIZED (widget
))
604 gdk_window_move_resize( widget
->window
, x
, y
, w
, h
);
605 gdk_window_move_resize( pizza
->bin_window
, 0, 0, w
, h
);
608 children
= pizza
->children
;
611 child
= children
->data
;
612 children
= children
->next
;
614 gtk_pizza_allocate_child (pizza
, child
);
619 gtk_pizza_expose (GtkWidget
*widget
,
620 GdkEventExpose
*event
)
624 g_return_val_if_fail (widget
!= NULL
, FALSE
);
625 g_return_val_if_fail (GTK_IS_PIZZA (widget
), FALSE
);
626 g_return_val_if_fail (event
!= NULL
, FALSE
);
628 pizza
= GTK_PIZZA (widget
);
630 if (event
->window
!= pizza
->bin_window
)
633 /* We handle all expose events in window.cpp now. */
634 if (pizza
->external_expose
)
637 (* GTK_WIDGET_CLASS (pizza_parent_class
)->expose_event
) (widget
, event
);
643 gtk_pizza_style_set(GtkWidget
*widget
, GtkStyle
*previous_style
)
645 if (GTK_WIDGET_REALIZED(widget
))
647 gtk_style_set_background(widget
->style
, widget
->window
, GTK_STATE_NORMAL
);
648 gtk_style_set_background(widget
->style
, GTK_PIZZA(widget
)->bin_window
, GTK_STATE_NORMAL
);
651 (* GTK_WIDGET_CLASS (pizza_parent_class
)->style_set
) (widget
, previous_style
);
655 gtk_pizza_add (GtkContainer
*container
,
658 g_return_if_fail (container
!= NULL
);
659 g_return_if_fail (GTK_IS_PIZZA (container
));
660 g_return_if_fail (widget
!= NULL
);
662 gtk_pizza_put (GTK_PIZZA (container
), widget
, 0, 0, 20, 20 );
666 gtk_pizza_remove (GtkContainer
*container
,
670 GtkPizzaChild
*child
;
673 g_return_if_fail (container
!= NULL
);
674 g_return_if_fail (GTK_IS_PIZZA (container
));
675 g_return_if_fail (widget
!= NULL
);
677 pizza
= GTK_PIZZA (container
);
679 children
= pizza
->children
;
682 child
= children
->data
;
684 if (child
->widget
== widget
)
686 gtk_widget_unparent (widget
);
688 /* security checks */
689 g_return_if_fail (GTK_IS_WIDGET (widget
));
691 pizza
->children
= g_list_remove_link (pizza
->children
, children
);
692 g_list_free (children
);
695 /* security checks */
696 g_return_if_fail (GTK_IS_WIDGET (widget
));
701 children
= children
->next
;
706 gtk_pizza_forall (GtkContainer
*container
,
707 gboolean include_internals
,
708 GtkCallback callback
,
709 gpointer callback_data
)
712 GtkPizzaChild
*child
;
715 g_return_if_fail (container
!= NULL
);
716 g_return_if_fail (GTK_IS_PIZZA (container
));
717 g_return_if_fail (callback
!= (GtkCallback
)NULL
);
719 pizza
= GTK_PIZZA (container
);
721 children
= pizza
->children
;
724 child
= children
->data
;
725 children
= children
->next
;
727 (* callback
) (child
->widget
, callback_data
);
732 gtk_pizza_allocate_child (GtkPizza
*pizza
,
733 GtkPizzaChild
*child
)
735 GtkAllocation allocation
;
736 GtkRequisition requisition
;
738 allocation
.x
= child
->x
- pizza
->m_xoffset
;
739 allocation
.y
= child
->y
- pizza
->m_yoffset
;
740 gtk_widget_get_child_requisition (child
->widget
, &requisition
);
741 allocation
.width
= requisition
.width
;
742 allocation
.height
= requisition
.height
;
744 gtk_widget_size_allocate (child
->widget
, &allocation
);
748 gtk_pizza_adjust_allocations_recurse (GtkWidget
*widget
,
751 GtkPizzaAdjData
*data
= cb_data
;
753 widget
->allocation
.x
+= data
->dx
;
754 widget
->allocation
.y
+= data
->dy
;
756 if (GTK_WIDGET_NO_WINDOW (widget
) && GTK_IS_CONTAINER (widget
))
758 gtk_container_forall (GTK_CONTAINER (widget
),
759 gtk_pizza_adjust_allocations_recurse
,
765 gtk_pizza_adjust_allocations (GtkPizza
*pizza
,
770 GtkPizzaAdjData data
;
775 tmp_list
= pizza
->children
;
778 GtkPizzaChild
*child
= tmp_list
->data
;
779 tmp_list
= tmp_list
->next
;
781 child
->widget
->allocation
.x
+= dx
;
782 child
->widget
->allocation
.y
+= dy
;
784 if (GTK_WIDGET_NO_WINDOW (child
->widget
) &&
785 GTK_IS_CONTAINER (child
->widget
))
787 gtk_container_forall (GTK_CONTAINER (child
->widget
),
788 gtk_pizza_adjust_allocations_recurse
,
795 gtk_pizza_scroll (GtkPizza
*pizza
, gint dx
, gint dy
)
797 pizza
->m_xoffset
+= dx
;
798 pizza
->m_yoffset
+= dy
;
800 gtk_pizza_adjust_allocations (pizza
, -dx
, -dy
);
802 if (pizza
->bin_window
)
803 gdk_window_scroll( pizza
->bin_window
, -dx
, -dy
);
808 #endif /* __cplusplus */