1 /////////////////////////////////////////////////////////////////////////////
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"
17 #include "wx/control.h"
18 #include "wx/fontutil.h"
19 #include "wx/settings.h"
23 //-----------------------------------------------------------------------------
25 //-----------------------------------------------------------------------------
27 IMPLEMENT_DYNAMIC_CLASS(wxControl
, wxWindow
)
29 wxControl::wxControl()
34 bool wxControl::Create( wxWindow
*parent
,
39 const wxValidator
& validator
,
40 const wxString
&name
)
42 bool ret
= wxWindow::Create(parent
, id
, pos
, size
, style
, name
);
45 SetValidator(validator
);
51 void wxControl::SetLabel( const wxString
&label
)
54 for ( const wxChar
*pc
= label
; *pc
!= wxT('\0'); pc
++ )
56 if ( *pc
== wxT('&') )
59 #if 0 // it would be unused anyhow for now - kbd interface not done yet
60 if ( *pc
!= wxT('&') ) m_chAccel
= *pc
;
68 wxString
wxControl::GetLabel() const
74 wxSize
wxControl::DoGetBestSize() const
76 // Do not return any arbitrary default value...
77 wxASSERT_MSG( m_widget
, wxT("DoGetBestSize called before creation") );
82 (* GTK_WIDGET_CLASS( GTK_OBJECT_GET_CLASS(m_widget
) )->size_request
)
85 wxSize
best(req
.width
, req
.height
);
91 void wxControl::PostCreation(const wxSize
& size
)
93 wxWindow::PostCreation();
95 // NB: GetBestSize needs to know the style, otherwise it will assume
96 // default font and if the user uses a different font, determined
97 // best size will be different (typically, smaller) than the desired
98 // size. This call ensure that a style is available at the time
99 // GetBestSize is called.
100 gtk_widget_ensure_style(m_widget
);
103 SetInitialBestSize(size
);
108 wxString
wxControl::PrepareLabelMnemonics( const wxString
&label
) const
110 //Format mnemonics properly for GTK2. This can be called from GTK1.x, but
111 //it's not very useful because mnemonics don't exist prior to GTK2.
113 for (size_t i
= 0; i
< label
.Len(); i
++)
115 if (label
.GetChar(i
) == wxT('&'))
117 //Mnemonic escape sequence "&&" is a literal "&" in the output.
118 if (label
.GetChar(i
+ 1) == wxT('&'))
123 //Handle special case of "&_" (i.e. "_" is the mnemonic).
124 //FIXME - Is it possible to use "_" as a GTK mnemonic? Just use a
126 else if (label
.GetChar(i
+ 1) == wxT('_'))
131 //Replace WX mnemonic indicator "&" with GTK indicator "_".
137 else if (label
.GetChar(i
) == wxT('_'))
139 //Escape any underlines in the string so GTK doesn't use them.
144 label2
<< label
.GetChar(i
);
152 wxVisualAttributes
wxControl::GetDefaultAttributes() const
154 return GetDefaultAttributesFromGTKWidget(m_widget
,
159 #define SHIFT (8*(sizeof(short int)-sizeof(char)))
163 wxControl::GetDefaultAttributesFromGTKWidget(GtkWidget
* widget
,
168 wxVisualAttributes attr
;
170 style
= gtk_rc_get_style(widget
);
172 style
= gtk_widget_get_default_style();
176 return wxWindow::GetClassDefaultAttributes(wxWINDOW_VARIANT_NORMAL
);
180 state
= GTK_STATE_NORMAL
;
182 // get the style's colours
183 attr
.colFg
= wxColour(style
->fg
[state
].red
>> SHIFT
,
184 style
->fg
[state
].green
>> SHIFT
,
185 style
->fg
[state
].blue
>> SHIFT
);
187 attr
.colBg
= wxColour(style
->base
[state
].red
>> SHIFT
,
188 style
->base
[state
].green
>> SHIFT
,
189 style
->base
[state
].blue
>> SHIFT
);
191 attr
.colBg
= wxColour(style
->bg
[state
].red
>> SHIFT
,
192 style
->bg
[state
].green
>> SHIFT
,
193 style
->bg
[state
].blue
>> SHIFT
);
195 // get the style's font
197 if ( !style
->font_desc
)
198 style
= gtk_widget_get_default_style();
199 if ( style
&& style
->font_desc
)
201 wxNativeFontInfo info
;
202 info
.description
= pango_font_description_copy(style
->font_desc
);
203 attr
.font
= wxFont(info
);
207 GtkSettings
*settings
= gtk_settings_get_default();
208 gchar
*font_name
= NULL
;
209 g_object_get ( settings
,
214 attr
.font
= wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT
);
216 attr
.font
= wxFont(wxString::FromAscii(font_name
));
220 // TODO: isn't there a way to get a standard gtk 1.2 font?
221 attr
.font
= wxFont( 12, wxSWISS
, wxNORMAL
, wxNORMAL
);
230 wxControl::GetDefaultAttributesFromGTKWidget(wxGtkWidgetNew_t widget_new
,
234 wxVisualAttributes attr
;
235 // NB: we need toplevel window so that GTK+ can find the right style
236 GtkWidget
*wnd
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
237 GtkWidget
* widget
= widget_new();
238 gtk_container_add(GTK_CONTAINER(wnd
), widget
);
239 attr
= GetDefaultAttributesFromGTKWidget(widget
, useBase
, state
);
240 gtk_widget_destroy(wnd
);
246 wxControl::GetDefaultAttributesFromGTKWidget(wxGtkWidgetNewFromStr_t widget_new
,
250 wxVisualAttributes attr
;
251 // NB: we need toplevel window so that GTK+ can find the right style
252 GtkWidget
*wnd
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
253 GtkWidget
* widget
= widget_new("");
254 gtk_container_add(GTK_CONTAINER(wnd
), widget
);
255 attr
= GetDefaultAttributesFromGTKWidget(widget
, useBase
, state
);
256 gtk_widget_destroy(wnd
);
263 wxControl::GetDefaultAttributesFromGTKWidget(wxGtkWidgetNewFromAdj_t widget_new
,
267 wxVisualAttributes attr
;
268 // NB: we need toplevel window so that GTK+ can find the right style
269 GtkWidget
*wnd
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
270 GtkWidget
* widget
= widget_new(NULL
);
271 gtk_container_add(GTK_CONTAINER(wnd
), widget
);
272 attr
= GetDefaultAttributesFromGTKWidget(widget
, useBase
, state
);
273 gtk_widget_destroy(wnd
);
277 #endif // wxUSE_CONTROLS