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
This commit is contained in:
Vadim Zeitlin
2008-12-14 17:28:57 +00:00
parent 0c1c1c714a
commit 009c72169f
4 changed files with 124 additions and 60 deletions

View File

@@ -1505,11 +1505,19 @@ public:
return s; return s;
} }
// ------ row and col sizes
void SetDefaultRowSize( int height, bool resizeExistingRows = false ); void SetDefaultRowSize( int height, bool resizeExistingRows = false );
void SetRowSize( int row, int height ); 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 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 // columns index <-> positions mapping: by default, the position of the
// column is the same as its index, but the columns can also be reordered // column is the same as its index, but the columns can also be reordered
@@ -1551,6 +1559,8 @@ public:
return wxNOT_FOUND; 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 // automatically size the column or row to fit to its contents, if
@@ -2279,6 +2289,11 @@ private:
// the sorting indicator to effectively show // the sorting indicator to effectively show
void UpdateColumnSortingIndicator(int col); 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 // return the position (not index) of the column at the given logical pixel
// position // position

View File

@@ -2085,17 +2085,33 @@ public:
/** /**
Sets the width of the specified column. 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 @param col
The column index. The column index.
@param width @param width
The new column width in pixels or a negative value to fit the The new column width in pixels, 0 to hide the column or -1 to fit
column width to its label width. the column width to its label width.
*/ */
void SetColSize(int col, int 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. Sets the default width for columns in the grid.
@@ -2148,6 +2164,24 @@ public:
*/ */
void SetRowSize(int row, int height); 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); void SetColPos(int colID, int newPos);
/**
Resets the position of the columns to the default.
*/
void ResetColPos();
//@} //@}

View File

@@ -1740,6 +1740,13 @@ private:
m_grid->EnableDragColMove(m_chkEnableColMove->IsChecked()); 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&) void OnMoveColumn(wxCommandEvent&)
{ {
int col = m_txtColIndex->GetCol(); int col = m_txtColIndex->GetCol();
@@ -1797,7 +1804,8 @@ private:
*m_chkEnableColMove; *m_chkEnableColMove;
ColIndexEntry *m_txtColIndex, ColIndexEntry *m_txtColIndex,
*m_txtColPos; *m_txtColPos,
*m_txtColShowHide;
wxStaticText *m_statOrder; wxStaticText *m_statOrder;
@@ -1822,6 +1830,8 @@ BEGIN_EVENT_TABLE(TabularGridFrame, wxFrame)
TabularGridFrame::OnUpdateDrawNativeLabelsUI) TabularGridFrame::OnUpdateDrawNativeLabelsUI)
EVT_BUTTON(wxID_APPLY, TabularGridFrame::OnMoveColumn) 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_SORT(TabularGridFrame::OnGridColSort)
EVT_GRID_COL_MOVE(TabularGridFrame::OnGridColMove) EVT_GRID_COL_MOVE(TabularGridFrame::OnGridColMove)
@@ -1897,6 +1907,15 @@ TabularGridFrame::TabularGridFrame()
sizerShowCols->Add(m_statOrder, flagsHorz); sizerShowCols->Add(m_statOrder, flagsHorz);
sizerColumns->Add(sizerShowCols, wxSizerFlags().Expand().Border(wxTOP)); 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()); sizerControls->Add(sizerColumns, wxSizerFlags(1).Expand().Border());
sizerTop->Add(sizerControls, wxSizerFlags().Expand().Border()); sizerTop->Add(sizerControls, wxSizerFlags().Expand().Border());

View File

@@ -184,11 +184,15 @@ public:
virtual int GetFlags() const 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() ) if ( m_grid->CanDragColSize() )
flags |= wxCOL_RESIZABLE; flags |= wxCOL_RESIZABLE;
if ( m_grid->CanDragColMove() ) if ( m_grid->CanDragColMove() )
flags |= wxCOL_REORDERABLE; flags |= wxCOL_REORDERABLE;
if ( GetWidth() == 0 )
flags |= wxCOL_HIDDEN;
return flags; return flags;
} }
@@ -5124,20 +5128,6 @@ bool wxGrid::Redimension( wxGridTableMessage& msg )
// cell it might want to save that stuff to might no longer exist. // cell it might want to save that stuff to might no longer exist.
HideCellEditControl(); 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() ) switch ( msg.GetId() )
{ {
case wxGRIDTABLE_NOTIFY_ROWS_INSERTED: case wxGRIDTABLE_NOTIFY_ROWS_INSERTED:
@@ -6852,6 +6842,21 @@ void wxGrid::DoEndMoveCol(int pos)
m_dragRowOrCol = -1; 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) void wxGrid::SetColPos(int idx, int pos)
{ {
// we're going to need m_colAt now, initialize it if needed // 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); wxHeaderCtrl::MoveColumnInOrderArray(m_colAt, idx, pos);
// also recalculate the column rights // also recalculate the column rights as the column positions have changed
if ( !m_colWidths.IsEmpty() ) UpdateColumnRights();
{
int colRight = 0;
int colPos;
for ( colPos = 0; colPos < m_numCols; colPos++ )
{
int colID = GetColAt( colPos );
colRight += m_colWidths[colID];
m_colRights[colID] = colRight;
}
}
// and make the changes visible // and make the changes visible
if ( m_useNativeHeader ) if ( m_useNativeHeader )
@@ -6886,34 +6880,28 @@ void wxGrid::SetColPos(int idx, int pos)
m_gridWin->Refresh(); m_gridWin->Refresh();
} }
void wxGrid::ResetColPos()
{
m_colAt.clear();
}
void wxGrid::EnableDragColMove( bool enable ) void wxGrid::EnableDragColMove( bool enable )
{ {
if ( m_canDragColMove == enable ) if ( m_canDragColMove == enable )
return; return;
if ( m_useNativeHeader )
{
// update all columns to make them [not] reorderable
GetColHeader()->SetColumnCount(m_numCols);
}
m_canDragColMove = enable; m_canDragColMove = enable;
if ( !m_canDragColMove ) // 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
m_colAt.Clear(); // columns order by disabling moving them after putting them in the desired
// order, whereas now you can always call ResetColPos() manually if needed
//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_colWindow->Refresh();
m_gridWin->Refresh();
}
} }
@@ -10401,8 +10389,9 @@ void wxGrid::SetColSize( int col, int width )
// we intentionally don't test whether the width is less than // we intentionally don't test whether the width is less than
// GetColMinimalWidth() here but we do compare it with // GetColMinimalWidth() here but we do compare it with
// GetColMinimalAcceptableWidth() as otherwise things currently break (see // GetColMinimalAcceptableWidth() as otherwise things currently break (see
// #651) // #651) -- and we also always allow the width of 0 as it has the special
if ( width < GetColMinimalAcceptableWidth() ) // sense of hiding the column
if ( width > 0 && width < GetColMinimalAcceptableWidth() )
return; return;
if ( m_colWidths.IsEmpty() ) if ( m_colWidths.IsEmpty() )
@@ -10411,9 +10400,11 @@ void wxGrid::SetColSize( int col, int width )
InitColWidths(); InitColWidths();
} }
int w = wxMax( 0, width ); const int diff = width - m_colWidths[col];
int diff = w - m_colWidths[col]; m_colWidths[col] = width;
m_colWidths[col] = w; if ( m_useNativeHeader )
GetColHeader()->UpdateColumn(col);
//else: will be refreshed when the header is redrawn
for ( int colPos = GetColPos(col); colPos < m_numCols; colPos++ ) for ( int colPos = GetColPos(col); colPos < m_numCols; colPos++ )
{ {