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; 
 462 // ============================================================================ 
 464 // ============================================================================ 
 466 // ---------------------------------------------------------------------------- 
 468 // ---------------------------------------------------------------------------- 
 470 wxGridCellEditor::wxGridCellEditor() 
 476 wxGridCellEditor::~wxGridCellEditor() 
 481 void wxGridCellEditor::Create(wxWindow
* WXUNUSED(parent
), 
 482                               wxWindowID 
WXUNUSED(id
), 
 483                               wxEvtHandler
* evtHandler
) 
 486         m_control
->PushEventHandler(evtHandler
); 
 489 void wxGridCellEditor::PaintBackground(const wxRect
& rectCell
, 
 490                                        wxGridCellAttr 
*attr
) 
 492     // erase the background because we might not fill the cell 
 493     wxClientDC 
dc(m_control
->GetParent()); 
 494     wxGridWindow
* gridWindow 
= wxDynamicCast(m_control
->GetParent(), wxGridWindow
); 
 496         gridWindow
->GetOwner()->PrepareDC(dc
); 
 498     dc
.SetPen(*wxTRANSPARENT_PEN
); 
 499     dc
.SetBrush(wxBrush(attr
->GetBackgroundColour(), wxBRUSHSTYLE_SOLID
)); 
 500     dc
.DrawRectangle(rectCell
); 
 502     // redraw the control we just painted over 
 503     m_control
->Refresh(); 
 506 void wxGridCellEditor::Destroy() 
 510         m_control
->PopEventHandler( true /* delete it*/ ); 
 512         m_control
->Destroy(); 
 517 void wxGridCellEditor::Show(bool show
, wxGridCellAttr 
*attr
) 
 519     wxASSERT_MSG(m_control
, wxT("The wxGridCellEditor must be created first!")); 
 521     m_control
->Show(show
); 
 525         // set the colours/fonts if we have any 
 528             m_colFgOld 
= m_control
->GetForegroundColour(); 
 529             m_control
->SetForegroundColour(attr
->GetTextColour()); 
 531             m_colBgOld 
= m_control
->GetBackgroundColour(); 
 532             m_control
->SetBackgroundColour(attr
->GetBackgroundColour()); 
 534 // Workaround for GTK+1 font setting problem on some platforms 
 535 #if !defined(__WXGTK__) || defined(__WXGTK20__) 
 536             m_fontOld 
= m_control
->GetFont(); 
 537             m_control
->SetFont(attr
->GetFont()); 
 540             // can't do anything more in the base class version, the other 
 541             // attributes may only be used by the derived classes 
 546         // restore the standard colours fonts 
 547         if ( m_colFgOld
.Ok() ) 
 549             m_control
->SetForegroundColour(m_colFgOld
); 
 550             m_colFgOld 
= wxNullColour
; 
 553         if ( m_colBgOld
.Ok() ) 
 555             m_control
->SetBackgroundColour(m_colBgOld
); 
 556             m_colBgOld 
= wxNullColour
; 
 559 // Workaround for GTK+1 font setting problem on some platforms 
 560 #if !defined(__WXGTK__) || defined(__WXGTK20__) 
 561         if ( m_fontOld
.Ok() ) 
 563             m_control
->SetFont(m_fontOld
); 
 564             m_fontOld 
= wxNullFont
; 
 570 void wxGridCellEditor::SetSize(const wxRect
& rect
) 
 572     wxASSERT_MSG(m_control
, wxT("The wxGridCellEditor must be created first!")); 
 574     m_control
->SetSize(rect
, wxSIZE_ALLOW_MINUS_ONE
); 
 577 void wxGridCellEditor::HandleReturn(wxKeyEvent
& event
) 
 582 bool wxGridCellEditor::IsAcceptedKey(wxKeyEvent
& event
) 
 584     bool ctrl 
= event
.ControlDown(); 
 585     bool alt  
= event
.AltDown(); 
 588     // On the Mac the Alt key is more like shift and is used for entry of 
 589     // valid characters, so check for Ctrl and Meta instead. 
 590     alt 
= event
.MetaDown(); 
 593     // Assume it's not a valid char if ctrl or alt is down, but if both are 
 594     // down then it may be because of an AltGr key combination, so let them 
 595     // through in that case. 
 596     if ((ctrl 
|| alt
) && !(ctrl 
&& alt
)) 
 603     // If it's a F-Key or other special key then it shouldn't start the 
 605     if (event
.GetKeyCode() >= WXK_START
) 
 609     // if the unicode key code is not really a unicode character (it may 
 610     // be a function key or etc., the platforms appear to always give us a 
 611     // small value in this case) then fallback to the ASCII key code but 
 612     // don't do anything for function keys or etc. 
 613     key 
= event
.GetUnicodeKey(); 
 616         key 
= event
.GetKeyCode(); 
 617         keyOk 
= (key 
<= 127); 
 620     key 
= event
.GetKeyCode(); 
 621     keyOk 
= (key 
<= 255); 
 627 void wxGridCellEditor::StartingKey(wxKeyEvent
& event
) 
 632 void wxGridCellEditor::StartingClick() 
 638 // ---------------------------------------------------------------------------- 
 639 // wxGridCellTextEditor 
 640 // ---------------------------------------------------------------------------- 
 642 wxGridCellTextEditor::wxGridCellTextEditor() 
 647 void wxGridCellTextEditor::Create(wxWindow
* parent
, 
 649                                   wxEvtHandler
* evtHandler
) 
 651     DoCreate(parent
, id
, evtHandler
); 
 654 void wxGridCellTextEditor::DoCreate(wxWindow
* parent
, 
 656                                     wxEvtHandler
* evtHandler
, 
 659     style 
|= wxTE_PROCESS_ENTER 
| wxTE_PROCESS_TAB 
| wxNO_BORDER
; 
 661     m_control 
= new wxTextCtrl(parent
, id
, wxEmptyString
, 
 662                                wxDefaultPosition
, wxDefaultSize
, 
 665     // set max length allowed in the textctrl, if the parameter was set 
 666     if ( m_maxChars 
!= 0 ) 
 668         Text()->SetMaxLength(m_maxChars
); 
 671     wxGridCellEditor::Create(parent
, id
, evtHandler
); 
 674 void wxGridCellTextEditor::PaintBackground(const wxRect
& WXUNUSED(rectCell
), 
 675                                            wxGridCellAttr 
* WXUNUSED(attr
)) 
 677     // as we fill the entire client area, 
 678     // don't do anything here to minimize flicker 
 681 void wxGridCellTextEditor::SetSize(const wxRect
& rectOrig
) 
 683     wxRect 
rect(rectOrig
); 
 685     // Make the edit control large enough to allow for internal margins 
 687     // TODO: remove this if the text ctrl sizing is improved esp. for unix 
 689 #if defined(__WXGTK__) 
 697 #elif defined(__WXMSW__) 
 711     int extra_x 
= ( rect
.x 
> 2 ) ? 2 : 1; 
 712     int extra_y 
= ( rect
.y 
> 2 ) ? 2 : 1; 
 714     #if defined(__WXMOTIF__) 
 719     rect
.SetLeft( wxMax(0, rect
.x 
- extra_x
) ); 
 720     rect
.SetTop( wxMax(0, rect
.y 
- extra_y
) ); 
 721     rect
.SetRight( rect
.GetRight() + 2 * extra_x 
); 
 722     rect
.SetBottom( rect
.GetBottom() + 2 * extra_y 
); 
 725     wxGridCellEditor::SetSize(rect
); 
 728 void wxGridCellTextEditor::BeginEdit(int row
, int col
, wxGrid
* grid
) 
 730     wxASSERT_MSG(m_control
, wxT("The wxGridCellEditor must be created first!")); 
 732     m_startValue 
= grid
->GetTable()->GetValue(row
, col
); 
 734     DoBeginEdit(m_startValue
); 
 737 void wxGridCellTextEditor::DoBeginEdit(const wxString
& startValue
) 
 739     Text()->SetValue(startValue
); 
 740     Text()->SetInsertionPointEnd(); 
 741     Text()->SetSelection(-1, -1); 
 745 bool wxGridCellTextEditor::EndEdit(int row
, int col
, wxGrid
* grid
) 
 747     wxASSERT_MSG(m_control
, wxT("The wxGridCellEditor must be created first!")); 
 749     bool changed 
= false; 
 750     wxString value 
= Text()->GetValue(); 
 751     if (value 
!= m_startValue
) 
 755         grid
->GetTable()->SetValue(row
, col
, value
); 
 757     m_startValue 
= wxEmptyString
; 
 759     // No point in setting the text of the hidden control 
 760     //Text()->SetValue(m_startValue); 
 765 void wxGridCellTextEditor::Reset() 
 767     wxASSERT_MSG(m_control
, wxT("The wxGridCellEditor must be created first!")); 
 769     DoReset(m_startValue
); 
 772 void wxGridCellTextEditor::DoReset(const wxString
& startValue
) 
 774     Text()->SetValue(startValue
); 
 775     Text()->SetInsertionPointEnd(); 
 778 bool wxGridCellTextEditor::IsAcceptedKey(wxKeyEvent
& event
) 
 780     return wxGridCellEditor::IsAcceptedKey(event
); 
 783 void wxGridCellTextEditor::StartingKey(wxKeyEvent
& event
) 
 785     // Since this is now happening in the EVT_CHAR event EmulateKeyPress is no 
 786     // longer an appropriate way to get the character into the text control. 
 787     // Do it ourselves instead.  We know that if we get this far that we have 
 788     // a valid character, so not a whole lot of testing needs to be done. 
 790     wxTextCtrl
* tc 
= Text(); 
 795     ch 
= event
.GetUnicodeKey(); 
 797         ch 
= (wxChar
)event
.GetKeyCode(); 
 799     ch 
= (wxChar
)event
.GetKeyCode(); 
 805             // delete the character at the cursor 
 806             pos 
= tc
->GetInsertionPoint(); 
 807             if (pos 
< tc
->GetLastPosition()) 
 808                 tc
->Remove(pos
, pos 
+ 1); 
 812             // delete the character before the cursor 
 813             pos 
= tc
->GetInsertionPoint(); 
 815                 tc
->Remove(pos 
- 1, pos
); 
 824 void wxGridCellTextEditor::HandleReturn( wxKeyEvent
& 
 825                                          WXUNUSED_GTK(WXUNUSED_MOTIF(event
)) ) 
 827 #if defined(__WXMOTIF__) || defined(__WXGTK__) 
 828     // wxMotif needs a little extra help... 
 829     size_t pos 
= (size_t)( Text()->GetInsertionPoint() ); 
 830     wxString 
s( Text()->GetValue() ); 
 831     s 
= s
.Left(pos
) + wxT("\n") + s
.Mid(pos
); 
 833     Text()->SetInsertionPoint( pos 
); 
 835     // the other ports can handle a Return key press 
 841 void wxGridCellTextEditor::SetParameters(const wxString
& params
) 
 851         if ( params
.ToLong(&tmp
) ) 
 853             m_maxChars 
= (size_t)tmp
; 
 857             wxLogDebug( _T("Invalid wxGridCellTextEditor parameter string '%s' ignored"), params
.c_str() ); 
 862 // return the value in the text control 
 863 wxString 
wxGridCellTextEditor::GetValue() const 
 865     return Text()->GetValue(); 
 868 // ---------------------------------------------------------------------------- 
 869 // wxGridCellNumberEditor 
 870 // ---------------------------------------------------------------------------- 
 872 wxGridCellNumberEditor::wxGridCellNumberEditor(int min
, int max
) 
 878 void wxGridCellNumberEditor::Create(wxWindow
* parent
, 
 880                                     wxEvtHandler
* evtHandler
) 
 885         // create a spin ctrl 
 886         m_control 
= new wxSpinCtrl(parent
, wxID_ANY
, wxEmptyString
, 
 887                                    wxDefaultPosition
, wxDefaultSize
, 
 891         wxGridCellEditor::Create(parent
, id
, evtHandler
); 
 896         // just a text control 
 897         wxGridCellTextEditor::Create(parent
, id
, evtHandler
); 
 900         Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC
)); 
 905 void wxGridCellNumberEditor::BeginEdit(int row
, int col
, wxGrid
* grid
) 
 907     // first get the value 
 908     wxGridTableBase 
*table 
= grid
->GetTable(); 
 909     if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) ) 
 911         m_valueOld 
= table
->GetValueAsLong(row
, col
); 
 916         wxString sValue 
= table
->GetValue(row
, col
); 
 917         if (! sValue
.ToLong(&m_valueOld
) && ! sValue
.empty()) 
 919             wxFAIL_MSG( _T("this cell doesn't have numeric value") ); 
 927         Spin()->SetValue((int)m_valueOld
); 
 933         DoBeginEdit(GetString()); 
 937 bool wxGridCellNumberEditor::EndEdit(int row
, int col
, 
 946         value 
= Spin()->GetValue(); 
 947         if ( value 
== m_valueOld 
) 
 950         text
.Printf(wxT("%ld"), value
); 
 952     else // using unconstrained input 
 953 #endif // wxUSE_SPINCTRL 
 955         const wxString 
textOld(grid
->GetCellValue(row
, col
)); 
 956         text 
= Text()->GetValue(); 
 959             if ( textOld
.empty() ) 
 962         else // non-empty text now (maybe 0) 
 964             if ( !text
.ToLong(&value
) ) 
 967             // if value == m_valueOld == 0 but old text was "" and new one is 
 968             // "0" something still did change 
 969             if ( value 
== m_valueOld 
&& (value 
|| !textOld
.empty()) ) 
 974     wxGridTableBase 
* const table 
= grid
->GetTable(); 
 975     if ( table
->CanSetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) ) 
 976         table
->SetValueAsLong(row
, col
, value
); 
 978         table
->SetValue(row
, col
, text
); 
 983 void wxGridCellNumberEditor::Reset() 
 988         Spin()->SetValue((int)m_valueOld
); 
 993         DoReset(GetString()); 
 997 bool wxGridCellNumberEditor::IsAcceptedKey(wxKeyEvent
& event
) 
 999     if ( wxGridCellEditor::IsAcceptedKey(event
) ) 
1001         int keycode 
= event
.GetKeyCode(); 
1002         if ( (keycode 
< 128) && 
1003              (wxIsdigit(keycode
) || keycode 
== '+' || keycode 
== '-')) 
1012 void wxGridCellNumberEditor::StartingKey(wxKeyEvent
& event
) 
1014     int keycode 
= event
.GetKeyCode(); 
1017         if ( wxIsdigit(keycode
) || keycode 
== '+' || keycode 
== '-') 
1019             wxGridCellTextEditor::StartingKey(event
); 
1021             // skip Skip() below 
1028         if ( wxIsdigit(keycode
) ) 
1030             wxSpinCtrl
* spin 
= (wxSpinCtrl
*)m_control
; 
1031             spin
->SetValue(keycode 
- '0'); 
1032             spin
->SetSelection(1,1); 
1041 void wxGridCellNumberEditor::SetParameters(const wxString
& params
) 
1052         if ( params
.BeforeFirst(_T(',')).ToLong(&tmp
) ) 
1056             if ( params
.AfterFirst(_T(',')).ToLong(&tmp
) ) 
1060                 // skip the error message below 
1065         wxLogDebug(_T("Invalid wxGridCellNumberEditor parameter string '%s' ignored"), params
.c_str()); 
1069 // return the value in the spin control if it is there (the text control otherwise) 
1070 wxString 
wxGridCellNumberEditor::GetValue() const 
1077         long value 
= Spin()->GetValue(); 
1078         s
.Printf(wxT("%ld"), value
); 
1083         s 
= Text()->GetValue(); 
1089 // ---------------------------------------------------------------------------- 
1090 // wxGridCellFloatEditor 
1091 // ---------------------------------------------------------------------------- 
1093 wxGridCellFloatEditor::wxGridCellFloatEditor(int width
, int precision
) 
1096     m_precision 
= precision
; 
1099 void wxGridCellFloatEditor::Create(wxWindow
* parent
, 
1101                                    wxEvtHandler
* evtHandler
) 
1103     wxGridCellTextEditor::Create(parent
, id
, evtHandler
); 
1105 #if wxUSE_VALIDATORS 
1106     Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC
)); 
1110 void wxGridCellFloatEditor::BeginEdit(int row
, int col
, wxGrid
* grid
) 
1112     // first get the value 
1113     wxGridTableBase 
* const table 
= grid
->GetTable(); 
1114     if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) ) 
1116         m_valueOld 
= table
->GetValueAsDouble(row
, col
); 
1122         const wxString value 
= table
->GetValue(row
, col
); 
1123         if ( !value
.empty() ) 
1125             if ( !value
.ToDouble(&m_valueOld
) ) 
1127                 wxFAIL_MSG( _T("this cell doesn't have float value") ); 
1133     DoBeginEdit(GetString()); 
1136 bool wxGridCellFloatEditor::EndEdit(int row
, int col
, wxGrid
* grid
) 
1138     const wxString 
text(Text()->GetValue()), 
1139                    textOld(grid
->GetCellValue(row
, col
)); 
1142     if ( !text
.empty() ) 
1144         if ( !text
.ToDouble(&value
) ) 
1147     else // new value is empty string 
1149         if ( textOld
.empty() ) 
1150             return false;           // nothing changed 
1155     // the test for empty strings ensures that we don't skip the value setting 
1156     // when "" is replaced by "0" or vice versa as "" numeric value is also 0. 
1157     if ( wxIsSameDouble(value
, m_valueOld
) && !text
.empty() && !textOld
.empty() ) 
1158         return false;           // nothing changed 
1160     wxGridTableBase 
* const table 
= grid
->GetTable(); 
1162     if ( table
->CanSetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) ) 
1163         table
->SetValueAsDouble(row
, col
, value
); 
1165         table
->SetValue(row
, col
, text
); 
1170 void wxGridCellFloatEditor::Reset() 
1172     DoReset(GetString()); 
1175 void wxGridCellFloatEditor::StartingKey(wxKeyEvent
& event
) 
1177     int keycode 
= event
.GetKeyCode(); 
1179     tmpbuf
[0] = (char) keycode
; 
1181     wxString 
strbuf(tmpbuf
, *wxConvCurrent
); 
1184     bool is_decimal_point 
= ( strbuf 
== 
1185        wxLocale::GetInfo(wxLOCALE_DECIMAL_POINT
, wxLOCALE_CAT_NUMBER
) ); 
1187     bool is_decimal_point 
= ( strbuf 
== _T(".") ); 
1190     if ( wxIsdigit(keycode
) || keycode 
== '+' || keycode 
== '-' 
1191          || is_decimal_point 
) 
1193         wxGridCellTextEditor::StartingKey(event
); 
1195         // skip Skip() below 
1202 void wxGridCellFloatEditor::SetParameters(const wxString
& params
) 
1213         if ( params
.BeforeFirst(_T(',')).ToLong(&tmp
) ) 
1217             if ( params
.AfterFirst(_T(',')).ToLong(&tmp
) ) 
1219                 m_precision 
= (int)tmp
; 
1221                 // skip the error message below 
1226         wxLogDebug(_T("Invalid wxGridCellFloatEditor parameter string '%s' ignored"), params
.c_str()); 
1230 wxString 
wxGridCellFloatEditor::GetString() const 
1233     if ( m_precision 
== -1 && m_width 
!= -1) 
1235         // default precision 
1236         fmt
.Printf(_T("%%%d.f"), m_width
); 
1238     else if ( m_precision 
!= -1 && m_width 
== -1) 
1241         fmt
.Printf(_T("%%.%df"), m_precision
); 
1243     else if ( m_precision 
!= -1 && m_width 
!= -1 ) 
1245         fmt
.Printf(_T("%%%d.%df"), m_width
, m_precision
); 
1249         // default width/precision 
1253     return wxString::Format(fmt
, m_valueOld
); 
1256 bool wxGridCellFloatEditor::IsAcceptedKey(wxKeyEvent
& event
) 
1258     if ( wxGridCellEditor::IsAcceptedKey(event
) ) 
1260         const int keycode 
= event
.GetKeyCode(); 
1261         if ( isascii(keycode
) ) 
1264             tmpbuf
[0] = (char) keycode
; 
1266             wxString 
strbuf(tmpbuf
, *wxConvCurrent
); 
1269             const wxString decimalPoint 
= 
1270                 wxLocale::GetInfo(wxLOCALE_DECIMAL_POINT
, wxLOCALE_CAT_NUMBER
); 
1272             const wxString 
decimalPoint(_T('.')); 
1275             // accept digits, 'e' as in '1e+6', also '-', '+', and '.' 
1276             if ( wxIsdigit(keycode
) || 
1277                     tolower(keycode
) == 'e' || 
1278                         keycode 
== decimalPoint 
|| 
1290 #endif // wxUSE_TEXTCTRL 
1294 // ---------------------------------------------------------------------------- 
1295 // wxGridCellBoolEditor 
1296 // ---------------------------------------------------------------------------- 
1298 // the default values for GetValue() 
1299 wxString 
wxGridCellBoolEditor::ms_stringValues
[2] = { _T(""), _T("1") }; 
1301 void wxGridCellBoolEditor::Create(wxWindow
* parent
, 
1303                                   wxEvtHandler
* evtHandler
) 
1305     m_control 
= new wxCheckBox(parent
, id
, wxEmptyString
, 
1306                                wxDefaultPosition
, wxDefaultSize
, 
1309     wxGridCellEditor::Create(parent
, id
, evtHandler
); 
1312 void wxGridCellBoolEditor::SetSize(const wxRect
& r
) 
1314     bool resize 
= false; 
1315     wxSize size 
= m_control
->GetSize(); 
1316     wxCoord minSize 
= wxMin(r
.width
, r
.height
); 
1318     // check if the checkbox is not too big/small for this cell 
1319     wxSize sizeBest 
= m_control
->GetBestSize(); 
1320     if ( !(size 
== sizeBest
) ) 
1322         // reset to default size if it had been made smaller 
1328     if ( size
.x 
>= minSize 
|| size
.y 
>= minSize 
) 
1330         // leave 1 pixel margin 
1331         size
.x 
= size
.y 
= minSize 
- 2; 
1338         m_control
->SetSize(size
); 
1341     // position it in the centre of the rectangle (TODO: support alignment?) 
1343 #if defined(__WXGTK__) || defined (__WXMOTIF__) 
1344     // the checkbox without label still has some space to the right in wxGTK, 
1345     // so shift it to the right 
1347 #elif defined(__WXMSW__) 
1348     // here too, but in other way 
1353     int hAlign 
= wxALIGN_CENTRE
; 
1354     int vAlign 
= wxALIGN_CENTRE
; 
1356         GetCellAttr()->GetAlignment(& hAlign
, & vAlign
); 
1359     if (hAlign 
== wxALIGN_LEFT
) 
1367         y 
= r
.y 
+ r
.height 
/ 2 - size
.y 
/ 2; 
1369     else if (hAlign 
== wxALIGN_RIGHT
) 
1371         x 
= r
.x 
+ r
.width 
- size
.x 
- 2; 
1372         y 
= r
.y 
+ r
.height 
/ 2 - size
.y 
/ 2; 
1374     else if (hAlign 
== wxALIGN_CENTRE
) 
1376         x 
= r
.x 
+ r
.width 
/ 2 - size
.x 
/ 2; 
1377         y 
= r
.y 
+ r
.height 
/ 2 - size
.y 
/ 2; 
1380     m_control
->Move(x
, y
); 
1383 void wxGridCellBoolEditor::Show(bool show
, wxGridCellAttr 
*attr
) 
1385     m_control
->Show(show
); 
1389         wxColour colBg 
= attr 
? attr
->GetBackgroundColour() : *wxLIGHT_GREY
; 
1390         CBox()->SetBackgroundColour(colBg
); 
1394 void wxGridCellBoolEditor::BeginEdit(int row
, int col
, wxGrid
* grid
) 
1396     wxASSERT_MSG(m_control
, 
1397                  wxT("The wxGridCellEditor must be created first!")); 
1399     if (grid
->GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
)) 
1401         m_startValue 
= grid
->GetTable()->GetValueAsBool(row
, col
); 
1405         wxString 
cellval( grid
->GetTable()->GetValue(row
, col
) ); 
1407         if ( cellval 
== ms_stringValues
[false] ) 
1408             m_startValue 
= false; 
1409         else if ( cellval 
== ms_stringValues
[true] ) 
1410             m_startValue 
= true; 
1413             // do not try to be smart here and convert it to true or false 
1414             // because we'll still overwrite it with something different and 
1415             // this risks to be very surprising for the user code, let them 
1417             wxFAIL_MSG( _T("invalid value for a cell with bool editor!") ); 
1421     CBox()->SetValue(m_startValue
); 
1425 bool wxGridCellBoolEditor::EndEdit(int row
, int col
, 
1428     wxASSERT_MSG(m_control
, 
1429                  wxT("The wxGridCellEditor must be created first!")); 
1431     bool changed 
= false; 
1432     bool value 
= CBox()->GetValue(); 
1433     if ( value 
!= m_startValue 
) 
1438         wxGridTableBase 
* const table 
= grid
->GetTable(); 
1439         if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
) ) 
1440             table
->SetValueAsBool(row
, col
, value
); 
1442             table
->SetValue(row
, col
, GetValue()); 
1448 void wxGridCellBoolEditor::Reset() 
1450     wxASSERT_MSG(m_control
, 
1451                  wxT("The wxGridCellEditor must be created first!")); 
1453     CBox()->SetValue(m_startValue
); 
1456 void wxGridCellBoolEditor::StartingClick() 
1458     CBox()->SetValue(!CBox()->GetValue()); 
1461 bool wxGridCellBoolEditor::IsAcceptedKey(wxKeyEvent
& event
) 
1463     if ( wxGridCellEditor::IsAcceptedKey(event
) ) 
1465         int keycode 
= event
.GetKeyCode(); 
1478 void wxGridCellBoolEditor::StartingKey(wxKeyEvent
& event
) 
1480     int keycode 
= event
.GetKeyCode(); 
1484             CBox()->SetValue(!CBox()->GetValue()); 
1488             CBox()->SetValue(true); 
1492             CBox()->SetValue(false); 
1497 wxString 
wxGridCellBoolEditor::GetValue() const 
1499   return ms_stringValues
[CBox()->GetValue()]; 
1503 wxGridCellBoolEditor::UseStringValues(const wxString
& valueTrue
, 
1504                                       const wxString
& valueFalse
) 
1506     ms_stringValues
[false] = valueFalse
; 
1507     ms_stringValues
[true] = valueTrue
; 
1511 wxGridCellBoolEditor::IsTrueValue(const wxString
& value
) 
1513     return value 
== ms_stringValues
[true]; 
1516 #endif // wxUSE_CHECKBOX 
1520 // ---------------------------------------------------------------------------- 
1521 // wxGridCellChoiceEditor 
1522 // ---------------------------------------------------------------------------- 
1524 wxGridCellChoiceEditor::wxGridCellChoiceEditor(const wxArrayString
& choices
, 
1526     : m_choices(choices
), 
1527       m_allowOthers(allowOthers
) { } 
1529 wxGridCellChoiceEditor::wxGridCellChoiceEditor(size_t count
, 
1530                                                const wxString choices
[], 
1532                       : m_allowOthers(allowOthers
) 
1536         m_choices
.Alloc(count
); 
1537         for ( size_t n 
= 0; n 
< count
; n
++ ) 
1539             m_choices
.Add(choices
[n
]); 
1544 wxGridCellEditor 
*wxGridCellChoiceEditor::Clone() const 
1546     wxGridCellChoiceEditor 
*editor 
= new wxGridCellChoiceEditor
; 
1547     editor
->m_allowOthers 
= m_allowOthers
; 
1548     editor
->m_choices 
= m_choices
; 
1553 void wxGridCellChoiceEditor::Create(wxWindow
* parent
, 
1555                                     wxEvtHandler
* evtHandler
) 
1557     int style 
= wxTE_PROCESS_ENTER 
| 
1561     if ( !m_allowOthers 
) 
1562         style 
|= wxCB_READONLY
; 
1563     m_control 
= new wxComboBox(parent
, id
, wxEmptyString
, 
1564                                wxDefaultPosition
, wxDefaultSize
, 
1568     wxGridCellEditor::Create(parent
, id
, evtHandler
); 
1571 void wxGridCellChoiceEditor::PaintBackground(const wxRect
& rectCell
, 
1572                                              wxGridCellAttr 
* attr
) 
1574     // as we fill the entire client area, don't do anything here to minimize 
1577     // TODO: It doesn't actually fill the client area since the height of a 
1578     // combo always defaults to the standard.  Until someone has time to 
1579     // figure out the right rectangle to paint, just do it the normal way. 
1580     wxGridCellEditor::PaintBackground(rectCell
, attr
); 
1583 void wxGridCellChoiceEditor::BeginEdit(int row
, int col
, wxGrid
* grid
) 
1585     wxASSERT_MSG(m_control
, 
1586                  wxT("The wxGridCellEditor must be created first!")); 
1588     wxGridCellEditorEvtHandler
* evtHandler 
= NULL
; 
1590         evtHandler 
= wxDynamicCast(m_control
->GetEventHandler(), wxGridCellEditorEvtHandler
); 
1592     // Don't immediately end if we get a kill focus event within BeginEdit 
1594         evtHandler
->SetInSetFocus(true); 
1596     m_startValue 
= grid
->GetTable()->GetValue(row
, col
); 
1598     Reset(); // this updates combo box to correspond to m_startValue 
1600     Combo()->SetFocus(); 
1604         // When dropping down the menu, a kill focus event 
1605         // happens after this point, so we can't reset the flag yet. 
1606 #if !defined(__WXGTK20__) 
1607         evtHandler
->SetInSetFocus(false); 
1612 bool wxGridCellChoiceEditor::EndEdit(int row
, int col
, 
1615     wxString value 
= Combo()->GetValue(); 
1616     if ( value 
== m_startValue 
) 
1619     grid
->GetTable()->SetValue(row
, col
, value
); 
1624 void wxGridCellChoiceEditor::Reset() 
1628         Combo()->SetValue(m_startValue
); 
1629         Combo()->SetInsertionPointEnd(); 
1631     else // the combobox is read-only 
1633         // find the right position, or default to the first if not found 
1634         int pos 
= Combo()->FindString(m_startValue
); 
1635         if (pos 
== wxNOT_FOUND
) 
1637         Combo()->SetSelection(pos
); 
1641 void wxGridCellChoiceEditor::SetParameters(const wxString
& params
) 
1651     wxStringTokenizer 
tk(params
, _T(',')); 
1652     while ( tk
.HasMoreTokens() ) 
1654         m_choices
.Add(tk
.GetNextToken()); 
1658 // return the value in the text control 
1659 wxString 
wxGridCellChoiceEditor::GetValue() const 
1661   return Combo()->GetValue(); 
1664 #endif // wxUSE_COMBOBOX 
1666 // ---------------------------------------------------------------------------- 
1667 // wxGridCellEditorEvtHandler 
1668 // ---------------------------------------------------------------------------- 
1670 void wxGridCellEditorEvtHandler::OnKillFocus(wxFocusEvent
& event
) 
1672     // Don't disable the cell if we're just starting to edit it 
1677     m_grid
->DisableCellEditControl(); 
1682 void wxGridCellEditorEvtHandler::OnKeyDown(wxKeyEvent
& event
) 
1684     switch ( event
.GetKeyCode() ) 
1688             m_grid
->DisableCellEditControl(); 
1692             m_grid
->GetEventHandler()->ProcessEvent( event 
); 
1696         case WXK_NUMPAD_ENTER
: 
1697             if (!m_grid
->GetEventHandler()->ProcessEvent(event
)) 
1698                 m_editor
->HandleReturn(event
); 
1707 void wxGridCellEditorEvtHandler::OnChar(wxKeyEvent
& event
) 
1709     int row 
= m_grid
->GetGridCursorRow(); 
1710     int col 
= m_grid
->GetGridCursorCol(); 
1711     wxRect rect 
= m_grid
->CellToRect( row
, col 
); 
1713     m_grid
->GetGridWindow()->GetClientSize( &cw
, &ch 
); 
1715     // if cell width is smaller than grid client area, cell is wholly visible 
1716     bool wholeCellVisible 
= (rect
.GetWidth() < cw
); 
1718     switch ( event
.GetKeyCode() ) 
1723         case WXK_NUMPAD_ENTER
: 
1728             if ( wholeCellVisible 
) 
1730                 // no special processing needed... 
1735             // do special processing for partly visible cell... 
1737             // get the widths of all cells previous to this one 
1739             for ( int i 
= 0; i 
< col
; i
++ ) 
1741                 colXPos 
+= m_grid
->GetColSize(i
); 
1744             int xUnit 
= 1, yUnit 
= 1; 
1745             m_grid
->GetScrollPixelsPerUnit(&xUnit
, &yUnit
); 
1748                 m_grid
->Scroll(colXPos 
/ xUnit 
- 1, m_grid
->GetScrollPos(wxVERTICAL
)); 
1752                 m_grid
->Scroll(colXPos 
/ xUnit
, m_grid
->GetScrollPos(wxVERTICAL
)); 
1760             if ( wholeCellVisible 
) 
1762                 // no special processing needed... 
1767             // do special processing for partly visible cell... 
1770             wxString value 
= m_grid
->GetCellValue(row
, col
); 
1771             if ( wxEmptyString 
!= value 
) 
1773                 // get width of cell CONTENTS (text) 
1775                 wxFont font 
= m_grid
->GetCellFont(row
, col
); 
1776                 m_grid
->GetTextExtent(value
, &textWidth
, &y
, NULL
, NULL
, &font
); 
1778                 // try to RIGHT align the text by scrolling 
1779                 int client_right 
= m_grid
->GetGridWindow()->GetClientSize().GetWidth(); 
1781                 // (m_grid->GetScrollLineX()*2) is a factor for not scrolling to far, 
1782                 // otherwise the last part of the cell content might be hidden below the scroll bar 
1783                 // FIXME: maybe there is a more suitable correction? 
1784                 textWidth 
-= (client_right 
- (m_grid
->GetScrollLineX() * 2)); 
1785                 if ( textWidth 
< 0 ) 
1791             // get the widths of all cells previous to this one 
1793             for ( int i 
= 0; i 
< col
; i
++ ) 
1795                 colXPos 
+= m_grid
->GetColSize(i
); 
1798             // and add the (modified) text width of the cell contents 
1799             // as we'd like to see the last part of the cell contents 
1800             colXPos 
+= textWidth
; 
1802             int xUnit 
= 1, yUnit 
= 1; 
1803             m_grid
->GetScrollPixelsPerUnit(&xUnit
, &yUnit
); 
1804             m_grid
->Scroll(colXPos 
/ xUnit 
- 1, m_grid
->GetScrollPos(wxVERTICAL
)); 
1815 // ---------------------------------------------------------------------------- 
1816 // wxGridCellWorker is an (almost) empty common base class for 
1817 // wxGridCellRenderer and wxGridCellEditor managing ref counting 
1818 // ---------------------------------------------------------------------------- 
1820 void wxGridCellWorker::SetParameters(const wxString
& WXUNUSED(params
)) 
1825 wxGridCellWorker::~wxGridCellWorker() 
1829 // ============================================================================ 
1831 // ============================================================================ 
1833 // ---------------------------------------------------------------------------- 
1834 // wxGridCellRenderer 
1835 // ---------------------------------------------------------------------------- 
1837 void wxGridCellRenderer::Draw(wxGrid
& grid
, 
1838                               wxGridCellAttr
& attr
, 
1841                               int WXUNUSED(row
), int WXUNUSED(col
), 
1844     dc
.SetBackgroundMode( wxBRUSHSTYLE_SOLID 
); 
1846     // grey out fields if the grid is disabled 
1847     if ( grid
.IsEnabled() ) 
1852             if ( grid
.HasFocus() ) 
1853                 clr 
= grid
.GetSelectionBackground(); 
1855                 clr 
= wxSystemSettings::GetColour(wxSYS_COLOUR_BTNSHADOW
); 
1856             dc
.SetBrush( wxBrush(clr
, wxBRUSHSTYLE_SOLID
) ); 
1860             dc
.SetBrush( wxBrush(attr
.GetBackgroundColour(), wxBRUSHSTYLE_SOLID
) ); 
1865         dc
.SetBrush(wxBrush(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
), wxBRUSHSTYLE_SOLID
)); 
1868     dc
.SetPen( *wxTRANSPARENT_PEN 
); 
1869     dc
.DrawRectangle(rect
); 
1872 // ---------------------------------------------------------------------------- 
1873 // wxGridCellStringRenderer 
1874 // ---------------------------------------------------------------------------- 
1876 void wxGridCellStringRenderer::SetTextColoursAndFont(const wxGrid
& grid
, 
1877                                                      const wxGridCellAttr
& attr
, 
1881     dc
.SetBackgroundMode( wxBRUSHSTYLE_TRANSPARENT 
); 
1883     // TODO some special colours for attr.IsReadOnly() case? 
1885     // different coloured text when the grid is disabled 
1886     if ( grid
.IsEnabled() ) 
1891             if ( grid
.HasFocus() ) 
1892                 clr 
= grid
.GetSelectionBackground(); 
1894                 clr 
= wxSystemSettings::GetColour(wxSYS_COLOUR_BTNSHADOW
); 
1895             dc
.SetTextBackground( clr 
); 
1896             dc
.SetTextForeground( grid
.GetSelectionForeground() ); 
1900             dc
.SetTextBackground( attr
.GetBackgroundColour() ); 
1901             dc
.SetTextForeground( attr
.GetTextColour() ); 
1906         dc
.SetTextBackground(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
)); 
1907         dc
.SetTextForeground(wxSystemSettings::GetColour(wxSYS_COLOUR_GRAYTEXT
)); 
1910     dc
.SetFont( attr
.GetFont() ); 
1913 wxSize 
wxGridCellStringRenderer::DoGetBestSize(const wxGridCellAttr
& attr
, 
1915                                                const wxString
& text
) 
1917     wxCoord x 
= 0, y 
= 0, max_x 
= 0; 
1918     dc
.SetFont(attr
.GetFont()); 
1919     wxStringTokenizer 
tk(text
, _T('\n')); 
1920     while ( tk
.HasMoreTokens() ) 
1922         dc
.GetTextExtent(tk
.GetNextToken(), &x
, &y
); 
1923         max_x 
= wxMax(max_x
, x
); 
1926     y 
*= 1 + text
.Freq(wxT('\n')); // multiply by the number of lines. 
1928     return wxSize(max_x
, y
); 
1931 wxSize 
wxGridCellStringRenderer::GetBestSize(wxGrid
& grid
, 
1932                                              wxGridCellAttr
& attr
, 
1936     return DoGetBestSize(attr
, dc
, grid
.GetCellValue(row
, col
)); 
1939 void wxGridCellStringRenderer::Draw(wxGrid
& grid
, 
1940                                     wxGridCellAttr
& attr
, 
1942                                     const wxRect
& rectCell
, 
1946     wxRect rect 
= rectCell
; 
1949     // erase only this cells background, overflow cells should have been erased 
1950     wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
); 
1953     attr
.GetAlignment(&hAlign
, &vAlign
); 
1955     int overflowCols 
= 0; 
1957     if (attr
.GetOverflow()) 
1959         int cols 
= grid
.GetNumberCols(); 
1960         int best_width 
= GetBestSize(grid
,attr
,dc
,row
,col
).GetWidth(); 
1961         int cell_rows
, cell_cols
; 
1962         attr
.GetSize( &cell_rows
, &cell_cols 
); // shouldn't get here if <= 0 
1963         if ((best_width 
> rectCell
.width
) && (col 
< cols
) && grid
.GetTable()) 
1965             int i
, c_cols
, c_rows
; 
1966             for (i 
= col
+cell_cols
; i 
< cols
; i
++) 
1968                 bool is_empty 
= true; 
1969                 for (int j
=row
; j 
< row 
+ cell_rows
; j
++) 
1971                     // check w/ anchor cell for multicell block 
1972                     grid
.GetCellSize(j
, i
, &c_rows
, &c_cols
); 
1975                     if (!grid
.GetTable()->IsEmptyCell(j 
+ c_rows
, i
)) 
1984                     rect
.width 
+= grid
.GetColSize(i
); 
1992                 if (rect
.width 
>= best_width
) 
1996             overflowCols 
= i 
- col 
- cell_cols 
+ 1; 
1997             if (overflowCols 
>= cols
) 
1998                 overflowCols 
= cols 
- 1; 
2001         if (overflowCols 
> 0) // redraw overflow cells w/ proper hilight 
2003             hAlign 
= wxALIGN_LEFT
; // if oveflowed then it's left aligned 
2005             clip
.x 
+= rectCell
.width
; 
2006             // draw each overflow cell individually 
2007             int col_end 
= col 
+ cell_cols 
+ overflowCols
; 
2008             if (col_end 
>= grid
.GetNumberCols()) 
2009                 col_end 
= grid
.GetNumberCols() - 1; 
2010             for (int i 
= col 
+ cell_cols
; i 
<= col_end
; i
++) 
2012                 clip
.width 
= grid
.GetColSize(i
) - 1; 
2013                 dc
.DestroyClippingRegion(); 
2014                 dc
.SetClippingRegion(clip
); 
2016                 SetTextColoursAndFont(grid
, attr
, dc
, 
2017                         grid
.IsInSelection(row
,i
)); 
2019                 grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
), 
2020                         rect
, hAlign
, vAlign
); 
2021                 clip
.x 
+= grid
.GetColSize(i
) - 1; 
2027             dc
.DestroyClippingRegion(); 
2031     // now we only have to draw the text 
2032     SetTextColoursAndFont(grid
, attr
, dc
, isSelected
); 
2034     grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
), 
2035                            rect
, hAlign
, vAlign
); 
2038 // ---------------------------------------------------------------------------- 
2039 // wxGridCellNumberRenderer 
2040 // ---------------------------------------------------------------------------- 
2042 wxString 
wxGridCellNumberRenderer::GetString(const wxGrid
& grid
, int row
, int col
) 
2044     wxGridTableBase 
*table 
= grid
.GetTable(); 
2046     if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) ) 
2048         text
.Printf(_T("%ld"), table
->GetValueAsLong(row
, col
)); 
2052         text 
= table
->GetValue(row
, col
); 
2058 void wxGridCellNumberRenderer::Draw(wxGrid
& grid
, 
2059                                     wxGridCellAttr
& attr
, 
2061                                     const wxRect
& rectCell
, 
2065     wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
); 
2067     SetTextColoursAndFont(grid
, attr
, dc
, isSelected
); 
2069     // draw the text right aligned by default 
2071     attr
.GetAlignment(&hAlign
, &vAlign
); 
2072     hAlign 
= wxALIGN_RIGHT
; 
2074     wxRect rect 
= rectCell
; 
2077     grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
); 
2080 wxSize 
wxGridCellNumberRenderer::GetBestSize(wxGrid
& grid
, 
2081                                              wxGridCellAttr
& attr
, 
2085     return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
)); 
2088 // ---------------------------------------------------------------------------- 
2089 // wxGridCellFloatRenderer 
2090 // ---------------------------------------------------------------------------- 
2092 wxGridCellFloatRenderer::wxGridCellFloatRenderer(int width
, int precision
) 
2095     SetPrecision(precision
); 
2098 wxGridCellRenderer 
*wxGridCellFloatRenderer::Clone() const 
2100     wxGridCellFloatRenderer 
*renderer 
= new wxGridCellFloatRenderer
; 
2101     renderer
->m_width 
= m_width
; 
2102     renderer
->m_precision 
= m_precision
; 
2103     renderer
->m_format 
= m_format
; 
2108 wxString 
wxGridCellFloatRenderer::GetString(const wxGrid
& grid
, int row
, int col
) 
2110     wxGridTableBase 
*table 
= grid
.GetTable(); 
2115     if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) ) 
2117         val 
= table
->GetValueAsDouble(row
, col
); 
2122         text 
= table
->GetValue(row
, col
); 
2123         hasDouble 
= text
.ToDouble(&val
); 
2130             if ( m_width 
== -1 ) 
2132                 if ( m_precision 
== -1 ) 
2134                     // default width/precision 
2135                     m_format 
= _T("%f"); 
2139                     m_format
.Printf(_T("%%.%df"), m_precision
); 
2142             else if ( m_precision 
== -1 ) 
2144                 // default precision 
2145                 m_format
.Printf(_T("%%%d.f"), m_width
); 
2149                 m_format
.Printf(_T("%%%d.%df"), m_width
, m_precision
); 
2153         text
.Printf(m_format
, val
); 
2156     //else: text already contains the string 
2161 void wxGridCellFloatRenderer::Draw(wxGrid
& grid
, 
2162                                    wxGridCellAttr
& attr
, 
2164                                    const wxRect
& rectCell
, 
2168     wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
); 
2170     SetTextColoursAndFont(grid
, attr
, dc
, isSelected
); 
2172     // draw the text right aligned by default 
2174     attr
.GetAlignment(&hAlign
, &vAlign
); 
2175     hAlign 
= wxALIGN_RIGHT
; 
2177     wxRect rect 
= rectCell
; 
2180     grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
); 
2183 wxSize 
wxGridCellFloatRenderer::GetBestSize(wxGrid
& grid
, 
2184                                             wxGridCellAttr
& attr
, 
2188     return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
)); 
2191 void wxGridCellFloatRenderer::SetParameters(const wxString
& params
) 
2195         // reset to defaults 
2201         wxString tmp 
= params
.BeforeFirst(_T(',')); 
2205             if ( tmp
.ToLong(&width
) ) 
2207                 SetWidth((int)width
); 
2211                 wxLogDebug(_T("Invalid wxGridCellFloatRenderer width parameter string '%s ignored"), params
.c_str()); 
2215         tmp 
= params
.AfterFirst(_T(',')); 
2219             if ( tmp
.ToLong(&precision
) ) 
2221                 SetPrecision((int)precision
); 
2225                 wxLogDebug(_T("Invalid wxGridCellFloatRenderer precision parameter string '%s ignored"), params
.c_str()); 
2231 // ---------------------------------------------------------------------------- 
2232 // wxGridCellBoolRenderer 
2233 // ---------------------------------------------------------------------------- 
2235 wxSize 
wxGridCellBoolRenderer::ms_sizeCheckMark
; 
2237 // FIXME these checkbox size calculations are really ugly... 
2239 // between checkmark and box 
2240 static const wxCoord wxGRID_CHECKMARK_MARGIN 
= 2; 
2242 wxSize 
wxGridCellBoolRenderer::GetBestSize(wxGrid
& grid
, 
2243                                            wxGridCellAttr
& WXUNUSED(attr
), 
2248     // compute it only once (no locks for MT safeness in GUI thread...) 
2249     if ( !ms_sizeCheckMark
.x 
) 
2251         // get checkbox size 
2252         wxCheckBox 
*checkbox 
= new wxCheckBox(&grid
, wxID_ANY
, wxEmptyString
); 
2253         wxSize size 
= checkbox
->GetBestSize(); 
2254         wxCoord checkSize 
= size
.y 
+ 2 * wxGRID_CHECKMARK_MARGIN
; 
2256 #if defined(__WXMOTIF__) 
2257         checkSize 
-= size
.y 
/ 2; 
2262         ms_sizeCheckMark
.x 
= ms_sizeCheckMark
.y 
= checkSize
; 
2265     return ms_sizeCheckMark
; 
2268 void wxGridCellBoolRenderer::Draw(wxGrid
& grid
, 
2269                                   wxGridCellAttr
& attr
, 
2275     wxGridCellRenderer::Draw(grid
, attr
, dc
, rect
, row
, col
, isSelected
); 
2277     // draw a check mark in the centre (ignoring alignment - TODO) 
2278     wxSize size 
= GetBestSize(grid
, attr
, dc
, row
, col
); 
2280     // don't draw outside the cell 
2281     wxCoord minSize 
= wxMin(rect
.width
, rect
.height
); 
2282     if ( size
.x 
>= minSize 
|| size
.y 
>= minSize 
) 
2284         // and even leave (at least) 1 pixel margin 
2285         size
.x 
= size
.y 
= minSize
; 
2288     // draw a border around checkmark 
2290     attr
.GetAlignment(&hAlign
, &vAlign
); 
2293     if (hAlign 
== wxALIGN_CENTRE
) 
2295         rectBorder
.x 
= rect
.x 
+ rect
.width 
/ 2 - size
.x 
/ 2; 
2296         rectBorder
.y 
= rect
.y 
+ rect
.height 
/ 2 - size
.y 
/ 2; 
2297         rectBorder
.width 
= size
.x
; 
2298         rectBorder
.height 
= size
.y
; 
2300     else if (hAlign 
== wxALIGN_LEFT
) 
2302         rectBorder
.x 
= rect
.x 
+ 2; 
2303         rectBorder
.y 
= rect
.y 
+ rect
.height 
/ 2 - size
.y 
/ 2; 
2304         rectBorder
.width 
= size
.x
; 
2305         rectBorder
.height 
= size
.y
; 
2307     else if (hAlign 
== wxALIGN_RIGHT
) 
2309         rectBorder
.x 
= rect
.x 
+ rect
.width 
- size
.x 
- 2; 
2310         rectBorder
.y 
= rect
.y 
+ rect
.height 
/ 2 - size
.y 
/ 2; 
2311         rectBorder
.width 
= size
.x
; 
2312         rectBorder
.height 
= size
.y
; 
2316     if ( grid
.GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
) ) 
2318         value 
= grid
.GetTable()->GetValueAsBool(row
, col
); 
2322         wxString 
cellval( grid
.GetTable()->GetValue(row
, col
) ); 
2323         value 
= wxGridCellBoolEditor::IsTrueValue(cellval
); 
2328         flags 
|= wxCONTROL_CHECKED
; 
2330     wxRendererNative::Get().DrawCheckBox( &grid
, dc
, rectBorder
, flags 
); 
2333 // ---------------------------------------------------------------------------- 
2335 // ---------------------------------------------------------------------------- 
2337 void wxGridCellAttr::Init(wxGridCellAttr 
*attrDefault
) 
2341     m_isReadOnly 
= Unset
; 
2346     m_attrkind 
= wxGridCellAttr::Cell
; 
2348     m_sizeRows 
= m_sizeCols 
= 1; 
2349     m_overflow 
= UnsetOverflow
; 
2351     SetDefAttr(attrDefault
); 
2354 wxGridCellAttr 
*wxGridCellAttr::Clone() const 
2356     wxGridCellAttr 
*attr 
= new wxGridCellAttr(m_defGridAttr
); 
2358     if ( HasTextColour() ) 
2359         attr
->SetTextColour(GetTextColour()); 
2360     if ( HasBackgroundColour() ) 
2361         attr
->SetBackgroundColour(GetBackgroundColour()); 
2363         attr
->SetFont(GetFont()); 
2364     if ( HasAlignment() ) 
2365         attr
->SetAlignment(m_hAlign
, m_vAlign
); 
2367     attr
->SetSize( m_sizeRows
, m_sizeCols 
); 
2371         attr
->SetRenderer(m_renderer
); 
2372         m_renderer
->IncRef(); 
2376         attr
->SetEditor(m_editor
); 
2381         attr
->SetReadOnly(); 
2383     attr
->SetOverflow( m_overflow 
== Overflow 
); 
2384     attr
->SetKind( m_attrkind 
); 
2389 void wxGridCellAttr::MergeWith(wxGridCellAttr 
*mergefrom
) 
2391     if ( !HasTextColour() && mergefrom
->HasTextColour() ) 
2392         SetTextColour(mergefrom
->GetTextColour()); 
2393     if ( !HasBackgroundColour() && mergefrom
->HasBackgroundColour() ) 
2394         SetBackgroundColour(mergefrom
->GetBackgroundColour()); 
2395     if ( !HasFont() && mergefrom
->HasFont() ) 
2396         SetFont(mergefrom
->GetFont()); 
2397     if ( !HasAlignment() && mergefrom
->HasAlignment() ) 
2400         mergefrom
->GetAlignment( &hAlign
, &vAlign
); 
2401         SetAlignment(hAlign
, vAlign
); 
2403     if ( !HasSize() && mergefrom
->HasSize() ) 
2404         mergefrom
->GetSize( &m_sizeRows
, &m_sizeCols 
); 
2406     // Directly access member functions as GetRender/Editor don't just return 
2407     // m_renderer/m_editor 
2409     // Maybe add support for merge of Render and Editor? 
2410     if (!HasRenderer() && mergefrom
->HasRenderer() ) 
2412         m_renderer 
= mergefrom
->m_renderer
; 
2413         m_renderer
->IncRef(); 
2415     if ( !HasEditor() && mergefrom
->HasEditor() ) 
2417         m_editor 
=  mergefrom
->m_editor
; 
2420     if ( !HasReadWriteMode() && mergefrom
->HasReadWriteMode() ) 
2421         SetReadOnly(mergefrom
->IsReadOnly()); 
2423     if (!HasOverflowMode() && mergefrom
->HasOverflowMode() ) 
2424         SetOverflow(mergefrom
->GetOverflow()); 
2426     SetDefAttr(mergefrom
->m_defGridAttr
); 
2429 void wxGridCellAttr::SetSize(int num_rows
, int num_cols
) 
2431     // The size of a cell is normally 1,1 
2433     // If this cell is larger (2,2) then this is the top left cell 
2434     // the other cells that will be covered (lower right cells) must be 
2435     // set to negative or zero values such that 
2436     // row + num_rows of the covered cell points to the larger cell (this cell) 
2437     // same goes for the col + num_cols. 
2439     // Size of 0,0 is NOT valid, neither is <=0 and any positive value 
2441     wxASSERT_MSG( (!((num_rows 
> 0) && (num_cols 
<= 0)) || 
2442                   !((num_rows 
<= 0) && (num_cols 
> 0)) || 
2443                   !((num_rows 
== 0) && (num_cols 
== 0))), 
2444                   wxT("wxGridCellAttr::SetSize only takes two postive values or negative/zero values")); 
2446     m_sizeRows 
= num_rows
; 
2447     m_sizeCols 
= num_cols
; 
2450 const wxColour
& wxGridCellAttr::GetTextColour() const 
2452     if (HasTextColour()) 
2456     else if (m_defGridAttr 
&& m_defGridAttr 
!= this) 
2458         return m_defGridAttr
->GetTextColour(); 
2462         wxFAIL_MSG(wxT("Missing default cell attribute")); 
2463         return wxNullColour
; 
2467 const wxColour
& wxGridCellAttr::GetBackgroundColour() const 
2469     if (HasBackgroundColour()) 
2473     else if (m_defGridAttr 
&& m_defGridAttr 
!= this) 
2475         return m_defGridAttr
->GetBackgroundColour(); 
2479         wxFAIL_MSG(wxT("Missing default cell attribute")); 
2480         return wxNullColour
; 
2484 const wxFont
& wxGridCellAttr::GetFont() const 
2490     else if (m_defGridAttr 
&& m_defGridAttr 
!= this) 
2492         return m_defGridAttr
->GetFont(); 
2496         wxFAIL_MSG(wxT("Missing default cell attribute")); 
2501 void wxGridCellAttr::GetAlignment(int *hAlign
, int *vAlign
) const 
2510     else if (m_defGridAttr 
&& m_defGridAttr 
!= this) 
2512         m_defGridAttr
->GetAlignment(hAlign
, vAlign
); 
2516         wxFAIL_MSG(wxT("Missing default cell attribute")); 
2520 void wxGridCellAttr::GetSize( int *num_rows
, int *num_cols 
) const 
2523         *num_rows 
= m_sizeRows
; 
2525         *num_cols 
= m_sizeCols
; 
2528 // GetRenderer and GetEditor use a slightly different decision path about 
2529 // which attribute to use.  If a non-default attr object has one then it is 
2530 // used, otherwise the default editor or renderer is fetched from the grid and 
2531 // used.  It should be the default for the data type of the cell.  If it is 
2532 // NULL (because the table has a type that the grid does not have in its 
2533 // registry), then the grid's default editor or renderer is used. 
2535 wxGridCellRenderer
* wxGridCellAttr::GetRenderer(const wxGrid
* grid
, int row
, int col
) const 
2537     wxGridCellRenderer 
*renderer 
= NULL
; 
2539     if ( m_renderer 
&& this != m_defGridAttr 
) 
2541         // use the cells renderer if it has one 
2542         renderer 
= m_renderer
; 
2545     else // no non-default cell renderer 
2547         // get default renderer for the data type 
2550             // GetDefaultRendererForCell() will do IncRef() for us 
2551             renderer 
= grid
->GetDefaultRendererForCell(row
, col
); 
2554         if ( renderer 
== NULL 
) 
2556             if ( (m_defGridAttr 
!= NULL
) && (m_defGridAttr 
!= this) ) 
2558                 // if we still don't have one then use the grid default 
2559                 // (no need for IncRef() here neither) 
2560                 renderer 
= m_defGridAttr
->GetRenderer(NULL
, 0, 0); 
2562             else // default grid attr 
2564                 // use m_renderer which we had decided not to use initially 
2565                 renderer 
= m_renderer
; 
2572     // we're supposed to always find something 
2573     wxASSERT_MSG(renderer
, wxT("Missing default cell renderer")); 
2578 // same as above, except for s/renderer/editor/g 
2579 wxGridCellEditor
* wxGridCellAttr::GetEditor(const wxGrid
* grid
, int row
, int col
) const 
2581     wxGridCellEditor 
*editor 
= NULL
; 
2583     if ( m_editor 
&& this != m_defGridAttr 
) 
2585         // use the cells editor if it has one 
2589     else // no non default cell editor 
2591         // get default editor for the data type 
2594             // GetDefaultEditorForCell() will do IncRef() for us 
2595             editor 
= grid
->GetDefaultEditorForCell(row
, col
); 
2598         if ( editor 
== NULL 
) 
2600             if ( (m_defGridAttr 
!= NULL
) && (m_defGridAttr 
!= this) ) 
2602                 // if we still don't have one then use the grid default 
2603                 // (no need for IncRef() here neither) 
2604                 editor 
= m_defGridAttr
->GetEditor(NULL
, 0, 0); 
2606             else // default grid attr 
2608                 // use m_editor which we had decided not to use initially 
2616     // we're supposed to always find something 
2617     wxASSERT_MSG(editor
, wxT("Missing default cell editor")); 
2622 // ---------------------------------------------------------------------------- 
2623 // wxGridCellAttrData 
2624 // ---------------------------------------------------------------------------- 
2626 void wxGridCellAttrData::SetAttr(wxGridCellAttr 
*attr
, int row
, int col
) 
2628     // Note: contrary to wxGridRowOrColAttrData::SetAttr, we must not 
2629     //       touch attribute's reference counting explicitly, since this 
2630     //       is managed by class wxGridCellWithAttr 
2631     int n 
= FindIndex(row
, col
); 
2632     if ( n 
== wxNOT_FOUND 
) 
2636             // add the attribute 
2637             m_attrs
.Add(new wxGridCellWithAttr(row
, col
, attr
)); 
2639         //else: nothing to do 
2641     else // we already have an attribute for this cell 
2645             // change the attribute 
2646             m_attrs
[(size_t)n
].ChangeAttr(attr
); 
2650             // remove this attribute 
2651             m_attrs
.RemoveAt((size_t)n
); 
2656 wxGridCellAttr 
*wxGridCellAttrData::GetAttr(int row
, int col
) const 
2658     wxGridCellAttr 
*attr 
= (wxGridCellAttr 
*)NULL
; 
2660     int n 
= FindIndex(row
, col
); 
2661     if ( n 
!= wxNOT_FOUND 
) 
2663         attr 
= m_attrs
[(size_t)n
].attr
; 
2670 void wxGridCellAttrData::UpdateAttrRows( size_t pos
, int numRows 
) 
2672     size_t count 
= m_attrs
.GetCount(); 
2673     for ( size_t n 
= 0; n 
< count
; n
++ ) 
2675         wxGridCellCoords
& coords 
= m_attrs
[n
].coords
; 
2676         wxCoord row 
= coords
.GetRow(); 
2677         if ((size_t)row 
>= pos
) 
2681                 // If rows inserted, include row counter where necessary 
2682                 coords
.SetRow(row 
+ numRows
); 
2684             else if (numRows 
< 0) 
2686                 // If rows deleted ... 
2687                 if ((size_t)row 
>= pos 
- numRows
) 
2689                     // ...either decrement row counter (if row still exists)... 
2690                     coords
.SetRow(row 
+ numRows
); 
2694                     // ...or remove the attribute 
2695                     m_attrs
.RemoveAt(n
); 
2704 void wxGridCellAttrData::UpdateAttrCols( size_t pos
, int numCols 
) 
2706     size_t count 
= m_attrs
.GetCount(); 
2707     for ( size_t n 
= 0; n 
< count
; n
++ ) 
2709         wxGridCellCoords
& coords 
= m_attrs
[n
].coords
; 
2710         wxCoord col 
= coords
.GetCol(); 
2711         if ( (size_t)col 
>= pos 
) 
2715                 // If rows inserted, include row counter where necessary 
2716                 coords
.SetCol(col 
+ numCols
); 
2718             else if (numCols 
< 0) 
2720                 // If rows deleted ... 
2721                 if ((size_t)col 
>= pos 
- numCols
) 
2723                     // ...either decrement row counter (if row still exists)... 
2724                     coords
.SetCol(col 
+ numCols
); 
2728                     // ...or remove the attribute 
2729                     m_attrs
.RemoveAt(n
); 
2738 int wxGridCellAttrData::FindIndex(int row
, int col
) const 
2740     size_t count 
= m_attrs
.GetCount(); 
2741     for ( size_t n 
= 0; n 
< count
; n
++ ) 
2743         const wxGridCellCoords
& coords 
= m_attrs
[n
].coords
; 
2744         if ( (coords
.GetRow() == row
) && (coords
.GetCol() == col
) ) 
2753 // ---------------------------------------------------------------------------- 
2754 // wxGridRowOrColAttrData 
2755 // ---------------------------------------------------------------------------- 
2757 wxGridRowOrColAttrData::~wxGridRowOrColAttrData() 
2759     size_t count 
= m_attrs
.GetCount(); 
2760     for ( size_t n 
= 0; n 
< count
; n
++ ) 
2762         m_attrs
[n
]->DecRef(); 
2766 wxGridCellAttr 
*wxGridRowOrColAttrData::GetAttr(int rowOrCol
) const 
2768     wxGridCellAttr 
*attr 
= (wxGridCellAttr 
*)NULL
; 
2770     int n 
= m_rowsOrCols
.Index(rowOrCol
); 
2771     if ( n 
!= wxNOT_FOUND 
) 
2773         attr 
= m_attrs
[(size_t)n
]; 
2780 void wxGridRowOrColAttrData::SetAttr(wxGridCellAttr 
*attr
, int rowOrCol
) 
2782     int i 
= m_rowsOrCols
.Index(rowOrCol
); 
2783     if ( i 
== wxNOT_FOUND 
) 
2787             // add the attribute - no need to do anything to reference count 
2788             //                     since we take ownership of the attribute. 
2789             m_rowsOrCols
.Add(rowOrCol
); 
2792         // nothing to remove 
2796         size_t n 
= (size_t)i
; 
2797         if ( m_attrs
[n
] == attr 
) 
2802             // change the attribute, handling reference count manually, 
2803             //                       taking ownership of the new attribute. 
2804             m_attrs
[n
]->DecRef(); 
2809             // remove this attribute, handling reference count manually 
2810             m_attrs
[n
]->DecRef(); 
2811             m_rowsOrCols
.RemoveAt(n
); 
2812             m_attrs
.RemoveAt(n
); 
2817 void wxGridRowOrColAttrData::UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols 
) 
2819     size_t count 
= m_attrs
.GetCount(); 
2820     for ( size_t n 
= 0; n 
< count
; n
++ ) 
2822         int & rowOrCol 
= m_rowsOrCols
[n
]; 
2823         if ( (size_t)rowOrCol 
>= pos 
) 
2825             if ( numRowsOrCols 
> 0 ) 
2827                 // If rows inserted, include row counter where necessary 
2828                 rowOrCol 
+= numRowsOrCols
; 
2830             else if ( numRowsOrCols 
< 0) 
2832                 // If rows deleted, either decrement row counter (if row still exists) 
2833                 if ((size_t)rowOrCol 
>= pos 
- numRowsOrCols
) 
2834                     rowOrCol 
+= numRowsOrCols
; 
2837                     m_rowsOrCols
.RemoveAt(n
); 
2838                     m_attrs
[n
]->DecRef(); 
2839                     m_attrs
.RemoveAt(n
); 
2848 // ---------------------------------------------------------------------------- 
2849 // wxGridCellAttrProvider 
2850 // ---------------------------------------------------------------------------- 
2852 wxGridCellAttrProvider::wxGridCellAttrProvider() 
2854     m_data 
= (wxGridCellAttrProviderData 
*)NULL
; 
2857 wxGridCellAttrProvider::~wxGridCellAttrProvider() 
2862 void wxGridCellAttrProvider::InitData() 
2864     m_data 
= new wxGridCellAttrProviderData
; 
2867 wxGridCellAttr 
*wxGridCellAttrProvider::GetAttr(int row
, int col
, 
2868                                                 wxGridCellAttr::wxAttrKind  kind 
) const 
2870     wxGridCellAttr 
*attr 
= (wxGridCellAttr 
*)NULL
; 
2875             case (wxGridCellAttr::Any
): 
2876                 // Get cached merge attributes. 
2877                 // Currently not used as no cache implemented as not mutable 
2878                 // attr = m_data->m_mergeAttr.GetAttr(row, col); 
2881                     // Basically implement old version. 
2882                     // Also check merge cache, so we don't have to re-merge every time.. 
2883                     wxGridCellAttr 
*attrcell 
= m_data
->m_cellAttrs
.GetAttr(row
, col
); 
2884                     wxGridCellAttr 
*attrrow 
= m_data
->m_rowAttrs
.GetAttr(row
); 
2885                     wxGridCellAttr 
*attrcol 
= m_data
->m_colAttrs
.GetAttr(col
); 
2887                     if ((attrcell 
!= attrrow
) && (attrrow 
!= attrcol
) && (attrcell 
!= attrcol
)) 
2889                         // Two or more are non NULL 
2890                         attr 
= new wxGridCellAttr
; 
2891                         attr
->SetKind(wxGridCellAttr::Merged
); 
2893                         // Order is important.. 
2896                             attr
->MergeWith(attrcell
); 
2901                             attr
->MergeWith(attrcol
); 
2906                             attr
->MergeWith(attrrow
); 
2910                         // store merge attr if cache implemented 
2912                         //m_data->m_mergeAttr.SetAttr(attr, row, col); 
2916                         // one or none is non null return it or null. 
2935             case (wxGridCellAttr::Cell
): 
2936                 attr 
= m_data
->m_cellAttrs
.GetAttr(row
, col
); 
2939             case (wxGridCellAttr::Col
): 
2940                 attr 
= m_data
->m_colAttrs
.GetAttr(col
); 
2943             case (wxGridCellAttr::Row
): 
2944                 attr 
= m_data
->m_rowAttrs
.GetAttr(row
); 
2949                 // (wxGridCellAttr::Default): 
2950                 // (wxGridCellAttr::Merged): 
2958 void wxGridCellAttrProvider::SetAttr(wxGridCellAttr 
*attr
, 
2964     m_data
->m_cellAttrs
.SetAttr(attr
, row
, col
); 
2967 void wxGridCellAttrProvider::SetRowAttr(wxGridCellAttr 
*attr
, int row
) 
2972     m_data
->m_rowAttrs
.SetAttr(attr
, row
); 
2975 void wxGridCellAttrProvider::SetColAttr(wxGridCellAttr 
*attr
, int col
) 
2980     m_data
->m_colAttrs
.SetAttr(attr
, col
); 
2983 void wxGridCellAttrProvider::UpdateAttrRows( size_t pos
, int numRows 
) 
2987         m_data
->m_cellAttrs
.UpdateAttrRows( pos
, numRows 
); 
2989         m_data
->m_rowAttrs
.UpdateAttrRowsOrCols( pos
, numRows 
); 
2993 void wxGridCellAttrProvider::UpdateAttrCols( size_t pos
, int numCols 
) 
2997         m_data
->m_cellAttrs
.UpdateAttrCols( pos
, numCols 
); 
2999         m_data
->m_colAttrs
.UpdateAttrRowsOrCols( pos
, numCols 
); 
3003 // ---------------------------------------------------------------------------- 
3004 // wxGridTypeRegistry 
3005 // ---------------------------------------------------------------------------- 
3007 wxGridTypeRegistry::~wxGridTypeRegistry() 
3009     size_t count 
= m_typeinfo
.GetCount(); 
3010     for ( size_t i 
= 0; i 
< count
; i
++ ) 
3011         delete m_typeinfo
[i
]; 
3014 void wxGridTypeRegistry::RegisterDataType(const wxString
& typeName
, 
3015                                           wxGridCellRenderer
* renderer
, 
3016                                           wxGridCellEditor
* editor
) 
3018     wxGridDataTypeInfo
* info 
= new wxGridDataTypeInfo(typeName
, renderer
, editor
); 
3020     // is it already registered? 
3021     int loc 
= FindRegisteredDataType(typeName
); 
3022     if ( loc 
!= wxNOT_FOUND 
) 
3024         delete m_typeinfo
[loc
]; 
3025         m_typeinfo
[loc
] = info
; 
3029         m_typeinfo
.Add(info
); 
3033 int wxGridTypeRegistry::FindRegisteredDataType(const wxString
& typeName
) 
3035     size_t count 
= m_typeinfo
.GetCount(); 
3036     for ( size_t i 
= 0; i 
< count
; i
++ ) 
3038         if ( typeName 
== m_typeinfo
[i
]->m_typeName 
) 
3047 int wxGridTypeRegistry::FindDataType(const wxString
& typeName
) 
3049     int index 
= FindRegisteredDataType(typeName
); 
3050     if ( index 
== wxNOT_FOUND 
) 
3052         // check whether this is one of the standard ones, in which case 
3053         // register it "on the fly" 
3055         if ( typeName 
== wxGRID_VALUE_STRING 
) 
3057             RegisterDataType(wxGRID_VALUE_STRING
, 
3058                              new wxGridCellStringRenderer
, 
3059                              new wxGridCellTextEditor
); 
3062 #endif // wxUSE_TEXTCTRL 
3064         if ( typeName 
== wxGRID_VALUE_BOOL 
) 
3066             RegisterDataType(wxGRID_VALUE_BOOL
, 
3067                              new wxGridCellBoolRenderer
, 
3068                              new wxGridCellBoolEditor
); 
3071 #endif // wxUSE_CHECKBOX 
3073         if ( typeName 
== wxGRID_VALUE_NUMBER 
) 
3075             RegisterDataType(wxGRID_VALUE_NUMBER
, 
3076                              new wxGridCellNumberRenderer
, 
3077                              new wxGridCellNumberEditor
); 
3079         else if ( typeName 
== wxGRID_VALUE_FLOAT 
) 
3081             RegisterDataType(wxGRID_VALUE_FLOAT
, 
3082                              new wxGridCellFloatRenderer
, 
3083                              new wxGridCellFloatEditor
); 
3086 #endif // wxUSE_TEXTCTRL 
3088         if ( typeName 
== wxGRID_VALUE_CHOICE 
) 
3090             RegisterDataType(wxGRID_VALUE_CHOICE
, 
3091                              new wxGridCellStringRenderer
, 
3092                              new wxGridCellChoiceEditor
); 
3095 #endif // wxUSE_COMBOBOX 
3100         // we get here only if just added the entry for this type, so return 
3102         index 
= m_typeinfo
.GetCount() - 1; 
3108 int wxGridTypeRegistry::FindOrCloneDataType(const wxString
& typeName
) 
3110     int index 
= FindDataType(typeName
); 
3111     if ( index 
== wxNOT_FOUND 
) 
3113         // the first part of the typename is the "real" type, anything after ':' 
3114         // are the parameters for the renderer 
3115         index 
= FindDataType(typeName
.BeforeFirst(_T(':'))); 
3116         if ( index 
== wxNOT_FOUND 
) 
3121         wxGridCellRenderer 
*renderer 
= GetRenderer(index
); 
3122         wxGridCellRenderer 
*rendererOld 
= renderer
; 
3123         renderer 
= renderer
->Clone(); 
3124         rendererOld
->DecRef(); 
3126         wxGridCellEditor 
*editor 
= GetEditor(index
); 
3127         wxGridCellEditor 
*editorOld 
= editor
; 
3128         editor 
= editor
->Clone(); 
3129         editorOld
->DecRef(); 
3131         // do it even if there are no parameters to reset them to defaults 
3132         wxString params 
= typeName
.AfterFirst(_T(':')); 
3133         renderer
->SetParameters(params
); 
3134         editor
->SetParameters(params
); 
3136         // register the new typename 
3137         RegisterDataType(typeName
, renderer
, editor
); 
3139         // we just registered it, it's the last one 
3140         index 
= m_typeinfo
.GetCount() - 1; 
3146 wxGridCellRenderer
* wxGridTypeRegistry::GetRenderer(int index
) 
3148     wxGridCellRenderer
* renderer 
= m_typeinfo
[index
]->m_renderer
; 
3155 wxGridCellEditor
* wxGridTypeRegistry::GetEditor(int index
) 
3157     wxGridCellEditor
* editor 
= m_typeinfo
[index
]->m_editor
; 
3164 // ---------------------------------------------------------------------------- 
3166 // ---------------------------------------------------------------------------- 
3168 IMPLEMENT_ABSTRACT_CLASS( wxGridTableBase
, wxObject 
) 
3170 wxGridTableBase::wxGridTableBase() 
3172     m_view 
= (wxGrid 
*) NULL
; 
3173     m_attrProvider 
= (wxGridCellAttrProvider 
*) NULL
; 
3176 wxGridTableBase::~wxGridTableBase() 
3178     delete m_attrProvider
; 
3181 void wxGridTableBase::SetAttrProvider(wxGridCellAttrProvider 
*attrProvider
) 
3183     delete m_attrProvider
; 
3184     m_attrProvider 
= attrProvider
; 
3187 bool wxGridTableBase::CanHaveAttributes() 
3189     if ( ! GetAttrProvider() ) 
3191         // use the default attr provider by default 
3192         SetAttrProvider(new wxGridCellAttrProvider
); 
3198 wxGridCellAttr 
*wxGridTableBase::GetAttr(int row
, int col
, wxGridCellAttr::wxAttrKind  kind
) 
3200     if ( m_attrProvider 
) 
3201         return m_attrProvider
->GetAttr(row
, col
, kind
); 
3203         return (wxGridCellAttr 
*)NULL
; 
3206 void wxGridTableBase::SetAttr(wxGridCellAttr
* attr
, int row
, int col
) 
3208     if ( m_attrProvider 
) 
3211             attr
->SetKind(wxGridCellAttr::Cell
); 
3212         m_attrProvider
->SetAttr(attr
, row
, col
); 
3216         // as we take ownership of the pointer and don't store it, we must 
3222 void wxGridTableBase::SetRowAttr(wxGridCellAttr 
*attr
, int row
) 
3224     if ( m_attrProvider 
) 
3226         attr
->SetKind(wxGridCellAttr::Row
); 
3227         m_attrProvider
->SetRowAttr(attr
, row
); 
3231         // as we take ownership of the pointer and don't store it, we must 
3237 void wxGridTableBase::SetColAttr(wxGridCellAttr 
*attr
, int col
) 
3239     if ( m_attrProvider 
) 
3241         attr
->SetKind(wxGridCellAttr::Col
); 
3242         m_attrProvider
->SetColAttr(attr
, col
); 
3246         // as we take ownership of the pointer and don't store it, we must 
3252 bool wxGridTableBase::InsertRows( size_t WXUNUSED(pos
), 
3253                                   size_t WXUNUSED(numRows
) ) 
3255     wxFAIL_MSG( wxT("Called grid table class function InsertRows\nbut your derived table class does not override this function") ); 
3260 bool wxGridTableBase::AppendRows( size_t WXUNUSED(numRows
) ) 
3262     wxFAIL_MSG( wxT("Called grid table class function AppendRows\nbut your derived table class does not override this function")); 
3267 bool wxGridTableBase::DeleteRows( size_t WXUNUSED(pos
), 
3268                                   size_t WXUNUSED(numRows
) ) 
3270     wxFAIL_MSG( wxT("Called grid table class function DeleteRows\nbut your derived table class does not override this function")); 
3275 bool wxGridTableBase::InsertCols( size_t WXUNUSED(pos
), 
3276                                   size_t WXUNUSED(numCols
) ) 
3278     wxFAIL_MSG( wxT("Called grid table class function InsertCols\nbut your derived table class does not override this function")); 
3283 bool wxGridTableBase::AppendCols( size_t WXUNUSED(numCols
) ) 
3285     wxFAIL_MSG(wxT("Called grid table class function AppendCols\nbut your derived table class does not override this function")); 
3290 bool wxGridTableBase::DeleteCols( size_t WXUNUSED(pos
), 
3291                                   size_t WXUNUSED(numCols
) ) 
3293     wxFAIL_MSG( wxT("Called grid table class function DeleteCols\nbut your derived table class does not override this function")); 
3298 wxString 
wxGridTableBase::GetRowLabelValue( int row 
) 
3302     // RD: Starting the rows at zero confuses users, 
3303     // no matter how much it makes sense to us geeks. 
3309 wxString 
wxGridTableBase::GetColLabelValue( int col 
) 
3311     // default col labels are: 
3312     //   cols 0 to 25   : A-Z 
3313     //   cols 26 to 675 : AA-ZZ 
3318     for ( n 
= 1; ; n
++ ) 
3320         s 
+= (wxChar
) (_T('A') + (wxChar
)(col 
% 26)); 
3326     // reverse the string... 
3328     for ( i 
= 0; i 
< n
; i
++ ) 
3336 wxString 
wxGridTableBase::GetTypeName( int WXUNUSED(row
), int WXUNUSED(col
) ) 
3338     return wxGRID_VALUE_STRING
; 
3341 bool wxGridTableBase::CanGetValueAs( int WXUNUSED(row
), int WXUNUSED(col
), 
3342                                      const wxString
& typeName 
) 
3344     return typeName 
== wxGRID_VALUE_STRING
; 
3347 bool wxGridTableBase::CanSetValueAs( int row
, int col
, const wxString
& typeName 
) 
3349     return CanGetValueAs(row
, col
, typeName
); 
3352 long wxGridTableBase::GetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
) ) 
3357 double wxGridTableBase::GetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
) ) 
3362 bool wxGridTableBase::GetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
) ) 
3367 void wxGridTableBase::SetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
), 
3368                                       long WXUNUSED(value
) ) 
3372 void wxGridTableBase::SetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
), 
3373                                         double WXUNUSED(value
) ) 
3377 void wxGridTableBase::SetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
), 
3378                                       bool WXUNUSED(value
) ) 
3382 void* wxGridTableBase::GetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
), 
3383                                          const wxString
& WXUNUSED(typeName
) ) 
3388 void  wxGridTableBase::SetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
), 
3389                                          const wxString
& WXUNUSED(typeName
), 
3390                                          void* WXUNUSED(value
) ) 
3394 ////////////////////////////////////////////////////////////////////// 
3396 // Message class for the grid table to send requests and notifications 
3400 wxGridTableMessage::wxGridTableMessage() 
3402     m_table 
= (wxGridTableBase 
*) NULL
; 
3408 wxGridTableMessage::wxGridTableMessage( wxGridTableBase 
*table
, int id
, 
3409                                         int commandInt1
, int commandInt2 
) 
3413     m_comInt1 
= commandInt1
; 
3414     m_comInt2 
= commandInt2
; 
3417 ////////////////////////////////////////////////////////////////////// 
3419 // A basic grid table for string data. An object of this class will 
3420 // created by wxGrid if you don't specify an alternative table class. 
3423 WX_DEFINE_OBJARRAY(wxGridStringArray
) 
3425 IMPLEMENT_DYNAMIC_CLASS( wxGridStringTable
, wxGridTableBase 
) 
3427 wxGridStringTable::wxGridStringTable() 
3432 wxGridStringTable::wxGridStringTable( int numRows
, int numCols 
) 
3435     m_data
.Alloc( numRows 
); 
3438     sa
.Alloc( numCols 
); 
3439     sa
.Add( wxEmptyString
, numCols 
); 
3441     m_data
.Add( sa
, numRows 
); 
3444 wxGridStringTable::~wxGridStringTable() 
3448 int wxGridStringTable::GetNumberRows() 
3450     return m_data
.GetCount(); 
3453 int wxGridStringTable::GetNumberCols() 
3455     if ( m_data
.GetCount() > 0 ) 
3456         return m_data
[0].GetCount(); 
3461 wxString 
wxGridStringTable::GetValue( int row
, int col 
) 
3463     wxCHECK_MSG( (row 
< GetNumberRows()) && (col 
< GetNumberCols()), 
3465                  _T("invalid row or column index in wxGridStringTable") ); 
3467     return m_data
[row
][col
]; 
3470 void wxGridStringTable::SetValue( int row
, int col
, const wxString
& value 
) 
3472     wxCHECK_RET( (row 
< GetNumberRows()) && (col 
< GetNumberCols()), 
3473                  _T("invalid row or column index in wxGridStringTable") ); 
3475     m_data
[row
][col
] = value
; 
3478 bool wxGridStringTable::IsEmptyCell( int row
, int col 
) 
3480     wxCHECK_MSG( (row 
< GetNumberRows()) && (col 
< GetNumberCols()), 
3482                   _T("invalid row or column index in wxGridStringTable") ); 
3484     return (m_data
[row
][col
] == wxEmptyString
); 
3487 void wxGridStringTable::Clear() 
3490     int numRows
, numCols
; 
3492     numRows 
= m_data
.GetCount(); 
3495         numCols 
= m_data
[0].GetCount(); 
3497         for ( row 
= 0; row 
< numRows
; row
++ ) 
3499             for ( col 
= 0; col 
< numCols
; col
++ ) 
3501                 m_data
[row
][col
] = wxEmptyString
; 
3507 bool wxGridStringTable::InsertRows( size_t pos
, size_t numRows 
) 
3509     size_t curNumRows 
= m_data
.GetCount(); 
3510     size_t curNumCols 
= ( curNumRows 
> 0 ? m_data
[0].GetCount() : 
3511                           ( GetView() ? GetView()->GetNumberCols() : 0 ) ); 
3513     if ( pos 
>= curNumRows 
) 
3515         return AppendRows( numRows 
); 
3519     sa
.Alloc( curNumCols 
); 
3520     sa
.Add( wxEmptyString
, curNumCols 
); 
3521     m_data
.Insert( sa
, pos
, numRows 
); 
3525         wxGridTableMessage 
msg( this, 
3526                                 wxGRIDTABLE_NOTIFY_ROWS_INSERTED
, 
3530         GetView()->ProcessTableMessage( msg 
); 
3536 bool wxGridStringTable::AppendRows( size_t numRows 
) 
3538     size_t curNumRows 
= m_data
.GetCount(); 
3539     size_t curNumCols 
= ( curNumRows 
> 0 
3540                          ? m_data
[0].GetCount() 
3541                          : ( GetView() ? GetView()->GetNumberCols() : 0 ) ); 
3544     if ( curNumCols 
> 0 ) 
3546         sa
.Alloc( curNumCols 
); 
3547         sa
.Add( wxEmptyString
, curNumCols 
); 
3550     m_data
.Add( sa
, numRows 
); 
3554         wxGridTableMessage 
msg( this, 
3555                                 wxGRIDTABLE_NOTIFY_ROWS_APPENDED
, 
3558         GetView()->ProcessTableMessage( msg 
); 
3564 bool wxGridStringTable::DeleteRows( size_t pos
, size_t numRows 
) 
3566     size_t curNumRows 
= m_data
.GetCount(); 
3568     if ( pos 
>= curNumRows 
) 
3570         wxFAIL_MSG( wxString::Format
 
3572                         wxT("Called wxGridStringTable::DeleteRows(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu rows"), 
3574                         (unsigned long)numRows
, 
3575                         (unsigned long)curNumRows
 
3581     if ( numRows 
> curNumRows 
- pos 
) 
3583         numRows 
= curNumRows 
- pos
; 
3586     if ( numRows 
>= curNumRows 
) 
3592         m_data
.RemoveAt( pos
, numRows 
); 
3597         wxGridTableMessage 
msg( this, 
3598                                 wxGRIDTABLE_NOTIFY_ROWS_DELETED
, 
3602         GetView()->ProcessTableMessage( msg 
); 
3608 bool wxGridStringTable::InsertCols( size_t pos
, size_t numCols 
) 
3612     size_t curNumRows 
= m_data
.GetCount(); 
3613     size_t curNumCols 
= ( curNumRows 
> 0 
3614                          ? m_data
[0].GetCount() 
3615                          : ( GetView() ? GetView()->GetNumberCols() : 0 ) ); 
3617     if ( pos 
>= curNumCols 
) 
3619         return AppendCols( numCols 
); 
3622     if ( !m_colLabels
.IsEmpty() ) 
3624         m_colLabels
.Insert( wxEmptyString
, pos
, numCols 
); 
3627         for ( i 
= pos
; i 
< pos 
+ numCols
; i
++ ) 
3628             m_colLabels
[i
] = wxGridTableBase::GetColLabelValue( i 
); 
3631     for ( row 
= 0; row 
< curNumRows
; row
++ ) 
3633         for ( col 
= pos
; col 
< pos 
+ numCols
; col
++ ) 
3635             m_data
[row
].Insert( wxEmptyString
, col 
); 
3641         wxGridTableMessage 
msg( this, 
3642                                 wxGRIDTABLE_NOTIFY_COLS_INSERTED
, 
3646         GetView()->ProcessTableMessage( msg 
); 
3652 bool wxGridStringTable::AppendCols( size_t numCols 
) 
3656     size_t curNumRows 
= m_data
.GetCount(); 
3661         // TODO: something better than this ? 
3663         wxFAIL_MSG( wxT("Unable to append cols to a grid table with no rows.\nCall AppendRows() first") ); 
3668     for ( row 
= 0; row 
< curNumRows
; row
++ ) 
3670         m_data
[row
].Add( wxEmptyString
, numCols 
); 
3675         wxGridTableMessage 
msg( this, 
3676                                 wxGRIDTABLE_NOTIFY_COLS_APPENDED
, 
3679         GetView()->ProcessTableMessage( msg 
); 
3685 bool wxGridStringTable::DeleteCols( size_t pos
, size_t numCols 
) 
3689     size_t curNumRows 
= m_data
.GetCount(); 
3690     size_t curNumCols 
= ( curNumRows 
> 0 ? m_data
[0].GetCount() : 
3691                           ( GetView() ? GetView()->GetNumberCols() : 0 ) ); 
3693     if ( pos 
>= curNumCols 
) 
3695         wxFAIL_MSG( wxString::Format
 
3697                         wxT("Called wxGridStringTable::DeleteCols(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu cols"), 
3699                         (unsigned long)numCols
, 
3700                         (unsigned long)curNumCols
 
3707         colID 
= GetView()->GetColAt( pos 
); 
3711     if ( numCols 
> curNumCols 
- colID 
) 
3713         numCols 
= curNumCols 
- colID
; 
3716     if ( !m_colLabels
.IsEmpty() ) 
3718         // m_colLabels stores just as many elements as it needs, e.g. if only 
3719         // the label of the first column had been set it would have only one 
3720         // element and not numCols, so account for it 
3721         int nToRm 
= m_colLabels
.size() - colID
; 
3723             m_colLabels
.RemoveAt( colID
, nToRm 
); 
3726     for ( row 
= 0; row 
< curNumRows
; row
++ ) 
3728         if ( numCols 
>= curNumCols 
) 
3730             m_data
[row
].Clear(); 
3734             m_data
[row
].RemoveAt( colID
, numCols 
); 
3740         wxGridTableMessage 
msg( this, 
3741                                 wxGRIDTABLE_NOTIFY_COLS_DELETED
, 
3745         GetView()->ProcessTableMessage( msg 
); 
3751 wxString 
wxGridStringTable::GetRowLabelValue( int row 
) 
3753     if ( row 
> (int)(m_rowLabels
.GetCount()) - 1 ) 
3755         // using default label 
3757         return wxGridTableBase::GetRowLabelValue( row 
); 
3761         return m_rowLabels
[row
]; 
3765 wxString 
wxGridStringTable::GetColLabelValue( int col 
) 
3767     if ( col 
> (int)(m_colLabels
.GetCount()) - 1 ) 
3769         // using default label 
3771         return wxGridTableBase::GetColLabelValue( col 
); 
3775         return m_colLabels
[col
]; 
3779 void wxGridStringTable::SetRowLabelValue( int row
, const wxString
& value 
) 
3781     if ( row 
> (int)(m_rowLabels
.GetCount()) - 1 ) 
3783         int n 
= m_rowLabels
.GetCount(); 
3786         for ( i 
= n
; i 
<= row
; i
++ ) 
3788             m_rowLabels
.Add( wxGridTableBase::GetRowLabelValue(i
) ); 
3792     m_rowLabels
[row
] = value
; 
3795 void wxGridStringTable::SetColLabelValue( int col
, const wxString
& value 
) 
3797     if ( col 
> (int)(m_colLabels
.GetCount()) - 1 ) 
3799         int n 
= m_colLabels
.GetCount(); 
3802         for ( i 
= n
; i 
<= col
; i
++ ) 
3804             m_colLabels
.Add( wxGridTableBase::GetColLabelValue(i
) ); 
3808     m_colLabels
[col
] = value
; 
3812 ////////////////////////////////////////////////////////////////////// 
3813 ////////////////////////////////////////////////////////////////////// 
3815 BEGIN_EVENT_TABLE(wxGridSubwindow
, wxWindow
) 
3816     EVT_MOUSE_CAPTURE_LOST(wxGridSubwindow::OnMouseCaptureLost
) 
3819 void wxGridSubwindow::OnMouseCaptureLost(wxMouseCaptureLostEvent
& WXUNUSED(event
)) 
3821     m_owner
->CancelMouseCapture(); 
3824 IMPLEMENT_DYNAMIC_CLASS( wxGridRowLabelWindow
, wxWindow 
) 
3826 BEGIN_EVENT_TABLE( wxGridRowLabelWindow
, wxGridSubwindow 
) 
3827     EVT_PAINT( wxGridRowLabelWindow::OnPaint 
) 
3828     EVT_MOUSEWHEEL( wxGridRowLabelWindow::OnMouseWheel 
) 
3829     EVT_MOUSE_EVENTS( wxGridRowLabelWindow::OnMouseEvent 
) 
3832 wxGridRowLabelWindow::wxGridRowLabelWindow( wxGrid 
*parent
, 
3834                                             const wxPoint 
&pos
, const wxSize 
&size 
) 
3835   : wxGridSubwindow(parent
, id
, pos
, size
) 
3840 void wxGridRowLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) ) 
3844     // NO - don't do this because it will set both the x and y origin 
3845     // coords to match the parent scrolled window and we just want to 
3846     // set the y coord  - MB 
3848     // m_owner->PrepareDC( dc ); 
3851     m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y 
); 
3852     wxPoint pt 
= dc
.GetDeviceOrigin(); 
3853     dc
.SetDeviceOrigin( pt
.x
, pt
.y
-y 
); 
3855     wxArrayInt rows 
= m_owner
->CalcRowLabelsExposed( GetUpdateRegion() ); 
3856     m_owner
->DrawRowLabels( dc
, rows 
); 
3859 void wxGridRowLabelWindow::OnMouseEvent( wxMouseEvent
& event 
) 
3861     m_owner
->ProcessRowLabelMouseEvent( event 
); 
3864 void wxGridRowLabelWindow::OnMouseWheel( wxMouseEvent
& event 
) 
3866     m_owner
->GetEventHandler()->ProcessEvent( event 
); 
3869 ////////////////////////////////////////////////////////////////////// 
3871 IMPLEMENT_DYNAMIC_CLASS( wxGridColLabelWindow
, wxWindow 
) 
3873 BEGIN_EVENT_TABLE( wxGridColLabelWindow
, wxGridSubwindow 
) 
3874     EVT_PAINT( wxGridColLabelWindow::OnPaint 
) 
3875     EVT_MOUSEWHEEL( wxGridColLabelWindow::OnMouseWheel 
) 
3876     EVT_MOUSE_EVENTS( wxGridColLabelWindow::OnMouseEvent 
) 
3879 wxGridColLabelWindow::wxGridColLabelWindow( wxGrid 
*parent
, 
3881                                             const wxPoint 
&pos
, const wxSize 
&size 
) 
3882   : wxGridSubwindow(parent
, id
, pos
, size
) 
3887 void wxGridColLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) ) 
3891     // NO - don't do this because it will set both the x and y origin 
3892     // coords to match the parent scrolled window and we just want to 
3893     // set the x coord  - MB 
3895     // m_owner->PrepareDC( dc ); 
3898     m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y 
); 
3899     wxPoint pt 
= dc
.GetDeviceOrigin(); 
3900     if (GetLayoutDirection() == wxLayout_RightToLeft
) 
3901         dc
.SetDeviceOrigin( pt
.x
+x
, pt
.y 
); 
3903         dc
.SetDeviceOrigin( pt
.x
-x
, pt
.y 
); 
3905     wxArrayInt cols 
= m_owner
->CalcColLabelsExposed( GetUpdateRegion() ); 
3906     m_owner
->DrawColLabels( dc
, cols 
); 
3909 void wxGridColLabelWindow::OnMouseEvent( wxMouseEvent
& event 
) 
3911     m_owner
->ProcessColLabelMouseEvent( event 
); 
3914 void wxGridColLabelWindow::OnMouseWheel( wxMouseEvent
& event 
) 
3916     m_owner
->GetEventHandler()->ProcessEvent( event 
); 
3919 ////////////////////////////////////////////////////////////////////// 
3921 IMPLEMENT_DYNAMIC_CLASS( wxGridCornerLabelWindow
, wxWindow 
) 
3923 BEGIN_EVENT_TABLE( wxGridCornerLabelWindow
, wxGridSubwindow 
) 
3924     EVT_MOUSEWHEEL( wxGridCornerLabelWindow::OnMouseWheel 
) 
3925     EVT_MOUSE_EVENTS( wxGridCornerLabelWindow::OnMouseEvent 
) 
3926     EVT_PAINT( wxGridCornerLabelWindow::OnPaint 
) 
3929 wxGridCornerLabelWindow::wxGridCornerLabelWindow( wxGrid 
*parent
, 
3931                                                   const wxPoint 
&pos
, const wxSize 
&size 
) 
3932   : wxGridSubwindow(parent
, id
, pos
, size
) 
3937 void wxGridCornerLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) ) 
3941     int client_height 
= 0; 
3942     int client_width 
= 0; 
3943     GetClientSize( &client_width
, &client_height 
); 
3945     // VZ: any reason for this ifdef? (FIXME) 
3951     rect
.SetWidth( client_width 
- 2 ); 
3952     rect
.SetHeight( client_height 
- 2 ); 
3954     wxRendererNative::Get().DrawHeaderButton( this, dc
, rect
, 0 ); 
3956     dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW
), 1, wxPENSTYLE_SOLID
) ); 
3957     dc
.DrawLine( client_width 
- 1, client_height 
- 1, client_width 
- 1, 0 ); 
3958     dc
.DrawLine( client_width 
- 1, client_height 
- 1, 0, client_height 
- 1 ); 
3959     dc
.DrawLine( 0, 0, client_width
, 0 ); 
3960     dc
.DrawLine( 0, 0, 0, client_height 
); 
3962     dc
.SetPen( *wxWHITE_PEN 
); 
3963     dc
.DrawLine( 1, 1, client_width 
- 1, 1 ); 
3964     dc
.DrawLine( 1, 1, 1, client_height 
- 1 ); 
3968 void wxGridCornerLabelWindow::OnMouseEvent( wxMouseEvent
& event 
) 
3970     m_owner
->ProcessCornerLabelMouseEvent( event 
); 
3973 void wxGridCornerLabelWindow::OnMouseWheel( wxMouseEvent
& event 
) 
3975     m_owner
->GetEventHandler()->ProcessEvent(event
); 
3978 ////////////////////////////////////////////////////////////////////// 
3980 IMPLEMENT_DYNAMIC_CLASS( wxGridWindow
, wxWindow 
) 
3982 BEGIN_EVENT_TABLE( wxGridWindow
, wxGridSubwindow 
) 
3983     EVT_PAINT( wxGridWindow::OnPaint 
) 
3984     EVT_MOUSEWHEEL( wxGridWindow::OnMouseWheel 
) 
3985     EVT_MOUSE_EVENTS( wxGridWindow::OnMouseEvent 
) 
3986     EVT_KEY_DOWN( wxGridWindow::OnKeyDown 
) 
3987     EVT_KEY_UP( wxGridWindow::OnKeyUp 
) 
3988     EVT_CHAR( wxGridWindow::OnChar 
) 
3989     EVT_SET_FOCUS( wxGridWindow::OnFocus 
) 
3990     EVT_KILL_FOCUS( wxGridWindow::OnFocus 
) 
3991     EVT_ERASE_BACKGROUND( wxGridWindow::OnEraseBackground 
) 
3994 wxGridWindow::wxGridWindow( wxGrid 
*parent
, 
3995                             wxGridRowLabelWindow 
*rowLblWin
, 
3996                             wxGridColLabelWindow 
*colLblWin
, 
3999                             const wxSize 
&size 
) 
4000             : wxGridSubwindow(parent
, id
, pos
, size
, 
4001                               wxWANTS_CHARS 
| wxCLIP_CHILDREN
, 
4002                               wxT("grid window") ) 
4005     m_rowLabelWin 
= rowLblWin
; 
4006     m_colLabelWin 
= colLblWin
; 
4009 void wxGridWindow::OnPaint( wxPaintEvent 
&WXUNUSED(event
) ) 
4011     wxPaintDC 
dc( this ); 
4012     m_owner
->PrepareDC( dc 
); 
4013     wxRegion reg 
= GetUpdateRegion(); 
4014     wxGridCellCoordsArray dirtyCells 
= m_owner
->CalcCellsExposed( reg 
); 
4015     m_owner
->DrawGridCellArea( dc
, dirtyCells 
); 
4017 #if WXGRID_DRAW_LINES 
4018     m_owner
->DrawAllGridLines( dc
, reg 
); 
4021     m_owner
->DrawGridSpace( dc 
); 
4022     m_owner
->DrawHighlight( dc
, dirtyCells 
); 
4025 void wxGridWindow::ScrollWindow( int dx
, int dy
, const wxRect 
*rect 
) 
4027     wxWindow::ScrollWindow( dx
, dy
, rect 
); 
4028     m_rowLabelWin
->ScrollWindow( 0, dy
, rect 
); 
4029     m_colLabelWin
->ScrollWindow( dx
, 0, rect 
); 
4032 void wxGridWindow::OnMouseEvent( wxMouseEvent
& event 
) 
4034     if (event
.ButtonDown(wxMOUSE_BTN_LEFT
) && FindFocus() != this) 
4037     m_owner
->ProcessGridCellMouseEvent( event 
); 
4040 void wxGridWindow::OnMouseWheel( wxMouseEvent
& event 
) 
4042     m_owner
->GetEventHandler()->ProcessEvent( event 
); 
4045 // This seems to be required for wxMotif/wxGTK otherwise the mouse 
4046 // cursor must be in the cell edit control to get key events 
4048 void wxGridWindow::OnKeyDown( wxKeyEvent
& event 
) 
4050     if ( !m_owner
->GetEventHandler()->ProcessEvent( event 
) ) 
4054 void wxGridWindow::OnKeyUp( wxKeyEvent
& event 
) 
4056     if ( !m_owner
->GetEventHandler()->ProcessEvent( event 
) ) 
4060 void wxGridWindow::OnChar( wxKeyEvent
& event 
) 
4062     if ( !m_owner
->GetEventHandler()->ProcessEvent( event 
) ) 
4066 void wxGridWindow::OnEraseBackground( wxEraseEvent
& WXUNUSED(event
) ) 
4070 void wxGridWindow::OnFocus(wxFocusEvent
& event
) 
4072     // and if we have any selection, it has to be repainted, because it 
4073     // uses different colour when the grid is not focused: 
4074     if ( m_owner
->IsSelection() ) 
4080         // NB: Note that this code is in "else" branch only because the other 
4081         //     branch refreshes everything and so there's no point in calling 
4082         //     Refresh() again, *not* because it should only be done if 
4083         //     !IsSelection(). If the above code is ever optimized to refresh 
4084         //     only selected area, this needs to be moved out of the "else" 
4085         //     branch so that it's always executed. 
4087         // current cell cursor {dis,re}appears on focus change: 
4088         const wxGridCellCoords 
cursorCoords(m_owner
->GetGridCursorRow(), 
4089                                             m_owner
->GetGridCursorCol()); 
4090         const wxRect cursor 
= 
4091             m_owner
->BlockToDeviceRect(cursorCoords
, cursorCoords
); 
4092         Refresh(true, &cursor
); 
4095     if ( !m_owner
->GetEventHandler()->ProcessEvent( event 
) ) 
4099 ////////////////////////////////////////////////////////////////////// 
4101 // Internal Helper function for computing row or column from some 
4102 // (unscrolled) coordinate value, using either 
4103 // m_defaultRowHeight/m_defaultColWidth or binary search on array 
4104 // of m_rowBottoms/m_ColRights to speed up the search! 
4106 // Internal helper macros for simpler use of that function 
4108 static int CoordToRowOrCol(int coord
, int defaultDist
, int minDist
, 
4109                            const wxArrayInt
& BorderArray
, int nMax
, 
4112 #define internalXToCol(x) XToCol(x, true) 
4113 #define internalYToRow(y) CoordToRowOrCol(y, m_defaultRowHeight, \ 
4114                                           m_minAcceptableRowHeight, \ 
4115                                           m_rowBottoms, m_numRows, true) 
4117 ///////////////////////////////////////////////////////////////////// 
4119 #if wxUSE_EXTENDED_RTTI 
4120 WX_DEFINE_FLAGS( wxGridStyle 
) 
4122 wxBEGIN_FLAGS( wxGridStyle 
) 
4123     // new style border flags, we put them first to 
4124     // use them for streaming out 
4125     wxFLAGS_MEMBER(wxBORDER_SIMPLE
) 
4126     wxFLAGS_MEMBER(wxBORDER_SUNKEN
) 
4127     wxFLAGS_MEMBER(wxBORDER_DOUBLE
) 
4128     wxFLAGS_MEMBER(wxBORDER_RAISED
) 
4129     wxFLAGS_MEMBER(wxBORDER_STATIC
) 
4130     wxFLAGS_MEMBER(wxBORDER_NONE
) 
4132     // old style border flags 
4133     wxFLAGS_MEMBER(wxSIMPLE_BORDER
) 
4134     wxFLAGS_MEMBER(wxSUNKEN_BORDER
) 
4135     wxFLAGS_MEMBER(wxDOUBLE_BORDER
) 
4136     wxFLAGS_MEMBER(wxRAISED_BORDER
) 
4137     wxFLAGS_MEMBER(wxSTATIC_BORDER
) 
4138     wxFLAGS_MEMBER(wxBORDER
) 
4140     // standard window styles 
4141     wxFLAGS_MEMBER(wxTAB_TRAVERSAL
) 
4142     wxFLAGS_MEMBER(wxCLIP_CHILDREN
) 
4143     wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
) 
4144     wxFLAGS_MEMBER(wxWANTS_CHARS
) 
4145     wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE
) 
4146     wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
) 
4147     wxFLAGS_MEMBER(wxVSCROLL
) 
4148     wxFLAGS_MEMBER(wxHSCROLL
) 
4150 wxEND_FLAGS( wxGridStyle 
) 
4152 IMPLEMENT_DYNAMIC_CLASS_XTI(wxGrid
, wxScrolledWindow
,"wx/grid.h") 
4154 wxBEGIN_PROPERTIES_TABLE(wxGrid
) 
4155     wxHIDE_PROPERTY( Children 
) 
4156     wxPROPERTY_FLAGS( WindowStyle 
, wxGridStyle 
, long , SetWindowStyleFlag 
, GetWindowStyleFlag 
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style 
4157 wxEND_PROPERTIES_TABLE() 
4159 wxBEGIN_HANDLERS_TABLE(wxGrid
) 
4160 wxEND_HANDLERS_TABLE() 
4162 wxCONSTRUCTOR_5( wxGrid 
, wxWindow
* , Parent 
, wxWindowID 
, Id 
, wxPoint 
, Position 
, wxSize 
, Size 
, long , WindowStyle 
) 
4165  TODO : Expose more information of a list's layout, etc. via appropriate objects (e.g., NotebookPageInfo) 
4168 IMPLEMENT_DYNAMIC_CLASS( wxGrid
, wxScrolledWindow 
) 
4171 BEGIN_EVENT_TABLE( wxGrid
, wxScrolledWindow 
) 
4172     EVT_PAINT( wxGrid::OnPaint 
) 
4173     EVT_SIZE( wxGrid::OnSize 
) 
4174     EVT_KEY_DOWN( wxGrid::OnKeyDown 
) 
4175     EVT_KEY_UP( wxGrid::OnKeyUp 
) 
4176     EVT_CHAR ( wxGrid::OnChar 
) 
4177     EVT_ERASE_BACKGROUND( wxGrid::OnEraseBackground 
) 
4185 wxGrid::wxGrid( wxWindow 
*parent
, 
4190                  const wxString
& name 
) 
4193     Create(parent
, id
, pos
, size
, style
, name
); 
4196 bool wxGrid::Create(wxWindow 
*parent
, wxWindowID id
, 
4197                           const wxPoint
& pos
, const wxSize
& size
, 
4198                           long style
, const wxString
& name
) 
4200     if (!wxScrolledWindow::Create(parent
, id
, pos
, size
, 
4201                                   style 
| wxWANTS_CHARS
, name
)) 
4204     m_colMinWidths 
= wxLongToLongHashMap(GRID_HASH_SIZE
); 
4205     m_rowMinHeights 
= wxLongToLongHashMap(GRID_HASH_SIZE
); 
4208     SetInitialSize(size
); 
4209     SetScrollRate(m_scrollLineX
, m_scrollLineY
); 
4217     // Must do this or ~wxScrollHelper will pop the wrong event handler 
4218     SetTargetWindow(this); 
4220     wxSafeDecRef(m_defaultCellAttr
); 
4222 #ifdef DEBUG_ATTR_CACHE 
4223     size_t total 
= gs_nAttrCacheHits 
+ gs_nAttrCacheMisses
; 
4224     wxPrintf(_T("wxGrid attribute cache statistics: " 
4225                 "total: %u, hits: %u (%u%%)\n"), 
4226              total
, gs_nAttrCacheHits
, 
4227              total 
? (gs_nAttrCacheHits
*100) / total 
: 0); 
4230     // if we own the table, just delete it, otherwise at least don't leave it 
4231     // with dangling view pointer 
4234     else if ( m_table 
&& m_table
->GetView() == this ) 
4235         m_table
->SetView(NULL
); 
4237     delete m_typeRegistry
; 
4242 // ----- internal init and update functions 
4245 // NOTE: If using the default visual attributes works everywhere then this can 
4246 // be removed as well as the #else cases below. 
4247 #define _USE_VISATTR 0 
4249 void wxGrid::Create() 
4251     // create the type registry 
4252     m_typeRegistry 
= new wxGridTypeRegistry
; 
4254     m_cellEditCtrlEnabled 
= false; 
4256     m_defaultCellAttr 
= new wxGridCellAttr(); 
4258     // Set default cell attributes 
4259     m_defaultCellAttr
->SetDefAttr(m_defaultCellAttr
); 
4260     m_defaultCellAttr
->SetKind(wxGridCellAttr::Default
); 
4261     m_defaultCellAttr
->SetFont(GetFont()); 
4262     m_defaultCellAttr
->SetAlignment(wxALIGN_LEFT
, wxALIGN_TOP
); 
4263     m_defaultCellAttr
->SetRenderer(new wxGridCellStringRenderer
); 
4264     m_defaultCellAttr
->SetEditor(new wxGridCellTextEditor
); 
4267     wxVisualAttributes gva 
= wxListBox::GetClassDefaultAttributes(); 
4268     wxVisualAttributes lva 
= wxPanel::GetClassDefaultAttributes(); 
4270     m_defaultCellAttr
->SetTextColour(gva
.colFg
); 
4271     m_defaultCellAttr
->SetBackgroundColour(gva
.colBg
); 
4274     m_defaultCellAttr
->SetTextColour( 
4275         wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
)); 
4276     m_defaultCellAttr
->SetBackgroundColour( 
4277         wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
)); 
4282     m_currentCellCoords 
= wxGridNoCellCoords
; 
4284     m_rowLabelWidth 
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
; 
4285     m_colLabelHeight 
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
; 
4287     // subwindow components that make up the wxGrid 
4288     m_rowLabelWin 
= new wxGridRowLabelWindow( this, 
4293     m_colLabelWin 
= new wxGridColLabelWindow( this, 
4298     m_cornerLabelWin 
= new wxGridCornerLabelWindow( this, 
4303     m_gridWin 
= new wxGridWindow( this, 
4310     SetTargetWindow( m_gridWin 
); 
4313     wxColour gfg 
= gva
.colFg
; 
4314     wxColour gbg 
= gva
.colBg
; 
4315     wxColour lfg 
= lva
.colFg
; 
4316     wxColour lbg 
= lva
.colBg
; 
4318     wxColour gfg 
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT 
); 
4319     wxColour gbg 
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOW 
); 
4320     wxColour lfg 
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT 
); 
4321     wxColour lbg 
= wxSystemSettings::GetColour( wxSYS_COLOUR_BTNFACE 
); 
4324     m_cornerLabelWin
->SetOwnForegroundColour(lfg
); 
4325     m_cornerLabelWin
->SetOwnBackgroundColour(lbg
); 
4326     m_rowLabelWin
->SetOwnForegroundColour(lfg
); 
4327     m_rowLabelWin
->SetOwnBackgroundColour(lbg
); 
4328     m_colLabelWin
->SetOwnForegroundColour(lfg
); 
4329     m_colLabelWin
->SetOwnBackgroundColour(lbg
); 
4331     m_gridWin
->SetOwnForegroundColour(gfg
); 
4332     m_gridWin
->SetOwnBackgroundColour(gbg
); 
4337 bool wxGrid::CreateGrid( int numRows
, int numCols
, 
4338                          wxGrid::wxGridSelectionModes selmode 
) 
4340     wxCHECK_MSG( !m_created
, 
4342                  wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") ); 
4344     m_numRows 
= numRows
; 
4345     m_numCols 
= numCols
; 
4347     m_table 
= new wxGridStringTable( m_numRows
, m_numCols 
); 
4348     m_table
->SetView( this ); 
4350     m_selection 
= new wxGridSelection( this, selmode 
); 
4359 void wxGrid::SetSelectionMode(wxGrid::wxGridSelectionModes selmode
) 
4361     wxCHECK_RET( m_created
, 
4362                  wxT("Called wxGrid::SetSelectionMode() before calling CreateGrid()") ); 
4364     m_selection
->SetSelectionMode( selmode 
); 
4367 wxGrid::wxGridSelectionModes 
wxGrid::GetSelectionMode() const 
4369     wxCHECK_MSG( m_created
, wxGrid::wxGridSelectCells
, 
4370                  wxT("Called wxGrid::GetSelectionMode() before calling CreateGrid()") ); 
4372     return m_selection
->GetSelectionMode(); 
4375 bool wxGrid::SetTable( wxGridTableBase 
*table
, bool takeOwnership
, 
4376                        wxGrid::wxGridSelectionModes selmode 
) 
4378     bool checkSelection 
= false; 
4381         // stop all processing 
4386             m_table
->SetView(0); 
4398         checkSelection 
= true; 
4400         // kill row and column size arrays 
4401         m_colWidths
.Empty(); 
4402         m_colRights
.Empty(); 
4403         m_rowHeights
.Empty(); 
4404         m_rowBottoms
.Empty(); 
4409         m_numRows 
= table
->GetNumberRows(); 
4410         m_numCols 
= table
->GetNumberCols(); 
4413         m_table
->SetView( this ); 
4414         m_ownTable 
= takeOwnership
; 
4415         m_selection 
= new wxGridSelection( this, selmode 
); 
4418             // If the newly set table is smaller than the 
4419             // original one current cell and selection regions 
4420             // might be invalid, 
4421             m_selectingKeyboard 
= wxGridNoCellCoords
; 
4422             m_currentCellCoords 
= 
4423               wxGridCellCoords(wxMin(m_numRows
, m_currentCellCoords
.GetRow()), 
4424                                wxMin(m_numCols
, m_currentCellCoords
.GetCol())); 
4425             if (m_selectingTopLeft
.GetRow() >= m_numRows 
|| 
4426                 m_selectingTopLeft
.GetCol() >= m_numCols
) 
4428                 m_selectingTopLeft 
= wxGridNoCellCoords
; 
4429                 m_selectingBottomRight 
= wxGridNoCellCoords
; 
4432                 m_selectingBottomRight 
= 
4433                   wxGridCellCoords(wxMin(m_numRows
, 
4434                                          m_selectingBottomRight
.GetRow()), 
4436                                          m_selectingBottomRight
.GetCol())); 
4446 void wxGrid::InitVars() 
4450     m_cornerLabelWin 
= NULL
; 
4451     m_rowLabelWin 
= NULL
; 
4452     m_colLabelWin 
= NULL
; 
4459     m_defaultCellAttr 
= NULL
; 
4460     m_typeRegistry 
= NULL
; 
4461     m_winCapture 
= NULL
; 
4466     m_rowLabelWidth  
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
; 
4467     m_colLabelHeight 
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
; 
4469     if ( m_rowLabelWin 
) 
4471         m_labelBackgroundColour 
= m_rowLabelWin
->GetBackgroundColour(); 
4475         m_labelBackgroundColour 
= *wxWHITE
; 
4478     m_labelTextColour 
= *wxBLACK
; 
4481     m_attrCache
.row 
= -1; 
4482     m_attrCache
.col 
= -1; 
4483     m_attrCache
.attr 
= NULL
; 
4485     // TODO: something better than this ? 
4487     m_labelFont 
= this->GetFont(); 
4488     m_labelFont
.SetWeight( wxBOLD 
); 
4490     m_rowLabelHorizAlign 
= wxALIGN_CENTRE
; 
4491     m_rowLabelVertAlign  
= wxALIGN_CENTRE
; 
4493     m_colLabelHorizAlign 
= wxALIGN_CENTRE
; 
4494     m_colLabelVertAlign  
= wxALIGN_CENTRE
; 
4495     m_colLabelTextOrientation 
= wxHORIZONTAL
; 
4497     m_defaultColWidth  
= WXGRID_DEFAULT_COL_WIDTH
; 
4498     m_defaultRowHeight 
= m_gridWin
->GetCharHeight(); 
4500     m_minAcceptableColWidth  
= WXGRID_MIN_COL_WIDTH
; 
4501     m_minAcceptableRowHeight 
= WXGRID_MIN_ROW_HEIGHT
; 
4503 #if defined(__WXMOTIF__) || defined(__WXGTK__)  // see also text ctrl sizing in ShowCellEditControl() 
4504     m_defaultRowHeight 
+= 8; 
4506     m_defaultRowHeight 
+= 4; 
4509     m_gridLineColour 
= wxColour( 192,192,192 ); 
4510     m_gridLinesEnabled 
= true; 
4511     m_cellHighlightColour 
= *wxBLACK
; 
4512     m_cellHighlightPenWidth 
= 2; 
4513     m_cellHighlightROPenWidth 
= 1; 
4515     m_canDragColMove 
= false; 
4517     m_cursorMode  
= WXGRID_CURSOR_SELECT_CELL
; 
4518     m_winCapture 
= (wxWindow 
*)NULL
; 
4519     m_canDragRowSize 
= true; 
4520     m_canDragColSize 
= true; 
4521     m_canDragGridSize 
= true; 
4522     m_canDragCell 
= false; 
4524     m_dragRowOrCol 
= -1; 
4525     m_isDragging 
= false; 
4526     m_startDragPos 
= wxDefaultPosition
; 
4527     m_nativeColumnLabels 
= false; 
4529     m_waitForSlowClick 
= false; 
4531     m_rowResizeCursor 
= wxCursor( wxCURSOR_SIZENS 
); 
4532     m_colResizeCursor 
= wxCursor( wxCURSOR_SIZEWE 
); 
4534     m_currentCellCoords 
= wxGridNoCellCoords
; 
4538     m_selectionBackground 
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
); 
4539     m_selectionForeground 
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
); 
4541     m_editable 
= true;  // default for whole grid 
4543     m_inOnKeyDown 
= false; 
4549     m_scrollLineX 
= GRID_SCROLL_LINE_X
; 
4550     m_scrollLineY 
= GRID_SCROLL_LINE_Y
; 
4553 // ---------------------------------------------------------------------------- 
4554 // the idea is to call these functions only when necessary because they create 
4555 // quite big arrays which eat memory mostly unnecessary - in particular, if 
4556 // default widths/heights are used for all rows/columns, we may not use these 
4559 // with some extra code, it should be possible to only store the widths/heights 
4560 // different from default ones (resulting in space savings for huge grids) but 
4561 // this is not done currently 
4562 // ---------------------------------------------------------------------------- 
4564 void wxGrid::InitRowHeights() 
4566     m_rowHeights
.Empty(); 
4567     m_rowBottoms
.Empty(); 
4569     m_rowHeights
.Alloc( m_numRows 
); 
4570     m_rowBottoms
.Alloc( m_numRows 
); 
4572     m_rowHeights
.Add( m_defaultRowHeight
, m_numRows 
); 
4575     for ( int i 
= 0; i 
< m_numRows
; i
++ ) 
4577         rowBottom 
+= m_defaultRowHeight
; 
4578         m_rowBottoms
.Add( rowBottom 
); 
4582 void wxGrid::InitColWidths() 
4584     m_colWidths
.Empty(); 
4585     m_colRights
.Empty(); 
4587     m_colWidths
.Alloc( m_numCols 
); 
4588     m_colRights
.Alloc( m_numCols 
); 
4590     m_colWidths
.Add( m_defaultColWidth
, m_numCols 
); 
4593     for ( int i 
= 0; i 
< m_numCols
; i
++ ) 
4595         colRight 
= ( GetColPos( i 
) + 1 ) * m_defaultColWidth
; 
4596         m_colRights
.Add( colRight 
); 
4600 int wxGrid::GetColWidth(int col
) const 
4602     return m_colWidths
.IsEmpty() ? m_defaultColWidth 
: m_colWidths
[col
]; 
4605 int wxGrid::GetColLeft(int col
) const 
4607     return m_colRights
.IsEmpty() ? GetColPos( col 
) * m_defaultColWidth
 
4608                                  : m_colRights
[col
] - m_colWidths
[col
]; 
4611 int wxGrid::GetColRight(int col
) const 
4613     return m_colRights
.IsEmpty() ? (GetColPos( col 
) + 1) * m_defaultColWidth
 
4617 int wxGrid::GetRowHeight(int row
) const 
4619     return m_rowHeights
.IsEmpty() ? m_defaultRowHeight 
: m_rowHeights
[row
]; 
4622 int wxGrid::GetRowTop(int row
) const 
4624     return m_rowBottoms
.IsEmpty() ? row 
* m_defaultRowHeight
 
4625                                   : m_rowBottoms
[row
] - m_rowHeights
[row
]; 
4628 int wxGrid::GetRowBottom(int row
) const 
4630     return m_rowBottoms
.IsEmpty() ? (row 
+ 1) * m_defaultRowHeight
 
4631                                   : m_rowBottoms
[row
]; 
4634 void wxGrid::CalcDimensions() 
4636     // compute the size of the scrollable area 
4637     int w 
= m_numCols 
> 0 ? GetColRight(GetColAt(m_numCols 
- 1)) : 0; 
4638     int h 
= m_numRows 
> 0 ? GetRowBottom(m_numRows 
- 1) : 0; 
4643     // take into account editor if shown 
4644     if ( IsCellEditControlShown() ) 
4647         int r 
= m_currentCellCoords
.GetRow(); 
4648         int c 
= m_currentCellCoords
.GetCol(); 
4649         int x 
= GetColLeft(c
); 
4650         int y 
= GetRowTop(r
); 
4652         // how big is the editor 
4653         wxGridCellAttr
* attr 
= GetCellAttr(r
, c
); 
4654         wxGridCellEditor
* editor 
= attr
->GetEditor(this, r
, c
); 
4655         editor
->GetControl()->GetSize(&w2
, &h2
); 
4666     // preserve (more or less) the previous position 
4668     GetViewStart( &x
, &y 
); 
4670     // ensure the position is valid for the new scroll ranges 
4672         x 
= wxMax( w 
- 1, 0 ); 
4674         y 
= wxMax( h 
- 1, 0 ); 
4676     // update the virtual size and refresh the scrollbars to reflect it 
4677     m_gridWin
->SetVirtualSize(w
, h
); 
4681     // if our OnSize() hadn't been called (it would if we have scrollbars), we 
4682     // still must reposition the children 
4686 wxSize 
wxGrid::GetSizeAvailableForScrollTarget(const wxSize
& size
) 
4688     wxSize 
sizeGridWin(size
); 
4689     sizeGridWin
.x 
-= m_rowLabelWidth
; 
4690     sizeGridWin
.y 
-= m_colLabelHeight
; 
4695 void wxGrid::CalcWindowSizes() 
4697     // escape if the window is has not been fully created yet 
4699     if ( m_cornerLabelWin 
== NULL 
) 
4703     GetClientSize( &cw
, &ch 
); 
4705     // the grid may be too small to have enough space for the labels yet, don't 
4706     // size the windows to negative sizes in this case 
4707     int gw 
= cw 
- m_rowLabelWidth
; 
4708     int gh 
= ch 
- m_colLabelHeight
; 
4714     if ( m_cornerLabelWin 
&& m_cornerLabelWin
->IsShown() ) 
4715         m_cornerLabelWin
->SetSize( 0, 0, m_rowLabelWidth
, m_colLabelHeight 
); 
4717     if ( m_colLabelWin 
&& m_colLabelWin
->IsShown() ) 
4718         m_colLabelWin
->SetSize( m_rowLabelWidth
, 0, gw
, m_colLabelHeight 
); 
4720     if ( m_rowLabelWin 
&& m_rowLabelWin
->IsShown() ) 
4721         m_rowLabelWin
->SetSize( 0, m_colLabelHeight
, m_rowLabelWidth
, gh 
); 
4723     if ( m_gridWin 
&& m_gridWin
->IsShown() ) 
4724         m_gridWin
->SetSize( m_rowLabelWidth
, m_colLabelHeight
, gw
, gh 
); 
4727 // this is called when the grid table sends a message 
4728 // to indicate that it has been redimensioned 
4730 bool wxGrid::Redimension( wxGridTableMessage
& msg 
) 
4733     bool result 
= false; 
4735     // Clear the attribute cache as the attribute might refer to a different 
4736     // cell than stored in the cache after adding/removing rows/columns. 
4739     // By the same reasoning, the editor should be dismissed if columns are 
4740     // added or removed. And for consistency, it should IMHO always be 
4741     // removed, not only if the cell "underneath" it actually changes. 
4742     // For now, I intentionally do not save the editor's content as the 
4743     // cell it might want to save that stuff to might no longer exist. 
4744     HideCellEditControl(); 
4747     // if we were using the default widths/heights so far, we must change them 
4749     if ( m_colWidths
.IsEmpty() ) 
4754     if ( m_rowHeights
.IsEmpty() ) 
4760     switch ( msg
.GetId() ) 
4762         case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
: 
4764             size_t pos 
= msg
.GetCommandInt(); 
4765             int numRows 
= msg
.GetCommandInt2(); 
4767             m_numRows 
+= numRows
; 
4769             if ( !m_rowHeights
.IsEmpty() ) 
4771                 m_rowHeights
.Insert( m_defaultRowHeight
, pos
, numRows 
); 
4772                 m_rowBottoms
.Insert( 0, pos
, numRows 
); 
4776                     bottom 
= m_rowBottoms
[pos 
- 1]; 
4778                 for ( i 
= pos
; i 
< m_numRows
; i
++ ) 
4780                     bottom 
+= m_rowHeights
[i
]; 
4781                     m_rowBottoms
[i
] = bottom
; 
4785             if ( m_currentCellCoords 
== wxGridNoCellCoords 
) 
4787                 // if we have just inserted cols into an empty grid the current 
4788                 // cell will be undefined... 
4790                 SetCurrentCell( 0, 0 ); 
4794                 m_selection
->UpdateRows( pos
, numRows 
); 
4795             wxGridCellAttrProvider 
* attrProvider 
= m_table
->GetAttrProvider(); 
4797                 attrProvider
->UpdateAttrRows( pos
, numRows 
); 
4799             if ( !GetBatchCount() ) 
4802                 m_rowLabelWin
->Refresh(); 
4808         case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
: 
4810             int numRows 
= msg
.GetCommandInt(); 
4811             int oldNumRows 
= m_numRows
; 
4812             m_numRows 
+= numRows
; 
4814             if ( !m_rowHeights
.IsEmpty() ) 
4816                 m_rowHeights
.Add( m_defaultRowHeight
, numRows 
); 
4817                 m_rowBottoms
.Add( 0, numRows 
); 
4820                 if ( oldNumRows 
> 0 ) 
4821                     bottom 
= m_rowBottoms
[oldNumRows 
- 1]; 
4823                 for ( i 
= oldNumRows
; i 
< m_numRows
; i
++ ) 
4825                     bottom 
+= m_rowHeights
[i
]; 
4826                     m_rowBottoms
[i
] = bottom
; 
4830             if ( m_currentCellCoords 
== wxGridNoCellCoords 
) 
4832                 // if we have just inserted cols into an empty grid the current 
4833                 // cell will be undefined... 
4835                 SetCurrentCell( 0, 0 ); 
4838             if ( !GetBatchCount() ) 
4841                 m_rowLabelWin
->Refresh(); 
4847         case wxGRIDTABLE_NOTIFY_ROWS_DELETED
: 
4849             size_t pos 
= msg
.GetCommandInt(); 
4850             int numRows 
= msg
.GetCommandInt2(); 
4851             m_numRows 
-= numRows
; 
4853             if ( !m_rowHeights
.IsEmpty() ) 
4855                 m_rowHeights
.RemoveAt( pos
, numRows 
); 
4856                 m_rowBottoms
.RemoveAt( pos
, numRows 
); 
4859                 for ( i 
= 0; i 
< m_numRows
; i
++ ) 
4861                     h 
+= m_rowHeights
[i
]; 
4862                     m_rowBottoms
[i
] = h
; 
4868                 m_currentCellCoords 
= wxGridNoCellCoords
; 
4872                 if ( m_currentCellCoords
.GetRow() >= m_numRows 
) 
4873                     m_currentCellCoords
.Set( 0, 0 ); 
4877                 m_selection
->UpdateRows( pos
, -((int)numRows
) ); 
4878             wxGridCellAttrProvider 
* attrProvider 
= m_table
->GetAttrProvider(); 
4881                 attrProvider
->UpdateAttrRows( pos
, -((int)numRows
) ); 
4883 // ifdef'd out following patch from Paul Gammans 
4885                 // No need to touch column attributes, unless we 
4886                 // removed _all_ rows, in this case, we remove 
4887                 // all column attributes. 
4888                 // I hate to do this here, but the 
4889                 // needed data is not available inside UpdateAttrRows. 
4890                 if ( !GetNumberRows() ) 
4891                     attrProvider
->UpdateAttrCols( 0, -GetNumberCols() ); 
4895             if ( !GetBatchCount() ) 
4898                 m_rowLabelWin
->Refresh(); 
4904         case wxGRIDTABLE_NOTIFY_COLS_INSERTED
: 
4906             size_t pos 
= msg
.GetCommandInt(); 
4907             int numCols 
= msg
.GetCommandInt2(); 
4908             m_numCols 
+= numCols
; 
4910             if ( !m_colAt
.IsEmpty() ) 
4912                 //Shift the column IDs 
4914                 for ( i 
= 0; i 
< m_numCols 
- numCols
; i
++ ) 
4916                     if ( m_colAt
[i
] >= (int)pos 
) 
4917                         m_colAt
[i
] += numCols
; 
4920                 m_colAt
.Insert( pos
, pos
, numCols 
); 
4922                 //Set the new columns' positions 
4923                 for ( i 
= pos 
+ 1; i 
< (int)pos 
+ numCols
; i
++ ) 
4929             if ( !m_colWidths
.IsEmpty() ) 
4931                 m_colWidths
.Insert( m_defaultColWidth
, pos
, numCols 
); 
4932                 m_colRights
.Insert( 0, pos
, numCols 
); 
4936                     right 
= m_colRights
[GetColAt( pos 
- 1 )]; 
4939                 for ( colPos 
= pos
; colPos 
< m_numCols
; colPos
++ ) 
4941                     i 
= GetColAt( colPos 
); 
4943                     right 
+= m_colWidths
[i
]; 
4944                     m_colRights
[i
] = right
; 
4948             if ( m_currentCellCoords 
== wxGridNoCellCoords 
) 
4950                 // if we have just inserted cols into an empty grid the current 
4951                 // cell will be undefined... 
4953                 SetCurrentCell( 0, 0 ); 
4957                 m_selection
->UpdateCols( pos
, numCols 
); 
4958             wxGridCellAttrProvider 
* attrProvider 
= m_table
->GetAttrProvider(); 
4960                 attrProvider
->UpdateAttrCols( pos
, numCols 
); 
4961             if ( !GetBatchCount() ) 
4964                 m_colLabelWin
->Refresh(); 
4970         case wxGRIDTABLE_NOTIFY_COLS_APPENDED
: 
4972             int numCols 
= msg
.GetCommandInt(); 
4973             int oldNumCols 
= m_numCols
; 
4974             m_numCols 
+= numCols
; 
4976             if ( !m_colAt
.IsEmpty() ) 
4978                 m_colAt
.Add( 0, numCols 
); 
4980                 //Set the new columns' positions 
4982                 for ( i 
= oldNumCols
; i 
< m_numCols
; i
++ ) 
4988             if ( !m_colWidths
.IsEmpty() ) 
4990                 m_colWidths
.Add( m_defaultColWidth
, numCols 
); 
4991                 m_colRights
.Add( 0, numCols 
); 
4994                 if ( oldNumCols 
> 0 ) 
4995                     right 
= m_colRights
[GetColAt( oldNumCols 
- 1 )]; 
4998                 for ( colPos 
= oldNumCols
; colPos 
< m_numCols
; colPos
++ ) 
5000                     i 
= GetColAt( colPos 
); 
5002                     right 
+= m_colWidths
[i
]; 
5003                     m_colRights
[i
] = right
; 
5007             if ( m_currentCellCoords 
== wxGridNoCellCoords 
) 
5009                 // if we have just inserted cols into an empty grid the current 
5010                 // cell will be undefined... 
5012                 SetCurrentCell( 0, 0 ); 
5014             if ( !GetBatchCount() ) 
5017                 m_colLabelWin
->Refresh(); 
5023         case wxGRIDTABLE_NOTIFY_COLS_DELETED
: 
5025             size_t pos 
= msg
.GetCommandInt(); 
5026             int numCols 
= msg
.GetCommandInt2(); 
5027             m_numCols 
-= numCols
; 
5029             if ( !m_colAt
.IsEmpty() ) 
5031                 int colID 
= GetColAt( pos 
); 
5033                 m_colAt
.RemoveAt( pos
, numCols 
); 
5035                 //Shift the column IDs 
5037                 for ( colPos 
= 0; colPos 
< m_numCols
; colPos
++ ) 
5039                     if ( m_colAt
[colPos
] > colID 
) 
5040                         m_colAt
[colPos
] -= numCols
; 
5044             if ( !m_colWidths
.IsEmpty() ) 
5046                 m_colWidths
.RemoveAt( pos
, numCols 
); 
5047                 m_colRights
.RemoveAt( pos
, numCols 
); 
5051                 for ( colPos 
= 0; colPos 
< m_numCols
; colPos
++ ) 
5053                     i 
= GetColAt( colPos 
); 
5055                     w 
+= m_colWidths
[i
]; 
5062                 m_currentCellCoords 
= wxGridNoCellCoords
; 
5066                 if ( m_currentCellCoords
.GetCol() >= m_numCols 
) 
5067                   m_currentCellCoords
.Set( 0, 0 ); 
5071                 m_selection
->UpdateCols( pos
, -((int)numCols
) ); 
5072             wxGridCellAttrProvider 
* attrProvider 
= m_table
->GetAttrProvider(); 
5075                 attrProvider
->UpdateAttrCols( pos
, -((int)numCols
) ); 
5077 // ifdef'd out following patch from Paul Gammans 
5079                 // No need to touch row attributes, unless we 
5080                 // removed _all_ columns, in this case, we remove 
5081                 // all row attributes. 
5082                 // I hate to do this here, but the 
5083                 // needed data is not available inside UpdateAttrCols. 
5084                 if ( !GetNumberCols() ) 
5085                     attrProvider
->UpdateAttrRows( 0, -GetNumberRows() ); 
5089             if ( !GetBatchCount() ) 
5092                 m_colLabelWin
->Refresh(); 
5099     if (result 
&& !GetBatchCount() ) 
5100         m_gridWin
->Refresh(); 
5105 wxArrayInt 
wxGrid::CalcRowLabelsExposed( const wxRegion
& reg 
) const 
5107     wxRegionIterator 
iter( reg 
); 
5110     wxArrayInt  rowlabels
; 
5117         // TODO: remove this when we can... 
5118         // There is a bug in wxMotif that gives garbage update 
5119         // rectangles if you jump-scroll a long way by clicking the 
5120         // scrollbar with middle button.  This is a work-around 
5122 #if defined(__WXMOTIF__) 
5124         m_gridWin
->GetClientSize( &cw
, &ch 
); 
5125         if ( r
.GetTop() > ch 
) 
5127         r
.SetBottom( wxMin( r
.GetBottom(), ch 
) ); 
5130         // logical bounds of update region 
5133         CalcUnscrolledPosition( 0, r
.GetTop(), &dummy
, &top 
); 
5134         CalcUnscrolledPosition( 0, r
.GetBottom(), &dummy
, &bottom 
); 
5136         // find the row labels within these bounds 
5139         for ( row 
= internalYToRow(top
); row 
< m_numRows
; row
++ ) 
5141             if ( GetRowBottom(row
) < top 
) 
5144             if ( GetRowTop(row
) > bottom 
) 
5147             rowlabels
.Add( row 
); 
5156 wxArrayInt 
wxGrid::CalcColLabelsExposed( const wxRegion
& reg 
) const 
5158     wxRegionIterator 
iter( reg 
); 
5161     wxArrayInt colLabels
; 
5168         // TODO: remove this when we can... 
5169         // There is a bug in wxMotif that gives garbage update 
5170         // rectangles if you jump-scroll a long way by clicking the 
5171         // scrollbar with middle button.  This is a work-around 
5173 #if defined(__WXMOTIF__) 
5175         m_gridWin
->GetClientSize( &cw
, &ch 
); 
5176         if ( r
.GetLeft() > cw 
) 
5178         r
.SetRight( wxMin( r
.GetRight(), cw 
) ); 
5181         // logical bounds of update region 
5184         CalcUnscrolledPosition( r
.GetLeft(), 0, &left
, &dummy 
); 
5185         CalcUnscrolledPosition( r
.GetRight(), 0, &right
, &dummy 
); 
5187         // find the cells within these bounds 
5191         for ( colPos 
= GetColPos( internalXToCol(left
) ); colPos 
< m_numCols
; colPos
++ ) 
5193             col 
= GetColAt( colPos 
); 
5195             if ( GetColRight(col
) < left 
) 
5198             if ( GetColLeft(col
) > right 
) 
5201             colLabels
.Add( col 
); 
5210 wxGridCellCoordsArray 
wxGrid::CalcCellsExposed( const wxRegion
& reg 
) const 
5212     wxRegionIterator 
iter( reg 
); 
5215     wxGridCellCoordsArray  cellsExposed
; 
5217     int left
, top
, right
, bottom
; 
5222         // TODO: remove this when we can... 
5223         // There is a bug in wxMotif that gives garbage update 
5224         // rectangles if you jump-scroll a long way by clicking the 
5225         // scrollbar with middle button.  This is a work-around 
5227 #if defined(__WXMOTIF__) 
5229         m_gridWin
->GetClientSize( &cw
, &ch 
); 
5230         if ( r
.GetTop() > ch 
) r
.SetTop( 0 ); 
5231         if ( r
.GetLeft() > cw 
) r
.SetLeft( 0 ); 
5232         r
.SetRight( wxMin( r
.GetRight(), cw 
) ); 
5233         r
.SetBottom( wxMin( r
.GetBottom(), ch 
) ); 
5236         // logical bounds of update region 
5238         CalcUnscrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top 
); 
5239         CalcUnscrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom 
); 
5241         // find the cells within these bounds 
5244         for ( row 
= internalYToRow(top
); row 
< m_numRows
; row
++ ) 
5246             if ( GetRowBottom(row
) <= top 
) 
5249             if ( GetRowTop(row
) > bottom 
) 
5253             for ( colPos 
= GetColPos( internalXToCol(left
) ); colPos 
< m_numCols
; colPos
++ ) 
5255                 col 
= GetColAt( colPos 
); 
5257                 if ( GetColRight(col
) <= left 
) 
5260                 if ( GetColLeft(col
) > right 
) 
5263                 cellsExposed
.Add( wxGridCellCoords( row
, col 
) ); 
5270     return cellsExposed
; 
5274 void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent
& event 
) 
5277     wxPoint 
pos( event
.GetPosition() ); 
5278     CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y 
); 
5280     if ( event
.Dragging() ) 
5284             m_isDragging 
= true; 
5285             m_rowLabelWin
->CaptureMouse(); 
5288         if ( event
.LeftIsDown() ) 
5290             switch ( m_cursorMode 
) 
5292                 case WXGRID_CURSOR_RESIZE_ROW
: 
5294                     int cw
, ch
, left
, dummy
; 
5295                     m_gridWin
->GetClientSize( &cw
, &ch 
); 
5296                     CalcUnscrolledPosition( 0, 0, &left
, &dummy 
); 
5298                     wxClientDC 
dc( m_gridWin 
); 
5301                                GetRowTop(m_dragRowOrCol
) + 
5302                                GetRowMinimalHeight(m_dragRowOrCol
) ); 
5303                     dc
.SetLogicalFunction(wxINVERT
); 
5304                     if ( m_dragLastPos 
>= 0 ) 
5306                         dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos 
); 
5308                     dc
.DrawLine( left
, y
, left
+cw
, y 
); 
5313                 case WXGRID_CURSOR_SELECT_ROW
: 
5315                     if ( (row 
= YToRow( y 
)) >= 0 ) 
5319                             m_selection
->SelectRow( row
, 
5320                                                     event
.ControlDown(), 
5329                 // default label to suppress warnings about "enumeration value 
5330                 // 'xxx' not handled in switch 
5338     if ( m_isDragging 
&& (event
.Entering() || event
.Leaving()) ) 
5343         if (m_rowLabelWin
->HasCapture()) 
5344             m_rowLabelWin
->ReleaseMouse(); 
5345         m_isDragging 
= false; 
5348     // ------------ Entering or leaving the window 
5350     if ( event
.Entering() || event
.Leaving() ) 
5352         ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
); 
5355     // ------------ Left button pressed 
5357     else if ( event
.LeftDown() ) 
5359         // don't send a label click event for a hit on the 
5360         // edge of the row label - this is probably the user 
5361         // wanting to resize the row 
5363         if ( YToEdgeOfRow(y
) < 0 ) 
5367                  !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, row
, -1, event 
) ) 
5369                 if ( !event
.ShiftDown() && !event
.CmdDown() ) 
5373                     if ( event
.ShiftDown() ) 
5375                         m_selection
->SelectBlock( m_currentCellCoords
.GetRow(), 
5378                                                   GetNumberCols() - 1, 
5379                                                   event
.ControlDown(), 
5386                         m_selection
->SelectRow( row
, 
5387                                                 event
.ControlDown(), 
5394                 ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW
, m_rowLabelWin
); 
5399             // starting to drag-resize a row 
5400             if ( CanDragRowSize() ) 
5401                 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
); 
5405     // ------------ Left double click 
5407     else if (event
.LeftDClick() ) 
5409         row 
= YToEdgeOfRow(y
); 
5414                  !SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, row
, -1, event 
) ) 
5416                 // no default action at the moment 
5421             // adjust row height depending on label text 
5422             AutoSizeRowLabelSize( row 
); 
5424             ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
); 
5429     // ------------ Left button released 
5431     else if ( event
.LeftUp() ) 
5433         if ( m_cursorMode 
== WXGRID_CURSOR_RESIZE_ROW 
) 
5435             DoEndDragResizeRow(); 
5437             // Note: we are ending the event *after* doing 
5438             // default processing in this case 
5440             SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event 
); 
5443         ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
); 
5447     // ------------ Right button down 
5449     else if ( event
.RightDown() ) 
5453              !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, row
, -1, event 
) ) 
5455             // no default action at the moment 
5459     // ------------ Right double click 
5461     else if ( event
.RightDClick() ) 
5465              !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, row
, -1, event 
) ) 
5467             // no default action at the moment 
5471     // ------------ No buttons down and mouse moving 
5473     else if ( event
.Moving() ) 
5475         m_dragRowOrCol 
= YToEdgeOfRow( y 
); 
5476         if ( m_dragRowOrCol 
>= 0 ) 
5478             if ( m_cursorMode 
== WXGRID_CURSOR_SELECT_CELL 
) 
5480                 // don't capture the mouse yet 
5481                 if ( CanDragRowSize() ) 
5482                     ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
, false); 
5485         else if ( m_cursorMode 
!= WXGRID_CURSOR_SELECT_CELL 
) 
5487             ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
, false); 
5492 void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent
& event 
) 
5495     wxPoint 
pos( event
.GetPosition() ); 
5496     CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y 
); 
5498     if ( event
.Dragging() ) 
5502             m_isDragging 
= true; 
5503             m_colLabelWin
->CaptureMouse(); 
5505             if ( m_cursorMode 
== WXGRID_CURSOR_MOVE_COL 
) 
5506                 m_dragRowOrCol 
= XToCol( x 
); 
5509         if ( event
.LeftIsDown() ) 
5511             switch ( m_cursorMode 
) 
5513                 case WXGRID_CURSOR_RESIZE_COL
: 
5515                     int cw
, ch
, dummy
, top
; 
5516                     m_gridWin
->GetClientSize( &cw
, &ch 
); 
5517                     CalcUnscrolledPosition( 0, 0, &dummy
, &top 
); 
5519                     wxClientDC 
dc( m_gridWin 
); 
5522                     x 
= wxMax( x
, GetColLeft(m_dragRowOrCol
) + 
5523                                   GetColMinimalWidth(m_dragRowOrCol
)); 
5524                     dc
.SetLogicalFunction(wxINVERT
); 
5525                     if ( m_dragLastPos 
>= 0 ) 
5527                         dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top 
+ ch 
); 
5529                     dc
.DrawLine( x
, top
, x
, top 
+ ch 
); 
5534                 case WXGRID_CURSOR_SELECT_COL
: 
5536                     if ( (col 
= XToCol( x 
)) >= 0 ) 
5540                             m_selection
->SelectCol( col
, 
5541                                                     event
.ControlDown(), 
5550                 case WXGRID_CURSOR_MOVE_COL
: 
5553                         m_moveToCol 
= GetColAt( 0 ); 
5555                         m_moveToCol 
= XToCol( x 
); 
5559                     if ( m_moveToCol 
< 0 ) 
5560                         markerX 
= GetColRight( GetColAt( m_numCols 
- 1 ) ); 
5561                     else if ( x 
>= (GetColLeft( m_moveToCol 
) + (GetColWidth(m_moveToCol
) / 2)) ) 
5563                         m_moveToCol 
= GetColAt( GetColPos( m_moveToCol 
) + 1 ); 
5564                         if ( m_moveToCol 
< 0 ) 
5565                             markerX 
= GetColRight( GetColAt( m_numCols 
- 1 ) ); 
5567                             markerX 
= GetColLeft( m_moveToCol 
); 
5570                         markerX 
= GetColLeft( m_moveToCol 
); 
5572                     if ( markerX 
!= m_dragLastPos 
) 
5574                         wxClientDC 
dc( m_colLabelWin 
); 
5578                         m_colLabelWin
->GetClientSize( &cw
, &ch 
); 
5582                         //Clean up the last indicator 
5583                         if ( m_dragLastPos 
>= 0 ) 
5585                             wxPen 
pen( m_colLabelWin
->GetBackgroundColour(), 2 ); 
5587                             dc
.DrawLine( m_dragLastPos 
+ 1, 0, m_dragLastPos 
+ 1, ch 
); 
5588                             dc
.SetPen(wxNullPen
); 
5590                             if ( XToCol( m_dragLastPos 
) != -1 ) 
5591                                 DrawColLabel( dc
, XToCol( m_dragLastPos 
) ); 
5594                         const wxColour 
*color
; 
5595                         //Moving to the same place? Don't draw a marker 
5596                         if ( (m_moveToCol 
== m_dragRowOrCol
) 
5597                           || (GetColPos( m_moveToCol 
) == GetColPos( m_dragRowOrCol 
) + 1) 
5598                           || (m_moveToCol 
< 0 && m_dragRowOrCol 
== GetColAt( m_numCols 
- 1 ))) 
5599                             color 
= wxLIGHT_GREY
; 
5604                         wxPen 
pen( *color
, 2 ); 
5607                         dc
.DrawLine( markerX
, 0, markerX
, ch 
); 
5609                         dc
.SetPen(wxNullPen
); 
5611                         m_dragLastPos 
= markerX 
- 1; 
5616                 // default label to suppress warnings about "enumeration value 
5617                 // 'xxx' not handled in switch 
5625     if ( m_isDragging 
&& (event
.Entering() || event
.Leaving()) ) 
5630         if (m_colLabelWin
->HasCapture()) 
5631             m_colLabelWin
->ReleaseMouse(); 
5632         m_isDragging 
= false; 
5635     // ------------ Entering or leaving the window 
5637     if ( event
.Entering() || event
.Leaving() ) 
5639         ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
); 
5642     // ------------ Left button pressed 
5644     else if ( event
.LeftDown() ) 
5646         // don't send a label click event for a hit on the 
5647         // edge of the col label - this is probably the user 
5648         // wanting to resize the col 
5650         if ( XToEdgeOfCol(x
) < 0 ) 
5654                  !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, col
, event 
) ) 
5656                 if ( m_canDragColMove 
) 
5658                     //Show button as pressed 
5659                     wxClientDC 
dc( m_colLabelWin 
); 
5660                     int colLeft 
= GetColLeft( col 
); 
5661                     int colRight 
= GetColRight( col 
) - 1; 
5662                     dc
.SetPen( wxPen( m_colLabelWin
->GetBackgroundColour(), 1 ) ); 
5663                     dc
.DrawLine( colLeft
, 1, colLeft
, m_colLabelHeight
-1 ); 
5664                     dc
.DrawLine( colLeft
, 1, colRight
, 1 ); 
5666                     ChangeCursorMode(WXGRID_CURSOR_MOVE_COL
, m_colLabelWin
); 
5670                     if ( !event
.ShiftDown() && !event
.CmdDown() ) 
5674                         if ( event
.ShiftDown() ) 
5676                             m_selection
->SelectBlock( 0, 
5677                                                       m_currentCellCoords
.GetCol(), 
5678                                                       GetNumberRows() - 1, col
, 
5679                                                       event
.ControlDown(), 
5686                             m_selection
->SelectCol( col
, 
5687                                                     event
.ControlDown(), 
5694                     ChangeCursorMode(WXGRID_CURSOR_SELECT_COL
, m_colLabelWin
); 
5700             // starting to drag-resize a col 
5702             if ( CanDragColSize() ) 
5703                 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
); 
5707     // ------------ Left double click 
5709     if ( event
.LeftDClick() ) 
5711         col 
= XToEdgeOfCol(x
); 
5716                  ! SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, col
, event 
) ) 
5718                 // no default action at the moment 
5723             // adjust column width depending on label text 
5724             AutoSizeColLabelSize( col 
); 
5726             ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
); 
5731     // ------------ Left button released 
5733     else if ( event
.LeftUp() ) 
5735         switch ( m_cursorMode 
) 
5737             case WXGRID_CURSOR_RESIZE_COL
: 
5738                 DoEndDragResizeCol(); 
5740                 // Note: we are ending the event *after* doing 
5741                 // default processing in this case 
5743                 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event 
); 
5746             case WXGRID_CURSOR_MOVE_COL
: 
5749                 SendEvent( wxEVT_GRID_COL_MOVE
, -1, m_dragRowOrCol
, event 
); 
5752             case WXGRID_CURSOR_SELECT_COL
: 
5753             case WXGRID_CURSOR_SELECT_CELL
: 
5754             case WXGRID_CURSOR_RESIZE_ROW
: 
5755             case WXGRID_CURSOR_SELECT_ROW
: 
5756                 // nothing to do (?) 
5760         ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
); 
5764     // ------------ Right button down 
5766     else if ( event
.RightDown() ) 
5770              !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, col
, event 
) ) 
5772             // no default action at the moment 
5776     // ------------ Right double click 
5778     else if ( event
.RightDClick() ) 
5782              !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, col
, event 
) ) 
5784             // no default action at the moment 
5788     // ------------ No buttons down and mouse moving 
5790     else if ( event
.Moving() ) 
5792         m_dragRowOrCol 
= XToEdgeOfCol( x 
); 
5793         if ( m_dragRowOrCol 
>= 0 ) 
5795             if ( m_cursorMode 
== WXGRID_CURSOR_SELECT_CELL 
) 
5797                 // don't capture the cursor yet 
5798                 if ( CanDragColSize() ) 
5799                     ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
, false); 
5802         else if ( m_cursorMode 
!= WXGRID_CURSOR_SELECT_CELL 
) 
5804             ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
, false); 
5809 void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent
& event 
) 
5811     if ( event
.LeftDown() ) 
5813         // indicate corner label by having both row and 
5816         if ( !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, -1, event 
) ) 
5821     else if ( event
.LeftDClick() ) 
5823         SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, -1, event 
); 
5825     else if ( event
.RightDown() ) 
5827         if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, -1, event 
) ) 
5829             // no default action at the moment 
5832     else if ( event
.RightDClick() ) 
5834         if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, -1, event 
) ) 
5836             // no default action at the moment 
5841 void wxGrid::CancelMouseCapture() 
5843     // cancel operation currently in progress, whatever it is 
5846         m_isDragging 
= false; 
5847         m_cursorMode 
= WXGRID_CURSOR_SELECT_CELL
; 
5848         m_winCapture
->SetCursor( *wxSTANDARD_CURSOR 
); 
5849         m_winCapture 
= NULL
; 
5851         // remove traces of whatever we drew on screen 
5856 void wxGrid::ChangeCursorMode(CursorMode mode
, 
5861     static const wxChar 
*cursorModes
[] = 
5871     wxLogTrace(_T("grid"), 
5872                _T("wxGrid cursor mode (mouse capture for %s): %s -> %s"), 
5873                win 
== m_colLabelWin 
? _T("colLabelWin") 
5874                                     : win 
? _T("rowLabelWin") 
5876                cursorModes
[m_cursorMode
], cursorModes
[mode
]); 
5879     if ( mode 
== m_cursorMode 
&& 
5880          win 
== m_winCapture 
&& 
5881          captureMouse 
== (m_winCapture 
!= NULL
)) 
5886         // by default use the grid itself 
5892         if (m_winCapture
->HasCapture()) 
5893             m_winCapture
->ReleaseMouse(); 
5894         m_winCapture 
= (wxWindow 
*)NULL
; 
5897     m_cursorMode 
= mode
; 
5899     switch ( m_cursorMode 
) 
5901         case WXGRID_CURSOR_RESIZE_ROW
: 
5902             win
->SetCursor( m_rowResizeCursor 
); 
5905         case WXGRID_CURSOR_RESIZE_COL
: 
5906             win
->SetCursor( m_colResizeCursor 
); 
5909         case WXGRID_CURSOR_MOVE_COL
: 
5910             win
->SetCursor( wxCursor(wxCURSOR_HAND
) ); 
5914             win
->SetCursor( *wxSTANDARD_CURSOR 
); 
5918     // we need to capture mouse when resizing 
5919     bool resize 
= m_cursorMode 
== WXGRID_CURSOR_RESIZE_ROW 
|| 
5920                   m_cursorMode 
== WXGRID_CURSOR_RESIZE_COL
; 
5922     if ( captureMouse 
&& resize 
) 
5924         win
->CaptureMouse(); 
5929 void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent
& event 
) 
5932     wxPoint 
pos( event
.GetPosition() ); 
5933     CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y 
); 
5935     wxGridCellCoords coords
; 
5936     XYToCell( x
, y
, coords 
); 
5938     int cell_rows
, cell_cols
; 
5939     bool isFirstDrag 
= !m_isDragging
; 
5940     GetCellSize( coords
.GetRow(), coords
.GetCol(), &cell_rows
, &cell_cols 
); 
5941     if ((cell_rows 
< 0) || (cell_cols 
< 0)) 
5943         coords
.SetRow(coords
.GetRow() + cell_rows
); 
5944         coords
.SetCol(coords
.GetCol() + cell_cols
); 
5947     if ( event
.Dragging() ) 
5949         //wxLogDebug("pos(%d, %d) coords(%d, %d)", pos.x, pos.y, coords.GetRow(), coords.GetCol()); 
5951         // Don't start doing anything until the mouse has been dragged at 
5952         // least 3 pixels in any direction... 
5955             if (m_startDragPos 
== wxDefaultPosition
) 
5957                 m_startDragPos 
= pos
; 
5960             if (abs(m_startDragPos
.x 
- pos
.x
) < 4 && abs(m_startDragPos
.y 
- pos
.y
) < 4) 
5964         m_isDragging 
= true; 
5965         if ( m_cursorMode 
== WXGRID_CURSOR_SELECT_CELL 
) 
5967             // Hide the edit control, so it 
5968             // won't interfere with drag-shrinking. 
5969             if ( IsCellEditControlShown() ) 
5971                 HideCellEditControl(); 
5972                 SaveEditControlValue(); 
5975             if ( coords 
!= wxGridNoCellCoords 
) 
5977                 if ( event
.CmdDown() ) 
5979                     if ( m_selectingKeyboard 
== wxGridNoCellCoords
) 
5980                         m_selectingKeyboard 
= coords
; 
5981                     HighlightBlock( m_selectingKeyboard
, coords 
); 
5983                 else if ( CanDragCell() ) 
5987                         if ( m_selectingKeyboard 
== wxGridNoCellCoords
) 
5988                             m_selectingKeyboard 
= coords
; 
5990                         SendEvent( wxEVT_GRID_CELL_BEGIN_DRAG
, 
5999                     if ( !IsSelection() ) 
6001                         HighlightBlock( coords
, coords 
); 
6005                         HighlightBlock( m_currentCellCoords
, coords 
); 
6009                 if (! IsVisible(coords
)) 
6011                     MakeCellVisible(coords
); 
6012                     // TODO: need to introduce a delay or something here.  The 
6013                     // scrolling is way to fast, at least on MSW - also on GTK. 
6016             // Have we captured the mouse yet? 
6019                 m_winCapture 
= m_gridWin
; 
6020                 m_winCapture
->CaptureMouse(); 
6025         else if ( event
.LeftIsDown() && 
6026                   m_cursorMode 
== WXGRID_CURSOR_RESIZE_ROW 
) 
6028             int cw
, ch
, left
, dummy
; 
6029             m_gridWin
->GetClientSize( &cw
, &ch 
); 
6030             CalcUnscrolledPosition( 0, 0, &left
, &dummy 
); 
6032             wxClientDC 
dc( m_gridWin 
); 
6034             y 
= wxMax( y
, GetRowTop(m_dragRowOrCol
) + 
6035                           GetRowMinimalHeight(m_dragRowOrCol
) ); 
6036             dc
.SetLogicalFunction(wxINVERT
); 
6037             if ( m_dragLastPos 
>= 0 ) 
6039                 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos 
); 
6041             dc
.DrawLine( left
, y
, left
+cw
, y 
); 
6044         else if ( event
.LeftIsDown() && 
6045                   m_cursorMode 
== WXGRID_CURSOR_RESIZE_COL 
) 
6047             int cw
, ch
, dummy
, top
; 
6048             m_gridWin
->GetClientSize( &cw
, &ch 
); 
6049             CalcUnscrolledPosition( 0, 0, &dummy
, &top 
); 
6051             wxClientDC 
dc( m_gridWin 
); 
6053             x 
= wxMax( x
, GetColLeft(m_dragRowOrCol
) + 
6054                           GetColMinimalWidth(m_dragRowOrCol
) ); 
6055             dc
.SetLogicalFunction(wxINVERT
); 
6056             if ( m_dragLastPos 
>= 0 ) 
6058                 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top 
+ ch 
); 
6060             dc
.DrawLine( x
, top
, x
, top 
+ ch 
); 
6067     m_isDragging 
= false; 
6068     m_startDragPos 
= wxDefaultPosition
; 
6070     // VZ: if we do this, the mode is reset to WXGRID_CURSOR_SELECT_CELL 
6071     //     immediately after it becomes WXGRID_CURSOR_RESIZE_ROW/COL under 
6074     if ( event
.Entering() || event
.Leaving() ) 
6076         ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
); 
6077         m_gridWin
->SetCursor( *wxSTANDARD_CURSOR 
); 
6082     // ------------ Left button pressed 
6084     if ( event
.LeftDown() && coords 
!= wxGridNoCellCoords 
) 
6086         if ( !SendEvent( wxEVT_GRID_CELL_LEFT_CLICK
, 
6091             if ( !event
.CmdDown() ) 
6093             if ( event
.ShiftDown() ) 
6097                     m_selection
->SelectBlock( m_currentCellCoords
.GetRow(), 
6098                                               m_currentCellCoords
.GetCol(), 
6101                                               event
.ControlDown(), 
6107             else if ( XToEdgeOfCol(x
) < 0 && 
6108                       YToEdgeOfRow(y
) < 0 ) 
6110                 DisableCellEditControl(); 
6111                 MakeCellVisible( coords 
); 
6113                 if ( event
.CmdDown() ) 
6117                         m_selection
->ToggleCellSelection( coords
.GetRow(), 
6119                                                           event
.ControlDown(), 
6124                     m_selectingTopLeft 
= wxGridNoCellCoords
; 
6125                     m_selectingBottomRight 
= wxGridNoCellCoords
; 
6126                     m_selectingKeyboard 
= coords
; 
6130                     m_waitForSlowClick 
= m_currentCellCoords 
== coords 
&& 
6131                                                 coords 
!= wxGridNoCellCoords
; 
6132                     SetCurrentCell( coords 
); 
6138     // ------------ Left double click 
6140     else if ( event
.LeftDClick() && coords 
!= wxGridNoCellCoords 
) 
6142         DisableCellEditControl(); 
6144         if ( XToEdgeOfCol(x
) < 0 && YToEdgeOfRow(y
) < 0 ) 
6146             if ( !SendEvent( wxEVT_GRID_CELL_LEFT_DCLICK
, 
6151                 // we want double click to select a cell and start editing 
6152                 // (i.e. to behave in same way as sequence of two slow clicks): 
6153                 m_waitForSlowClick 
= true; 
6158     // ------------ Left button released 
6160     else if ( event
.LeftUp() ) 
6162         if ( m_cursorMode 
== WXGRID_CURSOR_SELECT_CELL 
) 
6166                 if (m_winCapture
->HasCapture()) 
6167                     m_winCapture
->ReleaseMouse(); 
6168                 m_winCapture 
= NULL
; 
6171             if ( coords 
== m_currentCellCoords 
&& m_waitForSlowClick 
&& CanEnableCellControl() ) 
6174                 EnableCellEditControl(); 
6176                 wxGridCellAttr 
*attr 
= GetCellAttr(coords
); 
6177                 wxGridCellEditor 
*editor 
= attr
->GetEditor(this, coords
.GetRow(), coords
.GetCol()); 
6178                 editor
->StartingClick(); 
6182                 m_waitForSlowClick 
= false; 
6184             else if ( m_selectingTopLeft 
!= wxGridNoCellCoords 
&& 
6185                  m_selectingBottomRight 
!= wxGridNoCellCoords 
) 
6189                     m_selection
->SelectBlock( m_selectingTopLeft
.GetRow(), 
6190                                               m_selectingTopLeft
.GetCol(), 
6191                                               m_selectingBottomRight
.GetRow(), 
6192                                               m_selectingBottomRight
.GetCol(), 
6193                                               event
.ControlDown(), 
6199                 m_selectingTopLeft 
= wxGridNoCellCoords
; 
6200                 m_selectingBottomRight 
= wxGridNoCellCoords
; 
6202                 // Show the edit control, if it has been hidden for 
6204                 ShowCellEditControl(); 
6207         else if ( m_cursorMode 
== WXGRID_CURSOR_RESIZE_ROW 
) 
6209             ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
); 
6210             DoEndDragResizeRow(); 
6212             // Note: we are ending the event *after* doing 
6213             // default processing in this case 
6215             SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event 
); 
6217         else if ( m_cursorMode 
== WXGRID_CURSOR_RESIZE_COL 
) 
6219             ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
); 
6220             DoEndDragResizeCol(); 
6222             // Note: we are ending the event *after* doing 
6223             // default processing in this case 
6225             SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event 
); 
6231     // ------------ Right button down 
6233     else if ( event
.RightDown() && coords 
!= wxGridNoCellCoords 
) 
6235         DisableCellEditControl(); 
6236         if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_CLICK
, 
6241             // no default action at the moment 
6245     // ------------ Right double click 
6247     else if ( event
.RightDClick() && coords 
!= wxGridNoCellCoords 
) 
6249         DisableCellEditControl(); 
6250         if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_DCLICK
, 
6255             // no default action at the moment 
6259     // ------------ Moving and no button action 
6261     else if ( event
.Moving() && !event
.IsButton() ) 
6263         if ( coords
.GetRow() < 0 || coords
.GetCol() < 0 ) 
6265             // out of grid cell area 
6266             ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
); 
6270         int dragRow 
= YToEdgeOfRow( y 
); 
6271         int dragCol 
= XToEdgeOfCol( x 
); 
6273         // Dragging on the corner of a cell to resize in both 
6274         // directions is not implemented yet... 
6276         if ( dragRow 
>= 0 && dragCol 
>= 0 ) 
6278             ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
); 
6284             m_dragRowOrCol 
= dragRow
; 
6286             if ( m_cursorMode 
== WXGRID_CURSOR_SELECT_CELL 
) 
6288                 if ( CanDragRowSize() && CanDragGridSize() ) 
6289                     ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, NULL
, false); 
6292         else if ( dragCol 
>= 0 ) 
6294             m_dragRowOrCol 
= dragCol
; 
6296             if ( m_cursorMode 
== WXGRID_CURSOR_SELECT_CELL 
) 
6298                 if ( CanDragColSize() && CanDragGridSize() ) 
6299                     ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, NULL
, false); 
6302         else // Neither on a row or col edge 
6304             if ( m_cursorMode 
!= WXGRID_CURSOR_SELECT_CELL 
) 
6306                 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
); 
6312 void wxGrid::DoEndDragResizeRow() 
6314     if ( m_dragLastPos 
>= 0 ) 
6316         // erase the last line and resize the row 
6318         int cw
, ch
, left
, dummy
; 
6319         m_gridWin
->GetClientSize( &cw
, &ch 
); 
6320         CalcUnscrolledPosition( 0, 0, &left
, &dummy 
); 
6322         wxClientDC 
dc( m_gridWin 
); 
6324         dc
.SetLogicalFunction( wxINVERT 
); 
6325         dc
.DrawLine( left
, m_dragLastPos
, left 
+ cw
, m_dragLastPos 
); 
6326         HideCellEditControl(); 
6327         SaveEditControlValue(); 
6329         int rowTop 
= GetRowTop(m_dragRowOrCol
); 
6330         SetRowSize( m_dragRowOrCol
, 
6331                     wxMax( m_dragLastPos 
- rowTop
, m_minAcceptableRowHeight 
) ); 
6333         if ( !GetBatchCount() ) 
6335             // Only needed to get the correct rect.y: 
6336             wxRect 
rect ( CellToRect( m_dragRowOrCol
, 0 ) ); 
6338             CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
); 
6339             rect
.width 
= m_rowLabelWidth
; 
6340             rect
.height 
= ch 
- rect
.y
; 
6341             m_rowLabelWin
->Refresh( true, &rect 
); 
6344             // if there is a multicell block, paint all of it 
6347                 int i
, cell_rows
, cell_cols
, subtract_rows 
= 0; 
6348                 int leftCol 
= XToCol(left
); 
6349                 int rightCol 
= internalXToCol(left 
+ cw
); 
6352                     for (i
=leftCol
; i
<rightCol
; i
++) 
6354                         GetCellSize(m_dragRowOrCol
, i
, &cell_rows
, &cell_cols
); 
6355                         if (cell_rows 
< subtract_rows
) 
6356                             subtract_rows 
= cell_rows
; 
6358                     rect
.y 
= GetRowTop(m_dragRowOrCol 
+ subtract_rows
); 
6359                     CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
); 
6360                     rect
.height 
= ch 
- rect
.y
; 
6363             m_gridWin
->Refresh( false, &rect 
); 
6366         ShowCellEditControl(); 
6371 void wxGrid::DoEndDragResizeCol() 
6373     if ( m_dragLastPos 
>= 0 ) 
6375         // erase the last line and resize the col 
6377         int cw
, ch
, dummy
, top
; 
6378         m_gridWin
->GetClientSize( &cw
, &ch 
); 
6379         CalcUnscrolledPosition( 0, 0, &dummy
, &top 
); 
6381         wxClientDC 
dc( m_gridWin 
); 
6383         dc
.SetLogicalFunction( wxINVERT 
); 
6384         dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top 
+ ch 
); 
6385         HideCellEditControl(); 
6386         SaveEditControlValue(); 
6388         int colLeft 
= GetColLeft(m_dragRowOrCol
); 
6389         SetColSize( m_dragRowOrCol
, 
6390                     wxMax( m_dragLastPos 
- colLeft
, 
6391                            GetColMinimalWidth(m_dragRowOrCol
) ) ); 
6393         if ( !GetBatchCount() ) 
6395             // Only needed to get the correct rect.x: 
6396             wxRect 
rect ( CellToRect( 0, m_dragRowOrCol 
) ); 
6398             CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
); 
6399             rect
.width 
= cw 
- rect
.x
; 
6400             rect
.height 
= m_colLabelHeight
; 
6401             m_colLabelWin
->Refresh( true, &rect 
); 
6404             // if there is a multicell block, paint all of it 
6407                 int i
, cell_rows
, cell_cols
, subtract_cols 
= 0; 
6408                 int topRow 
= YToRow(top
); 
6409                 int bottomRow 
= internalYToRow(top 
+ cw
); 
6412                     for (i
=topRow
; i
<bottomRow
; i
++) 
6414                         GetCellSize(i
, m_dragRowOrCol
, &cell_rows
, &cell_cols
); 
6415                         if (cell_cols 
< subtract_cols
) 
6416                             subtract_cols 
= cell_cols
; 
6419                     rect
.x 
= GetColLeft(m_dragRowOrCol 
+ subtract_cols
); 
6420                     CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
); 
6421                     rect
.width 
= cw 
- rect
.x
; 
6425             m_gridWin
->Refresh( false, &rect 
); 
6428         ShowCellEditControl(); 
6432 void wxGrid::DoEndDragMoveCol() 
6434     //The user clicked on the column but didn't actually drag 
6435     if ( m_dragLastPos 
< 0 ) 
6437         m_colLabelWin
->Refresh();   //Do this to "unpress" the column 
6442     if ( m_moveToCol 
== -1 ) 
6443         newPos 
= m_numCols 
- 1; 
6446         newPos 
= GetColPos( m_moveToCol 
); 
6447         if ( newPos 
> GetColPos( m_dragRowOrCol 
) ) 
6451     SetColPos( m_dragRowOrCol
, newPos 
); 
6454 void wxGrid::SetColPos( int colID
, int newPos 
) 
6456     if ( m_colAt
.IsEmpty() ) 
6458         m_colAt
.Alloc( m_numCols 
); 
6461         for ( i 
= 0; i 
< m_numCols
; i
++ ) 
6467     int oldPos 
= GetColPos( colID 
); 
6469     //Reshuffle the m_colAt array 
6470     if ( newPos 
> oldPos 
) 
6473         for ( i 
= oldPos
; i 
< newPos
; i
++ ) 
6475             m_colAt
[i
] = m_colAt
[i
+1]; 
6481         for ( i 
= oldPos
; i 
> newPos
; i
-- ) 
6483             m_colAt
[i
] = m_colAt
[i
-1]; 
6487     m_colAt
[newPos
] = colID
; 
6489     //Recalculate the column rights 
6490     if ( !m_colWidths
.IsEmpty() ) 
6494         for ( colPos 
= 0; colPos 
< m_numCols
; colPos
++ ) 
6496             int colID 
= GetColAt( colPos 
); 
6498             colRight 
+= m_colWidths
[colID
]; 
6499             m_colRights
[colID
] = colRight
; 
6503     m_colLabelWin
->Refresh(); 
6504     m_gridWin
->Refresh(); 
6509 void wxGrid::EnableDragColMove( bool enable 
) 
6511     if ( m_canDragColMove 
== enable 
) 
6514     m_canDragColMove 
= enable
; 
6516     if ( !m_canDragColMove 
) 
6520         //Recalculate the column rights 
6521         if ( !m_colWidths
.IsEmpty() ) 
6525             for ( colPos 
= 0; colPos 
< m_numCols
; colPos
++ ) 
6527                 colRight 
+= m_colWidths
[colPos
]; 
6528                 m_colRights
[colPos
] = colRight
; 
6532         m_colLabelWin
->Refresh(); 
6533         m_gridWin
->Refresh(); 
6539 // ------ interaction with data model 
6541 bool wxGrid::ProcessTableMessage( wxGridTableMessage
& msg 
) 
6543     switch ( msg
.GetId() ) 
6545         case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES
: 
6546             return GetModelValues(); 
6548         case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES
: 
6549             return SetModelValues(); 
6551         case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
: 
6552         case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
: 
6553         case wxGRIDTABLE_NOTIFY_ROWS_DELETED
: 
6554         case wxGRIDTABLE_NOTIFY_COLS_INSERTED
: 
6555         case wxGRIDTABLE_NOTIFY_COLS_APPENDED
: 
6556         case wxGRIDTABLE_NOTIFY_COLS_DELETED
: 
6557             return Redimension( msg 
); 
6564 // The behaviour of this function depends on the grid table class 
6565 // Clear() function. For the default wxGridStringTable class the 
6566 // behavious is to replace all cell contents with wxEmptyString but 
6567 // not to change the number of rows or cols. 
6569 void wxGrid::ClearGrid() 
6573         if (IsCellEditControlEnabled()) 
6574             DisableCellEditControl(); 
6577         if (!GetBatchCount()) 
6578             m_gridWin
->Refresh(); 
6582 bool wxGrid::InsertRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) ) 
6584     // TODO: something with updateLabels flag 
6588         wxFAIL_MSG( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") ); 
6594         if (IsCellEditControlEnabled()) 
6595             DisableCellEditControl(); 
6597         bool done 
= m_table
->InsertRows( pos
, numRows 
); 
6600         // the table will have sent the results of the insert row 
6601         // operation to this view object as a grid table message 
6607 bool wxGrid::AppendRows( int numRows
, bool WXUNUSED(updateLabels
) ) 
6609     // TODO: something with updateLabels flag 
6613         wxFAIL_MSG( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") ); 
6619         bool done 
= m_table 
&& m_table
->AppendRows( numRows 
); 
6622         // the table will have sent the results of the append row 
6623         // operation to this view object as a grid table message 
6629 bool wxGrid::DeleteRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) ) 
6631     // TODO: something with updateLabels flag 
6635         wxFAIL_MSG( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") ); 
6641         if (IsCellEditControlEnabled()) 
6642             DisableCellEditControl(); 
6644         bool done 
= m_table
->DeleteRows( pos
, numRows 
); 
6646         // the table will have sent the results of the delete row 
6647         // operation to this view object as a grid table message 
6653 bool wxGrid::InsertCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) ) 
6655     // TODO: something with updateLabels flag 
6659         wxFAIL_MSG( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") ); 
6665         if (IsCellEditControlEnabled()) 
6666             DisableCellEditControl(); 
6668         bool done 
= m_table
->InsertCols( pos
, numCols 
); 
6670         // the table will have sent the results of the insert col 
6671         // operation to this view object as a grid table message 
6677 bool wxGrid::AppendCols( int numCols
, bool WXUNUSED(updateLabels
) ) 
6679     // TODO: something with updateLabels flag 
6683         wxFAIL_MSG( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") ); 
6689         bool done 
= m_table
->AppendCols( numCols 
); 
6691         // the table will have sent the results of the append col 
6692         // operation to this view object as a grid table message 
6698 bool wxGrid::DeleteCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) ) 
6700     // TODO: something with updateLabels flag 
6704         wxFAIL_MSG( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") ); 
6710         if (IsCellEditControlEnabled()) 
6711             DisableCellEditControl(); 
6713         bool done 
= m_table
->DeleteCols( pos
, numCols 
); 
6715         // the table will have sent the results of the delete col 
6716         // operation to this view object as a grid table message 
6723 // ----- event handlers 
6726 // Generate a grid event based on a mouse event and 
6727 // return the result of ProcessEvent() 
6729 int wxGrid::SendEvent( const wxEventType type
, 
6731                         wxMouseEvent
& mouseEv 
) 
6733    bool claimed
, vetoed
; 
6735    if ( type 
== wxEVT_GRID_ROW_SIZE 
|| type 
== wxEVT_GRID_COL_SIZE 
) 
6737        int rowOrCol 
= (row 
== -1 ? col 
: row
); 
6739        wxGridSizeEvent 
gridEvt( GetId(), 
6743                mouseEv
.GetX() + GetRowLabelSize(), 
6744                mouseEv
.GetY() + GetColLabelSize(), 
6745                mouseEv
.ControlDown(), 
6746                mouseEv
.ShiftDown(), 
6748                mouseEv
.MetaDown() ); 
6750        claimed 
= GetEventHandler()->ProcessEvent(gridEvt
); 
6751        vetoed 
= !gridEvt
.IsAllowed(); 
6753    else if ( type 
== wxEVT_GRID_RANGE_SELECT 
) 
6755        // Right now, it should _never_ end up here! 
6756        wxGridRangeSelectEvent 
gridEvt( GetId(), 
6760                m_selectingBottomRight
, 
6762                mouseEv
.ControlDown(), 
6763                mouseEv
.ShiftDown(), 
6765                mouseEv
.MetaDown() ); 
6767        claimed 
= GetEventHandler()->ProcessEvent(gridEvt
); 
6768        vetoed 
= !gridEvt
.IsAllowed(); 
6770    else if ( type 
== wxEVT_GRID_LABEL_LEFT_CLICK 
|| 
6771              type 
== wxEVT_GRID_LABEL_LEFT_DCLICK 
|| 
6772              type 
== wxEVT_GRID_LABEL_RIGHT_CLICK 
|| 
6773              type 
== wxEVT_GRID_LABEL_RIGHT_DCLICK 
) 
6775        wxPoint pos 
= mouseEv
.GetPosition(); 
6777        if ( mouseEv
.GetEventObject() == GetGridRowLabelWindow() ) 
6778            pos
.y 
+= GetColLabelSize(); 
6779        if ( mouseEv
.GetEventObject() == GetGridColLabelWindow() ) 
6780            pos
.x 
+= GetRowLabelSize(); 
6782        wxGridEvent 
gridEvt( GetId(), 
6789                mouseEv
.ControlDown(), 
6790                mouseEv
.ShiftDown(), 
6792                mouseEv
.MetaDown() ); 
6793        claimed 
= GetEventHandler()->ProcessEvent(gridEvt
); 
6794        vetoed 
= !gridEvt
.IsAllowed(); 
6798        wxGridEvent 
gridEvt( GetId(), 
6802                mouseEv
.GetX() + GetRowLabelSize(), 
6803                mouseEv
.GetY() + GetColLabelSize(), 
6805                mouseEv
.ControlDown(), 
6806                mouseEv
.ShiftDown(), 
6808                mouseEv
.MetaDown() ); 
6809        claimed 
= GetEventHandler()->ProcessEvent(gridEvt
); 
6810        vetoed 
= !gridEvt
.IsAllowed(); 
6813    // A Veto'd event may not be `claimed' so test this first 
6817    return claimed 
? 1 : 0; 
6820 // Generate a grid event of specified type and return the result 
6821 // of ProcessEvent(). 
6823 int wxGrid::SendEvent( const wxEventType type
, 
6826    bool claimed
, vetoed
; 
6828     if ( type 
== wxEVT_GRID_ROW_SIZE 
|| type 
== wxEVT_GRID_COL_SIZE 
) 
6830         int rowOrCol 
= (row 
== -1 ? col 
: row
); 
6832         wxGridSizeEvent 
gridEvt( GetId(), type
, this, rowOrCol 
); 
6834         claimed 
= GetEventHandler()->ProcessEvent(gridEvt
); 
6835         vetoed  
= !gridEvt
.IsAllowed(); 
6839         wxGridEvent 
gridEvt( GetId(), type
, this, row
, col 
); 
6841         claimed 
= GetEventHandler()->ProcessEvent(gridEvt
); 
6842         vetoed  
= !gridEvt
.IsAllowed(); 
6845     // A Veto'd event may not be `claimed' so test this first 
6849     return claimed 
? 1 : 0; 
6852 void wxGrid::OnPaint( wxPaintEvent
& WXUNUSED(event
) ) 
6854     // needed to prevent zillions of paint events on MSW 
6858 void wxGrid::Refresh(bool eraseb
, const wxRect
* rect
) 
6860     // Don't do anything if between Begin/EndBatch... 
6861     // EndBatch() will do all this on the last nested one anyway. 
6862     if ( m_created 
&& !GetBatchCount() ) 
6864         // Refresh to get correct scrolled position: 
6865         wxScrolledWindow::Refresh(eraseb
, rect
); 
6869             int rect_x
, rect_y
, rectWidth
, rectHeight
; 
6870             int width_label
, width_cell
, height_label
, height_cell
; 
6873             // Copy rectangle can get scroll offsets.. 
6874             rect_x 
= rect
->GetX(); 
6875             rect_y 
= rect
->GetY(); 
6876             rectWidth 
= rect
->GetWidth(); 
6877             rectHeight 
= rect
->GetHeight(); 
6879             width_label 
= m_rowLabelWidth 
- rect_x
; 
6880             if (width_label 
> rectWidth
) 
6881                 width_label 
= rectWidth
; 
6883             height_label 
= m_colLabelHeight 
- rect_y
; 
6884             if (height_label 
> rectHeight
) 
6885                 height_label 
= rectHeight
; 
6887             if (rect_x 
> m_rowLabelWidth
) 
6889                 x 
= rect_x 
- m_rowLabelWidth
; 
6890                 width_cell 
= rectWidth
; 
6895                 width_cell 
= rectWidth 
- (m_rowLabelWidth 
- rect_x
); 
6898             if (rect_y 
> m_colLabelHeight
) 
6900                 y 
= rect_y 
- m_colLabelHeight
; 
6901                 height_cell 
= rectHeight
; 
6906                 height_cell 
= rectHeight 
- (m_colLabelHeight 
- rect_y
); 
6909             // Paint corner label part intersecting rect. 
6910             if ( width_label 
> 0 && height_label 
> 0 ) 
6912                 wxRect 
anotherrect(rect_x
, rect_y
, width_label
, height_label
); 
6913                 m_cornerLabelWin
->Refresh(eraseb
, &anotherrect
); 
6916             // Paint col labels part intersecting rect. 
6917             if ( width_cell 
> 0 && height_label 
> 0 ) 
6919                 wxRect 
anotherrect(x
, rect_y
, width_cell
, height_label
); 
6920                 m_colLabelWin
->Refresh(eraseb
, &anotherrect
); 
6923             // Paint row labels part intersecting rect. 
6924             if ( width_label 
> 0 && height_cell 
> 0 ) 
6926                 wxRect 
anotherrect(rect_x
, y
, width_label
, height_cell
); 
6927                 m_rowLabelWin
->Refresh(eraseb
, &anotherrect
); 
6930             // Paint cell area part intersecting rect. 
6931             if ( width_cell 
> 0 && height_cell 
> 0 ) 
6933                 wxRect 
anotherrect(x
, y
, width_cell
, height_cell
); 
6934                 m_gridWin
->Refresh(eraseb
, &anotherrect
); 
6939             m_cornerLabelWin
->Refresh(eraseb
, NULL
); 
6940             m_colLabelWin
->Refresh(eraseb
, NULL
); 
6941             m_rowLabelWin
->Refresh(eraseb
, NULL
); 
6942             m_gridWin
->Refresh(eraseb
, NULL
); 
6947 void wxGrid::OnSize(wxSizeEvent
& WXUNUSED(event
)) 
6949     if (m_targetWindow 
!= this) // check whether initialisation has been done 
6951         // reposition our children windows 
6956 void wxGrid::OnKeyDown( wxKeyEvent
& event 
) 
6958     if ( m_inOnKeyDown 
) 
6960         // shouldn't be here - we are going round in circles... 
6962         wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") ); 
6965     m_inOnKeyDown 
= true; 
6967     // propagate the event up and see if it gets processed 
6968     wxWindow 
*parent 
= GetParent(); 
6969     wxKeyEvent 
keyEvt( event 
); 
6970     keyEvt
.SetEventObject( parent 
); 
6972     if ( !parent
->GetEventHandler()->ProcessEvent( keyEvt 
) ) 
6974         if (GetLayoutDirection() == wxLayout_RightToLeft
) 
6976             if (event
.GetKeyCode() == WXK_RIGHT
) 
6977                 event
.m_keyCode 
= WXK_LEFT
; 
6978             else if (event
.GetKeyCode() == WXK_LEFT
) 
6979                 event
.m_keyCode 
= WXK_RIGHT
; 
6982         // try local handlers 
6983         switch ( event
.GetKeyCode() ) 
6986                 if ( event
.ControlDown() ) 
6987                     MoveCursorUpBlock( event
.ShiftDown() ); 
6989                     MoveCursorUp( event
.ShiftDown() ); 
6993                 if ( event
.ControlDown() ) 
6994                     MoveCursorDownBlock( event
.ShiftDown() ); 
6996                     MoveCursorDown( event
.ShiftDown() ); 
7000                 if ( event
.ControlDown() ) 
7001                     MoveCursorLeftBlock( event
.ShiftDown() ); 
7003                     MoveCursorLeft( event
.ShiftDown() ); 
7007                 if ( event
.ControlDown() ) 
7008                     MoveCursorRightBlock( event
.ShiftDown() ); 
7010                     MoveCursorRight( event
.ShiftDown() ); 
7014             case WXK_NUMPAD_ENTER
: 
7015                 if ( event
.ControlDown() ) 
7017                     event
.Skip();  // to let the edit control have the return 
7021                     if ( GetGridCursorRow() < GetNumberRows()-1 ) 
7023                         MoveCursorDown( event
.ShiftDown() ); 
7027                         // at the bottom of a column 
7028                         DisableCellEditControl(); 
7038                 if (event
.ShiftDown()) 
7040                     if ( GetGridCursorCol() > 0 ) 
7042                         MoveCursorLeft( false ); 
7047                         DisableCellEditControl(); 
7052                     if ( GetGridCursorCol() < GetNumberCols() - 1 ) 
7054                         MoveCursorRight( false ); 
7059                         DisableCellEditControl(); 
7065                 if ( event
.ControlDown() ) 
7067                     MakeCellVisible( 0, 0 ); 
7068                     SetCurrentCell( 0, 0 ); 
7077                 if ( event
.ControlDown() ) 
7079                     MakeCellVisible( m_numRows 
- 1, m_numCols 
- 1 ); 
7080                     SetCurrentCell( m_numRows 
- 1, m_numCols 
- 1 ); 
7097                 // Ctrl-Space selects the current column, Shift-Space -- the 
7098                 // current row and Ctrl-Shift-Space -- everything 
7099                 switch ( m_selection 
? event
.GetModifiers() : wxMOD_NONE 
) 
7102                         m_selection
->SelectCol(m_currentCellCoords
.GetCol()); 
7106                         m_selection
->SelectRow(m_currentCellCoords
.GetRow()); 
7109                     case wxMOD_CONTROL 
| wxMOD_SHIFT
: 
7110                         m_selection
->SelectBlock(0, 0, 
7111                                                  m_numRows 
- 1, m_numCols 
- 1); 
7115                         if ( !IsEditable() ) 
7117                             MoveCursorRight(false); 
7120                         //else: fall through 
7133     m_inOnKeyDown 
= false; 
7136 void wxGrid::OnKeyUp( wxKeyEvent
& event 
) 
7138     // try local handlers 
7140     if ( event
.GetKeyCode() == WXK_SHIFT 
) 
7142         if ( m_selectingTopLeft 
!= wxGridNoCellCoords 
&& 
7143              m_selectingBottomRight 
!= wxGridNoCellCoords 
) 
7147                 m_selection
->SelectBlock( 
7148                     m_selectingTopLeft
.GetRow(), 
7149                     m_selectingTopLeft
.GetCol(), 
7150                     m_selectingBottomRight
.GetRow(), 
7151                     m_selectingBottomRight
.GetCol(), 
7152                     event
.ControlDown(), 
7159         m_selectingTopLeft 
= wxGridNoCellCoords
; 
7160         m_selectingBottomRight 
= wxGridNoCellCoords
; 
7161         m_selectingKeyboard 
= wxGridNoCellCoords
; 
7165 void wxGrid::OnChar( wxKeyEvent
& event 
) 
7167     // is it possible to edit the current cell at all? 
7168     if ( !IsCellEditControlEnabled() && CanEnableCellControl() ) 
7170         // yes, now check whether the cells editor accepts the key 
7171         int row 
= m_currentCellCoords
.GetRow(); 
7172         int col 
= m_currentCellCoords
.GetCol(); 
7173         wxGridCellAttr 
*attr 
= GetCellAttr(row
, col
); 
7174         wxGridCellEditor 
*editor 
= attr
->GetEditor(this, row
, col
); 
7176         // <F2> is special and will always start editing, for 
7177         // other keys - ask the editor itself 
7178         if ( (event
.GetKeyCode() == WXK_F2 
&& !event
.HasModifiers()) 
7179              || editor
->IsAcceptedKey(event
) ) 
7181             // ensure cell is visble 
7182             MakeCellVisible(row
, col
); 
7183             EnableCellEditControl(); 
7185             // a problem can arise if the cell is not completely 
7186             // visible (even after calling MakeCellVisible the 
7187             // control is not created and calling StartingKey will 
7189             if ( event
.GetKeyCode() != WXK_F2 
&& editor
->IsCreated() && m_cellEditCtrlEnabled 
) 
7190                 editor
->StartingKey(event
); 
7206 void wxGrid::OnEraseBackground(wxEraseEvent
&) 
7210 void wxGrid::SetCurrentCell( const wxGridCellCoords
& coords 
) 
7212     if ( SendEvent( wxEVT_GRID_SELECT_CELL
, coords
.GetRow(), coords
.GetCol() ) ) 
7214         // the event has been intercepted - do nothing 
7218 #if !defined(__WXMAC__) 
7219     wxClientDC 
dc( m_gridWin 
); 
7223     if ( m_currentCellCoords 
!= wxGridNoCellCoords 
) 
7225         DisableCellEditControl(); 
7227         if ( IsVisible( m_currentCellCoords
, false ) ) 
7230             r 
= BlockToDeviceRect( m_currentCellCoords
, m_currentCellCoords 
); 
7231             if ( !m_gridLinesEnabled 
) 
7239             wxGridCellCoordsArray cells 
= CalcCellsExposed( r 
); 
7241             // Otherwise refresh redraws the highlight! 
7242             m_currentCellCoords 
= coords
; 
7244 #if defined(__WXMAC__) 
7245             m_gridWin
->Refresh(true /*, & r */); 
7247             DrawGridCellArea( dc
, cells 
); 
7248             DrawAllGridLines( dc
, r 
); 
7253     m_currentCellCoords 
= coords
; 
7255     wxGridCellAttr 
*attr 
= GetCellAttr( coords 
); 
7256 #if !defined(__WXMAC__) 
7257     DrawCellHighlight( dc
, attr 
); 
7262 void wxGrid::HighlightBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol 
) 
7265     wxGridCellCoords updateTopLeft
, updateBottomRight
; 
7269         if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows 
) 
7272             rightCol 
= GetNumberCols() - 1; 
7274         else if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns 
) 
7277             bottomRow 
= GetNumberRows() - 1; 
7281     if ( topRow 
> bottomRow 
) 
7288     if ( leftCol 
> rightCol 
) 
7295     updateTopLeft 
= wxGridCellCoords( topRow
, leftCol 
); 
7296     updateBottomRight 
= wxGridCellCoords( bottomRow
, rightCol 
); 
7298     // First the case that we selected a completely new area 
7299     if ( m_selectingTopLeft 
== wxGridNoCellCoords 
|| 
7300          m_selectingBottomRight 
== wxGridNoCellCoords 
) 
7303         rect 
= BlockToDeviceRect( wxGridCellCoords ( topRow
, leftCol 
), 
7304                                   wxGridCellCoords ( bottomRow
, rightCol 
) ); 
7305         m_gridWin
->Refresh( false, &rect 
); 
7308     // Now handle changing an existing selection area. 
7309     else if ( m_selectingTopLeft 
!= updateTopLeft 
|| 
7310               m_selectingBottomRight 
!= updateBottomRight 
) 
7312         // Compute two optimal update rectangles: 
7313         // Either one rectangle is a real subset of the 
7314         // other, or they are (almost) disjoint! 
7316         bool    need_refresh
[4]; 
7320         need_refresh
[3] = false; 
7323         // Store intermediate values 
7324         wxCoord oldLeft 
= m_selectingTopLeft
.GetCol(); 
7325         wxCoord oldTop 
= m_selectingTopLeft
.GetRow(); 
7326         wxCoord oldRight 
= m_selectingBottomRight
.GetCol(); 
7327         wxCoord oldBottom 
= m_selectingBottomRight
.GetRow(); 
7329         // Determine the outer/inner coordinates. 
7330         if (oldLeft 
> leftCol
) 
7336         if (oldTop 
> topRow 
) 
7342         if (oldRight 
< rightCol 
) 
7345             oldRight 
= rightCol
; 
7348         if (oldBottom 
< bottomRow
) 
7351             oldBottom 
= bottomRow
; 
7355         // Now, either the stuff marked old is the outer 
7356         // rectangle or we don't have a situation where one 
7357         // is contained in the other. 
7359         if ( oldLeft 
< leftCol 
) 
7361             // Refresh the newly selected or deselected 
7362             // area to the left of the old or new selection. 
7363             need_refresh
[0] = true; 
7364             rect
[0] = BlockToDeviceRect( 
7365                 wxGridCellCoords( oldTop
,  oldLeft 
), 
7366                 wxGridCellCoords( oldBottom
, leftCol 
- 1 ) ); 
7369         if ( oldTop 
< topRow 
) 
7371             // Refresh the newly selected or deselected 
7372             // area above the old or new selection. 
7373             need_refresh
[1] = true; 
7374             rect
[1] = BlockToDeviceRect( 
7375                 wxGridCellCoords( oldTop
, leftCol 
), 
7376                 wxGridCellCoords( topRow 
- 1, rightCol 
) ); 
7379         if ( oldRight 
> rightCol 
) 
7381             // Refresh the newly selected or deselected 
7382             // area to the right of the old or new selection. 
7383             need_refresh
[2] = true; 
7384             rect
[2] = BlockToDeviceRect( 
7385                 wxGridCellCoords( oldTop
, rightCol 
+ 1 ), 
7386                 wxGridCellCoords( oldBottom
, oldRight 
) ); 
7389         if ( oldBottom 
> bottomRow 
) 
7391             // Refresh the newly selected or deselected 
7392             // area below the old or new selection. 
7393             need_refresh
[3] = true; 
7394             rect
[3] = BlockToDeviceRect( 
7395                 wxGridCellCoords( bottomRow 
+ 1, leftCol 
), 
7396                 wxGridCellCoords( oldBottom
, rightCol 
) ); 
7399         // various Refresh() calls 
7400         for (i 
= 0; i 
< 4; i
++ ) 
7401             if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect 
) 
7402                 m_gridWin
->Refresh( false, &(rect
[i
]) ); 
7406     m_selectingTopLeft 
= updateTopLeft
; 
7407     m_selectingBottomRight 
= updateBottomRight
; 
7411 // ------ functions to get/send data (see also public functions) 
7414 bool wxGrid::GetModelValues() 
7416     // Hide the editor, so it won't hide a changed value. 
7417     HideCellEditControl(); 
7421         // all we need to do is repaint the grid 
7423         m_gridWin
->Refresh(); 
7430 bool wxGrid::SetModelValues() 
7434     // Disable the editor, so it won't hide a changed value. 
7435     // Do we also want to save the current value of the editor first? 
7437     DisableCellEditControl(); 
7441         for ( row 
= 0; row 
< m_numRows
; row
++ ) 
7443             for ( col 
= 0; col 
< m_numCols
; col
++ ) 
7445                 m_table
->SetValue( row
, col
, GetCellValue(row
, col
) ); 
7455 // Note - this function only draws cells that are in the list of 
7456 // exposed cells (usually set from the update region by 
7457 // CalcExposedCells) 
7459 void wxGrid::DrawGridCellArea( wxDC
& dc
, const wxGridCellCoordsArray
& cells 
) 
7461     if ( !m_numRows 
|| !m_numCols 
) 
7464     int i
, numCells 
= cells
.GetCount(); 
7465     int row
, col
, cell_rows
, cell_cols
; 
7466     wxGridCellCoordsArray redrawCells
; 
7468     for ( i 
= numCells 
- 1; i 
>= 0; i
-- ) 
7470         row 
= cells
[i
].GetRow(); 
7471         col 
= cells
[i
].GetCol(); 
7472         GetCellSize( row
, col
, &cell_rows
, &cell_cols 
); 
7474         // If this cell is part of a multicell block, find owner for repaint 
7475         if ( cell_rows 
<= 0 || cell_cols 
<= 0 ) 
7477             wxGridCellCoords 
cell( row 
+ cell_rows
, col 
+ cell_cols 
); 
7478             bool marked 
= false; 
7479             for ( int j 
= 0; j 
< numCells
; j
++ ) 
7481                 if ( cell 
== cells
[j
] ) 
7490                 int count 
= redrawCells
.GetCount(); 
7491                 for (int j 
= 0; j 
< count
; j
++) 
7493                     if ( cell 
== redrawCells
[j
] ) 
7501                     redrawCells
.Add( cell 
); 
7504             // don't bother drawing this cell 
7508         // If this cell is empty, find cell to left that might want to overflow 
7509         if (m_table 
&& m_table
->IsEmptyCell(row
, col
)) 
7511             for ( int l 
= 0; l 
< cell_rows
; l
++ ) 
7513                 // find a cell in this row to leave already marked for repaint 
7515                 for (int k 
= 0; k 
< int(redrawCells
.GetCount()); k
++) 
7516                     if ((redrawCells
[k
].GetCol() < left
) && 
7517                         (redrawCells
[k
].GetRow() == row
)) 
7519                         left 
= redrawCells
[k
].GetCol(); 
7523                     left 
= 0; // oh well 
7525                 for (int j 
= col 
- 1; j 
>= left
; j
--) 
7527                     if (!m_table
->IsEmptyCell(row 
+ l
, j
)) 
7529                         if (GetCellOverflow(row 
+ l
, j
)) 
7531                             wxGridCellCoords 
cell(row 
+ l
, j
); 
7532                             bool marked 
= false; 
7534                             for (int k 
= 0; k 
< numCells
; k
++) 
7536                                 if ( cell 
== cells
[k
] ) 
7545                                 int count 
= redrawCells
.GetCount(); 
7546                                 for (int k 
= 0; k 
< count
; k
++) 
7548                                     if ( cell 
== redrawCells
[k
] ) 
7555                                     redrawCells
.Add( cell 
); 
7564         DrawCell( dc
, cells
[i
] ); 
7567     numCells 
= redrawCells
.GetCount(); 
7569     for ( i 
= numCells 
- 1; i 
>= 0; i
-- ) 
7571         DrawCell( dc
, redrawCells
[i
] ); 
7575 void wxGrid::DrawGridSpace( wxDC
& dc 
) 
7578   m_gridWin
->GetClientSize( &cw
, &ch 
); 
7581   CalcUnscrolledPosition( cw
, ch
, &right
, &bottom 
); 
7583   int rightCol 
= m_numCols 
> 0 ? GetColRight(GetColAt( m_numCols 
- 1 )) : 0; 
7584   int bottomRow 
= m_numRows 
> 0 ? GetRowBottom(m_numRows 
- 1) : 0; 
7586   if ( right 
> rightCol 
|| bottom 
> bottomRow 
) 
7589       CalcUnscrolledPosition( 0, 0, &left
, &top 
); 
7591       dc
.SetBrush( wxBrush(GetDefaultCellBackgroundColour(), wxBRUSHSTYLE_SOLID
) ); 
7592       dc
.SetPen( *wxTRANSPARENT_PEN 
); 
7594       if ( right 
> rightCol 
) 
7596           dc
.DrawRectangle( rightCol
, top
, right 
- rightCol
, ch 
); 
7599       if ( bottom 
> bottomRow 
) 
7601           dc
.DrawRectangle( left
, bottomRow
, cw
, bottom 
- bottomRow 
); 
7606 void wxGrid::DrawCell( wxDC
& dc
, const wxGridCellCoords
& coords 
) 
7608     int row 
= coords
.GetRow(); 
7609     int col 
= coords
.GetCol(); 
7611     if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 ) 
7614     // we draw the cell border ourselves 
7615 #if !WXGRID_DRAW_LINES 
7616     if ( m_gridLinesEnabled 
) 
7617         DrawCellBorder( dc
, coords 
); 
7620     wxGridCellAttr
* attr 
= GetCellAttr(row
, col
); 
7622     bool isCurrent 
= coords 
== m_currentCellCoords
; 
7624     wxRect rect 
= CellToRect( row
, col 
); 
7626     // if the editor is shown, we should use it and not the renderer 
7627     // Note: However, only if it is really _shown_, i.e. not hidden! 
7628     if ( isCurrent 
&& IsCellEditControlShown() ) 
7630         // NB: this "#if..." is temporary and fixes a problem where the 
7631         // edit control is erased by this code after being rendered. 
7632         // On wxMac (QD build only), the cell editor is a wxTextCntl and is rendered 
7633         // implicitly, causing this out-of order render. 
7634 #if !defined(__WXMAC__) 
7635         wxGridCellEditor 
*editor 
= attr
->GetEditor(this, row
, col
); 
7636         editor
->PaintBackground(rect
, attr
); 
7642         // but all the rest is drawn by the cell renderer and hence may be customized 
7643         wxGridCellRenderer 
*renderer 
= attr
->GetRenderer(this, row
, col
); 
7644         renderer
->Draw(*this, *attr
, dc
, rect
, row
, col
, IsInSelection(coords
)); 
7651 void wxGrid::DrawCellHighlight( wxDC
& dc
, const wxGridCellAttr 
*attr 
) 
7653     // don't show highlight when the grid doesn't have focus 
7657     int row 
= m_currentCellCoords
.GetRow(); 
7658     int col 
= m_currentCellCoords
.GetCol(); 
7660     if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 ) 
7663     wxRect rect 
= CellToRect(row
, col
); 
7665     // hmmm... what could we do here to show that the cell is disabled? 
7666     // for now, I just draw a thinner border than for the other ones, but 
7667     // it doesn't look really good 
7669     int penWidth 
= attr
->IsReadOnly() ? m_cellHighlightROPenWidth 
: m_cellHighlightPenWidth
; 
7673         // The center of the drawn line is where the position/width/height of 
7674         // the rectangle is actually at (on wxMSW at least), so the 
7675         // size of the rectangle is reduced to compensate for the thickness of 
7676         // the line. If this is too strange on non-wxMSW platforms then 
7677         // please #ifdef this appropriately. 
7678         rect
.x 
+= penWidth 
/ 2; 
7679         rect
.y 
+= penWidth 
/ 2; 
7680         rect
.width 
-= penWidth 
- 1; 
7681         rect
.height 
-= penWidth 
- 1; 
7683         // Now draw the rectangle 
7684         // use the cellHighlightColour if the cell is inside a selection, this 
7685         // will ensure the cell is always visible. 
7686         dc
.SetPen(wxPen(IsInSelection(row
,col
) ? m_selectionForeground 
: m_cellHighlightColour
, penWidth
, wxPENSTYLE_SOLID
)); 
7687         dc
.SetBrush(*wxTRANSPARENT_BRUSH
); 
7688         dc
.DrawRectangle(rect
); 
7692         // VZ: my experiments with 3D borders... 
7694         // how to properly set colours for arbitrary bg? 
7695         wxCoord x1 
= rect
.x
, 
7697                 x2 
= rect
.x 
+ rect
.width 
- 1, 
7698                 y2 
= rect
.y 
+ rect
.height 
- 1; 
7700         dc
.SetPen(*wxWHITE_PEN
); 
7701         dc
.DrawLine(x1
, y1
, x2
, y1
); 
7702         dc
.DrawLine(x1
, y1
, x1
, y2
); 
7704         dc
.DrawLine(x1 
+ 1, y2 
- 1, x2 
- 1, y2 
- 1); 
7705         dc
.DrawLine(x2 
- 1, y1 
+ 1, x2 
- 1, y2
); 
7707         dc
.SetPen(*wxBLACK_PEN
); 
7708         dc
.DrawLine(x1
, y2
, x2
, y2
); 
7709         dc
.DrawLine(x2
, y1
, x2
, y2 
+ 1); 
7713 wxPen 
wxGrid::GetDefaultGridLinePen() 
7715     return wxPen(GetGridLineColour(), 1, wxPENSTYLE_SOLID
); 
7718 wxPen 
wxGrid::GetRowGridLinePen(int WXUNUSED(row
)) 
7720     return GetDefaultGridLinePen(); 
7723 wxPen 
wxGrid::GetColGridLinePen(int WXUNUSED(col
)) 
7725     return GetDefaultGridLinePen(); 
7728 void wxGrid::DrawCellBorder( wxDC
& dc
, const wxGridCellCoords
& coords 
) 
7730     int row 
= coords
.GetRow(); 
7731     int col 
= coords
.GetCol(); 
7732     if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 ) 
7736     wxRect rect 
= CellToRect( row
, col 
); 
7738     // right hand border 
7739     dc
.SetPen( GetColGridLinePen(col
) ); 
7740     dc
.DrawLine( rect
.x 
+ rect
.width
, rect
.y
, 
7741                  rect
.x 
+ rect
.width
, rect
.y 
+ rect
.height 
+ 1 ); 
7744     dc
.SetPen( GetRowGridLinePen(row
) ); 
7745     dc
.DrawLine( rect
.x
, rect
.y 
+ rect
.height
, 
7746                  rect
.x 
+ rect
.width
, rect
.y 
+ rect
.height
); 
7749 void wxGrid::DrawHighlight(wxDC
& dc
, const wxGridCellCoordsArray
& cells
) 
7751     // This if block was previously in wxGrid::OnPaint but that doesn't 
7752     // seem to get called under wxGTK - MB 
7754     if ( m_currentCellCoords 
== wxGridNoCellCoords 
&& 
7755          m_numRows 
&& m_numCols 
) 
7757         m_currentCellCoords
.Set(0, 0); 
7760     if ( IsCellEditControlShown() ) 
7762         // don't show highlight when the edit control is shown 
7766     // if the active cell was repainted, repaint its highlight too because it 
7767     // might have been damaged by the grid lines 
7768     size_t count 
= cells
.GetCount(); 
7769     for ( size_t n 
= 0; n 
< count
; n
++ ) 
7771         wxGridCellCoords cell 
= cells
[n
]; 
7773         // If we are using attributes, then we may have just exposed another 
7774         // cell in a partially-visible merged cluster of cells. If the "anchor" 
7775         // (upper left) cell of this merged cluster is the cell indicated by 
7776         // m_currentCellCoords, then we need to refresh the cell highlight even 
7777         // though the "anchor" itself is not part of our update segment. 
7778         if ( CanHaveAttributes() ) 
7782             GetCellSize(cell
.GetRow(), cell
.GetCol(), &rows
, &cols
); 
7785                 cell
.SetRow(cell
.GetRow() + rows
); 
7788                 cell
.SetCol(cell
.GetCol() + cols
); 
7791         if ( cell 
== m_currentCellCoords 
) 
7793             wxGridCellAttr
* attr 
= GetCellAttr(m_currentCellCoords
); 
7794             DrawCellHighlight(dc
, attr
); 
7802 // TODO: remove this ??? 
7803 // This is used to redraw all grid lines e.g. when the grid line colour 
7806 void wxGrid::DrawAllGridLines( wxDC
& dc
, const wxRegion 
& WXUNUSED(reg
) ) 
7808 #if !WXGRID_DRAW_LINES 
7812     if ( !m_gridLinesEnabled 
|| !m_numRows 
|| !m_numCols 
) 
7815     int top
, bottom
, left
, right
; 
7817 #if 0  //#ifndef __WXGTK__ 
7821       m_gridWin
->GetClientSize(&cw
, &ch
); 
7823       // virtual coords of visible area 
7825       CalcUnscrolledPosition( 0, 0, &left
, &top 
); 
7826       CalcUnscrolledPosition( cw
, ch
, &right
, &bottom 
); 
7831       reg
.GetBox(x
, y
, w
, h
); 
7832       CalcUnscrolledPosition( x
, y
, &left
, &top 
); 
7833       CalcUnscrolledPosition( x 
+ w
, y 
+ h
, &right
, &bottom 
); 
7837       m_gridWin
->GetClientSize(&cw
, &ch
); 
7838       CalcUnscrolledPosition( 0, 0, &left
, &top 
); 
7839       CalcUnscrolledPosition( cw
, ch
, &right
, &bottom 
); 
7842     // avoid drawing grid lines past the last row and col 
7844     right 
= wxMin( right
, GetColRight(GetColAt( m_numCols 
- 1 )) ); 
7845     bottom 
= wxMin( bottom
, GetRowBottom(m_numRows 
- 1) ); 
7847     // no gridlines inside multicells, clip them out 
7848     int leftCol 
= GetColPos( internalXToCol(left
) ); 
7849     int topRow 
= internalYToRow(top
); 
7850     int rightCol 
= GetColPos( internalXToCol(right
) ); 
7851     int bottomRow 
= internalYToRow(bottom
); 
7853     wxRegion 
clippedcells(0, 0, cw
, ch
); 
7855     int i
, j
, cell_rows
, cell_cols
; 
7858     for (j
=topRow
; j
<=bottomRow
; j
++) 
7861         for (colPos
=leftCol
; colPos
<=rightCol
; colPos
++) 
7863             i 
= GetColAt( colPos 
); 
7865             GetCellSize( j
, i
, &cell_rows
, &cell_cols 
); 
7866             if ((cell_rows 
> 1) || (cell_cols 
> 1)) 
7868                 rect 
= CellToRect(j
,i
); 
7869                 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y 
); 
7870                 clippedcells
.Subtract(rect
); 
7872             else if ((cell_rows 
< 0) || (cell_cols 
< 0)) 
7874                 rect 
= CellToRect(j 
+ cell_rows
, i 
+ cell_cols
); 
7875                 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y 
); 
7876                 clippedcells
.Subtract(rect
); 
7881     dc
.SetDeviceClippingRegion( clippedcells 
); 
7884     // horizontal grid lines 
7886     // already declared above - int i; 
7887     for ( i 
= internalYToRow(top
); i 
< m_numRows
; i
++ ) 
7889         int bot 
= GetRowBottom(i
) - 1; 
7898             dc
.SetPen( GetRowGridLinePen(i
) ); 
7899             dc
.DrawLine( left
, bot
, right
, bot 
); 
7903     // vertical grid lines 
7906     for ( colPos 
= leftCol
; colPos 
< m_numCols
; colPos
++ ) 
7908         i 
= GetColAt( colPos 
); 
7910         int colRight 
= GetColRight(i
); 
7912         if (GetLayoutDirection() != wxLayout_RightToLeft
) 
7916         if ( colRight 
> right 
) 
7921         if ( colRight 
>= left 
) 
7923             dc
.SetPen( GetColGridLinePen(i
) ); 
7924             dc
.DrawLine( colRight
, top
, colRight
, bottom 
); 
7928     dc
.DestroyClippingRegion(); 
7931 void wxGrid::DrawRowLabels( wxDC
& dc
, const wxArrayInt
& rows
) 
7937     size_t numLabels 
= rows
.GetCount(); 
7939     for ( i 
= 0; i 
< numLabels
; i
++ ) 
7941         DrawRowLabel( dc
, rows
[i
] ); 
7945 void wxGrid::DrawRowLabel( wxDC
& dc
, int row 
) 
7947     if ( GetRowHeight(row
) <= 0 || m_rowLabelWidth 
<= 0 ) 
7952     int rowTop 
= GetRowTop(row
), 
7953         rowBottom 
= GetRowBottom(row
) - 1; 
7955     dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW
), 1, wxPENSTYLE_SOLID
) ); 
7956     dc
.DrawLine( m_rowLabelWidth 
- 1, rowTop
, m_rowLabelWidth 
- 1, rowBottom 
); 
7957     dc
.DrawLine( 0, rowTop
, 0, rowBottom 
); 
7958     dc
.DrawLine( 0, rowBottom
, m_rowLabelWidth
, rowBottom 
); 
7960     dc
.SetPen( *wxWHITE_PEN 
); 
7961     dc
.DrawLine( 1, rowTop
, 1, rowBottom 
); 
7962     dc
.DrawLine( 1, rowTop
, m_rowLabelWidth 
- 1, rowTop 
); 
7964     dc
.SetBackgroundMode( wxBRUSHSTYLE_TRANSPARENT 
); 
7965     dc
.SetTextForeground( GetLabelTextColour() ); 
7966     dc
.SetFont( GetLabelFont() ); 
7969     GetRowLabelAlignment( &hAlign
, &vAlign 
); 
7972     rect
.SetY( GetRowTop(row
) + 2 ); 
7973     rect
.SetWidth( m_rowLabelWidth 
- 4 ); 
7974     rect
.SetHeight( GetRowHeight(row
) - 4 ); 
7975     DrawTextRectangle( dc
, GetRowLabelValue( row 
), rect
, hAlign
, vAlign 
); 
7978 void wxGrid::SetUseNativeColLabels( bool native 
) 
7980     m_nativeColumnLabels 
= native
; 
7983         int height 
= wxRendererNative::Get().GetHeaderButtonHeight( this ); 
7984         SetColLabelSize( height 
); 
7987     m_colLabelWin
->Refresh(); 
7990 void wxGrid::DrawColLabels( wxDC
& dc
,const wxArrayInt
& cols 
) 
7996     size_t numLabels 
= cols
.GetCount(); 
7998     for ( i 
= 0; i 
< numLabels
; i
++ ) 
8000         DrawColLabel( dc
, cols
[i
] ); 
8004 void wxGrid::DrawColLabel( wxDC
& dc
, int col 
) 
8006     if ( GetColWidth(col
) <= 0 || m_colLabelHeight 
<= 0 ) 
8009     int colLeft 
= GetColLeft(col
); 
8013     if (m_nativeColumnLabels
) 
8015         rect
.SetX( colLeft
); 
8017         rect
.SetWidth( GetColWidth(col
)); 
8018         rect
.SetHeight( m_colLabelHeight 
); 
8020         wxWindowDC 
*win_dc 
= (wxWindowDC
*) &dc
; 
8021         wxRendererNative::Get().DrawHeaderButton( win_dc
->GetWindow(), dc
, rect
, 0 ); 
8025         int colRight 
= GetColRight(col
) - 1; 
8027         dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW
), 1, wxPENSTYLE_SOLID
) ); 
8028         dc
.DrawLine( colRight
, 0, colRight
, m_colLabelHeight 
- 1 ); 
8029         dc
.DrawLine( colLeft
, 0, colRight
, 0 ); 
8030         dc
.DrawLine( colLeft
, m_colLabelHeight 
- 1, 
8031                  colRight 
+ 1, m_colLabelHeight 
- 1 ); 
8033         dc
.SetPen( *wxWHITE_PEN 
); 
8034         dc
.DrawLine( colLeft
, 1, colLeft
, m_colLabelHeight 
- 1 ); 
8035         dc
.DrawLine( colLeft
, 1, colRight
, 1 ); 
8038     dc
.SetBackgroundMode( wxBRUSHSTYLE_TRANSPARENT 
); 
8039     dc
.SetTextForeground( GetLabelTextColour() ); 
8040     dc
.SetFont( GetLabelFont() ); 
8042     int hAlign
, vAlign
, orient
; 
8043     GetColLabelAlignment( &hAlign
, &vAlign 
); 
8044     orient 
= GetColLabelTextOrientation(); 
8046     rect
.SetX( colLeft 
+ 2 ); 
8048     rect
.SetWidth( GetColWidth(col
) - 4 ); 
8049     rect
.SetHeight( m_colLabelHeight 
- 4 ); 
8050     DrawTextRectangle( dc
, GetColLabelValue( col 
), rect
, hAlign
, vAlign
, orient 
); 
8053 void wxGrid::DrawTextRectangle( wxDC
& dc
, 
8054                                 const wxString
& value
, 
8058                                 int textOrientation 
) 
8060     wxArrayString lines
; 
8062     StringToLines( value
, lines 
); 
8064     // Forward to new API. 
8065     DrawTextRectangle( dc
, 
8073 // VZ: this should be replaced with wxDC::DrawLabel() to which we just have to 
8074 //     add textOrientation support 
8075 void wxGrid::DrawTextRectangle(wxDC
& dc
, 
8076                                const wxArrayString
& lines
, 
8080                                int textOrientation
) 
8082     if ( lines
.empty() ) 
8085     wxDCClipper 
clip(dc
, rect
); 
8090     if ( textOrientation 
== wxHORIZONTAL 
) 
8091         GetTextBoxSize( dc
, lines
, &textWidth
, &textHeight 
); 
8093         GetTextBoxSize( dc
, lines
, &textHeight
, &textWidth 
); 
8097     switch ( vertAlign 
) 
8099         case wxALIGN_BOTTOM
: 
8100             if ( textOrientation 
== wxHORIZONTAL 
) 
8101                 y 
= rect
.y 
+ (rect
.height 
- textHeight 
- 1); 
8103                 x 
= rect
.x 
+ rect
.width 
- textWidth
; 
8106         case wxALIGN_CENTRE
: 
8107             if ( textOrientation 
== wxHORIZONTAL 
) 
8108                 y 
= rect
.y 
+ ((rect
.height 
- textHeight
) / 2); 
8110                 x 
= rect
.x 
+ ((rect
.width 
- textWidth
) / 2); 
8115             if ( textOrientation 
== wxHORIZONTAL 
) 
8122     // Align each line of a multi-line label 
8123     size_t nLines 
= lines
.GetCount(); 
8124     for ( size_t l 
= 0; l 
< nLines
; l
++ ) 
8126         const wxString
& line 
= lines
[l
]; 
8130             *(textOrientation 
== wxHORIZONTAL 
? &y 
: &x
) += dc
.GetCharHeight(); 
8134         wxCoord lineWidth 
= 0, 
8136         dc
.GetTextExtent(line
, &lineWidth
, &lineHeight
); 
8138         switch ( horizAlign 
) 
8141                 if ( textOrientation 
== wxHORIZONTAL 
) 
8142                     x 
= rect
.x 
+ (rect
.width 
- lineWidth 
- 1); 
8144                     y 
= rect
.y 
+ lineWidth 
+ 1; 
8147             case wxALIGN_CENTRE
: 
8148                 if ( textOrientation 
== wxHORIZONTAL 
) 
8149                     x 
= rect
.x 
+ ((rect
.width 
- lineWidth
) / 2); 
8151                     y 
= rect
.y 
+ rect
.height 
- ((rect
.height 
- lineWidth
) / 2); 
8156                 if ( textOrientation 
== wxHORIZONTAL 
) 
8159                     y 
= rect
.y 
+ rect
.height 
- 1; 
8163         if ( textOrientation 
== wxHORIZONTAL 
) 
8165             dc
.DrawText( line
, x
, y 
); 
8170             dc
.DrawRotatedText( line
, x
, y
, 90.0 ); 
8176 // Split multi-line text up into an array of strings. 
8177 // Any existing contents of the string array are preserved. 
8179 // TODO: refactor wxTextFile::Read() and reuse the same code from here 
8180 void wxGrid::StringToLines( const wxString
& value
, wxArrayString
& lines 
) const 
8184     wxString eol 
= wxTextFile::GetEOL( wxTextFileType_Unix 
); 
8185     wxString tVal 
= wxTextFile::Translate( value
, wxTextFileType_Unix 
); 
8187     while ( startPos 
< (int)tVal
.length() ) 
8189         pos 
= tVal
.Mid(startPos
).Find( eol 
); 
8194         else if ( pos 
== 0 ) 
8196             lines
.Add( wxEmptyString 
); 
8200             lines
.Add( tVal
.Mid(startPos
, pos
) ); 
8203         startPos 
+= pos 
+ 1; 
8206     if ( startPos 
< (int)tVal
.length() ) 
8208         lines
.Add( tVal
.Mid( startPos 
) ); 
8212 void wxGrid::GetTextBoxSize( const wxDC
& dc
, 
8213                              const wxArrayString
& lines
, 
8214                              long *width
, long *height 
) const 
8218     wxCoord lineW 
= 0, lineH 
= 0; 
8221     for ( i 
= 0; i 
< lines
.GetCount(); i
++ ) 
8223         dc
.GetTextExtent( lines
[i
], &lineW
, &lineH 
); 
8224         w 
= wxMax( w
, lineW 
); 
8233 // ------ Batch processing. 
8235 void wxGrid::EndBatch() 
8237     if ( m_batchCount 
> 0 ) 
8240         if ( !m_batchCount 
) 
8243             m_rowLabelWin
->Refresh(); 
8244             m_colLabelWin
->Refresh(); 
8245             m_cornerLabelWin
->Refresh(); 
8246             m_gridWin
->Refresh(); 
8251 // Use this, rather than wxWindow::Refresh(), to force an immediate 
8252 // repainting of the grid. Has no effect if you are already inside a 
8253 // BeginBatch / EndBatch block. 
8255 void wxGrid::ForceRefresh() 
8261 bool wxGrid::Enable(bool enable
) 
8263     if ( !wxScrolledWindow::Enable(enable
) ) 
8266     // redraw in the new state 
8267     m_gridWin
->Refresh(); 
8273 // ------ Edit control functions 
8276 void wxGrid::EnableEditing( bool edit 
) 
8278     // TODO: improve this ? 
8280     if ( edit 
!= m_editable 
) 
8283             EnableCellEditControl(edit
); 
8288 void wxGrid::EnableCellEditControl( bool enable 
) 
8293     if ( enable 
!= m_cellEditCtrlEnabled 
) 
8297             if (SendEvent( wxEVT_GRID_EDITOR_SHOWN
) <0) 
8300             // this should be checked by the caller! 
8301             wxASSERT_MSG( CanEnableCellControl(), _T("can't enable editing for this cell!") ); 
8303             // do it before ShowCellEditControl() 
8304             m_cellEditCtrlEnabled 
= enable
; 
8306             ShowCellEditControl(); 
8310             //FIXME:add veto support 
8311             SendEvent( wxEVT_GRID_EDITOR_HIDDEN 
); 
8313             HideCellEditControl(); 
8314             SaveEditControlValue(); 
8316             // do it after HideCellEditControl() 
8317             m_cellEditCtrlEnabled 
= enable
; 
8322 bool wxGrid::IsCurrentCellReadOnly() const 
8325     wxGridCellAttr
* attr 
= ((wxGrid 
*)this)->GetCellAttr(m_currentCellCoords
); 
8326     bool readonly 
= attr
->IsReadOnly(); 
8332 bool wxGrid::CanEnableCellControl() const 
8334     return m_editable 
&& (m_currentCellCoords 
!= wxGridNoCellCoords
) && 
8335         !IsCurrentCellReadOnly(); 
8338 bool wxGrid::IsCellEditControlEnabled() const 
8340     // the cell edit control might be disable for all cells or just for the 
8341     // current one if it's read only 
8342     return m_cellEditCtrlEnabled 
? !IsCurrentCellReadOnly() : false; 
8345 bool wxGrid::IsCellEditControlShown() const 
8347     bool isShown 
= false; 
8349     if ( m_cellEditCtrlEnabled 
) 
8351         int row 
= m_currentCellCoords
.GetRow(); 
8352         int col 
= m_currentCellCoords
.GetCol(); 
8353         wxGridCellAttr
* attr 
= GetCellAttr(row
, col
); 
8354         wxGridCellEditor
* editor 
= attr
->GetEditor((wxGrid
*) this, row
, col
); 
8359             if ( editor
->IsCreated() ) 
8361                 isShown 
= editor
->GetControl()->IsShown(); 
8371 void wxGrid::ShowCellEditControl() 
8373     if ( IsCellEditControlEnabled() ) 
8375         if ( !IsVisible( m_currentCellCoords
, false ) ) 
8377             m_cellEditCtrlEnabled 
= false; 
8382             wxRect rect 
= CellToRect( m_currentCellCoords 
); 
8383             int row 
= m_currentCellCoords
.GetRow(); 
8384             int col 
= m_currentCellCoords
.GetCol(); 
8386             // if this is part of a multicell, find owner (topleft) 
8387             int cell_rows
, cell_cols
; 
8388             GetCellSize( row
, col
, &cell_rows
, &cell_cols 
); 
8389             if ( cell_rows 
<= 0 || cell_cols 
<= 0 ) 
8393                 m_currentCellCoords
.SetRow( row 
); 
8394                 m_currentCellCoords
.SetCol( col 
); 
8397             // erase the highlight and the cell contents because the editor 
8398             // might not cover the entire cell 
8399             wxClientDC 
dc( m_gridWin 
); 
8401             wxGridCellAttr
* attr 
= GetCellAttr(row
, col
); 
8402             dc
.SetBrush(wxBrush(attr
->GetBackgroundColour(), wxBRUSHSTYLE_SOLID
)); 
8403             dc
.SetPen(*wxTRANSPARENT_PEN
); 
8404             dc
.DrawRectangle(rect
); 
8406             // convert to scrolled coords 
8407             CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y 
); 
8413             // cell is shifted by one pixel 
8414             // However, don't allow x or y to become negative 
8415             // since the SetSize() method interprets that as 
8422             wxGridCellEditor
* editor 
= attr
->GetEditor(this, row
, col
); 
8423             if ( !editor
->IsCreated() ) 
8425                 editor
->Create(m_gridWin
, wxID_ANY
, 
8426                                new wxGridCellEditorEvtHandler(this, editor
)); 
8428                 wxGridEditorCreatedEvent 
evt(GetId(), 
8429                                              wxEVT_GRID_EDITOR_CREATED
, 
8433                                              editor
->GetControl()); 
8434                 GetEventHandler()->ProcessEvent(evt
); 
8437             // resize editor to overflow into righthand cells if allowed 
8438             int maxWidth 
= rect
.width
; 
8439             wxString value 
= GetCellValue(row
, col
); 
8440             if ( (value 
!= wxEmptyString
) && (attr
->GetOverflow()) ) 
8443                 GetTextExtent(value
, &maxWidth
, &y
, NULL
, NULL
, &attr
->GetFont()); 
8444                 if (maxWidth 
< rect
.width
) 
8445                     maxWidth 
= rect
.width
; 
8448             int client_right 
= m_gridWin
->GetClientSize().GetWidth(); 
8449             if (rect
.x 
+ maxWidth 
> client_right
) 
8450                 maxWidth 
= client_right 
- rect
.x
; 
8452             if ((maxWidth 
> rect
.width
) && (col 
< m_numCols
) && m_table
) 
8454                 GetCellSize( row
, col
, &cell_rows
, &cell_cols 
); 
8455                 // may have changed earlier 
8456                 for (int i 
= col 
+ cell_cols
; i 
< m_numCols
; i
++) 
8459                     GetCellSize( row
, i
, &c_rows
, &c_cols 
); 
8461                     // looks weird going over a multicell 
8462                     if (m_table
->IsEmptyCell( row
, i 
) && 
8463                             (rect
.width 
< maxWidth
) && (c_rows 
== 1)) 
8465                         rect
.width 
+= GetColWidth( i 
); 
8471                 if (rect
.GetRight() > client_right
) 
8472                     rect
.SetRight( client_right 
- 1 ); 
8475             editor
->SetCellAttr( attr 
); 
8476             editor
->SetSize( rect 
); 
8478                 editor
->GetControl()->Move( 
8479                     editor
->GetControl()->GetPosition().x 
+ nXMove
, 
8480                     editor
->GetControl()->GetPosition().y 
); 
8481             editor
->Show( true, attr 
); 
8483             // recalc dimensions in case we need to 
8484             // expand the scrolled window to account for editor 
8487             editor
->BeginEdit(row
, col
, this); 
8488             editor
->SetCellAttr(NULL
); 
8496 void wxGrid::HideCellEditControl() 
8498     if ( IsCellEditControlEnabled() ) 
8500         int row 
= m_currentCellCoords
.GetRow(); 
8501         int col 
= m_currentCellCoords
.GetCol(); 
8503         wxGridCellAttr 
*attr 
= GetCellAttr(row
, col
); 
8504         wxGridCellEditor 
*editor 
= attr
->GetEditor(this, row
, col
); 
8505         const bool editorHadFocus 
= editor
->GetControl()->HasFocus(); 
8506         editor
->Show( false ); 
8510         // return the focus to the grid itself if the editor had it 
8512         // note that we must not do this unconditionally to avoid stealing 
8513         // focus from the window which just received it if we are hiding the 
8514         // editor precisely because we lost focus 
8515         if ( editorHadFocus 
) 
8516             m_gridWin
->SetFocus(); 
8518         // refresh whole row to the right 
8519         wxRect 
rect( CellToRect(row
, col
) ); 
8520         CalcScrolledPosition(rect
.x
, rect
.y
, &rect
.x
, &rect
.y 
); 
8521         rect
.width 
= m_gridWin
->GetClientSize().GetWidth() - rect
.x
; 
8524         // ensure that the pixels under the focus ring get refreshed as well 
8525         rect
.Inflate(10, 10); 
8528         m_gridWin
->Refresh( false, &rect 
); 
8532 void wxGrid::SaveEditControlValue() 
8534     if ( IsCellEditControlEnabled() ) 
8536         int row 
= m_currentCellCoords
.GetRow(); 
8537         int col 
= m_currentCellCoords
.GetCol(); 
8539         wxString oldval 
= GetCellValue(row
, col
); 
8541         wxGridCellAttr
* attr 
= GetCellAttr(row
, col
); 
8542         wxGridCellEditor
* editor 
= attr
->GetEditor(this, row
, col
); 
8543         bool changed 
= editor
->EndEdit(row
, col
, this); 
8550             if ( SendEvent( wxEVT_GRID_CELL_CHANGE
, 
8551                        m_currentCellCoords
.GetRow(), 
8552                        m_currentCellCoords
.GetCol() ) < 0 ) 
8554                 // Event has been vetoed, set the data back. 
8555                 SetCellValue(row
, col
, oldval
); 
8562 // ------ Grid location functions 
8563 //  Note that all of these functions work with the logical coordinates of 
8564 //  grid cells and labels so you will need to convert from device 
8565 //  coordinates for mouse events etc. 
8568 void wxGrid::XYToCell( int x
, int y
, wxGridCellCoords
& coords 
) const 
8570     int row 
= YToRow(y
); 
8571     int col 
= XToCol(x
); 
8573     if ( row 
== -1 || col 
== -1 ) 
8575         coords 
= wxGridNoCellCoords
; 
8579         coords
.Set( row
, col 
); 
8583 // Internal Helper function for computing row or column from some 
8584 // (unscrolled) coordinate value, using either 
8585 // m_defaultRowHeight/m_defaultColWidth or binary search on array 
8586 // of m_rowBottoms/m_ColRights to speed up the search! 
8588 static int CoordToRowOrCol(int coord
, int defaultDist
, int minDist
, 
8589                            const wxArrayInt
& BorderArray
, int nMax
, 
8593         return clipToMinMax 
&& (nMax 
> 0) ? 0 : -1; 
8598     size_t i_max 
= coord 
/ defaultDist
, 
8601     if (BorderArray
.IsEmpty()) 
8603         if ((int) i_max 
< nMax
) 
8605         return clipToMinMax 
? nMax 
- 1 : -1; 
8608     if ( i_max 
>= BorderArray
.GetCount()) 
8610         i_max 
= BorderArray
.GetCount() - 1; 
8614         if ( coord 
>= BorderArray
[i_max
]) 
8618                 i_max 
= coord 
/ minDist
; 
8620                 i_max 
=  BorderArray
.GetCount() - 1; 
8623         if ( i_max 
>= BorderArray
.GetCount()) 
8624             i_max 
= BorderArray
.GetCount() - 1; 
8627     if ( coord 
>= BorderArray
[i_max
]) 
8628         return clipToMinMax 
? (int)i_max 
: -1; 
8629     if ( coord 
< BorderArray
[0] ) 
8632     while ( i_max 
- i_min 
> 0 ) 
8634         wxCHECK_MSG(BorderArray
[i_min
] <= coord 
&& coord 
< BorderArray
[i_max
], 
8635                     0, _T("wxGrid: internal error in CoordToRowOrCol")); 
8636         if (coord 
>=  BorderArray
[ i_max 
- 1]) 
8640         int median 
= i_min 
+ (i_max 
- i_min 
+ 1) / 2; 
8641         if (coord 
< BorderArray
[median
]) 
8650 int wxGrid::YToRow( int y 
) const 
8652     return CoordToRowOrCol(y
, m_defaultRowHeight
, 
8653                            m_minAcceptableRowHeight
, m_rowBottoms
, m_numRows
, false); 
8656 int wxGrid::XToCol( int x
, bool clipToMinMax 
) const 
8659         return clipToMinMax 
&& (m_numCols 
> 0) ? GetColAt( 0 ) : -1; 
8661     wxASSERT_MSG(m_defaultColWidth 
> 0, wxT("Default column width can not be zero")); 
8663     int maxPos 
= x 
/ m_defaultColWidth
; 
8666     if (m_colRights
.IsEmpty()) 
8668         if(maxPos 
< m_numCols
) 
8669             return GetColAt( maxPos 
); 
8670         return clipToMinMax 
? GetColAt( m_numCols 
- 1 ) : -1; 
8673     if ( maxPos 
>= m_numCols
) 
8674         maxPos 
= m_numCols 
- 1; 
8677         if ( x 
>= m_colRights
[GetColAt( maxPos 
)]) 
8680             if (m_minAcceptableColWidth
) 
8681                 maxPos 
= x 
/ m_minAcceptableColWidth
; 
8683                 maxPos 
=  m_numCols 
- 1; 
8685         if ( maxPos 
>= m_numCols
) 
8686             maxPos 
= m_numCols 
- 1; 
8689     //X is beyond the last column 
8690     if ( x 
>= m_colRights
[GetColAt( maxPos 
)]) 
8691         return clipToMinMax 
? GetColAt( maxPos 
) : -1; 
8693     //X is before the first column 
8694     if ( x 
< m_colRights
[GetColAt( 0 )] ) 
8695         return GetColAt( 0 ); 
8697     //Perform a binary search 
8698     while ( maxPos 
- minPos 
> 0 ) 
8700         wxCHECK_MSG(m_colRights
[GetColAt( minPos 
)] <= x 
&& x 
< m_colRights
[GetColAt( maxPos 
)], 
8701                     0, _T("wxGrid: internal error in XToCol")); 
8703         if (x 
>=  m_colRights
[GetColAt( maxPos 
- 1 )]) 
8704             return GetColAt( maxPos 
); 
8707         int median 
= minPos 
+ (maxPos 
- minPos 
+ 1) / 2; 
8708         if (x 
< m_colRights
[GetColAt( median 
)]) 
8713     return GetColAt( maxPos 
); 
8716 // return the row number that that the y coord is near 
8717 //  the edge of, or -1 if not near an edge. 
8718 // coords can only possibly be near an edge if 
8719 //    (a) the row/column is large enough to still allow for an "inner" area 
8720 //        that is _not_ nead the edge (i.e., if the height/width is smaller 
8721 //        than WXGRID_LABEL_EDGE_ZONE, coords are _never_ considered to be 
8724 //    (b) resizing rows/columns (the thing for which edge detection is 
8725 //        relevant at all) is enabled. 
8727 int wxGrid::YToEdgeOfRow( int y 
) const 
8730     i 
= internalYToRow(y
); 
8732     if ( GetRowHeight(i
) > WXGRID_LABEL_EDGE_ZONE 
&& CanDragRowSize() ) 
8734         // We know that we are in row i, test whether we are 
8735         // close enough to lower or upper border, respectively. 
8736         if ( abs(GetRowBottom(i
) - y
) < WXGRID_LABEL_EDGE_ZONE 
) 
8738         else if ( i 
> 0 && y 
- GetRowTop(i
) < WXGRID_LABEL_EDGE_ZONE 
) 
8745 // return the col number that that the x coord is near the edge of, or 
8746 // -1 if not near an edge 
8747 // See comment at YToEdgeOfRow for conditions on edge detection. 
8749 int wxGrid::XToEdgeOfCol( int x 
) const 
8752     i 
= internalXToCol(x
); 
8754     if ( GetColWidth(i
) > WXGRID_LABEL_EDGE_ZONE 
&& CanDragColSize() ) 
8756         // We know that we are in column i; test whether we are 
8757         // close enough to right or left border, respectively. 
8758         if ( abs(GetColRight(i
) - x
) < WXGRID_LABEL_EDGE_ZONE 
) 
8760         else if ( i 
> 0 && x 
- GetColLeft(i
) < WXGRID_LABEL_EDGE_ZONE 
) 
8767 wxRect 
wxGrid::CellToRect( int row
, int col 
) const 
8769     wxRect 
rect( -1, -1, -1, -1 ); 
8771     if ( row 
>= 0 && row 
< m_numRows 
&& 
8772          col 
>= 0 && col 
< m_numCols 
) 
8774         int i
, cell_rows
, cell_cols
; 
8775         rect
.width 
= rect
.height 
= 0; 
8776         GetCellSize( row
, col
, &cell_rows
, &cell_cols 
); 
8777         // if negative then find multicell owner 
8782         GetCellSize( row
, col
, &cell_rows
, &cell_cols 
); 
8784         rect
.x 
= GetColLeft(col
); 
8785         rect
.y 
= GetRowTop(row
); 
8786         for (i
=col
; i 
< col 
+ cell_cols
; i
++) 
8787             rect
.width 
+= GetColWidth(i
); 
8788         for (i
=row
; i 
< row 
+ cell_rows
; i
++) 
8789             rect
.height 
+= GetRowHeight(i
); 
8792     // if grid lines are enabled, then the area of the cell is a bit smaller 
8793     if (m_gridLinesEnabled
) 
8802 bool wxGrid::IsVisible( int row
, int col
, bool wholeCellVisible 
) const 
8804     // get the cell rectangle in logical coords 
8806     wxRect 
r( CellToRect( row
, col 
) ); 
8808     // convert to device coords 
8810     int left
, top
, right
, bottom
; 
8811     CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top 
); 
8812     CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom 
); 
8814     // check against the client area of the grid window 
8816     m_gridWin
->GetClientSize( &cw
, &ch 
); 
8818     if ( wholeCellVisible 
) 
8820         // is the cell wholly visible ? 
8821         return ( left 
>= 0 && right 
<= cw 
&& 
8822                  top 
>= 0 && bottom 
<= ch 
); 
8826         // is the cell partly visible ? 
8828         return ( ((left 
>= 0 && left 
< cw
) || (right 
> 0 && right 
<= cw
)) && 
8829                  ((top 
>= 0 && top 
< ch
) || (bottom 
> 0 && bottom 
<= ch
)) ); 
8833 // make the specified cell location visible by doing a minimal amount 
8836 void wxGrid::MakeCellVisible( int row
, int col 
) 
8839     int xpos 
= -1, ypos 
= -1; 
8841     if ( row 
>= 0 && row 
< m_numRows 
&& 
8842          col 
>= 0 && col 
< m_numCols 
) 
8844         // get the cell rectangle in logical coords 
8845         wxRect 
r( CellToRect( row
, col 
) ); 
8847         // convert to device coords 
8848         int left
, top
, right
, bottom
; 
8849         CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top 
); 
8850         CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom 
); 
8853         m_gridWin
->GetClientSize( &cw
, &ch 
); 
8859         else if ( bottom 
> ch 
) 
8861             int h 
= r
.GetHeight(); 
8863             for ( i 
= row 
- 1; i 
>= 0; i
-- ) 
8865                 int rowHeight 
= GetRowHeight(i
); 
8866                 if ( h 
+ rowHeight 
> ch 
) 
8873             // we divide it later by GRID_SCROLL_LINE, make sure that we don't 
8874             // have rounding errors (this is important, because if we do, 
8875             // we might not scroll at all and some cells won't be redrawn) 
8877             // Sometimes GRID_SCROLL_LINE / 2 is not enough, 
8878             // so just add a full scroll unit... 
8879             ypos 
+= m_scrollLineY
; 
8882         // special handling for wide cells - show always left part of the cell! 
8883         // Otherwise, e.g. when stepping from row to row, it would jump between 
8884         // left and right part of the cell on every step! 
8886         if ( left 
< 0 || (right 
- left
) >= cw 
) 
8890         else if ( right 
> cw 
) 
8892             // position the view so that the cell is on the right 
8894             CalcUnscrolledPosition(0, 0, &x0
, &y0
); 
8895             xpos 
= x0 
+ (right 
- cw
); 
8897             // see comment for ypos above 
8898             xpos 
+= m_scrollLineX
; 
8901         if ( xpos 
!= -1 || ypos 
!= -1 ) 
8904                 xpos 
/= m_scrollLineX
; 
8906                 ypos 
/= m_scrollLineY
; 
8907             Scroll( xpos
, ypos 
); 
8914 // ------ Grid cursor movement functions 
8917 bool wxGrid::MoveCursorUp( bool expandSelection 
) 
8919     if ( m_currentCellCoords 
!= wxGridNoCellCoords 
&& 
8920          m_currentCellCoords
.GetRow() >= 0 ) 
8922         if ( expandSelection 
) 
8924             if ( m_selectingKeyboard 
== wxGridNoCellCoords 
) 
8925                 m_selectingKeyboard 
= m_currentCellCoords
; 
8926             if ( m_selectingKeyboard
.GetRow() > 0 ) 
8928                 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() - 1 ); 
8929                 MakeCellVisible( m_selectingKeyboard
.GetRow(), 
8930                                  m_selectingKeyboard
.GetCol() ); 
8931                 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard 
); 
8934         else if ( m_currentCellCoords
.GetRow() > 0 ) 
8936             int row 
= m_currentCellCoords
.GetRow() - 1; 
8937             int col 
= m_currentCellCoords
.GetCol(); 
8939             MakeCellVisible( row
, col 
); 
8940             SetCurrentCell( row
, col 
); 
8951 bool wxGrid::MoveCursorDown( bool expandSelection 
) 
8953     if ( m_currentCellCoords 
!= wxGridNoCellCoords 
&& 
8954          m_currentCellCoords
.GetRow() < m_numRows 
) 
8956         if ( expandSelection 
) 
8958             if ( m_selectingKeyboard 
== wxGridNoCellCoords 
) 
8959                 m_selectingKeyboard 
= m_currentCellCoords
; 
8960             if ( m_selectingKeyboard
.GetRow() < m_numRows 
- 1 ) 
8962                 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() + 1 ); 
8963                 MakeCellVisible( m_selectingKeyboard
.GetRow(), 
8964                         m_selectingKeyboard
.GetCol() ); 
8965                 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard 
); 
8968         else if ( m_currentCellCoords
.GetRow() < m_numRows 
- 1 ) 
8970             int row 
= m_currentCellCoords
.GetRow() + 1; 
8971             int col 
= m_currentCellCoords
.GetCol(); 
8973             MakeCellVisible( row
, col 
); 
8974             SetCurrentCell( row
, col 
); 
8985 bool wxGrid::MoveCursorLeft( bool expandSelection 
) 
8987     if ( m_currentCellCoords 
!= wxGridNoCellCoords 
&& 
8988          m_currentCellCoords
.GetCol() >= 0 ) 
8990         if ( expandSelection 
) 
8992             if ( m_selectingKeyboard 
== wxGridNoCellCoords 
) 
8993                 m_selectingKeyboard 
= m_currentCellCoords
; 
8994             if ( m_selectingKeyboard
.GetCol() > 0 ) 
8996                 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() - 1 ); 
8997                 MakeCellVisible( m_selectingKeyboard
.GetRow(), 
8998                         m_selectingKeyboard
.GetCol() ); 
8999                 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard 
); 
9002         else if ( GetColPos( m_currentCellCoords
.GetCol() ) > 0 ) 
9004             int row 
= m_currentCellCoords
.GetRow(); 
9005             int col 
= GetColAt( GetColPos( m_currentCellCoords
.GetCol() ) - 1 ); 
9008             MakeCellVisible( row
, col 
); 
9009             SetCurrentCell( row
, col 
); 
9020 bool wxGrid::MoveCursorRight( bool expandSelection 
) 
9022     if ( m_currentCellCoords 
!= wxGridNoCellCoords 
&& 
9023          m_currentCellCoords
.GetCol() < m_numCols 
) 
9025         if ( expandSelection 
) 
9027             if ( m_selectingKeyboard 
== wxGridNoCellCoords 
) 
9028                 m_selectingKeyboard 
= m_currentCellCoords
; 
9029             if ( m_selectingKeyboard
.GetCol() < m_numCols 
- 1 ) 
9031                 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() + 1 ); 
9032                 MakeCellVisible( m_selectingKeyboard
.GetRow(), 
9033                         m_selectingKeyboard
.GetCol() ); 
9034                 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard 
); 
9037         else if ( GetColPos( m_currentCellCoords
.GetCol() ) < m_numCols 
- 1 ) 
9039             int row 
= m_currentCellCoords
.GetRow(); 
9040             int col 
= GetColAt( GetColPos( m_currentCellCoords
.GetCol() ) + 1 ); 
9043             MakeCellVisible( row
, col 
); 
9044             SetCurrentCell( row
, col 
); 
9055 bool wxGrid::MovePageUp() 
9057     if ( m_currentCellCoords 
== wxGridNoCellCoords 
) 
9060     int row 
= m_currentCellCoords
.GetRow(); 
9064         m_gridWin
->GetClientSize( &cw
, &ch 
); 
9066         int y 
= GetRowTop(row
); 
9067         int newRow 
= internalYToRow( y 
- ch 
+ 1 ); 
9069         if ( newRow 
== row 
) 
9071             // row > 0, so newRow can never be less than 0 here. 
9075         MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() ); 
9076         SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() ); 
9084 bool wxGrid::MovePageDown() 
9086     if ( m_currentCellCoords 
== wxGridNoCellCoords 
) 
9089     int row 
= m_currentCellCoords
.GetRow(); 
9090     if ( (row 
+ 1) < m_numRows 
) 
9093         m_gridWin
->GetClientSize( &cw
, &ch 
); 
9095         int y 
= GetRowTop(row
); 
9096         int newRow 
= internalYToRow( y 
+ ch 
); 
9097         if ( newRow 
== row 
) 
9099             // row < m_numRows, so newRow can't overflow here. 
9103         MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() ); 
9104         SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() ); 
9112 bool wxGrid::MoveCursorUpBlock( bool expandSelection 
) 
9115          m_currentCellCoords 
!= wxGridNoCellCoords 
&& 
9116          m_currentCellCoords
.GetRow() > 0 ) 
9118         int row 
= m_currentCellCoords
.GetRow(); 
9119         int col 
= m_currentCellCoords
.GetCol(); 
9121         if ( m_table
->IsEmptyCell(row
, col
) ) 
9123             // starting in an empty cell: find the next block of 
9129                 if ( !(m_table
->IsEmptyCell(row
, col
)) ) 
9133         else if ( m_table
->IsEmptyCell(row 
- 1, col
) ) 
9135             // starting at the top of a block: find the next block 
9141                 if ( !(m_table
->IsEmptyCell(row
, col
)) ) 
9147             // starting within a block: find the top of the block 
9152                 if ( m_table
->IsEmptyCell(row
, col
) ) 
9160         MakeCellVisible( row
, col 
); 
9161         if ( expandSelection 
) 
9163             m_selectingKeyboard 
= wxGridCellCoords( row
, col 
); 
9164             HighlightBlock( m_currentCellCoords
, m_selectingKeyboard 
); 
9169             SetCurrentCell( row
, col 
); 
9178 bool wxGrid::MoveCursorDownBlock( bool expandSelection 
) 
9181          m_currentCellCoords 
!= wxGridNoCellCoords 
&& 
9182          m_currentCellCoords
.GetRow() < m_numRows 
- 1 ) 
9184         int row 
= m_currentCellCoords
.GetRow(); 
9185         int col 
= m_currentCellCoords
.GetCol(); 
9187         if ( m_table
->IsEmptyCell(row
, col
) ) 
9189             // starting in an empty cell: find the next block of 
9192             while ( row 
< m_numRows 
- 1 ) 
9195                 if ( !(m_table
->IsEmptyCell(row
, col
)) ) 
9199         else if ( m_table
->IsEmptyCell(row 
+ 1, col
) ) 
9201             // starting at the bottom of a block: find the next block 
9204             while ( row 
< m_numRows 
- 1 ) 
9207                 if ( !(m_table
->IsEmptyCell(row
, col
)) ) 
9213             // starting within a block: find the bottom of the block 
9215             while ( row 
< m_numRows 
- 1 ) 
9218                 if ( m_table
->IsEmptyCell(row
, col
) ) 
9226         MakeCellVisible( row
, col 
); 
9227         if ( expandSelection 
) 
9229             m_selectingKeyboard 
= wxGridCellCoords( row
, col 
); 
9230             HighlightBlock( m_currentCellCoords
, m_selectingKeyboard 
); 
9235             SetCurrentCell( row
, col 
); 
9244 bool wxGrid::MoveCursorLeftBlock( bool expandSelection 
) 
9247          m_currentCellCoords 
!= wxGridNoCellCoords 
&& 
9248          m_currentCellCoords
.GetCol() > 0 ) 
9250         int row 
= m_currentCellCoords
.GetRow(); 
9251         int col 
= m_currentCellCoords
.GetCol(); 
9253         if ( m_table
->IsEmptyCell(row
, col
) ) 
9255             // starting in an empty cell: find the next block of 
9261                 if ( !(m_table
->IsEmptyCell(row
, col
)) ) 
9265         else if ( m_table
->IsEmptyCell(row
, col 
- 1) ) 
9267             // starting at the left of a block: find the next block 
9273                 if ( !(m_table
->IsEmptyCell(row
, col
)) ) 
9279             // starting within a block: find the left of the block 
9284                 if ( m_table
->IsEmptyCell(row
, col
) ) 
9292         MakeCellVisible( row
, col 
); 
9293         if ( expandSelection 
) 
9295             m_selectingKeyboard 
= wxGridCellCoords( row
, col 
); 
9296             HighlightBlock( m_currentCellCoords
, m_selectingKeyboard 
); 
9301             SetCurrentCell( row
, col 
); 
9310 bool wxGrid::MoveCursorRightBlock( bool expandSelection 
) 
9313          m_currentCellCoords 
!= wxGridNoCellCoords 
&& 
9314          m_currentCellCoords
.GetCol() < m_numCols 
- 1 ) 
9316         int row 
= m_currentCellCoords
.GetRow(); 
9317         int col 
= m_currentCellCoords
.GetCol(); 
9319         if ( m_table
->IsEmptyCell(row
, col
) ) 
9321             // starting in an empty cell: find the next block of 
9324             while ( col 
< m_numCols 
- 1 ) 
9327                 if ( !(m_table
->IsEmptyCell(row
, col
)) ) 
9331         else if ( m_table
->IsEmptyCell(row
, col 
+ 1) ) 
9333             // starting at the right of a block: find the next block 
9336             while ( col 
< m_numCols 
- 1 ) 
9339                 if ( !(m_table
->IsEmptyCell(row
, col
)) ) 
9345             // starting within a block: find the right of the block 
9347             while ( col 
< m_numCols 
- 1 ) 
9350                 if ( m_table
->IsEmptyCell(row
, col
) ) 
9358         MakeCellVisible( row
, col 
); 
9359         if ( expandSelection 
) 
9361             m_selectingKeyboard 
= wxGridCellCoords( row
, col 
); 
9362             HighlightBlock( m_currentCellCoords
, m_selectingKeyboard 
); 
9367             SetCurrentCell( row
, col 
); 
9377 // ------ Label values and formatting 
9380 void wxGrid::GetRowLabelAlignment( int *horiz
, int *vert 
) const 
9383         *horiz 
= m_rowLabelHorizAlign
; 
9385         *vert  
= m_rowLabelVertAlign
; 
9388 void wxGrid::GetColLabelAlignment( int *horiz
, int *vert 
) const 
9391         *horiz 
= m_colLabelHorizAlign
; 
9393         *vert  
= m_colLabelVertAlign
; 
9396 int wxGrid::GetColLabelTextOrientation() const 
9398     return m_colLabelTextOrientation
; 
9401 wxString 
wxGrid::GetRowLabelValue( int row 
) const 
9405         return m_table
->GetRowLabelValue( row 
); 
9415 wxString 
wxGrid::GetColLabelValue( int col 
) const 
9419         return m_table
->GetColLabelValue( col 
); 
9429 void wxGrid::SetRowLabelSize( int width 
) 
9431     wxASSERT( width 
>= 0 || width 
== wxGRID_AUTOSIZE 
); 
9433     if ( width 
== wxGRID_AUTOSIZE 
) 
9435         width 
= CalcColOrRowLabelAreaMinSize(wxGRID_ROW
); 
9438     if ( width 
!= m_rowLabelWidth 
) 
9442             m_rowLabelWin
->Show( false ); 
9443             m_cornerLabelWin
->Show( false ); 
9445         else if ( m_rowLabelWidth 
== 0 ) 
9447             m_rowLabelWin
->Show( true ); 
9448             if ( m_colLabelHeight 
> 0 ) 
9449                 m_cornerLabelWin
->Show( true ); 
9452         m_rowLabelWidth 
= width
; 
9454         wxScrolledWindow::Refresh( true ); 
9458 void wxGrid::SetColLabelSize( int height 
) 
9460     wxASSERT( height 
>=0 || height 
== wxGRID_AUTOSIZE 
); 
9462     if ( height 
== wxGRID_AUTOSIZE 
) 
9464         height 
= CalcColOrRowLabelAreaMinSize(wxGRID_COLUMN
); 
9467     if ( height 
!= m_colLabelHeight 
) 
9471             m_colLabelWin
->Show( false ); 
9472             m_cornerLabelWin
->Show( false ); 
9474         else if ( m_colLabelHeight 
== 0 ) 
9476             m_colLabelWin
->Show( true ); 
9477             if ( m_rowLabelWidth 
> 0 ) 
9478                 m_cornerLabelWin
->Show( true ); 
9481         m_colLabelHeight 
= height
; 
9483         wxScrolledWindow::Refresh( true ); 
9487 void wxGrid::SetLabelBackgroundColour( const wxColour
& colour 
) 
9489     if ( m_labelBackgroundColour 
!= colour 
) 
9491         m_labelBackgroundColour 
= colour
; 
9492         m_rowLabelWin
->SetBackgroundColour( colour 
); 
9493         m_colLabelWin
->SetBackgroundColour( colour 
); 
9494         m_cornerLabelWin
->SetBackgroundColour( colour 
); 
9496         if ( !GetBatchCount() ) 
9498             m_rowLabelWin
->Refresh(); 
9499             m_colLabelWin
->Refresh(); 
9500             m_cornerLabelWin
->Refresh(); 
9505 void wxGrid::SetLabelTextColour( const wxColour
& colour 
) 
9507     if ( m_labelTextColour 
!= colour 
) 
9509         m_labelTextColour 
= colour
; 
9510         if ( !GetBatchCount() ) 
9512             m_rowLabelWin
->Refresh(); 
9513             m_colLabelWin
->Refresh(); 
9518 void wxGrid::SetLabelFont( const wxFont
& font 
) 
9521     if ( !GetBatchCount() ) 
9523         m_rowLabelWin
->Refresh(); 
9524         m_colLabelWin
->Refresh(); 
9528 void wxGrid::SetRowLabelAlignment( int horiz
, int vert 
) 
9530     // allow old (incorrect) defs to be used 
9533         case wxLEFT
:   horiz 
= wxALIGN_LEFT
; break; 
9534         case wxRIGHT
:  horiz 
= wxALIGN_RIGHT
; break; 
9535         case wxCENTRE
: horiz 
= wxALIGN_CENTRE
; break; 
9540         case wxTOP
:    vert 
= wxALIGN_TOP
;    break; 
9541         case wxBOTTOM
: vert 
= wxALIGN_BOTTOM
; break; 
9542         case wxCENTRE
: vert 
= wxALIGN_CENTRE
; break; 
9545     if ( horiz 
== wxALIGN_LEFT 
|| horiz 
== wxALIGN_CENTRE 
|| horiz 
== wxALIGN_RIGHT 
) 
9547         m_rowLabelHorizAlign 
= horiz
; 
9550     if ( vert 
== wxALIGN_TOP 
|| vert 
== wxALIGN_CENTRE 
|| vert 
== wxALIGN_BOTTOM 
) 
9552         m_rowLabelVertAlign 
= vert
; 
9555     if ( !GetBatchCount() ) 
9557         m_rowLabelWin
->Refresh(); 
9561 void wxGrid::SetColLabelAlignment( int horiz
, int vert 
) 
9563     // allow old (incorrect) defs to be used 
9566         case wxLEFT
:   horiz 
= wxALIGN_LEFT
; break; 
9567         case wxRIGHT
:  horiz 
= wxALIGN_RIGHT
; break; 
9568         case wxCENTRE
: horiz 
= wxALIGN_CENTRE
; break; 
9573         case wxTOP
:    vert 
= wxALIGN_TOP
;    break; 
9574         case wxBOTTOM
: vert 
= wxALIGN_BOTTOM
; break; 
9575         case wxCENTRE
: vert 
= wxALIGN_CENTRE
; break; 
9578     if ( horiz 
== wxALIGN_LEFT 
|| horiz 
== wxALIGN_CENTRE 
|| horiz 
== wxALIGN_RIGHT 
) 
9580         m_colLabelHorizAlign 
= horiz
; 
9583     if ( vert 
== wxALIGN_TOP 
|| vert 
== wxALIGN_CENTRE 
|| vert 
== wxALIGN_BOTTOM 
) 
9585         m_colLabelVertAlign 
= vert
; 
9588     if ( !GetBatchCount() ) 
9590         m_colLabelWin
->Refresh(); 
9594 // Note: under MSW, the default column label font must be changed because it 
9595 //       does not support vertical printing 
9597 // Example: wxFont font(9, wxSWISS, wxNORMAL, wxBOLD); 
9598 //                      pGrid->SetLabelFont(font); 
9599 //                      pGrid->SetColLabelTextOrientation(wxVERTICAL); 
9601 void wxGrid::SetColLabelTextOrientation( int textOrientation 
) 
9603     if ( textOrientation 
== wxHORIZONTAL 
|| textOrientation 
== wxVERTICAL 
) 
9604         m_colLabelTextOrientation 
= textOrientation
; 
9606     if ( !GetBatchCount() ) 
9607         m_colLabelWin
->Refresh(); 
9610 void wxGrid::SetRowLabelValue( int row
, const wxString
& s 
) 
9614         m_table
->SetRowLabelValue( row
, s 
); 
9615         if ( !GetBatchCount() ) 
9617             wxRect rect 
= CellToRect( row
, 0 ); 
9618             if ( rect
.height 
> 0 ) 
9620                 CalcScrolledPosition(0, rect
.y
, &rect
.x
, &rect
.y
); 
9622                 rect
.width 
= m_rowLabelWidth
; 
9623                 m_rowLabelWin
->Refresh( true, &rect 
); 
9629 void wxGrid::SetColLabelValue( int col
, const wxString
& s 
) 
9633         m_table
->SetColLabelValue( col
, s 
); 
9634         if ( !GetBatchCount() ) 
9636             wxRect rect 
= CellToRect( 0, col 
); 
9637             if ( rect
.width 
> 0 ) 
9639                 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &rect
.y
); 
9641                 rect
.height 
= m_colLabelHeight
; 
9642                 m_colLabelWin
->Refresh( true, &rect 
); 
9648 void wxGrid::SetGridLineColour( const wxColour
& colour 
) 
9650     if ( m_gridLineColour 
!= colour 
) 
9652         m_gridLineColour 
= colour
; 
9654         wxClientDC 
dc( m_gridWin 
); 
9656         DrawAllGridLines( dc
, wxRegion() ); 
9660 void wxGrid::SetCellHighlightColour( const wxColour
& colour 
) 
9662     if ( m_cellHighlightColour 
!= colour 
) 
9664         m_cellHighlightColour 
= colour
; 
9666         wxClientDC 
dc( m_gridWin 
); 
9668         wxGridCellAttr
* attr 
= GetCellAttr(m_currentCellCoords
); 
9669         DrawCellHighlight(dc
, attr
); 
9674 void wxGrid::SetCellHighlightPenWidth(int width
) 
9676     if (m_cellHighlightPenWidth 
!= width
) 
9678         m_cellHighlightPenWidth 
= width
; 
9680         // Just redrawing the cell highlight is not enough since that won't 
9681         // make any visible change if the the thickness is getting smaller. 
9682         int row 
= m_currentCellCoords
.GetRow(); 
9683         int col 
= m_currentCellCoords
.GetCol(); 
9684         if ( row 
== -1 || col 
== -1 || GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 ) 
9687         wxRect rect 
= CellToRect(row
, col
); 
9688         m_gridWin
->Refresh(true, &rect
); 
9692 void wxGrid::SetCellHighlightROPenWidth(int width
) 
9694     if (m_cellHighlightROPenWidth 
!= width
) 
9696         m_cellHighlightROPenWidth 
= width
; 
9698         // Just redrawing the cell highlight is not enough since that won't 
9699         // make any visible change if the the thickness is getting smaller. 
9700         int row 
= m_currentCellCoords
.GetRow(); 
9701         int col 
= m_currentCellCoords
.GetCol(); 
9702         if ( row 
== -1 || col 
== -1 || 
9703                 GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 ) 
9706         wxRect rect 
= CellToRect(row
, col
); 
9707         m_gridWin
->Refresh(true, &rect
); 
9711 void wxGrid::EnableGridLines( bool enable 
) 
9713     if ( enable 
!= m_gridLinesEnabled 
) 
9715         m_gridLinesEnabled 
= enable
; 
9717         if ( !GetBatchCount() ) 
9721                 wxClientDC 
dc( m_gridWin 
); 
9723                 DrawAllGridLines( dc
, wxRegion() ); 
9727                 m_gridWin
->Refresh(); 
9733 int wxGrid::GetDefaultRowSize() const 
9735     return m_defaultRowHeight
; 
9738 int wxGrid::GetRowSize( int row 
) const 
9740     wxCHECK_MSG( row 
>= 0 && row 
< m_numRows
, 0, _T("invalid row index") ); 
9742     return GetRowHeight(row
); 
9745 int wxGrid::GetDefaultColSize() const 
9747     return m_defaultColWidth
; 
9750 int wxGrid::GetColSize( int col 
) const 
9752     wxCHECK_MSG( col 
>= 0 && col 
< m_numCols
, 0, _T("invalid column index") ); 
9754     return GetColWidth(col
); 
9757 // ============================================================================ 
9758 // access to the grid attributes: each of them has a default value in the grid 
9759 // itself and may be overidden on a per-cell basis 
9760 // ============================================================================ 
9762 // ---------------------------------------------------------------------------- 
9763 // setting default attributes 
9764 // ---------------------------------------------------------------------------- 
9766 void wxGrid::SetDefaultCellBackgroundColour( const wxColour
& col 
) 
9768     m_defaultCellAttr
->SetBackgroundColour(col
); 
9770     m_gridWin
->SetBackgroundColour(col
); 
9774 void wxGrid::SetDefaultCellTextColour( const wxColour
& col 
) 
9776     m_defaultCellAttr
->SetTextColour(col
); 
9779 void wxGrid::SetDefaultCellAlignment( int horiz
, int vert 
) 
9781     m_defaultCellAttr
->SetAlignment(horiz
, vert
); 
9784 void wxGrid::SetDefaultCellOverflow( bool allow 
) 
9786     m_defaultCellAttr
->SetOverflow(allow
); 
9789 void wxGrid::SetDefaultCellFont( const wxFont
& font 
) 
9791     m_defaultCellAttr
->SetFont(font
); 
9794 // For editors and renderers the type registry takes precedence over the 
9795 // default attr, so we need to register the new editor/renderer for the string 
9796 // data type in order to make setting a default editor/renderer appear to 
9799 void wxGrid::SetDefaultRenderer(wxGridCellRenderer 
*renderer
) 
9801     RegisterDataType(wxGRID_VALUE_STRING
, 
9803                      GetDefaultEditorForType(wxGRID_VALUE_STRING
)); 
9806 void wxGrid::SetDefaultEditor(wxGridCellEditor 
*editor
) 
9808     RegisterDataType(wxGRID_VALUE_STRING
, 
9809                      GetDefaultRendererForType(wxGRID_VALUE_STRING
), 
9813 // ---------------------------------------------------------------------------- 
9814 // access to the default attributes 
9815 // ---------------------------------------------------------------------------- 
9817 wxColour 
wxGrid::GetDefaultCellBackgroundColour() const 
9819     return m_defaultCellAttr
->GetBackgroundColour(); 
9822 wxColour 
wxGrid::GetDefaultCellTextColour() const 
9824     return m_defaultCellAttr
->GetTextColour(); 
9827 wxFont 
wxGrid::GetDefaultCellFont() const 
9829     return m_defaultCellAttr
->GetFont(); 
9832 void wxGrid::GetDefaultCellAlignment( int *horiz
, int *vert 
) const 
9834     m_defaultCellAttr
->GetAlignment(horiz
, vert
); 
9837 bool wxGrid::GetDefaultCellOverflow() const 
9839     return m_defaultCellAttr
->GetOverflow(); 
9842 wxGridCellRenderer 
*wxGrid::GetDefaultRenderer() const 
9844     return m_defaultCellAttr
->GetRenderer(NULL
, 0, 0); 
9847 wxGridCellEditor 
*wxGrid::GetDefaultEditor() const 
9849     return m_defaultCellAttr
->GetEditor(NULL
, 0, 0); 
9852 // ---------------------------------------------------------------------------- 
9853 // access to cell attributes 
9854 // ---------------------------------------------------------------------------- 
9856 wxColour 
wxGrid::GetCellBackgroundColour(int row
, int col
) const 
9858     wxGridCellAttr 
*attr 
= GetCellAttr(row
, col
); 
9859     wxColour colour 
= attr
->GetBackgroundColour(); 
9865 wxColour 
wxGrid::GetCellTextColour( int row
, int col 
) const 
9867     wxGridCellAttr 
*attr 
= GetCellAttr(row
, col
); 
9868     wxColour colour 
= attr
->GetTextColour(); 
9874 wxFont 
wxGrid::GetCellFont( int row
, int col 
) const 
9876     wxGridCellAttr 
*attr 
= GetCellAttr(row
, col
); 
9877     wxFont font 
= attr
->GetFont(); 
9883 void wxGrid::GetCellAlignment( int row
, int col
, int *horiz
, int *vert 
) const 
9885     wxGridCellAttr 
*attr 
= GetCellAttr(row
, col
); 
9886     attr
->GetAlignment(horiz
, vert
); 
9890 bool wxGrid::GetCellOverflow( int row
, int col 
) const 
9892     wxGridCellAttr 
*attr 
= GetCellAttr(row
, col
); 
9893     bool allow 
= attr
->GetOverflow(); 
9899 void wxGrid::GetCellSize( int row
, int col
, int *num_rows
, int *num_cols 
) const 
9901     wxGridCellAttr 
*attr 
= GetCellAttr(row
, col
); 
9902     attr
->GetSize( num_rows
, num_cols 
); 
9906 wxGridCellRenderer
* wxGrid::GetCellRenderer(int row
, int col
) const 
9908     wxGridCellAttr
* attr 
= GetCellAttr(row
, col
); 
9909     wxGridCellRenderer
* renderer 
= attr
->GetRenderer(this, row
, col
); 
9915 wxGridCellEditor
* wxGrid::GetCellEditor(int row
, int col
) const 
9917     wxGridCellAttr
* attr 
= GetCellAttr(row
, col
); 
9918     wxGridCellEditor
* editor 
= attr
->GetEditor(this, row
, col
); 
9924 bool wxGrid::IsReadOnly(int row
, int col
) const 
9926     wxGridCellAttr
* attr 
= GetCellAttr(row
, col
); 
9927     bool isReadOnly 
= attr
->IsReadOnly(); 
9933 // ---------------------------------------------------------------------------- 
9934 // attribute support: cache, automatic provider creation, ... 
9935 // ---------------------------------------------------------------------------- 
9937 bool wxGrid::CanHaveAttributes() const 
9944     return m_table
->CanHaveAttributes(); 
9947 void wxGrid::ClearAttrCache() 
9949     if ( m_attrCache
.row 
!= -1 ) 
9951         wxGridCellAttr 
*oldAttr 
= m_attrCache
.attr
; 
9952         m_attrCache
.attr 
= NULL
; 
9953         m_attrCache
.row 
= -1; 
9954         // wxSafeDecRec(...) might cause event processing that accesses 
9955         // the cached attribute, if one exists (e.g. by deleting the 
9956         // editor stored within the attribute). Therefore it is important 
9957         // to invalidate the cache  before calling wxSafeDecRef! 
9958         wxSafeDecRef(oldAttr
); 
9962 void wxGrid::CacheAttr(int row
, int col
, wxGridCellAttr 
*attr
) const 
9966         wxGrid 
*self 
= (wxGrid 
*)this;  // const_cast 
9968         self
->ClearAttrCache(); 
9969         self
->m_attrCache
.row 
= row
; 
9970         self
->m_attrCache
.col 
= col
; 
9971         self
->m_attrCache
.attr 
= attr
; 
9976 bool wxGrid::LookupAttr(int row
, int col
, wxGridCellAttr 
**attr
) const 
9978     if ( row 
== m_attrCache
.row 
&& col 
== m_attrCache
.col 
) 
9980         *attr 
= m_attrCache
.attr
; 
9981         wxSafeIncRef(m_attrCache
.attr
); 
9983 #ifdef DEBUG_ATTR_CACHE 
9984         gs_nAttrCacheHits
++; 
9991 #ifdef DEBUG_ATTR_CACHE 
9992         gs_nAttrCacheMisses
++; 
9999 wxGridCellAttr 
*wxGrid::GetCellAttr(int row
, int col
) const 
10001     wxGridCellAttr 
*attr 
= NULL
; 
10002     // Additional test to avoid looking at the cache e.g. for 
10003     // wxNoCellCoords, as this will confuse memory management. 
10006         if ( !LookupAttr(row
, col
, &attr
) ) 
10008             attr 
= m_table 
? m_table
->GetAttr(row
, col
, wxGridCellAttr::Any
) 
10009                            : (wxGridCellAttr 
*)NULL
; 
10010             CacheAttr(row
, col
, attr
); 
10016         attr
->SetDefAttr(m_defaultCellAttr
); 
10020         attr 
= m_defaultCellAttr
; 
10027 wxGridCellAttr 
*wxGrid::GetOrCreateCellAttr(int row
, int col
) const 
10029     wxGridCellAttr 
*attr 
= (wxGridCellAttr 
*)NULL
; 
10030     bool canHave 
= ((wxGrid
*)this)->CanHaveAttributes(); 
10032     wxCHECK_MSG( canHave
, attr
, _T("Cell attributes not allowed")); 
10033     wxCHECK_MSG( m_table
, attr
, _T("must have a table") ); 
10035     attr 
= m_table
->GetAttr(row
, col
, wxGridCellAttr::Cell
); 
10038         attr 
= new wxGridCellAttr(m_defaultCellAttr
); 
10040         // artificially inc the ref count to match DecRef() in caller 
10042         m_table
->SetAttr(attr
, row
, col
); 
10048 // ---------------------------------------------------------------------------- 
10049 // setting column attributes (wrappers around SetColAttr) 
10050 // ---------------------------------------------------------------------------- 
10052 void wxGrid::SetColFormatBool(int col
) 
10054     SetColFormatCustom(col
, wxGRID_VALUE_BOOL
); 
10057 void wxGrid::SetColFormatNumber(int col
) 
10059     SetColFormatCustom(col
, wxGRID_VALUE_NUMBER
); 
10062 void wxGrid::SetColFormatFloat(int col
, int width
, int precision
) 
10064     wxString typeName 
= wxGRID_VALUE_FLOAT
; 
10065     if ( (width 
!= -1) || (precision 
!= -1) ) 
10067         typeName 
<< _T(':') << width 
<< _T(',') << precision
; 
10070     SetColFormatCustom(col
, typeName
); 
10073 void wxGrid::SetColFormatCustom(int col
, const wxString
& typeName
) 
10075     wxGridCellAttr 
*attr 
= m_table
->GetAttr(-1, col
, wxGridCellAttr::Col 
); 
10077         attr 
= new wxGridCellAttr
; 
10078     wxGridCellRenderer 
*renderer 
= GetDefaultRendererForType(typeName
); 
10079     attr
->SetRenderer(renderer
); 
10080     wxGridCellEditor 
*editor 
= GetDefaultEditorForType(typeName
); 
10081     attr
->SetEditor(editor
); 
10083     SetColAttr(col
, attr
); 
10087 // ---------------------------------------------------------------------------- 
10088 // setting cell attributes: this is forwarded to the table 
10089 // ---------------------------------------------------------------------------- 
10091 void wxGrid::SetAttr(int row
, int col
, wxGridCellAttr 
*attr
) 
10093     if ( CanHaveAttributes() ) 
10095         m_table
->SetAttr(attr
, row
, col
); 
10100         wxSafeDecRef(attr
); 
10104 void wxGrid::SetRowAttr(int row
, wxGridCellAttr 
*attr
) 
10106     if ( CanHaveAttributes() ) 
10108         m_table
->SetRowAttr(attr
, row
); 
10113         wxSafeDecRef(attr
); 
10117 void wxGrid::SetColAttr(int col
, wxGridCellAttr 
*attr
) 
10119     if ( CanHaveAttributes() ) 
10121         m_table
->SetColAttr(attr
, col
); 
10126         wxSafeDecRef(attr
); 
10130 void wxGrid::SetCellBackgroundColour( int row
, int col
, const wxColour
& colour 
) 
10132     if ( CanHaveAttributes() ) 
10134         wxGridCellAttr 
*attr 
= GetOrCreateCellAttr(row
, col
); 
10135         attr
->SetBackgroundColour(colour
); 
10140 void wxGrid::SetCellTextColour( int row
, int col
, const wxColour
& colour 
) 
10142     if ( CanHaveAttributes() ) 
10144         wxGridCellAttr 
*attr 
= GetOrCreateCellAttr(row
, col
); 
10145         attr
->SetTextColour(colour
); 
10150 void wxGrid::SetCellFont( int row
, int col
, const wxFont
& font 
) 
10152     if ( CanHaveAttributes() ) 
10154         wxGridCellAttr 
*attr 
= GetOrCreateCellAttr(row
, col
); 
10155         attr
->SetFont(font
); 
10160 void wxGrid::SetCellAlignment( int row
, int col
, int horiz
, int vert 
) 
10162     if ( CanHaveAttributes() ) 
10164         wxGridCellAttr 
*attr 
= GetOrCreateCellAttr(row
, col
); 
10165         attr
->SetAlignment(horiz
, vert
); 
10170 void wxGrid::SetCellOverflow( int row
, int col
, bool allow 
) 
10172     if ( CanHaveAttributes() ) 
10174         wxGridCellAttr 
*attr 
= GetOrCreateCellAttr(row
, col
); 
10175         attr
->SetOverflow(allow
); 
10180 void wxGrid::SetCellSize( int row
, int col
, int num_rows
, int num_cols 
) 
10182     if ( CanHaveAttributes() ) 
10184         int cell_rows
, cell_cols
; 
10186         wxGridCellAttr 
*attr 
= GetOrCreateCellAttr(row
, col
); 
10187         attr
->GetSize(&cell_rows
, &cell_cols
); 
10188         attr
->SetSize(num_rows
, num_cols
); 
10191         // Cannot set the size of a cell to 0 or negative values 
10192         // While it is perfectly legal to do that, this function cannot 
10193         // handle all the possibilies, do it by hand by getting the CellAttr. 
10194         // You can only set the size of a cell to 1,1 or greater with this fn 
10195         wxASSERT_MSG( !((cell_rows 
< 1) || (cell_cols 
< 1)), 
10196                       wxT("wxGrid::SetCellSize setting cell size that is already part of another cell")); 
10197         wxASSERT_MSG( !((num_rows 
< 1) || (num_cols 
< 1)), 
10198                       wxT("wxGrid::SetCellSize setting cell size to < 1")); 
10200         // if this was already a multicell then "turn off" the other cells first 
10201         if ((cell_rows 
> 1) || (cell_cols 
> 1)) 
10204             for (j
=row
; j 
< row 
+ cell_rows
; j
++) 
10206                 for (i
=col
; i 
< col 
+ cell_cols
; i
++) 
10208                     if ((i 
!= col
) || (j 
!= row
)) 
10210                         wxGridCellAttr 
*attr_stub 
= GetOrCreateCellAttr(j
, i
); 
10211                         attr_stub
->SetSize( 1, 1 ); 
10212                         attr_stub
->DecRef(); 
10218         // mark the cells that will be covered by this cell to 
10219         // negative or zero values to point back at this cell 
10220         if (((num_rows 
> 1) || (num_cols 
> 1)) && (num_rows 
>= 1) && (num_cols 
>= 1)) 
10223             for (j
=row
; j 
< row 
+ num_rows
; j
++) 
10225                 for (i
=col
; i 
< col 
+ num_cols
; i
++) 
10227                     if ((i 
!= col
) || (j 
!= row
)) 
10229                         wxGridCellAttr 
*attr_stub 
= GetOrCreateCellAttr(j
, i
); 
10230                         attr_stub
->SetSize( row 
- j
, col 
- i 
); 
10231                         attr_stub
->DecRef(); 
10239 void wxGrid::SetCellRenderer(int row
, int col
, wxGridCellRenderer 
*renderer
) 
10241     if ( CanHaveAttributes() ) 
10243         wxGridCellAttr 
*attr 
= GetOrCreateCellAttr(row
, col
); 
10244         attr
->SetRenderer(renderer
); 
10249 void wxGrid::SetCellEditor(int row
, int col
, wxGridCellEditor
* editor
) 
10251     if ( CanHaveAttributes() ) 
10253         wxGridCellAttr 
*attr 
= GetOrCreateCellAttr(row
, col
); 
10254         attr
->SetEditor(editor
); 
10259 void wxGrid::SetReadOnly(int row
, int col
, bool isReadOnly
) 
10261     if ( CanHaveAttributes() ) 
10263         wxGridCellAttr 
*attr 
= GetOrCreateCellAttr(row
, col
); 
10264         attr
->SetReadOnly(isReadOnly
); 
10269 // ---------------------------------------------------------------------------- 
10270 // Data type registration 
10271 // ---------------------------------------------------------------------------- 
10273 void wxGrid::RegisterDataType(const wxString
& typeName
, 
10274                               wxGridCellRenderer
* renderer
, 
10275                               wxGridCellEditor
* editor
) 
10277     m_typeRegistry
->RegisterDataType(typeName
, renderer
, editor
); 
10281 wxGridCellEditor 
* wxGrid::GetDefaultEditorForCell(int row
, int col
) const 
10283     wxString typeName 
= m_table
->GetTypeName(row
, col
); 
10284     return GetDefaultEditorForType(typeName
); 
10287 wxGridCellRenderer 
* wxGrid::GetDefaultRendererForCell(int row
, int col
) const 
10289     wxString typeName 
= m_table
->GetTypeName(row
, col
); 
10290     return GetDefaultRendererForType(typeName
); 
10293 wxGridCellEditor 
* wxGrid::GetDefaultEditorForType(const wxString
& typeName
) const 
10295     int index 
= m_typeRegistry
->FindOrCloneDataType(typeName
); 
10296     if ( index 
== wxNOT_FOUND 
) 
10298         wxFAIL_MSG(wxString::Format(wxT("Unknown data type name [%s]"), typeName
.c_str())); 
10303     return m_typeRegistry
->GetEditor(index
); 
10306 wxGridCellRenderer 
* wxGrid::GetDefaultRendererForType(const wxString
& typeName
) const 
10308     int index 
= m_typeRegistry
->FindOrCloneDataType(typeName
); 
10309     if ( index 
== wxNOT_FOUND 
) 
10311         wxFAIL_MSG(wxString::Format(wxT("Unknown data type name [%s]"), typeName
.c_str())); 
10316     return m_typeRegistry
->GetRenderer(index
); 
10319 // ---------------------------------------------------------------------------- 
10321 // ---------------------------------------------------------------------------- 
10323 void wxGrid::EnableDragRowSize( bool enable 
) 
10325     m_canDragRowSize 
= enable
; 
10328 void wxGrid::EnableDragColSize( bool enable 
) 
10330     m_canDragColSize 
= enable
; 
10333 void wxGrid::EnableDragGridSize( bool enable 
) 
10335     m_canDragGridSize 
= enable
; 
10338 void wxGrid::EnableDragCell( bool enable 
) 
10340     m_canDragCell 
= enable
; 
10343 void wxGrid::SetDefaultRowSize( int height
, bool resizeExistingRows 
) 
10345     m_defaultRowHeight 
= wxMax( height
, m_minAcceptableRowHeight 
); 
10347     if ( resizeExistingRows 
) 
10349         // since we are resizing all rows to the default row size, 
10350         // we can simply clear the row heights and row bottoms 
10351         // arrays (which also allows us to take advantage of 
10352         // some speed optimisations) 
10353         m_rowHeights
.Empty(); 
10354         m_rowBottoms
.Empty(); 
10355         if ( !GetBatchCount() ) 
10360 void wxGrid::SetRowSize( int row
, int height 
) 
10362     wxCHECK_RET( row 
>= 0 && row 
< m_numRows
, _T("invalid row index") ); 
10364     // if < 0 then calculate new height from label 
10368         wxArrayString lines
; 
10369         wxClientDC 
dc(m_rowLabelWin
); 
10370         dc
.SetFont(GetLabelFont()); 
10371         StringToLines(GetRowLabelValue( row 
), lines
); 
10372         GetTextBoxSize( dc
, lines
, &w
, &h 
); 
10373         //check that it is not less than the minimal height 
10374         height 
= wxMax(h
, GetRowMinimalAcceptableHeight()); 
10377     // See comment in SetColSize 
10378     if ( height 
< GetRowMinimalAcceptableHeight()) 
10381     if ( m_rowHeights
.IsEmpty() ) 
10383         // need to really create the array 
10387     int h 
= wxMax( 0, height 
); 
10388     int diff 
= h 
- m_rowHeights
[row
]; 
10390     m_rowHeights
[row
] = h
; 
10391     for ( int i 
= row
; i 
< m_numRows
; i
++ ) 
10393         m_rowBottoms
[i
] += diff
; 
10396     if ( !GetBatchCount() ) 
10400 void wxGrid::SetDefaultColSize( int width
, bool resizeExistingCols 
) 
10402     // we dont allow zero default column width 
10403     m_defaultColWidth 
= wxMax( wxMax( width
, m_minAcceptableColWidth 
), 1 ); 
10405     if ( resizeExistingCols 
) 
10407         // since we are resizing all columns to the default column size, 
10408         // we can simply clear the col widths and col rights 
10409         // arrays (which also allows us to take advantage of 
10410         // some speed optimisations) 
10411         m_colWidths
.Empty(); 
10412         m_colRights
.Empty(); 
10413         if ( !GetBatchCount() ) 
10418 void wxGrid::SetColSize( int col
, int width 
) 
10420     wxCHECK_RET( col 
>= 0 && col 
< m_numCols
, _T("invalid column index") ); 
10422     // if < 0 then calculate new width from label 
10426         wxArrayString lines
; 
10427         wxClientDC 
dc(m_colLabelWin
); 
10428         dc
.SetFont(GetLabelFont()); 
10429         StringToLines(GetColLabelValue(col
), lines
); 
10430         if ( GetColLabelTextOrientation() == wxHORIZONTAL 
) 
10431             GetTextBoxSize( dc
, lines
, &w
, &h 
); 
10433             GetTextBoxSize( dc
, lines
, &h
, &w 
); 
10435         //check that it is not less than the minimal width 
10436         width 
= wxMax(width
, GetColMinimalAcceptableWidth()); 
10439     // should we check that it's bigger than GetColMinimalWidth(col) here? 
10441     // No, because it is reasonable to assume the library user know's 
10442     // what he is doing. However we should test against the weaker 
10443     // constraint of minimalAcceptableWidth, as this breaks rendering 
10445     // This test then fixes sf.net bug #645734 
10447     if ( width 
< GetColMinimalAcceptableWidth() ) 
10450     if ( m_colWidths
.IsEmpty() ) 
10452         // need to really create the array 
10456     int w 
= wxMax( 0, width 
); 
10457     int diff 
= w 
- m_colWidths
[col
]; 
10458     m_colWidths
[col
] = w
; 
10460     for ( int colPos 
= GetColPos(col
); colPos 
< m_numCols
; colPos
++ ) 
10462         m_colRights
[GetColAt(colPos
)] += diff
; 
10465     if ( !GetBatchCount() ) 
10469 void wxGrid::SetColMinimalWidth( int col
, int width 
) 
10471     if (width 
> GetColMinimalAcceptableWidth()) 
10473         wxLongToLongHashMap::key_type key 
= (wxLongToLongHashMap::key_type
)col
; 
10474         m_colMinWidths
[key
] = width
; 
10478 void wxGrid::SetRowMinimalHeight( int row
, int width 
) 
10480     if (width 
> GetRowMinimalAcceptableHeight()) 
10482         wxLongToLongHashMap::key_type key 
= (wxLongToLongHashMap::key_type
)row
; 
10483         m_rowMinHeights
[key
] = width
; 
10487 int wxGrid::GetColMinimalWidth(int col
) const 
10489     wxLongToLongHashMap::key_type key 
= (wxLongToLongHashMap::key_type
)col
; 
10490     wxLongToLongHashMap::const_iterator it 
= m_colMinWidths
.find(key
); 
10492     return it 
!= m_colMinWidths
.end() ? (int)it
->second 
: m_minAcceptableColWidth
; 
10495 int wxGrid::GetRowMinimalHeight(int row
) const 
10497     wxLongToLongHashMap::key_type key 
= (wxLongToLongHashMap::key_type
)row
; 
10498     wxLongToLongHashMap::const_iterator it 
= m_rowMinHeights
.find(key
); 
10500     return it 
!= m_rowMinHeights
.end() ? (int)it
->second 
: m_minAcceptableRowHeight
; 
10503 void wxGrid::SetColMinimalAcceptableWidth( int width 
) 
10505     // We do allow a width of 0 since this gives us 
10506     // an easy way to temporarily hiding columns. 
10508         m_minAcceptableColWidth 
= width
; 
10511 void wxGrid::SetRowMinimalAcceptableHeight( int height 
) 
10513     // We do allow a height of 0 since this gives us 
10514     // an easy way to temporarily hiding rows. 
10516         m_minAcceptableRowHeight 
= height
; 
10519 int  wxGrid::GetColMinimalAcceptableWidth() const 
10521     return m_minAcceptableColWidth
; 
10524 int  wxGrid::GetRowMinimalAcceptableHeight() const 
10526     return m_minAcceptableRowHeight
; 
10529 // ---------------------------------------------------------------------------- 
10531 // ---------------------------------------------------------------------------- 
10534 wxGrid::AutoSizeColOrRow(int colOrRow
, bool setAsMin
, wxGridDirection direction
) 
10536     const bool column 
= direction 
== wxGRID_COLUMN
; 
10538     wxClientDC 
dc(m_gridWin
); 
10540     // cancel editing of cell 
10541     HideCellEditControl(); 
10542     SaveEditControlValue(); 
10544     // init both of them to avoid compiler warnings, even if we only need one 
10552     wxCoord extent
, extentMax 
= 0; 
10553     int max 
= column 
? m_numRows 
: m_numCols
; 
10554     for ( int rowOrCol 
= 0; rowOrCol 
< max
; rowOrCol
++ ) 
10561         wxGridCellAttr 
*attr 
= GetCellAttr(row
, col
); 
10562         wxGridCellRenderer 
*renderer 
= attr
->GetRenderer(this, row
, col
); 
10565             wxSize size 
= renderer
->GetBestSize(*this, *attr
, dc
, row
, col
); 
10566             extent 
= column 
? size
.x 
: size
.y
; 
10567             if ( extent 
> extentMax 
) 
10568                 extentMax 
= extent
; 
10570             renderer
->DecRef(); 
10576     // now also compare with the column label extent 
10578     dc
.SetFont( GetLabelFont() ); 
10582         dc
.GetMultiLineTextExtent( GetColLabelValue(col
), &w
, &h 
); 
10583         if ( GetColLabelTextOrientation() == wxVERTICAL 
) 
10587         dc
.GetMultiLineTextExtent( GetRowLabelValue(row
), &w
, &h 
); 
10589     extent 
= column 
? w 
: h
; 
10590     if ( extent 
> extentMax 
) 
10591         extentMax 
= extent
; 
10595         // empty column - give default extent (notice that if extentMax is less 
10596         // than default extent but != 0, it's OK) 
10597         extentMax 
= column 
? m_defaultColWidth 
: m_defaultRowHeight
; 
10602             // leave some space around text 
10610         // Ensure automatic width is not less than minimal width. See the 
10611         // comment in SetColSize() for explanation of why this isn't done 
10612         // in SetColSize(). 
10614             extentMax 
= wxMax(extentMax
, GetColMinimalWidth(col
)); 
10616         SetColSize( col
, extentMax 
); 
10617         if ( !GetBatchCount() ) 
10620             m_gridWin
->GetClientSize( &cw
, &ch 
); 
10621             wxRect 
rect ( CellToRect( 0, col 
) ); 
10623             CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
); 
10624             rect
.width 
= cw 
- rect
.x
; 
10625             rect
.height 
= m_colLabelHeight
; 
10626             m_colLabelWin
->Refresh( true, &rect 
); 
10631         // Ensure automatic width is not less than minimal height. See the 
10632         // comment in SetColSize() for explanation of why this isn't done 
10633         // in SetRowSize(). 
10635             extentMax 
= wxMax(extentMax
, GetRowMinimalHeight(row
)); 
10637         SetRowSize(row
, extentMax
); 
10638         if ( !GetBatchCount() ) 
10641             m_gridWin
->GetClientSize( &cw
, &ch 
); 
10642             wxRect 
rect( CellToRect( row
, 0 ) ); 
10644             CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
); 
10645             rect
.width 
= m_rowLabelWidth
; 
10646             rect
.height 
= ch 
- rect
.y
; 
10647             m_rowLabelWin
->Refresh( true, &rect 
); 
10654             SetColMinimalWidth(col
, extentMax
); 
10656             SetRowMinimalHeight(row
, extentMax
); 
10660 wxCoord 
wxGrid::CalcColOrRowLabelAreaMinSize(wxGridDirection direction
) 
10662     // calculate size for the rows or columns? 
10663     const bool calcRows 
= direction 
== wxGRID_ROW
; 
10665     wxClientDC 
dc(calcRows 
? GetGridRowLabelWindow() 
10666                            : GetGridColLabelWindow()); 
10667     dc
.SetFont(GetLabelFont()); 
10669     // which dimension should we take into account for calculations? 
10671     // for columns, the text can be only horizontal so it's easy but for rows 
10672     // we also have to take into account the text orientation 
10674         useWidth 
= calcRows 
|| (GetColLabelTextOrientation() == wxVERTICAL
); 
10676     wxArrayString lines
; 
10677     wxCoord extentMax 
= 0; 
10679     const int numRowsOrCols 
= calcRows 
? m_numRows 
: m_numCols
; 
10680     for ( int rowOrCol 
= 0; rowOrCol 
< numRowsOrCols
; rowOrCol
++ ) 
10684         wxString label 
= calcRows 
? GetRowLabelValue(rowOrCol
) 
10685                                   : GetColLabelValue(rowOrCol
); 
10686         StringToLines(label
, lines
); 
10689         GetTextBoxSize(dc
, lines
, &w
, &h
); 
10691         const wxCoord extent 
= useWidth 
? w 
: h
; 
10692         if ( extent 
> extentMax 
) 
10693             extentMax 
= extent
; 
10698         // empty column - give default extent (notice that if extentMax is less 
10699         // than default extent but != 0, it's OK) 
10700         extentMax 
= calcRows 
? GetDefaultRowLabelSize() 
10701                              : GetDefaultColLabelSize(); 
10704     // leave some space around text (taken from AutoSizeColOrRow) 
10713 int wxGrid::SetOrCalcColumnSizes(bool calcOnly
, bool setAsMin
) 
10715     int width 
= m_rowLabelWidth
; 
10717     wxGridUpdateLocker locker
; 
10719         locker
.Create(this); 
10721     for ( int col 
= 0; col 
< m_numCols
; col
++ ) 
10724             AutoSizeColumn(col
, setAsMin
); 
10726         width 
+= GetColWidth(col
); 
10732 int wxGrid::SetOrCalcRowSizes(bool calcOnly
, bool setAsMin
) 
10734     int height 
= m_colLabelHeight
; 
10736     wxGridUpdateLocker locker
; 
10738         locker
.Create(this); 
10740     for ( int row 
= 0; row 
< m_numRows
; row
++ ) 
10743             AutoSizeRow(row
, setAsMin
); 
10745         height 
+= GetRowHeight(row
); 
10751 void wxGrid::AutoSize() 
10753     wxGridUpdateLocker 
locker(this); 
10755     wxSize 
size(SetOrCalcColumnSizes(false) - m_rowLabelWidth 
+ m_extraWidth
, 
10756                 SetOrCalcRowSizes(false) - m_colLabelHeight 
+ m_extraHeight
); 
10758     // we know that we're not going to have scrollbars so disable them now to 
10759     // avoid trouble in SetClientSize() which can otherwise set the correct 
10760     // client size but also leave space for (not needed any more) scrollbars 
10761     SetScrollbars(0, 0, 0, 0, 0, 0, true); 
10763     // restore the scroll rate parameters overwritten by SetScrollbars() 
10764     SetScrollRate(m_scrollLineX
, m_scrollLineY
); 
10766     SetClientSize(size
.x 
+ m_rowLabelWidth
, size
.y 
+ m_colLabelHeight
); 
10769 void wxGrid::AutoSizeRowLabelSize( int row 
) 
10771     // Hide the edit control, so it 
10772     // won't interfere with drag-shrinking. 
10773     if ( IsCellEditControlShown() ) 
10775         HideCellEditControl(); 
10776         SaveEditControlValue(); 
10779     // autosize row height depending on label text 
10780     SetRowSize(row
, -1); 
10784 void wxGrid::AutoSizeColLabelSize( int col 
) 
10786     // Hide the edit control, so it 
10787     // won't interfere with drag-shrinking. 
10788     if ( IsCellEditControlShown() ) 
10790         HideCellEditControl(); 
10791         SaveEditControlValue(); 
10794     // autosize column width depending on label text 
10795     SetColSize(col
, -1); 
10799 wxSize 
wxGrid::DoGetBestSize() const 
10801     wxGrid 
*self 
= (wxGrid 
*)this;  // const_cast 
10803     // we do the same as in AutoSize() here with the exception that we don't 
10804     // change the column/row sizes, only calculate them 
10805     wxSize 
size(self
->SetOrCalcColumnSizes(true) - m_rowLabelWidth 
+ m_extraWidth
, 
10806                 self
->SetOrCalcRowSizes(true) - m_colLabelHeight 
+ m_extraHeight
); 
10808     // NOTE: This size should be cached, but first we need to add calls to 
10809     // InvalidateBestSize everywhere that could change the results of this 
10811     // CacheBestSize(size); 
10813     return wxSize(size
.x 
+ m_rowLabelWidth
, size
.y 
+ m_colLabelHeight
) 
10814             + GetWindowBorderSize(); 
10822 wxPen
& wxGrid::GetDividerPen() const 
10827 // ---------------------------------------------------------------------------- 
10828 // cell value accessor functions 
10829 // ---------------------------------------------------------------------------- 
10831 void wxGrid::SetCellValue( int row
, int col
, const wxString
& s 
) 
10835         m_table
->SetValue( row
, col
, s 
); 
10836         if ( !GetBatchCount() ) 
10839             wxRect 
rect( CellToRect( row
, col 
) ); 
10841             rect
.width 
= m_gridWin
->GetClientSize().GetWidth(); 
10842             CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
); 
10843             m_gridWin
->Refresh( false, &rect 
); 
10846         if ( m_currentCellCoords
.GetRow() == row 
&& 
10847              m_currentCellCoords
.GetCol() == col 
&& 
10848              IsCellEditControlShown()) 
10849              // Note: If we are using IsCellEditControlEnabled, 
10850              // this interacts badly with calling SetCellValue from 
10851              // an EVT_GRID_CELL_CHANGE handler. 
10853             HideCellEditControl(); 
10854             ShowCellEditControl(); // will reread data from table 
10859 // ---------------------------------------------------------------------------- 
10860 // block, row and column selection 
10861 // ---------------------------------------------------------------------------- 
10863 void wxGrid::SelectRow( int row
, bool addToSelected 
) 
10865     if ( IsSelection() && !addToSelected 
) 
10869         m_selection
->SelectRow( row
, false, addToSelected 
); 
10872 void wxGrid::SelectCol( int col
, bool addToSelected 
) 
10874     if ( IsSelection() && !addToSelected 
) 
10878         m_selection
->SelectCol( col
, false, addToSelected 
); 
10881 void wxGrid::SelectBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
, 
10882                           bool addToSelected 
) 
10884     if ( IsSelection() && !addToSelected 
) 
10888         m_selection
->SelectBlock( topRow
, leftCol
, bottomRow
, rightCol
, 
10889                                   false, addToSelected 
); 
10892 void wxGrid::SelectAll() 
10894     if ( m_numRows 
> 0 && m_numCols 
> 0 ) 
10897             m_selection
->SelectBlock( 0, 0, m_numRows 
- 1, m_numCols 
- 1 ); 
10901 // ---------------------------------------------------------------------------- 
10902 // cell, row and col deselection 
10903 // ---------------------------------------------------------------------------- 
10905 void wxGrid::DeselectRow( int row 
) 
10907     if ( !m_selection 
) 
10910     if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows 
) 
10912         if ( m_selection
->IsInSelection(row
, 0 ) ) 
10913             m_selection
->ToggleCellSelection(row
, 0); 
10917         int nCols 
= GetNumberCols(); 
10918         for ( int i 
= 0; i 
< nCols
; i
++ ) 
10920             if ( m_selection
->IsInSelection(row
, i 
) ) 
10921                 m_selection
->ToggleCellSelection(row
, i
); 
10926 void wxGrid::DeselectCol( int col 
) 
10928     if ( !m_selection 
) 
10931     if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns 
) 
10933         if ( m_selection
->IsInSelection(0, col 
) ) 
10934             m_selection
->ToggleCellSelection(0, col
); 
10938         int nRows 
= GetNumberRows(); 
10939         for ( int i 
= 0; i 
< nRows
; i
++ ) 
10941             if ( m_selection
->IsInSelection(i
, col 
) ) 
10942                 m_selection
->ToggleCellSelection(i
, col
); 
10947 void wxGrid::DeselectCell( int row
, int col 
) 
10949     if ( m_selection 
&& m_selection
->IsInSelection(row
, col
) ) 
10950         m_selection
->ToggleCellSelection(row
, col
); 
10953 bool wxGrid::IsSelection() const 
10955     return ( m_selection 
&& (m_selection
->IsSelection() || 
10956              ( m_selectingTopLeft 
!= wxGridNoCellCoords 
&& 
10957                m_selectingBottomRight 
!= wxGridNoCellCoords
) ) ); 
10960 bool wxGrid::IsInSelection( int row
, int col 
) const 
10962     return ( m_selection 
&& (m_selection
->IsInSelection( row
, col 
) || 
10963              ( row 
>= m_selectingTopLeft
.GetRow() && 
10964                col 
>= m_selectingTopLeft
.GetCol() && 
10965                row 
<= m_selectingBottomRight
.GetRow() && 
10966                col 
<= m_selectingBottomRight
.GetCol() )) ); 
10969 wxGridCellCoordsArray 
wxGrid::GetSelectedCells() const 
10973         wxGridCellCoordsArray a
; 
10977     return m_selection
->m_cellSelection
; 
10980 wxGridCellCoordsArray 
wxGrid::GetSelectionBlockTopLeft() const 
10984         wxGridCellCoordsArray a
; 
10988     return m_selection
->m_blockSelectionTopLeft
; 
10991 wxGridCellCoordsArray 
wxGrid::GetSelectionBlockBottomRight() const 
10995         wxGridCellCoordsArray a
; 
10999     return m_selection
->m_blockSelectionBottomRight
; 
11002 wxArrayInt 
wxGrid::GetSelectedRows() const 
11010     return m_selection
->m_rowSelection
; 
11013 wxArrayInt 
wxGrid::GetSelectedCols() const 
11021     return m_selection
->m_colSelection
; 
11024 void wxGrid::ClearSelection() 
11026     wxRect r1 
= BlockToDeviceRect( m_selectingTopLeft
, m_selectingBottomRight
); 
11027     wxRect r2 
= BlockToDeviceRect( m_currentCellCoords
, m_selectingKeyboard 
); 
11028     m_selectingTopLeft 
= 
11029     m_selectingBottomRight 
= 
11030     m_selectingKeyboard 
= wxGridNoCellCoords
; 
11031     Refresh( false, &r1 
); 
11032     Refresh( false, &r2 
); 
11034         m_selection
->ClearSelection(); 
11037 // This function returns the rectangle that encloses the given block 
11038 // in device coords clipped to the client size of the grid window. 
11040 wxRect 
wxGrid::BlockToDeviceRect( const wxGridCellCoords
& topLeft
, 
11041                                   const wxGridCellCoords
& bottomRight 
) const 
11044     wxRect tempCellRect 
= CellToRect(topLeft
); 
11045     if ( tempCellRect 
!= wxGridNoCellRect 
) 
11047         resultRect 
= tempCellRect
; 
11051         resultRect 
= wxRect(0, 0, 0, 0); 
11054     tempCellRect 
= CellToRect(bottomRight
); 
11055     if ( tempCellRect 
!= wxGridNoCellRect 
) 
11057         resultRect 
+= tempCellRect
; 
11061         // If both inputs were "wxGridNoCellRect," then there's nothing to do. 
11062         return wxGridNoCellRect
; 
11065     // Ensure that left/right and top/bottom pairs are in order. 
11066     int left 
= resultRect
.GetLeft(); 
11067     int top 
= resultRect
.GetTop(); 
11068     int right 
= resultRect
.GetRight(); 
11069     int bottom 
= resultRect
.GetBottom(); 
11071     int leftCol 
= topLeft
.GetCol(); 
11072     int topRow 
= topLeft
.GetRow(); 
11073     int rightCol 
= bottomRight
.GetCol(); 
11074     int bottomRow 
= bottomRight
.GetRow(); 
11083         leftCol 
= rightCol
; 
11094         topRow 
= bottomRow
; 
11098     // The following loop is ONLY necessary to detect and handle merged cells. 
11100     m_gridWin
->GetClientSize( &cw
, &ch 
); 
11102     // Get the origin coordinates: notice that they will be negative if the 
11103     // grid is scrolled downwards/to the right. 
11104     int gridOriginX 
= 0; 
11105     int gridOriginY 
= 0; 
11106     CalcScrolledPosition(gridOriginX
, gridOriginY
, &gridOriginX
, &gridOriginY
); 
11108     int onScreenLeftmostCol 
= internalXToCol(-gridOriginX
); 
11109     int onScreenUppermostRow 
= internalYToRow(-gridOriginY
); 
11111     int onScreenRightmostCol 
= internalXToCol(-gridOriginX 
+ cw
); 
11112     int onScreenBottommostRow 
= internalYToRow(-gridOriginY 
+ ch
); 
11114     // Bound our loop so that we only examine the portion of the selected block 
11115     // that is shown on screen. Therefore, we compare the Top-Left block values 
11116     // to the Top-Left screen values, and the Bottom-Right block values to the 
11117     // Bottom-Right screen values, choosing appropriately. 
11118     const int visibleTopRow 
= wxMax(topRow
, onScreenUppermostRow
); 
11119     const int visibleBottomRow 
= wxMin(bottomRow
, onScreenBottommostRow
); 
11120     const int visibleLeftCol 
= wxMax(leftCol
, onScreenLeftmostCol
); 
11121     const int visibleRightCol 
= wxMin(rightCol
, onScreenRightmostCol
); 
11123     for ( int j 
= visibleTopRow
; j 
<= visibleBottomRow
; j
++ ) 
11125         for ( int i 
= visibleLeftCol
; i 
<= visibleRightCol
; i
++ ) 
11127             if ( (j 
== visibleTopRow
) || (j 
== visibleBottomRow
) || 
11128                     (i 
== visibleLeftCol
) || (i 
== visibleRightCol
) ) 
11130                 tempCellRect 
= CellToRect( j
, i 
); 
11132                 if (tempCellRect
.x 
< left
) 
11133                     left 
= tempCellRect
.x
; 
11134                 if (tempCellRect
.y 
< top
) 
11135                     top 
= tempCellRect
.y
; 
11136                 if (tempCellRect
.x 
+ tempCellRect
.width 
> right
) 
11137                     right 
= tempCellRect
.x 
+ tempCellRect
.width
; 
11138                 if (tempCellRect
.y 
+ tempCellRect
.height 
> bottom
) 
11139                     bottom 
= tempCellRect
.y 
+ tempCellRect
.height
; 
11143                 i 
= visibleRightCol
; // jump over inner cells. 
11148     // Convert to scrolled coords 
11149     CalcScrolledPosition( left
, top
, &left
, &top 
); 
11150     CalcScrolledPosition( right
, bottom
, &right
, &bottom 
); 
11152     if (right 
< 0 || bottom 
< 0 || left 
> cw 
|| top 
> ch
) 
11153         return wxRect(0,0,0,0); 
11155     resultRect
.SetLeft( wxMax(0, left
) ); 
11156     resultRect
.SetTop( wxMax(0, top
) ); 
11157     resultRect
.SetRight( wxMin(cw
, right
) ); 
11158     resultRect
.SetBottom( wxMin(ch
, bottom
) ); 
11163 // ---------------------------------------------------------------------------- 
11165 // ---------------------------------------------------------------------------- 
11167 #if wxUSE_DRAG_AND_DROP 
11169 // this allow setting drop target directly on wxGrid 
11170 void wxGrid::SetDropTarget(wxDropTarget 
*dropTarget
) 
11172     GetGridWindow()->SetDropTarget(dropTarget
); 
11175 #endif // wxUSE_DRAG_AND_DROP 
11177 // ---------------------------------------------------------------------------- 
11178 // grid event classes 
11179 // ---------------------------------------------------------------------------- 
11181 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent
, wxNotifyEvent 
) 
11183 wxGridEvent::wxGridEvent( int id
, wxEventType type
, wxObject
* obj
, 
11184                           int row
, int col
, int x
, int y
, bool sel
, 
11185                           bool control
, bool shift
, bool alt
, bool meta 
) 
11186         : wxNotifyEvent( type
, id 
) 
11193     m_control 
= control
; 
11198     SetEventObject(obj
); 
11202 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent
, wxNotifyEvent 
) 
11204 wxGridSizeEvent::wxGridSizeEvent( int id
, wxEventType type
, wxObject
* obj
, 
11205                                   int rowOrCol
, int x
, int y
, 
11206                                   bool control
, bool shift
, bool alt
, bool meta 
) 
11207         : wxNotifyEvent( type
, id 
) 
11209     m_rowOrCol 
= rowOrCol
; 
11212     m_control 
= control
; 
11217     SetEventObject(obj
); 
11221 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent
, wxNotifyEvent 
) 
11223 wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id
, wxEventType type
, wxObject
* obj
, 
11224                                                const wxGridCellCoords
& topLeft
, 
11225                                                const wxGridCellCoords
& bottomRight
, 
11226                                                bool sel
, bool control
, 
11227                                                bool shift
, bool alt
, bool meta 
) 
11228         : wxNotifyEvent( type
, id 
) 
11230     m_topLeft 
= topLeft
; 
11231     m_bottomRight 
= bottomRight
; 
11233     m_control 
= control
; 
11238     SetEventObject(obj
); 
11242 IMPLEMENT_DYNAMIC_CLASS(wxGridEditorCreatedEvent
, wxCommandEvent
) 
11244 wxGridEditorCreatedEvent::wxGridEditorCreatedEvent(int id
, wxEventType type
, 
11245                                                    wxObject
* obj
, int row
, 
11246                                                    int col
, wxControl
* ctrl
) 
11247     : wxCommandEvent(type
, id
) 
11249     SetEventObject(obj
); 
11255 #endif // wxUSE_GRID