1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/win_gtk.cpp
3 // Purpose: native GTK+ widget for wxWindow
4 // Author: Paul Cornett
6 // Copyright: (c) 2007 Paul Cornett
7 // Licence: wxWindows licence
8 ///////////////////////////////////////////////////////////////////////////////
10 #include "wx/wxprec.h"
13 #include "wx/gtk/private.h"
14 #include "wx/gtk/private/win_gtk.h"
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
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
24 position in size_allocate.
26 For borders, space is reserved in realize and size_allocate. The border is
27 drawn on wxPizza's parent GdkWindow.
29 For RTL, child widget positions are mirrored in size_allocate.
32 static GtkWidgetClass
* parent_class
;
39 void (*set_scroll_adjustments
)(GtkWidget
*, GtkAdjustment
*, GtkAdjustment
*);
42 static void size_allocate(GtkWidget
* widget
, GtkAllocation
* alloc
)
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
;
50 if (gtk_widget_get_realized(widget
))
52 int h
= alloc
->height
- 2 * border_y
;
54 const int x
= alloc
->x
+ border_x
;
55 const int y
= alloc
->y
+ border_y
;
57 GdkWindow
* window
= gtk_widget_get_window(widget
);
59 gdk_window_get_position(window
, &old_x
, &old_y
);
61 if (x
!= old_x
|| y
!= old_y
||
62 w
!= gdk_window_get_width(window
) || h
!= gdk_window_get_height(window
))
64 gdk_window_move_resize(window
, x
, y
, w
, h
);
66 if (border_x
+ border_y
)
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);
77 widget
->allocation
= *alloc
;
79 // adjust child positions
80 for (const GList
* list
= pizza
->m_fixed
.children
; list
; list
= list
->next
)
82 const GtkFixedChild
* child
= static_cast<GtkFixedChild
*>(list
->data
);
83 if (gtk_widget_get_visible(child
->widget
))
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
;
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
)
96 child_alloc
.x
= w
- child_alloc
.x
- child_alloc
.width
;
97 gtk_widget_size_allocate(child
->widget
, &child_alloc
);
102 static void realize(GtkWidget
* widget
)
104 parent_class
->realize(widget
);
106 wxPizza
* pizza
= WX_PIZZA(widget
);
107 if (pizza
->m_border_style
)
109 int border_x
, border_y
;
110 pizza
->get_border_widths(border_x
, border_y
);
111 int x
= widget
->allocation
.x
+ border_x
;
112 int y
= widget
->allocation
.y
+ border_y
;
113 int w
= widget
->allocation
.width
- 2 * border_x
;
114 int h
= widget
->allocation
.height
- 2 * border_y
;
117 gdk_window_move_resize(widget
->window
, x
, y
, w
, h
);
121 static void show(GtkWidget
* widget
)
123 if (widget
->parent
&& WX_PIZZA(widget
)->m_border_style
)
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
);
130 parent_class
->show(widget
);
133 static void hide(GtkWidget
* widget
)
135 if (widget
->parent
&& WX_PIZZA(widget
)->m_border_style
)
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
);
142 parent_class
->hide(widget
);
145 // not used, but needs to exist so gtk_widget_set_scroll_adjustments will work
146 static void set_scroll_adjustments(GtkWidget
*, GtkAdjustment
*, GtkAdjustment
*)
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)
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
)
161 typedef void (*GMarshalFunc_VOID__OBJECT_OBJECT
) (gpointer data1
,
165 register GMarshalFunc_VOID__OBJECT_OBJECT callback
;
166 register GCClosure
*cc
= (GCClosure
*) closure
;
167 register gpointer data1
, data2
;
169 g_return_if_fail (n_param_values
== 3);
171 if (G_CCLOSURE_SWAP_DATA (closure
))
173 data1
= closure
->data
;
174 data2
= g_value_peek_pointer (param_values
+ 0);
178 data1
= g_value_peek_pointer (param_values
+ 0);
179 data2
= closure
->data
;
181 callback
= (GMarshalFunc_VOID__OBJECT_OBJECT
) (marshal_data
? marshal_data
: cc
->callback
);
184 g_marshal_value_peek_object (param_values
+ 1),
185 g_marshal_value_peek_object (param_values
+ 2),
189 static void class_init(void* g_class
, void*)
191 GtkWidgetClass
* widget_class
= (GtkWidgetClass
*)g_class
;
192 widget_class
->size_allocate
= size_allocate
;
193 widget_class
->realize
= realize
;
194 widget_class
->show
= show
;
195 widget_class
->hide
= hide
;
196 wxPizzaClass
* klass
= (wxPizzaClass
*)g_class
;
198 // needed to make widget appear scrollable to GTK+
199 klass
->set_scroll_adjustments
= set_scroll_adjustments
;
200 widget_class
->set_scroll_adjustments_signal
=
202 "set_scroll_adjustments",
203 G_TYPE_FROM_CLASS(g_class
),
205 G_STRUCT_OFFSET(wxPizzaClass
, set_scroll_adjustments
),
207 g_cclosure_user_marshal_VOID__OBJECT_OBJECT
,
208 G_TYPE_NONE
, 2, GTK_TYPE_ADJUSTMENT
, GTK_TYPE_ADJUSTMENT
);
210 parent_class
= GTK_WIDGET_CLASS(g_type_class_peek_parent(g_class
));
215 GType
wxPizza::type()
220 const GTypeInfo info
= {
221 sizeof(wxPizzaClass
),
228 type
= g_type_register_static(
229 GTK_TYPE_FIXED
, "wxPizza", &info
, GTypeFlags(0));
234 GtkWidget
* wxPizza::New(long windowStyle
)
236 GtkWidget
* widget
= GTK_WIDGET(g_object_new(type(), NULL
));
237 wxPizza
* pizza
= WX_PIZZA(widget
);
238 pizza
->m_scroll_x
= 0;
239 pizza
->m_scroll_y
= 0;
240 pizza
->m_is_scrollable
= (windowStyle
& (wxHSCROLL
| wxVSCROLL
)) != 0;
241 // mask off border styles not useable with wxPizza
242 pizza
->m_border_style
= int(windowStyle
& BORDER_STYLES
);
243 #if GTK_CHECK_VERSION(3,0,0) || defined(GTK_DISABLE_DEPRECATED)
244 gtk_widget_set_has_window(widget
, true);
246 gtk_fixed_set_has_window(GTK_FIXED(widget
), true);
248 gtk_widget_add_events(widget
,
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
|
260 GDK_KEY_RELEASE_MASK
|
261 GDK_ENTER_NOTIFY_MASK
|
262 GDK_LEAVE_NOTIFY_MASK
|
263 GDK_FOCUS_CHANGE_MASK
);
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
)
271 GtkFixed
* fixed
= &m_fixed
;
272 for (const GList
* list
= fixed
->children
; list
; list
= list
->next
)
274 const GtkFixedChild
* child
= static_cast<GtkFixedChild
*>(list
->data
);
275 if (child
->widget
== widget
)
277 if (child
->x
!= x
|| child
->y
!= y
)
278 gtk_fixed_move(fixed
, widget
, x
, y
);
284 void wxPizza::put(GtkWidget
* widget
, int x
, int y
)
286 gtk_fixed_put(&m_fixed
, widget
, x
, y
);
294 // Adjust allocations for all widgets using the GdkWindow which was just scrolled
296 static void scroll_adjust(GtkWidget
* widget
, void* data
)
298 const AdjustData
* p
= static_cast<AdjustData
*>(data
);
299 widget
->allocation
.x
+= p
->dx
;
300 widget
->allocation
.y
+= p
->dy
;
302 if (widget
->window
== p
->window
)
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
);
314 void wxPizza::scroll(int dx
, int dy
)
316 GtkWidget
* widget
= GTK_WIDGET(this);
317 if (gtk_widget_get_direction(widget
) == GTK_TEXT_DIR_RTL
)
323 gdk_window_scroll(widget
->window
, dx
, dy
);
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.
326 AdjustData data
= { widget
->window
, dx
, dy
};
327 gtk_container_forall(GTK_CONTAINER(widget
), scroll_adjust
, &data
);
331 void wxPizza::get_border_widths(int& x
, int& y
)
334 if (m_border_style
== 0)
337 #ifndef __WXUNIVERSAL__
338 if (m_border_style
& wxBORDER_SIMPLE
)
340 else if (m_is_scrollable
/* || (m_border_style & wxBORDER_THEME) */)
342 GtkWidget
*style_widget
= wxGTKPrivate::GetTreeWidget();
344 if (style_widget
->style
)
346 x
= style_widget
->style
->xthickness
;
347 y
= style_widget
->style
->ythickness
;
352 GtkWidget
*style_widget
= wxGTKPrivate::GetEntryWidget();
354 if (style_widget
->style
)
356 x
= style_widget
->style
->xthickness
;
357 y
= style_widget
->style
->ythickness
;