X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/a06072d02625035418d2d4e103ff86a00969f5de..824eec7e595ba4d1022900f24173f335ba723f8c:/src/generic/grid.cpp diff --git a/src/generic/grid.cpp b/src/generic/grid.cpp index 542231d8f3..1b1ed6c604 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; } @@ -220,12 +224,13 @@ public: wxID_ANY, wxDefaultPosition, wxDefaultSize, - owner->CanDragColMove() ? wxHD_DRAGDROP : 0) + wxHD_ALLOW_HIDE | + (owner->CanDragColMove() ? wxHD_ALLOW_REORDER : 0)) { } protected: - virtual wxHeaderColumn& GetColumn(unsigned int idx) + virtual const wxHeaderColumn& GetColumn(unsigned int idx) const { return m_columns[idx]; } @@ -263,6 +268,23 @@ private: return true; } + // overridden to react to the actions using the columns popup menu + virtual void UpdateColumnVisibility(unsigned int idx, bool show) + { + GetOwner()->SetColSize(idx, show ? wxGRID_AUTOSIZE : 0); + + // as this is done by the user we should notify the main program about + // it + GetOwner()->SendEvent(wxEVT_GRID_COL_SIZE, -1, idx); + } + + // overridden to react to the columns order changes in the customization + // dialog + virtual void UpdateColumnsOrder(const wxArrayInt& order) + { + GetOwner()->SetColumnsOrder(order); + } + // event handlers forwarding wxHeaderCtrl events to wxGrid void OnClick(wxHeaderCtrlEvent& event) @@ -4802,7 +4824,7 @@ wxGrid::SetTable(wxGridTableBase *table, m_numCols = table->GetNumberCols(); if ( m_useNativeHeader ) - GetColHeader()->SetColumnCount(m_numCols); + GetGridColHeader()->SetColumnCount(m_numCols); m_table = table; m_table->SetView( this ); @@ -5124,20 +5146,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: @@ -5289,7 +5297,7 @@ bool wxGrid::Redimension( wxGridTableMessage& msg ) m_numCols += numCols; if ( m_useNativeHeader ) - GetColHeader()->SetColumnCount(m_numCols); + GetGridColHeader()->SetColumnCount(m_numCols); if ( !m_colAt.IsEmpty() ) { @@ -5357,7 +5365,7 @@ bool wxGrid::Redimension( wxGridTableMessage& msg ) int oldNumCols = m_numCols; m_numCols += numCols; if ( m_useNativeHeader ) - GetColHeader()->SetColumnCount(m_numCols); + GetGridColHeader()->SetColumnCount(m_numCols); if ( !m_colAt.IsEmpty() ) { @@ -5412,7 +5420,7 @@ bool wxGrid::Redimension( wxGridTableMessage& msg ) int numCols = msg.GetCommandInt2(); m_numCols -= numCols; if ( m_useNativeHeader ) - GetColHeader()->SetColumnCount(m_numCols); + GetGridColHeader()->SetColumnCount(m_numCols); if ( !m_colAt.IsEmpty() ) { @@ -5873,7 +5881,7 @@ void wxGrid::UpdateColumnSortingIndicator(int col) wxCHECK_RET( col != wxNOT_FOUND, "invalid column index" ); if ( m_useNativeHeader ) - GetColHeader()->UpdateColumn(col); + GetGridColHeader()->UpdateColumn(col); else if ( m_nativeColumnLabels ) m_colWindow->Refresh(); //else: sorting indicator display not yet implemented in grid version @@ -6852,24 +6860,15 @@ void wxGrid::DoEndMoveCol(int pos) m_dragRowOrCol = -1; } -void wxGrid::SetColPos(int idx, int pos) +void wxGrid::RefreshAfterColPosChange() { - // we're going to need m_colAt now, initialize it if needed - if ( m_colAt.empty() ) - { - m_colAt.reserve(m_numCols); - for ( int i = 0; i < m_numCols; i++ ) - m_colAt.push_back(i); - } - - wxHeaderCtrl::MoveColumnInOrderArray(m_colAt, idx, pos); - - // also recalculate the column rights - if ( !m_colWidths.IsEmpty() ) + // recalculate the column rights as the column positions have changed, + // unless we calculate them dynamically because all columns widths are the + // same and it's easy to do + if ( !m_colWidths.empty() ) { int colRight = 0; - int colPos; - for ( colPos = 0; colPos < m_numCols; colPos++ ) + for ( int colPos = 0; colPos < m_numCols; colPos++ ) { int colID = GetColAt( colPos ); @@ -6880,40 +6879,65 @@ void wxGrid::SetColPos(int idx, int pos) // and make the changes visible if ( m_useNativeHeader ) - GetColHeader()->SetColumnsOrder(m_colAt); + { + if ( m_colAt.empty() ) + GetGridColHeader()->ResetColumnsOrder(); + else + GetGridColHeader()->SetColumnsOrder(m_colAt); + } else + { m_colWindow->Refresh(); + } m_gridWin->Refresh(); } +void wxGrid::SetColumnsOrder(const wxArrayInt& order) +{ + m_colAt = order; + + RefreshAfterColPosChange(); +} + +void wxGrid::SetColPos(int idx, int pos) +{ + // we're going to need m_colAt now, initialize it if needed + if ( m_colAt.empty() ) + { + m_colAt.reserve(m_numCols); + for ( int i = 0; i < m_numCols; i++ ) + m_colAt.push_back(i); + } + + wxHeaderCtrl::MoveColumnInOrderArray(m_colAt, idx, pos); + + RefreshAfterColPosChange(); +} + +void wxGrid::ResetColPos() +{ + m_colAt.clear(); + RefreshAfterColPosChange(); +} 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 + GetGridColHeader()->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 } @@ -8190,7 +8214,7 @@ void wxGrid::UseNativeColHeader(bool native) CreateColumnWindow(); if ( m_useNativeHeader ) - GetColHeader()->SetColumnCount(m_numCols); + GetGridColHeader()->SetColumnCount(m_numCols); CalcWindowSizes(); } @@ -9574,7 +9598,7 @@ void wxGrid::SetColLabelValue( int col, const wxString& s ) { if ( m_useNativeHeader ) { - GetColHeader()->UpdateColumn(col); + GetGridColHeader()->UpdateColumn(col); } else { @@ -10401,8 +10425,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 +10436,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 ) + GetGridColHeader()->UpdateColumn(col); + //else: will be refreshed when the header is redrawn for ( int colPos = GetColPos(col); colPos < m_numCols; colPos++ ) { @@ -10579,7 +10606,7 @@ wxGrid::AutoSizeColOrRow(int colOrRow, bool setAsMin, wxGridDirection direction) { if ( m_useNativeHeader ) { - GetColHeader()->UpdateColumn(col); + GetGridColHeader()->UpdateColumn(col); } else {