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