]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/control.cpp
interpret m_has..Col, m_hasFont correctly
[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;
fe161a26 36 m_createComplete = false;
6de97a3b 37}
c801d85f 38
04165bec 39bool wxControl::Create( wxWindow *parent,
31528cd3
VZ
40 wxWindowID id,
41 const wxPoint &pos,
42 const wxSize &size,
43 long style,
8d772832 44 const wxValidator& validator,
04165bec 45 const wxString &name )
8d772832 46{
fe161a26 47 m_createComplete = false;
04165bec
RR
48 bool ret = wxWindow::Create(parent, id, pos, size, style, name);
49
50#if wxUSE_VALIDATORS
8d772832 51 SetValidator(validator);
8d772832
RD
52#endif
53
04165bec
RR
54 return ret;
55}
56
c801d85f
KB
57void wxControl::SetLabel( const wxString &label )
58{
d9ea011f 59 m_label.Empty();
223d09f6 60 for ( const wxChar *pc = label; *pc != wxT('\0'); pc++ )
b292e2f5 61 {
223d09f6 62 if ( *pc == wxT('&') )
31528cd3 63 {
b292e2f5 64 pc++; // skip it
32c77a71 65#if 0 // it would be unused anyhow for now - kbd interface not done yet
223d09f6 66 if ( *pc != wxT('&') ) m_chAccel = *pc;
32c77a71 67#endif
b292e2f5
RR
68 }
69 m_label << *pc;
32c77a71 70 }
6de97a3b 71}
c801d85f 72
fe161a26
RD
73void wxControl::PostSetLabel()
74{
75 // make sure the widget has been created, and that PostCreate has already
76 // been called
77 if (m_widget && m_createComplete && GetAdjustMinSizeFlag())
78 SetBestSize(wxDefaultSize);
79}
80
9abe166a 81wxString wxControl::GetLabel() const
c801d85f 82{
b292e2f5 83 return m_label;
6de97a3b 84}
c801d85f
KB
85
86
f68586e5
VZ
87wxSize wxControl::DoGetBestSize() const
88{
0279e844
RR
89 // Do not return any arbitrary default value...
90 wxASSERT_MSG( m_widget, wxT("DoGetBestSize called before creation") );
91
f68586e5 92 GtkRequisition req;
33720b2d
RR
93 req.width = 2;
94 req.height = 2;
2afa14f2 95 (* GTK_WIDGET_CLASS( GTK_OBJECT_GET_CLASS(m_widget) )->size_request )
f68586e5
VZ
96 (m_widget, &req );
97
98 return wxSize(req.width, req.height);
99}
100
abdeb9e7
RD
101
102void wxControl::PostCreation(const wxSize& size)
103{
104 wxWindow::PostCreation();
f40fdaa3
VS
105
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);
112
abdeb9e7
RD
113 InheritAttributes();
114 ApplyWidgetStyle();
115 SetInitialBestSize(size);
fe161a26 116 m_createComplete = true;
abdeb9e7
RD
117}
118
119
393cbb8f 120#ifdef __WXGTK20__
eaafd2f8
VS
121wxString wxControl::PrepareLabelMnemonics( const wxString &label ) const
122{
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.
125 wxString label2;
126 for (size_t i = 0; i < label.Len(); i++)
127 {
128 if (label.GetChar(i) == wxT('&'))
129 {
130 //Mnemonic escape sequence "&&" is a literal "&" in the output.
131 if (label.GetChar(i + 1) == wxT('&'))
132 {
133 label2 << wxT('&');
134 i++;
135 }
136 //Handle special case of "&_" (i.e. "_" is the mnemonic).
137 //FIXME - Is it possible to use "_" as a GTK mnemonic? Just use a
138 //dash for now.
139 else if (label.GetChar(i + 1) == wxT('_'))
140 {
141 label2 << wxT("_-");
142 i++;
143 }
144 //Replace WX mnemonic indicator "&" with GTK indicator "_".
145 else
146 {
147 label2 << wxT('_');
148 }
149 }
150 else if (label.GetChar(i) == wxT('_'))
151 {
152 //Escape any underlines in the string so GTK doesn't use them.
153 label2 << wxT("__");
154 }
155 else
156 {
157 label2 << label.GetChar(i);
158 }
159 }
160 return label2;
161}
393cbb8f 162#endif
eaafd2f8 163
9d522606
RD
164
165wxVisualAttributes wxControl::GetDefaultAttributes() const
166{
167 return GetDefaultAttributesFromGTKWidget(m_widget,
168 UseGTKStyleBase());
169}
170
171
172#define SHIFT (8*(sizeof(short int)-sizeof(char)))
173
174// static
175wxVisualAttributes
176wxControl::GetDefaultAttributesFromGTKWidget(GtkWidget* widget,
177 bool useBase,
178 int state)
179{
180 GtkStyle* style;
181 wxVisualAttributes attr;
182
183 style = gtk_rc_get_style(widget);
184 if (!style)
185 style = gtk_widget_get_default_style();
186
187 if (!style)
188 {
189 return wxWindow::GetClassDefaultAttributes(wxWINDOW_VARIANT_NORMAL);
190 }
191
192 if (state == -1)
193 state = GTK_STATE_NORMAL;
194
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);
199 if (useBase)
200 attr.colBg = wxColour(style->base[state].red >> SHIFT,
201 style->base[state].green >> SHIFT,
202 style->base[state].blue >> SHIFT);
203 else
204 attr.colBg = wxColour(style->bg[state].red >> SHIFT,
205 style->bg[state].green >> SHIFT,
206 style->bg[state].blue >> SHIFT);
207
208 // get the style's font
209#ifdef __WXGTK20__
210 if ( !style->font_desc )
211 style = gtk_widget_get_default_style();
212 if ( style && style->font_desc )
213 {
214 wxNativeFontInfo info;
215 info.description = style->font_desc;
216 attr.font = wxFont(info);
217 }
218 else
219 {
220 GtkSettings *settings = gtk_settings_get_default();
221 gchar *font_name = NULL;
222 g_object_get ( settings,
223 "gtk-font-name",
224 &font_name,
225 NULL);
226 if (!font_name)
227 attr.font = wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT );
228 else
229 attr.font = wxFont(wxString::FromAscii(font_name));
230 g_free (font_name);
231 }
232#else
233 // TODO: isn't there a way to get a standard gtk 1.2 font?
234 attr.font = wxFont( 12, wxSWISS, wxNORMAL, wxNORMAL );
235#endif
236
237 return attr;
238}
239
240
241//static
242wxVisualAttributes
243wxControl::GetDefaultAttributesFromGTKWidget(GtkWidget* (*widget_new)(void),
244 bool useBase,
245 int state)
246{
247 wxVisualAttributes attr;
248 GtkWidget* widget = widget_new();
249 attr = GetDefaultAttributesFromGTKWidget(widget, useBase, state);
250 gtk_widget_destroy(widget);
251 return attr;
252}
253
254//static
255wxVisualAttributes
256wxControl::GetDefaultAttributesFromGTKWidget(GtkWidget* (*widget_new)(const gchar*),
257 bool useBase,
258 int state)
259{
260 wxVisualAttributes attr;
261 GtkWidget* widget = widget_new("");
262 attr = GetDefaultAttributesFromGTKWidget(widget, useBase, state);
263 gtk_widget_destroy(widget);
264 return attr;
265}
266
267
268//static
269wxVisualAttributes
270wxControl::GetDefaultAttributesFromGTKWidget(GtkWidget* (*widget_new)(GtkAdjustment*),
271 bool useBase,
272 int state)
273{
274 wxVisualAttributes attr;
275 GtkWidget* widget = widget_new(NULL);
276 attr = GetDefaultAttributesFromGTKWidget(widget, useBase, state);
277 gtk_widget_destroy(widget);
278 return attr;
279}
280
1e6feb95
VZ
281#endif // wxUSE_CONTROLS
282