From: Vadim Zeitlin Date: Sun, 14 Dec 2008 17:28:57 +0000 (+0000) Subject: add support for hiding columns when using native header control in wxGrid; also added... X-Git-Url: https://git.saurik.com/wxWidgets.git/commitdiff_plain/009c72169f3dc90dbec89fdfe2956065aa35377f add support for hiding columns when using native header control in wxGrid; also added convenient Hide/ShowCol() methods git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@57336 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- diff --git a/include/wx/generic/grid.h b/include/wx/generic/grid.h index 98427527e8..8611b6ad6c 100644 --- a/include/wx/generic/grid.h +++ b/include/wx/generic/grid.h @@ -1505,11 +1505,19 @@ public: return s; } + // ------ row and col sizes void SetDefaultRowSize( int height, bool resizeExistingRows = false ); void SetRowSize( int row, int height ); - void SetDefaultColSize( int width, bool resizeExistingCols = false ); + void HideRow(int row) { SetRowSize(row, 0); } + void ShowRow(int row) { SetRowSize(row, -1); } + void SetDefaultColSize( int width, bool resizeExistingCols = false ); void SetColSize( int col, int width ); + void HideCol(int col) { SetColSize(col, 0); } + void ShowCol(int col) { SetColSize(col, -1); } + + + // ------- columns (only, for now) reordering // columns index <-> positions mapping: by default, the position of the // column is the same as its index, but the columns can also be reordered @@ -1551,6 +1559,8 @@ public: return wxNOT_FOUND; } + // reset the columns positions to the default order + void ResetColPos(); // automatically size the column or row to fit to its contents, if @@ -2279,6 +2289,11 @@ private: // the sorting indicator to effectively show void UpdateColumnSortingIndicator(int col); + // update column right positions after their order changed (does nothing if + // we only use the default widths as in this case m_colRights is not used + // neither) + void UpdateColumnRights(); + // return the position (not index) of the column at the given logical pixel // position diff --git a/interface/wx/grid.h b/interface/wx/grid.h index bb38bdaeaf..9072c1992c 100644 --- a/interface/wx/grid.h +++ b/interface/wx/grid.h @@ -2085,17 +2085,33 @@ public: /** Sets the width of the specified column. - Notice that this function does not refresh the grid, you need to call - ForceRefresh() to make the changes take effect immediately. - @param col The column index. @param width - The new column width in pixels or a negative value to fit the - column width to its label width. + The new column width in pixels, 0 to hide the column or -1 to fit + the column width to its label width. */ void SetColSize(int col, int width); + /** + Hides the specified column. + + To show the column later you need to call SetColSize() with non-0 + width or ShowCol(). + + @param col + The column index. + */ + void HideCol(int col); + + /** + Shows the previously hidden column by resizing it to non-0 size. + + @see HideCol(), SetColSize() + */ + void ShowCol(int col); + + /** Sets the default width for columns in the grid. @@ -2148,6 +2164,24 @@ public: */ void SetRowSize(int row, int height); + /** + Hides the specified row. + + To show the row later you need to call SetRowSize() with non-0 + width or ShowRow(). + + @param col + The row index. + */ + void HideRow(int col); + + /** + Shows the previously hidden row by resizing it to non-0 size. + + @see HideRow(), SetRowSize() + */ + void ShowRow(int col); + //@} @@ -2261,6 +2295,11 @@ public: */ void SetColPos(int colID, int newPos); + /** + Resets the position of the columns to the default. + */ + void ResetColPos(); + //@} diff --git a/samples/grid/griddemo.cpp b/samples/grid/griddemo.cpp index 3e2e45147b..ea97cc9bc6 100644 --- a/samples/grid/griddemo.cpp +++ b/samples/grid/griddemo.cpp @@ -1740,6 +1740,13 @@ private: m_grid->EnableDragColMove(m_chkEnableColMove->IsChecked()); } + void OnShowHideColumn(wxCommandEvent& event) + { + int col = m_txtColShowHide->GetCol(); + if ( col != -1 ) + m_grid->SetColSize(col, event.GetId() == wxID_ADD ? -1 : 0); + } + void OnMoveColumn(wxCommandEvent&) { int col = m_txtColIndex->GetCol(); @@ -1797,7 +1804,8 @@ private: *m_chkEnableColMove; ColIndexEntry *m_txtColIndex, - *m_txtColPos; + *m_txtColPos, + *m_txtColShowHide; wxStaticText *m_statOrder; @@ -1822,6 +1830,8 @@ BEGIN_EVENT_TABLE(TabularGridFrame, wxFrame) TabularGridFrame::OnUpdateDrawNativeLabelsUI) EVT_BUTTON(wxID_APPLY, TabularGridFrame::OnMoveColumn) + EVT_BUTTON(wxID_ADD, TabularGridFrame::OnShowHideColumn) + EVT_BUTTON(wxID_DELETE, TabularGridFrame::OnShowHideColumn) EVT_GRID_COL_SORT(TabularGridFrame::OnGridColSort) EVT_GRID_COL_MOVE(TabularGridFrame::OnGridColMove) @@ -1897,6 +1907,15 @@ TabularGridFrame::TabularGridFrame() sizerShowCols->Add(m_statOrder, flagsHorz); sizerColumns->Add(sizerShowCols, wxSizerFlags().Expand().Border(wxTOP)); + wxSizer * const sizerShowHide = new wxBoxSizer(wxHORIZONTAL); + sizerShowHide->Add(new wxStaticText(panel, wxID_ANY, "Show/hide column:"), + flagsHorz); + m_txtColShowHide = new ColIndexEntry(panel); + sizerShowHide->Add(m_txtColShowHide, flagsHorz); + sizerShowHide->Add(new wxButton(panel, wxID_ADD, "&Show"), flagsHorz); + sizerShowHide->Add(new wxButton(panel, wxID_DELETE, "&Hide"), flagsHorz); + sizerColumns->Add(sizerShowHide, wxSizerFlags().Expand().Border(wxTOP)); + sizerControls->Add(sizerColumns, wxSizerFlags(1).Expand().Border()); sizerTop->Add(sizerControls, wxSizerFlags().Expand().Border()); diff --git a/src/generic/grid.cpp b/src/generic/grid.cpp index 542231d8f3..418b97bffb 100644 --- a/src/generic/grid.cpp +++ b/src/generic/grid.cpp @@ -184,11 +184,15 @@ public: virtual int GetFlags() const { - int flags = 0; + // we can't know in advance whether we can sort by this column or not + // with wxGrid API so suppose we can by default + int flags = wxCOL_SORTABLE; if ( m_grid->CanDragColSize() ) flags |= wxCOL_RESIZABLE; if ( m_grid->CanDragColMove() ) flags |= wxCOL_REORDERABLE; + if ( GetWidth() == 0 ) + flags |= wxCOL_HIDDEN; return flags; } @@ -5124,20 +5128,6 @@ bool wxGrid::Redimension( wxGridTableMessage& msg ) // cell it might want to save that stuff to might no longer exist. HideCellEditControl(); -#if 0 - // if we were using the default widths/heights so far, we must change them - // now - if ( m_colWidths.IsEmpty() ) - { - InitColWidths(); - } - - if ( m_rowHeights.IsEmpty() ) - { - InitRowHeights(); - } -#endif - switch ( msg.GetId() ) { case wxGRIDTABLE_NOTIFY_ROWS_INSERTED: @@ -6852,6 +6842,21 @@ void wxGrid::DoEndMoveCol(int pos) m_dragRowOrCol = -1; } +void wxGrid::UpdateColumnRights() +{ + if ( m_colWidths.empty() ) + return; + + int colRight = 0; + for ( int colPos = 0; colPos < m_numCols; colPos++ ) + { + int colID = GetColAt( colPos ); + + colRight += m_colWidths[colID]; + m_colRights[colID] = colRight; + } +} + void wxGrid::SetColPos(int idx, int pos) { // we're going to need m_colAt now, initialize it if needed @@ -6864,19 +6869,8 @@ void wxGrid::SetColPos(int idx, int pos) wxHeaderCtrl::MoveColumnInOrderArray(m_colAt, idx, pos); - // also recalculate the column rights - if ( !m_colWidths.IsEmpty() ) - { - int colRight = 0; - int colPos; - for ( colPos = 0; colPos < m_numCols; colPos++ ) - { - int colID = GetColAt( colPos ); - - colRight += m_colWidths[colID]; - m_colRights[colID] = colRight; - } - } + // also recalculate the column rights as the column positions have changed + UpdateColumnRights(); // and make the changes visible if ( m_useNativeHeader ) @@ -6886,34 +6880,28 @@ void wxGrid::SetColPos(int idx, int pos) m_gridWin->Refresh(); } - +void wxGrid::ResetColPos() +{ + m_colAt.clear(); +} void wxGrid::EnableDragColMove( bool enable ) { if ( m_canDragColMove == enable ) return; - m_canDragColMove = enable; - - if ( !m_canDragColMove ) + if ( m_useNativeHeader ) { - m_colAt.Clear(); + // update all columns to make them [not] reorderable + GetColHeader()->SetColumnCount(m_numCols); + } - //Recalculate the column rights - if ( !m_colWidths.IsEmpty() ) - { - int colRight = 0; - int colPos; - for ( colPos = 0; colPos < m_numCols; colPos++ ) - { - colRight += m_colWidths[colPos]; - m_colRights[colPos] = colRight; - } - } + m_canDragColMove = enable; - m_colWindow->Refresh(); - m_gridWin->Refresh(); - } + // we use to call ResetColPos() from here if !enable but this doesn't seem + // right as it would mean there would be no way to "freeze" the current + // columns order by disabling moving them after putting them in the desired + // order, whereas now you can always call ResetColPos() manually if needed } @@ -10401,8 +10389,9 @@ void wxGrid::SetColSize( int col, int width ) // we intentionally don't test whether the width is less than // GetColMinimalWidth() here but we do compare it with // GetColMinimalAcceptableWidth() as otherwise things currently break (see - // #651) - if ( width < GetColMinimalAcceptableWidth() ) + // #651) -- and we also always allow the width of 0 as it has the special + // sense of hiding the column + if ( width > 0 && width < GetColMinimalAcceptableWidth() ) return; if ( m_colWidths.IsEmpty() ) @@ -10411,9 +10400,11 @@ void wxGrid::SetColSize( int col, int width ) InitColWidths(); } - int w = wxMax( 0, width ); - int diff = w - m_colWidths[col]; - m_colWidths[col] = w; + const int diff = width - m_colWidths[col]; + m_colWidths[col] = width; + if ( m_useNativeHeader ) + GetColHeader()->UpdateColumn(col); + //else: will be refreshed when the header is redrawn for ( int colPos = GetColPos(col); colPos < m_numCols; colPos++ ) {