]>
git.saurik.com Git - wxWidgets.git/blob - src/gtk/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()
36 m_createComplete
= false;
39 bool wxControl::Create( wxWindow
*parent
,
44 const wxValidator
& validator
,
45 const wxString
&name
)
47 m_createComplete
= false;
48 bool ret
= wxWindow::Create(parent
, id
, pos
, size
, style
, name
);
51 SetValidator(validator
);
57 void wxControl::SetLabel( const wxString
&label
)
60 for ( const wxChar
*pc
= label
; *pc
!= wxT('\0'); pc
++ )
62 if ( *pc
== wxT('&') )
65 #if 0 // it would be unused anyhow for now - kbd interface not done yet
66 if ( *pc
!= wxT('&') ) m_chAccel
= *pc
;
73 void wxControl::PostSetLabel()
75 // make sure the widget has been created, and that PostCreate has already
77 if (m_widget
&& m_createComplete
&& GetAdjustMinSizeFlag())
78 SetBestSize(wxDefaultSize
);
81 wxString
wxControl::GetLabel() const
87 wxSize
wxControl::DoGetBestSize() const
89 // Do not return any arbitrary default value...
90 wxASSERT_MSG( m_widget
, wxT("DoGetBestSize called before creation") );
95 (* GTK_WIDGET_CLASS( GTK_OBJECT_GET_CLASS(m_widget
) )->size_request
)
98 return wxSize(req
.width
, req
.height
);
102 void wxControl::PostCreation(const wxSize
& size
)
104 wxWindow::PostCreation();
106 // NB: GetBestSize needs to know the style, otherwise it will assume
107 // default font and if the user uses a different font, determined
108 // best size will be different (typically, smaller) than the desired
109 // size. This call ensure that a style is available at the time
110 // GetBestSize is called.
111 gtk_widget_ensure_style(m_widget
);
115 SetInitialBestSize(size
);
116 m_createComplete
= true;
121 wxString
wxControl::PrepareLabelMnemonics( const wxString
&label
) const
123 //Format mnemonics properly for GTK2. This can be called from GTK1.x, but
124 //it's not very useful because mnemonics don't exist prior to GTK2.
126 for (size_t i
= 0; i
< label
.Len(); i
++)
128 if (label
.GetChar(i
) == wxT('&'))
130 //Mnemonic escape sequence "&&" is a literal "&" in the output.
131 if (label
.GetChar(i
+ 1) == wxT('&'))
136 //Handle special case of "&_" (i.e. "_" is the mnemonic).
137 //FIXME - Is it possible to use "_" as a GTK mnemonic? Just use a
139 else if (label
.GetChar(i
+ 1) == wxT('_'))
144 //Replace WX mnemonic indicator "&" with GTK indicator "_".
150 else if (label
.GetChar(i
) == wxT('_'))
152 //Escape any underlines in the string so GTK doesn't use them.
157 label2
<< label
.GetChar(i
);
165 wxVisualAttributes
wxControl::GetDefaultAttributes() const
167 return GetDefaultAttributesFromGTKWidget(m_widget
,
172 #define SHIFT (8*(sizeof(short int)-sizeof(char)))
176 wxControl::GetDefaultAttributesFromGTKWidget(GtkWidget
* widget
,
181 wxVisualAttributes attr
;
183 style
= gtk_rc_get_style(widget
);
185 style
= gtk_widget_get_default_style();
189 return wxWindow::GetClassDefaultAttributes(wxWINDOW_VARIANT_NORMAL
);
193 state
= GTK_STATE_NORMAL
;
195 // get the style's colours
196 attr
.colFg
= wxColour(style
->fg
[state
].red
>> SHIFT
,
197 style
->fg
[state
].green
>> SHIFT
,
198 style
->fg
[state
].blue
>> SHIFT
);
200 attr
.colBg
= wxColour(style
->base
[state
].red
>> SHIFT
,
201 style
->base
[state
].green
>> SHIFT
,
202 style
->base
[state
].blue
>> SHIFT
);
204 attr
.colBg
= wxColour(style
->bg
[state
].red
>> SHIFT
,
205 style
->bg
[state
].green
>> SHIFT
,
206 style
->bg
[state
].blue
>> SHIFT
);
208 // get the style's font
210 if ( !style
->font_desc
)
211 style
= gtk_widget_get_default_style();
212 if ( style
&& style
->font_desc
)
214 wxNativeFontInfo info
;
215 info
.description
= style
->font_desc
;
216 attr
.font
= wxFont(info
);
220 GtkSettings
*settings
= gtk_settings_get_default();
221 gchar
*font_name
= NULL
;
222 g_object_get ( settings
,
227 attr
.font
= wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT
);
229 attr
.font
= wxFont(wxString::FromAscii(font_name
));
233 // TODO: isn't there a way to get a standard gtk 1.2 font?
234 attr
.font
= wxFont( 12, wxSWISS
, wxNORMAL
, wxNORMAL
);
243 wxControl::GetDefaultAttributesFromGTKWidget(GtkWidget
* (*widget_new
)(void),
247 wxVisualAttributes attr
;
248 GtkWidget
* widget
= widget_new();
249 attr
= GetDefaultAttributesFromGTKWidget(widget
, useBase
, state
);
250 gtk_widget_destroy(widget
);
256 wxControl::GetDefaultAttributesFromGTKWidget(GtkWidget
* (*widget_new
)(const gchar
*),
260 wxVisualAttributes attr
;
261 GtkWidget
* widget
= widget_new("");
262 attr
= GetDefaultAttributesFromGTKWidget(widget
, useBase
, state
);
263 gtk_widget_destroy(widget
);
270 wxControl::GetDefaultAttributesFromGTKWidget(GtkWidget
* (*widget_new
)(GtkAdjustment
*),
274 wxVisualAttributes attr
;
275 GtkWidget
* widget
= widget_new(NULL
);
276 attr
= GetDefaultAttributesFromGTKWidget(widget
, useBase
, state
);
277 gtk_widget_destroy(widget
);
281 #endif // wxUSE_CONTROLS