From 439479796a09c87993223c043553ccdc6cd66aed Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Mon, 21 Feb 2000 18:01:45 +0000 Subject: [PATCH] added SetColMinimalWidth() git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@6184 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- include/wx/generic/grid.h | 36 +++++++++++++++++++++++------------- samples/newgrid/griddemo.cpp | 7 ++++--- src/generic/grid.cpp | 35 ++++++++++++++++++++++++++++++----- 3 files changed, 57 insertions(+), 21 deletions(-) diff --git a/include/wx/generic/grid.h b/include/wx/generic/grid.h index 5089cc7f55..33cb963168 100644 --- a/include/wx/generic/grid.h +++ b/include/wx/generic/grid.h @@ -1,5 +1,5 @@ ///////////////////////////////////////////////////////////////////////////// -// Name: grid.h +// Name: wx/generic/grid.h // Purpose: wxGrid and related classes // Author: Michael Bedward (based on code by Julian Smart, Robin Dunn) // Modified by: @@ -22,6 +22,7 @@ #pragma interface "grid.h" #endif +#include "wx/hash.h" #include "wx/panel.h" #include "wx/scrolwin.h" #include "wx/string.h" @@ -559,11 +560,13 @@ private: -////////////////////////////////////////////////////////////////////// -// +// ============================================================================ // Grid view classes -// -////////////////////////////////////////////////////////////////////// +// ============================================================================ + +// ---------------------------------------------------------------------------- +// wxGridCellCoords: location of a cell in the grid +// ---------------------------------------------------------------------------- class WXDLLEXPORT wxGridCellCoords { @@ -619,8 +622,6 @@ extern wxRect wxGridNoCellRect; // WX_DECLARE_EXPORTED_OBJARRAY(wxGridCellCoords, wxGridCellCoordsArray); - - // ---------------------------------------------------------------------------- // wxGrid // ---------------------------------------------------------------------------- @@ -842,6 +843,12 @@ public: void SetDefaultColSize( int width, bool resizeExistingCols = FALSE ); void SetColSize( int col, int width ); + + // column won't be resized to be lesser width - this must be called during + // the grid creation because it won't resize the column if it's already + // narrower than the minimal width + void SetColMinimalWidth( int col, int width ); + void SetDefaultCellBackgroundColour( const wxColour& ); void SetCellBackgroundColour( int row, int col, const wxColour& ); void SetDefaultCellTextColour( const wxColour& ); @@ -1200,6 +1207,12 @@ protected: wxColour m_gridLineColour; bool m_gridLinesEnabled; + // if a column has a minimal width, it will be the value for it in this + // hash table + wxHashTable m_colMinWidths; + + // get the minimal width of the given column + int GetColMinimalWidth(int col) const; // do we have some place to store attributes in? bool CanHaveAttributes(); @@ -1322,12 +1335,9 @@ protected: }; - - - -// -// ------ Grid event class and event types -// +// ---------------------------------------------------------------------------- +// Grid event class and event types +// ---------------------------------------------------------------------------- class WXDLLEXPORT wxGridEvent : public wxNotifyEvent { diff --git a/samples/newgrid/griddemo.cpp b/samples/newgrid/griddemo.cpp index 0b31be5e19..212e068c3d 100644 --- a/samples/newgrid/griddemo.cpp +++ b/samples/newgrid/griddemo.cpp @@ -110,7 +110,7 @@ GridFrame::GridFrame() int logW = gridW, logH = 100; wxMenu *fileMenu = new wxMenu; - fileMenu->Append( ID_VTABLE, "&Virtual table test"); + fileMenu->Append( ID_VTABLE, "&Virtual table test\tCtrl-V"); fileMenu->AppendSeparator(); fileMenu->Append( wxID_EXIT, "E&xit\tAlt-X" ); @@ -220,8 +220,9 @@ GridFrame::GridFrame() attr->SetBackgroundColour(*wxBLUE); grid->SetRowAttr(5, attr); - // VZ: cell borders don't look nice otherwise :-) (for now...) - grid->SetDefaultCellBackgroundColour(wxColour(200, 200, 180)); + grid->SetCellValue(2, 4, "a wider column"); + grid->SetColSize(4, 120); + grid->SetColMinimalWidth(4, 120); wxBoxSizer *topSizer = new wxBoxSizer( wxVERTICAL ); topSizer->Add( grid, diff --git a/src/generic/grid.cpp b/src/generic/grid.cpp index c04690a1e9..623d484c3b 100644 --- a/src/generic/grid.cpp +++ b/src/generic/grid.cpp @@ -48,7 +48,6 @@ #include "wx/grid.h" - // ---------------------------------------------------------------------------- // array classes // ---------------------------------------------------------------------------- @@ -267,6 +266,10 @@ public: static size_t gs_nAttrCacheMisses = 0; #endif // DEBUG_ATTR_CACHE +// ---------------------------------------------------------------------------- +// constants +// ---------------------------------------------------------------------------- + wxGridCellCoords wxGridNoCellCoords( -1, -1 ); wxRect wxGridNoCellRect( -1, -1, -1, -1 ); @@ -274,6 +277,10 @@ wxRect wxGridNoCellRect( -1, -1, -1, -1 ); // TODO: fixed so far - make configurable later (and also different for x/y) static const size_t GRID_SCROLL_LINE = 10; +// the size of hash tables used a bit everywhere (the max number of elements +// in these hash tables is the number of rows/columns) +static const int GRID_HASH_SIZE = 100; + // ============================================================================ // implementation // ============================================================================ @@ -2020,7 +2027,8 @@ wxGrid::wxGrid( wxWindow *parent, const wxSize& size, long style, const wxString& name ) - : wxScrolledWindow( parent, id, pos, size, style, name ) + : wxScrolledWindow( parent, id, pos, size, style, name ), + m_colMinWidths(wxKEY_INTEGER, GRID_HASH_SIZE) { Create(); } @@ -2887,7 +2895,9 @@ void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent& event ) wxClientDC dc( m_gridWin ); PrepareDC( dc ); - x = wxMax( x, GetColLeft(m_dragRowOrCol) + WXGRID_MIN_COL_WIDTH ); + + x = wxMax( x, GetColLeft(m_dragRowOrCol) + + GetColMinimalWidth(m_dragRowOrCol)); dc.SetLogicalFunction(wxINVERT); if ( m_dragLastPos >= 0 ) { @@ -3212,7 +3222,8 @@ void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent& event ) wxClientDC dc( m_gridWin ); PrepareDC( dc ); - x = wxMax( x, GetColLeft(m_dragRowOrCol) + WXGRID_MIN_COL_WIDTH ); + x = wxMax( x, GetColLeft(m_dragRowOrCol) + + GetColMinimalWidth(m_dragRowOrCol) ); dc.SetLogicalFunction(wxINVERT); if ( m_dragLastPos >= 0 ) { @@ -3482,7 +3493,8 @@ void wxGrid::DoEndDragResizeCol() int colLeft = GetColLeft(m_dragRowOrCol); SetColSize( m_dragRowOrCol, - wxMax( m_dragLastPos - colLeft, WXGRID_MIN_COL_WIDTH ) ); + wxMax( m_dragLastPos - colLeft, + GetColMinimalWidth(m_dragRowOrCol) ) ); if ( !GetBatchCount() ) { @@ -6035,6 +6047,8 @@ void wxGrid::SetColSize( int col, int width ) { wxCHECK_RET( col >= 0 && col < m_numCols, _T("invalid column index") ); + // should we check that it's bigger than GetColMinimalWidth(col) here? + if ( m_colWidths.IsEmpty() ) { // need to really create the array @@ -6054,6 +6068,17 @@ void wxGrid::SetColSize( int col, int width ) } +void wxGrid::SetColMinimalWidth( int col, int width ) +{ + m_colMinWidths.Put(col, (wxObject *)width); +} + +int wxGrid::GetColMinimalWidth(int col) const +{ + wxObject *obj = m_colMinWidths.Get(m_dragRowOrCol); + return obj ? (int)obj : WXGRID_MIN_COL_WIDTH; +} + // // ------ cell value accessor functions // -- 2.45.2