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
188 if ( m_grid
->CanDragColSize() )
189 flags
|= wxCOL_RESIZABLE
;
190 if ( m_grid
->CanDragColMove() )
191 flags
|= wxCOL_REORDERABLE
;
196 virtual bool IsSortKey() const
198 return m_grid
->IsSortingBy(m_col
);
201 virtual bool IsSortOrderAscending() const
203 return m_grid
->IsSortOrderAscending();
207 // these really should be const but are not because the column needs to be
208 // assignable to be used in a wxVector (in STL build, in non-STL build we
209 // avoid the need for this)
214 // header control retreiving column information from the grid
215 class wxGridHeaderCtrl
: public wxHeaderCtrl
218 wxGridHeaderCtrl(wxGrid
*owner
)
219 : wxHeaderCtrl(owner
,
223 owner
->CanDragColMove() ? wxHD_DRAGDROP
: 0)
228 virtual wxHeaderColumn
& GetColumn(unsigned int idx
)
230 return m_columns
[idx
];
234 wxGrid
*GetOwner() const { return static_cast<wxGrid
*>(GetParent()); }
236 // override the base class method to update our m_columns array
237 virtual void OnColumnCountChanging(unsigned int count
)
239 const unsigned countOld
= m_columns
.size();
240 if ( count
< countOld
)
242 // just discard the columns which don't exist any more (notice that
243 // we can't use resize() here as it would require the vector
244 // value_type, i.e. wxGridHeaderColumn to be default constructible,
246 m_columns
.erase(m_columns
.begin() + count
, m_columns
.end());
248 else // new columns added
250 // add columns for the new elements
251 for ( unsigned n
= countOld
; n
< count
; n
++ )
252 m_columns
.push_back(wxGridHeaderColumn(GetOwner(), n
));
256 // override to implement column auto sizing
257 virtual bool UpdateColumnWidthToFit(unsigned int idx
, int widthTitle
)
259 // TODO: currently grid doesn't support computing the column best width
260 // from its contents so we just use the best label width as is
261 GetOwner()->SetColSize(idx
, widthTitle
);
267 // event handlers forwarding wxHeaderCtrl events to wxGrid
268 void OnClick(wxHeaderCtrlEvent
& event
)
270 GetOwner()->DoColHeaderClick(event
.GetColumn());
273 void OnBeginResize(wxHeaderCtrlEvent
& event
)
275 GetOwner()->DoStartResizeCol(event
.GetColumn());
280 void OnResizing(wxHeaderCtrlEvent
& event
)
282 GetOwner()->DoUpdateResizeColWidth(event
.GetWidth());
285 void OnEndResize(wxHeaderCtrlEvent
& event
)
287 GetOwner()->DoEndDragResizeCol();
292 void OnBeginReorder(wxHeaderCtrlEvent
& event
)
294 GetOwner()->DoStartMoveCol(event
.GetColumn());
297 void OnEndReorder(wxHeaderCtrlEvent
& event
)
299 GetOwner()->DoEndMoveCol(event
.GetNewOrder());
302 wxVector
<wxGridHeaderColumn
> m_columns
;
304 DECLARE_EVENT_TABLE()
305 DECLARE_NO_COPY_CLASS(wxGridHeaderCtrl
)
308 BEGIN_EVENT_TABLE(wxGridHeaderCtrl
, wxHeaderCtrl
)
309 EVT_HEADER_CLICK(wxID_ANY
, wxGridHeaderCtrl::OnClick
)
311 EVT_HEADER_BEGIN_RESIZE(wxID_ANY
, wxGridHeaderCtrl::OnBeginResize
)
312 EVT_HEADER_RESIZING(wxID_ANY
, wxGridHeaderCtrl::OnResizing
)
313 EVT_HEADER_END_RESIZE(wxID_ANY
, wxGridHeaderCtrl::OnEndResize
)
315 EVT_HEADER_BEGIN_REORDER(wxID_ANY
, wxGridHeaderCtrl::OnBeginReorder
)
316 EVT_HEADER_END_REORDER(wxID_ANY
, wxGridHeaderCtrl::OnEndReorder
)
319 // common base class for various grid subwindows
320 class WXDLLIMPEXP_ADV wxGridSubwindow
: public wxWindow
323 wxGridSubwindow(wxGrid
*owner
,
324 int additionalStyle
= 0,
325 const wxString
& name
= wxPanelNameStr
)
326 : wxWindow(owner
, wxID_ANY
,
327 wxDefaultPosition
, wxDefaultSize
,
328 wxBORDER_NONE
| additionalStyle
,
334 virtual bool AcceptsFocus() const { return false; }
336 wxGrid
*GetOwner() { return m_owner
; }
339 void OnMouseCaptureLost(wxMouseCaptureLostEvent
& event
);
343 DECLARE_EVENT_TABLE()
344 DECLARE_NO_COPY_CLASS(wxGridSubwindow
)
347 class WXDLLIMPEXP_ADV wxGridRowLabelWindow
: public wxGridSubwindow
350 wxGridRowLabelWindow(wxGrid
*parent
)
351 : wxGridSubwindow(parent
)
357 void OnPaint( wxPaintEvent
& event
);
358 void OnMouseEvent( wxMouseEvent
& event
);
359 void OnMouseWheel( wxMouseEvent
& event
);
361 DECLARE_EVENT_TABLE()
362 DECLARE_NO_COPY_CLASS(wxGridRowLabelWindow
)
366 class WXDLLIMPEXP_ADV wxGridColLabelWindow
: public wxGridSubwindow
369 wxGridColLabelWindow(wxGrid
*parent
)
370 : wxGridSubwindow(parent
)
376 void OnPaint( wxPaintEvent
& event
);
377 void OnMouseEvent( wxMouseEvent
& event
);
378 void OnMouseWheel( wxMouseEvent
& event
);
380 DECLARE_EVENT_TABLE()
381 DECLARE_NO_COPY_CLASS(wxGridColLabelWindow
)
385 class WXDLLIMPEXP_ADV wxGridCornerLabelWindow
: public wxGridSubwindow
388 wxGridCornerLabelWindow(wxGrid
*parent
)
389 : wxGridSubwindow(parent
)
394 void OnMouseEvent( wxMouseEvent
& event
);
395 void OnMouseWheel( wxMouseEvent
& event
);
396 void OnPaint( wxPaintEvent
& event
);
398 DECLARE_EVENT_TABLE()
399 DECLARE_NO_COPY_CLASS(wxGridCornerLabelWindow
)
402 class WXDLLIMPEXP_ADV wxGridWindow
: public wxGridSubwindow
405 wxGridWindow(wxGrid
*parent
)
406 : wxGridSubwindow(parent
,
407 wxWANTS_CHARS
| wxCLIP_CHILDREN
,
413 virtual void ScrollWindow( int dx
, int dy
, const wxRect
*rect
);
415 virtual bool AcceptsFocus() const { return true; }
418 void OnPaint( wxPaintEvent
&event
);
419 void OnMouseWheel( wxMouseEvent
& event
);
420 void OnMouseEvent( wxMouseEvent
& event
);
421 void OnKeyDown( wxKeyEvent
& );
422 void OnKeyUp( wxKeyEvent
& );
423 void OnChar( wxKeyEvent
& );
424 void OnEraseBackground( wxEraseEvent
& );
425 void OnFocus( wxFocusEvent
& );
427 DECLARE_EVENT_TABLE()
428 DECLARE_NO_COPY_CLASS(wxGridWindow
)
432 class wxGridCellEditorEvtHandler
: public wxEvtHandler
435 wxGridCellEditorEvtHandler(wxGrid
* grid
, wxGridCellEditor
* editor
)
442 void OnKillFocus(wxFocusEvent
& event
);
443 void OnKeyDown(wxKeyEvent
& event
);
444 void OnChar(wxKeyEvent
& event
);
446 void SetInSetFocus(bool inSetFocus
) { m_inSetFocus
= inSetFocus
; }
450 wxGridCellEditor
*m_editor
;
452 // Work around the fact that a focus kill event can be sent to
453 // a combobox within a set focus event.
456 DECLARE_EVENT_TABLE()
457 DECLARE_DYNAMIC_CLASS(wxGridCellEditorEvtHandler
)
458 DECLARE_NO_COPY_CLASS(wxGridCellEditorEvtHandler
)
462 IMPLEMENT_ABSTRACT_CLASS(wxGridCellEditorEvtHandler
, wxEvtHandler
)
464 BEGIN_EVENT_TABLE( wxGridCellEditorEvtHandler
, wxEvtHandler
)
465 EVT_KILL_FOCUS( wxGridCellEditorEvtHandler::OnKillFocus
)
466 EVT_KEY_DOWN( wxGridCellEditorEvtHandler::OnKeyDown
)
467 EVT_CHAR( wxGridCellEditorEvtHandler::OnChar
)
471 // ----------------------------------------------------------------------------
472 // the internal data representation used by wxGridCellAttrProvider
473 // ----------------------------------------------------------------------------
475 // this class stores attributes set for cells
476 class WXDLLIMPEXP_ADV wxGridCellAttrData
479 void SetAttr(wxGridCellAttr
*attr
, int row
, int col
);
480 wxGridCellAttr
*GetAttr(int row
, int col
) const;
481 void UpdateAttrRows( size_t pos
, int numRows
);
482 void UpdateAttrCols( size_t pos
, int numCols
);
485 // searches for the attr for given cell, returns wxNOT_FOUND if not found
486 int FindIndex(int row
, int col
) const;
488 wxGridCellWithAttrArray m_attrs
;
491 // this class stores attributes set for rows or columns
492 class WXDLLIMPEXP_ADV wxGridRowOrColAttrData
495 // empty ctor to suppress warnings
496 wxGridRowOrColAttrData() {}
497 ~wxGridRowOrColAttrData();
499 void SetAttr(wxGridCellAttr
*attr
, int rowOrCol
);
500 wxGridCellAttr
*GetAttr(int rowOrCol
) const;
501 void UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
);
504 wxArrayInt m_rowsOrCols
;
505 wxArrayAttrs m_attrs
;
508 // NB: this is just a wrapper around 3 objects: one which stores cell
509 // attributes, and 2 others for row/col ones
510 class WXDLLIMPEXP_ADV wxGridCellAttrProviderData
513 wxGridCellAttrData m_cellAttrs
;
514 wxGridRowOrColAttrData m_rowAttrs
,
519 // ----------------------------------------------------------------------------
520 // data structures used for the data type registry
521 // ----------------------------------------------------------------------------
523 struct wxGridDataTypeInfo
525 wxGridDataTypeInfo(const wxString
& typeName
,
526 wxGridCellRenderer
* renderer
,
527 wxGridCellEditor
* editor
)
528 : m_typeName(typeName
), m_renderer(renderer
), m_editor(editor
)
531 ~wxGridDataTypeInfo()
533 wxSafeDecRef(m_renderer
);
534 wxSafeDecRef(m_editor
);
538 wxGridCellRenderer
* m_renderer
;
539 wxGridCellEditor
* m_editor
;
541 DECLARE_NO_COPY_CLASS(wxGridDataTypeInfo
)
545 WX_DEFINE_ARRAY_WITH_DECL_PTR(wxGridDataTypeInfo
*, wxGridDataTypeInfoArray
,
546 class WXDLLIMPEXP_ADV
);
549 class WXDLLIMPEXP_ADV wxGridTypeRegistry
552 wxGridTypeRegistry() {}
553 ~wxGridTypeRegistry();
555 void RegisterDataType(const wxString
& typeName
,
556 wxGridCellRenderer
* renderer
,
557 wxGridCellEditor
* editor
);
559 // find one of already registered data types
560 int FindRegisteredDataType(const wxString
& typeName
);
562 // try to FindRegisteredDataType(), if this fails and typeName is one of
563 // standard typenames, register it and return its index
564 int FindDataType(const wxString
& typeName
);
566 // try to FindDataType(), if it fails see if it is not one of already
567 // registered data types with some params in which case clone the
568 // registered data type and set params for it
569 int FindOrCloneDataType(const wxString
& typeName
);
571 wxGridCellRenderer
* GetRenderer(int index
);
572 wxGridCellEditor
* GetEditor(int index
);
575 wxGridDataTypeInfoArray m_typeinfo
;
578 // ----------------------------------------------------------------------------
579 // operations classes abstracting the difference between operating on rows and
581 // ----------------------------------------------------------------------------
583 // This class allows to write a function only once because by using its methods
584 // it will apply to both columns and rows.
586 // This is an abstract interface definition, the two concrete implementations
587 // below should be used when working with rows and columns respectively.
588 class wxGridOperations
591 // Returns the operations in the other direction, i.e. wxGridRowOperations
592 // if this object is a wxGridColumnOperations and vice versa.
593 virtual wxGridOperations
& Dual() const = 0;
595 // Return the number of rows or columns.
596 virtual int GetNumberOfLines(const wxGrid
*grid
) const = 0;
598 // Return the selection mode which allows selecting rows or columns.
599 virtual wxGrid::wxGridSelectionModes
GetSelectionMode() const = 0;
601 // Make a wxGridCellCoords from the given components: thisDir is row or
602 // column and otherDir is column or row
603 virtual wxGridCellCoords
MakeCoords(int thisDir
, int otherDir
) const = 0;
605 // Calculate the scrolled position of the given abscissa or ordinate.
606 virtual int CalcScrolledPosition(wxGrid
*grid
, int pos
) const = 0;
608 // Selects the horizontal or vertical component from the given object.
609 virtual int Select(const wxGridCellCoords
& coords
) const = 0;
610 virtual int Select(const wxPoint
& pt
) const = 0;
611 virtual int Select(const wxSize
& sz
) const = 0;
612 virtual int Select(const wxRect
& r
) const = 0;
613 virtual int& Select(wxRect
& r
) const = 0;
615 // Returns width or height of the rectangle
616 virtual int& SelectSize(wxRect
& r
) const = 0;
618 // Make a wxSize such that Select() applied to it returns first component
619 virtual wxSize
MakeSize(int first
, int second
) const = 0;
621 // Sets the row or column component of the given cell coordinates
622 virtual void Set(wxGridCellCoords
& coords
, int line
) const = 0;
625 // Draws a line parallel to the row or column, i.e. horizontal or vertical:
626 // pos is the horizontal or vertical position of the line and start and end
627 // are the coordinates of the line extremities in the other direction
629 DrawParallelLine(wxDC
& dc
, int start
, int end
, int pos
) const = 0;
631 // Draw a horizontal or vertical line across the given rectangle
632 // (this is implemented in terms of above and uses Select() to extract
633 // start and end from the given rectangle)
634 void DrawParallelLineInRect(wxDC
& dc
, const wxRect
& rect
, int pos
) const
636 const int posStart
= Select(rect
.GetPosition());
637 DrawParallelLine(dc
, posStart
, posStart
+ Select(rect
.GetSize()), pos
);
641 // Return the index of the row or column at the given pixel coordinate.
643 PosToLine(const wxGrid
*grid
, int pos
, bool clip
= false) const = 0;
645 // Get the top/left position, in pixels, of the given row or column
646 virtual int GetLineStartPos(const wxGrid
*grid
, int line
) const = 0;
648 // Get the bottom/right position, in pixels, of the given row or column
649 virtual int GetLineEndPos(const wxGrid
*grid
, int line
) const = 0;
651 // Get the height/width of the given row/column
652 virtual int GetLineSize(const wxGrid
*grid
, int line
) const = 0;
654 // Get wxGrid::m_rowBottoms/m_colRights array
655 virtual const wxArrayInt
& GetLineEnds(const wxGrid
*grid
) const = 0;
657 // Get default height row height or column width
658 virtual int GetDefaultLineSize(const wxGrid
*grid
) const = 0;
660 // Return the minimal acceptable row height or column width
661 virtual int GetMinimalAcceptableLineSize(const wxGrid
*grid
) const = 0;
663 // Return the minimal row height or column width
664 virtual int GetMinimalLineSize(const wxGrid
*grid
, int line
) const = 0;
666 // Set the row height or column width
667 virtual void SetLineSize(wxGrid
*grid
, int line
, int size
) const = 0;
669 // True if rows/columns can be resized by user
670 virtual bool CanResizeLines(const wxGrid
*grid
) const = 0;
673 // Return the index of the line at the given position
675 // NB: currently this is always identity for the rows as reordering is only
676 // implemented for the lines
677 virtual int GetLineAt(const wxGrid
*grid
, int line
) const = 0;
680 // Get the row or column label window
681 virtual wxWindow
*GetHeaderWindow(wxGrid
*grid
) const = 0;
683 // Get the width or height of the row or column label window
684 virtual int GetHeaderWindowSize(wxGrid
*grid
) const = 0;
687 // This class is never used polymorphically but give it a virtual dtor
688 // anyhow to suppress g++ complaints about it
689 virtual ~wxGridOperations() { }
692 class wxGridRowOperations
: public wxGridOperations
695 virtual wxGridOperations
& Dual() const;
697 virtual int GetNumberOfLines(const wxGrid
*grid
) const
698 { return grid
->GetNumberRows(); }
700 virtual wxGrid::wxGridSelectionModes
GetSelectionMode() const
701 { return wxGrid::wxGridSelectRows
; }
703 virtual wxGridCellCoords
MakeCoords(int thisDir
, int otherDir
) const
704 { return wxGridCellCoords(thisDir
, otherDir
); }
706 virtual int CalcScrolledPosition(wxGrid
*grid
, int pos
) const
707 { return grid
->CalcScrolledPosition(wxPoint(pos
, 0)).x
; }
709 virtual int Select(const wxGridCellCoords
& c
) const { return c
.GetRow(); }
710 virtual int Select(const wxPoint
& pt
) const { return pt
.x
; }
711 virtual int Select(const wxSize
& sz
) const { return sz
.x
; }
712 virtual int Select(const wxRect
& r
) const { return r
.x
; }
713 virtual int& Select(wxRect
& r
) const { return r
.x
; }
714 virtual int& SelectSize(wxRect
& r
) const { return r
.width
; }
715 virtual wxSize
MakeSize(int first
, int second
) const
716 { return wxSize(first
, second
); }
717 virtual void Set(wxGridCellCoords
& coords
, int line
) const
718 { coords
.SetRow(line
); }
720 virtual void DrawParallelLine(wxDC
& dc
, int start
, int end
, int pos
) const
721 { dc
.DrawLine(start
, pos
, end
, pos
); }
723 virtual int PosToLine(const wxGrid
*grid
, int pos
, bool clip
= false) const
724 { return grid
->YToRow(pos
, clip
); }
725 virtual int GetLineStartPos(const wxGrid
*grid
, int line
) const
726 { return grid
->GetRowTop(line
); }
727 virtual int GetLineEndPos(const wxGrid
*grid
, int line
) const
728 { return grid
->GetRowBottom(line
); }
729 virtual int GetLineSize(const wxGrid
*grid
, int line
) const
730 { return grid
->GetRowHeight(line
); }
731 virtual const wxArrayInt
& GetLineEnds(const wxGrid
*grid
) const
732 { return grid
->m_rowBottoms
; }
733 virtual int GetDefaultLineSize(const wxGrid
*grid
) const
734 { return grid
->GetDefaultRowSize(); }
735 virtual int GetMinimalAcceptableLineSize(const wxGrid
*grid
) const
736 { return grid
->GetRowMinimalAcceptableHeight(); }
737 virtual int GetMinimalLineSize(const wxGrid
*grid
, int line
) const
738 { return grid
->GetRowMinimalHeight(line
); }
739 virtual void SetLineSize(wxGrid
*grid
, int line
, int size
) const
740 { grid
->SetRowSize(line
, size
); }
741 virtual bool CanResizeLines(const wxGrid
*grid
) const
742 { return grid
->CanDragRowSize(); }
744 virtual int GetLineAt(const wxGrid
* WXUNUSED(grid
), int line
) const
745 { return line
; } // TODO: implement row reordering
747 virtual wxWindow
*GetHeaderWindow(wxGrid
*grid
) const
748 { return grid
->GetGridRowLabelWindow(); }
749 virtual int GetHeaderWindowSize(wxGrid
*grid
) const
750 { return grid
->GetRowLabelSize(); }
753 class wxGridColumnOperations
: public wxGridOperations
756 virtual wxGridOperations
& Dual() const;
758 virtual int GetNumberOfLines(const wxGrid
*grid
) const
759 { return grid
->GetNumberCols(); }
761 virtual wxGrid::wxGridSelectionModes
GetSelectionMode() const
762 { return wxGrid::wxGridSelectColumns
; }
764 virtual wxGridCellCoords
MakeCoords(int thisDir
, int otherDir
) const
765 { return wxGridCellCoords(otherDir
, thisDir
); }
767 virtual int CalcScrolledPosition(wxGrid
*grid
, int pos
) const
768 { return grid
->CalcScrolledPosition(wxPoint(0, pos
)).y
; }
770 virtual int Select(const wxGridCellCoords
& c
) const { return c
.GetCol(); }
771 virtual int Select(const wxPoint
& pt
) const { return pt
.y
; }
772 virtual int Select(const wxSize
& sz
) const { return sz
.y
; }
773 virtual int Select(const wxRect
& r
) const { return r
.y
; }
774 virtual int& Select(wxRect
& r
) const { return r
.y
; }
775 virtual int& SelectSize(wxRect
& r
) const { return r
.height
; }
776 virtual wxSize
MakeSize(int first
, int second
) const
777 { return wxSize(second
, first
); }
778 virtual void Set(wxGridCellCoords
& coords
, int line
) const
779 { coords
.SetCol(line
); }
781 virtual void DrawParallelLine(wxDC
& dc
, int start
, int end
, int pos
) const
782 { dc
.DrawLine(pos
, start
, pos
, end
); }
784 virtual int PosToLine(const wxGrid
*grid
, int pos
, bool clip
= false) const
785 { return grid
->XToCol(pos
, clip
); }
786 virtual int GetLineStartPos(const wxGrid
*grid
, int line
) const
787 { return grid
->GetColLeft(line
); }
788 virtual int GetLineEndPos(const wxGrid
*grid
, int line
) const
789 { return grid
->GetColRight(line
); }
790 virtual int GetLineSize(const wxGrid
*grid
, int line
) const
791 { return grid
->GetColWidth(line
); }
792 virtual const wxArrayInt
& GetLineEnds(const wxGrid
*grid
) const
793 { return grid
->m_colRights
; }
794 virtual int GetDefaultLineSize(const wxGrid
*grid
) const
795 { return grid
->GetDefaultColSize(); }
796 virtual int GetMinimalAcceptableLineSize(const wxGrid
*grid
) const
797 { return grid
->GetColMinimalAcceptableWidth(); }
798 virtual int GetMinimalLineSize(const wxGrid
*grid
, int line
) const
799 { return grid
->GetColMinimalWidth(line
); }
800 virtual void SetLineSize(wxGrid
*grid
, int line
, int size
) const
801 { grid
->SetColSize(line
, size
); }
802 virtual bool CanResizeLines(const wxGrid
*grid
) const
803 { return grid
->CanDragColSize(); }
805 virtual int GetLineAt(const wxGrid
*grid
, int line
) const
806 { return grid
->GetColAt(line
); }
808 virtual wxWindow
*GetHeaderWindow(wxGrid
*grid
) const
809 { return grid
->GetGridColLabelWindow(); }
810 virtual int GetHeaderWindowSize(wxGrid
*grid
) const
811 { return grid
->GetColLabelSize(); }
814 wxGridOperations
& wxGridRowOperations::Dual() const
816 static wxGridColumnOperations s_colOper
;
821 wxGridOperations
& wxGridColumnOperations::Dual() const
823 static wxGridRowOperations s_rowOper
;
828 // This class abstracts the difference between operations going forward
829 // (down/right) and backward (up/left) and allows to use the same code for
830 // functions which differ only in the direction of grid traversal
832 // Like wxGridOperations it's an ABC with two concrete subclasses below. Unlike
833 // it, this is a normal object and not just a function dispatch table and has a
836 // Note: the explanation of this discrepancy is the existence of (very useful)
837 // Dual() method in wxGridOperations which forces us to make wxGridOperations a
838 // function dispatcher only.
839 class wxGridDirectionOperations
842 // The oper parameter to ctor selects whether we work with rows or columns
843 wxGridDirectionOperations(wxGrid
*grid
, const wxGridOperations
& oper
)
849 // Check if the component of this point in our direction is at the
850 // boundary, i.e. is the first/last row/column
851 virtual bool IsAtBoundary(const wxGridCellCoords
& coords
) const = 0;
853 // Increment the component of this point in our direction
854 virtual void Advance(wxGridCellCoords
& coords
) const = 0;
856 // Find the line at the given distance, in pixels, away from this one
857 // (this uses clipping, i.e. anything after the last line is counted as the
858 // last one and anything before the first one as 0)
859 virtual int MoveByPixelDistance(int line
, int distance
) const = 0;
861 // This class is never used polymorphically but give it a virtual dtor
862 // anyhow to suppress g++ complaints about it
863 virtual ~wxGridDirectionOperations() { }
866 wxGrid
* const m_grid
;
867 const wxGridOperations
& m_oper
;
870 class wxGridBackwardOperations
: public wxGridDirectionOperations
873 wxGridBackwardOperations(wxGrid
*grid
, const wxGridOperations
& oper
)
874 : wxGridDirectionOperations(grid
, oper
)
878 virtual bool IsAtBoundary(const wxGridCellCoords
& coords
) const
880 wxASSERT_MSG( m_oper
.Select(coords
) >= 0, "invalid row/column" );
882 return m_oper
.Select(coords
) == 0;
885 virtual void Advance(wxGridCellCoords
& coords
) const
887 wxASSERT( !IsAtBoundary(coords
) );
889 m_oper
.Set(coords
, m_oper
.Select(coords
) - 1);
892 virtual int MoveByPixelDistance(int line
, int distance
) const
894 int pos
= m_oper
.GetLineStartPos(m_grid
, line
);
895 return m_oper
.PosToLine(m_grid
, pos
- distance
+ 1, true);
899 class wxGridForwardOperations
: public wxGridDirectionOperations
902 wxGridForwardOperations(wxGrid
*grid
, const wxGridOperations
& oper
)
903 : wxGridDirectionOperations(grid
, oper
),
904 m_numLines(oper
.GetNumberOfLines(grid
))
908 virtual bool IsAtBoundary(const wxGridCellCoords
& coords
) const
910 wxASSERT_MSG( m_oper
.Select(coords
) < m_numLines
, "invalid row/column" );
912 return m_oper
.Select(coords
) == m_numLines
- 1;
915 virtual void Advance(wxGridCellCoords
& coords
) const
917 wxASSERT( !IsAtBoundary(coords
) );
919 m_oper
.Set(coords
, m_oper
.Select(coords
) + 1);
922 virtual int MoveByPixelDistance(int line
, int distance
) const
924 int pos
= m_oper
.GetLineStartPos(m_grid
, line
);
925 return m_oper
.PosToLine(m_grid
, pos
+ distance
, true);
929 const int m_numLines
;
932 // ----------------------------------------------------------------------------
934 // ----------------------------------------------------------------------------
936 //#define DEBUG_ATTR_CACHE
937 #ifdef DEBUG_ATTR_CACHE
938 static size_t gs_nAttrCacheHits
= 0;
939 static size_t gs_nAttrCacheMisses
= 0;
942 // ----------------------------------------------------------------------------
944 // ----------------------------------------------------------------------------
946 wxGridCellCoords
wxGridNoCellCoords( -1, -1 );
947 wxRect
wxGridNoCellRect( -1, -1, -1, -1 );
953 const size_t GRID_SCROLL_LINE_X
= 15;
954 const size_t GRID_SCROLL_LINE_Y
= GRID_SCROLL_LINE_X
;
956 // the size of hash tables used a bit everywhere (the max number of elements
957 // in these hash tables is the number of rows/columns)
958 const int GRID_HASH_SIZE
= 100;
960 // the minimal distance in pixels the mouse needs to move to start a drag
962 const int DRAG_SENSITIVITY
= 3;
964 } // anonymous namespace
966 // ----------------------------------------------------------------------------
968 // ----------------------------------------------------------------------------
973 // ensure that first is less or equal to second, swapping the values if
975 void EnsureFirstLessThanSecond(int& first
, int& second
)
977 if ( first
> second
)
978 wxSwap(first
, second
);
981 } // anonymous namespace
983 // ============================================================================
985 // ============================================================================
987 // ----------------------------------------------------------------------------
989 // ----------------------------------------------------------------------------
991 wxGridCellEditor::wxGridCellEditor()
997 wxGridCellEditor::~wxGridCellEditor()
1002 void wxGridCellEditor::Create(wxWindow
* WXUNUSED(parent
),
1003 wxWindowID
WXUNUSED(id
),
1004 wxEvtHandler
* evtHandler
)
1007 m_control
->PushEventHandler(evtHandler
);
1010 void wxGridCellEditor::PaintBackground(const wxRect
& rectCell
,
1011 wxGridCellAttr
*attr
)
1013 // erase the background because we might not fill the cell
1014 wxClientDC
dc(m_control
->GetParent());
1015 wxGridWindow
* gridWindow
= wxDynamicCast(m_control
->GetParent(), wxGridWindow
);
1017 gridWindow
->GetOwner()->PrepareDC(dc
);
1019 dc
.SetPen(*wxTRANSPARENT_PEN
);
1020 dc
.SetBrush(wxBrush(attr
->GetBackgroundColour()));
1021 dc
.DrawRectangle(rectCell
);
1023 // redraw the control we just painted over
1024 m_control
->Refresh();
1027 void wxGridCellEditor::Destroy()
1031 m_control
->PopEventHandler( true /* delete it*/ );
1033 m_control
->Destroy();
1038 void wxGridCellEditor::Show(bool show
, wxGridCellAttr
*attr
)
1040 wxASSERT_MSG(m_control
, wxT("The wxGridCellEditor must be created first!"));
1042 m_control
->Show(show
);
1046 // set the colours/fonts if we have any
1049 m_colFgOld
= m_control
->GetForegroundColour();
1050 m_control
->SetForegroundColour(attr
->GetTextColour());
1052 m_colBgOld
= m_control
->GetBackgroundColour();
1053 m_control
->SetBackgroundColour(attr
->GetBackgroundColour());
1055 // Workaround for GTK+1 font setting problem on some platforms
1056 #if !defined(__WXGTK__) || defined(__WXGTK20__)
1057 m_fontOld
= m_control
->GetFont();
1058 m_control
->SetFont(attr
->GetFont());
1061 // can't do anything more in the base class version, the other
1062 // attributes may only be used by the derived classes
1067 // restore the standard colours fonts
1068 if ( m_colFgOld
.Ok() )
1070 m_control
->SetForegroundColour(m_colFgOld
);
1071 m_colFgOld
= wxNullColour
;
1074 if ( m_colBgOld
.Ok() )
1076 m_control
->SetBackgroundColour(m_colBgOld
);
1077 m_colBgOld
= wxNullColour
;
1080 // Workaround for GTK+1 font setting problem on some platforms
1081 #if !defined(__WXGTK__) || defined(__WXGTK20__)
1082 if ( m_fontOld
.Ok() )
1084 m_control
->SetFont(m_fontOld
);
1085 m_fontOld
= wxNullFont
;
1091 void wxGridCellEditor::SetSize(const wxRect
& rect
)
1093 wxASSERT_MSG(m_control
, wxT("The wxGridCellEditor must be created first!"));
1095 m_control
->SetSize(rect
, wxSIZE_ALLOW_MINUS_ONE
);
1098 void wxGridCellEditor::HandleReturn(wxKeyEvent
& event
)
1103 bool wxGridCellEditor::IsAcceptedKey(wxKeyEvent
& event
)
1105 bool ctrl
= event
.ControlDown();
1106 bool alt
= event
.AltDown();
1109 // On the Mac the Alt key is more like shift and is used for entry of
1110 // valid characters, so check for Ctrl and Meta instead.
1111 alt
= event
.MetaDown();
1114 // Assume it's not a valid char if ctrl or alt is down, but if both are
1115 // down then it may be because of an AltGr key combination, so let them
1116 // through in that case.
1117 if ((ctrl
|| alt
) && !(ctrl
&& alt
))
1121 // if the unicode key code is not really a unicode character (it may
1122 // be a function key or etc., the platforms appear to always give us a
1123 // small value in this case) then fallback to the ASCII key code but
1124 // don't do anything for function keys or etc.
1125 if ( event
.GetUnicodeKey() > 127 && event
.GetKeyCode() > 127 )
1128 if ( event
.GetKeyCode() > 255 )
1135 void wxGridCellEditor::StartingKey(wxKeyEvent
& event
)
1140 void wxGridCellEditor::StartingClick()
1146 // ----------------------------------------------------------------------------
1147 // wxGridCellTextEditor
1148 // ----------------------------------------------------------------------------
1150 wxGridCellTextEditor::wxGridCellTextEditor()
1155 void wxGridCellTextEditor::Create(wxWindow
* parent
,
1157 wxEvtHandler
* evtHandler
)
1159 DoCreate(parent
, id
, evtHandler
);
1162 void wxGridCellTextEditor::DoCreate(wxWindow
* parent
,
1164 wxEvtHandler
* evtHandler
,
1167 style
|= wxTE_PROCESS_ENTER
| wxTE_PROCESS_TAB
| wxNO_BORDER
;
1169 m_control
= new wxTextCtrl(parent
, id
, wxEmptyString
,
1170 wxDefaultPosition
, wxDefaultSize
,
1173 // set max length allowed in the textctrl, if the parameter was set
1174 if ( m_maxChars
!= 0 )
1176 Text()->SetMaxLength(m_maxChars
);
1179 wxGridCellEditor::Create(parent
, id
, evtHandler
);
1182 void wxGridCellTextEditor::PaintBackground(const wxRect
& WXUNUSED(rectCell
),
1183 wxGridCellAttr
* WXUNUSED(attr
))
1185 // as we fill the entire client area,
1186 // don't do anything here to minimize flicker
1189 void wxGridCellTextEditor::SetSize(const wxRect
& rectOrig
)
1191 wxRect
rect(rectOrig
);
1193 // Make the edit control large enough to allow for internal margins
1195 // TODO: remove this if the text ctrl sizing is improved esp. for unix
1197 #if defined(__WXGTK__)
1205 #elif defined(__WXMSW__)
1219 int extra_x
= ( rect
.x
> 2 ) ? 2 : 1;
1220 int extra_y
= ( rect
.y
> 2 ) ? 2 : 1;
1222 #if defined(__WXMOTIF__)
1227 rect
.SetLeft( wxMax(0, rect
.x
- extra_x
) );
1228 rect
.SetTop( wxMax(0, rect
.y
- extra_y
) );
1229 rect
.SetRight( rect
.GetRight() + 2 * extra_x
);
1230 rect
.SetBottom( rect
.GetBottom() + 2 * extra_y
);
1233 wxGridCellEditor::SetSize(rect
);
1236 void wxGridCellTextEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1238 wxASSERT_MSG(m_control
, wxT("The wxGridCellEditor must be created first!"));
1240 m_startValue
= grid
->GetTable()->GetValue(row
, col
);
1242 DoBeginEdit(m_startValue
);
1245 void wxGridCellTextEditor::DoBeginEdit(const wxString
& startValue
)
1247 Text()->SetValue(startValue
);
1248 Text()->SetInsertionPointEnd();
1249 Text()->SetSelection(-1, -1);
1253 bool wxGridCellTextEditor::EndEdit(int row
, int col
, wxGrid
* grid
)
1255 wxASSERT_MSG(m_control
, wxT("The wxGridCellEditor must be created first!"));
1257 bool changed
= false;
1258 wxString value
= Text()->GetValue();
1259 if (value
!= m_startValue
)
1263 grid
->GetTable()->SetValue(row
, col
, value
);
1265 m_startValue
= wxEmptyString
;
1267 // No point in setting the text of the hidden control
1268 //Text()->SetValue(m_startValue);
1273 void wxGridCellTextEditor::Reset()
1275 wxASSERT_MSG(m_control
, wxT("The wxGridCellEditor must be created first!"));
1277 DoReset(m_startValue
);
1280 void wxGridCellTextEditor::DoReset(const wxString
& startValue
)
1282 Text()->SetValue(startValue
);
1283 Text()->SetInsertionPointEnd();
1286 bool wxGridCellTextEditor::IsAcceptedKey(wxKeyEvent
& event
)
1288 return wxGridCellEditor::IsAcceptedKey(event
);
1291 void wxGridCellTextEditor::StartingKey(wxKeyEvent
& event
)
1293 // Since this is now happening in the EVT_CHAR event EmulateKeyPress is no
1294 // longer an appropriate way to get the character into the text control.
1295 // Do it ourselves instead. We know that if we get this far that we have
1296 // a valid character, so not a whole lot of testing needs to be done.
1298 wxTextCtrl
* tc
= Text();
1303 ch
= event
.GetUnicodeKey();
1305 ch
= (wxChar
)event
.GetKeyCode();
1307 ch
= (wxChar
)event
.GetKeyCode();
1313 // delete the character at the cursor
1314 pos
= tc
->GetInsertionPoint();
1315 if (pos
< tc
->GetLastPosition())
1316 tc
->Remove(pos
, pos
+ 1);
1320 // delete the character before the cursor
1321 pos
= tc
->GetInsertionPoint();
1323 tc
->Remove(pos
- 1, pos
);
1332 void wxGridCellTextEditor::HandleReturn( wxKeyEvent
&
1333 WXUNUSED_GTK(WXUNUSED_MOTIF(event
)) )
1335 #if defined(__WXMOTIF__) || defined(__WXGTK__)
1336 // wxMotif needs a little extra help...
1337 size_t pos
= (size_t)( Text()->GetInsertionPoint() );
1338 wxString
s( Text()->GetValue() );
1339 s
= s
.Left(pos
) + wxT("\n") + s
.Mid(pos
);
1340 Text()->SetValue(s
);
1341 Text()->SetInsertionPoint( pos
);
1343 // the other ports can handle a Return key press
1349 void wxGridCellTextEditor::SetParameters(const wxString
& params
)
1359 if ( params
.ToLong(&tmp
) )
1361 m_maxChars
= (size_t)tmp
;
1365 wxLogDebug( _T("Invalid wxGridCellTextEditor parameter string '%s' ignored"), params
.c_str() );
1370 // return the value in the text control
1371 wxString
wxGridCellTextEditor::GetValue() const
1373 return Text()->GetValue();
1376 // ----------------------------------------------------------------------------
1377 // wxGridCellNumberEditor
1378 // ----------------------------------------------------------------------------
1380 wxGridCellNumberEditor::wxGridCellNumberEditor(int min
, int max
)
1386 void wxGridCellNumberEditor::Create(wxWindow
* parent
,
1388 wxEvtHandler
* evtHandler
)
1393 // create a spin ctrl
1394 m_control
= new wxSpinCtrl(parent
, wxID_ANY
, wxEmptyString
,
1395 wxDefaultPosition
, wxDefaultSize
,
1399 wxGridCellEditor::Create(parent
, id
, evtHandler
);
1404 // just a text control
1405 wxGridCellTextEditor::Create(parent
, id
, evtHandler
);
1407 #if wxUSE_VALIDATORS
1408 Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC
));
1413 void wxGridCellNumberEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1415 // first get the value
1416 wxGridTableBase
*table
= grid
->GetTable();
1417 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
1419 m_valueOld
= table
->GetValueAsLong(row
, col
);
1424 wxString sValue
= table
->GetValue(row
, col
);
1425 if (! sValue
.ToLong(&m_valueOld
) && ! sValue
.empty())
1427 wxFAIL_MSG( _T("this cell doesn't have numeric value") );
1435 Spin()->SetValue((int)m_valueOld
);
1441 DoBeginEdit(GetString());
1445 bool wxGridCellNumberEditor::EndEdit(int row
, int col
,
1454 value
= Spin()->GetValue();
1455 if ( value
== m_valueOld
)
1458 text
.Printf(wxT("%ld"), value
);
1460 else // using unconstrained input
1461 #endif // wxUSE_SPINCTRL
1463 const wxString
textOld(grid
->GetCellValue(row
, col
));
1464 text
= Text()->GetValue();
1467 if ( textOld
.empty() )
1470 else // non-empty text now (maybe 0)
1472 if ( !text
.ToLong(&value
) )
1475 // if value == m_valueOld == 0 but old text was "" and new one is
1476 // "0" something still did change
1477 if ( value
== m_valueOld
&& (value
|| !textOld
.empty()) )
1482 wxGridTableBase
* const table
= grid
->GetTable();
1483 if ( table
->CanSetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
1484 table
->SetValueAsLong(row
, col
, value
);
1486 table
->SetValue(row
, col
, text
);
1491 void wxGridCellNumberEditor::Reset()
1496 Spin()->SetValue((int)m_valueOld
);
1501 DoReset(GetString());
1505 bool wxGridCellNumberEditor::IsAcceptedKey(wxKeyEvent
& event
)
1507 if ( wxGridCellEditor::IsAcceptedKey(event
) )
1509 int keycode
= event
.GetKeyCode();
1510 if ( (keycode
< 128) &&
1511 (wxIsdigit(keycode
) || keycode
== '+' || keycode
== '-'))
1520 void wxGridCellNumberEditor::StartingKey(wxKeyEvent
& event
)
1522 int keycode
= event
.GetKeyCode();
1525 if ( wxIsdigit(keycode
) || keycode
== '+' || keycode
== '-')
1527 wxGridCellTextEditor::StartingKey(event
);
1529 // skip Skip() below
1536 if ( wxIsdigit(keycode
) )
1538 wxSpinCtrl
* spin
= (wxSpinCtrl
*)m_control
;
1539 spin
->SetValue(keycode
- '0');
1540 spin
->SetSelection(1,1);
1549 void wxGridCellNumberEditor::SetParameters(const wxString
& params
)
1560 if ( params
.BeforeFirst(_T(',')).ToLong(&tmp
) )
1564 if ( params
.AfterFirst(_T(',')).ToLong(&tmp
) )
1568 // skip the error message below
1573 wxLogDebug(_T("Invalid wxGridCellNumberEditor parameter string '%s' ignored"), params
.c_str());
1577 // return the value in the spin control if it is there (the text control otherwise)
1578 wxString
wxGridCellNumberEditor::GetValue() const
1585 long value
= Spin()->GetValue();
1586 s
.Printf(wxT("%ld"), value
);
1591 s
= Text()->GetValue();
1597 // ----------------------------------------------------------------------------
1598 // wxGridCellFloatEditor
1599 // ----------------------------------------------------------------------------
1601 wxGridCellFloatEditor::wxGridCellFloatEditor(int width
, int precision
)
1604 m_precision
= precision
;
1607 void wxGridCellFloatEditor::Create(wxWindow
* parent
,
1609 wxEvtHandler
* evtHandler
)
1611 wxGridCellTextEditor::Create(parent
, id
, evtHandler
);
1613 #if wxUSE_VALIDATORS
1614 Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC
));
1618 void wxGridCellFloatEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1620 // first get the value
1621 wxGridTableBase
* const table
= grid
->GetTable();
1622 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
1624 m_valueOld
= table
->GetValueAsDouble(row
, col
);
1630 const wxString value
= table
->GetValue(row
, col
);
1631 if ( !value
.empty() )
1633 if ( !value
.ToDouble(&m_valueOld
) )
1635 wxFAIL_MSG( _T("this cell doesn't have float value") );
1641 DoBeginEdit(GetString());
1644 bool wxGridCellFloatEditor::EndEdit(int row
, int col
, wxGrid
* grid
)
1646 const wxString
text(Text()->GetValue()),
1647 textOld(grid
->GetCellValue(row
, col
));
1650 if ( !text
.empty() )
1652 if ( !text
.ToDouble(&value
) )
1655 else // new value is empty string
1657 if ( textOld
.empty() )
1658 return false; // nothing changed
1663 // the test for empty strings ensures that we don't skip the value setting
1664 // when "" is replaced by "0" or vice versa as "" numeric value is also 0.
1665 if ( wxIsSameDouble(value
, m_valueOld
) && !text
.empty() && !textOld
.empty() )
1666 return false; // nothing changed
1668 wxGridTableBase
* const table
= grid
->GetTable();
1670 if ( table
->CanSetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
1671 table
->SetValueAsDouble(row
, col
, value
);
1673 table
->SetValue(row
, col
, text
);
1678 void wxGridCellFloatEditor::Reset()
1680 DoReset(GetString());
1683 void wxGridCellFloatEditor::StartingKey(wxKeyEvent
& event
)
1685 int keycode
= event
.GetKeyCode();
1687 tmpbuf
[0] = (char) keycode
;
1689 wxString
strbuf(tmpbuf
, *wxConvCurrent
);
1692 bool is_decimal_point
= ( strbuf
==
1693 wxLocale::GetInfo(wxLOCALE_DECIMAL_POINT
, wxLOCALE_CAT_NUMBER
) );
1695 bool is_decimal_point
= ( strbuf
== _T(".") );
1698 if ( wxIsdigit(keycode
) || keycode
== '+' || keycode
== '-'
1699 || is_decimal_point
)
1701 wxGridCellTextEditor::StartingKey(event
);
1703 // skip Skip() below
1710 void wxGridCellFloatEditor::SetParameters(const wxString
& params
)
1721 if ( params
.BeforeFirst(_T(',')).ToLong(&tmp
) )
1725 if ( params
.AfterFirst(_T(',')).ToLong(&tmp
) )
1727 m_precision
= (int)tmp
;
1729 // skip the error message below
1734 wxLogDebug(_T("Invalid wxGridCellFloatEditor parameter string '%s' ignored"), params
.c_str());
1738 wxString
wxGridCellFloatEditor::GetString() const
1741 if ( m_precision
== -1 && m_width
!= -1)
1743 // default precision
1744 fmt
.Printf(_T("%%%d.f"), m_width
);
1746 else if ( m_precision
!= -1 && m_width
== -1)
1749 fmt
.Printf(_T("%%.%df"), m_precision
);
1751 else if ( m_precision
!= -1 && m_width
!= -1 )
1753 fmt
.Printf(_T("%%%d.%df"), m_width
, m_precision
);
1757 // default width/precision
1761 return wxString::Format(fmt
, m_valueOld
);
1764 bool wxGridCellFloatEditor::IsAcceptedKey(wxKeyEvent
& event
)
1766 if ( wxGridCellEditor::IsAcceptedKey(event
) )
1768 const int keycode
= event
.GetKeyCode();
1769 if ( isascii(keycode
) )
1772 tmpbuf
[0] = (char) keycode
;
1774 wxString
strbuf(tmpbuf
, *wxConvCurrent
);
1777 const wxString decimalPoint
=
1778 wxLocale::GetInfo(wxLOCALE_DECIMAL_POINT
, wxLOCALE_CAT_NUMBER
);
1780 const wxString
decimalPoint(_T('.'));
1783 // accept digits, 'e' as in '1e+6', also '-', '+', and '.'
1784 if ( wxIsdigit(keycode
) ||
1785 tolower(keycode
) == 'e' ||
1786 keycode
== decimalPoint
||
1798 #endif // wxUSE_TEXTCTRL
1802 // ----------------------------------------------------------------------------
1803 // wxGridCellBoolEditor
1804 // ----------------------------------------------------------------------------
1806 // the default values for GetValue()
1807 wxString
wxGridCellBoolEditor::ms_stringValues
[2] = { _T(""), _T("1") };
1809 void wxGridCellBoolEditor::Create(wxWindow
* parent
,
1811 wxEvtHandler
* evtHandler
)
1813 m_control
= new wxCheckBox(parent
, id
, wxEmptyString
,
1814 wxDefaultPosition
, wxDefaultSize
,
1817 wxGridCellEditor::Create(parent
, id
, evtHandler
);
1820 void wxGridCellBoolEditor::SetSize(const wxRect
& r
)
1822 bool resize
= false;
1823 wxSize size
= m_control
->GetSize();
1824 wxCoord minSize
= wxMin(r
.width
, r
.height
);
1826 // check if the checkbox is not too big/small for this cell
1827 wxSize sizeBest
= m_control
->GetBestSize();
1828 if ( !(size
== sizeBest
) )
1830 // reset to default size if it had been made smaller
1836 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
1838 // leave 1 pixel margin
1839 size
.x
= size
.y
= minSize
- 2;
1846 m_control
->SetSize(size
);
1849 // position it in the centre of the rectangle (TODO: support alignment?)
1851 #if defined(__WXGTK__) || defined (__WXMOTIF__)
1852 // the checkbox without label still has some space to the right in wxGTK,
1853 // so shift it to the right
1855 #elif defined(__WXMSW__)
1856 // here too, but in other way
1861 int hAlign
= wxALIGN_CENTRE
;
1862 int vAlign
= wxALIGN_CENTRE
;
1864 GetCellAttr()->GetAlignment(& hAlign
, & vAlign
);
1867 if (hAlign
== wxALIGN_LEFT
)
1875 y
= r
.y
+ r
.height
/ 2 - size
.y
/ 2;
1877 else if (hAlign
== wxALIGN_RIGHT
)
1879 x
= r
.x
+ r
.width
- size
.x
- 2;
1880 y
= r
.y
+ r
.height
/ 2 - size
.y
/ 2;
1882 else if (hAlign
== wxALIGN_CENTRE
)
1884 x
= r
.x
+ r
.width
/ 2 - size
.x
/ 2;
1885 y
= r
.y
+ r
.height
/ 2 - size
.y
/ 2;
1888 m_control
->Move(x
, y
);
1891 void wxGridCellBoolEditor::Show(bool show
, wxGridCellAttr
*attr
)
1893 m_control
->Show(show
);
1897 wxColour colBg
= attr
? attr
->GetBackgroundColour() : *wxLIGHT_GREY
;
1898 CBox()->SetBackgroundColour(colBg
);
1902 void wxGridCellBoolEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1904 wxASSERT_MSG(m_control
,
1905 wxT("The wxGridCellEditor must be created first!"));
1907 if (grid
->GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
))
1909 m_startValue
= grid
->GetTable()->GetValueAsBool(row
, col
);
1913 wxString
cellval( grid
->GetTable()->GetValue(row
, col
) );
1915 if ( cellval
== ms_stringValues
[false] )
1916 m_startValue
= false;
1917 else if ( cellval
== ms_stringValues
[true] )
1918 m_startValue
= true;
1921 // do not try to be smart here and convert it to true or false
1922 // because we'll still overwrite it with something different and
1923 // this risks to be very surprising for the user code, let them
1925 wxFAIL_MSG( _T("invalid value for a cell with bool editor!") );
1929 CBox()->SetValue(m_startValue
);
1933 bool wxGridCellBoolEditor::EndEdit(int row
, int col
,
1936 wxASSERT_MSG(m_control
,
1937 wxT("The wxGridCellEditor must be created first!"));
1939 bool changed
= false;
1940 bool value
= CBox()->GetValue();
1941 if ( value
!= m_startValue
)
1946 wxGridTableBase
* const table
= grid
->GetTable();
1947 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
) )
1948 table
->SetValueAsBool(row
, col
, value
);
1950 table
->SetValue(row
, col
, GetValue());
1956 void wxGridCellBoolEditor::Reset()
1958 wxASSERT_MSG(m_control
,
1959 wxT("The wxGridCellEditor must be created first!"));
1961 CBox()->SetValue(m_startValue
);
1964 void wxGridCellBoolEditor::StartingClick()
1966 CBox()->SetValue(!CBox()->GetValue());
1969 bool wxGridCellBoolEditor::IsAcceptedKey(wxKeyEvent
& event
)
1971 if ( wxGridCellEditor::IsAcceptedKey(event
) )
1973 int keycode
= event
.GetKeyCode();
1986 void wxGridCellBoolEditor::StartingKey(wxKeyEvent
& event
)
1988 int keycode
= event
.GetKeyCode();
1992 CBox()->SetValue(!CBox()->GetValue());
1996 CBox()->SetValue(true);
2000 CBox()->SetValue(false);
2005 wxString
wxGridCellBoolEditor::GetValue() const
2007 return ms_stringValues
[CBox()->GetValue()];
2011 wxGridCellBoolEditor::UseStringValues(const wxString
& valueTrue
,
2012 const wxString
& valueFalse
)
2014 ms_stringValues
[false] = valueFalse
;
2015 ms_stringValues
[true] = valueTrue
;
2019 wxGridCellBoolEditor::IsTrueValue(const wxString
& value
)
2021 return value
== ms_stringValues
[true];
2024 #endif // wxUSE_CHECKBOX
2028 // ----------------------------------------------------------------------------
2029 // wxGridCellChoiceEditor
2030 // ----------------------------------------------------------------------------
2032 wxGridCellChoiceEditor::wxGridCellChoiceEditor(const wxArrayString
& choices
,
2034 : m_choices(choices
),
2035 m_allowOthers(allowOthers
) { }
2037 wxGridCellChoiceEditor::wxGridCellChoiceEditor(size_t count
,
2038 const wxString choices
[],
2040 : m_allowOthers(allowOthers
)
2044 m_choices
.Alloc(count
);
2045 for ( size_t n
= 0; n
< count
; n
++ )
2047 m_choices
.Add(choices
[n
]);
2052 wxGridCellEditor
*wxGridCellChoiceEditor::Clone() const
2054 wxGridCellChoiceEditor
*editor
= new wxGridCellChoiceEditor
;
2055 editor
->m_allowOthers
= m_allowOthers
;
2056 editor
->m_choices
= m_choices
;
2061 void wxGridCellChoiceEditor::Create(wxWindow
* parent
,
2063 wxEvtHandler
* evtHandler
)
2065 int style
= wxTE_PROCESS_ENTER
|
2069 if ( !m_allowOthers
)
2070 style
|= wxCB_READONLY
;
2071 m_control
= new wxComboBox(parent
, id
, wxEmptyString
,
2072 wxDefaultPosition
, wxDefaultSize
,
2076 wxGridCellEditor::Create(parent
, id
, evtHandler
);
2079 void wxGridCellChoiceEditor::PaintBackground(const wxRect
& rectCell
,
2080 wxGridCellAttr
* attr
)
2082 // as we fill the entire client area, don't do anything here to minimize
2085 // TODO: It doesn't actually fill the client area since the height of a
2086 // combo always defaults to the standard. Until someone has time to
2087 // figure out the right rectangle to paint, just do it the normal way.
2088 wxGridCellEditor::PaintBackground(rectCell
, attr
);
2091 void wxGridCellChoiceEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
2093 wxASSERT_MSG(m_control
,
2094 wxT("The wxGridCellEditor must be created first!"));
2096 wxGridCellEditorEvtHandler
* evtHandler
= NULL
;
2098 evtHandler
= wxDynamicCast(m_control
->GetEventHandler(), wxGridCellEditorEvtHandler
);
2100 // Don't immediately end if we get a kill focus event within BeginEdit
2102 evtHandler
->SetInSetFocus(true);
2104 m_startValue
= grid
->GetTable()->GetValue(row
, col
);
2106 Reset(); // this updates combo box to correspond to m_startValue
2108 Combo()->SetFocus();
2112 // When dropping down the menu, a kill focus event
2113 // happens after this point, so we can't reset the flag yet.
2114 #if !defined(__WXGTK20__)
2115 evtHandler
->SetInSetFocus(false);
2120 bool wxGridCellChoiceEditor::EndEdit(int row
, int col
,
2123 wxString value
= Combo()->GetValue();
2124 if ( value
== m_startValue
)
2127 grid
->GetTable()->SetValue(row
, col
, value
);
2132 void wxGridCellChoiceEditor::Reset()
2136 Combo()->SetValue(m_startValue
);
2137 Combo()->SetInsertionPointEnd();
2139 else // the combobox is read-only
2141 // find the right position, or default to the first if not found
2142 int pos
= Combo()->FindString(m_startValue
);
2143 if (pos
== wxNOT_FOUND
)
2145 Combo()->SetSelection(pos
);
2149 void wxGridCellChoiceEditor::SetParameters(const wxString
& params
)
2159 wxStringTokenizer
tk(params
, _T(','));
2160 while ( tk
.HasMoreTokens() )
2162 m_choices
.Add(tk
.GetNextToken());
2166 // return the value in the text control
2167 wxString
wxGridCellChoiceEditor::GetValue() const
2169 return Combo()->GetValue();
2172 #endif // wxUSE_COMBOBOX
2174 // ----------------------------------------------------------------------------
2175 // wxGridCellEditorEvtHandler
2176 // ----------------------------------------------------------------------------
2178 void wxGridCellEditorEvtHandler::OnKillFocus(wxFocusEvent
& event
)
2180 // Don't disable the cell if we're just starting to edit it
2185 m_grid
->DisableCellEditControl();
2190 void wxGridCellEditorEvtHandler::OnKeyDown(wxKeyEvent
& event
)
2192 switch ( event
.GetKeyCode() )
2196 m_grid
->DisableCellEditControl();
2200 m_grid
->GetEventHandler()->ProcessEvent( event
);
2204 case WXK_NUMPAD_ENTER
:
2205 if (!m_grid
->GetEventHandler()->ProcessEvent(event
))
2206 m_editor
->HandleReturn(event
);
2215 void wxGridCellEditorEvtHandler::OnChar(wxKeyEvent
& event
)
2217 int row
= m_grid
->GetGridCursorRow();
2218 int col
= m_grid
->GetGridCursorCol();
2219 wxRect rect
= m_grid
->CellToRect( row
, col
);
2221 m_grid
->GetGridWindow()->GetClientSize( &cw
, &ch
);
2223 // if cell width is smaller than grid client area, cell is wholly visible
2224 bool wholeCellVisible
= (rect
.GetWidth() < cw
);
2226 switch ( event
.GetKeyCode() )
2231 case WXK_NUMPAD_ENTER
:
2236 if ( wholeCellVisible
)
2238 // no special processing needed...
2243 // do special processing for partly visible cell...
2245 // get the widths of all cells previous to this one
2247 for ( int i
= 0; i
< col
; i
++ )
2249 colXPos
+= m_grid
->GetColSize(i
);
2252 int xUnit
= 1, yUnit
= 1;
2253 m_grid
->GetScrollPixelsPerUnit(&xUnit
, &yUnit
);
2256 m_grid
->Scroll(colXPos
/ xUnit
- 1, m_grid
->GetScrollPos(wxVERTICAL
));
2260 m_grid
->Scroll(colXPos
/ xUnit
, m_grid
->GetScrollPos(wxVERTICAL
));
2268 if ( wholeCellVisible
)
2270 // no special processing needed...
2275 // do special processing for partly visible cell...
2278 wxString value
= m_grid
->GetCellValue(row
, col
);
2279 if ( wxEmptyString
!= value
)
2281 // get width of cell CONTENTS (text)
2283 wxFont font
= m_grid
->GetCellFont(row
, col
);
2284 m_grid
->GetTextExtent(value
, &textWidth
, &y
, NULL
, NULL
, &font
);
2286 // try to RIGHT align the text by scrolling
2287 int client_right
= m_grid
->GetGridWindow()->GetClientSize().GetWidth();
2289 // (m_grid->GetScrollLineX()*2) is a factor for not scrolling to far,
2290 // otherwise the last part of the cell content might be hidden below the scroll bar
2291 // FIXME: maybe there is a more suitable correction?
2292 textWidth
-= (client_right
- (m_grid
->GetScrollLineX() * 2));
2293 if ( textWidth
< 0 )
2299 // get the widths of all cells previous to this one
2301 for ( int i
= 0; i
< col
; i
++ )
2303 colXPos
+= m_grid
->GetColSize(i
);
2306 // and add the (modified) text width of the cell contents
2307 // as we'd like to see the last part of the cell contents
2308 colXPos
+= textWidth
;
2310 int xUnit
= 1, yUnit
= 1;
2311 m_grid
->GetScrollPixelsPerUnit(&xUnit
, &yUnit
);
2312 m_grid
->Scroll(colXPos
/ xUnit
- 1, m_grid
->GetScrollPos(wxVERTICAL
));
2323 // ----------------------------------------------------------------------------
2324 // wxGridCellWorker is an (almost) empty common base class for
2325 // wxGridCellRenderer and wxGridCellEditor managing ref counting
2326 // ----------------------------------------------------------------------------
2328 void wxGridCellWorker::SetParameters(const wxString
& WXUNUSED(params
))
2333 wxGridCellWorker::~wxGridCellWorker()
2337 // ============================================================================
2339 // ============================================================================
2341 // ----------------------------------------------------------------------------
2342 // wxGridCellRenderer
2343 // ----------------------------------------------------------------------------
2345 void wxGridCellRenderer::Draw(wxGrid
& grid
,
2346 wxGridCellAttr
& attr
,
2349 int WXUNUSED(row
), int WXUNUSED(col
),
2352 dc
.SetBackgroundMode( wxBRUSHSTYLE_SOLID
);
2355 if ( grid
.IsEnabled() )
2359 if ( grid
.HasFocus() )
2360 clr
= grid
.GetSelectionBackground();
2362 clr
= wxSystemSettings::GetColour(wxSYS_COLOUR_BTNSHADOW
);
2366 clr
= attr
.GetBackgroundColour();
2369 else // grey out fields if the grid is disabled
2371 clr
= wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
);
2375 dc
.SetPen( *wxTRANSPARENT_PEN
);
2376 dc
.DrawRectangle(rect
);
2379 // ----------------------------------------------------------------------------
2380 // wxGridCellStringRenderer
2381 // ----------------------------------------------------------------------------
2383 void wxGridCellStringRenderer::SetTextColoursAndFont(const wxGrid
& grid
,
2384 const wxGridCellAttr
& attr
,
2388 dc
.SetBackgroundMode( wxBRUSHSTYLE_TRANSPARENT
);
2390 // TODO some special colours for attr.IsReadOnly() case?
2392 // different coloured text when the grid is disabled
2393 if ( grid
.IsEnabled() )
2398 if ( grid
.HasFocus() )
2399 clr
= grid
.GetSelectionBackground();
2401 clr
= wxSystemSettings::GetColour(wxSYS_COLOUR_BTNSHADOW
);
2402 dc
.SetTextBackground( clr
);
2403 dc
.SetTextForeground( grid
.GetSelectionForeground() );
2407 dc
.SetTextBackground( attr
.GetBackgroundColour() );
2408 dc
.SetTextForeground( attr
.GetTextColour() );
2413 dc
.SetTextBackground(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
));
2414 dc
.SetTextForeground(wxSystemSettings::GetColour(wxSYS_COLOUR_GRAYTEXT
));
2417 dc
.SetFont( attr
.GetFont() );
2420 wxSize
wxGridCellStringRenderer::DoGetBestSize(const wxGridCellAttr
& attr
,
2422 const wxString
& text
)
2424 wxCoord x
= 0, y
= 0, max_x
= 0;
2425 dc
.SetFont(attr
.GetFont());
2426 wxStringTokenizer
tk(text
, _T('\n'));
2427 while ( tk
.HasMoreTokens() )
2429 dc
.GetTextExtent(tk
.GetNextToken(), &x
, &y
);
2430 max_x
= wxMax(max_x
, x
);
2433 y
*= 1 + text
.Freq(wxT('\n')); // multiply by the number of lines.
2435 return wxSize(max_x
, y
);
2438 wxSize
wxGridCellStringRenderer::GetBestSize(wxGrid
& grid
,
2439 wxGridCellAttr
& attr
,
2443 return DoGetBestSize(attr
, dc
, grid
.GetCellValue(row
, col
));
2446 void wxGridCellStringRenderer::Draw(wxGrid
& grid
,
2447 wxGridCellAttr
& attr
,
2449 const wxRect
& rectCell
,
2453 wxRect rect
= rectCell
;
2456 // erase only this cells background, overflow cells should have been erased
2457 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
2460 attr
.GetAlignment(&hAlign
, &vAlign
);
2462 int overflowCols
= 0;
2464 if (attr
.GetOverflow())
2466 int cols
= grid
.GetNumberCols();
2467 int best_width
= GetBestSize(grid
,attr
,dc
,row
,col
).GetWidth();
2468 int cell_rows
, cell_cols
;
2469 attr
.GetSize( &cell_rows
, &cell_cols
); // shouldn't get here if <= 0
2470 if ((best_width
> rectCell
.width
) && (col
< cols
) && grid
.GetTable())
2472 int i
, c_cols
, c_rows
;
2473 for (i
= col
+cell_cols
; i
< cols
; i
++)
2475 bool is_empty
= true;
2476 for (int j
=row
; j
< row
+ cell_rows
; j
++)
2478 // check w/ anchor cell for multicell block
2479 grid
.GetCellSize(j
, i
, &c_rows
, &c_cols
);
2482 if (!grid
.GetTable()->IsEmptyCell(j
+ c_rows
, i
))
2491 rect
.width
+= grid
.GetColSize(i
);
2499 if (rect
.width
>= best_width
)
2503 overflowCols
= i
- col
- cell_cols
+ 1;
2504 if (overflowCols
>= cols
)
2505 overflowCols
= cols
- 1;
2508 if (overflowCols
> 0) // redraw overflow cells w/ proper hilight
2510 hAlign
= wxALIGN_LEFT
; // if oveflowed then it's left aligned
2512 clip
.x
+= rectCell
.width
;
2513 // draw each overflow cell individually
2514 int col_end
= col
+ cell_cols
+ overflowCols
;
2515 if (col_end
>= grid
.GetNumberCols())
2516 col_end
= grid
.GetNumberCols() - 1;
2517 for (int i
= col
+ cell_cols
; i
<= col_end
; i
++)
2519 clip
.width
= grid
.GetColSize(i
) - 1;
2520 dc
.DestroyClippingRegion();
2521 dc
.SetClippingRegion(clip
);
2523 SetTextColoursAndFont(grid
, attr
, dc
,
2524 grid
.IsInSelection(row
,i
));
2526 grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
),
2527 rect
, hAlign
, vAlign
);
2528 clip
.x
+= grid
.GetColSize(i
) - 1;
2534 dc
.DestroyClippingRegion();
2538 // now we only have to draw the text
2539 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
2541 grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
),
2542 rect
, hAlign
, vAlign
);
2545 // ----------------------------------------------------------------------------
2546 // wxGridCellNumberRenderer
2547 // ----------------------------------------------------------------------------
2549 wxString
wxGridCellNumberRenderer::GetString(const wxGrid
& grid
, int row
, int col
)
2551 wxGridTableBase
*table
= grid
.GetTable();
2553 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
2555 text
.Printf(_T("%ld"), table
->GetValueAsLong(row
, col
));
2559 text
= table
->GetValue(row
, col
);
2565 void wxGridCellNumberRenderer::Draw(wxGrid
& grid
,
2566 wxGridCellAttr
& attr
,
2568 const wxRect
& rectCell
,
2572 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
2574 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
2576 // draw the text right aligned by default
2578 attr
.GetAlignment(&hAlign
, &vAlign
);
2579 hAlign
= wxALIGN_RIGHT
;
2581 wxRect rect
= rectCell
;
2584 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
2587 wxSize
wxGridCellNumberRenderer::GetBestSize(wxGrid
& grid
,
2588 wxGridCellAttr
& attr
,
2592 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
2595 // ----------------------------------------------------------------------------
2596 // wxGridCellFloatRenderer
2597 // ----------------------------------------------------------------------------
2599 wxGridCellFloatRenderer::wxGridCellFloatRenderer(int width
, int precision
)
2602 SetPrecision(precision
);
2605 wxGridCellRenderer
*wxGridCellFloatRenderer::Clone() const
2607 wxGridCellFloatRenderer
*renderer
= new wxGridCellFloatRenderer
;
2608 renderer
->m_width
= m_width
;
2609 renderer
->m_precision
= m_precision
;
2610 renderer
->m_format
= m_format
;
2615 wxString
wxGridCellFloatRenderer::GetString(const wxGrid
& grid
, int row
, int col
)
2617 wxGridTableBase
*table
= grid
.GetTable();
2622 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
2624 val
= table
->GetValueAsDouble(row
, col
);
2629 text
= table
->GetValue(row
, col
);
2630 hasDouble
= text
.ToDouble(&val
);
2637 if ( m_width
== -1 )
2639 if ( m_precision
== -1 )
2641 // default width/precision
2642 m_format
= _T("%f");
2646 m_format
.Printf(_T("%%.%df"), m_precision
);
2649 else if ( m_precision
== -1 )
2651 // default precision
2652 m_format
.Printf(_T("%%%d.f"), m_width
);
2656 m_format
.Printf(_T("%%%d.%df"), m_width
, m_precision
);
2660 text
.Printf(m_format
, val
);
2663 //else: text already contains the string
2668 void wxGridCellFloatRenderer::Draw(wxGrid
& grid
,
2669 wxGridCellAttr
& attr
,
2671 const wxRect
& rectCell
,
2675 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
2677 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
2679 // draw the text right aligned by default
2681 attr
.GetAlignment(&hAlign
, &vAlign
);
2682 hAlign
= wxALIGN_RIGHT
;
2684 wxRect rect
= rectCell
;
2687 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
2690 wxSize
wxGridCellFloatRenderer::GetBestSize(wxGrid
& grid
,
2691 wxGridCellAttr
& attr
,
2695 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
2698 void wxGridCellFloatRenderer::SetParameters(const wxString
& params
)
2702 // reset to defaults
2708 wxString tmp
= params
.BeforeFirst(_T(','));
2712 if ( tmp
.ToLong(&width
) )
2714 SetWidth((int)width
);
2718 wxLogDebug(_T("Invalid wxGridCellFloatRenderer width parameter string '%s ignored"), params
.c_str());
2722 tmp
= params
.AfterFirst(_T(','));
2726 if ( tmp
.ToLong(&precision
) )
2728 SetPrecision((int)precision
);
2732 wxLogDebug(_T("Invalid wxGridCellFloatRenderer precision parameter string '%s ignored"), params
.c_str());
2738 // ----------------------------------------------------------------------------
2739 // wxGridCellBoolRenderer
2740 // ----------------------------------------------------------------------------
2742 wxSize
wxGridCellBoolRenderer::ms_sizeCheckMark
;
2744 // FIXME these checkbox size calculations are really ugly...
2746 // between checkmark and box
2747 static const wxCoord wxGRID_CHECKMARK_MARGIN
= 2;
2749 wxSize
wxGridCellBoolRenderer::GetBestSize(wxGrid
& grid
,
2750 wxGridCellAttr
& WXUNUSED(attr
),
2755 // compute it only once (no locks for MT safeness in GUI thread...)
2756 if ( !ms_sizeCheckMark
.x
)
2758 // get checkbox size
2759 wxCheckBox
*checkbox
= new wxCheckBox(&grid
, wxID_ANY
, wxEmptyString
);
2760 wxSize size
= checkbox
->GetBestSize();
2761 wxCoord checkSize
= size
.y
+ 2 * wxGRID_CHECKMARK_MARGIN
;
2763 #if defined(__WXMOTIF__)
2764 checkSize
-= size
.y
/ 2;
2769 ms_sizeCheckMark
.x
= ms_sizeCheckMark
.y
= checkSize
;
2772 return ms_sizeCheckMark
;
2775 void wxGridCellBoolRenderer::Draw(wxGrid
& grid
,
2776 wxGridCellAttr
& attr
,
2782 wxGridCellRenderer::Draw(grid
, attr
, dc
, rect
, row
, col
, isSelected
);
2784 // draw a check mark in the centre (ignoring alignment - TODO)
2785 wxSize size
= GetBestSize(grid
, attr
, dc
, row
, col
);
2787 // don't draw outside the cell
2788 wxCoord minSize
= wxMin(rect
.width
, rect
.height
);
2789 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
2791 // and even leave (at least) 1 pixel margin
2792 size
.x
= size
.y
= minSize
;
2795 // draw a border around checkmark
2797 attr
.GetAlignment(&hAlign
, &vAlign
);
2800 if (hAlign
== wxALIGN_CENTRE
)
2802 rectBorder
.x
= rect
.x
+ rect
.width
/ 2 - size
.x
/ 2;
2803 rectBorder
.y
= rect
.y
+ rect
.height
/ 2 - size
.y
/ 2;
2804 rectBorder
.width
= size
.x
;
2805 rectBorder
.height
= size
.y
;
2807 else if (hAlign
== wxALIGN_LEFT
)
2809 rectBorder
.x
= rect
.x
+ 2;
2810 rectBorder
.y
= rect
.y
+ rect
.height
/ 2 - size
.y
/ 2;
2811 rectBorder
.width
= size
.x
;
2812 rectBorder
.height
= size
.y
;
2814 else if (hAlign
== wxALIGN_RIGHT
)
2816 rectBorder
.x
= rect
.x
+ rect
.width
- size
.x
- 2;
2817 rectBorder
.y
= rect
.y
+ rect
.height
/ 2 - size
.y
/ 2;
2818 rectBorder
.width
= size
.x
;
2819 rectBorder
.height
= size
.y
;
2823 if ( grid
.GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
) )
2825 value
= grid
.GetTable()->GetValueAsBool(row
, col
);
2829 wxString
cellval( grid
.GetTable()->GetValue(row
, col
) );
2830 value
= wxGridCellBoolEditor::IsTrueValue(cellval
);
2835 flags
|= wxCONTROL_CHECKED
;
2837 wxRendererNative::Get().DrawCheckBox( &grid
, dc
, rectBorder
, flags
);
2840 // ----------------------------------------------------------------------------
2842 // ----------------------------------------------------------------------------
2844 void wxGridCellAttr::Init(wxGridCellAttr
*attrDefault
)
2848 m_isReadOnly
= Unset
;
2853 m_attrkind
= wxGridCellAttr::Cell
;
2855 m_sizeRows
= m_sizeCols
= 1;
2856 m_overflow
= UnsetOverflow
;
2858 SetDefAttr(attrDefault
);
2861 wxGridCellAttr
*wxGridCellAttr::Clone() const
2863 wxGridCellAttr
*attr
= new wxGridCellAttr(m_defGridAttr
);
2865 if ( HasTextColour() )
2866 attr
->SetTextColour(GetTextColour());
2867 if ( HasBackgroundColour() )
2868 attr
->SetBackgroundColour(GetBackgroundColour());
2870 attr
->SetFont(GetFont());
2871 if ( HasAlignment() )
2872 attr
->SetAlignment(m_hAlign
, m_vAlign
);
2874 attr
->SetSize( m_sizeRows
, m_sizeCols
);
2878 attr
->SetRenderer(m_renderer
);
2879 m_renderer
->IncRef();
2883 attr
->SetEditor(m_editor
);
2888 attr
->SetReadOnly();
2890 attr
->SetOverflow( m_overflow
== Overflow
);
2891 attr
->SetKind( m_attrkind
);
2896 void wxGridCellAttr::MergeWith(wxGridCellAttr
*mergefrom
)
2898 if ( !HasTextColour() && mergefrom
->HasTextColour() )
2899 SetTextColour(mergefrom
->GetTextColour());
2900 if ( !HasBackgroundColour() && mergefrom
->HasBackgroundColour() )
2901 SetBackgroundColour(mergefrom
->GetBackgroundColour());
2902 if ( !HasFont() && mergefrom
->HasFont() )
2903 SetFont(mergefrom
->GetFont());
2904 if ( !HasAlignment() && mergefrom
->HasAlignment() )
2907 mergefrom
->GetAlignment( &hAlign
, &vAlign
);
2908 SetAlignment(hAlign
, vAlign
);
2910 if ( !HasSize() && mergefrom
->HasSize() )
2911 mergefrom
->GetSize( &m_sizeRows
, &m_sizeCols
);
2913 // Directly access member functions as GetRender/Editor don't just return
2914 // m_renderer/m_editor
2916 // Maybe add support for merge of Render and Editor?
2917 if (!HasRenderer() && mergefrom
->HasRenderer() )
2919 m_renderer
= mergefrom
->m_renderer
;
2920 m_renderer
->IncRef();
2922 if ( !HasEditor() && mergefrom
->HasEditor() )
2924 m_editor
= mergefrom
->m_editor
;
2927 if ( !HasReadWriteMode() && mergefrom
->HasReadWriteMode() )
2928 SetReadOnly(mergefrom
->IsReadOnly());
2930 if (!HasOverflowMode() && mergefrom
->HasOverflowMode() )
2931 SetOverflow(mergefrom
->GetOverflow());
2933 SetDefAttr(mergefrom
->m_defGridAttr
);
2936 void wxGridCellAttr::SetSize(int num_rows
, int num_cols
)
2938 // The size of a cell is normally 1,1
2940 // If this cell is larger (2,2) then this is the top left cell
2941 // the other cells that will be covered (lower right cells) must be
2942 // set to negative or zero values such that
2943 // row + num_rows of the covered cell points to the larger cell (this cell)
2944 // same goes for the col + num_cols.
2946 // Size of 0,0 is NOT valid, neither is <=0 and any positive value
2948 wxASSERT_MSG( (!((num_rows
> 0) && (num_cols
<= 0)) ||
2949 !((num_rows
<= 0) && (num_cols
> 0)) ||
2950 !((num_rows
== 0) && (num_cols
== 0))),
2951 wxT("wxGridCellAttr::SetSize only takes two postive values or negative/zero values"));
2953 m_sizeRows
= num_rows
;
2954 m_sizeCols
= num_cols
;
2957 const wxColour
& wxGridCellAttr::GetTextColour() const
2959 if (HasTextColour())
2963 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2965 return m_defGridAttr
->GetTextColour();
2969 wxFAIL_MSG(wxT("Missing default cell attribute"));
2970 return wxNullColour
;
2974 const wxColour
& wxGridCellAttr::GetBackgroundColour() const
2976 if (HasBackgroundColour())
2980 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2982 return m_defGridAttr
->GetBackgroundColour();
2986 wxFAIL_MSG(wxT("Missing default cell attribute"));
2987 return wxNullColour
;
2991 const wxFont
& wxGridCellAttr::GetFont() const
2997 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2999 return m_defGridAttr
->GetFont();
3003 wxFAIL_MSG(wxT("Missing default cell attribute"));
3008 void wxGridCellAttr::GetAlignment(int *hAlign
, int *vAlign
) const
3017 else if (m_defGridAttr
&& m_defGridAttr
!= this)
3019 m_defGridAttr
->GetAlignment(hAlign
, vAlign
);
3023 wxFAIL_MSG(wxT("Missing default cell attribute"));
3027 void wxGridCellAttr::GetSize( int *num_rows
, int *num_cols
) const
3030 *num_rows
= m_sizeRows
;
3032 *num_cols
= m_sizeCols
;
3035 // GetRenderer and GetEditor use a slightly different decision path about
3036 // which attribute to use. If a non-default attr object has one then it is
3037 // used, otherwise the default editor or renderer is fetched from the grid and
3038 // used. It should be the default for the data type of the cell. If it is
3039 // NULL (because the table has a type that the grid does not have in its
3040 // registry), then the grid's default editor or renderer is used.
3042 wxGridCellRenderer
* wxGridCellAttr::GetRenderer(const wxGrid
* grid
, int row
, int col
) const
3044 wxGridCellRenderer
*renderer
= NULL
;
3046 if ( m_renderer
&& this != m_defGridAttr
)
3048 // use the cells renderer if it has one
3049 renderer
= m_renderer
;
3052 else // no non-default cell renderer
3054 // get default renderer for the data type
3057 // GetDefaultRendererForCell() will do IncRef() for us
3058 renderer
= grid
->GetDefaultRendererForCell(row
, col
);
3061 if ( renderer
== NULL
)
3063 if ( (m_defGridAttr
!= NULL
) && (m_defGridAttr
!= this) )
3065 // if we still don't have one then use the grid default
3066 // (no need for IncRef() here neither)
3067 renderer
= m_defGridAttr
->GetRenderer(NULL
, 0, 0);
3069 else // default grid attr
3071 // use m_renderer which we had decided not to use initially
3072 renderer
= m_renderer
;
3079 // we're supposed to always find something
3080 wxASSERT_MSG(renderer
, wxT("Missing default cell renderer"));
3085 // same as above, except for s/renderer/editor/g
3086 wxGridCellEditor
* wxGridCellAttr::GetEditor(const wxGrid
* grid
, int row
, int col
) const
3088 wxGridCellEditor
*editor
= NULL
;
3090 if ( m_editor
&& this != m_defGridAttr
)
3092 // use the cells editor if it has one
3096 else // no non default cell editor
3098 // get default editor for the data type
3101 // GetDefaultEditorForCell() will do IncRef() for us
3102 editor
= grid
->GetDefaultEditorForCell(row
, col
);
3105 if ( editor
== NULL
)
3107 if ( (m_defGridAttr
!= NULL
) && (m_defGridAttr
!= this) )
3109 // if we still don't have one then use the grid default
3110 // (no need for IncRef() here neither)
3111 editor
= m_defGridAttr
->GetEditor(NULL
, 0, 0);
3113 else // default grid attr
3115 // use m_editor which we had decided not to use initially
3123 // we're supposed to always find something
3124 wxASSERT_MSG(editor
, wxT("Missing default cell editor"));
3129 // ----------------------------------------------------------------------------
3130 // wxGridCellAttrData
3131 // ----------------------------------------------------------------------------
3133 void wxGridCellAttrData::SetAttr(wxGridCellAttr
*attr
, int row
, int col
)
3135 // Note: contrary to wxGridRowOrColAttrData::SetAttr, we must not
3136 // touch attribute's reference counting explicitly, since this
3137 // is managed by class wxGridCellWithAttr
3138 int n
= FindIndex(row
, col
);
3139 if ( n
== wxNOT_FOUND
)
3143 // add the attribute
3144 m_attrs
.Add(new wxGridCellWithAttr(row
, col
, attr
));
3146 //else: nothing to do
3148 else // we already have an attribute for this cell
3152 // change the attribute
3153 m_attrs
[(size_t)n
].ChangeAttr(attr
);
3157 // remove this attribute
3158 m_attrs
.RemoveAt((size_t)n
);
3163 wxGridCellAttr
*wxGridCellAttrData::GetAttr(int row
, int col
) const
3165 wxGridCellAttr
*attr
= NULL
;
3167 int n
= FindIndex(row
, col
);
3168 if ( n
!= wxNOT_FOUND
)
3170 attr
= m_attrs
[(size_t)n
].attr
;
3177 void wxGridCellAttrData::UpdateAttrRows( size_t pos
, int numRows
)
3179 size_t count
= m_attrs
.GetCount();
3180 for ( size_t n
= 0; n
< count
; n
++ )
3182 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
3183 wxCoord row
= coords
.GetRow();
3184 if ((size_t)row
>= pos
)
3188 // If rows inserted, include row counter where necessary
3189 coords
.SetRow(row
+ numRows
);
3191 else if (numRows
< 0)
3193 // If rows deleted ...
3194 if ((size_t)row
>= pos
- numRows
)
3196 // ...either decrement row counter (if row still exists)...
3197 coords
.SetRow(row
+ numRows
);
3201 // ...or remove the attribute
3202 m_attrs
.RemoveAt(n
);
3211 void wxGridCellAttrData::UpdateAttrCols( size_t pos
, int numCols
)
3213 size_t count
= m_attrs
.GetCount();
3214 for ( size_t n
= 0; n
< count
; n
++ )
3216 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
3217 wxCoord col
= coords
.GetCol();
3218 if ( (size_t)col
>= pos
)
3222 // If rows inserted, include row counter where necessary
3223 coords
.SetCol(col
+ numCols
);
3225 else if (numCols
< 0)
3227 // If rows deleted ...
3228 if ((size_t)col
>= pos
- numCols
)
3230 // ...either decrement row counter (if row still exists)...
3231 coords
.SetCol(col
+ numCols
);
3235 // ...or remove the attribute
3236 m_attrs
.RemoveAt(n
);
3245 int wxGridCellAttrData::FindIndex(int row
, int col
) const
3247 size_t count
= m_attrs
.GetCount();
3248 for ( size_t n
= 0; n
< count
; n
++ )
3250 const wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
3251 if ( (coords
.GetRow() == row
) && (coords
.GetCol() == col
) )
3260 // ----------------------------------------------------------------------------
3261 // wxGridRowOrColAttrData
3262 // ----------------------------------------------------------------------------
3264 wxGridRowOrColAttrData::~wxGridRowOrColAttrData()
3266 size_t count
= m_attrs
.GetCount();
3267 for ( size_t n
= 0; n
< count
; n
++ )
3269 m_attrs
[n
]->DecRef();
3273 wxGridCellAttr
*wxGridRowOrColAttrData::GetAttr(int rowOrCol
) const
3275 wxGridCellAttr
*attr
= NULL
;
3277 int n
= m_rowsOrCols
.Index(rowOrCol
);
3278 if ( n
!= wxNOT_FOUND
)
3280 attr
= m_attrs
[(size_t)n
];
3287 void wxGridRowOrColAttrData::SetAttr(wxGridCellAttr
*attr
, int rowOrCol
)
3289 int i
= m_rowsOrCols
.Index(rowOrCol
);
3290 if ( i
== wxNOT_FOUND
)
3294 // add the attribute - no need to do anything to reference count
3295 // since we take ownership of the attribute.
3296 m_rowsOrCols
.Add(rowOrCol
);
3299 // nothing to remove
3303 size_t n
= (size_t)i
;
3304 if ( m_attrs
[n
] == attr
)
3309 // change the attribute, handling reference count manually,
3310 // taking ownership of the new attribute.
3311 m_attrs
[n
]->DecRef();
3316 // remove this attribute, handling reference count manually
3317 m_attrs
[n
]->DecRef();
3318 m_rowsOrCols
.RemoveAt(n
);
3319 m_attrs
.RemoveAt(n
);
3324 void wxGridRowOrColAttrData::UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
)
3326 size_t count
= m_attrs
.GetCount();
3327 for ( size_t n
= 0; n
< count
; n
++ )
3329 int & rowOrCol
= m_rowsOrCols
[n
];
3330 if ( (size_t)rowOrCol
>= pos
)
3332 if ( numRowsOrCols
> 0 )
3334 // If rows inserted, include row counter where necessary
3335 rowOrCol
+= numRowsOrCols
;
3337 else if ( numRowsOrCols
< 0)
3339 // If rows deleted, either decrement row counter (if row still exists)
3340 if ((size_t)rowOrCol
>= pos
- numRowsOrCols
)
3341 rowOrCol
+= numRowsOrCols
;
3344 m_rowsOrCols
.RemoveAt(n
);
3345 m_attrs
[n
]->DecRef();
3346 m_attrs
.RemoveAt(n
);
3355 // ----------------------------------------------------------------------------
3356 // wxGridCellAttrProvider
3357 // ----------------------------------------------------------------------------
3359 wxGridCellAttrProvider::wxGridCellAttrProvider()
3364 wxGridCellAttrProvider::~wxGridCellAttrProvider()
3369 void wxGridCellAttrProvider::InitData()
3371 m_data
= new wxGridCellAttrProviderData
;
3374 wxGridCellAttr
*wxGridCellAttrProvider::GetAttr(int row
, int col
,
3375 wxGridCellAttr::wxAttrKind kind
) const
3377 wxGridCellAttr
*attr
= NULL
;
3382 case (wxGridCellAttr::Any
):
3383 // Get cached merge attributes.
3384 // Currently not used as no cache implemented as not mutable
3385 // attr = m_data->m_mergeAttr.GetAttr(row, col);
3388 // Basically implement old version.
3389 // Also check merge cache, so we don't have to re-merge every time..
3390 wxGridCellAttr
*attrcell
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
3391 wxGridCellAttr
*attrrow
= m_data
->m_rowAttrs
.GetAttr(row
);
3392 wxGridCellAttr
*attrcol
= m_data
->m_colAttrs
.GetAttr(col
);
3394 if ((attrcell
!= attrrow
) && (attrrow
!= attrcol
) && (attrcell
!= attrcol
))
3396 // Two or more are non NULL
3397 attr
= new wxGridCellAttr
;
3398 attr
->SetKind(wxGridCellAttr::Merged
);
3400 // Order is important..
3403 attr
->MergeWith(attrcell
);
3408 attr
->MergeWith(attrcol
);
3413 attr
->MergeWith(attrrow
);
3417 // store merge attr if cache implemented
3419 //m_data->m_mergeAttr.SetAttr(attr, row, col);
3423 // one or none is non null return it or null.
3442 case (wxGridCellAttr::Cell
):
3443 attr
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
3446 case (wxGridCellAttr::Col
):
3447 attr
= m_data
->m_colAttrs
.GetAttr(col
);
3450 case (wxGridCellAttr::Row
):
3451 attr
= m_data
->m_rowAttrs
.GetAttr(row
);
3456 // (wxGridCellAttr::Default):
3457 // (wxGridCellAttr::Merged):
3465 void wxGridCellAttrProvider::SetAttr(wxGridCellAttr
*attr
,
3471 m_data
->m_cellAttrs
.SetAttr(attr
, row
, col
);
3474 void wxGridCellAttrProvider::SetRowAttr(wxGridCellAttr
*attr
, int row
)
3479 m_data
->m_rowAttrs
.SetAttr(attr
, row
);
3482 void wxGridCellAttrProvider::SetColAttr(wxGridCellAttr
*attr
, int col
)
3487 m_data
->m_colAttrs
.SetAttr(attr
, col
);
3490 void wxGridCellAttrProvider::UpdateAttrRows( size_t pos
, int numRows
)
3494 m_data
->m_cellAttrs
.UpdateAttrRows( pos
, numRows
);
3496 m_data
->m_rowAttrs
.UpdateAttrRowsOrCols( pos
, numRows
);
3500 void wxGridCellAttrProvider::UpdateAttrCols( size_t pos
, int numCols
)
3504 m_data
->m_cellAttrs
.UpdateAttrCols( pos
, numCols
);
3506 m_data
->m_colAttrs
.UpdateAttrRowsOrCols( pos
, numCols
);
3510 // ----------------------------------------------------------------------------
3511 // wxGridTypeRegistry
3512 // ----------------------------------------------------------------------------
3514 wxGridTypeRegistry::~wxGridTypeRegistry()
3516 size_t count
= m_typeinfo
.GetCount();
3517 for ( size_t i
= 0; i
< count
; i
++ )
3518 delete m_typeinfo
[i
];
3521 void wxGridTypeRegistry::RegisterDataType(const wxString
& typeName
,
3522 wxGridCellRenderer
* renderer
,
3523 wxGridCellEditor
* editor
)
3525 wxGridDataTypeInfo
* info
= new wxGridDataTypeInfo(typeName
, renderer
, editor
);
3527 // is it already registered?
3528 int loc
= FindRegisteredDataType(typeName
);
3529 if ( loc
!= wxNOT_FOUND
)
3531 delete m_typeinfo
[loc
];
3532 m_typeinfo
[loc
] = info
;
3536 m_typeinfo
.Add(info
);
3540 int wxGridTypeRegistry::FindRegisteredDataType(const wxString
& typeName
)
3542 size_t count
= m_typeinfo
.GetCount();
3543 for ( size_t i
= 0; i
< count
; i
++ )
3545 if ( typeName
== m_typeinfo
[i
]->m_typeName
)
3554 int wxGridTypeRegistry::FindDataType(const wxString
& typeName
)
3556 int index
= FindRegisteredDataType(typeName
);
3557 if ( index
== wxNOT_FOUND
)
3559 // check whether this is one of the standard ones, in which case
3560 // register it "on the fly"
3562 if ( typeName
== wxGRID_VALUE_STRING
)
3564 RegisterDataType(wxGRID_VALUE_STRING
,
3565 new wxGridCellStringRenderer
,
3566 new wxGridCellTextEditor
);
3569 #endif // wxUSE_TEXTCTRL
3571 if ( typeName
== wxGRID_VALUE_BOOL
)
3573 RegisterDataType(wxGRID_VALUE_BOOL
,
3574 new wxGridCellBoolRenderer
,
3575 new wxGridCellBoolEditor
);
3578 #endif // wxUSE_CHECKBOX
3580 if ( typeName
== wxGRID_VALUE_NUMBER
)
3582 RegisterDataType(wxGRID_VALUE_NUMBER
,
3583 new wxGridCellNumberRenderer
,
3584 new wxGridCellNumberEditor
);
3586 else if ( typeName
== wxGRID_VALUE_FLOAT
)
3588 RegisterDataType(wxGRID_VALUE_FLOAT
,
3589 new wxGridCellFloatRenderer
,
3590 new wxGridCellFloatEditor
);
3593 #endif // wxUSE_TEXTCTRL
3595 if ( typeName
== wxGRID_VALUE_CHOICE
)
3597 RegisterDataType(wxGRID_VALUE_CHOICE
,
3598 new wxGridCellStringRenderer
,
3599 new wxGridCellChoiceEditor
);
3602 #endif // wxUSE_COMBOBOX
3607 // we get here only if just added the entry for this type, so return
3609 index
= m_typeinfo
.GetCount() - 1;
3615 int wxGridTypeRegistry::FindOrCloneDataType(const wxString
& typeName
)
3617 int index
= FindDataType(typeName
);
3618 if ( index
== wxNOT_FOUND
)
3620 // the first part of the typename is the "real" type, anything after ':'
3621 // are the parameters for the renderer
3622 index
= FindDataType(typeName
.BeforeFirst(_T(':')));
3623 if ( index
== wxNOT_FOUND
)
3628 wxGridCellRenderer
*renderer
= GetRenderer(index
);
3629 wxGridCellRenderer
*rendererOld
= renderer
;
3630 renderer
= renderer
->Clone();
3631 rendererOld
->DecRef();
3633 wxGridCellEditor
*editor
= GetEditor(index
);
3634 wxGridCellEditor
*editorOld
= editor
;
3635 editor
= editor
->Clone();
3636 editorOld
->DecRef();
3638 // do it even if there are no parameters to reset them to defaults
3639 wxString params
= typeName
.AfterFirst(_T(':'));
3640 renderer
->SetParameters(params
);
3641 editor
->SetParameters(params
);
3643 // register the new typename
3644 RegisterDataType(typeName
, renderer
, editor
);
3646 // we just registered it, it's the last one
3647 index
= m_typeinfo
.GetCount() - 1;
3653 wxGridCellRenderer
* wxGridTypeRegistry::GetRenderer(int index
)
3655 wxGridCellRenderer
* renderer
= m_typeinfo
[index
]->m_renderer
;
3662 wxGridCellEditor
* wxGridTypeRegistry::GetEditor(int index
)
3664 wxGridCellEditor
* editor
= m_typeinfo
[index
]->m_editor
;
3671 // ----------------------------------------------------------------------------
3673 // ----------------------------------------------------------------------------
3675 IMPLEMENT_ABSTRACT_CLASS( wxGridTableBase
, wxObject
)
3677 wxGridTableBase::wxGridTableBase()
3680 m_attrProvider
= NULL
;
3683 wxGridTableBase::~wxGridTableBase()
3685 delete m_attrProvider
;
3688 void wxGridTableBase::SetAttrProvider(wxGridCellAttrProvider
*attrProvider
)
3690 delete m_attrProvider
;
3691 m_attrProvider
= attrProvider
;
3694 bool wxGridTableBase::CanHaveAttributes()
3696 if ( ! GetAttrProvider() )
3698 // use the default attr provider by default
3699 SetAttrProvider(new wxGridCellAttrProvider
);
3705 wxGridCellAttr
*wxGridTableBase::GetAttr(int row
, int col
, wxGridCellAttr::wxAttrKind kind
)
3707 if ( m_attrProvider
)
3708 return m_attrProvider
->GetAttr(row
, col
, kind
);
3713 void wxGridTableBase::SetAttr(wxGridCellAttr
* attr
, int row
, int col
)
3715 if ( m_attrProvider
)
3718 attr
->SetKind(wxGridCellAttr::Cell
);
3719 m_attrProvider
->SetAttr(attr
, row
, col
);
3723 // as we take ownership of the pointer and don't store it, we must
3729 void wxGridTableBase::SetRowAttr(wxGridCellAttr
*attr
, int row
)
3731 if ( m_attrProvider
)
3733 attr
->SetKind(wxGridCellAttr::Row
);
3734 m_attrProvider
->SetRowAttr(attr
, row
);
3738 // as we take ownership of the pointer and don't store it, we must
3744 void wxGridTableBase::SetColAttr(wxGridCellAttr
*attr
, int col
)
3746 if ( m_attrProvider
)
3748 attr
->SetKind(wxGridCellAttr::Col
);
3749 m_attrProvider
->SetColAttr(attr
, col
);
3753 // as we take ownership of the pointer and don't store it, we must
3759 bool wxGridTableBase::InsertRows( size_t WXUNUSED(pos
),
3760 size_t WXUNUSED(numRows
) )
3762 wxFAIL_MSG( wxT("Called grid table class function InsertRows\nbut your derived table class does not override this function") );
3767 bool wxGridTableBase::AppendRows( size_t WXUNUSED(numRows
) )
3769 wxFAIL_MSG( wxT("Called grid table class function AppendRows\nbut your derived table class does not override this function"));
3774 bool wxGridTableBase::DeleteRows( size_t WXUNUSED(pos
),
3775 size_t WXUNUSED(numRows
) )
3777 wxFAIL_MSG( wxT("Called grid table class function DeleteRows\nbut your derived table class does not override this function"));
3782 bool wxGridTableBase::InsertCols( size_t WXUNUSED(pos
),
3783 size_t WXUNUSED(numCols
) )
3785 wxFAIL_MSG( wxT("Called grid table class function InsertCols\nbut your derived table class does not override this function"));
3790 bool wxGridTableBase::AppendCols( size_t WXUNUSED(numCols
) )
3792 wxFAIL_MSG(wxT("Called grid table class function AppendCols\nbut your derived table class does not override this function"));
3797 bool wxGridTableBase::DeleteCols( size_t WXUNUSED(pos
),
3798 size_t WXUNUSED(numCols
) )
3800 wxFAIL_MSG( wxT("Called grid table class function DeleteCols\nbut your derived table class does not override this function"));
3805 wxString
wxGridTableBase::GetRowLabelValue( int row
)
3809 // RD: Starting the rows at zero confuses users,
3810 // no matter how much it makes sense to us geeks.
3816 wxString
wxGridTableBase::GetColLabelValue( int col
)
3818 // default col labels are:
3819 // cols 0 to 25 : A-Z
3820 // cols 26 to 675 : AA-ZZ
3825 for ( n
= 1; ; n
++ )
3827 s
+= (wxChar
) (_T('A') + (wxChar
)(col
% 26));
3833 // reverse the string...
3835 for ( i
= 0; i
< n
; i
++ )
3843 wxString
wxGridTableBase::GetTypeName( int WXUNUSED(row
), int WXUNUSED(col
) )
3845 return wxGRID_VALUE_STRING
;
3848 bool wxGridTableBase::CanGetValueAs( int WXUNUSED(row
), int WXUNUSED(col
),
3849 const wxString
& typeName
)
3851 return typeName
== wxGRID_VALUE_STRING
;
3854 bool wxGridTableBase::CanSetValueAs( int row
, int col
, const wxString
& typeName
)
3856 return CanGetValueAs(row
, col
, typeName
);
3859 long wxGridTableBase::GetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
) )
3864 double wxGridTableBase::GetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
) )
3869 bool wxGridTableBase::GetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
) )
3874 void wxGridTableBase::SetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
),
3875 long WXUNUSED(value
) )
3879 void wxGridTableBase::SetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
),
3880 double WXUNUSED(value
) )
3884 void wxGridTableBase::SetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
),
3885 bool WXUNUSED(value
) )
3889 void* wxGridTableBase::GetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
3890 const wxString
& WXUNUSED(typeName
) )
3895 void wxGridTableBase::SetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
3896 const wxString
& WXUNUSED(typeName
),
3897 void* WXUNUSED(value
) )
3901 //////////////////////////////////////////////////////////////////////
3903 // Message class for the grid table to send requests and notifications
3907 wxGridTableMessage::wxGridTableMessage()
3915 wxGridTableMessage::wxGridTableMessage( wxGridTableBase
*table
, int id
,
3916 int commandInt1
, int commandInt2
)
3920 m_comInt1
= commandInt1
;
3921 m_comInt2
= commandInt2
;
3924 //////////////////////////////////////////////////////////////////////
3926 // A basic grid table for string data. An object of this class will
3927 // created by wxGrid if you don't specify an alternative table class.
3930 WX_DEFINE_OBJARRAY(wxGridStringArray
)
3932 IMPLEMENT_DYNAMIC_CLASS( wxGridStringTable
, wxGridTableBase
)
3934 wxGridStringTable::wxGridStringTable()
3939 wxGridStringTable::wxGridStringTable( int numRows
, int numCols
)
3942 m_data
.Alloc( numRows
);
3945 sa
.Alloc( numCols
);
3946 sa
.Add( wxEmptyString
, numCols
);
3948 m_data
.Add( sa
, numRows
);
3951 wxGridStringTable::~wxGridStringTable()
3955 int wxGridStringTable::GetNumberRows()
3957 return m_data
.GetCount();
3960 int wxGridStringTable::GetNumberCols()
3962 if ( m_data
.GetCount() > 0 )
3963 return m_data
[0].GetCount();
3968 wxString
wxGridStringTable::GetValue( int row
, int col
)
3970 wxCHECK_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3972 _T("invalid row or column index in wxGridStringTable") );
3974 return m_data
[row
][col
];
3977 void wxGridStringTable::SetValue( int row
, int col
, const wxString
& value
)
3979 wxCHECK_RET( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3980 _T("invalid row or column index in wxGridStringTable") );
3982 m_data
[row
][col
] = value
;
3985 void wxGridStringTable::Clear()
3988 int numRows
, numCols
;
3990 numRows
= m_data
.GetCount();
3993 numCols
= m_data
[0].GetCount();
3995 for ( row
= 0; row
< numRows
; row
++ )
3997 for ( col
= 0; col
< numCols
; col
++ )
3999 m_data
[row
][col
] = wxEmptyString
;
4005 bool wxGridStringTable::InsertRows( size_t pos
, size_t numRows
)
4007 size_t curNumRows
= m_data
.GetCount();
4008 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
4009 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
4011 if ( pos
>= curNumRows
)
4013 return AppendRows( numRows
);
4017 sa
.Alloc( curNumCols
);
4018 sa
.Add( wxEmptyString
, curNumCols
);
4019 m_data
.Insert( sa
, pos
, numRows
);
4023 wxGridTableMessage
msg( this,
4024 wxGRIDTABLE_NOTIFY_ROWS_INSERTED
,
4028 GetView()->ProcessTableMessage( msg
);
4034 bool wxGridStringTable::AppendRows( size_t numRows
)
4036 size_t curNumRows
= m_data
.GetCount();
4037 size_t curNumCols
= ( curNumRows
> 0
4038 ? m_data
[0].GetCount()
4039 : ( GetView() ? GetView()->GetNumberCols() : 0 ) );
4042 if ( curNumCols
> 0 )
4044 sa
.Alloc( curNumCols
);
4045 sa
.Add( wxEmptyString
, curNumCols
);
4048 m_data
.Add( sa
, numRows
);
4052 wxGridTableMessage
msg( this,
4053 wxGRIDTABLE_NOTIFY_ROWS_APPENDED
,
4056 GetView()->ProcessTableMessage( msg
);
4062 bool wxGridStringTable::DeleteRows( size_t pos
, size_t numRows
)
4064 size_t curNumRows
= m_data
.GetCount();
4066 if ( pos
>= curNumRows
)
4068 wxFAIL_MSG( wxString::Format
4070 wxT("Called wxGridStringTable::DeleteRows(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu rows"),
4072 (unsigned long)numRows
,
4073 (unsigned long)curNumRows
4079 if ( numRows
> curNumRows
- pos
)
4081 numRows
= curNumRows
- pos
;
4084 if ( numRows
>= curNumRows
)
4090 m_data
.RemoveAt( pos
, numRows
);
4095 wxGridTableMessage
msg( this,
4096 wxGRIDTABLE_NOTIFY_ROWS_DELETED
,
4100 GetView()->ProcessTableMessage( msg
);
4106 bool wxGridStringTable::InsertCols( size_t pos
, size_t numCols
)
4110 size_t curNumRows
= m_data
.GetCount();
4111 size_t curNumCols
= ( curNumRows
> 0
4112 ? m_data
[0].GetCount()
4113 : ( GetView() ? GetView()->GetNumberCols() : 0 ) );
4115 if ( pos
>= curNumCols
)
4117 return AppendCols( numCols
);
4120 if ( !m_colLabels
.IsEmpty() )
4122 m_colLabels
.Insert( wxEmptyString
, pos
, numCols
);
4125 for ( i
= pos
; i
< pos
+ numCols
; i
++ )
4126 m_colLabels
[i
] = wxGridTableBase::GetColLabelValue( i
);
4129 for ( row
= 0; row
< curNumRows
; row
++ )
4131 for ( col
= pos
; col
< pos
+ numCols
; col
++ )
4133 m_data
[row
].Insert( wxEmptyString
, col
);
4139 wxGridTableMessage
msg( this,
4140 wxGRIDTABLE_NOTIFY_COLS_INSERTED
,
4144 GetView()->ProcessTableMessage( msg
);
4150 bool wxGridStringTable::AppendCols( size_t numCols
)
4154 size_t curNumRows
= m_data
.GetCount();
4156 for ( row
= 0; row
< curNumRows
; row
++ )
4158 m_data
[row
].Add( wxEmptyString
, numCols
);
4163 wxGridTableMessage
msg( this,
4164 wxGRIDTABLE_NOTIFY_COLS_APPENDED
,
4167 GetView()->ProcessTableMessage( msg
);
4173 bool wxGridStringTable::DeleteCols( size_t pos
, size_t numCols
)
4177 size_t curNumRows
= m_data
.GetCount();
4178 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
4179 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
4181 if ( pos
>= curNumCols
)
4183 wxFAIL_MSG( wxString::Format
4185 wxT("Called wxGridStringTable::DeleteCols(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu cols"),
4187 (unsigned long)numCols
,
4188 (unsigned long)curNumCols
4195 colID
= GetView()->GetColAt( pos
);
4199 if ( numCols
> curNumCols
- colID
)
4201 numCols
= curNumCols
- colID
;
4204 if ( !m_colLabels
.IsEmpty() )
4206 // m_colLabels stores just as many elements as it needs, e.g. if only
4207 // the label of the first column had been set it would have only one
4208 // element and not numCols, so account for it
4209 int nToRm
= m_colLabels
.size() - colID
;
4211 m_colLabels
.RemoveAt( colID
, nToRm
);
4214 for ( row
= 0; row
< curNumRows
; row
++ )
4216 if ( numCols
>= curNumCols
)
4218 m_data
[row
].Clear();
4222 m_data
[row
].RemoveAt( colID
, numCols
);
4228 wxGridTableMessage
msg( this,
4229 wxGRIDTABLE_NOTIFY_COLS_DELETED
,
4233 GetView()->ProcessTableMessage( msg
);
4239 wxString
wxGridStringTable::GetRowLabelValue( int row
)
4241 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
4243 // using default label
4245 return wxGridTableBase::GetRowLabelValue( row
);
4249 return m_rowLabels
[row
];
4253 wxString
wxGridStringTable::GetColLabelValue( int col
)
4255 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
4257 // using default label
4259 return wxGridTableBase::GetColLabelValue( col
);
4263 return m_colLabels
[col
];
4267 void wxGridStringTable::SetRowLabelValue( int row
, const wxString
& value
)
4269 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
4271 int n
= m_rowLabels
.GetCount();
4274 for ( i
= n
; i
<= row
; i
++ )
4276 m_rowLabels
.Add( wxGridTableBase::GetRowLabelValue(i
) );
4280 m_rowLabels
[row
] = value
;
4283 void wxGridStringTable::SetColLabelValue( int col
, const wxString
& value
)
4285 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
4287 int n
= m_colLabels
.GetCount();
4290 for ( i
= n
; i
<= col
; i
++ )
4292 m_colLabels
.Add( wxGridTableBase::GetColLabelValue(i
) );
4296 m_colLabels
[col
] = value
;
4300 //////////////////////////////////////////////////////////////////////
4301 //////////////////////////////////////////////////////////////////////
4303 BEGIN_EVENT_TABLE(wxGridSubwindow
, wxWindow
)
4304 EVT_MOUSE_CAPTURE_LOST(wxGridSubwindow::OnMouseCaptureLost
)
4307 void wxGridSubwindow::OnMouseCaptureLost(wxMouseCaptureLostEvent
& WXUNUSED(event
))
4309 m_owner
->CancelMouseCapture();
4312 BEGIN_EVENT_TABLE( wxGridRowLabelWindow
, wxGridSubwindow
)
4313 EVT_PAINT( wxGridRowLabelWindow::OnPaint
)
4314 EVT_MOUSEWHEEL( wxGridRowLabelWindow::OnMouseWheel
)
4315 EVT_MOUSE_EVENTS( wxGridRowLabelWindow::OnMouseEvent
)
4318 void wxGridRowLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
4322 // NO - don't do this because it will set both the x and y origin
4323 // coords to match the parent scrolled window and we just want to
4324 // set the y coord - MB
4326 // m_owner->PrepareDC( dc );
4329 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
4330 wxPoint pt
= dc
.GetDeviceOrigin();
4331 dc
.SetDeviceOrigin( pt
.x
, pt
.y
-y
);
4333 wxArrayInt rows
= m_owner
->CalcRowLabelsExposed( GetUpdateRegion() );
4334 m_owner
->DrawRowLabels( dc
, rows
);
4337 void wxGridRowLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
4339 m_owner
->ProcessRowLabelMouseEvent( event
);
4342 void wxGridRowLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
4344 if (!m_owner
->GetEventHandler()->ProcessEvent( event
))
4348 //////////////////////////////////////////////////////////////////////
4350 BEGIN_EVENT_TABLE( wxGridColLabelWindow
, wxGridSubwindow
)
4351 EVT_PAINT( wxGridColLabelWindow::OnPaint
)
4352 EVT_MOUSEWHEEL( wxGridColLabelWindow::OnMouseWheel
)
4353 EVT_MOUSE_EVENTS( wxGridColLabelWindow::OnMouseEvent
)
4356 void wxGridColLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
4360 // NO - don't do this because it will set both the x and y origin
4361 // coords to match the parent scrolled window and we just want to
4362 // set the x coord - MB
4364 // m_owner->PrepareDC( dc );
4367 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
4368 wxPoint pt
= dc
.GetDeviceOrigin();
4369 if (GetLayoutDirection() == wxLayout_RightToLeft
)
4370 dc
.SetDeviceOrigin( pt
.x
+x
, pt
.y
);
4372 dc
.SetDeviceOrigin( pt
.x
-x
, pt
.y
);
4374 wxArrayInt cols
= m_owner
->CalcColLabelsExposed( GetUpdateRegion() );
4375 m_owner
->DrawColLabels( dc
, cols
);
4378 void wxGridColLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
4380 m_owner
->ProcessColLabelMouseEvent( event
);
4383 void wxGridColLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
4385 if (!m_owner
->GetEventHandler()->ProcessEvent( event
))
4389 //////////////////////////////////////////////////////////////////////
4391 BEGIN_EVENT_TABLE( wxGridCornerLabelWindow
, wxGridSubwindow
)
4392 EVT_MOUSEWHEEL( wxGridCornerLabelWindow::OnMouseWheel
)
4393 EVT_MOUSE_EVENTS( wxGridCornerLabelWindow::OnMouseEvent
)
4394 EVT_PAINT( wxGridCornerLabelWindow::OnPaint
)
4397 void wxGridCornerLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
4401 m_owner
->DrawCornerLabel(dc
);
4404 void wxGridCornerLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
4406 m_owner
->ProcessCornerLabelMouseEvent( event
);
4409 void wxGridCornerLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
4411 if (!m_owner
->GetEventHandler()->ProcessEvent(event
))
4415 //////////////////////////////////////////////////////////////////////
4417 BEGIN_EVENT_TABLE( wxGridWindow
, wxGridSubwindow
)
4418 EVT_PAINT( wxGridWindow::OnPaint
)
4419 EVT_MOUSEWHEEL( wxGridWindow::OnMouseWheel
)
4420 EVT_MOUSE_EVENTS( wxGridWindow::OnMouseEvent
)
4421 EVT_KEY_DOWN( wxGridWindow::OnKeyDown
)
4422 EVT_KEY_UP( wxGridWindow::OnKeyUp
)
4423 EVT_CHAR( wxGridWindow::OnChar
)
4424 EVT_SET_FOCUS( wxGridWindow::OnFocus
)
4425 EVT_KILL_FOCUS( wxGridWindow::OnFocus
)
4426 EVT_ERASE_BACKGROUND( wxGridWindow::OnEraseBackground
)
4429 void wxGridWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
4431 wxPaintDC
dc( this );
4432 m_owner
->PrepareDC( dc
);
4433 wxRegion reg
= GetUpdateRegion();
4434 wxGridCellCoordsArray dirtyCells
= m_owner
->CalcCellsExposed( reg
);
4435 m_owner
->DrawGridCellArea( dc
, dirtyCells
);
4437 m_owner
->DrawGridSpace( dc
);
4439 m_owner
->DrawAllGridLines( dc
, reg
);
4441 m_owner
->DrawHighlight( dc
, dirtyCells
);
4444 void wxGridWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
4446 wxWindow::ScrollWindow( dx
, dy
, rect
);
4447 m_owner
->GetGridRowLabelWindow()->ScrollWindow( 0, dy
, rect
);
4448 m_owner
->GetGridColLabelWindow()->ScrollWindow( dx
, 0, rect
);
4451 void wxGridWindow::OnMouseEvent( wxMouseEvent
& event
)
4453 if (event
.ButtonDown(wxMOUSE_BTN_LEFT
) && FindFocus() != this)
4456 m_owner
->ProcessGridCellMouseEvent( event
);
4459 void wxGridWindow::OnMouseWheel( wxMouseEvent
& event
)
4461 if (!m_owner
->GetEventHandler()->ProcessEvent( event
))
4465 // This seems to be required for wxMotif/wxGTK otherwise the mouse
4466 // cursor must be in the cell edit control to get key events
4468 void wxGridWindow::OnKeyDown( wxKeyEvent
& event
)
4470 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
4474 void wxGridWindow::OnKeyUp( wxKeyEvent
& event
)
4476 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
4480 void wxGridWindow::OnChar( wxKeyEvent
& event
)
4482 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
4486 void wxGridWindow::OnEraseBackground( wxEraseEvent
& WXUNUSED(event
) )
4490 void wxGridWindow::OnFocus(wxFocusEvent
& event
)
4492 // and if we have any selection, it has to be repainted, because it
4493 // uses different colour when the grid is not focused:
4494 if ( m_owner
->IsSelection() )
4500 // NB: Note that this code is in "else" branch only because the other
4501 // branch refreshes everything and so there's no point in calling
4502 // Refresh() again, *not* because it should only be done if
4503 // !IsSelection(). If the above code is ever optimized to refresh
4504 // only selected area, this needs to be moved out of the "else"
4505 // branch so that it's always executed.
4507 // current cell cursor {dis,re}appears on focus change:
4508 const wxGridCellCoords
cursorCoords(m_owner
->GetGridCursorRow(),
4509 m_owner
->GetGridCursorCol());
4510 const wxRect cursor
=
4511 m_owner
->BlockToDeviceRect(cursorCoords
, cursorCoords
);
4512 Refresh(true, &cursor
);
4515 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
4519 #define internalXToCol(x) XToCol(x, true)
4520 #define internalYToRow(y) YToRow(y, true)
4522 /////////////////////////////////////////////////////////////////////
4524 #if wxUSE_EXTENDED_RTTI
4525 WX_DEFINE_FLAGS( wxGridStyle
)
4527 wxBEGIN_FLAGS( wxGridStyle
)
4528 // new style border flags, we put them first to
4529 // use them for streaming out
4530 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
4531 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
4532 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
4533 wxFLAGS_MEMBER(wxBORDER_RAISED
)
4534 wxFLAGS_MEMBER(wxBORDER_STATIC
)
4535 wxFLAGS_MEMBER(wxBORDER_NONE
)
4537 // old style border flags
4538 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
4539 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
4540 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
4541 wxFLAGS_MEMBER(wxRAISED_BORDER
)
4542 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
4543 wxFLAGS_MEMBER(wxBORDER
)
4545 // standard window styles
4546 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
4547 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
4548 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
4549 wxFLAGS_MEMBER(wxWANTS_CHARS
)
4550 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE
)
4551 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
4552 wxFLAGS_MEMBER(wxVSCROLL
)
4553 wxFLAGS_MEMBER(wxHSCROLL
)
4555 wxEND_FLAGS( wxGridStyle
)
4557 IMPLEMENT_DYNAMIC_CLASS_XTI(wxGrid
, wxScrolledWindow
,"wx/grid.h")
4559 wxBEGIN_PROPERTIES_TABLE(wxGrid
)
4560 wxHIDE_PROPERTY( Children
)
4561 wxPROPERTY_FLAGS( WindowStyle
, wxGridStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
4562 wxEND_PROPERTIES_TABLE()
4564 wxBEGIN_HANDLERS_TABLE(wxGrid
)
4565 wxEND_HANDLERS_TABLE()
4567 wxCONSTRUCTOR_5( wxGrid
, wxWindow
* , Parent
, wxWindowID
, Id
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
4570 TODO : Expose more information of a list's layout, etc. via appropriate objects (e.g., NotebookPageInfo)
4573 IMPLEMENT_DYNAMIC_CLASS( wxGrid
, wxScrolledWindow
)
4576 BEGIN_EVENT_TABLE( wxGrid
, wxScrolledWindow
)
4577 EVT_PAINT( wxGrid::OnPaint
)
4578 EVT_SIZE( wxGrid::OnSize
)
4579 EVT_KEY_DOWN( wxGrid::OnKeyDown
)
4580 EVT_KEY_UP( wxGrid::OnKeyUp
)
4581 EVT_CHAR ( wxGrid::OnChar
)
4582 EVT_ERASE_BACKGROUND( wxGrid::OnEraseBackground
)
4585 bool wxGrid::Create(wxWindow
*parent
, wxWindowID id
,
4586 const wxPoint
& pos
, const wxSize
& size
,
4587 long style
, const wxString
& name
)
4589 if (!wxScrolledWindow::Create(parent
, id
, pos
, size
,
4590 style
| wxWANTS_CHARS
, name
))
4593 m_colMinWidths
= wxLongToLongHashMap(GRID_HASH_SIZE
);
4594 m_rowMinHeights
= wxLongToLongHashMap(GRID_HASH_SIZE
);
4597 SetInitialSize(size
);
4598 SetScrollRate(m_scrollLineX
, m_scrollLineY
);
4607 m_winCapture
->ReleaseMouse();
4609 // Ensure that the editor control is destroyed before the grid is,
4610 // otherwise we crash later when the editor tries to do something with the
4611 // half destroyed grid
4612 HideCellEditControl();
4614 // Must do this or ~wxScrollHelper will pop the wrong event handler
4615 SetTargetWindow(this);
4617 wxSafeDecRef(m_defaultCellAttr
);
4619 #ifdef DEBUG_ATTR_CACHE
4620 size_t total
= gs_nAttrCacheHits
+ gs_nAttrCacheMisses
;
4621 wxPrintf(_T("wxGrid attribute cache statistics: "
4622 "total: %u, hits: %u (%u%%)\n"),
4623 total
, gs_nAttrCacheHits
,
4624 total
? (gs_nAttrCacheHits
*100) / total
: 0);
4627 // if we own the table, just delete it, otherwise at least don't leave it
4628 // with dangling view pointer
4631 else if ( m_table
&& m_table
->GetView() == this )
4632 m_table
->SetView(NULL
);
4634 delete m_typeRegistry
;
4639 // ----- internal init and update functions
4642 // NOTE: If using the default visual attributes works everywhere then this can
4643 // be removed as well as the #else cases below.
4644 #define _USE_VISATTR 0
4646 void wxGrid::Create()
4648 // create the type registry
4649 m_typeRegistry
= new wxGridTypeRegistry
;
4651 m_cellEditCtrlEnabled
= false;
4653 m_defaultCellAttr
= new wxGridCellAttr();
4655 // Set default cell attributes
4656 m_defaultCellAttr
->SetDefAttr(m_defaultCellAttr
);
4657 m_defaultCellAttr
->SetKind(wxGridCellAttr::Default
);
4658 m_defaultCellAttr
->SetFont(GetFont());
4659 m_defaultCellAttr
->SetAlignment(wxALIGN_LEFT
, wxALIGN_TOP
);
4660 m_defaultCellAttr
->SetRenderer(new wxGridCellStringRenderer
);
4661 m_defaultCellAttr
->SetEditor(new wxGridCellTextEditor
);
4664 wxVisualAttributes gva
= wxListBox::GetClassDefaultAttributes();
4665 wxVisualAttributes lva
= wxPanel::GetClassDefaultAttributes();
4667 m_defaultCellAttr
->SetTextColour(gva
.colFg
);
4668 m_defaultCellAttr
->SetBackgroundColour(gva
.colBg
);
4671 m_defaultCellAttr
->SetTextColour(
4672 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
4673 m_defaultCellAttr
->SetBackgroundColour(
4674 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
4679 m_currentCellCoords
= wxGridNoCellCoords
;
4681 // subwindow components that make up the wxGrid
4682 m_rowLabelWin
= new wxGridRowLabelWindow(this);
4683 CreateColumnWindow();
4684 m_cornerLabelWin
= new wxGridCornerLabelWindow(this);
4685 m_gridWin
= new wxGridWindow( this );
4687 SetTargetWindow( m_gridWin
);
4690 wxColour gfg
= gva
.colFg
;
4691 wxColour gbg
= gva
.colBg
;
4692 wxColour lfg
= lva
.colFg
;
4693 wxColour lbg
= lva
.colBg
;
4695 wxColour gfg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT
);
4696 wxColour gbg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOW
);
4697 wxColour lfg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT
);
4698 wxColour lbg
= wxSystemSettings::GetColour( wxSYS_COLOUR_BTNFACE
);
4701 m_cornerLabelWin
->SetOwnForegroundColour(lfg
);
4702 m_cornerLabelWin
->SetOwnBackgroundColour(lbg
);
4703 m_rowLabelWin
->SetOwnForegroundColour(lfg
);
4704 m_rowLabelWin
->SetOwnBackgroundColour(lbg
);
4705 m_colWindow
->SetOwnForegroundColour(lfg
);
4706 m_colWindow
->SetOwnBackgroundColour(lbg
);
4708 m_gridWin
->SetOwnForegroundColour(gfg
);
4709 m_gridWin
->SetOwnBackgroundColour(gbg
);
4711 m_labelBackgroundColour
= m_rowLabelWin
->GetBackgroundColour();
4712 m_labelTextColour
= m_rowLabelWin
->GetForegroundColour();
4714 // now that we have the grid window, use its font to compute the default
4716 m_defaultRowHeight
= m_gridWin
->GetCharHeight();
4717 #if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl()
4718 m_defaultRowHeight
+= 8;
4720 m_defaultRowHeight
+= 4;
4725 void wxGrid::CreateColumnWindow()
4727 if ( m_useNativeHeader
)
4729 m_colWindow
= new wxGridHeaderCtrl(this);
4730 m_colLabelHeight
= m_colWindow
->GetBestSize().y
;
4732 else // draw labels ourselves
4734 m_colWindow
= new wxGridColLabelWindow(this);
4735 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
4739 bool wxGrid::CreateGrid( int numRows
, int numCols
,
4740 wxGridSelectionModes selmode
)
4742 wxCHECK_MSG( !m_created
,
4744 wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
4746 return SetTable(new wxGridStringTable(numRows
, numCols
), true, selmode
);
4749 void wxGrid::SetSelectionMode(wxGridSelectionModes selmode
)
4751 wxCHECK_RET( m_created
,
4752 wxT("Called wxGrid::SetSelectionMode() before calling CreateGrid()") );
4754 m_selection
->SetSelectionMode( selmode
);
4757 wxGrid::wxGridSelectionModes
wxGrid::GetSelectionMode() const
4759 wxCHECK_MSG( m_created
, wxGridSelectCells
,
4760 wxT("Called wxGrid::GetSelectionMode() before calling CreateGrid()") );
4762 return m_selection
->GetSelectionMode();
4766 wxGrid::SetTable(wxGridTableBase
*table
,
4768 wxGrid::wxGridSelectionModes selmode
)
4770 bool checkSelection
= false;
4773 // stop all processing
4778 m_table
->SetView(0);
4790 checkSelection
= true;
4792 // kill row and column size arrays
4793 m_colWidths
.Empty();
4794 m_colRights
.Empty();
4795 m_rowHeights
.Empty();
4796 m_rowBottoms
.Empty();
4801 m_numRows
= table
->GetNumberRows();
4802 m_numCols
= table
->GetNumberCols();
4804 if ( m_useNativeHeader
)
4805 GetColHeader()->SetColumnCount(m_numCols
);
4808 m_table
->SetView( this );
4809 m_ownTable
= takeOwnership
;
4810 m_selection
= new wxGridSelection( this, selmode
);
4813 // If the newly set table is smaller than the
4814 // original one current cell and selection regions
4815 // might be invalid,
4816 m_selectedBlockCorner
= wxGridNoCellCoords
;
4817 m_currentCellCoords
=
4818 wxGridCellCoords(wxMin(m_numRows
, m_currentCellCoords
.GetRow()),
4819 wxMin(m_numCols
, m_currentCellCoords
.GetCol()));
4820 if (m_selectedBlockTopLeft
.GetRow() >= m_numRows
||
4821 m_selectedBlockTopLeft
.GetCol() >= m_numCols
)
4823 m_selectedBlockTopLeft
= wxGridNoCellCoords
;
4824 m_selectedBlockBottomRight
= wxGridNoCellCoords
;
4827 m_selectedBlockBottomRight
=
4828 wxGridCellCoords(wxMin(m_numRows
,
4829 m_selectedBlockBottomRight
.GetRow()),
4831 m_selectedBlockBottomRight
.GetCol()));
4845 m_cornerLabelWin
= NULL
;
4846 m_rowLabelWin
= NULL
;
4854 m_defaultCellAttr
= NULL
;
4855 m_typeRegistry
= NULL
;
4856 m_winCapture
= NULL
;
4858 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
4859 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
4862 m_attrCache
.row
= -1;
4863 m_attrCache
.col
= -1;
4864 m_attrCache
.attr
= NULL
;
4866 m_labelFont
= GetFont();
4867 m_labelFont
.SetWeight( wxBOLD
);
4869 m_rowLabelHorizAlign
= wxALIGN_CENTRE
;
4870 m_rowLabelVertAlign
= wxALIGN_CENTRE
;
4872 m_colLabelHorizAlign
= wxALIGN_CENTRE
;
4873 m_colLabelVertAlign
= wxALIGN_CENTRE
;
4874 m_colLabelTextOrientation
= wxHORIZONTAL
;
4876 m_defaultColWidth
= WXGRID_DEFAULT_COL_WIDTH
;
4877 m_defaultRowHeight
= 0; // this will be initialized after creation
4879 m_minAcceptableColWidth
= WXGRID_MIN_COL_WIDTH
;
4880 m_minAcceptableRowHeight
= WXGRID_MIN_ROW_HEIGHT
;
4882 m_gridLineColour
= wxColour( 192,192,192 );
4883 m_gridLinesEnabled
= true;
4884 m_gridLinesClipHorz
=
4885 m_gridLinesClipVert
= true;
4886 m_cellHighlightColour
= *wxBLACK
;
4887 m_cellHighlightPenWidth
= 2;
4888 m_cellHighlightROPenWidth
= 1;
4890 m_canDragColMove
= false;
4892 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
4893 m_winCapture
= NULL
;
4894 m_canDragRowSize
= true;
4895 m_canDragColSize
= true;
4896 m_canDragGridSize
= true;
4897 m_canDragCell
= false;
4899 m_dragRowOrCol
= -1;
4900 m_isDragging
= false;
4901 m_startDragPos
= wxDefaultPosition
;
4903 m_sortCol
= wxNOT_FOUND
;
4904 m_sortIsAscending
= true;
4907 m_nativeColumnLabels
= false;
4909 m_waitForSlowClick
= false;
4911 m_rowResizeCursor
= wxCursor( wxCURSOR_SIZENS
);
4912 m_colResizeCursor
= wxCursor( wxCURSOR_SIZEWE
);
4914 m_currentCellCoords
= wxGridNoCellCoords
;
4916 m_selectedBlockTopLeft
=
4917 m_selectedBlockBottomRight
=
4918 m_selectedBlockCorner
= wxGridNoCellCoords
;
4920 m_selectionBackground
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
);
4921 m_selectionForeground
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
4923 m_editable
= true; // default for whole grid
4925 m_inOnKeyDown
= false;
4931 m_scrollLineX
= GRID_SCROLL_LINE_X
;
4932 m_scrollLineY
= GRID_SCROLL_LINE_Y
;
4935 // ----------------------------------------------------------------------------
4936 // the idea is to call these functions only when necessary because they create
4937 // quite big arrays which eat memory mostly unnecessary - in particular, if
4938 // default widths/heights are used for all rows/columns, we may not use these
4941 // with some extra code, it should be possible to only store the widths/heights
4942 // different from default ones (resulting in space savings for huge grids) but
4943 // this is not done currently
4944 // ----------------------------------------------------------------------------
4946 void wxGrid::InitRowHeights()
4948 m_rowHeights
.Empty();
4949 m_rowBottoms
.Empty();
4951 m_rowHeights
.Alloc( m_numRows
);
4952 m_rowBottoms
.Alloc( m_numRows
);
4954 m_rowHeights
.Add( m_defaultRowHeight
, m_numRows
);
4957 for ( int i
= 0; i
< m_numRows
; i
++ )
4959 rowBottom
+= m_defaultRowHeight
;
4960 m_rowBottoms
.Add( rowBottom
);
4964 void wxGrid::InitColWidths()
4966 m_colWidths
.Empty();
4967 m_colRights
.Empty();
4969 m_colWidths
.Alloc( m_numCols
);
4970 m_colRights
.Alloc( m_numCols
);
4972 m_colWidths
.Add( m_defaultColWidth
, m_numCols
);
4974 for ( int i
= 0; i
< m_numCols
; i
++ )
4976 int colRight
= ( GetColPos( i
) + 1 ) * m_defaultColWidth
;
4977 m_colRights
.Add( colRight
);
4981 int wxGrid::GetColWidth(int col
) const
4983 return m_colWidths
.IsEmpty() ? m_defaultColWidth
: m_colWidths
[col
];
4986 int wxGrid::GetColLeft(int col
) const
4988 return m_colRights
.IsEmpty() ? GetColPos( col
) * m_defaultColWidth
4989 : m_colRights
[col
] - m_colWidths
[col
];
4992 int wxGrid::GetColRight(int col
) const
4994 return m_colRights
.IsEmpty() ? (GetColPos( col
) + 1) * m_defaultColWidth
4998 int wxGrid::GetRowHeight(int row
) const
5000 return m_rowHeights
.IsEmpty() ? m_defaultRowHeight
: m_rowHeights
[row
];
5003 int wxGrid::GetRowTop(int row
) const
5005 return m_rowBottoms
.IsEmpty() ? row
* m_defaultRowHeight
5006 : m_rowBottoms
[row
] - m_rowHeights
[row
];
5009 int wxGrid::GetRowBottom(int row
) const
5011 return m_rowBottoms
.IsEmpty() ? (row
+ 1) * m_defaultRowHeight
5012 : m_rowBottoms
[row
];
5015 void wxGrid::CalcDimensions()
5017 // compute the size of the scrollable area
5018 int w
= m_numCols
> 0 ? GetColRight(GetColAt(m_numCols
- 1)) : 0;
5019 int h
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) : 0;
5024 // take into account editor if shown
5025 if ( IsCellEditControlShown() )
5028 int r
= m_currentCellCoords
.GetRow();
5029 int c
= m_currentCellCoords
.GetCol();
5030 int x
= GetColLeft(c
);
5031 int y
= GetRowTop(r
);
5033 // how big is the editor
5034 wxGridCellAttr
* attr
= GetCellAttr(r
, c
);
5035 wxGridCellEditor
* editor
= attr
->GetEditor(this, r
, c
);
5036 editor
->GetControl()->GetSize(&w2
, &h2
);
5047 // preserve (more or less) the previous position
5049 GetViewStart( &x
, &y
);
5051 // ensure the position is valid for the new scroll ranges
5053 x
= wxMax( w
- 1, 0 );
5055 y
= wxMax( h
- 1, 0 );
5057 // update the virtual size and refresh the scrollbars to reflect it
5058 m_gridWin
->SetVirtualSize(w
, h
);
5062 // if our OnSize() hadn't been called (it would if we have scrollbars), we
5063 // still must reposition the children
5067 wxSize
wxGrid::GetSizeAvailableForScrollTarget(const wxSize
& size
)
5069 wxSize
sizeGridWin(size
);
5070 sizeGridWin
.x
-= m_rowLabelWidth
;
5071 sizeGridWin
.y
-= m_colLabelHeight
;
5076 void wxGrid::CalcWindowSizes()
5078 // escape if the window is has not been fully created yet
5080 if ( m_cornerLabelWin
== NULL
)
5084 GetClientSize( &cw
, &ch
);
5086 // the grid may be too small to have enough space for the labels yet, don't
5087 // size the windows to negative sizes in this case
5088 int gw
= cw
- m_rowLabelWidth
;
5089 int gh
= ch
- m_colLabelHeight
;
5095 if ( m_cornerLabelWin
&& m_cornerLabelWin
->IsShown() )
5096 m_cornerLabelWin
->SetSize( 0, 0, m_rowLabelWidth
, m_colLabelHeight
);
5098 if ( m_colWindow
&& m_colWindow
->IsShown() )
5099 m_colWindow
->SetSize( m_rowLabelWidth
, 0, gw
, m_colLabelHeight
);
5101 if ( m_rowLabelWin
&& m_rowLabelWin
->IsShown() )
5102 m_rowLabelWin
->SetSize( 0, m_colLabelHeight
, m_rowLabelWidth
, gh
);
5104 if ( m_gridWin
&& m_gridWin
->IsShown() )
5105 m_gridWin
->SetSize( m_rowLabelWidth
, m_colLabelHeight
, gw
, gh
);
5108 // this is called when the grid table sends a message
5109 // to indicate that it has been redimensioned
5111 bool wxGrid::Redimension( wxGridTableMessage
& msg
)
5114 bool result
= false;
5116 // Clear the attribute cache as the attribute might refer to a different
5117 // cell than stored in the cache after adding/removing rows/columns.
5120 // By the same reasoning, the editor should be dismissed if columns are
5121 // added or removed. And for consistency, it should IMHO always be
5122 // removed, not only if the cell "underneath" it actually changes.
5123 // For now, I intentionally do not save the editor's content as the
5124 // cell it might want to save that stuff to might no longer exist.
5125 HideCellEditControl();
5128 // if we were using the default widths/heights so far, we must change them
5130 if ( m_colWidths
.IsEmpty() )
5135 if ( m_rowHeights
.IsEmpty() )
5141 switch ( msg
.GetId() )
5143 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
5145 size_t pos
= msg
.GetCommandInt();
5146 int numRows
= msg
.GetCommandInt2();
5148 m_numRows
+= numRows
;
5150 if ( !m_rowHeights
.IsEmpty() )
5152 m_rowHeights
.Insert( m_defaultRowHeight
, pos
, numRows
);
5153 m_rowBottoms
.Insert( 0, pos
, numRows
);
5157 bottom
= m_rowBottoms
[pos
- 1];
5159 for ( i
= pos
; i
< m_numRows
; i
++ )
5161 bottom
+= m_rowHeights
[i
];
5162 m_rowBottoms
[i
] = bottom
;
5166 if ( m_currentCellCoords
== wxGridNoCellCoords
)
5168 // if we have just inserted cols into an empty grid the current
5169 // cell will be undefined...
5171 SetCurrentCell( 0, 0 );
5175 m_selection
->UpdateRows( pos
, numRows
);
5176 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
5178 attrProvider
->UpdateAttrRows( pos
, numRows
);
5180 if ( !GetBatchCount() )
5183 m_rowLabelWin
->Refresh();
5189 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
5191 int numRows
= msg
.GetCommandInt();
5192 int oldNumRows
= m_numRows
;
5193 m_numRows
+= numRows
;
5195 if ( !m_rowHeights
.IsEmpty() )
5197 m_rowHeights
.Add( m_defaultRowHeight
, numRows
);
5198 m_rowBottoms
.Add( 0, numRows
);
5201 if ( oldNumRows
> 0 )
5202 bottom
= m_rowBottoms
[oldNumRows
- 1];
5204 for ( i
= oldNumRows
; i
< m_numRows
; i
++ )
5206 bottom
+= m_rowHeights
[i
];
5207 m_rowBottoms
[i
] = bottom
;
5211 if ( m_currentCellCoords
== wxGridNoCellCoords
)
5213 // if we have just inserted cols into an empty grid the current
5214 // cell will be undefined...
5216 SetCurrentCell( 0, 0 );
5219 if ( !GetBatchCount() )
5222 m_rowLabelWin
->Refresh();
5228 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
5230 size_t pos
= msg
.GetCommandInt();
5231 int numRows
= msg
.GetCommandInt2();
5232 m_numRows
-= numRows
;
5234 if ( !m_rowHeights
.IsEmpty() )
5236 m_rowHeights
.RemoveAt( pos
, numRows
);
5237 m_rowBottoms
.RemoveAt( pos
, numRows
);
5240 for ( i
= 0; i
< m_numRows
; i
++ )
5242 h
+= m_rowHeights
[i
];
5243 m_rowBottoms
[i
] = h
;
5249 m_currentCellCoords
= wxGridNoCellCoords
;
5253 if ( m_currentCellCoords
.GetRow() >= m_numRows
)
5254 m_currentCellCoords
.Set( 0, 0 );
5258 m_selection
->UpdateRows( pos
, -((int)numRows
) );
5259 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
5262 attrProvider
->UpdateAttrRows( pos
, -((int)numRows
) );
5264 // ifdef'd out following patch from Paul Gammans
5266 // No need to touch column attributes, unless we
5267 // removed _all_ rows, in this case, we remove
5268 // all column attributes.
5269 // I hate to do this here, but the
5270 // needed data is not available inside UpdateAttrRows.
5271 if ( !GetNumberRows() )
5272 attrProvider
->UpdateAttrCols( 0, -GetNumberCols() );
5276 if ( !GetBatchCount() )
5279 m_rowLabelWin
->Refresh();
5285 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
5287 size_t pos
= msg
.GetCommandInt();
5288 int numCols
= msg
.GetCommandInt2();
5289 m_numCols
+= numCols
;
5291 if ( m_useNativeHeader
)
5292 GetColHeader()->SetColumnCount(m_numCols
);
5294 if ( !m_colAt
.IsEmpty() )
5296 //Shift the column IDs
5298 for ( i
= 0; i
< m_numCols
- numCols
; i
++ )
5300 if ( m_colAt
[i
] >= (int)pos
)
5301 m_colAt
[i
] += numCols
;
5304 m_colAt
.Insert( pos
, pos
, numCols
);
5306 //Set the new columns' positions
5307 for ( i
= pos
+ 1; i
< (int)pos
+ numCols
; i
++ )
5313 if ( !m_colWidths
.IsEmpty() )
5315 m_colWidths
.Insert( m_defaultColWidth
, pos
, numCols
);
5316 m_colRights
.Insert( 0, pos
, numCols
);
5320 right
= m_colRights
[GetColAt( pos
- 1 )];
5323 for ( colPos
= pos
; colPos
< m_numCols
; colPos
++ )
5325 i
= GetColAt( colPos
);
5327 right
+= m_colWidths
[i
];
5328 m_colRights
[i
] = right
;
5332 if ( m_currentCellCoords
== wxGridNoCellCoords
)
5334 // if we have just inserted cols into an empty grid the current
5335 // cell will be undefined...
5337 SetCurrentCell( 0, 0 );
5341 m_selection
->UpdateCols( pos
, numCols
);
5342 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
5344 attrProvider
->UpdateAttrCols( pos
, numCols
);
5345 if ( !GetBatchCount() )
5348 m_colWindow
->Refresh();
5354 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
5356 int numCols
= msg
.GetCommandInt();
5357 int oldNumCols
= m_numCols
;
5358 m_numCols
+= numCols
;
5359 if ( m_useNativeHeader
)
5360 GetColHeader()->SetColumnCount(m_numCols
);
5362 if ( !m_colAt
.IsEmpty() )
5364 m_colAt
.Add( 0, numCols
);
5366 //Set the new columns' positions
5368 for ( i
= oldNumCols
; i
< m_numCols
; i
++ )
5374 if ( !m_colWidths
.IsEmpty() )
5376 m_colWidths
.Add( m_defaultColWidth
, numCols
);
5377 m_colRights
.Add( 0, numCols
);
5380 if ( oldNumCols
> 0 )
5381 right
= m_colRights
[GetColAt( oldNumCols
- 1 )];
5384 for ( colPos
= oldNumCols
; colPos
< m_numCols
; colPos
++ )
5386 i
= GetColAt( colPos
);
5388 right
+= m_colWidths
[i
];
5389 m_colRights
[i
] = right
;
5393 if ( m_currentCellCoords
== wxGridNoCellCoords
)
5395 // if we have just inserted cols into an empty grid the current
5396 // cell will be undefined...
5398 SetCurrentCell( 0, 0 );
5400 if ( !GetBatchCount() )
5403 m_colWindow
->Refresh();
5409 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
5411 size_t pos
= msg
.GetCommandInt();
5412 int numCols
= msg
.GetCommandInt2();
5413 m_numCols
-= numCols
;
5414 if ( m_useNativeHeader
)
5415 GetColHeader()->SetColumnCount(m_numCols
);
5417 if ( !m_colAt
.IsEmpty() )
5419 int colID
= GetColAt( pos
);
5421 m_colAt
.RemoveAt( pos
, numCols
);
5423 //Shift the column IDs
5425 for ( colPos
= 0; colPos
< m_numCols
; colPos
++ )
5427 if ( m_colAt
[colPos
] > colID
)
5428 m_colAt
[colPos
] -= numCols
;
5432 if ( !m_colWidths
.IsEmpty() )
5434 m_colWidths
.RemoveAt( pos
, numCols
);
5435 m_colRights
.RemoveAt( pos
, numCols
);
5439 for ( colPos
= 0; colPos
< m_numCols
; colPos
++ )
5441 i
= GetColAt( colPos
);
5443 w
+= m_colWidths
[i
];
5450 m_currentCellCoords
= wxGridNoCellCoords
;
5454 if ( m_currentCellCoords
.GetCol() >= m_numCols
)
5455 m_currentCellCoords
.Set( 0, 0 );
5459 m_selection
->UpdateCols( pos
, -((int)numCols
) );
5460 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
5463 attrProvider
->UpdateAttrCols( pos
, -((int)numCols
) );
5465 // ifdef'd out following patch from Paul Gammans
5467 // No need to touch row attributes, unless we
5468 // removed _all_ columns, in this case, we remove
5469 // all row attributes.
5470 // I hate to do this here, but the
5471 // needed data is not available inside UpdateAttrCols.
5472 if ( !GetNumberCols() )
5473 attrProvider
->UpdateAttrRows( 0, -GetNumberRows() );
5477 if ( !GetBatchCount() )
5480 m_colWindow
->Refresh();
5487 if (result
&& !GetBatchCount() )
5488 m_gridWin
->Refresh();
5493 wxArrayInt
wxGrid::CalcRowLabelsExposed( const wxRegion
& reg
) const
5495 wxRegionIterator
iter( reg
);
5498 wxArrayInt rowlabels
;
5505 // TODO: remove this when we can...
5506 // There is a bug in wxMotif that gives garbage update
5507 // rectangles if you jump-scroll a long way by clicking the
5508 // scrollbar with middle button. This is a work-around
5510 #if defined(__WXMOTIF__)
5512 m_gridWin
->GetClientSize( &cw
, &ch
);
5513 if ( r
.GetTop() > ch
)
5515 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
5518 // logical bounds of update region
5521 CalcUnscrolledPosition( 0, r
.GetTop(), &dummy
, &top
);
5522 CalcUnscrolledPosition( 0, r
.GetBottom(), &dummy
, &bottom
);
5524 // find the row labels within these bounds
5527 for ( row
= internalYToRow(top
); row
< m_numRows
; row
++ )
5529 if ( GetRowBottom(row
) < top
)
5532 if ( GetRowTop(row
) > bottom
)
5535 rowlabels
.Add( row
);
5544 wxArrayInt
wxGrid::CalcColLabelsExposed( const wxRegion
& reg
) const
5546 wxRegionIterator
iter( reg
);
5549 wxArrayInt colLabels
;
5556 // TODO: remove this when we can...
5557 // There is a bug in wxMotif that gives garbage update
5558 // rectangles if you jump-scroll a long way by clicking the
5559 // scrollbar with middle button. This is a work-around
5561 #if defined(__WXMOTIF__)
5563 m_gridWin
->GetClientSize( &cw
, &ch
);
5564 if ( r
.GetLeft() > cw
)
5566 r
.SetRight( wxMin( r
.GetRight(), cw
) );
5569 // logical bounds of update region
5572 CalcUnscrolledPosition( r
.GetLeft(), 0, &left
, &dummy
);
5573 CalcUnscrolledPosition( r
.GetRight(), 0, &right
, &dummy
);
5575 // find the cells within these bounds
5579 for ( colPos
= GetColPos( internalXToCol(left
) ); colPos
< m_numCols
; colPos
++ )
5581 col
= GetColAt( colPos
);
5583 if ( GetColRight(col
) < left
)
5586 if ( GetColLeft(col
) > right
)
5589 colLabels
.Add( col
);
5598 wxGridCellCoordsArray
wxGrid::CalcCellsExposed( const wxRegion
& reg
) const
5600 wxRegionIterator
iter( reg
);
5603 wxGridCellCoordsArray cellsExposed
;
5605 int left
, top
, right
, bottom
;
5610 // TODO: remove this when we can...
5611 // There is a bug in wxMotif that gives garbage update
5612 // rectangles if you jump-scroll a long way by clicking the
5613 // scrollbar with middle button. This is a work-around
5615 #if defined(__WXMOTIF__)
5617 m_gridWin
->GetClientSize( &cw
, &ch
);
5618 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
5619 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
5620 r
.SetRight( wxMin( r
.GetRight(), cw
) );
5621 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
5624 // logical bounds of update region
5626 CalcUnscrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
5627 CalcUnscrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
5629 // find the cells within these bounds
5631 for ( int row
= internalYToRow(top
); row
< m_numRows
; row
++ )
5633 if ( GetRowBottom(row
) <= top
)
5636 if ( GetRowTop(row
) > bottom
)
5639 // add all dirty cells in this row: notice that the columns which
5640 // are dirty don't depend on the row so we compute them only once
5641 // for the first dirty row and then reuse for all the next ones
5644 // do determine the dirty columns
5645 for ( int pos
= XToPos(left
); pos
<= XToPos(right
); pos
++ )
5646 cols
.push_back(GetColAt(pos
));
5648 // if there are no dirty columns at all, nothing to do
5653 const size_t count
= cols
.size();
5654 for ( size_t n
= 0; n
< count
; n
++ )
5655 cellsExposed
.Add(wxGridCellCoords(row
, cols
[n
]));
5661 return cellsExposed
;
5665 void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent
& event
)
5668 wxPoint
pos( event
.GetPosition() );
5669 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
5671 if ( event
.Dragging() )
5675 m_isDragging
= true;
5676 m_rowLabelWin
->CaptureMouse();
5679 if ( event
.LeftIsDown() )
5681 switch ( m_cursorMode
)
5683 case WXGRID_CURSOR_RESIZE_ROW
:
5685 int cw
, ch
, left
, dummy
;
5686 m_gridWin
->GetClientSize( &cw
, &ch
);
5687 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
5689 wxClientDC
dc( m_gridWin
);
5692 GetRowTop(m_dragRowOrCol
) +
5693 GetRowMinimalHeight(m_dragRowOrCol
) );
5694 dc
.SetLogicalFunction(wxINVERT
);
5695 if ( m_dragLastPos
>= 0 )
5697 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
5699 dc
.DrawLine( left
, y
, left
+cw
, y
);
5704 case WXGRID_CURSOR_SELECT_ROW
:
5706 if ( (row
= YToRow( y
)) >= 0 )
5709 m_selection
->SelectRow(row
, event
);
5714 // default label to suppress warnings about "enumeration value
5715 // 'xxx' not handled in switch
5723 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
5728 if (m_rowLabelWin
->HasCapture())
5729 m_rowLabelWin
->ReleaseMouse();
5730 m_isDragging
= false;
5733 // ------------ Entering or leaving the window
5735 if ( event
.Entering() || event
.Leaving() )
5737 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
5740 // ------------ Left button pressed
5742 else if ( event
.LeftDown() )
5744 // don't send a label click event for a hit on the
5745 // edge of the row label - this is probably the user
5746 // wanting to resize the row
5748 if ( YToEdgeOfRow(y
) < 0 )
5752 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, row
, -1, event
) )
5754 if ( !event
.ShiftDown() && !event
.CmdDown() )
5758 if ( event
.ShiftDown() )
5760 m_selection
->SelectBlock
5762 m_currentCellCoords
.GetRow(), 0,
5763 row
, GetNumberCols() - 1,
5769 m_selection
->SelectRow(row
, event
);
5773 ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW
, m_rowLabelWin
);
5778 // starting to drag-resize a row
5779 if ( CanDragRowSize() )
5780 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
);
5784 // ------------ Left double click
5786 else if (event
.LeftDClick() )
5788 row
= YToEdgeOfRow(y
);
5793 !SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, row
, -1, event
) )
5795 // no default action at the moment
5800 // adjust row height depending on label text
5801 AutoSizeRowLabelSize( row
);
5803 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, GetColLabelWindow());
5808 // ------------ Left button released
5810 else if ( event
.LeftUp() )
5812 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
5814 DoEndDragResizeRow();
5816 // Note: we are ending the event *after* doing
5817 // default processing in this case
5819 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
5822 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
5826 // ------------ Right button down
5828 else if ( event
.RightDown() )
5832 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, row
, -1, event
) )
5834 // no default action at the moment
5838 // ------------ Right double click
5840 else if ( event
.RightDClick() )
5844 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, row
, -1, event
) )
5846 // no default action at the moment
5850 // ------------ No buttons down and mouse moving
5852 else if ( event
.Moving() )
5854 m_dragRowOrCol
= YToEdgeOfRow( y
);
5855 if ( m_dragRowOrCol
>= 0 )
5857 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5859 // don't capture the mouse yet
5860 if ( CanDragRowSize() )
5861 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
, false);
5864 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5866 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
, false);
5871 void wxGrid::UpdateColumnSortingIndicator(int col
)
5873 wxCHECK_RET( col
!= wxNOT_FOUND
, "invalid column index" );
5875 if ( m_useNativeHeader
)
5876 GetColHeader()->UpdateColumn(col
);
5877 else if ( m_nativeColumnLabels
)
5878 m_colWindow
->Refresh();
5879 //else: sorting indicator display not yet implemented in grid version
5882 void wxGrid::SetSortingColumn(int col
, bool ascending
)
5884 if ( col
== m_sortCol
)
5886 // we are already using this column for sorting (or not sorting at all)
5887 // but we might still change the sorting order, check for it
5888 if ( m_sortCol
!= wxNOT_FOUND
&& ascending
!= m_sortIsAscending
)
5890 m_sortIsAscending
= ascending
;
5892 UpdateColumnSortingIndicator(m_sortCol
);
5895 else // we're changing the column used for sorting
5897 const int sortColOld
= m_sortCol
;
5899 // change it before updating the column as we want GetSortingColumn()
5900 // to return the correct new value
5903 if ( sortColOld
!= wxNOT_FOUND
)
5904 UpdateColumnSortingIndicator(sortColOld
);
5906 if ( m_sortCol
!= wxNOT_FOUND
)
5908 m_sortIsAscending
= ascending
;
5909 UpdateColumnSortingIndicator(m_sortCol
);
5914 void wxGrid::DoColHeaderClick(int col
)
5916 // we consider that the grid was resorted if this event is processed and
5918 if ( SendEvent(wxEVT_GRID_COL_SORT
, -1, col
) == 1 )
5920 SetSortingColumn(col
, IsSortingBy(col
) ? !m_sortIsAscending
: true);
5925 void wxGrid::DoStartResizeCol(int col
)
5927 m_dragRowOrCol
= col
;
5929 DoUpdateResizeColWidth(GetColWidth(m_dragRowOrCol
));
5932 void wxGrid::DoUpdateResizeCol(int x
)
5934 int cw
, ch
, dummy
, top
;
5935 m_gridWin
->GetClientSize( &cw
, &ch
);
5936 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
5938 wxClientDC
dc( m_gridWin
);
5941 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) + GetColMinimalWidth(m_dragRowOrCol
));
5942 dc
.SetLogicalFunction(wxINVERT
);
5943 if ( m_dragLastPos
>= 0 )
5945 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ ch
);
5947 dc
.DrawLine( x
, top
, x
, top
+ ch
);
5951 void wxGrid::DoUpdateResizeColWidth(int w
)
5953 DoUpdateResizeCol(GetColLeft(m_dragRowOrCol
) + w
);
5956 void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent
& event
)
5959 wxPoint
pos( event
.GetPosition() );
5960 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
5962 int col
= XToCol(x
);
5963 if ( event
.Dragging() )
5967 m_isDragging
= true;
5968 GetColLabelWindow()->CaptureMouse();
5970 if ( m_cursorMode
== WXGRID_CURSOR_MOVE_COL
&& col
!= -1 )
5971 DoStartMoveCol(col
);
5974 if ( event
.LeftIsDown() )
5976 switch ( m_cursorMode
)
5978 case WXGRID_CURSOR_RESIZE_COL
:
5979 DoUpdateResizeCol(x
);
5982 case WXGRID_CURSOR_SELECT_COL
:
5987 m_selection
->SelectCol(col
, event
);
5992 case WXGRID_CURSOR_MOVE_COL
:
5994 int posNew
= XToPos(x
);
5995 int colNew
= GetColAt(posNew
);
5997 // determine the position of the drop marker
5999 if ( x
>= GetColLeft(colNew
) + (GetColWidth(colNew
) / 2) )
6000 markerX
= GetColRight(colNew
);
6002 markerX
= GetColLeft(colNew
);
6004 if ( markerX
!= m_dragLastPos
)
6006 wxClientDC
dc( GetColLabelWindow() );
6010 GetColLabelWindow()->GetClientSize( &cw
, &ch
);
6014 //Clean up the last indicator
6015 if ( m_dragLastPos
>= 0 )
6017 wxPen
pen( GetColLabelWindow()->GetBackgroundColour(), 2 );
6019 dc
.DrawLine( m_dragLastPos
+ 1, 0, m_dragLastPos
+ 1, ch
);
6020 dc
.SetPen(wxNullPen
);
6022 if ( XToCol( m_dragLastPos
) != -1 )
6023 DrawColLabel( dc
, XToCol( m_dragLastPos
) );
6026 const wxColour
*color
;
6027 //Moving to the same place? Don't draw a marker
6028 if ( colNew
== m_dragRowOrCol
)
6029 color
= wxLIGHT_GREY
;
6034 wxPen
pen( *color
, 2 );
6037 dc
.DrawLine( markerX
, 0, markerX
, ch
);
6039 dc
.SetPen(wxNullPen
);
6041 m_dragLastPos
= markerX
- 1;
6046 // default label to suppress warnings about "enumeration value
6047 // 'xxx' not handled in switch
6055 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
6060 if (GetColLabelWindow()->HasCapture())
6061 GetColLabelWindow()->ReleaseMouse();
6062 m_isDragging
= false;
6065 // ------------ Entering or leaving the window
6067 if ( event
.Entering() || event
.Leaving() )
6069 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, GetColLabelWindow());
6072 // ------------ Left button pressed
6074 else if ( event
.LeftDown() )
6076 // don't send a label click event for a hit on the
6077 // edge of the col label - this is probably the user
6078 // wanting to resize the col
6080 if ( XToEdgeOfCol(x
) < 0 )
6083 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, col
, event
) )
6085 if ( m_canDragColMove
)
6087 //Show button as pressed
6088 wxClientDC
dc( GetColLabelWindow() );
6089 int colLeft
= GetColLeft( col
);
6090 int colRight
= GetColRight( col
) - 1;
6091 dc
.SetPen( wxPen( GetColLabelWindow()->GetBackgroundColour(), 1 ) );
6092 dc
.DrawLine( colLeft
, 1, colLeft
, m_colLabelHeight
-1 );
6093 dc
.DrawLine( colLeft
, 1, colRight
, 1 );
6095 ChangeCursorMode(WXGRID_CURSOR_MOVE_COL
, GetColLabelWindow());
6099 if ( !event
.ShiftDown() && !event
.CmdDown() )
6103 if ( event
.ShiftDown() )
6105 m_selection
->SelectBlock
6107 0, m_currentCellCoords
.GetCol(),
6108 GetNumberRows() - 1, col
,
6114 m_selection
->SelectCol(col
, event
);
6118 ChangeCursorMode(WXGRID_CURSOR_SELECT_COL
, GetColLabelWindow());
6124 // starting to drag-resize a col
6126 if ( CanDragColSize() )
6127 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, GetColLabelWindow());
6131 // ------------ Left double click
6133 if ( event
.LeftDClick() )
6135 const int colEdge
= XToEdgeOfCol(x
);
6136 if ( colEdge
== -1 )
6139 ! SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, col
, event
) )
6141 // no default action at the moment
6146 // adjust column width depending on label text
6147 AutoSizeColLabelSize( colEdge
);
6149 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, GetColLabelWindow());
6154 // ------------ Left button released
6156 else if ( event
.LeftUp() )
6158 switch ( m_cursorMode
)
6160 case WXGRID_CURSOR_RESIZE_COL
:
6161 DoEndDragResizeCol();
6164 case WXGRID_CURSOR_MOVE_COL
:
6165 if ( m_dragLastPos
== -1 || col
== m_dragRowOrCol
)
6167 // the column didn't actually move anywhere
6169 DoColHeaderClick(col
);
6170 m_colWindow
->Refresh(); // "unpress" the column
6174 DoEndMoveCol(XToPos(x
));
6178 case WXGRID_CURSOR_SELECT_COL
:
6179 case WXGRID_CURSOR_SELECT_CELL
:
6180 case WXGRID_CURSOR_RESIZE_ROW
:
6181 case WXGRID_CURSOR_SELECT_ROW
:
6183 DoColHeaderClick(col
);
6187 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, GetColLabelWindow());
6191 // ------------ Right button down
6193 else if ( event
.RightDown() )
6196 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, col
, event
) )
6198 // no default action at the moment
6202 // ------------ Right double click
6204 else if ( event
.RightDClick() )
6207 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, col
, event
) )
6209 // no default action at the moment
6213 // ------------ No buttons down and mouse moving
6215 else if ( event
.Moving() )
6217 m_dragRowOrCol
= XToEdgeOfCol( x
);
6218 if ( m_dragRowOrCol
>= 0 )
6220 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
6222 // don't capture the cursor yet
6223 if ( CanDragColSize() )
6224 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, GetColLabelWindow(), false);
6227 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
6229 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, GetColLabelWindow(), false);
6234 void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent
& event
)
6236 if ( event
.LeftDown() )
6238 // indicate corner label by having both row and
6241 if ( !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, -1, event
) )
6246 else if ( event
.LeftDClick() )
6248 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, -1, event
);
6250 else if ( event
.RightDown() )
6252 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, -1, event
) )
6254 // no default action at the moment
6257 else if ( event
.RightDClick() )
6259 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, -1, event
) )
6261 // no default action at the moment
6266 void wxGrid::CancelMouseCapture()
6268 // cancel operation currently in progress, whatever it is
6271 m_isDragging
= false;
6272 m_startDragPos
= wxDefaultPosition
;
6274 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
6275 m_winCapture
->SetCursor( *wxSTANDARD_CURSOR
);
6276 m_winCapture
= NULL
;
6278 // remove traces of whatever we drew on screen
6283 void wxGrid::ChangeCursorMode(CursorMode mode
,
6288 static const wxChar
*cursorModes
[] =
6298 wxLogTrace(_T("grid"),
6299 _T("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
6300 win
== m_colWindow
? _T("colLabelWin")
6301 : win
? _T("rowLabelWin")
6303 cursorModes
[m_cursorMode
], cursorModes
[mode
]);
6306 if ( mode
== m_cursorMode
&&
6307 win
== m_winCapture
&&
6308 captureMouse
== (m_winCapture
!= NULL
))
6313 // by default use the grid itself
6319 m_winCapture
->ReleaseMouse();
6320 m_winCapture
= NULL
;
6323 m_cursorMode
= mode
;
6325 switch ( m_cursorMode
)
6327 case WXGRID_CURSOR_RESIZE_ROW
:
6328 win
->SetCursor( m_rowResizeCursor
);
6331 case WXGRID_CURSOR_RESIZE_COL
:
6332 win
->SetCursor( m_colResizeCursor
);
6335 case WXGRID_CURSOR_MOVE_COL
:
6336 win
->SetCursor( wxCursor(wxCURSOR_HAND
) );
6340 win
->SetCursor( *wxSTANDARD_CURSOR
);
6344 // we need to capture mouse when resizing
6345 bool resize
= m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
||
6346 m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
;
6348 if ( captureMouse
&& resize
)
6350 win
->CaptureMouse();
6355 // ----------------------------------------------------------------------------
6356 // grid mouse event processing
6357 // ----------------------------------------------------------------------------
6360 wxGrid::DoGridCellDrag(wxMouseEvent
& event
,
6361 const wxGridCellCoords
& coords
,
6364 if ( coords
== wxGridNoCellCoords
)
6365 return; // we're outside any valid cell
6367 // Hide the edit control, so it won't interfere with drag-shrinking.
6368 if ( IsCellEditControlShown() )
6370 HideCellEditControl();
6371 SaveEditControlValue();
6374 switch ( event
.GetModifiers() )
6377 if ( m_selectedBlockCorner
== wxGridNoCellCoords
)
6378 m_selectedBlockCorner
= coords
;
6379 UpdateBlockBeingSelected(m_selectedBlockCorner
, coords
);
6383 if ( CanDragCell() )
6387 if ( m_selectedBlockCorner
== wxGridNoCellCoords
)
6388 m_selectedBlockCorner
= coords
;
6390 SendEvent(wxEVT_GRID_CELL_BEGIN_DRAG
, coords
, event
);
6395 UpdateBlockBeingSelected(m_currentCellCoords
, coords
);
6399 // we don't handle the other key modifiers
6404 void wxGrid::DoGridLineDrag(wxMouseEvent
& event
, const wxGridOperations
& oper
)
6406 wxClientDC
dc(m_gridWin
);
6408 dc
.SetLogicalFunction(wxINVERT
);
6410 const wxRect
rectWin(CalcUnscrolledPosition(wxPoint(0, 0)),
6411 m_gridWin
->GetClientSize());
6413 // erase the previously drawn line, if any
6414 if ( m_dragLastPos
>= 0 )
6415 oper
.DrawParallelLineInRect(dc
, rectWin
, m_dragLastPos
);
6417 // we need the vertical position for rows and horizontal for columns here
6418 m_dragLastPos
= oper
.Dual().Select(CalcUnscrolledPosition(event
.GetPosition()));
6420 // don't allow resizing beneath the minimal size
6421 const int posMin
= oper
.GetLineStartPos(this, m_dragRowOrCol
) +
6422 oper
.GetMinimalLineSize(this, m_dragRowOrCol
);
6423 if ( m_dragLastPos
< posMin
)
6424 m_dragLastPos
= posMin
;
6426 // and draw it at the new position
6427 oper
.DrawParallelLineInRect(dc
, rectWin
, m_dragLastPos
);
6430 void wxGrid::DoGridDragEvent(wxMouseEvent
& event
, const wxGridCellCoords
& coords
)
6432 if ( !m_isDragging
)
6434 // Don't start doing anything until the mouse has been dragged far
6436 const wxPoint
& pt
= event
.GetPosition();
6437 if ( m_startDragPos
== wxDefaultPosition
)
6439 m_startDragPos
= pt
;
6443 if ( abs(m_startDragPos
.x
- pt
.x
) <= DRAG_SENSITIVITY
&&
6444 abs(m_startDragPos
.y
- pt
.y
) <= DRAG_SENSITIVITY
)
6448 const bool isFirstDrag
= !m_isDragging
;
6449 m_isDragging
= true;
6451 switch ( m_cursorMode
)
6453 case WXGRID_CURSOR_SELECT_CELL
:
6454 DoGridCellDrag(event
, coords
, isFirstDrag
);
6457 case WXGRID_CURSOR_RESIZE_ROW
:
6458 DoGridLineDrag(event
, wxGridRowOperations());
6461 case WXGRID_CURSOR_RESIZE_COL
:
6462 DoGridLineDrag(event
, wxGridColumnOperations());
6471 m_winCapture
= m_gridWin
;
6472 m_winCapture
->CaptureMouse();
6477 wxGrid::DoGridCellLeftDown(wxMouseEvent
& event
,
6478 const wxGridCellCoords
& coords
,
6481 if ( SendEvent(wxEVT_GRID_CELL_LEFT_CLICK
, coords
, event
) )
6483 // event handled by user code, no need to do anything here
6487 if ( !event
.CmdDown() )
6490 if ( event
.ShiftDown() )
6494 m_selection
->SelectBlock(m_currentCellCoords
, coords
, event
);
6495 m_selectedBlockCorner
= coords
;
6498 else if ( XToEdgeOfCol(pos
.x
) < 0 && YToEdgeOfRow(pos
.y
) < 0 )
6500 DisableCellEditControl();
6501 MakeCellVisible( coords
);
6503 if ( event
.CmdDown() )
6507 m_selection
->ToggleCellSelection(coords
, event
);
6510 m_selectedBlockTopLeft
= wxGridNoCellCoords
;
6511 m_selectedBlockBottomRight
= wxGridNoCellCoords
;
6512 m_selectedBlockCorner
= coords
;
6516 m_waitForSlowClick
= m_currentCellCoords
== coords
&&
6517 coords
!= wxGridNoCellCoords
;
6518 SetCurrentCell( coords
);
6524 wxGrid::DoGridCellLeftDClick(wxMouseEvent
& event
,
6525 const wxGridCellCoords
& coords
,
6528 if ( XToEdgeOfCol(pos
.x
) < 0 && YToEdgeOfRow(pos
.y
) < 0 )
6530 if ( !SendEvent(wxEVT_GRID_CELL_LEFT_DCLICK
, coords
, event
) )
6532 // we want double click to select a cell and start editing
6533 // (i.e. to behave in same way as sequence of two slow clicks):
6534 m_waitForSlowClick
= true;
6540 wxGrid::DoGridCellLeftUp(wxMouseEvent
& event
, const wxGridCellCoords
& coords
)
6542 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
6546 m_winCapture
->ReleaseMouse();
6547 m_winCapture
= NULL
;
6550 if ( coords
== m_currentCellCoords
&& m_waitForSlowClick
&& CanEnableCellControl() )
6553 EnableCellEditControl();
6555 wxGridCellAttr
*attr
= GetCellAttr(coords
);
6556 wxGridCellEditor
*editor
= attr
->GetEditor(this, coords
.GetRow(), coords
.GetCol());
6557 editor
->StartingClick();
6561 m_waitForSlowClick
= false;
6563 else if ( m_selectedBlockTopLeft
!= wxGridNoCellCoords
&&
6564 m_selectedBlockBottomRight
!= wxGridNoCellCoords
)
6568 m_selection
->SelectBlock( m_selectedBlockTopLeft
,
6569 m_selectedBlockBottomRight
,
6573 m_selectedBlockTopLeft
= wxGridNoCellCoords
;
6574 m_selectedBlockBottomRight
= wxGridNoCellCoords
;
6576 // Show the edit control, if it has been hidden for
6578 ShowCellEditControl();
6581 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
6583 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6584 DoEndDragResizeRow();
6586 // Note: we are ending the event *after* doing
6587 // default processing in this case
6589 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
6591 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
6593 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6594 DoEndDragResizeCol();
6601 wxGrid::DoGridMouseMoveEvent(wxMouseEvent
& WXUNUSED(event
),
6602 const wxGridCellCoords
& coords
,
6605 if ( coords
.GetRow() < 0 || coords
.GetCol() < 0 )
6607 // out of grid cell area
6608 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6612 int dragRow
= YToEdgeOfRow( pos
.y
);
6613 int dragCol
= XToEdgeOfCol( pos
.x
);
6615 // Dragging on the corner of a cell to resize in both
6616 // directions is not implemented yet...
6618 if ( dragRow
>= 0 && dragCol
>= 0 )
6620 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6626 m_dragRowOrCol
= dragRow
;
6628 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
6630 if ( CanDragRowSize() && CanDragGridSize() )
6631 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, NULL
, false);
6634 // When using the native header window we can only resize the columns by
6635 // dragging the dividers in it because we can't make it enter into the
6636 // column resizing mode programmatically
6637 else if ( dragCol
>= 0 && !m_useNativeHeader
)
6639 m_dragRowOrCol
= dragCol
;
6641 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
6643 if ( CanDragColSize() && CanDragGridSize() )
6644 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, NULL
, false);
6647 else // Neither on a row or col edge
6649 if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
6651 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6656 void wxGrid::ProcessGridCellMouseEvent(wxMouseEvent
& event
)
6658 const wxPoint pos
= CalcUnscrolledPosition(event
.GetPosition());
6660 // coordinates of the cell under mouse
6661 wxGridCellCoords coords
= XYToCell(pos
);
6663 int cell_rows
, cell_cols
;
6664 GetCellSize( coords
.GetRow(), coords
.GetCol(), &cell_rows
, &cell_cols
);
6665 if ( (cell_rows
< 0) || (cell_cols
< 0) )
6667 coords
.SetRow(coords
.GetRow() + cell_rows
);
6668 coords
.SetCol(coords
.GetCol() + cell_cols
);
6671 if ( event
.Dragging() )
6673 if ( event
.LeftIsDown() )
6674 DoGridDragEvent(event
, coords
);
6680 m_isDragging
= false;
6681 m_startDragPos
= wxDefaultPosition
;
6683 // VZ: if we do this, the mode is reset to WXGRID_CURSOR_SELECT_CELL
6684 // immediately after it becomes WXGRID_CURSOR_RESIZE_ROW/COL under
6687 if ( event
.Entering() || event
.Leaving() )
6689 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6690 m_gridWin
->SetCursor( *wxSTANDARD_CURSOR
);
6694 // deal with various button presses
6695 if ( event
.IsButton() )
6697 if ( coords
!= wxGridNoCellCoords
)
6699 DisableCellEditControl();
6701 if ( event
.LeftDown() )
6702 DoGridCellLeftDown(event
, coords
, pos
);
6703 else if ( event
.LeftDClick() )
6704 DoGridCellLeftDClick(event
, coords
, pos
);
6705 else if ( event
.RightDown() )
6706 SendEvent(wxEVT_GRID_CELL_RIGHT_CLICK
, coords
, event
);
6707 else if ( event
.RightDClick() )
6708 SendEvent(wxEVT_GRID_CELL_RIGHT_DCLICK
, coords
, event
);
6711 // this one should be called even if we're not over any cell
6712 if ( event
.LeftUp() )
6714 DoGridCellLeftUp(event
, coords
);
6717 else if ( event
.Moving() )
6719 DoGridMouseMoveEvent(event
, coords
, pos
);
6721 else // unknown mouse event?
6727 void wxGrid::DoEndDragResizeLine(const wxGridOperations
& oper
)
6729 if ( m_dragLastPos
== -1 )
6732 const wxGridOperations
& doper
= oper
.Dual();
6734 const wxSize size
= m_gridWin
->GetClientSize();
6736 const wxPoint ptOrigin
= CalcUnscrolledPosition(wxPoint(0, 0));
6738 // erase the last line we drew
6739 wxClientDC
dc(m_gridWin
);
6741 dc
.SetLogicalFunction(wxINVERT
);
6743 const int posLineStart
= oper
.Select(ptOrigin
);
6744 const int posLineEnd
= oper
.Select(ptOrigin
) + oper
.Select(size
);
6746 oper
.DrawParallelLine(dc
, posLineStart
, posLineEnd
, m_dragLastPos
);
6748 // temporarily hide the edit control before resizing
6749 HideCellEditControl();
6750 SaveEditControlValue();
6752 // do resize the line
6753 const int lineStart
= oper
.GetLineStartPos(this, m_dragRowOrCol
);
6754 oper
.SetLineSize(this, m_dragRowOrCol
,
6755 wxMax(m_dragLastPos
- lineStart
,
6756 oper
.GetMinimalLineSize(this, m_dragRowOrCol
)));
6760 // refresh now if we're not frozen
6761 if ( !GetBatchCount() )
6763 // we need to refresh everything beyond the resized line in the header
6766 // get the position from which to refresh in the other direction
6767 wxRect
rect(CellToRect(oper
.MakeCoords(m_dragRowOrCol
, 0)));
6768 rect
.SetPosition(CalcScrolledPosition(rect
.GetPosition()));
6770 // we only need the ordinate (for rows) or abscissa (for columns) here,
6771 // and need to cover the entire window in the other direction
6772 oper
.Select(rect
) = 0;
6774 wxRect
rectHeader(rect
.GetPosition(),
6777 oper
.GetHeaderWindowSize(this),
6778 doper
.Select(size
) - doper
.Select(rect
)
6781 oper
.GetHeaderWindow(this)->Refresh(true, &rectHeader
);
6784 // also refresh the grid window: extend the rectangle
6787 oper
.SelectSize(rect
) = oper
.Select(size
);
6789 int subtractLines
= 0;
6790 const int lineStart
= oper
.PosToLine(this, posLineStart
);
6791 if ( lineStart
>= 0 )
6793 // ensure that if we have a multi-cell block we redraw all of
6794 // it by increasing the refresh area to cover it entirely if a
6795 // part of it is affected
6796 const int lineEnd
= oper
.PosToLine(this, posLineEnd
, true);
6797 for ( int line
= lineStart
; line
< lineEnd
; line
++ )
6799 int cellLines
= oper
.Select(
6800 GetCellSize(oper
.MakeCoords(m_dragRowOrCol
, line
)));
6801 if ( cellLines
< subtractLines
)
6802 subtractLines
= cellLines
;
6807 oper
.GetLineStartPos(this, m_dragRowOrCol
+ subtractLines
);
6808 startPos
= doper
.CalcScrolledPosition(this, startPos
);
6810 doper
.Select(rect
) = startPos
;
6811 doper
.SelectSize(rect
) = doper
.Select(size
) - startPos
;
6813 m_gridWin
->Refresh(false, &rect
);
6817 // show the edit control back again
6818 ShowCellEditControl();
6821 void wxGrid::DoEndDragResizeRow()
6823 DoEndDragResizeLine(wxGridRowOperations());
6826 void wxGrid::DoEndDragResizeCol(wxMouseEvent
*event
)
6828 DoEndDragResizeLine(wxGridColumnOperations());
6830 // Note: we are ending the event *after* doing
6831 // default processing in this case
6834 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, *event
);
6836 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
);
6839 void wxGrid::DoStartMoveCol(int col
)
6841 m_dragRowOrCol
= col
;
6844 void wxGrid::DoEndMoveCol(int pos
)
6846 wxASSERT_MSG( m_dragRowOrCol
!= -1, "no matching DoStartMoveCol?" );
6848 if ( SendEvent(wxEVT_GRID_COL_MOVE
, -1, m_dragRowOrCol
) != -1 )
6849 SetColPos(m_dragRowOrCol
, pos
);
6850 //else: vetoed by user
6852 m_dragRowOrCol
= -1;
6855 void wxGrid::SetColPos(int idx
, int pos
)
6857 // we're going to need m_colAt now, initialize it if needed
6858 if ( m_colAt
.empty() )
6860 m_colAt
.reserve(m_numCols
);
6861 for ( int i
= 0; i
< m_numCols
; i
++ )
6862 m_colAt
.push_back(i
);
6865 wxHeaderCtrl::MoveColumnInOrderArray(m_colAt
, idx
, pos
);
6867 // also recalculate the column rights
6868 if ( !m_colWidths
.IsEmpty() )
6872 for ( colPos
= 0; colPos
< m_numCols
; colPos
++ )
6874 int colID
= GetColAt( colPos
);
6876 colRight
+= m_colWidths
[colID
];
6877 m_colRights
[colID
] = colRight
;
6881 // and make the changes visible
6882 if ( m_useNativeHeader
)
6883 GetColHeader()->SetColumnsOrder(m_colAt
);
6885 m_colWindow
->Refresh();
6886 m_gridWin
->Refresh();
6891 void wxGrid::EnableDragColMove( bool enable
)
6893 if ( m_canDragColMove
== enable
)
6896 m_canDragColMove
= enable
;
6898 if ( !m_canDragColMove
)
6902 //Recalculate the column rights
6903 if ( !m_colWidths
.IsEmpty() )
6907 for ( colPos
= 0; colPos
< m_numCols
; colPos
++ )
6909 colRight
+= m_colWidths
[colPos
];
6910 m_colRights
[colPos
] = colRight
;
6914 m_colWindow
->Refresh();
6915 m_gridWin
->Refresh();
6921 // ------ interaction with data model
6923 bool wxGrid::ProcessTableMessage( wxGridTableMessage
& msg
)
6925 switch ( msg
.GetId() )
6927 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES
:
6928 return GetModelValues();
6930 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES
:
6931 return SetModelValues();
6933 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
6934 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
6935 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
6936 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
6937 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
6938 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
6939 return Redimension( msg
);
6946 // The behaviour of this function depends on the grid table class
6947 // Clear() function. For the default wxGridStringTable class the
6948 // behaviour is to replace all cell contents with wxEmptyString but
6949 // not to change the number of rows or cols.
6951 void wxGrid::ClearGrid()
6955 if (IsCellEditControlEnabled())
6956 DisableCellEditControl();
6959 if (!GetBatchCount())
6960 m_gridWin
->Refresh();
6965 wxGrid::DoModifyLines(bool (wxGridTableBase::*funcModify
)(size_t, size_t),
6966 int pos
, int num
, bool WXUNUSED(updateLabels
) )
6968 wxCHECK_MSG( m_created
, false, "must finish creating the grid first" );
6973 if ( IsCellEditControlEnabled() )
6974 DisableCellEditControl();
6976 return (m_table
->*funcModify
)(pos
, num
);
6978 // the table will have sent the results of the insert row
6979 // operation to this view object as a grid table message
6983 wxGrid::DoAppendLines(bool (wxGridTableBase::*funcAppend
)(size_t),
6984 int num
, bool WXUNUSED(updateLabels
))
6986 wxCHECK_MSG( m_created
, false, "must finish creating the grid first" );
6991 return (m_table
->*funcAppend
)(num
);
6995 // ----- event handlers
6998 // Generate a grid event based on a mouse event and return:
6999 // -1 if the event was vetoed
7000 // +1 if the event was processed (but not vetoed)
7001 // 0 if the event wasn't handled
7003 wxGrid::SendEvent(const wxEventType type
,
7005 wxMouseEvent
& mouseEv
)
7007 bool claimed
, vetoed
;
7009 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
7011 int rowOrCol
= (row
== -1 ? col
: row
);
7013 wxGridSizeEvent
gridEvt( GetId(),
7017 mouseEv
.GetX() + GetRowLabelSize(),
7018 mouseEv
.GetY() + GetColLabelSize(),
7021 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
7022 vetoed
= !gridEvt
.IsAllowed();
7024 else if ( type
== wxEVT_GRID_RANGE_SELECT
)
7026 // Right now, it should _never_ end up here!
7027 wxGridRangeSelectEvent
gridEvt( GetId(),
7030 m_selectedBlockTopLeft
,
7031 m_selectedBlockBottomRight
,
7035 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
7036 vetoed
= !gridEvt
.IsAllowed();
7038 else if ( type
== wxEVT_GRID_LABEL_LEFT_CLICK
||
7039 type
== wxEVT_GRID_LABEL_LEFT_DCLICK
||
7040 type
== wxEVT_GRID_LABEL_RIGHT_CLICK
||
7041 type
== wxEVT_GRID_LABEL_RIGHT_DCLICK
)
7043 wxPoint pos
= mouseEv
.GetPosition();
7045 if ( mouseEv
.GetEventObject() == GetGridRowLabelWindow() )
7046 pos
.y
+= GetColLabelSize();
7047 if ( mouseEv
.GetEventObject() == GetGridColLabelWindow() )
7048 pos
.x
+= GetRowLabelSize();
7050 wxGridEvent
gridEvt( GetId(),
7058 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
7059 vetoed
= !gridEvt
.IsAllowed();
7063 wxGridEvent
gridEvt( GetId(),
7067 mouseEv
.GetX() + GetRowLabelSize(),
7068 mouseEv
.GetY() + GetColLabelSize(),
7071 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
7072 vetoed
= !gridEvt
.IsAllowed();
7075 // A Veto'd event may not be `claimed' so test this first
7079 return claimed
? 1 : 0;
7082 // Generate a grid event of specified type, return value same as above
7084 int wxGrid::SendEvent(const wxEventType type
, int row
, int col
)
7086 bool claimed
, vetoed
;
7088 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
7090 int rowOrCol
= (row
== -1 ? col
: row
);
7092 wxGridSizeEvent
gridEvt( GetId(), type
, this, rowOrCol
);
7094 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
7095 vetoed
= !gridEvt
.IsAllowed();
7099 wxGridEvent
gridEvt( GetId(), type
, this, row
, col
);
7101 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
7102 vetoed
= !gridEvt
.IsAllowed();
7105 // A Veto'd event may not be `claimed' so test this first
7109 return claimed
? 1 : 0;
7112 void wxGrid::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
7114 // needed to prevent zillions of paint events on MSW
7118 void wxGrid::Refresh(bool eraseb
, const wxRect
* rect
)
7120 // Don't do anything if between Begin/EndBatch...
7121 // EndBatch() will do all this on the last nested one anyway.
7122 if ( m_created
&& !GetBatchCount() )
7124 // Refresh to get correct scrolled position:
7125 wxScrolledWindow::Refresh(eraseb
, rect
);
7129 int rect_x
, rect_y
, rectWidth
, rectHeight
;
7130 int width_label
, width_cell
, height_label
, height_cell
;
7133 // Copy rectangle can get scroll offsets..
7134 rect_x
= rect
->GetX();
7135 rect_y
= rect
->GetY();
7136 rectWidth
= rect
->GetWidth();
7137 rectHeight
= rect
->GetHeight();
7139 width_label
= m_rowLabelWidth
- rect_x
;
7140 if (width_label
> rectWidth
)
7141 width_label
= rectWidth
;
7143 height_label
= m_colLabelHeight
- rect_y
;
7144 if (height_label
> rectHeight
)
7145 height_label
= rectHeight
;
7147 if (rect_x
> m_rowLabelWidth
)
7149 x
= rect_x
- m_rowLabelWidth
;
7150 width_cell
= rectWidth
;
7155 width_cell
= rectWidth
- (m_rowLabelWidth
- rect_x
);
7158 if (rect_y
> m_colLabelHeight
)
7160 y
= rect_y
- m_colLabelHeight
;
7161 height_cell
= rectHeight
;
7166 height_cell
= rectHeight
- (m_colLabelHeight
- rect_y
);
7169 // Paint corner label part intersecting rect.
7170 if ( width_label
> 0 && height_label
> 0 )
7172 wxRect
anotherrect(rect_x
, rect_y
, width_label
, height_label
);
7173 m_cornerLabelWin
->Refresh(eraseb
, &anotherrect
);
7176 // Paint col labels part intersecting rect.
7177 if ( width_cell
> 0 && height_label
> 0 )
7179 wxRect
anotherrect(x
, rect_y
, width_cell
, height_label
);
7180 m_colWindow
->Refresh(eraseb
, &anotherrect
);
7183 // Paint row labels part intersecting rect.
7184 if ( width_label
> 0 && height_cell
> 0 )
7186 wxRect
anotherrect(rect_x
, y
, width_label
, height_cell
);
7187 m_rowLabelWin
->Refresh(eraseb
, &anotherrect
);
7190 // Paint cell area part intersecting rect.
7191 if ( width_cell
> 0 && height_cell
> 0 )
7193 wxRect
anotherrect(x
, y
, width_cell
, height_cell
);
7194 m_gridWin
->Refresh(eraseb
, &anotherrect
);
7199 m_cornerLabelWin
->Refresh(eraseb
, NULL
);
7200 m_colWindow
->Refresh(eraseb
, NULL
);
7201 m_rowLabelWin
->Refresh(eraseb
, NULL
);
7202 m_gridWin
->Refresh(eraseb
, NULL
);
7207 void wxGrid::OnSize(wxSizeEvent
& WXUNUSED(event
))
7209 if (m_targetWindow
!= this) // check whether initialisation has been done
7211 // reposition our children windows
7216 void wxGrid::OnKeyDown( wxKeyEvent
& event
)
7218 if ( m_inOnKeyDown
)
7220 // shouldn't be here - we are going round in circles...
7222 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") );
7225 m_inOnKeyDown
= true;
7227 // propagate the event up and see if it gets processed
7228 wxWindow
*parent
= GetParent();
7229 wxKeyEvent
keyEvt( event
);
7230 keyEvt
.SetEventObject( parent
);
7232 if ( !parent
->GetEventHandler()->ProcessEvent( keyEvt
) )
7234 if (GetLayoutDirection() == wxLayout_RightToLeft
)
7236 if (event
.GetKeyCode() == WXK_RIGHT
)
7237 event
.m_keyCode
= WXK_LEFT
;
7238 else if (event
.GetKeyCode() == WXK_LEFT
)
7239 event
.m_keyCode
= WXK_RIGHT
;
7242 // try local handlers
7243 switch ( event
.GetKeyCode() )
7246 if ( event
.ControlDown() )
7247 MoveCursorUpBlock( event
.ShiftDown() );
7249 MoveCursorUp( event
.ShiftDown() );
7253 if ( event
.ControlDown() )
7254 MoveCursorDownBlock( event
.ShiftDown() );
7256 MoveCursorDown( event
.ShiftDown() );
7260 if ( event
.ControlDown() )
7261 MoveCursorLeftBlock( event
.ShiftDown() );
7263 MoveCursorLeft( event
.ShiftDown() );
7267 if ( event
.ControlDown() )
7268 MoveCursorRightBlock( event
.ShiftDown() );
7270 MoveCursorRight( event
.ShiftDown() );
7274 case WXK_NUMPAD_ENTER
:
7275 if ( event
.ControlDown() )
7277 event
.Skip(); // to let the edit control have the return
7281 if ( GetGridCursorRow() < GetNumberRows()-1 )
7283 MoveCursorDown( event
.ShiftDown() );
7287 // at the bottom of a column
7288 DisableCellEditControl();
7298 if (event
.ShiftDown())
7300 if ( GetGridCursorCol() > 0 )
7302 MoveCursorLeft( false );
7307 DisableCellEditControl();
7312 if ( GetGridCursorCol() < GetNumberCols() - 1 )
7314 MoveCursorRight( false );
7319 DisableCellEditControl();
7325 if ( event
.ControlDown() )
7336 if ( event
.ControlDown() )
7338 GoToCell(m_numRows
- 1, m_numCols
- 1);
7355 // Ctrl-Space selects the current column, Shift-Space -- the
7356 // current row and Ctrl-Shift-Space -- everything
7357 switch ( m_selection
? event
.GetModifiers() : wxMOD_NONE
)
7360 m_selection
->SelectCol(m_currentCellCoords
.GetCol());
7364 m_selection
->SelectRow(m_currentCellCoords
.GetRow());
7367 case wxMOD_CONTROL
| wxMOD_SHIFT
:
7368 m_selection
->SelectBlock(0, 0,
7369 m_numRows
- 1, m_numCols
- 1);
7373 if ( !IsEditable() )
7375 MoveCursorRight(false);
7378 //else: fall through
7391 m_inOnKeyDown
= false;
7394 void wxGrid::OnKeyUp( wxKeyEvent
& event
)
7396 // try local handlers
7398 if ( event
.GetKeyCode() == WXK_SHIFT
)
7400 if ( m_selectedBlockTopLeft
!= wxGridNoCellCoords
&&
7401 m_selectedBlockBottomRight
!= wxGridNoCellCoords
)
7405 m_selection
->SelectBlock(
7406 m_selectedBlockTopLeft
,
7407 m_selectedBlockBottomRight
,
7412 m_selectedBlockTopLeft
= wxGridNoCellCoords
;
7413 m_selectedBlockBottomRight
= wxGridNoCellCoords
;
7414 m_selectedBlockCorner
= wxGridNoCellCoords
;
7418 void wxGrid::OnChar( wxKeyEvent
& event
)
7420 // is it possible to edit the current cell at all?
7421 if ( !IsCellEditControlEnabled() && CanEnableCellControl() )
7423 // yes, now check whether the cells editor accepts the key
7424 int row
= m_currentCellCoords
.GetRow();
7425 int col
= m_currentCellCoords
.GetCol();
7426 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7427 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
7429 // <F2> is special and will always start editing, for
7430 // other keys - ask the editor itself
7431 if ( (event
.GetKeyCode() == WXK_F2
&& !event
.HasModifiers())
7432 || editor
->IsAcceptedKey(event
) )
7434 // ensure cell is visble
7435 MakeCellVisible(row
, col
);
7436 EnableCellEditControl();
7438 // a problem can arise if the cell is not completely
7439 // visible (even after calling MakeCellVisible the
7440 // control is not created and calling StartingKey will
7442 if ( event
.GetKeyCode() != WXK_F2
&& editor
->IsCreated() && m_cellEditCtrlEnabled
)
7443 editor
->StartingKey(event
);
7459 void wxGrid::OnEraseBackground(wxEraseEvent
&)
7463 bool wxGrid::SetCurrentCell( const wxGridCellCoords
& coords
)
7465 if ( SendEvent(wxEVT_GRID_SELECT_CELL
, coords
) == -1 )
7467 // the event has been vetoed - do nothing
7471 #if !defined(__WXMAC__)
7472 wxClientDC
dc( m_gridWin
);
7476 if ( m_currentCellCoords
!= wxGridNoCellCoords
)
7478 DisableCellEditControl();
7480 if ( IsVisible( m_currentCellCoords
, false ) )
7483 r
= BlockToDeviceRect( m_currentCellCoords
, m_currentCellCoords
);
7484 if ( !m_gridLinesEnabled
)
7492 wxGridCellCoordsArray cells
= CalcCellsExposed( r
);
7494 // Otherwise refresh redraws the highlight!
7495 m_currentCellCoords
= coords
;
7497 #if defined(__WXMAC__)
7498 m_gridWin
->Refresh(true /*, & r */);
7500 DrawGridCellArea( dc
, cells
);
7501 DrawAllGridLines( dc
, r
);
7506 m_currentCellCoords
= coords
;
7508 wxGridCellAttr
*attr
= GetCellAttr( coords
);
7509 #if !defined(__WXMAC__)
7510 DrawCellHighlight( dc
, attr
);
7518 wxGrid::UpdateBlockBeingSelected(int topRow
, int leftCol
,
7519 int bottomRow
, int rightCol
)
7523 switch ( m_selection
->GetSelectionMode() )
7526 wxFAIL_MSG( "unknown selection mode" );
7529 case wxGridSelectCells
:
7530 // arbitrary blocks selection allowed so just use the cell
7531 // coordinates as is
7534 case wxGridSelectRows
:
7535 // only full rows selection allowd, ensure that we do select
7538 rightCol
= GetNumberCols() - 1;
7541 case wxGridSelectColumns
:
7542 // same as above but for columns
7544 bottomRow
= GetNumberRows() - 1;
7547 case wxGridSelectRowsOrColumns
:
7548 // in this mode we can select only full rows or full columns so
7549 // it doesn't make sense to select blocks at all (and we can't
7550 // extend the block because there is no preferred direction, we
7551 // could only extend it to cover the entire grid but this is
7557 m_selectedBlockCorner
= wxGridCellCoords(bottomRow
, rightCol
);
7558 MakeCellVisible(m_selectedBlockCorner
);
7560 EnsureFirstLessThanSecond(topRow
, bottomRow
);
7561 EnsureFirstLessThanSecond(leftCol
, rightCol
);
7563 wxGridCellCoords updateTopLeft
= wxGridCellCoords(topRow
, leftCol
),
7564 updateBottomRight
= wxGridCellCoords(bottomRow
, rightCol
);
7566 // First the case that we selected a completely new area
7567 if ( m_selectedBlockTopLeft
== wxGridNoCellCoords
||
7568 m_selectedBlockBottomRight
== wxGridNoCellCoords
)
7571 rect
= BlockToDeviceRect( wxGridCellCoords ( topRow
, leftCol
),
7572 wxGridCellCoords ( bottomRow
, rightCol
) );
7573 m_gridWin
->Refresh( false, &rect
);
7576 // Now handle changing an existing selection area.
7577 else if ( m_selectedBlockTopLeft
!= updateTopLeft
||
7578 m_selectedBlockBottomRight
!= updateBottomRight
)
7580 // Compute two optimal update rectangles:
7581 // Either one rectangle is a real subset of the
7582 // other, or they are (almost) disjoint!
7584 bool need_refresh
[4];
7588 need_refresh
[3] = false;
7591 // Store intermediate values
7592 wxCoord oldLeft
= m_selectedBlockTopLeft
.GetCol();
7593 wxCoord oldTop
= m_selectedBlockTopLeft
.GetRow();
7594 wxCoord oldRight
= m_selectedBlockBottomRight
.GetCol();
7595 wxCoord oldBottom
= m_selectedBlockBottomRight
.GetRow();
7597 // Determine the outer/inner coordinates.
7598 EnsureFirstLessThanSecond(oldLeft
, leftCol
);
7599 EnsureFirstLessThanSecond(oldTop
, topRow
);
7600 EnsureFirstLessThanSecond(rightCol
, oldRight
);
7601 EnsureFirstLessThanSecond(bottomRow
, oldBottom
);
7603 // Now, either the stuff marked old is the outer
7604 // rectangle or we don't have a situation where one
7605 // is contained in the other.
7607 if ( oldLeft
< leftCol
)
7609 // Refresh the newly selected or deselected
7610 // area to the left of the old or new selection.
7611 need_refresh
[0] = true;
7612 rect
[0] = BlockToDeviceRect(
7613 wxGridCellCoords( oldTop
, oldLeft
),
7614 wxGridCellCoords( oldBottom
, leftCol
- 1 ) );
7617 if ( oldTop
< topRow
)
7619 // Refresh the newly selected or deselected
7620 // area above the old or new selection.
7621 need_refresh
[1] = true;
7622 rect
[1] = BlockToDeviceRect(
7623 wxGridCellCoords( oldTop
, leftCol
),
7624 wxGridCellCoords( topRow
- 1, rightCol
) );
7627 if ( oldRight
> rightCol
)
7629 // Refresh the newly selected or deselected
7630 // area to the right of the old or new selection.
7631 need_refresh
[2] = true;
7632 rect
[2] = BlockToDeviceRect(
7633 wxGridCellCoords( oldTop
, rightCol
+ 1 ),
7634 wxGridCellCoords( oldBottom
, oldRight
) );
7637 if ( oldBottom
> bottomRow
)
7639 // Refresh the newly selected or deselected
7640 // area below the old or new selection.
7641 need_refresh
[3] = true;
7642 rect
[3] = BlockToDeviceRect(
7643 wxGridCellCoords( bottomRow
+ 1, leftCol
),
7644 wxGridCellCoords( oldBottom
, rightCol
) );
7647 // various Refresh() calls
7648 for (i
= 0; i
< 4; i
++ )
7649 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
7650 m_gridWin
->Refresh( false, &(rect
[i
]) );
7654 m_selectedBlockTopLeft
= updateTopLeft
;
7655 m_selectedBlockBottomRight
= updateBottomRight
;
7659 // ------ functions to get/send data (see also public functions)
7662 bool wxGrid::GetModelValues()
7664 // Hide the editor, so it won't hide a changed value.
7665 HideCellEditControl();
7669 // all we need to do is repaint the grid
7671 m_gridWin
->Refresh();
7678 bool wxGrid::SetModelValues()
7682 // Disable the editor, so it won't hide a changed value.
7683 // Do we also want to save the current value of the editor first?
7685 DisableCellEditControl();
7689 for ( row
= 0; row
< m_numRows
; row
++ )
7691 for ( col
= 0; col
< m_numCols
; col
++ )
7693 m_table
->SetValue( row
, col
, GetCellValue(row
, col
) );
7703 // Note - this function only draws cells that are in the list of
7704 // exposed cells (usually set from the update region by
7705 // CalcExposedCells)
7707 void wxGrid::DrawGridCellArea( wxDC
& dc
, const wxGridCellCoordsArray
& cells
)
7709 if ( !m_numRows
|| !m_numCols
)
7712 int i
, numCells
= cells
.GetCount();
7713 int row
, col
, cell_rows
, cell_cols
;
7714 wxGridCellCoordsArray redrawCells
;
7716 for ( i
= numCells
- 1; i
>= 0; i
-- )
7718 row
= cells
[i
].GetRow();
7719 col
= cells
[i
].GetCol();
7720 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7722 // If this cell is part of a multicell block, find owner for repaint
7723 if ( cell_rows
<= 0 || cell_cols
<= 0 )
7725 wxGridCellCoords
cell( row
+ cell_rows
, col
+ cell_cols
);
7726 bool marked
= false;
7727 for ( int j
= 0; j
< numCells
; j
++ )
7729 if ( cell
== cells
[j
] )
7738 int count
= redrawCells
.GetCount();
7739 for (int j
= 0; j
< count
; j
++)
7741 if ( cell
== redrawCells
[j
] )
7749 redrawCells
.Add( cell
);
7752 // don't bother drawing this cell
7756 // If this cell is empty, find cell to left that might want to overflow
7757 if (m_table
&& m_table
->IsEmptyCell(row
, col
))
7759 for ( int l
= 0; l
< cell_rows
; l
++ )
7761 // find a cell in this row to leave already marked for repaint
7763 for (int k
= 0; k
< int(redrawCells
.GetCount()); k
++)
7764 if ((redrawCells
[k
].GetCol() < left
) &&
7765 (redrawCells
[k
].GetRow() == row
))
7767 left
= redrawCells
[k
].GetCol();
7771 left
= 0; // oh well
7773 for (int j
= col
- 1; j
>= left
; j
--)
7775 if (!m_table
->IsEmptyCell(row
+ l
, j
))
7777 if (GetCellOverflow(row
+ l
, j
))
7779 wxGridCellCoords
cell(row
+ l
, j
);
7780 bool marked
= false;
7782 for (int k
= 0; k
< numCells
; k
++)
7784 if ( cell
== cells
[k
] )
7793 int count
= redrawCells
.GetCount();
7794 for (int k
= 0; k
< count
; k
++)
7796 if ( cell
== redrawCells
[k
] )
7803 redrawCells
.Add( cell
);
7812 DrawCell( dc
, cells
[i
] );
7815 numCells
= redrawCells
.GetCount();
7817 for ( i
= numCells
- 1; i
>= 0; i
-- )
7819 DrawCell( dc
, redrawCells
[i
] );
7823 void wxGrid::DrawGridSpace( wxDC
& dc
)
7826 m_gridWin
->GetClientSize( &cw
, &ch
);
7829 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
7831 int rightCol
= m_numCols
> 0 ? GetColRight(GetColAt( m_numCols
- 1 )) : 0;
7832 int bottomRow
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) : 0;
7834 if ( right
> rightCol
|| bottom
> bottomRow
)
7837 CalcUnscrolledPosition( 0, 0, &left
, &top
);
7839 dc
.SetBrush(GetDefaultCellBackgroundColour());
7840 dc
.SetPen( *wxTRANSPARENT_PEN
);
7842 if ( right
> rightCol
)
7844 dc
.DrawRectangle( rightCol
, top
, right
- rightCol
, ch
);
7847 if ( bottom
> bottomRow
)
7849 dc
.DrawRectangle( left
, bottomRow
, cw
, bottom
- bottomRow
);
7854 void wxGrid::DrawCell( wxDC
& dc
, const wxGridCellCoords
& coords
)
7856 int row
= coords
.GetRow();
7857 int col
= coords
.GetCol();
7859 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7862 // we draw the cell border ourselves
7863 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7865 bool isCurrent
= coords
== m_currentCellCoords
;
7867 wxRect rect
= CellToRect( row
, col
);
7869 // if the editor is shown, we should use it and not the renderer
7870 // Note: However, only if it is really _shown_, i.e. not hidden!
7871 if ( isCurrent
&& IsCellEditControlShown() )
7873 // NB: this "#if..." is temporary and fixes a problem where the
7874 // edit control is erased by this code after being rendered.
7875 // On wxMac (QD build only), the cell editor is a wxTextCntl and is rendered
7876 // implicitly, causing this out-of order render.
7877 #if !defined(__WXMAC__)
7878 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
7879 editor
->PaintBackground(rect
, attr
);
7885 // but all the rest is drawn by the cell renderer and hence may be customized
7886 wxGridCellRenderer
*renderer
= attr
->GetRenderer(this, row
, col
);
7887 renderer
->Draw(*this, *attr
, dc
, rect
, row
, col
, IsInSelection(coords
));
7894 void wxGrid::DrawCellHighlight( wxDC
& dc
, const wxGridCellAttr
*attr
)
7896 // don't show highlight when the grid doesn't have focus
7900 int row
= m_currentCellCoords
.GetRow();
7901 int col
= m_currentCellCoords
.GetCol();
7903 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7906 wxRect rect
= CellToRect(row
, col
);
7908 // hmmm... what could we do here to show that the cell is disabled?
7909 // for now, I just draw a thinner border than for the other ones, but
7910 // it doesn't look really good
7912 int penWidth
= attr
->IsReadOnly() ? m_cellHighlightROPenWidth
: m_cellHighlightPenWidth
;
7916 // The center of the drawn line is where the position/width/height of
7917 // the rectangle is actually at (on wxMSW at least), so the
7918 // size of the rectangle is reduced to compensate for the thickness of
7919 // the line. If this is too strange on non-wxMSW platforms then
7920 // please #ifdef this appropriately.
7921 rect
.x
+= penWidth
/ 2;
7922 rect
.y
+= penWidth
/ 2;
7923 rect
.width
-= penWidth
- 1;
7924 rect
.height
-= penWidth
- 1;
7926 // Now draw the rectangle
7927 // use the cellHighlightColour if the cell is inside a selection, this
7928 // will ensure the cell is always visible.
7929 dc
.SetPen(wxPen(IsInSelection(row
,col
) ? m_selectionForeground
7930 : m_cellHighlightColour
,
7932 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
7933 dc
.DrawRectangle(rect
);
7937 wxPen
wxGrid::GetDefaultGridLinePen()
7939 return wxPen(GetGridLineColour());
7942 wxPen
wxGrid::GetRowGridLinePen(int WXUNUSED(row
))
7944 return GetDefaultGridLinePen();
7947 wxPen
wxGrid::GetColGridLinePen(int WXUNUSED(col
))
7949 return GetDefaultGridLinePen();
7952 void wxGrid::DrawCellBorder( wxDC
& dc
, const wxGridCellCoords
& coords
)
7954 int row
= coords
.GetRow();
7955 int col
= coords
.GetCol();
7956 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7960 wxRect rect
= CellToRect( row
, col
);
7962 // right hand border
7963 dc
.SetPen( GetColGridLinePen(col
) );
7964 dc
.DrawLine( rect
.x
+ rect
.width
, rect
.y
,
7965 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
+ 1 );
7968 dc
.SetPen( GetRowGridLinePen(row
) );
7969 dc
.DrawLine( rect
.x
, rect
.y
+ rect
.height
,
7970 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
);
7973 void wxGrid::DrawHighlight(wxDC
& dc
, const wxGridCellCoordsArray
& cells
)
7975 // This if block was previously in wxGrid::OnPaint but that doesn't
7976 // seem to get called under wxGTK - MB
7978 if ( m_currentCellCoords
== wxGridNoCellCoords
&&
7979 m_numRows
&& m_numCols
)
7981 m_currentCellCoords
.Set(0, 0);
7984 if ( IsCellEditControlShown() )
7986 // don't show highlight when the edit control is shown
7990 // if the active cell was repainted, repaint its highlight too because it
7991 // might have been damaged by the grid lines
7992 size_t count
= cells
.GetCount();
7993 for ( size_t n
= 0; n
< count
; n
++ )
7995 wxGridCellCoords cell
= cells
[n
];
7997 // If we are using attributes, then we may have just exposed another
7998 // cell in a partially-visible merged cluster of cells. If the "anchor"
7999 // (upper left) cell of this merged cluster is the cell indicated by
8000 // m_currentCellCoords, then we need to refresh the cell highlight even
8001 // though the "anchor" itself is not part of our update segment.
8002 if ( CanHaveAttributes() )
8006 GetCellSize(cell
.GetRow(), cell
.GetCol(), &rows
, &cols
);
8009 cell
.SetRow(cell
.GetRow() + rows
);
8012 cell
.SetCol(cell
.GetCol() + cols
);
8015 if ( cell
== m_currentCellCoords
)
8017 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
8018 DrawCellHighlight(dc
, attr
);
8026 // This is used to redraw all grid lines e.g. when the grid line colour
8029 void wxGrid::DrawAllGridLines( wxDC
& dc
, const wxRegion
& WXUNUSED(reg
) )
8031 if ( !m_gridLinesEnabled
)
8034 int top
, bottom
, left
, right
;
8037 m_gridWin
->GetClientSize(&cw
, &ch
);
8038 CalcUnscrolledPosition( 0, 0, &left
, &top
);
8039 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
8041 // avoid drawing grid lines past the last row and col
8042 if ( m_gridLinesClipHorz
)
8047 const int lastColRight
= GetColRight(GetColAt(m_numCols
- 1));
8048 if ( right
> lastColRight
)
8049 right
= lastColRight
;
8052 if ( m_gridLinesClipVert
)
8057 const int lastRowBottom
= GetRowBottom(m_numRows
- 1);
8058 if ( bottom
> lastRowBottom
)
8059 bottom
= lastRowBottom
;
8062 // no gridlines inside multicells, clip them out
8063 int leftCol
= GetColPos( internalXToCol(left
) );
8064 int topRow
= internalYToRow(top
);
8065 int rightCol
= GetColPos( internalXToCol(right
) );
8066 int bottomRow
= internalYToRow(bottom
);
8068 wxRegion
clippedcells(0, 0, cw
, ch
);
8070 int cell_rows
, cell_cols
;
8073 for ( int j
= topRow
; j
<= bottomRow
; j
++ )
8075 for ( int colPos
= leftCol
; colPos
<= rightCol
; colPos
++ )
8077 int i
= GetColAt( colPos
);
8079 GetCellSize( j
, i
, &cell_rows
, &cell_cols
);
8080 if ((cell_rows
> 1) || (cell_cols
> 1))
8082 rect
= CellToRect(j
,i
);
8083 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
8084 clippedcells
.Subtract(rect
);
8086 else if ((cell_rows
< 0) || (cell_cols
< 0))
8088 rect
= CellToRect(j
+ cell_rows
, i
+ cell_cols
);
8089 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
8090 clippedcells
.Subtract(rect
);
8095 dc
.SetDeviceClippingRegion( clippedcells
);
8098 // horizontal grid lines
8099 for ( int i
= internalYToRow(top
); i
< m_numRows
; i
++ )
8101 int bot
= GetRowBottom(i
) - 1;
8108 dc
.SetPen( GetRowGridLinePen(i
) );
8109 dc
.DrawLine( left
, bot
, right
, bot
);
8113 // vertical grid lines
8114 for ( int colPos
= leftCol
; colPos
< m_numCols
; colPos
++ )
8116 int i
= GetColAt( colPos
);
8118 int colRight
= GetColRight(i
);
8120 if (GetLayoutDirection() != wxLayout_RightToLeft
)
8124 if ( colRight
> right
)
8127 if ( colRight
>= left
)
8129 dc
.SetPen( GetColGridLinePen(i
) );
8130 dc
.DrawLine( colRight
, top
, colRight
, bottom
);
8134 dc
.DestroyClippingRegion();
8137 void wxGrid::DrawRowLabels( wxDC
& dc
, const wxArrayInt
& rows
)
8142 const size_t numLabels
= rows
.GetCount();
8143 for ( size_t i
= 0; i
< numLabels
; i
++ )
8145 DrawRowLabel( dc
, rows
[i
] );
8149 void wxGrid::DrawRowLabel( wxDC
& dc
, int row
)
8151 if ( GetRowHeight(row
) <= 0 || m_rowLabelWidth
<= 0 )
8156 int rowTop
= GetRowTop(row
),
8157 rowBottom
= GetRowBottom(row
) - 1;
8159 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW
)));
8160 dc
.DrawLine( m_rowLabelWidth
- 1, rowTop
, m_rowLabelWidth
- 1, rowBottom
);
8161 dc
.DrawLine( 0, rowTop
, 0, rowBottom
);
8162 dc
.DrawLine( 0, rowBottom
, m_rowLabelWidth
, rowBottom
);
8164 dc
.SetPen( *wxWHITE_PEN
);
8165 dc
.DrawLine( 1, rowTop
, 1, rowBottom
);
8166 dc
.DrawLine( 1, rowTop
, m_rowLabelWidth
- 1, rowTop
);
8168 dc
.SetBackgroundMode( wxBRUSHSTYLE_TRANSPARENT
);
8169 dc
.SetTextForeground( GetLabelTextColour() );
8170 dc
.SetFont( GetLabelFont() );
8173 GetRowLabelAlignment( &hAlign
, &vAlign
);
8176 rect
.SetY( GetRowTop(row
) + 2 );
8177 rect
.SetWidth( m_rowLabelWidth
- 4 );
8178 rect
.SetHeight( GetRowHeight(row
) - 4 );
8179 DrawTextRectangle( dc
, GetRowLabelValue( row
), rect
, hAlign
, vAlign
);
8182 void wxGrid::UseNativeColHeader(bool native
)
8184 if ( native
== m_useNativeHeader
)
8188 m_useNativeHeader
= native
;
8190 CreateColumnWindow();
8192 if ( m_useNativeHeader
)
8193 GetColHeader()->SetColumnCount(m_numCols
);
8197 void wxGrid::SetUseNativeColLabels( bool native
)
8199 wxASSERT_MSG( !m_useNativeHeader
,
8200 "doesn't make sense when using native header" );
8202 m_nativeColumnLabels
= native
;
8205 int height
= wxRendererNative::Get().GetHeaderButtonHeight( this );
8206 SetColLabelSize( height
);
8209 GetColLabelWindow()->Refresh();
8210 m_cornerLabelWin
->Refresh();
8213 void wxGrid::DrawColLabels( wxDC
& dc
,const wxArrayInt
& cols
)
8218 const size_t numLabels
= cols
.GetCount();
8219 for ( size_t i
= 0; i
< numLabels
; i
++ )
8221 DrawColLabel( dc
, cols
[i
] );
8225 void wxGrid::DrawCornerLabel(wxDC
& dc
)
8227 if ( m_nativeColumnLabels
)
8229 wxRect
rect(wxSize(m_rowLabelWidth
, m_colLabelHeight
));
8232 wxRendererNative::Get().DrawHeaderButton(m_cornerLabelWin
, dc
, rect
, 0);
8236 dc
.SetPen(wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW
)));
8237 dc
.DrawLine( m_rowLabelWidth
- 1, m_colLabelHeight
- 1,
8238 m_rowLabelWidth
- 1, 0 );
8239 dc
.DrawLine( m_rowLabelWidth
- 1, m_colLabelHeight
- 1,
8240 0, m_colLabelHeight
- 1 );
8241 dc
.DrawLine( 0, 0, m_rowLabelWidth
, 0 );
8242 dc
.DrawLine( 0, 0, 0, m_colLabelHeight
);
8244 dc
.SetPen( *wxWHITE_PEN
);
8245 dc
.DrawLine( 1, 1, m_rowLabelWidth
- 1, 1 );
8246 dc
.DrawLine( 1, 1, 1, m_colLabelHeight
- 1 );
8250 void wxGrid::DrawColLabel(wxDC
& dc
, int col
)
8252 if ( GetColWidth(col
) <= 0 || m_colLabelHeight
<= 0 )
8255 int colLeft
= GetColLeft(col
);
8257 wxRect
rect(colLeft
, 0, GetColWidth(col
), m_colLabelHeight
);
8259 if ( m_nativeColumnLabels
)
8261 wxRendererNative::Get().DrawHeaderButton
8263 GetColLabelWindow(),
8268 ? IsSortOrderAscending()
8269 ? wxHDR_SORT_ICON_UP
8270 : wxHDR_SORT_ICON_DOWN
8271 : wxHDR_SORT_ICON_NONE
8276 int colRight
= GetColRight(col
) - 1;
8278 dc
.SetPen(wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW
)));
8279 dc
.DrawLine( colRight
, 0,
8280 colRight
, m_colLabelHeight
- 1 );
8281 dc
.DrawLine( colLeft
, 0,
8283 dc
.DrawLine( colLeft
, m_colLabelHeight
- 1,
8284 colRight
+ 1, m_colLabelHeight
- 1 );
8286 dc
.SetPen( *wxWHITE_PEN
);
8287 dc
.DrawLine( colLeft
, 1, colLeft
, m_colLabelHeight
- 1 );
8288 dc
.DrawLine( colLeft
, 1, colRight
, 1 );
8291 dc
.SetBackgroundMode( wxBRUSHSTYLE_TRANSPARENT
);
8292 dc
.SetTextForeground( GetLabelTextColour() );
8293 dc
.SetFont( GetLabelFont() );
8296 GetColLabelAlignment( &hAlign
, &vAlign
);
8297 const int orient
= GetColLabelTextOrientation();
8300 DrawTextRectangle(dc
, GetColLabelValue(col
), rect
, hAlign
, vAlign
, orient
);
8303 // TODO: these 2 functions should be replaced with wxDC::DrawLabel() to which
8304 // we just have to add textOrientation support
8305 void wxGrid::DrawTextRectangle( wxDC
& dc
,
8306 const wxString
& value
,
8310 int textOrientation
)
8312 wxArrayString lines
;
8314 StringToLines( value
, lines
);
8316 DrawTextRectangle(dc
, lines
, rect
, horizAlign
, vertAlign
, textOrientation
);
8319 void wxGrid::DrawTextRectangle(wxDC
& dc
,
8320 const wxArrayString
& lines
,
8324 int textOrientation
)
8326 if ( lines
.empty() )
8329 wxDCClipper
clip(dc
, rect
);
8334 if ( textOrientation
== wxHORIZONTAL
)
8335 GetTextBoxSize( dc
, lines
, &textWidth
, &textHeight
);
8337 GetTextBoxSize( dc
, lines
, &textHeight
, &textWidth
);
8341 switch ( vertAlign
)
8343 case wxALIGN_BOTTOM
:
8344 if ( textOrientation
== wxHORIZONTAL
)
8345 y
= rect
.y
+ (rect
.height
- textHeight
- 1);
8347 x
= rect
.x
+ rect
.width
- textWidth
;
8350 case wxALIGN_CENTRE
:
8351 if ( textOrientation
== wxHORIZONTAL
)
8352 y
= rect
.y
+ ((rect
.height
- textHeight
) / 2);
8354 x
= rect
.x
+ ((rect
.width
- textWidth
) / 2);
8359 if ( textOrientation
== wxHORIZONTAL
)
8366 // Align each line of a multi-line label
8367 size_t nLines
= lines
.GetCount();
8368 for ( size_t l
= 0; l
< nLines
; l
++ )
8370 const wxString
& line
= lines
[l
];
8374 *(textOrientation
== wxHORIZONTAL
? &y
: &x
) += dc
.GetCharHeight();
8378 wxCoord lineWidth
= 0,
8380 dc
.GetTextExtent(line
, &lineWidth
, &lineHeight
);
8382 switch ( horizAlign
)
8385 if ( textOrientation
== wxHORIZONTAL
)
8386 x
= rect
.x
+ (rect
.width
- lineWidth
- 1);
8388 y
= rect
.y
+ lineWidth
+ 1;
8391 case wxALIGN_CENTRE
:
8392 if ( textOrientation
== wxHORIZONTAL
)
8393 x
= rect
.x
+ ((rect
.width
- lineWidth
) / 2);
8395 y
= rect
.y
+ rect
.height
- ((rect
.height
- lineWidth
) / 2);
8400 if ( textOrientation
== wxHORIZONTAL
)
8403 y
= rect
.y
+ rect
.height
- 1;
8407 if ( textOrientation
== wxHORIZONTAL
)
8409 dc
.DrawText( line
, x
, y
);
8414 dc
.DrawRotatedText( line
, x
, y
, 90.0 );
8420 // Split multi-line text up into an array of strings.
8421 // Any existing contents of the string array are preserved.
8423 // TODO: refactor wxTextFile::Read() and reuse the same code from here
8424 void wxGrid::StringToLines( const wxString
& value
, wxArrayString
& lines
) const
8428 wxString eol
= wxTextFile::GetEOL( wxTextFileType_Unix
);
8429 wxString tVal
= wxTextFile::Translate( value
, wxTextFileType_Unix
);
8431 while ( startPos
< (int)tVal
.length() )
8433 pos
= tVal
.Mid(startPos
).Find( eol
);
8438 else if ( pos
== 0 )
8440 lines
.Add( wxEmptyString
);
8444 lines
.Add( tVal
.Mid(startPos
, pos
) );
8447 startPos
+= pos
+ 1;
8450 if ( startPos
< (int)tVal
.length() )
8452 lines
.Add( tVal
.Mid( startPos
) );
8456 void wxGrid::GetTextBoxSize( const wxDC
& dc
,
8457 const wxArrayString
& lines
,
8458 long *width
, long *height
) const
8462 wxCoord lineW
= 0, lineH
= 0;
8465 for ( i
= 0; i
< lines
.GetCount(); i
++ )
8467 dc
.GetTextExtent( lines
[i
], &lineW
, &lineH
);
8468 w
= wxMax( w
, lineW
);
8477 // ------ Batch processing.
8479 void wxGrid::EndBatch()
8481 if ( m_batchCount
> 0 )
8484 if ( !m_batchCount
)
8487 m_rowLabelWin
->Refresh();
8488 m_colWindow
->Refresh();
8489 m_cornerLabelWin
->Refresh();
8490 m_gridWin
->Refresh();
8495 // Use this, rather than wxWindow::Refresh(), to force an immediate
8496 // repainting of the grid. Has no effect if you are already inside a
8497 // BeginBatch / EndBatch block.
8499 void wxGrid::ForceRefresh()
8505 bool wxGrid::Enable(bool enable
)
8507 if ( !wxScrolledWindow::Enable(enable
) )
8510 // redraw in the new state
8511 m_gridWin
->Refresh();
8517 // ------ Edit control functions
8520 void wxGrid::EnableEditing( bool edit
)
8522 if ( edit
!= m_editable
)
8525 EnableCellEditControl(edit
);
8530 void wxGrid::EnableCellEditControl( bool enable
)
8535 if ( enable
!= m_cellEditCtrlEnabled
)
8539 if ( SendEvent(wxEVT_GRID_EDITOR_SHOWN
) == -1 )
8542 // this should be checked by the caller!
8543 wxASSERT_MSG( CanEnableCellControl(), _T("can't enable editing for this cell!") );
8545 // do it before ShowCellEditControl()
8546 m_cellEditCtrlEnabled
= enable
;
8548 ShowCellEditControl();
8552 //FIXME:add veto support
8553 SendEvent(wxEVT_GRID_EDITOR_HIDDEN
);
8555 HideCellEditControl();
8556 SaveEditControlValue();
8558 // do it after HideCellEditControl()
8559 m_cellEditCtrlEnabled
= enable
;
8564 bool wxGrid::IsCurrentCellReadOnly() const
8567 wxGridCellAttr
* attr
= ((wxGrid
*)this)->GetCellAttr(m_currentCellCoords
);
8568 bool readonly
= attr
->IsReadOnly();
8574 bool wxGrid::CanEnableCellControl() const
8576 return m_editable
&& (m_currentCellCoords
!= wxGridNoCellCoords
) &&
8577 !IsCurrentCellReadOnly();
8580 bool wxGrid::IsCellEditControlEnabled() const
8582 // the cell edit control might be disable for all cells or just for the
8583 // current one if it's read only
8584 return m_cellEditCtrlEnabled
? !IsCurrentCellReadOnly() : false;
8587 bool wxGrid::IsCellEditControlShown() const
8589 bool isShown
= false;
8591 if ( m_cellEditCtrlEnabled
)
8593 int row
= m_currentCellCoords
.GetRow();
8594 int col
= m_currentCellCoords
.GetCol();
8595 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8596 wxGridCellEditor
* editor
= attr
->GetEditor((wxGrid
*) this, row
, col
);
8601 if ( editor
->IsCreated() )
8603 isShown
= editor
->GetControl()->IsShown();
8613 void wxGrid::ShowCellEditControl()
8615 if ( IsCellEditControlEnabled() )
8617 if ( !IsVisible( m_currentCellCoords
, false ) )
8619 m_cellEditCtrlEnabled
= false;
8624 wxRect rect
= CellToRect( m_currentCellCoords
);
8625 int row
= m_currentCellCoords
.GetRow();
8626 int col
= m_currentCellCoords
.GetCol();
8628 // if this is part of a multicell, find owner (topleft)
8629 int cell_rows
, cell_cols
;
8630 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
8631 if ( cell_rows
<= 0 || cell_cols
<= 0 )
8635 m_currentCellCoords
.SetRow( row
);
8636 m_currentCellCoords
.SetCol( col
);
8639 // erase the highlight and the cell contents because the editor
8640 // might not cover the entire cell
8641 wxClientDC
dc( m_gridWin
);
8643 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8644 dc
.SetBrush(wxBrush(attr
->GetBackgroundColour()));
8645 dc
.SetPen(*wxTRANSPARENT_PEN
);
8646 dc
.DrawRectangle(rect
);
8648 // convert to scrolled coords
8649 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
8655 // cell is shifted by one pixel
8656 // However, don't allow x or y to become negative
8657 // since the SetSize() method interprets that as
8664 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
8665 if ( !editor
->IsCreated() )
8667 editor
->Create(m_gridWin
, wxID_ANY
,
8668 new wxGridCellEditorEvtHandler(this, editor
));
8670 wxGridEditorCreatedEvent
evt(GetId(),
8671 wxEVT_GRID_EDITOR_CREATED
,
8675 editor
->GetControl());
8676 GetEventHandler()->ProcessEvent(evt
);
8679 // resize editor to overflow into righthand cells if allowed
8680 int maxWidth
= rect
.width
;
8681 wxString value
= GetCellValue(row
, col
);
8682 if ( (value
!= wxEmptyString
) && (attr
->GetOverflow()) )
8685 GetTextExtent(value
, &maxWidth
, &y
, NULL
, NULL
, &attr
->GetFont());
8686 if (maxWidth
< rect
.width
)
8687 maxWidth
= rect
.width
;
8690 int client_right
= m_gridWin
->GetClientSize().GetWidth();
8691 if (rect
.x
+ maxWidth
> client_right
)
8692 maxWidth
= client_right
- rect
.x
;
8694 if ((maxWidth
> rect
.width
) && (col
< m_numCols
) && m_table
)
8696 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
8697 // may have changed earlier
8698 for (int i
= col
+ cell_cols
; i
< m_numCols
; i
++)
8701 GetCellSize( row
, i
, &c_rows
, &c_cols
);
8703 // looks weird going over a multicell
8704 if (m_table
->IsEmptyCell( row
, i
) &&
8705 (rect
.width
< maxWidth
) && (c_rows
== 1))
8707 rect
.width
+= GetColWidth( i
);
8713 if (rect
.GetRight() > client_right
)
8714 rect
.SetRight( client_right
- 1 );
8717 editor
->SetCellAttr( attr
);
8718 editor
->SetSize( rect
);
8720 editor
->GetControl()->Move(
8721 editor
->GetControl()->GetPosition().x
+ nXMove
,
8722 editor
->GetControl()->GetPosition().y
);
8723 editor
->Show( true, attr
);
8725 // recalc dimensions in case we need to
8726 // expand the scrolled window to account for editor
8729 editor
->BeginEdit(row
, col
, this);
8730 editor
->SetCellAttr(NULL
);
8738 void wxGrid::HideCellEditControl()
8740 if ( IsCellEditControlEnabled() )
8742 int row
= m_currentCellCoords
.GetRow();
8743 int col
= m_currentCellCoords
.GetCol();
8745 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8746 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
8747 const bool editorHadFocus
= editor
->GetControl()->HasFocus();
8748 editor
->Show( false );
8752 // return the focus to the grid itself if the editor had it
8754 // note that we must not do this unconditionally to avoid stealing
8755 // focus from the window which just received it if we are hiding the
8756 // editor precisely because we lost focus
8757 if ( editorHadFocus
)
8758 m_gridWin
->SetFocus();
8760 // refresh whole row to the right
8761 wxRect
rect( CellToRect(row
, col
) );
8762 CalcScrolledPosition(rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
8763 rect
.width
= m_gridWin
->GetClientSize().GetWidth() - rect
.x
;
8766 // ensure that the pixels under the focus ring get refreshed as well
8767 rect
.Inflate(10, 10);
8770 m_gridWin
->Refresh( false, &rect
);
8774 void wxGrid::SaveEditControlValue()
8776 if ( IsCellEditControlEnabled() )
8778 int row
= m_currentCellCoords
.GetRow();
8779 int col
= m_currentCellCoords
.GetCol();
8781 wxString oldval
= GetCellValue(row
, col
);
8783 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8784 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
8785 bool changed
= editor
->EndEdit(row
, col
, this);
8792 if ( SendEvent(wxEVT_GRID_CELL_CHANGE
) == -1 )
8794 // Event has been vetoed, set the data back.
8795 SetCellValue(row
, col
, oldval
);
8802 // ------ Grid location functions
8803 // Note that all of these functions work with the logical coordinates of
8804 // grid cells and labels so you will need to convert from device
8805 // coordinates for mouse events etc.
8808 wxGridCellCoords
wxGrid::XYToCell(int x
, int y
) const
8810 int row
= YToRow(y
);
8811 int col
= XToCol(x
);
8813 return row
== -1 || col
== -1 ? wxGridNoCellCoords
8814 : wxGridCellCoords(row
, col
);
8817 // compute row or column from some (unscrolled) coordinate value, using either
8818 // m_defaultRowHeight/m_defaultColWidth or binary search on array of
8819 // m_rowBottoms/m_colRights to do it quickly (linear search shouldn't be used
8821 int wxGrid::PosToLinePos(int coord
,
8823 const wxGridOperations
& oper
) const
8825 const int numLines
= oper
.GetNumberOfLines(this);
8828 return clipToMinMax
&& numLines
> 0 ? 0 : wxNOT_FOUND
;
8830 const int defaultLineSize
= oper
.GetDefaultLineSize(this);
8831 wxCHECK_MSG( defaultLineSize
, -1, "can't have 0 default line size" );
8833 int maxPos
= coord
/ defaultLineSize
,
8836 // check for the simplest case: if we have no explicit line sizes
8837 // configured, then we already know the line this position falls in
8838 const wxArrayInt
& lineEnds
= oper
.GetLineEnds(this);
8839 if ( lineEnds
.empty() )
8841 if ( maxPos
< numLines
)
8844 return clipToMinMax
? numLines
- 1 : -1;
8848 // adjust maxPos before starting the binary search
8849 if ( maxPos
>= numLines
)
8851 maxPos
= numLines
- 1;
8855 if ( coord
>= lineEnds
[oper
.GetLineAt(this, maxPos
)])
8858 const int minDist
= oper
.GetMinimalAcceptableLineSize(this);
8860 maxPos
= coord
/ minDist
;
8862 maxPos
= numLines
- 1;
8865 if ( maxPos
>= numLines
)
8866 maxPos
= numLines
- 1;
8869 // check if the position is beyond the last column
8870 const int lineAtMaxPos
= oper
.GetLineAt(this, maxPos
);
8871 if ( coord
>= lineEnds
[lineAtMaxPos
] )
8872 return clipToMinMax
? maxPos
: -1;
8874 // or before the first one
8875 const int lineAt0
= oper
.GetLineAt(this, 0);
8876 if ( coord
< lineEnds
[lineAt0
] )
8880 // finally do perform the binary search
8881 while ( minPos
< maxPos
)
8883 wxCHECK_MSG( lineEnds
[oper
.GetLineAt(this, minPos
)] <= coord
&&
8884 coord
< lineEnds
[oper
.GetLineAt(this, maxPos
)],
8886 "wxGrid: internal error in PosToLinePos()" );
8888 if ( coord
>= lineEnds
[oper
.GetLineAt(this, maxPos
- 1)] )
8893 const int median
= minPos
+ (maxPos
- minPos
+ 1) / 2;
8894 if ( coord
< lineEnds
[oper
.GetLineAt(this, median
)] )
8904 wxGrid::PosToLine(int coord
,
8906 const wxGridOperations
& oper
) const
8908 int pos
= PosToLinePos(coord
, clipToMinMax
, oper
);
8910 return pos
== wxNOT_FOUND
? wxNOT_FOUND
: oper
.GetLineAt(this, pos
);
8913 int wxGrid::YToRow(int y
, bool clipToMinMax
) const
8915 return PosToLine(y
, clipToMinMax
, wxGridRowOperations());
8918 int wxGrid::XToCol(int x
, bool clipToMinMax
) const
8920 return PosToLine(x
, clipToMinMax
, wxGridColumnOperations());
8923 int wxGrid::XToPos(int x
) const
8925 return PosToLinePos(x
, true /* clip */, wxGridColumnOperations());
8928 // return the row number that that the y coord is near the edge of, or -1 if
8929 // not near an edge.
8931 // coords can only possibly be near an edge if
8932 // (a) the row/column is large enough to still allow for an "inner" area
8933 // that is _not_ near the edge (i.e., if the height/width is smaller
8934 // than WXGRID_LABEL_EDGE_ZONE, coords are _never_ considered to be
8937 // (b) resizing rows/columns (the thing for which edge detection is
8938 // relevant at all) is enabled.
8940 int wxGrid::PosToEdgeOfLine(int pos
, const wxGridOperations
& oper
) const
8942 if ( !oper
.CanResizeLines(this) )
8945 const int line
= oper
.PosToLine(this, pos
, true);
8947 if ( oper
.GetLineSize(this, line
) > WXGRID_LABEL_EDGE_ZONE
)
8949 // We know that we are in this line, test whether we are close enough
8950 // to start or end border, respectively.
8951 if ( abs(oper
.GetLineEndPos(this, line
) - pos
) < WXGRID_LABEL_EDGE_ZONE
)
8953 else if ( line
> 0 &&
8954 pos
- oper
.GetLineStartPos(this,
8955 line
) < WXGRID_LABEL_EDGE_ZONE
)
8962 int wxGrid::YToEdgeOfRow(int y
) const
8964 return PosToEdgeOfLine(y
, wxGridRowOperations());
8967 int wxGrid::XToEdgeOfCol(int x
) const
8969 return PosToEdgeOfLine(x
, wxGridColumnOperations());
8972 wxRect
wxGrid::CellToRect( int row
, int col
) const
8974 wxRect
rect( -1, -1, -1, -1 );
8976 if ( row
>= 0 && row
< m_numRows
&&
8977 col
>= 0 && col
< m_numCols
)
8979 int i
, cell_rows
, cell_cols
;
8980 rect
.width
= rect
.height
= 0;
8981 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
8982 // if negative then find multicell owner
8987 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
8989 rect
.x
= GetColLeft(col
);
8990 rect
.y
= GetRowTop(row
);
8991 for (i
=col
; i
< col
+ cell_cols
; i
++)
8992 rect
.width
+= GetColWidth(i
);
8993 for (i
=row
; i
< row
+ cell_rows
; i
++)
8994 rect
.height
+= GetRowHeight(i
);
8997 // if grid lines are enabled, then the area of the cell is a bit smaller
8998 if (m_gridLinesEnabled
)
9007 bool wxGrid::IsVisible( int row
, int col
, bool wholeCellVisible
) const
9009 // get the cell rectangle in logical coords
9011 wxRect
r( CellToRect( row
, col
) );
9013 // convert to device coords
9015 int left
, top
, right
, bottom
;
9016 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
9017 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
9019 // check against the client area of the grid window
9021 m_gridWin
->GetClientSize( &cw
, &ch
);
9023 if ( wholeCellVisible
)
9025 // is the cell wholly visible ?
9026 return ( left
>= 0 && right
<= cw
&&
9027 top
>= 0 && bottom
<= ch
);
9031 // is the cell partly visible ?
9033 return ( ((left
>= 0 && left
< cw
) || (right
> 0 && right
<= cw
)) &&
9034 ((top
>= 0 && top
< ch
) || (bottom
> 0 && bottom
<= ch
)) );
9038 // make the specified cell location visible by doing a minimal amount
9041 void wxGrid::MakeCellVisible( int row
, int col
)
9044 int xpos
= -1, ypos
= -1;
9046 if ( row
>= 0 && row
< m_numRows
&&
9047 col
>= 0 && col
< m_numCols
)
9049 // get the cell rectangle in logical coords
9050 wxRect
r( CellToRect( row
, col
) );
9052 // convert to device coords
9053 int left
, top
, right
, bottom
;
9054 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
9055 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
9058 m_gridWin
->GetClientSize( &cw
, &ch
);
9064 else if ( bottom
> ch
)
9066 int h
= r
.GetHeight();
9068 for ( i
= row
- 1; i
>= 0; i
-- )
9070 int rowHeight
= GetRowHeight(i
);
9071 if ( h
+ rowHeight
> ch
)
9078 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
9079 // have rounding errors (this is important, because if we do,
9080 // we might not scroll at all and some cells won't be redrawn)
9082 // Sometimes GRID_SCROLL_LINE / 2 is not enough,
9083 // so just add a full scroll unit...
9084 ypos
+= m_scrollLineY
;
9087 // special handling for wide cells - show always left part of the cell!
9088 // Otherwise, e.g. when stepping from row to row, it would jump between
9089 // left and right part of the cell on every step!
9091 if ( left
< 0 || (right
- left
) >= cw
)
9095 else if ( right
> cw
)
9097 // position the view so that the cell is on the right
9099 CalcUnscrolledPosition(0, 0, &x0
, &y0
);
9100 xpos
= x0
+ (right
- cw
);
9102 // see comment for ypos above
9103 xpos
+= m_scrollLineX
;
9106 if ( xpos
!= -1 || ypos
!= -1 )
9109 xpos
/= m_scrollLineX
;
9111 ypos
/= m_scrollLineY
;
9112 Scroll( xpos
, ypos
);
9119 // ------ Grid cursor movement functions
9123 wxGrid::DoMoveCursor(bool expandSelection
,
9124 const wxGridDirectionOperations
& diroper
)
9126 if ( m_currentCellCoords
== wxGridNoCellCoords
)
9129 if ( expandSelection
)
9131 wxGridCellCoords coords
= m_selectedBlockCorner
;
9132 if ( coords
== wxGridNoCellCoords
)
9133 coords
= m_currentCellCoords
;
9135 if ( diroper
.IsAtBoundary(coords
) )
9138 diroper
.Advance(coords
);
9140 UpdateBlockBeingSelected(m_currentCellCoords
, coords
);
9142 else // don't expand selection
9146 if ( diroper
.IsAtBoundary(m_currentCellCoords
) )
9149 wxGridCellCoords coords
= m_currentCellCoords
;
9150 diroper
.Advance(coords
);
9158 bool wxGrid::MoveCursorUp(bool expandSelection
)
9160 return DoMoveCursor(expandSelection
,
9161 wxGridBackwardOperations(this, wxGridRowOperations()));
9164 bool wxGrid::MoveCursorDown(bool expandSelection
)
9166 return DoMoveCursor(expandSelection
,
9167 wxGridForwardOperations(this, wxGridRowOperations()));
9170 bool wxGrid::MoveCursorLeft(bool expandSelection
)
9172 return DoMoveCursor(expandSelection
,
9173 wxGridBackwardOperations(this, wxGridColumnOperations()));
9176 bool wxGrid::MoveCursorRight(bool expandSelection
)
9178 return DoMoveCursor(expandSelection
,
9179 wxGridForwardOperations(this, wxGridColumnOperations()));
9182 bool wxGrid::DoMoveCursorByPage(const wxGridDirectionOperations
& diroper
)
9184 if ( m_currentCellCoords
== wxGridNoCellCoords
)
9187 if ( diroper
.IsAtBoundary(m_currentCellCoords
) )
9190 const int oldRow
= m_currentCellCoords
.GetRow();
9191 int newRow
= diroper
.MoveByPixelDistance(oldRow
, m_gridWin
->GetClientSize().y
);
9192 if ( newRow
== oldRow
)
9194 wxGridCellCoords
coords(m_currentCellCoords
);
9195 diroper
.Advance(coords
);
9196 newRow
= coords
.GetRow();
9199 GoToCell(newRow
, m_currentCellCoords
.GetCol());
9204 bool wxGrid::MovePageUp()
9206 return DoMoveCursorByPage(
9207 wxGridBackwardOperations(this, wxGridRowOperations()));
9210 bool wxGrid::MovePageDown()
9212 return DoMoveCursorByPage(
9213 wxGridForwardOperations(this, wxGridRowOperations()));
9216 // helper of DoMoveCursorByBlock(): advance the cell coordinates using diroper
9217 // until we find a non-empty cell or reach the grid end
9219 wxGrid::AdvanceToNextNonEmpty(wxGridCellCoords
& coords
,
9220 const wxGridDirectionOperations
& diroper
)
9222 while ( !diroper
.IsAtBoundary(coords
) )
9224 diroper
.Advance(coords
);
9225 if ( !m_table
->IsEmpty(coords
) )
9231 wxGrid::DoMoveCursorByBlock(bool expandSelection
,
9232 const wxGridDirectionOperations
& diroper
)
9234 if ( !m_table
|| m_currentCellCoords
== wxGridNoCellCoords
)
9237 if ( diroper
.IsAtBoundary(m_currentCellCoords
) )
9240 wxGridCellCoords
coords(m_currentCellCoords
);
9241 if ( m_table
->IsEmpty(coords
) )
9243 // we are in an empty cell: find the next block of non-empty cells
9244 AdvanceToNextNonEmpty(coords
, diroper
);
9246 else // current cell is not empty
9248 diroper
.Advance(coords
);
9249 if ( m_table
->IsEmpty(coords
) )
9251 // we started at the end of a block, find the next one
9252 AdvanceToNextNonEmpty(coords
, diroper
);
9254 else // we're in a middle of a block
9256 // go to the end of it, i.e. find the last cell before the next
9258 while ( !diroper
.IsAtBoundary(coords
) )
9260 wxGridCellCoords
coordsNext(coords
);
9261 diroper
.Advance(coordsNext
);
9262 if ( m_table
->IsEmpty(coordsNext
) )
9265 coords
= coordsNext
;
9270 if ( expandSelection
)
9272 UpdateBlockBeingSelected(m_currentCellCoords
, coords
);
9283 bool wxGrid::MoveCursorUpBlock(bool expandSelection
)
9285 return DoMoveCursorByBlock(
9287 wxGridBackwardOperations(this, wxGridRowOperations())
9291 bool wxGrid::MoveCursorDownBlock( bool expandSelection
)
9293 return DoMoveCursorByBlock(
9295 wxGridForwardOperations(this, wxGridRowOperations())
9299 bool wxGrid::MoveCursorLeftBlock( bool expandSelection
)
9301 return DoMoveCursorByBlock(
9303 wxGridBackwardOperations(this, wxGridColumnOperations())
9307 bool wxGrid::MoveCursorRightBlock( bool expandSelection
)
9309 return DoMoveCursorByBlock(
9311 wxGridForwardOperations(this, wxGridColumnOperations())
9316 // ------ Label values and formatting
9319 void wxGrid::GetRowLabelAlignment( int *horiz
, int *vert
) const
9322 *horiz
= m_rowLabelHorizAlign
;
9324 *vert
= m_rowLabelVertAlign
;
9327 void wxGrid::GetColLabelAlignment( int *horiz
, int *vert
) const
9330 *horiz
= m_colLabelHorizAlign
;
9332 *vert
= m_colLabelVertAlign
;
9335 int wxGrid::GetColLabelTextOrientation() const
9337 return m_colLabelTextOrientation
;
9340 wxString
wxGrid::GetRowLabelValue( int row
) const
9344 return m_table
->GetRowLabelValue( row
);
9354 wxString
wxGrid::GetColLabelValue( int col
) const
9358 return m_table
->GetColLabelValue( col
);
9368 void wxGrid::SetRowLabelSize( int width
)
9370 wxASSERT( width
>= 0 || width
== wxGRID_AUTOSIZE
);
9372 if ( width
== wxGRID_AUTOSIZE
)
9374 width
= CalcColOrRowLabelAreaMinSize(wxGRID_ROW
);
9377 if ( width
!= m_rowLabelWidth
)
9381 m_rowLabelWin
->Show( false );
9382 m_cornerLabelWin
->Show( false );
9384 else if ( m_rowLabelWidth
== 0 )
9386 m_rowLabelWin
->Show( true );
9387 if ( m_colLabelHeight
> 0 )
9388 m_cornerLabelWin
->Show( true );
9391 m_rowLabelWidth
= width
;
9393 wxScrolledWindow::Refresh( true );
9397 void wxGrid::SetColLabelSize( int height
)
9399 wxASSERT( height
>=0 || height
== wxGRID_AUTOSIZE
);
9401 if ( height
== wxGRID_AUTOSIZE
)
9403 height
= CalcColOrRowLabelAreaMinSize(wxGRID_COLUMN
);
9406 if ( height
!= m_colLabelHeight
)
9410 m_colWindow
->Show( false );
9411 m_cornerLabelWin
->Show( false );
9413 else if ( m_colLabelHeight
== 0 )
9415 m_colWindow
->Show( true );
9416 if ( m_rowLabelWidth
> 0 )
9417 m_cornerLabelWin
->Show( true );
9420 m_colLabelHeight
= height
;
9422 wxScrolledWindow::Refresh( true );
9426 void wxGrid::SetLabelBackgroundColour( const wxColour
& colour
)
9428 if ( m_labelBackgroundColour
!= colour
)
9430 m_labelBackgroundColour
= colour
;
9431 m_rowLabelWin
->SetBackgroundColour( colour
);
9432 m_colWindow
->SetBackgroundColour( colour
);
9433 m_cornerLabelWin
->SetBackgroundColour( colour
);
9435 if ( !GetBatchCount() )
9437 m_rowLabelWin
->Refresh();
9438 m_colWindow
->Refresh();
9439 m_cornerLabelWin
->Refresh();
9444 void wxGrid::SetLabelTextColour( const wxColour
& colour
)
9446 if ( m_labelTextColour
!= colour
)
9448 m_labelTextColour
= colour
;
9449 if ( !GetBatchCount() )
9451 m_rowLabelWin
->Refresh();
9452 m_colWindow
->Refresh();
9457 void wxGrid::SetLabelFont( const wxFont
& font
)
9460 if ( !GetBatchCount() )
9462 m_rowLabelWin
->Refresh();
9463 m_colWindow
->Refresh();
9467 void wxGrid::SetRowLabelAlignment( int horiz
, int vert
)
9469 // allow old (incorrect) defs to be used
9472 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
9473 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
9474 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
9479 case wxTOP
: vert
= wxALIGN_TOP
; break;
9480 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
9481 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
9484 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
9486 m_rowLabelHorizAlign
= horiz
;
9489 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
9491 m_rowLabelVertAlign
= vert
;
9494 if ( !GetBatchCount() )
9496 m_rowLabelWin
->Refresh();
9500 void wxGrid::SetColLabelAlignment( int horiz
, int vert
)
9502 // allow old (incorrect) defs to be used
9505 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
9506 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
9507 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
9512 case wxTOP
: vert
= wxALIGN_TOP
; break;
9513 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
9514 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
9517 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
9519 m_colLabelHorizAlign
= horiz
;
9522 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
9524 m_colLabelVertAlign
= vert
;
9527 if ( !GetBatchCount() )
9529 m_colWindow
->Refresh();
9533 // Note: under MSW, the default column label font must be changed because it
9534 // does not support vertical printing
9536 // Example: wxFont font(9, wxSWISS, wxNORMAL, wxBOLD);
9537 // pGrid->SetLabelFont(font);
9538 // pGrid->SetColLabelTextOrientation(wxVERTICAL);
9540 void wxGrid::SetColLabelTextOrientation( int textOrientation
)
9542 if ( textOrientation
== wxHORIZONTAL
|| textOrientation
== wxVERTICAL
)
9543 m_colLabelTextOrientation
= textOrientation
;
9545 if ( !GetBatchCount() )
9546 m_colWindow
->Refresh();
9549 void wxGrid::SetRowLabelValue( int row
, const wxString
& s
)
9553 m_table
->SetRowLabelValue( row
, s
);
9554 if ( !GetBatchCount() )
9556 wxRect rect
= CellToRect( row
, 0 );
9557 if ( rect
.height
> 0 )
9559 CalcScrolledPosition(0, rect
.y
, &rect
.x
, &rect
.y
);
9561 rect
.width
= m_rowLabelWidth
;
9562 m_rowLabelWin
->Refresh( true, &rect
);
9568 void wxGrid::SetColLabelValue( int col
, const wxString
& s
)
9572 m_table
->SetColLabelValue( col
, s
);
9573 if ( !GetBatchCount() )
9575 if ( m_useNativeHeader
)
9577 GetColHeader()->UpdateColumn(col
);
9581 wxRect rect
= CellToRect( 0, col
);
9582 if ( rect
.width
> 0 )
9584 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &rect
.y
);
9586 rect
.height
= m_colLabelHeight
;
9587 GetColLabelWindow()->Refresh( true, &rect
);
9594 void wxGrid::SetGridLineColour( const wxColour
& colour
)
9596 if ( m_gridLineColour
!= colour
)
9598 m_gridLineColour
= colour
;
9600 if ( GridLinesEnabled() )
9605 void wxGrid::SetCellHighlightColour( const wxColour
& colour
)
9607 if ( m_cellHighlightColour
!= colour
)
9609 m_cellHighlightColour
= colour
;
9611 wxClientDC
dc( m_gridWin
);
9613 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
9614 DrawCellHighlight(dc
, attr
);
9619 void wxGrid::SetCellHighlightPenWidth(int width
)
9621 if (m_cellHighlightPenWidth
!= width
)
9623 m_cellHighlightPenWidth
= width
;
9625 // Just redrawing the cell highlight is not enough since that won't
9626 // make any visible change if the the thickness is getting smaller.
9627 int row
= m_currentCellCoords
.GetRow();
9628 int col
= m_currentCellCoords
.GetCol();
9629 if ( row
== -1 || col
== -1 || GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
9632 wxRect rect
= CellToRect(row
, col
);
9633 m_gridWin
->Refresh(true, &rect
);
9637 void wxGrid::SetCellHighlightROPenWidth(int width
)
9639 if (m_cellHighlightROPenWidth
!= width
)
9641 m_cellHighlightROPenWidth
= width
;
9643 // Just redrawing the cell highlight is not enough since that won't
9644 // make any visible change if the the thickness is getting smaller.
9645 int row
= m_currentCellCoords
.GetRow();
9646 int col
= m_currentCellCoords
.GetCol();
9647 if ( row
== -1 || col
== -1 ||
9648 GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
9651 wxRect rect
= CellToRect(row
, col
);
9652 m_gridWin
->Refresh(true, &rect
);
9656 void wxGrid::RedrawGridLines()
9658 // the lines will be redrawn when the window is thawn
9659 if ( GetBatchCount() )
9662 if ( GridLinesEnabled() )
9664 wxClientDC
dc( m_gridWin
);
9666 DrawAllGridLines( dc
, wxRegion() );
9668 else // remove the grid lines
9670 m_gridWin
->Refresh();
9674 void wxGrid::EnableGridLines( bool enable
)
9676 if ( enable
!= m_gridLinesEnabled
)
9678 m_gridLinesEnabled
= enable
;
9684 void wxGrid::DoClipGridLines(bool& var
, bool clip
)
9690 if ( GridLinesEnabled() )
9695 int wxGrid::GetDefaultRowSize() const
9697 return m_defaultRowHeight
;
9700 int wxGrid::GetRowSize( int row
) const
9702 wxCHECK_MSG( row
>= 0 && row
< m_numRows
, 0, _T("invalid row index") );
9704 return GetRowHeight(row
);
9707 int wxGrid::GetDefaultColSize() const
9709 return m_defaultColWidth
;
9712 int wxGrid::GetColSize( int col
) const
9714 wxCHECK_MSG( col
>= 0 && col
< m_numCols
, 0, _T("invalid column index") );
9716 return GetColWidth(col
);
9719 // ============================================================================
9720 // access to the grid attributes: each of them has a default value in the grid
9721 // itself and may be overidden on a per-cell basis
9722 // ============================================================================
9724 // ----------------------------------------------------------------------------
9725 // setting default attributes
9726 // ----------------------------------------------------------------------------
9728 void wxGrid::SetDefaultCellBackgroundColour( const wxColour
& col
)
9730 m_defaultCellAttr
->SetBackgroundColour(col
);
9732 m_gridWin
->SetBackgroundColour(col
);
9736 void wxGrid::SetDefaultCellTextColour( const wxColour
& col
)
9738 m_defaultCellAttr
->SetTextColour(col
);
9741 void wxGrid::SetDefaultCellAlignment( int horiz
, int vert
)
9743 m_defaultCellAttr
->SetAlignment(horiz
, vert
);
9746 void wxGrid::SetDefaultCellOverflow( bool allow
)
9748 m_defaultCellAttr
->SetOverflow(allow
);
9751 void wxGrid::SetDefaultCellFont( const wxFont
& font
)
9753 m_defaultCellAttr
->SetFont(font
);
9756 // For editors and renderers the type registry takes precedence over the
9757 // default attr, so we need to register the new editor/renderer for the string
9758 // data type in order to make setting a default editor/renderer appear to
9761 void wxGrid::SetDefaultRenderer(wxGridCellRenderer
*renderer
)
9763 RegisterDataType(wxGRID_VALUE_STRING
,
9765 GetDefaultEditorForType(wxGRID_VALUE_STRING
));
9768 void wxGrid::SetDefaultEditor(wxGridCellEditor
*editor
)
9770 RegisterDataType(wxGRID_VALUE_STRING
,
9771 GetDefaultRendererForType(wxGRID_VALUE_STRING
),
9775 // ----------------------------------------------------------------------------
9776 // access to the default attributes
9777 // ----------------------------------------------------------------------------
9779 wxColour
wxGrid::GetDefaultCellBackgroundColour() const
9781 return m_defaultCellAttr
->GetBackgroundColour();
9784 wxColour
wxGrid::GetDefaultCellTextColour() const
9786 return m_defaultCellAttr
->GetTextColour();
9789 wxFont
wxGrid::GetDefaultCellFont() const
9791 return m_defaultCellAttr
->GetFont();
9794 void wxGrid::GetDefaultCellAlignment( int *horiz
, int *vert
) const
9796 m_defaultCellAttr
->GetAlignment(horiz
, vert
);
9799 bool wxGrid::GetDefaultCellOverflow() const
9801 return m_defaultCellAttr
->GetOverflow();
9804 wxGridCellRenderer
*wxGrid::GetDefaultRenderer() const
9806 return m_defaultCellAttr
->GetRenderer(NULL
, 0, 0);
9809 wxGridCellEditor
*wxGrid::GetDefaultEditor() const
9811 return m_defaultCellAttr
->GetEditor(NULL
, 0, 0);
9814 // ----------------------------------------------------------------------------
9815 // access to cell attributes
9816 // ----------------------------------------------------------------------------
9818 wxColour
wxGrid::GetCellBackgroundColour(int row
, int col
) const
9820 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9821 wxColour colour
= attr
->GetBackgroundColour();
9827 wxColour
wxGrid::GetCellTextColour( int row
, int col
) const
9829 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9830 wxColour colour
= attr
->GetTextColour();
9836 wxFont
wxGrid::GetCellFont( int row
, int col
) const
9838 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9839 wxFont font
= attr
->GetFont();
9845 void wxGrid::GetCellAlignment( int row
, int col
, int *horiz
, int *vert
) const
9847 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9848 attr
->GetAlignment(horiz
, vert
);
9852 bool wxGrid::GetCellOverflow( int row
, int col
) const
9854 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9855 bool allow
= attr
->GetOverflow();
9861 void wxGrid::GetCellSize( int row
, int col
, int *num_rows
, int *num_cols
) const
9863 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9864 attr
->GetSize( num_rows
, num_cols
);
9868 wxGridCellRenderer
* wxGrid::GetCellRenderer(int row
, int col
) const
9870 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9871 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
9877 wxGridCellEditor
* wxGrid::GetCellEditor(int row
, int col
) const
9879 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9880 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
9886 bool wxGrid::IsReadOnly(int row
, int col
) const
9888 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9889 bool isReadOnly
= attr
->IsReadOnly();
9895 // ----------------------------------------------------------------------------
9896 // attribute support: cache, automatic provider creation, ...
9897 // ----------------------------------------------------------------------------
9899 bool wxGrid::CanHaveAttributes() const
9906 return m_table
->CanHaveAttributes();
9909 void wxGrid::ClearAttrCache()
9911 if ( m_attrCache
.row
!= -1 )
9913 wxGridCellAttr
*oldAttr
= m_attrCache
.attr
;
9914 m_attrCache
.attr
= NULL
;
9915 m_attrCache
.row
= -1;
9916 // wxSafeDecRec(...) might cause event processing that accesses
9917 // the cached attribute, if one exists (e.g. by deleting the
9918 // editor stored within the attribute). Therefore it is important
9919 // to invalidate the cache before calling wxSafeDecRef!
9920 wxSafeDecRef(oldAttr
);
9924 void wxGrid::CacheAttr(int row
, int col
, wxGridCellAttr
*attr
) const
9928 wxGrid
*self
= (wxGrid
*)this; // const_cast
9930 self
->ClearAttrCache();
9931 self
->m_attrCache
.row
= row
;
9932 self
->m_attrCache
.col
= col
;
9933 self
->m_attrCache
.attr
= attr
;
9938 bool wxGrid::LookupAttr(int row
, int col
, wxGridCellAttr
**attr
) const
9940 if ( row
== m_attrCache
.row
&& col
== m_attrCache
.col
)
9942 *attr
= m_attrCache
.attr
;
9943 wxSafeIncRef(m_attrCache
.attr
);
9945 #ifdef DEBUG_ATTR_CACHE
9946 gs_nAttrCacheHits
++;
9953 #ifdef DEBUG_ATTR_CACHE
9954 gs_nAttrCacheMisses
++;
9961 wxGridCellAttr
*wxGrid::GetCellAttr(int row
, int col
) const
9963 wxGridCellAttr
*attr
= NULL
;
9964 // Additional test to avoid looking at the cache e.g. for
9965 // wxNoCellCoords, as this will confuse memory management.
9968 if ( !LookupAttr(row
, col
, &attr
) )
9970 attr
= m_table
? m_table
->GetAttr(row
, col
, wxGridCellAttr::Any
)
9972 CacheAttr(row
, col
, attr
);
9978 attr
->SetDefAttr(m_defaultCellAttr
);
9982 attr
= m_defaultCellAttr
;
9989 wxGridCellAttr
*wxGrid::GetOrCreateCellAttr(int row
, int col
) const
9991 wxGridCellAttr
*attr
= NULL
;
9992 bool canHave
= ((wxGrid
*)this)->CanHaveAttributes();
9994 wxCHECK_MSG( canHave
, attr
, _T("Cell attributes not allowed"));
9995 wxCHECK_MSG( m_table
, attr
, _T("must have a table") );
9997 attr
= m_table
->GetAttr(row
, col
, wxGridCellAttr::Cell
);
10000 attr
= new wxGridCellAttr(m_defaultCellAttr
);
10002 // artificially inc the ref count to match DecRef() in caller
10004 m_table
->SetAttr(attr
, row
, col
);
10010 // ----------------------------------------------------------------------------
10011 // setting column attributes (wrappers around SetColAttr)
10012 // ----------------------------------------------------------------------------
10014 void wxGrid::SetColFormatBool(int col
)
10016 SetColFormatCustom(col
, wxGRID_VALUE_BOOL
);
10019 void wxGrid::SetColFormatNumber(int col
)
10021 SetColFormatCustom(col
, wxGRID_VALUE_NUMBER
);
10024 void wxGrid::SetColFormatFloat(int col
, int width
, int precision
)
10026 wxString typeName
= wxGRID_VALUE_FLOAT
;
10027 if ( (width
!= -1) || (precision
!= -1) )
10029 typeName
<< _T(':') << width
<< _T(',') << precision
;
10032 SetColFormatCustom(col
, typeName
);
10035 void wxGrid::SetColFormatCustom(int col
, const wxString
& typeName
)
10037 wxGridCellAttr
*attr
= m_table
->GetAttr(-1, col
, wxGridCellAttr::Col
);
10039 attr
= new wxGridCellAttr
;
10040 wxGridCellRenderer
*renderer
= GetDefaultRendererForType(typeName
);
10041 attr
->SetRenderer(renderer
);
10042 wxGridCellEditor
*editor
= GetDefaultEditorForType(typeName
);
10043 attr
->SetEditor(editor
);
10045 SetColAttr(col
, attr
);
10049 // ----------------------------------------------------------------------------
10050 // setting cell attributes: this is forwarded to the table
10051 // ----------------------------------------------------------------------------
10053 void wxGrid::SetAttr(int row
, int col
, wxGridCellAttr
*attr
)
10055 if ( CanHaveAttributes() )
10057 m_table
->SetAttr(attr
, row
, col
);
10062 wxSafeDecRef(attr
);
10066 void wxGrid::SetRowAttr(int row
, wxGridCellAttr
*attr
)
10068 if ( CanHaveAttributes() )
10070 m_table
->SetRowAttr(attr
, row
);
10075 wxSafeDecRef(attr
);
10079 void wxGrid::SetColAttr(int col
, wxGridCellAttr
*attr
)
10081 if ( CanHaveAttributes() )
10083 m_table
->SetColAttr(attr
, col
);
10088 wxSafeDecRef(attr
);
10092 void wxGrid::SetCellBackgroundColour( int row
, int col
, const wxColour
& colour
)
10094 if ( CanHaveAttributes() )
10096 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10097 attr
->SetBackgroundColour(colour
);
10102 void wxGrid::SetCellTextColour( int row
, int col
, const wxColour
& colour
)
10104 if ( CanHaveAttributes() )
10106 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10107 attr
->SetTextColour(colour
);
10112 void wxGrid::SetCellFont( int row
, int col
, const wxFont
& font
)
10114 if ( CanHaveAttributes() )
10116 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10117 attr
->SetFont(font
);
10122 void wxGrid::SetCellAlignment( int row
, int col
, int horiz
, int vert
)
10124 if ( CanHaveAttributes() )
10126 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10127 attr
->SetAlignment(horiz
, vert
);
10132 void wxGrid::SetCellOverflow( int row
, int col
, bool allow
)
10134 if ( CanHaveAttributes() )
10136 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10137 attr
->SetOverflow(allow
);
10142 void wxGrid::SetCellSize( int row
, int col
, int num_rows
, int num_cols
)
10144 if ( CanHaveAttributes() )
10146 int cell_rows
, cell_cols
;
10148 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10149 attr
->GetSize(&cell_rows
, &cell_cols
);
10150 attr
->SetSize(num_rows
, num_cols
);
10153 // Cannot set the size of a cell to 0 or negative values
10154 // While it is perfectly legal to do that, this function cannot
10155 // handle all the possibilies, do it by hand by getting the CellAttr.
10156 // You can only set the size of a cell to 1,1 or greater with this fn
10157 wxASSERT_MSG( !((cell_rows
< 1) || (cell_cols
< 1)),
10158 wxT("wxGrid::SetCellSize setting cell size that is already part of another cell"));
10159 wxASSERT_MSG( !((num_rows
< 1) || (num_cols
< 1)),
10160 wxT("wxGrid::SetCellSize setting cell size to < 1"));
10162 // if this was already a multicell then "turn off" the other cells first
10163 if ((cell_rows
> 1) || (cell_cols
> 1))
10166 for (j
=row
; j
< row
+ cell_rows
; j
++)
10168 for (i
=col
; i
< col
+ cell_cols
; i
++)
10170 if ((i
!= col
) || (j
!= row
))
10172 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
10173 attr_stub
->SetSize( 1, 1 );
10174 attr_stub
->DecRef();
10180 // mark the cells that will be covered by this cell to
10181 // negative or zero values to point back at this cell
10182 if (((num_rows
> 1) || (num_cols
> 1)) && (num_rows
>= 1) && (num_cols
>= 1))
10185 for (j
=row
; j
< row
+ num_rows
; j
++)
10187 for (i
=col
; i
< col
+ num_cols
; i
++)
10189 if ((i
!= col
) || (j
!= row
))
10191 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
10192 attr_stub
->SetSize( row
- j
, col
- i
);
10193 attr_stub
->DecRef();
10201 void wxGrid::SetCellRenderer(int row
, int col
, wxGridCellRenderer
*renderer
)
10203 if ( CanHaveAttributes() )
10205 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10206 attr
->SetRenderer(renderer
);
10211 void wxGrid::SetCellEditor(int row
, int col
, wxGridCellEditor
* editor
)
10213 if ( CanHaveAttributes() )
10215 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10216 attr
->SetEditor(editor
);
10221 void wxGrid::SetReadOnly(int row
, int col
, bool isReadOnly
)
10223 if ( CanHaveAttributes() )
10225 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10226 attr
->SetReadOnly(isReadOnly
);
10231 // ----------------------------------------------------------------------------
10232 // Data type registration
10233 // ----------------------------------------------------------------------------
10235 void wxGrid::RegisterDataType(const wxString
& typeName
,
10236 wxGridCellRenderer
* renderer
,
10237 wxGridCellEditor
* editor
)
10239 m_typeRegistry
->RegisterDataType(typeName
, renderer
, editor
);
10243 wxGridCellEditor
* wxGrid::GetDefaultEditorForCell(int row
, int col
) const
10245 wxString typeName
= m_table
->GetTypeName(row
, col
);
10246 return GetDefaultEditorForType(typeName
);
10249 wxGridCellRenderer
* wxGrid::GetDefaultRendererForCell(int row
, int col
) const
10251 wxString typeName
= m_table
->GetTypeName(row
, col
);
10252 return GetDefaultRendererForType(typeName
);
10255 wxGridCellEditor
* wxGrid::GetDefaultEditorForType(const wxString
& typeName
) const
10257 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
10258 if ( index
== wxNOT_FOUND
)
10260 wxFAIL_MSG(wxString::Format(wxT("Unknown data type name [%s]"), typeName
.c_str()));
10265 return m_typeRegistry
->GetEditor(index
);
10268 wxGridCellRenderer
* wxGrid::GetDefaultRendererForType(const wxString
& typeName
) const
10270 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
10271 if ( index
== wxNOT_FOUND
)
10273 wxFAIL_MSG(wxString::Format(wxT("Unknown data type name [%s]"), typeName
.c_str()));
10278 return m_typeRegistry
->GetRenderer(index
);
10281 // ----------------------------------------------------------------------------
10283 // ----------------------------------------------------------------------------
10285 void wxGrid::EnableDragRowSize( bool enable
)
10287 m_canDragRowSize
= enable
;
10290 void wxGrid::EnableDragColSize( bool enable
)
10292 m_canDragColSize
= enable
;
10295 void wxGrid::EnableDragGridSize( bool enable
)
10297 m_canDragGridSize
= enable
;
10300 void wxGrid::EnableDragCell( bool enable
)
10302 m_canDragCell
= enable
;
10305 void wxGrid::SetDefaultRowSize( int height
, bool resizeExistingRows
)
10307 m_defaultRowHeight
= wxMax( height
, m_minAcceptableRowHeight
);
10309 if ( resizeExistingRows
)
10311 // since we are resizing all rows to the default row size,
10312 // we can simply clear the row heights and row bottoms
10313 // arrays (which also allows us to take advantage of
10314 // some speed optimisations)
10315 m_rowHeights
.Empty();
10316 m_rowBottoms
.Empty();
10317 if ( !GetBatchCount() )
10322 void wxGrid::SetRowSize( int row
, int height
)
10324 wxCHECK_RET( row
>= 0 && row
< m_numRows
, _T("invalid row index") );
10326 // if < 0 then calculate new height from label
10330 wxArrayString lines
;
10331 wxClientDC
dc(m_rowLabelWin
);
10332 dc
.SetFont(GetLabelFont());
10333 StringToLines(GetRowLabelValue( row
), lines
);
10334 GetTextBoxSize( dc
, lines
, &w
, &h
);
10335 //check that it is not less than the minimal height
10336 height
= wxMax(h
, GetRowMinimalAcceptableHeight());
10339 // See comment in SetColSize
10340 if ( height
< GetRowMinimalAcceptableHeight())
10343 if ( m_rowHeights
.IsEmpty() )
10345 // need to really create the array
10349 int h
= wxMax( 0, height
);
10350 int diff
= h
- m_rowHeights
[row
];
10352 m_rowHeights
[row
] = h
;
10353 for ( int i
= row
; i
< m_numRows
; i
++ )
10355 m_rowBottoms
[i
] += diff
;
10358 if ( !GetBatchCount() )
10362 void wxGrid::SetDefaultColSize( int width
, bool resizeExistingCols
)
10364 // we dont allow zero default column width
10365 m_defaultColWidth
= wxMax( wxMax( width
, m_minAcceptableColWidth
), 1 );
10367 if ( resizeExistingCols
)
10369 // since we are resizing all columns to the default column size,
10370 // we can simply clear the col widths and col rights
10371 // arrays (which also allows us to take advantage of
10372 // some speed optimisations)
10373 m_colWidths
.Empty();
10374 m_colRights
.Empty();
10375 if ( !GetBatchCount() )
10380 void wxGrid::SetColSize( int col
, int width
)
10382 wxCHECK_RET( col
>= 0 && col
< m_numCols
, _T("invalid column index") );
10384 // if < 0 then calculate new width from label
10388 wxArrayString lines
;
10389 wxClientDC
dc(m_colWindow
);
10390 dc
.SetFont(GetLabelFont());
10391 StringToLines(GetColLabelValue(col
), lines
);
10392 if ( GetColLabelTextOrientation() == wxHORIZONTAL
)
10393 GetTextBoxSize( dc
, lines
, &w
, &h
);
10395 GetTextBoxSize( dc
, lines
, &h
, &w
);
10397 //check that it is not less than the minimal width
10398 width
= wxMax(width
, GetColMinimalAcceptableWidth());
10401 // should we check that it's bigger than GetColMinimalWidth(col) here?
10403 // No, because it is reasonable to assume the library user know's
10404 // what he is doing. However we should test against the weaker
10405 // constraint of minimalAcceptableWidth, as this breaks rendering
10407 // This test then fixes sf.net bug #645734
10409 if ( width
< GetColMinimalAcceptableWidth() )
10412 if ( m_colWidths
.IsEmpty() )
10414 // need to really create the array
10418 int w
= wxMax( 0, width
);
10419 int diff
= w
- m_colWidths
[col
];
10420 m_colWidths
[col
] = w
;
10422 for ( int colPos
= GetColPos(col
); colPos
< m_numCols
; colPos
++ )
10424 m_colRights
[GetColAt(colPos
)] += diff
;
10427 if ( !GetBatchCount() )
10434 void wxGrid::SetColMinimalWidth( int col
, int width
)
10436 if (width
> GetColMinimalAcceptableWidth())
10438 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)col
;
10439 m_colMinWidths
[key
] = width
;
10443 void wxGrid::SetRowMinimalHeight( int row
, int width
)
10445 if (width
> GetRowMinimalAcceptableHeight())
10447 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)row
;
10448 m_rowMinHeights
[key
] = width
;
10452 int wxGrid::GetColMinimalWidth(int col
) const
10454 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)col
;
10455 wxLongToLongHashMap::const_iterator it
= m_colMinWidths
.find(key
);
10457 return it
!= m_colMinWidths
.end() ? (int)it
->second
: m_minAcceptableColWidth
;
10460 int wxGrid::GetRowMinimalHeight(int row
) const
10462 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)row
;
10463 wxLongToLongHashMap::const_iterator it
= m_rowMinHeights
.find(key
);
10465 return it
!= m_rowMinHeights
.end() ? (int)it
->second
: m_minAcceptableRowHeight
;
10468 void wxGrid::SetColMinimalAcceptableWidth( int width
)
10470 // We do allow a width of 0 since this gives us
10471 // an easy way to temporarily hiding columns.
10473 m_minAcceptableColWidth
= width
;
10476 void wxGrid::SetRowMinimalAcceptableHeight( int height
)
10478 // We do allow a height of 0 since this gives us
10479 // an easy way to temporarily hiding rows.
10481 m_minAcceptableRowHeight
= height
;
10484 int wxGrid::GetColMinimalAcceptableWidth() const
10486 return m_minAcceptableColWidth
;
10489 int wxGrid::GetRowMinimalAcceptableHeight() const
10491 return m_minAcceptableRowHeight
;
10494 // ----------------------------------------------------------------------------
10496 // ----------------------------------------------------------------------------
10499 wxGrid::AutoSizeColOrRow(int colOrRow
, bool setAsMin
, wxGridDirection direction
)
10501 const bool column
= direction
== wxGRID_COLUMN
;
10503 wxClientDC
dc(m_gridWin
);
10505 // cancel editing of cell
10506 HideCellEditControl();
10507 SaveEditControlValue();
10509 // init both of them to avoid compiler warnings, even if we only need one
10517 wxCoord extent
, extentMax
= 0;
10518 int max
= column
? m_numRows
: m_numCols
;
10519 for ( int rowOrCol
= 0; rowOrCol
< max
; rowOrCol
++ )
10526 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
10527 wxGridCellRenderer
*renderer
= attr
->GetRenderer(this, row
, col
);
10530 wxSize size
= renderer
->GetBestSize(*this, *attr
, dc
, row
, col
);
10531 extent
= column
? size
.x
: size
.y
;
10532 if ( extent
> extentMax
)
10533 extentMax
= extent
;
10535 renderer
->DecRef();
10541 // now also compare with the column label extent
10543 dc
.SetFont( GetLabelFont() );
10547 dc
.GetMultiLineTextExtent( GetColLabelValue(col
), &w
, &h
);
10548 if ( GetColLabelTextOrientation() == wxVERTICAL
)
10552 dc
.GetMultiLineTextExtent( GetRowLabelValue(row
), &w
, &h
);
10554 extent
= column
? w
: h
;
10555 if ( extent
> extentMax
)
10556 extentMax
= extent
;
10560 // empty column - give default extent (notice that if extentMax is less
10561 // than default extent but != 0, it's OK)
10562 extentMax
= column
? m_defaultColWidth
: m_defaultRowHeight
;
10567 // leave some space around text
10575 // Ensure automatic width is not less than minimal width. See the
10576 // comment in SetColSize() for explanation of why this isn't done
10577 // in SetColSize().
10579 extentMax
= wxMax(extentMax
, GetColMinimalWidth(col
));
10581 SetColSize( col
, extentMax
);
10582 if ( !GetBatchCount() )
10584 if ( m_useNativeHeader
)
10586 GetColHeader()->UpdateColumn(col
);
10591 m_gridWin
->GetClientSize( &cw
, &ch
);
10592 wxRect
rect ( CellToRect( 0, col
) );
10594 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
10595 rect
.width
= cw
- rect
.x
;
10596 rect
.height
= m_colLabelHeight
;
10597 GetColLabelWindow()->Refresh( true, &rect
);
10603 // Ensure automatic width is not less than minimal height. See the
10604 // comment in SetColSize() for explanation of why this isn't done
10605 // in SetRowSize().
10607 extentMax
= wxMax(extentMax
, GetRowMinimalHeight(row
));
10609 SetRowSize(row
, extentMax
);
10610 if ( !GetBatchCount() )
10613 m_gridWin
->GetClientSize( &cw
, &ch
);
10614 wxRect
rect( CellToRect( row
, 0 ) );
10616 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
10617 rect
.width
= m_rowLabelWidth
;
10618 rect
.height
= ch
- rect
.y
;
10619 m_rowLabelWin
->Refresh( true, &rect
);
10626 SetColMinimalWidth(col
, extentMax
);
10628 SetRowMinimalHeight(row
, extentMax
);
10632 wxCoord
wxGrid::CalcColOrRowLabelAreaMinSize(wxGridDirection direction
)
10634 // calculate size for the rows or columns?
10635 const bool calcRows
= direction
== wxGRID_ROW
;
10637 wxClientDC
dc(calcRows
? GetGridRowLabelWindow()
10638 : GetGridColLabelWindow());
10639 dc
.SetFont(GetLabelFont());
10641 // which dimension should we take into account for calculations?
10643 // for columns, the text can be only horizontal so it's easy but for rows
10644 // we also have to take into account the text orientation
10646 useWidth
= calcRows
|| (GetColLabelTextOrientation() == wxVERTICAL
);
10648 wxArrayString lines
;
10649 wxCoord extentMax
= 0;
10651 const int numRowsOrCols
= calcRows
? m_numRows
: m_numCols
;
10652 for ( int rowOrCol
= 0; rowOrCol
< numRowsOrCols
; rowOrCol
++ )
10656 wxString label
= calcRows
? GetRowLabelValue(rowOrCol
)
10657 : GetColLabelValue(rowOrCol
);
10658 StringToLines(label
, lines
);
10661 GetTextBoxSize(dc
, lines
, &w
, &h
);
10663 const wxCoord extent
= useWidth
? w
: h
;
10664 if ( extent
> extentMax
)
10665 extentMax
= extent
;
10670 // empty column - give default extent (notice that if extentMax is less
10671 // than default extent but != 0, it's OK)
10672 extentMax
= calcRows
? GetDefaultRowLabelSize()
10673 : GetDefaultColLabelSize();
10676 // leave some space around text (taken from AutoSizeColOrRow)
10685 int wxGrid::SetOrCalcColumnSizes(bool calcOnly
, bool setAsMin
)
10687 int width
= m_rowLabelWidth
;
10689 wxGridUpdateLocker locker
;
10691 locker
.Create(this);
10693 for ( int col
= 0; col
< m_numCols
; col
++ )
10696 AutoSizeColumn(col
, setAsMin
);
10698 width
+= GetColWidth(col
);
10704 int wxGrid::SetOrCalcRowSizes(bool calcOnly
, bool setAsMin
)
10706 int height
= m_colLabelHeight
;
10708 wxGridUpdateLocker locker
;
10710 locker
.Create(this);
10712 for ( int row
= 0; row
< m_numRows
; row
++ )
10715 AutoSizeRow(row
, setAsMin
);
10717 height
+= GetRowHeight(row
);
10723 void wxGrid::AutoSize()
10725 wxGridUpdateLocker
locker(this);
10727 wxSize
size(SetOrCalcColumnSizes(false) - m_rowLabelWidth
+ m_extraWidth
,
10728 SetOrCalcRowSizes(false) - m_colLabelHeight
+ m_extraHeight
);
10730 // we know that we're not going to have scrollbars so disable them now to
10731 // avoid trouble in SetClientSize() which can otherwise set the correct
10732 // client size but also leave space for (not needed any more) scrollbars
10733 SetScrollbars(0, 0, 0, 0, 0, 0, true);
10735 // restore the scroll rate parameters overwritten by SetScrollbars()
10736 SetScrollRate(m_scrollLineX
, m_scrollLineY
);
10738 SetClientSize(size
.x
+ m_rowLabelWidth
, size
.y
+ m_colLabelHeight
);
10741 void wxGrid::AutoSizeRowLabelSize( int row
)
10743 // Hide the edit control, so it
10744 // won't interfere with drag-shrinking.
10745 if ( IsCellEditControlShown() )
10747 HideCellEditControl();
10748 SaveEditControlValue();
10751 // autosize row height depending on label text
10752 SetRowSize(row
, -1);
10756 void wxGrid::AutoSizeColLabelSize( int col
)
10758 // Hide the edit control, so it
10759 // won't interfere with drag-shrinking.
10760 if ( IsCellEditControlShown() )
10762 HideCellEditControl();
10763 SaveEditControlValue();
10766 // autosize column width depending on label text
10767 SetColSize(col
, -1);
10771 wxSize
wxGrid::DoGetBestSize() const
10773 wxGrid
*self
= (wxGrid
*)this; // const_cast
10775 // we do the same as in AutoSize() here with the exception that we don't
10776 // change the column/row sizes, only calculate them
10777 wxSize
size(self
->SetOrCalcColumnSizes(true) - m_rowLabelWidth
+ m_extraWidth
,
10778 self
->SetOrCalcRowSizes(true) - m_colLabelHeight
+ m_extraHeight
);
10780 // NOTE: This size should be cached, but first we need to add calls to
10781 // InvalidateBestSize everywhere that could change the results of this
10783 // CacheBestSize(size);
10785 return wxSize(size
.x
+ m_rowLabelWidth
, size
.y
+ m_colLabelHeight
)
10786 + GetWindowBorderSize();
10794 wxPen
& wxGrid::GetDividerPen() const
10799 // ----------------------------------------------------------------------------
10800 // cell value accessor functions
10801 // ----------------------------------------------------------------------------
10803 void wxGrid::SetCellValue( int row
, int col
, const wxString
& s
)
10807 m_table
->SetValue( row
, col
, s
);
10808 if ( !GetBatchCount() )
10811 wxRect
rect( CellToRect( row
, col
) );
10813 rect
.width
= m_gridWin
->GetClientSize().GetWidth();
10814 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
10815 m_gridWin
->Refresh( false, &rect
);
10818 if ( m_currentCellCoords
.GetRow() == row
&&
10819 m_currentCellCoords
.GetCol() == col
&&
10820 IsCellEditControlShown())
10821 // Note: If we are using IsCellEditControlEnabled,
10822 // this interacts badly with calling SetCellValue from
10823 // an EVT_GRID_CELL_CHANGE handler.
10825 HideCellEditControl();
10826 ShowCellEditControl(); // will reread data from table
10831 // ----------------------------------------------------------------------------
10832 // block, row and column selection
10833 // ----------------------------------------------------------------------------
10835 void wxGrid::SelectRow( int row
, bool addToSelected
)
10837 if ( !m_selection
)
10840 if ( !addToSelected
)
10843 m_selection
->SelectRow(row
);
10846 void wxGrid::SelectCol( int col
, bool addToSelected
)
10848 if ( !m_selection
)
10851 if ( !addToSelected
)
10854 m_selection
->SelectCol(col
);
10857 void wxGrid::SelectBlock(int topRow
, int leftCol
, int bottomRow
, int rightCol
,
10858 bool addToSelected
)
10860 if ( !m_selection
)
10863 if ( !addToSelected
)
10866 m_selection
->SelectBlock(topRow
, leftCol
, bottomRow
, rightCol
);
10869 void wxGrid::SelectAll()
10871 if ( m_numRows
> 0 && m_numCols
> 0 )
10874 m_selection
->SelectBlock( 0, 0, m_numRows
- 1, m_numCols
- 1 );
10878 // ----------------------------------------------------------------------------
10879 // cell, row and col deselection
10880 // ----------------------------------------------------------------------------
10882 void wxGrid::DeselectLine(int line
, const wxGridOperations
& oper
)
10884 if ( !m_selection
)
10887 const wxGridSelectionModes mode
= m_selection
->GetSelectionMode();
10888 if ( mode
== oper
.GetSelectionMode() )
10890 const wxGridCellCoords
c(oper
.MakeCoords(line
, 0));
10891 if ( m_selection
->IsInSelection(c
) )
10892 m_selection
->ToggleCellSelection(c
);
10894 else if ( mode
!= oper
.Dual().GetSelectionMode() )
10896 const int nOther
= oper
.Dual().GetNumberOfLines(this);
10897 for ( int i
= 0; i
< nOther
; i
++ )
10899 const wxGridCellCoords
c(oper
.MakeCoords(line
, i
));
10900 if ( m_selection
->IsInSelection(c
) )
10901 m_selection
->ToggleCellSelection(c
);
10904 //else: can only select orthogonal lines so no lines in this direction
10905 // could have been selected anyhow
10908 void wxGrid::DeselectRow(int row
)
10910 DeselectLine(row
, wxGridRowOperations());
10913 void wxGrid::DeselectCol(int col
)
10915 DeselectLine(col
, wxGridColumnOperations());
10918 void wxGrid::DeselectCell( int row
, int col
)
10920 if ( m_selection
&& m_selection
->IsInSelection(row
, col
) )
10921 m_selection
->ToggleCellSelection(row
, col
);
10924 bool wxGrid::IsSelection() const
10926 return ( m_selection
&& (m_selection
->IsSelection() ||
10927 ( m_selectedBlockTopLeft
!= wxGridNoCellCoords
&&
10928 m_selectedBlockBottomRight
!= wxGridNoCellCoords
) ) );
10931 bool wxGrid::IsInSelection( int row
, int col
) const
10933 return ( m_selection
&& (m_selection
->IsInSelection( row
, col
) ||
10934 ( row
>= m_selectedBlockTopLeft
.GetRow() &&
10935 col
>= m_selectedBlockTopLeft
.GetCol() &&
10936 row
<= m_selectedBlockBottomRight
.GetRow() &&
10937 col
<= m_selectedBlockBottomRight
.GetCol() )) );
10940 wxGridCellCoordsArray
wxGrid::GetSelectedCells() const
10944 wxGridCellCoordsArray a
;
10948 return m_selection
->m_cellSelection
;
10951 wxGridCellCoordsArray
wxGrid::GetSelectionBlockTopLeft() const
10955 wxGridCellCoordsArray a
;
10959 return m_selection
->m_blockSelectionTopLeft
;
10962 wxGridCellCoordsArray
wxGrid::GetSelectionBlockBottomRight() const
10966 wxGridCellCoordsArray a
;
10970 return m_selection
->m_blockSelectionBottomRight
;
10973 wxArrayInt
wxGrid::GetSelectedRows() const
10981 return m_selection
->m_rowSelection
;
10984 wxArrayInt
wxGrid::GetSelectedCols() const
10992 return m_selection
->m_colSelection
;
10995 void wxGrid::ClearSelection()
10997 wxRect r1
= BlockToDeviceRect(m_selectedBlockTopLeft
,
10998 m_selectedBlockBottomRight
);
10999 wxRect r2
= BlockToDeviceRect(m_currentCellCoords
,
11000 m_selectedBlockCorner
);
11002 m_selectedBlockTopLeft
=
11003 m_selectedBlockBottomRight
=
11004 m_selectedBlockCorner
= wxGridNoCellCoords
;
11006 Refresh( false, &r1
);
11007 Refresh( false, &r2
);
11010 m_selection
->ClearSelection();
11013 // This function returns the rectangle that encloses the given block
11014 // in device coords clipped to the client size of the grid window.
11016 wxRect
wxGrid::BlockToDeviceRect( const wxGridCellCoords
& topLeft
,
11017 const wxGridCellCoords
& bottomRight
) const
11020 wxRect tempCellRect
= CellToRect(topLeft
);
11021 if ( tempCellRect
!= wxGridNoCellRect
)
11023 resultRect
= tempCellRect
;
11027 resultRect
= wxRect(0, 0, 0, 0);
11030 tempCellRect
= CellToRect(bottomRight
);
11031 if ( tempCellRect
!= wxGridNoCellRect
)
11033 resultRect
+= tempCellRect
;
11037 // If both inputs were "wxGridNoCellRect," then there's nothing to do.
11038 return wxGridNoCellRect
;
11041 // Ensure that left/right and top/bottom pairs are in order.
11042 int left
= resultRect
.GetLeft();
11043 int top
= resultRect
.GetTop();
11044 int right
= resultRect
.GetRight();
11045 int bottom
= resultRect
.GetBottom();
11047 int leftCol
= topLeft
.GetCol();
11048 int topRow
= topLeft
.GetRow();
11049 int rightCol
= bottomRight
.GetCol();
11050 int bottomRow
= bottomRight
.GetRow();
11059 leftCol
= rightCol
;
11070 topRow
= bottomRow
;
11074 // The following loop is ONLY necessary to detect and handle merged cells.
11076 m_gridWin
->GetClientSize( &cw
, &ch
);
11078 // Get the origin coordinates: notice that they will be negative if the
11079 // grid is scrolled downwards/to the right.
11080 int gridOriginX
= 0;
11081 int gridOriginY
= 0;
11082 CalcScrolledPosition(gridOriginX
, gridOriginY
, &gridOriginX
, &gridOriginY
);
11084 int onScreenLeftmostCol
= internalXToCol(-gridOriginX
);
11085 int onScreenUppermostRow
= internalYToRow(-gridOriginY
);
11087 int onScreenRightmostCol
= internalXToCol(-gridOriginX
+ cw
);
11088 int onScreenBottommostRow
= internalYToRow(-gridOriginY
+ ch
);
11090 // Bound our loop so that we only examine the portion of the selected block
11091 // that is shown on screen. Therefore, we compare the Top-Left block values
11092 // to the Top-Left screen values, and the Bottom-Right block values to the
11093 // Bottom-Right screen values, choosing appropriately.
11094 const int visibleTopRow
= wxMax(topRow
, onScreenUppermostRow
);
11095 const int visibleBottomRow
= wxMin(bottomRow
, onScreenBottommostRow
);
11096 const int visibleLeftCol
= wxMax(leftCol
, onScreenLeftmostCol
);
11097 const int visibleRightCol
= wxMin(rightCol
, onScreenRightmostCol
);
11099 for ( int j
= visibleTopRow
; j
<= visibleBottomRow
; j
++ )
11101 for ( int i
= visibleLeftCol
; i
<= visibleRightCol
; i
++ )
11103 if ( (j
== visibleTopRow
) || (j
== visibleBottomRow
) ||
11104 (i
== visibleLeftCol
) || (i
== visibleRightCol
) )
11106 tempCellRect
= CellToRect( j
, i
);
11108 if (tempCellRect
.x
< left
)
11109 left
= tempCellRect
.x
;
11110 if (tempCellRect
.y
< top
)
11111 top
= tempCellRect
.y
;
11112 if (tempCellRect
.x
+ tempCellRect
.width
> right
)
11113 right
= tempCellRect
.x
+ tempCellRect
.width
;
11114 if (tempCellRect
.y
+ tempCellRect
.height
> bottom
)
11115 bottom
= tempCellRect
.y
+ tempCellRect
.height
;
11119 i
= visibleRightCol
; // jump over inner cells.
11124 // Convert to scrolled coords
11125 CalcScrolledPosition( left
, top
, &left
, &top
);
11126 CalcScrolledPosition( right
, bottom
, &right
, &bottom
);
11128 if (right
< 0 || bottom
< 0 || left
> cw
|| top
> ch
)
11129 return wxRect(0,0,0,0);
11131 resultRect
.SetLeft( wxMax(0, left
) );
11132 resultRect
.SetTop( wxMax(0, top
) );
11133 resultRect
.SetRight( wxMin(cw
, right
) );
11134 resultRect
.SetBottom( wxMin(ch
, bottom
) );
11139 // ----------------------------------------------------------------------------
11141 // ----------------------------------------------------------------------------
11143 #if wxUSE_DRAG_AND_DROP
11145 // this allow setting drop target directly on wxGrid
11146 void wxGrid::SetDropTarget(wxDropTarget
*dropTarget
)
11148 GetGridWindow()->SetDropTarget(dropTarget
);
11151 #endif // wxUSE_DRAG_AND_DROP
11153 // ----------------------------------------------------------------------------
11154 // grid event classes
11155 // ----------------------------------------------------------------------------
11157 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent
, wxNotifyEvent
)
11159 wxGridEvent::wxGridEvent( int id
, wxEventType type
, wxObject
* obj
,
11160 int row
, int col
, int x
, int y
, bool sel
,
11161 bool control
, bool shift
, bool alt
, bool meta
)
11162 : wxNotifyEvent( type
, id
),
11163 wxKeyboardState(control
, shift
, alt
, meta
)
11165 Init(row
, col
, x
, y
, sel
);
11167 SetEventObject(obj
);
11170 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent
, wxNotifyEvent
)
11172 wxGridSizeEvent::wxGridSizeEvent( int id
, wxEventType type
, wxObject
* obj
,
11173 int rowOrCol
, int x
, int y
,
11174 bool control
, bool shift
, bool alt
, bool meta
)
11175 : wxNotifyEvent( type
, id
),
11176 wxKeyboardState(control
, shift
, alt
, meta
)
11178 Init(rowOrCol
, x
, y
);
11180 SetEventObject(obj
);
11184 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent
, wxNotifyEvent
)
11186 wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id
, wxEventType type
, wxObject
* obj
,
11187 const wxGridCellCoords
& topLeft
,
11188 const wxGridCellCoords
& bottomRight
,
11189 bool sel
, bool control
,
11190 bool shift
, bool alt
, bool meta
)
11191 : wxNotifyEvent( type
, id
),
11192 wxKeyboardState(control
, shift
, alt
, meta
)
11194 Init(topLeft
, bottomRight
, sel
);
11196 SetEventObject(obj
);
11200 IMPLEMENT_DYNAMIC_CLASS(wxGridEditorCreatedEvent
, wxCommandEvent
)
11202 wxGridEditorCreatedEvent::wxGridEditorCreatedEvent(int id
, wxEventType type
,
11203 wxObject
* obj
, int row
,
11204 int col
, wxControl
* ctrl
)
11205 : wxCommandEvent(type
, id
)
11207 SetEventObject(obj
);
11213 #endif // wxUSE_GRID