1///////////////////////////////////////////////////////////////////////////////
2// Name:        src/palmos/toplevel.cpp
3// Purpose:     implements wxTopLevelWindow for Palm OS
4// Author:      William Osborne - minimal working wxPalmOS port
5// Modified by: Wlodzimierz ABX Skiba - more than minimal functionality
6// Created:     10/13/04
7// RCS-ID:      $Id: toplevel.cpp 40943 2006-08-31 19:31:43Z ABX $
8// Copyright:   (c) William Osborne <wbo@freeshell.org>, Wlodzimierz Skiba
9// Licence:     wxWindows licence
10///////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
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#include "wx/toplevel.h"
28
29#ifndef WX_PRECOMP
30    #include "wx/app.h"
31    #include "wx/dialog.h"
32    #include "wx/string.h"
33    #include "wx/log.h"
34    #include "wx/intl.h"
35    #include "wx/frame.h"
36    #include "wx/containr.h"        // wxSetFocusToChild()
37    #include "wx/button.h"
38    #include "wx/checkbox.h"
39    #include "wx/radiobut.h"
40    #include "wx/slider.h"
41    #include "wx/module.h"
42#endif //WX_PRECOMP
43
44#include "wx/display.h"
45
46// controls for sending select event
47#include "wx/tglbtn.h"
48#include "wx/datectrl.h"
49
50#include <Window.h>
51#include <Form.h>
52
53// ----------------------------------------------------------------------------
54// globals
55// ----------------------------------------------------------------------------
56
57// the name of the default wxWidgets class
58extern const wxChar *wxCanvasClassName;
59
60// Pointer to the currently active frame for the form event handler.
61wxTopLevelWindowPalm* ActiveParentFrame;
62
63static Boolean FrameFormHandleEvent(EventType *event);
64
65// ============================================================================
66// wxTopLevelWindowPalm implementation
67// ============================================================================
68
69BEGIN_EVENT_TABLE(wxTopLevelWindowPalm, wxTopLevelWindowBase)
70    EVT_ACTIVATE(wxTopLevelWindowPalm::OnActivate)
71END_EVENT_TABLE()
72
73// ----------------------------------------------------------------------------
74// wxTopLevelWindowPalm creation
75// ----------------------------------------------------------------------------
76
77void wxTopLevelWindowPalm::Init()
78{
79}
80
81WXDWORD wxTopLevelWindowPalm::PalmGetStyle(long style, WXDWORD *exflags) const
82{
83    return 0;
84}
85
86bool wxTopLevelWindowPalm::Create(wxWindow *parent,
87                                  wxWindowID id,
88                                  const wxString& title,
89                                  const wxPoint& pos,
90                                  const wxSize& size,
91                                  long style,
92                                  const wxString& name)
93{
94    // this is a check for limitation mentioned before FrameFormHandleEvent() code
95    if(wxTopLevelWindows.GetCount()>0)
96        return false;
97
98    ActiveParentFrame=NULL;
99
100    wxTopLevelWindows.Append(this);
101
102    if ( parent )
103        parent->AddChild(this);
104
105    SetId( id == wxID_ANY ? NewControlId() : id );
106
107    WinConstraintsType constraints;
108    memset(&constraints, 0, sizeof(WinConstraintsType));
109    // position
110    constraints.x_pos = ( pos.x == wxDefaultCoord ) ? winUndefConstraint : pos.x;
111    constraints.y_pos = ( pos.y == wxDefaultCoord ) ? winUndefConstraint : pos.y;
112    // size
113    constraints.x_min = winUndefConstraint;
114    constraints.x_max = winMaxConstraint;
115    constraints.x_pref = ( size.x == wxDefaultCoord ) ? winUndefConstraint : size.x;
116    constraints.y_min = winUndefConstraint;
117    constraints.y_max = winMaxConstraint;
118    constraints.y_pref = ( size.y == wxDefaultCoord ) ? winUndefConstraint : size.y;
119
120    FrameForm = FrmNewFormWithConstraints(
121                    GetId(),
122                    title.c_str(),
123                    winFlagBackBuffer,
124                    &constraints,
125                    0,
126                    NULL,
127                    0,
128                    NULL,
129                    0
130                );
131
132    if(FrameForm==NULL)
133        return false;
134
135    FrmSetEventHandler((FormType *)FrameForm,FrameFormHandleEvent);
136
137    FrmSetActiveForm((FormType *)FrameForm);
138
139    ActiveParentFrame=this;
140
141    return true;
142}
143
144wxTopLevelWindowPalm::~wxTopLevelWindowPalm()
145{
146}
147
148// ---------------------------------------------------------------------------
149// implementation
150// ---------------------------------------------------------------------------
151
152WXWINHANDLE wxTopLevelWindowPalm::GetWinHandle() const
153{
154    FormType *form = (FormType *)GetForm();
155    if(form)
156        return FrmGetWindowHandle(form);
157    return NULL;
158}
159
160// ----------------------------------------------------------------------------
161// wxTopLevelWindowPalm showing
162// ----------------------------------------------------------------------------
163
164void wxTopLevelWindowPalm::DoShowWindow(int nShowCmd)
165{
166}
167
168bool wxTopLevelWindowPalm::Show(bool show)
169{
170    FrmDrawForm((FormType *)FrameForm);
171
172    wxPaintEvent event(m_windowId);
173    event.SetEventObject(this);
174    GetEventHandler()->ProcessEvent(event);
175
176    return true;
177}
178
179// ----------------------------------------------------------------------------
180// wxTopLevelWindowPalm maximize/minimize
181// ----------------------------------------------------------------------------
182
183void wxTopLevelWindowPalm::Maximize(bool maximize)
184{
185}
186
187bool wxTopLevelWindowPalm::IsMaximized() const
188{
189    return false;
190}
191
192void wxTopLevelWindowPalm::Iconize(bool iconize)
193{
194}
195
196bool wxTopLevelWindowPalm::IsIconized() const
197{
198    return false;
199}
200
201void wxTopLevelWindowPalm::Restore()
202{
203}
204
205void wxTopLevelWindowPalm::DoGetSize( int *width, int *height ) const
206{
207    RectangleType rect;
208    FrmGetFormBounds( (FormType *)GetForm() , &rect );
209    if(width)
210        *width = rect.extent.x;
211    if(height)
212        *height = rect.extent.y;
213}
214
215// ----------------------------------------------------------------------------
216// wxTopLevelWindowPalm fullscreen
217// ----------------------------------------------------------------------------
218
219bool wxTopLevelWindowPalm::ShowFullScreen(bool show, long style)
220{
221    return false;
222}
223
224// ----------------------------------------------------------------------------
225// wxTopLevelWindowPalm misc
226// ----------------------------------------------------------------------------
227
228void wxTopLevelWindowPalm::SetTitle( const wxString& WXUNUSED(title))
229{
230}
231
232wxString wxTopLevelWindowPalm::GetTitle() const
233{
234    return wxEmptyString;
235}
236
237void wxTopLevelWindowPalm::SetIcon(const wxIcon& icon)
238{
239}
240
241void wxTopLevelWindowPalm::SetIcons(const wxIconBundle& icons)
242{
243}
244
245bool wxTopLevelWindowPalm::EnableCloseButton(bool enable)
246{
247    return false;
248}
249
250WXFORMPTR wxTopLevelWindowPalm::GetForm() const
251{
252    return FrmGetActiveForm();
253}
254
255bool wxTopLevelWindowPalm::SetShape(const wxRegion& region)
256{
257    return false;
258}
259
260// ----------------------------------------------------------------------------
261// wxTopLevelWindow native event handling
262// ----------------------------------------------------------------------------
263
264bool wxTopLevelWindowPalm::HandleControlSelect(WXEVENTPTR event)
265{
266    const EventType *palmEvent = (EventType *)event;
267    const int id = palmEvent->data.ctlSelect.controlID;
268
269    wxWindow* win = FindWindowById(id,this);
270    if(win==NULL)
271        return false;
272
273#if wxUSE_BUTTON
274    wxButton* button = wxDynamicCast(win,wxButton);
275    if(button)
276        return button->SendClickEvent();
277#endif // wxUSE_BUTTON
278
279#if wxUSE_CHECKBOX
280    wxCheckBox* checkbox = wxDynamicCast(win,wxCheckBox);
281    if(checkbox)
282        return checkbox->SendClickEvent();
283#endif // wxUSE_CHECKBOX
284
285#if wxUSE_TOGGLEBTN
286    wxToggleButton* toggle = wxDynamicCast(win,wxToggleButton);
287    if(toggle)
288        return toggle->SendClickEvent();
289#endif // wxUSE_TOGGLEBTN
290
291#if wxUSE_RADIOBTN
292    wxRadioButton* radio = wxDynamicCast(win,wxRadioButton);
293    if(radio)
294        return radio->SendClickEvent();
295#endif // wxUSE_RADIOBTN
296
297#if wxUSE_DATEPICKCTRL
298    wxDatePickerCtrl* datepicker = wxDynamicCast(win,wxDatePickerCtrl);
299    if(datepicker)
300        return datepicker->SendClickEvent();
301#endif // wxUSE_DATEPICKCTRL
302
303#if wxUSE_SLIDER
304    wxSlider* slider = wxDynamicCast(win,wxSlider);
305    if(slider)
306        return slider->SendUpdatedEvent();
307#endif // wxUSE_SLIDER
308
309    return false;
310}
311
312bool wxTopLevelWindowPalm::HandleControlRepeat(WXEVENTPTR event)
313{
314    const EventType *palmEvent = (EventType *)event;
315    const int id = palmEvent->data.ctlRepeat.controlID;
316
317    wxWindow* win = FindWindowById(id, this);
318    if(win==NULL)
319        return false;
320
321#if wxUSE_SLIDER
322    wxSlider* slider = wxDynamicCast(win,wxSlider);
323    if(slider)
324        return slider->SendScrollEvent(event);
325#endif // wxUSE_SLIDER
326
327    return false;
328}
329
330bool wxTopLevelWindowPalm::HandleSize(WXEVENTPTR event)
331{
332    const EventType *palmEvent = (EventType *)event;
333    wxSize newSize(palmEvent->data.winResized.newBounds.extent.x,
334                   palmEvent->data.winResized.newBounds.extent.y);
335    wxSizeEvent eventWx(newSize,GetId());
336    eventWx.SetEventObject(this);
337    return GetEventHandler()->ProcessEvent(eventWx);
338}
339
340void wxTopLevelWindowPalm::OnActivate(wxActivateEvent& event)
341{
342}
343
344/* Palm OS Event handler for the window
345 *
346 * This function *must* be located outside of the wxTopLevelWindow class because
347 * the Palm OS API expects a standalone C function as a callback.  You cannot
348 * pass a pointer to a member function of a C++ class as a callback because the
349 * prototypes don't match.  (A member function has a hidden "this" pointer as
350 * its first parameter).
351 *
352 * This event handler uses a global pointer to the current wxFrame to process
353 * the events generated by the Palm OS form API.  I know this is ugly, but right
354 * now I can't think of any other way to deal with this problem.  If someone
355 * finds a better solution, please let me know.  My email address is
356 * wbo@freeshell.org
357 */
358static Boolean FrameFormHandleEvent(EventType *event)
359{
360    // frame and tlw point to the same object but they are for convenience
361    // of calling proper structure withiout later dynamic typcasting
362    wxFrame* frame = wxDynamicCast(ActiveParentFrame,wxFrame);
363    wxTopLevelWindowPalm* tlw = ActiveParentFrame;
364    Boolean     handled = false;
365
366    switch (event->eType) {
367        case ctlSelectEvent:
368            handled = tlw->HandleControlSelect(event);
369            break;
370        case ctlRepeatEvent:
371            handled = tlw->HandleControlRepeat(event);
372            break;
373        case winResizedEvent:
374            handled = tlw->HandleSize(event);
375            break;
376#if wxUSE_MENUS_NATIVE
377        case menuOpenEvent:
378            handled = frame->HandleMenuOpen();
379            break;
380        case menuEvent:
381            handled = frame->HandleMenuSelect(event);
382            break;
383#endif
384        default:
385            break;
386    }
387
388    return handled;
389}
390