// Creates the actual edit control
virtual void Create(wxWindow* parent,
wxWindowID id,
- const wxPoint& pos,
- const wxSize& size,
wxEvtHandler* evtHandler) = 0;
// Size and position the edit control
// Reset the value in the control back to its starting value
virtual void Reset() = 0;
+ // If the editor is enabled by pressing keys on the grid, this
+ // will be called to let the editor do something about that key
+ // if desired.
+ virtual void StartingKey(wxKeyEvent& event);
+
// Some types of controls on some platforms may need some help
// with the Return key.
virtual void HandleReturn(wxKeyEvent& event);
virtual void Create(wxWindow* parent,
wxWindowID id,
- const wxPoint& pos,
- const wxSize& size,
wxEvtHandler* evtHandler);
virtual void BeginEdit(int row, int col, wxGrid* grid,
wxGrid* grid, wxGridCellAttr* attr);
virtual void Reset();
+ virtual void StartingKey(wxKeyEvent& event);
virtual void HandleReturn(wxKeyEvent& event);
public:
wxGrid()
{
- m_table = (wxGridTableBase *) NULL;
- m_gridWin = (wxGridWindow *) NULL;
- m_rowLabelWin = (wxGridRowLabelWindow *) NULL;
- m_colLabelWin = (wxGridColLabelWindow *) NULL;
- m_cornerLabelWin = (wxGridCornerLabelWindow *) NULL;
- m_cellEditCtrl = (wxWindow *) NULL;
+ Create();
}
wxGrid( wxWindow *parent,
void EnableCellEditControl( bool enable );
bool IsCellEditControlEnabled()
- { return (m_cellEditCtrl && m_cellEditCtrlEnabled); }
+ { return m_cellEditCtrlEnabled; }
void ShowCellEditControl();
void HideCellEditControl();
// looks for the attr in cache, if not found asks the table and caches the
// result
wxGridCellAttr *GetCellAttr(int row, int col) const;
+ wxGridCellAttr *GetCellAttr(const wxGridCellCoords& coords )
+ { return GetCellAttr( coords.GetRow(), coords.GetCol() ); }
// the default cell attr object for cells that don't have their own
wxGridCellAttr* m_defaultCellAttr;
wxCursor m_colResizeCursor;
bool m_editable; // applies to whole grid
- int m_editCtrlType; // for current cell
- wxWindow* m_cellEditCtrl;
bool m_cellEditCtrlEnabled;
}
+void wxGridCellEditor::StartingKey(wxKeyEvent& event)
+{
+}
+
+
+
wxGridCellTextEditor::wxGridCellTextEditor()
{
}
void wxGridCellTextEditor::Create(wxWindow* parent,
wxWindowID id,
- const wxPoint& pos,
- const wxSize& size,
wxEvtHandler* evtHandler)
{
- m_control = new wxTextCtrl(parent, -1, "", pos, size
+ m_control = new wxTextCtrl(parent, -1, "",
+ wxDefaultPosition, wxDefaultSize
#if defined(__WXMSW__)
- , wxTE_MULTILINE | wxTE_NO_VSCROLL
+ , wxTE_MULTILINE | wxTE_NO_VSCROLL // necessary ???
#endif
);
if (changed)
grid->GetTable()->SetValue(row, col, value);
+
m_startValue = "";
+ ((wxTextCtrl*)m_control)->SetValue(m_startValue);
return changed;
}
((wxTextCtrl*)m_control)->SetInsertionPointEnd();
}
+
+void wxGridCellTextEditor::StartingKey(wxKeyEvent& event)
+{
+ wxASSERT_MSG(m_control,
+ wxT("The wxGridCellEditor must be Created first!"));
+
+ int code = event.KeyCode();
+ if (code >= 32 && code < 255) {
+ wxString st((char)code);
+ if (! event.ShiftDown())
+ st.LowerCase();
+ ((wxTextCtrl*)m_control)->AppendText(st);
+ }
+}
+
+
void wxGridCellTextEditor::HandleReturn(wxKeyEvent& event)
{
#if defined(__WXMOTIF__) || defined(__WXGTK__)
{
case WXK_ESCAPE:
m_editor->Reset();
+ m_grid->EnableCellEditControl(FALSE);
break;
- case WXK_UP:
- case WXK_DOWN:
- case WXK_LEFT:
- case WXK_RIGHT:
- case WXK_PRIOR:
- case WXK_NEXT:
- case WXK_SPACE:
- // send the event to the parent grid, skipping the
- // event if nothing happens
- //
- event.Skip( m_grid->ProcessEvent( event ) );
- break;
-
+// case WXK_UP:
+// case WXK_DOWN:
+// case WXK_LEFT:
+// case WXK_RIGHT:
+// case WXK_PRIOR:
+// case WXK_NEXT:
+// case WXK_SPACE:
+// case WXK_HOME:
+// case WXK_END:
+// // send the event to the parent grid, skipping the
+// // event if nothing happens
+// //
+// event.Skip( m_grid->ProcessEvent( event ) );
+// break;
+
+ case WXK_TAB:
case WXK_RETURN:
if (!m_grid->ProcessEvent(event))
m_editor->HandleReturn(event);
break;
- case WXK_HOME:
- case WXK_END:
- // send the event to the parent grid, skipping the
- // event if nothing happens
- //
- event.Skip( m_grid->ProcessEvent( event ) );
- break;
default:
event.Skip();
m_table = (wxGridTableBase *) NULL;
m_ownTable = FALSE;
- m_cellEditCtrl = (wxWindow *) NULL;
+
+ m_cellEditCtrlEnabled = FALSE;
m_defaultCellAttr = new wxGridCellAttr;
m_defaultCellAttr->SetDefAttr(m_defaultCellAttr);
m_inOnKeyDown = FALSE;
m_batchCount = 0;
- // TODO: extend this to other types of controls
- //
- m_cellEditCtrl = new wxGridTextCtrl( m_gridWin,
- this,
- FALSE,
- wxGRID_CELLCTRL,
- "",
- wxPoint(1,1),
- wxSize(1,1)
-#if defined(__WXMSW__)
- , wxTE_MULTILINE | wxTE_NO_VSCROLL
-#endif
- );
-
- m_cellEditCtrl->Show( FALSE );
- m_cellEditCtrlEnabled = FALSE;
- m_editCtrlType = wxGRID_TEXTCTRL;
}
}
break;
+ case WXK_TAB:
+ if (event.ShiftDown())
+ MoveCursorLeft();
+ else
+ MoveCursorRight();
+ break;
+
case WXK_HOME:
if ( event.ControlDown() )
{
case WXK_SHIFT:
case WXK_ALT:
case WXK_CONTROL:
+ case WXK_CAPITAL:
event.Skip();
break;
//
if ( !IsCellEditControlEnabled() )
EnableCellEditControl( TRUE );
- wxKeyEvent evt(event);
- evt.SetEventObject( m_cellEditCtrl );
- m_cellEditCtrl->GetEventHandler()->ProcessEvent( evt );
+ if (IsCellEditControlEnabled()) {
+ wxGridCellAttr* attr = GetCellAttr(m_currentCellCoords);
+ attr->GetEditor()->StartingKey(event);
+ attr->DecRef();
+ }
break;
}
}
{
HideCellEditControl();
SaveEditControlValue();
+ EnableCellEditControl(FALSE);
// Clear the old current cell highlight
wxRect r = BlockToDeviceRect(m_currentCellCoords, m_currentCellCoords);
{
m_editable = edit;
- // TODO: extend this for other edit control types
- //
- if ( m_editCtrlType == wxGRID_TEXTCTRL )
- {
- ((wxTextCtrl *)m_cellEditCtrl)->SetEditable( m_editable );
- }
+ EnableCellEditControl(m_editable);
}
}
void wxGrid::EnableCellEditControl( bool enable )
{
+ if (! m_editable)
+ return;
+
if ( m_currentCellCoords == wxGridNoCellCoords )
SetCurrentCell( 0, 0 );
- if ( m_cellEditCtrl &&
- enable != m_cellEditCtrlEnabled )
- {
+ if ( enable != m_cellEditCtrlEnabled )
+ {
if ( enable )
{
m_cellEditCtrlEnabled = enable;
SetEditControlValue();
- // requires m_cellEditCtrlEnabled to be already true
ShowCellEditControl();
}
else
{
HideCellEditControl();
- // requires m_cellEditCtrlEnabled to be still true
SaveEditControlValue();
m_cellEditCtrlEnabled = enable;
}
void wxGrid::ShowCellEditControl()
{
- wxRect rect;
-
if ( IsCellEditControlEnabled() )
{
if ( !IsVisible( m_currentCellCoords ) )
}
else
{
- rect = CellToRect( m_currentCellCoords );
+ wxRect rect = CellToRect( m_currentCellCoords );
+ int row = m_currentCellCoords.GetRow();
+ int col = m_currentCellCoords.GetCol();
// convert to scrolled coords
//
//
int extra;
#if defined(__WXMOTIF__)
- if ( m_currentCellCoords.GetRow() == 0 ||
- m_currentCellCoords.GetCol() == 0 )
+ if ( row == 0 || col == 0 )
{
extra = 2;
}
extra = 4;
}
#else
- if ( m_currentCellCoords.GetRow() == 0 ||
- m_currentCellCoords.GetCol() == 0 )
+ if ( row == 0 || col == 0 )
{
extra = 1;
}
rect.SetBottom( rect.GetBottom() + 2*extra );
#endif
- m_cellEditCtrl->SetSize( rect );
- m_cellEditCtrl->Show( TRUE );
-
- switch ( m_editCtrlType )
- {
- case wxGRID_TEXTCTRL:
- ((wxTextCtrl *) m_cellEditCtrl)->SetInsertionPointEnd();
- break;
-
- case wxGRID_CHECKBOX:
- // TODO: anything ???
- //
- break;
-
- case wxGRID_CHOICE:
- // TODO: anything ???
- //
- break;
-
- case wxGRID_COMBOBOX:
- // TODO: anything ???
- //
- break;
+ wxGridCellAttr* attr = GetCellAttr(row, col);
+ wxGridCellEditor* editor = attr->GetEditor();
+ if (! editor->IsCreated()) {
+ editor->Create(m_gridWin, -1,
+ new wxGridCellEditorEvtHandler(this, editor));
}
- m_cellEditCtrl->SetFocus();
- }
+ editor->SetSize( rect );
+ editor->Show( TRUE );
+ editor->BeginEdit(row, col, this, attr);
+ attr->DecRef();
+ }
}
}
{
if ( IsCellEditControlEnabled() )
{
- m_cellEditCtrl->Show( FALSE );
- SetFocus();
+ int row = m_currentCellCoords.GetRow();
+ int col = m_currentCellCoords.GetCol();
+
+ wxGridCellAttr* attr = GetCellAttr(row, col);
+ attr->GetEditor()->Show( FALSE );
+ attr->DecRef();
+ m_gridWin->SetFocus();
}
}
void wxGrid::SetEditControlValue( const wxString& value )
{
- if ( m_table )
- {
- wxString s;
- if ( !value )
- s = GetCellValue(m_currentCellCoords);
- else
- s = value;
-
- if ( IsCellEditControlEnabled() )
- {
- switch ( m_editCtrlType )
- {
- case wxGRID_TEXTCTRL:
- ((wxGridTextCtrl *)m_cellEditCtrl)->SetStartValue(s);
- break;
-
- case wxGRID_CHECKBOX:
- // TODO: implement this
- //
- break;
-
- case wxGRID_CHOICE:
- // TODO: implement this
- //
- break;
-
- case wxGRID_COMBOBOX:
- // TODO: implement this
- //
- break;
- }
- }
- }
}
void wxGrid::SaveEditControlValue()
{
- if ( m_table )
- {
- wxWindow *ctrl = (wxWindow *)NULL;
-
- if ( IsCellEditControlEnabled() )
- {
- ctrl = m_cellEditCtrl;
- }
- else
- {
- return;
- }
-
- bool valueChanged = FALSE;
-
- switch ( m_editCtrlType )
- {
- case wxGRID_TEXTCTRL:
- valueChanged = (((wxGridTextCtrl *)ctrl)->GetValue() !=
- ((wxGridTextCtrl *)ctrl)->GetStartValue());
- SetCellValue( m_currentCellCoords,
- ((wxTextCtrl *) ctrl)->GetValue() );
- break;
-
- case wxGRID_CHECKBOX:
- // TODO: implement this
- //
- break;
+ if (IsCellEditControlEnabled()) {
+ int row = m_currentCellCoords.GetRow();
+ int col = m_currentCellCoords.GetCol();
- case wxGRID_CHOICE:
- // TODO: implement this
- //
- break;
+ wxGridCellAttr* attr = GetCellAttr(row, col);
+ bool changed = attr->GetEditor()->EndEdit(row, col, TRUE, this, attr);
- case wxGRID_COMBOBOX:
- // TODO: implement this
- //
- break;
- }
+ attr->DecRef();
- if ( valueChanged )
- {
+ if (changed) {
SendEvent( EVT_GRID_CELL_CHANGE,
m_currentCellCoords.GetRow(),
m_currentCellCoords.GetCol() );