Audacity  2.2.2
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/event.h>
19 #include <wx/grid.h>
20 #include <wx/string.h>
21 #include <wx/window.h>
22 #include "NumericTextCtrl.h"
23 #include "../Internat.h"
24 
25 #if wxUSE_ACCESSIBILITY
26 #include <wx/access.h>
27 #include "WindowAccessible.h"
28 
29 class GridAx;
30 
31 #endif
32 
33 class NumericTextCtrl;
35 
36 // ----------------------------------------------------------------------------
37 // NumericEditor
38 //
39 // wxGridCellEditor for the NumericTextCtrl.
40 // ----------------------------------------------------------------------------
41 #define GRID_VALUE_TIME wxT("Time")
42 #define GRID_VALUE_FREQUENCY wxT("Frequency")
43 
44 class NumericEditor /* not final */ : public wxGridCellEditor
45 {
46 public:
47 
49  (NumericConverter::Type type, const NumericFormatId &format, double rate);
50 
52 
53  // Precondition: parent != NULL
54  void Create(wxWindow *parent, wxWindowID id, wxEvtHandler *handler) override;
55 
56  bool IsAcceptedKey(wxKeyEvent &event) override;
57 
58  void SetSize(const wxRect &rect) override;
59 
60  void BeginEdit(int row, int col, wxGrid *grid) override;
61 
62  bool EndEdit(int row, int col, const wxGrid *grid, const wxString &oldval, wxString *newval) override;
63 
64  void ApplyEdit(int row, int col, wxGrid *grid) override;
65 
66  void Reset() override;
67 
68  NumericFormatId GetFormat() const;
69  double GetRate() const;
70  void SetFormat(const NumericFormatId &format);
71  void SetRate(double rate);
72 
73  wxGridCellEditor *Clone() const override;
74  wxString GetValue() const override;
75 
77  { return static_cast<NumericTextCtrl *>(m_control); }
78 
79  private:
80 
82  double mRate;
84  double mOld;
85  wxString mOldString;
86  wxString mValueAsString;
87 };
88 
89 // ----------------------------------------------------------------------------
90 // NumericRenderer
91 //
92 // wxGridCellRenderer for the NumericTextCtrl.
93 // ----------------------------------------------------------------------------
94 
95 class NumericRenderer final : public wxGridCellRenderer
96 {
97  public:
99  ~NumericRenderer() override;
100 
101  void Draw(wxGrid &grid,
102  wxGridCellAttr &attr,
103  wxDC &dc,
104  const wxRect &rect,
105  int row,
106  int col,
107  bool isSelected) override;
108 
109  wxSize GetBestSize(wxGrid &grid,
110  wxGridCellAttr &attr,
111  wxDC &dc,
112  int row,
113  int col) override;
114 
115  wxGridCellRenderer *Clone() const override;
116 
117 private:
119 };
120 
121 // ----------------------------------------------------------------------------
122 // ChoiceEditor
123 //
124 // Modified version of wxGridChoiceEditor using wxChoice instead of wxComboBox.
125 // ----------------------------------------------------------------------------
126 #define GRID_VALUE_CHOICE wxT("Choice")
127 
128 class ChoiceEditor final : public wxGridCellEditor, wxEvtHandler
129 {
130 public:
131 
132  ChoiceEditor(size_t count = 0,
133  const wxString choices[] = NULL);
134 
135  ChoiceEditor(const wxArrayString &choices);
136 
137  ~ChoiceEditor();
138 
139  void Create(wxWindow *parent,
140  wxWindowID id,
141  wxEvtHandler *evtHandler) override;
142 
143  void SetSize(const wxRect &rect) override;
144  void BeginEdit(int row, int col, wxGrid *grid) override;
145  bool EndEdit(int row, int col, wxGrid *grid);
146  bool EndEdit(int row, int col, const wxGrid *grid,
147  const wxString &oldval, wxString *newval) override;
148  void ApplyEdit(int row, int col, wxGrid *grid) override;
149  void Reset() override;
150 
151  wxGridCellEditor *Clone() const override;
152 
153  void SetChoices(const wxArrayString &choices);
154  wxString GetValue() const override;
155 
156  protected:
157 
158  wxChoice *Choice() const { return (wxChoice *)m_control; }
159 
160  private:
161 
162  // A whole separate class just to get rid of Visual C++ warning C4407
163  class FocusHandler:wxEvtHandler
164  {
165  public:
166  void ConnectEvent(wxWindow *w)
167  {
168  // Need to use a named function pointer, not a lambda, so that we
169  // can unbind the same later
170  w->GetEventHandler()->Bind(wxEVT_KILL_FOCUS, OnKillFocus);
171  };
172  void DisconnectEvent(wxWindow *w)
173  {
174  w->GetEventHandler()->Unbind(wxEVT_KILL_FOCUS, OnKillFocus);
175  };
176  static 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<std::unique_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 WindowAccessible
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 Create(wxWindow *parent, wxWindowID id, wxEvtHandler *handler) override
Definition: Grid.cpp:44
bool EndEdit(int row, int col, wxGrid *grid)
Definition: Grid.cpp:318
NumericFormatId GetFormat() const
Definition: Grid.cpp:134
~Grid()
Definition: Grid.cpp:413
void OnSetFocus(wxFocusEvent &event)
Definition: Grid.cpp:425
void ApplyEdit(int row, int col, wxGrid *grid) override
Definition: Grid.cpp:352
void DisconnectEvent(wxWindow *w)
Definition: Grid.h:172
ChoiceEditor::FocusHandler mHandler
~NumericRenderer() override
Definition: Grid.cpp:154
wxString GetValue() const override
Definition: Grid.cpp:367
wxArrayString mChoices
Definition: Grid.h:182
wxChoice * Choice() const
Definition: Grid.h:158
wxString mOldString
Definition: Grid.h:85
NumericEditor(NumericConverter::Type type, const NumericFormatId &format, double rate)
Definition: Grid.cpp:32
void OnKeyDown(wxKeyEvent &event)
Definition: Grid.cpp:443
double mOld
Definition: Grid.h:84
void Reset() override
Definition: Grid.cpp:357
~ChoiceEditor()
Definition: Grid.cpp:270
An alternative to using wxWindowAccessible, which in wxWidgets 3.1.1 contained GetParent() which was ...
bool EndEdit(int row, int col, const wxGrid *grid, const wxString &oldval, wxString *newval) override
Definition: Grid.cpp:89
double mRate
Definition: Grid.h:82
int format
Definition: ExportPCM.cpp:56
void SetFormat(const NumericFormatId &format)
Definition: Grid.cpp:144
void BeginEdit(int row, int col, wxGrid *grid) override
Definition: Grid.cpp:305
void BeginEdit(int row, int col, wxGrid *grid) override
Definition: Grid.cpp:74
void Create(wxWindow *parent, wxWindowID id, wxEvtHandler *evtHandler) override
Definition: Grid.cpp:282
double GetRate() const
Definition: Grid.cpp:139
void ConnectEvent(wxWindow *w)
Definition: Grid.h:166
void SetChoices(const wxArrayString &choices)
Definition: Grid.cpp:362
static void OnKillFocus(wxFocusEvent &WXUNUSED(event))
Definition: Grid.h:176
wxString mValueAsString
Definition: Grid.h:86
IdentInterfaceSymbol pairs a persistent string identifier used internally with an optional...
void SetSize(const wxRect &rect) override
Definition: Grid.cpp:294
NumericConverter::Type mType
Definition: Grid.h:83
NumericConverter::Type mType
Definition: Grid.h:118
NumericRenderer(NumericConverter::Type type)
Definition: Grid.h:98
void ApplyEdit(int row, int col, wxGrid *grid) override
Definition: Grid.cpp:102
void Draw(wxGrid &grid, wxGridCellAttr &attr, wxDC &dc, const wxRect &rect, int row, int col, bool isSelected) override
Definition: Grid.cpp:158
void OnSelectCell(wxGridEvent &event)
Definition: Grid.cpp:434
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:124
bool IsAcceptedKey(wxKeyEvent &event) override
Definition: Grid.cpp:112
NumericFormatId mFormat
Definition: Grid.h:81
wxSize GetBestSize(wxGrid &grid, wxGridCellAttr &attr, wxDC &dc, int row, int col) override
Definition: Grid.cpp:220
~NumericEditor()
Definition: Grid.cpp:40
wxString mValueAsString
Definition: Grid.h:184
NumericTextCtrl * GetNumericTextControl() const
Definition: Grid.h:76
wxGridCellEditor * Clone() const override
Definition: Grid.cpp:277
Supplies an accessible grid based on wxGrid.
Definition: Grid.h:193
void Reset() override
Definition: Grid.cpp:107
void SetRate(double rate)
Definition: Grid.cpp:149
wxString GetValue() const override
Definition: Grid.cpp:129
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
void SetSize(const wxRect &rect) override
Definition: Grid.cpp:63