]> git.saurik.com Git - wxWidgets.git/blob - src/palmos/control.cpp
a better fix for using builtin regex under BSD (also fixes compilation for Mac OS...
[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 int groupID)
115 {
116 FormType* form = GetParentForm();
117 if(form==NULL)
118 return false;
119
120 ControlType *control = CtlNewControl(
121 (void **)&form,
122 GetId(),
123 style,
124 wxEmptyString,
125 ( pos.x == wxDefaultCoord ) ? winUndefConstraint : pos.x,
126 ( pos.y == wxDefaultCoord ) ? winUndefConstraint : pos.y,
127 ( size.x == wxDefaultCoord ) ? winUndefConstraint : size.x,
128 ( size.y == wxDefaultCoord ) ? winUndefConstraint : size.y,
129 stdFont,
130 groupID,
131 true
132 );
133
134 if(control==NULL)
135 return false;
136
137 m_palmControl = true;
138
139 SetLabel(label);
140 Show();
141 return true;
142 }
143
144 bool wxControl::PalmCreateField(const wxString& label,
145 const wxPoint& pos,
146 const wxSize& size,
147 bool editable,
148 bool underlined,
149 JustificationType justification)
150 {
151 FormType* form = GetParentForm();
152 if(form==NULL)
153 return false;
154
155 m_label = label;
156
157 FieldType *field = FldNewField(
158 (void **)&form,
159 GetId(),
160 ( pos.x == wxDefaultCoord ) ? winUndefConstraint : pos.x,
161 ( pos.y == wxDefaultCoord ) ? winUndefConstraint : pos.y,
162 ( size.x == wxDefaultCoord ) ? winUndefConstraint : size.x,
163 ( size.y == wxDefaultCoord ) ? winUndefConstraint : size.y,
164 stdFont,
165 10,
166 editable,
167 underlined,
168 false,
169 false,
170 justification,
171 false,
172 false,
173 false
174 );
175
176 if(field==NULL)
177 return false;
178
179 m_palmField = true;
180
181 Show();
182 SetLabel(label);
183 return true;
184 }
185
186 // ----------------------------------------------------------------------------
187 // various accessors
188 // ----------------------------------------------------------------------------
189
190 FormType* wxControl::GetParentForm() const
191 {
192 wxWindow* parentTLW = GetParent();
193 while ( parentTLW && !parentTLW->IsTopLevel() )
194 {
195 parentTLW = parentTLW->GetParent();
196 }
197 wxTopLevelWindowPalm* tlw = wxDynamicCast(parentTLW, wxTopLevelWindowPalm);
198 if(!tlw)
199 return NULL;
200 return tlw->GetForm();
201 }
202
203 FormType* wxControl::GetObjectFormIndex(uint16_t& index) const
204 {
205 FormType* form = GetParentForm();
206 if(form!=NULL)
207 index = FrmGetObjectIndex(form, GetId());
208 else
209 index = frmInvalidObjectId;
210 return form;
211 }
212
213 void* wxControl::GetObjectPtr() const
214 {
215 uint16_t index;
216 FormType* form = GetObjectFormIndex(index);
217 if(form==NULL || index==frmInvalidObjectId)return NULL;
218 return FrmGetObjectPtr(form,index);
219 }
220
221 wxBorder wxControl::GetDefaultBorder() const
222 {
223 // we want to automatically give controls a sunken style (confusingly,
224 // it may not really mean sunken at all as we map it to WS_EX_CLIENTEDGE
225 // which is not sunken at all under Windows XP -- rather, just the default)
226 return wxBORDER_SUNKEN;
227 }
228
229 void wxControl::SetIntValue(int val)
230 {
231 FormType* form = GetParentForm();
232 if(form==NULL)
233 return;
234 uint16_t index = FrmGetObjectIndex(form, GetId());
235 if(index==frmInvalidObjectId)
236 return;
237 FrmSetControlValue(form, index, val);
238 }
239
240 void wxControl::SetBoolValue(bool val)
241 {
242 SetIntValue(val?1:0);
243 }
244
245 bool wxControl::GetBoolValue() const
246 {
247 FormType* form = GetParentForm();
248 if(form==NULL)
249 return false;
250 uint16_t index = FrmGetObjectIndex(form, GetId());
251 if(index==frmInvalidObjectId)
252 return false;
253 return ( FrmGetControlValue(form, index) == 1 );
254 }
255
256 wxSize wxControl::DoGetBestSize() const
257 {
258 return wxSize(16, 16);
259 }
260
261 void wxControl::DoGetBounds( RectangleType &rect ) const
262 {
263 FormType* form = GetParentForm();
264 if(form==NULL)
265 return;
266 uint16_t index = FrmGetObjectIndex(form,GetId());
267 if(index==frmInvalidObjectId)
268 return;
269 FrmGetObjectBounds(form,index,&rect);
270 }
271
272 void wxControl::DoGetPosition( int *x, int *y ) const
273 {
274 RectangleType rect;
275 DoGetBounds(rect);
276 if(x)
277 *x = rect.topLeft.x;
278 if(y)
279 *y = rect.topLeft.y;
280 }
281
282 void wxControl::DoGetSize( int *width, int *height ) const
283 {
284 RectangleType rect;
285 DoGetBounds(rect);
286 if(width)
287 *width = rect.extent.x;
288 if(height)
289 *height = rect.extent.y;
290 }
291
292 bool wxControl::Enable(bool enable)
293 {
294 ControlType *control = (ControlType *)GetObjectPtr();
295 if( (IsPalmControl()) || (control == NULL))
296 return false;
297 if( CtlEnabled(control) == enable)
298 return false;
299 CtlSetEnabled( control, enable);
300 return true;
301 }
302
303 bool wxControl::IsEnabled() const
304 {
305 ControlType *control = (ControlType *)GetObjectPtr();
306 if( (IsPalmControl()) || (control == NULL))
307 return false;
308 return CtlEnabled(control);
309 }
310
311 bool wxControl::IsShown() const
312 {
313 return StatGetAttribute ( statAttrBarVisible , NULL );
314 }
315
316 bool wxControl::Show( bool show )
317 {
318 FormType* form = GetParentForm();
319 if(form==NULL)
320 return false;
321 uint16_t index = FrmGetObjectIndex(form,GetId());
322 if(index==frmInvalidObjectId)
323 return false;
324 if(show)
325 FrmShowObject(form,index);
326 else
327 FrmHideObject(form,index);
328 return true;
329 }
330
331 void wxControl::SetFieldLabel(const wxString& label)
332 {
333 FieldType* field = (FieldType*)GetObjectPtr();
334 if(field==NULL)
335 return;
336
337 uint16_t newSize = label.Length() + 1;
338 MemHandle strHandle = FldGetTextHandle(field);
339 FldSetTextHandle(field, NULL );
340 if (strHandle)
341 {
342 if(MemHandleResize(strHandle, newSize)!=errNone)
343 strHandle = 0;
344 }
345 else
346 {
347 strHandle = MemHandleNew( newSize );
348 }
349 if(!strHandle)
350 return;
351
352 char* str = (char*) MemHandleLock( strHandle );
353 if(str==NULL)
354 return;
355
356 strcpy(str, label.c_str());
357 MemHandleUnlock(strHandle);
358 FldSetTextHandle(field, strHandle);
359 FldRecalculateField(field, true);
360 }
361
362 void wxControl::SetControlLabel(const wxString& label)
363 {
364 ControlType* control = (ControlType*)GetObjectPtr();
365 if(control==NULL)
366 return;
367 CtlSetLabel(control,wxEmptyString);
368 m_label = label;
369 if(!m_label.empty())
370 CtlSetLabel(control,m_label.c_str());
371 }
372
373 void wxControl::SetLabel(const wxString& label)
374 {
375 if(IsPalmField())
376 SetFieldLabel(label);
377
378 // unlike other native controls, slider has no label
379 if(IsPalmControl() && !wxDynamicCast(this,wxSlider))
380 SetControlLabel(label);
381 }
382
383 wxString wxControl::GetFieldLabel()
384 {
385 FieldType* field = (FieldType*)GetObjectPtr();
386 if(field==NULL)
387 return wxEmptyString;
388 return FldGetTextPtr(field);
389 }
390
391 wxString wxControl::GetControlLabel()
392 {
393 ControlType* control = (ControlType*)GetObjectPtr();
394 if(control==NULL)
395 return wxEmptyString;
396 return CtlGetLabel(control);
397 }
398
399 wxString wxControl::GetLabel()
400 {
401 if(IsPalmField())
402 return GetFieldLabel();
403
404 // unlike other native controls, slider has no label
405 if(IsPalmControl() && !wxDynamicCast(this,wxSlider))
406 return GetControlLabel();
407
408 return wxEmptyString;
409 }
410
411 /* static */ wxVisualAttributes
412 wxControl::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
413 {
414 wxVisualAttributes attrs;
415
416 // old school (i.e. not "common") controls use the standard dialog font
417 // by default
418 attrs.font = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT);
419
420 // most, or at least many, of the controls use the same colours as the
421 // buttons -- others will have to override this (and possibly simply call
422 // GetCompositeControlsDefaultAttributes() from their versions)
423 attrs.colFg = wxSystemSettings::GetColour(wxSYS_COLOUR_BTNTEXT);
424 attrs.colBg = wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE);
425
426 return attrs;
427 }
428
429 // another version for the "composite", i.e. non simple controls
430 /* static */ wxVisualAttributes
431 wxControl::GetCompositeControlsDefaultAttributes(wxWindowVariant WXUNUSED(variant))
432 {
433 wxVisualAttributes attrs;
434 attrs.font = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT);
435 attrs.colFg = wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT);
436 attrs.colBg = wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW);
437
438 return attrs;
439 }
440
441 // ----------------------------------------------------------------------------
442 // message handling
443 // ----------------------------------------------------------------------------
444
445 bool wxControl::ProcessCommand(wxCommandEvent& event)
446 {
447 return GetEventHandler()->ProcessEvent(event);
448 }
449
450 void wxControl::OnEraseBackground(wxEraseEvent& event)
451 {
452 }
453
454 WXHBRUSH wxControl::OnCtlColor(WXHDC pDC, WXHWND WXUNUSED(pWnd), WXUINT WXUNUSED(nCtlColor),
455 WXUINT WXUNUSED(message),
456 WXWPARAM WXUNUSED(wParam),
457 WXLPARAM WXUNUSED(lParam)
458 )
459 {
460 return (WXHBRUSH)0;
461 }
462
463 // ---------------------------------------------------------------------------
464 // global functions
465 // ---------------------------------------------------------------------------
466
467 #endif // wxUSE_CONTROLS