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
)
245 g_return_val_if_fail ( (pizza
!= NULL
), 0 );
246 g_return_val_if_fail ( (GTK_IS_PIZZA (pizza
)), 0 );
248 if (!pizza
->bin_window
) return 0;
250 gdk_window_get_geometry( pizza
->bin_window
, NULL
, NULL
, &width
, NULL
, NULL
);
252 border
= pizza
->container
.border_width
;
254 return width
-border
*2;
259 gtk_pizza_scroll_set_adjustments (GtkPizza
*pizza
,
263 /* We handle scrolling in the wxScrolledWindow, not here. */
267 gtk_pizza_put (GtkPizza
*pizza
,
274 GtkPizzaChild
*child_info
;
276 g_return_if_fail (pizza
!= NULL
);
277 g_return_if_fail (GTK_IS_PIZZA (pizza
));
278 g_return_if_fail (widget
!= NULL
);
280 child_info
= g_new (GtkPizzaChild
, 1);
282 child_info
->widget
= widget
;
285 child_info
->width
= width
;
286 child_info
->height
= height
;
288 pizza
->children
= g_list_append (pizza
->children
, child_info
);
290 if (GTK_WIDGET_REALIZED (pizza
))
291 gtk_widget_set_parent_window (widget
, pizza
->bin_window
);
293 gtk_widget_set_parent (widget
, GTK_WIDGET (pizza
));
295 if (GTK_WIDGET_REALIZED (pizza
))
296 gtk_pizza_allocate_child (pizza
, child_info
);
300 gtk_pizza_set_size (GtkPizza
*pizza
,
307 GtkPizzaChild
*child
;
310 g_return_if_fail (pizza
!= NULL
);
311 g_return_if_fail (GTK_IS_PIZZA (pizza
));
312 g_return_if_fail (widget
!= NULL
);
314 #ifndef WX_WARN_ILLEGAL_SETSIZE
315 /* this really shouldn't happen -- but it does, a lot, right now and we
316 can't pass negative values to gtk_widget_set_size_request() without getting
317 a warning printed out, so filter them out here */
324 children
= pizza
->children
;
327 child
= children
->data
;
328 children
= children
->next
;
330 if (child
->widget
== widget
)
332 if ((child
->x
== x
) &&
334 (child
->width
== width
) &&
335 (child
->height
== height
)) return;
339 child
->width
= width
;
340 child
->height
= height
;
342 gtk_widget_set_size_request (widget
, width
, height
);
350 gtk_pizza_map (GtkWidget
*widget
)
353 GtkPizzaChild
*child
;
356 g_return_if_fail (widget
!= NULL
);
357 g_return_if_fail (GTK_IS_PIZZA (widget
));
359 GTK_WIDGET_SET_FLAGS (widget
, GTK_MAPPED
);
360 pizza
= GTK_PIZZA (widget
);
362 children
= pizza
->children
;
365 child
= children
->data
;
366 children
= children
->next
;
368 if ( GTK_WIDGET_VISIBLE (child
->widget
) &&
369 !GTK_WIDGET_MAPPED (child
->widget
) )
371 gtk_widget_map (child
->widget
);
375 gdk_window_show (widget
->window
);
376 gdk_window_show (pizza
->bin_window
);
380 gtk_pizza_realize (GtkWidget
*widget
)
383 GdkWindowAttr attributes
;
384 gint attributes_mask
;
385 GtkPizzaChild
*child
;
389 g_return_if_fail (widget
!= NULL
);
390 g_return_if_fail (GTK_IS_PIZZA (widget
));
392 pizza
= GTK_PIZZA (widget
);
393 GTK_WIDGET_SET_FLAGS (widget
, GTK_REALIZED
);
395 attributes
.window_type
= GDK_WINDOW_CHILD
;
397 attributes
.x
= widget
->allocation
.x
;
398 attributes
.y
= widget
->allocation
.y
;
399 attributes
.width
= widget
->allocation
.width
;
400 attributes
.height
= widget
->allocation
.height
;
402 border
= pizza
->container
.border_width
;
403 attributes
.x
+= border
;
404 attributes
.y
+= border
;
405 attributes
.width
-= 2 * border
;
406 attributes
.height
-= 2 * border
;
409 if (attributes
.width
< 2) attributes
.width
= 2;
410 if (attributes
.height
< 2) attributes
.height
= 2;
412 attributes
.wclass
= GDK_INPUT_OUTPUT
;
413 attributes
.visual
= gtk_widget_get_visual (widget
);
414 attributes
.colormap
= gtk_widget_get_colormap (widget
);
415 attributes
.event_mask
= GDK_VISIBILITY_NOTIFY_MASK
;
416 attributes_mask
= GDK_WA_X
| GDK_WA_Y
| GDK_WA_VISUAL
| GDK_WA_COLORMAP
;
418 widget
->window
= gdk_window_new(gtk_widget_get_parent_window (widget
),
419 &attributes
, attributes_mask
);
420 gdk_window_set_user_data (widget
->window
, widget
);
425 attributes
.event_mask
= gtk_widget_get_events (widget
);
426 attributes
.event_mask
|= GDK_EXPOSURE_MASK
|
428 GDK_POINTER_MOTION_MASK
|
429 GDK_POINTER_MOTION_HINT_MASK
|
430 GDK_BUTTON_MOTION_MASK
|
431 GDK_BUTTON1_MOTION_MASK
|
432 GDK_BUTTON2_MOTION_MASK
|
433 GDK_BUTTON3_MOTION_MASK
|
434 GDK_BUTTON_PRESS_MASK
|
435 GDK_BUTTON_RELEASE_MASK
|
437 GDK_KEY_RELEASE_MASK
|
438 GDK_ENTER_NOTIFY_MASK
|
439 GDK_LEAVE_NOTIFY_MASK
|
440 GDK_FOCUS_CHANGE_MASK
;
442 pizza
->bin_window
= gdk_window_new(widget
->window
,
443 &attributes
, attributes_mask
);
444 gdk_window_set_user_data (pizza
->bin_window
, widget
);
446 widget
->style
= gtk_style_attach (widget
->style
, widget
->window
);
447 gtk_style_set_background (widget
->style
, widget
->window
, GTK_STATE_NORMAL
);
448 gtk_style_set_background (widget
->style
, pizza
->bin_window
, GTK_STATE_NORMAL
);
451 gdk_window_set_back_pixmap( widget->window, NULL, FALSE );
452 gdk_window_set_back_pixmap( pizza->bin_window, NULL, FALSE );
455 /* cannot be done before realisation */
456 children
= pizza
->children
;
459 child
= children
->data
;
460 children
= children
->next
;
462 gtk_widget_set_parent_window (child
->widget
, pizza
->bin_window
);
467 gtk_pizza_unrealize (GtkWidget
*widget
)
471 g_return_if_fail (widget
!= NULL
);
472 g_return_if_fail (GTK_IS_PIZZA (widget
));
474 pizza
= GTK_PIZZA (widget
);
476 gdk_window_set_user_data (pizza
->bin_window
, NULL
);
477 gdk_window_destroy (pizza
->bin_window
);
478 pizza
->bin_window
= NULL
;
480 if (pizza_parent_class
->unrealize
)
481 pizza_parent_class
->unrealize(widget
);
485 gtk_pizza_size_request (GtkWidget
*widget
,
486 GtkRequisition
*requisition
)
489 GtkPizzaChild
*child
;
491 GtkRequisition child_requisition
;
493 g_return_if_fail (widget
!= NULL
);
494 g_return_if_fail (GTK_IS_PIZZA (widget
));
495 g_return_if_fail (requisition
!= NULL
);
497 pizza
= GTK_PIZZA (widget
);
499 children
= pizza
->children
;
502 child
= children
->data
;
503 children
= children
->next
;
505 if (GTK_WIDGET_VISIBLE (child
->widget
))
507 gtk_widget_size_request (child
->widget
, &child_requisition
);
511 /* request very little, I'm not sure if requesting nothing
512 will always have positive effects on stability... */
513 requisition
->width
= 2;
514 requisition
->height
= 2;
518 gtk_pizza_size_allocate (GtkWidget
*widget
,
519 GtkAllocation
*allocation
)
524 GtkPizzaChild
*child
;
526 gboolean only_resize
;
528 g_return_if_fail (widget
!= NULL
);
529 g_return_if_fail (GTK_IS_PIZZA(widget
));
530 g_return_if_fail (allocation
!= NULL
);
532 pizza
= GTK_PIZZA (widget
);
534 only_resize
= ((widget
->allocation
.x
== allocation
->x
) &&
535 (widget
->allocation
.y
== allocation
->y
));
536 widget
->allocation
= *allocation
;
538 border
= pizza
->container
.border_width
;
540 x
= allocation
->x
+ border
;
541 y
= allocation
->y
+ border
;
542 w
= allocation
->width
- border
*2;
543 h
= allocation
->height
- border
*2;
549 if (GTK_WIDGET_REALIZED (widget
))
552 gdk_window_resize( widget
->window
, w
, h
);
554 gdk_window_move_resize( widget
->window
, x
, y
, w
, h
);
556 gdk_window_resize( pizza
->bin_window
, w
, h
);
559 children
= pizza
->children
;
562 child
= children
->data
;
563 children
= children
->next
;
565 gtk_pizza_allocate_child (pizza
, child
);
570 gtk_pizza_expose (GtkWidget
*widget
,
571 GdkEventExpose
*event
)
575 g_return_val_if_fail (widget
!= NULL
, FALSE
);
576 g_return_val_if_fail (GTK_IS_PIZZA (widget
), FALSE
);
577 g_return_val_if_fail (event
!= NULL
, FALSE
);
579 pizza
= (GtkPizza
*)widget
;
581 if (event
->window
!= pizza
->bin_window
)
584 pizza_parent_class
->expose_event(widget
, event
);
590 gtk_pizza_style_set(GtkWidget
*widget
, GtkStyle
*previous_style
)
592 if (GTK_WIDGET_REALIZED(widget
))
594 gtk_style_set_background(widget
->style
, widget
->window
, GTK_STATE_NORMAL
);
595 gtk_style_set_background(widget
->style
, GTK_PIZZA(widget
)->bin_window
, GTK_STATE_NORMAL
);
598 pizza_parent_class
->style_set(widget
, previous_style
);
602 gtk_pizza_add (GtkContainer
*container
,
605 g_return_if_fail (container
!= NULL
);
606 g_return_if_fail (GTK_IS_PIZZA (container
));
607 g_return_if_fail (widget
!= NULL
);
609 gtk_pizza_put (GTK_PIZZA (container
), widget
, 0, 0, 20, 20 );
613 gtk_pizza_remove (GtkContainer
*container
,
617 GtkPizzaChild
*child
;
620 g_return_if_fail (container
!= NULL
);
621 g_return_if_fail (GTK_IS_PIZZA (container
));
622 g_return_if_fail (widget
!= NULL
);
624 pizza
= GTK_PIZZA (container
);
626 children
= pizza
->children
;
629 child
= children
->data
;
631 if (child
->widget
== widget
)
633 gtk_widget_unparent (widget
);
635 /* security checks */
636 g_return_if_fail (GTK_IS_WIDGET (widget
));
638 pizza
->children
= g_list_remove_link (pizza
->children
, children
);
639 g_list_free (children
);
642 /* security checks */
643 g_return_if_fail (GTK_IS_WIDGET (widget
));
648 children
= children
->next
;
653 gtk_pizza_forall (GtkContainer
*container
,
654 gboolean include_internals
,
655 GtkCallback callback
,
656 gpointer callback_data
)
659 GtkPizzaChild
*child
;
662 g_return_if_fail (container
!= NULL
);
663 g_return_if_fail (GTK_IS_PIZZA (container
));
664 g_return_if_fail (callback
!= (GtkCallback
)NULL
);
666 pizza
= GTK_PIZZA (container
);
668 children
= pizza
->children
;
671 child
= children
->data
;
672 children
= children
->next
;
674 (* callback
) (child
->widget
, callback_data
);
679 gtk_pizza_allocate_child (GtkPizza
*pizza
,
680 GtkPizzaChild
*child
)
682 GtkAllocation allocation
;
683 GtkRequisition requisition
;
685 allocation
.x
= child
->x
- pizza
->m_xoffset
;
686 allocation
.y
= child
->y
- pizza
->m_yoffset
;
687 gtk_widget_get_child_requisition (child
->widget
, &requisition
);
688 allocation
.width
= requisition
.width
;
689 allocation
.height
= requisition
.height
;
691 if (gtk_widget_get_direction( GTK_WIDGET(pizza
) ) == GTK_TEXT_DIR_RTL
)
693 /* reverse horizontal placement */
696 offset
= GTK_WIDGET(pizza
)->allocation
.width
;
697 border
= pizza
->container
.border_width
;
700 allocation
.x
= offset
- child
->x
- allocation
.width
- pizza
->m_xoffset
;
703 gtk_widget_size_allocate (child
->widget
, &allocation
);
707 gtk_pizza_adjust_allocations_recurse (GtkWidget
*widget
,
710 GtkPizzaAdjData
*data
= cb_data
;
712 widget
->allocation
.x
+= data
->dx
;
713 widget
->allocation
.y
+= data
->dy
;
715 if (GTK_WIDGET_NO_WINDOW (widget
) && GTK_IS_CONTAINER (widget
))
717 gtk_container_forall (GTK_CONTAINER (widget
),
718 gtk_pizza_adjust_allocations_recurse
,
724 gtk_pizza_adjust_allocations (GtkPizza
*pizza
,
729 GtkPizzaAdjData data
;
734 tmp_list
= pizza
->children
;
737 GtkPizzaChild
*child
= tmp_list
->data
;
738 tmp_list
= tmp_list
->next
;
740 child
->widget
->allocation
.x
+= dx
;
741 child
->widget
->allocation
.y
+= dy
;
743 if (GTK_WIDGET_NO_WINDOW (child
->widget
) &&
744 GTK_IS_CONTAINER (child
->widget
))
746 gtk_container_forall (GTK_CONTAINER (child
->widget
),
747 gtk_pizza_adjust_allocations_recurse
,
754 gtk_pizza_scroll (GtkPizza
*pizza
, gint dx
, gint dy
)
756 pizza
->m_xoffset
+= dx
;
757 pizza
->m_yoffset
+= dy
;
759 gtk_pizza_adjust_allocations (pizza
, -dx
, -dy
);
761 if (pizza
->bin_window
)
762 gdk_window_scroll( pizza
->bin_window
, -dx
, -dy
);
767 #endif /* __cplusplus */