]> git.saurik.com Git - wxWidgets.git/blobdiff - src/os2/checklst.cpp
fixed memory leak in RestoreState
[wxWidgets.git] / src / os2 / checklst.cpp
index ec5776444d2965b20cd9c670ab8a2c6cf2866492..c32fc84787a593b1cb7709dcd65b4607e41d69a4 100644 (file)
@@ -1,5 +1,5 @@
 ///////////////////////////////////////////////////////////////////////////////
 ///////////////////////////////////////////////////////////////////////////////
-// Name:        checklst.cpp
+// Name:        src/os2/checklst.cpp
 // Purpose:     implementation of wxCheckListBox class
 // Author:      David Webster
 // Modified by:
 // Purpose:     implementation of wxCheckListBox class
 // Author:      David Webster
 // Modified by:
 // For compilers that support precompilation, includes "wx.h".
 #include "wx/wxprec.h"
 
 // For compilers that support precompilation, includes "wx.h".
 #include "wx/wxprec.h"
 
-#if wxUSE_OWNER_DRAWN
+#if wxUSE_CHECKLISTBOX && wxUSE_OWNER_DRAWN
+
+#include "wx/checklst.h"
+
+#ifndef WX_PRECOMP
+    #include "wx/object.h"
+    #include "wx/log.h"
+    #include "wx/window.h"
+    #include "wx/dcmemory.h"
+    #include "wx/dcscreen.h"
+    #include "wx/settings.h"
+    #include "wx/listbox.h"
+    #include "wx/bitmap.h"
+    #include "wx/colour.h"
+    #include "wx/font.h"
+#endif
 
 
-#include "wx/object.h"
-#include "wx/colour.h"
-#include "wx/font.h"
-#include "wx/bitmap.h"
-#include "wx/window.h"
-#include "wx/listbox.h"
 #include "wx/ownerdrw.h"
 #include "wx/ownerdrw.h"
-#include "wx/settings.h"
-#include "wx/dcmemory.h"
-#include "wx/dcscreen.h"
-#include "wx/os2/checklst.h"
-#include "wx/log.h"
 
 #define INCL_PM
 #include <os2.h>
 
 #define INCL_PM
 #include <os2.h>
@@ -58,17 +62,15 @@ public:
     //
     // ctor
     //
     //
     // ctor
     //
-    wxCheckListBoxItem( wxCheckListBox* pParent
-                       ,size_t          nIndex
-                      );
+    wxCheckListBoxItem(wxCheckListBox* pParent, size_t nIndex);
 
     //
     // Drawing functions
     //
 
     //
     // Drawing functions
     //
-    virtual bool OnDrawItem( wxDC&         rDc
-                            ,const wxRect& rRect
-                            ,wxODAction    eAct
-                            ,wxODStatus    eStat
+    virtual bool OnDrawItem( wxDC&         rDc,
+                             const wxRect& rRect,
+                             wxODAction    eAct,
+                             wxODStatus    eStat
                            );
 
     //
                            );
 
     //
@@ -79,20 +81,17 @@ public:
     void Toggle(void) { Check(!IsChecked()); }
 
 private:
     void Toggle(void) { Check(!IsChecked()); }
 
 private:
-    bool                            m_bChecked;
-    wxCheckListBox*                 m_pParent;
-    size_t                          m_nIndex;
+    bool            m_bChecked;
+    wxCheckListBox* m_pParent;
+    size_t          m_nIndex;
 }; // end of CLASS wxCheckListBoxItem
 
 }; // end of CLASS wxCheckListBoxItem
 
-wxCheckListBoxItem::wxCheckListBoxItem (
-  wxCheckListBox*                   pParent
-, size_t                            nIndex
-)
-: wxOwnerDrawn( ""
-               ,TRUE // checkable
-              )
+
+
+wxCheckListBoxItem::wxCheckListBoxItem(wxCheckListBox* pParent, size_t nIndex)
+                   :wxOwnerDrawn( wxEmptyString, true /* checkable */ )
 {
 {
-    m_bChecked = FALSE;
+    m_bChecked = false;
     m_pParent  = pParent;
     m_nIndex   = nIndex;
 
     m_pParent  = pParent;
     m_nIndex   = nIndex;
 
@@ -104,61 +103,48 @@ wxCheckListBoxItem::wxCheckListBoxItem (
     SetMarginWidth(GetDefaultMarginWidth());
 } // end of wxCheckListBoxItem::wxCheckListBoxItem
 
     SetMarginWidth(GetDefaultMarginWidth());
 } // end of wxCheckListBoxItem::wxCheckListBoxItem
 
-bool wxCheckListBoxItem::OnDrawItem (
-  wxDC&                             rDc
-, const wxRect&                     rRect
-, wxODAction                        eAct
-, wxODStatus                        eStat
-)
+
+
+bool wxCheckListBoxItem::OnDrawItem ( wxDC& rDc,
+                                      const wxRect& rRect,
+                                      wxODAction eAct,
+                                      wxODStatus eStat )
 {
 {
-    wxRect                          vRect = rRect;
+    wxRect vRect = rRect;
 
 
-    ::WinQueryWindowRect( m_pParent->GetHWND()
-                         ,&rDc.m_vRclPaint
-                        );
+    ::WinQueryWindowRect( m_pParent->GetHWND(), &rDc.m_vRclPaint );
     if (IsChecked())
         eStat = (wxOwnerDrawn::wxODStatus)(eStat | wxOwnerDrawn::wxODChecked);
 
     //
     // Unfortunately PM doesn't quite get the text position exact.  We need to alter
     // it down and to the right, just a little bit.  The coords in rRect are OS/2
     if (IsChecked())
         eStat = (wxOwnerDrawn::wxODStatus)(eStat | wxOwnerDrawn::wxODChecked);
 
     //
     // Unfortunately PM doesn't quite get the text position exact.  We need to alter
     // it down and to the right, just a little bit.  The coords in rRect are OS/2
-    // coords not wxWindows coords.
+    // coords not wxWidgets coords.
     //
     vRect.x += 5;
     vRect.y -= 3;
     //
     vRect.x += 5;
     vRect.y -= 3;
-    if (wxOwnerDrawn::OnDrawItem( rDc
-                                 ,vRect
-                                 ,eAct
-                                 ,eStat))
+    if (wxOwnerDrawn::OnDrawItem( rDc, vRect, eAct, eStat))
     {
     {
-        size_t                      nCheckWidth  = GetDefaultMarginWidth();
-        size_t                      nCheckHeight = m_pParent->GetItemHeight();
-        int                         nParentHeight;
-        int                         nX = rRect.GetX();
-        int                         nY = rRect.GetY();
-        int                         nOldY = nY;
-        wxColour                    vColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW));
-        wxPen                       vPenBack;
-        wxPen                       vPenGray;
-        wxPen                       vPenPrev;
-
-        m_pParent->GetSize( NULL
-                           ,&nParentHeight
-                          );
+        size_t    nCheckWidth  = GetDefaultMarginWidth();
+        size_t    nCheckHeight = m_pParent->GetItemHeight();
+        int       nParentHeight;
+        int       nX = rRect.GetX();
+        int       nY = rRect.GetY();
+        int       nOldY = nY;
+        wxColour  vColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW));
+        wxPen     vPenBack;
+        wxPen     vPenPrev;
+
+        m_pParent->GetSize( NULL, &nParentHeight);
 
         nY = nParentHeight - nY - nCheckHeight;
         vPenBack = wxPen(vColour, 1, wxSOLID);
 
         nY = nParentHeight - nY - nCheckHeight;
         vPenBack = wxPen(vColour, 1, wxSOLID);
-        vPenGray = wxPen(wxColour(127, 127, 127), 1, wxSOLID);
 
         //
         // Erase the 1-pixel border
         //
         rDc.SetPen(vPenBack);
 
         //
         // Erase the 1-pixel border
         //
         rDc.SetPen(vPenBack);
-        rDc.DrawRectangle( nX
-                          ,nY
-                          ,nCheckWidth
-                          ,nCheckHeight
-                         );
+        rDc.DrawRectangle( nX, nY, nCheckWidth, nCheckHeight );
 
         //
         // Now we draw the smaller rectangle
 
         //
         // Now we draw the smaller rectangle
@@ -170,12 +156,8 @@ bool wxCheckListBoxItem::OnDrawItem (
         //
         // Draw hollow gray rectangle
         //
         //
         // Draw hollow gray rectangle
         //
-        rDc.SetPen(vPenGray);
-        rDc.DrawRectangle( nX
-                          ,nY
-                          ,nCheckWidth
-                          ,nCheckHeight
-                         );
+        rDc.SetPen(*wxGREY_PEN);
+        rDc.DrawRectangle( nX, nY, nCheckWidth, nCheckHeight );
 
         nX++;
         if (IsChecked())
 
         nX++;
         if (IsChecked())
@@ -183,31 +165,27 @@ bool wxCheckListBoxItem::OnDrawItem (
             //
             // Draw the check by loading the sys standard bitmap and drawing it
             //
             //
             // Draw the check by loading the sys standard bitmap and drawing it
             //
-            HBITMAP                 hChkBmp = ::WinGetSysBitmap( HWND_DESKTOP
-                                                                ,SBMP_MENUCHECK
-                                                               );
-            POINTL                  vPoint = {nX, nOldY + 3};
-
-            ::WinDrawBitmap( rDc.GetHPS()
-                            ,hChkBmp
-                            ,NULL
-                            ,&vPoint
-                            ,NULL
-                            ,NULL
-                            ,DBM_NORMAL
+            HBITMAP hChkBmp = ::WinGetSysBitmap( HWND_DESKTOP, SBMP_MENUCHECK );
+            POINTL  vPoint = {nX, nOldY + 3};
+
+            ::WinDrawBitmap( rDc.GetHPS(),
+                             hChkBmp,
+                             NULL,
+                             &vPoint,
+                             NULL,
+                             NULL,
+                             DBM_NORMAL
                            );
         }
                            );
         }
-        return TRUE;
+        return true;
     }
     }
-    return FALSE;
+    return false;
 } // end of wxCheckListBoxItem::OnDrawItem
 
 //
 // Change the state of the item and redraw it
 //
 } // end of wxCheckListBoxItem::OnDrawItem
 
 //
 // Change the state of the item and redraw it
 //
-void wxCheckListBoxItem::Check (
-  bool                              bCheck
-)
+void wxCheckListBoxItem::Check( bool bCheck )
 {
     m_bChecked = bCheck;
 
 {
     m_bChecked = bCheck;
 
@@ -219,26 +197,22 @@ void wxCheckListBoxItem::Check (
         //
         // Update it
         //
         //
         // Update it
         //
-        int                         nIndex = m_pParent->GetItemIndex(this);
+        int nIndex = m_pParent->GetItemIndex(this);
 
         wxASSERT_MSG(nIndex != wxNOT_FOUND, wxT("what does this item do here?"));
 
         m_nIndex = (size_t)nIndex;
     }
 
 
         wxASSERT_MSG(nIndex != wxNOT_FOUND, wxT("what does this item do here?"));
 
         m_nIndex = (size_t)nIndex;
     }
 
-    HWND                            hWndListbox = (HWND)m_pParent->GetHWND();
-    RECTL                           rUpdate;
-    MRESULT                         mRc;
 
 
-    wxCommandEvent                  vEvent( wxEVT_COMMAND_CHECKLISTBOX_TOGGLED
-                                           ,m_pParent->GetId()
-                                          );
+    wxCommandEvent vEvent( wxEVT_COMMAND_CHECKLISTBOX_TOGGLED,m_pParent->GetId());
 
     vEvent.SetInt(m_nIndex);
     vEvent.SetEventObject(m_pParent);
     m_pParent->ProcessCommand(vEvent);
 } // end of wxCheckListBoxItem::Check
 
 
     vEvent.SetInt(m_nIndex);
     vEvent.SetEventObject(m_pParent);
     m_pParent->ProcessCommand(vEvent);
 } // end of wxCheckListBoxItem::Check
 
+
 // ----------------------------------------------------------------------------
 // implementation of wxCheckListBox class
 // ----------------------------------------------------------------------------
 // ----------------------------------------------------------------------------
 // implementation of wxCheckListBox class
 // ----------------------------------------------------------------------------
@@ -246,10 +220,12 @@ void wxCheckListBoxItem::Check (
 // define event table
 // ------------------
 BEGIN_EVENT_TABLE(wxCheckListBox, wxListBox)
 // define event table
 // ------------------
 BEGIN_EVENT_TABLE(wxCheckListBox, wxListBox)
-  EVT_CHAR(wxCheckListBox::OnChar)
-  EVT_LEFT_DOWN(wxCheckListBox::OnLeftClick)
+    EVT_CHAR(wxCheckListBox::OnChar)
+    EVT_LEFT_DOWN(wxCheckListBox::OnLeftClick)
 END_EVENT_TABLE()
 
 END_EVENT_TABLE()
 
+
+
 //
 // Control creation
 // ----------------
 //
 // Control creation
 // ----------------
@@ -259,94 +235,88 @@ END_EVENT_TABLE()
 // Default ctor: use Create() to really create the control
 //
 wxCheckListBox::wxCheckListBox()
 // Default ctor: use Create() to really create the control
 //
 wxCheckListBox::wxCheckListBox()
-: wxListBox()
+               :wxCheckListBoxBase()
 {
 } // end of wxCheckListBox::wxCheckListBox
 
 //
 // Ctor which creates the associated control
 //
 {
 } // end of wxCheckListBox::wxCheckListBox
 
 //
 // Ctor which creates the associated control
 //
-wxCheckListBox::wxCheckListBox (
-  wxWindow*                         pParent
-, wxWindowID                        vId
-, const wxPoint&                    rPos
-, const wxSize&                     rSize
-, int                               nStrings
-, const wxString                    asChoices[]
-, long                              lStyle
-, const wxValidator&                rVal
-, const wxString&                   rsName
-)
-              : wxListBox()
+wxCheckListBox::wxCheckListBox ( wxWindow* pParent,
+                                 wxWindowID vId,
+                                 const wxPoint& rPos,
+                                 const wxSize& rSize,
+                                 int nStrings,
+                                 const wxString asChoices[],
+                                 long lStyle,
+                                 const wxValidator& rVal,
+                                 const wxString& rsName)
+               :wxCheckListBoxBase()
+{
+    Create( pParent, vId, rPos, rSize, nStrings, asChoices, lStyle | wxLB_OWNERDRAW, rVal, rsName );
+} // end of wxCheckListBox::wxCheckListBox
+
+wxCheckListBox::wxCheckListBox ( wxWindow* pParent,
+                                 wxWindowID vId,
+                                 const wxPoint& rPos,
+                                 const wxSize& rSize,
+                                 const wxArrayString& asChoices,
+                                 long lStyle,
+                                 const wxValidator& rVal,
+                                 const wxString& rsName )
+               :wxCheckListBoxBase()
 {
 {
-    Create( pParent
-           ,vId
-           ,rPos
-           ,rSize
-           ,nStrings
-           ,asChoices
-           ,lStyle | wxLB_OWNERDRAW
-           ,rVal
-           ,rsName
-          );
+    wxCArrayString chs(asChoices);
+    Create( pParent, vId, rPos, rSize, chs.GetCount(), chs.GetStrings(),
+            lStyle | wxLB_OWNERDRAW, rVal, rsName );
 } // end of wxCheckListBox::wxCheckListBox
 
 } // end of wxCheckListBox::wxCheckListBox
 
-void wxCheckListBox::Delete(
-  int                               N
-)
+void wxCheckListBox::Delete(unsigned int n)
 {
 {
-    wxCHECK_RET( N >= 0 && N < m_nNumItems,
-                 wxT("invalid index in wxListBox::Delete") );
-    wxListBox::Delete(N);
+    wxCHECK_RET( IsValid(n),
+                 wxT("invalid index in wxCheckListBox::Delete") );
+    wxListBox::Delete(n);
 
     //
     // Free memory
     //
 
     //
     // Free memory
     //
-    delete m_aItems[N];
-    m_aItems.RemoveAt(N);
+    delete m_aItems[n];
+    m_aItems.RemoveAt(n);
 } // end of wxCheckListBox::Delete
 
 } // end of wxCheckListBox::Delete
 
-void wxCheckListBox::InsertItems (
-  int                               nItems
-, const wxString                    asItems[]
-, int                               nPos
-)
+int wxCheckListBox::DoInsertItems(const wxArrayStringsAdapter& items,
+                                  unsigned int pos,
+                                  void **clientData,
+                                  wxClientDataType type)
 {
 {
-    int                             i;
-
-    wxCHECK_RET( nPos >= 0 && nPos <= m_nNumItems,
-                 wxT("invalid index in wxCheckListBox::InsertItems") );
-
-    wxListBox::InsertItems( nItems
-                           ,asItems
-                           ,nPos
-                          );
-    for (i = 0; i < nItems; i++)
+    // pos is validated in wxListBox
+    int result = wxListBox::DoInsertItems( items, pos, clientData, type );
+    unsigned int n = items.GetCount();
+    for (unsigned int i = 0; i < n; i++)
     {
     {
-        wxOwnerDrawn*               pNewItem = CreateItem((size_t)(nPos + i));
-
-        pNewItem->SetName(asItems[i]);
-        m_aItems.Insert(pNewItem, (size_t)(nPos + i));
-        ::WinSendMsg( (HWND)GetHWND()
-                     ,LM_SETITEMHANDLE
-                     ,(MPARAM)(i + nPos)
-                     ,MPFROMP(pNewItem)
+        wxOwnerDrawn* pNewItem = CreateItem((size_t)(pos + i));
+
+        pNewItem->SetName(items[i]);
+        m_aItems.Insert(pNewItem, (size_t)(pos + i));
+        ::WinSendMsg( (HWND)GetHWND(),
+                      LM_SETITEMHANDLE,
+                      (MPARAM)(i + pos),
+                      MPFROMP(pNewItem)
                     );
     }
                     );
     }
-} // end of wxCheckListBox::InsertItems
+    return result;
+} // end of wxCheckListBox::DoInsertItems
 
 
-bool wxCheckListBox::SetFont (
-  const wxFont&                     rFont
-)
+bool wxCheckListBox::SetFont ( const wxFont& rFont )
 {
 {
-    size_t                          i;
-
-    for (i = 0; i < m_aItems.GetCount(); i++)
+    for (unsigned int i = 0; i < m_aItems.GetCount(); i++)
         m_aItems[i]->SetFont(rFont);
     wxListBox::SetFont(rFont);
         m_aItems[i]->SetFont(rFont);
     wxListBox::SetFont(rFont);
-    return TRUE;
+    return true;
 } // end of wxCheckListBox::SetFont
 
 } // end of wxCheckListBox::SetFont
 
+
+
 //
 // Create/retrieve item
 // --------------------
 //
 // Create/retrieve item
 // --------------------
@@ -355,29 +325,25 @@ bool wxCheckListBox::SetFont (
 //
 // Create a check list box item
 //
 //
 // Create a check list box item
 //
-wxOwnerDrawn* wxCheckListBox::CreateItem (
-  size_t                            nIndex
-)
+wxOwnerDrawn* wxCheckListBox::CreateItem(size_t nIndex)
 {
 {
-    wxCheckListBoxItem*             pItem = new wxCheckListBoxItem( this
-                                                                   ,nIndex
-                                                                  );
+    wxCheckListBoxItem* pItem = new wxCheckListBoxItem( this, nIndex );
     return pItem;
 } // end of wxCheckListBox::CreateItem
 
     return pItem;
 } // end of wxCheckListBox::CreateItem
 
+
+
 //
 // Return item size
 // ----------------
 //
 //
 // Return item size
 // ----------------
 //
-long wxCheckListBox::OS2OnMeasure (
-  WXMEASUREITEMSTRUCT*             pItem
-)
+long wxCheckListBox::OS2OnMeasure ( WXMEASUREITEMSTRUCT* pItem )
 {
     if (!pItem)
         pItem = (WXMEASUREITEMSTRUCT*)new OWNERITEM;
 {
     if (!pItem)
         pItem = (WXMEASUREITEMSTRUCT*)new OWNERITEM;
-    if (wxListBox::OS2OnMeasure(pItem) )
+    if (wxListBox::OS2OnMeasure(pItem))
     {
     {
-        POWNERITEM                  pStruct = (POWNERITEM)pItem;
+        POWNERITEM pStruct = (POWNERITEM)pItem;
 
         //
         // Save item height
 
         //
         // Save item height
@@ -393,42 +359,37 @@ long wxCheckListBox::OS2OnMeasure (
     return 0L;
 } // end of wxCheckListBox::CreateItem
 
     return 0L;
 } // end of wxCheckListBox::CreateItem
 
+
+
 //
 // Check items
 // -----------
 //
 //
 // Check items
 // -----------
 //
-bool wxCheckListBox::IsChecked (
-  size_t                            uiIndex
-) const
+bool wxCheckListBox::IsChecked(unsigned int uiIndex) const
 {
     return GetItem(uiIndex)->IsChecked();
 } // end of wxCheckListBox::IsChecked
 
 {
     return GetItem(uiIndex)->IsChecked();
 } // end of wxCheckListBox::IsChecked
 
-void wxCheckListBox::Check (
-  size_t                            uiIndex
-, bool                              bCheck
-)
+void wxCheckListBox::Check(unsigned int uiIndex, bool bCheck)
 {
     GetItem(uiIndex)->Check(bCheck);
 } // end of wxCheckListBox::Check
 
 {
     GetItem(uiIndex)->Check(bCheck);
 } // end of wxCheckListBox::Check
 
+
+
 //
 // Process events
 // --------------
 //
 //
 // Process events
 // --------------
 //
-void wxCheckListBox::OnChar (
-  wxKeyEvent&                       rEvent
-)
+void wxCheckListBox::OnChar ( wxKeyEvent& rEvent )
 {
 {
-    if (rEvent.KeyCode() == WXK_SPACE)
+    if (rEvent.GetKeyCode() == WXK_SPACE)
         GetItem(GetSelection())->Toggle();
     else
         rEvent.Skip();
 } // end of wxCheckListBox::OnChar
 
         GetItem(GetSelection())->Toggle();
     else
         rEvent.Skip();
 } // end of wxCheckListBox::OnChar
 
-void wxCheckListBox::OnLeftClick (
-  wxMouseEvent&                     rEvent
-)
+void wxCheckListBox::OnLeftClick ( wxMouseEvent& rEvent )
 {
     //
     // Clicking on the item selects it, clicking on the checkmark toggles
 {
     //
     // Clicking on the item selects it, clicking on the checkmark toggles
@@ -439,22 +400,20 @@ void wxCheckListBox::OnLeftClick (
         wxScreenDC                  vDc;
         wxCoord                     vHeight;
 
         wxScreenDC                  vDc;
         wxCoord                     vHeight;
 
-        GetSize( NULL
-                ,&nParentHeight
-               );
+        GetSize( NULL, &nParentHeight );
         vDc.SetFont(GetFont());
         vDc.SetFont(GetFont());
-        vHeight = vDc.GetCharHeight() * 2.5;
+        vHeight = (wxCoord)(vDc.GetCharHeight() * 2.5);
 
         //
         // This, of course, will not work if the LB is scrolled
         //
 
         //
         // This, of course, will not work if the LB is scrolled
         //
-        int                         nY = rEvent.GetY();
+        int nY = rEvent.GetY();
 
         nY = nParentHeight - (nY + vHeight);
 
 
         nY = nParentHeight - (nY + vHeight);
 
-        size_t                      nItem = (size_t)(nY / vHeight);
+        size_t nItem = (size_t)(nY / vHeight);
 
 
-        if (nItem < (size_t)m_nNumItems)
+        if (nItem < m_nNumItems)
             GetItem(nItem)->Toggle();
         //
         // else: it's not an error, just click outside of client zone
             GetItem(nItem)->Toggle();
         //
         // else: it's not an error, just click outside of client zone
@@ -469,5 +428,4 @@ void wxCheckListBox::OnLeftClick (
     }
 } // end of wxCheckListBox::OnLeftClick
 
     }
 } // end of wxCheckListBox::OnLeftClick
 
-#endif
-
+#endif // wxUSE_CHECKLISTBOX && wxUSE_OWNER_DRAWN