1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/win_gtk.cpp
3 // Purpose: native GTK+ widget for wxWindow
4 // Author: Paul Cornett
6 // Copyright: (c) 2007 Paul Cornett
7 // Licence: wxWindows licence
8 ///////////////////////////////////////////////////////////////////////////////
11 #include "wx/gtk/private.h"
12 #include "wx/gtk/private/win_gtk.h"
15 wxPizza is a custom GTK+ widget derived from GtkFixed. A custom widget
16 is needed to adapt GTK+ to wxWidgets needs in 3 areas: scrolling, window
19 For scrolling, the "set_scroll_adjustments" signal is implemented
20 to make wxPizza appear scrollable to GTK+, allowing it to be put in a
21 GtkScrolledWindow. Child widget positions are adjusted for the scrolling
22 position in size_allocate. A second same-size GdkWindow is placed behind
23 widget->window, to allow GDK to use a more efficient scrolling technique.
25 For borders, space is reserved in realize and size_allocate. The border is
26 drawn on wxPizza's parent GdkWindow.
28 For RTL, child widget positions are mirrored in size_allocate.
31 static GtkWidgetClass
* parent_class
;
38 void (*set_scroll_adjustments
)(GtkWidget
*, GtkAdjustment
*, GtkAdjustment
*);
41 static void size_allocate(GtkWidget
* widget
, GtkAllocation
* alloc
)
43 const bool is_resize
=
44 widget
->allocation
.width
!= alloc
->width
||
45 widget
->allocation
.height
!= alloc
->height
;
47 widget
->allocation
.x
!= alloc
->x
||
48 widget
->allocation
.y
!= alloc
->y
;
50 widget
->allocation
= *alloc
;
52 wxPizza
* pizza
= WX_PIZZA(widget
);
53 int border_x
, border_y
;
54 pizza
->get_border_widths(border_x
, border_y
);
55 int w
= alloc
->width
- 2 * border_x
;
58 if (GTK_WIDGET_REALIZED(widget
) && (is_move
|| is_resize
))
60 int h
= alloc
->height
- 2 * border_y
;
63 if (pizza
->m_is_scrollable
)
65 // two windows, both same size
66 gdk_window_move_resize(pizza
->m_backing_window
,
67 alloc
->x
+ border_x
, alloc
->y
+ border_y
, w
, h
);
69 gdk_window_resize(widget
->window
, w
, h
);
74 gdk_window_move_resize(widget
->window
,
75 alloc
->x
+ border_x
, alloc
->y
+ border_y
, w
, h
);
78 // adjust child positions
79 for (const GList
* list
= pizza
->m_fixed
.children
; list
; list
= list
->next
)
81 const GtkFixedChild
* child
= static_cast<GtkFixedChild
*>(list
->data
);
82 if (GTK_WIDGET_VISIBLE(child
->widget
))
84 GtkAllocation child_alloc
;
85 // note that child positions do not take border into
86 // account, they need to be relative to widget->window,
87 // which has already been adjusted
88 child_alloc
.x
= child
->x
- pizza
->m_scroll_x
;
89 child_alloc
.y
= child
->y
- pizza
->m_scroll_y
;
91 gtk_widget_get_child_requisition(child
->widget
, &req
);
92 child_alloc
.width
= req
.width
;
93 child_alloc
.height
= req
.height
;
94 if (gtk_widget_get_direction(widget
) == GTK_TEXT_DIR_RTL
)
96 child_alloc
.x
= w
- child_alloc
.x
- child_alloc
.width
;
98 gtk_widget_size_allocate(child
->widget
, &child_alloc
);
103 static void realize(GtkWidget
* widget
)
105 parent_class
->realize(widget
);
107 wxPizza
* pizza
= WX_PIZZA(widget
);
108 if (pizza
->m_border_style
|| pizza
->m_is_scrollable
)
110 int border_x
, border_y
;
111 pizza
->get_border_widths(border_x
, border_y
);
112 int x
= widget
->allocation
.x
+ border_x
;
113 int y
= widget
->allocation
.y
+ border_y
;
114 int w
= widget
->allocation
.width
- 2 * border_x
;
115 int h
= widget
->allocation
.height
- 2 * border_y
;
118 if (pizza
->m_is_scrollable
)
120 // second window is created if wxWindow is scrollable
127 attr
.wclass
= GDK_INPUT_OUTPUT
;
128 attr
.visual
= gtk_widget_get_visual(widget
);
129 attr
.colormap
= gtk_widget_get_colormap(widget
);
130 attr
.window_type
= GDK_WINDOW_CHILD
;
132 pizza
->m_backing_window
= gdk_window_new(
133 gdk_window_get_parent(widget
->window
),
135 GDK_WA_X
| GDK_WA_Y
| GDK_WA_VISUAL
| GDK_WA_COLORMAP
);
137 gdk_window_set_user_data(pizza
->m_backing_window
, widget
);
138 gdk_window_reparent(widget
->window
, pizza
->m_backing_window
, 0, 0);
139 gdk_window_resize(widget
->window
, w
, h
);
141 // Parts of m_backing_window may be exposed temporarily while
142 // resizing. Setting the backing pixmap to None prevents those
143 // areas from being briefly painted black.
144 gdk_window_set_back_pixmap(pizza
->m_backing_window
, NULL
, false);
147 gdk_window_move_resize(widget
->window
, x
, y
, w
, h
);
151 static void unrealize(GtkWidget
* widget
)
153 parent_class
->unrealize(widget
);
155 wxPizza
* pizza
= WX_PIZZA(widget
);
156 if (pizza
->m_backing_window
)
158 gdk_window_set_user_data(pizza
->m_backing_window
, NULL
);
159 gdk_window_destroy(pizza
->m_backing_window
);
160 pizza
->m_backing_window
= NULL
;
164 static void map(GtkWidget
* widget
)
166 parent_class
->map(widget
);
168 wxPizza
* pizza
= WX_PIZZA(widget
);
169 if (pizza
->m_backing_window
)
170 gdk_window_show(pizza
->m_backing_window
);
173 static void unmap(GtkWidget
* widget
)
175 parent_class
->unmap(widget
);
177 wxPizza
* pizza
= WX_PIZZA(widget
);
178 if (pizza
->m_backing_window
)
179 gdk_window_hide(pizza
->m_backing_window
);
182 // not used, but needs to exist so gtk_widget_set_scroll_adjustments will work
183 static void set_scroll_adjustments(GtkWidget
*, GtkAdjustment
*, GtkAdjustment
*)
187 // Marshaller needed for set_scroll_adjustments signal,
188 // generated with GLib-2.4.6 glib-genmarshal
189 #define g_marshal_value_peek_object(v) g_value_get_object (v)
191 g_cclosure_user_marshal_VOID__OBJECT_OBJECT (GClosure
*closure
,
192 GValue
* /*return_value*/,
193 guint n_param_values
,
194 const GValue
*param_values
,
195 gpointer
/*invocation_hint*/,
196 gpointer marshal_data
)
198 typedef void (*GMarshalFunc_VOID__OBJECT_OBJECT
) (gpointer data1
,
202 register GMarshalFunc_VOID__OBJECT_OBJECT callback
;
203 register GCClosure
*cc
= (GCClosure
*) closure
;
204 register gpointer data1
, data2
;
206 g_return_if_fail (n_param_values
== 3);
208 if (G_CCLOSURE_SWAP_DATA (closure
))
210 data1
= closure
->data
;
211 data2
= g_value_peek_pointer (param_values
+ 0);
215 data1
= g_value_peek_pointer (param_values
+ 0);
216 data2
= closure
->data
;
218 callback
= (GMarshalFunc_VOID__OBJECT_OBJECT
) (marshal_data
? marshal_data
: cc
->callback
);
221 g_marshal_value_peek_object (param_values
+ 1),
222 g_marshal_value_peek_object (param_values
+ 2),
226 static void class_init(void* g_class
, void*)
228 GtkWidgetClass
* widget_class
= (GtkWidgetClass
*)g_class
;
229 widget_class
->size_allocate
= size_allocate
;
230 widget_class
->realize
= realize
;
231 widget_class
->unrealize
= unrealize
;
232 widget_class
->map
= map
;
233 widget_class
->unmap
= unmap
;
234 wxPizzaClass
* klass
= (wxPizzaClass
*)g_class
;
236 // needed to make widget appear scrollable to GTK+
237 klass
->set_scroll_adjustments
= set_scroll_adjustments
;
238 widget_class
->set_scroll_adjustments_signal
=
240 "set_scroll_adjustments",
241 G_TYPE_FROM_CLASS(g_class
),
243 G_STRUCT_OFFSET(wxPizzaClass
, set_scroll_adjustments
),
245 g_cclosure_user_marshal_VOID__OBJECT_OBJECT
,
246 G_TYPE_NONE
, 2, GTK_TYPE_ADJUSTMENT
, GTK_TYPE_ADJUSTMENT
);
248 parent_class
= GTK_WIDGET_CLASS(g_type_class_peek_parent(g_class
));
253 GType
wxPizza::type()
258 const GTypeInfo info
= {
259 sizeof(wxPizzaClass
),
266 type
= g_type_register_static(
267 GTK_TYPE_FIXED
, "wxPizza", &info
, GTypeFlags(0));
272 GtkWidget
* wxPizza::New(long windowStyle
)
274 GtkWidget
* widget
= GTK_WIDGET(g_object_new(type(), NULL
));
275 wxPizza
* pizza
= WX_PIZZA(widget
);
276 pizza
->m_backing_window
= NULL
;
277 pizza
->m_scroll_x
= 0;
278 pizza
->m_scroll_y
= 0;
279 pizza
->m_is_scrollable
= (windowStyle
& (wxHSCROLL
| wxVSCROLL
)) != 0;
280 // mask off border styles not useable with wxPizza
281 pizza
->m_border_style
= int(windowStyle
& BORDER_STYLES
);
282 gtk_fixed_set_has_window(GTK_FIXED(widget
), true);
283 gtk_widget_add_events(widget
,
286 GDK_POINTER_MOTION_MASK
|
287 GDK_POINTER_MOTION_HINT_MASK
|
288 GDK_BUTTON_MOTION_MASK
|
289 GDK_BUTTON1_MOTION_MASK
|
290 GDK_BUTTON2_MOTION_MASK
|
291 GDK_BUTTON3_MOTION_MASK
|
292 GDK_BUTTON_PRESS_MASK
|
293 GDK_BUTTON_RELEASE_MASK
|
295 GDK_KEY_RELEASE_MASK
|
296 GDK_ENTER_NOTIFY_MASK
|
297 GDK_LEAVE_NOTIFY_MASK
|
298 GDK_FOCUS_CHANGE_MASK
);
302 // gtk_fixed_move does not check for a change before issuing a queue_resize,
303 // we need to avoid that to prevent endless sizing loops, so check first
304 void wxPizza::move(GtkWidget
* widget
, int x
, int y
)
306 GtkFixed
* fixed
= &m_fixed
;
307 for (const GList
* list
= fixed
->children
; list
; list
= list
->next
)
309 const GtkFixedChild
* child
= static_cast<GtkFixedChild
*>(list
->data
);
310 if (child
->widget
== widget
)
312 if (child
->x
!= x
|| child
->y
!= y
)
313 gtk_fixed_move(fixed
, widget
, x
, y
);
324 // Adjust allocations for all widgets using the GdkWindow which was just scrolled
326 static void scroll_adjust(GtkWidget
* widget
, void* data
)
328 const AdjustData
* p
= static_cast<AdjustData
*>(data
);
329 if (widget
->window
== p
->window
)
331 widget
->allocation
.x
+= p
->dx
;
332 widget
->allocation
.y
+= p
->dy
;
333 // GtkFrame requires a queue_resize, otherwise parts of
334 // the frame newly exposed by the scroll are not drawn.
335 // To be safe, do it for all widgets.
336 gtk_widget_queue_resize_no_redraw(widget
);
337 if (GTK_IS_CONTAINER(widget
))
338 gtk_container_forall(GTK_CONTAINER(widget
), scroll_adjust
, data
);
343 void wxPizza::scroll(int dx
, int dy
)
345 GtkWidget
* widget
= GTK_WIDGET(this);
346 if (gtk_widget_get_direction(widget
) == GTK_TEXT_DIR_RTL
)
352 gdk_window_scroll(widget
->window
, dx
, dy
);
353 // Adjust child allocations. Doing a queue_resize on the children is not
354 // enough, sometimes they redraw in the wrong place during fast scrolling.
355 AdjustData data
= { widget
->window
, dx
, dy
};
356 gtk_container_forall(GTK_CONTAINER(widget
), scroll_adjust
, &data
);
360 void wxPizza::get_border_widths(int& x
, int& y
)
363 #ifndef __WXUNIVERSAL__
364 if (m_border_style
& wxBORDER_SIMPLE
)
366 else if (m_border_style
)
368 GtkWidget
*entry_widget
= wxGTKPrivate::GetEntryWidget();
369 if (entry_widget
->style
)
371 x
= entry_widget
->style
->xthickness
;
372 y
= entry_widget
->style
->ythickness
;