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