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