Skip to content

Commit

Permalink
Add demonstration of wxRearrangeList to widgets sample
Browse files Browse the repository at this point in the history
Add wxRearrangeList as another option on wxListBox page.
  • Loading branch information
a-wi committed Apr 28, 2017
1 parent 7e1b64a commit 88b1699
Showing 1 changed file with 107 additions and 16 deletions.
123 changes: 107 additions & 16 deletions samples/widgets/listbox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
#include "wx/sizer.h"

#include "wx/checklst.h"
#include "wx/rearrangectrl.h"

#include "itemcontainer.h"
#include "widgets.h"
Expand Down Expand Up @@ -71,7 +72,9 @@ enum
ListboxPage_EnsureVisibleText,
ListboxPage_ContainerTests,
ListboxPage_GetTopItem,
ListboxPage_GetCountPerPage
ListboxPage_GetCountPerPage,
ListboxPage_MoveUp,
ListboxPage_MoveDown,
};

// ----------------------------------------------------------------------------
Expand Down Expand Up @@ -103,6 +106,8 @@ class ListboxWidgetsPage : public ItemContainerWidgetsPage
void OnButtonAddMany(wxCommandEvent& event);
void OnButtonTopItem(wxCommandEvent& event);
void OnButtonPageCount(wxCommandEvent& event);
void OnButtonMoveUp(wxCommandEvent& evt);
void OnButtonMoveDown(wxCommandEvent& evt);

void OnListbox(wxCommandEvent& event);
void OnListboxDClick(wxCommandEvent& event);
Expand All @@ -116,6 +121,7 @@ class ListboxWidgetsPage : public ItemContainerWidgetsPage
void OnUpdateUIDeleteButton(wxUpdateUIEvent& event);
void OnUpdateUIDeleteSelButton(wxUpdateUIEvent& event);
void OnUpdateUIResetButton(wxUpdateUIEvent& event);
void OnUpdateUIMoveButtons(wxUpdateUIEvent& evt);

// reset the listbox parameters
void Reset();
Expand All @@ -139,6 +145,14 @@ class ListboxWidgetsPage : public ItemContainerWidgetsPage
LboxSel_Multiple
} m_lboxSelMode;

// the list type
enum LboxType
{
LboxType_ListBox,
LboxType_CheckListBox,
LboxType_RearrangeList
};

// should it be sorted?
bool m_sorted;

Expand All @@ -152,10 +166,12 @@ class ListboxWidgetsPage : public ItemContainerWidgetsPage
// the sel mode radiobox
wxRadioBox *m_radioSelMode;

// List type selection radiobox
wxRadioBox *m_radioListType;

// the checkboxes
wxCheckBox *m_chkVScroll,
*m_chkHScroll,
*m_chkCheck,
*m_chkSort,
*m_chkOwnerDraw;

Expand Down Expand Up @@ -192,6 +208,8 @@ wxBEGIN_EVENT_TABLE(ListboxWidgetsPage, WidgetsPage)
EVT_BUTTON(ListboxPage_ContainerTests, ItemContainerWidgetsPage::OnButtonTestItemContainer)
EVT_BUTTON(ListboxPage_GetTopItem, ListboxWidgetsPage::OnButtonTopItem)
EVT_BUTTON(ListboxPage_GetCountPerPage, ListboxWidgetsPage::OnButtonPageCount)
EVT_BUTTON(ListboxPage_MoveUp, ListboxWidgetsPage::OnButtonMoveUp)
EVT_BUTTON(ListboxPage_MoveDown, ListboxWidgetsPage::OnButtonMoveDown)

EVT_TEXT_ENTER(ListboxPage_AddText, ListboxWidgetsPage::OnButtonAdd)
EVT_TEXT_ENTER(ListboxPage_DeleteText, ListboxWidgetsPage::OnButtonDelete)
Expand All @@ -206,6 +224,7 @@ wxBEGIN_EVENT_TABLE(ListboxWidgetsPage, WidgetsPage)
EVT_UPDATE_UI(ListboxPage_ChangeText, ListboxWidgetsPage::OnUpdateUIDeleteSelButton)
EVT_UPDATE_UI(ListboxPage_DeleteSel, ListboxWidgetsPage::OnUpdateUIDeleteSelButton)
EVT_UPDATE_UI(ListboxPage_EnsureVisible, ListboxWidgetsPage::OnUpdateUIEnsureVisibleButton)
EVT_UPDATE_UI_RANGE(ListboxPage_MoveUp, ListboxPage_MoveDown, ListboxWidgetsPage::OnUpdateUIMoveButtons)

EVT_LISTBOX(ListboxPage_Listbox, ListboxWidgetsPage::OnListbox)
EVT_LISTBOX_DCLICK(ListboxPage_Listbox, ListboxWidgetsPage::OnListboxDClick)
Expand Down Expand Up @@ -235,10 +254,10 @@ ListboxWidgetsPage::ListboxWidgetsPage(WidgetsBookCtrl *book,
{
// init everything
m_radioSelMode = (wxRadioBox *)NULL;
m_radioListType = (wxRadioBox*)NULL;

m_chkVScroll =
m_chkHScroll =
m_chkCheck =
m_chkSort =
m_chkOwnerDraw = (wxCheckBox *)NULL;

Expand Down Expand Up @@ -274,6 +293,21 @@ void ListboxWidgetsPage::CreateContent()
WXSIZEOF(modes), modes,
1, wxRA_SPECIFY_COLS);

static const wxString listTypes[] =
{
wxS("list box")
#if wxUSE_CHECKLISTBOX
, wxS("check list box")
#endif // wxUSE_CHECKLISTBOX
#if wxUSE_REARRANGECTRL
, wxS("rearrange list")
#endif // wxUSE_REARRANGECTRL
};
m_radioListType = new wxRadioBox(this, wxID_ANY, wxS("&List type:"),
wxDefaultPosition, wxDefaultSize,
WXSIZEOF(listTypes), listTypes,
1, wxRA_SPECIFY_COLS);

m_chkVScroll = CreateCheckBoxAndAddToSizer
(
sizerLeft,
Expand All @@ -284,13 +318,15 @@ void ListboxWidgetsPage::CreateContent()
sizerLeft,
wxT("Show &horizontal scrollbar")
);
m_chkCheck = CreateCheckBoxAndAddToSizer(sizerLeft, wxT("&Check list box"));
m_chkSort = CreateCheckBoxAndAddToSizer(sizerLeft, wxT("&Sort items"));
m_chkOwnerDraw = CreateCheckBoxAndAddToSizer(sizerLeft, wxT("&Owner drawn"));

sizerLeft->Add(5, 5, 0, wxGROW | wxALL, 5); // spacer
sizerLeft->Add(m_radioSelMode, 0, wxGROW | wxALL, 5);

sizerLeft->Add(5, 5, 0, wxGROW | wxALL, 5); // spacer
sizerLeft->Add(m_radioListType, 0, wxGROW | wxALL, 5);

wxButton *btn = new wxButton(this, ListboxPage_Reset, wxT("&Reset"));
sizerLeft->Add(btn, 0, wxALIGN_CENTRE_HORIZONTAL | wxALL, 15);

Expand Down Expand Up @@ -339,6 +375,12 @@ void ListboxWidgetsPage::CreateContent()
btn = new wxButton(this, ListboxPage_Clear, wxT("&Clear"));
sizerMiddle->Add(btn, 0, wxALL | wxGROW, 5);

btn = new wxButton(this, ListboxPage_MoveUp, wxT("Move item &up"));
sizerMiddle->Add(btn, 0, wxALL | wxGROW, 5);

btn = new wxButton(this, ListboxPage_MoveDown, wxT("Move item &down"));
sizerMiddle->Add(btn, 0, wxALL | wxGROW, 5);

btn = new wxButton(this, ListboxPage_GetTopItem, wxT("Get top item"));
sizerMiddle->Add(btn, 0, wxALL | wxGROW, 5);

Expand Down Expand Up @@ -376,9 +418,9 @@ void ListboxWidgetsPage::CreateContent()
void ListboxWidgetsPage::Reset()
{
m_radioSelMode->SetSelection(LboxSel_Single);
m_radioListType->SetSelection(LboxType_ListBox);
m_chkVScroll->SetValue(false);
m_chkHScroll->SetValue(true);
m_chkCheck->SetValue(false);
m_chkSort->SetValue(false);
m_chkOwnerDraw->SetValue(false);
}
Expand Down Expand Up @@ -406,6 +448,7 @@ void ListboxWidgetsPage::CreateLbox()
flags |= wxLB_OWNERDRAW;

wxArrayString items;
wxArrayInt order;
if ( m_lbox )
{
int count = m_lbox->GetCount();
Expand All @@ -414,28 +457,55 @@ void ListboxWidgetsPage::CreateLbox()
items.Add(m_lbox->GetString(n));
}

order.reserve(count);
#if wxUSE_CHECKLISTBOX
wxCheckListBox* cblist = wxDynamicCast(m_lbox, wxCheckListBox);
if ( cblist )
{
for ( int n = 0; n < count; n++ )
order.Add(cblist->IsChecked(n) ? n : ~n);
}
else
#endif // wxUSE_CHECKLISTBOX
{
for( int n = 0; n < count; n++ )
order.Add(~n);
}

m_sizerLbox->Detach( m_lbox );
delete m_lbox;
}

#if wxUSE_CHECKLISTBOX
if ( m_chkCheck->GetValue() )
switch( m_radioListType->GetSelection() )
{
m_lbox = new wxCheckListBox(this, ListboxPage_Listbox,
#if wxUSE_CHECKLISTBOX
case LboxType_CheckListBox:
m_lbox = new wxCheckListBox(this, ListboxPage_Listbox,
wxDefaultPosition, wxDefaultSize,
0, NULL,
items,
flags);
}
else // just a listbox
#endif
{
m_lbox = new wxListBox(this, ListboxPage_Listbox,
{
wxCheckListBox* cblist = wxDynamicCast(m_lbox, wxCheckListBox);
for ( size_t n = 0; n < order.size(); n++ )
cblist->Check(n, order[n] >= 0);
}
break;
#endif // wxUSE_CHECKLISTBOX
#if wxUSE_REARRANGECTRL
case LboxType_RearrangeList:
m_lbox = new wxRearrangeList(this, ListboxPage_Listbox,
wxDefaultPosition, wxDefaultSize,
order, items,
flags);
break;
#endif // wxUSE_REARRANGECTRL
default:
m_lbox = new wxListBox(this, ListboxPage_Listbox,
wxDefaultPosition, wxDefaultSize,
0, NULL,
items,
flags);
}

m_lbox->Set(items);
m_sizerLbox->Add(m_lbox, 1, wxGROW | wxALL, 5);
m_sizerLbox->Layout();
}
Expand Down Expand Up @@ -570,6 +640,22 @@ void ListboxWidgetsPage::OnButtonAddSeveral(wxCommandEvent& WXUNUSED(event))
m_lbox->InsertItems(items, 0);
}

void ListboxWidgetsPage::OnButtonMoveUp(wxCommandEvent& WXUNUSED(evt))
{
#if wxUSE_REARRANGECTRL
wxRearrangeList* list = wxDynamicCast(m_lbox, wxRearrangeList);
list->MoveCurrentUp();
#endif // wxUSE_REARRANGECTRL
}

void ListboxWidgetsPage::OnButtonMoveDown(wxCommandEvent& WXUNUSED(evt))
{
#if wxUSE_REARRANGECTRL
wxRearrangeList* list = wxDynamicCast(m_lbox, wxRearrangeList);
list->MoveCurrentDown();
#endif // wxUSE_REARRANGECTRL
}

void ListboxWidgetsPage::OnUpdateUIResetButton(wxUpdateUIEvent& event)
{
event.Enable( (m_radioSelMode->GetSelection() != LboxSel_Single) ||
Expand Down Expand Up @@ -605,6 +691,11 @@ void ListboxWidgetsPage::OnUpdateUIAddSeveral(wxUpdateUIEvent& event)
event.Enable(!(m_lbox->GetWindowStyle() & wxLB_SORT));
}

void ListboxWidgetsPage::OnUpdateUIMoveButtons(wxUpdateUIEvent& evt)
{
evt.Enable(m_radioListType->GetSelection() == LboxType_RearrangeList);
}

void ListboxWidgetsPage::OnListbox(wxCommandEvent& event)
{
long sel = event.GetSelection();
Expand Down

0 comments on commit 88b1699

Please sign in to comment.