]> git.saurik.com Git - wxWidgets.git/blob - src/palmos/control.cpp
Global platform header (<PalmOS.h>) removed from public wx-headers (but included...
[wxWidgets.git] / src / palmos / control.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/palmos/control.cpp
3 // Purpose: wxControl class
4 // Author: William Osborne - minimal working wxPalmOS port
5 // Modified by: Wlodzimierz ABX Skiba - native implementation
6 // Created: 10/13/04
7 // RCS-ID: $Id$
8 // Copyright: (c) William Osborne, Wlodzimierz Skiba
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"
42 #include "wx/toplevel.h"
43 #include "wx/button.h"
44 #include "wx/checkbox.h"
45 #include "wx/tglbtn.h"
46 #include "wx/radiobut.h"
47 #include "wx/slider.h"
48
49 #include <Control.h>
50 #include <Form.h>
51 #include <StatusBar.h>
52
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
71 void wxControl::Init()
72 {
73 m_palmControl = false;
74 m_palmField = false;
75 }
76
77 wxControl::~wxControl()
78 {
79 SetLabel(wxEmptyString);
80 m_isBeingDeleted = true;
81
82 DestroyChildren();
83
84 uint16_t index;
85 FormType* form = (FormType*)GetObjectFormIndex(index);
86 if(form!=NULL && index!=frmInvalidObjectId)
87 {
88 FrmRemoveObject((FormType **)&form,index);
89 }
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) )
105 return false;
106
107 #if wxUSE_VALIDATORS
108 SetValidator(validator);
109 #endif
110
111 return true;
112 }
113
114 bool wxControl::PalmCreateControl(int style,
115 const wxString& label,
116 const wxPoint& pos,
117 const wxSize& size,
118 uint8_t groupID)
119 {
120 FormType* form = (FormType*)GetParentForm();
121 if(form==NULL)
122 return false;
123
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
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 }
138
139 ControlType *control = CtlNewControl(
140 (void **)&form,
141 GetId(),
142 (ControlStyleType)style,
143 wxEmptyString,
144 x,
145 y,
146 w,
147 h,
148 stdFont,
149 groupID,
150 true
151 );
152
153 if(control==NULL)
154 return false;
155
156 m_palmControl = true;
157
158 SetInitialBestSize(size);
159 SetLabel(label);
160 Show();
161 return true;
162 }
163
164 bool wxControl::PalmCreateField(const wxString& label,
165 const wxPoint& pos,
166 const wxSize& size,
167 bool editable,
168 bool underlined,
169 int justification)
170 {
171 FormType* form = (FormType*)GetParentForm();
172 if(form==NULL)
173 return false;
174
175 m_label = label;
176
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
184 FieldType *field = FldNewField(
185 (void **)&form,
186 GetId(),
187 x,
188 y,
189 w,
190 h,
191 stdFont,
192 10,
193 editable,
194 underlined,
195 false,
196 false,
197 (JustificationType)justification,
198 false,
199 false,
200 false
201 );
202
203 if(field==NULL)
204 return false;
205
206 m_palmField = true;
207
208 SetInitialBestSize(size);
209 SetLabel(label);
210 Show();
211 return true;
212 }
213
214 // ----------------------------------------------------------------------------
215 // various accessors
216 // ----------------------------------------------------------------------------
217
218 WXFORMPTR wxControl::GetParentForm() const
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
231 WXFORMPTR wxControl::GetObjectFormIndex(uint16_t& index) const
232 {
233 FormType* form = (FormType* )GetParentForm();
234 if(form!=NULL)
235 index = FrmGetObjectIndex(form, GetId());
236 else
237 index = frmInvalidObjectId;
238 return form;
239 }
240
241 void* wxControl::GetObjectPtr() const
242 {
243 uint16_t index;
244 FormType* form = (FormType*)GetObjectFormIndex(index);
245 if(form==NULL || index==frmInvalidObjectId)return NULL;
246 return FrmGetObjectPtr(form,index);
247 }
248
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
257 void wxControl::SetIntValue(int val)
258 {
259 FormType* form = (FormType*)GetParentForm();
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 {
275 FormType* form = (FormType*)GetParentForm();
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
284 wxSize wxControl::DoGetBestSize() const
285 {
286 return wxSize(16, 16);
287 }
288
289 void wxControl::DoGetBounds( WXRECTANGLEPTR rect ) const
290 {
291 if(rect==NULL)
292 return;
293 FormType* form = (FormType*)GetParentForm();
294 if(form==NULL)
295 return;
296 uint16_t index = FrmGetObjectIndex(form,GetId());
297 if(index==frmInvalidObjectId)
298 return;
299 FrmGetObjectBounds(form,index,(RectangleType*)rect);
300 }
301
302 void wxControl::DoSetBounds( WXRECTANGLEPTR rect )
303 {
304 if(rect==NULL)
305 return;
306 FormType* form = (FormType*)GetParentForm();
307 if(form==NULL)
308 return;
309 uint16_t index = FrmGetObjectIndex(form,GetId());
310 if(index==frmInvalidObjectId)
311 return;
312 FrmSetObjectBounds(form,index,(RectangleType*)rect);
313 }
314
315 void wxControl::DoGetPosition( int *x, int *y ) const
316 {
317 int ox = 0, oy = 0;
318 AdjustForParentClientOrigin(ox, oy);
319
320 RectangleType rect;
321 DoGetBounds(&rect);
322
323 if(x)
324 *x = rect.topLeft.x - ox;
325 if(y)
326 *y = rect.topLeft.y - oy;
327 }
328
329 void wxControl::DoGetSize( int *width, int *height ) const
330 {
331 RectangleType rect;
332 DoGetBounds(&rect);
333
334 if(width)
335 *width = rect.extent.x;
336 if(height)
337 *height = rect.extent.y;
338 }
339
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;
348 DoSetBounds(&rect);
349 GetParent()->Refresh(true, &area);
350 }
351
352 bool wxControl::Enable(bool enable)
353 {
354 ControlType *control = (ControlType *)GetObjectPtr();
355 if( !IsPalmControl() || (control == NULL))
356 return false;
357 if( CtlEnabled(control) == enable)
358 return false;
359 CtlSetEnabled( control, enable);
360 return true;
361 }
362
363 bool wxControl::IsEnabled() const
364 {
365 ControlType *control = (ControlType *)GetObjectPtr();
366 if( !IsPalmControl() || (control == NULL))
367 return false;
368 return CtlEnabled(control);
369 }
370
371 bool wxControl::IsShown() const
372 {
373 return StatGetAttribute ( statAttrBarVisible , NULL );
374 }
375
376 bool wxControl::Show( bool show )
377 {
378 FormType* form = (FormType*)GetParentForm();
379 if(form==NULL)
380 return false;
381 uint16_t index = FrmGetObjectIndex(form,GetId());
382 if(index==frmInvalidObjectId)
383 return false;
384 if(show)
385 FrmShowObject(form,index);
386 else
387 FrmHideObject(form,index);
388 return true;
389 }
390
391 void wxControl::SetFieldLabel(const wxString& label)
392 {
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)
401 {
402 if(MemHandleResize(strHandle, newSize)!=errNone)
403 strHandle = 0;
404 }
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 {
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());
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);
457 }
458
459 wxString wxControl::GetLabel()
460 {
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();
467
468 return wxEmptyString;
469 }
470
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
510 void wxControl::OnEraseBackground(wxEraseEvent& event)
511 {
512 }
513
514 #endif // wxUSE_CONTROLS