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
,
67 static GtkWidgetClass
* pizza_parent_class
;
72 static GtkType pizza_type
= 0;
76 static const GTypeInfo pizza_info
=
78 sizeof (GtkPizzaClass
),
80 NULL
, /* base_finalize */
81 (GClassInitFunc
) gtk_pizza_class_init
,
82 NULL
, /* class_finalize */
83 NULL
, /* class_data */
86 (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
;
192 pizza
->bin_window
= NULL
;
194 pizza
->m_xoffset
= 0;
195 pizza
->m_yoffset
= 0;
203 pizza
= g_object_new (gtk_pizza_get_type (), NULL
);
205 return GTK_WIDGET (pizza
);
208 gint
gtk_pizza_get_xoffset (GtkPizza
*pizza
)
210 g_return_val_if_fail ( (pizza
!= NULL
), -1 );
211 g_return_val_if_fail ( (GTK_IS_PIZZA (pizza
)), -1 );
213 return pizza
->m_xoffset
;
216 gint
gtk_pizza_get_yoffset (GtkPizza
*pizza
)
218 g_return_val_if_fail ( (pizza
!= NULL
), -1 );
219 g_return_val_if_fail ( (GTK_IS_PIZZA (pizza
)), -1 );
221 return pizza
->m_yoffset
;
224 void gtk_pizza_set_xoffset (GtkPizza
*pizza
, gint xoffset
)
226 g_return_if_fail (pizza
!= NULL
);
227 g_return_if_fail (GTK_IS_PIZZA (pizza
));
229 pizza
->m_xoffset
= xoffset
;
233 void gtk_pizza_set_yoffset (GtkPizza
*pizza
, gint yoffset
)
235 g_return_if_fail (pizza
!= NULL
);
236 g_return_if_fail (GTK_IS_PIZZA (pizza
));
238 pizza
->m_xoffset
= yoffset
;
242 gint
gtk_pizza_get_rtl_offset (GtkPizza
*pizza
)
247 g_return_val_if_fail ( (pizza
!= NULL
), 0 );
248 g_return_val_if_fail ( (GTK_IS_PIZZA (pizza
)), 0 );
250 if (!pizza
->bin_window
) return 0;
252 gdk_window_get_geometry( pizza
->bin_window
, NULL
, NULL
, &width
, NULL
, NULL
);
254 if (pizza
->shadow_type
== GTK_MYSHADOW_NONE
)
257 if (pizza
->shadow_type
== GTK_MYSHADOW_THIN
)
262 return width
-border
*2;
267 gtk_pizza_scroll_set_adjustments (GtkPizza
*pizza
,
271 /* We handle scrolling in the wxScrolledWindow, not here. */
275 gtk_pizza_set_shadow_type (GtkPizza
*pizza
,
276 GtkMyShadowType type
)
278 g_return_if_fail (pizza
!= NULL
);
279 g_return_if_fail (GTK_IS_PIZZA (pizza
));
281 if (pizza
->shadow_type
!= type
)
283 pizza
->shadow_type
= type
;
285 if (GTK_WIDGET_VISIBLE (pizza
))
287 gtk_widget_size_allocate (GTK_WIDGET (pizza
), &(GTK_WIDGET (pizza
)->allocation
));
288 gtk_widget_queue_draw (GTK_WIDGET (pizza
));
294 gtk_pizza_put (GtkPizza
*pizza
,
301 GtkPizzaChild
*child_info
;
303 g_return_if_fail (pizza
!= NULL
);
304 g_return_if_fail (GTK_IS_PIZZA (pizza
));
305 g_return_if_fail (widget
!= NULL
);
307 if (gtk_widget_get_direction( GTK_WIDGET(pizza
) ) == GTK_TEXT_DIR_RTL
)
309 /* reverse horizontal placement */
310 x
= GTK_WIDGET(pizza
)->allocation
.width
- x
- width
;
313 child_info
= g_new (GtkPizzaChild
, 1);
315 child_info
->widget
= widget
;
318 child_info
->width
= width
;
319 child_info
->height
= height
;
321 pizza
->children
= g_list_append (pizza
->children
, child_info
);
323 if (GTK_WIDGET_REALIZED (pizza
))
324 gtk_widget_set_parent_window (widget
, pizza
->bin_window
);
326 gtk_widget_set_parent (widget
, GTK_WIDGET (pizza
));
328 gtk_widget_set_size_request (widget
, width
, height
);
332 gtk_pizza_set_size (GtkPizza
*pizza
,
339 GtkPizzaChild
*child
;
342 g_return_if_fail (pizza
!= NULL
);
343 g_return_if_fail (GTK_IS_PIZZA (pizza
));
344 g_return_if_fail (widget
!= NULL
);
346 #ifndef WX_WARN_ILLEGAL_SETSIZE
347 /* this really shouldn't happen -- but it does, a lot, right now and we
348 can't pass negative values to gtk_widget_set_size_request() without getting
349 a warning printed out, so filter them out here */
356 children
= pizza
->children
;
359 child
= children
->data
;
360 children
= children
->next
;
362 if (child
->widget
== widget
)
364 if ((child
->x
== x
) &&
366 (child
->width
== width
) &&
367 (child
->height
== height
)) return;
371 child
->width
= width
;
372 child
->height
= height
;
374 gtk_widget_set_size_request (widget
, width
, height
);
382 gtk_pizza_map (GtkWidget
*widget
)
385 GtkPizzaChild
*child
;
388 g_return_if_fail (widget
!= NULL
);
389 g_return_if_fail (GTK_IS_PIZZA (widget
));
391 GTK_WIDGET_SET_FLAGS (widget
, GTK_MAPPED
);
392 pizza
= GTK_PIZZA (widget
);
394 children
= pizza
->children
;
397 child
= children
->data
;
398 children
= children
->next
;
400 if ( GTK_WIDGET_VISIBLE (child
->widget
) &&
401 !GTK_WIDGET_MAPPED (child
->widget
) )
403 gtk_widget_map (child
->widget
);
407 gdk_window_show (widget
->window
);
408 gdk_window_show (pizza
->bin_window
);
412 gtk_pizza_realize (GtkWidget
*widget
)
415 GdkWindowAttr attributes
;
416 gint attributes_mask
;
417 GtkPizzaChild
*child
;
420 g_return_if_fail (widget
!= NULL
);
421 g_return_if_fail (GTK_IS_PIZZA (widget
));
423 pizza
= GTK_PIZZA (widget
);
424 GTK_WIDGET_SET_FLAGS (widget
, GTK_REALIZED
);
426 attributes
.window_type
= GDK_WINDOW_CHILD
;
428 attributes
.x
= widget
->allocation
.x
;
429 attributes
.y
= widget
->allocation
.y
;
430 attributes
.width
= widget
->allocation
.width
;
431 attributes
.height
= widget
->allocation
.height
;
433 #ifndef __WXUNIVERSAL__
434 if (pizza
->shadow_type
== GTK_MYSHADOW_NONE
)
436 /* no border, no changes to sizes */
438 else if (pizza
->shadow_type
== GTK_MYSHADOW_THIN
)
440 /* GTK_MYSHADOW_THIN == wxSIMPLE_BORDER */
443 attributes
.width
-= 2;
444 attributes
.height
-= 2;
448 /* GTK_MYSHADOW_IN == wxSUNKEN_BORDER */
449 /* GTK_MYSHADOW_OUT == wxRAISED_BORDER */
452 attributes
.width
-= 4;
453 attributes
.height
-= 4;
455 #endif /* __WXUNIVERSAL__ */
458 if (attributes
.width
< 2) attributes
.width
= 2;
459 if (attributes
.height
< 2) attributes
.height
= 2;
461 attributes
.wclass
= GDK_INPUT_OUTPUT
;
462 attributes
.visual
= gtk_widget_get_visual (widget
);
463 attributes
.colormap
= gtk_widget_get_colormap (widget
);
464 attributes
.event_mask
= GDK_VISIBILITY_NOTIFY_MASK
;
465 attributes_mask
= GDK_WA_X
| GDK_WA_Y
| GDK_WA_VISUAL
| GDK_WA_COLORMAP
;
467 widget
->window
= gdk_window_new(gtk_widget_get_parent_window (widget
),
468 &attributes
, attributes_mask
);
469 gdk_window_set_user_data (widget
->window
, widget
);
474 attributes
.event_mask
= gtk_widget_get_events (widget
);
475 attributes
.event_mask
|= GDK_EXPOSURE_MASK
|
477 GDK_POINTER_MOTION_MASK
|
478 GDK_POINTER_MOTION_HINT_MASK
|
479 GDK_BUTTON_MOTION_MASK
|
480 GDK_BUTTON1_MOTION_MASK
|
481 GDK_BUTTON2_MOTION_MASK
|
482 GDK_BUTTON3_MOTION_MASK
|
483 GDK_BUTTON_PRESS_MASK
|
484 GDK_BUTTON_RELEASE_MASK
|
486 GDK_KEY_RELEASE_MASK
|
487 GDK_ENTER_NOTIFY_MASK
|
488 GDK_LEAVE_NOTIFY_MASK
|
489 GDK_FOCUS_CHANGE_MASK
;
491 pizza
->bin_window
= gdk_window_new(widget
->window
,
492 &attributes
, attributes_mask
);
493 gdk_window_set_user_data (pizza
->bin_window
, widget
);
495 widget
->style
= gtk_style_attach (widget
->style
, widget
->window
);
496 gtk_style_set_background (widget
->style
, widget
->window
, GTK_STATE_NORMAL
);
497 gtk_style_set_background (widget
->style
, pizza
->bin_window
, GTK_STATE_NORMAL
);
500 gdk_window_set_back_pixmap( widget->window, NULL, FALSE );
501 gdk_window_set_back_pixmap( pizza->bin_window, NULL, FALSE );
504 /* cannot be done before realisation */
505 children
= pizza
->children
;
508 child
= children
->data
;
509 children
= children
->next
;
511 gtk_widget_set_parent_window (child
->widget
, pizza
->bin_window
);
516 gtk_pizza_unrealize (GtkWidget
*widget
)
520 g_return_if_fail (widget
!= NULL
);
521 g_return_if_fail (GTK_IS_PIZZA (widget
));
523 pizza
= GTK_PIZZA (widget
);
525 gdk_window_set_user_data (pizza
->bin_window
, NULL
);
526 gdk_window_destroy (pizza
->bin_window
);
527 pizza
->bin_window
= NULL
;
529 if (pizza_parent_class
->unrealize
)
530 pizza_parent_class
->unrealize(widget
);
534 gtk_pizza_size_request (GtkWidget
*widget
,
535 GtkRequisition
*requisition
)
538 GtkPizzaChild
*child
;
540 GtkRequisition child_requisition
;
542 g_return_if_fail (widget
!= NULL
);
543 g_return_if_fail (GTK_IS_PIZZA (widget
));
544 g_return_if_fail (requisition
!= NULL
);
546 pizza
= GTK_PIZZA (widget
);
548 children
= pizza
->children
;
551 child
= children
->data
;
552 children
= children
->next
;
554 if (GTK_WIDGET_VISIBLE (child
->widget
))
556 gtk_widget_size_request (child
->widget
, &child_requisition
);
560 /* request very little, I'm not sure if requesting nothing
561 will always have positive effects on stability... */
562 requisition
->width
= 2;
563 requisition
->height
= 2;
567 gtk_pizza_size_allocate (GtkWidget
*widget
,
568 GtkAllocation
*allocation
)
573 GtkPizzaChild
*child
;
575 gboolean only_resize
;
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 only_resize
= ((widget
->allocation
.x
== allocation
->x
) &&
584 (widget
->allocation
.y
== allocation
->y
));
585 widget
->allocation
= *allocation
;
587 if (pizza
->shadow_type
== GTK_MYSHADOW_NONE
)
590 if (pizza
->shadow_type
== GTK_MYSHADOW_THIN
)
595 x
= allocation
->x
+ border
;
596 y
= allocation
->y
+ border
;
597 w
= allocation
->width
- border
*2;
598 h
= allocation
->height
- border
*2;
604 if (GTK_WIDGET_REALIZED (widget
))
607 gdk_window_resize( widget
->window
, w
, h
);
609 gdk_window_move_resize( widget
->window
, x
, y
, w
, h
);
611 gdk_window_resize( pizza
->bin_window
, w
, h
);
614 children
= pizza
->children
;
617 child
= children
->data
;
618 children
= children
->next
;
620 gtk_pizza_allocate_child (pizza
, child
);
625 gtk_pizza_expose (GtkWidget
*widget
,
626 GdkEventExpose
*event
)
630 g_return_val_if_fail (widget
!= NULL
, FALSE
);
631 g_return_val_if_fail (GTK_IS_PIZZA (widget
), FALSE
);
632 g_return_val_if_fail (event
!= NULL
, FALSE
);
634 pizza
= (GtkPizza
*)widget
;
636 if (event
->window
!= pizza
->bin_window
)
639 pizza_parent_class
->expose_event(widget
, event
);
645 gtk_pizza_style_set(GtkWidget
*widget
, GtkStyle
*previous_style
)
647 if (GTK_WIDGET_REALIZED(widget
))
649 gtk_style_set_background(widget
->style
, widget
->window
, GTK_STATE_NORMAL
);
650 gtk_style_set_background(widget
->style
, GTK_PIZZA(widget
)->bin_window
, GTK_STATE_NORMAL
);
653 pizza_parent_class
->style_set(widget
, previous_style
);
657 gtk_pizza_add (GtkContainer
*container
,
660 g_return_if_fail (container
!= NULL
);
661 g_return_if_fail (GTK_IS_PIZZA (container
));
662 g_return_if_fail (widget
!= NULL
);
664 gtk_pizza_put (GTK_PIZZA (container
), widget
, 0, 0, 20, 20 );
668 gtk_pizza_remove (GtkContainer
*container
,
672 GtkPizzaChild
*child
;
675 g_return_if_fail (container
!= NULL
);
676 g_return_if_fail (GTK_IS_PIZZA (container
));
677 g_return_if_fail (widget
!= NULL
);
679 pizza
= GTK_PIZZA (container
);
681 children
= pizza
->children
;
684 child
= children
->data
;
686 if (child
->widget
== widget
)
688 gtk_widget_unparent (widget
);
690 /* security checks */
691 g_return_if_fail (GTK_IS_WIDGET (widget
));
693 pizza
->children
= g_list_remove_link (pizza
->children
, children
);
694 g_list_free (children
);
697 /* security checks */
698 g_return_if_fail (GTK_IS_WIDGET (widget
));
703 children
= children
->next
;
708 gtk_pizza_forall (GtkContainer
*container
,
709 gboolean include_internals
,
710 GtkCallback callback
,
711 gpointer callback_data
)
714 GtkPizzaChild
*child
;
717 g_return_if_fail (container
!= NULL
);
718 g_return_if_fail (GTK_IS_PIZZA (container
));
719 g_return_if_fail (callback
!= (GtkCallback
)NULL
);
721 pizza
= GTK_PIZZA (container
);
723 children
= pizza
->children
;
726 child
= children
->data
;
727 children
= children
->next
;
729 (* callback
) (child
->widget
, callback_data
);
734 gtk_pizza_allocate_child (GtkPizza
*pizza
,
735 GtkPizzaChild
*child
)
737 GtkAllocation allocation
;
738 GtkRequisition requisition
;
740 allocation
.x
= child
->x
- pizza
->m_xoffset
;
741 allocation
.y
= child
->y
- pizza
->m_yoffset
;
742 gtk_widget_get_child_requisition (child
->widget
, &requisition
);
743 allocation
.width
= requisition
.width
;
744 allocation
.height
= requisition
.height
;
746 if (gtk_widget_get_direction( GTK_WIDGET(pizza
) ) == GTK_TEXT_DIR_RTL
)
748 /* reverse horizontal placement */
751 offset
= GTK_WIDGET(pizza
)->allocation
.width
;
753 if (pizza
->shadow_type
== GTK_MYSHADOW_NONE
)
756 if (pizza
->shadow_type
== GTK_MYSHADOW_THIN
)
762 allocation
.x
= offset
- child
->x
- allocation
.width
- pizza
->m_xoffset
;
765 gtk_widget_size_allocate (child
->widget
, &allocation
);
769 gtk_pizza_adjust_allocations_recurse (GtkWidget
*widget
,
772 GtkPizzaAdjData
*data
= cb_data
;
774 widget
->allocation
.x
+= data
->dx
;
775 widget
->allocation
.y
+= data
->dy
;
777 if (GTK_WIDGET_NO_WINDOW (widget
) && GTK_IS_CONTAINER (widget
))
779 gtk_container_forall (GTK_CONTAINER (widget
),
780 gtk_pizza_adjust_allocations_recurse
,
786 gtk_pizza_adjust_allocations (GtkPizza
*pizza
,
791 GtkPizzaAdjData data
;
796 tmp_list
= pizza
->children
;
799 GtkPizzaChild
*child
= tmp_list
->data
;
800 tmp_list
= tmp_list
->next
;
802 child
->widget
->allocation
.x
+= dx
;
803 child
->widget
->allocation
.y
+= dy
;
805 if (GTK_WIDGET_NO_WINDOW (child
->widget
) &&
806 GTK_IS_CONTAINER (child
->widget
))
808 gtk_container_forall (GTK_CONTAINER (child
->widget
),
809 gtk_pizza_adjust_allocations_recurse
,
816 gtk_pizza_scroll (GtkPizza
*pizza
, gint dx
, gint dy
)
818 pizza
->m_xoffset
+= dx
;
819 pizza
->m_yoffset
+= dy
;
821 gtk_pizza_adjust_allocations (pizza
, -dx
, -dy
);
823 if (pizza
->bin_window
)
824 gdk_window_scroll( pizza
->bin_window
, -dx
, -dy
);
829 #endif /* __cplusplus */