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