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