Audacity  2.2.0
Grid.h
Go to the documentation of this file.
1 /**********************************************************************
2 
3  Audacity: A Digital Audio Editor
4 
5  Grid.h
6 
7  Leland Lucius
8 
9 **********************************************************************/
10 
11 #ifndef __AUDACITY_WIDGETS_GRID__
12 #define __AUDACITY_WIDGETS_GRID__
13 
14 #include "../MemoryX.h"
15 #include <vector>
16 #include <wx/defs.h>
17 #include <wx/choice.h>
18 #include <wx/dynarray.h>
19 #include <wx/event.h>
20 #include <wx/grid.h>
21 #include <wx/string.h>
22 #include <wx/window.h>
23 #include "NumericTextCtrl.h"
24 
25 #if wxUSE_ACCESSIBILITY
26 #include <wx/access.h>
27 
28 class GridAx;
29 
30 #endif
31 
32 class NumericTextCtrl;
33 
34 // ----------------------------------------------------------------------------
35 // NumericEditor
36 //
37 // wxGridCellEditor for the NumericTextCtrl.
38 // ----------------------------------------------------------------------------
39 #define GRID_VALUE_TIME wxT("Time")
40 #define GRID_VALUE_FREQUENCY wxT("Frequency")
41 
42 class NumericEditor /* not final */ : public wxGridCellEditor
43 {
44 public:
45 
47  (NumericConverter::Type type, const wxString &format, double rate);
48 
50 
51  // Precondition: parent != NULL
52  void Create(wxWindow *parent, wxWindowID id, wxEvtHandler *handler) override;
53 
54  bool IsAcceptedKey(wxKeyEvent &event) override;
55 
56  void SetSize(const wxRect &rect) override;
57 
58  void BeginEdit(int row, int col, wxGrid *grid) override;
59 
60  bool EndEdit(int row, int col, const wxGrid *grid, const wxString &oldval, wxString *newval) override;
61 
62  void ApplyEdit(int row, int col, wxGrid *grid) override;
63 
64  void Reset() override;
65 
66  wxString GetFormat() const;
67  double GetRate() const;
68  void SetFormat(const wxString &format);
69  void SetRate(double rate);
70 
71  wxGridCellEditor *Clone() const override;
72  wxString GetValue() const override;
73 
75  { return static_cast<NumericTextCtrl *>(m_control); }
76 
77  private:
78 
79  wxString mFormat;
80  double mRate;
82  double mOld;
83  wxString mOldString;
84  wxString mValueAsString;
85 };
86 
87 // ----------------------------------------------------------------------------
88 // NumericRenderer
89 //
90 // wxGridCellRenderer for the NumericTextCtrl.
91 // ----------------------------------------------------------------------------
92 
93 class NumericRenderer final : public wxGridCellRenderer
94 {
95  public:
97  ~NumericRenderer() override;
98 
99  void Draw(wxGrid &grid,
100  wxGridCellAttr &attr,
101  wxDC &dc,
102  const wxRect &rect,
103  int row,
104  int col,
105  bool isSelected) override;
106 
107  wxSize GetBestSize(wxGrid &grid,
108  wxGridCellAttr &attr,
109  wxDC &dc,
110  int row,
111  int col) override;
112 
113  wxGridCellRenderer *Clone() const override;
114 
115 private:
117 };
118 
119 // ----------------------------------------------------------------------------
120 // ChoiceEditor
121 //
122 // Modified version of wxGridChoiceEditor using wxChoice instead of wxComboBox.
123 // ----------------------------------------------------------------------------
124 #define GRID_VALUE_CHOICE wxT("Choice")
125 
126 class ChoiceEditor final : public wxGridCellEditor, wxEvtHandler
127 {
128 public:
129 
130  ChoiceEditor(size_t count = 0,
131  const wxString choices[] = NULL);
132 
133  ChoiceEditor(const wxArrayString &choices);
134 
135  ~ChoiceEditor();
136 
137  void Create(wxWindow *parent,
138  wxWindowID id,
139  wxEvtHandler *evtHandler) override;
140 
141  void SetSize(const wxRect &rect);
142 
143  void BeginEdit(int row, int col, wxGrid *grid);
144 
145  bool EndEdit(int row, int col, wxGrid *grid);
146 
147  bool EndEdit(int row, int col, const wxGrid *grid, const wxString &oldval, wxString *newval);
148 
149  void ApplyEdit(int row, int col, wxGrid *grid);
150 
151  void Reset();
152 
153  wxGridCellEditor *Clone() const override;
154 
155  void SetChoices(const wxArrayString &choices);
156  wxString GetValue() const;
157 
158  protected:
159 
160  wxChoice *Choice() const { return (wxChoice *)m_control; }
161 
162  private:
163 
164  // A whole separate class just to get rid of Visual C++ warning C4407
165  class FocusHandler:wxEvtHandler
166  {
167  public:
168  void ConnectEvent(wxWindow *w)
169  {
170  w->GetEventHandler()->Connect(wxEVT_KILL_FOCUS, wxFocusEventHandler(FocusHandler::OnKillFocus));
171  };
172  void DisconnectEvent(wxWindow *w)
173  {
174  w->GetEventHandler()->Disconnect(wxEVT_KILL_FOCUS, wxFocusEventHandler(FocusHandler::OnKillFocus));
175  };
176  void OnKillFocus(wxFocusEvent & WXUNUSED(event))
177  {
178  return;
179  };
180  } mHandler;
181 
182  wxArrayString mChoices;
183  wxString mOld;
184  wxString mValueAsString;
185 };
186 
187 // ----------------------------------------------------------------------------
188 // Grid
189 //
190 // wxGrid with support for accessibility.
191 // ----------------------------------------------------------------------------
192 
193 class Grid final : public wxGrid
194 {
195 
196  public:
197 
198  Grid(wxWindow *parent,
199  wxWindowID id,
200  const wxPoint& pos = wxDefaultPosition,
201  const wxSize& size = wxDefaultSize,
202  long style = wxWANTS_CHARS | wxBORDER,
203  const wxString& name = wxPanelNameStr);
204 
205  ~Grid();
206 
207 #if wxUSE_ACCESSIBILITY
208  void ClearGrid();
209  bool InsertRows(int pos = 0, int numRows = 1, bool updateLabels = true);
210  bool AppendRows(int numRows = 1, bool updateLabels = true);
211  bool DeleteRows(int pos = 0, int numRows = 1, bool updateLabels = true);
212  bool InsertCols(int pos = 0, int numCols = 1, bool updateLabels = true);
213  bool AppendCols(int numCols = 1, bool updateLabels = true);
214  bool DeleteCols(int pos = 0, int numCols = 1, bool updateLabels = true);
215 
216  GridAx *GetNextAx(GridAx *parent, wxAccRole role, int row, int col);
217 #endif
218 
219  protected:
220 
221  void OnSetFocus(wxFocusEvent &event);
222  void OnSelectCell(wxGridEvent &event);
223  void OnKeyDown(wxKeyEvent &event);
224 
225  private:
226 
227 #if wxUSE_ACCESSIBILITY
228  GridAx *mAx;
229  std::vector<movable_ptr<GridAx>> mChildren;
230  int mObjNdx;
231 #endif
232 
233  public:
234 
235  DECLARE_EVENT_TABLE()
236 };
237 
238 #if wxUSE_ACCESSIBILITY
239 // ----------------------------------------------------------------------------
240 // GridAx
241 //
242 // wxAccessible object providing grid information for Grid.
243 // ----------------------------------------------------------------------------
244 
245 class GridAx final : public wxWindowAccessible
246 {
247 
248  public:
249 
250  GridAx(Grid *grid);
251 
252  void SetCurrentCell(int row, int col);
253  void TableUpdated();
254  bool GetRowCol(int childId, int & row, int & col);
255 
256  // Retrieves the address of an IDispatch interface for the specified child.
257  // All objects must support this property.
258  wxAccStatus GetChild(int childId, wxAccessible **child) override;
259 
260  // Gets the number of children.
261  wxAccStatus GetChildCount(int *childCount) override;
262 
263  // Gets the default action for this object (0) or > 0 (the action for a child).
264  // Return wxACC_OK even if there is no action. actionName is the action, or the empty
265  // string if there is no action.
266  // The retrieved string describes the action that is performed on an object,
267  // not what the object does as a result. For example, a toolbar button that prints
268  // a document has a default action of "Press" rather than "Prints the current document."
269  wxAccStatus GetDefaultAction(int childId, wxString *actionName) override;
270 
271  // Returns the description for this object or a child.
272  wxAccStatus GetDescription(int childId, wxString *description) override;
273 
274  // Gets the window with the keyboard focus.
275  // If childId is 0 and child is NULL, no object in
276  // this subhierarchy has the focus.
277  // If this object has the focus, child should be 'this'.
278  wxAccStatus GetFocus(int *childId, wxAccessible **child) override;
279 
280  // Returns help text for this object or a child, similar to tooltip text.
281  wxAccStatus GetHelpText(int childId, wxString *helpText) override;
282 
283  // Returns the keyboard shortcut for this object or child.
284  // Return e.g. ALT+K
285  wxAccStatus GetKeyboardShortcut(int childId, wxString *shortcut) override;
286 
287  // Returns the rectangle for this object (id = 0) or a child element (id > 0).
288  // rect is in screen coordinates.
289  wxAccStatus GetLocation(wxRect & rect, int elementId) override;
290 
291  // Gets the name of the specified object.
292  wxAccStatus GetName(int childId, wxString *name) override;
293 
294  // Gets the parent, or NULL.
295  wxAccStatus GetParent(wxAccessible **parent) override;
296 
297  // Returns a role constant.
298  wxAccStatus GetRole(int childId, wxAccRole *role) override;
299 
300  // Gets a variant representing the selected children
301  // of this object.
302  // Acceptable values:
303  // - a null variant (IsNull() returns TRUE)
304  // - a list variant (GetType() == wxT("list"))
305  // - an integer representing the selected child element,
306  // or 0 if this object is selected (GetType() == wxT("long"))
307  // - a "void*" pointer to a wxAccessible child object
308  wxAccStatus GetSelections(wxVariant *selections) override;
309 
310  // Returns a state constant.
311  wxAccStatus GetState(int childId, long* state) override;
312 
313  // Returns a localized string representing the value for the object
314  // or child.
315  wxAccStatus GetValue(int childId, wxString* strValue) override;
316 
317 #if defined(__WXMAC__)
318  // Selects the object or child.
319  wxAccStatus Select(int childId, wxAccSelectionFlags selectFlags) override;
320 #endif
321 
322  Grid *mGrid;
323  int mLastId;
324 
325 };
326 #endif
327 
328 #endif
329 
void ApplyEdit(int row, int col, wxGrid *grid)
Definition: Grid.cpp:352
void Create(wxWindow *parent, wxWindowID id, wxEvtHandler *handler) override
Definition: Grid.cpp:43
bool EndEdit(int row, int col, wxGrid *grid)
Definition: Grid.cpp:318
~Grid()
Definition: Grid.cpp:411
Definition: Grid.h:126
void SetSize(const wxRect &rect)
Definition: Grid.cpp:294
void OnSetFocus(wxFocusEvent &event)
Definition: Grid.cpp:423
void DisconnectEvent(wxWindow *w)
Definition: Grid.h:172
ChoiceEditor::FocusHandler mHandler
Definition: Grid.h:93
~NumericRenderer() override
Definition: Grid.cpp:152
void BeginEdit(int row, int col, wxGrid *grid)
Definition: Grid.cpp:305
wxArrayString mChoices
Definition: Grid.h:182
wxChoice * Choice() const
Definition: Grid.h:160
wxString mOldString
Definition: Grid.h:83
void OnKeyDown(wxKeyEvent &event)
Definition: Grid.cpp:441
double mOld
Definition: Grid.h:82
~ChoiceEditor()
Definition: Grid.cpp:270
NumericEditor(NumericConverter::Type type, const wxString &format, double rate)
Definition: Grid.cpp:31
bool EndEdit(int row, int col, const wxGrid *grid, const wxString &oldval, wxString *newval) override
Definition: Grid.cpp:87
double mRate
Definition: Grid.h:80
int format
Definition: ExportPCM.cpp:56
wxString mFormat
Definition: Grid.h:79
wxString GetFormat() const
Definition: Grid.cpp:132
void BeginEdit(int row, int col, wxGrid *grid) override
Definition: Grid.cpp:72
void Create(wxWindow *parent, wxWindowID id, wxEvtHandler *evtHandler) override
Definition: Grid.cpp:282
double GetRate() const
Definition: Grid.cpp:137
Definition: Grid.h:165
void ConnectEvent(wxWindow *w)
Definition: Grid.h:168
void SetChoices(const wxArrayString &choices)
Definition: Grid.cpp:362
wxString mValueAsString
Definition: Grid.h:84
Definition: Grid.h:42
void SetFormat(const wxString &format)
Definition: Grid.cpp:142
Definition: NumericTextCtrl.h:141
NumericConverter::Type mType
Definition: Grid.h:81
NumericConverter::Type mType
Definition: Grid.h:116
NumericRenderer(NumericConverter::Type type)
Definition: Grid.h:96
void ApplyEdit(int row, int col, wxGrid *grid) override
Definition: Grid.cpp:100
void Draw(wxGrid &grid, wxGridCellAttr &attr, wxDC &dc, const wxRect &rect, int row, int col, bool isSelected) override
Definition: Grid.cpp:156
void Reset()
Definition: Grid.cpp:357
void OnSelectCell(wxGridEvent &event)
Definition: Grid.cpp:432
const wxChar * name
Definition: Distortion.cpp:94
ChoiceEditor(size_t count=0, const wxString choices[]=NULL)
Definition: Grid.cpp:255
wxGridCellRenderer * Clone() const override
Definition: Grid.cpp:250
wxGridCellEditor * Clone() const override
Definition: Grid.cpp:122
bool IsAcceptedKey(wxKeyEvent &event) override
Definition: Grid.cpp:110
wxSize GetBestSize(wxGrid &grid, wxGridCellAttr &attr, wxDC &dc, int row, int col) override
Definition: Grid.cpp:219
~NumericEditor()
Definition: Grid.cpp:39
wxString mValueAsString
Definition: Grid.h:184
NumericTextCtrl * GetNumericTextControl() const
Definition: Grid.h:74
wxGridCellEditor * Clone() const override
Definition: Grid.cpp:277
void OnKillFocus(wxFocusEvent &WXUNUSED(event))
Definition: Grid.h:176
Supplies an accessible grid based on wxGrid.
Definition: Grid.h:193
void Reset() override
Definition: Grid.cpp:105
Type
Definition: NumericTextCtrl.h:57
void SetRate(double rate)
Definition: Grid.cpp:147
wxString GetValue() const override
Definition: Grid.cpp:127
Grid(wxWindow *parent, wxWindowID id, const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxDefaultSize, long style=wxWANTS_CHARS|wxBORDER, const wxString &name=wxPanelNameStr)
Definition: Grid.cpp:382
wxString mOld
Definition: Grid.h:183
wxString GetValue() const
Definition: Grid.cpp:367
void SetSize(const wxRect &rect) override
Definition: Grid.cpp:61