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
,
88 pizza_type
= g_type_register_static (GTK_TYPE_CONTAINER
, "GtkPizza", &pizza_info
, (GTypeFlags
)0);
94 /* Marshaller needed for set_scroll_adjustments signal,
95 generated with GLib-2.4.6 glib-genmarshal */
96 #define g_marshal_value_peek_object(v) g_value_get_object (v)
98 g_cclosure_user_marshal_VOID__OBJECT_OBJECT (GClosure
*closure
,
100 guint n_param_values
,
101 const GValue
*param_values
,
102 gpointer invocation_hint
,
103 gpointer marshal_data
)
105 typedef void (*GMarshalFunc_VOID__OBJECT_OBJECT
) (gpointer data1
,
109 register GMarshalFunc_VOID__OBJECT_OBJECT callback
;
110 register GCClosure
*cc
= (GCClosure
*) closure
;
111 register gpointer data1
, data2
;
113 g_return_if_fail (n_param_values
== 3);
115 if (G_CCLOSURE_SWAP_DATA (closure
))
117 data1
= closure
->data
;
118 data2
= g_value_peek_pointer (param_values
+ 0);
122 data1
= g_value_peek_pointer (param_values
+ 0);
123 data2
= closure
->data
;
125 callback
= (GMarshalFunc_VOID__OBJECT_OBJECT
) (marshal_data
? marshal_data
: cc
->callback
);
128 g_marshal_value_peek_object (param_values
+ 1),
129 g_marshal_value_peek_object (param_values
+ 2),
134 gtk_pizza_class_init (GtkPizzaClass
*klass
)
136 GtkObjectClass
*object_class
;
137 GtkWidgetClass
*widget_class
;
138 GtkContainerClass
*container_class
;
140 object_class
= (GtkObjectClass
*) klass
;
141 widget_class
= (GtkWidgetClass
*) klass
;
142 container_class
= (GtkContainerClass
*) klass
;
143 pizza_parent_class
= gtk_type_class (GTK_TYPE_CONTAINER
);
145 widget_class
->map
= gtk_pizza_map
;
146 widget_class
->realize
= gtk_pizza_realize
;
147 widget_class
->unrealize
= gtk_pizza_unrealize
;
148 widget_class
->size_request
= gtk_pizza_size_request
;
149 widget_class
->size_allocate
= gtk_pizza_size_allocate
;
150 widget_class
->expose_event
= gtk_pizza_expose
;
151 widget_class
->style_set
= gtk_pizza_style_set
;
153 container_class
->add
= gtk_pizza_add
;
154 container_class
->remove
= gtk_pizza_remove
;
155 container_class
->forall
= gtk_pizza_forall
;
157 container_class
->child_type
= gtk_pizza_child_type
;
159 klass
->set_scroll_adjustments
= gtk_pizza_scroll_set_adjustments
;
161 widget_class
->set_scroll_adjustments_signal
=
163 "set_scroll_adjustments",
164 G_TYPE_FROM_CLASS(object_class
),
166 G_STRUCT_OFFSET(GtkPizzaClass
, set_scroll_adjustments
),
169 g_cclosure_user_marshal_VOID__OBJECT_OBJECT
,
173 GTK_TYPE_ADJUSTMENT
);
177 gtk_pizza_child_type (GtkContainer
*container
)
179 return GTK_TYPE_WIDGET
;
183 gtk_pizza_init (GtkPizza
*pizza
)
185 GTK_WIDGET_UNSET_FLAGS (pizza
, GTK_NO_WINDOW
);
187 pizza
->shadow_type
= GTK_MYSHADOW_NONE
;
189 pizza
->children
= NULL
;
191 pizza
->bin_window
= NULL
;
193 pizza
->m_xoffset
= 0;
194 pizza
->m_yoffset
= 0;
204 pizza
= g_object_new (gtk_pizza_get_type (), NULL
);
206 return GTK_WIDGET (pizza
);
209 gint
gtk_pizza_get_xoffset (GtkPizza
*pizza
)
211 g_return_val_if_fail ( (pizza
!= NULL
), -1 );
212 g_return_val_if_fail ( (GTK_IS_PIZZA (pizza
)), -1 );
214 return pizza
->m_xoffset
;
217 gint
gtk_pizza_get_yoffset (GtkPizza
*pizza
)
219 g_return_val_if_fail ( (pizza
!= NULL
), -1 );
220 g_return_val_if_fail ( (GTK_IS_PIZZA (pizza
)), -1 );
222 return pizza
->m_yoffset
;
225 void gtk_pizza_set_xoffset (GtkPizza
*pizza
, gint xoffset
)
227 g_return_if_fail (pizza
!= NULL
);
228 g_return_if_fail (GTK_IS_PIZZA (pizza
));
230 pizza
->m_xoffset
= xoffset
;
234 void gtk_pizza_set_yoffset (GtkPizza
*pizza
, gint yoffset
)
236 g_return_if_fail (pizza
!= NULL
);
237 g_return_if_fail (GTK_IS_PIZZA (pizza
));
239 pizza
->m_xoffset
= yoffset
;
243 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
);
259 gtk_pizza_scroll_set_adjustments (GtkPizza
*pizza
,
263 /* We handle scrolling in the wxScrolledWindow, not here. */
267 gtk_pizza_set_shadow_type (GtkPizza
*pizza
,
268 GtkMyShadowType type
)
270 g_return_if_fail (pizza
!= NULL
);
271 g_return_if_fail (GTK_IS_PIZZA (pizza
));
273 if (pizza
->shadow_type
!= type
)
275 pizza
->shadow_type
= type
;
277 if (GTK_WIDGET_VISIBLE (pizza
))
279 gtk_widget_size_allocate (GTK_WIDGET (pizza
), &(GTK_WIDGET (pizza
)->allocation
));
280 gtk_widget_queue_draw (GTK_WIDGET (pizza
));
286 gtk_pizza_put (GtkPizza
*pizza
,
293 GtkPizzaChild
*child_info
;
295 g_return_if_fail (pizza
!= NULL
);
296 g_return_if_fail (GTK_IS_PIZZA (pizza
));
297 g_return_if_fail (widget
!= NULL
);
299 if (gtk_widget_get_direction( GTK_WIDGET(pizza
) ) == GTK_TEXT_DIR_RTL
)
301 // reverse horizontal placement
302 x
= pizza
->m_width
- x
- width
;
305 child_info
= g_new (GtkPizzaChild
, 1);
307 child_info
->widget
= widget
;
310 child_info
->width
= width
;
311 child_info
->height
= height
;
313 if (GTK_IS_PIZZA(widget
))
314 GTK_PIZZA(widget
)->m_width
= width
;
316 pizza
->children
= g_list_append (pizza
->children
, child_info
);
318 if (GTK_WIDGET_REALIZED (pizza
))
319 gtk_widget_set_parent_window (widget
, pizza
->bin_window
);
321 gtk_widget_set_parent (widget
, GTK_WIDGET (pizza
));
323 gtk_widget_set_size_request (widget
, width
, height
);
327 gtk_pizza_set_size (GtkPizza
*pizza
,
334 GtkPizzaChild
*child
;
337 g_return_if_fail (pizza
!= NULL
);
338 g_return_if_fail (GTK_IS_PIZZA (pizza
));
339 g_return_if_fail (widget
!= NULL
);
341 #ifndef WX_WARN_ILLEGAL_SETSIZE
342 /* this really shouldn't happen -- but it does, a lot, right now and we
343 can't pass negative values to gtk_widget_set_size_request() without getting
344 a warning printed out, so filter them out here */
351 children
= pizza
->children
;
354 child
= children
->data
;
355 children
= children
->next
;
357 if (child
->widget
== widget
)
360 if (gtk_widget_get_direction( GTK_WIDGET(pizza
) ) == GTK_TEXT_DIR_RTL
)
362 // reverse horizontal placement
363 new_x
= pizza
->m_width
- new_x
- width
;
366 if ((child
->x
== new_x
) &&
368 (child
->width
== width
) &&
369 (child
->height
== height
)) return;
373 child
->width
= width
;
374 child
->height
= height
;
376 if (GTK_IS_PIZZA(widget
))
377 GTK_PIZZA(widget
)->m_width
= width
;
379 gtk_widget_set_size_request (widget
, width
, height
);
387 gtk_pizza_map (GtkWidget
*widget
)
390 GtkPizzaChild
*child
;
393 g_return_if_fail (widget
!= NULL
);
394 g_return_if_fail (GTK_IS_PIZZA (widget
));
396 GTK_WIDGET_SET_FLAGS (widget
, GTK_MAPPED
);
397 pizza
= GTK_PIZZA (widget
);
399 children
= pizza
->children
;
402 child
= children
->data
;
403 children
= children
->next
;
405 if ( GTK_WIDGET_VISIBLE (child
->widget
) &&
406 !GTK_WIDGET_MAPPED (child
->widget
) )
408 gtk_widget_map (child
->widget
);
412 gdk_window_show (widget
->window
);
413 gdk_window_show (pizza
->bin_window
);
417 gtk_pizza_realize (GtkWidget
*widget
)
420 GdkWindowAttr attributes
;
421 gint attributes_mask
;
422 GtkPizzaChild
*child
;
425 g_return_if_fail (widget
!= NULL
);
426 g_return_if_fail (GTK_IS_PIZZA (widget
));
428 pizza
= GTK_PIZZA (widget
);
429 GTK_WIDGET_SET_FLAGS (widget
, GTK_REALIZED
);
431 attributes
.window_type
= GDK_WINDOW_CHILD
;
433 attributes
.x
= widget
->allocation
.x
;
434 attributes
.y
= widget
->allocation
.y
;
435 attributes
.width
= widget
->allocation
.width
;
436 attributes
.height
= widget
->allocation
.height
;
438 #ifndef __WXUNIVERSAL__
439 if (pizza
->shadow_type
== GTK_MYSHADOW_NONE
)
441 /* no border, no changes to sizes */
443 else if (pizza
->shadow_type
== GTK_MYSHADOW_THIN
)
445 /* GTK_MYSHADOW_THIN == wxSIMPLE_BORDER */
448 attributes
.width
-= 2;
449 attributes
.height
-= 2;
453 /* GTK_MYSHADOW_IN == wxSUNKEN_BORDER */
454 /* GTK_MYSHADOW_OUT == wxRAISED_BORDER */
457 attributes
.width
-= 4;
458 attributes
.height
-= 4;
460 #endif /* __WXUNIVERSAL__ */
463 if (attributes
.width
< 2) attributes
.width
= 2;
464 if (attributes
.height
< 2) attributes
.height
= 2;
466 attributes
.wclass
= GDK_INPUT_OUTPUT
;
467 attributes
.visual
= gtk_widget_get_visual (widget
);
468 attributes
.colormap
= gtk_widget_get_colormap (widget
);
469 attributes
.event_mask
= GDK_VISIBILITY_NOTIFY_MASK
;
470 attributes_mask
= GDK_WA_X
| GDK_WA_Y
| GDK_WA_VISUAL
| GDK_WA_COLORMAP
;
472 widget
->window
= gdk_window_new(gtk_widget_get_parent_window (widget
),
473 &attributes
, attributes_mask
);
474 gdk_window_set_user_data (widget
->window
, widget
);
479 attributes
.event_mask
= gtk_widget_get_events (widget
);
480 attributes
.event_mask
|= GDK_EXPOSURE_MASK
|
482 GDK_POINTER_MOTION_MASK
|
483 GDK_POINTER_MOTION_HINT_MASK
|
484 GDK_BUTTON_MOTION_MASK
|
485 GDK_BUTTON1_MOTION_MASK
|
486 GDK_BUTTON2_MOTION_MASK
|
487 GDK_BUTTON3_MOTION_MASK
|
488 GDK_BUTTON_PRESS_MASK
|
489 GDK_BUTTON_RELEASE_MASK
|
491 GDK_KEY_RELEASE_MASK
|
492 GDK_ENTER_NOTIFY_MASK
|
493 GDK_LEAVE_NOTIFY_MASK
|
494 GDK_FOCUS_CHANGE_MASK
;
496 pizza
->bin_window
= gdk_window_new(widget
->window
,
497 &attributes
, attributes_mask
);
498 gdk_window_set_user_data (pizza
->bin_window
, widget
);
500 widget
->style
= gtk_style_attach (widget
->style
, widget
->window
);
501 gtk_style_set_background (widget
->style
, widget
->window
, GTK_STATE_NORMAL
);
502 gtk_style_set_background (widget
->style
, pizza
->bin_window
, GTK_STATE_NORMAL
);
505 gdk_window_set_back_pixmap( widget->window, NULL, FALSE );
506 gdk_window_set_back_pixmap( pizza->bin_window, NULL, FALSE );
509 /* cannot be done before realisation */
510 children
= pizza
->children
;
513 child
= children
->data
;
514 children
= children
->next
;
516 gtk_widget_set_parent_window (child
->widget
, pizza
->bin_window
);
521 gtk_pizza_unrealize (GtkWidget
*widget
)
525 g_return_if_fail (widget
!= NULL
);
526 g_return_if_fail (GTK_IS_PIZZA (widget
));
528 pizza
= GTK_PIZZA (widget
);
530 gdk_window_set_user_data (pizza
->bin_window
, NULL
);
531 gdk_window_destroy (pizza
->bin_window
);
532 pizza
->bin_window
= NULL
;
534 if (pizza_parent_class
->unrealize
)
535 pizza_parent_class
->unrealize(widget
);
539 gtk_pizza_size_request (GtkWidget
*widget
,
540 GtkRequisition
*requisition
)
543 GtkPizzaChild
*child
;
545 GtkRequisition child_requisition
;
547 g_return_if_fail (widget
!= NULL
);
548 g_return_if_fail (GTK_IS_PIZZA (widget
));
549 g_return_if_fail (requisition
!= NULL
);
551 pizza
= GTK_PIZZA (widget
);
553 children
= pizza
->children
;
556 child
= children
->data
;
557 children
= children
->next
;
559 if (GTK_WIDGET_VISIBLE (child
->widget
))
561 gtk_widget_size_request (child
->widget
, &child_requisition
);
565 /* request very little, I'm not sure if requesting nothing
566 will always have positive effects on stability... */
567 requisition
->width
= 2;
568 requisition
->height
= 2;
572 gtk_pizza_size_allocate (GtkWidget
*widget
,
573 GtkAllocation
*allocation
)
578 GtkPizzaChild
*child
;
581 g_return_if_fail (widget
!= NULL
);
582 g_return_if_fail (GTK_IS_PIZZA(widget
));
583 g_return_if_fail (allocation
!= NULL
);
585 pizza
= GTK_PIZZA (widget
);
587 widget
->allocation
= *allocation
;
589 if (pizza
->shadow_type
== GTK_MYSHADOW_NONE
)
592 if (pizza
->shadow_type
== GTK_MYSHADOW_THIN
)
597 x
= allocation
->x
+ border
;
598 y
= allocation
->y
+ border
;
599 w
= allocation
->width
- border
*2;
600 h
= allocation
->height
- border
*2;
606 if (GTK_WIDGET_REALIZED (widget
))
608 gdk_window_move_resize( widget
->window
, x
, y
, w
, h
);
609 gdk_window_move_resize( pizza
->bin_window
, 0, 0, w
, h
);
612 children
= pizza
->children
;
615 child
= children
->data
;
616 children
= children
->next
;
618 gtk_pizza_allocate_child (pizza
, child
);
623 gtk_pizza_expose (GtkWidget
*widget
,
624 GdkEventExpose
*event
)
628 g_return_val_if_fail (widget
!= NULL
, FALSE
);
629 g_return_val_if_fail (GTK_IS_PIZZA (widget
), FALSE
);
630 g_return_val_if_fail (event
!= NULL
, FALSE
);
632 pizza
= (GtkPizza
*)widget
;
634 if (event
->window
!= pizza
->bin_window
)
637 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 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 */