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_COL_SORT
)
150 DEFINE_EVENT_TYPE(wxEVT_GRID_RANGE_SELECT
)
151 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_CHANGE
)
152 DEFINE_EVENT_TYPE(wxEVT_GRID_SELECT_CELL
)
153 DEFINE_EVENT_TYPE(wxEVT_GRID_EDITOR_SHOWN
)
154 DEFINE_EVENT_TYPE(wxEVT_GRID_EDITOR_HIDDEN
)
155 DEFINE_EVENT_TYPE(wxEVT_GRID_EDITOR_CREATED
)
157 // ----------------------------------------------------------------------------
159 // ----------------------------------------------------------------------------
161 // header column providing access to the column information stored in wxGrid
162 // via wxHeaderColumn interface
163 class wxGridHeaderColumn
: public wxHeaderColumn
166 wxGridHeaderColumn(wxGrid
*grid
, int col
)
172 virtual wxString
GetTitle() const { return m_grid
->GetColLabelValue(m_col
); }
173 virtual wxBitmap
GetBitmap() const { return wxNullBitmap
; }
174 virtual int GetWidth() const { return m_grid
->GetColSize(m_col
); }
175 virtual int GetMinWidth() const { return 0; }
176 virtual wxAlignment
GetAlignment() const
180 m_grid
->GetColLabelAlignment(&horz
, &vert
);
182 return static_cast<wxAlignment
>(horz
);
185 virtual int GetFlags() const
187 // we can't know in advance whether we can sort by this column or not
188 // with wxGrid API so suppose we can by default
189 int flags
= wxCOL_SORTABLE
;
190 if ( m_grid
->CanDragColSize() )
191 flags
|= wxCOL_RESIZABLE
;
192 if ( m_grid
->CanDragColMove() )
193 flags
|= wxCOL_REORDERABLE
;
194 if ( GetWidth() == 0 )
195 flags
|= wxCOL_HIDDEN
;
200 virtual bool IsSortKey() const
202 return m_grid
->IsSortingBy(m_col
);
205 virtual bool IsSortOrderAscending() const
207 return m_grid
->IsSortOrderAscending();
211 // these really should be const but are not because the column needs to be
212 // assignable to be used in a wxVector (in STL build, in non-STL build we
213 // avoid the need for this)
218 // header control retreiving column information from the grid
219 class wxGridHeaderCtrl
: public wxHeaderCtrl
222 wxGridHeaderCtrl(wxGrid
*owner
)
223 : wxHeaderCtrl(owner
,
228 (owner
->CanDragColMove() ? wxHD_ALLOW_REORDER
: 0))
233 virtual wxHeaderColumn
& GetColumn(unsigned int idx
)
235 return m_columns
[idx
];
239 wxGrid
*GetOwner() const { return static_cast<wxGrid
*>(GetParent()); }
241 // override the base class method to update our m_columns array
242 virtual void OnColumnCountChanging(unsigned int count
)
244 const unsigned countOld
= m_columns
.size();
245 if ( count
< countOld
)
247 // just discard the columns which don't exist any more (notice that
248 // we can't use resize() here as it would require the vector
249 // value_type, i.e. wxGridHeaderColumn to be default constructible,
251 m_columns
.erase(m_columns
.begin() + count
, m_columns
.end());
253 else // new columns added
255 // add columns for the new elements
256 for ( unsigned n
= countOld
; n
< count
; n
++ )
257 m_columns
.push_back(wxGridHeaderColumn(GetOwner(), n
));
261 // override to implement column auto sizing
262 virtual bool UpdateColumnWidthToFit(unsigned int idx
, int widthTitle
)
264 // TODO: currently grid doesn't support computing the column best width
265 // from its contents so we just use the best label width as is
266 GetOwner()->SetColSize(idx
, widthTitle
);
271 // overridden to react to the actions using the columns popup menu
272 virtual void UpdateColumnVisibility(unsigned int idx
, bool show
)
274 GetOwner()->SetColSize(idx
, show
? wxGRID_AUTOSIZE
: 0);
276 // as this is done by the user we should notify the main program about
278 GetOwner()->SendEvent(wxEVT_GRID_COL_SIZE
, -1, idx
);
282 // event handlers forwarding wxHeaderCtrl events to wxGrid
283 void OnClick(wxHeaderCtrlEvent
& event
)
285 GetOwner()->DoColHeaderClick(event
.GetColumn());
288 void OnBeginResize(wxHeaderCtrlEvent
& event
)
290 GetOwner()->DoStartResizeCol(event
.GetColumn());
295 void OnResizing(wxHeaderCtrlEvent
& event
)
297 GetOwner()->DoUpdateResizeColWidth(event
.GetWidth());
300 void OnEndResize(wxHeaderCtrlEvent
& event
)
302 GetOwner()->DoEndDragResizeCol();
307 void OnBeginReorder(wxHeaderCtrlEvent
& event
)
309 GetOwner()->DoStartMoveCol(event
.GetColumn());
312 void OnEndReorder(wxHeaderCtrlEvent
& event
)
314 GetOwner()->DoEndMoveCol(event
.GetNewOrder());
317 wxVector
<wxGridHeaderColumn
> m_columns
;
319 DECLARE_EVENT_TABLE()
320 DECLARE_NO_COPY_CLASS(wxGridHeaderCtrl
)
323 BEGIN_EVENT_TABLE(wxGridHeaderCtrl
, wxHeaderCtrl
)
324 EVT_HEADER_CLICK(wxID_ANY
, wxGridHeaderCtrl::OnClick
)
326 EVT_HEADER_BEGIN_RESIZE(wxID_ANY
, wxGridHeaderCtrl::OnBeginResize
)
327 EVT_HEADER_RESIZING(wxID_ANY
, wxGridHeaderCtrl::OnResizing
)
328 EVT_HEADER_END_RESIZE(wxID_ANY
, wxGridHeaderCtrl::OnEndResize
)
330 EVT_HEADER_BEGIN_REORDER(wxID_ANY
, wxGridHeaderCtrl::OnBeginReorder
)
331 EVT_HEADER_END_REORDER(wxID_ANY
, wxGridHeaderCtrl::OnEndReorder
)
334 // common base class for various grid subwindows
335 class WXDLLIMPEXP_ADV wxGridSubwindow
: public wxWindow
338 wxGridSubwindow(wxGrid
*owner
,
339 int additionalStyle
= 0,
340 const wxString
& name
= wxPanelNameStr
)
341 : wxWindow(owner
, wxID_ANY
,
342 wxDefaultPosition
, wxDefaultSize
,
343 wxBORDER_NONE
| additionalStyle
,
349 virtual bool AcceptsFocus() const { return false; }
351 wxGrid
*GetOwner() { return m_owner
; }
354 void OnMouseCaptureLost(wxMouseCaptureLostEvent
& event
);
358 DECLARE_EVENT_TABLE()
359 DECLARE_NO_COPY_CLASS(wxGridSubwindow
)
362 class WXDLLIMPEXP_ADV wxGridRowLabelWindow
: public wxGridSubwindow
365 wxGridRowLabelWindow(wxGrid
*parent
)
366 : wxGridSubwindow(parent
)
372 void OnPaint( wxPaintEvent
& event
);
373 void OnMouseEvent( wxMouseEvent
& event
);
374 void OnMouseWheel( wxMouseEvent
& event
);
376 DECLARE_EVENT_TABLE()
377 DECLARE_NO_COPY_CLASS(wxGridRowLabelWindow
)
381 class WXDLLIMPEXP_ADV wxGridColLabelWindow
: public wxGridSubwindow
384 wxGridColLabelWindow(wxGrid
*parent
)
385 : wxGridSubwindow(parent
)
391 void OnPaint( wxPaintEvent
& event
);
392 void OnMouseEvent( wxMouseEvent
& event
);
393 void OnMouseWheel( wxMouseEvent
& event
);
395 DECLARE_EVENT_TABLE()
396 DECLARE_NO_COPY_CLASS(wxGridColLabelWindow
)
400 class WXDLLIMPEXP_ADV wxGridCornerLabelWindow
: public wxGridSubwindow
403 wxGridCornerLabelWindow(wxGrid
*parent
)
404 : wxGridSubwindow(parent
)
409 void OnMouseEvent( wxMouseEvent
& event
);
410 void OnMouseWheel( wxMouseEvent
& event
);
411 void OnPaint( wxPaintEvent
& event
);
413 DECLARE_EVENT_TABLE()
414 DECLARE_NO_COPY_CLASS(wxGridCornerLabelWindow
)
417 class WXDLLIMPEXP_ADV wxGridWindow
: public wxGridSubwindow
420 wxGridWindow(wxGrid
*parent
)
421 : wxGridSubwindow(parent
,
422 wxWANTS_CHARS
| wxCLIP_CHILDREN
,
428 virtual void ScrollWindow( int dx
, int dy
, const wxRect
*rect
);
430 virtual bool AcceptsFocus() const { return true; }
433 void OnPaint( wxPaintEvent
&event
);
434 void OnMouseWheel( wxMouseEvent
& event
);
435 void OnMouseEvent( wxMouseEvent
& event
);
436 void OnKeyDown( wxKeyEvent
& );
437 void OnKeyUp( wxKeyEvent
& );
438 void OnChar( wxKeyEvent
& );
439 void OnEraseBackground( wxEraseEvent
& );
440 void OnFocus( wxFocusEvent
& );
442 DECLARE_EVENT_TABLE()
443 DECLARE_NO_COPY_CLASS(wxGridWindow
)
447 class wxGridCellEditorEvtHandler
: public wxEvtHandler
450 wxGridCellEditorEvtHandler(wxGrid
* grid
, wxGridCellEditor
* editor
)
457 void OnKillFocus(wxFocusEvent
& event
);
458 void OnKeyDown(wxKeyEvent
& event
);
459 void OnChar(wxKeyEvent
& event
);
461 void SetInSetFocus(bool inSetFocus
) { m_inSetFocus
= inSetFocus
; }
465 wxGridCellEditor
*m_editor
;
467 // Work around the fact that a focus kill event can be sent to
468 // a combobox within a set focus event.
471 DECLARE_EVENT_TABLE()
472 DECLARE_DYNAMIC_CLASS(wxGridCellEditorEvtHandler
)
473 DECLARE_NO_COPY_CLASS(wxGridCellEditorEvtHandler
)
477 IMPLEMENT_ABSTRACT_CLASS(wxGridCellEditorEvtHandler
, wxEvtHandler
)
479 BEGIN_EVENT_TABLE( wxGridCellEditorEvtHandler
, wxEvtHandler
)
480 EVT_KILL_FOCUS( wxGridCellEditorEvtHandler::OnKillFocus
)
481 EVT_KEY_DOWN( wxGridCellEditorEvtHandler::OnKeyDown
)
482 EVT_CHAR( wxGridCellEditorEvtHandler::OnChar
)
486 // ----------------------------------------------------------------------------
487 // the internal data representation used by wxGridCellAttrProvider
488 // ----------------------------------------------------------------------------
490 // this class stores attributes set for cells
491 class WXDLLIMPEXP_ADV wxGridCellAttrData
494 void SetAttr(wxGridCellAttr
*attr
, int row
, int col
);
495 wxGridCellAttr
*GetAttr(int row
, int col
) const;
496 void UpdateAttrRows( size_t pos
, int numRows
);
497 void UpdateAttrCols( size_t pos
, int numCols
);
500 // searches for the attr for given cell, returns wxNOT_FOUND if not found
501 int FindIndex(int row
, int col
) const;
503 wxGridCellWithAttrArray m_attrs
;
506 // this class stores attributes set for rows or columns
507 class WXDLLIMPEXP_ADV wxGridRowOrColAttrData
510 // empty ctor to suppress warnings
511 wxGridRowOrColAttrData() {}
512 ~wxGridRowOrColAttrData();
514 void SetAttr(wxGridCellAttr
*attr
, int rowOrCol
);
515 wxGridCellAttr
*GetAttr(int rowOrCol
) const;
516 void UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
);
519 wxArrayInt m_rowsOrCols
;
520 wxArrayAttrs m_attrs
;
523 // NB: this is just a wrapper around 3 objects: one which stores cell
524 // attributes, and 2 others for row/col ones
525 class WXDLLIMPEXP_ADV wxGridCellAttrProviderData
528 wxGridCellAttrData m_cellAttrs
;
529 wxGridRowOrColAttrData m_rowAttrs
,
534 // ----------------------------------------------------------------------------
535 // data structures used for the data type registry
536 // ----------------------------------------------------------------------------
538 struct wxGridDataTypeInfo
540 wxGridDataTypeInfo(const wxString
& typeName
,
541 wxGridCellRenderer
* renderer
,
542 wxGridCellEditor
* editor
)
543 : m_typeName(typeName
), m_renderer(renderer
), m_editor(editor
)
546 ~wxGridDataTypeInfo()
548 wxSafeDecRef(m_renderer
);
549 wxSafeDecRef(m_editor
);
553 wxGridCellRenderer
* m_renderer
;
554 wxGridCellEditor
* m_editor
;
556 DECLARE_NO_COPY_CLASS(wxGridDataTypeInfo
)
560 WX_DEFINE_ARRAY_WITH_DECL_PTR(wxGridDataTypeInfo
*, wxGridDataTypeInfoArray
,
561 class WXDLLIMPEXP_ADV
);
564 class WXDLLIMPEXP_ADV wxGridTypeRegistry
567 wxGridTypeRegistry() {}
568 ~wxGridTypeRegistry();
570 void RegisterDataType(const wxString
& typeName
,
571 wxGridCellRenderer
* renderer
,
572 wxGridCellEditor
* editor
);
574 // find one of already registered data types
575 int FindRegisteredDataType(const wxString
& typeName
);
577 // try to FindRegisteredDataType(), if this fails and typeName is one of
578 // standard typenames, register it and return its index
579 int FindDataType(const wxString
& typeName
);
581 // try to FindDataType(), if it fails see if it is not one of already
582 // registered data types with some params in which case clone the
583 // registered data type and set params for it
584 int FindOrCloneDataType(const wxString
& typeName
);
586 wxGridCellRenderer
* GetRenderer(int index
);
587 wxGridCellEditor
* GetEditor(int index
);
590 wxGridDataTypeInfoArray m_typeinfo
;
593 // ----------------------------------------------------------------------------
594 // operations classes abstracting the difference between operating on rows and
596 // ----------------------------------------------------------------------------
598 // This class allows to write a function only once because by using its methods
599 // it will apply to both columns and rows.
601 // This is an abstract interface definition, the two concrete implementations
602 // below should be used when working with rows and columns respectively.
603 class wxGridOperations
606 // Returns the operations in the other direction, i.e. wxGridRowOperations
607 // if this object is a wxGridColumnOperations and vice versa.
608 virtual wxGridOperations
& Dual() const = 0;
610 // Return the number of rows or columns.
611 virtual int GetNumberOfLines(const wxGrid
*grid
) const = 0;
613 // Return the selection mode which allows selecting rows or columns.
614 virtual wxGrid::wxGridSelectionModes
GetSelectionMode() const = 0;
616 // Make a wxGridCellCoords from the given components: thisDir is row or
617 // column and otherDir is column or row
618 virtual wxGridCellCoords
MakeCoords(int thisDir
, int otherDir
) const = 0;
620 // Calculate the scrolled position of the given abscissa or ordinate.
621 virtual int CalcScrolledPosition(wxGrid
*grid
, int pos
) const = 0;
623 // Selects the horizontal or vertical component from the given object.
624 virtual int Select(const wxGridCellCoords
& coords
) const = 0;
625 virtual int Select(const wxPoint
& pt
) const = 0;
626 virtual int Select(const wxSize
& sz
) const = 0;
627 virtual int Select(const wxRect
& r
) const = 0;
628 virtual int& Select(wxRect
& r
) const = 0;
630 // Returns width or height of the rectangle
631 virtual int& SelectSize(wxRect
& r
) const = 0;
633 // Make a wxSize such that Select() applied to it returns first component
634 virtual wxSize
MakeSize(int first
, int second
) const = 0;
636 // Sets the row or column component of the given cell coordinates
637 virtual void Set(wxGridCellCoords
& coords
, int line
) const = 0;
640 // Draws a line parallel to the row or column, i.e. horizontal or vertical:
641 // pos is the horizontal or vertical position of the line and start and end
642 // are the coordinates of the line extremities in the other direction
644 DrawParallelLine(wxDC
& dc
, int start
, int end
, int pos
) const = 0;
646 // Draw a horizontal or vertical line across the given rectangle
647 // (this is implemented in terms of above and uses Select() to extract
648 // start and end from the given rectangle)
649 void DrawParallelLineInRect(wxDC
& dc
, const wxRect
& rect
, int pos
) const
651 const int posStart
= Select(rect
.GetPosition());
652 DrawParallelLine(dc
, posStart
, posStart
+ Select(rect
.GetSize()), pos
);
656 // Return the index of the row or column at the given pixel coordinate.
658 PosToLine(const wxGrid
*grid
, int pos
, bool clip
= false) const = 0;
660 // Get the top/left position, in pixels, of the given row or column
661 virtual int GetLineStartPos(const wxGrid
*grid
, int line
) const = 0;
663 // Get the bottom/right position, in pixels, of the given row or column
664 virtual int GetLineEndPos(const wxGrid
*grid
, int line
) const = 0;
666 // Get the height/width of the given row/column
667 virtual int GetLineSize(const wxGrid
*grid
, int line
) const = 0;
669 // Get wxGrid::m_rowBottoms/m_colRights array
670 virtual const wxArrayInt
& GetLineEnds(const wxGrid
*grid
) const = 0;
672 // Get default height row height or column width
673 virtual int GetDefaultLineSize(const wxGrid
*grid
) const = 0;
675 // Return the minimal acceptable row height or column width
676 virtual int GetMinimalAcceptableLineSize(const wxGrid
*grid
) const = 0;
678 // Return the minimal row height or column width
679 virtual int GetMinimalLineSize(const wxGrid
*grid
, int line
) const = 0;
681 // Set the row height or column width
682 virtual void SetLineSize(wxGrid
*grid
, int line
, int size
) const = 0;
684 // True if rows/columns can be resized by user
685 virtual bool CanResizeLines(const wxGrid
*grid
) const = 0;
688 // Return the index of the line at the given position
690 // NB: currently this is always identity for the rows as reordering is only
691 // implemented for the lines
692 virtual int GetLineAt(const wxGrid
*grid
, int line
) const = 0;
695 // Get the row or column label window
696 virtual wxWindow
*GetHeaderWindow(wxGrid
*grid
) const = 0;
698 // Get the width or height of the row or column label window
699 virtual int GetHeaderWindowSize(wxGrid
*grid
) const = 0;
702 // This class is never used polymorphically but give it a virtual dtor
703 // anyhow to suppress g++ complaints about it
704 virtual ~wxGridOperations() { }
707 class wxGridRowOperations
: public wxGridOperations
710 virtual wxGridOperations
& Dual() const;
712 virtual int GetNumberOfLines(const wxGrid
*grid
) const
713 { return grid
->GetNumberRows(); }
715 virtual wxGrid::wxGridSelectionModes
GetSelectionMode() const
716 { return wxGrid::wxGridSelectRows
; }
718 virtual wxGridCellCoords
MakeCoords(int thisDir
, int otherDir
) const
719 { return wxGridCellCoords(thisDir
, otherDir
); }
721 virtual int CalcScrolledPosition(wxGrid
*grid
, int pos
) const
722 { return grid
->CalcScrolledPosition(wxPoint(pos
, 0)).x
; }
724 virtual int Select(const wxGridCellCoords
& c
) const { return c
.GetRow(); }
725 virtual int Select(const wxPoint
& pt
) const { return pt
.x
; }
726 virtual int Select(const wxSize
& sz
) const { return sz
.x
; }
727 virtual int Select(const wxRect
& r
) const { return r
.x
; }
728 virtual int& Select(wxRect
& r
) const { return r
.x
; }
729 virtual int& SelectSize(wxRect
& r
) const { return r
.width
; }
730 virtual wxSize
MakeSize(int first
, int second
) const
731 { return wxSize(first
, second
); }
732 virtual void Set(wxGridCellCoords
& coords
, int line
) const
733 { coords
.SetRow(line
); }
735 virtual void DrawParallelLine(wxDC
& dc
, int start
, int end
, int pos
) const
736 { dc
.DrawLine(start
, pos
, end
, pos
); }
738 virtual int PosToLine(const wxGrid
*grid
, int pos
, bool clip
= false) const
739 { return grid
->YToRow(pos
, clip
); }
740 virtual int GetLineStartPos(const wxGrid
*grid
, int line
) const
741 { return grid
->GetRowTop(line
); }
742 virtual int GetLineEndPos(const wxGrid
*grid
, int line
) const
743 { return grid
->GetRowBottom(line
); }
744 virtual int GetLineSize(const wxGrid
*grid
, int line
) const
745 { return grid
->GetRowHeight(line
); }
746 virtual const wxArrayInt
& GetLineEnds(const wxGrid
*grid
) const
747 { return grid
->m_rowBottoms
; }
748 virtual int GetDefaultLineSize(const wxGrid
*grid
) const
749 { return grid
->GetDefaultRowSize(); }
750 virtual int GetMinimalAcceptableLineSize(const wxGrid
*grid
) const
751 { return grid
->GetRowMinimalAcceptableHeight(); }
752 virtual int GetMinimalLineSize(const wxGrid
*grid
, int line
) const
753 { return grid
->GetRowMinimalHeight(line
); }
754 virtual void SetLineSize(wxGrid
*grid
, int line
, int size
) const
755 { grid
->SetRowSize(line
, size
); }
756 virtual bool CanResizeLines(const wxGrid
*grid
) const
757 { return grid
->CanDragRowSize(); }
759 virtual int GetLineAt(const wxGrid
* WXUNUSED(grid
), int line
) const
760 { return line
; } // TODO: implement row reordering
762 virtual wxWindow
*GetHeaderWindow(wxGrid
*grid
) const
763 { return grid
->GetGridRowLabelWindow(); }
764 virtual int GetHeaderWindowSize(wxGrid
*grid
) const
765 { return grid
->GetRowLabelSize(); }
768 class wxGridColumnOperations
: public wxGridOperations
771 virtual wxGridOperations
& Dual() const;
773 virtual int GetNumberOfLines(const wxGrid
*grid
) const
774 { return grid
->GetNumberCols(); }
776 virtual wxGrid::wxGridSelectionModes
GetSelectionMode() const
777 { return wxGrid::wxGridSelectColumns
; }
779 virtual wxGridCellCoords
MakeCoords(int thisDir
, int otherDir
) const
780 { return wxGridCellCoords(otherDir
, thisDir
); }
782 virtual int CalcScrolledPosition(wxGrid
*grid
, int pos
) const
783 { return grid
->CalcScrolledPosition(wxPoint(0, pos
)).y
; }
785 virtual int Select(const wxGridCellCoords
& c
) const { return c
.GetCol(); }
786 virtual int Select(const wxPoint
& pt
) const { return pt
.y
; }
787 virtual int Select(const wxSize
& sz
) const { return sz
.y
; }
788 virtual int Select(const wxRect
& r
) const { return r
.y
; }
789 virtual int& Select(wxRect
& r
) const { return r
.y
; }
790 virtual int& SelectSize(wxRect
& r
) const { return r
.height
; }
791 virtual wxSize
MakeSize(int first
, int second
) const
792 { return wxSize(second
, first
); }
793 virtual void Set(wxGridCellCoords
& coords
, int line
) const
794 { coords
.SetCol(line
); }
796 virtual void DrawParallelLine(wxDC
& dc
, int start
, int end
, int pos
) const
797 { dc
.DrawLine(pos
, start
, pos
, end
); }
799 virtual int PosToLine(const wxGrid
*grid
, int pos
, bool clip
= false) const
800 { return grid
->XToCol(pos
, clip
); }
801 virtual int GetLineStartPos(const wxGrid
*grid
, int line
) const
802 { return grid
->GetColLeft(line
); }
803 virtual int GetLineEndPos(const wxGrid
*grid
, int line
) const
804 { return grid
->GetColRight(line
); }
805 virtual int GetLineSize(const wxGrid
*grid
, int line
) const
806 { return grid
->GetColWidth(line
); }
807 virtual const wxArrayInt
& GetLineEnds(const wxGrid
*grid
) const
808 { return grid
->m_colRights
; }
809 virtual int GetDefaultLineSize(const wxGrid
*grid
) const
810 { return grid
->GetDefaultColSize(); }
811 virtual int GetMinimalAcceptableLineSize(const wxGrid
*grid
) const
812 { return grid
->GetColMinimalAcceptableWidth(); }
813 virtual int GetMinimalLineSize(const wxGrid
*grid
, int line
) const
814 { return grid
->GetColMinimalWidth(line
); }
815 virtual void SetLineSize(wxGrid
*grid
, int line
, int size
) const
816 { grid
->SetColSize(line
, size
); }
817 virtual bool CanResizeLines(const wxGrid
*grid
) const
818 { return grid
->CanDragColSize(); }
820 virtual int GetLineAt(const wxGrid
*grid
, int line
) const
821 { return grid
->GetColAt(line
); }
823 virtual wxWindow
*GetHeaderWindow(wxGrid
*grid
) const
824 { return grid
->GetGridColLabelWindow(); }
825 virtual int GetHeaderWindowSize(wxGrid
*grid
) const
826 { return grid
->GetColLabelSize(); }
829 wxGridOperations
& wxGridRowOperations::Dual() const
831 static wxGridColumnOperations s_colOper
;
836 wxGridOperations
& wxGridColumnOperations::Dual() const
838 static wxGridRowOperations s_rowOper
;
843 // This class abstracts the difference between operations going forward
844 // (down/right) and backward (up/left) and allows to use the same code for
845 // functions which differ only in the direction of grid traversal
847 // Like wxGridOperations it's an ABC with two concrete subclasses below. Unlike
848 // it, this is a normal object and not just a function dispatch table and has a
851 // Note: the explanation of this discrepancy is the existence of (very useful)
852 // Dual() method in wxGridOperations which forces us to make wxGridOperations a
853 // function dispatcher only.
854 class wxGridDirectionOperations
857 // The oper parameter to ctor selects whether we work with rows or columns
858 wxGridDirectionOperations(wxGrid
*grid
, const wxGridOperations
& oper
)
864 // Check if the component of this point in our direction is at the
865 // boundary, i.e. is the first/last row/column
866 virtual bool IsAtBoundary(const wxGridCellCoords
& coords
) const = 0;
868 // Increment the component of this point in our direction
869 virtual void Advance(wxGridCellCoords
& coords
) const = 0;
871 // Find the line at the given distance, in pixels, away from this one
872 // (this uses clipping, i.e. anything after the last line is counted as the
873 // last one and anything before the first one as 0)
874 virtual int MoveByPixelDistance(int line
, int distance
) const = 0;
876 // This class is never used polymorphically but give it a virtual dtor
877 // anyhow to suppress g++ complaints about it
878 virtual ~wxGridDirectionOperations() { }
881 wxGrid
* const m_grid
;
882 const wxGridOperations
& m_oper
;
885 class wxGridBackwardOperations
: public wxGridDirectionOperations
888 wxGridBackwardOperations(wxGrid
*grid
, const wxGridOperations
& oper
)
889 : wxGridDirectionOperations(grid
, oper
)
893 virtual bool IsAtBoundary(const wxGridCellCoords
& coords
) const
895 wxASSERT_MSG( m_oper
.Select(coords
) >= 0, "invalid row/column" );
897 return m_oper
.Select(coords
) == 0;
900 virtual void Advance(wxGridCellCoords
& coords
) const
902 wxASSERT( !IsAtBoundary(coords
) );
904 m_oper
.Set(coords
, m_oper
.Select(coords
) - 1);
907 virtual int MoveByPixelDistance(int line
, int distance
) const
909 int pos
= m_oper
.GetLineStartPos(m_grid
, line
);
910 return m_oper
.PosToLine(m_grid
, pos
- distance
+ 1, true);
914 class wxGridForwardOperations
: public wxGridDirectionOperations
917 wxGridForwardOperations(wxGrid
*grid
, const wxGridOperations
& oper
)
918 : wxGridDirectionOperations(grid
, oper
),
919 m_numLines(oper
.GetNumberOfLines(grid
))
923 virtual bool IsAtBoundary(const wxGridCellCoords
& coords
) const
925 wxASSERT_MSG( m_oper
.Select(coords
) < m_numLines
, "invalid row/column" );
927 return m_oper
.Select(coords
) == m_numLines
- 1;
930 virtual void Advance(wxGridCellCoords
& coords
) const
932 wxASSERT( !IsAtBoundary(coords
) );
934 m_oper
.Set(coords
, m_oper
.Select(coords
) + 1);
937 virtual int MoveByPixelDistance(int line
, int distance
) const
939 int pos
= m_oper
.GetLineStartPos(m_grid
, line
);
940 return m_oper
.PosToLine(m_grid
, pos
+ distance
, true);
944 const int m_numLines
;
947 // ----------------------------------------------------------------------------
949 // ----------------------------------------------------------------------------
951 //#define DEBUG_ATTR_CACHE
952 #ifdef DEBUG_ATTR_CACHE
953 static size_t gs_nAttrCacheHits
= 0;
954 static size_t gs_nAttrCacheMisses
= 0;
957 // ----------------------------------------------------------------------------
959 // ----------------------------------------------------------------------------
961 wxGridCellCoords
wxGridNoCellCoords( -1, -1 );
962 wxRect
wxGridNoCellRect( -1, -1, -1, -1 );
968 const size_t GRID_SCROLL_LINE_X
= 15;
969 const size_t GRID_SCROLL_LINE_Y
= GRID_SCROLL_LINE_X
;
971 // the size of hash tables used a bit everywhere (the max number of elements
972 // in these hash tables is the number of rows/columns)
973 const int GRID_HASH_SIZE
= 100;
975 // the minimal distance in pixels the mouse needs to move to start a drag
977 const int DRAG_SENSITIVITY
= 3;
979 } // anonymous namespace
981 // ----------------------------------------------------------------------------
983 // ----------------------------------------------------------------------------
988 // ensure that first is less or equal to second, swapping the values if
990 void EnsureFirstLessThanSecond(int& first
, int& second
)
992 if ( first
> second
)
993 wxSwap(first
, second
);
996 } // anonymous namespace
998 // ============================================================================
1000 // ============================================================================
1002 // ----------------------------------------------------------------------------
1004 // ----------------------------------------------------------------------------
1006 wxGridCellEditor::wxGridCellEditor()
1012 wxGridCellEditor::~wxGridCellEditor()
1017 void wxGridCellEditor::Create(wxWindow
* WXUNUSED(parent
),
1018 wxWindowID
WXUNUSED(id
),
1019 wxEvtHandler
* evtHandler
)
1022 m_control
->PushEventHandler(evtHandler
);
1025 void wxGridCellEditor::PaintBackground(const wxRect
& rectCell
,
1026 wxGridCellAttr
*attr
)
1028 // erase the background because we might not fill the cell
1029 wxClientDC
dc(m_control
->GetParent());
1030 wxGridWindow
* gridWindow
= wxDynamicCast(m_control
->GetParent(), wxGridWindow
);
1032 gridWindow
->GetOwner()->PrepareDC(dc
);
1034 dc
.SetPen(*wxTRANSPARENT_PEN
);
1035 dc
.SetBrush(wxBrush(attr
->GetBackgroundColour()));
1036 dc
.DrawRectangle(rectCell
);
1038 // redraw the control we just painted over
1039 m_control
->Refresh();
1042 void wxGridCellEditor::Destroy()
1046 m_control
->PopEventHandler( true /* delete it*/ );
1048 m_control
->Destroy();
1053 void wxGridCellEditor::Show(bool show
, wxGridCellAttr
*attr
)
1055 wxASSERT_MSG(m_control
, wxT("The wxGridCellEditor must be created first!"));
1057 m_control
->Show(show
);
1061 // set the colours/fonts if we have any
1064 m_colFgOld
= m_control
->GetForegroundColour();
1065 m_control
->SetForegroundColour(attr
->GetTextColour());
1067 m_colBgOld
= m_control
->GetBackgroundColour();
1068 m_control
->SetBackgroundColour(attr
->GetBackgroundColour());
1070 // Workaround for GTK+1 font setting problem on some platforms
1071 #if !defined(__WXGTK__) || defined(__WXGTK20__)
1072 m_fontOld
= m_control
->GetFont();
1073 m_control
->SetFont(attr
->GetFont());
1076 // can't do anything more in the base class version, the other
1077 // attributes may only be used by the derived classes
1082 // restore the standard colours fonts
1083 if ( m_colFgOld
.Ok() )
1085 m_control
->SetForegroundColour(m_colFgOld
);
1086 m_colFgOld
= wxNullColour
;
1089 if ( m_colBgOld
.Ok() )
1091 m_control
->SetBackgroundColour(m_colBgOld
);
1092 m_colBgOld
= wxNullColour
;
1095 // Workaround for GTK+1 font setting problem on some platforms
1096 #if !defined(__WXGTK__) || defined(__WXGTK20__)
1097 if ( m_fontOld
.Ok() )
1099 m_control
->SetFont(m_fontOld
);
1100 m_fontOld
= wxNullFont
;
1106 void wxGridCellEditor::SetSize(const wxRect
& rect
)
1108 wxASSERT_MSG(m_control
, wxT("The wxGridCellEditor must be created first!"));
1110 m_control
->SetSize(rect
, wxSIZE_ALLOW_MINUS_ONE
);
1113 void wxGridCellEditor::HandleReturn(wxKeyEvent
& event
)
1118 bool wxGridCellEditor::IsAcceptedKey(wxKeyEvent
& event
)
1120 bool ctrl
= event
.ControlDown();
1121 bool alt
= event
.AltDown();
1124 // On the Mac the Alt key is more like shift and is used for entry of
1125 // valid characters, so check for Ctrl and Meta instead.
1126 alt
= event
.MetaDown();
1129 // Assume it's not a valid char if ctrl or alt is down, but if both are
1130 // down then it may be because of an AltGr key combination, so let them
1131 // through in that case.
1132 if ((ctrl
|| alt
) && !(ctrl
&& alt
))
1136 // if the unicode key code is not really a unicode character (it may
1137 // be a function key or etc., the platforms appear to always give us a
1138 // small value in this case) then fallback to the ASCII key code but
1139 // don't do anything for function keys or etc.
1140 if ( event
.GetUnicodeKey() > 127 && event
.GetKeyCode() > 127 )
1143 if ( event
.GetKeyCode() > 255 )
1150 void wxGridCellEditor::StartingKey(wxKeyEvent
& event
)
1155 void wxGridCellEditor::StartingClick()
1161 // ----------------------------------------------------------------------------
1162 // wxGridCellTextEditor
1163 // ----------------------------------------------------------------------------
1165 wxGridCellTextEditor::wxGridCellTextEditor()
1170 void wxGridCellTextEditor::Create(wxWindow
* parent
,
1172 wxEvtHandler
* evtHandler
)
1174 DoCreate(parent
, id
, evtHandler
);
1177 void wxGridCellTextEditor::DoCreate(wxWindow
* parent
,
1179 wxEvtHandler
* evtHandler
,
1182 style
|= wxTE_PROCESS_ENTER
| wxTE_PROCESS_TAB
| wxNO_BORDER
;
1184 m_control
= new wxTextCtrl(parent
, id
, wxEmptyString
,
1185 wxDefaultPosition
, wxDefaultSize
,
1188 // set max length allowed in the textctrl, if the parameter was set
1189 if ( m_maxChars
!= 0 )
1191 Text()->SetMaxLength(m_maxChars
);
1194 wxGridCellEditor::Create(parent
, id
, evtHandler
);
1197 void wxGridCellTextEditor::PaintBackground(const wxRect
& WXUNUSED(rectCell
),
1198 wxGridCellAttr
* WXUNUSED(attr
))
1200 // as we fill the entire client area,
1201 // don't do anything here to minimize flicker
1204 void wxGridCellTextEditor::SetSize(const wxRect
& rectOrig
)
1206 wxRect
rect(rectOrig
);
1208 // Make the edit control large enough to allow for internal margins
1210 // TODO: remove this if the text ctrl sizing is improved esp. for unix
1212 #if defined(__WXGTK__)
1220 #elif defined(__WXMSW__)
1234 int extra_x
= ( rect
.x
> 2 ) ? 2 : 1;
1235 int extra_y
= ( rect
.y
> 2 ) ? 2 : 1;
1237 #if defined(__WXMOTIF__)
1242 rect
.SetLeft( wxMax(0, rect
.x
- extra_x
) );
1243 rect
.SetTop( wxMax(0, rect
.y
- extra_y
) );
1244 rect
.SetRight( rect
.GetRight() + 2 * extra_x
);
1245 rect
.SetBottom( rect
.GetBottom() + 2 * extra_y
);
1248 wxGridCellEditor::SetSize(rect
);
1251 void wxGridCellTextEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1253 wxASSERT_MSG(m_control
, wxT("The wxGridCellEditor must be created first!"));
1255 m_startValue
= grid
->GetTable()->GetValue(row
, col
);
1257 DoBeginEdit(m_startValue
);
1260 void wxGridCellTextEditor::DoBeginEdit(const wxString
& startValue
)
1262 Text()->SetValue(startValue
);
1263 Text()->SetInsertionPointEnd();
1264 Text()->SetSelection(-1, -1);
1268 bool wxGridCellTextEditor::EndEdit(int row
, int col
, wxGrid
* grid
)
1270 wxASSERT_MSG(m_control
, wxT("The wxGridCellEditor must be created first!"));
1272 bool changed
= false;
1273 wxString value
= Text()->GetValue();
1274 if (value
!= m_startValue
)
1278 grid
->GetTable()->SetValue(row
, col
, value
);
1280 m_startValue
= wxEmptyString
;
1282 // No point in setting the text of the hidden control
1283 //Text()->SetValue(m_startValue);
1288 void wxGridCellTextEditor::Reset()
1290 wxASSERT_MSG(m_control
, wxT("The wxGridCellEditor must be created first!"));
1292 DoReset(m_startValue
);
1295 void wxGridCellTextEditor::DoReset(const wxString
& startValue
)
1297 Text()->SetValue(startValue
);
1298 Text()->SetInsertionPointEnd();
1301 bool wxGridCellTextEditor::IsAcceptedKey(wxKeyEvent
& event
)
1303 return wxGridCellEditor::IsAcceptedKey(event
);
1306 void wxGridCellTextEditor::StartingKey(wxKeyEvent
& event
)
1308 // Since this is now happening in the EVT_CHAR event EmulateKeyPress is no
1309 // longer an appropriate way to get the character into the text control.
1310 // Do it ourselves instead. We know that if we get this far that we have
1311 // a valid character, so not a whole lot of testing needs to be done.
1313 wxTextCtrl
* tc
= Text();
1318 ch
= event
.GetUnicodeKey();
1320 ch
= (wxChar
)event
.GetKeyCode();
1322 ch
= (wxChar
)event
.GetKeyCode();
1328 // delete the character at the cursor
1329 pos
= tc
->GetInsertionPoint();
1330 if (pos
< tc
->GetLastPosition())
1331 tc
->Remove(pos
, pos
+ 1);
1335 // delete the character before the cursor
1336 pos
= tc
->GetInsertionPoint();
1338 tc
->Remove(pos
- 1, pos
);
1347 void wxGridCellTextEditor::HandleReturn( wxKeyEvent
&
1348 WXUNUSED_GTK(WXUNUSED_MOTIF(event
)) )
1350 #if defined(__WXMOTIF__) || defined(__WXGTK__)
1351 // wxMotif needs a little extra help...
1352 size_t pos
= (size_t)( Text()->GetInsertionPoint() );
1353 wxString
s( Text()->GetValue() );
1354 s
= s
.Left(pos
) + wxT("\n") + s
.Mid(pos
);
1355 Text()->SetValue(s
);
1356 Text()->SetInsertionPoint( pos
);
1358 // the other ports can handle a Return key press
1364 void wxGridCellTextEditor::SetParameters(const wxString
& params
)
1374 if ( params
.ToLong(&tmp
) )
1376 m_maxChars
= (size_t)tmp
;
1380 wxLogDebug( _T("Invalid wxGridCellTextEditor parameter string '%s' ignored"), params
.c_str() );
1385 // return the value in the text control
1386 wxString
wxGridCellTextEditor::GetValue() const
1388 return Text()->GetValue();
1391 // ----------------------------------------------------------------------------
1392 // wxGridCellNumberEditor
1393 // ----------------------------------------------------------------------------
1395 wxGridCellNumberEditor::wxGridCellNumberEditor(int min
, int max
)
1401 void wxGridCellNumberEditor::Create(wxWindow
* parent
,
1403 wxEvtHandler
* evtHandler
)
1408 // create a spin ctrl
1409 m_control
= new wxSpinCtrl(parent
, wxID_ANY
, wxEmptyString
,
1410 wxDefaultPosition
, wxDefaultSize
,
1414 wxGridCellEditor::Create(parent
, id
, evtHandler
);
1419 // just a text control
1420 wxGridCellTextEditor::Create(parent
, id
, evtHandler
);
1422 #if wxUSE_VALIDATORS
1423 Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC
));
1428 void wxGridCellNumberEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1430 // first get the value
1431 wxGridTableBase
*table
= grid
->GetTable();
1432 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
1434 m_valueOld
= table
->GetValueAsLong(row
, col
);
1439 wxString sValue
= table
->GetValue(row
, col
);
1440 if (! sValue
.ToLong(&m_valueOld
) && ! sValue
.empty())
1442 wxFAIL_MSG( _T("this cell doesn't have numeric value") );
1450 Spin()->SetValue((int)m_valueOld
);
1456 DoBeginEdit(GetString());
1460 bool wxGridCellNumberEditor::EndEdit(int row
, int col
,
1469 value
= Spin()->GetValue();
1470 if ( value
== m_valueOld
)
1473 text
.Printf(wxT("%ld"), value
);
1475 else // using unconstrained input
1476 #endif // wxUSE_SPINCTRL
1478 const wxString
textOld(grid
->GetCellValue(row
, col
));
1479 text
= Text()->GetValue();
1482 if ( textOld
.empty() )
1485 else // non-empty text now (maybe 0)
1487 if ( !text
.ToLong(&value
) )
1490 // if value == m_valueOld == 0 but old text was "" and new one is
1491 // "0" something still did change
1492 if ( value
== m_valueOld
&& (value
|| !textOld
.empty()) )
1497 wxGridTableBase
* const table
= grid
->GetTable();
1498 if ( table
->CanSetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
1499 table
->SetValueAsLong(row
, col
, value
);
1501 table
->SetValue(row
, col
, text
);
1506 void wxGridCellNumberEditor::Reset()
1511 Spin()->SetValue((int)m_valueOld
);
1516 DoReset(GetString());
1520 bool wxGridCellNumberEditor::IsAcceptedKey(wxKeyEvent
& event
)
1522 if ( wxGridCellEditor::IsAcceptedKey(event
) )
1524 int keycode
= event
.GetKeyCode();
1525 if ( (keycode
< 128) &&
1526 (wxIsdigit(keycode
) || keycode
== '+' || keycode
== '-'))
1535 void wxGridCellNumberEditor::StartingKey(wxKeyEvent
& event
)
1537 int keycode
= event
.GetKeyCode();
1540 if ( wxIsdigit(keycode
) || keycode
== '+' || keycode
== '-')
1542 wxGridCellTextEditor::StartingKey(event
);
1544 // skip Skip() below
1551 if ( wxIsdigit(keycode
) )
1553 wxSpinCtrl
* spin
= (wxSpinCtrl
*)m_control
;
1554 spin
->SetValue(keycode
- '0');
1555 spin
->SetSelection(1,1);
1564 void wxGridCellNumberEditor::SetParameters(const wxString
& params
)
1575 if ( params
.BeforeFirst(_T(',')).ToLong(&tmp
) )
1579 if ( params
.AfterFirst(_T(',')).ToLong(&tmp
) )
1583 // skip the error message below
1588 wxLogDebug(_T("Invalid wxGridCellNumberEditor parameter string '%s' ignored"), params
.c_str());
1592 // return the value in the spin control if it is there (the text control otherwise)
1593 wxString
wxGridCellNumberEditor::GetValue() const
1600 long value
= Spin()->GetValue();
1601 s
.Printf(wxT("%ld"), value
);
1606 s
= Text()->GetValue();
1612 // ----------------------------------------------------------------------------
1613 // wxGridCellFloatEditor
1614 // ----------------------------------------------------------------------------
1616 wxGridCellFloatEditor::wxGridCellFloatEditor(int width
, int precision
)
1619 m_precision
= precision
;
1622 void wxGridCellFloatEditor::Create(wxWindow
* parent
,
1624 wxEvtHandler
* evtHandler
)
1626 wxGridCellTextEditor::Create(parent
, id
, evtHandler
);
1628 #if wxUSE_VALIDATORS
1629 Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC
));
1633 void wxGridCellFloatEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1635 // first get the value
1636 wxGridTableBase
* const table
= grid
->GetTable();
1637 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
1639 m_valueOld
= table
->GetValueAsDouble(row
, col
);
1645 const wxString value
= table
->GetValue(row
, col
);
1646 if ( !value
.empty() )
1648 if ( !value
.ToDouble(&m_valueOld
) )
1650 wxFAIL_MSG( _T("this cell doesn't have float value") );
1656 DoBeginEdit(GetString());
1659 bool wxGridCellFloatEditor::EndEdit(int row
, int col
, wxGrid
* grid
)
1661 const wxString
text(Text()->GetValue()),
1662 textOld(grid
->GetCellValue(row
, col
));
1665 if ( !text
.empty() )
1667 if ( !text
.ToDouble(&value
) )
1670 else // new value is empty string
1672 if ( textOld
.empty() )
1673 return false; // nothing changed
1678 // the test for empty strings ensures that we don't skip the value setting
1679 // when "" is replaced by "0" or vice versa as "" numeric value is also 0.
1680 if ( wxIsSameDouble(value
, m_valueOld
) && !text
.empty() && !textOld
.empty() )
1681 return false; // nothing changed
1683 wxGridTableBase
* const table
= grid
->GetTable();
1685 if ( table
->CanSetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
1686 table
->SetValueAsDouble(row
, col
, value
);
1688 table
->SetValue(row
, col
, text
);
1693 void wxGridCellFloatEditor::Reset()
1695 DoReset(GetString());
1698 void wxGridCellFloatEditor::StartingKey(wxKeyEvent
& event
)
1700 int keycode
= event
.GetKeyCode();
1702 tmpbuf
[0] = (char) keycode
;
1704 wxString
strbuf(tmpbuf
, *wxConvCurrent
);
1707 bool is_decimal_point
= ( strbuf
==
1708 wxLocale::GetInfo(wxLOCALE_DECIMAL_POINT
, wxLOCALE_CAT_NUMBER
) );
1710 bool is_decimal_point
= ( strbuf
== _T(".") );
1713 if ( wxIsdigit(keycode
) || keycode
== '+' || keycode
== '-'
1714 || is_decimal_point
)
1716 wxGridCellTextEditor::StartingKey(event
);
1718 // skip Skip() below
1725 void wxGridCellFloatEditor::SetParameters(const wxString
& params
)
1736 if ( params
.BeforeFirst(_T(',')).ToLong(&tmp
) )
1740 if ( params
.AfterFirst(_T(',')).ToLong(&tmp
) )
1742 m_precision
= (int)tmp
;
1744 // skip the error message below
1749 wxLogDebug(_T("Invalid wxGridCellFloatEditor parameter string '%s' ignored"), params
.c_str());
1753 wxString
wxGridCellFloatEditor::GetString() const
1756 if ( m_precision
== -1 && m_width
!= -1)
1758 // default precision
1759 fmt
.Printf(_T("%%%d.f"), m_width
);
1761 else if ( m_precision
!= -1 && m_width
== -1)
1764 fmt
.Printf(_T("%%.%df"), m_precision
);
1766 else if ( m_precision
!= -1 && m_width
!= -1 )
1768 fmt
.Printf(_T("%%%d.%df"), m_width
, m_precision
);
1772 // default width/precision
1776 return wxString::Format(fmt
, m_valueOld
);
1779 bool wxGridCellFloatEditor::IsAcceptedKey(wxKeyEvent
& event
)
1781 if ( wxGridCellEditor::IsAcceptedKey(event
) )
1783 const int keycode
= event
.GetKeyCode();
1784 if ( isascii(keycode
) )
1787 tmpbuf
[0] = (char) keycode
;
1789 wxString
strbuf(tmpbuf
, *wxConvCurrent
);
1792 const wxString decimalPoint
=
1793 wxLocale::GetInfo(wxLOCALE_DECIMAL_POINT
, wxLOCALE_CAT_NUMBER
);
1795 const wxString
decimalPoint(_T('.'));
1798 // accept digits, 'e' as in '1e+6', also '-', '+', and '.'
1799 if ( wxIsdigit(keycode
) ||
1800 tolower(keycode
) == 'e' ||
1801 keycode
== decimalPoint
||
1813 #endif // wxUSE_TEXTCTRL
1817 // ----------------------------------------------------------------------------
1818 // wxGridCellBoolEditor
1819 // ----------------------------------------------------------------------------
1821 // the default values for GetValue()
1822 wxString
wxGridCellBoolEditor::ms_stringValues
[2] = { _T(""), _T("1") };
1824 void wxGridCellBoolEditor::Create(wxWindow
* parent
,
1826 wxEvtHandler
* evtHandler
)
1828 m_control
= new wxCheckBox(parent
, id
, wxEmptyString
,
1829 wxDefaultPosition
, wxDefaultSize
,
1832 wxGridCellEditor::Create(parent
, id
, evtHandler
);
1835 void wxGridCellBoolEditor::SetSize(const wxRect
& r
)
1837 bool resize
= false;
1838 wxSize size
= m_control
->GetSize();
1839 wxCoord minSize
= wxMin(r
.width
, r
.height
);
1841 // check if the checkbox is not too big/small for this cell
1842 wxSize sizeBest
= m_control
->GetBestSize();
1843 if ( !(size
== sizeBest
) )
1845 // reset to default size if it had been made smaller
1851 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
1853 // leave 1 pixel margin
1854 size
.x
= size
.y
= minSize
- 2;
1861 m_control
->SetSize(size
);
1864 // position it in the centre of the rectangle (TODO: support alignment?)
1866 #if defined(__WXGTK__) || defined (__WXMOTIF__)
1867 // the checkbox without label still has some space to the right in wxGTK,
1868 // so shift it to the right
1870 #elif defined(__WXMSW__)
1871 // here too, but in other way
1876 int hAlign
= wxALIGN_CENTRE
;
1877 int vAlign
= wxALIGN_CENTRE
;
1879 GetCellAttr()->GetAlignment(& hAlign
, & vAlign
);
1882 if (hAlign
== wxALIGN_LEFT
)
1890 y
= r
.y
+ r
.height
/ 2 - size
.y
/ 2;
1892 else if (hAlign
== wxALIGN_RIGHT
)
1894 x
= r
.x
+ r
.width
- size
.x
- 2;
1895 y
= r
.y
+ r
.height
/ 2 - size
.y
/ 2;
1897 else if (hAlign
== wxALIGN_CENTRE
)
1899 x
= r
.x
+ r
.width
/ 2 - size
.x
/ 2;
1900 y
= r
.y
+ r
.height
/ 2 - size
.y
/ 2;
1903 m_control
->Move(x
, y
);
1906 void wxGridCellBoolEditor::Show(bool show
, wxGridCellAttr
*attr
)
1908 m_control
->Show(show
);
1912 wxColour colBg
= attr
? attr
->GetBackgroundColour() : *wxLIGHT_GREY
;
1913 CBox()->SetBackgroundColour(colBg
);
1917 void wxGridCellBoolEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1919 wxASSERT_MSG(m_control
,
1920 wxT("The wxGridCellEditor must be created first!"));
1922 if (grid
->GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
))
1924 m_startValue
= grid
->GetTable()->GetValueAsBool(row
, col
);
1928 wxString
cellval( grid
->GetTable()->GetValue(row
, col
) );
1930 if ( cellval
== ms_stringValues
[false] )
1931 m_startValue
= false;
1932 else if ( cellval
== ms_stringValues
[true] )
1933 m_startValue
= true;
1936 // do not try to be smart here and convert it to true or false
1937 // because we'll still overwrite it with something different and
1938 // this risks to be very surprising for the user code, let them
1940 wxFAIL_MSG( _T("invalid value for a cell with bool editor!") );
1944 CBox()->SetValue(m_startValue
);
1948 bool wxGridCellBoolEditor::EndEdit(int row
, int col
,
1951 wxASSERT_MSG(m_control
,
1952 wxT("The wxGridCellEditor must be created first!"));
1954 bool changed
= false;
1955 bool value
= CBox()->GetValue();
1956 if ( value
!= m_startValue
)
1961 wxGridTableBase
* const table
= grid
->GetTable();
1962 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
) )
1963 table
->SetValueAsBool(row
, col
, value
);
1965 table
->SetValue(row
, col
, GetValue());
1971 void wxGridCellBoolEditor::Reset()
1973 wxASSERT_MSG(m_control
,
1974 wxT("The wxGridCellEditor must be created first!"));
1976 CBox()->SetValue(m_startValue
);
1979 void wxGridCellBoolEditor::StartingClick()
1981 CBox()->SetValue(!CBox()->GetValue());
1984 bool wxGridCellBoolEditor::IsAcceptedKey(wxKeyEvent
& event
)
1986 if ( wxGridCellEditor::IsAcceptedKey(event
) )
1988 int keycode
= event
.GetKeyCode();
2001 void wxGridCellBoolEditor::StartingKey(wxKeyEvent
& event
)
2003 int keycode
= event
.GetKeyCode();
2007 CBox()->SetValue(!CBox()->GetValue());
2011 CBox()->SetValue(true);
2015 CBox()->SetValue(false);
2020 wxString
wxGridCellBoolEditor::GetValue() const
2022 return ms_stringValues
[CBox()->GetValue()];
2026 wxGridCellBoolEditor::UseStringValues(const wxString
& valueTrue
,
2027 const wxString
& valueFalse
)
2029 ms_stringValues
[false] = valueFalse
;
2030 ms_stringValues
[true] = valueTrue
;
2034 wxGridCellBoolEditor::IsTrueValue(const wxString
& value
)
2036 return value
== ms_stringValues
[true];
2039 #endif // wxUSE_CHECKBOX
2043 // ----------------------------------------------------------------------------
2044 // wxGridCellChoiceEditor
2045 // ----------------------------------------------------------------------------
2047 wxGridCellChoiceEditor::wxGridCellChoiceEditor(const wxArrayString
& choices
,
2049 : m_choices(choices
),
2050 m_allowOthers(allowOthers
) { }
2052 wxGridCellChoiceEditor::wxGridCellChoiceEditor(size_t count
,
2053 const wxString choices
[],
2055 : m_allowOthers(allowOthers
)
2059 m_choices
.Alloc(count
);
2060 for ( size_t n
= 0; n
< count
; n
++ )
2062 m_choices
.Add(choices
[n
]);
2067 wxGridCellEditor
*wxGridCellChoiceEditor::Clone() const
2069 wxGridCellChoiceEditor
*editor
= new wxGridCellChoiceEditor
;
2070 editor
->m_allowOthers
= m_allowOthers
;
2071 editor
->m_choices
= m_choices
;
2076 void wxGridCellChoiceEditor::Create(wxWindow
* parent
,
2078 wxEvtHandler
* evtHandler
)
2080 int style
= wxTE_PROCESS_ENTER
|
2084 if ( !m_allowOthers
)
2085 style
|= wxCB_READONLY
;
2086 m_control
= new wxComboBox(parent
, id
, wxEmptyString
,
2087 wxDefaultPosition
, wxDefaultSize
,
2091 wxGridCellEditor::Create(parent
, id
, evtHandler
);
2094 void wxGridCellChoiceEditor::PaintBackground(const wxRect
& rectCell
,
2095 wxGridCellAttr
* attr
)
2097 // as we fill the entire client area, don't do anything here to minimize
2100 // TODO: It doesn't actually fill the client area since the height of a
2101 // combo always defaults to the standard. Until someone has time to
2102 // figure out the right rectangle to paint, just do it the normal way.
2103 wxGridCellEditor::PaintBackground(rectCell
, attr
);
2106 void wxGridCellChoiceEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
2108 wxASSERT_MSG(m_control
,
2109 wxT("The wxGridCellEditor must be created first!"));
2111 wxGridCellEditorEvtHandler
* evtHandler
= NULL
;
2113 evtHandler
= wxDynamicCast(m_control
->GetEventHandler(), wxGridCellEditorEvtHandler
);
2115 // Don't immediately end if we get a kill focus event within BeginEdit
2117 evtHandler
->SetInSetFocus(true);
2119 m_startValue
= grid
->GetTable()->GetValue(row
, col
);
2121 Reset(); // this updates combo box to correspond to m_startValue
2123 Combo()->SetFocus();
2127 // When dropping down the menu, a kill focus event
2128 // happens after this point, so we can't reset the flag yet.
2129 #if !defined(__WXGTK20__)
2130 evtHandler
->SetInSetFocus(false);
2135 bool wxGridCellChoiceEditor::EndEdit(int row
, int col
,
2138 wxString value
= Combo()->GetValue();
2139 if ( value
== m_startValue
)
2142 grid
->GetTable()->SetValue(row
, col
, value
);
2147 void wxGridCellChoiceEditor::Reset()
2151 Combo()->SetValue(m_startValue
);
2152 Combo()->SetInsertionPointEnd();
2154 else // the combobox is read-only
2156 // find the right position, or default to the first if not found
2157 int pos
= Combo()->FindString(m_startValue
);
2158 if (pos
== wxNOT_FOUND
)
2160 Combo()->SetSelection(pos
);
2164 void wxGridCellChoiceEditor::SetParameters(const wxString
& params
)
2174 wxStringTokenizer
tk(params
, _T(','));
2175 while ( tk
.HasMoreTokens() )
2177 m_choices
.Add(tk
.GetNextToken());
2181 // return the value in the text control
2182 wxString
wxGridCellChoiceEditor::GetValue() const
2184 return Combo()->GetValue();
2187 #endif // wxUSE_COMBOBOX
2189 // ----------------------------------------------------------------------------
2190 // wxGridCellEditorEvtHandler
2191 // ----------------------------------------------------------------------------
2193 void wxGridCellEditorEvtHandler::OnKillFocus(wxFocusEvent
& event
)
2195 // Don't disable the cell if we're just starting to edit it
2200 m_grid
->DisableCellEditControl();
2205 void wxGridCellEditorEvtHandler::OnKeyDown(wxKeyEvent
& event
)
2207 switch ( event
.GetKeyCode() )
2211 m_grid
->DisableCellEditControl();
2215 m_grid
->GetEventHandler()->ProcessEvent( event
);
2219 case WXK_NUMPAD_ENTER
:
2220 if (!m_grid
->GetEventHandler()->ProcessEvent(event
))
2221 m_editor
->HandleReturn(event
);
2230 void wxGridCellEditorEvtHandler::OnChar(wxKeyEvent
& event
)
2232 int row
= m_grid
->GetGridCursorRow();
2233 int col
= m_grid
->GetGridCursorCol();
2234 wxRect rect
= m_grid
->CellToRect( row
, col
);
2236 m_grid
->GetGridWindow()->GetClientSize( &cw
, &ch
);
2238 // if cell width is smaller than grid client area, cell is wholly visible
2239 bool wholeCellVisible
= (rect
.GetWidth() < cw
);
2241 switch ( event
.GetKeyCode() )
2246 case WXK_NUMPAD_ENTER
:
2251 if ( wholeCellVisible
)
2253 // no special processing needed...
2258 // do special processing for partly visible cell...
2260 // get the widths of all cells previous to this one
2262 for ( int i
= 0; i
< col
; i
++ )
2264 colXPos
+= m_grid
->GetColSize(i
);
2267 int xUnit
= 1, yUnit
= 1;
2268 m_grid
->GetScrollPixelsPerUnit(&xUnit
, &yUnit
);
2271 m_grid
->Scroll(colXPos
/ xUnit
- 1, m_grid
->GetScrollPos(wxVERTICAL
));
2275 m_grid
->Scroll(colXPos
/ xUnit
, m_grid
->GetScrollPos(wxVERTICAL
));
2283 if ( wholeCellVisible
)
2285 // no special processing needed...
2290 // do special processing for partly visible cell...
2293 wxString value
= m_grid
->GetCellValue(row
, col
);
2294 if ( wxEmptyString
!= value
)
2296 // get width of cell CONTENTS (text)
2298 wxFont font
= m_grid
->GetCellFont(row
, col
);
2299 m_grid
->GetTextExtent(value
, &textWidth
, &y
, NULL
, NULL
, &font
);
2301 // try to RIGHT align the text by scrolling
2302 int client_right
= m_grid
->GetGridWindow()->GetClientSize().GetWidth();
2304 // (m_grid->GetScrollLineX()*2) is a factor for not scrolling to far,
2305 // otherwise the last part of the cell content might be hidden below the scroll bar
2306 // FIXME: maybe there is a more suitable correction?
2307 textWidth
-= (client_right
- (m_grid
->GetScrollLineX() * 2));
2308 if ( textWidth
< 0 )
2314 // get the widths of all cells previous to this one
2316 for ( int i
= 0; i
< col
; i
++ )
2318 colXPos
+= m_grid
->GetColSize(i
);
2321 // and add the (modified) text width of the cell contents
2322 // as we'd like to see the last part of the cell contents
2323 colXPos
+= textWidth
;
2325 int xUnit
= 1, yUnit
= 1;
2326 m_grid
->GetScrollPixelsPerUnit(&xUnit
, &yUnit
);
2327 m_grid
->Scroll(colXPos
/ xUnit
- 1, m_grid
->GetScrollPos(wxVERTICAL
));
2338 // ----------------------------------------------------------------------------
2339 // wxGridCellWorker is an (almost) empty common base class for
2340 // wxGridCellRenderer and wxGridCellEditor managing ref counting
2341 // ----------------------------------------------------------------------------
2343 void wxGridCellWorker::SetParameters(const wxString
& WXUNUSED(params
))
2348 wxGridCellWorker::~wxGridCellWorker()
2352 // ============================================================================
2354 // ============================================================================
2356 // ----------------------------------------------------------------------------
2357 // wxGridCellRenderer
2358 // ----------------------------------------------------------------------------
2360 void wxGridCellRenderer::Draw(wxGrid
& grid
,
2361 wxGridCellAttr
& attr
,
2364 int WXUNUSED(row
), int WXUNUSED(col
),
2367 dc
.SetBackgroundMode( wxBRUSHSTYLE_SOLID
);
2370 if ( grid
.IsEnabled() )
2374 if ( grid
.HasFocus() )
2375 clr
= grid
.GetSelectionBackground();
2377 clr
= wxSystemSettings::GetColour(wxSYS_COLOUR_BTNSHADOW
);
2381 clr
= attr
.GetBackgroundColour();
2384 else // grey out fields if the grid is disabled
2386 clr
= wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
);
2390 dc
.SetPen( *wxTRANSPARENT_PEN
);
2391 dc
.DrawRectangle(rect
);
2394 // ----------------------------------------------------------------------------
2395 // wxGridCellStringRenderer
2396 // ----------------------------------------------------------------------------
2398 void wxGridCellStringRenderer::SetTextColoursAndFont(const wxGrid
& grid
,
2399 const wxGridCellAttr
& attr
,
2403 dc
.SetBackgroundMode( wxBRUSHSTYLE_TRANSPARENT
);
2405 // TODO some special colours for attr.IsReadOnly() case?
2407 // different coloured text when the grid is disabled
2408 if ( grid
.IsEnabled() )
2413 if ( grid
.HasFocus() )
2414 clr
= grid
.GetSelectionBackground();
2416 clr
= wxSystemSettings::GetColour(wxSYS_COLOUR_BTNSHADOW
);
2417 dc
.SetTextBackground( clr
);
2418 dc
.SetTextForeground( grid
.GetSelectionForeground() );
2422 dc
.SetTextBackground( attr
.GetBackgroundColour() );
2423 dc
.SetTextForeground( attr
.GetTextColour() );
2428 dc
.SetTextBackground(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
));
2429 dc
.SetTextForeground(wxSystemSettings::GetColour(wxSYS_COLOUR_GRAYTEXT
));
2432 dc
.SetFont( attr
.GetFont() );
2435 wxSize
wxGridCellStringRenderer::DoGetBestSize(const wxGridCellAttr
& attr
,
2437 const wxString
& text
)
2439 wxCoord x
= 0, y
= 0, max_x
= 0;
2440 dc
.SetFont(attr
.GetFont());
2441 wxStringTokenizer
tk(text
, _T('\n'));
2442 while ( tk
.HasMoreTokens() )
2444 dc
.GetTextExtent(tk
.GetNextToken(), &x
, &y
);
2445 max_x
= wxMax(max_x
, x
);
2448 y
*= 1 + text
.Freq(wxT('\n')); // multiply by the number of lines.
2450 return wxSize(max_x
, y
);
2453 wxSize
wxGridCellStringRenderer::GetBestSize(wxGrid
& grid
,
2454 wxGridCellAttr
& attr
,
2458 return DoGetBestSize(attr
, dc
, grid
.GetCellValue(row
, col
));
2461 void wxGridCellStringRenderer::Draw(wxGrid
& grid
,
2462 wxGridCellAttr
& attr
,
2464 const wxRect
& rectCell
,
2468 wxRect rect
= rectCell
;
2471 // erase only this cells background, overflow cells should have been erased
2472 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
2475 attr
.GetAlignment(&hAlign
, &vAlign
);
2477 int overflowCols
= 0;
2479 if (attr
.GetOverflow())
2481 int cols
= grid
.GetNumberCols();
2482 int best_width
= GetBestSize(grid
,attr
,dc
,row
,col
).GetWidth();
2483 int cell_rows
, cell_cols
;
2484 attr
.GetSize( &cell_rows
, &cell_cols
); // shouldn't get here if <= 0
2485 if ((best_width
> rectCell
.width
) && (col
< cols
) && grid
.GetTable())
2487 int i
, c_cols
, c_rows
;
2488 for (i
= col
+cell_cols
; i
< cols
; i
++)
2490 bool is_empty
= true;
2491 for (int j
=row
; j
< row
+ cell_rows
; j
++)
2493 // check w/ anchor cell for multicell block
2494 grid
.GetCellSize(j
, i
, &c_rows
, &c_cols
);
2497 if (!grid
.GetTable()->IsEmptyCell(j
+ c_rows
, i
))
2506 rect
.width
+= grid
.GetColSize(i
);
2514 if (rect
.width
>= best_width
)
2518 overflowCols
= i
- col
- cell_cols
+ 1;
2519 if (overflowCols
>= cols
)
2520 overflowCols
= cols
- 1;
2523 if (overflowCols
> 0) // redraw overflow cells w/ proper hilight
2525 hAlign
= wxALIGN_LEFT
; // if oveflowed then it's left aligned
2527 clip
.x
+= rectCell
.width
;
2528 // draw each overflow cell individually
2529 int col_end
= col
+ cell_cols
+ overflowCols
;
2530 if (col_end
>= grid
.GetNumberCols())
2531 col_end
= grid
.GetNumberCols() - 1;
2532 for (int i
= col
+ cell_cols
; i
<= col_end
; i
++)
2534 clip
.width
= grid
.GetColSize(i
) - 1;
2535 dc
.DestroyClippingRegion();
2536 dc
.SetClippingRegion(clip
);
2538 SetTextColoursAndFont(grid
, attr
, dc
,
2539 grid
.IsInSelection(row
,i
));
2541 grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
),
2542 rect
, hAlign
, vAlign
);
2543 clip
.x
+= grid
.GetColSize(i
) - 1;
2549 dc
.DestroyClippingRegion();
2553 // now we only have to draw the text
2554 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
2556 grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
),
2557 rect
, hAlign
, vAlign
);
2560 // ----------------------------------------------------------------------------
2561 // wxGridCellNumberRenderer
2562 // ----------------------------------------------------------------------------
2564 wxString
wxGridCellNumberRenderer::GetString(const wxGrid
& grid
, int row
, int col
)
2566 wxGridTableBase
*table
= grid
.GetTable();
2568 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
2570 text
.Printf(_T("%ld"), table
->GetValueAsLong(row
, col
));
2574 text
= table
->GetValue(row
, col
);
2580 void wxGridCellNumberRenderer::Draw(wxGrid
& grid
,
2581 wxGridCellAttr
& attr
,
2583 const wxRect
& rectCell
,
2587 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
2589 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
2591 // draw the text right aligned by default
2593 attr
.GetAlignment(&hAlign
, &vAlign
);
2594 hAlign
= wxALIGN_RIGHT
;
2596 wxRect rect
= rectCell
;
2599 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
2602 wxSize
wxGridCellNumberRenderer::GetBestSize(wxGrid
& grid
,
2603 wxGridCellAttr
& attr
,
2607 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
2610 // ----------------------------------------------------------------------------
2611 // wxGridCellFloatRenderer
2612 // ----------------------------------------------------------------------------
2614 wxGridCellFloatRenderer::wxGridCellFloatRenderer(int width
, int precision
)
2617 SetPrecision(precision
);
2620 wxGridCellRenderer
*wxGridCellFloatRenderer::Clone() const
2622 wxGridCellFloatRenderer
*renderer
= new wxGridCellFloatRenderer
;
2623 renderer
->m_width
= m_width
;
2624 renderer
->m_precision
= m_precision
;
2625 renderer
->m_format
= m_format
;
2630 wxString
wxGridCellFloatRenderer::GetString(const wxGrid
& grid
, int row
, int col
)
2632 wxGridTableBase
*table
= grid
.GetTable();
2637 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
2639 val
= table
->GetValueAsDouble(row
, col
);
2644 text
= table
->GetValue(row
, col
);
2645 hasDouble
= text
.ToDouble(&val
);
2652 if ( m_width
== -1 )
2654 if ( m_precision
== -1 )
2656 // default width/precision
2657 m_format
= _T("%f");
2661 m_format
.Printf(_T("%%.%df"), m_precision
);
2664 else if ( m_precision
== -1 )
2666 // default precision
2667 m_format
.Printf(_T("%%%d.f"), m_width
);
2671 m_format
.Printf(_T("%%%d.%df"), m_width
, m_precision
);
2675 text
.Printf(m_format
, val
);
2678 //else: text already contains the string
2683 void wxGridCellFloatRenderer::Draw(wxGrid
& grid
,
2684 wxGridCellAttr
& attr
,
2686 const wxRect
& rectCell
,
2690 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
2692 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
2694 // draw the text right aligned by default
2696 attr
.GetAlignment(&hAlign
, &vAlign
);
2697 hAlign
= wxALIGN_RIGHT
;
2699 wxRect rect
= rectCell
;
2702 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
2705 wxSize
wxGridCellFloatRenderer::GetBestSize(wxGrid
& grid
,
2706 wxGridCellAttr
& attr
,
2710 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
2713 void wxGridCellFloatRenderer::SetParameters(const wxString
& params
)
2717 // reset to defaults
2723 wxString tmp
= params
.BeforeFirst(_T(','));
2727 if ( tmp
.ToLong(&width
) )
2729 SetWidth((int)width
);
2733 wxLogDebug(_T("Invalid wxGridCellFloatRenderer width parameter string '%s ignored"), params
.c_str());
2737 tmp
= params
.AfterFirst(_T(','));
2741 if ( tmp
.ToLong(&precision
) )
2743 SetPrecision((int)precision
);
2747 wxLogDebug(_T("Invalid wxGridCellFloatRenderer precision parameter string '%s ignored"), params
.c_str());
2753 // ----------------------------------------------------------------------------
2754 // wxGridCellBoolRenderer
2755 // ----------------------------------------------------------------------------
2757 wxSize
wxGridCellBoolRenderer::ms_sizeCheckMark
;
2759 // FIXME these checkbox size calculations are really ugly...
2761 // between checkmark and box
2762 static const wxCoord wxGRID_CHECKMARK_MARGIN
= 2;
2764 wxSize
wxGridCellBoolRenderer::GetBestSize(wxGrid
& grid
,
2765 wxGridCellAttr
& WXUNUSED(attr
),
2770 // compute it only once (no locks for MT safeness in GUI thread...)
2771 if ( !ms_sizeCheckMark
.x
)
2773 // get checkbox size
2774 wxCheckBox
*checkbox
= new wxCheckBox(&grid
, wxID_ANY
, wxEmptyString
);
2775 wxSize size
= checkbox
->GetBestSize();
2776 wxCoord checkSize
= size
.y
+ 2 * wxGRID_CHECKMARK_MARGIN
;
2778 #if defined(__WXMOTIF__)
2779 checkSize
-= size
.y
/ 2;
2784 ms_sizeCheckMark
.x
= ms_sizeCheckMark
.y
= checkSize
;
2787 return ms_sizeCheckMark
;
2790 void wxGridCellBoolRenderer::Draw(wxGrid
& grid
,
2791 wxGridCellAttr
& attr
,
2797 wxGridCellRenderer::Draw(grid
, attr
, dc
, rect
, row
, col
, isSelected
);
2799 // draw a check mark in the centre (ignoring alignment - TODO)
2800 wxSize size
= GetBestSize(grid
, attr
, dc
, row
, col
);
2802 // don't draw outside the cell
2803 wxCoord minSize
= wxMin(rect
.width
, rect
.height
);
2804 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
2806 // and even leave (at least) 1 pixel margin
2807 size
.x
= size
.y
= minSize
;
2810 // draw a border around checkmark
2812 attr
.GetAlignment(&hAlign
, &vAlign
);
2815 if (hAlign
== wxALIGN_CENTRE
)
2817 rectBorder
.x
= rect
.x
+ rect
.width
/ 2 - size
.x
/ 2;
2818 rectBorder
.y
= rect
.y
+ rect
.height
/ 2 - size
.y
/ 2;
2819 rectBorder
.width
= size
.x
;
2820 rectBorder
.height
= size
.y
;
2822 else if (hAlign
== wxALIGN_LEFT
)
2824 rectBorder
.x
= rect
.x
+ 2;
2825 rectBorder
.y
= rect
.y
+ rect
.height
/ 2 - size
.y
/ 2;
2826 rectBorder
.width
= size
.x
;
2827 rectBorder
.height
= size
.y
;
2829 else if (hAlign
== wxALIGN_RIGHT
)
2831 rectBorder
.x
= rect
.x
+ rect
.width
- size
.x
- 2;
2832 rectBorder
.y
= rect
.y
+ rect
.height
/ 2 - size
.y
/ 2;
2833 rectBorder
.width
= size
.x
;
2834 rectBorder
.height
= size
.y
;
2838 if ( grid
.GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
) )
2840 value
= grid
.GetTable()->GetValueAsBool(row
, col
);
2844 wxString
cellval( grid
.GetTable()->GetValue(row
, col
) );
2845 value
= wxGridCellBoolEditor::IsTrueValue(cellval
);
2850 flags
|= wxCONTROL_CHECKED
;
2852 wxRendererNative::Get().DrawCheckBox( &grid
, dc
, rectBorder
, flags
);
2855 // ----------------------------------------------------------------------------
2857 // ----------------------------------------------------------------------------
2859 void wxGridCellAttr::Init(wxGridCellAttr
*attrDefault
)
2863 m_isReadOnly
= Unset
;
2868 m_attrkind
= wxGridCellAttr::Cell
;
2870 m_sizeRows
= m_sizeCols
= 1;
2871 m_overflow
= UnsetOverflow
;
2873 SetDefAttr(attrDefault
);
2876 wxGridCellAttr
*wxGridCellAttr::Clone() const
2878 wxGridCellAttr
*attr
= new wxGridCellAttr(m_defGridAttr
);
2880 if ( HasTextColour() )
2881 attr
->SetTextColour(GetTextColour());
2882 if ( HasBackgroundColour() )
2883 attr
->SetBackgroundColour(GetBackgroundColour());
2885 attr
->SetFont(GetFont());
2886 if ( HasAlignment() )
2887 attr
->SetAlignment(m_hAlign
, m_vAlign
);
2889 attr
->SetSize( m_sizeRows
, m_sizeCols
);
2893 attr
->SetRenderer(m_renderer
);
2894 m_renderer
->IncRef();
2898 attr
->SetEditor(m_editor
);
2903 attr
->SetReadOnly();
2905 attr
->SetOverflow( m_overflow
== Overflow
);
2906 attr
->SetKind( m_attrkind
);
2911 void wxGridCellAttr::MergeWith(wxGridCellAttr
*mergefrom
)
2913 if ( !HasTextColour() && mergefrom
->HasTextColour() )
2914 SetTextColour(mergefrom
->GetTextColour());
2915 if ( !HasBackgroundColour() && mergefrom
->HasBackgroundColour() )
2916 SetBackgroundColour(mergefrom
->GetBackgroundColour());
2917 if ( !HasFont() && mergefrom
->HasFont() )
2918 SetFont(mergefrom
->GetFont());
2919 if ( !HasAlignment() && mergefrom
->HasAlignment() )
2922 mergefrom
->GetAlignment( &hAlign
, &vAlign
);
2923 SetAlignment(hAlign
, vAlign
);
2925 if ( !HasSize() && mergefrom
->HasSize() )
2926 mergefrom
->GetSize( &m_sizeRows
, &m_sizeCols
);
2928 // Directly access member functions as GetRender/Editor don't just return
2929 // m_renderer/m_editor
2931 // Maybe add support for merge of Render and Editor?
2932 if (!HasRenderer() && mergefrom
->HasRenderer() )
2934 m_renderer
= mergefrom
->m_renderer
;
2935 m_renderer
->IncRef();
2937 if ( !HasEditor() && mergefrom
->HasEditor() )
2939 m_editor
= mergefrom
->m_editor
;
2942 if ( !HasReadWriteMode() && mergefrom
->HasReadWriteMode() )
2943 SetReadOnly(mergefrom
->IsReadOnly());
2945 if (!HasOverflowMode() && mergefrom
->HasOverflowMode() )
2946 SetOverflow(mergefrom
->GetOverflow());
2948 SetDefAttr(mergefrom
->m_defGridAttr
);
2951 void wxGridCellAttr::SetSize(int num_rows
, int num_cols
)
2953 // The size of a cell is normally 1,1
2955 // If this cell is larger (2,2) then this is the top left cell
2956 // the other cells that will be covered (lower right cells) must be
2957 // set to negative or zero values such that
2958 // row + num_rows of the covered cell points to the larger cell (this cell)
2959 // same goes for the col + num_cols.
2961 // Size of 0,0 is NOT valid, neither is <=0 and any positive value
2963 wxASSERT_MSG( (!((num_rows
> 0) && (num_cols
<= 0)) ||
2964 !((num_rows
<= 0) && (num_cols
> 0)) ||
2965 !((num_rows
== 0) && (num_cols
== 0))),
2966 wxT("wxGridCellAttr::SetSize only takes two postive values or negative/zero values"));
2968 m_sizeRows
= num_rows
;
2969 m_sizeCols
= num_cols
;
2972 const wxColour
& wxGridCellAttr::GetTextColour() const
2974 if (HasTextColour())
2978 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2980 return m_defGridAttr
->GetTextColour();
2984 wxFAIL_MSG(wxT("Missing default cell attribute"));
2985 return wxNullColour
;
2989 const wxColour
& wxGridCellAttr::GetBackgroundColour() const
2991 if (HasBackgroundColour())
2995 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2997 return m_defGridAttr
->GetBackgroundColour();
3001 wxFAIL_MSG(wxT("Missing default cell attribute"));
3002 return wxNullColour
;
3006 const wxFont
& wxGridCellAttr::GetFont() const
3012 else if (m_defGridAttr
&& m_defGridAttr
!= this)
3014 return m_defGridAttr
->GetFont();
3018 wxFAIL_MSG(wxT("Missing default cell attribute"));
3023 void wxGridCellAttr::GetAlignment(int *hAlign
, int *vAlign
) const
3032 else if (m_defGridAttr
&& m_defGridAttr
!= this)
3034 m_defGridAttr
->GetAlignment(hAlign
, vAlign
);
3038 wxFAIL_MSG(wxT("Missing default cell attribute"));
3042 void wxGridCellAttr::GetSize( int *num_rows
, int *num_cols
) const
3045 *num_rows
= m_sizeRows
;
3047 *num_cols
= m_sizeCols
;
3050 // GetRenderer and GetEditor use a slightly different decision path about
3051 // which attribute to use. If a non-default attr object has one then it is
3052 // used, otherwise the default editor or renderer is fetched from the grid and
3053 // used. It should be the default for the data type of the cell. If it is
3054 // NULL (because the table has a type that the grid does not have in its
3055 // registry), then the grid's default editor or renderer is used.
3057 wxGridCellRenderer
* wxGridCellAttr::GetRenderer(const wxGrid
* grid
, int row
, int col
) const
3059 wxGridCellRenderer
*renderer
= NULL
;
3061 if ( m_renderer
&& this != m_defGridAttr
)
3063 // use the cells renderer if it has one
3064 renderer
= m_renderer
;
3067 else // no non-default cell renderer
3069 // get default renderer for the data type
3072 // GetDefaultRendererForCell() will do IncRef() for us
3073 renderer
= grid
->GetDefaultRendererForCell(row
, col
);
3076 if ( renderer
== NULL
)
3078 if ( (m_defGridAttr
!= NULL
) && (m_defGridAttr
!= this) )
3080 // if we still don't have one then use the grid default
3081 // (no need for IncRef() here neither)
3082 renderer
= m_defGridAttr
->GetRenderer(NULL
, 0, 0);
3084 else // default grid attr
3086 // use m_renderer which we had decided not to use initially
3087 renderer
= m_renderer
;
3094 // we're supposed to always find something
3095 wxASSERT_MSG(renderer
, wxT("Missing default cell renderer"));
3100 // same as above, except for s/renderer/editor/g
3101 wxGridCellEditor
* wxGridCellAttr::GetEditor(const wxGrid
* grid
, int row
, int col
) const
3103 wxGridCellEditor
*editor
= NULL
;
3105 if ( m_editor
&& this != m_defGridAttr
)
3107 // use the cells editor if it has one
3111 else // no non default cell editor
3113 // get default editor for the data type
3116 // GetDefaultEditorForCell() will do IncRef() for us
3117 editor
= grid
->GetDefaultEditorForCell(row
, col
);
3120 if ( editor
== NULL
)
3122 if ( (m_defGridAttr
!= NULL
) && (m_defGridAttr
!= this) )
3124 // if we still don't have one then use the grid default
3125 // (no need for IncRef() here neither)
3126 editor
= m_defGridAttr
->GetEditor(NULL
, 0, 0);
3128 else // default grid attr
3130 // use m_editor which we had decided not to use initially
3138 // we're supposed to always find something
3139 wxASSERT_MSG(editor
, wxT("Missing default cell editor"));
3144 // ----------------------------------------------------------------------------
3145 // wxGridCellAttrData
3146 // ----------------------------------------------------------------------------
3148 void wxGridCellAttrData::SetAttr(wxGridCellAttr
*attr
, int row
, int col
)
3150 // Note: contrary to wxGridRowOrColAttrData::SetAttr, we must not
3151 // touch attribute's reference counting explicitly, since this
3152 // is managed by class wxGridCellWithAttr
3153 int n
= FindIndex(row
, col
);
3154 if ( n
== wxNOT_FOUND
)
3158 // add the attribute
3159 m_attrs
.Add(new wxGridCellWithAttr(row
, col
, attr
));
3161 //else: nothing to do
3163 else // we already have an attribute for this cell
3167 // change the attribute
3168 m_attrs
[(size_t)n
].ChangeAttr(attr
);
3172 // remove this attribute
3173 m_attrs
.RemoveAt((size_t)n
);
3178 wxGridCellAttr
*wxGridCellAttrData::GetAttr(int row
, int col
) const
3180 wxGridCellAttr
*attr
= NULL
;
3182 int n
= FindIndex(row
, col
);
3183 if ( n
!= wxNOT_FOUND
)
3185 attr
= m_attrs
[(size_t)n
].attr
;
3192 void wxGridCellAttrData::UpdateAttrRows( size_t pos
, int numRows
)
3194 size_t count
= m_attrs
.GetCount();
3195 for ( size_t n
= 0; n
< count
; n
++ )
3197 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
3198 wxCoord row
= coords
.GetRow();
3199 if ((size_t)row
>= pos
)
3203 // If rows inserted, include row counter where necessary
3204 coords
.SetRow(row
+ numRows
);
3206 else if (numRows
< 0)
3208 // If rows deleted ...
3209 if ((size_t)row
>= pos
- numRows
)
3211 // ...either decrement row counter (if row still exists)...
3212 coords
.SetRow(row
+ numRows
);
3216 // ...or remove the attribute
3217 m_attrs
.RemoveAt(n
);
3226 void wxGridCellAttrData::UpdateAttrCols( size_t pos
, int numCols
)
3228 size_t count
= m_attrs
.GetCount();
3229 for ( size_t n
= 0; n
< count
; n
++ )
3231 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
3232 wxCoord col
= coords
.GetCol();
3233 if ( (size_t)col
>= pos
)
3237 // If rows inserted, include row counter where necessary
3238 coords
.SetCol(col
+ numCols
);
3240 else if (numCols
< 0)
3242 // If rows deleted ...
3243 if ((size_t)col
>= pos
- numCols
)
3245 // ...either decrement row counter (if row still exists)...
3246 coords
.SetCol(col
+ numCols
);
3250 // ...or remove the attribute
3251 m_attrs
.RemoveAt(n
);
3260 int wxGridCellAttrData::FindIndex(int row
, int col
) const
3262 size_t count
= m_attrs
.GetCount();
3263 for ( size_t n
= 0; n
< count
; n
++ )
3265 const wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
3266 if ( (coords
.GetRow() == row
) && (coords
.GetCol() == col
) )
3275 // ----------------------------------------------------------------------------
3276 // wxGridRowOrColAttrData
3277 // ----------------------------------------------------------------------------
3279 wxGridRowOrColAttrData::~wxGridRowOrColAttrData()
3281 size_t count
= m_attrs
.GetCount();
3282 for ( size_t n
= 0; n
< count
; n
++ )
3284 m_attrs
[n
]->DecRef();
3288 wxGridCellAttr
*wxGridRowOrColAttrData::GetAttr(int rowOrCol
) const
3290 wxGridCellAttr
*attr
= NULL
;
3292 int n
= m_rowsOrCols
.Index(rowOrCol
);
3293 if ( n
!= wxNOT_FOUND
)
3295 attr
= m_attrs
[(size_t)n
];
3302 void wxGridRowOrColAttrData::SetAttr(wxGridCellAttr
*attr
, int rowOrCol
)
3304 int i
= m_rowsOrCols
.Index(rowOrCol
);
3305 if ( i
== wxNOT_FOUND
)
3309 // add the attribute - no need to do anything to reference count
3310 // since we take ownership of the attribute.
3311 m_rowsOrCols
.Add(rowOrCol
);
3314 // nothing to remove
3318 size_t n
= (size_t)i
;
3319 if ( m_attrs
[n
] == attr
)
3324 // change the attribute, handling reference count manually,
3325 // taking ownership of the new attribute.
3326 m_attrs
[n
]->DecRef();
3331 // remove this attribute, handling reference count manually
3332 m_attrs
[n
]->DecRef();
3333 m_rowsOrCols
.RemoveAt(n
);
3334 m_attrs
.RemoveAt(n
);
3339 void wxGridRowOrColAttrData::UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
)
3341 size_t count
= m_attrs
.GetCount();
3342 for ( size_t n
= 0; n
< count
; n
++ )
3344 int & rowOrCol
= m_rowsOrCols
[n
];
3345 if ( (size_t)rowOrCol
>= pos
)
3347 if ( numRowsOrCols
> 0 )
3349 // If rows inserted, include row counter where necessary
3350 rowOrCol
+= numRowsOrCols
;
3352 else if ( numRowsOrCols
< 0)
3354 // If rows deleted, either decrement row counter (if row still exists)
3355 if ((size_t)rowOrCol
>= pos
- numRowsOrCols
)
3356 rowOrCol
+= numRowsOrCols
;
3359 m_rowsOrCols
.RemoveAt(n
);
3360 m_attrs
[n
]->DecRef();
3361 m_attrs
.RemoveAt(n
);
3370 // ----------------------------------------------------------------------------
3371 // wxGridCellAttrProvider
3372 // ----------------------------------------------------------------------------
3374 wxGridCellAttrProvider::wxGridCellAttrProvider()
3379 wxGridCellAttrProvider::~wxGridCellAttrProvider()
3384 void wxGridCellAttrProvider::InitData()
3386 m_data
= new wxGridCellAttrProviderData
;
3389 wxGridCellAttr
*wxGridCellAttrProvider::GetAttr(int row
, int col
,
3390 wxGridCellAttr::wxAttrKind kind
) const
3392 wxGridCellAttr
*attr
= NULL
;
3397 case (wxGridCellAttr::Any
):
3398 // Get cached merge attributes.
3399 // Currently not used as no cache implemented as not mutable
3400 // attr = m_data->m_mergeAttr.GetAttr(row, col);
3403 // Basically implement old version.
3404 // Also check merge cache, so we don't have to re-merge every time..
3405 wxGridCellAttr
*attrcell
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
3406 wxGridCellAttr
*attrrow
= m_data
->m_rowAttrs
.GetAttr(row
);
3407 wxGridCellAttr
*attrcol
= m_data
->m_colAttrs
.GetAttr(col
);
3409 if ((attrcell
!= attrrow
) && (attrrow
!= attrcol
) && (attrcell
!= attrcol
))
3411 // Two or more are non NULL
3412 attr
= new wxGridCellAttr
;
3413 attr
->SetKind(wxGridCellAttr::Merged
);
3415 // Order is important..
3418 attr
->MergeWith(attrcell
);
3423 attr
->MergeWith(attrcol
);
3428 attr
->MergeWith(attrrow
);
3432 // store merge attr if cache implemented
3434 //m_data->m_mergeAttr.SetAttr(attr, row, col);
3438 // one or none is non null return it or null.
3457 case (wxGridCellAttr::Cell
):
3458 attr
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
3461 case (wxGridCellAttr::Col
):
3462 attr
= m_data
->m_colAttrs
.GetAttr(col
);
3465 case (wxGridCellAttr::Row
):
3466 attr
= m_data
->m_rowAttrs
.GetAttr(row
);
3471 // (wxGridCellAttr::Default):
3472 // (wxGridCellAttr::Merged):
3480 void wxGridCellAttrProvider::SetAttr(wxGridCellAttr
*attr
,
3486 m_data
->m_cellAttrs
.SetAttr(attr
, row
, col
);
3489 void wxGridCellAttrProvider::SetRowAttr(wxGridCellAttr
*attr
, int row
)
3494 m_data
->m_rowAttrs
.SetAttr(attr
, row
);
3497 void wxGridCellAttrProvider::SetColAttr(wxGridCellAttr
*attr
, int col
)
3502 m_data
->m_colAttrs
.SetAttr(attr
, col
);
3505 void wxGridCellAttrProvider::UpdateAttrRows( size_t pos
, int numRows
)
3509 m_data
->m_cellAttrs
.UpdateAttrRows( pos
, numRows
);
3511 m_data
->m_rowAttrs
.UpdateAttrRowsOrCols( pos
, numRows
);
3515 void wxGridCellAttrProvider::UpdateAttrCols( size_t pos
, int numCols
)
3519 m_data
->m_cellAttrs
.UpdateAttrCols( pos
, numCols
);
3521 m_data
->m_colAttrs
.UpdateAttrRowsOrCols( pos
, numCols
);
3525 // ----------------------------------------------------------------------------
3526 // wxGridTypeRegistry
3527 // ----------------------------------------------------------------------------
3529 wxGridTypeRegistry::~wxGridTypeRegistry()
3531 size_t count
= m_typeinfo
.GetCount();
3532 for ( size_t i
= 0; i
< count
; i
++ )
3533 delete m_typeinfo
[i
];
3536 void wxGridTypeRegistry::RegisterDataType(const wxString
& typeName
,
3537 wxGridCellRenderer
* renderer
,
3538 wxGridCellEditor
* editor
)
3540 wxGridDataTypeInfo
* info
= new wxGridDataTypeInfo(typeName
, renderer
, editor
);
3542 // is it already registered?
3543 int loc
= FindRegisteredDataType(typeName
);
3544 if ( loc
!= wxNOT_FOUND
)
3546 delete m_typeinfo
[loc
];
3547 m_typeinfo
[loc
] = info
;
3551 m_typeinfo
.Add(info
);
3555 int wxGridTypeRegistry::FindRegisteredDataType(const wxString
& typeName
)
3557 size_t count
= m_typeinfo
.GetCount();
3558 for ( size_t i
= 0; i
< count
; i
++ )
3560 if ( typeName
== m_typeinfo
[i
]->m_typeName
)
3569 int wxGridTypeRegistry::FindDataType(const wxString
& typeName
)
3571 int index
= FindRegisteredDataType(typeName
);
3572 if ( index
== wxNOT_FOUND
)
3574 // check whether this is one of the standard ones, in which case
3575 // register it "on the fly"
3577 if ( typeName
== wxGRID_VALUE_STRING
)
3579 RegisterDataType(wxGRID_VALUE_STRING
,
3580 new wxGridCellStringRenderer
,
3581 new wxGridCellTextEditor
);
3584 #endif // wxUSE_TEXTCTRL
3586 if ( typeName
== wxGRID_VALUE_BOOL
)
3588 RegisterDataType(wxGRID_VALUE_BOOL
,
3589 new wxGridCellBoolRenderer
,
3590 new wxGridCellBoolEditor
);
3593 #endif // wxUSE_CHECKBOX
3595 if ( typeName
== wxGRID_VALUE_NUMBER
)
3597 RegisterDataType(wxGRID_VALUE_NUMBER
,
3598 new wxGridCellNumberRenderer
,
3599 new wxGridCellNumberEditor
);
3601 else if ( typeName
== wxGRID_VALUE_FLOAT
)
3603 RegisterDataType(wxGRID_VALUE_FLOAT
,
3604 new wxGridCellFloatRenderer
,
3605 new wxGridCellFloatEditor
);
3608 #endif // wxUSE_TEXTCTRL
3610 if ( typeName
== wxGRID_VALUE_CHOICE
)
3612 RegisterDataType(wxGRID_VALUE_CHOICE
,
3613 new wxGridCellStringRenderer
,
3614 new wxGridCellChoiceEditor
);
3617 #endif // wxUSE_COMBOBOX
3622 // we get here only if just added the entry for this type, so return
3624 index
= m_typeinfo
.GetCount() - 1;
3630 int wxGridTypeRegistry::FindOrCloneDataType(const wxString
& typeName
)
3632 int index
= FindDataType(typeName
);
3633 if ( index
== wxNOT_FOUND
)
3635 // the first part of the typename is the "real" type, anything after ':'
3636 // are the parameters for the renderer
3637 index
= FindDataType(typeName
.BeforeFirst(_T(':')));
3638 if ( index
== wxNOT_FOUND
)
3643 wxGridCellRenderer
*renderer
= GetRenderer(index
);
3644 wxGridCellRenderer
*rendererOld
= renderer
;
3645 renderer
= renderer
->Clone();
3646 rendererOld
->DecRef();
3648 wxGridCellEditor
*editor
= GetEditor(index
);
3649 wxGridCellEditor
*editorOld
= editor
;
3650 editor
= editor
->Clone();
3651 editorOld
->DecRef();
3653 // do it even if there are no parameters to reset them to defaults
3654 wxString params
= typeName
.AfterFirst(_T(':'));
3655 renderer
->SetParameters(params
);
3656 editor
->SetParameters(params
);
3658 // register the new typename
3659 RegisterDataType(typeName
, renderer
, editor
);
3661 // we just registered it, it's the last one
3662 index
= m_typeinfo
.GetCount() - 1;
3668 wxGridCellRenderer
* wxGridTypeRegistry::GetRenderer(int index
)
3670 wxGridCellRenderer
* renderer
= m_typeinfo
[index
]->m_renderer
;
3677 wxGridCellEditor
* wxGridTypeRegistry::GetEditor(int index
)
3679 wxGridCellEditor
* editor
= m_typeinfo
[index
]->m_editor
;
3686 // ----------------------------------------------------------------------------
3688 // ----------------------------------------------------------------------------
3690 IMPLEMENT_ABSTRACT_CLASS( wxGridTableBase
, wxObject
)
3692 wxGridTableBase::wxGridTableBase()
3695 m_attrProvider
= NULL
;
3698 wxGridTableBase::~wxGridTableBase()
3700 delete m_attrProvider
;
3703 void wxGridTableBase::SetAttrProvider(wxGridCellAttrProvider
*attrProvider
)
3705 delete m_attrProvider
;
3706 m_attrProvider
= attrProvider
;
3709 bool wxGridTableBase::CanHaveAttributes()
3711 if ( ! GetAttrProvider() )
3713 // use the default attr provider by default
3714 SetAttrProvider(new wxGridCellAttrProvider
);
3720 wxGridCellAttr
*wxGridTableBase::GetAttr(int row
, int col
, wxGridCellAttr::wxAttrKind kind
)
3722 if ( m_attrProvider
)
3723 return m_attrProvider
->GetAttr(row
, col
, kind
);
3728 void wxGridTableBase::SetAttr(wxGridCellAttr
* attr
, int row
, int col
)
3730 if ( m_attrProvider
)
3733 attr
->SetKind(wxGridCellAttr::Cell
);
3734 m_attrProvider
->SetAttr(attr
, row
, col
);
3738 // as we take ownership of the pointer and don't store it, we must
3744 void wxGridTableBase::SetRowAttr(wxGridCellAttr
*attr
, int row
)
3746 if ( m_attrProvider
)
3748 attr
->SetKind(wxGridCellAttr::Row
);
3749 m_attrProvider
->SetRowAttr(attr
, row
);
3753 // as we take ownership of the pointer and don't store it, we must
3759 void wxGridTableBase::SetColAttr(wxGridCellAttr
*attr
, int col
)
3761 if ( m_attrProvider
)
3763 attr
->SetKind(wxGridCellAttr::Col
);
3764 m_attrProvider
->SetColAttr(attr
, col
);
3768 // as we take ownership of the pointer and don't store it, we must
3774 bool wxGridTableBase::InsertRows( size_t WXUNUSED(pos
),
3775 size_t WXUNUSED(numRows
) )
3777 wxFAIL_MSG( wxT("Called grid table class function InsertRows\nbut your derived table class does not override this function") );
3782 bool wxGridTableBase::AppendRows( size_t WXUNUSED(numRows
) )
3784 wxFAIL_MSG( wxT("Called grid table class function AppendRows\nbut your derived table class does not override this function"));
3789 bool wxGridTableBase::DeleteRows( size_t WXUNUSED(pos
),
3790 size_t WXUNUSED(numRows
) )
3792 wxFAIL_MSG( wxT("Called grid table class function DeleteRows\nbut your derived table class does not override this function"));
3797 bool wxGridTableBase::InsertCols( size_t WXUNUSED(pos
),
3798 size_t WXUNUSED(numCols
) )
3800 wxFAIL_MSG( wxT("Called grid table class function InsertCols\nbut your derived table class does not override this function"));
3805 bool wxGridTableBase::AppendCols( size_t WXUNUSED(numCols
) )
3807 wxFAIL_MSG(wxT("Called grid table class function AppendCols\nbut your derived table class does not override this function"));
3812 bool wxGridTableBase::DeleteCols( size_t WXUNUSED(pos
),
3813 size_t WXUNUSED(numCols
) )
3815 wxFAIL_MSG( wxT("Called grid table class function DeleteCols\nbut your derived table class does not override this function"));
3820 wxString
wxGridTableBase::GetRowLabelValue( int row
)
3824 // RD: Starting the rows at zero confuses users,
3825 // no matter how much it makes sense to us geeks.
3831 wxString
wxGridTableBase::GetColLabelValue( int col
)
3833 // default col labels are:
3834 // cols 0 to 25 : A-Z
3835 // cols 26 to 675 : AA-ZZ
3840 for ( n
= 1; ; n
++ )
3842 s
+= (wxChar
) (_T('A') + (wxChar
)(col
% 26));
3848 // reverse the string...
3850 for ( i
= 0; i
< n
; i
++ )
3858 wxString
wxGridTableBase::GetTypeName( int WXUNUSED(row
), int WXUNUSED(col
) )
3860 return wxGRID_VALUE_STRING
;
3863 bool wxGridTableBase::CanGetValueAs( int WXUNUSED(row
), int WXUNUSED(col
),
3864 const wxString
& typeName
)
3866 return typeName
== wxGRID_VALUE_STRING
;
3869 bool wxGridTableBase::CanSetValueAs( int row
, int col
, const wxString
& typeName
)
3871 return CanGetValueAs(row
, col
, typeName
);
3874 long wxGridTableBase::GetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
) )
3879 double wxGridTableBase::GetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
) )
3884 bool wxGridTableBase::GetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
) )
3889 void wxGridTableBase::SetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
),
3890 long WXUNUSED(value
) )
3894 void wxGridTableBase::SetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
),
3895 double WXUNUSED(value
) )
3899 void wxGridTableBase::SetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
),
3900 bool WXUNUSED(value
) )
3904 void* wxGridTableBase::GetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
3905 const wxString
& WXUNUSED(typeName
) )
3910 void wxGridTableBase::SetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
3911 const wxString
& WXUNUSED(typeName
),
3912 void* WXUNUSED(value
) )
3916 //////////////////////////////////////////////////////////////////////
3918 // Message class for the grid table to send requests and notifications
3922 wxGridTableMessage::wxGridTableMessage()
3930 wxGridTableMessage::wxGridTableMessage( wxGridTableBase
*table
, int id
,
3931 int commandInt1
, int commandInt2
)
3935 m_comInt1
= commandInt1
;
3936 m_comInt2
= commandInt2
;
3939 //////////////////////////////////////////////////////////////////////
3941 // A basic grid table for string data. An object of this class will
3942 // created by wxGrid if you don't specify an alternative table class.
3945 WX_DEFINE_OBJARRAY(wxGridStringArray
)
3947 IMPLEMENT_DYNAMIC_CLASS( wxGridStringTable
, wxGridTableBase
)
3949 wxGridStringTable::wxGridStringTable()
3954 wxGridStringTable::wxGridStringTable( int numRows
, int numCols
)
3957 m_data
.Alloc( numRows
);
3960 sa
.Alloc( numCols
);
3961 sa
.Add( wxEmptyString
, numCols
);
3963 m_data
.Add( sa
, numRows
);
3966 wxGridStringTable::~wxGridStringTable()
3970 int wxGridStringTable::GetNumberRows()
3972 return m_data
.GetCount();
3975 int wxGridStringTable::GetNumberCols()
3977 if ( m_data
.GetCount() > 0 )
3978 return m_data
[0].GetCount();
3983 wxString
wxGridStringTable::GetValue( int row
, int col
)
3985 wxCHECK_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3987 _T("invalid row or column index in wxGridStringTable") );
3989 return m_data
[row
][col
];
3992 void wxGridStringTable::SetValue( int row
, int col
, const wxString
& value
)
3994 wxCHECK_RET( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3995 _T("invalid row or column index in wxGridStringTable") );
3997 m_data
[row
][col
] = value
;
4000 void wxGridStringTable::Clear()
4003 int numRows
, numCols
;
4005 numRows
= m_data
.GetCount();
4008 numCols
= m_data
[0].GetCount();
4010 for ( row
= 0; row
< numRows
; row
++ )
4012 for ( col
= 0; col
< numCols
; col
++ )
4014 m_data
[row
][col
] = wxEmptyString
;
4020 bool wxGridStringTable::InsertRows( size_t pos
, size_t numRows
)
4022 size_t curNumRows
= m_data
.GetCount();
4023 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
4024 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
4026 if ( pos
>= curNumRows
)
4028 return AppendRows( numRows
);
4032 sa
.Alloc( curNumCols
);
4033 sa
.Add( wxEmptyString
, curNumCols
);
4034 m_data
.Insert( sa
, pos
, numRows
);
4038 wxGridTableMessage
msg( this,
4039 wxGRIDTABLE_NOTIFY_ROWS_INSERTED
,
4043 GetView()->ProcessTableMessage( msg
);
4049 bool wxGridStringTable::AppendRows( size_t numRows
)
4051 size_t curNumRows
= m_data
.GetCount();
4052 size_t curNumCols
= ( curNumRows
> 0
4053 ? m_data
[0].GetCount()
4054 : ( GetView() ? GetView()->GetNumberCols() : 0 ) );
4057 if ( curNumCols
> 0 )
4059 sa
.Alloc( curNumCols
);
4060 sa
.Add( wxEmptyString
, curNumCols
);
4063 m_data
.Add( sa
, numRows
);
4067 wxGridTableMessage
msg( this,
4068 wxGRIDTABLE_NOTIFY_ROWS_APPENDED
,
4071 GetView()->ProcessTableMessage( msg
);
4077 bool wxGridStringTable::DeleteRows( size_t pos
, size_t numRows
)
4079 size_t curNumRows
= m_data
.GetCount();
4081 if ( pos
>= curNumRows
)
4083 wxFAIL_MSG( wxString::Format
4085 wxT("Called wxGridStringTable::DeleteRows(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu rows"),
4087 (unsigned long)numRows
,
4088 (unsigned long)curNumRows
4094 if ( numRows
> curNumRows
- pos
)
4096 numRows
= curNumRows
- pos
;
4099 if ( numRows
>= curNumRows
)
4105 m_data
.RemoveAt( pos
, numRows
);
4110 wxGridTableMessage
msg( this,
4111 wxGRIDTABLE_NOTIFY_ROWS_DELETED
,
4115 GetView()->ProcessTableMessage( msg
);
4121 bool wxGridStringTable::InsertCols( size_t pos
, size_t numCols
)
4125 size_t curNumRows
= m_data
.GetCount();
4126 size_t curNumCols
= ( curNumRows
> 0
4127 ? m_data
[0].GetCount()
4128 : ( GetView() ? GetView()->GetNumberCols() : 0 ) );
4130 if ( pos
>= curNumCols
)
4132 return AppendCols( numCols
);
4135 if ( !m_colLabels
.IsEmpty() )
4137 m_colLabels
.Insert( wxEmptyString
, pos
, numCols
);
4140 for ( i
= pos
; i
< pos
+ numCols
; i
++ )
4141 m_colLabels
[i
] = wxGridTableBase::GetColLabelValue( i
);
4144 for ( row
= 0; row
< curNumRows
; row
++ )
4146 for ( col
= pos
; col
< pos
+ numCols
; col
++ )
4148 m_data
[row
].Insert( wxEmptyString
, col
);
4154 wxGridTableMessage
msg( this,
4155 wxGRIDTABLE_NOTIFY_COLS_INSERTED
,
4159 GetView()->ProcessTableMessage( msg
);
4165 bool wxGridStringTable::AppendCols( size_t numCols
)
4169 size_t curNumRows
= m_data
.GetCount();
4171 for ( row
= 0; row
< curNumRows
; row
++ )
4173 m_data
[row
].Add( wxEmptyString
, numCols
);
4178 wxGridTableMessage
msg( this,
4179 wxGRIDTABLE_NOTIFY_COLS_APPENDED
,
4182 GetView()->ProcessTableMessage( msg
);
4188 bool wxGridStringTable::DeleteCols( size_t pos
, size_t numCols
)
4192 size_t curNumRows
= m_data
.GetCount();
4193 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
4194 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
4196 if ( pos
>= curNumCols
)
4198 wxFAIL_MSG( wxString::Format
4200 wxT("Called wxGridStringTable::DeleteCols(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu cols"),
4202 (unsigned long)numCols
,
4203 (unsigned long)curNumCols
4210 colID
= GetView()->GetColAt( pos
);
4214 if ( numCols
> curNumCols
- colID
)
4216 numCols
= curNumCols
- colID
;
4219 if ( !m_colLabels
.IsEmpty() )
4221 // m_colLabels stores just as many elements as it needs, e.g. if only
4222 // the label of the first column had been set it would have only one
4223 // element and not numCols, so account for it
4224 int nToRm
= m_colLabels
.size() - colID
;
4226 m_colLabels
.RemoveAt( colID
, nToRm
);
4229 for ( row
= 0; row
< curNumRows
; row
++ )
4231 if ( numCols
>= curNumCols
)
4233 m_data
[row
].Clear();
4237 m_data
[row
].RemoveAt( colID
, numCols
);
4243 wxGridTableMessage
msg( this,
4244 wxGRIDTABLE_NOTIFY_COLS_DELETED
,
4248 GetView()->ProcessTableMessage( msg
);
4254 wxString
wxGridStringTable::GetRowLabelValue( int row
)
4256 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
4258 // using default label
4260 return wxGridTableBase::GetRowLabelValue( row
);
4264 return m_rowLabels
[row
];
4268 wxString
wxGridStringTable::GetColLabelValue( int col
)
4270 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
4272 // using default label
4274 return wxGridTableBase::GetColLabelValue( col
);
4278 return m_colLabels
[col
];
4282 void wxGridStringTable::SetRowLabelValue( int row
, const wxString
& value
)
4284 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
4286 int n
= m_rowLabels
.GetCount();
4289 for ( i
= n
; i
<= row
; i
++ )
4291 m_rowLabels
.Add( wxGridTableBase::GetRowLabelValue(i
) );
4295 m_rowLabels
[row
] = value
;
4298 void wxGridStringTable::SetColLabelValue( int col
, const wxString
& value
)
4300 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
4302 int n
= m_colLabels
.GetCount();
4305 for ( i
= n
; i
<= col
; i
++ )
4307 m_colLabels
.Add( wxGridTableBase::GetColLabelValue(i
) );
4311 m_colLabels
[col
] = value
;
4315 //////////////////////////////////////////////////////////////////////
4316 //////////////////////////////////////////////////////////////////////
4318 BEGIN_EVENT_TABLE(wxGridSubwindow
, wxWindow
)
4319 EVT_MOUSE_CAPTURE_LOST(wxGridSubwindow::OnMouseCaptureLost
)
4322 void wxGridSubwindow::OnMouseCaptureLost(wxMouseCaptureLostEvent
& WXUNUSED(event
))
4324 m_owner
->CancelMouseCapture();
4327 BEGIN_EVENT_TABLE( wxGridRowLabelWindow
, wxGridSubwindow
)
4328 EVT_PAINT( wxGridRowLabelWindow::OnPaint
)
4329 EVT_MOUSEWHEEL( wxGridRowLabelWindow::OnMouseWheel
)
4330 EVT_MOUSE_EVENTS( wxGridRowLabelWindow::OnMouseEvent
)
4333 void wxGridRowLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
4337 // NO - don't do this because it will set both the x and y origin
4338 // coords to match the parent scrolled window and we just want to
4339 // set the y coord - MB
4341 // m_owner->PrepareDC( dc );
4344 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
4345 wxPoint pt
= dc
.GetDeviceOrigin();
4346 dc
.SetDeviceOrigin( pt
.x
, pt
.y
-y
);
4348 wxArrayInt rows
= m_owner
->CalcRowLabelsExposed( GetUpdateRegion() );
4349 m_owner
->DrawRowLabels( dc
, rows
);
4352 void wxGridRowLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
4354 m_owner
->ProcessRowLabelMouseEvent( event
);
4357 void wxGridRowLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
4359 if (!m_owner
->GetEventHandler()->ProcessEvent( event
))
4363 //////////////////////////////////////////////////////////////////////
4365 BEGIN_EVENT_TABLE( wxGridColLabelWindow
, wxGridSubwindow
)
4366 EVT_PAINT( wxGridColLabelWindow::OnPaint
)
4367 EVT_MOUSEWHEEL( wxGridColLabelWindow::OnMouseWheel
)
4368 EVT_MOUSE_EVENTS( wxGridColLabelWindow::OnMouseEvent
)
4371 void wxGridColLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
4375 // NO - don't do this because it will set both the x and y origin
4376 // coords to match the parent scrolled window and we just want to
4377 // set the x coord - MB
4379 // m_owner->PrepareDC( dc );
4382 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
4383 wxPoint pt
= dc
.GetDeviceOrigin();
4384 if (GetLayoutDirection() == wxLayout_RightToLeft
)
4385 dc
.SetDeviceOrigin( pt
.x
+x
, pt
.y
);
4387 dc
.SetDeviceOrigin( pt
.x
-x
, pt
.y
);
4389 wxArrayInt cols
= m_owner
->CalcColLabelsExposed( GetUpdateRegion() );
4390 m_owner
->DrawColLabels( dc
, cols
);
4393 void wxGridColLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
4395 m_owner
->ProcessColLabelMouseEvent( event
);
4398 void wxGridColLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
4400 if (!m_owner
->GetEventHandler()->ProcessEvent( event
))
4404 //////////////////////////////////////////////////////////////////////
4406 BEGIN_EVENT_TABLE( wxGridCornerLabelWindow
, wxGridSubwindow
)
4407 EVT_MOUSEWHEEL( wxGridCornerLabelWindow::OnMouseWheel
)
4408 EVT_MOUSE_EVENTS( wxGridCornerLabelWindow::OnMouseEvent
)
4409 EVT_PAINT( wxGridCornerLabelWindow::OnPaint
)
4412 void wxGridCornerLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
4416 m_owner
->DrawCornerLabel(dc
);
4419 void wxGridCornerLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
4421 m_owner
->ProcessCornerLabelMouseEvent( event
);
4424 void wxGridCornerLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
4426 if (!m_owner
->GetEventHandler()->ProcessEvent(event
))
4430 //////////////////////////////////////////////////////////////////////
4432 BEGIN_EVENT_TABLE( wxGridWindow
, wxGridSubwindow
)
4433 EVT_PAINT( wxGridWindow::OnPaint
)
4434 EVT_MOUSEWHEEL( wxGridWindow::OnMouseWheel
)
4435 EVT_MOUSE_EVENTS( wxGridWindow::OnMouseEvent
)
4436 EVT_KEY_DOWN( wxGridWindow::OnKeyDown
)
4437 EVT_KEY_UP( wxGridWindow::OnKeyUp
)
4438 EVT_CHAR( wxGridWindow::OnChar
)
4439 EVT_SET_FOCUS( wxGridWindow::OnFocus
)
4440 EVT_KILL_FOCUS( wxGridWindow::OnFocus
)
4441 EVT_ERASE_BACKGROUND( wxGridWindow::OnEraseBackground
)
4444 void wxGridWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
4446 wxPaintDC
dc( this );
4447 m_owner
->PrepareDC( dc
);
4448 wxRegion reg
= GetUpdateRegion();
4449 wxGridCellCoordsArray dirtyCells
= m_owner
->CalcCellsExposed( reg
);
4450 m_owner
->DrawGridCellArea( dc
, dirtyCells
);
4452 m_owner
->DrawGridSpace( dc
);
4454 m_owner
->DrawAllGridLines( dc
, reg
);
4456 m_owner
->DrawHighlight( dc
, dirtyCells
);
4459 void wxGridWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
4461 wxWindow::ScrollWindow( dx
, dy
, rect
);
4462 m_owner
->GetGridRowLabelWindow()->ScrollWindow( 0, dy
, rect
);
4463 m_owner
->GetGridColLabelWindow()->ScrollWindow( dx
, 0, rect
);
4466 void wxGridWindow::OnMouseEvent( wxMouseEvent
& event
)
4468 if (event
.ButtonDown(wxMOUSE_BTN_LEFT
) && FindFocus() != this)
4471 m_owner
->ProcessGridCellMouseEvent( event
);
4474 void wxGridWindow::OnMouseWheel( wxMouseEvent
& event
)
4476 if (!m_owner
->GetEventHandler()->ProcessEvent( event
))
4480 // This seems to be required for wxMotif/wxGTK otherwise the mouse
4481 // cursor must be in the cell edit control to get key events
4483 void wxGridWindow::OnKeyDown( wxKeyEvent
& event
)
4485 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
4489 void wxGridWindow::OnKeyUp( wxKeyEvent
& event
)
4491 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
4495 void wxGridWindow::OnChar( wxKeyEvent
& event
)
4497 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
4501 void wxGridWindow::OnEraseBackground( wxEraseEvent
& WXUNUSED(event
) )
4505 void wxGridWindow::OnFocus(wxFocusEvent
& event
)
4507 // and if we have any selection, it has to be repainted, because it
4508 // uses different colour when the grid is not focused:
4509 if ( m_owner
->IsSelection() )
4515 // NB: Note that this code is in "else" branch only because the other
4516 // branch refreshes everything and so there's no point in calling
4517 // Refresh() again, *not* because it should only be done if
4518 // !IsSelection(). If the above code is ever optimized to refresh
4519 // only selected area, this needs to be moved out of the "else"
4520 // branch so that it's always executed.
4522 // current cell cursor {dis,re}appears on focus change:
4523 const wxGridCellCoords
cursorCoords(m_owner
->GetGridCursorRow(),
4524 m_owner
->GetGridCursorCol());
4525 const wxRect cursor
=
4526 m_owner
->BlockToDeviceRect(cursorCoords
, cursorCoords
);
4527 Refresh(true, &cursor
);
4530 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
4534 #define internalXToCol(x) XToCol(x, true)
4535 #define internalYToRow(y) YToRow(y, true)
4537 /////////////////////////////////////////////////////////////////////
4539 #if wxUSE_EXTENDED_RTTI
4540 WX_DEFINE_FLAGS( wxGridStyle
)
4542 wxBEGIN_FLAGS( wxGridStyle
)
4543 // new style border flags, we put them first to
4544 // use them for streaming out
4545 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
4546 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
4547 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
4548 wxFLAGS_MEMBER(wxBORDER_RAISED
)
4549 wxFLAGS_MEMBER(wxBORDER_STATIC
)
4550 wxFLAGS_MEMBER(wxBORDER_NONE
)
4552 // old style border flags
4553 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
4554 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
4555 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
4556 wxFLAGS_MEMBER(wxRAISED_BORDER
)
4557 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
4558 wxFLAGS_MEMBER(wxBORDER
)
4560 // standard window styles
4561 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
4562 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
4563 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
4564 wxFLAGS_MEMBER(wxWANTS_CHARS
)
4565 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE
)
4566 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
4567 wxFLAGS_MEMBER(wxVSCROLL
)
4568 wxFLAGS_MEMBER(wxHSCROLL
)
4570 wxEND_FLAGS( wxGridStyle
)
4572 IMPLEMENT_DYNAMIC_CLASS_XTI(wxGrid
, wxScrolledWindow
,"wx/grid.h")
4574 wxBEGIN_PROPERTIES_TABLE(wxGrid
)
4575 wxHIDE_PROPERTY( Children
)
4576 wxPROPERTY_FLAGS( WindowStyle
, wxGridStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
4577 wxEND_PROPERTIES_TABLE()
4579 wxBEGIN_HANDLERS_TABLE(wxGrid
)
4580 wxEND_HANDLERS_TABLE()
4582 wxCONSTRUCTOR_5( wxGrid
, wxWindow
* , Parent
, wxWindowID
, Id
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
4585 TODO : Expose more information of a list's layout, etc. via appropriate objects (e.g., NotebookPageInfo)
4588 IMPLEMENT_DYNAMIC_CLASS( wxGrid
, wxScrolledWindow
)
4591 BEGIN_EVENT_TABLE( wxGrid
, wxScrolledWindow
)
4592 EVT_PAINT( wxGrid::OnPaint
)
4593 EVT_SIZE( wxGrid::OnSize
)
4594 EVT_KEY_DOWN( wxGrid::OnKeyDown
)
4595 EVT_KEY_UP( wxGrid::OnKeyUp
)
4596 EVT_CHAR ( wxGrid::OnChar
)
4597 EVT_ERASE_BACKGROUND( wxGrid::OnEraseBackground
)
4600 bool wxGrid::Create(wxWindow
*parent
, wxWindowID id
,
4601 const wxPoint
& pos
, const wxSize
& size
,
4602 long style
, const wxString
& name
)
4604 if (!wxScrolledWindow::Create(parent
, id
, pos
, size
,
4605 style
| wxWANTS_CHARS
, name
))
4608 m_colMinWidths
= wxLongToLongHashMap(GRID_HASH_SIZE
);
4609 m_rowMinHeights
= wxLongToLongHashMap(GRID_HASH_SIZE
);
4612 SetInitialSize(size
);
4613 SetScrollRate(m_scrollLineX
, m_scrollLineY
);
4622 m_winCapture
->ReleaseMouse();
4624 // Ensure that the editor control is destroyed before the grid is,
4625 // otherwise we crash later when the editor tries to do something with the
4626 // half destroyed grid
4627 HideCellEditControl();
4629 // Must do this or ~wxScrollHelper will pop the wrong event handler
4630 SetTargetWindow(this);
4632 wxSafeDecRef(m_defaultCellAttr
);
4634 #ifdef DEBUG_ATTR_CACHE
4635 size_t total
= gs_nAttrCacheHits
+ gs_nAttrCacheMisses
;
4636 wxPrintf(_T("wxGrid attribute cache statistics: "
4637 "total: %u, hits: %u (%u%%)\n"),
4638 total
, gs_nAttrCacheHits
,
4639 total
? (gs_nAttrCacheHits
*100) / total
: 0);
4642 // if we own the table, just delete it, otherwise at least don't leave it
4643 // with dangling view pointer
4646 else if ( m_table
&& m_table
->GetView() == this )
4647 m_table
->SetView(NULL
);
4649 delete m_typeRegistry
;
4654 // ----- internal init and update functions
4657 // NOTE: If using the default visual attributes works everywhere then this can
4658 // be removed as well as the #else cases below.
4659 #define _USE_VISATTR 0
4661 void wxGrid::Create()
4663 // create the type registry
4664 m_typeRegistry
= new wxGridTypeRegistry
;
4666 m_cellEditCtrlEnabled
= false;
4668 m_defaultCellAttr
= new wxGridCellAttr();
4670 // Set default cell attributes
4671 m_defaultCellAttr
->SetDefAttr(m_defaultCellAttr
);
4672 m_defaultCellAttr
->SetKind(wxGridCellAttr::Default
);
4673 m_defaultCellAttr
->SetFont(GetFont());
4674 m_defaultCellAttr
->SetAlignment(wxALIGN_LEFT
, wxALIGN_TOP
);
4675 m_defaultCellAttr
->SetRenderer(new wxGridCellStringRenderer
);
4676 m_defaultCellAttr
->SetEditor(new wxGridCellTextEditor
);
4679 wxVisualAttributes gva
= wxListBox::GetClassDefaultAttributes();
4680 wxVisualAttributes lva
= wxPanel::GetClassDefaultAttributes();
4682 m_defaultCellAttr
->SetTextColour(gva
.colFg
);
4683 m_defaultCellAttr
->SetBackgroundColour(gva
.colBg
);
4686 m_defaultCellAttr
->SetTextColour(
4687 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
4688 m_defaultCellAttr
->SetBackgroundColour(
4689 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
4694 m_currentCellCoords
= wxGridNoCellCoords
;
4696 // subwindow components that make up the wxGrid
4697 m_rowLabelWin
= new wxGridRowLabelWindow(this);
4698 CreateColumnWindow();
4699 m_cornerLabelWin
= new wxGridCornerLabelWindow(this);
4700 m_gridWin
= new wxGridWindow( this );
4702 SetTargetWindow( m_gridWin
);
4705 wxColour gfg
= gva
.colFg
;
4706 wxColour gbg
= gva
.colBg
;
4707 wxColour lfg
= lva
.colFg
;
4708 wxColour lbg
= lva
.colBg
;
4710 wxColour gfg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT
);
4711 wxColour gbg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOW
);
4712 wxColour lfg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT
);
4713 wxColour lbg
= wxSystemSettings::GetColour( wxSYS_COLOUR_BTNFACE
);
4716 m_cornerLabelWin
->SetOwnForegroundColour(lfg
);
4717 m_cornerLabelWin
->SetOwnBackgroundColour(lbg
);
4718 m_rowLabelWin
->SetOwnForegroundColour(lfg
);
4719 m_rowLabelWin
->SetOwnBackgroundColour(lbg
);
4720 m_colWindow
->SetOwnForegroundColour(lfg
);
4721 m_colWindow
->SetOwnBackgroundColour(lbg
);
4723 m_gridWin
->SetOwnForegroundColour(gfg
);
4724 m_gridWin
->SetOwnBackgroundColour(gbg
);
4726 m_labelBackgroundColour
= m_rowLabelWin
->GetBackgroundColour();
4727 m_labelTextColour
= m_rowLabelWin
->GetForegroundColour();
4729 // now that we have the grid window, use its font to compute the default
4731 m_defaultRowHeight
= m_gridWin
->GetCharHeight();
4732 #if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl()
4733 m_defaultRowHeight
+= 8;
4735 m_defaultRowHeight
+= 4;
4740 void wxGrid::CreateColumnWindow()
4742 if ( m_useNativeHeader
)
4744 m_colWindow
= new wxGridHeaderCtrl(this);
4745 m_colLabelHeight
= m_colWindow
->GetBestSize().y
;
4747 else // draw labels ourselves
4749 m_colWindow
= new wxGridColLabelWindow(this);
4750 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
4754 bool wxGrid::CreateGrid( int numRows
, int numCols
,
4755 wxGridSelectionModes selmode
)
4757 wxCHECK_MSG( !m_created
,
4759 wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
4761 return SetTable(new wxGridStringTable(numRows
, numCols
), true, selmode
);
4764 void wxGrid::SetSelectionMode(wxGridSelectionModes selmode
)
4766 wxCHECK_RET( m_created
,
4767 wxT("Called wxGrid::SetSelectionMode() before calling CreateGrid()") );
4769 m_selection
->SetSelectionMode( selmode
);
4772 wxGrid::wxGridSelectionModes
wxGrid::GetSelectionMode() const
4774 wxCHECK_MSG( m_created
, wxGridSelectCells
,
4775 wxT("Called wxGrid::GetSelectionMode() before calling CreateGrid()") );
4777 return m_selection
->GetSelectionMode();
4781 wxGrid::SetTable(wxGridTableBase
*table
,
4783 wxGrid::wxGridSelectionModes selmode
)
4785 bool checkSelection
= false;
4788 // stop all processing
4793 m_table
->SetView(0);
4805 checkSelection
= true;
4807 // kill row and column size arrays
4808 m_colWidths
.Empty();
4809 m_colRights
.Empty();
4810 m_rowHeights
.Empty();
4811 m_rowBottoms
.Empty();
4816 m_numRows
= table
->GetNumberRows();
4817 m_numCols
= table
->GetNumberCols();
4819 if ( m_useNativeHeader
)
4820 GetGridColHeader()->SetColumnCount(m_numCols
);
4823 m_table
->SetView( this );
4824 m_ownTable
= takeOwnership
;
4825 m_selection
= new wxGridSelection( this, selmode
);
4828 // If the newly set table is smaller than the
4829 // original one current cell and selection regions
4830 // might be invalid,
4831 m_selectedBlockCorner
= wxGridNoCellCoords
;
4832 m_currentCellCoords
=
4833 wxGridCellCoords(wxMin(m_numRows
, m_currentCellCoords
.GetRow()),
4834 wxMin(m_numCols
, m_currentCellCoords
.GetCol()));
4835 if (m_selectedBlockTopLeft
.GetRow() >= m_numRows
||
4836 m_selectedBlockTopLeft
.GetCol() >= m_numCols
)
4838 m_selectedBlockTopLeft
= wxGridNoCellCoords
;
4839 m_selectedBlockBottomRight
= wxGridNoCellCoords
;
4842 m_selectedBlockBottomRight
=
4843 wxGridCellCoords(wxMin(m_numRows
,
4844 m_selectedBlockBottomRight
.GetRow()),
4846 m_selectedBlockBottomRight
.GetCol()));
4860 m_cornerLabelWin
= NULL
;
4861 m_rowLabelWin
= NULL
;
4869 m_defaultCellAttr
= NULL
;
4870 m_typeRegistry
= NULL
;
4871 m_winCapture
= NULL
;
4873 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
4874 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
4877 m_attrCache
.row
= -1;
4878 m_attrCache
.col
= -1;
4879 m_attrCache
.attr
= NULL
;
4881 m_labelFont
= GetFont();
4882 m_labelFont
.SetWeight( wxBOLD
);
4884 m_rowLabelHorizAlign
= wxALIGN_CENTRE
;
4885 m_rowLabelVertAlign
= wxALIGN_CENTRE
;
4887 m_colLabelHorizAlign
= wxALIGN_CENTRE
;
4888 m_colLabelVertAlign
= wxALIGN_CENTRE
;
4889 m_colLabelTextOrientation
= wxHORIZONTAL
;
4891 m_defaultColWidth
= WXGRID_DEFAULT_COL_WIDTH
;
4892 m_defaultRowHeight
= 0; // this will be initialized after creation
4894 m_minAcceptableColWidth
= WXGRID_MIN_COL_WIDTH
;
4895 m_minAcceptableRowHeight
= WXGRID_MIN_ROW_HEIGHT
;
4897 m_gridLineColour
= wxColour( 192,192,192 );
4898 m_gridLinesEnabled
= true;
4899 m_gridLinesClipHorz
=
4900 m_gridLinesClipVert
= true;
4901 m_cellHighlightColour
= *wxBLACK
;
4902 m_cellHighlightPenWidth
= 2;
4903 m_cellHighlightROPenWidth
= 1;
4905 m_canDragColMove
= false;
4907 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
4908 m_winCapture
= NULL
;
4909 m_canDragRowSize
= true;
4910 m_canDragColSize
= true;
4911 m_canDragGridSize
= true;
4912 m_canDragCell
= false;
4914 m_dragRowOrCol
= -1;
4915 m_isDragging
= false;
4916 m_startDragPos
= wxDefaultPosition
;
4918 m_sortCol
= wxNOT_FOUND
;
4919 m_sortIsAscending
= true;
4922 m_nativeColumnLabels
= false;
4924 m_waitForSlowClick
= false;
4926 m_rowResizeCursor
= wxCursor( wxCURSOR_SIZENS
);
4927 m_colResizeCursor
= wxCursor( wxCURSOR_SIZEWE
);
4929 m_currentCellCoords
= wxGridNoCellCoords
;
4931 m_selectedBlockTopLeft
=
4932 m_selectedBlockBottomRight
=
4933 m_selectedBlockCorner
= wxGridNoCellCoords
;
4935 m_selectionBackground
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
);
4936 m_selectionForeground
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
4938 m_editable
= true; // default for whole grid
4940 m_inOnKeyDown
= false;
4946 m_scrollLineX
= GRID_SCROLL_LINE_X
;
4947 m_scrollLineY
= GRID_SCROLL_LINE_Y
;
4950 // ----------------------------------------------------------------------------
4951 // the idea is to call these functions only when necessary because they create
4952 // quite big arrays which eat memory mostly unnecessary - in particular, if
4953 // default widths/heights are used for all rows/columns, we may not use these
4956 // with some extra code, it should be possible to only store the widths/heights
4957 // different from default ones (resulting in space savings for huge grids) but
4958 // this is not done currently
4959 // ----------------------------------------------------------------------------
4961 void wxGrid::InitRowHeights()
4963 m_rowHeights
.Empty();
4964 m_rowBottoms
.Empty();
4966 m_rowHeights
.Alloc( m_numRows
);
4967 m_rowBottoms
.Alloc( m_numRows
);
4969 m_rowHeights
.Add( m_defaultRowHeight
, m_numRows
);
4972 for ( int i
= 0; i
< m_numRows
; i
++ )
4974 rowBottom
+= m_defaultRowHeight
;
4975 m_rowBottoms
.Add( rowBottom
);
4979 void wxGrid::InitColWidths()
4981 m_colWidths
.Empty();
4982 m_colRights
.Empty();
4984 m_colWidths
.Alloc( m_numCols
);
4985 m_colRights
.Alloc( m_numCols
);
4987 m_colWidths
.Add( m_defaultColWidth
, m_numCols
);
4989 for ( int i
= 0; i
< m_numCols
; i
++ )
4991 int colRight
= ( GetColPos( i
) + 1 ) * m_defaultColWidth
;
4992 m_colRights
.Add( colRight
);
4996 int wxGrid::GetColWidth(int col
) const
4998 return m_colWidths
.IsEmpty() ? m_defaultColWidth
: m_colWidths
[col
];
5001 int wxGrid::GetColLeft(int col
) const
5003 return m_colRights
.IsEmpty() ? GetColPos( col
) * m_defaultColWidth
5004 : m_colRights
[col
] - m_colWidths
[col
];
5007 int wxGrid::GetColRight(int col
) const
5009 return m_colRights
.IsEmpty() ? (GetColPos( col
) + 1) * m_defaultColWidth
5013 int wxGrid::GetRowHeight(int row
) const
5015 return m_rowHeights
.IsEmpty() ? m_defaultRowHeight
: m_rowHeights
[row
];
5018 int wxGrid::GetRowTop(int row
) const
5020 return m_rowBottoms
.IsEmpty() ? row
* m_defaultRowHeight
5021 : m_rowBottoms
[row
] - m_rowHeights
[row
];
5024 int wxGrid::GetRowBottom(int row
) const
5026 return m_rowBottoms
.IsEmpty() ? (row
+ 1) * m_defaultRowHeight
5027 : m_rowBottoms
[row
];
5030 void wxGrid::CalcDimensions()
5032 // compute the size of the scrollable area
5033 int w
= m_numCols
> 0 ? GetColRight(GetColAt(m_numCols
- 1)) : 0;
5034 int h
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) : 0;
5039 // take into account editor if shown
5040 if ( IsCellEditControlShown() )
5043 int r
= m_currentCellCoords
.GetRow();
5044 int c
= m_currentCellCoords
.GetCol();
5045 int x
= GetColLeft(c
);
5046 int y
= GetRowTop(r
);
5048 // how big is the editor
5049 wxGridCellAttr
* attr
= GetCellAttr(r
, c
);
5050 wxGridCellEditor
* editor
= attr
->GetEditor(this, r
, c
);
5051 editor
->GetControl()->GetSize(&w2
, &h2
);
5062 // preserve (more or less) the previous position
5064 GetViewStart( &x
, &y
);
5066 // ensure the position is valid for the new scroll ranges
5068 x
= wxMax( w
- 1, 0 );
5070 y
= wxMax( h
- 1, 0 );
5072 // update the virtual size and refresh the scrollbars to reflect it
5073 m_gridWin
->SetVirtualSize(w
, h
);
5077 // if our OnSize() hadn't been called (it would if we have scrollbars), we
5078 // still must reposition the children
5082 wxSize
wxGrid::GetSizeAvailableForScrollTarget(const wxSize
& size
)
5084 wxSize
sizeGridWin(size
);
5085 sizeGridWin
.x
-= m_rowLabelWidth
;
5086 sizeGridWin
.y
-= m_colLabelHeight
;
5091 void wxGrid::CalcWindowSizes()
5093 // escape if the window is has not been fully created yet
5095 if ( m_cornerLabelWin
== NULL
)
5099 GetClientSize( &cw
, &ch
);
5101 // the grid may be too small to have enough space for the labels yet, don't
5102 // size the windows to negative sizes in this case
5103 int gw
= cw
- m_rowLabelWidth
;
5104 int gh
= ch
- m_colLabelHeight
;
5110 if ( m_cornerLabelWin
&& m_cornerLabelWin
->IsShown() )
5111 m_cornerLabelWin
->SetSize( 0, 0, m_rowLabelWidth
, m_colLabelHeight
);
5113 if ( m_colWindow
&& m_colWindow
->IsShown() )
5114 m_colWindow
->SetSize( m_rowLabelWidth
, 0, gw
, m_colLabelHeight
);
5116 if ( m_rowLabelWin
&& m_rowLabelWin
->IsShown() )
5117 m_rowLabelWin
->SetSize( 0, m_colLabelHeight
, m_rowLabelWidth
, gh
);
5119 if ( m_gridWin
&& m_gridWin
->IsShown() )
5120 m_gridWin
->SetSize( m_rowLabelWidth
, m_colLabelHeight
, gw
, gh
);
5123 // this is called when the grid table sends a message
5124 // to indicate that it has been redimensioned
5126 bool wxGrid::Redimension( wxGridTableMessage
& msg
)
5129 bool result
= false;
5131 // Clear the attribute cache as the attribute might refer to a different
5132 // cell than stored in the cache after adding/removing rows/columns.
5135 // By the same reasoning, the editor should be dismissed if columns are
5136 // added or removed. And for consistency, it should IMHO always be
5137 // removed, not only if the cell "underneath" it actually changes.
5138 // For now, I intentionally do not save the editor's content as the
5139 // cell it might want to save that stuff to might no longer exist.
5140 HideCellEditControl();
5142 switch ( msg
.GetId() )
5144 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
5146 size_t pos
= msg
.GetCommandInt();
5147 int numRows
= msg
.GetCommandInt2();
5149 m_numRows
+= numRows
;
5151 if ( !m_rowHeights
.IsEmpty() )
5153 m_rowHeights
.Insert( m_defaultRowHeight
, pos
, numRows
);
5154 m_rowBottoms
.Insert( 0, pos
, numRows
);
5158 bottom
= m_rowBottoms
[pos
- 1];
5160 for ( i
= pos
; i
< m_numRows
; i
++ )
5162 bottom
+= m_rowHeights
[i
];
5163 m_rowBottoms
[i
] = bottom
;
5167 if ( m_currentCellCoords
== wxGridNoCellCoords
)
5169 // if we have just inserted cols into an empty grid the current
5170 // cell will be undefined...
5172 SetCurrentCell( 0, 0 );
5176 m_selection
->UpdateRows( pos
, numRows
);
5177 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
5179 attrProvider
->UpdateAttrRows( pos
, numRows
);
5181 if ( !GetBatchCount() )
5184 m_rowLabelWin
->Refresh();
5190 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
5192 int numRows
= msg
.GetCommandInt();
5193 int oldNumRows
= m_numRows
;
5194 m_numRows
+= numRows
;
5196 if ( !m_rowHeights
.IsEmpty() )
5198 m_rowHeights
.Add( m_defaultRowHeight
, numRows
);
5199 m_rowBottoms
.Add( 0, numRows
);
5202 if ( oldNumRows
> 0 )
5203 bottom
= m_rowBottoms
[oldNumRows
- 1];
5205 for ( i
= oldNumRows
; i
< m_numRows
; i
++ )
5207 bottom
+= m_rowHeights
[i
];
5208 m_rowBottoms
[i
] = bottom
;
5212 if ( m_currentCellCoords
== wxGridNoCellCoords
)
5214 // if we have just inserted cols into an empty grid the current
5215 // cell will be undefined...
5217 SetCurrentCell( 0, 0 );
5220 if ( !GetBatchCount() )
5223 m_rowLabelWin
->Refresh();
5229 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
5231 size_t pos
= msg
.GetCommandInt();
5232 int numRows
= msg
.GetCommandInt2();
5233 m_numRows
-= numRows
;
5235 if ( !m_rowHeights
.IsEmpty() )
5237 m_rowHeights
.RemoveAt( pos
, numRows
);
5238 m_rowBottoms
.RemoveAt( pos
, numRows
);
5241 for ( i
= 0; i
< m_numRows
; i
++ )
5243 h
+= m_rowHeights
[i
];
5244 m_rowBottoms
[i
] = h
;
5250 m_currentCellCoords
= wxGridNoCellCoords
;
5254 if ( m_currentCellCoords
.GetRow() >= m_numRows
)
5255 m_currentCellCoords
.Set( 0, 0 );
5259 m_selection
->UpdateRows( pos
, -((int)numRows
) );
5260 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
5263 attrProvider
->UpdateAttrRows( pos
, -((int)numRows
) );
5265 // ifdef'd out following patch from Paul Gammans
5267 // No need to touch column attributes, unless we
5268 // removed _all_ rows, in this case, we remove
5269 // all column attributes.
5270 // I hate to do this here, but the
5271 // needed data is not available inside UpdateAttrRows.
5272 if ( !GetNumberRows() )
5273 attrProvider
->UpdateAttrCols( 0, -GetNumberCols() );
5277 if ( !GetBatchCount() )
5280 m_rowLabelWin
->Refresh();
5286 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
5288 size_t pos
= msg
.GetCommandInt();
5289 int numCols
= msg
.GetCommandInt2();
5290 m_numCols
+= numCols
;
5292 if ( m_useNativeHeader
)
5293 GetGridColHeader()->SetColumnCount(m_numCols
);
5295 if ( !m_colAt
.IsEmpty() )
5297 //Shift the column IDs
5299 for ( i
= 0; i
< m_numCols
- numCols
; i
++ )
5301 if ( m_colAt
[i
] >= (int)pos
)
5302 m_colAt
[i
] += numCols
;
5305 m_colAt
.Insert( pos
, pos
, numCols
);
5307 //Set the new columns' positions
5308 for ( i
= pos
+ 1; i
< (int)pos
+ numCols
; i
++ )
5314 if ( !m_colWidths
.IsEmpty() )
5316 m_colWidths
.Insert( m_defaultColWidth
, pos
, numCols
);
5317 m_colRights
.Insert( 0, pos
, numCols
);
5321 right
= m_colRights
[GetColAt( pos
- 1 )];
5324 for ( colPos
= pos
; colPos
< m_numCols
; colPos
++ )
5326 i
= GetColAt( colPos
);
5328 right
+= m_colWidths
[i
];
5329 m_colRights
[i
] = right
;
5333 if ( m_currentCellCoords
== wxGridNoCellCoords
)
5335 // if we have just inserted cols into an empty grid the current
5336 // cell will be undefined...
5338 SetCurrentCell( 0, 0 );
5342 m_selection
->UpdateCols( pos
, numCols
);
5343 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
5345 attrProvider
->UpdateAttrCols( pos
, numCols
);
5346 if ( !GetBatchCount() )
5349 m_colWindow
->Refresh();
5355 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
5357 int numCols
= msg
.GetCommandInt();
5358 int oldNumCols
= m_numCols
;
5359 m_numCols
+= numCols
;
5360 if ( m_useNativeHeader
)
5361 GetGridColHeader()->SetColumnCount(m_numCols
);
5363 if ( !m_colAt
.IsEmpty() )
5365 m_colAt
.Add( 0, numCols
);
5367 //Set the new columns' positions
5369 for ( i
= oldNumCols
; i
< m_numCols
; i
++ )
5375 if ( !m_colWidths
.IsEmpty() )
5377 m_colWidths
.Add( m_defaultColWidth
, numCols
);
5378 m_colRights
.Add( 0, numCols
);
5381 if ( oldNumCols
> 0 )
5382 right
= m_colRights
[GetColAt( oldNumCols
- 1 )];
5385 for ( colPos
= oldNumCols
; colPos
< m_numCols
; colPos
++ )
5387 i
= GetColAt( colPos
);
5389 right
+= m_colWidths
[i
];
5390 m_colRights
[i
] = right
;
5394 if ( m_currentCellCoords
== wxGridNoCellCoords
)
5396 // if we have just inserted cols into an empty grid the current
5397 // cell will be undefined...
5399 SetCurrentCell( 0, 0 );
5401 if ( !GetBatchCount() )
5404 m_colWindow
->Refresh();
5410 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
5412 size_t pos
= msg
.GetCommandInt();
5413 int numCols
= msg
.GetCommandInt2();
5414 m_numCols
-= numCols
;
5415 if ( m_useNativeHeader
)
5416 GetGridColHeader()->SetColumnCount(m_numCols
);
5418 if ( !m_colAt
.IsEmpty() )
5420 int colID
= GetColAt( pos
);
5422 m_colAt
.RemoveAt( pos
, numCols
);
5424 //Shift the column IDs
5426 for ( colPos
= 0; colPos
< m_numCols
; colPos
++ )
5428 if ( m_colAt
[colPos
] > colID
)
5429 m_colAt
[colPos
] -= numCols
;
5433 if ( !m_colWidths
.IsEmpty() )
5435 m_colWidths
.RemoveAt( pos
, numCols
);
5436 m_colRights
.RemoveAt( pos
, numCols
);
5440 for ( colPos
= 0; colPos
< m_numCols
; colPos
++ )
5442 i
= GetColAt( colPos
);
5444 w
+= m_colWidths
[i
];
5451 m_currentCellCoords
= wxGridNoCellCoords
;
5455 if ( m_currentCellCoords
.GetCol() >= m_numCols
)
5456 m_currentCellCoords
.Set( 0, 0 );
5460 m_selection
->UpdateCols( pos
, -((int)numCols
) );
5461 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
5464 attrProvider
->UpdateAttrCols( pos
, -((int)numCols
) );
5466 // ifdef'd out following patch from Paul Gammans
5468 // No need to touch row attributes, unless we
5469 // removed _all_ columns, in this case, we remove
5470 // all row attributes.
5471 // I hate to do this here, but the
5472 // needed data is not available inside UpdateAttrCols.
5473 if ( !GetNumberCols() )
5474 attrProvider
->UpdateAttrRows( 0, -GetNumberRows() );
5478 if ( !GetBatchCount() )
5481 m_colWindow
->Refresh();
5488 if (result
&& !GetBatchCount() )
5489 m_gridWin
->Refresh();
5494 wxArrayInt
wxGrid::CalcRowLabelsExposed( const wxRegion
& reg
) const
5496 wxRegionIterator
iter( reg
);
5499 wxArrayInt rowlabels
;
5506 // TODO: remove this when we can...
5507 // There is a bug in wxMotif that gives garbage update
5508 // rectangles if you jump-scroll a long way by clicking the
5509 // scrollbar with middle button. This is a work-around
5511 #if defined(__WXMOTIF__)
5513 m_gridWin
->GetClientSize( &cw
, &ch
);
5514 if ( r
.GetTop() > ch
)
5516 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
5519 // logical bounds of update region
5522 CalcUnscrolledPosition( 0, r
.GetTop(), &dummy
, &top
);
5523 CalcUnscrolledPosition( 0, r
.GetBottom(), &dummy
, &bottom
);
5525 // find the row labels within these bounds
5528 for ( row
= internalYToRow(top
); row
< m_numRows
; row
++ )
5530 if ( GetRowBottom(row
) < top
)
5533 if ( GetRowTop(row
) > bottom
)
5536 rowlabels
.Add( row
);
5545 wxArrayInt
wxGrid::CalcColLabelsExposed( const wxRegion
& reg
) const
5547 wxRegionIterator
iter( reg
);
5550 wxArrayInt colLabels
;
5557 // TODO: remove this when we can...
5558 // There is a bug in wxMotif that gives garbage update
5559 // rectangles if you jump-scroll a long way by clicking the
5560 // scrollbar with middle button. This is a work-around
5562 #if defined(__WXMOTIF__)
5564 m_gridWin
->GetClientSize( &cw
, &ch
);
5565 if ( r
.GetLeft() > cw
)
5567 r
.SetRight( wxMin( r
.GetRight(), cw
) );
5570 // logical bounds of update region
5573 CalcUnscrolledPosition( r
.GetLeft(), 0, &left
, &dummy
);
5574 CalcUnscrolledPosition( r
.GetRight(), 0, &right
, &dummy
);
5576 // find the cells within these bounds
5580 for ( colPos
= GetColPos( internalXToCol(left
) ); colPos
< m_numCols
; colPos
++ )
5582 col
= GetColAt( colPos
);
5584 if ( GetColRight(col
) < left
)
5587 if ( GetColLeft(col
) > right
)
5590 colLabels
.Add( col
);
5599 wxGridCellCoordsArray
wxGrid::CalcCellsExposed( const wxRegion
& reg
) const
5601 wxRegionIterator
iter( reg
);
5604 wxGridCellCoordsArray cellsExposed
;
5606 int left
, top
, right
, bottom
;
5611 // TODO: remove this when we can...
5612 // There is a bug in wxMotif that gives garbage update
5613 // rectangles if you jump-scroll a long way by clicking the
5614 // scrollbar with middle button. This is a work-around
5616 #if defined(__WXMOTIF__)
5618 m_gridWin
->GetClientSize( &cw
, &ch
);
5619 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
5620 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
5621 r
.SetRight( wxMin( r
.GetRight(), cw
) );
5622 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
5625 // logical bounds of update region
5627 CalcUnscrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
5628 CalcUnscrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
5630 // find the cells within these bounds
5632 for ( int row
= internalYToRow(top
); row
< m_numRows
; row
++ )
5634 if ( GetRowBottom(row
) <= top
)
5637 if ( GetRowTop(row
) > bottom
)
5640 // add all dirty cells in this row: notice that the columns which
5641 // are dirty don't depend on the row so we compute them only once
5642 // for the first dirty row and then reuse for all the next ones
5645 // do determine the dirty columns
5646 for ( int pos
= XToPos(left
); pos
<= XToPos(right
); pos
++ )
5647 cols
.push_back(GetColAt(pos
));
5649 // if there are no dirty columns at all, nothing to do
5654 const size_t count
= cols
.size();
5655 for ( size_t n
= 0; n
< count
; n
++ )
5656 cellsExposed
.Add(wxGridCellCoords(row
, cols
[n
]));
5662 return cellsExposed
;
5666 void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent
& event
)
5669 wxPoint
pos( event
.GetPosition() );
5670 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
5672 if ( event
.Dragging() )
5676 m_isDragging
= true;
5677 m_rowLabelWin
->CaptureMouse();
5680 if ( event
.LeftIsDown() )
5682 switch ( m_cursorMode
)
5684 case WXGRID_CURSOR_RESIZE_ROW
:
5686 int cw
, ch
, left
, dummy
;
5687 m_gridWin
->GetClientSize( &cw
, &ch
);
5688 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
5690 wxClientDC
dc( m_gridWin
);
5693 GetRowTop(m_dragRowOrCol
) +
5694 GetRowMinimalHeight(m_dragRowOrCol
) );
5695 dc
.SetLogicalFunction(wxINVERT
);
5696 if ( m_dragLastPos
>= 0 )
5698 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
5700 dc
.DrawLine( left
, y
, left
+cw
, y
);
5705 case WXGRID_CURSOR_SELECT_ROW
:
5707 if ( (row
= YToRow( y
)) >= 0 )
5710 m_selection
->SelectRow(row
, event
);
5715 // default label to suppress warnings about "enumeration value
5716 // 'xxx' not handled in switch
5724 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
5729 if (m_rowLabelWin
->HasCapture())
5730 m_rowLabelWin
->ReleaseMouse();
5731 m_isDragging
= false;
5734 // ------------ Entering or leaving the window
5736 if ( event
.Entering() || event
.Leaving() )
5738 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
5741 // ------------ Left button pressed
5743 else if ( event
.LeftDown() )
5745 // don't send a label click event for a hit on the
5746 // edge of the row label - this is probably the user
5747 // wanting to resize the row
5749 if ( YToEdgeOfRow(y
) < 0 )
5753 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, row
, -1, event
) )
5755 if ( !event
.ShiftDown() && !event
.CmdDown() )
5759 if ( event
.ShiftDown() )
5761 m_selection
->SelectBlock
5763 m_currentCellCoords
.GetRow(), 0,
5764 row
, GetNumberCols() - 1,
5770 m_selection
->SelectRow(row
, event
);
5774 ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW
, m_rowLabelWin
);
5779 // starting to drag-resize a row
5780 if ( CanDragRowSize() )
5781 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
);
5785 // ------------ Left double click
5787 else if (event
.LeftDClick() )
5789 row
= YToEdgeOfRow(y
);
5794 !SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, row
, -1, event
) )
5796 // no default action at the moment
5801 // adjust row height depending on label text
5802 AutoSizeRowLabelSize( row
);
5804 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, GetColLabelWindow());
5809 // ------------ Left button released
5811 else if ( event
.LeftUp() )
5813 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
5815 DoEndDragResizeRow();
5817 // Note: we are ending the event *after* doing
5818 // default processing in this case
5820 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
5823 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
5827 // ------------ Right button down
5829 else if ( event
.RightDown() )
5833 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, row
, -1, event
) )
5835 // no default action at the moment
5839 // ------------ Right double click
5841 else if ( event
.RightDClick() )
5845 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, row
, -1, event
) )
5847 // no default action at the moment
5851 // ------------ No buttons down and mouse moving
5853 else if ( event
.Moving() )
5855 m_dragRowOrCol
= YToEdgeOfRow( y
);
5856 if ( m_dragRowOrCol
>= 0 )
5858 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5860 // don't capture the mouse yet
5861 if ( CanDragRowSize() )
5862 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
, false);
5865 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5867 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
, false);
5872 void wxGrid::UpdateColumnSortingIndicator(int col
)
5874 wxCHECK_RET( col
!= wxNOT_FOUND
, "invalid column index" );
5876 if ( m_useNativeHeader
)
5877 GetGridColHeader()->UpdateColumn(col
);
5878 else if ( m_nativeColumnLabels
)
5879 m_colWindow
->Refresh();
5880 //else: sorting indicator display not yet implemented in grid version
5883 void wxGrid::SetSortingColumn(int col
, bool ascending
)
5885 if ( col
== m_sortCol
)
5887 // we are already using this column for sorting (or not sorting at all)
5888 // but we might still change the sorting order, check for it
5889 if ( m_sortCol
!= wxNOT_FOUND
&& ascending
!= m_sortIsAscending
)
5891 m_sortIsAscending
= ascending
;
5893 UpdateColumnSortingIndicator(m_sortCol
);
5896 else // we're changing the column used for sorting
5898 const int sortColOld
= m_sortCol
;
5900 // change it before updating the column as we want GetSortingColumn()
5901 // to return the correct new value
5904 if ( sortColOld
!= wxNOT_FOUND
)
5905 UpdateColumnSortingIndicator(sortColOld
);
5907 if ( m_sortCol
!= wxNOT_FOUND
)
5909 m_sortIsAscending
= ascending
;
5910 UpdateColumnSortingIndicator(m_sortCol
);
5915 void wxGrid::DoColHeaderClick(int col
)
5917 // we consider that the grid was resorted if this event is processed and
5919 if ( SendEvent(wxEVT_GRID_COL_SORT
, -1, col
) == 1 )
5921 SetSortingColumn(col
, IsSortingBy(col
) ? !m_sortIsAscending
: true);
5926 void wxGrid::DoStartResizeCol(int col
)
5928 m_dragRowOrCol
= col
;
5930 DoUpdateResizeColWidth(GetColWidth(m_dragRowOrCol
));
5933 void wxGrid::DoUpdateResizeCol(int x
)
5935 int cw
, ch
, dummy
, top
;
5936 m_gridWin
->GetClientSize( &cw
, &ch
);
5937 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
5939 wxClientDC
dc( m_gridWin
);
5942 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) + GetColMinimalWidth(m_dragRowOrCol
));
5943 dc
.SetLogicalFunction(wxINVERT
);
5944 if ( m_dragLastPos
>= 0 )
5946 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ ch
);
5948 dc
.DrawLine( x
, top
, x
, top
+ ch
);
5952 void wxGrid::DoUpdateResizeColWidth(int w
)
5954 DoUpdateResizeCol(GetColLeft(m_dragRowOrCol
) + w
);
5957 void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent
& event
)
5960 wxPoint
pos( event
.GetPosition() );
5961 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
5963 int col
= XToCol(x
);
5964 if ( event
.Dragging() )
5968 m_isDragging
= true;
5969 GetColLabelWindow()->CaptureMouse();
5971 if ( m_cursorMode
== WXGRID_CURSOR_MOVE_COL
&& col
!= -1 )
5972 DoStartMoveCol(col
);
5975 if ( event
.LeftIsDown() )
5977 switch ( m_cursorMode
)
5979 case WXGRID_CURSOR_RESIZE_COL
:
5980 DoUpdateResizeCol(x
);
5983 case WXGRID_CURSOR_SELECT_COL
:
5988 m_selection
->SelectCol(col
, event
);
5993 case WXGRID_CURSOR_MOVE_COL
:
5995 int posNew
= XToPos(x
);
5996 int colNew
= GetColAt(posNew
);
5998 // determine the position of the drop marker
6000 if ( x
>= GetColLeft(colNew
) + (GetColWidth(colNew
) / 2) )
6001 markerX
= GetColRight(colNew
);
6003 markerX
= GetColLeft(colNew
);
6005 if ( markerX
!= m_dragLastPos
)
6007 wxClientDC
dc( GetColLabelWindow() );
6011 GetColLabelWindow()->GetClientSize( &cw
, &ch
);
6015 //Clean up the last indicator
6016 if ( m_dragLastPos
>= 0 )
6018 wxPen
pen( GetColLabelWindow()->GetBackgroundColour(), 2 );
6020 dc
.DrawLine( m_dragLastPos
+ 1, 0, m_dragLastPos
+ 1, ch
);
6021 dc
.SetPen(wxNullPen
);
6023 if ( XToCol( m_dragLastPos
) != -1 )
6024 DrawColLabel( dc
, XToCol( m_dragLastPos
) );
6027 const wxColour
*color
;
6028 //Moving to the same place? Don't draw a marker
6029 if ( colNew
== m_dragRowOrCol
)
6030 color
= wxLIGHT_GREY
;
6035 wxPen
pen( *color
, 2 );
6038 dc
.DrawLine( markerX
, 0, markerX
, ch
);
6040 dc
.SetPen(wxNullPen
);
6042 m_dragLastPos
= markerX
- 1;
6047 // default label to suppress warnings about "enumeration value
6048 // 'xxx' not handled in switch
6056 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
6061 if (GetColLabelWindow()->HasCapture())
6062 GetColLabelWindow()->ReleaseMouse();
6063 m_isDragging
= false;
6066 // ------------ Entering or leaving the window
6068 if ( event
.Entering() || event
.Leaving() )
6070 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, GetColLabelWindow());
6073 // ------------ Left button pressed
6075 else if ( event
.LeftDown() )
6077 // don't send a label click event for a hit on the
6078 // edge of the col label - this is probably the user
6079 // wanting to resize the col
6081 if ( XToEdgeOfCol(x
) < 0 )
6084 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, col
, event
) )
6086 if ( m_canDragColMove
)
6088 //Show button as pressed
6089 wxClientDC
dc( GetColLabelWindow() );
6090 int colLeft
= GetColLeft( col
);
6091 int colRight
= GetColRight( col
) - 1;
6092 dc
.SetPen( wxPen( GetColLabelWindow()->GetBackgroundColour(), 1 ) );
6093 dc
.DrawLine( colLeft
, 1, colLeft
, m_colLabelHeight
-1 );
6094 dc
.DrawLine( colLeft
, 1, colRight
, 1 );
6096 ChangeCursorMode(WXGRID_CURSOR_MOVE_COL
, GetColLabelWindow());
6100 if ( !event
.ShiftDown() && !event
.CmdDown() )
6104 if ( event
.ShiftDown() )
6106 m_selection
->SelectBlock
6108 0, m_currentCellCoords
.GetCol(),
6109 GetNumberRows() - 1, col
,
6115 m_selection
->SelectCol(col
, event
);
6119 ChangeCursorMode(WXGRID_CURSOR_SELECT_COL
, GetColLabelWindow());
6125 // starting to drag-resize a col
6127 if ( CanDragColSize() )
6128 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, GetColLabelWindow());
6132 // ------------ Left double click
6134 if ( event
.LeftDClick() )
6136 const int colEdge
= XToEdgeOfCol(x
);
6137 if ( colEdge
== -1 )
6140 ! SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, col
, event
) )
6142 // no default action at the moment
6147 // adjust column width depending on label text
6148 AutoSizeColLabelSize( colEdge
);
6150 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, GetColLabelWindow());
6155 // ------------ Left button released
6157 else if ( event
.LeftUp() )
6159 switch ( m_cursorMode
)
6161 case WXGRID_CURSOR_RESIZE_COL
:
6162 DoEndDragResizeCol();
6165 case WXGRID_CURSOR_MOVE_COL
:
6166 if ( m_dragLastPos
== -1 || col
== m_dragRowOrCol
)
6168 // the column didn't actually move anywhere
6170 DoColHeaderClick(col
);
6171 m_colWindow
->Refresh(); // "unpress" the column
6175 DoEndMoveCol(XToPos(x
));
6179 case WXGRID_CURSOR_SELECT_COL
:
6180 case WXGRID_CURSOR_SELECT_CELL
:
6181 case WXGRID_CURSOR_RESIZE_ROW
:
6182 case WXGRID_CURSOR_SELECT_ROW
:
6184 DoColHeaderClick(col
);
6188 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, GetColLabelWindow());
6192 // ------------ Right button down
6194 else if ( event
.RightDown() )
6197 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, col
, event
) )
6199 // no default action at the moment
6203 // ------------ Right double click
6205 else if ( event
.RightDClick() )
6208 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, col
, event
) )
6210 // no default action at the moment
6214 // ------------ No buttons down and mouse moving
6216 else if ( event
.Moving() )
6218 m_dragRowOrCol
= XToEdgeOfCol( x
);
6219 if ( m_dragRowOrCol
>= 0 )
6221 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
6223 // don't capture the cursor yet
6224 if ( CanDragColSize() )
6225 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, GetColLabelWindow(), false);
6228 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
6230 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, GetColLabelWindow(), false);
6235 void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent
& event
)
6237 if ( event
.LeftDown() )
6239 // indicate corner label by having both row and
6242 if ( !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, -1, event
) )
6247 else if ( event
.LeftDClick() )
6249 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, -1, event
);
6251 else if ( event
.RightDown() )
6253 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, -1, event
) )
6255 // no default action at the moment
6258 else if ( event
.RightDClick() )
6260 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, -1, event
) )
6262 // no default action at the moment
6267 void wxGrid::CancelMouseCapture()
6269 // cancel operation currently in progress, whatever it is
6272 m_isDragging
= false;
6273 m_startDragPos
= wxDefaultPosition
;
6275 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
6276 m_winCapture
->SetCursor( *wxSTANDARD_CURSOR
);
6277 m_winCapture
= NULL
;
6279 // remove traces of whatever we drew on screen
6284 void wxGrid::ChangeCursorMode(CursorMode mode
,
6289 static const wxChar
*cursorModes
[] =
6299 wxLogTrace(_T("grid"),
6300 _T("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
6301 win
== m_colWindow
? _T("colLabelWin")
6302 : win
? _T("rowLabelWin")
6304 cursorModes
[m_cursorMode
], cursorModes
[mode
]);
6307 if ( mode
== m_cursorMode
&&
6308 win
== m_winCapture
&&
6309 captureMouse
== (m_winCapture
!= NULL
))
6314 // by default use the grid itself
6320 m_winCapture
->ReleaseMouse();
6321 m_winCapture
= NULL
;
6324 m_cursorMode
= mode
;
6326 switch ( m_cursorMode
)
6328 case WXGRID_CURSOR_RESIZE_ROW
:
6329 win
->SetCursor( m_rowResizeCursor
);
6332 case WXGRID_CURSOR_RESIZE_COL
:
6333 win
->SetCursor( m_colResizeCursor
);
6336 case WXGRID_CURSOR_MOVE_COL
:
6337 win
->SetCursor( wxCursor(wxCURSOR_HAND
) );
6341 win
->SetCursor( *wxSTANDARD_CURSOR
);
6345 // we need to capture mouse when resizing
6346 bool resize
= m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
||
6347 m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
;
6349 if ( captureMouse
&& resize
)
6351 win
->CaptureMouse();
6356 // ----------------------------------------------------------------------------
6357 // grid mouse event processing
6358 // ----------------------------------------------------------------------------
6361 wxGrid::DoGridCellDrag(wxMouseEvent
& event
,
6362 const wxGridCellCoords
& coords
,
6365 if ( coords
== wxGridNoCellCoords
)
6366 return; // we're outside any valid cell
6368 // Hide the edit control, so it won't interfere with drag-shrinking.
6369 if ( IsCellEditControlShown() )
6371 HideCellEditControl();
6372 SaveEditControlValue();
6375 switch ( event
.GetModifiers() )
6378 if ( m_selectedBlockCorner
== wxGridNoCellCoords
)
6379 m_selectedBlockCorner
= coords
;
6380 UpdateBlockBeingSelected(m_selectedBlockCorner
, coords
);
6384 if ( CanDragCell() )
6388 if ( m_selectedBlockCorner
== wxGridNoCellCoords
)
6389 m_selectedBlockCorner
= coords
;
6391 SendEvent(wxEVT_GRID_CELL_BEGIN_DRAG
, coords
, event
);
6396 UpdateBlockBeingSelected(m_currentCellCoords
, coords
);
6400 // we don't handle the other key modifiers
6405 void wxGrid::DoGridLineDrag(wxMouseEvent
& event
, const wxGridOperations
& oper
)
6407 wxClientDC
dc(m_gridWin
);
6409 dc
.SetLogicalFunction(wxINVERT
);
6411 const wxRect
rectWin(CalcUnscrolledPosition(wxPoint(0, 0)),
6412 m_gridWin
->GetClientSize());
6414 // erase the previously drawn line, if any
6415 if ( m_dragLastPos
>= 0 )
6416 oper
.DrawParallelLineInRect(dc
, rectWin
, m_dragLastPos
);
6418 // we need the vertical position for rows and horizontal for columns here
6419 m_dragLastPos
= oper
.Dual().Select(CalcUnscrolledPosition(event
.GetPosition()));
6421 // don't allow resizing beneath the minimal size
6422 const int posMin
= oper
.GetLineStartPos(this, m_dragRowOrCol
) +
6423 oper
.GetMinimalLineSize(this, m_dragRowOrCol
);
6424 if ( m_dragLastPos
< posMin
)
6425 m_dragLastPos
= posMin
;
6427 // and draw it at the new position
6428 oper
.DrawParallelLineInRect(dc
, rectWin
, m_dragLastPos
);
6431 void wxGrid::DoGridDragEvent(wxMouseEvent
& event
, const wxGridCellCoords
& coords
)
6433 if ( !m_isDragging
)
6435 // Don't start doing anything until the mouse has been dragged far
6437 const wxPoint
& pt
= event
.GetPosition();
6438 if ( m_startDragPos
== wxDefaultPosition
)
6440 m_startDragPos
= pt
;
6444 if ( abs(m_startDragPos
.x
- pt
.x
) <= DRAG_SENSITIVITY
&&
6445 abs(m_startDragPos
.y
- pt
.y
) <= DRAG_SENSITIVITY
)
6449 const bool isFirstDrag
= !m_isDragging
;
6450 m_isDragging
= true;
6452 switch ( m_cursorMode
)
6454 case WXGRID_CURSOR_SELECT_CELL
:
6455 DoGridCellDrag(event
, coords
, isFirstDrag
);
6458 case WXGRID_CURSOR_RESIZE_ROW
:
6459 DoGridLineDrag(event
, wxGridRowOperations());
6462 case WXGRID_CURSOR_RESIZE_COL
:
6463 DoGridLineDrag(event
, wxGridColumnOperations());
6472 m_winCapture
= m_gridWin
;
6473 m_winCapture
->CaptureMouse();
6478 wxGrid::DoGridCellLeftDown(wxMouseEvent
& event
,
6479 const wxGridCellCoords
& coords
,
6482 if ( SendEvent(wxEVT_GRID_CELL_LEFT_CLICK
, coords
, event
) )
6484 // event handled by user code, no need to do anything here
6488 if ( !event
.CmdDown() )
6491 if ( event
.ShiftDown() )
6495 m_selection
->SelectBlock(m_currentCellCoords
, coords
, event
);
6496 m_selectedBlockCorner
= coords
;
6499 else if ( XToEdgeOfCol(pos
.x
) < 0 && YToEdgeOfRow(pos
.y
) < 0 )
6501 DisableCellEditControl();
6502 MakeCellVisible( coords
);
6504 if ( event
.CmdDown() )
6508 m_selection
->ToggleCellSelection(coords
, event
);
6511 m_selectedBlockTopLeft
= wxGridNoCellCoords
;
6512 m_selectedBlockBottomRight
= wxGridNoCellCoords
;
6513 m_selectedBlockCorner
= coords
;
6517 m_waitForSlowClick
= m_currentCellCoords
== coords
&&
6518 coords
!= wxGridNoCellCoords
;
6519 SetCurrentCell( coords
);
6525 wxGrid::DoGridCellLeftDClick(wxMouseEvent
& event
,
6526 const wxGridCellCoords
& coords
,
6529 if ( XToEdgeOfCol(pos
.x
) < 0 && YToEdgeOfRow(pos
.y
) < 0 )
6531 if ( !SendEvent(wxEVT_GRID_CELL_LEFT_DCLICK
, coords
, event
) )
6533 // we want double click to select a cell and start editing
6534 // (i.e. to behave in same way as sequence of two slow clicks):
6535 m_waitForSlowClick
= true;
6541 wxGrid::DoGridCellLeftUp(wxMouseEvent
& event
, const wxGridCellCoords
& coords
)
6543 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
6547 m_winCapture
->ReleaseMouse();
6548 m_winCapture
= NULL
;
6551 if ( coords
== m_currentCellCoords
&& m_waitForSlowClick
&& CanEnableCellControl() )
6554 EnableCellEditControl();
6556 wxGridCellAttr
*attr
= GetCellAttr(coords
);
6557 wxGridCellEditor
*editor
= attr
->GetEditor(this, coords
.GetRow(), coords
.GetCol());
6558 editor
->StartingClick();
6562 m_waitForSlowClick
= false;
6564 else if ( m_selectedBlockTopLeft
!= wxGridNoCellCoords
&&
6565 m_selectedBlockBottomRight
!= wxGridNoCellCoords
)
6569 m_selection
->SelectBlock( m_selectedBlockTopLeft
,
6570 m_selectedBlockBottomRight
,
6574 m_selectedBlockTopLeft
= wxGridNoCellCoords
;
6575 m_selectedBlockBottomRight
= wxGridNoCellCoords
;
6577 // Show the edit control, if it has been hidden for
6579 ShowCellEditControl();
6582 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
6584 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6585 DoEndDragResizeRow();
6587 // Note: we are ending the event *after* doing
6588 // default processing in this case
6590 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
6592 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
6594 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6595 DoEndDragResizeCol();
6602 wxGrid::DoGridMouseMoveEvent(wxMouseEvent
& WXUNUSED(event
),
6603 const wxGridCellCoords
& coords
,
6606 if ( coords
.GetRow() < 0 || coords
.GetCol() < 0 )
6608 // out of grid cell area
6609 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6613 int dragRow
= YToEdgeOfRow( pos
.y
);
6614 int dragCol
= XToEdgeOfCol( pos
.x
);
6616 // Dragging on the corner of a cell to resize in both
6617 // directions is not implemented yet...
6619 if ( dragRow
>= 0 && dragCol
>= 0 )
6621 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6627 m_dragRowOrCol
= dragRow
;
6629 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
6631 if ( CanDragRowSize() && CanDragGridSize() )
6632 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, NULL
, false);
6635 // When using the native header window we can only resize the columns by
6636 // dragging the dividers in it because we can't make it enter into the
6637 // column resizing mode programmatically
6638 else if ( dragCol
>= 0 && !m_useNativeHeader
)
6640 m_dragRowOrCol
= dragCol
;
6642 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
6644 if ( CanDragColSize() && CanDragGridSize() )
6645 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, NULL
, false);
6648 else // Neither on a row or col edge
6650 if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
6652 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6657 void wxGrid::ProcessGridCellMouseEvent(wxMouseEvent
& event
)
6659 const wxPoint pos
= CalcUnscrolledPosition(event
.GetPosition());
6661 // coordinates of the cell under mouse
6662 wxGridCellCoords coords
= XYToCell(pos
);
6664 int cell_rows
, cell_cols
;
6665 GetCellSize( coords
.GetRow(), coords
.GetCol(), &cell_rows
, &cell_cols
);
6666 if ( (cell_rows
< 0) || (cell_cols
< 0) )
6668 coords
.SetRow(coords
.GetRow() + cell_rows
);
6669 coords
.SetCol(coords
.GetCol() + cell_cols
);
6672 if ( event
.Dragging() )
6674 if ( event
.LeftIsDown() )
6675 DoGridDragEvent(event
, coords
);
6681 m_isDragging
= false;
6682 m_startDragPos
= wxDefaultPosition
;
6684 // VZ: if we do this, the mode is reset to WXGRID_CURSOR_SELECT_CELL
6685 // immediately after it becomes WXGRID_CURSOR_RESIZE_ROW/COL under
6688 if ( event
.Entering() || event
.Leaving() )
6690 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6691 m_gridWin
->SetCursor( *wxSTANDARD_CURSOR
);
6695 // deal with various button presses
6696 if ( event
.IsButton() )
6698 if ( coords
!= wxGridNoCellCoords
)
6700 DisableCellEditControl();
6702 if ( event
.LeftDown() )
6703 DoGridCellLeftDown(event
, coords
, pos
);
6704 else if ( event
.LeftDClick() )
6705 DoGridCellLeftDClick(event
, coords
, pos
);
6706 else if ( event
.RightDown() )
6707 SendEvent(wxEVT_GRID_CELL_RIGHT_CLICK
, coords
, event
);
6708 else if ( event
.RightDClick() )
6709 SendEvent(wxEVT_GRID_CELL_RIGHT_DCLICK
, coords
, event
);
6712 // this one should be called even if we're not over any cell
6713 if ( event
.LeftUp() )
6715 DoGridCellLeftUp(event
, coords
);
6718 else if ( event
.Moving() )
6720 DoGridMouseMoveEvent(event
, coords
, pos
);
6722 else // unknown mouse event?
6728 void wxGrid::DoEndDragResizeLine(const wxGridOperations
& oper
)
6730 if ( m_dragLastPos
== -1 )
6733 const wxGridOperations
& doper
= oper
.Dual();
6735 const wxSize size
= m_gridWin
->GetClientSize();
6737 const wxPoint ptOrigin
= CalcUnscrolledPosition(wxPoint(0, 0));
6739 // erase the last line we drew
6740 wxClientDC
dc(m_gridWin
);
6742 dc
.SetLogicalFunction(wxINVERT
);
6744 const int posLineStart
= oper
.Select(ptOrigin
);
6745 const int posLineEnd
= oper
.Select(ptOrigin
) + oper
.Select(size
);
6747 oper
.DrawParallelLine(dc
, posLineStart
, posLineEnd
, m_dragLastPos
);
6749 // temporarily hide the edit control before resizing
6750 HideCellEditControl();
6751 SaveEditControlValue();
6753 // do resize the line
6754 const int lineStart
= oper
.GetLineStartPos(this, m_dragRowOrCol
);
6755 oper
.SetLineSize(this, m_dragRowOrCol
,
6756 wxMax(m_dragLastPos
- lineStart
,
6757 oper
.GetMinimalLineSize(this, m_dragRowOrCol
)));
6761 // refresh now if we're not frozen
6762 if ( !GetBatchCount() )
6764 // we need to refresh everything beyond the resized line in the header
6767 // get the position from which to refresh in the other direction
6768 wxRect
rect(CellToRect(oper
.MakeCoords(m_dragRowOrCol
, 0)));
6769 rect
.SetPosition(CalcScrolledPosition(rect
.GetPosition()));
6771 // we only need the ordinate (for rows) or abscissa (for columns) here,
6772 // and need to cover the entire window in the other direction
6773 oper
.Select(rect
) = 0;
6775 wxRect
rectHeader(rect
.GetPosition(),
6778 oper
.GetHeaderWindowSize(this),
6779 doper
.Select(size
) - doper
.Select(rect
)
6782 oper
.GetHeaderWindow(this)->Refresh(true, &rectHeader
);
6785 // also refresh the grid window: extend the rectangle
6788 oper
.SelectSize(rect
) = oper
.Select(size
);
6790 int subtractLines
= 0;
6791 const int lineStart
= oper
.PosToLine(this, posLineStart
);
6792 if ( lineStart
>= 0 )
6794 // ensure that if we have a multi-cell block we redraw all of
6795 // it by increasing the refresh area to cover it entirely if a
6796 // part of it is affected
6797 const int lineEnd
= oper
.PosToLine(this, posLineEnd
, true);
6798 for ( int line
= lineStart
; line
< lineEnd
; line
++ )
6800 int cellLines
= oper
.Select(
6801 GetCellSize(oper
.MakeCoords(m_dragRowOrCol
, line
)));
6802 if ( cellLines
< subtractLines
)
6803 subtractLines
= cellLines
;
6808 oper
.GetLineStartPos(this, m_dragRowOrCol
+ subtractLines
);
6809 startPos
= doper
.CalcScrolledPosition(this, startPos
);
6811 doper
.Select(rect
) = startPos
;
6812 doper
.SelectSize(rect
) = doper
.Select(size
) - startPos
;
6814 m_gridWin
->Refresh(false, &rect
);
6818 // show the edit control back again
6819 ShowCellEditControl();
6822 void wxGrid::DoEndDragResizeRow()
6824 DoEndDragResizeLine(wxGridRowOperations());
6827 void wxGrid::DoEndDragResizeCol(wxMouseEvent
*event
)
6829 DoEndDragResizeLine(wxGridColumnOperations());
6831 // Note: we are ending the event *after* doing
6832 // default processing in this case
6835 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, *event
);
6837 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
);
6840 void wxGrid::DoStartMoveCol(int col
)
6842 m_dragRowOrCol
= col
;
6845 void wxGrid::DoEndMoveCol(int pos
)
6847 wxASSERT_MSG( m_dragRowOrCol
!= -1, "no matching DoStartMoveCol?" );
6849 if ( SendEvent(wxEVT_GRID_COL_MOVE
, -1, m_dragRowOrCol
) != -1 )
6850 SetColPos(m_dragRowOrCol
, pos
);
6851 //else: vetoed by user
6853 m_dragRowOrCol
= -1;
6856 void wxGrid::RefreshAfterColPosChange()
6858 // recalculate the column rights as the column positions have changed,
6859 // unless we calculate them dynamically because all columns widths are the
6860 // same and it's easy to do
6861 if ( !m_colWidths
.empty() )
6864 for ( int colPos
= 0; colPos
< m_numCols
; colPos
++ )
6866 int colID
= GetColAt( colPos
);
6868 colRight
+= m_colWidths
[colID
];
6869 m_colRights
[colID
] = colRight
;
6873 // and make the changes visible
6874 if ( m_useNativeHeader
)
6876 if ( m_colAt
.empty() )
6877 GetGridColHeader()->ResetColumnsOrder();
6879 GetGridColHeader()->SetColumnsOrder(m_colAt
);
6883 m_colWindow
->Refresh();
6885 m_gridWin
->Refresh();
6888 void wxGrid::SetColPos(int idx
, int pos
)
6890 // we're going to need m_colAt now, initialize it if needed
6891 if ( m_colAt
.empty() )
6893 m_colAt
.reserve(m_numCols
);
6894 for ( int i
= 0; i
< m_numCols
; i
++ )
6895 m_colAt
.push_back(i
);
6898 wxHeaderCtrl::MoveColumnInOrderArray(m_colAt
, idx
, pos
);
6900 RefreshAfterColPosChange();
6903 void wxGrid::ResetColPos()
6907 RefreshAfterColPosChange();
6910 void wxGrid::EnableDragColMove( bool enable
)
6912 if ( m_canDragColMove
== enable
)
6915 if ( m_useNativeHeader
)
6917 // update all columns to make them [not] reorderable
6918 GetGridColHeader()->SetColumnCount(m_numCols
);
6921 m_canDragColMove
= enable
;
6923 // we use to call ResetColPos() from here if !enable but this doesn't seem
6924 // right as it would mean there would be no way to "freeze" the current
6925 // columns order by disabling moving them after putting them in the desired
6926 // order, whereas now you can always call ResetColPos() manually if needed
6931 // ------ interaction with data model
6933 bool wxGrid::ProcessTableMessage( wxGridTableMessage
& msg
)
6935 switch ( msg
.GetId() )
6937 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES
:
6938 return GetModelValues();
6940 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES
:
6941 return SetModelValues();
6943 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
6944 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
6945 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
6946 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
6947 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
6948 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
6949 return Redimension( msg
);
6956 // The behaviour of this function depends on the grid table class
6957 // Clear() function. For the default wxGridStringTable class the
6958 // behaviour is to replace all cell contents with wxEmptyString but
6959 // not to change the number of rows or cols.
6961 void wxGrid::ClearGrid()
6965 if (IsCellEditControlEnabled())
6966 DisableCellEditControl();
6969 if (!GetBatchCount())
6970 m_gridWin
->Refresh();
6975 wxGrid::DoModifyLines(bool (wxGridTableBase::*funcModify
)(size_t, size_t),
6976 int pos
, int num
, bool WXUNUSED(updateLabels
) )
6978 wxCHECK_MSG( m_created
, false, "must finish creating the grid first" );
6983 if ( IsCellEditControlEnabled() )
6984 DisableCellEditControl();
6986 return (m_table
->*funcModify
)(pos
, num
);
6988 // the table will have sent the results of the insert row
6989 // operation to this view object as a grid table message
6993 wxGrid::DoAppendLines(bool (wxGridTableBase::*funcAppend
)(size_t),
6994 int num
, bool WXUNUSED(updateLabels
))
6996 wxCHECK_MSG( m_created
, false, "must finish creating the grid first" );
7001 return (m_table
->*funcAppend
)(num
);
7005 // ----- event handlers
7008 // Generate a grid event based on a mouse event and return:
7009 // -1 if the event was vetoed
7010 // +1 if the event was processed (but not vetoed)
7011 // 0 if the event wasn't handled
7013 wxGrid::SendEvent(const wxEventType type
,
7015 wxMouseEvent
& mouseEv
)
7017 bool claimed
, vetoed
;
7019 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
7021 int rowOrCol
= (row
== -1 ? col
: row
);
7023 wxGridSizeEvent
gridEvt( GetId(),
7027 mouseEv
.GetX() + GetRowLabelSize(),
7028 mouseEv
.GetY() + GetColLabelSize(),
7031 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
7032 vetoed
= !gridEvt
.IsAllowed();
7034 else if ( type
== wxEVT_GRID_RANGE_SELECT
)
7036 // Right now, it should _never_ end up here!
7037 wxGridRangeSelectEvent
gridEvt( GetId(),
7040 m_selectedBlockTopLeft
,
7041 m_selectedBlockBottomRight
,
7045 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
7046 vetoed
= !gridEvt
.IsAllowed();
7048 else if ( type
== wxEVT_GRID_LABEL_LEFT_CLICK
||
7049 type
== wxEVT_GRID_LABEL_LEFT_DCLICK
||
7050 type
== wxEVT_GRID_LABEL_RIGHT_CLICK
||
7051 type
== wxEVT_GRID_LABEL_RIGHT_DCLICK
)
7053 wxPoint pos
= mouseEv
.GetPosition();
7055 if ( mouseEv
.GetEventObject() == GetGridRowLabelWindow() )
7056 pos
.y
+= GetColLabelSize();
7057 if ( mouseEv
.GetEventObject() == GetGridColLabelWindow() )
7058 pos
.x
+= GetRowLabelSize();
7060 wxGridEvent
gridEvt( GetId(),
7068 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
7069 vetoed
= !gridEvt
.IsAllowed();
7073 wxGridEvent
gridEvt( GetId(),
7077 mouseEv
.GetX() + GetRowLabelSize(),
7078 mouseEv
.GetY() + GetColLabelSize(),
7081 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
7082 vetoed
= !gridEvt
.IsAllowed();
7085 // A Veto'd event may not be `claimed' so test this first
7089 return claimed
? 1 : 0;
7092 // Generate a grid event of specified type, return value same as above
7094 int wxGrid::SendEvent(const wxEventType type
, int row
, int col
)
7096 bool claimed
, vetoed
;
7098 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
7100 int rowOrCol
= (row
== -1 ? col
: row
);
7102 wxGridSizeEvent
gridEvt( GetId(), type
, this, rowOrCol
);
7104 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
7105 vetoed
= !gridEvt
.IsAllowed();
7109 wxGridEvent
gridEvt( GetId(), type
, this, row
, col
);
7111 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
7112 vetoed
= !gridEvt
.IsAllowed();
7115 // A Veto'd event may not be `claimed' so test this first
7119 return claimed
? 1 : 0;
7122 void wxGrid::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
7124 // needed to prevent zillions of paint events on MSW
7128 void wxGrid::Refresh(bool eraseb
, const wxRect
* rect
)
7130 // Don't do anything if between Begin/EndBatch...
7131 // EndBatch() will do all this on the last nested one anyway.
7132 if ( m_created
&& !GetBatchCount() )
7134 // Refresh to get correct scrolled position:
7135 wxScrolledWindow::Refresh(eraseb
, rect
);
7139 int rect_x
, rect_y
, rectWidth
, rectHeight
;
7140 int width_label
, width_cell
, height_label
, height_cell
;
7143 // Copy rectangle can get scroll offsets..
7144 rect_x
= rect
->GetX();
7145 rect_y
= rect
->GetY();
7146 rectWidth
= rect
->GetWidth();
7147 rectHeight
= rect
->GetHeight();
7149 width_label
= m_rowLabelWidth
- rect_x
;
7150 if (width_label
> rectWidth
)
7151 width_label
= rectWidth
;
7153 height_label
= m_colLabelHeight
- rect_y
;
7154 if (height_label
> rectHeight
)
7155 height_label
= rectHeight
;
7157 if (rect_x
> m_rowLabelWidth
)
7159 x
= rect_x
- m_rowLabelWidth
;
7160 width_cell
= rectWidth
;
7165 width_cell
= rectWidth
- (m_rowLabelWidth
- rect_x
);
7168 if (rect_y
> m_colLabelHeight
)
7170 y
= rect_y
- m_colLabelHeight
;
7171 height_cell
= rectHeight
;
7176 height_cell
= rectHeight
- (m_colLabelHeight
- rect_y
);
7179 // Paint corner label part intersecting rect.
7180 if ( width_label
> 0 && height_label
> 0 )
7182 wxRect
anotherrect(rect_x
, rect_y
, width_label
, height_label
);
7183 m_cornerLabelWin
->Refresh(eraseb
, &anotherrect
);
7186 // Paint col labels part intersecting rect.
7187 if ( width_cell
> 0 && height_label
> 0 )
7189 wxRect
anotherrect(x
, rect_y
, width_cell
, height_label
);
7190 m_colWindow
->Refresh(eraseb
, &anotherrect
);
7193 // Paint row labels part intersecting rect.
7194 if ( width_label
> 0 && height_cell
> 0 )
7196 wxRect
anotherrect(rect_x
, y
, width_label
, height_cell
);
7197 m_rowLabelWin
->Refresh(eraseb
, &anotherrect
);
7200 // Paint cell area part intersecting rect.
7201 if ( width_cell
> 0 && height_cell
> 0 )
7203 wxRect
anotherrect(x
, y
, width_cell
, height_cell
);
7204 m_gridWin
->Refresh(eraseb
, &anotherrect
);
7209 m_cornerLabelWin
->Refresh(eraseb
, NULL
);
7210 m_colWindow
->Refresh(eraseb
, NULL
);
7211 m_rowLabelWin
->Refresh(eraseb
, NULL
);
7212 m_gridWin
->Refresh(eraseb
, NULL
);
7217 void wxGrid::OnSize(wxSizeEvent
& WXUNUSED(event
))
7219 if (m_targetWindow
!= this) // check whether initialisation has been done
7221 // reposition our children windows
7226 void wxGrid::OnKeyDown( wxKeyEvent
& event
)
7228 if ( m_inOnKeyDown
)
7230 // shouldn't be here - we are going round in circles...
7232 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") );
7235 m_inOnKeyDown
= true;
7237 // propagate the event up and see if it gets processed
7238 wxWindow
*parent
= GetParent();
7239 wxKeyEvent
keyEvt( event
);
7240 keyEvt
.SetEventObject( parent
);
7242 if ( !parent
->GetEventHandler()->ProcessEvent( keyEvt
) )
7244 if (GetLayoutDirection() == wxLayout_RightToLeft
)
7246 if (event
.GetKeyCode() == WXK_RIGHT
)
7247 event
.m_keyCode
= WXK_LEFT
;
7248 else if (event
.GetKeyCode() == WXK_LEFT
)
7249 event
.m_keyCode
= WXK_RIGHT
;
7252 // try local handlers
7253 switch ( event
.GetKeyCode() )
7256 if ( event
.ControlDown() )
7257 MoveCursorUpBlock( event
.ShiftDown() );
7259 MoveCursorUp( event
.ShiftDown() );
7263 if ( event
.ControlDown() )
7264 MoveCursorDownBlock( event
.ShiftDown() );
7266 MoveCursorDown( event
.ShiftDown() );
7270 if ( event
.ControlDown() )
7271 MoveCursorLeftBlock( event
.ShiftDown() );
7273 MoveCursorLeft( event
.ShiftDown() );
7277 if ( event
.ControlDown() )
7278 MoveCursorRightBlock( event
.ShiftDown() );
7280 MoveCursorRight( event
.ShiftDown() );
7284 case WXK_NUMPAD_ENTER
:
7285 if ( event
.ControlDown() )
7287 event
.Skip(); // to let the edit control have the return
7291 if ( GetGridCursorRow() < GetNumberRows()-1 )
7293 MoveCursorDown( event
.ShiftDown() );
7297 // at the bottom of a column
7298 DisableCellEditControl();
7308 if (event
.ShiftDown())
7310 if ( GetGridCursorCol() > 0 )
7312 MoveCursorLeft( false );
7317 DisableCellEditControl();
7322 if ( GetGridCursorCol() < GetNumberCols() - 1 )
7324 MoveCursorRight( false );
7329 DisableCellEditControl();
7335 if ( event
.ControlDown() )
7346 if ( event
.ControlDown() )
7348 GoToCell(m_numRows
- 1, m_numCols
- 1);
7365 // Ctrl-Space selects the current column, Shift-Space -- the
7366 // current row and Ctrl-Shift-Space -- everything
7367 switch ( m_selection
? event
.GetModifiers() : wxMOD_NONE
)
7370 m_selection
->SelectCol(m_currentCellCoords
.GetCol());
7374 m_selection
->SelectRow(m_currentCellCoords
.GetRow());
7377 case wxMOD_CONTROL
| wxMOD_SHIFT
:
7378 m_selection
->SelectBlock(0, 0,
7379 m_numRows
- 1, m_numCols
- 1);
7383 if ( !IsEditable() )
7385 MoveCursorRight(false);
7388 //else: fall through
7401 m_inOnKeyDown
= false;
7404 void wxGrid::OnKeyUp( wxKeyEvent
& event
)
7406 // try local handlers
7408 if ( event
.GetKeyCode() == WXK_SHIFT
)
7410 if ( m_selectedBlockTopLeft
!= wxGridNoCellCoords
&&
7411 m_selectedBlockBottomRight
!= wxGridNoCellCoords
)
7415 m_selection
->SelectBlock(
7416 m_selectedBlockTopLeft
,
7417 m_selectedBlockBottomRight
,
7422 m_selectedBlockTopLeft
= wxGridNoCellCoords
;
7423 m_selectedBlockBottomRight
= wxGridNoCellCoords
;
7424 m_selectedBlockCorner
= wxGridNoCellCoords
;
7428 void wxGrid::OnChar( wxKeyEvent
& event
)
7430 // is it possible to edit the current cell at all?
7431 if ( !IsCellEditControlEnabled() && CanEnableCellControl() )
7433 // yes, now check whether the cells editor accepts the key
7434 int row
= m_currentCellCoords
.GetRow();
7435 int col
= m_currentCellCoords
.GetCol();
7436 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7437 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
7439 // <F2> is special and will always start editing, for
7440 // other keys - ask the editor itself
7441 if ( (event
.GetKeyCode() == WXK_F2
&& !event
.HasModifiers())
7442 || editor
->IsAcceptedKey(event
) )
7444 // ensure cell is visble
7445 MakeCellVisible(row
, col
);
7446 EnableCellEditControl();
7448 // a problem can arise if the cell is not completely
7449 // visible (even after calling MakeCellVisible the
7450 // control is not created and calling StartingKey will
7452 if ( event
.GetKeyCode() != WXK_F2
&& editor
->IsCreated() && m_cellEditCtrlEnabled
)
7453 editor
->StartingKey(event
);
7469 void wxGrid::OnEraseBackground(wxEraseEvent
&)
7473 bool wxGrid::SetCurrentCell( const wxGridCellCoords
& coords
)
7475 if ( SendEvent(wxEVT_GRID_SELECT_CELL
, coords
) == -1 )
7477 // the event has been vetoed - do nothing
7481 #if !defined(__WXMAC__)
7482 wxClientDC
dc( m_gridWin
);
7486 if ( m_currentCellCoords
!= wxGridNoCellCoords
)
7488 DisableCellEditControl();
7490 if ( IsVisible( m_currentCellCoords
, false ) )
7493 r
= BlockToDeviceRect( m_currentCellCoords
, m_currentCellCoords
);
7494 if ( !m_gridLinesEnabled
)
7502 wxGridCellCoordsArray cells
= CalcCellsExposed( r
);
7504 // Otherwise refresh redraws the highlight!
7505 m_currentCellCoords
= coords
;
7507 #if defined(__WXMAC__)
7508 m_gridWin
->Refresh(true /*, & r */);
7510 DrawGridCellArea( dc
, cells
);
7511 DrawAllGridLines( dc
, r
);
7516 m_currentCellCoords
= coords
;
7518 wxGridCellAttr
*attr
= GetCellAttr( coords
);
7519 #if !defined(__WXMAC__)
7520 DrawCellHighlight( dc
, attr
);
7528 wxGrid::UpdateBlockBeingSelected(int topRow
, int leftCol
,
7529 int bottomRow
, int rightCol
)
7533 switch ( m_selection
->GetSelectionMode() )
7536 wxFAIL_MSG( "unknown selection mode" );
7539 case wxGridSelectCells
:
7540 // arbitrary blocks selection allowed so just use the cell
7541 // coordinates as is
7544 case wxGridSelectRows
:
7545 // only full rows selection allowd, ensure that we do select
7548 rightCol
= GetNumberCols() - 1;
7551 case wxGridSelectColumns
:
7552 // same as above but for columns
7554 bottomRow
= GetNumberRows() - 1;
7557 case wxGridSelectRowsOrColumns
:
7558 // in this mode we can select only full rows or full columns so
7559 // it doesn't make sense to select blocks at all (and we can't
7560 // extend the block because there is no preferred direction, we
7561 // could only extend it to cover the entire grid but this is
7567 m_selectedBlockCorner
= wxGridCellCoords(bottomRow
, rightCol
);
7568 MakeCellVisible(m_selectedBlockCorner
);
7570 EnsureFirstLessThanSecond(topRow
, bottomRow
);
7571 EnsureFirstLessThanSecond(leftCol
, rightCol
);
7573 wxGridCellCoords updateTopLeft
= wxGridCellCoords(topRow
, leftCol
),
7574 updateBottomRight
= wxGridCellCoords(bottomRow
, rightCol
);
7576 // First the case that we selected a completely new area
7577 if ( m_selectedBlockTopLeft
== wxGridNoCellCoords
||
7578 m_selectedBlockBottomRight
== wxGridNoCellCoords
)
7581 rect
= BlockToDeviceRect( wxGridCellCoords ( topRow
, leftCol
),
7582 wxGridCellCoords ( bottomRow
, rightCol
) );
7583 m_gridWin
->Refresh( false, &rect
);
7586 // Now handle changing an existing selection area.
7587 else if ( m_selectedBlockTopLeft
!= updateTopLeft
||
7588 m_selectedBlockBottomRight
!= updateBottomRight
)
7590 // Compute two optimal update rectangles:
7591 // Either one rectangle is a real subset of the
7592 // other, or they are (almost) disjoint!
7594 bool need_refresh
[4];
7598 need_refresh
[3] = false;
7601 // Store intermediate values
7602 wxCoord oldLeft
= m_selectedBlockTopLeft
.GetCol();
7603 wxCoord oldTop
= m_selectedBlockTopLeft
.GetRow();
7604 wxCoord oldRight
= m_selectedBlockBottomRight
.GetCol();
7605 wxCoord oldBottom
= m_selectedBlockBottomRight
.GetRow();
7607 // Determine the outer/inner coordinates.
7608 EnsureFirstLessThanSecond(oldLeft
, leftCol
);
7609 EnsureFirstLessThanSecond(oldTop
, topRow
);
7610 EnsureFirstLessThanSecond(rightCol
, oldRight
);
7611 EnsureFirstLessThanSecond(bottomRow
, oldBottom
);
7613 // Now, either the stuff marked old is the outer
7614 // rectangle or we don't have a situation where one
7615 // is contained in the other.
7617 if ( oldLeft
< leftCol
)
7619 // Refresh the newly selected or deselected
7620 // area to the left of the old or new selection.
7621 need_refresh
[0] = true;
7622 rect
[0] = BlockToDeviceRect(
7623 wxGridCellCoords( oldTop
, oldLeft
),
7624 wxGridCellCoords( oldBottom
, leftCol
- 1 ) );
7627 if ( oldTop
< topRow
)
7629 // Refresh the newly selected or deselected
7630 // area above the old or new selection.
7631 need_refresh
[1] = true;
7632 rect
[1] = BlockToDeviceRect(
7633 wxGridCellCoords( oldTop
, leftCol
),
7634 wxGridCellCoords( topRow
- 1, rightCol
) );
7637 if ( oldRight
> rightCol
)
7639 // Refresh the newly selected or deselected
7640 // area to the right of the old or new selection.
7641 need_refresh
[2] = true;
7642 rect
[2] = BlockToDeviceRect(
7643 wxGridCellCoords( oldTop
, rightCol
+ 1 ),
7644 wxGridCellCoords( oldBottom
, oldRight
) );
7647 if ( oldBottom
> bottomRow
)
7649 // Refresh the newly selected or deselected
7650 // area below the old or new selection.
7651 need_refresh
[3] = true;
7652 rect
[3] = BlockToDeviceRect(
7653 wxGridCellCoords( bottomRow
+ 1, leftCol
),
7654 wxGridCellCoords( oldBottom
, rightCol
) );
7657 // various Refresh() calls
7658 for (i
= 0; i
< 4; i
++ )
7659 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
7660 m_gridWin
->Refresh( false, &(rect
[i
]) );
7664 m_selectedBlockTopLeft
= updateTopLeft
;
7665 m_selectedBlockBottomRight
= updateBottomRight
;
7669 // ------ functions to get/send data (see also public functions)
7672 bool wxGrid::GetModelValues()
7674 // Hide the editor, so it won't hide a changed value.
7675 HideCellEditControl();
7679 // all we need to do is repaint the grid
7681 m_gridWin
->Refresh();
7688 bool wxGrid::SetModelValues()
7692 // Disable the editor, so it won't hide a changed value.
7693 // Do we also want to save the current value of the editor first?
7695 DisableCellEditControl();
7699 for ( row
= 0; row
< m_numRows
; row
++ )
7701 for ( col
= 0; col
< m_numCols
; col
++ )
7703 m_table
->SetValue( row
, col
, GetCellValue(row
, col
) );
7713 // Note - this function only draws cells that are in the list of
7714 // exposed cells (usually set from the update region by
7715 // CalcExposedCells)
7717 void wxGrid::DrawGridCellArea( wxDC
& dc
, const wxGridCellCoordsArray
& cells
)
7719 if ( !m_numRows
|| !m_numCols
)
7722 int i
, numCells
= cells
.GetCount();
7723 int row
, col
, cell_rows
, cell_cols
;
7724 wxGridCellCoordsArray redrawCells
;
7726 for ( i
= numCells
- 1; i
>= 0; i
-- )
7728 row
= cells
[i
].GetRow();
7729 col
= cells
[i
].GetCol();
7730 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7732 // If this cell is part of a multicell block, find owner for repaint
7733 if ( cell_rows
<= 0 || cell_cols
<= 0 )
7735 wxGridCellCoords
cell( row
+ cell_rows
, col
+ cell_cols
);
7736 bool marked
= false;
7737 for ( int j
= 0; j
< numCells
; j
++ )
7739 if ( cell
== cells
[j
] )
7748 int count
= redrawCells
.GetCount();
7749 for (int j
= 0; j
< count
; j
++)
7751 if ( cell
== redrawCells
[j
] )
7759 redrawCells
.Add( cell
);
7762 // don't bother drawing this cell
7766 // If this cell is empty, find cell to left that might want to overflow
7767 if (m_table
&& m_table
->IsEmptyCell(row
, col
))
7769 for ( int l
= 0; l
< cell_rows
; l
++ )
7771 // find a cell in this row to leave already marked for repaint
7773 for (int k
= 0; k
< int(redrawCells
.GetCount()); k
++)
7774 if ((redrawCells
[k
].GetCol() < left
) &&
7775 (redrawCells
[k
].GetRow() == row
))
7777 left
= redrawCells
[k
].GetCol();
7781 left
= 0; // oh well
7783 for (int j
= col
- 1; j
>= left
; j
--)
7785 if (!m_table
->IsEmptyCell(row
+ l
, j
))
7787 if (GetCellOverflow(row
+ l
, j
))
7789 wxGridCellCoords
cell(row
+ l
, j
);
7790 bool marked
= false;
7792 for (int k
= 0; k
< numCells
; k
++)
7794 if ( cell
== cells
[k
] )
7803 int count
= redrawCells
.GetCount();
7804 for (int k
= 0; k
< count
; k
++)
7806 if ( cell
== redrawCells
[k
] )
7813 redrawCells
.Add( cell
);
7822 DrawCell( dc
, cells
[i
] );
7825 numCells
= redrawCells
.GetCount();
7827 for ( i
= numCells
- 1; i
>= 0; i
-- )
7829 DrawCell( dc
, redrawCells
[i
] );
7833 void wxGrid::DrawGridSpace( wxDC
& dc
)
7836 m_gridWin
->GetClientSize( &cw
, &ch
);
7839 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
7841 int rightCol
= m_numCols
> 0 ? GetColRight(GetColAt( m_numCols
- 1 )) : 0;
7842 int bottomRow
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) : 0;
7844 if ( right
> rightCol
|| bottom
> bottomRow
)
7847 CalcUnscrolledPosition( 0, 0, &left
, &top
);
7849 dc
.SetBrush(GetDefaultCellBackgroundColour());
7850 dc
.SetPen( *wxTRANSPARENT_PEN
);
7852 if ( right
> rightCol
)
7854 dc
.DrawRectangle( rightCol
, top
, right
- rightCol
, ch
);
7857 if ( bottom
> bottomRow
)
7859 dc
.DrawRectangle( left
, bottomRow
, cw
, bottom
- bottomRow
);
7864 void wxGrid::DrawCell( wxDC
& dc
, const wxGridCellCoords
& coords
)
7866 int row
= coords
.GetRow();
7867 int col
= coords
.GetCol();
7869 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7872 // we draw the cell border ourselves
7873 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7875 bool isCurrent
= coords
== m_currentCellCoords
;
7877 wxRect rect
= CellToRect( row
, col
);
7879 // if the editor is shown, we should use it and not the renderer
7880 // Note: However, only if it is really _shown_, i.e. not hidden!
7881 if ( isCurrent
&& IsCellEditControlShown() )
7883 // NB: this "#if..." is temporary and fixes a problem where the
7884 // edit control is erased by this code after being rendered.
7885 // On wxMac (QD build only), the cell editor is a wxTextCntl and is rendered
7886 // implicitly, causing this out-of order render.
7887 #if !defined(__WXMAC__)
7888 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
7889 editor
->PaintBackground(rect
, attr
);
7895 // but all the rest is drawn by the cell renderer and hence may be customized
7896 wxGridCellRenderer
*renderer
= attr
->GetRenderer(this, row
, col
);
7897 renderer
->Draw(*this, *attr
, dc
, rect
, row
, col
, IsInSelection(coords
));
7904 void wxGrid::DrawCellHighlight( wxDC
& dc
, const wxGridCellAttr
*attr
)
7906 // don't show highlight when the grid doesn't have focus
7910 int row
= m_currentCellCoords
.GetRow();
7911 int col
= m_currentCellCoords
.GetCol();
7913 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7916 wxRect rect
= CellToRect(row
, col
);
7918 // hmmm... what could we do here to show that the cell is disabled?
7919 // for now, I just draw a thinner border than for the other ones, but
7920 // it doesn't look really good
7922 int penWidth
= attr
->IsReadOnly() ? m_cellHighlightROPenWidth
: m_cellHighlightPenWidth
;
7926 // The center of the drawn line is where the position/width/height of
7927 // the rectangle is actually at (on wxMSW at least), so the
7928 // size of the rectangle is reduced to compensate for the thickness of
7929 // the line. If this is too strange on non-wxMSW platforms then
7930 // please #ifdef this appropriately.
7931 rect
.x
+= penWidth
/ 2;
7932 rect
.y
+= penWidth
/ 2;
7933 rect
.width
-= penWidth
- 1;
7934 rect
.height
-= penWidth
- 1;
7936 // Now draw the rectangle
7937 // use the cellHighlightColour if the cell is inside a selection, this
7938 // will ensure the cell is always visible.
7939 dc
.SetPen(wxPen(IsInSelection(row
,col
) ? m_selectionForeground
7940 : m_cellHighlightColour
,
7942 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
7943 dc
.DrawRectangle(rect
);
7947 wxPen
wxGrid::GetDefaultGridLinePen()
7949 return wxPen(GetGridLineColour());
7952 wxPen
wxGrid::GetRowGridLinePen(int WXUNUSED(row
))
7954 return GetDefaultGridLinePen();
7957 wxPen
wxGrid::GetColGridLinePen(int WXUNUSED(col
))
7959 return GetDefaultGridLinePen();
7962 void wxGrid::DrawCellBorder( wxDC
& dc
, const wxGridCellCoords
& coords
)
7964 int row
= coords
.GetRow();
7965 int col
= coords
.GetCol();
7966 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7970 wxRect rect
= CellToRect( row
, col
);
7972 // right hand border
7973 dc
.SetPen( GetColGridLinePen(col
) );
7974 dc
.DrawLine( rect
.x
+ rect
.width
, rect
.y
,
7975 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
+ 1 );
7978 dc
.SetPen( GetRowGridLinePen(row
) );
7979 dc
.DrawLine( rect
.x
, rect
.y
+ rect
.height
,
7980 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
);
7983 void wxGrid::DrawHighlight(wxDC
& dc
, const wxGridCellCoordsArray
& cells
)
7985 // This if block was previously in wxGrid::OnPaint but that doesn't
7986 // seem to get called under wxGTK - MB
7988 if ( m_currentCellCoords
== wxGridNoCellCoords
&&
7989 m_numRows
&& m_numCols
)
7991 m_currentCellCoords
.Set(0, 0);
7994 if ( IsCellEditControlShown() )
7996 // don't show highlight when the edit control is shown
8000 // if the active cell was repainted, repaint its highlight too because it
8001 // might have been damaged by the grid lines
8002 size_t count
= cells
.GetCount();
8003 for ( size_t n
= 0; n
< count
; n
++ )
8005 wxGridCellCoords cell
= cells
[n
];
8007 // If we are using attributes, then we may have just exposed another
8008 // cell in a partially-visible merged cluster of cells. If the "anchor"
8009 // (upper left) cell of this merged cluster is the cell indicated by
8010 // m_currentCellCoords, then we need to refresh the cell highlight even
8011 // though the "anchor" itself is not part of our update segment.
8012 if ( CanHaveAttributes() )
8016 GetCellSize(cell
.GetRow(), cell
.GetCol(), &rows
, &cols
);
8019 cell
.SetRow(cell
.GetRow() + rows
);
8022 cell
.SetCol(cell
.GetCol() + cols
);
8025 if ( cell
== m_currentCellCoords
)
8027 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
8028 DrawCellHighlight(dc
, attr
);
8036 // This is used to redraw all grid lines e.g. when the grid line colour
8039 void wxGrid::DrawAllGridLines( wxDC
& dc
, const wxRegion
& WXUNUSED(reg
) )
8041 if ( !m_gridLinesEnabled
)
8044 int top
, bottom
, left
, right
;
8047 m_gridWin
->GetClientSize(&cw
, &ch
);
8048 CalcUnscrolledPosition( 0, 0, &left
, &top
);
8049 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
8051 // avoid drawing grid lines past the last row and col
8052 if ( m_gridLinesClipHorz
)
8057 const int lastColRight
= GetColRight(GetColAt(m_numCols
- 1));
8058 if ( right
> lastColRight
)
8059 right
= lastColRight
;
8062 if ( m_gridLinesClipVert
)
8067 const int lastRowBottom
= GetRowBottom(m_numRows
- 1);
8068 if ( bottom
> lastRowBottom
)
8069 bottom
= lastRowBottom
;
8072 // no gridlines inside multicells, clip them out
8073 int leftCol
= GetColPos( internalXToCol(left
) );
8074 int topRow
= internalYToRow(top
);
8075 int rightCol
= GetColPos( internalXToCol(right
) );
8076 int bottomRow
= internalYToRow(bottom
);
8078 wxRegion
clippedcells(0, 0, cw
, ch
);
8080 int cell_rows
, cell_cols
;
8083 for ( int j
= topRow
; j
<= bottomRow
; j
++ )
8085 for ( int colPos
= leftCol
; colPos
<= rightCol
; colPos
++ )
8087 int i
= GetColAt( colPos
);
8089 GetCellSize( j
, i
, &cell_rows
, &cell_cols
);
8090 if ((cell_rows
> 1) || (cell_cols
> 1))
8092 rect
= CellToRect(j
,i
);
8093 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
8094 clippedcells
.Subtract(rect
);
8096 else if ((cell_rows
< 0) || (cell_cols
< 0))
8098 rect
= CellToRect(j
+ cell_rows
, i
+ cell_cols
);
8099 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
8100 clippedcells
.Subtract(rect
);
8105 dc
.SetDeviceClippingRegion( clippedcells
);
8108 // horizontal grid lines
8109 for ( int i
= internalYToRow(top
); i
< m_numRows
; i
++ )
8111 int bot
= GetRowBottom(i
) - 1;
8118 dc
.SetPen( GetRowGridLinePen(i
) );
8119 dc
.DrawLine( left
, bot
, right
, bot
);
8123 // vertical grid lines
8124 for ( int colPos
= leftCol
; colPos
< m_numCols
; colPos
++ )
8126 int i
= GetColAt( colPos
);
8128 int colRight
= GetColRight(i
);
8130 if (GetLayoutDirection() != wxLayout_RightToLeft
)
8134 if ( colRight
> right
)
8137 if ( colRight
>= left
)
8139 dc
.SetPen( GetColGridLinePen(i
) );
8140 dc
.DrawLine( colRight
, top
, colRight
, bottom
);
8144 dc
.DestroyClippingRegion();
8147 void wxGrid::DrawRowLabels( wxDC
& dc
, const wxArrayInt
& rows
)
8152 const size_t numLabels
= rows
.GetCount();
8153 for ( size_t i
= 0; i
< numLabels
; i
++ )
8155 DrawRowLabel( dc
, rows
[i
] );
8159 void wxGrid::DrawRowLabel( wxDC
& dc
, int row
)
8161 if ( GetRowHeight(row
) <= 0 || m_rowLabelWidth
<= 0 )
8166 int rowTop
= GetRowTop(row
),
8167 rowBottom
= GetRowBottom(row
) - 1;
8169 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW
)));
8170 dc
.DrawLine( m_rowLabelWidth
- 1, rowTop
, m_rowLabelWidth
- 1, rowBottom
);
8171 dc
.DrawLine( 0, rowTop
, 0, rowBottom
);
8172 dc
.DrawLine( 0, rowBottom
, m_rowLabelWidth
, rowBottom
);
8174 dc
.SetPen( *wxWHITE_PEN
);
8175 dc
.DrawLine( 1, rowTop
, 1, rowBottom
);
8176 dc
.DrawLine( 1, rowTop
, m_rowLabelWidth
- 1, rowTop
);
8178 dc
.SetBackgroundMode( wxBRUSHSTYLE_TRANSPARENT
);
8179 dc
.SetTextForeground( GetLabelTextColour() );
8180 dc
.SetFont( GetLabelFont() );
8183 GetRowLabelAlignment( &hAlign
, &vAlign
);
8186 rect
.SetY( GetRowTop(row
) + 2 );
8187 rect
.SetWidth( m_rowLabelWidth
- 4 );
8188 rect
.SetHeight( GetRowHeight(row
) - 4 );
8189 DrawTextRectangle( dc
, GetRowLabelValue( row
), rect
, hAlign
, vAlign
);
8192 void wxGrid::UseNativeColHeader(bool native
)
8194 if ( native
== m_useNativeHeader
)
8198 m_useNativeHeader
= native
;
8200 CreateColumnWindow();
8202 if ( m_useNativeHeader
)
8203 GetGridColHeader()->SetColumnCount(m_numCols
);
8207 void wxGrid::SetUseNativeColLabels( bool native
)
8209 wxASSERT_MSG( !m_useNativeHeader
,
8210 "doesn't make sense when using native header" );
8212 m_nativeColumnLabels
= native
;
8215 int height
= wxRendererNative::Get().GetHeaderButtonHeight( this );
8216 SetColLabelSize( height
);
8219 GetColLabelWindow()->Refresh();
8220 m_cornerLabelWin
->Refresh();
8223 void wxGrid::DrawColLabels( wxDC
& dc
,const wxArrayInt
& cols
)
8228 const size_t numLabels
= cols
.GetCount();
8229 for ( size_t i
= 0; i
< numLabels
; i
++ )
8231 DrawColLabel( dc
, cols
[i
] );
8235 void wxGrid::DrawCornerLabel(wxDC
& dc
)
8237 if ( m_nativeColumnLabels
)
8239 wxRect
rect(wxSize(m_rowLabelWidth
, m_colLabelHeight
));
8242 wxRendererNative::Get().DrawHeaderButton(m_cornerLabelWin
, dc
, rect
, 0);
8246 dc
.SetPen(wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW
)));
8247 dc
.DrawLine( m_rowLabelWidth
- 1, m_colLabelHeight
- 1,
8248 m_rowLabelWidth
- 1, 0 );
8249 dc
.DrawLine( m_rowLabelWidth
- 1, m_colLabelHeight
- 1,
8250 0, m_colLabelHeight
- 1 );
8251 dc
.DrawLine( 0, 0, m_rowLabelWidth
, 0 );
8252 dc
.DrawLine( 0, 0, 0, m_colLabelHeight
);
8254 dc
.SetPen( *wxWHITE_PEN
);
8255 dc
.DrawLine( 1, 1, m_rowLabelWidth
- 1, 1 );
8256 dc
.DrawLine( 1, 1, 1, m_colLabelHeight
- 1 );
8260 void wxGrid::DrawColLabel(wxDC
& dc
, int col
)
8262 if ( GetColWidth(col
) <= 0 || m_colLabelHeight
<= 0 )
8265 int colLeft
= GetColLeft(col
);
8267 wxRect
rect(colLeft
, 0, GetColWidth(col
), m_colLabelHeight
);
8269 if ( m_nativeColumnLabels
)
8271 wxRendererNative::Get().DrawHeaderButton
8273 GetColLabelWindow(),
8278 ? IsSortOrderAscending()
8279 ? wxHDR_SORT_ICON_UP
8280 : wxHDR_SORT_ICON_DOWN
8281 : wxHDR_SORT_ICON_NONE
8286 int colRight
= GetColRight(col
) - 1;
8288 dc
.SetPen(wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW
)));
8289 dc
.DrawLine( colRight
, 0,
8290 colRight
, m_colLabelHeight
- 1 );
8291 dc
.DrawLine( colLeft
, 0,
8293 dc
.DrawLine( colLeft
, m_colLabelHeight
- 1,
8294 colRight
+ 1, m_colLabelHeight
- 1 );
8296 dc
.SetPen( *wxWHITE_PEN
);
8297 dc
.DrawLine( colLeft
, 1, colLeft
, m_colLabelHeight
- 1 );
8298 dc
.DrawLine( colLeft
, 1, colRight
, 1 );
8301 dc
.SetBackgroundMode( wxBRUSHSTYLE_TRANSPARENT
);
8302 dc
.SetTextForeground( GetLabelTextColour() );
8303 dc
.SetFont( GetLabelFont() );
8306 GetColLabelAlignment( &hAlign
, &vAlign
);
8307 const int orient
= GetColLabelTextOrientation();
8310 DrawTextRectangle(dc
, GetColLabelValue(col
), rect
, hAlign
, vAlign
, orient
);
8313 // TODO: these 2 functions should be replaced with wxDC::DrawLabel() to which
8314 // we just have to add textOrientation support
8315 void wxGrid::DrawTextRectangle( wxDC
& dc
,
8316 const wxString
& value
,
8320 int textOrientation
)
8322 wxArrayString lines
;
8324 StringToLines( value
, lines
);
8326 DrawTextRectangle(dc
, lines
, rect
, horizAlign
, vertAlign
, textOrientation
);
8329 void wxGrid::DrawTextRectangle(wxDC
& dc
,
8330 const wxArrayString
& lines
,
8334 int textOrientation
)
8336 if ( lines
.empty() )
8339 wxDCClipper
clip(dc
, rect
);
8344 if ( textOrientation
== wxHORIZONTAL
)
8345 GetTextBoxSize( dc
, lines
, &textWidth
, &textHeight
);
8347 GetTextBoxSize( dc
, lines
, &textHeight
, &textWidth
);
8351 switch ( vertAlign
)
8353 case wxALIGN_BOTTOM
:
8354 if ( textOrientation
== wxHORIZONTAL
)
8355 y
= rect
.y
+ (rect
.height
- textHeight
- 1);
8357 x
= rect
.x
+ rect
.width
- textWidth
;
8360 case wxALIGN_CENTRE
:
8361 if ( textOrientation
== wxHORIZONTAL
)
8362 y
= rect
.y
+ ((rect
.height
- textHeight
) / 2);
8364 x
= rect
.x
+ ((rect
.width
- textWidth
) / 2);
8369 if ( textOrientation
== wxHORIZONTAL
)
8376 // Align each line of a multi-line label
8377 size_t nLines
= lines
.GetCount();
8378 for ( size_t l
= 0; l
< nLines
; l
++ )
8380 const wxString
& line
= lines
[l
];
8384 *(textOrientation
== wxHORIZONTAL
? &y
: &x
) += dc
.GetCharHeight();
8388 wxCoord lineWidth
= 0,
8390 dc
.GetTextExtent(line
, &lineWidth
, &lineHeight
);
8392 switch ( horizAlign
)
8395 if ( textOrientation
== wxHORIZONTAL
)
8396 x
= rect
.x
+ (rect
.width
- lineWidth
- 1);
8398 y
= rect
.y
+ lineWidth
+ 1;
8401 case wxALIGN_CENTRE
:
8402 if ( textOrientation
== wxHORIZONTAL
)
8403 x
= rect
.x
+ ((rect
.width
- lineWidth
) / 2);
8405 y
= rect
.y
+ rect
.height
- ((rect
.height
- lineWidth
) / 2);
8410 if ( textOrientation
== wxHORIZONTAL
)
8413 y
= rect
.y
+ rect
.height
- 1;
8417 if ( textOrientation
== wxHORIZONTAL
)
8419 dc
.DrawText( line
, x
, y
);
8424 dc
.DrawRotatedText( line
, x
, y
, 90.0 );
8430 // Split multi-line text up into an array of strings.
8431 // Any existing contents of the string array are preserved.
8433 // TODO: refactor wxTextFile::Read() and reuse the same code from here
8434 void wxGrid::StringToLines( const wxString
& value
, wxArrayString
& lines
) const
8438 wxString eol
= wxTextFile::GetEOL( wxTextFileType_Unix
);
8439 wxString tVal
= wxTextFile::Translate( value
, wxTextFileType_Unix
);
8441 while ( startPos
< (int)tVal
.length() )
8443 pos
= tVal
.Mid(startPos
).Find( eol
);
8448 else if ( pos
== 0 )
8450 lines
.Add( wxEmptyString
);
8454 lines
.Add( tVal
.Mid(startPos
, pos
) );
8457 startPos
+= pos
+ 1;
8460 if ( startPos
< (int)tVal
.length() )
8462 lines
.Add( tVal
.Mid( startPos
) );
8466 void wxGrid::GetTextBoxSize( const wxDC
& dc
,
8467 const wxArrayString
& lines
,
8468 long *width
, long *height
) const
8472 wxCoord lineW
= 0, lineH
= 0;
8475 for ( i
= 0; i
< lines
.GetCount(); i
++ )
8477 dc
.GetTextExtent( lines
[i
], &lineW
, &lineH
);
8478 w
= wxMax( w
, lineW
);
8487 // ------ Batch processing.
8489 void wxGrid::EndBatch()
8491 if ( m_batchCount
> 0 )
8494 if ( !m_batchCount
)
8497 m_rowLabelWin
->Refresh();
8498 m_colWindow
->Refresh();
8499 m_cornerLabelWin
->Refresh();
8500 m_gridWin
->Refresh();
8505 // Use this, rather than wxWindow::Refresh(), to force an immediate
8506 // repainting of the grid. Has no effect if you are already inside a
8507 // BeginBatch / EndBatch block.
8509 void wxGrid::ForceRefresh()
8515 bool wxGrid::Enable(bool enable
)
8517 if ( !wxScrolledWindow::Enable(enable
) )
8520 // redraw in the new state
8521 m_gridWin
->Refresh();
8527 // ------ Edit control functions
8530 void wxGrid::EnableEditing( bool edit
)
8532 if ( edit
!= m_editable
)
8535 EnableCellEditControl(edit
);
8540 void wxGrid::EnableCellEditControl( bool enable
)
8545 if ( enable
!= m_cellEditCtrlEnabled
)
8549 if ( SendEvent(wxEVT_GRID_EDITOR_SHOWN
) == -1 )
8552 // this should be checked by the caller!
8553 wxASSERT_MSG( CanEnableCellControl(), _T("can't enable editing for this cell!") );
8555 // do it before ShowCellEditControl()
8556 m_cellEditCtrlEnabled
= enable
;
8558 ShowCellEditControl();
8562 //FIXME:add veto support
8563 SendEvent(wxEVT_GRID_EDITOR_HIDDEN
);
8565 HideCellEditControl();
8566 SaveEditControlValue();
8568 // do it after HideCellEditControl()
8569 m_cellEditCtrlEnabled
= enable
;
8574 bool wxGrid::IsCurrentCellReadOnly() const
8577 wxGridCellAttr
* attr
= ((wxGrid
*)this)->GetCellAttr(m_currentCellCoords
);
8578 bool readonly
= attr
->IsReadOnly();
8584 bool wxGrid::CanEnableCellControl() const
8586 return m_editable
&& (m_currentCellCoords
!= wxGridNoCellCoords
) &&
8587 !IsCurrentCellReadOnly();
8590 bool wxGrid::IsCellEditControlEnabled() const
8592 // the cell edit control might be disable for all cells or just for the
8593 // current one if it's read only
8594 return m_cellEditCtrlEnabled
? !IsCurrentCellReadOnly() : false;
8597 bool wxGrid::IsCellEditControlShown() const
8599 bool isShown
= false;
8601 if ( m_cellEditCtrlEnabled
)
8603 int row
= m_currentCellCoords
.GetRow();
8604 int col
= m_currentCellCoords
.GetCol();
8605 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8606 wxGridCellEditor
* editor
= attr
->GetEditor((wxGrid
*) this, row
, col
);
8611 if ( editor
->IsCreated() )
8613 isShown
= editor
->GetControl()->IsShown();
8623 void wxGrid::ShowCellEditControl()
8625 if ( IsCellEditControlEnabled() )
8627 if ( !IsVisible( m_currentCellCoords
, false ) )
8629 m_cellEditCtrlEnabled
= false;
8634 wxRect rect
= CellToRect( m_currentCellCoords
);
8635 int row
= m_currentCellCoords
.GetRow();
8636 int col
= m_currentCellCoords
.GetCol();
8638 // if this is part of a multicell, find owner (topleft)
8639 int cell_rows
, cell_cols
;
8640 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
8641 if ( cell_rows
<= 0 || cell_cols
<= 0 )
8645 m_currentCellCoords
.SetRow( row
);
8646 m_currentCellCoords
.SetCol( col
);
8649 // erase the highlight and the cell contents because the editor
8650 // might not cover the entire cell
8651 wxClientDC
dc( m_gridWin
);
8653 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8654 dc
.SetBrush(wxBrush(attr
->GetBackgroundColour()));
8655 dc
.SetPen(*wxTRANSPARENT_PEN
);
8656 dc
.DrawRectangle(rect
);
8658 // convert to scrolled coords
8659 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
8665 // cell is shifted by one pixel
8666 // However, don't allow x or y to become negative
8667 // since the SetSize() method interprets that as
8674 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
8675 if ( !editor
->IsCreated() )
8677 editor
->Create(m_gridWin
, wxID_ANY
,
8678 new wxGridCellEditorEvtHandler(this, editor
));
8680 wxGridEditorCreatedEvent
evt(GetId(),
8681 wxEVT_GRID_EDITOR_CREATED
,
8685 editor
->GetControl());
8686 GetEventHandler()->ProcessEvent(evt
);
8689 // resize editor to overflow into righthand cells if allowed
8690 int maxWidth
= rect
.width
;
8691 wxString value
= GetCellValue(row
, col
);
8692 if ( (value
!= wxEmptyString
) && (attr
->GetOverflow()) )
8695 GetTextExtent(value
, &maxWidth
, &y
, NULL
, NULL
, &attr
->GetFont());
8696 if (maxWidth
< rect
.width
)
8697 maxWidth
= rect
.width
;
8700 int client_right
= m_gridWin
->GetClientSize().GetWidth();
8701 if (rect
.x
+ maxWidth
> client_right
)
8702 maxWidth
= client_right
- rect
.x
;
8704 if ((maxWidth
> rect
.width
) && (col
< m_numCols
) && m_table
)
8706 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
8707 // may have changed earlier
8708 for (int i
= col
+ cell_cols
; i
< m_numCols
; i
++)
8711 GetCellSize( row
, i
, &c_rows
, &c_cols
);
8713 // looks weird going over a multicell
8714 if (m_table
->IsEmptyCell( row
, i
) &&
8715 (rect
.width
< maxWidth
) && (c_rows
== 1))
8717 rect
.width
+= GetColWidth( i
);
8723 if (rect
.GetRight() > client_right
)
8724 rect
.SetRight( client_right
- 1 );
8727 editor
->SetCellAttr( attr
);
8728 editor
->SetSize( rect
);
8730 editor
->GetControl()->Move(
8731 editor
->GetControl()->GetPosition().x
+ nXMove
,
8732 editor
->GetControl()->GetPosition().y
);
8733 editor
->Show( true, attr
);
8735 // recalc dimensions in case we need to
8736 // expand the scrolled window to account for editor
8739 editor
->BeginEdit(row
, col
, this);
8740 editor
->SetCellAttr(NULL
);
8748 void wxGrid::HideCellEditControl()
8750 if ( IsCellEditControlEnabled() )
8752 int row
= m_currentCellCoords
.GetRow();
8753 int col
= m_currentCellCoords
.GetCol();
8755 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8756 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
8757 const bool editorHadFocus
= editor
->GetControl()->HasFocus();
8758 editor
->Show( false );
8762 // return the focus to the grid itself if the editor had it
8764 // note that we must not do this unconditionally to avoid stealing
8765 // focus from the window which just received it if we are hiding the
8766 // editor precisely because we lost focus
8767 if ( editorHadFocus
)
8768 m_gridWin
->SetFocus();
8770 // refresh whole row to the right
8771 wxRect
rect( CellToRect(row
, col
) );
8772 CalcScrolledPosition(rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
8773 rect
.width
= m_gridWin
->GetClientSize().GetWidth() - rect
.x
;
8776 // ensure that the pixels under the focus ring get refreshed as well
8777 rect
.Inflate(10, 10);
8780 m_gridWin
->Refresh( false, &rect
);
8784 void wxGrid::SaveEditControlValue()
8786 if ( IsCellEditControlEnabled() )
8788 int row
= m_currentCellCoords
.GetRow();
8789 int col
= m_currentCellCoords
.GetCol();
8791 wxString oldval
= GetCellValue(row
, col
);
8793 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8794 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
8795 bool changed
= editor
->EndEdit(row
, col
, this);
8802 if ( SendEvent(wxEVT_GRID_CELL_CHANGE
) == -1 )
8804 // Event has been vetoed, set the data back.
8805 SetCellValue(row
, col
, oldval
);
8812 // ------ Grid location functions
8813 // Note that all of these functions work with the logical coordinates of
8814 // grid cells and labels so you will need to convert from device
8815 // coordinates for mouse events etc.
8818 wxGridCellCoords
wxGrid::XYToCell(int x
, int y
) const
8820 int row
= YToRow(y
);
8821 int col
= XToCol(x
);
8823 return row
== -1 || col
== -1 ? wxGridNoCellCoords
8824 : wxGridCellCoords(row
, col
);
8827 // compute row or column from some (unscrolled) coordinate value, using either
8828 // m_defaultRowHeight/m_defaultColWidth or binary search on array of
8829 // m_rowBottoms/m_colRights to do it quickly (linear search shouldn't be used
8831 int wxGrid::PosToLinePos(int coord
,
8833 const wxGridOperations
& oper
) const
8835 const int numLines
= oper
.GetNumberOfLines(this);
8838 return clipToMinMax
&& numLines
> 0 ? 0 : wxNOT_FOUND
;
8840 const int defaultLineSize
= oper
.GetDefaultLineSize(this);
8841 wxCHECK_MSG( defaultLineSize
, -1, "can't have 0 default line size" );
8843 int maxPos
= coord
/ defaultLineSize
,
8846 // check for the simplest case: if we have no explicit line sizes
8847 // configured, then we already know the line this position falls in
8848 const wxArrayInt
& lineEnds
= oper
.GetLineEnds(this);
8849 if ( lineEnds
.empty() )
8851 if ( maxPos
< numLines
)
8854 return clipToMinMax
? numLines
- 1 : -1;
8858 // adjust maxPos before starting the binary search
8859 if ( maxPos
>= numLines
)
8861 maxPos
= numLines
- 1;
8865 if ( coord
>= lineEnds
[oper
.GetLineAt(this, maxPos
)])
8868 const int minDist
= oper
.GetMinimalAcceptableLineSize(this);
8870 maxPos
= coord
/ minDist
;
8872 maxPos
= numLines
- 1;
8875 if ( maxPos
>= numLines
)
8876 maxPos
= numLines
- 1;
8879 // check if the position is beyond the last column
8880 const int lineAtMaxPos
= oper
.GetLineAt(this, maxPos
);
8881 if ( coord
>= lineEnds
[lineAtMaxPos
] )
8882 return clipToMinMax
? maxPos
: -1;
8884 // or before the first one
8885 const int lineAt0
= oper
.GetLineAt(this, 0);
8886 if ( coord
< lineEnds
[lineAt0
] )
8890 // finally do perform the binary search
8891 while ( minPos
< maxPos
)
8893 wxCHECK_MSG( lineEnds
[oper
.GetLineAt(this, minPos
)] <= coord
&&
8894 coord
< lineEnds
[oper
.GetLineAt(this, maxPos
)],
8896 "wxGrid: internal error in PosToLinePos()" );
8898 if ( coord
>= lineEnds
[oper
.GetLineAt(this, maxPos
- 1)] )
8903 const int median
= minPos
+ (maxPos
- minPos
+ 1) / 2;
8904 if ( coord
< lineEnds
[oper
.GetLineAt(this, median
)] )
8914 wxGrid::PosToLine(int coord
,
8916 const wxGridOperations
& oper
) const
8918 int pos
= PosToLinePos(coord
, clipToMinMax
, oper
);
8920 return pos
== wxNOT_FOUND
? wxNOT_FOUND
: oper
.GetLineAt(this, pos
);
8923 int wxGrid::YToRow(int y
, bool clipToMinMax
) const
8925 return PosToLine(y
, clipToMinMax
, wxGridRowOperations());
8928 int wxGrid::XToCol(int x
, bool clipToMinMax
) const
8930 return PosToLine(x
, clipToMinMax
, wxGridColumnOperations());
8933 int wxGrid::XToPos(int x
) const
8935 return PosToLinePos(x
, true /* clip */, wxGridColumnOperations());
8938 // return the row number that that the y coord is near the edge of, or -1 if
8939 // not near an edge.
8941 // coords can only possibly be near an edge if
8942 // (a) the row/column is large enough to still allow for an "inner" area
8943 // that is _not_ near the edge (i.e., if the height/width is smaller
8944 // than WXGRID_LABEL_EDGE_ZONE, coords are _never_ considered to be
8947 // (b) resizing rows/columns (the thing for which edge detection is
8948 // relevant at all) is enabled.
8950 int wxGrid::PosToEdgeOfLine(int pos
, const wxGridOperations
& oper
) const
8952 if ( !oper
.CanResizeLines(this) )
8955 const int line
= oper
.PosToLine(this, pos
, true);
8957 if ( oper
.GetLineSize(this, line
) > WXGRID_LABEL_EDGE_ZONE
)
8959 // We know that we are in this line, test whether we are close enough
8960 // to start or end border, respectively.
8961 if ( abs(oper
.GetLineEndPos(this, line
) - pos
) < WXGRID_LABEL_EDGE_ZONE
)
8963 else if ( line
> 0 &&
8964 pos
- oper
.GetLineStartPos(this,
8965 line
) < WXGRID_LABEL_EDGE_ZONE
)
8972 int wxGrid::YToEdgeOfRow(int y
) const
8974 return PosToEdgeOfLine(y
, wxGridRowOperations());
8977 int wxGrid::XToEdgeOfCol(int x
) const
8979 return PosToEdgeOfLine(x
, wxGridColumnOperations());
8982 wxRect
wxGrid::CellToRect( int row
, int col
) const
8984 wxRect
rect( -1, -1, -1, -1 );
8986 if ( row
>= 0 && row
< m_numRows
&&
8987 col
>= 0 && col
< m_numCols
)
8989 int i
, cell_rows
, cell_cols
;
8990 rect
.width
= rect
.height
= 0;
8991 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
8992 // if negative then find multicell owner
8997 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
8999 rect
.x
= GetColLeft(col
);
9000 rect
.y
= GetRowTop(row
);
9001 for (i
=col
; i
< col
+ cell_cols
; i
++)
9002 rect
.width
+= GetColWidth(i
);
9003 for (i
=row
; i
< row
+ cell_rows
; i
++)
9004 rect
.height
+= GetRowHeight(i
);
9007 // if grid lines are enabled, then the area of the cell is a bit smaller
9008 if (m_gridLinesEnabled
)
9017 bool wxGrid::IsVisible( int row
, int col
, bool wholeCellVisible
) const
9019 // get the cell rectangle in logical coords
9021 wxRect
r( CellToRect( row
, col
) );
9023 // convert to device coords
9025 int left
, top
, right
, bottom
;
9026 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
9027 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
9029 // check against the client area of the grid window
9031 m_gridWin
->GetClientSize( &cw
, &ch
);
9033 if ( wholeCellVisible
)
9035 // is the cell wholly visible ?
9036 return ( left
>= 0 && right
<= cw
&&
9037 top
>= 0 && bottom
<= ch
);
9041 // is the cell partly visible ?
9043 return ( ((left
>= 0 && left
< cw
) || (right
> 0 && right
<= cw
)) &&
9044 ((top
>= 0 && top
< ch
) || (bottom
> 0 && bottom
<= ch
)) );
9048 // make the specified cell location visible by doing a minimal amount
9051 void wxGrid::MakeCellVisible( int row
, int col
)
9054 int xpos
= -1, ypos
= -1;
9056 if ( row
>= 0 && row
< m_numRows
&&
9057 col
>= 0 && col
< m_numCols
)
9059 // get the cell rectangle in logical coords
9060 wxRect
r( CellToRect( row
, col
) );
9062 // convert to device coords
9063 int left
, top
, right
, bottom
;
9064 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
9065 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
9068 m_gridWin
->GetClientSize( &cw
, &ch
);
9074 else if ( bottom
> ch
)
9076 int h
= r
.GetHeight();
9078 for ( i
= row
- 1; i
>= 0; i
-- )
9080 int rowHeight
= GetRowHeight(i
);
9081 if ( h
+ rowHeight
> ch
)
9088 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
9089 // have rounding errors (this is important, because if we do,
9090 // we might not scroll at all and some cells won't be redrawn)
9092 // Sometimes GRID_SCROLL_LINE / 2 is not enough,
9093 // so just add a full scroll unit...
9094 ypos
+= m_scrollLineY
;
9097 // special handling for wide cells - show always left part of the cell!
9098 // Otherwise, e.g. when stepping from row to row, it would jump between
9099 // left and right part of the cell on every step!
9101 if ( left
< 0 || (right
- left
) >= cw
)
9105 else if ( right
> cw
)
9107 // position the view so that the cell is on the right
9109 CalcUnscrolledPosition(0, 0, &x0
, &y0
);
9110 xpos
= x0
+ (right
- cw
);
9112 // see comment for ypos above
9113 xpos
+= m_scrollLineX
;
9116 if ( xpos
!= -1 || ypos
!= -1 )
9119 xpos
/= m_scrollLineX
;
9121 ypos
/= m_scrollLineY
;
9122 Scroll( xpos
, ypos
);
9129 // ------ Grid cursor movement functions
9133 wxGrid::DoMoveCursor(bool expandSelection
,
9134 const wxGridDirectionOperations
& diroper
)
9136 if ( m_currentCellCoords
== wxGridNoCellCoords
)
9139 if ( expandSelection
)
9141 wxGridCellCoords coords
= m_selectedBlockCorner
;
9142 if ( coords
== wxGridNoCellCoords
)
9143 coords
= m_currentCellCoords
;
9145 if ( diroper
.IsAtBoundary(coords
) )
9148 diroper
.Advance(coords
);
9150 UpdateBlockBeingSelected(m_currentCellCoords
, coords
);
9152 else // don't expand selection
9156 if ( diroper
.IsAtBoundary(m_currentCellCoords
) )
9159 wxGridCellCoords coords
= m_currentCellCoords
;
9160 diroper
.Advance(coords
);
9168 bool wxGrid::MoveCursorUp(bool expandSelection
)
9170 return DoMoveCursor(expandSelection
,
9171 wxGridBackwardOperations(this, wxGridRowOperations()));
9174 bool wxGrid::MoveCursorDown(bool expandSelection
)
9176 return DoMoveCursor(expandSelection
,
9177 wxGridForwardOperations(this, wxGridRowOperations()));
9180 bool wxGrid::MoveCursorLeft(bool expandSelection
)
9182 return DoMoveCursor(expandSelection
,
9183 wxGridBackwardOperations(this, wxGridColumnOperations()));
9186 bool wxGrid::MoveCursorRight(bool expandSelection
)
9188 return DoMoveCursor(expandSelection
,
9189 wxGridForwardOperations(this, wxGridColumnOperations()));
9192 bool wxGrid::DoMoveCursorByPage(const wxGridDirectionOperations
& diroper
)
9194 if ( m_currentCellCoords
== wxGridNoCellCoords
)
9197 if ( diroper
.IsAtBoundary(m_currentCellCoords
) )
9200 const int oldRow
= m_currentCellCoords
.GetRow();
9201 int newRow
= diroper
.MoveByPixelDistance(oldRow
, m_gridWin
->GetClientSize().y
);
9202 if ( newRow
== oldRow
)
9204 wxGridCellCoords
coords(m_currentCellCoords
);
9205 diroper
.Advance(coords
);
9206 newRow
= coords
.GetRow();
9209 GoToCell(newRow
, m_currentCellCoords
.GetCol());
9214 bool wxGrid::MovePageUp()
9216 return DoMoveCursorByPage(
9217 wxGridBackwardOperations(this, wxGridRowOperations()));
9220 bool wxGrid::MovePageDown()
9222 return DoMoveCursorByPage(
9223 wxGridForwardOperations(this, wxGridRowOperations()));
9226 // helper of DoMoveCursorByBlock(): advance the cell coordinates using diroper
9227 // until we find a non-empty cell or reach the grid end
9229 wxGrid::AdvanceToNextNonEmpty(wxGridCellCoords
& coords
,
9230 const wxGridDirectionOperations
& diroper
)
9232 while ( !diroper
.IsAtBoundary(coords
) )
9234 diroper
.Advance(coords
);
9235 if ( !m_table
->IsEmpty(coords
) )
9241 wxGrid::DoMoveCursorByBlock(bool expandSelection
,
9242 const wxGridDirectionOperations
& diroper
)
9244 if ( !m_table
|| m_currentCellCoords
== wxGridNoCellCoords
)
9247 if ( diroper
.IsAtBoundary(m_currentCellCoords
) )
9250 wxGridCellCoords
coords(m_currentCellCoords
);
9251 if ( m_table
->IsEmpty(coords
) )
9253 // we are in an empty cell: find the next block of non-empty cells
9254 AdvanceToNextNonEmpty(coords
, diroper
);
9256 else // current cell is not empty
9258 diroper
.Advance(coords
);
9259 if ( m_table
->IsEmpty(coords
) )
9261 // we started at the end of a block, find the next one
9262 AdvanceToNextNonEmpty(coords
, diroper
);
9264 else // we're in a middle of a block
9266 // go to the end of it, i.e. find the last cell before the next
9268 while ( !diroper
.IsAtBoundary(coords
) )
9270 wxGridCellCoords
coordsNext(coords
);
9271 diroper
.Advance(coordsNext
);
9272 if ( m_table
->IsEmpty(coordsNext
) )
9275 coords
= coordsNext
;
9280 if ( expandSelection
)
9282 UpdateBlockBeingSelected(m_currentCellCoords
, coords
);
9293 bool wxGrid::MoveCursorUpBlock(bool expandSelection
)
9295 return DoMoveCursorByBlock(
9297 wxGridBackwardOperations(this, wxGridRowOperations())
9301 bool wxGrid::MoveCursorDownBlock( bool expandSelection
)
9303 return DoMoveCursorByBlock(
9305 wxGridForwardOperations(this, wxGridRowOperations())
9309 bool wxGrid::MoveCursorLeftBlock( bool expandSelection
)
9311 return DoMoveCursorByBlock(
9313 wxGridBackwardOperations(this, wxGridColumnOperations())
9317 bool wxGrid::MoveCursorRightBlock( bool expandSelection
)
9319 return DoMoveCursorByBlock(
9321 wxGridForwardOperations(this, wxGridColumnOperations())
9326 // ------ Label values and formatting
9329 void wxGrid::GetRowLabelAlignment( int *horiz
, int *vert
) const
9332 *horiz
= m_rowLabelHorizAlign
;
9334 *vert
= m_rowLabelVertAlign
;
9337 void wxGrid::GetColLabelAlignment( int *horiz
, int *vert
) const
9340 *horiz
= m_colLabelHorizAlign
;
9342 *vert
= m_colLabelVertAlign
;
9345 int wxGrid::GetColLabelTextOrientation() const
9347 return m_colLabelTextOrientation
;
9350 wxString
wxGrid::GetRowLabelValue( int row
) const
9354 return m_table
->GetRowLabelValue( row
);
9364 wxString
wxGrid::GetColLabelValue( int col
) const
9368 return m_table
->GetColLabelValue( col
);
9378 void wxGrid::SetRowLabelSize( int width
)
9380 wxASSERT( width
>= 0 || width
== wxGRID_AUTOSIZE
);
9382 if ( width
== wxGRID_AUTOSIZE
)
9384 width
= CalcColOrRowLabelAreaMinSize(wxGRID_ROW
);
9387 if ( width
!= m_rowLabelWidth
)
9391 m_rowLabelWin
->Show( false );
9392 m_cornerLabelWin
->Show( false );
9394 else if ( m_rowLabelWidth
== 0 )
9396 m_rowLabelWin
->Show( true );
9397 if ( m_colLabelHeight
> 0 )
9398 m_cornerLabelWin
->Show( true );
9401 m_rowLabelWidth
= width
;
9403 wxScrolledWindow::Refresh( true );
9407 void wxGrid::SetColLabelSize( int height
)
9409 wxASSERT( height
>=0 || height
== wxGRID_AUTOSIZE
);
9411 if ( height
== wxGRID_AUTOSIZE
)
9413 height
= CalcColOrRowLabelAreaMinSize(wxGRID_COLUMN
);
9416 if ( height
!= m_colLabelHeight
)
9420 m_colWindow
->Show( false );
9421 m_cornerLabelWin
->Show( false );
9423 else if ( m_colLabelHeight
== 0 )
9425 m_colWindow
->Show( true );
9426 if ( m_rowLabelWidth
> 0 )
9427 m_cornerLabelWin
->Show( true );
9430 m_colLabelHeight
= height
;
9432 wxScrolledWindow::Refresh( true );
9436 void wxGrid::SetLabelBackgroundColour( const wxColour
& colour
)
9438 if ( m_labelBackgroundColour
!= colour
)
9440 m_labelBackgroundColour
= colour
;
9441 m_rowLabelWin
->SetBackgroundColour( colour
);
9442 m_colWindow
->SetBackgroundColour( colour
);
9443 m_cornerLabelWin
->SetBackgroundColour( colour
);
9445 if ( !GetBatchCount() )
9447 m_rowLabelWin
->Refresh();
9448 m_colWindow
->Refresh();
9449 m_cornerLabelWin
->Refresh();
9454 void wxGrid::SetLabelTextColour( const wxColour
& colour
)
9456 if ( m_labelTextColour
!= colour
)
9458 m_labelTextColour
= colour
;
9459 if ( !GetBatchCount() )
9461 m_rowLabelWin
->Refresh();
9462 m_colWindow
->Refresh();
9467 void wxGrid::SetLabelFont( const wxFont
& font
)
9470 if ( !GetBatchCount() )
9472 m_rowLabelWin
->Refresh();
9473 m_colWindow
->Refresh();
9477 void wxGrid::SetRowLabelAlignment( int horiz
, int vert
)
9479 // allow old (incorrect) defs to be used
9482 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
9483 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
9484 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
9489 case wxTOP
: vert
= wxALIGN_TOP
; break;
9490 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
9491 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
9494 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
9496 m_rowLabelHorizAlign
= horiz
;
9499 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
9501 m_rowLabelVertAlign
= vert
;
9504 if ( !GetBatchCount() )
9506 m_rowLabelWin
->Refresh();
9510 void wxGrid::SetColLabelAlignment( int horiz
, int vert
)
9512 // allow old (incorrect) defs to be used
9515 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
9516 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
9517 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
9522 case wxTOP
: vert
= wxALIGN_TOP
; break;
9523 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
9524 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
9527 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
9529 m_colLabelHorizAlign
= horiz
;
9532 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
9534 m_colLabelVertAlign
= vert
;
9537 if ( !GetBatchCount() )
9539 m_colWindow
->Refresh();
9543 // Note: under MSW, the default column label font must be changed because it
9544 // does not support vertical printing
9546 // Example: wxFont font(9, wxSWISS, wxNORMAL, wxBOLD);
9547 // pGrid->SetLabelFont(font);
9548 // pGrid->SetColLabelTextOrientation(wxVERTICAL);
9550 void wxGrid::SetColLabelTextOrientation( int textOrientation
)
9552 if ( textOrientation
== wxHORIZONTAL
|| textOrientation
== wxVERTICAL
)
9553 m_colLabelTextOrientation
= textOrientation
;
9555 if ( !GetBatchCount() )
9556 m_colWindow
->Refresh();
9559 void wxGrid::SetRowLabelValue( int row
, const wxString
& s
)
9563 m_table
->SetRowLabelValue( row
, s
);
9564 if ( !GetBatchCount() )
9566 wxRect rect
= CellToRect( row
, 0 );
9567 if ( rect
.height
> 0 )
9569 CalcScrolledPosition(0, rect
.y
, &rect
.x
, &rect
.y
);
9571 rect
.width
= m_rowLabelWidth
;
9572 m_rowLabelWin
->Refresh( true, &rect
);
9578 void wxGrid::SetColLabelValue( int col
, const wxString
& s
)
9582 m_table
->SetColLabelValue( col
, s
);
9583 if ( !GetBatchCount() )
9585 if ( m_useNativeHeader
)
9587 GetGridColHeader()->UpdateColumn(col
);
9591 wxRect rect
= CellToRect( 0, col
);
9592 if ( rect
.width
> 0 )
9594 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &rect
.y
);
9596 rect
.height
= m_colLabelHeight
;
9597 GetColLabelWindow()->Refresh( true, &rect
);
9604 void wxGrid::SetGridLineColour( const wxColour
& colour
)
9606 if ( m_gridLineColour
!= colour
)
9608 m_gridLineColour
= colour
;
9610 if ( GridLinesEnabled() )
9615 void wxGrid::SetCellHighlightColour( const wxColour
& colour
)
9617 if ( m_cellHighlightColour
!= colour
)
9619 m_cellHighlightColour
= colour
;
9621 wxClientDC
dc( m_gridWin
);
9623 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
9624 DrawCellHighlight(dc
, attr
);
9629 void wxGrid::SetCellHighlightPenWidth(int width
)
9631 if (m_cellHighlightPenWidth
!= width
)
9633 m_cellHighlightPenWidth
= width
;
9635 // Just redrawing the cell highlight is not enough since that won't
9636 // make any visible change if the the thickness is getting smaller.
9637 int row
= m_currentCellCoords
.GetRow();
9638 int col
= m_currentCellCoords
.GetCol();
9639 if ( row
== -1 || col
== -1 || GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
9642 wxRect rect
= CellToRect(row
, col
);
9643 m_gridWin
->Refresh(true, &rect
);
9647 void wxGrid::SetCellHighlightROPenWidth(int width
)
9649 if (m_cellHighlightROPenWidth
!= width
)
9651 m_cellHighlightROPenWidth
= width
;
9653 // Just redrawing the cell highlight is not enough since that won't
9654 // make any visible change if the the thickness is getting smaller.
9655 int row
= m_currentCellCoords
.GetRow();
9656 int col
= m_currentCellCoords
.GetCol();
9657 if ( row
== -1 || col
== -1 ||
9658 GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
9661 wxRect rect
= CellToRect(row
, col
);
9662 m_gridWin
->Refresh(true, &rect
);
9666 void wxGrid::RedrawGridLines()
9668 // the lines will be redrawn when the window is thawn
9669 if ( GetBatchCount() )
9672 if ( GridLinesEnabled() )
9674 wxClientDC
dc( m_gridWin
);
9676 DrawAllGridLines( dc
, wxRegion() );
9678 else // remove the grid lines
9680 m_gridWin
->Refresh();
9684 void wxGrid::EnableGridLines( bool enable
)
9686 if ( enable
!= m_gridLinesEnabled
)
9688 m_gridLinesEnabled
= enable
;
9694 void wxGrid::DoClipGridLines(bool& var
, bool clip
)
9700 if ( GridLinesEnabled() )
9705 int wxGrid::GetDefaultRowSize() const
9707 return m_defaultRowHeight
;
9710 int wxGrid::GetRowSize( int row
) const
9712 wxCHECK_MSG( row
>= 0 && row
< m_numRows
, 0, _T("invalid row index") );
9714 return GetRowHeight(row
);
9717 int wxGrid::GetDefaultColSize() const
9719 return m_defaultColWidth
;
9722 int wxGrid::GetColSize( int col
) const
9724 wxCHECK_MSG( col
>= 0 && col
< m_numCols
, 0, _T("invalid column index") );
9726 return GetColWidth(col
);
9729 // ============================================================================
9730 // access to the grid attributes: each of them has a default value in the grid
9731 // itself and may be overidden on a per-cell basis
9732 // ============================================================================
9734 // ----------------------------------------------------------------------------
9735 // setting default attributes
9736 // ----------------------------------------------------------------------------
9738 void wxGrid::SetDefaultCellBackgroundColour( const wxColour
& col
)
9740 m_defaultCellAttr
->SetBackgroundColour(col
);
9742 m_gridWin
->SetBackgroundColour(col
);
9746 void wxGrid::SetDefaultCellTextColour( const wxColour
& col
)
9748 m_defaultCellAttr
->SetTextColour(col
);
9751 void wxGrid::SetDefaultCellAlignment( int horiz
, int vert
)
9753 m_defaultCellAttr
->SetAlignment(horiz
, vert
);
9756 void wxGrid::SetDefaultCellOverflow( bool allow
)
9758 m_defaultCellAttr
->SetOverflow(allow
);
9761 void wxGrid::SetDefaultCellFont( const wxFont
& font
)
9763 m_defaultCellAttr
->SetFont(font
);
9766 // For editors and renderers the type registry takes precedence over the
9767 // default attr, so we need to register the new editor/renderer for the string
9768 // data type in order to make setting a default editor/renderer appear to
9771 void wxGrid::SetDefaultRenderer(wxGridCellRenderer
*renderer
)
9773 RegisterDataType(wxGRID_VALUE_STRING
,
9775 GetDefaultEditorForType(wxGRID_VALUE_STRING
));
9778 void wxGrid::SetDefaultEditor(wxGridCellEditor
*editor
)
9780 RegisterDataType(wxGRID_VALUE_STRING
,
9781 GetDefaultRendererForType(wxGRID_VALUE_STRING
),
9785 // ----------------------------------------------------------------------------
9786 // access to the default attributes
9787 // ----------------------------------------------------------------------------
9789 wxColour
wxGrid::GetDefaultCellBackgroundColour() const
9791 return m_defaultCellAttr
->GetBackgroundColour();
9794 wxColour
wxGrid::GetDefaultCellTextColour() const
9796 return m_defaultCellAttr
->GetTextColour();
9799 wxFont
wxGrid::GetDefaultCellFont() const
9801 return m_defaultCellAttr
->GetFont();
9804 void wxGrid::GetDefaultCellAlignment( int *horiz
, int *vert
) const
9806 m_defaultCellAttr
->GetAlignment(horiz
, vert
);
9809 bool wxGrid::GetDefaultCellOverflow() const
9811 return m_defaultCellAttr
->GetOverflow();
9814 wxGridCellRenderer
*wxGrid::GetDefaultRenderer() const
9816 return m_defaultCellAttr
->GetRenderer(NULL
, 0, 0);
9819 wxGridCellEditor
*wxGrid::GetDefaultEditor() const
9821 return m_defaultCellAttr
->GetEditor(NULL
, 0, 0);
9824 // ----------------------------------------------------------------------------
9825 // access to cell attributes
9826 // ----------------------------------------------------------------------------
9828 wxColour
wxGrid::GetCellBackgroundColour(int row
, int col
) const
9830 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9831 wxColour colour
= attr
->GetBackgroundColour();
9837 wxColour
wxGrid::GetCellTextColour( int row
, int col
) const
9839 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9840 wxColour colour
= attr
->GetTextColour();
9846 wxFont
wxGrid::GetCellFont( int row
, int col
) const
9848 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9849 wxFont font
= attr
->GetFont();
9855 void wxGrid::GetCellAlignment( int row
, int col
, int *horiz
, int *vert
) const
9857 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9858 attr
->GetAlignment(horiz
, vert
);
9862 bool wxGrid::GetCellOverflow( int row
, int col
) const
9864 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9865 bool allow
= attr
->GetOverflow();
9871 void wxGrid::GetCellSize( int row
, int col
, int *num_rows
, int *num_cols
) const
9873 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9874 attr
->GetSize( num_rows
, num_cols
);
9878 wxGridCellRenderer
* wxGrid::GetCellRenderer(int row
, int col
) const
9880 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9881 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
9887 wxGridCellEditor
* wxGrid::GetCellEditor(int row
, int col
) const
9889 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9890 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
9896 bool wxGrid::IsReadOnly(int row
, int col
) const
9898 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9899 bool isReadOnly
= attr
->IsReadOnly();
9905 // ----------------------------------------------------------------------------
9906 // attribute support: cache, automatic provider creation, ...
9907 // ----------------------------------------------------------------------------
9909 bool wxGrid::CanHaveAttributes() const
9916 return m_table
->CanHaveAttributes();
9919 void wxGrid::ClearAttrCache()
9921 if ( m_attrCache
.row
!= -1 )
9923 wxGridCellAttr
*oldAttr
= m_attrCache
.attr
;
9924 m_attrCache
.attr
= NULL
;
9925 m_attrCache
.row
= -1;
9926 // wxSafeDecRec(...) might cause event processing that accesses
9927 // the cached attribute, if one exists (e.g. by deleting the
9928 // editor stored within the attribute). Therefore it is important
9929 // to invalidate the cache before calling wxSafeDecRef!
9930 wxSafeDecRef(oldAttr
);
9934 void wxGrid::CacheAttr(int row
, int col
, wxGridCellAttr
*attr
) const
9938 wxGrid
*self
= (wxGrid
*)this; // const_cast
9940 self
->ClearAttrCache();
9941 self
->m_attrCache
.row
= row
;
9942 self
->m_attrCache
.col
= col
;
9943 self
->m_attrCache
.attr
= attr
;
9948 bool wxGrid::LookupAttr(int row
, int col
, wxGridCellAttr
**attr
) const
9950 if ( row
== m_attrCache
.row
&& col
== m_attrCache
.col
)
9952 *attr
= m_attrCache
.attr
;
9953 wxSafeIncRef(m_attrCache
.attr
);
9955 #ifdef DEBUG_ATTR_CACHE
9956 gs_nAttrCacheHits
++;
9963 #ifdef DEBUG_ATTR_CACHE
9964 gs_nAttrCacheMisses
++;
9971 wxGridCellAttr
*wxGrid::GetCellAttr(int row
, int col
) const
9973 wxGridCellAttr
*attr
= NULL
;
9974 // Additional test to avoid looking at the cache e.g. for
9975 // wxNoCellCoords, as this will confuse memory management.
9978 if ( !LookupAttr(row
, col
, &attr
) )
9980 attr
= m_table
? m_table
->GetAttr(row
, col
, wxGridCellAttr::Any
)
9982 CacheAttr(row
, col
, attr
);
9988 attr
->SetDefAttr(m_defaultCellAttr
);
9992 attr
= m_defaultCellAttr
;
9999 wxGridCellAttr
*wxGrid::GetOrCreateCellAttr(int row
, int col
) const
10001 wxGridCellAttr
*attr
= NULL
;
10002 bool canHave
= ((wxGrid
*)this)->CanHaveAttributes();
10004 wxCHECK_MSG( canHave
, attr
, _T("Cell attributes not allowed"));
10005 wxCHECK_MSG( m_table
, attr
, _T("must have a table") );
10007 attr
= m_table
->GetAttr(row
, col
, wxGridCellAttr::Cell
);
10010 attr
= new wxGridCellAttr(m_defaultCellAttr
);
10012 // artificially inc the ref count to match DecRef() in caller
10014 m_table
->SetAttr(attr
, row
, col
);
10020 // ----------------------------------------------------------------------------
10021 // setting column attributes (wrappers around SetColAttr)
10022 // ----------------------------------------------------------------------------
10024 void wxGrid::SetColFormatBool(int col
)
10026 SetColFormatCustom(col
, wxGRID_VALUE_BOOL
);
10029 void wxGrid::SetColFormatNumber(int col
)
10031 SetColFormatCustom(col
, wxGRID_VALUE_NUMBER
);
10034 void wxGrid::SetColFormatFloat(int col
, int width
, int precision
)
10036 wxString typeName
= wxGRID_VALUE_FLOAT
;
10037 if ( (width
!= -1) || (precision
!= -1) )
10039 typeName
<< _T(':') << width
<< _T(',') << precision
;
10042 SetColFormatCustom(col
, typeName
);
10045 void wxGrid::SetColFormatCustom(int col
, const wxString
& typeName
)
10047 wxGridCellAttr
*attr
= m_table
->GetAttr(-1, col
, wxGridCellAttr::Col
);
10049 attr
= new wxGridCellAttr
;
10050 wxGridCellRenderer
*renderer
= GetDefaultRendererForType(typeName
);
10051 attr
->SetRenderer(renderer
);
10052 wxGridCellEditor
*editor
= GetDefaultEditorForType(typeName
);
10053 attr
->SetEditor(editor
);
10055 SetColAttr(col
, attr
);
10059 // ----------------------------------------------------------------------------
10060 // setting cell attributes: this is forwarded to the table
10061 // ----------------------------------------------------------------------------
10063 void wxGrid::SetAttr(int row
, int col
, wxGridCellAttr
*attr
)
10065 if ( CanHaveAttributes() )
10067 m_table
->SetAttr(attr
, row
, col
);
10072 wxSafeDecRef(attr
);
10076 void wxGrid::SetRowAttr(int row
, wxGridCellAttr
*attr
)
10078 if ( CanHaveAttributes() )
10080 m_table
->SetRowAttr(attr
, row
);
10085 wxSafeDecRef(attr
);
10089 void wxGrid::SetColAttr(int col
, wxGridCellAttr
*attr
)
10091 if ( CanHaveAttributes() )
10093 m_table
->SetColAttr(attr
, col
);
10098 wxSafeDecRef(attr
);
10102 void wxGrid::SetCellBackgroundColour( int row
, int col
, const wxColour
& colour
)
10104 if ( CanHaveAttributes() )
10106 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10107 attr
->SetBackgroundColour(colour
);
10112 void wxGrid::SetCellTextColour( int row
, int col
, const wxColour
& colour
)
10114 if ( CanHaveAttributes() )
10116 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10117 attr
->SetTextColour(colour
);
10122 void wxGrid::SetCellFont( int row
, int col
, const wxFont
& font
)
10124 if ( CanHaveAttributes() )
10126 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10127 attr
->SetFont(font
);
10132 void wxGrid::SetCellAlignment( int row
, int col
, int horiz
, int vert
)
10134 if ( CanHaveAttributes() )
10136 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10137 attr
->SetAlignment(horiz
, vert
);
10142 void wxGrid::SetCellOverflow( int row
, int col
, bool allow
)
10144 if ( CanHaveAttributes() )
10146 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10147 attr
->SetOverflow(allow
);
10152 void wxGrid::SetCellSize( int row
, int col
, int num_rows
, int num_cols
)
10154 if ( CanHaveAttributes() )
10156 int cell_rows
, cell_cols
;
10158 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10159 attr
->GetSize(&cell_rows
, &cell_cols
);
10160 attr
->SetSize(num_rows
, num_cols
);
10163 // Cannot set the size of a cell to 0 or negative values
10164 // While it is perfectly legal to do that, this function cannot
10165 // handle all the possibilies, do it by hand by getting the CellAttr.
10166 // You can only set the size of a cell to 1,1 or greater with this fn
10167 wxASSERT_MSG( !((cell_rows
< 1) || (cell_cols
< 1)),
10168 wxT("wxGrid::SetCellSize setting cell size that is already part of another cell"));
10169 wxASSERT_MSG( !((num_rows
< 1) || (num_cols
< 1)),
10170 wxT("wxGrid::SetCellSize setting cell size to < 1"));
10172 // if this was already a multicell then "turn off" the other cells first
10173 if ((cell_rows
> 1) || (cell_cols
> 1))
10176 for (j
=row
; j
< row
+ cell_rows
; j
++)
10178 for (i
=col
; i
< col
+ cell_cols
; i
++)
10180 if ((i
!= col
) || (j
!= row
))
10182 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
10183 attr_stub
->SetSize( 1, 1 );
10184 attr_stub
->DecRef();
10190 // mark the cells that will be covered by this cell to
10191 // negative or zero values to point back at this cell
10192 if (((num_rows
> 1) || (num_cols
> 1)) && (num_rows
>= 1) && (num_cols
>= 1))
10195 for (j
=row
; j
< row
+ num_rows
; j
++)
10197 for (i
=col
; i
< col
+ num_cols
; i
++)
10199 if ((i
!= col
) || (j
!= row
))
10201 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
10202 attr_stub
->SetSize( row
- j
, col
- i
);
10203 attr_stub
->DecRef();
10211 void wxGrid::SetCellRenderer(int row
, int col
, wxGridCellRenderer
*renderer
)
10213 if ( CanHaveAttributes() )
10215 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10216 attr
->SetRenderer(renderer
);
10221 void wxGrid::SetCellEditor(int row
, int col
, wxGridCellEditor
* editor
)
10223 if ( CanHaveAttributes() )
10225 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10226 attr
->SetEditor(editor
);
10231 void wxGrid::SetReadOnly(int row
, int col
, bool isReadOnly
)
10233 if ( CanHaveAttributes() )
10235 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10236 attr
->SetReadOnly(isReadOnly
);
10241 // ----------------------------------------------------------------------------
10242 // Data type registration
10243 // ----------------------------------------------------------------------------
10245 void wxGrid::RegisterDataType(const wxString
& typeName
,
10246 wxGridCellRenderer
* renderer
,
10247 wxGridCellEditor
* editor
)
10249 m_typeRegistry
->RegisterDataType(typeName
, renderer
, editor
);
10253 wxGridCellEditor
* wxGrid::GetDefaultEditorForCell(int row
, int col
) const
10255 wxString typeName
= m_table
->GetTypeName(row
, col
);
10256 return GetDefaultEditorForType(typeName
);
10259 wxGridCellRenderer
* wxGrid::GetDefaultRendererForCell(int row
, int col
) const
10261 wxString typeName
= m_table
->GetTypeName(row
, col
);
10262 return GetDefaultRendererForType(typeName
);
10265 wxGridCellEditor
* wxGrid::GetDefaultEditorForType(const wxString
& typeName
) const
10267 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
10268 if ( index
== wxNOT_FOUND
)
10270 wxFAIL_MSG(wxString::Format(wxT("Unknown data type name [%s]"), typeName
.c_str()));
10275 return m_typeRegistry
->GetEditor(index
);
10278 wxGridCellRenderer
* wxGrid::GetDefaultRendererForType(const wxString
& typeName
) const
10280 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
10281 if ( index
== wxNOT_FOUND
)
10283 wxFAIL_MSG(wxString::Format(wxT("Unknown data type name [%s]"), typeName
.c_str()));
10288 return m_typeRegistry
->GetRenderer(index
);
10291 // ----------------------------------------------------------------------------
10293 // ----------------------------------------------------------------------------
10295 void wxGrid::EnableDragRowSize( bool enable
)
10297 m_canDragRowSize
= enable
;
10300 void wxGrid::EnableDragColSize( bool enable
)
10302 m_canDragColSize
= enable
;
10305 void wxGrid::EnableDragGridSize( bool enable
)
10307 m_canDragGridSize
= enable
;
10310 void wxGrid::EnableDragCell( bool enable
)
10312 m_canDragCell
= enable
;
10315 void wxGrid::SetDefaultRowSize( int height
, bool resizeExistingRows
)
10317 m_defaultRowHeight
= wxMax( height
, m_minAcceptableRowHeight
);
10319 if ( resizeExistingRows
)
10321 // since we are resizing all rows to the default row size,
10322 // we can simply clear the row heights and row bottoms
10323 // arrays (which also allows us to take advantage of
10324 // some speed optimisations)
10325 m_rowHeights
.Empty();
10326 m_rowBottoms
.Empty();
10327 if ( !GetBatchCount() )
10332 void wxGrid::SetRowSize( int row
, int height
)
10334 wxCHECK_RET( row
>= 0 && row
< m_numRows
, _T("invalid row index") );
10336 // if < 0 then calculate new height from label
10340 wxArrayString lines
;
10341 wxClientDC
dc(m_rowLabelWin
);
10342 dc
.SetFont(GetLabelFont());
10343 StringToLines(GetRowLabelValue( row
), lines
);
10344 GetTextBoxSize( dc
, lines
, &w
, &h
);
10345 //check that it is not less than the minimal height
10346 height
= wxMax(h
, GetRowMinimalAcceptableHeight());
10349 // See comment in SetColSize
10350 if ( height
< GetRowMinimalAcceptableHeight())
10353 if ( m_rowHeights
.IsEmpty() )
10355 // need to really create the array
10359 int h
= wxMax( 0, height
);
10360 int diff
= h
- m_rowHeights
[row
];
10362 m_rowHeights
[row
] = h
;
10363 for ( int i
= row
; i
< m_numRows
; i
++ )
10365 m_rowBottoms
[i
] += diff
;
10368 if ( !GetBatchCount() )
10372 void wxGrid::SetDefaultColSize( int width
, bool resizeExistingCols
)
10374 // we dont allow zero default column width
10375 m_defaultColWidth
= wxMax( wxMax( width
, m_minAcceptableColWidth
), 1 );
10377 if ( resizeExistingCols
)
10379 // since we are resizing all columns to the default column size,
10380 // we can simply clear the col widths and col rights
10381 // arrays (which also allows us to take advantage of
10382 // some speed optimisations)
10383 m_colWidths
.Empty();
10384 m_colRights
.Empty();
10385 if ( !GetBatchCount() )
10390 void wxGrid::SetColSize( int col
, int width
)
10392 wxCHECK_RET( col
>= 0 && col
< m_numCols
, _T("invalid column index") );
10394 // if < 0 then calculate new width from label
10398 wxArrayString lines
;
10399 wxClientDC
dc(m_colWindow
);
10400 dc
.SetFont(GetLabelFont());
10401 StringToLines(GetColLabelValue(col
), lines
);
10402 if ( GetColLabelTextOrientation() == wxHORIZONTAL
)
10403 GetTextBoxSize( dc
, lines
, &w
, &h
);
10405 GetTextBoxSize( dc
, lines
, &h
, &w
);
10407 //check that it is not less than the minimal width
10408 width
= wxMax(width
, GetColMinimalAcceptableWidth());
10411 // we intentionally don't test whether the width is less than
10412 // GetColMinimalWidth() here but we do compare it with
10413 // GetColMinimalAcceptableWidth() as otherwise things currently break (see
10414 // #651) -- and we also always allow the width of 0 as it has the special
10415 // sense of hiding the column
10416 if ( width
> 0 && width
< GetColMinimalAcceptableWidth() )
10419 if ( m_colWidths
.IsEmpty() )
10421 // need to really create the array
10425 const int diff
= width
- m_colWidths
[col
];
10426 m_colWidths
[col
] = width
;
10427 if ( m_useNativeHeader
)
10428 GetGridColHeader()->UpdateColumn(col
);
10429 //else: will be refreshed when the header is redrawn
10431 for ( int colPos
= GetColPos(col
); colPos
< m_numCols
; colPos
++ )
10433 m_colRights
[GetColAt(colPos
)] += diff
;
10436 if ( !GetBatchCount() )
10443 void wxGrid::SetColMinimalWidth( int col
, int width
)
10445 if (width
> GetColMinimalAcceptableWidth())
10447 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)col
;
10448 m_colMinWidths
[key
] = width
;
10452 void wxGrid::SetRowMinimalHeight( int row
, int width
)
10454 if (width
> GetRowMinimalAcceptableHeight())
10456 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)row
;
10457 m_rowMinHeights
[key
] = width
;
10461 int wxGrid::GetColMinimalWidth(int col
) const
10463 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)col
;
10464 wxLongToLongHashMap::const_iterator it
= m_colMinWidths
.find(key
);
10466 return it
!= m_colMinWidths
.end() ? (int)it
->second
: m_minAcceptableColWidth
;
10469 int wxGrid::GetRowMinimalHeight(int row
) const
10471 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)row
;
10472 wxLongToLongHashMap::const_iterator it
= m_rowMinHeights
.find(key
);
10474 return it
!= m_rowMinHeights
.end() ? (int)it
->second
: m_minAcceptableRowHeight
;
10477 void wxGrid::SetColMinimalAcceptableWidth( int width
)
10479 // We do allow a width of 0 since this gives us
10480 // an easy way to temporarily hiding columns.
10482 m_minAcceptableColWidth
= width
;
10485 void wxGrid::SetRowMinimalAcceptableHeight( int height
)
10487 // We do allow a height of 0 since this gives us
10488 // an easy way to temporarily hiding rows.
10490 m_minAcceptableRowHeight
= height
;
10493 int wxGrid::GetColMinimalAcceptableWidth() const
10495 return m_minAcceptableColWidth
;
10498 int wxGrid::GetRowMinimalAcceptableHeight() const
10500 return m_minAcceptableRowHeight
;
10503 // ----------------------------------------------------------------------------
10505 // ----------------------------------------------------------------------------
10508 wxGrid::AutoSizeColOrRow(int colOrRow
, bool setAsMin
, wxGridDirection direction
)
10510 const bool column
= direction
== wxGRID_COLUMN
;
10512 wxClientDC
dc(m_gridWin
);
10514 // cancel editing of cell
10515 HideCellEditControl();
10516 SaveEditControlValue();
10518 // init both of them to avoid compiler warnings, even if we only need one
10526 wxCoord extent
, extentMax
= 0;
10527 int max
= column
? m_numRows
: m_numCols
;
10528 for ( int rowOrCol
= 0; rowOrCol
< max
; rowOrCol
++ )
10535 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
10536 wxGridCellRenderer
*renderer
= attr
->GetRenderer(this, row
, col
);
10539 wxSize size
= renderer
->GetBestSize(*this, *attr
, dc
, row
, col
);
10540 extent
= column
? size
.x
: size
.y
;
10541 if ( extent
> extentMax
)
10542 extentMax
= extent
;
10544 renderer
->DecRef();
10550 // now also compare with the column label extent
10552 dc
.SetFont( GetLabelFont() );
10556 dc
.GetMultiLineTextExtent( GetColLabelValue(col
), &w
, &h
);
10557 if ( GetColLabelTextOrientation() == wxVERTICAL
)
10561 dc
.GetMultiLineTextExtent( GetRowLabelValue(row
), &w
, &h
);
10563 extent
= column
? w
: h
;
10564 if ( extent
> extentMax
)
10565 extentMax
= extent
;
10569 // empty column - give default extent (notice that if extentMax is less
10570 // than default extent but != 0, it's OK)
10571 extentMax
= column
? m_defaultColWidth
: m_defaultRowHeight
;
10576 // leave some space around text
10584 // Ensure automatic width is not less than minimal width. See the
10585 // comment in SetColSize() for explanation of why this isn't done
10586 // in SetColSize().
10588 extentMax
= wxMax(extentMax
, GetColMinimalWidth(col
));
10590 SetColSize( col
, extentMax
);
10591 if ( !GetBatchCount() )
10593 if ( m_useNativeHeader
)
10595 GetGridColHeader()->UpdateColumn(col
);
10600 m_gridWin
->GetClientSize( &cw
, &ch
);
10601 wxRect
rect ( CellToRect( 0, col
) );
10603 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
10604 rect
.width
= cw
- rect
.x
;
10605 rect
.height
= m_colLabelHeight
;
10606 GetColLabelWindow()->Refresh( true, &rect
);
10612 // Ensure automatic width is not less than minimal height. See the
10613 // comment in SetColSize() for explanation of why this isn't done
10614 // in SetRowSize().
10616 extentMax
= wxMax(extentMax
, GetRowMinimalHeight(row
));
10618 SetRowSize(row
, extentMax
);
10619 if ( !GetBatchCount() )
10622 m_gridWin
->GetClientSize( &cw
, &ch
);
10623 wxRect
rect( CellToRect( row
, 0 ) );
10625 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
10626 rect
.width
= m_rowLabelWidth
;
10627 rect
.height
= ch
- rect
.y
;
10628 m_rowLabelWin
->Refresh( true, &rect
);
10635 SetColMinimalWidth(col
, extentMax
);
10637 SetRowMinimalHeight(row
, extentMax
);
10641 wxCoord
wxGrid::CalcColOrRowLabelAreaMinSize(wxGridDirection direction
)
10643 // calculate size for the rows or columns?
10644 const bool calcRows
= direction
== wxGRID_ROW
;
10646 wxClientDC
dc(calcRows
? GetGridRowLabelWindow()
10647 : GetGridColLabelWindow());
10648 dc
.SetFont(GetLabelFont());
10650 // which dimension should we take into account for calculations?
10652 // for columns, the text can be only horizontal so it's easy but for rows
10653 // we also have to take into account the text orientation
10655 useWidth
= calcRows
|| (GetColLabelTextOrientation() == wxVERTICAL
);
10657 wxArrayString lines
;
10658 wxCoord extentMax
= 0;
10660 const int numRowsOrCols
= calcRows
? m_numRows
: m_numCols
;
10661 for ( int rowOrCol
= 0; rowOrCol
< numRowsOrCols
; rowOrCol
++ )
10665 wxString label
= calcRows
? GetRowLabelValue(rowOrCol
)
10666 : GetColLabelValue(rowOrCol
);
10667 StringToLines(label
, lines
);
10670 GetTextBoxSize(dc
, lines
, &w
, &h
);
10672 const wxCoord extent
= useWidth
? w
: h
;
10673 if ( extent
> extentMax
)
10674 extentMax
= extent
;
10679 // empty column - give default extent (notice that if extentMax is less
10680 // than default extent but != 0, it's OK)
10681 extentMax
= calcRows
? GetDefaultRowLabelSize()
10682 : GetDefaultColLabelSize();
10685 // leave some space around text (taken from AutoSizeColOrRow)
10694 int wxGrid::SetOrCalcColumnSizes(bool calcOnly
, bool setAsMin
)
10696 int width
= m_rowLabelWidth
;
10698 wxGridUpdateLocker locker
;
10700 locker
.Create(this);
10702 for ( int col
= 0; col
< m_numCols
; col
++ )
10705 AutoSizeColumn(col
, setAsMin
);
10707 width
+= GetColWidth(col
);
10713 int wxGrid::SetOrCalcRowSizes(bool calcOnly
, bool setAsMin
)
10715 int height
= m_colLabelHeight
;
10717 wxGridUpdateLocker locker
;
10719 locker
.Create(this);
10721 for ( int row
= 0; row
< m_numRows
; row
++ )
10724 AutoSizeRow(row
, setAsMin
);
10726 height
+= GetRowHeight(row
);
10732 void wxGrid::AutoSize()
10734 wxGridUpdateLocker
locker(this);
10736 wxSize
size(SetOrCalcColumnSizes(false) - m_rowLabelWidth
+ m_extraWidth
,
10737 SetOrCalcRowSizes(false) - m_colLabelHeight
+ m_extraHeight
);
10739 // we know that we're not going to have scrollbars so disable them now to
10740 // avoid trouble in SetClientSize() which can otherwise set the correct
10741 // client size but also leave space for (not needed any more) scrollbars
10742 SetScrollbars(0, 0, 0, 0, 0, 0, true);
10744 // restore the scroll rate parameters overwritten by SetScrollbars()
10745 SetScrollRate(m_scrollLineX
, m_scrollLineY
);
10747 SetClientSize(size
.x
+ m_rowLabelWidth
, size
.y
+ m_colLabelHeight
);
10750 void wxGrid::AutoSizeRowLabelSize( int row
)
10752 // Hide the edit control, so it
10753 // won't interfere with drag-shrinking.
10754 if ( IsCellEditControlShown() )
10756 HideCellEditControl();
10757 SaveEditControlValue();
10760 // autosize row height depending on label text
10761 SetRowSize(row
, -1);
10765 void wxGrid::AutoSizeColLabelSize( int col
)
10767 // Hide the edit control, so it
10768 // won't interfere with drag-shrinking.
10769 if ( IsCellEditControlShown() )
10771 HideCellEditControl();
10772 SaveEditControlValue();
10775 // autosize column width depending on label text
10776 SetColSize(col
, -1);
10780 wxSize
wxGrid::DoGetBestSize() const
10782 wxGrid
*self
= (wxGrid
*)this; // const_cast
10784 // we do the same as in AutoSize() here with the exception that we don't
10785 // change the column/row sizes, only calculate them
10786 wxSize
size(self
->SetOrCalcColumnSizes(true) - m_rowLabelWidth
+ m_extraWidth
,
10787 self
->SetOrCalcRowSizes(true) - m_colLabelHeight
+ m_extraHeight
);
10789 // NOTE: This size should be cached, but first we need to add calls to
10790 // InvalidateBestSize everywhere that could change the results of this
10792 // CacheBestSize(size);
10794 return wxSize(size
.x
+ m_rowLabelWidth
, size
.y
+ m_colLabelHeight
)
10795 + GetWindowBorderSize();
10803 wxPen
& wxGrid::GetDividerPen() const
10808 // ----------------------------------------------------------------------------
10809 // cell value accessor functions
10810 // ----------------------------------------------------------------------------
10812 void wxGrid::SetCellValue( int row
, int col
, const wxString
& s
)
10816 m_table
->SetValue( row
, col
, s
);
10817 if ( !GetBatchCount() )
10820 wxRect
rect( CellToRect( row
, col
) );
10822 rect
.width
= m_gridWin
->GetClientSize().GetWidth();
10823 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
10824 m_gridWin
->Refresh( false, &rect
);
10827 if ( m_currentCellCoords
.GetRow() == row
&&
10828 m_currentCellCoords
.GetCol() == col
&&
10829 IsCellEditControlShown())
10830 // Note: If we are using IsCellEditControlEnabled,
10831 // this interacts badly with calling SetCellValue from
10832 // an EVT_GRID_CELL_CHANGE handler.
10834 HideCellEditControl();
10835 ShowCellEditControl(); // will reread data from table
10840 // ----------------------------------------------------------------------------
10841 // block, row and column selection
10842 // ----------------------------------------------------------------------------
10844 void wxGrid::SelectRow( int row
, bool addToSelected
)
10846 if ( !m_selection
)
10849 if ( !addToSelected
)
10852 m_selection
->SelectRow(row
);
10855 void wxGrid::SelectCol( int col
, bool addToSelected
)
10857 if ( !m_selection
)
10860 if ( !addToSelected
)
10863 m_selection
->SelectCol(col
);
10866 void wxGrid::SelectBlock(int topRow
, int leftCol
, int bottomRow
, int rightCol
,
10867 bool addToSelected
)
10869 if ( !m_selection
)
10872 if ( !addToSelected
)
10875 m_selection
->SelectBlock(topRow
, leftCol
, bottomRow
, rightCol
);
10878 void wxGrid::SelectAll()
10880 if ( m_numRows
> 0 && m_numCols
> 0 )
10883 m_selection
->SelectBlock( 0, 0, m_numRows
- 1, m_numCols
- 1 );
10887 // ----------------------------------------------------------------------------
10888 // cell, row and col deselection
10889 // ----------------------------------------------------------------------------
10891 void wxGrid::DeselectLine(int line
, const wxGridOperations
& oper
)
10893 if ( !m_selection
)
10896 const wxGridSelectionModes mode
= m_selection
->GetSelectionMode();
10897 if ( mode
== oper
.GetSelectionMode() )
10899 const wxGridCellCoords
c(oper
.MakeCoords(line
, 0));
10900 if ( m_selection
->IsInSelection(c
) )
10901 m_selection
->ToggleCellSelection(c
);
10903 else if ( mode
!= oper
.Dual().GetSelectionMode() )
10905 const int nOther
= oper
.Dual().GetNumberOfLines(this);
10906 for ( int i
= 0; i
< nOther
; i
++ )
10908 const wxGridCellCoords
c(oper
.MakeCoords(line
, i
));
10909 if ( m_selection
->IsInSelection(c
) )
10910 m_selection
->ToggleCellSelection(c
);
10913 //else: can only select orthogonal lines so no lines in this direction
10914 // could have been selected anyhow
10917 void wxGrid::DeselectRow(int row
)
10919 DeselectLine(row
, wxGridRowOperations());
10922 void wxGrid::DeselectCol(int col
)
10924 DeselectLine(col
, wxGridColumnOperations());
10927 void wxGrid::DeselectCell( int row
, int col
)
10929 if ( m_selection
&& m_selection
->IsInSelection(row
, col
) )
10930 m_selection
->ToggleCellSelection(row
, col
);
10933 bool wxGrid::IsSelection() const
10935 return ( m_selection
&& (m_selection
->IsSelection() ||
10936 ( m_selectedBlockTopLeft
!= wxGridNoCellCoords
&&
10937 m_selectedBlockBottomRight
!= wxGridNoCellCoords
) ) );
10940 bool wxGrid::IsInSelection( int row
, int col
) const
10942 return ( m_selection
&& (m_selection
->IsInSelection( row
, col
) ||
10943 ( row
>= m_selectedBlockTopLeft
.GetRow() &&
10944 col
>= m_selectedBlockTopLeft
.GetCol() &&
10945 row
<= m_selectedBlockBottomRight
.GetRow() &&
10946 col
<= m_selectedBlockBottomRight
.GetCol() )) );
10949 wxGridCellCoordsArray
wxGrid::GetSelectedCells() const
10953 wxGridCellCoordsArray a
;
10957 return m_selection
->m_cellSelection
;
10960 wxGridCellCoordsArray
wxGrid::GetSelectionBlockTopLeft() const
10964 wxGridCellCoordsArray a
;
10968 return m_selection
->m_blockSelectionTopLeft
;
10971 wxGridCellCoordsArray
wxGrid::GetSelectionBlockBottomRight() const
10975 wxGridCellCoordsArray a
;
10979 return m_selection
->m_blockSelectionBottomRight
;
10982 wxArrayInt
wxGrid::GetSelectedRows() const
10990 return m_selection
->m_rowSelection
;
10993 wxArrayInt
wxGrid::GetSelectedCols() const
11001 return m_selection
->m_colSelection
;
11004 void wxGrid::ClearSelection()
11006 wxRect r1
= BlockToDeviceRect(m_selectedBlockTopLeft
,
11007 m_selectedBlockBottomRight
);
11008 wxRect r2
= BlockToDeviceRect(m_currentCellCoords
,
11009 m_selectedBlockCorner
);
11011 m_selectedBlockTopLeft
=
11012 m_selectedBlockBottomRight
=
11013 m_selectedBlockCorner
= wxGridNoCellCoords
;
11015 Refresh( false, &r1
);
11016 Refresh( false, &r2
);
11019 m_selection
->ClearSelection();
11022 // This function returns the rectangle that encloses the given block
11023 // in device coords clipped to the client size of the grid window.
11025 wxRect
wxGrid::BlockToDeviceRect( const wxGridCellCoords
& topLeft
,
11026 const wxGridCellCoords
& bottomRight
) const
11029 wxRect tempCellRect
= CellToRect(topLeft
);
11030 if ( tempCellRect
!= wxGridNoCellRect
)
11032 resultRect
= tempCellRect
;
11036 resultRect
= wxRect(0, 0, 0, 0);
11039 tempCellRect
= CellToRect(bottomRight
);
11040 if ( tempCellRect
!= wxGridNoCellRect
)
11042 resultRect
+= tempCellRect
;
11046 // If both inputs were "wxGridNoCellRect," then there's nothing to do.
11047 return wxGridNoCellRect
;
11050 // Ensure that left/right and top/bottom pairs are in order.
11051 int left
= resultRect
.GetLeft();
11052 int top
= resultRect
.GetTop();
11053 int right
= resultRect
.GetRight();
11054 int bottom
= resultRect
.GetBottom();
11056 int leftCol
= topLeft
.GetCol();
11057 int topRow
= topLeft
.GetRow();
11058 int rightCol
= bottomRight
.GetCol();
11059 int bottomRow
= bottomRight
.GetRow();
11068 leftCol
= rightCol
;
11079 topRow
= bottomRow
;
11083 // The following loop is ONLY necessary to detect and handle merged cells.
11085 m_gridWin
->GetClientSize( &cw
, &ch
);
11087 // Get the origin coordinates: notice that they will be negative if the
11088 // grid is scrolled downwards/to the right.
11089 int gridOriginX
= 0;
11090 int gridOriginY
= 0;
11091 CalcScrolledPosition(gridOriginX
, gridOriginY
, &gridOriginX
, &gridOriginY
);
11093 int onScreenLeftmostCol
= internalXToCol(-gridOriginX
);
11094 int onScreenUppermostRow
= internalYToRow(-gridOriginY
);
11096 int onScreenRightmostCol
= internalXToCol(-gridOriginX
+ cw
);
11097 int onScreenBottommostRow
= internalYToRow(-gridOriginY
+ ch
);
11099 // Bound our loop so that we only examine the portion of the selected block
11100 // that is shown on screen. Therefore, we compare the Top-Left block values
11101 // to the Top-Left screen values, and the Bottom-Right block values to the
11102 // Bottom-Right screen values, choosing appropriately.
11103 const int visibleTopRow
= wxMax(topRow
, onScreenUppermostRow
);
11104 const int visibleBottomRow
= wxMin(bottomRow
, onScreenBottommostRow
);
11105 const int visibleLeftCol
= wxMax(leftCol
, onScreenLeftmostCol
);
11106 const int visibleRightCol
= wxMin(rightCol
, onScreenRightmostCol
);
11108 for ( int j
= visibleTopRow
; j
<= visibleBottomRow
; j
++ )
11110 for ( int i
= visibleLeftCol
; i
<= visibleRightCol
; i
++ )
11112 if ( (j
== visibleTopRow
) || (j
== visibleBottomRow
) ||
11113 (i
== visibleLeftCol
) || (i
== visibleRightCol
) )
11115 tempCellRect
= CellToRect( j
, i
);
11117 if (tempCellRect
.x
< left
)
11118 left
= tempCellRect
.x
;
11119 if (tempCellRect
.y
< top
)
11120 top
= tempCellRect
.y
;
11121 if (tempCellRect
.x
+ tempCellRect
.width
> right
)
11122 right
= tempCellRect
.x
+ tempCellRect
.width
;
11123 if (tempCellRect
.y
+ tempCellRect
.height
> bottom
)
11124 bottom
= tempCellRect
.y
+ tempCellRect
.height
;
11128 i
= visibleRightCol
; // jump over inner cells.
11133 // Convert to scrolled coords
11134 CalcScrolledPosition( left
, top
, &left
, &top
);
11135 CalcScrolledPosition( right
, bottom
, &right
, &bottom
);
11137 if (right
< 0 || bottom
< 0 || left
> cw
|| top
> ch
)
11138 return wxRect(0,0,0,0);
11140 resultRect
.SetLeft( wxMax(0, left
) );
11141 resultRect
.SetTop( wxMax(0, top
) );
11142 resultRect
.SetRight( wxMin(cw
, right
) );
11143 resultRect
.SetBottom( wxMin(ch
, bottom
) );
11148 // ----------------------------------------------------------------------------
11150 // ----------------------------------------------------------------------------
11152 #if wxUSE_DRAG_AND_DROP
11154 // this allow setting drop target directly on wxGrid
11155 void wxGrid::SetDropTarget(wxDropTarget
*dropTarget
)
11157 GetGridWindow()->SetDropTarget(dropTarget
);
11160 #endif // wxUSE_DRAG_AND_DROP
11162 // ----------------------------------------------------------------------------
11163 // grid event classes
11164 // ----------------------------------------------------------------------------
11166 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent
, wxNotifyEvent
)
11168 wxGridEvent::wxGridEvent( int id
, wxEventType type
, wxObject
* obj
,
11169 int row
, int col
, int x
, int y
, bool sel
,
11170 bool control
, bool shift
, bool alt
, bool meta
)
11171 : wxNotifyEvent( type
, id
),
11172 wxKeyboardState(control
, shift
, alt
, meta
)
11174 Init(row
, col
, x
, y
, sel
);
11176 SetEventObject(obj
);
11179 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent
, wxNotifyEvent
)
11181 wxGridSizeEvent::wxGridSizeEvent( int id
, wxEventType type
, wxObject
* obj
,
11182 int rowOrCol
, int x
, int y
,
11183 bool control
, bool shift
, bool alt
, bool meta
)
11184 : wxNotifyEvent( type
, id
),
11185 wxKeyboardState(control
, shift
, alt
, meta
)
11187 Init(rowOrCol
, x
, y
);
11189 SetEventObject(obj
);
11193 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent
, wxNotifyEvent
)
11195 wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id
, wxEventType type
, wxObject
* obj
,
11196 const wxGridCellCoords
& topLeft
,
11197 const wxGridCellCoords
& bottomRight
,
11198 bool sel
, bool control
,
11199 bool shift
, bool alt
, bool meta
)
11200 : wxNotifyEvent( type
, id
),
11201 wxKeyboardState(control
, shift
, alt
, meta
)
11203 Init(topLeft
, bottomRight
, sel
);
11205 SetEventObject(obj
);
11209 IMPLEMENT_DYNAMIC_CLASS(wxGridEditorCreatedEvent
, wxCommandEvent
)
11211 wxGridEditorCreatedEvent::wxGridEditorCreatedEvent(int id
, wxEventType type
,
11212 wxObject
* obj
, int row
,
11213 int col
, wxControl
* ctrl
)
11214 : wxCommandEvent(type
, id
)
11216 SetEventObject(obj
);
11222 #endif // wxUSE_GRID