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