//////////////////////////////////////////////////////////////////////
+// Internal Helper function for computing row or column from some
+// (unscrolled) coordinate value, using either
+// m_defaultRowHeight/m_defaultColWidth or binary search on array
+// of m_rowBottoms/m_ColRights to speed up the search!
+
+// Internal helper macros for simpler use of that function
+
+static int CoordToRowOrCol(int coord, int defaultDist, int minDist,
+ const wxArrayInt& BorderArray, bool maxOnOverflow);
+
+#define internalXToCol(x) CoordToRowOrCol(x, m_defaultColWidth, \
+ WXGRID_MIN_COL_WIDTH, \
+ m_colRights, TRUE)
+#define internalYToRow(y) CoordToRowOrCol(y, m_defaultRowHeight, \
+ WXGRID_MIN_ROW_HEIGHT, \
+ m_rowBottoms, TRUE)
+/////////////////////////////////////////////////////////////////////
IMPLEMENT_DYNAMIC_CLASS( wxGrid, wxScrolledWindow )
// find the row labels within these bounds
//
int row;
- for ( row = 0; row < m_numRows; row++ )
+ for ( row = internalYToRow(top); row < m_numRows; row++ )
{
if ( GetRowBottom(row) < top )
continue;
// find the cells within these bounds
//
int col;
- for ( col = 0; col < m_numCols; col++ )
+ for ( col = internalXToCol(left); col < m_numCols; col++ )
{
if ( GetColRight(col) < left )
continue;
// find the cells within these bounds
//
int row, col;
- for ( row = 0; row < m_numRows; row++ )
+ for ( row = internalYToRow(top); row < m_numRows; row++ )
{
if ( GetRowBottom(row) <= top )
continue;
if ( GetRowTop(row) > bottom )
break;
-
- for ( col = 0; col < m_numCols; col++ )
+ for ( col = internalXToCol(left); col < m_numCols; col++ )
{
if ( GetColRight(col) <= left )
continue;
// horizontal grid lines
//
int i;
- for ( i = 0; i < m_numRows; i++ )
+ for ( i = internalYToRow(top); i < m_numRows; i++ )
{
int bot = GetRowBottom(i) - 1;
// vertical grid lines
//
- for ( i = 0; i < m_numCols; i++ )
+ for ( i = internalXToCol(left); i < m_numCols; i++ )
{
int colRight = GetColRight(i) - 1;
if ( colRight > right )
#endif // 0
// cell is shifted by one pixel
- rect.x--;
- rect.y--;
+ // However, don't allow x or y to become negative
+ // since the SetSize() method interprets that as
+ // "don't change."
+ if (rect.x > 0)
+ rect.x--;
+ if (rect.y > 0)
+ rect.y--;
wxGridCellAttr* attr = GetCellAttr(row, col);
wxGridCellEditor* editor = attr->GetEditor(this, row, col);
}
-int wxGrid::YToRow( int y )
+// Internal Helper function for computing row or column from some
+// (unscrolled) coordinate value, using either
+// m_defaultRowHeight/m_defaultColWidth or binary search on array
+// of m_rowBottoms/m_ColRights to speed up the search!
+
+static int CoordToRowOrCol(int coord, int defaultDist, int minDist,
+ const wxArrayInt& BorderArray, bool maxOnOverflow)
{
- int i;
+ if (!defaultDist)
+ defaultDist = 1;
+ size_t i_max = coord / defaultDist,
+ i_min = 0;
+ if (BorderArray.IsEmpty())
+ {
+ return i_max;
+ }
- for ( i = 0; i < m_numRows; i++ )
+ if ( i_max >= BorderArray.GetCount())
+ i_max = BorderArray.GetCount() - 1;
+ else
{
- if ( y < GetRowBottom(i) )
- return i;
+ if ( coord >= BorderArray[i_max])
+ {
+ i_min = i_max;
+ i_max = coord / minDist;
+ }
+ if ( i_max >= BorderArray.GetCount())
+ i_max = BorderArray.GetCount() - 1;
}
+ if ( coord >= BorderArray[i_max])
+ return maxOnOverflow ? (int)i_max : -1;
+ if ( coord < BorderArray[0] )
+ return 0;
- return -1;
+ while ( i_max - i_min > 0 )
+ {
+ wxCHECK_MSG(BorderArray[i_min] <= coord && coord < BorderArray[i_max],
+ 0, _T("wxGrid: internal error in CoordToRowOrCol"));
+ if (coord >= BorderArray[ i_max - 1])
+ return i_max;
+ else
+ i_max--;
+ int median = i_min + (i_max - i_min + 1) / 2;
+ if (coord < BorderArray[median])
+ i_max = median;
+ else
+ i_min = median;
+ }
+ return i_max;
}
-
-int wxGrid::XToCol( int x )
+int wxGrid::YToRow( int y )
{
- int i;
+ return CoordToRowOrCol(y, m_defaultRowHeight,
+ WXGRID_MIN_ROW_HEIGHT, m_rowBottoms, FALSE);
+}
- for ( i = 0; i < m_numCols; i++ )
- {
- if ( x < GetColRight(i) )
- return i;
- }
- return -1;
+int wxGrid::XToCol( int x )
+{
+ return CoordToRowOrCol(x, m_defaultColWidth,
+ WXGRID_MIN_COL_WIDTH, m_colRights, FALSE);
}
//
int wxGrid::YToEdgeOfRow( int y )
{
- int i, d;
+ int i;
+ i = internalYToRow(y);
- for ( i = 0; i < m_numRows; i++ )
+ if ( GetRowHeight(i) > WXGRID_LABEL_EDGE_ZONE )
{
- if ( GetRowHeight(i) > WXGRID_LABEL_EDGE_ZONE )
- {
- d = abs( y - GetRowBottom(i) );
- if ( d < WXGRID_LABEL_EDGE_ZONE )
- return i;
- }
+ // We know that we are in row i, test whether we are
+ // close enough to lower or upper border, respectively.
+ if ( abs(GetRowBottom(i) - y) < WXGRID_LABEL_EDGE_ZONE )
+ return i;
+ else if( i > 0 && y - GetRowTop(i) < WXGRID_LABEL_EDGE_ZONE )
+ return i - 1;
}
return -1;
//
int wxGrid::XToEdgeOfCol( int x )
{
- int i, d;
+ int i;
+ i = internalXToCol(x);
- for ( i = 0; i < m_numCols; i++ )
+ if ( GetColWidth(i) > WXGRID_LABEL_EDGE_ZONE )
{
- if ( GetColWidth(i) > WXGRID_LABEL_EDGE_ZONE )
- {
- d = abs( x - GetColRight(i) );
- if ( d < WXGRID_LABEL_EDGE_ZONE )
- return i;
- }
+ // We know that we are in column i, test whether we are
+ // close enough to right or left border, respectively.
+ if ( abs(GetColRight(i) - x) < WXGRID_LABEL_EDGE_ZONE )
+ return i;
+ else if( i > 0 && x - GetColLeft(i) < WXGRID_LABEL_EDGE_ZONE )
+ return i - 1;
}
return -1;
if ( resizeExistingRows )
{
- InitRowHeights();
+ // since we are resizing all rows to the default row size,
+ // we can simply clear the row heights and row bottoms
+ // arrays (which also allows us to take advantage of
+ // some speed optimisations)
+ m_rowHeights.Empty();
+ m_rowBottoms.Empty();
if ( !GetBatchCount() )
CalcDimensions();
}
if ( resizeExistingCols )
{
- InitColWidths();
+ // since we are resizing all columns to the default column size,
+ // we can simply clear the col widths and col rights
+ // arrays (which also allows us to take advantage of
+ // some speed optimisations)
+ m_colWidths.Empty();
+ m_colRights.Empty();
if ( !GetBatchCount() )
CalcDimensions();
}