]>
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 | ||
324eeecb WS |
289 | void wxControl::DoSetBounds( RectangleType &rect ) |
290 | { | |
291 | FormType* form = GetParentForm(); | |
292 | if(form==NULL) | |
293 | return; | |
294 | uint16_t index = FrmGetObjectIndex(form,GetId()); | |
295 | if(index==frmInvalidObjectId) | |
296 | return; | |
297 | FrmSetObjectBounds(form,index,&rect); | |
298 | } | |
299 | ||
808e3bce WS |
300 | void wxControl::DoGetPosition( int *x, int *y ) const |
301 | { | |
302 | RectangleType rect; | |
303 | DoGetBounds(rect); | |
304 | if(x) | |
305 | *x = rect.topLeft.x; | |
306 | if(y) | |
307 | *y = rect.topLeft.y; | |
308 | } | |
309 | ||
310 | void wxControl::DoGetSize( int *width, int *height ) const | |
311 | { | |
312 | RectangleType rect; | |
313 | DoGetBounds(rect); | |
314 | if(width) | |
315 | *width = rect.extent.x; | |
316 | if(height) | |
317 | *height = rect.extent.y; | |
318 | } | |
319 | ||
324eeecb WS |
320 | void wxControl::DoMoveWindow(int x, int y, int width, int height) |
321 | { | |
322 | wxRect area = GetRect(); | |
323 | RectangleType rect; | |
324 | rect.topLeft.x = x; | |
325 | rect.topLeft.y = y; | |
326 | rect.extent.x = width; | |
327 | rect.extent.y = height; | |
328 | DoSetBounds(rect); | |
329 | GetParent()->Refresh(true, &area); | |
330 | } | |
331 | ||
db101bd3 | 332 | bool wxControl::Enable(bool enable) |
ffecfa5a | 333 | { |
a152561c WS |
334 | ControlType *control = (ControlType *)GetObjectPtr(); |
335 | if( (IsPalmControl()) || (control == NULL)) | |
db101bd3 | 336 | return false; |
a152561c | 337 | if( CtlEnabled(control) == enable) |
db101bd3 | 338 | return false; |
a152561c | 339 | CtlSetEnabled( control, enable); |
db101bd3 WS |
340 | return true; |
341 | } | |
342 | ||
343 | bool wxControl::IsEnabled() const | |
344 | { | |
a152561c WS |
345 | ControlType *control = (ControlType *)GetObjectPtr(); |
346 | if( (IsPalmControl()) || (control == NULL)) | |
db101bd3 | 347 | return false; |
a152561c | 348 | return CtlEnabled(control); |
db101bd3 WS |
349 | } |
350 | ||
351 | bool wxControl::IsShown() const | |
352 | { | |
353 | return StatGetAttribute ( statAttrBarVisible , NULL ); | |
354 | } | |
355 | ||
356 | bool wxControl::Show( bool show ) | |
357 | { | |
ba889513 WS |
358 | FormType* form = GetParentForm(); |
359 | if(form==NULL) | |
360 | return false; | |
361 | uint16_t index = FrmGetObjectIndex(form,GetId()); | |
362 | if(index==frmInvalidObjectId) | |
363 | return false; | |
db101bd3 | 364 | if(show) |
ba889513 | 365 | FrmShowObject(form,index); |
db101bd3 | 366 | else |
ba889513 | 367 | FrmHideObject(form,index); |
db101bd3 | 368 | return true; |
ffecfa5a JS |
369 | } |
370 | ||
a152561c | 371 | void wxControl::SetFieldLabel(const wxString& label) |
bdb54365 | 372 | { |
a152561c WS |
373 | FieldType* field = (FieldType*)GetObjectPtr(); |
374 | if(field==NULL) | |
375 | return; | |
376 | ||
377 | uint16_t newSize = label.Length() + 1; | |
378 | MemHandle strHandle = FldGetTextHandle(field); | |
379 | FldSetTextHandle(field, NULL ); | |
380 | if (strHandle) | |
bdb54365 | 381 | { |
a152561c WS |
382 | if(MemHandleResize(strHandle, newSize)!=errNone) |
383 | strHandle = 0; | |
bdb54365 | 384 | } |
a152561c WS |
385 | else |
386 | { | |
387 | strHandle = MemHandleNew( newSize ); | |
388 | } | |
389 | if(!strHandle) | |
390 | return; | |
391 | ||
392 | char* str = (char*) MemHandleLock( strHandle ); | |
393 | if(str==NULL) | |
394 | return; | |
395 | ||
396 | strcpy(str, label.c_str()); | |
397 | MemHandleUnlock(strHandle); | |
398 | FldSetTextHandle(field, strHandle); | |
399 | FldRecalculateField(field, true); | |
400 | } | |
401 | ||
402 | void wxControl::SetControlLabel(const wxString& label) | |
403 | { | |
9a727a3b WS |
404 | ControlType* control = (ControlType*)GetObjectPtr(); |
405 | if(control==NULL) | |
406 | return; | |
407 | CtlSetLabel(control,wxEmptyString); | |
408 | m_label = label; | |
409 | if(!m_label.empty()) | |
410 | CtlSetLabel(control,m_label.c_str()); | |
a152561c WS |
411 | } |
412 | ||
413 | void wxControl::SetLabel(const wxString& label) | |
414 | { | |
415 | if(IsPalmField()) | |
416 | SetFieldLabel(label); | |
417 | ||
418 | // unlike other native controls, slider has no label | |
419 | if(IsPalmControl() && !wxDynamicCast(this,wxSlider)) | |
420 | SetControlLabel(label); | |
421 | } | |
422 | ||
423 | wxString wxControl::GetFieldLabel() | |
424 | { | |
425 | FieldType* field = (FieldType*)GetObjectPtr(); | |
426 | if(field==NULL) | |
427 | return wxEmptyString; | |
428 | return FldGetTextPtr(field); | |
429 | } | |
430 | ||
431 | wxString wxControl::GetControlLabel() | |
432 | { | |
433 | ControlType* control = (ControlType*)GetObjectPtr(); | |
434 | if(control==NULL) | |
435 | return wxEmptyString; | |
436 | return CtlGetLabel(control); | |
bdb54365 WS |
437 | } |
438 | ||
439 | wxString wxControl::GetLabel() | |
440 | { | |
a152561c WS |
441 | if(IsPalmField()) |
442 | return GetFieldLabel(); | |
443 | ||
444 | // unlike other native controls, slider has no label | |
445 | if(IsPalmControl() && !wxDynamicCast(this,wxSlider)) | |
446 | return GetControlLabel(); | |
bdb54365 WS |
447 | |
448 | return wxEmptyString; | |
449 | } | |
450 | ||
ffecfa5a JS |
451 | /* static */ wxVisualAttributes |
452 | wxControl::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant)) | |
453 | { | |
454 | wxVisualAttributes attrs; | |
455 | ||
456 | // old school (i.e. not "common") controls use the standard dialog font | |
457 | // by default | |
458 | attrs.font = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT); | |
459 | ||
460 | // most, or at least many, of the controls use the same colours as the | |
461 | // buttons -- others will have to override this (and possibly simply call | |
462 | // GetCompositeControlsDefaultAttributes() from their versions) | |
463 | attrs.colFg = wxSystemSettings::GetColour(wxSYS_COLOUR_BTNTEXT); | |
464 | attrs.colBg = wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE); | |
465 | ||
466 | return attrs; | |
467 | } | |
468 | ||
469 | // another version for the "composite", i.e. non simple controls | |
470 | /* static */ wxVisualAttributes | |
471 | wxControl::GetCompositeControlsDefaultAttributes(wxWindowVariant WXUNUSED(variant)) | |
472 | { | |
473 | wxVisualAttributes attrs; | |
474 | attrs.font = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT); | |
475 | attrs.colFg = wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT); | |
476 | attrs.colBg = wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW); | |
477 | ||
478 | return attrs; | |
479 | } | |
480 | ||
481 | // ---------------------------------------------------------------------------- | |
482 | // message handling | |
483 | // ---------------------------------------------------------------------------- | |
484 | ||
485 | bool wxControl::ProcessCommand(wxCommandEvent& event) | |
486 | { | |
487 | return GetEventHandler()->ProcessEvent(event); | |
488 | } | |
489 | ||
ffecfa5a JS |
490 | void wxControl::OnEraseBackground(wxEraseEvent& event) |
491 | { | |
492 | } | |
493 | ||
ffecfa5a | 494 | #endif // wxUSE_CONTROLS |