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 _GtkPizzaChild GtkPizzaChild
;
24 typedef struct _GtkPizzaClass GtkPizzaClass
;
25 typedef struct _GtkPizzaAdjData GtkPizzaAdjData
;
29 GtkContainerClass parent_class
;
31 void (*set_scroll_adjustments
) (GtkPizza
*pizza
,
32 GtkAdjustment
*hadjustment
,
33 GtkAdjustment
*vadjustment
);
43 struct _GtkPizzaAdjData
49 static void gtk_pizza_class_init (GtkPizzaClass
*klass
);
50 static void gtk_pizza_init (GtkPizza
*pizza
);
52 static void gtk_pizza_realize (GtkWidget
*widget
);
53 static void gtk_pizza_unrealize (GtkWidget
*widget
);
55 static void gtk_pizza_map (GtkWidget
*widget
);
57 static void gtk_pizza_size_request (GtkWidget
*widget
,
58 GtkRequisition
*requisition
);
59 static void gtk_pizza_size_allocate (GtkWidget
*widget
,
60 GtkAllocation
*allocation
);
61 static void gtk_pizza_style_set (GtkWidget
*widget
,
62 GtkStyle
*previous_style
);
63 static void gtk_pizza_add (GtkContainer
*container
,
65 static void gtk_pizza_remove (GtkContainer
*container
,
67 static void gtk_pizza_forall (GtkContainer
*container
,
68 gboolean include_internals
,
70 gpointer callback_data
);
72 static void gtk_pizza_allocate_child (GtkPizza
*pizza
,
73 GtkPizzaChild
*child
);
74 static void gtk_pizza_adjust_allocations_recurse (GtkWidget
*widget
,
77 static GtkType
gtk_pizza_child_type (GtkContainer
*container
);
79 static void gtk_pizza_scroll_set_adjustments (GtkPizza
*pizza
,
83 static GtkWidgetClass
* pizza_parent_class
;
88 static GtkType pizza_type
= 0;
92 const GTypeInfo pizza_info
=
94 sizeof (GtkPizzaClass
),
96 NULL
, /* base_finalize */
97 (GClassInitFunc
) gtk_pizza_class_init
,
98 NULL
, /* class_finalize */
99 NULL
, /* class_data */
101 16, /* n_preallocs */
102 (GInstanceInitFunc
) gtk_pizza_init
,
105 pizza_type
= g_type_register_static (GTK_TYPE_CONTAINER
, "GtkPizza", &pizza_info
, (GTypeFlags
)0);
111 /* Marshaller needed for set_scroll_adjustments signal,
112 generated with GLib-2.4.6 glib-genmarshal */
113 #define g_marshal_value_peek_object(v) g_value_get_object (v)
115 g_cclosure_user_marshal_VOID__OBJECT_OBJECT (GClosure
*closure
,
116 GValue
*return_value
,
117 guint n_param_values
,
118 const GValue
*param_values
,
119 gpointer invocation_hint
,
120 gpointer marshal_data
)
122 typedef void (*GMarshalFunc_VOID__OBJECT_OBJECT
) (gpointer data1
,
126 register GMarshalFunc_VOID__OBJECT_OBJECT callback
;
127 register GCClosure
*cc
= (GCClosure
*) closure
;
128 register gpointer data1
, data2
;
130 g_return_if_fail (n_param_values
== 3);
132 if (G_CCLOSURE_SWAP_DATA (closure
))
134 data1
= closure
->data
;
135 data2
= g_value_peek_pointer (param_values
+ 0);
139 data1
= g_value_peek_pointer (param_values
+ 0);
140 data2
= closure
->data
;
142 callback
= (GMarshalFunc_VOID__OBJECT_OBJECT
) (marshal_data
? marshal_data
: cc
->callback
);
145 g_marshal_value_peek_object (param_values
+ 1),
146 g_marshal_value_peek_object (param_values
+ 2),
151 gtk_pizza_class_init (GtkPizzaClass
*klass
)
153 GtkObjectClass
*object_class
;
154 GtkWidgetClass
*widget_class
;
155 GtkContainerClass
*container_class
;
157 object_class
= (GtkObjectClass
*) klass
;
158 widget_class
= (GtkWidgetClass
*) klass
;
159 container_class
= (GtkContainerClass
*) klass
;
160 pizza_parent_class
= gtk_type_class (GTK_TYPE_CONTAINER
);
162 widget_class
->map
= gtk_pizza_map
;
163 widget_class
->realize
= gtk_pizza_realize
;
164 widget_class
->unrealize
= gtk_pizza_unrealize
;
165 widget_class
->size_request
= gtk_pizza_size_request
;
166 widget_class
->size_allocate
= gtk_pizza_size_allocate
;
167 widget_class
->style_set
= gtk_pizza_style_set
;
169 container_class
->add
= gtk_pizza_add
;
170 container_class
->remove
= gtk_pizza_remove
;
171 container_class
->forall
= gtk_pizza_forall
;
173 container_class
->child_type
= gtk_pizza_child_type
;
175 klass
->set_scroll_adjustments
= gtk_pizza_scroll_set_adjustments
;
177 widget_class
->set_scroll_adjustments_signal
=
179 "set_scroll_adjustments",
180 G_TYPE_FROM_CLASS(object_class
),
182 G_STRUCT_OFFSET(GtkPizzaClass
, set_scroll_adjustments
),
185 g_cclosure_user_marshal_VOID__OBJECT_OBJECT
,
189 GTK_TYPE_ADJUSTMENT
);
193 gtk_pizza_child_type (GtkContainer
*container
)
195 return GTK_TYPE_WIDGET
;
199 gtk_pizza_init (GtkPizza
*pizza
)
201 GTK_WIDGET_SET_FLAGS (pizza
, GTK_CAN_FOCUS
);
202 GTK_WIDGET_UNSET_FLAGS (pizza
, GTK_NO_WINDOW
);
204 pizza
->children
= NULL
;
206 pizza
->bin_window
= NULL
;
208 pizza
->m_xoffset
= 0;
209 pizza
->m_yoffset
= 0;
217 pizza
= g_object_new (gtk_pizza_get_type (), NULL
);
219 return GTK_WIDGET (pizza
);
222 gint
gtk_pizza_get_xoffset (GtkPizza
*pizza
)
224 g_return_val_if_fail ( (pizza
!= NULL
), -1 );
225 g_return_val_if_fail ( (GTK_IS_PIZZA (pizza
)), -1 );
227 return pizza
->m_xoffset
;
230 gint
gtk_pizza_get_yoffset (GtkPizza
*pizza
)
232 g_return_val_if_fail ( (pizza
!= NULL
), -1 );
233 g_return_val_if_fail ( (GTK_IS_PIZZA (pizza
)), -1 );
235 return pizza
->m_yoffset
;
238 void gtk_pizza_set_xoffset (GtkPizza
*pizza
, gint xoffset
)
240 g_return_if_fail (pizza
!= NULL
);
241 g_return_if_fail (GTK_IS_PIZZA (pizza
));
243 pizza
->m_xoffset
= xoffset
;
247 void gtk_pizza_set_yoffset (GtkPizza
*pizza
, gint yoffset
)
249 g_return_if_fail (pizza
!= NULL
);
250 g_return_if_fail (GTK_IS_PIZZA (pizza
));
252 pizza
->m_xoffset
= yoffset
;
256 gint
gtk_pizza_get_rtl_offset (GtkPizza
*pizza
)
260 g_return_val_if_fail ( (pizza
!= NULL
), 0 );
261 g_return_val_if_fail ( (GTK_IS_PIZZA (pizza
)), 0 );
263 if (!pizza
->bin_window
) return 0;
265 border
= pizza
->container
.border_width
;
267 return GTK_WIDGET(pizza
)->allocation
.width
- border
*2;
272 gtk_pizza_scroll_set_adjustments (GtkPizza
*pizza
,
276 /* We handle scrolling in the wxScrolledWindow, not here. */
280 gtk_pizza_put (GtkPizza
*pizza
,
287 GtkPizzaChild
*child_info
;
289 g_return_if_fail (pizza
!= NULL
);
290 g_return_if_fail (GTK_IS_PIZZA (pizza
));
291 g_return_if_fail (widget
!= NULL
);
293 child_info
= g_new (GtkPizzaChild
, 1);
295 child_info
->widget
= widget
;
299 pizza
->children
= g_list_append (pizza
->children
, child_info
);
301 if (GTK_WIDGET_REALIZED (pizza
))
302 gtk_widget_set_parent_window (widget
, pizza
->bin_window
);
304 gtk_widget_set_parent (widget
, GTK_WIDGET (pizza
));
306 gtk_widget_set_size_request( widget
, width
, height
);
307 if (GTK_WIDGET_REALIZED (pizza
))
308 gtk_pizza_allocate_child (pizza
, child_info
);
312 gtk_pizza_set_size (GtkPizza
*pizza
,
319 GtkPizzaChild
*child
;
322 g_return_if_fail (pizza
!= NULL
);
323 g_return_if_fail (GTK_IS_PIZZA (pizza
));
324 g_return_if_fail (widget
!= NULL
);
326 #ifndef WX_WARN_ILLEGAL_SETSIZE
327 /* this really shouldn't happen -- but it does, a lot, right now and we
328 can't pass negative values to gtk_widget_set_size_request() without getting
329 a warning printed out, so filter them out here */
336 children
= pizza
->children
;
339 child
= children
->data
;
340 children
= children
->next
;
342 if (child
->widget
== widget
)
344 if (child
->x
!= x
|| child
->y
!= y
)
348 gtk_widget_queue_resize(widget
);
351 gtk_widget_set_size_request (widget
, width
, height
);
359 gtk_pizza_map (GtkWidget
*widget
)
362 GtkPizzaChild
*child
;
365 g_return_if_fail (widget
!= NULL
);
366 g_return_if_fail (GTK_IS_PIZZA (widget
));
368 GTK_WIDGET_SET_FLAGS (widget
, GTK_MAPPED
);
369 pizza
= GTK_PIZZA (widget
);
371 children
= pizza
->children
;
374 child
= children
->data
;
375 children
= children
->next
;
377 if ( GTK_WIDGET_VISIBLE (child
->widget
) &&
378 !GTK_WIDGET_MAPPED (child
->widget
) )
380 gtk_widget_map (child
->widget
);
384 gdk_window_show (widget
->window
);
385 gdk_window_show (pizza
->bin_window
);
389 gtk_pizza_realize (GtkWidget
*widget
)
392 GdkWindowAttr attributes
;
393 gint attributes_mask
;
394 GtkPizzaChild
*child
;
398 g_return_if_fail (widget
!= NULL
);
399 g_return_if_fail (GTK_IS_PIZZA (widget
));
401 pizza
= GTK_PIZZA (widget
);
402 GTK_WIDGET_SET_FLAGS (widget
, GTK_REALIZED
);
404 attributes
.window_type
= GDK_WINDOW_CHILD
;
406 attributes
.x
= widget
->allocation
.x
;
407 attributes
.y
= widget
->allocation
.y
;
408 attributes
.width
= widget
->allocation
.width
;
409 attributes
.height
= widget
->allocation
.height
;
411 border
= pizza
->container
.border_width
;
412 attributes
.x
+= border
;
413 attributes
.y
+= border
;
414 attributes
.width
-= 2 * border
;
415 attributes
.height
-= 2 * border
;
418 if (attributes
.width
< 2) attributes
.width
= 2;
419 if (attributes
.height
< 2) attributes
.height
= 2;
421 attributes
.wclass
= GDK_INPUT_OUTPUT
;
422 attributes
.visual
= gtk_widget_get_visual (widget
);
423 attributes
.colormap
= gtk_widget_get_colormap (widget
);
424 attributes
.event_mask
= GDK_VISIBILITY_NOTIFY_MASK
;
425 attributes_mask
= GDK_WA_X
| GDK_WA_Y
| GDK_WA_VISUAL
| GDK_WA_COLORMAP
;
427 widget
->window
= gdk_window_new(gtk_widget_get_parent_window (widget
),
428 &attributes
, attributes_mask
);
429 gdk_window_set_user_data (widget
->window
, widget
);
434 attributes
.event_mask
= gtk_widget_get_events (widget
);
435 attributes
.event_mask
|= GDK_EXPOSURE_MASK
|
437 GDK_POINTER_MOTION_MASK
|
438 GDK_POINTER_MOTION_HINT_MASK
|
439 GDK_BUTTON_MOTION_MASK
|
440 GDK_BUTTON1_MOTION_MASK
|
441 GDK_BUTTON2_MOTION_MASK
|
442 GDK_BUTTON3_MOTION_MASK
|
443 GDK_BUTTON_PRESS_MASK
|
444 GDK_BUTTON_RELEASE_MASK
|
446 GDK_KEY_RELEASE_MASK
|
447 GDK_ENTER_NOTIFY_MASK
|
448 GDK_LEAVE_NOTIFY_MASK
|
449 GDK_FOCUS_CHANGE_MASK
;
451 pizza
->bin_window
= gdk_window_new(widget
->window
,
452 &attributes
, attributes_mask
);
453 gdk_window_set_user_data (pizza
->bin_window
, widget
);
455 widget
->style
= gtk_style_attach (widget
->style
, widget
->window
);
456 gtk_style_set_background (widget
->style
, widget
->window
, GTK_STATE_NORMAL
);
457 gtk_style_set_background (widget
->style
, pizza
->bin_window
, GTK_STATE_NORMAL
);
460 gdk_window_set_back_pixmap( widget->window, NULL, FALSE );
461 gdk_window_set_back_pixmap( pizza->bin_window, NULL, FALSE );
464 /* cannot be done before realisation */
465 children
= pizza
->children
;
468 child
= children
->data
;
469 children
= children
->next
;
471 gtk_widget_set_parent_window (child
->widget
, pizza
->bin_window
);
476 gtk_pizza_unrealize (GtkWidget
*widget
)
480 g_return_if_fail (widget
!= NULL
);
481 g_return_if_fail (GTK_IS_PIZZA (widget
));
483 pizza
= GTK_PIZZA (widget
);
485 gdk_window_set_user_data (pizza
->bin_window
, NULL
);
486 gdk_window_destroy (pizza
->bin_window
);
487 pizza
->bin_window
= NULL
;
489 if (pizza_parent_class
->unrealize
)
490 pizza_parent_class
->unrealize(widget
);
494 gtk_pizza_size_request (GtkWidget
*widget
,
495 GtkRequisition
*requisition
)
498 GtkPizzaChild
*child
;
500 GtkRequisition child_requisition
;
502 g_return_if_fail (widget
!= NULL
);
503 g_return_if_fail (GTK_IS_PIZZA (widget
));
504 g_return_if_fail (requisition
!= NULL
);
506 pizza
= GTK_PIZZA (widget
);
508 children
= pizza
->children
;
511 child
= children
->data
;
512 children
= children
->next
;
514 if (GTK_WIDGET_VISIBLE (child
->widget
))
516 gtk_widget_size_request (child
->widget
, &child_requisition
);
520 /* request very little, I'm not sure if requesting nothing
521 will always have positive effects on stability... */
522 requisition
->width
= 2;
523 requisition
->height
= 2;
527 gtk_pizza_size_allocate (GtkWidget
*widget
,
528 GtkAllocation
*allocation
)
533 GtkPizzaChild
*child
;
535 gboolean only_resize
;
537 g_return_if_fail (widget
!= NULL
);
538 g_return_if_fail (GTK_IS_PIZZA(widget
));
539 g_return_if_fail (allocation
!= NULL
);
541 pizza
= GTK_PIZZA (widget
);
543 only_resize
= ((widget
->allocation
.x
== allocation
->x
) &&
544 (widget
->allocation
.y
== allocation
->y
));
545 widget
->allocation
= *allocation
;
547 border
= pizza
->container
.border_width
;
549 x
= allocation
->x
+ border
;
550 y
= allocation
->y
+ border
;
551 w
= allocation
->width
- border
*2;
552 h
= allocation
->height
- border
*2;
558 if (GTK_WIDGET_REALIZED (widget
))
561 gdk_window_resize( widget
->window
, w
, h
);
563 gdk_window_move_resize( widget
->window
, x
, y
, w
, h
);
565 gdk_window_resize( pizza
->bin_window
, w
, h
);
568 children
= pizza
->children
;
571 child
= children
->data
;
572 children
= children
->next
;
574 gtk_pizza_allocate_child (pizza
, child
);
579 gtk_pizza_style_set(GtkWidget
*widget
, GtkStyle
*previous_style
)
581 if (GTK_WIDGET_REALIZED(widget
))
583 gtk_style_set_background(widget
->style
, widget
->window
, GTK_STATE_NORMAL
);
584 gtk_style_set_background(widget
->style
, GTK_PIZZA(widget
)->bin_window
, GTK_STATE_NORMAL
);
587 pizza_parent_class
->style_set(widget
, previous_style
);
591 gtk_pizza_add (GtkContainer
*container
,
594 g_return_if_fail (container
!= NULL
);
595 g_return_if_fail (GTK_IS_PIZZA (container
));
596 g_return_if_fail (widget
!= NULL
);
598 gtk_pizza_put (GTK_PIZZA (container
), widget
, 0, 0, 20, 20 );
602 gtk_pizza_remove (GtkContainer
*container
,
606 GtkPizzaChild
*child
;
609 g_return_if_fail (container
!= NULL
);
610 g_return_if_fail (GTK_IS_PIZZA (container
));
611 g_return_if_fail (widget
!= NULL
);
613 pizza
= GTK_PIZZA (container
);
615 children
= pizza
->children
;
618 child
= children
->data
;
620 if (child
->widget
== widget
)
622 gtk_widget_unparent (widget
);
624 /* security checks */
625 g_return_if_fail (GTK_IS_WIDGET (widget
));
627 pizza
->children
= g_list_remove_link (pizza
->children
, children
);
628 g_list_free (children
);
631 /* security checks */
632 g_return_if_fail (GTK_IS_WIDGET (widget
));
637 children
= children
->next
;
642 gtk_pizza_forall (GtkContainer
*container
,
643 gboolean include_internals
,
644 GtkCallback callback
,
645 gpointer callback_data
)
648 GtkPizzaChild
*child
;
651 g_return_if_fail (container
!= NULL
);
652 g_return_if_fail (GTK_IS_PIZZA (container
));
653 g_return_if_fail (callback
!= (GtkCallback
)NULL
);
655 pizza
= GTK_PIZZA (container
);
657 children
= pizza
->children
;
660 child
= children
->data
;
661 children
= children
->next
;
663 (* callback
) (child
->widget
, callback_data
);
668 gtk_pizza_allocate_child (GtkPizza
*pizza
,
669 GtkPizzaChild
*child
)
671 GtkAllocation allocation
;
672 GtkRequisition requisition
;
674 allocation
.x
= child
->x
- pizza
->m_xoffset
;
675 allocation
.y
= child
->y
- pizza
->m_yoffset
;
676 gtk_widget_get_child_requisition (child
->widget
, &requisition
);
677 allocation
.width
= requisition
.width
;
678 allocation
.height
= requisition
.height
;
680 if (gtk_widget_get_direction( GTK_WIDGET(pizza
) ) == GTK_TEXT_DIR_RTL
)
682 /* reverse horizontal placement */
685 offset
= GTK_WIDGET(pizza
)->allocation
.width
;
686 border
= pizza
->container
.border_width
;
689 allocation
.x
= offset
- child
->x
- allocation
.width
+ pizza
->m_xoffset
;
692 gtk_widget_size_allocate (child
->widget
, &allocation
);
696 gtk_pizza_adjust_allocations_recurse (GtkWidget
*widget
,
699 GtkPizzaAdjData
*data
= cb_data
;
701 widget
->allocation
.x
+= data
->dx
;
702 widget
->allocation
.y
+= data
->dy
;
704 if (GTK_WIDGET_NO_WINDOW (widget
) && GTK_IS_CONTAINER (widget
))
706 gtk_container_forall (GTK_CONTAINER (widget
),
707 gtk_pizza_adjust_allocations_recurse
,
713 gtk_pizza_adjust_allocations (GtkPizza
*pizza
,
718 GtkPizzaAdjData data
;
723 tmp_list
= pizza
->children
;
726 GtkPizzaChild
*child
= tmp_list
->data
;
727 tmp_list
= tmp_list
->next
;
729 child
->widget
->allocation
.x
+= dx
;
730 child
->widget
->allocation
.y
+= dy
;
732 if (GTK_WIDGET_NO_WINDOW (child
->widget
) &&
733 GTK_IS_CONTAINER (child
->widget
))
735 gtk_container_forall (GTK_CONTAINER (child
->widget
),
736 gtk_pizza_adjust_allocations_recurse
,
743 gtk_pizza_scroll (GtkPizza
*pizza
, gint dx
, gint dy
)
745 pizza
->m_xoffset
+= dx
;
746 pizza
->m_yoffset
+= dy
;
748 gtk_pizza_adjust_allocations (pizza
, -dx
, -dy
);
750 if (pizza
->bin_window
)
751 gdk_window_scroll( pizza
->bin_window
, -dx
, -dy
);
756 #endif /* __cplusplus */