1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/control.cpp
3 // Purpose: wxControl implementation for wxGTK
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling, Julian Smart and Vadim Zeitlin
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
15 #include "wx/control.h"
19 #include "wx/settings.h"
22 #include "wx/fontutil.h"
24 #include "wx/sysopt.h"
27 #include "wx/gtk/private.h"
28 #include "wx/gtk/private/mnemonics.h"
30 // ============================================================================
31 // wxControl implementation
32 // ============================================================================
34 // ----------------------------------------------------------------------------
36 // ----------------------------------------------------------------------------
38 IMPLEMENT_DYNAMIC_CLASS(wxControl
, wxWindow
)
40 wxControl::wxControl()
44 bool wxControl::Create( wxWindow
*parent
,
49 const wxValidator
& validator
,
50 const wxString
&name
)
52 bool ret
= wxWindow::Create(parent
, id
, pos
, size
, style
, name
);
55 SetValidator(validator
);
62 bool wxControl::SetFont(const wxFont
& font
)
64 const bool changed
= base_type::SetFont(font
);
65 if (changed
&& !gtk_widget_get_realized(m_widget
))
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");
76 wxSize
wxControl::DoGetBestSize() const
78 // Do not return any arbitrary default value...
79 wxASSERT_MSG( m_widget
, wxT("DoGetBestSize called before creation") );
84 // this is not a native control, size_request is likely to be (0,0)
85 best
= wxControlBase::DoGetBestSize();
89 best
= GTKGetPreferredSize(m_widget
);
95 void wxControl::PostCreation(const wxSize
& size
)
97 wxWindow::PostCreation();
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
);
108 GTKApplyWidgetStyle();
109 SetInitialSize(size
);
112 // ----------------------------------------------------------------------------
113 // Work around a GTK+ bug whereby button is insensitive after being
115 // ----------------------------------------------------------------------------
117 // Fix sensitivity due to bug in GTK+ < 2.14
118 void wxControl::GTKFixSensitivity(bool WXUNUSED_IN_GTK3(onlyIfUnderMouse
))
121 if (gtk_check_version(2,14,0)
122 #if wxUSE_SYSTEM_OPTIONS
123 && (wxSystemOptions::GetOptionInt(wxT("gtk.control.disable-sensitivity-fix")) != 1)
127 if (!onlyIfUnderMouse
|| GetScreenRect().Contains(wxGetMousePosition()))
136 // ----------------------------------------------------------------------------
137 // wxControl dealing with labels
138 // ----------------------------------------------------------------------------
140 void wxControl::GTKSetLabelForLabel(GtkLabel
*w
, const wxString
& label
)
142 const wxString labelGTK
= GTKConvertMnemonics(label
);
143 gtk_label_set_text_with_mnemonic(w
, wxGTK_CONV(labelGTK
));
148 void wxControl::GTKSetLabelWithMarkupForLabel(GtkLabel
*w
, const wxString
& label
)
150 const wxString labelGTK
= GTKConvertMnemonicsWithMarkup(label
);
151 gtk_label_set_markup_with_mnemonic(w
, wxGTK_CONV(labelGTK
));
154 #endif // wxUSE_MARKUP
156 // ----------------------------------------------------------------------------
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 // ----------------------------------------------------------------------------
164 GtkWidget
* wxControl::GTKCreateFrame(const wxString
& label
)
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...
170 GtkWidget
* framewidget
= gtk_frame_new(NULL
);
171 gtk_frame_set_label_widget(GTK_FRAME(framewidget
), labelwidget
);
173 return framewidget
; // note that the label is already set so you'll
174 // only need to call wxControl::SetLabel afterwards
177 void wxControl::GTKSetLabelForFrame(GtkFrame
*w
, const wxString
& label
)
179 wxControlBase::SetLabel(label
);
181 GtkLabel
* labelwidget
= GTK_LABEL(gtk_frame_get_label_widget(w
));
182 GTKSetLabelForLabel(labelwidget
, label
);
185 void wxControl::GTKFrameApplyWidgetStyle(GtkFrame
* w
, GtkRcStyle
* style
)
187 GTKApplyStyle(GTK_WIDGET(w
), style
);
188 GTKApplyStyle(gtk_frame_get_label_widget(w
), style
);
191 void wxControl::GTKFrameSetMnemonicWidget(GtkFrame
* w
, GtkWidget
* widget
)
193 GtkLabel
* labelwidget
= GTK_LABEL(gtk_frame_get_label_widget(w
));
195 gtk_label_set_mnemonic_widget(labelwidget
, widget
);
198 // ----------------------------------------------------------------------------
199 // worker function implementing GTK*Mnemonics() functions
200 // ----------------------------------------------------------------------------
203 wxString
wxControl::GTKRemoveMnemonics(const wxString
& label
)
205 return wxGTKRemoveMnemonics(label
);
209 wxString
wxControl::GTKConvertMnemonics(const wxString
& label
)
211 return wxConvertMnemonicsToGTK(label
);
215 wxString
wxControl::GTKConvertMnemonicsWithMarkup(const wxString
& label
)
217 return wxConvertMnemonicsToGTKMarkup(label
);
220 // ----------------------------------------------------------------------------
221 // wxControl styles (a.k.a. attributes)
222 // ----------------------------------------------------------------------------
224 wxVisualAttributes
wxControl::GetDefaultAttributes() const
226 return GetDefaultAttributesFromGTKWidget(m_widget
,
232 wxControl::GetDefaultAttributesFromGTKWidget(GtkWidget
* widget
,
233 bool WXUNUSED_IN_GTK3(useBase
),
236 wxVisualAttributes attr
;
238 GtkStateFlags stateFlag
= GTK_STATE_FLAG_NORMAL
;
241 wxASSERT(state
== GTK_STATE_ACTIVE
);
242 stateFlag
= GTK_STATE_FLAG_ACTIVE
;
244 GtkStyleContext
* sc
= gtk_widget_get_style_context(widget
);
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
;
257 style
= gtk_rc_get_style(widget
);
259 style
= gtk_widget_get_default_style();
263 return wxWindow::GetClassDefaultAttributes(wxWINDOW_VARIANT_NORMAL
);
266 // get the style's colours
267 attr
.colFg
= wxColour(style
->fg
[state
]);
269 attr
.colBg
= wxColour(style
->base
[state
]);
271 attr
.colBg
= wxColour(style
->bg
[state
]);
273 // get the style's font
274 if ( !style
->font_desc
)
275 style
= gtk_widget_get_default_style();
276 if ( style
&& style
->font_desc
)
278 wxNativeFontInfo info
;
279 info
.description
= style
->font_desc
;
280 attr
.font
= wxFont(info
);
281 info
.description
= NULL
;
284 if (!attr
.font
.IsOk())
286 GtkSettings
*settings
= gtk_settings_get_default();
287 gchar
*font_name
= NULL
;
288 g_object_get ( settings
,
293 attr
.font
= wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT
);
295 attr
.font
= wxFont(wxString::FromAscii(font_name
));
305 wxControl::GetDefaultAttributesFromGTKWidget(wxGtkWidgetNew_t widget_new
,
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
);
321 wxControl::GetDefaultAttributesFromGTKWidget(wxGtkWidgetNewFromStr_t widget_new
,
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
);
338 wxControl::GetDefaultAttributesFromGTKWidget(wxGtkWidgetNewFromAdj_t widget_new
,
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
);
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
358 if (gtk_widget_get_request_mode(widget
) != GTK_SIZE_REQUEST_HEIGHT_FOR_WIDTH
)
360 gtk_widget_get_preferred_height(widget
, NULL
, &req
.height
);
361 gtk_widget_get_preferred_width_for_height(widget
, req
.height
, NULL
, &req
.width
);
365 gtk_widget_get_preferred_width(widget
, NULL
, &req
.width
);
366 gtk_widget_get_preferred_height_for_width(widget
, req
.width
, NULL
, &req
.height
);
369 GTK_WIDGET_GET_CLASS(widget
)->size_request(widget
, &req
);
372 return wxSize(req
.width
, req
.height
);
375 wxPoint
wxControl::GTKGetEntryMargins(GtkEntry
* entry
) const
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
);
385 marg
.x
= border
->left
+ border
->right
;
386 marg
.y
= border
->top
+ border
->bottom
;
390 // Gtk3 does not use inner border, but StyleContext and CSS
391 // TODO: implement it, starting with wxTextEntry::DoSetMargins()
395 gtk_entry_get_layout_offsets(entry
, &x
, &y
);
396 // inner borders are included. Substract them so we can get other margins
406 #endif // wxUSE_CONTROLS