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