+#if wxUSE_REARRANGECTRL
+// custom rearrange dialog: it adds the possibility to rename an item to the
+// base class functionality
+class MyRearrangeDialog : public wxRearrangeDialog
+{
+public:
+ MyRearrangeDialog(wxWindow *parent,
+ wxArrayInt& order,
+ wxArrayString& labels,
+ wxArrayString& labelsOrig)
+ : wxRearrangeDialog
+ (
+ parent,
+ "Configure the columns shown:",
+ "wxRearrangeDialog example",
+ order,
+ labels
+ ),
+ m_order(order),
+ m_labels(labels),
+ m_labelsOrig(labelsOrig)
+ {
+ m_sel = wxNOT_FOUND;
+
+ wxPanel * const panel = new wxPanel(this);
+ wxSizer * const sizer = new wxBoxSizer(wxHORIZONTAL);
+
+ m_labelOrig = new wxStaticText(panel, wxID_ANY, "");
+ sizer->Add(m_labelOrig, wxSizerFlags().Centre().Border(wxRIGHT));
+
+ m_text = new wxTextCtrl(panel, wxID_ANY, "",
+ wxDefaultPosition, wxDefaultSize,
+ wxTE_PROCESS_ENTER);
+ sizer->Add(m_text, wxSizerFlags().Centre().Border(wxRIGHT));
+
+ sizer->Add(new wxButton(panel, wxID_APPLY, "&Rename"),
+ wxSizerFlags().Centre());
+
+ panel->SetSizer(sizer);
+
+ // call this first to ensure that the controls have a reasonable best
+ // size before they're added
+ DoUpdateExtraControls(GetList()->GetSelection());
+
+ AddExtraControls(panel);
+
+
+ // another customization not directly supported by the dialog: add a
+ // custom button
+ wxWindow * const btnOk = FindWindow(wxID_OK);
+ wxCHECK_RET( btnOk, "no Ok button?" );
+
+ wxSizer * const sizerBtns = btnOk->GetContainingSizer();
+ wxCHECK_RET( sizerBtns, "no buttons sizer?" );
+
+ sizerBtns->Add(new wxButton(this, wxID_RESET, "&Reset all"),
+ wxSizerFlags().Border(wxLEFT));
+ }
+
+ // call this instead of ShowModal() to update order and labels array in
+ // case the dialog was not cancelled
+ bool Rearrange()
+ {
+ switch ( ShowModal() )
+ {
+ case wxID_CANCEL:
+ return false;
+
+ case wxID_OK:
+ m_order = GetOrder();
+ break;
+
+ case wxID_RESET:
+ // order already reset
+ break;
+ }
+
+ return true;
+ }
+
+private:
+ void OnSelChange(wxCommandEvent& event)
+ {
+ DoUpdateExtraControls(event.GetInt());
+ }
+
+ void OnUpdateUIRename(wxUpdateUIEvent& event)
+ {
+ event.Enable( CanRename() );
+ }
+
+ void OnRename(wxCommandEvent& WXUNUSED(event))
+ {
+ if ( !CanRename() )
+ return;
+
+ m_labels[m_sel] = m_text->GetValue();
+ GetList()->SetString(m_sel, m_labels[m_sel]);
+ }
+
+ void OnReset(wxCommandEvent& WXUNUSED(event))
+ {
+ // in a real program we should probably ask if the user really wants to
+ // do this but here we just go ahead and reset all columns labels and
+ // their order without confirmation
+ const unsigned count = m_order.size();
+ for ( unsigned n = 0; n < count; n++ )
+ {
+ m_order[n] = n;
+ m_labels[n] = m_labelsOrig[n];
+ }
+
+ EndModal(wxID_RESET);
+ }
+
+ bool CanRename() const
+ {
+ // only allow renaming if the user modified the currently selected item
+ // text (which presupposes that we do have a current item)
+ return m_sel != wxNOT_FOUND && m_text->GetValue() != m_labels[m_sel];
+ }
+
+ void DoUpdateExtraControls(int sel)
+ {
+ m_sel = sel;
+
+ if ( m_sel == wxNOT_FOUND )
+ {
+ m_labelOrig->SetLabel("<no selection>");
+ m_text->Clear();
+ m_text->Disable();
+ }
+ else // have valid item
+ {
+ m_labelOrig->SetLabelText(m_labelsOrig[m_sel]);
+ m_text->Enable();
+ m_text->SetValue(m_labels[m_sel]);
+ }
+ }
+
+ wxArrayInt& m_order;
+ wxArrayString& m_labels,
+ m_labelsOrig;
+
+ int m_sel;
+ wxStaticText *m_labelOrig;
+ wxTextCtrl *m_text;
+
+ DECLARE_EVENT_TABLE()
+ wxDECLARE_NO_COPY_CLASS(MyRearrangeDialog);
+};
+
+BEGIN_EVENT_TABLE(MyRearrangeDialog, wxRearrangeDialog)
+ EVT_LISTBOX(wxID_ANY, MyRearrangeDialog::OnSelChange)
+
+ EVT_UPDATE_UI(wxID_APPLY, MyRearrangeDialog::OnUpdateUIRename)
+
+ EVT_TEXT_ENTER(wxID_ANY, MyRearrangeDialog::OnRename)
+ EVT_BUTTON(wxID_APPLY, MyRearrangeDialog::OnRename)
+ EVT_BUTTON(wxID_RESET, MyRearrangeDialog::OnReset)
+END_EVENT_TABLE()
+
+void MyFrame::Rearrange(wxCommandEvent& WXUNUSED(event))
+{
+ // the arrays are static so that we preserve the items order between calls
+ // to this function
+ static wxArrayInt s_order;
+ static wxArrayString s_labels,
+ s_labelsOrig;
+
+ // initialize them on the first call
+ if ( s_labelsOrig.empty() )
+ {
+ static const struct ItemInfo
+ {
+ const char *label;
+ const char *labelOrig;
+ int order;
+ } items[] =
+ {
+ { "File name", "Name", 0 },
+ { "File type", "Ext", 1 },
+ { "Size", "Size", 2 },
+ { "Creation time", "Ctime", ~3 }, // negated so hidden
+ { "Last accessed", "Atime", ~4 },
+ { "Last modified", "Mtime", 5 },
+ };
+
+ s_order.reserve(WXSIZEOF(items));
+ s_labels.reserve(WXSIZEOF(items));
+ s_labelsOrig.reserve(WXSIZEOF(items));
+ for ( unsigned n = 0; n < WXSIZEOF(items); n++ )
+ {
+ const ItemInfo& item = items[n];
+ s_order.push_back(item.order);
+ s_labels.push_back(item.label);
+ s_labelsOrig.push_back(item.labelOrig);
+ }
+ }
+
+ MyRearrangeDialog dlg(this, s_order, s_labels, s_labelsOrig);
+ if ( !dlg.Rearrange() )
+ return;
+
+ wxString columns;
+ for ( unsigned n = 0; n < s_order.size(); n++ )
+ {
+ columns += wxString::Format("\n %u: ", n);
+ int idx = s_order[n];
+ if ( idx < 0 )
+ {
+ columns += "[hidden] ";
+ idx = ~idx;
+ }
+
+ columns += s_labels[idx];
+ if ( s_labels[idx] != s_labelsOrig[idx] )
+ {
+ columns += wxString::Format(" (original label: \"%s\")",
+ s_labelsOrig[idx]);
+ }
+ }
+
+ wxLogMessage("The columns order now is:%s", columns);
+}
+#endif // wxUSE_REARRANGECTRL
+