+// This class abstracts the difference between operations going forward
+// (down/right) and backward (up/left) and allows to use the same code for
+// functions which differ only in the direction of grid traversal
+//
+// Like wxGridOperations it's an ABC with two concrete subclasses below. Unlike
+// it, this is a normal object and not just a function dispatch table and has a
+// non-default ctor.
+//
+// Note: the explanation of this discrepancy is the existence of (very useful)
+// Dual() method in wxGridOperations which forces us to make wxGridOperations a
+// function dispatcher only.
+class wxGridDirectionOperations
+{
+public:
+ // The oper parameter to ctor selects whether we work with rows or columns
+ wxGridDirectionOperations(wxGrid *grid, const wxGridOperations& oper)
+ : m_grid(grid),
+ m_oper(oper)
+ {
+ }
+
+ // Check if the component of this point in our direction is at the
+ // boundary, i.e. is the first/last row/column
+ virtual bool IsAtBoundary(const wxGridCellCoords& coords) const = 0;
+
+ // Increment the component of this point in our direction
+ virtual void Advance(wxGridCellCoords& coords) const = 0;
+
+ // Find the line at the given distance, in pixels, away from this one
+ // (this uses clipping, i.e. anything after the last line is counted as the
+ // last one and anything before the first one as 0)
+ virtual int MoveByPixelDistance(int line, int distance) const = 0;
+
+ // This class is never used polymorphically but give it a virtual dtor
+ // anyhow to suppress g++ complaints about it
+ virtual ~wxGridDirectionOperations() { }
+
+protected:
+ wxGrid * const m_grid;
+ const wxGridOperations& m_oper;
+};
+
+class wxGridBackwardOperations : public wxGridDirectionOperations
+{
+public:
+ wxGridBackwardOperations(wxGrid *grid, const wxGridOperations& oper)
+ : wxGridDirectionOperations(grid, oper)
+ {
+ }
+
+ virtual bool IsAtBoundary(const wxGridCellCoords& coords) const
+ {
+ wxASSERT_MSG( m_oper.Select(coords) >= 0, "invalid row/column" );
+
+ return m_oper.Select(coords) == 0;
+ }
+
+ virtual void Advance(wxGridCellCoords& coords) const
+ {
+ wxASSERT( !IsAtBoundary(coords) );
+
+ m_oper.Set(coords, m_oper.Select(coords) - 1);
+ }
+
+ virtual int MoveByPixelDistance(int line, int distance) const
+ {
+ int pos = m_oper.GetLineStartPos(m_grid, line);
+ return m_oper.PosToLine(m_grid, pos - distance + 1, true);
+ }
+};
+
+class wxGridForwardOperations : public wxGridDirectionOperations
+{
+public:
+ wxGridForwardOperations(wxGrid *grid, const wxGridOperations& oper)
+ : wxGridDirectionOperations(grid, oper),
+ m_numLines(oper.GetNumberOfLines(grid))
+ {
+ }
+
+ virtual bool IsAtBoundary(const wxGridCellCoords& coords) const
+ {
+ wxASSERT_MSG( m_oper.Select(coords) < m_numLines, "invalid row/column" );
+
+ return m_oper.Select(coords) == m_numLines - 1;
+ }
+
+ virtual void Advance(wxGridCellCoords& coords) const
+ {
+ wxASSERT( !IsAtBoundary(coords) );
+
+ m_oper.Set(coords, m_oper.Select(coords) + 1);
+ }
+
+ virtual int MoveByPixelDistance(int line, int distance) const
+ {
+ int pos = m_oper.GetLineStartPos(m_grid, line);
+ return m_oper.PosToLine(m_grid, pos + distance, true);
+ }
+
+private:
+ const int m_numLines;
+};