add wxRenderer::GetCheckBoxSize(); refactor wxGTK code to avoid duplication (#9642)
[wxWidgets.git] / src / gtk / win_gtk.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/win_gtk.cpp
3 // Purpose: native GTK+ widget for wxWindow
4 // Author: Paul Cornett
5 // Id: $Id$
6 // Copyright: (c) 2007 Paul Cornett
7 // Licence: wxWindows licence
8 ///////////////////////////////////////////////////////////////////////////////
9
10 #include "wx/defs.h"
11 #include "wx/gtk/private.h"
12 #include "wx/gtk/private/win_gtk.h"
13
14 /*
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
17 borders, and RTL.
18
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.
24
25 For borders, space is reserved in realize and size_allocate.
26 For non-scrolling wxWindows, a second GdkWindow is used for the
27 border; scrolling wxWindows draw their border on wxPizza's parent
28 (a GtkScrolledWindow) GdkWindow. Border widths for sunken and raised
29 styles are taken from widget->style, so they will match the current theme.
30
31 For RTL, child widget positions are mirrored in size_allocate.
32 */
33
34 static GtkWidgetClass* parent_class;
35
36 extern "C" {
37
38 struct wxPizzaClass
39 {
40 GtkFixedClass parent;
41 void (*set_scroll_adjustments)(GtkWidget*, GtkAdjustment*, GtkAdjustment*);
42 };
43
44 static void size_allocate(GtkWidget* widget, GtkAllocation* alloc)
45 {
46 const bool is_resize =
47 widget->allocation.width != alloc->width ||
48 widget->allocation.height != alloc->height;
49 const bool is_move =
50 widget->allocation.x != alloc->x ||
51 widget->allocation.y != alloc->y;
52
53 widget->allocation = *alloc;
54
55 wxPizza* pizza = WX_PIZZA(widget);
56 int border_x, border_y;
57 pizza->get_border_widths(border_x, border_y);
58 int w = alloc->width - 2 * border_x;
59 if (w < 0) w = 0;
60
61 if (GTK_WIDGET_REALIZED(widget) && (is_move || is_resize))
62 {
63 int h = alloc->height - 2 * border_y;
64 if (h < 0) h = 0;
65
66 if (pizza->m_is_scrollable)
67 {
68 // two windows, both same size
69 gdk_window_move_resize(pizza->m_backing_window,
70 alloc->x + border_x, alloc->y + border_y, w, h);
71 if (is_resize)
72 gdk_window_resize(widget->window, w, h);
73 }
74 else if (pizza->m_backing_window)
75 {
76 // two windows, widget->window is smaller by border widths (need to
77 // move widget->window as well as resize because border can change)
78 gdk_window_move_resize(pizza->m_backing_window,
79 alloc->x, alloc->y, alloc->width, alloc->height);
80 gdk_window_move_resize(widget->window, border_x, border_y, w, h);
81 }
82 else
83 {
84 // one window
85 gdk_window_move_resize(widget->window,
86 alloc->x, alloc->y, alloc->width, alloc->height);
87 }
88 if (is_resize && pizza->m_backing_window)
89 {
90 // wxWidgets turns off redraw-on-allocate by default,
91 // so border area needs to be invalidated
92 if (border_x > 1 || border_y > 1)
93 {
94 if (pizza->m_is_scrollable)
95 ; // invalidate does not seem to be needed in this case
96 else
97 gdk_window_invalidate_rect(pizza->m_backing_window, NULL, false);
98 }
99 }
100 }
101 // adjust child positions
102 for (const GList* list = pizza->m_fixed.children; list; list = list->next)
103 {
104 const GtkFixedChild* child = static_cast<GtkFixedChild*>(list->data);
105 if (GTK_WIDGET_VISIBLE(child->widget))
106 {
107 GtkAllocation child_alloc;
108 // note that child positions do not take border into
109 // account, they need to be relative to widget->window,
110 // which has already been adjusted
111 child_alloc.x = child->x - pizza->m_scroll_x;
112 child_alloc.y = child->y - pizza->m_scroll_y;
113 GtkRequisition req;
114 gtk_widget_get_child_requisition(child->widget, &req);
115 child_alloc.width = req.width;
116 child_alloc.height = req.height;
117 if (gtk_widget_get_direction(widget) == GTK_TEXT_DIR_RTL)
118 {
119 child_alloc.x = w - child_alloc.x - child_alloc.width;
120 }
121 gtk_widget_size_allocate(child->widget, &child_alloc);
122 }
123 }
124 }
125
126 static void realize(GtkWidget* widget)
127 {
128 parent_class->realize(widget);
129
130 wxPizza* pizza = WX_PIZZA(widget);
131 // second window is created if there is a border, or wxWindow is scrollable
132 if (pizza->m_border_style || pizza->m_is_scrollable)
133 {
134 GdkWindowAttr attr;
135 attr.event_mask = pizza->m_is_scrollable ? 0 : GDK_EXPOSURE_MASK;
136 attr.x = widget->allocation.x;
137 attr.y = widget->allocation.y;
138 attr.width = widget->allocation.width;
139 attr.height = widget->allocation.height;
140 int border_x, border_y;
141 pizza->get_border_widths(border_x, border_y);
142 int w = widget->allocation.width - 2 * border_x;
143 int h = widget->allocation.height - 2 * border_y;
144 if (w < 0) w = 0;
145 if (h < 0) h = 0;
146 if (pizza->m_is_scrollable)
147 {
148 attr.x += border_x;
149 attr.y += border_y;
150 attr.width = w;
151 attr.height = h;
152 }
153 attr.wclass = GDK_INPUT_OUTPUT;
154 attr.visual = gtk_widget_get_visual(widget);
155 attr.colormap = gtk_widget_get_colormap(widget);
156 attr.window_type = GDK_WINDOW_CHILD;
157
158 pizza->m_backing_window = gdk_window_new(
159 gdk_window_get_parent(widget->window),
160 &attr,
161 GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP);
162
163 gdk_window_set_user_data(pizza->m_backing_window, widget);
164 if (pizza->m_is_scrollable)
165 gdk_window_reparent(widget->window, pizza->m_backing_window, 0, 0);
166 else
167 gdk_window_reparent(widget->window, pizza->m_backing_window, border_x, border_y);
168 gdk_window_resize(widget->window, w, h);
169
170 // Parts of m_backing_window which are supposed to be obscured may be
171 // exposed temporarily while resizing. Setting the backing pixmap to
172 // None prevents those areas from being briefly painted black.
173 gdk_window_set_back_pixmap(pizza->m_backing_window, NULL, false);
174 }
175 }
176
177 static void unrealize(GtkWidget* widget)
178 {
179 parent_class->unrealize(widget);
180
181 wxPizza* pizza = WX_PIZZA(widget);
182 if (pizza->m_backing_window)
183 {
184 gdk_window_set_user_data(pizza->m_backing_window, NULL);
185 gdk_window_destroy(pizza->m_backing_window);
186 pizza->m_backing_window = NULL;
187 }
188 }
189
190 static void map(GtkWidget* widget)
191 {
192 parent_class->map(widget);
193
194 wxPizza* pizza = WX_PIZZA(widget);
195 if (pizza->m_backing_window)
196 gdk_window_show(pizza->m_backing_window);
197 }
198
199 static void unmap(GtkWidget* widget)
200 {
201 parent_class->unmap(widget);
202
203 wxPizza* pizza = WX_PIZZA(widget);
204 if (pizza->m_backing_window)
205 gdk_window_hide(pizza->m_backing_window);
206 }
207
208 // not used, but needs to exist so gtk_widget_set_scroll_adjustments will work
209 static void set_scroll_adjustments(GtkWidget*, GtkAdjustment*, GtkAdjustment*)
210 {
211 }
212
213 // Marshaller needed for set_scroll_adjustments signal,
214 // generated with GLib-2.4.6 glib-genmarshal
215 #define g_marshal_value_peek_object(v) g_value_get_object (v)
216 static void
217 g_cclosure_user_marshal_VOID__OBJECT_OBJECT (GClosure *closure,
218 GValue * /*return_value*/,
219 guint n_param_values,
220 const GValue *param_values,
221 gpointer /*invocation_hint*/,
222 gpointer marshal_data)
223 {
224 typedef void (*GMarshalFunc_VOID__OBJECT_OBJECT) (gpointer data1,
225 gpointer arg_1,
226 gpointer arg_2,
227 gpointer data2);
228 register GMarshalFunc_VOID__OBJECT_OBJECT callback;
229 register GCClosure *cc = (GCClosure*) closure;
230 register gpointer data1, data2;
231
232 g_return_if_fail (n_param_values == 3);
233
234 if (G_CCLOSURE_SWAP_DATA (closure))
235 {
236 data1 = closure->data;
237 data2 = g_value_peek_pointer (param_values + 0);
238 }
239 else
240 {
241 data1 = g_value_peek_pointer (param_values + 0);
242 data2 = closure->data;
243 }
244 callback = (GMarshalFunc_VOID__OBJECT_OBJECT) (marshal_data ? marshal_data : cc->callback);
245
246 callback (data1,
247 g_marshal_value_peek_object (param_values + 1),
248 g_marshal_value_peek_object (param_values + 2),
249 data2);
250 }
251
252 static void class_init(void* g_class, void*)
253 {
254 GtkWidgetClass* widget_class = (GtkWidgetClass*)g_class;
255 widget_class->size_allocate = size_allocate;
256 widget_class->realize = realize;
257 widget_class->unrealize = unrealize;
258 widget_class->map = map;
259 widget_class->unmap = unmap;
260 wxPizzaClass* klass = (wxPizzaClass*)g_class;
261
262 // needed to make widget appear scrollable to GTK+
263 klass->set_scroll_adjustments = set_scroll_adjustments;
264 widget_class->set_scroll_adjustments_signal =
265 g_signal_new(
266 "set_scroll_adjustments",
267 G_TYPE_FROM_CLASS(g_class),
268 G_SIGNAL_RUN_LAST,
269 G_STRUCT_OFFSET(wxPizzaClass, set_scroll_adjustments),
270 NULL, NULL,
271 g_cclosure_user_marshal_VOID__OBJECT_OBJECT,
272 G_TYPE_NONE, 2, GTK_TYPE_ADJUSTMENT, GTK_TYPE_ADJUSTMENT);
273
274 parent_class = GTK_WIDGET_CLASS(g_type_class_peek_parent(g_class));
275 }
276
277 } // extern "C"
278
279 GType wxPizza::type()
280 {
281 static GtkType type;
282 if (type == 0)
283 {
284 const GTypeInfo info = {
285 sizeof(wxPizzaClass),
286 NULL, NULL,
287 class_init,
288 NULL, NULL,
289 sizeof(wxPizza), 0,
290 NULL, NULL
291 };
292 type = g_type_register_static(
293 GTK_TYPE_FIXED, "wxPizza", &info, GTypeFlags(0));
294 }
295 return type;
296 }
297
298 GtkWidget* wxPizza::New(long windowStyle)
299 {
300 GtkWidget* widget = GTK_WIDGET(g_object_new(type(), NULL));
301 wxPizza* pizza = WX_PIZZA(widget);
302 pizza->m_backing_window = NULL;
303 pizza->m_scroll_x = 0;
304 pizza->m_scroll_y = 0;
305 pizza->m_is_scrollable = (windowStyle & (wxHSCROLL | wxVSCROLL)) != 0;
306 // mask off border styles not useable with wxPizza
307 pizza->m_border_style = int(windowStyle & BORDER_STYLES);
308 gtk_fixed_set_has_window(GTK_FIXED(widget), true);
309 gtk_widget_add_events(widget,
310 GDK_EXPOSURE_MASK |
311 GDK_SCROLL_MASK |
312 GDK_POINTER_MOTION_MASK |
313 GDK_POINTER_MOTION_HINT_MASK |
314 GDK_BUTTON_MOTION_MASK |
315 GDK_BUTTON1_MOTION_MASK |
316 GDK_BUTTON2_MOTION_MASK |
317 GDK_BUTTON3_MOTION_MASK |
318 GDK_BUTTON_PRESS_MASK |
319 GDK_BUTTON_RELEASE_MASK |
320 GDK_KEY_PRESS_MASK |
321 GDK_KEY_RELEASE_MASK |
322 GDK_ENTER_NOTIFY_MASK |
323 GDK_LEAVE_NOTIFY_MASK |
324 GDK_FOCUS_CHANGE_MASK);
325 return widget;
326 }
327
328 // gtk_fixed_move does not check for a change before issuing a queue_resize,
329 // we need to avoid that to prevent endless sizing loops, so check first
330 void wxPizza::move(GtkWidget* widget, int x, int y)
331 {
332 GtkFixed* fixed = &m_fixed;
333 for (const GList* list = fixed->children; list; list = list->next)
334 {
335 const GtkFixedChild* child = static_cast<GtkFixedChild*>(list->data);
336 if (child->widget == widget)
337 {
338 if (child->x != x || child->y != y)
339 gtk_fixed_move(fixed, widget, x, y);
340 break;
341 }
342 }
343 }
344
345 struct AdjustData {
346 GdkWindow* window;
347 int dx, dy;
348 };
349
350 // Adjust allocations for all widgets using the GdkWindow which was just scrolled
351 extern "C" {
352 static void scroll_adjust(GtkWidget* widget, void* data)
353 {
354 const AdjustData* p = static_cast<AdjustData*>(data);
355 if (widget->window == p->window)
356 {
357 widget->allocation.x += p->dx;
358 widget->allocation.y += p->dy;
359 // GtkFrame requires a queue_resize, otherwise parts of
360 // the frame newly exposed by the scroll are not drawn.
361 // To be safe, do it for all widgets.
362 gtk_widget_queue_resize_no_redraw(widget);
363 if (GTK_IS_CONTAINER(widget))
364 gtk_container_forall(GTK_CONTAINER(widget), scroll_adjust, data);
365 }
366 }
367 }
368
369 void wxPizza::scroll(int dx, int dy)
370 {
371 GtkWidget* widget = GTK_WIDGET(this);
372 if (gtk_widget_get_direction(widget) == GTK_TEXT_DIR_RTL)
373 dx = -dx;
374 m_scroll_x -= dx;
375 m_scroll_y -= dy;
376 if (widget->window)
377 {
378 gdk_window_scroll(widget->window, dx, dy);
379 // Adjust child allocations. Doing a queue_resize on the children is not
380 // enough, sometimes they redraw in the wrong place during fast scrolling.
381 AdjustData data = { widget->window, dx, dy };
382 gtk_container_forall(GTK_CONTAINER(widget), scroll_adjust, &data);
383 }
384 }
385
386 void wxPizza::get_border_widths(int& x, int& y)
387 {
388 x = y = 0;
389 if (m_border_style & wxBORDER_SIMPLE)
390 x = y = 1;
391 else if (m_border_style)
392 {
393 GtkWidget *entry_widget = wxGTKPrivate::GetEntryWidget();
394 if (entry_widget->style)
395 {
396 x = entry_widget->style->xthickness;
397 y = entry_widget->style->ythickness;
398 }
399 }
400 }