// Copyright (c) Sean Walton 1999-2006 All rights reserved // // Standard disclaimer: you use it --> you're responsible // Standard license: you can use it in any way, but keep // my copyright attached. #include "BBGrid.h" /////////////////////////////////////////////////////////////////////////////////////// /// BBGrid BBGrid::BBGrid(ubyte Rows, ubyte Columns) : Field(), Rows(Rows), Columns(Columns) { SelectBy = eCell; Selected = 0; RowHeight = CELLHEIGHT; int remainder = DISPLAYWIDTH%Columns; for ( int c = 0; c < Columns; c++ ) { ColumnWidths[c] = DISPLAYWIDTH/Columns; if ( remainder-- > 0 ) ColumnWidths[c]++; } ColumnsChanged = true; OnScroll(1,1); Redraw(); } void BBGrid::Paint(Graphics& g) { if ( ColumnsChanged ) { Width = 0; int remainder = DISPLAYWIDTH%Columns; for ( int c = 0; c < Columns; c++ ) { ColumnPlaces[c] = Width; Width += ColumnWidths[c]; } ColumnsChanged = false; } for ( int r = 0; r < Rows; r++ ) for ( int c = 0; c < Columns; c++ ) { AreaVirtual area(&g); area.Translate(ColumnPlaces[c], r*RowHeight); NowDisplaying(r, c, area); } } void BBGrid::Redraw(int Row, int Col) { AreaVirtual area(&GetGraphics()); area.Translate(ColumnPlaces[Col], Row*RowHeight); area.Clear(); NowDisplaying(Row, Col, area); } int BBGrid::OnScroll(int axis, int directionMagnitude) { int retval=0; int r, c; do { Selected += directionMagnitude; BBGrid::GetSelected(r, c); } while ( SelectBy == eCell && !AllowSelect(r, c) ); switch ( SelectBy ) { case eNone: if ( Selected >= 0 ) { retval = 1; Selected = 1; } break; case eColumn: if ( Selected >= Columns ) { retval = 1; Selected = Columns; } break; case eRow: if ( Selected >= Rows ) { retval = 1; Selected = Rows; } break; case eCell: if ( Selected >= Rows*Columns ) { retval = 1; Selected = Rows*Columns-1; } break; } /* if ( axis == 1 ) // X axis { if ( SelectBy != eColumn ) { SelectBy = eColumn; Selected = 0; } if ( Selected >= Columns ) { retval = 1; Selected = Columns-1; } } else if ( axis == 2 ) // Y axis { if ( SelectBy != eRow ) { SelectBy = eRow; Selected = 1; } else if ( Selected == 0 ) Selected += directionMagnitude; if ( Selected >= Rows ) { retval = 1; Selected = Rows-1; } }*/ if ( Selected < 0 ) { retval = -1; Selected = 0; } return retval; } XYRect BBGrid::GetFocusRect(void) const { int height = CELLHEIGHT; switch ( SelectBy ) { case eRow: if ( Selected < Rows ) return XYRect(0, RowHeight*Selected, Width, RowHeight); break; case eColumn: if ( Selected < Columns ) return XYRect(ColumnPlaces[Selected], Rows*RowHeight, ColumnWidths[Selected], Rows*RowHeight); break; case eCell: int r,c; BBGrid::GetSelected(r, c); return XYRect(ColumnPlaces[c]+1, r*RowHeight+1, ColumnWidths[c]-1, RowHeight); } return XYRect(0, 0, 0, 0); } void BBGrid::SetColumnWidth(ubyte column, unsigned short width, bool redraw) { if ( column < MAXCOLUMNS ) ColumnWidths[column] = width; ColumnsChanged = true; if ( redraw ) Redraw(); } void BBGrid::GetSelected(int& Row, int& Column) const { switch ( SelectBy ) { case eRow: Row = Selected; Column = -1; case eColumn: Row = -1; Column = Selected; case eCell: Row = Selected/Columns; Column = Selected%Columns; } } void BBGrid::SetRows(int Row) { Rows = Row; Redraw(); } void BBGrid::SetSelected(int Row, int Column) { if ( Row < 0 ) { Selected = Column; SelectBy = eColumn; } else if ( Column < 0 ) { Selected = Row; SelectBy = eRow; } else { Selected = Row*Columns+Column; SelectBy = eCell; } }