]> git.saurik.com Git - wxWidgets.git/blame - src/palmos/control.cpp
Include wx/string.h according to precompiled headers of wx/wx.h (with other minor...
[wxWidgets.git] / src / palmos / control.cpp
CommitLineData
ffecfa5a 1/////////////////////////////////////////////////////////////////////////////
e2731512 2// Name: src/palmos/control.cpp
ffecfa5a 3// Purpose: wxControl class
e2731512 4// Author: William Osborne - minimal working wxPalmOS port
bdb54365 5// Modified by: Wlodzimierz ABX Skiba - native implementation
ffecfa5a 6// Created: 10/13/04
e2731512 7// RCS-ID: $Id$
bdb54365 8// Copyright: (c) William Osborne, Wlodzimierz Skiba
ffecfa5a
JS
9// Licence: wxWindows licence
10/////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
ffecfa5a
JS
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"
bdb54365
WS
38#include "wx/toplevel.h"
39#include "wx/button.h"
40#include "wx/checkbox.h"
41#include "wx/tglbtn.h"
808e3bce 42#include "wx/radiobut.h"
a152561c 43#include "wx/slider.h"
ffecfa5a 44
20bc5ad8
WS
45#include <Control.h>
46#include <Form.h>
47#include <StatusBar.h>
48
ffecfa5a
JS
49// ----------------------------------------------------------------------------
50// wxWin macros
51// ----------------------------------------------------------------------------
52
53IMPLEMENT_ABSTRACT_CLASS(wxControl, wxWindow)
54
55BEGIN_EVENT_TABLE(wxControl, wxWindow)
56 EVT_ERASE_BACKGROUND(wxControl::OnEraseBackground)
57END_EVENT_TABLE()
58
59// ============================================================================
60// wxControl implementation
61// ============================================================================
62
63// ----------------------------------------------------------------------------
64// wxControl ctor/dtor
65// ----------------------------------------------------------------------------
66
a152561c
WS
67void wxControl::Init()
68{
69 m_palmControl = false;
70 m_palmField = false;
71}
72
ffecfa5a
JS
73wxControl::~wxControl()
74{
9a727a3b 75 SetLabel(wxEmptyString);
db101bd3 76 m_isBeingDeleted = true;
5b72333d
WS
77
78 DestroyChildren();
79
80 uint16_t index;
20bc5ad8 81 FormType* form = (FormType*)GetObjectFormIndex(index);
5b72333d
WS
82 if(form!=NULL && index!=frmInvalidObjectId)
83 {
84 FrmRemoveObject((FormType **)&form,index);
85 }
ffecfa5a
JS
86}
87
88// ----------------------------------------------------------------------------
89// control window creation
90// ----------------------------------------------------------------------------
91
92bool 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) )
db101bd3 101 return false;
ffecfa5a
JS
102
103#if wxUSE_VALIDATORS
104 SetValidator(validator);
105#endif
106
db101bd3 107 return true;
ffecfa5a
JS
108}
109
20bc5ad8 110bool wxControl::PalmCreateControl(int style,
db101bd3
WS
111 const wxString& label,
112 const wxPoint& pos,
a152561c 113 const wxSize& size,
8be10866 114 uint8_t groupID)
ffecfa5a 115{
20bc5ad8 116 FormType* form = (FormType*)GetParentForm();
ba889513 117 if(form==NULL)
bdb54365 118 return false;
bdb54365 119
be4e4e27
WS
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
d2893292
WS
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 }
be4e4e27 134
a152561c
WS
135 ControlType *control = CtlNewControl(
136 (void **)&form,
137 GetId(),
20bc5ad8 138 (ControlStyleType)style,
9a727a3b 139 wxEmptyString,
be4e4e27
WS
140 x,
141 y,
142 w,
143 h,
a152561c
WS
144 stdFont,
145 groupID,
5b72333d 146 true
a152561c
WS
147 );
148
149 if(control==NULL)
db101bd3
WS
150 return false;
151
a152561c
WS
152 m_palmControl = true;
153
be4e4e27 154 SetInitialBestSize(size);
9a727a3b 155 SetLabel(label);
db101bd3
WS
156 Show();
157 return true;
ffecfa5a
JS
158}
159
a152561c
WS
160bool wxControl::PalmCreateField(const wxString& label,
161 const wxPoint& pos,
162 const wxSize& size,
163 bool editable,
164 bool underlined,
20bc5ad8 165 int justification)
a152561c 166{
20bc5ad8 167 FormType* form = (FormType*)GetParentForm();
a152561c
WS
168 if(form==NULL)
169 return false;
170
171 m_label = label;
172
be4e4e27
WS
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
a152561c
WS
180 FieldType *field = FldNewField(
181 (void **)&form,
182 GetId(),
be4e4e27
WS
183 x,
184 y,
185 w,
186 h,
a152561c
WS
187 stdFont,
188 10,
189 editable,
190 underlined,
191 false,
192 false,
20bc5ad8 193 (JustificationType)justification,
a152561c
WS
194 false,
195 false,
196 false
197 );
198
199 if(field==NULL)
200 return false;
201
202 m_palmField = true;
203
be4e4e27 204 SetInitialBestSize(size);
a152561c 205 SetLabel(label);
be4e4e27 206 Show();
a152561c
WS
207 return true;
208}
209
ffecfa5a
JS
210// ----------------------------------------------------------------------------
211// various accessors
212// ----------------------------------------------------------------------------
213
20bc5ad8 214WXFORMPTR wxControl::GetParentForm() const
ba889513
WS
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
20bc5ad8 227WXFORMPTR wxControl::GetObjectFormIndex(uint16_t& index) const
a152561c 228{
20bc5ad8 229 FormType* form = (FormType* )GetParentForm();
5b72333d
WS
230 if(form!=NULL)
231 index = FrmGetObjectIndex(form, GetId());
232 else
233 index = frmInvalidObjectId;
234 return form;
a152561c
WS
235}
236
237void* wxControl::GetObjectPtr() const
238{
5b72333d 239 uint16_t index;
20bc5ad8 240 FormType* form = (FormType*)GetObjectFormIndex(index);
5b72333d 241 if(form==NULL || index==frmInvalidObjectId)return NULL;
a152561c
WS
242 return FrmGetObjectPtr(form,index);
243}
244
ffecfa5a
JS
245wxBorder 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
ba889513
WS
253void wxControl::SetIntValue(int val)
254{
20bc5ad8 255 FormType* form = (FormType*)GetParentForm();
ba889513
WS
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
264void wxControl::SetBoolValue(bool val)
265{
266 SetIntValue(val?1:0);
267}
268
269bool wxControl::GetBoolValue() const
270{
20bc5ad8 271 FormType* form = (FormType*)GetParentForm();
ba889513
WS
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
db101bd3 280wxSize wxControl::DoGetBestSize() const
ffecfa5a 281{
db101bd3 282 return wxSize(16, 16);
ffecfa5a
JS
283}
284
20bc5ad8 285void wxControl::DoGetBounds( WXRECTANGLEPTR rect ) const
808e3bce 286{
20bc5ad8
WS
287 if(rect==NULL)
288 return;
289 FormType* form = (FormType*)GetParentForm();
808e3bce
WS
290 if(form==NULL)
291 return;
292 uint16_t index = FrmGetObjectIndex(form,GetId());
293 if(index==frmInvalidObjectId)
294 return;
20bc5ad8 295 FrmGetObjectBounds(form,index,(RectangleType*)rect);
808e3bce
WS
296}
297
20bc5ad8 298void wxControl::DoSetBounds( WXRECTANGLEPTR rect )
324eeecb 299{
20bc5ad8
WS
300 if(rect==NULL)
301 return;
302 FormType* form = (FormType*)GetParentForm();
324eeecb
WS
303 if(form==NULL)
304 return;
305 uint16_t index = FrmGetObjectIndex(form,GetId());
306 if(index==frmInvalidObjectId)
307 return;
20bc5ad8 308 FrmSetObjectBounds(form,index,(RectangleType*)rect);
324eeecb
WS
309}
310
808e3bce
WS
311void wxControl::DoGetPosition( int *x, int *y ) const
312{
d2893292
WS
313 int ox = 0, oy = 0;
314 AdjustForParentClientOrigin(ox, oy);
315
808e3bce 316 RectangleType rect;
20bc5ad8 317 DoGetBounds(&rect);
d2893292 318
808e3bce 319 if(x)
d2893292 320 *x = rect.topLeft.x - ox;
808e3bce 321 if(y)
d2893292 322 *y = rect.topLeft.y - oy;
808e3bce
WS
323}
324
325void wxControl::DoGetSize( int *width, int *height ) const
326{
327 RectangleType rect;
20bc5ad8 328 DoGetBounds(&rect);
d2893292 329
808e3bce
WS
330 if(width)
331 *width = rect.extent.x;
332 if(height)
333 *height = rect.extent.y;
334}
335
324eeecb
WS
336void 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;
20bc5ad8 344 DoSetBounds(&rect);
324eeecb
WS
345 GetParent()->Refresh(true, &area);
346}
347
db101bd3 348bool wxControl::Enable(bool enable)
ffecfa5a 349{
a152561c 350 ControlType *control = (ControlType *)GetObjectPtr();
1a87edf2 351 if( !IsPalmControl() || (control == NULL))
db101bd3 352 return false;
a152561c 353 if( CtlEnabled(control) == enable)
db101bd3 354 return false;
a152561c 355 CtlSetEnabled( control, enable);
db101bd3
WS
356 return true;
357}
358
359bool wxControl::IsEnabled() const
360{
a152561c 361 ControlType *control = (ControlType *)GetObjectPtr();
1a87edf2 362 if( !IsPalmControl() || (control == NULL))
db101bd3 363 return false;
a152561c 364 return CtlEnabled(control);
db101bd3
WS
365}
366
367bool wxControl::IsShown() const
368{
369 return StatGetAttribute ( statAttrBarVisible , NULL );
370}
371
372bool wxControl::Show( bool show )
373{
20bc5ad8 374 FormType* form = (FormType*)GetParentForm();
ba889513
WS
375 if(form==NULL)
376 return false;
377 uint16_t index = FrmGetObjectIndex(form,GetId());
378 if(index==frmInvalidObjectId)
379 return false;
db101bd3 380 if(show)
ba889513 381 FrmShowObject(form,index);
db101bd3 382 else
ba889513 383 FrmHideObject(form,index);
db101bd3 384 return true;
ffecfa5a
JS
385}
386
a152561c 387void wxControl::SetFieldLabel(const wxString& label)
bdb54365 388{
a152561c
WS
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)
bdb54365 397 {
a152561c
WS
398 if(MemHandleResize(strHandle, newSize)!=errNone)
399 strHandle = 0;
bdb54365 400 }
a152561c
WS
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
418void wxControl::SetControlLabel(const wxString& label)
419{
9a727a3b
WS
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());
a152561c
WS
427}
428
429void 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
439wxString wxControl::GetFieldLabel()
440{
441 FieldType* field = (FieldType*)GetObjectPtr();
442 if(field==NULL)
443 return wxEmptyString;
444 return FldGetTextPtr(field);
445}
446
447wxString wxControl::GetControlLabel()
448{
449 ControlType* control = (ControlType*)GetObjectPtr();
450 if(control==NULL)
451 return wxEmptyString;
452 return CtlGetLabel(control);
bdb54365
WS
453}
454
455wxString wxControl::GetLabel()
456{
a152561c
WS
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();
bdb54365
WS
463
464 return wxEmptyString;
465}
466
ffecfa5a
JS
467/* static */ wxVisualAttributes
468wxControl::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
487wxControl::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
501bool wxControl::ProcessCommand(wxCommandEvent& event)
502{
503 return GetEventHandler()->ProcessEvent(event);
504}
505
ffecfa5a
JS
506void wxControl::OnEraseBackground(wxEraseEvent& event)
507{
508}
509
ffecfa5a 510#endif // wxUSE_CONTROLS