+// scroll line size
+// 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
+// ============================================================================
+
+// ----------------------------------------------------------------------------
+// wxGridCellEditor
+// ----------------------------------------------------------------------------
+
+wxGridCellEditor::wxGridCellEditor()
+{
+ m_control = NULL;
+}
+
+
+wxGridCellEditor::~wxGridCellEditor()
+{
+ Destroy();
+}
+
+void wxGridCellEditor::Create(wxWindow* WXUNUSED(parent),
+ wxWindowID WXUNUSED(id),
+ wxEvtHandler* evtHandler)
+{
+ if ( evtHandler )
+ m_control->PushEventHandler(evtHandler);
+}
+
+void wxGridCellEditor::PaintBackground(const wxRect& rectCell,
+ wxGridCellAttr *attr)
+{
+ // erase the background because we might not fill the cell
+ wxClientDC dc(m_control->GetParent());
+ dc.SetPen(*wxTRANSPARENT_PEN);
+ dc.SetBrush(wxBrush(attr->GetBackgroundColour(), wxSOLID));
+ dc.DrawRectangle(rectCell);
+
+ // redraw the control we just painted over
+ m_control->Refresh();
+}
+
+void wxGridCellEditor::Destroy()
+{
+ if (m_control)
+ {
+ m_control->PopEventHandler(TRUE /* delete it*/);
+
+ m_control->Destroy();
+ m_control = NULL;
+ }
+}
+
+void wxGridCellEditor::Show(bool show, wxGridCellAttr *attr)
+{
+ wxASSERT_MSG(m_control,
+ wxT("The wxGridCellEditor must be Created first!"));
+ m_control->Show(show);
+
+ if ( show )
+ {
+ // set the colours/fonts if we have any
+ if ( attr )
+ {
+ m_colFgOld = m_control->GetForegroundColour();
+ m_control->SetForegroundColour(attr->GetTextColour());
+
+ m_colBgOld = m_control->GetBackgroundColour();
+ m_control->SetBackgroundColour(attr->GetBackgroundColour());
+
+ m_fontOld = m_control->GetFont();
+ m_control->SetFont(attr->GetFont());
+
+ // can't do anything more in the base class version, the other
+ // attributes may only be used by the derived classes
+ }
+ }
+ else
+ {
+ // restore the standard colours fonts
+ if ( m_colFgOld.Ok() )
+ {
+ m_control->SetForegroundColour(m_colFgOld);
+ m_colFgOld = wxNullColour;
+ }
+
+ if ( m_colBgOld.Ok() )
+ {
+ m_control->SetBackgroundColour(m_colBgOld);
+ m_colBgOld = wxNullColour;
+ }
+
+ if ( m_fontOld.Ok() )
+ {
+ m_control->SetFont(m_fontOld);
+ m_fontOld = wxNullFont;
+ }
+ }
+}
+
+void wxGridCellEditor::SetSize(const wxRect& rect)
+{
+ wxASSERT_MSG(m_control,
+ wxT("The wxGridCellEditor must be Created first!"));
+ m_control->SetSize(rect, wxSIZE_ALLOW_MINUS_ONE);
+}
+
+void wxGridCellEditor::HandleReturn(wxKeyEvent& event)
+{
+ event.Skip();
+}
+
+
+void wxGridCellEditor::StartingKey(wxKeyEvent& event)
+{
+ event.Skip();
+}
+
+void wxGridCellEditor::StartingClick()
+{
+}
+
+// ----------------------------------------------------------------------------
+// wxGridCellTextEditor
+// ----------------------------------------------------------------------------
+
+wxGridCellTextEditor::wxGridCellTextEditor()
+{
+ m_maxChars = 0;
+}
+
+void wxGridCellTextEditor::Create(wxWindow* parent,
+ wxWindowID id,
+ wxEvtHandler* evtHandler)
+{
+ m_control = new wxTextCtrl(parent, id, wxEmptyString,
+ wxDefaultPosition, wxDefaultSize
+#if defined(__WXMSW__)
+ , wxTE_MULTILINE | wxTE_NO_VSCROLL // necessary ???
+#endif
+ );
+
+ // TODO: use m_maxChars
+
+ wxGridCellEditor::Create(parent, id, evtHandler);
+}
+
+void wxGridCellTextEditor::PaintBackground(const wxRect& WXUNUSED(rectCell),
+ wxGridCellAttr * WXUNUSED(attr))
+{
+ // as we fill the entire client area, don't do anything here to minimize
+ // flicker
+}
+
+void wxGridCellTextEditor::SetSize(const wxRect& rectOrig)
+{
+ wxRect rect(rectOrig);
+
+ // Make the edit control large enough to allow for internal
+ // margins
+ //
+ // TODO: remove this if the text ctrl sizing is improved esp. for
+ // unix
+ //
+#if defined(__WXGTK__)
+ if (rect.x != 0)
+ {
+ rect.x += 1;
+ rect.y += 1;
+ rect.width -= 1;
+ rect.height -= 1;
+ }
+#else // !GTK
+ int extra_x = ( rect.x > 2 )? 2 : 1;
+ int extra_y = ( rect.y > 2 )? 2 : 1;
+#if defined(__WXMOTIF__)
+ extra_x *= 2;
+ extra_y *= 2;
+#endif
+ rect.SetLeft( wxMax(0, rect.x - extra_x) );
+ rect.SetTop( wxMax(0, rect.y - extra_y) );
+ rect.SetRight( rect.GetRight() + 2*extra_x );
+ rect.SetBottom( rect.GetBottom() + 2*extra_y );
+#endif // GTK/!GTK
+
+ wxGridCellEditor::SetSize(rect);
+}
+
+void wxGridCellTextEditor::BeginEdit(int row, int col, wxGrid* grid)
+{
+ wxASSERT_MSG(m_control,
+ wxT("The wxGridCellEditor must be Created first!"));
+
+ m_startValue = grid->GetTable()->GetValue(row, col);
+
+ DoBeginEdit(m_startValue);
+}
+
+void wxGridCellTextEditor::DoBeginEdit(const wxString& startValue)
+{
+ Text()->SetValue(startValue);
+ Text()->SetInsertionPointEnd();
+ Text()->SetFocus();
+}
+
+bool wxGridCellTextEditor::EndEdit(int row, int col,
+ wxGrid* grid)
+{
+ wxASSERT_MSG(m_control,
+ wxT("The wxGridCellEditor must be Created first!"));
+
+ bool changed = FALSE;
+ wxString value = Text()->GetValue();
+ if (value != m_startValue)
+ changed = TRUE;
+
+ if (changed)
+ grid->GetTable()->SetValue(row, col, value);
+
+ m_startValue = wxEmptyString;
+ Text()->SetValue(m_startValue);
+
+ return changed;
+}
+
+
+void wxGridCellTextEditor::Reset()
+{
+ wxASSERT_MSG(m_control,
+ wxT("The wxGridCellEditor must be Created first!"));
+
+ DoReset(m_startValue);
+}
+
+void wxGridCellTextEditor::DoReset(const wxString& startValue)
+{
+ Text()->SetValue(startValue);
+ Text()->SetInsertionPointEnd();
+}
+
+void wxGridCellTextEditor::StartingKey(wxKeyEvent& event)
+{
+ if ( !event.AltDown() && !event.MetaDown() && !event.ControlDown() )
+ {
+ // insert the key in the control
+ int keycode = (int)event.KeyCode();
+ if ( isprint(keycode) )
+ {
+ // FIXME this is not going to work for non letters...
+ if ( !event.ShiftDown() )
+ {
+ keycode = tolower(keycode);
+ }
+
+ Text()->AppendText((wxChar)keycode);
+
+ return;
+ }
+
+ }
+
+ event.Skip();
+}
+
+void wxGridCellTextEditor::HandleReturn( wxKeyEvent&
+ WXUNUSED_GTK(WXUNUSED_MOTIF(event)) )
+{
+#if defined(__WXMOTIF__) || defined(__WXGTK__)
+ // wxMotif needs a little extra help...
+ long pos = Text()->GetInsertionPoint();
+ wxString s( Text()->GetValue() );
+ s = s.Left(pos) + "\n" + s.Mid(pos);
+ Text()->SetValue(s);
+ Text()->SetInsertionPoint( pos );
+#else
+ // the other ports can handle a Return key press
+ //
+ event.Skip();
+#endif
+}
+
+void wxGridCellTextEditor::SetParameters(const wxString& params)
+{
+ if ( !params )
+ {
+ // reset to default
+ m_maxChars = 0;
+ }
+ else
+ {
+ long tmp;
+ if ( !params.ToLong(&tmp) )
+ {
+ wxLogDebug(_T("Invalid wxGridCellTextEditor parameter string "
+ "'%s' ignored"), params.c_str());
+ }
+ else
+ {
+ m_maxChars = (size_t)tmp;
+ }
+ }
+}
+
+// ----------------------------------------------------------------------------
+// wxGridCellNumberEditor
+// ----------------------------------------------------------------------------
+
+wxGridCellNumberEditor::wxGridCellNumberEditor(int min, int max)
+{
+ m_min = min;
+ m_max = max;
+}
+
+void wxGridCellNumberEditor::Create(wxWindow* parent,
+ wxWindowID id,
+ wxEvtHandler* evtHandler)
+{
+ if ( HasRange() )
+ {
+ // create a spin ctrl
+ m_control = new wxSpinCtrl(parent, -1, wxEmptyString,
+ wxDefaultPosition, wxDefaultSize,
+ wxSP_ARROW_KEYS,
+ m_min, m_max);
+
+ wxGridCellEditor::Create(parent, id, evtHandler);
+ }
+ else
+ {
+ // just a text control
+ wxGridCellTextEditor::Create(parent, id, evtHandler);
+
+#if wxUSE_VALIDATORS
+ Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC));
+#endif // wxUSE_VALIDATORS
+ }
+}
+
+void wxGridCellNumberEditor::BeginEdit(int row, int col, wxGrid* grid)
+{
+ // first get the value
+ wxGridTableBase *table = grid->GetTable();
+ if ( table->CanGetValueAs(row, col, wxGRID_VALUE_NUMBER) )
+ {
+ m_valueOld = table->GetValueAsLong(row, col);
+ }
+ else
+ {
+ wxString sValue = table->GetValue(row, col);
+ if (! sValue.ToLong(&m_valueOld))
+ {
+ wxFAIL_MSG( _T("this cell doesn't have numeric value") );
+ return;
+ }
+ }
+
+ if ( HasRange() )
+ {
+ Spin()->SetValue((int)m_valueOld);
+ }
+ else
+ {
+ DoBeginEdit(GetString());
+ }
+}
+
+bool wxGridCellNumberEditor::EndEdit(int row, int col,
+ wxGrid* grid)
+{
+ bool changed;
+ long value;
+
+ if ( HasRange() )
+ {
+ value = Spin()->GetValue();
+ changed = value != m_valueOld;
+ }
+ else
+ {
+ changed = Text()->GetValue().ToLong(&value) && (value != m_valueOld);
+ }
+
+ if ( changed )
+ {
+ if (grid->GetTable()->CanSetValueAs(row, col, wxGRID_VALUE_NUMBER))
+ grid->GetTable()->SetValueAsLong(row, col, value);
+ else
+ grid->GetTable()->SetValue(row, col, wxString::Format("%ld", value));
+ }
+
+ return changed;
+}
+
+void wxGridCellNumberEditor::Reset()
+{
+ if ( HasRange() )
+ {
+ Spin()->SetValue((int)m_valueOld);
+ }
+ else
+ {
+ DoReset(GetString());
+ }
+}
+
+void wxGridCellNumberEditor::StartingKey(wxKeyEvent& event)
+{
+ if ( !HasRange() )
+ {
+ int keycode = (int) event.KeyCode();
+ if ( isdigit(keycode) || keycode == '+' || keycode == '-' )
+ {
+ wxGridCellTextEditor::StartingKey(event);
+
+ // skip Skip() below
+ return;
+ }
+ }
+
+ event.Skip();
+}
+
+void wxGridCellNumberEditor::SetParameters(const wxString& params)
+{
+ if ( !params )
+ {
+ // reset to default
+ m_min =
+ m_max = -1;
+ }
+ else
+ {
+ long tmp;
+ if ( params.BeforeFirst(_T(',')).ToLong(&tmp) )
+ {
+ m_min = (int)tmp;
+
+ if ( params.AfterFirst(_T(',')).ToLong(&tmp) )
+ {
+ m_max = (int)tmp;
+
+ // skip the error message below
+ return;
+ }
+ }
+
+ wxLogDebug(_T("Invalid wxGridCellNumberEditor parameter string "
+ "'%s' ignored"), params.c_str());
+ }
+}
+
+// ----------------------------------------------------------------------------
+// wxGridCellFloatEditor
+// ----------------------------------------------------------------------------
+
+void wxGridCellFloatEditor::Create(wxWindow* parent,
+ wxWindowID id,
+ wxEvtHandler* evtHandler)
+{
+ wxGridCellTextEditor::Create(parent, id, evtHandler);
+
+#if wxUSE_VALIDATORS
+ Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC));
+#endif // wxUSE_VALIDATORS
+}
+
+void wxGridCellFloatEditor::BeginEdit(int row, int col, wxGrid* grid)
+{
+ // first get the value
+ wxGridTableBase *table = grid->GetTable();
+ if ( table->CanGetValueAs(row, col, wxGRID_VALUE_FLOAT) )
+ {
+ m_valueOld = table->GetValueAsDouble(row, col);
+ }
+ else
+ {
+ wxString sValue = table->GetValue(row, col);
+ if (! sValue.ToDouble(&m_valueOld))
+ {
+ wxFAIL_MSG( _T("this cell doesn't have float value") );
+ return;
+ }
+ }
+
+ DoBeginEdit(GetString());
+}
+
+bool wxGridCellFloatEditor::EndEdit(int row, int col,
+ wxGrid* grid)
+{
+ double value;
+ if ( Text()->GetValue().ToDouble(&value) && (value != m_valueOld) )
+ {
+ if (grid->GetTable()->CanSetValueAs(row, col, wxGRID_VALUE_FLOAT))
+ grid->GetTable()->SetValueAsDouble(row, col, value);
+ else
+ grid->GetTable()->SetValue(row, col, wxString::Format("%f", value));
+
+ return TRUE;
+ }
+ else
+ {
+ return FALSE;
+ }
+}
+
+void wxGridCellFloatEditor::Reset()
+{
+ DoReset(GetString());
+}
+
+void wxGridCellFloatEditor::StartingKey(wxKeyEvent& event)
+{
+ int keycode = (int)event.KeyCode();
+ if ( isdigit(keycode) ||
+ keycode == '+' || keycode == '-' || keycode == '.' )
+ {
+ wxGridCellTextEditor::StartingKey(event);
+
+ // skip Skip() below
+ return;
+ }
+
+ event.Skip();
+}
+
+// ----------------------------------------------------------------------------
+// wxGridCellBoolEditor
+// ----------------------------------------------------------------------------
+
+void wxGridCellBoolEditor::Create(wxWindow* parent,
+ wxWindowID id,
+ wxEvtHandler* evtHandler)
+{
+ m_control = new wxCheckBox(parent, id, wxEmptyString,
+ wxDefaultPosition, wxDefaultSize,
+ wxNO_BORDER);
+
+ wxGridCellEditor::Create(parent, id, evtHandler);
+}
+
+void wxGridCellBoolEditor::SetSize(const wxRect& r)
+{
+ bool resize = FALSE;
+ wxSize size = m_control->GetSize();
+ wxCoord minSize = wxMin(r.width, r.height);
+
+ // check if the checkbox is not too big/small for this cell
+ wxSize sizeBest = m_control->GetBestSize();
+ if ( !(size == sizeBest) )
+ {
+ // reset to default size if it had been made smaller
+ size = sizeBest;
+
+ resize = TRUE;
+ }
+
+ if ( size.x >= minSize || size.y >= minSize )
+ {
+ // leave 1 pixel margin
+ size.x = size.y = minSize - 2;
+
+ resize = TRUE;
+ }
+
+ if ( resize )
+ {
+ m_control->SetSize(size);
+ }
+
+ // position it in the centre of the rectangle (TODO: support alignment?)
+
+#if defined(__WXGTK__) || defined (__WXMOTIF__)
+ // the checkbox without label still has some space to the right in wxGTK,
+ // so shift it to the right
+ size.x -= 8;
+#elif defined(__WXMSW__)
+ // here too, but in other way
+ size.x += 1;
+ size.y -= 2;
+#endif
+
+ m_control->Move(r.x + r.width/2 - size.x/2, r.y + r.height/2 - size.y/2);
+}
+
+void wxGridCellBoolEditor::Show(bool show, wxGridCellAttr *attr)
+{
+ m_control->Show(show);
+
+ if ( show )
+ {
+ wxColour colBg = attr ? attr->GetBackgroundColour() : *wxLIGHT_GREY;
+ CBox()->SetBackgroundColour(colBg);
+ }
+}
+
+void wxGridCellBoolEditor::BeginEdit(int row, int col, wxGrid* grid)
+{
+ wxASSERT_MSG(m_control,
+ wxT("The wxGridCellEditor must be Created first!"));
+
+ if (grid->GetTable()->CanGetValueAs(row, col, wxGRID_VALUE_BOOL))
+ m_startValue = grid->GetTable()->GetValueAsBool(row, col);
+ else
+ m_startValue = !!grid->GetTable()->GetValue(row, col);
+ CBox()->SetValue(m_startValue);
+ CBox()->SetFocus();
+}
+
+bool wxGridCellBoolEditor::EndEdit(int row, int col,
+ wxGrid* grid)
+{
+ wxASSERT_MSG(m_control,
+ wxT("The wxGridCellEditor must be Created first!"));
+
+ bool changed = FALSE;
+ bool value = CBox()->GetValue();
+ if ( value != m_startValue )
+ changed = TRUE;
+
+ if ( changed )
+ {
+ if (grid->GetTable()->CanGetValueAs(row, col, wxGRID_VALUE_BOOL))
+ grid->GetTable()->SetValueAsBool(row, col, value);
+ else
+ grid->GetTable()->SetValue(row, col, value ? _T("1") : wxEmptyString);
+ }
+
+ return changed;
+}
+
+void wxGridCellBoolEditor::Reset()
+{
+ wxASSERT_MSG(m_control,
+ wxT("The wxGridCellEditor must be Created first!"));
+
+ CBox()->SetValue(m_startValue);
+}
+
+void wxGridCellBoolEditor::StartingClick()
+{
+ CBox()->SetValue(!CBox()->GetValue());
+}
+
+// ----------------------------------------------------------------------------
+// wxGridCellChoiceEditor
+// ----------------------------------------------------------------------------
+
+wxGridCellChoiceEditor::wxGridCellChoiceEditor(size_t count,
+ const wxChar* choices[],
+ bool allowOthers)
+ : m_allowOthers(allowOthers)
+{
+ if ( count )
+ {
+ m_choices.Alloc(count);
+ for ( size_t n = 0; n < count; n++ )
+ {
+ m_choices.Add(choices[n]);
+ }
+ }
+}
+
+wxGridCellEditor *wxGridCellChoiceEditor::Clone() const
+{
+ wxGridCellChoiceEditor *editor = new wxGridCellChoiceEditor;
+ editor->m_allowOthers = m_allowOthers;
+ editor->m_choices = m_choices;
+
+ return editor;
+}
+
+void wxGridCellChoiceEditor::Create(wxWindow* parent,
+ wxWindowID id,
+ wxEvtHandler* evtHandler)
+{
+ size_t count = m_choices.GetCount();
+ wxString *choices = new wxString[count];
+ for ( size_t n = 0; n < count; n++ )
+ {
+ choices[n] = m_choices[n];
+ }
+
+ m_control = new wxComboBox(parent, id, wxEmptyString,
+ wxDefaultPosition, wxDefaultSize,
+ count, choices,
+ m_allowOthers ? 0 : wxCB_READONLY);
+
+ delete [] choices;
+
+ wxGridCellEditor::Create(parent, id, evtHandler);
+}
+
+void wxGridCellChoiceEditor::PaintBackground(const wxRect& rectCell,
+ wxGridCellAttr * attr)
+{
+ // as we fill the entire client area, don't do anything here to minimize
+ // flicker
+
+ // TODO: It doesn't actually fill the client area since the height of a
+ // combo always defaults to the standard... Until someone has time to
+ // figure out the right rectangle to paint, just do it the normal way...
+ wxGridCellEditor::PaintBackground(rectCell, attr);
+}
+
+void wxGridCellChoiceEditor::BeginEdit(int row, int col, wxGrid* grid)
+{
+ wxASSERT_MSG(m_control,
+ wxT("The wxGridCellEditor must be Created first!"));
+
+ m_startValue = grid->GetTable()->GetValue(row, col);
+
+ Combo()->SetValue(m_startValue);
+ size_t count = m_choices.GetCount();
+ for (size_t i=0; i<count; i++)
+ {
+ if (m_startValue == m_choices[i])
+ {
+ Combo()->SetSelection(i);
+ break;
+ }
+ }
+ Combo()->SetInsertionPointEnd();
+ Combo()->SetFocus();
+}
+
+bool wxGridCellChoiceEditor::EndEdit(int row, int col,
+ wxGrid* grid)
+{
+ wxString value = Combo()->GetValue();
+ bool changed = value != m_startValue;
+
+ if ( changed )
+ grid->GetTable()->SetValue(row, col, value);
+
+ m_startValue = wxEmptyString;
+ Combo()->SetValue(m_startValue);
+
+ return changed;
+}
+
+void wxGridCellChoiceEditor::Reset()
+{
+ Combo()->SetValue(m_startValue);
+ Combo()->SetInsertionPointEnd();
+}
+
+void wxGridCellChoiceEditor::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());
+ }
+}
+
+// ----------------------------------------------------------------------------
+// wxGridCellEditorEvtHandler
+// ----------------------------------------------------------------------------
+
+void wxGridCellEditorEvtHandler::OnKeyDown(wxKeyEvent& event)
+{
+ switch ( event.KeyCode() )
+ {
+ case WXK_ESCAPE:
+ m_editor->Reset();
+ m_grid->DisableCellEditControl();
+ break;
+
+ case WXK_TAB:
+ event.Skip( m_grid->ProcessEvent( event ) );
+ break;
+
+ case WXK_RETURN:
+ if (!m_grid->ProcessEvent(event))
+ m_editor->HandleReturn(event);
+ break;
+
+
+ default:
+ event.Skip();
+ }
+}
+
+void wxGridCellEditorEvtHandler::OnChar(wxKeyEvent& event)
+{
+ switch ( event.KeyCode() )
+ {
+ case WXK_ESCAPE:
+ case WXK_TAB:
+ case WXK_RETURN:
+ break;
+
+ default:
+ event.Skip();
+ }
+}
+
+// ----------------------------------------------------------------------------
+// wxGridCellWorker is an (almost) empty common base class for
+// wxGridCellRenderer and wxGridCellEditor managing ref counting
+// ----------------------------------------------------------------------------
+
+void wxGridCellWorker::SetParameters(const wxString& WXUNUSED(params))
+{
+ // nothing to do
+}
+
+wxGridCellWorker::~wxGridCellWorker()
+{
+}
+
+// ============================================================================
+// renderer classes
+// ============================================================================
+
+// ----------------------------------------------------------------------------
+// wxGridCellRenderer
+// ----------------------------------------------------------------------------
+
+void wxGridCellRenderer::Draw(wxGrid& grid,
+ wxGridCellAttr& attr,
+ wxDC& dc,
+ const wxRect& rect,
+ int WXUNUSED(row), int WXUNUSED(col),
+ bool isSelected)
+{
+ dc.SetBackgroundMode( wxSOLID );
+
+ if ( isSelected )
+ {
+ dc.SetBrush( wxBrush(grid.GetSelectionBackground(), wxSOLID) );
+ }
+ else
+ {
+ dc.SetBrush( wxBrush(attr.GetBackgroundColour(), wxSOLID) );
+ }
+
+ dc.SetPen( *wxTRANSPARENT_PEN );
+ dc.DrawRectangle(rect);
+}
+
+// ----------------------------------------------------------------------------
+// wxGridCellStringRenderer
+// ----------------------------------------------------------------------------
+
+void wxGridCellStringRenderer::SetTextColoursAndFont(wxGrid& grid,
+ wxGridCellAttr& attr,
+ wxDC& dc,
+ bool isSelected)
+{
+ dc.SetBackgroundMode( wxTRANSPARENT );
+
+ // TODO some special colours for attr.IsReadOnly() case?
+
+ if ( isSelected )
+ {
+ dc.SetTextBackground( grid.GetSelectionBackground() );
+ dc.SetTextForeground( grid.GetSelectionForeground() );
+ }
+ else
+ {
+ dc.SetTextBackground( attr.GetBackgroundColour() );
+ dc.SetTextForeground( attr.GetTextColour() );
+ }
+
+ dc.SetFont( attr.GetFont() );
+}
+
+wxSize wxGridCellStringRenderer::DoGetBestSize(wxGridCellAttr& attr,
+ wxDC& dc,
+ const wxString& text)
+{
+ wxCoord x, y;
+ dc.SetFont(attr.GetFont());
+ dc.GetTextExtent(text, &x, &y);
+
+ return wxSize(x, y);
+}
+
+wxSize wxGridCellStringRenderer::GetBestSize(wxGrid& grid,
+ wxGridCellAttr& attr,
+ wxDC& dc,
+ int row, int col)
+{
+ return DoGetBestSize(attr, dc, grid.GetCellValue(row, col));
+}
+
+void wxGridCellStringRenderer::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 hAlign, vAlign;
+ attr.GetAlignment(&hAlign, &vAlign);
+
+ wxRect rect = rectCell;
+ rect.Inflate(-1);
+
+ grid.DrawTextRectangle(dc, grid.GetCellValue(row, col),
+ rect, hAlign, vAlign);
+}
+
+// ----------------------------------------------------------------------------
+// wxGridCellNumberRenderer
+// ----------------------------------------------------------------------------
+
+wxString wxGridCellNumberRenderer::GetString(wxGrid& grid, int row, int col)
+{
+ wxGridTableBase *table = grid.GetTable();
+ wxString text;
+ if ( table->CanGetValueAs(row, col, wxGRID_VALUE_NUMBER) )
+ {
+ text.Printf(_T("%ld"), table->GetValueAsLong(row, col));
+ }
+ else
+ {
+ text = table->GetValue(row, col);
+ }
+
+ return text;
+}
+
+void wxGridCellNumberRenderer::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 wxGridCellNumberRenderer::GetBestSize(wxGrid& grid,
+ wxGridCellAttr& attr,
+ wxDC& dc,
+ int row, int col)
+{
+ return DoGetBestSize(attr, dc, GetString(grid, row, col));
+}
+
+// ----------------------------------------------------------------------------
+// wxGridCellFloatRenderer
+// ----------------------------------------------------------------------------
+
+wxGridCellFloatRenderer::wxGridCellFloatRenderer(int width, int precision)
+{
+ SetWidth(width);
+ SetPrecision(precision);
+}
+
+wxGridCellRenderer *wxGridCellFloatRenderer::Clone() const
+{
+ wxGridCellFloatRenderer *renderer = new wxGridCellFloatRenderer;
+ renderer->m_width = m_width;
+ renderer->m_precision = m_precision;
+ renderer->m_format = m_format;
+
+ return renderer;
+}
+
+wxString wxGridCellFloatRenderer::GetString(wxGrid& grid, int row, int col)
+{
+ wxGridTableBase *table = grid.GetTable();
+
+ bool hasDouble;
+ double val;
+ wxString text;
+ if ( table->CanGetValueAs(row, col, wxGRID_VALUE_FLOAT) )
+ {
+ val = table->GetValueAsDouble(row, col);
+ hasDouble = TRUE;
+ }
+ else
+ {
+ text = table->GetValue(row, col);
+ hasDouble = text.ToDouble(&val);
+ }
+
+ if ( hasDouble )
+ {
+ if ( !m_format )
+ {
+ if ( m_width == -1 )
+ {
+ // default width/precision
+ m_format = _T("%f");
+ }
+ else if ( m_precision == -1 )
+ {
+ // default precision
+ m_format.Printf(_T("%%%d.f"), m_width);
+ }
+ else
+ {
+ m_format.Printf(_T("%%%d.%df"), m_width, m_precision);
+ }
+ }
+
+ text.Printf(m_format, val);
+ }
+ //else: text already contains the string
+
+ return text;
+}
+
+void wxGridCellFloatRenderer::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 wxGridCellFloatRenderer::GetBestSize(wxGrid& grid,
+ wxGridCellAttr& attr,
+ wxDC& dc,
+ int row, int col)
+{
+ return DoGetBestSize(attr, dc, GetString(grid, row, col));
+}
+
+void wxGridCellFloatRenderer::SetParameters(const wxString& params)
+{
+ bool ok = TRUE;
+
+ if ( !params )
+ {
+ // reset to defaults
+ SetWidth(-1);
+ SetPrecision(-1);
+ }
+ else
+ {
+ wxString tmp = params.BeforeFirst(_T(','));
+ if ( !!tmp )
+ {
+ long width;
+ if ( !tmp.ToLong(&width) )
+ {
+ ok = FALSE;
+ }
+ else
+ {
+ SetWidth((int)width);
+
+ tmp = params.AfterFirst(_T(','));
+ if ( !!tmp )
+ {
+ long precision;
+ if ( !tmp.ToLong(&precision) )
+ {
+ ok = FALSE;
+ }
+ else
+ {
+ SetPrecision((int)precision);
+ }
+ }
+ }
+ }
+
+ if ( !ok )
+ {
+ wxLogDebug(_T("Invalid wxGridCellFloatRenderer parameter string "
+ "'%s ignored"), params.c_str());
+ }
+ }
+}
+
+// ----------------------------------------------------------------------------
+// wxGridCellBoolRenderer
+// ----------------------------------------------------------------------------
+
+wxSize wxGridCellBoolRenderer::ms_sizeCheckMark;
+
+// FIXME these checkbox size calculations are really ugly...
+
+// between checkmark and box
+static const wxCoord wxGRID_CHECKMARK_MARGIN = 2;
+
+wxSize wxGridCellBoolRenderer::GetBestSize(wxGrid& grid,
+ wxGridCellAttr& WXUNUSED(attr),
+ wxDC& WXUNUSED(dc),
+ int WXUNUSED(row),
+ int WXUNUSED(col))
+{
+ // compute it only once (no locks for MT safeness in GUI thread...)
+ if ( !ms_sizeCheckMark.x )
+ {
+ // get checkbox size
+ wxCoord checkSize = 0;
+ wxCheckBox *checkbox = new wxCheckBox(&grid, -1, wxEmptyString);
+ wxSize size = checkbox->GetBestSize();
+ checkSize = size.y + 2*wxGRID_CHECKMARK_MARGIN;
+
+ // FIXME wxGTK::wxCheckBox::GetBestSize() gives "wrong" result
+#if defined(__WXGTK__) || defined(__WXMOTIF__)
+ checkSize -= size.y / 2;
+#endif
+
+ delete checkbox;
+
+ ms_sizeCheckMark.x = ms_sizeCheckMark.y = checkSize;
+ }
+
+ return ms_sizeCheckMark;
+}
+
+void wxGridCellBoolRenderer::Draw(wxGrid& grid,
+ wxGridCellAttr& attr,
+ wxDC& dc,
+ const wxRect& rect,
+ int row, int col,
+ bool isSelected)
+{
+ wxGridCellRenderer::Draw(grid, attr, dc, rect, row, col, isSelected);
+
+ // draw a check mark in the centre (ignoring alignment - TODO)
+ wxSize size = GetBestSize(grid, attr, dc, row, col);
+
+ // don't draw outside the cell
+ wxCoord minSize = wxMin(rect.width, rect.height);
+ if ( size.x >= minSize || size.y >= minSize )
+ {
+ // and even leave (at least) 1 pixel margin
+ size.x = size.y = minSize - 2;
+ }
+
+ // draw a border around checkmark
+ wxRect rectBorder;
+ rectBorder.x = rect.x + rect.width/2 - size.x/2;
+ rectBorder.y = rect.y + rect.height/2 - size.y/2;
+ rectBorder.width = size.x;
+ rectBorder.height = size.y;
+
+ bool value;
+ if ( grid.GetTable()->CanGetValueAs(row, col, wxGRID_VALUE_BOOL) )
+ value = grid.GetTable()->GetValueAsBool(row, col);
+ else
+ value = !!grid.GetTable()->GetValue(row, col);
+
+ if ( value )
+ {
+ wxRect rectMark = rectBorder;
+#ifdef __WXMSW__
+ // MSW DrawCheckMark() is weird (and should probably be changed...)
+ rectMark.Inflate(-wxGRID_CHECKMARK_MARGIN/2);
+ rectMark.x++;
+ rectMark.y++;
+#else // !MSW
+ rectMark.Inflate(-wxGRID_CHECKMARK_MARGIN);
+#endif // MSW/!MSW
+
+ dc.SetTextForeground(attr.GetTextColour());
+ dc.DrawCheckMark(rectMark);
+ }
+
+ dc.SetBrush(*wxTRANSPARENT_BRUSH);
+ dc.SetPen(wxPen(attr.GetTextColour(), 1, wxSOLID));
+ dc.DrawRectangle(rectBorder);
+}
+
+// ----------------------------------------------------------------------------
+// wxGridCellAttr
+// ----------------------------------------------------------------------------
+
+wxGridCellAttr *wxGridCellAttr::Clone() const
+{
+ wxGridCellAttr *attr = new wxGridCellAttr;
+ if ( HasTextColour() )
+ attr->SetTextColour(GetTextColour());
+ if ( HasBackgroundColour() )
+ attr->SetBackgroundColour(GetBackgroundColour());
+ if ( HasFont() )
+ attr->SetFont(GetFont());
+ if ( HasAlignment() )
+ attr->SetAlignment(m_hAlign, m_vAlign);
+
+ if ( m_renderer )
+ {
+ attr->SetRenderer(m_renderer);
+ m_renderer->IncRef();
+ }
+ if ( m_editor )
+ {
+ attr->SetEditor(m_editor);
+ m_editor->IncRef();
+ }
+
+ if ( IsReadOnly() )
+ attr->SetReadOnly();
+
+ attr->SetDefAttr(m_defGridAttr);
+
+ return attr;
+}
+
+const wxColour& wxGridCellAttr::GetTextColour() const
+{
+ if (HasTextColour())
+ {
+ return m_colText;
+ }
+ else if (m_defGridAttr != this)
+ {
+ return m_defGridAttr->GetTextColour();
+ }
+ else
+ {
+ wxFAIL_MSG(wxT("Missing default cell attribute"));
+ return wxNullColour;
+ }
+}
+
+
+const wxColour& wxGridCellAttr::GetBackgroundColour() const
+{
+ if (HasBackgroundColour())
+ return m_colBack;
+ else if (m_defGridAttr != this)
+ return m_defGridAttr->GetBackgroundColour();
+ else
+ {
+ wxFAIL_MSG(wxT("Missing default cell attribute"));
+ return wxNullColour;
+ }
+}
+
+
+const wxFont& wxGridCellAttr::GetFont() const
+{
+ if (HasFont())
+ return m_font;
+ else if (m_defGridAttr != this)
+ return m_defGridAttr->GetFont();
+ else
+ {
+ wxFAIL_MSG(wxT("Missing default cell attribute"));
+ return wxNullFont;
+ }
+}
+
+
+void wxGridCellAttr::GetAlignment(int *hAlign, int *vAlign) const
+{
+ if (HasAlignment())
+ {
+ if ( hAlign ) *hAlign = m_hAlign;
+ if ( vAlign ) *vAlign = m_vAlign;
+ }
+ else if (m_defGridAttr != this)
+ m_defGridAttr->GetAlignment(hAlign, vAlign);
+ else
+ {
+ wxFAIL_MSG(wxT("Missing default cell attribute"));
+ }
+}
+
+
+// GetRenderer and GetEditor use a slightly different decision path about
+// which attribute to use. If a non-default attr object has one then it is
+// used, otherwise the default editor or renderer is fetched from the grid and
+// used. It should be the default for the data type of the cell. If it is
+// NULL (because the table has a type that the grid does not have in its
+// registry,) then the grid's default editor or renderer is used.
+
+wxGridCellRenderer* wxGridCellAttr::GetRenderer(wxGrid* grid, int row, int col) const
+{
+ wxGridCellRenderer* renderer = NULL;
+
+ if ( m_defGridAttr != this || grid == NULL )
+ {
+ renderer = m_renderer; // use local attribute
+ if ( renderer )
+ renderer->IncRef();
+ }
+
+ if ( !renderer && grid ) // get renderer for the data type
+ {
+ // GetDefaultRendererForCell() will do IncRef() for us
+ renderer = grid->GetDefaultRendererForCell(row, col);
+ }
+
+ if ( !renderer )
+ {
+ // if we still don't have one then use the grid default
+ // (no need for IncRef() here neither)
+ renderer = m_defGridAttr->GetRenderer(NULL,0,0);
+ }
+
+ if ( !renderer)
+ {
+ wxFAIL_MSG(wxT("Missing default cell attribute"));
+ }
+
+ return renderer;
+}
+
+wxGridCellEditor* wxGridCellAttr::GetEditor(wxGrid* grid, int row, int col) const
+{
+ wxGridCellEditor* editor = NULL;
+
+ if ( m_defGridAttr != this || grid == NULL )
+ {
+ editor = m_editor; // use local attribute
+ if ( editor )
+ editor->IncRef();
+ }
+
+ if ( !editor && grid ) // get renderer for the data type
+ editor = grid->GetDefaultEditorForCell(row, col);
+
+ if ( !editor )
+ // if we still don't have one then use the grid default
+ editor = m_defGridAttr->GetEditor(NULL,0,0);
+
+ if ( !editor )
+ {
+ wxFAIL_MSG(wxT("Missing default cell attribute"));
+ }
+
+ return editor;
+}
+
+// ----------------------------------------------------------------------------
+// wxGridCellAttrData
+// ----------------------------------------------------------------------------
+
+void wxGridCellAttrData::SetAttr(wxGridCellAttr *attr, int row, int col)
+{
+ int n = FindIndex(row, col);
+ if ( n == wxNOT_FOUND )
+ {
+ // add the attribute
+ m_attrs.Add(new wxGridCellWithAttr(row, col, attr));
+ }
+ else
+ {
+ if ( attr )
+ {
+ // change the attribute
+ m_attrs[(size_t)n].attr = attr;
+ }
+ else
+ {
+ // remove this attribute
+ m_attrs.RemoveAt((size_t)n);
+ }
+ }
+}
+
+wxGridCellAttr *wxGridCellAttrData::GetAttr(int row, int col) const
+{
+ wxGridCellAttr *attr = (wxGridCellAttr *)NULL;
+
+ int n = FindIndex(row, col);
+ if ( n != wxNOT_FOUND )
+ {
+ attr = m_attrs[(size_t)n].attr;
+ attr->IncRef();
+ }
+
+ return attr;
+}
+
+void wxGridCellAttrData::UpdateAttrRows( size_t pos, int numRows )
+{
+ size_t count = m_attrs.GetCount();
+ for ( size_t n = 0; n < count; n++ )
+ {
+ wxGridCellCoords& coords = m_attrs[n].coords;
+ wxCoord row = coords.GetRow();
+ if ((size_t)row >= pos)
+ {
+ if (numRows > 0)
+ {
+ // If rows inserted, include row counter where necessary
+ coords.SetRow(row + numRows);
+ }
+ else if (numRows < 0)
+ {
+ // If rows deleted ...
+ if ((size_t)row >= pos - numRows)
+ {
+ // ...either decrement row counter (if row still exists)...
+ coords.SetRow(row + numRows);
+ }
+ else
+ {
+ // ...or remove the attribute
+ m_attrs.RemoveAt((size_t)n);
+ n--; count--;
+ }
+ }
+ }
+ }
+}
+
+void wxGridCellAttrData::UpdateAttrCols( size_t pos, int numCols )
+{
+ size_t count = m_attrs.GetCount();
+ for ( size_t n = 0; n < count; n++ )
+ {
+ wxGridCellCoords& coords = m_attrs[n].coords;
+ wxCoord col = coords.GetCol();
+ if ( (size_t)col >= pos )
+ {
+ if ( numCols > 0 )
+ {
+ // If rows inserted, include row counter where necessary
+ coords.SetCol(col + numCols);
+ }
+ else if (numCols < 0)
+ {
+ // If rows deleted ...
+ if ((size_t)col >= pos - numCols)
+ {
+ // ...either decrement row counter (if row still exists)...
+ coords.SetCol(col + numCols);
+ }
+ else
+ {
+ // ...or remove the attribute
+ m_attrs.RemoveAt((size_t)n);
+ n--; count--;
+ }
+ }
+ }
+ }
+}
+
+int wxGridCellAttrData::FindIndex(int row, int col) const
+{
+ size_t count = m_attrs.GetCount();
+ for ( size_t n = 0; n < count; n++ )
+ {
+ const wxGridCellCoords& coords = m_attrs[n].coords;
+ if ( (coords.GetRow() == row) && (coords.GetCol() == col) )
+ {
+ return n;
+ }
+ }
+
+ return wxNOT_FOUND;
+}
+
+// ----------------------------------------------------------------------------
+// wxGridRowOrColAttrData
+// ----------------------------------------------------------------------------
+
+wxGridRowOrColAttrData::~wxGridRowOrColAttrData()
+{
+ size_t count = m_attrs.Count();
+ for ( size_t n = 0; n < count; n++ )
+ {
+ m_attrs[n]->DecRef();
+ }
+}
+
+wxGridCellAttr *wxGridRowOrColAttrData::GetAttr(int rowOrCol) const
+{
+ wxGridCellAttr *attr = (wxGridCellAttr *)NULL;
+
+ int n = m_rowsOrCols.Index(rowOrCol);
+ if ( n != wxNOT_FOUND )
+ {
+ attr = m_attrs[(size_t)n];
+ attr->IncRef();
+ }