1 ///////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/grid.cpp
3 // Purpose: wxGrid and related classes
4 // Author: Michael Bedward (based on code by Julian Smart, Robin Dunn)
5 // Modified by: Robin Dunn, Vadim Zeitlin, Santiago Palacios
8 // Copyright: (c) Michael Bedward (mbedward@ozemail.com.au)
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
15 - Replace use of wxINVERT with wxOverlay
16 - Make Begin/EndBatch() the same as the generic Freeze/Thaw()
17 - Review the column reordering code, it's a mess.
18 - Implement row reordering after dealing with the columns.
21 // For compilers that support precompilation, includes "wx/wx.h".
22 #include "wx/wxprec.h"
34 #include "wx/dcclient.h"
35 #include "wx/settings.h"
37 #include "wx/textctrl.h"
38 #include "wx/checkbox.h"
39 #include "wx/combobox.h"
40 #include "wx/valtext.h"
43 #include "wx/listbox.h"
46 #include "wx/textfile.h"
47 #include "wx/spinctrl.h"
48 #include "wx/tokenzr.h"
49 #include "wx/renderer.h"
50 #include "wx/headerctrl.h"
52 #include "wx/generic/gridsel.h"
54 const char wxGridNameStr
[] = "grid";
56 #if defined(__WXMOTIF__)
57 #define WXUNUSED_MOTIF(identifier) WXUNUSED(identifier)
59 #define WXUNUSED_MOTIF(identifier) identifier
62 #if defined(__WXGTK__)
63 #define WXUNUSED_GTK(identifier) WXUNUSED(identifier)
65 #define WXUNUSED_GTK(identifier) identifier
68 // Required for wxIs... functions
71 // ----------------------------------------------------------------------------
73 // ----------------------------------------------------------------------------
75 WX_DEFINE_ARRAY_WITH_DECL_PTR(wxGridCellAttr
*, wxArrayAttrs
,
76 class WXDLLIMPEXP_ADV
);
78 struct wxGridCellWithAttr
80 wxGridCellWithAttr(int row
, int col
, wxGridCellAttr
*attr_
)
81 : coords(row
, col
), attr(attr_
)
86 wxGridCellWithAttr(const wxGridCellWithAttr
& other
)
87 : coords(other
.coords
),
93 wxGridCellWithAttr
& operator=(const wxGridCellWithAttr
& other
)
95 coords
= other
.coords
;
96 if (attr
!= other
.attr
)
105 void ChangeAttr(wxGridCellAttr
* new_attr
)
107 if (attr
!= new_attr
)
109 // "Delete" (i.e. DecRef) the old attribute.
112 // Take ownership of the new attribute, i.e. no IncRef.
116 ~wxGridCellWithAttr()
121 wxGridCellCoords coords
;
122 wxGridCellAttr
*attr
;
125 WX_DECLARE_OBJARRAY_WITH_DECL(wxGridCellWithAttr
, wxGridCellWithAttrArray
,
126 class WXDLLIMPEXP_ADV
);
128 #include "wx/arrimpl.cpp"
130 WX_DEFINE_OBJARRAY(wxGridCellCoordsArray
)
131 WX_DEFINE_OBJARRAY(wxGridCellWithAttrArray
)
133 // ----------------------------------------------------------------------------
135 // ----------------------------------------------------------------------------
137 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_LEFT_CLICK
)
138 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_RIGHT_CLICK
)
139 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_LEFT_DCLICK
)
140 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_RIGHT_DCLICK
)
141 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_BEGIN_DRAG
)
142 DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_LEFT_CLICK
)
143 DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_RIGHT_CLICK
)
144 DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_LEFT_DCLICK
)
145 DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_RIGHT_DCLICK
)
146 DEFINE_EVENT_TYPE(wxEVT_GRID_ROW_SIZE
)
147 DEFINE_EVENT_TYPE(wxEVT_GRID_COL_SIZE
)
148 DEFINE_EVENT_TYPE(wxEVT_GRID_COL_MOVE
)
149 DEFINE_EVENT_TYPE(wxEVT_GRID_RANGE_SELECT
)
150 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_CHANGE
)
151 DEFINE_EVENT_TYPE(wxEVT_GRID_SELECT_CELL
)
152 DEFINE_EVENT_TYPE(wxEVT_GRID_EDITOR_SHOWN
)
153 DEFINE_EVENT_TYPE(wxEVT_GRID_EDITOR_HIDDEN
)
154 DEFINE_EVENT_TYPE(wxEVT_GRID_EDITOR_CREATED
)
156 // ----------------------------------------------------------------------------
158 // ----------------------------------------------------------------------------
160 // header column providing access to the column information stored in wxGrid
161 // via wxHeaderColumn interface
162 class wxGridHeaderColumn
: public wxHeaderColumn
165 wxGridHeaderColumn(wxGrid
*grid
, int col
)
171 virtual wxString
GetTitle() const { return m_grid
->GetColLabelValue(m_col
); }
172 virtual wxBitmap
GetBitmap() const { return wxNullBitmap
; }
173 virtual int GetWidth() const { return m_grid
->GetColSize(m_col
); }
174 virtual int GetMinWidth() const { return 0; }
175 virtual wxAlignment
GetAlignment() const
179 m_grid
->GetColLabelAlignment(&horz
, &vert
);
181 return static_cast<wxAlignment
>(horz
);
184 virtual int GetFlags() const
187 if ( m_grid
->CanDragColSize() )
188 flags
|= wxCOL_RESIZABLE
;
189 if ( m_grid
->CanDragColMove() )
190 flags
|= wxCOL_REORDERABLE
;
195 // TODO: currently there is no support for sorting
196 virtual bool IsSortKey() const { return false; }
197 virtual bool IsSortOrderAscending() const { return false; }
200 wxGrid
* const m_grid
;
203 DECLARE_NO_ASSIGN_CLASS(wxGridHeaderColumn
)
206 // header control retreiving column information from the grid
207 class wxGridHeaderCtrl
: public wxHeaderCtrl
210 wxGridHeaderCtrl(wxGrid
*owner
)
211 : wxHeaderCtrl(owner
,
215 owner
->CanDragColMove() ? wxHD_DRAGDROP
: 0)
220 virtual wxHeaderColumn
& GetColumn(unsigned int idx
)
222 return m_columns
[idx
];
226 wxGrid
*GetOwner() const { return static_cast<wxGrid
*>(GetParent()); }
228 // override the base class method to update our m_columns array
229 virtual void OnColumnCountChanging(unsigned int count
)
231 const unsigned countOld
= m_columns
.size();
232 if ( count
< countOld
)
234 // just discard the columns which don't exist any more (notice that
235 // we can't use resize() here as it would require the vector
236 // value_type, i.e. wxGridHeaderColumn to be default constructible,
238 m_columns
.erase(m_columns
.begin() + count
, m_columns
.end());
240 else // new columns added
242 // add columns for the new elements
243 for ( unsigned n
= countOld
; n
< count
; n
++ )
244 m_columns
.push_back(wxGridHeaderColumn(GetOwner(), n
));
248 // override to implement column auto sizing
249 virtual bool UpdateColumnWidthToFit(unsigned int idx
, int widthTitle
)
251 GetOwner()->SetColSize(idx
, widthTitle
);
257 // event handlers forwarding wxHeaderCtrl events to wxGrid
258 void OnBeginResize(wxHeaderCtrlEvent
& event
)
260 GetOwner()->DoStartResizeCol(event
.GetColumn());
265 void OnResizing(wxHeaderCtrlEvent
& event
)
267 GetOwner()->DoUpdateResizeColWidth(event
.GetWidth());
270 void OnEndResize(wxHeaderCtrlEvent
& event
)
272 GetOwner()->DoEndDragResizeCol();
277 void OnEndReorder(wxHeaderCtrlEvent
& event
)
279 event
.Skip(); // TODO: position it at event.GetNewOrder()
282 wxVector
<wxGridHeaderColumn
> m_columns
;
284 DECLARE_EVENT_TABLE()
285 DECLARE_NO_COPY_CLASS(wxGridHeaderCtrl
)
288 BEGIN_EVENT_TABLE(wxGridHeaderCtrl
, wxHeaderCtrl
)
289 EVT_HEADER_BEGIN_RESIZE(wxID_ANY
, wxGridHeaderCtrl::OnBeginResize
)
290 EVT_HEADER_RESIZING(wxID_ANY
, wxGridHeaderCtrl::OnResizing
)
291 EVT_HEADER_END_RESIZE(wxID_ANY
, wxGridHeaderCtrl::OnEndResize
)
293 EVT_HEADER_END_REORDER(wxID_ANY
, wxGridHeaderCtrl::OnEndReorder
)
296 // common base class for various grid subwindows
297 class WXDLLIMPEXP_ADV wxGridSubwindow
: public wxWindow
300 wxGridSubwindow(wxGrid
*owner
,
301 int additionalStyle
= 0,
302 const wxString
& name
= wxPanelNameStr
)
303 : wxWindow(owner
, wxID_ANY
,
304 wxDefaultPosition
, wxDefaultSize
,
305 wxBORDER_NONE
| additionalStyle
,
311 virtual bool AcceptsFocus() const { return false; }
313 wxGrid
*GetOwner() { return m_owner
; }
316 void OnMouseCaptureLost(wxMouseCaptureLostEvent
& event
);
320 DECLARE_EVENT_TABLE()
321 DECLARE_NO_COPY_CLASS(wxGridSubwindow
)
324 class WXDLLIMPEXP_ADV wxGridRowLabelWindow
: public wxGridSubwindow
327 wxGridRowLabelWindow(wxGrid
*parent
)
328 : wxGridSubwindow(parent
)
334 void OnPaint( wxPaintEvent
& event
);
335 void OnMouseEvent( wxMouseEvent
& event
);
336 void OnMouseWheel( wxMouseEvent
& event
);
338 DECLARE_EVENT_TABLE()
339 DECLARE_NO_COPY_CLASS(wxGridRowLabelWindow
)
343 class WXDLLIMPEXP_ADV wxGridColLabelWindow
: public wxGridSubwindow
346 wxGridColLabelWindow(wxGrid
*parent
)
347 : wxGridSubwindow(parent
)
353 void OnPaint( wxPaintEvent
& event
);
354 void OnMouseEvent( wxMouseEvent
& event
);
355 void OnMouseWheel( wxMouseEvent
& event
);
357 DECLARE_EVENT_TABLE()
358 DECLARE_NO_COPY_CLASS(wxGridColLabelWindow
)
362 class WXDLLIMPEXP_ADV wxGridCornerLabelWindow
: public wxGridSubwindow
365 wxGridCornerLabelWindow(wxGrid
*parent
)
366 : wxGridSubwindow(parent
)
371 void OnMouseEvent( wxMouseEvent
& event
);
372 void OnMouseWheel( wxMouseEvent
& event
);
373 void OnPaint( wxPaintEvent
& event
);
375 DECLARE_EVENT_TABLE()
376 DECLARE_NO_COPY_CLASS(wxGridCornerLabelWindow
)
379 class WXDLLIMPEXP_ADV wxGridWindow
: public wxGridSubwindow
382 wxGridWindow(wxGrid
*parent
)
383 : wxGridSubwindow(parent
,
384 wxWANTS_CHARS
| wxCLIP_CHILDREN
,
390 virtual void ScrollWindow( int dx
, int dy
, const wxRect
*rect
);
392 virtual bool AcceptsFocus() const { return true; }
395 void OnPaint( wxPaintEvent
&event
);
396 void OnMouseWheel( wxMouseEvent
& event
);
397 void OnMouseEvent( wxMouseEvent
& event
);
398 void OnKeyDown( wxKeyEvent
& );
399 void OnKeyUp( wxKeyEvent
& );
400 void OnChar( wxKeyEvent
& );
401 void OnEraseBackground( wxEraseEvent
& );
402 void OnFocus( wxFocusEvent
& );
404 DECLARE_EVENT_TABLE()
405 DECLARE_NO_COPY_CLASS(wxGridWindow
)
409 class wxGridCellEditorEvtHandler
: public wxEvtHandler
412 wxGridCellEditorEvtHandler(wxGrid
* grid
, wxGridCellEditor
* editor
)
419 void OnKillFocus(wxFocusEvent
& event
);
420 void OnKeyDown(wxKeyEvent
& event
);
421 void OnChar(wxKeyEvent
& event
);
423 void SetInSetFocus(bool inSetFocus
) { m_inSetFocus
= inSetFocus
; }
427 wxGridCellEditor
*m_editor
;
429 // Work around the fact that a focus kill event can be sent to
430 // a combobox within a set focus event.
433 DECLARE_EVENT_TABLE()
434 DECLARE_DYNAMIC_CLASS(wxGridCellEditorEvtHandler
)
435 DECLARE_NO_COPY_CLASS(wxGridCellEditorEvtHandler
)
439 IMPLEMENT_ABSTRACT_CLASS(wxGridCellEditorEvtHandler
, wxEvtHandler
)
441 BEGIN_EVENT_TABLE( wxGridCellEditorEvtHandler
, wxEvtHandler
)
442 EVT_KILL_FOCUS( wxGridCellEditorEvtHandler::OnKillFocus
)
443 EVT_KEY_DOWN( wxGridCellEditorEvtHandler::OnKeyDown
)
444 EVT_CHAR( wxGridCellEditorEvtHandler::OnChar
)
448 // ----------------------------------------------------------------------------
449 // the internal data representation used by wxGridCellAttrProvider
450 // ----------------------------------------------------------------------------
452 // this class stores attributes set for cells
453 class WXDLLIMPEXP_ADV wxGridCellAttrData
456 void SetAttr(wxGridCellAttr
*attr
, int row
, int col
);
457 wxGridCellAttr
*GetAttr(int row
, int col
) const;
458 void UpdateAttrRows( size_t pos
, int numRows
);
459 void UpdateAttrCols( size_t pos
, int numCols
);
462 // searches for the attr for given cell, returns wxNOT_FOUND if not found
463 int FindIndex(int row
, int col
) const;
465 wxGridCellWithAttrArray m_attrs
;
468 // this class stores attributes set for rows or columns
469 class WXDLLIMPEXP_ADV wxGridRowOrColAttrData
472 // empty ctor to suppress warnings
473 wxGridRowOrColAttrData() {}
474 ~wxGridRowOrColAttrData();
476 void SetAttr(wxGridCellAttr
*attr
, int rowOrCol
);
477 wxGridCellAttr
*GetAttr(int rowOrCol
) const;
478 void UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
);
481 wxArrayInt m_rowsOrCols
;
482 wxArrayAttrs m_attrs
;
485 // NB: this is just a wrapper around 3 objects: one which stores cell
486 // attributes, and 2 others for row/col ones
487 class WXDLLIMPEXP_ADV wxGridCellAttrProviderData
490 wxGridCellAttrData m_cellAttrs
;
491 wxGridRowOrColAttrData m_rowAttrs
,
496 // ----------------------------------------------------------------------------
497 // data structures used for the data type registry
498 // ----------------------------------------------------------------------------
500 struct wxGridDataTypeInfo
502 wxGridDataTypeInfo(const wxString
& typeName
,
503 wxGridCellRenderer
* renderer
,
504 wxGridCellEditor
* editor
)
505 : m_typeName(typeName
), m_renderer(renderer
), m_editor(editor
)
508 ~wxGridDataTypeInfo()
510 wxSafeDecRef(m_renderer
);
511 wxSafeDecRef(m_editor
);
515 wxGridCellRenderer
* m_renderer
;
516 wxGridCellEditor
* m_editor
;
518 DECLARE_NO_COPY_CLASS(wxGridDataTypeInfo
)
522 WX_DEFINE_ARRAY_WITH_DECL_PTR(wxGridDataTypeInfo
*, wxGridDataTypeInfoArray
,
523 class WXDLLIMPEXP_ADV
);
526 class WXDLLIMPEXP_ADV wxGridTypeRegistry
529 wxGridTypeRegistry() {}
530 ~wxGridTypeRegistry();
532 void RegisterDataType(const wxString
& typeName
,
533 wxGridCellRenderer
* renderer
,
534 wxGridCellEditor
* editor
);
536 // find one of already registered data types
537 int FindRegisteredDataType(const wxString
& typeName
);
539 // try to FindRegisteredDataType(), if this fails and typeName is one of
540 // standard typenames, register it and return its index
541 int FindDataType(const wxString
& typeName
);
543 // try to FindDataType(), if it fails see if it is not one of already
544 // registered data types with some params in which case clone the
545 // registered data type and set params for it
546 int FindOrCloneDataType(const wxString
& typeName
);
548 wxGridCellRenderer
* GetRenderer(int index
);
549 wxGridCellEditor
* GetEditor(int index
);
552 wxGridDataTypeInfoArray m_typeinfo
;
555 // ----------------------------------------------------------------------------
556 // operations classes abstracting the difference between operating on rows and
558 // ----------------------------------------------------------------------------
560 // This class allows to write a function only once because by using its methods
561 // it will apply to both columns and rows.
563 // This is an abstract interface definition, the two concrete implementations
564 // below should be used when working with rows and columns respectively.
565 class wxGridOperations
568 // Returns the operations in the other direction, i.e. wxGridRowOperations
569 // if this object is a wxGridColumnOperations and vice versa.
570 virtual wxGridOperations
& Dual() const = 0;
572 // Return the number of rows or columns.
573 virtual int GetNumberOfLines(const wxGrid
*grid
) const = 0;
575 // Return the selection mode which allows selecting rows or columns.
576 virtual wxGrid::wxGridSelectionModes
GetSelectionMode() const = 0;
578 // Make a wxGridCellCoords from the given components: thisDir is row or
579 // column and otherDir is column or row
580 virtual wxGridCellCoords
MakeCoords(int thisDir
, int otherDir
) const = 0;
582 // Calculate the scrolled position of the given abscissa or ordinate.
583 virtual int CalcScrolledPosition(wxGrid
*grid
, int pos
) const = 0;
585 // Selects the horizontal or vertical component from the given object.
586 virtual int Select(const wxGridCellCoords
& coords
) const = 0;
587 virtual int Select(const wxPoint
& pt
) const = 0;
588 virtual int Select(const wxSize
& sz
) const = 0;
589 virtual int Select(const wxRect
& r
) const = 0;
590 virtual int& Select(wxRect
& r
) const = 0;
592 // Returns width or height of the rectangle
593 virtual int& SelectSize(wxRect
& r
) const = 0;
595 // Make a wxSize such that Select() applied to it returns first component
596 virtual wxSize
MakeSize(int first
, int second
) const = 0;
598 // Sets the row or column component of the given cell coordinates
599 virtual void Set(wxGridCellCoords
& coords
, int line
) const = 0;
602 // Draws a line parallel to the row or column, i.e. horizontal or vertical:
603 // pos is the horizontal or vertical position of the line and start and end
604 // are the coordinates of the line extremities in the other direction
606 DrawParallelLine(wxDC
& dc
, int start
, int end
, int pos
) const = 0;
608 // Draw a horizontal or vertical line across the given rectangle
609 // (this is implemented in terms of above and uses Select() to extract
610 // start and end from the given rectangle)
611 void DrawParallelLineInRect(wxDC
& dc
, const wxRect
& rect
, int pos
) const
613 const int posStart
= Select(rect
.GetPosition());
614 DrawParallelLine(dc
, posStart
, posStart
+ Select(rect
.GetSize()), pos
);
618 // Return the row or column at the given pixel coordinate.
620 PosToLine(const wxGrid
*grid
, int pos
, bool clip
= false) const = 0;
622 // Get the top/left position, in pixels, of the given row or column
623 virtual int GetLineStartPos(const wxGrid
*grid
, int line
) const = 0;
625 // Get the bottom/right position, in pixels, of the given row or column
626 virtual int GetLineEndPos(const wxGrid
*grid
, int line
) const = 0;
628 // Get the height/width of the given row/column
629 virtual int GetLineSize(const wxGrid
*grid
, int line
) const = 0;
631 // Get wxGrid::m_rowBottoms/m_colRights array
632 virtual const wxArrayInt
& GetLineEnds(const wxGrid
*grid
) const = 0;
634 // Get default height row height or column width
635 virtual int GetDefaultLineSize(const wxGrid
*grid
) const = 0;
637 // Return the minimal acceptable row height or column width
638 virtual int GetMinimalAcceptableLineSize(const wxGrid
*grid
) const = 0;
640 // Return the minimal row height or column width
641 virtual int GetMinimalLineSize(const wxGrid
*grid
, int line
) const = 0;
643 // Set the row height or column width
644 virtual void SetLineSize(wxGrid
*grid
, int line
, int size
) const = 0;
646 // True if rows/columns can be resized by user
647 virtual bool CanResizeLines(const wxGrid
*grid
) const = 0;
650 // Return the index of the line at the given position
652 // NB: currently this is always identity for the rows as reordering is only
653 // implemented for the lines
654 virtual int GetLineAt(const wxGrid
*grid
, int line
) const = 0;
657 // Get the row or column label window
658 virtual wxWindow
*GetHeaderWindow(wxGrid
*grid
) const = 0;
660 // Get the width or height of the row or column label window
661 virtual int GetHeaderWindowSize(wxGrid
*grid
) const = 0;
664 // This class is never used polymorphically but give it a virtual dtor
665 // anyhow to suppress g++ complaints about it
666 virtual ~wxGridOperations() { }
669 class wxGridRowOperations
: public wxGridOperations
672 virtual wxGridOperations
& Dual() const;
674 virtual int GetNumberOfLines(const wxGrid
*grid
) const
675 { return grid
->GetNumberRows(); }
677 virtual wxGrid::wxGridSelectionModes
GetSelectionMode() const
678 { return wxGrid::wxGridSelectRows
; }
680 virtual wxGridCellCoords
MakeCoords(int thisDir
, int otherDir
) const
681 { return wxGridCellCoords(thisDir
, otherDir
); }
683 virtual int CalcScrolledPosition(wxGrid
*grid
, int pos
) const
684 { return grid
->CalcScrolledPosition(wxPoint(pos
, 0)).x
; }
686 virtual int Select(const wxGridCellCoords
& c
) const { return c
.GetRow(); }
687 virtual int Select(const wxPoint
& pt
) const { return pt
.x
; }
688 virtual int Select(const wxSize
& sz
) const { return sz
.x
; }
689 virtual int Select(const wxRect
& r
) const { return r
.x
; }
690 virtual int& Select(wxRect
& r
) const { return r
.x
; }
691 virtual int& SelectSize(wxRect
& r
) const { return r
.width
; }
692 virtual wxSize
MakeSize(int first
, int second
) const
693 { return wxSize(first
, second
); }
694 virtual void Set(wxGridCellCoords
& coords
, int line
) const
695 { coords
.SetRow(line
); }
697 virtual void DrawParallelLine(wxDC
& dc
, int start
, int end
, int pos
) const
698 { dc
.DrawLine(start
, pos
, end
, pos
); }
700 virtual int PosToLine(const wxGrid
*grid
, int pos
, bool clip
= false) const
701 { return grid
->YToRow(pos
, clip
); }
702 virtual int GetLineStartPos(const wxGrid
*grid
, int line
) const
703 { return grid
->GetRowTop(line
); }
704 virtual int GetLineEndPos(const wxGrid
*grid
, int line
) const
705 { return grid
->GetRowBottom(line
); }
706 virtual int GetLineSize(const wxGrid
*grid
, int line
) const
707 { return grid
->GetRowHeight(line
); }
708 virtual const wxArrayInt
& GetLineEnds(const wxGrid
*grid
) const
709 { return grid
->m_rowBottoms
; }
710 virtual int GetDefaultLineSize(const wxGrid
*grid
) const
711 { return grid
->GetDefaultRowSize(); }
712 virtual int GetMinimalAcceptableLineSize(const wxGrid
*grid
) const
713 { return grid
->GetRowMinimalAcceptableHeight(); }
714 virtual int GetMinimalLineSize(const wxGrid
*grid
, int line
) const
715 { return grid
->GetRowMinimalHeight(line
); }
716 virtual void SetLineSize(wxGrid
*grid
, int line
, int size
) const
717 { grid
->SetRowSize(line
, size
); }
718 virtual bool CanResizeLines(const wxGrid
*grid
) const
719 { return grid
->CanDragRowSize(); }
721 virtual int GetLineAt(const wxGrid
* WXUNUSED(grid
), int line
) const
722 { return line
; } // TODO: implement row reordering
724 virtual wxWindow
*GetHeaderWindow(wxGrid
*grid
) const
725 { return grid
->GetGridRowLabelWindow(); }
726 virtual int GetHeaderWindowSize(wxGrid
*grid
) const
727 { return grid
->GetRowLabelSize(); }
730 class wxGridColumnOperations
: public wxGridOperations
733 virtual wxGridOperations
& Dual() const;
735 virtual int GetNumberOfLines(const wxGrid
*grid
) const
736 { return grid
->GetNumberCols(); }
738 virtual wxGrid::wxGridSelectionModes
GetSelectionMode() const
739 { return wxGrid::wxGridSelectColumns
; }
741 virtual wxGridCellCoords
MakeCoords(int thisDir
, int otherDir
) const
742 { return wxGridCellCoords(otherDir
, thisDir
); }
744 virtual int CalcScrolledPosition(wxGrid
*grid
, int pos
) const
745 { return grid
->CalcScrolledPosition(wxPoint(0, pos
)).y
; }
747 virtual int Select(const wxGridCellCoords
& c
) const { return c
.GetCol(); }
748 virtual int Select(const wxPoint
& pt
) const { return pt
.y
; }
749 virtual int Select(const wxSize
& sz
) const { return sz
.y
; }
750 virtual int Select(const wxRect
& r
) const { return r
.y
; }
751 virtual int& Select(wxRect
& r
) const { return r
.y
; }
752 virtual int& SelectSize(wxRect
& r
) const { return r
.height
; }
753 virtual wxSize
MakeSize(int first
, int second
) const
754 { return wxSize(second
, first
); }
755 virtual void Set(wxGridCellCoords
& coords
, int line
) const
756 { coords
.SetCol(line
); }
758 virtual void DrawParallelLine(wxDC
& dc
, int start
, int end
, int pos
) const
759 { dc
.DrawLine(pos
, start
, pos
, end
); }
761 virtual int PosToLine(const wxGrid
*grid
, int pos
, bool clip
= false) const
762 { return grid
->XToCol(pos
, clip
); }
763 virtual int GetLineStartPos(const wxGrid
*grid
, int line
) const
764 { return grid
->GetColLeft(line
); }
765 virtual int GetLineEndPos(const wxGrid
*grid
, int line
) const
766 { return grid
->GetColRight(line
); }
767 virtual int GetLineSize(const wxGrid
*grid
, int line
) const
768 { return grid
->GetColWidth(line
); }
769 virtual const wxArrayInt
& GetLineEnds(const wxGrid
*grid
) const
770 { return grid
->m_colRights
; }
771 virtual int GetDefaultLineSize(const wxGrid
*grid
) const
772 { return grid
->GetDefaultColSize(); }
773 virtual int GetMinimalAcceptableLineSize(const wxGrid
*grid
) const
774 { return grid
->GetColMinimalAcceptableWidth(); }
775 virtual int GetMinimalLineSize(const wxGrid
*grid
, int line
) const
776 { return grid
->GetColMinimalWidth(line
); }
777 virtual void SetLineSize(wxGrid
*grid
, int line
, int size
) const
778 { grid
->SetColSize(line
, size
); }
779 virtual bool CanResizeLines(const wxGrid
*grid
) const
780 { return grid
->CanDragColSize(); }
782 virtual int GetLineAt(const wxGrid
*grid
, int line
) const
783 { return grid
->GetColAt(line
); }
785 virtual wxWindow
*GetHeaderWindow(wxGrid
*grid
) const
786 { return grid
->GetGridColLabelWindow(); }
787 virtual int GetHeaderWindowSize(wxGrid
*grid
) const
788 { return grid
->GetColLabelSize(); }
791 wxGridOperations
& wxGridRowOperations::Dual() const
793 static wxGridColumnOperations s_colOper
;
798 wxGridOperations
& wxGridColumnOperations::Dual() const
800 static wxGridRowOperations s_rowOper
;
805 // This class abstracts the difference between operations going forward
806 // (down/right) and backward (up/left) and allows to use the same code for
807 // functions which differ only in the direction of grid traversal
809 // Like wxGridOperations it's an ABC with two concrete subclasses below. Unlike
810 // it, this is a normal object and not just a function dispatch table and has a
813 // Note: the explanation of this discrepancy is the existence of (very useful)
814 // Dual() method in wxGridOperations which forces us to make wxGridOperations a
815 // function dispatcher only.
816 class wxGridDirectionOperations
819 // The oper parameter to ctor selects whether we work with rows or columns
820 wxGridDirectionOperations(wxGrid
*grid
, const wxGridOperations
& oper
)
826 // Check if the component of this point in our direction is at the
827 // boundary, i.e. is the first/last row/column
828 virtual bool IsAtBoundary(const wxGridCellCoords
& coords
) const = 0;
830 // Increment the component of this point in our direction
831 virtual void Advance(wxGridCellCoords
& coords
) const = 0;
833 // Find the line at the given distance, in pixels, away from this one
834 // (this uses clipping, i.e. anything after the last line is counted as the
835 // last one and anything before the first one as 0)
836 virtual int MoveByPixelDistance(int line
, int distance
) const = 0;
838 // This class is never used polymorphically but give it a virtual dtor
839 // anyhow to suppress g++ complaints about it
840 virtual ~wxGridDirectionOperations() { }
843 wxGrid
* const m_grid
;
844 const wxGridOperations
& m_oper
;
847 class wxGridBackwardOperations
: public wxGridDirectionOperations
850 wxGridBackwardOperations(wxGrid
*grid
, const wxGridOperations
& oper
)
851 : wxGridDirectionOperations(grid
, oper
)
855 virtual bool IsAtBoundary(const wxGridCellCoords
& coords
) const
857 wxASSERT_MSG( m_oper
.Select(coords
) >= 0, "invalid row/column" );
859 return m_oper
.Select(coords
) == 0;
862 virtual void Advance(wxGridCellCoords
& coords
) const
864 wxASSERT( !IsAtBoundary(coords
) );
866 m_oper
.Set(coords
, m_oper
.Select(coords
) - 1);
869 virtual int MoveByPixelDistance(int line
, int distance
) const
871 int pos
= m_oper
.GetLineStartPos(m_grid
, line
);
872 return m_oper
.PosToLine(m_grid
, pos
- distance
+ 1, true);
876 class wxGridForwardOperations
: public wxGridDirectionOperations
879 wxGridForwardOperations(wxGrid
*grid
, const wxGridOperations
& oper
)
880 : wxGridDirectionOperations(grid
, oper
),
881 m_numLines(oper
.GetNumberOfLines(grid
))
885 virtual bool IsAtBoundary(const wxGridCellCoords
& coords
) const
887 wxASSERT_MSG( m_oper
.Select(coords
) < m_numLines
, "invalid row/column" );
889 return m_oper
.Select(coords
) == m_numLines
- 1;
892 virtual void Advance(wxGridCellCoords
& coords
) const
894 wxASSERT( !IsAtBoundary(coords
) );
896 m_oper
.Set(coords
, m_oper
.Select(coords
) + 1);
899 virtual int MoveByPixelDistance(int line
, int distance
) const
901 int pos
= m_oper
.GetLineStartPos(m_grid
, line
);
902 return m_oper
.PosToLine(m_grid
, pos
+ distance
, true);
906 const int m_numLines
;
909 // ----------------------------------------------------------------------------
911 // ----------------------------------------------------------------------------
913 //#define DEBUG_ATTR_CACHE
914 #ifdef DEBUG_ATTR_CACHE
915 static size_t gs_nAttrCacheHits
= 0;
916 static size_t gs_nAttrCacheMisses
= 0;
919 // ----------------------------------------------------------------------------
921 // ----------------------------------------------------------------------------
923 wxGridCellCoords
wxGridNoCellCoords( -1, -1 );
924 wxRect
wxGridNoCellRect( -1, -1, -1, -1 );
930 const size_t GRID_SCROLL_LINE_X
= 15;
931 const size_t GRID_SCROLL_LINE_Y
= GRID_SCROLL_LINE_X
;
933 // the size of hash tables used a bit everywhere (the max number of elements
934 // in these hash tables is the number of rows/columns)
935 const int GRID_HASH_SIZE
= 100;
937 // the minimal distance in pixels the mouse needs to move to start a drag
939 const int DRAG_SENSITIVITY
= 3;
941 } // anonymous namespace
943 // ----------------------------------------------------------------------------
945 // ----------------------------------------------------------------------------
950 // ensure that first is less or equal to second, swapping the values if
952 void EnsureFirstLessThanSecond(int& first
, int& second
)
954 if ( first
> second
)
955 wxSwap(first
, second
);
958 } // anonymous namespace
960 // ============================================================================
962 // ============================================================================
964 // ----------------------------------------------------------------------------
966 // ----------------------------------------------------------------------------
968 wxGridCellEditor::wxGridCellEditor()
974 wxGridCellEditor::~wxGridCellEditor()
979 void wxGridCellEditor::Create(wxWindow
* WXUNUSED(parent
),
980 wxWindowID
WXUNUSED(id
),
981 wxEvtHandler
* evtHandler
)
984 m_control
->PushEventHandler(evtHandler
);
987 void wxGridCellEditor::PaintBackground(const wxRect
& rectCell
,
988 wxGridCellAttr
*attr
)
990 // erase the background because we might not fill the cell
991 wxClientDC
dc(m_control
->GetParent());
992 wxGridWindow
* gridWindow
= wxDynamicCast(m_control
->GetParent(), wxGridWindow
);
994 gridWindow
->GetOwner()->PrepareDC(dc
);
996 dc
.SetPen(*wxTRANSPARENT_PEN
);
997 dc
.SetBrush(wxBrush(attr
->GetBackgroundColour()));
998 dc
.DrawRectangle(rectCell
);
1000 // redraw the control we just painted over
1001 m_control
->Refresh();
1004 void wxGridCellEditor::Destroy()
1008 m_control
->PopEventHandler( true /* delete it*/ );
1010 m_control
->Destroy();
1015 void wxGridCellEditor::Show(bool show
, wxGridCellAttr
*attr
)
1017 wxASSERT_MSG(m_control
, wxT("The wxGridCellEditor must be created first!"));
1019 m_control
->Show(show
);
1023 // set the colours/fonts if we have any
1026 m_colFgOld
= m_control
->GetForegroundColour();
1027 m_control
->SetForegroundColour(attr
->GetTextColour());
1029 m_colBgOld
= m_control
->GetBackgroundColour();
1030 m_control
->SetBackgroundColour(attr
->GetBackgroundColour());
1032 // Workaround for GTK+1 font setting problem on some platforms
1033 #if !defined(__WXGTK__) || defined(__WXGTK20__)
1034 m_fontOld
= m_control
->GetFont();
1035 m_control
->SetFont(attr
->GetFont());
1038 // can't do anything more in the base class version, the other
1039 // attributes may only be used by the derived classes
1044 // restore the standard colours fonts
1045 if ( m_colFgOld
.Ok() )
1047 m_control
->SetForegroundColour(m_colFgOld
);
1048 m_colFgOld
= wxNullColour
;
1051 if ( m_colBgOld
.Ok() )
1053 m_control
->SetBackgroundColour(m_colBgOld
);
1054 m_colBgOld
= wxNullColour
;
1057 // Workaround for GTK+1 font setting problem on some platforms
1058 #if !defined(__WXGTK__) || defined(__WXGTK20__)
1059 if ( m_fontOld
.Ok() )
1061 m_control
->SetFont(m_fontOld
);
1062 m_fontOld
= wxNullFont
;
1068 void wxGridCellEditor::SetSize(const wxRect
& rect
)
1070 wxASSERT_MSG(m_control
, wxT("The wxGridCellEditor must be created first!"));
1072 m_control
->SetSize(rect
, wxSIZE_ALLOW_MINUS_ONE
);
1075 void wxGridCellEditor::HandleReturn(wxKeyEvent
& event
)
1080 bool wxGridCellEditor::IsAcceptedKey(wxKeyEvent
& event
)
1082 bool ctrl
= event
.ControlDown();
1083 bool alt
= event
.AltDown();
1086 // On the Mac the Alt key is more like shift and is used for entry of
1087 // valid characters, so check for Ctrl and Meta instead.
1088 alt
= event
.MetaDown();
1091 // Assume it's not a valid char if ctrl or alt is down, but if both are
1092 // down then it may be because of an AltGr key combination, so let them
1093 // through in that case.
1094 if ((ctrl
|| alt
) && !(ctrl
&& alt
))
1098 // if the unicode key code is not really a unicode character (it may
1099 // be a function key or etc., the platforms appear to always give us a
1100 // small value in this case) then fallback to the ASCII key code but
1101 // don't do anything for function keys or etc.
1102 if ( event
.GetUnicodeKey() > 127 && event
.GetKeyCode() > 127 )
1105 if ( event
.GetKeyCode() > 255 )
1112 void wxGridCellEditor::StartingKey(wxKeyEvent
& event
)
1117 void wxGridCellEditor::StartingClick()
1123 // ----------------------------------------------------------------------------
1124 // wxGridCellTextEditor
1125 // ----------------------------------------------------------------------------
1127 wxGridCellTextEditor::wxGridCellTextEditor()
1132 void wxGridCellTextEditor::Create(wxWindow
* parent
,
1134 wxEvtHandler
* evtHandler
)
1136 DoCreate(parent
, id
, evtHandler
);
1139 void wxGridCellTextEditor::DoCreate(wxWindow
* parent
,
1141 wxEvtHandler
* evtHandler
,
1144 style
|= wxTE_PROCESS_ENTER
| wxTE_PROCESS_TAB
| wxNO_BORDER
;
1146 m_control
= new wxTextCtrl(parent
, id
, wxEmptyString
,
1147 wxDefaultPosition
, wxDefaultSize
,
1150 // set max length allowed in the textctrl, if the parameter was set
1151 if ( m_maxChars
!= 0 )
1153 Text()->SetMaxLength(m_maxChars
);
1156 wxGridCellEditor::Create(parent
, id
, evtHandler
);
1159 void wxGridCellTextEditor::PaintBackground(const wxRect
& WXUNUSED(rectCell
),
1160 wxGridCellAttr
* WXUNUSED(attr
))
1162 // as we fill the entire client area,
1163 // don't do anything here to minimize flicker
1166 void wxGridCellTextEditor::SetSize(const wxRect
& rectOrig
)
1168 wxRect
rect(rectOrig
);
1170 // Make the edit control large enough to allow for internal margins
1172 // TODO: remove this if the text ctrl sizing is improved esp. for unix
1174 #if defined(__WXGTK__)
1182 #elif defined(__WXMSW__)
1196 int extra_x
= ( rect
.x
> 2 ) ? 2 : 1;
1197 int extra_y
= ( rect
.y
> 2 ) ? 2 : 1;
1199 #if defined(__WXMOTIF__)
1204 rect
.SetLeft( wxMax(0, rect
.x
- extra_x
) );
1205 rect
.SetTop( wxMax(0, rect
.y
- extra_y
) );
1206 rect
.SetRight( rect
.GetRight() + 2 * extra_x
);
1207 rect
.SetBottom( rect
.GetBottom() + 2 * extra_y
);
1210 wxGridCellEditor::SetSize(rect
);
1213 void wxGridCellTextEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1215 wxASSERT_MSG(m_control
, wxT("The wxGridCellEditor must be created first!"));
1217 m_startValue
= grid
->GetTable()->GetValue(row
, col
);
1219 DoBeginEdit(m_startValue
);
1222 void wxGridCellTextEditor::DoBeginEdit(const wxString
& startValue
)
1224 Text()->SetValue(startValue
);
1225 Text()->SetInsertionPointEnd();
1226 Text()->SetSelection(-1, -1);
1230 bool wxGridCellTextEditor::EndEdit(int row
, int col
, wxGrid
* grid
)
1232 wxASSERT_MSG(m_control
, wxT("The wxGridCellEditor must be created first!"));
1234 bool changed
= false;
1235 wxString value
= Text()->GetValue();
1236 if (value
!= m_startValue
)
1240 grid
->GetTable()->SetValue(row
, col
, value
);
1242 m_startValue
= wxEmptyString
;
1244 // No point in setting the text of the hidden control
1245 //Text()->SetValue(m_startValue);
1250 void wxGridCellTextEditor::Reset()
1252 wxASSERT_MSG(m_control
, wxT("The wxGridCellEditor must be created first!"));
1254 DoReset(m_startValue
);
1257 void wxGridCellTextEditor::DoReset(const wxString
& startValue
)
1259 Text()->SetValue(startValue
);
1260 Text()->SetInsertionPointEnd();
1263 bool wxGridCellTextEditor::IsAcceptedKey(wxKeyEvent
& event
)
1265 return wxGridCellEditor::IsAcceptedKey(event
);
1268 void wxGridCellTextEditor::StartingKey(wxKeyEvent
& event
)
1270 // Since this is now happening in the EVT_CHAR event EmulateKeyPress is no
1271 // longer an appropriate way to get the character into the text control.
1272 // Do it ourselves instead. We know that if we get this far that we have
1273 // a valid character, so not a whole lot of testing needs to be done.
1275 wxTextCtrl
* tc
= Text();
1280 ch
= event
.GetUnicodeKey();
1282 ch
= (wxChar
)event
.GetKeyCode();
1284 ch
= (wxChar
)event
.GetKeyCode();
1290 // delete the character at the cursor
1291 pos
= tc
->GetInsertionPoint();
1292 if (pos
< tc
->GetLastPosition())
1293 tc
->Remove(pos
, pos
+ 1);
1297 // delete the character before the cursor
1298 pos
= tc
->GetInsertionPoint();
1300 tc
->Remove(pos
- 1, pos
);
1309 void wxGridCellTextEditor::HandleReturn( wxKeyEvent
&
1310 WXUNUSED_GTK(WXUNUSED_MOTIF(event
)) )
1312 #if defined(__WXMOTIF__) || defined(__WXGTK__)
1313 // wxMotif needs a little extra help...
1314 size_t pos
= (size_t)( Text()->GetInsertionPoint() );
1315 wxString
s( Text()->GetValue() );
1316 s
= s
.Left(pos
) + wxT("\n") + s
.Mid(pos
);
1317 Text()->SetValue(s
);
1318 Text()->SetInsertionPoint( pos
);
1320 // the other ports can handle a Return key press
1326 void wxGridCellTextEditor::SetParameters(const wxString
& params
)
1336 if ( params
.ToLong(&tmp
) )
1338 m_maxChars
= (size_t)tmp
;
1342 wxLogDebug( _T("Invalid wxGridCellTextEditor parameter string '%s' ignored"), params
.c_str() );
1347 // return the value in the text control
1348 wxString
wxGridCellTextEditor::GetValue() const
1350 return Text()->GetValue();
1353 // ----------------------------------------------------------------------------
1354 // wxGridCellNumberEditor
1355 // ----------------------------------------------------------------------------
1357 wxGridCellNumberEditor::wxGridCellNumberEditor(int min
, int max
)
1363 void wxGridCellNumberEditor::Create(wxWindow
* parent
,
1365 wxEvtHandler
* evtHandler
)
1370 // create a spin ctrl
1371 m_control
= new wxSpinCtrl(parent
, wxID_ANY
, wxEmptyString
,
1372 wxDefaultPosition
, wxDefaultSize
,
1376 wxGridCellEditor::Create(parent
, id
, evtHandler
);
1381 // just a text control
1382 wxGridCellTextEditor::Create(parent
, id
, evtHandler
);
1384 #if wxUSE_VALIDATORS
1385 Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC
));
1390 void wxGridCellNumberEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1392 // first get the value
1393 wxGridTableBase
*table
= grid
->GetTable();
1394 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
1396 m_valueOld
= table
->GetValueAsLong(row
, col
);
1401 wxString sValue
= table
->GetValue(row
, col
);
1402 if (! sValue
.ToLong(&m_valueOld
) && ! sValue
.empty())
1404 wxFAIL_MSG( _T("this cell doesn't have numeric value") );
1412 Spin()->SetValue((int)m_valueOld
);
1418 DoBeginEdit(GetString());
1422 bool wxGridCellNumberEditor::EndEdit(int row
, int col
,
1431 value
= Spin()->GetValue();
1432 if ( value
== m_valueOld
)
1435 text
.Printf(wxT("%ld"), value
);
1437 else // using unconstrained input
1438 #endif // wxUSE_SPINCTRL
1440 const wxString
textOld(grid
->GetCellValue(row
, col
));
1441 text
= Text()->GetValue();
1444 if ( textOld
.empty() )
1447 else // non-empty text now (maybe 0)
1449 if ( !text
.ToLong(&value
) )
1452 // if value == m_valueOld == 0 but old text was "" and new one is
1453 // "0" something still did change
1454 if ( value
== m_valueOld
&& (value
|| !textOld
.empty()) )
1459 wxGridTableBase
* const table
= grid
->GetTable();
1460 if ( table
->CanSetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
1461 table
->SetValueAsLong(row
, col
, value
);
1463 table
->SetValue(row
, col
, text
);
1468 void wxGridCellNumberEditor::Reset()
1473 Spin()->SetValue((int)m_valueOld
);
1478 DoReset(GetString());
1482 bool wxGridCellNumberEditor::IsAcceptedKey(wxKeyEvent
& event
)
1484 if ( wxGridCellEditor::IsAcceptedKey(event
) )
1486 int keycode
= event
.GetKeyCode();
1487 if ( (keycode
< 128) &&
1488 (wxIsdigit(keycode
) || keycode
== '+' || keycode
== '-'))
1497 void wxGridCellNumberEditor::StartingKey(wxKeyEvent
& event
)
1499 int keycode
= event
.GetKeyCode();
1502 if ( wxIsdigit(keycode
) || keycode
== '+' || keycode
== '-')
1504 wxGridCellTextEditor::StartingKey(event
);
1506 // skip Skip() below
1513 if ( wxIsdigit(keycode
) )
1515 wxSpinCtrl
* spin
= (wxSpinCtrl
*)m_control
;
1516 spin
->SetValue(keycode
- '0');
1517 spin
->SetSelection(1,1);
1526 void wxGridCellNumberEditor::SetParameters(const wxString
& params
)
1537 if ( params
.BeforeFirst(_T(',')).ToLong(&tmp
) )
1541 if ( params
.AfterFirst(_T(',')).ToLong(&tmp
) )
1545 // skip the error message below
1550 wxLogDebug(_T("Invalid wxGridCellNumberEditor parameter string '%s' ignored"), params
.c_str());
1554 // return the value in the spin control if it is there (the text control otherwise)
1555 wxString
wxGridCellNumberEditor::GetValue() const
1562 long value
= Spin()->GetValue();
1563 s
.Printf(wxT("%ld"), value
);
1568 s
= Text()->GetValue();
1574 // ----------------------------------------------------------------------------
1575 // wxGridCellFloatEditor
1576 // ----------------------------------------------------------------------------
1578 wxGridCellFloatEditor::wxGridCellFloatEditor(int width
, int precision
)
1581 m_precision
= precision
;
1584 void wxGridCellFloatEditor::Create(wxWindow
* parent
,
1586 wxEvtHandler
* evtHandler
)
1588 wxGridCellTextEditor::Create(parent
, id
, evtHandler
);
1590 #if wxUSE_VALIDATORS
1591 Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC
));
1595 void wxGridCellFloatEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1597 // first get the value
1598 wxGridTableBase
* const table
= grid
->GetTable();
1599 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
1601 m_valueOld
= table
->GetValueAsDouble(row
, col
);
1607 const wxString value
= table
->GetValue(row
, col
);
1608 if ( !value
.empty() )
1610 if ( !value
.ToDouble(&m_valueOld
) )
1612 wxFAIL_MSG( _T("this cell doesn't have float value") );
1618 DoBeginEdit(GetString());
1621 bool wxGridCellFloatEditor::EndEdit(int row
, int col
, wxGrid
* grid
)
1623 const wxString
text(Text()->GetValue()),
1624 textOld(grid
->GetCellValue(row
, col
));
1627 if ( !text
.empty() )
1629 if ( !text
.ToDouble(&value
) )
1632 else // new value is empty string
1634 if ( textOld
.empty() )
1635 return false; // nothing changed
1640 // the test for empty strings ensures that we don't skip the value setting
1641 // when "" is replaced by "0" or vice versa as "" numeric value is also 0.
1642 if ( wxIsSameDouble(value
, m_valueOld
) && !text
.empty() && !textOld
.empty() )
1643 return false; // nothing changed
1645 wxGridTableBase
* const table
= grid
->GetTable();
1647 if ( table
->CanSetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
1648 table
->SetValueAsDouble(row
, col
, value
);
1650 table
->SetValue(row
, col
, text
);
1655 void wxGridCellFloatEditor::Reset()
1657 DoReset(GetString());
1660 void wxGridCellFloatEditor::StartingKey(wxKeyEvent
& event
)
1662 int keycode
= event
.GetKeyCode();
1664 tmpbuf
[0] = (char) keycode
;
1666 wxString
strbuf(tmpbuf
, *wxConvCurrent
);
1669 bool is_decimal_point
= ( strbuf
==
1670 wxLocale::GetInfo(wxLOCALE_DECIMAL_POINT
, wxLOCALE_CAT_NUMBER
) );
1672 bool is_decimal_point
= ( strbuf
== _T(".") );
1675 if ( wxIsdigit(keycode
) || keycode
== '+' || keycode
== '-'
1676 || is_decimal_point
)
1678 wxGridCellTextEditor::StartingKey(event
);
1680 // skip Skip() below
1687 void wxGridCellFloatEditor::SetParameters(const wxString
& params
)
1698 if ( params
.BeforeFirst(_T(',')).ToLong(&tmp
) )
1702 if ( params
.AfterFirst(_T(',')).ToLong(&tmp
) )
1704 m_precision
= (int)tmp
;
1706 // skip the error message below
1711 wxLogDebug(_T("Invalid wxGridCellFloatEditor parameter string '%s' ignored"), params
.c_str());
1715 wxString
wxGridCellFloatEditor::GetString() const
1718 if ( m_precision
== -1 && m_width
!= -1)
1720 // default precision
1721 fmt
.Printf(_T("%%%d.f"), m_width
);
1723 else if ( m_precision
!= -1 && m_width
== -1)
1726 fmt
.Printf(_T("%%.%df"), m_precision
);
1728 else if ( m_precision
!= -1 && m_width
!= -1 )
1730 fmt
.Printf(_T("%%%d.%df"), m_width
, m_precision
);
1734 // default width/precision
1738 return wxString::Format(fmt
, m_valueOld
);
1741 bool wxGridCellFloatEditor::IsAcceptedKey(wxKeyEvent
& event
)
1743 if ( wxGridCellEditor::IsAcceptedKey(event
) )
1745 const int keycode
= event
.GetKeyCode();
1746 if ( isascii(keycode
) )
1749 tmpbuf
[0] = (char) keycode
;
1751 wxString
strbuf(tmpbuf
, *wxConvCurrent
);
1754 const wxString decimalPoint
=
1755 wxLocale::GetInfo(wxLOCALE_DECIMAL_POINT
, wxLOCALE_CAT_NUMBER
);
1757 const wxString
decimalPoint(_T('.'));
1760 // accept digits, 'e' as in '1e+6', also '-', '+', and '.'
1761 if ( wxIsdigit(keycode
) ||
1762 tolower(keycode
) == 'e' ||
1763 keycode
== decimalPoint
||
1775 #endif // wxUSE_TEXTCTRL
1779 // ----------------------------------------------------------------------------
1780 // wxGridCellBoolEditor
1781 // ----------------------------------------------------------------------------
1783 // the default values for GetValue()
1784 wxString
wxGridCellBoolEditor::ms_stringValues
[2] = { _T(""), _T("1") };
1786 void wxGridCellBoolEditor::Create(wxWindow
* parent
,
1788 wxEvtHandler
* evtHandler
)
1790 m_control
= new wxCheckBox(parent
, id
, wxEmptyString
,
1791 wxDefaultPosition
, wxDefaultSize
,
1794 wxGridCellEditor::Create(parent
, id
, evtHandler
);
1797 void wxGridCellBoolEditor::SetSize(const wxRect
& r
)
1799 bool resize
= false;
1800 wxSize size
= m_control
->GetSize();
1801 wxCoord minSize
= wxMin(r
.width
, r
.height
);
1803 // check if the checkbox is not too big/small for this cell
1804 wxSize sizeBest
= m_control
->GetBestSize();
1805 if ( !(size
== sizeBest
) )
1807 // reset to default size if it had been made smaller
1813 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
1815 // leave 1 pixel margin
1816 size
.x
= size
.y
= minSize
- 2;
1823 m_control
->SetSize(size
);
1826 // position it in the centre of the rectangle (TODO: support alignment?)
1828 #if defined(__WXGTK__) || defined (__WXMOTIF__)
1829 // the checkbox without label still has some space to the right in wxGTK,
1830 // so shift it to the right
1832 #elif defined(__WXMSW__)
1833 // here too, but in other way
1838 int hAlign
= wxALIGN_CENTRE
;
1839 int vAlign
= wxALIGN_CENTRE
;
1841 GetCellAttr()->GetAlignment(& hAlign
, & vAlign
);
1844 if (hAlign
== wxALIGN_LEFT
)
1852 y
= r
.y
+ r
.height
/ 2 - size
.y
/ 2;
1854 else if (hAlign
== wxALIGN_RIGHT
)
1856 x
= r
.x
+ r
.width
- size
.x
- 2;
1857 y
= r
.y
+ r
.height
/ 2 - size
.y
/ 2;
1859 else if (hAlign
== wxALIGN_CENTRE
)
1861 x
= r
.x
+ r
.width
/ 2 - size
.x
/ 2;
1862 y
= r
.y
+ r
.height
/ 2 - size
.y
/ 2;
1865 m_control
->Move(x
, y
);
1868 void wxGridCellBoolEditor::Show(bool show
, wxGridCellAttr
*attr
)
1870 m_control
->Show(show
);
1874 wxColour colBg
= attr
? attr
->GetBackgroundColour() : *wxLIGHT_GREY
;
1875 CBox()->SetBackgroundColour(colBg
);
1879 void wxGridCellBoolEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1881 wxASSERT_MSG(m_control
,
1882 wxT("The wxGridCellEditor must be created first!"));
1884 if (grid
->GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
))
1886 m_startValue
= grid
->GetTable()->GetValueAsBool(row
, col
);
1890 wxString
cellval( grid
->GetTable()->GetValue(row
, col
) );
1892 if ( cellval
== ms_stringValues
[false] )
1893 m_startValue
= false;
1894 else if ( cellval
== ms_stringValues
[true] )
1895 m_startValue
= true;
1898 // do not try to be smart here and convert it to true or false
1899 // because we'll still overwrite it with something different and
1900 // this risks to be very surprising for the user code, let them
1902 wxFAIL_MSG( _T("invalid value for a cell with bool editor!") );
1906 CBox()->SetValue(m_startValue
);
1910 bool wxGridCellBoolEditor::EndEdit(int row
, int col
,
1913 wxASSERT_MSG(m_control
,
1914 wxT("The wxGridCellEditor must be created first!"));
1916 bool changed
= false;
1917 bool value
= CBox()->GetValue();
1918 if ( value
!= m_startValue
)
1923 wxGridTableBase
* const table
= grid
->GetTable();
1924 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
) )
1925 table
->SetValueAsBool(row
, col
, value
);
1927 table
->SetValue(row
, col
, GetValue());
1933 void wxGridCellBoolEditor::Reset()
1935 wxASSERT_MSG(m_control
,
1936 wxT("The wxGridCellEditor must be created first!"));
1938 CBox()->SetValue(m_startValue
);
1941 void wxGridCellBoolEditor::StartingClick()
1943 CBox()->SetValue(!CBox()->GetValue());
1946 bool wxGridCellBoolEditor::IsAcceptedKey(wxKeyEvent
& event
)
1948 if ( wxGridCellEditor::IsAcceptedKey(event
) )
1950 int keycode
= event
.GetKeyCode();
1963 void wxGridCellBoolEditor::StartingKey(wxKeyEvent
& event
)
1965 int keycode
= event
.GetKeyCode();
1969 CBox()->SetValue(!CBox()->GetValue());
1973 CBox()->SetValue(true);
1977 CBox()->SetValue(false);
1982 wxString
wxGridCellBoolEditor::GetValue() const
1984 return ms_stringValues
[CBox()->GetValue()];
1988 wxGridCellBoolEditor::UseStringValues(const wxString
& valueTrue
,
1989 const wxString
& valueFalse
)
1991 ms_stringValues
[false] = valueFalse
;
1992 ms_stringValues
[true] = valueTrue
;
1996 wxGridCellBoolEditor::IsTrueValue(const wxString
& value
)
1998 return value
== ms_stringValues
[true];
2001 #endif // wxUSE_CHECKBOX
2005 // ----------------------------------------------------------------------------
2006 // wxGridCellChoiceEditor
2007 // ----------------------------------------------------------------------------
2009 wxGridCellChoiceEditor::wxGridCellChoiceEditor(const wxArrayString
& choices
,
2011 : m_choices(choices
),
2012 m_allowOthers(allowOthers
) { }
2014 wxGridCellChoiceEditor::wxGridCellChoiceEditor(size_t count
,
2015 const wxString choices
[],
2017 : m_allowOthers(allowOthers
)
2021 m_choices
.Alloc(count
);
2022 for ( size_t n
= 0; n
< count
; n
++ )
2024 m_choices
.Add(choices
[n
]);
2029 wxGridCellEditor
*wxGridCellChoiceEditor::Clone() const
2031 wxGridCellChoiceEditor
*editor
= new wxGridCellChoiceEditor
;
2032 editor
->m_allowOthers
= m_allowOthers
;
2033 editor
->m_choices
= m_choices
;
2038 void wxGridCellChoiceEditor::Create(wxWindow
* parent
,
2040 wxEvtHandler
* evtHandler
)
2042 int style
= wxTE_PROCESS_ENTER
|
2046 if ( !m_allowOthers
)
2047 style
|= wxCB_READONLY
;
2048 m_control
= new wxComboBox(parent
, id
, wxEmptyString
,
2049 wxDefaultPosition
, wxDefaultSize
,
2053 wxGridCellEditor::Create(parent
, id
, evtHandler
);
2056 void wxGridCellChoiceEditor::PaintBackground(const wxRect
& rectCell
,
2057 wxGridCellAttr
* attr
)
2059 // as we fill the entire client area, don't do anything here to minimize
2062 // TODO: It doesn't actually fill the client area since the height of a
2063 // combo always defaults to the standard. Until someone has time to
2064 // figure out the right rectangle to paint, just do it the normal way.
2065 wxGridCellEditor::PaintBackground(rectCell
, attr
);
2068 void wxGridCellChoiceEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
2070 wxASSERT_MSG(m_control
,
2071 wxT("The wxGridCellEditor must be created first!"));
2073 wxGridCellEditorEvtHandler
* evtHandler
= NULL
;
2075 evtHandler
= wxDynamicCast(m_control
->GetEventHandler(), wxGridCellEditorEvtHandler
);
2077 // Don't immediately end if we get a kill focus event within BeginEdit
2079 evtHandler
->SetInSetFocus(true);
2081 m_startValue
= grid
->GetTable()->GetValue(row
, col
);
2083 Reset(); // this updates combo box to correspond to m_startValue
2085 Combo()->SetFocus();
2089 // When dropping down the menu, a kill focus event
2090 // happens after this point, so we can't reset the flag yet.
2091 #if !defined(__WXGTK20__)
2092 evtHandler
->SetInSetFocus(false);
2097 bool wxGridCellChoiceEditor::EndEdit(int row
, int col
,
2100 wxString value
= Combo()->GetValue();
2101 if ( value
== m_startValue
)
2104 grid
->GetTable()->SetValue(row
, col
, value
);
2109 void wxGridCellChoiceEditor::Reset()
2113 Combo()->SetValue(m_startValue
);
2114 Combo()->SetInsertionPointEnd();
2116 else // the combobox is read-only
2118 // find the right position, or default to the first if not found
2119 int pos
= Combo()->FindString(m_startValue
);
2120 if (pos
== wxNOT_FOUND
)
2122 Combo()->SetSelection(pos
);
2126 void wxGridCellChoiceEditor::SetParameters(const wxString
& params
)
2136 wxStringTokenizer
tk(params
, _T(','));
2137 while ( tk
.HasMoreTokens() )
2139 m_choices
.Add(tk
.GetNextToken());
2143 // return the value in the text control
2144 wxString
wxGridCellChoiceEditor::GetValue() const
2146 return Combo()->GetValue();
2149 #endif // wxUSE_COMBOBOX
2151 // ----------------------------------------------------------------------------
2152 // wxGridCellEditorEvtHandler
2153 // ----------------------------------------------------------------------------
2155 void wxGridCellEditorEvtHandler::OnKillFocus(wxFocusEvent
& event
)
2157 // Don't disable the cell if we're just starting to edit it
2162 m_grid
->DisableCellEditControl();
2167 void wxGridCellEditorEvtHandler::OnKeyDown(wxKeyEvent
& event
)
2169 switch ( event
.GetKeyCode() )
2173 m_grid
->DisableCellEditControl();
2177 m_grid
->GetEventHandler()->ProcessEvent( event
);
2181 case WXK_NUMPAD_ENTER
:
2182 if (!m_grid
->GetEventHandler()->ProcessEvent(event
))
2183 m_editor
->HandleReturn(event
);
2192 void wxGridCellEditorEvtHandler::OnChar(wxKeyEvent
& event
)
2194 int row
= m_grid
->GetGridCursorRow();
2195 int col
= m_grid
->GetGridCursorCol();
2196 wxRect rect
= m_grid
->CellToRect( row
, col
);
2198 m_grid
->GetGridWindow()->GetClientSize( &cw
, &ch
);
2200 // if cell width is smaller than grid client area, cell is wholly visible
2201 bool wholeCellVisible
= (rect
.GetWidth() < cw
);
2203 switch ( event
.GetKeyCode() )
2208 case WXK_NUMPAD_ENTER
:
2213 if ( wholeCellVisible
)
2215 // no special processing needed...
2220 // do special processing for partly visible cell...
2222 // get the widths of all cells previous to this one
2224 for ( int i
= 0; i
< col
; i
++ )
2226 colXPos
+= m_grid
->GetColSize(i
);
2229 int xUnit
= 1, yUnit
= 1;
2230 m_grid
->GetScrollPixelsPerUnit(&xUnit
, &yUnit
);
2233 m_grid
->Scroll(colXPos
/ xUnit
- 1, m_grid
->GetScrollPos(wxVERTICAL
));
2237 m_grid
->Scroll(colXPos
/ xUnit
, m_grid
->GetScrollPos(wxVERTICAL
));
2245 if ( wholeCellVisible
)
2247 // no special processing needed...
2252 // do special processing for partly visible cell...
2255 wxString value
= m_grid
->GetCellValue(row
, col
);
2256 if ( wxEmptyString
!= value
)
2258 // get width of cell CONTENTS (text)
2260 wxFont font
= m_grid
->GetCellFont(row
, col
);
2261 m_grid
->GetTextExtent(value
, &textWidth
, &y
, NULL
, NULL
, &font
);
2263 // try to RIGHT align the text by scrolling
2264 int client_right
= m_grid
->GetGridWindow()->GetClientSize().GetWidth();
2266 // (m_grid->GetScrollLineX()*2) is a factor for not scrolling to far,
2267 // otherwise the last part of the cell content might be hidden below the scroll bar
2268 // FIXME: maybe there is a more suitable correction?
2269 textWidth
-= (client_right
- (m_grid
->GetScrollLineX() * 2));
2270 if ( textWidth
< 0 )
2276 // get the widths of all cells previous to this one
2278 for ( int i
= 0; i
< col
; i
++ )
2280 colXPos
+= m_grid
->GetColSize(i
);
2283 // and add the (modified) text width of the cell contents
2284 // as we'd like to see the last part of the cell contents
2285 colXPos
+= textWidth
;
2287 int xUnit
= 1, yUnit
= 1;
2288 m_grid
->GetScrollPixelsPerUnit(&xUnit
, &yUnit
);
2289 m_grid
->Scroll(colXPos
/ xUnit
- 1, m_grid
->GetScrollPos(wxVERTICAL
));
2300 // ----------------------------------------------------------------------------
2301 // wxGridCellWorker is an (almost) empty common base class for
2302 // wxGridCellRenderer and wxGridCellEditor managing ref counting
2303 // ----------------------------------------------------------------------------
2305 void wxGridCellWorker::SetParameters(const wxString
& WXUNUSED(params
))
2310 wxGridCellWorker::~wxGridCellWorker()
2314 // ============================================================================
2316 // ============================================================================
2318 // ----------------------------------------------------------------------------
2319 // wxGridCellRenderer
2320 // ----------------------------------------------------------------------------
2322 void wxGridCellRenderer::Draw(wxGrid
& grid
,
2323 wxGridCellAttr
& attr
,
2326 int WXUNUSED(row
), int WXUNUSED(col
),
2329 dc
.SetBackgroundMode( wxBRUSHSTYLE_SOLID
);
2332 if ( grid
.IsEnabled() )
2336 if ( grid
.HasFocus() )
2337 clr
= grid
.GetSelectionBackground();
2339 clr
= wxSystemSettings::GetColour(wxSYS_COLOUR_BTNSHADOW
);
2343 clr
= attr
.GetBackgroundColour();
2346 else // grey out fields if the grid is disabled
2348 clr
= wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
);
2352 dc
.SetPen( *wxTRANSPARENT_PEN
);
2353 dc
.DrawRectangle(rect
);
2356 // ----------------------------------------------------------------------------
2357 // wxGridCellStringRenderer
2358 // ----------------------------------------------------------------------------
2360 void wxGridCellStringRenderer::SetTextColoursAndFont(const wxGrid
& grid
,
2361 const wxGridCellAttr
& attr
,
2365 dc
.SetBackgroundMode( wxBRUSHSTYLE_TRANSPARENT
);
2367 // TODO some special colours for attr.IsReadOnly() case?
2369 // different coloured text when the grid is disabled
2370 if ( grid
.IsEnabled() )
2375 if ( grid
.HasFocus() )
2376 clr
= grid
.GetSelectionBackground();
2378 clr
= wxSystemSettings::GetColour(wxSYS_COLOUR_BTNSHADOW
);
2379 dc
.SetTextBackground( clr
);
2380 dc
.SetTextForeground( grid
.GetSelectionForeground() );
2384 dc
.SetTextBackground( attr
.GetBackgroundColour() );
2385 dc
.SetTextForeground( attr
.GetTextColour() );
2390 dc
.SetTextBackground(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
));
2391 dc
.SetTextForeground(wxSystemSettings::GetColour(wxSYS_COLOUR_GRAYTEXT
));
2394 dc
.SetFont( attr
.GetFont() );
2397 wxSize
wxGridCellStringRenderer::DoGetBestSize(const wxGridCellAttr
& attr
,
2399 const wxString
& text
)
2401 wxCoord x
= 0, y
= 0, max_x
= 0;
2402 dc
.SetFont(attr
.GetFont());
2403 wxStringTokenizer
tk(text
, _T('\n'));
2404 while ( tk
.HasMoreTokens() )
2406 dc
.GetTextExtent(tk
.GetNextToken(), &x
, &y
);
2407 max_x
= wxMax(max_x
, x
);
2410 y
*= 1 + text
.Freq(wxT('\n')); // multiply by the number of lines.
2412 return wxSize(max_x
, y
);
2415 wxSize
wxGridCellStringRenderer::GetBestSize(wxGrid
& grid
,
2416 wxGridCellAttr
& attr
,
2420 return DoGetBestSize(attr
, dc
, grid
.GetCellValue(row
, col
));
2423 void wxGridCellStringRenderer::Draw(wxGrid
& grid
,
2424 wxGridCellAttr
& attr
,
2426 const wxRect
& rectCell
,
2430 wxRect rect
= rectCell
;
2433 // erase only this cells background, overflow cells should have been erased
2434 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
2437 attr
.GetAlignment(&hAlign
, &vAlign
);
2439 int overflowCols
= 0;
2441 if (attr
.GetOverflow())
2443 int cols
= grid
.GetNumberCols();
2444 int best_width
= GetBestSize(grid
,attr
,dc
,row
,col
).GetWidth();
2445 int cell_rows
, cell_cols
;
2446 attr
.GetSize( &cell_rows
, &cell_cols
); // shouldn't get here if <= 0
2447 if ((best_width
> rectCell
.width
) && (col
< cols
) && grid
.GetTable())
2449 int i
, c_cols
, c_rows
;
2450 for (i
= col
+cell_cols
; i
< cols
; i
++)
2452 bool is_empty
= true;
2453 for (int j
=row
; j
< row
+ cell_rows
; j
++)
2455 // check w/ anchor cell for multicell block
2456 grid
.GetCellSize(j
, i
, &c_rows
, &c_cols
);
2459 if (!grid
.GetTable()->IsEmptyCell(j
+ c_rows
, i
))
2468 rect
.width
+= grid
.GetColSize(i
);
2476 if (rect
.width
>= best_width
)
2480 overflowCols
= i
- col
- cell_cols
+ 1;
2481 if (overflowCols
>= cols
)
2482 overflowCols
= cols
- 1;
2485 if (overflowCols
> 0) // redraw overflow cells w/ proper hilight
2487 hAlign
= wxALIGN_LEFT
; // if oveflowed then it's left aligned
2489 clip
.x
+= rectCell
.width
;
2490 // draw each overflow cell individually
2491 int col_end
= col
+ cell_cols
+ overflowCols
;
2492 if (col_end
>= grid
.GetNumberCols())
2493 col_end
= grid
.GetNumberCols() - 1;
2494 for (int i
= col
+ cell_cols
; i
<= col_end
; i
++)
2496 clip
.width
= grid
.GetColSize(i
) - 1;
2497 dc
.DestroyClippingRegion();
2498 dc
.SetClippingRegion(clip
);
2500 SetTextColoursAndFont(grid
, attr
, dc
,
2501 grid
.IsInSelection(row
,i
));
2503 grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
),
2504 rect
, hAlign
, vAlign
);
2505 clip
.x
+= grid
.GetColSize(i
) - 1;
2511 dc
.DestroyClippingRegion();
2515 // now we only have to draw the text
2516 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
2518 grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
),
2519 rect
, hAlign
, vAlign
);
2522 // ----------------------------------------------------------------------------
2523 // wxGridCellNumberRenderer
2524 // ----------------------------------------------------------------------------
2526 wxString
wxGridCellNumberRenderer::GetString(const wxGrid
& grid
, int row
, int col
)
2528 wxGridTableBase
*table
= grid
.GetTable();
2530 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
2532 text
.Printf(_T("%ld"), table
->GetValueAsLong(row
, col
));
2536 text
= table
->GetValue(row
, col
);
2542 void wxGridCellNumberRenderer::Draw(wxGrid
& grid
,
2543 wxGridCellAttr
& attr
,
2545 const wxRect
& rectCell
,
2549 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
2551 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
2553 // draw the text right aligned by default
2555 attr
.GetAlignment(&hAlign
, &vAlign
);
2556 hAlign
= wxALIGN_RIGHT
;
2558 wxRect rect
= rectCell
;
2561 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
2564 wxSize
wxGridCellNumberRenderer::GetBestSize(wxGrid
& grid
,
2565 wxGridCellAttr
& attr
,
2569 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
2572 // ----------------------------------------------------------------------------
2573 // wxGridCellFloatRenderer
2574 // ----------------------------------------------------------------------------
2576 wxGridCellFloatRenderer::wxGridCellFloatRenderer(int width
, int precision
)
2579 SetPrecision(precision
);
2582 wxGridCellRenderer
*wxGridCellFloatRenderer::Clone() const
2584 wxGridCellFloatRenderer
*renderer
= new wxGridCellFloatRenderer
;
2585 renderer
->m_width
= m_width
;
2586 renderer
->m_precision
= m_precision
;
2587 renderer
->m_format
= m_format
;
2592 wxString
wxGridCellFloatRenderer::GetString(const wxGrid
& grid
, int row
, int col
)
2594 wxGridTableBase
*table
= grid
.GetTable();
2599 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
2601 val
= table
->GetValueAsDouble(row
, col
);
2606 text
= table
->GetValue(row
, col
);
2607 hasDouble
= text
.ToDouble(&val
);
2614 if ( m_width
== -1 )
2616 if ( m_precision
== -1 )
2618 // default width/precision
2619 m_format
= _T("%f");
2623 m_format
.Printf(_T("%%.%df"), m_precision
);
2626 else if ( m_precision
== -1 )
2628 // default precision
2629 m_format
.Printf(_T("%%%d.f"), m_width
);
2633 m_format
.Printf(_T("%%%d.%df"), m_width
, m_precision
);
2637 text
.Printf(m_format
, val
);
2640 //else: text already contains the string
2645 void wxGridCellFloatRenderer::Draw(wxGrid
& grid
,
2646 wxGridCellAttr
& attr
,
2648 const wxRect
& rectCell
,
2652 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
2654 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
2656 // draw the text right aligned by default
2658 attr
.GetAlignment(&hAlign
, &vAlign
);
2659 hAlign
= wxALIGN_RIGHT
;
2661 wxRect rect
= rectCell
;
2664 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
2667 wxSize
wxGridCellFloatRenderer::GetBestSize(wxGrid
& grid
,
2668 wxGridCellAttr
& attr
,
2672 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
2675 void wxGridCellFloatRenderer::SetParameters(const wxString
& params
)
2679 // reset to defaults
2685 wxString tmp
= params
.BeforeFirst(_T(','));
2689 if ( tmp
.ToLong(&width
) )
2691 SetWidth((int)width
);
2695 wxLogDebug(_T("Invalid wxGridCellFloatRenderer width parameter string '%s ignored"), params
.c_str());
2699 tmp
= params
.AfterFirst(_T(','));
2703 if ( tmp
.ToLong(&precision
) )
2705 SetPrecision((int)precision
);
2709 wxLogDebug(_T("Invalid wxGridCellFloatRenderer precision parameter string '%s ignored"), params
.c_str());
2715 // ----------------------------------------------------------------------------
2716 // wxGridCellBoolRenderer
2717 // ----------------------------------------------------------------------------
2719 wxSize
wxGridCellBoolRenderer::ms_sizeCheckMark
;
2721 // FIXME these checkbox size calculations are really ugly...
2723 // between checkmark and box
2724 static const wxCoord wxGRID_CHECKMARK_MARGIN
= 2;
2726 wxSize
wxGridCellBoolRenderer::GetBestSize(wxGrid
& grid
,
2727 wxGridCellAttr
& WXUNUSED(attr
),
2732 // compute it only once (no locks for MT safeness in GUI thread...)
2733 if ( !ms_sizeCheckMark
.x
)
2735 // get checkbox size
2736 wxCheckBox
*checkbox
= new wxCheckBox(&grid
, wxID_ANY
, wxEmptyString
);
2737 wxSize size
= checkbox
->GetBestSize();
2738 wxCoord checkSize
= size
.y
+ 2 * wxGRID_CHECKMARK_MARGIN
;
2740 #if defined(__WXMOTIF__)
2741 checkSize
-= size
.y
/ 2;
2746 ms_sizeCheckMark
.x
= ms_sizeCheckMark
.y
= checkSize
;
2749 return ms_sizeCheckMark
;
2752 void wxGridCellBoolRenderer::Draw(wxGrid
& grid
,
2753 wxGridCellAttr
& attr
,
2759 wxGridCellRenderer::Draw(grid
, attr
, dc
, rect
, row
, col
, isSelected
);
2761 // draw a check mark in the centre (ignoring alignment - TODO)
2762 wxSize size
= GetBestSize(grid
, attr
, dc
, row
, col
);
2764 // don't draw outside the cell
2765 wxCoord minSize
= wxMin(rect
.width
, rect
.height
);
2766 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
2768 // and even leave (at least) 1 pixel margin
2769 size
.x
= size
.y
= minSize
;
2772 // draw a border around checkmark
2774 attr
.GetAlignment(&hAlign
, &vAlign
);
2777 if (hAlign
== wxALIGN_CENTRE
)
2779 rectBorder
.x
= rect
.x
+ rect
.width
/ 2 - size
.x
/ 2;
2780 rectBorder
.y
= rect
.y
+ rect
.height
/ 2 - size
.y
/ 2;
2781 rectBorder
.width
= size
.x
;
2782 rectBorder
.height
= size
.y
;
2784 else if (hAlign
== wxALIGN_LEFT
)
2786 rectBorder
.x
= rect
.x
+ 2;
2787 rectBorder
.y
= rect
.y
+ rect
.height
/ 2 - size
.y
/ 2;
2788 rectBorder
.width
= size
.x
;
2789 rectBorder
.height
= size
.y
;
2791 else if (hAlign
== wxALIGN_RIGHT
)
2793 rectBorder
.x
= rect
.x
+ rect
.width
- size
.x
- 2;
2794 rectBorder
.y
= rect
.y
+ rect
.height
/ 2 - size
.y
/ 2;
2795 rectBorder
.width
= size
.x
;
2796 rectBorder
.height
= size
.y
;
2800 if ( grid
.GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
) )
2802 value
= grid
.GetTable()->GetValueAsBool(row
, col
);
2806 wxString
cellval( grid
.GetTable()->GetValue(row
, col
) );
2807 value
= wxGridCellBoolEditor::IsTrueValue(cellval
);
2812 flags
|= wxCONTROL_CHECKED
;
2814 wxRendererNative::Get().DrawCheckBox( &grid
, dc
, rectBorder
, flags
);
2817 // ----------------------------------------------------------------------------
2819 // ----------------------------------------------------------------------------
2821 void wxGridCellAttr::Init(wxGridCellAttr
*attrDefault
)
2825 m_isReadOnly
= Unset
;
2830 m_attrkind
= wxGridCellAttr::Cell
;
2832 m_sizeRows
= m_sizeCols
= 1;
2833 m_overflow
= UnsetOverflow
;
2835 SetDefAttr(attrDefault
);
2838 wxGridCellAttr
*wxGridCellAttr::Clone() const
2840 wxGridCellAttr
*attr
= new wxGridCellAttr(m_defGridAttr
);
2842 if ( HasTextColour() )
2843 attr
->SetTextColour(GetTextColour());
2844 if ( HasBackgroundColour() )
2845 attr
->SetBackgroundColour(GetBackgroundColour());
2847 attr
->SetFont(GetFont());
2848 if ( HasAlignment() )
2849 attr
->SetAlignment(m_hAlign
, m_vAlign
);
2851 attr
->SetSize( m_sizeRows
, m_sizeCols
);
2855 attr
->SetRenderer(m_renderer
);
2856 m_renderer
->IncRef();
2860 attr
->SetEditor(m_editor
);
2865 attr
->SetReadOnly();
2867 attr
->SetOverflow( m_overflow
== Overflow
);
2868 attr
->SetKind( m_attrkind
);
2873 void wxGridCellAttr::MergeWith(wxGridCellAttr
*mergefrom
)
2875 if ( !HasTextColour() && mergefrom
->HasTextColour() )
2876 SetTextColour(mergefrom
->GetTextColour());
2877 if ( !HasBackgroundColour() && mergefrom
->HasBackgroundColour() )
2878 SetBackgroundColour(mergefrom
->GetBackgroundColour());
2879 if ( !HasFont() && mergefrom
->HasFont() )
2880 SetFont(mergefrom
->GetFont());
2881 if ( !HasAlignment() && mergefrom
->HasAlignment() )
2884 mergefrom
->GetAlignment( &hAlign
, &vAlign
);
2885 SetAlignment(hAlign
, vAlign
);
2887 if ( !HasSize() && mergefrom
->HasSize() )
2888 mergefrom
->GetSize( &m_sizeRows
, &m_sizeCols
);
2890 // Directly access member functions as GetRender/Editor don't just return
2891 // m_renderer/m_editor
2893 // Maybe add support for merge of Render and Editor?
2894 if (!HasRenderer() && mergefrom
->HasRenderer() )
2896 m_renderer
= mergefrom
->m_renderer
;
2897 m_renderer
->IncRef();
2899 if ( !HasEditor() && mergefrom
->HasEditor() )
2901 m_editor
= mergefrom
->m_editor
;
2904 if ( !HasReadWriteMode() && mergefrom
->HasReadWriteMode() )
2905 SetReadOnly(mergefrom
->IsReadOnly());
2907 if (!HasOverflowMode() && mergefrom
->HasOverflowMode() )
2908 SetOverflow(mergefrom
->GetOverflow());
2910 SetDefAttr(mergefrom
->m_defGridAttr
);
2913 void wxGridCellAttr::SetSize(int num_rows
, int num_cols
)
2915 // The size of a cell is normally 1,1
2917 // If this cell is larger (2,2) then this is the top left cell
2918 // the other cells that will be covered (lower right cells) must be
2919 // set to negative or zero values such that
2920 // row + num_rows of the covered cell points to the larger cell (this cell)
2921 // same goes for the col + num_cols.
2923 // Size of 0,0 is NOT valid, neither is <=0 and any positive value
2925 wxASSERT_MSG( (!((num_rows
> 0) && (num_cols
<= 0)) ||
2926 !((num_rows
<= 0) && (num_cols
> 0)) ||
2927 !((num_rows
== 0) && (num_cols
== 0))),
2928 wxT("wxGridCellAttr::SetSize only takes two postive values or negative/zero values"));
2930 m_sizeRows
= num_rows
;
2931 m_sizeCols
= num_cols
;
2934 const wxColour
& wxGridCellAttr::GetTextColour() const
2936 if (HasTextColour())
2940 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2942 return m_defGridAttr
->GetTextColour();
2946 wxFAIL_MSG(wxT("Missing default cell attribute"));
2947 return wxNullColour
;
2951 const wxColour
& wxGridCellAttr::GetBackgroundColour() const
2953 if (HasBackgroundColour())
2957 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2959 return m_defGridAttr
->GetBackgroundColour();
2963 wxFAIL_MSG(wxT("Missing default cell attribute"));
2964 return wxNullColour
;
2968 const wxFont
& wxGridCellAttr::GetFont() const
2974 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2976 return m_defGridAttr
->GetFont();
2980 wxFAIL_MSG(wxT("Missing default cell attribute"));
2985 void wxGridCellAttr::GetAlignment(int *hAlign
, int *vAlign
) const
2994 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2996 m_defGridAttr
->GetAlignment(hAlign
, vAlign
);
3000 wxFAIL_MSG(wxT("Missing default cell attribute"));
3004 void wxGridCellAttr::GetSize( int *num_rows
, int *num_cols
) const
3007 *num_rows
= m_sizeRows
;
3009 *num_cols
= m_sizeCols
;
3012 // GetRenderer and GetEditor use a slightly different decision path about
3013 // which attribute to use. If a non-default attr object has one then it is
3014 // used, otherwise the default editor or renderer is fetched from the grid and
3015 // used. It should be the default for the data type of the cell. If it is
3016 // NULL (because the table has a type that the grid does not have in its
3017 // registry), then the grid's default editor or renderer is used.
3019 wxGridCellRenderer
* wxGridCellAttr::GetRenderer(const wxGrid
* grid
, int row
, int col
) const
3021 wxGridCellRenderer
*renderer
= NULL
;
3023 if ( m_renderer
&& this != m_defGridAttr
)
3025 // use the cells renderer if it has one
3026 renderer
= m_renderer
;
3029 else // no non-default cell renderer
3031 // get default renderer for the data type
3034 // GetDefaultRendererForCell() will do IncRef() for us
3035 renderer
= grid
->GetDefaultRendererForCell(row
, col
);
3038 if ( renderer
== NULL
)
3040 if ( (m_defGridAttr
!= NULL
) && (m_defGridAttr
!= this) )
3042 // if we still don't have one then use the grid default
3043 // (no need for IncRef() here neither)
3044 renderer
= m_defGridAttr
->GetRenderer(NULL
, 0, 0);
3046 else // default grid attr
3048 // use m_renderer which we had decided not to use initially
3049 renderer
= m_renderer
;
3056 // we're supposed to always find something
3057 wxASSERT_MSG(renderer
, wxT("Missing default cell renderer"));
3062 // same as above, except for s/renderer/editor/g
3063 wxGridCellEditor
* wxGridCellAttr::GetEditor(const wxGrid
* grid
, int row
, int col
) const
3065 wxGridCellEditor
*editor
= NULL
;
3067 if ( m_editor
&& this != m_defGridAttr
)
3069 // use the cells editor if it has one
3073 else // no non default cell editor
3075 // get default editor for the data type
3078 // GetDefaultEditorForCell() will do IncRef() for us
3079 editor
= grid
->GetDefaultEditorForCell(row
, col
);
3082 if ( editor
== NULL
)
3084 if ( (m_defGridAttr
!= NULL
) && (m_defGridAttr
!= this) )
3086 // if we still don't have one then use the grid default
3087 // (no need for IncRef() here neither)
3088 editor
= m_defGridAttr
->GetEditor(NULL
, 0, 0);
3090 else // default grid attr
3092 // use m_editor which we had decided not to use initially
3100 // we're supposed to always find something
3101 wxASSERT_MSG(editor
, wxT("Missing default cell editor"));
3106 // ----------------------------------------------------------------------------
3107 // wxGridCellAttrData
3108 // ----------------------------------------------------------------------------
3110 void wxGridCellAttrData::SetAttr(wxGridCellAttr
*attr
, int row
, int col
)
3112 // Note: contrary to wxGridRowOrColAttrData::SetAttr, we must not
3113 // touch attribute's reference counting explicitly, since this
3114 // is managed by class wxGridCellWithAttr
3115 int n
= FindIndex(row
, col
);
3116 if ( n
== wxNOT_FOUND
)
3120 // add the attribute
3121 m_attrs
.Add(new wxGridCellWithAttr(row
, col
, attr
));
3123 //else: nothing to do
3125 else // we already have an attribute for this cell
3129 // change the attribute
3130 m_attrs
[(size_t)n
].ChangeAttr(attr
);
3134 // remove this attribute
3135 m_attrs
.RemoveAt((size_t)n
);
3140 wxGridCellAttr
*wxGridCellAttrData::GetAttr(int row
, int col
) const
3142 wxGridCellAttr
*attr
= NULL
;
3144 int n
= FindIndex(row
, col
);
3145 if ( n
!= wxNOT_FOUND
)
3147 attr
= m_attrs
[(size_t)n
].attr
;
3154 void wxGridCellAttrData::UpdateAttrRows( size_t pos
, int numRows
)
3156 size_t count
= m_attrs
.GetCount();
3157 for ( size_t n
= 0; n
< count
; n
++ )
3159 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
3160 wxCoord row
= coords
.GetRow();
3161 if ((size_t)row
>= pos
)
3165 // If rows inserted, include row counter where necessary
3166 coords
.SetRow(row
+ numRows
);
3168 else if (numRows
< 0)
3170 // If rows deleted ...
3171 if ((size_t)row
>= pos
- numRows
)
3173 // ...either decrement row counter (if row still exists)...
3174 coords
.SetRow(row
+ numRows
);
3178 // ...or remove the attribute
3179 m_attrs
.RemoveAt(n
);
3188 void wxGridCellAttrData::UpdateAttrCols( size_t pos
, int numCols
)
3190 size_t count
= m_attrs
.GetCount();
3191 for ( size_t n
= 0; n
< count
; n
++ )
3193 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
3194 wxCoord col
= coords
.GetCol();
3195 if ( (size_t)col
>= pos
)
3199 // If rows inserted, include row counter where necessary
3200 coords
.SetCol(col
+ numCols
);
3202 else if (numCols
< 0)
3204 // If rows deleted ...
3205 if ((size_t)col
>= pos
- numCols
)
3207 // ...either decrement row counter (if row still exists)...
3208 coords
.SetCol(col
+ numCols
);
3212 // ...or remove the attribute
3213 m_attrs
.RemoveAt(n
);
3222 int wxGridCellAttrData::FindIndex(int row
, int col
) const
3224 size_t count
= m_attrs
.GetCount();
3225 for ( size_t n
= 0; n
< count
; n
++ )
3227 const wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
3228 if ( (coords
.GetRow() == row
) && (coords
.GetCol() == col
) )
3237 // ----------------------------------------------------------------------------
3238 // wxGridRowOrColAttrData
3239 // ----------------------------------------------------------------------------
3241 wxGridRowOrColAttrData::~wxGridRowOrColAttrData()
3243 size_t count
= m_attrs
.GetCount();
3244 for ( size_t n
= 0; n
< count
; n
++ )
3246 m_attrs
[n
]->DecRef();
3250 wxGridCellAttr
*wxGridRowOrColAttrData::GetAttr(int rowOrCol
) const
3252 wxGridCellAttr
*attr
= NULL
;
3254 int n
= m_rowsOrCols
.Index(rowOrCol
);
3255 if ( n
!= wxNOT_FOUND
)
3257 attr
= m_attrs
[(size_t)n
];
3264 void wxGridRowOrColAttrData::SetAttr(wxGridCellAttr
*attr
, int rowOrCol
)
3266 int i
= m_rowsOrCols
.Index(rowOrCol
);
3267 if ( i
== wxNOT_FOUND
)
3271 // add the attribute - no need to do anything to reference count
3272 // since we take ownership of the attribute.
3273 m_rowsOrCols
.Add(rowOrCol
);
3276 // nothing to remove
3280 size_t n
= (size_t)i
;
3281 if ( m_attrs
[n
] == attr
)
3286 // change the attribute, handling reference count manually,
3287 // taking ownership of the new attribute.
3288 m_attrs
[n
]->DecRef();
3293 // remove this attribute, handling reference count manually
3294 m_attrs
[n
]->DecRef();
3295 m_rowsOrCols
.RemoveAt(n
);
3296 m_attrs
.RemoveAt(n
);
3301 void wxGridRowOrColAttrData::UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
)
3303 size_t count
= m_attrs
.GetCount();
3304 for ( size_t n
= 0; n
< count
; n
++ )
3306 int & rowOrCol
= m_rowsOrCols
[n
];
3307 if ( (size_t)rowOrCol
>= pos
)
3309 if ( numRowsOrCols
> 0 )
3311 // If rows inserted, include row counter where necessary
3312 rowOrCol
+= numRowsOrCols
;
3314 else if ( numRowsOrCols
< 0)
3316 // If rows deleted, either decrement row counter (if row still exists)
3317 if ((size_t)rowOrCol
>= pos
- numRowsOrCols
)
3318 rowOrCol
+= numRowsOrCols
;
3321 m_rowsOrCols
.RemoveAt(n
);
3322 m_attrs
[n
]->DecRef();
3323 m_attrs
.RemoveAt(n
);
3332 // ----------------------------------------------------------------------------
3333 // wxGridCellAttrProvider
3334 // ----------------------------------------------------------------------------
3336 wxGridCellAttrProvider::wxGridCellAttrProvider()
3341 wxGridCellAttrProvider::~wxGridCellAttrProvider()
3346 void wxGridCellAttrProvider::InitData()
3348 m_data
= new wxGridCellAttrProviderData
;
3351 wxGridCellAttr
*wxGridCellAttrProvider::GetAttr(int row
, int col
,
3352 wxGridCellAttr::wxAttrKind kind
) const
3354 wxGridCellAttr
*attr
= NULL
;
3359 case (wxGridCellAttr::Any
):
3360 // Get cached merge attributes.
3361 // Currently not used as no cache implemented as not mutable
3362 // attr = m_data->m_mergeAttr.GetAttr(row, col);
3365 // Basically implement old version.
3366 // Also check merge cache, so we don't have to re-merge every time..
3367 wxGridCellAttr
*attrcell
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
3368 wxGridCellAttr
*attrrow
= m_data
->m_rowAttrs
.GetAttr(row
);
3369 wxGridCellAttr
*attrcol
= m_data
->m_colAttrs
.GetAttr(col
);
3371 if ((attrcell
!= attrrow
) && (attrrow
!= attrcol
) && (attrcell
!= attrcol
))
3373 // Two or more are non NULL
3374 attr
= new wxGridCellAttr
;
3375 attr
->SetKind(wxGridCellAttr::Merged
);
3377 // Order is important..
3380 attr
->MergeWith(attrcell
);
3385 attr
->MergeWith(attrcol
);
3390 attr
->MergeWith(attrrow
);
3394 // store merge attr if cache implemented
3396 //m_data->m_mergeAttr.SetAttr(attr, row, col);
3400 // one or none is non null return it or null.
3419 case (wxGridCellAttr::Cell
):
3420 attr
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
3423 case (wxGridCellAttr::Col
):
3424 attr
= m_data
->m_colAttrs
.GetAttr(col
);
3427 case (wxGridCellAttr::Row
):
3428 attr
= m_data
->m_rowAttrs
.GetAttr(row
);
3433 // (wxGridCellAttr::Default):
3434 // (wxGridCellAttr::Merged):
3442 void wxGridCellAttrProvider::SetAttr(wxGridCellAttr
*attr
,
3448 m_data
->m_cellAttrs
.SetAttr(attr
, row
, col
);
3451 void wxGridCellAttrProvider::SetRowAttr(wxGridCellAttr
*attr
, int row
)
3456 m_data
->m_rowAttrs
.SetAttr(attr
, row
);
3459 void wxGridCellAttrProvider::SetColAttr(wxGridCellAttr
*attr
, int col
)
3464 m_data
->m_colAttrs
.SetAttr(attr
, col
);
3467 void wxGridCellAttrProvider::UpdateAttrRows( size_t pos
, int numRows
)
3471 m_data
->m_cellAttrs
.UpdateAttrRows( pos
, numRows
);
3473 m_data
->m_rowAttrs
.UpdateAttrRowsOrCols( pos
, numRows
);
3477 void wxGridCellAttrProvider::UpdateAttrCols( size_t pos
, int numCols
)
3481 m_data
->m_cellAttrs
.UpdateAttrCols( pos
, numCols
);
3483 m_data
->m_colAttrs
.UpdateAttrRowsOrCols( pos
, numCols
);
3487 // ----------------------------------------------------------------------------
3488 // wxGridTypeRegistry
3489 // ----------------------------------------------------------------------------
3491 wxGridTypeRegistry::~wxGridTypeRegistry()
3493 size_t count
= m_typeinfo
.GetCount();
3494 for ( size_t i
= 0; i
< count
; i
++ )
3495 delete m_typeinfo
[i
];
3498 void wxGridTypeRegistry::RegisterDataType(const wxString
& typeName
,
3499 wxGridCellRenderer
* renderer
,
3500 wxGridCellEditor
* editor
)
3502 wxGridDataTypeInfo
* info
= new wxGridDataTypeInfo(typeName
, renderer
, editor
);
3504 // is it already registered?
3505 int loc
= FindRegisteredDataType(typeName
);
3506 if ( loc
!= wxNOT_FOUND
)
3508 delete m_typeinfo
[loc
];
3509 m_typeinfo
[loc
] = info
;
3513 m_typeinfo
.Add(info
);
3517 int wxGridTypeRegistry::FindRegisteredDataType(const wxString
& typeName
)
3519 size_t count
= m_typeinfo
.GetCount();
3520 for ( size_t i
= 0; i
< count
; i
++ )
3522 if ( typeName
== m_typeinfo
[i
]->m_typeName
)
3531 int wxGridTypeRegistry::FindDataType(const wxString
& typeName
)
3533 int index
= FindRegisteredDataType(typeName
);
3534 if ( index
== wxNOT_FOUND
)
3536 // check whether this is one of the standard ones, in which case
3537 // register it "on the fly"
3539 if ( typeName
== wxGRID_VALUE_STRING
)
3541 RegisterDataType(wxGRID_VALUE_STRING
,
3542 new wxGridCellStringRenderer
,
3543 new wxGridCellTextEditor
);
3546 #endif // wxUSE_TEXTCTRL
3548 if ( typeName
== wxGRID_VALUE_BOOL
)
3550 RegisterDataType(wxGRID_VALUE_BOOL
,
3551 new wxGridCellBoolRenderer
,
3552 new wxGridCellBoolEditor
);
3555 #endif // wxUSE_CHECKBOX
3557 if ( typeName
== wxGRID_VALUE_NUMBER
)
3559 RegisterDataType(wxGRID_VALUE_NUMBER
,
3560 new wxGridCellNumberRenderer
,
3561 new wxGridCellNumberEditor
);
3563 else if ( typeName
== wxGRID_VALUE_FLOAT
)
3565 RegisterDataType(wxGRID_VALUE_FLOAT
,
3566 new wxGridCellFloatRenderer
,
3567 new wxGridCellFloatEditor
);
3570 #endif // wxUSE_TEXTCTRL
3572 if ( typeName
== wxGRID_VALUE_CHOICE
)
3574 RegisterDataType(wxGRID_VALUE_CHOICE
,
3575 new wxGridCellStringRenderer
,
3576 new wxGridCellChoiceEditor
);
3579 #endif // wxUSE_COMBOBOX
3584 // we get here only if just added the entry for this type, so return
3586 index
= m_typeinfo
.GetCount() - 1;
3592 int wxGridTypeRegistry::FindOrCloneDataType(const wxString
& typeName
)
3594 int index
= FindDataType(typeName
);
3595 if ( index
== wxNOT_FOUND
)
3597 // the first part of the typename is the "real" type, anything after ':'
3598 // are the parameters for the renderer
3599 index
= FindDataType(typeName
.BeforeFirst(_T(':')));
3600 if ( index
== wxNOT_FOUND
)
3605 wxGridCellRenderer
*renderer
= GetRenderer(index
);
3606 wxGridCellRenderer
*rendererOld
= renderer
;
3607 renderer
= renderer
->Clone();
3608 rendererOld
->DecRef();
3610 wxGridCellEditor
*editor
= GetEditor(index
);
3611 wxGridCellEditor
*editorOld
= editor
;
3612 editor
= editor
->Clone();
3613 editorOld
->DecRef();
3615 // do it even if there are no parameters to reset them to defaults
3616 wxString params
= typeName
.AfterFirst(_T(':'));
3617 renderer
->SetParameters(params
);
3618 editor
->SetParameters(params
);
3620 // register the new typename
3621 RegisterDataType(typeName
, renderer
, editor
);
3623 // we just registered it, it's the last one
3624 index
= m_typeinfo
.GetCount() - 1;
3630 wxGridCellRenderer
* wxGridTypeRegistry::GetRenderer(int index
)
3632 wxGridCellRenderer
* renderer
= m_typeinfo
[index
]->m_renderer
;
3639 wxGridCellEditor
* wxGridTypeRegistry::GetEditor(int index
)
3641 wxGridCellEditor
* editor
= m_typeinfo
[index
]->m_editor
;
3648 // ----------------------------------------------------------------------------
3650 // ----------------------------------------------------------------------------
3652 IMPLEMENT_ABSTRACT_CLASS( wxGridTableBase
, wxObject
)
3654 wxGridTableBase::wxGridTableBase()
3657 m_attrProvider
= NULL
;
3660 wxGridTableBase::~wxGridTableBase()
3662 delete m_attrProvider
;
3665 void wxGridTableBase::SetAttrProvider(wxGridCellAttrProvider
*attrProvider
)
3667 delete m_attrProvider
;
3668 m_attrProvider
= attrProvider
;
3671 bool wxGridTableBase::CanHaveAttributes()
3673 if ( ! GetAttrProvider() )
3675 // use the default attr provider by default
3676 SetAttrProvider(new wxGridCellAttrProvider
);
3682 wxGridCellAttr
*wxGridTableBase::GetAttr(int row
, int col
, wxGridCellAttr::wxAttrKind kind
)
3684 if ( m_attrProvider
)
3685 return m_attrProvider
->GetAttr(row
, col
, kind
);
3690 void wxGridTableBase::SetAttr(wxGridCellAttr
* attr
, int row
, int col
)
3692 if ( m_attrProvider
)
3695 attr
->SetKind(wxGridCellAttr::Cell
);
3696 m_attrProvider
->SetAttr(attr
, row
, col
);
3700 // as we take ownership of the pointer and don't store it, we must
3706 void wxGridTableBase::SetRowAttr(wxGridCellAttr
*attr
, int row
)
3708 if ( m_attrProvider
)
3710 attr
->SetKind(wxGridCellAttr::Row
);
3711 m_attrProvider
->SetRowAttr(attr
, row
);
3715 // as we take ownership of the pointer and don't store it, we must
3721 void wxGridTableBase::SetColAttr(wxGridCellAttr
*attr
, int col
)
3723 if ( m_attrProvider
)
3725 attr
->SetKind(wxGridCellAttr::Col
);
3726 m_attrProvider
->SetColAttr(attr
, col
);
3730 // as we take ownership of the pointer and don't store it, we must
3736 bool wxGridTableBase::InsertRows( size_t WXUNUSED(pos
),
3737 size_t WXUNUSED(numRows
) )
3739 wxFAIL_MSG( wxT("Called grid table class function InsertRows\nbut your derived table class does not override this function") );
3744 bool wxGridTableBase::AppendRows( size_t WXUNUSED(numRows
) )
3746 wxFAIL_MSG( wxT("Called grid table class function AppendRows\nbut your derived table class does not override this function"));
3751 bool wxGridTableBase::DeleteRows( size_t WXUNUSED(pos
),
3752 size_t WXUNUSED(numRows
) )
3754 wxFAIL_MSG( wxT("Called grid table class function DeleteRows\nbut your derived table class does not override this function"));
3759 bool wxGridTableBase::InsertCols( size_t WXUNUSED(pos
),
3760 size_t WXUNUSED(numCols
) )
3762 wxFAIL_MSG( wxT("Called grid table class function InsertCols\nbut your derived table class does not override this function"));
3767 bool wxGridTableBase::AppendCols( size_t WXUNUSED(numCols
) )
3769 wxFAIL_MSG(wxT("Called grid table class function AppendCols\nbut your derived table class does not override this function"));
3774 bool wxGridTableBase::DeleteCols( size_t WXUNUSED(pos
),
3775 size_t WXUNUSED(numCols
) )
3777 wxFAIL_MSG( wxT("Called grid table class function DeleteCols\nbut your derived table class does not override this function"));
3782 wxString
wxGridTableBase::GetRowLabelValue( int row
)
3786 // RD: Starting the rows at zero confuses users,
3787 // no matter how much it makes sense to us geeks.
3793 wxString
wxGridTableBase::GetColLabelValue( int col
)
3795 // default col labels are:
3796 // cols 0 to 25 : A-Z
3797 // cols 26 to 675 : AA-ZZ
3802 for ( n
= 1; ; n
++ )
3804 s
+= (wxChar
) (_T('A') + (wxChar
)(col
% 26));
3810 // reverse the string...
3812 for ( i
= 0; i
< n
; i
++ )
3820 wxString
wxGridTableBase::GetTypeName( int WXUNUSED(row
), int WXUNUSED(col
) )
3822 return wxGRID_VALUE_STRING
;
3825 bool wxGridTableBase::CanGetValueAs( int WXUNUSED(row
), int WXUNUSED(col
),
3826 const wxString
& typeName
)
3828 return typeName
== wxGRID_VALUE_STRING
;
3831 bool wxGridTableBase::CanSetValueAs( int row
, int col
, const wxString
& typeName
)
3833 return CanGetValueAs(row
, col
, typeName
);
3836 long wxGridTableBase::GetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
) )
3841 double wxGridTableBase::GetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
) )
3846 bool wxGridTableBase::GetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
) )
3851 void wxGridTableBase::SetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
),
3852 long WXUNUSED(value
) )
3856 void wxGridTableBase::SetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
),
3857 double WXUNUSED(value
) )
3861 void wxGridTableBase::SetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
),
3862 bool WXUNUSED(value
) )
3866 void* wxGridTableBase::GetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
3867 const wxString
& WXUNUSED(typeName
) )
3872 void wxGridTableBase::SetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
3873 const wxString
& WXUNUSED(typeName
),
3874 void* WXUNUSED(value
) )
3878 //////////////////////////////////////////////////////////////////////
3880 // Message class for the grid table to send requests and notifications
3884 wxGridTableMessage::wxGridTableMessage()
3892 wxGridTableMessage::wxGridTableMessage( wxGridTableBase
*table
, int id
,
3893 int commandInt1
, int commandInt2
)
3897 m_comInt1
= commandInt1
;
3898 m_comInt2
= commandInt2
;
3901 //////////////////////////////////////////////////////////////////////
3903 // A basic grid table for string data. An object of this class will
3904 // created by wxGrid if you don't specify an alternative table class.
3907 WX_DEFINE_OBJARRAY(wxGridStringArray
)
3909 IMPLEMENT_DYNAMIC_CLASS( wxGridStringTable
, wxGridTableBase
)
3911 wxGridStringTable::wxGridStringTable()
3916 wxGridStringTable::wxGridStringTable( int numRows
, int numCols
)
3919 m_data
.Alloc( numRows
);
3922 sa
.Alloc( numCols
);
3923 sa
.Add( wxEmptyString
, numCols
);
3925 m_data
.Add( sa
, numRows
);
3928 wxGridStringTable::~wxGridStringTable()
3932 int wxGridStringTable::GetNumberRows()
3934 return m_data
.GetCount();
3937 int wxGridStringTable::GetNumberCols()
3939 if ( m_data
.GetCount() > 0 )
3940 return m_data
[0].GetCount();
3945 wxString
wxGridStringTable::GetValue( int row
, int col
)
3947 wxCHECK_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3949 _T("invalid row or column index in wxGridStringTable") );
3951 return m_data
[row
][col
];
3954 void wxGridStringTable::SetValue( int row
, int col
, const wxString
& value
)
3956 wxCHECK_RET( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3957 _T("invalid row or column index in wxGridStringTable") );
3959 m_data
[row
][col
] = value
;
3962 void wxGridStringTable::Clear()
3965 int numRows
, numCols
;
3967 numRows
= m_data
.GetCount();
3970 numCols
= m_data
[0].GetCount();
3972 for ( row
= 0; row
< numRows
; row
++ )
3974 for ( col
= 0; col
< numCols
; col
++ )
3976 m_data
[row
][col
] = wxEmptyString
;
3982 bool wxGridStringTable::InsertRows( size_t pos
, size_t numRows
)
3984 size_t curNumRows
= m_data
.GetCount();
3985 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3986 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3988 if ( pos
>= curNumRows
)
3990 return AppendRows( numRows
);
3994 sa
.Alloc( curNumCols
);
3995 sa
.Add( wxEmptyString
, curNumCols
);
3996 m_data
.Insert( sa
, pos
, numRows
);
4000 wxGridTableMessage
msg( this,
4001 wxGRIDTABLE_NOTIFY_ROWS_INSERTED
,
4005 GetView()->ProcessTableMessage( msg
);
4011 bool wxGridStringTable::AppendRows( size_t numRows
)
4013 size_t curNumRows
= m_data
.GetCount();
4014 size_t curNumCols
= ( curNumRows
> 0
4015 ? m_data
[0].GetCount()
4016 : ( GetView() ? GetView()->GetNumberCols() : 0 ) );
4019 if ( curNumCols
> 0 )
4021 sa
.Alloc( curNumCols
);
4022 sa
.Add( wxEmptyString
, curNumCols
);
4025 m_data
.Add( sa
, numRows
);
4029 wxGridTableMessage
msg( this,
4030 wxGRIDTABLE_NOTIFY_ROWS_APPENDED
,
4033 GetView()->ProcessTableMessage( msg
);
4039 bool wxGridStringTable::DeleteRows( size_t pos
, size_t numRows
)
4041 size_t curNumRows
= m_data
.GetCount();
4043 if ( pos
>= curNumRows
)
4045 wxFAIL_MSG( wxString::Format
4047 wxT("Called wxGridStringTable::DeleteRows(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu rows"),
4049 (unsigned long)numRows
,
4050 (unsigned long)curNumRows
4056 if ( numRows
> curNumRows
- pos
)
4058 numRows
= curNumRows
- pos
;
4061 if ( numRows
>= curNumRows
)
4067 m_data
.RemoveAt( pos
, numRows
);
4072 wxGridTableMessage
msg( this,
4073 wxGRIDTABLE_NOTIFY_ROWS_DELETED
,
4077 GetView()->ProcessTableMessage( msg
);
4083 bool wxGridStringTable::InsertCols( size_t pos
, size_t numCols
)
4087 size_t curNumRows
= m_data
.GetCount();
4088 size_t curNumCols
= ( curNumRows
> 0
4089 ? m_data
[0].GetCount()
4090 : ( GetView() ? GetView()->GetNumberCols() : 0 ) );
4092 if ( pos
>= curNumCols
)
4094 return AppendCols( numCols
);
4097 if ( !m_colLabels
.IsEmpty() )
4099 m_colLabels
.Insert( wxEmptyString
, pos
, numCols
);
4102 for ( i
= pos
; i
< pos
+ numCols
; i
++ )
4103 m_colLabels
[i
] = wxGridTableBase::GetColLabelValue( i
);
4106 for ( row
= 0; row
< curNumRows
; row
++ )
4108 for ( col
= pos
; col
< pos
+ numCols
; col
++ )
4110 m_data
[row
].Insert( wxEmptyString
, col
);
4116 wxGridTableMessage
msg( this,
4117 wxGRIDTABLE_NOTIFY_COLS_INSERTED
,
4121 GetView()->ProcessTableMessage( msg
);
4127 bool wxGridStringTable::AppendCols( size_t numCols
)
4131 size_t curNumRows
= m_data
.GetCount();
4133 for ( row
= 0; row
< curNumRows
; row
++ )
4135 m_data
[row
].Add( wxEmptyString
, numCols
);
4140 wxGridTableMessage
msg( this,
4141 wxGRIDTABLE_NOTIFY_COLS_APPENDED
,
4144 GetView()->ProcessTableMessage( msg
);
4150 bool wxGridStringTable::DeleteCols( size_t pos
, size_t numCols
)
4154 size_t curNumRows
= m_data
.GetCount();
4155 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
4156 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
4158 if ( pos
>= curNumCols
)
4160 wxFAIL_MSG( wxString::Format
4162 wxT("Called wxGridStringTable::DeleteCols(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu cols"),
4164 (unsigned long)numCols
,
4165 (unsigned long)curNumCols
4172 colID
= GetView()->GetColAt( pos
);
4176 if ( numCols
> curNumCols
- colID
)
4178 numCols
= curNumCols
- colID
;
4181 if ( !m_colLabels
.IsEmpty() )
4183 // m_colLabels stores just as many elements as it needs, e.g. if only
4184 // the label of the first column had been set it would have only one
4185 // element and not numCols, so account for it
4186 int nToRm
= m_colLabels
.size() - colID
;
4188 m_colLabels
.RemoveAt( colID
, nToRm
);
4191 for ( row
= 0; row
< curNumRows
; row
++ )
4193 if ( numCols
>= curNumCols
)
4195 m_data
[row
].Clear();
4199 m_data
[row
].RemoveAt( colID
, numCols
);
4205 wxGridTableMessage
msg( this,
4206 wxGRIDTABLE_NOTIFY_COLS_DELETED
,
4210 GetView()->ProcessTableMessage( msg
);
4216 wxString
wxGridStringTable::GetRowLabelValue( int row
)
4218 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
4220 // using default label
4222 return wxGridTableBase::GetRowLabelValue( row
);
4226 return m_rowLabels
[row
];
4230 wxString
wxGridStringTable::GetColLabelValue( int col
)
4232 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
4234 // using default label
4236 return wxGridTableBase::GetColLabelValue( col
);
4240 return m_colLabels
[col
];
4244 void wxGridStringTable::SetRowLabelValue( int row
, const wxString
& value
)
4246 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
4248 int n
= m_rowLabels
.GetCount();
4251 for ( i
= n
; i
<= row
; i
++ )
4253 m_rowLabels
.Add( wxGridTableBase::GetRowLabelValue(i
) );
4257 m_rowLabels
[row
] = value
;
4260 void wxGridStringTable::SetColLabelValue( int col
, const wxString
& value
)
4262 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
4264 int n
= m_colLabels
.GetCount();
4267 for ( i
= n
; i
<= col
; i
++ )
4269 m_colLabels
.Add( wxGridTableBase::GetColLabelValue(i
) );
4273 m_colLabels
[col
] = value
;
4277 //////////////////////////////////////////////////////////////////////
4278 //////////////////////////////////////////////////////////////////////
4280 BEGIN_EVENT_TABLE(wxGridSubwindow
, wxWindow
)
4281 EVT_MOUSE_CAPTURE_LOST(wxGridSubwindow::OnMouseCaptureLost
)
4284 void wxGridSubwindow::OnMouseCaptureLost(wxMouseCaptureLostEvent
& WXUNUSED(event
))
4286 m_owner
->CancelMouseCapture();
4289 BEGIN_EVENT_TABLE( wxGridRowLabelWindow
, wxGridSubwindow
)
4290 EVT_PAINT( wxGridRowLabelWindow::OnPaint
)
4291 EVT_MOUSEWHEEL( wxGridRowLabelWindow::OnMouseWheel
)
4292 EVT_MOUSE_EVENTS( wxGridRowLabelWindow::OnMouseEvent
)
4295 void wxGridRowLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
4299 // NO - don't do this because it will set both the x and y origin
4300 // coords to match the parent scrolled window and we just want to
4301 // set the y coord - MB
4303 // m_owner->PrepareDC( dc );
4306 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
4307 wxPoint pt
= dc
.GetDeviceOrigin();
4308 dc
.SetDeviceOrigin( pt
.x
, pt
.y
-y
);
4310 wxArrayInt rows
= m_owner
->CalcRowLabelsExposed( GetUpdateRegion() );
4311 m_owner
->DrawRowLabels( dc
, rows
);
4314 void wxGridRowLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
4316 m_owner
->ProcessRowLabelMouseEvent( event
);
4319 void wxGridRowLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
4321 if (!m_owner
->GetEventHandler()->ProcessEvent( event
))
4325 //////////////////////////////////////////////////////////////////////
4327 BEGIN_EVENT_TABLE( wxGridColLabelWindow
, wxGridSubwindow
)
4328 EVT_PAINT( wxGridColLabelWindow::OnPaint
)
4329 EVT_MOUSEWHEEL( wxGridColLabelWindow::OnMouseWheel
)
4330 EVT_MOUSE_EVENTS( wxGridColLabelWindow::OnMouseEvent
)
4333 void wxGridColLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
4337 // NO - don't do this because it will set both the x and y origin
4338 // coords to match the parent scrolled window and we just want to
4339 // set the x coord - MB
4341 // m_owner->PrepareDC( dc );
4344 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
4345 wxPoint pt
= dc
.GetDeviceOrigin();
4346 if (GetLayoutDirection() == wxLayout_RightToLeft
)
4347 dc
.SetDeviceOrigin( pt
.x
+x
, pt
.y
);
4349 dc
.SetDeviceOrigin( pt
.x
-x
, pt
.y
);
4351 wxArrayInt cols
= m_owner
->CalcColLabelsExposed( GetUpdateRegion() );
4352 m_owner
->DrawColLabels( dc
, cols
);
4355 void wxGridColLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
4357 m_owner
->ProcessColLabelMouseEvent( event
);
4360 void wxGridColLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
4362 if (!m_owner
->GetEventHandler()->ProcessEvent( event
))
4366 //////////////////////////////////////////////////////////////////////
4368 BEGIN_EVENT_TABLE( wxGridCornerLabelWindow
, wxGridSubwindow
)
4369 EVT_MOUSEWHEEL( wxGridCornerLabelWindow::OnMouseWheel
)
4370 EVT_MOUSE_EVENTS( wxGridCornerLabelWindow::OnMouseEvent
)
4371 EVT_PAINT( wxGridCornerLabelWindow::OnPaint
)
4374 void wxGridCornerLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
4378 m_owner
->DrawCornerLabel(dc
);
4381 void wxGridCornerLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
4383 m_owner
->ProcessCornerLabelMouseEvent( event
);
4386 void wxGridCornerLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
4388 if (!m_owner
->GetEventHandler()->ProcessEvent(event
))
4392 //////////////////////////////////////////////////////////////////////
4394 BEGIN_EVENT_TABLE( wxGridWindow
, wxGridSubwindow
)
4395 EVT_PAINT( wxGridWindow::OnPaint
)
4396 EVT_MOUSEWHEEL( wxGridWindow::OnMouseWheel
)
4397 EVT_MOUSE_EVENTS( wxGridWindow::OnMouseEvent
)
4398 EVT_KEY_DOWN( wxGridWindow::OnKeyDown
)
4399 EVT_KEY_UP( wxGridWindow::OnKeyUp
)
4400 EVT_CHAR( wxGridWindow::OnChar
)
4401 EVT_SET_FOCUS( wxGridWindow::OnFocus
)
4402 EVT_KILL_FOCUS( wxGridWindow::OnFocus
)
4403 EVT_ERASE_BACKGROUND( wxGridWindow::OnEraseBackground
)
4406 void wxGridWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
4408 wxPaintDC
dc( this );
4409 m_owner
->PrepareDC( dc
);
4410 wxRegion reg
= GetUpdateRegion();
4411 wxGridCellCoordsArray dirtyCells
= m_owner
->CalcCellsExposed( reg
);
4412 m_owner
->DrawGridCellArea( dc
, dirtyCells
);
4414 m_owner
->DrawGridSpace( dc
);
4416 m_owner
->DrawAllGridLines( dc
, reg
);
4418 m_owner
->DrawHighlight( dc
, dirtyCells
);
4421 void wxGridWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
4423 wxWindow::ScrollWindow( dx
, dy
, rect
);
4424 m_owner
->GetGridRowLabelWindow()->ScrollWindow( 0, dy
, rect
);
4425 m_owner
->GetGridColLabelWindow()->ScrollWindow( dx
, 0, rect
);
4428 void wxGridWindow::OnMouseEvent( wxMouseEvent
& event
)
4430 if (event
.ButtonDown(wxMOUSE_BTN_LEFT
) && FindFocus() != this)
4433 m_owner
->ProcessGridCellMouseEvent( event
);
4436 void wxGridWindow::OnMouseWheel( wxMouseEvent
& event
)
4438 if (!m_owner
->GetEventHandler()->ProcessEvent( event
))
4442 // This seems to be required for wxMotif/wxGTK otherwise the mouse
4443 // cursor must be in the cell edit control to get key events
4445 void wxGridWindow::OnKeyDown( wxKeyEvent
& event
)
4447 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
4451 void wxGridWindow::OnKeyUp( wxKeyEvent
& event
)
4453 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
4457 void wxGridWindow::OnChar( wxKeyEvent
& event
)
4459 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
4463 void wxGridWindow::OnEraseBackground( wxEraseEvent
& WXUNUSED(event
) )
4467 void wxGridWindow::OnFocus(wxFocusEvent
& event
)
4469 // and if we have any selection, it has to be repainted, because it
4470 // uses different colour when the grid is not focused:
4471 if ( m_owner
->IsSelection() )
4477 // NB: Note that this code is in "else" branch only because the other
4478 // branch refreshes everything and so there's no point in calling
4479 // Refresh() again, *not* because it should only be done if
4480 // !IsSelection(). If the above code is ever optimized to refresh
4481 // only selected area, this needs to be moved out of the "else"
4482 // branch so that it's always executed.
4484 // current cell cursor {dis,re}appears on focus change:
4485 const wxGridCellCoords
cursorCoords(m_owner
->GetGridCursorRow(),
4486 m_owner
->GetGridCursorCol());
4487 const wxRect cursor
=
4488 m_owner
->BlockToDeviceRect(cursorCoords
, cursorCoords
);
4489 Refresh(true, &cursor
);
4492 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
4496 #define internalXToCol(x) XToCol(x, true)
4497 #define internalYToRow(y) YToRow(y, true)
4499 /////////////////////////////////////////////////////////////////////
4501 #if wxUSE_EXTENDED_RTTI
4502 WX_DEFINE_FLAGS( wxGridStyle
)
4504 wxBEGIN_FLAGS( wxGridStyle
)
4505 // new style border flags, we put them first to
4506 // use them for streaming out
4507 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
4508 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
4509 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
4510 wxFLAGS_MEMBER(wxBORDER_RAISED
)
4511 wxFLAGS_MEMBER(wxBORDER_STATIC
)
4512 wxFLAGS_MEMBER(wxBORDER_NONE
)
4514 // old style border flags
4515 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
4516 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
4517 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
4518 wxFLAGS_MEMBER(wxRAISED_BORDER
)
4519 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
4520 wxFLAGS_MEMBER(wxBORDER
)
4522 // standard window styles
4523 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
4524 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
4525 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
4526 wxFLAGS_MEMBER(wxWANTS_CHARS
)
4527 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE
)
4528 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
4529 wxFLAGS_MEMBER(wxVSCROLL
)
4530 wxFLAGS_MEMBER(wxHSCROLL
)
4532 wxEND_FLAGS( wxGridStyle
)
4534 IMPLEMENT_DYNAMIC_CLASS_XTI(wxGrid
, wxScrolledWindow
,"wx/grid.h")
4536 wxBEGIN_PROPERTIES_TABLE(wxGrid
)
4537 wxHIDE_PROPERTY( Children
)
4538 wxPROPERTY_FLAGS( WindowStyle
, wxGridStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
4539 wxEND_PROPERTIES_TABLE()
4541 wxBEGIN_HANDLERS_TABLE(wxGrid
)
4542 wxEND_HANDLERS_TABLE()
4544 wxCONSTRUCTOR_5( wxGrid
, wxWindow
* , Parent
, wxWindowID
, Id
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
4547 TODO : Expose more information of a list's layout, etc. via appropriate objects (e.g., NotebookPageInfo)
4550 IMPLEMENT_DYNAMIC_CLASS( wxGrid
, wxScrolledWindow
)
4553 BEGIN_EVENT_TABLE( wxGrid
, wxScrolledWindow
)
4554 EVT_PAINT( wxGrid::OnPaint
)
4555 EVT_SIZE( wxGrid::OnSize
)
4556 EVT_KEY_DOWN( wxGrid::OnKeyDown
)
4557 EVT_KEY_UP( wxGrid::OnKeyUp
)
4558 EVT_CHAR ( wxGrid::OnChar
)
4559 EVT_ERASE_BACKGROUND( wxGrid::OnEraseBackground
)
4562 bool wxGrid::Create(wxWindow
*parent
, wxWindowID id
,
4563 const wxPoint
& pos
, const wxSize
& size
,
4564 long style
, const wxString
& name
)
4566 if (!wxScrolledWindow::Create(parent
, id
, pos
, size
,
4567 style
| wxWANTS_CHARS
, name
))
4570 m_colMinWidths
= wxLongToLongHashMap(GRID_HASH_SIZE
);
4571 m_rowMinHeights
= wxLongToLongHashMap(GRID_HASH_SIZE
);
4574 SetInitialSize(size
);
4575 SetScrollRate(m_scrollLineX
, m_scrollLineY
);
4584 m_winCapture
->ReleaseMouse();
4586 // Ensure that the editor control is destroyed before the grid is,
4587 // otherwise we crash later when the editor tries to do something with the
4588 // half destroyed grid
4589 HideCellEditControl();
4591 // Must do this or ~wxScrollHelper will pop the wrong event handler
4592 SetTargetWindow(this);
4594 wxSafeDecRef(m_defaultCellAttr
);
4596 #ifdef DEBUG_ATTR_CACHE
4597 size_t total
= gs_nAttrCacheHits
+ gs_nAttrCacheMisses
;
4598 wxPrintf(_T("wxGrid attribute cache statistics: "
4599 "total: %u, hits: %u (%u%%)\n"),
4600 total
, gs_nAttrCacheHits
,
4601 total
? (gs_nAttrCacheHits
*100) / total
: 0);
4604 // if we own the table, just delete it, otherwise at least don't leave it
4605 // with dangling view pointer
4608 else if ( m_table
&& m_table
->GetView() == this )
4609 m_table
->SetView(NULL
);
4611 delete m_typeRegistry
;
4616 // ----- internal init and update functions
4619 // NOTE: If using the default visual attributes works everywhere then this can
4620 // be removed as well as the #else cases below.
4621 #define _USE_VISATTR 0
4623 void wxGrid::Create()
4625 // create the type registry
4626 m_typeRegistry
= new wxGridTypeRegistry
;
4628 m_cellEditCtrlEnabled
= false;
4630 m_defaultCellAttr
= new wxGridCellAttr();
4632 // Set default cell attributes
4633 m_defaultCellAttr
->SetDefAttr(m_defaultCellAttr
);
4634 m_defaultCellAttr
->SetKind(wxGridCellAttr::Default
);
4635 m_defaultCellAttr
->SetFont(GetFont());
4636 m_defaultCellAttr
->SetAlignment(wxALIGN_LEFT
, wxALIGN_TOP
);
4637 m_defaultCellAttr
->SetRenderer(new wxGridCellStringRenderer
);
4638 m_defaultCellAttr
->SetEditor(new wxGridCellTextEditor
);
4641 wxVisualAttributes gva
= wxListBox::GetClassDefaultAttributes();
4642 wxVisualAttributes lva
= wxPanel::GetClassDefaultAttributes();
4644 m_defaultCellAttr
->SetTextColour(gva
.colFg
);
4645 m_defaultCellAttr
->SetBackgroundColour(gva
.colBg
);
4648 m_defaultCellAttr
->SetTextColour(
4649 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
4650 m_defaultCellAttr
->SetBackgroundColour(
4651 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
4656 m_currentCellCoords
= wxGridNoCellCoords
;
4658 // subwindow components that make up the wxGrid
4659 m_rowLabelWin
= new wxGridRowLabelWindow(this);
4660 CreateColumnWindow();
4661 m_cornerLabelWin
= new wxGridCornerLabelWindow(this);
4662 m_gridWin
= new wxGridWindow( this );
4664 SetTargetWindow( m_gridWin
);
4667 wxColour gfg
= gva
.colFg
;
4668 wxColour gbg
= gva
.colBg
;
4669 wxColour lfg
= lva
.colFg
;
4670 wxColour lbg
= lva
.colBg
;
4672 wxColour gfg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT
);
4673 wxColour gbg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOW
);
4674 wxColour lfg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT
);
4675 wxColour lbg
= wxSystemSettings::GetColour( wxSYS_COLOUR_BTNFACE
);
4678 m_cornerLabelWin
->SetOwnForegroundColour(lfg
);
4679 m_cornerLabelWin
->SetOwnBackgroundColour(lbg
);
4680 m_rowLabelWin
->SetOwnForegroundColour(lfg
);
4681 m_rowLabelWin
->SetOwnBackgroundColour(lbg
);
4682 m_colWindow
->SetOwnForegroundColour(lfg
);
4683 m_colWindow
->SetOwnBackgroundColour(lbg
);
4685 m_gridWin
->SetOwnForegroundColour(gfg
);
4686 m_gridWin
->SetOwnBackgroundColour(gbg
);
4688 m_labelBackgroundColour
= m_rowLabelWin
->GetBackgroundColour();
4689 m_labelTextColour
= m_rowLabelWin
->GetForegroundColour();
4691 // now that we have the grid window, use its font to compute the default
4693 m_defaultRowHeight
= m_gridWin
->GetCharHeight();
4694 #if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl()
4695 m_defaultRowHeight
+= 8;
4697 m_defaultRowHeight
+= 4;
4702 void wxGrid::CreateColumnWindow()
4704 if ( m_useNativeHeader
)
4706 m_colWindow
= new wxGridHeaderCtrl(this);
4707 m_colLabelHeight
= m_colWindow
->GetBestSize().y
;
4709 else // draw labels ourselves
4711 m_colWindow
= new wxGridColLabelWindow(this);
4712 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
4716 bool wxGrid::CreateGrid( int numRows
, int numCols
,
4717 wxGridSelectionModes selmode
)
4719 wxCHECK_MSG( !m_created
,
4721 wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
4723 return SetTable(new wxGridStringTable(numRows
, numCols
), true, selmode
);
4726 void wxGrid::SetSelectionMode(wxGridSelectionModes selmode
)
4728 wxCHECK_RET( m_created
,
4729 wxT("Called wxGrid::SetSelectionMode() before calling CreateGrid()") );
4731 m_selection
->SetSelectionMode( selmode
);
4734 wxGrid::wxGridSelectionModes
wxGrid::GetSelectionMode() const
4736 wxCHECK_MSG( m_created
, wxGridSelectCells
,
4737 wxT("Called wxGrid::GetSelectionMode() before calling CreateGrid()") );
4739 return m_selection
->GetSelectionMode();
4743 wxGrid::SetTable(wxGridTableBase
*table
,
4745 wxGrid::wxGridSelectionModes selmode
)
4747 bool checkSelection
= false;
4750 // stop all processing
4755 m_table
->SetView(0);
4767 checkSelection
= true;
4769 // kill row and column size arrays
4770 m_colWidths
.Empty();
4771 m_colRights
.Empty();
4772 m_rowHeights
.Empty();
4773 m_rowBottoms
.Empty();
4778 m_numRows
= table
->GetNumberRows();
4779 m_numCols
= table
->GetNumberCols();
4781 if ( m_useNativeHeader
)
4782 GetColHeader()->SetColumnCount(m_numCols
);
4785 m_table
->SetView( this );
4786 m_ownTable
= takeOwnership
;
4787 m_selection
= new wxGridSelection( this, selmode
);
4790 // If the newly set table is smaller than the
4791 // original one current cell and selection regions
4792 // might be invalid,
4793 m_selectedBlockCorner
= wxGridNoCellCoords
;
4794 m_currentCellCoords
=
4795 wxGridCellCoords(wxMin(m_numRows
, m_currentCellCoords
.GetRow()),
4796 wxMin(m_numCols
, m_currentCellCoords
.GetCol()));
4797 if (m_selectedBlockTopLeft
.GetRow() >= m_numRows
||
4798 m_selectedBlockTopLeft
.GetCol() >= m_numCols
)
4800 m_selectedBlockTopLeft
= wxGridNoCellCoords
;
4801 m_selectedBlockBottomRight
= wxGridNoCellCoords
;
4804 m_selectedBlockBottomRight
=
4805 wxGridCellCoords(wxMin(m_numRows
,
4806 m_selectedBlockBottomRight
.GetRow()),
4808 m_selectedBlockBottomRight
.GetCol()));
4822 m_cornerLabelWin
= NULL
;
4823 m_rowLabelWin
= NULL
;
4831 m_defaultCellAttr
= NULL
;
4832 m_typeRegistry
= NULL
;
4833 m_winCapture
= NULL
;
4835 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
4836 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
4839 m_attrCache
.row
= -1;
4840 m_attrCache
.col
= -1;
4841 m_attrCache
.attr
= NULL
;
4843 m_labelFont
= GetFont();
4844 m_labelFont
.SetWeight( wxBOLD
);
4846 m_rowLabelHorizAlign
= wxALIGN_CENTRE
;
4847 m_rowLabelVertAlign
= wxALIGN_CENTRE
;
4849 m_colLabelHorizAlign
= wxALIGN_CENTRE
;
4850 m_colLabelVertAlign
= wxALIGN_CENTRE
;
4851 m_colLabelTextOrientation
= wxHORIZONTAL
;
4853 m_defaultColWidth
= WXGRID_DEFAULT_COL_WIDTH
;
4854 m_defaultRowHeight
= 0; // this will be initialized after creation
4856 m_minAcceptableColWidth
= WXGRID_MIN_COL_WIDTH
;
4857 m_minAcceptableRowHeight
= WXGRID_MIN_ROW_HEIGHT
;
4859 m_gridLineColour
= wxColour( 192,192,192 );
4860 m_gridLinesEnabled
= true;
4861 m_gridLinesClipHorz
=
4862 m_gridLinesClipVert
= true;
4863 m_cellHighlightColour
= *wxBLACK
;
4864 m_cellHighlightPenWidth
= 2;
4865 m_cellHighlightROPenWidth
= 1;
4867 m_canDragColMove
= false;
4869 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
4870 m_winCapture
= NULL
;
4871 m_canDragRowSize
= true;
4872 m_canDragColSize
= true;
4873 m_canDragGridSize
= true;
4874 m_canDragCell
= false;
4876 m_dragRowOrCol
= -1;
4877 m_isDragging
= false;
4878 m_startDragPos
= wxDefaultPosition
;
4881 m_nativeColumnLabels
= false;
4883 m_waitForSlowClick
= false;
4885 m_rowResizeCursor
= wxCursor( wxCURSOR_SIZENS
);
4886 m_colResizeCursor
= wxCursor( wxCURSOR_SIZEWE
);
4888 m_currentCellCoords
= wxGridNoCellCoords
;
4890 m_selectedBlockTopLeft
=
4891 m_selectedBlockBottomRight
=
4892 m_selectedBlockCorner
= wxGridNoCellCoords
;
4894 m_selectionBackground
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
);
4895 m_selectionForeground
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
4897 m_editable
= true; // default for whole grid
4899 m_inOnKeyDown
= false;
4905 m_scrollLineX
= GRID_SCROLL_LINE_X
;
4906 m_scrollLineY
= GRID_SCROLL_LINE_Y
;
4909 // ----------------------------------------------------------------------------
4910 // the idea is to call these functions only when necessary because they create
4911 // quite big arrays which eat memory mostly unnecessary - in particular, if
4912 // default widths/heights are used for all rows/columns, we may not use these
4915 // with some extra code, it should be possible to only store the widths/heights
4916 // different from default ones (resulting in space savings for huge grids) but
4917 // this is not done currently
4918 // ----------------------------------------------------------------------------
4920 void wxGrid::InitRowHeights()
4922 m_rowHeights
.Empty();
4923 m_rowBottoms
.Empty();
4925 m_rowHeights
.Alloc( m_numRows
);
4926 m_rowBottoms
.Alloc( m_numRows
);
4928 m_rowHeights
.Add( m_defaultRowHeight
, m_numRows
);
4931 for ( int i
= 0; i
< m_numRows
; i
++ )
4933 rowBottom
+= m_defaultRowHeight
;
4934 m_rowBottoms
.Add( rowBottom
);
4938 void wxGrid::InitColWidths()
4940 m_colWidths
.Empty();
4941 m_colRights
.Empty();
4943 m_colWidths
.Alloc( m_numCols
);
4944 m_colRights
.Alloc( m_numCols
);
4946 m_colWidths
.Add( m_defaultColWidth
, m_numCols
);
4948 for ( int i
= 0; i
< m_numCols
; i
++ )
4950 int colRight
= ( GetColPos( i
) + 1 ) * m_defaultColWidth
;
4951 m_colRights
.Add( colRight
);
4955 int wxGrid::GetColWidth(int col
) const
4957 return m_colWidths
.IsEmpty() ? m_defaultColWidth
: m_colWidths
[col
];
4960 int wxGrid::GetColLeft(int col
) const
4962 return m_colRights
.IsEmpty() ? GetColPos( col
) * m_defaultColWidth
4963 : m_colRights
[col
] - m_colWidths
[col
];
4966 int wxGrid::GetColRight(int col
) const
4968 return m_colRights
.IsEmpty() ? (GetColPos( col
) + 1) * m_defaultColWidth
4972 int wxGrid::GetRowHeight(int row
) const
4974 return m_rowHeights
.IsEmpty() ? m_defaultRowHeight
: m_rowHeights
[row
];
4977 int wxGrid::GetRowTop(int row
) const
4979 return m_rowBottoms
.IsEmpty() ? row
* m_defaultRowHeight
4980 : m_rowBottoms
[row
] - m_rowHeights
[row
];
4983 int wxGrid::GetRowBottom(int row
) const
4985 return m_rowBottoms
.IsEmpty() ? (row
+ 1) * m_defaultRowHeight
4986 : m_rowBottoms
[row
];
4989 void wxGrid::CalcDimensions()
4991 // compute the size of the scrollable area
4992 int w
= m_numCols
> 0 ? GetColRight(GetColAt(m_numCols
- 1)) : 0;
4993 int h
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) : 0;
4998 // take into account editor if shown
4999 if ( IsCellEditControlShown() )
5002 int r
= m_currentCellCoords
.GetRow();
5003 int c
= m_currentCellCoords
.GetCol();
5004 int x
= GetColLeft(c
);
5005 int y
= GetRowTop(r
);
5007 // how big is the editor
5008 wxGridCellAttr
* attr
= GetCellAttr(r
, c
);
5009 wxGridCellEditor
* editor
= attr
->GetEditor(this, r
, c
);
5010 editor
->GetControl()->GetSize(&w2
, &h2
);
5021 // preserve (more or less) the previous position
5023 GetViewStart( &x
, &y
);
5025 // ensure the position is valid for the new scroll ranges
5027 x
= wxMax( w
- 1, 0 );
5029 y
= wxMax( h
- 1, 0 );
5031 // update the virtual size and refresh the scrollbars to reflect it
5032 m_gridWin
->SetVirtualSize(w
, h
);
5036 // if our OnSize() hadn't been called (it would if we have scrollbars), we
5037 // still must reposition the children
5041 wxSize
wxGrid::GetSizeAvailableForScrollTarget(const wxSize
& size
)
5043 wxSize
sizeGridWin(size
);
5044 sizeGridWin
.x
-= m_rowLabelWidth
;
5045 sizeGridWin
.y
-= m_colLabelHeight
;
5050 void wxGrid::CalcWindowSizes()
5052 // escape if the window is has not been fully created yet
5054 if ( m_cornerLabelWin
== NULL
)
5058 GetClientSize( &cw
, &ch
);
5060 // the grid may be too small to have enough space for the labels yet, don't
5061 // size the windows to negative sizes in this case
5062 int gw
= cw
- m_rowLabelWidth
;
5063 int gh
= ch
- m_colLabelHeight
;
5069 if ( m_cornerLabelWin
&& m_cornerLabelWin
->IsShown() )
5070 m_cornerLabelWin
->SetSize( 0, 0, m_rowLabelWidth
, m_colLabelHeight
);
5072 if ( m_colWindow
&& m_colWindow
->IsShown() )
5073 m_colWindow
->SetSize( m_rowLabelWidth
, 0, gw
, m_colLabelHeight
);
5075 if ( m_rowLabelWin
&& m_rowLabelWin
->IsShown() )
5076 m_rowLabelWin
->SetSize( 0, m_colLabelHeight
, m_rowLabelWidth
, gh
);
5078 if ( m_gridWin
&& m_gridWin
->IsShown() )
5079 m_gridWin
->SetSize( m_rowLabelWidth
, m_colLabelHeight
, gw
, gh
);
5082 // this is called when the grid table sends a message
5083 // to indicate that it has been redimensioned
5085 bool wxGrid::Redimension( wxGridTableMessage
& msg
)
5088 bool result
= false;
5090 // Clear the attribute cache as the attribute might refer to a different
5091 // cell than stored in the cache after adding/removing rows/columns.
5094 // By the same reasoning, the editor should be dismissed if columns are
5095 // added or removed. And for consistency, it should IMHO always be
5096 // removed, not only if the cell "underneath" it actually changes.
5097 // For now, I intentionally do not save the editor's content as the
5098 // cell it might want to save that stuff to might no longer exist.
5099 HideCellEditControl();
5102 // if we were using the default widths/heights so far, we must change them
5104 if ( m_colWidths
.IsEmpty() )
5109 if ( m_rowHeights
.IsEmpty() )
5115 switch ( msg
.GetId() )
5117 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
5119 size_t pos
= msg
.GetCommandInt();
5120 int numRows
= msg
.GetCommandInt2();
5122 m_numRows
+= numRows
;
5124 if ( !m_rowHeights
.IsEmpty() )
5126 m_rowHeights
.Insert( m_defaultRowHeight
, pos
, numRows
);
5127 m_rowBottoms
.Insert( 0, pos
, numRows
);
5131 bottom
= m_rowBottoms
[pos
- 1];
5133 for ( i
= pos
; i
< m_numRows
; i
++ )
5135 bottom
+= m_rowHeights
[i
];
5136 m_rowBottoms
[i
] = bottom
;
5140 if ( m_currentCellCoords
== wxGridNoCellCoords
)
5142 // if we have just inserted cols into an empty grid the current
5143 // cell will be undefined...
5145 SetCurrentCell( 0, 0 );
5149 m_selection
->UpdateRows( pos
, numRows
);
5150 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
5152 attrProvider
->UpdateAttrRows( pos
, numRows
);
5154 if ( !GetBatchCount() )
5157 m_rowLabelWin
->Refresh();
5163 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
5165 int numRows
= msg
.GetCommandInt();
5166 int oldNumRows
= m_numRows
;
5167 m_numRows
+= numRows
;
5169 if ( !m_rowHeights
.IsEmpty() )
5171 m_rowHeights
.Add( m_defaultRowHeight
, numRows
);
5172 m_rowBottoms
.Add( 0, numRows
);
5175 if ( oldNumRows
> 0 )
5176 bottom
= m_rowBottoms
[oldNumRows
- 1];
5178 for ( i
= oldNumRows
; i
< m_numRows
; i
++ )
5180 bottom
+= m_rowHeights
[i
];
5181 m_rowBottoms
[i
] = bottom
;
5185 if ( m_currentCellCoords
== wxGridNoCellCoords
)
5187 // if we have just inserted cols into an empty grid the current
5188 // cell will be undefined...
5190 SetCurrentCell( 0, 0 );
5193 if ( !GetBatchCount() )
5196 m_rowLabelWin
->Refresh();
5202 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
5204 size_t pos
= msg
.GetCommandInt();
5205 int numRows
= msg
.GetCommandInt2();
5206 m_numRows
-= numRows
;
5208 if ( !m_rowHeights
.IsEmpty() )
5210 m_rowHeights
.RemoveAt( pos
, numRows
);
5211 m_rowBottoms
.RemoveAt( pos
, numRows
);
5214 for ( i
= 0; i
< m_numRows
; i
++ )
5216 h
+= m_rowHeights
[i
];
5217 m_rowBottoms
[i
] = h
;
5223 m_currentCellCoords
= wxGridNoCellCoords
;
5227 if ( m_currentCellCoords
.GetRow() >= m_numRows
)
5228 m_currentCellCoords
.Set( 0, 0 );
5232 m_selection
->UpdateRows( pos
, -((int)numRows
) );
5233 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
5236 attrProvider
->UpdateAttrRows( pos
, -((int)numRows
) );
5238 // ifdef'd out following patch from Paul Gammans
5240 // No need to touch column attributes, unless we
5241 // removed _all_ rows, in this case, we remove
5242 // all column attributes.
5243 // I hate to do this here, but the
5244 // needed data is not available inside UpdateAttrRows.
5245 if ( !GetNumberRows() )
5246 attrProvider
->UpdateAttrCols( 0, -GetNumberCols() );
5250 if ( !GetBatchCount() )
5253 m_rowLabelWin
->Refresh();
5259 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
5261 size_t pos
= msg
.GetCommandInt();
5262 int numCols
= msg
.GetCommandInt2();
5263 m_numCols
+= numCols
;
5265 if ( m_useNativeHeader
)
5266 GetColHeader()->SetColumnCount(m_numCols
);
5268 if ( !m_colAt
.IsEmpty() )
5270 //Shift the column IDs
5272 for ( i
= 0; i
< m_numCols
- numCols
; i
++ )
5274 if ( m_colAt
[i
] >= (int)pos
)
5275 m_colAt
[i
] += numCols
;
5278 m_colAt
.Insert( pos
, pos
, numCols
);
5280 //Set the new columns' positions
5281 for ( i
= pos
+ 1; i
< (int)pos
+ numCols
; i
++ )
5287 if ( !m_colWidths
.IsEmpty() )
5289 m_colWidths
.Insert( m_defaultColWidth
, pos
, numCols
);
5290 m_colRights
.Insert( 0, pos
, numCols
);
5294 right
= m_colRights
[GetColAt( pos
- 1 )];
5297 for ( colPos
= pos
; colPos
< m_numCols
; colPos
++ )
5299 i
= GetColAt( colPos
);
5301 right
+= m_colWidths
[i
];
5302 m_colRights
[i
] = right
;
5306 if ( m_currentCellCoords
== wxGridNoCellCoords
)
5308 // if we have just inserted cols into an empty grid the current
5309 // cell will be undefined...
5311 SetCurrentCell( 0, 0 );
5315 m_selection
->UpdateCols( pos
, numCols
);
5316 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
5318 attrProvider
->UpdateAttrCols( pos
, numCols
);
5319 if ( !GetBatchCount() )
5322 m_colWindow
->Refresh();
5328 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
5330 int numCols
= msg
.GetCommandInt();
5331 int oldNumCols
= m_numCols
;
5332 m_numCols
+= numCols
;
5333 if ( m_useNativeHeader
)
5334 GetColHeader()->SetColumnCount(m_numCols
);
5336 if ( !m_colAt
.IsEmpty() )
5338 m_colAt
.Add( 0, numCols
);
5340 //Set the new columns' positions
5342 for ( i
= oldNumCols
; i
< m_numCols
; i
++ )
5348 if ( !m_colWidths
.IsEmpty() )
5350 m_colWidths
.Add( m_defaultColWidth
, numCols
);
5351 m_colRights
.Add( 0, numCols
);
5354 if ( oldNumCols
> 0 )
5355 right
= m_colRights
[GetColAt( oldNumCols
- 1 )];
5358 for ( colPos
= oldNumCols
; colPos
< m_numCols
; colPos
++ )
5360 i
= GetColAt( colPos
);
5362 right
+= m_colWidths
[i
];
5363 m_colRights
[i
] = right
;
5367 if ( m_currentCellCoords
== wxGridNoCellCoords
)
5369 // if we have just inserted cols into an empty grid the current
5370 // cell will be undefined...
5372 SetCurrentCell( 0, 0 );
5374 if ( !GetBatchCount() )
5377 m_colWindow
->Refresh();
5383 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
5385 size_t pos
= msg
.GetCommandInt();
5386 int numCols
= msg
.GetCommandInt2();
5387 m_numCols
-= numCols
;
5388 if ( m_useNativeHeader
)
5389 GetColHeader()->SetColumnCount(m_numCols
);
5391 if ( !m_colAt
.IsEmpty() )
5393 int colID
= GetColAt( pos
);
5395 m_colAt
.RemoveAt( pos
, numCols
);
5397 //Shift the column IDs
5399 for ( colPos
= 0; colPos
< m_numCols
; colPos
++ )
5401 if ( m_colAt
[colPos
] > colID
)
5402 m_colAt
[colPos
] -= numCols
;
5406 if ( !m_colWidths
.IsEmpty() )
5408 m_colWidths
.RemoveAt( pos
, numCols
);
5409 m_colRights
.RemoveAt( pos
, numCols
);
5413 for ( colPos
= 0; colPos
< m_numCols
; colPos
++ )
5415 i
= GetColAt( colPos
);
5417 w
+= m_colWidths
[i
];
5424 m_currentCellCoords
= wxGridNoCellCoords
;
5428 if ( m_currentCellCoords
.GetCol() >= m_numCols
)
5429 m_currentCellCoords
.Set( 0, 0 );
5433 m_selection
->UpdateCols( pos
, -((int)numCols
) );
5434 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
5437 attrProvider
->UpdateAttrCols( pos
, -((int)numCols
) );
5439 // ifdef'd out following patch from Paul Gammans
5441 // No need to touch row attributes, unless we
5442 // removed _all_ columns, in this case, we remove
5443 // all row attributes.
5444 // I hate to do this here, but the
5445 // needed data is not available inside UpdateAttrCols.
5446 if ( !GetNumberCols() )
5447 attrProvider
->UpdateAttrRows( 0, -GetNumberRows() );
5451 if ( !GetBatchCount() )
5454 m_colWindow
->Refresh();
5461 if (result
&& !GetBatchCount() )
5462 m_gridWin
->Refresh();
5467 wxArrayInt
wxGrid::CalcRowLabelsExposed( const wxRegion
& reg
) const
5469 wxRegionIterator
iter( reg
);
5472 wxArrayInt rowlabels
;
5479 // TODO: remove this when we can...
5480 // There is a bug in wxMotif that gives garbage update
5481 // rectangles if you jump-scroll a long way by clicking the
5482 // scrollbar with middle button. This is a work-around
5484 #if defined(__WXMOTIF__)
5486 m_gridWin
->GetClientSize( &cw
, &ch
);
5487 if ( r
.GetTop() > ch
)
5489 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
5492 // logical bounds of update region
5495 CalcUnscrolledPosition( 0, r
.GetTop(), &dummy
, &top
);
5496 CalcUnscrolledPosition( 0, r
.GetBottom(), &dummy
, &bottom
);
5498 // find the row labels within these bounds
5501 for ( row
= internalYToRow(top
); row
< m_numRows
; row
++ )
5503 if ( GetRowBottom(row
) < top
)
5506 if ( GetRowTop(row
) > bottom
)
5509 rowlabels
.Add( row
);
5518 wxArrayInt
wxGrid::CalcColLabelsExposed( const wxRegion
& reg
) const
5520 wxRegionIterator
iter( reg
);
5523 wxArrayInt colLabels
;
5530 // TODO: remove this when we can...
5531 // There is a bug in wxMotif that gives garbage update
5532 // rectangles if you jump-scroll a long way by clicking the
5533 // scrollbar with middle button. This is a work-around
5535 #if defined(__WXMOTIF__)
5537 m_gridWin
->GetClientSize( &cw
, &ch
);
5538 if ( r
.GetLeft() > cw
)
5540 r
.SetRight( wxMin( r
.GetRight(), cw
) );
5543 // logical bounds of update region
5546 CalcUnscrolledPosition( r
.GetLeft(), 0, &left
, &dummy
);
5547 CalcUnscrolledPosition( r
.GetRight(), 0, &right
, &dummy
);
5549 // find the cells within these bounds
5553 for ( colPos
= GetColPos( internalXToCol(left
) ); colPos
< m_numCols
; colPos
++ )
5555 col
= GetColAt( colPos
);
5557 if ( GetColRight(col
) < left
)
5560 if ( GetColLeft(col
) > right
)
5563 colLabels
.Add( col
);
5572 wxGridCellCoordsArray
wxGrid::CalcCellsExposed( const wxRegion
& reg
) const
5574 wxRegionIterator
iter( reg
);
5577 wxGridCellCoordsArray cellsExposed
;
5579 int left
, top
, right
, bottom
;
5584 // TODO: remove this when we can...
5585 // There is a bug in wxMotif that gives garbage update
5586 // rectangles if you jump-scroll a long way by clicking the
5587 // scrollbar with middle button. This is a work-around
5589 #if defined(__WXMOTIF__)
5591 m_gridWin
->GetClientSize( &cw
, &ch
);
5592 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
5593 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
5594 r
.SetRight( wxMin( r
.GetRight(), cw
) );
5595 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
5598 // logical bounds of update region
5600 CalcUnscrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
5601 CalcUnscrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
5603 // find the cells within these bounds
5606 for ( row
= internalYToRow(top
); row
< m_numRows
; row
++ )
5608 if ( GetRowBottom(row
) <= top
)
5611 if ( GetRowTop(row
) > bottom
)
5615 for ( colPos
= GetColPos( internalXToCol(left
) ); colPos
< m_numCols
; colPos
++ )
5617 col
= GetColAt( colPos
);
5619 if ( GetColRight(col
) <= left
)
5622 if ( GetColLeft(col
) > right
)
5625 cellsExposed
.Add( wxGridCellCoords( row
, col
) );
5632 return cellsExposed
;
5636 void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent
& event
)
5639 wxPoint
pos( event
.GetPosition() );
5640 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
5642 if ( event
.Dragging() )
5646 m_isDragging
= true;
5647 m_rowLabelWin
->CaptureMouse();
5650 if ( event
.LeftIsDown() )
5652 switch ( m_cursorMode
)
5654 case WXGRID_CURSOR_RESIZE_ROW
:
5656 int cw
, ch
, left
, dummy
;
5657 m_gridWin
->GetClientSize( &cw
, &ch
);
5658 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
5660 wxClientDC
dc( m_gridWin
);
5663 GetRowTop(m_dragRowOrCol
) +
5664 GetRowMinimalHeight(m_dragRowOrCol
) );
5665 dc
.SetLogicalFunction(wxINVERT
);
5666 if ( m_dragLastPos
>= 0 )
5668 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
5670 dc
.DrawLine( left
, y
, left
+cw
, y
);
5675 case WXGRID_CURSOR_SELECT_ROW
:
5677 if ( (row
= YToRow( y
)) >= 0 )
5680 m_selection
->SelectRow(row
, event
);
5685 // default label to suppress warnings about "enumeration value
5686 // 'xxx' not handled in switch
5694 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
5699 if (m_rowLabelWin
->HasCapture())
5700 m_rowLabelWin
->ReleaseMouse();
5701 m_isDragging
= false;
5704 // ------------ Entering or leaving the window
5706 if ( event
.Entering() || event
.Leaving() )
5708 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
5711 // ------------ Left button pressed
5713 else if ( event
.LeftDown() )
5715 // don't send a label click event for a hit on the
5716 // edge of the row label - this is probably the user
5717 // wanting to resize the row
5719 if ( YToEdgeOfRow(y
) < 0 )
5723 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, row
, -1, event
) )
5725 if ( !event
.ShiftDown() && !event
.CmdDown() )
5729 if ( event
.ShiftDown() )
5731 m_selection
->SelectBlock
5733 m_currentCellCoords
.GetRow(), 0,
5734 row
, GetNumberCols() - 1,
5740 m_selection
->SelectRow(row
, event
);
5744 ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW
, m_rowLabelWin
);
5749 // starting to drag-resize a row
5750 if ( CanDragRowSize() )
5751 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
);
5755 // ------------ Left double click
5757 else if (event
.LeftDClick() )
5759 row
= YToEdgeOfRow(y
);
5764 !SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, row
, -1, event
) )
5766 // no default action at the moment
5771 // adjust row height depending on label text
5772 AutoSizeRowLabelSize( row
);
5774 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, GetColLabelWindow());
5779 // ------------ Left button released
5781 else if ( event
.LeftUp() )
5783 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
5785 DoEndDragResizeRow();
5787 // Note: we are ending the event *after* doing
5788 // default processing in this case
5790 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
5793 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
5797 // ------------ Right button down
5799 else if ( event
.RightDown() )
5803 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, row
, -1, event
) )
5805 // no default action at the moment
5809 // ------------ Right double click
5811 else if ( event
.RightDClick() )
5815 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, row
, -1, event
) )
5817 // no default action at the moment
5821 // ------------ No buttons down and mouse moving
5823 else if ( event
.Moving() )
5825 m_dragRowOrCol
= YToEdgeOfRow( y
);
5826 if ( m_dragRowOrCol
>= 0 )
5828 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5830 // don't capture the mouse yet
5831 if ( CanDragRowSize() )
5832 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
, false);
5835 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5837 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
, false);
5842 void wxGrid::DoStartResizeCol(int col
)
5844 m_dragRowOrCol
= col
;
5846 DoUpdateResizeColWidth(GetColWidth(m_dragRowOrCol
));
5849 void wxGrid::DoUpdateResizeCol(int x
)
5851 int cw
, ch
, dummy
, top
;
5852 m_gridWin
->GetClientSize( &cw
, &ch
);
5853 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
5855 wxClientDC
dc( m_gridWin
);
5858 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) + GetColMinimalWidth(m_dragRowOrCol
));
5859 dc
.SetLogicalFunction(wxINVERT
);
5860 if ( m_dragLastPos
>= 0 )
5862 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ ch
);
5864 dc
.DrawLine( x
, top
, x
, top
+ ch
);
5868 void wxGrid::DoUpdateResizeColWidth(int w
)
5870 DoUpdateResizeCol(GetColLeft(m_dragRowOrCol
) + w
);
5873 void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent
& event
)
5876 wxPoint
pos( event
.GetPosition() );
5877 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
5879 if ( event
.Dragging() )
5883 m_isDragging
= true;
5884 GetColLabelWindow()->CaptureMouse();
5886 if ( m_cursorMode
== WXGRID_CURSOR_MOVE_COL
)
5887 m_dragRowOrCol
= XToCol( x
);
5890 if ( event
.LeftIsDown() )
5892 switch ( m_cursorMode
)
5894 case WXGRID_CURSOR_RESIZE_COL
:
5895 DoUpdateResizeCol(x
);
5898 case WXGRID_CURSOR_SELECT_COL
:
5900 if ( (col
= XToCol( x
)) >= 0 )
5903 m_selection
->SelectCol(col
, event
);
5908 case WXGRID_CURSOR_MOVE_COL
:
5911 m_moveToCol
= GetColAt( 0 );
5913 m_moveToCol
= XToCol( x
);
5917 if ( m_moveToCol
< 0 )
5918 markerX
= GetColRight( GetColAt( m_numCols
- 1 ) );
5919 else if ( x
>= (GetColLeft( m_moveToCol
) + (GetColWidth(m_moveToCol
) / 2)) )
5921 m_moveToCol
= GetColAt( GetColPos( m_moveToCol
) + 1 );
5922 if ( m_moveToCol
< 0 )
5923 markerX
= GetColRight( GetColAt( m_numCols
- 1 ) );
5925 markerX
= GetColLeft( m_moveToCol
);
5928 markerX
= GetColLeft( m_moveToCol
);
5930 if ( markerX
!= m_dragLastPos
)
5932 wxClientDC
dc( GetColLabelWindow() );
5936 GetColLabelWindow()->GetClientSize( &cw
, &ch
);
5940 //Clean up the last indicator
5941 if ( m_dragLastPos
>= 0 )
5943 wxPen
pen( GetColLabelWindow()->GetBackgroundColour(), 2 );
5945 dc
.DrawLine( m_dragLastPos
+ 1, 0, m_dragLastPos
+ 1, ch
);
5946 dc
.SetPen(wxNullPen
);
5948 if ( XToCol( m_dragLastPos
) != -1 )
5949 DrawColLabel( dc
, XToCol( m_dragLastPos
) );
5952 const wxColour
*color
;
5953 //Moving to the same place? Don't draw a marker
5954 if ( (m_moveToCol
== m_dragRowOrCol
)
5955 || (GetColPos( m_moveToCol
) == GetColPos( m_dragRowOrCol
) + 1)
5956 || (m_moveToCol
< 0 && m_dragRowOrCol
== GetColAt( m_numCols
- 1 )))
5957 color
= wxLIGHT_GREY
;
5962 wxPen
pen( *color
, 2 );
5965 dc
.DrawLine( markerX
, 0, markerX
, ch
);
5967 dc
.SetPen(wxNullPen
);
5969 m_dragLastPos
= markerX
- 1;
5974 // default label to suppress warnings about "enumeration value
5975 // 'xxx' not handled in switch
5983 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
5988 if (GetColLabelWindow()->HasCapture())
5989 GetColLabelWindow()->ReleaseMouse();
5990 m_isDragging
= false;
5993 // ------------ Entering or leaving the window
5995 if ( event
.Entering() || event
.Leaving() )
5997 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, GetColLabelWindow());
6000 // ------------ Left button pressed
6002 else if ( event
.LeftDown() )
6004 // don't send a label click event for a hit on the
6005 // edge of the col label - this is probably the user
6006 // wanting to resize the col
6008 if ( XToEdgeOfCol(x
) < 0 )
6012 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, col
, event
) )
6014 if ( m_canDragColMove
)
6016 //Show button as pressed
6017 wxClientDC
dc( GetColLabelWindow() );
6018 int colLeft
= GetColLeft( col
);
6019 int colRight
= GetColRight( col
) - 1;
6020 dc
.SetPen( wxPen( GetColLabelWindow()->GetBackgroundColour(), 1 ) );
6021 dc
.DrawLine( colLeft
, 1, colLeft
, m_colLabelHeight
-1 );
6022 dc
.DrawLine( colLeft
, 1, colRight
, 1 );
6024 ChangeCursorMode(WXGRID_CURSOR_MOVE_COL
, GetColLabelWindow());
6028 if ( !event
.ShiftDown() && !event
.CmdDown() )
6032 if ( event
.ShiftDown() )
6034 m_selection
->SelectBlock
6036 0, m_currentCellCoords
.GetCol(),
6037 GetNumberRows() - 1, col
,
6043 m_selection
->SelectCol(col
, event
);
6047 ChangeCursorMode(WXGRID_CURSOR_SELECT_COL
, GetColLabelWindow());
6053 // starting to drag-resize a col
6055 if ( CanDragColSize() )
6056 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, GetColLabelWindow());
6060 // ------------ Left double click
6062 if ( event
.LeftDClick() )
6064 col
= XToEdgeOfCol(x
);
6069 ! SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, col
, event
) )
6071 // no default action at the moment
6076 // adjust column width depending on label text
6077 AutoSizeColLabelSize( col
);
6079 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, GetColLabelWindow());
6084 // ------------ Left button released
6086 else if ( event
.LeftUp() )
6088 switch ( m_cursorMode
)
6090 case WXGRID_CURSOR_RESIZE_COL
:
6091 DoEndDragResizeCol();
6094 case WXGRID_CURSOR_MOVE_COL
:
6097 SendEvent( wxEVT_GRID_COL_MOVE
, -1, m_dragRowOrCol
, event
);
6100 case WXGRID_CURSOR_SELECT_COL
:
6101 case WXGRID_CURSOR_SELECT_CELL
:
6102 case WXGRID_CURSOR_RESIZE_ROW
:
6103 case WXGRID_CURSOR_SELECT_ROW
:
6104 // nothing to do (?)
6108 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, GetColLabelWindow());
6112 // ------------ Right button down
6114 else if ( event
.RightDown() )
6118 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, col
, event
) )
6120 // no default action at the moment
6124 // ------------ Right double click
6126 else if ( event
.RightDClick() )
6130 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, col
, event
) )
6132 // no default action at the moment
6136 // ------------ No buttons down and mouse moving
6138 else if ( event
.Moving() )
6140 m_dragRowOrCol
= XToEdgeOfCol( x
);
6141 if ( m_dragRowOrCol
>= 0 )
6143 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
6145 // don't capture the cursor yet
6146 if ( CanDragColSize() )
6147 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, GetColLabelWindow(), false);
6150 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
6152 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, GetColLabelWindow(), false);
6157 void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent
& event
)
6159 if ( event
.LeftDown() )
6161 // indicate corner label by having both row and
6164 if ( !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, -1, event
) )
6169 else if ( event
.LeftDClick() )
6171 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, -1, event
);
6173 else if ( event
.RightDown() )
6175 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, -1, event
) )
6177 // no default action at the moment
6180 else if ( event
.RightDClick() )
6182 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, -1, event
) )
6184 // no default action at the moment
6189 void wxGrid::CancelMouseCapture()
6191 // cancel operation currently in progress, whatever it is
6194 m_isDragging
= false;
6195 m_startDragPos
= wxDefaultPosition
;
6197 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
6198 m_winCapture
->SetCursor( *wxSTANDARD_CURSOR
);
6199 m_winCapture
= NULL
;
6201 // remove traces of whatever we drew on screen
6206 void wxGrid::ChangeCursorMode(CursorMode mode
,
6211 static const wxChar
*cursorModes
[] =
6221 wxLogTrace(_T("grid"),
6222 _T("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
6223 win
== m_colWindow
? _T("colLabelWin")
6224 : win
? _T("rowLabelWin")
6226 cursorModes
[m_cursorMode
], cursorModes
[mode
]);
6229 if ( mode
== m_cursorMode
&&
6230 win
== m_winCapture
&&
6231 captureMouse
== (m_winCapture
!= NULL
))
6236 // by default use the grid itself
6242 m_winCapture
->ReleaseMouse();
6243 m_winCapture
= NULL
;
6246 m_cursorMode
= mode
;
6248 switch ( m_cursorMode
)
6250 case WXGRID_CURSOR_RESIZE_ROW
:
6251 win
->SetCursor( m_rowResizeCursor
);
6254 case WXGRID_CURSOR_RESIZE_COL
:
6255 win
->SetCursor( m_colResizeCursor
);
6258 case WXGRID_CURSOR_MOVE_COL
:
6259 win
->SetCursor( wxCursor(wxCURSOR_HAND
) );
6263 win
->SetCursor( *wxSTANDARD_CURSOR
);
6267 // we need to capture mouse when resizing
6268 bool resize
= m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
||
6269 m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
;
6271 if ( captureMouse
&& resize
)
6273 win
->CaptureMouse();
6278 // ----------------------------------------------------------------------------
6279 // grid mouse event processing
6280 // ----------------------------------------------------------------------------
6283 wxGrid::DoGridCellDrag(wxMouseEvent
& event
,
6284 const wxGridCellCoords
& coords
,
6287 if ( coords
== wxGridNoCellCoords
)
6288 return; // we're outside any valid cell
6290 // Hide the edit control, so it won't interfere with drag-shrinking.
6291 if ( IsCellEditControlShown() )
6293 HideCellEditControl();
6294 SaveEditControlValue();
6297 switch ( event
.GetModifiers() )
6300 if ( m_selectedBlockCorner
== wxGridNoCellCoords
)
6301 m_selectedBlockCorner
= coords
;
6302 UpdateBlockBeingSelected(m_selectedBlockCorner
, coords
);
6306 if ( CanDragCell() )
6310 if ( m_selectedBlockCorner
== wxGridNoCellCoords
)
6311 m_selectedBlockCorner
= coords
;
6313 SendEvent(wxEVT_GRID_CELL_BEGIN_DRAG
, coords
, event
);
6318 UpdateBlockBeingSelected(m_currentCellCoords
, coords
);
6322 // we don't handle the other key modifiers
6327 void wxGrid::DoGridLineDrag(wxMouseEvent
& event
, const wxGridOperations
& oper
)
6329 wxClientDC
dc(m_gridWin
);
6331 dc
.SetLogicalFunction(wxINVERT
);
6333 const wxRect
rectWin(CalcUnscrolledPosition(wxPoint(0, 0)),
6334 m_gridWin
->GetClientSize());
6336 // erase the previously drawn line, if any
6337 if ( m_dragLastPos
>= 0 )
6338 oper
.DrawParallelLineInRect(dc
, rectWin
, m_dragLastPos
);
6340 // we need the vertical position for rows and horizontal for columns here
6341 m_dragLastPos
= oper
.Dual().Select(CalcUnscrolledPosition(event
.GetPosition()));
6343 // don't allow resizing beneath the minimal size
6344 const int posMin
= oper
.GetLineStartPos(this, m_dragRowOrCol
) +
6345 oper
.GetMinimalLineSize(this, m_dragRowOrCol
);
6346 if ( m_dragLastPos
< posMin
)
6347 m_dragLastPos
= posMin
;
6349 // and draw it at the new position
6350 oper
.DrawParallelLineInRect(dc
, rectWin
, m_dragLastPos
);
6353 void wxGrid::DoGridDragEvent(wxMouseEvent
& event
, const wxGridCellCoords
& coords
)
6355 if ( !m_isDragging
)
6357 // Don't start doing anything until the mouse has been dragged far
6359 const wxPoint
& pt
= event
.GetPosition();
6360 if ( m_startDragPos
== wxDefaultPosition
)
6362 m_startDragPos
= pt
;
6366 if ( abs(m_startDragPos
.x
- pt
.x
) <= DRAG_SENSITIVITY
&&
6367 abs(m_startDragPos
.y
- pt
.y
) <= DRAG_SENSITIVITY
)
6371 const bool isFirstDrag
= !m_isDragging
;
6372 m_isDragging
= true;
6374 switch ( m_cursorMode
)
6376 case WXGRID_CURSOR_SELECT_CELL
:
6377 DoGridCellDrag(event
, coords
, isFirstDrag
);
6380 case WXGRID_CURSOR_RESIZE_ROW
:
6381 DoGridLineDrag(event
, wxGridRowOperations());
6384 case WXGRID_CURSOR_RESIZE_COL
:
6385 DoGridLineDrag(event
, wxGridColumnOperations());
6394 m_winCapture
= m_gridWin
;
6395 m_winCapture
->CaptureMouse();
6400 wxGrid::DoGridCellLeftDown(wxMouseEvent
& event
,
6401 const wxGridCellCoords
& coords
,
6404 if ( SendEvent(wxEVT_GRID_CELL_LEFT_CLICK
, coords
, event
) )
6406 // event handled by user code, no need to do anything here
6410 if ( !event
.CmdDown() )
6413 if ( event
.ShiftDown() )
6417 m_selection
->SelectBlock(m_currentCellCoords
, coords
, event
);
6418 m_selectedBlockCorner
= coords
;
6421 else if ( XToEdgeOfCol(pos
.x
) < 0 && YToEdgeOfRow(pos
.y
) < 0 )
6423 DisableCellEditControl();
6424 MakeCellVisible( coords
);
6426 if ( event
.CmdDown() )
6430 m_selection
->ToggleCellSelection(coords
, event
);
6433 m_selectedBlockTopLeft
= wxGridNoCellCoords
;
6434 m_selectedBlockBottomRight
= wxGridNoCellCoords
;
6435 m_selectedBlockCorner
= coords
;
6439 m_waitForSlowClick
= m_currentCellCoords
== coords
&&
6440 coords
!= wxGridNoCellCoords
;
6441 SetCurrentCell( coords
);
6447 wxGrid::DoGridCellLeftDClick(wxMouseEvent
& event
,
6448 const wxGridCellCoords
& coords
,
6451 if ( XToEdgeOfCol(pos
.x
) < 0 && YToEdgeOfRow(pos
.y
) < 0 )
6453 if ( !SendEvent(wxEVT_GRID_CELL_LEFT_DCLICK
, coords
, event
) )
6455 // we want double click to select a cell and start editing
6456 // (i.e. to behave in same way as sequence of two slow clicks):
6457 m_waitForSlowClick
= true;
6463 wxGrid::DoGridCellLeftUp(wxMouseEvent
& event
, const wxGridCellCoords
& coords
)
6465 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
6469 m_winCapture
->ReleaseMouse();
6470 m_winCapture
= NULL
;
6473 if ( coords
== m_currentCellCoords
&& m_waitForSlowClick
&& CanEnableCellControl() )
6476 EnableCellEditControl();
6478 wxGridCellAttr
*attr
= GetCellAttr(coords
);
6479 wxGridCellEditor
*editor
= attr
->GetEditor(this, coords
.GetRow(), coords
.GetCol());
6480 editor
->StartingClick();
6484 m_waitForSlowClick
= false;
6486 else if ( m_selectedBlockTopLeft
!= wxGridNoCellCoords
&&
6487 m_selectedBlockBottomRight
!= wxGridNoCellCoords
)
6491 m_selection
->SelectBlock( m_selectedBlockTopLeft
,
6492 m_selectedBlockBottomRight
,
6496 m_selectedBlockTopLeft
= wxGridNoCellCoords
;
6497 m_selectedBlockBottomRight
= wxGridNoCellCoords
;
6499 // Show the edit control, if it has been hidden for
6501 ShowCellEditControl();
6504 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
6506 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6507 DoEndDragResizeRow();
6509 // Note: we are ending the event *after* doing
6510 // default processing in this case
6512 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
6514 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
6516 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6517 DoEndDragResizeCol();
6524 wxGrid::DoGridMouseMoveEvent(wxMouseEvent
& WXUNUSED(event
),
6525 const wxGridCellCoords
& coords
,
6528 if ( coords
.GetRow() < 0 || coords
.GetCol() < 0 )
6530 // out of grid cell area
6531 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6535 int dragRow
= YToEdgeOfRow( pos
.y
);
6536 int dragCol
= XToEdgeOfCol( pos
.x
);
6538 // Dragging on the corner of a cell to resize in both
6539 // directions is not implemented yet...
6541 if ( dragRow
>= 0 && dragCol
>= 0 )
6543 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6549 m_dragRowOrCol
= dragRow
;
6551 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
6553 if ( CanDragRowSize() && CanDragGridSize() )
6554 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, NULL
, false);
6557 // When using the native header window we can only resize the columns by
6558 // dragging the dividers in it because we can't make it enter into the
6559 // column resizing mode programmatically
6560 else if ( dragCol
>= 0 && !m_useNativeHeader
)
6562 m_dragRowOrCol
= dragCol
;
6564 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
6566 if ( CanDragColSize() && CanDragGridSize() )
6567 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, NULL
, false);
6570 else // Neither on a row or col edge
6572 if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
6574 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6579 void wxGrid::ProcessGridCellMouseEvent(wxMouseEvent
& event
)
6581 const wxPoint pos
= CalcUnscrolledPosition(event
.GetPosition());
6583 // coordinates of the cell under mouse
6584 wxGridCellCoords coords
= XYToCell(pos
);
6586 int cell_rows
, cell_cols
;
6587 GetCellSize( coords
.GetRow(), coords
.GetCol(), &cell_rows
, &cell_cols
);
6588 if ( (cell_rows
< 0) || (cell_cols
< 0) )
6590 coords
.SetRow(coords
.GetRow() + cell_rows
);
6591 coords
.SetCol(coords
.GetCol() + cell_cols
);
6594 if ( event
.Dragging() )
6596 if ( event
.LeftIsDown() )
6597 DoGridDragEvent(event
, coords
);
6603 m_isDragging
= false;
6604 m_startDragPos
= wxDefaultPosition
;
6606 // VZ: if we do this, the mode is reset to WXGRID_CURSOR_SELECT_CELL
6607 // immediately after it becomes WXGRID_CURSOR_RESIZE_ROW/COL under
6610 if ( event
.Entering() || event
.Leaving() )
6612 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6613 m_gridWin
->SetCursor( *wxSTANDARD_CURSOR
);
6617 // deal with various button presses
6618 if ( event
.IsButton() )
6620 if ( coords
!= wxGridNoCellCoords
)
6622 DisableCellEditControl();
6624 if ( event
.LeftDown() )
6625 DoGridCellLeftDown(event
, coords
, pos
);
6626 else if ( event
.LeftDClick() )
6627 DoGridCellLeftDClick(event
, coords
, pos
);
6628 else if ( event
.RightDown() )
6629 SendEvent(wxEVT_GRID_CELL_RIGHT_CLICK
, coords
, event
);
6630 else if ( event
.RightDClick() )
6631 SendEvent(wxEVT_GRID_CELL_RIGHT_DCLICK
, coords
, event
);
6634 // this one should be called even if we're not over any cell
6635 if ( event
.LeftUp() )
6637 DoGridCellLeftUp(event
, coords
);
6640 else if ( event
.Moving() )
6642 DoGridMouseMoveEvent(event
, coords
, pos
);
6644 else // unknown mouse event?
6650 void wxGrid::DoEndDragResizeLine(const wxGridOperations
& oper
)
6652 if ( m_dragLastPos
== -1 )
6655 const wxGridOperations
& doper
= oper
.Dual();
6657 const wxSize size
= m_gridWin
->GetClientSize();
6659 const wxPoint ptOrigin
= CalcUnscrolledPosition(wxPoint(0, 0));
6661 // erase the last line we drew
6662 wxClientDC
dc(m_gridWin
);
6664 dc
.SetLogicalFunction(wxINVERT
);
6666 const int posLineStart
= oper
.Select(ptOrigin
);
6667 const int posLineEnd
= oper
.Select(ptOrigin
) + oper
.Select(size
);
6669 oper
.DrawParallelLine(dc
, posLineStart
, posLineEnd
, m_dragLastPos
);
6671 // temporarily hide the edit control before resizing
6672 HideCellEditControl();
6673 SaveEditControlValue();
6675 // do resize the line
6676 const int lineStart
= oper
.GetLineStartPos(this, m_dragRowOrCol
);
6677 oper
.SetLineSize(this, m_dragRowOrCol
,
6678 wxMax(m_dragLastPos
- lineStart
,
6679 oper
.GetMinimalLineSize(this, m_dragRowOrCol
)));
6683 // refresh now if we're not frozen
6684 if ( !GetBatchCount() )
6686 // we need to refresh everything beyond the resized line in the header
6689 // get the position from which to refresh in the other direction
6690 wxRect
rect(CellToRect(oper
.MakeCoords(m_dragRowOrCol
, 0)));
6691 rect
.SetPosition(CalcScrolledPosition(rect
.GetPosition()));
6693 // we only need the ordinate (for rows) or abscissa (for columns) here,
6694 // and need to cover the entire window in the other direction
6695 oper
.Select(rect
) = 0;
6697 wxRect
rectHeader(rect
.GetPosition(),
6700 oper
.GetHeaderWindowSize(this),
6701 doper
.Select(size
) - doper
.Select(rect
)
6704 oper
.GetHeaderWindow(this)->Refresh(true, &rectHeader
);
6707 // also refresh the grid window: extend the rectangle
6710 oper
.SelectSize(rect
) = oper
.Select(size
);
6712 int subtractLines
= 0;
6713 const int lineStart
= oper
.PosToLine(this, posLineStart
);
6714 if ( lineStart
>= 0 )
6716 // ensure that if we have a multi-cell block we redraw all of
6717 // it by increasing the refresh area to cover it entirely if a
6718 // part of it is affected
6719 const int lineEnd
= oper
.PosToLine(this, posLineEnd
, true);
6720 for ( int line
= lineStart
; line
< lineEnd
; line
++ )
6722 int cellLines
= oper
.Select(
6723 GetCellSize(oper
.MakeCoords(m_dragRowOrCol
, line
)));
6724 if ( cellLines
< subtractLines
)
6725 subtractLines
= cellLines
;
6730 oper
.GetLineStartPos(this, m_dragRowOrCol
+ subtractLines
);
6731 startPos
= doper
.CalcScrolledPosition(this, startPos
);
6733 doper
.Select(rect
) = startPos
;
6734 doper
.SelectSize(rect
) = doper
.Select(size
) - startPos
;
6736 m_gridWin
->Refresh(false, &rect
);
6740 // show the edit control back again
6741 ShowCellEditControl();
6744 void wxGrid::DoEndDragResizeRow()
6746 DoEndDragResizeLine(wxGridRowOperations());
6749 void wxGrid::DoEndDragResizeCol(wxMouseEvent
*event
)
6751 DoEndDragResizeLine(wxGridColumnOperations());
6753 // Note: we are ending the event *after* doing
6754 // default processing in this case
6757 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, *event
);
6759 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
);
6762 void wxGrid::DoEndDragMoveCol()
6764 //The user clicked on the column but didn't actually drag
6765 if ( m_dragLastPos
< 0 )
6767 m_colWindow
->Refresh(); //Do this to "unpress" the column
6772 if ( m_moveToCol
== -1 )
6773 newPos
= m_numCols
- 1;
6776 newPos
= GetColPos( m_moveToCol
);
6777 if ( newPos
> GetColPos( m_dragRowOrCol
) )
6781 SetColPos( m_dragRowOrCol
, newPos
);
6784 void wxGrid::SetColPos( int colID
, int newPos
)
6786 if ( m_colAt
.IsEmpty() )
6788 m_colAt
.Alloc( m_numCols
);
6791 for ( i
= 0; i
< m_numCols
; i
++ )
6797 int oldPos
= GetColPos( colID
);
6799 //Reshuffle the m_colAt array
6800 if ( newPos
> oldPos
)
6803 for ( i
= oldPos
; i
< newPos
; i
++ )
6805 m_colAt
[i
] = m_colAt
[i
+1];
6811 for ( i
= oldPos
; i
> newPos
; i
-- )
6813 m_colAt
[i
] = m_colAt
[i
-1];
6817 m_colAt
[newPos
] = colID
;
6819 //Recalculate the column rights
6820 if ( !m_colWidths
.IsEmpty() )
6824 for ( colPos
= 0; colPos
< m_numCols
; colPos
++ )
6826 int colID
= GetColAt( colPos
);
6828 colRight
+= m_colWidths
[colID
];
6829 m_colRights
[colID
] = colRight
;
6833 m_colWindow
->Refresh();
6834 m_gridWin
->Refresh();
6839 void wxGrid::EnableDragColMove( bool enable
)
6841 if ( m_canDragColMove
== enable
)
6844 m_canDragColMove
= enable
;
6846 if ( !m_canDragColMove
)
6850 //Recalculate the column rights
6851 if ( !m_colWidths
.IsEmpty() )
6855 for ( colPos
= 0; colPos
< m_numCols
; colPos
++ )
6857 colRight
+= m_colWidths
[colPos
];
6858 m_colRights
[colPos
] = colRight
;
6862 m_colWindow
->Refresh();
6863 m_gridWin
->Refresh();
6869 // ------ interaction with data model
6871 bool wxGrid::ProcessTableMessage( wxGridTableMessage
& msg
)
6873 switch ( msg
.GetId() )
6875 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES
:
6876 return GetModelValues();
6878 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES
:
6879 return SetModelValues();
6881 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
6882 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
6883 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
6884 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
6885 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
6886 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
6887 return Redimension( msg
);
6894 // The behaviour of this function depends on the grid table class
6895 // Clear() function. For the default wxGridStringTable class the
6896 // behaviour is to replace all cell contents with wxEmptyString but
6897 // not to change the number of rows or cols.
6899 void wxGrid::ClearGrid()
6903 if (IsCellEditControlEnabled())
6904 DisableCellEditControl();
6907 if (!GetBatchCount())
6908 m_gridWin
->Refresh();
6913 wxGrid::DoModifyLines(bool (wxGridTableBase::*funcModify
)(size_t, size_t),
6914 int pos
, int num
, bool WXUNUSED(updateLabels
) )
6916 wxCHECK_MSG( m_created
, false, "must finish creating the grid first" );
6921 if ( IsCellEditControlEnabled() )
6922 DisableCellEditControl();
6924 return (m_table
->*funcModify
)(pos
, num
);
6926 // the table will have sent the results of the insert row
6927 // operation to this view object as a grid table message
6931 wxGrid::DoAppendLines(bool (wxGridTableBase::*funcAppend
)(size_t),
6932 int num
, bool WXUNUSED(updateLabels
))
6934 wxCHECK_MSG( m_created
, false, "must finish creating the grid first" );
6939 return (m_table
->*funcAppend
)(num
);
6943 // ----- event handlers
6946 // Generate a grid event based on a mouse event and return:
6947 // -1 if the event was vetoed
6948 // +1 if the event was processed (but not vetoed)
6949 // 0 if the event wasn't handled
6951 wxGrid::SendEvent(const wxEventType type
,
6953 wxMouseEvent
& mouseEv
)
6955 bool claimed
, vetoed
;
6957 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
6959 int rowOrCol
= (row
== -1 ? col
: row
);
6961 wxGridSizeEvent
gridEvt( GetId(),
6965 mouseEv
.GetX() + GetRowLabelSize(),
6966 mouseEv
.GetY() + GetColLabelSize(),
6969 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6970 vetoed
= !gridEvt
.IsAllowed();
6972 else if ( type
== wxEVT_GRID_RANGE_SELECT
)
6974 // Right now, it should _never_ end up here!
6975 wxGridRangeSelectEvent
gridEvt( GetId(),
6978 m_selectedBlockTopLeft
,
6979 m_selectedBlockBottomRight
,
6983 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6984 vetoed
= !gridEvt
.IsAllowed();
6986 else if ( type
== wxEVT_GRID_LABEL_LEFT_CLICK
||
6987 type
== wxEVT_GRID_LABEL_LEFT_DCLICK
||
6988 type
== wxEVT_GRID_LABEL_RIGHT_CLICK
||
6989 type
== wxEVT_GRID_LABEL_RIGHT_DCLICK
)
6991 wxPoint pos
= mouseEv
.GetPosition();
6993 if ( mouseEv
.GetEventObject() == GetGridRowLabelWindow() )
6994 pos
.y
+= GetColLabelSize();
6995 if ( mouseEv
.GetEventObject() == GetGridColLabelWindow() )
6996 pos
.x
+= GetRowLabelSize();
6998 wxGridEvent
gridEvt( GetId(),
7006 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
7007 vetoed
= !gridEvt
.IsAllowed();
7011 wxGridEvent
gridEvt( GetId(),
7015 mouseEv
.GetX() + GetRowLabelSize(),
7016 mouseEv
.GetY() + GetColLabelSize(),
7019 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
7020 vetoed
= !gridEvt
.IsAllowed();
7023 // A Veto'd event may not be `claimed' so test this first
7027 return claimed
? 1 : 0;
7030 // Generate a grid event of specified type, return value same as above
7032 int wxGrid::SendEvent(const wxEventType type
, int row
, int col
)
7034 bool claimed
, vetoed
;
7036 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
7038 int rowOrCol
= (row
== -1 ? col
: row
);
7040 wxGridSizeEvent
gridEvt( GetId(), type
, this, rowOrCol
);
7042 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
7043 vetoed
= !gridEvt
.IsAllowed();
7047 wxGridEvent
gridEvt( GetId(), type
, this, row
, col
);
7049 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
7050 vetoed
= !gridEvt
.IsAllowed();
7053 // A Veto'd event may not be `claimed' so test this first
7057 return claimed
? 1 : 0;
7060 void wxGrid::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
7062 // needed to prevent zillions of paint events on MSW
7066 void wxGrid::Refresh(bool eraseb
, const wxRect
* rect
)
7068 // Don't do anything if between Begin/EndBatch...
7069 // EndBatch() will do all this on the last nested one anyway.
7070 if ( m_created
&& !GetBatchCount() )
7072 // Refresh to get correct scrolled position:
7073 wxScrolledWindow::Refresh(eraseb
, rect
);
7077 int rect_x
, rect_y
, rectWidth
, rectHeight
;
7078 int width_label
, width_cell
, height_label
, height_cell
;
7081 // Copy rectangle can get scroll offsets..
7082 rect_x
= rect
->GetX();
7083 rect_y
= rect
->GetY();
7084 rectWidth
= rect
->GetWidth();
7085 rectHeight
= rect
->GetHeight();
7087 width_label
= m_rowLabelWidth
- rect_x
;
7088 if (width_label
> rectWidth
)
7089 width_label
= rectWidth
;
7091 height_label
= m_colLabelHeight
- rect_y
;
7092 if (height_label
> rectHeight
)
7093 height_label
= rectHeight
;
7095 if (rect_x
> m_rowLabelWidth
)
7097 x
= rect_x
- m_rowLabelWidth
;
7098 width_cell
= rectWidth
;
7103 width_cell
= rectWidth
- (m_rowLabelWidth
- rect_x
);
7106 if (rect_y
> m_colLabelHeight
)
7108 y
= rect_y
- m_colLabelHeight
;
7109 height_cell
= rectHeight
;
7114 height_cell
= rectHeight
- (m_colLabelHeight
- rect_y
);
7117 // Paint corner label part intersecting rect.
7118 if ( width_label
> 0 && height_label
> 0 )
7120 wxRect
anotherrect(rect_x
, rect_y
, width_label
, height_label
);
7121 m_cornerLabelWin
->Refresh(eraseb
, &anotherrect
);
7124 // Paint col labels part intersecting rect.
7125 if ( width_cell
> 0 && height_label
> 0 )
7127 wxRect
anotherrect(x
, rect_y
, width_cell
, height_label
);
7128 m_colWindow
->Refresh(eraseb
, &anotherrect
);
7131 // Paint row labels part intersecting rect.
7132 if ( width_label
> 0 && height_cell
> 0 )
7134 wxRect
anotherrect(rect_x
, y
, width_label
, height_cell
);
7135 m_rowLabelWin
->Refresh(eraseb
, &anotherrect
);
7138 // Paint cell area part intersecting rect.
7139 if ( width_cell
> 0 && height_cell
> 0 )
7141 wxRect
anotherrect(x
, y
, width_cell
, height_cell
);
7142 m_gridWin
->Refresh(eraseb
, &anotherrect
);
7147 m_cornerLabelWin
->Refresh(eraseb
, NULL
);
7148 m_colWindow
->Refresh(eraseb
, NULL
);
7149 m_rowLabelWin
->Refresh(eraseb
, NULL
);
7150 m_gridWin
->Refresh(eraseb
, NULL
);
7155 void wxGrid::OnSize(wxSizeEvent
& WXUNUSED(event
))
7157 if (m_targetWindow
!= this) // check whether initialisation has been done
7159 // reposition our children windows
7164 void wxGrid::OnKeyDown( wxKeyEvent
& event
)
7166 if ( m_inOnKeyDown
)
7168 // shouldn't be here - we are going round in circles...
7170 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") );
7173 m_inOnKeyDown
= true;
7175 // propagate the event up and see if it gets processed
7176 wxWindow
*parent
= GetParent();
7177 wxKeyEvent
keyEvt( event
);
7178 keyEvt
.SetEventObject( parent
);
7180 if ( !parent
->GetEventHandler()->ProcessEvent( keyEvt
) )
7182 if (GetLayoutDirection() == wxLayout_RightToLeft
)
7184 if (event
.GetKeyCode() == WXK_RIGHT
)
7185 event
.m_keyCode
= WXK_LEFT
;
7186 else if (event
.GetKeyCode() == WXK_LEFT
)
7187 event
.m_keyCode
= WXK_RIGHT
;
7190 // try local handlers
7191 switch ( event
.GetKeyCode() )
7194 if ( event
.ControlDown() )
7195 MoveCursorUpBlock( event
.ShiftDown() );
7197 MoveCursorUp( event
.ShiftDown() );
7201 if ( event
.ControlDown() )
7202 MoveCursorDownBlock( event
.ShiftDown() );
7204 MoveCursorDown( event
.ShiftDown() );
7208 if ( event
.ControlDown() )
7209 MoveCursorLeftBlock( event
.ShiftDown() );
7211 MoveCursorLeft( event
.ShiftDown() );
7215 if ( event
.ControlDown() )
7216 MoveCursorRightBlock( event
.ShiftDown() );
7218 MoveCursorRight( event
.ShiftDown() );
7222 case WXK_NUMPAD_ENTER
:
7223 if ( event
.ControlDown() )
7225 event
.Skip(); // to let the edit control have the return
7229 if ( GetGridCursorRow() < GetNumberRows()-1 )
7231 MoveCursorDown( event
.ShiftDown() );
7235 // at the bottom of a column
7236 DisableCellEditControl();
7246 if (event
.ShiftDown())
7248 if ( GetGridCursorCol() > 0 )
7250 MoveCursorLeft( false );
7255 DisableCellEditControl();
7260 if ( GetGridCursorCol() < GetNumberCols() - 1 )
7262 MoveCursorRight( false );
7267 DisableCellEditControl();
7273 if ( event
.ControlDown() )
7284 if ( event
.ControlDown() )
7286 GoToCell(m_numRows
- 1, m_numCols
- 1);
7303 // Ctrl-Space selects the current column, Shift-Space -- the
7304 // current row and Ctrl-Shift-Space -- everything
7305 switch ( m_selection
? event
.GetModifiers() : wxMOD_NONE
)
7308 m_selection
->SelectCol(m_currentCellCoords
.GetCol());
7312 m_selection
->SelectRow(m_currentCellCoords
.GetRow());
7315 case wxMOD_CONTROL
| wxMOD_SHIFT
:
7316 m_selection
->SelectBlock(0, 0,
7317 m_numRows
- 1, m_numCols
- 1);
7321 if ( !IsEditable() )
7323 MoveCursorRight(false);
7326 //else: fall through
7339 m_inOnKeyDown
= false;
7342 void wxGrid::OnKeyUp( wxKeyEvent
& event
)
7344 // try local handlers
7346 if ( event
.GetKeyCode() == WXK_SHIFT
)
7348 if ( m_selectedBlockTopLeft
!= wxGridNoCellCoords
&&
7349 m_selectedBlockBottomRight
!= wxGridNoCellCoords
)
7353 m_selection
->SelectBlock(
7354 m_selectedBlockTopLeft
,
7355 m_selectedBlockBottomRight
,
7360 m_selectedBlockTopLeft
= wxGridNoCellCoords
;
7361 m_selectedBlockBottomRight
= wxGridNoCellCoords
;
7362 m_selectedBlockCorner
= wxGridNoCellCoords
;
7366 void wxGrid::OnChar( wxKeyEvent
& event
)
7368 // is it possible to edit the current cell at all?
7369 if ( !IsCellEditControlEnabled() && CanEnableCellControl() )
7371 // yes, now check whether the cells editor accepts the key
7372 int row
= m_currentCellCoords
.GetRow();
7373 int col
= m_currentCellCoords
.GetCol();
7374 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7375 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
7377 // <F2> is special and will always start editing, for
7378 // other keys - ask the editor itself
7379 if ( (event
.GetKeyCode() == WXK_F2
&& !event
.HasModifiers())
7380 || editor
->IsAcceptedKey(event
) )
7382 // ensure cell is visble
7383 MakeCellVisible(row
, col
);
7384 EnableCellEditControl();
7386 // a problem can arise if the cell is not completely
7387 // visible (even after calling MakeCellVisible the
7388 // control is not created and calling StartingKey will
7390 if ( event
.GetKeyCode() != WXK_F2
&& editor
->IsCreated() && m_cellEditCtrlEnabled
)
7391 editor
->StartingKey(event
);
7407 void wxGrid::OnEraseBackground(wxEraseEvent
&)
7411 bool wxGrid::SetCurrentCell( const wxGridCellCoords
& coords
)
7413 if ( SendEvent(wxEVT_GRID_SELECT_CELL
, coords
) == -1 )
7415 // the event has been vetoed - do nothing
7419 #if !defined(__WXMAC__)
7420 wxClientDC
dc( m_gridWin
);
7424 if ( m_currentCellCoords
!= wxGridNoCellCoords
)
7426 DisableCellEditControl();
7428 if ( IsVisible( m_currentCellCoords
, false ) )
7431 r
= BlockToDeviceRect( m_currentCellCoords
, m_currentCellCoords
);
7432 if ( !m_gridLinesEnabled
)
7440 wxGridCellCoordsArray cells
= CalcCellsExposed( r
);
7442 // Otherwise refresh redraws the highlight!
7443 m_currentCellCoords
= coords
;
7445 #if defined(__WXMAC__)
7446 m_gridWin
->Refresh(true /*, & r */);
7448 DrawGridCellArea( dc
, cells
);
7449 DrawAllGridLines( dc
, r
);
7454 m_currentCellCoords
= coords
;
7456 wxGridCellAttr
*attr
= GetCellAttr( coords
);
7457 #if !defined(__WXMAC__)
7458 DrawCellHighlight( dc
, attr
);
7466 wxGrid::UpdateBlockBeingSelected(int topRow
, int leftCol
,
7467 int bottomRow
, int rightCol
)
7471 switch ( m_selection
->GetSelectionMode() )
7474 wxFAIL_MSG( "unknown selection mode" );
7477 case wxGridSelectCells
:
7478 // arbitrary blocks selection allowed so just use the cell
7479 // coordinates as is
7482 case wxGridSelectRows
:
7483 // only full rows selection allowd, ensure that we do select
7486 rightCol
= GetNumberCols() - 1;
7489 case wxGridSelectColumns
:
7490 // same as above but for columns
7492 bottomRow
= GetNumberRows() - 1;
7495 case wxGridSelectRowsOrColumns
:
7496 // in this mode we can select only full rows or full columns so
7497 // it doesn't make sense to select blocks at all (and we can't
7498 // extend the block because there is no preferred direction, we
7499 // could only extend it to cover the entire grid but this is
7505 m_selectedBlockCorner
= wxGridCellCoords(bottomRow
, rightCol
);
7506 MakeCellVisible(m_selectedBlockCorner
);
7508 EnsureFirstLessThanSecond(topRow
, bottomRow
);
7509 EnsureFirstLessThanSecond(leftCol
, rightCol
);
7511 wxGridCellCoords updateTopLeft
= wxGridCellCoords(topRow
, leftCol
),
7512 updateBottomRight
= wxGridCellCoords(bottomRow
, rightCol
);
7514 // First the case that we selected a completely new area
7515 if ( m_selectedBlockTopLeft
== wxGridNoCellCoords
||
7516 m_selectedBlockBottomRight
== wxGridNoCellCoords
)
7519 rect
= BlockToDeviceRect( wxGridCellCoords ( topRow
, leftCol
),
7520 wxGridCellCoords ( bottomRow
, rightCol
) );
7521 m_gridWin
->Refresh( false, &rect
);
7524 // Now handle changing an existing selection area.
7525 else if ( m_selectedBlockTopLeft
!= updateTopLeft
||
7526 m_selectedBlockBottomRight
!= updateBottomRight
)
7528 // Compute two optimal update rectangles:
7529 // Either one rectangle is a real subset of the
7530 // other, or they are (almost) disjoint!
7532 bool need_refresh
[4];
7536 need_refresh
[3] = false;
7539 // Store intermediate values
7540 wxCoord oldLeft
= m_selectedBlockTopLeft
.GetCol();
7541 wxCoord oldTop
= m_selectedBlockTopLeft
.GetRow();
7542 wxCoord oldRight
= m_selectedBlockBottomRight
.GetCol();
7543 wxCoord oldBottom
= m_selectedBlockBottomRight
.GetRow();
7545 // Determine the outer/inner coordinates.
7546 EnsureFirstLessThanSecond(oldLeft
, leftCol
);
7547 EnsureFirstLessThanSecond(oldTop
, topRow
);
7548 EnsureFirstLessThanSecond(rightCol
, oldRight
);
7549 EnsureFirstLessThanSecond(bottomRow
, oldBottom
);
7551 // Now, either the stuff marked old is the outer
7552 // rectangle or we don't have a situation where one
7553 // is contained in the other.
7555 if ( oldLeft
< leftCol
)
7557 // Refresh the newly selected or deselected
7558 // area to the left of the old or new selection.
7559 need_refresh
[0] = true;
7560 rect
[0] = BlockToDeviceRect(
7561 wxGridCellCoords( oldTop
, oldLeft
),
7562 wxGridCellCoords( oldBottom
, leftCol
- 1 ) );
7565 if ( oldTop
< topRow
)
7567 // Refresh the newly selected or deselected
7568 // area above the old or new selection.
7569 need_refresh
[1] = true;
7570 rect
[1] = BlockToDeviceRect(
7571 wxGridCellCoords( oldTop
, leftCol
),
7572 wxGridCellCoords( topRow
- 1, rightCol
) );
7575 if ( oldRight
> rightCol
)
7577 // Refresh the newly selected or deselected
7578 // area to the right of the old or new selection.
7579 need_refresh
[2] = true;
7580 rect
[2] = BlockToDeviceRect(
7581 wxGridCellCoords( oldTop
, rightCol
+ 1 ),
7582 wxGridCellCoords( oldBottom
, oldRight
) );
7585 if ( oldBottom
> bottomRow
)
7587 // Refresh the newly selected or deselected
7588 // area below the old or new selection.
7589 need_refresh
[3] = true;
7590 rect
[3] = BlockToDeviceRect(
7591 wxGridCellCoords( bottomRow
+ 1, leftCol
),
7592 wxGridCellCoords( oldBottom
, rightCol
) );
7595 // various Refresh() calls
7596 for (i
= 0; i
< 4; i
++ )
7597 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
7598 m_gridWin
->Refresh( false, &(rect
[i
]) );
7602 m_selectedBlockTopLeft
= updateTopLeft
;
7603 m_selectedBlockBottomRight
= updateBottomRight
;
7607 // ------ functions to get/send data (see also public functions)
7610 bool wxGrid::GetModelValues()
7612 // Hide the editor, so it won't hide a changed value.
7613 HideCellEditControl();
7617 // all we need to do is repaint the grid
7619 m_gridWin
->Refresh();
7626 bool wxGrid::SetModelValues()
7630 // Disable the editor, so it won't hide a changed value.
7631 // Do we also want to save the current value of the editor first?
7633 DisableCellEditControl();
7637 for ( row
= 0; row
< m_numRows
; row
++ )
7639 for ( col
= 0; col
< m_numCols
; col
++ )
7641 m_table
->SetValue( row
, col
, GetCellValue(row
, col
) );
7651 // Note - this function only draws cells that are in the list of
7652 // exposed cells (usually set from the update region by
7653 // CalcExposedCells)
7655 void wxGrid::DrawGridCellArea( wxDC
& dc
, const wxGridCellCoordsArray
& cells
)
7657 if ( !m_numRows
|| !m_numCols
)
7660 int i
, numCells
= cells
.GetCount();
7661 int row
, col
, cell_rows
, cell_cols
;
7662 wxGridCellCoordsArray redrawCells
;
7664 for ( i
= numCells
- 1; i
>= 0; i
-- )
7666 row
= cells
[i
].GetRow();
7667 col
= cells
[i
].GetCol();
7668 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7670 // If this cell is part of a multicell block, find owner for repaint
7671 if ( cell_rows
<= 0 || cell_cols
<= 0 )
7673 wxGridCellCoords
cell( row
+ cell_rows
, col
+ cell_cols
);
7674 bool marked
= false;
7675 for ( int j
= 0; j
< numCells
; j
++ )
7677 if ( cell
== cells
[j
] )
7686 int count
= redrawCells
.GetCount();
7687 for (int j
= 0; j
< count
; j
++)
7689 if ( cell
== redrawCells
[j
] )
7697 redrawCells
.Add( cell
);
7700 // don't bother drawing this cell
7704 // If this cell is empty, find cell to left that might want to overflow
7705 if (m_table
&& m_table
->IsEmptyCell(row
, col
))
7707 for ( int l
= 0; l
< cell_rows
; l
++ )
7709 // find a cell in this row to leave already marked for repaint
7711 for (int k
= 0; k
< int(redrawCells
.GetCount()); k
++)
7712 if ((redrawCells
[k
].GetCol() < left
) &&
7713 (redrawCells
[k
].GetRow() == row
))
7715 left
= redrawCells
[k
].GetCol();
7719 left
= 0; // oh well
7721 for (int j
= col
- 1; j
>= left
; j
--)
7723 if (!m_table
->IsEmptyCell(row
+ l
, j
))
7725 if (GetCellOverflow(row
+ l
, j
))
7727 wxGridCellCoords
cell(row
+ l
, j
);
7728 bool marked
= false;
7730 for (int k
= 0; k
< numCells
; k
++)
7732 if ( cell
== cells
[k
] )
7741 int count
= redrawCells
.GetCount();
7742 for (int k
= 0; k
< count
; k
++)
7744 if ( cell
== redrawCells
[k
] )
7751 redrawCells
.Add( cell
);
7760 DrawCell( dc
, cells
[i
] );
7763 numCells
= redrawCells
.GetCount();
7765 for ( i
= numCells
- 1; i
>= 0; i
-- )
7767 DrawCell( dc
, redrawCells
[i
] );
7771 void wxGrid::DrawGridSpace( wxDC
& dc
)
7774 m_gridWin
->GetClientSize( &cw
, &ch
);
7777 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
7779 int rightCol
= m_numCols
> 0 ? GetColRight(GetColAt( m_numCols
- 1 )) : 0;
7780 int bottomRow
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) : 0;
7782 if ( right
> rightCol
|| bottom
> bottomRow
)
7785 CalcUnscrolledPosition( 0, 0, &left
, &top
);
7787 dc
.SetBrush(GetDefaultCellBackgroundColour());
7788 dc
.SetPen( *wxTRANSPARENT_PEN
);
7790 if ( right
> rightCol
)
7792 dc
.DrawRectangle( rightCol
, top
, right
- rightCol
, ch
);
7795 if ( bottom
> bottomRow
)
7797 dc
.DrawRectangle( left
, bottomRow
, cw
, bottom
- bottomRow
);
7802 void wxGrid::DrawCell( wxDC
& dc
, const wxGridCellCoords
& coords
)
7804 int row
= coords
.GetRow();
7805 int col
= coords
.GetCol();
7807 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7810 // we draw the cell border ourselves
7811 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7813 bool isCurrent
= coords
== m_currentCellCoords
;
7815 wxRect rect
= CellToRect( row
, col
);
7817 // if the editor is shown, we should use it and not the renderer
7818 // Note: However, only if it is really _shown_, i.e. not hidden!
7819 if ( isCurrent
&& IsCellEditControlShown() )
7821 // NB: this "#if..." is temporary and fixes a problem where the
7822 // edit control is erased by this code after being rendered.
7823 // On wxMac (QD build only), the cell editor is a wxTextCntl and is rendered
7824 // implicitly, causing this out-of order render.
7825 #if !defined(__WXMAC__)
7826 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
7827 editor
->PaintBackground(rect
, attr
);
7833 // but all the rest is drawn by the cell renderer and hence may be customized
7834 wxGridCellRenderer
*renderer
= attr
->GetRenderer(this, row
, col
);
7835 renderer
->Draw(*this, *attr
, dc
, rect
, row
, col
, IsInSelection(coords
));
7842 void wxGrid::DrawCellHighlight( wxDC
& dc
, const wxGridCellAttr
*attr
)
7844 // don't show highlight when the grid doesn't have focus
7848 int row
= m_currentCellCoords
.GetRow();
7849 int col
= m_currentCellCoords
.GetCol();
7851 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7854 wxRect rect
= CellToRect(row
, col
);
7856 // hmmm... what could we do here to show that the cell is disabled?
7857 // for now, I just draw a thinner border than for the other ones, but
7858 // it doesn't look really good
7860 int penWidth
= attr
->IsReadOnly() ? m_cellHighlightROPenWidth
: m_cellHighlightPenWidth
;
7864 // The center of the drawn line is where the position/width/height of
7865 // the rectangle is actually at (on wxMSW at least), so the
7866 // size of the rectangle is reduced to compensate for the thickness of
7867 // the line. If this is too strange on non-wxMSW platforms then
7868 // please #ifdef this appropriately.
7869 rect
.x
+= penWidth
/ 2;
7870 rect
.y
+= penWidth
/ 2;
7871 rect
.width
-= penWidth
- 1;
7872 rect
.height
-= penWidth
- 1;
7874 // Now draw the rectangle
7875 // use the cellHighlightColour if the cell is inside a selection, this
7876 // will ensure the cell is always visible.
7877 dc
.SetPen(wxPen(IsInSelection(row
,col
) ? m_selectionForeground
7878 : m_cellHighlightColour
,
7880 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
7881 dc
.DrawRectangle(rect
);
7885 wxPen
wxGrid::GetDefaultGridLinePen()
7887 return wxPen(GetGridLineColour());
7890 wxPen
wxGrid::GetRowGridLinePen(int WXUNUSED(row
))
7892 return GetDefaultGridLinePen();
7895 wxPen
wxGrid::GetColGridLinePen(int WXUNUSED(col
))
7897 return GetDefaultGridLinePen();
7900 void wxGrid::DrawCellBorder( wxDC
& dc
, const wxGridCellCoords
& coords
)
7902 int row
= coords
.GetRow();
7903 int col
= coords
.GetCol();
7904 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7908 wxRect rect
= CellToRect( row
, col
);
7910 // right hand border
7911 dc
.SetPen( GetColGridLinePen(col
) );
7912 dc
.DrawLine( rect
.x
+ rect
.width
, rect
.y
,
7913 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
+ 1 );
7916 dc
.SetPen( GetRowGridLinePen(row
) );
7917 dc
.DrawLine( rect
.x
, rect
.y
+ rect
.height
,
7918 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
);
7921 void wxGrid::DrawHighlight(wxDC
& dc
, const wxGridCellCoordsArray
& cells
)
7923 // This if block was previously in wxGrid::OnPaint but that doesn't
7924 // seem to get called under wxGTK - MB
7926 if ( m_currentCellCoords
== wxGridNoCellCoords
&&
7927 m_numRows
&& m_numCols
)
7929 m_currentCellCoords
.Set(0, 0);
7932 if ( IsCellEditControlShown() )
7934 // don't show highlight when the edit control is shown
7938 // if the active cell was repainted, repaint its highlight too because it
7939 // might have been damaged by the grid lines
7940 size_t count
= cells
.GetCount();
7941 for ( size_t n
= 0; n
< count
; n
++ )
7943 wxGridCellCoords cell
= cells
[n
];
7945 // If we are using attributes, then we may have just exposed another
7946 // cell in a partially-visible merged cluster of cells. If the "anchor"
7947 // (upper left) cell of this merged cluster is the cell indicated by
7948 // m_currentCellCoords, then we need to refresh the cell highlight even
7949 // though the "anchor" itself is not part of our update segment.
7950 if ( CanHaveAttributes() )
7954 GetCellSize(cell
.GetRow(), cell
.GetCol(), &rows
, &cols
);
7957 cell
.SetRow(cell
.GetRow() + rows
);
7960 cell
.SetCol(cell
.GetCol() + cols
);
7963 if ( cell
== m_currentCellCoords
)
7965 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
7966 DrawCellHighlight(dc
, attr
);
7974 // This is used to redraw all grid lines e.g. when the grid line colour
7977 void wxGrid::DrawAllGridLines( wxDC
& dc
, const wxRegion
& WXUNUSED(reg
) )
7979 if ( !m_gridLinesEnabled
)
7982 int top
, bottom
, left
, right
;
7985 m_gridWin
->GetClientSize(&cw
, &ch
);
7986 CalcUnscrolledPosition( 0, 0, &left
, &top
);
7987 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
7989 // avoid drawing grid lines past the last row and col
7990 if ( m_gridLinesClipHorz
)
7995 const int lastColRight
= GetColRight(GetColAt(m_numCols
- 1));
7996 if ( right
> lastColRight
)
7997 right
= lastColRight
;
8000 if ( m_gridLinesClipVert
)
8005 const int lastRowBottom
= GetRowBottom(m_numRows
- 1);
8006 if ( bottom
> lastRowBottom
)
8007 bottom
= lastRowBottom
;
8010 // no gridlines inside multicells, clip them out
8011 int leftCol
= GetColPos( internalXToCol(left
) );
8012 int topRow
= internalYToRow(top
);
8013 int rightCol
= GetColPos( internalXToCol(right
) );
8014 int bottomRow
= internalYToRow(bottom
);
8016 wxRegion
clippedcells(0, 0, cw
, ch
);
8018 int cell_rows
, cell_cols
;
8021 for ( int j
= topRow
; j
<= bottomRow
; j
++ )
8023 for ( int colPos
= leftCol
; colPos
<= rightCol
; colPos
++ )
8025 int i
= GetColAt( colPos
);
8027 GetCellSize( j
, i
, &cell_rows
, &cell_cols
);
8028 if ((cell_rows
> 1) || (cell_cols
> 1))
8030 rect
= CellToRect(j
,i
);
8031 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
8032 clippedcells
.Subtract(rect
);
8034 else if ((cell_rows
< 0) || (cell_cols
< 0))
8036 rect
= CellToRect(j
+ cell_rows
, i
+ cell_cols
);
8037 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
8038 clippedcells
.Subtract(rect
);
8043 dc
.SetDeviceClippingRegion( clippedcells
);
8046 // horizontal grid lines
8047 for ( int i
= internalYToRow(top
); i
< m_numRows
; i
++ )
8049 int bot
= GetRowBottom(i
) - 1;
8056 dc
.SetPen( GetRowGridLinePen(i
) );
8057 dc
.DrawLine( left
, bot
, right
, bot
);
8061 // vertical grid lines
8062 for ( int colPos
= leftCol
; colPos
< m_numCols
; colPos
++ )
8064 int i
= GetColAt( colPos
);
8066 int colRight
= GetColRight(i
);
8068 if (GetLayoutDirection() != wxLayout_RightToLeft
)
8072 if ( colRight
> right
)
8075 if ( colRight
>= left
)
8077 dc
.SetPen( GetColGridLinePen(i
) );
8078 dc
.DrawLine( colRight
, top
, colRight
, bottom
);
8082 dc
.DestroyClippingRegion();
8085 void wxGrid::DrawRowLabels( wxDC
& dc
, const wxArrayInt
& rows
)
8090 const size_t numLabels
= rows
.GetCount();
8091 for ( size_t i
= 0; i
< numLabels
; i
++ )
8093 DrawRowLabel( dc
, rows
[i
] );
8097 void wxGrid::DrawRowLabel( wxDC
& dc
, int row
)
8099 if ( GetRowHeight(row
) <= 0 || m_rowLabelWidth
<= 0 )
8104 int rowTop
= GetRowTop(row
),
8105 rowBottom
= GetRowBottom(row
) - 1;
8107 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW
)));
8108 dc
.DrawLine( m_rowLabelWidth
- 1, rowTop
, m_rowLabelWidth
- 1, rowBottom
);
8109 dc
.DrawLine( 0, rowTop
, 0, rowBottom
);
8110 dc
.DrawLine( 0, rowBottom
, m_rowLabelWidth
, rowBottom
);
8112 dc
.SetPen( *wxWHITE_PEN
);
8113 dc
.DrawLine( 1, rowTop
, 1, rowBottom
);
8114 dc
.DrawLine( 1, rowTop
, m_rowLabelWidth
- 1, rowTop
);
8116 dc
.SetBackgroundMode( wxBRUSHSTYLE_TRANSPARENT
);
8117 dc
.SetTextForeground( GetLabelTextColour() );
8118 dc
.SetFont( GetLabelFont() );
8121 GetRowLabelAlignment( &hAlign
, &vAlign
);
8124 rect
.SetY( GetRowTop(row
) + 2 );
8125 rect
.SetWidth( m_rowLabelWidth
- 4 );
8126 rect
.SetHeight( GetRowHeight(row
) - 4 );
8127 DrawTextRectangle( dc
, GetRowLabelValue( row
), rect
, hAlign
, vAlign
);
8130 void wxGrid::UseNativeColHeader(bool native
)
8132 if ( native
== m_useNativeHeader
)
8136 m_useNativeHeader
= native
;
8138 CreateColumnWindow();
8140 if ( m_useNativeHeader
)
8141 GetColHeader()->SetColumnCount(m_numCols
);
8145 void wxGrid::SetUseNativeColLabels( bool native
)
8147 wxASSERT_MSG( !m_useNativeHeader
,
8148 "doesn't make sense when using native header" );
8150 m_nativeColumnLabels
= native
;
8153 int height
= wxRendererNative::Get().GetHeaderButtonHeight( this );
8154 SetColLabelSize( height
);
8157 GetColLabelWindow()->Refresh();
8158 m_cornerLabelWin
->Refresh();
8161 void wxGrid::DrawColLabels( wxDC
& dc
,const wxArrayInt
& cols
)
8166 const size_t numLabels
= cols
.GetCount();
8167 for ( size_t i
= 0; i
< numLabels
; i
++ )
8169 DrawColLabel( dc
, cols
[i
] );
8173 void wxGrid::DrawCornerLabel(wxDC
& dc
)
8175 if ( m_nativeColumnLabels
)
8177 wxRect
rect(wxSize(m_rowLabelWidth
, m_colLabelHeight
));
8180 wxRendererNative::Get().DrawHeaderButton(m_cornerLabelWin
, dc
, rect
, 0);
8184 dc
.SetPen(wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW
)));
8185 dc
.DrawLine( m_rowLabelWidth
- 1, m_colLabelHeight
- 1,
8186 m_rowLabelWidth
- 1, 0 );
8187 dc
.DrawLine( m_rowLabelWidth
- 1, m_colLabelHeight
- 1,
8188 0, m_colLabelHeight
- 1 );
8189 dc
.DrawLine( 0, 0, m_rowLabelWidth
, 0 );
8190 dc
.DrawLine( 0, 0, 0, m_colLabelHeight
);
8192 dc
.SetPen( *wxWHITE_PEN
);
8193 dc
.DrawLine( 1, 1, m_rowLabelWidth
- 1, 1 );
8194 dc
.DrawLine( 1, 1, 1, m_colLabelHeight
- 1 );
8198 void wxGrid::DrawColLabel(wxDC
& dc
, int col
)
8200 if ( GetColWidth(col
) <= 0 || m_colLabelHeight
<= 0 )
8203 int colLeft
= GetColLeft(col
);
8205 wxRect
rect(colLeft
, 0, GetColWidth(col
), m_colLabelHeight
);
8207 if ( m_nativeColumnLabels
)
8209 wxRendererNative::Get().DrawHeaderButton(GetColLabelWindow(), dc
, rect
, 0);
8213 int colRight
= GetColRight(col
) - 1;
8215 dc
.SetPen(wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW
)));
8216 dc
.DrawLine( colRight
, 0,
8217 colRight
, m_colLabelHeight
- 1 );
8218 dc
.DrawLine( colLeft
, 0,
8220 dc
.DrawLine( colLeft
, m_colLabelHeight
- 1,
8221 colRight
+ 1, m_colLabelHeight
- 1 );
8223 dc
.SetPen( *wxWHITE_PEN
);
8224 dc
.DrawLine( colLeft
, 1, colLeft
, m_colLabelHeight
- 1 );
8225 dc
.DrawLine( colLeft
, 1, colRight
, 1 );
8228 dc
.SetBackgroundMode( wxBRUSHSTYLE_TRANSPARENT
);
8229 dc
.SetTextForeground( GetLabelTextColour() );
8230 dc
.SetFont( GetLabelFont() );
8233 GetColLabelAlignment( &hAlign
, &vAlign
);
8234 const int orient
= GetColLabelTextOrientation();
8237 DrawTextRectangle(dc
, GetColLabelValue(col
), rect
, hAlign
, vAlign
, orient
);
8240 // TODO: these 2 functions should be replaced with wxDC::DrawLabel() to which
8241 // we just have to add textOrientation support
8242 void wxGrid::DrawTextRectangle( wxDC
& dc
,
8243 const wxString
& value
,
8247 int textOrientation
)
8249 wxArrayString lines
;
8251 StringToLines( value
, lines
);
8253 DrawTextRectangle(dc
, lines
, rect
, horizAlign
, vertAlign
, textOrientation
);
8256 void wxGrid::DrawTextRectangle(wxDC
& dc
,
8257 const wxArrayString
& lines
,
8261 int textOrientation
)
8263 if ( lines
.empty() )
8266 wxDCClipper
clip(dc
, rect
);
8271 if ( textOrientation
== wxHORIZONTAL
)
8272 GetTextBoxSize( dc
, lines
, &textWidth
, &textHeight
);
8274 GetTextBoxSize( dc
, lines
, &textHeight
, &textWidth
);
8278 switch ( vertAlign
)
8280 case wxALIGN_BOTTOM
:
8281 if ( textOrientation
== wxHORIZONTAL
)
8282 y
= rect
.y
+ (rect
.height
- textHeight
- 1);
8284 x
= rect
.x
+ rect
.width
- textWidth
;
8287 case wxALIGN_CENTRE
:
8288 if ( textOrientation
== wxHORIZONTAL
)
8289 y
= rect
.y
+ ((rect
.height
- textHeight
) / 2);
8291 x
= rect
.x
+ ((rect
.width
- textWidth
) / 2);
8296 if ( textOrientation
== wxHORIZONTAL
)
8303 // Align each line of a multi-line label
8304 size_t nLines
= lines
.GetCount();
8305 for ( size_t l
= 0; l
< nLines
; l
++ )
8307 const wxString
& line
= lines
[l
];
8311 *(textOrientation
== wxHORIZONTAL
? &y
: &x
) += dc
.GetCharHeight();
8315 wxCoord lineWidth
= 0,
8317 dc
.GetTextExtent(line
, &lineWidth
, &lineHeight
);
8319 switch ( horizAlign
)
8322 if ( textOrientation
== wxHORIZONTAL
)
8323 x
= rect
.x
+ (rect
.width
- lineWidth
- 1);
8325 y
= rect
.y
+ lineWidth
+ 1;
8328 case wxALIGN_CENTRE
:
8329 if ( textOrientation
== wxHORIZONTAL
)
8330 x
= rect
.x
+ ((rect
.width
- lineWidth
) / 2);
8332 y
= rect
.y
+ rect
.height
- ((rect
.height
- lineWidth
) / 2);
8337 if ( textOrientation
== wxHORIZONTAL
)
8340 y
= rect
.y
+ rect
.height
- 1;
8344 if ( textOrientation
== wxHORIZONTAL
)
8346 dc
.DrawText( line
, x
, y
);
8351 dc
.DrawRotatedText( line
, x
, y
, 90.0 );
8357 // Split multi-line text up into an array of strings.
8358 // Any existing contents of the string array are preserved.
8360 // TODO: refactor wxTextFile::Read() and reuse the same code from here
8361 void wxGrid::StringToLines( const wxString
& value
, wxArrayString
& lines
) const
8365 wxString eol
= wxTextFile::GetEOL( wxTextFileType_Unix
);
8366 wxString tVal
= wxTextFile::Translate( value
, wxTextFileType_Unix
);
8368 while ( startPos
< (int)tVal
.length() )
8370 pos
= tVal
.Mid(startPos
).Find( eol
);
8375 else if ( pos
== 0 )
8377 lines
.Add( wxEmptyString
);
8381 lines
.Add( tVal
.Mid(startPos
, pos
) );
8384 startPos
+= pos
+ 1;
8387 if ( startPos
< (int)tVal
.length() )
8389 lines
.Add( tVal
.Mid( startPos
) );
8393 void wxGrid::GetTextBoxSize( const wxDC
& dc
,
8394 const wxArrayString
& lines
,
8395 long *width
, long *height
) const
8399 wxCoord lineW
= 0, lineH
= 0;
8402 for ( i
= 0; i
< lines
.GetCount(); i
++ )
8404 dc
.GetTextExtent( lines
[i
], &lineW
, &lineH
);
8405 w
= wxMax( w
, lineW
);
8414 // ------ Batch processing.
8416 void wxGrid::EndBatch()
8418 if ( m_batchCount
> 0 )
8421 if ( !m_batchCount
)
8424 m_rowLabelWin
->Refresh();
8425 m_colWindow
->Refresh();
8426 m_cornerLabelWin
->Refresh();
8427 m_gridWin
->Refresh();
8432 // Use this, rather than wxWindow::Refresh(), to force an immediate
8433 // repainting of the grid. Has no effect if you are already inside a
8434 // BeginBatch / EndBatch block.
8436 void wxGrid::ForceRefresh()
8442 bool wxGrid::Enable(bool enable
)
8444 if ( !wxScrolledWindow::Enable(enable
) )
8447 // redraw in the new state
8448 m_gridWin
->Refresh();
8454 // ------ Edit control functions
8457 void wxGrid::EnableEditing( bool edit
)
8459 if ( edit
!= m_editable
)
8462 EnableCellEditControl(edit
);
8467 void wxGrid::EnableCellEditControl( bool enable
)
8472 if ( enable
!= m_cellEditCtrlEnabled
)
8476 if ( SendEvent(wxEVT_GRID_EDITOR_SHOWN
) == -1 )
8479 // this should be checked by the caller!
8480 wxASSERT_MSG( CanEnableCellControl(), _T("can't enable editing for this cell!") );
8482 // do it before ShowCellEditControl()
8483 m_cellEditCtrlEnabled
= enable
;
8485 ShowCellEditControl();
8489 //FIXME:add veto support
8490 SendEvent(wxEVT_GRID_EDITOR_HIDDEN
);
8492 HideCellEditControl();
8493 SaveEditControlValue();
8495 // do it after HideCellEditControl()
8496 m_cellEditCtrlEnabled
= enable
;
8501 bool wxGrid::IsCurrentCellReadOnly() const
8504 wxGridCellAttr
* attr
= ((wxGrid
*)this)->GetCellAttr(m_currentCellCoords
);
8505 bool readonly
= attr
->IsReadOnly();
8511 bool wxGrid::CanEnableCellControl() const
8513 return m_editable
&& (m_currentCellCoords
!= wxGridNoCellCoords
) &&
8514 !IsCurrentCellReadOnly();
8517 bool wxGrid::IsCellEditControlEnabled() const
8519 // the cell edit control might be disable for all cells or just for the
8520 // current one if it's read only
8521 return m_cellEditCtrlEnabled
? !IsCurrentCellReadOnly() : false;
8524 bool wxGrid::IsCellEditControlShown() const
8526 bool isShown
= false;
8528 if ( m_cellEditCtrlEnabled
)
8530 int row
= m_currentCellCoords
.GetRow();
8531 int col
= m_currentCellCoords
.GetCol();
8532 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8533 wxGridCellEditor
* editor
= attr
->GetEditor((wxGrid
*) this, row
, col
);
8538 if ( editor
->IsCreated() )
8540 isShown
= editor
->GetControl()->IsShown();
8550 void wxGrid::ShowCellEditControl()
8552 if ( IsCellEditControlEnabled() )
8554 if ( !IsVisible( m_currentCellCoords
, false ) )
8556 m_cellEditCtrlEnabled
= false;
8561 wxRect rect
= CellToRect( m_currentCellCoords
);
8562 int row
= m_currentCellCoords
.GetRow();
8563 int col
= m_currentCellCoords
.GetCol();
8565 // if this is part of a multicell, find owner (topleft)
8566 int cell_rows
, cell_cols
;
8567 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
8568 if ( cell_rows
<= 0 || cell_cols
<= 0 )
8572 m_currentCellCoords
.SetRow( row
);
8573 m_currentCellCoords
.SetCol( col
);
8576 // erase the highlight and the cell contents because the editor
8577 // might not cover the entire cell
8578 wxClientDC
dc( m_gridWin
);
8580 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8581 dc
.SetBrush(wxBrush(attr
->GetBackgroundColour()));
8582 dc
.SetPen(*wxTRANSPARENT_PEN
);
8583 dc
.DrawRectangle(rect
);
8585 // convert to scrolled coords
8586 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
8592 // cell is shifted by one pixel
8593 // However, don't allow x or y to become negative
8594 // since the SetSize() method interprets that as
8601 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
8602 if ( !editor
->IsCreated() )
8604 editor
->Create(m_gridWin
, wxID_ANY
,
8605 new wxGridCellEditorEvtHandler(this, editor
));
8607 wxGridEditorCreatedEvent
evt(GetId(),
8608 wxEVT_GRID_EDITOR_CREATED
,
8612 editor
->GetControl());
8613 GetEventHandler()->ProcessEvent(evt
);
8616 // resize editor to overflow into righthand cells if allowed
8617 int maxWidth
= rect
.width
;
8618 wxString value
= GetCellValue(row
, col
);
8619 if ( (value
!= wxEmptyString
) && (attr
->GetOverflow()) )
8622 GetTextExtent(value
, &maxWidth
, &y
, NULL
, NULL
, &attr
->GetFont());
8623 if (maxWidth
< rect
.width
)
8624 maxWidth
= rect
.width
;
8627 int client_right
= m_gridWin
->GetClientSize().GetWidth();
8628 if (rect
.x
+ maxWidth
> client_right
)
8629 maxWidth
= client_right
- rect
.x
;
8631 if ((maxWidth
> rect
.width
) && (col
< m_numCols
) && m_table
)
8633 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
8634 // may have changed earlier
8635 for (int i
= col
+ cell_cols
; i
< m_numCols
; i
++)
8638 GetCellSize( row
, i
, &c_rows
, &c_cols
);
8640 // looks weird going over a multicell
8641 if (m_table
->IsEmptyCell( row
, i
) &&
8642 (rect
.width
< maxWidth
) && (c_rows
== 1))
8644 rect
.width
+= GetColWidth( i
);
8650 if (rect
.GetRight() > client_right
)
8651 rect
.SetRight( client_right
- 1 );
8654 editor
->SetCellAttr( attr
);
8655 editor
->SetSize( rect
);
8657 editor
->GetControl()->Move(
8658 editor
->GetControl()->GetPosition().x
+ nXMove
,
8659 editor
->GetControl()->GetPosition().y
);
8660 editor
->Show( true, attr
);
8662 // recalc dimensions in case we need to
8663 // expand the scrolled window to account for editor
8666 editor
->BeginEdit(row
, col
, this);
8667 editor
->SetCellAttr(NULL
);
8675 void wxGrid::HideCellEditControl()
8677 if ( IsCellEditControlEnabled() )
8679 int row
= m_currentCellCoords
.GetRow();
8680 int col
= m_currentCellCoords
.GetCol();
8682 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8683 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
8684 const bool editorHadFocus
= editor
->GetControl()->HasFocus();
8685 editor
->Show( false );
8689 // return the focus to the grid itself if the editor had it
8691 // note that we must not do this unconditionally to avoid stealing
8692 // focus from the window which just received it if we are hiding the
8693 // editor precisely because we lost focus
8694 if ( editorHadFocus
)
8695 m_gridWin
->SetFocus();
8697 // refresh whole row to the right
8698 wxRect
rect( CellToRect(row
, col
) );
8699 CalcScrolledPosition(rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
8700 rect
.width
= m_gridWin
->GetClientSize().GetWidth() - rect
.x
;
8703 // ensure that the pixels under the focus ring get refreshed as well
8704 rect
.Inflate(10, 10);
8707 m_gridWin
->Refresh( false, &rect
);
8711 void wxGrid::SaveEditControlValue()
8713 if ( IsCellEditControlEnabled() )
8715 int row
= m_currentCellCoords
.GetRow();
8716 int col
= m_currentCellCoords
.GetCol();
8718 wxString oldval
= GetCellValue(row
, col
);
8720 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8721 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
8722 bool changed
= editor
->EndEdit(row
, col
, this);
8729 if ( SendEvent(wxEVT_GRID_CELL_CHANGE
) == -1 )
8731 // Event has been vetoed, set the data back.
8732 SetCellValue(row
, col
, oldval
);
8739 // ------ Grid location functions
8740 // Note that all of these functions work with the logical coordinates of
8741 // grid cells and labels so you will need to convert from device
8742 // coordinates for mouse events etc.
8745 wxGridCellCoords
wxGrid::XYToCell(int x
, int y
) const
8747 int row
= YToRow(y
);
8748 int col
= XToCol(x
);
8750 return row
== -1 || col
== -1 ? wxGridNoCellCoords
8751 : wxGridCellCoords(row
, col
);
8754 // compute row or column from some (unscrolled) coordinate value, using either
8755 // m_defaultRowHeight/m_defaultColWidth or binary search on array of
8756 // m_rowBottoms/m_colRights to do it quickly (linear search shouldn't be used
8759 wxGrid::PosToLine(int coord
,
8761 const wxGridOperations
& oper
) const
8763 const int numLines
= oper
.GetNumberOfLines(this);
8766 return clipToMinMax
&& numLines
> 0 ? oper
.GetLineAt(this, 0) : -1;
8768 const int defaultLineSize
= oper
.GetDefaultLineSize(this);
8769 wxCHECK_MSG( defaultLineSize
, -1, "can't have 0 default line size" );
8771 int maxPos
= coord
/ defaultLineSize
,
8774 // check for the simplest case: if we have no explicit line sizes
8775 // configured, then we already know the line this position falls in
8776 const wxArrayInt
& lineEnds
= oper
.GetLineEnds(this);
8777 if ( lineEnds
.empty() )
8779 if ( maxPos
< numLines
)
8782 return clipToMinMax
? numLines
- 1 : -1;
8786 // adjust maxPos before starting the binary search
8787 if ( maxPos
>= numLines
)
8789 maxPos
= numLines
- 1;
8793 if ( coord
>= lineEnds
[oper
.GetLineAt(this, maxPos
)])
8796 const int minDist
= oper
.GetMinimalAcceptableLineSize(this);
8798 maxPos
= coord
/ minDist
;
8800 maxPos
= numLines
- 1;
8803 if ( maxPos
>= numLines
)
8804 maxPos
= numLines
- 1;
8807 // check if the position is beyond the last column
8808 const int lineAtMaxPos
= oper
.GetLineAt(this, maxPos
);
8809 if ( coord
>= lineEnds
[lineAtMaxPos
] )
8810 return clipToMinMax
? lineAtMaxPos
: -1;
8812 // or before the first one
8813 const int lineAt0
= oper
.GetLineAt(this, 0);
8814 if ( coord
< lineEnds
[lineAt0
] )
8818 // finally do perform the binary search
8819 while ( minPos
< maxPos
)
8821 wxCHECK_MSG( lineEnds
[oper
.GetLineAt(this, minPos
)] <= coord
&&
8822 coord
< lineEnds
[oper
.GetLineAt(this, maxPos
)],
8824 "wxGrid: internal error in PosToLine()" );
8826 if ( coord
>= lineEnds
[oper
.GetLineAt(this, maxPos
- 1)] )
8827 return oper
.GetLineAt(this, maxPos
);
8831 const int median
= minPos
+ (maxPos
- minPos
+ 1) / 2;
8832 if ( coord
< lineEnds
[oper
.GetLineAt(this, median
)] )
8838 return oper
.GetLineAt(this, maxPos
);
8841 int wxGrid::YToRow(int y
, bool clipToMinMax
) const
8843 return PosToLine(y
, clipToMinMax
, wxGridRowOperations());
8846 int wxGrid::XToCol(int x
, bool clipToMinMax
) const
8848 return PosToLine(x
, clipToMinMax
, wxGridColumnOperations());
8851 // return the row number that that the y coord is near the edge of, or -1 if
8852 // not near an edge.
8854 // coords can only possibly be near an edge if
8855 // (a) the row/column is large enough to still allow for an "inner" area
8856 // that is _not_ near the edge (i.e., if the height/width is smaller
8857 // than WXGRID_LABEL_EDGE_ZONE, coords are _never_ considered to be
8860 // (b) resizing rows/columns (the thing for which edge detection is
8861 // relevant at all) is enabled.
8863 int wxGrid::PosToEdgeOfLine(int pos
, const wxGridOperations
& oper
) const
8865 if ( !oper
.CanResizeLines(this) )
8868 const int line
= oper
.PosToLine(this, pos
, true);
8870 if ( oper
.GetLineSize(this, line
) > WXGRID_LABEL_EDGE_ZONE
)
8872 // We know that we are in this line, test whether we are close enough
8873 // to start or end border, respectively.
8874 if ( abs(oper
.GetLineEndPos(this, line
) - pos
) < WXGRID_LABEL_EDGE_ZONE
)
8876 else if ( line
> 0 &&
8877 pos
- oper
.GetLineStartPos(this,
8878 line
) < WXGRID_LABEL_EDGE_ZONE
)
8885 int wxGrid::YToEdgeOfRow(int y
) const
8887 return PosToEdgeOfLine(y
, wxGridRowOperations());
8890 int wxGrid::XToEdgeOfCol(int x
) const
8892 return PosToEdgeOfLine(x
, wxGridColumnOperations());
8895 wxRect
wxGrid::CellToRect( int row
, int col
) const
8897 wxRect
rect( -1, -1, -1, -1 );
8899 if ( row
>= 0 && row
< m_numRows
&&
8900 col
>= 0 && col
< m_numCols
)
8902 int i
, cell_rows
, cell_cols
;
8903 rect
.width
= rect
.height
= 0;
8904 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
8905 // if negative then find multicell owner
8910 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
8912 rect
.x
= GetColLeft(col
);
8913 rect
.y
= GetRowTop(row
);
8914 for (i
=col
; i
< col
+ cell_cols
; i
++)
8915 rect
.width
+= GetColWidth(i
);
8916 for (i
=row
; i
< row
+ cell_rows
; i
++)
8917 rect
.height
+= GetRowHeight(i
);
8920 // if grid lines are enabled, then the area of the cell is a bit smaller
8921 if (m_gridLinesEnabled
)
8930 bool wxGrid::IsVisible( int row
, int col
, bool wholeCellVisible
) const
8932 // get the cell rectangle in logical coords
8934 wxRect
r( CellToRect( row
, col
) );
8936 // convert to device coords
8938 int left
, top
, right
, bottom
;
8939 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
8940 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
8942 // check against the client area of the grid window
8944 m_gridWin
->GetClientSize( &cw
, &ch
);
8946 if ( wholeCellVisible
)
8948 // is the cell wholly visible ?
8949 return ( left
>= 0 && right
<= cw
&&
8950 top
>= 0 && bottom
<= ch
);
8954 // is the cell partly visible ?
8956 return ( ((left
>= 0 && left
< cw
) || (right
> 0 && right
<= cw
)) &&
8957 ((top
>= 0 && top
< ch
) || (bottom
> 0 && bottom
<= ch
)) );
8961 // make the specified cell location visible by doing a minimal amount
8964 void wxGrid::MakeCellVisible( int row
, int col
)
8967 int xpos
= -1, ypos
= -1;
8969 if ( row
>= 0 && row
< m_numRows
&&
8970 col
>= 0 && col
< m_numCols
)
8972 // get the cell rectangle in logical coords
8973 wxRect
r( CellToRect( row
, col
) );
8975 // convert to device coords
8976 int left
, top
, right
, bottom
;
8977 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
8978 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
8981 m_gridWin
->GetClientSize( &cw
, &ch
);
8987 else if ( bottom
> ch
)
8989 int h
= r
.GetHeight();
8991 for ( i
= row
- 1; i
>= 0; i
-- )
8993 int rowHeight
= GetRowHeight(i
);
8994 if ( h
+ rowHeight
> ch
)
9001 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
9002 // have rounding errors (this is important, because if we do,
9003 // we might not scroll at all and some cells won't be redrawn)
9005 // Sometimes GRID_SCROLL_LINE / 2 is not enough,
9006 // so just add a full scroll unit...
9007 ypos
+= m_scrollLineY
;
9010 // special handling for wide cells - show always left part of the cell!
9011 // Otherwise, e.g. when stepping from row to row, it would jump between
9012 // left and right part of the cell on every step!
9014 if ( left
< 0 || (right
- left
) >= cw
)
9018 else if ( right
> cw
)
9020 // position the view so that the cell is on the right
9022 CalcUnscrolledPosition(0, 0, &x0
, &y0
);
9023 xpos
= x0
+ (right
- cw
);
9025 // see comment for ypos above
9026 xpos
+= m_scrollLineX
;
9029 if ( xpos
!= -1 || ypos
!= -1 )
9032 xpos
/= m_scrollLineX
;
9034 ypos
/= m_scrollLineY
;
9035 Scroll( xpos
, ypos
);
9042 // ------ Grid cursor movement functions
9046 wxGrid::DoMoveCursor(bool expandSelection
,
9047 const wxGridDirectionOperations
& diroper
)
9049 if ( m_currentCellCoords
== wxGridNoCellCoords
)
9052 if ( expandSelection
)
9054 wxGridCellCoords coords
= m_selectedBlockCorner
;
9055 if ( coords
== wxGridNoCellCoords
)
9056 coords
= m_currentCellCoords
;
9058 if ( diroper
.IsAtBoundary(coords
) )
9061 diroper
.Advance(coords
);
9063 UpdateBlockBeingSelected(m_currentCellCoords
, coords
);
9065 else // don't expand selection
9069 if ( diroper
.IsAtBoundary(m_currentCellCoords
) )
9072 wxGridCellCoords coords
= m_currentCellCoords
;
9073 diroper
.Advance(coords
);
9081 bool wxGrid::MoveCursorUp(bool expandSelection
)
9083 return DoMoveCursor(expandSelection
,
9084 wxGridBackwardOperations(this, wxGridRowOperations()));
9087 bool wxGrid::MoveCursorDown(bool expandSelection
)
9089 return DoMoveCursor(expandSelection
,
9090 wxGridForwardOperations(this, wxGridRowOperations()));
9093 bool wxGrid::MoveCursorLeft(bool expandSelection
)
9095 return DoMoveCursor(expandSelection
,
9096 wxGridBackwardOperations(this, wxGridColumnOperations()));
9099 bool wxGrid::MoveCursorRight(bool expandSelection
)
9101 return DoMoveCursor(expandSelection
,
9102 wxGridForwardOperations(this, wxGridColumnOperations()));
9105 bool wxGrid::DoMoveCursorByPage(const wxGridDirectionOperations
& diroper
)
9107 if ( m_currentCellCoords
== wxGridNoCellCoords
)
9110 if ( diroper
.IsAtBoundary(m_currentCellCoords
) )
9113 const int oldRow
= m_currentCellCoords
.GetRow();
9114 int newRow
= diroper
.MoveByPixelDistance(oldRow
, m_gridWin
->GetClientSize().y
);
9115 if ( newRow
== oldRow
)
9117 wxGridCellCoords
coords(m_currentCellCoords
);
9118 diroper
.Advance(coords
);
9119 newRow
= coords
.GetRow();
9122 GoToCell(newRow
, m_currentCellCoords
.GetCol());
9127 bool wxGrid::MovePageUp()
9129 return DoMoveCursorByPage(
9130 wxGridBackwardOperations(this, wxGridRowOperations()));
9133 bool wxGrid::MovePageDown()
9135 return DoMoveCursorByPage(
9136 wxGridForwardOperations(this, wxGridRowOperations()));
9139 // helper of DoMoveCursorByBlock(): advance the cell coordinates using diroper
9140 // until we find a non-empty cell or reach the grid end
9142 wxGrid::AdvanceToNextNonEmpty(wxGridCellCoords
& coords
,
9143 const wxGridDirectionOperations
& diroper
)
9145 while ( !diroper
.IsAtBoundary(coords
) )
9147 diroper
.Advance(coords
);
9148 if ( !m_table
->IsEmpty(coords
) )
9154 wxGrid::DoMoveCursorByBlock(bool expandSelection
,
9155 const wxGridDirectionOperations
& diroper
)
9157 if ( !m_table
|| m_currentCellCoords
== wxGridNoCellCoords
)
9160 if ( diroper
.IsAtBoundary(m_currentCellCoords
) )
9163 wxGridCellCoords
coords(m_currentCellCoords
);
9164 if ( m_table
->IsEmpty(coords
) )
9166 // we are in an empty cell: find the next block of non-empty cells
9167 AdvanceToNextNonEmpty(coords
, diroper
);
9169 else // current cell is not empty
9171 diroper
.Advance(coords
);
9172 if ( m_table
->IsEmpty(coords
) )
9174 // we started at the end of a block, find the next one
9175 AdvanceToNextNonEmpty(coords
, diroper
);
9177 else // we're in a middle of a block
9179 // go to the end of it, i.e. find the last cell before the next
9181 while ( !diroper
.IsAtBoundary(coords
) )
9183 wxGridCellCoords
coordsNext(coords
);
9184 diroper
.Advance(coordsNext
);
9185 if ( m_table
->IsEmpty(coordsNext
) )
9188 coords
= coordsNext
;
9193 if ( expandSelection
)
9195 UpdateBlockBeingSelected(m_currentCellCoords
, coords
);
9206 bool wxGrid::MoveCursorUpBlock(bool expandSelection
)
9208 return DoMoveCursorByBlock(
9210 wxGridBackwardOperations(this, wxGridRowOperations())
9214 bool wxGrid::MoveCursorDownBlock( bool expandSelection
)
9216 return DoMoveCursorByBlock(
9218 wxGridForwardOperations(this, wxGridRowOperations())
9222 bool wxGrid::MoveCursorLeftBlock( bool expandSelection
)
9224 return DoMoveCursorByBlock(
9226 wxGridBackwardOperations(this, wxGridColumnOperations())
9230 bool wxGrid::MoveCursorRightBlock( bool expandSelection
)
9232 return DoMoveCursorByBlock(
9234 wxGridForwardOperations(this, wxGridColumnOperations())
9239 // ------ Label values and formatting
9242 void wxGrid::GetRowLabelAlignment( int *horiz
, int *vert
) const
9245 *horiz
= m_rowLabelHorizAlign
;
9247 *vert
= m_rowLabelVertAlign
;
9250 void wxGrid::GetColLabelAlignment( int *horiz
, int *vert
) const
9253 *horiz
= m_colLabelHorizAlign
;
9255 *vert
= m_colLabelVertAlign
;
9258 int wxGrid::GetColLabelTextOrientation() const
9260 return m_colLabelTextOrientation
;
9263 wxString
wxGrid::GetRowLabelValue( int row
) const
9267 return m_table
->GetRowLabelValue( row
);
9277 wxString
wxGrid::GetColLabelValue( int col
) const
9281 return m_table
->GetColLabelValue( col
);
9291 void wxGrid::SetRowLabelSize( int width
)
9293 wxASSERT( width
>= 0 || width
== wxGRID_AUTOSIZE
);
9295 if ( width
== wxGRID_AUTOSIZE
)
9297 width
= CalcColOrRowLabelAreaMinSize(wxGRID_ROW
);
9300 if ( width
!= m_rowLabelWidth
)
9304 m_rowLabelWin
->Show( false );
9305 m_cornerLabelWin
->Show( false );
9307 else if ( m_rowLabelWidth
== 0 )
9309 m_rowLabelWin
->Show( true );
9310 if ( m_colLabelHeight
> 0 )
9311 m_cornerLabelWin
->Show( true );
9314 m_rowLabelWidth
= width
;
9316 wxScrolledWindow::Refresh( true );
9320 void wxGrid::SetColLabelSize( int height
)
9322 wxASSERT( height
>=0 || height
== wxGRID_AUTOSIZE
);
9324 if ( height
== wxGRID_AUTOSIZE
)
9326 height
= CalcColOrRowLabelAreaMinSize(wxGRID_COLUMN
);
9329 if ( height
!= m_colLabelHeight
)
9333 m_colWindow
->Show( false );
9334 m_cornerLabelWin
->Show( false );
9336 else if ( m_colLabelHeight
== 0 )
9338 m_colWindow
->Show( true );
9339 if ( m_rowLabelWidth
> 0 )
9340 m_cornerLabelWin
->Show( true );
9343 m_colLabelHeight
= height
;
9345 wxScrolledWindow::Refresh( true );
9349 void wxGrid::SetLabelBackgroundColour( const wxColour
& colour
)
9351 if ( m_labelBackgroundColour
!= colour
)
9353 m_labelBackgroundColour
= colour
;
9354 m_rowLabelWin
->SetBackgroundColour( colour
);
9355 m_colWindow
->SetBackgroundColour( colour
);
9356 m_cornerLabelWin
->SetBackgroundColour( colour
);
9358 if ( !GetBatchCount() )
9360 m_rowLabelWin
->Refresh();
9361 m_colWindow
->Refresh();
9362 m_cornerLabelWin
->Refresh();
9367 void wxGrid::SetLabelTextColour( const wxColour
& colour
)
9369 if ( m_labelTextColour
!= colour
)
9371 m_labelTextColour
= colour
;
9372 if ( !GetBatchCount() )
9374 m_rowLabelWin
->Refresh();
9375 m_colWindow
->Refresh();
9380 void wxGrid::SetLabelFont( const wxFont
& font
)
9383 if ( !GetBatchCount() )
9385 m_rowLabelWin
->Refresh();
9386 m_colWindow
->Refresh();
9390 void wxGrid::SetRowLabelAlignment( int horiz
, int vert
)
9392 // allow old (incorrect) defs to be used
9395 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
9396 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
9397 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
9402 case wxTOP
: vert
= wxALIGN_TOP
; break;
9403 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
9404 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
9407 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
9409 m_rowLabelHorizAlign
= horiz
;
9412 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
9414 m_rowLabelVertAlign
= vert
;
9417 if ( !GetBatchCount() )
9419 m_rowLabelWin
->Refresh();
9423 void wxGrid::SetColLabelAlignment( int horiz
, int vert
)
9425 // allow old (incorrect) defs to be used
9428 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
9429 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
9430 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
9435 case wxTOP
: vert
= wxALIGN_TOP
; break;
9436 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
9437 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
9440 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
9442 m_colLabelHorizAlign
= horiz
;
9445 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
9447 m_colLabelVertAlign
= vert
;
9450 if ( !GetBatchCount() )
9452 m_colWindow
->Refresh();
9456 // Note: under MSW, the default column label font must be changed because it
9457 // does not support vertical printing
9459 // Example: wxFont font(9, wxSWISS, wxNORMAL, wxBOLD);
9460 // pGrid->SetLabelFont(font);
9461 // pGrid->SetColLabelTextOrientation(wxVERTICAL);
9463 void wxGrid::SetColLabelTextOrientation( int textOrientation
)
9465 if ( textOrientation
== wxHORIZONTAL
|| textOrientation
== wxVERTICAL
)
9466 m_colLabelTextOrientation
= textOrientation
;
9468 if ( !GetBatchCount() )
9469 m_colWindow
->Refresh();
9472 void wxGrid::SetRowLabelValue( int row
, const wxString
& s
)
9476 m_table
->SetRowLabelValue( row
, s
);
9477 if ( !GetBatchCount() )
9479 wxRect rect
= CellToRect( row
, 0 );
9480 if ( rect
.height
> 0 )
9482 CalcScrolledPosition(0, rect
.y
, &rect
.x
, &rect
.y
);
9484 rect
.width
= m_rowLabelWidth
;
9485 m_rowLabelWin
->Refresh( true, &rect
);
9491 void wxGrid::SetColLabelValue( int col
, const wxString
& s
)
9495 m_table
->SetColLabelValue( col
, s
);
9496 if ( !GetBatchCount() )
9498 if ( m_useNativeHeader
)
9500 GetColHeader()->UpdateColumn(col
);
9504 wxRect rect
= CellToRect( 0, col
);
9505 if ( rect
.width
> 0 )
9507 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &rect
.y
);
9509 rect
.height
= m_colLabelHeight
;
9510 GetColLabelWindow()->Refresh( true, &rect
);
9517 void wxGrid::SetGridLineColour( const wxColour
& colour
)
9519 if ( m_gridLineColour
!= colour
)
9521 m_gridLineColour
= colour
;
9523 if ( GridLinesEnabled() )
9528 void wxGrid::SetCellHighlightColour( const wxColour
& colour
)
9530 if ( m_cellHighlightColour
!= colour
)
9532 m_cellHighlightColour
= colour
;
9534 wxClientDC
dc( m_gridWin
);
9536 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
9537 DrawCellHighlight(dc
, attr
);
9542 void wxGrid::SetCellHighlightPenWidth(int width
)
9544 if (m_cellHighlightPenWidth
!= width
)
9546 m_cellHighlightPenWidth
= width
;
9548 // Just redrawing the cell highlight is not enough since that won't
9549 // make any visible change if the the thickness is getting smaller.
9550 int row
= m_currentCellCoords
.GetRow();
9551 int col
= m_currentCellCoords
.GetCol();
9552 if ( row
== -1 || col
== -1 || GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
9555 wxRect rect
= CellToRect(row
, col
);
9556 m_gridWin
->Refresh(true, &rect
);
9560 void wxGrid::SetCellHighlightROPenWidth(int width
)
9562 if (m_cellHighlightROPenWidth
!= width
)
9564 m_cellHighlightROPenWidth
= width
;
9566 // Just redrawing the cell highlight is not enough since that won't
9567 // make any visible change if the the thickness is getting smaller.
9568 int row
= m_currentCellCoords
.GetRow();
9569 int col
= m_currentCellCoords
.GetCol();
9570 if ( row
== -1 || col
== -1 ||
9571 GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
9574 wxRect rect
= CellToRect(row
, col
);
9575 m_gridWin
->Refresh(true, &rect
);
9579 void wxGrid::RedrawGridLines()
9581 // the lines will be redrawn when the window is thawn
9582 if ( GetBatchCount() )
9585 if ( GridLinesEnabled() )
9587 wxClientDC
dc( m_gridWin
);
9589 DrawAllGridLines( dc
, wxRegion() );
9591 else // remove the grid lines
9593 m_gridWin
->Refresh();
9597 void wxGrid::EnableGridLines( bool enable
)
9599 if ( enable
!= m_gridLinesEnabled
)
9601 m_gridLinesEnabled
= enable
;
9607 void wxGrid::DoClipGridLines(bool& var
, bool clip
)
9613 if ( GridLinesEnabled() )
9618 int wxGrid::GetDefaultRowSize() const
9620 return m_defaultRowHeight
;
9623 int wxGrid::GetRowSize( int row
) const
9625 wxCHECK_MSG( row
>= 0 && row
< m_numRows
, 0, _T("invalid row index") );
9627 return GetRowHeight(row
);
9630 int wxGrid::GetDefaultColSize() const
9632 return m_defaultColWidth
;
9635 int wxGrid::GetColSize( int col
) const
9637 wxCHECK_MSG( col
>= 0 && col
< m_numCols
, 0, _T("invalid column index") );
9639 return GetColWidth(col
);
9642 // ============================================================================
9643 // access to the grid attributes: each of them has a default value in the grid
9644 // itself and may be overidden on a per-cell basis
9645 // ============================================================================
9647 // ----------------------------------------------------------------------------
9648 // setting default attributes
9649 // ----------------------------------------------------------------------------
9651 void wxGrid::SetDefaultCellBackgroundColour( const wxColour
& col
)
9653 m_defaultCellAttr
->SetBackgroundColour(col
);
9655 m_gridWin
->SetBackgroundColour(col
);
9659 void wxGrid::SetDefaultCellTextColour( const wxColour
& col
)
9661 m_defaultCellAttr
->SetTextColour(col
);
9664 void wxGrid::SetDefaultCellAlignment( int horiz
, int vert
)
9666 m_defaultCellAttr
->SetAlignment(horiz
, vert
);
9669 void wxGrid::SetDefaultCellOverflow( bool allow
)
9671 m_defaultCellAttr
->SetOverflow(allow
);
9674 void wxGrid::SetDefaultCellFont( const wxFont
& font
)
9676 m_defaultCellAttr
->SetFont(font
);
9679 // For editors and renderers the type registry takes precedence over the
9680 // default attr, so we need to register the new editor/renderer for the string
9681 // data type in order to make setting a default editor/renderer appear to
9684 void wxGrid::SetDefaultRenderer(wxGridCellRenderer
*renderer
)
9686 RegisterDataType(wxGRID_VALUE_STRING
,
9688 GetDefaultEditorForType(wxGRID_VALUE_STRING
));
9691 void wxGrid::SetDefaultEditor(wxGridCellEditor
*editor
)
9693 RegisterDataType(wxGRID_VALUE_STRING
,
9694 GetDefaultRendererForType(wxGRID_VALUE_STRING
),
9698 // ----------------------------------------------------------------------------
9699 // access to the default attributes
9700 // ----------------------------------------------------------------------------
9702 wxColour
wxGrid::GetDefaultCellBackgroundColour() const
9704 return m_defaultCellAttr
->GetBackgroundColour();
9707 wxColour
wxGrid::GetDefaultCellTextColour() const
9709 return m_defaultCellAttr
->GetTextColour();
9712 wxFont
wxGrid::GetDefaultCellFont() const
9714 return m_defaultCellAttr
->GetFont();
9717 void wxGrid::GetDefaultCellAlignment( int *horiz
, int *vert
) const
9719 m_defaultCellAttr
->GetAlignment(horiz
, vert
);
9722 bool wxGrid::GetDefaultCellOverflow() const
9724 return m_defaultCellAttr
->GetOverflow();
9727 wxGridCellRenderer
*wxGrid::GetDefaultRenderer() const
9729 return m_defaultCellAttr
->GetRenderer(NULL
, 0, 0);
9732 wxGridCellEditor
*wxGrid::GetDefaultEditor() const
9734 return m_defaultCellAttr
->GetEditor(NULL
, 0, 0);
9737 // ----------------------------------------------------------------------------
9738 // access to cell attributes
9739 // ----------------------------------------------------------------------------
9741 wxColour
wxGrid::GetCellBackgroundColour(int row
, int col
) const
9743 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9744 wxColour colour
= attr
->GetBackgroundColour();
9750 wxColour
wxGrid::GetCellTextColour( int row
, int col
) const
9752 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9753 wxColour colour
= attr
->GetTextColour();
9759 wxFont
wxGrid::GetCellFont( int row
, int col
) const
9761 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9762 wxFont font
= attr
->GetFont();
9768 void wxGrid::GetCellAlignment( int row
, int col
, int *horiz
, int *vert
) const
9770 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9771 attr
->GetAlignment(horiz
, vert
);
9775 bool wxGrid::GetCellOverflow( int row
, int col
) const
9777 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9778 bool allow
= attr
->GetOverflow();
9784 void wxGrid::GetCellSize( int row
, int col
, int *num_rows
, int *num_cols
) const
9786 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9787 attr
->GetSize( num_rows
, num_cols
);
9791 wxGridCellRenderer
* wxGrid::GetCellRenderer(int row
, int col
) const
9793 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9794 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
9800 wxGridCellEditor
* wxGrid::GetCellEditor(int row
, int col
) const
9802 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9803 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
9809 bool wxGrid::IsReadOnly(int row
, int col
) const
9811 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9812 bool isReadOnly
= attr
->IsReadOnly();
9818 // ----------------------------------------------------------------------------
9819 // attribute support: cache, automatic provider creation, ...
9820 // ----------------------------------------------------------------------------
9822 bool wxGrid::CanHaveAttributes() const
9829 return m_table
->CanHaveAttributes();
9832 void wxGrid::ClearAttrCache()
9834 if ( m_attrCache
.row
!= -1 )
9836 wxGridCellAttr
*oldAttr
= m_attrCache
.attr
;
9837 m_attrCache
.attr
= NULL
;
9838 m_attrCache
.row
= -1;
9839 // wxSafeDecRec(...) might cause event processing that accesses
9840 // the cached attribute, if one exists (e.g. by deleting the
9841 // editor stored within the attribute). Therefore it is important
9842 // to invalidate the cache before calling wxSafeDecRef!
9843 wxSafeDecRef(oldAttr
);
9847 void wxGrid::CacheAttr(int row
, int col
, wxGridCellAttr
*attr
) const
9851 wxGrid
*self
= (wxGrid
*)this; // const_cast
9853 self
->ClearAttrCache();
9854 self
->m_attrCache
.row
= row
;
9855 self
->m_attrCache
.col
= col
;
9856 self
->m_attrCache
.attr
= attr
;
9861 bool wxGrid::LookupAttr(int row
, int col
, wxGridCellAttr
**attr
) const
9863 if ( row
== m_attrCache
.row
&& col
== m_attrCache
.col
)
9865 *attr
= m_attrCache
.attr
;
9866 wxSafeIncRef(m_attrCache
.attr
);
9868 #ifdef DEBUG_ATTR_CACHE
9869 gs_nAttrCacheHits
++;
9876 #ifdef DEBUG_ATTR_CACHE
9877 gs_nAttrCacheMisses
++;
9884 wxGridCellAttr
*wxGrid::GetCellAttr(int row
, int col
) const
9886 wxGridCellAttr
*attr
= NULL
;
9887 // Additional test to avoid looking at the cache e.g. for
9888 // wxNoCellCoords, as this will confuse memory management.
9891 if ( !LookupAttr(row
, col
, &attr
) )
9893 attr
= m_table
? m_table
->GetAttr(row
, col
, wxGridCellAttr::Any
)
9895 CacheAttr(row
, col
, attr
);
9901 attr
->SetDefAttr(m_defaultCellAttr
);
9905 attr
= m_defaultCellAttr
;
9912 wxGridCellAttr
*wxGrid::GetOrCreateCellAttr(int row
, int col
) const
9914 wxGridCellAttr
*attr
= NULL
;
9915 bool canHave
= ((wxGrid
*)this)->CanHaveAttributes();
9917 wxCHECK_MSG( canHave
, attr
, _T("Cell attributes not allowed"));
9918 wxCHECK_MSG( m_table
, attr
, _T("must have a table") );
9920 attr
= m_table
->GetAttr(row
, col
, wxGridCellAttr::Cell
);
9923 attr
= new wxGridCellAttr(m_defaultCellAttr
);
9925 // artificially inc the ref count to match DecRef() in caller
9927 m_table
->SetAttr(attr
, row
, col
);
9933 // ----------------------------------------------------------------------------
9934 // setting column attributes (wrappers around SetColAttr)
9935 // ----------------------------------------------------------------------------
9937 void wxGrid::SetColFormatBool(int col
)
9939 SetColFormatCustom(col
, wxGRID_VALUE_BOOL
);
9942 void wxGrid::SetColFormatNumber(int col
)
9944 SetColFormatCustom(col
, wxGRID_VALUE_NUMBER
);
9947 void wxGrid::SetColFormatFloat(int col
, int width
, int precision
)
9949 wxString typeName
= wxGRID_VALUE_FLOAT
;
9950 if ( (width
!= -1) || (precision
!= -1) )
9952 typeName
<< _T(':') << width
<< _T(',') << precision
;
9955 SetColFormatCustom(col
, typeName
);
9958 void wxGrid::SetColFormatCustom(int col
, const wxString
& typeName
)
9960 wxGridCellAttr
*attr
= m_table
->GetAttr(-1, col
, wxGridCellAttr::Col
);
9962 attr
= new wxGridCellAttr
;
9963 wxGridCellRenderer
*renderer
= GetDefaultRendererForType(typeName
);
9964 attr
->SetRenderer(renderer
);
9965 wxGridCellEditor
*editor
= GetDefaultEditorForType(typeName
);
9966 attr
->SetEditor(editor
);
9968 SetColAttr(col
, attr
);
9972 // ----------------------------------------------------------------------------
9973 // setting cell attributes: this is forwarded to the table
9974 // ----------------------------------------------------------------------------
9976 void wxGrid::SetAttr(int row
, int col
, wxGridCellAttr
*attr
)
9978 if ( CanHaveAttributes() )
9980 m_table
->SetAttr(attr
, row
, col
);
9989 void wxGrid::SetRowAttr(int row
, wxGridCellAttr
*attr
)
9991 if ( CanHaveAttributes() )
9993 m_table
->SetRowAttr(attr
, row
);
10002 void wxGrid::SetColAttr(int col
, wxGridCellAttr
*attr
)
10004 if ( CanHaveAttributes() )
10006 m_table
->SetColAttr(attr
, col
);
10011 wxSafeDecRef(attr
);
10015 void wxGrid::SetCellBackgroundColour( int row
, int col
, const wxColour
& colour
)
10017 if ( CanHaveAttributes() )
10019 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10020 attr
->SetBackgroundColour(colour
);
10025 void wxGrid::SetCellTextColour( int row
, int col
, const wxColour
& colour
)
10027 if ( CanHaveAttributes() )
10029 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10030 attr
->SetTextColour(colour
);
10035 void wxGrid::SetCellFont( int row
, int col
, const wxFont
& font
)
10037 if ( CanHaveAttributes() )
10039 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10040 attr
->SetFont(font
);
10045 void wxGrid::SetCellAlignment( int row
, int col
, int horiz
, int vert
)
10047 if ( CanHaveAttributes() )
10049 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10050 attr
->SetAlignment(horiz
, vert
);
10055 void wxGrid::SetCellOverflow( int row
, int col
, bool allow
)
10057 if ( CanHaveAttributes() )
10059 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10060 attr
->SetOverflow(allow
);
10065 void wxGrid::SetCellSize( int row
, int col
, int num_rows
, int num_cols
)
10067 if ( CanHaveAttributes() )
10069 int cell_rows
, cell_cols
;
10071 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10072 attr
->GetSize(&cell_rows
, &cell_cols
);
10073 attr
->SetSize(num_rows
, num_cols
);
10076 // Cannot set the size of a cell to 0 or negative values
10077 // While it is perfectly legal to do that, this function cannot
10078 // handle all the possibilies, do it by hand by getting the CellAttr.
10079 // You can only set the size of a cell to 1,1 or greater with this fn
10080 wxASSERT_MSG( !((cell_rows
< 1) || (cell_cols
< 1)),
10081 wxT("wxGrid::SetCellSize setting cell size that is already part of another cell"));
10082 wxASSERT_MSG( !((num_rows
< 1) || (num_cols
< 1)),
10083 wxT("wxGrid::SetCellSize setting cell size to < 1"));
10085 // if this was already a multicell then "turn off" the other cells first
10086 if ((cell_rows
> 1) || (cell_cols
> 1))
10089 for (j
=row
; j
< row
+ cell_rows
; j
++)
10091 for (i
=col
; i
< col
+ cell_cols
; i
++)
10093 if ((i
!= col
) || (j
!= row
))
10095 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
10096 attr_stub
->SetSize( 1, 1 );
10097 attr_stub
->DecRef();
10103 // mark the cells that will be covered by this cell to
10104 // negative or zero values to point back at this cell
10105 if (((num_rows
> 1) || (num_cols
> 1)) && (num_rows
>= 1) && (num_cols
>= 1))
10108 for (j
=row
; j
< row
+ num_rows
; j
++)
10110 for (i
=col
; i
< col
+ num_cols
; i
++)
10112 if ((i
!= col
) || (j
!= row
))
10114 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
10115 attr_stub
->SetSize( row
- j
, col
- i
);
10116 attr_stub
->DecRef();
10124 void wxGrid::SetCellRenderer(int row
, int col
, wxGridCellRenderer
*renderer
)
10126 if ( CanHaveAttributes() )
10128 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10129 attr
->SetRenderer(renderer
);
10134 void wxGrid::SetCellEditor(int row
, int col
, wxGridCellEditor
* editor
)
10136 if ( CanHaveAttributes() )
10138 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10139 attr
->SetEditor(editor
);
10144 void wxGrid::SetReadOnly(int row
, int col
, bool isReadOnly
)
10146 if ( CanHaveAttributes() )
10148 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10149 attr
->SetReadOnly(isReadOnly
);
10154 // ----------------------------------------------------------------------------
10155 // Data type registration
10156 // ----------------------------------------------------------------------------
10158 void wxGrid::RegisterDataType(const wxString
& typeName
,
10159 wxGridCellRenderer
* renderer
,
10160 wxGridCellEditor
* editor
)
10162 m_typeRegistry
->RegisterDataType(typeName
, renderer
, editor
);
10166 wxGridCellEditor
* wxGrid::GetDefaultEditorForCell(int row
, int col
) const
10168 wxString typeName
= m_table
->GetTypeName(row
, col
);
10169 return GetDefaultEditorForType(typeName
);
10172 wxGridCellRenderer
* wxGrid::GetDefaultRendererForCell(int row
, int col
) const
10174 wxString typeName
= m_table
->GetTypeName(row
, col
);
10175 return GetDefaultRendererForType(typeName
);
10178 wxGridCellEditor
* wxGrid::GetDefaultEditorForType(const wxString
& typeName
) const
10180 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
10181 if ( index
== wxNOT_FOUND
)
10183 wxFAIL_MSG(wxString::Format(wxT("Unknown data type name [%s]"), typeName
.c_str()));
10188 return m_typeRegistry
->GetEditor(index
);
10191 wxGridCellRenderer
* wxGrid::GetDefaultRendererForType(const wxString
& typeName
) const
10193 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
10194 if ( index
== wxNOT_FOUND
)
10196 wxFAIL_MSG(wxString::Format(wxT("Unknown data type name [%s]"), typeName
.c_str()));
10201 return m_typeRegistry
->GetRenderer(index
);
10204 // ----------------------------------------------------------------------------
10206 // ----------------------------------------------------------------------------
10208 void wxGrid::EnableDragRowSize( bool enable
)
10210 m_canDragRowSize
= enable
;
10213 void wxGrid::EnableDragColSize( bool enable
)
10215 m_canDragColSize
= enable
;
10218 void wxGrid::EnableDragGridSize( bool enable
)
10220 m_canDragGridSize
= enable
;
10223 void wxGrid::EnableDragCell( bool enable
)
10225 m_canDragCell
= enable
;
10228 void wxGrid::SetDefaultRowSize( int height
, bool resizeExistingRows
)
10230 m_defaultRowHeight
= wxMax( height
, m_minAcceptableRowHeight
);
10232 if ( resizeExistingRows
)
10234 // since we are resizing all rows to the default row size,
10235 // we can simply clear the row heights and row bottoms
10236 // arrays (which also allows us to take advantage of
10237 // some speed optimisations)
10238 m_rowHeights
.Empty();
10239 m_rowBottoms
.Empty();
10240 if ( !GetBatchCount() )
10245 void wxGrid::SetRowSize( int row
, int height
)
10247 wxCHECK_RET( row
>= 0 && row
< m_numRows
, _T("invalid row index") );
10249 // if < 0 then calculate new height from label
10253 wxArrayString lines
;
10254 wxClientDC
dc(m_rowLabelWin
);
10255 dc
.SetFont(GetLabelFont());
10256 StringToLines(GetRowLabelValue( row
), lines
);
10257 GetTextBoxSize( dc
, lines
, &w
, &h
);
10258 //check that it is not less than the minimal height
10259 height
= wxMax(h
, GetRowMinimalAcceptableHeight());
10262 // See comment in SetColSize
10263 if ( height
< GetRowMinimalAcceptableHeight())
10266 if ( m_rowHeights
.IsEmpty() )
10268 // need to really create the array
10272 int h
= wxMax( 0, height
);
10273 int diff
= h
- m_rowHeights
[row
];
10275 m_rowHeights
[row
] = h
;
10276 for ( int i
= row
; i
< m_numRows
; i
++ )
10278 m_rowBottoms
[i
] += diff
;
10281 if ( !GetBatchCount() )
10285 void wxGrid::SetDefaultColSize( int width
, bool resizeExistingCols
)
10287 // we dont allow zero default column width
10288 m_defaultColWidth
= wxMax( wxMax( width
, m_minAcceptableColWidth
), 1 );
10290 if ( resizeExistingCols
)
10292 // since we are resizing all columns to the default column size,
10293 // we can simply clear the col widths and col rights
10294 // arrays (which also allows us to take advantage of
10295 // some speed optimisations)
10296 m_colWidths
.Empty();
10297 m_colRights
.Empty();
10298 if ( !GetBatchCount() )
10303 void wxGrid::SetColSize( int col
, int width
)
10305 wxCHECK_RET( col
>= 0 && col
< m_numCols
, _T("invalid column index") );
10307 // if < 0 then calculate new width from label
10311 wxArrayString lines
;
10312 wxClientDC
dc(m_colWindow
);
10313 dc
.SetFont(GetLabelFont());
10314 StringToLines(GetColLabelValue(col
), lines
);
10315 if ( GetColLabelTextOrientation() == wxHORIZONTAL
)
10316 GetTextBoxSize( dc
, lines
, &w
, &h
);
10318 GetTextBoxSize( dc
, lines
, &h
, &w
);
10320 //check that it is not less than the minimal width
10321 width
= wxMax(width
, GetColMinimalAcceptableWidth());
10324 // should we check that it's bigger than GetColMinimalWidth(col) here?
10326 // No, because it is reasonable to assume the library user know's
10327 // what he is doing. However we should test against the weaker
10328 // constraint of minimalAcceptableWidth, as this breaks rendering
10330 // This test then fixes sf.net bug #645734
10332 if ( width
< GetColMinimalAcceptableWidth() )
10335 if ( m_colWidths
.IsEmpty() )
10337 // need to really create the array
10341 int w
= wxMax( 0, width
);
10342 int diff
= w
- m_colWidths
[col
];
10343 m_colWidths
[col
] = w
;
10345 for ( int colPos
= GetColPos(col
); colPos
< m_numCols
; colPos
++ )
10347 m_colRights
[GetColAt(colPos
)] += diff
;
10350 if ( !GetBatchCount() )
10357 void wxGrid::SetColMinimalWidth( int col
, int width
)
10359 if (width
> GetColMinimalAcceptableWidth())
10361 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)col
;
10362 m_colMinWidths
[key
] = width
;
10366 void wxGrid::SetRowMinimalHeight( int row
, int width
)
10368 if (width
> GetRowMinimalAcceptableHeight())
10370 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)row
;
10371 m_rowMinHeights
[key
] = width
;
10375 int wxGrid::GetColMinimalWidth(int col
) const
10377 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)col
;
10378 wxLongToLongHashMap::const_iterator it
= m_colMinWidths
.find(key
);
10380 return it
!= m_colMinWidths
.end() ? (int)it
->second
: m_minAcceptableColWidth
;
10383 int wxGrid::GetRowMinimalHeight(int row
) const
10385 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)row
;
10386 wxLongToLongHashMap::const_iterator it
= m_rowMinHeights
.find(key
);
10388 return it
!= m_rowMinHeights
.end() ? (int)it
->second
: m_minAcceptableRowHeight
;
10391 void wxGrid::SetColMinimalAcceptableWidth( int width
)
10393 // We do allow a width of 0 since this gives us
10394 // an easy way to temporarily hiding columns.
10396 m_minAcceptableColWidth
= width
;
10399 void wxGrid::SetRowMinimalAcceptableHeight( int height
)
10401 // We do allow a height of 0 since this gives us
10402 // an easy way to temporarily hiding rows.
10404 m_minAcceptableRowHeight
= height
;
10407 int wxGrid::GetColMinimalAcceptableWidth() const
10409 return m_minAcceptableColWidth
;
10412 int wxGrid::GetRowMinimalAcceptableHeight() const
10414 return m_minAcceptableRowHeight
;
10417 // ----------------------------------------------------------------------------
10419 // ----------------------------------------------------------------------------
10422 wxGrid::AutoSizeColOrRow(int colOrRow
, bool setAsMin
, wxGridDirection direction
)
10424 const bool column
= direction
== wxGRID_COLUMN
;
10426 wxClientDC
dc(m_gridWin
);
10428 // cancel editing of cell
10429 HideCellEditControl();
10430 SaveEditControlValue();
10432 // init both of them to avoid compiler warnings, even if we only need one
10440 wxCoord extent
, extentMax
= 0;
10441 int max
= column
? m_numRows
: m_numCols
;
10442 for ( int rowOrCol
= 0; rowOrCol
< max
; rowOrCol
++ )
10449 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
10450 wxGridCellRenderer
*renderer
= attr
->GetRenderer(this, row
, col
);
10453 wxSize size
= renderer
->GetBestSize(*this, *attr
, dc
, row
, col
);
10454 extent
= column
? size
.x
: size
.y
;
10455 if ( extent
> extentMax
)
10456 extentMax
= extent
;
10458 renderer
->DecRef();
10464 // now also compare with the column label extent
10466 dc
.SetFont( GetLabelFont() );
10470 dc
.GetMultiLineTextExtent( GetColLabelValue(col
), &w
, &h
);
10471 if ( GetColLabelTextOrientation() == wxVERTICAL
)
10475 dc
.GetMultiLineTextExtent( GetRowLabelValue(row
), &w
, &h
);
10477 extent
= column
? w
: h
;
10478 if ( extent
> extentMax
)
10479 extentMax
= extent
;
10483 // empty column - give default extent (notice that if extentMax is less
10484 // than default extent but != 0, it's OK)
10485 extentMax
= column
? m_defaultColWidth
: m_defaultRowHeight
;
10490 // leave some space around text
10498 // Ensure automatic width is not less than minimal width. See the
10499 // comment in SetColSize() for explanation of why this isn't done
10500 // in SetColSize().
10502 extentMax
= wxMax(extentMax
, GetColMinimalWidth(col
));
10504 SetColSize( col
, extentMax
);
10505 if ( !GetBatchCount() )
10507 if ( m_useNativeHeader
)
10509 GetColHeader()->UpdateColumn(col
);
10514 m_gridWin
->GetClientSize( &cw
, &ch
);
10515 wxRect
rect ( CellToRect( 0, col
) );
10517 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
10518 rect
.width
= cw
- rect
.x
;
10519 rect
.height
= m_colLabelHeight
;
10520 GetColLabelWindow()->Refresh( true, &rect
);
10526 // Ensure automatic width is not less than minimal height. See the
10527 // comment in SetColSize() for explanation of why this isn't done
10528 // in SetRowSize().
10530 extentMax
= wxMax(extentMax
, GetRowMinimalHeight(row
));
10532 SetRowSize(row
, extentMax
);
10533 if ( !GetBatchCount() )
10536 m_gridWin
->GetClientSize( &cw
, &ch
);
10537 wxRect
rect( CellToRect( row
, 0 ) );
10539 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
10540 rect
.width
= m_rowLabelWidth
;
10541 rect
.height
= ch
- rect
.y
;
10542 m_rowLabelWin
->Refresh( true, &rect
);
10549 SetColMinimalWidth(col
, extentMax
);
10551 SetRowMinimalHeight(row
, extentMax
);
10555 wxCoord
wxGrid::CalcColOrRowLabelAreaMinSize(wxGridDirection direction
)
10557 // calculate size for the rows or columns?
10558 const bool calcRows
= direction
== wxGRID_ROW
;
10560 wxClientDC
dc(calcRows
? GetGridRowLabelWindow()
10561 : GetGridColLabelWindow());
10562 dc
.SetFont(GetLabelFont());
10564 // which dimension should we take into account for calculations?
10566 // for columns, the text can be only horizontal so it's easy but for rows
10567 // we also have to take into account the text orientation
10569 useWidth
= calcRows
|| (GetColLabelTextOrientation() == wxVERTICAL
);
10571 wxArrayString lines
;
10572 wxCoord extentMax
= 0;
10574 const int numRowsOrCols
= calcRows
? m_numRows
: m_numCols
;
10575 for ( int rowOrCol
= 0; rowOrCol
< numRowsOrCols
; rowOrCol
++ )
10579 wxString label
= calcRows
? GetRowLabelValue(rowOrCol
)
10580 : GetColLabelValue(rowOrCol
);
10581 StringToLines(label
, lines
);
10584 GetTextBoxSize(dc
, lines
, &w
, &h
);
10586 const wxCoord extent
= useWidth
? w
: h
;
10587 if ( extent
> extentMax
)
10588 extentMax
= extent
;
10593 // empty column - give default extent (notice that if extentMax is less
10594 // than default extent but != 0, it's OK)
10595 extentMax
= calcRows
? GetDefaultRowLabelSize()
10596 : GetDefaultColLabelSize();
10599 // leave some space around text (taken from AutoSizeColOrRow)
10608 int wxGrid::SetOrCalcColumnSizes(bool calcOnly
, bool setAsMin
)
10610 int width
= m_rowLabelWidth
;
10612 wxGridUpdateLocker locker
;
10614 locker
.Create(this);
10616 for ( int col
= 0; col
< m_numCols
; col
++ )
10619 AutoSizeColumn(col
, setAsMin
);
10621 width
+= GetColWidth(col
);
10627 int wxGrid::SetOrCalcRowSizes(bool calcOnly
, bool setAsMin
)
10629 int height
= m_colLabelHeight
;
10631 wxGridUpdateLocker locker
;
10633 locker
.Create(this);
10635 for ( int row
= 0; row
< m_numRows
; row
++ )
10638 AutoSizeRow(row
, setAsMin
);
10640 height
+= GetRowHeight(row
);
10646 void wxGrid::AutoSize()
10648 wxGridUpdateLocker
locker(this);
10650 wxSize
size(SetOrCalcColumnSizes(false) - m_rowLabelWidth
+ m_extraWidth
,
10651 SetOrCalcRowSizes(false) - m_colLabelHeight
+ m_extraHeight
);
10653 // we know that we're not going to have scrollbars so disable them now to
10654 // avoid trouble in SetClientSize() which can otherwise set the correct
10655 // client size but also leave space for (not needed any more) scrollbars
10656 SetScrollbars(0, 0, 0, 0, 0, 0, true);
10658 // restore the scroll rate parameters overwritten by SetScrollbars()
10659 SetScrollRate(m_scrollLineX
, m_scrollLineY
);
10661 SetClientSize(size
.x
+ m_rowLabelWidth
, size
.y
+ m_colLabelHeight
);
10664 void wxGrid::AutoSizeRowLabelSize( int row
)
10666 // Hide the edit control, so it
10667 // won't interfere with drag-shrinking.
10668 if ( IsCellEditControlShown() )
10670 HideCellEditControl();
10671 SaveEditControlValue();
10674 // autosize row height depending on label text
10675 SetRowSize(row
, -1);
10679 void wxGrid::AutoSizeColLabelSize( int col
)
10681 // Hide the edit control, so it
10682 // won't interfere with drag-shrinking.
10683 if ( IsCellEditControlShown() )
10685 HideCellEditControl();
10686 SaveEditControlValue();
10689 // autosize column width depending on label text
10690 SetColSize(col
, -1);
10694 wxSize
wxGrid::DoGetBestSize() const
10696 wxGrid
*self
= (wxGrid
*)this; // const_cast
10698 // we do the same as in AutoSize() here with the exception that we don't
10699 // change the column/row sizes, only calculate them
10700 wxSize
size(self
->SetOrCalcColumnSizes(true) - m_rowLabelWidth
+ m_extraWidth
,
10701 self
->SetOrCalcRowSizes(true) - m_colLabelHeight
+ m_extraHeight
);
10703 // NOTE: This size should be cached, but first we need to add calls to
10704 // InvalidateBestSize everywhere that could change the results of this
10706 // CacheBestSize(size);
10708 return wxSize(size
.x
+ m_rowLabelWidth
, size
.y
+ m_colLabelHeight
)
10709 + GetWindowBorderSize();
10717 wxPen
& wxGrid::GetDividerPen() const
10722 // ----------------------------------------------------------------------------
10723 // cell value accessor functions
10724 // ----------------------------------------------------------------------------
10726 void wxGrid::SetCellValue( int row
, int col
, const wxString
& s
)
10730 m_table
->SetValue( row
, col
, s
);
10731 if ( !GetBatchCount() )
10734 wxRect
rect( CellToRect( row
, col
) );
10736 rect
.width
= m_gridWin
->GetClientSize().GetWidth();
10737 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
10738 m_gridWin
->Refresh( false, &rect
);
10741 if ( m_currentCellCoords
.GetRow() == row
&&
10742 m_currentCellCoords
.GetCol() == col
&&
10743 IsCellEditControlShown())
10744 // Note: If we are using IsCellEditControlEnabled,
10745 // this interacts badly with calling SetCellValue from
10746 // an EVT_GRID_CELL_CHANGE handler.
10748 HideCellEditControl();
10749 ShowCellEditControl(); // will reread data from table
10754 // ----------------------------------------------------------------------------
10755 // block, row and column selection
10756 // ----------------------------------------------------------------------------
10758 void wxGrid::SelectRow( int row
, bool addToSelected
)
10760 if ( !m_selection
)
10763 if ( !addToSelected
)
10766 m_selection
->SelectRow(row
);
10769 void wxGrid::SelectCol( int col
, bool addToSelected
)
10771 if ( !m_selection
)
10774 if ( !addToSelected
)
10777 m_selection
->SelectCol(col
);
10780 void wxGrid::SelectBlock(int topRow
, int leftCol
, int bottomRow
, int rightCol
,
10781 bool addToSelected
)
10783 if ( !m_selection
)
10786 if ( !addToSelected
)
10789 m_selection
->SelectBlock(topRow
, leftCol
, bottomRow
, rightCol
);
10792 void wxGrid::SelectAll()
10794 if ( m_numRows
> 0 && m_numCols
> 0 )
10797 m_selection
->SelectBlock( 0, 0, m_numRows
- 1, m_numCols
- 1 );
10801 // ----------------------------------------------------------------------------
10802 // cell, row and col deselection
10803 // ----------------------------------------------------------------------------
10805 void wxGrid::DeselectLine(int line
, const wxGridOperations
& oper
)
10807 if ( !m_selection
)
10810 const wxGridSelectionModes mode
= m_selection
->GetSelectionMode();
10811 if ( mode
== oper
.GetSelectionMode() )
10813 const wxGridCellCoords
c(oper
.MakeCoords(line
, 0));
10814 if ( m_selection
->IsInSelection(c
) )
10815 m_selection
->ToggleCellSelection(c
);
10817 else if ( mode
!= oper
.Dual().GetSelectionMode() )
10819 const int nOther
= oper
.Dual().GetNumberOfLines(this);
10820 for ( int i
= 0; i
< nOther
; i
++ )
10822 const wxGridCellCoords
c(oper
.MakeCoords(line
, i
));
10823 if ( m_selection
->IsInSelection(c
) )
10824 m_selection
->ToggleCellSelection(c
);
10827 //else: can only select orthogonal lines so no lines in this direction
10828 // could have been selected anyhow
10831 void wxGrid::DeselectRow(int row
)
10833 DeselectLine(row
, wxGridRowOperations());
10836 void wxGrid::DeselectCol(int col
)
10838 DeselectLine(col
, wxGridColumnOperations());
10841 void wxGrid::DeselectCell( int row
, int col
)
10843 if ( m_selection
&& m_selection
->IsInSelection(row
, col
) )
10844 m_selection
->ToggleCellSelection(row
, col
);
10847 bool wxGrid::IsSelection() const
10849 return ( m_selection
&& (m_selection
->IsSelection() ||
10850 ( m_selectedBlockTopLeft
!= wxGridNoCellCoords
&&
10851 m_selectedBlockBottomRight
!= wxGridNoCellCoords
) ) );
10854 bool wxGrid::IsInSelection( int row
, int col
) const
10856 return ( m_selection
&& (m_selection
->IsInSelection( row
, col
) ||
10857 ( row
>= m_selectedBlockTopLeft
.GetRow() &&
10858 col
>= m_selectedBlockTopLeft
.GetCol() &&
10859 row
<= m_selectedBlockBottomRight
.GetRow() &&
10860 col
<= m_selectedBlockBottomRight
.GetCol() )) );
10863 wxGridCellCoordsArray
wxGrid::GetSelectedCells() const
10867 wxGridCellCoordsArray a
;
10871 return m_selection
->m_cellSelection
;
10874 wxGridCellCoordsArray
wxGrid::GetSelectionBlockTopLeft() const
10878 wxGridCellCoordsArray a
;
10882 return m_selection
->m_blockSelectionTopLeft
;
10885 wxGridCellCoordsArray
wxGrid::GetSelectionBlockBottomRight() const
10889 wxGridCellCoordsArray a
;
10893 return m_selection
->m_blockSelectionBottomRight
;
10896 wxArrayInt
wxGrid::GetSelectedRows() const
10904 return m_selection
->m_rowSelection
;
10907 wxArrayInt
wxGrid::GetSelectedCols() const
10915 return m_selection
->m_colSelection
;
10918 void wxGrid::ClearSelection()
10920 wxRect r1
= BlockToDeviceRect(m_selectedBlockTopLeft
,
10921 m_selectedBlockBottomRight
);
10922 wxRect r2
= BlockToDeviceRect(m_currentCellCoords
,
10923 m_selectedBlockCorner
);
10925 m_selectedBlockTopLeft
=
10926 m_selectedBlockBottomRight
=
10927 m_selectedBlockCorner
= wxGridNoCellCoords
;
10929 Refresh( false, &r1
);
10930 Refresh( false, &r2
);
10933 m_selection
->ClearSelection();
10936 // This function returns the rectangle that encloses the given block
10937 // in device coords clipped to the client size of the grid window.
10939 wxRect
wxGrid::BlockToDeviceRect( const wxGridCellCoords
& topLeft
,
10940 const wxGridCellCoords
& bottomRight
) const
10943 wxRect tempCellRect
= CellToRect(topLeft
);
10944 if ( tempCellRect
!= wxGridNoCellRect
)
10946 resultRect
= tempCellRect
;
10950 resultRect
= wxRect(0, 0, 0, 0);
10953 tempCellRect
= CellToRect(bottomRight
);
10954 if ( tempCellRect
!= wxGridNoCellRect
)
10956 resultRect
+= tempCellRect
;
10960 // If both inputs were "wxGridNoCellRect," then there's nothing to do.
10961 return wxGridNoCellRect
;
10964 // Ensure that left/right and top/bottom pairs are in order.
10965 int left
= resultRect
.GetLeft();
10966 int top
= resultRect
.GetTop();
10967 int right
= resultRect
.GetRight();
10968 int bottom
= resultRect
.GetBottom();
10970 int leftCol
= topLeft
.GetCol();
10971 int topRow
= topLeft
.GetRow();
10972 int rightCol
= bottomRight
.GetCol();
10973 int bottomRow
= bottomRight
.GetRow();
10982 leftCol
= rightCol
;
10993 topRow
= bottomRow
;
10997 // The following loop is ONLY necessary to detect and handle merged cells.
10999 m_gridWin
->GetClientSize( &cw
, &ch
);
11001 // Get the origin coordinates: notice that they will be negative if the
11002 // grid is scrolled downwards/to the right.
11003 int gridOriginX
= 0;
11004 int gridOriginY
= 0;
11005 CalcScrolledPosition(gridOriginX
, gridOriginY
, &gridOriginX
, &gridOriginY
);
11007 int onScreenLeftmostCol
= internalXToCol(-gridOriginX
);
11008 int onScreenUppermostRow
= internalYToRow(-gridOriginY
);
11010 int onScreenRightmostCol
= internalXToCol(-gridOriginX
+ cw
);
11011 int onScreenBottommostRow
= internalYToRow(-gridOriginY
+ ch
);
11013 // Bound our loop so that we only examine the portion of the selected block
11014 // that is shown on screen. Therefore, we compare the Top-Left block values
11015 // to the Top-Left screen values, and the Bottom-Right block values to the
11016 // Bottom-Right screen values, choosing appropriately.
11017 const int visibleTopRow
= wxMax(topRow
, onScreenUppermostRow
);
11018 const int visibleBottomRow
= wxMin(bottomRow
, onScreenBottommostRow
);
11019 const int visibleLeftCol
= wxMax(leftCol
, onScreenLeftmostCol
);
11020 const int visibleRightCol
= wxMin(rightCol
, onScreenRightmostCol
);
11022 for ( int j
= visibleTopRow
; j
<= visibleBottomRow
; j
++ )
11024 for ( int i
= visibleLeftCol
; i
<= visibleRightCol
; i
++ )
11026 if ( (j
== visibleTopRow
) || (j
== visibleBottomRow
) ||
11027 (i
== visibleLeftCol
) || (i
== visibleRightCol
) )
11029 tempCellRect
= CellToRect( j
, i
);
11031 if (tempCellRect
.x
< left
)
11032 left
= tempCellRect
.x
;
11033 if (tempCellRect
.y
< top
)
11034 top
= tempCellRect
.y
;
11035 if (tempCellRect
.x
+ tempCellRect
.width
> right
)
11036 right
= tempCellRect
.x
+ tempCellRect
.width
;
11037 if (tempCellRect
.y
+ tempCellRect
.height
> bottom
)
11038 bottom
= tempCellRect
.y
+ tempCellRect
.height
;
11042 i
= visibleRightCol
; // jump over inner cells.
11047 // Convert to scrolled coords
11048 CalcScrolledPosition( left
, top
, &left
, &top
);
11049 CalcScrolledPosition( right
, bottom
, &right
, &bottom
);
11051 if (right
< 0 || bottom
< 0 || left
> cw
|| top
> ch
)
11052 return wxRect(0,0,0,0);
11054 resultRect
.SetLeft( wxMax(0, left
) );
11055 resultRect
.SetTop( wxMax(0, top
) );
11056 resultRect
.SetRight( wxMin(cw
, right
) );
11057 resultRect
.SetBottom( wxMin(ch
, bottom
) );
11062 // ----------------------------------------------------------------------------
11064 // ----------------------------------------------------------------------------
11066 #if wxUSE_DRAG_AND_DROP
11068 // this allow setting drop target directly on wxGrid
11069 void wxGrid::SetDropTarget(wxDropTarget
*dropTarget
)
11071 GetGridWindow()->SetDropTarget(dropTarget
);
11074 #endif // wxUSE_DRAG_AND_DROP
11076 // ----------------------------------------------------------------------------
11077 // grid event classes
11078 // ----------------------------------------------------------------------------
11080 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent
, wxNotifyEvent
)
11082 wxGridEvent::wxGridEvent( int id
, wxEventType type
, wxObject
* obj
,
11083 int row
, int col
, int x
, int y
, bool sel
,
11084 bool control
, bool shift
, bool alt
, bool meta
)
11085 : wxNotifyEvent( type
, id
),
11086 wxKeyboardState(control
, shift
, alt
, meta
)
11088 Init(row
, col
, x
, y
, sel
);
11090 SetEventObject(obj
);
11093 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent
, wxNotifyEvent
)
11095 wxGridSizeEvent::wxGridSizeEvent( int id
, wxEventType type
, wxObject
* obj
,
11096 int rowOrCol
, int x
, int y
,
11097 bool control
, bool shift
, bool alt
, bool meta
)
11098 : wxNotifyEvent( type
, id
),
11099 wxKeyboardState(control
, shift
, alt
, meta
)
11101 Init(rowOrCol
, x
, y
);
11103 SetEventObject(obj
);
11107 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent
, wxNotifyEvent
)
11109 wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id
, wxEventType type
, wxObject
* obj
,
11110 const wxGridCellCoords
& topLeft
,
11111 const wxGridCellCoords
& bottomRight
,
11112 bool sel
, bool control
,
11113 bool shift
, bool alt
, bool meta
)
11114 : wxNotifyEvent( type
, id
),
11115 wxKeyboardState(control
, shift
, alt
, meta
)
11117 Init(topLeft
, bottomRight
, sel
);
11119 SetEventObject(obj
);
11123 IMPLEMENT_DYNAMIC_CLASS(wxGridEditorCreatedEvent
, wxCommandEvent
)
11125 wxGridEditorCreatedEvent::wxGridEditorCreatedEvent(int id
, wxEventType type
,
11126 wxObject
* obj
, int row
,
11127 int col
, wxControl
* ctrl
)
11128 : wxCommandEvent(type
, id
)
11130 SetEventObject(obj
);
11136 #endif // wxUSE_GRID