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 /////////////////////////////////////////////////////////////////////////////
15 - Replace use of wxINVERT with wxOverlay
16 - Make Begin/EndBatch() the same as the generic Freeze/Thaw()
17 - Review the column reordering code, it's a mess.
18 - Implement row reordering after dealing with the columns.
21 // For compilers that support precompilation, includes "wx/wx.h".
22 #include "wx/wxprec.h"
34 #include "wx/dcclient.h"
35 #include "wx/settings.h"
37 #include "wx/textctrl.h"
38 #include "wx/checkbox.h"
39 #include "wx/combobox.h"
40 #include "wx/valtext.h"
43 #include "wx/listbox.h"
46 #include "wx/textfile.h"
47 #include "wx/spinctrl.h"
48 #include "wx/tokenzr.h"
49 #include "wx/renderer.h"
50 #include "wx/headerctrl.h"
52 #include "wx/generic/gridsel.h"
54 const char wxGridNameStr
[] = "grid";
56 #if defined(__WXMOTIF__)
57 #define WXUNUSED_MOTIF(identifier) WXUNUSED(identifier)
59 #define WXUNUSED_MOTIF(identifier) identifier
62 #if defined(__WXGTK__)
63 #define WXUNUSED_GTK(identifier) WXUNUSED(identifier)
65 #define WXUNUSED_GTK(identifier) identifier
68 // Required for wxIs... functions
71 // ----------------------------------------------------------------------------
73 // ----------------------------------------------------------------------------
75 WX_DEFINE_ARRAY_WITH_DECL_PTR(wxGridCellAttr
*, wxArrayAttrs
,
76 class WXDLLIMPEXP_ADV
);
78 struct wxGridCellWithAttr
80 wxGridCellWithAttr(int row
, int col
, wxGridCellAttr
*attr_
)
81 : coords(row
, col
), attr(attr_
)
86 wxGridCellWithAttr(const wxGridCellWithAttr
& other
)
87 : coords(other
.coords
),
93 wxGridCellWithAttr
& operator=(const wxGridCellWithAttr
& other
)
95 coords
= other
.coords
;
96 if (attr
!= other
.attr
)
105 void ChangeAttr(wxGridCellAttr
* new_attr
)
107 if (attr
!= new_attr
)
109 // "Delete" (i.e. DecRef) the old attribute.
112 // Take ownership of the new attribute, i.e. no IncRef.
116 ~wxGridCellWithAttr()
121 wxGridCellCoords coords
;
122 wxGridCellAttr
*attr
;
125 WX_DECLARE_OBJARRAY_WITH_DECL(wxGridCellWithAttr
, wxGridCellWithAttrArray
,
126 class WXDLLIMPEXP_ADV
);
128 #include "wx/arrimpl.cpp"
130 WX_DEFINE_OBJARRAY(wxGridCellCoordsArray
)
131 WX_DEFINE_OBJARRAY(wxGridCellWithAttrArray
)
133 // ----------------------------------------------------------------------------
135 // ----------------------------------------------------------------------------
137 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_LEFT_CLICK
)
138 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_RIGHT_CLICK
)
139 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_LEFT_DCLICK
)
140 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_RIGHT_DCLICK
)
141 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_BEGIN_DRAG
)
142 DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_LEFT_CLICK
)
143 DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_RIGHT_CLICK
)
144 DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_LEFT_DCLICK
)
145 DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_RIGHT_DCLICK
)
146 DEFINE_EVENT_TYPE(wxEVT_GRID_ROW_SIZE
)
147 DEFINE_EVENT_TYPE(wxEVT_GRID_COL_SIZE
)
148 DEFINE_EVENT_TYPE(wxEVT_GRID_COL_MOVE
)
149 DEFINE_EVENT_TYPE(wxEVT_GRID_RANGE_SELECT
)
150 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_CHANGE
)
151 DEFINE_EVENT_TYPE(wxEVT_GRID_SELECT_CELL
)
152 DEFINE_EVENT_TYPE(wxEVT_GRID_EDITOR_SHOWN
)
153 DEFINE_EVENT_TYPE(wxEVT_GRID_EDITOR_HIDDEN
)
154 DEFINE_EVENT_TYPE(wxEVT_GRID_EDITOR_CREATED
)
156 // ----------------------------------------------------------------------------
158 // ----------------------------------------------------------------------------
160 // header column providing access to the column information stored in wxGrid
161 // via wxHeaderColumn interface
162 class wxGridHeaderColumn
: public wxHeaderColumn
165 wxGridHeaderColumn(wxGrid
*grid
, int col
)
171 virtual wxString
GetTitle() const { return m_grid
->GetColLabelValue(m_col
); }
172 virtual wxBitmap
GetBitmap() const { return wxNullBitmap
; }
173 virtual int GetWidth() const { return m_grid
->GetColSize(m_col
); }
174 virtual int GetMinWidth() const { return 0; }
175 virtual wxAlignment
GetAlignment() const
179 m_grid
->GetColLabelAlignment(&horz
, &vert
);
181 return static_cast<wxAlignment
>(horz
);
184 virtual int GetFlags() const
187 if ( m_grid
->CanDragColSize() )
188 flags
|= wxCOL_RESIZABLE
;
189 if ( m_grid
->CanDragColMove() )
190 flags
|= wxCOL_REORDERABLE
;
195 // TODO: currently there is no support for sorting
196 virtual bool IsSortKey() const { return false; }
197 virtual bool IsSortOrderAscending() const { return false; }
200 // these really should be const but are not because the column needs to be
201 // assignable to be used in a wxVector (in STL build, in non-STL build we
202 // avoid the need for this)
207 // header control retreiving column information from the grid
208 class wxGridHeaderCtrl
: public wxHeaderCtrl
211 wxGridHeaderCtrl(wxGrid
*owner
)
212 : wxHeaderCtrl(owner
,
216 owner
->CanDragColMove() ? wxHD_DRAGDROP
: 0)
221 virtual wxHeaderColumn
& GetColumn(unsigned int idx
)
223 return m_columns
[idx
];
227 wxGrid
*GetOwner() const { return static_cast<wxGrid
*>(GetParent()); }
229 // override the base class method to update our m_columns array
230 virtual void OnColumnCountChanging(unsigned int count
)
232 const unsigned countOld
= m_columns
.size();
233 if ( count
< countOld
)
235 // just discard the columns which don't exist any more (notice that
236 // we can't use resize() here as it would require the vector
237 // value_type, i.e. wxGridHeaderColumn to be default constructible,
239 m_columns
.erase(m_columns
.begin() + count
, m_columns
.end());
241 else // new columns added
243 // add columns for the new elements
244 for ( unsigned n
= countOld
; n
< count
; n
++ )
245 m_columns
.push_back(wxGridHeaderColumn(GetOwner(), n
));
249 // override to implement column auto sizing
250 virtual bool UpdateColumnWidthToFit(unsigned int idx
, int widthTitle
)
252 GetOwner()->SetColSize(idx
, widthTitle
);
258 // event handlers forwarding wxHeaderCtrl events to wxGrid
259 void OnBeginResize(wxHeaderCtrlEvent
& event
)
261 GetOwner()->DoStartResizeCol(event
.GetColumn());
266 void OnResizing(wxHeaderCtrlEvent
& event
)
268 GetOwner()->DoUpdateResizeColWidth(event
.GetWidth());
271 void OnEndResize(wxHeaderCtrlEvent
& event
)
273 GetOwner()->DoEndDragResizeCol();
278 void OnEndReorder(wxHeaderCtrlEvent
& event
)
280 event
.Skip(); // TODO: position it at event.GetNewOrder()
283 wxVector
<wxGridHeaderColumn
> m_columns
;
285 DECLARE_EVENT_TABLE()
286 DECLARE_NO_COPY_CLASS(wxGridHeaderCtrl
)
289 BEGIN_EVENT_TABLE(wxGridHeaderCtrl
, wxHeaderCtrl
)
290 EVT_HEADER_BEGIN_RESIZE(wxID_ANY
, wxGridHeaderCtrl
::OnBeginResize
)
291 EVT_HEADER_RESIZING(wxID_ANY
, wxGridHeaderCtrl
::OnResizing
)
292 EVT_HEADER_END_RESIZE(wxID_ANY
, wxGridHeaderCtrl
::OnEndResize
)
294 EVT_HEADER_END_REORDER(wxID_ANY
, wxGridHeaderCtrl
::OnEndReorder
)
297 // common base class for various grid subwindows
298 class WXDLLIMPEXP_ADV wxGridSubwindow
: public wxWindow
301 wxGridSubwindow(wxGrid
*owner
,
302 int additionalStyle
= 0,
303 const wxString
& name
= wxPanelNameStr
)
304 : wxWindow(owner
, wxID_ANY
,
305 wxDefaultPosition
, wxDefaultSize
,
306 wxBORDER_NONE
| additionalStyle
,
312 virtual bool AcceptsFocus() const { return false; }
314 wxGrid
*GetOwner() { return m_owner
; }
317 void OnMouseCaptureLost(wxMouseCaptureLostEvent
& event
);
321 DECLARE_EVENT_TABLE()
322 DECLARE_NO_COPY_CLASS(wxGridSubwindow
)
325 class WXDLLIMPEXP_ADV wxGridRowLabelWindow
: public wxGridSubwindow
328 wxGridRowLabelWindow(wxGrid
*parent
)
329 : wxGridSubwindow(parent
)
335 void OnPaint( wxPaintEvent
& event
);
336 void OnMouseEvent( wxMouseEvent
& event
);
337 void OnMouseWheel( wxMouseEvent
& event
);
339 DECLARE_EVENT_TABLE()
340 DECLARE_NO_COPY_CLASS(wxGridRowLabelWindow
)
344 class WXDLLIMPEXP_ADV wxGridColLabelWindow
: public wxGridSubwindow
347 wxGridColLabelWindow(wxGrid
*parent
)
348 : wxGridSubwindow(parent
)
354 void OnPaint( wxPaintEvent
& event
);
355 void OnMouseEvent( wxMouseEvent
& event
);
356 void OnMouseWheel( wxMouseEvent
& event
);
358 DECLARE_EVENT_TABLE()
359 DECLARE_NO_COPY_CLASS(wxGridColLabelWindow
)
363 class WXDLLIMPEXP_ADV wxGridCornerLabelWindow
: public wxGridSubwindow
366 wxGridCornerLabelWindow(wxGrid
*parent
)
367 : wxGridSubwindow(parent
)
372 void OnMouseEvent( wxMouseEvent
& event
);
373 void OnMouseWheel( wxMouseEvent
& event
);
374 void OnPaint( wxPaintEvent
& event
);
376 DECLARE_EVENT_TABLE()
377 DECLARE_NO_COPY_CLASS(wxGridCornerLabelWindow
)
380 class WXDLLIMPEXP_ADV wxGridWindow
: public wxGridSubwindow
383 wxGridWindow(wxGrid
*parent
)
384 : wxGridSubwindow(parent
,
385 wxWANTS_CHARS
| wxCLIP_CHILDREN
,
391 virtual void ScrollWindow( int dx
, int dy
, const wxRect
*rect
);
393 virtual bool AcceptsFocus() const { return true; }
396 void OnPaint( wxPaintEvent
&event
);
397 void OnMouseWheel( wxMouseEvent
& event
);
398 void OnMouseEvent( wxMouseEvent
& event
);
399 void OnKeyDown( wxKeyEvent
& );
400 void OnKeyUp( wxKeyEvent
& );
401 void OnChar( wxKeyEvent
& );
402 void OnEraseBackground( wxEraseEvent
& );
403 void OnFocus( wxFocusEvent
& );
405 DECLARE_EVENT_TABLE()
406 DECLARE_NO_COPY_CLASS(wxGridWindow
)
410 class wxGridCellEditorEvtHandler
: public wxEvtHandler
413 wxGridCellEditorEvtHandler(wxGrid
* grid
, wxGridCellEditor
* editor
)
420 void OnKillFocus(wxFocusEvent
& event
);
421 void OnKeyDown(wxKeyEvent
& event
);
422 void OnChar(wxKeyEvent
& event
);
424 void SetInSetFocus(bool inSetFocus
) { m_inSetFocus
= inSetFocus
; }
428 wxGridCellEditor
*m_editor
;
430 // Work around the fact that a focus kill event can be sent to
431 // a combobox within a set focus event.
434 DECLARE_EVENT_TABLE()
435 DECLARE_DYNAMIC_CLASS(wxGridCellEditorEvtHandler
)
436 DECLARE_NO_COPY_CLASS(wxGridCellEditorEvtHandler
)
440 IMPLEMENT_ABSTRACT_CLASS(wxGridCellEditorEvtHandler
, wxEvtHandler
)
442 BEGIN_EVENT_TABLE( wxGridCellEditorEvtHandler
, wxEvtHandler
)
443 EVT_KILL_FOCUS( wxGridCellEditorEvtHandler
::OnKillFocus
)
444 EVT_KEY_DOWN( wxGridCellEditorEvtHandler
::OnKeyDown
)
445 EVT_CHAR( wxGridCellEditorEvtHandler
::OnChar
)
449 // ----------------------------------------------------------------------------
450 // the internal data representation used by wxGridCellAttrProvider
451 // ----------------------------------------------------------------------------
453 // this class stores attributes set for cells
454 class WXDLLIMPEXP_ADV wxGridCellAttrData
457 void SetAttr(wxGridCellAttr
*attr
, int row
, int col
);
458 wxGridCellAttr
*GetAttr(int row
, int col
) const;
459 void UpdateAttrRows( size_t pos
, int numRows
);
460 void UpdateAttrCols( size_t pos
, int numCols
);
463 // searches for the attr for given cell, returns wxNOT_FOUND if not found
464 int FindIndex(int row
, int col
) const;
466 wxGridCellWithAttrArray m_attrs
;
469 // this class stores attributes set for rows or columns
470 class WXDLLIMPEXP_ADV wxGridRowOrColAttrData
473 // empty ctor to suppress warnings
474 wxGridRowOrColAttrData() {}
475 ~wxGridRowOrColAttrData();
477 void SetAttr(wxGridCellAttr
*attr
, int rowOrCol
);
478 wxGridCellAttr
*GetAttr(int rowOrCol
) const;
479 void UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
);
482 wxArrayInt m_rowsOrCols
;
483 wxArrayAttrs m_attrs
;
486 // NB: this is just a wrapper around 3 objects: one which stores cell
487 // attributes, and 2 others for row/col ones
488 class WXDLLIMPEXP_ADV wxGridCellAttrProviderData
491 wxGridCellAttrData m_cellAttrs
;
492 wxGridRowOrColAttrData m_rowAttrs
,
497 // ----------------------------------------------------------------------------
498 // data structures used for the data type registry
499 // ----------------------------------------------------------------------------
501 struct wxGridDataTypeInfo
503 wxGridDataTypeInfo(const wxString
& typeName
,
504 wxGridCellRenderer
* renderer
,
505 wxGridCellEditor
* editor
)
506 : m_typeName(typeName
), m_renderer(renderer
), m_editor(editor
)
509 ~wxGridDataTypeInfo()
511 wxSafeDecRef(m_renderer
);
512 wxSafeDecRef(m_editor
);
516 wxGridCellRenderer
* m_renderer
;
517 wxGridCellEditor
* m_editor
;
519 DECLARE_NO_COPY_CLASS(wxGridDataTypeInfo
)
523 WX_DEFINE_ARRAY_WITH_DECL_PTR(wxGridDataTypeInfo
*, wxGridDataTypeInfoArray
,
524 class WXDLLIMPEXP_ADV
);
527 class WXDLLIMPEXP_ADV wxGridTypeRegistry
530 wxGridTypeRegistry() {}
531 ~wxGridTypeRegistry();
533 void RegisterDataType(const wxString
& typeName
,
534 wxGridCellRenderer
* renderer
,
535 wxGridCellEditor
* editor
);
537 // find one of already registered data types
538 int FindRegisteredDataType(const wxString
& typeName
);
540 // try to FindRegisteredDataType(), if this fails and typeName is one of
541 // standard typenames, register it and return its index
542 int FindDataType(const wxString
& typeName
);
544 // try to FindDataType(), if it fails see if it is not one of already
545 // registered data types with some params in which case clone the
546 // registered data type and set params for it
547 int FindOrCloneDataType(const wxString
& typeName
);
549 wxGridCellRenderer
* GetRenderer(int index
);
550 wxGridCellEditor
* GetEditor(int index
);
553 wxGridDataTypeInfoArray m_typeinfo
;
556 // ----------------------------------------------------------------------------
557 // operations classes abstracting the difference between operating on rows and
559 // ----------------------------------------------------------------------------
561 // This class allows to write a function only once because by using its methods
562 // it will apply to both columns and rows.
564 // This is an abstract interface definition, the two concrete implementations
565 // below should be used when working with rows and columns respectively.
566 class wxGridOperations
569 // Returns the operations in the other direction, i.e. wxGridRowOperations
570 // if this object is a wxGridColumnOperations and vice versa.
571 virtual wxGridOperations
& Dual() const = 0;
573 // Return the number of rows or columns.
574 virtual int GetNumberOfLines(const wxGrid
*grid
) const = 0;
576 // Return the selection mode which allows selecting rows or columns.
577 virtual wxGrid
::wxGridSelectionModes
GetSelectionMode() const = 0;
579 // Make a wxGridCellCoords from the given components: thisDir is row or
580 // column and otherDir is column or row
581 virtual wxGridCellCoords
MakeCoords(int thisDir
, int otherDir
) const = 0;
583 // Calculate the scrolled position of the given abscissa or ordinate.
584 virtual int CalcScrolledPosition(wxGrid
*grid
, int pos
) const = 0;
586 // Selects the horizontal or vertical component from the given object.
587 virtual int Select(const wxGridCellCoords
& coords
) const = 0;
588 virtual int Select(const wxPoint
& pt
) const = 0;
589 virtual int Select(const wxSize
& sz
) const = 0;
590 virtual int Select(const wxRect
& r
) const = 0;
591 virtual int& Select(wxRect
& r
) const = 0;
593 // Returns width or height of the rectangle
594 virtual int& SelectSize(wxRect
& r
) const = 0;
596 // Make a wxSize such that Select() applied to it returns first component
597 virtual wxSize
MakeSize(int first
, int second
) const = 0;
599 // Sets the row or column component of the given cell coordinates
600 virtual void Set(wxGridCellCoords
& coords
, int line
) const = 0;
603 // Draws a line parallel to the row or column, i.e. horizontal or vertical:
604 // pos is the horizontal or vertical position of the line and start and end
605 // are the coordinates of the line extremities in the other direction
607 DrawParallelLine(wxDC
& dc
, int start
, int end
, int pos
) const = 0;
609 // Draw a horizontal or vertical line across the given rectangle
610 // (this is implemented in terms of above and uses Select() to extract
611 // start and end from the given rectangle)
612 void DrawParallelLineInRect(wxDC
& dc
, const wxRect
& rect
, int pos
) const
614 const int posStart
= Select(rect
.GetPosition());
615 DrawParallelLine(dc
, posStart
, posStart
+ Select(rect
.GetSize()), pos
);
619 // Return the row or column at the given pixel coordinate.
621 PosToLine(const wxGrid
*grid
, int pos
, bool clip
= false) const = 0;
623 // Get the top/left position, in pixels, of the given row or column
624 virtual int GetLineStartPos(const wxGrid
*grid
, int line
) const = 0;
626 // Get the bottom/right position, in pixels, of the given row or column
627 virtual int GetLineEndPos(const wxGrid
*grid
, int line
) const = 0;
629 // Get the height/width of the given row/column
630 virtual int GetLineSize(const wxGrid
*grid
, int line
) const = 0;
632 // Get wxGrid::m_rowBottoms/m_colRights array
633 virtual const wxArrayInt
& GetLineEnds(const wxGrid
*grid
) const = 0;
635 // Get default height row height or column width
636 virtual int GetDefaultLineSize(const wxGrid
*grid
) const = 0;
638 // Return the minimal acceptable row height or column width
639 virtual int GetMinimalAcceptableLineSize(const wxGrid
*grid
) const = 0;
641 // Return the minimal row height or column width
642 virtual int GetMinimalLineSize(const wxGrid
*grid
, int line
) const = 0;
644 // Set the row height or column width
645 virtual void SetLineSize(wxGrid
*grid
, int line
, int size
) const = 0;
647 // True if rows/columns can be resized by user
648 virtual bool CanResizeLines(const wxGrid
*grid
) const = 0;
651 // Return the index of the line at the given position
653 // NB: currently this is always identity for the rows as reordering is only
654 // implemented for the lines
655 virtual int GetLineAt(const wxGrid
*grid
, int line
) const = 0;
658 // Get the row or column label window
659 virtual wxWindow
*GetHeaderWindow(wxGrid
*grid
) const = 0;
661 // Get the width or height of the row or column label window
662 virtual int GetHeaderWindowSize(wxGrid
*grid
) const = 0;
665 // This class is never used polymorphically but give it a virtual dtor
666 // anyhow to suppress g++ complaints about it
667 virtual ~wxGridOperations() { }
670 class wxGridRowOperations
: public wxGridOperations
673 virtual wxGridOperations
& Dual() const;
675 virtual int GetNumberOfLines(const wxGrid
*grid
) const
676 { return grid
->GetNumberRows(); }
678 virtual wxGrid
::wxGridSelectionModes
GetSelectionMode() const
679 { return wxGrid
::wxGridSelectRows
; }
681 virtual wxGridCellCoords
MakeCoords(int thisDir
, int otherDir
) const
682 { return wxGridCellCoords(thisDir
, otherDir
); }
684 virtual int CalcScrolledPosition(wxGrid
*grid
, int pos
) const
685 { return grid
->CalcScrolledPosition(wxPoint(pos
, 0)).x
; }
687 virtual int Select(const wxGridCellCoords
& c
) const { return c
.GetRow(); }
688 virtual int Select(const wxPoint
& pt
) const { return pt
.x
; }
689 virtual int Select(const wxSize
& sz
) const { return sz
.x
; }
690 virtual int Select(const wxRect
& r
) const { return r
.x
; }
691 virtual int& Select(wxRect
& r
) const { return r
.x
; }
692 virtual int& SelectSize(wxRect
& r
) const { return r
.width
; }
693 virtual wxSize
MakeSize(int first
, int second
) const
694 { return wxSize(first
, second
); }
695 virtual void Set(wxGridCellCoords
& coords
, int line
) const
696 { coords
.SetRow(line
); }
698 virtual void DrawParallelLine(wxDC
& dc
, int start
, int end
, int pos
) const
699 { dc
.DrawLine(start
, pos
, end
, pos
); }
701 virtual int PosToLine(const wxGrid
*grid
, int pos
, bool clip
= false) const
702 { return grid
->YToRow(pos
, clip
); }
703 virtual int GetLineStartPos(const wxGrid
*grid
, int line
) const
704 { return grid
->GetRowTop(line
); }
705 virtual int GetLineEndPos(const wxGrid
*grid
, int line
) const
706 { return grid
->GetRowBottom(line
); }
707 virtual int GetLineSize(const wxGrid
*grid
, int line
) const
708 { return grid
->GetRowHeight(line
); }
709 virtual const wxArrayInt
& GetLineEnds(const wxGrid
*grid
) const
710 { return grid
->m_rowBottoms
; }
711 virtual int GetDefaultLineSize(const wxGrid
*grid
) const
712 { return grid
->GetDefaultRowSize(); }
713 virtual int GetMinimalAcceptableLineSize(const wxGrid
*grid
) const
714 { return grid
->GetRowMinimalAcceptableHeight(); }
715 virtual int GetMinimalLineSize(const wxGrid
*grid
, int line
) const
716 { return grid
->GetRowMinimalHeight(line
); }
717 virtual void SetLineSize(wxGrid
*grid
, int line
, int size
) const
718 { grid
->SetRowSize(line
, size
); }
719 virtual bool CanResizeLines(const wxGrid
*grid
) const
720 { return grid
->CanDragRowSize(); }
722 virtual int GetLineAt(const wxGrid
* WXUNUSED(grid
), int line
) const
723 { return line
; } // TODO: implement row reordering
725 virtual wxWindow
*GetHeaderWindow(wxGrid
*grid
) const
726 { return grid
->GetGridRowLabelWindow(); }
727 virtual int GetHeaderWindowSize(wxGrid
*grid
) const
728 { return grid
->GetRowLabelSize(); }
731 class wxGridColumnOperations
: public wxGridOperations
734 virtual wxGridOperations
& Dual() const;
736 virtual int GetNumberOfLines(const wxGrid
*grid
) const
737 { return grid
->GetNumberCols(); }
739 virtual wxGrid
::wxGridSelectionModes
GetSelectionMode() const
740 { return wxGrid
::wxGridSelectColumns
; }
742 virtual wxGridCellCoords
MakeCoords(int thisDir
, int otherDir
) const
743 { return wxGridCellCoords(otherDir
, thisDir
); }
745 virtual int CalcScrolledPosition(wxGrid
*grid
, int pos
) const
746 { return grid
->CalcScrolledPosition(wxPoint(0, pos
)).y
; }
748 virtual int Select(const wxGridCellCoords
& c
) const { return c
.GetCol(); }
749 virtual int Select(const wxPoint
& pt
) const { return pt
.y
; }
750 virtual int Select(const wxSize
& sz
) const { return sz
.y
; }
751 virtual int Select(const wxRect
& r
) const { return r
.y
; }
752 virtual int& Select(wxRect
& r
) const { return r
.y
; }
753 virtual int& SelectSize(wxRect
& r
) const { return r
.height
; }
754 virtual wxSize
MakeSize(int first
, int second
) const
755 { return wxSize(second
, first
); }
756 virtual void Set(wxGridCellCoords
& coords
, int line
) const
757 { coords
.SetCol(line
); }
759 virtual void DrawParallelLine(wxDC
& dc
, int start
, int end
, int pos
) const
760 { dc
.DrawLine(pos
, start
, pos
, end
); }
762 virtual int PosToLine(const wxGrid
*grid
, int pos
, bool clip
= false) const
763 { return grid
->XToCol(pos
, clip
); }
764 virtual int GetLineStartPos(const wxGrid
*grid
, int line
) const
765 { return grid
->GetColLeft(line
); }
766 virtual int GetLineEndPos(const wxGrid
*grid
, int line
) const
767 { return grid
->GetColRight(line
); }
768 virtual int GetLineSize(const wxGrid
*grid
, int line
) const
769 { return grid
->GetColWidth(line
); }
770 virtual const wxArrayInt
& GetLineEnds(const wxGrid
*grid
) const
771 { return grid
->m_colRights
; }
772 virtual int GetDefaultLineSize(const wxGrid
*grid
) const
773 { return grid
->GetDefaultColSize(); }
774 virtual int GetMinimalAcceptableLineSize(const wxGrid
*grid
) const
775 { return grid
->GetColMinimalAcceptableWidth(); }
776 virtual int GetMinimalLineSize(const wxGrid
*grid
, int line
) const
777 { return grid
->GetColMinimalWidth(line
); }
778 virtual void SetLineSize(wxGrid
*grid
, int line
, int size
) const
779 { grid
->SetColSize(line
, size
); }
780 virtual bool CanResizeLines(const wxGrid
*grid
) const
781 { return grid
->CanDragColSize(); }
783 virtual int GetLineAt(const wxGrid
*grid
, int line
) const
784 { return grid
->GetColAt(line
); }
786 virtual wxWindow
*GetHeaderWindow(wxGrid
*grid
) const
787 { return grid
->GetGridColLabelWindow(); }
788 virtual int GetHeaderWindowSize(wxGrid
*grid
) const
789 { return grid
->GetColLabelSize(); }
792 wxGridOperations
& wxGridRowOperations
::Dual() const
794 static wxGridColumnOperations s_colOper
;
799 wxGridOperations
& wxGridColumnOperations
::Dual() const
801 static wxGridRowOperations s_rowOper
;
806 // This class abstracts the difference between operations going forward
807 // (down/right) and backward (up/left) and allows to use the same code for
808 // functions which differ only in the direction of grid traversal
810 // Like wxGridOperations it's an ABC with two concrete subclasses below. Unlike
811 // it, this is a normal object and not just a function dispatch table and has a
814 // Note: the explanation of this discrepancy is the existence of (very useful)
815 // Dual() method in wxGridOperations which forces us to make wxGridOperations a
816 // function dispatcher only.
817 class wxGridDirectionOperations
820 // The oper parameter to ctor selects whether we work with rows or columns
821 wxGridDirectionOperations(wxGrid
*grid
, const wxGridOperations
& oper
)
827 // Check if the component of this point in our direction is at the
828 // boundary, i.e. is the first/last row/column
829 virtual bool IsAtBoundary(const wxGridCellCoords
& coords
) const = 0;
831 // Increment the component of this point in our direction
832 virtual void Advance(wxGridCellCoords
& coords
) const = 0;
834 // Find the line at the given distance, in pixels, away from this one
835 // (this uses clipping, i.e. anything after the last line is counted as the
836 // last one and anything before the first one as 0)
837 virtual int MoveByPixelDistance(int line
, int distance
) const = 0;
839 // This class is never used polymorphically but give it a virtual dtor
840 // anyhow to suppress g++ complaints about it
841 virtual ~wxGridDirectionOperations() { }
844 wxGrid
* const m_grid
;
845 const wxGridOperations
& m_oper
;
848 class wxGridBackwardOperations
: public wxGridDirectionOperations
851 wxGridBackwardOperations(wxGrid
*grid
, const wxGridOperations
& oper
)
852 : wxGridDirectionOperations(grid
, oper
)
856 virtual bool IsAtBoundary(const wxGridCellCoords
& coords
) const
858 wxASSERT_MSG( m_oper
.Select(coords
) >= 0, "invalid row/column" );
860 return m_oper
.Select(coords
) == 0;
863 virtual void Advance(wxGridCellCoords
& coords
) const
865 wxASSERT( !IsAtBoundary(coords
) );
867 m_oper
.Set(coords
, m_oper
.Select(coords
) - 1);
870 virtual int MoveByPixelDistance(int line
, int distance
) const
872 int pos
= m_oper
.GetLineStartPos(m_grid
, line
);
873 return m_oper
.PosToLine(m_grid
, pos
- distance
+ 1, true);
877 class wxGridForwardOperations
: public wxGridDirectionOperations
880 wxGridForwardOperations(wxGrid
*grid
, const wxGridOperations
& oper
)
881 : wxGridDirectionOperations(grid
, oper
),
882 m_numLines(oper
.GetNumberOfLines(grid
))
886 virtual bool IsAtBoundary(const wxGridCellCoords
& coords
) const
888 wxASSERT_MSG( m_oper
.Select(coords
) < m_numLines
, "invalid row/column" );
890 return m_oper
.Select(coords
) == m_numLines
- 1;
893 virtual void Advance(wxGridCellCoords
& coords
) const
895 wxASSERT( !IsAtBoundary(coords
) );
897 m_oper
.Set(coords
, m_oper
.Select(coords
) + 1);
900 virtual int MoveByPixelDistance(int line
, int distance
) const
902 int pos
= m_oper
.GetLineStartPos(m_grid
, line
);
903 return m_oper
.PosToLine(m_grid
, pos
+ distance
, true);
907 const int m_numLines
;
910 // ----------------------------------------------------------------------------
912 // ----------------------------------------------------------------------------
914 //#define DEBUG_ATTR_CACHE
915 #ifdef DEBUG_ATTR_CACHE
916 static size_t gs_nAttrCacheHits
= 0;
917 static size_t gs_nAttrCacheMisses
= 0;
920 // ----------------------------------------------------------------------------
922 // ----------------------------------------------------------------------------
924 wxGridCellCoords
wxGridNoCellCoords( -1, -1 );
925 wxRect
wxGridNoCellRect( -1, -1, -1, -1 );
931 const size_t GRID_SCROLL_LINE_X
= 15;
932 const size_t GRID_SCROLL_LINE_Y
= GRID_SCROLL_LINE_X
;
934 // the size of hash tables used a bit everywhere (the max number of elements
935 // in these hash tables is the number of rows/columns)
936 const int GRID_HASH_SIZE
= 100;
938 // the minimal distance in pixels the mouse needs to move to start a drag
940 const int DRAG_SENSITIVITY
= 3;
942 } // anonymous namespace
944 // ----------------------------------------------------------------------------
946 // ----------------------------------------------------------------------------
951 // ensure that first is less or equal to second, swapping the values if
953 void EnsureFirstLessThanSecond(int& first
, int& second
)
955 if ( first
> second
)
956 wxSwap(first
, second
);
959 } // anonymous namespace
961 // ============================================================================
963 // ============================================================================
965 // ----------------------------------------------------------------------------
967 // ----------------------------------------------------------------------------
969 wxGridCellEditor
::wxGridCellEditor()
975 wxGridCellEditor
::~wxGridCellEditor()
980 void wxGridCellEditor
::Create(wxWindow
* WXUNUSED(parent
),
981 wxWindowID
WXUNUSED(id
),
982 wxEvtHandler
* evtHandler
)
985 m_control
->PushEventHandler(evtHandler
);
988 void wxGridCellEditor
::PaintBackground(const wxRect
& rectCell
,
989 wxGridCellAttr
*attr
)
991 // erase the background because we might not fill the cell
992 wxClientDC
dc(m_control
->GetParent());
993 wxGridWindow
* gridWindow
= wxDynamicCast(m_control
->GetParent(), wxGridWindow
);
995 gridWindow
->GetOwner()->PrepareDC(dc
);
997 dc
.SetPen(*wxTRANSPARENT_PEN
);
998 dc
.SetBrush(wxBrush(attr
->GetBackgroundColour()));
999 dc
.DrawRectangle(rectCell
);
1001 // redraw the control we just painted over
1002 m_control
->Refresh();
1005 void wxGridCellEditor
::Destroy()
1009 m_control
->PopEventHandler( true /* delete it*/ );
1011 m_control
->Destroy();
1016 void wxGridCellEditor
::Show(bool show
, wxGridCellAttr
*attr
)
1018 wxASSERT_MSG(m_control
, wxT("The wxGridCellEditor must be created first!"));
1020 m_control
->Show(show
);
1024 // set the colours/fonts if we have any
1027 m_colFgOld
= m_control
->GetForegroundColour();
1028 m_control
->SetForegroundColour(attr
->GetTextColour());
1030 m_colBgOld
= m_control
->GetBackgroundColour();
1031 m_control
->SetBackgroundColour(attr
->GetBackgroundColour());
1033 // Workaround for GTK+1 font setting problem on some platforms
1034 #if !defined(__WXGTK__) || defined(__WXGTK20__)
1035 m_fontOld
= m_control
->GetFont();
1036 m_control
->SetFont(attr
->GetFont());
1039 // can't do anything more in the base class version, the other
1040 // attributes may only be used by the derived classes
1045 // restore the standard colours fonts
1046 if ( m_colFgOld
.Ok() )
1048 m_control
->SetForegroundColour(m_colFgOld
);
1049 m_colFgOld
= wxNullColour
;
1052 if ( m_colBgOld
.Ok() )
1054 m_control
->SetBackgroundColour(m_colBgOld
);
1055 m_colBgOld
= wxNullColour
;
1058 // Workaround for GTK+1 font setting problem on some platforms
1059 #if !defined(__WXGTK__) || defined(__WXGTK20__)
1060 if ( m_fontOld
.Ok() )
1062 m_control
->SetFont(m_fontOld
);
1063 m_fontOld
= wxNullFont
;
1069 void wxGridCellEditor
::SetSize(const wxRect
& rect
)
1071 wxASSERT_MSG(m_control
, wxT("The wxGridCellEditor must be created first!"));
1073 m_control
->SetSize(rect
, wxSIZE_ALLOW_MINUS_ONE
);
1076 void wxGridCellEditor
::HandleReturn(wxKeyEvent
& event
)
1081 bool wxGridCellEditor
::IsAcceptedKey(wxKeyEvent
& event
)
1083 bool ctrl
= event
.ControlDown();
1084 bool alt
= event
.AltDown();
1087 // On the Mac the Alt key is more like shift and is used for entry of
1088 // valid characters, so check for Ctrl and Meta instead.
1089 alt
= event
.MetaDown();
1092 // Assume it's not a valid char if ctrl or alt is down, but if both are
1093 // down then it may be because of an AltGr key combination, so let them
1094 // through in that case.
1095 if ((ctrl
|| alt
) && !(ctrl
&& alt
))
1099 // if the unicode key code is not really a unicode character (it may
1100 // be a function key or etc., the platforms appear to always give us a
1101 // small value in this case) then fallback to the ASCII key code but
1102 // don't do anything for function keys or etc.
1103 if ( event
.GetUnicodeKey() > 127 && event
.GetKeyCode() > 127 )
1106 if ( event
.GetKeyCode() > 255 )
1113 void wxGridCellEditor
::StartingKey(wxKeyEvent
& event
)
1118 void wxGridCellEditor
::StartingClick()
1124 // ----------------------------------------------------------------------------
1125 // wxGridCellTextEditor
1126 // ----------------------------------------------------------------------------
1128 wxGridCellTextEditor
::wxGridCellTextEditor()
1133 void wxGridCellTextEditor
::Create(wxWindow
* parent
,
1135 wxEvtHandler
* evtHandler
)
1137 DoCreate(parent
, id
, evtHandler
);
1140 void wxGridCellTextEditor
::DoCreate(wxWindow
* parent
,
1142 wxEvtHandler
* evtHandler
,
1145 style
|= wxTE_PROCESS_ENTER
| wxTE_PROCESS_TAB
| wxNO_BORDER
;
1147 m_control
= new wxTextCtrl(parent
, id
, wxEmptyString
,
1148 wxDefaultPosition
, wxDefaultSize
,
1151 // set max length allowed in the textctrl, if the parameter was set
1152 if ( m_maxChars
!= 0 )
1154 Text()->SetMaxLength(m_maxChars
);
1157 wxGridCellEditor
::Create(parent
, id
, evtHandler
);
1160 void wxGridCellTextEditor
::PaintBackground(const wxRect
& WXUNUSED(rectCell
),
1161 wxGridCellAttr
* WXUNUSED(attr
))
1163 // as we fill the entire client area,
1164 // don't do anything here to minimize flicker
1167 void wxGridCellTextEditor
::SetSize(const wxRect
& rectOrig
)
1169 wxRect
rect(rectOrig
);
1171 // Make the edit control large enough to allow for internal margins
1173 // TODO: remove this if the text ctrl sizing is improved esp. for unix
1175 #if defined(__WXGTK__)
1183 #elif defined(__WXMSW__)
1197 int extra_x
= ( rect
.x
> 2 ) ?
2 : 1;
1198 int extra_y
= ( rect
.y
> 2 ) ?
2 : 1;
1200 #if defined(__WXMOTIF__)
1205 rect
.SetLeft( wxMax(0, rect
.x
- extra_x
) );
1206 rect
.SetTop( wxMax(0, rect
.y
- extra_y
) );
1207 rect
.SetRight( rect
.GetRight() + 2 * extra_x
);
1208 rect
.SetBottom( rect
.GetBottom() + 2 * extra_y
);
1211 wxGridCellEditor
::SetSize(rect
);
1214 void wxGridCellTextEditor
::BeginEdit(int row
, int col
, wxGrid
* grid
)
1216 wxASSERT_MSG(m_control
, wxT("The wxGridCellEditor must be created first!"));
1218 m_startValue
= grid
->GetTable()->GetValue(row
, col
);
1220 DoBeginEdit(m_startValue
);
1223 void wxGridCellTextEditor
::DoBeginEdit(const wxString
& startValue
)
1225 Text()->SetValue(startValue
);
1226 Text()->SetInsertionPointEnd();
1227 Text()->SetSelection(-1, -1);
1231 bool wxGridCellTextEditor
::EndEdit(int row
, int col
, wxGrid
* grid
)
1233 wxASSERT_MSG(m_control
, wxT("The wxGridCellEditor must be created first!"));
1235 bool changed
= false;
1236 wxString value
= Text()->GetValue();
1237 if (value
!= m_startValue
)
1241 grid
->GetTable()->SetValue(row
, col
, value
);
1243 m_startValue
= wxEmptyString
;
1245 // No point in setting the text of the hidden control
1246 //Text()->SetValue(m_startValue);
1251 void wxGridCellTextEditor
::Reset()
1253 wxASSERT_MSG(m_control
, wxT("The wxGridCellEditor must be created first!"));
1255 DoReset(m_startValue
);
1258 void wxGridCellTextEditor
::DoReset(const wxString
& startValue
)
1260 Text()->SetValue(startValue
);
1261 Text()->SetInsertionPointEnd();
1264 bool wxGridCellTextEditor
::IsAcceptedKey(wxKeyEvent
& event
)
1266 return wxGridCellEditor
::IsAcceptedKey(event
);
1269 void wxGridCellTextEditor
::StartingKey(wxKeyEvent
& event
)
1271 // Since this is now happening in the EVT_CHAR event EmulateKeyPress is no
1272 // longer an appropriate way to get the character into the text control.
1273 // Do it ourselves instead. We know that if we get this far that we have
1274 // a valid character, so not a whole lot of testing needs to be done.
1276 wxTextCtrl
* tc
= Text();
1281 ch
= event
.GetUnicodeKey();
1283 ch
= (wxChar
)event
.GetKeyCode();
1285 ch
= (wxChar
)event
.GetKeyCode();
1291 // delete the character at the cursor
1292 pos
= tc
->GetInsertionPoint();
1293 if (pos
< tc
->GetLastPosition())
1294 tc
->Remove(pos
, pos
+ 1);
1298 // delete the character before the cursor
1299 pos
= tc
->GetInsertionPoint();
1301 tc
->Remove(pos
- 1, pos
);
1310 void wxGridCellTextEditor
::HandleReturn( wxKeyEvent
&
1311 WXUNUSED_GTK(WXUNUSED_MOTIF(event
)) )
1313 #if defined(__WXMOTIF__) || defined(__WXGTK__)
1314 // wxMotif needs a little extra help...
1315 size_t pos
= (size_t)( Text()->GetInsertionPoint() );
1316 wxString
s( Text()->GetValue() );
1317 s
= s
.Left(pos
) + wxT("\n") + s
.Mid(pos
);
1318 Text()->SetValue(s
);
1319 Text()->SetInsertionPoint( pos
);
1321 // the other ports can handle a Return key press
1327 void wxGridCellTextEditor
::SetParameters(const wxString
& params
)
1337 if ( params
.ToLong(&tmp
) )
1339 m_maxChars
= (size_t)tmp
;
1343 wxLogDebug( _T("Invalid wxGridCellTextEditor parameter string '%s' ignored"), params
.c_str() );
1348 // return the value in the text control
1349 wxString wxGridCellTextEditor
::GetValue() const
1351 return Text()->GetValue();
1354 // ----------------------------------------------------------------------------
1355 // wxGridCellNumberEditor
1356 // ----------------------------------------------------------------------------
1358 wxGridCellNumberEditor
::wxGridCellNumberEditor(int min
, int max
)
1364 void wxGridCellNumberEditor
::Create(wxWindow
* parent
,
1366 wxEvtHandler
* evtHandler
)
1371 // create a spin ctrl
1372 m_control
= new wxSpinCtrl(parent
, wxID_ANY
, wxEmptyString
,
1373 wxDefaultPosition
, wxDefaultSize
,
1377 wxGridCellEditor
::Create(parent
, id
, evtHandler
);
1382 // just a text control
1383 wxGridCellTextEditor
::Create(parent
, id
, evtHandler
);
1385 #if wxUSE_VALIDATORS
1386 Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC
));
1391 void wxGridCellNumberEditor
::BeginEdit(int row
, int col
, wxGrid
* grid
)
1393 // first get the value
1394 wxGridTableBase
*table
= grid
->GetTable();
1395 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
1397 m_valueOld
= table
->GetValueAsLong(row
, col
);
1402 wxString sValue
= table
->GetValue(row
, col
);
1403 if (! sValue
.ToLong(&m_valueOld
) && ! sValue
.empty())
1405 wxFAIL_MSG( _T("this cell doesn't have numeric value") );
1413 Spin()->SetValue((int)m_valueOld
);
1419 DoBeginEdit(GetString());
1423 bool wxGridCellNumberEditor
::EndEdit(int row
, int col
,
1432 value
= Spin()->GetValue();
1433 if ( value
== m_valueOld
)
1436 text
.Printf(wxT("%ld"), value
);
1438 else // using unconstrained input
1439 #endif // wxUSE_SPINCTRL
1441 const wxString
textOld(grid
->GetCellValue(row
, col
));
1442 text
= Text()->GetValue();
1445 if ( textOld
.empty() )
1448 else // non-empty text now (maybe 0)
1450 if ( !text
.ToLong(&value
) )
1453 // if value == m_valueOld == 0 but old text was "" and new one is
1454 // "0" something still did change
1455 if ( value
== m_valueOld
&& (value
|| !textOld
.empty()) )
1460 wxGridTableBase
* const table
= grid
->GetTable();
1461 if ( table
->CanSetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
1462 table
->SetValueAsLong(row
, col
, value
);
1464 table
->SetValue(row
, col
, text
);
1469 void wxGridCellNumberEditor
::Reset()
1474 Spin()->SetValue((int)m_valueOld
);
1479 DoReset(GetString());
1483 bool wxGridCellNumberEditor
::IsAcceptedKey(wxKeyEvent
& event
)
1485 if ( wxGridCellEditor
::IsAcceptedKey(event
) )
1487 int keycode
= event
.GetKeyCode();
1488 if ( (keycode
< 128) &&
1489 (wxIsdigit(keycode
) || keycode
== '+' || keycode
== '-'))
1498 void wxGridCellNumberEditor
::StartingKey(wxKeyEvent
& event
)
1500 int keycode
= event
.GetKeyCode();
1503 if ( wxIsdigit(keycode
) || keycode
== '+' || keycode
== '-')
1505 wxGridCellTextEditor
::StartingKey(event
);
1507 // skip Skip() below
1514 if ( wxIsdigit(keycode
) )
1516 wxSpinCtrl
* spin
= (wxSpinCtrl
*)m_control
;
1517 spin
->SetValue(keycode
- '0');
1518 spin
->SetSelection(1,1);
1527 void wxGridCellNumberEditor
::SetParameters(const wxString
& params
)
1538 if ( params
.BeforeFirst(_T(',')).ToLong(&tmp
) )
1542 if ( params
.AfterFirst(_T(',')).ToLong(&tmp
) )
1546 // skip the error message below
1551 wxLogDebug(_T("Invalid wxGridCellNumberEditor parameter string '%s' ignored"), params
.c_str());
1555 // return the value in the spin control if it is there (the text control otherwise)
1556 wxString wxGridCellNumberEditor
::GetValue() const
1563 long value
= Spin()->GetValue();
1564 s
.Printf(wxT("%ld"), value
);
1569 s
= Text()->GetValue();
1575 // ----------------------------------------------------------------------------
1576 // wxGridCellFloatEditor
1577 // ----------------------------------------------------------------------------
1579 wxGridCellFloatEditor
::wxGridCellFloatEditor(int width
, int precision
)
1582 m_precision
= precision
;
1585 void wxGridCellFloatEditor
::Create(wxWindow
* parent
,
1587 wxEvtHandler
* evtHandler
)
1589 wxGridCellTextEditor
::Create(parent
, id
, evtHandler
);
1591 #if wxUSE_VALIDATORS
1592 Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC
));
1596 void wxGridCellFloatEditor
::BeginEdit(int row
, int col
, wxGrid
* grid
)
1598 // first get the value
1599 wxGridTableBase
* const table
= grid
->GetTable();
1600 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
1602 m_valueOld
= table
->GetValueAsDouble(row
, col
);
1608 const wxString value
= table
->GetValue(row
, col
);
1609 if ( !value
.empty() )
1611 if ( !value
.ToDouble(&m_valueOld
) )
1613 wxFAIL_MSG( _T("this cell doesn't have float value") );
1619 DoBeginEdit(GetString());
1622 bool wxGridCellFloatEditor
::EndEdit(int row
, int col
, wxGrid
* grid
)
1624 const wxString
text(Text()->GetValue()),
1625 textOld(grid
->GetCellValue(row
, col
));
1628 if ( !text
.empty() )
1630 if ( !text
.ToDouble(&value
) )
1633 else // new value is empty string
1635 if ( textOld
.empty() )
1636 return false; // nothing changed
1641 // the test for empty strings ensures that we don't skip the value setting
1642 // when "" is replaced by "0" or vice versa as "" numeric value is also 0.
1643 if ( wxIsSameDouble(value
, m_valueOld
) && !text
.empty() && !textOld
.empty() )
1644 return false; // nothing changed
1646 wxGridTableBase
* const table
= grid
->GetTable();
1648 if ( table
->CanSetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
1649 table
->SetValueAsDouble(row
, col
, value
);
1651 table
->SetValue(row
, col
, text
);
1656 void wxGridCellFloatEditor
::Reset()
1658 DoReset(GetString());
1661 void wxGridCellFloatEditor
::StartingKey(wxKeyEvent
& event
)
1663 int keycode
= event
.GetKeyCode();
1665 tmpbuf
[0] = (char) keycode
;
1667 wxString
strbuf(tmpbuf
, *wxConvCurrent
);
1670 bool is_decimal_point
= ( strbuf
==
1671 wxLocale
::GetInfo(wxLOCALE_DECIMAL_POINT
, wxLOCALE_CAT_NUMBER
) );
1673 bool is_decimal_point
= ( strbuf
== _T(".") );
1676 if ( wxIsdigit(keycode
) || keycode
== '+' || keycode
== '-'
1677 || is_decimal_point
)
1679 wxGridCellTextEditor
::StartingKey(event
);
1681 // skip Skip() below
1688 void wxGridCellFloatEditor
::SetParameters(const wxString
& params
)
1699 if ( params
.BeforeFirst(_T(',')).ToLong(&tmp
) )
1703 if ( params
.AfterFirst(_T(',')).ToLong(&tmp
) )
1705 m_precision
= (int)tmp
;
1707 // skip the error message below
1712 wxLogDebug(_T("Invalid wxGridCellFloatEditor parameter string '%s' ignored"), params
.c_str());
1716 wxString wxGridCellFloatEditor
::GetString() const
1719 if ( m_precision
== -1 && m_width
!= -1)
1721 // default precision
1722 fmt
.Printf(_T("%%%d.f"), m_width
);
1724 else if ( m_precision
!= -1 && m_width
== -1)
1727 fmt
.Printf(_T("%%.%df"), m_precision
);
1729 else if ( m_precision
!= -1 && m_width
!= -1 )
1731 fmt
.Printf(_T("%%%d.%df"), m_width
, m_precision
);
1735 // default width/precision
1739 return wxString
::Format(fmt
, m_valueOld
);
1742 bool wxGridCellFloatEditor
::IsAcceptedKey(wxKeyEvent
& event
)
1744 if ( wxGridCellEditor
::IsAcceptedKey(event
) )
1746 const int keycode
= event
.GetKeyCode();
1747 if ( isascii(keycode
) )
1750 tmpbuf
[0] = (char) keycode
;
1752 wxString
strbuf(tmpbuf
, *wxConvCurrent
);
1755 const wxString decimalPoint
=
1756 wxLocale
::GetInfo(wxLOCALE_DECIMAL_POINT
, wxLOCALE_CAT_NUMBER
);
1758 const wxString
decimalPoint(_T('.'));
1761 // accept digits, 'e' as in '1e+6', also '-', '+', and '.'
1762 if ( wxIsdigit(keycode
) ||
1763 tolower(keycode
) == 'e' ||
1764 keycode
== decimalPoint
||
1776 #endif // wxUSE_TEXTCTRL
1780 // ----------------------------------------------------------------------------
1781 // wxGridCellBoolEditor
1782 // ----------------------------------------------------------------------------
1784 // the default values for GetValue()
1785 wxString wxGridCellBoolEditor
::ms_stringValues
[2] = { _T(""), _T("1") };
1787 void wxGridCellBoolEditor
::Create(wxWindow
* parent
,
1789 wxEvtHandler
* evtHandler
)
1791 m_control
= new wxCheckBox(parent
, id
, wxEmptyString
,
1792 wxDefaultPosition
, wxDefaultSize
,
1795 wxGridCellEditor
::Create(parent
, id
, evtHandler
);
1798 void wxGridCellBoolEditor
::SetSize(const wxRect
& r
)
1800 bool resize
= false;
1801 wxSize size
= m_control
->GetSize();
1802 wxCoord minSize
= wxMin(r
.width
, r
.height
);
1804 // check if the checkbox is not too big/small for this cell
1805 wxSize sizeBest
= m_control
->GetBestSize();
1806 if ( !(size
== sizeBest
) )
1808 // reset to default size if it had been made smaller
1814 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
1816 // leave 1 pixel margin
1817 size
.x
= size
.y
= minSize
- 2;
1824 m_control
->SetSize(size
);
1827 // position it in the centre of the rectangle (TODO: support alignment?)
1829 #if defined(__WXGTK__) || defined (__WXMOTIF__)
1830 // the checkbox without label still has some space to the right in wxGTK,
1831 // so shift it to the right
1833 #elif defined(__WXMSW__)
1834 // here too, but in other way
1839 int hAlign
= wxALIGN_CENTRE
;
1840 int vAlign
= wxALIGN_CENTRE
;
1842 GetCellAttr()->GetAlignment(& hAlign
, & vAlign
);
1845 if (hAlign
== wxALIGN_LEFT
)
1853 y
= r
.y
+ r
.height
/ 2 - size
.y
/ 2;
1855 else if (hAlign
== wxALIGN_RIGHT
)
1857 x
= r
.x
+ r
.width
- size
.x
- 2;
1858 y
= r
.y
+ r
.height
/ 2 - size
.y
/ 2;
1860 else if (hAlign
== wxALIGN_CENTRE
)
1862 x
= r
.x
+ r
.width
/ 2 - size
.x
/ 2;
1863 y
= r
.y
+ r
.height
/ 2 - size
.y
/ 2;
1866 m_control
->Move(x
, y
);
1869 void wxGridCellBoolEditor
::Show(bool show
, wxGridCellAttr
*attr
)
1871 m_control
->Show(show
);
1875 wxColour colBg
= attr ? attr
->GetBackgroundColour() : *wxLIGHT_GREY
;
1876 CBox()->SetBackgroundColour(colBg
);
1880 void wxGridCellBoolEditor
::BeginEdit(int row
, int col
, wxGrid
* grid
)
1882 wxASSERT_MSG(m_control
,
1883 wxT("The wxGridCellEditor must be created first!"));
1885 if (grid
->GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
))
1887 m_startValue
= grid
->GetTable()->GetValueAsBool(row
, col
);
1891 wxString
cellval( grid
->GetTable()->GetValue(row
, col
) );
1893 if ( cellval
== ms_stringValues
[false] )
1894 m_startValue
= false;
1895 else if ( cellval
== ms_stringValues
[true] )
1896 m_startValue
= true;
1899 // do not try to be smart here and convert it to true or false
1900 // because we'll still overwrite it with something different and
1901 // this risks to be very surprising for the user code, let them
1903 wxFAIL_MSG( _T("invalid value for a cell with bool editor!") );
1907 CBox()->SetValue(m_startValue
);
1911 bool wxGridCellBoolEditor
::EndEdit(int row
, int col
,
1914 wxASSERT_MSG(m_control
,
1915 wxT("The wxGridCellEditor must be created first!"));
1917 bool changed
= false;
1918 bool value
= CBox()->GetValue();
1919 if ( value
!= m_startValue
)
1924 wxGridTableBase
* const table
= grid
->GetTable();
1925 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
) )
1926 table
->SetValueAsBool(row
, col
, value
);
1928 table
->SetValue(row
, col
, GetValue());
1934 void wxGridCellBoolEditor
::Reset()
1936 wxASSERT_MSG(m_control
,
1937 wxT("The wxGridCellEditor must be created first!"));
1939 CBox()->SetValue(m_startValue
);
1942 void wxGridCellBoolEditor
::StartingClick()
1944 CBox()->SetValue(!CBox()->GetValue());
1947 bool wxGridCellBoolEditor
::IsAcceptedKey(wxKeyEvent
& event
)
1949 if ( wxGridCellEditor
::IsAcceptedKey(event
) )
1951 int keycode
= event
.GetKeyCode();
1964 void wxGridCellBoolEditor
::StartingKey(wxKeyEvent
& event
)
1966 int keycode
= event
.GetKeyCode();
1970 CBox()->SetValue(!CBox()->GetValue());
1974 CBox()->SetValue(true);
1978 CBox()->SetValue(false);
1983 wxString wxGridCellBoolEditor
::GetValue() const
1985 return ms_stringValues
[CBox()->GetValue()];
1989 wxGridCellBoolEditor
::UseStringValues(const wxString
& valueTrue
,
1990 const wxString
& valueFalse
)
1992 ms_stringValues
[false] = valueFalse
;
1993 ms_stringValues
[true] = valueTrue
;
1997 wxGridCellBoolEditor
::IsTrueValue(const wxString
& value
)
1999 return value
== ms_stringValues
[true];
2002 #endif // wxUSE_CHECKBOX
2006 // ----------------------------------------------------------------------------
2007 // wxGridCellChoiceEditor
2008 // ----------------------------------------------------------------------------
2010 wxGridCellChoiceEditor
::wxGridCellChoiceEditor(const wxArrayString
& choices
,
2012 : m_choices(choices
),
2013 m_allowOthers(allowOthers
) { }
2015 wxGridCellChoiceEditor
::wxGridCellChoiceEditor(size_t count
,
2016 const wxString choices
[],
2018 : m_allowOthers(allowOthers
)
2022 m_choices
.Alloc(count
);
2023 for ( size_t n
= 0; n
< count
; n
++ )
2025 m_choices
.Add(choices
[n
]);
2030 wxGridCellEditor
*wxGridCellChoiceEditor
::Clone() const
2032 wxGridCellChoiceEditor
*editor
= new wxGridCellChoiceEditor
;
2033 editor
->m_allowOthers
= m_allowOthers
;
2034 editor
->m_choices
= m_choices
;
2039 void wxGridCellChoiceEditor
::Create(wxWindow
* parent
,
2041 wxEvtHandler
* evtHandler
)
2043 int style
= wxTE_PROCESS_ENTER
|
2047 if ( !m_allowOthers
)
2048 style
|= wxCB_READONLY
;
2049 m_control
= new wxComboBox(parent
, id
, wxEmptyString
,
2050 wxDefaultPosition
, wxDefaultSize
,
2054 wxGridCellEditor
::Create(parent
, id
, evtHandler
);
2057 void wxGridCellChoiceEditor
::PaintBackground(const wxRect
& rectCell
,
2058 wxGridCellAttr
* attr
)
2060 // as we fill the entire client area, don't do anything here to minimize
2063 // TODO: It doesn't actually fill the client area since the height of a
2064 // combo always defaults to the standard. Until someone has time to
2065 // figure out the right rectangle to paint, just do it the normal way.
2066 wxGridCellEditor
::PaintBackground(rectCell
, attr
);
2069 void wxGridCellChoiceEditor
::BeginEdit(int row
, int col
, wxGrid
* grid
)
2071 wxASSERT_MSG(m_control
,
2072 wxT("The wxGridCellEditor must be created first!"));
2074 wxGridCellEditorEvtHandler
* evtHandler
= NULL
;
2076 evtHandler
= wxDynamicCast(m_control
->GetEventHandler(), wxGridCellEditorEvtHandler
);
2078 // Don't immediately end if we get a kill focus event within BeginEdit
2080 evtHandler
->SetInSetFocus(true);
2082 m_startValue
= grid
->GetTable()->GetValue(row
, col
);
2084 Reset(); // this updates combo box to correspond to m_startValue
2086 Combo()->SetFocus();
2090 // When dropping down the menu, a kill focus event
2091 // happens after this point, so we can't reset the flag yet.
2092 #if !defined(__WXGTK20__)
2093 evtHandler
->SetInSetFocus(false);
2098 bool wxGridCellChoiceEditor
::EndEdit(int row
, int col
,
2101 wxString value
= Combo()->GetValue();
2102 if ( value
== m_startValue
)
2105 grid
->GetTable()->SetValue(row
, col
, value
);
2110 void wxGridCellChoiceEditor
::Reset()
2114 Combo()->SetValue(m_startValue
);
2115 Combo()->SetInsertionPointEnd();
2117 else // the combobox is read-only
2119 // find the right position, or default to the first if not found
2120 int pos
= Combo()->FindString(m_startValue
);
2121 if (pos
== wxNOT_FOUND
)
2123 Combo()->SetSelection(pos
);
2127 void wxGridCellChoiceEditor
::SetParameters(const wxString
& params
)
2137 wxStringTokenizer
tk(params
, _T(','));
2138 while ( tk
.HasMoreTokens() )
2140 m_choices
.Add(tk
.GetNextToken());
2144 // return the value in the text control
2145 wxString wxGridCellChoiceEditor
::GetValue() const
2147 return Combo()->GetValue();
2150 #endif // wxUSE_COMBOBOX
2152 // ----------------------------------------------------------------------------
2153 // wxGridCellEditorEvtHandler
2154 // ----------------------------------------------------------------------------
2156 void wxGridCellEditorEvtHandler
::OnKillFocus(wxFocusEvent
& event
)
2158 // Don't disable the cell if we're just starting to edit it
2163 m_grid
->DisableCellEditControl();
2168 void wxGridCellEditorEvtHandler
::OnKeyDown(wxKeyEvent
& event
)
2170 switch ( event
.GetKeyCode() )
2174 m_grid
->DisableCellEditControl();
2178 m_grid
->GetEventHandler()->ProcessEvent( event
);
2182 case WXK_NUMPAD_ENTER
:
2183 if (!m_grid
->GetEventHandler()->ProcessEvent(event
))
2184 m_editor
->HandleReturn(event
);
2193 void wxGridCellEditorEvtHandler
::OnChar(wxKeyEvent
& event
)
2195 int row
= m_grid
->GetGridCursorRow();
2196 int col
= m_grid
->GetGridCursorCol();
2197 wxRect rect
= m_grid
->CellToRect( row
, col
);
2199 m_grid
->GetGridWindow()->GetClientSize( &cw
, &ch
);
2201 // if cell width is smaller than grid client area, cell is wholly visible
2202 bool wholeCellVisible
= (rect
.GetWidth() < cw
);
2204 switch ( event
.GetKeyCode() )
2209 case WXK_NUMPAD_ENTER
:
2214 if ( wholeCellVisible
)
2216 // no special processing needed...
2221 // do special processing for partly visible cell...
2223 // get the widths of all cells previous to this one
2225 for ( int i
= 0; i
< col
; i
++ )
2227 colXPos
+= m_grid
->GetColSize(i
);
2230 int xUnit
= 1, yUnit
= 1;
2231 m_grid
->GetScrollPixelsPerUnit(&xUnit
, &yUnit
);
2234 m_grid
->Scroll(colXPos
/ xUnit
- 1, m_grid
->GetScrollPos(wxVERTICAL
));
2238 m_grid
->Scroll(colXPos
/ xUnit
, m_grid
->GetScrollPos(wxVERTICAL
));
2246 if ( wholeCellVisible
)
2248 // no special processing needed...
2253 // do special processing for partly visible cell...
2256 wxString value
= m_grid
->GetCellValue(row
, col
);
2257 if ( wxEmptyString
!= value
)
2259 // get width of cell CONTENTS (text)
2261 wxFont font
= m_grid
->GetCellFont(row
, col
);
2262 m_grid
->GetTextExtent(value
, &textWidth
, &y
, NULL
, NULL
, &font
);
2264 // try to RIGHT align the text by scrolling
2265 int client_right
= m_grid
->GetGridWindow()->GetClientSize().GetWidth();
2267 // (m_grid->GetScrollLineX()*2) is a factor for not scrolling to far,
2268 // otherwise the last part of the cell content might be hidden below the scroll bar
2269 // FIXME: maybe there is a more suitable correction?
2270 textWidth
-= (client_right
- (m_grid
->GetScrollLineX() * 2));
2271 if ( textWidth
< 0 )
2277 // get the widths of all cells previous to this one
2279 for ( int i
= 0; i
< col
; i
++ )
2281 colXPos
+= m_grid
->GetColSize(i
);
2284 // and add the (modified) text width of the cell contents
2285 // as we'd like to see the last part of the cell contents
2286 colXPos
+= textWidth
;
2288 int xUnit
= 1, yUnit
= 1;
2289 m_grid
->GetScrollPixelsPerUnit(&xUnit
, &yUnit
);
2290 m_grid
->Scroll(colXPos
/ xUnit
- 1, m_grid
->GetScrollPos(wxVERTICAL
));
2301 // ----------------------------------------------------------------------------
2302 // wxGridCellWorker is an (almost) empty common base class for
2303 // wxGridCellRenderer and wxGridCellEditor managing ref counting
2304 // ----------------------------------------------------------------------------
2306 void wxGridCellWorker
::SetParameters(const wxString
& WXUNUSED(params
))
2311 wxGridCellWorker
::~wxGridCellWorker()
2315 // ============================================================================
2317 // ============================================================================
2319 // ----------------------------------------------------------------------------
2320 // wxGridCellRenderer
2321 // ----------------------------------------------------------------------------
2323 void wxGridCellRenderer
::Draw(wxGrid
& grid
,
2324 wxGridCellAttr
& attr
,
2327 int WXUNUSED(row
), int WXUNUSED(col
),
2330 dc
.SetBackgroundMode( wxBRUSHSTYLE_SOLID
);
2333 if ( grid
.IsEnabled() )
2337 if ( grid
.HasFocus() )
2338 clr
= grid
.GetSelectionBackground();
2340 clr
= wxSystemSettings
::GetColour(wxSYS_COLOUR_BTNSHADOW
);
2344 clr
= attr
.GetBackgroundColour();
2347 else // grey out fields if the grid is disabled
2349 clr
= wxSystemSettings
::GetColour(wxSYS_COLOUR_BTNFACE
);
2353 dc
.SetPen( *wxTRANSPARENT_PEN
);
2354 dc
.DrawRectangle(rect
);
2357 // ----------------------------------------------------------------------------
2358 // wxGridCellStringRenderer
2359 // ----------------------------------------------------------------------------
2361 void wxGridCellStringRenderer
::SetTextColoursAndFont(const wxGrid
& grid
,
2362 const wxGridCellAttr
& attr
,
2366 dc
.SetBackgroundMode( wxBRUSHSTYLE_TRANSPARENT
);
2368 // TODO some special colours for attr.IsReadOnly() case?
2370 // different coloured text when the grid is disabled
2371 if ( grid
.IsEnabled() )
2376 if ( grid
.HasFocus() )
2377 clr
= grid
.GetSelectionBackground();
2379 clr
= wxSystemSettings
::GetColour(wxSYS_COLOUR_BTNSHADOW
);
2380 dc
.SetTextBackground( clr
);
2381 dc
.SetTextForeground( grid
.GetSelectionForeground() );
2385 dc
.SetTextBackground( attr
.GetBackgroundColour() );
2386 dc
.SetTextForeground( attr
.GetTextColour() );
2391 dc
.SetTextBackground(wxSystemSettings
::GetColour(wxSYS_COLOUR_BTNFACE
));
2392 dc
.SetTextForeground(wxSystemSettings
::GetColour(wxSYS_COLOUR_GRAYTEXT
));
2395 dc
.SetFont( attr
.GetFont() );
2398 wxSize wxGridCellStringRenderer
::DoGetBestSize(const wxGridCellAttr
& attr
,
2400 const wxString
& text
)
2402 wxCoord x
= 0, y
= 0, max_x
= 0;
2403 dc
.SetFont(attr
.GetFont());
2404 wxStringTokenizer
tk(text
, _T('\n'));
2405 while ( tk
.HasMoreTokens() )
2407 dc
.GetTextExtent(tk
.GetNextToken(), &x
, &y
);
2408 max_x
= wxMax(max_x
, x
);
2411 y
*= 1 + text
.Freq(wxT('\n')); // multiply by the number of lines.
2413 return wxSize(max_x
, y
);
2416 wxSize wxGridCellStringRenderer
::GetBestSize(wxGrid
& grid
,
2417 wxGridCellAttr
& attr
,
2421 return DoGetBestSize(attr
, dc
, grid
.GetCellValue(row
, col
));
2424 void wxGridCellStringRenderer
::Draw(wxGrid
& grid
,
2425 wxGridCellAttr
& attr
,
2427 const wxRect
& rectCell
,
2431 wxRect rect
= rectCell
;
2434 // erase only this cells background, overflow cells should have been erased
2435 wxGridCellRenderer
::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
2438 attr
.GetAlignment(&hAlign
, &vAlign
);
2440 int overflowCols
= 0;
2442 if (attr
.GetOverflow())
2444 int cols
= grid
.GetNumberCols();
2445 int best_width
= GetBestSize(grid
,attr
,dc
,row
,col
).GetWidth();
2446 int cell_rows
, cell_cols
;
2447 attr
.GetSize( &cell_rows
, &cell_cols
); // shouldn't get here if <= 0
2448 if ((best_width
> rectCell
.width
) && (col
< cols
) && grid
.GetTable())
2450 int i
, c_cols
, c_rows
;
2451 for (i
= col
+cell_cols
; i
< cols
; i
++)
2453 bool is_empty
= true;
2454 for (int j
=row
; j
< row
+ cell_rows
; j
++)
2456 // check w/ anchor cell for multicell block
2457 grid
.GetCellSize(j
, i
, &c_rows
, &c_cols
);
2460 if (!grid
.GetTable()->IsEmptyCell(j
+ c_rows
, i
))
2469 rect
.width
+= grid
.GetColSize(i
);
2477 if (rect
.width
>= best_width
)
2481 overflowCols
= i
- col
- cell_cols
+ 1;
2482 if (overflowCols
>= cols
)
2483 overflowCols
= cols
- 1;
2486 if (overflowCols
> 0) // redraw overflow cells w/ proper hilight
2488 hAlign
= wxALIGN_LEFT
; // if oveflowed then it's left aligned
2490 clip
.x
+= rectCell
.width
;
2491 // draw each overflow cell individually
2492 int col_end
= col
+ cell_cols
+ overflowCols
;
2493 if (col_end
>= grid
.GetNumberCols())
2494 col_end
= grid
.GetNumberCols() - 1;
2495 for (int i
= col
+ cell_cols
; i
<= col_end
; i
++)
2497 clip
.width
= grid
.GetColSize(i
) - 1;
2498 dc
.DestroyClippingRegion();
2499 dc
.SetClippingRegion(clip
);
2501 SetTextColoursAndFont(grid
, attr
, dc
,
2502 grid
.IsInSelection(row
,i
));
2504 grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
),
2505 rect
, hAlign
, vAlign
);
2506 clip
.x
+= grid
.GetColSize(i
) - 1;
2512 dc
.DestroyClippingRegion();
2516 // now we only have to draw the text
2517 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
2519 grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
),
2520 rect
, hAlign
, vAlign
);
2523 // ----------------------------------------------------------------------------
2524 // wxGridCellNumberRenderer
2525 // ----------------------------------------------------------------------------
2527 wxString wxGridCellNumberRenderer
::GetString(const wxGrid
& grid
, int row
, int col
)
2529 wxGridTableBase
*table
= grid
.GetTable();
2531 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
2533 text
.Printf(_T("%ld"), table
->GetValueAsLong(row
, col
));
2537 text
= table
->GetValue(row
, col
);
2543 void wxGridCellNumberRenderer
::Draw(wxGrid
& grid
,
2544 wxGridCellAttr
& attr
,
2546 const wxRect
& rectCell
,
2550 wxGridCellRenderer
::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
2552 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
2554 // draw the text right aligned by default
2556 attr
.GetAlignment(&hAlign
, &vAlign
);
2557 hAlign
= wxALIGN_RIGHT
;
2559 wxRect rect
= rectCell
;
2562 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
2565 wxSize wxGridCellNumberRenderer
::GetBestSize(wxGrid
& grid
,
2566 wxGridCellAttr
& attr
,
2570 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
2573 // ----------------------------------------------------------------------------
2574 // wxGridCellFloatRenderer
2575 // ----------------------------------------------------------------------------
2577 wxGridCellFloatRenderer
::wxGridCellFloatRenderer(int width
, int precision
)
2580 SetPrecision(precision
);
2583 wxGridCellRenderer
*wxGridCellFloatRenderer
::Clone() const
2585 wxGridCellFloatRenderer
*renderer
= new wxGridCellFloatRenderer
;
2586 renderer
->m_width
= m_width
;
2587 renderer
->m_precision
= m_precision
;
2588 renderer
->m_format
= m_format
;
2593 wxString wxGridCellFloatRenderer
::GetString(const wxGrid
& grid
, int row
, int col
)
2595 wxGridTableBase
*table
= grid
.GetTable();
2600 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
2602 val
= table
->GetValueAsDouble(row
, col
);
2607 text
= table
->GetValue(row
, col
);
2608 hasDouble
= text
.ToDouble(&val
);
2615 if ( m_width
== -1 )
2617 if ( m_precision
== -1 )
2619 // default width/precision
2620 m_format
= _T("%f");
2624 m_format
.Printf(_T("%%.%df"), m_precision
);
2627 else if ( m_precision
== -1 )
2629 // default precision
2630 m_format
.Printf(_T("%%%d.f"), m_width
);
2634 m_format
.Printf(_T("%%%d.%df"), m_width
, m_precision
);
2638 text
.Printf(m_format
, val
);
2641 //else: text already contains the string
2646 void wxGridCellFloatRenderer
::Draw(wxGrid
& grid
,
2647 wxGridCellAttr
& attr
,
2649 const wxRect
& rectCell
,
2653 wxGridCellRenderer
::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
2655 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
2657 // draw the text right aligned by default
2659 attr
.GetAlignment(&hAlign
, &vAlign
);
2660 hAlign
= wxALIGN_RIGHT
;
2662 wxRect rect
= rectCell
;
2665 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
2668 wxSize wxGridCellFloatRenderer
::GetBestSize(wxGrid
& grid
,
2669 wxGridCellAttr
& attr
,
2673 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
2676 void wxGridCellFloatRenderer
::SetParameters(const wxString
& params
)
2680 // reset to defaults
2686 wxString tmp
= params
.BeforeFirst(_T(','));
2690 if ( tmp
.ToLong(&width
) )
2692 SetWidth((int)width
);
2696 wxLogDebug(_T("Invalid wxGridCellFloatRenderer width parameter string '%s ignored"), params
.c_str());
2700 tmp
= params
.AfterFirst(_T(','));
2704 if ( tmp
.ToLong(&precision
) )
2706 SetPrecision((int)precision
);
2710 wxLogDebug(_T("Invalid wxGridCellFloatRenderer precision parameter string '%s ignored"), params
.c_str());
2716 // ----------------------------------------------------------------------------
2717 // wxGridCellBoolRenderer
2718 // ----------------------------------------------------------------------------
2720 wxSize wxGridCellBoolRenderer
::ms_sizeCheckMark
;
2722 // FIXME these checkbox size calculations are really ugly...
2724 // between checkmark and box
2725 static const wxCoord wxGRID_CHECKMARK_MARGIN
= 2;
2727 wxSize wxGridCellBoolRenderer
::GetBestSize(wxGrid
& grid
,
2728 wxGridCellAttr
& WXUNUSED(attr
),
2733 // compute it only once (no locks for MT safeness in GUI thread...)
2734 if ( !ms_sizeCheckMark
.x
)
2736 // get checkbox size
2737 wxCheckBox
*checkbox
= new wxCheckBox(&grid
, wxID_ANY
, wxEmptyString
);
2738 wxSize size
= checkbox
->GetBestSize();
2739 wxCoord checkSize
= size
.y
+ 2 * wxGRID_CHECKMARK_MARGIN
;
2741 #if defined(__WXMOTIF__)
2742 checkSize
-= size
.y
/ 2;
2747 ms_sizeCheckMark
.x
= ms_sizeCheckMark
.y
= checkSize
;
2750 return ms_sizeCheckMark
;
2753 void wxGridCellBoolRenderer
::Draw(wxGrid
& grid
,
2754 wxGridCellAttr
& attr
,
2760 wxGridCellRenderer
::Draw(grid
, attr
, dc
, rect
, row
, col
, isSelected
);
2762 // draw a check mark in the centre (ignoring alignment - TODO)
2763 wxSize size
= GetBestSize(grid
, attr
, dc
, row
, col
);
2765 // don't draw outside the cell
2766 wxCoord minSize
= wxMin(rect
.width
, rect
.height
);
2767 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
2769 // and even leave (at least) 1 pixel margin
2770 size
.x
= size
.y
= minSize
;
2773 // draw a border around checkmark
2775 attr
.GetAlignment(&hAlign
, &vAlign
);
2778 if (hAlign
== wxALIGN_CENTRE
)
2780 rectBorder
.x
= rect
.x
+ rect
.width
/ 2 - size
.x
/ 2;
2781 rectBorder
.y
= rect
.y
+ rect
.height
/ 2 - size
.y
/ 2;
2782 rectBorder
.width
= size
.x
;
2783 rectBorder
.height
= size
.y
;
2785 else if (hAlign
== wxALIGN_LEFT
)
2787 rectBorder
.x
= rect
.x
+ 2;
2788 rectBorder
.y
= rect
.y
+ rect
.height
/ 2 - size
.y
/ 2;
2789 rectBorder
.width
= size
.x
;
2790 rectBorder
.height
= size
.y
;
2792 else if (hAlign
== wxALIGN_RIGHT
)
2794 rectBorder
.x
= rect
.x
+ rect
.width
- size
.x
- 2;
2795 rectBorder
.y
= rect
.y
+ rect
.height
/ 2 - size
.y
/ 2;
2796 rectBorder
.width
= size
.x
;
2797 rectBorder
.height
= size
.y
;
2801 if ( grid
.GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
) )
2803 value
= grid
.GetTable()->GetValueAsBool(row
, col
);
2807 wxString
cellval( grid
.GetTable()->GetValue(row
, col
) );
2808 value
= wxGridCellBoolEditor
::IsTrueValue(cellval
);
2813 flags
|= wxCONTROL_CHECKED
;
2815 wxRendererNative
::Get().DrawCheckBox( &grid
, dc
, rectBorder
, flags
);
2818 // ----------------------------------------------------------------------------
2820 // ----------------------------------------------------------------------------
2822 void wxGridCellAttr
::Init(wxGridCellAttr
*attrDefault
)
2826 m_isReadOnly
= Unset
;
2831 m_attrkind
= wxGridCellAttr
::Cell
;
2833 m_sizeRows
= m_sizeCols
= 1;
2834 m_overflow
= UnsetOverflow
;
2836 SetDefAttr(attrDefault
);
2839 wxGridCellAttr
*wxGridCellAttr
::Clone() const
2841 wxGridCellAttr
*attr
= new wxGridCellAttr(m_defGridAttr
);
2843 if ( HasTextColour() )
2844 attr
->SetTextColour(GetTextColour());
2845 if ( HasBackgroundColour() )
2846 attr
->SetBackgroundColour(GetBackgroundColour());
2848 attr
->SetFont(GetFont());
2849 if ( HasAlignment() )
2850 attr
->SetAlignment(m_hAlign
, m_vAlign
);
2852 attr
->SetSize( m_sizeRows
, m_sizeCols
);
2856 attr
->SetRenderer(m_renderer
);
2857 m_renderer
->IncRef();
2861 attr
->SetEditor(m_editor
);
2866 attr
->SetReadOnly();
2868 attr
->SetOverflow( m_overflow
== Overflow
);
2869 attr
->SetKind( m_attrkind
);
2874 void wxGridCellAttr
::MergeWith(wxGridCellAttr
*mergefrom
)
2876 if ( !HasTextColour() && mergefrom
->HasTextColour() )
2877 SetTextColour(mergefrom
->GetTextColour());
2878 if ( !HasBackgroundColour() && mergefrom
->HasBackgroundColour() )
2879 SetBackgroundColour(mergefrom
->GetBackgroundColour());
2880 if ( !HasFont() && mergefrom
->HasFont() )
2881 SetFont(mergefrom
->GetFont());
2882 if ( !HasAlignment() && mergefrom
->HasAlignment() )
2885 mergefrom
->GetAlignment( &hAlign
, &vAlign
);
2886 SetAlignment(hAlign
, vAlign
);
2888 if ( !HasSize() && mergefrom
->HasSize() )
2889 mergefrom
->GetSize( &m_sizeRows
, &m_sizeCols
);
2891 // Directly access member functions as GetRender/Editor don't just return
2892 // m_renderer/m_editor
2894 // Maybe add support for merge of Render and Editor?
2895 if (!HasRenderer() && mergefrom
->HasRenderer() )
2897 m_renderer
= mergefrom
->m_renderer
;
2898 m_renderer
->IncRef();
2900 if ( !HasEditor() && mergefrom
->HasEditor() )
2902 m_editor
= mergefrom
->m_editor
;
2905 if ( !HasReadWriteMode() && mergefrom
->HasReadWriteMode() )
2906 SetReadOnly(mergefrom
->IsReadOnly());
2908 if (!HasOverflowMode() && mergefrom
->HasOverflowMode() )
2909 SetOverflow(mergefrom
->GetOverflow());
2911 SetDefAttr(mergefrom
->m_defGridAttr
);
2914 void wxGridCellAttr
::SetSize(int num_rows
, int num_cols
)
2916 // The size of a cell is normally 1,1
2918 // If this cell is larger (2,2) then this is the top left cell
2919 // the other cells that will be covered (lower right cells) must be
2920 // set to negative or zero values such that
2921 // row + num_rows of the covered cell points to the larger cell (this cell)
2922 // same goes for the col + num_cols.
2924 // Size of 0,0 is NOT valid, neither is <=0 and any positive value
2926 wxASSERT_MSG( (!((num_rows
> 0) && (num_cols
<= 0)) ||
2927 !((num_rows
<= 0) && (num_cols
> 0)) ||
2928 !((num_rows
== 0) && (num_cols
== 0))),
2929 wxT("wxGridCellAttr::SetSize only takes two postive values or negative/zero values"));
2931 m_sizeRows
= num_rows
;
2932 m_sizeCols
= num_cols
;
2935 const wxColour
& wxGridCellAttr
::GetTextColour() const
2937 if (HasTextColour())
2941 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2943 return m_defGridAttr
->GetTextColour();
2947 wxFAIL_MSG(wxT("Missing default cell attribute"));
2948 return wxNullColour
;
2952 const wxColour
& wxGridCellAttr
::GetBackgroundColour() const
2954 if (HasBackgroundColour())
2958 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2960 return m_defGridAttr
->GetBackgroundColour();
2964 wxFAIL_MSG(wxT("Missing default cell attribute"));
2965 return wxNullColour
;
2969 const wxFont
& wxGridCellAttr
::GetFont() const
2975 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2977 return m_defGridAttr
->GetFont();
2981 wxFAIL_MSG(wxT("Missing default cell attribute"));
2986 void wxGridCellAttr
::GetAlignment(int *hAlign
, int *vAlign
) const
2995 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2997 m_defGridAttr
->GetAlignment(hAlign
, vAlign
);
3001 wxFAIL_MSG(wxT("Missing default cell attribute"));
3005 void wxGridCellAttr
::GetSize( int *num_rows
, int *num_cols
) const
3008 *num_rows
= m_sizeRows
;
3010 *num_cols
= m_sizeCols
;
3013 // GetRenderer and GetEditor use a slightly different decision path about
3014 // which attribute to use. If a non-default attr object has one then it is
3015 // used, otherwise the default editor or renderer is fetched from the grid and
3016 // used. It should be the default for the data type of the cell. If it is
3017 // NULL (because the table has a type that the grid does not have in its
3018 // registry), then the grid's default editor or renderer is used.
3020 wxGridCellRenderer
* wxGridCellAttr
::GetRenderer(const wxGrid
* grid
, int row
, int col
) const
3022 wxGridCellRenderer
*renderer
= NULL
;
3024 if ( m_renderer
&& this != m_defGridAttr
)
3026 // use the cells renderer if it has one
3027 renderer
= m_renderer
;
3030 else // no non-default cell renderer
3032 // get default renderer for the data type
3035 // GetDefaultRendererForCell() will do IncRef() for us
3036 renderer
= grid
->GetDefaultRendererForCell(row
, col
);
3039 if ( renderer
== NULL
)
3041 if ( (m_defGridAttr
!= NULL
) && (m_defGridAttr
!= this) )
3043 // if we still don't have one then use the grid default
3044 // (no need for IncRef() here neither)
3045 renderer
= m_defGridAttr
->GetRenderer(NULL
, 0, 0);
3047 else // default grid attr
3049 // use m_renderer which we had decided not to use initially
3050 renderer
= m_renderer
;
3057 // we're supposed to always find something
3058 wxASSERT_MSG(renderer
, wxT("Missing default cell renderer"));
3063 // same as above, except for s/renderer/editor/g
3064 wxGridCellEditor
* wxGridCellAttr
::GetEditor(const wxGrid
* grid
, int row
, int col
) const
3066 wxGridCellEditor
*editor
= NULL
;
3068 if ( m_editor
&& this != m_defGridAttr
)
3070 // use the cells editor if it has one
3074 else // no non default cell editor
3076 // get default editor for the data type
3079 // GetDefaultEditorForCell() will do IncRef() for us
3080 editor
= grid
->GetDefaultEditorForCell(row
, col
);
3083 if ( editor
== NULL
)
3085 if ( (m_defGridAttr
!= NULL
) && (m_defGridAttr
!= this) )
3087 // if we still don't have one then use the grid default
3088 // (no need for IncRef() here neither)
3089 editor
= m_defGridAttr
->GetEditor(NULL
, 0, 0);
3091 else // default grid attr
3093 // use m_editor which we had decided not to use initially
3101 // we're supposed to always find something
3102 wxASSERT_MSG(editor
, wxT("Missing default cell editor"));
3107 // ----------------------------------------------------------------------------
3108 // wxGridCellAttrData
3109 // ----------------------------------------------------------------------------
3111 void wxGridCellAttrData
::SetAttr(wxGridCellAttr
*attr
, int row
, int col
)
3113 // Note: contrary to wxGridRowOrColAttrData::SetAttr, we must not
3114 // touch attribute's reference counting explicitly, since this
3115 // is managed by class wxGridCellWithAttr
3116 int n
= FindIndex(row
, col
);
3117 if ( n
== wxNOT_FOUND
)
3121 // add the attribute
3122 m_attrs
.Add(new wxGridCellWithAttr(row
, col
, attr
));
3124 //else: nothing to do
3126 else // we already have an attribute for this cell
3130 // change the attribute
3131 m_attrs
[(size_t)n
].ChangeAttr(attr
);
3135 // remove this attribute
3136 m_attrs
.RemoveAt((size_t)n
);
3141 wxGridCellAttr
*wxGridCellAttrData
::GetAttr(int row
, int col
) const
3143 wxGridCellAttr
*attr
= NULL
;
3145 int n
= FindIndex(row
, col
);
3146 if ( n
!= wxNOT_FOUND
)
3148 attr
= m_attrs
[(size_t)n
].attr
;
3155 void wxGridCellAttrData
::UpdateAttrRows( size_t pos
, int numRows
)
3157 size_t count
= m_attrs
.GetCount();
3158 for ( size_t n
= 0; n
< count
; n
++ )
3160 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
3161 wxCoord row
= coords
.GetRow();
3162 if ((size_t)row
>= pos
)
3166 // If rows inserted, include row counter where necessary
3167 coords
.SetRow(row
+ numRows
);
3169 else if (numRows
< 0)
3171 // If rows deleted ...
3172 if ((size_t)row
>= pos
- numRows
)
3174 // ...either decrement row counter (if row still exists)...
3175 coords
.SetRow(row
+ numRows
);
3179 // ...or remove the attribute
3180 m_attrs
.RemoveAt(n
);
3189 void wxGridCellAttrData
::UpdateAttrCols( size_t pos
, int numCols
)
3191 size_t count
= m_attrs
.GetCount();
3192 for ( size_t n
= 0; n
< count
; n
++ )
3194 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
3195 wxCoord col
= coords
.GetCol();
3196 if ( (size_t)col
>= pos
)
3200 // If rows inserted, include row counter where necessary
3201 coords
.SetCol(col
+ numCols
);
3203 else if (numCols
< 0)
3205 // If rows deleted ...
3206 if ((size_t)col
>= pos
- numCols
)
3208 // ...either decrement row counter (if row still exists)...
3209 coords
.SetCol(col
+ numCols
);
3213 // ...or remove the attribute
3214 m_attrs
.RemoveAt(n
);
3223 int wxGridCellAttrData
::FindIndex(int row
, int col
) const
3225 size_t count
= m_attrs
.GetCount();
3226 for ( size_t n
= 0; n
< count
; n
++ )
3228 const wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
3229 if ( (coords
.GetRow() == row
) && (coords
.GetCol() == col
) )
3238 // ----------------------------------------------------------------------------
3239 // wxGridRowOrColAttrData
3240 // ----------------------------------------------------------------------------
3242 wxGridRowOrColAttrData
::~wxGridRowOrColAttrData()
3244 size_t count
= m_attrs
.GetCount();
3245 for ( size_t n
= 0; n
< count
; n
++ )
3247 m_attrs
[n
]->DecRef();
3251 wxGridCellAttr
*wxGridRowOrColAttrData
::GetAttr(int rowOrCol
) const
3253 wxGridCellAttr
*attr
= NULL
;
3255 int n
= m_rowsOrCols
.Index(rowOrCol
);
3256 if ( n
!= wxNOT_FOUND
)
3258 attr
= m_attrs
[(size_t)n
];
3265 void wxGridRowOrColAttrData
::SetAttr(wxGridCellAttr
*attr
, int rowOrCol
)
3267 int i
= m_rowsOrCols
.Index(rowOrCol
);
3268 if ( i
== wxNOT_FOUND
)
3272 // add the attribute - no need to do anything to reference count
3273 // since we take ownership of the attribute.
3274 m_rowsOrCols
.Add(rowOrCol
);
3277 // nothing to remove
3281 size_t n
= (size_t)i
;
3282 if ( m_attrs
[n
] == attr
)
3287 // change the attribute, handling reference count manually,
3288 // taking ownership of the new attribute.
3289 m_attrs
[n
]->DecRef();
3294 // remove this attribute, handling reference count manually
3295 m_attrs
[n
]->DecRef();
3296 m_rowsOrCols
.RemoveAt(n
);
3297 m_attrs
.RemoveAt(n
);
3302 void wxGridRowOrColAttrData
::UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
)
3304 size_t count
= m_attrs
.GetCount();
3305 for ( size_t n
= 0; n
< count
; n
++ )
3307 int & rowOrCol
= m_rowsOrCols
[n
];
3308 if ( (size_t)rowOrCol
>= pos
)
3310 if ( numRowsOrCols
> 0 )
3312 // If rows inserted, include row counter where necessary
3313 rowOrCol
+= numRowsOrCols
;
3315 else if ( numRowsOrCols
< 0)
3317 // If rows deleted, either decrement row counter (if row still exists)
3318 if ((size_t)rowOrCol
>= pos
- numRowsOrCols
)
3319 rowOrCol
+= numRowsOrCols
;
3322 m_rowsOrCols
.RemoveAt(n
);
3323 m_attrs
[n
]->DecRef();
3324 m_attrs
.RemoveAt(n
);
3333 // ----------------------------------------------------------------------------
3334 // wxGridCellAttrProvider
3335 // ----------------------------------------------------------------------------
3337 wxGridCellAttrProvider
::wxGridCellAttrProvider()
3342 wxGridCellAttrProvider
::~wxGridCellAttrProvider()
3347 void wxGridCellAttrProvider
::InitData()
3349 m_data
= new wxGridCellAttrProviderData
;
3352 wxGridCellAttr
*wxGridCellAttrProvider
::GetAttr(int row
, int col
,
3353 wxGridCellAttr
::wxAttrKind kind
) const
3355 wxGridCellAttr
*attr
= NULL
;
3360 case (wxGridCellAttr
::Any
):
3361 // Get cached merge attributes.
3362 // Currently not used as no cache implemented as not mutable
3363 // attr = m_data->m_mergeAttr.GetAttr(row, col);
3366 // Basically implement old version.
3367 // Also check merge cache, so we don't have to re-merge every time..
3368 wxGridCellAttr
*attrcell
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
3369 wxGridCellAttr
*attrrow
= m_data
->m_rowAttrs
.GetAttr(row
);
3370 wxGridCellAttr
*attrcol
= m_data
->m_colAttrs
.GetAttr(col
);
3372 if ((attrcell
!= attrrow
) && (attrrow
!= attrcol
) && (attrcell
!= attrcol
))
3374 // Two or more are non NULL
3375 attr
= new wxGridCellAttr
;
3376 attr
->SetKind(wxGridCellAttr
::Merged
);
3378 // Order is important..
3381 attr
->MergeWith(attrcell
);
3386 attr
->MergeWith(attrcol
);
3391 attr
->MergeWith(attrrow
);
3395 // store merge attr if cache implemented
3397 //m_data->m_mergeAttr.SetAttr(attr, row, col);
3401 // one or none is non null return it or null.
3420 case (wxGridCellAttr
::Cell
):
3421 attr
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
3424 case (wxGridCellAttr
::Col
):
3425 attr
= m_data
->m_colAttrs
.GetAttr(col
);
3428 case (wxGridCellAttr
::Row
):
3429 attr
= m_data
->m_rowAttrs
.GetAttr(row
);
3434 // (wxGridCellAttr::Default):
3435 // (wxGridCellAttr::Merged):
3443 void wxGridCellAttrProvider
::SetAttr(wxGridCellAttr
*attr
,
3449 m_data
->m_cellAttrs
.SetAttr(attr
, row
, col
);
3452 void wxGridCellAttrProvider
::SetRowAttr(wxGridCellAttr
*attr
, int row
)
3457 m_data
->m_rowAttrs
.SetAttr(attr
, row
);
3460 void wxGridCellAttrProvider
::SetColAttr(wxGridCellAttr
*attr
, int col
)
3465 m_data
->m_colAttrs
.SetAttr(attr
, col
);
3468 void wxGridCellAttrProvider
::UpdateAttrRows( size_t pos
, int numRows
)
3472 m_data
->m_cellAttrs
.UpdateAttrRows( pos
, numRows
);
3474 m_data
->m_rowAttrs
.UpdateAttrRowsOrCols( pos
, numRows
);
3478 void wxGridCellAttrProvider
::UpdateAttrCols( size_t pos
, int numCols
)
3482 m_data
->m_cellAttrs
.UpdateAttrCols( pos
, numCols
);
3484 m_data
->m_colAttrs
.UpdateAttrRowsOrCols( pos
, numCols
);
3488 // ----------------------------------------------------------------------------
3489 // wxGridTypeRegistry
3490 // ----------------------------------------------------------------------------
3492 wxGridTypeRegistry
::~wxGridTypeRegistry()
3494 size_t count
= m_typeinfo
.GetCount();
3495 for ( size_t i
= 0; i
< count
; i
++ )
3496 delete m_typeinfo
[i
];
3499 void wxGridTypeRegistry
::RegisterDataType(const wxString
& typeName
,
3500 wxGridCellRenderer
* renderer
,
3501 wxGridCellEditor
* editor
)
3503 wxGridDataTypeInfo
* info
= new wxGridDataTypeInfo(typeName
, renderer
, editor
);
3505 // is it already registered?
3506 int loc
= FindRegisteredDataType(typeName
);
3507 if ( loc
!= wxNOT_FOUND
)
3509 delete m_typeinfo
[loc
];
3510 m_typeinfo
[loc
] = info
;
3514 m_typeinfo
.Add(info
);
3518 int wxGridTypeRegistry
::FindRegisteredDataType(const wxString
& typeName
)
3520 size_t count
= m_typeinfo
.GetCount();
3521 for ( size_t i
= 0; i
< count
; i
++ )
3523 if ( typeName
== m_typeinfo
[i
]->m_typeName
)
3532 int wxGridTypeRegistry
::FindDataType(const wxString
& typeName
)
3534 int index
= FindRegisteredDataType(typeName
);
3535 if ( index
== wxNOT_FOUND
)
3537 // check whether this is one of the standard ones, in which case
3538 // register it "on the fly"
3540 if ( typeName
== wxGRID_VALUE_STRING
)
3542 RegisterDataType(wxGRID_VALUE_STRING
,
3543 new wxGridCellStringRenderer
,
3544 new wxGridCellTextEditor
);
3547 #endif // wxUSE_TEXTCTRL
3549 if ( typeName
== wxGRID_VALUE_BOOL
)
3551 RegisterDataType(wxGRID_VALUE_BOOL
,
3552 new wxGridCellBoolRenderer
,
3553 new wxGridCellBoolEditor
);
3556 #endif // wxUSE_CHECKBOX
3558 if ( typeName
== wxGRID_VALUE_NUMBER
)
3560 RegisterDataType(wxGRID_VALUE_NUMBER
,
3561 new wxGridCellNumberRenderer
,
3562 new wxGridCellNumberEditor
);
3564 else if ( typeName
== wxGRID_VALUE_FLOAT
)
3566 RegisterDataType(wxGRID_VALUE_FLOAT
,
3567 new wxGridCellFloatRenderer
,
3568 new wxGridCellFloatEditor
);
3571 #endif // wxUSE_TEXTCTRL
3573 if ( typeName
== wxGRID_VALUE_CHOICE
)
3575 RegisterDataType(wxGRID_VALUE_CHOICE
,
3576 new wxGridCellStringRenderer
,
3577 new wxGridCellChoiceEditor
);
3580 #endif // wxUSE_COMBOBOX
3585 // we get here only if just added the entry for this type, so return
3587 index
= m_typeinfo
.GetCount() - 1;
3593 int wxGridTypeRegistry
::FindOrCloneDataType(const wxString
& typeName
)
3595 int index
= FindDataType(typeName
);
3596 if ( index
== wxNOT_FOUND
)
3598 // the first part of the typename is the "real" type, anything after ':'
3599 // are the parameters for the renderer
3600 index
= FindDataType(typeName
.BeforeFirst(_T(':')));
3601 if ( index
== wxNOT_FOUND
)
3606 wxGridCellRenderer
*renderer
= GetRenderer(index
);
3607 wxGridCellRenderer
*rendererOld
= renderer
;
3608 renderer
= renderer
->Clone();
3609 rendererOld
->DecRef();
3611 wxGridCellEditor
*editor
= GetEditor(index
);
3612 wxGridCellEditor
*editorOld
= editor
;
3613 editor
= editor
->Clone();
3614 editorOld
->DecRef();
3616 // do it even if there are no parameters to reset them to defaults
3617 wxString params
= typeName
.AfterFirst(_T(':'));
3618 renderer
->SetParameters(params
);
3619 editor
->SetParameters(params
);
3621 // register the new typename
3622 RegisterDataType(typeName
, renderer
, editor
);
3624 // we just registered it, it's the last one
3625 index
= m_typeinfo
.GetCount() - 1;
3631 wxGridCellRenderer
* wxGridTypeRegistry
::GetRenderer(int index
)
3633 wxGridCellRenderer
* renderer
= m_typeinfo
[index
]->m_renderer
;
3640 wxGridCellEditor
* wxGridTypeRegistry
::GetEditor(int index
)
3642 wxGridCellEditor
* editor
= m_typeinfo
[index
]->m_editor
;
3649 // ----------------------------------------------------------------------------
3651 // ----------------------------------------------------------------------------
3653 IMPLEMENT_ABSTRACT_CLASS( wxGridTableBase
, wxObject
)
3655 wxGridTableBase
::wxGridTableBase()
3658 m_attrProvider
= NULL
;
3661 wxGridTableBase
::~wxGridTableBase()
3663 delete m_attrProvider
;
3666 void wxGridTableBase
::SetAttrProvider(wxGridCellAttrProvider
*attrProvider
)
3668 delete m_attrProvider
;
3669 m_attrProvider
= attrProvider
;
3672 bool wxGridTableBase
::CanHaveAttributes()
3674 if ( ! GetAttrProvider() )
3676 // use the default attr provider by default
3677 SetAttrProvider(new wxGridCellAttrProvider
);
3683 wxGridCellAttr
*wxGridTableBase
::GetAttr(int row
, int col
, wxGridCellAttr
::wxAttrKind kind
)
3685 if ( m_attrProvider
)
3686 return m_attrProvider
->GetAttr(row
, col
, kind
);
3691 void wxGridTableBase
::SetAttr(wxGridCellAttr
* attr
, int row
, int col
)
3693 if ( m_attrProvider
)
3696 attr
->SetKind(wxGridCellAttr
::Cell
);
3697 m_attrProvider
->SetAttr(attr
, row
, col
);
3701 // as we take ownership of the pointer and don't store it, we must
3707 void wxGridTableBase
::SetRowAttr(wxGridCellAttr
*attr
, int row
)
3709 if ( m_attrProvider
)
3711 attr
->SetKind(wxGridCellAttr
::Row
);
3712 m_attrProvider
->SetRowAttr(attr
, row
);
3716 // as we take ownership of the pointer and don't store it, we must
3722 void wxGridTableBase
::SetColAttr(wxGridCellAttr
*attr
, int col
)
3724 if ( m_attrProvider
)
3726 attr
->SetKind(wxGridCellAttr
::Col
);
3727 m_attrProvider
->SetColAttr(attr
, col
);
3731 // as we take ownership of the pointer and don't store it, we must
3737 bool wxGridTableBase
::InsertRows( size_t WXUNUSED(pos
),
3738 size_t WXUNUSED(numRows
) )
3740 wxFAIL_MSG( wxT("Called grid table class function InsertRows\nbut your derived table class does not override this function") );
3745 bool wxGridTableBase
::AppendRows( size_t WXUNUSED(numRows
) )
3747 wxFAIL_MSG( wxT("Called grid table class function AppendRows\nbut your derived table class does not override this function"));
3752 bool wxGridTableBase
::DeleteRows( size_t WXUNUSED(pos
),
3753 size_t WXUNUSED(numRows
) )
3755 wxFAIL_MSG( wxT("Called grid table class function DeleteRows\nbut your derived table class does not override this function"));
3760 bool wxGridTableBase
::InsertCols( size_t WXUNUSED(pos
),
3761 size_t WXUNUSED(numCols
) )
3763 wxFAIL_MSG( wxT("Called grid table class function InsertCols\nbut your derived table class does not override this function"));
3768 bool wxGridTableBase
::AppendCols( size_t WXUNUSED(numCols
) )
3770 wxFAIL_MSG(wxT("Called grid table class function AppendCols\nbut your derived table class does not override this function"));
3775 bool wxGridTableBase
::DeleteCols( size_t WXUNUSED(pos
),
3776 size_t WXUNUSED(numCols
) )
3778 wxFAIL_MSG( wxT("Called grid table class function DeleteCols\nbut your derived table class does not override this function"));
3783 wxString wxGridTableBase
::GetRowLabelValue( int row
)
3787 // RD: Starting the rows at zero confuses users,
3788 // no matter how much it makes sense to us geeks.
3794 wxString wxGridTableBase
::GetColLabelValue( int col
)
3796 // default col labels are:
3797 // cols 0 to 25 : A-Z
3798 // cols 26 to 675 : AA-ZZ
3803 for ( n
= 1; ; n
++ )
3805 s
+= (wxChar
) (_T('A') + (wxChar
)(col
% 26));
3811 // reverse the string...
3813 for ( i
= 0; i
< n
; i
++ )
3821 wxString wxGridTableBase
::GetTypeName( int WXUNUSED(row
), int WXUNUSED(col
) )
3823 return wxGRID_VALUE_STRING
;
3826 bool wxGridTableBase
::CanGetValueAs( int WXUNUSED(row
), int WXUNUSED(col
),
3827 const wxString
& typeName
)
3829 return typeName
== wxGRID_VALUE_STRING
;
3832 bool wxGridTableBase
::CanSetValueAs( int row
, int col
, const wxString
& typeName
)
3834 return CanGetValueAs(row
, col
, typeName
);
3837 long wxGridTableBase
::GetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
) )
3842 double wxGridTableBase
::GetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
) )
3847 bool wxGridTableBase
::GetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
) )
3852 void wxGridTableBase
::SetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
),
3853 long WXUNUSED(value
) )
3857 void wxGridTableBase
::SetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
),
3858 double WXUNUSED(value
) )
3862 void wxGridTableBase
::SetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
),
3863 bool WXUNUSED(value
) )
3867 void* wxGridTableBase
::GetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
3868 const wxString
& WXUNUSED(typeName
) )
3873 void wxGridTableBase
::SetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
3874 const wxString
& WXUNUSED(typeName
),
3875 void* WXUNUSED(value
) )
3879 //////////////////////////////////////////////////////////////////////
3881 // Message class for the grid table to send requests and notifications
3885 wxGridTableMessage
::wxGridTableMessage()
3893 wxGridTableMessage
::wxGridTableMessage( wxGridTableBase
*table
, int id
,
3894 int commandInt1
, int commandInt2
)
3898 m_comInt1
= commandInt1
;
3899 m_comInt2
= commandInt2
;
3902 //////////////////////////////////////////////////////////////////////
3904 // A basic grid table for string data. An object of this class will
3905 // created by wxGrid if you don't specify an alternative table class.
3908 WX_DEFINE_OBJARRAY(wxGridStringArray
)
3910 IMPLEMENT_DYNAMIC_CLASS( wxGridStringTable
, wxGridTableBase
)
3912 wxGridStringTable
::wxGridStringTable()
3917 wxGridStringTable
::wxGridStringTable( int numRows
, int numCols
)
3920 m_data
.Alloc( numRows
);
3923 sa
.Alloc( numCols
);
3924 sa
.Add( wxEmptyString
, numCols
);
3926 m_data
.Add( sa
, numRows
);
3929 wxGridStringTable
::~wxGridStringTable()
3933 int wxGridStringTable
::GetNumberRows()
3935 return m_data
.GetCount();
3938 int wxGridStringTable
::GetNumberCols()
3940 if ( m_data
.GetCount() > 0 )
3941 return m_data
[0].GetCount();
3946 wxString wxGridStringTable
::GetValue( int row
, int col
)
3948 wxCHECK_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3950 _T("invalid row or column index in wxGridStringTable") );
3952 return m_data
[row
][col
];
3955 void wxGridStringTable
::SetValue( int row
, int col
, const wxString
& value
)
3957 wxCHECK_RET( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3958 _T("invalid row or column index in wxGridStringTable") );
3960 m_data
[row
][col
] = value
;
3963 void wxGridStringTable
::Clear()
3966 int numRows
, numCols
;
3968 numRows
= m_data
.GetCount();
3971 numCols
= m_data
[0].GetCount();
3973 for ( row
= 0; row
< numRows
; row
++ )
3975 for ( col
= 0; col
< numCols
; col
++ )
3977 m_data
[row
][col
] = wxEmptyString
;
3983 bool wxGridStringTable
::InsertRows( size_t pos
, size_t numRows
)
3985 size_t curNumRows
= m_data
.GetCount();
3986 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3987 ( GetView() ?
GetView()->GetNumberCols() : 0 ) );
3989 if ( pos
>= curNumRows
)
3991 return AppendRows( numRows
);
3995 sa
.Alloc( curNumCols
);
3996 sa
.Add( wxEmptyString
, curNumCols
);
3997 m_data
.Insert( sa
, pos
, numRows
);
4001 wxGridTableMessage
msg( this,
4002 wxGRIDTABLE_NOTIFY_ROWS_INSERTED
,
4006 GetView()->ProcessTableMessage( msg
);
4012 bool wxGridStringTable
::AppendRows( size_t numRows
)
4014 size_t curNumRows
= m_data
.GetCount();
4015 size_t curNumCols
= ( curNumRows
> 0
4016 ? m_data
[0].GetCount()
4017 : ( GetView() ?
GetView()->GetNumberCols() : 0 ) );
4020 if ( curNumCols
> 0 )
4022 sa
.Alloc( curNumCols
);
4023 sa
.Add( wxEmptyString
, curNumCols
);
4026 m_data
.Add( sa
, numRows
);
4030 wxGridTableMessage
msg( this,
4031 wxGRIDTABLE_NOTIFY_ROWS_APPENDED
,
4034 GetView()->ProcessTableMessage( msg
);
4040 bool wxGridStringTable
::DeleteRows( size_t pos
, size_t numRows
)
4042 size_t curNumRows
= m_data
.GetCount();
4044 if ( pos
>= curNumRows
)
4046 wxFAIL_MSG( wxString
::Format
4048 wxT("Called wxGridStringTable::DeleteRows(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu rows"),
4050 (unsigned long)numRows
,
4051 (unsigned long)curNumRows
4057 if ( numRows
> curNumRows
- pos
)
4059 numRows
= curNumRows
- pos
;
4062 if ( numRows
>= curNumRows
)
4068 m_data
.RemoveAt( pos
, numRows
);
4073 wxGridTableMessage
msg( this,
4074 wxGRIDTABLE_NOTIFY_ROWS_DELETED
,
4078 GetView()->ProcessTableMessage( msg
);
4084 bool wxGridStringTable
::InsertCols( size_t pos
, size_t numCols
)
4088 size_t curNumRows
= m_data
.GetCount();
4089 size_t curNumCols
= ( curNumRows
> 0
4090 ? m_data
[0].GetCount()
4091 : ( GetView() ?
GetView()->GetNumberCols() : 0 ) );
4093 if ( pos
>= curNumCols
)
4095 return AppendCols( numCols
);
4098 if ( !m_colLabels
.IsEmpty() )
4100 m_colLabels
.Insert( wxEmptyString
, pos
, numCols
);
4103 for ( i
= pos
; i
< pos
+ numCols
; i
++ )
4104 m_colLabels
[i
] = wxGridTableBase
::GetColLabelValue( i
);
4107 for ( row
= 0; row
< curNumRows
; row
++ )
4109 for ( col
= pos
; col
< pos
+ numCols
; col
++ )
4111 m_data
[row
].Insert( wxEmptyString
, col
);
4117 wxGridTableMessage
msg( this,
4118 wxGRIDTABLE_NOTIFY_COLS_INSERTED
,
4122 GetView()->ProcessTableMessage( msg
);
4128 bool wxGridStringTable
::AppendCols( size_t numCols
)
4132 size_t curNumRows
= m_data
.GetCount();
4134 for ( row
= 0; row
< curNumRows
; row
++ )
4136 m_data
[row
].Add( wxEmptyString
, numCols
);
4141 wxGridTableMessage
msg( this,
4142 wxGRIDTABLE_NOTIFY_COLS_APPENDED
,
4145 GetView()->ProcessTableMessage( msg
);
4151 bool wxGridStringTable
::DeleteCols( size_t pos
, size_t numCols
)
4155 size_t curNumRows
= m_data
.GetCount();
4156 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
4157 ( GetView() ?
GetView()->GetNumberCols() : 0 ) );
4159 if ( pos
>= curNumCols
)
4161 wxFAIL_MSG( wxString
::Format
4163 wxT("Called wxGridStringTable::DeleteCols(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu cols"),
4165 (unsigned long)numCols
,
4166 (unsigned long)curNumCols
4173 colID
= GetView()->GetColAt( pos
);
4177 if ( numCols
> curNumCols
- colID
)
4179 numCols
= curNumCols
- colID
;
4182 if ( !m_colLabels
.IsEmpty() )
4184 // m_colLabels stores just as many elements as it needs, e.g. if only
4185 // the label of the first column had been set it would have only one
4186 // element and not numCols, so account for it
4187 int nToRm
= m_colLabels
.size() - colID
;
4189 m_colLabels
.RemoveAt( colID
, nToRm
);
4192 for ( row
= 0; row
< curNumRows
; row
++ )
4194 if ( numCols
>= curNumCols
)
4196 m_data
[row
].Clear();
4200 m_data
[row
].RemoveAt( colID
, numCols
);
4206 wxGridTableMessage
msg( this,
4207 wxGRIDTABLE_NOTIFY_COLS_DELETED
,
4211 GetView()->ProcessTableMessage( msg
);
4217 wxString wxGridStringTable
::GetRowLabelValue( int row
)
4219 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
4221 // using default label
4223 return wxGridTableBase
::GetRowLabelValue( row
);
4227 return m_rowLabels
[row
];
4231 wxString wxGridStringTable
::GetColLabelValue( int col
)
4233 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
4235 // using default label
4237 return wxGridTableBase
::GetColLabelValue( col
);
4241 return m_colLabels
[col
];
4245 void wxGridStringTable
::SetRowLabelValue( int row
, const wxString
& value
)
4247 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
4249 int n
= m_rowLabels
.GetCount();
4252 for ( i
= n
; i
<= row
; i
++ )
4254 m_rowLabels
.Add( wxGridTableBase
::GetRowLabelValue(i
) );
4258 m_rowLabels
[row
] = value
;
4261 void wxGridStringTable
::SetColLabelValue( int col
, const wxString
& value
)
4263 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
4265 int n
= m_colLabels
.GetCount();
4268 for ( i
= n
; i
<= col
; i
++ )
4270 m_colLabels
.Add( wxGridTableBase
::GetColLabelValue(i
) );
4274 m_colLabels
[col
] = value
;
4278 //////////////////////////////////////////////////////////////////////
4279 //////////////////////////////////////////////////////////////////////
4281 BEGIN_EVENT_TABLE(wxGridSubwindow
, wxWindow
)
4282 EVT_MOUSE_CAPTURE_LOST(wxGridSubwindow
::OnMouseCaptureLost
)
4285 void wxGridSubwindow
::OnMouseCaptureLost(wxMouseCaptureLostEvent
& WXUNUSED(event
))
4287 m_owner
->CancelMouseCapture();
4290 BEGIN_EVENT_TABLE( wxGridRowLabelWindow
, wxGridSubwindow
)
4291 EVT_PAINT( wxGridRowLabelWindow
::OnPaint
)
4292 EVT_MOUSEWHEEL( wxGridRowLabelWindow
::OnMouseWheel
)
4293 EVT_MOUSE_EVENTS( wxGridRowLabelWindow
::OnMouseEvent
)
4296 void wxGridRowLabelWindow
::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
4300 // NO - don't do this because it will set both the x and y origin
4301 // coords to match the parent scrolled window and we just want to
4302 // set the y coord - MB
4304 // m_owner->PrepareDC( dc );
4307 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
4308 wxPoint pt
= dc
.GetDeviceOrigin();
4309 dc
.SetDeviceOrigin( pt
.x
, pt
.y
-y
);
4311 wxArrayInt rows
= m_owner
->CalcRowLabelsExposed( GetUpdateRegion() );
4312 m_owner
->DrawRowLabels( dc
, rows
);
4315 void wxGridRowLabelWindow
::OnMouseEvent( wxMouseEvent
& event
)
4317 m_owner
->ProcessRowLabelMouseEvent( event
);
4320 void wxGridRowLabelWindow
::OnMouseWheel( wxMouseEvent
& event
)
4322 if (!m_owner
->GetEventHandler()->ProcessEvent( event
))
4326 //////////////////////////////////////////////////////////////////////
4328 BEGIN_EVENT_TABLE( wxGridColLabelWindow
, wxGridSubwindow
)
4329 EVT_PAINT( wxGridColLabelWindow
::OnPaint
)
4330 EVT_MOUSEWHEEL( wxGridColLabelWindow
::OnMouseWheel
)
4331 EVT_MOUSE_EVENTS( wxGridColLabelWindow
::OnMouseEvent
)
4334 void wxGridColLabelWindow
::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
4338 // NO - don't do this because it will set both the x and y origin
4339 // coords to match the parent scrolled window and we just want to
4340 // set the x coord - MB
4342 // m_owner->PrepareDC( dc );
4345 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
4346 wxPoint pt
= dc
.GetDeviceOrigin();
4347 if (GetLayoutDirection() == wxLayout_RightToLeft
)
4348 dc
.SetDeviceOrigin( pt
.x
+x
, pt
.y
);
4350 dc
.SetDeviceOrigin( pt
.x
-x
, pt
.y
);
4352 wxArrayInt cols
= m_owner
->CalcColLabelsExposed( GetUpdateRegion() );
4353 m_owner
->DrawColLabels( dc
, cols
);
4356 void wxGridColLabelWindow
::OnMouseEvent( wxMouseEvent
& event
)
4358 m_owner
->ProcessColLabelMouseEvent( event
);
4361 void wxGridColLabelWindow
::OnMouseWheel( wxMouseEvent
& event
)
4363 if (!m_owner
->GetEventHandler()->ProcessEvent( event
))
4367 //////////////////////////////////////////////////////////////////////
4369 BEGIN_EVENT_TABLE( wxGridCornerLabelWindow
, wxGridSubwindow
)
4370 EVT_MOUSEWHEEL( wxGridCornerLabelWindow
::OnMouseWheel
)
4371 EVT_MOUSE_EVENTS( wxGridCornerLabelWindow
::OnMouseEvent
)
4372 EVT_PAINT( wxGridCornerLabelWindow
::OnPaint
)
4375 void wxGridCornerLabelWindow
::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
4379 m_owner
->DrawCornerLabel(dc
);
4382 void wxGridCornerLabelWindow
::OnMouseEvent( wxMouseEvent
& event
)
4384 m_owner
->ProcessCornerLabelMouseEvent( event
);
4387 void wxGridCornerLabelWindow
::OnMouseWheel( wxMouseEvent
& event
)
4389 if (!m_owner
->GetEventHandler()->ProcessEvent(event
))
4393 //////////////////////////////////////////////////////////////////////
4395 BEGIN_EVENT_TABLE( wxGridWindow
, wxGridSubwindow
)
4396 EVT_PAINT( wxGridWindow
::OnPaint
)
4397 EVT_MOUSEWHEEL( wxGridWindow
::OnMouseWheel
)
4398 EVT_MOUSE_EVENTS( wxGridWindow
::OnMouseEvent
)
4399 EVT_KEY_DOWN( wxGridWindow
::OnKeyDown
)
4400 EVT_KEY_UP( wxGridWindow
::OnKeyUp
)
4401 EVT_CHAR( wxGridWindow
::OnChar
)
4402 EVT_SET_FOCUS( wxGridWindow
::OnFocus
)
4403 EVT_KILL_FOCUS( wxGridWindow
::OnFocus
)
4404 EVT_ERASE_BACKGROUND( wxGridWindow
::OnEraseBackground
)
4407 void wxGridWindow
::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
4409 wxPaintDC
dc( this );
4410 m_owner
->PrepareDC( dc
);
4411 wxRegion reg
= GetUpdateRegion();
4412 wxGridCellCoordsArray dirtyCells
= m_owner
->CalcCellsExposed( reg
);
4413 m_owner
->DrawGridCellArea( dc
, dirtyCells
);
4415 m_owner
->DrawGridSpace( dc
);
4417 m_owner
->DrawAllGridLines( dc
, reg
);
4419 m_owner
->DrawHighlight( dc
, dirtyCells
);
4422 void wxGridWindow
::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
4424 wxWindow
::ScrollWindow( dx
, dy
, rect
);
4425 m_owner
->GetGridRowLabelWindow()->ScrollWindow( 0, dy
, rect
);
4426 m_owner
->GetGridColLabelWindow()->ScrollWindow( dx
, 0, rect
);
4429 void wxGridWindow
::OnMouseEvent( wxMouseEvent
& event
)
4431 if (event
.ButtonDown(wxMOUSE_BTN_LEFT
) && FindFocus() != this)
4434 m_owner
->ProcessGridCellMouseEvent( event
);
4437 void wxGridWindow
::OnMouseWheel( wxMouseEvent
& event
)
4439 if (!m_owner
->GetEventHandler()->ProcessEvent( event
))
4443 // This seems to be required for wxMotif/wxGTK otherwise the mouse
4444 // cursor must be in the cell edit control to get key events
4446 void wxGridWindow
::OnKeyDown( wxKeyEvent
& event
)
4448 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
4452 void wxGridWindow
::OnKeyUp( wxKeyEvent
& event
)
4454 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
4458 void wxGridWindow
::OnChar( wxKeyEvent
& event
)
4460 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
4464 void wxGridWindow
::OnEraseBackground( wxEraseEvent
& WXUNUSED(event
) )
4468 void wxGridWindow
::OnFocus(wxFocusEvent
& event
)
4470 // and if we have any selection, it has to be repainted, because it
4471 // uses different colour when the grid is not focused:
4472 if ( m_owner
->IsSelection() )
4478 // NB: Note that this code is in "else" branch only because the other
4479 // branch refreshes everything and so there's no point in calling
4480 // Refresh() again, *not* because it should only be done if
4481 // !IsSelection(). If the above code is ever optimized to refresh
4482 // only selected area, this needs to be moved out of the "else"
4483 // branch so that it's always executed.
4485 // current cell cursor {dis,re}appears on focus change:
4486 const wxGridCellCoords
cursorCoords(m_owner
->GetGridCursorRow(),
4487 m_owner
->GetGridCursorCol());
4488 const wxRect cursor
=
4489 m_owner
->BlockToDeviceRect(cursorCoords
, cursorCoords
);
4490 Refresh(true, &cursor
);
4493 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
4497 #define internalXToCol(x) XToCol(x, true)
4498 #define internalYToRow(y) YToRow(y, true)
4500 /////////////////////////////////////////////////////////////////////
4502 #if wxUSE_EXTENDED_RTTI
4503 WX_DEFINE_FLAGS( wxGridStyle
)
4505 wxBEGIN_FLAGS( wxGridStyle
)
4506 // new style border flags, we put them first to
4507 // use them for streaming out
4508 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
4509 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
4510 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
4511 wxFLAGS_MEMBER(wxBORDER_RAISED
)
4512 wxFLAGS_MEMBER(wxBORDER_STATIC
)
4513 wxFLAGS_MEMBER(wxBORDER_NONE
)
4515 // old style border flags
4516 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
4517 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
4518 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
4519 wxFLAGS_MEMBER(wxRAISED_BORDER
)
4520 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
4521 wxFLAGS_MEMBER(wxBORDER
)
4523 // standard window styles
4524 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
4525 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
4526 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
4527 wxFLAGS_MEMBER(wxWANTS_CHARS
)
4528 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE
)
4529 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
4530 wxFLAGS_MEMBER(wxVSCROLL
)
4531 wxFLAGS_MEMBER(wxHSCROLL
)
4533 wxEND_FLAGS( wxGridStyle
)
4535 IMPLEMENT_DYNAMIC_CLASS_XTI(wxGrid
, wxScrolledWindow
,"wx/grid.h")
4537 wxBEGIN_PROPERTIES_TABLE(wxGrid
)
4538 wxHIDE_PROPERTY( Children
)
4539 wxPROPERTY_FLAGS( WindowStyle
, wxGridStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
4540 wxEND_PROPERTIES_TABLE()
4542 wxBEGIN_HANDLERS_TABLE(wxGrid
)
4543 wxEND_HANDLERS_TABLE()
4545 wxCONSTRUCTOR_5( wxGrid
, wxWindow
* , Parent
, wxWindowID
, Id
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
4548 TODO : Expose more information of a list's layout, etc. via appropriate objects (e.g., NotebookPageInfo)
4551 IMPLEMENT_DYNAMIC_CLASS( wxGrid
, wxScrolledWindow
)
4554 BEGIN_EVENT_TABLE( wxGrid
, wxScrolledWindow
)
4555 EVT_PAINT( wxGrid
::OnPaint
)
4556 EVT_SIZE( wxGrid
::OnSize
)
4557 EVT_KEY_DOWN( wxGrid
::OnKeyDown
)
4558 EVT_KEY_UP( wxGrid
::OnKeyUp
)
4559 EVT_CHAR ( wxGrid
::OnChar
)
4560 EVT_ERASE_BACKGROUND( wxGrid
::OnEraseBackground
)
4563 bool wxGrid
::Create(wxWindow
*parent
, wxWindowID id
,
4564 const wxPoint
& pos
, const wxSize
& size
,
4565 long style
, const wxString
& name
)
4567 if (!wxScrolledWindow
::Create(parent
, id
, pos
, size
,
4568 style
| wxWANTS_CHARS
, name
))
4571 m_colMinWidths
= wxLongToLongHashMap(GRID_HASH_SIZE
);
4572 m_rowMinHeights
= wxLongToLongHashMap(GRID_HASH_SIZE
);
4575 SetInitialSize(size
);
4576 SetScrollRate(m_scrollLineX
, m_scrollLineY
);
4585 m_winCapture
->ReleaseMouse();
4587 // Ensure that the editor control is destroyed before the grid is,
4588 // otherwise we crash later when the editor tries to do something with the
4589 // half destroyed grid
4590 HideCellEditControl();
4592 // Must do this or ~wxScrollHelper will pop the wrong event handler
4593 SetTargetWindow(this);
4595 wxSafeDecRef(m_defaultCellAttr
);
4597 #ifdef DEBUG_ATTR_CACHE
4598 size_t total
= gs_nAttrCacheHits
+ gs_nAttrCacheMisses
;
4599 wxPrintf(_T("wxGrid attribute cache statistics: "
4600 "total: %u, hits: %u (%u%%)\n"),
4601 total
, gs_nAttrCacheHits
,
4602 total ?
(gs_nAttrCacheHits
*100) / total
: 0);
4605 // if we own the table, just delete it, otherwise at least don't leave it
4606 // with dangling view pointer
4609 else if ( m_table
&& m_table
->GetView() == this )
4610 m_table
->SetView(NULL
);
4612 delete m_typeRegistry
;
4617 // ----- internal init and update functions
4620 // NOTE: If using the default visual attributes works everywhere then this can
4621 // be removed as well as the #else cases below.
4622 #define _USE_VISATTR 0
4624 void wxGrid
::Create()
4626 // create the type registry
4627 m_typeRegistry
= new wxGridTypeRegistry
;
4629 m_cellEditCtrlEnabled
= false;
4631 m_defaultCellAttr
= new wxGridCellAttr();
4633 // Set default cell attributes
4634 m_defaultCellAttr
->SetDefAttr(m_defaultCellAttr
);
4635 m_defaultCellAttr
->SetKind(wxGridCellAttr
::Default
);
4636 m_defaultCellAttr
->SetFont(GetFont());
4637 m_defaultCellAttr
->SetAlignment(wxALIGN_LEFT
, wxALIGN_TOP
);
4638 m_defaultCellAttr
->SetRenderer(new wxGridCellStringRenderer
);
4639 m_defaultCellAttr
->SetEditor(new wxGridCellTextEditor
);
4642 wxVisualAttributes gva
= wxListBox
::GetClassDefaultAttributes();
4643 wxVisualAttributes lva
= wxPanel
::GetClassDefaultAttributes();
4645 m_defaultCellAttr
->SetTextColour(gva
.colFg
);
4646 m_defaultCellAttr
->SetBackgroundColour(gva
.colBg
);
4649 m_defaultCellAttr
->SetTextColour(
4650 wxSystemSettings
::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
4651 m_defaultCellAttr
->SetBackgroundColour(
4652 wxSystemSettings
::GetColour(wxSYS_COLOUR_WINDOW
));
4657 m_currentCellCoords
= wxGridNoCellCoords
;
4659 // subwindow components that make up the wxGrid
4660 m_rowLabelWin
= new wxGridRowLabelWindow(this);
4661 CreateColumnWindow();
4662 m_cornerLabelWin
= new wxGridCornerLabelWindow(this);
4663 m_gridWin
= new wxGridWindow( this );
4665 SetTargetWindow( m_gridWin
);
4668 wxColour gfg
= gva
.colFg
;
4669 wxColour gbg
= gva
.colBg
;
4670 wxColour lfg
= lva
.colFg
;
4671 wxColour lbg
= lva
.colBg
;
4673 wxColour gfg
= wxSystemSettings
::GetColour( wxSYS_COLOUR_WINDOWTEXT
);
4674 wxColour gbg
= wxSystemSettings
::GetColour( wxSYS_COLOUR_WINDOW
);
4675 wxColour lfg
= wxSystemSettings
::GetColour( wxSYS_COLOUR_WINDOWTEXT
);
4676 wxColour lbg
= wxSystemSettings
::GetColour( wxSYS_COLOUR_BTNFACE
);
4679 m_cornerLabelWin
->SetOwnForegroundColour(lfg
);
4680 m_cornerLabelWin
->SetOwnBackgroundColour(lbg
);
4681 m_rowLabelWin
->SetOwnForegroundColour(lfg
);
4682 m_rowLabelWin
->SetOwnBackgroundColour(lbg
);
4683 m_colWindow
->SetOwnForegroundColour(lfg
);
4684 m_colWindow
->SetOwnBackgroundColour(lbg
);
4686 m_gridWin
->SetOwnForegroundColour(gfg
);
4687 m_gridWin
->SetOwnBackgroundColour(gbg
);
4689 m_labelBackgroundColour
= m_rowLabelWin
->GetBackgroundColour();
4690 m_labelTextColour
= m_rowLabelWin
->GetForegroundColour();
4692 // now that we have the grid window, use its font to compute the default
4694 m_defaultRowHeight
= m_gridWin
->GetCharHeight();
4695 #if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl()
4696 m_defaultRowHeight
+= 8;
4698 m_defaultRowHeight
+= 4;
4703 void wxGrid
::CreateColumnWindow()
4705 if ( m_useNativeHeader
)
4707 m_colWindow
= new wxGridHeaderCtrl(this);
4708 m_colLabelHeight
= m_colWindow
->GetBestSize().y
;
4710 else // draw labels ourselves
4712 m_colWindow
= new wxGridColLabelWindow(this);
4713 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
4717 bool wxGrid
::CreateGrid( int numRows
, int numCols
,
4718 wxGridSelectionModes selmode
)
4720 wxCHECK_MSG( !m_created
,
4722 wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
4724 return SetTable(new wxGridStringTable(numRows
, numCols
), true, selmode
);
4727 void wxGrid
::SetSelectionMode(wxGridSelectionModes selmode
)
4729 wxCHECK_RET( m_created
,
4730 wxT("Called wxGrid::SetSelectionMode() before calling CreateGrid()") );
4732 m_selection
->SetSelectionMode( selmode
);
4735 wxGrid
::wxGridSelectionModes wxGrid
::GetSelectionMode() const
4737 wxCHECK_MSG( m_created
, wxGridSelectCells
,
4738 wxT("Called wxGrid::GetSelectionMode() before calling CreateGrid()") );
4740 return m_selection
->GetSelectionMode();
4744 wxGrid
::SetTable(wxGridTableBase
*table
,
4746 wxGrid
::wxGridSelectionModes selmode
)
4748 bool checkSelection
= false;
4751 // stop all processing
4756 m_table
->SetView(0);
4768 checkSelection
= true;
4770 // kill row and column size arrays
4771 m_colWidths
.Empty();
4772 m_colRights
.Empty();
4773 m_rowHeights
.Empty();
4774 m_rowBottoms
.Empty();
4779 m_numRows
= table
->GetNumberRows();
4780 m_numCols
= table
->GetNumberCols();
4782 if ( m_useNativeHeader
)
4783 GetColHeader()->SetColumnCount(m_numCols
);
4786 m_table
->SetView( this );
4787 m_ownTable
= takeOwnership
;
4788 m_selection
= new wxGridSelection( this, selmode
);
4791 // If the newly set table is smaller than the
4792 // original one current cell and selection regions
4793 // might be invalid,
4794 m_selectedBlockCorner
= wxGridNoCellCoords
;
4795 m_currentCellCoords
=
4796 wxGridCellCoords(wxMin(m_numRows
, m_currentCellCoords
.GetRow()),
4797 wxMin(m_numCols
, m_currentCellCoords
.GetCol()));
4798 if (m_selectedBlockTopLeft
.GetRow() >= m_numRows
||
4799 m_selectedBlockTopLeft
.GetCol() >= m_numCols
)
4801 m_selectedBlockTopLeft
= wxGridNoCellCoords
;
4802 m_selectedBlockBottomRight
= wxGridNoCellCoords
;
4805 m_selectedBlockBottomRight
=
4806 wxGridCellCoords(wxMin(m_numRows
,
4807 m_selectedBlockBottomRight
.GetRow()),
4809 m_selectedBlockBottomRight
.GetCol()));
4823 m_cornerLabelWin
= NULL
;
4824 m_rowLabelWin
= NULL
;
4832 m_defaultCellAttr
= NULL
;
4833 m_typeRegistry
= NULL
;
4834 m_winCapture
= NULL
;
4836 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
4837 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
4840 m_attrCache
.row
= -1;
4841 m_attrCache
.col
= -1;
4842 m_attrCache
.attr
= NULL
;
4844 m_labelFont
= GetFont();
4845 m_labelFont
.SetWeight( wxBOLD
);
4847 m_rowLabelHorizAlign
= wxALIGN_CENTRE
;
4848 m_rowLabelVertAlign
= wxALIGN_CENTRE
;
4850 m_colLabelHorizAlign
= wxALIGN_CENTRE
;
4851 m_colLabelVertAlign
= wxALIGN_CENTRE
;
4852 m_colLabelTextOrientation
= wxHORIZONTAL
;
4854 m_defaultColWidth
= WXGRID_DEFAULT_COL_WIDTH
;
4855 m_defaultRowHeight
= 0; // this will be initialized after creation
4857 m_minAcceptableColWidth
= WXGRID_MIN_COL_WIDTH
;
4858 m_minAcceptableRowHeight
= WXGRID_MIN_ROW_HEIGHT
;
4860 m_gridLineColour
= wxColour( 192,192,192 );
4861 m_gridLinesEnabled
= true;
4862 m_gridLinesClipHorz
=
4863 m_gridLinesClipVert
= true;
4864 m_cellHighlightColour
= *wxBLACK
;
4865 m_cellHighlightPenWidth
= 2;
4866 m_cellHighlightROPenWidth
= 1;
4868 m_canDragColMove
= false;
4870 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
4871 m_winCapture
= NULL
;
4872 m_canDragRowSize
= true;
4873 m_canDragColSize
= true;
4874 m_canDragGridSize
= true;
4875 m_canDragCell
= false;
4877 m_dragRowOrCol
= -1;
4878 m_isDragging
= false;
4879 m_startDragPos
= wxDefaultPosition
;
4882 m_nativeColumnLabels
= false;
4884 m_waitForSlowClick
= false;
4886 m_rowResizeCursor
= wxCursor( wxCURSOR_SIZENS
);
4887 m_colResizeCursor
= wxCursor( wxCURSOR_SIZEWE
);
4889 m_currentCellCoords
= wxGridNoCellCoords
;
4891 m_selectedBlockTopLeft
=
4892 m_selectedBlockBottomRight
=
4893 m_selectedBlockCorner
= wxGridNoCellCoords
;
4895 m_selectionBackground
= wxSystemSettings
::GetColour(wxSYS_COLOUR_HIGHLIGHT
);
4896 m_selectionForeground
= wxSystemSettings
::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
4898 m_editable
= true; // default for whole grid
4900 m_inOnKeyDown
= false;
4906 m_scrollLineX
= GRID_SCROLL_LINE_X
;
4907 m_scrollLineY
= GRID_SCROLL_LINE_Y
;
4910 // ----------------------------------------------------------------------------
4911 // the idea is to call these functions only when necessary because they create
4912 // quite big arrays which eat memory mostly unnecessary - in particular, if
4913 // default widths/heights are used for all rows/columns, we may not use these
4916 // with some extra code, it should be possible to only store the widths/heights
4917 // different from default ones (resulting in space savings for huge grids) but
4918 // this is not done currently
4919 // ----------------------------------------------------------------------------
4921 void wxGrid
::InitRowHeights()
4923 m_rowHeights
.Empty();
4924 m_rowBottoms
.Empty();
4926 m_rowHeights
.Alloc( m_numRows
);
4927 m_rowBottoms
.Alloc( m_numRows
);
4929 m_rowHeights
.Add( m_defaultRowHeight
, m_numRows
);
4932 for ( int i
= 0; i
< m_numRows
; i
++ )
4934 rowBottom
+= m_defaultRowHeight
;
4935 m_rowBottoms
.Add( rowBottom
);
4939 void wxGrid
::InitColWidths()
4941 m_colWidths
.Empty();
4942 m_colRights
.Empty();
4944 m_colWidths
.Alloc( m_numCols
);
4945 m_colRights
.Alloc( m_numCols
);
4947 m_colWidths
.Add( m_defaultColWidth
, m_numCols
);
4949 for ( int i
= 0; i
< m_numCols
; i
++ )
4951 int colRight
= ( GetColPos( i
) + 1 ) * m_defaultColWidth
;
4952 m_colRights
.Add( colRight
);
4956 int wxGrid
::GetColWidth(int col
) const
4958 return m_colWidths
.IsEmpty() ? m_defaultColWidth
: m_colWidths
[col
];
4961 int wxGrid
::GetColLeft(int col
) const
4963 return m_colRights
.IsEmpty() ?
GetColPos( col
) * m_defaultColWidth
4964 : m_colRights
[col
] - m_colWidths
[col
];
4967 int wxGrid
::GetColRight(int col
) const
4969 return m_colRights
.IsEmpty() ?
(GetColPos( col
) + 1) * m_defaultColWidth
4973 int wxGrid
::GetRowHeight(int row
) const
4975 return m_rowHeights
.IsEmpty() ? m_defaultRowHeight
: m_rowHeights
[row
];
4978 int wxGrid
::GetRowTop(int row
) const
4980 return m_rowBottoms
.IsEmpty() ? row
* m_defaultRowHeight
4981 : m_rowBottoms
[row
] - m_rowHeights
[row
];
4984 int wxGrid
::GetRowBottom(int row
) const
4986 return m_rowBottoms
.IsEmpty() ?
(row
+ 1) * m_defaultRowHeight
4987 : m_rowBottoms
[row
];
4990 void wxGrid
::CalcDimensions()
4992 // compute the size of the scrollable area
4993 int w
= m_numCols
> 0 ?
GetColRight(GetColAt(m_numCols
- 1)) : 0;
4994 int h
= m_numRows
> 0 ?
GetRowBottom(m_numRows
- 1) : 0;
4999 // take into account editor if shown
5000 if ( IsCellEditControlShown() )
5003 int r
= m_currentCellCoords
.GetRow();
5004 int c
= m_currentCellCoords
.GetCol();
5005 int x
= GetColLeft(c
);
5006 int y
= GetRowTop(r
);
5008 // how big is the editor
5009 wxGridCellAttr
* attr
= GetCellAttr(r
, c
);
5010 wxGridCellEditor
* editor
= attr
->GetEditor(this, r
, c
);
5011 editor
->GetControl()->GetSize(&w2
, &h2
);
5022 // preserve (more or less) the previous position
5024 GetViewStart( &x
, &y
);
5026 // ensure the position is valid for the new scroll ranges
5028 x
= wxMax( w
- 1, 0 );
5030 y
= wxMax( h
- 1, 0 );
5032 // update the virtual size and refresh the scrollbars to reflect it
5033 m_gridWin
->SetVirtualSize(w
, h
);
5037 // if our OnSize() hadn't been called (it would if we have scrollbars), we
5038 // still must reposition the children
5042 wxSize wxGrid
::GetSizeAvailableForScrollTarget(const wxSize
& size
)
5044 wxSize
sizeGridWin(size
);
5045 sizeGridWin
.x
-= m_rowLabelWidth
;
5046 sizeGridWin
.y
-= m_colLabelHeight
;
5051 void wxGrid
::CalcWindowSizes()
5053 // escape if the window is has not been fully created yet
5055 if ( m_cornerLabelWin
== NULL
)
5059 GetClientSize( &cw
, &ch
);
5061 // the grid may be too small to have enough space for the labels yet, don't
5062 // size the windows to negative sizes in this case
5063 int gw
= cw
- m_rowLabelWidth
;
5064 int gh
= ch
- m_colLabelHeight
;
5070 if ( m_cornerLabelWin
&& m_cornerLabelWin
->IsShown() )
5071 m_cornerLabelWin
->SetSize( 0, 0, m_rowLabelWidth
, m_colLabelHeight
);
5073 if ( m_colWindow
&& m_colWindow
->IsShown() )
5074 m_colWindow
->SetSize( m_rowLabelWidth
, 0, gw
, m_colLabelHeight
);
5076 if ( m_rowLabelWin
&& m_rowLabelWin
->IsShown() )
5077 m_rowLabelWin
->SetSize( 0, m_colLabelHeight
, m_rowLabelWidth
, gh
);
5079 if ( m_gridWin
&& m_gridWin
->IsShown() )
5080 m_gridWin
->SetSize( m_rowLabelWidth
, m_colLabelHeight
, gw
, gh
);
5083 // this is called when the grid table sends a message
5084 // to indicate that it has been redimensioned
5086 bool wxGrid
::Redimension( wxGridTableMessage
& msg
)
5089 bool result
= false;
5091 // Clear the attribute cache as the attribute might refer to a different
5092 // cell than stored in the cache after adding/removing rows/columns.
5095 // By the same reasoning, the editor should be dismissed if columns are
5096 // added or removed. And for consistency, it should IMHO always be
5097 // removed, not only if the cell "underneath" it actually changes.
5098 // For now, I intentionally do not save the editor's content as the
5099 // cell it might want to save that stuff to might no longer exist.
5100 HideCellEditControl();
5103 // if we were using the default widths/heights so far, we must change them
5105 if ( m_colWidths
.IsEmpty() )
5110 if ( m_rowHeights
.IsEmpty() )
5116 switch ( msg
.GetId() )
5118 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
5120 size_t pos
= msg
.GetCommandInt();
5121 int numRows
= msg
.GetCommandInt2();
5123 m_numRows
+= numRows
;
5125 if ( !m_rowHeights
.IsEmpty() )
5127 m_rowHeights
.Insert( m_defaultRowHeight
, pos
, numRows
);
5128 m_rowBottoms
.Insert( 0, pos
, numRows
);
5132 bottom
= m_rowBottoms
[pos
- 1];
5134 for ( i
= pos
; i
< m_numRows
; i
++ )
5136 bottom
+= m_rowHeights
[i
];
5137 m_rowBottoms
[i
] = bottom
;
5141 if ( m_currentCellCoords
== wxGridNoCellCoords
)
5143 // if we have just inserted cols into an empty grid the current
5144 // cell will be undefined...
5146 SetCurrentCell( 0, 0 );
5150 m_selection
->UpdateRows( pos
, numRows
);
5151 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
5153 attrProvider
->UpdateAttrRows( pos
, numRows
);
5155 if ( !GetBatchCount() )
5158 m_rowLabelWin
->Refresh();
5164 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
5166 int numRows
= msg
.GetCommandInt();
5167 int oldNumRows
= m_numRows
;
5168 m_numRows
+= numRows
;
5170 if ( !m_rowHeights
.IsEmpty() )
5172 m_rowHeights
.Add( m_defaultRowHeight
, numRows
);
5173 m_rowBottoms
.Add( 0, numRows
);
5176 if ( oldNumRows
> 0 )
5177 bottom
= m_rowBottoms
[oldNumRows
- 1];
5179 for ( i
= oldNumRows
; i
< m_numRows
; i
++ )
5181 bottom
+= m_rowHeights
[i
];
5182 m_rowBottoms
[i
] = bottom
;
5186 if ( m_currentCellCoords
== wxGridNoCellCoords
)
5188 // if we have just inserted cols into an empty grid the current
5189 // cell will be undefined...
5191 SetCurrentCell( 0, 0 );
5194 if ( !GetBatchCount() )
5197 m_rowLabelWin
->Refresh();
5203 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
5205 size_t pos
= msg
.GetCommandInt();
5206 int numRows
= msg
.GetCommandInt2();
5207 m_numRows
-= numRows
;
5209 if ( !m_rowHeights
.IsEmpty() )
5211 m_rowHeights
.RemoveAt( pos
, numRows
);
5212 m_rowBottoms
.RemoveAt( pos
, numRows
);
5215 for ( i
= 0; i
< m_numRows
; i
++ )
5217 h
+= m_rowHeights
[i
];
5218 m_rowBottoms
[i
] = h
;
5224 m_currentCellCoords
= wxGridNoCellCoords
;
5228 if ( m_currentCellCoords
.GetRow() >= m_numRows
)
5229 m_currentCellCoords
.Set( 0, 0 );
5233 m_selection
->UpdateRows( pos
, -((int)numRows
) );
5234 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
5237 attrProvider
->UpdateAttrRows( pos
, -((int)numRows
) );
5239 // ifdef'd out following patch from Paul Gammans
5241 // No need to touch column attributes, unless we
5242 // removed _all_ rows, in this case, we remove
5243 // all column attributes.
5244 // I hate to do this here, but the
5245 // needed data is not available inside UpdateAttrRows.
5246 if ( !GetNumberRows() )
5247 attrProvider
->UpdateAttrCols( 0, -GetNumberCols() );
5251 if ( !GetBatchCount() )
5254 m_rowLabelWin
->Refresh();
5260 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
5262 size_t pos
= msg
.GetCommandInt();
5263 int numCols
= msg
.GetCommandInt2();
5264 m_numCols
+= numCols
;
5266 if ( m_useNativeHeader
)
5267 GetColHeader()->SetColumnCount(m_numCols
);
5269 if ( !m_colAt
.IsEmpty() )
5271 //Shift the column IDs
5273 for ( i
= 0; i
< m_numCols
- numCols
; i
++ )
5275 if ( m_colAt
[i
] >= (int)pos
)
5276 m_colAt
[i
] += numCols
;
5279 m_colAt
.Insert( pos
, pos
, numCols
);
5281 //Set the new columns' positions
5282 for ( i
= pos
+ 1; i
< (int)pos
+ numCols
; i
++ )
5288 if ( !m_colWidths
.IsEmpty() )
5290 m_colWidths
.Insert( m_defaultColWidth
, pos
, numCols
);
5291 m_colRights
.Insert( 0, pos
, numCols
);
5295 right
= m_colRights
[GetColAt( pos
- 1 )];
5298 for ( colPos
= pos
; colPos
< m_numCols
; colPos
++ )
5300 i
= GetColAt( colPos
);
5302 right
+= m_colWidths
[i
];
5303 m_colRights
[i
] = right
;
5307 if ( m_currentCellCoords
== wxGridNoCellCoords
)
5309 // if we have just inserted cols into an empty grid the current
5310 // cell will be undefined...
5312 SetCurrentCell( 0, 0 );
5316 m_selection
->UpdateCols( pos
, numCols
);
5317 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
5319 attrProvider
->UpdateAttrCols( pos
, numCols
);
5320 if ( !GetBatchCount() )
5323 m_colWindow
->Refresh();
5329 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
5331 int numCols
= msg
.GetCommandInt();
5332 int oldNumCols
= m_numCols
;
5333 m_numCols
+= numCols
;
5334 if ( m_useNativeHeader
)
5335 GetColHeader()->SetColumnCount(m_numCols
);
5337 if ( !m_colAt
.IsEmpty() )
5339 m_colAt
.Add( 0, numCols
);
5341 //Set the new columns' positions
5343 for ( i
= oldNumCols
; i
< m_numCols
; i
++ )
5349 if ( !m_colWidths
.IsEmpty() )
5351 m_colWidths
.Add( m_defaultColWidth
, numCols
);
5352 m_colRights
.Add( 0, numCols
);
5355 if ( oldNumCols
> 0 )
5356 right
= m_colRights
[GetColAt( oldNumCols
- 1 )];
5359 for ( colPos
= oldNumCols
; colPos
< m_numCols
; colPos
++ )
5361 i
= GetColAt( colPos
);
5363 right
+= m_colWidths
[i
];
5364 m_colRights
[i
] = right
;
5368 if ( m_currentCellCoords
== wxGridNoCellCoords
)
5370 // if we have just inserted cols into an empty grid the current
5371 // cell will be undefined...
5373 SetCurrentCell( 0, 0 );
5375 if ( !GetBatchCount() )
5378 m_colWindow
->Refresh();
5384 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
5386 size_t pos
= msg
.GetCommandInt();
5387 int numCols
= msg
.GetCommandInt2();
5388 m_numCols
-= numCols
;
5389 if ( m_useNativeHeader
)
5390 GetColHeader()->SetColumnCount(m_numCols
);
5392 if ( !m_colAt
.IsEmpty() )
5394 int colID
= GetColAt( pos
);
5396 m_colAt
.RemoveAt( pos
, numCols
);
5398 //Shift the column IDs
5400 for ( colPos
= 0; colPos
< m_numCols
; colPos
++ )
5402 if ( m_colAt
[colPos
] > colID
)
5403 m_colAt
[colPos
] -= numCols
;
5407 if ( !m_colWidths
.IsEmpty() )
5409 m_colWidths
.RemoveAt( pos
, numCols
);
5410 m_colRights
.RemoveAt( pos
, numCols
);
5414 for ( colPos
= 0; colPos
< m_numCols
; colPos
++ )
5416 i
= GetColAt( colPos
);
5418 w
+= m_colWidths
[i
];
5425 m_currentCellCoords
= wxGridNoCellCoords
;
5429 if ( m_currentCellCoords
.GetCol() >= m_numCols
)
5430 m_currentCellCoords
.Set( 0, 0 );
5434 m_selection
->UpdateCols( pos
, -((int)numCols
) );
5435 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
5438 attrProvider
->UpdateAttrCols( pos
, -((int)numCols
) );
5440 // ifdef'd out following patch from Paul Gammans
5442 // No need to touch row attributes, unless we
5443 // removed _all_ columns, in this case, we remove
5444 // all row attributes.
5445 // I hate to do this here, but the
5446 // needed data is not available inside UpdateAttrCols.
5447 if ( !GetNumberCols() )
5448 attrProvider
->UpdateAttrRows( 0, -GetNumberRows() );
5452 if ( !GetBatchCount() )
5455 m_colWindow
->Refresh();
5462 if (result
&& !GetBatchCount() )
5463 m_gridWin
->Refresh();
5468 wxArrayInt wxGrid
::CalcRowLabelsExposed( const wxRegion
& reg
) const
5470 wxRegionIterator
iter( reg
);
5473 wxArrayInt rowlabels
;
5480 // TODO: remove this when we can...
5481 // There is a bug in wxMotif that gives garbage update
5482 // rectangles if you jump-scroll a long way by clicking the
5483 // scrollbar with middle button. This is a work-around
5485 #if defined(__WXMOTIF__)
5487 m_gridWin
->GetClientSize( &cw
, &ch
);
5488 if ( r
.GetTop() > ch
)
5490 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
5493 // logical bounds of update region
5496 CalcUnscrolledPosition( 0, r
.GetTop(), &dummy
, &top
);
5497 CalcUnscrolledPosition( 0, r
.GetBottom(), &dummy
, &bottom
);
5499 // find the row labels within these bounds
5502 for ( row
= internalYToRow(top
); row
< m_numRows
; row
++ )
5504 if ( GetRowBottom(row
) < top
)
5507 if ( GetRowTop(row
) > bottom
)
5510 rowlabels
.Add( row
);
5519 wxArrayInt wxGrid
::CalcColLabelsExposed( const wxRegion
& reg
) const
5521 wxRegionIterator
iter( reg
);
5524 wxArrayInt colLabels
;
5531 // TODO: remove this when we can...
5532 // There is a bug in wxMotif that gives garbage update
5533 // rectangles if you jump-scroll a long way by clicking the
5534 // scrollbar with middle button. This is a work-around
5536 #if defined(__WXMOTIF__)
5538 m_gridWin
->GetClientSize( &cw
, &ch
);
5539 if ( r
.GetLeft() > cw
)
5541 r
.SetRight( wxMin( r
.GetRight(), cw
) );
5544 // logical bounds of update region
5547 CalcUnscrolledPosition( r
.GetLeft(), 0, &left
, &dummy
);
5548 CalcUnscrolledPosition( r
.GetRight(), 0, &right
, &dummy
);
5550 // find the cells within these bounds
5554 for ( colPos
= GetColPos( internalXToCol(left
) ); colPos
< m_numCols
; colPos
++ )
5556 col
= GetColAt( colPos
);
5558 if ( GetColRight(col
) < left
)
5561 if ( GetColLeft(col
) > right
)
5564 colLabels
.Add( col
);
5573 wxGridCellCoordsArray wxGrid
::CalcCellsExposed( const wxRegion
& reg
) const
5575 wxRegionIterator
iter( reg
);
5578 wxGridCellCoordsArray cellsExposed
;
5580 int left
, top
, right
, bottom
;
5585 // TODO: remove this when we can...
5586 // There is a bug in wxMotif that gives garbage update
5587 // rectangles if you jump-scroll a long way by clicking the
5588 // scrollbar with middle button. This is a work-around
5590 #if defined(__WXMOTIF__)
5592 m_gridWin
->GetClientSize( &cw
, &ch
);
5593 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
5594 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
5595 r
.SetRight( wxMin( r
.GetRight(), cw
) );
5596 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
5599 // logical bounds of update region
5601 CalcUnscrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
5602 CalcUnscrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
5604 // find the cells within these bounds
5607 for ( row
= internalYToRow(top
); row
< m_numRows
; row
++ )
5609 if ( GetRowBottom(row
) <= top
)
5612 if ( GetRowTop(row
) > bottom
)
5616 for ( colPos
= GetColPos( internalXToCol(left
) ); colPos
< m_numCols
; colPos
++ )
5618 col
= GetColAt( colPos
);
5620 if ( GetColRight(col
) <= left
)
5623 if ( GetColLeft(col
) > right
)
5626 cellsExposed
.Add( wxGridCellCoords( row
, col
) );
5633 return cellsExposed
;
5637 void wxGrid
::ProcessRowLabelMouseEvent( wxMouseEvent
& event
)
5640 wxPoint
pos( event
.GetPosition() );
5641 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
5643 if ( event
.Dragging() )
5647 m_isDragging
= true;
5648 m_rowLabelWin
->CaptureMouse();
5651 if ( event
.LeftIsDown() )
5653 switch ( m_cursorMode
)
5655 case WXGRID_CURSOR_RESIZE_ROW
:
5657 int cw
, ch
, left
, dummy
;
5658 m_gridWin
->GetClientSize( &cw
, &ch
);
5659 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
5661 wxClientDC
dc( m_gridWin
);
5664 GetRowTop(m_dragRowOrCol
) +
5665 GetRowMinimalHeight(m_dragRowOrCol
) );
5666 dc
.SetLogicalFunction(wxINVERT
);
5667 if ( m_dragLastPos
>= 0 )
5669 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
5671 dc
.DrawLine( left
, y
, left
+cw
, y
);
5676 case WXGRID_CURSOR_SELECT_ROW
:
5678 if ( (row
= YToRow( y
)) >= 0 )
5681 m_selection
->SelectRow(row
, event
);
5686 // default label to suppress warnings about "enumeration value
5687 // 'xxx' not handled in switch
5695 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
5700 if (m_rowLabelWin
->HasCapture())
5701 m_rowLabelWin
->ReleaseMouse();
5702 m_isDragging
= false;
5705 // ------------ Entering or leaving the window
5707 if ( event
.Entering() || event
.Leaving() )
5709 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
5712 // ------------ Left button pressed
5714 else if ( event
.LeftDown() )
5716 // don't send a label click event for a hit on the
5717 // edge of the row label - this is probably the user
5718 // wanting to resize the row
5720 if ( YToEdgeOfRow(y
) < 0 )
5724 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, row
, -1, event
) )
5726 if ( !event
.ShiftDown() && !event
.CmdDown() )
5730 if ( event
.ShiftDown() )
5732 m_selection
->SelectBlock
5734 m_currentCellCoords
.GetRow(), 0,
5735 row
, GetNumberCols() - 1,
5741 m_selection
->SelectRow(row
, event
);
5745 ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW
, m_rowLabelWin
);
5750 // starting to drag-resize a row
5751 if ( CanDragRowSize() )
5752 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
);
5756 // ------------ Left double click
5758 else if (event
.LeftDClick() )
5760 row
= YToEdgeOfRow(y
);
5765 !SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, row
, -1, event
) )
5767 // no default action at the moment
5772 // adjust row height depending on label text
5773 AutoSizeRowLabelSize( row
);
5775 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, GetColLabelWindow());
5780 // ------------ Left button released
5782 else if ( event
.LeftUp() )
5784 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
5786 DoEndDragResizeRow();
5788 // Note: we are ending the event *after* doing
5789 // default processing in this case
5791 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
5794 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
5798 // ------------ Right button down
5800 else if ( event
.RightDown() )
5804 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, row
, -1, event
) )
5806 // no default action at the moment
5810 // ------------ Right double click
5812 else if ( event
.RightDClick() )
5816 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, row
, -1, event
) )
5818 // no default action at the moment
5822 // ------------ No buttons down and mouse moving
5824 else if ( event
.Moving() )
5826 m_dragRowOrCol
= YToEdgeOfRow( y
);
5827 if ( m_dragRowOrCol
>= 0 )
5829 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5831 // don't capture the mouse yet
5832 if ( CanDragRowSize() )
5833 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
, false);
5836 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5838 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
, false);
5843 void wxGrid
::DoStartResizeCol(int col
)
5845 m_dragRowOrCol
= col
;
5847 DoUpdateResizeColWidth(GetColWidth(m_dragRowOrCol
));
5850 void wxGrid
::DoUpdateResizeCol(int x
)
5852 int cw
, ch
, dummy
, top
;
5853 m_gridWin
->GetClientSize( &cw
, &ch
);
5854 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
5856 wxClientDC
dc( m_gridWin
);
5859 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) + GetColMinimalWidth(m_dragRowOrCol
));
5860 dc
.SetLogicalFunction(wxINVERT
);
5861 if ( m_dragLastPos
>= 0 )
5863 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ ch
);
5865 dc
.DrawLine( x
, top
, x
, top
+ ch
);
5869 void wxGrid
::DoUpdateResizeColWidth(int w
)
5871 DoUpdateResizeCol(GetColLeft(m_dragRowOrCol
) + w
);
5874 void wxGrid
::ProcessColLabelMouseEvent( wxMouseEvent
& event
)
5877 wxPoint
pos( event
.GetPosition() );
5878 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
5880 if ( event
.Dragging() )
5884 m_isDragging
= true;
5885 GetColLabelWindow()->CaptureMouse();
5887 if ( m_cursorMode
== WXGRID_CURSOR_MOVE_COL
)
5888 m_dragRowOrCol
= XToCol( x
);
5891 if ( event
.LeftIsDown() )
5893 switch ( m_cursorMode
)
5895 case WXGRID_CURSOR_RESIZE_COL
:
5896 DoUpdateResizeCol(x
);
5899 case WXGRID_CURSOR_SELECT_COL
:
5901 if ( (col
= XToCol( x
)) >= 0 )
5904 m_selection
->SelectCol(col
, event
);
5909 case WXGRID_CURSOR_MOVE_COL
:
5912 m_moveToCol
= GetColAt( 0 );
5914 m_moveToCol
= XToCol( x
);
5918 if ( m_moveToCol
< 0 )
5919 markerX
= GetColRight( GetColAt( m_numCols
- 1 ) );
5920 else if ( x
>= (GetColLeft( m_moveToCol
) + (GetColWidth(m_moveToCol
) / 2)) )
5922 m_moveToCol
= GetColAt( GetColPos( m_moveToCol
) + 1 );
5923 if ( m_moveToCol
< 0 )
5924 markerX
= GetColRight( GetColAt( m_numCols
- 1 ) );
5926 markerX
= GetColLeft( m_moveToCol
);
5929 markerX
= GetColLeft( m_moveToCol
);
5931 if ( markerX
!= m_dragLastPos
)
5933 wxClientDC
dc( GetColLabelWindow() );
5937 GetColLabelWindow()->GetClientSize( &cw
, &ch
);
5941 //Clean up the last indicator
5942 if ( m_dragLastPos
>= 0 )
5944 wxPen
pen( GetColLabelWindow()->GetBackgroundColour(), 2 );
5946 dc
.DrawLine( m_dragLastPos
+ 1, 0, m_dragLastPos
+ 1, ch
);
5947 dc
.SetPen(wxNullPen
);
5949 if ( XToCol( m_dragLastPos
) != -1 )
5950 DrawColLabel( dc
, XToCol( m_dragLastPos
) );
5953 const wxColour
*color
;
5954 //Moving to the same place? Don't draw a marker
5955 if ( (m_moveToCol
== m_dragRowOrCol
)
5956 || (GetColPos( m_moveToCol
) == GetColPos( m_dragRowOrCol
) + 1)
5957 || (m_moveToCol
< 0 && m_dragRowOrCol
== GetColAt( m_numCols
- 1 )))
5958 color
= wxLIGHT_GREY
;
5963 wxPen
pen( *color
, 2 );
5966 dc
.DrawLine( markerX
, 0, markerX
, ch
);
5968 dc
.SetPen(wxNullPen
);
5970 m_dragLastPos
= markerX
- 1;
5975 // default label to suppress warnings about "enumeration value
5976 // 'xxx' not handled in switch
5984 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
5989 if (GetColLabelWindow()->HasCapture())
5990 GetColLabelWindow()->ReleaseMouse();
5991 m_isDragging
= false;
5994 // ------------ Entering or leaving the window
5996 if ( event
.Entering() || event
.Leaving() )
5998 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, GetColLabelWindow());
6001 // ------------ Left button pressed
6003 else if ( event
.LeftDown() )
6005 // don't send a label click event for a hit on the
6006 // edge of the col label - this is probably the user
6007 // wanting to resize the col
6009 if ( XToEdgeOfCol(x
) < 0 )
6013 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, col
, event
) )
6015 if ( m_canDragColMove
)
6017 //Show button as pressed
6018 wxClientDC
dc( GetColLabelWindow() );
6019 int colLeft
= GetColLeft( col
);
6020 int colRight
= GetColRight( col
) - 1;
6021 dc
.SetPen( wxPen( GetColLabelWindow()->GetBackgroundColour(), 1 ) );
6022 dc
.DrawLine( colLeft
, 1, colLeft
, m_colLabelHeight
-1 );
6023 dc
.DrawLine( colLeft
, 1, colRight
, 1 );
6025 ChangeCursorMode(WXGRID_CURSOR_MOVE_COL
, GetColLabelWindow());
6029 if ( !event
.ShiftDown() && !event
.CmdDown() )
6033 if ( event
.ShiftDown() )
6035 m_selection
->SelectBlock
6037 0, m_currentCellCoords
.GetCol(),
6038 GetNumberRows() - 1, col
,
6044 m_selection
->SelectCol(col
, event
);
6048 ChangeCursorMode(WXGRID_CURSOR_SELECT_COL
, GetColLabelWindow());
6054 // starting to drag-resize a col
6056 if ( CanDragColSize() )
6057 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, GetColLabelWindow());
6061 // ------------ Left double click
6063 if ( event
.LeftDClick() )
6065 col
= XToEdgeOfCol(x
);
6070 ! SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, col
, event
) )
6072 // no default action at the moment
6077 // adjust column width depending on label text
6078 AutoSizeColLabelSize( col
);
6080 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, GetColLabelWindow());
6085 // ------------ Left button released
6087 else if ( event
.LeftUp() )
6089 switch ( m_cursorMode
)
6091 case WXGRID_CURSOR_RESIZE_COL
:
6092 DoEndDragResizeCol();
6095 case WXGRID_CURSOR_MOVE_COL
:
6098 SendEvent( wxEVT_GRID_COL_MOVE
, -1, m_dragRowOrCol
, event
);
6101 case WXGRID_CURSOR_SELECT_COL
:
6102 case WXGRID_CURSOR_SELECT_CELL
:
6103 case WXGRID_CURSOR_RESIZE_ROW
:
6104 case WXGRID_CURSOR_SELECT_ROW
:
6105 // nothing to do (?)
6109 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, GetColLabelWindow());
6113 // ------------ Right button down
6115 else if ( event
.RightDown() )
6119 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, col
, event
) )
6121 // no default action at the moment
6125 // ------------ Right double click
6127 else if ( event
.RightDClick() )
6131 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, col
, event
) )
6133 // no default action at the moment
6137 // ------------ No buttons down and mouse moving
6139 else if ( event
.Moving() )
6141 m_dragRowOrCol
= XToEdgeOfCol( x
);
6142 if ( m_dragRowOrCol
>= 0 )
6144 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
6146 // don't capture the cursor yet
6147 if ( CanDragColSize() )
6148 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, GetColLabelWindow(), false);
6151 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
6153 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, GetColLabelWindow(), false);
6158 void wxGrid
::ProcessCornerLabelMouseEvent( wxMouseEvent
& event
)
6160 if ( event
.LeftDown() )
6162 // indicate corner label by having both row and
6165 if ( !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, -1, event
) )
6170 else if ( event
.LeftDClick() )
6172 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, -1, event
);
6174 else if ( event
.RightDown() )
6176 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, -1, event
) )
6178 // no default action at the moment
6181 else if ( event
.RightDClick() )
6183 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, -1, event
) )
6185 // no default action at the moment
6190 void wxGrid
::CancelMouseCapture()
6192 // cancel operation currently in progress, whatever it is
6195 m_isDragging
= false;
6196 m_startDragPos
= wxDefaultPosition
;
6198 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
6199 m_winCapture
->SetCursor( *wxSTANDARD_CURSOR
);
6200 m_winCapture
= NULL
;
6202 // remove traces of whatever we drew on screen
6207 void wxGrid
::ChangeCursorMode(CursorMode mode
,
6212 static const wxChar
*cursorModes
[] =
6222 wxLogTrace(_T("grid"),
6223 _T("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
6224 win
== m_colWindow ?
_T("colLabelWin")
6225 : win ?
_T("rowLabelWin")
6227 cursorModes
[m_cursorMode
], cursorModes
[mode
]);
6230 if ( mode
== m_cursorMode
&&
6231 win
== m_winCapture
&&
6232 captureMouse
== (m_winCapture
!= NULL
))
6237 // by default use the grid itself
6243 m_winCapture
->ReleaseMouse();
6244 m_winCapture
= NULL
;
6247 m_cursorMode
= mode
;
6249 switch ( m_cursorMode
)
6251 case WXGRID_CURSOR_RESIZE_ROW
:
6252 win
->SetCursor( m_rowResizeCursor
);
6255 case WXGRID_CURSOR_RESIZE_COL
:
6256 win
->SetCursor( m_colResizeCursor
);
6259 case WXGRID_CURSOR_MOVE_COL
:
6260 win
->SetCursor( wxCursor(wxCURSOR_HAND
) );
6264 win
->SetCursor( *wxSTANDARD_CURSOR
);
6268 // we need to capture mouse when resizing
6269 bool resize
= m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
||
6270 m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
;
6272 if ( captureMouse
&& resize
)
6274 win
->CaptureMouse();
6279 // ----------------------------------------------------------------------------
6280 // grid mouse event processing
6281 // ----------------------------------------------------------------------------
6284 wxGrid
::DoGridCellDrag(wxMouseEvent
& event
,
6285 const wxGridCellCoords
& coords
,
6288 if ( coords
== wxGridNoCellCoords
)
6289 return; // we're outside any valid cell
6291 // Hide the edit control, so it won't interfere with drag-shrinking.
6292 if ( IsCellEditControlShown() )
6294 HideCellEditControl();
6295 SaveEditControlValue();
6298 switch ( event
.GetModifiers() )
6301 if ( m_selectedBlockCorner
== wxGridNoCellCoords
)
6302 m_selectedBlockCorner
= coords
;
6303 UpdateBlockBeingSelected(m_selectedBlockCorner
, coords
);
6307 if ( CanDragCell() )
6311 if ( m_selectedBlockCorner
== wxGridNoCellCoords
)
6312 m_selectedBlockCorner
= coords
;
6314 SendEvent(wxEVT_GRID_CELL_BEGIN_DRAG
, coords
, event
);
6319 UpdateBlockBeingSelected(m_currentCellCoords
, coords
);
6323 // we don't handle the other key modifiers
6328 void wxGrid
::DoGridLineDrag(wxMouseEvent
& event
, const wxGridOperations
& oper
)
6330 wxClientDC
dc(m_gridWin
);
6332 dc
.SetLogicalFunction(wxINVERT
);
6334 const wxRect
rectWin(CalcUnscrolledPosition(wxPoint(0, 0)),
6335 m_gridWin
->GetClientSize());
6337 // erase the previously drawn line, if any
6338 if ( m_dragLastPos
>= 0 )
6339 oper
.DrawParallelLineInRect(dc
, rectWin
, m_dragLastPos
);
6341 // we need the vertical position for rows and horizontal for columns here
6342 m_dragLastPos
= oper
.Dual().Select(CalcUnscrolledPosition(event
.GetPosition()));
6344 // don't allow resizing beneath the minimal size
6345 const int posMin
= oper
.GetLineStartPos(this, m_dragRowOrCol
) +
6346 oper
.GetMinimalLineSize(this, m_dragRowOrCol
);
6347 if ( m_dragLastPos
< posMin
)
6348 m_dragLastPos
= posMin
;
6350 // and draw it at the new position
6351 oper
.DrawParallelLineInRect(dc
, rectWin
, m_dragLastPos
);
6354 void wxGrid
::DoGridDragEvent(wxMouseEvent
& event
, const wxGridCellCoords
& coords
)
6356 if ( !m_isDragging
)
6358 // Don't start doing anything until the mouse has been dragged far
6360 const wxPoint
& pt
= event
.GetPosition();
6361 if ( m_startDragPos
== wxDefaultPosition
)
6363 m_startDragPos
= pt
;
6367 if ( abs(m_startDragPos
.x
- pt
.x
) <= DRAG_SENSITIVITY
&&
6368 abs(m_startDragPos
.y
- pt
.y
) <= DRAG_SENSITIVITY
)
6372 const bool isFirstDrag
= !m_isDragging
;
6373 m_isDragging
= true;
6375 switch ( m_cursorMode
)
6377 case WXGRID_CURSOR_SELECT_CELL
:
6378 DoGridCellDrag(event
, coords
, isFirstDrag
);
6381 case WXGRID_CURSOR_RESIZE_ROW
:
6382 DoGridLineDrag(event
, wxGridRowOperations());
6385 case WXGRID_CURSOR_RESIZE_COL
:
6386 DoGridLineDrag(event
, wxGridColumnOperations());
6395 m_winCapture
= m_gridWin
;
6396 m_winCapture
->CaptureMouse();
6401 wxGrid
::DoGridCellLeftDown(wxMouseEvent
& event
,
6402 const wxGridCellCoords
& coords
,
6405 if ( SendEvent(wxEVT_GRID_CELL_LEFT_CLICK
, coords
, event
) )
6407 // event handled by user code, no need to do anything here
6411 if ( !event
.CmdDown() )
6414 if ( event
.ShiftDown() )
6418 m_selection
->SelectBlock(m_currentCellCoords
, coords
, event
);
6419 m_selectedBlockCorner
= coords
;
6422 else if ( XToEdgeOfCol(pos
.x
) < 0 && YToEdgeOfRow(pos
.y
) < 0 )
6424 DisableCellEditControl();
6425 MakeCellVisible( coords
);
6427 if ( event
.CmdDown() )
6431 m_selection
->ToggleCellSelection(coords
, event
);
6434 m_selectedBlockTopLeft
= wxGridNoCellCoords
;
6435 m_selectedBlockBottomRight
= wxGridNoCellCoords
;
6436 m_selectedBlockCorner
= coords
;
6440 m_waitForSlowClick
= m_currentCellCoords
== coords
&&
6441 coords
!= wxGridNoCellCoords
;
6442 SetCurrentCell( coords
);
6448 wxGrid
::DoGridCellLeftDClick(wxMouseEvent
& event
,
6449 const wxGridCellCoords
& coords
,
6452 if ( XToEdgeOfCol(pos
.x
) < 0 && YToEdgeOfRow(pos
.y
) < 0 )
6454 if ( !SendEvent(wxEVT_GRID_CELL_LEFT_DCLICK
, coords
, event
) )
6456 // we want double click to select a cell and start editing
6457 // (i.e. to behave in same way as sequence of two slow clicks):
6458 m_waitForSlowClick
= true;
6464 wxGrid
::DoGridCellLeftUp(wxMouseEvent
& event
, const wxGridCellCoords
& coords
)
6466 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
6470 m_winCapture
->ReleaseMouse();
6471 m_winCapture
= NULL
;
6474 if ( coords
== m_currentCellCoords
&& m_waitForSlowClick
&& CanEnableCellControl() )
6477 EnableCellEditControl();
6479 wxGridCellAttr
*attr
= GetCellAttr(coords
);
6480 wxGridCellEditor
*editor
= attr
->GetEditor(this, coords
.GetRow(), coords
.GetCol());
6481 editor
->StartingClick();
6485 m_waitForSlowClick
= false;
6487 else if ( m_selectedBlockTopLeft
!= wxGridNoCellCoords
&&
6488 m_selectedBlockBottomRight
!= wxGridNoCellCoords
)
6492 m_selection
->SelectBlock( m_selectedBlockTopLeft
,
6493 m_selectedBlockBottomRight
,
6497 m_selectedBlockTopLeft
= wxGridNoCellCoords
;
6498 m_selectedBlockBottomRight
= wxGridNoCellCoords
;
6500 // Show the edit control, if it has been hidden for
6502 ShowCellEditControl();
6505 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
6507 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6508 DoEndDragResizeRow();
6510 // Note: we are ending the event *after* doing
6511 // default processing in this case
6513 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
6515 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
6517 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6518 DoEndDragResizeCol();
6525 wxGrid
::DoGridMouseMoveEvent(wxMouseEvent
& WXUNUSED(event
),
6526 const wxGridCellCoords
& coords
,
6529 if ( coords
.GetRow() < 0 || coords
.GetCol() < 0 )
6531 // out of grid cell area
6532 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6536 int dragRow
= YToEdgeOfRow( pos
.y
);
6537 int dragCol
= XToEdgeOfCol( pos
.x
);
6539 // Dragging on the corner of a cell to resize in both
6540 // directions is not implemented yet...
6542 if ( dragRow
>= 0 && dragCol
>= 0 )
6544 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6550 m_dragRowOrCol
= dragRow
;
6552 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
6554 if ( CanDragRowSize() && CanDragGridSize() )
6555 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, NULL
, false);
6558 // When using the native header window we can only resize the columns by
6559 // dragging the dividers in it because we can't make it enter into the
6560 // column resizing mode programmatically
6561 else if ( dragCol
>= 0 && !m_useNativeHeader
)
6563 m_dragRowOrCol
= dragCol
;
6565 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
6567 if ( CanDragColSize() && CanDragGridSize() )
6568 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, NULL
, false);
6571 else // Neither on a row or col edge
6573 if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
6575 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6580 void wxGrid
::ProcessGridCellMouseEvent(wxMouseEvent
& event
)
6582 const wxPoint pos
= CalcUnscrolledPosition(event
.GetPosition());
6584 // coordinates of the cell under mouse
6585 wxGridCellCoords coords
= XYToCell(pos
);
6587 int cell_rows
, cell_cols
;
6588 GetCellSize( coords
.GetRow(), coords
.GetCol(), &cell_rows
, &cell_cols
);
6589 if ( (cell_rows
< 0) || (cell_cols
< 0) )
6591 coords
.SetRow(coords
.GetRow() + cell_rows
);
6592 coords
.SetCol(coords
.GetCol() + cell_cols
);
6595 if ( event
.Dragging() )
6597 if ( event
.LeftIsDown() )
6598 DoGridDragEvent(event
, coords
);
6604 m_isDragging
= false;
6605 m_startDragPos
= wxDefaultPosition
;
6607 // VZ: if we do this, the mode is reset to WXGRID_CURSOR_SELECT_CELL
6608 // immediately after it becomes WXGRID_CURSOR_RESIZE_ROW/COL under
6611 if ( event
.Entering() || event
.Leaving() )
6613 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6614 m_gridWin
->SetCursor( *wxSTANDARD_CURSOR
);
6618 // deal with various button presses
6619 if ( event
.IsButton() )
6621 if ( coords
!= wxGridNoCellCoords
)
6623 DisableCellEditControl();
6625 if ( event
.LeftDown() )
6626 DoGridCellLeftDown(event
, coords
, pos
);
6627 else if ( event
.LeftDClick() )
6628 DoGridCellLeftDClick(event
, coords
, pos
);
6629 else if ( event
.RightDown() )
6630 SendEvent(wxEVT_GRID_CELL_RIGHT_CLICK
, coords
, event
);
6631 else if ( event
.RightDClick() )
6632 SendEvent(wxEVT_GRID_CELL_RIGHT_DCLICK
, coords
, event
);
6635 // this one should be called even if we're not over any cell
6636 if ( event
.LeftUp() )
6638 DoGridCellLeftUp(event
, coords
);
6641 else if ( event
.Moving() )
6643 DoGridMouseMoveEvent(event
, coords
, pos
);
6645 else // unknown mouse event?
6651 void wxGrid
::DoEndDragResizeLine(const wxGridOperations
& oper
)
6653 if ( m_dragLastPos
== -1 )
6656 const wxGridOperations
& doper
= oper
.Dual();
6658 const wxSize size
= m_gridWin
->GetClientSize();
6660 const wxPoint ptOrigin
= CalcUnscrolledPosition(wxPoint(0, 0));
6662 // erase the last line we drew
6663 wxClientDC
dc(m_gridWin
);
6665 dc
.SetLogicalFunction(wxINVERT
);
6667 const int posLineStart
= oper
.Select(ptOrigin
);
6668 const int posLineEnd
= oper
.Select(ptOrigin
) + oper
.Select(size
);
6670 oper
.DrawParallelLine(dc
, posLineStart
, posLineEnd
, m_dragLastPos
);
6672 // temporarily hide the edit control before resizing
6673 HideCellEditControl();
6674 SaveEditControlValue();
6676 // do resize the line
6677 const int lineStart
= oper
.GetLineStartPos(this, m_dragRowOrCol
);
6678 oper
.SetLineSize(this, m_dragRowOrCol
,
6679 wxMax(m_dragLastPos
- lineStart
,
6680 oper
.GetMinimalLineSize(this, m_dragRowOrCol
)));
6684 // refresh now if we're not frozen
6685 if ( !GetBatchCount() )
6687 // we need to refresh everything beyond the resized line in the header
6690 // get the position from which to refresh in the other direction
6691 wxRect
rect(CellToRect(oper
.MakeCoords(m_dragRowOrCol
, 0)));
6692 rect
.SetPosition(CalcScrolledPosition(rect
.GetPosition()));
6694 // we only need the ordinate (for rows) or abscissa (for columns) here,
6695 // and need to cover the entire window in the other direction
6696 oper
.Select(rect
) = 0;
6698 wxRect
rectHeader(rect
.GetPosition(),
6701 oper
.GetHeaderWindowSize(this),
6702 doper
.Select(size
) - doper
.Select(rect
)
6705 oper
.GetHeaderWindow(this)->Refresh(true, &rectHeader
);
6708 // also refresh the grid window: extend the rectangle
6711 oper
.SelectSize(rect
) = oper
.Select(size
);
6713 int subtractLines
= 0;
6714 const int lineStart
= oper
.PosToLine(this, posLineStart
);
6715 if ( lineStart
>= 0 )
6717 // ensure that if we have a multi-cell block we redraw all of
6718 // it by increasing the refresh area to cover it entirely if a
6719 // part of it is affected
6720 const int lineEnd
= oper
.PosToLine(this, posLineEnd
, true);
6721 for ( int line
= lineStart
; line
< lineEnd
; line
++ )
6723 int cellLines
= oper
.Select(
6724 GetCellSize(oper
.MakeCoords(m_dragRowOrCol
, line
)));
6725 if ( cellLines
< subtractLines
)
6726 subtractLines
= cellLines
;
6731 oper
.GetLineStartPos(this, m_dragRowOrCol
+ subtractLines
);
6732 startPos
= doper
.CalcScrolledPosition(this, startPos
);
6734 doper
.Select(rect
) = startPos
;
6735 doper
.SelectSize(rect
) = doper
.Select(size
) - startPos
;
6737 m_gridWin
->Refresh(false, &rect
);
6741 // show the edit control back again
6742 ShowCellEditControl();
6745 void wxGrid
::DoEndDragResizeRow()
6747 DoEndDragResizeLine(wxGridRowOperations());
6750 void wxGrid
::DoEndDragResizeCol(wxMouseEvent
*event
)
6752 DoEndDragResizeLine(wxGridColumnOperations());
6754 // Note: we are ending the event *after* doing
6755 // default processing in this case
6758 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, *event
);
6760 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
);
6763 void wxGrid
::DoEndDragMoveCol()
6765 //The user clicked on the column but didn't actually drag
6766 if ( m_dragLastPos
< 0 )
6768 m_colWindow
->Refresh(); //Do this to "unpress" the column
6773 if ( m_moveToCol
== -1 )
6774 newPos
= m_numCols
- 1;
6777 newPos
= GetColPos( m_moveToCol
);
6778 if ( newPos
> GetColPos( m_dragRowOrCol
) )
6782 SetColPos( m_dragRowOrCol
, newPos
);
6785 void wxGrid
::SetColPos(int idx
, int pos
)
6787 // we're going to need m_colAt now, initialize it if needed
6788 if ( m_colAt
.empty() )
6790 m_colAt
.reserve(m_numCols
);
6791 for ( int i
= 0; i
< m_numCols
; i
++ )
6792 m_colAt
.push_back(i
);
6795 wxHeaderCtrl
::MoveColumnInOrderArray(m_colAt
, idx
, pos
);
6797 // also recalculate the column rights
6798 if ( !m_colWidths
.IsEmpty() )
6802 for ( colPos
= 0; colPos
< m_numCols
; colPos
++ )
6804 int colID
= GetColAt( colPos
);
6806 colRight
+= m_colWidths
[colID
];
6807 m_colRights
[colID
] = colRight
;
6811 // and make the changes visible
6812 if ( m_useNativeHeader
)
6813 GetColHeader()->SetColumnsOrder(m_colAt
);
6815 m_colWindow
->Refresh();
6816 m_gridWin
->Refresh();
6821 void wxGrid
::EnableDragColMove( bool enable
)
6823 if ( m_canDragColMove
== enable
)
6826 m_canDragColMove
= enable
;
6828 if ( !m_canDragColMove
)
6832 //Recalculate the column rights
6833 if ( !m_colWidths
.IsEmpty() )
6837 for ( colPos
= 0; colPos
< m_numCols
; colPos
++ )
6839 colRight
+= m_colWidths
[colPos
];
6840 m_colRights
[colPos
] = colRight
;
6844 m_colWindow
->Refresh();
6845 m_gridWin
->Refresh();
6851 // ------ interaction with data model
6853 bool wxGrid
::ProcessTableMessage( wxGridTableMessage
& msg
)
6855 switch ( msg
.GetId() )
6857 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES
:
6858 return GetModelValues();
6860 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES
:
6861 return SetModelValues();
6863 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
6864 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
6865 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
6866 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
6867 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
6868 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
6869 return Redimension( msg
);
6876 // The behaviour of this function depends on the grid table class
6877 // Clear() function. For the default wxGridStringTable class the
6878 // behaviour is to replace all cell contents with wxEmptyString but
6879 // not to change the number of rows or cols.
6881 void wxGrid
::ClearGrid()
6885 if (IsCellEditControlEnabled())
6886 DisableCellEditControl();
6889 if (!GetBatchCount())
6890 m_gridWin
->Refresh();
6895 wxGrid
::DoModifyLines(bool (wxGridTableBase
::*funcModify
)(size_t, size_t),
6896 int pos
, int num
, bool WXUNUSED(updateLabels
) )
6898 wxCHECK_MSG( m_created
, false, "must finish creating the grid first" );
6903 if ( IsCellEditControlEnabled() )
6904 DisableCellEditControl();
6906 return (m_table
->*funcModify
)(pos
, num
);
6908 // the table will have sent the results of the insert row
6909 // operation to this view object as a grid table message
6913 wxGrid
::DoAppendLines(bool (wxGridTableBase
::*funcAppend
)(size_t),
6914 int num
, bool WXUNUSED(updateLabels
))
6916 wxCHECK_MSG( m_created
, false, "must finish creating the grid first" );
6921 return (m_table
->*funcAppend
)(num
);
6925 // ----- event handlers
6928 // Generate a grid event based on a mouse event and return:
6929 // -1 if the event was vetoed
6930 // +1 if the event was processed (but not vetoed)
6931 // 0 if the event wasn't handled
6933 wxGrid
::SendEvent(const wxEventType type
,
6935 wxMouseEvent
& mouseEv
)
6937 bool claimed
, vetoed
;
6939 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
6941 int rowOrCol
= (row
== -1 ? col
: row
);
6943 wxGridSizeEvent
gridEvt( GetId(),
6947 mouseEv
.GetX() + GetRowLabelSize(),
6948 mouseEv
.GetY() + GetColLabelSize(),
6951 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6952 vetoed
= !gridEvt
.IsAllowed();
6954 else if ( type
== wxEVT_GRID_RANGE_SELECT
)
6956 // Right now, it should _never_ end up here!
6957 wxGridRangeSelectEvent
gridEvt( GetId(),
6960 m_selectedBlockTopLeft
,
6961 m_selectedBlockBottomRight
,
6965 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6966 vetoed
= !gridEvt
.IsAllowed();
6968 else if ( type
== wxEVT_GRID_LABEL_LEFT_CLICK
||
6969 type
== wxEVT_GRID_LABEL_LEFT_DCLICK
||
6970 type
== wxEVT_GRID_LABEL_RIGHT_CLICK
||
6971 type
== wxEVT_GRID_LABEL_RIGHT_DCLICK
)
6973 wxPoint pos
= mouseEv
.GetPosition();
6975 if ( mouseEv
.GetEventObject() == GetGridRowLabelWindow() )
6976 pos
.y
+= GetColLabelSize();
6977 if ( mouseEv
.GetEventObject() == GetGridColLabelWindow() )
6978 pos
.x
+= GetRowLabelSize();
6980 wxGridEvent
gridEvt( GetId(),
6988 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6989 vetoed
= !gridEvt
.IsAllowed();
6993 wxGridEvent
gridEvt( GetId(),
6997 mouseEv
.GetX() + GetRowLabelSize(),
6998 mouseEv
.GetY() + GetColLabelSize(),
7001 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
7002 vetoed
= !gridEvt
.IsAllowed();
7005 // A Veto'd event may not be `claimed' so test this first
7009 return claimed ?
1 : 0;
7012 // Generate a grid event of specified type, return value same as above
7014 int wxGrid
::SendEvent(const wxEventType type
, int row
, int col
)
7016 bool claimed
, vetoed
;
7018 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
7020 int rowOrCol
= (row
== -1 ? col
: row
);
7022 wxGridSizeEvent
gridEvt( GetId(), type
, this, rowOrCol
);
7024 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
7025 vetoed
= !gridEvt
.IsAllowed();
7029 wxGridEvent
gridEvt( GetId(), type
, this, row
, col
);
7031 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
7032 vetoed
= !gridEvt
.IsAllowed();
7035 // A Veto'd event may not be `claimed' so test this first
7039 return claimed ?
1 : 0;
7042 void wxGrid
::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
7044 // needed to prevent zillions of paint events on MSW
7048 void wxGrid
::Refresh(bool eraseb
, const wxRect
* rect
)
7050 // Don't do anything if between Begin/EndBatch...
7051 // EndBatch() will do all this on the last nested one anyway.
7052 if ( m_created
&& !GetBatchCount() )
7054 // Refresh to get correct scrolled position:
7055 wxScrolledWindow
::Refresh(eraseb
, rect
);
7059 int rect_x
, rect_y
, rectWidth
, rectHeight
;
7060 int width_label
, width_cell
, height_label
, height_cell
;
7063 // Copy rectangle can get scroll offsets..
7064 rect_x
= rect
->GetX();
7065 rect_y
= rect
->GetY();
7066 rectWidth
= rect
->GetWidth();
7067 rectHeight
= rect
->GetHeight();
7069 width_label
= m_rowLabelWidth
- rect_x
;
7070 if (width_label
> rectWidth
)
7071 width_label
= rectWidth
;
7073 height_label
= m_colLabelHeight
- rect_y
;
7074 if (height_label
> rectHeight
)
7075 height_label
= rectHeight
;
7077 if (rect_x
> m_rowLabelWidth
)
7079 x
= rect_x
- m_rowLabelWidth
;
7080 width_cell
= rectWidth
;
7085 width_cell
= rectWidth
- (m_rowLabelWidth
- rect_x
);
7088 if (rect_y
> m_colLabelHeight
)
7090 y
= rect_y
- m_colLabelHeight
;
7091 height_cell
= rectHeight
;
7096 height_cell
= rectHeight
- (m_colLabelHeight
- rect_y
);
7099 // Paint corner label part intersecting rect.
7100 if ( width_label
> 0 && height_label
> 0 )
7102 wxRect
anotherrect(rect_x
, rect_y
, width_label
, height_label
);
7103 m_cornerLabelWin
->Refresh(eraseb
, &anotherrect
);
7106 // Paint col labels part intersecting rect.
7107 if ( width_cell
> 0 && height_label
> 0 )
7109 wxRect
anotherrect(x
, rect_y
, width_cell
, height_label
);
7110 m_colWindow
->Refresh(eraseb
, &anotherrect
);
7113 // Paint row labels part intersecting rect.
7114 if ( width_label
> 0 && height_cell
> 0 )
7116 wxRect
anotherrect(rect_x
, y
, width_label
, height_cell
);
7117 m_rowLabelWin
->Refresh(eraseb
, &anotherrect
);
7120 // Paint cell area part intersecting rect.
7121 if ( width_cell
> 0 && height_cell
> 0 )
7123 wxRect
anotherrect(x
, y
, width_cell
, height_cell
);
7124 m_gridWin
->Refresh(eraseb
, &anotherrect
);
7129 m_cornerLabelWin
->Refresh(eraseb
, NULL
);
7130 m_colWindow
->Refresh(eraseb
, NULL
);
7131 m_rowLabelWin
->Refresh(eraseb
, NULL
);
7132 m_gridWin
->Refresh(eraseb
, NULL
);
7137 void wxGrid
::OnSize(wxSizeEvent
& WXUNUSED(event
))
7139 if (m_targetWindow
!= this) // check whether initialisation has been done
7141 // reposition our children windows
7146 void wxGrid
::OnKeyDown( wxKeyEvent
& event
)
7148 if ( m_inOnKeyDown
)
7150 // shouldn't be here - we are going round in circles...
7152 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") );
7155 m_inOnKeyDown
= true;
7157 // propagate the event up and see if it gets processed
7158 wxWindow
*parent
= GetParent();
7159 wxKeyEvent
keyEvt( event
);
7160 keyEvt
.SetEventObject( parent
);
7162 if ( !parent
->GetEventHandler()->ProcessEvent( keyEvt
) )
7164 if (GetLayoutDirection() == wxLayout_RightToLeft
)
7166 if (event
.GetKeyCode() == WXK_RIGHT
)
7167 event
.m_keyCode
= WXK_LEFT
;
7168 else if (event
.GetKeyCode() == WXK_LEFT
)
7169 event
.m_keyCode
= WXK_RIGHT
;
7172 // try local handlers
7173 switch ( event
.GetKeyCode() )
7176 if ( event
.ControlDown() )
7177 MoveCursorUpBlock( event
.ShiftDown() );
7179 MoveCursorUp( event
.ShiftDown() );
7183 if ( event
.ControlDown() )
7184 MoveCursorDownBlock( event
.ShiftDown() );
7186 MoveCursorDown( event
.ShiftDown() );
7190 if ( event
.ControlDown() )
7191 MoveCursorLeftBlock( event
.ShiftDown() );
7193 MoveCursorLeft( event
.ShiftDown() );
7197 if ( event
.ControlDown() )
7198 MoveCursorRightBlock( event
.ShiftDown() );
7200 MoveCursorRight( event
.ShiftDown() );
7204 case WXK_NUMPAD_ENTER
:
7205 if ( event
.ControlDown() )
7207 event
.Skip(); // to let the edit control have the return
7211 if ( GetGridCursorRow() < GetNumberRows()-1 )
7213 MoveCursorDown( event
.ShiftDown() );
7217 // at the bottom of a column
7218 DisableCellEditControl();
7228 if (event
.ShiftDown())
7230 if ( GetGridCursorCol() > 0 )
7232 MoveCursorLeft( false );
7237 DisableCellEditControl();
7242 if ( GetGridCursorCol() < GetNumberCols() - 1 )
7244 MoveCursorRight( false );
7249 DisableCellEditControl();
7255 if ( event
.ControlDown() )
7266 if ( event
.ControlDown() )
7268 GoToCell(m_numRows
- 1, m_numCols
- 1);
7285 // Ctrl-Space selects the current column, Shift-Space -- the
7286 // current row and Ctrl-Shift-Space -- everything
7287 switch ( m_selection ? event
.GetModifiers() : wxMOD_NONE
)
7290 m_selection
->SelectCol(m_currentCellCoords
.GetCol());
7294 m_selection
->SelectRow(m_currentCellCoords
.GetRow());
7297 case wxMOD_CONTROL
| wxMOD_SHIFT
:
7298 m_selection
->SelectBlock(0, 0,
7299 m_numRows
- 1, m_numCols
- 1);
7303 if ( !IsEditable() )
7305 MoveCursorRight(false);
7308 //else: fall through
7321 m_inOnKeyDown
= false;
7324 void wxGrid
::OnKeyUp( wxKeyEvent
& event
)
7326 // try local handlers
7328 if ( event
.GetKeyCode() == WXK_SHIFT
)
7330 if ( m_selectedBlockTopLeft
!= wxGridNoCellCoords
&&
7331 m_selectedBlockBottomRight
!= wxGridNoCellCoords
)
7335 m_selection
->SelectBlock(
7336 m_selectedBlockTopLeft
,
7337 m_selectedBlockBottomRight
,
7342 m_selectedBlockTopLeft
= wxGridNoCellCoords
;
7343 m_selectedBlockBottomRight
= wxGridNoCellCoords
;
7344 m_selectedBlockCorner
= wxGridNoCellCoords
;
7348 void wxGrid
::OnChar( wxKeyEvent
& event
)
7350 // is it possible to edit the current cell at all?
7351 if ( !IsCellEditControlEnabled() && CanEnableCellControl() )
7353 // yes, now check whether the cells editor accepts the key
7354 int row
= m_currentCellCoords
.GetRow();
7355 int col
= m_currentCellCoords
.GetCol();
7356 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7357 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
7359 // <F2> is special and will always start editing, for
7360 // other keys - ask the editor itself
7361 if ( (event
.GetKeyCode() == WXK_F2
&& !event
.HasModifiers())
7362 || editor
->IsAcceptedKey(event
) )
7364 // ensure cell is visble
7365 MakeCellVisible(row
, col
);
7366 EnableCellEditControl();
7368 // a problem can arise if the cell is not completely
7369 // visible (even after calling MakeCellVisible the
7370 // control is not created and calling StartingKey will
7372 if ( event
.GetKeyCode() != WXK_F2
&& editor
->IsCreated() && m_cellEditCtrlEnabled
)
7373 editor
->StartingKey(event
);
7389 void wxGrid
::OnEraseBackground(wxEraseEvent
&)
7393 bool wxGrid
::SetCurrentCell( const wxGridCellCoords
& coords
)
7395 if ( SendEvent(wxEVT_GRID_SELECT_CELL
, coords
) == -1 )
7397 // the event has been vetoed - do nothing
7401 #if !defined(__WXMAC__)
7402 wxClientDC
dc( m_gridWin
);
7406 if ( m_currentCellCoords
!= wxGridNoCellCoords
)
7408 DisableCellEditControl();
7410 if ( IsVisible( m_currentCellCoords
, false ) )
7413 r
= BlockToDeviceRect( m_currentCellCoords
, m_currentCellCoords
);
7414 if ( !m_gridLinesEnabled
)
7422 wxGridCellCoordsArray cells
= CalcCellsExposed( r
);
7424 // Otherwise refresh redraws the highlight!
7425 m_currentCellCoords
= coords
;
7427 #if defined(__WXMAC__)
7428 m_gridWin
->Refresh(true /*, & r */);
7430 DrawGridCellArea( dc
, cells
);
7431 DrawAllGridLines( dc
, r
);
7436 m_currentCellCoords
= coords
;
7438 wxGridCellAttr
*attr
= GetCellAttr( coords
);
7439 #if !defined(__WXMAC__)
7440 DrawCellHighlight( dc
, attr
);
7448 wxGrid
::UpdateBlockBeingSelected(int topRow
, int leftCol
,
7449 int bottomRow
, int rightCol
)
7453 switch ( m_selection
->GetSelectionMode() )
7456 wxFAIL_MSG( "unknown selection mode" );
7459 case wxGridSelectCells
:
7460 // arbitrary blocks selection allowed so just use the cell
7461 // coordinates as is
7464 case wxGridSelectRows
:
7465 // only full rows selection allowd, ensure that we do select
7468 rightCol
= GetNumberCols() - 1;
7471 case wxGridSelectColumns
:
7472 // same as above but for columns
7474 bottomRow
= GetNumberRows() - 1;
7477 case wxGridSelectRowsOrColumns
:
7478 // in this mode we can select only full rows or full columns so
7479 // it doesn't make sense to select blocks at all (and we can't
7480 // extend the block because there is no preferred direction, we
7481 // could only extend it to cover the entire grid but this is
7487 m_selectedBlockCorner
= wxGridCellCoords(bottomRow
, rightCol
);
7488 MakeCellVisible(m_selectedBlockCorner
);
7490 EnsureFirstLessThanSecond(topRow
, bottomRow
);
7491 EnsureFirstLessThanSecond(leftCol
, rightCol
);
7493 wxGridCellCoords updateTopLeft
= wxGridCellCoords(topRow
, leftCol
),
7494 updateBottomRight
= wxGridCellCoords(bottomRow
, rightCol
);
7496 // First the case that we selected a completely new area
7497 if ( m_selectedBlockTopLeft
== wxGridNoCellCoords
||
7498 m_selectedBlockBottomRight
== wxGridNoCellCoords
)
7501 rect
= BlockToDeviceRect( wxGridCellCoords ( topRow
, leftCol
),
7502 wxGridCellCoords ( bottomRow
, rightCol
) );
7503 m_gridWin
->Refresh( false, &rect
);
7506 // Now handle changing an existing selection area.
7507 else if ( m_selectedBlockTopLeft
!= updateTopLeft
||
7508 m_selectedBlockBottomRight
!= updateBottomRight
)
7510 // Compute two optimal update rectangles:
7511 // Either one rectangle is a real subset of the
7512 // other, or they are (almost) disjoint!
7514 bool need_refresh
[4];
7518 need_refresh
[3] = false;
7521 // Store intermediate values
7522 wxCoord oldLeft
= m_selectedBlockTopLeft
.GetCol();
7523 wxCoord oldTop
= m_selectedBlockTopLeft
.GetRow();
7524 wxCoord oldRight
= m_selectedBlockBottomRight
.GetCol();
7525 wxCoord oldBottom
= m_selectedBlockBottomRight
.GetRow();
7527 // Determine the outer/inner coordinates.
7528 EnsureFirstLessThanSecond(oldLeft
, leftCol
);
7529 EnsureFirstLessThanSecond(oldTop
, topRow
);
7530 EnsureFirstLessThanSecond(rightCol
, oldRight
);
7531 EnsureFirstLessThanSecond(bottomRow
, oldBottom
);
7533 // Now, either the stuff marked old is the outer
7534 // rectangle or we don't have a situation where one
7535 // is contained in the other.
7537 if ( oldLeft
< leftCol
)
7539 // Refresh the newly selected or deselected
7540 // area to the left of the old or new selection.
7541 need_refresh
[0] = true;
7542 rect
[0] = BlockToDeviceRect(
7543 wxGridCellCoords( oldTop
, oldLeft
),
7544 wxGridCellCoords( oldBottom
, leftCol
- 1 ) );
7547 if ( oldTop
< topRow
)
7549 // Refresh the newly selected or deselected
7550 // area above the old or new selection.
7551 need_refresh
[1] = true;
7552 rect
[1] = BlockToDeviceRect(
7553 wxGridCellCoords( oldTop
, leftCol
),
7554 wxGridCellCoords( topRow
- 1, rightCol
) );
7557 if ( oldRight
> rightCol
)
7559 // Refresh the newly selected or deselected
7560 // area to the right of the old or new selection.
7561 need_refresh
[2] = true;
7562 rect
[2] = BlockToDeviceRect(
7563 wxGridCellCoords( oldTop
, rightCol
+ 1 ),
7564 wxGridCellCoords( oldBottom
, oldRight
) );
7567 if ( oldBottom
> bottomRow
)
7569 // Refresh the newly selected or deselected
7570 // area below the old or new selection.
7571 need_refresh
[3] = true;
7572 rect
[3] = BlockToDeviceRect(
7573 wxGridCellCoords( bottomRow
+ 1, leftCol
),
7574 wxGridCellCoords( oldBottom
, rightCol
) );
7577 // various Refresh() calls
7578 for (i
= 0; i
< 4; i
++ )
7579 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
7580 m_gridWin
->Refresh( false, &(rect
[i
]) );
7584 m_selectedBlockTopLeft
= updateTopLeft
;
7585 m_selectedBlockBottomRight
= updateBottomRight
;
7589 // ------ functions to get/send data (see also public functions)
7592 bool wxGrid
::GetModelValues()
7594 // Hide the editor, so it won't hide a changed value.
7595 HideCellEditControl();
7599 // all we need to do is repaint the grid
7601 m_gridWin
->Refresh();
7608 bool wxGrid
::SetModelValues()
7612 // Disable the editor, so it won't hide a changed value.
7613 // Do we also want to save the current value of the editor first?
7615 DisableCellEditControl();
7619 for ( row
= 0; row
< m_numRows
; row
++ )
7621 for ( col
= 0; col
< m_numCols
; col
++ )
7623 m_table
->SetValue( row
, col
, GetCellValue(row
, col
) );
7633 // Note - this function only draws cells that are in the list of
7634 // exposed cells (usually set from the update region by
7635 // CalcExposedCells)
7637 void wxGrid
::DrawGridCellArea( wxDC
& dc
, const wxGridCellCoordsArray
& cells
)
7639 if ( !m_numRows
|| !m_numCols
)
7642 int i
, numCells
= cells
.GetCount();
7643 int row
, col
, cell_rows
, cell_cols
;
7644 wxGridCellCoordsArray redrawCells
;
7646 for ( i
= numCells
- 1; i
>= 0; i
-- )
7648 row
= cells
[i
].GetRow();
7649 col
= cells
[i
].GetCol();
7650 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7652 // If this cell is part of a multicell block, find owner for repaint
7653 if ( cell_rows
<= 0 || cell_cols
<= 0 )
7655 wxGridCellCoords
cell( row
+ cell_rows
, col
+ cell_cols
);
7656 bool marked
= false;
7657 for ( int j
= 0; j
< numCells
; j
++ )
7659 if ( cell
== cells
[j
] )
7668 int count
= redrawCells
.GetCount();
7669 for (int j
= 0; j
< count
; j
++)
7671 if ( cell
== redrawCells
[j
] )
7679 redrawCells
.Add( cell
);
7682 // don't bother drawing this cell
7686 // If this cell is empty, find cell to left that might want to overflow
7687 if (m_table
&& m_table
->IsEmptyCell(row
, col
))
7689 for ( int l
= 0; l
< cell_rows
; l
++ )
7691 // find a cell in this row to leave already marked for repaint
7693 for (int k
= 0; k
< int(redrawCells
.GetCount()); k
++)
7694 if ((redrawCells
[k
].GetCol() < left
) &&
7695 (redrawCells
[k
].GetRow() == row
))
7697 left
= redrawCells
[k
].GetCol();
7701 left
= 0; // oh well
7703 for (int j
= col
- 1; j
>= left
; j
--)
7705 if (!m_table
->IsEmptyCell(row
+ l
, j
))
7707 if (GetCellOverflow(row
+ l
, j
))
7709 wxGridCellCoords
cell(row
+ l
, j
);
7710 bool marked
= false;
7712 for (int k
= 0; k
< numCells
; k
++)
7714 if ( cell
== cells
[k
] )
7723 int count
= redrawCells
.GetCount();
7724 for (int k
= 0; k
< count
; k
++)
7726 if ( cell
== redrawCells
[k
] )
7733 redrawCells
.Add( cell
);
7742 DrawCell( dc
, cells
[i
] );
7745 numCells
= redrawCells
.GetCount();
7747 for ( i
= numCells
- 1; i
>= 0; i
-- )
7749 DrawCell( dc
, redrawCells
[i
] );
7753 void wxGrid
::DrawGridSpace( wxDC
& dc
)
7756 m_gridWin
->GetClientSize( &cw
, &ch
);
7759 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
7761 int rightCol
= m_numCols
> 0 ?
GetColRight(GetColAt( m_numCols
- 1 )) : 0;
7762 int bottomRow
= m_numRows
> 0 ?
GetRowBottom(m_numRows
- 1) : 0;
7764 if ( right
> rightCol
|| bottom
> bottomRow
)
7767 CalcUnscrolledPosition( 0, 0, &left
, &top
);
7769 dc
.SetBrush(GetDefaultCellBackgroundColour());
7770 dc
.SetPen( *wxTRANSPARENT_PEN
);
7772 if ( right
> rightCol
)
7774 dc
.DrawRectangle( rightCol
, top
, right
- rightCol
, ch
);
7777 if ( bottom
> bottomRow
)
7779 dc
.DrawRectangle( left
, bottomRow
, cw
, bottom
- bottomRow
);
7784 void wxGrid
::DrawCell( wxDC
& dc
, const wxGridCellCoords
& coords
)
7786 int row
= coords
.GetRow();
7787 int col
= coords
.GetCol();
7789 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7792 // we draw the cell border ourselves
7793 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7795 bool isCurrent
= coords
== m_currentCellCoords
;
7797 wxRect rect
= CellToRect( row
, col
);
7799 // if the editor is shown, we should use it and not the renderer
7800 // Note: However, only if it is really _shown_, i.e. not hidden!
7801 if ( isCurrent
&& IsCellEditControlShown() )
7803 // NB: this "#if..." is temporary and fixes a problem where the
7804 // edit control is erased by this code after being rendered.
7805 // On wxMac (QD build only), the cell editor is a wxTextCntl and is rendered
7806 // implicitly, causing this out-of order render.
7807 #if !defined(__WXMAC__)
7808 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
7809 editor
->PaintBackground(rect
, attr
);
7815 // but all the rest is drawn by the cell renderer and hence may be customized
7816 wxGridCellRenderer
*renderer
= attr
->GetRenderer(this, row
, col
);
7817 renderer
->Draw(*this, *attr
, dc
, rect
, row
, col
, IsInSelection(coords
));
7824 void wxGrid
::DrawCellHighlight( wxDC
& dc
, const wxGridCellAttr
*attr
)
7826 // don't show highlight when the grid doesn't have focus
7830 int row
= m_currentCellCoords
.GetRow();
7831 int col
= m_currentCellCoords
.GetCol();
7833 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7836 wxRect rect
= CellToRect(row
, col
);
7838 // hmmm... what could we do here to show that the cell is disabled?
7839 // for now, I just draw a thinner border than for the other ones, but
7840 // it doesn't look really good
7842 int penWidth
= attr
->IsReadOnly() ? m_cellHighlightROPenWidth
: m_cellHighlightPenWidth
;
7846 // The center of the drawn line is where the position/width/height of
7847 // the rectangle is actually at (on wxMSW at least), so the
7848 // size of the rectangle is reduced to compensate for the thickness of
7849 // the line. If this is too strange on non-wxMSW platforms then
7850 // please #ifdef this appropriately.
7851 rect
.x
+= penWidth
/ 2;
7852 rect
.y
+= penWidth
/ 2;
7853 rect
.width
-= penWidth
- 1;
7854 rect
.height
-= penWidth
- 1;
7856 // Now draw the rectangle
7857 // use the cellHighlightColour if the cell is inside a selection, this
7858 // will ensure the cell is always visible.
7859 dc
.SetPen(wxPen(IsInSelection(row
,col
) ? m_selectionForeground
7860 : m_cellHighlightColour
,
7862 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
7863 dc
.DrawRectangle(rect
);
7867 wxPen wxGrid
::GetDefaultGridLinePen()
7869 return wxPen(GetGridLineColour());
7872 wxPen wxGrid
::GetRowGridLinePen(int WXUNUSED(row
))
7874 return GetDefaultGridLinePen();
7877 wxPen wxGrid
::GetColGridLinePen(int WXUNUSED(col
))
7879 return GetDefaultGridLinePen();
7882 void wxGrid
::DrawCellBorder( wxDC
& dc
, const wxGridCellCoords
& coords
)
7884 int row
= coords
.GetRow();
7885 int col
= coords
.GetCol();
7886 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7890 wxRect rect
= CellToRect( row
, col
);
7892 // right hand border
7893 dc
.SetPen( GetColGridLinePen(col
) );
7894 dc
.DrawLine( rect
.x
+ rect
.width
, rect
.y
,
7895 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
+ 1 );
7898 dc
.SetPen( GetRowGridLinePen(row
) );
7899 dc
.DrawLine( rect
.x
, rect
.y
+ rect
.height
,
7900 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
);
7903 void wxGrid
::DrawHighlight(wxDC
& dc
, const wxGridCellCoordsArray
& cells
)
7905 // This if block was previously in wxGrid::OnPaint but that doesn't
7906 // seem to get called under wxGTK - MB
7908 if ( m_currentCellCoords
== wxGridNoCellCoords
&&
7909 m_numRows
&& m_numCols
)
7911 m_currentCellCoords
.Set(0, 0);
7914 if ( IsCellEditControlShown() )
7916 // don't show highlight when the edit control is shown
7920 // if the active cell was repainted, repaint its highlight too because it
7921 // might have been damaged by the grid lines
7922 size_t count
= cells
.GetCount();
7923 for ( size_t n
= 0; n
< count
; n
++ )
7925 wxGridCellCoords cell
= cells
[n
];
7927 // If we are using attributes, then we may have just exposed another
7928 // cell in a partially-visible merged cluster of cells. If the "anchor"
7929 // (upper left) cell of this merged cluster is the cell indicated by
7930 // m_currentCellCoords, then we need to refresh the cell highlight even
7931 // though the "anchor" itself is not part of our update segment.
7932 if ( CanHaveAttributes() )
7936 GetCellSize(cell
.GetRow(), cell
.GetCol(), &rows
, &cols
);
7939 cell
.SetRow(cell
.GetRow() + rows
);
7942 cell
.SetCol(cell
.GetCol() + cols
);
7945 if ( cell
== m_currentCellCoords
)
7947 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
7948 DrawCellHighlight(dc
, attr
);
7956 // This is used to redraw all grid lines e.g. when the grid line colour
7959 void wxGrid
::DrawAllGridLines( wxDC
& dc
, const wxRegion
& WXUNUSED(reg
) )
7961 if ( !m_gridLinesEnabled
)
7964 int top
, bottom
, left
, right
;
7967 m_gridWin
->GetClientSize(&cw
, &ch
);
7968 CalcUnscrolledPosition( 0, 0, &left
, &top
);
7969 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
7971 // avoid drawing grid lines past the last row and col
7972 if ( m_gridLinesClipHorz
)
7977 const int lastColRight
= GetColRight(GetColAt(m_numCols
- 1));
7978 if ( right
> lastColRight
)
7979 right
= lastColRight
;
7982 if ( m_gridLinesClipVert
)
7987 const int lastRowBottom
= GetRowBottom(m_numRows
- 1);
7988 if ( bottom
> lastRowBottom
)
7989 bottom
= lastRowBottom
;
7992 // no gridlines inside multicells, clip them out
7993 int leftCol
= GetColPos( internalXToCol(left
) );
7994 int topRow
= internalYToRow(top
);
7995 int rightCol
= GetColPos( internalXToCol(right
) );
7996 int bottomRow
= internalYToRow(bottom
);
7998 wxRegion
clippedcells(0, 0, cw
, ch
);
8000 int cell_rows
, cell_cols
;
8003 for ( int j
= topRow
; j
<= bottomRow
; j
++ )
8005 for ( int colPos
= leftCol
; colPos
<= rightCol
; colPos
++ )
8007 int i
= GetColAt( colPos
);
8009 GetCellSize( j
, i
, &cell_rows
, &cell_cols
);
8010 if ((cell_rows
> 1) || (cell_cols
> 1))
8012 rect
= CellToRect(j
,i
);
8013 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
8014 clippedcells
.Subtract(rect
);
8016 else if ((cell_rows
< 0) || (cell_cols
< 0))
8018 rect
= CellToRect(j
+ cell_rows
, i
+ cell_cols
);
8019 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
8020 clippedcells
.Subtract(rect
);
8025 dc
.SetDeviceClippingRegion( clippedcells
);
8028 // horizontal grid lines
8029 for ( int i
= internalYToRow(top
); i
< m_numRows
; i
++ )
8031 int bot
= GetRowBottom(i
) - 1;
8038 dc
.SetPen( GetRowGridLinePen(i
) );
8039 dc
.DrawLine( left
, bot
, right
, bot
);
8043 // vertical grid lines
8044 for ( int colPos
= leftCol
; colPos
< m_numCols
; colPos
++ )
8046 int i
= GetColAt( colPos
);
8048 int colRight
= GetColRight(i
);
8050 if (GetLayoutDirection() != wxLayout_RightToLeft
)
8054 if ( colRight
> right
)
8057 if ( colRight
>= left
)
8059 dc
.SetPen( GetColGridLinePen(i
) );
8060 dc
.DrawLine( colRight
, top
, colRight
, bottom
);
8064 dc
.DestroyClippingRegion();
8067 void wxGrid
::DrawRowLabels( wxDC
& dc
, const wxArrayInt
& rows
)
8072 const size_t numLabels
= rows
.GetCount();
8073 for ( size_t i
= 0; i
< numLabels
; i
++ )
8075 DrawRowLabel( dc
, rows
[i
] );
8079 void wxGrid
::DrawRowLabel( wxDC
& dc
, int row
)
8081 if ( GetRowHeight(row
) <= 0 || m_rowLabelWidth
<= 0 )
8086 int rowTop
= GetRowTop(row
),
8087 rowBottom
= GetRowBottom(row
) - 1;
8089 dc
.SetPen( wxPen(wxSystemSettings
::GetColour(wxSYS_COLOUR_3DSHADOW
)));
8090 dc
.DrawLine( m_rowLabelWidth
- 1, rowTop
, m_rowLabelWidth
- 1, rowBottom
);
8091 dc
.DrawLine( 0, rowTop
, 0, rowBottom
);
8092 dc
.DrawLine( 0, rowBottom
, m_rowLabelWidth
, rowBottom
);
8094 dc
.SetPen( *wxWHITE_PEN
);
8095 dc
.DrawLine( 1, rowTop
, 1, rowBottom
);
8096 dc
.DrawLine( 1, rowTop
, m_rowLabelWidth
- 1, rowTop
);
8098 dc
.SetBackgroundMode( wxBRUSHSTYLE_TRANSPARENT
);
8099 dc
.SetTextForeground( GetLabelTextColour() );
8100 dc
.SetFont( GetLabelFont() );
8103 GetRowLabelAlignment( &hAlign
, &vAlign
);
8106 rect
.SetY( GetRowTop(row
) + 2 );
8107 rect
.SetWidth( m_rowLabelWidth
- 4 );
8108 rect
.SetHeight( GetRowHeight(row
) - 4 );
8109 DrawTextRectangle( dc
, GetRowLabelValue( row
), rect
, hAlign
, vAlign
);
8112 void wxGrid
::UseNativeColHeader(bool native
)
8114 if ( native
== m_useNativeHeader
)
8118 m_useNativeHeader
= native
;
8120 CreateColumnWindow();
8122 if ( m_useNativeHeader
)
8123 GetColHeader()->SetColumnCount(m_numCols
);
8127 void wxGrid
::SetUseNativeColLabels( bool native
)
8129 wxASSERT_MSG( !m_useNativeHeader
,
8130 "doesn't make sense when using native header" );
8132 m_nativeColumnLabels
= native
;
8135 int height
= wxRendererNative
::Get().GetHeaderButtonHeight( this );
8136 SetColLabelSize( height
);
8139 GetColLabelWindow()->Refresh();
8140 m_cornerLabelWin
->Refresh();
8143 void wxGrid
::DrawColLabels( wxDC
& dc
,const wxArrayInt
& cols
)
8148 const size_t numLabels
= cols
.GetCount();
8149 for ( size_t i
= 0; i
< numLabels
; i
++ )
8151 DrawColLabel( dc
, cols
[i
] );
8155 void wxGrid
::DrawCornerLabel(wxDC
& dc
)
8157 if ( m_nativeColumnLabels
)
8159 wxRect
rect(wxSize(m_rowLabelWidth
, m_colLabelHeight
));
8162 wxRendererNative
::Get().DrawHeaderButton(m_cornerLabelWin
, dc
, rect
, 0);
8166 dc
.SetPen(wxPen(wxSystemSettings
::GetColour(wxSYS_COLOUR_3DSHADOW
)));
8167 dc
.DrawLine( m_rowLabelWidth
- 1, m_colLabelHeight
- 1,
8168 m_rowLabelWidth
- 1, 0 );
8169 dc
.DrawLine( m_rowLabelWidth
- 1, m_colLabelHeight
- 1,
8170 0, m_colLabelHeight
- 1 );
8171 dc
.DrawLine( 0, 0, m_rowLabelWidth
, 0 );
8172 dc
.DrawLine( 0, 0, 0, m_colLabelHeight
);
8174 dc
.SetPen( *wxWHITE_PEN
);
8175 dc
.DrawLine( 1, 1, m_rowLabelWidth
- 1, 1 );
8176 dc
.DrawLine( 1, 1, 1, m_colLabelHeight
- 1 );
8180 void wxGrid
::DrawColLabel(wxDC
& dc
, int col
)
8182 if ( GetColWidth(col
) <= 0 || m_colLabelHeight
<= 0 )
8185 int colLeft
= GetColLeft(col
);
8187 wxRect
rect(colLeft
, 0, GetColWidth(col
), m_colLabelHeight
);
8189 if ( m_nativeColumnLabels
)
8191 wxRendererNative
::Get().DrawHeaderButton(GetColLabelWindow(), dc
, rect
, 0);
8195 int colRight
= GetColRight(col
) - 1;
8197 dc
.SetPen(wxPen(wxSystemSettings
::GetColour(wxSYS_COLOUR_3DSHADOW
)));
8198 dc
.DrawLine( colRight
, 0,
8199 colRight
, m_colLabelHeight
- 1 );
8200 dc
.DrawLine( colLeft
, 0,
8202 dc
.DrawLine( colLeft
, m_colLabelHeight
- 1,
8203 colRight
+ 1, m_colLabelHeight
- 1 );
8205 dc
.SetPen( *wxWHITE_PEN
);
8206 dc
.DrawLine( colLeft
, 1, colLeft
, m_colLabelHeight
- 1 );
8207 dc
.DrawLine( colLeft
, 1, colRight
, 1 );
8210 dc
.SetBackgroundMode( wxBRUSHSTYLE_TRANSPARENT
);
8211 dc
.SetTextForeground( GetLabelTextColour() );
8212 dc
.SetFont( GetLabelFont() );
8215 GetColLabelAlignment( &hAlign
, &vAlign
);
8216 const int orient
= GetColLabelTextOrientation();
8219 DrawTextRectangle(dc
, GetColLabelValue(col
), rect
, hAlign
, vAlign
, orient
);
8222 // TODO: these 2 functions should be replaced with wxDC::DrawLabel() to which
8223 // we just have to add textOrientation support
8224 void wxGrid
::DrawTextRectangle( wxDC
& dc
,
8225 const wxString
& value
,
8229 int textOrientation
)
8231 wxArrayString lines
;
8233 StringToLines( value
, lines
);
8235 DrawTextRectangle(dc
, lines
, rect
, horizAlign
, vertAlign
, textOrientation
);
8238 void wxGrid
::DrawTextRectangle(wxDC
& dc
,
8239 const wxArrayString
& lines
,
8243 int textOrientation
)
8245 if ( lines
.empty() )
8248 wxDCClipper
clip(dc
, rect
);
8253 if ( textOrientation
== wxHORIZONTAL
)
8254 GetTextBoxSize( dc
, lines
, &textWidth
, &textHeight
);
8256 GetTextBoxSize( dc
, lines
, &textHeight
, &textWidth
);
8260 switch ( vertAlign
)
8262 case wxALIGN_BOTTOM
:
8263 if ( textOrientation
== wxHORIZONTAL
)
8264 y
= rect
.y
+ (rect
.height
- textHeight
- 1);
8266 x
= rect
.x
+ rect
.width
- textWidth
;
8269 case wxALIGN_CENTRE
:
8270 if ( textOrientation
== wxHORIZONTAL
)
8271 y
= rect
.y
+ ((rect
.height
- textHeight
) / 2);
8273 x
= rect
.x
+ ((rect
.width
- textWidth
) / 2);
8278 if ( textOrientation
== wxHORIZONTAL
)
8285 // Align each line of a multi-line label
8286 size_t nLines
= lines
.GetCount();
8287 for ( size_t l
= 0; l
< nLines
; l
++ )
8289 const wxString
& line
= lines
[l
];
8293 *(textOrientation
== wxHORIZONTAL ?
&y
: &x
) += dc
.GetCharHeight();
8297 wxCoord lineWidth
= 0,
8299 dc
.GetTextExtent(line
, &lineWidth
, &lineHeight
);
8301 switch ( horizAlign
)
8304 if ( textOrientation
== wxHORIZONTAL
)
8305 x
= rect
.x
+ (rect
.width
- lineWidth
- 1);
8307 y
= rect
.y
+ lineWidth
+ 1;
8310 case wxALIGN_CENTRE
:
8311 if ( textOrientation
== wxHORIZONTAL
)
8312 x
= rect
.x
+ ((rect
.width
- lineWidth
) / 2);
8314 y
= rect
.y
+ rect
.height
- ((rect
.height
- lineWidth
) / 2);
8319 if ( textOrientation
== wxHORIZONTAL
)
8322 y
= rect
.y
+ rect
.height
- 1;
8326 if ( textOrientation
== wxHORIZONTAL
)
8328 dc
.DrawText( line
, x
, y
);
8333 dc
.DrawRotatedText( line
, x
, y
, 90.0 );
8339 // Split multi-line text up into an array of strings.
8340 // Any existing contents of the string array are preserved.
8342 // TODO: refactor wxTextFile::Read() and reuse the same code from here
8343 void wxGrid
::StringToLines( const wxString
& value
, wxArrayString
& lines
) const
8347 wxString eol
= wxTextFile
::GetEOL( wxTextFileType_Unix
);
8348 wxString tVal
= wxTextFile
::Translate( value
, wxTextFileType_Unix
);
8350 while ( startPos
< (int)tVal
.length() )
8352 pos
= tVal
.Mid(startPos
).Find( eol
);
8357 else if ( pos
== 0 )
8359 lines
.Add( wxEmptyString
);
8363 lines
.Add( tVal
.Mid(startPos
, pos
) );
8366 startPos
+= pos
+ 1;
8369 if ( startPos
< (int)tVal
.length() )
8371 lines
.Add( tVal
.Mid( startPos
) );
8375 void wxGrid
::GetTextBoxSize( const wxDC
& dc
,
8376 const wxArrayString
& lines
,
8377 long *width
, long *height
) const
8381 wxCoord lineW
= 0, lineH
= 0;
8384 for ( i
= 0; i
< lines
.GetCount(); i
++ )
8386 dc
.GetTextExtent( lines
[i
], &lineW
, &lineH
);
8387 w
= wxMax( w
, lineW
);
8396 // ------ Batch processing.
8398 void wxGrid
::EndBatch()
8400 if ( m_batchCount
> 0 )
8403 if ( !m_batchCount
)
8406 m_rowLabelWin
->Refresh();
8407 m_colWindow
->Refresh();
8408 m_cornerLabelWin
->Refresh();
8409 m_gridWin
->Refresh();
8414 // Use this, rather than wxWindow::Refresh(), to force an immediate
8415 // repainting of the grid. Has no effect if you are already inside a
8416 // BeginBatch / EndBatch block.
8418 void wxGrid
::ForceRefresh()
8424 bool wxGrid
::Enable(bool enable
)
8426 if ( !wxScrolledWindow
::Enable(enable
) )
8429 // redraw in the new state
8430 m_gridWin
->Refresh();
8436 // ------ Edit control functions
8439 void wxGrid
::EnableEditing( bool edit
)
8441 if ( edit
!= m_editable
)
8444 EnableCellEditControl(edit
);
8449 void wxGrid
::EnableCellEditControl( bool enable
)
8454 if ( enable
!= m_cellEditCtrlEnabled
)
8458 if ( SendEvent(wxEVT_GRID_EDITOR_SHOWN
) == -1 )
8461 // this should be checked by the caller!
8462 wxASSERT_MSG( CanEnableCellControl(), _T("can't enable editing for this cell!") );
8464 // do it before ShowCellEditControl()
8465 m_cellEditCtrlEnabled
= enable
;
8467 ShowCellEditControl();
8471 //FIXME:add veto support
8472 SendEvent(wxEVT_GRID_EDITOR_HIDDEN
);
8474 HideCellEditControl();
8475 SaveEditControlValue();
8477 // do it after HideCellEditControl()
8478 m_cellEditCtrlEnabled
= enable
;
8483 bool wxGrid
::IsCurrentCellReadOnly() const
8486 wxGridCellAttr
* attr
= ((wxGrid
*)this)->GetCellAttr(m_currentCellCoords
);
8487 bool readonly
= attr
->IsReadOnly();
8493 bool wxGrid
::CanEnableCellControl() const
8495 return m_editable
&& (m_currentCellCoords
!= wxGridNoCellCoords
) &&
8496 !IsCurrentCellReadOnly();
8499 bool wxGrid
::IsCellEditControlEnabled() const
8501 // the cell edit control might be disable for all cells or just for the
8502 // current one if it's read only
8503 return m_cellEditCtrlEnabled ?
!IsCurrentCellReadOnly() : false;
8506 bool wxGrid
::IsCellEditControlShown() const
8508 bool isShown
= false;
8510 if ( m_cellEditCtrlEnabled
)
8512 int row
= m_currentCellCoords
.GetRow();
8513 int col
= m_currentCellCoords
.GetCol();
8514 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8515 wxGridCellEditor
* editor
= attr
->GetEditor((wxGrid
*) this, row
, col
);
8520 if ( editor
->IsCreated() )
8522 isShown
= editor
->GetControl()->IsShown();
8532 void wxGrid
::ShowCellEditControl()
8534 if ( IsCellEditControlEnabled() )
8536 if ( !IsVisible( m_currentCellCoords
, false ) )
8538 m_cellEditCtrlEnabled
= false;
8543 wxRect rect
= CellToRect( m_currentCellCoords
);
8544 int row
= m_currentCellCoords
.GetRow();
8545 int col
= m_currentCellCoords
.GetCol();
8547 // if this is part of a multicell, find owner (topleft)
8548 int cell_rows
, cell_cols
;
8549 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
8550 if ( cell_rows
<= 0 || cell_cols
<= 0 )
8554 m_currentCellCoords
.SetRow( row
);
8555 m_currentCellCoords
.SetCol( col
);
8558 // erase the highlight and the cell contents because the editor
8559 // might not cover the entire cell
8560 wxClientDC
dc( m_gridWin
);
8562 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8563 dc
.SetBrush(wxBrush(attr
->GetBackgroundColour()));
8564 dc
.SetPen(*wxTRANSPARENT_PEN
);
8565 dc
.DrawRectangle(rect
);
8567 // convert to scrolled coords
8568 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
8574 // cell is shifted by one pixel
8575 // However, don't allow x or y to become negative
8576 // since the SetSize() method interprets that as
8583 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
8584 if ( !editor
->IsCreated() )
8586 editor
->Create(m_gridWin
, wxID_ANY
,
8587 new wxGridCellEditorEvtHandler(this, editor
));
8589 wxGridEditorCreatedEvent
evt(GetId(),
8590 wxEVT_GRID_EDITOR_CREATED
,
8594 editor
->GetControl());
8595 GetEventHandler()->ProcessEvent(evt
);
8598 // resize editor to overflow into righthand cells if allowed
8599 int maxWidth
= rect
.width
;
8600 wxString value
= GetCellValue(row
, col
);
8601 if ( (value
!= wxEmptyString
) && (attr
->GetOverflow()) )
8604 GetTextExtent(value
, &maxWidth
, &y
, NULL
, NULL
, &attr
->GetFont());
8605 if (maxWidth
< rect
.width
)
8606 maxWidth
= rect
.width
;
8609 int client_right
= m_gridWin
->GetClientSize().GetWidth();
8610 if (rect
.x
+ maxWidth
> client_right
)
8611 maxWidth
= client_right
- rect
.x
;
8613 if ((maxWidth
> rect
.width
) && (col
< m_numCols
) && m_table
)
8615 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
8616 // may have changed earlier
8617 for (int i
= col
+ cell_cols
; i
< m_numCols
; i
++)
8620 GetCellSize( row
, i
, &c_rows
, &c_cols
);
8622 // looks weird going over a multicell
8623 if (m_table
->IsEmptyCell( row
, i
) &&
8624 (rect
.width
< maxWidth
) && (c_rows
== 1))
8626 rect
.width
+= GetColWidth( i
);
8632 if (rect
.GetRight() > client_right
)
8633 rect
.SetRight( client_right
- 1 );
8636 editor
->SetCellAttr( attr
);
8637 editor
->SetSize( rect
);
8639 editor
->GetControl()->Move(
8640 editor
->GetControl()->GetPosition().x
+ nXMove
,
8641 editor
->GetControl()->GetPosition().y
);
8642 editor
->Show( true, attr
);
8644 // recalc dimensions in case we need to
8645 // expand the scrolled window to account for editor
8648 editor
->BeginEdit(row
, col
, this);
8649 editor
->SetCellAttr(NULL
);
8657 void wxGrid
::HideCellEditControl()
8659 if ( IsCellEditControlEnabled() )
8661 int row
= m_currentCellCoords
.GetRow();
8662 int col
= m_currentCellCoords
.GetCol();
8664 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8665 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
8666 const bool editorHadFocus
= editor
->GetControl()->HasFocus();
8667 editor
->Show( false );
8671 // return the focus to the grid itself if the editor had it
8673 // note that we must not do this unconditionally to avoid stealing
8674 // focus from the window which just received it if we are hiding the
8675 // editor precisely because we lost focus
8676 if ( editorHadFocus
)
8677 m_gridWin
->SetFocus();
8679 // refresh whole row to the right
8680 wxRect
rect( CellToRect(row
, col
) );
8681 CalcScrolledPosition(rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
8682 rect
.width
= m_gridWin
->GetClientSize().GetWidth() - rect
.x
;
8685 // ensure that the pixels under the focus ring get refreshed as well
8686 rect
.Inflate(10, 10);
8689 m_gridWin
->Refresh( false, &rect
);
8693 void wxGrid
::SaveEditControlValue()
8695 if ( IsCellEditControlEnabled() )
8697 int row
= m_currentCellCoords
.GetRow();
8698 int col
= m_currentCellCoords
.GetCol();
8700 wxString oldval
= GetCellValue(row
, col
);
8702 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8703 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
8704 bool changed
= editor
->EndEdit(row
, col
, this);
8711 if ( SendEvent(wxEVT_GRID_CELL_CHANGE
) == -1 )
8713 // Event has been vetoed, set the data back.
8714 SetCellValue(row
, col
, oldval
);
8721 // ------ Grid location functions
8722 // Note that all of these functions work with the logical coordinates of
8723 // grid cells and labels so you will need to convert from device
8724 // coordinates for mouse events etc.
8727 wxGridCellCoords wxGrid
::XYToCell(int x
, int y
) const
8729 int row
= YToRow(y
);
8730 int col
= XToCol(x
);
8732 return row
== -1 || col
== -1 ? wxGridNoCellCoords
8733 : wxGridCellCoords(row
, col
);
8736 // compute row or column from some (unscrolled) coordinate value, using either
8737 // m_defaultRowHeight/m_defaultColWidth or binary search on array of
8738 // m_rowBottoms/m_colRights to do it quickly (linear search shouldn't be used
8741 wxGrid
::PosToLine(int coord
,
8743 const wxGridOperations
& oper
) const
8745 const int numLines
= oper
.GetNumberOfLines(this);
8748 return clipToMinMax
&& numLines
> 0 ? oper
.GetLineAt(this, 0) : -1;
8750 const int defaultLineSize
= oper
.GetDefaultLineSize(this);
8751 wxCHECK_MSG( defaultLineSize
, -1, "can't have 0 default line size" );
8753 int maxPos
= coord
/ defaultLineSize
,
8756 // check for the simplest case: if we have no explicit line sizes
8757 // configured, then we already know the line this position falls in
8758 const wxArrayInt
& lineEnds
= oper
.GetLineEnds(this);
8759 if ( lineEnds
.empty() )
8761 if ( maxPos
< numLines
)
8764 return clipToMinMax ? numLines
- 1 : -1;
8768 // adjust maxPos before starting the binary search
8769 if ( maxPos
>= numLines
)
8771 maxPos
= numLines
- 1;
8775 if ( coord
>= lineEnds
[oper
.GetLineAt(this, maxPos
)])
8778 const int minDist
= oper
.GetMinimalAcceptableLineSize(this);
8780 maxPos
= coord
/ minDist
;
8782 maxPos
= numLines
- 1;
8785 if ( maxPos
>= numLines
)
8786 maxPos
= numLines
- 1;
8789 // check if the position is beyond the last column
8790 const int lineAtMaxPos
= oper
.GetLineAt(this, maxPos
);
8791 if ( coord
>= lineEnds
[lineAtMaxPos
] )
8792 return clipToMinMax ? lineAtMaxPos
: -1;
8794 // or before the first one
8795 const int lineAt0
= oper
.GetLineAt(this, 0);
8796 if ( coord
< lineEnds
[lineAt0
] )
8800 // finally do perform the binary search
8801 while ( minPos
< maxPos
)
8803 wxCHECK_MSG( lineEnds
[oper
.GetLineAt(this, minPos
)] <= coord
&&
8804 coord
< lineEnds
[oper
.GetLineAt(this, maxPos
)],
8806 "wxGrid: internal error in PosToLine()" );
8808 if ( coord
>= lineEnds
[oper
.GetLineAt(this, maxPos
- 1)] )
8809 return oper
.GetLineAt(this, maxPos
);
8813 const int median
= minPos
+ (maxPos
- minPos
+ 1) / 2;
8814 if ( coord
< lineEnds
[oper
.GetLineAt(this, median
)] )
8820 return oper
.GetLineAt(this, maxPos
);
8823 int wxGrid
::YToRow(int y
, bool clipToMinMax
) const
8825 return PosToLine(y
, clipToMinMax
, wxGridRowOperations());
8828 int wxGrid
::XToCol(int x
, bool clipToMinMax
) const
8830 return PosToLine(x
, clipToMinMax
, wxGridColumnOperations());
8833 // return the row number that that the y coord is near the edge of, or -1 if
8834 // not near an edge.
8836 // coords can only possibly be near an edge if
8837 // (a) the row/column is large enough to still allow for an "inner" area
8838 // that is _not_ near the edge (i.e., if the height/width is smaller
8839 // than WXGRID_LABEL_EDGE_ZONE, coords are _never_ considered to be
8842 // (b) resizing rows/columns (the thing for which edge detection is
8843 // relevant at all) is enabled.
8845 int wxGrid
::PosToEdgeOfLine(int pos
, const wxGridOperations
& oper
) const
8847 if ( !oper
.CanResizeLines(this) )
8850 const int line
= oper
.PosToLine(this, pos
, true);
8852 if ( oper
.GetLineSize(this, line
) > WXGRID_LABEL_EDGE_ZONE
)
8854 // We know that we are in this line, test whether we are close enough
8855 // to start or end border, respectively.
8856 if ( abs(oper
.GetLineEndPos(this, line
) - pos
) < WXGRID_LABEL_EDGE_ZONE
)
8858 else if ( line
> 0 &&
8859 pos
- oper
.GetLineStartPos(this,
8860 line
) < WXGRID_LABEL_EDGE_ZONE
)
8867 int wxGrid
::YToEdgeOfRow(int y
) const
8869 return PosToEdgeOfLine(y
, wxGridRowOperations());
8872 int wxGrid
::XToEdgeOfCol(int x
) const
8874 return PosToEdgeOfLine(x
, wxGridColumnOperations());
8877 wxRect wxGrid
::CellToRect( int row
, int col
) const
8879 wxRect
rect( -1, -1, -1, -1 );
8881 if ( row
>= 0 && row
< m_numRows
&&
8882 col
>= 0 && col
< m_numCols
)
8884 int i
, cell_rows
, cell_cols
;
8885 rect
.width
= rect
.height
= 0;
8886 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
8887 // if negative then find multicell owner
8892 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
8894 rect
.x
= GetColLeft(col
);
8895 rect
.y
= GetRowTop(row
);
8896 for (i
=col
; i
< col
+ cell_cols
; i
++)
8897 rect
.width
+= GetColWidth(i
);
8898 for (i
=row
; i
< row
+ cell_rows
; i
++)
8899 rect
.height
+= GetRowHeight(i
);
8902 // if grid lines are enabled, then the area of the cell is a bit smaller
8903 if (m_gridLinesEnabled
)
8912 bool wxGrid
::IsVisible( int row
, int col
, bool wholeCellVisible
) const
8914 // get the cell rectangle in logical coords
8916 wxRect
r( CellToRect( row
, col
) );
8918 // convert to device coords
8920 int left
, top
, right
, bottom
;
8921 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
8922 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
8924 // check against the client area of the grid window
8926 m_gridWin
->GetClientSize( &cw
, &ch
);
8928 if ( wholeCellVisible
)
8930 // is the cell wholly visible ?
8931 return ( left
>= 0 && right
<= cw
&&
8932 top
>= 0 && bottom
<= ch
);
8936 // is the cell partly visible ?
8938 return ( ((left
>= 0 && left
< cw
) || (right
> 0 && right
<= cw
)) &&
8939 ((top
>= 0 && top
< ch
) || (bottom
> 0 && bottom
<= ch
)) );
8943 // make the specified cell location visible by doing a minimal amount
8946 void wxGrid
::MakeCellVisible( int row
, int col
)
8949 int xpos
= -1, ypos
= -1;
8951 if ( row
>= 0 && row
< m_numRows
&&
8952 col
>= 0 && col
< m_numCols
)
8954 // get the cell rectangle in logical coords
8955 wxRect
r( CellToRect( row
, col
) );
8957 // convert to device coords
8958 int left
, top
, right
, bottom
;
8959 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
8960 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
8963 m_gridWin
->GetClientSize( &cw
, &ch
);
8969 else if ( bottom
> ch
)
8971 int h
= r
.GetHeight();
8973 for ( i
= row
- 1; i
>= 0; i
-- )
8975 int rowHeight
= GetRowHeight(i
);
8976 if ( h
+ rowHeight
> ch
)
8983 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
8984 // have rounding errors (this is important, because if we do,
8985 // we might not scroll at all and some cells won't be redrawn)
8987 // Sometimes GRID_SCROLL_LINE / 2 is not enough,
8988 // so just add a full scroll unit...
8989 ypos
+= m_scrollLineY
;
8992 // special handling for wide cells - show always left part of the cell!
8993 // Otherwise, e.g. when stepping from row to row, it would jump between
8994 // left and right part of the cell on every step!
8996 if ( left
< 0 || (right
- left
) >= cw
)
9000 else if ( right
> cw
)
9002 // position the view so that the cell is on the right
9004 CalcUnscrolledPosition(0, 0, &x0
, &y0
);
9005 xpos
= x0
+ (right
- cw
);
9007 // see comment for ypos above
9008 xpos
+= m_scrollLineX
;
9011 if ( xpos
!= -1 || ypos
!= -1 )
9014 xpos
/= m_scrollLineX
;
9016 ypos
/= m_scrollLineY
;
9017 Scroll( xpos
, ypos
);
9024 // ------ Grid cursor movement functions
9028 wxGrid
::DoMoveCursor(bool expandSelection
,
9029 const wxGridDirectionOperations
& diroper
)
9031 if ( m_currentCellCoords
== wxGridNoCellCoords
)
9034 if ( expandSelection
)
9036 wxGridCellCoords coords
= m_selectedBlockCorner
;
9037 if ( coords
== wxGridNoCellCoords
)
9038 coords
= m_currentCellCoords
;
9040 if ( diroper
.IsAtBoundary(coords
) )
9043 diroper
.Advance(coords
);
9045 UpdateBlockBeingSelected(m_currentCellCoords
, coords
);
9047 else // don't expand selection
9051 if ( diroper
.IsAtBoundary(m_currentCellCoords
) )
9054 wxGridCellCoords coords
= m_currentCellCoords
;
9055 diroper
.Advance(coords
);
9063 bool wxGrid
::MoveCursorUp(bool expandSelection
)
9065 return DoMoveCursor(expandSelection
,
9066 wxGridBackwardOperations(this, wxGridRowOperations()));
9069 bool wxGrid
::MoveCursorDown(bool expandSelection
)
9071 return DoMoveCursor(expandSelection
,
9072 wxGridForwardOperations(this, wxGridRowOperations()));
9075 bool wxGrid
::MoveCursorLeft(bool expandSelection
)
9077 return DoMoveCursor(expandSelection
,
9078 wxGridBackwardOperations(this, wxGridColumnOperations()));
9081 bool wxGrid
::MoveCursorRight(bool expandSelection
)
9083 return DoMoveCursor(expandSelection
,
9084 wxGridForwardOperations(this, wxGridColumnOperations()));
9087 bool wxGrid
::DoMoveCursorByPage(const wxGridDirectionOperations
& diroper
)
9089 if ( m_currentCellCoords
== wxGridNoCellCoords
)
9092 if ( diroper
.IsAtBoundary(m_currentCellCoords
) )
9095 const int oldRow
= m_currentCellCoords
.GetRow();
9096 int newRow
= diroper
.MoveByPixelDistance(oldRow
, m_gridWin
->GetClientSize().y
);
9097 if ( newRow
== oldRow
)
9099 wxGridCellCoords
coords(m_currentCellCoords
);
9100 diroper
.Advance(coords
);
9101 newRow
= coords
.GetRow();
9104 GoToCell(newRow
, m_currentCellCoords
.GetCol());
9109 bool wxGrid
::MovePageUp()
9111 return DoMoveCursorByPage(
9112 wxGridBackwardOperations(this, wxGridRowOperations()));
9115 bool wxGrid
::MovePageDown()
9117 return DoMoveCursorByPage(
9118 wxGridForwardOperations(this, wxGridRowOperations()));
9121 // helper of DoMoveCursorByBlock(): advance the cell coordinates using diroper
9122 // until we find a non-empty cell or reach the grid end
9124 wxGrid
::AdvanceToNextNonEmpty(wxGridCellCoords
& coords
,
9125 const wxGridDirectionOperations
& diroper
)
9127 while ( !diroper
.IsAtBoundary(coords
) )
9129 diroper
.Advance(coords
);
9130 if ( !m_table
->IsEmpty(coords
) )
9136 wxGrid
::DoMoveCursorByBlock(bool expandSelection
,
9137 const wxGridDirectionOperations
& diroper
)
9139 if ( !m_table
|| m_currentCellCoords
== wxGridNoCellCoords
)
9142 if ( diroper
.IsAtBoundary(m_currentCellCoords
) )
9145 wxGridCellCoords
coords(m_currentCellCoords
);
9146 if ( m_table
->IsEmpty(coords
) )
9148 // we are in an empty cell: find the next block of non-empty cells
9149 AdvanceToNextNonEmpty(coords
, diroper
);
9151 else // current cell is not empty
9153 diroper
.Advance(coords
);
9154 if ( m_table
->IsEmpty(coords
) )
9156 // we started at the end of a block, find the next one
9157 AdvanceToNextNonEmpty(coords
, diroper
);
9159 else // we're in a middle of a block
9161 // go to the end of it, i.e. find the last cell before the next
9163 while ( !diroper
.IsAtBoundary(coords
) )
9165 wxGridCellCoords
coordsNext(coords
);
9166 diroper
.Advance(coordsNext
);
9167 if ( m_table
->IsEmpty(coordsNext
) )
9170 coords
= coordsNext
;
9175 if ( expandSelection
)
9177 UpdateBlockBeingSelected(m_currentCellCoords
, coords
);
9188 bool wxGrid
::MoveCursorUpBlock(bool expandSelection
)
9190 return DoMoveCursorByBlock(
9192 wxGridBackwardOperations(this, wxGridRowOperations())
9196 bool wxGrid
::MoveCursorDownBlock( bool expandSelection
)
9198 return DoMoveCursorByBlock(
9200 wxGridForwardOperations(this, wxGridRowOperations())
9204 bool wxGrid
::MoveCursorLeftBlock( bool expandSelection
)
9206 return DoMoveCursorByBlock(
9208 wxGridBackwardOperations(this, wxGridColumnOperations())
9212 bool wxGrid
::MoveCursorRightBlock( bool expandSelection
)
9214 return DoMoveCursorByBlock(
9216 wxGridForwardOperations(this, wxGridColumnOperations())
9221 // ------ Label values and formatting
9224 void wxGrid
::GetRowLabelAlignment( int *horiz
, int *vert
) const
9227 *horiz
= m_rowLabelHorizAlign
;
9229 *vert
= m_rowLabelVertAlign
;
9232 void wxGrid
::GetColLabelAlignment( int *horiz
, int *vert
) const
9235 *horiz
= m_colLabelHorizAlign
;
9237 *vert
= m_colLabelVertAlign
;
9240 int wxGrid
::GetColLabelTextOrientation() const
9242 return m_colLabelTextOrientation
;
9245 wxString wxGrid
::GetRowLabelValue( int row
) const
9249 return m_table
->GetRowLabelValue( row
);
9259 wxString wxGrid
::GetColLabelValue( int col
) const
9263 return m_table
->GetColLabelValue( col
);
9273 void wxGrid
::SetRowLabelSize( int width
)
9275 wxASSERT( width
>= 0 || width
== wxGRID_AUTOSIZE
);
9277 if ( width
== wxGRID_AUTOSIZE
)
9279 width
= CalcColOrRowLabelAreaMinSize(wxGRID_ROW
);
9282 if ( width
!= m_rowLabelWidth
)
9286 m_rowLabelWin
->Show( false );
9287 m_cornerLabelWin
->Show( false );
9289 else if ( m_rowLabelWidth
== 0 )
9291 m_rowLabelWin
->Show( true );
9292 if ( m_colLabelHeight
> 0 )
9293 m_cornerLabelWin
->Show( true );
9296 m_rowLabelWidth
= width
;
9298 wxScrolledWindow
::Refresh( true );
9302 void wxGrid
::SetColLabelSize( int height
)
9304 wxASSERT( height
>=0 || height
== wxGRID_AUTOSIZE
);
9306 if ( height
== wxGRID_AUTOSIZE
)
9308 height
= CalcColOrRowLabelAreaMinSize(wxGRID_COLUMN
);
9311 if ( height
!= m_colLabelHeight
)
9315 m_colWindow
->Show( false );
9316 m_cornerLabelWin
->Show( false );
9318 else if ( m_colLabelHeight
== 0 )
9320 m_colWindow
->Show( true );
9321 if ( m_rowLabelWidth
> 0 )
9322 m_cornerLabelWin
->Show( true );
9325 m_colLabelHeight
= height
;
9327 wxScrolledWindow
::Refresh( true );
9331 void wxGrid
::SetLabelBackgroundColour( const wxColour
& colour
)
9333 if ( m_labelBackgroundColour
!= colour
)
9335 m_labelBackgroundColour
= colour
;
9336 m_rowLabelWin
->SetBackgroundColour( colour
);
9337 m_colWindow
->SetBackgroundColour( colour
);
9338 m_cornerLabelWin
->SetBackgroundColour( colour
);
9340 if ( !GetBatchCount() )
9342 m_rowLabelWin
->Refresh();
9343 m_colWindow
->Refresh();
9344 m_cornerLabelWin
->Refresh();
9349 void wxGrid
::SetLabelTextColour( const wxColour
& colour
)
9351 if ( m_labelTextColour
!= colour
)
9353 m_labelTextColour
= colour
;
9354 if ( !GetBatchCount() )
9356 m_rowLabelWin
->Refresh();
9357 m_colWindow
->Refresh();
9362 void wxGrid
::SetLabelFont( const wxFont
& font
)
9365 if ( !GetBatchCount() )
9367 m_rowLabelWin
->Refresh();
9368 m_colWindow
->Refresh();
9372 void wxGrid
::SetRowLabelAlignment( int horiz
, int vert
)
9374 // allow old (incorrect) defs to be used
9377 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
9378 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
9379 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
9384 case wxTOP
: vert
= wxALIGN_TOP
; break;
9385 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
9386 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
9389 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
9391 m_rowLabelHorizAlign
= horiz
;
9394 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
9396 m_rowLabelVertAlign
= vert
;
9399 if ( !GetBatchCount() )
9401 m_rowLabelWin
->Refresh();
9405 void wxGrid
::SetColLabelAlignment( int horiz
, int vert
)
9407 // allow old (incorrect) defs to be used
9410 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
9411 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
9412 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
9417 case wxTOP
: vert
= wxALIGN_TOP
; break;
9418 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
9419 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
9422 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
9424 m_colLabelHorizAlign
= horiz
;
9427 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
9429 m_colLabelVertAlign
= vert
;
9432 if ( !GetBatchCount() )
9434 m_colWindow
->Refresh();
9438 // Note: under MSW, the default column label font must be changed because it
9439 // does not support vertical printing
9441 // Example: wxFont font(9, wxSWISS, wxNORMAL, wxBOLD);
9442 // pGrid->SetLabelFont(font);
9443 // pGrid->SetColLabelTextOrientation(wxVERTICAL);
9445 void wxGrid
::SetColLabelTextOrientation( int textOrientation
)
9447 if ( textOrientation
== wxHORIZONTAL
|| textOrientation
== wxVERTICAL
)
9448 m_colLabelTextOrientation
= textOrientation
;
9450 if ( !GetBatchCount() )
9451 m_colWindow
->Refresh();
9454 void wxGrid
::SetRowLabelValue( int row
, const wxString
& s
)
9458 m_table
->SetRowLabelValue( row
, s
);
9459 if ( !GetBatchCount() )
9461 wxRect rect
= CellToRect( row
, 0 );
9462 if ( rect
.height
> 0 )
9464 CalcScrolledPosition(0, rect
.y
, &rect
.x
, &rect
.y
);
9466 rect
.width
= m_rowLabelWidth
;
9467 m_rowLabelWin
->Refresh( true, &rect
);
9473 void wxGrid
::SetColLabelValue( int col
, const wxString
& s
)
9477 m_table
->SetColLabelValue( col
, s
);
9478 if ( !GetBatchCount() )
9480 if ( m_useNativeHeader
)
9482 GetColHeader()->UpdateColumn(col
);
9486 wxRect rect
= CellToRect( 0, col
);
9487 if ( rect
.width
> 0 )
9489 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &rect
.y
);
9491 rect
.height
= m_colLabelHeight
;
9492 GetColLabelWindow()->Refresh( true, &rect
);
9499 void wxGrid
::SetGridLineColour( const wxColour
& colour
)
9501 if ( m_gridLineColour
!= colour
)
9503 m_gridLineColour
= colour
;
9505 if ( GridLinesEnabled() )
9510 void wxGrid
::SetCellHighlightColour( const wxColour
& colour
)
9512 if ( m_cellHighlightColour
!= colour
)
9514 m_cellHighlightColour
= colour
;
9516 wxClientDC
dc( m_gridWin
);
9518 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
9519 DrawCellHighlight(dc
, attr
);
9524 void wxGrid
::SetCellHighlightPenWidth(int width
)
9526 if (m_cellHighlightPenWidth
!= width
)
9528 m_cellHighlightPenWidth
= width
;
9530 // Just redrawing the cell highlight is not enough since that won't
9531 // make any visible change if the the thickness is getting smaller.
9532 int row
= m_currentCellCoords
.GetRow();
9533 int col
= m_currentCellCoords
.GetCol();
9534 if ( row
== -1 || col
== -1 || GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
9537 wxRect rect
= CellToRect(row
, col
);
9538 m_gridWin
->Refresh(true, &rect
);
9542 void wxGrid
::SetCellHighlightROPenWidth(int width
)
9544 if (m_cellHighlightROPenWidth
!= width
)
9546 m_cellHighlightROPenWidth
= width
;
9548 // Just redrawing the cell highlight is not enough since that won't
9549 // make any visible change if the the thickness is getting smaller.
9550 int row
= m_currentCellCoords
.GetRow();
9551 int col
= m_currentCellCoords
.GetCol();
9552 if ( row
== -1 || col
== -1 ||
9553 GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
9556 wxRect rect
= CellToRect(row
, col
);
9557 m_gridWin
->Refresh(true, &rect
);
9561 void wxGrid
::RedrawGridLines()
9563 // the lines will be redrawn when the window is thawn
9564 if ( GetBatchCount() )
9567 if ( GridLinesEnabled() )
9569 wxClientDC
dc( m_gridWin
);
9571 DrawAllGridLines( dc
, wxRegion() );
9573 else // remove the grid lines
9575 m_gridWin
->Refresh();
9579 void wxGrid
::EnableGridLines( bool enable
)
9581 if ( enable
!= m_gridLinesEnabled
)
9583 m_gridLinesEnabled
= enable
;
9589 void wxGrid
::DoClipGridLines(bool& var
, bool clip
)
9595 if ( GridLinesEnabled() )
9600 int wxGrid
::GetDefaultRowSize() const
9602 return m_defaultRowHeight
;
9605 int wxGrid
::GetRowSize( int row
) const
9607 wxCHECK_MSG( row
>= 0 && row
< m_numRows
, 0, _T("invalid row index") );
9609 return GetRowHeight(row
);
9612 int wxGrid
::GetDefaultColSize() const
9614 return m_defaultColWidth
;
9617 int wxGrid
::GetColSize( int col
) const
9619 wxCHECK_MSG( col
>= 0 && col
< m_numCols
, 0, _T("invalid column index") );
9621 return GetColWidth(col
);
9624 // ============================================================================
9625 // access to the grid attributes: each of them has a default value in the grid
9626 // itself and may be overidden on a per-cell basis
9627 // ============================================================================
9629 // ----------------------------------------------------------------------------
9630 // setting default attributes
9631 // ----------------------------------------------------------------------------
9633 void wxGrid
::SetDefaultCellBackgroundColour( const wxColour
& col
)
9635 m_defaultCellAttr
->SetBackgroundColour(col
);
9637 m_gridWin
->SetBackgroundColour(col
);
9641 void wxGrid
::SetDefaultCellTextColour( const wxColour
& col
)
9643 m_defaultCellAttr
->SetTextColour(col
);
9646 void wxGrid
::SetDefaultCellAlignment( int horiz
, int vert
)
9648 m_defaultCellAttr
->SetAlignment(horiz
, vert
);
9651 void wxGrid
::SetDefaultCellOverflow( bool allow
)
9653 m_defaultCellAttr
->SetOverflow(allow
);
9656 void wxGrid
::SetDefaultCellFont( const wxFont
& font
)
9658 m_defaultCellAttr
->SetFont(font
);
9661 // For editors and renderers the type registry takes precedence over the
9662 // default attr, so we need to register the new editor/renderer for the string
9663 // data type in order to make setting a default editor/renderer appear to
9666 void wxGrid
::SetDefaultRenderer(wxGridCellRenderer
*renderer
)
9668 RegisterDataType(wxGRID_VALUE_STRING
,
9670 GetDefaultEditorForType(wxGRID_VALUE_STRING
));
9673 void wxGrid
::SetDefaultEditor(wxGridCellEditor
*editor
)
9675 RegisterDataType(wxGRID_VALUE_STRING
,
9676 GetDefaultRendererForType(wxGRID_VALUE_STRING
),
9680 // ----------------------------------------------------------------------------
9681 // access to the default attributes
9682 // ----------------------------------------------------------------------------
9684 wxColour wxGrid
::GetDefaultCellBackgroundColour() const
9686 return m_defaultCellAttr
->GetBackgroundColour();
9689 wxColour wxGrid
::GetDefaultCellTextColour() const
9691 return m_defaultCellAttr
->GetTextColour();
9694 wxFont wxGrid
::GetDefaultCellFont() const
9696 return m_defaultCellAttr
->GetFont();
9699 void wxGrid
::GetDefaultCellAlignment( int *horiz
, int *vert
) const
9701 m_defaultCellAttr
->GetAlignment(horiz
, vert
);
9704 bool wxGrid
::GetDefaultCellOverflow() const
9706 return m_defaultCellAttr
->GetOverflow();
9709 wxGridCellRenderer
*wxGrid
::GetDefaultRenderer() const
9711 return m_defaultCellAttr
->GetRenderer(NULL
, 0, 0);
9714 wxGridCellEditor
*wxGrid
::GetDefaultEditor() const
9716 return m_defaultCellAttr
->GetEditor(NULL
, 0, 0);
9719 // ----------------------------------------------------------------------------
9720 // access to cell attributes
9721 // ----------------------------------------------------------------------------
9723 wxColour wxGrid
::GetCellBackgroundColour(int row
, int col
) const
9725 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9726 wxColour colour
= attr
->GetBackgroundColour();
9732 wxColour wxGrid
::GetCellTextColour( int row
, int col
) const
9734 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9735 wxColour colour
= attr
->GetTextColour();
9741 wxFont wxGrid
::GetCellFont( int row
, int col
) const
9743 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9744 wxFont font
= attr
->GetFont();
9750 void wxGrid
::GetCellAlignment( int row
, int col
, int *horiz
, int *vert
) const
9752 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9753 attr
->GetAlignment(horiz
, vert
);
9757 bool wxGrid
::GetCellOverflow( int row
, int col
) const
9759 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9760 bool allow
= attr
->GetOverflow();
9766 void wxGrid
::GetCellSize( int row
, int col
, int *num_rows
, int *num_cols
) const
9768 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9769 attr
->GetSize( num_rows
, num_cols
);
9773 wxGridCellRenderer
* wxGrid
::GetCellRenderer(int row
, int col
) const
9775 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9776 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
9782 wxGridCellEditor
* wxGrid
::GetCellEditor(int row
, int col
) const
9784 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9785 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
9791 bool wxGrid
::IsReadOnly(int row
, int col
) const
9793 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9794 bool isReadOnly
= attr
->IsReadOnly();
9800 // ----------------------------------------------------------------------------
9801 // attribute support: cache, automatic provider creation, ...
9802 // ----------------------------------------------------------------------------
9804 bool wxGrid
::CanHaveAttributes() const
9811 return m_table
->CanHaveAttributes();
9814 void wxGrid
::ClearAttrCache()
9816 if ( m_attrCache
.row
!= -1 )
9818 wxGridCellAttr
*oldAttr
= m_attrCache
.attr
;
9819 m_attrCache
.attr
= NULL
;
9820 m_attrCache
.row
= -1;
9821 // wxSafeDecRec(...) might cause event processing that accesses
9822 // the cached attribute, if one exists (e.g. by deleting the
9823 // editor stored within the attribute). Therefore it is important
9824 // to invalidate the cache before calling wxSafeDecRef!
9825 wxSafeDecRef(oldAttr
);
9829 void wxGrid
::CacheAttr(int row
, int col
, wxGridCellAttr
*attr
) const
9833 wxGrid
*self
= (wxGrid
*)this; // const_cast
9835 self
->ClearAttrCache();
9836 self
->m_attrCache
.row
= row
;
9837 self
->m_attrCache
.col
= col
;
9838 self
->m_attrCache
.attr
= attr
;
9843 bool wxGrid
::LookupAttr(int row
, int col
, wxGridCellAttr
**attr
) const
9845 if ( row
== m_attrCache
.row
&& col
== m_attrCache
.col
)
9847 *attr
= m_attrCache
.attr
;
9848 wxSafeIncRef(m_attrCache
.attr
);
9850 #ifdef DEBUG_ATTR_CACHE
9851 gs_nAttrCacheHits
++;
9858 #ifdef DEBUG_ATTR_CACHE
9859 gs_nAttrCacheMisses
++;
9866 wxGridCellAttr
*wxGrid
::GetCellAttr(int row
, int col
) const
9868 wxGridCellAttr
*attr
= NULL
;
9869 // Additional test to avoid looking at the cache e.g. for
9870 // wxNoCellCoords, as this will confuse memory management.
9873 if ( !LookupAttr(row
, col
, &attr
) )
9875 attr
= m_table ? m_table
->GetAttr(row
, col
, wxGridCellAttr
::Any
)
9877 CacheAttr(row
, col
, attr
);
9883 attr
->SetDefAttr(m_defaultCellAttr
);
9887 attr
= m_defaultCellAttr
;
9894 wxGridCellAttr
*wxGrid
::GetOrCreateCellAttr(int row
, int col
) const
9896 wxGridCellAttr
*attr
= NULL
;
9897 bool canHave
= ((wxGrid
*)this)->CanHaveAttributes();
9899 wxCHECK_MSG( canHave
, attr
, _T("Cell attributes not allowed"));
9900 wxCHECK_MSG( m_table
, attr
, _T("must have a table") );
9902 attr
= m_table
->GetAttr(row
, col
, wxGridCellAttr
::Cell
);
9905 attr
= new wxGridCellAttr(m_defaultCellAttr
);
9907 // artificially inc the ref count to match DecRef() in caller
9909 m_table
->SetAttr(attr
, row
, col
);
9915 // ----------------------------------------------------------------------------
9916 // setting column attributes (wrappers around SetColAttr)
9917 // ----------------------------------------------------------------------------
9919 void wxGrid
::SetColFormatBool(int col
)
9921 SetColFormatCustom(col
, wxGRID_VALUE_BOOL
);
9924 void wxGrid
::SetColFormatNumber(int col
)
9926 SetColFormatCustom(col
, wxGRID_VALUE_NUMBER
);
9929 void wxGrid
::SetColFormatFloat(int col
, int width
, int precision
)
9931 wxString typeName
= wxGRID_VALUE_FLOAT
;
9932 if ( (width
!= -1) || (precision
!= -1) )
9934 typeName
<< _T(':') << width
<< _T(',') << precision
;
9937 SetColFormatCustom(col
, typeName
);
9940 void wxGrid
::SetColFormatCustom(int col
, const wxString
& typeName
)
9942 wxGridCellAttr
*attr
= m_table
->GetAttr(-1, col
, wxGridCellAttr
::Col
);
9944 attr
= new wxGridCellAttr
;
9945 wxGridCellRenderer
*renderer
= GetDefaultRendererForType(typeName
);
9946 attr
->SetRenderer(renderer
);
9947 wxGridCellEditor
*editor
= GetDefaultEditorForType(typeName
);
9948 attr
->SetEditor(editor
);
9950 SetColAttr(col
, attr
);
9954 // ----------------------------------------------------------------------------
9955 // setting cell attributes: this is forwarded to the table
9956 // ----------------------------------------------------------------------------
9958 void wxGrid
::SetAttr(int row
, int col
, wxGridCellAttr
*attr
)
9960 if ( CanHaveAttributes() )
9962 m_table
->SetAttr(attr
, row
, col
);
9971 void wxGrid
::SetRowAttr(int row
, wxGridCellAttr
*attr
)
9973 if ( CanHaveAttributes() )
9975 m_table
->SetRowAttr(attr
, row
);
9984 void wxGrid
::SetColAttr(int col
, wxGridCellAttr
*attr
)
9986 if ( CanHaveAttributes() )
9988 m_table
->SetColAttr(attr
, col
);
9997 void wxGrid
::SetCellBackgroundColour( int row
, int col
, const wxColour
& colour
)
9999 if ( CanHaveAttributes() )
10001 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10002 attr
->SetBackgroundColour(colour
);
10007 void wxGrid
::SetCellTextColour( int row
, int col
, const wxColour
& colour
)
10009 if ( CanHaveAttributes() )
10011 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10012 attr
->SetTextColour(colour
);
10017 void wxGrid
::SetCellFont( int row
, int col
, const wxFont
& font
)
10019 if ( CanHaveAttributes() )
10021 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10022 attr
->SetFont(font
);
10027 void wxGrid
::SetCellAlignment( int row
, int col
, int horiz
, int vert
)
10029 if ( CanHaveAttributes() )
10031 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10032 attr
->SetAlignment(horiz
, vert
);
10037 void wxGrid
::SetCellOverflow( int row
, int col
, bool allow
)
10039 if ( CanHaveAttributes() )
10041 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10042 attr
->SetOverflow(allow
);
10047 void wxGrid
::SetCellSize( int row
, int col
, int num_rows
, int num_cols
)
10049 if ( CanHaveAttributes() )
10051 int cell_rows
, cell_cols
;
10053 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10054 attr
->GetSize(&cell_rows
, &cell_cols
);
10055 attr
->SetSize(num_rows
, num_cols
);
10058 // Cannot set the size of a cell to 0 or negative values
10059 // While it is perfectly legal to do that, this function cannot
10060 // handle all the possibilies, do it by hand by getting the CellAttr.
10061 // You can only set the size of a cell to 1,1 or greater with this fn
10062 wxASSERT_MSG( !((cell_rows
< 1) || (cell_cols
< 1)),
10063 wxT("wxGrid::SetCellSize setting cell size that is already part of another cell"));
10064 wxASSERT_MSG( !((num_rows
< 1) || (num_cols
< 1)),
10065 wxT("wxGrid::SetCellSize setting cell size to < 1"));
10067 // if this was already a multicell then "turn off" the other cells first
10068 if ((cell_rows
> 1) || (cell_cols
> 1))
10071 for (j
=row
; j
< row
+ cell_rows
; j
++)
10073 for (i
=col
; i
< col
+ cell_cols
; i
++)
10075 if ((i
!= col
) || (j
!= row
))
10077 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
10078 attr_stub
->SetSize( 1, 1 );
10079 attr_stub
->DecRef();
10085 // mark the cells that will be covered by this cell to
10086 // negative or zero values to point back at this cell
10087 if (((num_rows
> 1) || (num_cols
> 1)) && (num_rows
>= 1) && (num_cols
>= 1))
10090 for (j
=row
; j
< row
+ num_rows
; j
++)
10092 for (i
=col
; i
< col
+ num_cols
; i
++)
10094 if ((i
!= col
) || (j
!= row
))
10096 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
10097 attr_stub
->SetSize( row
- j
, col
- i
);
10098 attr_stub
->DecRef();
10106 void wxGrid
::SetCellRenderer(int row
, int col
, wxGridCellRenderer
*renderer
)
10108 if ( CanHaveAttributes() )
10110 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10111 attr
->SetRenderer(renderer
);
10116 void wxGrid
::SetCellEditor(int row
, int col
, wxGridCellEditor
* editor
)
10118 if ( CanHaveAttributes() )
10120 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10121 attr
->SetEditor(editor
);
10126 void wxGrid
::SetReadOnly(int row
, int col
, bool isReadOnly
)
10128 if ( CanHaveAttributes() )
10130 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10131 attr
->SetReadOnly(isReadOnly
);
10136 // ----------------------------------------------------------------------------
10137 // Data type registration
10138 // ----------------------------------------------------------------------------
10140 void wxGrid
::RegisterDataType(const wxString
& typeName
,
10141 wxGridCellRenderer
* renderer
,
10142 wxGridCellEditor
* editor
)
10144 m_typeRegistry
->RegisterDataType(typeName
, renderer
, editor
);
10148 wxGridCellEditor
* wxGrid
::GetDefaultEditorForCell(int row
, int col
) const
10150 wxString typeName
= m_table
->GetTypeName(row
, col
);
10151 return GetDefaultEditorForType(typeName
);
10154 wxGridCellRenderer
* wxGrid
::GetDefaultRendererForCell(int row
, int col
) const
10156 wxString typeName
= m_table
->GetTypeName(row
, col
);
10157 return GetDefaultRendererForType(typeName
);
10160 wxGridCellEditor
* wxGrid
::GetDefaultEditorForType(const wxString
& typeName
) const
10162 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
10163 if ( index
== wxNOT_FOUND
)
10165 wxFAIL_MSG(wxString
::Format(wxT("Unknown data type name [%s]"), typeName
.c_str()));
10170 return m_typeRegistry
->GetEditor(index
);
10173 wxGridCellRenderer
* wxGrid
::GetDefaultRendererForType(const wxString
& typeName
) const
10175 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
10176 if ( index
== wxNOT_FOUND
)
10178 wxFAIL_MSG(wxString
::Format(wxT("Unknown data type name [%s]"), typeName
.c_str()));
10183 return m_typeRegistry
->GetRenderer(index
);
10186 // ----------------------------------------------------------------------------
10188 // ----------------------------------------------------------------------------
10190 void wxGrid
::EnableDragRowSize( bool enable
)
10192 m_canDragRowSize
= enable
;
10195 void wxGrid
::EnableDragColSize( bool enable
)
10197 m_canDragColSize
= enable
;
10200 void wxGrid
::EnableDragGridSize( bool enable
)
10202 m_canDragGridSize
= enable
;
10205 void wxGrid
::EnableDragCell( bool enable
)
10207 m_canDragCell
= enable
;
10210 void wxGrid
::SetDefaultRowSize( int height
, bool resizeExistingRows
)
10212 m_defaultRowHeight
= wxMax( height
, m_minAcceptableRowHeight
);
10214 if ( resizeExistingRows
)
10216 // since we are resizing all rows to the default row size,
10217 // we can simply clear the row heights and row bottoms
10218 // arrays (which also allows us to take advantage of
10219 // some speed optimisations)
10220 m_rowHeights
.Empty();
10221 m_rowBottoms
.Empty();
10222 if ( !GetBatchCount() )
10227 void wxGrid
::SetRowSize( int row
, int height
)
10229 wxCHECK_RET( row
>= 0 && row
< m_numRows
, _T("invalid row index") );
10231 // if < 0 then calculate new height from label
10235 wxArrayString lines
;
10236 wxClientDC
dc(m_rowLabelWin
);
10237 dc
.SetFont(GetLabelFont());
10238 StringToLines(GetRowLabelValue( row
), lines
);
10239 GetTextBoxSize( dc
, lines
, &w
, &h
);
10240 //check that it is not less than the minimal height
10241 height
= wxMax(h
, GetRowMinimalAcceptableHeight());
10244 // See comment in SetColSize
10245 if ( height
< GetRowMinimalAcceptableHeight())
10248 if ( m_rowHeights
.IsEmpty() )
10250 // need to really create the array
10254 int h
= wxMax( 0, height
);
10255 int diff
= h
- m_rowHeights
[row
];
10257 m_rowHeights
[row
] = h
;
10258 for ( int i
= row
; i
< m_numRows
; i
++ )
10260 m_rowBottoms
[i
] += diff
;
10263 if ( !GetBatchCount() )
10267 void wxGrid
::SetDefaultColSize( int width
, bool resizeExistingCols
)
10269 // we dont allow zero default column width
10270 m_defaultColWidth
= wxMax( wxMax( width
, m_minAcceptableColWidth
), 1 );
10272 if ( resizeExistingCols
)
10274 // since we are resizing all columns to the default column size,
10275 // we can simply clear the col widths and col rights
10276 // arrays (which also allows us to take advantage of
10277 // some speed optimisations)
10278 m_colWidths
.Empty();
10279 m_colRights
.Empty();
10280 if ( !GetBatchCount() )
10285 void wxGrid
::SetColSize( int col
, int width
)
10287 wxCHECK_RET( col
>= 0 && col
< m_numCols
, _T("invalid column index") );
10289 // if < 0 then calculate new width from label
10293 wxArrayString lines
;
10294 wxClientDC
dc(m_colWindow
);
10295 dc
.SetFont(GetLabelFont());
10296 StringToLines(GetColLabelValue(col
), lines
);
10297 if ( GetColLabelTextOrientation() == wxHORIZONTAL
)
10298 GetTextBoxSize( dc
, lines
, &w
, &h
);
10300 GetTextBoxSize( dc
, lines
, &h
, &w
);
10302 //check that it is not less than the minimal width
10303 width
= wxMax(width
, GetColMinimalAcceptableWidth());
10306 // should we check that it's bigger than GetColMinimalWidth(col) here?
10308 // No, because it is reasonable to assume the library user know's
10309 // what he is doing. However we should test against the weaker
10310 // constraint of minimalAcceptableWidth, as this breaks rendering
10312 // This test then fixes sf.net bug #645734
10314 if ( width
< GetColMinimalAcceptableWidth() )
10317 if ( m_colWidths
.IsEmpty() )
10319 // need to really create the array
10323 int w
= wxMax( 0, width
);
10324 int diff
= w
- m_colWidths
[col
];
10325 m_colWidths
[col
] = w
;
10327 for ( int colPos
= GetColPos(col
); colPos
< m_numCols
; colPos
++ )
10329 m_colRights
[GetColAt(colPos
)] += diff
;
10332 if ( !GetBatchCount() )
10339 void wxGrid
::SetColMinimalWidth( int col
, int width
)
10341 if (width
> GetColMinimalAcceptableWidth())
10343 wxLongToLongHashMap
::key_type key
= (wxLongToLongHashMap
::key_type
)col
;
10344 m_colMinWidths
[key
] = width
;
10348 void wxGrid
::SetRowMinimalHeight( int row
, int width
)
10350 if (width
> GetRowMinimalAcceptableHeight())
10352 wxLongToLongHashMap
::key_type key
= (wxLongToLongHashMap
::key_type
)row
;
10353 m_rowMinHeights
[key
] = width
;
10357 int wxGrid
::GetColMinimalWidth(int col
) const
10359 wxLongToLongHashMap
::key_type key
= (wxLongToLongHashMap
::key_type
)col
;
10360 wxLongToLongHashMap
::const_iterator it
= m_colMinWidths
.find(key
);
10362 return it
!= m_colMinWidths
.end() ?
(int)it
->second
: m_minAcceptableColWidth
;
10365 int wxGrid
::GetRowMinimalHeight(int row
) const
10367 wxLongToLongHashMap
::key_type key
= (wxLongToLongHashMap
::key_type
)row
;
10368 wxLongToLongHashMap
::const_iterator it
= m_rowMinHeights
.find(key
);
10370 return it
!= m_rowMinHeights
.end() ?
(int)it
->second
: m_minAcceptableRowHeight
;
10373 void wxGrid
::SetColMinimalAcceptableWidth( int width
)
10375 // We do allow a width of 0 since this gives us
10376 // an easy way to temporarily hiding columns.
10378 m_minAcceptableColWidth
= width
;
10381 void wxGrid
::SetRowMinimalAcceptableHeight( int height
)
10383 // We do allow a height of 0 since this gives us
10384 // an easy way to temporarily hiding rows.
10386 m_minAcceptableRowHeight
= height
;
10389 int wxGrid
::GetColMinimalAcceptableWidth() const
10391 return m_minAcceptableColWidth
;
10394 int wxGrid
::GetRowMinimalAcceptableHeight() const
10396 return m_minAcceptableRowHeight
;
10399 // ----------------------------------------------------------------------------
10401 // ----------------------------------------------------------------------------
10404 wxGrid
::AutoSizeColOrRow(int colOrRow
, bool setAsMin
, wxGridDirection direction
)
10406 const bool column
= direction
== wxGRID_COLUMN
;
10408 wxClientDC
dc(m_gridWin
);
10410 // cancel editing of cell
10411 HideCellEditControl();
10412 SaveEditControlValue();
10414 // init both of them to avoid compiler warnings, even if we only need one
10422 wxCoord extent
, extentMax
= 0;
10423 int max
= column ? m_numRows
: m_numCols
;
10424 for ( int rowOrCol
= 0; rowOrCol
< max
; rowOrCol
++ )
10431 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
10432 wxGridCellRenderer
*renderer
= attr
->GetRenderer(this, row
, col
);
10435 wxSize size
= renderer
->GetBestSize(*this, *attr
, dc
, row
, col
);
10436 extent
= column ? size
.x
: size
.y
;
10437 if ( extent
> extentMax
)
10438 extentMax
= extent
;
10440 renderer
->DecRef();
10446 // now also compare with the column label extent
10448 dc
.SetFont( GetLabelFont() );
10452 dc
.GetMultiLineTextExtent( GetColLabelValue(col
), &w
, &h
);
10453 if ( GetColLabelTextOrientation() == wxVERTICAL
)
10457 dc
.GetMultiLineTextExtent( GetRowLabelValue(row
), &w
, &h
);
10459 extent
= column ? w
: h
;
10460 if ( extent
> extentMax
)
10461 extentMax
= extent
;
10465 // empty column - give default extent (notice that if extentMax is less
10466 // than default extent but != 0, it's OK)
10467 extentMax
= column ? m_defaultColWidth
: m_defaultRowHeight
;
10472 // leave some space around text
10480 // Ensure automatic width is not less than minimal width. See the
10481 // comment in SetColSize() for explanation of why this isn't done
10482 // in SetColSize().
10484 extentMax
= wxMax(extentMax
, GetColMinimalWidth(col
));
10486 SetColSize( col
, extentMax
);
10487 if ( !GetBatchCount() )
10489 if ( m_useNativeHeader
)
10491 GetColHeader()->UpdateColumn(col
);
10496 m_gridWin
->GetClientSize( &cw
, &ch
);
10497 wxRect
rect ( CellToRect( 0, col
) );
10499 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
10500 rect
.width
= cw
- rect
.x
;
10501 rect
.height
= m_colLabelHeight
;
10502 GetColLabelWindow()->Refresh( true, &rect
);
10508 // Ensure automatic width is not less than minimal height. See the
10509 // comment in SetColSize() for explanation of why this isn't done
10510 // in SetRowSize().
10512 extentMax
= wxMax(extentMax
, GetRowMinimalHeight(row
));
10514 SetRowSize(row
, extentMax
);
10515 if ( !GetBatchCount() )
10518 m_gridWin
->GetClientSize( &cw
, &ch
);
10519 wxRect
rect( CellToRect( row
, 0 ) );
10521 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
10522 rect
.width
= m_rowLabelWidth
;
10523 rect
.height
= ch
- rect
.y
;
10524 m_rowLabelWin
->Refresh( true, &rect
);
10531 SetColMinimalWidth(col
, extentMax
);
10533 SetRowMinimalHeight(row
, extentMax
);
10537 wxCoord wxGrid
::CalcColOrRowLabelAreaMinSize(wxGridDirection direction
)
10539 // calculate size for the rows or columns?
10540 const bool calcRows
= direction
== wxGRID_ROW
;
10542 wxClientDC
dc(calcRows ?
GetGridRowLabelWindow()
10543 : GetGridColLabelWindow());
10544 dc
.SetFont(GetLabelFont());
10546 // which dimension should we take into account for calculations?
10548 // for columns, the text can be only horizontal so it's easy but for rows
10549 // we also have to take into account the text orientation
10551 useWidth
= calcRows
|| (GetColLabelTextOrientation() == wxVERTICAL
);
10553 wxArrayString lines
;
10554 wxCoord extentMax
= 0;
10556 const int numRowsOrCols
= calcRows ? m_numRows
: m_numCols
;
10557 for ( int rowOrCol
= 0; rowOrCol
< numRowsOrCols
; rowOrCol
++ )
10561 wxString label
= calcRows ?
GetRowLabelValue(rowOrCol
)
10562 : GetColLabelValue(rowOrCol
);
10563 StringToLines(label
, lines
);
10566 GetTextBoxSize(dc
, lines
, &w
, &h
);
10568 const wxCoord extent
= useWidth ? w
: h
;
10569 if ( extent
> extentMax
)
10570 extentMax
= extent
;
10575 // empty column - give default extent (notice that if extentMax is less
10576 // than default extent but != 0, it's OK)
10577 extentMax
= calcRows ?
GetDefaultRowLabelSize()
10578 : GetDefaultColLabelSize();
10581 // leave some space around text (taken from AutoSizeColOrRow)
10590 int wxGrid
::SetOrCalcColumnSizes(bool calcOnly
, bool setAsMin
)
10592 int width
= m_rowLabelWidth
;
10594 wxGridUpdateLocker locker
;
10596 locker
.Create(this);
10598 for ( int col
= 0; col
< m_numCols
; col
++ )
10601 AutoSizeColumn(col
, setAsMin
);
10603 width
+= GetColWidth(col
);
10609 int wxGrid
::SetOrCalcRowSizes(bool calcOnly
, bool setAsMin
)
10611 int height
= m_colLabelHeight
;
10613 wxGridUpdateLocker locker
;
10615 locker
.Create(this);
10617 for ( int row
= 0; row
< m_numRows
; row
++ )
10620 AutoSizeRow(row
, setAsMin
);
10622 height
+= GetRowHeight(row
);
10628 void wxGrid
::AutoSize()
10630 wxGridUpdateLocker
locker(this);
10632 wxSize
size(SetOrCalcColumnSizes(false) - m_rowLabelWidth
+ m_extraWidth
,
10633 SetOrCalcRowSizes(false) - m_colLabelHeight
+ m_extraHeight
);
10635 // we know that we're not going to have scrollbars so disable them now to
10636 // avoid trouble in SetClientSize() which can otherwise set the correct
10637 // client size but also leave space for (not needed any more) scrollbars
10638 SetScrollbars(0, 0, 0, 0, 0, 0, true);
10640 // restore the scroll rate parameters overwritten by SetScrollbars()
10641 SetScrollRate(m_scrollLineX
, m_scrollLineY
);
10643 SetClientSize(size
.x
+ m_rowLabelWidth
, size
.y
+ m_colLabelHeight
);
10646 void wxGrid
::AutoSizeRowLabelSize( int row
)
10648 // Hide the edit control, so it
10649 // won't interfere with drag-shrinking.
10650 if ( IsCellEditControlShown() )
10652 HideCellEditControl();
10653 SaveEditControlValue();
10656 // autosize row height depending on label text
10657 SetRowSize(row
, -1);
10661 void wxGrid
::AutoSizeColLabelSize( int col
)
10663 // Hide the edit control, so it
10664 // won't interfere with drag-shrinking.
10665 if ( IsCellEditControlShown() )
10667 HideCellEditControl();
10668 SaveEditControlValue();
10671 // autosize column width depending on label text
10672 SetColSize(col
, -1);
10676 wxSize wxGrid
::DoGetBestSize() const
10678 wxGrid
*self
= (wxGrid
*)this; // const_cast
10680 // we do the same as in AutoSize() here with the exception that we don't
10681 // change the column/row sizes, only calculate them
10682 wxSize
size(self
->SetOrCalcColumnSizes(true) - m_rowLabelWidth
+ m_extraWidth
,
10683 self
->SetOrCalcRowSizes(true) - m_colLabelHeight
+ m_extraHeight
);
10685 // NOTE: This size should be cached, but first we need to add calls to
10686 // InvalidateBestSize everywhere that could change the results of this
10688 // CacheBestSize(size);
10690 return wxSize(size
.x
+ m_rowLabelWidth
, size
.y
+ m_colLabelHeight
)
10691 + GetWindowBorderSize();
10699 wxPen
& wxGrid
::GetDividerPen() const
10704 // ----------------------------------------------------------------------------
10705 // cell value accessor functions
10706 // ----------------------------------------------------------------------------
10708 void wxGrid
::SetCellValue( int row
, int col
, const wxString
& s
)
10712 m_table
->SetValue( row
, col
, s
);
10713 if ( !GetBatchCount() )
10716 wxRect
rect( CellToRect( row
, col
) );
10718 rect
.width
= m_gridWin
->GetClientSize().GetWidth();
10719 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
10720 m_gridWin
->Refresh( false, &rect
);
10723 if ( m_currentCellCoords
.GetRow() == row
&&
10724 m_currentCellCoords
.GetCol() == col
&&
10725 IsCellEditControlShown())
10726 // Note: If we are using IsCellEditControlEnabled,
10727 // this interacts badly with calling SetCellValue from
10728 // an EVT_GRID_CELL_CHANGE handler.
10730 HideCellEditControl();
10731 ShowCellEditControl(); // will reread data from table
10736 // ----------------------------------------------------------------------------
10737 // block, row and column selection
10738 // ----------------------------------------------------------------------------
10740 void wxGrid
::SelectRow( int row
, bool addToSelected
)
10742 if ( !m_selection
)
10745 if ( !addToSelected
)
10748 m_selection
->SelectRow(row
);
10751 void wxGrid
::SelectCol( int col
, bool addToSelected
)
10753 if ( !m_selection
)
10756 if ( !addToSelected
)
10759 m_selection
->SelectCol(col
);
10762 void wxGrid
::SelectBlock(int topRow
, int leftCol
, int bottomRow
, int rightCol
,
10763 bool addToSelected
)
10765 if ( !m_selection
)
10768 if ( !addToSelected
)
10771 m_selection
->SelectBlock(topRow
, leftCol
, bottomRow
, rightCol
);
10774 void wxGrid
::SelectAll()
10776 if ( m_numRows
> 0 && m_numCols
> 0 )
10779 m_selection
->SelectBlock( 0, 0, m_numRows
- 1, m_numCols
- 1 );
10783 // ----------------------------------------------------------------------------
10784 // cell, row and col deselection
10785 // ----------------------------------------------------------------------------
10787 void wxGrid
::DeselectLine(int line
, const wxGridOperations
& oper
)
10789 if ( !m_selection
)
10792 const wxGridSelectionModes mode
= m_selection
->GetSelectionMode();
10793 if ( mode
== oper
.GetSelectionMode() )
10795 const wxGridCellCoords
c(oper
.MakeCoords(line
, 0));
10796 if ( m_selection
->IsInSelection(c
) )
10797 m_selection
->ToggleCellSelection(c
);
10799 else if ( mode
!= oper
.Dual().GetSelectionMode() )
10801 const int nOther
= oper
.Dual().GetNumberOfLines(this);
10802 for ( int i
= 0; i
< nOther
; i
++ )
10804 const wxGridCellCoords
c(oper
.MakeCoords(line
, i
));
10805 if ( m_selection
->IsInSelection(c
) )
10806 m_selection
->ToggleCellSelection(c
);
10809 //else: can only select orthogonal lines so no lines in this direction
10810 // could have been selected anyhow
10813 void wxGrid
::DeselectRow(int row
)
10815 DeselectLine(row
, wxGridRowOperations());
10818 void wxGrid
::DeselectCol(int col
)
10820 DeselectLine(col
, wxGridColumnOperations());
10823 void wxGrid
::DeselectCell( int row
, int col
)
10825 if ( m_selection
&& m_selection
->IsInSelection(row
, col
) )
10826 m_selection
->ToggleCellSelection(row
, col
);
10829 bool wxGrid
::IsSelection() const
10831 return ( m_selection
&& (m_selection
->IsSelection() ||
10832 ( m_selectedBlockTopLeft
!= wxGridNoCellCoords
&&
10833 m_selectedBlockBottomRight
!= wxGridNoCellCoords
) ) );
10836 bool wxGrid
::IsInSelection( int row
, int col
) const
10838 return ( m_selection
&& (m_selection
->IsInSelection( row
, col
) ||
10839 ( row
>= m_selectedBlockTopLeft
.GetRow() &&
10840 col
>= m_selectedBlockTopLeft
.GetCol() &&
10841 row
<= m_selectedBlockBottomRight
.GetRow() &&
10842 col
<= m_selectedBlockBottomRight
.GetCol() )) );
10845 wxGridCellCoordsArray wxGrid
::GetSelectedCells() const
10849 wxGridCellCoordsArray a
;
10853 return m_selection
->m_cellSelection
;
10856 wxGridCellCoordsArray wxGrid
::GetSelectionBlockTopLeft() const
10860 wxGridCellCoordsArray a
;
10864 return m_selection
->m_blockSelectionTopLeft
;
10867 wxGridCellCoordsArray wxGrid
::GetSelectionBlockBottomRight() const
10871 wxGridCellCoordsArray a
;
10875 return m_selection
->m_blockSelectionBottomRight
;
10878 wxArrayInt wxGrid
::GetSelectedRows() const
10886 return m_selection
->m_rowSelection
;
10889 wxArrayInt wxGrid
::GetSelectedCols() const
10897 return m_selection
->m_colSelection
;
10900 void wxGrid
::ClearSelection()
10902 wxRect r1
= BlockToDeviceRect(m_selectedBlockTopLeft
,
10903 m_selectedBlockBottomRight
);
10904 wxRect r2
= BlockToDeviceRect(m_currentCellCoords
,
10905 m_selectedBlockCorner
);
10907 m_selectedBlockTopLeft
=
10908 m_selectedBlockBottomRight
=
10909 m_selectedBlockCorner
= wxGridNoCellCoords
;
10911 Refresh( false, &r1
);
10912 Refresh( false, &r2
);
10915 m_selection
->ClearSelection();
10918 // This function returns the rectangle that encloses the given block
10919 // in device coords clipped to the client size of the grid window.
10921 wxRect wxGrid
::BlockToDeviceRect( const wxGridCellCoords
& topLeft
,
10922 const wxGridCellCoords
& bottomRight
) const
10925 wxRect tempCellRect
= CellToRect(topLeft
);
10926 if ( tempCellRect
!= wxGridNoCellRect
)
10928 resultRect
= tempCellRect
;
10932 resultRect
= wxRect(0, 0, 0, 0);
10935 tempCellRect
= CellToRect(bottomRight
);
10936 if ( tempCellRect
!= wxGridNoCellRect
)
10938 resultRect
+= tempCellRect
;
10942 // If both inputs were "wxGridNoCellRect," then there's nothing to do.
10943 return wxGridNoCellRect
;
10946 // Ensure that left/right and top/bottom pairs are in order.
10947 int left
= resultRect
.GetLeft();
10948 int top
= resultRect
.GetTop();
10949 int right
= resultRect
.GetRight();
10950 int bottom
= resultRect
.GetBottom();
10952 int leftCol
= topLeft
.GetCol();
10953 int topRow
= topLeft
.GetRow();
10954 int rightCol
= bottomRight
.GetCol();
10955 int bottomRow
= bottomRight
.GetRow();
10964 leftCol
= rightCol
;
10975 topRow
= bottomRow
;
10979 // The following loop is ONLY necessary to detect and handle merged cells.
10981 m_gridWin
->GetClientSize( &cw
, &ch
);
10983 // Get the origin coordinates: notice that they will be negative if the
10984 // grid is scrolled downwards/to the right.
10985 int gridOriginX
= 0;
10986 int gridOriginY
= 0;
10987 CalcScrolledPosition(gridOriginX
, gridOriginY
, &gridOriginX
, &gridOriginY
);
10989 int onScreenLeftmostCol
= internalXToCol(-gridOriginX
);
10990 int onScreenUppermostRow
= internalYToRow(-gridOriginY
);
10992 int onScreenRightmostCol
= internalXToCol(-gridOriginX
+ cw
);
10993 int onScreenBottommostRow
= internalYToRow(-gridOriginY
+ ch
);
10995 // Bound our loop so that we only examine the portion of the selected block
10996 // that is shown on screen. Therefore, we compare the Top-Left block values
10997 // to the Top-Left screen values, and the Bottom-Right block values to the
10998 // Bottom-Right screen values, choosing appropriately.
10999 const int visibleTopRow
= wxMax(topRow
, onScreenUppermostRow
);
11000 const int visibleBottomRow
= wxMin(bottomRow
, onScreenBottommostRow
);
11001 const int visibleLeftCol
= wxMax(leftCol
, onScreenLeftmostCol
);
11002 const int visibleRightCol
= wxMin(rightCol
, onScreenRightmostCol
);
11004 for ( int j
= visibleTopRow
; j
<= visibleBottomRow
; j
++ )
11006 for ( int i
= visibleLeftCol
; i
<= visibleRightCol
; i
++ )
11008 if ( (j
== visibleTopRow
) || (j
== visibleBottomRow
) ||
11009 (i
== visibleLeftCol
) || (i
== visibleRightCol
) )
11011 tempCellRect
= CellToRect( j
, i
);
11013 if (tempCellRect
.x
< left
)
11014 left
= tempCellRect
.x
;
11015 if (tempCellRect
.y
< top
)
11016 top
= tempCellRect
.y
;
11017 if (tempCellRect
.x
+ tempCellRect
.width
> right
)
11018 right
= tempCellRect
.x
+ tempCellRect
.width
;
11019 if (tempCellRect
.y
+ tempCellRect
.height
> bottom
)
11020 bottom
= tempCellRect
.y
+ tempCellRect
.height
;
11024 i
= visibleRightCol
; // jump over inner cells.
11029 // Convert to scrolled coords
11030 CalcScrolledPosition( left
, top
, &left
, &top
);
11031 CalcScrolledPosition( right
, bottom
, &right
, &bottom
);
11033 if (right
< 0 || bottom
< 0 || left
> cw
|| top
> ch
)
11034 return wxRect(0,0,0,0);
11036 resultRect
.SetLeft( wxMax(0, left
) );
11037 resultRect
.SetTop( wxMax(0, top
) );
11038 resultRect
.SetRight( wxMin(cw
, right
) );
11039 resultRect
.SetBottom( wxMin(ch
, bottom
) );
11044 // ----------------------------------------------------------------------------
11046 // ----------------------------------------------------------------------------
11048 #if wxUSE_DRAG_AND_DROP
11050 // this allow setting drop target directly on wxGrid
11051 void wxGrid
::SetDropTarget(wxDropTarget
*dropTarget
)
11053 GetGridWindow()->SetDropTarget(dropTarget
);
11056 #endif // wxUSE_DRAG_AND_DROP
11058 // ----------------------------------------------------------------------------
11059 // grid event classes
11060 // ----------------------------------------------------------------------------
11062 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent
, wxNotifyEvent
)
11064 wxGridEvent
::wxGridEvent( int id
, wxEventType type
, wxObject
* obj
,
11065 int row
, int col
, int x
, int y
, bool sel
,
11066 bool control
, bool shift
, bool alt
, bool meta
)
11067 : wxNotifyEvent( type
, id
),
11068 wxKeyboardState(control
, shift
, alt
, meta
)
11070 Init(row
, col
, x
, y
, sel
);
11072 SetEventObject(obj
);
11075 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent
, wxNotifyEvent
)
11077 wxGridSizeEvent
::wxGridSizeEvent( int id
, wxEventType type
, wxObject
* obj
,
11078 int rowOrCol
, int x
, int y
,
11079 bool control
, bool shift
, bool alt
, bool meta
)
11080 : wxNotifyEvent( type
, id
),
11081 wxKeyboardState(control
, shift
, alt
, meta
)
11083 Init(rowOrCol
, x
, y
);
11085 SetEventObject(obj
);
11089 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent
, wxNotifyEvent
)
11091 wxGridRangeSelectEvent
::wxGridRangeSelectEvent(int id
, wxEventType type
, wxObject
* obj
,
11092 const wxGridCellCoords
& topLeft
,
11093 const wxGridCellCoords
& bottomRight
,
11094 bool sel
, bool control
,
11095 bool shift
, bool alt
, bool meta
)
11096 : wxNotifyEvent( type
, id
),
11097 wxKeyboardState(control
, shift
, alt
, meta
)
11099 Init(topLeft
, bottomRight
, sel
);
11101 SetEventObject(obj
);
11105 IMPLEMENT_DYNAMIC_CLASS(wxGridEditorCreatedEvent
, wxCommandEvent
)
11107 wxGridEditorCreatedEvent
::wxGridEditorCreatedEvent(int id
, wxEventType type
,
11108 wxObject
* obj
, int row
,
11109 int col
, wxControl
* ctrl
)
11110 : wxCommandEvent(type
, id
)
11112 SetEventObject(obj
);
11118 #endif // wxUSE_GRID