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"
16 #include "wx/control.h"
17 #include "wx/fontutil.h"
18 #include "wx/settings.h"
19 #include "wx/gtk/private.h"
21 // ============================================================================
22 // wxControl implementation
23 // ============================================================================
25 // ----------------------------------------------------------------------------
27 // ----------------------------------------------------------------------------
29 IMPLEMENT_DYNAMIC_CLASS(wxControl
, wxWindow
)
31 wxControl::wxControl()
36 bool wxControl::Create( wxWindow
*parent
,
41 const wxValidator
& validator
,
42 const wxString
&name
)
44 bool ret
= wxWindow::Create(parent
, id
, pos
, size
, style
, name
);
47 SetValidator(validator
);
53 wxSize
wxControl::DoGetBestSize() const
55 // Do not return any arbitrary default value...
56 wxASSERT_MSG( m_widget
, wxT("DoGetBestSize called before creation") );
61 (* GTK_WIDGET_CLASS( GTK_OBJECT_GET_CLASS(m_widget
) )->size_request
)
64 wxSize
best(req
.width
, req
.height
);
70 void wxControl::PostCreation(const wxSize
& size
)
72 wxWindow::PostCreation();
74 // NB: GetBestSize needs to know the style, otherwise it will assume
75 // default font and if the user uses a different font, determined
76 // best size will be different (typically, smaller) than the desired
77 // size. This call ensure that a style is available at the time
78 // GetBestSize is called.
79 gtk_widget_ensure_style(m_widget
);
82 SetInitialBestSize(size
);
85 // ----------------------------------------------------------------------------
86 // wxControl dealing with labels
87 // ----------------------------------------------------------------------------
89 void wxControl::SetLabel( const wxString
&label
)
91 // keep the original string internally to be able to return it later (for
92 // consistency with the other ports)
98 wxString
wxControl::GetLabel() const
103 void wxControl::GTKSetLabelForLabel(GtkLabel
*w
, const wxString
& label
)
105 // don't call the virtual function which might call this one back again
106 wxControl::SetLabel(label
);
108 const wxString labelGTK
= GTKConvertMnemonics(label
);
111 gtk_label_set_text_with_mnemonic(w
, wxGTK_CONV(labelGTK
));
113 gtk_label_set(w
, wxGTK_CONV(labelGTK
));
117 void wxControl::GTKSetLabelForFrame(GtkFrame
*w
, const wxString
& label
)
119 wxControl::SetLabel(label
);
121 // frames don't support mnemonics even under GTK+ 2
122 const wxString labelGTK
= GTKRemoveMnemonics(label
);
124 gtk_frame_set_label(w
, labelGTK
.empty() ? (char *)NULL
125 : wxGTK_CONV(labelGTK
));
128 // worker function implementing both GTKConvert/RemoveMnemonics()
130 // notice that under GTK+ 1 we only really need to support MNEMONICS_REMOVE as
131 // it doesn't support mnemonics anyhow but this would make the code so ugly
132 // that we do the same thing for GKT+ 1 and 2
139 static wxString
GTKProcessMnemonics(const wxString
& label
, MnemonicsFlag flag
)
141 const size_t len
= label
.length();
143 labelGTK
.reserve(len
);
144 for ( size_t i
= 0; i
< len
; i
++ )
146 wxChar ch
= label
[i
];
153 // "&" at the end of string is an error
154 wxLogDebug(wxT("Invalid label \"%s\"."), label
.c_str());
158 ch
= label
[++i
]; // skip '&' itself
162 // special case: "&&" is not a mnemonic at all but just
164 labelGTK
+= wxT('&');
168 if ( flag
== MNEMONICS_CONVERT
)
170 // '_' can't be a GTK mnemonic apparently so
171 // replace it with something similar
172 labelGTK
+= wxT("_-");
178 if ( flag
== MNEMONICS_CONVERT
)
179 labelGTK
+= wxT('_');
185 if ( flag
== MNEMONICS_CONVERT
)
187 // escape any existing underlines in the string so that
188 // they don't become mnemonics accidentally
189 labelGTK
+= wxT("__");
203 wxString
wxControl::GTKRemoveMnemonics(const wxString
& label
)
205 return GTKProcessMnemonics(label
, MNEMONICS_REMOVE
);
209 wxString
wxControl::GTKConvertMnemonics(const wxString
& label
)
212 return GTKProcessMnemonics(label
, MNEMONICS_CONVERT
);
214 return GTKRemoveMnemonics(label
);
218 // ----------------------------------------------------------------------------
219 // wxControl styles (a.k.a. attributes)
220 // ----------------------------------------------------------------------------
222 wxVisualAttributes
wxControl::GetDefaultAttributes() const
224 return GetDefaultAttributesFromGTKWidget(m_widget
,
229 #define SHIFT (8*(sizeof(short int)-sizeof(char)))
233 wxControl::GetDefaultAttributesFromGTKWidget(GtkWidget
* widget
,
238 wxVisualAttributes attr
;
240 style
= gtk_rc_get_style(widget
);
242 style
= gtk_widget_get_default_style();
246 return wxWindow::GetClassDefaultAttributes(wxWINDOW_VARIANT_NORMAL
);
250 state
= GTK_STATE_NORMAL
;
252 // get the style's colours
253 attr
.colFg
= wxColour(style
->fg
[state
].red
>> SHIFT
,
254 style
->fg
[state
].green
>> SHIFT
,
255 style
->fg
[state
].blue
>> SHIFT
);
257 attr
.colBg
= wxColour(style
->base
[state
].red
>> SHIFT
,
258 style
->base
[state
].green
>> SHIFT
,
259 style
->base
[state
].blue
>> SHIFT
);
261 attr
.colBg
= wxColour(style
->bg
[state
].red
>> SHIFT
,
262 style
->bg
[state
].green
>> SHIFT
,
263 style
->bg
[state
].blue
>> SHIFT
);
265 // get the style's font
267 if ( !style
->font_desc
)
268 style
= gtk_widget_get_default_style();
269 if ( style
&& style
->font_desc
)
271 wxNativeFontInfo info
;
272 info
.description
= pango_font_description_copy(style
->font_desc
);
273 attr
.font
= wxFont(info
);
277 GtkSettings
*settings
= gtk_settings_get_default();
278 gchar
*font_name
= NULL
;
279 g_object_get ( settings
,
284 attr
.font
= wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT
);
286 attr
.font
= wxFont(wxString::FromAscii(font_name
));
290 // TODO: isn't there a way to get a standard gtk 1.2 font?
291 attr
.font
= wxFont( 12, wxSWISS
, wxNORMAL
, wxNORMAL
);
300 wxControl::GetDefaultAttributesFromGTKWidget(wxGtkWidgetNew_t widget_new
,
304 wxVisualAttributes attr
;
305 // NB: we need toplevel window so that GTK+ can find the right style
306 GtkWidget
*wnd
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
307 GtkWidget
* widget
= widget_new();
308 gtk_container_add(GTK_CONTAINER(wnd
), widget
);
309 attr
= GetDefaultAttributesFromGTKWidget(widget
, useBase
, state
);
310 gtk_widget_destroy(wnd
);
316 wxControl::GetDefaultAttributesFromGTKWidget(wxGtkWidgetNewFromStr_t widget_new
,
320 wxVisualAttributes attr
;
321 // NB: we need toplevel window so that GTK+ can find the right style
322 GtkWidget
*wnd
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
323 GtkWidget
* widget
= widget_new("");
324 gtk_container_add(GTK_CONTAINER(wnd
), widget
);
325 attr
= GetDefaultAttributesFromGTKWidget(widget
, useBase
, state
);
326 gtk_widget_destroy(wnd
);
333 wxControl::GetDefaultAttributesFromGTKWidget(wxGtkWidgetNewFromAdj_t widget_new
,
337 wxVisualAttributes attr
;
338 // NB: we need toplevel window so that GTK+ can find the right style
339 GtkWidget
*wnd
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
340 GtkWidget
* widget
= widget_new(NULL
);
341 gtk_container_add(GTK_CONTAINER(wnd
), widget
);
342 attr
= GetDefaultAttributesFromGTKWidget(widget
, useBase
, state
);
343 gtk_widget_destroy(wnd
);
347 #endif // wxUSE_CONTROLS