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 void gtk_pizza_style_set (GtkWidget
*widget
,
44 GtkStyle
*previous_style
);
45 static void gtk_pizza_add (GtkContainer
*container
,
47 static void gtk_pizza_remove (GtkContainer
*container
,
49 static void gtk_pizza_forall (GtkContainer
*container
,
50 gboolean include_internals
,
52 gpointer callback_data
);
54 static void gtk_pizza_allocate_child (GtkPizza
*pizza
,
55 GtkPizzaChild
*child
);
56 static void gtk_pizza_adjust_allocations_recurse (GtkWidget
*widget
,
59 static GtkType
gtk_pizza_child_type (GtkContainer
*container
);
61 static void gtk_pizza_scroll_set_adjustments (GtkPizza
*pizza
,
65 static GtkWidgetClass
* pizza_parent_class
;
70 static GtkType pizza_type
= 0;
74 static const GTypeInfo pizza_info
=
76 sizeof (GtkPizzaClass
),
78 NULL
, /* base_finalize */
79 (GClassInitFunc
) gtk_pizza_class_init
,
80 NULL
, /* class_finalize */
81 NULL
, /* class_data */
84 (GInstanceInitFunc
) gtk_pizza_init
,
87 pizza_type
= g_type_register_static (GTK_TYPE_CONTAINER
, "GtkPizza", &pizza_info
, (GTypeFlags
)0);
93 /* Marshaller needed for set_scroll_adjustments signal,
94 generated with GLib-2.4.6 glib-genmarshal */
95 #define g_marshal_value_peek_object(v) g_value_get_object (v)
97 g_cclosure_user_marshal_VOID__OBJECT_OBJECT (GClosure
*closure
,
100 const GValue
*param_values
,
101 gpointer invocation_hint
,
102 gpointer marshal_data
)
104 typedef void (*GMarshalFunc_VOID__OBJECT_OBJECT
) (gpointer data1
,
108 register GMarshalFunc_VOID__OBJECT_OBJECT callback
;
109 register GCClosure
*cc
= (GCClosure
*) closure
;
110 register gpointer data1
, data2
;
112 g_return_if_fail (n_param_values
== 3);
114 if (G_CCLOSURE_SWAP_DATA (closure
))
116 data1
= closure
->data
;
117 data2
= g_value_peek_pointer (param_values
+ 0);
121 data1
= g_value_peek_pointer (param_values
+ 0);
122 data2
= closure
->data
;
124 callback
= (GMarshalFunc_VOID__OBJECT_OBJECT
) (marshal_data
? marshal_data
: cc
->callback
);
127 g_marshal_value_peek_object (param_values
+ 1),
128 g_marshal_value_peek_object (param_values
+ 2),
133 gtk_pizza_class_init (GtkPizzaClass
*klass
)
135 GtkObjectClass
*object_class
;
136 GtkWidgetClass
*widget_class
;
137 GtkContainerClass
*container_class
;
139 object_class
= (GtkObjectClass
*) klass
;
140 widget_class
= (GtkWidgetClass
*) klass
;
141 container_class
= (GtkContainerClass
*) klass
;
142 pizza_parent_class
= gtk_type_class (GTK_TYPE_CONTAINER
);
144 widget_class
->map
= gtk_pizza_map
;
145 widget_class
->realize
= gtk_pizza_realize
;
146 widget_class
->unrealize
= gtk_pizza_unrealize
;
147 widget_class
->size_request
= gtk_pizza_size_request
;
148 widget_class
->size_allocate
= gtk_pizza_size_allocate
;
149 widget_class
->style_set
= gtk_pizza_style_set
;
151 container_class
->add
= gtk_pizza_add
;
152 container_class
->remove
= gtk_pizza_remove
;
153 container_class
->forall
= gtk_pizza_forall
;
155 container_class
->child_type
= gtk_pizza_child_type
;
157 klass
->set_scroll_adjustments
= gtk_pizza_scroll_set_adjustments
;
159 widget_class
->set_scroll_adjustments_signal
=
161 "set_scroll_adjustments",
162 G_TYPE_FROM_CLASS(object_class
),
164 G_STRUCT_OFFSET(GtkPizzaClass
, set_scroll_adjustments
),
167 g_cclosure_user_marshal_VOID__OBJECT_OBJECT
,
171 GTK_TYPE_ADJUSTMENT
);
175 gtk_pizza_child_type (GtkContainer
*container
)
177 return GTK_TYPE_WIDGET
;
181 gtk_pizza_init (GtkPizza
*pizza
)
183 GTK_WIDGET_UNSET_FLAGS (pizza
, GTK_NO_WINDOW
);
185 pizza
->children
= NULL
;
187 pizza
->bin_window
= NULL
;
189 pizza
->m_xoffset
= 0;
190 pizza
->m_yoffset
= 0;
198 pizza
= g_object_new (gtk_pizza_get_type (), NULL
);
200 return GTK_WIDGET (pizza
);
203 gint
gtk_pizza_get_xoffset (GtkPizza
*pizza
)
205 g_return_val_if_fail ( (pizza
!= NULL
), -1 );
206 g_return_val_if_fail ( (GTK_IS_PIZZA (pizza
)), -1 );
208 return pizza
->m_xoffset
;
211 gint
gtk_pizza_get_yoffset (GtkPizza
*pizza
)
213 g_return_val_if_fail ( (pizza
!= NULL
), -1 );
214 g_return_val_if_fail ( (GTK_IS_PIZZA (pizza
)), -1 );
216 return pizza
->m_yoffset
;
219 void gtk_pizza_set_xoffset (GtkPizza
*pizza
, gint xoffset
)
221 g_return_if_fail (pizza
!= NULL
);
222 g_return_if_fail (GTK_IS_PIZZA (pizza
));
224 pizza
->m_xoffset
= xoffset
;
228 void gtk_pizza_set_yoffset (GtkPizza
*pizza
, gint yoffset
)
230 g_return_if_fail (pizza
!= NULL
);
231 g_return_if_fail (GTK_IS_PIZZA (pizza
));
233 pizza
->m_xoffset
= yoffset
;
237 gint
gtk_pizza_get_rtl_offset (GtkPizza
*pizza
)
241 g_return_val_if_fail ( (pizza
!= NULL
), 0 );
242 g_return_val_if_fail ( (GTK_IS_PIZZA (pizza
)), 0 );
244 if (!pizza
->bin_window
) return 0;
246 border
= pizza
->container
.border_width
;
248 return GTK_WIDGET(pizza
)->allocation
.width
- border
*2;
253 gtk_pizza_scroll_set_adjustments (GtkPizza
*pizza
,
257 /* We handle scrolling in the wxScrolledWindow, not here. */
261 gtk_pizza_put (GtkPizza
*pizza
,
268 GtkPizzaChild
*child_info
;
270 g_return_if_fail (pizza
!= NULL
);
271 g_return_if_fail (GTK_IS_PIZZA (pizza
));
272 g_return_if_fail (widget
!= NULL
);
274 child_info
= g_new (GtkPizzaChild
, 1);
276 child_info
->widget
= widget
;
279 child_info
->width
= width
;
280 child_info
->height
= height
;
282 pizza
->children
= g_list_append (pizza
->children
, child_info
);
284 if (GTK_WIDGET_REALIZED (pizza
))
285 gtk_widget_set_parent_window (widget
, pizza
->bin_window
);
287 gtk_widget_set_parent (widget
, GTK_WIDGET (pizza
));
289 gtk_widget_set_size_request( widget
, width
, height
);
290 if (GTK_WIDGET_REALIZED (pizza
))
291 gtk_pizza_allocate_child (pizza
, child_info
);
295 gtk_pizza_set_size (GtkPizza
*pizza
,
302 GtkPizzaChild
*child
;
305 g_return_if_fail (pizza
!= NULL
);
306 g_return_if_fail (GTK_IS_PIZZA (pizza
));
307 g_return_if_fail (widget
!= NULL
);
309 #ifndef WX_WARN_ILLEGAL_SETSIZE
310 /* this really shouldn't happen -- but it does, a lot, right now and we
311 can't pass negative values to gtk_widget_set_size_request() without getting
312 a warning printed out, so filter them out here */
319 children
= pizza
->children
;
322 child
= children
->data
;
323 children
= children
->next
;
325 if (child
->widget
== widget
)
327 if ((child
->x
== x
) &&
329 (child
->width
== width
) &&
330 (child
->height
== height
)) return;
334 child
->width
= width
;
335 child
->height
= height
;
337 gtk_widget_set_size_request (widget
, width
, height
);
345 gtk_pizza_map (GtkWidget
*widget
)
348 GtkPizzaChild
*child
;
351 g_return_if_fail (widget
!= NULL
);
352 g_return_if_fail (GTK_IS_PIZZA (widget
));
354 GTK_WIDGET_SET_FLAGS (widget
, GTK_MAPPED
);
355 pizza
= GTK_PIZZA (widget
);
357 children
= pizza
->children
;
360 child
= children
->data
;
361 children
= children
->next
;
363 if ( GTK_WIDGET_VISIBLE (child
->widget
) &&
364 !GTK_WIDGET_MAPPED (child
->widget
) )
366 gtk_widget_map (child
->widget
);
370 gdk_window_show (widget
->window
);
371 gdk_window_show (pizza
->bin_window
);
375 gtk_pizza_realize (GtkWidget
*widget
)
378 GdkWindowAttr attributes
;
379 gint attributes_mask
;
380 GtkPizzaChild
*child
;
384 g_return_if_fail (widget
!= NULL
);
385 g_return_if_fail (GTK_IS_PIZZA (widget
));
387 pizza
= GTK_PIZZA (widget
);
388 GTK_WIDGET_SET_FLAGS (widget
, GTK_REALIZED
);
390 attributes
.window_type
= GDK_WINDOW_CHILD
;
392 attributes
.x
= widget
->allocation
.x
;
393 attributes
.y
= widget
->allocation
.y
;
394 attributes
.width
= widget
->allocation
.width
;
395 attributes
.height
= widget
->allocation
.height
;
397 border
= pizza
->container
.border_width
;
398 attributes
.x
+= border
;
399 attributes
.y
+= border
;
400 attributes
.width
-= 2 * border
;
401 attributes
.height
-= 2 * border
;
404 if (attributes
.width
< 2) attributes
.width
= 2;
405 if (attributes
.height
< 2) attributes
.height
= 2;
407 attributes
.wclass
= GDK_INPUT_OUTPUT
;
408 attributes
.visual
= gtk_widget_get_visual (widget
);
409 attributes
.colormap
= gtk_widget_get_colormap (widget
);
410 attributes
.event_mask
= GDK_VISIBILITY_NOTIFY_MASK
;
411 attributes_mask
= GDK_WA_X
| GDK_WA_Y
| GDK_WA_VISUAL
| GDK_WA_COLORMAP
;
413 widget
->window
= gdk_window_new(gtk_widget_get_parent_window (widget
),
414 &attributes
, attributes_mask
);
415 gdk_window_set_user_data (widget
->window
, widget
);
420 attributes
.event_mask
= gtk_widget_get_events (widget
);
421 attributes
.event_mask
|= GDK_EXPOSURE_MASK
|
423 GDK_POINTER_MOTION_MASK
|
424 GDK_POINTER_MOTION_HINT_MASK
|
425 GDK_BUTTON_MOTION_MASK
|
426 GDK_BUTTON1_MOTION_MASK
|
427 GDK_BUTTON2_MOTION_MASK
|
428 GDK_BUTTON3_MOTION_MASK
|
429 GDK_BUTTON_PRESS_MASK
|
430 GDK_BUTTON_RELEASE_MASK
|
432 GDK_KEY_RELEASE_MASK
|
433 GDK_ENTER_NOTIFY_MASK
|
434 GDK_LEAVE_NOTIFY_MASK
|
435 GDK_FOCUS_CHANGE_MASK
;
437 pizza
->bin_window
= gdk_window_new(widget
->window
,
438 &attributes
, attributes_mask
);
439 gdk_window_set_user_data (pizza
->bin_window
, widget
);
441 widget
->style
= gtk_style_attach (widget
->style
, widget
->window
);
442 gtk_style_set_background (widget
->style
, widget
->window
, GTK_STATE_NORMAL
);
443 gtk_style_set_background (widget
->style
, pizza
->bin_window
, GTK_STATE_NORMAL
);
446 gdk_window_set_back_pixmap( widget->window, NULL, FALSE );
447 gdk_window_set_back_pixmap( pizza->bin_window, NULL, FALSE );
450 /* cannot be done before realisation */
451 children
= pizza
->children
;
454 child
= children
->data
;
455 children
= children
->next
;
457 gtk_widget_set_parent_window (child
->widget
, pizza
->bin_window
);
462 gtk_pizza_unrealize (GtkWidget
*widget
)
466 g_return_if_fail (widget
!= NULL
);
467 g_return_if_fail (GTK_IS_PIZZA (widget
));
469 pizza
= GTK_PIZZA (widget
);
471 gdk_window_set_user_data (pizza
->bin_window
, NULL
);
472 gdk_window_destroy (pizza
->bin_window
);
473 pizza
->bin_window
= NULL
;
475 if (pizza_parent_class
->unrealize
)
476 pizza_parent_class
->unrealize(widget
);
480 gtk_pizza_size_request (GtkWidget
*widget
,
481 GtkRequisition
*requisition
)
484 GtkPizzaChild
*child
;
486 GtkRequisition child_requisition
;
488 g_return_if_fail (widget
!= NULL
);
489 g_return_if_fail (GTK_IS_PIZZA (widget
));
490 g_return_if_fail (requisition
!= NULL
);
492 pizza
= GTK_PIZZA (widget
);
494 children
= pizza
->children
;
497 child
= children
->data
;
498 children
= children
->next
;
500 if (GTK_WIDGET_VISIBLE (child
->widget
))
502 gtk_widget_size_request (child
->widget
, &child_requisition
);
506 /* request very little, I'm not sure if requesting nothing
507 will always have positive effects on stability... */
508 requisition
->width
= 2;
509 requisition
->height
= 2;
513 gtk_pizza_size_allocate (GtkWidget
*widget
,
514 GtkAllocation
*allocation
)
519 GtkPizzaChild
*child
;
521 gboolean only_resize
;
523 g_return_if_fail (widget
!= NULL
);
524 g_return_if_fail (GTK_IS_PIZZA(widget
));
525 g_return_if_fail (allocation
!= NULL
);
527 pizza
= GTK_PIZZA (widget
);
529 only_resize
= ((widget
->allocation
.x
== allocation
->x
) &&
530 (widget
->allocation
.y
== allocation
->y
));
531 widget
->allocation
= *allocation
;
533 border
= pizza
->container
.border_width
;
535 x
= allocation
->x
+ border
;
536 y
= allocation
->y
+ border
;
537 w
= allocation
->width
- border
*2;
538 h
= allocation
->height
- border
*2;
544 if (GTK_WIDGET_REALIZED (widget
))
547 gdk_window_resize( widget
->window
, w
, h
);
549 gdk_window_move_resize( widget
->window
, x
, y
, w
, h
);
551 gdk_window_resize( pizza
->bin_window
, w
, h
);
554 children
= pizza
->children
;
557 child
= children
->data
;
558 children
= children
->next
;
560 gtk_pizza_allocate_child (pizza
, child
);
565 gtk_pizza_style_set(GtkWidget
*widget
, GtkStyle
*previous_style
)
567 if (GTK_WIDGET_REALIZED(widget
))
569 gtk_style_set_background(widget
->style
, widget
->window
, GTK_STATE_NORMAL
);
570 gtk_style_set_background(widget
->style
, GTK_PIZZA(widget
)->bin_window
, GTK_STATE_NORMAL
);
573 pizza_parent_class
->style_set(widget
, previous_style
);
577 gtk_pizza_add (GtkContainer
*container
,
580 g_return_if_fail (container
!= NULL
);
581 g_return_if_fail (GTK_IS_PIZZA (container
));
582 g_return_if_fail (widget
!= NULL
);
584 gtk_pizza_put (GTK_PIZZA (container
), widget
, 0, 0, 20, 20 );
588 gtk_pizza_remove (GtkContainer
*container
,
592 GtkPizzaChild
*child
;
595 g_return_if_fail (container
!= NULL
);
596 g_return_if_fail (GTK_IS_PIZZA (container
));
597 g_return_if_fail (widget
!= NULL
);
599 pizza
= GTK_PIZZA (container
);
601 children
= pizza
->children
;
604 child
= children
->data
;
606 if (child
->widget
== widget
)
608 gtk_widget_unparent (widget
);
610 /* security checks */
611 g_return_if_fail (GTK_IS_WIDGET (widget
));
613 pizza
->children
= g_list_remove_link (pizza
->children
, children
);
614 g_list_free (children
);
617 /* security checks */
618 g_return_if_fail (GTK_IS_WIDGET (widget
));
623 children
= children
->next
;
628 gtk_pizza_forall (GtkContainer
*container
,
629 gboolean include_internals
,
630 GtkCallback callback
,
631 gpointer callback_data
)
634 GtkPizzaChild
*child
;
637 g_return_if_fail (container
!= NULL
);
638 g_return_if_fail (GTK_IS_PIZZA (container
));
639 g_return_if_fail (callback
!= (GtkCallback
)NULL
);
641 pizza
= GTK_PIZZA (container
);
643 children
= pizza
->children
;
646 child
= children
->data
;
647 children
= children
->next
;
649 (* callback
) (child
->widget
, callback_data
);
654 gtk_pizza_allocate_child (GtkPizza
*pizza
,
655 GtkPizzaChild
*child
)
657 GtkAllocation allocation
;
658 GtkRequisition requisition
;
660 allocation
.x
= child
->x
- pizza
->m_xoffset
;
661 allocation
.y
= child
->y
- pizza
->m_yoffset
;
662 gtk_widget_get_child_requisition (child
->widget
, &requisition
);
663 allocation
.width
= requisition
.width
;
664 allocation
.height
= requisition
.height
;
666 if (gtk_widget_get_direction( GTK_WIDGET(pizza
) ) == GTK_TEXT_DIR_RTL
)
668 /* reverse horizontal placement */
671 offset
= GTK_WIDGET(pizza
)->allocation
.width
;
672 border
= pizza
->container
.border_width
;
675 allocation
.x
= offset
- child
->x
- allocation
.width
+ pizza
->m_xoffset
;
678 gtk_widget_size_allocate (child
->widget
, &allocation
);
682 gtk_pizza_adjust_allocations_recurse (GtkWidget
*widget
,
685 GtkPizzaAdjData
*data
= cb_data
;
687 widget
->allocation
.x
+= data
->dx
;
688 widget
->allocation
.y
+= data
->dy
;
690 if (GTK_WIDGET_NO_WINDOW (widget
) && GTK_IS_CONTAINER (widget
))
692 gtk_container_forall (GTK_CONTAINER (widget
),
693 gtk_pizza_adjust_allocations_recurse
,
699 gtk_pizza_adjust_allocations (GtkPizza
*pizza
,
704 GtkPizzaAdjData data
;
709 tmp_list
= pizza
->children
;
712 GtkPizzaChild
*child
= tmp_list
->data
;
713 tmp_list
= tmp_list
->next
;
715 child
->widget
->allocation
.x
+= dx
;
716 child
->widget
->allocation
.y
+= dy
;
718 if (GTK_WIDGET_NO_WINDOW (child
->widget
) &&
719 GTK_IS_CONTAINER (child
->widget
))
721 gtk_container_forall (GTK_CONTAINER (child
->widget
),
722 gtk_pizza_adjust_allocations_recurse
,
729 gtk_pizza_scroll (GtkPizza
*pizza
, gint dx
, gint dy
)
731 pizza
->m_xoffset
+= dx
;
732 pizza
->m_yoffset
+= dy
;
734 gtk_pizza_adjust_allocations (pizza
, -dx
, -dy
);
736 if (pizza
->bin_window
)
737 gdk_window_scroll( pizza
->bin_window
, -dx
, -dy
);
742 #endif /* __cplusplus */