From d10f4bf950c4ad064b982fe8fe07d5ca5e3af7ba Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sun, 27 May 2001 15:22:28 +0000 Subject: [PATCH] applied tons of wxGrid patches git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@10352 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- distrib/msw/tmake/filelist.txt | 3 + include/wx/generic/grid.h | 27 ++- include/wx/generic/gridctrl.h | 150 +++++++++++++ samples/newgrid/griddemo.cpp | 11 +- samples/newgrid/griddemo.h | 2 +- src/generic/grid.cpp | 101 +++++---- src/generic/gridctrl.cpp | 381 +++++++++++++++++++++++++++++++++ 7 files changed, 613 insertions(+), 62 deletions(-) create mode 100644 include/wx/generic/gridctrl.h create mode 100644 src/generic/gridctrl.cpp diff --git a/distrib/msw/tmake/filelist.txt b/distrib/msw/tmake/filelist.txt index fac1e049c7..23403c1c87 100644 --- a/distrib/msw/tmake/filelist.txt +++ b/distrib/msw/tmake/filelist.txt @@ -71,6 +71,7 @@ dragimgg.cpp G P fontdlgg.cpp G G,R,P filedlgg.cpp G U,X,P grid.cpp G +gridctrl.cpp G gridsel.cpp G helpext.cpp G G helphtml.cpp G G @@ -630,6 +631,7 @@ gifdecod.h W xpmdecod.h W glcanvas.h W grid.h W +gridctrl.h W gsocket.h W B hash.h W B help.h W @@ -1112,6 +1114,7 @@ dragimgg.h N 16 fontdlgg.h N filedlgg.h N grid.h N +gridctrl.h N gridg.h N helpext.h N helpwxht.h N diff --git a/include/wx/generic/grid.h b/include/wx/generic/grid.h index 02c593a38a..13a06d6f55 100644 --- a/include/wx/generic/grid.h +++ b/include/wx/generic/grid.h @@ -1027,10 +1027,10 @@ public: // ------ display update functions // - void CalcRowLabelsExposed( const wxRegion& reg ); + wxArrayInt CalcRowLabelsExposed( const wxRegion& reg ); - void CalcColLabelsExposed( const wxRegion& reg ); - void CalcCellsExposed( const wxRegion& reg ); + wxArrayInt CalcColLabelsExposed( const wxRegion& reg ); + wxGridCellCoordsArray CalcCellsExposed( const wxRegion& reg ); // ------ event handlers @@ -1057,21 +1057,21 @@ public: bool AppendCols( int numCols = 1, bool updateLabels=TRUE ); bool DeleteCols( int pos = 0, int numCols = 1, bool updateLabels=TRUE ); - void DrawGridCellArea( wxDC& dc ); + void DrawGridCellArea( wxDC& dc , const wxGridCellCoordsArray& cells ); void DrawGridSpace( wxDC& dc ); void DrawCellBorder( wxDC& dc, const wxGridCellCoords& ); void DrawAllGridLines( wxDC& dc, const wxRegion & reg ); void DrawCell( wxDC& dc, const wxGridCellCoords& ); - void DrawHighlight(wxDC& dc); + void DrawHighlight(wxDC& dc, const wxGridCellCoordsArray& cells); // this function is called when the current cell highlight must be redrawn // and may be overridden by the user virtual void DrawCellHighlight( wxDC& dc, const wxGridCellAttr *attr ); - void DrawRowLabels( wxDC& dc ); + void DrawRowLabels( wxDC& dc, const wxArrayInt& rows ); void DrawRowLabel( wxDC& dc, int row ); - void DrawColLabels( wxDC& dc ); + void DrawColLabels( wxDC& dc, const wxArrayInt& cols ); void DrawColLabel( wxDC& dc, int col ); @@ -1080,6 +1080,11 @@ public: void DrawTextRectangle( wxDC& dc, const wxString&, const wxRect&, int horizontalAlignment = wxALIGN_LEFT, int verticalAlignment = wxALIGN_TOP ); + + void DrawTextRectangle( wxDC& dc, const wxArrayString& lines, const wxRect&, + int horizontalAlignment = wxALIGN_LEFT, + int verticalAlignment = wxALIGN_TOP ); + // Split a string containing newline chararcters into an array of // strings and return the number of lines @@ -1087,7 +1092,7 @@ public: void StringToLines( const wxString& value, wxArrayString& lines ); void GetTextBoxSize( wxDC& dc, - wxArrayString& lines, + const wxArrayString& lines, long *width, long *height ); @@ -1704,12 +1709,6 @@ protected: wxGridCellAttr* m_defaultCellAttr; - wxGridCellCoordsArray m_cellsExposed; - wxArrayInt m_rowsExposed; - wxArrayInt m_colsExposed; - wxArrayInt m_rowLabelsExposed; - wxArrayInt m_colLabelsExposed; - bool m_inOnKeyDown; int m_batchCount; diff --git a/include/wx/generic/gridctrl.h b/include/wx/generic/gridctrl.h new file mode 100644 index 0000000000..9a87f635ff --- /dev/null +++ b/include/wx/generic/gridctrl.h @@ -0,0 +1,150 @@ +/////////////////////////////////////////////////////////////////////////// +// Name: generic/gridctrl.h +// Purpose: wxGrid controls +// Author: Paul Gammans, Roger Gammans +// Modified by: +// Created: 11/04/2001 +// RCS-ID: $Id$ +// Copyright: (c) The Computer Surgery (paul@compsurg.co.uk) +// Licence: wxWindows licence +///////////////////////////////////////////////////////////////////////////// + +#ifndef _WX_GENERIC_GRIDCTRL_H_ +#define _WX_GENERIC_GRIDCTRL_H_ + +#ifdef __GNUG__ + #pragma interface "gridctrl.h" +#endif + +#include "wx/grid.h" +#include "wx/string.h" +#include "wx/datetime.h" + +#define wxGRID_VALUE_CHOICEINT _T("choiceint") +#define wxGRID_VALUE_DATETIME _T("datetime") + +// the default renderer for the cells containing Time and dates.. +class WXDLLEXPORT wxGridCellDateTimeRenderer : public wxGridCellStringRenderer +{ +public: + wxGridCellDateTimeRenderer(wxString outformat = _T("%c"), + wxString informat = _T("%c")); + + // draw the string right aligned + virtual void Draw(wxGrid& grid, + wxGridCellAttr& attr, + wxDC& dc, + const wxRect& rect, + int row, int col, + bool isSelected); + + virtual wxSize GetBestSize(wxGrid& grid, + wxGridCellAttr& attr, + wxDC& dc, + int row, int col); + + virtual wxGridCellRenderer *Clone() const; + + // parameters string format is "width[,precision]" + virtual void SetParameters(const wxString& params); + +protected: + wxString GetString(wxGrid& grid, int row, int col); + + wxString m_iformat; + wxString m_oformat; + wxDateTime m_dateDef; + wxDateTime::TimeZone m_tz; +}; + + +// the default renderer for the cells containing Time and dates.. +class WXDLLEXPORT wxGridCellEnumRenderer : public wxGridCellStringRenderer +{ +public: + wxGridCellEnumRenderer( const wxString& choices = wxEmptyString ); + + // draw the string right aligned + virtual void Draw(wxGrid& grid, + wxGridCellAttr& attr, + wxDC& dc, + const wxRect& rect, + int row, int col, + bool isSelected); + + virtual wxSize GetBestSize(wxGrid& grid, + wxGridCellAttr& attr, + wxDC& dc, + int row, int col); + + virtual wxGridCellRenderer *Clone() const; + + // parameters string format is "item1[,item2[...,itemN]]" + virtual void SetParameters(const wxString& params); + +protected: + wxString GetString(wxGrid& grid, int row, int col); + + wxArrayString m_choices; +}; + + +class WXDLLEXPORT wxGridCellEnumEditor : public wxGridCellChoiceEditor +{ +public: + wxGridCellEnumEditor( const wxString& choices = wxEmptyString ); + virtual ~wxGridCellEnumEditor() {}; + + virtual wxGridCellEditor* Clone() const; + + virtual bool EndEdit(int row, int col, wxGrid* grid); + virtual void BeginEdit(int row, int col, wxGrid* grid); + +private: + long int m_startint; +}; + + +class wxGridCellAutoWrapStringEditor : public wxGridCellTextEditor +{ +public: + wxGridCellAutoWrapStringEditor() : wxGridCellTextEditor() { } + virtual void Create(wxWindow* parent, + wxWindowID id, + wxEvtHandler* evtHandler); + + virtual wxGridCellEditor *Clone() const + { return new wxGridCellAutoWrapStringEditor; } +}; + +class wxGridCellAutoWrapStringRenderer : public wxGridCellStringRenderer +{ +public: + wxGridCellAutoWrapStringRenderer() : wxGridCellStringRenderer() { } + + virtual void Draw(wxGrid& grid, + wxGridCellAttr& attr, + wxDC& dc, + const wxRect& rect, + int row, int col, + bool isSelected); + + virtual wxSize GetBestSize(wxGrid& grid, + wxGridCellAttr& attr, + wxDC& dc, + int row, int col); + + virtual wxGridCellRenderer *Clone() const + { return new wxGridCellAutoWrapStringRenderer; } + +private: + wxArrayString GetTextLines( wxGrid& grid, + wxDC& dc, + wxGridCellAttr& attr, + const wxRect& rect, + int row, int col); + +}; + +#endif //_WX_GENERIC_GRIDCTRL_H_ + diff --git a/samples/newgrid/griddemo.cpp b/samples/newgrid/griddemo.cpp index 6d8e208c06..c8d022ef55 100644 --- a/samples/newgrid/griddemo.cpp +++ b/samples/newgrid/griddemo.cpp @@ -35,6 +35,7 @@ #include "wx/colordlg.h" #include "wx/grid.h" +#include "wx/generic/gridctrl.h" #include "griddemo.h" @@ -241,7 +242,10 @@ GridFrame::GridFrame() grid->SetRowSize( 0, 60 ); grid->SetCellValue( 0, 0, "Ctrl+Home\nwill go to\nthis cell" ); - grid->SetCellValue( 0, 1, "Blah" ); + grid->SetCellValue( 0, 1, "A long piece of text to demonstrate wrapping." ); + grid->SetCellRenderer(0 , 1, new wxGridCellAutoWrapStringRenderer); + grid->SetCellEditor( 0, 1 , new wxGridCellAutoWrapStringEditor); + grid->SetCellValue( 0, 2, "Blah" ); grid->SetCellValue( 0, 3, "Read only" ); grid->SetReadOnly( 0, 3 ); @@ -1257,10 +1261,7 @@ BugsGridFrame::BugsGridFrame() grid->SetMargins(0, 0); grid->Fit(); - wxSize size = grid->GetSize(); - size.x += 10; - size.y += 10; - SetClientSize(size); + SetClientSize(grid->GetSize()); } diff --git a/samples/newgrid/griddemo.h b/samples/newgrid/griddemo.h index 48f5d77870..d10a53a2f3 100644 --- a/samples/newgrid/griddemo.h +++ b/samples/newgrid/griddemo.h @@ -208,7 +208,7 @@ public: virtual ~MyGridCellAttrProvider(); virtual wxGridCellAttr *GetAttr(int row, int col, - wxGridCellAttr::wxAttrKind kind /* = wxGridCellAttr::Any */) const; + wxGridCellAttr::wxAttrKind kind) const; private: wxGridCellAttr *m_attrForOddRows; diff --git a/src/generic/grid.cpp b/src/generic/grid.cpp index 4957e2a461..83b2116205 100644 --- a/src/generic/grid.cpp +++ b/src/generic/grid.cpp @@ -3230,8 +3230,8 @@ void wxGridRowLabelWindow::OnPaint( wxPaintEvent& WXUNUSED(event) ) m_owner->CalcUnscrolledPosition( 0, 0, &x, &y ); dc.SetDeviceOrigin( 0, -y ); - m_owner->CalcRowLabelsExposed( GetUpdateRegion() ); - m_owner->DrawRowLabels( dc ); + wxArrayInt rows = m_owner->CalcRowLabelsExposed( GetUpdateRegion() ); + m_owner->DrawRowLabels( dc , rows ); } @@ -3296,8 +3296,8 @@ void wxGridColLabelWindow::OnPaint( wxPaintEvent& WXUNUSED(event) ) m_owner->CalcUnscrolledPosition( 0, 0, &x, &y ); dc.SetDeviceOrigin( -x, 0 ); - m_owner->CalcColLabelsExposed( GetUpdateRegion() ); - m_owner->DrawColLabels( dc ); + wxArrayInt cols = m_owner->CalcColLabelsExposed( GetUpdateRegion() ); + m_owner->DrawColLabels( dc , cols ); } @@ -3427,13 +3427,13 @@ void wxGridWindow::OnPaint( wxPaintEvent &WXUNUSED(event) ) wxPaintDC dc( this ); m_owner->PrepareDC( dc ); wxRegion reg = GetUpdateRegion(); - m_owner->CalcCellsExposed( reg ); - m_owner->DrawGridCellArea( dc ); + wxGridCellCoordsArray DirtyCells = m_owner->CalcCellsExposed( reg ); + m_owner->DrawGridCellArea( dc , DirtyCells); #if WXGRID_DRAW_LINES m_owner->DrawAllGridLines( dc, reg ); #endif m_owner->DrawGridSpace( dc ); - m_owner->DrawHighlight( dc ); + m_owner->DrawHighlight( dc , DirtyCells ); } @@ -4173,13 +4173,13 @@ bool wxGrid::Redimension( wxGridTableMessage& msg ) } -void wxGrid::CalcRowLabelsExposed( const wxRegion& reg ) +wxArrayInt wxGrid::CalcRowLabelsExposed( const wxRegion& reg ) { wxRegionIterator iter( reg ); wxRect r; - m_rowLabelsExposed.Empty(); - + wxArrayInt rowlabels; + int top, bottom; while ( iter ) { @@ -4214,20 +4214,22 @@ void wxGrid::CalcRowLabelsExposed( const wxRegion& reg ) if ( GetRowTop(row) > bottom ) break; - m_rowLabelsExposed.Add( row ); + rowlabels.Add( row ); } iter++ ; } + + return rowlabels; } -void wxGrid::CalcColLabelsExposed( const wxRegion& reg ) +wxArrayInt wxGrid::CalcColLabelsExposed( const wxRegion& reg ) { wxRegionIterator iter( reg ); wxRect r; - m_colLabelsExposed.Empty(); + wxArrayInt colLabels; int left, right; while ( iter ) @@ -4263,22 +4265,21 @@ void wxGrid::CalcColLabelsExposed( const wxRegion& reg ) if ( GetColLeft(col) > right ) break; - m_colLabelsExposed.Add( col ); + colLabels.Add( col ); } iter++ ; } + return colLabels; } -void wxGrid::CalcCellsExposed( const wxRegion& reg ) +wxGridCellCoordsArray wxGrid::CalcCellsExposed( const wxRegion& reg ) { wxRegionIterator iter( reg ); wxRect r; - m_cellsExposed.Empty(); - m_rowsExposed.Empty(); - m_colsExposed.Empty(); + wxGridCellCoordsArray cellsExposed; int left, top, right, bottom; while ( iter ) @@ -4315,7 +4316,6 @@ void wxGrid::CalcCellsExposed( const wxRegion& reg ) if ( GetRowTop(row) > bottom ) break; - m_rowsExposed.Add( row ); for ( col = 0; col < m_numCols; col++ ) { @@ -4325,14 +4325,14 @@ void wxGrid::CalcCellsExposed( const wxRegion& reg ) if ( GetColLeft(col) > right ) break; - if ( m_colsExposed.Index( col ) == wxNOT_FOUND ) - m_colsExposed.Add( col ); - m_cellsExposed.Add( wxGridCellCoords( row, col ) ); + cellsExposed.Add( wxGridCellCoords( row, col ) ); } } iter++; } + + return cellsExposed; } @@ -5781,7 +5781,7 @@ void wxGrid::SetCurrentCell( const wxGridCellCoords& coords ) if ( IsVisible( m_currentCellCoords, FALSE ) ) { wxRect r; - r = BlockToDeviceRect(m_currentCellCoords, m_currentCellCoords); + r = BlockToDeviceRect(m_currentCellCoords, coords); if ( !m_gridLinesEnabled ) { r.x--; @@ -5790,12 +5790,12 @@ void wxGrid::SetCurrentCell( const wxGridCellCoords& coords ) r.height++; } - CalcCellsExposed( r ); + wxGridCellCoordsArray cells = CalcCellsExposed( r ); // Otherwise refresh redraws the highlight! m_currentCellCoords = coords; - - DrawGridCellArea(dc); + + DrawGridCellArea(dc,cells); DrawAllGridLines( dc, r ); } } @@ -5986,16 +5986,16 @@ bool wxGrid::SetModelValues() // exposed cells (usually set from the update region by // CalcExposedCells) // -void wxGrid::DrawGridCellArea( wxDC& dc ) +void wxGrid::DrawGridCellArea( wxDC& dc, const wxGridCellCoordsArray& cells ) { if ( !m_numRows || !m_numCols ) return; size_t i; - size_t numCells = m_cellsExposed.GetCount(); + size_t numCells = cells.GetCount(); for ( i = 0; i < numCells; i++ ) { - DrawCell( dc, m_cellsExposed[i] ); + DrawCell( dc, cells[i] ); } } @@ -6150,7 +6150,7 @@ void wxGrid::DrawCellBorder( wxDC& dc, const wxGridCellCoords& coords ) GetColRight(col), GetRowBottom(row) ); } -void wxGrid::DrawHighlight(wxDC& dc) +void wxGrid::DrawHighlight(wxDC& dc,const wxGridCellCoordsArray& cells) { // This if block was previously in wxGrid::OnPaint but that doesn't // seem to get called under wxGTK - MB @@ -6169,10 +6169,10 @@ void wxGrid::DrawHighlight(wxDC& dc) // if the active cell was repainted, repaint its highlight too because it // might have been damaged by the grid lines - size_t count = m_cellsExposed.GetCount(); + size_t count = cells.GetCount(); for ( size_t n = 0; n < count; n++ ) { - if ( m_cellsExposed[n] == m_currentCellCoords ) + if ( cells[n] == m_currentCellCoords ) { wxGridCellAttr* attr = GetCellAttr(m_currentCellCoords); DrawCellHighlight(dc, attr); @@ -6264,16 +6264,16 @@ void wxGrid::DrawAllGridLines( wxDC& dc, const wxRegion & WXUNUSED(reg) ) } -void wxGrid::DrawRowLabels( wxDC& dc ) +void wxGrid::DrawRowLabels( wxDC& dc ,const wxArrayInt& rows) { if ( !m_numRows ) return; size_t i; - size_t numLabels = m_rowLabelsExposed.GetCount(); + size_t numLabels = rows.GetCount(); for ( i = 0; i < numLabels; i++ ) { - DrawRowLabel( dc, m_rowLabelsExposed[i] ); + DrawRowLabel( dc, rows[i] ); } } @@ -6312,16 +6312,16 @@ void wxGrid::DrawRowLabel( wxDC& dc, int row ) } -void wxGrid::DrawColLabels( wxDC& dc ) +void wxGrid::DrawColLabels( wxDC& dc,const wxArrayInt& cols ) { if ( !m_numCols ) return; size_t i; - size_t numLabels = m_colLabelsExposed.GetCount(); + size_t numLabels = cols.GetCount(); for ( i = 0; i < numLabels; i++ ) { - DrawColLabel( dc, m_colLabelsExposed[i] ); + DrawColLabel( dc, cols[i] ); } } @@ -6364,19 +6364,36 @@ void wxGrid::DrawColLabel( wxDC& dc, int col ) DrawTextRectangle( dc, GetColLabelValue( col ), rect, hAlign, vAlign ); } - void wxGrid::DrawTextRectangle( wxDC& dc, const wxString& value, const wxRect& rect, int horizAlign, int vertAlign ) { - long textWidth, textHeight; - long lineWidth, lineHeight; wxArrayString lines; dc.SetClippingRegion( rect ); StringToLines( value, lines ); + + + //Forward to new API. + DrawTextRectangle( dc, + lines, + rect, + horizAlign, + vertAlign ); + +} + +void wxGrid::DrawTextRectangle( wxDC& dc, + const wxArrayString& lines, + const wxRect& rect, + int horizAlign, + int vertAlign ) +{ + long textWidth, textHeight; + long lineWidth, lineHeight; + if ( lines.GetCount() ) { GetTextBoxSize( dc, lines, &textWidth, &textHeight ); @@ -6461,7 +6478,7 @@ void wxGrid::StringToLines( const wxString& value, wxArrayString& lines ) void wxGrid::GetTextBoxSize( wxDC& dc, - wxArrayString& lines, + const wxArrayString& lines, long *width, long *height ) { long w = 0; diff --git a/src/generic/gridctrl.cpp b/src/generic/gridctrl.cpp new file mode 100644 index 0000000000..23fa38899a --- /dev/null +++ b/src/generic/gridctrl.cpp @@ -0,0 +1,381 @@ +/////////////////////////////////////////////////////////////////////////// +// Name: generic/gridctrl.cpp +// Purpose: wxGrid controls +// Author: Paul Gammans, Roger Gammans +// Modified by: +// Created: 11/04/2001 +// RCS-ID: $Id$ +// Copyright: (c) The Computer Surgery (paul@compsurg.co.uk) +// Licence: wxWindows licence +///////////////////////////////////////////////////////////////////////////// + +#ifdef __GNUG__ + #pragma interface "gridctrl.h" +#endif + +#include "wx/wxprec.h" + +#ifdef __BORLANDC__ + #pragma hdrstop +#endif + +#include "wx/generic/gridctrl.h" +#include "wx/tokenzr.h" + +// ---------------------------------------------------------------------------- +// wxGridCellDateTimeRenderer +// ---------------------------------------------------------------------------- + +// Enables a grid cell to display a formated date and or time + +wxGridCellDateTimeRenderer::wxGridCellDateTimeRenderer(wxString outformat, wxString informat) +{ + m_iformat = informat; + m_oformat = outformat; + m_tz = wxDateTime::Local; + m_dateDef = wxDefaultDateTime; +} + +wxGridCellRenderer *wxGridCellDateTimeRenderer::Clone() const +{ + wxGridCellDateTimeRenderer *renderer = new wxGridCellDateTimeRenderer; + renderer->m_iformat = m_iformat; + renderer->m_oformat = m_oformat; + renderer->m_dateDef = m_dateDef; + renderer->m_tz = m_tz; + + return renderer; +} + +wxString wxGridCellDateTimeRenderer::GetString(wxGrid& grid, int row, int col) +{ + wxGridTableBase *table = grid.GetTable(); + + bool hasDatetime = FALSE; + wxDateTime val; + wxString text; + if ( table->CanGetValueAs(row, col, wxGRID_VALUE_DATETIME) ) + { + void * tempval = table->GetValueAsCustom(row, col,wxGRID_VALUE_DATETIME); + + if (tempval){ + val = *((wxDateTime *)tempval); + hasDatetime = TRUE; + delete (wxDateTime *)tempval; + } + + } + + if (!hasDatetime ) + { + text = table->GetValue(row, col); + hasDatetime = (val.ParseFormat( text, m_iformat, m_dateDef ) != (wxChar *)NULL) ; + } + + if ( hasDatetime ) + text = val.Format(m_oformat, m_tz ); + + //If we faild to parse string just show what we where given? + return text; +} + +void wxGridCellDateTimeRenderer::Draw(wxGrid& grid, + wxGridCellAttr& attr, + wxDC& dc, + const wxRect& rectCell, + int row, int col, + bool isSelected) +{ + wxGridCellRenderer::Draw(grid, attr, dc, rectCell, row, col, isSelected); + + SetTextColoursAndFont(grid, attr, dc, isSelected); + + // draw the text right aligned by default + int hAlign, vAlign; + attr.GetAlignment(&hAlign, &vAlign); + hAlign = wxRIGHT; + + wxRect rect = rectCell; + rect.Inflate(-1); + + grid.DrawTextRectangle(dc, GetString(grid, row, col), rect, hAlign, vAlign); +} + +wxSize wxGridCellDateTimeRenderer::GetBestSize(wxGrid& grid, + wxGridCellAttr& attr, + wxDC& dc, + int row, int col) +{ + return DoGetBestSize(attr, dc, GetString(grid, row, col)); +} + +void wxGridCellDateTimeRenderer::SetParameters(const wxString& params){ + if(params) + m_oformat=params; +} + +// ---------------------------------------------------------------------------- +// wxGridCellChoiceNumberRenderer +// ---------------------------------------------------------------------------- +// Renders a number as a textual equivalent. +// eg data in cell is 0,1,2 ... n the cell could be rendered as "John","Fred"..."Bob" + + +wxGridCellEnumRenderer::wxGridCellEnumRenderer(const wxString& choices) +{ + if(choices) + SetParameters(choices); +} + +wxGridCellRenderer *wxGridCellEnumRenderer::Clone() const +{ + wxGridCellEnumRenderer *renderer = new wxGridCellEnumRenderer; + renderer->m_choices = m_choices; + return renderer; +} + +wxString wxGridCellEnumRenderer::GetString(wxGrid& grid, int row, int col) +{ + wxGridTableBase *table = grid.GetTable(); + wxString text; + if ( table->CanGetValueAs(row, col, wxGRID_VALUE_NUMBER) ) + { + int choiceno = table->GetValueAsLong(row, col); + text.Printf(_T("%s"), m_choices[ choiceno ].c_str() ); + } + else + { + text = table->GetValue(row, col); + } + + + //If we faild to parse string just show what we where given? + return text; +} + +void wxGridCellEnumRenderer::Draw(wxGrid& grid, + wxGridCellAttr& attr, + wxDC& dc, + const wxRect& rectCell, + int row, int col, + bool isSelected) +{ + wxGridCellRenderer::Draw(grid, attr, dc, rectCell, row, col, isSelected); + + SetTextColoursAndFont(grid, attr, dc, isSelected); + + // draw the text right aligned by default + int hAlign, vAlign; + attr.GetAlignment(&hAlign, &vAlign); + hAlign = wxRIGHT; + + wxRect rect = rectCell; + rect.Inflate(-1); + + grid.DrawTextRectangle(dc, GetString(grid, row, col), rect, hAlign, vAlign); +} + +wxSize wxGridCellEnumRenderer::GetBestSize(wxGrid& grid, + wxGridCellAttr& attr, + wxDC& dc, + int row, int col) +{ + return DoGetBestSize(attr, dc, GetString(grid, row, col)); +} + +void wxGridCellEnumRenderer::SetParameters(const wxString& params) +{ + if ( !params ) + { + // what can we do? + return; + } + + m_choices.Empty(); + + wxStringTokenizer tk(params, _T(',')); + while ( tk.HasMoreTokens() ) + { + m_choices.Add(tk.GetNextToken()); + } +} + +// ---------------------------------------------------------------------------- +// wxGridCellEnumEditor +// ---------------------------------------------------------------------------- +// A cell editor which displays an enum number as a textual equivalent. +// eg data in cell is 0,1,2 ... n the cell could be displayed as "John","Fred"..."Bob" +// in the combo choice box +// +wxGridCellEnumEditor::wxGridCellEnumEditor(const wxString& choices) + : wxGridCellChoiceEditor() +{ + if(choices) + SetParameters(choices); +} + +wxGridCellEditor *wxGridCellEnumEditor::Clone() const +{ + wxGridCellEnumEditor *editor = new wxGridCellEnumEditor(); + editor->m_startint = m_startint; + return editor; +} + +void wxGridCellEnumEditor::BeginEdit(int row, int col, wxGrid* grid) +{ + wxASSERT_MSG(m_control, + wxT("The wxGridCellEnumEditor must be Created first!")); + + wxGridTableBase *table = grid->GetTable(); + + if ( table->CanGetValueAs(row, col, wxGRID_VALUE_NUMBER) ) + { + m_startint = table->GetValueAsLong(row, col); + } + else + { + wxString startValue = table->GetValue(row, col); + if (startValue.IsNumber() && !startValue.IsEmpty()) + { + startValue.ToLong(&m_startint); + } + else + { + m_startint=-1; + } + } + + Combo()->SetSelection(m_startint); + Combo()->SetInsertionPointEnd(); + Combo()->SetFocus(); + +} + +bool wxGridCellEnumEditor::EndEdit(int row, int col, wxGrid* grid) +{ + int pos = Combo()->GetSelection(); + bool changed = (pos != m_startint); + if (changed) + { + if (grid->GetTable()->CanSetValueAs(row, col, wxGRID_VALUE_NUMBER)) + grid->GetTable()->SetValueAsLong(row, col, pos); + else + grid->GetTable()->SetValue(row, col,wxString::Format("%i",pos)); + } + + return changed; +} + + +void +wxGridCellAutoWrapStringEditor::Create(wxWindow* parent, + wxWindowID id, + wxEvtHandler* evtHandler) +{ + m_control = new wxTextCtrl(parent, id, wxEmptyString, + wxDefaultPosition, wxDefaultSize, + wxTE_MULTILINE | wxTE_RICH); + + + wxGridCellEditor::Create(parent, id, evtHandler); +} + +void +wxGridCellAutoWrapStringRenderer::Draw(wxGrid& grid, + wxGridCellAttr& attr, + wxDC& dc, + const wxRect& rectCell, + int row, int col, + bool isSelected) { + + + wxGridCellRenderer::Draw(grid, attr, dc, rectCell, row, col, isSelected); + + // now we only have to draw the text + SetTextColoursAndFont(grid, attr, dc, isSelected); + + int horizAlign, vertAlign; + attr.GetAlignment(&horizAlign, &vertAlign); + + wxRect rect = rectCell; + rect.Inflate(-1); + + grid.DrawTextRectangle(dc, GetTextLines(grid,dc,attr,rect,row,col), + rect, horizAlign, vertAlign); +} + + +wxArrayString +wxGridCellAutoWrapStringRenderer::GetTextLines(wxGrid& grid, + wxDC& dc, + wxGridCellAttr& attr, + const wxRect& rect, + int row, int col) +{ + wxString data = grid.GetCellValue(row, col); + + wxArrayString lines; + dc.SetFont(attr.GetFont()); + + //Taken from wxGrid again! + wxCoord x = 0, y = 0, curr_x = 0; + wxCoord max_x = rect.GetWidth(); + + dc.SetFont(attr.GetFont()); + wxStringTokenizer tk(data , _T(" \n\t\r")); + wxString thisline(""); + + while ( tk.HasMoreTokens() ) + { + wxString tok = tk.GetNextToken(); + //FIXME: this causes us to print an extra unnecesary + // space at the end of the line. But it + // is invisible , simplifies the size calculation + // and ensures tokens are seperated in the display + tok += _T(" "); + + dc.GetTextExtent(tok, &x, &y); + if ( curr_x + x > max_x) { + lines.Add( wxString(thisline) ); + thisline = tok; + curr_x=x; + } else { + thisline+= tok; + curr_x += x; + } + + } + //Add last line + lines.Add( wxString(thisline) ); + + return lines; +} + + +wxSize +wxGridCellAutoWrapStringRenderer::GetBestSize(wxGrid& grid, + wxGridCellAttr& attr, + wxDC& dc, + int row, int col) +{ + int x,y, height , width = grid.GetColSize(col) -10; + int count = 250; //Limit iterations.. + + wxRect rect(0,0,width,10); + + // M is a nice large character 'y' gives descender!. + dc.GetTextExtent("My", &x, &y); + + do + { + width+=10; + rect.SetWidth(width); + height = y *( GetTextLines(grid,dc,attr,rect,row,col).GetCount()); + count--; + // Search for a shape no taller than the golden ratio. + } while (count && (width < (height*1.68)) ); + + + return wxSize(width,height); +} + -- 2.49.0