]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/win_gtk.cpp
note for future implementation using a former private API
[wxWidgets.git] / src / gtk / win_gtk.cpp
CommitLineData
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/*
15wxPizza is a custom GTK+ widget derived from GtkFixed. A custom widget
16is needed to adapt GTK+ to wxWidgets needs in 3 areas: scrolling, window
17borders, and RTL.
18
19For scrolling, the "set_scroll_adjustments" signal is implemented
20to make wxPizza appear scrollable to GTK+, allowing it to be put in a
21GtkScrolledWindow. Child widget positions are adjusted for the scrolling
f089940f 22position in size_allocate. A second same-size GdkWindow is placed in front of
e56307d3
PC
23widget->window, to allow GDK to use a more efficient scrolling technique.
24
75f661bb
PC
25For borders, space is reserved in realize and size_allocate. The border is
26drawn on wxPizza's parent GdkWindow.
e56307d3
PC
27
28For RTL, child widget positions are mirrored in size_allocate.
29*/
30
31static GtkWidgetClass* parent_class;
32
33extern "C" {
34
35struct wxPizzaClass
36{
37 GtkFixedClass parent;
38 void (*set_scroll_adjustments)(GtkWidget*, GtkAdjustment*, GtkAdjustment*);
39};
40
41static 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
f089940f 61 if (pizza->m_draw_window != widget->window)
e56307d3 62 {
4eccf53d 63 // two windows, both same size
f089940f 64 gdk_window_move_resize(widget->window,
e56307d3 65 alloc->x + border_x, alloc->y + border_y, w, h);
4eccf53d 66 if (is_resize)
f089940f 67 gdk_window_resize(pizza->m_draw_window, w, h);
4eccf53d 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
f089940f
PC
79 GdkWindow* parent = gtk_widget_get_parent_window(widget);
80 gdk_window_invalidate_rect(parent, &widget->allocation, false);
81 gdk_window_invalidate_rect(parent, alloc, false);
e66a89ed 82 }
e56307d3
PC
83 }
84 }
7456fe19
PC
85
86 widget->allocation = *alloc;
03647350 87
e56307d3
PC
88 // adjust child positions
89 for (const GList* list = pizza->m_fixed.children; list; list = list->next)
90 {
91 const GtkFixedChild* child = static_cast<GtkFixedChild*>(list->data);
92 if (GTK_WIDGET_VISIBLE(child->widget))
93 {
94 GtkAllocation child_alloc;
95 // note that child positions do not take border into
96 // account, they need to be relative to widget->window,
97 // which has already been adjusted
98 child_alloc.x = child->x - pizza->m_scroll_x;
99 child_alloc.y = child->y - pizza->m_scroll_y;
100 GtkRequisition req;
101 gtk_widget_get_child_requisition(child->widget, &req);
102 child_alloc.width = req.width;
103 child_alloc.height = req.height;
104 if (gtk_widget_get_direction(widget) == GTK_TEXT_DIR_RTL)
e56307d3 105 child_alloc.x = w - child_alloc.x - child_alloc.width;
18319143 106 gtk_widget_size_allocate(child->widget, &child_alloc);
e56307d3
PC
107 }
108 }
109}
110
111static void realize(GtkWidget* widget)
112{
113 parent_class->realize(widget);
114
115 wxPizza* pizza = WX_PIZZA(widget);
f089940f 116 pizza->m_draw_window = widget->window;
e56307d3
PC
117 if (pizza->m_border_style || pizza->m_is_scrollable)
118 {
e56307d3
PC
119 int border_x, border_y;
120 pizza->get_border_widths(border_x, border_y);
75f661bb
PC
121 int x = widget->allocation.x + border_x;
122 int y = widget->allocation.y + border_y;
e56307d3
PC
123 int w = widget->allocation.width - 2 * border_x;
124 int h = widget->allocation.height - 2 * border_y;
125 if (w < 0) w = 0;
126 if (h < 0) h = 0;
127 if (pizza->m_is_scrollable)
128 {
75f661bb
PC
129 // second window is created if wxWindow is scrollable
130 GdkWindowAttr attr;
131 attr.event_mask = 0;
132 attr.x = x;
133 attr.y = y;
e56307d3
PC
134 attr.width = w;
135 attr.height = h;
75f661bb
PC
136 attr.wclass = GDK_INPUT_OUTPUT;
137 attr.visual = gtk_widget_get_visual(widget);
138 attr.colormap = gtk_widget_get_colormap(widget);
139 attr.window_type = GDK_WINDOW_CHILD;
e56307d3 140
f089940f
PC
141 // existing widget->window becomes m_draw_window,
142 // new widget->window created as parent of m_draw_window
143 widget->window = gdk_window_new(
75f661bb
PC
144 gdk_window_get_parent(widget->window),
145 &attr,
146 GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP);
e56307d3 147
f089940f
PC
148 gdk_window_set_user_data(widget->window, widget);
149 gdk_window_reparent(pizza->m_draw_window, widget->window, 0, 0);
150 gdk_window_resize(pizza->m_draw_window, w, h);
4eccf53d 151
1827c0f5 152 // Parts of backing window may be exposed temporarily while
75f661bb
PC
153 // resizing. Setting the backing pixmap to None prevents those
154 // areas from being briefly painted black.
f089940f
PC
155 gdk_window_set_back_pixmap(widget->window, NULL, false);
156
157 for (const GList* list = pizza->m_fixed.children; list; list = list->next)
158 {
159 const GtkFixedChild* child = static_cast<GtkFixedChild*>(list->data);
160 gtk_widget_set_parent_window(child->widget, pizza->m_draw_window);
161 }
75f661bb
PC
162 }
163 else
164 gdk_window_move_resize(widget->window, x, y, w, h);
e56307d3
PC
165 }
166}
167
168static void unrealize(GtkWidget* widget)
169{
f089940f
PC
170 wxPizza* pizza = WX_PIZZA(widget);
171 if (pizza->m_draw_window != widget->window)
e56307d3 172 {
f089940f
PC
173 gdk_window_set_user_data(pizza->m_draw_window, NULL);
174 gdk_window_destroy(pizza->m_draw_window);
e56307d3 175 }
f089940f
PC
176 pizza->m_draw_window = NULL;
177
178 parent_class->unrealize(widget);
e56307d3
PC
179}
180
181static void map(GtkWidget* widget)
182{
1827c0f5 183 parent_class->map(widget);
f089940f
PC
184
185 GdkWindow* window = WX_PIZZA(widget)->m_draw_window;
186 if (window != widget->window)
187 gdk_window_show(window);
e56307d3
PC
188}
189
190static void unmap(GtkWidget* widget)
191{
f089940f
PC
192 GdkWindow* window = WX_PIZZA(widget)->m_draw_window;
193 if (window != widget->window)
194 gdk_window_hide(window);
1827c0f5 195
e56307d3 196 parent_class->unmap(widget);
e56307d3
PC
197}
198
199// not used, but needs to exist so gtk_widget_set_scroll_adjustments will work
200static void set_scroll_adjustments(GtkWidget*, GtkAdjustment*, GtkAdjustment*)
201{
202}
203
204// Marshaller needed for set_scroll_adjustments signal,
205// generated with GLib-2.4.6 glib-genmarshal
206#define g_marshal_value_peek_object(v) g_value_get_object (v)
207static void
208g_cclosure_user_marshal_VOID__OBJECT_OBJECT (GClosure *closure,
209 GValue * /*return_value*/,
210 guint n_param_values,
211 const GValue *param_values,
212 gpointer /*invocation_hint*/,
213 gpointer marshal_data)
214{
215 typedef void (*GMarshalFunc_VOID__OBJECT_OBJECT) (gpointer data1,
216 gpointer arg_1,
217 gpointer arg_2,
218 gpointer data2);
219 register GMarshalFunc_VOID__OBJECT_OBJECT callback;
220 register GCClosure *cc = (GCClosure*) closure;
221 register gpointer data1, data2;
222
223 g_return_if_fail (n_param_values == 3);
224
225 if (G_CCLOSURE_SWAP_DATA (closure))
226 {
227 data1 = closure->data;
228 data2 = g_value_peek_pointer (param_values + 0);
229 }
230 else
231 {
232 data1 = g_value_peek_pointer (param_values + 0);
233 data2 = closure->data;
234 }
235 callback = (GMarshalFunc_VOID__OBJECT_OBJECT) (marshal_data ? marshal_data : cc->callback);
236
237 callback (data1,
238 g_marshal_value_peek_object (param_values + 1),
239 g_marshal_value_peek_object (param_values + 2),
240 data2);
241}
242
243static void class_init(void* g_class, void*)
244{
245 GtkWidgetClass* widget_class = (GtkWidgetClass*)g_class;
246 widget_class->size_allocate = size_allocate;
247 widget_class->realize = realize;
248 widget_class->unrealize = unrealize;
249 widget_class->map = map;
250 widget_class->unmap = unmap;
251 wxPizzaClass* klass = (wxPizzaClass*)g_class;
252
253 // needed to make widget appear scrollable to GTK+
254 klass->set_scroll_adjustments = set_scroll_adjustments;
255 widget_class->set_scroll_adjustments_signal =
256 g_signal_new(
257 "set_scroll_adjustments",
258 G_TYPE_FROM_CLASS(g_class),
259 G_SIGNAL_RUN_LAST,
260 G_STRUCT_OFFSET(wxPizzaClass, set_scroll_adjustments),
261 NULL, NULL,
262 g_cclosure_user_marshal_VOID__OBJECT_OBJECT,
263 G_TYPE_NONE, 2, GTK_TYPE_ADJUSTMENT, GTK_TYPE_ADJUSTMENT);
264
265 parent_class = GTK_WIDGET_CLASS(g_type_class_peek_parent(g_class));
266}
267
268} // extern "C"
269
270GType wxPizza::type()
271{
272 static GtkType type;
273 if (type == 0)
274 {
275 const GTypeInfo info = {
276 sizeof(wxPizzaClass),
277 NULL, NULL,
278 class_init,
279 NULL, NULL,
280 sizeof(wxPizza), 0,
281 NULL, NULL
282 };
283 type = g_type_register_static(
284 GTK_TYPE_FIXED, "wxPizza", &info, GTypeFlags(0));
285 }
286 return type;
287}
288
0f52f610 289GtkWidget* wxPizza::New(long windowStyle)
e56307d3
PC
290{
291 GtkWidget* widget = GTK_WIDGET(g_object_new(type(), NULL));
292 wxPizza* pizza = WX_PIZZA(widget);
f089940f 293 pizza->m_draw_window = NULL;
e56307d3
PC
294 pizza->m_scroll_x = 0;
295 pizza->m_scroll_y = 0;
296 pizza->m_is_scrollable = (windowStyle & (wxHSCROLL | wxVSCROLL)) != 0;
59d09ad4
PC
297 // mask off border styles not useable with wxPizza
298 pizza->m_border_style = int(windowStyle & BORDER_STYLES);
e56307d3
PC
299 gtk_fixed_set_has_window(GTK_FIXED(widget), true);
300 gtk_widget_add_events(widget,
301 GDK_EXPOSURE_MASK |
302 GDK_SCROLL_MASK |
303 GDK_POINTER_MOTION_MASK |
304 GDK_POINTER_MOTION_HINT_MASK |
305 GDK_BUTTON_MOTION_MASK |
306 GDK_BUTTON1_MOTION_MASK |
307 GDK_BUTTON2_MOTION_MASK |
308 GDK_BUTTON3_MOTION_MASK |
309 GDK_BUTTON_PRESS_MASK |
310 GDK_BUTTON_RELEASE_MASK |
311 GDK_KEY_PRESS_MASK |
312 GDK_KEY_RELEASE_MASK |
313 GDK_ENTER_NOTIFY_MASK |
314 GDK_LEAVE_NOTIFY_MASK |
315 GDK_FOCUS_CHANGE_MASK);
e56307d3
PC
316 return widget;
317}
318
319// gtk_fixed_move does not check for a change before issuing a queue_resize,
320// we need to avoid that to prevent endless sizing loops, so check first
321void wxPizza::move(GtkWidget* widget, int x, int y)
322{
323 GtkFixed* fixed = &m_fixed;
324 for (const GList* list = fixed->children; list; list = list->next)
325 {
326 const GtkFixedChild* child = static_cast<GtkFixedChild*>(list->data);
327 if (child->widget == widget)
328 {
329 if (child->x != x || child->y != y)
330 gtk_fixed_move(fixed, widget, x, y);
331 break;
332 }
333 }
334}
335
f089940f
PC
336void wxPizza::put(GtkWidget* widget, int x, int y)
337{
338 gtk_fixed_put(&m_fixed, widget, x, y);
339 if (m_draw_window != GTK_WIDGET(this)->window)
340 gtk_widget_set_parent_window(widget, m_draw_window);
341}
342
a8997071
PC
343struct AdjustData {
344 GdkWindow* window;
345 int dx, dy;
346};
347
348// Adjust allocations for all widgets using the GdkWindow which was just scrolled
349extern "C" {
350static void scroll_adjust(GtkWidget* widget, void* data)
351{
352 const AdjustData* p = static_cast<AdjustData*>(data);
eb56793e
RR
353 widget->allocation.x += p->dx;
354 widget->allocation.y += p->dy;
03647350 355
a8997071
PC
356 if (widget->window == p->window)
357 {
a8997071
PC
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
e56307d3
PC
368void 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;
f089940f 375 if (m_draw_window)
e56307d3 376 {
f089940f 377 gdk_window_scroll(m_draw_window, dx, dy);
a8997071
PC
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.
f089940f 380 AdjustData data = { m_draw_window, dx, dy };
a8997071 381 gtk_container_forall(GTK_CONTAINER(widget), scroll_adjust, &data);
e56307d3
PC
382 }
383}
384
385void wxPizza::get_border_widths(int& x, int& y)
386{
387 x = y = 0;
d7b63a01
RR
388 if (m_border_style == 0)
389 return;
03647350 390
75f661bb 391#ifndef __WXUNIVERSAL__
e56307d3
PC
392 if (m_border_style & wxBORDER_SIMPLE)
393 x = y = 1;
ec2d6790 394 else if (m_is_scrollable /* || (m_border_style & wxBORDER_THEME) */)
d7b63a01
RR
395 {
396 GtkWidget *style_widget = wxGTKPrivate::GetTreeWidget();
03647350 397
d7b63a01
RR
398 if (style_widget->style)
399 {
400 x = style_widget->style->xthickness;
401 y = style_widget->style->ythickness;
402 }
403 }
03647350 404 else
e56307d3 405 {
06a42745 406 GtkWidget *style_widget = wxGTKPrivate::GetEntryWidget();
03647350 407
06a42745 408 if (style_widget->style)
e56307d3 409 {
06a42745
RR
410 x = style_widget->style->xthickness;
411 y = style_widget->style->ythickness;
e56307d3
PC
412 }
413 }
75f661bb 414#endif
e56307d3 415}