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