]> git.saurik.com Git - wxWidgets.git/blob - src/palmos/control.cpp
1. added SetSelection() to wxItemContainer and removed its declarations
[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 AdjustForParentClientOrigin(x, y);
127
128 ControlType *control = CtlNewControl(
129 (void **)&form,
130 GetId(),
131 style,
132 wxEmptyString,
133 x,
134 y,
135 w,
136 h,
137 stdFont,
138 groupID,
139 true
140 );
141
142 if(control==NULL)
143 return false;
144
145 m_palmControl = true;
146
147 SetInitialBestSize(size);
148 SetLabel(label);
149 Show();
150 return true;
151 }
152
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
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
173 FieldType *field = FldNewField(
174 (void **)&form,
175 GetId(),
176 x,
177 y,
178 w,
179 h,
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
197 SetInitialBestSize(size);
198 SetLabel(label);
199 Show();
200 return true;
201 }
202
203 // ----------------------------------------------------------------------------
204 // various accessors
205 // ----------------------------------------------------------------------------
206
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
220 FormType* wxControl::GetObjectFormIndex(uint16_t& index) const
221 {
222 FormType* form = GetParentForm();
223 if(form!=NULL)
224 index = FrmGetObjectIndex(form, GetId());
225 else
226 index = frmInvalidObjectId;
227 return form;
228 }
229
230 void* wxControl::GetObjectPtr() const
231 {
232 uint16_t index;
233 FormType* form = GetObjectFormIndex(index);
234 if(form==NULL || index==frmInvalidObjectId)return NULL;
235 return FrmGetObjectPtr(form,index);
236 }
237
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
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
273 wxSize wxControl::DoGetBestSize() const
274 {
275 return wxSize(16, 16);
276 }
277
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
289 void wxControl::DoGetPosition( int *x, int *y ) const
290 {
291 RectangleType rect;
292 DoGetBounds(rect);
293 if(x)
294 *x = rect.topLeft.x;
295 if(y)
296 *y = rect.topLeft.y;
297 }
298
299 void wxControl::DoGetSize( int *width, int *height ) const
300 {
301 RectangleType rect;
302 DoGetBounds(rect);
303 if(width)
304 *width = rect.extent.x;
305 if(height)
306 *height = rect.extent.y;
307 }
308
309 bool wxControl::Enable(bool enable)
310 {
311 ControlType *control = (ControlType *)GetObjectPtr();
312 if( (IsPalmControl()) || (control == NULL))
313 return false;
314 if( CtlEnabled(control) == enable)
315 return false;
316 CtlSetEnabled( control, enable);
317 return true;
318 }
319
320 bool wxControl::IsEnabled() const
321 {
322 ControlType *control = (ControlType *)GetObjectPtr();
323 if( (IsPalmControl()) || (control == NULL))
324 return false;
325 return CtlEnabled(control);
326 }
327
328 bool wxControl::IsShown() const
329 {
330 return StatGetAttribute ( statAttrBarVisible , NULL );
331 }
332
333 bool wxControl::Show( bool show )
334 {
335 FormType* form = GetParentForm();
336 if(form==NULL)
337 return false;
338 uint16_t index = FrmGetObjectIndex(form,GetId());
339 if(index==frmInvalidObjectId)
340 return false;
341 if(show)
342 FrmShowObject(form,index);
343 else
344 FrmHideObject(form,index);
345 return true;
346 }
347
348 void wxControl::SetFieldLabel(const wxString& label)
349 {
350 FieldType* field = (FieldType*)GetObjectPtr();
351 if(field==NULL)
352 return;
353
354 uint16_t newSize = label.Length() + 1;
355 MemHandle strHandle = FldGetTextHandle(field);
356 FldSetTextHandle(field, NULL );
357 if (strHandle)
358 {
359 if(MemHandleResize(strHandle, newSize)!=errNone)
360 strHandle = 0;
361 }
362 else
363 {
364 strHandle = MemHandleNew( newSize );
365 }
366 if(!strHandle)
367 return;
368
369 char* str = (char*) MemHandleLock( strHandle );
370 if(str==NULL)
371 return;
372
373 strcpy(str, label.c_str());
374 MemHandleUnlock(strHandle);
375 FldSetTextHandle(field, strHandle);
376 FldRecalculateField(field, true);
377 }
378
379 void wxControl::SetControlLabel(const wxString& label)
380 {
381 ControlType* control = (ControlType*)GetObjectPtr();
382 if(control==NULL)
383 return;
384 CtlSetLabel(control,wxEmptyString);
385 m_label = label;
386 if(!m_label.empty())
387 CtlSetLabel(control,m_label.c_str());
388 }
389
390 void wxControl::SetLabel(const wxString& label)
391 {
392 if(IsPalmField())
393 SetFieldLabel(label);
394
395 // unlike other native controls, slider has no label
396 if(IsPalmControl() && !wxDynamicCast(this,wxSlider))
397 SetControlLabel(label);
398 }
399
400 wxString wxControl::GetFieldLabel()
401 {
402 FieldType* field = (FieldType*)GetObjectPtr();
403 if(field==NULL)
404 return wxEmptyString;
405 return FldGetTextPtr(field);
406 }
407
408 wxString wxControl::GetControlLabel()
409 {
410 ControlType* control = (ControlType*)GetObjectPtr();
411 if(control==NULL)
412 return wxEmptyString;
413 return CtlGetLabel(control);
414 }
415
416 wxString wxControl::GetLabel()
417 {
418 if(IsPalmField())
419 return GetFieldLabel();
420
421 // unlike other native controls, slider has no label
422 if(IsPalmControl() && !wxDynamicCast(this,wxSlider))
423 return GetControlLabel();
424
425 return wxEmptyString;
426 }
427
428 /* static */ wxVisualAttributes
429 wxControl::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
430 {
431 wxVisualAttributes attrs;
432
433 // old school (i.e. not "common") controls use the standard dialog font
434 // by default
435 attrs.font = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT);
436
437 // most, or at least many, of the controls use the same colours as the
438 // buttons -- others will have to override this (and possibly simply call
439 // GetCompositeControlsDefaultAttributes() from their versions)
440 attrs.colFg = wxSystemSettings::GetColour(wxSYS_COLOUR_BTNTEXT);
441 attrs.colBg = wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE);
442
443 return attrs;
444 }
445
446 // another version for the "composite", i.e. non simple controls
447 /* static */ wxVisualAttributes
448 wxControl::GetCompositeControlsDefaultAttributes(wxWindowVariant WXUNUSED(variant))
449 {
450 wxVisualAttributes attrs;
451 attrs.font = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT);
452 attrs.colFg = wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT);
453 attrs.colBg = wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW);
454
455 return attrs;
456 }
457
458 // ----------------------------------------------------------------------------
459 // message handling
460 // ----------------------------------------------------------------------------
461
462 bool wxControl::ProcessCommand(wxCommandEvent& event)
463 {
464 return GetEventHandler()->ProcessEvent(event);
465 }
466
467 void wxControl::OnEraseBackground(wxEraseEvent& event)
468 {
469 }
470
471 WXHBRUSH wxControl::OnCtlColor(WXHDC pDC, WXHWND WXUNUSED(pWnd), WXUINT WXUNUSED(nCtlColor),
472 WXUINT WXUNUSED(message),
473 WXWPARAM WXUNUSED(wParam),
474 WXLPARAM WXUNUSED(lParam)
475 )
476 {
477 return (WXHBRUSH)0;
478 }
479
480 // ---------------------------------------------------------------------------
481 // global functions
482 // ---------------------------------------------------------------------------
483
484 #endif // wxUSE_CONTROLS