From 9c687f055cccb3522dc8789b9639512094307a26 Mon Sep 17 00:00:00 2001 From: Paul Licameli Date: Sun, 26 Jun 2016 19:05:06 -0400 Subject: [PATCH] Generalize TimeRenderer and TimeEditor to handle frequencies in grid --- src/LabelDialog.cpp | 3 +- src/LabelDialog.h | 2 +- src/widgets/Grid.cpp | 105 ++++++++++++++++++++++++------------------- src/widgets/Grid.h | 46 ++++++++++--------- 4 files changed, 87 insertions(+), 69 deletions(-) diff --git a/src/LabelDialog.cpp b/src/LabelDialog.cpp index 5b0afc2a6..819fe4d26 100644 --- a/src/LabelDialog.cpp +++ b/src/LabelDialog.cpp @@ -163,7 +163,8 @@ LabelDialog::LabelDialog(wxWindow *parent, // do it for us. (The DecRef() that is needed after GetDefaultEditorForType // becomes the duty of the wxGridCellAttr objects after we set them in the grid.) mChoiceEditor = (ChoiceEditor *) mGrid->GetDefaultEditorForType(GRID_VALUE_CHOICE); - mTimeEditor = (TimeEditor *) mGrid->GetDefaultEditorForType(GRID_VALUE_TIME); + mTimeEditor = static_cast + (mGrid->GetDefaultEditorForType(GRID_VALUE_TIME)); // Initialize and set the track name column attributes wxGridCellAttr *attr; diff --git a/src/LabelDialog.h b/src/LabelDialog.h index 66d071504..4da0c72c2 100644 --- a/src/LabelDialog.h +++ b/src/LabelDialog.h @@ -80,7 +80,7 @@ class LabelDialog final : public wxDialog Grid *mGrid; ChoiceEditor *mChoiceEditor; - TimeEditor *mTimeEditor; + NumericEditor *mTimeEditor; RowDataArray mData; diff --git a/src/widgets/Grid.cpp b/src/widgets/Grid.cpp index 9f27a4d30..e55e2c508 100644 --- a/src/widgets/Grid.cpp +++ b/src/widgets/Grid.cpp @@ -25,27 +25,25 @@ #include "Grid.h" #include "NumericTextCtrl.h" +#include "../SelectedRegion.h" -TimeEditor::TimeEditor() -{ - TimeEditor(wxT("seconds"), 44100); -} - -TimeEditor::TimeEditor(const wxString &format, double rate) +NumericEditor::NumericEditor + (NumericConverter::Type type, const wxString &format, double rate) { + mType = type; mFormat = format; mRate = rate; mOld = 0.0; } -TimeEditor::~TimeEditor() +NumericEditor::~NumericEditor() { } -void TimeEditor::Create(wxWindow *parent, wxWindowID id, wxEvtHandler *handler) +void NumericEditor::Create(wxWindow *parent, wxWindowID id, wxEvtHandler *handler) { wxASSERT(parent); // to justify safenew - m_control = safenew NumericTextCtrl(NumericConverter::TIME, parent, + auto control = safenew NumericTextCtrl(mType, parent, wxID_ANY, mFormat, mOld, @@ -53,11 +51,14 @@ void TimeEditor::Create(wxWindow *parent, wxWindowID id, wxEvtHandler *handler) wxDefaultPosition, wxDefaultSize, true); + if (mType == NumericTextCtrl::FREQUENCY) + control->SetInvalidValue(SelectedRegion::UndefinedFrequency); + m_control = control; wxGridCellEditor::Create(parent, id, handler); } -void TimeEditor::SetSize(const wxRect &rect) +void NumericEditor::SetSize(const wxRect &rect) { wxSize size = m_control->GetSize(); @@ -68,22 +69,24 @@ void TimeEditor::SetSize(const wxRect &rect) m_control->Move(x, y); } -void TimeEditor::BeginEdit(int row, int col, wxGrid *grid) +void NumericEditor::BeginEdit(int row, int col, wxGrid *grid) { wxGridTableBase *table = grid->GetTable(); mOldString = table->GetValue(row, col); mOldString.ToDouble(&mOld); - GetTimeCtrl()->SetValue(mOld); - GetTimeCtrl()->EnableMenu(); + auto control = GetNumericTextControl(); + control->SetValue(mOld); + control->EnableMenu(); - GetTimeCtrl()->SetFocus(); + control->SetFocus(); } -bool TimeEditor::EndEdit(int WXUNUSED(row), int WXUNUSED(col), const wxGrid *WXUNUSED(grid), const wxString &WXUNUSED(oldval), wxString *newval) + +bool NumericEditor::EndEdit(int WXUNUSED(row), int WXUNUSED(col), const wxGrid *WXUNUSED(grid), const wxString &WXUNUSED(oldval), wxString *newval) { - double newtime = GetTimeCtrl()->GetValue(); + double newtime = GetNumericTextControl()->GetValue(); bool changed = newtime != mOld; if (changed) { @@ -94,17 +97,17 @@ bool TimeEditor::EndEdit(int WXUNUSED(row), int WXUNUSED(col), const wxGrid *WXU return changed; } -void TimeEditor::ApplyEdit(int row, int col, wxGrid *grid) +void NumericEditor::ApplyEdit(int row, int col, wxGrid *grid) { grid->GetTable()->SetValue(row, col, mValueAsString); } -void TimeEditor::Reset() +void NumericEditor::Reset() { - GetTimeCtrl()->SetValue(mOld); + GetNumericTextControl()->SetValue(mOld); } -bool TimeEditor::IsAcceptedKey(wxKeyEvent &event) +bool NumericEditor::IsAcceptedKey(wxKeyEvent &event) { if (wxGridCellEditor::IsAcceptedKey(event)) { if (event.GetKeyCode() == WXK_RETURN) { @@ -116,41 +119,41 @@ bool TimeEditor::IsAcceptedKey(wxKeyEvent &event) } // Clone is required by wxwidgets; implemented via copy constructor -wxGridCellEditor *TimeEditor::Clone() const +wxGridCellEditor *NumericEditor::Clone() const { - return safenew TimeEditor(mFormat, mRate); + return safenew NumericEditor{ mType, mFormat, mRate }; } -wxString TimeEditor::GetValue() const +wxString NumericEditor::GetValue() const { - return wxString::Format(wxT("%g"), GetTimeCtrl()->GetValue()); + return wxString::Format(wxT("%g"), GetNumericTextControl()->GetValue()); } -wxString TimeEditor::GetFormat() +wxString NumericEditor::GetFormat() const { return mFormat; } -double TimeEditor::GetRate() +double NumericEditor::GetRate() const { return mRate; } -void TimeEditor::SetFormat(const wxString &format) +void NumericEditor::SetFormat(const wxString &format) { mFormat = format; } -void TimeEditor::SetRate(double rate) +void NumericEditor::SetRate(double rate) { mRate = rate; } -TimeRenderer::~TimeRenderer() +NumericRenderer::~NumericRenderer() { } -void TimeRenderer::Draw(wxGrid &grid, +void NumericRenderer::Draw(wxGrid &grid, wxGridCellAttr &attr, wxDC &dc, const wxRect &rect, @@ -161,25 +164,26 @@ void TimeRenderer::Draw(wxGrid &grid, wxGridCellRenderer::Draw(grid, attr, dc, rect, row, col, isSelected); wxGridTableBase *table = grid.GetTable(); - TimeEditor *te = (TimeEditor *) grid.GetCellEditor(row, col); + NumericEditor *ne = + static_cast(grid.GetCellEditor(row, col)); wxString tstr; - if (te) { + if (ne) { double value; table->GetValue(row, col).ToDouble(&value); - NumericTextCtrl tt(NumericConverter::TIME, &grid, + NumericTextCtrl tt(mType, &grid, wxID_ANY, - te->GetFormat(), + ne->GetFormat(), value, - te->GetRate(), + ne->GetRate(), wxPoint(10000, 10000), // create offscreen wxDefaultSize, true); tstr = tt.GetString(); - te->DecRef(); + ne->DecRef(); } dc.SetBackgroundMode(wxTRANSPARENT); @@ -212,39 +216,40 @@ void TimeRenderer::Draw(wxGrid &grid, grid.DrawTextRectangle(dc, tstr, rect, hAlign, vAlign); } -wxSize TimeRenderer::GetBestSize(wxGrid &grid, +wxSize NumericRenderer::GetBestSize(wxGrid &grid, wxGridCellAttr & WXUNUSED(attr), wxDC & WXUNUSED(dc), int row, int col) { wxGridTableBase *table = grid.GetTable(); - TimeEditor *te = (TimeEditor *) grid.GetCellEditor(row, col); + NumericEditor *ne = + static_cast(grid.GetCellEditor(row, col)); wxSize sz; - if (te) { + if (ne) { double value; table->GetValue(row, col).ToDouble(&value); - NumericTextCtrl tt(NumericConverter::TIME, &grid, + NumericTextCtrl tt(mType, &grid, wxID_ANY, - te->GetFormat(), + ne->GetFormat(), value, - te->GetRate(), + ne->GetRate(), wxPoint(10000, 10000), // create offscreen wxDefaultSize, true); sz = tt.GetSize(); - te->DecRef(); + ne->DecRef(); } return sz; } // Clone is required by wxwidgets; implemented via copy constructor -wxGridCellRenderer *TimeRenderer::Clone() const +wxGridCellRenderer *NumericRenderer::Clone() const { - return safenew TimeRenderer(); + return safenew NumericRenderer{ mType }; } ChoiceEditor::ChoiceEditor(size_t count, const wxString choices[]) @@ -389,8 +394,14 @@ Grid::Grid(wxWindow *parent, // RegisterDataType takes ownership of renderer and editor RegisterDataType(GRID_VALUE_TIME, - safenew TimeRenderer, - safenew TimeEditor); + safenew NumericRenderer{ NumericConverter::TIME }, + safenew NumericEditor + { NumericTextCtrl::TIME, wxT("seconds"), 44100.0 }); + + RegisterDataType(GRID_VALUE_FREQUENCY, + safenew NumericRenderer{ NumericConverter::FREQUENCY }, + safenew NumericEditor + { NumericTextCtrl::FREQUENCY, wxT("Hz"), 44100.0 }); RegisterDataType(GRID_VALUE_CHOICE, safenew wxGridCellStringRenderer, diff --git a/src/widgets/Grid.h b/src/widgets/Grid.h index 538a336a8..1a354e8ef 100644 --- a/src/widgets/Grid.h +++ b/src/widgets/Grid.h @@ -18,6 +18,7 @@ #include #include #include +#include "NumericTextCtrl.h" #if wxUSE_ACCESSIBILITY #include @@ -29,21 +30,20 @@ class GridAx; class NumericTextCtrl; // ---------------------------------------------------------------------------- -// TimeEditor +// NumericEditor // // wxGridCellEditor for the NumericTextCtrl. // ---------------------------------------------------------------------------- #define GRID_VALUE_TIME wxT("Time") -class TimeEditor final : public wxGridCellEditor +class NumericEditor /* not final */ : public wxGridCellEditor { - public: +public: - TimeEditor(); + NumericEditor + (NumericConverter::Type type, const wxString &format, double rate); - TimeEditor(const wxString &format, double rate); - - ~TimeEditor(); + ~NumericEditor(); // Precondition: parent != NULL void Create(wxWindow *parent, wxWindowID id, wxEvtHandler *handler) override; @@ -60,43 +60,46 @@ class TimeEditor final : public wxGridCellEditor void Reset() override; - wxString GetFormat(); - double GetRate(); + wxString GetFormat() const; + double GetRate() const; void SetFormat(const wxString &format); void SetRate(double rate); wxGridCellEditor *Clone() const override; wxString GetValue() const override; - NumericTextCtrl *GetTimeCtrl() const { return (NumericTextCtrl *)m_control; } + NumericTextCtrl *GetNumericTextControl() const + { return static_cast(m_control); } private: wxString mFormat; double mRate; + NumericConverter::Type mType; double mOld; wxString mOldString; wxString mValueAsString; }; // ---------------------------------------------------------------------------- -// TimeRenderer +// NumericRenderer // // wxGridCellRenderer for the NumericTextCtrl. // ---------------------------------------------------------------------------- -class TimeRenderer final : public wxGridCellRenderer +class NumericRenderer final : public wxGridCellRenderer { -public: - ~TimeRenderer() override; + public: + NumericRenderer(NumericConverter::Type type) : mType{ type } {} + ~NumericRenderer() override; void Draw(wxGrid &grid, - wxGridCellAttr &attr, - wxDC &dc, - const wxRect &rect, - int row, - int col, - bool isSelected) override; + wxGridCellAttr &attr, + wxDC &dc, + const wxRect &rect, + int row, + int col, + bool isSelected) override; wxSize GetBestSize(wxGrid &grid, wxGridCellAttr &attr, @@ -105,6 +108,9 @@ public: int col) override; wxGridCellRenderer *Clone() const override; + +private: + NumericConverter::Type mType; }; // ----------------------------------------------------------------------------