]>
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
;
72 wxString
wxControl::GetLabel() const
78 wxSize
wxControl::DoGetBestSize() const
80 // Do not return any arbitrary default value...
81 wxASSERT_MSG( m_widget
, wxT("DoGetBestSize called before creation") );
86 (* GTK_WIDGET_CLASS( GTK_OBJECT_GET_CLASS(m_widget
) )->size_request
)
89 wxSize
best(req
.width
, req
.height
);
95 void wxControl::PostCreation(const wxSize
& size
)
97 wxWindow::PostCreation();
99 // NB: GetBestSize needs to know the style, otherwise it will assume
100 // default font and if the user uses a different font, determined
101 // best size will be different (typically, smaller) than the desired
102 // size. This call ensure that a style is available at the time
103 // GetBestSize is called.
104 gtk_widget_ensure_style(m_widget
);
107 SetInitialBestSize(size
);
112 wxString
wxControl::PrepareLabelMnemonics( const wxString
&label
) const
114 //Format mnemonics properly for GTK2. This can be called from GTK1.x, but
115 //it's not very useful because mnemonics don't exist prior to GTK2.
117 for (size_t i
= 0; i
< label
.Len(); i
++)
119 if (label
.GetChar(i
) == wxT('&'))
121 //Mnemonic escape sequence "&&" is a literal "&" in the output.
122 if (label
.GetChar(i
+ 1) == wxT('&'))
127 //Handle special case of "&_" (i.e. "_" is the mnemonic).
128 //FIXME - Is it possible to use "_" as a GTK mnemonic? Just use a
130 else if (label
.GetChar(i
+ 1) == wxT('_'))
135 //Replace WX mnemonic indicator "&" with GTK indicator "_".
141 else if (label
.GetChar(i
) == wxT('_'))
143 //Escape any underlines in the string so GTK doesn't use them.
148 label2
<< label
.GetChar(i
);
156 wxVisualAttributes
wxControl::GetDefaultAttributes() const
158 return GetDefaultAttributesFromGTKWidget(m_widget
,
163 #define SHIFT (8*(sizeof(short int)-sizeof(char)))
167 wxControl::GetDefaultAttributesFromGTKWidget(GtkWidget
* widget
,
172 wxVisualAttributes attr
;
174 style
= gtk_rc_get_style(widget
);
176 style
= gtk_widget_get_default_style();
180 return wxWindow::GetClassDefaultAttributes(wxWINDOW_VARIANT_NORMAL
);
184 state
= GTK_STATE_NORMAL
;
186 // get the style's colours
187 attr
.colFg
= wxColour(style
->fg
[state
].red
>> SHIFT
,
188 style
->fg
[state
].green
>> SHIFT
,
189 style
->fg
[state
].blue
>> SHIFT
);
191 attr
.colBg
= wxColour(style
->base
[state
].red
>> SHIFT
,
192 style
->base
[state
].green
>> SHIFT
,
193 style
->base
[state
].blue
>> SHIFT
);
195 attr
.colBg
= wxColour(style
->bg
[state
].red
>> SHIFT
,
196 style
->bg
[state
].green
>> SHIFT
,
197 style
->bg
[state
].blue
>> SHIFT
);
199 // get the style's font
201 if ( !style
->font_desc
)
202 style
= gtk_widget_get_default_style();
203 if ( style
&& style
->font_desc
)
205 wxNativeFontInfo info
;
206 info
.description
= pango_font_description_copy(style
->font_desc
);
207 attr
.font
= wxFont(info
);
211 GtkSettings
*settings
= gtk_settings_get_default();
212 gchar
*font_name
= NULL
;
213 g_object_get ( settings
,
218 attr
.font
= wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT
);
220 attr
.font
= wxFont(wxString::FromAscii(font_name
));
224 // TODO: isn't there a way to get a standard gtk 1.2 font?
225 attr
.font
= wxFont( 12, wxSWISS
, wxNORMAL
, wxNORMAL
);
234 wxControl::GetDefaultAttributesFromGTKWidget(GtkWidget
* (*widget_new
)(void),
238 wxVisualAttributes attr
;
239 // NB: we need toplevel window so that GTK+ can find the right style
240 GtkWidget
*wnd
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
241 GtkWidget
* widget
= widget_new();
242 gtk_container_add(GTK_CONTAINER(wnd
), widget
);
243 attr
= GetDefaultAttributesFromGTKWidget(widget
, useBase
, state
);
244 gtk_widget_destroy(wnd
);
250 wxControl::GetDefaultAttributesFromGTKWidget(GtkWidget
* (*widget_new
)(const gchar
*),
254 wxVisualAttributes attr
;
255 // NB: we need toplevel window so that GTK+ can find the right style
256 GtkWidget
*wnd
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
257 GtkWidget
* widget
= widget_new("");
258 gtk_container_add(GTK_CONTAINER(wnd
), widget
);
259 attr
= GetDefaultAttributesFromGTKWidget(widget
, useBase
, state
);
260 gtk_widget_destroy(wnd
);
267 wxControl::GetDefaultAttributesFromGTKWidget(GtkWidget
* (*widget_new
)(GtkAdjustment
*),
271 wxVisualAttributes attr
;
272 // NB: we need toplevel window so that GTK+ can find the right style
273 GtkWidget
*wnd
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
274 GtkWidget
* widget
= widget_new(NULL
);
275 gtk_container_add(GTK_CONTAINER(wnd
), widget
);
276 attr
= GetDefaultAttributesFromGTKWidget(widget
, useBase
, state
);
277 gtk_widget_destroy(wnd
);
281 #endif // wxUSE_CONTROLS