wxCheckListBox cleanup.

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@35761 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Włodzimierz Skiba
2005-09-29 10:00:16 +00:00
parent c2b77b4301
commit 84882850e5
2 changed files with 132 additions and 205 deletions

View File

@@ -1,5 +1,5 @@
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
// Name: checklst.h // Name: wx/os2/checklst.h
// Purpose: wxCheckListBox class - a listbox with checkable items // Purpose: wxCheckListBox class - a listbox with checkable items
// Note: this is an optional class. // Note: this is an optional class.
// Author: David Webster // Author: David Webster
@@ -15,13 +15,11 @@
#include <stddef.h> #include <stddef.h>
#include "wx/setup.h" #include "wx/defs.h"
class wxOwnerDrawn; // so the compiler knows, it is a class. class wxOwnerDrawn; // so the compiler knows, it is a class.
class wxCheckListBoxItem; // fwd decl, define in checklst.cpp class WXDLLEXPORT wxCheckListBox : public wxCheckListBoxBase
class WXDLLEXPORT wxCheckListBox : public wxListBox
{ {
public: public:
// //
@@ -52,10 +50,6 @@ public:
// Override base class virtuals // Override base class virtuals
// //
virtual void Delete(int n); virtual void Delete(int n);
virtual void InsertItems( int nItems
,const wxString asItems[]
,int nPos
);
virtual bool SetFont(const wxFont &rFont); virtual bool SetFont(const wxFont &rFont);
@@ -64,7 +58,7 @@ public:
// //
bool IsChecked(size_t uiIndex) const; bool IsChecked(size_t uiIndex) const;
void Check( size_t uiIndex void Check( size_t uiIndex
,bool bCheck = TRUE ,bool bCheck = true
); );
// //
@@ -80,6 +74,8 @@ protected:
virtual wxOwnerDrawn* CreateItem(size_t n); virtual wxOwnerDrawn* CreateItem(size_t n);
virtual long OS2OnMeasure(WXMEASUREITEMSTRUCT* pItem); virtual long OS2OnMeasure(WXMEASUREITEMSTRUCT* pItem);
virtual void DoInsertItems(const wxArrayString& items, int pos);
// //
// Pressing space or clicking the check box toggles the item // Pressing space or clicking the check box toggles the item
// //
@@ -91,7 +87,7 @@ private:
DECLARE_DYNAMIC_CLASS(wxCheckListBox) DECLARE_DYNAMIC_CLASS(wxCheckListBox)
DECLARE_EVENT_TABLE() DECLARE_EVENT_TABLE()
}; // end of CLASS wxCheckListBoxItem }; // end of CLASS wxCheckListBox
#endif #endif
// _WX_CHECKLST_H_ // _WX_CHECKLST_H_

View File

@@ -1,5 +1,5 @@
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
// Name: checklst.cpp // Name: src/os2/checklst.cpp
// Purpose: implementation of wxCheckListBox class // Purpose: implementation of wxCheckListBox class
// Author: David Webster // Author: David Webster
// Modified by: // Modified by:
@@ -16,7 +16,7 @@
// For compilers that support precompilation, includes "wx.h". // For compilers that support precompilation, includes "wx.h".
#include "wx/wxprec.h" #include "wx/wxprec.h"
#if wxUSE_OWNER_DRAWN #if wxUSE_CHECKLISTBOX && wxUSE_OWNER_DRAWN
#include "wx/object.h" #include "wx/object.h"
#include "wx/colour.h" #include "wx/colour.h"
@@ -28,7 +28,7 @@
#include "wx/settings.h" #include "wx/settings.h"
#include "wx/dcmemory.h" #include "wx/dcmemory.h"
#include "wx/dcscreen.h" #include "wx/dcscreen.h"
#include "wx/os2/checklst.h" #include "wx/checklst.h"
#include "wx/log.h" #include "wx/log.h"
#define INCL_PM #define INCL_PM
@@ -58,17 +58,17 @@ public:
// //
// ctor // ctor
// //
wxCheckListBoxItem( wxCheckListBox* pParent wxCheckListBoxItem( wxCheckListBox* pParent,
,size_t nIndex size_t nIndex
); );
// //
// Drawing functions // Drawing functions
// //
virtual bool OnDrawItem( wxDC& rDc virtual bool OnDrawItem( wxDC& rDc,
,const wxRect& rRect const wxRect& rRect,
,wxODAction eAct wxODAction eAct,
,wxODStatus eStat wxODStatus eStat
); );
// //
@@ -79,20 +79,18 @@ public:
void Toggle(void) { Check(!IsChecked()); } void Toggle(void) { Check(!IsChecked()); }
private: private:
bool m_bChecked; bool m_bChecked;
wxCheckListBox* m_pParent; wxCheckListBox* m_pParent;
size_t m_nIndex; size_t m_nIndex;
}; // end of CLASS wxCheckListBoxItem }; // end of CLASS wxCheckListBoxItem
wxCheckListBoxItem::wxCheckListBoxItem (
wxCheckListBox* pParent
, size_t nIndex wxCheckListBoxItem::wxCheckListBoxItem ( wxCheckListBox* pParent,
) size_t nIndex)
: wxOwnerDrawn( wxEmptyString :wxOwnerDrawn( wxEmptyString, true /* checkable */ )
,TRUE // checkable
)
{ {
m_bChecked = FALSE; m_bChecked = false;
m_pParent = pParent; m_pParent = pParent;
m_nIndex = nIndex; m_nIndex = nIndex;
@@ -104,18 +102,16 @@ wxCheckListBoxItem::wxCheckListBoxItem (
SetMarginWidth(GetDefaultMarginWidth()); SetMarginWidth(GetDefaultMarginWidth());
} // end of wxCheckListBoxItem::wxCheckListBoxItem } // end of wxCheckListBoxItem::wxCheckListBoxItem
bool wxCheckListBoxItem::OnDrawItem (
wxDC& rDc
, const wxRect& rRect
, wxODAction eAct
, wxODStatus eStat
)
{
wxRect vRect = rRect;
::WinQueryWindowRect( m_pParent->GetHWND()
,&rDc.m_vRclPaint bool wxCheckListBoxItem::OnDrawItem ( wxDC& rDc,
); const wxRect& rRect,
wxODAction eAct,
wxODStatus eStat )
{
wxRect vRect = rRect;
::WinQueryWindowRect( m_pParent->GetHWND(), &rDc.m_vRclPaint );
if (IsChecked()) if (IsChecked())
eStat = (wxOwnerDrawn::wxODStatus)(eStat | wxOwnerDrawn::wxODChecked); eStat = (wxOwnerDrawn::wxODStatus)(eStat | wxOwnerDrawn::wxODChecked);
@@ -126,39 +122,28 @@ bool wxCheckListBoxItem::OnDrawItem (
// //
vRect.x += 5; vRect.x += 5;
vRect.y -= 3; vRect.y -= 3;
if (wxOwnerDrawn::OnDrawItem( rDc if (wxOwnerDrawn::OnDrawItem( rDc, vRect, eAct, eStat))
,vRect
,eAct
,eStat))
{ {
size_t nCheckWidth = GetDefaultMarginWidth(); size_t nCheckWidth = GetDefaultMarginWidth();
size_t nCheckHeight = m_pParent->GetItemHeight(); size_t nCheckHeight = m_pParent->GetItemHeight();
int nParentHeight; int nParentHeight;
int nX = rRect.GetX(); int nX = rRect.GetX();
int nY = rRect.GetY(); int nY = rRect.GetY();
int nOldY = nY; int nOldY = nY;
wxColour vColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW)); wxColour vColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW));
wxPen vPenBack; wxPen vPenBack;
wxPen vPenGray; wxPen vPenPrev;
wxPen vPenPrev;
m_pParent->GetSize( NULL m_pParent->GetSize( NULL, &nParentHeight);
,&nParentHeight
);
nY = nParentHeight - nY - nCheckHeight; nY = nParentHeight - nY - nCheckHeight;
vPenBack = wxPen(vColour, 1, wxSOLID); vPenBack = wxPen(vColour, 1, wxSOLID);
vPenGray = wxPen(wxColour(127, 127, 127), 1, wxSOLID);
// //
// Erase the 1-pixel border // Erase the 1-pixel border
// //
rDc.SetPen(vPenBack); rDc.SetPen(vPenBack);
rDc.DrawRectangle( nX rDc.DrawRectangle( nX, nY, nCheckWidth, nCheckHeight );
,nY
,nCheckWidth
,nCheckHeight
);
// //
// Now we draw the smaller rectangle // Now we draw the smaller rectangle
@@ -170,12 +155,8 @@ bool wxCheckListBoxItem::OnDrawItem (
// //
// Draw hollow gray rectangle // Draw hollow gray rectangle
// //
rDc.SetPen(vPenGray); rDc.SetPen(*wxGREY_PEN);
rDc.DrawRectangle( nX rDc.DrawRectangle( nX, nY, nCheckWidth, nCheckHeight );
,nY
,nCheckWidth
,nCheckHeight
);
nX++; nX++;
if (IsChecked()) if (IsChecked())
@@ -183,31 +164,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 HBITMAP hChkBmp = ::WinGetSysBitmap( HWND_DESKTOP, SBMP_MENUCHECK );
,SBMP_MENUCHECK POINTL vPoint = {nX, nOldY + 3};
);
POINTL vPoint = {nX, nOldY + 3};
::WinDrawBitmap( rDc.GetHPS() ::WinDrawBitmap( rDc.GetHPS(),
,hChkBmp hChkBmp,
,NULL NULL,
,&vPoint &vPoint,
,NULL NULL,
,NULL NULL,
,DBM_NORMAL DBM_NORMAL
); );
} }
return TRUE; return true;
} }
return FALSE; return false;
} // end of wxCheckListBoxItem::OnDrawItem } // end of wxCheckListBoxItem::OnDrawItem
// //
// Change the state of the item and redraw it // Change the state of the item and redraw it
// //
void wxCheckListBoxItem::Check ( void wxCheckListBoxItem::Check ( bool bCheck )
bool bCheck
)
{ {
m_bChecked = bCheck; m_bChecked = bCheck;
@@ -219,7 +196,7 @@ 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?")); wxASSERT_MSG(nIndex != wxNOT_FOUND, wxT("what does this item do here?"));
@@ -227,15 +204,14 @@ void wxCheckListBoxItem::Check (
} }
wxCommandEvent vEvent( wxEVT_COMMAND_CHECKLISTBOX_TOGGLED wxCommandEvent vEvent( wxEVT_COMMAND_CHECKLISTBOX_TOGGLED,m_pParent->GetId());
,m_pParent->GetId()
);
vEvent.SetInt(m_nIndex); vEvent.SetInt(m_nIndex);
vEvent.SetEventObject(m_pParent); vEvent.SetEventObject(m_pParent);
m_pParent->ProcessCommand(vEvent); m_pParent->ProcessCommand(vEvent);
} // end of wxCheckListBoxItem::Check } // end of wxCheckListBoxItem::Check
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// implementation of wxCheckListBox class // implementation of wxCheckListBox class
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
@@ -243,10 +219,12 @@ void wxCheckListBoxItem::Check (
// define event table // define event table
// ------------------ // ------------------
BEGIN_EVENT_TABLE(wxCheckListBox, wxListBox) BEGIN_EVENT_TABLE(wxCheckListBox, wxListBox)
EVT_CHAR(wxCheckListBox::OnChar) EVT_CHAR(wxCheckListBox::OnChar)
EVT_LEFT_DOWN(wxCheckListBox::OnLeftClick) EVT_LEFT_DOWN(wxCheckListBox::OnLeftClick)
END_EVENT_TABLE() END_EVENT_TABLE()
// //
// Control creation // Control creation
// ---------------- // ----------------
@@ -256,119 +234,84 @@ END_EVENT_TABLE()
// Default ctor: use Create() to really create the control // Default ctor: use Create() to really create the control
// //
wxCheckListBox::wxCheckListBox() wxCheckListBox::wxCheckListBox()
: wxListBox() :wxCheckListBoxBase()
{ {
} // end of wxCheckListBox::wxCheckListBox } // end of wxCheckListBox::wxCheckListBox
// //
// Ctor which creates the associated control // Ctor which creates the associated control
// //
wxCheckListBox::wxCheckListBox ( wxCheckListBox::wxCheckListBox ( wxWindow* pParent,
wxWindow* pParent wxWindowID vId,
, wxWindowID vId const wxPoint& rPos,
, const wxPoint& rPos const wxSize& rSize,
, const wxSize& rSize int nStrings,
, int nStrings const wxString asChoices[],
, const wxString asChoices[] long lStyle,
, long lStyle const wxValidator& rVal,
, const wxValidator& rVal const wxString& rsName)
, const wxString& rsName :wxCheckListBoxBase()
)
: wxListBox()
{ {
Create( pParent Create( pParent, vId, rPos, rSize, nStrings, asChoices, lStyle | wxLB_OWNERDRAW, rVal, rsName );
,vId
,rPos
,rSize
,nStrings
,asChoices
,lStyle | wxLB_OWNERDRAW
,rVal
,rsName
);
} // end of wxCheckListBox::wxCheckListBox } // end of wxCheckListBox::wxCheckListBox
wxCheckListBox::wxCheckListBox ( wxCheckListBox::wxCheckListBox ( wxWindow* pParent,
wxWindow* pParent wxWindowID vId,
, wxWindowID vId const wxPoint& rPos,
, const wxPoint& rPos const wxSize& rSize,
, const wxSize& rSize const wxArrayString& asChoices,
, const wxArrayString& asChoices long lStyle,
, long lStyle const wxValidator& rVal,
, const wxValidator& rVal const wxString& rsName )
, const wxString& rsName :wxCheckListBoxBase()
)
: wxListBox()
{ {
wxCArrayString chs(asChoices); wxCArrayString chs(asChoices);
Create( pParent Create( pParent, vId, rPos, rSize, chs.GetCount(), chs.GetStrings(),
,vId lStyle | wxLB_OWNERDRAW, rVal, rsName );
,rPos
,rSize
,chs.GetCount()
,chs.GetStrings()
,lStyle | wxLB_OWNERDRAW
,rVal
,rsName
);
} // end of wxCheckListBox::wxCheckListBox } // end of wxCheckListBox::wxCheckListBox
void wxCheckListBox::Delete( void wxCheckListBox::Delete( int n )
int N
)
{ {
wxCHECK_RET( N >= 0 && N < m_nNumItems, wxCHECK_RET( n >= 0 && n < m_nNumItems,
wxT("invalid index in wxListBox::Delete") ); wxT("invalid index in wxCheckListBox::Delete") );
wxListBox::Delete(N); wxListBox::Delete(n);
// //
// Free memory // Free memory
// //
delete m_aItems[N]; delete m_aItems[n];
m_aItems.RemoveAt(N); m_aItems.RemoveAt(n);
} // end of wxCheckListBox::Delete } // end of wxCheckListBox::Delete
void wxCheckListBox::InsertItems ( void wxCheckListBox::DoInsertItems(const wxArrayString& items, int pos)
int nItems
, const wxString asItems[]
, int nPos
)
{ {
int i; // pos is validated in wxListBox
wxListBox::DoInsertItems( items, pos );
wxCHECK_RET( nPos >= 0 && nPos <= m_nNumItems, int n = items.GetCount();
wxT("invalid index in wxCheckListBox::InsertItems") ); for (int i = 0; i < n; i++)
wxListBox::InsertItems( nItems
,asItems
,nPos
);
for (i = 0; i < nItems; i++)
{ {
wxOwnerDrawn* pNewItem = CreateItem((size_t)(nPos + i)); wxOwnerDrawn* pNewItem = CreateItem((size_t)(pos + i));
pNewItem->SetName(asItems[i]); pNewItem->SetName(items[i]);
m_aItems.Insert(pNewItem, (size_t)(nPos + i)); m_aItems.Insert(pNewItem, (size_t)(pos + i));
::WinSendMsg( (HWND)GetHWND() ::WinSendMsg( (HWND)GetHWND(),
,LM_SETITEMHANDLE LM_SETITEMHANDLE,
,(MPARAM)(i + nPos) (MPARAM)(i + pos),
,MPFROMP(pNewItem) MPFROMP(pNewItem)
); );
} }
} // end of wxCheckListBox::InsertItems } // end of wxCheckListBox::InsertItems
bool wxCheckListBox::SetFont ( bool wxCheckListBox::SetFont ( const wxFont& rFont )
const wxFont& rFont
)
{ {
size_t i; for (size_t i = 0; i < m_aItems.GetCount(); i++)
for (i = 0; i < m_aItems.GetCount(); i++)
m_aItems[i]->SetFont(rFont); m_aItems[i]->SetFont(rFont);
wxListBox::SetFont(rFont); wxListBox::SetFont(rFont);
return TRUE; return true;
} // end of wxCheckListBox::SetFont } // end of wxCheckListBox::SetFont
// //
// Create/retrieve item // Create/retrieve item
// -------------------- // --------------------
@@ -377,29 +320,25 @@ bool wxCheckListBox::SetFont (
// //
// Create a check list box item // Create a check list box item
// //
wxOwnerDrawn* wxCheckListBox::CreateItem ( wxOwnerDrawn* wxCheckListBox::CreateItem ( size_t nIndex )
size_t nIndex
)
{ {
wxCheckListBoxItem* pItem = new wxCheckListBoxItem( this wxCheckListBoxItem* pItem = new wxCheckListBoxItem( this, nIndex );
,nIndex
);
return pItem; return pItem;
} // end of wxCheckListBox::CreateItem } // end of wxCheckListBox::CreateItem
// //
// Return item size // Return item size
// ---------------- // ----------------
// //
long wxCheckListBox::OS2OnMeasure ( long wxCheckListBox::OS2OnMeasure ( WXMEASUREITEMSTRUCT* pItem )
WXMEASUREITEMSTRUCT* pItem
)
{ {
if (!pItem) if (!pItem)
pItem = (WXMEASUREITEMSTRUCT*)new OWNERITEM; 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
@@ -415,32 +354,29 @@ long wxCheckListBox::OS2OnMeasure (
return 0L; return 0L;
} // end of wxCheckListBox::CreateItem } // end of wxCheckListBox::CreateItem
// //
// Check items // Check items
// ----------- // -----------
// //
bool wxCheckListBox::IsChecked ( bool wxCheckListBox::IsChecked ( size_t uiIndex) const
size_t uiIndex
) const
{ {
return GetItem(uiIndex)->IsChecked(); return GetItem(uiIndex)->IsChecked();
} // end of wxCheckListBox::IsChecked } // end of wxCheckListBox::IsChecked
void wxCheckListBox::Check ( void wxCheckListBox::Check ( size_t uiIndex, bool bCheck )
size_t uiIndex
, bool bCheck
)
{ {
GetItem(uiIndex)->Check(bCheck); GetItem(uiIndex)->Check(bCheck);
} // end of wxCheckListBox::Check } // end of wxCheckListBox::Check
// //
// Process events // Process events
// -------------- // --------------
// //
void wxCheckListBox::OnChar ( void wxCheckListBox::OnChar ( wxKeyEvent& rEvent )
wxKeyEvent& rEvent
)
{ {
if (rEvent.GetKeyCode() == WXK_SPACE) if (rEvent.GetKeyCode() == WXK_SPACE)
GetItem(GetSelection())->Toggle(); GetItem(GetSelection())->Toggle();
@@ -448,9 +384,7 @@ void wxCheckListBox::OnChar (
rEvent.Skip(); rEvent.Skip();
} // end of wxCheckListBox::OnChar } // end of wxCheckListBox::OnChar
void wxCheckListBox::OnLeftClick ( void wxCheckListBox::OnLeftClick ( wxMouseEvent& rEvent )
wxMouseEvent& rEvent
)
{ {
// //
// Clicking on the item selects it, clicking on the checkmark toggles // Clicking on the item selects it, clicking on the checkmark toggles
@@ -461,20 +395,18 @@ void wxCheckListBox::OnLeftClick (
wxScreenDC vDc; wxScreenDC vDc;
wxCoord vHeight; wxCoord vHeight;
GetSize( NULL GetSize( NULL, &nParentHeight );
,&nParentHeight
);
vDc.SetFont(GetFont()); vDc.SetFont(GetFont());
vHeight = (wxCoord)(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 < (size_t)m_nNumItems)
GetItem(nItem)->Toggle(); GetItem(nItem)->Toggle();
@@ -491,5 +423,4 @@ void wxCheckListBox::OnLeftClick (
} }
} // end of wxCheckListBox::OnLeftClick } // end of wxCheckListBox::OnLeftClick
#endif #endif // wxUSE_CHECKLISTBOX && wxUSE_OWNER_DRAWN