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