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"
23 #include "wx/gtk/private.h"
25 #include "wx/sysopt.h"
27 #include "wx/gtk/private/mnemonics.h"
29 // ============================================================================
30 // wxControl implementation
31 // ============================================================================
33 // ----------------------------------------------------------------------------
35 // ----------------------------------------------------------------------------
37 IMPLEMENT_DYNAMIC_CLASS(wxControl
, wxWindow
)
39 wxControl::wxControl()
43 bool wxControl::Create( wxWindow
*parent
,
48 const wxValidator
& validator
,
49 const wxString
&name
)
51 bool ret
= wxWindow::Create(parent
, id
, pos
, size
, style
, name
);
54 SetValidator(validator
);
60 wxSize
wxControl::DoGetBestSize() const
62 // Do not return any arbitrary default value...
63 wxASSERT_MSG( m_widget
, wxT("DoGetBestSize called before creation") );
68 // this is not a native control, size_request is likely to be (0,0)
69 best
= wxControlBase::DoGetBestSize();
74 GTK_WIDGET_GET_CLASS(m_widget
)->size_request(m_widget
, &req
);
75 best
.Set(req
.width
, req
.height
);
81 void wxControl::PostCreation(const wxSize
& size
)
83 wxWindow::PostCreation();
85 // NB: GetBestSize needs to know the style, otherwise it will assume
86 // default font and if the user uses a different font, determined
87 // best size will be different (typically, smaller) than the desired
88 // size. This call ensure that a style is available at the time
89 // GetBestSize is called.
90 gtk_widget_ensure_style(m_widget
);
92 GTKApplyWidgetStyle();
96 // ----------------------------------------------------------------------------
97 // Work around a GTK+ bug whereby button is insensitive after being
99 // ----------------------------------------------------------------------------
101 // Fix sensitivity due to bug in GTK+ < 2.14
102 void wxControl::GTKFixSensitivity(bool onlyIfUnderMouse
)
104 if (gtk_check_version(2,14,0)
105 #if wxUSE_SYSTEM_OPTIONS
106 && (wxSystemOptions::GetOptionInt(wxT("gtk.control.disable-sensitivity-fix")) != 1)
110 wxPoint pt
= wxGetMousePosition();
111 wxRect
rect(ClientToScreen(wxPoint(0, 0)), GetSize());
112 if (!onlyIfUnderMouse
|| rect
.Contains(pt
))
120 // ----------------------------------------------------------------------------
121 // wxControl dealing with labels
122 // ----------------------------------------------------------------------------
124 void wxControl::GTKSetLabelForLabel(GtkLabel
*w
, const wxString
& label
)
126 const wxString labelGTK
= GTKConvertMnemonics(label
);
127 gtk_label_set_text_with_mnemonic(w
, wxGTK_CONV(labelGTK
));
132 void wxControl::GTKSetLabelWithMarkupForLabel(GtkLabel
*w
, const wxString
& label
)
134 const wxString labelGTK
= GTKConvertMnemonicsWithMarkup(label
);
135 gtk_label_set_markup_with_mnemonic(w
, wxGTK_CONV(labelGTK
));
138 #endif // wxUSE_MARKUP
140 // ----------------------------------------------------------------------------
143 // GtkFrames do in fact support mnemonics in GTK2+ but not through
144 // gtk_frame_set_label, rather you need to use a custom label widget
145 // instead (idea gleaned from the native gtk font dialog code in GTK)
146 // ----------------------------------------------------------------------------
148 GtkWidget
* wxControl::GTKCreateFrame(const wxString
& label
)
150 const wxString labelGTK
= GTKConvertMnemonics(label
);
151 GtkWidget
* labelwidget
= gtk_label_new_with_mnemonic(wxGTK_CONV(labelGTK
));
152 gtk_widget_show(labelwidget
); // without this it won't show...
154 GtkWidget
* framewidget
= gtk_frame_new(NULL
);
155 gtk_frame_set_label_widget(GTK_FRAME(framewidget
), labelwidget
);
157 return framewidget
; // note that the label is already set so you'll
158 // only need to call wxControl::SetLabel afterwards
161 void wxControl::GTKSetLabelForFrame(GtkFrame
*w
, const wxString
& label
)
163 wxControlBase::SetLabel(label
);
165 GtkLabel
* labelwidget
= GTK_LABEL(gtk_frame_get_label_widget(w
));
166 GTKSetLabelForLabel(labelwidget
, label
);
169 void wxControl::GTKFrameApplyWidgetStyle(GtkFrame
* w
, GtkRcStyle
* style
)
171 gtk_widget_modify_style(GTK_WIDGET(w
), style
);
172 gtk_widget_modify_style(gtk_frame_get_label_widget (w
), style
);
175 void wxControl::GTKFrameSetMnemonicWidget(GtkFrame
* w
, GtkWidget
* widget
)
177 GtkLabel
* labelwidget
= GTK_LABEL(gtk_frame_get_label_widget(w
));
179 gtk_label_set_mnemonic_widget(labelwidget
, widget
);
182 // ----------------------------------------------------------------------------
183 // worker function implementing GTK*Mnemonics() functions
184 // ----------------------------------------------------------------------------
187 wxString
wxControl::GTKRemoveMnemonics(const wxString
& label
)
189 return wxGTKRemoveMnemonics(label
);
193 wxString
wxControl::GTKConvertMnemonics(const wxString
& label
)
195 return wxConvertMnemonicsToGTK(label
);
199 wxString
wxControl::GTKConvertMnemonicsWithMarkup(const wxString
& label
)
201 return wxConvertMnemonicsToGTKMarkup(label
);
204 // ----------------------------------------------------------------------------
205 // wxControl styles (a.k.a. attributes)
206 // ----------------------------------------------------------------------------
208 wxVisualAttributes
wxControl::GetDefaultAttributes() const
210 return GetDefaultAttributesFromGTKWidget(m_widget
,
216 wxControl::GetDefaultAttributesFromGTKWidget(GtkWidget
* widget
,
221 wxVisualAttributes attr
;
223 style
= gtk_rc_get_style(widget
);
225 style
= gtk_widget_get_default_style();
229 return wxWindow::GetClassDefaultAttributes(wxWINDOW_VARIANT_NORMAL
);
233 state
= GTK_STATE_NORMAL
;
235 // get the style's colours
236 attr
.colFg
= wxColour(style
->fg
[state
]);
238 attr
.colBg
= wxColour(style
->base
[state
]);
240 attr
.colBg
= wxColour(style
->bg
[state
]);
242 // get the style's font
243 if ( !style
->font_desc
)
244 style
= gtk_widget_get_default_style();
245 if ( style
&& style
->font_desc
)
247 wxNativeFontInfo info
;
248 info
.description
= pango_font_description_copy(style
->font_desc
);
249 attr
.font
= wxFont(info
);
253 GtkSettings
*settings
= gtk_settings_get_default();
254 gchar
*font_name
= NULL
;
255 g_object_get ( settings
,
260 attr
.font
= wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT
);
262 attr
.font
= wxFont(wxString::FromAscii(font_name
));
272 wxControl::GetDefaultAttributesFromGTKWidget(wxGtkWidgetNew_t widget_new
,
276 wxVisualAttributes attr
;
277 // NB: we need toplevel window so that GTK+ can find the right style
278 GtkWidget
*wnd
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
279 GtkWidget
* widget
= widget_new();
280 gtk_container_add(GTK_CONTAINER(wnd
), widget
);
281 attr
= GetDefaultAttributesFromGTKWidget(widget
, useBase
, state
);
282 gtk_widget_destroy(wnd
);
288 wxControl::GetDefaultAttributesFromGTKWidget(wxGtkWidgetNewFromStr_t widget_new
,
292 wxVisualAttributes attr
;
293 // NB: we need toplevel window so that GTK+ can find the right style
294 GtkWidget
*wnd
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
295 GtkWidget
* widget
= widget_new("");
296 gtk_container_add(GTK_CONTAINER(wnd
), widget
);
297 attr
= GetDefaultAttributesFromGTKWidget(widget
, useBase
, state
);
298 gtk_widget_destroy(wnd
);
305 wxControl::GetDefaultAttributesFromGTKWidget(wxGtkWidgetNewFromAdj_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(NULL
);
313 gtk_container_add(GTK_CONTAINER(wnd
), widget
);
314 attr
= GetDefaultAttributesFromGTKWidget(widget
, useBase
, state
);
315 gtk_widget_destroy(wnd
);
319 #endif // wxUSE_CONTROLS