X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/14ac4f3c548f32d9bd461734a30c1e0787c7cc06..19de4eecd5d48c18bfdc040e763f69a6d8168599:/src/generic/grid.cpp diff --git a/src/generic/grid.cpp b/src/generic/grid.cpp index 405f72dc0d..068d735b61 100644 --- a/src/generic/grid.cpp +++ b/src/generic/grid.cpp @@ -32,10 +32,6 @@ #if wxUSE_GRID -#if !defined(wxUSE_NEW_GRID) || !(wxUSE_NEW_GRID) - #include "gridg.cpp" -#else // wxUSE_NEW_GRID - #ifndef WX_PRECOMP #include "wx/utils.h" #include "wx/dcclient.h" @@ -89,6 +85,10 @@ struct wxGridCellWithAttr wxGridCellCoords coords; wxGridCellAttr *attr; + +// Cannot do this: +// DECLARE_NO_COPY_CLASS(wxGridCellWithAttr) +// without rewriting the macros, which require a public copy constructor. }; WX_DECLARE_EXPORTED_OBJARRAY(wxGridCellWithAttr, wxGridCellWithAttrArray); @@ -141,6 +141,7 @@ private: DECLARE_DYNAMIC_CLASS(wxGridRowLabelWindow) DECLARE_EVENT_TABLE() + DECLARE_NO_COPY_CLASS(wxGridRowLabelWindow) }; @@ -162,6 +163,7 @@ private: DECLARE_DYNAMIC_CLASS(wxGridColLabelWindow) DECLARE_EVENT_TABLE() + DECLARE_NO_COPY_CLASS(wxGridColLabelWindow) }; @@ -183,6 +185,7 @@ private: DECLARE_DYNAMIC_CLASS(wxGridCornerLabelWindow) DECLARE_EVENT_TABLE() + DECLARE_NO_COPY_CLASS(wxGridCornerLabelWindow) }; class WXDLLEXPORT wxGridWindow : public wxWindow @@ -203,6 +206,8 @@ public: void ScrollWindow( int dx, int dy, const wxRect *rect ); + wxGrid* GetOwner() { return m_owner; } + private: wxGrid *m_owner; wxGridRowLabelWindow *m_rowLabelWin; @@ -218,6 +223,7 @@ private: DECLARE_DYNAMIC_CLASS(wxGridWindow) DECLARE_EVENT_TABLE() + DECLARE_NO_COPY_CLASS(wxGridWindow) }; @@ -240,6 +246,7 @@ private: wxGridCellEditor* m_editor; DECLARE_DYNAMIC_CLASS(wxGridCellEditorEvtHandler) DECLARE_EVENT_TABLE() + DECLARE_NO_COPY_CLASS(wxGridCellEditorEvtHandler) }; @@ -320,6 +327,8 @@ struct wxGridDataTypeInfo wxString m_typeName; wxGridCellRenderer* m_renderer; wxGridCellEditor* m_editor; + + DECLARE_NO_COPY_CLASS(wxGridDataTypeInfo) }; @@ -424,6 +433,7 @@ static inline int GetScrollY(int y) wxGridCellEditor::wxGridCellEditor() { m_control = NULL; + m_attr = NULL; } @@ -445,6 +455,10 @@ void wxGridCellEditor::PaintBackground(const wxRect& rectCell, { // erase the background because we might not fill the cell wxClientDC dc(m_control->GetParent()); + wxGridWindow* gridWindow = wxDynamicCast(m_control->GetParent(), wxGridWindow); + if (gridWindow) + gridWindow->GetOwner()->PrepareDC(dc); + dc.SetPen(*wxTRANSPARENT_PEN); dc.SetBrush(wxBrush(attr->GetBackgroundColour(), wxSOLID)); dc.DrawRectangle(rectCell); @@ -561,7 +575,11 @@ void wxGridCellTextEditor::Create(wxWindow* parent, #endif ); - // TODO: use m_maxChars + // set max length allowed in the textctrl, if the parameter was set + if (m_maxChars != 0) + { + ((wxTextCtrl*)m_control)->SetMaxLength(m_maxChars); + } wxGridCellEditor::Create(parent, id, evtHandler); } @@ -905,7 +923,7 @@ void wxGridCellNumberEditor::StartingKey(wxKeyEvent& event) { if ( !HasRange() ) { - int keycode = (int) event.KeyCode(); + int keycode = event.GetKeyCode(); if ( isdigit(keycode) || keycode == '+' || keycode == '-' || keycode == WXK_NUMPAD0 || keycode == WXK_NUMPAD1 @@ -1045,7 +1063,7 @@ void wxGridCellFloatEditor::Reset() void wxGridCellFloatEditor::StartingKey(wxKeyEvent& event) { - int keycode = (int)event.KeyCode(); + int keycode = event.GetKeyCode(); if ( isdigit(keycode) || keycode == '+' || keycode == '-' || keycode == '.' || keycode == WXK_NUMPAD0 || keycode == WXK_NUMPAD1 @@ -1216,7 +1234,32 @@ void wxGridCellBoolEditor::SetSize(const wxRect& r) size.y -= 2; #endif - m_control->Move(r.x + r.width/2 - size.x/2, r.y + r.height/2 - size.y/2); + int hAlign = wxALIGN_CENTRE; + int vAlign = wxALIGN_CENTRE; + if (GetCellAttr()) + GetCellAttr()->GetAlignment(& hAlign, & vAlign); + + int x = 0, y = 0; + if (hAlign == wxALIGN_LEFT) + { + x = r.x + 2; +#ifdef __WXMSW__ + x += 2; +#endif + y = r.y + r.height/2 - size.y/2; + } + else if (hAlign == wxALIGN_RIGHT) + { + x = r.x + r.width - size.x - 2; + y = r.y + r.height/2 - size.y/2; + } + else if (hAlign == wxALIGN_CENTRE) + { + x = r.x + r.width/2 - size.x/2; + y = r.y + r.height/2 - size.y/2; + } + + m_control->Move(x, y); } void wxGridCellBoolEditor::Show(bool show, wxGridCellAttr *attr) @@ -1452,7 +1495,7 @@ wxString wxGridCellChoiceEditor::GetValue() const void wxGridCellEditorEvtHandler::OnKeyDown(wxKeyEvent& event) { - switch ( event.KeyCode() ) + switch ( event.GetKeyCode() ) { case WXK_ESCAPE: m_editor->Reset(); @@ -1477,7 +1520,7 @@ void wxGridCellEditorEvtHandler::OnKeyDown(wxKeyEvent& event) void wxGridCellEditorEvtHandler::OnChar(wxKeyEvent& event) { - switch ( event.KeyCode() ) + switch ( event.GetKeyCode() ) { case WXK_ESCAPE: case WXK_TAB: @@ -1532,7 +1575,7 @@ void wxGridCellRenderer::Draw(wxGrid& grid, { dc.SetBrush( wxBrush(attr.GetBackgroundColour(), wxSOLID) ); } - } + } else { dc.SetBrush(wxBrush(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_BTNFACE), wxSOLID)); @@ -1950,11 +1993,31 @@ void wxGridCellBoolRenderer::Draw(wxGrid& grid, } // draw a border around checkmark + int vAlign, hAlign; + attr.GetAlignment(& hAlign, &vAlign); + wxRect rectBorder; - rectBorder.x = rect.x + rect.width/2 - size.x/2; - rectBorder.y = rect.y + rect.height/2 - size.y/2; - rectBorder.width = size.x; - rectBorder.height = size.y; + if (hAlign == wxALIGN_CENTRE) + { + rectBorder.x = rect.x + rect.width/2 - size.x/2; + rectBorder.y = rect.y + rect.height/2 - size.y/2; + rectBorder.width = size.x; + rectBorder.height = size.y; + } + else if (hAlign == wxALIGN_LEFT) + { + rectBorder.x = rect.x + 2; + rectBorder.y = rect.y + rect.height/2 - size.y/2; + rectBorder.width = size.x; + rectBorder.height = size.y; + } + else if (hAlign == wxALIGN_RIGHT) + { + rectBorder.x = rect.x + rect.width - size.x - 2; + rectBorder.y = rect.y + rect.height/2 - size.y/2; + rectBorder.width = size.x; + rectBorder.height = size.y; + } bool value; if ( grid.GetTable()->CanGetValueAs(row, col, wxGRID_VALUE_BOOL) ) @@ -2002,7 +2065,7 @@ void wxGridCellAttr::Init(wxGridCellAttr *attrDefault) m_attrkind = wxGridCellAttr::Cell; m_sizeRows = m_sizeCols = 1; - m_overflow = TRUE; + m_overflow = UnsetOverflow; SetDefAttr(attrDefault); } @@ -2074,6 +2137,9 @@ void wxGridCellAttr::MergeWith(wxGridCellAttr *mergefrom) if ( !HasReadWriteMode() && mergefrom->HasReadWriteMode() ) SetReadOnly(mergefrom->IsReadOnly()); + if (!HasOverflowMode() && mergefrom->HasOverflowMode() ) + SetOverflow(mergefrom->GetOverflow()); + SetDefAttr(mergefrom->m_defGridAttr); } @@ -3400,7 +3466,7 @@ END_EVENT_TABLE() wxGridRowLabelWindow::wxGridRowLabelWindow( wxGrid *parent, wxWindowID id, const wxPoint &pos, const wxSize &size ) - : wxWindow( parent, id, pos, size, wxWANTS_CHARS ) + : wxWindow( parent, id, pos, size, wxWANTS_CHARS|wxBORDER_NONE ) { m_owner = parent; } @@ -3466,7 +3532,7 @@ END_EVENT_TABLE() wxGridColLabelWindow::wxGridColLabelWindow( wxGrid *parent, wxWindowID id, const wxPoint &pos, const wxSize &size ) - : wxWindow( parent, id, pos, size, wxWANTS_CHARS ) + : wxWindow( parent, id, pos, size, wxWANTS_CHARS|wxBORDER_NONE ) { m_owner = parent; } @@ -3531,7 +3597,7 @@ END_EVENT_TABLE() wxGridCornerLabelWindow::wxGridCornerLabelWindow( wxGrid *parent, wxWindowID id, const wxPoint &pos, const wxSize &size ) - : wxWindow( parent, id, pos, size, wxWANTS_CHARS ) + : wxWindow( parent, id, pos, size, wxWANTS_CHARS|wxBORDER_NONE ) { m_owner = parent; } @@ -3601,7 +3667,7 @@ wxGridWindow::wxGridWindow( wxGrid *parent, wxWindowID id, const wxPoint &pos, const wxSize &size ) - : wxWindow( parent, id, pos, size, wxWANTS_CHARS | wxCLIP_CHILDREN, + : wxWindow( parent, id, pos, size, wxWANTS_CHARS | wxBORDER_NONE | wxCLIP_CHILDREN, wxT("grid window") ) { @@ -3679,13 +3745,13 @@ void wxGridWindow::OnEraseBackground( wxEraseEvent& WXUNUSED(event) ) static int CoordToRowOrCol(int coord, int defaultDist, int minDist, const wxArrayInt& BorderArray, int nMax, - bool maxOnOverflow); + bool clipToMinMax); #define internalXToCol(x) CoordToRowOrCol(x, m_defaultColWidth, \ - WXGRID_MIN_COL_WIDTH, \ + m_minAcceptableColWidth, \ m_colRights, m_numCols, TRUE) #define internalYToRow(y) CoordToRowOrCol(y, m_defaultRowHeight, \ - WXGRID_MIN_ROW_HEIGHT, \ + m_minAcceptableRowHeight, \ m_rowBottoms, m_numRows, TRUE) ///////////////////////////////////////////////////////////////////// @@ -3847,16 +3913,23 @@ bool wxGrid::SetTable( wxGridTableBase *table, bool takeOwnership, { if ( m_created ) { - // RD: Actually, this should probably be allowed. I think it would be - // nice to be able to switch multiple Tables in and out of a single - // View at runtime. Is there anything in the implementation that - // would prevent this? + // stop all processing + m_created = FALSE; - // At least, you now have to cope with m_selection - wxFAIL_MSG( wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") ); - return FALSE; + if (m_ownTable) + { + wxGridTableBase *t=m_table; + m_table=0; + delete t; + } + delete m_selection; + + m_table=0; + m_selection=0; + m_numRows=0; + m_numCols=0; } - else + if (table) { m_numRows = table->GetNumberRows(); m_numCols = table->GetNumberCols(); @@ -3900,18 +3973,21 @@ void wxGrid::Init() // TODO: something better than this ? // m_labelFont = this->GetFont(); -// m_labelFont = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT); -// m_labelFont.SetWeight( m_labelFont.GetWeight() + 2 ); + m_labelFont.SetWeight( wxBOLD ); m_rowLabelHorizAlign = wxALIGN_CENTRE; m_rowLabelVertAlign = wxALIGN_CENTRE; m_colLabelHorizAlign = wxALIGN_CENTRE; m_colLabelVertAlign = wxALIGN_CENTRE; + m_colLabelTextOrientation = wxHORIZONTAL; m_defaultColWidth = WXGRID_DEFAULT_COL_WIDTH; m_defaultRowHeight = m_gridWin->GetCharHeight(); + m_minAcceptableColWidth = WXGRID_MIN_COL_WIDTH; + m_minAcceptableRowHeight = WXGRID_MIN_ROW_HEIGHT; + #if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl() m_defaultRowHeight += 8; #else @@ -3943,10 +4019,8 @@ void wxGrid::Init() m_selectingTopLeft = wxGridNoCellCoords; m_selectingBottomRight = wxGridNoCellCoords; -// m_selectionBackground = wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT); -// m_selectionForeground = wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT); - m_selectionBackground = *wxBLACK; - m_selectionForeground = *wxWHITE; + m_selectionBackground = wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT); + m_selectionForeground = wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT); m_editable = TRUE; // default for whole grid @@ -4730,10 +4804,23 @@ void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent& event ) // else if (event.LeftDClick() ) { - if ( YToEdgeOfRow(y) < 0 ) + int row = YToEdgeOfRow(y); + if ( row < 0 ) { row = YToRow(y); - SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK, row, -1, event ); + if ( row >=0 && + !SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK, row, -1, event ) ) + { + // no default action at the moment + } + } + else + { + // adjust row height depending on label text + AutoSizeRowLabelSize( row ); + + ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_colLabelWin); + m_dragLastPos = -1; } } @@ -4762,7 +4849,8 @@ void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent& event ) else if ( event.RightDown() ) { row = YToRow(y); - if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK, row, -1, event ) ) + if ( row >=0 && + !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK, row, -1, event ) ) { // no default action at the moment } @@ -4774,7 +4862,8 @@ void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent& event ) else if ( event.RightDClick() ) { row = YToRow(y); - if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK, row, -1, event ) ) + if ( row >= 0 && + !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK, row, -1, event ) ) { // no default action at the moment } @@ -4936,10 +5025,23 @@ void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent& event ) // if ( event.LeftDClick() ) { - if ( XToEdgeOfCol(x) < 0 ) + int col = XToEdgeOfCol(x); + if ( col < 0 ) { col = XToCol(x); - SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK, -1, col, event ); + if ( col >= 0 && + ! SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK, -1, col, event ) ) + { + // no default action at the moment + } + } + else + { + // adjust column width depending on label text + AutoSizeColLabelSize( col ); + + ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_colLabelWin); + m_dragLastPos = -1; } } @@ -4968,7 +5070,8 @@ void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent& event ) else if ( event.RightDown() ) { col = XToCol(x); - if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK, -1, col, event ) ) + if ( col >= 0 && + !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK, -1, col, event ) ) { // no default action at the moment } @@ -4980,7 +5083,8 @@ void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent& event ) else if ( event.RightDClick() ) { col = XToCol(x); - if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK, -1, col, event ) ) + if ( col >= 0 && + !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK, -1, col, event ) ) { // no default action at the moment } @@ -5331,15 +5435,28 @@ void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent& event ) { if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL ) { - if ( m_selectingTopLeft != wxGridNoCellCoords && - m_selectingBottomRight != wxGridNoCellCoords ) + if (m_winCapture) { - if (m_winCapture) - { - if (m_winCapture->HasCapture()) m_winCapture->ReleaseMouse(); - m_winCapture = NULL; - } + if (m_winCapture->HasCapture()) m_winCapture->ReleaseMouse(); + m_winCapture = NULL; + } + + if ( coords == m_currentCellCoords && m_waitForSlowClick && CanEnableCellControl()) + { + ClearSelection(); + EnableCellEditControl(); + wxGridCellAttr* attr = GetCellAttr(coords); + wxGridCellEditor *editor = attr->GetEditor(this, coords.GetRow(), coords.GetCol()); + editor->StartingClick(); + editor->DecRef(); + attr->DecRef(); + + m_waitForSlowClick = FALSE; + } + else if ( m_selectingTopLeft != wxGridNoCellCoords && + m_selectingBottomRight != wxGridNoCellCoords ) + { if ( m_selection ) { m_selection->SelectBlock( m_selectingTopLeft.GetRow(), @@ -5359,21 +5476,6 @@ void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent& event ) // drag-shrinking. ShowCellEditControl(); } - else - { - if( m_waitForSlowClick && CanEnableCellControl()) - { - EnableCellEditControl(); - - wxGridCellAttr* attr = GetCellAttr(coords); - wxGridCellEditor *editor = attr->GetEditor(this, coords.GetRow(), coords.GetCol()); - editor->StartingClick(); - editor->DecRef(); - attr->DecRef(); - - m_waitForSlowClick = FALSE; - } - } } else if ( m_cursorMode == WXGRID_CURSOR_RESIZE_ROW ) { @@ -5512,7 +5614,7 @@ void wxGrid::DoEndDragResizeRow() int rowTop = GetRowTop(m_dragRowOrCol); SetRowSize( m_dragRowOrCol, - wxMax( m_dragLastPos - rowTop, WXGRID_MIN_ROW_HEIGHT ) ); + wxMax( m_dragLastPos - rowTop, m_minAcceptableRowHeight ) ); if ( !GetBatchCount() ) { @@ -5529,10 +5631,9 @@ void wxGrid::DoEndDragResizeRow() { int i, cell_rows, cell_cols, subtract_rows = 0; int leftCol = XToCol(left); - int rightCol = XToCol(left+cw); + int rightCol = internalXToCol(left+cw); if (leftCol >= 0) { - if (rightCol < 0) rightCol = m_numCols; for (i=leftCol; i= 0) { - if (bottomRow < 0) bottomRow = m_numRows; for (i=topRow; iInsertRows( pos, numRows ); + bool done = m_table->InsertRows( pos, numRows ); + return done; // the table will have sent the results of the insert row // operation to this view object as a grid table message @@ -5693,9 +5794,14 @@ bool wxGrid::AppendRows( int numRows, bool WXUNUSED(updateLabels) ) return FALSE; } - return ( m_table && m_table->AppendRows( numRows ) ); - // the table will have sent the results of the append row - // operation to this view object as a grid table message + if ( m_table ) + { + bool done = m_table && m_table->AppendRows( numRows ); + return done; + // the table will have sent the results of the append row + // operation to this view object as a grid table message + } + return FALSE; } @@ -5714,7 +5820,8 @@ bool wxGrid::DeleteRows( int pos, int numRows, bool WXUNUSED(updateLabels) ) if (IsCellEditControlEnabled()) DisableCellEditControl(); - return (m_table->DeleteRows( pos, numRows )); + bool done = m_table->DeleteRows( pos, numRows ); + return done; // the table will have sent the results of the delete row // operation to this view object as a grid table message } @@ -5737,7 +5844,8 @@ bool wxGrid::InsertCols( int pos, int numCols, bool WXUNUSED(updateLabels) ) if (IsCellEditControlEnabled()) DisableCellEditControl(); - return m_table->InsertCols( pos, numCols ); + bool done = m_table->InsertCols( pos, numCols ); + return done; // the table will have sent the results of the insert col // operation to this view object as a grid table message } @@ -5755,9 +5863,14 @@ bool wxGrid::AppendCols( int numCols, bool WXUNUSED(updateLabels) ) return FALSE; } - return ( m_table && m_table->AppendCols( numCols ) ); - // the table will have sent the results of the append col - // operation to this view object as a grid table message + if ( m_table ) + { + bool done = m_table->AppendCols( numCols ); + return done; + // the table will have sent the results of the append col + // operation to this view object as a grid table message + } + return FALSE; } @@ -5776,7 +5889,8 @@ bool wxGrid::DeleteCols( int pos, int numCols, bool WXUNUSED(updateLabels) ) if (IsCellEditControlEnabled()) DisableCellEditControl(); - return ( m_table->DeleteCols( pos, numCols ) ); + bool done = m_table->DeleteCols( pos, numCols ); + return done; // the table will have sent the results of the delete col // operation to this view object as a grid table message } @@ -6020,7 +6134,7 @@ void wxGrid::OnKeyDown( wxKeyEvent& event ) // try local handlers // - switch ( event.KeyCode() ) + switch ( event.GetKeyCode() ) { case WXK_UP: if ( event.ControlDown() ) @@ -6185,7 +6299,7 @@ void wxGrid::OnKeyDown( wxKeyEvent& event ) // is special and will always start editing, for // other keys - ask the editor itself - if ( (event.KeyCode() == WXK_F2 && !event.HasModifiers()) + if ( (event.GetKeyCode() == WXK_F2 && !event.HasModifiers()) || editor->IsAcceptedKey(event) ) { // ensure cell is visble @@ -6223,7 +6337,7 @@ void wxGrid::OnKeyUp( wxKeyEvent& event ) { // try local handlers // - if ( event.KeyCode() == WXK_SHIFT ) + if ( event.GetKeyCode() == WXK_SHIFT ) { if ( m_selectingTopLeft != wxGridNoCellCoords && m_selectingBottomRight != wxGridNoCellCoords ) @@ -6838,37 +6952,32 @@ void wxGrid::DrawAllGridLines( wxDC& dc, const wxRegion & WXUNUSED(reg) ) bottom = wxMin( bottom, GetRowBottom(m_numRows - 1) ); // no gridlines inside multicells, clip them out - int leftCol = XToCol(left); - int topRow = YToRow(top); - int rightCol = XToCol(right); - int bottomRow = YToRow(bottom); + int leftCol = internalXToCol(left); + int topRow = internalYToRow(top); + int rightCol = internalXToCol(right); + int bottomRow = internalYToRow(bottom); wxRegion clippedcells(0, 0, cw, ch); - if ((leftCol >= 0) && (topRow >= 0)) - { - if (rightCol < 0) rightCol = m_numCols; - if (bottomRow < 0) bottomRow = m_numRows; - int i, j, cell_rows, cell_cols; - wxRect rect; + int i, j, cell_rows, cell_cols; + wxRect rect; - for (j=topRow; j 1) || (cell_cols > 1)) { - GetCellSize( j, i, &cell_rows, &cell_cols ); - if ((cell_rows > 1) || (cell_cols > 1)) - { - rect = CellToRect(j,i); - CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y ); - clippedcells.Subtract(rect); - } - else if ((cell_rows < 0) || (cell_cols < 0)) - { - rect = CellToRect(j+cell_rows, i+cell_cols); - CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y ); - clippedcells.Subtract(rect); - } + rect = CellToRect(j,i); + CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y ); + clippedcells.Subtract(rect); + } + else if ((cell_rows < 0) || (cell_cols < 0)) + { + rect = CellToRect(j+cell_rows, i+cell_cols); + CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y ); + clippedcells.Subtract(rect); } } } @@ -6878,7 +6987,7 @@ void wxGrid::DrawAllGridLines( wxDC& dc, const wxRegion & WXUNUSED(reg) ) // horizontal grid lines // - int i; + // already declared above - int i; for ( i = internalYToRow(top); i < m_numRows; i++ ) { int bot = GetRowBottom(i) - 1; @@ -7003,26 +7112,24 @@ void wxGrid::DrawColLabel( wxDC& dc, int col ) dc.SetTextForeground( GetLabelTextColour() ); dc.SetFont( GetLabelFont() ); - dc.SetBackgroundMode( wxTRANSPARENT ); - dc.SetTextForeground( GetLabelTextColour() ); - dc.SetFont( GetLabelFont() ); - - int hAlign, vAlign; + int hAlign, vAlign, orient; GetColLabelAlignment( &hAlign, &vAlign ); + orient = GetColLabelTextOrientation(); wxRect rect; rect.SetX( colLeft + 2 ); rect.SetY( 2 ); rect.SetWidth( GetColWidth(col) - 4 ); rect.SetHeight( m_colLabelHeight - 4 ); - DrawTextRectangle( dc, GetColLabelValue( col ), rect, hAlign, vAlign ); + DrawTextRectangle( dc, GetColLabelValue( col ), rect, hAlign, vAlign, orient ); } void wxGrid::DrawTextRectangle( wxDC& dc, const wxString& value, const wxRect& rect, int horizAlign, - int vertAlign ) + int vertAlign, + int textOrientation ) { wxArrayString lines; @@ -7031,18 +7138,20 @@ void wxGrid::DrawTextRectangle( wxDC& dc, //Forward to new API. DrawTextRectangle( dc, - lines, - rect, - horizAlign, - vertAlign ); + lines, + rect, + horizAlign, + vertAlign, + textOrientation ); } void wxGrid::DrawTextRectangle( wxDC& dc, - const wxArrayString& lines, - const wxRect& rect, - int horizAlign, - int vertAlign ) + const wxArrayString& lines, + const wxRect& rect, + int horizAlign, + int vertAlign, + int textOrientation ) { long textWidth, textHeight; long lineWidth, lineHeight; @@ -7053,49 +7162,80 @@ void wxGrid::DrawTextRectangle( wxDC& dc, nLines = lines.GetCount(); if( nLines > 0 ) { - int l; - float x, y; - GetTextBoxSize(dc, lines, &textWidth, &textHeight); - switch( vertAlign ) - { + int l; + float x = 0.0, y = 0.0; + + if( textOrientation == wxHORIZONTAL ) + GetTextBoxSize(dc, lines, &textWidth, &textHeight); + else + GetTextBoxSize( dc, lines, &textHeight, &textWidth ); + + switch( vertAlign ) + { case wxALIGN_BOTTOM: - y = rect.y + (rect.height - textHeight - 1); - break; + if( textOrientation == wxHORIZONTAL ) + y = rect.y + (rect.height - textHeight - 1); + else + x = rect.x + rect.width - textWidth; + break; case wxALIGN_CENTRE: - y = rect.y + ((rect.height - textHeight)/2); - break; + if( textOrientation == wxHORIZONTAL ) + y = rect.y + ((rect.height - textHeight)/2); + else + x = rect.x + ((rect.width - textWidth)/2); + break; case wxALIGN_TOP: default: - y = rect.y + 1; - break; - } - - // Align each line of a multi-line label - for( l = 0; l < nLines; l++ ) - { - dc.GetTextExtent(lines[l], &lineWidth, &lineHeight); + if( textOrientation == wxHORIZONTAL ) + y = rect.y + 1; + else + x = rect.x + 1; + break; + } - switch( horizAlign ) + // Align each line of a multi-line label + for( l = 0; l < nLines; l++ ) { - case wxALIGN_RIGHT: - x = rect.x + (rect.width - lineWidth - 1); - break; + dc.GetTextExtent(lines[l], &lineWidth, &lineHeight); - case wxALIGN_CENTRE: - x = rect.x + ((rect.width - lineWidth)/2); - break; + switch( horizAlign ) + { + case wxALIGN_RIGHT: + if( textOrientation == wxHORIZONTAL ) + x = rect.x + (rect.width - lineWidth - 1); + else + y = rect.y + lineWidth + 1; + break; - case wxALIGN_LEFT: - default: - x = rect.x + 1; - break; - } + case wxALIGN_CENTRE: + if( textOrientation == wxHORIZONTAL ) + x = rect.x + ((rect.width - lineWidth)/2); + else + y = rect.y + rect.height - ((rect.height - lineWidth)/2); + break; - dc.DrawText( lines[l], (int)x, (int)y ); - y += lineHeight; - } + case wxALIGN_LEFT: + default: + if( textOrientation == wxHORIZONTAL ) + x = rect.x + 1; + else + y = rect.y + rect.height - 1; + break; + } + + if( textOrientation == wxHORIZONTAL ) + { + dc.DrawText( lines[l], (int)x, (int)y ); + y += lineHeight; + } + else + { + dc.DrawRotatedText( lines[l], (int)x, (int)y, 90.0 ); + x += lineHeight; + } + } } dc.DestroyClippingRegion(); } @@ -7388,6 +7528,7 @@ void wxGrid::ShowCellEditControl() rect.SetRight(client_right-1); } + editor->SetCellAttr(attr); editor->SetSize( rect ); editor->Show( TRUE, attr ); @@ -7396,6 +7537,7 @@ void wxGrid::ShowCellEditControl() CalcDimensions(); editor->BeginEdit(row, col, this); + editor->SetCellAttr(NULL); editor->DecRef(); attr->DecRef(); @@ -7486,10 +7628,16 @@ void wxGrid::XYToCell( int x, int y, wxGridCellCoords& coords ) static int CoordToRowOrCol(int coord, int defaultDist, int minDist, const wxArrayInt& BorderArray, int nMax, - bool maxOnOverflow) + bool clipToMinMax) { + + if (coord < 0) + return clipToMinMax && (nMax > 0) ? 0 : -1; + + if (!defaultDist) defaultDist = 1; + size_t i_max = coord / defaultDist, i_min = 0; @@ -7497,7 +7645,7 @@ static int CoordToRowOrCol(int coord, int defaultDist, int minDist, { if((int) i_max < nMax) return i_max; - return maxOnOverflow ? nMax - 1 : -1; + return clipToMinMax ? nMax - 1 : -1; } if ( i_max >= BorderArray.GetCount()) @@ -7513,7 +7661,7 @@ static int CoordToRowOrCol(int coord, int defaultDist, int minDist, i_max = BorderArray.GetCount() - 1; } if ( coord >= BorderArray[i_max]) - return maxOnOverflow ? (int)i_max : -1; + return clipToMinMax ? (int)i_max : -1; if ( coord < BorderArray[0] ) return 0; @@ -7537,14 +7685,14 @@ static int CoordToRowOrCol(int coord, int defaultDist, int minDist, int wxGrid::YToRow( int y ) { return CoordToRowOrCol(y, m_defaultRowHeight, - WXGRID_MIN_ROW_HEIGHT, m_rowBottoms, m_numRows, FALSE); + m_minAcceptableRowHeight, m_rowBottoms, m_numRows, FALSE); } int wxGrid::XToCol( int x ) { return CoordToRowOrCol(x, m_defaultColWidth, - WXGRID_MIN_COL_WIDTH, m_colRights, m_numCols, FALSE); + m_minAcceptableColWidth, m_colRights, m_numCols, FALSE); } @@ -7889,13 +8037,11 @@ bool wxGrid::MovePageUp() m_gridWin->GetClientSize( &cw, &ch ); int y = GetRowTop(row); - int newRow = YToRow( y - ch + 1 ); - if ( newRow == -1 ) - { - newRow = 0; - } - else if ( newRow == row ) + int newRow = internalYToRow( y - ch + 1 ); + + if ( newRow == row ) { + //row > 0 , so newrow can never be less than 0 here. newRow = row - 1; } @@ -7913,20 +8059,17 @@ bool wxGrid::MovePageDown() if ( m_currentCellCoords == wxGridNoCellCoords ) return FALSE; int row = m_currentCellCoords.GetRow(); - if ( row < m_numRows ) + if ( (row+1) < m_numRows ) { int cw, ch; m_gridWin->GetClientSize( &cw, &ch ); int y = GetRowTop(row); - int newRow = YToRow( y + ch ); - if ( newRow == -1 ) + int newRow = internalYToRow( y + ch ); + if ( newRow == row ) { - newRow = m_numRows - 1; - } - else if ( newRow == row ) - { - newRow = row + 1; + // row < m_numRows , so newrow can't overflow here. + newRow = row + 1; } MakeCellVisible( newRow, m_currentCellCoords.GetCol() ); @@ -8211,6 +8354,11 @@ void wxGrid::GetColLabelAlignment( int *horiz, int *vert ) *vert = m_colLabelVertAlign; } +int wxGrid::GetColLabelTextOrientation() +{ + return m_colLabelTextOrientation; +} + wxString wxGrid::GetRowLabelValue( int row ) { if ( m_table ) @@ -8393,6 +8541,26 @@ void wxGrid::SetColLabelAlignment( int horiz, int vert ) } } +// Note: under MSW, the default column label font must be changed because it +// does not support vertical printing +// +// Example: wxFont font(9, wxSWISS, wxNORMAL, wxBOLD); +// pGrid->SetLabelFont(font); +// pGrid->SetColLabelTextOrientation(wxVERTICAL); +// +void wxGrid::SetColLabelTextOrientation( int textOrientation ) +{ + if( textOrientation == wxHORIZONTAL || textOrientation == wxVERTICAL ) + { + m_colLabelTextOrientation = textOrientation; + } + + if ( !GetBatchCount() ) + { + m_colLabelWin->Refresh(); + } +} + void wxGrid::SetRowLabelValue( int row, const wxString& s ) { if ( m_table ) @@ -9103,7 +9271,7 @@ void wxGrid::EnableDragGridSize( bool enable ) void wxGrid::SetDefaultRowSize( int height, bool resizeExistingRows ) { - m_defaultRowHeight = wxMax( height, WXGRID_MIN_ROW_HEIGHT ); + m_defaultRowHeight = wxMax( height, m_minAcceptableRowHeight ); if ( resizeExistingRows ) { @@ -9122,6 +9290,9 @@ void wxGrid::SetRowSize( int row, int height ) { wxCHECK_RET( row >= 0 && row < m_numRows, _T("invalid row index") ); + // See comment in SetColSize + if ( height < GetRowMinimalAcceptableHeight()) { return; } + if ( m_rowHeights.IsEmpty() ) { // need to really create the array @@ -9143,7 +9314,7 @@ void wxGrid::SetRowSize( int row, int height ) void wxGrid::SetDefaultColSize( int width, bool resizeExistingCols ) { - m_defaultColWidth = wxMax( width, WXGRID_MIN_COL_WIDTH ); + m_defaultColWidth = wxMax( width, m_minAcceptableColWidth ); if ( resizeExistingCols ) { @@ -9163,7 +9334,15 @@ void wxGrid::SetColSize( int col, int width ) wxCHECK_RET( col >= 0 && col < m_numCols, _T("invalid column index") ); // should we check that it's bigger than GetColMinimalWidth(col) here? - + // (VZ) + // No, because it is reasonable to assume the library user know's + // what he is doing. However whe should test against the weaker + // constariant of minimalAcceptableWidth, as this breaks rendering + // + // This test then fixes sf.net bug #645734 + + if ( width < GetColMinimalAcceptableWidth()) { return; } + if ( m_colWidths.IsEmpty() ) { // need to really create the array @@ -9197,24 +9376,52 @@ void wxGrid::SetColSize( int col, int width ) void wxGrid::SetColMinimalWidth( int col, int width ) { - m_colMinWidths.Put(col, width); + if (width > GetColMinimalAcceptableWidth()) { + m_colMinWidths[col] = width; + } } void wxGrid::SetRowMinimalHeight( int row, int width ) { - m_rowMinHeights.Put(row, width); + if (width > GetRowMinimalAcceptableHeight()) { + m_rowMinHeights[row] = width; + } } int wxGrid::GetColMinimalWidth(int col) const { - long value = m_colMinWidths.Get(col); - return value != wxNOT_FOUND ? (int)value : WXGRID_MIN_COL_WIDTH; + wxLongToLongHashMap::const_iterator it = m_colMinWidths.find(col); + return it != m_colMinWidths.end() ? (int)it->second : m_minAcceptableColWidth; } int wxGrid::GetRowMinimalHeight(int row) const { - long value = m_rowMinHeights.Get(row); - return value != wxNOT_FOUND ? (int)value : WXGRID_MIN_ROW_HEIGHT; + wxLongToLongHashMap::const_iterator it = m_rowMinHeights.find(row); + return it != m_rowMinHeights.end() ? (int)it->second : m_minAcceptableRowHeight; +} + +void wxGrid::SetColMinimalAcceptableWidth( int width ) +{ + if ( width<1 ) + return; + m_minAcceptableColWidth = width; +} + +void wxGrid::SetRowMinimalAcceptableHeight( int height ) +{ + if ( height<1 ) + return; + m_minAcceptableRowHeight = height; +}; + +int wxGrid::GetColMinimalAcceptableWidth() const +{ + return m_minAcceptableColWidth; +} + +int wxGrid::GetRowMinimalAcceptableHeight() const +{ + return m_minAcceptableRowHeight; } // ---------------------------------------------------------------------------- @@ -9264,7 +9471,11 @@ void wxGrid::AutoSizeColOrRow( int colOrRow, bool setAsMin, bool column ) dc.SetFont( GetLabelFont() ); if ( column ) + { dc.GetTextExtent( GetColLabelValue(col), &w, &h ); + if( GetColLabelTextOrientation() == wxVERTICAL ) + w = h; + } else dc.GetTextExtent( GetRowLabelValue(row), &w, &h ); @@ -9449,6 +9660,55 @@ void wxGrid::AutoSize() SetClientSize(sizeFit); } +void wxGrid::AutoSizeRowLabelSize( int row ) +{ + wxArrayString lines; + long w, h; + + // Hide the edit control, so it + // won't interfer with drag-shrinking. + if( IsCellEditControlShown() ) + { + HideCellEditControl(); + SaveEditControlValue(); + } + + // autosize row height depending on label text + StringToLines( GetRowLabelValue( row ), lines ); + wxClientDC dc( m_rowLabelWin ); + GetTextBoxSize( dc, lines, &w, &h); + if( h < m_defaultRowHeight ) + h = m_defaultRowHeight; + SetRowSize(row, h); + ForceRefresh(); +} + +void wxGrid::AutoSizeColLabelSize( int col ) +{ + wxArrayString lines; + long w, h; + + // Hide the edit control, so it + // won't interfer with drag-shrinking. + if( IsCellEditControlShown() ) + { + HideCellEditControl(); + SaveEditControlValue(); + } + + // autosize column width depending on label text + StringToLines( GetColLabelValue( col ), lines ); + wxClientDC dc( m_colLabelWin ); + if( GetColLabelTextOrientation() == wxHORIZONTAL ) + GetTextBoxSize( dc, lines, &w, &h); + else + GetTextBoxSize( dc, lines, &h, &w); + if( w < m_defaultColWidth ) + w = m_defaultColWidth; + SetColSize(col, w); + ForceRefresh(); +} + wxSize wxGrid::DoGetBestSize() const { // don't set sizes, only calculate them @@ -9761,8 +10021,6 @@ wxRect wxGrid::BlockToDeviceRect( const wxGridCellCoords &topLeft, return rect; } - - // // ------ Grid event classes // @@ -9841,7 +10099,5 @@ wxGridEditorCreatedEvent::wxGridEditorCreatedEvent(int id, wxEventType type, m_ctrl = ctrl; } - -#endif // !wxUSE_NEW_GRID/wxUSE_NEW_GRID - #endif // wxUSE_GRID +