]> git.saurik.com Git - wxWidgets.git/blob - src/gtk1/control.cpp
Fix after removal of PRIOR/NEXT in common headers.
[wxWidgets.git] / src / gtk1 / control.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk1/control.cpp
3 // Purpose: wxControl implementation for wxGTK
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 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
12
13 #if wxUSE_CONTROLS
14
15 #include "wx/log.h"
16 #include "wx/control.h"
17 #include "wx/fontutil.h"
18 #include "wx/settings.h"
19 #include "wx/gtk1/private.h"
20
21 // ============================================================================
22 // wxControl implementation
23 // ============================================================================
24
25 // ----------------------------------------------------------------------------
26 // wxControl creation
27 // ----------------------------------------------------------------------------
28
29 IMPLEMENT_DYNAMIC_CLASS(wxControl, wxWindow)
30
31 wxControl::wxControl()
32 {
33 m_needParent = TRUE;
34 }
35
36 bool wxControl::Create( wxWindow *parent,
37 wxWindowID id,
38 const wxPoint &pos,
39 const wxSize &size,
40 long style,
41 const wxValidator& validator,
42 const wxString &name )
43 {
44 bool ret = wxWindow::Create(parent, id, pos, size, style, name);
45
46 #if wxUSE_VALIDATORS
47 SetValidator(validator);
48 #endif
49
50 return ret;
51 }
52
53 wxSize wxControl::DoGetBestSize() const
54 {
55 // Do not return any arbitrary default value...
56 wxASSERT_MSG( m_widget, wxT("DoGetBestSize called before creation") );
57
58 GtkRequisition req;
59 req.width = 2;
60 req.height = 2;
61 (* GTK_WIDGET_CLASS( GTK_OBJECT_GET_CLASS(m_widget) )->size_request )
62 (m_widget, &req );
63
64 wxSize best(req.width, req.height);
65 CacheBestSize(best);
66 return best;
67 }
68
69
70 void wxControl::PostCreation(const wxSize& size)
71 {
72 wxWindow::PostCreation();
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);
80
81 ApplyWidgetStyle();
82 SetInitialBestSize(size);
83 }
84
85 // ----------------------------------------------------------------------------
86 // wxControl dealing with labels
87 // ----------------------------------------------------------------------------
88
89 void 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
98 wxString wxControl::GetLabel() const
99 {
100 return m_label;
101 }
102
103 void 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 = GTKRemoveMnemonics(label);
109
110 gtk_label_set(w, wxGTK_CONV(labelGTK));
111 }
112
113 void wxControl::GTKSetLabelForFrame(GtkFrame *w, const wxString& label)
114 {
115 wxControl::SetLabel(label);
116
117 const wxString labelGTK = GTKRemoveMnemonics(label);
118
119 gtk_frame_set_label(w, labelGTK.empty() ? (char *)NULL
120 : wxGTK_CONV(labelGTK));
121 }
122
123 /* static */
124 wxString wxControl::GTKRemoveMnemonics(const wxString& label)
125 {
126 const size_t len = label.length();
127 wxString labelGTK;
128 labelGTK.reserve(len);
129 for ( size_t i = 0; i < len; i++ )
130 {
131 wxChar ch = label[i];
132
133 if ( ch == _T('&') )
134 {
135 if ( i == len - 1 )
136 {
137 // "&" at the end of string is an error
138 wxLogDebug(wxT("Invalid label \"%s\"."), label.c_str());
139 break;
140 }
141
142 ch = label[++i]; // skip '&' itself
143 if ( ch == _T('&') )
144 {
145 // special case: "&&" is not a mnemonic at all but just an
146 // escaped "&"
147 labelGTK += wxT('&');
148 continue;
149 }
150 }
151
152 labelGTK += ch;
153 }
154
155 return labelGTK;
156 }
157
158 // ----------------------------------------------------------------------------
159 // wxControl styles (a.k.a. attributes)
160 // ----------------------------------------------------------------------------
161
162 wxVisualAttributes wxControl::GetDefaultAttributes() const
163 {
164 return GetDefaultAttributesFromGTKWidget(m_widget,
165 UseGTKStyleBase());
166 }
167
168
169 #define SHIFT (8*(sizeof(short int)-sizeof(char)))
170
171 // static
172 wxVisualAttributes
173 wxControl::GetDefaultAttributesFromGTKWidget(GtkWidget* widget,
174 bool useBase,
175 int state)
176 {
177 GtkStyle* style;
178 wxVisualAttributes attr;
179
180 style = gtk_rc_get_style(widget);
181 if (!style)
182 style = gtk_widget_get_default_style();
183
184 if (!style)
185 {
186 return wxWindow::GetClassDefaultAttributes(wxWINDOW_VARIANT_NORMAL);
187 }
188
189 if (state == -1)
190 state = GTK_STATE_NORMAL;
191
192 // get the style's colours
193 attr.colFg = wxColour(style->fg[state].red >> SHIFT,
194 style->fg[state].green >> SHIFT,
195 style->fg[state].blue >> SHIFT);
196 if (useBase)
197 attr.colBg = wxColour(style->base[state].red >> SHIFT,
198 style->base[state].green >> SHIFT,
199 style->base[state].blue >> SHIFT);
200 else
201 attr.colBg = wxColour(style->bg[state].red >> SHIFT,
202 style->bg[state].green >> SHIFT,
203 style->bg[state].blue >> SHIFT);
204
205 // get the style's font
206 // TODO: isn't there a way to get a standard gtk 1.2 font?
207 attr.font = wxFont( 12, wxSWISS, wxNORMAL, wxNORMAL );
208
209 return attr;
210 }
211
212
213 //static
214 wxVisualAttributes
215 wxControl::GetDefaultAttributesFromGTKWidget(wxGtkWidgetNew_t widget_new,
216 bool useBase,
217 int state)
218 {
219 wxVisualAttributes attr;
220 // NB: we need toplevel window so that GTK+ can find the right style
221 GtkWidget *wnd = gtk_window_new(GTK_WINDOW_TOPLEVEL);
222 GtkWidget* widget = widget_new();
223 gtk_container_add(GTK_CONTAINER(wnd), widget);
224 attr = GetDefaultAttributesFromGTKWidget(widget, useBase, state);
225 gtk_widget_destroy(wnd);
226 return attr;
227 }
228
229 //static
230 wxVisualAttributes
231 wxControl::GetDefaultAttributesFromGTKWidget(wxGtkWidgetNewFromStr_t widget_new,
232 bool useBase,
233 int state)
234 {
235 wxVisualAttributes attr;
236 // NB: we need toplevel window so that GTK+ can find the right style
237 GtkWidget *wnd = gtk_window_new(GTK_WINDOW_TOPLEVEL);
238 GtkWidget* widget = widget_new("");
239 gtk_container_add(GTK_CONTAINER(wnd), widget);
240 attr = GetDefaultAttributesFromGTKWidget(widget, useBase, state);
241 gtk_widget_destroy(wnd);
242 return attr;
243 }
244
245
246 //static
247 wxVisualAttributes
248 wxControl::GetDefaultAttributesFromGTKWidget(wxGtkWidgetNewFromAdj_t widget_new,
249 bool useBase,
250 int state)
251 {
252 wxVisualAttributes attr;
253 // NB: we need toplevel window so that GTK+ can find the right style
254 GtkWidget *wnd = gtk_window_new(GTK_WINDOW_TOPLEVEL);
255 GtkWidget* widget = widget_new(NULL);
256 gtk_container_add(GTK_CONTAINER(wnd), widget);
257 attr = GetDefaultAttributesFromGTKWidget(widget, useBase, state);
258 gtk_widget_destroy(wnd);
259 return attr;
260 }
261
262 #endif // wxUSE_CONTROLS
263