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
->children
= NULL
;
190 pizza
->bin_window
= NULL
;
192 pizza
->m_xoffset
= 0;
193 pizza
->m_yoffset
= 0;
201 pizza
= g_object_new (gtk_pizza_get_type (), NULL
);
203 return GTK_WIDGET (pizza
);
206 gint
gtk_pizza_get_xoffset (GtkPizza
*pizza
)
208 g_return_val_if_fail ( (pizza
!= NULL
), -1 );
209 g_return_val_if_fail ( (GTK_IS_PIZZA (pizza
)), -1 );
211 return pizza
->m_xoffset
;
214 gint
gtk_pizza_get_yoffset (GtkPizza
*pizza
)
216 g_return_val_if_fail ( (pizza
!= NULL
), -1 );
217 g_return_val_if_fail ( (GTK_IS_PIZZA (pizza
)), -1 );
219 return pizza
->m_yoffset
;
222 void gtk_pizza_set_xoffset (GtkPizza
*pizza
, gint xoffset
)
224 g_return_if_fail (pizza
!= NULL
);
225 g_return_if_fail (GTK_IS_PIZZA (pizza
));
227 pizza
->m_xoffset
= xoffset
;
231 void gtk_pizza_set_yoffset (GtkPizza
*pizza
, gint yoffset
)
233 g_return_if_fail (pizza
!= NULL
);
234 g_return_if_fail (GTK_IS_PIZZA (pizza
));
236 pizza
->m_xoffset
= yoffset
;
240 gint
gtk_pizza_get_rtl_offset (GtkPizza
*pizza
)
244 g_return_val_if_fail ( (pizza
!= NULL
), 0 );
245 g_return_val_if_fail ( (GTK_IS_PIZZA (pizza
)), 0 );
247 if (!pizza
->bin_window
) return 0;
249 border
= pizza
->container
.border_width
;
251 return GTK_WIDGET(pizza
)->allocation
.width
- border
*2;
256 gtk_pizza_scroll_set_adjustments (GtkPizza
*pizza
,
260 /* We handle scrolling in the wxScrolledWindow, not here. */
264 gtk_pizza_put (GtkPizza
*pizza
,
271 GtkPizzaChild
*child_info
;
273 g_return_if_fail (pizza
!= NULL
);
274 g_return_if_fail (GTK_IS_PIZZA (pizza
));
275 g_return_if_fail (widget
!= NULL
);
277 child_info
= g_new (GtkPizzaChild
, 1);
279 child_info
->widget
= widget
;
282 child_info
->width
= width
;
283 child_info
->height
= height
;
285 pizza
->children
= g_list_append (pizza
->children
, child_info
);
287 if (GTK_WIDGET_REALIZED (pizza
))
288 gtk_widget_set_parent_window (widget
, pizza
->bin_window
);
290 gtk_widget_set_parent (widget
, GTK_WIDGET (pizza
));
292 if (GTK_WIDGET_REALIZED (pizza
))
293 gtk_pizza_allocate_child (pizza
, child_info
);
297 gtk_pizza_set_size (GtkPizza
*pizza
,
304 GtkPizzaChild
*child
;
307 g_return_if_fail (pizza
!= NULL
);
308 g_return_if_fail (GTK_IS_PIZZA (pizza
));
309 g_return_if_fail (widget
!= NULL
);
311 #ifndef WX_WARN_ILLEGAL_SETSIZE
312 /* this really shouldn't happen -- but it does, a lot, right now and we
313 can't pass negative values to gtk_widget_set_size_request() without getting
314 a warning printed out, so filter them out here */
321 children
= pizza
->children
;
324 child
= children
->data
;
325 children
= children
->next
;
327 if (child
->widget
== widget
)
329 if ((child
->x
== x
) &&
331 (child
->width
== width
) &&
332 (child
->height
== height
)) return;
336 child
->width
= width
;
337 child
->height
= height
;
339 gtk_widget_set_size_request (widget
, width
, height
);
347 gtk_pizza_map (GtkWidget
*widget
)
350 GtkPizzaChild
*child
;
353 g_return_if_fail (widget
!= NULL
);
354 g_return_if_fail (GTK_IS_PIZZA (widget
));
356 GTK_WIDGET_SET_FLAGS (widget
, GTK_MAPPED
);
357 pizza
= GTK_PIZZA (widget
);
359 children
= pizza
->children
;
362 child
= children
->data
;
363 children
= children
->next
;
365 if ( GTK_WIDGET_VISIBLE (child
->widget
) &&
366 !GTK_WIDGET_MAPPED (child
->widget
) )
368 gtk_widget_map (child
->widget
);
372 gdk_window_show (widget
->window
);
373 gdk_window_show (pizza
->bin_window
);
377 gtk_pizza_realize (GtkWidget
*widget
)
380 GdkWindowAttr attributes
;
381 gint attributes_mask
;
382 GtkPizzaChild
*child
;
386 g_return_if_fail (widget
!= NULL
);
387 g_return_if_fail (GTK_IS_PIZZA (widget
));
389 pizza
= GTK_PIZZA (widget
);
390 GTK_WIDGET_SET_FLAGS (widget
, GTK_REALIZED
);
392 attributes
.window_type
= GDK_WINDOW_CHILD
;
394 attributes
.x
= widget
->allocation
.x
;
395 attributes
.y
= widget
->allocation
.y
;
396 attributes
.width
= widget
->allocation
.width
;
397 attributes
.height
= widget
->allocation
.height
;
399 border
= pizza
->container
.border_width
;
400 attributes
.x
+= border
;
401 attributes
.y
+= border
;
402 attributes
.width
-= 2 * border
;
403 attributes
.height
-= 2 * border
;
406 if (attributes
.width
< 2) attributes
.width
= 2;
407 if (attributes
.height
< 2) attributes
.height
= 2;
409 attributes
.wclass
= GDK_INPUT_OUTPUT
;
410 attributes
.visual
= gtk_widget_get_visual (widget
);
411 attributes
.colormap
= gtk_widget_get_colormap (widget
);
412 attributes
.event_mask
= GDK_VISIBILITY_NOTIFY_MASK
;
413 attributes_mask
= GDK_WA_X
| GDK_WA_Y
| GDK_WA_VISUAL
| GDK_WA_COLORMAP
;
415 widget
->window
= gdk_window_new(gtk_widget_get_parent_window (widget
),
416 &attributes
, attributes_mask
);
417 gdk_window_set_user_data (widget
->window
, widget
);
422 attributes
.event_mask
= gtk_widget_get_events (widget
);
423 attributes
.event_mask
|= GDK_EXPOSURE_MASK
|
425 GDK_POINTER_MOTION_MASK
|
426 GDK_POINTER_MOTION_HINT_MASK
|
427 GDK_BUTTON_MOTION_MASK
|
428 GDK_BUTTON1_MOTION_MASK
|
429 GDK_BUTTON2_MOTION_MASK
|
430 GDK_BUTTON3_MOTION_MASK
|
431 GDK_BUTTON_PRESS_MASK
|
432 GDK_BUTTON_RELEASE_MASK
|
434 GDK_KEY_RELEASE_MASK
|
435 GDK_ENTER_NOTIFY_MASK
|
436 GDK_LEAVE_NOTIFY_MASK
|
437 GDK_FOCUS_CHANGE_MASK
;
439 pizza
->bin_window
= gdk_window_new(widget
->window
,
440 &attributes
, attributes_mask
);
441 gdk_window_set_user_data (pizza
->bin_window
, widget
);
443 widget
->style
= gtk_style_attach (widget
->style
, widget
->window
);
444 gtk_style_set_background (widget
->style
, widget
->window
, GTK_STATE_NORMAL
);
445 gtk_style_set_background (widget
->style
, pizza
->bin_window
, GTK_STATE_NORMAL
);
448 gdk_window_set_back_pixmap( widget->window, NULL, FALSE );
449 gdk_window_set_back_pixmap( pizza->bin_window, NULL, FALSE );
452 /* cannot be done before realisation */
453 children
= pizza
->children
;
456 child
= children
->data
;
457 children
= children
->next
;
459 gtk_widget_set_parent_window (child
->widget
, pizza
->bin_window
);
464 gtk_pizza_unrealize (GtkWidget
*widget
)
468 g_return_if_fail (widget
!= NULL
);
469 g_return_if_fail (GTK_IS_PIZZA (widget
));
471 pizza
= GTK_PIZZA (widget
);
473 gdk_window_set_user_data (pizza
->bin_window
, NULL
);
474 gdk_window_destroy (pizza
->bin_window
);
475 pizza
->bin_window
= NULL
;
477 if (pizza_parent_class
->unrealize
)
478 pizza_parent_class
->unrealize(widget
);
482 gtk_pizza_size_request (GtkWidget
*widget
,
483 GtkRequisition
*requisition
)
486 GtkPizzaChild
*child
;
488 GtkRequisition child_requisition
;
490 g_return_if_fail (widget
!= NULL
);
491 g_return_if_fail (GTK_IS_PIZZA (widget
));
492 g_return_if_fail (requisition
!= NULL
);
494 pizza
= GTK_PIZZA (widget
);
496 children
= pizza
->children
;
499 child
= children
->data
;
500 children
= children
->next
;
502 if (GTK_WIDGET_VISIBLE (child
->widget
))
504 gtk_widget_size_request (child
->widget
, &child_requisition
);
508 /* request very little, I'm not sure if requesting nothing
509 will always have positive effects on stability... */
510 requisition
->width
= 2;
511 requisition
->height
= 2;
515 gtk_pizza_size_allocate (GtkWidget
*widget
,
516 GtkAllocation
*allocation
)
521 GtkPizzaChild
*child
;
523 gboolean only_resize
;
525 g_return_if_fail (widget
!= NULL
);
526 g_return_if_fail (GTK_IS_PIZZA(widget
));
527 g_return_if_fail (allocation
!= NULL
);
529 pizza
= GTK_PIZZA (widget
);
531 only_resize
= ((widget
->allocation
.x
== allocation
->x
) &&
532 (widget
->allocation
.y
== allocation
->y
));
533 widget
->allocation
= *allocation
;
535 border
= pizza
->container
.border_width
;
537 x
= allocation
->x
+ border
;
538 y
= allocation
->y
+ border
;
539 w
= allocation
->width
- border
*2;
540 h
= allocation
->height
- border
*2;
546 if (GTK_WIDGET_REALIZED (widget
))
549 gdk_window_resize( widget
->window
, w
, h
);
551 gdk_window_move_resize( widget
->window
, x
, y
, w
, h
);
553 gdk_window_resize( pizza
->bin_window
, w
, h
);
556 children
= pizza
->children
;
559 child
= children
->data
;
560 children
= children
->next
;
562 gtk_pizza_allocate_child (pizza
, child
);
567 gtk_pizza_expose (GtkWidget
*widget
,
568 GdkEventExpose
*event
)
572 g_return_val_if_fail (widget
!= NULL
, FALSE
);
573 g_return_val_if_fail (GTK_IS_PIZZA (widget
), FALSE
);
574 g_return_val_if_fail (event
!= NULL
, FALSE
);
576 pizza
= (GtkPizza
*)widget
;
578 if (event
->window
!= pizza
->bin_window
)
581 pizza_parent_class
->expose_event(widget
, event
);
587 gtk_pizza_style_set(GtkWidget
*widget
, GtkStyle
*previous_style
)
589 if (GTK_WIDGET_REALIZED(widget
))
591 gtk_style_set_background(widget
->style
, widget
->window
, GTK_STATE_NORMAL
);
592 gtk_style_set_background(widget
->style
, GTK_PIZZA(widget
)->bin_window
, GTK_STATE_NORMAL
);
595 pizza_parent_class
->style_set(widget
, previous_style
);
599 gtk_pizza_add (GtkContainer
*container
,
602 g_return_if_fail (container
!= NULL
);
603 g_return_if_fail (GTK_IS_PIZZA (container
));
604 g_return_if_fail (widget
!= NULL
);
606 gtk_pizza_put (GTK_PIZZA (container
), widget
, 0, 0, 20, 20 );
610 gtk_pizza_remove (GtkContainer
*container
,
614 GtkPizzaChild
*child
;
617 g_return_if_fail (container
!= NULL
);
618 g_return_if_fail (GTK_IS_PIZZA (container
));
619 g_return_if_fail (widget
!= NULL
);
621 pizza
= GTK_PIZZA (container
);
623 children
= pizza
->children
;
626 child
= children
->data
;
628 if (child
->widget
== widget
)
630 gtk_widget_unparent (widget
);
632 /* security checks */
633 g_return_if_fail (GTK_IS_WIDGET (widget
));
635 pizza
->children
= g_list_remove_link (pizza
->children
, children
);
636 g_list_free (children
);
639 /* security checks */
640 g_return_if_fail (GTK_IS_WIDGET (widget
));
645 children
= children
->next
;
650 gtk_pizza_forall (GtkContainer
*container
,
651 gboolean include_internals
,
652 GtkCallback callback
,
653 gpointer callback_data
)
656 GtkPizzaChild
*child
;
659 g_return_if_fail (container
!= NULL
);
660 g_return_if_fail (GTK_IS_PIZZA (container
));
661 g_return_if_fail (callback
!= (GtkCallback
)NULL
);
663 pizza
= GTK_PIZZA (container
);
665 children
= pizza
->children
;
668 child
= children
->data
;
669 children
= children
->next
;
671 (* callback
) (child
->widget
, callback_data
);
676 gtk_pizza_allocate_child (GtkPizza
*pizza
,
677 GtkPizzaChild
*child
)
679 GtkAllocation allocation
;
680 GtkRequisition requisition
;
682 allocation
.x
= child
->x
- pizza
->m_xoffset
;
683 allocation
.y
= child
->y
- pizza
->m_yoffset
;
684 gtk_widget_get_child_requisition (child
->widget
, &requisition
);
685 allocation
.width
= requisition
.width
;
686 allocation
.height
= requisition
.height
;
688 if (gtk_widget_get_direction( GTK_WIDGET(pizza
) ) == GTK_TEXT_DIR_RTL
)
690 /* reverse horizontal placement */
693 offset
= GTK_WIDGET(pizza
)->allocation
.width
;
694 border
= pizza
->container
.border_width
;
697 allocation
.x
= offset
- child
->x
- allocation
.width
+ pizza
->m_xoffset
;
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
->m_xoffset
+= dx
;
754 pizza
->m_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 */