fixed multiple bugs in multiple selection wxCheckListBoxes

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@15393 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Vadim Zeitlin
2002-05-05 23:09:37 +00:00
parent 98dd66cf4f
commit d90879fa39
4 changed files with 125 additions and 40 deletions

View File

@@ -190,6 +190,7 @@ wxMSW:
- Multiline labels in buttons are now supoprted (simply use "\n" in the label) - Multiline labels in buttons are now supoprted (simply use "\n" in the label)
- Implemented wxMouseCaptureChangedEvent and made wxGenericDragImage check it - Implemented wxMouseCaptureChangedEvent and made wxGenericDragImage check it
has the capture before release it. has the capture before release it.
- fixed bugs in multiple selection wxCheckListBox
wxGTK: wxGTK:

View File

@@ -16,19 +16,15 @@
#pragma interface "checklst.h" #pragma interface "checklst.h"
#endif #endif
#include "wx/setup.h"
#if !wxUSE_OWNER_DRAWN #if !wxUSE_OWNER_DRAWN
#error "wxCheckListBox class requires owner-drawn functionality." #error "wxCheckListBox class requires owner-drawn functionality."
#endif #endif
#include "wx/listbox.h" class WXDLLEXPORT wxOwnerDrawn;
class WXDLLEXPORT wxCheckListBoxItem; // fwd decl, defined in checklst.cpp
class wxCheckListBoxItem; // fwd decl, defined in checklst.cpp class WXDLLEXPORT wxCheckListBox : public wxCheckListBoxBase
class WXDLLEXPORT wxCheckListBox : public wxListBox
{ {
DECLARE_DYNAMIC_CLASS(wxCheckListBox)
public: public:
// ctors // ctors
wxCheckListBox(); wxCheckListBox();
@@ -47,8 +43,8 @@ public:
virtual bool SetFont( const wxFont &font ); virtual bool SetFont( const wxFont &font );
// items may be checked // items may be checked
bool IsChecked(size_t uiIndex) const; virtual bool IsChecked(size_t uiIndex) const;
void Check(size_t uiIndex, bool bCheck = TRUE); virtual void Check(size_t uiIndex, bool bCheck = TRUE);
// accessors // accessors
size_t GetItemHeight() const { return m_nItemHeight; } size_t GetItemHeight() const { return m_nItemHeight; }
@@ -60,13 +56,14 @@ protected:
virtual bool MSWOnMeasure(WXMEASUREITEMSTRUCT *item); virtual bool MSWOnMeasure(WXMEASUREITEMSTRUCT *item);
// pressing space or clicking the check box toggles the item // pressing space or clicking the check box toggles the item
void OnChar(wxKeyEvent& event); void OnKeyDown(wxKeyEvent& event);
void OnLeftClick(wxMouseEvent& event); void OnLeftClick(wxMouseEvent& event);
private: private:
size_t m_nItemHeight; // height of checklistbox items (the same for all) size_t m_nItemHeight; // height of checklistbox items (the same for all)
DECLARE_EVENT_TABLE() DECLARE_EVENT_TABLE()
DECLARE_DYNAMIC_CLASS(wxCheckListBox)
}; };
#endif //_CHECKLST_H #endif //_CHECKLST_H

View File

@@ -30,17 +30,22 @@
#if wxUSE_OWNER_DRAWN #if wxUSE_OWNER_DRAWN
#ifndef WX_PRECOMP
#include "wx/object.h" #include "wx/object.h"
#include "wx/colour.h" #include "wx/colour.h"
#include "wx/font.h" #include "wx/font.h"
#include "wx/bitmap.h" #include "wx/bitmap.h"
#include "wx/window.h" #include "wx/window.h"
#include "wx/listbox.h" #include "wx/listbox.h"
#include "wx/ownerdrw.h"
#include "wx/settings.h"
#include "wx/dcmemory.h" #include "wx/dcmemory.h"
#include "wx/msw/checklst.h"
#include "wx/settings.h"
#include "wx/log.h" #include "wx/log.h"
#endif
#include "wx/ownerdrw.h"
#include "wx/checklst.h"
#include <windows.h> #include <windows.h>
#include <windowsx.h> #include <windowsx.h>
@@ -262,7 +267,7 @@ void wxCheckListBoxItem::Check(bool check)
// define event table // define event table
// ------------------ // ------------------
BEGIN_EVENT_TABLE(wxCheckListBox, wxListBox) BEGIN_EVENT_TABLE(wxCheckListBox, wxListBox)
EVT_CHAR(wxCheckListBox::OnChar) EVT_KEY_DOWN(wxCheckListBox::OnKeyDown)
EVT_LEFT_DOWN(wxCheckListBox::OnLeftClick) EVT_LEFT_DOWN(wxCheckListBox::OnLeftClick)
END_EVENT_TABLE() END_EVENT_TABLE()
@@ -270,7 +275,7 @@ END_EVENT_TABLE()
// ---------------- // ----------------
// def ctor: use Create() to really create the control // def ctor: use Create() to really create the control
wxCheckListBox::wxCheckListBox() : wxListBox() wxCheckListBox::wxCheckListBox()
{ {
} }
@@ -280,7 +285,6 @@ wxCheckListBox::wxCheckListBox(wxWindow *parent, wxWindowID id,
int nStrings, const wxString choices[], int nStrings, const wxString choices[],
long style, const wxValidator& val, long style, const wxValidator& val,
const wxString& name) const wxString& name)
: wxListBox()
{ {
Create(parent, id, pos, size, nStrings, choices, Create(parent, id, pos, size, nStrings, choices,
style | wxLB_OWNERDRAW, val, name); style | wxLB_OWNERDRAW, val, name);
@@ -344,24 +348,96 @@ bool wxCheckListBox::MSWOnMeasure(WXMEASUREITEMSTRUCT *item)
bool wxCheckListBox::IsChecked(size_t uiIndex) const bool wxCheckListBox::IsChecked(size_t uiIndex) const
{ {
wxCHECK_MSG( uiIndex < (size_t)GetCount(), FALSE, _T("bad wxCheckListBox index") );
return GetItem(uiIndex)->IsChecked(); return GetItem(uiIndex)->IsChecked();
} }
void wxCheckListBox::Check(size_t uiIndex, bool bCheck) void wxCheckListBox::Check(size_t uiIndex, bool bCheck)
{ {
wxCHECK_RET( uiIndex < (size_t)GetCount(), _T("bad wxCheckListBox index") );
GetItem(uiIndex)->Check(bCheck); GetItem(uiIndex)->Check(bCheck);
} }
// process events // process events
// -------------- // --------------
void wxCheckListBox::OnChar(wxKeyEvent& event) void wxCheckListBox::OnKeyDown(wxKeyEvent& event)
{ {
if ( event.KeyCode() == WXK_SPACE ) // what do we do?
GetItem(GetSelection())->Toggle(); enum
{
None,
Toggle,
Set,
Clear
} oper;
switch ( event.KeyCode() )
{
case WXK_SPACE:
oper = Toggle;
break;
case WXK_NUMPAD_ADD:
case '+':
oper = Set;
break;
case WXK_NUMPAD_SUBTRACT:
case '-':
oper = Clear;
break;
default:
oper = None;
}
if ( oper != None )
{
wxArrayInt selections;
int count;
if ( HasMultipleSelection() )
{
count = GetSelections(selections);
}
else else
{
count = 1;
selections.Add(GetSelection());
}
for ( int i = 0; i < count; i++ )
{
wxCheckListBoxItem *item = GetItem(selections[i]);
if ( !item )
{
wxFAIL_MSG( _T("no wxCheckListBoxItem?") );
continue;
}
switch ( oper )
{
case Toggle:
item->Toggle();
break;
case Set:
case Clear:
item->Check( oper == Set );
break;
default:
wxFAIL_MSG( _T("what should this key do?") );
}
}
}
else // nothing to do
{
event.Skip(); event.Skip();
} }
}
void wxCheckListBox::OnLeftClick(wxMouseEvent& event) void wxCheckListBox::OnLeftClick(wxMouseEvent& event)
{ {

View File

@@ -453,21 +453,32 @@ int wxListBox::GetSelections(wxArrayInt& aSelections) const
if ( HasMultipleSelection() ) if ( HasMultipleSelection() )
{ {
int no_sel = ListBox_GetSelCount(GetHwnd()); int countSel = ListBox_GetSelCount(GetHwnd());
if (no_sel != 0) { if ( countSel == LB_ERR )
int *selections = new int[no_sel]; {
int rc = ListBox_GetSelItems(GetHwnd(), no_sel, selections); wxLogDebug(_T("ListBox_GetSelCount failed"));
}
else if ( countSel != 0 )
{
int *selections = new int[countSel];
wxCHECK_MSG(rc != LB_ERR, -1, wxT("ListBox_GetSelItems failed")); if ( ListBox_GetSelItems(GetHwnd(),
countSel, selections) == LB_ERR )
aSelections.Alloc(no_sel); {
for ( int n = 0; n < no_sel; n++ ) wxLogDebug(wxT("ListBox_GetSelItems failed"));
countSel = -1;
}
else
{
aSelections.Alloc(countSel);
for ( int n = 0; n < countSel; n++ )
aSelections.Add(selections[n]); aSelections.Add(selections[n]);
}
delete [] selections; delete [] selections;
} }
return no_sel; return countSel;
} }
else // single-selection listbox else // single-selection listbox
{ {