From e8f25dbbced23734c716f1c5bda91c30635e894b Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Mon, 15 Dec 2008 11:03:59 +0000 Subject: [PATCH] added a helper function to show the popup menu allowing to configure the columns in header control git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@57356 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- include/wx/headerctrl.h | 15 +++++++++++++++ interface/wx/headerctrl.h | 18 ++++++++++++++++++ samples/grid/griddemo.cpp | 25 ++++++++++++++++++++++++- src/common/headerctrlcmn.cpp | 23 +++++++++++++++++++++++ 4 files changed, 80 insertions(+), 1 deletion(-) diff --git a/include/wx/headerctrl.h b/include/wx/headerctrl.h index 10ae50118c..6b3ec3e059 100644 --- a/include/wx/headerctrl.h +++ b/include/wx/headerctrl.h @@ -86,6 +86,10 @@ public: DoUpdate(idx); } + + // columns order + // ------------- + // set the columns order: the array defines the column index which appears // the given position, it must have GetColumnCount() elements and contain // all indices exactly once @@ -110,6 +114,17 @@ public: unsigned int pos); + // UI helpers + // ---------- + + // show the popup menu containing all columns with check marks for the ones + // which are currently shown -- this is meant to be called from + // EVT_HEADER_RIGHT_CLICK handler and should toggle the visibility of the + // n-th column if the function returns valid column index and not wxID_NONE + // which is returned if the user cancels the menu + int ShowColumnsMenu(const wxString& title = wxString()); + + // implementation only from now on // ------------------------------- diff --git a/interface/wx/headerctrl.h b/interface/wx/headerctrl.h index c2eca870e3..f4a1306b66 100644 --- a/interface/wx/headerctrl.h +++ b/interface/wx/headerctrl.h @@ -296,6 +296,24 @@ public: unsigned int idx, unsigned int pos); + /** + Show the popup menu allowing the user to show or hide the columns. + + This functions shows the popup menu containing all columns with check + marks for the ones which are currently shown at the current mouse + position. It is meant to be called from EVT_HEADER_RIGHT_CLICK handler + and should toggle the visibility of the n-th column if the function + returns valid column index and not wxID_NONE which is returned if the + user cancels the menu. + + @param title + The title for the menu if not empty. + @return + A valid column index or wxID_NONE if the user didn't select any + column. + */ + int ShowColumnsMenu(const wxString& title = wxString()); + protected: /** Method to be implemented by the derived classes to return the diff --git a/samples/grid/griddemo.cpp b/samples/grid/griddemo.cpp index bb502b9590..8ea628b1c5 100644 --- a/samples/grid/griddemo.cpp +++ b/samples/grid/griddemo.cpp @@ -33,6 +33,7 @@ #include "wx/aboutdlg.h" #include "wx/grid.h" +#include "wx/headerctrl.h" #include "wx/generic/gridctrl.h" #include "griddemo.h" @@ -1797,6 +1798,20 @@ private: event.Skip(); } + void OnColRightClick(wxHeaderCtrlEvent&) + { + int col = m_grid->GetGridColHeader()->ShowColumnsMenu("Columns:"); + if ( col == wxID_NONE ) + return; + + if ( m_grid->IsColShown(col) ) + m_grid->HideCol(col); + else + m_grid->ShowCol(col); + + UpdateOrderAndVisibility(); + } + void UpdateOrderAndVisibility() { wxString s; @@ -1880,6 +1895,14 @@ TabularGridFrame::TabularGridFrame() m_grid->UseNativeColHeader(); m_grid->HideRowLabels(); + m_grid->GetGridColHeader()->Connect + ( + wxEVT_COMMAND_HEADER_RIGHT_CLICK, + wxHeaderCtrlEventHandler(TabularGridFrame::OnColRightClick), + NULL, + this + ); + // add it and the other controls to the frame wxSizer * const sizerTop = new wxBoxSizer(wxVERTICAL); sizerTop->Add(m_grid, wxSizerFlags(1).Expand().Border()); @@ -1926,7 +1949,7 @@ TabularGridFrame::TabularGridFrame() wxSizer * const sizerShowCols = new wxBoxSizer(wxHORIZONTAL); sizerShowCols->Add(new wxStaticText(panel, wxID_ANY, "Current order:"), flagsHorz); - m_statOrder = new wxStaticText(panel, wxID_ANY, ""); + m_statOrder = new wxStaticText(panel, wxID_ANY, "<<< default >>>"); sizerShowCols->Add(m_statOrder, flagsHorz); sizerShowCols->Add(new wxButton(panel, wxID_RESET, "&Reset order")); sizerColumns->Add(sizerShowCols, wxSizerFlags().Expand().Border(wxTOP)); diff --git a/src/common/headerctrlcmn.cpp b/src/common/headerctrlcmn.cpp index 67b81cf962..7282425044 100644 --- a/src/common/headerctrlcmn.cpp +++ b/src/common/headerctrlcmn.cpp @@ -226,6 +226,29 @@ wxHeaderCtrlBase::DoResizeColumnIndices(wxArrayInt& colIndices, unsigned int cou wxASSERT_MSG( colIndices.size() == count, "logic error" ); } +// ---------------------------------------------------------------------------- +// wxHeaderCtrl extra UI +// ---------------------------------------------------------------------------- + +int wxHeaderCtrlBase::ShowColumnsMenu(const wxString& title) +{ + wxMenu menu; + if ( !title.empty() ) + menu.SetTitle(title); + + const unsigned count = GetColumnCount(); + for ( unsigned n = 0; n < count; n++ ) + { + const wxHeaderColumn& col = GetColumn(n); + menu.AppendCheckItem(n, col.GetTitle()); + if ( col.IsShown() ) + menu.Check(n, true); + } + + return GetPopupMenuSelectionFromUser(menu, + ScreenToClient(wxGetMousePosition())); +} + // ============================================================================ // wxHeaderCtrlSimple implementation // ============================================================================ -- 2.45.2