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