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
);
75 static GtkType
gtk_pizza_child_type (GtkContainer
*container
);
77 static void gtk_pizza_scroll_set_adjustments (GtkPizza
*pizza
,
81 static GtkWidgetClass
* pizza_parent_class
;
86 static GtkType pizza_type
= 0;
90 const GTypeInfo pizza_info
=
92 sizeof (GtkPizzaClass
),
94 NULL
, /* base_finalize */
95 (GClassInitFunc
) gtk_pizza_class_init
,
96 NULL
, /* class_finalize */
97 NULL
, /* class_data */
100 (GInstanceInitFunc
) gtk_pizza_init
,
103 pizza_type
= g_type_register_static (GTK_TYPE_CONTAINER
, "GtkPizza", &pizza_info
, (GTypeFlags
)0);
109 /* Marshaller needed for set_scroll_adjustments signal,
110 generated with GLib-2.4.6 glib-genmarshal */
111 #define g_marshal_value_peek_object(v) g_value_get_object (v)
113 g_cclosure_user_marshal_VOID__OBJECT_OBJECT (GClosure
*closure
,
114 GValue
*return_value
,
115 guint n_param_values
,
116 const GValue
*param_values
,
117 gpointer invocation_hint
,
118 gpointer marshal_data
)
120 typedef void (*GMarshalFunc_VOID__OBJECT_OBJECT
) (gpointer data1
,
124 register GMarshalFunc_VOID__OBJECT_OBJECT callback
;
125 register GCClosure
*cc
= (GCClosure
*) closure
;
126 register gpointer data1
, data2
;
128 g_return_if_fail (n_param_values
== 3);
130 if (G_CCLOSURE_SWAP_DATA (closure
))
132 data1
= closure
->data
;
133 data2
= g_value_peek_pointer (param_values
+ 0);
137 data1
= g_value_peek_pointer (param_values
+ 0);
138 data2
= closure
->data
;
140 callback
= (GMarshalFunc_VOID__OBJECT_OBJECT
) (marshal_data
? marshal_data
: cc
->callback
);
143 g_marshal_value_peek_object (param_values
+ 1),
144 g_marshal_value_peek_object (param_values
+ 2),
149 gtk_pizza_class_init (GtkPizzaClass
*klass
)
151 GtkObjectClass
*object_class
;
152 GtkWidgetClass
*widget_class
;
153 GtkContainerClass
*container_class
;
155 object_class
= (GtkObjectClass
*) klass
;
156 widget_class
= (GtkWidgetClass
*) klass
;
157 container_class
= (GtkContainerClass
*) klass
;
158 pizza_parent_class
= gtk_type_class (GTK_TYPE_CONTAINER
);
160 widget_class
->map
= gtk_pizza_map
;
161 widget_class
->realize
= gtk_pizza_realize
;
162 widget_class
->unrealize
= gtk_pizza_unrealize
;
163 widget_class
->size_request
= gtk_pizza_size_request
;
164 widget_class
->size_allocate
= gtk_pizza_size_allocate
;
165 widget_class
->style_set
= gtk_pizza_style_set
;
167 container_class
->add
= gtk_pizza_add
;
168 container_class
->remove
= gtk_pizza_remove
;
169 container_class
->forall
= gtk_pizza_forall
;
171 container_class
->child_type
= gtk_pizza_child_type
;
173 klass
->set_scroll_adjustments
= gtk_pizza_scroll_set_adjustments
;
175 widget_class
->set_scroll_adjustments_signal
=
177 "set_scroll_adjustments",
178 G_TYPE_FROM_CLASS(object_class
),
180 G_STRUCT_OFFSET(GtkPizzaClass
, set_scroll_adjustments
),
183 g_cclosure_user_marshal_VOID__OBJECT_OBJECT
,
187 GTK_TYPE_ADJUSTMENT
);
191 gtk_pizza_child_type (GtkContainer
*container
)
193 return GTK_TYPE_WIDGET
;
197 gtk_pizza_init (GtkPizza
*pizza
)
199 GTK_WIDGET_SET_FLAGS (pizza
, GTK_CAN_FOCUS
);
200 GTK_WIDGET_UNSET_FLAGS (pizza
, GTK_NO_WINDOW
);
202 pizza
->children
= NULL
;
204 pizza
->bin_window
= NULL
;
206 pizza
->m_xoffset
= 0;
207 pizza
->m_yoffset
= 0;
215 pizza
= g_object_new (gtk_pizza_get_type (), NULL
);
217 pizza
->m_noscroll
= FALSE
;
219 return GTK_WIDGET (pizza
);
223 gtk_pizza_new_no_scroll ()
227 pizza
= g_object_new (gtk_pizza_get_type (), NULL
);
229 pizza
->m_noscroll
= TRUE
;
231 return GTK_WIDGET (pizza
);
234 gint
gtk_pizza_get_xoffset (GtkPizza
*pizza
)
236 g_return_val_if_fail ( (pizza
!= NULL
), -1 );
237 g_return_val_if_fail ( (GTK_IS_PIZZA (pizza
)), -1 );
239 return pizza
->m_xoffset
;
242 gint
gtk_pizza_get_yoffset (GtkPizza
*pizza
)
244 g_return_val_if_fail ( (pizza
!= NULL
), -1 );
245 g_return_val_if_fail ( (GTK_IS_PIZZA (pizza
)), -1 );
247 return pizza
->m_yoffset
;
250 void gtk_pizza_set_xoffset (GtkPizza
*pizza
, gint xoffset
)
252 g_return_if_fail (pizza
!= NULL
);
253 g_return_if_fail (GTK_IS_PIZZA (pizza
));
255 pizza
->m_xoffset
= xoffset
;
259 void gtk_pizza_set_yoffset (GtkPizza
*pizza
, gint yoffset
)
261 g_return_if_fail (pizza
!= NULL
);
262 g_return_if_fail (GTK_IS_PIZZA (pizza
));
264 pizza
->m_xoffset
= yoffset
;
268 gint
gtk_pizza_get_rtl_offset (GtkPizza
*pizza
)
272 g_return_val_if_fail ( (pizza
!= NULL
), 0 );
273 g_return_val_if_fail ( (GTK_IS_PIZZA (pizza
)), 0 );
275 if (!pizza
->bin_window
) return 0;
277 border
= pizza
->container
.border_width
;
279 return GTK_WIDGET(pizza
)->allocation
.width
- border
*2;
284 gtk_pizza_scroll_set_adjustments (GtkPizza
*pizza
,
288 /* We handle scrolling in the wxScrolledWindow, not here. */
292 gtk_pizza_put (GtkPizza
*pizza
,
299 GtkPizzaChild
*child_info
;
301 g_return_if_fail (pizza
!= NULL
);
302 g_return_if_fail (GTK_IS_PIZZA (pizza
));
303 g_return_if_fail (widget
!= NULL
);
305 child_info
= g_new (GtkPizzaChild
, 1);
307 child_info
->widget
= widget
;
311 pizza
->children
= g_list_append (pizza
->children
, child_info
);
313 if (GTK_WIDGET_REALIZED (pizza
))
314 gtk_widget_set_parent_window (widget
, pizza
->bin_window
);
316 gtk_widget_set_parent (widget
, GTK_WIDGET (pizza
));
318 gtk_widget_set_size_request( widget
, width
, height
);
319 if (GTK_WIDGET_REALIZED (pizza
))
320 gtk_pizza_allocate_child (pizza
, child_info
);
324 gtk_pizza_set_size (GtkPizza
*pizza
,
331 GtkPizzaChild
*child
;
334 g_return_if_fail (pizza
!= NULL
);
335 g_return_if_fail (GTK_IS_PIZZA (pizza
));
336 g_return_if_fail (widget
!= NULL
);
338 #ifndef WX_WARN_ILLEGAL_SETSIZE
339 /* this really shouldn't happen -- but it does, a lot, right now and we
340 can't pass negative values to gtk_widget_set_size_request() without getting
341 a warning printed out, so filter them out here */
348 children
= pizza
->children
;
351 child
= children
->data
;
352 children
= children
->next
;
354 if (child
->widget
== widget
)
356 if (child
->x
!= x
|| child
->y
!= y
)
360 gtk_widget_queue_resize(widget
);
363 gtk_widget_set_size_request (widget
, width
, height
);
371 gtk_pizza_map (GtkWidget
*widget
)
374 GtkPizzaChild
*child
;
377 g_return_if_fail (widget
!= NULL
);
378 g_return_if_fail (GTK_IS_PIZZA (widget
));
380 GTK_WIDGET_SET_FLAGS (widget
, GTK_MAPPED
);
381 pizza
= GTK_PIZZA (widget
);
383 children
= pizza
->children
;
386 child
= children
->data
;
387 children
= children
->next
;
389 if ( GTK_WIDGET_VISIBLE (child
->widget
) &&
390 !GTK_WIDGET_MAPPED (child
->widget
) )
392 gtk_widget_map (child
->widget
);
396 gdk_window_show (widget
->window
);
397 gdk_window_show (pizza
->bin_window
);
401 gtk_pizza_realize (GtkWidget
*widget
)
404 GdkWindowAttr attributes
;
405 gint attributes_mask
;
406 GtkPizzaChild
*child
;
411 g_return_if_fail (widget
!= NULL
);
412 g_return_if_fail (GTK_IS_PIZZA (widget
));
414 pizza
= GTK_PIZZA (widget
);
415 GTK_WIDGET_SET_FLAGS (widget
, GTK_REALIZED
);
417 attributes
.window_type
= GDK_WINDOW_CHILD
;
419 attributes
.x
= widget
->allocation
.x
;
420 attributes
.y
= widget
->allocation
.y
;
421 attributes
.width
= widget
->allocation
.width
;
422 attributes
.height
= widget
->allocation
.height
;
425 if (attributes
.width
< 2) attributes
.width
= 2;
426 if (attributes
.height
< 2) attributes
.height
= 2;
428 border
= pizza
->container
.border_width
;
429 w
= attributes
.width
- 2 * border
;
430 h
= attributes
.height
- 2 * border
;
434 if (!pizza
->m_noscroll
)
436 attributes
.x
+= border
;
437 attributes
.y
+= border
;
438 attributes
.width
= w
;
439 attributes
.height
= h
;
442 attributes
.wclass
= GDK_INPUT_OUTPUT
;
443 attributes
.visual
= gtk_widget_get_visual (widget
);
444 attributes
.colormap
= gtk_widget_get_colormap (widget
);
445 attributes
.event_mask
= GDK_VISIBILITY_NOTIFY_MASK
;
446 attributes_mask
= GDK_WA_X
| GDK_WA_Y
| GDK_WA_VISUAL
| GDK_WA_COLORMAP
;
448 widget
->window
= gdk_window_new(gtk_widget_get_parent_window (widget
),
449 &attributes
, attributes_mask
);
450 gdk_window_set_user_data (widget
->window
, widget
);
454 if (pizza
->m_noscroll
)
456 attributes
.x
= border
;
457 attributes
.y
= border
;
458 attributes
.width
= w
;
459 attributes
.height
= h
;
462 attributes
.event_mask
= gtk_widget_get_events (widget
);
463 attributes
.event_mask
|= GDK_EXPOSURE_MASK
|
465 GDK_POINTER_MOTION_MASK
|
466 GDK_POINTER_MOTION_HINT_MASK
|
467 GDK_BUTTON_MOTION_MASK
|
468 GDK_BUTTON1_MOTION_MASK
|
469 GDK_BUTTON2_MOTION_MASK
|
470 GDK_BUTTON3_MOTION_MASK
|
471 GDK_BUTTON_PRESS_MASK
|
472 GDK_BUTTON_RELEASE_MASK
|
474 GDK_KEY_RELEASE_MASK
|
475 GDK_ENTER_NOTIFY_MASK
|
476 GDK_LEAVE_NOTIFY_MASK
|
477 GDK_FOCUS_CHANGE_MASK
;
479 pizza
->bin_window
= gdk_window_new(widget
->window
,
480 &attributes
, attributes_mask
);
481 gdk_window_set_user_data (pizza
->bin_window
, widget
);
483 widget
->style
= gtk_style_attach (widget
->style
, widget
->window
);
484 gtk_style_set_background (widget
->style
, widget
->window
, GTK_STATE_NORMAL
);
485 gtk_style_set_background (widget
->style
, pizza
->bin_window
, GTK_STATE_NORMAL
);
488 gdk_window_set_back_pixmap( widget->window, NULL, FALSE );
489 gdk_window_set_back_pixmap( pizza->bin_window, NULL, FALSE );
492 /* cannot be done before realisation */
493 children
= pizza
->children
;
496 child
= children
->data
;
497 children
= children
->next
;
499 gtk_widget_set_parent_window (child
->widget
, pizza
->bin_window
);
504 gtk_pizza_unrealize (GtkWidget
*widget
)
508 g_return_if_fail (widget
!= NULL
);
509 g_return_if_fail (GTK_IS_PIZZA (widget
));
511 pizza
= GTK_PIZZA (widget
);
513 gdk_window_set_user_data (pizza
->bin_window
, NULL
);
514 gdk_window_destroy (pizza
->bin_window
);
515 pizza
->bin_window
= NULL
;
517 if (pizza_parent_class
->unrealize
)
518 pizza_parent_class
->unrealize(widget
);
522 gtk_pizza_size_request (GtkWidget
*widget
,
523 GtkRequisition
*requisition
)
526 GtkPizzaChild
*child
;
528 GtkRequisition child_requisition
;
530 g_return_if_fail (widget
!= NULL
);
531 g_return_if_fail (GTK_IS_PIZZA (widget
));
532 g_return_if_fail (requisition
!= NULL
);
534 pizza
= GTK_PIZZA (widget
);
536 children
= pizza
->children
;
539 child
= children
->data
;
540 children
= children
->next
;
542 if (GTK_WIDGET_VISIBLE (child
->widget
))
544 gtk_widget_size_request (child
->widget
, &child_requisition
);
548 /* request very little, I'm not sure if requesting nothing
549 will always have positive effects on stability... */
550 requisition
->width
= 2;
551 requisition
->height
= 2;
555 gtk_pizza_size_allocate (GtkWidget
*widget
,
556 GtkAllocation
*allocation
)
561 GtkPizzaChild
*child
;
563 gboolean only_resize
;
565 g_return_if_fail (widget
!= NULL
);
566 g_return_if_fail (GTK_IS_PIZZA(widget
));
567 g_return_if_fail (allocation
!= NULL
);
569 pizza
= GTK_PIZZA (widget
);
571 only_resize
= ((widget
->allocation
.x
== allocation
->x
) &&
572 (widget
->allocation
.y
== allocation
->y
));
573 widget
->allocation
= *allocation
;
575 if (GTK_WIDGET_REALIZED(widget
))
577 border
= pizza
->container
.border_width
;
579 x
= allocation
->x
+ border
;
580 y
= allocation
->y
+ border
;
581 w
= allocation
->width
- border
*2;
582 h
= allocation
->height
- border
*2;
588 if (pizza
->m_noscroll
)
591 gdk_window_resize( widget
->window
, allocation
->width
, allocation
->height
);
593 gdk_window_move_resize( widget
->window
, allocation
->x
, allocation
->y
,
594 allocation
->width
, allocation
->height
);
596 gdk_window_move_resize( pizza
->bin_window
, border
, border
, w
, h
);
601 gdk_window_resize( widget
->window
, w
, h
);
603 gdk_window_move_resize( widget
->window
, x
, y
, w
, h
);
605 gdk_window_resize( pizza
->bin_window
, w
, h
);
609 children
= pizza
->children
;
612 child
= children
->data
;
613 children
= children
->next
;
615 gtk_pizza_allocate_child (pizza
, child
);
620 gtk_pizza_style_set(GtkWidget
*widget
, GtkStyle
*previous_style
)
622 if (GTK_WIDGET_REALIZED(widget
))
624 gtk_style_set_background(widget
->style
, widget
->window
, GTK_STATE_NORMAL
);
625 gtk_style_set_background(widget
->style
, GTK_PIZZA(widget
)->bin_window
, GTK_STATE_NORMAL
);
628 pizza_parent_class
->style_set(widget
, previous_style
);
632 gtk_pizza_add (GtkContainer
*container
,
635 g_return_if_fail (container
!= NULL
);
636 g_return_if_fail (GTK_IS_PIZZA (container
));
637 g_return_if_fail (widget
!= NULL
);
639 gtk_pizza_put (GTK_PIZZA (container
), widget
, 0, 0, 20, 20 );
643 gtk_pizza_remove (GtkContainer
*container
,
647 GtkPizzaChild
*child
;
650 g_return_if_fail (container
!= NULL
);
651 g_return_if_fail (GTK_IS_PIZZA (container
));
652 g_return_if_fail (widget
!= NULL
);
654 pizza
= GTK_PIZZA (container
);
656 children
= pizza
->children
;
659 child
= children
->data
;
661 if (child
->widget
== widget
)
663 gtk_widget_unparent (widget
);
665 /* security checks */
666 g_return_if_fail (GTK_IS_WIDGET (widget
));
668 pizza
->children
= g_list_remove_link (pizza
->children
, children
);
669 g_list_free (children
);
672 /* security checks */
673 g_return_if_fail (GTK_IS_WIDGET (widget
));
678 children
= children
->next
;
683 gtk_pizza_forall (GtkContainer
*container
,
684 gboolean include_internals
,
685 GtkCallback callback
,
686 gpointer callback_data
)
689 GtkPizzaChild
*child
;
692 g_return_if_fail (container
!= NULL
);
693 g_return_if_fail (GTK_IS_PIZZA (container
));
694 g_return_if_fail (callback
!= (GtkCallback
)NULL
);
696 pizza
= GTK_PIZZA (container
);
698 children
= pizza
->children
;
701 child
= children
->data
;
702 children
= children
->next
;
704 (* callback
) (child
->widget
, callback_data
);
709 gtk_pizza_allocate_child (GtkPizza
*pizza
,
710 GtkPizzaChild
*child
)
712 GtkAllocation allocation
;
713 GtkRequisition requisition
;
715 allocation
.x
= child
->x
- pizza
->m_xoffset
;
716 allocation
.y
= child
->y
- pizza
->m_yoffset
;
717 gtk_widget_get_child_requisition (child
->widget
, &requisition
);
718 allocation
.width
= requisition
.width
;
719 allocation
.height
= requisition
.height
;
721 if (gtk_widget_get_direction( GTK_WIDGET(pizza
) ) == GTK_TEXT_DIR_RTL
)
723 /* reverse horizontal placement */
726 offset
= GTK_WIDGET(pizza
)->allocation
.width
;
727 border
= pizza
->container
.border_width
;
730 allocation
.x
= offset
- child
->x
- allocation
.width
+ pizza
->m_xoffset
;
733 gtk_widget_size_allocate (child
->widget
, &allocation
);
737 gtk_pizza_scroll (GtkPizza
*pizza
, gint dx
, gint dy
)
741 pizza
->m_xoffset
+= dx
;
742 pizza
->m_yoffset
+= dy
;
744 if (pizza
->bin_window
)
745 gdk_window_scroll( pizza
->bin_window
, -dx
, -dy
);
747 tmp_list
= pizza
->children
;
750 GtkPizzaChild
*child
= tmp_list
->data
;
751 tmp_list
= tmp_list
->next
;
753 GtkAllocation alloc
= child
->widget
->allocation
;
756 gtk_widget_size_allocate( child
->widget
, &alloc
);
762 #endif /* __cplusplus */