1 /////////////////////////////////////////////////////////////////////////// 
   2 // Name:        src/generic/grid.cpp 
   3 // Purpose:     wxGrid and related classes 
   4 // Author:      Michael Bedward (based on code by Julian Smart, Robin Dunn) 
   5 // Modified by: Robin Dunn, Vadim Zeitlin, Santiago Palacios 
   8 // Copyright:   (c) Michael Bedward (mbedward@ozemail.com.au) 
   9 // Licence:     wxWindows licence 
  10 ///////////////////////////////////////////////////////////////////////////// 
  12 // For compilers that support precompilation, includes "wx/wx.h". 
  13 #include "wx/wxprec.h" 
  25     #include "wx/dcclient.h" 
  26     #include "wx/settings.h" 
  28     #include "wx/textctrl.h" 
  29     #include "wx/checkbox.h" 
  30     #include "wx/combobox.h" 
  31     #include "wx/valtext.h" 
  34     #include "wx/listbox.h" 
  37 #include "wx/textfile.h" 
  38 #include "wx/spinctrl.h" 
  39 #include "wx/tokenzr.h" 
  40 #include "wx/renderer.h" 
  42 #include "wx/generic/gridsel.h" 
  44 const wxChar wxGridNameStr
[] = wxT("grid"); 
  46 #if defined(__WXMOTIF__) 
  47     #define WXUNUSED_MOTIF(identifier)  WXUNUSED(identifier) 
  49     #define WXUNUSED_MOTIF(identifier)  identifier 
  52 #if defined(__WXGTK__) 
  53     #define WXUNUSED_GTK(identifier)    WXUNUSED(identifier) 
  55     #define WXUNUSED_GTK(identifier)    identifier 
  58 // Required for wxIs... functions 
  61 // ---------------------------------------------------------------------------- 
  63 // ---------------------------------------------------------------------------- 
  65 WX_DEFINE_ARRAY_WITH_DECL_PTR(wxGridCellAttr 
*, wxArrayAttrs
, 
  66                                  class WXDLLIMPEXP_ADV
); 
  68 struct wxGridCellWithAttr
 
  70     wxGridCellWithAttr(int row
, int col
, wxGridCellAttr 
*attr_
) 
  71         : coords(row
, col
), attr(attr_
) 
  76     wxGridCellWithAttr(const wxGridCellWithAttr
& other
) 
  77         : coords(other
.coords
), 
  83     wxGridCellWithAttr
& operator=(const wxGridCellWithAttr
& other
) 
  85         coords 
= other
.coords
; 
  86         if (attr 
!= other
.attr
) 
  95     void ChangeAttr(wxGridCellAttr
* new_attr
) 
  99             // "Delete" (i.e. DecRef) the old attribute. 
 102             // Take ownership of the new attribute, i.e. no IncRef. 
 106     ~wxGridCellWithAttr() 
 111     wxGridCellCoords coords
; 
 112     wxGridCellAttr  
*attr
; 
 115 WX_DECLARE_OBJARRAY_WITH_DECL(wxGridCellWithAttr
, wxGridCellWithAttrArray
, 
 116                               class WXDLLIMPEXP_ADV
); 
 118 #include "wx/arrimpl.cpp" 
 120 WX_DEFINE_OBJARRAY(wxGridCellCoordsArray
) 
 121 WX_DEFINE_OBJARRAY(wxGridCellWithAttrArray
) 
 123 // ---------------------------------------------------------------------------- 
 125 // ---------------------------------------------------------------------------- 
 127 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_LEFT_CLICK
) 
 128 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_RIGHT_CLICK
) 
 129 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_LEFT_DCLICK
) 
 130 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_RIGHT_DCLICK
) 
 131 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_BEGIN_DRAG
) 
 132 DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_LEFT_CLICK
) 
 133 DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_RIGHT_CLICK
) 
 134 DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_LEFT_DCLICK
) 
 135 DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_RIGHT_DCLICK
) 
 136 DEFINE_EVENT_TYPE(wxEVT_GRID_ROW_SIZE
) 
 137 DEFINE_EVENT_TYPE(wxEVT_GRID_COL_SIZE
) 
 138 DEFINE_EVENT_TYPE(wxEVT_GRID_COL_MOVE
) 
 139 DEFINE_EVENT_TYPE(wxEVT_GRID_RANGE_SELECT
) 
 140 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_CHANGE
) 
 141 DEFINE_EVENT_TYPE(wxEVT_GRID_SELECT_CELL
) 
 142 DEFINE_EVENT_TYPE(wxEVT_GRID_EDITOR_SHOWN
) 
 143 DEFINE_EVENT_TYPE(wxEVT_GRID_EDITOR_HIDDEN
) 
 144 DEFINE_EVENT_TYPE(wxEVT_GRID_EDITOR_CREATED
) 
 146 // ---------------------------------------------------------------------------- 
 148 // ---------------------------------------------------------------------------- 
 150 // common base class for various grid subwindows 
 151 class WXDLLIMPEXP_ADV wxGridSubwindow 
: public wxWindow
 
 154     wxGridSubwindow() { m_owner 
= NULL
; } 
 155     wxGridSubwindow(wxGrid 
*owner
, 
 159                     int additionalStyle 
= 0, 
 160                     const wxString
& name 
= wxPanelNameStr
) 
 161         : wxWindow(owner
, id
, pos
, size
, 
 162                    wxBORDER_NONE 
| additionalStyle
, 
 168     virtual bool AcceptsFocus() const { return false; } 
 170     wxGrid 
*GetOwner() { return m_owner
; } 
 173     void OnMouseCaptureLost(wxMouseCaptureLostEvent
& event
); 
 177     DECLARE_EVENT_TABLE() 
 178     DECLARE_NO_COPY_CLASS(wxGridSubwindow
) 
 181 class WXDLLIMPEXP_ADV wxGridRowLabelWindow 
: public wxGridSubwindow
 
 184     wxGridRowLabelWindow() { } 
 185     wxGridRowLabelWindow( wxGrid 
*parent
, wxWindowID id
, 
 186                           const wxPoint 
&pos
, const wxSize 
&size 
); 
 189     void OnPaint( wxPaintEvent
& event 
); 
 190     void OnMouseEvent( wxMouseEvent
& event 
); 
 191     void OnMouseWheel( wxMouseEvent
& event 
); 
 193     DECLARE_DYNAMIC_CLASS(wxGridRowLabelWindow
) 
 194     DECLARE_EVENT_TABLE() 
 195     DECLARE_NO_COPY_CLASS(wxGridRowLabelWindow
) 
 199 class WXDLLIMPEXP_ADV wxGridColLabelWindow 
: public wxGridSubwindow
 
 202     wxGridColLabelWindow() { } 
 203     wxGridColLabelWindow( wxGrid 
*parent
, wxWindowID id
, 
 204                           const wxPoint 
&pos
, const wxSize 
&size 
); 
 207     void OnPaint( wxPaintEvent
& event 
); 
 208     void OnMouseEvent( wxMouseEvent
& event 
); 
 209     void OnMouseWheel( wxMouseEvent
& event 
); 
 211     DECLARE_DYNAMIC_CLASS(wxGridColLabelWindow
) 
 212     DECLARE_EVENT_TABLE() 
 213     DECLARE_NO_COPY_CLASS(wxGridColLabelWindow
) 
 217 class WXDLLIMPEXP_ADV wxGridCornerLabelWindow 
: public wxGridSubwindow
 
 220     wxGridCornerLabelWindow() { } 
 221     wxGridCornerLabelWindow( wxGrid 
*parent
, wxWindowID id
, 
 222                              const wxPoint 
&pos
, const wxSize 
&size 
); 
 225     void OnMouseEvent( wxMouseEvent
& event 
); 
 226     void OnMouseWheel( wxMouseEvent
& event 
); 
 227     void OnPaint( wxPaintEvent
& event 
); 
 229     DECLARE_DYNAMIC_CLASS(wxGridCornerLabelWindow
) 
 230     DECLARE_EVENT_TABLE() 
 231     DECLARE_NO_COPY_CLASS(wxGridCornerLabelWindow
) 
 234 class WXDLLIMPEXP_ADV wxGridWindow 
: public wxGridSubwindow
 
 239         m_rowLabelWin 
= NULL
; 
 240         m_colLabelWin 
= NULL
; 
 243     wxGridWindow( wxGrid 
*parent
, 
 244                   wxGridRowLabelWindow 
*rowLblWin
, 
 245                   wxGridColLabelWindow 
*colLblWin
, 
 246                   wxWindowID id
, const wxPoint 
&pos
, const wxSize 
&size 
); 
 248     void ScrollWindow( int dx
, int dy
, const wxRect 
*rect 
); 
 250     virtual bool AcceptsFocus() const { return true; } 
 253     wxGridRowLabelWindow     
*m_rowLabelWin
; 
 254     wxGridColLabelWindow     
*m_colLabelWin
; 
 256     void OnPaint( wxPaintEvent 
&event 
); 
 257     void OnMouseWheel( wxMouseEvent
& event 
); 
 258     void OnMouseEvent( wxMouseEvent
& event 
); 
 259     void OnKeyDown( wxKeyEvent
& ); 
 260     void OnKeyUp( wxKeyEvent
& ); 
 261     void OnChar( wxKeyEvent
& ); 
 262     void OnEraseBackground( wxEraseEvent
& ); 
 263     void OnFocus( wxFocusEvent
& ); 
 265     DECLARE_DYNAMIC_CLASS(wxGridWindow
) 
 266     DECLARE_EVENT_TABLE() 
 267     DECLARE_NO_COPY_CLASS(wxGridWindow
) 
 271 class wxGridCellEditorEvtHandler 
: public wxEvtHandler
 
 274     wxGridCellEditorEvtHandler(wxGrid
* grid
, wxGridCellEditor
* editor
) 
 281     void OnKillFocus(wxFocusEvent
& event
); 
 282     void OnKeyDown(wxKeyEvent
& event
); 
 283     void OnChar(wxKeyEvent
& event
); 
 285     void SetInSetFocus(bool inSetFocus
) { m_inSetFocus 
= inSetFocus
; } 
 289     wxGridCellEditor   
*m_editor
; 
 291     // Work around the fact that a focus kill event can be sent to 
 292     // a combobox within a set focus event. 
 295     DECLARE_EVENT_TABLE() 
 296     DECLARE_DYNAMIC_CLASS(wxGridCellEditorEvtHandler
) 
 297     DECLARE_NO_COPY_CLASS(wxGridCellEditorEvtHandler
) 
 301 IMPLEMENT_ABSTRACT_CLASS(wxGridCellEditorEvtHandler
, wxEvtHandler
) 
 303 BEGIN_EVENT_TABLE( wxGridCellEditorEvtHandler
, wxEvtHandler 
) 
 304     EVT_KILL_FOCUS( wxGridCellEditorEvtHandler::OnKillFocus 
) 
 305     EVT_KEY_DOWN( wxGridCellEditorEvtHandler::OnKeyDown 
) 
 306     EVT_CHAR( wxGridCellEditorEvtHandler::OnChar 
) 
 310 // ---------------------------------------------------------------------------- 
 311 // the internal data representation used by wxGridCellAttrProvider 
 312 // ---------------------------------------------------------------------------- 
 314 // this class stores attributes set for cells 
 315 class WXDLLIMPEXP_ADV wxGridCellAttrData
 
 318     void SetAttr(wxGridCellAttr 
*attr
, int row
, int col
); 
 319     wxGridCellAttr 
*GetAttr(int row
, int col
) const; 
 320     void UpdateAttrRows( size_t pos
, int numRows 
); 
 321     void UpdateAttrCols( size_t pos
, int numCols 
); 
 324     // searches for the attr for given cell, returns wxNOT_FOUND if not found 
 325     int FindIndex(int row
, int col
) const; 
 327     wxGridCellWithAttrArray m_attrs
; 
 330 // this class stores attributes set for rows or columns 
 331 class WXDLLIMPEXP_ADV wxGridRowOrColAttrData
 
 334     // empty ctor to suppress warnings 
 335     wxGridRowOrColAttrData() {} 
 336     ~wxGridRowOrColAttrData(); 
 338     void SetAttr(wxGridCellAttr 
*attr
, int rowOrCol
); 
 339     wxGridCellAttr 
*GetAttr(int rowOrCol
) const; 
 340     void UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols 
); 
 343     wxArrayInt m_rowsOrCols
; 
 344     wxArrayAttrs m_attrs
; 
 347 // NB: this is just a wrapper around 3 objects: one which stores cell 
 348 //     attributes, and 2 others for row/col ones 
 349 class WXDLLIMPEXP_ADV wxGridCellAttrProviderData
 
 352     wxGridCellAttrData m_cellAttrs
; 
 353     wxGridRowOrColAttrData m_rowAttrs
, 
 358 // ---------------------------------------------------------------------------- 
 359 // data structures used for the data type registry 
 360 // ---------------------------------------------------------------------------- 
 362 struct wxGridDataTypeInfo
 
 364     wxGridDataTypeInfo(const wxString
& typeName
, 
 365                        wxGridCellRenderer
* renderer
, 
 366                        wxGridCellEditor
* editor
) 
 367         : m_typeName(typeName
), m_renderer(renderer
), m_editor(editor
) 
 370     ~wxGridDataTypeInfo() 
 372         wxSafeDecRef(m_renderer
); 
 373         wxSafeDecRef(m_editor
); 
 377     wxGridCellRenderer
* m_renderer
; 
 378     wxGridCellEditor
*   m_editor
; 
 380     DECLARE_NO_COPY_CLASS(wxGridDataTypeInfo
) 
 384 WX_DEFINE_ARRAY_WITH_DECL_PTR(wxGridDataTypeInfo
*, wxGridDataTypeInfoArray
, 
 385                                  class WXDLLIMPEXP_ADV
); 
 388 class WXDLLIMPEXP_ADV wxGridTypeRegistry
 
 391   wxGridTypeRegistry() {} 
 392     ~wxGridTypeRegistry(); 
 394     void RegisterDataType(const wxString
& typeName
, 
 395                      wxGridCellRenderer
* renderer
, 
 396                      wxGridCellEditor
* editor
); 
 398     // find one of already registered data types 
 399     int FindRegisteredDataType(const wxString
& typeName
); 
 401     // try to FindRegisteredDataType(), if this fails and typeName is one of 
 402     // standard typenames, register it and return its index 
 403     int FindDataType(const wxString
& typeName
); 
 405     // try to FindDataType(), if it fails see if it is not one of already 
 406     // registered data types with some params in which case clone the 
 407     // registered data type and set params for it 
 408     int FindOrCloneDataType(const wxString
& typeName
); 
 410     wxGridCellRenderer
* GetRenderer(int index
); 
 411     wxGridCellEditor
*   GetEditor(int index
); 
 414     wxGridDataTypeInfoArray m_typeinfo
; 
 418 // ---------------------------------------------------------------------------- 
 419 // conditional compilation 
 420 // ---------------------------------------------------------------------------- 
 422 #ifndef WXGRID_DRAW_LINES 
 423 #define WXGRID_DRAW_LINES 1 
 426 // ---------------------------------------------------------------------------- 
 428 // ---------------------------------------------------------------------------- 
 430 //#define DEBUG_ATTR_CACHE 
 431 #ifdef DEBUG_ATTR_CACHE 
 432     static size_t gs_nAttrCacheHits 
= 0; 
 433     static size_t gs_nAttrCacheMisses 
= 0; 
 436 // ---------------------------------------------------------------------------- 
 438 // ---------------------------------------------------------------------------- 
 440 wxGridCellCoords 
wxGridNoCellCoords( -1, -1 ); 
 441 wxRect 
wxGridNoCellRect( -1, -1, -1, -1 ); 
 444 // TODO: this doesn't work at all, grid cells have different sizes and approx 
 445 //       calculations don't work as because of the size mismatch scrollbars 
 446 //       sometimes fail to be shown when they should be or vice versa 
 448 //       The scroll bars may be a little flakey once in a while, but that is 
 449 //       surely much less horrible than having scroll lines of only 1!!! 
 452 //       Well, it's still seriously broken so it might be better but needs 
 455 static const size_t GRID_SCROLL_LINE_X 
= 15;  // 1; 
 456 static const size_t GRID_SCROLL_LINE_Y 
= GRID_SCROLL_LINE_X
; 
 458 // the size of hash tables used a bit everywhere (the max number of elements 
 459 // in these hash tables is the number of rows/columns) 
 460 static const int GRID_HASH_SIZE 
= 100; 
 463 // ---------------------------------------------------------------------------- 
 465 // ---------------------------------------------------------------------------- 
 467 static inline int GetScrollX(int x
) 
 469     return (x 
+ GRID_SCROLL_LINE_X 
- 1) / GRID_SCROLL_LINE_X
; 
 472 static inline int GetScrollY(int y
) 
 474     return (y 
+ GRID_SCROLL_LINE_Y 
- 1) / GRID_SCROLL_LINE_Y
; 
 478 // ============================================================================ 
 480 // ============================================================================ 
 482 // ---------------------------------------------------------------------------- 
 484 // ---------------------------------------------------------------------------- 
 486 wxGridCellEditor::wxGridCellEditor() 
 492 wxGridCellEditor::~wxGridCellEditor() 
 497 void wxGridCellEditor::Create(wxWindow
* WXUNUSED(parent
), 
 498                               wxWindowID 
WXUNUSED(id
), 
 499                               wxEvtHandler
* evtHandler
) 
 502         m_control
->PushEventHandler(evtHandler
); 
 505 void wxGridCellEditor::PaintBackground(const wxRect
& rectCell
, 
 506                                        wxGridCellAttr 
*attr
) 
 508     // erase the background because we might not fill the cell 
 509     wxClientDC 
dc(m_control
->GetParent()); 
 510     wxGridWindow
* gridWindow 
= wxDynamicCast(m_control
->GetParent(), wxGridWindow
); 
 512         gridWindow
->GetOwner()->PrepareDC(dc
); 
 514     dc
.SetPen(*wxTRANSPARENT_PEN
); 
 515     dc
.SetBrush(wxBrush(attr
->GetBackgroundColour(), wxBRUSHSTYLE_SOLID
)); 
 516     dc
.DrawRectangle(rectCell
); 
 518     // redraw the control we just painted over 
 519     m_control
->Refresh(); 
 522 void wxGridCellEditor::Destroy() 
 526         m_control
->PopEventHandler( true /* delete it*/ ); 
 528         m_control
->Destroy(); 
 533 void wxGridCellEditor::Show(bool show
, wxGridCellAttr 
*attr
) 
 535     wxASSERT_MSG(m_control
, wxT("The wxGridCellEditor must be created first!")); 
 537     m_control
->Show(show
); 
 541         // set the colours/fonts if we have any 
 544             m_colFgOld 
= m_control
->GetForegroundColour(); 
 545             m_control
->SetForegroundColour(attr
->GetTextColour()); 
 547             m_colBgOld 
= m_control
->GetBackgroundColour(); 
 548             m_control
->SetBackgroundColour(attr
->GetBackgroundColour()); 
 550 // Workaround for GTK+1 font setting problem on some platforms 
 551 #if !defined(__WXGTK__) || defined(__WXGTK20__) 
 552             m_fontOld 
= m_control
->GetFont(); 
 553             m_control
->SetFont(attr
->GetFont()); 
 556             // can't do anything more in the base class version, the other 
 557             // attributes may only be used by the derived classes 
 562         // restore the standard colours fonts 
 563         if ( m_colFgOld
.Ok() ) 
 565             m_control
->SetForegroundColour(m_colFgOld
); 
 566             m_colFgOld 
= wxNullColour
; 
 569         if ( m_colBgOld
.Ok() ) 
 571             m_control
->SetBackgroundColour(m_colBgOld
); 
 572             m_colBgOld 
= wxNullColour
; 
 575 // Workaround for GTK+1 font setting problem on some platforms 
 576 #if !defined(__WXGTK__) || defined(__WXGTK20__) 
 577         if ( m_fontOld
.Ok() ) 
 579             m_control
->SetFont(m_fontOld
); 
 580             m_fontOld 
= wxNullFont
; 
 586 void wxGridCellEditor::SetSize(const wxRect
& rect
) 
 588     wxASSERT_MSG(m_control
, wxT("The wxGridCellEditor must be created first!")); 
 590     m_control
->SetSize(rect
, wxSIZE_ALLOW_MINUS_ONE
); 
 593 void wxGridCellEditor::HandleReturn(wxKeyEvent
& event
) 
 598 bool wxGridCellEditor::IsAcceptedKey(wxKeyEvent
& event
) 
 600     bool ctrl 
= event
.ControlDown(); 
 601     bool alt  
= event
.AltDown(); 
 604     // On the Mac the Alt key is more like shift and is used for entry of 
 605     // valid characters, so check for Ctrl and Meta instead. 
 606     alt 
= event
.MetaDown(); 
 609     // Assume it's not a valid char if ctrl or alt is down, but if both are 
 610     // down then it may be because of an AltGr key combination, so let them 
 611     // through in that case. 
 612     if ((ctrl 
|| alt
) && !(ctrl 
&& alt
)) 
 619     // If it's a F-Key or other special key then it shouldn't start the 
 621     if (event
.GetKeyCode() >= WXK_START
) 
 625     // if the unicode key code is not really a unicode character (it may 
 626     // be a function key or etc., the platforms appear to always give us a 
 627     // small value in this case) then fallback to the ASCII key code but 
 628     // don't do anything for function keys or etc. 
 629     key 
= event
.GetUnicodeKey(); 
 632         key 
= event
.GetKeyCode(); 
 633         keyOk 
= (key 
<= 127); 
 636     key 
= event
.GetKeyCode(); 
 637     keyOk 
= (key 
<= 255); 
 643 void wxGridCellEditor::StartingKey(wxKeyEvent
& event
) 
 648 void wxGridCellEditor::StartingClick() 
 654 // ---------------------------------------------------------------------------- 
 655 // wxGridCellTextEditor 
 656 // ---------------------------------------------------------------------------- 
 658 wxGridCellTextEditor::wxGridCellTextEditor() 
 663 void wxGridCellTextEditor::Create(wxWindow
* parent
, 
 665                                   wxEvtHandler
* evtHandler
) 
 667     DoCreate(parent
, id
, evtHandler
); 
 670 void wxGridCellTextEditor::DoCreate(wxWindow
* parent
, 
 672                                     wxEvtHandler
* evtHandler
, 
 675     style 
|= wxTE_PROCESS_ENTER 
| wxTE_PROCESS_TAB 
| wxNO_BORDER
; 
 677     m_control 
= new wxTextCtrl(parent
, id
, wxEmptyString
, 
 678                                wxDefaultPosition
, wxDefaultSize
, 
 681     // set max length allowed in the textctrl, if the parameter was set 
 682     if ( m_maxChars 
!= 0 ) 
 684         Text()->SetMaxLength(m_maxChars
); 
 687     wxGridCellEditor::Create(parent
, id
, evtHandler
); 
 690 void wxGridCellTextEditor::PaintBackground(const wxRect
& WXUNUSED(rectCell
), 
 691                                            wxGridCellAttr 
* WXUNUSED(attr
)) 
 693     // as we fill the entire client area, 
 694     // don't do anything here to minimize flicker 
 697 void wxGridCellTextEditor::SetSize(const wxRect
& rectOrig
) 
 699     wxRect 
rect(rectOrig
); 
 701     // Make the edit control large enough to allow for internal margins 
 703     // TODO: remove this if the text ctrl sizing is improved esp. for unix 
 705 #if defined(__WXGTK__) 
 713 #elif defined(__WXMSW__) 
 727     int extra_x 
= ( rect
.x 
> 2 ) ? 2 : 1; 
 728     int extra_y 
= ( rect
.y 
> 2 ) ? 2 : 1; 
 730     #if defined(__WXMOTIF__) 
 735     rect
.SetLeft( wxMax(0, rect
.x 
- extra_x
) ); 
 736     rect
.SetTop( wxMax(0, rect
.y 
- extra_y
) ); 
 737     rect
.SetRight( rect
.GetRight() + 2 * extra_x 
); 
 738     rect
.SetBottom( rect
.GetBottom() + 2 * extra_y 
); 
 741     wxGridCellEditor::SetSize(rect
); 
 744 void wxGridCellTextEditor::BeginEdit(int row
, int col
, wxGrid
* grid
) 
 746     wxASSERT_MSG(m_control
, wxT("The wxGridCellEditor must be created first!")); 
 748     m_startValue 
= grid
->GetTable()->GetValue(row
, col
); 
 750     DoBeginEdit(m_startValue
); 
 753 void wxGridCellTextEditor::DoBeginEdit(const wxString
& startValue
) 
 755     Text()->SetValue(startValue
); 
 756     Text()->SetInsertionPointEnd(); 
 757     Text()->SetSelection(-1, -1); 
 761 bool wxGridCellTextEditor::EndEdit(int row
, int col
, wxGrid
* grid
) 
 763     wxASSERT_MSG(m_control
, wxT("The wxGridCellEditor must be created first!")); 
 765     bool changed 
= false; 
 766     wxString value 
= Text()->GetValue(); 
 767     if (value 
!= m_startValue
) 
 771         grid
->GetTable()->SetValue(row
, col
, value
); 
 773     m_startValue 
= wxEmptyString
; 
 775     // No point in setting the text of the hidden control 
 776     //Text()->SetValue(m_startValue); 
 781 void wxGridCellTextEditor::Reset() 
 783     wxASSERT_MSG(m_control
, wxT("The wxGridCellEditor must be created first!")); 
 785     DoReset(m_startValue
); 
 788 void wxGridCellTextEditor::DoReset(const wxString
& startValue
) 
 790     Text()->SetValue(startValue
); 
 791     Text()->SetInsertionPointEnd(); 
 794 bool wxGridCellTextEditor::IsAcceptedKey(wxKeyEvent
& event
) 
 796     return wxGridCellEditor::IsAcceptedKey(event
); 
 799 void wxGridCellTextEditor::StartingKey(wxKeyEvent
& event
) 
 801     // Since this is now happening in the EVT_CHAR event EmulateKeyPress is no 
 802     // longer an appropriate way to get the character into the text control. 
 803     // Do it ourselves instead.  We know that if we get this far that we have 
 804     // a valid character, so not a whole lot of testing needs to be done. 
 806     wxTextCtrl
* tc 
= Text(); 
 811     ch 
= event
.GetUnicodeKey(); 
 813         ch 
= (wxChar
)event
.GetKeyCode(); 
 815     ch 
= (wxChar
)event
.GetKeyCode(); 
 821             // delete the character at the cursor 
 822             pos 
= tc
->GetInsertionPoint(); 
 823             if (pos 
< tc
->GetLastPosition()) 
 824                 tc
->Remove(pos
, pos 
+ 1); 
 828             // delete the character before the cursor 
 829             pos 
= tc
->GetInsertionPoint(); 
 831                 tc
->Remove(pos 
- 1, pos
); 
 840 void wxGridCellTextEditor::HandleReturn( wxKeyEvent
& 
 841                                          WXUNUSED_GTK(WXUNUSED_MOTIF(event
)) ) 
 843 #if defined(__WXMOTIF__) || defined(__WXGTK__) 
 844     // wxMotif needs a little extra help... 
 845     size_t pos 
= (size_t)( Text()->GetInsertionPoint() ); 
 846     wxString 
s( Text()->GetValue() ); 
 847     s 
= s
.Left(pos
) + wxT("\n") + s
.Mid(pos
); 
 849     Text()->SetInsertionPoint( pos 
); 
 851     // the other ports can handle a Return key press 
 857 void wxGridCellTextEditor::SetParameters(const wxString
& params
) 
 867         if ( params
.ToLong(&tmp
) ) 
 869             m_maxChars 
= (size_t)tmp
; 
 873             wxLogDebug( _T("Invalid wxGridCellTextEditor parameter string '%s' ignored"), params
.c_str() ); 
 878 // return the value in the text control 
 879 wxString 
wxGridCellTextEditor::GetValue() const 
 881     return Text()->GetValue(); 
 884 // ---------------------------------------------------------------------------- 
 885 // wxGridCellNumberEditor 
 886 // ---------------------------------------------------------------------------- 
 888 wxGridCellNumberEditor::wxGridCellNumberEditor(int min
, int max
) 
 894 void wxGridCellNumberEditor::Create(wxWindow
* parent
, 
 896                                     wxEvtHandler
* evtHandler
) 
 901         // create a spin ctrl 
 902         m_control 
= new wxSpinCtrl(parent
, wxID_ANY
, wxEmptyString
, 
 903                                    wxDefaultPosition
, wxDefaultSize
, 
 907         wxGridCellEditor::Create(parent
, id
, evtHandler
); 
 912         // just a text control 
 913         wxGridCellTextEditor::Create(parent
, id
, evtHandler
); 
 916         Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC
)); 
 921 void wxGridCellNumberEditor::BeginEdit(int row
, int col
, wxGrid
* grid
) 
 923     // first get the value 
 924     wxGridTableBase 
*table 
= grid
->GetTable(); 
 925     if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) ) 
 927         m_valueOld 
= table
->GetValueAsLong(row
, col
); 
 932         wxString sValue 
= table
->GetValue(row
, col
); 
 933         if (! sValue
.ToLong(&m_valueOld
) && ! sValue
.empty()) 
 935             wxFAIL_MSG( _T("this cell doesn't have numeric value") ); 
 943         Spin()->SetValue((int)m_valueOld
); 
 949         DoBeginEdit(GetString()); 
 953 bool wxGridCellNumberEditor::EndEdit(int row
, int col
, 
 962         value 
= Spin()->GetValue(); 
 963         if ( value 
== m_valueOld 
) 
 966         text
.Printf(wxT("%ld"), value
); 
 968     else // using unconstrained input 
 969 #endif // wxUSE_SPINCTRL 
 971         const wxString 
textOld(grid
->GetCellValue(row
, col
)); 
 972         text 
= Text()->GetValue(); 
 975             if ( textOld
.empty() ) 
 978         else // non-empty text now (maybe 0) 
 980             if ( !text
.ToLong(&value
) ) 
 983             // if value == m_valueOld == 0 but old text was "" and new one is 
 984             // "0" something still did change 
 985             if ( value 
== m_valueOld 
&& (value 
|| !textOld
.empty()) ) 
 990     wxGridTableBase 
* const table 
= grid
->GetTable(); 
 991     if ( table
->CanSetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) ) 
 992         table
->SetValueAsLong(row
, col
, value
); 
 994         table
->SetValue(row
, col
, text
); 
 999 void wxGridCellNumberEditor::Reset() 
1004         Spin()->SetValue((int)m_valueOld
); 
1009         DoReset(GetString()); 
1013 bool wxGridCellNumberEditor::IsAcceptedKey(wxKeyEvent
& event
) 
1015     if ( wxGridCellEditor::IsAcceptedKey(event
) ) 
1017         int keycode 
= event
.GetKeyCode(); 
1018         if ( (keycode 
< 128) && 
1019              (wxIsdigit(keycode
) || keycode 
== '+' || keycode 
== '-')) 
1028 void wxGridCellNumberEditor::StartingKey(wxKeyEvent
& event
) 
1030     int keycode 
= event
.GetKeyCode(); 
1033         if ( wxIsdigit(keycode
) || keycode 
== '+' || keycode 
== '-') 
1035             wxGridCellTextEditor::StartingKey(event
); 
1037             // skip Skip() below 
1044         if ( wxIsdigit(keycode
) ) 
1046             wxSpinCtrl
* spin 
= (wxSpinCtrl
*)m_control
; 
1047             spin
->SetValue(keycode 
- '0'); 
1048             spin
->SetSelection(1,1); 
1057 void wxGridCellNumberEditor::SetParameters(const wxString
& params
) 
1068         if ( params
.BeforeFirst(_T(',')).ToLong(&tmp
) ) 
1072             if ( params
.AfterFirst(_T(',')).ToLong(&tmp
) ) 
1076                 // skip the error message below 
1081         wxLogDebug(_T("Invalid wxGridCellNumberEditor parameter string '%s' ignored"), params
.c_str()); 
1085 // return the value in the spin control if it is there (the text control otherwise) 
1086 wxString 
wxGridCellNumberEditor::GetValue() const 
1093         long value 
= Spin()->GetValue(); 
1094         s
.Printf(wxT("%ld"), value
); 
1099         s 
= Text()->GetValue(); 
1105 // ---------------------------------------------------------------------------- 
1106 // wxGridCellFloatEditor 
1107 // ---------------------------------------------------------------------------- 
1109 wxGridCellFloatEditor::wxGridCellFloatEditor(int width
, int precision
) 
1112     m_precision 
= precision
; 
1115 void wxGridCellFloatEditor::Create(wxWindow
* parent
, 
1117                                    wxEvtHandler
* evtHandler
) 
1119     wxGridCellTextEditor::Create(parent
, id
, evtHandler
); 
1121 #if wxUSE_VALIDATORS 
1122     Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC
)); 
1126 void wxGridCellFloatEditor::BeginEdit(int row
, int col
, wxGrid
* grid
) 
1128     // first get the value 
1129     wxGridTableBase 
* const table 
= grid
->GetTable(); 
1130     if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) ) 
1132         m_valueOld 
= table
->GetValueAsDouble(row
, col
); 
1138         const wxString value 
= table
->GetValue(row
, col
); 
1139         if ( !value
.empty() ) 
1141             if ( !value
.ToDouble(&m_valueOld
) ) 
1143                 wxFAIL_MSG( _T("this cell doesn't have float value") ); 
1149     DoBeginEdit(GetString()); 
1152 bool wxGridCellFloatEditor::EndEdit(int row
, int col
, wxGrid
* grid
) 
1154     const wxString 
text(Text()->GetValue()), 
1155                    textOld(grid
->GetCellValue(row
, col
)); 
1158     if ( !text
.empty() ) 
1160         if ( !text
.ToDouble(&value
) ) 
1163     else // new value is empty string 
1165         if ( textOld
.empty() ) 
1166             return false;           // nothing changed 
1171     // the test for empty strings ensures that we don't skip the value setting 
1172     // when "" is replaced by "0" or vice versa as "" numeric value is also 0. 
1173     if ( wxIsSameDouble(value
, m_valueOld
) && !text
.empty() && !textOld
.empty() ) 
1174         return false;           // nothing changed 
1176     wxGridTableBase 
* const table 
= grid
->GetTable(); 
1178     if ( table
->CanSetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) ) 
1179         table
->SetValueAsDouble(row
, col
, value
); 
1181         table
->SetValue(row
, col
, text
); 
1186 void wxGridCellFloatEditor::Reset() 
1188     DoReset(GetString()); 
1191 void wxGridCellFloatEditor::StartingKey(wxKeyEvent
& event
) 
1193     int keycode 
= event
.GetKeyCode(); 
1195     tmpbuf
[0] = (char) keycode
; 
1197     wxString 
strbuf(tmpbuf
, *wxConvCurrent
); 
1200     bool is_decimal_point 
= ( strbuf 
== 
1201        wxLocale::GetInfo(wxLOCALE_DECIMAL_POINT
, wxLOCALE_CAT_NUMBER
) ); 
1203     bool is_decimal_point 
= ( strbuf 
== _T(".") ); 
1206     if ( wxIsdigit(keycode
) || keycode 
== '+' || keycode 
== '-' 
1207          || is_decimal_point 
) 
1209         wxGridCellTextEditor::StartingKey(event
); 
1211         // skip Skip() below 
1218 void wxGridCellFloatEditor::SetParameters(const wxString
& params
) 
1229         if ( params
.BeforeFirst(_T(',')).ToLong(&tmp
) ) 
1233             if ( params
.AfterFirst(_T(',')).ToLong(&tmp
) ) 
1235                 m_precision 
= (int)tmp
; 
1237                 // skip the error message below 
1242         wxLogDebug(_T("Invalid wxGridCellFloatEditor parameter string '%s' ignored"), params
.c_str()); 
1246 wxString 
wxGridCellFloatEditor::GetString() const 
1249     if ( m_precision 
== -1 && m_width 
!= -1) 
1251         // default precision 
1252         fmt
.Printf(_T("%%%d.f"), m_width
); 
1254     else if ( m_precision 
!= -1 && m_width 
== -1) 
1257         fmt
.Printf(_T("%%.%df"), m_precision
); 
1259     else if ( m_precision 
!= -1 && m_width 
!= -1 ) 
1261         fmt
.Printf(_T("%%%d.%df"), m_width
, m_precision
); 
1265         // default width/precision 
1269     return wxString::Format(fmt
, m_valueOld
); 
1272 bool wxGridCellFloatEditor::IsAcceptedKey(wxKeyEvent
& event
) 
1274     if ( wxGridCellEditor::IsAcceptedKey(event
) ) 
1276         const int keycode 
= event
.GetKeyCode(); 
1277         if ( isascii(keycode
) ) 
1280             tmpbuf
[0] = (char) keycode
; 
1282             wxString 
strbuf(tmpbuf
, *wxConvCurrent
); 
1285             const wxString decimalPoint 
= 
1286                 wxLocale::GetInfo(wxLOCALE_DECIMAL_POINT
, wxLOCALE_CAT_NUMBER
); 
1288             const wxString 
decimalPoint(_T('.')); 
1291             // accept digits, 'e' as in '1e+6', also '-', '+', and '.' 
1292             if ( wxIsdigit(keycode
) || 
1293                     tolower(keycode
) == 'e' || 
1294                         keycode 
== decimalPoint 
|| 
1306 #endif // wxUSE_TEXTCTRL 
1310 // ---------------------------------------------------------------------------- 
1311 // wxGridCellBoolEditor 
1312 // ---------------------------------------------------------------------------- 
1314 // the default values for GetValue() 
1315 wxString 
wxGridCellBoolEditor::ms_stringValues
[2] = { _T(""), _T("1") }; 
1317 void wxGridCellBoolEditor::Create(wxWindow
* parent
, 
1319                                   wxEvtHandler
* evtHandler
) 
1321     m_control 
= new wxCheckBox(parent
, id
, wxEmptyString
, 
1322                                wxDefaultPosition
, wxDefaultSize
, 
1325     wxGridCellEditor::Create(parent
, id
, evtHandler
); 
1328 void wxGridCellBoolEditor::SetSize(const wxRect
& r
) 
1330     bool resize 
= false; 
1331     wxSize size 
= m_control
->GetSize(); 
1332     wxCoord minSize 
= wxMin(r
.width
, r
.height
); 
1334     // check if the checkbox is not too big/small for this cell 
1335     wxSize sizeBest 
= m_control
->GetBestSize(); 
1336     if ( !(size 
== sizeBest
) ) 
1338         // reset to default size if it had been made smaller 
1344     if ( size
.x 
>= minSize 
|| size
.y 
>= minSize 
) 
1346         // leave 1 pixel margin 
1347         size
.x 
= size
.y 
= minSize 
- 2; 
1354         m_control
->SetSize(size
); 
1357     // position it in the centre of the rectangle (TODO: support alignment?) 
1359 #if defined(__WXGTK__) || defined (__WXMOTIF__) 
1360     // the checkbox without label still has some space to the right in wxGTK, 
1361     // so shift it to the right 
1363 #elif defined(__WXMSW__) 
1364     // here too, but in other way 
1369     int hAlign 
= wxALIGN_CENTRE
; 
1370     int vAlign 
= wxALIGN_CENTRE
; 
1372         GetCellAttr()->GetAlignment(& hAlign
, & vAlign
); 
1375     if (hAlign 
== wxALIGN_LEFT
) 
1383         y 
= r
.y 
+ r
.height 
/ 2 - size
.y 
/ 2; 
1385     else if (hAlign 
== wxALIGN_RIGHT
) 
1387         x 
= r
.x 
+ r
.width 
- size
.x 
- 2; 
1388         y 
= r
.y 
+ r
.height 
/ 2 - size
.y 
/ 2; 
1390     else if (hAlign 
== wxALIGN_CENTRE
) 
1392         x 
= r
.x 
+ r
.width 
/ 2 - size
.x 
/ 2; 
1393         y 
= r
.y 
+ r
.height 
/ 2 - size
.y 
/ 2; 
1396     m_control
->Move(x
, y
); 
1399 void wxGridCellBoolEditor::Show(bool show
, wxGridCellAttr 
*attr
) 
1401     m_control
->Show(show
); 
1405         wxColour colBg 
= attr 
? attr
->GetBackgroundColour() : *wxLIGHT_GREY
; 
1406         CBox()->SetBackgroundColour(colBg
); 
1410 void wxGridCellBoolEditor::BeginEdit(int row
, int col
, wxGrid
* grid
) 
1412     wxASSERT_MSG(m_control
, 
1413                  wxT("The wxGridCellEditor must be created first!")); 
1415     if (grid
->GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
)) 
1417         m_startValue 
= grid
->GetTable()->GetValueAsBool(row
, col
); 
1421         wxString 
cellval( grid
->GetTable()->GetValue(row
, col
) ); 
1423         if ( cellval 
== ms_stringValues
[false] ) 
1424             m_startValue 
= false; 
1425         else if ( cellval 
== ms_stringValues
[true] ) 
1426             m_startValue 
= true; 
1429             // do not try to be smart here and convert it to true or false 
1430             // because we'll still overwrite it with something different and 
1431             // this risks to be very surprising for the user code, let them 
1433             wxFAIL_MSG( _T("invalid value for a cell with bool editor!") ); 
1437     CBox()->SetValue(m_startValue
); 
1441 bool wxGridCellBoolEditor::EndEdit(int row
, int col
, 
1444     wxASSERT_MSG(m_control
, 
1445                  wxT("The wxGridCellEditor must be created first!")); 
1447     bool changed 
= false; 
1448     bool value 
= CBox()->GetValue(); 
1449     if ( value 
!= m_startValue 
) 
1454         wxGridTableBase 
* const table 
= grid
->GetTable(); 
1455         if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
) ) 
1456             table
->SetValueAsBool(row
, col
, value
); 
1458             table
->SetValue(row
, col
, GetValue()); 
1464 void wxGridCellBoolEditor::Reset() 
1466     wxASSERT_MSG(m_control
, 
1467                  wxT("The wxGridCellEditor must be created first!")); 
1469     CBox()->SetValue(m_startValue
); 
1472 void wxGridCellBoolEditor::StartingClick() 
1474     CBox()->SetValue(!CBox()->GetValue()); 
1477 bool wxGridCellBoolEditor::IsAcceptedKey(wxKeyEvent
& event
) 
1479     if ( wxGridCellEditor::IsAcceptedKey(event
) ) 
1481         int keycode 
= event
.GetKeyCode(); 
1494 void wxGridCellBoolEditor::StartingKey(wxKeyEvent
& event
) 
1496     int keycode 
= event
.GetKeyCode(); 
1500             CBox()->SetValue(!CBox()->GetValue()); 
1504             CBox()->SetValue(true); 
1508             CBox()->SetValue(false); 
1513 wxString 
wxGridCellBoolEditor::GetValue() const 
1515   return ms_stringValues
[CBox()->GetValue()]; 
1519 wxGridCellBoolEditor::UseStringValues(const wxString
& valueTrue
, 
1520                                       const wxString
& valueFalse
) 
1522     ms_stringValues
[false] = valueFalse
; 
1523     ms_stringValues
[true] = valueTrue
; 
1527 wxGridCellBoolEditor::IsTrueValue(const wxString
& value
) 
1529     return value 
== ms_stringValues
[true]; 
1532 #endif // wxUSE_CHECKBOX 
1536 // ---------------------------------------------------------------------------- 
1537 // wxGridCellChoiceEditor 
1538 // ---------------------------------------------------------------------------- 
1540 wxGridCellChoiceEditor::wxGridCellChoiceEditor(const wxArrayString
& choices
, 
1542     : m_choices(choices
), 
1543       m_allowOthers(allowOthers
) { } 
1545 wxGridCellChoiceEditor::wxGridCellChoiceEditor(size_t count
, 
1546                                                const wxString choices
[], 
1548                       : m_allowOthers(allowOthers
) 
1552         m_choices
.Alloc(count
); 
1553         for ( size_t n 
= 0; n 
< count
; n
++ ) 
1555             m_choices
.Add(choices
[n
]); 
1560 wxGridCellEditor 
*wxGridCellChoiceEditor::Clone() const 
1562     wxGridCellChoiceEditor 
*editor 
= new wxGridCellChoiceEditor
; 
1563     editor
->m_allowOthers 
= m_allowOthers
; 
1564     editor
->m_choices 
= m_choices
; 
1569 void wxGridCellChoiceEditor::Create(wxWindow
* parent
, 
1571                                     wxEvtHandler
* evtHandler
) 
1573     int style 
= wxTE_PROCESS_ENTER 
| 
1577     if ( !m_allowOthers 
) 
1578         style 
|= wxCB_READONLY
; 
1579     m_control 
= new wxComboBox(parent
, id
, wxEmptyString
, 
1580                                wxDefaultPosition
, wxDefaultSize
, 
1584     wxGridCellEditor::Create(parent
, id
, evtHandler
); 
1587 void wxGridCellChoiceEditor::PaintBackground(const wxRect
& rectCell
, 
1588                                              wxGridCellAttr 
* attr
) 
1590     // as we fill the entire client area, don't do anything here to minimize 
1593     // TODO: It doesn't actually fill the client area since the height of a 
1594     // combo always defaults to the standard.  Until someone has time to 
1595     // figure out the right rectangle to paint, just do it the normal way. 
1596     wxGridCellEditor::PaintBackground(rectCell
, attr
); 
1599 void wxGridCellChoiceEditor::BeginEdit(int row
, int col
, wxGrid
* grid
) 
1601     wxASSERT_MSG(m_control
, 
1602                  wxT("The wxGridCellEditor must be created first!")); 
1604     wxGridCellEditorEvtHandler
* evtHandler 
= NULL
; 
1606         evtHandler 
= wxDynamicCast(m_control
->GetEventHandler(), wxGridCellEditorEvtHandler
); 
1608     // Don't immediately end if we get a kill focus event within BeginEdit 
1610         evtHandler
->SetInSetFocus(true); 
1612     m_startValue 
= grid
->GetTable()->GetValue(row
, col
); 
1614     Reset(); // this updates combo box to correspond to m_startValue 
1616     Combo()->SetFocus(); 
1620         // When dropping down the menu, a kill focus event 
1621         // happens after this point, so we can't reset the flag yet. 
1622 #if !defined(__WXGTK20__) 
1623         evtHandler
->SetInSetFocus(false); 
1628 bool wxGridCellChoiceEditor::EndEdit(int row
, int col
, 
1631     wxString value 
= Combo()->GetValue(); 
1632     if ( value 
== m_startValue 
) 
1635     grid
->GetTable()->SetValue(row
, col
, value
); 
1640 void wxGridCellChoiceEditor::Reset() 
1644         Combo()->SetValue(m_startValue
); 
1645         Combo()->SetInsertionPointEnd(); 
1647     else // the combobox is read-only 
1649         // find the right position, or default to the first if not found 
1650         int pos 
= Combo()->FindString(m_startValue
); 
1651         if (pos 
== wxNOT_FOUND
) 
1653         Combo()->SetSelection(pos
); 
1657 void wxGridCellChoiceEditor::SetParameters(const wxString
& params
) 
1667     wxStringTokenizer 
tk(params
, _T(',')); 
1668     while ( tk
.HasMoreTokens() ) 
1670         m_choices
.Add(tk
.GetNextToken()); 
1674 // return the value in the text control 
1675 wxString 
wxGridCellChoiceEditor::GetValue() const 
1677   return Combo()->GetValue(); 
1680 #endif // wxUSE_COMBOBOX 
1682 // ---------------------------------------------------------------------------- 
1683 // wxGridCellEditorEvtHandler 
1684 // ---------------------------------------------------------------------------- 
1686 void wxGridCellEditorEvtHandler::OnKillFocus(wxFocusEvent
& event
) 
1688     // Don't disable the cell if we're just starting to edit it 
1693     m_grid
->DisableCellEditControl(); 
1698 void wxGridCellEditorEvtHandler::OnKeyDown(wxKeyEvent
& event
) 
1700     switch ( event
.GetKeyCode() ) 
1704             m_grid
->DisableCellEditControl(); 
1708             m_grid
->GetEventHandler()->ProcessEvent( event 
); 
1712         case WXK_NUMPAD_ENTER
: 
1713             if (!m_grid
->GetEventHandler()->ProcessEvent(event
)) 
1714                 m_editor
->HandleReturn(event
); 
1723 void wxGridCellEditorEvtHandler::OnChar(wxKeyEvent
& event
) 
1725     int row 
= m_grid
->GetGridCursorRow(); 
1726     int col 
= m_grid
->GetGridCursorCol(); 
1727     wxRect rect 
= m_grid
->CellToRect( row
, col 
); 
1729     m_grid
->GetGridWindow()->GetClientSize( &cw
, &ch 
); 
1731     // if cell width is smaller than grid client area, cell is wholly visible 
1732     bool wholeCellVisible 
= (rect
.GetWidth() < cw
); 
1734     switch ( event
.GetKeyCode() ) 
1739         case WXK_NUMPAD_ENTER
: 
1744             if ( wholeCellVisible 
) 
1746                 // no special processing needed... 
1751             // do special processing for partly visible cell... 
1753             // get the widths of all cells previous to this one 
1755             for ( int i 
= 0; i 
< col
; i
++ ) 
1757                 colXPos 
+= m_grid
->GetColSize(i
); 
1760             int xUnit 
= 1, yUnit 
= 1; 
1761             m_grid
->GetScrollPixelsPerUnit(&xUnit
, &yUnit
); 
1764                 m_grid
->Scroll(colXPos 
/ xUnit 
- 1, m_grid
->GetScrollPos(wxVERTICAL
)); 
1768                 m_grid
->Scroll(colXPos 
/ xUnit
, m_grid
->GetScrollPos(wxVERTICAL
)); 
1776             if ( wholeCellVisible 
) 
1778                 // no special processing needed... 
1783             // do special processing for partly visible cell... 
1786             wxString value 
= m_grid
->GetCellValue(row
, col
); 
1787             if ( wxEmptyString 
!= value 
) 
1789                 // get width of cell CONTENTS (text) 
1791                 wxFont font 
= m_grid
->GetCellFont(row
, col
); 
1792                 m_grid
->GetTextExtent(value
, &textWidth
, &y
, NULL
, NULL
, &font
); 
1794                 // try to RIGHT align the text by scrolling 
1795                 int client_right 
= m_grid
->GetGridWindow()->GetClientSize().GetWidth(); 
1797                 // (m_grid->GetScrollLineX()*2) is a factor for not scrolling to far, 
1798                 // otherwise the last part of the cell content might be hidden below the scroll bar 
1799                 // FIXME: maybe there is a more suitable correction? 
1800                 textWidth 
-= (client_right 
- (m_grid
->GetScrollLineX() * 2)); 
1801                 if ( textWidth 
< 0 ) 
1807             // get the widths of all cells previous to this one 
1809             for ( int i 
= 0; i 
< col
; i
++ ) 
1811                 colXPos 
+= m_grid
->GetColSize(i
); 
1814             // and add the (modified) text width of the cell contents 
1815             // as we'd like to see the last part of the cell contents 
1816             colXPos 
+= textWidth
; 
1818             int xUnit 
= 1, yUnit 
= 1; 
1819             m_grid
->GetScrollPixelsPerUnit(&xUnit
, &yUnit
); 
1820             m_grid
->Scroll(colXPos 
/ xUnit 
- 1, m_grid
->GetScrollPos(wxVERTICAL
)); 
1831 // ---------------------------------------------------------------------------- 
1832 // wxGridCellWorker is an (almost) empty common base class for 
1833 // wxGridCellRenderer and wxGridCellEditor managing ref counting 
1834 // ---------------------------------------------------------------------------- 
1836 void wxGridCellWorker::SetParameters(const wxString
& WXUNUSED(params
)) 
1841 wxGridCellWorker::~wxGridCellWorker() 
1845 // ============================================================================ 
1847 // ============================================================================ 
1849 // ---------------------------------------------------------------------------- 
1850 // wxGridCellRenderer 
1851 // ---------------------------------------------------------------------------- 
1853 void wxGridCellRenderer::Draw(wxGrid
& grid
, 
1854                               wxGridCellAttr
& attr
, 
1857                               int WXUNUSED(row
), int WXUNUSED(col
), 
1860     dc
.SetBackgroundMode( wxBRUSHSTYLE_SOLID 
); 
1862     // grey out fields if the grid is disabled 
1863     if ( grid
.IsEnabled() ) 
1868             if ( grid
.HasFocus() ) 
1869                 clr 
= grid
.GetSelectionBackground(); 
1871                 clr 
= wxSystemSettings::GetColour(wxSYS_COLOUR_BTNSHADOW
); 
1872             dc
.SetBrush( wxBrush(clr
, wxBRUSHSTYLE_SOLID
) ); 
1876             dc
.SetBrush( wxBrush(attr
.GetBackgroundColour(), wxBRUSHSTYLE_SOLID
) ); 
1881         dc
.SetBrush(wxBrush(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
), wxBRUSHSTYLE_SOLID
)); 
1884     dc
.SetPen( *wxTRANSPARENT_PEN 
); 
1885     dc
.DrawRectangle(rect
); 
1888 // ---------------------------------------------------------------------------- 
1889 // wxGridCellStringRenderer 
1890 // ---------------------------------------------------------------------------- 
1892 void wxGridCellStringRenderer::SetTextColoursAndFont(const wxGrid
& grid
, 
1893                                                      const wxGridCellAttr
& attr
, 
1897     dc
.SetBackgroundMode( wxBRUSHSTYLE_TRANSPARENT 
); 
1899     // TODO some special colours for attr.IsReadOnly() case? 
1901     // different coloured text when the grid is disabled 
1902     if ( grid
.IsEnabled() ) 
1907             if ( grid
.HasFocus() ) 
1908                 clr 
= grid
.GetSelectionBackground(); 
1910                 clr 
= wxSystemSettings::GetColour(wxSYS_COLOUR_BTNSHADOW
); 
1911             dc
.SetTextBackground( clr 
); 
1912             dc
.SetTextForeground( grid
.GetSelectionForeground() ); 
1916             dc
.SetTextBackground( attr
.GetBackgroundColour() ); 
1917             dc
.SetTextForeground( attr
.GetTextColour() ); 
1922         dc
.SetTextBackground(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
)); 
1923         dc
.SetTextForeground(wxSystemSettings::GetColour(wxSYS_COLOUR_GRAYTEXT
)); 
1926     dc
.SetFont( attr
.GetFont() ); 
1929 wxSize 
wxGridCellStringRenderer::DoGetBestSize(const wxGridCellAttr
& attr
, 
1931                                                const wxString
& text
) 
1933     wxCoord x 
= 0, y 
= 0, max_x 
= 0; 
1934     dc
.SetFont(attr
.GetFont()); 
1935     wxStringTokenizer 
tk(text
, _T('\n')); 
1936     while ( tk
.HasMoreTokens() ) 
1938         dc
.GetTextExtent(tk
.GetNextToken(), &x
, &y
); 
1939         max_x 
= wxMax(max_x
, x
); 
1942     y 
*= 1 + text
.Freq(wxT('\n')); // multiply by the number of lines. 
1944     return wxSize(max_x
, y
); 
1947 wxSize 
wxGridCellStringRenderer::GetBestSize(wxGrid
& grid
, 
1948                                              wxGridCellAttr
& attr
, 
1952     return DoGetBestSize(attr
, dc
, grid
.GetCellValue(row
, col
)); 
1955 void wxGridCellStringRenderer::Draw(wxGrid
& grid
, 
1956                                     wxGridCellAttr
& attr
, 
1958                                     const wxRect
& rectCell
, 
1962     wxRect rect 
= rectCell
; 
1965     // erase only this cells background, overflow cells should have been erased 
1966     wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
); 
1969     attr
.GetAlignment(&hAlign
, &vAlign
); 
1971     int overflowCols 
= 0; 
1973     if (attr
.GetOverflow()) 
1975         int cols 
= grid
.GetNumberCols(); 
1976         int best_width 
= GetBestSize(grid
,attr
,dc
,row
,col
).GetWidth(); 
1977         int cell_rows
, cell_cols
; 
1978         attr
.GetSize( &cell_rows
, &cell_cols 
); // shouldn't get here if <= 0 
1979         if ((best_width 
> rectCell
.width
) && (col 
< cols
) && grid
.GetTable()) 
1981             int i
, c_cols
, c_rows
; 
1982             for (i 
= col
+cell_cols
; i 
< cols
; i
++) 
1984                 bool is_empty 
= true; 
1985                 for (int j
=row
; j 
< row 
+ cell_rows
; j
++) 
1987                     // check w/ anchor cell for multicell block 
1988                     grid
.GetCellSize(j
, i
, &c_rows
, &c_cols
); 
1991                     if (!grid
.GetTable()->IsEmptyCell(j 
+ c_rows
, i
)) 
2000                     rect
.width 
+= grid
.GetColSize(i
); 
2008                 if (rect
.width 
>= best_width
) 
2012             overflowCols 
= i 
- col 
- cell_cols 
+ 1; 
2013             if (overflowCols 
>= cols
) 
2014                 overflowCols 
= cols 
- 1; 
2017         if (overflowCols 
> 0) // redraw overflow cells w/ proper hilight 
2019             hAlign 
= wxALIGN_LEFT
; // if oveflowed then it's left aligned 
2021             clip
.x 
+= rectCell
.width
; 
2022             // draw each overflow cell individually 
2023             int col_end 
= col 
+ cell_cols 
+ overflowCols
; 
2024             if (col_end 
>= grid
.GetNumberCols()) 
2025                 col_end 
= grid
.GetNumberCols() - 1; 
2026             for (int i 
= col 
+ cell_cols
; i 
<= col_end
; i
++) 
2028                 clip
.width 
= grid
.GetColSize(i
) - 1; 
2029                 dc
.DestroyClippingRegion(); 
2030                 dc
.SetClippingRegion(clip
); 
2032                 SetTextColoursAndFont(grid
, attr
, dc
, 
2033                         grid
.IsInSelection(row
,i
)); 
2035                 grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
), 
2036                         rect
, hAlign
, vAlign
); 
2037                 clip
.x 
+= grid
.GetColSize(i
) - 1; 
2043             dc
.DestroyClippingRegion(); 
2047     // now we only have to draw the text 
2048     SetTextColoursAndFont(grid
, attr
, dc
, isSelected
); 
2050     grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
), 
2051                            rect
, hAlign
, vAlign
); 
2054 // ---------------------------------------------------------------------------- 
2055 // wxGridCellNumberRenderer 
2056 // ---------------------------------------------------------------------------- 
2058 wxString 
wxGridCellNumberRenderer::GetString(const wxGrid
& grid
, int row
, int col
) 
2060     wxGridTableBase 
*table 
= grid
.GetTable(); 
2062     if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) ) 
2064         text
.Printf(_T("%ld"), table
->GetValueAsLong(row
, col
)); 
2068         text 
= table
->GetValue(row
, col
); 
2074 void wxGridCellNumberRenderer::Draw(wxGrid
& grid
, 
2075                                     wxGridCellAttr
& attr
, 
2077                                     const wxRect
& rectCell
, 
2081     wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
); 
2083     SetTextColoursAndFont(grid
, attr
, dc
, isSelected
); 
2085     // draw the text right aligned by default 
2087     attr
.GetAlignment(&hAlign
, &vAlign
); 
2088     hAlign 
= wxALIGN_RIGHT
; 
2090     wxRect rect 
= rectCell
; 
2093     grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
); 
2096 wxSize 
wxGridCellNumberRenderer::GetBestSize(wxGrid
& grid
, 
2097                                              wxGridCellAttr
& attr
, 
2101     return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
)); 
2104 // ---------------------------------------------------------------------------- 
2105 // wxGridCellFloatRenderer 
2106 // ---------------------------------------------------------------------------- 
2108 wxGridCellFloatRenderer::wxGridCellFloatRenderer(int width
, int precision
) 
2111     SetPrecision(precision
); 
2114 wxGridCellRenderer 
*wxGridCellFloatRenderer::Clone() const 
2116     wxGridCellFloatRenderer 
*renderer 
= new wxGridCellFloatRenderer
; 
2117     renderer
->m_width 
= m_width
; 
2118     renderer
->m_precision 
= m_precision
; 
2119     renderer
->m_format 
= m_format
; 
2124 wxString 
wxGridCellFloatRenderer::GetString(const wxGrid
& grid
, int row
, int col
) 
2126     wxGridTableBase 
*table 
= grid
.GetTable(); 
2131     if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) ) 
2133         val 
= table
->GetValueAsDouble(row
, col
); 
2138         text 
= table
->GetValue(row
, col
); 
2139         hasDouble 
= text
.ToDouble(&val
); 
2146             if ( m_width 
== -1 ) 
2148                 if ( m_precision 
== -1 ) 
2150                     // default width/precision 
2151                     m_format 
= _T("%f"); 
2155                     m_format
.Printf(_T("%%.%df"), m_precision
); 
2158             else if ( m_precision 
== -1 ) 
2160                 // default precision 
2161                 m_format
.Printf(_T("%%%d.f"), m_width
); 
2165                 m_format
.Printf(_T("%%%d.%df"), m_width
, m_precision
); 
2169         text
.Printf(m_format
, val
); 
2172     //else: text already contains the string 
2177 void wxGridCellFloatRenderer::Draw(wxGrid
& grid
, 
2178                                    wxGridCellAttr
& attr
, 
2180                                    const wxRect
& rectCell
, 
2184     wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
); 
2186     SetTextColoursAndFont(grid
, attr
, dc
, isSelected
); 
2188     // draw the text right aligned by default 
2190     attr
.GetAlignment(&hAlign
, &vAlign
); 
2191     hAlign 
= wxALIGN_RIGHT
; 
2193     wxRect rect 
= rectCell
; 
2196     grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
); 
2199 wxSize 
wxGridCellFloatRenderer::GetBestSize(wxGrid
& grid
, 
2200                                             wxGridCellAttr
& attr
, 
2204     return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
)); 
2207 void wxGridCellFloatRenderer::SetParameters(const wxString
& params
) 
2211         // reset to defaults 
2217         wxString tmp 
= params
.BeforeFirst(_T(',')); 
2221             if ( tmp
.ToLong(&width
) ) 
2223                 SetWidth((int)width
); 
2227                 wxLogDebug(_T("Invalid wxGridCellFloatRenderer width parameter string '%s ignored"), params
.c_str()); 
2231         tmp 
= params
.AfterFirst(_T(',')); 
2235             if ( tmp
.ToLong(&precision
) ) 
2237                 SetPrecision((int)precision
); 
2241                 wxLogDebug(_T("Invalid wxGridCellFloatRenderer precision parameter string '%s ignored"), params
.c_str()); 
2247 // ---------------------------------------------------------------------------- 
2248 // wxGridCellBoolRenderer 
2249 // ---------------------------------------------------------------------------- 
2251 wxSize 
wxGridCellBoolRenderer::ms_sizeCheckMark
; 
2253 // FIXME these checkbox size calculations are really ugly... 
2255 // between checkmark and box 
2256 static const wxCoord wxGRID_CHECKMARK_MARGIN 
= 2; 
2258 wxSize 
wxGridCellBoolRenderer::GetBestSize(wxGrid
& grid
, 
2259                                            wxGridCellAttr
& WXUNUSED(attr
), 
2264     // compute it only once (no locks for MT safeness in GUI thread...) 
2265     if ( !ms_sizeCheckMark
.x 
) 
2267         // get checkbox size 
2268         wxCheckBox 
*checkbox 
= new wxCheckBox(&grid
, wxID_ANY
, wxEmptyString
); 
2269         wxSize size 
= checkbox
->GetBestSize(); 
2270         wxCoord checkSize 
= size
.y 
+ 2 * wxGRID_CHECKMARK_MARGIN
; 
2272 #if defined(__WXMOTIF__) 
2273         checkSize 
-= size
.y 
/ 2; 
2278         ms_sizeCheckMark
.x 
= ms_sizeCheckMark
.y 
= checkSize
; 
2281     return ms_sizeCheckMark
; 
2284 void wxGridCellBoolRenderer::Draw(wxGrid
& grid
, 
2285                                   wxGridCellAttr
& attr
, 
2291     wxGridCellRenderer::Draw(grid
, attr
, dc
, rect
, row
, col
, isSelected
); 
2293     // draw a check mark in the centre (ignoring alignment - TODO) 
2294     wxSize size 
= GetBestSize(grid
, attr
, dc
, row
, col
); 
2296     // don't draw outside the cell 
2297     wxCoord minSize 
= wxMin(rect
.width
, rect
.height
); 
2298     if ( size
.x 
>= minSize 
|| size
.y 
>= minSize 
) 
2300         // and even leave (at least) 1 pixel margin 
2301         size
.x 
= size
.y 
= minSize
; 
2304     // draw a border around checkmark 
2306     attr
.GetAlignment(&hAlign
, &vAlign
); 
2309     if (hAlign 
== wxALIGN_CENTRE
) 
2311         rectBorder
.x 
= rect
.x 
+ rect
.width 
/ 2 - size
.x 
/ 2; 
2312         rectBorder
.y 
= rect
.y 
+ rect
.height 
/ 2 - size
.y 
/ 2; 
2313         rectBorder
.width 
= size
.x
; 
2314         rectBorder
.height 
= size
.y
; 
2316     else if (hAlign 
== wxALIGN_LEFT
) 
2318         rectBorder
.x 
= rect
.x 
+ 2; 
2319         rectBorder
.y 
= rect
.y 
+ rect
.height 
/ 2 - size
.y 
/ 2; 
2320         rectBorder
.width 
= size
.x
; 
2321         rectBorder
.height 
= size
.y
; 
2323     else if (hAlign 
== wxALIGN_RIGHT
) 
2325         rectBorder
.x 
= rect
.x 
+ rect
.width 
- size
.x 
- 2; 
2326         rectBorder
.y 
= rect
.y 
+ rect
.height 
/ 2 - size
.y 
/ 2; 
2327         rectBorder
.width 
= size
.x
; 
2328         rectBorder
.height 
= size
.y
; 
2332     if ( grid
.GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
) ) 
2334         value 
= grid
.GetTable()->GetValueAsBool(row
, col
); 
2338         wxString 
cellval( grid
.GetTable()->GetValue(row
, col
) ); 
2339         value 
= wxGridCellBoolEditor::IsTrueValue(cellval
); 
2344         flags 
|= wxCONTROL_CHECKED
; 
2346     wxRendererNative::Get().DrawCheckBox( &grid
, dc
, rectBorder
, flags 
); 
2349 // ---------------------------------------------------------------------------- 
2351 // ---------------------------------------------------------------------------- 
2353 void wxGridCellAttr::Init(wxGridCellAttr 
*attrDefault
) 
2357     m_isReadOnly 
= Unset
; 
2362     m_attrkind 
= wxGridCellAttr::Cell
; 
2364     m_sizeRows 
= m_sizeCols 
= 1; 
2365     m_overflow 
= UnsetOverflow
; 
2367     SetDefAttr(attrDefault
); 
2370 wxGridCellAttr 
*wxGridCellAttr::Clone() const 
2372     wxGridCellAttr 
*attr 
= new wxGridCellAttr(m_defGridAttr
); 
2374     if ( HasTextColour() ) 
2375         attr
->SetTextColour(GetTextColour()); 
2376     if ( HasBackgroundColour() ) 
2377         attr
->SetBackgroundColour(GetBackgroundColour()); 
2379         attr
->SetFont(GetFont()); 
2380     if ( HasAlignment() ) 
2381         attr
->SetAlignment(m_hAlign
, m_vAlign
); 
2383     attr
->SetSize( m_sizeRows
, m_sizeCols 
); 
2387         attr
->SetRenderer(m_renderer
); 
2388         m_renderer
->IncRef(); 
2392         attr
->SetEditor(m_editor
); 
2397         attr
->SetReadOnly(); 
2399     attr
->SetOverflow( m_overflow 
== Overflow 
); 
2400     attr
->SetKind( m_attrkind 
); 
2405 void wxGridCellAttr::MergeWith(wxGridCellAttr 
*mergefrom
) 
2407     if ( !HasTextColour() && mergefrom
->HasTextColour() ) 
2408         SetTextColour(mergefrom
->GetTextColour()); 
2409     if ( !HasBackgroundColour() && mergefrom
->HasBackgroundColour() ) 
2410         SetBackgroundColour(mergefrom
->GetBackgroundColour()); 
2411     if ( !HasFont() && mergefrom
->HasFont() ) 
2412         SetFont(mergefrom
->GetFont()); 
2413     if ( !HasAlignment() && mergefrom
->HasAlignment() ) 
2416         mergefrom
->GetAlignment( &hAlign
, &vAlign
); 
2417         SetAlignment(hAlign
, vAlign
); 
2419     if ( !HasSize() && mergefrom
->HasSize() ) 
2420         mergefrom
->GetSize( &m_sizeRows
, &m_sizeCols 
); 
2422     // Directly access member functions as GetRender/Editor don't just return 
2423     // m_renderer/m_editor 
2425     // Maybe add support for merge of Render and Editor? 
2426     if (!HasRenderer() && mergefrom
->HasRenderer() ) 
2428         m_renderer 
= mergefrom
->m_renderer
; 
2429         m_renderer
->IncRef(); 
2431     if ( !HasEditor() && mergefrom
->HasEditor() ) 
2433         m_editor 
=  mergefrom
->m_editor
; 
2436     if ( !HasReadWriteMode() && mergefrom
->HasReadWriteMode() ) 
2437         SetReadOnly(mergefrom
->IsReadOnly()); 
2439     if (!HasOverflowMode() && mergefrom
->HasOverflowMode() ) 
2440         SetOverflow(mergefrom
->GetOverflow()); 
2442     SetDefAttr(mergefrom
->m_defGridAttr
); 
2445 void wxGridCellAttr::SetSize(int num_rows
, int num_cols
) 
2447     // The size of a cell is normally 1,1 
2449     // If this cell is larger (2,2) then this is the top left cell 
2450     // the other cells that will be covered (lower right cells) must be 
2451     // set to negative or zero values such that 
2452     // row + num_rows of the covered cell points to the larger cell (this cell) 
2453     // same goes for the col + num_cols. 
2455     // Size of 0,0 is NOT valid, neither is <=0 and any positive value 
2457     wxASSERT_MSG( (!((num_rows 
> 0) && (num_cols 
<= 0)) || 
2458                   !((num_rows 
<= 0) && (num_cols 
> 0)) || 
2459                   !((num_rows 
== 0) && (num_cols 
== 0))), 
2460                   wxT("wxGridCellAttr::SetSize only takes two postive values or negative/zero values")); 
2462     m_sizeRows 
= num_rows
; 
2463     m_sizeCols 
= num_cols
; 
2466 const wxColour
& wxGridCellAttr::GetTextColour() const 
2468     if (HasTextColour()) 
2472     else if (m_defGridAttr 
&& m_defGridAttr 
!= this) 
2474         return m_defGridAttr
->GetTextColour(); 
2478         wxFAIL_MSG(wxT("Missing default cell attribute")); 
2479         return wxNullColour
; 
2483 const wxColour
& wxGridCellAttr::GetBackgroundColour() const 
2485     if (HasBackgroundColour()) 
2489     else if (m_defGridAttr 
&& m_defGridAttr 
!= this) 
2491         return m_defGridAttr
->GetBackgroundColour(); 
2495         wxFAIL_MSG(wxT("Missing default cell attribute")); 
2496         return wxNullColour
; 
2500 const wxFont
& wxGridCellAttr::GetFont() const 
2506     else if (m_defGridAttr 
&& m_defGridAttr 
!= this) 
2508         return m_defGridAttr
->GetFont(); 
2512         wxFAIL_MSG(wxT("Missing default cell attribute")); 
2517 void wxGridCellAttr::GetAlignment(int *hAlign
, int *vAlign
) const 
2526     else if (m_defGridAttr 
&& m_defGridAttr 
!= this) 
2528         m_defGridAttr
->GetAlignment(hAlign
, vAlign
); 
2532         wxFAIL_MSG(wxT("Missing default cell attribute")); 
2536 void wxGridCellAttr::GetSize( int *num_rows
, int *num_cols 
) const 
2539         *num_rows 
= m_sizeRows
; 
2541         *num_cols 
= m_sizeCols
; 
2544 // GetRenderer and GetEditor use a slightly different decision path about 
2545 // which attribute to use.  If a non-default attr object has one then it is 
2546 // used, otherwise the default editor or renderer is fetched from the grid and 
2547 // used.  It should be the default for the data type of the cell.  If it is 
2548 // NULL (because the table has a type that the grid does not have in its 
2549 // registry), then the grid's default editor or renderer is used. 
2551 wxGridCellRenderer
* wxGridCellAttr::GetRenderer(const wxGrid
* grid
, int row
, int col
) const 
2553     wxGridCellRenderer 
*renderer 
= NULL
; 
2555     if ( m_renderer 
&& this != m_defGridAttr 
) 
2557         // use the cells renderer if it has one 
2558         renderer 
= m_renderer
; 
2561     else // no non-default cell renderer 
2563         // get default renderer for the data type 
2566             // GetDefaultRendererForCell() will do IncRef() for us 
2567             renderer 
= grid
->GetDefaultRendererForCell(row
, col
); 
2570         if ( renderer 
== NULL 
) 
2572             if ( (m_defGridAttr 
!= NULL
) && (m_defGridAttr 
!= this) ) 
2574                 // if we still don't have one then use the grid default 
2575                 // (no need for IncRef() here neither) 
2576                 renderer 
= m_defGridAttr
->GetRenderer(NULL
, 0, 0); 
2578             else // default grid attr 
2580                 // use m_renderer which we had decided not to use initially 
2581                 renderer 
= m_renderer
; 
2588     // we're supposed to always find something 
2589     wxASSERT_MSG(renderer
, wxT("Missing default cell renderer")); 
2594 // same as above, except for s/renderer/editor/g 
2595 wxGridCellEditor
* wxGridCellAttr::GetEditor(const wxGrid
* grid
, int row
, int col
) const 
2597     wxGridCellEditor 
*editor 
= NULL
; 
2599     if ( m_editor 
&& this != m_defGridAttr 
) 
2601         // use the cells editor if it has one 
2605     else // no non default cell editor 
2607         // get default editor for the data type 
2610             // GetDefaultEditorForCell() will do IncRef() for us 
2611             editor 
= grid
->GetDefaultEditorForCell(row
, col
); 
2614         if ( editor 
== NULL 
) 
2616             if ( (m_defGridAttr 
!= NULL
) && (m_defGridAttr 
!= this) ) 
2618                 // if we still don't have one then use the grid default 
2619                 // (no need for IncRef() here neither) 
2620                 editor 
= m_defGridAttr
->GetEditor(NULL
, 0, 0); 
2622             else // default grid attr 
2624                 // use m_editor which we had decided not to use initially 
2632     // we're supposed to always find something 
2633     wxASSERT_MSG(editor
, wxT("Missing default cell editor")); 
2638 // ---------------------------------------------------------------------------- 
2639 // wxGridCellAttrData 
2640 // ---------------------------------------------------------------------------- 
2642 void wxGridCellAttrData::SetAttr(wxGridCellAttr 
*attr
, int row
, int col
) 
2644     // Note: contrary to wxGridRowOrColAttrData::SetAttr, we must not 
2645     //       touch attribute's reference counting explicitly, since this 
2646     //       is managed by class wxGridCellWithAttr 
2647     int n 
= FindIndex(row
, col
); 
2648     if ( n 
== wxNOT_FOUND 
) 
2652             // add the attribute 
2653             m_attrs
.Add(new wxGridCellWithAttr(row
, col
, attr
)); 
2655         //else: nothing to do 
2657     else // we already have an attribute for this cell 
2661             // change the attribute 
2662             m_attrs
[(size_t)n
].ChangeAttr(attr
); 
2666             // remove this attribute 
2667             m_attrs
.RemoveAt((size_t)n
); 
2672 wxGridCellAttr 
*wxGridCellAttrData::GetAttr(int row
, int col
) const 
2674     wxGridCellAttr 
*attr 
= (wxGridCellAttr 
*)NULL
; 
2676     int n 
= FindIndex(row
, col
); 
2677     if ( n 
!= wxNOT_FOUND 
) 
2679         attr 
= m_attrs
[(size_t)n
].attr
; 
2686 void wxGridCellAttrData::UpdateAttrRows( size_t pos
, int numRows 
) 
2688     size_t count 
= m_attrs
.GetCount(); 
2689     for ( size_t n 
= 0; n 
< count
; n
++ ) 
2691         wxGridCellCoords
& coords 
= m_attrs
[n
].coords
; 
2692         wxCoord row 
= coords
.GetRow(); 
2693         if ((size_t)row 
>= pos
) 
2697                 // If rows inserted, include row counter where necessary 
2698                 coords
.SetRow(row 
+ numRows
); 
2700             else if (numRows 
< 0) 
2702                 // If rows deleted ... 
2703                 if ((size_t)row 
>= pos 
- numRows
) 
2705                     // ...either decrement row counter (if row still exists)... 
2706                     coords
.SetRow(row 
+ numRows
); 
2710                     // ...or remove the attribute 
2711                     m_attrs
.RemoveAt(n
); 
2720 void wxGridCellAttrData::UpdateAttrCols( size_t pos
, int numCols 
) 
2722     size_t count 
= m_attrs
.GetCount(); 
2723     for ( size_t n 
= 0; n 
< count
; n
++ ) 
2725         wxGridCellCoords
& coords 
= m_attrs
[n
].coords
; 
2726         wxCoord col 
= coords
.GetCol(); 
2727         if ( (size_t)col 
>= pos 
) 
2731                 // If rows inserted, include row counter where necessary 
2732                 coords
.SetCol(col 
+ numCols
); 
2734             else if (numCols 
< 0) 
2736                 // If rows deleted ... 
2737                 if ((size_t)col 
>= pos 
- numCols
) 
2739                     // ...either decrement row counter (if row still exists)... 
2740                     coords
.SetCol(col 
+ numCols
); 
2744                     // ...or remove the attribute 
2745                     m_attrs
.RemoveAt(n
); 
2754 int wxGridCellAttrData::FindIndex(int row
, int col
) const 
2756     size_t count 
= m_attrs
.GetCount(); 
2757     for ( size_t n 
= 0; n 
< count
; n
++ ) 
2759         const wxGridCellCoords
& coords 
= m_attrs
[n
].coords
; 
2760         if ( (coords
.GetRow() == row
) && (coords
.GetCol() == col
) ) 
2769 // ---------------------------------------------------------------------------- 
2770 // wxGridRowOrColAttrData 
2771 // ---------------------------------------------------------------------------- 
2773 wxGridRowOrColAttrData::~wxGridRowOrColAttrData() 
2775     size_t count 
= m_attrs
.GetCount(); 
2776     for ( size_t n 
= 0; n 
< count
; n
++ ) 
2778         m_attrs
[n
]->DecRef(); 
2782 wxGridCellAttr 
*wxGridRowOrColAttrData::GetAttr(int rowOrCol
) const 
2784     wxGridCellAttr 
*attr 
= (wxGridCellAttr 
*)NULL
; 
2786     int n 
= m_rowsOrCols
.Index(rowOrCol
); 
2787     if ( n 
!= wxNOT_FOUND 
) 
2789         attr 
= m_attrs
[(size_t)n
]; 
2796 void wxGridRowOrColAttrData::SetAttr(wxGridCellAttr 
*attr
, int rowOrCol
) 
2798     int i 
= m_rowsOrCols
.Index(rowOrCol
); 
2799     if ( i 
== wxNOT_FOUND 
) 
2803             // add the attribute - no need to do anything to reference count 
2804             //                     since we take ownership of the attribute. 
2805             m_rowsOrCols
.Add(rowOrCol
); 
2808         // nothing to remove 
2812         size_t n 
= (size_t)i
; 
2813         if ( m_attrs
[n
] == attr 
) 
2818             // change the attribute, handling reference count manually, 
2819             //                       taking ownership of the new attribute. 
2820             m_attrs
[n
]->DecRef(); 
2825             // remove this attribute, handling reference count manually 
2826             m_attrs
[n
]->DecRef(); 
2827             m_rowsOrCols
.RemoveAt(n
); 
2828             m_attrs
.RemoveAt(n
); 
2833 void wxGridRowOrColAttrData::UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols 
) 
2835     size_t count 
= m_attrs
.GetCount(); 
2836     for ( size_t n 
= 0; n 
< count
; n
++ ) 
2838         int & rowOrCol 
= m_rowsOrCols
[n
]; 
2839         if ( (size_t)rowOrCol 
>= pos 
) 
2841             if ( numRowsOrCols 
> 0 ) 
2843                 // If rows inserted, include row counter where necessary 
2844                 rowOrCol 
+= numRowsOrCols
; 
2846             else if ( numRowsOrCols 
< 0) 
2848                 // If rows deleted, either decrement row counter (if row still exists) 
2849                 if ((size_t)rowOrCol 
>= pos 
- numRowsOrCols
) 
2850                     rowOrCol 
+= numRowsOrCols
; 
2853                     m_rowsOrCols
.RemoveAt(n
); 
2854                     m_attrs
[n
]->DecRef(); 
2855                     m_attrs
.RemoveAt(n
); 
2864 // ---------------------------------------------------------------------------- 
2865 // wxGridCellAttrProvider 
2866 // ---------------------------------------------------------------------------- 
2868 wxGridCellAttrProvider::wxGridCellAttrProvider() 
2870     m_data 
= (wxGridCellAttrProviderData 
*)NULL
; 
2873 wxGridCellAttrProvider::~wxGridCellAttrProvider() 
2878 void wxGridCellAttrProvider::InitData() 
2880     m_data 
= new wxGridCellAttrProviderData
; 
2883 wxGridCellAttr 
*wxGridCellAttrProvider::GetAttr(int row
, int col
, 
2884                                                 wxGridCellAttr::wxAttrKind  kind 
) const 
2886     wxGridCellAttr 
*attr 
= (wxGridCellAttr 
*)NULL
; 
2891             case (wxGridCellAttr::Any
): 
2892                 // Get cached merge attributes. 
2893                 // Currently not used as no cache implemented as not mutable 
2894                 // attr = m_data->m_mergeAttr.GetAttr(row, col); 
2897                     // Basically implement old version. 
2898                     // Also check merge cache, so we don't have to re-merge every time.. 
2899                     wxGridCellAttr 
*attrcell 
= m_data
->m_cellAttrs
.GetAttr(row
, col
); 
2900                     wxGridCellAttr 
*attrrow 
= m_data
->m_rowAttrs
.GetAttr(row
); 
2901                     wxGridCellAttr 
*attrcol 
= m_data
->m_colAttrs
.GetAttr(col
); 
2903                     if ((attrcell 
!= attrrow
) && (attrrow 
!= attrcol
) && (attrcell 
!= attrcol
)) 
2905                         // Two or more are non NULL 
2906                         attr 
= new wxGridCellAttr
; 
2907                         attr
->SetKind(wxGridCellAttr::Merged
); 
2909                         // Order is important.. 
2912                             attr
->MergeWith(attrcell
); 
2917                             attr
->MergeWith(attrcol
); 
2922                             attr
->MergeWith(attrrow
); 
2926                         // store merge attr if cache implemented 
2928                         //m_data->m_mergeAttr.SetAttr(attr, row, col); 
2932                         // one or none is non null return it or null. 
2951             case (wxGridCellAttr::Cell
): 
2952                 attr 
= m_data
->m_cellAttrs
.GetAttr(row
, col
); 
2955             case (wxGridCellAttr::Col
): 
2956                 attr 
= m_data
->m_colAttrs
.GetAttr(col
); 
2959             case (wxGridCellAttr::Row
): 
2960                 attr 
= m_data
->m_rowAttrs
.GetAttr(row
); 
2965                 // (wxGridCellAttr::Default): 
2966                 // (wxGridCellAttr::Merged): 
2974 void wxGridCellAttrProvider::SetAttr(wxGridCellAttr 
*attr
, 
2980     m_data
->m_cellAttrs
.SetAttr(attr
, row
, col
); 
2983 void wxGridCellAttrProvider::SetRowAttr(wxGridCellAttr 
*attr
, int row
) 
2988     m_data
->m_rowAttrs
.SetAttr(attr
, row
); 
2991 void wxGridCellAttrProvider::SetColAttr(wxGridCellAttr 
*attr
, int col
) 
2996     m_data
->m_colAttrs
.SetAttr(attr
, col
); 
2999 void wxGridCellAttrProvider::UpdateAttrRows( size_t pos
, int numRows 
) 
3003         m_data
->m_cellAttrs
.UpdateAttrRows( pos
, numRows 
); 
3005         m_data
->m_rowAttrs
.UpdateAttrRowsOrCols( pos
, numRows 
); 
3009 void wxGridCellAttrProvider::UpdateAttrCols( size_t pos
, int numCols 
) 
3013         m_data
->m_cellAttrs
.UpdateAttrCols( pos
, numCols 
); 
3015         m_data
->m_colAttrs
.UpdateAttrRowsOrCols( pos
, numCols 
); 
3019 // ---------------------------------------------------------------------------- 
3020 // wxGridTypeRegistry 
3021 // ---------------------------------------------------------------------------- 
3023 wxGridTypeRegistry::~wxGridTypeRegistry() 
3025     size_t count 
= m_typeinfo
.GetCount(); 
3026     for ( size_t i 
= 0; i 
< count
; i
++ ) 
3027         delete m_typeinfo
[i
]; 
3030 void wxGridTypeRegistry::RegisterDataType(const wxString
& typeName
, 
3031                                           wxGridCellRenderer
* renderer
, 
3032                                           wxGridCellEditor
* editor
) 
3034     wxGridDataTypeInfo
* info 
= new wxGridDataTypeInfo(typeName
, renderer
, editor
); 
3036     // is it already registered? 
3037     int loc 
= FindRegisteredDataType(typeName
); 
3038     if ( loc 
!= wxNOT_FOUND 
) 
3040         delete m_typeinfo
[loc
]; 
3041         m_typeinfo
[loc
] = info
; 
3045         m_typeinfo
.Add(info
); 
3049 int wxGridTypeRegistry::FindRegisteredDataType(const wxString
& typeName
) 
3051     size_t count 
= m_typeinfo
.GetCount(); 
3052     for ( size_t i 
= 0; i 
< count
; i
++ ) 
3054         if ( typeName 
== m_typeinfo
[i
]->m_typeName 
) 
3063 int wxGridTypeRegistry::FindDataType(const wxString
& typeName
) 
3065     int index 
= FindRegisteredDataType(typeName
); 
3066     if ( index 
== wxNOT_FOUND 
) 
3068         // check whether this is one of the standard ones, in which case 
3069         // register it "on the fly" 
3071         if ( typeName 
== wxGRID_VALUE_STRING 
) 
3073             RegisterDataType(wxGRID_VALUE_STRING
, 
3074                              new wxGridCellStringRenderer
, 
3075                              new wxGridCellTextEditor
); 
3078 #endif // wxUSE_TEXTCTRL 
3080         if ( typeName 
== wxGRID_VALUE_BOOL 
) 
3082             RegisterDataType(wxGRID_VALUE_BOOL
, 
3083                              new wxGridCellBoolRenderer
, 
3084                              new wxGridCellBoolEditor
); 
3087 #endif // wxUSE_CHECKBOX 
3089         if ( typeName 
== wxGRID_VALUE_NUMBER 
) 
3091             RegisterDataType(wxGRID_VALUE_NUMBER
, 
3092                              new wxGridCellNumberRenderer
, 
3093                              new wxGridCellNumberEditor
); 
3095         else if ( typeName 
== wxGRID_VALUE_FLOAT 
) 
3097             RegisterDataType(wxGRID_VALUE_FLOAT
, 
3098                              new wxGridCellFloatRenderer
, 
3099                              new wxGridCellFloatEditor
); 
3102 #endif // wxUSE_TEXTCTRL 
3104         if ( typeName 
== wxGRID_VALUE_CHOICE 
) 
3106             RegisterDataType(wxGRID_VALUE_CHOICE
, 
3107                              new wxGridCellStringRenderer
, 
3108                              new wxGridCellChoiceEditor
); 
3111 #endif // wxUSE_COMBOBOX 
3116         // we get here only if just added the entry for this type, so return 
3118         index 
= m_typeinfo
.GetCount() - 1; 
3124 int wxGridTypeRegistry::FindOrCloneDataType(const wxString
& typeName
) 
3126     int index 
= FindDataType(typeName
); 
3127     if ( index 
== wxNOT_FOUND 
) 
3129         // the first part of the typename is the "real" type, anything after ':' 
3130         // are the parameters for the renderer 
3131         index 
= FindDataType(typeName
.BeforeFirst(_T(':'))); 
3132         if ( index 
== wxNOT_FOUND 
) 
3137         wxGridCellRenderer 
*renderer 
= GetRenderer(index
); 
3138         wxGridCellRenderer 
*rendererOld 
= renderer
; 
3139         renderer 
= renderer
->Clone(); 
3140         rendererOld
->DecRef(); 
3142         wxGridCellEditor 
*editor 
= GetEditor(index
); 
3143         wxGridCellEditor 
*editorOld 
= editor
; 
3144         editor 
= editor
->Clone(); 
3145         editorOld
->DecRef(); 
3147         // do it even if there are no parameters to reset them to defaults 
3148         wxString params 
= typeName
.AfterFirst(_T(':')); 
3149         renderer
->SetParameters(params
); 
3150         editor
->SetParameters(params
); 
3152         // register the new typename 
3153         RegisterDataType(typeName
, renderer
, editor
); 
3155         // we just registered it, it's the last one 
3156         index 
= m_typeinfo
.GetCount() - 1; 
3162 wxGridCellRenderer
* wxGridTypeRegistry::GetRenderer(int index
) 
3164     wxGridCellRenderer
* renderer 
= m_typeinfo
[index
]->m_renderer
; 
3171 wxGridCellEditor
* wxGridTypeRegistry::GetEditor(int index
) 
3173     wxGridCellEditor
* editor 
= m_typeinfo
[index
]->m_editor
; 
3180 // ---------------------------------------------------------------------------- 
3182 // ---------------------------------------------------------------------------- 
3184 IMPLEMENT_ABSTRACT_CLASS( wxGridTableBase
, wxObject 
) 
3186 wxGridTableBase::wxGridTableBase() 
3188     m_view 
= (wxGrid 
*) NULL
; 
3189     m_attrProvider 
= (wxGridCellAttrProvider 
*) NULL
; 
3192 wxGridTableBase::~wxGridTableBase() 
3194     delete m_attrProvider
; 
3197 void wxGridTableBase::SetAttrProvider(wxGridCellAttrProvider 
*attrProvider
) 
3199     delete m_attrProvider
; 
3200     m_attrProvider 
= attrProvider
; 
3203 bool wxGridTableBase::CanHaveAttributes() 
3205     if ( ! GetAttrProvider() ) 
3207         // use the default attr provider by default 
3208         SetAttrProvider(new wxGridCellAttrProvider
); 
3214 wxGridCellAttr 
*wxGridTableBase::GetAttr(int row
, int col
, wxGridCellAttr::wxAttrKind  kind
) 
3216     if ( m_attrProvider 
) 
3217         return m_attrProvider
->GetAttr(row
, col
, kind
); 
3219         return (wxGridCellAttr 
*)NULL
; 
3222 void wxGridTableBase::SetAttr(wxGridCellAttr
* attr
, int row
, int col
) 
3224     if ( m_attrProvider 
) 
3227             attr
->SetKind(wxGridCellAttr::Cell
); 
3228         m_attrProvider
->SetAttr(attr
, row
, col
); 
3232         // as we take ownership of the pointer and don't store it, we must 
3238 void wxGridTableBase::SetRowAttr(wxGridCellAttr 
*attr
, int row
) 
3240     if ( m_attrProvider 
) 
3242         attr
->SetKind(wxGridCellAttr::Row
); 
3243         m_attrProvider
->SetRowAttr(attr
, row
); 
3247         // as we take ownership of the pointer and don't store it, we must 
3253 void wxGridTableBase::SetColAttr(wxGridCellAttr 
*attr
, int col
) 
3255     if ( m_attrProvider 
) 
3257         attr
->SetKind(wxGridCellAttr::Col
); 
3258         m_attrProvider
->SetColAttr(attr
, col
); 
3262         // as we take ownership of the pointer and don't store it, we must 
3268 bool wxGridTableBase::InsertRows( size_t WXUNUSED(pos
), 
3269                                   size_t WXUNUSED(numRows
) ) 
3271     wxFAIL_MSG( wxT("Called grid table class function InsertRows\nbut your derived table class does not override this function") ); 
3276 bool wxGridTableBase::AppendRows( size_t WXUNUSED(numRows
) ) 
3278     wxFAIL_MSG( wxT("Called grid table class function AppendRows\nbut your derived table class does not override this function")); 
3283 bool wxGridTableBase::DeleteRows( size_t WXUNUSED(pos
), 
3284                                   size_t WXUNUSED(numRows
) ) 
3286     wxFAIL_MSG( wxT("Called grid table class function DeleteRows\nbut your derived table class does not override this function")); 
3291 bool wxGridTableBase::InsertCols( size_t WXUNUSED(pos
), 
3292                                   size_t WXUNUSED(numCols
) ) 
3294     wxFAIL_MSG( wxT("Called grid table class function InsertCols\nbut your derived table class does not override this function")); 
3299 bool wxGridTableBase::AppendCols( size_t WXUNUSED(numCols
) ) 
3301     wxFAIL_MSG(wxT("Called grid table class function AppendCols\nbut your derived table class does not override this function")); 
3306 bool wxGridTableBase::DeleteCols( size_t WXUNUSED(pos
), 
3307                                   size_t WXUNUSED(numCols
) ) 
3309     wxFAIL_MSG( wxT("Called grid table class function DeleteCols\nbut your derived table class does not override this function")); 
3314 wxString 
wxGridTableBase::GetRowLabelValue( int row 
) 
3318     // RD: Starting the rows at zero confuses users, 
3319     // no matter how much it makes sense to us geeks. 
3325 wxString 
wxGridTableBase::GetColLabelValue( int col 
) 
3327     // default col labels are: 
3328     //   cols 0 to 25   : A-Z 
3329     //   cols 26 to 675 : AA-ZZ 
3334     for ( n 
= 1; ; n
++ ) 
3336         s 
+= (wxChar
) (_T('A') + (wxChar
)(col 
% 26)); 
3342     // reverse the string... 
3344     for ( i 
= 0; i 
< n
; i
++ ) 
3352 wxString 
wxGridTableBase::GetTypeName( int WXUNUSED(row
), int WXUNUSED(col
) ) 
3354     return wxGRID_VALUE_STRING
; 
3357 bool wxGridTableBase::CanGetValueAs( int WXUNUSED(row
), int WXUNUSED(col
), 
3358                                      const wxString
& typeName 
) 
3360     return typeName 
== wxGRID_VALUE_STRING
; 
3363 bool wxGridTableBase::CanSetValueAs( int row
, int col
, const wxString
& typeName 
) 
3365     return CanGetValueAs(row
, col
, typeName
); 
3368 long wxGridTableBase::GetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
) ) 
3373 double wxGridTableBase::GetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
) ) 
3378 bool wxGridTableBase::GetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
) ) 
3383 void wxGridTableBase::SetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
), 
3384                                       long WXUNUSED(value
) ) 
3388 void wxGridTableBase::SetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
), 
3389                                         double WXUNUSED(value
) ) 
3393 void wxGridTableBase::SetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
), 
3394                                       bool WXUNUSED(value
) ) 
3398 void* wxGridTableBase::GetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
), 
3399                                          const wxString
& WXUNUSED(typeName
) ) 
3404 void  wxGridTableBase::SetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
), 
3405                                          const wxString
& WXUNUSED(typeName
), 
3406                                          void* WXUNUSED(value
) ) 
3410 ////////////////////////////////////////////////////////////////////// 
3412 // Message class for the grid table to send requests and notifications 
3416 wxGridTableMessage::wxGridTableMessage() 
3418     m_table 
= (wxGridTableBase 
*) NULL
; 
3424 wxGridTableMessage::wxGridTableMessage( wxGridTableBase 
*table
, int id
, 
3425                                         int commandInt1
, int commandInt2 
) 
3429     m_comInt1 
= commandInt1
; 
3430     m_comInt2 
= commandInt2
; 
3433 ////////////////////////////////////////////////////////////////////// 
3435 // A basic grid table for string data. An object of this class will 
3436 // created by wxGrid if you don't specify an alternative table class. 
3439 WX_DEFINE_OBJARRAY(wxGridStringArray
) 
3441 IMPLEMENT_DYNAMIC_CLASS( wxGridStringTable
, wxGridTableBase 
) 
3443 wxGridStringTable::wxGridStringTable() 
3448 wxGridStringTable::wxGridStringTable( int numRows
, int numCols 
) 
3451     m_data
.Alloc( numRows 
); 
3454     sa
.Alloc( numCols 
); 
3455     sa
.Add( wxEmptyString
, numCols 
); 
3457     m_data
.Add( sa
, numRows 
); 
3460 wxGridStringTable::~wxGridStringTable() 
3464 int wxGridStringTable::GetNumberRows() 
3466     return m_data
.GetCount(); 
3469 int wxGridStringTable::GetNumberCols() 
3471     if ( m_data
.GetCount() > 0 ) 
3472         return m_data
[0].GetCount(); 
3477 wxString 
wxGridStringTable::GetValue( int row
, int col 
) 
3479     wxCHECK_MSG( (row 
< GetNumberRows()) && (col 
< GetNumberCols()), 
3481                  _T("invalid row or column index in wxGridStringTable") ); 
3483     return m_data
[row
][col
]; 
3486 void wxGridStringTable::SetValue( int row
, int col
, const wxString
& value 
) 
3488     wxCHECK_RET( (row 
< GetNumberRows()) && (col 
< GetNumberCols()), 
3489                  _T("invalid row or column index in wxGridStringTable") ); 
3491     m_data
[row
][col
] = value
; 
3494 bool wxGridStringTable::IsEmptyCell( int row
, int col 
) 
3496     wxCHECK_MSG( (row 
< GetNumberRows()) && (col 
< GetNumberCols()), 
3498                   _T("invalid row or column index in wxGridStringTable") ); 
3500     return (m_data
[row
][col
] == wxEmptyString
); 
3503 void wxGridStringTable::Clear() 
3506     int numRows
, numCols
; 
3508     numRows 
= m_data
.GetCount(); 
3511         numCols 
= m_data
[0].GetCount(); 
3513         for ( row 
= 0; row 
< numRows
; row
++ ) 
3515             for ( col 
= 0; col 
< numCols
; col
++ ) 
3517                 m_data
[row
][col
] = wxEmptyString
; 
3523 bool wxGridStringTable::InsertRows( size_t pos
, size_t numRows 
) 
3525     size_t curNumRows 
= m_data
.GetCount(); 
3526     size_t curNumCols 
= ( curNumRows 
> 0 ? m_data
[0].GetCount() : 
3527                           ( GetView() ? GetView()->GetNumberCols() : 0 ) ); 
3529     if ( pos 
>= curNumRows 
) 
3531         return AppendRows( numRows 
); 
3535     sa
.Alloc( curNumCols 
); 
3536     sa
.Add( wxEmptyString
, curNumCols 
); 
3537     m_data
.Insert( sa
, pos
, numRows 
); 
3541         wxGridTableMessage 
msg( this, 
3542                                 wxGRIDTABLE_NOTIFY_ROWS_INSERTED
, 
3546         GetView()->ProcessTableMessage( msg 
); 
3552 bool wxGridStringTable::AppendRows( size_t numRows 
) 
3554     size_t curNumRows 
= m_data
.GetCount(); 
3555     size_t curNumCols 
= ( curNumRows 
> 0 
3556                          ? m_data
[0].GetCount() 
3557                          : ( GetView() ? GetView()->GetNumberCols() : 0 ) ); 
3560     if ( curNumCols 
> 0 ) 
3562         sa
.Alloc( curNumCols 
); 
3563         sa
.Add( wxEmptyString
, curNumCols 
); 
3566     m_data
.Add( sa
, numRows 
); 
3570         wxGridTableMessage 
msg( this, 
3571                                 wxGRIDTABLE_NOTIFY_ROWS_APPENDED
, 
3574         GetView()->ProcessTableMessage( msg 
); 
3580 bool wxGridStringTable::DeleteRows( size_t pos
, size_t numRows 
) 
3582     size_t curNumRows 
= m_data
.GetCount(); 
3584     if ( pos 
>= curNumRows 
) 
3586         wxFAIL_MSG( wxString::Format
 
3588                         wxT("Called wxGridStringTable::DeleteRows(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu rows"), 
3590                         (unsigned long)numRows
, 
3591                         (unsigned long)curNumRows
 
3597     if ( numRows 
> curNumRows 
- pos 
) 
3599         numRows 
= curNumRows 
- pos
; 
3602     if ( numRows 
>= curNumRows 
) 
3608         m_data
.RemoveAt( pos
, numRows 
); 
3613         wxGridTableMessage 
msg( this, 
3614                                 wxGRIDTABLE_NOTIFY_ROWS_DELETED
, 
3618         GetView()->ProcessTableMessage( msg 
); 
3624 bool wxGridStringTable::InsertCols( size_t pos
, size_t numCols 
) 
3628     size_t curNumRows 
= m_data
.GetCount(); 
3629     size_t curNumCols 
= ( curNumRows 
> 0 
3630                          ? m_data
[0].GetCount() 
3631                          : ( GetView() ? GetView()->GetNumberCols() : 0 ) ); 
3633     if ( pos 
>= curNumCols 
) 
3635         return AppendCols( numCols 
); 
3638     if ( !m_colLabels
.IsEmpty() ) 
3640         m_colLabels
.Insert( wxEmptyString
, pos
, numCols 
); 
3643         for ( i 
= pos
; i 
< pos 
+ numCols
; i
++ ) 
3644             m_colLabels
[i
] = wxGridTableBase::GetColLabelValue( i 
); 
3647     for ( row 
= 0; row 
< curNumRows
; row
++ ) 
3649         for ( col 
= pos
; col 
< pos 
+ numCols
; col
++ ) 
3651             m_data
[row
].Insert( wxEmptyString
, col 
); 
3657         wxGridTableMessage 
msg( this, 
3658                                 wxGRIDTABLE_NOTIFY_COLS_INSERTED
, 
3662         GetView()->ProcessTableMessage( msg 
); 
3668 bool wxGridStringTable::AppendCols( size_t numCols 
) 
3672     size_t curNumRows 
= m_data
.GetCount(); 
3677         // TODO: something better than this ? 
3679         wxFAIL_MSG( wxT("Unable to append cols to a grid table with no rows.\nCall AppendRows() first") ); 
3684     for ( row 
= 0; row 
< curNumRows
; row
++ ) 
3686         m_data
[row
].Add( wxEmptyString
, numCols 
); 
3691         wxGridTableMessage 
msg( this, 
3692                                 wxGRIDTABLE_NOTIFY_COLS_APPENDED
, 
3695         GetView()->ProcessTableMessage( msg 
); 
3701 bool wxGridStringTable::DeleteCols( size_t pos
, size_t numCols 
) 
3705     size_t curNumRows 
= m_data
.GetCount(); 
3706     size_t curNumCols 
= ( curNumRows 
> 0 ? m_data
[0].GetCount() : 
3707                           ( GetView() ? GetView()->GetNumberCols() : 0 ) ); 
3709     if ( pos 
>= curNumCols 
) 
3711         wxFAIL_MSG( wxString::Format
 
3713                         wxT("Called wxGridStringTable::DeleteCols(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu cols"), 
3715                         (unsigned long)numCols
, 
3716                         (unsigned long)curNumCols
 
3723         colID 
= GetView()->GetColAt( pos 
); 
3727     if ( numCols 
> curNumCols 
- colID 
) 
3729         numCols 
= curNumCols 
- colID
; 
3732     if ( !m_colLabels
.IsEmpty() ) 
3734         // m_colLabels stores just as many elements as it needs, e.g. if only 
3735         // the label of the first column had been set it would have only one 
3736         // element and not numCols, so account for it 
3737         int nToRm 
= m_colLabels
.size() - colID
; 
3739             m_colLabels
.RemoveAt( colID
, nToRm 
); 
3742     for ( row 
= 0; row 
< curNumRows
; row
++ ) 
3744         if ( numCols 
>= curNumCols 
) 
3746             m_data
[row
].Clear(); 
3750             m_data
[row
].RemoveAt( colID
, numCols 
); 
3756         wxGridTableMessage 
msg( this, 
3757                                 wxGRIDTABLE_NOTIFY_COLS_DELETED
, 
3761         GetView()->ProcessTableMessage( msg 
); 
3767 wxString 
wxGridStringTable::GetRowLabelValue( int row 
) 
3769     if ( row 
> (int)(m_rowLabels
.GetCount()) - 1 ) 
3771         // using default label 
3773         return wxGridTableBase::GetRowLabelValue( row 
); 
3777         return m_rowLabels
[row
]; 
3781 wxString 
wxGridStringTable::GetColLabelValue( int col 
) 
3783     if ( col 
> (int)(m_colLabels
.GetCount()) - 1 ) 
3785         // using default label 
3787         return wxGridTableBase::GetColLabelValue( col 
); 
3791         return m_colLabels
[col
]; 
3795 void wxGridStringTable::SetRowLabelValue( int row
, const wxString
& value 
) 
3797     if ( row 
> (int)(m_rowLabels
.GetCount()) - 1 ) 
3799         int n 
= m_rowLabels
.GetCount(); 
3802         for ( i 
= n
; i 
<= row
; i
++ ) 
3804             m_rowLabels
.Add( wxGridTableBase::GetRowLabelValue(i
) ); 
3808     m_rowLabels
[row
] = value
; 
3811 void wxGridStringTable::SetColLabelValue( int col
, const wxString
& value 
) 
3813     if ( col 
> (int)(m_colLabels
.GetCount()) - 1 ) 
3815         int n 
= m_colLabels
.GetCount(); 
3818         for ( i 
= n
; i 
<= col
; i
++ ) 
3820             m_colLabels
.Add( wxGridTableBase::GetColLabelValue(i
) ); 
3824     m_colLabels
[col
] = value
; 
3828 ////////////////////////////////////////////////////////////////////// 
3829 ////////////////////////////////////////////////////////////////////// 
3831 BEGIN_EVENT_TABLE(wxGridSubwindow
, wxWindow
) 
3832     EVT_MOUSE_CAPTURE_LOST(wxGridSubwindow::OnMouseCaptureLost
) 
3835 void wxGridSubwindow::OnMouseCaptureLost(wxMouseCaptureLostEvent
& WXUNUSED(event
)) 
3837     m_owner
->CancelMouseCapture(); 
3840 IMPLEMENT_DYNAMIC_CLASS( wxGridRowLabelWindow
, wxWindow 
) 
3842 BEGIN_EVENT_TABLE( wxGridRowLabelWindow
, wxGridSubwindow 
) 
3843     EVT_PAINT( wxGridRowLabelWindow::OnPaint 
) 
3844     EVT_MOUSEWHEEL( wxGridRowLabelWindow::OnMouseWheel 
) 
3845     EVT_MOUSE_EVENTS( wxGridRowLabelWindow::OnMouseEvent 
) 
3848 wxGridRowLabelWindow::wxGridRowLabelWindow( wxGrid 
*parent
, 
3850                                             const wxPoint 
&pos
, const wxSize 
&size 
) 
3851   : wxGridSubwindow(parent
, id
, pos
, size
) 
3856 void wxGridRowLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) ) 
3860     // NO - don't do this because it will set both the x and y origin 
3861     // coords to match the parent scrolled window and we just want to 
3862     // set the y coord  - MB 
3864     // m_owner->PrepareDC( dc ); 
3867     m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y 
); 
3868     wxPoint pt 
= dc
.GetDeviceOrigin(); 
3869     dc
.SetDeviceOrigin( pt
.x
, pt
.y
-y 
); 
3871     wxArrayInt rows 
= m_owner
->CalcRowLabelsExposed( GetUpdateRegion() ); 
3872     m_owner
->DrawRowLabels( dc
, rows 
); 
3875 void wxGridRowLabelWindow::OnMouseEvent( wxMouseEvent
& event 
) 
3877     m_owner
->ProcessRowLabelMouseEvent( event 
); 
3880 void wxGridRowLabelWindow::OnMouseWheel( wxMouseEvent
& event 
) 
3882     m_owner
->GetEventHandler()->ProcessEvent( event 
); 
3885 ////////////////////////////////////////////////////////////////////// 
3887 IMPLEMENT_DYNAMIC_CLASS( wxGridColLabelWindow
, wxWindow 
) 
3889 BEGIN_EVENT_TABLE( wxGridColLabelWindow
, wxGridSubwindow 
) 
3890     EVT_PAINT( wxGridColLabelWindow::OnPaint 
) 
3891     EVT_MOUSEWHEEL( wxGridColLabelWindow::OnMouseWheel 
) 
3892     EVT_MOUSE_EVENTS( wxGridColLabelWindow::OnMouseEvent 
) 
3895 wxGridColLabelWindow::wxGridColLabelWindow( wxGrid 
*parent
, 
3897                                             const wxPoint 
&pos
, const wxSize 
&size 
) 
3898   : wxGridSubwindow(parent
, id
, pos
, size
) 
3903 void wxGridColLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) ) 
3907     // NO - don't do this because it will set both the x and y origin 
3908     // coords to match the parent scrolled window and we just want to 
3909     // set the x coord  - MB 
3911     // m_owner->PrepareDC( dc ); 
3914     m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y 
); 
3915     wxPoint pt 
= dc
.GetDeviceOrigin(); 
3916     if (GetLayoutDirection() == wxLayout_RightToLeft
) 
3917         dc
.SetDeviceOrigin( pt
.x
+x
, pt
.y 
); 
3919         dc
.SetDeviceOrigin( pt
.x
-x
, pt
.y 
); 
3921     wxArrayInt cols 
= m_owner
->CalcColLabelsExposed( GetUpdateRegion() ); 
3922     m_owner
->DrawColLabels( dc
, cols 
); 
3925 void wxGridColLabelWindow::OnMouseEvent( wxMouseEvent
& event 
) 
3927     m_owner
->ProcessColLabelMouseEvent( event 
); 
3930 void wxGridColLabelWindow::OnMouseWheel( wxMouseEvent
& event 
) 
3932     m_owner
->GetEventHandler()->ProcessEvent( event 
); 
3935 ////////////////////////////////////////////////////////////////////// 
3937 IMPLEMENT_DYNAMIC_CLASS( wxGridCornerLabelWindow
, wxWindow 
) 
3939 BEGIN_EVENT_TABLE( wxGridCornerLabelWindow
, wxGridSubwindow 
) 
3940     EVT_MOUSEWHEEL( wxGridCornerLabelWindow::OnMouseWheel 
) 
3941     EVT_MOUSE_EVENTS( wxGridCornerLabelWindow::OnMouseEvent 
) 
3942     EVT_PAINT( wxGridCornerLabelWindow::OnPaint 
) 
3945 wxGridCornerLabelWindow::wxGridCornerLabelWindow( wxGrid 
*parent
, 
3947                                                   const wxPoint 
&pos
, const wxSize 
&size 
) 
3948   : wxGridSubwindow(parent
, id
, pos
, size
) 
3953 void wxGridCornerLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) ) 
3957     int client_height 
= 0; 
3958     int client_width 
= 0; 
3959     GetClientSize( &client_width
, &client_height 
); 
3961     // VZ: any reason for this ifdef? (FIXME) 
3967     rect
.SetWidth( client_width 
- 2 ); 
3968     rect
.SetHeight( client_height 
- 2 ); 
3970     wxRendererNative::Get().DrawHeaderButton( this, dc
, rect
, 0 ); 
3972     dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW
), 1, wxPENSTYLE_SOLID
) ); 
3973     dc
.DrawLine( client_width 
- 1, client_height 
- 1, client_width 
- 1, 0 ); 
3974     dc
.DrawLine( client_width 
- 1, client_height 
- 1, 0, client_height 
- 1 ); 
3975     dc
.DrawLine( 0, 0, client_width
, 0 ); 
3976     dc
.DrawLine( 0, 0, 0, client_height 
); 
3978     dc
.SetPen( *wxWHITE_PEN 
); 
3979     dc
.DrawLine( 1, 1, client_width 
- 1, 1 ); 
3980     dc
.DrawLine( 1, 1, 1, client_height 
- 1 ); 
3984 void wxGridCornerLabelWindow::OnMouseEvent( wxMouseEvent
& event 
) 
3986     m_owner
->ProcessCornerLabelMouseEvent( event 
); 
3989 void wxGridCornerLabelWindow::OnMouseWheel( wxMouseEvent
& event 
) 
3991     m_owner
->GetEventHandler()->ProcessEvent(event
); 
3994 ////////////////////////////////////////////////////////////////////// 
3996 IMPLEMENT_DYNAMIC_CLASS( wxGridWindow
, wxWindow 
) 
3998 BEGIN_EVENT_TABLE( wxGridWindow
, wxGridSubwindow 
) 
3999     EVT_PAINT( wxGridWindow::OnPaint 
) 
4000     EVT_MOUSEWHEEL( wxGridWindow::OnMouseWheel 
) 
4001     EVT_MOUSE_EVENTS( wxGridWindow::OnMouseEvent 
) 
4002     EVT_KEY_DOWN( wxGridWindow::OnKeyDown 
) 
4003     EVT_KEY_UP( wxGridWindow::OnKeyUp 
) 
4004     EVT_CHAR( wxGridWindow::OnChar 
) 
4005     EVT_SET_FOCUS( wxGridWindow::OnFocus 
) 
4006     EVT_KILL_FOCUS( wxGridWindow::OnFocus 
) 
4007     EVT_ERASE_BACKGROUND( wxGridWindow::OnEraseBackground 
) 
4010 wxGridWindow::wxGridWindow( wxGrid 
*parent
, 
4011                             wxGridRowLabelWindow 
*rowLblWin
, 
4012                             wxGridColLabelWindow 
*colLblWin
, 
4015                             const wxSize 
&size 
) 
4016             : wxGridSubwindow(parent
, id
, pos
, size
, 
4017                               wxWANTS_CHARS 
| wxCLIP_CHILDREN
, 
4018                               wxT("grid window") ) 
4021     m_rowLabelWin 
= rowLblWin
; 
4022     m_colLabelWin 
= colLblWin
; 
4025 void wxGridWindow::OnPaint( wxPaintEvent 
&WXUNUSED(event
) ) 
4027     wxPaintDC 
dc( this ); 
4028     m_owner
->PrepareDC( dc 
); 
4029     wxRegion reg 
= GetUpdateRegion(); 
4030     wxGridCellCoordsArray dirtyCells 
= m_owner
->CalcCellsExposed( reg 
); 
4031     m_owner
->DrawGridCellArea( dc
, dirtyCells 
); 
4033 #if WXGRID_DRAW_LINES 
4034     m_owner
->DrawAllGridLines( dc
, reg 
); 
4037     m_owner
->DrawGridSpace( dc 
); 
4038     m_owner
->DrawHighlight( dc
, dirtyCells 
); 
4041 void wxGridWindow::ScrollWindow( int dx
, int dy
, const wxRect 
*rect 
) 
4043     wxWindow::ScrollWindow( dx
, dy
, rect 
); 
4044     m_rowLabelWin
->ScrollWindow( 0, dy
, rect 
); 
4045     m_colLabelWin
->ScrollWindow( dx
, 0, rect 
); 
4048 void wxGridWindow::OnMouseEvent( wxMouseEvent
& event 
) 
4050     if (event
.ButtonDown(wxMOUSE_BTN_LEFT
) && FindFocus() != this) 
4053     m_owner
->ProcessGridCellMouseEvent( event 
); 
4056 void wxGridWindow::OnMouseWheel( wxMouseEvent
& event 
) 
4058     m_owner
->GetEventHandler()->ProcessEvent( event 
); 
4061 // This seems to be required for wxMotif/wxGTK otherwise the mouse 
4062 // cursor must be in the cell edit control to get key events 
4064 void wxGridWindow::OnKeyDown( wxKeyEvent
& event 
) 
4066     if ( !m_owner
->GetEventHandler()->ProcessEvent( event 
) ) 
4070 void wxGridWindow::OnKeyUp( wxKeyEvent
& event 
) 
4072     if ( !m_owner
->GetEventHandler()->ProcessEvent( event 
) ) 
4076 void wxGridWindow::OnChar( wxKeyEvent
& event 
) 
4078     if ( !m_owner
->GetEventHandler()->ProcessEvent( event 
) ) 
4082 void wxGridWindow::OnEraseBackground( wxEraseEvent
& WXUNUSED(event
) ) 
4086 void wxGridWindow::OnFocus(wxFocusEvent
& event
) 
4088     // and if we have any selection, it has to be repainted, because it 
4089     // uses different colour when the grid is not focused: 
4090     if ( m_owner
->IsSelection() ) 
4096         // NB: Note that this code is in "else" branch only because the other 
4097         //     branch refreshes everything and so there's no point in calling 
4098         //     Refresh() again, *not* because it should only be done if 
4099         //     !IsSelection(). If the above code is ever optimized to refresh 
4100         //     only selected area, this needs to be moved out of the "else" 
4101         //     branch so that it's always executed. 
4103         // current cell cursor {dis,re}appears on focus change: 
4104         const wxGridCellCoords 
cursorCoords(m_owner
->GetGridCursorRow(), 
4105                                             m_owner
->GetGridCursorCol()); 
4106         const wxRect cursor 
= 
4107             m_owner
->BlockToDeviceRect(cursorCoords
, cursorCoords
); 
4108         Refresh(true, &cursor
); 
4111     if ( !m_owner
->GetEventHandler()->ProcessEvent( event 
) ) 
4115 ////////////////////////////////////////////////////////////////////// 
4117 // Internal Helper function for computing row or column from some 
4118 // (unscrolled) coordinate value, using either 
4119 // m_defaultRowHeight/m_defaultColWidth or binary search on array 
4120 // of m_rowBottoms/m_ColRights to speed up the search! 
4122 // Internal helper macros for simpler use of that function 
4124 static int CoordToRowOrCol(int coord
, int defaultDist
, int minDist
, 
4125                            const wxArrayInt
& BorderArray
, int nMax
, 
4128 #define internalXToCol(x) XToCol(x, true) 
4129 #define internalYToRow(y) CoordToRowOrCol(y, m_defaultRowHeight, \ 
4130                                           m_minAcceptableRowHeight, \ 
4131                                           m_rowBottoms, m_numRows, true) 
4133 ///////////////////////////////////////////////////////////////////// 
4135 #if wxUSE_EXTENDED_RTTI 
4136 WX_DEFINE_FLAGS( wxGridStyle 
) 
4138 wxBEGIN_FLAGS( wxGridStyle 
) 
4139     // new style border flags, we put them first to 
4140     // use them for streaming out 
4141     wxFLAGS_MEMBER(wxBORDER_SIMPLE
) 
4142     wxFLAGS_MEMBER(wxBORDER_SUNKEN
) 
4143     wxFLAGS_MEMBER(wxBORDER_DOUBLE
) 
4144     wxFLAGS_MEMBER(wxBORDER_RAISED
) 
4145     wxFLAGS_MEMBER(wxBORDER_STATIC
) 
4146     wxFLAGS_MEMBER(wxBORDER_NONE
) 
4148     // old style border flags 
4149     wxFLAGS_MEMBER(wxSIMPLE_BORDER
) 
4150     wxFLAGS_MEMBER(wxSUNKEN_BORDER
) 
4151     wxFLAGS_MEMBER(wxDOUBLE_BORDER
) 
4152     wxFLAGS_MEMBER(wxRAISED_BORDER
) 
4153     wxFLAGS_MEMBER(wxSTATIC_BORDER
) 
4154     wxFLAGS_MEMBER(wxBORDER
) 
4156     // standard window styles 
4157     wxFLAGS_MEMBER(wxTAB_TRAVERSAL
) 
4158     wxFLAGS_MEMBER(wxCLIP_CHILDREN
) 
4159     wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
) 
4160     wxFLAGS_MEMBER(wxWANTS_CHARS
) 
4161     wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE
) 
4162     wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
) 
4163     wxFLAGS_MEMBER(wxVSCROLL
) 
4164     wxFLAGS_MEMBER(wxHSCROLL
) 
4166 wxEND_FLAGS( wxGridStyle 
) 
4168 IMPLEMENT_DYNAMIC_CLASS_XTI(wxGrid
, wxScrolledWindow
,"wx/grid.h") 
4170 wxBEGIN_PROPERTIES_TABLE(wxGrid
) 
4171     wxHIDE_PROPERTY( Children 
) 
4172     wxPROPERTY_FLAGS( WindowStyle 
, wxGridStyle 
, long , SetWindowStyleFlag 
, GetWindowStyleFlag 
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style 
4173 wxEND_PROPERTIES_TABLE() 
4175 wxBEGIN_HANDLERS_TABLE(wxGrid
) 
4176 wxEND_HANDLERS_TABLE() 
4178 wxCONSTRUCTOR_5( wxGrid 
, wxWindow
* , Parent 
, wxWindowID 
, Id 
, wxPoint 
, Position 
, wxSize 
, Size 
, long , WindowStyle 
) 
4181  TODO : Expose more information of a list's layout, etc. via appropriate objects (e.g., NotebookPageInfo) 
4184 IMPLEMENT_DYNAMIC_CLASS( wxGrid
, wxScrolledWindow 
) 
4187 BEGIN_EVENT_TABLE( wxGrid
, wxScrolledWindow 
) 
4188     EVT_PAINT( wxGrid::OnPaint 
) 
4189     EVT_SIZE( wxGrid::OnSize 
) 
4190     EVT_KEY_DOWN( wxGrid::OnKeyDown 
) 
4191     EVT_KEY_UP( wxGrid::OnKeyUp 
) 
4192     EVT_CHAR ( wxGrid::OnChar 
) 
4193     EVT_ERASE_BACKGROUND( wxGrid::OnEraseBackground 
) 
4201 wxGrid::wxGrid( wxWindow 
*parent
, 
4206                  const wxString
& name 
) 
4209     Create(parent
, id
, pos
, size
, style
, name
); 
4212 bool wxGrid::Create(wxWindow 
*parent
, wxWindowID id
, 
4213                           const wxPoint
& pos
, const wxSize
& size
, 
4214                           long style
, const wxString
& name
) 
4216     if (!wxScrolledWindow::Create(parent
, id
, pos
, size
, 
4217                                   style 
| wxWANTS_CHARS
, name
)) 
4220     m_colMinWidths 
= wxLongToLongHashMap(GRID_HASH_SIZE
); 
4221     m_rowMinHeights 
= wxLongToLongHashMap(GRID_HASH_SIZE
); 
4224     SetInitialSize(size
); 
4225     SetScrollRate(m_scrollLineX
, m_scrollLineY
); 
4233     // Must do this or ~wxScrollHelper will pop the wrong event handler 
4234     SetTargetWindow(this); 
4236     wxSafeDecRef(m_defaultCellAttr
); 
4238 #ifdef DEBUG_ATTR_CACHE 
4239     size_t total 
= gs_nAttrCacheHits 
+ gs_nAttrCacheMisses
; 
4240     wxPrintf(_T("wxGrid attribute cache statistics: " 
4241                 "total: %u, hits: %u (%u%%)\n"), 
4242              total
, gs_nAttrCacheHits
, 
4243              total 
? (gs_nAttrCacheHits
*100) / total 
: 0); 
4246     // if we own the table, just delete it, otherwise at least don't leave it 
4247     // with dangling view pointer 
4250     else if ( m_table 
&& m_table
->GetView() == this ) 
4251         m_table
->SetView(NULL
); 
4253     delete m_typeRegistry
; 
4258 // ----- internal init and update functions 
4261 // NOTE: If using the default visual attributes works everywhere then this can 
4262 // be removed as well as the #else cases below. 
4263 #define _USE_VISATTR 0 
4265 void wxGrid::Create() 
4267     // create the type registry 
4268     m_typeRegistry 
= new wxGridTypeRegistry
; 
4270     m_cellEditCtrlEnabled 
= false; 
4272     m_defaultCellAttr 
= new wxGridCellAttr(); 
4274     // Set default cell attributes 
4275     m_defaultCellAttr
->SetDefAttr(m_defaultCellAttr
); 
4276     m_defaultCellAttr
->SetKind(wxGridCellAttr::Default
); 
4277     m_defaultCellAttr
->SetFont(GetFont()); 
4278     m_defaultCellAttr
->SetAlignment(wxALIGN_LEFT
, wxALIGN_TOP
); 
4279     m_defaultCellAttr
->SetRenderer(new wxGridCellStringRenderer
); 
4280     m_defaultCellAttr
->SetEditor(new wxGridCellTextEditor
); 
4283     wxVisualAttributes gva 
= wxListBox::GetClassDefaultAttributes(); 
4284     wxVisualAttributes lva 
= wxPanel::GetClassDefaultAttributes(); 
4286     m_defaultCellAttr
->SetTextColour(gva
.colFg
); 
4287     m_defaultCellAttr
->SetBackgroundColour(gva
.colBg
); 
4290     m_defaultCellAttr
->SetTextColour( 
4291         wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
)); 
4292     m_defaultCellAttr
->SetBackgroundColour( 
4293         wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
)); 
4298     m_currentCellCoords 
= wxGridNoCellCoords
; 
4300     m_rowLabelWidth 
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
; 
4301     m_colLabelHeight 
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
; 
4303     // subwindow components that make up the wxGrid 
4304     m_rowLabelWin 
= new wxGridRowLabelWindow( this, 
4309     m_colLabelWin 
= new wxGridColLabelWindow( this, 
4314     m_cornerLabelWin 
= new wxGridCornerLabelWindow( this, 
4319     m_gridWin 
= new wxGridWindow( this, 
4326     SetTargetWindow( m_gridWin 
); 
4329     wxColour gfg 
= gva
.colFg
; 
4330     wxColour gbg 
= gva
.colBg
; 
4331     wxColour lfg 
= lva
.colFg
; 
4332     wxColour lbg 
= lva
.colBg
; 
4334     wxColour gfg 
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT 
); 
4335     wxColour gbg 
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOW 
); 
4336     wxColour lfg 
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT 
); 
4337     wxColour lbg 
= wxSystemSettings::GetColour( wxSYS_COLOUR_BTNFACE 
); 
4340     m_cornerLabelWin
->SetOwnForegroundColour(lfg
); 
4341     m_cornerLabelWin
->SetOwnBackgroundColour(lbg
); 
4342     m_rowLabelWin
->SetOwnForegroundColour(lfg
); 
4343     m_rowLabelWin
->SetOwnBackgroundColour(lbg
); 
4344     m_colLabelWin
->SetOwnForegroundColour(lfg
); 
4345     m_colLabelWin
->SetOwnBackgroundColour(lbg
); 
4347     m_gridWin
->SetOwnForegroundColour(gfg
); 
4348     m_gridWin
->SetOwnBackgroundColour(gbg
); 
4353 bool wxGrid::CreateGrid( int numRows
, int numCols
, 
4354                          wxGrid::wxGridSelectionModes selmode 
) 
4356     wxCHECK_MSG( !m_created
, 
4358                  wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") ); 
4360     m_numRows 
= numRows
; 
4361     m_numCols 
= numCols
; 
4363     m_table 
= new wxGridStringTable( m_numRows
, m_numCols 
); 
4364     m_table
->SetView( this ); 
4366     m_selection 
= new wxGridSelection( this, selmode 
); 
4375 void wxGrid::SetSelectionMode(wxGrid::wxGridSelectionModes selmode
) 
4377     wxCHECK_RET( m_created
, 
4378                  wxT("Called wxGrid::SetSelectionMode() before calling CreateGrid()") ); 
4380     m_selection
->SetSelectionMode( selmode 
); 
4383 wxGrid::wxGridSelectionModes 
wxGrid::GetSelectionMode() const 
4385     wxCHECK_MSG( m_created
, wxGrid::wxGridSelectCells
, 
4386                  wxT("Called wxGrid::GetSelectionMode() before calling CreateGrid()") ); 
4388     return m_selection
->GetSelectionMode(); 
4391 bool wxGrid::SetTable( wxGridTableBase 
*table
, bool takeOwnership
, 
4392                        wxGrid::wxGridSelectionModes selmode 
) 
4394     bool checkSelection 
= false; 
4397         // stop all processing 
4402             m_table
->SetView(0); 
4414         checkSelection 
= true; 
4416         // kill row and column size arrays 
4417         m_colWidths
.Empty(); 
4418         m_colRights
.Empty(); 
4419         m_rowHeights
.Empty(); 
4420         m_rowBottoms
.Empty(); 
4425         m_numRows 
= table
->GetNumberRows(); 
4426         m_numCols 
= table
->GetNumberCols(); 
4429         m_table
->SetView( this ); 
4430         m_ownTable 
= takeOwnership
; 
4431         m_selection 
= new wxGridSelection( this, selmode 
); 
4434             // If the newly set table is smaller than the 
4435             // original one current cell and selection regions 
4436             // might be invalid, 
4437             m_selectingKeyboard 
= wxGridNoCellCoords
; 
4438             m_currentCellCoords 
= 
4439               wxGridCellCoords(wxMin(m_numRows
, m_currentCellCoords
.GetRow()), 
4440                                wxMin(m_numCols
, m_currentCellCoords
.GetCol())); 
4441             if (m_selectingTopLeft
.GetRow() >= m_numRows 
|| 
4442                 m_selectingTopLeft
.GetCol() >= m_numCols
) 
4444                 m_selectingTopLeft 
= wxGridNoCellCoords
; 
4445                 m_selectingBottomRight 
= wxGridNoCellCoords
; 
4448                 m_selectingBottomRight 
= 
4449                   wxGridCellCoords(wxMin(m_numRows
, 
4450                                          m_selectingBottomRight
.GetRow()), 
4452                                          m_selectingBottomRight
.GetCol())); 
4462 void wxGrid::InitVars() 
4466     m_cornerLabelWin 
= NULL
; 
4467     m_rowLabelWin 
= NULL
; 
4468     m_colLabelWin 
= NULL
; 
4475     m_defaultCellAttr 
= NULL
; 
4476     m_typeRegistry 
= NULL
; 
4477     m_winCapture 
= NULL
; 
4482     m_rowLabelWidth  
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
; 
4483     m_colLabelHeight 
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
; 
4485     if ( m_rowLabelWin 
) 
4487         m_labelBackgroundColour 
= m_rowLabelWin
->GetBackgroundColour(); 
4491         m_labelBackgroundColour 
= *wxWHITE
; 
4494     m_labelTextColour 
= *wxBLACK
; 
4497     m_attrCache
.row 
= -1; 
4498     m_attrCache
.col 
= -1; 
4499     m_attrCache
.attr 
= NULL
; 
4501     // TODO: something better than this ? 
4503     m_labelFont 
= this->GetFont(); 
4504     m_labelFont
.SetWeight( wxBOLD 
); 
4506     m_rowLabelHorizAlign 
= wxALIGN_CENTRE
; 
4507     m_rowLabelVertAlign  
= wxALIGN_CENTRE
; 
4509     m_colLabelHorizAlign 
= wxALIGN_CENTRE
; 
4510     m_colLabelVertAlign  
= wxALIGN_CENTRE
; 
4511     m_colLabelTextOrientation 
= wxHORIZONTAL
; 
4513     m_defaultColWidth  
= WXGRID_DEFAULT_COL_WIDTH
; 
4514     m_defaultRowHeight 
= m_gridWin
->GetCharHeight(); 
4516     m_minAcceptableColWidth  
= WXGRID_MIN_COL_WIDTH
; 
4517     m_minAcceptableRowHeight 
= WXGRID_MIN_ROW_HEIGHT
; 
4519 #if defined(__WXMOTIF__) || defined(__WXGTK__)  // see also text ctrl sizing in ShowCellEditControl() 
4520     m_defaultRowHeight 
+= 8; 
4522     m_defaultRowHeight 
+= 4; 
4525     m_gridLineColour 
= wxColour( 192,192,192 ); 
4526     m_gridLinesEnabled 
= true; 
4527     m_cellHighlightColour 
= *wxBLACK
; 
4528     m_cellHighlightPenWidth 
= 2; 
4529     m_cellHighlightROPenWidth 
= 1; 
4531     m_canDragColMove 
= false; 
4533     m_cursorMode  
= WXGRID_CURSOR_SELECT_CELL
; 
4534     m_winCapture 
= (wxWindow 
*)NULL
; 
4535     m_canDragRowSize 
= true; 
4536     m_canDragColSize 
= true; 
4537     m_canDragGridSize 
= true; 
4538     m_canDragCell 
= false; 
4540     m_dragRowOrCol 
= -1; 
4541     m_isDragging 
= false; 
4542     m_startDragPos 
= wxDefaultPosition
; 
4543     m_nativeColumnLabels 
= false; 
4545     m_waitForSlowClick 
= false; 
4547     m_rowResizeCursor 
= wxCursor( wxCURSOR_SIZENS 
); 
4548     m_colResizeCursor 
= wxCursor( wxCURSOR_SIZEWE 
); 
4550     m_currentCellCoords 
= wxGridNoCellCoords
; 
4554     m_selectionBackground 
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
); 
4555     m_selectionForeground 
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
); 
4557     m_editable 
= true;  // default for whole grid 
4559     m_inOnKeyDown 
= false; 
4565     m_scrollLineX 
= GRID_SCROLL_LINE_X
; 
4566     m_scrollLineY 
= GRID_SCROLL_LINE_Y
; 
4569 // ---------------------------------------------------------------------------- 
4570 // the idea is to call these functions only when necessary because they create 
4571 // quite big arrays which eat memory mostly unnecessary - in particular, if 
4572 // default widths/heights are used for all rows/columns, we may not use these 
4575 // with some extra code, it should be possible to only store the widths/heights 
4576 // different from default ones (resulting in space savings for huge grids) but 
4577 // this is not done currently 
4578 // ---------------------------------------------------------------------------- 
4580 void wxGrid::InitRowHeights() 
4582     m_rowHeights
.Empty(); 
4583     m_rowBottoms
.Empty(); 
4585     m_rowHeights
.Alloc( m_numRows 
); 
4586     m_rowBottoms
.Alloc( m_numRows 
); 
4588     m_rowHeights
.Add( m_defaultRowHeight
, m_numRows 
); 
4591     for ( int i 
= 0; i 
< m_numRows
; i
++ ) 
4593         rowBottom 
+= m_defaultRowHeight
; 
4594         m_rowBottoms
.Add( rowBottom 
); 
4598 void wxGrid::InitColWidths() 
4600     m_colWidths
.Empty(); 
4601     m_colRights
.Empty(); 
4603     m_colWidths
.Alloc( m_numCols 
); 
4604     m_colRights
.Alloc( m_numCols 
); 
4606     m_colWidths
.Add( m_defaultColWidth
, m_numCols 
); 
4609     for ( int i 
= 0; i 
< m_numCols
; i
++ ) 
4611         colRight 
= ( GetColPos( i 
) + 1 ) * m_defaultColWidth
; 
4612         m_colRights
.Add( colRight 
); 
4616 int wxGrid::GetColWidth(int col
) const 
4618     return m_colWidths
.IsEmpty() ? m_defaultColWidth 
: m_colWidths
[col
]; 
4621 int wxGrid::GetColLeft(int col
) const 
4623     return m_colRights
.IsEmpty() ? GetColPos( col 
) * m_defaultColWidth
 
4624                                  : m_colRights
[col
] - m_colWidths
[col
]; 
4627 int wxGrid::GetColRight(int col
) const 
4629     return m_colRights
.IsEmpty() ? (GetColPos( col 
) + 1) * m_defaultColWidth
 
4633 int wxGrid::GetRowHeight(int row
) const 
4635     return m_rowHeights
.IsEmpty() ? m_defaultRowHeight 
: m_rowHeights
[row
]; 
4638 int wxGrid::GetRowTop(int row
) const 
4640     return m_rowBottoms
.IsEmpty() ? row 
* m_defaultRowHeight
 
4641                                   : m_rowBottoms
[row
] - m_rowHeights
[row
]; 
4644 int wxGrid::GetRowBottom(int row
) const 
4646     return m_rowBottoms
.IsEmpty() ? (row 
+ 1) * m_defaultRowHeight
 
4647                                   : m_rowBottoms
[row
]; 
4650 void wxGrid::CalcDimensions() 
4652     // compute the size of the scrollable area 
4653     int w 
= m_numCols 
> 0 ? GetColRight(GetColAt(m_numCols 
- 1)) : 0; 
4654     int h 
= m_numRows 
> 0 ? GetRowBottom(m_numRows 
- 1) : 0; 
4659     // take into account editor if shown 
4660     if ( IsCellEditControlShown() ) 
4663         int r 
= m_currentCellCoords
.GetRow(); 
4664         int c 
= m_currentCellCoords
.GetCol(); 
4665         int x 
= GetColLeft(c
); 
4666         int y 
= GetRowTop(r
); 
4668         // how big is the editor 
4669         wxGridCellAttr
* attr 
= GetCellAttr(r
, c
); 
4670         wxGridCellEditor
* editor 
= attr
->GetEditor(this, r
, c
); 
4671         editor
->GetControl()->GetSize(&w2
, &h2
); 
4682     // preserve (more or less) the previous position 
4684     GetViewStart( &x
, &y 
); 
4686     // ensure the position is valid for the new scroll ranges 
4688         x 
= wxMax( w 
- 1, 0 ); 
4690         y 
= wxMax( h 
- 1, 0 ); 
4692     // update the virtual size and refresh the scrollbars to reflect it 
4693     m_gridWin
->SetVirtualSize(w
, h
); 
4697     // if our OnSize() hadn't been called (it would if we have scrollbars), we 
4698     // still must reposition the children 
4702 wxSize 
wxGrid::GetSizeAvailableForScrollTarget(const wxSize
& size
) 
4704     wxSize 
sizeGridWin(size
); 
4705     sizeGridWin
.x 
-= m_rowLabelWidth
; 
4706     sizeGridWin
.y 
-= m_colLabelHeight
; 
4711 void wxGrid::CalcWindowSizes() 
4713     // escape if the window is has not been fully created yet 
4715     if ( m_cornerLabelWin 
== NULL 
) 
4719     GetClientSize( &cw
, &ch 
); 
4721     // the grid may be too small to have enough space for the labels yet, don't 
4722     // size the windows to negative sizes in this case 
4723     int gw 
= cw 
- m_rowLabelWidth
; 
4724     int gh 
= ch 
- m_colLabelHeight
; 
4730     if ( m_cornerLabelWin 
&& m_cornerLabelWin
->IsShown() ) 
4731         m_cornerLabelWin
->SetSize( 0, 0, m_rowLabelWidth
, m_colLabelHeight 
); 
4733     if ( m_colLabelWin 
&& m_colLabelWin
->IsShown() ) 
4734         m_colLabelWin
->SetSize( m_rowLabelWidth
, 0, gw
, m_colLabelHeight 
); 
4736     if ( m_rowLabelWin 
&& m_rowLabelWin
->IsShown() ) 
4737         m_rowLabelWin
->SetSize( 0, m_colLabelHeight
, m_rowLabelWidth
, gh 
); 
4739     if ( m_gridWin 
&& m_gridWin
->IsShown() ) 
4740         m_gridWin
->SetSize( m_rowLabelWidth
, m_colLabelHeight
, gw
, gh 
); 
4743 // this is called when the grid table sends a message 
4744 // to indicate that it has been redimensioned 
4746 bool wxGrid::Redimension( wxGridTableMessage
& msg 
) 
4749     bool result 
= false; 
4751     // Clear the attribute cache as the attribute might refer to a different 
4752     // cell than stored in the cache after adding/removing rows/columns. 
4755     // By the same reasoning, the editor should be dismissed if columns are 
4756     // added or removed. And for consistency, it should IMHO always be 
4757     // removed, not only if the cell "underneath" it actually changes. 
4758     // For now, I intentionally do not save the editor's content as the 
4759     // cell it might want to save that stuff to might no longer exist. 
4760     HideCellEditControl(); 
4763     // if we were using the default widths/heights so far, we must change them 
4765     if ( m_colWidths
.IsEmpty() ) 
4770     if ( m_rowHeights
.IsEmpty() ) 
4776     switch ( msg
.GetId() ) 
4778         case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
: 
4780             size_t pos 
= msg
.GetCommandInt(); 
4781             int numRows 
= msg
.GetCommandInt2(); 
4783             m_numRows 
+= numRows
; 
4785             if ( !m_rowHeights
.IsEmpty() ) 
4787                 m_rowHeights
.Insert( m_defaultRowHeight
, pos
, numRows 
); 
4788                 m_rowBottoms
.Insert( 0, pos
, numRows 
); 
4792                     bottom 
= m_rowBottoms
[pos 
- 1]; 
4794                 for ( i 
= pos
; i 
< m_numRows
; i
++ ) 
4796                     bottom 
+= m_rowHeights
[i
]; 
4797                     m_rowBottoms
[i
] = bottom
; 
4801             if ( m_currentCellCoords 
== wxGridNoCellCoords 
) 
4803                 // if we have just inserted cols into an empty grid the current 
4804                 // cell will be undefined... 
4806                 SetCurrentCell( 0, 0 ); 
4810                 m_selection
->UpdateRows( pos
, numRows 
); 
4811             wxGridCellAttrProvider 
* attrProvider 
= m_table
->GetAttrProvider(); 
4813                 attrProvider
->UpdateAttrRows( pos
, numRows 
); 
4815             if ( !GetBatchCount() ) 
4818                 m_rowLabelWin
->Refresh(); 
4824         case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
: 
4826             int numRows 
= msg
.GetCommandInt(); 
4827             int oldNumRows 
= m_numRows
; 
4828             m_numRows 
+= numRows
; 
4830             if ( !m_rowHeights
.IsEmpty() ) 
4832                 m_rowHeights
.Add( m_defaultRowHeight
, numRows 
); 
4833                 m_rowBottoms
.Add( 0, numRows 
); 
4836                 if ( oldNumRows 
> 0 ) 
4837                     bottom 
= m_rowBottoms
[oldNumRows 
- 1]; 
4839                 for ( i 
= oldNumRows
; i 
< m_numRows
; i
++ ) 
4841                     bottom 
+= m_rowHeights
[i
]; 
4842                     m_rowBottoms
[i
] = bottom
; 
4846             if ( m_currentCellCoords 
== wxGridNoCellCoords 
) 
4848                 // if we have just inserted cols into an empty grid the current 
4849                 // cell will be undefined... 
4851                 SetCurrentCell( 0, 0 ); 
4854             if ( !GetBatchCount() ) 
4857                 m_rowLabelWin
->Refresh(); 
4863         case wxGRIDTABLE_NOTIFY_ROWS_DELETED
: 
4865             size_t pos 
= msg
.GetCommandInt(); 
4866             int numRows 
= msg
.GetCommandInt2(); 
4867             m_numRows 
-= numRows
; 
4869             if ( !m_rowHeights
.IsEmpty() ) 
4871                 m_rowHeights
.RemoveAt( pos
, numRows 
); 
4872                 m_rowBottoms
.RemoveAt( pos
, numRows 
); 
4875                 for ( i 
= 0; i 
< m_numRows
; i
++ ) 
4877                     h 
+= m_rowHeights
[i
]; 
4878                     m_rowBottoms
[i
] = h
; 
4884                 m_currentCellCoords 
= wxGridNoCellCoords
; 
4888                 if ( m_currentCellCoords
.GetRow() >= m_numRows 
) 
4889                     m_currentCellCoords
.Set( 0, 0 ); 
4893                 m_selection
->UpdateRows( pos
, -((int)numRows
) ); 
4894             wxGridCellAttrProvider 
* attrProvider 
= m_table
->GetAttrProvider(); 
4897                 attrProvider
->UpdateAttrRows( pos
, -((int)numRows
) ); 
4899 // ifdef'd out following patch from Paul Gammans 
4901                 // No need to touch column attributes, unless we 
4902                 // removed _all_ rows, in this case, we remove 
4903                 // all column attributes. 
4904                 // I hate to do this here, but the 
4905                 // needed data is not available inside UpdateAttrRows. 
4906                 if ( !GetNumberRows() ) 
4907                     attrProvider
->UpdateAttrCols( 0, -GetNumberCols() ); 
4911             if ( !GetBatchCount() ) 
4914                 m_rowLabelWin
->Refresh(); 
4920         case wxGRIDTABLE_NOTIFY_COLS_INSERTED
: 
4922             size_t pos 
= msg
.GetCommandInt(); 
4923             int numCols 
= msg
.GetCommandInt2(); 
4924             m_numCols 
+= numCols
; 
4926             if ( !m_colAt
.IsEmpty() ) 
4928                 //Shift the column IDs 
4930                 for ( i 
= 0; i 
< m_numCols 
- numCols
; i
++ ) 
4932                     if ( m_colAt
[i
] >= (int)pos 
) 
4933                         m_colAt
[i
] += numCols
; 
4936                 m_colAt
.Insert( pos
, pos
, numCols 
); 
4938                 //Set the new columns' positions 
4939                 for ( i 
= pos 
+ 1; i 
< (int)pos 
+ numCols
; i
++ ) 
4945             if ( !m_colWidths
.IsEmpty() ) 
4947                 m_colWidths
.Insert( m_defaultColWidth
, pos
, numCols 
); 
4948                 m_colRights
.Insert( 0, pos
, numCols 
); 
4952                     right 
= m_colRights
[GetColAt( pos 
- 1 )]; 
4955                 for ( colPos 
= pos
; colPos 
< m_numCols
; colPos
++ ) 
4957                     i 
= GetColAt( colPos 
); 
4959                     right 
+= m_colWidths
[i
]; 
4960                     m_colRights
[i
] = right
; 
4964             if ( m_currentCellCoords 
== wxGridNoCellCoords 
) 
4966                 // if we have just inserted cols into an empty grid the current 
4967                 // cell will be undefined... 
4969                 SetCurrentCell( 0, 0 ); 
4973                 m_selection
->UpdateCols( pos
, numCols 
); 
4974             wxGridCellAttrProvider 
* attrProvider 
= m_table
->GetAttrProvider(); 
4976                 attrProvider
->UpdateAttrCols( pos
, numCols 
); 
4977             if ( !GetBatchCount() ) 
4980                 m_colLabelWin
->Refresh(); 
4986         case wxGRIDTABLE_NOTIFY_COLS_APPENDED
: 
4988             int numCols 
= msg
.GetCommandInt(); 
4989             int oldNumCols 
= m_numCols
; 
4990             m_numCols 
+= numCols
; 
4992             if ( !m_colAt
.IsEmpty() ) 
4994                 m_colAt
.Add( 0, numCols 
); 
4996                 //Set the new columns' positions 
4998                 for ( i 
= oldNumCols
; i 
< m_numCols
; i
++ ) 
5004             if ( !m_colWidths
.IsEmpty() ) 
5006                 m_colWidths
.Add( m_defaultColWidth
, numCols 
); 
5007                 m_colRights
.Add( 0, numCols 
); 
5010                 if ( oldNumCols 
> 0 ) 
5011                     right 
= m_colRights
[GetColAt( oldNumCols 
- 1 )]; 
5014                 for ( colPos 
= oldNumCols
; colPos 
< m_numCols
; colPos
++ ) 
5016                     i 
= GetColAt( colPos 
); 
5018                     right 
+= m_colWidths
[i
]; 
5019                     m_colRights
[i
] = right
; 
5023             if ( m_currentCellCoords 
== wxGridNoCellCoords 
) 
5025                 // if we have just inserted cols into an empty grid the current 
5026                 // cell will be undefined... 
5028                 SetCurrentCell( 0, 0 ); 
5030             if ( !GetBatchCount() ) 
5033                 m_colLabelWin
->Refresh(); 
5039         case wxGRIDTABLE_NOTIFY_COLS_DELETED
: 
5041             size_t pos 
= msg
.GetCommandInt(); 
5042             int numCols 
= msg
.GetCommandInt2(); 
5043             m_numCols 
-= numCols
; 
5045             if ( !m_colAt
.IsEmpty() ) 
5047                 int colID 
= GetColAt( pos 
); 
5049                 m_colAt
.RemoveAt( pos
, numCols 
); 
5051                 //Shift the column IDs 
5053                 for ( colPos 
= 0; colPos 
< m_numCols
; colPos
++ ) 
5055                     if ( m_colAt
[colPos
] > colID 
) 
5056                         m_colAt
[colPos
] -= numCols
; 
5060             if ( !m_colWidths
.IsEmpty() ) 
5062                 m_colWidths
.RemoveAt( pos
, numCols 
); 
5063                 m_colRights
.RemoveAt( pos
, numCols 
); 
5067                 for ( colPos 
= 0; colPos 
< m_numCols
; colPos
++ ) 
5069                     i 
= GetColAt( colPos 
); 
5071                     w 
+= m_colWidths
[i
]; 
5078                 m_currentCellCoords 
= wxGridNoCellCoords
; 
5082                 if ( m_currentCellCoords
.GetCol() >= m_numCols 
) 
5083                   m_currentCellCoords
.Set( 0, 0 ); 
5087                 m_selection
->UpdateCols( pos
, -((int)numCols
) ); 
5088             wxGridCellAttrProvider 
* attrProvider 
= m_table
->GetAttrProvider(); 
5091                 attrProvider
->UpdateAttrCols( pos
, -((int)numCols
) ); 
5093 // ifdef'd out following patch from Paul Gammans 
5095                 // No need to touch row attributes, unless we 
5096                 // removed _all_ columns, in this case, we remove 
5097                 // all row attributes. 
5098                 // I hate to do this here, but the 
5099                 // needed data is not available inside UpdateAttrCols. 
5100                 if ( !GetNumberCols() ) 
5101                     attrProvider
->UpdateAttrRows( 0, -GetNumberRows() ); 
5105             if ( !GetBatchCount() ) 
5108                 m_colLabelWin
->Refresh(); 
5115     if (result 
&& !GetBatchCount() ) 
5116         m_gridWin
->Refresh(); 
5121 wxArrayInt 
wxGrid::CalcRowLabelsExposed( const wxRegion
& reg 
) const 
5123     wxRegionIterator 
iter( reg 
); 
5126     wxArrayInt  rowlabels
; 
5133         // TODO: remove this when we can... 
5134         // There is a bug in wxMotif that gives garbage update 
5135         // rectangles if you jump-scroll a long way by clicking the 
5136         // scrollbar with middle button.  This is a work-around 
5138 #if defined(__WXMOTIF__) 
5140         m_gridWin
->GetClientSize( &cw
, &ch 
); 
5141         if ( r
.GetTop() > ch 
) 
5143         r
.SetBottom( wxMin( r
.GetBottom(), ch 
) ); 
5146         // logical bounds of update region 
5149         CalcUnscrolledPosition( 0, r
.GetTop(), &dummy
, &top 
); 
5150         CalcUnscrolledPosition( 0, r
.GetBottom(), &dummy
, &bottom 
); 
5152         // find the row labels within these bounds 
5155         for ( row 
= internalYToRow(top
); row 
< m_numRows
; row
++ ) 
5157             if ( GetRowBottom(row
) < top 
) 
5160             if ( GetRowTop(row
) > bottom 
) 
5163             rowlabels
.Add( row 
); 
5172 wxArrayInt 
wxGrid::CalcColLabelsExposed( const wxRegion
& reg 
) const 
5174     wxRegionIterator 
iter( reg 
); 
5177     wxArrayInt colLabels
; 
5184         // TODO: remove this when we can... 
5185         // There is a bug in wxMotif that gives garbage update 
5186         // rectangles if you jump-scroll a long way by clicking the 
5187         // scrollbar with middle button.  This is a work-around 
5189 #if defined(__WXMOTIF__) 
5191         m_gridWin
->GetClientSize( &cw
, &ch 
); 
5192         if ( r
.GetLeft() > cw 
) 
5194         r
.SetRight( wxMin( r
.GetRight(), cw 
) ); 
5197         // logical bounds of update region 
5200         CalcUnscrolledPosition( r
.GetLeft(), 0, &left
, &dummy 
); 
5201         CalcUnscrolledPosition( r
.GetRight(), 0, &right
, &dummy 
); 
5203         // find the cells within these bounds 
5207         for ( colPos 
= GetColPos( internalXToCol(left
) ); colPos 
< m_numCols
; colPos
++ ) 
5209             col 
= GetColAt( colPos 
); 
5211             if ( GetColRight(col
) < left 
) 
5214             if ( GetColLeft(col
) > right 
) 
5217             colLabels
.Add( col 
); 
5226 wxGridCellCoordsArray 
wxGrid::CalcCellsExposed( const wxRegion
& reg 
) const 
5228     wxRegionIterator 
iter( reg 
); 
5231     wxGridCellCoordsArray  cellsExposed
; 
5233     int left
, top
, right
, bottom
; 
5238         // TODO: remove this when we can... 
5239         // There is a bug in wxMotif that gives garbage update 
5240         // rectangles if you jump-scroll a long way by clicking the 
5241         // scrollbar with middle button.  This is a work-around 
5243 #if defined(__WXMOTIF__) 
5245         m_gridWin
->GetClientSize( &cw
, &ch 
); 
5246         if ( r
.GetTop() > ch 
) r
.SetTop( 0 ); 
5247         if ( r
.GetLeft() > cw 
) r
.SetLeft( 0 ); 
5248         r
.SetRight( wxMin( r
.GetRight(), cw 
) ); 
5249         r
.SetBottom( wxMin( r
.GetBottom(), ch 
) ); 
5252         // logical bounds of update region 
5254         CalcUnscrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top 
); 
5255         CalcUnscrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom 
); 
5257         // find the cells within these bounds 
5260         for ( row 
= internalYToRow(top
); row 
< m_numRows
; row
++ ) 
5262             if ( GetRowBottom(row
) <= top 
) 
5265             if ( GetRowTop(row
) > bottom 
) 
5269             for ( colPos 
= GetColPos( internalXToCol(left
) ); colPos 
< m_numCols
; colPos
++ ) 
5271                 col 
= GetColAt( colPos 
); 
5273                 if ( GetColRight(col
) <= left 
) 
5276                 if ( GetColLeft(col
) > right 
) 
5279                 cellsExposed
.Add( wxGridCellCoords( row
, col 
) ); 
5286     return cellsExposed
; 
5290 void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent
& event 
) 
5293     wxPoint 
pos( event
.GetPosition() ); 
5294     CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y 
); 
5296     if ( event
.Dragging() ) 
5300             m_isDragging 
= true; 
5301             m_rowLabelWin
->CaptureMouse(); 
5304         if ( event
.LeftIsDown() ) 
5306             switch ( m_cursorMode 
) 
5308                 case WXGRID_CURSOR_RESIZE_ROW
: 
5310                     int cw
, ch
, left
, dummy
; 
5311                     m_gridWin
->GetClientSize( &cw
, &ch 
); 
5312                     CalcUnscrolledPosition( 0, 0, &left
, &dummy 
); 
5314                     wxClientDC 
dc( m_gridWin 
); 
5317                                GetRowTop(m_dragRowOrCol
) + 
5318                                GetRowMinimalHeight(m_dragRowOrCol
) ); 
5319                     dc
.SetLogicalFunction(wxINVERT
); 
5320                     if ( m_dragLastPos 
>= 0 ) 
5322                         dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos 
); 
5324                     dc
.DrawLine( left
, y
, left
+cw
, y 
); 
5329                 case WXGRID_CURSOR_SELECT_ROW
: 
5331                     if ( (row 
= YToRow( y 
)) >= 0 ) 
5335                             m_selection
->SelectRow( row
, 
5336                                                     event
.ControlDown(), 
5345                 // default label to suppress warnings about "enumeration value 
5346                 // 'xxx' not handled in switch 
5354     if ( m_isDragging 
&& (event
.Entering() || event
.Leaving()) ) 
5359         if (m_rowLabelWin
->HasCapture()) 
5360             m_rowLabelWin
->ReleaseMouse(); 
5361         m_isDragging 
= false; 
5364     // ------------ Entering or leaving the window 
5366     if ( event
.Entering() || event
.Leaving() ) 
5368         ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
); 
5371     // ------------ Left button pressed 
5373     else if ( event
.LeftDown() ) 
5375         // don't send a label click event for a hit on the 
5376         // edge of the row label - this is probably the user 
5377         // wanting to resize the row 
5379         if ( YToEdgeOfRow(y
) < 0 ) 
5383                  !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, row
, -1, event 
) ) 
5385                 if ( !event
.ShiftDown() && !event
.CmdDown() ) 
5389                     if ( event
.ShiftDown() ) 
5391                         m_selection
->SelectBlock( m_currentCellCoords
.GetRow(), 
5394                                                   GetNumberCols() - 1, 
5395                                                   event
.ControlDown(), 
5402                         m_selection
->SelectRow( row
, 
5403                                                 event
.ControlDown(), 
5410                 ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW
, m_rowLabelWin
); 
5415             // starting to drag-resize a row 
5416             if ( CanDragRowSize() ) 
5417                 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
); 
5421     // ------------ Left double click 
5423     else if (event
.LeftDClick() ) 
5425         row 
= YToEdgeOfRow(y
); 
5430                  !SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, row
, -1, event 
) ) 
5432                 // no default action at the moment 
5437             // adjust row height depending on label text 
5438             AutoSizeRowLabelSize( row 
); 
5440             ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
); 
5445     // ------------ Left button released 
5447     else if ( event
.LeftUp() ) 
5449         if ( m_cursorMode 
== WXGRID_CURSOR_RESIZE_ROW 
) 
5451             DoEndDragResizeRow(); 
5453             // Note: we are ending the event *after* doing 
5454             // default processing in this case 
5456             SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event 
); 
5459         ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
); 
5463     // ------------ Right button down 
5465     else if ( event
.RightDown() ) 
5469              !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, row
, -1, event 
) ) 
5471             // no default action at the moment 
5475     // ------------ Right double click 
5477     else if ( event
.RightDClick() ) 
5481              !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, row
, -1, event 
) ) 
5483             // no default action at the moment 
5487     // ------------ No buttons down and mouse moving 
5489     else if ( event
.Moving() ) 
5491         m_dragRowOrCol 
= YToEdgeOfRow( y 
); 
5492         if ( m_dragRowOrCol 
>= 0 ) 
5494             if ( m_cursorMode 
== WXGRID_CURSOR_SELECT_CELL 
) 
5496                 // don't capture the mouse yet 
5497                 if ( CanDragRowSize() ) 
5498                     ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
, false); 
5501         else if ( m_cursorMode 
!= WXGRID_CURSOR_SELECT_CELL 
) 
5503             ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
, false); 
5508 void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent
& event 
) 
5511     wxPoint 
pos( event
.GetPosition() ); 
5512     CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y 
); 
5514     if ( event
.Dragging() ) 
5518             m_isDragging 
= true; 
5519             m_colLabelWin
->CaptureMouse(); 
5521             if ( m_cursorMode 
== WXGRID_CURSOR_MOVE_COL 
) 
5522                 m_dragRowOrCol 
= XToCol( x 
); 
5525         if ( event
.LeftIsDown() ) 
5527             switch ( m_cursorMode 
) 
5529                 case WXGRID_CURSOR_RESIZE_COL
: 
5531                     int cw
, ch
, dummy
, top
; 
5532                     m_gridWin
->GetClientSize( &cw
, &ch 
); 
5533                     CalcUnscrolledPosition( 0, 0, &dummy
, &top 
); 
5535                     wxClientDC 
dc( m_gridWin 
); 
5538                     x 
= wxMax( x
, GetColLeft(m_dragRowOrCol
) + 
5539                                   GetColMinimalWidth(m_dragRowOrCol
)); 
5540                     dc
.SetLogicalFunction(wxINVERT
); 
5541                     if ( m_dragLastPos 
>= 0 ) 
5543                         dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top 
+ ch 
); 
5545                     dc
.DrawLine( x
, top
, x
, top 
+ ch 
); 
5550                 case WXGRID_CURSOR_SELECT_COL
: 
5552                     if ( (col 
= XToCol( x 
)) >= 0 ) 
5556                             m_selection
->SelectCol( col
, 
5557                                                     event
.ControlDown(), 
5566                 case WXGRID_CURSOR_MOVE_COL
: 
5569                         m_moveToCol 
= GetColAt( 0 ); 
5571                         m_moveToCol 
= XToCol( x 
); 
5575                     if ( m_moveToCol 
< 0 ) 
5576                         markerX 
= GetColRight( GetColAt( m_numCols 
- 1 ) ); 
5578                         markerX 
= GetColLeft( m_moveToCol 
); 
5580                     if ( markerX 
!= m_dragLastPos 
) 
5582                         wxClientDC 
dc( m_colLabelWin 
); 
5585                         m_colLabelWin
->GetClientSize( &cw
, &ch 
); 
5589                         //Clean up the last indicator 
5590                         if ( m_dragLastPos 
>= 0 ) 
5592                             wxPen 
pen( m_colLabelWin
->GetBackgroundColour(), 2 ); 
5594                             dc
.DrawLine( m_dragLastPos 
+ 1, 0, m_dragLastPos 
+ 1, ch 
); 
5595                             dc
.SetPen(wxNullPen
); 
5597                             if ( XToCol( m_dragLastPos 
) != -1 ) 
5598                                 DrawColLabel( dc
, XToCol( m_dragLastPos 
) ); 
5601                         //Moving to the same place? Don't draw a marker 
5602                         if ( (m_moveToCol 
== m_dragRowOrCol
) 
5603                           || (GetColPos( m_moveToCol 
) == GetColPos( m_dragRowOrCol 
) + 1) 
5604                           || (m_moveToCol 
< 0 && m_dragRowOrCol 
== GetColAt( m_numCols 
- 1 ))) 
5611                         wxPen 
pen( *wxBLUE
, 2 ); 
5614                         dc
.DrawLine( markerX
, 0, markerX
, ch 
); 
5616                         dc
.SetPen(wxNullPen
); 
5618                         m_dragLastPos 
= markerX 
- 1; 
5623                 // default label to suppress warnings about "enumeration value 
5624                 // 'xxx' not handled in switch 
5632     if ( m_isDragging 
&& (event
.Entering() || event
.Leaving()) ) 
5637         if (m_colLabelWin
->HasCapture()) 
5638             m_colLabelWin
->ReleaseMouse(); 
5639         m_isDragging 
= false; 
5642     // ------------ Entering or leaving the window 
5644     if ( event
.Entering() || event
.Leaving() ) 
5646         ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
); 
5649     // ------------ Left button pressed 
5651     else if ( event
.LeftDown() ) 
5653         // don't send a label click event for a hit on the 
5654         // edge of the col label - this is probably the user 
5655         // wanting to resize the col 
5657         if ( XToEdgeOfCol(x
) < 0 ) 
5661                  !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, col
, event 
) ) 
5663                 if ( m_canDragColMove 
) 
5665                     //Show button as pressed 
5666                     wxClientDC 
dc( m_colLabelWin 
); 
5667                     int colLeft 
= GetColLeft( col 
); 
5668                     int colRight 
= GetColRight( col 
) - 1; 
5669                     dc
.SetPen( wxPen( m_colLabelWin
->GetBackgroundColour(), 1 ) ); 
5670                     dc
.DrawLine( colLeft
, 1, colLeft
, m_colLabelHeight
-1 ); 
5671                     dc
.DrawLine( colLeft
, 1, colRight
, 1 ); 
5673                     ChangeCursorMode(WXGRID_CURSOR_MOVE_COL
, m_colLabelWin
); 
5677                     if ( !event
.ShiftDown() && !event
.CmdDown() ) 
5681                         if ( event
.ShiftDown() ) 
5683                             m_selection
->SelectBlock( 0, 
5684                                                       m_currentCellCoords
.GetCol(), 
5685                                                       GetNumberRows() - 1, col
, 
5686                                                       event
.ControlDown(), 
5693                             m_selection
->SelectCol( col
, 
5694                                                     event
.ControlDown(), 
5701                     ChangeCursorMode(WXGRID_CURSOR_SELECT_COL
, m_colLabelWin
); 
5707             // starting to drag-resize a col 
5709             if ( CanDragColSize() ) 
5710                 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
); 
5714     // ------------ Left double click 
5716     if ( event
.LeftDClick() ) 
5718         col 
= XToEdgeOfCol(x
); 
5723                  ! SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, col
, event 
) ) 
5725                 // no default action at the moment 
5730             // adjust column width depending on label text 
5731             AutoSizeColLabelSize( col 
); 
5733             ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
); 
5738     // ------------ Left button released 
5740     else if ( event
.LeftUp() ) 
5742         switch ( m_cursorMode 
) 
5744             case WXGRID_CURSOR_RESIZE_COL
: 
5745                 DoEndDragResizeCol(); 
5747                 // Note: we are ending the event *after* doing 
5748                 // default processing in this case 
5750                 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event 
); 
5753             case WXGRID_CURSOR_MOVE_COL
: 
5756                 SendEvent( wxEVT_GRID_COL_MOVE
, -1, m_dragRowOrCol
, event 
); 
5759             case WXGRID_CURSOR_SELECT_COL
: 
5760             case WXGRID_CURSOR_SELECT_CELL
: 
5761             case WXGRID_CURSOR_RESIZE_ROW
: 
5762             case WXGRID_CURSOR_SELECT_ROW
: 
5763                 // nothing to do (?) 
5767         ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
); 
5771     // ------------ Right button down 
5773     else if ( event
.RightDown() ) 
5777              !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, col
, event 
) ) 
5779             // no default action at the moment 
5783     // ------------ Right double click 
5785     else if ( event
.RightDClick() ) 
5789              !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, col
, event 
) ) 
5791             // no default action at the moment 
5795     // ------------ No buttons down and mouse moving 
5797     else if ( event
.Moving() ) 
5799         m_dragRowOrCol 
= XToEdgeOfCol( x 
); 
5800         if ( m_dragRowOrCol 
>= 0 ) 
5802             if ( m_cursorMode 
== WXGRID_CURSOR_SELECT_CELL 
) 
5804                 // don't capture the cursor yet 
5805                 if ( CanDragColSize() ) 
5806                     ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
, false); 
5809         else if ( m_cursorMode 
!= WXGRID_CURSOR_SELECT_CELL 
) 
5811             ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
, false); 
5816 void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent
& event 
) 
5818     if ( event
.LeftDown() ) 
5820         // indicate corner label by having both row and 
5823         if ( !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, -1, event 
) ) 
5828     else if ( event
.LeftDClick() ) 
5830         SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, -1, event 
); 
5832     else if ( event
.RightDown() ) 
5834         if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, -1, event 
) ) 
5836             // no default action at the moment 
5839     else if ( event
.RightDClick() ) 
5841         if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, -1, event 
) ) 
5843             // no default action at the moment 
5848 void wxGrid::CancelMouseCapture() 
5850     // cancel operation currently in progress, whatever it is 
5853         m_isDragging 
= false; 
5854         m_cursorMode 
= WXGRID_CURSOR_SELECT_CELL
; 
5855         m_winCapture
->SetCursor( *wxSTANDARD_CURSOR 
); 
5856         m_winCapture 
= NULL
; 
5858         // remove traces of whatever we drew on screen 
5863 void wxGrid::ChangeCursorMode(CursorMode mode
, 
5868     static const wxChar 
*cursorModes
[] = 
5878     wxLogTrace(_T("grid"), 
5879                _T("wxGrid cursor mode (mouse capture for %s): %s -> %s"), 
5880                win 
== m_colLabelWin 
? _T("colLabelWin") 
5881                                     : win 
? _T("rowLabelWin") 
5883                cursorModes
[m_cursorMode
], cursorModes
[mode
]); 
5886     if ( mode 
== m_cursorMode 
&& 
5887          win 
== m_winCapture 
&& 
5888          captureMouse 
== (m_winCapture 
!= NULL
)) 
5893         // by default use the grid itself 
5899         if (m_winCapture
->HasCapture()) 
5900             m_winCapture
->ReleaseMouse(); 
5901         m_winCapture 
= (wxWindow 
*)NULL
; 
5904     m_cursorMode 
= mode
; 
5906     switch ( m_cursorMode 
) 
5908         case WXGRID_CURSOR_RESIZE_ROW
: 
5909             win
->SetCursor( m_rowResizeCursor 
); 
5912         case WXGRID_CURSOR_RESIZE_COL
: 
5913             win
->SetCursor( m_colResizeCursor 
); 
5916         case WXGRID_CURSOR_MOVE_COL
: 
5917             win
->SetCursor( wxCursor(wxCURSOR_HAND
) ); 
5921             win
->SetCursor( *wxSTANDARD_CURSOR 
); 
5925     // we need to capture mouse when resizing 
5926     bool resize 
= m_cursorMode 
== WXGRID_CURSOR_RESIZE_ROW 
|| 
5927                   m_cursorMode 
== WXGRID_CURSOR_RESIZE_COL
; 
5929     if ( captureMouse 
&& resize 
) 
5931         win
->CaptureMouse(); 
5936 void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent
& event 
) 
5939     wxPoint 
pos( event
.GetPosition() ); 
5940     CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y 
); 
5942     wxGridCellCoords coords
; 
5943     XYToCell( x
, y
, coords 
); 
5945     int cell_rows
, cell_cols
; 
5946     bool isFirstDrag 
= !m_isDragging
; 
5947     GetCellSize( coords
.GetRow(), coords
.GetCol(), &cell_rows
, &cell_cols 
); 
5948     if ((cell_rows 
< 0) || (cell_cols 
< 0)) 
5950         coords
.SetRow(coords
.GetRow() + cell_rows
); 
5951         coords
.SetCol(coords
.GetCol() + cell_cols
); 
5954     if ( event
.Dragging() ) 
5956         //wxLogDebug("pos(%d, %d) coords(%d, %d)", pos.x, pos.y, coords.GetRow(), coords.GetCol()); 
5958         // Don't start doing anything until the mouse has been dragged at 
5959         // least 3 pixels in any direction... 
5962             if (m_startDragPos 
== wxDefaultPosition
) 
5964                 m_startDragPos 
= pos
; 
5967             if (abs(m_startDragPos
.x 
- pos
.x
) < 4 && abs(m_startDragPos
.y 
- pos
.y
) < 4) 
5971         m_isDragging 
= true; 
5972         if ( m_cursorMode 
== WXGRID_CURSOR_SELECT_CELL 
) 
5974             // Hide the edit control, so it 
5975             // won't interfere with drag-shrinking. 
5976             if ( IsCellEditControlShown() ) 
5978                 HideCellEditControl(); 
5979                 SaveEditControlValue(); 
5982             if ( coords 
!= wxGridNoCellCoords 
) 
5984                 if ( event
.CmdDown() ) 
5986                     if ( m_selectingKeyboard 
== wxGridNoCellCoords
) 
5987                         m_selectingKeyboard 
= coords
; 
5988                     HighlightBlock( m_selectingKeyboard
, coords 
); 
5990                 else if ( CanDragCell() ) 
5994                         if ( m_selectingKeyboard 
== wxGridNoCellCoords
) 
5995                             m_selectingKeyboard 
= coords
; 
5997                         SendEvent( wxEVT_GRID_CELL_BEGIN_DRAG
, 
6006                     if ( !IsSelection() ) 
6008                         HighlightBlock( coords
, coords 
); 
6012                         HighlightBlock( m_currentCellCoords
, coords 
); 
6016                 if (! IsVisible(coords
)) 
6018                     MakeCellVisible(coords
); 
6019                     // TODO: need to introduce a delay or something here.  The 
6020                     // scrolling is way to fast, at least on MSW - also on GTK. 
6023             // Have we captured the mouse yet? 
6026                 m_winCapture 
= m_gridWin
; 
6027                 m_winCapture
->CaptureMouse(); 
6032         else if ( event
.LeftIsDown() && 
6033                   m_cursorMode 
== WXGRID_CURSOR_RESIZE_ROW 
) 
6035             int cw
, ch
, left
, dummy
; 
6036             m_gridWin
->GetClientSize( &cw
, &ch 
); 
6037             CalcUnscrolledPosition( 0, 0, &left
, &dummy 
); 
6039             wxClientDC 
dc( m_gridWin 
); 
6041             y 
= wxMax( y
, GetRowTop(m_dragRowOrCol
) + 
6042                           GetRowMinimalHeight(m_dragRowOrCol
) ); 
6043             dc
.SetLogicalFunction(wxINVERT
); 
6044             if ( m_dragLastPos 
>= 0 ) 
6046                 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos 
); 
6048             dc
.DrawLine( left
, y
, left
+cw
, y 
); 
6051         else if ( event
.LeftIsDown() && 
6052                   m_cursorMode 
== WXGRID_CURSOR_RESIZE_COL 
) 
6054             int cw
, ch
, dummy
, top
; 
6055             m_gridWin
->GetClientSize( &cw
, &ch 
); 
6056             CalcUnscrolledPosition( 0, 0, &dummy
, &top 
); 
6058             wxClientDC 
dc( m_gridWin 
); 
6060             x 
= wxMax( x
, GetColLeft(m_dragRowOrCol
) + 
6061                           GetColMinimalWidth(m_dragRowOrCol
) ); 
6062             dc
.SetLogicalFunction(wxINVERT
); 
6063             if ( m_dragLastPos 
>= 0 ) 
6065                 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top 
+ ch 
); 
6067             dc
.DrawLine( x
, top
, x
, top 
+ ch 
); 
6074     m_isDragging 
= false; 
6075     m_startDragPos 
= wxDefaultPosition
; 
6077     // VZ: if we do this, the mode is reset to WXGRID_CURSOR_SELECT_CELL 
6078     //     immediately after it becomes WXGRID_CURSOR_RESIZE_ROW/COL under 
6081     if ( event
.Entering() || event
.Leaving() ) 
6083         ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
); 
6084         m_gridWin
->SetCursor( *wxSTANDARD_CURSOR 
); 
6089     // ------------ Left button pressed 
6091     if ( event
.LeftDown() && coords 
!= wxGridNoCellCoords 
) 
6093         if ( !SendEvent( wxEVT_GRID_CELL_LEFT_CLICK
, 
6098             if ( !event
.CmdDown() ) 
6100             if ( event
.ShiftDown() ) 
6104                     m_selection
->SelectBlock( m_currentCellCoords
.GetRow(), 
6105                                               m_currentCellCoords
.GetCol(), 
6108                                               event
.ControlDown(), 
6114             else if ( XToEdgeOfCol(x
) < 0 && 
6115                       YToEdgeOfRow(y
) < 0 ) 
6117                 DisableCellEditControl(); 
6118                 MakeCellVisible( coords 
); 
6120                 if ( event
.CmdDown() ) 
6124                         m_selection
->ToggleCellSelection( coords
.GetRow(), 
6126                                                           event
.ControlDown(), 
6131                     m_selectingTopLeft 
= wxGridNoCellCoords
; 
6132                     m_selectingBottomRight 
= wxGridNoCellCoords
; 
6133                     m_selectingKeyboard 
= coords
; 
6137                     m_waitForSlowClick 
= m_currentCellCoords 
== coords 
&& coords 
!= wxGridNoCellCoords
; 
6138                     SetCurrentCell( coords 
); 
6141                         if ( m_selection
->GetSelectionMode() != 
6142                                 wxGrid::wxGridSelectCells 
) 
6144                             HighlightBlock( coords
, coords 
); 
6152     // ------------ Left double click 
6154     else if ( event
.LeftDClick() && coords 
!= wxGridNoCellCoords 
) 
6156         DisableCellEditControl(); 
6158         if ( XToEdgeOfCol(x
) < 0 && YToEdgeOfRow(y
) < 0 ) 
6160             if ( !SendEvent( wxEVT_GRID_CELL_LEFT_DCLICK
, 
6165                 // we want double click to select a cell and start editing 
6166                 // (i.e. to behave in same way as sequence of two slow clicks): 
6167                 m_waitForSlowClick 
= true; 
6172     // ------------ Left button released 
6174     else if ( event
.LeftUp() ) 
6176         if ( m_cursorMode 
== WXGRID_CURSOR_SELECT_CELL 
) 
6180                 if (m_winCapture
->HasCapture()) 
6181                     m_winCapture
->ReleaseMouse(); 
6182                 m_winCapture 
= NULL
; 
6185             if ( coords 
== m_currentCellCoords 
&& m_waitForSlowClick 
&& CanEnableCellControl() ) 
6188                 EnableCellEditControl(); 
6190                 wxGridCellAttr 
*attr 
= GetCellAttr(coords
); 
6191                 wxGridCellEditor 
*editor 
= attr
->GetEditor(this, coords
.GetRow(), coords
.GetCol()); 
6192                 editor
->StartingClick(); 
6196                 m_waitForSlowClick 
= false; 
6198             else if ( m_selectingTopLeft 
!= wxGridNoCellCoords 
&& 
6199                  m_selectingBottomRight 
!= wxGridNoCellCoords 
) 
6203                     m_selection
->SelectBlock( m_selectingTopLeft
.GetRow(), 
6204                                               m_selectingTopLeft
.GetCol(), 
6205                                               m_selectingBottomRight
.GetRow(), 
6206                                               m_selectingBottomRight
.GetCol(), 
6207                                               event
.ControlDown(), 
6213                 m_selectingTopLeft 
= wxGridNoCellCoords
; 
6214                 m_selectingBottomRight 
= wxGridNoCellCoords
; 
6216                 // Show the edit control, if it has been hidden for 
6218                 ShowCellEditControl(); 
6221         else if ( m_cursorMode 
== WXGRID_CURSOR_RESIZE_ROW 
) 
6223             ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
); 
6224             DoEndDragResizeRow(); 
6226             // Note: we are ending the event *after* doing 
6227             // default processing in this case 
6229             SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event 
); 
6231         else if ( m_cursorMode 
== WXGRID_CURSOR_RESIZE_COL 
) 
6233             ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
); 
6234             DoEndDragResizeCol(); 
6236             // Note: we are ending the event *after* doing 
6237             // default processing in this case 
6239             SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event 
); 
6245     // ------------ Right button down 
6247     else if ( event
.RightDown() && coords 
!= wxGridNoCellCoords 
) 
6249         DisableCellEditControl(); 
6250         if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_CLICK
, 
6255             // no default action at the moment 
6259     // ------------ Right double click 
6261     else if ( event
.RightDClick() && coords 
!= wxGridNoCellCoords 
) 
6263         DisableCellEditControl(); 
6264         if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_DCLICK
, 
6269             // no default action at the moment 
6273     // ------------ Moving and no button action 
6275     else if ( event
.Moving() && !event
.IsButton() ) 
6277         if ( coords
.GetRow() < 0 || coords
.GetCol() < 0 ) 
6279             // out of grid cell area 
6280             ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
); 
6284         int dragRow 
= YToEdgeOfRow( y 
); 
6285         int dragCol 
= XToEdgeOfCol( x 
); 
6287         // Dragging on the corner of a cell to resize in both 
6288         // directions is not implemented yet... 
6290         if ( dragRow 
>= 0 && dragCol 
>= 0 ) 
6292             ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
); 
6298             m_dragRowOrCol 
= dragRow
; 
6300             if ( m_cursorMode 
== WXGRID_CURSOR_SELECT_CELL 
) 
6302                 if ( CanDragRowSize() && CanDragGridSize() ) 
6303                     ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, NULL
, false); 
6306         else if ( dragCol 
>= 0 ) 
6308             m_dragRowOrCol 
= dragCol
; 
6310             if ( m_cursorMode 
== WXGRID_CURSOR_SELECT_CELL 
) 
6312                 if ( CanDragColSize() && CanDragGridSize() ) 
6313                     ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, NULL
, false); 
6316         else // Neither on a row or col edge 
6318             if ( m_cursorMode 
!= WXGRID_CURSOR_SELECT_CELL 
) 
6320                 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
); 
6326 void wxGrid::DoEndDragResizeRow() 
6328     if ( m_dragLastPos 
>= 0 ) 
6330         // erase the last line and resize the row 
6332         int cw
, ch
, left
, dummy
; 
6333         m_gridWin
->GetClientSize( &cw
, &ch 
); 
6334         CalcUnscrolledPosition( 0, 0, &left
, &dummy 
); 
6336         wxClientDC 
dc( m_gridWin 
); 
6338         dc
.SetLogicalFunction( wxINVERT 
); 
6339         dc
.DrawLine( left
, m_dragLastPos
, left 
+ cw
, m_dragLastPos 
); 
6340         HideCellEditControl(); 
6341         SaveEditControlValue(); 
6343         int rowTop 
= GetRowTop(m_dragRowOrCol
); 
6344         SetRowSize( m_dragRowOrCol
, 
6345                     wxMax( m_dragLastPos 
- rowTop
, m_minAcceptableRowHeight 
) ); 
6347         if ( !GetBatchCount() ) 
6349             // Only needed to get the correct rect.y: 
6350             wxRect 
rect ( CellToRect( m_dragRowOrCol
, 0 ) ); 
6352             CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
); 
6353             rect
.width 
= m_rowLabelWidth
; 
6354             rect
.height 
= ch 
- rect
.y
; 
6355             m_rowLabelWin
->Refresh( true, &rect 
); 
6358             // if there is a multicell block, paint all of it 
6361                 int i
, cell_rows
, cell_cols
, subtract_rows 
= 0; 
6362                 int leftCol 
= XToCol(left
); 
6363                 int rightCol 
= internalXToCol(left 
+ cw
); 
6366                     for (i
=leftCol
; i
<rightCol
; i
++) 
6368                         GetCellSize(m_dragRowOrCol
, i
, &cell_rows
, &cell_cols
); 
6369                         if (cell_rows 
< subtract_rows
) 
6370                             subtract_rows 
= cell_rows
; 
6372                     rect
.y 
= GetRowTop(m_dragRowOrCol 
+ subtract_rows
); 
6373                     CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
); 
6374                     rect
.height 
= ch 
- rect
.y
; 
6377             m_gridWin
->Refresh( false, &rect 
); 
6380         ShowCellEditControl(); 
6385 void wxGrid::DoEndDragResizeCol() 
6387     if ( m_dragLastPos 
>= 0 ) 
6389         // erase the last line and resize the col 
6391         int cw
, ch
, dummy
, top
; 
6392         m_gridWin
->GetClientSize( &cw
, &ch 
); 
6393         CalcUnscrolledPosition( 0, 0, &dummy
, &top 
); 
6395         wxClientDC 
dc( m_gridWin 
); 
6397         dc
.SetLogicalFunction( wxINVERT 
); 
6398         dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top 
+ ch 
); 
6399         HideCellEditControl(); 
6400         SaveEditControlValue(); 
6402         int colLeft 
= GetColLeft(m_dragRowOrCol
); 
6403         SetColSize( m_dragRowOrCol
, 
6404                     wxMax( m_dragLastPos 
- colLeft
, 
6405                            GetColMinimalWidth(m_dragRowOrCol
) ) ); 
6407         if ( !GetBatchCount() ) 
6409             // Only needed to get the correct rect.x: 
6410             wxRect 
rect ( CellToRect( 0, m_dragRowOrCol 
) ); 
6412             CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
); 
6413             rect
.width 
= cw 
- rect
.x
; 
6414             rect
.height 
= m_colLabelHeight
; 
6415             m_colLabelWin
->Refresh( true, &rect 
); 
6418             // if there is a multicell block, paint all of it 
6421                 int i
, cell_rows
, cell_cols
, subtract_cols 
= 0; 
6422                 int topRow 
= YToRow(top
); 
6423                 int bottomRow 
= internalYToRow(top 
+ cw
); 
6426                     for (i
=topRow
; i
<bottomRow
; i
++) 
6428                         GetCellSize(i
, m_dragRowOrCol
, &cell_rows
, &cell_cols
); 
6429                         if (cell_cols 
< subtract_cols
) 
6430                             subtract_cols 
= cell_cols
; 
6433                     rect
.x 
= GetColLeft(m_dragRowOrCol 
+ subtract_cols
); 
6434                     CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
); 
6435                     rect
.width 
= cw 
- rect
.x
; 
6439             m_gridWin
->Refresh( false, &rect 
); 
6442         ShowCellEditControl(); 
6446 void wxGrid::DoEndDragMoveCol() 
6448     //The user clicked on the column but didn't actually drag 
6449     if ( m_dragLastPos 
< 0 ) 
6451         m_colLabelWin
->Refresh();   //Do this to "unpress" the column 
6456     if ( m_moveToCol 
== -1 ) 
6457         newPos 
= m_numCols 
- 1; 
6460         newPos 
= GetColPos( m_moveToCol 
); 
6461         if ( newPos 
> GetColPos( m_dragRowOrCol 
) ) 
6465     SetColPos( m_dragRowOrCol
, newPos 
); 
6468 void wxGrid::SetColPos( int colID
, int newPos 
) 
6470     if ( m_colAt
.IsEmpty() ) 
6472         m_colAt
.Alloc( m_numCols 
); 
6475         for ( i 
= 0; i 
< m_numCols
; i
++ ) 
6481     int oldPos 
= GetColPos( colID 
); 
6483     //Reshuffle the m_colAt array 
6484     if ( newPos 
> oldPos 
) 
6487         for ( i 
= oldPos
; i 
< newPos
; i
++ ) 
6489             m_colAt
[i
] = m_colAt
[i
+1]; 
6495         for ( i 
= oldPos
; i 
> newPos
; i
-- ) 
6497             m_colAt
[i
] = m_colAt
[i
-1]; 
6501     m_colAt
[newPos
] = colID
; 
6503     //Recalculate the column rights 
6504     if ( !m_colWidths
.IsEmpty() ) 
6508         for ( colPos 
= 0; colPos 
< m_numCols
; colPos
++ ) 
6510             int colID 
= GetColAt( colPos 
); 
6512             colRight 
+= m_colWidths
[colID
]; 
6513             m_colRights
[colID
] = colRight
; 
6517     m_colLabelWin
->Refresh(); 
6518     m_gridWin
->Refresh(); 
6523 void wxGrid::EnableDragColMove( bool enable 
) 
6525     if ( m_canDragColMove 
== enable 
) 
6528     m_canDragColMove 
= enable
; 
6530     if ( !m_canDragColMove 
) 
6534         //Recalculate the column rights 
6535         if ( !m_colWidths
.IsEmpty() ) 
6539             for ( colPos 
= 0; colPos 
< m_numCols
; colPos
++ ) 
6541                 colRight 
+= m_colWidths
[colPos
]; 
6542                 m_colRights
[colPos
] = colRight
; 
6546         m_colLabelWin
->Refresh(); 
6547         m_gridWin
->Refresh(); 
6553 // ------ interaction with data model 
6555 bool wxGrid::ProcessTableMessage( wxGridTableMessage
& msg 
) 
6557     switch ( msg
.GetId() ) 
6559         case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES
: 
6560             return GetModelValues(); 
6562         case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES
: 
6563             return SetModelValues(); 
6565         case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
: 
6566         case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
: 
6567         case wxGRIDTABLE_NOTIFY_ROWS_DELETED
: 
6568         case wxGRIDTABLE_NOTIFY_COLS_INSERTED
: 
6569         case wxGRIDTABLE_NOTIFY_COLS_APPENDED
: 
6570         case wxGRIDTABLE_NOTIFY_COLS_DELETED
: 
6571             return Redimension( msg 
); 
6578 // The behaviour of this function depends on the grid table class 
6579 // Clear() function. For the default wxGridStringTable class the 
6580 // behavious is to replace all cell contents with wxEmptyString but 
6581 // not to change the number of rows or cols. 
6583 void wxGrid::ClearGrid() 
6587         if (IsCellEditControlEnabled()) 
6588             DisableCellEditControl(); 
6591         if (!GetBatchCount()) 
6592             m_gridWin
->Refresh(); 
6596 bool wxGrid::InsertRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) ) 
6598     // TODO: something with updateLabels flag 
6602         wxFAIL_MSG( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") ); 
6608         if (IsCellEditControlEnabled()) 
6609             DisableCellEditControl(); 
6611         bool done 
= m_table
->InsertRows( pos
, numRows 
); 
6614         // the table will have sent the results of the insert row 
6615         // operation to this view object as a grid table message 
6621 bool wxGrid::AppendRows( int numRows
, bool WXUNUSED(updateLabels
) ) 
6623     // TODO: something with updateLabels flag 
6627         wxFAIL_MSG( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") ); 
6633         bool done 
= m_table 
&& m_table
->AppendRows( numRows 
); 
6636         // the table will have sent the results of the append row 
6637         // operation to this view object as a grid table message 
6643 bool wxGrid::DeleteRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) ) 
6645     // TODO: something with updateLabels flag 
6649         wxFAIL_MSG( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") ); 
6655         if (IsCellEditControlEnabled()) 
6656             DisableCellEditControl(); 
6658         bool done 
= m_table
->DeleteRows( pos
, numRows 
); 
6660         // the table will have sent the results of the delete row 
6661         // operation to this view object as a grid table message 
6667 bool wxGrid::InsertCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) ) 
6669     // TODO: something with updateLabels flag 
6673         wxFAIL_MSG( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") ); 
6679         if (IsCellEditControlEnabled()) 
6680             DisableCellEditControl(); 
6682         bool done 
= m_table
->InsertCols( pos
, numCols 
); 
6684         // the table will have sent the results of the insert col 
6685         // operation to this view object as a grid table message 
6691 bool wxGrid::AppendCols( int numCols
, bool WXUNUSED(updateLabels
) ) 
6693     // TODO: something with updateLabels flag 
6697         wxFAIL_MSG( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") ); 
6703         bool done 
= m_table
->AppendCols( numCols 
); 
6705         // the table will have sent the results of the append col 
6706         // operation to this view object as a grid table message 
6712 bool wxGrid::DeleteCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) ) 
6714     // TODO: something with updateLabels flag 
6718         wxFAIL_MSG( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") ); 
6724         if (IsCellEditControlEnabled()) 
6725             DisableCellEditControl(); 
6727         bool done 
= m_table
->DeleteCols( pos
, numCols 
); 
6729         // the table will have sent the results of the delete col 
6730         // operation to this view object as a grid table message 
6737 // ----- event handlers 
6740 // Generate a grid event based on a mouse event and 
6741 // return the result of ProcessEvent() 
6743 int wxGrid::SendEvent( const wxEventType type
, 
6745                         wxMouseEvent
& mouseEv 
) 
6747    bool claimed
, vetoed
; 
6749    if ( type 
== wxEVT_GRID_ROW_SIZE 
|| type 
== wxEVT_GRID_COL_SIZE 
) 
6751        int rowOrCol 
= (row 
== -1 ? col 
: row
); 
6753        wxGridSizeEvent 
gridEvt( GetId(), 
6757                mouseEv
.GetX() + GetRowLabelSize(), 
6758                mouseEv
.GetY() + GetColLabelSize(), 
6759                mouseEv
.ControlDown(), 
6760                mouseEv
.ShiftDown(), 
6762                mouseEv
.MetaDown() ); 
6764        claimed 
= GetEventHandler()->ProcessEvent(gridEvt
); 
6765        vetoed 
= !gridEvt
.IsAllowed(); 
6767    else if ( type 
== wxEVT_GRID_RANGE_SELECT 
) 
6769        // Right now, it should _never_ end up here! 
6770        wxGridRangeSelectEvent 
gridEvt( GetId(), 
6774                m_selectingBottomRight
, 
6776                mouseEv
.ControlDown(), 
6777                mouseEv
.ShiftDown(), 
6779                mouseEv
.MetaDown() ); 
6781        claimed 
= GetEventHandler()->ProcessEvent(gridEvt
); 
6782        vetoed 
= !gridEvt
.IsAllowed(); 
6784    else if ( type 
== wxEVT_GRID_LABEL_LEFT_CLICK 
|| 
6785              type 
== wxEVT_GRID_LABEL_LEFT_DCLICK 
|| 
6786              type 
== wxEVT_GRID_LABEL_RIGHT_CLICK 
|| 
6787              type 
== wxEVT_GRID_LABEL_RIGHT_DCLICK 
) 
6789        wxPoint pos 
= mouseEv
.GetPosition(); 
6791        if ( mouseEv
.GetEventObject() == GetGridRowLabelWindow() ) 
6792            pos
.y 
+= GetColLabelSize(); 
6793        if ( mouseEv
.GetEventObject() == GetGridColLabelWindow() ) 
6794            pos
.x 
+= GetRowLabelSize(); 
6796        wxGridEvent 
gridEvt( GetId(), 
6803                mouseEv
.ControlDown(), 
6804                mouseEv
.ShiftDown(), 
6806                mouseEv
.MetaDown() ); 
6807        claimed 
= GetEventHandler()->ProcessEvent(gridEvt
); 
6808        vetoed 
= !gridEvt
.IsAllowed(); 
6812        wxGridEvent 
gridEvt( GetId(), 
6816                mouseEv
.GetX() + GetRowLabelSize(), 
6817                mouseEv
.GetY() + GetColLabelSize(), 
6819                mouseEv
.ControlDown(), 
6820                mouseEv
.ShiftDown(), 
6822                mouseEv
.MetaDown() ); 
6823        claimed 
= GetEventHandler()->ProcessEvent(gridEvt
); 
6824        vetoed 
= !gridEvt
.IsAllowed(); 
6827    // A Veto'd event may not be `claimed' so test this first 
6831    return claimed 
? 1 : 0; 
6834 // Generate a grid event of specified type and return the result 
6835 // of ProcessEvent(). 
6837 int wxGrid::SendEvent( const wxEventType type
, 
6840    bool claimed
, vetoed
; 
6842     if ( type 
== wxEVT_GRID_ROW_SIZE 
|| type 
== wxEVT_GRID_COL_SIZE 
) 
6844         int rowOrCol 
= (row 
== -1 ? col 
: row
); 
6846         wxGridSizeEvent 
gridEvt( GetId(), type
, this, rowOrCol 
); 
6848         claimed 
= GetEventHandler()->ProcessEvent(gridEvt
); 
6849         vetoed  
= !gridEvt
.IsAllowed(); 
6853         wxGridEvent 
gridEvt( GetId(), type
, this, row
, col 
); 
6855         claimed 
= GetEventHandler()->ProcessEvent(gridEvt
); 
6856         vetoed  
= !gridEvt
.IsAllowed(); 
6859     // A Veto'd event may not be `claimed' so test this first 
6863     return claimed 
? 1 : 0; 
6866 void wxGrid::OnPaint( wxPaintEvent
& WXUNUSED(event
) ) 
6868     // needed to prevent zillions of paint events on MSW 
6872 void wxGrid::Refresh(bool eraseb
, const wxRect
* rect
) 
6874     // Don't do anything if between Begin/EndBatch... 
6875     // EndBatch() will do all this on the last nested one anyway. 
6876     if ( m_created 
&& !GetBatchCount() ) 
6878         // Refresh to get correct scrolled position: 
6879         wxScrolledWindow::Refresh(eraseb
, rect
); 
6883             int rect_x
, rect_y
, rectWidth
, rectHeight
; 
6884             int width_label
, width_cell
, height_label
, height_cell
; 
6887             // Copy rectangle can get scroll offsets.. 
6888             rect_x 
= rect
->GetX(); 
6889             rect_y 
= rect
->GetY(); 
6890             rectWidth 
= rect
->GetWidth(); 
6891             rectHeight 
= rect
->GetHeight(); 
6893             width_label 
= m_rowLabelWidth 
- rect_x
; 
6894             if (width_label 
> rectWidth
) 
6895                 width_label 
= rectWidth
; 
6897             height_label 
= m_colLabelHeight 
- rect_y
; 
6898             if (height_label 
> rectHeight
) 
6899                 height_label 
= rectHeight
; 
6901             if (rect_x 
> m_rowLabelWidth
) 
6903                 x 
= rect_x 
- m_rowLabelWidth
; 
6904                 width_cell 
= rectWidth
; 
6909                 width_cell 
= rectWidth 
- (m_rowLabelWidth 
- rect_x
); 
6912             if (rect_y 
> m_colLabelHeight
) 
6914                 y 
= rect_y 
- m_colLabelHeight
; 
6915                 height_cell 
= rectHeight
; 
6920                 height_cell 
= rectHeight 
- (m_colLabelHeight 
- rect_y
); 
6923             // Paint corner label part intersecting rect. 
6924             if ( width_label 
> 0 && height_label 
> 0 ) 
6926                 wxRect 
anotherrect(rect_x
, rect_y
, width_label
, height_label
); 
6927                 m_cornerLabelWin
->Refresh(eraseb
, &anotherrect
); 
6930             // Paint col labels part intersecting rect. 
6931             if ( width_cell 
> 0 && height_label 
> 0 ) 
6933                 wxRect 
anotherrect(x
, rect_y
, width_cell
, height_label
); 
6934                 m_colLabelWin
->Refresh(eraseb
, &anotherrect
); 
6937             // Paint row labels part intersecting rect. 
6938             if ( width_label 
> 0 && height_cell 
> 0 ) 
6940                 wxRect 
anotherrect(rect_x
, y
, width_label
, height_cell
); 
6941                 m_rowLabelWin
->Refresh(eraseb
, &anotherrect
); 
6944             // Paint cell area part intersecting rect. 
6945             if ( width_cell 
> 0 && height_cell 
> 0 ) 
6947                 wxRect 
anotherrect(x
, y
, width_cell
, height_cell
); 
6948                 m_gridWin
->Refresh(eraseb
, &anotherrect
); 
6953             m_cornerLabelWin
->Refresh(eraseb
, NULL
); 
6954             m_colLabelWin
->Refresh(eraseb
, NULL
); 
6955             m_rowLabelWin
->Refresh(eraseb
, NULL
); 
6956             m_gridWin
->Refresh(eraseb
, NULL
); 
6961 void wxGrid::OnSize(wxSizeEvent
& WXUNUSED(event
)) 
6963     if (m_targetWindow 
!= this) // check whether initialisation has been done 
6965         // reposition our children windows 
6970 void wxGrid::OnKeyDown( wxKeyEvent
& event 
) 
6972     if ( m_inOnKeyDown 
) 
6974         // shouldn't be here - we are going round in circles... 
6976         wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") ); 
6979     m_inOnKeyDown 
= true; 
6981     // propagate the event up and see if it gets processed 
6982     wxWindow 
*parent 
= GetParent(); 
6983     wxKeyEvent 
keyEvt( event 
); 
6984     keyEvt
.SetEventObject( parent 
); 
6986     if ( !parent
->GetEventHandler()->ProcessEvent( keyEvt 
) ) 
6988         if (GetLayoutDirection() == wxLayout_RightToLeft
) 
6990             if (event
.GetKeyCode() == WXK_RIGHT
) 
6991                 event
.m_keyCode 
= WXK_LEFT
; 
6992             else if (event
.GetKeyCode() == WXK_LEFT
) 
6993                 event
.m_keyCode 
= WXK_RIGHT
; 
6996         // try local handlers 
6997         switch ( event
.GetKeyCode() ) 
7000                 if ( event
.ControlDown() ) 
7001                     MoveCursorUpBlock( event
.ShiftDown() ); 
7003                     MoveCursorUp( event
.ShiftDown() ); 
7007                 if ( event
.ControlDown() ) 
7008                     MoveCursorDownBlock( event
.ShiftDown() ); 
7010                     MoveCursorDown( event
.ShiftDown() ); 
7014                 if ( event
.ControlDown() ) 
7015                     MoveCursorLeftBlock( event
.ShiftDown() ); 
7017                     MoveCursorLeft( event
.ShiftDown() ); 
7021                 if ( event
.ControlDown() ) 
7022                     MoveCursorRightBlock( event
.ShiftDown() ); 
7024                     MoveCursorRight( event
.ShiftDown() ); 
7028             case WXK_NUMPAD_ENTER
: 
7029                 if ( event
.ControlDown() ) 
7031                     event
.Skip();  // to let the edit control have the return 
7035                     if ( GetGridCursorRow() < GetNumberRows()-1 ) 
7037                         MoveCursorDown( event
.ShiftDown() ); 
7041                         // at the bottom of a column 
7042                         DisableCellEditControl(); 
7052                 if (event
.ShiftDown()) 
7054                     if ( GetGridCursorCol() > 0 ) 
7056                         MoveCursorLeft( false ); 
7061                         DisableCellEditControl(); 
7066                     if ( GetGridCursorCol() < GetNumberCols() - 1 ) 
7068                         MoveCursorRight( false ); 
7073                         DisableCellEditControl(); 
7079                 if ( event
.ControlDown() ) 
7081                     MakeCellVisible( 0, 0 ); 
7082                     SetCurrentCell( 0, 0 ); 
7091                 if ( event
.ControlDown() ) 
7093                     MakeCellVisible( m_numRows 
- 1, m_numCols 
- 1 ); 
7094                     SetCurrentCell( m_numRows 
- 1, m_numCols 
- 1 ); 
7111                 if ( event
.ControlDown() ) 
7115                         m_selection
->ToggleCellSelection( 
7116                             m_currentCellCoords
.GetRow(), 
7117                             m_currentCellCoords
.GetCol(), 
7118                             event
.ControlDown(), 
7126                 if ( !IsEditable() ) 
7127                     MoveCursorRight( false ); 
7138     m_inOnKeyDown 
= false; 
7141 void wxGrid::OnKeyUp( wxKeyEvent
& event 
) 
7143     // try local handlers 
7145     if ( event
.GetKeyCode() == WXK_SHIFT 
) 
7147         if ( m_selectingTopLeft 
!= wxGridNoCellCoords 
&& 
7148              m_selectingBottomRight 
!= wxGridNoCellCoords 
) 
7152                 m_selection
->SelectBlock( 
7153                     m_selectingTopLeft
.GetRow(), 
7154                     m_selectingTopLeft
.GetCol(), 
7155                     m_selectingBottomRight
.GetRow(), 
7156                     m_selectingBottomRight
.GetCol(), 
7157                     event
.ControlDown(), 
7164         m_selectingTopLeft 
= wxGridNoCellCoords
; 
7165         m_selectingBottomRight 
= wxGridNoCellCoords
; 
7166         m_selectingKeyboard 
= wxGridNoCellCoords
; 
7170 void wxGrid::OnChar( wxKeyEvent
& event 
) 
7172     // is it possible to edit the current cell at all? 
7173     if ( !IsCellEditControlEnabled() && CanEnableCellControl() ) 
7175         // yes, now check whether the cells editor accepts the key 
7176         int row 
= m_currentCellCoords
.GetRow(); 
7177         int col 
= m_currentCellCoords
.GetCol(); 
7178         wxGridCellAttr 
*attr 
= GetCellAttr(row
, col
); 
7179         wxGridCellEditor 
*editor 
= attr
->GetEditor(this, row
, col
); 
7181         // <F2> is special and will always start editing, for 
7182         // other keys - ask the editor itself 
7183         if ( (event
.GetKeyCode() == WXK_F2 
&& !event
.HasModifiers()) 
7184              || editor
->IsAcceptedKey(event
) ) 
7186             // ensure cell is visble 
7187             MakeCellVisible(row
, col
); 
7188             EnableCellEditControl(); 
7190             // a problem can arise if the cell is not completely 
7191             // visible (even after calling MakeCellVisible the 
7192             // control is not created and calling StartingKey will 
7194             if ( event
.GetKeyCode() != WXK_F2 
&& editor
->IsCreated() && m_cellEditCtrlEnabled 
) 
7195                 editor
->StartingKey(event
); 
7211 void wxGrid::OnEraseBackground(wxEraseEvent
&) 
7215 void wxGrid::SetCurrentCell( const wxGridCellCoords
& coords 
) 
7217     if ( SendEvent( wxEVT_GRID_SELECT_CELL
, coords
.GetRow(), coords
.GetCol() ) ) 
7219         // the event has been intercepted - do nothing 
7223 #if !defined(__WXMAC__) 
7224     wxClientDC 
dc( m_gridWin 
); 
7228     if ( m_currentCellCoords 
!= wxGridNoCellCoords 
) 
7230         DisableCellEditControl(); 
7232         if ( IsVisible( m_currentCellCoords
, false ) ) 
7235             r 
= BlockToDeviceRect( m_currentCellCoords
, m_currentCellCoords 
); 
7236             if ( !m_gridLinesEnabled 
) 
7244             wxGridCellCoordsArray cells 
= CalcCellsExposed( r 
); 
7246             // Otherwise refresh redraws the highlight! 
7247             m_currentCellCoords 
= coords
; 
7249 #if defined(__WXMAC__) 
7250             m_gridWin
->Refresh(true /*, & r */); 
7252             DrawGridCellArea( dc
, cells 
); 
7253             DrawAllGridLines( dc
, r 
); 
7258     m_currentCellCoords 
= coords
; 
7260     wxGridCellAttr 
*attr 
= GetCellAttr( coords 
); 
7261 #if !defined(__WXMAC__) 
7262     DrawCellHighlight( dc
, attr 
); 
7267 void wxGrid::HighlightBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol 
) 
7270     wxGridCellCoords updateTopLeft
, updateBottomRight
; 
7274         if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows 
) 
7277             rightCol 
= GetNumberCols() - 1; 
7279         else if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns 
) 
7282             bottomRow 
= GetNumberRows() - 1; 
7286     if ( topRow 
> bottomRow 
) 
7293     if ( leftCol 
> rightCol 
) 
7300     updateTopLeft 
= wxGridCellCoords( topRow
, leftCol 
); 
7301     updateBottomRight 
= wxGridCellCoords( bottomRow
, rightCol 
); 
7303     // First the case that we selected a completely new area 
7304     if ( m_selectingTopLeft 
== wxGridNoCellCoords 
|| 
7305          m_selectingBottomRight 
== wxGridNoCellCoords 
) 
7308         rect 
= BlockToDeviceRect( wxGridCellCoords ( topRow
, leftCol 
), 
7309                                   wxGridCellCoords ( bottomRow
, rightCol 
) ); 
7310         m_gridWin
->Refresh( false, &rect 
); 
7313     // Now handle changing an existing selection area. 
7314     else if ( m_selectingTopLeft 
!= updateTopLeft 
|| 
7315               m_selectingBottomRight 
!= updateBottomRight 
) 
7317         // Compute two optimal update rectangles: 
7318         // Either one rectangle is a real subset of the 
7319         // other, or they are (almost) disjoint! 
7321         bool    need_refresh
[4]; 
7325         need_refresh
[3] = false; 
7328         // Store intermediate values 
7329         wxCoord oldLeft 
= m_selectingTopLeft
.GetCol(); 
7330         wxCoord oldTop 
= m_selectingTopLeft
.GetRow(); 
7331         wxCoord oldRight 
= m_selectingBottomRight
.GetCol(); 
7332         wxCoord oldBottom 
= m_selectingBottomRight
.GetRow(); 
7334         // Determine the outer/inner coordinates. 
7335         if (oldLeft 
> leftCol
) 
7341         if (oldTop 
> topRow 
) 
7347         if (oldRight 
< rightCol 
) 
7350             oldRight 
= rightCol
; 
7353         if (oldBottom 
< bottomRow
) 
7356             oldBottom 
= bottomRow
; 
7360         // Now, either the stuff marked old is the outer 
7361         // rectangle or we don't have a situation where one 
7362         // is contained in the other. 
7364         if ( oldLeft 
< leftCol 
) 
7366             // Refresh the newly selected or deselected 
7367             // area to the left of the old or new selection. 
7368             need_refresh
[0] = true; 
7369             rect
[0] = BlockToDeviceRect( 
7370                 wxGridCellCoords( oldTop
,  oldLeft 
), 
7371                 wxGridCellCoords( oldBottom
, leftCol 
- 1 ) ); 
7374         if ( oldTop 
< topRow 
) 
7376             // Refresh the newly selected or deselected 
7377             // area above the old or new selection. 
7378             need_refresh
[1] = true; 
7379             rect
[1] = BlockToDeviceRect( 
7380                 wxGridCellCoords( oldTop
, leftCol 
), 
7381                 wxGridCellCoords( topRow 
- 1, rightCol 
) ); 
7384         if ( oldRight 
> rightCol 
) 
7386             // Refresh the newly selected or deselected 
7387             // area to the right of the old or new selection. 
7388             need_refresh
[2] = true; 
7389             rect
[2] = BlockToDeviceRect( 
7390                 wxGridCellCoords( oldTop
, rightCol 
+ 1 ), 
7391                 wxGridCellCoords( oldBottom
, oldRight 
) ); 
7394         if ( oldBottom 
> bottomRow 
) 
7396             // Refresh the newly selected or deselected 
7397             // area below the old or new selection. 
7398             need_refresh
[3] = true; 
7399             rect
[3] = BlockToDeviceRect( 
7400                 wxGridCellCoords( bottomRow 
+ 1, leftCol 
), 
7401                 wxGridCellCoords( oldBottom
, rightCol 
) ); 
7404         // various Refresh() calls 
7405         for (i 
= 0; i 
< 4; i
++ ) 
7406             if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect 
) 
7407                 m_gridWin
->Refresh( false, &(rect
[i
]) ); 
7411     m_selectingTopLeft 
= updateTopLeft
; 
7412     m_selectingBottomRight 
= updateBottomRight
; 
7416 // ------ functions to get/send data (see also public functions) 
7419 bool wxGrid::GetModelValues() 
7421     // Hide the editor, so it won't hide a changed value. 
7422     HideCellEditControl(); 
7426         // all we need to do is repaint the grid 
7428         m_gridWin
->Refresh(); 
7435 bool wxGrid::SetModelValues() 
7439     // Disable the editor, so it won't hide a changed value. 
7440     // Do we also want to save the current value of the editor first? 
7442     DisableCellEditControl(); 
7446         for ( row 
= 0; row 
< m_numRows
; row
++ ) 
7448             for ( col 
= 0; col 
< m_numCols
; col
++ ) 
7450                 m_table
->SetValue( row
, col
, GetCellValue(row
, col
) ); 
7460 // Note - this function only draws cells that are in the list of 
7461 // exposed cells (usually set from the update region by 
7462 // CalcExposedCells) 
7464 void wxGrid::DrawGridCellArea( wxDC
& dc
, const wxGridCellCoordsArray
& cells 
) 
7466     if ( !m_numRows 
|| !m_numCols 
) 
7469     int i
, numCells 
= cells
.GetCount(); 
7470     int row
, col
, cell_rows
, cell_cols
; 
7471     wxGridCellCoordsArray redrawCells
; 
7473     for ( i 
= numCells 
- 1; i 
>= 0; i
-- ) 
7475         row 
= cells
[i
].GetRow(); 
7476         col 
= cells
[i
].GetCol(); 
7477         GetCellSize( row
, col
, &cell_rows
, &cell_cols 
); 
7479         // If this cell is part of a multicell block, find owner for repaint 
7480         if ( cell_rows 
<= 0 || cell_cols 
<= 0 ) 
7482             wxGridCellCoords 
cell( row 
+ cell_rows
, col 
+ cell_cols 
); 
7483             bool marked 
= false; 
7484             for ( int j 
= 0; j 
< numCells
; j
++ ) 
7486                 if ( cell 
== cells
[j
] ) 
7495                 int count 
= redrawCells
.GetCount(); 
7496                 for (int j 
= 0; j 
< count
; j
++) 
7498                     if ( cell 
== redrawCells
[j
] ) 
7506                     redrawCells
.Add( cell 
); 
7509             // don't bother drawing this cell 
7513         // If this cell is empty, find cell to left that might want to overflow 
7514         if (m_table 
&& m_table
->IsEmptyCell(row
, col
)) 
7516             for ( int l 
= 0; l 
< cell_rows
; l
++ ) 
7518                 // find a cell in this row to leave already marked for repaint 
7520                 for (int k 
= 0; k 
< int(redrawCells
.GetCount()); k
++) 
7521                     if ((redrawCells
[k
].GetCol() < left
) && 
7522                         (redrawCells
[k
].GetRow() == row
)) 
7524                         left 
= redrawCells
[k
].GetCol(); 
7528                     left 
= 0; // oh well 
7530                 for (int j 
= col 
- 1; j 
>= left
; j
--) 
7532                     if (!m_table
->IsEmptyCell(row 
+ l
, j
)) 
7534                         if (GetCellOverflow(row 
+ l
, j
)) 
7536                             wxGridCellCoords 
cell(row 
+ l
, j
); 
7537                             bool marked 
= false; 
7539                             for (int k 
= 0; k 
< numCells
; k
++) 
7541                                 if ( cell 
== cells
[k
] ) 
7550                                 int count 
= redrawCells
.GetCount(); 
7551                                 for (int k 
= 0; k 
< count
; k
++) 
7553                                     if ( cell 
== redrawCells
[k
] ) 
7560                                     redrawCells
.Add( cell 
); 
7569         DrawCell( dc
, cells
[i
] ); 
7572     numCells 
= redrawCells
.GetCount(); 
7574     for ( i 
= numCells 
- 1; i 
>= 0; i
-- ) 
7576         DrawCell( dc
, redrawCells
[i
] ); 
7580 void wxGrid::DrawGridSpace( wxDC
& dc 
) 
7583   m_gridWin
->GetClientSize( &cw
, &ch 
); 
7586   CalcUnscrolledPosition( cw
, ch
, &right
, &bottom 
); 
7588   int rightCol 
= m_numCols 
> 0 ? GetColRight(GetColAt( m_numCols 
- 1 )) : 0; 
7589   int bottomRow 
= m_numRows 
> 0 ? GetRowBottom(m_numRows 
- 1) : 0; 
7591   if ( right 
> rightCol 
|| bottom 
> bottomRow 
) 
7594       CalcUnscrolledPosition( 0, 0, &left
, &top 
); 
7596       dc
.SetBrush( wxBrush(GetDefaultCellBackgroundColour(), wxBRUSHSTYLE_SOLID
) ); 
7597       dc
.SetPen( *wxTRANSPARENT_PEN 
); 
7599       if ( right 
> rightCol 
) 
7601           dc
.DrawRectangle( rightCol
, top
, right 
- rightCol
, ch 
); 
7604       if ( bottom 
> bottomRow 
) 
7606           dc
.DrawRectangle( left
, bottomRow
, cw
, bottom 
- bottomRow 
); 
7611 void wxGrid::DrawCell( wxDC
& dc
, const wxGridCellCoords
& coords 
) 
7613     int row 
= coords
.GetRow(); 
7614     int col 
= coords
.GetCol(); 
7616     if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 ) 
7619     // we draw the cell border ourselves 
7620 #if !WXGRID_DRAW_LINES 
7621     if ( m_gridLinesEnabled 
) 
7622         DrawCellBorder( dc
, coords 
); 
7625     wxGridCellAttr
* attr 
= GetCellAttr(row
, col
); 
7627     bool isCurrent 
= coords 
== m_currentCellCoords
; 
7629     wxRect rect 
= CellToRect( row
, col 
); 
7631     // if the editor is shown, we should use it and not the renderer 
7632     // Note: However, only if it is really _shown_, i.e. not hidden! 
7633     if ( isCurrent 
&& IsCellEditControlShown() ) 
7635         // NB: this "#if..." is temporary and fixes a problem where the 
7636         // edit control is erased by this code after being rendered. 
7637         // On wxMac (QD build only), the cell editor is a wxTextCntl and is rendered 
7638         // implicitly, causing this out-of order render. 
7639 #if !defined(__WXMAC__) 
7640         wxGridCellEditor 
*editor 
= attr
->GetEditor(this, row
, col
); 
7641         editor
->PaintBackground(rect
, attr
); 
7647         // but all the rest is drawn by the cell renderer and hence may be customized 
7648         wxGridCellRenderer 
*renderer 
= attr
->GetRenderer(this, row
, col
); 
7649         renderer
->Draw(*this, *attr
, dc
, rect
, row
, col
, IsInSelection(coords
)); 
7656 void wxGrid::DrawCellHighlight( wxDC
& dc
, const wxGridCellAttr 
*attr 
) 
7658     // don't show highlight when the grid doesn't have focus 
7662     int row 
= m_currentCellCoords
.GetRow(); 
7663     int col 
= m_currentCellCoords
.GetCol(); 
7665     if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 ) 
7668     wxRect rect 
= CellToRect(row
, col
); 
7670     // hmmm... what could we do here to show that the cell is disabled? 
7671     // for now, I just draw a thinner border than for the other ones, but 
7672     // it doesn't look really good 
7674     int penWidth 
= attr
->IsReadOnly() ? m_cellHighlightROPenWidth 
: m_cellHighlightPenWidth
; 
7678         // The center of the drawn line is where the position/width/height of 
7679         // the rectangle is actually at (on wxMSW at least), so the 
7680         // size of the rectangle is reduced to compensate for the thickness of 
7681         // the line. If this is too strange on non-wxMSW platforms then 
7682         // please #ifdef this appropriately. 
7683         rect
.x 
+= penWidth 
/ 2; 
7684         rect
.y 
+= penWidth 
/ 2; 
7685         rect
.width 
-= penWidth 
- 1; 
7686         rect
.height 
-= penWidth 
- 1; 
7688         // Now draw the rectangle 
7689         // use the cellHighlightColour if the cell is inside a selection, this 
7690         // will ensure the cell is always visible. 
7691         dc
.SetPen(wxPen(IsInSelection(row
,col
) ? m_selectionForeground 
: m_cellHighlightColour
, penWidth
, wxPENSTYLE_SOLID
)); 
7692         dc
.SetBrush(*wxTRANSPARENT_BRUSH
); 
7693         dc
.DrawRectangle(rect
); 
7697         // VZ: my experiments with 3D borders... 
7699         // how to properly set colours for arbitrary bg? 
7700         wxCoord x1 
= rect
.x
, 
7702                 x2 
= rect
.x 
+ rect
.width 
- 1, 
7703                 y2 
= rect
.y 
+ rect
.height 
- 1; 
7705         dc
.SetPen(*wxWHITE_PEN
); 
7706         dc
.DrawLine(x1
, y1
, x2
, y1
); 
7707         dc
.DrawLine(x1
, y1
, x1
, y2
); 
7709         dc
.DrawLine(x1 
+ 1, y2 
- 1, x2 
- 1, y2 
- 1); 
7710         dc
.DrawLine(x2 
- 1, y1 
+ 1, x2 
- 1, y2
); 
7712         dc
.SetPen(*wxBLACK_PEN
); 
7713         dc
.DrawLine(x1
, y2
, x2
, y2
); 
7714         dc
.DrawLine(x2
, y1
, x2
, y2 
+ 1); 
7718 wxPen 
wxGrid::GetDefaultGridLinePen() 
7720     return wxPen(GetGridLineColour(), 1, wxPENSTYLE_SOLID
); 
7723 wxPen 
wxGrid::GetRowGridLinePen(int WXUNUSED(row
)) 
7725     return GetDefaultGridLinePen(); 
7728 wxPen 
wxGrid::GetColGridLinePen(int WXUNUSED(col
)) 
7730     return GetDefaultGridLinePen(); 
7733 void wxGrid::DrawCellBorder( wxDC
& dc
, const wxGridCellCoords
& coords 
) 
7735     int row 
= coords
.GetRow(); 
7736     int col 
= coords
.GetCol(); 
7737     if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 ) 
7741     wxRect rect 
= CellToRect( row
, col 
); 
7743     // right hand border 
7744     dc
.SetPen( GetColGridLinePen(col
) ); 
7745     dc
.DrawLine( rect
.x 
+ rect
.width
, rect
.y
, 
7746                  rect
.x 
+ rect
.width
, rect
.y 
+ rect
.height 
+ 1 ); 
7749     dc
.SetPen( GetRowGridLinePen(row
) ); 
7750     dc
.DrawLine( rect
.x
, rect
.y 
+ rect
.height
, 
7751                  rect
.x 
+ rect
.width
, rect
.y 
+ rect
.height
); 
7754 void wxGrid::DrawHighlight(wxDC
& dc
, const wxGridCellCoordsArray
& cells
) 
7756     // This if block was previously in wxGrid::OnPaint but that doesn't 
7757     // seem to get called under wxGTK - MB 
7759     if ( m_currentCellCoords 
== wxGridNoCellCoords 
&& 
7760          m_numRows 
&& m_numCols 
) 
7762         m_currentCellCoords
.Set(0, 0); 
7765     if ( IsCellEditControlShown() ) 
7767         // don't show highlight when the edit control is shown 
7771     // if the active cell was repainted, repaint its highlight too because it 
7772     // might have been damaged by the grid lines 
7773     size_t count 
= cells
.GetCount(); 
7774     for ( size_t n 
= 0; n 
< count
; n
++ ) 
7776         wxGridCellCoords cell 
= cells
[n
]; 
7778         // If we are using attributes, then we may have just exposed another 
7779         // cell in a partially-visible merged cluster of cells. If the "anchor" 
7780         // (upper left) cell of this merged cluster is the cell indicated by 
7781         // m_currentCellCoords, then we need to refresh the cell highlight even 
7782         // though the "anchor" itself is not part of our update segment. 
7783         if ( CanHaveAttributes() ) 
7787             GetCellSize(cell
.GetRow(), cell
.GetCol(), &rows
, &cols
); 
7790                 cell
.SetRow(cell
.GetRow() + rows
); 
7793                 cell
.SetCol(cell
.GetCol() + cols
); 
7796         if ( cell 
== m_currentCellCoords 
) 
7798             wxGridCellAttr
* attr 
= GetCellAttr(m_currentCellCoords
); 
7799             DrawCellHighlight(dc
, attr
); 
7807 // TODO: remove this ??? 
7808 // This is used to redraw all grid lines e.g. when the grid line colour 
7811 void wxGrid::DrawAllGridLines( wxDC
& dc
, const wxRegion 
& WXUNUSED(reg
) ) 
7813 #if !WXGRID_DRAW_LINES 
7817     if ( !m_gridLinesEnabled 
|| !m_numRows 
|| !m_numCols 
) 
7820     int top
, bottom
, left
, right
; 
7822 #if 0  //#ifndef __WXGTK__ 
7826       m_gridWin
->GetClientSize(&cw
, &ch
); 
7828       // virtual coords of visible area 
7830       CalcUnscrolledPosition( 0, 0, &left
, &top 
); 
7831       CalcUnscrolledPosition( cw
, ch
, &right
, &bottom 
); 
7836       reg
.GetBox(x
, y
, w
, h
); 
7837       CalcUnscrolledPosition( x
, y
, &left
, &top 
); 
7838       CalcUnscrolledPosition( x 
+ w
, y 
+ h
, &right
, &bottom 
); 
7842       m_gridWin
->GetClientSize(&cw
, &ch
); 
7843       CalcUnscrolledPosition( 0, 0, &left
, &top 
); 
7844       CalcUnscrolledPosition( cw
, ch
, &right
, &bottom 
); 
7847     // avoid drawing grid lines past the last row and col 
7849     right 
= wxMin( right
, GetColRight(GetColAt( m_numCols 
- 1 )) ); 
7850     bottom 
= wxMin( bottom
, GetRowBottom(m_numRows 
- 1) ); 
7852     // no gridlines inside multicells, clip them out 
7853     int leftCol 
= GetColPos( internalXToCol(left
) ); 
7854     int topRow 
= internalYToRow(top
); 
7855     int rightCol 
= GetColPos( internalXToCol(right
) ); 
7856     int bottomRow 
= internalYToRow(bottom
); 
7858     wxRegion 
clippedcells(0, 0, cw
, ch
); 
7860     int i
, j
, cell_rows
, cell_cols
; 
7863     for (j
=topRow
; j
<=bottomRow
; j
++) 
7866         for (colPos
=leftCol
; colPos
<=rightCol
; colPos
++) 
7868             i 
= GetColAt( colPos 
); 
7870             GetCellSize( j
, i
, &cell_rows
, &cell_cols 
); 
7871             if ((cell_rows 
> 1) || (cell_cols 
> 1)) 
7873                 rect 
= CellToRect(j
,i
); 
7874                 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y 
); 
7875                 clippedcells
.Subtract(rect
); 
7877             else if ((cell_rows 
< 0) || (cell_cols 
< 0)) 
7879                 rect 
= CellToRect(j 
+ cell_rows
, i 
+ cell_cols
); 
7880                 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y 
); 
7881                 clippedcells
.Subtract(rect
); 
7886     dc
.SetDeviceClippingRegion( clippedcells 
); 
7889     // horizontal grid lines 
7891     // already declared above - int i; 
7892     for ( i 
= internalYToRow(top
); i 
< m_numRows
; i
++ ) 
7894         int bot 
= GetRowBottom(i
) - 1; 
7903             dc
.SetPen( GetRowGridLinePen(i
) ); 
7904             dc
.DrawLine( left
, bot
, right
, bot 
); 
7908     // vertical grid lines 
7911     for ( colPos 
= leftCol
; colPos 
< m_numCols
; colPos
++ ) 
7913         i 
= GetColAt( colPos 
); 
7915         int colRight 
= GetColRight(i
); 
7917         if (GetLayoutDirection() != wxLayout_RightToLeft
) 
7921         if ( colRight 
> right 
) 
7926         if ( colRight 
>= left 
) 
7928             dc
.SetPen( GetColGridLinePen(i
) ); 
7929             dc
.DrawLine( colRight
, top
, colRight
, bottom 
); 
7933     dc
.DestroyClippingRegion(); 
7936 void wxGrid::DrawRowLabels( wxDC
& dc
, const wxArrayInt
& rows
) 
7942     size_t numLabels 
= rows
.GetCount(); 
7944     for ( i 
= 0; i 
< numLabels
; i
++ ) 
7946         DrawRowLabel( dc
, rows
[i
] ); 
7950 void wxGrid::DrawRowLabel( wxDC
& dc
, int row 
) 
7952     if ( GetRowHeight(row
) <= 0 || m_rowLabelWidth 
<= 0 ) 
7957     int rowTop 
= GetRowTop(row
), 
7958         rowBottom 
= GetRowBottom(row
) - 1; 
7960     dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW
), 1, wxPENSTYLE_SOLID
) ); 
7961     dc
.DrawLine( m_rowLabelWidth 
- 1, rowTop
, m_rowLabelWidth 
- 1, rowBottom 
); 
7962     dc
.DrawLine( 0, rowTop
, 0, rowBottom 
); 
7963     dc
.DrawLine( 0, rowBottom
, m_rowLabelWidth
, rowBottom 
); 
7965     dc
.SetPen( *wxWHITE_PEN 
); 
7966     dc
.DrawLine( 1, rowTop
, 1, rowBottom 
); 
7967     dc
.DrawLine( 1, rowTop
, m_rowLabelWidth 
- 1, rowTop 
); 
7969     dc
.SetBackgroundMode( wxBRUSHSTYLE_TRANSPARENT 
); 
7970     dc
.SetTextForeground( GetLabelTextColour() ); 
7971     dc
.SetFont( GetLabelFont() ); 
7974     GetRowLabelAlignment( &hAlign
, &vAlign 
); 
7977     rect
.SetY( GetRowTop(row
) + 2 ); 
7978     rect
.SetWidth( m_rowLabelWidth 
- 4 ); 
7979     rect
.SetHeight( GetRowHeight(row
) - 4 ); 
7980     DrawTextRectangle( dc
, GetRowLabelValue( row 
), rect
, hAlign
, vAlign 
); 
7983 void wxGrid::SetUseNativeColLabels( bool native 
) 
7985     m_nativeColumnLabels 
= native
; 
7988         int height 
= wxRendererNative::Get().GetHeaderButtonHeight( this ); 
7989         SetColLabelSize( height 
); 
7992     m_colLabelWin
->Refresh(); 
7995 void wxGrid::DrawColLabels( wxDC
& dc
,const wxArrayInt
& cols 
) 
8001     size_t numLabels 
= cols
.GetCount(); 
8003     for ( i 
= 0; i 
< numLabels
; i
++ ) 
8005         DrawColLabel( dc
, cols
[i
] ); 
8009 void wxGrid::DrawColLabel( wxDC
& dc
, int col 
) 
8011     if ( GetColWidth(col
) <= 0 || m_colLabelHeight 
<= 0 ) 
8014     int colLeft 
= GetColLeft(col
); 
8018     if (m_nativeColumnLabels
) 
8020         rect
.SetX( colLeft
); 
8022         rect
.SetWidth( GetColWidth(col
)); 
8023         rect
.SetHeight( m_colLabelHeight 
); 
8025         wxWindowDC 
*win_dc 
= (wxWindowDC
*) &dc
; 
8026         wxRendererNative::Get().DrawHeaderButton( win_dc
->GetWindow(), dc
, rect
, 0 ); 
8030         int colRight 
= GetColRight(col
) - 1; 
8032         dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW
), 1, wxPENSTYLE_SOLID
) ); 
8033         dc
.DrawLine( colRight
, 0, colRight
, m_colLabelHeight 
- 1 ); 
8034         dc
.DrawLine( colLeft
, 0, colRight
, 0 ); 
8035         dc
.DrawLine( colLeft
, m_colLabelHeight 
- 1, 
8036                  colRight 
+ 1, m_colLabelHeight 
- 1 ); 
8038         dc
.SetPen( *wxWHITE_PEN 
); 
8039         dc
.DrawLine( colLeft
, 1, colLeft
, m_colLabelHeight 
- 1 ); 
8040         dc
.DrawLine( colLeft
, 1, colRight
, 1 ); 
8043     dc
.SetBackgroundMode( wxBRUSHSTYLE_TRANSPARENT 
); 
8044     dc
.SetTextForeground( GetLabelTextColour() ); 
8045     dc
.SetFont( GetLabelFont() ); 
8047     int hAlign
, vAlign
, orient
; 
8048     GetColLabelAlignment( &hAlign
, &vAlign 
); 
8049     orient 
= GetColLabelTextOrientation(); 
8051     rect
.SetX( colLeft 
+ 2 ); 
8053     rect
.SetWidth( GetColWidth(col
) - 4 ); 
8054     rect
.SetHeight( m_colLabelHeight 
- 4 ); 
8055     DrawTextRectangle( dc
, GetColLabelValue( col 
), rect
, hAlign
, vAlign
, orient 
); 
8058 void wxGrid::DrawTextRectangle( wxDC
& dc
, 
8059                                 const wxString
& value
, 
8063                                 int textOrientation 
) 
8065     wxArrayString lines
; 
8067     StringToLines( value
, lines 
); 
8069     // Forward to new API. 
8070     DrawTextRectangle( dc
, 
8078 // VZ: this should be replaced with wxDC::DrawLabel() to which we just have to 
8079 //     add textOrientation support 
8080 void wxGrid::DrawTextRectangle(wxDC
& dc
, 
8081                                const wxArrayString
& lines
, 
8085                                int textOrientation
) 
8087     if ( lines
.empty() ) 
8090     wxDCClipper 
clip(dc
, rect
); 
8095     if ( textOrientation 
== wxHORIZONTAL 
) 
8096         GetTextBoxSize( dc
, lines
, &textWidth
, &textHeight 
); 
8098         GetTextBoxSize( dc
, lines
, &textHeight
, &textWidth 
); 
8102     switch ( vertAlign 
) 
8104         case wxALIGN_BOTTOM
: 
8105             if ( textOrientation 
== wxHORIZONTAL 
) 
8106                 y 
= rect
.y 
+ (rect
.height 
- textHeight 
- 1); 
8108                 x 
= rect
.x 
+ rect
.width 
- textWidth
; 
8111         case wxALIGN_CENTRE
: 
8112             if ( textOrientation 
== wxHORIZONTAL 
) 
8113                 y 
= rect
.y 
+ ((rect
.height 
- textHeight
) / 2); 
8115                 x 
= rect
.x 
+ ((rect
.width 
- textWidth
) / 2); 
8120             if ( textOrientation 
== wxHORIZONTAL 
) 
8127     // Align each line of a multi-line label 
8128     size_t nLines 
= lines
.GetCount(); 
8129     for ( size_t l 
= 0; l 
< nLines
; l
++ ) 
8131         const wxString
& line 
= lines
[l
]; 
8135             *(textOrientation 
== wxHORIZONTAL 
? &y 
: &x
) += dc
.GetCharHeight(); 
8139         wxCoord lineWidth 
= 0, 
8141         dc
.GetTextExtent(line
, &lineWidth
, &lineHeight
); 
8143         switch ( horizAlign 
) 
8146                 if ( textOrientation 
== wxHORIZONTAL 
) 
8147                     x 
= rect
.x 
+ (rect
.width 
- lineWidth 
- 1); 
8149                     y 
= rect
.y 
+ lineWidth 
+ 1; 
8152             case wxALIGN_CENTRE
: 
8153                 if ( textOrientation 
== wxHORIZONTAL 
) 
8154                     x 
= rect
.x 
+ ((rect
.width 
- lineWidth
) / 2); 
8156                     y 
= rect
.y 
+ rect
.height 
- ((rect
.height 
- lineWidth
) / 2); 
8161                 if ( textOrientation 
== wxHORIZONTAL 
) 
8164                     y 
= rect
.y 
+ rect
.height 
- 1; 
8168         if ( textOrientation 
== wxHORIZONTAL 
) 
8170             dc
.DrawText( line
, x
, y 
); 
8175             dc
.DrawRotatedText( line
, x
, y
, 90.0 ); 
8181 // Split multi-line text up into an array of strings. 
8182 // Any existing contents of the string array are preserved. 
8184 // TODO: refactor wxTextFile::Read() and reuse the same code from here 
8185 void wxGrid::StringToLines( const wxString
& value
, wxArrayString
& lines 
) const 
8189     wxString eol 
= wxTextFile::GetEOL( wxTextFileType_Unix 
); 
8190     wxString tVal 
= wxTextFile::Translate( value
, wxTextFileType_Unix 
); 
8192     while ( startPos 
< (int)tVal
.length() ) 
8194         pos 
= tVal
.Mid(startPos
).Find( eol 
); 
8199         else if ( pos 
== 0 ) 
8201             lines
.Add( wxEmptyString 
); 
8205             lines
.Add( tVal
.Mid(startPos
, pos
) ); 
8208         startPos 
+= pos 
+ 1; 
8211     if ( startPos 
< (int)tVal
.length() ) 
8213         lines
.Add( tVal
.Mid( startPos 
) ); 
8217 void wxGrid::GetTextBoxSize( const wxDC
& dc
, 
8218                              const wxArrayString
& lines
, 
8219                              long *width
, long *height 
) const 
8223     wxCoord lineW 
= 0, lineH 
= 0; 
8226     for ( i 
= 0; i 
< lines
.GetCount(); i
++ ) 
8228         dc
.GetTextExtent( lines
[i
], &lineW
, &lineH 
); 
8229         w 
= wxMax( w
, lineW 
); 
8238 // ------ Batch processing. 
8240 void wxGrid::EndBatch() 
8242     if ( m_batchCount 
> 0 ) 
8245         if ( !m_batchCount 
) 
8248             m_rowLabelWin
->Refresh(); 
8249             m_colLabelWin
->Refresh(); 
8250             m_cornerLabelWin
->Refresh(); 
8251             m_gridWin
->Refresh(); 
8256 // Use this, rather than wxWindow::Refresh(), to force an immediate 
8257 // repainting of the grid. Has no effect if you are already inside a 
8258 // BeginBatch / EndBatch block. 
8260 void wxGrid::ForceRefresh() 
8266 bool wxGrid::Enable(bool enable
) 
8268     if ( !wxScrolledWindow::Enable(enable
) ) 
8271     // redraw in the new state 
8272     m_gridWin
->Refresh(); 
8278 // ------ Edit control functions 
8281 void wxGrid::EnableEditing( bool edit 
) 
8283     // TODO: improve this ? 
8285     if ( edit 
!= m_editable 
) 
8288             EnableCellEditControl(edit
); 
8293 void wxGrid::EnableCellEditControl( bool enable 
) 
8298     if ( enable 
!= m_cellEditCtrlEnabled 
) 
8302             if (SendEvent( wxEVT_GRID_EDITOR_SHOWN
) <0) 
8305             // this should be checked by the caller! 
8306             wxASSERT_MSG( CanEnableCellControl(), _T("can't enable editing for this cell!") ); 
8308             // do it before ShowCellEditControl() 
8309             m_cellEditCtrlEnabled 
= enable
; 
8311             ShowCellEditControl(); 
8315             //FIXME:add veto support 
8316             SendEvent( wxEVT_GRID_EDITOR_HIDDEN 
); 
8318             HideCellEditControl(); 
8319             SaveEditControlValue(); 
8321             // do it after HideCellEditControl() 
8322             m_cellEditCtrlEnabled 
= enable
; 
8327 bool wxGrid::IsCurrentCellReadOnly() const 
8330     wxGridCellAttr
* attr 
= ((wxGrid 
*)this)->GetCellAttr(m_currentCellCoords
); 
8331     bool readonly 
= attr
->IsReadOnly(); 
8337 bool wxGrid::CanEnableCellControl() const 
8339     return m_editable 
&& (m_currentCellCoords 
!= wxGridNoCellCoords
) && 
8340         !IsCurrentCellReadOnly(); 
8343 bool wxGrid::IsCellEditControlEnabled() const 
8345     // the cell edit control might be disable for all cells or just for the 
8346     // current one if it's read only 
8347     return m_cellEditCtrlEnabled 
? !IsCurrentCellReadOnly() : false; 
8350 bool wxGrid::IsCellEditControlShown() const 
8352     bool isShown 
= false; 
8354     if ( m_cellEditCtrlEnabled 
) 
8356         int row 
= m_currentCellCoords
.GetRow(); 
8357         int col 
= m_currentCellCoords
.GetCol(); 
8358         wxGridCellAttr
* attr 
= GetCellAttr(row
, col
); 
8359         wxGridCellEditor
* editor 
= attr
->GetEditor((wxGrid
*) this, row
, col
); 
8364             if ( editor
->IsCreated() ) 
8366                 isShown 
= editor
->GetControl()->IsShown(); 
8376 void wxGrid::ShowCellEditControl() 
8378     if ( IsCellEditControlEnabled() ) 
8380         if ( !IsVisible( m_currentCellCoords
, false ) ) 
8382             m_cellEditCtrlEnabled 
= false; 
8387             wxRect rect 
= CellToRect( m_currentCellCoords 
); 
8388             int row 
= m_currentCellCoords
.GetRow(); 
8389             int col 
= m_currentCellCoords
.GetCol(); 
8391             // if this is part of a multicell, find owner (topleft) 
8392             int cell_rows
, cell_cols
; 
8393             GetCellSize( row
, col
, &cell_rows
, &cell_cols 
); 
8394             if ( cell_rows 
<= 0 || cell_cols 
<= 0 ) 
8398                 m_currentCellCoords
.SetRow( row 
); 
8399                 m_currentCellCoords
.SetCol( col 
); 
8402             // erase the highlight and the cell contents because the editor 
8403             // might not cover the entire cell 
8404             wxClientDC 
dc( m_gridWin 
); 
8406             wxGridCellAttr
* attr 
= GetCellAttr(row
, col
); 
8407             dc
.SetBrush(wxBrush(attr
->GetBackgroundColour(), wxBRUSHSTYLE_SOLID
)); 
8408             dc
.SetPen(*wxTRANSPARENT_PEN
); 
8409             dc
.DrawRectangle(rect
); 
8411             // convert to scrolled coords 
8412             CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y 
); 
8418             // cell is shifted by one pixel 
8419             // However, don't allow x or y to become negative 
8420             // since the SetSize() method interprets that as 
8427             wxGridCellEditor
* editor 
= attr
->GetEditor(this, row
, col
); 
8428             if ( !editor
->IsCreated() ) 
8430                 editor
->Create(m_gridWin
, wxID_ANY
, 
8431                                new wxGridCellEditorEvtHandler(this, editor
)); 
8433                 wxGridEditorCreatedEvent 
evt(GetId(), 
8434                                              wxEVT_GRID_EDITOR_CREATED
, 
8438                                              editor
->GetControl()); 
8439                 GetEventHandler()->ProcessEvent(evt
); 
8442             // resize editor to overflow into righthand cells if allowed 
8443             int maxWidth 
= rect
.width
; 
8444             wxString value 
= GetCellValue(row
, col
); 
8445             if ( (value 
!= wxEmptyString
) && (attr
->GetOverflow()) ) 
8448                 GetTextExtent(value
, &maxWidth
, &y
, NULL
, NULL
, &attr
->GetFont()); 
8449                 if (maxWidth 
< rect
.width
) 
8450                     maxWidth 
= rect
.width
; 
8453             int client_right 
= m_gridWin
->GetClientSize().GetWidth(); 
8454             if (rect
.x 
+ maxWidth 
> client_right
) 
8455                 maxWidth 
= client_right 
- rect
.x
; 
8457             if ((maxWidth 
> rect
.width
) && (col 
< m_numCols
) && m_table
) 
8459                 GetCellSize( row
, col
, &cell_rows
, &cell_cols 
); 
8460                 // may have changed earlier 
8461                 for (int i 
= col 
+ cell_cols
; i 
< m_numCols
; i
++) 
8464                     GetCellSize( row
, i
, &c_rows
, &c_cols 
); 
8466                     // looks weird going over a multicell 
8467                     if (m_table
->IsEmptyCell( row
, i 
) && 
8468                             (rect
.width 
< maxWidth
) && (c_rows 
== 1)) 
8470                         rect
.width 
+= GetColWidth( i 
); 
8476                 if (rect
.GetRight() > client_right
) 
8477                     rect
.SetRight( client_right 
- 1 ); 
8480             editor
->SetCellAttr( attr 
); 
8481             editor
->SetSize( rect 
); 
8483                 editor
->GetControl()->Move( 
8484                     editor
->GetControl()->GetPosition().x 
+ nXMove
, 
8485                     editor
->GetControl()->GetPosition().y 
); 
8486             editor
->Show( true, attr 
); 
8488             // recalc dimensions in case we need to 
8489             // expand the scrolled window to account for editor 
8492             editor
->BeginEdit(row
, col
, this); 
8493             editor
->SetCellAttr(NULL
); 
8501 void wxGrid::HideCellEditControl() 
8503     if ( IsCellEditControlEnabled() ) 
8505         int row 
= m_currentCellCoords
.GetRow(); 
8506         int col 
= m_currentCellCoords
.GetCol(); 
8508         wxGridCellAttr 
*attr 
= GetCellAttr(row
, col
); 
8509         wxGridCellEditor 
*editor 
= attr
->GetEditor(this, row
, col
); 
8510         editor
->Show( false ); 
8514         m_gridWin
->SetFocus(); 
8516         // refresh whole row to the right 
8517         wxRect 
rect( CellToRect(row
, col
) ); 
8518         CalcScrolledPosition(rect
.x
, rect
.y
, &rect
.x
, &rect
.y 
); 
8519         rect
.width 
= m_gridWin
->GetClientSize().GetWidth() - rect
.x
; 
8522         // ensure that the pixels under the focus ring get refreshed as well 
8523         rect
.Inflate(10, 10); 
8526         m_gridWin
->Refresh( false, &rect 
); 
8530 void wxGrid::SaveEditControlValue() 
8532     if ( IsCellEditControlEnabled() ) 
8534         int row 
= m_currentCellCoords
.GetRow(); 
8535         int col 
= m_currentCellCoords
.GetCol(); 
8537         wxString oldval 
= GetCellValue(row
, col
); 
8539         wxGridCellAttr
* attr 
= GetCellAttr(row
, col
); 
8540         wxGridCellEditor
* editor 
= attr
->GetEditor(this, row
, col
); 
8541         bool changed 
= editor
->EndEdit(row
, col
, this); 
8548             if ( SendEvent( wxEVT_GRID_CELL_CHANGE
, 
8549                        m_currentCellCoords
.GetRow(), 
8550                        m_currentCellCoords
.GetCol() ) < 0 ) 
8552                 // Event has been vetoed, set the data back. 
8553                 SetCellValue(row
, col
, oldval
); 
8560 // ------ Grid location functions 
8561 //  Note that all of these functions work with the logical coordinates of 
8562 //  grid cells and labels so you will need to convert from device 
8563 //  coordinates for mouse events etc. 
8566 void wxGrid::XYToCell( int x
, int y
, wxGridCellCoords
& coords 
) const 
8568     int row 
= YToRow(y
); 
8569     int col 
= XToCol(x
); 
8571     if ( row 
== -1 || col 
== -1 ) 
8573         coords 
= wxGridNoCellCoords
; 
8577         coords
.Set( row
, col 
); 
8581 // Internal Helper function for computing row or column from some 
8582 // (unscrolled) coordinate value, using either 
8583 // m_defaultRowHeight/m_defaultColWidth or binary search on array 
8584 // of m_rowBottoms/m_ColRights to speed up the search! 
8586 static int CoordToRowOrCol(int coord
, int defaultDist
, int minDist
, 
8587                            const wxArrayInt
& BorderArray
, int nMax
, 
8591         return clipToMinMax 
&& (nMax 
> 0) ? 0 : -1; 
8596     size_t i_max 
= coord 
/ defaultDist
, 
8599     if (BorderArray
.IsEmpty()) 
8601         if ((int) i_max 
< nMax
) 
8603         return clipToMinMax 
? nMax 
- 1 : -1; 
8606     if ( i_max 
>= BorderArray
.GetCount()) 
8608         i_max 
= BorderArray
.GetCount() - 1; 
8612         if ( coord 
>= BorderArray
[i_max
]) 
8616                 i_max 
= coord 
/ minDist
; 
8618                 i_max 
=  BorderArray
.GetCount() - 1; 
8621         if ( i_max 
>= BorderArray
.GetCount()) 
8622             i_max 
= BorderArray
.GetCount() - 1; 
8625     if ( coord 
>= BorderArray
[i_max
]) 
8626         return clipToMinMax 
? (int)i_max 
: -1; 
8627     if ( coord 
< BorderArray
[0] ) 
8630     while ( i_max 
- i_min 
> 0 ) 
8632         wxCHECK_MSG(BorderArray
[i_min
] <= coord 
&& coord 
< BorderArray
[i_max
], 
8633                     0, _T("wxGrid: internal error in CoordToRowOrCol")); 
8634         if (coord 
>=  BorderArray
[ i_max 
- 1]) 
8638         int median 
= i_min 
+ (i_max 
- i_min 
+ 1) / 2; 
8639         if (coord 
< BorderArray
[median
]) 
8648 int wxGrid::YToRow( int y 
) const 
8650     return CoordToRowOrCol(y
, m_defaultRowHeight
, 
8651                            m_minAcceptableRowHeight
, m_rowBottoms
, m_numRows
, false); 
8654 int wxGrid::XToCol( int x
, bool clipToMinMax 
) const 
8657         return clipToMinMax 
&& (m_numCols 
> 0) ? GetColAt( 0 ) : -1; 
8659     wxASSERT_MSG(m_defaultColWidth 
> 0, wxT("Default column width can not be zero")); 
8661     int maxPos 
= x 
/ m_defaultColWidth
; 
8664     if (m_colRights
.IsEmpty()) 
8666         if(maxPos 
< m_numCols
) 
8667             return GetColAt( maxPos 
); 
8668         return clipToMinMax 
? GetColAt( m_numCols 
- 1 ) : -1; 
8671     if ( maxPos 
>= m_numCols
) 
8672         maxPos 
= m_numCols 
- 1; 
8675         if ( x 
>= m_colRights
[GetColAt( maxPos 
)]) 
8678             if (m_minAcceptableColWidth
) 
8679                 maxPos 
= x 
/ m_minAcceptableColWidth
; 
8681                 maxPos 
=  m_numCols 
- 1; 
8683         if ( maxPos 
>= m_numCols
) 
8684             maxPos 
= m_numCols 
- 1; 
8687     //X is beyond the last column 
8688     if ( x 
>= m_colRights
[GetColAt( maxPos 
)]) 
8689         return clipToMinMax 
? GetColAt( maxPos 
) : -1; 
8691     //X is before the first column 
8692     if ( x 
< m_colRights
[GetColAt( 0 )] ) 
8693         return GetColAt( 0 ); 
8695     //Perform a binary search 
8696     while ( maxPos 
- minPos 
> 0 ) 
8698         wxCHECK_MSG(m_colRights
[GetColAt( minPos 
)] <= x 
&& x 
< m_colRights
[GetColAt( maxPos 
)], 
8699                     0, _T("wxGrid: internal error in XToCol")); 
8701         if (x 
>=  m_colRights
[GetColAt( maxPos 
- 1 )]) 
8702             return GetColAt( maxPos 
); 
8705         int median 
= minPos 
+ (maxPos 
- minPos 
+ 1) / 2; 
8706         if (x 
< m_colRights
[GetColAt( median 
)]) 
8711     return GetColAt( maxPos 
); 
8714 // return the row number that that the y coord is near 
8715 //  the edge of, or -1 if not near an edge. 
8716 // coords can only possibly be near an edge if 
8717 //    (a) the row/column is large enough to still allow for an "inner" area 
8718 //        that is _not_ nead the edge (i.e., if the height/width is smaller 
8719 //        than WXGRID_LABEL_EDGE_ZONE, coords are _never_ considered to be 
8722 //    (b) resizing rows/columns (the thing for which edge detection is 
8723 //        relevant at all) is enabled. 
8725 int wxGrid::YToEdgeOfRow( int y 
) const 
8728     i 
= internalYToRow(y
); 
8730     if ( GetRowHeight(i
) > WXGRID_LABEL_EDGE_ZONE 
&& CanDragRowSize() ) 
8732         // We know that we are in row i, test whether we are 
8733         // close enough to lower or upper border, respectively. 
8734         if ( abs(GetRowBottom(i
) - y
) < WXGRID_LABEL_EDGE_ZONE 
) 
8736         else if ( i 
> 0 && y 
- GetRowTop(i
) < WXGRID_LABEL_EDGE_ZONE 
) 
8743 // return the col number that that the x coord is near the edge of, or 
8744 // -1 if not near an edge 
8745 // See comment at YToEdgeOfRow for conditions on edge detection. 
8747 int wxGrid::XToEdgeOfCol( int x 
) const 
8750     i 
= internalXToCol(x
); 
8752     if ( GetColWidth(i
) > WXGRID_LABEL_EDGE_ZONE 
&& CanDragColSize() ) 
8754         // We know that we are in column i; test whether we are 
8755         // close enough to right or left border, respectively. 
8756         if ( abs(GetColRight(i
) - x
) < WXGRID_LABEL_EDGE_ZONE 
) 
8758         else if ( i 
> 0 && x 
- GetColLeft(i
) < WXGRID_LABEL_EDGE_ZONE 
) 
8765 wxRect 
wxGrid::CellToRect( int row
, int col 
) const 
8767     wxRect 
rect( -1, -1, -1, -1 ); 
8769     if ( row 
>= 0 && row 
< m_numRows 
&& 
8770          col 
>= 0 && col 
< m_numCols 
) 
8772         int i
, cell_rows
, cell_cols
; 
8773         rect
.width 
= rect
.height 
= 0; 
8774         GetCellSize( row
, col
, &cell_rows
, &cell_cols 
); 
8775         // if negative then find multicell owner 
8780         GetCellSize( row
, col
, &cell_rows
, &cell_cols 
); 
8782         rect
.x 
= GetColLeft(col
); 
8783         rect
.y 
= GetRowTop(row
); 
8784         for (i
=col
; i 
< col 
+ cell_cols
; i
++) 
8785             rect
.width 
+= GetColWidth(i
); 
8786         for (i
=row
; i 
< row 
+ cell_rows
; i
++) 
8787             rect
.height 
+= GetRowHeight(i
); 
8790     // if grid lines are enabled, then the area of the cell is a bit smaller 
8791     if (m_gridLinesEnabled
) 
8800 bool wxGrid::IsVisible( int row
, int col
, bool wholeCellVisible 
) const 
8802     // get the cell rectangle in logical coords 
8804     wxRect 
r( CellToRect( row
, col 
) ); 
8806     // convert to device coords 
8808     int left
, top
, right
, bottom
; 
8809     CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top 
); 
8810     CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom 
); 
8812     // check against the client area of the grid window 
8814     m_gridWin
->GetClientSize( &cw
, &ch 
); 
8816     if ( wholeCellVisible 
) 
8818         // is the cell wholly visible ? 
8819         return ( left 
>= 0 && right 
<= cw 
&& 
8820                  top 
>= 0 && bottom 
<= ch 
); 
8824         // is the cell partly visible ? 
8826         return ( ((left 
>= 0 && left 
< cw
) || (right 
> 0 && right 
<= cw
)) && 
8827                  ((top 
>= 0 && top 
< ch
) || (bottom 
> 0 && bottom 
<= ch
)) ); 
8831 // make the specified cell location visible by doing a minimal amount 
8834 void wxGrid::MakeCellVisible( int row
, int col 
) 
8837     int xpos 
= -1, ypos 
= -1; 
8839     if ( row 
>= 0 && row 
< m_numRows 
&& 
8840          col 
>= 0 && col 
< m_numCols 
) 
8842         // get the cell rectangle in logical coords 
8843         wxRect 
r( CellToRect( row
, col 
) ); 
8845         // convert to device coords 
8846         int left
, top
, right
, bottom
; 
8847         CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top 
); 
8848         CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom 
); 
8851         m_gridWin
->GetClientSize( &cw
, &ch 
); 
8857         else if ( bottom 
> ch 
) 
8859             int h 
= r
.GetHeight(); 
8861             for ( i 
= row 
- 1; i 
>= 0; i
-- ) 
8863                 int rowHeight 
= GetRowHeight(i
); 
8864                 if ( h 
+ rowHeight 
> ch 
) 
8871             // we divide it later by GRID_SCROLL_LINE, make sure that we don't 
8872             // have rounding errors (this is important, because if we do, 
8873             // we might not scroll at all and some cells won't be redrawn) 
8875             // Sometimes GRID_SCROLL_LINE / 2 is not enough, 
8876             // so just add a full scroll unit... 
8877             ypos 
+= m_scrollLineY
; 
8880         // special handling for wide cells - show always left part of the cell! 
8881         // Otherwise, e.g. when stepping from row to row, it would jump between 
8882         // left and right part of the cell on every step! 
8884         if ( left 
< 0 || (right 
- left
) >= cw 
) 
8888         else if ( right 
> cw 
) 
8890             // position the view so that the cell is on the right 
8892             CalcUnscrolledPosition(0, 0, &x0
, &y0
); 
8893             xpos 
= x0 
+ (right 
- cw
); 
8895             // see comment for ypos above 
8896             xpos 
+= m_scrollLineX
; 
8899         if ( xpos 
!= -1 || ypos 
!= -1 ) 
8902                 xpos 
/= m_scrollLineX
; 
8904                 ypos 
/= m_scrollLineY
; 
8905             Scroll( xpos
, ypos 
); 
8912 // ------ Grid cursor movement functions 
8915 bool wxGrid::MoveCursorUp( bool expandSelection 
) 
8917     if ( m_currentCellCoords 
!= wxGridNoCellCoords 
&& 
8918          m_currentCellCoords
.GetRow() >= 0 ) 
8920         if ( expandSelection 
) 
8922             if ( m_selectingKeyboard 
== wxGridNoCellCoords 
) 
8923                 m_selectingKeyboard 
= m_currentCellCoords
; 
8924             if ( m_selectingKeyboard
.GetRow() > 0 ) 
8926                 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() - 1 ); 
8927                 MakeCellVisible( m_selectingKeyboard
.GetRow(), 
8928                                  m_selectingKeyboard
.GetCol() ); 
8929                 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard 
); 
8932         else if ( m_currentCellCoords
.GetRow() > 0 ) 
8934             int row 
= m_currentCellCoords
.GetRow() - 1; 
8935             int col 
= m_currentCellCoords
.GetCol(); 
8937             MakeCellVisible( row
, col 
); 
8938             SetCurrentCell( row
, col 
); 
8949 bool wxGrid::MoveCursorDown( bool expandSelection 
) 
8951     if ( m_currentCellCoords 
!= wxGridNoCellCoords 
&& 
8952          m_currentCellCoords
.GetRow() < m_numRows 
) 
8954         if ( expandSelection 
) 
8956             if ( m_selectingKeyboard 
== wxGridNoCellCoords 
) 
8957                 m_selectingKeyboard 
= m_currentCellCoords
; 
8958             if ( m_selectingKeyboard
.GetRow() < m_numRows 
- 1 ) 
8960                 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() + 1 ); 
8961                 MakeCellVisible( m_selectingKeyboard
.GetRow(), 
8962                         m_selectingKeyboard
.GetCol() ); 
8963                 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard 
); 
8966         else if ( m_currentCellCoords
.GetRow() < m_numRows 
- 1 ) 
8968             int row 
= m_currentCellCoords
.GetRow() + 1; 
8969             int col 
= m_currentCellCoords
.GetCol(); 
8971             MakeCellVisible( row
, col 
); 
8972             SetCurrentCell( row
, col 
); 
8983 bool wxGrid::MoveCursorLeft( bool expandSelection 
) 
8985     if ( m_currentCellCoords 
!= wxGridNoCellCoords 
&& 
8986          m_currentCellCoords
.GetCol() >= 0 ) 
8988         if ( expandSelection 
) 
8990             if ( m_selectingKeyboard 
== wxGridNoCellCoords 
) 
8991                 m_selectingKeyboard 
= m_currentCellCoords
; 
8992             if ( m_selectingKeyboard
.GetCol() > 0 ) 
8994                 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() - 1 ); 
8995                 MakeCellVisible( m_selectingKeyboard
.GetRow(), 
8996                         m_selectingKeyboard
.GetCol() ); 
8997                 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard 
); 
9000         else if ( GetColPos( m_currentCellCoords
.GetCol() ) > 0 ) 
9002             int row 
= m_currentCellCoords
.GetRow(); 
9003             int col 
= GetColAt( GetColPos( m_currentCellCoords
.GetCol() ) - 1 ); 
9006             MakeCellVisible( row
, col 
); 
9007             SetCurrentCell( row
, col 
); 
9018 bool wxGrid::MoveCursorRight( bool expandSelection 
) 
9020     if ( m_currentCellCoords 
!= wxGridNoCellCoords 
&& 
9021          m_currentCellCoords
.GetCol() < m_numCols 
) 
9023         if ( expandSelection 
) 
9025             if ( m_selectingKeyboard 
== wxGridNoCellCoords 
) 
9026                 m_selectingKeyboard 
= m_currentCellCoords
; 
9027             if ( m_selectingKeyboard
.GetCol() < m_numCols 
- 1 ) 
9029                 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() + 1 ); 
9030                 MakeCellVisible( m_selectingKeyboard
.GetRow(), 
9031                         m_selectingKeyboard
.GetCol() ); 
9032                 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard 
); 
9035         else if ( GetColPos( m_currentCellCoords
.GetCol() ) < m_numCols 
- 1 ) 
9037             int row 
= m_currentCellCoords
.GetRow(); 
9038             int col 
= GetColAt( GetColPos( m_currentCellCoords
.GetCol() ) + 1 ); 
9041             MakeCellVisible( row
, col 
); 
9042             SetCurrentCell( row
, col 
); 
9053 bool wxGrid::MovePageUp() 
9055     if ( m_currentCellCoords 
== wxGridNoCellCoords 
) 
9058     int row 
= m_currentCellCoords
.GetRow(); 
9062         m_gridWin
->GetClientSize( &cw
, &ch 
); 
9064         int y 
= GetRowTop(row
); 
9065         int newRow 
= internalYToRow( y 
- ch 
+ 1 ); 
9067         if ( newRow 
== row 
) 
9069             // row > 0, so newRow can never be less than 0 here. 
9073         MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() ); 
9074         SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() ); 
9082 bool wxGrid::MovePageDown() 
9084     if ( m_currentCellCoords 
== wxGridNoCellCoords 
) 
9087     int row 
= m_currentCellCoords
.GetRow(); 
9088     if ( (row 
+ 1) < m_numRows 
) 
9091         m_gridWin
->GetClientSize( &cw
, &ch 
); 
9093         int y 
= GetRowTop(row
); 
9094         int newRow 
= internalYToRow( y 
+ ch 
); 
9095         if ( newRow 
== row 
) 
9097             // row < m_numRows, so newRow can't overflow here. 
9101         MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() ); 
9102         SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() ); 
9110 bool wxGrid::MoveCursorUpBlock( bool expandSelection 
) 
9113          m_currentCellCoords 
!= wxGridNoCellCoords 
&& 
9114          m_currentCellCoords
.GetRow() > 0 ) 
9116         int row 
= m_currentCellCoords
.GetRow(); 
9117         int col 
= m_currentCellCoords
.GetCol(); 
9119         if ( m_table
->IsEmptyCell(row
, col
) ) 
9121             // starting in an empty cell: find the next block of 
9127                 if ( !(m_table
->IsEmptyCell(row
, col
)) ) 
9131         else if ( m_table
->IsEmptyCell(row 
- 1, col
) ) 
9133             // starting at the top of a block: find the next block 
9139                 if ( !(m_table
->IsEmptyCell(row
, col
)) ) 
9145             // starting within a block: find the top of the block 
9150                 if ( m_table
->IsEmptyCell(row
, col
) ) 
9158         MakeCellVisible( row
, col 
); 
9159         if ( expandSelection 
) 
9161             m_selectingKeyboard 
= wxGridCellCoords( row
, col 
); 
9162             HighlightBlock( m_currentCellCoords
, m_selectingKeyboard 
); 
9167             SetCurrentCell( row
, col 
); 
9176 bool wxGrid::MoveCursorDownBlock( bool expandSelection 
) 
9179          m_currentCellCoords 
!= wxGridNoCellCoords 
&& 
9180          m_currentCellCoords
.GetRow() < m_numRows 
- 1 ) 
9182         int row 
= m_currentCellCoords
.GetRow(); 
9183         int col 
= m_currentCellCoords
.GetCol(); 
9185         if ( m_table
->IsEmptyCell(row
, col
) ) 
9187             // starting in an empty cell: find the next block of 
9190             while ( row 
< m_numRows 
- 1 ) 
9193                 if ( !(m_table
->IsEmptyCell(row
, col
)) ) 
9197         else if ( m_table
->IsEmptyCell(row 
+ 1, col
) ) 
9199             // starting at the bottom of a block: find the next block 
9202             while ( row 
< m_numRows 
- 1 ) 
9205                 if ( !(m_table
->IsEmptyCell(row
, col
)) ) 
9211             // starting within a block: find the bottom of the block 
9213             while ( row 
< m_numRows 
- 1 ) 
9216                 if ( m_table
->IsEmptyCell(row
, col
) ) 
9224         MakeCellVisible( row
, col 
); 
9225         if ( expandSelection 
) 
9227             m_selectingKeyboard 
= wxGridCellCoords( row
, col 
); 
9228             HighlightBlock( m_currentCellCoords
, m_selectingKeyboard 
); 
9233             SetCurrentCell( row
, col 
); 
9242 bool wxGrid::MoveCursorLeftBlock( bool expandSelection 
) 
9245          m_currentCellCoords 
!= wxGridNoCellCoords 
&& 
9246          m_currentCellCoords
.GetCol() > 0 ) 
9248         int row 
= m_currentCellCoords
.GetRow(); 
9249         int col 
= m_currentCellCoords
.GetCol(); 
9251         if ( m_table
->IsEmptyCell(row
, col
) ) 
9253             // starting in an empty cell: find the next block of 
9259                 if ( !(m_table
->IsEmptyCell(row
, col
)) ) 
9263         else if ( m_table
->IsEmptyCell(row
, col 
- 1) ) 
9265             // starting at the left of a block: find the next block 
9271                 if ( !(m_table
->IsEmptyCell(row
, col
)) ) 
9277             // starting within a block: find the left of the block 
9282                 if ( m_table
->IsEmptyCell(row
, col
) ) 
9290         MakeCellVisible( row
, col 
); 
9291         if ( expandSelection 
) 
9293             m_selectingKeyboard 
= wxGridCellCoords( row
, col 
); 
9294             HighlightBlock( m_currentCellCoords
, m_selectingKeyboard 
); 
9299             SetCurrentCell( row
, col 
); 
9308 bool wxGrid::MoveCursorRightBlock( bool expandSelection 
) 
9311          m_currentCellCoords 
!= wxGridNoCellCoords 
&& 
9312          m_currentCellCoords
.GetCol() < m_numCols 
- 1 ) 
9314         int row 
= m_currentCellCoords
.GetRow(); 
9315         int col 
= m_currentCellCoords
.GetCol(); 
9317         if ( m_table
->IsEmptyCell(row
, col
) ) 
9319             // starting in an empty cell: find the next block of 
9322             while ( col 
< m_numCols 
- 1 ) 
9325                 if ( !(m_table
->IsEmptyCell(row
, col
)) ) 
9329         else if ( m_table
->IsEmptyCell(row
, col 
+ 1) ) 
9331             // starting at the right of a block: find the next block 
9334             while ( col 
< m_numCols 
- 1 ) 
9337                 if ( !(m_table
->IsEmptyCell(row
, col
)) ) 
9343             // starting within a block: find the right of the block 
9345             while ( col 
< m_numCols 
- 1 ) 
9348                 if ( m_table
->IsEmptyCell(row
, col
) ) 
9356         MakeCellVisible( row
, col 
); 
9357         if ( expandSelection 
) 
9359             m_selectingKeyboard 
= wxGridCellCoords( row
, col 
); 
9360             HighlightBlock( m_currentCellCoords
, m_selectingKeyboard 
); 
9365             SetCurrentCell( row
, col 
); 
9375 // ------ Label values and formatting 
9378 void wxGrid::GetRowLabelAlignment( int *horiz
, int *vert 
) const 
9381         *horiz 
= m_rowLabelHorizAlign
; 
9383         *vert  
= m_rowLabelVertAlign
; 
9386 void wxGrid::GetColLabelAlignment( int *horiz
, int *vert 
) const 
9389         *horiz 
= m_colLabelHorizAlign
; 
9391         *vert  
= m_colLabelVertAlign
; 
9394 int wxGrid::GetColLabelTextOrientation() const 
9396     return m_colLabelTextOrientation
; 
9399 wxString 
wxGrid::GetRowLabelValue( int row 
) const 
9403         return m_table
->GetRowLabelValue( row 
); 
9413 wxString 
wxGrid::GetColLabelValue( int col 
) const 
9417         return m_table
->GetColLabelValue( col 
); 
9427 void wxGrid::SetRowLabelSize( int width 
) 
9429     wxASSERT( width 
>= 0 || width 
== wxGRID_AUTOSIZE 
); 
9431     if ( width 
== wxGRID_AUTOSIZE 
) 
9433         width 
= CalcColOrRowLabelAreaMinSize(wxGRID_ROW
); 
9436     if ( width 
!= m_rowLabelWidth 
) 
9440             m_rowLabelWin
->Show( false ); 
9441             m_cornerLabelWin
->Show( false ); 
9443         else if ( m_rowLabelWidth 
== 0 ) 
9445             m_rowLabelWin
->Show( true ); 
9446             if ( m_colLabelHeight 
> 0 ) 
9447                 m_cornerLabelWin
->Show( true ); 
9450         m_rowLabelWidth 
= width
; 
9452         wxScrolledWindow::Refresh( true ); 
9456 void wxGrid::SetColLabelSize( int height 
) 
9458     wxASSERT( height 
>=0 || height 
== wxGRID_AUTOSIZE 
); 
9460     if ( height 
== wxGRID_AUTOSIZE 
) 
9462         height 
= CalcColOrRowLabelAreaMinSize(wxGRID_COLUMN
); 
9465     if ( height 
!= m_colLabelHeight 
) 
9469             m_colLabelWin
->Show( false ); 
9470             m_cornerLabelWin
->Show( false ); 
9472         else if ( m_colLabelHeight 
== 0 ) 
9474             m_colLabelWin
->Show( true ); 
9475             if ( m_rowLabelWidth 
> 0 ) 
9476                 m_cornerLabelWin
->Show( true ); 
9479         m_colLabelHeight 
= height
; 
9481         wxScrolledWindow::Refresh( true ); 
9485 void wxGrid::SetLabelBackgroundColour( const wxColour
& colour 
) 
9487     if ( m_labelBackgroundColour 
!= colour 
) 
9489         m_labelBackgroundColour 
= colour
; 
9490         m_rowLabelWin
->SetBackgroundColour( colour 
); 
9491         m_colLabelWin
->SetBackgroundColour( colour 
); 
9492         m_cornerLabelWin
->SetBackgroundColour( colour 
); 
9494         if ( !GetBatchCount() ) 
9496             m_rowLabelWin
->Refresh(); 
9497             m_colLabelWin
->Refresh(); 
9498             m_cornerLabelWin
->Refresh(); 
9503 void wxGrid::SetLabelTextColour( const wxColour
& colour 
) 
9505     if ( m_labelTextColour 
!= colour 
) 
9507         m_labelTextColour 
= colour
; 
9508         if ( !GetBatchCount() ) 
9510             m_rowLabelWin
->Refresh(); 
9511             m_colLabelWin
->Refresh(); 
9516 void wxGrid::SetLabelFont( const wxFont
& font 
) 
9519     if ( !GetBatchCount() ) 
9521         m_rowLabelWin
->Refresh(); 
9522         m_colLabelWin
->Refresh(); 
9526 void wxGrid::SetRowLabelAlignment( int horiz
, int vert 
) 
9528     // allow old (incorrect) defs to be used 
9531         case wxLEFT
:   horiz 
= wxALIGN_LEFT
; break; 
9532         case wxRIGHT
:  horiz 
= wxALIGN_RIGHT
; break; 
9533         case wxCENTRE
: horiz 
= wxALIGN_CENTRE
; break; 
9538         case wxTOP
:    vert 
= wxALIGN_TOP
;    break; 
9539         case wxBOTTOM
: vert 
= wxALIGN_BOTTOM
; break; 
9540         case wxCENTRE
: vert 
= wxALIGN_CENTRE
; break; 
9543     if ( horiz 
== wxALIGN_LEFT 
|| horiz 
== wxALIGN_CENTRE 
|| horiz 
== wxALIGN_RIGHT 
) 
9545         m_rowLabelHorizAlign 
= horiz
; 
9548     if ( vert 
== wxALIGN_TOP 
|| vert 
== wxALIGN_CENTRE 
|| vert 
== wxALIGN_BOTTOM 
) 
9550         m_rowLabelVertAlign 
= vert
; 
9553     if ( !GetBatchCount() ) 
9555         m_rowLabelWin
->Refresh(); 
9559 void wxGrid::SetColLabelAlignment( int horiz
, int vert 
) 
9561     // allow old (incorrect) defs to be used 
9564         case wxLEFT
:   horiz 
= wxALIGN_LEFT
; break; 
9565         case wxRIGHT
:  horiz 
= wxALIGN_RIGHT
; break; 
9566         case wxCENTRE
: horiz 
= wxALIGN_CENTRE
; break; 
9571         case wxTOP
:    vert 
= wxALIGN_TOP
;    break; 
9572         case wxBOTTOM
: vert 
= wxALIGN_BOTTOM
; break; 
9573         case wxCENTRE
: vert 
= wxALIGN_CENTRE
; break; 
9576     if ( horiz 
== wxALIGN_LEFT 
|| horiz 
== wxALIGN_CENTRE 
|| horiz 
== wxALIGN_RIGHT 
) 
9578         m_colLabelHorizAlign 
= horiz
; 
9581     if ( vert 
== wxALIGN_TOP 
|| vert 
== wxALIGN_CENTRE 
|| vert 
== wxALIGN_BOTTOM 
) 
9583         m_colLabelVertAlign 
= vert
; 
9586     if ( !GetBatchCount() ) 
9588         m_colLabelWin
->Refresh(); 
9592 // Note: under MSW, the default column label font must be changed because it 
9593 //       does not support vertical printing 
9595 // Example: wxFont font(9, wxSWISS, wxNORMAL, wxBOLD); 
9596 //                      pGrid->SetLabelFont(font); 
9597 //                      pGrid->SetColLabelTextOrientation(wxVERTICAL); 
9599 void wxGrid::SetColLabelTextOrientation( int textOrientation 
) 
9601     if ( textOrientation 
== wxHORIZONTAL 
|| textOrientation 
== wxVERTICAL 
) 
9602         m_colLabelTextOrientation 
= textOrientation
; 
9604     if ( !GetBatchCount() ) 
9605         m_colLabelWin
->Refresh(); 
9608 void wxGrid::SetRowLabelValue( int row
, const wxString
& s 
) 
9612         m_table
->SetRowLabelValue( row
, s 
); 
9613         if ( !GetBatchCount() ) 
9615             wxRect rect 
= CellToRect( row
, 0 ); 
9616             if ( rect
.height 
> 0 ) 
9618                 CalcScrolledPosition(0, rect
.y
, &rect
.x
, &rect
.y
); 
9620                 rect
.width 
= m_rowLabelWidth
; 
9621                 m_rowLabelWin
->Refresh( true, &rect 
); 
9627 void wxGrid::SetColLabelValue( int col
, const wxString
& s 
) 
9631         m_table
->SetColLabelValue( col
, s 
); 
9632         if ( !GetBatchCount() ) 
9634             wxRect rect 
= CellToRect( 0, col 
); 
9635             if ( rect
.width 
> 0 ) 
9637                 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &rect
.y
); 
9639                 rect
.height 
= m_colLabelHeight
; 
9640                 m_colLabelWin
->Refresh( true, &rect 
); 
9646 void wxGrid::SetGridLineColour( const wxColour
& colour 
) 
9648     if ( m_gridLineColour 
!= colour 
) 
9650         m_gridLineColour 
= colour
; 
9652         wxClientDC 
dc( m_gridWin 
); 
9654         DrawAllGridLines( dc
, wxRegion() ); 
9658 void wxGrid::SetCellHighlightColour( const wxColour
& colour 
) 
9660     if ( m_cellHighlightColour 
!= colour 
) 
9662         m_cellHighlightColour 
= colour
; 
9664         wxClientDC 
dc( m_gridWin 
); 
9666         wxGridCellAttr
* attr 
= GetCellAttr(m_currentCellCoords
); 
9667         DrawCellHighlight(dc
, attr
); 
9672 void wxGrid::SetCellHighlightPenWidth(int width
) 
9674     if (m_cellHighlightPenWidth 
!= width
) 
9676         m_cellHighlightPenWidth 
= width
; 
9678         // Just redrawing the cell highlight is not enough since that won't 
9679         // make any visible change if the the thickness is getting smaller. 
9680         int row 
= m_currentCellCoords
.GetRow(); 
9681         int col 
= m_currentCellCoords
.GetCol(); 
9682         if ( row 
== -1 || col 
== -1 || GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 ) 
9685         wxRect rect 
= CellToRect(row
, col
); 
9686         m_gridWin
->Refresh(true, &rect
); 
9690 void wxGrid::SetCellHighlightROPenWidth(int width
) 
9692     if (m_cellHighlightROPenWidth 
!= width
) 
9694         m_cellHighlightROPenWidth 
= width
; 
9696         // Just redrawing the cell highlight is not enough since that won't 
9697         // make any visible change if the the thickness is getting smaller. 
9698         int row 
= m_currentCellCoords
.GetRow(); 
9699         int col 
= m_currentCellCoords
.GetCol(); 
9700         if ( row 
== -1 || col 
== -1 || 
9701                 GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 ) 
9704         wxRect rect 
= CellToRect(row
, col
); 
9705         m_gridWin
->Refresh(true, &rect
); 
9709 void wxGrid::EnableGridLines( bool enable 
) 
9711     if ( enable 
!= m_gridLinesEnabled 
) 
9713         m_gridLinesEnabled 
= enable
; 
9715         if ( !GetBatchCount() ) 
9719                 wxClientDC 
dc( m_gridWin 
); 
9721                 DrawAllGridLines( dc
, wxRegion() ); 
9725                 m_gridWin
->Refresh(); 
9731 int wxGrid::GetDefaultRowSize() const 
9733     return m_defaultRowHeight
; 
9736 int wxGrid::GetRowSize( int row 
) const 
9738     wxCHECK_MSG( row 
>= 0 && row 
< m_numRows
, 0, _T("invalid row index") ); 
9740     return GetRowHeight(row
); 
9743 int wxGrid::GetDefaultColSize() const 
9745     return m_defaultColWidth
; 
9748 int wxGrid::GetColSize( int col 
) const 
9750     wxCHECK_MSG( col 
>= 0 && col 
< m_numCols
, 0, _T("invalid column index") ); 
9752     return GetColWidth(col
); 
9755 // ============================================================================ 
9756 // access to the grid attributes: each of them has a default value in the grid 
9757 // itself and may be overidden on a per-cell basis 
9758 // ============================================================================ 
9760 // ---------------------------------------------------------------------------- 
9761 // setting default attributes 
9762 // ---------------------------------------------------------------------------- 
9764 void wxGrid::SetDefaultCellBackgroundColour( const wxColour
& col 
) 
9766     m_defaultCellAttr
->SetBackgroundColour(col
); 
9768     m_gridWin
->SetBackgroundColour(col
); 
9772 void wxGrid::SetDefaultCellTextColour( const wxColour
& col 
) 
9774     m_defaultCellAttr
->SetTextColour(col
); 
9777 void wxGrid::SetDefaultCellAlignment( int horiz
, int vert 
) 
9779     m_defaultCellAttr
->SetAlignment(horiz
, vert
); 
9782 void wxGrid::SetDefaultCellOverflow( bool allow 
) 
9784     m_defaultCellAttr
->SetOverflow(allow
); 
9787 void wxGrid::SetDefaultCellFont( const wxFont
& font 
) 
9789     m_defaultCellAttr
->SetFont(font
); 
9792 // For editors and renderers the type registry takes precedence over the 
9793 // default attr, so we need to register the new editor/renderer for the string 
9794 // data type in order to make setting a default editor/renderer appear to 
9797 void wxGrid::SetDefaultRenderer(wxGridCellRenderer 
*renderer
) 
9799     RegisterDataType(wxGRID_VALUE_STRING
, 
9801                      GetDefaultEditorForType(wxGRID_VALUE_STRING
)); 
9804 void wxGrid::SetDefaultEditor(wxGridCellEditor 
*editor
) 
9806     RegisterDataType(wxGRID_VALUE_STRING
, 
9807                      GetDefaultRendererForType(wxGRID_VALUE_STRING
), 
9811 // ---------------------------------------------------------------------------- 
9812 // access to the default attributes 
9813 // ---------------------------------------------------------------------------- 
9815 wxColour 
wxGrid::GetDefaultCellBackgroundColour() const 
9817     return m_defaultCellAttr
->GetBackgroundColour(); 
9820 wxColour 
wxGrid::GetDefaultCellTextColour() const 
9822     return m_defaultCellAttr
->GetTextColour(); 
9825 wxFont 
wxGrid::GetDefaultCellFont() const 
9827     return m_defaultCellAttr
->GetFont(); 
9830 void wxGrid::GetDefaultCellAlignment( int *horiz
, int *vert 
) const 
9832     m_defaultCellAttr
->GetAlignment(horiz
, vert
); 
9835 bool wxGrid::GetDefaultCellOverflow() const 
9837     return m_defaultCellAttr
->GetOverflow(); 
9840 wxGridCellRenderer 
*wxGrid::GetDefaultRenderer() const 
9842     return m_defaultCellAttr
->GetRenderer(NULL
, 0, 0); 
9845 wxGridCellEditor 
*wxGrid::GetDefaultEditor() const 
9847     return m_defaultCellAttr
->GetEditor(NULL
, 0, 0); 
9850 // ---------------------------------------------------------------------------- 
9851 // access to cell attributes 
9852 // ---------------------------------------------------------------------------- 
9854 wxColour 
wxGrid::GetCellBackgroundColour(int row
, int col
) const 
9856     wxGridCellAttr 
*attr 
= GetCellAttr(row
, col
); 
9857     wxColour colour 
= attr
->GetBackgroundColour(); 
9863 wxColour 
wxGrid::GetCellTextColour( int row
, int col 
) const 
9865     wxGridCellAttr 
*attr 
= GetCellAttr(row
, col
); 
9866     wxColour colour 
= attr
->GetTextColour(); 
9872 wxFont 
wxGrid::GetCellFont( int row
, int col 
) const 
9874     wxGridCellAttr 
*attr 
= GetCellAttr(row
, col
); 
9875     wxFont font 
= attr
->GetFont(); 
9881 void wxGrid::GetCellAlignment( int row
, int col
, int *horiz
, int *vert 
) const 
9883     wxGridCellAttr 
*attr 
= GetCellAttr(row
, col
); 
9884     attr
->GetAlignment(horiz
, vert
); 
9888 bool wxGrid::GetCellOverflow( int row
, int col 
) const 
9890     wxGridCellAttr 
*attr 
= GetCellAttr(row
, col
); 
9891     bool allow 
= attr
->GetOverflow(); 
9897 void wxGrid::GetCellSize( int row
, int col
, int *num_rows
, int *num_cols 
) const 
9899     wxGridCellAttr 
*attr 
= GetCellAttr(row
, col
); 
9900     attr
->GetSize( num_rows
, num_cols 
); 
9904 wxGridCellRenderer
* wxGrid::GetCellRenderer(int row
, int col
) const 
9906     wxGridCellAttr
* attr 
= GetCellAttr(row
, col
); 
9907     wxGridCellRenderer
* renderer 
= attr
->GetRenderer(this, row
, col
); 
9913 wxGridCellEditor
* wxGrid::GetCellEditor(int row
, int col
) const 
9915     wxGridCellAttr
* attr 
= GetCellAttr(row
, col
); 
9916     wxGridCellEditor
* editor 
= attr
->GetEditor(this, row
, col
); 
9922 bool wxGrid::IsReadOnly(int row
, int col
) const 
9924     wxGridCellAttr
* attr 
= GetCellAttr(row
, col
); 
9925     bool isReadOnly 
= attr
->IsReadOnly(); 
9931 // ---------------------------------------------------------------------------- 
9932 // attribute support: cache, automatic provider creation, ... 
9933 // ---------------------------------------------------------------------------- 
9935 bool wxGrid::CanHaveAttributes() const 
9942     return m_table
->CanHaveAttributes(); 
9945 void wxGrid::ClearAttrCache() 
9947     if ( m_attrCache
.row 
!= -1 ) 
9949         wxGridCellAttr 
*oldAttr 
= m_attrCache
.attr
; 
9950         m_attrCache
.attr 
= NULL
; 
9951         m_attrCache
.row 
= -1; 
9952         // wxSafeDecRec(...) might cause event processing that accesses 
9953         // the cached attribute, if one exists (e.g. by deleting the 
9954         // editor stored within the attribute). Therefore it is important 
9955         // to invalidate the cache  before calling wxSafeDecRef! 
9956         wxSafeDecRef(oldAttr
); 
9960 void wxGrid::CacheAttr(int row
, int col
, wxGridCellAttr 
*attr
) const 
9964         wxGrid 
*self 
= (wxGrid 
*)this;  // const_cast 
9966         self
->ClearAttrCache(); 
9967         self
->m_attrCache
.row 
= row
; 
9968         self
->m_attrCache
.col 
= col
; 
9969         self
->m_attrCache
.attr 
= attr
; 
9974 bool wxGrid::LookupAttr(int row
, int col
, wxGridCellAttr 
**attr
) const 
9976     if ( row 
== m_attrCache
.row 
&& col 
== m_attrCache
.col 
) 
9978         *attr 
= m_attrCache
.attr
; 
9979         wxSafeIncRef(m_attrCache
.attr
); 
9981 #ifdef DEBUG_ATTR_CACHE 
9982         gs_nAttrCacheHits
++; 
9989 #ifdef DEBUG_ATTR_CACHE 
9990         gs_nAttrCacheMisses
++; 
9997 wxGridCellAttr 
*wxGrid::GetCellAttr(int row
, int col
) const 
9999     wxGridCellAttr 
*attr 
= NULL
; 
10000     // Additional test to avoid looking at the cache e.g. for 
10001     // wxNoCellCoords, as this will confuse memory management. 
10004         if ( !LookupAttr(row
, col
, &attr
) ) 
10006             attr 
= m_table 
? m_table
->GetAttr(row
, col
, wxGridCellAttr::Any
) 
10007                            : (wxGridCellAttr 
*)NULL
; 
10008             CacheAttr(row
, col
, attr
); 
10014         attr
->SetDefAttr(m_defaultCellAttr
); 
10018         attr 
= m_defaultCellAttr
; 
10025 wxGridCellAttr 
*wxGrid::GetOrCreateCellAttr(int row
, int col
) const 
10027     wxGridCellAttr 
*attr 
= (wxGridCellAttr 
*)NULL
; 
10028     bool canHave 
= ((wxGrid
*)this)->CanHaveAttributes(); 
10030     wxCHECK_MSG( canHave
, attr
, _T("Cell attributes not allowed")); 
10031     wxCHECK_MSG( m_table
, attr
, _T("must have a table") ); 
10033     attr 
= m_table
->GetAttr(row
, col
, wxGridCellAttr::Cell
); 
10036         attr 
= new wxGridCellAttr(m_defaultCellAttr
); 
10038         // artificially inc the ref count to match DecRef() in caller 
10040         m_table
->SetAttr(attr
, row
, col
); 
10046 // ---------------------------------------------------------------------------- 
10047 // setting column attributes (wrappers around SetColAttr) 
10048 // ---------------------------------------------------------------------------- 
10050 void wxGrid::SetColFormatBool(int col
) 
10052     SetColFormatCustom(col
, wxGRID_VALUE_BOOL
); 
10055 void wxGrid::SetColFormatNumber(int col
) 
10057     SetColFormatCustom(col
, wxGRID_VALUE_NUMBER
); 
10060 void wxGrid::SetColFormatFloat(int col
, int width
, int precision
) 
10062     wxString typeName 
= wxGRID_VALUE_FLOAT
; 
10063     if ( (width 
!= -1) || (precision 
!= -1) ) 
10065         typeName 
<< _T(':') << width 
<< _T(',') << precision
; 
10068     SetColFormatCustom(col
, typeName
); 
10071 void wxGrid::SetColFormatCustom(int col
, const wxString
& typeName
) 
10073     wxGridCellAttr 
*attr 
= m_table
->GetAttr(-1, col
, wxGridCellAttr::Col 
); 
10075         attr 
= new wxGridCellAttr
; 
10076     wxGridCellRenderer 
*renderer 
= GetDefaultRendererForType(typeName
); 
10077     attr
->SetRenderer(renderer
); 
10078     wxGridCellEditor 
*editor 
= GetDefaultEditorForType(typeName
); 
10079     attr
->SetEditor(editor
); 
10081     SetColAttr(col
, attr
); 
10085 // ---------------------------------------------------------------------------- 
10086 // setting cell attributes: this is forwarded to the table 
10087 // ---------------------------------------------------------------------------- 
10089 void wxGrid::SetAttr(int row
, int col
, wxGridCellAttr 
*attr
) 
10091     if ( CanHaveAttributes() ) 
10093         m_table
->SetAttr(attr
, row
, col
); 
10098         wxSafeDecRef(attr
); 
10102 void wxGrid::SetRowAttr(int row
, wxGridCellAttr 
*attr
) 
10104     if ( CanHaveAttributes() ) 
10106         m_table
->SetRowAttr(attr
, row
); 
10111         wxSafeDecRef(attr
); 
10115 void wxGrid::SetColAttr(int col
, wxGridCellAttr 
*attr
) 
10117     if ( CanHaveAttributes() ) 
10119         m_table
->SetColAttr(attr
, col
); 
10124         wxSafeDecRef(attr
); 
10128 void wxGrid::SetCellBackgroundColour( int row
, int col
, const wxColour
& colour 
) 
10130     if ( CanHaveAttributes() ) 
10132         wxGridCellAttr 
*attr 
= GetOrCreateCellAttr(row
, col
); 
10133         attr
->SetBackgroundColour(colour
); 
10138 void wxGrid::SetCellTextColour( int row
, int col
, const wxColour
& colour 
) 
10140     if ( CanHaveAttributes() ) 
10142         wxGridCellAttr 
*attr 
= GetOrCreateCellAttr(row
, col
); 
10143         attr
->SetTextColour(colour
); 
10148 void wxGrid::SetCellFont( int row
, int col
, const wxFont
& font 
) 
10150     if ( CanHaveAttributes() ) 
10152         wxGridCellAttr 
*attr 
= GetOrCreateCellAttr(row
, col
); 
10153         attr
->SetFont(font
); 
10158 void wxGrid::SetCellAlignment( int row
, int col
, int horiz
, int vert 
) 
10160     if ( CanHaveAttributes() ) 
10162         wxGridCellAttr 
*attr 
= GetOrCreateCellAttr(row
, col
); 
10163         attr
->SetAlignment(horiz
, vert
); 
10168 void wxGrid::SetCellOverflow( int row
, int col
, bool allow 
) 
10170     if ( CanHaveAttributes() ) 
10172         wxGridCellAttr 
*attr 
= GetOrCreateCellAttr(row
, col
); 
10173         attr
->SetOverflow(allow
); 
10178 void wxGrid::SetCellSize( int row
, int col
, int num_rows
, int num_cols 
) 
10180     if ( CanHaveAttributes() ) 
10182         int cell_rows
, cell_cols
; 
10184         wxGridCellAttr 
*attr 
= GetOrCreateCellAttr(row
, col
); 
10185         attr
->GetSize(&cell_rows
, &cell_cols
); 
10186         attr
->SetSize(num_rows
, num_cols
); 
10189         // Cannot set the size of a cell to 0 or negative values 
10190         // While it is perfectly legal to do that, this function cannot 
10191         // handle all the possibilies, do it by hand by getting the CellAttr. 
10192         // You can only set the size of a cell to 1,1 or greater with this fn 
10193         wxASSERT_MSG( !((cell_rows 
< 1) || (cell_cols 
< 1)), 
10194                       wxT("wxGrid::SetCellSize setting cell size that is already part of another cell")); 
10195         wxASSERT_MSG( !((num_rows 
< 1) || (num_cols 
< 1)), 
10196                       wxT("wxGrid::SetCellSize setting cell size to < 1")); 
10198         // if this was already a multicell then "turn off" the other cells first 
10199         if ((cell_rows 
> 1) || (cell_cols 
> 1)) 
10202             for (j
=row
; j 
< row 
+ cell_rows
; j
++) 
10204                 for (i
=col
; i 
< col 
+ cell_cols
; i
++) 
10206                     if ((i 
!= col
) || (j 
!= row
)) 
10208                         wxGridCellAttr 
*attr_stub 
= GetOrCreateCellAttr(j
, i
); 
10209                         attr_stub
->SetSize( 1, 1 ); 
10210                         attr_stub
->DecRef(); 
10216         // mark the cells that will be covered by this cell to 
10217         // negative or zero values to point back at this cell 
10218         if (((num_rows 
> 1) || (num_cols 
> 1)) && (num_rows 
>= 1) && (num_cols 
>= 1)) 
10221             for (j
=row
; j 
< row 
+ num_rows
; j
++) 
10223                 for (i
=col
; i 
< col 
+ num_cols
; i
++) 
10225                     if ((i 
!= col
) || (j 
!= row
)) 
10227                         wxGridCellAttr 
*attr_stub 
= GetOrCreateCellAttr(j
, i
); 
10228                         attr_stub
->SetSize( row 
- j
, col 
- i 
); 
10229                         attr_stub
->DecRef(); 
10237 void wxGrid::SetCellRenderer(int row
, int col
, wxGridCellRenderer 
*renderer
) 
10239     if ( CanHaveAttributes() ) 
10241         wxGridCellAttr 
*attr 
= GetOrCreateCellAttr(row
, col
); 
10242         attr
->SetRenderer(renderer
); 
10247 void wxGrid::SetCellEditor(int row
, int col
, wxGridCellEditor
* editor
) 
10249     if ( CanHaveAttributes() ) 
10251         wxGridCellAttr 
*attr 
= GetOrCreateCellAttr(row
, col
); 
10252         attr
->SetEditor(editor
); 
10257 void wxGrid::SetReadOnly(int row
, int col
, bool isReadOnly
) 
10259     if ( CanHaveAttributes() ) 
10261         wxGridCellAttr 
*attr 
= GetOrCreateCellAttr(row
, col
); 
10262         attr
->SetReadOnly(isReadOnly
); 
10267 // ---------------------------------------------------------------------------- 
10268 // Data type registration 
10269 // ---------------------------------------------------------------------------- 
10271 void wxGrid::RegisterDataType(const wxString
& typeName
, 
10272                               wxGridCellRenderer
* renderer
, 
10273                               wxGridCellEditor
* editor
) 
10275     m_typeRegistry
->RegisterDataType(typeName
, renderer
, editor
); 
10279 wxGridCellEditor 
* wxGrid::GetDefaultEditorForCell(int row
, int col
) const 
10281     wxString typeName 
= m_table
->GetTypeName(row
, col
); 
10282     return GetDefaultEditorForType(typeName
); 
10285 wxGridCellRenderer 
* wxGrid::GetDefaultRendererForCell(int row
, int col
) const 
10287     wxString typeName 
= m_table
->GetTypeName(row
, col
); 
10288     return GetDefaultRendererForType(typeName
); 
10291 wxGridCellEditor 
* wxGrid::GetDefaultEditorForType(const wxString
& typeName
) const 
10293     int index 
= m_typeRegistry
->FindOrCloneDataType(typeName
); 
10294     if ( index 
== wxNOT_FOUND 
) 
10296         wxFAIL_MSG(wxString::Format(wxT("Unknown data type name [%s]"), typeName
.c_str())); 
10301     return m_typeRegistry
->GetEditor(index
); 
10304 wxGridCellRenderer 
* wxGrid::GetDefaultRendererForType(const wxString
& typeName
) const 
10306     int index 
= m_typeRegistry
->FindOrCloneDataType(typeName
); 
10307     if ( index 
== wxNOT_FOUND 
) 
10309         wxFAIL_MSG(wxString::Format(wxT("Unknown data type name [%s]"), typeName
.c_str())); 
10314     return m_typeRegistry
->GetRenderer(index
); 
10317 // ---------------------------------------------------------------------------- 
10319 // ---------------------------------------------------------------------------- 
10321 void wxGrid::EnableDragRowSize( bool enable 
) 
10323     m_canDragRowSize 
= enable
; 
10326 void wxGrid::EnableDragColSize( bool enable 
) 
10328     m_canDragColSize 
= enable
; 
10331 void wxGrid::EnableDragGridSize( bool enable 
) 
10333     m_canDragGridSize 
= enable
; 
10336 void wxGrid::EnableDragCell( bool enable 
) 
10338     m_canDragCell 
= enable
; 
10341 void wxGrid::SetDefaultRowSize( int height
, bool resizeExistingRows 
) 
10343     m_defaultRowHeight 
= wxMax( height
, m_minAcceptableRowHeight 
); 
10345     if ( resizeExistingRows 
) 
10347         // since we are resizing all rows to the default row size, 
10348         // we can simply clear the row heights and row bottoms 
10349         // arrays (which also allows us to take advantage of 
10350         // some speed optimisations) 
10351         m_rowHeights
.Empty(); 
10352         m_rowBottoms
.Empty(); 
10353         if ( !GetBatchCount() ) 
10358 void wxGrid::SetRowSize( int row
, int height 
) 
10360     wxCHECK_RET( row 
>= 0 && row 
< m_numRows
, _T("invalid row index") ); 
10362     // if < 0 then calculate new height from label 
10366         wxArrayString lines
; 
10367         wxClientDC 
dc(m_rowLabelWin
); 
10368         dc
.SetFont(GetLabelFont()); 
10369         StringToLines(GetRowLabelValue( row 
), lines
); 
10370         GetTextBoxSize( dc
, lines
, &w
, &h 
); 
10371         //check that it is not less than the minimal height 
10372         height 
= wxMax(h
, GetRowMinimalAcceptableHeight()); 
10375     // See comment in SetColSize 
10376     if ( height 
< GetRowMinimalAcceptableHeight()) 
10379     if ( m_rowHeights
.IsEmpty() ) 
10381         // need to really create the array 
10385     int h 
= wxMax( 0, height 
); 
10386     int diff 
= h 
- m_rowHeights
[row
]; 
10388     m_rowHeights
[row
] = h
; 
10389     for ( int i 
= row
; i 
< m_numRows
; i
++ ) 
10391         m_rowBottoms
[i
] += diff
; 
10394     if ( !GetBatchCount() ) 
10398 void wxGrid::SetDefaultColSize( int width
, bool resizeExistingCols 
) 
10400     // we dont allow zero default column width 
10401     m_defaultColWidth 
= wxMax( wxMax( width
, m_minAcceptableColWidth 
), 1 ); 
10403     if ( resizeExistingCols 
) 
10405         // since we are resizing all columns to the default column size, 
10406         // we can simply clear the col widths and col rights 
10407         // arrays (which also allows us to take advantage of 
10408         // some speed optimisations) 
10409         m_colWidths
.Empty(); 
10410         m_colRights
.Empty(); 
10411         if ( !GetBatchCount() ) 
10416 void wxGrid::SetColSize( int col
, int width 
) 
10418     wxCHECK_RET( col 
>= 0 && col 
< m_numCols
, _T("invalid column index") ); 
10420     // if < 0 then calculate new width from label 
10424         wxArrayString lines
; 
10425         wxClientDC 
dc(m_colLabelWin
); 
10426         dc
.SetFont(GetLabelFont()); 
10427         StringToLines(GetColLabelValue(col
), lines
); 
10428         if ( GetColLabelTextOrientation() == wxHORIZONTAL 
) 
10429             GetTextBoxSize( dc
, lines
, &w
, &h 
); 
10431             GetTextBoxSize( dc
, lines
, &h
, &w 
); 
10433         //check that it is not less than the minimal width 
10434         width 
= wxMax(width
, GetColMinimalAcceptableWidth()); 
10437     // should we check that it's bigger than GetColMinimalWidth(col) here? 
10439     // No, because it is reasonable to assume the library user know's 
10440     // what he is doing. However we should test against the weaker 
10441     // constraint of minimalAcceptableWidth, as this breaks rendering 
10443     // This test then fixes sf.net bug #645734 
10445     if ( width 
< GetColMinimalAcceptableWidth() ) 
10448     if ( m_colWidths
.IsEmpty() ) 
10450         // need to really create the array 
10454     int w 
= wxMax( 0, width 
); 
10455     int diff 
= w 
- m_colWidths
[col
]; 
10456     m_colWidths
[col
] = w
; 
10458     for ( int colPos 
= GetColPos(col
); colPos 
< m_numCols
; colPos
++ ) 
10460         m_colRights
[GetColAt(colPos
)] += diff
; 
10463     if ( !GetBatchCount() ) 
10467 void wxGrid::SetColMinimalWidth( int col
, int width 
) 
10469     if (width 
> GetColMinimalAcceptableWidth()) 
10471         wxLongToLongHashMap::key_type key 
= (wxLongToLongHashMap::key_type
)col
; 
10472         m_colMinWidths
[key
] = width
; 
10476 void wxGrid::SetRowMinimalHeight( int row
, int width 
) 
10478     if (width 
> GetRowMinimalAcceptableHeight()) 
10480         wxLongToLongHashMap::key_type key 
= (wxLongToLongHashMap::key_type
)row
; 
10481         m_rowMinHeights
[key
] = width
; 
10485 int wxGrid::GetColMinimalWidth(int col
) const 
10487     wxLongToLongHashMap::key_type key 
= (wxLongToLongHashMap::key_type
)col
; 
10488     wxLongToLongHashMap::const_iterator it 
= m_colMinWidths
.find(key
); 
10490     return it 
!= m_colMinWidths
.end() ? (int)it
->second 
: m_minAcceptableColWidth
; 
10493 int wxGrid::GetRowMinimalHeight(int row
) const 
10495     wxLongToLongHashMap::key_type key 
= (wxLongToLongHashMap::key_type
)row
; 
10496     wxLongToLongHashMap::const_iterator it 
= m_rowMinHeights
.find(key
); 
10498     return it 
!= m_rowMinHeights
.end() ? (int)it
->second 
: m_minAcceptableRowHeight
; 
10501 void wxGrid::SetColMinimalAcceptableWidth( int width 
) 
10503     // We do allow a width of 0 since this gives us 
10504     // an easy way to temporarily hiding columns. 
10506         m_minAcceptableColWidth 
= width
; 
10509 void wxGrid::SetRowMinimalAcceptableHeight( int height 
) 
10511     // We do allow a height of 0 since this gives us 
10512     // an easy way to temporarily hiding rows. 
10514         m_minAcceptableRowHeight 
= height
; 
10517 int  wxGrid::GetColMinimalAcceptableWidth() const 
10519     return m_minAcceptableColWidth
; 
10522 int  wxGrid::GetRowMinimalAcceptableHeight() const 
10524     return m_minAcceptableRowHeight
; 
10527 // ---------------------------------------------------------------------------- 
10529 // ---------------------------------------------------------------------------- 
10532 wxGrid::AutoSizeColOrRow(int colOrRow
, bool setAsMin
, wxGridDirection direction
) 
10534     const bool column 
= direction 
== wxGRID_COLUMN
; 
10536     wxClientDC 
dc(m_gridWin
); 
10538     // cancel editing of cell 
10539     HideCellEditControl(); 
10540     SaveEditControlValue(); 
10542     // init both of them to avoid compiler warnings, even if we only need one 
10550     wxCoord extent
, extentMax 
= 0; 
10551     int max 
= column 
? m_numRows 
: m_numCols
; 
10552     for ( int rowOrCol 
= 0; rowOrCol 
< max
; rowOrCol
++ ) 
10559         wxGridCellAttr 
*attr 
= GetCellAttr(row
, col
); 
10560         wxGridCellRenderer 
*renderer 
= attr
->GetRenderer(this, row
, col
); 
10563             wxSize size 
= renderer
->GetBestSize(*this, *attr
, dc
, row
, col
); 
10564             extent 
= column 
? size
.x 
: size
.y
; 
10565             if ( extent 
> extentMax 
) 
10566                 extentMax 
= extent
; 
10568             renderer
->DecRef(); 
10574     // now also compare with the column label extent 
10576     dc
.SetFont( GetLabelFont() ); 
10580         dc
.GetMultiLineTextExtent( GetColLabelValue(col
), &w
, &h 
); 
10581         if ( GetColLabelTextOrientation() == wxVERTICAL 
) 
10585         dc
.GetMultiLineTextExtent( GetRowLabelValue(row
), &w
, &h 
); 
10587     extent 
= column 
? w 
: h
; 
10588     if ( extent 
> extentMax 
) 
10589         extentMax 
= extent
; 
10593         // empty column - give default extent (notice that if extentMax is less 
10594         // than default extent but != 0, it's OK) 
10595         extentMax 
= column 
? m_defaultColWidth 
: m_defaultRowHeight
; 
10600             // leave some space around text 
10608         // Ensure automatic width is not less than minimal width. See the 
10609         // comment in SetColSize() for explanation of why this isn't done 
10610         // in SetColSize(). 
10612             extentMax 
= wxMax(extentMax
, GetColMinimalWidth(col
)); 
10614         SetColSize( col
, extentMax 
); 
10615         if ( !GetBatchCount() ) 
10618             m_gridWin
->GetClientSize( &cw
, &ch 
); 
10619             wxRect 
rect ( CellToRect( 0, col 
) ); 
10621             CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
); 
10622             rect
.width 
= cw 
- rect
.x
; 
10623             rect
.height 
= m_colLabelHeight
; 
10624             m_colLabelWin
->Refresh( true, &rect 
); 
10629         // Ensure automatic width is not less than minimal height. See the 
10630         // comment in SetColSize() for explanation of why this isn't done 
10631         // in SetRowSize(). 
10633             extentMax 
= wxMax(extentMax
, GetRowMinimalHeight(row
)); 
10635         SetRowSize(row
, extentMax
); 
10636         if ( !GetBatchCount() ) 
10639             m_gridWin
->GetClientSize( &cw
, &ch 
); 
10640             wxRect 
rect( CellToRect( row
, 0 ) ); 
10642             CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
); 
10643             rect
.width 
= m_rowLabelWidth
; 
10644             rect
.height 
= ch 
- rect
.y
; 
10645             m_rowLabelWin
->Refresh( true, &rect 
); 
10652             SetColMinimalWidth(col
, extentMax
); 
10654             SetRowMinimalHeight(row
, extentMax
); 
10658 wxCoord 
wxGrid::CalcColOrRowLabelAreaMinSize(wxGridDirection direction
) 
10660     // calculate size for the rows or columns? 
10661     const bool calcRows 
= direction 
== wxGRID_ROW
; 
10663     wxClientDC 
dc(calcRows 
? GetGridRowLabelWindow() 
10664                            : GetGridColLabelWindow()); 
10665     dc
.SetFont(GetLabelFont()); 
10667     // which dimension should we take into account for calculations? 
10669     // for columns, the text can be only horizontal so it's easy but for rows 
10670     // we also have to take into account the text orientation 
10672         useWidth 
= calcRows 
|| (GetColLabelTextOrientation() == wxVERTICAL
); 
10674     wxArrayString lines
; 
10675     wxCoord extentMax 
= 0; 
10677     const int numRowsOrCols 
= calcRows 
? m_numRows 
: m_numCols
; 
10678     for ( int rowOrCol 
= 0; rowOrCol 
< numRowsOrCols
; rowOrCol
++ ) 
10682         wxString label 
= calcRows 
? GetRowLabelValue(rowOrCol
) 
10683                                   : GetColLabelValue(rowOrCol
); 
10684         StringToLines(label
, lines
); 
10687         GetTextBoxSize(dc
, lines
, &w
, &h
); 
10689         const wxCoord extent 
= useWidth 
? w 
: h
; 
10690         if ( extent 
> extentMax 
) 
10691             extentMax 
= extent
; 
10696         // empty column - give default extent (notice that if extentMax is less 
10697         // than default extent but != 0, it's OK) 
10698         extentMax 
= calcRows 
? GetDefaultRowLabelSize() 
10699                              : GetDefaultColLabelSize(); 
10702     // leave some space around text (taken from AutoSizeColOrRow) 
10711 int wxGrid::SetOrCalcColumnSizes(bool calcOnly
, bool setAsMin
) 
10713     int width 
= m_rowLabelWidth
; 
10715     wxGridUpdateLocker locker
; 
10717         locker
.Create(this); 
10719     for ( int col 
= 0; col 
< m_numCols
; col
++ ) 
10722             AutoSizeColumn(col
, setAsMin
); 
10724         width 
+= GetColWidth(col
); 
10730 int wxGrid::SetOrCalcRowSizes(bool calcOnly
, bool setAsMin
) 
10732     int height 
= m_colLabelHeight
; 
10734     wxGridUpdateLocker locker
; 
10736         locker
.Create(this); 
10738     for ( int row 
= 0; row 
< m_numRows
; row
++ ) 
10741             AutoSizeRow(row
, setAsMin
); 
10743         height 
+= GetRowHeight(row
); 
10749 void wxGrid::AutoSize() 
10751     wxGridUpdateLocker 
locker(this); 
10753     wxSize 
size(SetOrCalcColumnSizes(false) - m_rowLabelWidth 
+ m_extraWidth
, 
10754                 SetOrCalcRowSizes(false) - m_colLabelHeight 
+ m_extraHeight
); 
10756     // we know that we're not going to have scrollbars so disable them now to 
10757     // avoid trouble in SetClientSize() which can otherwise set the correct 
10758     // client size but also leave space for (not needed any more) scrollbars 
10759     SetScrollbars(0, 0, 0, 0, 0, 0, true); 
10761     // restore the scroll rate parameters overwritten by SetScrollbars() 
10762     SetScrollRate(m_scrollLineX
, m_scrollLineY
); 
10764     SetClientSize(size
.x 
+ m_rowLabelWidth
, size
.y 
+ m_colLabelHeight
); 
10767 void wxGrid::AutoSizeRowLabelSize( int row 
) 
10769     // Hide the edit control, so it 
10770     // won't interfere with drag-shrinking. 
10771     if ( IsCellEditControlShown() ) 
10773         HideCellEditControl(); 
10774         SaveEditControlValue(); 
10777     // autosize row height depending on label text 
10778     SetRowSize(row
, -1); 
10782 void wxGrid::AutoSizeColLabelSize( int col 
) 
10784     // Hide the edit control, so it 
10785     // won't interfere with drag-shrinking. 
10786     if ( IsCellEditControlShown() ) 
10788         HideCellEditControl(); 
10789         SaveEditControlValue(); 
10792     // autosize column width depending on label text 
10793     SetColSize(col
, -1); 
10797 wxSize 
wxGrid::DoGetBestSize() const 
10799     wxGrid 
*self 
= (wxGrid 
*)this;  // const_cast 
10801     // we do the same as in AutoSize() here with the exception that we don't 
10802     // change the column/row sizes, only calculate them 
10803     wxSize 
size(self
->SetOrCalcColumnSizes(true) - m_rowLabelWidth 
+ m_extraWidth
, 
10804                 self
->SetOrCalcRowSizes(true) - m_colLabelHeight 
+ m_extraHeight
); 
10806     // NOTE: This size should be cached, but first we need to add calls to 
10807     // InvalidateBestSize everywhere that could change the results of this 
10809     // CacheBestSize(size); 
10811     return wxSize(size
.x 
+ m_rowLabelWidth
, size
.y 
+ m_colLabelHeight
) 
10812             + GetWindowBorderSize(); 
10820 wxPen
& wxGrid::GetDividerPen() const 
10825 // ---------------------------------------------------------------------------- 
10826 // cell value accessor functions 
10827 // ---------------------------------------------------------------------------- 
10829 void wxGrid::SetCellValue( int row
, int col
, const wxString
& s 
) 
10833         m_table
->SetValue( row
, col
, s 
); 
10834         if ( !GetBatchCount() ) 
10837             wxRect 
rect( CellToRect( row
, col 
) ); 
10839             rect
.width 
= m_gridWin
->GetClientSize().GetWidth(); 
10840             CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
); 
10841             m_gridWin
->Refresh( false, &rect 
); 
10844         if ( m_currentCellCoords
.GetRow() == row 
&& 
10845              m_currentCellCoords
.GetCol() == col 
&& 
10846              IsCellEditControlShown()) 
10847              // Note: If we are using IsCellEditControlEnabled, 
10848              // this interacts badly with calling SetCellValue from 
10849              // an EVT_GRID_CELL_CHANGE handler. 
10851             HideCellEditControl(); 
10852             ShowCellEditControl(); // will reread data from table 
10857 // ---------------------------------------------------------------------------- 
10858 // block, row and column selection 
10859 // ---------------------------------------------------------------------------- 
10861 void wxGrid::SelectRow( int row
, bool addToSelected 
) 
10863     if ( IsSelection() && !addToSelected 
) 
10867         m_selection
->SelectRow( row
, false, addToSelected 
); 
10870 void wxGrid::SelectCol( int col
, bool addToSelected 
) 
10872     if ( IsSelection() && !addToSelected 
) 
10876         m_selection
->SelectCol( col
, false, addToSelected 
); 
10879 void wxGrid::SelectBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
, 
10880                           bool addToSelected 
) 
10882     if ( IsSelection() && !addToSelected 
) 
10886         m_selection
->SelectBlock( topRow
, leftCol
, bottomRow
, rightCol
, 
10887                                   false, addToSelected 
); 
10890 void wxGrid::SelectAll() 
10892     if ( m_numRows 
> 0 && m_numCols 
> 0 ) 
10895             m_selection
->SelectBlock( 0, 0, m_numRows 
- 1, m_numCols 
- 1 ); 
10899 // ---------------------------------------------------------------------------- 
10900 // cell, row and col deselection 
10901 // ---------------------------------------------------------------------------- 
10903 void wxGrid::DeselectRow( int row 
) 
10905     if ( !m_selection 
) 
10908     if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows 
) 
10910         if ( m_selection
->IsInSelection(row
, 0 ) ) 
10911             m_selection
->ToggleCellSelection(row
, 0); 
10915         int nCols 
= GetNumberCols(); 
10916         for ( int i 
= 0; i 
< nCols
; i
++ ) 
10918             if ( m_selection
->IsInSelection(row
, i 
) ) 
10919                 m_selection
->ToggleCellSelection(row
, i
); 
10924 void wxGrid::DeselectCol( int col 
) 
10926     if ( !m_selection 
) 
10929     if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns 
) 
10931         if ( m_selection
->IsInSelection(0, col 
) ) 
10932             m_selection
->ToggleCellSelection(0, col
); 
10936         int nRows 
= GetNumberRows(); 
10937         for ( int i 
= 0; i 
< nRows
; i
++ ) 
10939             if ( m_selection
->IsInSelection(i
, col 
) ) 
10940                 m_selection
->ToggleCellSelection(i
, col
); 
10945 void wxGrid::DeselectCell( int row
, int col 
) 
10947     if ( m_selection 
&& m_selection
->IsInSelection(row
, col
) ) 
10948         m_selection
->ToggleCellSelection(row
, col
); 
10951 bool wxGrid::IsSelection() const 
10953     return ( m_selection 
&& (m_selection
->IsSelection() || 
10954              ( m_selectingTopLeft 
!= wxGridNoCellCoords 
&& 
10955                m_selectingBottomRight 
!= wxGridNoCellCoords
) ) ); 
10958 bool wxGrid::IsInSelection( int row
, int col 
) const 
10960     return ( m_selection 
&& (m_selection
->IsInSelection( row
, col 
) || 
10961              ( row 
>= m_selectingTopLeft
.GetRow() && 
10962                col 
>= m_selectingTopLeft
.GetCol() && 
10963                row 
<= m_selectingBottomRight
.GetRow() && 
10964                col 
<= m_selectingBottomRight
.GetCol() )) ); 
10967 wxGridCellCoordsArray 
wxGrid::GetSelectedCells() const 
10971         wxGridCellCoordsArray a
; 
10975     return m_selection
->m_cellSelection
; 
10978 wxGridCellCoordsArray 
wxGrid::GetSelectionBlockTopLeft() const 
10982         wxGridCellCoordsArray a
; 
10986     return m_selection
->m_blockSelectionTopLeft
; 
10989 wxGridCellCoordsArray 
wxGrid::GetSelectionBlockBottomRight() const 
10993         wxGridCellCoordsArray a
; 
10997     return m_selection
->m_blockSelectionBottomRight
; 
11000 wxArrayInt 
wxGrid::GetSelectedRows() const 
11008     return m_selection
->m_rowSelection
; 
11011 wxArrayInt 
wxGrid::GetSelectedCols() const 
11019     return m_selection
->m_colSelection
; 
11022 void wxGrid::ClearSelection() 
11024     wxRect r1 
= BlockToDeviceRect( m_selectingTopLeft
, m_selectingBottomRight
); 
11025     wxRect r2 
= BlockToDeviceRect( m_currentCellCoords
, m_selectingKeyboard 
); 
11026     m_selectingTopLeft 
= 
11027     m_selectingBottomRight 
= 
11028     m_selectingKeyboard 
= wxGridNoCellCoords
; 
11029     Refresh( false, &r1 
); 
11030     Refresh( false, &r2 
); 
11032         m_selection
->ClearSelection(); 
11035 // This function returns the rectangle that encloses the given block 
11036 // in device coords clipped to the client size of the grid window. 
11038 wxRect 
wxGrid::BlockToDeviceRect( const wxGridCellCoords
& topLeft
, 
11039                                   const wxGridCellCoords
& bottomRight 
) const 
11042     wxRect tempCellRect 
= CellToRect(topLeft
); 
11043     if ( tempCellRect 
!= wxGridNoCellRect 
) 
11045         resultRect 
= tempCellRect
; 
11049         resultRect 
= wxRect(0, 0, 0, 0); 
11052     tempCellRect 
= CellToRect(bottomRight
); 
11053     if ( tempCellRect 
!= wxGridNoCellRect 
) 
11055         resultRect 
+= tempCellRect
; 
11059         // If both inputs were "wxGridNoCellRect," then there's nothing to do. 
11060         return wxGridNoCellRect
; 
11063     // Ensure that left/right and top/bottom pairs are in order. 
11064     int left 
= resultRect
.GetLeft(); 
11065     int top 
= resultRect
.GetTop(); 
11066     int right 
= resultRect
.GetRight(); 
11067     int bottom 
= resultRect
.GetBottom(); 
11069     int leftCol 
= topLeft
.GetCol(); 
11070     int topRow 
= topLeft
.GetRow(); 
11071     int rightCol 
= bottomRight
.GetCol(); 
11072     int bottomRow 
= bottomRight
.GetRow(); 
11081         leftCol 
= rightCol
; 
11092         topRow 
= bottomRow
; 
11096     // The following loop is ONLY necessary to detect and handle merged cells. 
11098     m_gridWin
->GetClientSize( &cw
, &ch 
); 
11100     // Get the origin coordinates: notice that they will be negative if the 
11101     // grid is scrolled downwards/to the right. 
11102     int gridOriginX 
= 0; 
11103     int gridOriginY 
= 0; 
11104     CalcScrolledPosition(gridOriginX
, gridOriginY
, &gridOriginX
, &gridOriginY
); 
11106     int onScreenLeftmostCol 
= internalXToCol(-gridOriginX
); 
11107     int onScreenUppermostRow 
= internalYToRow(-gridOriginY
); 
11109     int onScreenRightmostCol 
= internalXToCol(-gridOriginX 
+ cw
); 
11110     int onScreenBottommostRow 
= internalYToRow(-gridOriginY 
+ ch
); 
11112     // Bound our loop so that we only examine the portion of the selected block 
11113     // that is shown on screen. Therefore, we compare the Top-Left block values 
11114     // to the Top-Left screen values, and the Bottom-Right block values to the 
11115     // Bottom-Right screen values, choosing appropriately. 
11116     const int visibleTopRow 
= wxMax(topRow
, onScreenUppermostRow
); 
11117     const int visibleBottomRow 
= wxMin(bottomRow
, onScreenBottommostRow
); 
11118     const int visibleLeftCol 
= wxMax(leftCol
, onScreenLeftmostCol
); 
11119     const int visibleRightCol 
= wxMin(rightCol
, onScreenRightmostCol
); 
11121     for ( int j 
= visibleTopRow
; j 
<= visibleBottomRow
; j
++ ) 
11123         for ( int i 
= visibleLeftCol
; i 
<= visibleRightCol
; i
++ ) 
11125             if ( (j 
== visibleTopRow
) || (j 
== visibleBottomRow
) || 
11126                     (i 
== visibleLeftCol
) || (i 
== visibleRightCol
) ) 
11128                 tempCellRect 
= CellToRect( j
, i 
); 
11130                 if (tempCellRect
.x 
< left
) 
11131                     left 
= tempCellRect
.x
; 
11132                 if (tempCellRect
.y 
< top
) 
11133                     top 
= tempCellRect
.y
; 
11134                 if (tempCellRect
.x 
+ tempCellRect
.width 
> right
) 
11135                     right 
= tempCellRect
.x 
+ tempCellRect
.width
; 
11136                 if (tempCellRect
.y 
+ tempCellRect
.height 
> bottom
) 
11137                     bottom 
= tempCellRect
.y 
+ tempCellRect
.height
; 
11141                 i 
= visibleRightCol
; // jump over inner cells. 
11146     // Convert to scrolled coords 
11147     CalcScrolledPosition( left
, top
, &left
, &top 
); 
11148     CalcScrolledPosition( right
, bottom
, &right
, &bottom 
); 
11150     if (right 
< 0 || bottom 
< 0 || left 
> cw 
|| top 
> ch
) 
11151         return wxRect(0,0,0,0); 
11153     resultRect
.SetLeft( wxMax(0, left
) ); 
11154     resultRect
.SetTop( wxMax(0, top
) ); 
11155     resultRect
.SetRight( wxMin(cw
, right
) ); 
11156     resultRect
.SetBottom( wxMin(ch
, bottom
) ); 
11161 // ---------------------------------------------------------------------------- 
11163 // ---------------------------------------------------------------------------- 
11165 #if wxUSE_DRAG_AND_DROP 
11167 // this allow setting drop target directly on wxGrid 
11168 void wxGrid::SetDropTarget(wxDropTarget 
*dropTarget
) 
11170     GetGridWindow()->SetDropTarget(dropTarget
); 
11173 #endif // wxUSE_DRAG_AND_DROP 
11175 // ---------------------------------------------------------------------------- 
11176 // grid event classes 
11177 // ---------------------------------------------------------------------------- 
11179 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent
, wxNotifyEvent 
) 
11181 wxGridEvent::wxGridEvent( int id
, wxEventType type
, wxObject
* obj
, 
11182                           int row
, int col
, int x
, int y
, bool sel
, 
11183                           bool control
, bool shift
, bool alt
, bool meta 
) 
11184         : wxNotifyEvent( type
, id 
) 
11191     m_control 
= control
; 
11196     SetEventObject(obj
); 
11200 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent
, wxNotifyEvent 
) 
11202 wxGridSizeEvent::wxGridSizeEvent( int id
, wxEventType type
, wxObject
* obj
, 
11203                                   int rowOrCol
, int x
, int y
, 
11204                                   bool control
, bool shift
, bool alt
, bool meta 
) 
11205         : wxNotifyEvent( type
, id 
) 
11207     m_rowOrCol 
= rowOrCol
; 
11210     m_control 
= control
; 
11215     SetEventObject(obj
); 
11219 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent
, wxNotifyEvent 
) 
11221 wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id
, wxEventType type
, wxObject
* obj
, 
11222                                                const wxGridCellCoords
& topLeft
, 
11223                                                const wxGridCellCoords
& bottomRight
, 
11224                                                bool sel
, bool control
, 
11225                                                bool shift
, bool alt
, bool meta 
) 
11226         : wxNotifyEvent( type
, id 
) 
11228     m_topLeft 
= topLeft
; 
11229     m_bottomRight 
= bottomRight
; 
11231     m_control 
= control
; 
11236     SetEventObject(obj
); 
11240 IMPLEMENT_DYNAMIC_CLASS(wxGridEditorCreatedEvent
, wxCommandEvent
) 
11242 wxGridEditorCreatedEvent::wxGridEditorCreatedEvent(int id
, wxEventType type
, 
11243                                                    wxObject
* obj
, int row
, 
11244                                                    int col
, wxControl
* ctrl
) 
11245     : wxCommandEvent(type
, id
) 
11247     SetEventObject(obj
); 
11253 #endif // wxUSE_GRID