Fix wxALWAYS_SHOW_SB behaviour in wxGTK.
[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
238 GtkWidget* tlw = NULL;
239 if (gtk_widget_get_parent(widget) == NULL)
240 {
241 tlw = gtk_window_new(GTK_WINDOW_TOPLEVEL);
242 gtk_container_add(GTK_CONTAINER(tlw), widget);
243 }
244
245 #ifdef __WXGTK3__
246 GtkStateFlags stateFlag = GTK_STATE_FLAG_NORMAL;
247 if (state)
248 {
249 wxASSERT(state == GTK_STATE_ACTIVE);
250 stateFlag = GTK_STATE_FLAG_ACTIVE;
251 }
252 GtkStyleContext* sc = gtk_widget_get_style_context(widget);
253 GdkRGBA c;
254 gtk_style_context_get_color(sc, stateFlag, &c);
255 attr.colFg = wxColour(c);
256 gtk_style_context_get_background_color(sc, stateFlag, &c);
257 attr.colBg = wxColour(c);
258 wxNativeFontInfo info;
259 info.description = const_cast<PangoFontDescription*>(gtk_style_context_get_font(sc, stateFlag));
260 attr.font = wxFont(info);
261 info.description = NULL;
262 #else
263 GtkStyle* style;
264
265 style = gtk_rc_get_style(widget);
266 if (!style)
267 style = gtk_widget_get_default_style();
268
269 if (style)
270 {
271 // get the style's colours
272 attr.colFg = wxColour(style->fg[state]);
273 if (useBase)
274 attr.colBg = wxColour(style->base[state]);
275 else
276 attr.colBg = wxColour(style->bg[state]);
277
278 // get the style's font
279 if (!style->font_desc)
280 style = gtk_widget_get_default_style();
281 if (style && style->font_desc)
282 {
283 wxNativeFontInfo info;
284 info.description = style->font_desc;
285 attr.font = wxFont(info);
286 info.description = NULL;
287 }
288 }
289 else
290 attr = wxWindow::GetClassDefaultAttributes(wxWINDOW_VARIANT_NORMAL);
291 #endif
292
293 if (!attr.font.IsOk())
294 {
295 GtkSettings *settings = gtk_settings_get_default();
296 gchar *font_name = NULL;
297 g_object_get ( settings,
298 "gtk-font-name",
299 &font_name,
300 NULL);
301 if (!font_name)
302 attr.font = wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT );
303 else
304 attr.font = wxFont(wxString::FromAscii(font_name));
305 g_free (font_name);
306 }
307
308 if (tlw)
309 gtk_widget_destroy(tlw);
310
311 return attr;
312 }
313
314 // This is not the same as GetBestSize() because that size may have
315 // been recalculated and cached by us. We want GTK+ information.
316 wxSize wxControl::GTKGetPreferredSize(GtkWidget* widget) const
317 {
318 GtkRequisition req;
319 #ifdef __WXGTK3__
320 gtk_widget_get_preferred_size(widget, NULL, &req);
321 #else
322 GTK_WIDGET_GET_CLASS(widget)->size_request(widget, &req);
323 #endif
324
325 return wxSize(req.width, req.height);
326 }
327
328 wxPoint wxControl::GTKGetEntryMargins(GtkEntry* entry) const
329 {
330 wxPoint marg(0, 0);
331
332 #ifndef __WXGTK3__
333 #if GTK_CHECK_VERSION(2,10,0)
334 // The margins we have previously set
335 const GtkBorder* border = gtk_entry_get_inner_border(entry);
336 if ( border )
337 {
338 marg.x = border->left + border->right;
339 marg.y = border->top + border->bottom;
340 }
341 #endif // GTK+ 2.10+
342 #else // GTK+ 3
343 // Gtk3 does not use inner border, but StyleContext and CSS
344 // TODO: implement it, starting with wxTextEntry::DoSetMargins()
345 #endif // GTK+ 2/3
346
347 int x, y;
348 gtk_entry_get_layout_offsets(entry, &x, &y);
349 // inner borders are included. Substract them so we can get other margins
350 x -= marg.x;
351 y -= marg.y;
352 marg.x += 2 * x + 2;
353 marg.y += 2 * y + 2;
354
355 return marg;
356 }
357
358
359 #endif // wxUSE_CONTROLS