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;
205 pizza
= g_object_new (gtk_pizza_get_type (), NULL
);
207 return GTK_WIDGET (pizza
);
210 gint
gtk_pizza_get_xoffset (GtkPizza
*pizza
)
212 g_return_val_if_fail ( (pizza
!= NULL
), -1 );
213 g_return_val_if_fail ( (GTK_IS_PIZZA (pizza
)), -1 );
215 return pizza
->m_xoffset
;
218 gint
gtk_pizza_get_yoffset (GtkPizza
*pizza
)
220 g_return_val_if_fail ( (pizza
!= NULL
), -1 );
221 g_return_val_if_fail ( (GTK_IS_PIZZA (pizza
)), -1 );
223 return pizza
->m_yoffset
;
226 void gtk_pizza_set_xoffset (GtkPizza
*pizza
, gint xoffset
)
228 g_return_if_fail (pizza
!= NULL
);
229 g_return_if_fail (GTK_IS_PIZZA (pizza
));
231 pizza
->m_xoffset
= xoffset
;
235 void gtk_pizza_set_yoffset (GtkPizza
*pizza
, gint yoffset
)
237 g_return_if_fail (pizza
!= NULL
);
238 g_return_if_fail (GTK_IS_PIZZA (pizza
));
240 pizza
->m_xoffset
= yoffset
;
244 gint
gtk_pizza_get_rtl_offset (GtkPizza
*pizza
)
248 g_return_val_if_fail ( (pizza
!= NULL
), 0 );
249 g_return_val_if_fail ( (GTK_IS_PIZZA (pizza
)), 0 );
251 if (!pizza
->bin_window
) return 0;
253 gdk_window_get_geometry( pizza
->bin_window
, NULL
, NULL
, &width
, NULL
, NULL
);
260 gtk_pizza_scroll_set_adjustments (GtkPizza
*pizza
,
264 /* We handle scrolling in the wxScrolledWindow, not here. */
268 gtk_pizza_set_shadow_type (GtkPizza
*pizza
,
269 GtkMyShadowType type
)
271 g_return_if_fail (pizza
!= NULL
);
272 g_return_if_fail (GTK_IS_PIZZA (pizza
));
274 if (pizza
->shadow_type
!= type
)
276 pizza
->shadow_type
= type
;
278 if (GTK_WIDGET_VISIBLE (pizza
))
280 gtk_widget_size_allocate (GTK_WIDGET (pizza
), &(GTK_WIDGET (pizza
)->allocation
));
281 gtk_widget_queue_draw (GTK_WIDGET (pizza
));
287 gtk_pizza_put (GtkPizza
*pizza
,
294 GtkPizzaChild
*child_info
;
296 g_return_if_fail (pizza
!= NULL
);
297 g_return_if_fail (GTK_IS_PIZZA (pizza
));
298 g_return_if_fail (widget
!= NULL
);
300 if (gtk_widget_get_direction( GTK_WIDGET(pizza
) ) == GTK_TEXT_DIR_RTL
)
302 // reverse horizontal placement
303 x
= pizza
->m_width
- x
- width
;
306 child_info
= g_new (GtkPizzaChild
, 1);
308 child_info
->widget
= widget
;
311 child_info
->width
= width
;
312 child_info
->height
= height
;
314 if (GTK_IS_PIZZA(widget
))
315 GTK_PIZZA(widget
)->m_width
= width
;
317 pizza
->children
= g_list_append (pizza
->children
, child_info
);
319 if (GTK_WIDGET_REALIZED (pizza
))
320 gtk_widget_set_parent_window (widget
, pizza
->bin_window
);
322 gtk_widget_set_parent (widget
, GTK_WIDGET (pizza
));
324 gtk_widget_set_size_request (widget
, width
, height
);
328 gtk_pizza_set_size (GtkPizza
*pizza
,
335 GtkPizzaChild
*child
;
338 g_return_if_fail (pizza
!= NULL
);
339 g_return_if_fail (GTK_IS_PIZZA (pizza
));
340 g_return_if_fail (widget
!= NULL
);
342 #ifndef WX_WARN_ILLEGAL_SETSIZE
343 /* this really shouldn't happen -- but it does, a lot, right now and we
344 can't pass negative values to gtk_widget_set_size_request() without getting
345 a warning printed out, so filter them out here */
352 children
= pizza
->children
;
355 child
= children
->data
;
356 children
= children
->next
;
358 if (child
->widget
== widget
)
361 if (gtk_widget_get_direction( GTK_WIDGET(pizza
) ) == GTK_TEXT_DIR_RTL
)
363 // reverse horizontal placement
364 new_x
= pizza
->m_width
- new_x
- width
;
367 if ((child
->x
== new_x
) &&
369 (child
->width
== width
) &&
370 (child
->height
== height
)) return;
374 child
->width
= width
;
375 child
->height
= height
;
377 if (GTK_IS_PIZZA(widget
))
378 GTK_PIZZA(widget
)->m_width
= width
;
380 gtk_widget_set_size_request (widget
, width
, height
);
388 gtk_pizza_map (GtkWidget
*widget
)
391 GtkPizzaChild
*child
;
394 g_return_if_fail (widget
!= NULL
);
395 g_return_if_fail (GTK_IS_PIZZA (widget
));
397 GTK_WIDGET_SET_FLAGS (widget
, GTK_MAPPED
);
398 pizza
= GTK_PIZZA (widget
);
400 children
= pizza
->children
;
403 child
= children
->data
;
404 children
= children
->next
;
406 if ( GTK_WIDGET_VISIBLE (child
->widget
) &&
407 !GTK_WIDGET_MAPPED (child
->widget
) )
409 gtk_widget_map (child
->widget
);
413 gdk_window_show (widget
->window
);
414 gdk_window_show (pizza
->bin_window
);
418 gtk_pizza_realize (GtkWidget
*widget
)
421 GdkWindowAttr attributes
;
422 gint attributes_mask
;
423 GtkPizzaChild
*child
;
426 g_return_if_fail (widget
!= NULL
);
427 g_return_if_fail (GTK_IS_PIZZA (widget
));
429 pizza
= GTK_PIZZA (widget
);
430 GTK_WIDGET_SET_FLAGS (widget
, GTK_REALIZED
);
432 attributes
.window_type
= GDK_WINDOW_CHILD
;
434 attributes
.x
= widget
->allocation
.x
;
435 attributes
.y
= widget
->allocation
.y
;
436 attributes
.width
= widget
->allocation
.width
;
437 attributes
.height
= widget
->allocation
.height
;
439 #ifndef __WXUNIVERSAL__
440 if (pizza
->shadow_type
== GTK_MYSHADOW_NONE
)
442 /* no border, no changes to sizes */
444 else if (pizza
->shadow_type
== GTK_MYSHADOW_THIN
)
446 /* GTK_MYSHADOW_THIN == wxSIMPLE_BORDER */
449 attributes
.width
-= 2;
450 attributes
.height
-= 2;
454 /* GTK_MYSHADOW_IN == wxSUNKEN_BORDER */
455 /* GTK_MYSHADOW_OUT == wxRAISED_BORDER */
458 attributes
.width
-= 4;
459 attributes
.height
-= 4;
461 #endif /* __WXUNIVERSAL__ */
464 if (attributes
.width
< 2) attributes
.width
= 2;
465 if (attributes
.height
< 2) attributes
.height
= 2;
467 attributes
.wclass
= GDK_INPUT_OUTPUT
;
468 attributes
.visual
= gtk_widget_get_visual (widget
);
469 attributes
.colormap
= gtk_widget_get_colormap (widget
);
470 attributes
.event_mask
= GDK_VISIBILITY_NOTIFY_MASK
;
471 attributes_mask
= GDK_WA_X
| GDK_WA_Y
| GDK_WA_VISUAL
| GDK_WA_COLORMAP
;
473 widget
->window
= gdk_window_new(gtk_widget_get_parent_window (widget
),
474 &attributes
, attributes_mask
);
475 gdk_window_set_user_data (widget
->window
, widget
);
480 attributes
.event_mask
= gtk_widget_get_events (widget
);
481 attributes
.event_mask
|= GDK_EXPOSURE_MASK
|
483 GDK_POINTER_MOTION_MASK
|
484 GDK_POINTER_MOTION_HINT_MASK
|
485 GDK_BUTTON_MOTION_MASK
|
486 GDK_BUTTON1_MOTION_MASK
|
487 GDK_BUTTON2_MOTION_MASK
|
488 GDK_BUTTON3_MOTION_MASK
|
489 GDK_BUTTON_PRESS_MASK
|
490 GDK_BUTTON_RELEASE_MASK
|
492 GDK_KEY_RELEASE_MASK
|
493 GDK_ENTER_NOTIFY_MASK
|
494 GDK_LEAVE_NOTIFY_MASK
|
495 GDK_FOCUS_CHANGE_MASK
;
497 pizza
->bin_window
= gdk_window_new(widget
->window
,
498 &attributes
, attributes_mask
);
499 gdk_window_set_user_data (pizza
->bin_window
, widget
);
501 widget
->style
= gtk_style_attach (widget
->style
, widget
->window
);
502 gtk_style_set_background (widget
->style
, widget
->window
, GTK_STATE_NORMAL
);
503 gtk_style_set_background (widget
->style
, pizza
->bin_window
, GTK_STATE_NORMAL
);
506 gdk_window_set_back_pixmap( widget->window, NULL, FALSE );
507 gdk_window_set_back_pixmap( pizza->bin_window, NULL, FALSE );
510 /* cannot be done before realisation */
511 children
= pizza
->children
;
514 child
= children
->data
;
515 children
= children
->next
;
517 gtk_widget_set_parent_window (child
->widget
, pizza
->bin_window
);
522 gtk_pizza_unrealize (GtkWidget
*widget
)
526 g_return_if_fail (widget
!= NULL
);
527 g_return_if_fail (GTK_IS_PIZZA (widget
));
529 pizza
= GTK_PIZZA (widget
);
531 gdk_window_set_user_data (pizza
->bin_window
, NULL
);
532 gdk_window_destroy (pizza
->bin_window
);
533 pizza
->bin_window
= NULL
;
535 if (pizza_parent_class
->unrealize
)
536 pizza_parent_class
->unrealize(widget
);
540 gtk_pizza_size_request (GtkWidget
*widget
,
541 GtkRequisition
*requisition
)
544 GtkPizzaChild
*child
;
546 GtkRequisition child_requisition
;
548 g_return_if_fail (widget
!= NULL
);
549 g_return_if_fail (GTK_IS_PIZZA (widget
));
550 g_return_if_fail (requisition
!= NULL
);
552 pizza
= GTK_PIZZA (widget
);
554 children
= pizza
->children
;
557 child
= children
->data
;
558 children
= children
->next
;
560 if (GTK_WIDGET_VISIBLE (child
->widget
))
562 gtk_widget_size_request (child
->widget
, &child_requisition
);
566 /* request very little, I'm not sure if requesting nothing
567 will always have positive effects on stability... */
568 requisition
->width
= 2;
569 requisition
->height
= 2;
573 gtk_pizza_size_allocate (GtkWidget
*widget
,
574 GtkAllocation
*allocation
)
579 GtkPizzaChild
*child
;
582 g_return_if_fail (widget
!= NULL
);
583 g_return_if_fail (GTK_IS_PIZZA(widget
));
584 g_return_if_fail (allocation
!= NULL
);
586 pizza
= GTK_PIZZA (widget
);
588 widget
->allocation
= *allocation
;
590 if (pizza
->shadow_type
== GTK_MYSHADOW_NONE
)
593 if (pizza
->shadow_type
== GTK_MYSHADOW_THIN
)
598 x
= allocation
->x
+ border
;
599 y
= allocation
->y
+ border
;
600 w
= allocation
->width
- border
*2;
601 h
= allocation
->height
- border
*2;
607 if (GTK_WIDGET_REALIZED (widget
))
609 gdk_window_move_resize( widget
->window
, x
, y
, w
, h
);
610 gdk_window_move_resize( pizza
->bin_window
, 0, 0, w
, h
);
613 children
= pizza
->children
;
616 child
= children
->data
;
617 children
= children
->next
;
619 gtk_pizza_allocate_child (pizza
, child
);
624 gtk_pizza_expose (GtkWidget
*widget
,
625 GdkEventExpose
*event
)
629 g_return_val_if_fail (widget
!= NULL
, FALSE
);
630 g_return_val_if_fail (GTK_IS_PIZZA (widget
), FALSE
);
631 g_return_val_if_fail (event
!= NULL
, FALSE
);
633 pizza
= (GtkPizza
*)widget
;
635 if (event
->window
!= pizza
->bin_window
)
638 pizza_parent_class
->expose_event(widget
, event
);
644 gtk_pizza_style_set(GtkWidget
*widget
, GtkStyle
*previous_style
)
646 if (GTK_WIDGET_REALIZED(widget
))
648 gtk_style_set_background(widget
->style
, widget
->window
, GTK_STATE_NORMAL
);
649 gtk_style_set_background(widget
->style
, GTK_PIZZA(widget
)->bin_window
, GTK_STATE_NORMAL
);
652 pizza_parent_class
->style_set(widget
, previous_style
);
656 gtk_pizza_add (GtkContainer
*container
,
659 g_return_if_fail (container
!= NULL
);
660 g_return_if_fail (GTK_IS_PIZZA (container
));
661 g_return_if_fail (widget
!= NULL
);
663 gtk_pizza_put (GTK_PIZZA (container
), widget
, 0, 0, 20, 20 );
667 gtk_pizza_remove (GtkContainer
*container
,
671 GtkPizzaChild
*child
;
674 g_return_if_fail (container
!= NULL
);
675 g_return_if_fail (GTK_IS_PIZZA (container
));
676 g_return_if_fail (widget
!= NULL
);
678 pizza
= GTK_PIZZA (container
);
680 children
= pizza
->children
;
683 child
= children
->data
;
685 if (child
->widget
== widget
)
687 gtk_widget_unparent (widget
);
689 /* security checks */
690 g_return_if_fail (GTK_IS_WIDGET (widget
));
692 pizza
->children
= g_list_remove_link (pizza
->children
, children
);
693 g_list_free (children
);
696 /* security checks */
697 g_return_if_fail (GTK_IS_WIDGET (widget
));
702 children
= children
->next
;
707 gtk_pizza_forall (GtkContainer
*container
,
708 gboolean include_internals
,
709 GtkCallback callback
,
710 gpointer callback_data
)
713 GtkPizzaChild
*child
;
716 g_return_if_fail (container
!= NULL
);
717 g_return_if_fail (GTK_IS_PIZZA (container
));
718 g_return_if_fail (callback
!= (GtkCallback
)NULL
);
720 pizza
= GTK_PIZZA (container
);
722 children
= pizza
->children
;
725 child
= children
->data
;
726 children
= children
->next
;
728 (* callback
) (child
->widget
, callback_data
);
733 gtk_pizza_allocate_child (GtkPizza
*pizza
,
734 GtkPizzaChild
*child
)
736 GtkAllocation allocation
;
737 GtkRequisition requisition
;
739 allocation
.x
= child
->x
- pizza
->m_xoffset
;
740 allocation
.y
= child
->y
- pizza
->m_yoffset
;
741 gtk_widget_get_child_requisition (child
->widget
, &requisition
);
742 allocation
.width
= requisition
.width
;
743 allocation
.height
= requisition
.height
;
745 gtk_widget_size_allocate (child
->widget
, &allocation
);
749 gtk_pizza_adjust_allocations_recurse (GtkWidget
*widget
,
752 GtkPizzaAdjData
*data
= cb_data
;
754 widget
->allocation
.x
+= data
->dx
;
755 widget
->allocation
.y
+= data
->dy
;
757 if (GTK_WIDGET_NO_WINDOW (widget
) && GTK_IS_CONTAINER (widget
))
759 gtk_container_forall (GTK_CONTAINER (widget
),
760 gtk_pizza_adjust_allocations_recurse
,
766 gtk_pizza_adjust_allocations (GtkPizza
*pizza
,
771 GtkPizzaAdjData data
;
776 tmp_list
= pizza
->children
;
779 GtkPizzaChild
*child
= tmp_list
->data
;
780 tmp_list
= tmp_list
->next
;
782 child
->widget
->allocation
.x
+= dx
;
783 child
->widget
->allocation
.y
+= dy
;
785 if (GTK_WIDGET_NO_WINDOW (child
->widget
) &&
786 GTK_IS_CONTAINER (child
->widget
))
788 gtk_container_forall (GTK_CONTAINER (child
->widget
),
789 gtk_pizza_adjust_allocations_recurse
,
796 gtk_pizza_scroll (GtkPizza
*pizza
, gint dx
, gint dy
)
798 pizza
->m_xoffset
+= dx
;
799 pizza
->m_yoffset
+= dy
;
801 gtk_pizza_adjust_allocations (pizza
, -dx
, -dy
);
803 if (pizza
->bin_window
)
804 gdk_window_scroll( pizza
->bin_window
, -dx
, -dy
);
809 #endif /* __cplusplus */