]> git.saurik.com Git - wxWidgets.git/blame - src/palmos/control.cpp
map EAGAIN to wxSOCKET_WOULDBLOCK too as tit has this meaning for read() (even though...
[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
93fbbe07
WS
29#include "wx/control.h"
30
ffecfa5a
JS
31#ifndef WX_PRECOMP
32 #include "wx/event.h"
33 #include "wx/app.h"
34 #include "wx/dcclient.h"
35 #include "wx/log.h"
36 #include "wx/settings.h"
f1e01716 37 #include "wx/button.h"
ab1ce969 38 #include "wx/checkbox.h"
b0b5881a 39 #include "wx/radiobut.h"
f4da9a94 40 #include "wx/slider.h"
1832043f 41 #include "wx/toplevel.h"
ffecfa5a
JS
42#endif
43
bdb54365 44#include "wx/tglbtn.h"
ffecfa5a 45
20bc5ad8
WS
46#include <Control.h>
47#include <Form.h>
6afc1b46
VZ
48#ifdef __WXPALMOS6__
49 #include <StatusBar.h>
50#else
51 #include <PenInputMgr.h>
52#endif // __WXPALMOS6__
20bc5ad8 53
ffecfa5a
JS
54// ----------------------------------------------------------------------------
55// wxWin macros
56// ----------------------------------------------------------------------------
57
58IMPLEMENT_ABSTRACT_CLASS(wxControl, wxWindow)
59
60BEGIN_EVENT_TABLE(wxControl, wxWindow)
61 EVT_ERASE_BACKGROUND(wxControl::OnEraseBackground)
62END_EVENT_TABLE()
63
64// ============================================================================
65// wxControl implementation
66// ============================================================================
67
68// ----------------------------------------------------------------------------
69// wxControl ctor/dtor
70// ----------------------------------------------------------------------------
71
a152561c
WS
72void wxControl::Init()
73{
74 m_palmControl = false;
75 m_palmField = false;
76}
77
ffecfa5a
JS
78wxControl::~wxControl()
79{
9a727a3b 80 SetLabel(wxEmptyString);
db101bd3 81 m_isBeingDeleted = true;
5b72333d
WS
82
83 DestroyChildren();
84
85 uint16_t index;
20bc5ad8 86 FormType* form = (FormType*)GetObjectFormIndex(index);
5b72333d
WS
87 if(form!=NULL && index!=frmInvalidObjectId)
88 {
89 FrmRemoveObject((FormType **)&form,index);
90 }
ffecfa5a
JS
91}
92
93// ----------------------------------------------------------------------------
94// control window creation
95// ----------------------------------------------------------------------------
96
97bool wxControl::Create(wxWindow *parent,
98 wxWindowID id,
99 const wxPoint& pos,
100 const wxSize& size,
101 long style,
102 const wxValidator& wxVALIDATOR_PARAM(validator),
103 const wxString& name)
104{
105 if ( !wxWindow::Create(parent, id, pos, size, style, name) )
db101bd3 106 return false;
ffecfa5a
JS
107
108#if wxUSE_VALIDATORS
109 SetValidator(validator);
110#endif
111
db101bd3 112 return true;
ffecfa5a
JS
113}
114
20bc5ad8 115bool wxControl::PalmCreateControl(int style,
db101bd3
WS
116 const wxString& label,
117 const wxPoint& pos,
a152561c 118 const wxSize& size,
8be10866 119 uint8_t groupID)
ffecfa5a 120{
20bc5ad8 121 FormType* form = (FormType*)GetParentForm();
ba889513 122 if(form==NULL)
bdb54365 123 return false;
bdb54365 124
be4e4e27
WS
125
126 wxCoord x = pos.x == wxDefaultCoord ? 0 : pos.x,
127 y = pos.y == wxDefaultCoord ? 0 : pos.y,
128 w = size.x == wxDefaultCoord ? 1 : size.x,
129 h = size.y == wxDefaultCoord ? 1 : size.y;
130
d2893292
WS
131 wxWindow *win = this;
132 while(win->GetParent())
133 {
134 win = win->GetParent();
135 wxPoint pt(win->GetClientAreaOrigin());
136 x += pt.x;
137 y += pt.y;
138 }
be4e4e27 139
a152561c
WS
140 ControlType *control = CtlNewControl(
141 (void **)&form,
142 GetId(),
20bc5ad8 143 (ControlStyleType)style,
e2fc40b4 144 NULL,
be4e4e27
WS
145 x,
146 y,
147 w,
148 h,
a152561c
WS
149 stdFont,
150 groupID,
5b72333d 151 true
a152561c
WS
152 );
153
154 if(control==NULL)
db101bd3
WS
155 return false;
156
a152561c
WS
157 m_palmControl = true;
158
170acdc9 159 SetInitialSize(size);
9a727a3b 160 SetLabel(label);
db101bd3
WS
161 Show();
162 return true;
ffecfa5a
JS
163}
164
a152561c
WS
165bool wxControl::PalmCreateField(const wxString& label,
166 const wxPoint& pos,
167 const wxSize& size,
168 bool editable,
169 bool underlined,
20bc5ad8 170 int justification)
a152561c 171{
20bc5ad8 172 FormType* form = (FormType*)GetParentForm();
a152561c
WS
173 if(form==NULL)
174 return false;
175
176 m_label = label;
177
be4e4e27
WS
178 wxCoord x = pos.x == wxDefaultCoord ? 0 : pos.x,
179 y = pos.y == wxDefaultCoord ? 0 : pos.y,
180 w = size.x == wxDefaultCoord ? 1 : size.x,
181 h = size.y == wxDefaultCoord ? 1 : size.y;
182
183 AdjustForParentClientOrigin(x, y);
184
a152561c
WS
185 FieldType *field = FldNewField(
186 (void **)&form,
187 GetId(),
be4e4e27
WS
188 x,
189 y,
190 w,
191 h,
a152561c
WS
192 stdFont,
193 10,
194 editable,
195 underlined,
196 false,
197 false,
20bc5ad8 198 (JustificationType)justification,
a152561c
WS
199 false,
200 false,
201 false
202 );
203
204 if(field==NULL)
205 return false;
206
207 m_palmField = true;
208
170acdc9 209 SetInitialSize(size);
a152561c 210 SetLabel(label);
be4e4e27 211 Show();
a152561c
WS
212 return true;
213}
214
ffecfa5a
JS
215// ----------------------------------------------------------------------------
216// various accessors
217// ----------------------------------------------------------------------------
218
20bc5ad8 219WXFORMPTR wxControl::GetParentForm() const
ba889513
WS
220{
221 wxWindow* parentTLW = GetParent();
222 while ( parentTLW && !parentTLW->IsTopLevel() )
223 {
224 parentTLW = parentTLW->GetParent();
225 }
226 wxTopLevelWindowPalm* tlw = wxDynamicCast(parentTLW, wxTopLevelWindowPalm);
227 if(!tlw)
228 return NULL;
229 return tlw->GetForm();
230}
231
20bc5ad8 232WXFORMPTR wxControl::GetObjectFormIndex(uint16_t& index) const
a152561c 233{
20bc5ad8 234 FormType* form = (FormType* )GetParentForm();
5b72333d
WS
235 if(form!=NULL)
236 index = FrmGetObjectIndex(form, GetId());
237 else
238 index = frmInvalidObjectId;
239 return form;
a152561c
WS
240}
241
242void* wxControl::GetObjectPtr() const
243{
5b72333d 244 uint16_t index;
20bc5ad8 245 FormType* form = (FormType*)GetObjectFormIndex(index);
5b72333d 246 if(form==NULL || index==frmInvalidObjectId)return NULL;
a152561c
WS
247 return FrmGetObjectPtr(form,index);
248}
249
ffecfa5a
JS
250wxBorder wxControl::GetDefaultBorder() const
251{
252 // we want to automatically give controls a sunken style (confusingly,
253 // it may not really mean sunken at all as we map it to WS_EX_CLIENTEDGE
254 // which is not sunken at all under Windows XP -- rather, just the default)
255 return wxBORDER_SUNKEN;
256}
257
ba889513
WS
258void wxControl::SetIntValue(int val)
259{
20bc5ad8 260 FormType* form = (FormType*)GetParentForm();
ba889513
WS
261 if(form==NULL)
262 return;
263 uint16_t index = FrmGetObjectIndex(form, GetId());
264 if(index==frmInvalidObjectId)
265 return;
266 FrmSetControlValue(form, index, val);
267}
268
269void wxControl::SetBoolValue(bool val)
270{
271 SetIntValue(val?1:0);
272}
273
274bool wxControl::GetBoolValue() const
275{
20bc5ad8 276 FormType* form = (FormType*)GetParentForm();
ba889513
WS
277 if(form==NULL)
278 return false;
279 uint16_t index = FrmGetObjectIndex(form, GetId());
280 if(index==frmInvalidObjectId)
281 return false;
282 return ( FrmGetControlValue(form, index) == 1 );
283}
284
db101bd3 285wxSize wxControl::DoGetBestSize() const
ffecfa5a 286{
db101bd3 287 return wxSize(16, 16);
ffecfa5a
JS
288}
289
20bc5ad8 290void wxControl::DoGetBounds( WXRECTANGLEPTR rect ) const
808e3bce 291{
20bc5ad8
WS
292 if(rect==NULL)
293 return;
294 FormType* form = (FormType*)GetParentForm();
808e3bce
WS
295 if(form==NULL)
296 return;
297 uint16_t index = FrmGetObjectIndex(form,GetId());
298 if(index==frmInvalidObjectId)
299 return;
20bc5ad8 300 FrmGetObjectBounds(form,index,(RectangleType*)rect);
808e3bce
WS
301}
302
20bc5ad8 303void wxControl::DoSetBounds( WXRECTANGLEPTR rect )
324eeecb 304{
20bc5ad8
WS
305 if(rect==NULL)
306 return;
307 FormType* form = (FormType*)GetParentForm();
324eeecb
WS
308 if(form==NULL)
309 return;
310 uint16_t index = FrmGetObjectIndex(form,GetId());
311 if(index==frmInvalidObjectId)
312 return;
20bc5ad8 313 FrmSetObjectBounds(form,index,(RectangleType*)rect);
324eeecb
WS
314}
315
808e3bce
WS
316void wxControl::DoGetPosition( int *x, int *y ) const
317{
d2893292
WS
318 int ox = 0, oy = 0;
319 AdjustForParentClientOrigin(ox, oy);
320
808e3bce 321 RectangleType rect;
20bc5ad8 322 DoGetBounds(&rect);
d2893292 323
808e3bce 324 if(x)
d2893292 325 *x = rect.topLeft.x - ox;
808e3bce 326 if(y)
d2893292 327 *y = rect.topLeft.y - oy;
808e3bce
WS
328}
329
330void wxControl::DoGetSize( int *width, int *height ) const
331{
332 RectangleType rect;
20bc5ad8 333 DoGetBounds(&rect);
d2893292 334
808e3bce
WS
335 if(width)
336 *width = rect.extent.x;
337 if(height)
338 *height = rect.extent.y;
339}
340
324eeecb
WS
341void wxControl::DoMoveWindow(int x, int y, int width, int height)
342{
343 wxRect area = GetRect();
344 RectangleType rect;
345 rect.topLeft.x = x;
346 rect.topLeft.y = y;
347 rect.extent.x = width;
348 rect.extent.y = height;
20bc5ad8 349 DoSetBounds(&rect);
324eeecb
WS
350 GetParent()->Refresh(true, &area);
351}
352
db101bd3 353bool wxControl::Enable(bool enable)
ffecfa5a 354{
a152561c 355 ControlType *control = (ControlType *)GetObjectPtr();
1a87edf2 356 if( !IsPalmControl() || (control == NULL))
db101bd3 357 return false;
a152561c 358 if( CtlEnabled(control) == enable)
db101bd3 359 return false;
a152561c 360 CtlSetEnabled( control, enable);
db101bd3
WS
361 return true;
362}
363
364bool wxControl::IsEnabled() const
365{
a152561c 366 ControlType *control = (ControlType *)GetObjectPtr();
1a87edf2 367 if( !IsPalmControl() || (control == NULL))
db101bd3 368 return false;
a152561c 369 return CtlEnabled(control);
db101bd3
WS
370}
371
372bool wxControl::IsShown() const
373{
374 return StatGetAttribute ( statAttrBarVisible , NULL );
375}
376
377bool wxControl::Show( bool show )
378{
20bc5ad8 379 FormType* form = (FormType*)GetParentForm();
ba889513
WS
380 if(form==NULL)
381 return false;
382 uint16_t index = FrmGetObjectIndex(form,GetId());
383 if(index==frmInvalidObjectId)
384 return false;
db101bd3 385 if(show)
ba889513 386 FrmShowObject(form,index);
db101bd3 387 else
ba889513 388 FrmHideObject(form,index);
db101bd3 389 return true;
ffecfa5a
JS
390}
391
a152561c 392void wxControl::SetFieldLabel(const wxString& label)
bdb54365 393{
a152561c
WS
394 FieldType* field = (FieldType*)GetObjectPtr();
395 if(field==NULL)
396 return;
397
f1e01716 398 uint16_t newSize = label.length() + 1;
a152561c
WS
399 MemHandle strHandle = FldGetTextHandle(field);
400 FldSetTextHandle(field, NULL );
401 if (strHandle)
bdb54365 402 {
a152561c
WS
403 if(MemHandleResize(strHandle, newSize)!=errNone)
404 strHandle = 0;
bdb54365 405 }
a152561c
WS
406 else
407 {
408 strHandle = MemHandleNew( newSize );
409 }
410 if(!strHandle)
411 return;
412
413 char* str = (char*) MemHandleLock( strHandle );
414 if(str==NULL)
415 return;
416
417 strcpy(str, label.c_str());
418 MemHandleUnlock(strHandle);
419 FldSetTextHandle(field, strHandle);
420 FldRecalculateField(field, true);
421}
422
423void wxControl::SetControlLabel(const wxString& label)
424{
9a727a3b
WS
425 ControlType* control = (ControlType*)GetObjectPtr();
426 if(control==NULL)
427 return;
e2fc40b4 428 CtlSetLabel(control, "");
9a727a3b
WS
429 m_label = label;
430 if(!m_label.empty())
431 CtlSetLabel(control,m_label.c_str());
a152561c
WS
432}
433
434void wxControl::SetLabel(const wxString& label)
435{
436 if(IsPalmField())
437 SetFieldLabel(label);
438
439 // unlike other native controls, slider has no label
440 if(IsPalmControl() && !wxDynamicCast(this,wxSlider))
441 SetControlLabel(label);
442}
443
444wxString wxControl::GetFieldLabel()
445{
446 FieldType* field = (FieldType*)GetObjectPtr();
447 if(field==NULL)
448 return wxEmptyString;
449 return FldGetTextPtr(field);
450}
451
452wxString wxControl::GetControlLabel()
453{
454 ControlType* control = (ControlType*)GetObjectPtr();
455 if(control==NULL)
456 return wxEmptyString;
457 return CtlGetLabel(control);
bdb54365 458}
e2fc40b4 459#if 0
bdb54365
WS
460wxString wxControl::GetLabel()
461{
a152561c
WS
462 if(IsPalmField())
463 return GetFieldLabel();
464
465 // unlike other native controls, slider has no label
466 if(IsPalmControl() && !wxDynamicCast(this,wxSlider))
467 return GetControlLabel();
bdb54365
WS
468
469 return wxEmptyString;
470}
e2fc40b4 471#endif
ffecfa5a
JS
472/* static */ wxVisualAttributes
473wxControl::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
474{
475 wxVisualAttributes attrs;
476
477 // old school (i.e. not "common") controls use the standard dialog font
478 // by default
479 attrs.font = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT);
480
481 // most, or at least many, of the controls use the same colours as the
482 // buttons -- others will have to override this (and possibly simply call
483 // GetCompositeControlsDefaultAttributes() from their versions)
484 attrs.colFg = wxSystemSettings::GetColour(wxSYS_COLOUR_BTNTEXT);
485 attrs.colBg = wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE);
486
487 return attrs;
488}
489
490// another version for the "composite", i.e. non simple controls
491/* static */ wxVisualAttributes
492wxControl::GetCompositeControlsDefaultAttributes(wxWindowVariant WXUNUSED(variant))
493{
494 wxVisualAttributes attrs;
495 attrs.font = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT);
496 attrs.colFg = wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT);
497 attrs.colBg = wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW);
498
499 return attrs;
500}
501
502// ----------------------------------------------------------------------------
503// message handling
504// ----------------------------------------------------------------------------
505
506bool wxControl::ProcessCommand(wxCommandEvent& event)
507{
937013e0 508 return HandleWindowEvent(event);
ffecfa5a
JS
509}
510
ffecfa5a
JS
511void wxControl::OnEraseBackground(wxEraseEvent& event)
512{
513}
514
ffecfa5a 515#endif // wxUSE_CONTROLS