]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/win_gtk.cpp
another 64 bit fix: make SendMsg() return wxIntPtr instead of long too
[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/win_gtk.h"
12
13 /*
14 wxPizza is a custom GTK+ widget derived from GtkFixed. A custom widget
15 is needed to adapt GTK+ to wxWidgets needs in 3 areas: scrolling, window
16 borders, and RTL.
17
18 For scrolling, the "set_scroll_adjustments" signal is implemented
19 to make wxPizza appear scrollable to GTK+, allowing it to be put in a
20 GtkScrolledWindow. Child widget positions are adjusted for the scrolling
21 position in size_allocate. A second same-size GdkWindow is placed behind
22 widget->window, to allow GDK to use a more efficient scrolling technique.
23
24 For borders, space is reserved in realize and size_allocate.
25 For non-scrolling wxWindows, a second GdkWindow is used for the
26 border; scrolling wxWindows draw their border on wxPizza's parent
27 (a GtkScrolledWindow) GdkWindow. Border widths for sunken and raised
28 styles are taken from widget->style, so they will match the current theme.
29
30 For RTL, child widget positions are mirrored in size_allocate.
31 */
32
33 static GtkWidgetClass* parent_class;
34
35 extern "C" {
36
37 struct wxPizzaClass
38 {
39 GtkFixedClass parent;
40 void (*set_scroll_adjustments)(GtkWidget*, GtkAdjustment*, GtkAdjustment*);
41 };
42
43 static void size_allocate(GtkWidget* widget, GtkAllocation* alloc)
44 {
45 const bool is_resize =
46 widget->allocation.width != alloc->width ||
47 widget->allocation.height != alloc->height;
48 const bool is_move =
49 widget->allocation.x != alloc->x ||
50 widget->allocation.y != alloc->y;
51
52 widget->allocation = *alloc;
53
54 wxPizza* pizza = WX_PIZZA(widget);
55 int border_x, border_y;
56 pizza->get_border_widths(border_x, border_y);
57 int w = alloc->width - 2 * border_x;
58 if (w < 0) w = 0;
59
60 if (GTK_WIDGET_REALIZED(widget) && (is_move || is_resize))
61 {
62 int h = alloc->height - 2 * border_y;
63 if (h < 0) h = 0;
64
65 if (pizza->m_is_scrollable)
66 {
67 // two windows, both same size
68 gdk_window_move_resize(pizza->m_backing_window,
69 alloc->x + border_x, alloc->y + border_y, w, h);
70 if (is_resize)
71 gdk_window_resize(widget->window, w, h);
72 }
73 else if (pizza->m_backing_window)
74 {
75 // two windows, widget->window is smaller by border widths (need to
76 // move widget->window as well as resize because border can change)
77 gdk_window_move_resize(pizza->m_backing_window,
78 alloc->x, alloc->y, alloc->width, alloc->height);
79 gdk_window_move_resize(widget->window, border_x, border_y, w, h);
80 }
81 else
82 {
83 // one window
84 gdk_window_move_resize(widget->window,
85 alloc->x, alloc->y, alloc->width, alloc->height);
86 }
87 if (is_resize && pizza->m_backing_window)
88 {
89 // wxWidgets turns off redraw-on-allocate by default,
90 // so border area needs to be invalidated
91 if (border_x > 1 || border_y > 1)
92 {
93 if (pizza->m_is_scrollable)
94 ; // invalidate does not seem to be needed in this case
95 else
96 gdk_window_invalidate_rect(pizza->m_backing_window, NULL, false);
97 }
98 }
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_alloc;
107 // note that child positions do not take border into
108 // account, they need to be relative to widget->window,
109 // which has already been adjusted
110 child_alloc.x = child->x - pizza->m_scroll_x;
111 child_alloc.y = child->y - pizza->m_scroll_y;
112 GtkRequisition req;
113 gtk_widget_get_child_requisition(child->widget, &req);
114 child_alloc.width = req.width;
115 child_alloc.height = req.height;
116 if (gtk_widget_get_direction(widget) == GTK_TEXT_DIR_RTL)
117 {
118 child_alloc.x = w - child_alloc.x - child_alloc.width;
119 }
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 // second window is created if there is a border, or wxWindow is scrollable
131 if (pizza->m_border_style || pizza->m_is_scrollable)
132 {
133 GdkWindowAttr attr;
134 attr.event_mask = pizza->m_is_scrollable ? 0 : GDK_EXPOSURE_MASK;
135 attr.x = widget->allocation.x;
136 attr.y = widget->allocation.y;
137 attr.width = widget->allocation.width;
138 attr.height = widget->allocation.height;
139 int border_x, border_y;
140 pizza->get_border_widths(border_x, border_y);
141 int w = widget->allocation.width - 2 * border_x;
142 int h = widget->allocation.height - 2 * border_y;
143 if (w < 0) w = 0;
144 if (h < 0) h = 0;
145 if (pizza->m_is_scrollable)
146 {
147 attr.x += border_x;
148 attr.y += border_y;
149 attr.width = w;
150 attr.height = h;
151 }
152 attr.wclass = GDK_INPUT_OUTPUT;
153 attr.visual = gtk_widget_get_visual(widget);
154 attr.colormap = gtk_widget_get_colormap(widget);
155 attr.window_type = GDK_WINDOW_CHILD;
156
157 pizza->m_backing_window = gdk_window_new(
158 gdk_window_get_parent(widget->window),
159 &attr,
160 GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP);
161
162 gdk_window_set_user_data(pizza->m_backing_window, widget);
163 if (pizza->m_is_scrollable)
164 gdk_window_reparent(widget->window, pizza->m_backing_window, 0, 0);
165 else
166 gdk_window_reparent(widget->window, pizza->m_backing_window, border_x, border_y);
167 gdk_window_resize(widget->window, w, h);
168
169 // Parts of m_backing_window which are supposed to be obscured may be
170 // exposed temporarily while resizing. Setting the backing pixmap to
171 // None prevents those areas from being briefly painted black.
172 gdk_window_set_back_pixmap(pizza->m_backing_window, NULL, false);
173 }
174 }
175
176 static void unrealize(GtkWidget* widget)
177 {
178 parent_class->unrealize(widget);
179
180 wxPizza* pizza = WX_PIZZA(widget);
181 if (pizza->m_backing_window)
182 {
183 gdk_window_set_user_data(pizza->m_backing_window, NULL);
184 gdk_window_destroy(pizza->m_backing_window);
185 pizza->m_backing_window = NULL;
186 }
187 }
188
189 static void map(GtkWidget* widget)
190 {
191 parent_class->map(widget);
192
193 wxPizza* pizza = WX_PIZZA(widget);
194 if (pizza->m_backing_window)
195 gdk_window_show(pizza->m_backing_window);
196 }
197
198 static void unmap(GtkWidget* widget)
199 {
200 parent_class->unmap(widget);
201
202 wxPizza* pizza = WX_PIZZA(widget);
203 if (pizza->m_backing_window)
204 gdk_window_hide(pizza->m_backing_window);
205 }
206
207 // not used, but needs to exist so gtk_widget_set_scroll_adjustments will work
208 static void set_scroll_adjustments(GtkWidget*, GtkAdjustment*, GtkAdjustment*)
209 {
210 }
211
212 // Marshaller needed for set_scroll_adjustments signal,
213 // generated with GLib-2.4.6 glib-genmarshal
214 #define g_marshal_value_peek_object(v) g_value_get_object (v)
215 static void
216 g_cclosure_user_marshal_VOID__OBJECT_OBJECT (GClosure *closure,
217 GValue * /*return_value*/,
218 guint n_param_values,
219 const GValue *param_values,
220 gpointer /*invocation_hint*/,
221 gpointer marshal_data)
222 {
223 typedef void (*GMarshalFunc_VOID__OBJECT_OBJECT) (gpointer data1,
224 gpointer arg_1,
225 gpointer arg_2,
226 gpointer data2);
227 register GMarshalFunc_VOID__OBJECT_OBJECT callback;
228 register GCClosure *cc = (GCClosure*) closure;
229 register gpointer data1, data2;
230
231 g_return_if_fail (n_param_values == 3);
232
233 if (G_CCLOSURE_SWAP_DATA (closure))
234 {
235 data1 = closure->data;
236 data2 = g_value_peek_pointer (param_values + 0);
237 }
238 else
239 {
240 data1 = g_value_peek_pointer (param_values + 0);
241 data2 = closure->data;
242 }
243 callback = (GMarshalFunc_VOID__OBJECT_OBJECT) (marshal_data ? marshal_data : cc->callback);
244
245 callback (data1,
246 g_marshal_value_peek_object (param_values + 1),
247 g_marshal_value_peek_object (param_values + 2),
248 data2);
249 }
250
251 static void class_init(void* g_class, void*)
252 {
253 GtkWidgetClass* widget_class = (GtkWidgetClass*)g_class;
254 widget_class->size_allocate = size_allocate;
255 widget_class->realize = realize;
256 widget_class->unrealize = unrealize;
257 widget_class->map = map;
258 widget_class->unmap = unmap;
259 wxPizzaClass* klass = (wxPizzaClass*)g_class;
260
261 // needed to make widget appear scrollable to GTK+
262 klass->set_scroll_adjustments = set_scroll_adjustments;
263 widget_class->set_scroll_adjustments_signal =
264 g_signal_new(
265 "set_scroll_adjustments",
266 G_TYPE_FROM_CLASS(g_class),
267 G_SIGNAL_RUN_LAST,
268 G_STRUCT_OFFSET(wxPizzaClass, set_scroll_adjustments),
269 NULL, NULL,
270 g_cclosure_user_marshal_VOID__OBJECT_OBJECT,
271 G_TYPE_NONE, 2, GTK_TYPE_ADJUSTMENT, GTK_TYPE_ADJUSTMENT);
272
273 parent_class = GTK_WIDGET_CLASS(g_type_class_peek_parent(g_class));
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 if (widget->window == p->window)
355 {
356 widget->allocation.x += p->dx;
357 widget->allocation.y += p->dy;
358 // GtkFrame requires a queue_resize, otherwise parts of
359 // the frame newly exposed by the scroll are not drawn.
360 // To be safe, do it for all widgets.
361 gtk_widget_queue_resize_no_redraw(widget);
362 if (GTK_IS_CONTAINER(widget))
363 gtk_container_forall(GTK_CONTAINER(widget), scroll_adjust, data);
364 }
365 }
366 }
367
368 void wxPizza::scroll(int dx, int dy)
369 {
370 GtkWidget* widget = GTK_WIDGET(this);
371 if (gtk_widget_get_direction(widget) == GTK_TEXT_DIR_RTL)
372 dx = -dx;
373 m_scroll_x -= dx;
374 m_scroll_y -= dy;
375 if (widget->window)
376 {
377 gdk_window_scroll(widget->window, dx, dy);
378 // Adjust child allocations. Doing a queue_resize on the children is not
379 // enough, sometimes they redraw in the wrong place during fast scrolling.
380 AdjustData data = { widget->window, dx, dy };
381 gtk_container_forall(GTK_CONTAINER(widget), scroll_adjust, &data);
382 }
383 }
384
385 extern GtkWidget *GetEntryWidget();
386
387 void wxPizza::get_border_widths(int& x, int& y)
388 {
389 x = y = 0;
390 if (m_border_style & wxBORDER_SIMPLE)
391 x = y = 1;
392 else if (m_border_style)
393 {
394 GtkWidget *entry_widget = GetEntryWidget();
395 if (entry_widget->style)
396 {
397 x = entry_widget->style->xthickness;
398 y = entry_widget->style->ythickness;
399 }
400 }
401 }