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 // ============================================================================
26 // wxControl implementation
27 // ============================================================================
29 // ----------------------------------------------------------------------------
31 // ----------------------------------------------------------------------------
33 IMPLEMENT_DYNAMIC_CLASS(wxControl
, wxWindow
)
35 wxControl::wxControl()
40 bool wxControl::Create( wxWindow
*parent
,
45 const wxValidator
& validator
,
46 const wxString
&name
)
48 bool ret
= wxWindow::Create(parent
, id
, pos
, size
, style
, name
);
51 SetValidator(validator
);
57 wxSize
wxControl::DoGetBestSize() const
59 // Do not return any arbitrary default value...
60 wxASSERT_MSG( m_widget
, wxT("DoGetBestSize called before creation") );
65 (* GTK_WIDGET_CLASS( GTK_OBJECT_GET_CLASS(m_widget
) )->size_request
)
68 wxSize
best(req
.width
, req
.height
);
73 void wxControl::PostCreation(const wxSize
& size
)
75 wxWindow::PostCreation();
77 // NB: GetBestSize needs to know the style, otherwise it will assume
78 // default font and if the user uses a different font, determined
79 // best size will be different (typically, smaller) than the desired
80 // size. This call ensure that a style is available at the time
81 // GetBestSize is called.
82 gtk_widget_ensure_style(m_widget
);
88 // ----------------------------------------------------------------------------
89 // wxControl dealing with labels
90 // ----------------------------------------------------------------------------
92 void wxControl::SetLabel( const wxString
&label
)
94 // keep the original string internally to be able to return it later (for
95 // consistency with the other ports)
101 wxString
wxControl::GetLabel() const
106 void wxControl::GTKSetLabelForLabel(GtkLabel
*w
, const wxString
& label
)
108 // don't call the virtual function which might call this one back again
109 wxControl::SetLabel(label
);
111 const wxString labelGTK
= GTKConvertMnemonics(label
);
113 gtk_label_set_text_with_mnemonic(w
, wxGTK_CONV(labelGTK
));
116 // ----------------------------------------------------------------------------
119 // GtkFrames do in fact support mnemonics in GTK2+ but not through
120 // gtk_frame_set_label, rather you need to use a custom label widget
121 // instead (idea gleaned from the native gtk font dialog code in GTK)
122 // ----------------------------------------------------------------------------
124 GtkWidget
* wxControl::GTKCreateFrame(const wxString
& label
)
126 const wxString labelGTK
= GTKConvertMnemonics(label
);
127 GtkWidget
* labelwidget
= gtk_label_new_with_mnemonic(wxGTK_CONV(labelGTK
));
128 gtk_widget_show(labelwidget
); // without this it won't show...
130 GtkWidget
* framewidget
= gtk_frame_new(NULL
);
131 gtk_frame_set_label_widget(GTK_FRAME(framewidget
), labelwidget
);
133 return framewidget
; //note that the label is already set so you'll
134 //only need to call wxControl::SetLabel afterwards
137 void wxControl::GTKSetLabelForFrame(GtkFrame
*w
, const wxString
& label
)
139 GtkLabel
* labelwidget
= GTK_LABEL(gtk_frame_get_label_widget(w
));
140 GTKSetLabelForLabel(labelwidget
, label
);
143 void wxControl::GTKFrameApplyWidgetStyle(GtkFrame
* w
, GtkRcStyle
* style
)
145 gtk_widget_modify_style(GTK_WIDGET(w
), style
);
146 gtk_widget_modify_style(gtk_frame_get_label_widget (w
), style
);
149 void wxControl::GTKFrameSetMnemonicWidget(GtkFrame
* w
, GtkWidget
* widget
)
151 GtkLabel
* labelwidget
= GTK_LABEL(gtk_frame_get_label_widget(w
));
153 gtk_label_set_mnemonic_widget(labelwidget
, widget
);
156 // ----------------------------------------------------------------------------
157 // worker function implementing both GTKConvert/RemoveMnemonics()
159 // notice that under GTK+ 1 we only really need to support MNEMONICS_REMOVE as
160 // it doesn't support mnemonics anyhow but this would make the code so ugly
161 // that we do the same thing for GKT+ 1 and 2
162 // ----------------------------------------------------------------------------
170 static wxString
GTKProcessMnemonics(const wxString
& label
, MnemonicsFlag flag
)
172 const size_t len
= label
.length();
174 labelGTK
.reserve(len
);
175 for ( size_t i
= 0; i
< len
; i
++ )
177 wxChar ch
= label
[i
];
184 // "&" at the end of string is an error
185 wxLogDebug(wxT("Invalid label \"%s\"."), label
.c_str());
189 ch
= label
[++i
]; // skip '&' itself
193 // special case: "&&" is not a mnemonic at all but just
195 labelGTK
+= wxT('&');
199 if ( flag
== MNEMONICS_CONVERT
)
201 // '_' can't be a GTK mnemonic apparently so
202 // replace it with something similar
203 labelGTK
+= wxT("_-");
209 if ( flag
== MNEMONICS_CONVERT
)
210 labelGTK
+= wxT('_');
216 if ( flag
== MNEMONICS_CONVERT
)
218 // escape any existing underlines in the string so that
219 // they don't become mnemonics accidentally
220 labelGTK
+= wxT("__");
234 wxString
wxControl::GTKRemoveMnemonics(const wxString
& label
)
236 return GTKProcessMnemonics(label
, MNEMONICS_REMOVE
);
240 wxString
wxControl::GTKConvertMnemonics(const wxString
& label
)
242 return GTKProcessMnemonics(label
, MNEMONICS_CONVERT
);
245 // ----------------------------------------------------------------------------
246 // wxControl styles (a.k.a. attributes)
247 // ----------------------------------------------------------------------------
249 wxVisualAttributes
wxControl::GetDefaultAttributes() const
251 return GetDefaultAttributesFromGTKWidget(m_widget
,
257 wxControl::GetDefaultAttributesFromGTKWidget(GtkWidget
* widget
,
262 wxVisualAttributes attr
;
264 style
= gtk_rc_get_style(widget
);
266 style
= gtk_widget_get_default_style();
270 return wxWindow::GetClassDefaultAttributes(wxWINDOW_VARIANT_NORMAL
);
274 state
= GTK_STATE_NORMAL
;
276 // get the style's colours
277 attr
.colFg
= wxColour(style
->fg
[state
]);
279 attr
.colBg
= wxColour(style
->base
[state
]);
281 attr
.colBg
= wxColour(style
->bg
[state
]);
283 // get the style's font
284 if ( !style
->font_desc
)
285 style
= gtk_widget_get_default_style();
286 if ( style
&& style
->font_desc
)
288 wxNativeFontInfo info
;
289 info
.description
= pango_font_description_copy(style
->font_desc
);
290 attr
.font
= wxFont(info
);
294 GtkSettings
*settings
= gtk_settings_get_default();
295 gchar
*font_name
= NULL
;
296 g_object_get ( settings
,
301 attr
.font
= wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT
);
303 attr
.font
= wxFont(wxString::FromAscii(font_name
));
313 wxControl::GetDefaultAttributesFromGTKWidget(wxGtkWidgetNew_t widget_new
,
317 wxVisualAttributes attr
;
318 // NB: we need toplevel window so that GTK+ can find the right style
319 GtkWidget
*wnd
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
320 GtkWidget
* widget
= widget_new();
321 gtk_container_add(GTK_CONTAINER(wnd
), widget
);
322 attr
= GetDefaultAttributesFromGTKWidget(widget
, useBase
, state
);
323 gtk_widget_destroy(wnd
);
329 wxControl::GetDefaultAttributesFromGTKWidget(wxGtkWidgetNewFromStr_t widget_new
,
333 wxVisualAttributes attr
;
334 // NB: we need toplevel window so that GTK+ can find the right style
335 GtkWidget
*wnd
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
336 GtkWidget
* widget
= widget_new("");
337 gtk_container_add(GTK_CONTAINER(wnd
), widget
);
338 attr
= GetDefaultAttributesFromGTKWidget(widget
, useBase
, state
);
339 gtk_widget_destroy(wnd
);
346 wxControl::GetDefaultAttributesFromGTKWidget(wxGtkWidgetNewFromAdj_t widget_new
,
350 wxVisualAttributes attr
;
351 // NB: we need toplevel window so that GTK+ can find the right style
352 GtkWidget
*wnd
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
353 GtkWidget
* widget
= widget_new(NULL
);
354 gtk_container_add(GTK_CONTAINER(wnd
), widget
);
355 attr
= GetDefaultAttributesFromGTKWidget(widget
, useBase
, state
);
356 gtk_widget_destroy(wnd
);
360 // ----------------------------------------------------------------------------
362 // ----------------------------------------------------------------------------
364 void wxControl::OnInternalIdle()
366 if ( GtkShowFromOnIdle() )
369 if ( GTK_WIDGET_REALIZED(m_widget
) )
373 GTKSetDelayedFocusIfNeeded();
376 if ( wxUpdateUIEvent::CanUpdate(this) )
377 UpdateWindowUI(wxUPDATE_UI_FROMIDLE
);
380 #endif // wxUSE_CONTROLS