+wxGridCellFloatEditor::wxGridCellFloatEditor(int width, int precision)
+{
+ m_width = width;
+ m_precision = precision;
+}
+
+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(wxT("%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();
+}
+
+void wxGridCellFloatEditor::SetParameters(const wxString& params)
+{
+ if ( !params )
+ {
+ // reset to default
+ m_width =
+ m_precision = -1;
+ }
+ else
+ {
+ long tmp;
+ if ( params.BeforeFirst(_T(',')).ToLong(&tmp) )
+ {
+ m_width = (int)tmp;
+
+ if ( params.AfterFirst(_T(',')).ToLong(&tmp) )
+ {
+ m_precision = (int)tmp;
+
+ // skip the error message below
+ return;
+ }
+ }
+
+ wxLogDebug(_T("Invalid wxGridCellFloatEditor parameter string '%s' ignored"), params.c_str());
+ }
+}
+
+wxString wxGridCellFloatEditor::GetString() const
+{
+ wxString fmt;
+ if ( m_width == -1 )
+ {
+ // default width/precision
+ fmt = _T("%g");
+ }
+ else if ( m_precision == -1 )
+ {
+ // default precision
+ fmt.Printf(_T("%%%d.g"), m_width);
+ }
+ else
+ {
+ fmt.Printf(_T("%%%d.%dg"), m_width, m_precision);
+ }
+
+ return wxString::Format(fmt, m_valueOld);
+}
+
+bool wxGridCellFloatEditor::IsAcceptedKey(wxKeyEvent& event)
+{
+ if ( wxGridCellEditor::IsAcceptedKey(event) )
+ {
+ int keycode = event.GetKeyCode();
+ switch ( keycode )
+ {
+ case WXK_NUMPAD0:
+ case WXK_NUMPAD1:
+ case WXK_NUMPAD2:
+ case WXK_NUMPAD3:
+ case WXK_NUMPAD4:
+ case WXK_NUMPAD5:
+ case WXK_NUMPAD6:
+ case WXK_NUMPAD7:
+ case WXK_NUMPAD8:
+ case WXK_NUMPAD9:
+ case WXK_ADD:
+ case WXK_NUMPAD_ADD:
+ case WXK_SUBTRACT:
+ case WXK_NUMPAD_SUBTRACT:
+ case WXK_DECIMAL:
+ case WXK_NUMPAD_DECIMAL:
+ return TRUE;
+
+ default:
+ // additionally accept 'e' as in '1e+6'
+ if ( (keycode < 128) &&
+ (isdigit(keycode) || tolower(keycode) == 'e') )
+ return TRUE;
+ }
+ }
+
+ return FALSE;
+}
+
+// ----------------------------------------------------------------------------
+// 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
+ {
+ wxString cellval( grid->GetTable()->GetValue(row, col) );
+ m_startValue = !( !cellval || (cellval == "0") );
+ }
+ 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());
+}
+
+bool wxGridCellBoolEditor::IsAcceptedKey(wxKeyEvent& event)
+{
+ if ( wxGridCellEditor::IsAcceptedKey(event) )
+ {
+ int keycode = event.GetKeyCode();
+ switch ( keycode )
+ {
+ case WXK_MULTIPLY:
+ case WXK_NUMPAD_MULTIPLY:
+ case WXK_ADD:
+ case WXK_NUMPAD_ADD:
+ case WXK_SUBTRACT:
+ case WXK_NUMPAD_SUBTRACT:
+ case WXK_SPACE:
+ case '+':
+ case '-':
+ return TRUE;
+ }
+ }
+
+ return FALSE;
+}
+
+// ----------------------------------------------------------------------------
+// wxGridCellChoiceEditor
+// ----------------------------------------------------------------------------
+
+wxGridCellChoiceEditor::wxGridCellChoiceEditor(size_t count,
+ const wxString 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_NUMPAD_ENTER:
+ 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:
+ case WXK_NUMPAD_ENTER:
+ 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,