]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/win_gtk.cpp
unifying CFTypes
[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"
11#include "wx/gtk/win_gtk.h"
12
13/*
14wxPizza is a custom GTK+ widget derived from GtkFixed. A custom widget
15is needed to adapt GTK+ to wxWidgets needs in 3 areas: scrolling, window
16borders, and RTL.
17
18For scrolling, the "set_scroll_adjustments" signal is implemented
19to make wxPizza appear scrollable to GTK+, allowing it to be put in a
20GtkScrolledWindow. Child widget positions are adjusted for the scrolling
21position in size_allocate. A second same-size GdkWindow is placed behind
22widget->window, to allow GDK to use a more efficient scrolling technique.
23
24For borders, space is reserved in realize and size_allocate.
25For non-scrolling wxWindows, a second GdkWindow is used for the
26border; scrolling wxWindows draw their border on wxPizza's parent
27(a GtkScrolledWindow) GdkWindow. Border widths for sunken and raised
28styles are taken from widget->style, so they will match the current theme.
29
30For RTL, child widget positions are mirrored in size_allocate.
31*/
32
33static GtkWidgetClass* parent_class;
34
35extern "C" {
36
37struct wxPizzaClass
38{
39 GtkFixedClass parent;
40 void (*set_scroll_adjustments)(GtkWidget*, GtkAdjustment*, GtkAdjustment*);
41};
42
43static 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 // backing window is inside border
68 gdk_window_move_resize(pizza->m_backing_window,
69 alloc->x + border_x, alloc->y + border_y, w, h);
70 }
71 else
72 {
73 // border window (or only window if
74 // no-scroll no-border) is full size
75 GdkWindow* window = pizza->m_backing_window;
76 if (window == NULL)
77 window = widget->window;
78 gdk_window_move_resize(
79 window, alloc->x, alloc->y, alloc->width, alloc->height);
80 }
81 if (is_resize && pizza->m_backing_window)
82 {
83 // main window is inside border
84 if (pizza->m_is_scrollable)
85 gdk_window_resize(widget->window, w, h);
86 else
87 // need move as well as resize because border can change
88 gdk_window_move_resize(widget->window, border_x, border_y, w, h);
89
90 // wxWidgets turns off redraw-on-allocate by default,
91 // so border area needs to be invalidated
92 if (border_x > 1 || border_y > 1)
93 {
94 if (pizza->m_is_scrollable)
95 ; // invalidate does not seem to be needed in this case
96 else
97 gdk_window_invalidate_rect(pizza->m_backing_window, NULL, false);
98 }
99 }
100 }
101 // adjust child positions
102 for (const GList* list = pizza->m_fixed.children; list; list = list->next)
103 {
104 const GtkFixedChild* child = static_cast<GtkFixedChild*>(list->data);
105 if (GTK_WIDGET_VISIBLE(child->widget))
106 {
107 GtkAllocation child_alloc;
108 // note that child positions do not take border into
109 // account, they need to be relative to widget->window,
110 // which has already been adjusted
111 child_alloc.x = child->x - pizza->m_scroll_x;
112 child_alloc.y = child->y - pizza->m_scroll_y;
113 GtkRequisition req;
114 gtk_widget_get_child_requisition(child->widget, &req);
115 child_alloc.width = req.width;
116 child_alloc.height = req.height;
117 if (gtk_widget_get_direction(widget) == GTK_TEXT_DIR_RTL)
118 {
119 child_alloc.x = w - child_alloc.x - child_alloc.width;
120 }
121 gtk_widget_size_allocate(child->widget, &child_alloc);
122 }
123 }
124}
125
126static void realize(GtkWidget* widget)
127{
128 parent_class->realize(widget);
129
130 wxPizza* pizza = WX_PIZZA(widget);
131 // second window is created if there is a border, or wxWindow is scrollable
132 if (pizza->m_border_style || pizza->m_is_scrollable)
133 {
134 GdkWindowAttr attr;
135 attr.event_mask = pizza->m_is_scrollable ? 0 : GDK_EXPOSURE_MASK;
136 attr.x = widget->allocation.x;
137 attr.y = widget->allocation.y;
138 attr.width = widget->allocation.width;
139 attr.height = widget->allocation.height;
140 int border_x, border_y;
141 pizza->get_border_widths(border_x, border_y);
142 int w = widget->allocation.width - 2 * border_x;
143 int h = widget->allocation.height - 2 * border_y;
144 if (w < 0) w = 0;
145 if (h < 0) h = 0;
146 if (pizza->m_is_scrollable)
147 {
148 attr.x += border_x;
149 attr.y += border_y;
150 attr.width = w;
151 attr.height = h;
152 }
153 attr.wclass = GDK_INPUT_OUTPUT;
154 attr.visual = gtk_widget_get_visual(widget);
155 attr.colormap = gtk_widget_get_colormap(widget);
156 attr.window_type = GDK_WINDOW_CHILD;
157
158 pizza->m_backing_window = gdk_window_new(
159 gdk_window_get_parent(widget->window),
160 &attr,
161 GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP);
162
163 gdk_window_set_user_data(pizza->m_backing_window, widget);
164 if (pizza->m_is_scrollable)
165 gdk_window_reparent(widget->window, pizza->m_backing_window, 0, 0);
166 else
167 gdk_window_reparent(widget->window, pizza->m_backing_window, border_x, border_y);
168 gdk_window_resize(widget->window, w, h);
169 }
170}
171
172static void unrealize(GtkWidget* widget)
173{
174 parent_class->unrealize(widget);
175
176 wxPizza* pizza = WX_PIZZA(widget);
177 if (pizza->m_backing_window)
178 {
179 gdk_window_set_user_data(pizza->m_backing_window, NULL);
180 gdk_window_destroy(pizza->m_backing_window);
181 pizza->m_backing_window = NULL;
182 }
183}
184
185static void map(GtkWidget* widget)
186{
187 parent_class->map(widget);
188
189 wxPizza* pizza = WX_PIZZA(widget);
190 if (pizza->m_backing_window)
191 gdk_window_show(pizza->m_backing_window);
192}
193
194static void unmap(GtkWidget* widget)
195{
196 parent_class->unmap(widget);
197
198 wxPizza* pizza = WX_PIZZA(widget);
199 if (pizza->m_backing_window)
200 gdk_window_hide(pizza->m_backing_window);
201}
202
203// not used, but needs to exist so gtk_widget_set_scroll_adjustments will work
204static 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)
211static void
212g_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
247static 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
274GType 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
293GtkWidget* wxPizza::New(long windowStyle)
294{
295 GtkWidget* widget = GTK_WIDGET(g_object_new(type(), NULL));
296 wxPizza* pizza = WX_PIZZA(widget);
297 pizza->m_backing_window = NULL;
298 pizza->m_scroll_x = 0;
299 pizza->m_scroll_y = 0;
300 pizza->m_is_scrollable = (windowStyle & (wxHSCROLL | wxVSCROLL)) != 0;
59d09ad4
PC
301 // mask off border styles not useable with wxPizza
302 pizza->m_border_style = int(windowStyle & BORDER_STYLES);
e56307d3
PC
303 gtk_fixed_set_has_window(GTK_FIXED(widget), true);
304 gtk_widget_add_events(widget,
305 GDK_EXPOSURE_MASK |
306 GDK_SCROLL_MASK |
307 GDK_POINTER_MOTION_MASK |
308 GDK_POINTER_MOTION_HINT_MASK |
309 GDK_BUTTON_MOTION_MASK |
310 GDK_BUTTON1_MOTION_MASK |
311 GDK_BUTTON2_MOTION_MASK |
312 GDK_BUTTON3_MOTION_MASK |
313 GDK_BUTTON_PRESS_MASK |
314 GDK_BUTTON_RELEASE_MASK |
315 GDK_KEY_PRESS_MASK |
316 GDK_KEY_RELEASE_MASK |
317 GDK_ENTER_NOTIFY_MASK |
318 GDK_LEAVE_NOTIFY_MASK |
319 GDK_FOCUS_CHANGE_MASK);
e56307d3
PC
320 return widget;
321}
322
323// gtk_fixed_move does not check for a change before issuing a queue_resize,
324// we need to avoid that to prevent endless sizing loops, so check first
325void wxPizza::move(GtkWidget* widget, int x, int y)
326{
327 GtkFixed* fixed = &m_fixed;
328 for (const GList* list = fixed->children; list; list = list->next)
329 {
330 const GtkFixedChild* child = static_cast<GtkFixedChild*>(list->data);
331 if (child->widget == widget)
332 {
333 if (child->x != x || child->y != y)
334 gtk_fixed_move(fixed, widget, x, y);
335 break;
336 }
337 }
338}
339
340void wxPizza::scroll(int dx, int dy)
341{
342 GtkWidget* widget = GTK_WIDGET(this);
343 if (gtk_widget_get_direction(widget) == GTK_TEXT_DIR_RTL)
344 dx = -dx;
345 m_scroll_x -= dx;
346 m_scroll_y -= dy;
347 if (widget->window)
348 gdk_window_scroll(widget->window, dx, dy);
349 const GList* list = m_fixed.children;
350 if (list)
351 {
352 const GtkFixedChild* child = static_cast<GtkFixedChild*>(list->data);
353 // queueing a resize on any child will update them all
354 gtk_widget_queue_resize(child->widget);
355 }
356}
357
6de67633
RR
358extern GtkWidget *GetEntryWidget();
359
e56307d3
PC
360void wxPizza::get_border_widths(int& x, int& y)
361{
362 x = y = 0;
363 if (m_border_style & wxBORDER_SIMPLE)
364 x = y = 1;
365 else if (m_border_style)
366 {
6de67633
RR
367 GtkWidget *entry_widget = GetEntryWidget();
368 if (entry_widget->style)
e56307d3 369 {
6de67633
RR
370 x = entry_widget->style->xthickness;
371 y = entry_widget->style->ythickness;
e56307d3
PC
372 }
373 }
374}