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