From 4ee5fc9c92dcd59df908170ccf812c35d263d555 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Tue, 22 Feb 2000 16:57:12 +0000 Subject: [PATCH] added combobox editor git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@6214 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- include/wx/generic/grid.h | 28 ++++++++++++ samples/newgrid/griddemo.cpp | 7 ++- src/generic/grid.cpp | 83 +++++++++++++++++++++++++++++++++++- 3 files changed, 115 insertions(+), 3 deletions(-) diff --git a/include/wx/generic/grid.h b/include/wx/generic/grid.h index 2420ed4c02..e13f2a4a02 100644 --- a/include/wx/generic/grid.h +++ b/include/wx/generic/grid.h @@ -77,6 +77,7 @@ class WXDLLEXPORT wxGridWindow; class WXDLLEXPORT wxGridTypeRegistry; class WXDLLEXPORT wxCheckBox; +class WXDLLEXPORT wxComboBox; class WXDLLEXPORT wxTextCtrl; class WXDLLEXPORT wxSpinCtrl; @@ -409,6 +410,33 @@ private: bool m_startValue; }; +// the editor for string data allowing to choose from the list of strings +class WXDLLEXPORT wxGridCellChoiceEditor : public wxGridCellEditor +{ +public: + // if !allowOthers, user can't type a string not in choices array + wxGridCellChoiceEditor(size_t count, const wxChar* choices[], + bool allowOthers = FALSE); + + virtual void Create(wxWindow* parent, + wxWindowID id, + wxEvtHandler* evtHandler); + + virtual void PaintBackground(const wxRect& rectCell, wxGridCellAttr *attr); + + virtual void BeginEdit(int row, int col, wxGrid* grid); + virtual bool EndEdit(int row, int col, bool saveValue, wxGrid* grid); + + virtual void Reset(); + +protected: + wxComboBox *Combo() const { return (wxComboBox *)m_control; } + +private: + wxString m_startValue; + wxArrayString m_choices; + bool m_allowOthers; +}; // ---------------------------------------------------------------------------- // wxGridCellAttr: this class can be used to alter the cells appearance in // the grid by changing their colour/font/... from default. An object of this diff --git a/samples/newgrid/griddemo.cpp b/samples/newgrid/griddemo.cpp index c308c4e38a..62f86554f9 100644 --- a/samples/newgrid/griddemo.cpp +++ b/samples/newgrid/griddemo.cpp @@ -960,12 +960,17 @@ BugsGridFrame::BugsGridFrame() grid->SetTable(table, TRUE); wxGridCellAttr *attrRO = new wxGridCellAttr, - *attrRangeEditor = new wxGridCellAttr; + *attrRangeEditor = new wxGridCellAttr, + *attrCombo = new wxGridCellAttr; + attrRO->SetReadOnly(); attrRangeEditor->SetEditor(new wxGridCellNumberEditor(1, 5)); + attrCombo->SetEditor(new wxGridCellChoiceEditor(WXSIZEOF(severities), + severities)); grid->SetColAttr(Col_Id, attrRO); grid->SetColAttr(Col_Priority, attrRangeEditor); + grid->SetColAttr(Col_Severity, attrCombo); grid->AutoSizeColumns(); } diff --git a/src/generic/grid.cpp b/src/generic/grid.cpp index 4779b848a9..dd06aeda0b 100644 --- a/src/generic/grid.cpp +++ b/src/generic/grid.cpp @@ -41,6 +41,7 @@ #include "wx/log.h" #include "wx/textctrl.h" #include "wx/checkbox.h" + #include "wx/combobox.h" #include "wx/valtext.h" #endif @@ -858,6 +859,84 @@ void wxGridCellBoolEditor::StartingClick() CBox()->SetValue(!CBox()->GetValue()); } +// ---------------------------------------------------------------------------- +// wxGridCellChoiceEditor +// ---------------------------------------------------------------------------- + +wxGridCellChoiceEditor::wxGridCellChoiceEditor(size_t count, + const wxChar* choices[], + bool allowOthers) + : m_allowOthers(allowOthers) +{ + m_choices.Alloc(count); + for ( size_t n = 0; n < count; n++ ) + { + m_choices.Add(choices[n]); + } +} + +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& WXUNUSED(rectCell), + wxGridCellAttr * WXUNUSED(attr)) +{ + // as we fill the entire client area, don't do anything here to minimize + // flicker +} + +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); + Combo()->SetInsertionPointEnd(); + Combo()->SetFocus(); +} + +bool wxGridCellChoiceEditor::EndEdit(int row, int col, + bool saveValue, + 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(); +} + // ---------------------------------------------------------------------------- // wxGridCellEditorEvtHandler // ---------------------------------------------------------------------------- @@ -5363,7 +5442,7 @@ int wxGrid::YToRow( int y ) return i; } - return m_numRows; //-1; + return -1; } @@ -5377,7 +5456,7 @@ int wxGrid::XToCol( int x ) return i; } - return m_numCols; //-1; + return -1; } -- 2.45.2