From 283b7808d81dcf4cf97b268ec914796c0e206732 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Wed, 16 Feb 2000 21:10:45 +0000 Subject: [PATCH] added support for readonly cells and 3d border drawing git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@6094 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- include/wx/generic/grid.h | 24 ++++++++-- samples/newgrid/griddemo.cpp | 6 ++- src/generic/grid.cpp | 91 ++++++++++++++++++++++++++++++++---- 3 files changed, 107 insertions(+), 14 deletions(-) diff --git a/include/wx/generic/grid.h b/include/wx/generic/grid.h index 1ab8e0d265..e850045204 100644 --- a/include/wx/generic/grid.h +++ b/include/wx/generic/grid.h @@ -206,6 +206,8 @@ public: SetAlignment(0, 0); } + // VZ: considering the number of members wxGridCellAttr has now, this ctor + // seems to be pretty useless... may be we should just remove it? wxGridCellAttr(const wxColour& colText, const wxColour& colBack, const wxFont& font, @@ -236,6 +238,7 @@ public: m_hAlign = hAlign; m_vAlign = vAlign; } + void SetReadOnly(bool isReadOnly = TRUE) { m_isReadOnly = isReadOnly; } // takes ownership of the pointer void SetRenderer(wxGridCellRenderer *renderer) @@ -258,12 +261,18 @@ public: wxGridCellRenderer *GetRenderer() const; wxGridCellEditor *GetEditor() const; + bool IsReadOnly() const { return m_isReadOnly; } + void SetDefAttr(wxGridCellAttr* defAttr) { m_defGridAttr = defAttr; } private: // the common part of all ctors - void Init() { + void Init() + { m_nRef = 1; + + m_isReadOnly = FALSE; + m_renderer = NULL; m_editor = NULL; } @@ -284,6 +293,8 @@ private: wxGridCellEditor* m_editor; wxGridCellAttr* m_defGridAttr; + bool m_isReadOnly; + // suppress the stupid gcc warning about the class having private dtor and // no friends friend class wxGridCellAttrDummyFriend; @@ -624,7 +635,7 @@ public: void DrawCellBorder( wxDC& dc, const wxGridCellCoords& ); void DrawAllGridLines( wxDC& dc, const wxRegion & reg ); void DrawCell( wxDC& dc, const wxGridCellCoords& ); - void DrawCellHighlight( wxDC& dc ); + void DrawCellHighlight( wxDC& dc, const wxGridCellAttr *attr ); void DrawRowLabels( wxDC& dc ); void DrawRowLabel( wxDC& dc, int row ); @@ -666,8 +677,7 @@ public: void EnableCellEditControl( bool enable ); - bool IsCellEditControlEnabled() - { return m_cellEditCtrlEnabled; } + bool IsCellEditControlEnabled(); void ShowCellEditControl(); void HideCellEditControl(); @@ -791,7 +801,7 @@ public: wxGridCellRenderer *GetDefaultRenderer() const; wxGridCellRenderer* GetCellRenderer(int row, int col); - // takes ownership of the pointer + // takes ownership of the pointer void SetDefaultEditor(wxGridCellEditor *editor); void SetCellEditor(int row, int col, wxGridCellEditor *editor); wxGridCellEditor *GetDefaultEditor() const; @@ -819,7 +829,11 @@ public: void SetCellValue( const wxGridCellCoords& coords, const wxString& s ) { SetCellValue( coords.GetRow(), coords.GetCol(), s ); } + // returns TRUE if the cell can't be edited + bool IsReadOnly(int row, int col) const; + // make the cell editable/readonly + void SetReadOnly(int row, int col, bool isReadOnly = TRUE); // ------ selections of blocks of cells // diff --git a/samples/newgrid/griddemo.cpp b/samples/newgrid/griddemo.cpp index bd333d2b25..ed6bc2480a 100644 --- a/samples/newgrid/griddemo.cpp +++ b/samples/newgrid/griddemo.cpp @@ -95,7 +95,6 @@ BEGIN_EVENT_TABLE( GridFrame, wxFrame ) EVT_GRID_SELECT_CELL( GridFrame::OnSelectCell ) EVT_GRID_RANGE_SELECT( GridFrame::OnRangeSelected ) EVT_GRID_CELL_CHANGE( GridFrame::OnCellValueChanged ) - END_EVENT_TABLE() @@ -187,6 +186,8 @@ GridFrame::GridFrame() grid->SetCellValue( 0, 1, "Blah" ); grid->SetCellValue( 0, 2, "Blah" ); + grid->SetCellValue( 0, 3, "Read only" ); + grid->SetReadOnly( 0, 3 ); grid->SetCellValue( 0, 5, "Press\nCtrl+arrow\nto skip over\ncells" ); @@ -212,6 +213,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)); + wxBoxSizer *topSizer = new wxBoxSizer( wxVERTICAL ); topSizer->Add( grid, 1, diff --git a/src/generic/grid.cpp b/src/generic/grid.cpp index 7e0d05e7d3..9201252056 100644 --- a/src/generic/grid.cpp +++ b/src/generic/grid.cpp @@ -546,6 +546,8 @@ void wxGridCellStringRenderer::Draw(wxGrid& grid, // now we only have to draw the text dc.SetBackgroundMode( wxTRANSPARENT ); + // TODO some special colours for attr.IsReadOnly() case? + if ( isSelected ) { dc.SetTextBackground( grid.GetSelectionBackground() ); @@ -3780,7 +3782,10 @@ void wxGrid::SetCurrentCell( const wxGridCellCoords& coords ) { wxClientDC dc(m_gridWin); PrepareDC(dc); - DrawCellHighlight(dc); + + wxGridCellAttr* attr = GetCellAttr(coords); + DrawCellHighlight(dc, attr); + attr->DecRef(); if ( IsSelection() ) { @@ -3874,14 +3879,14 @@ void wxGrid::DrawCell( wxDC& dc, const wxGridCellCoords& coords ) wxGridCellAttr* attr = GetCellAttr(row, col); attr->GetRenderer()->Draw(*this, *attr, dc, rect, row, col, IsInSelection(coords)); - attr->DecRef(); if (m_currentCellCoords == coords) - DrawCellHighlight(dc); -} + DrawCellHighlight(dc, attr); + attr->DecRef(); +} -void wxGrid::DrawCellHighlight( wxDC& dc ) +void wxGrid::DrawCellHighlight( wxDC& dc, const wxGridCellAttr *attr ) { int row = m_currentCellCoords.GetRow(); int col = m_currentCellCoords.GetCol(); @@ -3895,10 +3900,44 @@ void wxGrid::DrawCellHighlight( wxDC& dc ) rect.width = m_colWidths[col] - 1; rect.height = m_rowHeights[row] - 1; - dc.SetPen(wxPen(m_gridLineColour, 3, wxSOLID)); - dc.SetBrush(*wxTRANSPARENT_BRUSH); + if ( attr->IsReadOnly() ) + { + // hmmm... what could we do here to show that the cell is disabled? + // for now, I just draw a thinner border than for the other ones, but + // it doesn't look really good + dc.SetPen(wxPen(m_gridLineColour, 2, wxSOLID)); + dc.SetBrush(*wxTRANSPARENT_BRUSH); - dc.DrawRectangle(rect); + dc.DrawRectangle(rect); + } + else + { + // VZ: my experiments with 3d borders... +#if 0 + dc.SetPen(wxPen(m_gridLineColour, 3, wxSOLID)); + dc.SetBrush(*wxTRANSPARENT_BRUSH); + + dc.DrawRectangle(rect); +#else //1 + // FIXME we should properly set colours for arbitrary bg + wxCoord x1 = rect.x, + y1 = rect.y, + x2 = rect.x + rect.width, + y2 = rect.y + rect.height; + + dc.SetPen(*wxWHITE_PEN); + dc.DrawLine(x1, y1, x2 - 1, y1); + dc.DrawLine(x1, y1, x1, y2 - 1); + + dc.SetPen(*wxLIGHT_GREY_PEN); + dc.DrawLine(x1 + 1, y2 - 1, x2 - 1, y2 - 1); + dc.DrawLine(x2 - 1, y1 + 1, x2 - 1, y2 - 1); + + dc.SetPen(*wxBLACK_PEN); + dc.DrawLine(x1, y2, x2, y2); + dc.DrawLine(x2, y1, x2, y2); +#endif // 0/1 + } } void wxGrid::DrawCellBorder( wxDC& dc, const wxGridCellCoords& coords ) @@ -4247,6 +4286,24 @@ void wxGrid::EnableCellEditControl( bool enable ) } +bool wxGrid::IsCellEditControlEnabled() +{ + bool enabled; + + if ( m_cellEditCtrlEnabled ) + { + wxGridCellAttr* attr = GetCellAttr(m_currentCellCoords); + enabled = !attr->IsReadOnly(); + attr->DecRef(); + } + else + { + enabled = FALSE; + } + + return enabled; +} + void wxGrid::ShowCellEditControl() { if ( IsCellEditControlEnabled() ) @@ -5344,6 +5401,14 @@ wxGridCellEditor* wxGrid::GetCellEditor(int row, int col) return editor; } +bool wxGrid::IsReadOnly(int row, int col) const +{ + wxGridCellAttr* attr = GetCellAttr(row, col); + bool isReadOnly = attr->IsReadOnly(); + attr->DecRef(); + return isReadOnly; +} + // ---------------------------------------------------------------------------- // attribute support: cache, automatic provider creation, ... // ---------------------------------------------------------------------------- @@ -5544,6 +5609,16 @@ void wxGrid::SetCellEditor(int row, int col, wxGridCellEditor* editor) } } +void wxGrid::SetReadOnly(int row, int col, bool isReadOnly) +{ + if ( CanHaveAttributes() ) + { + wxGridCellAttr *attr = GetOrCreateCellAttr(row, col); + attr->SetReadOnly(isReadOnly); + attr->DecRef(); + } +} + // ---------------------------------------------------------------------------- // row/col size // ---------------------------------------------------------------------------- -- 2.45.2