Add wxAddRemoveCtrl class.

This is a simple high level helper combining an arbitrary control showing
multiple items with the buttons allowing to add items to and remove items from
this control, but using the buttons and the layout appropriate for the current
platform.

Add the implementation itself, an example of using it to the dialogs sample
and the documentation.

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@78462 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Vadim Zeitlin
2015-02-09 00:26:11 +00:00
parent adb339078e
commit 453897149f
36 changed files with 1388 additions and 184 deletions

View File

@@ -47,6 +47,7 @@
#endif // wxUSE_CHOICEDLG
#include "wx/rearrangectrl.h"
#include "wx/addremovectrl.h"
#if wxUSE_STARTUP_TIPS
#include "wx/tipdlg.h"
@@ -173,6 +174,10 @@ wxBEGIN_EVENT_TABLE(MyFrame, wxFrame)
EVT_MENU(DIALOGS_REARRANGE, MyFrame::Rearrange)
#endif // wxUSE_REARRANGECTRL
#if wxUSE_ADDREMOVECTRL
EVT_MENU(DIALOGS_ADDREMOVE, MyFrame::AddRemove)
#endif // wxUSE_ADDREMOVECTRL
#if wxUSE_FILEDLG
EVT_MENU(DIALOGS_FILE_OPEN, MyFrame::FileOpen)
EVT_MENU(DIALOGS_FILE_OPEN2, MyFrame::FileOpen2)
@@ -386,6 +391,10 @@ bool MyApp::OnInit()
choices_menu->Append(DIALOGS_REARRANGE, wxT("&Rearrange dialog\tCtrl-R"));
#endif // wxUSE_REARRANGECTRL
#if wxUSE_ADDREMOVECTRL
choices_menu->Append(DIALOGS_ADDREMOVE, "&Add/remove items control\tCtrl-A");
#endif // wxUSE_ADDREMOVECTRL
#if USE_COLOURDLG_GENERIC || USE_FONTDLG_GENERIC
choices_menu->AppendSeparator();
#endif // USE_COLOURDLG_GENERIC || USE_FONTDLG_GENERIC
@@ -1333,6 +1342,91 @@ void MyFrame::Rearrange(wxCommandEvent& WXUNUSED(event))
}
#endif // wxUSE_REARRANGECTRL
#if wxUSE_ADDREMOVECTRL
void MyFrame::AddRemove(wxCommandEvent& WXUNUSED(event))
{
wxDialog dlg(this, wxID_ANY, "wxAddRemoveCtrl test",
wxDefaultPosition, wxDefaultSize,
wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER);
wxAddRemoveCtrl* const ctrl = new wxAddRemoveCtrl(&dlg);
ctrl->SetInitialSize(wxSize(-1, 12*GetCharHeight()));
const wxString items[] =
{
"some", "items", "for", "testing", "wxAddRemoveCtrl",
};
wxListBox* const lbox = new wxListBox(ctrl, wxID_ANY,
wxDefaultPosition, wxDefaultSize,
WXSIZEOF(items), items);
// Test adaptor class connecting wxAddRemoveCtrl with wxListBox we use
// inside it.
class ListBoxAdaptor : public wxAddRemoveAdaptor
{
public:
wxEXPLICIT ListBoxAdaptor(wxListBox* lbox)
: m_lbox(lbox)
{
}
wxWindow* GetItemsCtrl() const wxOVERRIDE
{
return m_lbox;
}
bool CanAdd() const wxOVERRIDE
{
// Restrict the maximal number of items to 10 just for testing.
return m_lbox->GetCount() <= 10;
}
bool CanRemove() const wxOVERRIDE
{
// We must have a selected item in order to be able to delete it.
return m_lbox->GetSelection() != wxNOT_FOUND;
}
void OnAdd() wxOVERRIDE
{
// A real program would use a wxDataViewCtrl or wxListCtrl and
// allow editing the newly edited item in place, here we just use a
// hardcoded item value instead.
static int s_item = 0;
m_lbox->Append(wxString::Format("new item #%d", ++s_item));
}
void OnRemove() wxOVERRIDE
{
// Notice that we don't need to check if we have a valid selection,
// we can be only called if CanRemove(), which already checks for
// this, had returned true.
const unsigned pos = m_lbox->GetSelection();
m_lbox->Delete(pos);
m_lbox->SetSelection(pos == m_lbox->GetCount() ? pos - 1 : pos);
}
private:
wxListBox* const m_lbox;
};
ctrl->SetAdaptor(new ListBoxAdaptor(lbox));
ctrl->SetButtonsToolTips("Add up to 10 items", "Remove current item");
wxSizer* const sizerTop = new wxBoxSizer(wxVERTICAL);
sizerTop->Add(ctrl, wxSizerFlags(1).Expand().Border());
sizerTop->Add(dlg.CreateStdDialogButtonSizer(wxOK | wxCANCEL),
wxSizerFlags().Expand().Border());
dlg.SetSizerAndFit(sizerTop);
dlg.ShowModal();
}
#endif // wxUSE_ADDREMOVECTRL
#if wxUSE_FILEDLG
// panel with custom controls for file dialog