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