]> git.saurik.com Git - wxWidgets.git/blobdiff - src/os2/listbox.cpp
simplify code so it always returns the same object
[wxWidgets.git] / src / os2 / listbox.cpp
index 7d226adb3b4c5255dd01968276cd20bb650eadfd..6fa76670e767fc08ecbb158f645d60f6c621ce1a 100644 (file)
@@ -29,6 +29,7 @@
     #include "wx/window.h"
 #endif
 
     #include "wx/window.h"
 #endif
 
+#include "wx/os2/dcclient.h"
 #include "wx/os2/private.h"
 
 #define INCL_M
 #include "wx/os2/private.h"
 
 #define INCL_M
@@ -38,8 +39,6 @@
     #include  "wx/ownerdrw.h"
 #endif
 
     #include  "wx/ownerdrw.h"
 #endif
 
-IMPLEMENT_DYNAMIC_CLASS(wxListBox, wxControlWithItems)
-
 // ============================================================================
 // list box item declaration and implementation
 // ============================================================================
 // ============================================================================
 // list box item declaration and implementation
 // ============================================================================
@@ -49,25 +48,25 @@ IMPLEMENT_DYNAMIC_CLASS(wxListBox, wxControlWithItems)
 class wxListBoxItem : public wxOwnerDrawn
 {
 public:
 class wxListBoxItem : public wxOwnerDrawn
 {
 public:
-    wxListBoxItem(const wxString& rsStr = wxEmptyString);
-};
+    wxListBoxItem(wxListBox *parent)
+        { m_parent = parent; }
 
 
-wxListBoxItem::wxListBoxItem(
-  const wxString& rsStr
-)
-: wxOwnerDrawn( rsStr
-               ,false
-              )
-{
-    //
-    // No bitmaps/checkmarks
-    //
-    SetMarginWidth(0);
-} // end of wxListBoxItem::wxListBoxItem
+    wxListBox *GetParent() const
+        { return m_parent; }
+
+    int GetIndex() const
+        { return m_parent->GetItemIndex(const_cast<wxListBoxItem*>(this)); }
+
+    wxString GetName() const
+        { return m_parent->GetString(GetIndex()); }
+
+private:
+    wxListBox *m_parent;
+};
 
 wxOwnerDrawn* wxListBox::CreateItem( size_t WXUNUSED(n) )
 {
 
 wxOwnerDrawn* wxListBox::CreateItem( size_t WXUNUSED(n) )
 {
-    return new wxListBoxItem();
+    return new wxListBoxItem(this);
 } // end of wxListBox::CreateItem
 
 #endif  //USE_OWNER_DRAWN
 } // end of wxListBox::CreateItem
 
 #endif  //USE_OWNER_DRAWN
@@ -224,14 +223,7 @@ bool wxListBox::Create( wxWindow* pParent,
 
 wxListBox::~wxListBox()
 {
 
 wxListBox::~wxListBox()
 {
-#if wxUSE_OWNER_DRAWN
-    size_t lUiCount = m_aItems.Count();
-
-    while (lUiCount-- != 0)
-    {
-        delete m_aItems[lUiCount];
-    }
-#endif // wxUSE_OWNER_DRAWN
+    Clear();
 } // end of wxListBox::~wxListBox
 
 void wxListBox::SetupColours()
 } // end of wxListBox::~wxListBox
 
 void wxListBox::SetupColours()
@@ -260,7 +252,7 @@ void wxListBox::DoDeleteOneItem(unsigned int n)
 #if wxUSE_OWNER_DRAWN
     delete m_aItems[n];
     m_aItems.RemoveAt(n);
 #if wxUSE_OWNER_DRAWN
     delete m_aItems[n];
     m_aItems.RemoveAt(n);
-#endif // wxUSE_OWNER_DRAWN/!wxUSE_OWNER_DRAWN
+#endif // wxUSE_OWNER_DRAWN
 
     ::WinSendMsg(GetHwnd(), LM_DELETEITEM, (MPARAM)n, (MPARAM)0);
     m_nNumItems--;
 
     ::WinSendMsg(GetHwnd(), LM_DELETEITEM, (MPARAM)n, (MPARAM)0);
     m_nNumItems--;
@@ -271,7 +263,6 @@ int wxListBox::DoInsertItems(const wxArrayStringsAdapter & items,
                              void **clientData,
                              wxClientDataType type)
 {
                              void **clientData,
                              wxClientDataType type)
 {
-    long lIndex = 0;
     LONG lIndexType = 0;
     bool incrementPos = false;
 
     LONG lIndexType = 0;
     bool incrementPos = false;
 
@@ -293,7 +284,7 @@ int wxListBox::DoInsertItems(const wxArrayStringsAdapter & items,
         n = (int)::WinSendMsg(GetHwnd(), LM_INSERTITEM, (MPARAM)lIndexType, (MPARAM)items[i].wx_str());
         if (n < 0)
         {
         n = (int)::WinSendMsg(GetHwnd(), LM_INSERTITEM, (MPARAM)lIndexType, (MPARAM)items[i].wx_str());
         if (n < 0)
         {
-            wxLogLastError(_T("WinSendMsg(LM_INSERTITEM)"));
+            wxLogLastError(wxT("WinSendMsg(LM_INSERTITEM)"));
             n = wxNOT_FOUND;
             break;
         }
             n = wxNOT_FOUND;
             break;
         }
@@ -302,13 +293,9 @@ int wxListBox::DoInsertItems(const wxArrayStringsAdapter & items,
 #if wxUSE_OWNER_DRAWN
         if (HasFlag(wxLB_OWNERDRAW))
         {
 #if wxUSE_OWNER_DRAWN
         if (HasFlag(wxLB_OWNERDRAW))
         {
-            wxOwnerDrawn*               pNewItem = CreateItem(n); // dummy argument
-            wxScreenDC                  vDc; // FIXME: is it really needed here?
-    
-            pNewItem->SetName(items[i]);
-            m_aItems.Insert(pNewItem, n);
-            ::WinSendMsg(GetHwnd(), LM_SETITEMHANDLE, (MPARAM)n, MPFROMP(pNewItem));
+            wxOwnerDrawn* pNewItem = CreateItem(n); // dummy argument
             pNewItem->SetFont(GetFont());
             pNewItem->SetFont(GetFont());
+            m_aItems.Insert(pNewItem, n);
         }
 #endif
         AssignNewItemClientData(n, clientData, i, type);
         }
 #endif
         AssignNewItemClientData(n, clientData, i, type);
@@ -323,14 +310,10 @@ int wxListBox::DoInsertItems(const wxArrayStringsAdapter & items,
 void wxListBox::DoClear()
 {
 #if wxUSE_OWNER_DRAWN
 void wxListBox::DoClear()
 {
 #if wxUSE_OWNER_DRAWN
-    unsigned int lUiCount = m_aItems.Count();
-
-    while (lUiCount-- != 0)
+    if ( m_windowStyle & wxLB_OWNERDRAW )
     {
     {
-        delete m_aItems[lUiCount];
+        WX_CLEAR_ARRAY(m_aItems);
     }
     }
-
-    m_aItems.Clear();
 #endif // wxUSE_OWNER_DRAWN
     ::WinSendMsg(GetHwnd(), LM_DELETEALL, (MPARAM)0, (MPARAM)0);
 
 #endif // wxUSE_OWNER_DRAWN
     ::WinSendMsg(GetHwnd(), LM_DELETEALL, (MPARAM)0, (MPARAM)0);
 
@@ -384,17 +367,6 @@ void wxListBox::DoSetItemClientData(unsigned int n, void* pClientData)
     wxCHECK_RET( IsValid(n),
                  wxT("invalid index in wxListBox::SetClientData") );
 
     wxCHECK_RET( IsValid(n),
                  wxT("invalid index in wxListBox::SetClientData") );
 
-#if wxUSE_OWNER_DRAWN
-    if ( m_windowStyle & wxLB_OWNERDRAW )
-    {
-        //
-        // Client data must be pointer to wxOwnerDrawn, otherwise we would crash
-        // in OnMeasure/OnDraw.
-        //
-        wxFAIL_MSG(wxT("Can't use client data with owner-drawn listboxes"));
-    }
-#endif // wxUSE_OWNER_DRAWN
-
     ::WinSendMsg(GetHwnd(), LM_SETITEMHANDLE, MPFROMLONG(n), MPFROMP(pClientData));
 } // end of wxListBox::DoSetItemClientData
 
     ::WinSendMsg(GetHwnd(), LM_SETITEMHANDLE, MPFROMLONG(n), MPFROMP(pClientData));
 } // end of wxListBox::DoSetItemClientData
 
@@ -548,14 +520,6 @@ void wxListBox::SetString(unsigned int n, const wxString& rsString)
     //
     if (bWasSelected)
         Select(n);
     //
     if (bWasSelected)
         Select(n);
-
-#if wxUSE_OWNER_DRAWN
-    if (m_windowStyle & wxLB_OWNERDRAW)
-        //
-        // Update item's text
-        //
-        m_aItems[n]->SetName(rsString);
-#endif  //USE_OWNER_DRAWN
 } // end of wxListBox::SetString
 
 unsigned int wxListBox::GetCount() const
 } // end of wxListBox::SetString
 
 unsigned int wxListBox::GetCount() const
@@ -660,7 +624,7 @@ bool wxListBox::OS2Command(
         n = -1;
     }
     vEvent.SetInt(n);
         n = -1;
     }
     vEvent.SetInt(n);
-    return GetEventHandler()->ProcessEvent(vEvent);
+    return HandleWindowEvent(vEvent);
 } // end of wxListBox::OS2Command
 
 // ----------------------------------------------------------------------------
 } // end of wxListBox::OS2Command
 
 // ----------------------------------------------------------------------------
@@ -713,7 +677,6 @@ bool wxListBox::OS2OnDraw (
 )
 {
     POWNERITEM                      pDrawStruct = (POWNERITEM)pItem;
 )
 {
     POWNERITEM                      pDrawStruct = (POWNERITEM)pItem;
-    LONG                            lItemID = pDrawStruct->idItem;
     int                             eAction = 0;
     int                             eStatus = 0;
 
     int                             eAction = 0;
     int                             eStatus = 0;
 
@@ -726,24 +689,18 @@ bool wxListBox::OS2OnDraw (
     //
     // The item may be -1 for an empty listbox
     //
     //
     // The item may be -1 for an empty listbox
     //
-    if (lItemID == -1L)
+    if (pDrawStruct->idItem == -1L)
         return false;
 
         return false;
 
-    wxListBoxItem*                   pData = (wxListBoxItem*)PVOIDFROMMR( ::WinSendMsg( GetHwnd()
-                                                                                       ,LM_QUERYITEMHANDLE
-                                                                                       ,MPFROMLONG(pDrawStruct->idItem)
-                                                                                       ,(MPARAM)0
-                                                                                      )
-                                                                        );
-
-    wxCHECK(pData, false );
+    wxListBoxItem* pData = (wxListBoxItem*)m_aItems[pDrawStruct->idItem];
 
 
-    wxDC    vDc;
+    wxClientDC    vDc(this);
+    wxPMDCImpl *impl = (wxPMDCImpl*) vDc.GetImpl();
     wxPoint pt1( pDrawStruct->rclItem.xLeft, pDrawStruct->rclItem.yTop );
     wxPoint pt2( pDrawStruct->rclItem.xRight, pDrawStruct->rclItem.yBottom );
     wxRect  vRect( pt1, pt2 );
 
     wxPoint pt1( pDrawStruct->rclItem.xLeft, pDrawStruct->rclItem.yTop );
     wxPoint pt2( pDrawStruct->rclItem.xRight, pDrawStruct->rclItem.yBottom );
     wxRect  vRect( pt1, pt2 );
 
-    vDc.SetHPS(pDrawStruct->hps);
+    impl->SetHPS(pDrawStruct->hps);
 
     if (pDrawStruct->fsAttribute == pDrawStruct->fsAttributeOld)
     {
 
     if (pDrawStruct->fsAttribute == pDrawStruct->fsAttributeOld)
     {
@@ -811,7 +768,7 @@ bool wxListBox::OS2OnDraw (
     return pData->OnDrawItem( vDc
                              ,vRect
                              ,(wxOwnerDrawn::wxODAction)eAction
     return pData->OnDrawItem( vDc
                              ,vRect
                              ,(wxOwnerDrawn::wxODAction)eAction
-                             ,(wxOwnerDrawn::wxODStatus)eStatus
+                             ,(wxOwnerDrawn::wxODStatus)(eStatus | wxOwnerDrawn::wxODHidePrefix)
                             );
 } // end of wxListBox::OS2OnDraw
 
                             );
 } // end of wxListBox::OS2OnDraw