Fix GetClientSize() when scrollbars are present
[wxWidgets.git] / src / gtk / control.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/control.cpp
3 // Purpose: wxControl implementation for wxGTK
4 // Author: Robert Roebling
5 // Id: $Id$
6 // Copyright: (c) 1998 Robert Roebling, Julian Smart and Vadim Zeitlin
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
12
13 #if wxUSE_CONTROLS
14
15 #include "wx/control.h"
16
17 #ifndef WX_PRECOMP
18 #include "wx/log.h"
19 #include "wx/settings.h"
20 #endif
21
22 #include "wx/fontutil.h"
23 #include "wx/utils.h"
24 #include "wx/sysopt.h"
25
26 #include <gtk/gtk.h>
27 #include "wx/gtk/private.h"
28 #include "wx/gtk/private/mnemonics.h"
29
30 // ============================================================================
31 // wxControl implementation
32 // ============================================================================
33
34 // ----------------------------------------------------------------------------
35 // wxControl creation
36 // ----------------------------------------------------------------------------
37
38 IMPLEMENT_DYNAMIC_CLASS(wxControl, wxWindow)
39
40 wxControl::wxControl()
41 {
42 }
43
44 bool wxControl::Create( wxWindow *parent,
45 wxWindowID id,
46 const wxPoint &pos,
47 const wxSize &size,
48 long style,
49 const wxValidator& validator,
50 const wxString &name )
51 {
52 bool ret = wxWindow::Create(parent, id, pos, size, style, name);
53
54 #if wxUSE_VALIDATORS
55 SetValidator(validator);
56 #endif
57
58 return ret;
59 }
60
61 #ifdef __WXGTK3__
62 bool wxControl::SetFont(const wxFont& font)
63 {
64 const bool changed = base_type::SetFont(font);
65 if (changed && !gtk_widget_get_realized(m_widget))
66 {
67 // GTK defers sending "style-updated" until widget is realized, but
68 // GetBestSize() won't compute correct result until the signal is sent,
69 // so we have to do it now
70 g_signal_emit_by_name(m_widget, "style-updated");
71 }
72 return changed;
73 }
74 #endif
75
76 wxSize wxControl::DoGetBestSize() const
77 {
78 // Do not return any arbitrary default value...
79 wxASSERT_MSG( m_widget, wxT("DoGetBestSize called before creation") );
80
81 wxSize best;
82 if (m_wxwindow)
83 {
84 // this is not a native control, size_request is likely to be (0,0)
85 best = wxControlBase::DoGetBestSize();
86 }
87 else
88 {
89 best = GTKGetPreferredSize(m_widget);
90 }
91
92 return best;
93 }
94
95 void wxControl::PostCreation(const wxSize& size)
96 {
97 wxWindow::PostCreation();
98
99 #ifndef __WXGTK3__
100 // NB: GetBestSize needs to know the style, otherwise it will assume
101 // default font and if the user uses a different font, determined
102 // best size will be different (typically, smaller) than the desired
103 // size. This call ensure that a style is available at the time
104 // GetBestSize is called.
105 gtk_widget_ensure_style(m_widget);
106 #endif
107
108 GTKApplyWidgetStyle();
109 SetInitialSize(size);
110 }
111
112 // ----------------------------------------------------------------------------
113 // Work around a GTK+ bug whereby button is insensitive after being
114 // enabled
115 // ----------------------------------------------------------------------------
116
117 // Fix sensitivity due to bug in GTK+ < 2.14
118 void wxControl::GTKFixSensitivity(bool WXUNUSED_IN_GTK3(onlyIfUnderMouse))
119 {
120 #ifndef __WXGTK3__
121 if (gtk_check_version(2,14,0)
122 #if wxUSE_SYSTEM_OPTIONS
123 && (wxSystemOptions::GetOptionInt(wxT("gtk.control.disable-sensitivity-fix")) != 1)
124 #endif
125 )
126 {
127 if (!onlyIfUnderMouse || GetScreenRect().Contains(wxGetMousePosition()))
128 {
129 Hide();
130 Show();
131 }
132 }
133 #endif
134 }
135
136 // ----------------------------------------------------------------------------
137 // wxControl dealing with labels
138 // ----------------------------------------------------------------------------
139
140 void wxControl::GTKSetLabelForLabel(GtkLabel *w, const wxString& label)
141 {
142 const wxString labelGTK = GTKConvertMnemonics(label);
143 gtk_label_set_text_with_mnemonic(w, wxGTK_CONV(labelGTK));
144 }
145
146 #if wxUSE_MARKUP
147
148 void wxControl::GTKSetLabelWithMarkupForLabel(GtkLabel *w, const wxString& label)
149 {
150 const wxString labelGTK = GTKConvertMnemonicsWithMarkup(label);
151 gtk_label_set_markup_with_mnemonic(w, wxGTK_CONV(labelGTK));
152 }
153
154 #endif // wxUSE_MARKUP
155
156 // ----------------------------------------------------------------------------
157 // GtkFrame helpers
158 //
159 // GtkFrames do in fact support mnemonics in GTK2+ but not through
160 // gtk_frame_set_label, rather you need to use a custom label widget
161 // instead (idea gleaned from the native gtk font dialog code in GTK)
162 // ----------------------------------------------------------------------------
163
164 GtkWidget* wxControl::GTKCreateFrame(const wxString& label)
165 {
166 const wxString labelGTK = GTKConvertMnemonics(label);
167 GtkWidget* labelwidget = gtk_label_new_with_mnemonic(wxGTK_CONV(labelGTK));
168 gtk_widget_show(labelwidget); // without this it won't show...
169
170 GtkWidget* framewidget = gtk_frame_new(NULL);
171 gtk_frame_set_label_widget(GTK_FRAME(framewidget), labelwidget);
172
173 return framewidget; // note that the label is already set so you'll
174 // only need to call wxControl::SetLabel afterwards
175 }
176
177 void wxControl::GTKSetLabelForFrame(GtkFrame *w, const wxString& label)
178 {
179 wxControlBase::SetLabel(label);
180
181 GtkLabel* labelwidget = GTK_LABEL(gtk_frame_get_label_widget(w));
182 GTKSetLabelForLabel(labelwidget, label);
183 }
184
185 void wxControl::GTKFrameApplyWidgetStyle(GtkFrame* w, GtkRcStyle* style)
186 {
187 GTKApplyStyle(GTK_WIDGET(w), style);
188 GTKApplyStyle(gtk_frame_get_label_widget(w), style);
189 }
190
191 void wxControl::GTKFrameSetMnemonicWidget(GtkFrame* w, GtkWidget* widget)
192 {
193 GtkLabel* labelwidget = GTK_LABEL(gtk_frame_get_label_widget(w));
194
195 gtk_label_set_mnemonic_widget(labelwidget, widget);
196 }
197
198 // ----------------------------------------------------------------------------
199 // worker function implementing GTK*Mnemonics() functions
200 // ----------------------------------------------------------------------------
201
202 /* static */
203 wxString wxControl::GTKRemoveMnemonics(const wxString& label)
204 {
205 return wxGTKRemoveMnemonics(label);
206 }
207
208 /* static */
209 wxString wxControl::GTKConvertMnemonics(const wxString& label)
210 {
211 return wxConvertMnemonicsToGTK(label);
212 }
213
214 /* static */
215 wxString wxControl::GTKConvertMnemonicsWithMarkup(const wxString& label)
216 {
217 return wxConvertMnemonicsToGTKMarkup(label);
218 }
219
220 // ----------------------------------------------------------------------------
221 // wxControl styles (a.k.a. attributes)
222 // ----------------------------------------------------------------------------
223
224 wxVisualAttributes wxControl::GetDefaultAttributes() const
225 {
226 return GetDefaultAttributesFromGTKWidget(m_widget,
227 UseGTKStyleBase());
228 }
229
230 // static
231 wxVisualAttributes
232 wxControl::GetDefaultAttributesFromGTKWidget(GtkWidget* widget,
233 bool WXUNUSED_IN_GTK3(useBase),
234 int state)
235 {
236 wxVisualAttributes attr;
237 #ifdef __WXGTK3__
238 GtkStateFlags stateFlag = GTK_STATE_FLAG_NORMAL;
239 if (state)
240 {
241 wxASSERT(state == GTK_STATE_ACTIVE);
242 stateFlag = GTK_STATE_FLAG_ACTIVE;
243 }
244 GtkStyleContext* sc = gtk_widget_get_style_context(widget);
245 GdkRGBA c;
246 gtk_style_context_get_color(sc, stateFlag, &c);
247 attr.colFg = wxColour(c);
248 gtk_style_context_get_background_color(sc, stateFlag, &c);
249 attr.colBg = wxColour(c);
250 wxNativeFontInfo info;
251 info.description = const_cast<PangoFontDescription*>(gtk_style_context_get_font(sc, stateFlag));
252 attr.font = wxFont(info);
253 info.description = NULL;
254 #else
255 GtkStyle* style;
256
257 style = gtk_rc_get_style(widget);
258 if (!style)
259 style = gtk_widget_get_default_style();
260
261 if (!style)
262 {
263 return wxWindow::GetClassDefaultAttributes(wxWINDOW_VARIANT_NORMAL);
264 }
265
266 // get the style's colours
267 attr.colFg = wxColour(style->fg[state]);
268 if (useBase)
269 attr.colBg = wxColour(style->base[state]);
270 else
271 attr.colBg = wxColour(style->bg[state]);
272
273 // get the style's font
274 if ( !style->font_desc )
275 style = gtk_widget_get_default_style();
276 if ( style && style->font_desc )
277 {
278 wxNativeFontInfo info;
279 info.description = style->font_desc;
280 attr.font = wxFont(info);
281 info.description = NULL;
282 }
283 #endif
284 if (!attr.font.IsOk())
285 {
286 GtkSettings *settings = gtk_settings_get_default();
287 gchar *font_name = NULL;
288 g_object_get ( settings,
289 "gtk-font-name",
290 &font_name,
291 NULL);
292 if (!font_name)
293 attr.font = wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT );
294 else
295 attr.font = wxFont(wxString::FromAscii(font_name));
296 g_free (font_name);
297 }
298
299 return attr;
300 }
301
302
303 //static
304 wxVisualAttributes
305 wxControl::GetDefaultAttributesFromGTKWidget(wxGtkWidgetNew_t widget_new,
306 bool useBase,
307 int state)
308 {
309 wxVisualAttributes attr;
310 // NB: we need toplevel window so that GTK+ can find the right style
311 GtkWidget *wnd = gtk_window_new(GTK_WINDOW_TOPLEVEL);
312 GtkWidget* widget = widget_new();
313 gtk_container_add(GTK_CONTAINER(wnd), widget);
314 attr = GetDefaultAttributesFromGTKWidget(widget, useBase, state);
315 gtk_widget_destroy(wnd);
316 return attr;
317 }
318
319 //static
320 wxVisualAttributes
321 wxControl::GetDefaultAttributesFromGTKWidget(wxGtkWidgetNewFromStr_t widget_new,
322 bool useBase,
323 int state)
324 {
325 wxVisualAttributes attr;
326 // NB: we need toplevel window so that GTK+ can find the right style
327 GtkWidget *wnd = gtk_window_new(GTK_WINDOW_TOPLEVEL);
328 GtkWidget* widget = widget_new("");
329 gtk_container_add(GTK_CONTAINER(wnd), widget);
330 attr = GetDefaultAttributesFromGTKWidget(widget, useBase, state);
331 gtk_widget_destroy(wnd);
332 return attr;
333 }
334
335
336 //static
337 wxVisualAttributes
338 wxControl::GetDefaultAttributesFromGTKWidget(wxGtkWidgetNewFromAdj_t widget_new,
339 bool useBase,
340 int state)
341 {
342 wxVisualAttributes attr;
343 // NB: we need toplevel window so that GTK+ can find the right style
344 GtkWidget *wnd = gtk_window_new(GTK_WINDOW_TOPLEVEL);
345 GtkWidget* widget = widget_new(NULL);
346 gtk_container_add(GTK_CONTAINER(wnd), widget);
347 attr = GetDefaultAttributesFromGTKWidget(widget, useBase, state);
348 gtk_widget_destroy(wnd);
349 return attr;
350 }
351
352 // This is not the same as GetBestSize() because that size may have
353 // been recalculated and cached by us. We want GTK+ information.
354 wxSize wxControl::GTKGetPreferredSize(GtkWidget* widget) const
355 {
356 GtkRequisition req;
357 #ifdef __WXGTK3__
358 if (gtk_widget_get_request_mode(widget) != GTK_SIZE_REQUEST_HEIGHT_FOR_WIDTH)
359 {
360 gtk_widget_get_preferred_height(widget, NULL, &req.height);
361 gtk_widget_get_preferred_width_for_height(widget, req.height, NULL, &req.width);
362 }
363 else
364 {
365 gtk_widget_get_preferred_width(widget, NULL, &req.width);
366 gtk_widget_get_preferred_height_for_width(widget, req.width, NULL, &req.height);
367 }
368 #else
369 GTK_WIDGET_GET_CLASS(widget)->size_request(widget, &req);
370 #endif
371
372 return wxSize(req.width, req.height);
373 }
374
375 wxPoint wxControl::GTKGetEntryMargins(GtkEntry* entry) const
376 {
377 wxPoint marg(0, 0);
378
379 #ifndef __WXGTK3__
380 #if GTK_CHECK_VERSION(2,10,0)
381 // The margins we have previously set
382 const GtkBorder* border = gtk_entry_get_inner_border(entry);
383 if ( border )
384 {
385 marg.x = border->left + border->right;
386 marg.y = border->top + border->bottom;
387 }
388 #endif // GTK+ 2.10+
389 #else // GTK+ 3
390 // Gtk3 does not use inner border, but StyleContext and CSS
391 // TODO: implement it, starting with wxTextEntry::DoSetMargins()
392 #endif // GTK+ 2/3
393
394 int x, y;
395 gtk_entry_get_layout_offsets(entry, &x, &y);
396 // inner borders are included. Substract them so we can get other margins
397 x -= marg.x;
398 y -= marg.y;
399 marg.x += 2 * x + 2;
400 marg.y += 2 * y + 2;
401
402 return marg;
403 }
404
405
406 #endif // wxUSE_CONTROLS