]> git.saurik.com Git - wxWidgets.git/blobdiff - src/palmos/control.cpp
I think I found out how GTK+ want popup windows
[wxWidgets.git] / src / palmos / control.cpp
index c4360886e7d9753eb0ad9ad76ccb891a7a2ea655..b9bb06863ef0a285d20efdf46b205731a3dbc4b7 100644 (file)
@@ -2,10 +2,10 @@
 // Name:        src/palmos/control.cpp
 // Purpose:     wxControl class
 // Author:      William Osborne - minimal working wxPalmOS port
-// Modified by:
+// Modified by: Wlodzimierz ABX Skiba - native implementation
 // Created:     10/13/04
 // RCS-ID:      $Id$
-// Copyright:   (c) William Osborne
+// Copyright:   (c) William Osborne, Wlodzimierz Skiba
 // Licence:     wxWindows licence
 /////////////////////////////////////////////////////////////////////////////
 
 #endif
 
 #include "wx/control.h"
+#include "wx/toplevel.h"
+#include "wx/button.h"
+#include "wx/checkbox.h"
+#include "wx/tglbtn.h"
+#include "wx/radiobut.h"
+#include "wx/slider.h"
 
 // ----------------------------------------------------------------------------
 // wxWin macros
@@ -58,9 +64,25 @@ END_EVENT_TABLE()
 // wxControl ctor/dtor
 // ----------------------------------------------------------------------------
 
+void wxControl::Init()
+{
+    m_palmControl = false;
+    m_palmField = false;
+}
+
 wxControl::~wxControl()
 {
+    SetLabel(wxEmptyString);
     m_isBeingDeleted = true;
+
+    DestroyChildren();
+
+    uint16_t index;
+    FormType* form = GetObjectFormIndex(index);
+    if(form!=NULL && index!=frmInvalidObjectId)
+    {
+        FrmRemoveObject((FormType **)&form,index);
+    }
 }
 
 // ----------------------------------------------------------------------------
@@ -86,33 +108,78 @@ bool wxControl::Create(wxWindow *parent,
 }
 
 bool wxControl::PalmCreateControl(ControlStyleType style,
-                                  wxWindow *parent,
-                                  wxWindowID id,
                                   const wxString& label,
                                   const wxPoint& pos,
-                                  const wxSize& size)
-{
-    FormType* form = FrmGetActiveForm ();
-    m_control = CtlNewControl (
-                    (void **)&form,
-                    id,
-                    style,
-                    label.c_str(),
-                    pos.x,
-                    pos.y,
-                    size.x,
-                    size.y,
-                    boldFont,
-                    0,
-                    false
-                );
-
-    if(m_control==NULL)
+                                  const wxSize& size,
+                                  int groupID)
+{
+    FormType* form = GetParentForm();
+    if(form==NULL)
+        return false;
+
+    ControlType *control = CtlNewControl(
+                               (void **)&form,
+                               GetId(),
+                               style,
+                               wxEmptyString,
+                               ( pos.x == wxDefaultCoord ) ? winUndefConstraint : pos.x,
+                               ( pos.y == wxDefaultCoord ) ? winUndefConstraint : pos.y,
+                               ( size.x == wxDefaultCoord ) ? winUndefConstraint : size.x,
+                               ( size.y == wxDefaultCoord ) ? winUndefConstraint : size.y,
+                               stdFont,
+                               groupID,
+                               true
+                           );
+
+    if(control==NULL)
         return false;
 
-    form = FrmGetActiveForm ();
-    m_objectIndex = FrmGetObjectIndex(form, id);
+    m_palmControl = true;
+
+    SetLabel(label);
+    Show();
+    return true;
+}
+
+bool wxControl::PalmCreateField(const wxString& label,
+                                const wxPoint& pos,
+                                const wxSize& size,
+                                bool editable,
+                                bool underlined,
+                                JustificationType justification)
+{
+    FormType* form = GetParentForm();
+    if(form==NULL)
+        return false;
+
+    m_label = label;
+
+    FieldType *field = FldNewField(
+                           (void **)&form,
+                           GetId(),
+                           ( pos.x == wxDefaultCoord ) ? winUndefConstraint : pos.x,
+                           ( pos.y == wxDefaultCoord ) ? winUndefConstraint : pos.y,
+                           ( size.x == wxDefaultCoord ) ? winUndefConstraint : size.x,
+                           ( size.y == wxDefaultCoord ) ? winUndefConstraint : size.y,
+                           stdFont,
+                           10,
+                           editable,
+                           underlined,
+                           false,
+                           false,
+                           justification,
+                           false,
+                           false,
+                           false
+                       );
+
+    if(field==NULL)
+        return false;
+
+    m_palmField = true;
+
     Show();
+    SetLabel(label);
     return true;
 }
 
@@ -120,6 +187,37 @@ bool wxControl::PalmCreateControl(ControlStyleType style,
 // various accessors
 // ----------------------------------------------------------------------------
 
+FormType* wxControl::GetParentForm() const
+{
+    wxWindow* parentTLW = GetParent();
+    while ( parentTLW && !parentTLW->IsTopLevel() )
+    {
+        parentTLW = parentTLW->GetParent();
+    }
+    wxTopLevelWindowPalm* tlw = wxDynamicCast(parentTLW, wxTopLevelWindowPalm);
+    if(!tlw)
+        return NULL;
+    return tlw->GetForm();
+}
+
+FormType* wxControl::GetObjectFormIndex(uint16_t& index) const
+{
+    FormType* form = GetParentForm();
+    if(form!=NULL)
+        index = FrmGetObjectIndex(form, GetId());
+    else
+        index = frmInvalidObjectId;
+    return form;
+}
+
+void* wxControl::GetObjectPtr() const
+{
+    uint16_t index;
+    FormType* form = GetObjectFormIndex(index);
+    if(form==NULL || index==frmInvalidObjectId)return NULL;
+    return FrmGetObjectPtr(form,index);
+}
+
 wxBorder wxControl::GetDefaultBorder() const
 {
     // we want to automatically give controls a sunken style (confusingly,
@@ -128,26 +226,86 @@ wxBorder wxControl::GetDefaultBorder() const
     return wxBORDER_SUNKEN;
 }
 
+void wxControl::SetIntValue(int val)
+{
+    FormType* form = GetParentForm();
+    if(form==NULL)
+        return;
+    uint16_t index = FrmGetObjectIndex(form, GetId());
+    if(index==frmInvalidObjectId)
+        return;
+    FrmSetControlValue(form, index, val);
+}
+
+void wxControl::SetBoolValue(bool val)
+{
+    SetIntValue(val?1:0);
+}
+
+bool wxControl::GetBoolValue() const
+{
+    FormType* form = GetParentForm();
+    if(form==NULL)
+        return false;
+    uint16_t index = FrmGetObjectIndex(form, GetId());
+    if(index==frmInvalidObjectId)
+        return false;
+    return ( FrmGetControlValue(form, index) == 1 );
+}
+
 wxSize wxControl::DoGetBestSize() const
 {
     return wxSize(16, 16);
 }
 
+void wxControl::DoGetBounds( RectangleType &rect ) const
+{
+    FormType* form = GetParentForm();
+    if(form==NULL)
+        return;
+    uint16_t index = FrmGetObjectIndex(form,GetId());
+    if(index==frmInvalidObjectId)
+        return;
+    FrmGetObjectBounds(form,index,&rect);
+}
+
+void wxControl::DoGetPosition( int *x, int *y ) const
+{
+    RectangleType rect;
+    DoGetBounds(rect);
+    if(x)
+        *x = rect.topLeft.x;
+    if(y)
+        *y = rect.topLeft.y;
+}
+
+void wxControl::DoGetSize( int *width, int *height ) const
+{
+    RectangleType rect;
+    DoGetBounds(rect);
+    if(width)
+        *width = rect.extent.x;
+    if(height)
+        *height = rect.extent.y;
+}
+
 bool wxControl::Enable(bool enable)
 {
-    if( m_control == NULL )
+    ControlType *control = (ControlType *)GetObjectPtr();
+    if( (IsPalmControl()) || (control == NULL))
         return false;
-    if( IsEnabled() == enable)
+    if( CtlEnabled(control) == enable)
         return false;
-    CtlSetEnabled( m_control, enable);
+    CtlSetEnabled( control, enable);
     return true;
 }
 
 bool wxControl::IsEnabled() const
 {
-    if( m_control == NULL )
+    ControlType *control = (ControlType *)GetObjectPtr();
+    if( (IsPalmControl()) || (control == NULL))
         return false;
-    return CtlEnabled(m_control);
+    return CtlEnabled(control);
 }
 
 bool wxControl::IsShown() const
@@ -157,13 +315,99 @@ bool wxControl::IsShown() const
 
 bool wxControl::Show( bool show )
 {
+    FormType* form = GetParentForm();
+    if(form==NULL)
+        return false;
+    uint16_t index = FrmGetObjectIndex(form,GetId());
+    if(index==frmInvalidObjectId)
+        return false;
     if(show)
-        FrmShowObject(FrmGetActiveForm(), m_objectIndex);
+        FrmShowObject(form,index);
     else
-        FrmHideObject(FrmGetActiveForm(), m_objectIndex);
+        FrmHideObject(form,index);
     return true;
 }
 
+void wxControl::SetFieldLabel(const wxString& label)
+{
+    FieldType* field = (FieldType*)GetObjectPtr();
+    if(field==NULL)
+        return;
+
+    uint16_t newSize = label.Length() + 1;
+    MemHandle strHandle = FldGetTextHandle(field);
+    FldSetTextHandle(field, NULL );
+    if (strHandle)
+    {
+        if(MemHandleResize(strHandle, newSize)!=errNone)
+            strHandle = 0;
+    }
+    else
+    {
+        strHandle = MemHandleNew( newSize );
+    }
+    if(!strHandle)
+        return;
+
+    char* str = (char*) MemHandleLock( strHandle );
+    if(str==NULL)
+        return;
+
+    strcpy(str, label.c_str());
+    MemHandleUnlock(strHandle);
+    FldSetTextHandle(field, strHandle);
+    FldRecalculateField(field, true);
+}
+
+void wxControl::SetControlLabel(const wxString& label)
+{
+    ControlType* control = (ControlType*)GetObjectPtr();
+    if(control==NULL)
+        return;
+    CtlSetLabel(control,wxEmptyString);
+    m_label = label;
+    if(!m_label.empty())
+        CtlSetLabel(control,m_label.c_str());
+}
+
+void wxControl::SetLabel(const wxString& label)
+{
+    if(IsPalmField())
+        SetFieldLabel(label);
+
+    // unlike other native controls, slider has no label
+    if(IsPalmControl() && !wxDynamicCast(this,wxSlider))
+        SetControlLabel(label);
+}
+
+wxString wxControl::GetFieldLabel()
+{
+    FieldType* field = (FieldType*)GetObjectPtr();
+    if(field==NULL)
+        return wxEmptyString;
+    return FldGetTextPtr(field);
+}
+
+wxString wxControl::GetControlLabel()
+{
+    ControlType* control = (ControlType*)GetObjectPtr();
+    if(control==NULL)
+        return wxEmptyString;
+    return CtlGetLabel(control);
+}
+
+wxString wxControl::GetLabel()
+{
+    if(IsPalmField())
+        return GetFieldLabel();
+
+    // unlike other native controls, slider has no label
+    if(IsPalmControl() && !wxDynamicCast(this,wxSlider))
+        return GetControlLabel();
+
+    return wxEmptyString;
+}
+
 /* static */ wxVisualAttributes
 wxControl::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
 {
@@ -208,15 +452,9 @@ void wxControl::OnEraseBackground(wxEraseEvent& event)
 }
 
 WXHBRUSH wxControl::OnCtlColor(WXHDC pDC, WXHWND WXUNUSED(pWnd), WXUINT WXUNUSED(nCtlColor),
-#if wxUSE_CTL3D
-                               WXUINT message,
-                               WXWPARAM wParam,
-                               WXLPARAM lParam
-#else
                                WXUINT WXUNUSED(message),
                                WXWPARAM WXUNUSED(wParam),
                                WXLPARAM WXUNUSED(lParam)
-#endif
     )
 {
     return (WXHBRUSH)0;