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