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