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