]> git.saurik.com Git - wxWidgets.git/commitdiff
added combobox editor
authorVadim Zeitlin <vadim@wxwidgets.org>
Tue, 22 Feb 2000 16:57:12 +0000 (16:57 +0000)
committerVadim Zeitlin <vadim@wxwidgets.org>
Tue, 22 Feb 2000 16:57:12 +0000 (16:57 +0000)
git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@6214 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775

include/wx/generic/grid.h
samples/newgrid/griddemo.cpp
src/generic/grid.cpp

index 2420ed4c026ac410a4145c7c11ea5bcd36908757..e13f2a4a021aeefd591765b20a56ed0718ff1239 100644 (file)
@@ -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
index c308c4e38ac5e0855e8a48d29db07fd2400e4e30..62f86554f9405205f49a4959124c9454b4a110c3 100644 (file)
@@ -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();
 }
index 4779b848a901d2f6f576465a8b0d7362ba562f7d..dd06aeda0b1d453f97319b3dbf3acc7b566479cd 100644 (file)
@@ -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;
 }