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
IMPLEMENT_DYNAMIC_CLASS(wxControl,wxWindow)37 IMPLEMENT_DYNAMIC_CLASS(wxControl, wxWindow)
38
39 wxControl::wxControl()
40 {
41 }
42
Create(wxWindow * parent,wxWindowID id,const wxPoint & pos,const wxSize & size,long style,const wxValidator & wxVALIDATOR_PARAM (validator),const wxString & name)43 bool wxControl::Create( wxWindow *parent,
44 wxWindowID id,
45 const wxPoint &pos,
46 const wxSize &size,
47 long style,
48 const wxValidator& wxVALIDATOR_PARAM(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__
SetFont(const wxFont & font)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) && gtk_check_version(3,5,0))
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 // But don't bother for GTK > 3.4, the change won't take effect until
70 // GTK updates it's style cache
71 g_signal_emit_by_name(m_widget, "style-updated");
72 }
73 return changed;
74 }
75 #endif
76
DoGetBestSize() const77 wxSize wxControl::DoGetBestSize() const
78 {
79 // Do not return any arbitrary default value...
80 wxASSERT_MSG( m_widget, wxT("DoGetBestSize called before creation") );
81
82 wxSize best;
83 if (m_wxwindow)
84 {
85 // this is not a native control, size_request is likely to be (0,0)
86 best = wxControlBase::DoGetBestSize();
87 }
88 else
89 {
90 best = GTKGetPreferredSize(m_widget);
91 }
92
93 return best;
94 }
95
PostCreation(const wxSize & size)96 void wxControl::PostCreation(const wxSize& size)
97 {
98 wxWindow::PostCreation();
99
100 #ifndef __WXGTK3__
101 // NB: GetBestSize needs to know the style, otherwise it will assume
102 // default font and if the user uses a different font, determined
103 // best size will be different (typically, smaller) than the desired
104 // size. This call ensure that a style is available at the time
105 // GetBestSize is called.
106 gtk_widget_ensure_style(m_widget);
107 #endif
108
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
GTKFixSensitivity(bool WXUNUSED_IN_GTK3 (onlyIfUnderMouse))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
GTKSetLabelForLabel(GtkLabel * w,const wxString & label)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
GTKSetLabelWithMarkupForLabel(GtkLabel * w,const wxString & label)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
GTKCreateFrame(const wxString & label)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
GTKSetLabelForFrame(GtkFrame * w,const wxString & label)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
GTKFrameApplyWidgetStyle(GtkFrame * w,GtkRcStyle * style)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
GTKFrameSetMnemonicWidget(GtkFrame * w,GtkWidget * widget)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 */
GTKRemoveMnemonics(const wxString & label)203 wxString wxControl::GTKRemoveMnemonics(const wxString& label)
204 {
205 return wxGTKRemoveMnemonics(label);
206 }
207
208 /* static */
GTKConvertMnemonics(const wxString & label)209 wxString wxControl::GTKConvertMnemonics(const wxString& label)
210 {
211 return wxConvertMnemonicsToGTK(label);
212 }
213
214 /* static */
GTKConvertMnemonicsWithMarkup(const wxString & label)215 wxString wxControl::GTKConvertMnemonicsWithMarkup(const wxString& label)
216 {
217 return wxConvertMnemonicsToGTKMarkup(label);
218 }
219
220 // ----------------------------------------------------------------------------
221 // wxControl styles (a.k.a. attributes)
222 // ----------------------------------------------------------------------------
223
GetDefaultAttributes() const224 wxVisualAttributes wxControl::GetDefaultAttributes() const
225 {
226 return GetDefaultAttributesFromGTKWidget(m_widget,
227 UseGTKStyleBase());
228 }
229
230 // static
231 wxVisualAttributes
GetDefaultAttributesFromGTKWidget(GtkWidget * widget,bool WXUNUSED_IN_GTK3 (useBase),int state)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 gtk_style_context_save(sc);
254 GdkRGBA c;
255 gtk_style_context_set_state(sc, stateFlag);
256 gtk_style_context_get_color(sc, stateFlag, &c);
257 attr.colFg = wxColour(c);
258 gtk_style_context_get_background_color(sc, stateFlag, &c);
259 attr.colBg = wxColour(c);
260 wxNativeFontInfo info;
261 gtk_style_context_get(
262 sc, stateFlag, GTK_STYLE_PROPERTY_FONT, &info.description, NULL);
263 attr.font = wxFont(info);
264 gtk_style_context_restore(sc);
265
266 // Go up the parent chain for a background color
267 while (attr.colBg.Alpha() == 0 && (widget = gtk_widget_get_parent(widget)))
268 {
269 sc = gtk_widget_get_style_context(widget);
270 gtk_style_context_save(sc);
271 gtk_style_context_set_state(sc, stateFlag);
272 gtk_style_context_get_background_color(sc, stateFlag, &c);
273 gtk_style_context_restore(sc);
274 attr.colBg = wxColour(c);
275 }
276 #else
277 GtkStyle* style;
278
279 style = gtk_rc_get_style(widget);
280 if (!style)
281 style = gtk_widget_get_default_style();
282
283 if (style)
284 {
285 // get the style's colours
286 attr.colFg = wxColour(style->fg[state]);
287 if (useBase)
288 attr.colBg = wxColour(style->base[state]);
289 else
290 attr.colBg = wxColour(style->bg[state]);
291
292 // get the style's font
293 if (!style->font_desc)
294 style = gtk_widget_get_default_style();
295 if (style && style->font_desc)
296 {
297 wxNativeFontInfo info;
298 info.description = style->font_desc;
299 attr.font = wxFont(info);
300 info.description = NULL;
301 }
302 }
303 else
304 attr = wxWindow::GetClassDefaultAttributes(wxWINDOW_VARIANT_NORMAL);
305 #endif
306
307 if (!attr.font.IsOk())
308 {
309 GtkSettings *settings = gtk_settings_get_default();
310 gchar *font_name = NULL;
311 g_object_get ( settings,
312 "gtk-font-name",
313 &font_name,
314 NULL);
315 if (!font_name)
316 attr.font = wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT );
317 else
318 {
319 attr.font = wxFont(wxString::FromUTF8(font_name));
320 g_free(font_name);
321 }
322 }
323
324 if (tlw)
325 gtk_widget_destroy(tlw);
326
327 return attr;
328 }
329
330 // This is not the same as GetBestSize() because that size may have
331 // been recalculated and cached by us. We want GTK+ information.
GTKGetPreferredSize(GtkWidget * widget) const332 wxSize wxControl::GTKGetPreferredSize(GtkWidget* widget) const
333 {
334 GtkRequisition req = { 0, 0 };
335 #ifdef __WXGTK3__
336 int w, h;
337 gtk_widget_get_size_request(widget, &w, &h);
338
339 // gtk_widget_get_preferred_size() just returns 0 if the control is hidden,
340 // so we have to temporarily show the widget before calling it to get
341 // something useful from it, if it's currently hidden.
342 // So workaround this case.
343 const bool wasHidden = !gtk_widget_get_visible(widget);
344 if ( wasHidden )
345 gtk_widget_show(widget);
346
347 gtk_widget_set_size_request(widget, -1, -1);
348 gtk_widget_get_preferred_size(widget, NULL, &req);
349 gtk_widget_set_size_request(widget, w, h);
350
351 if ( wasHidden )
352 gtk_widget_hide(widget);
353 #else
354 GTK_WIDGET_GET_CLASS(widget)->size_request(widget, &req);
355 #endif
356
357 return wxSize(req.width, req.height);
358 }
359
GTKGetEntryMargins(GtkEntry * entry) const360 wxPoint wxControl::GTKGetEntryMargins(GtkEntry* entry) const
361 {
362 wxPoint size;
363 gtk_entry_get_layout_offsets(entry, &size.x, &size.y);
364
365 #ifdef __WXGTK3__
366 GtkBorder border;
367 GtkStyleContext* sc = gtk_widget_get_style_context(GTK_WIDGET(entry));
368 gtk_style_context_get_padding(sc, gtk_style_context_get_state(sc), &border);
369 #else
370 // Equivalent to the GTK2 private function _gtk_entry_effective_inner_border()
371
372 GtkBorder border = { 2, 2, 2, 2 };
373
374 #if GTK_CHECK_VERSION(2,10,0)
375 if (gtk_check_version(2,10,0) == NULL)
376 {
377 const GtkBorder* innerBorder1 = gtk_entry_get_inner_border(entry);
378 if (innerBorder1)
379 border = *innerBorder1;
380 else
381 {
382 GtkBorder* innerBorder2;
383 gtk_widget_style_get(GTK_WIDGET(entry), "inner-border", &innerBorder2, NULL);
384 if (innerBorder2)
385 {
386 border = *innerBorder2;
387 gtk_border_free(innerBorder2);
388 }
389 }
390 }
391 #endif // GTK+ 2.10+
392 #endif
393
394 size.x += border.left + border.right;
395 size.y += border.top + border.bottom;
396
397 return size;
398 }
399
400
401 #endif // wxUSE_CONTROLS
402