]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/control.cpp
Don't have to explicitly set the background any more, the default will
[wxWidgets.git] / src / gtk / control.cpp
CommitLineData
c801d85f
KB
1/////////////////////////////////////////////////////////////////////////////
2// Name: control.cpp
3// Purpose:
4// Author: Robert Roebling
dbf858b5 5// Id: $Id$
01111366 6// Copyright: (c) 1998 Robert Roebling, Julian Smart and Vadim Zeitlin
65571936 7// Licence: wxWindows licence
c801d85f
KB
8/////////////////////////////////////////////////////////////////////////////
9
14f355c2 10#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
c801d85f
KB
11#pragma implementation "control.h"
12#endif
13
14f355c2
VS
14// For compilers that support precompilation, includes "wx.h".
15#include "wx/wxprec.h"
16
1e6feb95
VZ
17#include "wx/defs.h"
18
19#if wxUSE_CONTROLS
20
c801d85f 21#include "wx/control.h"
9d522606
RD
22#include "wx/fontutil.h"
23#include "wx/settings.h"
c801d85f 24
071a2d78 25#include <gtk/gtk.h>
034be888 26
c801d85f
KB
27//-----------------------------------------------------------------------------
28// wxControl
29//-----------------------------------------------------------------------------
30
9abe166a 31IMPLEMENT_DYNAMIC_CLASS(wxControl, wxWindow)
c801d85f 32
31528cd3 33wxControl::wxControl()
c801d85f 34{
b292e2f5 35 m_needParent = TRUE;
6de97a3b 36}
c801d85f 37
04165bec 38bool wxControl::Create( wxWindow *parent,
31528cd3
VZ
39 wxWindowID id,
40 const wxPoint &pos,
41 const wxSize &size,
42 long style,
8d772832 43 const wxValidator& validator,
04165bec 44 const wxString &name )
8d772832 45{
04165bec
RR
46 bool ret = wxWindow::Create(parent, id, pos, size, style, name);
47
48#if wxUSE_VALIDATORS
8d772832 49 SetValidator(validator);
8d772832
RD
50#endif
51
04165bec
RR
52 return ret;
53}
54
c801d85f
KB
55void wxControl::SetLabel( const wxString &label )
56{
d9ea011f 57 m_label.Empty();
223d09f6 58 for ( const wxChar *pc = label; *pc != wxT('\0'); pc++ )
b292e2f5 59 {
223d09f6 60 if ( *pc == wxT('&') )
31528cd3 61 {
b292e2f5 62 pc++; // skip it
32c77a71 63#if 0 // it would be unused anyhow for now - kbd interface not done yet
223d09f6 64 if ( *pc != wxT('&') ) m_chAccel = *pc;
32c77a71 65#endif
b292e2f5
RR
66 }
67 m_label << *pc;
32c77a71 68 }
6de97a3b 69}
c801d85f 70
9abe166a 71wxString wxControl::GetLabel() const
c801d85f 72{
b292e2f5 73 return m_label;
6de97a3b 74}
c801d85f
KB
75
76
f68586e5
VZ
77wxSize wxControl::DoGetBestSize() const
78{
0279e844
RR
79 // Do not return any arbitrary default value...
80 wxASSERT_MSG( m_widget, wxT("DoGetBestSize called before creation") );
81
f68586e5 82 GtkRequisition req;
33720b2d
RR
83 req.width = 2;
84 req.height = 2;
2afa14f2 85 (* GTK_WIDGET_CLASS( GTK_OBJECT_GET_CLASS(m_widget) )->size_request )
f68586e5
VZ
86 (m_widget, &req );
87
88 return wxSize(req.width, req.height);
89}
90
abdeb9e7
RD
91
92void wxControl::PostCreation(const wxSize& size)
93{
94 wxWindow::PostCreation();
f40fdaa3
VS
95
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);
102
abdeb9e7
RD
103 InheritAttributes();
104 ApplyWidgetStyle();
105 SetInitialBestSize(size);
106}
107
108
393cbb8f 109#ifdef __WXGTK20__
eaafd2f8
VS
110wxString wxControl::PrepareLabelMnemonics( const wxString &label ) const
111{
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.
114 wxString label2;
115 for (size_t i = 0; i < label.Len(); i++)
116 {
117 if (label.GetChar(i) == wxT('&'))
118 {
119 //Mnemonic escape sequence "&&" is a literal "&" in the output.
120 if (label.GetChar(i + 1) == wxT('&'))
121 {
122 label2 << wxT('&');
123 i++;
124 }
125 //Handle special case of "&_" (i.e. "_" is the mnemonic).
126 //FIXME - Is it possible to use "_" as a GTK mnemonic? Just use a
127 //dash for now.
128 else if (label.GetChar(i + 1) == wxT('_'))
129 {
130 label2 << wxT("_-");
131 i++;
132 }
133 //Replace WX mnemonic indicator "&" with GTK indicator "_".
134 else
135 {
136 label2 << wxT('_');
137 }
138 }
139 else if (label.GetChar(i) == wxT('_'))
140 {
141 //Escape any underlines in the string so GTK doesn't use them.
142 label2 << wxT("__");
143 }
144 else
145 {
146 label2 << label.GetChar(i);
147 }
148 }
149 return label2;
150}
393cbb8f 151#endif
eaafd2f8 152
9d522606
RD
153
154wxVisualAttributes wxControl::GetDefaultAttributes() const
155{
156 return GetDefaultAttributesFromGTKWidget(m_widget,
157 UseGTKStyleBase());
158}
159
160
161#define SHIFT (8*(sizeof(short int)-sizeof(char)))
162
163// static
164wxVisualAttributes
165wxControl::GetDefaultAttributesFromGTKWidget(GtkWidget* widget,
166 bool useBase,
167 int state)
168{
169 GtkStyle* style;
170 wxVisualAttributes attr;
171
172 style = gtk_rc_get_style(widget);
173 if (!style)
174 style = gtk_widget_get_default_style();
175
176 if (!style)
177 {
178 return wxWindow::GetClassDefaultAttributes(wxWINDOW_VARIANT_NORMAL);
179 }
180
181 if (state == -1)
182 state = GTK_STATE_NORMAL;
183
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);
188 if (useBase)
189 attr.colBg = wxColour(style->base[state].red >> SHIFT,
190 style->base[state].green >> SHIFT,
191 style->base[state].blue >> SHIFT);
192 else
193 attr.colBg = wxColour(style->bg[state].red >> SHIFT,
194 style->bg[state].green >> SHIFT,
195 style->bg[state].blue >> SHIFT);
196
197 // get the style's font
198#ifdef __WXGTK20__
199 if ( !style->font_desc )
200 style = gtk_widget_get_default_style();
201 if ( style && style->font_desc )
202 {
203 wxNativeFontInfo info;
204 info.description = style->font_desc;
205 attr.font = wxFont(info);
206 }
207 else
208 {
209 GtkSettings *settings = gtk_settings_get_default();
210 gchar *font_name = NULL;
211 g_object_get ( settings,
212 "gtk-font-name",
213 &font_name,
214 NULL);
215 if (!font_name)
216 attr.font = wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT );
217 else
218 attr.font = wxFont(wxString::FromAscii(font_name));
219 g_free (font_name);
220 }
221#else
222 // TODO: isn't there a way to get a standard gtk 1.2 font?
223 attr.font = wxFont( 12, wxSWISS, wxNORMAL, wxNORMAL );
224#endif
225
226 return attr;
227}
228
229
230//static
231wxVisualAttributes
232wxControl::GetDefaultAttributesFromGTKWidget(GtkWidget* (*widget_new)(void),
233 bool useBase,
234 int state)
235{
236 wxVisualAttributes attr;
237 GtkWidget* widget = widget_new();
238 attr = GetDefaultAttributesFromGTKWidget(widget, useBase, state);
239 gtk_widget_destroy(widget);
240 return attr;
241}
242
243//static
244wxVisualAttributes
245wxControl::GetDefaultAttributesFromGTKWidget(GtkWidget* (*widget_new)(const gchar*),
246 bool useBase,
247 int state)
248{
249 wxVisualAttributes attr;
250 GtkWidget* widget = widget_new("");
251 attr = GetDefaultAttributesFromGTKWidget(widget, useBase, state);
252 gtk_widget_destroy(widget);
253 return attr;
254}
255
256
257//static
258wxVisualAttributes
259wxControl::GetDefaultAttributesFromGTKWidget(GtkWidget* (*widget_new)(GtkAdjustment*),
260 bool useBase,
261 int state)
262{
263 wxVisualAttributes attr;
264 GtkWidget* widget = widget_new(NULL);
265 attr = GetDefaultAttributesFromGTKWidget(widget, useBase, state);
266 gtk_widget_destroy(widget);
267 return attr;
268}
269
f40fdaa3
VS
270void wxControl::ApplyWidgetStyle()
271{
272 GtkRcStyle *style = CreateWidgetStyle();
273 if ( style )
274 {
275 DoApplyWidgetStyle(style);
276 gtk_rc_style_unref(style);
277 }
278}
279
280void wxControl::DoApplyWidgetStyle(GtkRcStyle *style)
281{
282 gtk_widget_modify_style(m_widget, style);
283}
284
9d522606 285
1e6feb95
VZ
286#endif // wxUSE_CONTROLS
287