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