diff --git a/samples/widgets/listbox.cpp b/samples/widgets/listbox.cpp index fba1742286..aec1629942 100644 --- a/samples/widgets/listbox.cpp +++ b/samples/widgets/listbox.cpp @@ -42,6 +42,7 @@ #include "wx/sizer.h" #include "wx/checklst.h" +#include "wx/rearrangectrl.h" #include "itemcontainer.h" #include "widgets.h" @@ -71,7 +72,9 @@ enum ListboxPage_EnsureVisibleText, ListboxPage_ContainerTests, ListboxPage_GetTopItem, - ListboxPage_GetCountPerPage + ListboxPage_GetCountPerPage, + ListboxPage_MoveUp, + ListboxPage_MoveDown, }; // ---------------------------------------------------------------------------- @@ -103,6 +106,8 @@ protected: 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); @@ -116,6 +121,7 @@ protected: void OnUpdateUIDeleteButton(wxUpdateUIEvent& event); void OnUpdateUIDeleteSelButton(wxUpdateUIEvent& event); void OnUpdateUIResetButton(wxUpdateUIEvent& event); + void OnUpdateUIMoveButtons(wxUpdateUIEvent& evt); // reset the listbox parameters void Reset(); @@ -139,6 +145,14 @@ protected: LboxSel_Multiple } m_lboxSelMode; + // the list type + enum LboxType + { + LboxType_ListBox, + LboxType_CheckListBox, + LboxType_RearrangeList + }; + // should it be sorted? bool m_sorted; @@ -152,10 +166,12 @@ protected: // 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; @@ -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) @@ -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) @@ -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; @@ -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, @@ -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); @@ -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); @@ -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); } @@ -406,6 +448,7 @@ void ListboxWidgetsPage::CreateLbox() flags |= wxLB_OWNERDRAW; wxArrayString items; + wxArrayInt order; if ( m_lbox ) { int count = m_lbox->GetCount(); @@ -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; } + switch( m_radioListType->GetSelection() ) + { #if wxUSE_CHECKLISTBOX - if ( m_chkCheck->GetValue() ) - { - m_lbox = new wxCheckListBox(this, ListboxPage_Listbox, + 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(); } @@ -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) || @@ -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();