From 733f486aac7f5d92f902d6041338cd6e1bc7d99d Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Thu, 15 Mar 2007 17:44:41 +0000 Subject: [PATCH] added support for wxGRID_AUTOSIZE in wxGrid::SetRow/ColLabelSize() git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@44835 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- docs/changes.txt | 1 + docs/latex/wx/grid.tex | 20 +++++++++-- include/wx/generic/grid.h | 32 +++++++++++++---- samples/grid/griddemo.cpp | 73 +++++++++++++++++++++++++++++++++++++++ samples/grid/griddemo.h | 15 ++++++++ src/generic/grid.cpp | 73 +++++++++++++++++++++++++++++++++++++-- 6 files changed, 201 insertions(+), 13 deletions(-) diff --git a/docs/changes.txt b/docs/changes.txt index 0ea04eb93f..7e644bdaca 100644 --- a/docs/changes.txt +++ b/docs/changes.txt @@ -40,6 +40,7 @@ All: - Added wxMutex::LockTimeout() (Aleksandr Napylov) - Set locale to the default in all ports, not just wxGTK - Added wxGridUpdateLocker helper class (Evgeniy Tarassov) +- Support wxGRID_AUTOSIZE in wxGrid::SetRow/ColLabelSize() (Evgeniy Tarassov) wxGTK: diff --git a/docs/latex/wx/grid.tex b/docs/latex/wx/grid.tex index 62b640cda8..a6d4685ab2 100644 --- a/docs/latex/wx/grid.tex +++ b/docs/latex/wx/grid.tex @@ -183,11 +183,11 @@ The memory requirements for this could become prohibitive if your grid is very l -\membersection{wxGrid::AutoSizeColOrRow}\label{wxgridautosizecolorrow} +\membersection{wxGrid::AutoSizeColLabelSize}\label{wxgridautosizecollabelsize} -\func{void}{AutoSizeColOrRow}{\param{int }{n}, \param{bool }{setAsMin}, \param{bool }{column}} +\func{void}{AutoSizeColLabelSize}{\param{int }{col}} -Common part of AutoSizeColumn/Row() or row? +Automatically adjusts width of the column to fit its label. @@ -230,6 +230,14 @@ The memory requirements for this could become prohibitive if your grid is very l +\membersection{wxGrid::AutoSizeRowLabelSize}\label{wxgridautosizerowlabelsize} + +\func{void}{AutoSizeRowLabelSize}{\param{int }{col}} + +Automatically adjusts height of the row to fit its label. + + + \membersection{wxGrid::AutoSizeRows}\label{wxgridautosizerows} \func{void}{AutoSizeRows}{\param{bool }{setAsMin = true}} @@ -1612,6 +1620,9 @@ Vertical alignment should be one of wxALIGN\_TOP, wxALIGN\_CENTRE or wxALIGN\_BO Sets the height of the column labels. +If \arg{height} equals to \textt{wxGRID\_AUTOSIZE} then height is calculated automatically +so that no label is truncated. Note that this could be slow for a large table. + \membersection{wxGrid::SetColLabelValue}\label{wxgridsetcollabelvalue} @@ -1851,6 +1862,9 @@ Vertical alignment should be one of wxALIGN\_TOP, wxALIGN\_CENTRE or wxALIGN\_BO Sets the width of the row labels. +If \arg{width} equals \textt{wxGRID\_AUTOSIZE} then width is calculated automatically +so that no label is truncated. Note that this could be slow for a large table. + \membersection{wxGrid::SetRowLabelValue}\label{wxgridsetrowlabelvalue} diff --git a/include/wx/generic/grid.h b/include/wx/generic/grid.h index 2d04e59b6c..17d8bb22a1 100644 --- a/include/wx/generic/grid.h +++ b/include/wx/generic/grid.h @@ -51,6 +51,18 @@ extern WXDLLIMPEXP_DATA_ADV(const wxChar) wxGridNameStr[]; #define wxGRID_VALUE_TEXT wxGRID_VALUE_STRING #define wxGRID_VALUE_LONG wxGRID_VALUE_NUMBER +// magic constant which tells (to some functions) to automatically calculate +// the appropriate size +#define wxGRID_AUTOSIZE (-1) + +// many wxGrid methods work either with columns or rows, this enum is used for +// the parameter indicating which one should it be +enum wxGridDirection +{ + wxGRID_COLUMN, + wxGRID_ROW, +}; + // ---------------------------------------------------------------------------- // forward declarations // ---------------------------------------------------------------------------- @@ -1408,9 +1420,9 @@ public: // setAsMin is true, this optimal width will also be set as minimal width // for this column void AutoSizeColumn( int col, bool setAsMin = true ) - { AutoSizeColOrRow(col, setAsMin, true); } + { AutoSizeColOrRow(col, setAsMin, wxGRID_COLUMN); } void AutoSizeRow( int row, bool setAsMin = true ) - { AutoSizeColOrRow(row, setAsMin, false); } + { AutoSizeColOrRow(row, setAsMin, wxGRID_ROW); } // auto size all columns (very ineffective for big grids!) void AutoSizeColumns( bool setAsMin = true ) @@ -1423,6 +1435,10 @@ public: // and also set the grid size to just fit its contents void AutoSize(); + // Note for both AutoSizeRowLabelSize and AutoSizeColLabelSize: + // If col equals to wxGRID_AUTOSIZE value then function autosizes labels column + // instead of data column. Note that this operation may be slow for large + // tables. // autosize row height depending on label text void AutoSizeRowLabelSize( int row ); @@ -1862,7 +1878,10 @@ protected: int SetOrCalcRowSizes(bool calcOnly, bool setAsMin = true); // common part of AutoSizeColumn/Row() - void AutoSizeColOrRow(int n, bool setAsMin, bool column /* or row? */); + void AutoSizeColOrRow(int n, bool setAsMin, wxGridDirection direction); + + // Calculate the minimum acceptable size for labels area + wxCoord CalcColOrRowLabelAreaMinSize(wxGridDirection direction); // if a column has a minimal width, it will be the value for it in this // hash table @@ -2006,7 +2025,6 @@ protected: DECLARE_NO_COPY_CLASS(wxGrid) }; - // ---------------------------------------------------------------------------- // wxGridUpdateLocker prevents updates to a grid during its lifetime // ---------------------------------------------------------------------------- @@ -2125,7 +2143,7 @@ public: return ControlDown(); #endif } - + virtual wxEvent *Clone() const { return new wxGridSizeEvent(*this); } protected: @@ -2182,7 +2200,7 @@ public: return ControlDown(); #endif } - + virtual wxEvent *Clone() const { return new wxGridRangeSelectEvent(*this); } protected: @@ -2217,7 +2235,7 @@ public: void SetRow(int row) { m_row = row; } void SetCol(int col) { m_col = col; } void SetControl(wxControl* ctrl) { m_ctrl = ctrl; } - + virtual wxEvent *Clone() const { return new wxGridEditorCreatedEvent(*this); } private: diff --git a/samples/grid/griddemo.cpp b/samples/grid/griddemo.cpp index 85867844af..d6ceb21e3b 100644 --- a/samples/grid/griddemo.cpp +++ b/samples/grid/griddemo.cpp @@ -112,6 +112,14 @@ BEGIN_EVENT_TABLE( GridFrame, wxFrame ) EVT_MENU( ID_SELECT_UNSELECT, GridFrame::OnAddToSelectToggle) EVT_MENU( ID_SHOW_SELECTION, GridFrame::OnShowSelection) + EVT_MENU( ID_SIZE_ROW, GridFrame::AutoSizeRow ) + EVT_MENU( ID_SIZE_COL, GridFrame::AutoSizeCol ) + EVT_MENU( ID_SIZE_ROW_LABEL, GridFrame::AutoSizeRowLabel ) + EVT_MENU( ID_SIZE_COL_LABEL, GridFrame::AutoSizeColLabel ) + EVT_MENU( ID_SIZE_LABELS_COL, GridFrame::AutoSizeLabelsCol ) + EVT_MENU( ID_SIZE_LABELS_ROW, GridFrame::AutoSizeLabelsRow ) + EVT_MENU( ID_SIZE_GRID, GridFrame::AutoSizeTable ) + EVT_MENU( ID_SET_HIGHLIGHT_WIDTH, GridFrame::OnSetHighlightWidth) EVT_MENU( ID_SET_RO_HIGHLIGHT_WIDTH, GridFrame::OnSetROHighlightWidth) @@ -215,6 +223,14 @@ GridFrame::GridFrame() selectionMenu->Append( ID_SELROWS, _T("Select &Rows") ); selectionMenu->Append( ID_SELCOLS, _T("Select C&ols") ); + wxMenu *autosizeMenu = new wxMenu; + autosizeMenu->Append( ID_SIZE_ROW, _T("Selected &row data") ); + autosizeMenu->Append( ID_SIZE_COL, _T("Selected &column data") ); + autosizeMenu->Append( ID_SIZE_ROW_LABEL, _T("Selected row la&bel") ); + autosizeMenu->Append( ID_SIZE_COL_LABEL, _T("Selected column &label") ); + autosizeMenu->Append( ID_SIZE_LABELS_COL, _T("Column la&bels") ); + autosizeMenu->Append( ID_SIZE_LABELS_ROW, _T("Row label&s") ); + autosizeMenu->Append( ID_SIZE_GRID, _T("Entire &grid") ); wxMenu *helpMenu = new wxMenu; helpMenu->Append( wxID_ABOUT, _T("&About wxGrid demo") ); @@ -225,6 +241,7 @@ GridFrame::GridFrame() menuBar->Append( colMenu, _T("&Colours") ); menuBar->Append( editMenu, _T("&Edit") ); menuBar->Append( selectMenu, _T("&Select") ); + menuBar->Append( autosizeMenu, _T("&Autosize") ); menuBar->Append( helpMenu, _T("&Help") ); SetMenuBar( menuBar ); @@ -680,6 +697,62 @@ void GridFrame::DeleteSelectedRows( wxCommandEvent& WXUNUSED(ev) ) } +void GridFrame::AutoSizeRow(wxCommandEvent& WXUNUSED(event)) +{ + wxGridUpdateLocker locker(grid); + const wxArrayInt sels = grid->GetSelectedRows(); + for ( size_t n = 0, count = sels.size(); n < count; n++ ) + { + grid->AutoSizeRow( sels[n], false ); + } +} + +void GridFrame::AutoSizeCol(wxCommandEvent& WXUNUSED(event)) +{ + wxGridUpdateLocker locker(grid); + const wxArrayInt sels = grid->GetSelectedCols(); + for ( size_t n = 0, count = sels.size(); n < count; n++ ) + { + grid->AutoSizeColumn( sels[n], false ); + } +} + +void GridFrame::AutoSizeRowLabel(wxCommandEvent& WXUNUSED(event)) +{ + wxGridUpdateLocker locker(grid); + const wxArrayInt sels = grid->GetSelectedRows(); + for ( size_t n = 0, count = sels.size(); n < count; n++ ) + { + grid->AutoSizeRowLabelSize( sels[n] ); + } +} + +void GridFrame::AutoSizeColLabel(wxCommandEvent& WXUNUSED(event)) +{ + wxGridUpdateLocker locker(grid); + const wxArrayInt sels = grid->GetSelectedCols(); + for ( size_t n = 0, count = sels.size(); n < count; n++ ) + { + grid->AutoSizeColLabelSize( sels[n] ); + } +} + +void GridFrame::AutoSizeLabelsCol(wxCommandEvent& WXUNUSED(event)) +{ + grid->SetColLabelSize( wxGRID_AUTOSIZE ); +} + +void GridFrame::AutoSizeLabelsRow(wxCommandEvent& WXUNUSED(event)) +{ + grid->SetRowLabelSize( wxGRID_AUTOSIZE ); +} + +void GridFrame::AutoSizeTable(wxCommandEvent& WXUNUSED(event)) +{ + grid->AutoSize(); +} + + void GridFrame::DeleteSelectedCols( wxCommandEvent& WXUNUSED(ev) ) { if ( grid->IsSelection() ) diff --git a/samples/grid/griddemo.h b/samples/grid/griddemo.h index 285a4ac23e..ba0b549477 100644 --- a/samples/grid/griddemo.h +++ b/samples/grid/griddemo.h @@ -75,6 +75,14 @@ class GridFrame : public wxFrame void OnAddToSelectToggle(wxCommandEvent& event); void OnShowSelection(wxCommandEvent& event); + void AutoSizeRow(wxCommandEvent& event); + void AutoSizeCol(wxCommandEvent& event); + void AutoSizeRowLabel(wxCommandEvent& event); + void AutoSizeColLabel(wxCommandEvent& event); + void AutoSizeLabelsCol(wxCommandEvent& event); + void AutoSizeLabelsRow(wxCommandEvent& event); + void AutoSizeTable(wxCommandEvent& event); + void OnLabelLeftClick( wxGridEvent& ); void OnCellLeftClick( wxGridEvent& ); void OnRowSize( wxGridSizeEvent& ); @@ -148,6 +156,13 @@ public: ID_DESELECT_ROW, ID_DESELECT_COL, ID_DESELECT_CELL, + ID_SIZE_ROW, + ID_SIZE_COL, + ID_SIZE_ROW_LABEL, + ID_SIZE_COL_LABEL, + ID_SIZE_LABELS_COL, + ID_SIZE_LABELS_ROW, + ID_SIZE_GRID, ID_SET_HIGHLIGHT_WIDTH, ID_SET_RO_HIGHLIGHT_WIDTH, diff --git a/src/generic/grid.cpp b/src/generic/grid.cpp index e8ef3ce6dd..8fb7e5197b 100644 --- a/src/generic/grid.cpp +++ b/src/generic/grid.cpp @@ -9409,7 +9409,13 @@ wxString wxGrid::GetColLabelValue( int col ) const void wxGrid::SetRowLabelSize( int width ) { - width = wxMax( width, 0 ); + wxASSERT( width >= 0 || width == wxGRID_AUTOSIZE ); + + if ( width == wxGRID_AUTOSIZE ) + { + width = CalcColOrRowLabelAreaMinSize(wxGRID_ROW); + } + if ( width != m_rowLabelWidth ) { if ( width == 0 ) @@ -9432,7 +9438,13 @@ void wxGrid::SetRowLabelSize( int width ) void wxGrid::SetColLabelSize( int height ) { - height = wxMax( height, 0 ); + wxASSERT( height >=0 || height == wxGRID_AUTOSIZE ); + + if ( height == wxGRID_AUTOSIZE ) + { + height = CalcColOrRowLabelAreaMinSize(wxGRID_COLUMN); + } + if ( height != m_colLabelHeight ) { if ( height == 0 ) @@ -10479,8 +10491,11 @@ int wxGrid::GetRowMinimalAcceptableHeight() const // auto sizing // ---------------------------------------------------------------------------- -void wxGrid::AutoSizeColOrRow( int colOrRow, bool setAsMin, bool column ) +void +wxGrid::AutoSizeColOrRow(int colOrRow, bool setAsMin, wxGridDirection direction) { + const bool column = direction == wxGRID_COLUMN; + wxClientDC dc(m_gridWin); // cancel editing of cell @@ -10591,6 +10606,58 @@ void wxGrid::AutoSizeColOrRow( int colOrRow, bool setAsMin, bool column ) } } +wxCoord wxGrid::CalcColOrRowLabelAreaMinSize(wxGridDirection direction) +{ + // calculate size for the rows or columns? + const bool calcRows = direction == wxGRID_ROW; + + wxClientDC dc(calcRows ? GetGridRowLabelWindow() + : GetGridColLabelWindow()); + dc.SetFont(GetLabelFont()); + + // which dimension should we take into account for calculations? + // + // for columns, the text can be only horizontal so it's easy but for rows + // we also have to take into account the text orientation + const bool + useWidth = calcRows || (GetColLabelTextOrientation() == wxVERTICAL); + + wxArrayString lines; + wxCoord extentMax = 0; + + const int numRowsOrCols = calcRows ? m_numRows : m_numCols; + for ( int rowOrCol = 0; rowOrCol < numRowsOrCols; rowOrCol++ ) + { + lines.Clear(); + StringToLines(calcRows ? GetRowLabelValue(rowOrCol) + : GetColLabelValue(rowOrCol), + lines); + + long w, h; + GetTextBoxSize(dc, lines, &w, &h); + + const wxCoord extent = useWidth ? w : h; + if ( extent > extentMax ) + extentMax = extent; + } + + if ( !extentMax ) + { + // empty column - give default extent (notice that if extentMax is less + // than default extent but != 0, it's OK) + extentMax = calcRows ? GetDefaultRowLabelSize() + : GetDefaultColLabelSize(); + } + + // leave some space around text (taken from AutoSizeColOrRow) + if ( calcRows ) + extentMax += 10; + else + extentMax += 6; + + return extentMax; +} + int wxGrid::SetOrCalcColumnSizes(bool calcOnly, bool setAsMin) { int width = m_rowLabelWidth; -- 2.45.2