#include "wx/grid.h"
-
// ----------------------------------------------------------------------------
// array classes
// ----------------------------------------------------------------------------
m_colAttrs;
};
+
+// ----------------------------------------------------------------------------
+// data structures used for the data type registry
+// ----------------------------------------------------------------------------
+
+struct wxGridDataTypeInfo {
+ wxGridDataTypeInfo(const wxString& typeName,
+ wxGridCellRenderer* renderer,
+ wxGridCellEditor* editor)
+ : m_typeName(typeName), m_renderer(renderer), m_editor(editor)
+ { }
+
+ ~wxGridDataTypeInfo() { delete m_renderer; delete m_editor; }
+
+ wxString m_typeName;
+ wxGridCellRenderer* m_renderer;
+ wxGridCellEditor* m_editor;
+};
+
+
+WX_DEFINE_ARRAY(wxGridDataTypeInfo*, wxGridDataTypeInfoArray);
+
+
+class WXDLLEXPORT wxGridTypeRegistry {
+public:
+ ~wxGridTypeRegistry();
+ void RegisterDataType(const wxString& typeName,
+ wxGridCellRenderer* renderer,
+ wxGridCellEditor* editor);
+ int FindDataType(const wxString& typeName);
+ wxGridCellRenderer* GetRenderer(int index);
+ wxGridCellEditor* GetEditor(int index);
+
+private:
+ wxGridDataTypeInfoArray m_typeinfo;
+};
+
+
+
+
// ----------------------------------------------------------------------------
// conditional compilation
// ----------------------------------------------------------------------------
static size_t gs_nAttrCacheMisses = 0;
#endif // DEBUG_ATTR_CACHE
+// ----------------------------------------------------------------------------
+// constants
+// ----------------------------------------------------------------------------
+
wxGridCellCoords wxGridNoCellCoords( -1, -1 );
wxRect wxGridNoCellRect( -1, -1, -1, -1 );
// TODO: fixed so far - make configurable later (and also different for x/y)
static const size_t GRID_SCROLL_LINE = 10;
+// the size of hash tables used a bit everywhere (the max number of elements
+// in these hash tables is the number of rows/columns)
+static const int GRID_HASH_SIZE = 100;
+
// ============================================================================
// implementation
// ============================================================================
// set the colours/fonts if we have any
if ( attr )
{
- if ( attr->HasTextColour() )
- {
- m_colFgOld = m_control->GetForegroundColour();
- m_control->SetForegroundColour(attr->GetTextColour());
- }
+ m_colFgOld = m_control->GetForegroundColour();
+ m_control->SetForegroundColour(attr->GetTextColour());
- if ( attr->HasBackgroundColour() )
- {
- m_colBgOld = m_control->GetBackgroundColour();
- m_control->SetBackgroundColour(attr->GetBackgroundColour());
- }
+ m_colBgOld = m_control->GetBackgroundColour();
+ m_control->SetBackgroundColour(attr->GetBackgroundColour());
- if ( attr->HasFont() )
- {
- m_fontOld = m_control->GetFont();
- m_control->SetFont(attr->GetFont());
- }
+ m_fontOld = m_control->GetFont();
+ m_control->SetFont(attr->GetFont());
// can't do anything more in the base class version, the other
// attributes may only be used by the derived classes
wxASSERT_MSG(m_control,
wxT("The wxGridCellEditor must be Created first!"));
- m_startValue = !!grid->GetTable()->GetValue(row, col); // FIXME-DATA
+ if (grid->GetTable()->CanGetValueAs(row, col, wxT("bool")))
+ m_startValue = grid->GetTable()->GetValueAsBool(row, col);
+ else
+ m_startValue = !!grid->GetTable()->GetValue(row, col);
CBox()->SetValue(m_startValue);
CBox()->SetFocus();
}
if ( changed )
{
- // FIXME-DATA
- grid->GetTable()->SetValue(row, col, value ? _T("1") : wxEmptyString);
+ if (grid->GetTable()->CanGetValueAs(row, col, wxT("bool")))
+ grid->GetTable()->SetValueAsBool(row, col, value);
+ else
+ grid->GetTable()->SetValue(row, col, value ? _T("1") : wxEmptyString);
}
return changed;
rectMark.Inflate(-margin);
- if ( !!grid.GetTable()->GetValue(row, col) ) // FIXME-DATA
+ bool value;
+ if (grid.GetTable()->CanGetValueAs(row, col, wxT("bool")))
+ value = grid.GetTable()->GetValueAsBool(row, col);
+ else
+ value = !!grid.GetTable()->GetValue(row, col);
+
+ if ( value )
{
dc.SetTextForeground(attr.GetTextColour());
dc.DrawCheckMark(rectMark);
}
-wxGridCellRenderer* wxGridCellAttr::GetRenderer() const
+// GetRenderer and GetEditor use a slightly different decision path about
+// which to use. If a non-default attr object has one then it is used,
+// otherwise the default editor or renderer passed in is used. It should be
+// the default for the data type of the cell. If it is NULL (because the
+// table has a type that the grid does not have in its registry,) then the
+// grid's default editor or renderer is used.
+
+wxGridCellRenderer* wxGridCellAttr::GetRenderer(wxGridCellRenderer* def) const
{
- if (HasRenderer())
+ if ((m_defGridAttr != this || def == NULL) && HasRenderer())
return m_renderer;
+ else if (def)
+ return def;
else if (m_defGridAttr != this)
- return m_defGridAttr->GetRenderer();
+ return m_defGridAttr->GetRenderer(NULL);
else
{
wxFAIL_MSG(wxT("Missing default cell attribute"));
}
}
-wxGridCellEditor* wxGridCellAttr::GetEditor() const
+wxGridCellEditor* wxGridCellAttr::GetEditor(wxGridCellEditor* def) const
{
- if (HasEditor())
+ if ((m_defGridAttr != this || def == NULL) && HasEditor())
return m_editor;
+ else if (def)
+ return def;
else if (m_defGridAttr != this)
- return m_defGridAttr->GetEditor();
+ return m_defGridAttr->GetEditor(NULL);
else
{
wxFAIL_MSG(wxT("Missing default cell attribute"));
}
}
+// ----------------------------------------------------------------------------
+// wxGridTypeRegistry
+// ----------------------------------------------------------------------------
+
+wxGridTypeRegistry::~wxGridTypeRegistry()
+{
+ for (size_t i=0; i<m_typeinfo.Count(); i++)
+ delete m_typeinfo[i];
+}
+
+
+void wxGridTypeRegistry::RegisterDataType(const wxString& typeName,
+ wxGridCellRenderer* renderer,
+ wxGridCellEditor* editor)
+{
+ int loc;
+ wxGridDataTypeInfo* info = new wxGridDataTypeInfo(typeName, renderer, editor);
+
+ // is it already registered?
+ if ((loc = FindDataType(typeName)) != -1) {
+ delete m_typeinfo[loc];
+ m_typeinfo[loc] = info;
+ }
+ else {
+ m_typeinfo.Add(info);
+ }
+}
+
+int wxGridTypeRegistry::FindDataType(const wxString& typeName)
+{
+ int found = -1;
+
+ for (size_t i=0; i<m_typeinfo.Count(); i++) {
+ if (typeName == m_typeinfo[i]->m_typeName) {
+ found = i;
+ break;
+ }
+ }
+
+ return found;
+}
+
+wxGridCellRenderer* wxGridTypeRegistry::GetRenderer(int index)
+{
+ wxGridCellRenderer* renderer = m_typeinfo[index]->m_renderer;
+ return renderer;
+}
+
+wxGridCellEditor* wxGridTypeRegistry::GetEditor(int index)
+{
+ wxGridCellEditor* editor = m_typeinfo[index]->m_editor;
+ return editor;
+}
+
// ----------------------------------------------------------------------------
// wxGridTableBase
// ----------------------------------------------------------------------------
-//////////////////////////////////////////////////////////////////////
-//
-// Abstract base class for grid data (the model)
-//
IMPLEMENT_ABSTRACT_CLASS( wxGridTableBase, wxObject )
m_attrProvider = attrProvider;
}
+bool wxGridTableBase::CanHaveAttributes()
+{
+ if ( ! GetAttrProvider() )
+ {
+ // use the default attr provider by default
+ SetAttrProvider(new wxGridCellAttrProvider);
+ }
+ return TRUE;
+}
+
wxGridCellAttr *wxGridTableBase::GetAttr(int row, int col)
{
if ( m_attrProvider )
wxString wxGridTableBase::GetRowLabelValue( int row )
{
wxString s;
- s << row;
+ s << row + 1; // RD: Starting the rows at zero confuses users, no matter
+ // how much it makes sense to us geeks.
return s;
}
}
+wxString wxGridTableBase::GetTypeName( int WXUNUSED(row), int WXUNUSED(col) )
+{
+ return wxT("string");
+}
+
+bool wxGridTableBase::CanGetValueAs( int WXUNUSED(row), int WXUNUSED(col),
+ const wxString& typeName )
+{
+ return typeName == wxT("string");
+}
+
+bool wxGridTableBase::CanSetValueAs( int row, int col, const wxString& typeName )
+{
+ return CanGetValueAs(row, col, typeName);
+}
+
+long wxGridTableBase::GetValueAsLong( int WXUNUSED(row), int WXUNUSED(col) )
+{
+ return 0;
+}
+
+double wxGridTableBase::GetValueAsDouble( int WXUNUSED(row), int WXUNUSED(col) )
+{
+ return 0.0;
+}
+
+bool wxGridTableBase::GetValueAsBool( int WXUNUSED(row), int WXUNUSED(col) )
+{
+ return FALSE;
+}
+
+void wxGridTableBase::SetValueAsLong( int WXUNUSED(row), int WXUNUSED(col),
+ long WXUNUSED(value) )
+{
+}
+
+void wxGridTableBase::SetValueAsDouble( int WXUNUSED(row), int WXUNUSED(col),
+ double WXUNUSED(value) )
+{
+}
+
+void wxGridTableBase::SetValueAsBool( int WXUNUSED(row), int WXUNUSED(col),
+ bool WXUNUSED(value) )
+{
+}
+
+
+void* wxGridTableBase::GetValueAsCustom( int WXUNUSED(row), int WXUNUSED(col),
+ const wxString& WXUNUSED(typeName) )
+{
+ return NULL;
+}
+
+void wxGridTableBase::SetValueAsCustom( int WXUNUSED(row), int WXUNUSED(col),
+ const wxString& WXUNUSED(typeName),
+ void* WXUNUSED(value) )
+{
+}
+
//////////////////////////////////////////////////////////////////////
//
return m_data[row][col];
}
-void wxGridStringTable::SetValue( int row, int col, const wxString& s )
+void wxGridStringTable::SetValue( int row, int col, const wxString& value )
{
// TODO: bounds checking
//
- m_data[row][col] = s;
+ m_data[row][col] = value;
}
bool wxGridStringTable::IsEmptyCell( int row, int col )
{
int cw, ch;
GetClientSize( &cw, &ch );
-
+
int right, bottom;
m_owner->CalcUnscrolledPosition( cw, ch, &right, &bottom );
-
+
wxRect rightRect;
rightRect = m_owner->CellToRect( 0, m_owner->GetNumberCols()-1 );
wxRect bottomRect;
bottomRect = m_owner->CellToRect( m_owner->GetNumberRows()-1, 0 );
-
+
if ( right > rightRect.GetRight() || bottom > bottomRect.GetBottom() )
{
int left, top;
m_owner->CalcUnscrolledPosition( 0, 0, &left, &top );
-
+
wxClientDC dc( this );
m_owner->PrepareDC( dc );
dc.SetBrush( wxBrush(m_owner->GetDefaultCellBackgroundColour(), wxSOLID) );
dc.SetPen( *wxTRANSPARENT_PEN );
-
+
if ( right > rightRect.GetRight() )
dc.DrawRectangle( rightRect.GetRight()+1, top, right - rightRect.GetRight(), ch );
-
+
if ( bottom > bottomRect.GetBottom() )
dc.DrawRectangle( left, bottomRect.GetBottom()+1, cw, bottom - bottomRect.GetBottom() );
}
const wxSize& size,
long style,
const wxString& name )
- : wxScrolledWindow( parent, id, pos, size, style, name )
+ : wxScrolledWindow( parent, id, pos, size, style, name ),
+ m_colMinWidths(wxKEY_INTEGER, GRID_HASH_SIZE)
{
Create();
}
if (m_ownTable)
delete m_table;
+
+ delete m_typeRegistry;
}
m_defaultCellAttr = new wxGridCellAttr;
m_defaultCellAttr->SetDefAttr(m_defaultCellAttr);
- // RD: Should we fill the default attrs now or is waiting until Init() okay?
+
+ // Set default cell attributes
+ m_defaultCellAttr->SetFont(GetFont());
+ m_defaultCellAttr->SetAlignment(wxLEFT, wxTOP);
+ m_defaultCellAttr->SetTextColour(
+ wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOWTEXT));
+ m_defaultCellAttr->SetBackgroundColour(
+ wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW));
+ m_defaultCellAttr->SetRenderer(new wxGridCellStringRenderer);
+ m_defaultCellAttr->SetEditor(new wxGridCellTextEditor);
m_numRows = 0;
m_rowLabelWidth = WXGRID_DEFAULT_ROW_LABEL_WIDTH;
m_colLabelHeight = WXGRID_DEFAULT_COL_LABEL_HEIGHT;
+ // data type registration
+ m_typeRegistry = new wxGridTypeRegistry;
+ RegisterDataType(wxT("string"), new wxGridCellStringRenderer,
+ new wxGridCellTextEditor);
+ RegisterDataType(wxT("bool"), new wxGridCellBoolRenderer,
+ new wxGridCellBoolEditor);
+
+
+ // subwindow components that make up the wxGrid
m_cornerLabelWin = new wxGridCornerLabelWindow( this,
-1,
wxDefaultPosition,
void wxGrid::Init()
{
- int i;
-
if ( m_numRows <= 0 )
m_numRows = WXGRID_DEFAULT_NUMBER_ROWS;
m_defaultRowHeight += 4;
#endif
- m_rowHeights.Alloc( m_numRows );
- m_rowBottoms.Alloc( m_numRows );
- int rowBottom = 0;
- for ( i = 0; i < m_numRows; i++ )
- {
- m_rowHeights.Add( m_defaultRowHeight );
- rowBottom += m_defaultRowHeight;
- m_rowBottoms.Add( rowBottom );
- }
-
- m_colWidths.Alloc( m_numCols );
- m_colRights.Alloc( m_numCols );
- int colRight = 0;
- for ( i = 0; i < m_numCols; i++ )
- {
- m_colWidths.Add( m_defaultColWidth );
- colRight += m_defaultColWidth;
- m_colRights.Add( colRight );
- }
-
- // Set default cell attributes
- m_defaultCellAttr->SetFont(GetFont());
- m_defaultCellAttr->SetAlignment(wxLEFT, wxTOP);
- m_defaultCellAttr->SetRenderer(new wxGridCellStringRenderer);
- m_defaultCellAttr->SetEditor(new wxGridCellTextEditor);
- m_defaultCellAttr->SetTextColour(
- wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOWTEXT));
- m_defaultCellAttr->SetBackgroundColour(
- wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW));
-
-
m_gridLineColour = wxColour( 128, 128, 255 );
m_gridLinesEnabled = TRUE;
m_cursorMode = WXGRID_CURSOR_SELECT_CELL;
m_winCapture = (wxWindow *)NULL;
+ m_canDragRowSize = TRUE;
+ m_canDragColSize = TRUE;
m_dragLastPos = -1;
m_dragRowOrCol = -1;
m_isDragging = FALSE;
m_inOnKeyDown = FALSE;
m_batchCount = 0;
+}
+
+// ----------------------------------------------------------------------------
+// the idea is to call these functions only when necessary because they create
+// quite big arrays which eat memory mostly unnecessary - in particular, if
+// default widths/heights are used for all rows/columns, we may not use these
+// arrays at all
+//
+// with some extra code, it should be possible to only store the
+// widths/heights different from default ones but this will be done later...
+// ----------------------------------------------------------------------------
+
+void wxGrid::InitRowHeights()
+{
+ m_rowHeights.Empty();
+ m_rowBottoms.Empty();
+ m_rowHeights.Alloc( m_numRows );
+ m_rowBottoms.Alloc( m_numRows );
+
+ int rowBottom = 0;
+ for ( int i = 0; i < m_numRows; i++ )
+ {
+ m_rowHeights.Add( m_defaultRowHeight );
+ rowBottom += m_defaultRowHeight;
+ m_rowBottoms.Add( rowBottom );
+ }
}
+void wxGrid::InitColWidths()
+{
+ m_colWidths.Empty();
+ m_colRights.Empty();
+
+ m_colWidths.Alloc( m_numCols );
+ m_colRights.Alloc( m_numCols );
+ int colRight = 0;
+ for ( int i = 0; i < m_numCols; i++ )
+ {
+ m_colWidths.Add( m_defaultColWidth );
+ colRight += m_defaultColWidth;
+ m_colRights.Add( colRight );
+ }
+}
+
+int wxGrid::GetColWidth(int col) const
+{
+ return m_colWidths.IsEmpty() ? m_defaultColWidth : m_colWidths[col];
+}
+
+int wxGrid::GetColLeft(int col) const
+{
+ return m_colRights.IsEmpty() ? col * m_defaultColWidth
+ : m_colRights[col] - m_colWidths[col];
+}
+
+int wxGrid::GetColRight(int col) const
+{
+ return m_colRights.IsEmpty() ? (col + 1) * m_defaultColWidth
+ : m_colRights[col];
+}
+
+int wxGrid::GetRowHeight(int row) const
+{
+ return m_rowHeights.IsEmpty() ? m_defaultRowHeight : m_rowHeights[row];
+}
+
+int wxGrid::GetRowTop(int row) const
+{
+ return m_rowBottoms.IsEmpty() ? row * m_defaultRowHeight
+ : m_rowBottoms[row] - m_rowHeights[row];
+}
+
+int wxGrid::GetRowBottom(int row) const
+{
+ return m_rowBottoms.IsEmpty() ? (row + 1) * m_defaultRowHeight
+ : m_rowBottoms[row];
+}
void wxGrid::CalcDimensions()
{
if ( m_numRows > 0 && m_numCols > 0 )
{
- int right = m_colRights[ m_numCols-1 ] + 50;
- int bottom = m_rowBottoms[ m_numRows-1 ] + 50;
+ int right = GetColRight( m_numCols-1 ) + 50;
+ int bottom = GetRowBottom( m_numRows-1 ) + 50;
// TODO: restore the scroll position that we had before sizing
//
{
int i;
+ // if we were using the default widths/heights so far, we must change them
+ // now
+ if ( m_colWidths.IsEmpty() )
+ {
+ InitColWidths();
+ }
+
+ if ( m_rowHeights.IsEmpty() )
+ {
+ InitRowHeights();
+ }
+
switch ( msg.GetId() )
{
case wxGRIDTABLE_NOTIFY_ROWS_INSERTED:
// find the row labels within these bounds
//
int row;
- int rowTop;
for ( row = 0; row < m_numRows; row++ )
{
- if ( m_rowBottoms[row] < top ) continue;
+ if ( GetRowBottom(row) < top )
+ continue;
- rowTop = m_rowBottoms[row] - m_rowHeights[row];
- if ( rowTop > bottom ) break;
+ if ( GetRowTop(row) > bottom )
+ break;
m_rowLabelsExposed.Add( row );
}
// find the cells within these bounds
//
int col;
- int colLeft;
for ( col = 0; col < m_numCols; col++ )
{
- if ( m_colRights[col] < left ) continue;
+ if ( GetColRight(col) < left )
+ continue;
- colLeft = m_colRights[col] - m_colWidths[col];
- if ( colLeft > right ) break;
+ if ( GetColLeft(col) > right )
+ break;
m_colLabelsExposed.Add( col );
}
// find the cells within these bounds
//
int row, col;
- int colLeft, rowTop;
for ( row = 0; row < m_numRows; row++ )
{
- if ( m_rowBottoms[row] <= top ) continue;
+ if ( GetRowBottom(row) <= top )
+ continue;
- rowTop = m_rowBottoms[row] - m_rowHeights[row];
- if ( rowTop > bottom ) break;
+ if ( GetRowTop(row) > bottom )
+ break;
m_rowsExposed.Add( row );
for ( col = 0; col < m_numCols; col++ )
{
- if ( m_colRights[col] <= left ) continue;
+ if ( GetColRight(col) <= left )
+ continue;
- colLeft = m_colRights[col] - m_colWidths[col];
- if ( colLeft > right ) break;
+ if ( GetColLeft(col) > right )
+ break;
- if ( m_colsExposed.Index( col ) == wxNOT_FOUND ) m_colsExposed.Add( col );
+ if ( m_colsExposed.Index( col ) == wxNOT_FOUND )
+ m_colsExposed.Add( col );
m_cellsExposed.Add( wxGridCellCoords( row, col ) );
}
}
- iter++ ;
+ iter++;
}
}
wxClientDC dc( m_gridWin );
PrepareDC( dc );
- y = wxMax( y,
- m_rowBottoms[m_dragRowOrCol] -
- m_rowHeights[m_dragRowOrCol] +
- WXGRID_MIN_ROW_HEIGHT );
+ y = wxMax( y, GetRowTop(m_dragRowOrCol) + WXGRID_MIN_ROW_HEIGHT );
dc.SetLogicalFunction(wxINVERT);
if ( m_dragLastPos >= 0 )
{
{
// starting to drag-resize a row
//
- ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW, m_rowLabelWin);
+ if ( CanDragRowSize() )
+ ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW, m_rowLabelWin);
}
}
if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
{
// don't capture the mouse yet
- ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW, m_rowLabelWin, FALSE);
+ if ( CanDragRowSize() )
+ ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW, m_rowLabelWin, FALSE);
}
}
else if ( m_cursorMode != WXGRID_CURSOR_SELECT_CELL )
wxClientDC dc( m_gridWin );
PrepareDC( dc );
- x = wxMax( x,
- m_colRights[m_dragRowOrCol] -
- m_colWidths[m_dragRowOrCol] +
- WXGRID_MIN_COL_WIDTH );
+
+ x = wxMax( x, GetColLeft(m_dragRowOrCol) +
+ GetColMinimalWidth(m_dragRowOrCol));
dc.SetLogicalFunction(wxINVERT);
if ( m_dragLastPos >= 0 )
{
{
// starting to drag-resize a col
//
- ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL, m_colLabelWin);
+ if ( CanDragColSize() )
+ ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL, m_colLabelWin);
}
}
if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
{
// don't capture the cursor yet
- ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL, m_colLabelWin, FALSE);
+ if ( CanDragColSize() )
+ ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL, m_colLabelWin, FALSE);
}
}
else if ( m_cursorMode != WXGRID_CURSOR_SELECT_CELL )
wxClientDC dc( m_gridWin );
PrepareDC( dc );
- y = wxMax( y,
- m_rowBottoms[m_dragRowOrCol] -
- m_rowHeights[m_dragRowOrCol] +
- WXGRID_MIN_ROW_HEIGHT );
+ y = wxMax( y, GetRowTop(m_dragRowOrCol) + WXGRID_MIN_ROW_HEIGHT );
dc.SetLogicalFunction(wxINVERT);
if ( m_dragLastPos >= 0 )
{
wxClientDC dc( m_gridWin );
PrepareDC( dc );
- x = wxMax( x,
- m_colRights[m_dragRowOrCol] -
- m_colWidths[m_dragRowOrCol] + WXGRID_MIN_COL_WIDTH );
+ x = wxMax( x, GetColLeft(m_dragRowOrCol) +
+ GetColMinimalWidth(m_dragRowOrCol) );
dc.SetLogicalFunction(wxINVERT);
if ( m_dragLastPos >= 0 )
{
EnableCellEditControl();
wxGridCellAttr* attr = GetCellAttr(m_currentCellCoords);
- attr->GetEditor()->StartingClick();
+ attr->GetEditor(GetDefaultEditorForCell(coords.GetRow(), coords.GetCol()))->StartingClick();
attr->DecRef();
m_waitForSlowClick = FALSE;
if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
{
- ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW);
+ if ( CanDragRowSize() )
+ ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW);
}
return;
if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
{
- ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL);
+ if ( CanDragColSize() )
+ ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL);
}
return;
dc.DrawLine( left, m_dragLastPos, left+cw, m_dragLastPos );
HideCellEditControl();
- int rowTop = m_rowBottoms[m_dragRowOrCol] - m_rowHeights[m_dragRowOrCol];
+ int rowTop = GetRowTop(m_dragRowOrCol);
SetRowSize( m_dragRowOrCol,
wxMax( m_dragLastPos - rowTop, WXGRID_MIN_ROW_HEIGHT ) );
dc.DrawLine( m_dragLastPos, top, m_dragLastPos, top+ch );
HideCellEditControl();
- int colLeft = m_colRights[m_dragRowOrCol] - m_colWidths[m_dragRowOrCol];
+ int colLeft = GetColLeft(m_dragRowOrCol);
SetColSize( m_dragRowOrCol,
- wxMax( m_dragLastPos - colLeft, WXGRID_MIN_COL_WIDTH ) );
+ wxMax( m_dragLastPos - colLeft,
+ GetColMinimalWidth(m_dragRowOrCol) ) );
if ( !GetBatchCount() )
{
if ( !IsCellEditControlEnabled() && CanEnableCellControl() )
{
EnableCellEditControl();
- wxGridCellAttr* attr = GetCellAttr(m_currentCellCoords);
- attr->GetEditor()->StartingKey(event);
+ int row = m_currentCellCoords.GetRow();
+ int col = m_currentCellCoords.GetCol();
+ wxGridCellAttr* attr = GetCellAttr(row, col);
+ attr->GetEditor(GetDefaultEditorForCell(row, col))->StartingKey(event);
attr->DecRef();
}
else
int row = coords.GetRow();
int col = coords.GetCol();
- if ( m_colWidths[col] <= 0 || m_rowHeights[row] <= 0 )
+ if ( GetColWidth(col) <= 0 || GetRowHeight(row) <= 0 )
return;
// we draw the cell border ourselves
bool isCurrent = coords == m_currentCellCoords;
wxRect rect;
- rect.x = m_colRights[col] - m_colWidths[col];
- rect.y = m_rowBottoms[row] - m_rowHeights[row];
- rect.width = m_colWidths[col] - 1;
- rect.height = m_rowHeights[row] - 1;
+ rect.x = GetColLeft(col);
+ rect.y = GetRowTop(row);
+ rect.width = GetColWidth(col) - 1;
+ rect.height = GetRowHeight(row) - 1;
// if the editor is shown, we should use it and not the renderer
if ( isCurrent && IsCellEditControlEnabled() )
{
- attr->GetEditor()->PaintBackground(rect, attr);
+ attr->GetEditor(GetDefaultEditorForCell(row, col))->
+ PaintBackground(rect, attr);
}
else
{
// but all the rest is drawn by the cell renderer and hence may be
// customized
- attr->GetRenderer()->Draw(*this, *attr, dc, rect, row, col, IsInSelection(coords));
+ attr->GetRenderer(GetDefaultRendererForCell(row,col))->
+ Draw(*this, *attr, dc, rect, row, col, IsInSelection(coords));
+
}
attr->DecRef();
int row = m_currentCellCoords.GetRow();
int col = m_currentCellCoords.GetCol();
- if ( m_colWidths[col] <= 0 || m_rowHeights[row] <= 0 )
+ if ( GetColWidth(col) <= 0 || GetRowHeight(row) <= 0 )
return;
wxRect rect;
- rect.x = m_colRights[col] - m_colWidths[col];
- rect.y = m_rowBottoms[row] - m_rowHeights[row];
- rect.width = m_colWidths[col] - 1;
- rect.height = m_rowHeights[row] - 1;
+ rect.x = GetColLeft(col);
+ rect.y = GetRowTop(row);
+ rect.width = GetColWidth(col) - 1;
+ rect.height = GetRowHeight(row) - 1;
// hmmm... what could we do here to show that the cell is disabled?
// for now, I just draw a thinner border than for the other ones, but
#endif // 0
}
+
void wxGrid::DrawCellBorder( wxDC& dc, const wxGridCellCoords& coords )
{
- if ( m_colWidths[coords.GetCol()] <=0 ||
- m_rowHeights[coords.GetRow()] <= 0 ) return;
-
- dc.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID) );
int row = coords.GetRow();
int col = coords.GetCol();
+ if ( GetColWidth(col) <= 0 || GetRowHeight(row) <= 0 )
+ return;
+
+ dc.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID) );
// right hand border
//
- dc.DrawLine( m_colRights[col], m_rowBottoms[row] - m_rowHeights[row],
- m_colRights[col], m_rowBottoms[row] );
+ dc.DrawLine( GetColRight(col), GetRowTop(row),
+ GetColRight(col), GetRowBottom(row) );
// bottom border
//
- dc.DrawLine( m_colRights[col] - m_colWidths[col], m_rowBottoms[row],
- m_colRights[col], m_rowBottoms[row] );
+ dc.DrawLine( GetColLeft(col), GetRowBottom(row),
+ GetColRight(col), GetRowBottom(row) );
}
void wxGrid::DrawHighlight(wxDC& dc)
// avoid drawing grid lines past the last row and col
//
- right = wxMin( right, m_colRights[m_numCols-1] );
- bottom = wxMin( bottom, m_rowBottoms[m_numRows-1] );
+ right = wxMin( right, GetColRight(m_numCols - 1) );
+ bottom = wxMin( bottom, GetRowBottom(m_numRows - 1) );
dc.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID) );
int i;
for ( i = 0; i < m_numRows; i++ )
{
- if ( m_rowBottoms[i]-1 > bottom )
+ int bot = GetRowBottom(i) - 1;
+
+ if ( bot > bottom )
{
break;
}
- else if ( m_rowBottoms[i]-1 >= top )
+
+ if ( bot >= top )
{
- dc.DrawLine( left, m_rowBottoms[i]-1, right, m_rowBottoms[i]-1 );
+ dc.DrawLine( left, bot, right, bot );
}
}
//
for ( i = 0; i < m_numCols; i++ )
{
- if ( m_colRights[i]-1 > right )
+ int colRight = GetColRight(i) - 1;
+ if ( colRight > right )
{
break;
}
- else if ( m_colRights[i]-1 >= left )
+
+ if ( colRight >= left )
{
- dc.DrawLine( m_colRights[i]-1, top, m_colRights[i]-1, bottom );
+ dc.DrawLine( colRight, top, colRight, bottom );
}
}
}
void wxGrid::DrawRowLabel( wxDC& dc, int row )
{
- if ( m_rowHeights[row] <= 0 ) return;
+ if ( GetRowHeight(row) <= 0 )
+ return;
- int rowTop = m_rowBottoms[row] - m_rowHeights[row];
+ int rowTop = GetRowTop(row),
+ rowBottom = GetRowBottom(row) - 1;
dc.SetPen( *wxBLACK_PEN );
dc.DrawLine( m_rowLabelWidth-1, rowTop,
- m_rowLabelWidth-1, m_rowBottoms[row]-1 );
+ m_rowLabelWidth-1, rowBottom );
- dc.DrawLine( 0, m_rowBottoms[row]-1,
- m_rowLabelWidth-1, m_rowBottoms[row]-1 );
+ dc.DrawLine( 0, rowBottom, m_rowLabelWidth-1, rowBottom );
dc.SetPen( *wxWHITE_PEN );
- dc.DrawLine( 0, rowTop, 0, m_rowBottoms[row]-1 );
+ dc.DrawLine( 0, rowTop, 0, rowBottom );
dc.DrawLine( 0, rowTop, m_rowLabelWidth-1, rowTop );
dc.SetBackgroundMode( wxTRANSPARENT );
wxRect rect;
rect.SetX( 2 );
- rect.SetY( m_rowBottoms[row] - m_rowHeights[row] + 2 );
+ rect.SetY( GetRowTop(row) + 2 );
rect.SetWidth( m_rowLabelWidth - 4 );
- rect.SetHeight( m_rowHeights[row] - 4 );
+ rect.SetHeight( GetRowHeight(row) - 4 );
DrawTextRectangle( dc, GetRowLabelValue( row ), rect, hAlign, vAlign );
}
void wxGrid::DrawColLabel( wxDC& dc, int col )
{
- if ( m_colWidths[col] <= 0 ) return;
+ if ( GetColWidth(col) <= 0 )
+ return;
- int colLeft = m_colRights[col] - m_colWidths[col];
+ int colLeft = GetColLeft(col),
+ colRight = GetColRight(col) - 1;
dc.SetPen( *wxBLACK_PEN );
- dc.DrawLine( m_colRights[col]-1, 0,
- m_colRights[col]-1, m_colLabelHeight-1 );
+ dc.DrawLine( colRight, 0,
+ colRight, m_colLabelHeight-1 );
dc.DrawLine( colLeft, m_colLabelHeight-1,
- m_colRights[col]-1, m_colLabelHeight-1 );
+ colRight, m_colLabelHeight-1 );
dc.SetPen( *wxWHITE_PEN );
dc.DrawLine( colLeft, 0, colLeft, m_colLabelHeight-1 );
- dc.DrawLine( colLeft, 0, m_colRights[col]-1, 0 );
+ dc.DrawLine( colLeft, 0, colRight, 0 );
dc.SetBackgroundMode( wxTRANSPARENT );
dc.SetTextForeground( GetLabelTextColour() );
GetColLabelAlignment( &hAlign, &vAlign );
wxRect rect;
- rect.SetX( m_colRights[col] - m_colWidths[col] + 2 );
+ rect.SetX( colLeft + 2 );
rect.SetY( 2 );
- rect.SetWidth( m_colWidths[col] - 4 );
+ rect.SetWidth( GetColWidth(col) - 4 );
rect.SetHeight( m_colLabelHeight - 4 );
DrawTextRectangle( dc, GetColLabelValue( col ), rect, hAlign, vAlign );
}
#endif
wxGridCellAttr* attr = GetCellAttr(row, col);
- wxGridCellEditor* editor = attr->GetEditor();
+ wxGridCellEditor* editor = attr->GetEditor(GetDefaultEditorForCell(row, col));
if ( !editor->IsCreated() )
{
editor->Create(m_gridWin, -1,
int col = m_currentCellCoords.GetCol();
wxGridCellAttr* attr = GetCellAttr(row, col);
- attr->GetEditor()->Show( FALSE );
+ attr->GetEditor(GetDefaultEditorForCell(row, col))->Show( FALSE );
attr->DecRef();
m_gridWin->SetFocus();
}
int col = m_currentCellCoords.GetCol();
wxGridCellAttr* attr = GetCellAttr(row, col);
- bool changed = attr->GetEditor()->EndEdit(row, col, TRUE, this);
+ wxGridCellEditor* editor = attr->GetEditor(GetDefaultEditorForCell(row, col));
+ bool changed = editor->EndEdit(row, col, TRUE, this);
attr->DecRef();
for ( i = 0; i < m_numRows; i++ )
{
- if ( y < m_rowBottoms[i] ) return i;
+ if ( y < GetRowBottom(i) )
+ return i;
}
return m_numRows; //-1;
for ( i = 0; i < m_numCols; i++ )
{
- if ( x < m_colRights[i] ) return i;
+ if ( x < GetColRight(i) )
+ return i;
}
return m_numCols; //-1;
for ( i = 0; i < m_numRows; i++ )
{
- if ( m_rowHeights[i] > WXGRID_LABEL_EDGE_ZONE )
+ if ( GetRowHeight(i) > WXGRID_LABEL_EDGE_ZONE )
{
- d = abs( y - m_rowBottoms[i] );
- {
- if ( d < WXGRID_LABEL_EDGE_ZONE ) return i;
- }
+ d = abs( y - GetRowBottom(i) );
+ if ( d < WXGRID_LABEL_EDGE_ZONE )
+ return i;
}
}
for ( i = 0; i < m_numCols; i++ )
{
- if ( m_colWidths[i] > WXGRID_LABEL_EDGE_ZONE )
+ if ( GetColWidth(i) > WXGRID_LABEL_EDGE_ZONE )
{
- d = abs( x - m_colRights[i] );
- {
- if ( d < WXGRID_LABEL_EDGE_ZONE ) return i;
- }
+ d = abs( x - GetColRight(i) );
+ if ( d < WXGRID_LABEL_EDGE_ZONE )
+ return i;
}
}
if ( row >= 0 && row < m_numRows &&
col >= 0 && col < m_numCols )
{
- rect.x = m_colRights[col] - m_colWidths[col];
- rect.y = m_rowBottoms[row] - m_rowHeights[row];
- rect.width = m_colWidths[col];
- rect.height = m_rowHeights[ row ];
+ rect.x = GetColLeft(col);
+ rect.y = GetRowTop(row);
+ rect.width = GetColWidth(col);
+ rect.height = GetRowHeight(row);
}
return rect;
ypos = r.GetTop();
for ( i = row-1; i >= 0; i-- )
{
- if ( h + m_rowHeights[i] > ch ) break;
+ int rowHeight = GetRowHeight(i);
+ if ( h + rowHeight > ch )
+ break;
- h += m_rowHeights[i];
- ypos -= m_rowHeights[i];
+ h += rowHeight;
+ ypos -= rowHeight;
}
// we divide it later by GRID_SCROLL_LINE, make sure that we don't
xpos = r.GetLeft();
for ( i = col-1; i >= 0; i-- )
{
- if ( w + m_colWidths[i] > cw ) break;
+ int colWidth = GetColWidth(i);
+ if ( w + colWidth > cw )
+ break;
- w += m_colWidths[i];
- xpos -= m_colWidths[i];
+ w += colWidth;
+ xpos -= colWidth;
}
// see comment for ypos above
int cw, ch;
m_gridWin->GetClientSize( &cw, &ch );
- int y = m_rowBottoms[ row ] - m_rowHeights[ row ];
+ int y = GetRowTop(row);
int newRow = YToRow( y - ch + 1 );
if ( newRow == -1 )
{
int cw, ch;
m_gridWin->GetClientSize( &cw, &ch );
- int y = m_rowBottoms[ row ] - m_rowHeights[ row ];
+ int y = GetRowTop(row);
int newRow = YToRow( y + ch );
if ( newRow == -1 )
{
{
wxCHECK_MSG( row >= 0 && row < m_numRows, 0, _T("invalid row index") );
- return m_rowHeights[row];
+ return GetRowHeight(row);
}
int wxGrid::GetDefaultColSize()
{
wxCHECK_MSG( col >= 0 && col < m_numCols, 0, _T("invalid column index") );
- return m_colWidths[col];
+ return GetColWidth(col);
}
// ============================================================================
wxGridCellRenderer *wxGrid::GetDefaultRenderer() const
{
- return m_defaultCellAttr->GetRenderer();
+ return m_defaultCellAttr->GetRenderer(NULL);
}
wxGridCellEditor *wxGrid::GetDefaultEditor() const
{
- return m_defaultCellAttr->GetEditor();
+ return m_defaultCellAttr->GetEditor(NULL);
}
// ----------------------------------------------------------------------------
wxGridCellRenderer* wxGrid::GetCellRenderer(int row, int col)
{
wxGridCellAttr* attr = GetCellAttr(row, col);
- wxGridCellRenderer* renderer = attr->GetRenderer();
+ wxGridCellRenderer* renderer = attr->GetRenderer(GetDefaultRendererForCell(row,col));
attr->DecRef();
return renderer;
}
wxGridCellEditor* wxGrid::GetCellEditor(int row, int col)
{
wxGridCellAttr* attr = GetCellAttr(row, col);
- wxGridCellEditor* editor = attr->GetEditor();
+ wxGridCellEditor* editor = attr->GetEditor(GetDefaultEditorForCell(row, col));
attr->DecRef();
return editor;
}
return FALSE;
}
- // RD: Maybe m_table->CanHaveAttributes() would be better in case the
- // table is providing the attributes itself??? In which case
- // I don't think the grid should create a Provider object for the
- // table but the table should be smart enough to do that on its own.
- if ( !m_table->GetAttrProvider() )
- {
- // use the default attr provider by default
- // (another choice would be to just return FALSE thus forcing the user
- // to it himself)
- m_table->SetAttrProvider(new wxGridCellAttrProvider);
- }
-
- return TRUE;
+ return m_table->CanHaveAttributes();
}
void wxGrid::ClearAttrCache()
}
}
+// ----------------------------------------------------------------------------
+// Data type registration
+// ----------------------------------------------------------------------------
+
+void wxGrid::RegisterDataType(const wxString& typeName,
+ wxGridCellRenderer* renderer,
+ wxGridCellEditor* editor)
+{
+ m_typeRegistry->RegisterDataType(typeName, renderer, editor);
+}
+
+
+wxGridCellEditor* wxGrid::GetDefaultEditorForCell(int row, int col)
+{
+ wxString typeName = m_table->GetTypeName(row, col);
+ return GetDefaultEditorForType(typeName);
+}
+
+wxGridCellRenderer* wxGrid::GetDefaultRendererForCell(int row, int col)
+{
+ wxString typeName = m_table->GetTypeName(row, col);
+ return GetDefaultRendererForType(typeName);
+}
+
+wxGridCellEditor* wxGrid::GetDefaultEditorForType(const wxString& typeName)
+{
+ int index = m_typeRegistry->FindDataType(typeName);
+ if (index == -1) {
+ // Should we force the failure here or let it fallback to string handling???
+ // wxFAIL_MSG(wxT("Unknown data type name"));
+ return NULL;
+ }
+ return m_typeRegistry->GetEditor(index);
+}
+
+wxGridCellRenderer* wxGrid::GetDefaultRendererForType(const wxString& typeName)
+{
+ int index = m_typeRegistry->FindDataType(typeName);
+ if (index == -1) {
+ // Should we force the failure here or let it fallback to string handling???
+ // wxFAIL_MSG(wxT("Unknown data type name"));
+ return NULL;
+ }
+ return m_typeRegistry->GetRenderer(index);
+}
+
+
// ----------------------------------------------------------------------------
// row/col size
// ----------------------------------------------------------------------------
+void wxGrid::EnableDragRowSize( bool enable )
+{
+ m_canDragRowSize = enable;
+}
+
+
+void wxGrid::EnableDragColSize( bool enable )
+{
+ m_canDragColSize = enable;
+}
+
+
void wxGrid::SetDefaultRowSize( int height, bool resizeExistingRows )
{
m_defaultRowHeight = wxMax( height, WXGRID_MIN_ROW_HEIGHT );
if ( resizeExistingRows )
{
- int row;
- int bottom = 0;
- for ( row = 0; row < m_numRows; row++ )
- {
- m_rowHeights[row] = m_defaultRowHeight;
- bottom += m_defaultRowHeight;
- m_rowBottoms[row] = bottom;
- }
+ InitRowHeights();
+
CalcDimensions();
}
}
{
wxCHECK_RET( row >= 0 && row < m_numRows, _T("invalid row index") );
- int i;
+ if ( m_rowHeights.IsEmpty() )
+ {
+ // need to really create the array
+ InitRowHeights();
+ }
int h = wxMax( 0, height );
int diff = h - m_rowHeights[row];
m_rowHeights[row] = h;
+ int i;
for ( i = row; i < m_numRows; i++ )
{
m_rowBottoms[i] += diff;
if ( resizeExistingCols )
{
- int col;
- int right = 0;
- for ( col = 0; col < m_numCols; col++ )
- {
- m_colWidths[col] = m_defaultColWidth;
- right += m_defaultColWidth;
- m_colRights[col] = right;
- }
+ InitColWidths();
+
CalcDimensions();
}
}
{
wxCHECK_RET( col >= 0 && col < m_numCols, _T("invalid column index") );
- int i;
+ // should we check that it's bigger than GetColMinimalWidth(col) here?
+
+ if ( m_colWidths.IsEmpty() )
+ {
+ // need to really create the array
+ InitColWidths();
+ }
int w = wxMax( 0, width );
int diff = w - m_colWidths[col];
m_colWidths[col] = w;
+ int i;
for ( i = col; i < m_numCols; i++ )
{
m_colRights[i] += diff;
}
+void wxGrid::SetColMinimalWidth( int col, int width )
+{
+ m_colMinWidths.Put(col, (wxObject *)width);
+}
+
+int wxGrid::GetColMinimalWidth(int col) const
+{
+ wxObject *obj = m_colMinWidths.Get(m_dragRowOrCol);
+ return obj ? (int)obj : WXGRID_MIN_COL_WIDTH;
+}
+
//
// ------ cell value accessor functions
//