]>
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 | ||
e762ef8f VZ |
10 | #include "wx/wxprec.h" |
11 | ||
e56307d3 | 12 | #include "wx/defs.h" |
e8759560 | 13 | #include "wx/gtk/private.h" |
bbd92d1d | 14 | #include "wx/gtk/private/win_gtk.h" |
e56307d3 PC |
15 | |
16 | /* | |
17 | wxPizza is a custom GTK+ widget derived from GtkFixed. A custom widget | |
18 | is needed to adapt GTK+ to wxWidgets needs in 3 areas: scrolling, window | |
19 | borders, and RTL. | |
20 | ||
21 | For scrolling, the "set_scroll_adjustments" signal is implemented | |
22 | to make wxPizza appear scrollable to GTK+, allowing it to be put in a | |
23 | GtkScrolledWindow. Child widget positions are adjusted for the scrolling | |
375efc1f | 24 | position in size_allocate. |
e56307d3 | 25 | |
75f661bb PC |
26 | For borders, space is reserved in realize and size_allocate. The border is |
27 | drawn on wxPizza's parent GdkWindow. | |
e56307d3 PC |
28 | |
29 | For RTL, child widget positions are mirrored in size_allocate. | |
30 | */ | |
31 | ||
32 | static GtkWidgetClass* parent_class; | |
33 | ||
34 | extern "C" { | |
35 | ||
36 | struct wxPizzaClass | |
37 | { | |
38 | GtkFixedClass parent; | |
39 | void (*set_scroll_adjustments)(GtkWidget*, GtkAdjustment*, GtkAdjustment*); | |
40 | }; | |
41 | ||
42 | static void size_allocate(GtkWidget* widget, GtkAllocation* alloc) | |
43 | { | |
e56307d3 PC |
44 | wxPizza* pizza = WX_PIZZA(widget); |
45 | int border_x, border_y; | |
46 | pizza->get_border_widths(border_x, border_y); | |
47 | int w = alloc->width - 2 * border_x; | |
48 | if (w < 0) w = 0; | |
49 | ||
030f4112 | 50 | if (gtk_widget_get_realized(widget)) |
e56307d3 PC |
51 | { |
52 | int h = alloc->height - 2 * border_y; | |
53 | if (h < 0) h = 0; | |
030f4112 PC |
54 | const int x = alloc->x + border_x; |
55 | const int y = alloc->y + border_y; | |
e56307d3 | 56 | |
030f4112 PC |
57 | GdkWindow* window = gtk_widget_get_window(widget); |
58 | int old_x, old_y; | |
59 | gdk_window_get_position(window, &old_x, &old_y); | |
375efc1f | 60 | |
030f4112 PC |
61 | if (x != old_x || y != old_y || |
62 | w != gdk_window_get_width(window) || h != gdk_window_get_height(window)) | |
e56307d3 | 63 | { |
030f4112 PC |
64 | gdk_window_move_resize(window, x, y, w, h); |
65 | ||
66 | if (border_x + border_y) | |
67 | { | |
68 | // old and new border areas need to be invalidated, | |
69 | // otherwise they will not be erased/redrawn properly | |
70 | GdkWindow* parent = gtk_widget_get_parent_window(widget); | |
71 | gdk_window_invalidate_rect(parent, &widget->allocation, false); | |
72 | gdk_window_invalidate_rect(parent, alloc, false); | |
73 | } | |
e56307d3 PC |
74 | } |
75 | } | |
7456fe19 PC |
76 | |
77 | widget->allocation = *alloc; | |
03647350 | 78 | |
e56307d3 PC |
79 | // adjust child positions |
80 | for (const GList* list = pizza->m_fixed.children; list; list = list->next) | |
81 | { | |
82 | const GtkFixedChild* child = static_cast<GtkFixedChild*>(list->data); | |
fc9ab22a | 83 | if (gtk_widget_get_visible(child->widget)) |
e56307d3 PC |
84 | { |
85 | GtkAllocation child_alloc; | |
86 | // note that child positions do not take border into | |
87 | // account, they need to be relative to widget->window, | |
88 | // which has already been adjusted | |
89 | child_alloc.x = child->x - pizza->m_scroll_x; | |
90 | child_alloc.y = child->y - pizza->m_scroll_y; | |
91 | GtkRequisition req; | |
92 | gtk_widget_get_child_requisition(child->widget, &req); | |
93 | child_alloc.width = req.width; | |
94 | child_alloc.height = req.height; | |
95 | if (gtk_widget_get_direction(widget) == GTK_TEXT_DIR_RTL) | |
e56307d3 | 96 | child_alloc.x = w - child_alloc.x - child_alloc.width; |
18319143 | 97 | gtk_widget_size_allocate(child->widget, &child_alloc); |
e56307d3 PC |
98 | } |
99 | } | |
100 | } | |
101 | ||
102 | static void realize(GtkWidget* widget) | |
103 | { | |
104 | parent_class->realize(widget); | |
105 | ||
106 | wxPizza* pizza = WX_PIZZA(widget); | |
375efc1f | 107 | if (pizza->m_border_style) |
e56307d3 | 108 | { |
e56307d3 PC |
109 | int border_x, border_y; |
110 | pizza->get_border_widths(border_x, border_y); | |
75f661bb PC |
111 | int x = widget->allocation.x + border_x; |
112 | int y = widget->allocation.y + border_y; | |
e56307d3 PC |
113 | int w = widget->allocation.width - 2 * border_x; |
114 | int h = widget->allocation.height - 2 * border_y; | |
115 | if (w < 0) w = 0; | |
116 | if (h < 0) h = 0; | |
375efc1f | 117 | gdk_window_move_resize(widget->window, x, y, w, h); |
e56307d3 | 118 | } |
e56307d3 PC |
119 | } |
120 | ||
6e7cf3bd PC |
121 | static void show(GtkWidget* widget) |
122 | { | |
123 | if (widget->parent && WX_PIZZA(widget)->m_border_style) | |
124 | { | |
125 | // invalidate whole allocation so borders will be drawn properly | |
126 | const GtkAllocation& a = widget->allocation; | |
127 | gtk_widget_queue_draw_area(widget->parent, a.x, a.y, a.width, a.height); | |
128 | } | |
129 | ||
130 | parent_class->show(widget); | |
131 | } | |
132 | ||
133 | static void hide(GtkWidget* widget) | |
134 | { | |
135 | if (widget->parent && WX_PIZZA(widget)->m_border_style) | |
136 | { | |
137 | // invalidate whole allocation so borders will be erased properly | |
138 | const GtkAllocation& a = widget->allocation; | |
139 | gtk_widget_queue_draw_area(widget->parent, a.x, a.y, a.width, a.height); | |
140 | } | |
141 | ||
142 | parent_class->hide(widget); | |
143 | } | |
144 | ||
e56307d3 PC |
145 | // not used, but needs to exist so gtk_widget_set_scroll_adjustments will work |
146 | static void set_scroll_adjustments(GtkWidget*, GtkAdjustment*, GtkAdjustment*) | |
147 | { | |
148 | } | |
149 | ||
150 | // Marshaller needed for set_scroll_adjustments signal, | |
151 | // generated with GLib-2.4.6 glib-genmarshal | |
152 | #define g_marshal_value_peek_object(v) g_value_get_object (v) | |
153 | static void | |
154 | g_cclosure_user_marshal_VOID__OBJECT_OBJECT (GClosure *closure, | |
155 | GValue * /*return_value*/, | |
156 | guint n_param_values, | |
157 | const GValue *param_values, | |
158 | gpointer /*invocation_hint*/, | |
159 | gpointer marshal_data) | |
160 | { | |
161 | typedef void (*GMarshalFunc_VOID__OBJECT_OBJECT) (gpointer data1, | |
162 | gpointer arg_1, | |
163 | gpointer arg_2, | |
164 | gpointer data2); | |
165 | register GMarshalFunc_VOID__OBJECT_OBJECT callback; | |
166 | register GCClosure *cc = (GCClosure*) closure; | |
167 | register gpointer data1, data2; | |
168 | ||
169 | g_return_if_fail (n_param_values == 3); | |
170 | ||
171 | if (G_CCLOSURE_SWAP_DATA (closure)) | |
172 | { | |
173 | data1 = closure->data; | |
174 | data2 = g_value_peek_pointer (param_values + 0); | |
175 | } | |
176 | else | |
177 | { | |
178 | data1 = g_value_peek_pointer (param_values + 0); | |
179 | data2 = closure->data; | |
180 | } | |
181 | callback = (GMarshalFunc_VOID__OBJECT_OBJECT) (marshal_data ? marshal_data : cc->callback); | |
182 | ||
183 | callback (data1, | |
184 | g_marshal_value_peek_object (param_values + 1), | |
185 | g_marshal_value_peek_object (param_values + 2), | |
186 | data2); | |
187 | } | |
188 | ||
189 | static void class_init(void* g_class, void*) | |
190 | { | |
191 | GtkWidgetClass* widget_class = (GtkWidgetClass*)g_class; | |
192 | widget_class->size_allocate = size_allocate; | |
193 | widget_class->realize = realize; | |
6e7cf3bd PC |
194 | widget_class->show = show; |
195 | widget_class->hide = hide; | |
e56307d3 PC |
196 | wxPizzaClass* klass = (wxPizzaClass*)g_class; |
197 | ||
198 | // needed to make widget appear scrollable to GTK+ | |
199 | klass->set_scroll_adjustments = set_scroll_adjustments; | |
200 | widget_class->set_scroll_adjustments_signal = | |
201 | g_signal_new( | |
202 | "set_scroll_adjustments", | |
203 | G_TYPE_FROM_CLASS(g_class), | |
204 | G_SIGNAL_RUN_LAST, | |
205 | G_STRUCT_OFFSET(wxPizzaClass, set_scroll_adjustments), | |
206 | NULL, NULL, | |
207 | g_cclosure_user_marshal_VOID__OBJECT_OBJECT, | |
208 | G_TYPE_NONE, 2, GTK_TYPE_ADJUSTMENT, GTK_TYPE_ADJUSTMENT); | |
209 | ||
210 | parent_class = GTK_WIDGET_CLASS(g_type_class_peek_parent(g_class)); | |
211 | } | |
212 | ||
213 | } // extern "C" | |
214 | ||
215 | GType wxPizza::type() | |
216 | { | |
217 | static GtkType type; | |
218 | if (type == 0) | |
219 | { | |
220 | const GTypeInfo info = { | |
221 | sizeof(wxPizzaClass), | |
222 | NULL, NULL, | |
223 | class_init, | |
224 | NULL, NULL, | |
225 | sizeof(wxPizza), 0, | |
226 | NULL, NULL | |
227 | }; | |
228 | type = g_type_register_static( | |
229 | GTK_TYPE_FIXED, "wxPizza", &info, GTypeFlags(0)); | |
230 | } | |
231 | return type; | |
232 | } | |
233 | ||
0f52f610 | 234 | GtkWidget* wxPizza::New(long windowStyle) |
e56307d3 PC |
235 | { |
236 | GtkWidget* widget = GTK_WIDGET(g_object_new(type(), NULL)); | |
237 | wxPizza* pizza = WX_PIZZA(widget); | |
e56307d3 PC |
238 | pizza->m_scroll_x = 0; |
239 | pizza->m_scroll_y = 0; | |
240 | pizza->m_is_scrollable = (windowStyle & (wxHSCROLL | wxVSCROLL)) != 0; | |
59d09ad4 PC |
241 | // mask off border styles not useable with wxPizza |
242 | pizza->m_border_style = int(windowStyle & BORDER_STYLES); | |
5073cadd | 243 | #if GTK_CHECK_VERSION(3,0,0) || defined(GTK_DISABLE_DEPRECATED) |
fc9ab22a VS |
244 | gtk_widget_set_has_window(widget, true); |
245 | #else | |
e56307d3 | 246 | gtk_fixed_set_has_window(GTK_FIXED(widget), true); |
fc9ab22a | 247 | #endif |
e56307d3 PC |
248 | gtk_widget_add_events(widget, |
249 | GDK_EXPOSURE_MASK | | |
250 | GDK_SCROLL_MASK | | |
251 | GDK_POINTER_MOTION_MASK | | |
252 | GDK_POINTER_MOTION_HINT_MASK | | |
253 | GDK_BUTTON_MOTION_MASK | | |
254 | GDK_BUTTON1_MOTION_MASK | | |
255 | GDK_BUTTON2_MOTION_MASK | | |
256 | GDK_BUTTON3_MOTION_MASK | | |
257 | GDK_BUTTON_PRESS_MASK | | |
258 | GDK_BUTTON_RELEASE_MASK | | |
259 | GDK_KEY_PRESS_MASK | | |
260 | GDK_KEY_RELEASE_MASK | | |
261 | GDK_ENTER_NOTIFY_MASK | | |
262 | GDK_LEAVE_NOTIFY_MASK | | |
263 | GDK_FOCUS_CHANGE_MASK); | |
e56307d3 PC |
264 | return widget; |
265 | } | |
266 | ||
267 | // gtk_fixed_move does not check for a change before issuing a queue_resize, | |
268 | // we need to avoid that to prevent endless sizing loops, so check first | |
269 | void wxPizza::move(GtkWidget* widget, int x, int y) | |
270 | { | |
271 | GtkFixed* fixed = &m_fixed; | |
272 | for (const GList* list = fixed->children; list; list = list->next) | |
273 | { | |
274 | const GtkFixedChild* child = static_cast<GtkFixedChild*>(list->data); | |
275 | if (child->widget == widget) | |
276 | { | |
277 | if (child->x != x || child->y != y) | |
278 | gtk_fixed_move(fixed, widget, x, y); | |
279 | break; | |
280 | } | |
281 | } | |
282 | } | |
283 | ||
f089940f PC |
284 | void wxPizza::put(GtkWidget* widget, int x, int y) |
285 | { | |
286 | gtk_fixed_put(&m_fixed, widget, x, y); | |
f089940f PC |
287 | } |
288 | ||
a8997071 PC |
289 | struct AdjustData { |
290 | GdkWindow* window; | |
291 | int dx, dy; | |
292 | }; | |
293 | ||
294 | // Adjust allocations for all widgets using the GdkWindow which was just scrolled | |
295 | extern "C" { | |
296 | static void scroll_adjust(GtkWidget* widget, void* data) | |
297 | { | |
298 | const AdjustData* p = static_cast<AdjustData*>(data); | |
eb56793e RR |
299 | widget->allocation.x += p->dx; |
300 | widget->allocation.y += p->dy; | |
03647350 | 301 | |
a8997071 PC |
302 | if (widget->window == p->window) |
303 | { | |
a8997071 PC |
304 | // GtkFrame requires a queue_resize, otherwise parts of |
305 | // the frame newly exposed by the scroll are not drawn. | |
306 | // To be safe, do it for all widgets. | |
307 | gtk_widget_queue_resize_no_redraw(widget); | |
308 | if (GTK_IS_CONTAINER(widget)) | |
309 | gtk_container_forall(GTK_CONTAINER(widget), scroll_adjust, data); | |
310 | } | |
311 | } | |
312 | } | |
313 | ||
e56307d3 PC |
314 | void wxPizza::scroll(int dx, int dy) |
315 | { | |
316 | GtkWidget* widget = GTK_WIDGET(this); | |
317 | if (gtk_widget_get_direction(widget) == GTK_TEXT_DIR_RTL) | |
318 | dx = -dx; | |
319 | m_scroll_x -= dx; | |
320 | m_scroll_y -= dy; | |
375efc1f | 321 | if (widget->window) |
e56307d3 | 322 | { |
375efc1f | 323 | gdk_window_scroll(widget->window, dx, dy); |
a8997071 PC |
324 | // Adjust child allocations. Doing a queue_resize on the children is not |
325 | // enough, sometimes they redraw in the wrong place during fast scrolling. | |
375efc1f | 326 | AdjustData data = { widget->window, dx, dy }; |
a8997071 | 327 | gtk_container_forall(GTK_CONTAINER(widget), scroll_adjust, &data); |
e56307d3 PC |
328 | } |
329 | } | |
330 | ||
331 | void wxPizza::get_border_widths(int& x, int& y) | |
332 | { | |
333 | x = y = 0; | |
d7b63a01 RR |
334 | if (m_border_style == 0) |
335 | return; | |
03647350 | 336 | |
75f661bb | 337 | #ifndef __WXUNIVERSAL__ |
e56307d3 PC |
338 | if (m_border_style & wxBORDER_SIMPLE) |
339 | x = y = 1; | |
ec2d6790 | 340 | else if (m_is_scrollable /* || (m_border_style & wxBORDER_THEME) */) |
d7b63a01 RR |
341 | { |
342 | GtkWidget *style_widget = wxGTKPrivate::GetTreeWidget(); | |
03647350 | 343 | |
d7b63a01 RR |
344 | if (style_widget->style) |
345 | { | |
346 | x = style_widget->style->xthickness; | |
347 | y = style_widget->style->ythickness; | |
348 | } | |
349 | } | |
03647350 | 350 | else |
e56307d3 | 351 | { |
06a42745 | 352 | GtkWidget *style_widget = wxGTKPrivate::GetEntryWidget(); |
03647350 | 353 | |
06a42745 | 354 | if (style_widget->style) |
e56307d3 | 355 | { |
06a42745 RR |
356 | x = style_widget->style->xthickness; |
357 | y = style_widget->style->ythickness; | |
e56307d3 PC |
358 | } |
359 | } | |
75f661bb | 360 | #endif |
e56307d3 | 361 | } |