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/private/stattext.h"
28 // ============================================================================
29 // wxControl implementation
30 // ============================================================================
32 // ----------------------------------------------------------------------------
34 // ----------------------------------------------------------------------------
36 IMPLEMENT_DYNAMIC_CLASS(wxControl
, wxWindow
)
38 wxControl::wxControl()
42 bool wxControl::Create( wxWindow
*parent
,
47 const wxValidator
& validator
,
48 const wxString
&name
)
50 bool ret
= wxWindow::Create(parent
, id
, pos
, size
, style
, name
);
53 SetValidator(validator
);
59 wxSize
wxControl::DoGetBestSize() const
61 // Do not return any arbitrary default value...
62 wxASSERT_MSG( m_widget
, wxT("DoGetBestSize called before creation") );
67 (* GTK_WIDGET_CLASS( GTK_OBJECT_GET_CLASS(m_widget
) )->size_request
)
70 wxSize
best(req
.width
, req
.height
);
75 void wxControl::PostCreation(const wxSize
& size
)
77 wxWindow::PostCreation();
79 // NB: GetBestSize needs to know the style, otherwise it will assume
80 // default font and if the user uses a different font, determined
81 // best size will be different (typically, smaller) than the desired
82 // size. This call ensure that a style is available at the time
83 // GetBestSize is called.
84 gtk_widget_ensure_style(m_widget
);
90 // ----------------------------------------------------------------------------
91 // wxControl dealing with labels
92 // ----------------------------------------------------------------------------
94 void wxControl::GTKSetLabelForLabel(GtkLabel
*w
, const wxString
& label
)
96 // save the original label
97 wxControlBase::SetLabel(label
);
99 const wxString labelGTK
= GTKConvertMnemonics(label
);
100 gtk_label_set_text_with_mnemonic(w
, wxGTK_CONV(labelGTK
));
103 void wxControl::GTKSetLabelWithMarkupForLabel(GtkLabel
*w
, const wxString
& label
)
105 const wxString labelGTK
= GTKConvertMnemonicsWithMarkup(label
);
106 gtk_label_set_markup_with_mnemonic(w
, wxGTK_CONV(labelGTK
));
110 // ----------------------------------------------------------------------------
113 // GtkFrames do in fact support mnemonics in GTK2+ but not through
114 // gtk_frame_set_label, rather you need to use a custom label widget
115 // instead (idea gleaned from the native gtk font dialog code in GTK)
116 // ----------------------------------------------------------------------------
118 GtkWidget
* wxControl::GTKCreateFrame(const wxString
& label
)
120 const wxString labelGTK
= GTKConvertMnemonics(label
);
121 GtkWidget
* labelwidget
= gtk_label_new_with_mnemonic(wxGTK_CONV(labelGTK
));
122 gtk_widget_show(labelwidget
); // without this it won't show...
124 GtkWidget
* framewidget
= gtk_frame_new(NULL
);
125 gtk_frame_set_label_widget(GTK_FRAME(framewidget
), labelwidget
);
127 return framewidget
; // note that the label is already set so you'll
128 // only need to call wxControl::SetLabel afterwards
131 void wxControl::GTKSetLabelForFrame(GtkFrame
*w
, const wxString
& label
)
133 GtkLabel
* labelwidget
= GTK_LABEL(gtk_frame_get_label_widget(w
));
134 GTKSetLabelForLabel(labelwidget
, label
);
137 void wxControl::GTKFrameApplyWidgetStyle(GtkFrame
* w
, GtkRcStyle
* style
)
139 gtk_widget_modify_style(GTK_WIDGET(w
), style
);
140 gtk_widget_modify_style(gtk_frame_get_label_widget (w
), style
);
143 void wxControl::GTKFrameSetMnemonicWidget(GtkFrame
* w
, GtkWidget
* widget
)
145 GtkLabel
* labelwidget
= GTK_LABEL(gtk_frame_get_label_widget(w
));
147 gtk_label_set_mnemonic_widget(labelwidget
, widget
);
150 // ----------------------------------------------------------------------------
151 // worker function implementing GTK*Mnemonics() functions
152 // ----------------------------------------------------------------------------
158 MNEMONICS_CONVERT_MARKUP
161 static wxString
GTKProcessMnemonics(const wxString
& label
, MnemonicsFlag flag
)
164 labelGTK
.reserve(label
.length());
165 for ( wxString::const_iterator i
= label
.begin(); i
!= label
.end(); ++i
)
172 if ( i
+ 1 == label
.end() )
174 // "&" at the end of string is an error
175 wxLogDebug(wxT("Invalid label \"%s\"."), label
);
179 if ( flag
== MNEMONICS_CONVERT_MARKUP
)
181 bool isMnemonic
= true;
182 size_t distanceFromEnd
= label
.end() - i
;
184 // is this ampersand introducing a mnemonic or rather an entity?
185 for (size_t j
=0; j
< wxMARKUP_ENTITY_MAX
; j
++)
187 const wxChar
*entity
= wxMarkupEntities
[wxMARKUP_ELEMENT_NAME
][j
];
188 size_t entityLen
= wxStrlen(entity
);
190 if (distanceFromEnd
>= entityLen
&&
191 wxString(i
, i
+ entityLen
) == entity
)
194 i
+= entityLen
- 1; // the -1 is because main for()
195 // loop already increments i
206 ch
= *(++i
); // skip '&' itself
210 // special case: "&&" is not a mnemonic at all but just
212 if ( flag
== MNEMONICS_CONVERT_MARKUP
)
213 labelGTK
+= wxT("&");
215 labelGTK
+= wxT('&');
219 if ( flag
!= MNEMONICS_REMOVE
)
221 // '_' can't be a GTK mnemonic apparently so
222 // replace it with something similar
223 labelGTK
+= wxT("_-");
229 if ( flag
!= MNEMONICS_REMOVE
)
230 labelGTK
+= wxT('_');
236 if ( flag
!= MNEMONICS_REMOVE
)
238 // escape any existing underlines in the string so that
239 // they don't become mnemonics accidentally
240 labelGTK
+= wxT("__");
254 wxString
wxControl::GTKRemoveMnemonics(const wxString
& label
)
256 return GTKProcessMnemonics(label
, MNEMONICS_REMOVE
);
260 wxString
wxControl::GTKConvertMnemonics(const wxString
& label
)
262 return GTKProcessMnemonics(label
, MNEMONICS_CONVERT
);
266 wxString
wxControl::GTKConvertMnemonicsWithMarkup(const wxString
& label
)
268 return GTKProcessMnemonics(label
, MNEMONICS_CONVERT_MARKUP
);
271 // ----------------------------------------------------------------------------
272 // wxControl styles (a.k.a. attributes)
273 // ----------------------------------------------------------------------------
275 wxVisualAttributes
wxControl::GetDefaultAttributes() const
277 return GetDefaultAttributesFromGTKWidget(m_widget
,
283 wxControl::GetDefaultAttributesFromGTKWidget(GtkWidget
* widget
,
288 wxVisualAttributes attr
;
290 style
= gtk_rc_get_style(widget
);
292 style
= gtk_widget_get_default_style();
296 return wxWindow::GetClassDefaultAttributes(wxWINDOW_VARIANT_NORMAL
);
300 state
= GTK_STATE_NORMAL
;
302 // get the style's colours
303 attr
.colFg
= wxColour(style
->fg
[state
]);
305 attr
.colBg
= wxColour(style
->base
[state
]);
307 attr
.colBg
= wxColour(style
->bg
[state
]);
309 // get the style's font
310 if ( !style
->font_desc
)
311 style
= gtk_widget_get_default_style();
312 if ( style
&& style
->font_desc
)
314 wxNativeFontInfo info
;
315 info
.description
= pango_font_description_copy(style
->font_desc
);
316 attr
.font
= wxFont(info
);
320 GtkSettings
*settings
= gtk_settings_get_default();
321 gchar
*font_name
= NULL
;
322 g_object_get ( settings
,
327 attr
.font
= wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT
);
329 attr
.font
= wxFont(wxString::FromAscii(font_name
));
339 wxControl::GetDefaultAttributesFromGTKWidget(wxGtkWidgetNew_t widget_new
,
343 wxVisualAttributes attr
;
344 // NB: we need toplevel window so that GTK+ can find the right style
345 GtkWidget
*wnd
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
346 GtkWidget
* widget
= widget_new();
347 gtk_container_add(GTK_CONTAINER(wnd
), widget
);
348 attr
= GetDefaultAttributesFromGTKWidget(widget
, useBase
, state
);
349 gtk_widget_destroy(wnd
);
355 wxControl::GetDefaultAttributesFromGTKWidget(wxGtkWidgetNewFromStr_t widget_new
,
359 wxVisualAttributes attr
;
360 // NB: we need toplevel window so that GTK+ can find the right style
361 GtkWidget
*wnd
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
362 GtkWidget
* widget
= widget_new("");
363 gtk_container_add(GTK_CONTAINER(wnd
), widget
);
364 attr
= GetDefaultAttributesFromGTKWidget(widget
, useBase
, state
);
365 gtk_widget_destroy(wnd
);
372 wxControl::GetDefaultAttributesFromGTKWidget(wxGtkWidgetNewFromAdj_t widget_new
,
376 wxVisualAttributes attr
;
377 // NB: we need toplevel window so that GTK+ can find the right style
378 GtkWidget
*wnd
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
379 GtkWidget
* widget
= widget_new(NULL
);
380 gtk_container_add(GTK_CONTAINER(wnd
), widget
);
381 attr
= GetDefaultAttributesFromGTKWidget(widget
, useBase
, state
);
382 gtk_widget_destroy(wnd
);
386 // ----------------------------------------------------------------------------
388 // ----------------------------------------------------------------------------
390 void wxControl::OnInternalIdle()
392 if ( GtkShowFromOnIdle() )
395 if ( GTK_WIDGET_REALIZED(m_widget
) )
399 GTKSetDelayedFocusIfNeeded();
402 if ( wxUpdateUIEvent::CanUpdate(this) )
403 UpdateWindowUI(wxUPDATE_UI_FROMIDLE
);
406 #endif // wxUSE_CONTROLS