]>
git.saurik.com Git - wxWidgets.git/blob - src/gtk1/control.cpp
1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling, Julian Smart and Vadim Zeitlin
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
11 #pragma implementation "control.h"
14 // For compilers that support precompilation, includes "wx.h".
15 #include "wx/wxprec.h"
21 #include "wx/control.h"
22 #include "wx/fontutil.h"
23 #include "wx/settings.h"
27 //-----------------------------------------------------------------------------
29 //-----------------------------------------------------------------------------
31 IMPLEMENT_DYNAMIC_CLASS(wxControl
, wxWindow
)
33 wxControl::wxControl()
38 bool wxControl::Create( wxWindow
*parent
,
43 const wxValidator
& validator
,
44 const wxString
&name
)
46 bool ret
= wxWindow::Create(parent
, id
, pos
, size
, style
, name
);
49 SetValidator(validator
);
55 void wxControl::SetLabel( const wxString
&label
)
58 for ( const wxChar
*pc
= label
; *pc
!= wxT('\0'); pc
++ )
60 if ( *pc
== wxT('&') )
63 #if 0 // it would be unused anyhow for now - kbd interface not done yet
64 if ( *pc
!= wxT('&') ) m_chAccel
= *pc
;
71 wxString
wxControl::GetLabel() const
77 wxSize
wxControl::DoGetBestSize() const
79 // Do not return any arbitrary default value...
80 wxASSERT_MSG( m_widget
, wxT("DoGetBestSize called before creation") );
85 (* GTK_WIDGET_CLASS( GTK_OBJECT_GET_CLASS(m_widget
) )->size_request
)
88 return wxSize(req
.width
, req
.height
);
92 void wxControl::PostCreation(const wxSize
& size
)
94 wxWindow::PostCreation();
96 // NB: GetBestSize needs to know the style, otherwise it will assume
97 // default font and if the user uses a different font, determined
98 // best size will be different (typically, smaller) than the desired
99 // size. This call ensure that a style is available at the time
100 // GetBestSize is called.
101 gtk_widget_ensure_style(m_widget
);
105 SetInitialBestSize(size
);
110 wxString
wxControl::PrepareLabelMnemonics( const wxString
&label
) const
112 //Format mnemonics properly for GTK2. This can be called from GTK1.x, but
113 //it's not very useful because mnemonics don't exist prior to GTK2.
115 for (size_t i
= 0; i
< label
.Len(); i
++)
117 if (label
.GetChar(i
) == wxT('&'))
119 //Mnemonic escape sequence "&&" is a literal "&" in the output.
120 if (label
.GetChar(i
+ 1) == wxT('&'))
125 //Handle special case of "&_" (i.e. "_" is the mnemonic).
126 //FIXME - Is it possible to use "_" as a GTK mnemonic? Just use a
128 else if (label
.GetChar(i
+ 1) == wxT('_'))
133 //Replace WX mnemonic indicator "&" with GTK indicator "_".
139 else if (label
.GetChar(i
) == wxT('_'))
141 //Escape any underlines in the string so GTK doesn't use them.
146 label2
<< label
.GetChar(i
);
154 wxVisualAttributes
wxControl::GetDefaultAttributes() const
156 return GetDefaultAttributesFromGTKWidget(m_widget
,
161 #define SHIFT (8*(sizeof(short int)-sizeof(char)))
165 wxControl::GetDefaultAttributesFromGTKWidget(GtkWidget
* widget
,
170 wxVisualAttributes attr
;
172 style
= gtk_rc_get_style(widget
);
174 style
= gtk_widget_get_default_style();
178 return wxWindow::GetClassDefaultAttributes(wxWINDOW_VARIANT_NORMAL
);
182 state
= GTK_STATE_NORMAL
;
184 // get the style's colours
185 attr
.colFg
= wxColour(style
->fg
[state
].red
>> SHIFT
,
186 style
->fg
[state
].green
>> SHIFT
,
187 style
->fg
[state
].blue
>> SHIFT
);
189 attr
.colBg
= wxColour(style
->base
[state
].red
>> SHIFT
,
190 style
->base
[state
].green
>> SHIFT
,
191 style
->base
[state
].blue
>> SHIFT
);
193 attr
.colBg
= wxColour(style
->bg
[state
].red
>> SHIFT
,
194 style
->bg
[state
].green
>> SHIFT
,
195 style
->bg
[state
].blue
>> SHIFT
);
197 // get the style's font
199 if ( !style
->font_desc
)
200 style
= gtk_widget_get_default_style();
201 if ( style
&& style
->font_desc
)
203 wxNativeFontInfo info
;
204 info
.description
= style
->font_desc
;
205 attr
.font
= wxFont(info
);
209 GtkSettings
*settings
= gtk_settings_get_default();
210 gchar
*font_name
= NULL
;
211 g_object_get ( settings
,
216 attr
.font
= wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT
);
218 attr
.font
= wxFont(wxString::FromAscii(font_name
));
222 // TODO: isn't there a way to get a standard gtk 1.2 font?
223 attr
.font
= wxFont( 12, wxSWISS
, wxNORMAL
, wxNORMAL
);
232 wxControl::GetDefaultAttributesFromGTKWidget(GtkWidget
* (*widget_new
)(void),
236 wxVisualAttributes attr
;
237 GtkWidget
* widget
= widget_new();
238 attr
= GetDefaultAttributesFromGTKWidget(widget
, useBase
, state
);
239 gtk_widget_destroy(widget
);
245 wxControl::GetDefaultAttributesFromGTKWidget(GtkWidget
* (*widget_new
)(const gchar
*),
249 wxVisualAttributes attr
;
250 GtkWidget
* widget
= widget_new("");
251 attr
= GetDefaultAttributesFromGTKWidget(widget
, useBase
, state
);
252 gtk_widget_destroy(widget
);
259 wxControl::GetDefaultAttributesFromGTKWidget(GtkWidget
* (*widget_new
)(GtkAdjustment
*),
263 wxVisualAttributes attr
;
264 GtkWidget
* widget
= widget_new(NULL
);
265 attr
= GetDefaultAttributesFromGTKWidget(widget
, useBase
, state
);
266 gtk_widget_destroy(widget
);
270 void wxControl::ApplyWidgetStyle(bool forceStyle
)
272 GtkRcStyle
*style
= CreateWidgetStyle(forceStyle
);
275 DoApplyWidgetStyle(style
);
276 gtk_rc_style_unref(style
);
280 void wxControl::DoApplyWidgetStyle(GtkRcStyle
*style
)
282 gtk_widget_modify_style(m_widget
, style
);
286 #endif // wxUSE_CONTROLS