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