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/gtk/private/mnemonics.h"
27 // ============================================================================
28 // wxControl implementation
29 // ============================================================================
31 // ----------------------------------------------------------------------------
33 // ----------------------------------------------------------------------------
35 IMPLEMENT_DYNAMIC_CLASS(wxControl
, wxWindow
)
37 wxControl::wxControl()
41 bool wxControl::Create( wxWindow
*parent
,
46 const wxValidator
& validator
,
47 const wxString
&name
)
49 bool ret
= wxWindow::Create(parent
, id
, pos
, size
, style
, name
);
52 SetValidator(validator
);
58 wxSize
wxControl::DoGetBestSize() const
60 // Do not return any arbitrary default value...
61 wxASSERT_MSG( m_widget
, wxT("DoGetBestSize called before creation") );
66 (* GTK_WIDGET_CLASS( GTK_OBJECT_GET_CLASS(m_widget
) )->size_request
)
69 wxSize
best(req
.width
, req
.height
);
74 void wxControl::PostCreation(const wxSize
& size
)
76 wxWindow::PostCreation();
78 // NB: GetBestSize needs to know the style, otherwise it will assume
79 // default font and if the user uses a different font, determined
80 // best size will be different (typically, smaller) than the desired
81 // size. This call ensure that a style is available at the time
82 // GetBestSize is called.
83 gtk_widget_ensure_style(m_widget
);
89 // ----------------------------------------------------------------------------
90 // wxControl dealing with labels
91 // ----------------------------------------------------------------------------
93 void wxControl::GTKSetLabelForLabel(GtkLabel
*w
, const wxString
& label
)
95 // save the original label
96 wxControlBase::SetLabel(label
);
98 const wxString labelGTK
= GTKConvertMnemonics(label
);
99 gtk_label_set_text_with_mnemonic(w
, wxGTK_CONV(labelGTK
));
102 void wxControl::GTKSetLabelWithMarkupForLabel(GtkLabel
*w
, const wxString
& label
)
104 const wxString labelGTK
= GTKConvertMnemonicsWithMarkup(label
);
105 gtk_label_set_markup_with_mnemonic(w
, wxGTK_CONV(labelGTK
));
109 // ----------------------------------------------------------------------------
112 // GtkFrames do in fact support mnemonics in GTK2+ but not through
113 // gtk_frame_set_label, rather you need to use a custom label widget
114 // instead (idea gleaned from the native gtk font dialog code in GTK)
115 // ----------------------------------------------------------------------------
117 GtkWidget
* wxControl::GTKCreateFrame(const wxString
& label
)
119 const wxString labelGTK
= GTKConvertMnemonics(label
);
120 GtkWidget
* labelwidget
= gtk_label_new_with_mnemonic(wxGTK_CONV(labelGTK
));
121 gtk_widget_show(labelwidget
); // without this it won't show...
123 GtkWidget
* framewidget
= gtk_frame_new(NULL
);
124 gtk_frame_set_label_widget(GTK_FRAME(framewidget
), labelwidget
);
126 return framewidget
; // note that the label is already set so you'll
127 // only need to call wxControl::SetLabel afterwards
130 void wxControl::GTKSetLabelForFrame(GtkFrame
*w
, const wxString
& label
)
132 GtkLabel
* labelwidget
= GTK_LABEL(gtk_frame_get_label_widget(w
));
133 GTKSetLabelForLabel(labelwidget
, label
);
136 void wxControl::GTKFrameApplyWidgetStyle(GtkFrame
* w
, GtkRcStyle
* style
)
138 gtk_widget_modify_style(GTK_WIDGET(w
), style
);
139 gtk_widget_modify_style(gtk_frame_get_label_widget (w
), style
);
142 void wxControl::GTKFrameSetMnemonicWidget(GtkFrame
* w
, GtkWidget
* widget
)
144 GtkLabel
* labelwidget
= GTK_LABEL(gtk_frame_get_label_widget(w
));
146 gtk_label_set_mnemonic_widget(labelwidget
, widget
);
149 // ----------------------------------------------------------------------------
150 // worker function implementing GTK*Mnemonics() functions
151 // ----------------------------------------------------------------------------
154 wxString
wxControl::GTKRemoveMnemonics(const wxString
& label
)
156 return wxGTKRemoveMnemonics(label
);
160 wxString
wxControl::GTKConvertMnemonics(const wxString
& label
)
162 return wxConvertMnemonicsToGTK(label
);
166 wxString
wxControl::GTKConvertMnemonicsWithMarkup(const wxString
& label
)
168 return wxConvertMnemonicsToGTKMarkup(label
);
171 // ----------------------------------------------------------------------------
172 // wxControl styles (a.k.a. attributes)
173 // ----------------------------------------------------------------------------
175 wxVisualAttributes
wxControl::GetDefaultAttributes() const
177 return GetDefaultAttributesFromGTKWidget(m_widget
,
183 wxControl::GetDefaultAttributesFromGTKWidget(GtkWidget
* widget
,
188 wxVisualAttributes attr
;
190 style
= gtk_rc_get_style(widget
);
192 style
= gtk_widget_get_default_style();
196 return wxWindow::GetClassDefaultAttributes(wxWINDOW_VARIANT_NORMAL
);
200 state
= GTK_STATE_NORMAL
;
202 // get the style's colours
203 attr
.colFg
= wxColour(style
->fg
[state
]);
205 attr
.colBg
= wxColour(style
->base
[state
]);
207 attr
.colBg
= wxColour(style
->bg
[state
]);
209 // get the style's font
210 if ( !style
->font_desc
)
211 style
= gtk_widget_get_default_style();
212 if ( style
&& style
->font_desc
)
214 wxNativeFontInfo info
;
215 info
.description
= pango_font_description_copy(style
->font_desc
);
216 attr
.font
= wxFont(info
);
220 GtkSettings
*settings
= gtk_settings_get_default();
221 gchar
*font_name
= NULL
;
222 g_object_get ( settings
,
227 attr
.font
= wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT
);
229 attr
.font
= wxFont(wxString::FromAscii(font_name
));
239 wxControl::GetDefaultAttributesFromGTKWidget(wxGtkWidgetNew_t widget_new
,
243 wxVisualAttributes attr
;
244 // NB: we need toplevel window so that GTK+ can find the right style
245 GtkWidget
*wnd
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
246 GtkWidget
* widget
= widget_new();
247 gtk_container_add(GTK_CONTAINER(wnd
), widget
);
248 attr
= GetDefaultAttributesFromGTKWidget(widget
, useBase
, state
);
249 gtk_widget_destroy(wnd
);
255 wxControl::GetDefaultAttributesFromGTKWidget(wxGtkWidgetNewFromStr_t widget_new
,
259 wxVisualAttributes attr
;
260 // NB: we need toplevel window so that GTK+ can find the right style
261 GtkWidget
*wnd
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
262 GtkWidget
* widget
= widget_new("");
263 gtk_container_add(GTK_CONTAINER(wnd
), widget
);
264 attr
= GetDefaultAttributesFromGTKWidget(widget
, useBase
, state
);
265 gtk_widget_destroy(wnd
);
272 wxControl::GetDefaultAttributesFromGTKWidget(wxGtkWidgetNewFromAdj_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(NULL
);
280 gtk_container_add(GTK_CONTAINER(wnd
), widget
);
281 attr
= GetDefaultAttributesFromGTKWidget(widget
, useBase
, state
);
282 gtk_widget_destroy(wnd
);
286 // ----------------------------------------------------------------------------
288 // ----------------------------------------------------------------------------
290 void wxControl::OnInternalIdle()
292 if ( GtkShowFromOnIdle() )
295 if ( GTK_WIDGET_REALIZED(m_widget
) )
299 GTKSetDelayedFocusIfNeeded();
302 if ( wxUpdateUIEvent::CanUpdate(this) )
303 UpdateWindowUI(wxUPDATE_UI_FROMIDLE
);
306 #endif // wxUSE_CONTROLS