]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/control.cpp
Applied patch [ 1331340 ] faster Drawing of Polygons and Polylines
[wxWidgets.git] / src / gtk / control.cpp
CommitLineData
c801d85f 1/////////////////////////////////////////////////////////////////////////////
634fb750
VZ
2// Name: src/gtk/control.cpp
3// Purpose: wxControl implementation for wxGTK
c801d85f 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
VS
10// For compilers that support precompilation, includes "wx.h".
11#include "wx/wxprec.h"
12
1e6feb95
VZ
13#if wxUSE_CONTROLS
14
634fb750 15#include "wx/log.h"
c801d85f 16#include "wx/control.h"
9d522606
RD
17#include "wx/fontutil.h"
18#include "wx/settings.h"
b2ff89d6 19#include "wx/gtk/private.h"
034be888 20
634fb750
VZ
21// ============================================================================
22// wxControl implementation
23// ============================================================================
24
25// ----------------------------------------------------------------------------
26// wxControl creation
27// ----------------------------------------------------------------------------
c801d85f 28
9abe166a 29IMPLEMENT_DYNAMIC_CLASS(wxControl, wxWindow)
c801d85f 30
31528cd3 31wxControl::wxControl()
c801d85f 32{
b292e2f5 33 m_needParent = TRUE;
6de97a3b 34}
c801d85f 35
04165bec 36bool wxControl::Create( wxWindow *parent,
31528cd3
VZ
37 wxWindowID id,
38 const wxPoint &pos,
39 const wxSize &size,
40 long style,
8d772832 41 const wxValidator& validator,
04165bec 42 const wxString &name )
8d772832 43{
04165bec 44 bool ret = wxWindow::Create(parent, id, pos, size, style, name);
b2ff89d6 45
04165bec 46#if wxUSE_VALIDATORS
8d772832 47 SetValidator(validator);
8d772832
RD
48#endif
49
04165bec
RR
50 return ret;
51}
52
f68586e5
VZ
53wxSize wxControl::DoGetBestSize() const
54{
0279e844
RR
55 // Do not return any arbitrary default value...
56 wxASSERT_MSG( m_widget, wxT("DoGetBestSize called before creation") );
57
f68586e5 58 GtkRequisition req;
33720b2d
RR
59 req.width = 2;
60 req.height = 2;
2afa14f2 61 (* GTK_WIDGET_CLASS( GTK_OBJECT_GET_CLASS(m_widget) )->size_request )
f68586e5
VZ
62 (m_widget, &req );
63
9f884528
RD
64 wxSize best(req.width, req.height);
65 CacheBestSize(best);
66 return best;
f68586e5
VZ
67}
68
abdeb9e7
RD
69
70void wxControl::PostCreation(const wxSize& size)
71{
72 wxWindow::PostCreation();
f40fdaa3
VS
73
74 // NB: GetBestSize needs to know the style, otherwise it will assume
75 // default font and if the user uses a different font, determined
76 // best size will be different (typically, smaller) than the desired
77 // size. This call ensure that a style is available at the time
78 // GetBestSize is called.
79 gtk_widget_ensure_style(m_widget);
b2ff89d6 80
abdeb9e7
RD
81 ApplyWidgetStyle();
82 SetInitialBestSize(size);
83}
84
b2ff89d6
VZ
85// ----------------------------------------------------------------------------
86// wxControl dealing with labels
87// ----------------------------------------------------------------------------
88
89void wxControl::SetLabel( const wxString &label )
90{
91 // keep the original string internally to be able to return it later (for
92 // consistency with the other ports)
93 m_label = label;
94
95 InvalidateBestSize();
96}
97
98wxString wxControl::GetLabel() const
99{
100 return m_label;
101}
102
103void wxControl::GTKSetLabelForLabel(GtkLabel *w, const wxString& label)
104{
105 // don't call the virtual function which might call this one back again
106 wxControl::SetLabel(label);
107
108 const wxString labelGTK = GTKConvertMnemonics(label);
abdeb9e7 109
393cbb8f 110#ifdef __WXGTK20__
b2ff89d6
VZ
111 gtk_label_set_text_with_mnemonic(w, wxGTK_CONV(labelGTK));
112#else
113 gtk_label_set(w, wxGTK_CONV(labelGTK));
114#endif
115}
116
117void wxControl::GTKSetLabelForFrame(GtkFrame *w, const wxString& label)
118{
119 wxControl::SetLabel(label);
120
121 // frames don't support mnemonics even under GTK+ 2
122 const wxString labelGTK = GTKRemoveMnemonics(label);
123
124 gtk_frame_set_label(w, labelGTK.empty() ? (char *)NULL
125 : wxGTK_CONV(labelGTK));
126}
127
128// worker function implementing both GTKConvert/RemoveMnemonics()
129//
130// notice that under GTK+ 1 we only really need to support MNEMONICS_REMOVE as
131// it doesn't support mnemonics anyhow but this would make the code so ugly
132// that we do the same thing for GKT+ 1 and 2
133enum MnemonicsFlag
eaafd2f8 134{
b2ff89d6
VZ
135 MNEMONICS_REMOVE,
136 MNEMONICS_CONVERT
137};
138
139static wxString GTKProcessMnemonics(const wxString& label, MnemonicsFlag flag)
140{
141 const size_t len = label.length();
142 wxString labelGTK;
143 labelGTK.reserve(len);
144 for ( size_t i = 0; i < len; i++ )
eaafd2f8 145 {
b2ff89d6
VZ
146 wxChar ch = label[i];
147
148 switch ( ch )
eaafd2f8 149 {
b2ff89d6
VZ
150 case wxT('&'):
151 if ( i == len - 1 )
152 {
153 // "&" at the end of string is an error
154 wxLogDebug(wxT("Invalid label \"%s\"."), label.c_str());
155 break;
156 }
157
158 ch = label[++i]; // skip '&' itself
159 switch ( ch )
160 {
161 case wxT('&'):
162 // special case: "&&" is not a mnemonic at all but just
163 // an escaped "&"
164 labelGTK += wxT('&');
165 break;
166
167 case wxT('_'):
168 if ( flag == MNEMONICS_CONVERT )
169 {
170 // '_' can't be a GTK mnemonic apparently so
171 // replace it with something similar
172 labelGTK += wxT("_-");
173 break;
174 }
175 //else: fall through
176
177 default:
178 if ( flag == MNEMONICS_CONVERT )
179 labelGTK += wxT('_');
180 labelGTK += ch;
181 }
182 break;
183
184 case wxT('_'):
185 if ( flag == MNEMONICS_CONVERT )
186 {
187 // escape any existing underlines in the string so that
188 // they don't become mnemonics accidentally
189 labelGTK += wxT("__");
190 break;
191 }
192 //else: fall through
193
194 default:
195 labelGTK += ch;
eaafd2f8
VS
196 }
197 }
b2ff89d6
VZ
198
199 return labelGTK;
200}
201
202/* static */
203wxString wxControl::GTKRemoveMnemonics(const wxString& label)
204{
205 return GTKProcessMnemonics(label, MNEMONICS_REMOVE);
206}
207
208/* static */
209wxString wxControl::GTKConvertMnemonics(const wxString& label)
210{
d411c5d6 211#ifdef __WXGTK20__
b2ff89d6 212 return GTKProcessMnemonics(label, MNEMONICS_CONVERT);
d411c5d6
VZ
213#else
214 return GTKRemoveMnemonics(label);
215#endif
eaafd2f8
VS
216}
217
b2ff89d6
VZ
218// ----------------------------------------------------------------------------
219// wxControl styles (a.k.a. attributes)
220// ----------------------------------------------------------------------------
9d522606
RD
221
222wxVisualAttributes wxControl::GetDefaultAttributes() const
223{
224 return GetDefaultAttributesFromGTKWidget(m_widget,
225 UseGTKStyleBase());
226}
227
228
229#define SHIFT (8*(sizeof(short int)-sizeof(char)))
230
231// static
232wxVisualAttributes
233wxControl::GetDefaultAttributesFromGTKWidget(GtkWidget* widget,
234 bool useBase,
235 int state)
236{
237 GtkStyle* style;
238 wxVisualAttributes attr;
239
240 style = gtk_rc_get_style(widget);
241 if (!style)
242 style = gtk_widget_get_default_style();
243
244 if (!style)
245 {
246 return wxWindow::GetClassDefaultAttributes(wxWINDOW_VARIANT_NORMAL);
247 }
248
249 if (state == -1)
250 state = GTK_STATE_NORMAL;
b2ff89d6 251
9d522606
RD
252 // get the style's colours
253 attr.colFg = wxColour(style->fg[state].red >> SHIFT,
254 style->fg[state].green >> SHIFT,
255 style->fg[state].blue >> SHIFT);
256 if (useBase)
257 attr.colBg = wxColour(style->base[state].red >> SHIFT,
258 style->base[state].green >> SHIFT,
259 style->base[state].blue >> SHIFT);
260 else
261 attr.colBg = wxColour(style->bg[state].red >> SHIFT,
262 style->bg[state].green >> SHIFT,
263 style->bg[state].blue >> SHIFT);
264
265 // get the style's font
266#ifdef __WXGTK20__
267 if ( !style->font_desc )
b2ff89d6 268 style = gtk_widget_get_default_style();
9d522606 269 if ( style && style->font_desc )
b2ff89d6
VZ
270 {
271 wxNativeFontInfo info;
fdf7514a 272 info.description = pango_font_description_copy(style->font_desc);
b2ff89d6
VZ
273 attr.font = wxFont(info);
274 }
275 else
276 {
9d522606
RD
277 GtkSettings *settings = gtk_settings_get_default();
278 gchar *font_name = NULL;
279 g_object_get ( settings,
b2ff89d6 280 "gtk-font-name",
9d522606
RD
281 &font_name,
282 NULL);
283 if (!font_name)
284 attr.font = wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT );
285 else
286 attr.font = wxFont(wxString::FromAscii(font_name));
287 g_free (font_name);
b2ff89d6 288 }
9d522606
RD
289#else
290 // TODO: isn't there a way to get a standard gtk 1.2 font?
291 attr.font = wxFont( 12, wxSWISS, wxNORMAL, wxNORMAL );
292#endif
b2ff89d6 293
9d522606
RD
294 return attr;
295}
296
297
298//static
299wxVisualAttributes
865bb325 300wxControl::GetDefaultAttributesFromGTKWidget(wxGtkWidgetNew_t widget_new,
9d522606
RD
301 bool useBase,
302 int state)
303{
304 wxVisualAttributes attr;
66d8fe77
VS
305 // NB: we need toplevel window so that GTK+ can find the right style
306 GtkWidget *wnd = gtk_window_new(GTK_WINDOW_TOPLEVEL);
9d522606 307 GtkWidget* widget = widget_new();
66d8fe77 308 gtk_container_add(GTK_CONTAINER(wnd), widget);
9d522606 309 attr = GetDefaultAttributesFromGTKWidget(widget, useBase, state);
66d8fe77 310 gtk_widget_destroy(wnd);
9d522606
RD
311 return attr;
312}
313
314//static
315wxVisualAttributes
865bb325 316wxControl::GetDefaultAttributesFromGTKWidget(wxGtkWidgetNewFromStr_t widget_new,
9d522606
RD
317 bool useBase,
318 int state)
319{
320 wxVisualAttributes attr;
66d8fe77
VS
321 // NB: we need toplevel window so that GTK+ can find the right style
322 GtkWidget *wnd = gtk_window_new(GTK_WINDOW_TOPLEVEL);
9d522606 323 GtkWidget* widget = widget_new("");
66d8fe77 324 gtk_container_add(GTK_CONTAINER(wnd), widget);
9d522606 325 attr = GetDefaultAttributesFromGTKWidget(widget, useBase, state);
66d8fe77 326 gtk_widget_destroy(wnd);
9d522606
RD
327 return attr;
328}
329
330
331//static
332wxVisualAttributes
865bb325 333wxControl::GetDefaultAttributesFromGTKWidget(wxGtkWidgetNewFromAdj_t widget_new,
9d522606
RD
334 bool useBase,
335 int state)
336{
337 wxVisualAttributes attr;
66d8fe77
VS
338 // NB: we need toplevel window so that GTK+ can find the right style
339 GtkWidget *wnd = gtk_window_new(GTK_WINDOW_TOPLEVEL);
9d522606 340 GtkWidget* widget = widget_new(NULL);
66d8fe77 341 gtk_container_add(GTK_CONTAINER(wnd), widget);
9d522606 342 attr = GetDefaultAttributesFromGTKWidget(widget, useBase, state);
66d8fe77 343 gtk_widget_destroy(wnd);
9d522606
RD
344 return attr;
345}
346
1e6feb95
VZ
347#endif // wxUSE_CONTROLS
348