]>
Commit | Line | Data |
---|---|---|
ffecfa5a | 1 | ///////////////////////////////////////////////////////////////////////////// |
e2731512 | 2 | // Name: src/palmos/control.cpp |
ffecfa5a | 3 | // Purpose: wxControl class |
e2731512 | 4 | // Author: William Osborne - minimal working wxPalmOS port |
bdb54365 | 5 | // Modified by: Wlodzimierz ABX Skiba - native implementation |
ffecfa5a | 6 | // Created: 10/13/04 |
e2731512 | 7 | // RCS-ID: $Id$ |
bdb54365 | 8 | // Copyright: (c) William Osborne, Wlodzimierz Skiba |
ffecfa5a JS |
9 | // Licence: wxWindows licence |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | // ============================================================================ | |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
20 | #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) | |
21 | #pragma implementation "control.h" | |
22 | #endif | |
23 | ||
24 | // For compilers that support precompilation, includes "wx.h". | |
25 | #include "wx/wxprec.h" | |
26 | ||
27 | #ifdef __BORLANDC__ | |
28 | #pragma hdrstop | |
29 | #endif | |
30 | ||
31 | #if wxUSE_CONTROLS | |
32 | ||
33 | #ifndef WX_PRECOMP | |
34 | #include "wx/event.h" | |
35 | #include "wx/app.h" | |
36 | #include "wx/dcclient.h" | |
37 | #include "wx/log.h" | |
38 | #include "wx/settings.h" | |
39 | #endif | |
40 | ||
41 | #include "wx/control.h" | |
bdb54365 WS |
42 | #include "wx/toplevel.h" |
43 | #include "wx/button.h" | |
44 | #include "wx/checkbox.h" | |
45 | #include "wx/tglbtn.h" | |
808e3bce | 46 | #include "wx/radiobut.h" |
a152561c | 47 | #include "wx/slider.h" |
ffecfa5a JS |
48 | |
49 | // ---------------------------------------------------------------------------- | |
50 | // wxWin macros | |
51 | // ---------------------------------------------------------------------------- | |
52 | ||
53 | IMPLEMENT_ABSTRACT_CLASS(wxControl, wxWindow) | |
54 | ||
55 | BEGIN_EVENT_TABLE(wxControl, wxWindow) | |
56 | EVT_ERASE_BACKGROUND(wxControl::OnEraseBackground) | |
57 | END_EVENT_TABLE() | |
58 | ||
59 | // ============================================================================ | |
60 | // wxControl implementation | |
61 | // ============================================================================ | |
62 | ||
63 | // ---------------------------------------------------------------------------- | |
64 | // wxControl ctor/dtor | |
65 | // ---------------------------------------------------------------------------- | |
66 | ||
a152561c WS |
67 | void wxControl::Init() |
68 | { | |
69 | m_palmControl = false; | |
70 | m_palmField = false; | |
71 | } | |
72 | ||
ffecfa5a JS |
73 | wxControl::~wxControl() |
74 | { | |
9a727a3b | 75 | SetLabel(wxEmptyString); |
db101bd3 | 76 | m_isBeingDeleted = true; |
5b72333d WS |
77 | |
78 | DestroyChildren(); | |
79 | ||
80 | uint16_t index; | |
81 | FormType* form = GetObjectFormIndex(index); | |
82 | if(form!=NULL && index!=frmInvalidObjectId) | |
83 | { | |
84 | FrmRemoveObject((FormType **)&form,index); | |
85 | } | |
ffecfa5a JS |
86 | } |
87 | ||
88 | // ---------------------------------------------------------------------------- | |
89 | // control window creation | |
90 | // ---------------------------------------------------------------------------- | |
91 | ||
92 | bool wxControl::Create(wxWindow *parent, | |
93 | wxWindowID id, | |
94 | const wxPoint& pos, | |
95 | const wxSize& size, | |
96 | long style, | |
97 | const wxValidator& wxVALIDATOR_PARAM(validator), | |
98 | const wxString& name) | |
99 | { | |
100 | if ( !wxWindow::Create(parent, id, pos, size, style, name) ) | |
db101bd3 | 101 | return false; |
ffecfa5a JS |
102 | |
103 | #if wxUSE_VALIDATORS | |
104 | SetValidator(validator); | |
105 | #endif | |
106 | ||
db101bd3 | 107 | return true; |
ffecfa5a JS |
108 | } |
109 | ||
db101bd3 | 110 | bool wxControl::PalmCreateControl(ControlStyleType style, |
db101bd3 WS |
111 | const wxString& label, |
112 | const wxPoint& pos, | |
a152561c | 113 | const wxSize& size, |
8be10866 | 114 | uint8_t groupID) |
ffecfa5a | 115 | { |
ba889513 WS |
116 | FormType* form = GetParentForm(); |
117 | if(form==NULL) | |
bdb54365 | 118 | return false; |
bdb54365 | 119 | |
be4e4e27 WS |
120 | |
121 | wxCoord x = pos.x == wxDefaultCoord ? 0 : pos.x, | |
122 | y = pos.y == wxDefaultCoord ? 0 : pos.y, | |
123 | w = size.x == wxDefaultCoord ? 1 : size.x, | |
124 | h = size.y == wxDefaultCoord ? 1 : size.y; | |
125 | ||
126 | AdjustForParentClientOrigin(x, y); | |
127 | ||
a152561c WS |
128 | ControlType *control = CtlNewControl( |
129 | (void **)&form, | |
130 | GetId(), | |
131 | style, | |
9a727a3b | 132 | wxEmptyString, |
be4e4e27 WS |
133 | x, |
134 | y, | |
135 | w, | |
136 | h, | |
a152561c WS |
137 | stdFont, |
138 | groupID, | |
5b72333d | 139 | true |
a152561c WS |
140 | ); |
141 | ||
142 | if(control==NULL) | |
db101bd3 WS |
143 | return false; |
144 | ||
a152561c WS |
145 | m_palmControl = true; |
146 | ||
be4e4e27 | 147 | SetInitialBestSize(size); |
9a727a3b | 148 | SetLabel(label); |
db101bd3 WS |
149 | Show(); |
150 | return true; | |
ffecfa5a JS |
151 | } |
152 | ||
a152561c WS |
153 | bool wxControl::PalmCreateField(const wxString& label, |
154 | const wxPoint& pos, | |
155 | const wxSize& size, | |
156 | bool editable, | |
157 | bool underlined, | |
158 | JustificationType justification) | |
159 | { | |
160 | FormType* form = GetParentForm(); | |
161 | if(form==NULL) | |
162 | return false; | |
163 | ||
164 | m_label = label; | |
165 | ||
be4e4e27 WS |
166 | wxCoord x = pos.x == wxDefaultCoord ? 0 : pos.x, |
167 | y = pos.y == wxDefaultCoord ? 0 : pos.y, | |
168 | w = size.x == wxDefaultCoord ? 1 : size.x, | |
169 | h = size.y == wxDefaultCoord ? 1 : size.y; | |
170 | ||
171 | AdjustForParentClientOrigin(x, y); | |
172 | ||
a152561c WS |
173 | FieldType *field = FldNewField( |
174 | (void **)&form, | |
175 | GetId(), | |
be4e4e27 WS |
176 | x, |
177 | y, | |
178 | w, | |
179 | h, | |
a152561c WS |
180 | stdFont, |
181 | 10, | |
182 | editable, | |
183 | underlined, | |
184 | false, | |
185 | false, | |
186 | justification, | |
187 | false, | |
188 | false, | |
189 | false | |
190 | ); | |
191 | ||
192 | if(field==NULL) | |
193 | return false; | |
194 | ||
195 | m_palmField = true; | |
196 | ||
be4e4e27 | 197 | SetInitialBestSize(size); |
a152561c | 198 | SetLabel(label); |
be4e4e27 | 199 | Show(); |
a152561c WS |
200 | return true; |
201 | } | |
202 | ||
ffecfa5a JS |
203 | // ---------------------------------------------------------------------------- |
204 | // various accessors | |
205 | // ---------------------------------------------------------------------------- | |
206 | ||
ba889513 WS |
207 | FormType* wxControl::GetParentForm() const |
208 | { | |
209 | wxWindow* parentTLW = GetParent(); | |
210 | while ( parentTLW && !parentTLW->IsTopLevel() ) | |
211 | { | |
212 | parentTLW = parentTLW->GetParent(); | |
213 | } | |
214 | wxTopLevelWindowPalm* tlw = wxDynamicCast(parentTLW, wxTopLevelWindowPalm); | |
215 | if(!tlw) | |
216 | return NULL; | |
217 | return tlw->GetForm(); | |
218 | } | |
219 | ||
5b72333d | 220 | FormType* wxControl::GetObjectFormIndex(uint16_t& index) const |
a152561c WS |
221 | { |
222 | FormType* form = GetParentForm(); | |
5b72333d WS |
223 | if(form!=NULL) |
224 | index = FrmGetObjectIndex(form, GetId()); | |
225 | else | |
226 | index = frmInvalidObjectId; | |
227 | return form; | |
a152561c WS |
228 | } |
229 | ||
230 | void* wxControl::GetObjectPtr() const | |
231 | { | |
5b72333d WS |
232 | uint16_t index; |
233 | FormType* form = GetObjectFormIndex(index); | |
234 | if(form==NULL || index==frmInvalidObjectId)return NULL; | |
a152561c WS |
235 | return FrmGetObjectPtr(form,index); |
236 | } | |
237 | ||
ffecfa5a JS |
238 | wxBorder wxControl::GetDefaultBorder() const |
239 | { | |
240 | // we want to automatically give controls a sunken style (confusingly, | |
241 | // it may not really mean sunken at all as we map it to WS_EX_CLIENTEDGE | |
242 | // which is not sunken at all under Windows XP -- rather, just the default) | |
243 | return wxBORDER_SUNKEN; | |
244 | } | |
245 | ||
ba889513 WS |
246 | void wxControl::SetIntValue(int val) |
247 | { | |
248 | FormType* form = GetParentForm(); | |
249 | if(form==NULL) | |
250 | return; | |
251 | uint16_t index = FrmGetObjectIndex(form, GetId()); | |
252 | if(index==frmInvalidObjectId) | |
253 | return; | |
254 | FrmSetControlValue(form, index, val); | |
255 | } | |
256 | ||
257 | void wxControl::SetBoolValue(bool val) | |
258 | { | |
259 | SetIntValue(val?1:0); | |
260 | } | |
261 | ||
262 | bool wxControl::GetBoolValue() const | |
263 | { | |
264 | FormType* form = GetParentForm(); | |
265 | if(form==NULL) | |
266 | return false; | |
267 | uint16_t index = FrmGetObjectIndex(form, GetId()); | |
268 | if(index==frmInvalidObjectId) | |
269 | return false; | |
270 | return ( FrmGetControlValue(form, index) == 1 ); | |
271 | } | |
272 | ||
db101bd3 | 273 | wxSize wxControl::DoGetBestSize() const |
ffecfa5a | 274 | { |
db101bd3 | 275 | return wxSize(16, 16); |
ffecfa5a JS |
276 | } |
277 | ||
808e3bce WS |
278 | void wxControl::DoGetBounds( RectangleType &rect ) const |
279 | { | |
280 | FormType* form = GetParentForm(); | |
281 | if(form==NULL) | |
282 | return; | |
283 | uint16_t index = FrmGetObjectIndex(form,GetId()); | |
284 | if(index==frmInvalidObjectId) | |
285 | return; | |
286 | FrmGetObjectBounds(form,index,&rect); | |
287 | } | |
288 | ||
289 | void wxControl::DoGetPosition( int *x, int *y ) const | |
290 | { | |
291 | RectangleType rect; | |
292 | DoGetBounds(rect); | |
293 | if(x) | |
294 | *x = rect.topLeft.x; | |
295 | if(y) | |
296 | *y = rect.topLeft.y; | |
297 | } | |
298 | ||
299 | void wxControl::DoGetSize( int *width, int *height ) const | |
300 | { | |
301 | RectangleType rect; | |
302 | DoGetBounds(rect); | |
303 | if(width) | |
304 | *width = rect.extent.x; | |
305 | if(height) | |
306 | *height = rect.extent.y; | |
307 | } | |
308 | ||
db101bd3 | 309 | bool wxControl::Enable(bool enable) |
ffecfa5a | 310 | { |
a152561c WS |
311 | ControlType *control = (ControlType *)GetObjectPtr(); |
312 | if( (IsPalmControl()) || (control == NULL)) | |
db101bd3 | 313 | return false; |
a152561c | 314 | if( CtlEnabled(control) == enable) |
db101bd3 | 315 | return false; |
a152561c | 316 | CtlSetEnabled( control, enable); |
db101bd3 WS |
317 | return true; |
318 | } | |
319 | ||
320 | bool wxControl::IsEnabled() const | |
321 | { | |
a152561c WS |
322 | ControlType *control = (ControlType *)GetObjectPtr(); |
323 | if( (IsPalmControl()) || (control == NULL)) | |
db101bd3 | 324 | return false; |
a152561c | 325 | return CtlEnabled(control); |
db101bd3 WS |
326 | } |
327 | ||
328 | bool wxControl::IsShown() const | |
329 | { | |
330 | return StatGetAttribute ( statAttrBarVisible , NULL ); | |
331 | } | |
332 | ||
333 | bool wxControl::Show( bool show ) | |
334 | { | |
ba889513 WS |
335 | FormType* form = GetParentForm(); |
336 | if(form==NULL) | |
337 | return false; | |
338 | uint16_t index = FrmGetObjectIndex(form,GetId()); | |
339 | if(index==frmInvalidObjectId) | |
340 | return false; | |
db101bd3 | 341 | if(show) |
ba889513 | 342 | FrmShowObject(form,index); |
db101bd3 | 343 | else |
ba889513 | 344 | FrmHideObject(form,index); |
db101bd3 | 345 | return true; |
ffecfa5a JS |
346 | } |
347 | ||
a152561c | 348 | void wxControl::SetFieldLabel(const wxString& label) |
bdb54365 | 349 | { |
a152561c WS |
350 | FieldType* field = (FieldType*)GetObjectPtr(); |
351 | if(field==NULL) | |
352 | return; | |
353 | ||
354 | uint16_t newSize = label.Length() + 1; | |
355 | MemHandle strHandle = FldGetTextHandle(field); | |
356 | FldSetTextHandle(field, NULL ); | |
357 | if (strHandle) | |
bdb54365 | 358 | { |
a152561c WS |
359 | if(MemHandleResize(strHandle, newSize)!=errNone) |
360 | strHandle = 0; | |
bdb54365 | 361 | } |
a152561c WS |
362 | else |
363 | { | |
364 | strHandle = MemHandleNew( newSize ); | |
365 | } | |
366 | if(!strHandle) | |
367 | return; | |
368 | ||
369 | char* str = (char*) MemHandleLock( strHandle ); | |
370 | if(str==NULL) | |
371 | return; | |
372 | ||
373 | strcpy(str, label.c_str()); | |
374 | MemHandleUnlock(strHandle); | |
375 | FldSetTextHandle(field, strHandle); | |
376 | FldRecalculateField(field, true); | |
377 | } | |
378 | ||
379 | void wxControl::SetControlLabel(const wxString& label) | |
380 | { | |
9a727a3b WS |
381 | ControlType* control = (ControlType*)GetObjectPtr(); |
382 | if(control==NULL) | |
383 | return; | |
384 | CtlSetLabel(control,wxEmptyString); | |
385 | m_label = label; | |
386 | if(!m_label.empty()) | |
387 | CtlSetLabel(control,m_label.c_str()); | |
a152561c WS |
388 | } |
389 | ||
390 | void wxControl::SetLabel(const wxString& label) | |
391 | { | |
392 | if(IsPalmField()) | |
393 | SetFieldLabel(label); | |
394 | ||
395 | // unlike other native controls, slider has no label | |
396 | if(IsPalmControl() && !wxDynamicCast(this,wxSlider)) | |
397 | SetControlLabel(label); | |
398 | } | |
399 | ||
400 | wxString wxControl::GetFieldLabel() | |
401 | { | |
402 | FieldType* field = (FieldType*)GetObjectPtr(); | |
403 | if(field==NULL) | |
404 | return wxEmptyString; | |
405 | return FldGetTextPtr(field); | |
406 | } | |
407 | ||
408 | wxString wxControl::GetControlLabel() | |
409 | { | |
410 | ControlType* control = (ControlType*)GetObjectPtr(); | |
411 | if(control==NULL) | |
412 | return wxEmptyString; | |
413 | return CtlGetLabel(control); | |
bdb54365 WS |
414 | } |
415 | ||
416 | wxString wxControl::GetLabel() | |
417 | { | |
a152561c WS |
418 | if(IsPalmField()) |
419 | return GetFieldLabel(); | |
420 | ||
421 | // unlike other native controls, slider has no label | |
422 | if(IsPalmControl() && !wxDynamicCast(this,wxSlider)) | |
423 | return GetControlLabel(); | |
bdb54365 WS |
424 | |
425 | return wxEmptyString; | |
426 | } | |
427 | ||
ffecfa5a JS |
428 | /* static */ wxVisualAttributes |
429 | wxControl::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant)) | |
430 | { | |
431 | wxVisualAttributes attrs; | |
432 | ||
433 | // old school (i.e. not "common") controls use the standard dialog font | |
434 | // by default | |
435 | attrs.font = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT); | |
436 | ||
437 | // most, or at least many, of the controls use the same colours as the | |
438 | // buttons -- others will have to override this (and possibly simply call | |
439 | // GetCompositeControlsDefaultAttributes() from their versions) | |
440 | attrs.colFg = wxSystemSettings::GetColour(wxSYS_COLOUR_BTNTEXT); | |
441 | attrs.colBg = wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE); | |
442 | ||
443 | return attrs; | |
444 | } | |
445 | ||
446 | // another version for the "composite", i.e. non simple controls | |
447 | /* static */ wxVisualAttributes | |
448 | wxControl::GetCompositeControlsDefaultAttributes(wxWindowVariant WXUNUSED(variant)) | |
449 | { | |
450 | wxVisualAttributes attrs; | |
451 | attrs.font = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT); | |
452 | attrs.colFg = wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT); | |
453 | attrs.colBg = wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW); | |
454 | ||
455 | return attrs; | |
456 | } | |
457 | ||
458 | // ---------------------------------------------------------------------------- | |
459 | // message handling | |
460 | // ---------------------------------------------------------------------------- | |
461 | ||
462 | bool wxControl::ProcessCommand(wxCommandEvent& event) | |
463 | { | |
464 | return GetEventHandler()->ProcessEvent(event); | |
465 | } | |
466 | ||
ffecfa5a JS |
467 | void wxControl::OnEraseBackground(wxEraseEvent& event) |
468 | { | |
469 | } | |
470 | ||
471 | WXHBRUSH wxControl::OnCtlColor(WXHDC pDC, WXHWND WXUNUSED(pWnd), WXUINT WXUNUSED(nCtlColor), | |
ffecfa5a JS |
472 | WXUINT WXUNUSED(message), |
473 | WXWPARAM WXUNUSED(wParam), | |
474 | WXLPARAM WXUNUSED(lParam) | |
ffecfa5a JS |
475 | ) |
476 | { | |
477 | return (WXHBRUSH)0; | |
478 | } | |
479 | ||
480 | // --------------------------------------------------------------------------- | |
481 | // global functions | |
482 | // --------------------------------------------------------------------------- | |
483 | ||
484 | #endif // wxUSE_CONTROLS |