]> git.saurik.com Git - wxWidgets.git/commitdiff
added a helper function to show the popup menu allowing to configure the columns...
authorVadim Zeitlin <vadim@wxwidgets.org>
Mon, 15 Dec 2008 11:03:59 +0000 (11:03 +0000)
committerVadim Zeitlin <vadim@wxwidgets.org>
Mon, 15 Dec 2008 11:03:59 +0000 (11:03 +0000)
git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@57356 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775

include/wx/headerctrl.h
interface/wx/headerctrl.h
samples/grid/griddemo.cpp
src/common/headerctrlcmn.cpp

index 10ae50118cb894b80da6a58f5b47c121587e3607..6b3ec3e0594cbab1669fead0d39d94953960b9dd 100644 (file)
@@ -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
     // -------------------------------
 
index c2eca870e3a6e5ecd20b92019e52b6ed299890f1..f4a1306b66126a63668175633a0fb44324391790 100644 (file)
@@ -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
index bb502b95903d3aca2e0416efb50d9c89bfadc2e5..8ea628b1c59f20eea81a40f9336667b4c6bff6cc 100644 (file)
@@ -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, "<default>");
+    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));
index 67b81cf962fcee302d3f9bbd62f6e37140e64190..7282425044983e506c0e3ec6e8fbcee18a97cd58 100644 (file)
@@ -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
 // ============================================================================