1 ///////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/grid.cpp
3 // Purpose: wxGrid and related classes
4 // Author: Michael Bedward (based on code by Julian Smart, Robin Dunn)
5 // Modified by: Robin Dunn, Vadim Zeitlin, Santiago Palacios
8 // Copyright: (c) Michael Bedward (mbedward@ozemail.com.au)
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
15 - Replace use of wxINVERT with wxOverlay
16 - Make Begin/EndBatch() the same as the generic Freeze/Thaw()
17 - Review the column reordering code, it's a mess.
18 - Implement row reordering after dealing with the columns.
21 // For compilers that support precompilation, includes "wx/wx.h".
22 #include "wx/wxprec.h"
34 #include "wx/dcclient.h"
35 #include "wx/settings.h"
37 #include "wx/textctrl.h"
38 #include "wx/checkbox.h"
39 #include "wx/combobox.h"
40 #include "wx/valtext.h"
43 #include "wx/listbox.h"
46 #include "wx/textfile.h"
47 #include "wx/spinctrl.h"
48 #include "wx/tokenzr.h"
49 #include "wx/renderer.h"
50 #include "wx/headerctrl.h"
52 #include "wx/generic/gridsel.h"
54 const char wxGridNameStr
[] = "grid";
56 #if defined(__WXMOTIF__)
57 #define WXUNUSED_MOTIF(identifier) WXUNUSED(identifier)
59 #define WXUNUSED_MOTIF(identifier) identifier
62 #if defined(__WXGTK__)
63 #define WXUNUSED_GTK(identifier) WXUNUSED(identifier)
65 #define WXUNUSED_GTK(identifier) identifier
68 // Required for wxIs... functions
71 // ----------------------------------------------------------------------------
73 // ----------------------------------------------------------------------------
75 WX_DEFINE_ARRAY_WITH_DECL_PTR(wxGridCellAttr
*, wxArrayAttrs
,
76 class WXDLLIMPEXP_ADV
);
78 struct wxGridCellWithAttr
80 wxGridCellWithAttr(int row
, int col
, wxGridCellAttr
*attr_
)
81 : coords(row
, col
), attr(attr_
)
86 wxGridCellWithAttr(const wxGridCellWithAttr
& other
)
87 : coords(other
.coords
),
93 wxGridCellWithAttr
& operator=(const wxGridCellWithAttr
& other
)
95 coords
= other
.coords
;
96 if (attr
!= other
.attr
)
105 void ChangeAttr(wxGridCellAttr
* new_attr
)
107 if (attr
!= new_attr
)
109 // "Delete" (i.e. DecRef) the old attribute.
112 // Take ownership of the new attribute, i.e. no IncRef.
116 ~wxGridCellWithAttr()
121 wxGridCellCoords coords
;
122 wxGridCellAttr
*attr
;
125 WX_DECLARE_OBJARRAY_WITH_DECL(wxGridCellWithAttr
, wxGridCellWithAttrArray
,
126 class WXDLLIMPEXP_ADV
);
128 #include "wx/arrimpl.cpp"
130 WX_DEFINE_OBJARRAY(wxGridCellCoordsArray
)
131 WX_DEFINE_OBJARRAY(wxGridCellWithAttrArray
)
133 // ----------------------------------------------------------------------------
135 // ----------------------------------------------------------------------------
137 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_LEFT_CLICK
)
138 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_RIGHT_CLICK
)
139 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_LEFT_DCLICK
)
140 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_RIGHT_DCLICK
)
141 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_BEGIN_DRAG
)
142 DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_LEFT_CLICK
)
143 DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_RIGHT_CLICK
)
144 DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_LEFT_DCLICK
)
145 DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_RIGHT_DCLICK
)
146 DEFINE_EVENT_TYPE(wxEVT_GRID_ROW_SIZE
)
147 DEFINE_EVENT_TYPE(wxEVT_GRID_COL_SIZE
)
148 DEFINE_EVENT_TYPE(wxEVT_GRID_COL_MOVE
)
149 DEFINE_EVENT_TYPE(wxEVT_GRID_COL_SORT
)
150 DEFINE_EVENT_TYPE(wxEVT_GRID_RANGE_SELECT
)
151 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_CHANGE
)
152 DEFINE_EVENT_TYPE(wxEVT_GRID_SELECT_CELL
)
153 DEFINE_EVENT_TYPE(wxEVT_GRID_EDITOR_SHOWN
)
154 DEFINE_EVENT_TYPE(wxEVT_GRID_EDITOR_HIDDEN
)
155 DEFINE_EVENT_TYPE(wxEVT_GRID_EDITOR_CREATED
)
157 // ----------------------------------------------------------------------------
159 // ----------------------------------------------------------------------------
161 // header column providing access to the column information stored in wxGrid
162 // via wxHeaderColumn interface
163 class wxGridHeaderColumn
: public wxHeaderColumn
166 wxGridHeaderColumn(wxGrid
*grid
, int col
)
172 virtual wxString
GetTitle() const { return m_grid
->GetColLabelValue(m_col
); }
173 virtual wxBitmap
GetBitmap() const { return wxNullBitmap
; }
174 virtual int GetWidth() const { return m_grid
->GetColSize(m_col
); }
175 virtual int GetMinWidth() const { return 0; }
176 virtual wxAlignment
GetAlignment() const
180 m_grid
->GetColLabelAlignment(&horz
, &vert
);
182 return static_cast<wxAlignment
>(horz
);
185 virtual int GetFlags() const
187 // we can't know in advance whether we can sort by this column or not
188 // with wxGrid API so suppose we can by default
189 int flags
= wxCOL_SORTABLE
;
190 if ( m_grid
->CanDragColSize() )
191 flags
|= wxCOL_RESIZABLE
;
192 if ( m_grid
->CanDragColMove() )
193 flags
|= wxCOL_REORDERABLE
;
194 if ( GetWidth() == 0 )
195 flags
|= wxCOL_HIDDEN
;
200 virtual bool IsSortKey() const
202 return m_grid
->IsSortingBy(m_col
);
205 virtual bool IsSortOrderAscending() const
207 return m_grid
->IsSortOrderAscending();
211 // these really should be const but are not because the column needs to be
212 // assignable to be used in a wxVector (in STL build, in non-STL build we
213 // avoid the need for this)
218 // header control retreiving column information from the grid
219 class wxGridHeaderCtrl
: public wxHeaderCtrl
222 wxGridHeaderCtrl(wxGrid
*owner
)
223 : wxHeaderCtrl(owner
,
227 owner
->CanDragColMove() ? wxHD_DRAGDROP
: 0)
232 virtual wxHeaderColumn
& GetColumn(unsigned int idx
)
234 return m_columns
[idx
];
238 wxGrid
*GetOwner() const { return static_cast<wxGrid
*>(GetParent()); }
240 // override the base class method to update our m_columns array
241 virtual void OnColumnCountChanging(unsigned int count
)
243 const unsigned countOld
= m_columns
.size();
244 if ( count
< countOld
)
246 // just discard the columns which don't exist any more (notice that
247 // we can't use resize() here as it would require the vector
248 // value_type, i.e. wxGridHeaderColumn to be default constructible,
250 m_columns
.erase(m_columns
.begin() + count
, m_columns
.end());
252 else // new columns added
254 // add columns for the new elements
255 for ( unsigned n
= countOld
; n
< count
; n
++ )
256 m_columns
.push_back(wxGridHeaderColumn(GetOwner(), n
));
260 // override to implement column auto sizing
261 virtual bool UpdateColumnWidthToFit(unsigned int idx
, int widthTitle
)
263 // TODO: currently grid doesn't support computing the column best width
264 // from its contents so we just use the best label width as is
265 GetOwner()->SetColSize(idx
, widthTitle
);
271 // event handlers forwarding wxHeaderCtrl events to wxGrid
272 void OnClick(wxHeaderCtrlEvent
& event
)
274 GetOwner()->DoColHeaderClick(event
.GetColumn());
277 void OnBeginResize(wxHeaderCtrlEvent
& event
)
279 GetOwner()->DoStartResizeCol(event
.GetColumn());
284 void OnResizing(wxHeaderCtrlEvent
& event
)
286 GetOwner()->DoUpdateResizeColWidth(event
.GetWidth());
289 void OnEndResize(wxHeaderCtrlEvent
& event
)
291 GetOwner()->DoEndDragResizeCol();
296 void OnBeginReorder(wxHeaderCtrlEvent
& event
)
298 GetOwner()->DoStartMoveCol(event
.GetColumn());
301 void OnEndReorder(wxHeaderCtrlEvent
& event
)
303 GetOwner()->DoEndMoveCol(event
.GetNewOrder());
306 wxVector
<wxGridHeaderColumn
> m_columns
;
308 DECLARE_EVENT_TABLE()
309 DECLARE_NO_COPY_CLASS(wxGridHeaderCtrl
)
312 BEGIN_EVENT_TABLE(wxGridHeaderCtrl
, wxHeaderCtrl
)
313 EVT_HEADER_CLICK(wxID_ANY
, wxGridHeaderCtrl::OnClick
)
315 EVT_HEADER_BEGIN_RESIZE(wxID_ANY
, wxGridHeaderCtrl::OnBeginResize
)
316 EVT_HEADER_RESIZING(wxID_ANY
, wxGridHeaderCtrl::OnResizing
)
317 EVT_HEADER_END_RESIZE(wxID_ANY
, wxGridHeaderCtrl::OnEndResize
)
319 EVT_HEADER_BEGIN_REORDER(wxID_ANY
, wxGridHeaderCtrl::OnBeginReorder
)
320 EVT_HEADER_END_REORDER(wxID_ANY
, wxGridHeaderCtrl::OnEndReorder
)
323 // common base class for various grid subwindows
324 class WXDLLIMPEXP_ADV wxGridSubwindow
: public wxWindow
327 wxGridSubwindow(wxGrid
*owner
,
328 int additionalStyle
= 0,
329 const wxString
& name
= wxPanelNameStr
)
330 : wxWindow(owner
, wxID_ANY
,
331 wxDefaultPosition
, wxDefaultSize
,
332 wxBORDER_NONE
| additionalStyle
,
338 virtual bool AcceptsFocus() const { return false; }
340 wxGrid
*GetOwner() { return m_owner
; }
343 void OnMouseCaptureLost(wxMouseCaptureLostEvent
& event
);
347 DECLARE_EVENT_TABLE()
348 DECLARE_NO_COPY_CLASS(wxGridSubwindow
)
351 class WXDLLIMPEXP_ADV wxGridRowLabelWindow
: public wxGridSubwindow
354 wxGridRowLabelWindow(wxGrid
*parent
)
355 : wxGridSubwindow(parent
)
361 void OnPaint( wxPaintEvent
& event
);
362 void OnMouseEvent( wxMouseEvent
& event
);
363 void OnMouseWheel( wxMouseEvent
& event
);
365 DECLARE_EVENT_TABLE()
366 DECLARE_NO_COPY_CLASS(wxGridRowLabelWindow
)
370 class WXDLLIMPEXP_ADV wxGridColLabelWindow
: public wxGridSubwindow
373 wxGridColLabelWindow(wxGrid
*parent
)
374 : wxGridSubwindow(parent
)
380 void OnPaint( wxPaintEvent
& event
);
381 void OnMouseEvent( wxMouseEvent
& event
);
382 void OnMouseWheel( wxMouseEvent
& event
);
384 DECLARE_EVENT_TABLE()
385 DECLARE_NO_COPY_CLASS(wxGridColLabelWindow
)
389 class WXDLLIMPEXP_ADV wxGridCornerLabelWindow
: public wxGridSubwindow
392 wxGridCornerLabelWindow(wxGrid
*parent
)
393 : wxGridSubwindow(parent
)
398 void OnMouseEvent( wxMouseEvent
& event
);
399 void OnMouseWheel( wxMouseEvent
& event
);
400 void OnPaint( wxPaintEvent
& event
);
402 DECLARE_EVENT_TABLE()
403 DECLARE_NO_COPY_CLASS(wxGridCornerLabelWindow
)
406 class WXDLLIMPEXP_ADV wxGridWindow
: public wxGridSubwindow
409 wxGridWindow(wxGrid
*parent
)
410 : wxGridSubwindow(parent
,
411 wxWANTS_CHARS
| wxCLIP_CHILDREN
,
417 virtual void ScrollWindow( int dx
, int dy
, const wxRect
*rect
);
419 virtual bool AcceptsFocus() const { return true; }
422 void OnPaint( wxPaintEvent
&event
);
423 void OnMouseWheel( wxMouseEvent
& event
);
424 void OnMouseEvent( wxMouseEvent
& event
);
425 void OnKeyDown( wxKeyEvent
& );
426 void OnKeyUp( wxKeyEvent
& );
427 void OnChar( wxKeyEvent
& );
428 void OnEraseBackground( wxEraseEvent
& );
429 void OnFocus( wxFocusEvent
& );
431 DECLARE_EVENT_TABLE()
432 DECLARE_NO_COPY_CLASS(wxGridWindow
)
436 class wxGridCellEditorEvtHandler
: public wxEvtHandler
439 wxGridCellEditorEvtHandler(wxGrid
* grid
, wxGridCellEditor
* editor
)
446 void OnKillFocus(wxFocusEvent
& event
);
447 void OnKeyDown(wxKeyEvent
& event
);
448 void OnChar(wxKeyEvent
& event
);
450 void SetInSetFocus(bool inSetFocus
) { m_inSetFocus
= inSetFocus
; }
454 wxGridCellEditor
*m_editor
;
456 // Work around the fact that a focus kill event can be sent to
457 // a combobox within a set focus event.
460 DECLARE_EVENT_TABLE()
461 DECLARE_DYNAMIC_CLASS(wxGridCellEditorEvtHandler
)
462 DECLARE_NO_COPY_CLASS(wxGridCellEditorEvtHandler
)
466 IMPLEMENT_ABSTRACT_CLASS(wxGridCellEditorEvtHandler
, wxEvtHandler
)
468 BEGIN_EVENT_TABLE( wxGridCellEditorEvtHandler
, wxEvtHandler
)
469 EVT_KILL_FOCUS( wxGridCellEditorEvtHandler::OnKillFocus
)
470 EVT_KEY_DOWN( wxGridCellEditorEvtHandler::OnKeyDown
)
471 EVT_CHAR( wxGridCellEditorEvtHandler::OnChar
)
475 // ----------------------------------------------------------------------------
476 // the internal data representation used by wxGridCellAttrProvider
477 // ----------------------------------------------------------------------------
479 // this class stores attributes set for cells
480 class WXDLLIMPEXP_ADV wxGridCellAttrData
483 void SetAttr(wxGridCellAttr
*attr
, int row
, int col
);
484 wxGridCellAttr
*GetAttr(int row
, int col
) const;
485 void UpdateAttrRows( size_t pos
, int numRows
);
486 void UpdateAttrCols( size_t pos
, int numCols
);
489 // searches for the attr for given cell, returns wxNOT_FOUND if not found
490 int FindIndex(int row
, int col
) const;
492 wxGridCellWithAttrArray m_attrs
;
495 // this class stores attributes set for rows or columns
496 class WXDLLIMPEXP_ADV wxGridRowOrColAttrData
499 // empty ctor to suppress warnings
500 wxGridRowOrColAttrData() {}
501 ~wxGridRowOrColAttrData();
503 void SetAttr(wxGridCellAttr
*attr
, int rowOrCol
);
504 wxGridCellAttr
*GetAttr(int rowOrCol
) const;
505 void UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
);
508 wxArrayInt m_rowsOrCols
;
509 wxArrayAttrs m_attrs
;
512 // NB: this is just a wrapper around 3 objects: one which stores cell
513 // attributes, and 2 others for row/col ones
514 class WXDLLIMPEXP_ADV wxGridCellAttrProviderData
517 wxGridCellAttrData m_cellAttrs
;
518 wxGridRowOrColAttrData m_rowAttrs
,
523 // ----------------------------------------------------------------------------
524 // data structures used for the data type registry
525 // ----------------------------------------------------------------------------
527 struct wxGridDataTypeInfo
529 wxGridDataTypeInfo(const wxString
& typeName
,
530 wxGridCellRenderer
* renderer
,
531 wxGridCellEditor
* editor
)
532 : m_typeName(typeName
), m_renderer(renderer
), m_editor(editor
)
535 ~wxGridDataTypeInfo()
537 wxSafeDecRef(m_renderer
);
538 wxSafeDecRef(m_editor
);
542 wxGridCellRenderer
* m_renderer
;
543 wxGridCellEditor
* m_editor
;
545 DECLARE_NO_COPY_CLASS(wxGridDataTypeInfo
)
549 WX_DEFINE_ARRAY_WITH_DECL_PTR(wxGridDataTypeInfo
*, wxGridDataTypeInfoArray
,
550 class WXDLLIMPEXP_ADV
);
553 class WXDLLIMPEXP_ADV wxGridTypeRegistry
556 wxGridTypeRegistry() {}
557 ~wxGridTypeRegistry();
559 void RegisterDataType(const wxString
& typeName
,
560 wxGridCellRenderer
* renderer
,
561 wxGridCellEditor
* editor
);
563 // find one of already registered data types
564 int FindRegisteredDataType(const wxString
& typeName
);
566 // try to FindRegisteredDataType(), if this fails and typeName is one of
567 // standard typenames, register it and return its index
568 int FindDataType(const wxString
& typeName
);
570 // try to FindDataType(), if it fails see if it is not one of already
571 // registered data types with some params in which case clone the
572 // registered data type and set params for it
573 int FindOrCloneDataType(const wxString
& typeName
);
575 wxGridCellRenderer
* GetRenderer(int index
);
576 wxGridCellEditor
* GetEditor(int index
);
579 wxGridDataTypeInfoArray m_typeinfo
;
582 // ----------------------------------------------------------------------------
583 // operations classes abstracting the difference between operating on rows and
585 // ----------------------------------------------------------------------------
587 // This class allows to write a function only once because by using its methods
588 // it will apply to both columns and rows.
590 // This is an abstract interface definition, the two concrete implementations
591 // below should be used when working with rows and columns respectively.
592 class wxGridOperations
595 // Returns the operations in the other direction, i.e. wxGridRowOperations
596 // if this object is a wxGridColumnOperations and vice versa.
597 virtual wxGridOperations
& Dual() const = 0;
599 // Return the number of rows or columns.
600 virtual int GetNumberOfLines(const wxGrid
*grid
) const = 0;
602 // Return the selection mode which allows selecting rows or columns.
603 virtual wxGrid::wxGridSelectionModes
GetSelectionMode() const = 0;
605 // Make a wxGridCellCoords from the given components: thisDir is row or
606 // column and otherDir is column or row
607 virtual wxGridCellCoords
MakeCoords(int thisDir
, int otherDir
) const = 0;
609 // Calculate the scrolled position of the given abscissa or ordinate.
610 virtual int CalcScrolledPosition(wxGrid
*grid
, int pos
) const = 0;
612 // Selects the horizontal or vertical component from the given object.
613 virtual int Select(const wxGridCellCoords
& coords
) const = 0;
614 virtual int Select(const wxPoint
& pt
) const = 0;
615 virtual int Select(const wxSize
& sz
) const = 0;
616 virtual int Select(const wxRect
& r
) const = 0;
617 virtual int& Select(wxRect
& r
) const = 0;
619 // Returns width or height of the rectangle
620 virtual int& SelectSize(wxRect
& r
) const = 0;
622 // Make a wxSize such that Select() applied to it returns first component
623 virtual wxSize
MakeSize(int first
, int second
) const = 0;
625 // Sets the row or column component of the given cell coordinates
626 virtual void Set(wxGridCellCoords
& coords
, int line
) const = 0;
629 // Draws a line parallel to the row or column, i.e. horizontal or vertical:
630 // pos is the horizontal or vertical position of the line and start and end
631 // are the coordinates of the line extremities in the other direction
633 DrawParallelLine(wxDC
& dc
, int start
, int end
, int pos
) const = 0;
635 // Draw a horizontal or vertical line across the given rectangle
636 // (this is implemented in terms of above and uses Select() to extract
637 // start and end from the given rectangle)
638 void DrawParallelLineInRect(wxDC
& dc
, const wxRect
& rect
, int pos
) const
640 const int posStart
= Select(rect
.GetPosition());
641 DrawParallelLine(dc
, posStart
, posStart
+ Select(rect
.GetSize()), pos
);
645 // Return the index of the row or column at the given pixel coordinate.
647 PosToLine(const wxGrid
*grid
, int pos
, bool clip
= false) const = 0;
649 // Get the top/left position, in pixels, of the given row or column
650 virtual int GetLineStartPos(const wxGrid
*grid
, int line
) const = 0;
652 // Get the bottom/right position, in pixels, of the given row or column
653 virtual int GetLineEndPos(const wxGrid
*grid
, int line
) const = 0;
655 // Get the height/width of the given row/column
656 virtual int GetLineSize(const wxGrid
*grid
, int line
) const = 0;
658 // Get wxGrid::m_rowBottoms/m_colRights array
659 virtual const wxArrayInt
& GetLineEnds(const wxGrid
*grid
) const = 0;
661 // Get default height row height or column width
662 virtual int GetDefaultLineSize(const wxGrid
*grid
) const = 0;
664 // Return the minimal acceptable row height or column width
665 virtual int GetMinimalAcceptableLineSize(const wxGrid
*grid
) const = 0;
667 // Return the minimal row height or column width
668 virtual int GetMinimalLineSize(const wxGrid
*grid
, int line
) const = 0;
670 // Set the row height or column width
671 virtual void SetLineSize(wxGrid
*grid
, int line
, int size
) const = 0;
673 // True if rows/columns can be resized by user
674 virtual bool CanResizeLines(const wxGrid
*grid
) const = 0;
677 // Return the index of the line at the given position
679 // NB: currently this is always identity for the rows as reordering is only
680 // implemented for the lines
681 virtual int GetLineAt(const wxGrid
*grid
, int line
) const = 0;
684 // Get the row or column label window
685 virtual wxWindow
*GetHeaderWindow(wxGrid
*grid
) const = 0;
687 // Get the width or height of the row or column label window
688 virtual int GetHeaderWindowSize(wxGrid
*grid
) const = 0;
691 // This class is never used polymorphically but give it a virtual dtor
692 // anyhow to suppress g++ complaints about it
693 virtual ~wxGridOperations() { }
696 class wxGridRowOperations
: public wxGridOperations
699 virtual wxGridOperations
& Dual() const;
701 virtual int GetNumberOfLines(const wxGrid
*grid
) const
702 { return grid
->GetNumberRows(); }
704 virtual wxGrid::wxGridSelectionModes
GetSelectionMode() const
705 { return wxGrid::wxGridSelectRows
; }
707 virtual wxGridCellCoords
MakeCoords(int thisDir
, int otherDir
) const
708 { return wxGridCellCoords(thisDir
, otherDir
); }
710 virtual int CalcScrolledPosition(wxGrid
*grid
, int pos
) const
711 { return grid
->CalcScrolledPosition(wxPoint(pos
, 0)).x
; }
713 virtual int Select(const wxGridCellCoords
& c
) const { return c
.GetRow(); }
714 virtual int Select(const wxPoint
& pt
) const { return pt
.x
; }
715 virtual int Select(const wxSize
& sz
) const { return sz
.x
; }
716 virtual int Select(const wxRect
& r
) const { return r
.x
; }
717 virtual int& Select(wxRect
& r
) const { return r
.x
; }
718 virtual int& SelectSize(wxRect
& r
) const { return r
.width
; }
719 virtual wxSize
MakeSize(int first
, int second
) const
720 { return wxSize(first
, second
); }
721 virtual void Set(wxGridCellCoords
& coords
, int line
) const
722 { coords
.SetRow(line
); }
724 virtual void DrawParallelLine(wxDC
& dc
, int start
, int end
, int pos
) const
725 { dc
.DrawLine(start
, pos
, end
, pos
); }
727 virtual int PosToLine(const wxGrid
*grid
, int pos
, bool clip
= false) const
728 { return grid
->YToRow(pos
, clip
); }
729 virtual int GetLineStartPos(const wxGrid
*grid
, int line
) const
730 { return grid
->GetRowTop(line
); }
731 virtual int GetLineEndPos(const wxGrid
*grid
, int line
) const
732 { return grid
->GetRowBottom(line
); }
733 virtual int GetLineSize(const wxGrid
*grid
, int line
) const
734 { return grid
->GetRowHeight(line
); }
735 virtual const wxArrayInt
& GetLineEnds(const wxGrid
*grid
) const
736 { return grid
->m_rowBottoms
; }
737 virtual int GetDefaultLineSize(const wxGrid
*grid
) const
738 { return grid
->GetDefaultRowSize(); }
739 virtual int GetMinimalAcceptableLineSize(const wxGrid
*grid
) const
740 { return grid
->GetRowMinimalAcceptableHeight(); }
741 virtual int GetMinimalLineSize(const wxGrid
*grid
, int line
) const
742 { return grid
->GetRowMinimalHeight(line
); }
743 virtual void SetLineSize(wxGrid
*grid
, int line
, int size
) const
744 { grid
->SetRowSize(line
, size
); }
745 virtual bool CanResizeLines(const wxGrid
*grid
) const
746 { return grid
->CanDragRowSize(); }
748 virtual int GetLineAt(const wxGrid
* WXUNUSED(grid
), int line
) const
749 { return line
; } // TODO: implement row reordering
751 virtual wxWindow
*GetHeaderWindow(wxGrid
*grid
) const
752 { return grid
->GetGridRowLabelWindow(); }
753 virtual int GetHeaderWindowSize(wxGrid
*grid
) const
754 { return grid
->GetRowLabelSize(); }
757 class wxGridColumnOperations
: public wxGridOperations
760 virtual wxGridOperations
& Dual() const;
762 virtual int GetNumberOfLines(const wxGrid
*grid
) const
763 { return grid
->GetNumberCols(); }
765 virtual wxGrid::wxGridSelectionModes
GetSelectionMode() const
766 { return wxGrid::wxGridSelectColumns
; }
768 virtual wxGridCellCoords
MakeCoords(int thisDir
, int otherDir
) const
769 { return wxGridCellCoords(otherDir
, thisDir
); }
771 virtual int CalcScrolledPosition(wxGrid
*grid
, int pos
) const
772 { return grid
->CalcScrolledPosition(wxPoint(0, pos
)).y
; }
774 virtual int Select(const wxGridCellCoords
& c
) const { return c
.GetCol(); }
775 virtual int Select(const wxPoint
& pt
) const { return pt
.y
; }
776 virtual int Select(const wxSize
& sz
) const { return sz
.y
; }
777 virtual int Select(const wxRect
& r
) const { return r
.y
; }
778 virtual int& Select(wxRect
& r
) const { return r
.y
; }
779 virtual int& SelectSize(wxRect
& r
) const { return r
.height
; }
780 virtual wxSize
MakeSize(int first
, int second
) const
781 { return wxSize(second
, first
); }
782 virtual void Set(wxGridCellCoords
& coords
, int line
) const
783 { coords
.SetCol(line
); }
785 virtual void DrawParallelLine(wxDC
& dc
, int start
, int end
, int pos
) const
786 { dc
.DrawLine(pos
, start
, pos
, end
); }
788 virtual int PosToLine(const wxGrid
*grid
, int pos
, bool clip
= false) const
789 { return grid
->XToCol(pos
, clip
); }
790 virtual int GetLineStartPos(const wxGrid
*grid
, int line
) const
791 { return grid
->GetColLeft(line
); }
792 virtual int GetLineEndPos(const wxGrid
*grid
, int line
) const
793 { return grid
->GetColRight(line
); }
794 virtual int GetLineSize(const wxGrid
*grid
, int line
) const
795 { return grid
->GetColWidth(line
); }
796 virtual const wxArrayInt
& GetLineEnds(const wxGrid
*grid
) const
797 { return grid
->m_colRights
; }
798 virtual int GetDefaultLineSize(const wxGrid
*grid
) const
799 { return grid
->GetDefaultColSize(); }
800 virtual int GetMinimalAcceptableLineSize(const wxGrid
*grid
) const
801 { return grid
->GetColMinimalAcceptableWidth(); }
802 virtual int GetMinimalLineSize(const wxGrid
*grid
, int line
) const
803 { return grid
->GetColMinimalWidth(line
); }
804 virtual void SetLineSize(wxGrid
*grid
, int line
, int size
) const
805 { grid
->SetColSize(line
, size
); }
806 virtual bool CanResizeLines(const wxGrid
*grid
) const
807 { return grid
->CanDragColSize(); }
809 virtual int GetLineAt(const wxGrid
*grid
, int line
) const
810 { return grid
->GetColAt(line
); }
812 virtual wxWindow
*GetHeaderWindow(wxGrid
*grid
) const
813 { return grid
->GetGridColLabelWindow(); }
814 virtual int GetHeaderWindowSize(wxGrid
*grid
) const
815 { return grid
->GetColLabelSize(); }
818 wxGridOperations
& wxGridRowOperations::Dual() const
820 static wxGridColumnOperations s_colOper
;
825 wxGridOperations
& wxGridColumnOperations::Dual() const
827 static wxGridRowOperations s_rowOper
;
832 // This class abstracts the difference between operations going forward
833 // (down/right) and backward (up/left) and allows to use the same code for
834 // functions which differ only in the direction of grid traversal
836 // Like wxGridOperations it's an ABC with two concrete subclasses below. Unlike
837 // it, this is a normal object and not just a function dispatch table and has a
840 // Note: the explanation of this discrepancy is the existence of (very useful)
841 // Dual() method in wxGridOperations which forces us to make wxGridOperations a
842 // function dispatcher only.
843 class wxGridDirectionOperations
846 // The oper parameter to ctor selects whether we work with rows or columns
847 wxGridDirectionOperations(wxGrid
*grid
, const wxGridOperations
& oper
)
853 // Check if the component of this point in our direction is at the
854 // boundary, i.e. is the first/last row/column
855 virtual bool IsAtBoundary(const wxGridCellCoords
& coords
) const = 0;
857 // Increment the component of this point in our direction
858 virtual void Advance(wxGridCellCoords
& coords
) const = 0;
860 // Find the line at the given distance, in pixels, away from this one
861 // (this uses clipping, i.e. anything after the last line is counted as the
862 // last one and anything before the first one as 0)
863 virtual int MoveByPixelDistance(int line
, int distance
) const = 0;
865 // This class is never used polymorphically but give it a virtual dtor
866 // anyhow to suppress g++ complaints about it
867 virtual ~wxGridDirectionOperations() { }
870 wxGrid
* const m_grid
;
871 const wxGridOperations
& m_oper
;
874 class wxGridBackwardOperations
: public wxGridDirectionOperations
877 wxGridBackwardOperations(wxGrid
*grid
, const wxGridOperations
& oper
)
878 : wxGridDirectionOperations(grid
, oper
)
882 virtual bool IsAtBoundary(const wxGridCellCoords
& coords
) const
884 wxASSERT_MSG( m_oper
.Select(coords
) >= 0, "invalid row/column" );
886 return m_oper
.Select(coords
) == 0;
889 virtual void Advance(wxGridCellCoords
& coords
) const
891 wxASSERT( !IsAtBoundary(coords
) );
893 m_oper
.Set(coords
, m_oper
.Select(coords
) - 1);
896 virtual int MoveByPixelDistance(int line
, int distance
) const
898 int pos
= m_oper
.GetLineStartPos(m_grid
, line
);
899 return m_oper
.PosToLine(m_grid
, pos
- distance
+ 1, true);
903 class wxGridForwardOperations
: public wxGridDirectionOperations
906 wxGridForwardOperations(wxGrid
*grid
, const wxGridOperations
& oper
)
907 : wxGridDirectionOperations(grid
, oper
),
908 m_numLines(oper
.GetNumberOfLines(grid
))
912 virtual bool IsAtBoundary(const wxGridCellCoords
& coords
) const
914 wxASSERT_MSG( m_oper
.Select(coords
) < m_numLines
, "invalid row/column" );
916 return m_oper
.Select(coords
) == m_numLines
- 1;
919 virtual void Advance(wxGridCellCoords
& coords
) const
921 wxASSERT( !IsAtBoundary(coords
) );
923 m_oper
.Set(coords
, m_oper
.Select(coords
) + 1);
926 virtual int MoveByPixelDistance(int line
, int distance
) const
928 int pos
= m_oper
.GetLineStartPos(m_grid
, line
);
929 return m_oper
.PosToLine(m_grid
, pos
+ distance
, true);
933 const int m_numLines
;
936 // ----------------------------------------------------------------------------
938 // ----------------------------------------------------------------------------
940 //#define DEBUG_ATTR_CACHE
941 #ifdef DEBUG_ATTR_CACHE
942 static size_t gs_nAttrCacheHits
= 0;
943 static size_t gs_nAttrCacheMisses
= 0;
946 // ----------------------------------------------------------------------------
948 // ----------------------------------------------------------------------------
950 wxGridCellCoords
wxGridNoCellCoords( -1, -1 );
951 wxRect
wxGridNoCellRect( -1, -1, -1, -1 );
957 const size_t GRID_SCROLL_LINE_X
= 15;
958 const size_t GRID_SCROLL_LINE_Y
= GRID_SCROLL_LINE_X
;
960 // the size of hash tables used a bit everywhere (the max number of elements
961 // in these hash tables is the number of rows/columns)
962 const int GRID_HASH_SIZE
= 100;
964 // the minimal distance in pixels the mouse needs to move to start a drag
966 const int DRAG_SENSITIVITY
= 3;
968 } // anonymous namespace
970 // ----------------------------------------------------------------------------
972 // ----------------------------------------------------------------------------
977 // ensure that first is less or equal to second, swapping the values if
979 void EnsureFirstLessThanSecond(int& first
, int& second
)
981 if ( first
> second
)
982 wxSwap(first
, second
);
985 } // anonymous namespace
987 // ============================================================================
989 // ============================================================================
991 // ----------------------------------------------------------------------------
993 // ----------------------------------------------------------------------------
995 wxGridCellEditor::wxGridCellEditor()
1001 wxGridCellEditor::~wxGridCellEditor()
1006 void wxGridCellEditor::Create(wxWindow
* WXUNUSED(parent
),
1007 wxWindowID
WXUNUSED(id
),
1008 wxEvtHandler
* evtHandler
)
1011 m_control
->PushEventHandler(evtHandler
);
1014 void wxGridCellEditor::PaintBackground(const wxRect
& rectCell
,
1015 wxGridCellAttr
*attr
)
1017 // erase the background because we might not fill the cell
1018 wxClientDC
dc(m_control
->GetParent());
1019 wxGridWindow
* gridWindow
= wxDynamicCast(m_control
->GetParent(), wxGridWindow
);
1021 gridWindow
->GetOwner()->PrepareDC(dc
);
1023 dc
.SetPen(*wxTRANSPARENT_PEN
);
1024 dc
.SetBrush(wxBrush(attr
->GetBackgroundColour()));
1025 dc
.DrawRectangle(rectCell
);
1027 // redraw the control we just painted over
1028 m_control
->Refresh();
1031 void wxGridCellEditor::Destroy()
1035 m_control
->PopEventHandler( true /* delete it*/ );
1037 m_control
->Destroy();
1042 void wxGridCellEditor::Show(bool show
, wxGridCellAttr
*attr
)
1044 wxASSERT_MSG(m_control
, wxT("The wxGridCellEditor must be created first!"));
1046 m_control
->Show(show
);
1050 // set the colours/fonts if we have any
1053 m_colFgOld
= m_control
->GetForegroundColour();
1054 m_control
->SetForegroundColour(attr
->GetTextColour());
1056 m_colBgOld
= m_control
->GetBackgroundColour();
1057 m_control
->SetBackgroundColour(attr
->GetBackgroundColour());
1059 // Workaround for GTK+1 font setting problem on some platforms
1060 #if !defined(__WXGTK__) || defined(__WXGTK20__)
1061 m_fontOld
= m_control
->GetFont();
1062 m_control
->SetFont(attr
->GetFont());
1065 // can't do anything more in the base class version, the other
1066 // attributes may only be used by the derived classes
1071 // restore the standard colours fonts
1072 if ( m_colFgOld
.Ok() )
1074 m_control
->SetForegroundColour(m_colFgOld
);
1075 m_colFgOld
= wxNullColour
;
1078 if ( m_colBgOld
.Ok() )
1080 m_control
->SetBackgroundColour(m_colBgOld
);
1081 m_colBgOld
= wxNullColour
;
1084 // Workaround for GTK+1 font setting problem on some platforms
1085 #if !defined(__WXGTK__) || defined(__WXGTK20__)
1086 if ( m_fontOld
.Ok() )
1088 m_control
->SetFont(m_fontOld
);
1089 m_fontOld
= wxNullFont
;
1095 void wxGridCellEditor::SetSize(const wxRect
& rect
)
1097 wxASSERT_MSG(m_control
, wxT("The wxGridCellEditor must be created first!"));
1099 m_control
->SetSize(rect
, wxSIZE_ALLOW_MINUS_ONE
);
1102 void wxGridCellEditor::HandleReturn(wxKeyEvent
& event
)
1107 bool wxGridCellEditor::IsAcceptedKey(wxKeyEvent
& event
)
1109 bool ctrl
= event
.ControlDown();
1110 bool alt
= event
.AltDown();
1113 // On the Mac the Alt key is more like shift and is used for entry of
1114 // valid characters, so check for Ctrl and Meta instead.
1115 alt
= event
.MetaDown();
1118 // Assume it's not a valid char if ctrl or alt is down, but if both are
1119 // down then it may be because of an AltGr key combination, so let them
1120 // through in that case.
1121 if ((ctrl
|| alt
) && !(ctrl
&& alt
))
1125 // if the unicode key code is not really a unicode character (it may
1126 // be a function key or etc., the platforms appear to always give us a
1127 // small value in this case) then fallback to the ASCII key code but
1128 // don't do anything for function keys or etc.
1129 if ( event
.GetUnicodeKey() > 127 && event
.GetKeyCode() > 127 )
1132 if ( event
.GetKeyCode() > 255 )
1139 void wxGridCellEditor::StartingKey(wxKeyEvent
& event
)
1144 void wxGridCellEditor::StartingClick()
1150 // ----------------------------------------------------------------------------
1151 // wxGridCellTextEditor
1152 // ----------------------------------------------------------------------------
1154 wxGridCellTextEditor::wxGridCellTextEditor()
1159 void wxGridCellTextEditor::Create(wxWindow
* parent
,
1161 wxEvtHandler
* evtHandler
)
1163 DoCreate(parent
, id
, evtHandler
);
1166 void wxGridCellTextEditor::DoCreate(wxWindow
* parent
,
1168 wxEvtHandler
* evtHandler
,
1171 style
|= wxTE_PROCESS_ENTER
| wxTE_PROCESS_TAB
| wxNO_BORDER
;
1173 m_control
= new wxTextCtrl(parent
, id
, wxEmptyString
,
1174 wxDefaultPosition
, wxDefaultSize
,
1177 // set max length allowed in the textctrl, if the parameter was set
1178 if ( m_maxChars
!= 0 )
1180 Text()->SetMaxLength(m_maxChars
);
1183 wxGridCellEditor::Create(parent
, id
, evtHandler
);
1186 void wxGridCellTextEditor::PaintBackground(const wxRect
& WXUNUSED(rectCell
),
1187 wxGridCellAttr
* WXUNUSED(attr
))
1189 // as we fill the entire client area,
1190 // don't do anything here to minimize flicker
1193 void wxGridCellTextEditor::SetSize(const wxRect
& rectOrig
)
1195 wxRect
rect(rectOrig
);
1197 // Make the edit control large enough to allow for internal margins
1199 // TODO: remove this if the text ctrl sizing is improved esp. for unix
1201 #if defined(__WXGTK__)
1209 #elif defined(__WXMSW__)
1223 int extra_x
= ( rect
.x
> 2 ) ? 2 : 1;
1224 int extra_y
= ( rect
.y
> 2 ) ? 2 : 1;
1226 #if defined(__WXMOTIF__)
1231 rect
.SetLeft( wxMax(0, rect
.x
- extra_x
) );
1232 rect
.SetTop( wxMax(0, rect
.y
- extra_y
) );
1233 rect
.SetRight( rect
.GetRight() + 2 * extra_x
);
1234 rect
.SetBottom( rect
.GetBottom() + 2 * extra_y
);
1237 wxGridCellEditor::SetSize(rect
);
1240 void wxGridCellTextEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1242 wxASSERT_MSG(m_control
, wxT("The wxGridCellEditor must be created first!"));
1244 m_startValue
= grid
->GetTable()->GetValue(row
, col
);
1246 DoBeginEdit(m_startValue
);
1249 void wxGridCellTextEditor::DoBeginEdit(const wxString
& startValue
)
1251 Text()->SetValue(startValue
);
1252 Text()->SetInsertionPointEnd();
1253 Text()->SetSelection(-1, -1);
1257 bool wxGridCellTextEditor::EndEdit(int row
, int col
, wxGrid
* grid
)
1259 wxASSERT_MSG(m_control
, wxT("The wxGridCellEditor must be created first!"));
1261 bool changed
= false;
1262 wxString value
= Text()->GetValue();
1263 if (value
!= m_startValue
)
1267 grid
->GetTable()->SetValue(row
, col
, value
);
1269 m_startValue
= wxEmptyString
;
1271 // No point in setting the text of the hidden control
1272 //Text()->SetValue(m_startValue);
1277 void wxGridCellTextEditor::Reset()
1279 wxASSERT_MSG(m_control
, wxT("The wxGridCellEditor must be created first!"));
1281 DoReset(m_startValue
);
1284 void wxGridCellTextEditor::DoReset(const wxString
& startValue
)
1286 Text()->SetValue(startValue
);
1287 Text()->SetInsertionPointEnd();
1290 bool wxGridCellTextEditor::IsAcceptedKey(wxKeyEvent
& event
)
1292 return wxGridCellEditor::IsAcceptedKey(event
);
1295 void wxGridCellTextEditor::StartingKey(wxKeyEvent
& event
)
1297 // Since this is now happening in the EVT_CHAR event EmulateKeyPress is no
1298 // longer an appropriate way to get the character into the text control.
1299 // Do it ourselves instead. We know that if we get this far that we have
1300 // a valid character, so not a whole lot of testing needs to be done.
1302 wxTextCtrl
* tc
= Text();
1307 ch
= event
.GetUnicodeKey();
1309 ch
= (wxChar
)event
.GetKeyCode();
1311 ch
= (wxChar
)event
.GetKeyCode();
1317 // delete the character at the cursor
1318 pos
= tc
->GetInsertionPoint();
1319 if (pos
< tc
->GetLastPosition())
1320 tc
->Remove(pos
, pos
+ 1);
1324 // delete the character before the cursor
1325 pos
= tc
->GetInsertionPoint();
1327 tc
->Remove(pos
- 1, pos
);
1336 void wxGridCellTextEditor::HandleReturn( wxKeyEvent
&
1337 WXUNUSED_GTK(WXUNUSED_MOTIF(event
)) )
1339 #if defined(__WXMOTIF__) || defined(__WXGTK__)
1340 // wxMotif needs a little extra help...
1341 size_t pos
= (size_t)( Text()->GetInsertionPoint() );
1342 wxString
s( Text()->GetValue() );
1343 s
= s
.Left(pos
) + wxT("\n") + s
.Mid(pos
);
1344 Text()->SetValue(s
);
1345 Text()->SetInsertionPoint( pos
);
1347 // the other ports can handle a Return key press
1353 void wxGridCellTextEditor::SetParameters(const wxString
& params
)
1363 if ( params
.ToLong(&tmp
) )
1365 m_maxChars
= (size_t)tmp
;
1369 wxLogDebug( _T("Invalid wxGridCellTextEditor parameter string '%s' ignored"), params
.c_str() );
1374 // return the value in the text control
1375 wxString
wxGridCellTextEditor::GetValue() const
1377 return Text()->GetValue();
1380 // ----------------------------------------------------------------------------
1381 // wxGridCellNumberEditor
1382 // ----------------------------------------------------------------------------
1384 wxGridCellNumberEditor::wxGridCellNumberEditor(int min
, int max
)
1390 void wxGridCellNumberEditor::Create(wxWindow
* parent
,
1392 wxEvtHandler
* evtHandler
)
1397 // create a spin ctrl
1398 m_control
= new wxSpinCtrl(parent
, wxID_ANY
, wxEmptyString
,
1399 wxDefaultPosition
, wxDefaultSize
,
1403 wxGridCellEditor::Create(parent
, id
, evtHandler
);
1408 // just a text control
1409 wxGridCellTextEditor::Create(parent
, id
, evtHandler
);
1411 #if wxUSE_VALIDATORS
1412 Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC
));
1417 void wxGridCellNumberEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1419 // first get the value
1420 wxGridTableBase
*table
= grid
->GetTable();
1421 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
1423 m_valueOld
= table
->GetValueAsLong(row
, col
);
1428 wxString sValue
= table
->GetValue(row
, col
);
1429 if (! sValue
.ToLong(&m_valueOld
) && ! sValue
.empty())
1431 wxFAIL_MSG( _T("this cell doesn't have numeric value") );
1439 Spin()->SetValue((int)m_valueOld
);
1445 DoBeginEdit(GetString());
1449 bool wxGridCellNumberEditor::EndEdit(int row
, int col
,
1458 value
= Spin()->GetValue();
1459 if ( value
== m_valueOld
)
1462 text
.Printf(wxT("%ld"), value
);
1464 else // using unconstrained input
1465 #endif // wxUSE_SPINCTRL
1467 const wxString
textOld(grid
->GetCellValue(row
, col
));
1468 text
= Text()->GetValue();
1471 if ( textOld
.empty() )
1474 else // non-empty text now (maybe 0)
1476 if ( !text
.ToLong(&value
) )
1479 // if value == m_valueOld == 0 but old text was "" and new one is
1480 // "0" something still did change
1481 if ( value
== m_valueOld
&& (value
|| !textOld
.empty()) )
1486 wxGridTableBase
* const table
= grid
->GetTable();
1487 if ( table
->CanSetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
1488 table
->SetValueAsLong(row
, col
, value
);
1490 table
->SetValue(row
, col
, text
);
1495 void wxGridCellNumberEditor::Reset()
1500 Spin()->SetValue((int)m_valueOld
);
1505 DoReset(GetString());
1509 bool wxGridCellNumberEditor::IsAcceptedKey(wxKeyEvent
& event
)
1511 if ( wxGridCellEditor::IsAcceptedKey(event
) )
1513 int keycode
= event
.GetKeyCode();
1514 if ( (keycode
< 128) &&
1515 (wxIsdigit(keycode
) || keycode
== '+' || keycode
== '-'))
1524 void wxGridCellNumberEditor::StartingKey(wxKeyEvent
& event
)
1526 int keycode
= event
.GetKeyCode();
1529 if ( wxIsdigit(keycode
) || keycode
== '+' || keycode
== '-')
1531 wxGridCellTextEditor::StartingKey(event
);
1533 // skip Skip() below
1540 if ( wxIsdigit(keycode
) )
1542 wxSpinCtrl
* spin
= (wxSpinCtrl
*)m_control
;
1543 spin
->SetValue(keycode
- '0');
1544 spin
->SetSelection(1,1);
1553 void wxGridCellNumberEditor::SetParameters(const wxString
& params
)
1564 if ( params
.BeforeFirst(_T(',')).ToLong(&tmp
) )
1568 if ( params
.AfterFirst(_T(',')).ToLong(&tmp
) )
1572 // skip the error message below
1577 wxLogDebug(_T("Invalid wxGridCellNumberEditor parameter string '%s' ignored"), params
.c_str());
1581 // return the value in the spin control if it is there (the text control otherwise)
1582 wxString
wxGridCellNumberEditor::GetValue() const
1589 long value
= Spin()->GetValue();
1590 s
.Printf(wxT("%ld"), value
);
1595 s
= Text()->GetValue();
1601 // ----------------------------------------------------------------------------
1602 // wxGridCellFloatEditor
1603 // ----------------------------------------------------------------------------
1605 wxGridCellFloatEditor::wxGridCellFloatEditor(int width
, int precision
)
1608 m_precision
= precision
;
1611 void wxGridCellFloatEditor::Create(wxWindow
* parent
,
1613 wxEvtHandler
* evtHandler
)
1615 wxGridCellTextEditor::Create(parent
, id
, evtHandler
);
1617 #if wxUSE_VALIDATORS
1618 Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC
));
1622 void wxGridCellFloatEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1624 // first get the value
1625 wxGridTableBase
* const table
= grid
->GetTable();
1626 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
1628 m_valueOld
= table
->GetValueAsDouble(row
, col
);
1634 const wxString value
= table
->GetValue(row
, col
);
1635 if ( !value
.empty() )
1637 if ( !value
.ToDouble(&m_valueOld
) )
1639 wxFAIL_MSG( _T("this cell doesn't have float value") );
1645 DoBeginEdit(GetString());
1648 bool wxGridCellFloatEditor::EndEdit(int row
, int col
, wxGrid
* grid
)
1650 const wxString
text(Text()->GetValue()),
1651 textOld(grid
->GetCellValue(row
, col
));
1654 if ( !text
.empty() )
1656 if ( !text
.ToDouble(&value
) )
1659 else // new value is empty string
1661 if ( textOld
.empty() )
1662 return false; // nothing changed
1667 // the test for empty strings ensures that we don't skip the value setting
1668 // when "" is replaced by "0" or vice versa as "" numeric value is also 0.
1669 if ( wxIsSameDouble(value
, m_valueOld
) && !text
.empty() && !textOld
.empty() )
1670 return false; // nothing changed
1672 wxGridTableBase
* const table
= grid
->GetTable();
1674 if ( table
->CanSetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
1675 table
->SetValueAsDouble(row
, col
, value
);
1677 table
->SetValue(row
, col
, text
);
1682 void wxGridCellFloatEditor::Reset()
1684 DoReset(GetString());
1687 void wxGridCellFloatEditor::StartingKey(wxKeyEvent
& event
)
1689 int keycode
= event
.GetKeyCode();
1691 tmpbuf
[0] = (char) keycode
;
1693 wxString
strbuf(tmpbuf
, *wxConvCurrent
);
1696 bool is_decimal_point
= ( strbuf
==
1697 wxLocale::GetInfo(wxLOCALE_DECIMAL_POINT
, wxLOCALE_CAT_NUMBER
) );
1699 bool is_decimal_point
= ( strbuf
== _T(".") );
1702 if ( wxIsdigit(keycode
) || keycode
== '+' || keycode
== '-'
1703 || is_decimal_point
)
1705 wxGridCellTextEditor::StartingKey(event
);
1707 // skip Skip() below
1714 void wxGridCellFloatEditor::SetParameters(const wxString
& params
)
1725 if ( params
.BeforeFirst(_T(',')).ToLong(&tmp
) )
1729 if ( params
.AfterFirst(_T(',')).ToLong(&tmp
) )
1731 m_precision
= (int)tmp
;
1733 // skip the error message below
1738 wxLogDebug(_T("Invalid wxGridCellFloatEditor parameter string '%s' ignored"), params
.c_str());
1742 wxString
wxGridCellFloatEditor::GetString() const
1745 if ( m_precision
== -1 && m_width
!= -1)
1747 // default precision
1748 fmt
.Printf(_T("%%%d.f"), m_width
);
1750 else if ( m_precision
!= -1 && m_width
== -1)
1753 fmt
.Printf(_T("%%.%df"), m_precision
);
1755 else if ( m_precision
!= -1 && m_width
!= -1 )
1757 fmt
.Printf(_T("%%%d.%df"), m_width
, m_precision
);
1761 // default width/precision
1765 return wxString::Format(fmt
, m_valueOld
);
1768 bool wxGridCellFloatEditor::IsAcceptedKey(wxKeyEvent
& event
)
1770 if ( wxGridCellEditor::IsAcceptedKey(event
) )
1772 const int keycode
= event
.GetKeyCode();
1773 if ( isascii(keycode
) )
1776 tmpbuf
[0] = (char) keycode
;
1778 wxString
strbuf(tmpbuf
, *wxConvCurrent
);
1781 const wxString decimalPoint
=
1782 wxLocale::GetInfo(wxLOCALE_DECIMAL_POINT
, wxLOCALE_CAT_NUMBER
);
1784 const wxString
decimalPoint(_T('.'));
1787 // accept digits, 'e' as in '1e+6', also '-', '+', and '.'
1788 if ( wxIsdigit(keycode
) ||
1789 tolower(keycode
) == 'e' ||
1790 keycode
== decimalPoint
||
1802 #endif // wxUSE_TEXTCTRL
1806 // ----------------------------------------------------------------------------
1807 // wxGridCellBoolEditor
1808 // ----------------------------------------------------------------------------
1810 // the default values for GetValue()
1811 wxString
wxGridCellBoolEditor::ms_stringValues
[2] = { _T(""), _T("1") };
1813 void wxGridCellBoolEditor::Create(wxWindow
* parent
,
1815 wxEvtHandler
* evtHandler
)
1817 m_control
= new wxCheckBox(parent
, id
, wxEmptyString
,
1818 wxDefaultPosition
, wxDefaultSize
,
1821 wxGridCellEditor::Create(parent
, id
, evtHandler
);
1824 void wxGridCellBoolEditor::SetSize(const wxRect
& r
)
1826 bool resize
= false;
1827 wxSize size
= m_control
->GetSize();
1828 wxCoord minSize
= wxMin(r
.width
, r
.height
);
1830 // check if the checkbox is not too big/small for this cell
1831 wxSize sizeBest
= m_control
->GetBestSize();
1832 if ( !(size
== sizeBest
) )
1834 // reset to default size if it had been made smaller
1840 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
1842 // leave 1 pixel margin
1843 size
.x
= size
.y
= minSize
- 2;
1850 m_control
->SetSize(size
);
1853 // position it in the centre of the rectangle (TODO: support alignment?)
1855 #if defined(__WXGTK__) || defined (__WXMOTIF__)
1856 // the checkbox without label still has some space to the right in wxGTK,
1857 // so shift it to the right
1859 #elif defined(__WXMSW__)
1860 // here too, but in other way
1865 int hAlign
= wxALIGN_CENTRE
;
1866 int vAlign
= wxALIGN_CENTRE
;
1868 GetCellAttr()->GetAlignment(& hAlign
, & vAlign
);
1871 if (hAlign
== wxALIGN_LEFT
)
1879 y
= r
.y
+ r
.height
/ 2 - size
.y
/ 2;
1881 else if (hAlign
== wxALIGN_RIGHT
)
1883 x
= r
.x
+ r
.width
- size
.x
- 2;
1884 y
= r
.y
+ r
.height
/ 2 - size
.y
/ 2;
1886 else if (hAlign
== wxALIGN_CENTRE
)
1888 x
= r
.x
+ r
.width
/ 2 - size
.x
/ 2;
1889 y
= r
.y
+ r
.height
/ 2 - size
.y
/ 2;
1892 m_control
->Move(x
, y
);
1895 void wxGridCellBoolEditor::Show(bool show
, wxGridCellAttr
*attr
)
1897 m_control
->Show(show
);
1901 wxColour colBg
= attr
? attr
->GetBackgroundColour() : *wxLIGHT_GREY
;
1902 CBox()->SetBackgroundColour(colBg
);
1906 void wxGridCellBoolEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1908 wxASSERT_MSG(m_control
,
1909 wxT("The wxGridCellEditor must be created first!"));
1911 if (grid
->GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
))
1913 m_startValue
= grid
->GetTable()->GetValueAsBool(row
, col
);
1917 wxString
cellval( grid
->GetTable()->GetValue(row
, col
) );
1919 if ( cellval
== ms_stringValues
[false] )
1920 m_startValue
= false;
1921 else if ( cellval
== ms_stringValues
[true] )
1922 m_startValue
= true;
1925 // do not try to be smart here and convert it to true or false
1926 // because we'll still overwrite it with something different and
1927 // this risks to be very surprising for the user code, let them
1929 wxFAIL_MSG( _T("invalid value for a cell with bool editor!") );
1933 CBox()->SetValue(m_startValue
);
1937 bool wxGridCellBoolEditor::EndEdit(int row
, int col
,
1940 wxASSERT_MSG(m_control
,
1941 wxT("The wxGridCellEditor must be created first!"));
1943 bool changed
= false;
1944 bool value
= CBox()->GetValue();
1945 if ( value
!= m_startValue
)
1950 wxGridTableBase
* const table
= grid
->GetTable();
1951 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
) )
1952 table
->SetValueAsBool(row
, col
, value
);
1954 table
->SetValue(row
, col
, GetValue());
1960 void wxGridCellBoolEditor::Reset()
1962 wxASSERT_MSG(m_control
,
1963 wxT("The wxGridCellEditor must be created first!"));
1965 CBox()->SetValue(m_startValue
);
1968 void wxGridCellBoolEditor::StartingClick()
1970 CBox()->SetValue(!CBox()->GetValue());
1973 bool wxGridCellBoolEditor::IsAcceptedKey(wxKeyEvent
& event
)
1975 if ( wxGridCellEditor::IsAcceptedKey(event
) )
1977 int keycode
= event
.GetKeyCode();
1990 void wxGridCellBoolEditor::StartingKey(wxKeyEvent
& event
)
1992 int keycode
= event
.GetKeyCode();
1996 CBox()->SetValue(!CBox()->GetValue());
2000 CBox()->SetValue(true);
2004 CBox()->SetValue(false);
2009 wxString
wxGridCellBoolEditor::GetValue() const
2011 return ms_stringValues
[CBox()->GetValue()];
2015 wxGridCellBoolEditor::UseStringValues(const wxString
& valueTrue
,
2016 const wxString
& valueFalse
)
2018 ms_stringValues
[false] = valueFalse
;
2019 ms_stringValues
[true] = valueTrue
;
2023 wxGridCellBoolEditor::IsTrueValue(const wxString
& value
)
2025 return value
== ms_stringValues
[true];
2028 #endif // wxUSE_CHECKBOX
2032 // ----------------------------------------------------------------------------
2033 // wxGridCellChoiceEditor
2034 // ----------------------------------------------------------------------------
2036 wxGridCellChoiceEditor::wxGridCellChoiceEditor(const wxArrayString
& choices
,
2038 : m_choices(choices
),
2039 m_allowOthers(allowOthers
) { }
2041 wxGridCellChoiceEditor::wxGridCellChoiceEditor(size_t count
,
2042 const wxString choices
[],
2044 : m_allowOthers(allowOthers
)
2048 m_choices
.Alloc(count
);
2049 for ( size_t n
= 0; n
< count
; n
++ )
2051 m_choices
.Add(choices
[n
]);
2056 wxGridCellEditor
*wxGridCellChoiceEditor::Clone() const
2058 wxGridCellChoiceEditor
*editor
= new wxGridCellChoiceEditor
;
2059 editor
->m_allowOthers
= m_allowOthers
;
2060 editor
->m_choices
= m_choices
;
2065 void wxGridCellChoiceEditor::Create(wxWindow
* parent
,
2067 wxEvtHandler
* evtHandler
)
2069 int style
= wxTE_PROCESS_ENTER
|
2073 if ( !m_allowOthers
)
2074 style
|= wxCB_READONLY
;
2075 m_control
= new wxComboBox(parent
, id
, wxEmptyString
,
2076 wxDefaultPosition
, wxDefaultSize
,
2080 wxGridCellEditor::Create(parent
, id
, evtHandler
);
2083 void wxGridCellChoiceEditor::PaintBackground(const wxRect
& rectCell
,
2084 wxGridCellAttr
* attr
)
2086 // as we fill the entire client area, don't do anything here to minimize
2089 // TODO: It doesn't actually fill the client area since the height of a
2090 // combo always defaults to the standard. Until someone has time to
2091 // figure out the right rectangle to paint, just do it the normal way.
2092 wxGridCellEditor::PaintBackground(rectCell
, attr
);
2095 void wxGridCellChoiceEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
2097 wxASSERT_MSG(m_control
,
2098 wxT("The wxGridCellEditor must be created first!"));
2100 wxGridCellEditorEvtHandler
* evtHandler
= NULL
;
2102 evtHandler
= wxDynamicCast(m_control
->GetEventHandler(), wxGridCellEditorEvtHandler
);
2104 // Don't immediately end if we get a kill focus event within BeginEdit
2106 evtHandler
->SetInSetFocus(true);
2108 m_startValue
= grid
->GetTable()->GetValue(row
, col
);
2110 Reset(); // this updates combo box to correspond to m_startValue
2112 Combo()->SetFocus();
2116 // When dropping down the menu, a kill focus event
2117 // happens after this point, so we can't reset the flag yet.
2118 #if !defined(__WXGTK20__)
2119 evtHandler
->SetInSetFocus(false);
2124 bool wxGridCellChoiceEditor::EndEdit(int row
, int col
,
2127 wxString value
= Combo()->GetValue();
2128 if ( value
== m_startValue
)
2131 grid
->GetTable()->SetValue(row
, col
, value
);
2136 void wxGridCellChoiceEditor::Reset()
2140 Combo()->SetValue(m_startValue
);
2141 Combo()->SetInsertionPointEnd();
2143 else // the combobox is read-only
2145 // find the right position, or default to the first if not found
2146 int pos
= Combo()->FindString(m_startValue
);
2147 if (pos
== wxNOT_FOUND
)
2149 Combo()->SetSelection(pos
);
2153 void wxGridCellChoiceEditor::SetParameters(const wxString
& params
)
2163 wxStringTokenizer
tk(params
, _T(','));
2164 while ( tk
.HasMoreTokens() )
2166 m_choices
.Add(tk
.GetNextToken());
2170 // return the value in the text control
2171 wxString
wxGridCellChoiceEditor::GetValue() const
2173 return Combo()->GetValue();
2176 #endif // wxUSE_COMBOBOX
2178 // ----------------------------------------------------------------------------
2179 // wxGridCellEditorEvtHandler
2180 // ----------------------------------------------------------------------------
2182 void wxGridCellEditorEvtHandler::OnKillFocus(wxFocusEvent
& event
)
2184 // Don't disable the cell if we're just starting to edit it
2189 m_grid
->DisableCellEditControl();
2194 void wxGridCellEditorEvtHandler::OnKeyDown(wxKeyEvent
& event
)
2196 switch ( event
.GetKeyCode() )
2200 m_grid
->DisableCellEditControl();
2204 m_grid
->GetEventHandler()->ProcessEvent( event
);
2208 case WXK_NUMPAD_ENTER
:
2209 if (!m_grid
->GetEventHandler()->ProcessEvent(event
))
2210 m_editor
->HandleReturn(event
);
2219 void wxGridCellEditorEvtHandler::OnChar(wxKeyEvent
& event
)
2221 int row
= m_grid
->GetGridCursorRow();
2222 int col
= m_grid
->GetGridCursorCol();
2223 wxRect rect
= m_grid
->CellToRect( row
, col
);
2225 m_grid
->GetGridWindow()->GetClientSize( &cw
, &ch
);
2227 // if cell width is smaller than grid client area, cell is wholly visible
2228 bool wholeCellVisible
= (rect
.GetWidth() < cw
);
2230 switch ( event
.GetKeyCode() )
2235 case WXK_NUMPAD_ENTER
:
2240 if ( wholeCellVisible
)
2242 // no special processing needed...
2247 // do special processing for partly visible cell...
2249 // get the widths of all cells previous to this one
2251 for ( int i
= 0; i
< col
; i
++ )
2253 colXPos
+= m_grid
->GetColSize(i
);
2256 int xUnit
= 1, yUnit
= 1;
2257 m_grid
->GetScrollPixelsPerUnit(&xUnit
, &yUnit
);
2260 m_grid
->Scroll(colXPos
/ xUnit
- 1, m_grid
->GetScrollPos(wxVERTICAL
));
2264 m_grid
->Scroll(colXPos
/ xUnit
, m_grid
->GetScrollPos(wxVERTICAL
));
2272 if ( wholeCellVisible
)
2274 // no special processing needed...
2279 // do special processing for partly visible cell...
2282 wxString value
= m_grid
->GetCellValue(row
, col
);
2283 if ( wxEmptyString
!= value
)
2285 // get width of cell CONTENTS (text)
2287 wxFont font
= m_grid
->GetCellFont(row
, col
);
2288 m_grid
->GetTextExtent(value
, &textWidth
, &y
, NULL
, NULL
, &font
);
2290 // try to RIGHT align the text by scrolling
2291 int client_right
= m_grid
->GetGridWindow()->GetClientSize().GetWidth();
2293 // (m_grid->GetScrollLineX()*2) is a factor for not scrolling to far,
2294 // otherwise the last part of the cell content might be hidden below the scroll bar
2295 // FIXME: maybe there is a more suitable correction?
2296 textWidth
-= (client_right
- (m_grid
->GetScrollLineX() * 2));
2297 if ( textWidth
< 0 )
2303 // get the widths of all cells previous to this one
2305 for ( int i
= 0; i
< col
; i
++ )
2307 colXPos
+= m_grid
->GetColSize(i
);
2310 // and add the (modified) text width of the cell contents
2311 // as we'd like to see the last part of the cell contents
2312 colXPos
+= textWidth
;
2314 int xUnit
= 1, yUnit
= 1;
2315 m_grid
->GetScrollPixelsPerUnit(&xUnit
, &yUnit
);
2316 m_grid
->Scroll(colXPos
/ xUnit
- 1, m_grid
->GetScrollPos(wxVERTICAL
));
2327 // ----------------------------------------------------------------------------
2328 // wxGridCellWorker is an (almost) empty common base class for
2329 // wxGridCellRenderer and wxGridCellEditor managing ref counting
2330 // ----------------------------------------------------------------------------
2332 void wxGridCellWorker::SetParameters(const wxString
& WXUNUSED(params
))
2337 wxGridCellWorker::~wxGridCellWorker()
2341 // ============================================================================
2343 // ============================================================================
2345 // ----------------------------------------------------------------------------
2346 // wxGridCellRenderer
2347 // ----------------------------------------------------------------------------
2349 void wxGridCellRenderer::Draw(wxGrid
& grid
,
2350 wxGridCellAttr
& attr
,
2353 int WXUNUSED(row
), int WXUNUSED(col
),
2356 dc
.SetBackgroundMode( wxBRUSHSTYLE_SOLID
);
2359 if ( grid
.IsEnabled() )
2363 if ( grid
.HasFocus() )
2364 clr
= grid
.GetSelectionBackground();
2366 clr
= wxSystemSettings::GetColour(wxSYS_COLOUR_BTNSHADOW
);
2370 clr
= attr
.GetBackgroundColour();
2373 else // grey out fields if the grid is disabled
2375 clr
= wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
);
2379 dc
.SetPen( *wxTRANSPARENT_PEN
);
2380 dc
.DrawRectangle(rect
);
2383 // ----------------------------------------------------------------------------
2384 // wxGridCellStringRenderer
2385 // ----------------------------------------------------------------------------
2387 void wxGridCellStringRenderer::SetTextColoursAndFont(const wxGrid
& grid
,
2388 const wxGridCellAttr
& attr
,
2392 dc
.SetBackgroundMode( wxBRUSHSTYLE_TRANSPARENT
);
2394 // TODO some special colours for attr.IsReadOnly() case?
2396 // different coloured text when the grid is disabled
2397 if ( grid
.IsEnabled() )
2402 if ( grid
.HasFocus() )
2403 clr
= grid
.GetSelectionBackground();
2405 clr
= wxSystemSettings::GetColour(wxSYS_COLOUR_BTNSHADOW
);
2406 dc
.SetTextBackground( clr
);
2407 dc
.SetTextForeground( grid
.GetSelectionForeground() );
2411 dc
.SetTextBackground( attr
.GetBackgroundColour() );
2412 dc
.SetTextForeground( attr
.GetTextColour() );
2417 dc
.SetTextBackground(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
));
2418 dc
.SetTextForeground(wxSystemSettings::GetColour(wxSYS_COLOUR_GRAYTEXT
));
2421 dc
.SetFont( attr
.GetFont() );
2424 wxSize
wxGridCellStringRenderer::DoGetBestSize(const wxGridCellAttr
& attr
,
2426 const wxString
& text
)
2428 wxCoord x
= 0, y
= 0, max_x
= 0;
2429 dc
.SetFont(attr
.GetFont());
2430 wxStringTokenizer
tk(text
, _T('\n'));
2431 while ( tk
.HasMoreTokens() )
2433 dc
.GetTextExtent(tk
.GetNextToken(), &x
, &y
);
2434 max_x
= wxMax(max_x
, x
);
2437 y
*= 1 + text
.Freq(wxT('\n')); // multiply by the number of lines.
2439 return wxSize(max_x
, y
);
2442 wxSize
wxGridCellStringRenderer::GetBestSize(wxGrid
& grid
,
2443 wxGridCellAttr
& attr
,
2447 return DoGetBestSize(attr
, dc
, grid
.GetCellValue(row
, col
));
2450 void wxGridCellStringRenderer::Draw(wxGrid
& grid
,
2451 wxGridCellAttr
& attr
,
2453 const wxRect
& rectCell
,
2457 wxRect rect
= rectCell
;
2460 // erase only this cells background, overflow cells should have been erased
2461 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
2464 attr
.GetAlignment(&hAlign
, &vAlign
);
2466 int overflowCols
= 0;
2468 if (attr
.GetOverflow())
2470 int cols
= grid
.GetNumberCols();
2471 int best_width
= GetBestSize(grid
,attr
,dc
,row
,col
).GetWidth();
2472 int cell_rows
, cell_cols
;
2473 attr
.GetSize( &cell_rows
, &cell_cols
); // shouldn't get here if <= 0
2474 if ((best_width
> rectCell
.width
) && (col
< cols
) && grid
.GetTable())
2476 int i
, c_cols
, c_rows
;
2477 for (i
= col
+cell_cols
; i
< cols
; i
++)
2479 bool is_empty
= true;
2480 for (int j
=row
; j
< row
+ cell_rows
; j
++)
2482 // check w/ anchor cell for multicell block
2483 grid
.GetCellSize(j
, i
, &c_rows
, &c_cols
);
2486 if (!grid
.GetTable()->IsEmptyCell(j
+ c_rows
, i
))
2495 rect
.width
+= grid
.GetColSize(i
);
2503 if (rect
.width
>= best_width
)
2507 overflowCols
= i
- col
- cell_cols
+ 1;
2508 if (overflowCols
>= cols
)
2509 overflowCols
= cols
- 1;
2512 if (overflowCols
> 0) // redraw overflow cells w/ proper hilight
2514 hAlign
= wxALIGN_LEFT
; // if oveflowed then it's left aligned
2516 clip
.x
+= rectCell
.width
;
2517 // draw each overflow cell individually
2518 int col_end
= col
+ cell_cols
+ overflowCols
;
2519 if (col_end
>= grid
.GetNumberCols())
2520 col_end
= grid
.GetNumberCols() - 1;
2521 for (int i
= col
+ cell_cols
; i
<= col_end
; i
++)
2523 clip
.width
= grid
.GetColSize(i
) - 1;
2524 dc
.DestroyClippingRegion();
2525 dc
.SetClippingRegion(clip
);
2527 SetTextColoursAndFont(grid
, attr
, dc
,
2528 grid
.IsInSelection(row
,i
));
2530 grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
),
2531 rect
, hAlign
, vAlign
);
2532 clip
.x
+= grid
.GetColSize(i
) - 1;
2538 dc
.DestroyClippingRegion();
2542 // now we only have to draw the text
2543 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
2545 grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
),
2546 rect
, hAlign
, vAlign
);
2549 // ----------------------------------------------------------------------------
2550 // wxGridCellNumberRenderer
2551 // ----------------------------------------------------------------------------
2553 wxString
wxGridCellNumberRenderer::GetString(const wxGrid
& grid
, int row
, int col
)
2555 wxGridTableBase
*table
= grid
.GetTable();
2557 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
2559 text
.Printf(_T("%ld"), table
->GetValueAsLong(row
, col
));
2563 text
= table
->GetValue(row
, col
);
2569 void wxGridCellNumberRenderer::Draw(wxGrid
& grid
,
2570 wxGridCellAttr
& attr
,
2572 const wxRect
& rectCell
,
2576 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
2578 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
2580 // draw the text right aligned by default
2582 attr
.GetAlignment(&hAlign
, &vAlign
);
2583 hAlign
= wxALIGN_RIGHT
;
2585 wxRect rect
= rectCell
;
2588 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
2591 wxSize
wxGridCellNumberRenderer::GetBestSize(wxGrid
& grid
,
2592 wxGridCellAttr
& attr
,
2596 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
2599 // ----------------------------------------------------------------------------
2600 // wxGridCellFloatRenderer
2601 // ----------------------------------------------------------------------------
2603 wxGridCellFloatRenderer::wxGridCellFloatRenderer(int width
, int precision
)
2606 SetPrecision(precision
);
2609 wxGridCellRenderer
*wxGridCellFloatRenderer::Clone() const
2611 wxGridCellFloatRenderer
*renderer
= new wxGridCellFloatRenderer
;
2612 renderer
->m_width
= m_width
;
2613 renderer
->m_precision
= m_precision
;
2614 renderer
->m_format
= m_format
;
2619 wxString
wxGridCellFloatRenderer::GetString(const wxGrid
& grid
, int row
, int col
)
2621 wxGridTableBase
*table
= grid
.GetTable();
2626 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
2628 val
= table
->GetValueAsDouble(row
, col
);
2633 text
= table
->GetValue(row
, col
);
2634 hasDouble
= text
.ToDouble(&val
);
2641 if ( m_width
== -1 )
2643 if ( m_precision
== -1 )
2645 // default width/precision
2646 m_format
= _T("%f");
2650 m_format
.Printf(_T("%%.%df"), m_precision
);
2653 else if ( m_precision
== -1 )
2655 // default precision
2656 m_format
.Printf(_T("%%%d.f"), m_width
);
2660 m_format
.Printf(_T("%%%d.%df"), m_width
, m_precision
);
2664 text
.Printf(m_format
, val
);
2667 //else: text already contains the string
2672 void wxGridCellFloatRenderer::Draw(wxGrid
& grid
,
2673 wxGridCellAttr
& attr
,
2675 const wxRect
& rectCell
,
2679 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
2681 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
2683 // draw the text right aligned by default
2685 attr
.GetAlignment(&hAlign
, &vAlign
);
2686 hAlign
= wxALIGN_RIGHT
;
2688 wxRect rect
= rectCell
;
2691 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
2694 wxSize
wxGridCellFloatRenderer::GetBestSize(wxGrid
& grid
,
2695 wxGridCellAttr
& attr
,
2699 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
2702 void wxGridCellFloatRenderer::SetParameters(const wxString
& params
)
2706 // reset to defaults
2712 wxString tmp
= params
.BeforeFirst(_T(','));
2716 if ( tmp
.ToLong(&width
) )
2718 SetWidth((int)width
);
2722 wxLogDebug(_T("Invalid wxGridCellFloatRenderer width parameter string '%s ignored"), params
.c_str());
2726 tmp
= params
.AfterFirst(_T(','));
2730 if ( tmp
.ToLong(&precision
) )
2732 SetPrecision((int)precision
);
2736 wxLogDebug(_T("Invalid wxGridCellFloatRenderer precision parameter string '%s ignored"), params
.c_str());
2742 // ----------------------------------------------------------------------------
2743 // wxGridCellBoolRenderer
2744 // ----------------------------------------------------------------------------
2746 wxSize
wxGridCellBoolRenderer::ms_sizeCheckMark
;
2748 // FIXME these checkbox size calculations are really ugly...
2750 // between checkmark and box
2751 static const wxCoord wxGRID_CHECKMARK_MARGIN
= 2;
2753 wxSize
wxGridCellBoolRenderer::GetBestSize(wxGrid
& grid
,
2754 wxGridCellAttr
& WXUNUSED(attr
),
2759 // compute it only once (no locks for MT safeness in GUI thread...)
2760 if ( !ms_sizeCheckMark
.x
)
2762 // get checkbox size
2763 wxCheckBox
*checkbox
= new wxCheckBox(&grid
, wxID_ANY
, wxEmptyString
);
2764 wxSize size
= checkbox
->GetBestSize();
2765 wxCoord checkSize
= size
.y
+ 2 * wxGRID_CHECKMARK_MARGIN
;
2767 #if defined(__WXMOTIF__)
2768 checkSize
-= size
.y
/ 2;
2773 ms_sizeCheckMark
.x
= ms_sizeCheckMark
.y
= checkSize
;
2776 return ms_sizeCheckMark
;
2779 void wxGridCellBoolRenderer::Draw(wxGrid
& grid
,
2780 wxGridCellAttr
& attr
,
2786 wxGridCellRenderer::Draw(grid
, attr
, dc
, rect
, row
, col
, isSelected
);
2788 // draw a check mark in the centre (ignoring alignment - TODO)
2789 wxSize size
= GetBestSize(grid
, attr
, dc
, row
, col
);
2791 // don't draw outside the cell
2792 wxCoord minSize
= wxMin(rect
.width
, rect
.height
);
2793 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
2795 // and even leave (at least) 1 pixel margin
2796 size
.x
= size
.y
= minSize
;
2799 // draw a border around checkmark
2801 attr
.GetAlignment(&hAlign
, &vAlign
);
2804 if (hAlign
== wxALIGN_CENTRE
)
2806 rectBorder
.x
= rect
.x
+ rect
.width
/ 2 - size
.x
/ 2;
2807 rectBorder
.y
= rect
.y
+ rect
.height
/ 2 - size
.y
/ 2;
2808 rectBorder
.width
= size
.x
;
2809 rectBorder
.height
= size
.y
;
2811 else if (hAlign
== wxALIGN_LEFT
)
2813 rectBorder
.x
= rect
.x
+ 2;
2814 rectBorder
.y
= rect
.y
+ rect
.height
/ 2 - size
.y
/ 2;
2815 rectBorder
.width
= size
.x
;
2816 rectBorder
.height
= size
.y
;
2818 else if (hAlign
== wxALIGN_RIGHT
)
2820 rectBorder
.x
= rect
.x
+ rect
.width
- size
.x
- 2;
2821 rectBorder
.y
= rect
.y
+ rect
.height
/ 2 - size
.y
/ 2;
2822 rectBorder
.width
= size
.x
;
2823 rectBorder
.height
= size
.y
;
2827 if ( grid
.GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
) )
2829 value
= grid
.GetTable()->GetValueAsBool(row
, col
);
2833 wxString
cellval( grid
.GetTable()->GetValue(row
, col
) );
2834 value
= wxGridCellBoolEditor::IsTrueValue(cellval
);
2839 flags
|= wxCONTROL_CHECKED
;
2841 wxRendererNative::Get().DrawCheckBox( &grid
, dc
, rectBorder
, flags
);
2844 // ----------------------------------------------------------------------------
2846 // ----------------------------------------------------------------------------
2848 void wxGridCellAttr::Init(wxGridCellAttr
*attrDefault
)
2852 m_isReadOnly
= Unset
;
2857 m_attrkind
= wxGridCellAttr::Cell
;
2859 m_sizeRows
= m_sizeCols
= 1;
2860 m_overflow
= UnsetOverflow
;
2862 SetDefAttr(attrDefault
);
2865 wxGridCellAttr
*wxGridCellAttr::Clone() const
2867 wxGridCellAttr
*attr
= new wxGridCellAttr(m_defGridAttr
);
2869 if ( HasTextColour() )
2870 attr
->SetTextColour(GetTextColour());
2871 if ( HasBackgroundColour() )
2872 attr
->SetBackgroundColour(GetBackgroundColour());
2874 attr
->SetFont(GetFont());
2875 if ( HasAlignment() )
2876 attr
->SetAlignment(m_hAlign
, m_vAlign
);
2878 attr
->SetSize( m_sizeRows
, m_sizeCols
);
2882 attr
->SetRenderer(m_renderer
);
2883 m_renderer
->IncRef();
2887 attr
->SetEditor(m_editor
);
2892 attr
->SetReadOnly();
2894 attr
->SetOverflow( m_overflow
== Overflow
);
2895 attr
->SetKind( m_attrkind
);
2900 void wxGridCellAttr::MergeWith(wxGridCellAttr
*mergefrom
)
2902 if ( !HasTextColour() && mergefrom
->HasTextColour() )
2903 SetTextColour(mergefrom
->GetTextColour());
2904 if ( !HasBackgroundColour() && mergefrom
->HasBackgroundColour() )
2905 SetBackgroundColour(mergefrom
->GetBackgroundColour());
2906 if ( !HasFont() && mergefrom
->HasFont() )
2907 SetFont(mergefrom
->GetFont());
2908 if ( !HasAlignment() && mergefrom
->HasAlignment() )
2911 mergefrom
->GetAlignment( &hAlign
, &vAlign
);
2912 SetAlignment(hAlign
, vAlign
);
2914 if ( !HasSize() && mergefrom
->HasSize() )
2915 mergefrom
->GetSize( &m_sizeRows
, &m_sizeCols
);
2917 // Directly access member functions as GetRender/Editor don't just return
2918 // m_renderer/m_editor
2920 // Maybe add support for merge of Render and Editor?
2921 if (!HasRenderer() && mergefrom
->HasRenderer() )
2923 m_renderer
= mergefrom
->m_renderer
;
2924 m_renderer
->IncRef();
2926 if ( !HasEditor() && mergefrom
->HasEditor() )
2928 m_editor
= mergefrom
->m_editor
;
2931 if ( !HasReadWriteMode() && mergefrom
->HasReadWriteMode() )
2932 SetReadOnly(mergefrom
->IsReadOnly());
2934 if (!HasOverflowMode() && mergefrom
->HasOverflowMode() )
2935 SetOverflow(mergefrom
->GetOverflow());
2937 SetDefAttr(mergefrom
->m_defGridAttr
);
2940 void wxGridCellAttr::SetSize(int num_rows
, int num_cols
)
2942 // The size of a cell is normally 1,1
2944 // If this cell is larger (2,2) then this is the top left cell
2945 // the other cells that will be covered (lower right cells) must be
2946 // set to negative or zero values such that
2947 // row + num_rows of the covered cell points to the larger cell (this cell)
2948 // same goes for the col + num_cols.
2950 // Size of 0,0 is NOT valid, neither is <=0 and any positive value
2952 wxASSERT_MSG( (!((num_rows
> 0) && (num_cols
<= 0)) ||
2953 !((num_rows
<= 0) && (num_cols
> 0)) ||
2954 !((num_rows
== 0) && (num_cols
== 0))),
2955 wxT("wxGridCellAttr::SetSize only takes two postive values or negative/zero values"));
2957 m_sizeRows
= num_rows
;
2958 m_sizeCols
= num_cols
;
2961 const wxColour
& wxGridCellAttr::GetTextColour() const
2963 if (HasTextColour())
2967 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2969 return m_defGridAttr
->GetTextColour();
2973 wxFAIL_MSG(wxT("Missing default cell attribute"));
2974 return wxNullColour
;
2978 const wxColour
& wxGridCellAttr::GetBackgroundColour() const
2980 if (HasBackgroundColour())
2984 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2986 return m_defGridAttr
->GetBackgroundColour();
2990 wxFAIL_MSG(wxT("Missing default cell attribute"));
2991 return wxNullColour
;
2995 const wxFont
& wxGridCellAttr::GetFont() const
3001 else if (m_defGridAttr
&& m_defGridAttr
!= this)
3003 return m_defGridAttr
->GetFont();
3007 wxFAIL_MSG(wxT("Missing default cell attribute"));
3012 void wxGridCellAttr::GetAlignment(int *hAlign
, int *vAlign
) const
3021 else if (m_defGridAttr
&& m_defGridAttr
!= this)
3023 m_defGridAttr
->GetAlignment(hAlign
, vAlign
);
3027 wxFAIL_MSG(wxT("Missing default cell attribute"));
3031 void wxGridCellAttr::GetSize( int *num_rows
, int *num_cols
) const
3034 *num_rows
= m_sizeRows
;
3036 *num_cols
= m_sizeCols
;
3039 // GetRenderer and GetEditor use a slightly different decision path about
3040 // which attribute to use. If a non-default attr object has one then it is
3041 // used, otherwise the default editor or renderer is fetched from the grid and
3042 // used. It should be the default for the data type of the cell. If it is
3043 // NULL (because the table has a type that the grid does not have in its
3044 // registry), then the grid's default editor or renderer is used.
3046 wxGridCellRenderer
* wxGridCellAttr::GetRenderer(const wxGrid
* grid
, int row
, int col
) const
3048 wxGridCellRenderer
*renderer
= NULL
;
3050 if ( m_renderer
&& this != m_defGridAttr
)
3052 // use the cells renderer if it has one
3053 renderer
= m_renderer
;
3056 else // no non-default cell renderer
3058 // get default renderer for the data type
3061 // GetDefaultRendererForCell() will do IncRef() for us
3062 renderer
= grid
->GetDefaultRendererForCell(row
, col
);
3065 if ( renderer
== NULL
)
3067 if ( (m_defGridAttr
!= NULL
) && (m_defGridAttr
!= this) )
3069 // if we still don't have one then use the grid default
3070 // (no need for IncRef() here neither)
3071 renderer
= m_defGridAttr
->GetRenderer(NULL
, 0, 0);
3073 else // default grid attr
3075 // use m_renderer which we had decided not to use initially
3076 renderer
= m_renderer
;
3083 // we're supposed to always find something
3084 wxASSERT_MSG(renderer
, wxT("Missing default cell renderer"));
3089 // same as above, except for s/renderer/editor/g
3090 wxGridCellEditor
* wxGridCellAttr::GetEditor(const wxGrid
* grid
, int row
, int col
) const
3092 wxGridCellEditor
*editor
= NULL
;
3094 if ( m_editor
&& this != m_defGridAttr
)
3096 // use the cells editor if it has one
3100 else // no non default cell editor
3102 // get default editor for the data type
3105 // GetDefaultEditorForCell() will do IncRef() for us
3106 editor
= grid
->GetDefaultEditorForCell(row
, col
);
3109 if ( editor
== NULL
)
3111 if ( (m_defGridAttr
!= NULL
) && (m_defGridAttr
!= this) )
3113 // if we still don't have one then use the grid default
3114 // (no need for IncRef() here neither)
3115 editor
= m_defGridAttr
->GetEditor(NULL
, 0, 0);
3117 else // default grid attr
3119 // use m_editor which we had decided not to use initially
3127 // we're supposed to always find something
3128 wxASSERT_MSG(editor
, wxT("Missing default cell editor"));
3133 // ----------------------------------------------------------------------------
3134 // wxGridCellAttrData
3135 // ----------------------------------------------------------------------------
3137 void wxGridCellAttrData::SetAttr(wxGridCellAttr
*attr
, int row
, int col
)
3139 // Note: contrary to wxGridRowOrColAttrData::SetAttr, we must not
3140 // touch attribute's reference counting explicitly, since this
3141 // is managed by class wxGridCellWithAttr
3142 int n
= FindIndex(row
, col
);
3143 if ( n
== wxNOT_FOUND
)
3147 // add the attribute
3148 m_attrs
.Add(new wxGridCellWithAttr(row
, col
, attr
));
3150 //else: nothing to do
3152 else // we already have an attribute for this cell
3156 // change the attribute
3157 m_attrs
[(size_t)n
].ChangeAttr(attr
);
3161 // remove this attribute
3162 m_attrs
.RemoveAt((size_t)n
);
3167 wxGridCellAttr
*wxGridCellAttrData::GetAttr(int row
, int col
) const
3169 wxGridCellAttr
*attr
= NULL
;
3171 int n
= FindIndex(row
, col
);
3172 if ( n
!= wxNOT_FOUND
)
3174 attr
= m_attrs
[(size_t)n
].attr
;
3181 void wxGridCellAttrData::UpdateAttrRows( size_t pos
, int numRows
)
3183 size_t count
= m_attrs
.GetCount();
3184 for ( size_t n
= 0; n
< count
; n
++ )
3186 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
3187 wxCoord row
= coords
.GetRow();
3188 if ((size_t)row
>= pos
)
3192 // If rows inserted, include row counter where necessary
3193 coords
.SetRow(row
+ numRows
);
3195 else if (numRows
< 0)
3197 // If rows deleted ...
3198 if ((size_t)row
>= pos
- numRows
)
3200 // ...either decrement row counter (if row still exists)...
3201 coords
.SetRow(row
+ numRows
);
3205 // ...or remove the attribute
3206 m_attrs
.RemoveAt(n
);
3215 void wxGridCellAttrData::UpdateAttrCols( size_t pos
, int numCols
)
3217 size_t count
= m_attrs
.GetCount();
3218 for ( size_t n
= 0; n
< count
; n
++ )
3220 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
3221 wxCoord col
= coords
.GetCol();
3222 if ( (size_t)col
>= pos
)
3226 // If rows inserted, include row counter where necessary
3227 coords
.SetCol(col
+ numCols
);
3229 else if (numCols
< 0)
3231 // If rows deleted ...
3232 if ((size_t)col
>= pos
- numCols
)
3234 // ...either decrement row counter (if row still exists)...
3235 coords
.SetCol(col
+ numCols
);
3239 // ...or remove the attribute
3240 m_attrs
.RemoveAt(n
);
3249 int wxGridCellAttrData::FindIndex(int row
, int col
) const
3251 size_t count
= m_attrs
.GetCount();
3252 for ( size_t n
= 0; n
< count
; n
++ )
3254 const wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
3255 if ( (coords
.GetRow() == row
) && (coords
.GetCol() == col
) )
3264 // ----------------------------------------------------------------------------
3265 // wxGridRowOrColAttrData
3266 // ----------------------------------------------------------------------------
3268 wxGridRowOrColAttrData::~wxGridRowOrColAttrData()
3270 size_t count
= m_attrs
.GetCount();
3271 for ( size_t n
= 0; n
< count
; n
++ )
3273 m_attrs
[n
]->DecRef();
3277 wxGridCellAttr
*wxGridRowOrColAttrData::GetAttr(int rowOrCol
) const
3279 wxGridCellAttr
*attr
= NULL
;
3281 int n
= m_rowsOrCols
.Index(rowOrCol
);
3282 if ( n
!= wxNOT_FOUND
)
3284 attr
= m_attrs
[(size_t)n
];
3291 void wxGridRowOrColAttrData::SetAttr(wxGridCellAttr
*attr
, int rowOrCol
)
3293 int i
= m_rowsOrCols
.Index(rowOrCol
);
3294 if ( i
== wxNOT_FOUND
)
3298 // add the attribute - no need to do anything to reference count
3299 // since we take ownership of the attribute.
3300 m_rowsOrCols
.Add(rowOrCol
);
3303 // nothing to remove
3307 size_t n
= (size_t)i
;
3308 if ( m_attrs
[n
] == attr
)
3313 // change the attribute, handling reference count manually,
3314 // taking ownership of the new attribute.
3315 m_attrs
[n
]->DecRef();
3320 // remove this attribute, handling reference count manually
3321 m_attrs
[n
]->DecRef();
3322 m_rowsOrCols
.RemoveAt(n
);
3323 m_attrs
.RemoveAt(n
);
3328 void wxGridRowOrColAttrData::UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
)
3330 size_t count
= m_attrs
.GetCount();
3331 for ( size_t n
= 0; n
< count
; n
++ )
3333 int & rowOrCol
= m_rowsOrCols
[n
];
3334 if ( (size_t)rowOrCol
>= pos
)
3336 if ( numRowsOrCols
> 0 )
3338 // If rows inserted, include row counter where necessary
3339 rowOrCol
+= numRowsOrCols
;
3341 else if ( numRowsOrCols
< 0)
3343 // If rows deleted, either decrement row counter (if row still exists)
3344 if ((size_t)rowOrCol
>= pos
- numRowsOrCols
)
3345 rowOrCol
+= numRowsOrCols
;
3348 m_rowsOrCols
.RemoveAt(n
);
3349 m_attrs
[n
]->DecRef();
3350 m_attrs
.RemoveAt(n
);
3359 // ----------------------------------------------------------------------------
3360 // wxGridCellAttrProvider
3361 // ----------------------------------------------------------------------------
3363 wxGridCellAttrProvider::wxGridCellAttrProvider()
3368 wxGridCellAttrProvider::~wxGridCellAttrProvider()
3373 void wxGridCellAttrProvider::InitData()
3375 m_data
= new wxGridCellAttrProviderData
;
3378 wxGridCellAttr
*wxGridCellAttrProvider::GetAttr(int row
, int col
,
3379 wxGridCellAttr::wxAttrKind kind
) const
3381 wxGridCellAttr
*attr
= NULL
;
3386 case (wxGridCellAttr::Any
):
3387 // Get cached merge attributes.
3388 // Currently not used as no cache implemented as not mutable
3389 // attr = m_data->m_mergeAttr.GetAttr(row, col);
3392 // Basically implement old version.
3393 // Also check merge cache, so we don't have to re-merge every time..
3394 wxGridCellAttr
*attrcell
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
3395 wxGridCellAttr
*attrrow
= m_data
->m_rowAttrs
.GetAttr(row
);
3396 wxGridCellAttr
*attrcol
= m_data
->m_colAttrs
.GetAttr(col
);
3398 if ((attrcell
!= attrrow
) && (attrrow
!= attrcol
) && (attrcell
!= attrcol
))
3400 // Two or more are non NULL
3401 attr
= new wxGridCellAttr
;
3402 attr
->SetKind(wxGridCellAttr::Merged
);
3404 // Order is important..
3407 attr
->MergeWith(attrcell
);
3412 attr
->MergeWith(attrcol
);
3417 attr
->MergeWith(attrrow
);
3421 // store merge attr if cache implemented
3423 //m_data->m_mergeAttr.SetAttr(attr, row, col);
3427 // one or none is non null return it or null.
3446 case (wxGridCellAttr::Cell
):
3447 attr
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
3450 case (wxGridCellAttr::Col
):
3451 attr
= m_data
->m_colAttrs
.GetAttr(col
);
3454 case (wxGridCellAttr::Row
):
3455 attr
= m_data
->m_rowAttrs
.GetAttr(row
);
3460 // (wxGridCellAttr::Default):
3461 // (wxGridCellAttr::Merged):
3469 void wxGridCellAttrProvider::SetAttr(wxGridCellAttr
*attr
,
3475 m_data
->m_cellAttrs
.SetAttr(attr
, row
, col
);
3478 void wxGridCellAttrProvider::SetRowAttr(wxGridCellAttr
*attr
, int row
)
3483 m_data
->m_rowAttrs
.SetAttr(attr
, row
);
3486 void wxGridCellAttrProvider::SetColAttr(wxGridCellAttr
*attr
, int col
)
3491 m_data
->m_colAttrs
.SetAttr(attr
, col
);
3494 void wxGridCellAttrProvider::UpdateAttrRows( size_t pos
, int numRows
)
3498 m_data
->m_cellAttrs
.UpdateAttrRows( pos
, numRows
);
3500 m_data
->m_rowAttrs
.UpdateAttrRowsOrCols( pos
, numRows
);
3504 void wxGridCellAttrProvider::UpdateAttrCols( size_t pos
, int numCols
)
3508 m_data
->m_cellAttrs
.UpdateAttrCols( pos
, numCols
);
3510 m_data
->m_colAttrs
.UpdateAttrRowsOrCols( pos
, numCols
);
3514 // ----------------------------------------------------------------------------
3515 // wxGridTypeRegistry
3516 // ----------------------------------------------------------------------------
3518 wxGridTypeRegistry::~wxGridTypeRegistry()
3520 size_t count
= m_typeinfo
.GetCount();
3521 for ( size_t i
= 0; i
< count
; i
++ )
3522 delete m_typeinfo
[i
];
3525 void wxGridTypeRegistry::RegisterDataType(const wxString
& typeName
,
3526 wxGridCellRenderer
* renderer
,
3527 wxGridCellEditor
* editor
)
3529 wxGridDataTypeInfo
* info
= new wxGridDataTypeInfo(typeName
, renderer
, editor
);
3531 // is it already registered?
3532 int loc
= FindRegisteredDataType(typeName
);
3533 if ( loc
!= wxNOT_FOUND
)
3535 delete m_typeinfo
[loc
];
3536 m_typeinfo
[loc
] = info
;
3540 m_typeinfo
.Add(info
);
3544 int wxGridTypeRegistry::FindRegisteredDataType(const wxString
& typeName
)
3546 size_t count
= m_typeinfo
.GetCount();
3547 for ( size_t i
= 0; i
< count
; i
++ )
3549 if ( typeName
== m_typeinfo
[i
]->m_typeName
)
3558 int wxGridTypeRegistry::FindDataType(const wxString
& typeName
)
3560 int index
= FindRegisteredDataType(typeName
);
3561 if ( index
== wxNOT_FOUND
)
3563 // check whether this is one of the standard ones, in which case
3564 // register it "on the fly"
3566 if ( typeName
== wxGRID_VALUE_STRING
)
3568 RegisterDataType(wxGRID_VALUE_STRING
,
3569 new wxGridCellStringRenderer
,
3570 new wxGridCellTextEditor
);
3573 #endif // wxUSE_TEXTCTRL
3575 if ( typeName
== wxGRID_VALUE_BOOL
)
3577 RegisterDataType(wxGRID_VALUE_BOOL
,
3578 new wxGridCellBoolRenderer
,
3579 new wxGridCellBoolEditor
);
3582 #endif // wxUSE_CHECKBOX
3584 if ( typeName
== wxGRID_VALUE_NUMBER
)
3586 RegisterDataType(wxGRID_VALUE_NUMBER
,
3587 new wxGridCellNumberRenderer
,
3588 new wxGridCellNumberEditor
);
3590 else if ( typeName
== wxGRID_VALUE_FLOAT
)
3592 RegisterDataType(wxGRID_VALUE_FLOAT
,
3593 new wxGridCellFloatRenderer
,
3594 new wxGridCellFloatEditor
);
3597 #endif // wxUSE_TEXTCTRL
3599 if ( typeName
== wxGRID_VALUE_CHOICE
)
3601 RegisterDataType(wxGRID_VALUE_CHOICE
,
3602 new wxGridCellStringRenderer
,
3603 new wxGridCellChoiceEditor
);
3606 #endif // wxUSE_COMBOBOX
3611 // we get here only if just added the entry for this type, so return
3613 index
= m_typeinfo
.GetCount() - 1;
3619 int wxGridTypeRegistry::FindOrCloneDataType(const wxString
& typeName
)
3621 int index
= FindDataType(typeName
);
3622 if ( index
== wxNOT_FOUND
)
3624 // the first part of the typename is the "real" type, anything after ':'
3625 // are the parameters for the renderer
3626 index
= FindDataType(typeName
.BeforeFirst(_T(':')));
3627 if ( index
== wxNOT_FOUND
)
3632 wxGridCellRenderer
*renderer
= GetRenderer(index
);
3633 wxGridCellRenderer
*rendererOld
= renderer
;
3634 renderer
= renderer
->Clone();
3635 rendererOld
->DecRef();
3637 wxGridCellEditor
*editor
= GetEditor(index
);
3638 wxGridCellEditor
*editorOld
= editor
;
3639 editor
= editor
->Clone();
3640 editorOld
->DecRef();
3642 // do it even if there are no parameters to reset them to defaults
3643 wxString params
= typeName
.AfterFirst(_T(':'));
3644 renderer
->SetParameters(params
);
3645 editor
->SetParameters(params
);
3647 // register the new typename
3648 RegisterDataType(typeName
, renderer
, editor
);
3650 // we just registered it, it's the last one
3651 index
= m_typeinfo
.GetCount() - 1;
3657 wxGridCellRenderer
* wxGridTypeRegistry::GetRenderer(int index
)
3659 wxGridCellRenderer
* renderer
= m_typeinfo
[index
]->m_renderer
;
3666 wxGridCellEditor
* wxGridTypeRegistry::GetEditor(int index
)
3668 wxGridCellEditor
* editor
= m_typeinfo
[index
]->m_editor
;
3675 // ----------------------------------------------------------------------------
3677 // ----------------------------------------------------------------------------
3679 IMPLEMENT_ABSTRACT_CLASS( wxGridTableBase
, wxObject
)
3681 wxGridTableBase::wxGridTableBase()
3684 m_attrProvider
= NULL
;
3687 wxGridTableBase::~wxGridTableBase()
3689 delete m_attrProvider
;
3692 void wxGridTableBase::SetAttrProvider(wxGridCellAttrProvider
*attrProvider
)
3694 delete m_attrProvider
;
3695 m_attrProvider
= attrProvider
;
3698 bool wxGridTableBase::CanHaveAttributes()
3700 if ( ! GetAttrProvider() )
3702 // use the default attr provider by default
3703 SetAttrProvider(new wxGridCellAttrProvider
);
3709 wxGridCellAttr
*wxGridTableBase::GetAttr(int row
, int col
, wxGridCellAttr::wxAttrKind kind
)
3711 if ( m_attrProvider
)
3712 return m_attrProvider
->GetAttr(row
, col
, kind
);
3717 void wxGridTableBase::SetAttr(wxGridCellAttr
* attr
, int row
, int col
)
3719 if ( m_attrProvider
)
3722 attr
->SetKind(wxGridCellAttr::Cell
);
3723 m_attrProvider
->SetAttr(attr
, row
, col
);
3727 // as we take ownership of the pointer and don't store it, we must
3733 void wxGridTableBase::SetRowAttr(wxGridCellAttr
*attr
, int row
)
3735 if ( m_attrProvider
)
3737 attr
->SetKind(wxGridCellAttr::Row
);
3738 m_attrProvider
->SetRowAttr(attr
, row
);
3742 // as we take ownership of the pointer and don't store it, we must
3748 void wxGridTableBase::SetColAttr(wxGridCellAttr
*attr
, int col
)
3750 if ( m_attrProvider
)
3752 attr
->SetKind(wxGridCellAttr::Col
);
3753 m_attrProvider
->SetColAttr(attr
, col
);
3757 // as we take ownership of the pointer and don't store it, we must
3763 bool wxGridTableBase::InsertRows( size_t WXUNUSED(pos
),
3764 size_t WXUNUSED(numRows
) )
3766 wxFAIL_MSG( wxT("Called grid table class function InsertRows\nbut your derived table class does not override this function") );
3771 bool wxGridTableBase::AppendRows( size_t WXUNUSED(numRows
) )
3773 wxFAIL_MSG( wxT("Called grid table class function AppendRows\nbut your derived table class does not override this function"));
3778 bool wxGridTableBase::DeleteRows( size_t WXUNUSED(pos
),
3779 size_t WXUNUSED(numRows
) )
3781 wxFAIL_MSG( wxT("Called grid table class function DeleteRows\nbut your derived table class does not override this function"));
3786 bool wxGridTableBase::InsertCols( size_t WXUNUSED(pos
),
3787 size_t WXUNUSED(numCols
) )
3789 wxFAIL_MSG( wxT("Called grid table class function InsertCols\nbut your derived table class does not override this function"));
3794 bool wxGridTableBase::AppendCols( size_t WXUNUSED(numCols
) )
3796 wxFAIL_MSG(wxT("Called grid table class function AppendCols\nbut your derived table class does not override this function"));
3801 bool wxGridTableBase::DeleteCols( size_t WXUNUSED(pos
),
3802 size_t WXUNUSED(numCols
) )
3804 wxFAIL_MSG( wxT("Called grid table class function DeleteCols\nbut your derived table class does not override this function"));
3809 wxString
wxGridTableBase::GetRowLabelValue( int row
)
3813 // RD: Starting the rows at zero confuses users,
3814 // no matter how much it makes sense to us geeks.
3820 wxString
wxGridTableBase::GetColLabelValue( int col
)
3822 // default col labels are:
3823 // cols 0 to 25 : A-Z
3824 // cols 26 to 675 : AA-ZZ
3829 for ( n
= 1; ; n
++ )
3831 s
+= (wxChar
) (_T('A') + (wxChar
)(col
% 26));
3837 // reverse the string...
3839 for ( i
= 0; i
< n
; i
++ )
3847 wxString
wxGridTableBase::GetTypeName( int WXUNUSED(row
), int WXUNUSED(col
) )
3849 return wxGRID_VALUE_STRING
;
3852 bool wxGridTableBase::CanGetValueAs( int WXUNUSED(row
), int WXUNUSED(col
),
3853 const wxString
& typeName
)
3855 return typeName
== wxGRID_VALUE_STRING
;
3858 bool wxGridTableBase::CanSetValueAs( int row
, int col
, const wxString
& typeName
)
3860 return CanGetValueAs(row
, col
, typeName
);
3863 long wxGridTableBase::GetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
) )
3868 double wxGridTableBase::GetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
) )
3873 bool wxGridTableBase::GetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
) )
3878 void wxGridTableBase::SetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
),
3879 long WXUNUSED(value
) )
3883 void wxGridTableBase::SetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
),
3884 double WXUNUSED(value
) )
3888 void wxGridTableBase::SetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
),
3889 bool WXUNUSED(value
) )
3893 void* wxGridTableBase::GetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
3894 const wxString
& WXUNUSED(typeName
) )
3899 void wxGridTableBase::SetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
3900 const wxString
& WXUNUSED(typeName
),
3901 void* WXUNUSED(value
) )
3905 //////////////////////////////////////////////////////////////////////
3907 // Message class for the grid table to send requests and notifications
3911 wxGridTableMessage::wxGridTableMessage()
3919 wxGridTableMessage::wxGridTableMessage( wxGridTableBase
*table
, int id
,
3920 int commandInt1
, int commandInt2
)
3924 m_comInt1
= commandInt1
;
3925 m_comInt2
= commandInt2
;
3928 //////////////////////////////////////////////////////////////////////
3930 // A basic grid table for string data. An object of this class will
3931 // created by wxGrid if you don't specify an alternative table class.
3934 WX_DEFINE_OBJARRAY(wxGridStringArray
)
3936 IMPLEMENT_DYNAMIC_CLASS( wxGridStringTable
, wxGridTableBase
)
3938 wxGridStringTable::wxGridStringTable()
3943 wxGridStringTable::wxGridStringTable( int numRows
, int numCols
)
3946 m_data
.Alloc( numRows
);
3949 sa
.Alloc( numCols
);
3950 sa
.Add( wxEmptyString
, numCols
);
3952 m_data
.Add( sa
, numRows
);
3955 wxGridStringTable::~wxGridStringTable()
3959 int wxGridStringTable::GetNumberRows()
3961 return m_data
.GetCount();
3964 int wxGridStringTable::GetNumberCols()
3966 if ( m_data
.GetCount() > 0 )
3967 return m_data
[0].GetCount();
3972 wxString
wxGridStringTable::GetValue( int row
, int col
)
3974 wxCHECK_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3976 _T("invalid row or column index in wxGridStringTable") );
3978 return m_data
[row
][col
];
3981 void wxGridStringTable::SetValue( int row
, int col
, const wxString
& value
)
3983 wxCHECK_RET( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3984 _T("invalid row or column index in wxGridStringTable") );
3986 m_data
[row
][col
] = value
;
3989 void wxGridStringTable::Clear()
3992 int numRows
, numCols
;
3994 numRows
= m_data
.GetCount();
3997 numCols
= m_data
[0].GetCount();
3999 for ( row
= 0; row
< numRows
; row
++ )
4001 for ( col
= 0; col
< numCols
; col
++ )
4003 m_data
[row
][col
] = wxEmptyString
;
4009 bool wxGridStringTable::InsertRows( size_t pos
, size_t numRows
)
4011 size_t curNumRows
= m_data
.GetCount();
4012 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
4013 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
4015 if ( pos
>= curNumRows
)
4017 return AppendRows( numRows
);
4021 sa
.Alloc( curNumCols
);
4022 sa
.Add( wxEmptyString
, curNumCols
);
4023 m_data
.Insert( sa
, pos
, numRows
);
4027 wxGridTableMessage
msg( this,
4028 wxGRIDTABLE_NOTIFY_ROWS_INSERTED
,
4032 GetView()->ProcessTableMessage( msg
);
4038 bool wxGridStringTable::AppendRows( size_t numRows
)
4040 size_t curNumRows
= m_data
.GetCount();
4041 size_t curNumCols
= ( curNumRows
> 0
4042 ? m_data
[0].GetCount()
4043 : ( GetView() ? GetView()->GetNumberCols() : 0 ) );
4046 if ( curNumCols
> 0 )
4048 sa
.Alloc( curNumCols
);
4049 sa
.Add( wxEmptyString
, curNumCols
);
4052 m_data
.Add( sa
, numRows
);
4056 wxGridTableMessage
msg( this,
4057 wxGRIDTABLE_NOTIFY_ROWS_APPENDED
,
4060 GetView()->ProcessTableMessage( msg
);
4066 bool wxGridStringTable::DeleteRows( size_t pos
, size_t numRows
)
4068 size_t curNumRows
= m_data
.GetCount();
4070 if ( pos
>= curNumRows
)
4072 wxFAIL_MSG( wxString::Format
4074 wxT("Called wxGridStringTable::DeleteRows(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu rows"),
4076 (unsigned long)numRows
,
4077 (unsigned long)curNumRows
4083 if ( numRows
> curNumRows
- pos
)
4085 numRows
= curNumRows
- pos
;
4088 if ( numRows
>= curNumRows
)
4094 m_data
.RemoveAt( pos
, numRows
);
4099 wxGridTableMessage
msg( this,
4100 wxGRIDTABLE_NOTIFY_ROWS_DELETED
,
4104 GetView()->ProcessTableMessage( msg
);
4110 bool wxGridStringTable::InsertCols( size_t pos
, size_t numCols
)
4114 size_t curNumRows
= m_data
.GetCount();
4115 size_t curNumCols
= ( curNumRows
> 0
4116 ? m_data
[0].GetCount()
4117 : ( GetView() ? GetView()->GetNumberCols() : 0 ) );
4119 if ( pos
>= curNumCols
)
4121 return AppendCols( numCols
);
4124 if ( !m_colLabels
.IsEmpty() )
4126 m_colLabels
.Insert( wxEmptyString
, pos
, numCols
);
4129 for ( i
= pos
; i
< pos
+ numCols
; i
++ )
4130 m_colLabels
[i
] = wxGridTableBase::GetColLabelValue( i
);
4133 for ( row
= 0; row
< curNumRows
; row
++ )
4135 for ( col
= pos
; col
< pos
+ numCols
; col
++ )
4137 m_data
[row
].Insert( wxEmptyString
, col
);
4143 wxGridTableMessage
msg( this,
4144 wxGRIDTABLE_NOTIFY_COLS_INSERTED
,
4148 GetView()->ProcessTableMessage( msg
);
4154 bool wxGridStringTable::AppendCols( size_t numCols
)
4158 size_t curNumRows
= m_data
.GetCount();
4160 for ( row
= 0; row
< curNumRows
; row
++ )
4162 m_data
[row
].Add( wxEmptyString
, numCols
);
4167 wxGridTableMessage
msg( this,
4168 wxGRIDTABLE_NOTIFY_COLS_APPENDED
,
4171 GetView()->ProcessTableMessage( msg
);
4177 bool wxGridStringTable::DeleteCols( size_t pos
, size_t numCols
)
4181 size_t curNumRows
= m_data
.GetCount();
4182 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
4183 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
4185 if ( pos
>= curNumCols
)
4187 wxFAIL_MSG( wxString::Format
4189 wxT("Called wxGridStringTable::DeleteCols(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu cols"),
4191 (unsigned long)numCols
,
4192 (unsigned long)curNumCols
4199 colID
= GetView()->GetColAt( pos
);
4203 if ( numCols
> curNumCols
- colID
)
4205 numCols
= curNumCols
- colID
;
4208 if ( !m_colLabels
.IsEmpty() )
4210 // m_colLabels stores just as many elements as it needs, e.g. if only
4211 // the label of the first column had been set it would have only one
4212 // element and not numCols, so account for it
4213 int nToRm
= m_colLabels
.size() - colID
;
4215 m_colLabels
.RemoveAt( colID
, nToRm
);
4218 for ( row
= 0; row
< curNumRows
; row
++ )
4220 if ( numCols
>= curNumCols
)
4222 m_data
[row
].Clear();
4226 m_data
[row
].RemoveAt( colID
, numCols
);
4232 wxGridTableMessage
msg( this,
4233 wxGRIDTABLE_NOTIFY_COLS_DELETED
,
4237 GetView()->ProcessTableMessage( msg
);
4243 wxString
wxGridStringTable::GetRowLabelValue( int row
)
4245 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
4247 // using default label
4249 return wxGridTableBase::GetRowLabelValue( row
);
4253 return m_rowLabels
[row
];
4257 wxString
wxGridStringTable::GetColLabelValue( int col
)
4259 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
4261 // using default label
4263 return wxGridTableBase::GetColLabelValue( col
);
4267 return m_colLabels
[col
];
4271 void wxGridStringTable::SetRowLabelValue( int row
, const wxString
& value
)
4273 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
4275 int n
= m_rowLabels
.GetCount();
4278 for ( i
= n
; i
<= row
; i
++ )
4280 m_rowLabels
.Add( wxGridTableBase::GetRowLabelValue(i
) );
4284 m_rowLabels
[row
] = value
;
4287 void wxGridStringTable::SetColLabelValue( int col
, const wxString
& value
)
4289 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
4291 int n
= m_colLabels
.GetCount();
4294 for ( i
= n
; i
<= col
; i
++ )
4296 m_colLabels
.Add( wxGridTableBase::GetColLabelValue(i
) );
4300 m_colLabels
[col
] = value
;
4304 //////////////////////////////////////////////////////////////////////
4305 //////////////////////////////////////////////////////////////////////
4307 BEGIN_EVENT_TABLE(wxGridSubwindow
, wxWindow
)
4308 EVT_MOUSE_CAPTURE_LOST(wxGridSubwindow::OnMouseCaptureLost
)
4311 void wxGridSubwindow::OnMouseCaptureLost(wxMouseCaptureLostEvent
& WXUNUSED(event
))
4313 m_owner
->CancelMouseCapture();
4316 BEGIN_EVENT_TABLE( wxGridRowLabelWindow
, wxGridSubwindow
)
4317 EVT_PAINT( wxGridRowLabelWindow::OnPaint
)
4318 EVT_MOUSEWHEEL( wxGridRowLabelWindow::OnMouseWheel
)
4319 EVT_MOUSE_EVENTS( wxGridRowLabelWindow::OnMouseEvent
)
4322 void wxGridRowLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
4326 // NO - don't do this because it will set both the x and y origin
4327 // coords to match the parent scrolled window and we just want to
4328 // set the y coord - MB
4330 // m_owner->PrepareDC( dc );
4333 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
4334 wxPoint pt
= dc
.GetDeviceOrigin();
4335 dc
.SetDeviceOrigin( pt
.x
, pt
.y
-y
);
4337 wxArrayInt rows
= m_owner
->CalcRowLabelsExposed( GetUpdateRegion() );
4338 m_owner
->DrawRowLabels( dc
, rows
);
4341 void wxGridRowLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
4343 m_owner
->ProcessRowLabelMouseEvent( event
);
4346 void wxGridRowLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
4348 if (!m_owner
->GetEventHandler()->ProcessEvent( event
))
4352 //////////////////////////////////////////////////////////////////////
4354 BEGIN_EVENT_TABLE( wxGridColLabelWindow
, wxGridSubwindow
)
4355 EVT_PAINT( wxGridColLabelWindow::OnPaint
)
4356 EVT_MOUSEWHEEL( wxGridColLabelWindow::OnMouseWheel
)
4357 EVT_MOUSE_EVENTS( wxGridColLabelWindow::OnMouseEvent
)
4360 void wxGridColLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
4364 // NO - don't do this because it will set both the x and y origin
4365 // coords to match the parent scrolled window and we just want to
4366 // set the x coord - MB
4368 // m_owner->PrepareDC( dc );
4371 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
4372 wxPoint pt
= dc
.GetDeviceOrigin();
4373 if (GetLayoutDirection() == wxLayout_RightToLeft
)
4374 dc
.SetDeviceOrigin( pt
.x
+x
, pt
.y
);
4376 dc
.SetDeviceOrigin( pt
.x
-x
, pt
.y
);
4378 wxArrayInt cols
= m_owner
->CalcColLabelsExposed( GetUpdateRegion() );
4379 m_owner
->DrawColLabels( dc
, cols
);
4382 void wxGridColLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
4384 m_owner
->ProcessColLabelMouseEvent( event
);
4387 void wxGridColLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
4389 if (!m_owner
->GetEventHandler()->ProcessEvent( event
))
4393 //////////////////////////////////////////////////////////////////////
4395 BEGIN_EVENT_TABLE( wxGridCornerLabelWindow
, wxGridSubwindow
)
4396 EVT_MOUSEWHEEL( wxGridCornerLabelWindow::OnMouseWheel
)
4397 EVT_MOUSE_EVENTS( wxGridCornerLabelWindow::OnMouseEvent
)
4398 EVT_PAINT( wxGridCornerLabelWindow::OnPaint
)
4401 void wxGridCornerLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
4405 m_owner
->DrawCornerLabel(dc
);
4408 void wxGridCornerLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
4410 m_owner
->ProcessCornerLabelMouseEvent( event
);
4413 void wxGridCornerLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
4415 if (!m_owner
->GetEventHandler()->ProcessEvent(event
))
4419 //////////////////////////////////////////////////////////////////////
4421 BEGIN_EVENT_TABLE( wxGridWindow
, wxGridSubwindow
)
4422 EVT_PAINT( wxGridWindow::OnPaint
)
4423 EVT_MOUSEWHEEL( wxGridWindow::OnMouseWheel
)
4424 EVT_MOUSE_EVENTS( wxGridWindow::OnMouseEvent
)
4425 EVT_KEY_DOWN( wxGridWindow::OnKeyDown
)
4426 EVT_KEY_UP( wxGridWindow::OnKeyUp
)
4427 EVT_CHAR( wxGridWindow::OnChar
)
4428 EVT_SET_FOCUS( wxGridWindow::OnFocus
)
4429 EVT_KILL_FOCUS( wxGridWindow::OnFocus
)
4430 EVT_ERASE_BACKGROUND( wxGridWindow::OnEraseBackground
)
4433 void wxGridWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
4435 wxPaintDC
dc( this );
4436 m_owner
->PrepareDC( dc
);
4437 wxRegion reg
= GetUpdateRegion();
4438 wxGridCellCoordsArray dirtyCells
= m_owner
->CalcCellsExposed( reg
);
4439 m_owner
->DrawGridCellArea( dc
, dirtyCells
);
4441 m_owner
->DrawGridSpace( dc
);
4443 m_owner
->DrawAllGridLines( dc
, reg
);
4445 m_owner
->DrawHighlight( dc
, dirtyCells
);
4448 void wxGridWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
4450 wxWindow::ScrollWindow( dx
, dy
, rect
);
4451 m_owner
->GetGridRowLabelWindow()->ScrollWindow( 0, dy
, rect
);
4452 m_owner
->GetGridColLabelWindow()->ScrollWindow( dx
, 0, rect
);
4455 void wxGridWindow::OnMouseEvent( wxMouseEvent
& event
)
4457 if (event
.ButtonDown(wxMOUSE_BTN_LEFT
) && FindFocus() != this)
4460 m_owner
->ProcessGridCellMouseEvent( event
);
4463 void wxGridWindow::OnMouseWheel( wxMouseEvent
& event
)
4465 if (!m_owner
->GetEventHandler()->ProcessEvent( event
))
4469 // This seems to be required for wxMotif/wxGTK otherwise the mouse
4470 // cursor must be in the cell edit control to get key events
4472 void wxGridWindow::OnKeyDown( wxKeyEvent
& event
)
4474 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
4478 void wxGridWindow::OnKeyUp( wxKeyEvent
& event
)
4480 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
4484 void wxGridWindow::OnChar( wxKeyEvent
& event
)
4486 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
4490 void wxGridWindow::OnEraseBackground( wxEraseEvent
& WXUNUSED(event
) )
4494 void wxGridWindow::OnFocus(wxFocusEvent
& event
)
4496 // and if we have any selection, it has to be repainted, because it
4497 // uses different colour when the grid is not focused:
4498 if ( m_owner
->IsSelection() )
4504 // NB: Note that this code is in "else" branch only because the other
4505 // branch refreshes everything and so there's no point in calling
4506 // Refresh() again, *not* because it should only be done if
4507 // !IsSelection(). If the above code is ever optimized to refresh
4508 // only selected area, this needs to be moved out of the "else"
4509 // branch so that it's always executed.
4511 // current cell cursor {dis,re}appears on focus change:
4512 const wxGridCellCoords
cursorCoords(m_owner
->GetGridCursorRow(),
4513 m_owner
->GetGridCursorCol());
4514 const wxRect cursor
=
4515 m_owner
->BlockToDeviceRect(cursorCoords
, cursorCoords
);
4516 Refresh(true, &cursor
);
4519 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
4523 #define internalXToCol(x) XToCol(x, true)
4524 #define internalYToRow(y) YToRow(y, true)
4526 /////////////////////////////////////////////////////////////////////
4528 #if wxUSE_EXTENDED_RTTI
4529 WX_DEFINE_FLAGS( wxGridStyle
)
4531 wxBEGIN_FLAGS( wxGridStyle
)
4532 // new style border flags, we put them first to
4533 // use them for streaming out
4534 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
4535 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
4536 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
4537 wxFLAGS_MEMBER(wxBORDER_RAISED
)
4538 wxFLAGS_MEMBER(wxBORDER_STATIC
)
4539 wxFLAGS_MEMBER(wxBORDER_NONE
)
4541 // old style border flags
4542 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
4543 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
4544 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
4545 wxFLAGS_MEMBER(wxRAISED_BORDER
)
4546 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
4547 wxFLAGS_MEMBER(wxBORDER
)
4549 // standard window styles
4550 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
4551 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
4552 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
4553 wxFLAGS_MEMBER(wxWANTS_CHARS
)
4554 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE
)
4555 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
4556 wxFLAGS_MEMBER(wxVSCROLL
)
4557 wxFLAGS_MEMBER(wxHSCROLL
)
4559 wxEND_FLAGS( wxGridStyle
)
4561 IMPLEMENT_DYNAMIC_CLASS_XTI(wxGrid
, wxScrolledWindow
,"wx/grid.h")
4563 wxBEGIN_PROPERTIES_TABLE(wxGrid
)
4564 wxHIDE_PROPERTY( Children
)
4565 wxPROPERTY_FLAGS( WindowStyle
, wxGridStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
4566 wxEND_PROPERTIES_TABLE()
4568 wxBEGIN_HANDLERS_TABLE(wxGrid
)
4569 wxEND_HANDLERS_TABLE()
4571 wxCONSTRUCTOR_5( wxGrid
, wxWindow
* , Parent
, wxWindowID
, Id
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
4574 TODO : Expose more information of a list's layout, etc. via appropriate objects (e.g., NotebookPageInfo)
4577 IMPLEMENT_DYNAMIC_CLASS( wxGrid
, wxScrolledWindow
)
4580 BEGIN_EVENT_TABLE( wxGrid
, wxScrolledWindow
)
4581 EVT_PAINT( wxGrid::OnPaint
)
4582 EVT_SIZE( wxGrid::OnSize
)
4583 EVT_KEY_DOWN( wxGrid::OnKeyDown
)
4584 EVT_KEY_UP( wxGrid::OnKeyUp
)
4585 EVT_CHAR ( wxGrid::OnChar
)
4586 EVT_ERASE_BACKGROUND( wxGrid::OnEraseBackground
)
4589 bool wxGrid::Create(wxWindow
*parent
, wxWindowID id
,
4590 const wxPoint
& pos
, const wxSize
& size
,
4591 long style
, const wxString
& name
)
4593 if (!wxScrolledWindow::Create(parent
, id
, pos
, size
,
4594 style
| wxWANTS_CHARS
, name
))
4597 m_colMinWidths
= wxLongToLongHashMap(GRID_HASH_SIZE
);
4598 m_rowMinHeights
= wxLongToLongHashMap(GRID_HASH_SIZE
);
4601 SetInitialSize(size
);
4602 SetScrollRate(m_scrollLineX
, m_scrollLineY
);
4611 m_winCapture
->ReleaseMouse();
4613 // Ensure that the editor control is destroyed before the grid is,
4614 // otherwise we crash later when the editor tries to do something with the
4615 // half destroyed grid
4616 HideCellEditControl();
4618 // Must do this or ~wxScrollHelper will pop the wrong event handler
4619 SetTargetWindow(this);
4621 wxSafeDecRef(m_defaultCellAttr
);
4623 #ifdef DEBUG_ATTR_CACHE
4624 size_t total
= gs_nAttrCacheHits
+ gs_nAttrCacheMisses
;
4625 wxPrintf(_T("wxGrid attribute cache statistics: "
4626 "total: %u, hits: %u (%u%%)\n"),
4627 total
, gs_nAttrCacheHits
,
4628 total
? (gs_nAttrCacheHits
*100) / total
: 0);
4631 // if we own the table, just delete it, otherwise at least don't leave it
4632 // with dangling view pointer
4635 else if ( m_table
&& m_table
->GetView() == this )
4636 m_table
->SetView(NULL
);
4638 delete m_typeRegistry
;
4643 // ----- internal init and update functions
4646 // NOTE: If using the default visual attributes works everywhere then this can
4647 // be removed as well as the #else cases below.
4648 #define _USE_VISATTR 0
4650 void wxGrid::Create()
4652 // create the type registry
4653 m_typeRegistry
= new wxGridTypeRegistry
;
4655 m_cellEditCtrlEnabled
= false;
4657 m_defaultCellAttr
= new wxGridCellAttr();
4659 // Set default cell attributes
4660 m_defaultCellAttr
->SetDefAttr(m_defaultCellAttr
);
4661 m_defaultCellAttr
->SetKind(wxGridCellAttr::Default
);
4662 m_defaultCellAttr
->SetFont(GetFont());
4663 m_defaultCellAttr
->SetAlignment(wxALIGN_LEFT
, wxALIGN_TOP
);
4664 m_defaultCellAttr
->SetRenderer(new wxGridCellStringRenderer
);
4665 m_defaultCellAttr
->SetEditor(new wxGridCellTextEditor
);
4668 wxVisualAttributes gva
= wxListBox::GetClassDefaultAttributes();
4669 wxVisualAttributes lva
= wxPanel::GetClassDefaultAttributes();
4671 m_defaultCellAttr
->SetTextColour(gva
.colFg
);
4672 m_defaultCellAttr
->SetBackgroundColour(gva
.colBg
);
4675 m_defaultCellAttr
->SetTextColour(
4676 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
4677 m_defaultCellAttr
->SetBackgroundColour(
4678 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
4683 m_currentCellCoords
= wxGridNoCellCoords
;
4685 // subwindow components that make up the wxGrid
4686 m_rowLabelWin
= new wxGridRowLabelWindow(this);
4687 CreateColumnWindow();
4688 m_cornerLabelWin
= new wxGridCornerLabelWindow(this);
4689 m_gridWin
= new wxGridWindow( this );
4691 SetTargetWindow( m_gridWin
);
4694 wxColour gfg
= gva
.colFg
;
4695 wxColour gbg
= gva
.colBg
;
4696 wxColour lfg
= lva
.colFg
;
4697 wxColour lbg
= lva
.colBg
;
4699 wxColour gfg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT
);
4700 wxColour gbg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOW
);
4701 wxColour lfg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT
);
4702 wxColour lbg
= wxSystemSettings::GetColour( wxSYS_COLOUR_BTNFACE
);
4705 m_cornerLabelWin
->SetOwnForegroundColour(lfg
);
4706 m_cornerLabelWin
->SetOwnBackgroundColour(lbg
);
4707 m_rowLabelWin
->SetOwnForegroundColour(lfg
);
4708 m_rowLabelWin
->SetOwnBackgroundColour(lbg
);
4709 m_colWindow
->SetOwnForegroundColour(lfg
);
4710 m_colWindow
->SetOwnBackgroundColour(lbg
);
4712 m_gridWin
->SetOwnForegroundColour(gfg
);
4713 m_gridWin
->SetOwnBackgroundColour(gbg
);
4715 m_labelBackgroundColour
= m_rowLabelWin
->GetBackgroundColour();
4716 m_labelTextColour
= m_rowLabelWin
->GetForegroundColour();
4718 // now that we have the grid window, use its font to compute the default
4720 m_defaultRowHeight
= m_gridWin
->GetCharHeight();
4721 #if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl()
4722 m_defaultRowHeight
+= 8;
4724 m_defaultRowHeight
+= 4;
4729 void wxGrid::CreateColumnWindow()
4731 if ( m_useNativeHeader
)
4733 m_colWindow
= new wxGridHeaderCtrl(this);
4734 m_colLabelHeight
= m_colWindow
->GetBestSize().y
;
4736 else // draw labels ourselves
4738 m_colWindow
= new wxGridColLabelWindow(this);
4739 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
4743 bool wxGrid::CreateGrid( int numRows
, int numCols
,
4744 wxGridSelectionModes selmode
)
4746 wxCHECK_MSG( !m_created
,
4748 wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
4750 return SetTable(new wxGridStringTable(numRows
, numCols
), true, selmode
);
4753 void wxGrid::SetSelectionMode(wxGridSelectionModes selmode
)
4755 wxCHECK_RET( m_created
,
4756 wxT("Called wxGrid::SetSelectionMode() before calling CreateGrid()") );
4758 m_selection
->SetSelectionMode( selmode
);
4761 wxGrid::wxGridSelectionModes
wxGrid::GetSelectionMode() const
4763 wxCHECK_MSG( m_created
, wxGridSelectCells
,
4764 wxT("Called wxGrid::GetSelectionMode() before calling CreateGrid()") );
4766 return m_selection
->GetSelectionMode();
4770 wxGrid::SetTable(wxGridTableBase
*table
,
4772 wxGrid::wxGridSelectionModes selmode
)
4774 bool checkSelection
= false;
4777 // stop all processing
4782 m_table
->SetView(0);
4794 checkSelection
= true;
4796 // kill row and column size arrays
4797 m_colWidths
.Empty();
4798 m_colRights
.Empty();
4799 m_rowHeights
.Empty();
4800 m_rowBottoms
.Empty();
4805 m_numRows
= table
->GetNumberRows();
4806 m_numCols
= table
->GetNumberCols();
4808 if ( m_useNativeHeader
)
4809 GetGridColHeader()->SetColumnCount(m_numCols
);
4812 m_table
->SetView( this );
4813 m_ownTable
= takeOwnership
;
4814 m_selection
= new wxGridSelection( this, selmode
);
4817 // If the newly set table is smaller than the
4818 // original one current cell and selection regions
4819 // might be invalid,
4820 m_selectedBlockCorner
= wxGridNoCellCoords
;
4821 m_currentCellCoords
=
4822 wxGridCellCoords(wxMin(m_numRows
, m_currentCellCoords
.GetRow()),
4823 wxMin(m_numCols
, m_currentCellCoords
.GetCol()));
4824 if (m_selectedBlockTopLeft
.GetRow() >= m_numRows
||
4825 m_selectedBlockTopLeft
.GetCol() >= m_numCols
)
4827 m_selectedBlockTopLeft
= wxGridNoCellCoords
;
4828 m_selectedBlockBottomRight
= wxGridNoCellCoords
;
4831 m_selectedBlockBottomRight
=
4832 wxGridCellCoords(wxMin(m_numRows
,
4833 m_selectedBlockBottomRight
.GetRow()),
4835 m_selectedBlockBottomRight
.GetCol()));
4849 m_cornerLabelWin
= NULL
;
4850 m_rowLabelWin
= NULL
;
4858 m_defaultCellAttr
= NULL
;
4859 m_typeRegistry
= NULL
;
4860 m_winCapture
= NULL
;
4862 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
4863 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
4866 m_attrCache
.row
= -1;
4867 m_attrCache
.col
= -1;
4868 m_attrCache
.attr
= NULL
;
4870 m_labelFont
= GetFont();
4871 m_labelFont
.SetWeight( wxBOLD
);
4873 m_rowLabelHorizAlign
= wxALIGN_CENTRE
;
4874 m_rowLabelVertAlign
= wxALIGN_CENTRE
;
4876 m_colLabelHorizAlign
= wxALIGN_CENTRE
;
4877 m_colLabelVertAlign
= wxALIGN_CENTRE
;
4878 m_colLabelTextOrientation
= wxHORIZONTAL
;
4880 m_defaultColWidth
= WXGRID_DEFAULT_COL_WIDTH
;
4881 m_defaultRowHeight
= 0; // this will be initialized after creation
4883 m_minAcceptableColWidth
= WXGRID_MIN_COL_WIDTH
;
4884 m_minAcceptableRowHeight
= WXGRID_MIN_ROW_HEIGHT
;
4886 m_gridLineColour
= wxColour( 192,192,192 );
4887 m_gridLinesEnabled
= true;
4888 m_gridLinesClipHorz
=
4889 m_gridLinesClipVert
= true;
4890 m_cellHighlightColour
= *wxBLACK
;
4891 m_cellHighlightPenWidth
= 2;
4892 m_cellHighlightROPenWidth
= 1;
4894 m_canDragColMove
= false;
4896 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
4897 m_winCapture
= NULL
;
4898 m_canDragRowSize
= true;
4899 m_canDragColSize
= true;
4900 m_canDragGridSize
= true;
4901 m_canDragCell
= false;
4903 m_dragRowOrCol
= -1;
4904 m_isDragging
= false;
4905 m_startDragPos
= wxDefaultPosition
;
4907 m_sortCol
= wxNOT_FOUND
;
4908 m_sortIsAscending
= true;
4911 m_nativeColumnLabels
= false;
4913 m_waitForSlowClick
= false;
4915 m_rowResizeCursor
= wxCursor( wxCURSOR_SIZENS
);
4916 m_colResizeCursor
= wxCursor( wxCURSOR_SIZEWE
);
4918 m_currentCellCoords
= wxGridNoCellCoords
;
4920 m_selectedBlockTopLeft
=
4921 m_selectedBlockBottomRight
=
4922 m_selectedBlockCorner
= wxGridNoCellCoords
;
4924 m_selectionBackground
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
);
4925 m_selectionForeground
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
4927 m_editable
= true; // default for whole grid
4929 m_inOnKeyDown
= false;
4935 m_scrollLineX
= GRID_SCROLL_LINE_X
;
4936 m_scrollLineY
= GRID_SCROLL_LINE_Y
;
4939 // ----------------------------------------------------------------------------
4940 // the idea is to call these functions only when necessary because they create
4941 // quite big arrays which eat memory mostly unnecessary - in particular, if
4942 // default widths/heights are used for all rows/columns, we may not use these
4945 // with some extra code, it should be possible to only store the widths/heights
4946 // different from default ones (resulting in space savings for huge grids) but
4947 // this is not done currently
4948 // ----------------------------------------------------------------------------
4950 void wxGrid::InitRowHeights()
4952 m_rowHeights
.Empty();
4953 m_rowBottoms
.Empty();
4955 m_rowHeights
.Alloc( m_numRows
);
4956 m_rowBottoms
.Alloc( m_numRows
);
4958 m_rowHeights
.Add( m_defaultRowHeight
, m_numRows
);
4961 for ( int i
= 0; i
< m_numRows
; i
++ )
4963 rowBottom
+= m_defaultRowHeight
;
4964 m_rowBottoms
.Add( rowBottom
);
4968 void wxGrid::InitColWidths()
4970 m_colWidths
.Empty();
4971 m_colRights
.Empty();
4973 m_colWidths
.Alloc( m_numCols
);
4974 m_colRights
.Alloc( m_numCols
);
4976 m_colWidths
.Add( m_defaultColWidth
, m_numCols
);
4978 for ( int i
= 0; i
< m_numCols
; i
++ )
4980 int colRight
= ( GetColPos( i
) + 1 ) * m_defaultColWidth
;
4981 m_colRights
.Add( colRight
);
4985 int wxGrid::GetColWidth(int col
) const
4987 return m_colWidths
.IsEmpty() ? m_defaultColWidth
: m_colWidths
[col
];
4990 int wxGrid::GetColLeft(int col
) const
4992 return m_colRights
.IsEmpty() ? GetColPos( col
) * m_defaultColWidth
4993 : m_colRights
[col
] - m_colWidths
[col
];
4996 int wxGrid::GetColRight(int col
) const
4998 return m_colRights
.IsEmpty() ? (GetColPos( col
) + 1) * m_defaultColWidth
5002 int wxGrid::GetRowHeight(int row
) const
5004 return m_rowHeights
.IsEmpty() ? m_defaultRowHeight
: m_rowHeights
[row
];
5007 int wxGrid::GetRowTop(int row
) const
5009 return m_rowBottoms
.IsEmpty() ? row
* m_defaultRowHeight
5010 : m_rowBottoms
[row
] - m_rowHeights
[row
];
5013 int wxGrid::GetRowBottom(int row
) const
5015 return m_rowBottoms
.IsEmpty() ? (row
+ 1) * m_defaultRowHeight
5016 : m_rowBottoms
[row
];
5019 void wxGrid::CalcDimensions()
5021 // compute the size of the scrollable area
5022 int w
= m_numCols
> 0 ? GetColRight(GetColAt(m_numCols
- 1)) : 0;
5023 int h
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) : 0;
5028 // take into account editor if shown
5029 if ( IsCellEditControlShown() )
5032 int r
= m_currentCellCoords
.GetRow();
5033 int c
= m_currentCellCoords
.GetCol();
5034 int x
= GetColLeft(c
);
5035 int y
= GetRowTop(r
);
5037 // how big is the editor
5038 wxGridCellAttr
* attr
= GetCellAttr(r
, c
);
5039 wxGridCellEditor
* editor
= attr
->GetEditor(this, r
, c
);
5040 editor
->GetControl()->GetSize(&w2
, &h2
);
5051 // preserve (more or less) the previous position
5053 GetViewStart( &x
, &y
);
5055 // ensure the position is valid for the new scroll ranges
5057 x
= wxMax( w
- 1, 0 );
5059 y
= wxMax( h
- 1, 0 );
5061 // update the virtual size and refresh the scrollbars to reflect it
5062 m_gridWin
->SetVirtualSize(w
, h
);
5066 // if our OnSize() hadn't been called (it would if we have scrollbars), we
5067 // still must reposition the children
5071 wxSize
wxGrid::GetSizeAvailableForScrollTarget(const wxSize
& size
)
5073 wxSize
sizeGridWin(size
);
5074 sizeGridWin
.x
-= m_rowLabelWidth
;
5075 sizeGridWin
.y
-= m_colLabelHeight
;
5080 void wxGrid::CalcWindowSizes()
5082 // escape if the window is has not been fully created yet
5084 if ( m_cornerLabelWin
== NULL
)
5088 GetClientSize( &cw
, &ch
);
5090 // the grid may be too small to have enough space for the labels yet, don't
5091 // size the windows to negative sizes in this case
5092 int gw
= cw
- m_rowLabelWidth
;
5093 int gh
= ch
- m_colLabelHeight
;
5099 if ( m_cornerLabelWin
&& m_cornerLabelWin
->IsShown() )
5100 m_cornerLabelWin
->SetSize( 0, 0, m_rowLabelWidth
, m_colLabelHeight
);
5102 if ( m_colWindow
&& m_colWindow
->IsShown() )
5103 m_colWindow
->SetSize( m_rowLabelWidth
, 0, gw
, m_colLabelHeight
);
5105 if ( m_rowLabelWin
&& m_rowLabelWin
->IsShown() )
5106 m_rowLabelWin
->SetSize( 0, m_colLabelHeight
, m_rowLabelWidth
, gh
);
5108 if ( m_gridWin
&& m_gridWin
->IsShown() )
5109 m_gridWin
->SetSize( m_rowLabelWidth
, m_colLabelHeight
, gw
, gh
);
5112 // this is called when the grid table sends a message
5113 // to indicate that it has been redimensioned
5115 bool wxGrid::Redimension( wxGridTableMessage
& msg
)
5118 bool result
= false;
5120 // Clear the attribute cache as the attribute might refer to a different
5121 // cell than stored in the cache after adding/removing rows/columns.
5124 // By the same reasoning, the editor should be dismissed if columns are
5125 // added or removed. And for consistency, it should IMHO always be
5126 // removed, not only if the cell "underneath" it actually changes.
5127 // For now, I intentionally do not save the editor's content as the
5128 // cell it might want to save that stuff to might no longer exist.
5129 HideCellEditControl();
5131 switch ( msg
.GetId() )
5133 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
5135 size_t pos
= msg
.GetCommandInt();
5136 int numRows
= msg
.GetCommandInt2();
5138 m_numRows
+= numRows
;
5140 if ( !m_rowHeights
.IsEmpty() )
5142 m_rowHeights
.Insert( m_defaultRowHeight
, pos
, numRows
);
5143 m_rowBottoms
.Insert( 0, pos
, numRows
);
5147 bottom
= m_rowBottoms
[pos
- 1];
5149 for ( i
= pos
; i
< m_numRows
; i
++ )
5151 bottom
+= m_rowHeights
[i
];
5152 m_rowBottoms
[i
] = bottom
;
5156 if ( m_currentCellCoords
== wxGridNoCellCoords
)
5158 // if we have just inserted cols into an empty grid the current
5159 // cell will be undefined...
5161 SetCurrentCell( 0, 0 );
5165 m_selection
->UpdateRows( pos
, numRows
);
5166 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
5168 attrProvider
->UpdateAttrRows( pos
, numRows
);
5170 if ( !GetBatchCount() )
5173 m_rowLabelWin
->Refresh();
5179 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
5181 int numRows
= msg
.GetCommandInt();
5182 int oldNumRows
= m_numRows
;
5183 m_numRows
+= numRows
;
5185 if ( !m_rowHeights
.IsEmpty() )
5187 m_rowHeights
.Add( m_defaultRowHeight
, numRows
);
5188 m_rowBottoms
.Add( 0, numRows
);
5191 if ( oldNumRows
> 0 )
5192 bottom
= m_rowBottoms
[oldNumRows
- 1];
5194 for ( i
= oldNumRows
; i
< m_numRows
; i
++ )
5196 bottom
+= m_rowHeights
[i
];
5197 m_rowBottoms
[i
] = bottom
;
5201 if ( m_currentCellCoords
== wxGridNoCellCoords
)
5203 // if we have just inserted cols into an empty grid the current
5204 // cell will be undefined...
5206 SetCurrentCell( 0, 0 );
5209 if ( !GetBatchCount() )
5212 m_rowLabelWin
->Refresh();
5218 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
5220 size_t pos
= msg
.GetCommandInt();
5221 int numRows
= msg
.GetCommandInt2();
5222 m_numRows
-= numRows
;
5224 if ( !m_rowHeights
.IsEmpty() )
5226 m_rowHeights
.RemoveAt( pos
, numRows
);
5227 m_rowBottoms
.RemoveAt( pos
, numRows
);
5230 for ( i
= 0; i
< m_numRows
; i
++ )
5232 h
+= m_rowHeights
[i
];
5233 m_rowBottoms
[i
] = h
;
5239 m_currentCellCoords
= wxGridNoCellCoords
;
5243 if ( m_currentCellCoords
.GetRow() >= m_numRows
)
5244 m_currentCellCoords
.Set( 0, 0 );
5248 m_selection
->UpdateRows( pos
, -((int)numRows
) );
5249 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
5252 attrProvider
->UpdateAttrRows( pos
, -((int)numRows
) );
5254 // ifdef'd out following patch from Paul Gammans
5256 // No need to touch column attributes, unless we
5257 // removed _all_ rows, in this case, we remove
5258 // all column attributes.
5259 // I hate to do this here, but the
5260 // needed data is not available inside UpdateAttrRows.
5261 if ( !GetNumberRows() )
5262 attrProvider
->UpdateAttrCols( 0, -GetNumberCols() );
5266 if ( !GetBatchCount() )
5269 m_rowLabelWin
->Refresh();
5275 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
5277 size_t pos
= msg
.GetCommandInt();
5278 int numCols
= msg
.GetCommandInt2();
5279 m_numCols
+= numCols
;
5281 if ( m_useNativeHeader
)
5282 GetGridColHeader()->SetColumnCount(m_numCols
);
5284 if ( !m_colAt
.IsEmpty() )
5286 //Shift the column IDs
5288 for ( i
= 0; i
< m_numCols
- numCols
; i
++ )
5290 if ( m_colAt
[i
] >= (int)pos
)
5291 m_colAt
[i
] += numCols
;
5294 m_colAt
.Insert( pos
, pos
, numCols
);
5296 //Set the new columns' positions
5297 for ( i
= pos
+ 1; i
< (int)pos
+ numCols
; i
++ )
5303 if ( !m_colWidths
.IsEmpty() )
5305 m_colWidths
.Insert( m_defaultColWidth
, pos
, numCols
);
5306 m_colRights
.Insert( 0, pos
, numCols
);
5310 right
= m_colRights
[GetColAt( pos
- 1 )];
5313 for ( colPos
= pos
; colPos
< m_numCols
; colPos
++ )
5315 i
= GetColAt( colPos
);
5317 right
+= m_colWidths
[i
];
5318 m_colRights
[i
] = right
;
5322 if ( m_currentCellCoords
== wxGridNoCellCoords
)
5324 // if we have just inserted cols into an empty grid the current
5325 // cell will be undefined...
5327 SetCurrentCell( 0, 0 );
5331 m_selection
->UpdateCols( pos
, numCols
);
5332 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
5334 attrProvider
->UpdateAttrCols( pos
, numCols
);
5335 if ( !GetBatchCount() )
5338 m_colWindow
->Refresh();
5344 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
5346 int numCols
= msg
.GetCommandInt();
5347 int oldNumCols
= m_numCols
;
5348 m_numCols
+= numCols
;
5349 if ( m_useNativeHeader
)
5350 GetGridColHeader()->SetColumnCount(m_numCols
);
5352 if ( !m_colAt
.IsEmpty() )
5354 m_colAt
.Add( 0, numCols
);
5356 //Set the new columns' positions
5358 for ( i
= oldNumCols
; i
< m_numCols
; i
++ )
5364 if ( !m_colWidths
.IsEmpty() )
5366 m_colWidths
.Add( m_defaultColWidth
, numCols
);
5367 m_colRights
.Add( 0, numCols
);
5370 if ( oldNumCols
> 0 )
5371 right
= m_colRights
[GetColAt( oldNumCols
- 1 )];
5374 for ( colPos
= oldNumCols
; colPos
< m_numCols
; colPos
++ )
5376 i
= GetColAt( colPos
);
5378 right
+= m_colWidths
[i
];
5379 m_colRights
[i
] = right
;
5383 if ( m_currentCellCoords
== wxGridNoCellCoords
)
5385 // if we have just inserted cols into an empty grid the current
5386 // cell will be undefined...
5388 SetCurrentCell( 0, 0 );
5390 if ( !GetBatchCount() )
5393 m_colWindow
->Refresh();
5399 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
5401 size_t pos
= msg
.GetCommandInt();
5402 int numCols
= msg
.GetCommandInt2();
5403 m_numCols
-= numCols
;
5404 if ( m_useNativeHeader
)
5405 GetGridColHeader()->SetColumnCount(m_numCols
);
5407 if ( !m_colAt
.IsEmpty() )
5409 int colID
= GetColAt( pos
);
5411 m_colAt
.RemoveAt( pos
, numCols
);
5413 //Shift the column IDs
5415 for ( colPos
= 0; colPos
< m_numCols
; colPos
++ )
5417 if ( m_colAt
[colPos
] > colID
)
5418 m_colAt
[colPos
] -= numCols
;
5422 if ( !m_colWidths
.IsEmpty() )
5424 m_colWidths
.RemoveAt( pos
, numCols
);
5425 m_colRights
.RemoveAt( pos
, numCols
);
5429 for ( colPos
= 0; colPos
< m_numCols
; colPos
++ )
5431 i
= GetColAt( colPos
);
5433 w
+= m_colWidths
[i
];
5440 m_currentCellCoords
= wxGridNoCellCoords
;
5444 if ( m_currentCellCoords
.GetCol() >= m_numCols
)
5445 m_currentCellCoords
.Set( 0, 0 );
5449 m_selection
->UpdateCols( pos
, -((int)numCols
) );
5450 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
5453 attrProvider
->UpdateAttrCols( pos
, -((int)numCols
) );
5455 // ifdef'd out following patch from Paul Gammans
5457 // No need to touch row attributes, unless we
5458 // removed _all_ columns, in this case, we remove
5459 // all row attributes.
5460 // I hate to do this here, but the
5461 // needed data is not available inside UpdateAttrCols.
5462 if ( !GetNumberCols() )
5463 attrProvider
->UpdateAttrRows( 0, -GetNumberRows() );
5467 if ( !GetBatchCount() )
5470 m_colWindow
->Refresh();
5477 if (result
&& !GetBatchCount() )
5478 m_gridWin
->Refresh();
5483 wxArrayInt
wxGrid::CalcRowLabelsExposed( const wxRegion
& reg
) const
5485 wxRegionIterator
iter( reg
);
5488 wxArrayInt rowlabels
;
5495 // TODO: remove this when we can...
5496 // There is a bug in wxMotif that gives garbage update
5497 // rectangles if you jump-scroll a long way by clicking the
5498 // scrollbar with middle button. This is a work-around
5500 #if defined(__WXMOTIF__)
5502 m_gridWin
->GetClientSize( &cw
, &ch
);
5503 if ( r
.GetTop() > ch
)
5505 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
5508 // logical bounds of update region
5511 CalcUnscrolledPosition( 0, r
.GetTop(), &dummy
, &top
);
5512 CalcUnscrolledPosition( 0, r
.GetBottom(), &dummy
, &bottom
);
5514 // find the row labels within these bounds
5517 for ( row
= internalYToRow(top
); row
< m_numRows
; row
++ )
5519 if ( GetRowBottom(row
) < top
)
5522 if ( GetRowTop(row
) > bottom
)
5525 rowlabels
.Add( row
);
5534 wxArrayInt
wxGrid::CalcColLabelsExposed( const wxRegion
& reg
) const
5536 wxRegionIterator
iter( reg
);
5539 wxArrayInt colLabels
;
5546 // TODO: remove this when we can...
5547 // There is a bug in wxMotif that gives garbage update
5548 // rectangles if you jump-scroll a long way by clicking the
5549 // scrollbar with middle button. This is a work-around
5551 #if defined(__WXMOTIF__)
5553 m_gridWin
->GetClientSize( &cw
, &ch
);
5554 if ( r
.GetLeft() > cw
)
5556 r
.SetRight( wxMin( r
.GetRight(), cw
) );
5559 // logical bounds of update region
5562 CalcUnscrolledPosition( r
.GetLeft(), 0, &left
, &dummy
);
5563 CalcUnscrolledPosition( r
.GetRight(), 0, &right
, &dummy
);
5565 // find the cells within these bounds
5569 for ( colPos
= GetColPos( internalXToCol(left
) ); colPos
< m_numCols
; colPos
++ )
5571 col
= GetColAt( colPos
);
5573 if ( GetColRight(col
) < left
)
5576 if ( GetColLeft(col
) > right
)
5579 colLabels
.Add( col
);
5588 wxGridCellCoordsArray
wxGrid::CalcCellsExposed( const wxRegion
& reg
) const
5590 wxRegionIterator
iter( reg
);
5593 wxGridCellCoordsArray cellsExposed
;
5595 int left
, top
, right
, bottom
;
5600 // TODO: remove this when we can...
5601 // There is a bug in wxMotif that gives garbage update
5602 // rectangles if you jump-scroll a long way by clicking the
5603 // scrollbar with middle button. This is a work-around
5605 #if defined(__WXMOTIF__)
5607 m_gridWin
->GetClientSize( &cw
, &ch
);
5608 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
5609 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
5610 r
.SetRight( wxMin( r
.GetRight(), cw
) );
5611 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
5614 // logical bounds of update region
5616 CalcUnscrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
5617 CalcUnscrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
5619 // find the cells within these bounds
5621 for ( int row
= internalYToRow(top
); row
< m_numRows
; row
++ )
5623 if ( GetRowBottom(row
) <= top
)
5626 if ( GetRowTop(row
) > bottom
)
5629 // add all dirty cells in this row: notice that the columns which
5630 // are dirty don't depend on the row so we compute them only once
5631 // for the first dirty row and then reuse for all the next ones
5634 // do determine the dirty columns
5635 for ( int pos
= XToPos(left
); pos
<= XToPos(right
); pos
++ )
5636 cols
.push_back(GetColAt(pos
));
5638 // if there are no dirty columns at all, nothing to do
5643 const size_t count
= cols
.size();
5644 for ( size_t n
= 0; n
< count
; n
++ )
5645 cellsExposed
.Add(wxGridCellCoords(row
, cols
[n
]));
5651 return cellsExposed
;
5655 void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent
& event
)
5658 wxPoint
pos( event
.GetPosition() );
5659 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
5661 if ( event
.Dragging() )
5665 m_isDragging
= true;
5666 m_rowLabelWin
->CaptureMouse();
5669 if ( event
.LeftIsDown() )
5671 switch ( m_cursorMode
)
5673 case WXGRID_CURSOR_RESIZE_ROW
:
5675 int cw
, ch
, left
, dummy
;
5676 m_gridWin
->GetClientSize( &cw
, &ch
);
5677 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
5679 wxClientDC
dc( m_gridWin
);
5682 GetRowTop(m_dragRowOrCol
) +
5683 GetRowMinimalHeight(m_dragRowOrCol
) );
5684 dc
.SetLogicalFunction(wxINVERT
);
5685 if ( m_dragLastPos
>= 0 )
5687 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
5689 dc
.DrawLine( left
, y
, left
+cw
, y
);
5694 case WXGRID_CURSOR_SELECT_ROW
:
5696 if ( (row
= YToRow( y
)) >= 0 )
5699 m_selection
->SelectRow(row
, event
);
5704 // default label to suppress warnings about "enumeration value
5705 // 'xxx' not handled in switch
5713 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
5718 if (m_rowLabelWin
->HasCapture())
5719 m_rowLabelWin
->ReleaseMouse();
5720 m_isDragging
= false;
5723 // ------------ Entering or leaving the window
5725 if ( event
.Entering() || event
.Leaving() )
5727 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
5730 // ------------ Left button pressed
5732 else if ( event
.LeftDown() )
5734 // don't send a label click event for a hit on the
5735 // edge of the row label - this is probably the user
5736 // wanting to resize the row
5738 if ( YToEdgeOfRow(y
) < 0 )
5742 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, row
, -1, event
) )
5744 if ( !event
.ShiftDown() && !event
.CmdDown() )
5748 if ( event
.ShiftDown() )
5750 m_selection
->SelectBlock
5752 m_currentCellCoords
.GetRow(), 0,
5753 row
, GetNumberCols() - 1,
5759 m_selection
->SelectRow(row
, event
);
5763 ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW
, m_rowLabelWin
);
5768 // starting to drag-resize a row
5769 if ( CanDragRowSize() )
5770 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
);
5774 // ------------ Left double click
5776 else if (event
.LeftDClick() )
5778 row
= YToEdgeOfRow(y
);
5783 !SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, row
, -1, event
) )
5785 // no default action at the moment
5790 // adjust row height depending on label text
5791 AutoSizeRowLabelSize( row
);
5793 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, GetColLabelWindow());
5798 // ------------ Left button released
5800 else if ( event
.LeftUp() )
5802 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
5804 DoEndDragResizeRow();
5806 // Note: we are ending the event *after* doing
5807 // default processing in this case
5809 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
5812 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
5816 // ------------ Right button down
5818 else if ( event
.RightDown() )
5822 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, row
, -1, event
) )
5824 // no default action at the moment
5828 // ------------ Right double click
5830 else if ( event
.RightDClick() )
5834 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, row
, -1, event
) )
5836 // no default action at the moment
5840 // ------------ No buttons down and mouse moving
5842 else if ( event
.Moving() )
5844 m_dragRowOrCol
= YToEdgeOfRow( y
);
5845 if ( m_dragRowOrCol
>= 0 )
5847 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5849 // don't capture the mouse yet
5850 if ( CanDragRowSize() )
5851 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
, false);
5854 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5856 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
, false);
5861 void wxGrid::UpdateColumnSortingIndicator(int col
)
5863 wxCHECK_RET( col
!= wxNOT_FOUND
, "invalid column index" );
5865 if ( m_useNativeHeader
)
5866 GetGridColHeader()->UpdateColumn(col
);
5867 else if ( m_nativeColumnLabels
)
5868 m_colWindow
->Refresh();
5869 //else: sorting indicator display not yet implemented in grid version
5872 void wxGrid::SetSortingColumn(int col
, bool ascending
)
5874 if ( col
== m_sortCol
)
5876 // we are already using this column for sorting (or not sorting at all)
5877 // but we might still change the sorting order, check for it
5878 if ( m_sortCol
!= wxNOT_FOUND
&& ascending
!= m_sortIsAscending
)
5880 m_sortIsAscending
= ascending
;
5882 UpdateColumnSortingIndicator(m_sortCol
);
5885 else // we're changing the column used for sorting
5887 const int sortColOld
= m_sortCol
;
5889 // change it before updating the column as we want GetSortingColumn()
5890 // to return the correct new value
5893 if ( sortColOld
!= wxNOT_FOUND
)
5894 UpdateColumnSortingIndicator(sortColOld
);
5896 if ( m_sortCol
!= wxNOT_FOUND
)
5898 m_sortIsAscending
= ascending
;
5899 UpdateColumnSortingIndicator(m_sortCol
);
5904 void wxGrid::DoColHeaderClick(int col
)
5906 // we consider that the grid was resorted if this event is processed and
5908 if ( SendEvent(wxEVT_GRID_COL_SORT
, -1, col
) == 1 )
5910 SetSortingColumn(col
, IsSortingBy(col
) ? !m_sortIsAscending
: true);
5915 void wxGrid::DoStartResizeCol(int col
)
5917 m_dragRowOrCol
= col
;
5919 DoUpdateResizeColWidth(GetColWidth(m_dragRowOrCol
));
5922 void wxGrid::DoUpdateResizeCol(int x
)
5924 int cw
, ch
, dummy
, top
;
5925 m_gridWin
->GetClientSize( &cw
, &ch
);
5926 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
5928 wxClientDC
dc( m_gridWin
);
5931 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) + GetColMinimalWidth(m_dragRowOrCol
));
5932 dc
.SetLogicalFunction(wxINVERT
);
5933 if ( m_dragLastPos
>= 0 )
5935 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ ch
);
5937 dc
.DrawLine( x
, top
, x
, top
+ ch
);
5941 void wxGrid::DoUpdateResizeColWidth(int w
)
5943 DoUpdateResizeCol(GetColLeft(m_dragRowOrCol
) + w
);
5946 void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent
& event
)
5949 wxPoint
pos( event
.GetPosition() );
5950 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
5952 int col
= XToCol(x
);
5953 if ( event
.Dragging() )
5957 m_isDragging
= true;
5958 GetColLabelWindow()->CaptureMouse();
5960 if ( m_cursorMode
== WXGRID_CURSOR_MOVE_COL
&& col
!= -1 )
5961 DoStartMoveCol(col
);
5964 if ( event
.LeftIsDown() )
5966 switch ( m_cursorMode
)
5968 case WXGRID_CURSOR_RESIZE_COL
:
5969 DoUpdateResizeCol(x
);
5972 case WXGRID_CURSOR_SELECT_COL
:
5977 m_selection
->SelectCol(col
, event
);
5982 case WXGRID_CURSOR_MOVE_COL
:
5984 int posNew
= XToPos(x
);
5985 int colNew
= GetColAt(posNew
);
5987 // determine the position of the drop marker
5989 if ( x
>= GetColLeft(colNew
) + (GetColWidth(colNew
) / 2) )
5990 markerX
= GetColRight(colNew
);
5992 markerX
= GetColLeft(colNew
);
5994 if ( markerX
!= m_dragLastPos
)
5996 wxClientDC
dc( GetColLabelWindow() );
6000 GetColLabelWindow()->GetClientSize( &cw
, &ch
);
6004 //Clean up the last indicator
6005 if ( m_dragLastPos
>= 0 )
6007 wxPen
pen( GetColLabelWindow()->GetBackgroundColour(), 2 );
6009 dc
.DrawLine( m_dragLastPos
+ 1, 0, m_dragLastPos
+ 1, ch
);
6010 dc
.SetPen(wxNullPen
);
6012 if ( XToCol( m_dragLastPos
) != -1 )
6013 DrawColLabel( dc
, XToCol( m_dragLastPos
) );
6016 const wxColour
*color
;
6017 //Moving to the same place? Don't draw a marker
6018 if ( colNew
== m_dragRowOrCol
)
6019 color
= wxLIGHT_GREY
;
6024 wxPen
pen( *color
, 2 );
6027 dc
.DrawLine( markerX
, 0, markerX
, ch
);
6029 dc
.SetPen(wxNullPen
);
6031 m_dragLastPos
= markerX
- 1;
6036 // default label to suppress warnings about "enumeration value
6037 // 'xxx' not handled in switch
6045 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
6050 if (GetColLabelWindow()->HasCapture())
6051 GetColLabelWindow()->ReleaseMouse();
6052 m_isDragging
= false;
6055 // ------------ Entering or leaving the window
6057 if ( event
.Entering() || event
.Leaving() )
6059 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, GetColLabelWindow());
6062 // ------------ Left button pressed
6064 else if ( event
.LeftDown() )
6066 // don't send a label click event for a hit on the
6067 // edge of the col label - this is probably the user
6068 // wanting to resize the col
6070 if ( XToEdgeOfCol(x
) < 0 )
6073 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, col
, event
) )
6075 if ( m_canDragColMove
)
6077 //Show button as pressed
6078 wxClientDC
dc( GetColLabelWindow() );
6079 int colLeft
= GetColLeft( col
);
6080 int colRight
= GetColRight( col
) - 1;
6081 dc
.SetPen( wxPen( GetColLabelWindow()->GetBackgroundColour(), 1 ) );
6082 dc
.DrawLine( colLeft
, 1, colLeft
, m_colLabelHeight
-1 );
6083 dc
.DrawLine( colLeft
, 1, colRight
, 1 );
6085 ChangeCursorMode(WXGRID_CURSOR_MOVE_COL
, GetColLabelWindow());
6089 if ( !event
.ShiftDown() && !event
.CmdDown() )
6093 if ( event
.ShiftDown() )
6095 m_selection
->SelectBlock
6097 0, m_currentCellCoords
.GetCol(),
6098 GetNumberRows() - 1, col
,
6104 m_selection
->SelectCol(col
, event
);
6108 ChangeCursorMode(WXGRID_CURSOR_SELECT_COL
, GetColLabelWindow());
6114 // starting to drag-resize a col
6116 if ( CanDragColSize() )
6117 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, GetColLabelWindow());
6121 // ------------ Left double click
6123 if ( event
.LeftDClick() )
6125 const int colEdge
= XToEdgeOfCol(x
);
6126 if ( colEdge
== -1 )
6129 ! SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, col
, event
) )
6131 // no default action at the moment
6136 // adjust column width depending on label text
6137 AutoSizeColLabelSize( colEdge
);
6139 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, GetColLabelWindow());
6144 // ------------ Left button released
6146 else if ( event
.LeftUp() )
6148 switch ( m_cursorMode
)
6150 case WXGRID_CURSOR_RESIZE_COL
:
6151 DoEndDragResizeCol();
6154 case WXGRID_CURSOR_MOVE_COL
:
6155 if ( m_dragLastPos
== -1 || col
== m_dragRowOrCol
)
6157 // the column didn't actually move anywhere
6159 DoColHeaderClick(col
);
6160 m_colWindow
->Refresh(); // "unpress" the column
6164 DoEndMoveCol(XToPos(x
));
6168 case WXGRID_CURSOR_SELECT_COL
:
6169 case WXGRID_CURSOR_SELECT_CELL
:
6170 case WXGRID_CURSOR_RESIZE_ROW
:
6171 case WXGRID_CURSOR_SELECT_ROW
:
6173 DoColHeaderClick(col
);
6177 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, GetColLabelWindow());
6181 // ------------ Right button down
6183 else if ( event
.RightDown() )
6186 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, col
, event
) )
6188 // no default action at the moment
6192 // ------------ Right double click
6194 else if ( event
.RightDClick() )
6197 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, col
, event
) )
6199 // no default action at the moment
6203 // ------------ No buttons down and mouse moving
6205 else if ( event
.Moving() )
6207 m_dragRowOrCol
= XToEdgeOfCol( x
);
6208 if ( m_dragRowOrCol
>= 0 )
6210 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
6212 // don't capture the cursor yet
6213 if ( CanDragColSize() )
6214 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, GetColLabelWindow(), false);
6217 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
6219 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, GetColLabelWindow(), false);
6224 void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent
& event
)
6226 if ( event
.LeftDown() )
6228 // indicate corner label by having both row and
6231 if ( !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, -1, event
) )
6236 else if ( event
.LeftDClick() )
6238 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, -1, event
);
6240 else if ( event
.RightDown() )
6242 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, -1, event
) )
6244 // no default action at the moment
6247 else if ( event
.RightDClick() )
6249 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, -1, event
) )
6251 // no default action at the moment
6256 void wxGrid::CancelMouseCapture()
6258 // cancel operation currently in progress, whatever it is
6261 m_isDragging
= false;
6262 m_startDragPos
= wxDefaultPosition
;
6264 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
6265 m_winCapture
->SetCursor( *wxSTANDARD_CURSOR
);
6266 m_winCapture
= NULL
;
6268 // remove traces of whatever we drew on screen
6273 void wxGrid::ChangeCursorMode(CursorMode mode
,
6278 static const wxChar
*cursorModes
[] =
6288 wxLogTrace(_T("grid"),
6289 _T("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
6290 win
== m_colWindow
? _T("colLabelWin")
6291 : win
? _T("rowLabelWin")
6293 cursorModes
[m_cursorMode
], cursorModes
[mode
]);
6296 if ( mode
== m_cursorMode
&&
6297 win
== m_winCapture
&&
6298 captureMouse
== (m_winCapture
!= NULL
))
6303 // by default use the grid itself
6309 m_winCapture
->ReleaseMouse();
6310 m_winCapture
= NULL
;
6313 m_cursorMode
= mode
;
6315 switch ( m_cursorMode
)
6317 case WXGRID_CURSOR_RESIZE_ROW
:
6318 win
->SetCursor( m_rowResizeCursor
);
6321 case WXGRID_CURSOR_RESIZE_COL
:
6322 win
->SetCursor( m_colResizeCursor
);
6325 case WXGRID_CURSOR_MOVE_COL
:
6326 win
->SetCursor( wxCursor(wxCURSOR_HAND
) );
6330 win
->SetCursor( *wxSTANDARD_CURSOR
);
6334 // we need to capture mouse when resizing
6335 bool resize
= m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
||
6336 m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
;
6338 if ( captureMouse
&& resize
)
6340 win
->CaptureMouse();
6345 // ----------------------------------------------------------------------------
6346 // grid mouse event processing
6347 // ----------------------------------------------------------------------------
6350 wxGrid::DoGridCellDrag(wxMouseEvent
& event
,
6351 const wxGridCellCoords
& coords
,
6354 if ( coords
== wxGridNoCellCoords
)
6355 return; // we're outside any valid cell
6357 // Hide the edit control, so it won't interfere with drag-shrinking.
6358 if ( IsCellEditControlShown() )
6360 HideCellEditControl();
6361 SaveEditControlValue();
6364 switch ( event
.GetModifiers() )
6367 if ( m_selectedBlockCorner
== wxGridNoCellCoords
)
6368 m_selectedBlockCorner
= coords
;
6369 UpdateBlockBeingSelected(m_selectedBlockCorner
, coords
);
6373 if ( CanDragCell() )
6377 if ( m_selectedBlockCorner
== wxGridNoCellCoords
)
6378 m_selectedBlockCorner
= coords
;
6380 SendEvent(wxEVT_GRID_CELL_BEGIN_DRAG
, coords
, event
);
6385 UpdateBlockBeingSelected(m_currentCellCoords
, coords
);
6389 // we don't handle the other key modifiers
6394 void wxGrid::DoGridLineDrag(wxMouseEvent
& event
, const wxGridOperations
& oper
)
6396 wxClientDC
dc(m_gridWin
);
6398 dc
.SetLogicalFunction(wxINVERT
);
6400 const wxRect
rectWin(CalcUnscrolledPosition(wxPoint(0, 0)),
6401 m_gridWin
->GetClientSize());
6403 // erase the previously drawn line, if any
6404 if ( m_dragLastPos
>= 0 )
6405 oper
.DrawParallelLineInRect(dc
, rectWin
, m_dragLastPos
);
6407 // we need the vertical position for rows and horizontal for columns here
6408 m_dragLastPos
= oper
.Dual().Select(CalcUnscrolledPosition(event
.GetPosition()));
6410 // don't allow resizing beneath the minimal size
6411 const int posMin
= oper
.GetLineStartPos(this, m_dragRowOrCol
) +
6412 oper
.GetMinimalLineSize(this, m_dragRowOrCol
);
6413 if ( m_dragLastPos
< posMin
)
6414 m_dragLastPos
= posMin
;
6416 // and draw it at the new position
6417 oper
.DrawParallelLineInRect(dc
, rectWin
, m_dragLastPos
);
6420 void wxGrid::DoGridDragEvent(wxMouseEvent
& event
, const wxGridCellCoords
& coords
)
6422 if ( !m_isDragging
)
6424 // Don't start doing anything until the mouse has been dragged far
6426 const wxPoint
& pt
= event
.GetPosition();
6427 if ( m_startDragPos
== wxDefaultPosition
)
6429 m_startDragPos
= pt
;
6433 if ( abs(m_startDragPos
.x
- pt
.x
) <= DRAG_SENSITIVITY
&&
6434 abs(m_startDragPos
.y
- pt
.y
) <= DRAG_SENSITIVITY
)
6438 const bool isFirstDrag
= !m_isDragging
;
6439 m_isDragging
= true;
6441 switch ( m_cursorMode
)
6443 case WXGRID_CURSOR_SELECT_CELL
:
6444 DoGridCellDrag(event
, coords
, isFirstDrag
);
6447 case WXGRID_CURSOR_RESIZE_ROW
:
6448 DoGridLineDrag(event
, wxGridRowOperations());
6451 case WXGRID_CURSOR_RESIZE_COL
:
6452 DoGridLineDrag(event
, wxGridColumnOperations());
6461 m_winCapture
= m_gridWin
;
6462 m_winCapture
->CaptureMouse();
6467 wxGrid::DoGridCellLeftDown(wxMouseEvent
& event
,
6468 const wxGridCellCoords
& coords
,
6471 if ( SendEvent(wxEVT_GRID_CELL_LEFT_CLICK
, coords
, event
) )
6473 // event handled by user code, no need to do anything here
6477 if ( !event
.CmdDown() )
6480 if ( event
.ShiftDown() )
6484 m_selection
->SelectBlock(m_currentCellCoords
, coords
, event
);
6485 m_selectedBlockCorner
= coords
;
6488 else if ( XToEdgeOfCol(pos
.x
) < 0 && YToEdgeOfRow(pos
.y
) < 0 )
6490 DisableCellEditControl();
6491 MakeCellVisible( coords
);
6493 if ( event
.CmdDown() )
6497 m_selection
->ToggleCellSelection(coords
, event
);
6500 m_selectedBlockTopLeft
= wxGridNoCellCoords
;
6501 m_selectedBlockBottomRight
= wxGridNoCellCoords
;
6502 m_selectedBlockCorner
= coords
;
6506 m_waitForSlowClick
= m_currentCellCoords
== coords
&&
6507 coords
!= wxGridNoCellCoords
;
6508 SetCurrentCell( coords
);
6514 wxGrid::DoGridCellLeftDClick(wxMouseEvent
& event
,
6515 const wxGridCellCoords
& coords
,
6518 if ( XToEdgeOfCol(pos
.x
) < 0 && YToEdgeOfRow(pos
.y
) < 0 )
6520 if ( !SendEvent(wxEVT_GRID_CELL_LEFT_DCLICK
, coords
, event
) )
6522 // we want double click to select a cell and start editing
6523 // (i.e. to behave in same way as sequence of two slow clicks):
6524 m_waitForSlowClick
= true;
6530 wxGrid::DoGridCellLeftUp(wxMouseEvent
& event
, const wxGridCellCoords
& coords
)
6532 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
6536 m_winCapture
->ReleaseMouse();
6537 m_winCapture
= NULL
;
6540 if ( coords
== m_currentCellCoords
&& m_waitForSlowClick
&& CanEnableCellControl() )
6543 EnableCellEditControl();
6545 wxGridCellAttr
*attr
= GetCellAttr(coords
);
6546 wxGridCellEditor
*editor
= attr
->GetEditor(this, coords
.GetRow(), coords
.GetCol());
6547 editor
->StartingClick();
6551 m_waitForSlowClick
= false;
6553 else if ( m_selectedBlockTopLeft
!= wxGridNoCellCoords
&&
6554 m_selectedBlockBottomRight
!= wxGridNoCellCoords
)
6558 m_selection
->SelectBlock( m_selectedBlockTopLeft
,
6559 m_selectedBlockBottomRight
,
6563 m_selectedBlockTopLeft
= wxGridNoCellCoords
;
6564 m_selectedBlockBottomRight
= wxGridNoCellCoords
;
6566 // Show the edit control, if it has been hidden for
6568 ShowCellEditControl();
6571 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
6573 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6574 DoEndDragResizeRow();
6576 // Note: we are ending the event *after* doing
6577 // default processing in this case
6579 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
6581 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
6583 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6584 DoEndDragResizeCol();
6591 wxGrid::DoGridMouseMoveEvent(wxMouseEvent
& WXUNUSED(event
),
6592 const wxGridCellCoords
& coords
,
6595 if ( coords
.GetRow() < 0 || coords
.GetCol() < 0 )
6597 // out of grid cell area
6598 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6602 int dragRow
= YToEdgeOfRow( pos
.y
);
6603 int dragCol
= XToEdgeOfCol( pos
.x
);
6605 // Dragging on the corner of a cell to resize in both
6606 // directions is not implemented yet...
6608 if ( dragRow
>= 0 && dragCol
>= 0 )
6610 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6616 m_dragRowOrCol
= dragRow
;
6618 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
6620 if ( CanDragRowSize() && CanDragGridSize() )
6621 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, NULL
, false);
6624 // When using the native header window we can only resize the columns by
6625 // dragging the dividers in it because we can't make it enter into the
6626 // column resizing mode programmatically
6627 else if ( dragCol
>= 0 && !m_useNativeHeader
)
6629 m_dragRowOrCol
= dragCol
;
6631 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
6633 if ( CanDragColSize() && CanDragGridSize() )
6634 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, NULL
, false);
6637 else // Neither on a row or col edge
6639 if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
6641 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6646 void wxGrid::ProcessGridCellMouseEvent(wxMouseEvent
& event
)
6648 const wxPoint pos
= CalcUnscrolledPosition(event
.GetPosition());
6650 // coordinates of the cell under mouse
6651 wxGridCellCoords coords
= XYToCell(pos
);
6653 int cell_rows
, cell_cols
;
6654 GetCellSize( coords
.GetRow(), coords
.GetCol(), &cell_rows
, &cell_cols
);
6655 if ( (cell_rows
< 0) || (cell_cols
< 0) )
6657 coords
.SetRow(coords
.GetRow() + cell_rows
);
6658 coords
.SetCol(coords
.GetCol() + cell_cols
);
6661 if ( event
.Dragging() )
6663 if ( event
.LeftIsDown() )
6664 DoGridDragEvent(event
, coords
);
6670 m_isDragging
= false;
6671 m_startDragPos
= wxDefaultPosition
;
6673 // VZ: if we do this, the mode is reset to WXGRID_CURSOR_SELECT_CELL
6674 // immediately after it becomes WXGRID_CURSOR_RESIZE_ROW/COL under
6677 if ( event
.Entering() || event
.Leaving() )
6679 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6680 m_gridWin
->SetCursor( *wxSTANDARD_CURSOR
);
6684 // deal with various button presses
6685 if ( event
.IsButton() )
6687 if ( coords
!= wxGridNoCellCoords
)
6689 DisableCellEditControl();
6691 if ( event
.LeftDown() )
6692 DoGridCellLeftDown(event
, coords
, pos
);
6693 else if ( event
.LeftDClick() )
6694 DoGridCellLeftDClick(event
, coords
, pos
);
6695 else if ( event
.RightDown() )
6696 SendEvent(wxEVT_GRID_CELL_RIGHT_CLICK
, coords
, event
);
6697 else if ( event
.RightDClick() )
6698 SendEvent(wxEVT_GRID_CELL_RIGHT_DCLICK
, coords
, event
);
6701 // this one should be called even if we're not over any cell
6702 if ( event
.LeftUp() )
6704 DoGridCellLeftUp(event
, coords
);
6707 else if ( event
.Moving() )
6709 DoGridMouseMoveEvent(event
, coords
, pos
);
6711 else // unknown mouse event?
6717 void wxGrid::DoEndDragResizeLine(const wxGridOperations
& oper
)
6719 if ( m_dragLastPos
== -1 )
6722 const wxGridOperations
& doper
= oper
.Dual();
6724 const wxSize size
= m_gridWin
->GetClientSize();
6726 const wxPoint ptOrigin
= CalcUnscrolledPosition(wxPoint(0, 0));
6728 // erase the last line we drew
6729 wxClientDC
dc(m_gridWin
);
6731 dc
.SetLogicalFunction(wxINVERT
);
6733 const int posLineStart
= oper
.Select(ptOrigin
);
6734 const int posLineEnd
= oper
.Select(ptOrigin
) + oper
.Select(size
);
6736 oper
.DrawParallelLine(dc
, posLineStart
, posLineEnd
, m_dragLastPos
);
6738 // temporarily hide the edit control before resizing
6739 HideCellEditControl();
6740 SaveEditControlValue();
6742 // do resize the line
6743 const int lineStart
= oper
.GetLineStartPos(this, m_dragRowOrCol
);
6744 oper
.SetLineSize(this, m_dragRowOrCol
,
6745 wxMax(m_dragLastPos
- lineStart
,
6746 oper
.GetMinimalLineSize(this, m_dragRowOrCol
)));
6750 // refresh now if we're not frozen
6751 if ( !GetBatchCount() )
6753 // we need to refresh everything beyond the resized line in the header
6756 // get the position from which to refresh in the other direction
6757 wxRect
rect(CellToRect(oper
.MakeCoords(m_dragRowOrCol
, 0)));
6758 rect
.SetPosition(CalcScrolledPosition(rect
.GetPosition()));
6760 // we only need the ordinate (for rows) or abscissa (for columns) here,
6761 // and need to cover the entire window in the other direction
6762 oper
.Select(rect
) = 0;
6764 wxRect
rectHeader(rect
.GetPosition(),
6767 oper
.GetHeaderWindowSize(this),
6768 doper
.Select(size
) - doper
.Select(rect
)
6771 oper
.GetHeaderWindow(this)->Refresh(true, &rectHeader
);
6774 // also refresh the grid window: extend the rectangle
6777 oper
.SelectSize(rect
) = oper
.Select(size
);
6779 int subtractLines
= 0;
6780 const int lineStart
= oper
.PosToLine(this, posLineStart
);
6781 if ( lineStart
>= 0 )
6783 // ensure that if we have a multi-cell block we redraw all of
6784 // it by increasing the refresh area to cover it entirely if a
6785 // part of it is affected
6786 const int lineEnd
= oper
.PosToLine(this, posLineEnd
, true);
6787 for ( int line
= lineStart
; line
< lineEnd
; line
++ )
6789 int cellLines
= oper
.Select(
6790 GetCellSize(oper
.MakeCoords(m_dragRowOrCol
, line
)));
6791 if ( cellLines
< subtractLines
)
6792 subtractLines
= cellLines
;
6797 oper
.GetLineStartPos(this, m_dragRowOrCol
+ subtractLines
);
6798 startPos
= doper
.CalcScrolledPosition(this, startPos
);
6800 doper
.Select(rect
) = startPos
;
6801 doper
.SelectSize(rect
) = doper
.Select(size
) - startPos
;
6803 m_gridWin
->Refresh(false, &rect
);
6807 // show the edit control back again
6808 ShowCellEditControl();
6811 void wxGrid::DoEndDragResizeRow()
6813 DoEndDragResizeLine(wxGridRowOperations());
6816 void wxGrid::DoEndDragResizeCol(wxMouseEvent
*event
)
6818 DoEndDragResizeLine(wxGridColumnOperations());
6820 // Note: we are ending the event *after* doing
6821 // default processing in this case
6824 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, *event
);
6826 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
);
6829 void wxGrid::DoStartMoveCol(int col
)
6831 m_dragRowOrCol
= col
;
6834 void wxGrid::DoEndMoveCol(int pos
)
6836 wxASSERT_MSG( m_dragRowOrCol
!= -1, "no matching DoStartMoveCol?" );
6838 if ( SendEvent(wxEVT_GRID_COL_MOVE
, -1, m_dragRowOrCol
) != -1 )
6839 SetColPos(m_dragRowOrCol
, pos
);
6840 //else: vetoed by user
6842 m_dragRowOrCol
= -1;
6845 void wxGrid::RefreshAfterColPosChange()
6847 // recalculate the column rights as the column positions have changed,
6848 // unless we calculate them dynamically because all columns widths are the
6849 // same and it's easy to do
6850 if ( !m_colWidths
.empty() )
6853 for ( int colPos
= 0; colPos
< m_numCols
; colPos
++ )
6855 int colID
= GetColAt( colPos
);
6857 colRight
+= m_colWidths
[colID
];
6858 m_colRights
[colID
] = colRight
;
6862 // and make the changes visible
6863 if ( m_useNativeHeader
)
6865 if ( m_colAt
.empty() )
6866 GetGridColHeader()->ResetColumnsOrder();
6868 GetGridColHeader()->SetColumnsOrder(m_colAt
);
6872 m_colWindow
->Refresh();
6874 m_gridWin
->Refresh();
6877 void wxGrid::SetColPos(int idx
, int pos
)
6879 // we're going to need m_colAt now, initialize it if needed
6880 if ( m_colAt
.empty() )
6882 m_colAt
.reserve(m_numCols
);
6883 for ( int i
= 0; i
< m_numCols
; i
++ )
6884 m_colAt
.push_back(i
);
6887 wxHeaderCtrl::MoveColumnInOrderArray(m_colAt
, idx
, pos
);
6889 RefreshAfterColPosChange();
6892 void wxGrid::ResetColPos()
6896 RefreshAfterColPosChange();
6899 void wxGrid::EnableDragColMove( bool enable
)
6901 if ( m_canDragColMove
== enable
)
6904 if ( m_useNativeHeader
)
6906 // update all columns to make them [not] reorderable
6907 GetGridColHeader()->SetColumnCount(m_numCols
);
6910 m_canDragColMove
= enable
;
6912 // we use to call ResetColPos() from here if !enable but this doesn't seem
6913 // right as it would mean there would be no way to "freeze" the current
6914 // columns order by disabling moving them after putting them in the desired
6915 // order, whereas now you can always call ResetColPos() manually if needed
6920 // ------ interaction with data model
6922 bool wxGrid::ProcessTableMessage( wxGridTableMessage
& msg
)
6924 switch ( msg
.GetId() )
6926 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES
:
6927 return GetModelValues();
6929 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES
:
6930 return SetModelValues();
6932 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
6933 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
6934 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
6935 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
6936 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
6937 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
6938 return Redimension( msg
);
6945 // The behaviour of this function depends on the grid table class
6946 // Clear() function. For the default wxGridStringTable class the
6947 // behaviour is to replace all cell contents with wxEmptyString but
6948 // not to change the number of rows or cols.
6950 void wxGrid::ClearGrid()
6954 if (IsCellEditControlEnabled())
6955 DisableCellEditControl();
6958 if (!GetBatchCount())
6959 m_gridWin
->Refresh();
6964 wxGrid::DoModifyLines(bool (wxGridTableBase::*funcModify
)(size_t, size_t),
6965 int pos
, int num
, bool WXUNUSED(updateLabels
) )
6967 wxCHECK_MSG( m_created
, false, "must finish creating the grid first" );
6972 if ( IsCellEditControlEnabled() )
6973 DisableCellEditControl();
6975 return (m_table
->*funcModify
)(pos
, num
);
6977 // the table will have sent the results of the insert row
6978 // operation to this view object as a grid table message
6982 wxGrid::DoAppendLines(bool (wxGridTableBase::*funcAppend
)(size_t),
6983 int num
, bool WXUNUSED(updateLabels
))
6985 wxCHECK_MSG( m_created
, false, "must finish creating the grid first" );
6990 return (m_table
->*funcAppend
)(num
);
6994 // ----- event handlers
6997 // Generate a grid event based on a mouse event and return:
6998 // -1 if the event was vetoed
6999 // +1 if the event was processed (but not vetoed)
7000 // 0 if the event wasn't handled
7002 wxGrid::SendEvent(const wxEventType type
,
7004 wxMouseEvent
& mouseEv
)
7006 bool claimed
, vetoed
;
7008 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
7010 int rowOrCol
= (row
== -1 ? col
: row
);
7012 wxGridSizeEvent
gridEvt( GetId(),
7016 mouseEv
.GetX() + GetRowLabelSize(),
7017 mouseEv
.GetY() + GetColLabelSize(),
7020 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
7021 vetoed
= !gridEvt
.IsAllowed();
7023 else if ( type
== wxEVT_GRID_RANGE_SELECT
)
7025 // Right now, it should _never_ end up here!
7026 wxGridRangeSelectEvent
gridEvt( GetId(),
7029 m_selectedBlockTopLeft
,
7030 m_selectedBlockBottomRight
,
7034 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
7035 vetoed
= !gridEvt
.IsAllowed();
7037 else if ( type
== wxEVT_GRID_LABEL_LEFT_CLICK
||
7038 type
== wxEVT_GRID_LABEL_LEFT_DCLICK
||
7039 type
== wxEVT_GRID_LABEL_RIGHT_CLICK
||
7040 type
== wxEVT_GRID_LABEL_RIGHT_DCLICK
)
7042 wxPoint pos
= mouseEv
.GetPosition();
7044 if ( mouseEv
.GetEventObject() == GetGridRowLabelWindow() )
7045 pos
.y
+= GetColLabelSize();
7046 if ( mouseEv
.GetEventObject() == GetGridColLabelWindow() )
7047 pos
.x
+= GetRowLabelSize();
7049 wxGridEvent
gridEvt( GetId(),
7057 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
7058 vetoed
= !gridEvt
.IsAllowed();
7062 wxGridEvent
gridEvt( GetId(),
7066 mouseEv
.GetX() + GetRowLabelSize(),
7067 mouseEv
.GetY() + GetColLabelSize(),
7070 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
7071 vetoed
= !gridEvt
.IsAllowed();
7074 // A Veto'd event may not be `claimed' so test this first
7078 return claimed
? 1 : 0;
7081 // Generate a grid event of specified type, return value same as above
7083 int wxGrid::SendEvent(const wxEventType type
, int row
, int col
)
7085 bool claimed
, vetoed
;
7087 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
7089 int rowOrCol
= (row
== -1 ? col
: row
);
7091 wxGridSizeEvent
gridEvt( GetId(), type
, this, rowOrCol
);
7093 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
7094 vetoed
= !gridEvt
.IsAllowed();
7098 wxGridEvent
gridEvt( GetId(), type
, this, row
, col
);
7100 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
7101 vetoed
= !gridEvt
.IsAllowed();
7104 // A Veto'd event may not be `claimed' so test this first
7108 return claimed
? 1 : 0;
7111 void wxGrid::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
7113 // needed to prevent zillions of paint events on MSW
7117 void wxGrid::Refresh(bool eraseb
, const wxRect
* rect
)
7119 // Don't do anything if between Begin/EndBatch...
7120 // EndBatch() will do all this on the last nested one anyway.
7121 if ( m_created
&& !GetBatchCount() )
7123 // Refresh to get correct scrolled position:
7124 wxScrolledWindow::Refresh(eraseb
, rect
);
7128 int rect_x
, rect_y
, rectWidth
, rectHeight
;
7129 int width_label
, width_cell
, height_label
, height_cell
;
7132 // Copy rectangle can get scroll offsets..
7133 rect_x
= rect
->GetX();
7134 rect_y
= rect
->GetY();
7135 rectWidth
= rect
->GetWidth();
7136 rectHeight
= rect
->GetHeight();
7138 width_label
= m_rowLabelWidth
- rect_x
;
7139 if (width_label
> rectWidth
)
7140 width_label
= rectWidth
;
7142 height_label
= m_colLabelHeight
- rect_y
;
7143 if (height_label
> rectHeight
)
7144 height_label
= rectHeight
;
7146 if (rect_x
> m_rowLabelWidth
)
7148 x
= rect_x
- m_rowLabelWidth
;
7149 width_cell
= rectWidth
;
7154 width_cell
= rectWidth
- (m_rowLabelWidth
- rect_x
);
7157 if (rect_y
> m_colLabelHeight
)
7159 y
= rect_y
- m_colLabelHeight
;
7160 height_cell
= rectHeight
;
7165 height_cell
= rectHeight
- (m_colLabelHeight
- rect_y
);
7168 // Paint corner label part intersecting rect.
7169 if ( width_label
> 0 && height_label
> 0 )
7171 wxRect
anotherrect(rect_x
, rect_y
, width_label
, height_label
);
7172 m_cornerLabelWin
->Refresh(eraseb
, &anotherrect
);
7175 // Paint col labels part intersecting rect.
7176 if ( width_cell
> 0 && height_label
> 0 )
7178 wxRect
anotherrect(x
, rect_y
, width_cell
, height_label
);
7179 m_colWindow
->Refresh(eraseb
, &anotherrect
);
7182 // Paint row labels part intersecting rect.
7183 if ( width_label
> 0 && height_cell
> 0 )
7185 wxRect
anotherrect(rect_x
, y
, width_label
, height_cell
);
7186 m_rowLabelWin
->Refresh(eraseb
, &anotherrect
);
7189 // Paint cell area part intersecting rect.
7190 if ( width_cell
> 0 && height_cell
> 0 )
7192 wxRect
anotherrect(x
, y
, width_cell
, height_cell
);
7193 m_gridWin
->Refresh(eraseb
, &anotherrect
);
7198 m_cornerLabelWin
->Refresh(eraseb
, NULL
);
7199 m_colWindow
->Refresh(eraseb
, NULL
);
7200 m_rowLabelWin
->Refresh(eraseb
, NULL
);
7201 m_gridWin
->Refresh(eraseb
, NULL
);
7206 void wxGrid::OnSize(wxSizeEvent
& WXUNUSED(event
))
7208 if (m_targetWindow
!= this) // check whether initialisation has been done
7210 // reposition our children windows
7215 void wxGrid::OnKeyDown( wxKeyEvent
& event
)
7217 if ( m_inOnKeyDown
)
7219 // shouldn't be here - we are going round in circles...
7221 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") );
7224 m_inOnKeyDown
= true;
7226 // propagate the event up and see if it gets processed
7227 wxWindow
*parent
= GetParent();
7228 wxKeyEvent
keyEvt( event
);
7229 keyEvt
.SetEventObject( parent
);
7231 if ( !parent
->GetEventHandler()->ProcessEvent( keyEvt
) )
7233 if (GetLayoutDirection() == wxLayout_RightToLeft
)
7235 if (event
.GetKeyCode() == WXK_RIGHT
)
7236 event
.m_keyCode
= WXK_LEFT
;
7237 else if (event
.GetKeyCode() == WXK_LEFT
)
7238 event
.m_keyCode
= WXK_RIGHT
;
7241 // try local handlers
7242 switch ( event
.GetKeyCode() )
7245 if ( event
.ControlDown() )
7246 MoveCursorUpBlock( event
.ShiftDown() );
7248 MoveCursorUp( event
.ShiftDown() );
7252 if ( event
.ControlDown() )
7253 MoveCursorDownBlock( event
.ShiftDown() );
7255 MoveCursorDown( event
.ShiftDown() );
7259 if ( event
.ControlDown() )
7260 MoveCursorLeftBlock( event
.ShiftDown() );
7262 MoveCursorLeft( event
.ShiftDown() );
7266 if ( event
.ControlDown() )
7267 MoveCursorRightBlock( event
.ShiftDown() );
7269 MoveCursorRight( event
.ShiftDown() );
7273 case WXK_NUMPAD_ENTER
:
7274 if ( event
.ControlDown() )
7276 event
.Skip(); // to let the edit control have the return
7280 if ( GetGridCursorRow() < GetNumberRows()-1 )
7282 MoveCursorDown( event
.ShiftDown() );
7286 // at the bottom of a column
7287 DisableCellEditControl();
7297 if (event
.ShiftDown())
7299 if ( GetGridCursorCol() > 0 )
7301 MoveCursorLeft( false );
7306 DisableCellEditControl();
7311 if ( GetGridCursorCol() < GetNumberCols() - 1 )
7313 MoveCursorRight( false );
7318 DisableCellEditControl();
7324 if ( event
.ControlDown() )
7335 if ( event
.ControlDown() )
7337 GoToCell(m_numRows
- 1, m_numCols
- 1);
7354 // Ctrl-Space selects the current column, Shift-Space -- the
7355 // current row and Ctrl-Shift-Space -- everything
7356 switch ( m_selection
? event
.GetModifiers() : wxMOD_NONE
)
7359 m_selection
->SelectCol(m_currentCellCoords
.GetCol());
7363 m_selection
->SelectRow(m_currentCellCoords
.GetRow());
7366 case wxMOD_CONTROL
| wxMOD_SHIFT
:
7367 m_selection
->SelectBlock(0, 0,
7368 m_numRows
- 1, m_numCols
- 1);
7372 if ( !IsEditable() )
7374 MoveCursorRight(false);
7377 //else: fall through
7390 m_inOnKeyDown
= false;
7393 void wxGrid::OnKeyUp( wxKeyEvent
& event
)
7395 // try local handlers
7397 if ( event
.GetKeyCode() == WXK_SHIFT
)
7399 if ( m_selectedBlockTopLeft
!= wxGridNoCellCoords
&&
7400 m_selectedBlockBottomRight
!= wxGridNoCellCoords
)
7404 m_selection
->SelectBlock(
7405 m_selectedBlockTopLeft
,
7406 m_selectedBlockBottomRight
,
7411 m_selectedBlockTopLeft
= wxGridNoCellCoords
;
7412 m_selectedBlockBottomRight
= wxGridNoCellCoords
;
7413 m_selectedBlockCorner
= wxGridNoCellCoords
;
7417 void wxGrid::OnChar( wxKeyEvent
& event
)
7419 // is it possible to edit the current cell at all?
7420 if ( !IsCellEditControlEnabled() && CanEnableCellControl() )
7422 // yes, now check whether the cells editor accepts the key
7423 int row
= m_currentCellCoords
.GetRow();
7424 int col
= m_currentCellCoords
.GetCol();
7425 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7426 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
7428 // <F2> is special and will always start editing, for
7429 // other keys - ask the editor itself
7430 if ( (event
.GetKeyCode() == WXK_F2
&& !event
.HasModifiers())
7431 || editor
->IsAcceptedKey(event
) )
7433 // ensure cell is visble
7434 MakeCellVisible(row
, col
);
7435 EnableCellEditControl();
7437 // a problem can arise if the cell is not completely
7438 // visible (even after calling MakeCellVisible the
7439 // control is not created and calling StartingKey will
7441 if ( event
.GetKeyCode() != WXK_F2
&& editor
->IsCreated() && m_cellEditCtrlEnabled
)
7442 editor
->StartingKey(event
);
7458 void wxGrid::OnEraseBackground(wxEraseEvent
&)
7462 bool wxGrid::SetCurrentCell( const wxGridCellCoords
& coords
)
7464 if ( SendEvent(wxEVT_GRID_SELECT_CELL
, coords
) == -1 )
7466 // the event has been vetoed - do nothing
7470 #if !defined(__WXMAC__)
7471 wxClientDC
dc( m_gridWin
);
7475 if ( m_currentCellCoords
!= wxGridNoCellCoords
)
7477 DisableCellEditControl();
7479 if ( IsVisible( m_currentCellCoords
, false ) )
7482 r
= BlockToDeviceRect( m_currentCellCoords
, m_currentCellCoords
);
7483 if ( !m_gridLinesEnabled
)
7491 wxGridCellCoordsArray cells
= CalcCellsExposed( r
);
7493 // Otherwise refresh redraws the highlight!
7494 m_currentCellCoords
= coords
;
7496 #if defined(__WXMAC__)
7497 m_gridWin
->Refresh(true /*, & r */);
7499 DrawGridCellArea( dc
, cells
);
7500 DrawAllGridLines( dc
, r
);
7505 m_currentCellCoords
= coords
;
7507 wxGridCellAttr
*attr
= GetCellAttr( coords
);
7508 #if !defined(__WXMAC__)
7509 DrawCellHighlight( dc
, attr
);
7517 wxGrid::UpdateBlockBeingSelected(int topRow
, int leftCol
,
7518 int bottomRow
, int rightCol
)
7522 switch ( m_selection
->GetSelectionMode() )
7525 wxFAIL_MSG( "unknown selection mode" );
7528 case wxGridSelectCells
:
7529 // arbitrary blocks selection allowed so just use the cell
7530 // coordinates as is
7533 case wxGridSelectRows
:
7534 // only full rows selection allowd, ensure that we do select
7537 rightCol
= GetNumberCols() - 1;
7540 case wxGridSelectColumns
:
7541 // same as above but for columns
7543 bottomRow
= GetNumberRows() - 1;
7546 case wxGridSelectRowsOrColumns
:
7547 // in this mode we can select only full rows or full columns so
7548 // it doesn't make sense to select blocks at all (and we can't
7549 // extend the block because there is no preferred direction, we
7550 // could only extend it to cover the entire grid but this is
7556 m_selectedBlockCorner
= wxGridCellCoords(bottomRow
, rightCol
);
7557 MakeCellVisible(m_selectedBlockCorner
);
7559 EnsureFirstLessThanSecond(topRow
, bottomRow
);
7560 EnsureFirstLessThanSecond(leftCol
, rightCol
);
7562 wxGridCellCoords updateTopLeft
= wxGridCellCoords(topRow
, leftCol
),
7563 updateBottomRight
= wxGridCellCoords(bottomRow
, rightCol
);
7565 // First the case that we selected a completely new area
7566 if ( m_selectedBlockTopLeft
== wxGridNoCellCoords
||
7567 m_selectedBlockBottomRight
== wxGridNoCellCoords
)
7570 rect
= BlockToDeviceRect( wxGridCellCoords ( topRow
, leftCol
),
7571 wxGridCellCoords ( bottomRow
, rightCol
) );
7572 m_gridWin
->Refresh( false, &rect
);
7575 // Now handle changing an existing selection area.
7576 else if ( m_selectedBlockTopLeft
!= updateTopLeft
||
7577 m_selectedBlockBottomRight
!= updateBottomRight
)
7579 // Compute two optimal update rectangles:
7580 // Either one rectangle is a real subset of the
7581 // other, or they are (almost) disjoint!
7583 bool need_refresh
[4];
7587 need_refresh
[3] = false;
7590 // Store intermediate values
7591 wxCoord oldLeft
= m_selectedBlockTopLeft
.GetCol();
7592 wxCoord oldTop
= m_selectedBlockTopLeft
.GetRow();
7593 wxCoord oldRight
= m_selectedBlockBottomRight
.GetCol();
7594 wxCoord oldBottom
= m_selectedBlockBottomRight
.GetRow();
7596 // Determine the outer/inner coordinates.
7597 EnsureFirstLessThanSecond(oldLeft
, leftCol
);
7598 EnsureFirstLessThanSecond(oldTop
, topRow
);
7599 EnsureFirstLessThanSecond(rightCol
, oldRight
);
7600 EnsureFirstLessThanSecond(bottomRow
, oldBottom
);
7602 // Now, either the stuff marked old is the outer
7603 // rectangle or we don't have a situation where one
7604 // is contained in the other.
7606 if ( oldLeft
< leftCol
)
7608 // Refresh the newly selected or deselected
7609 // area to the left of the old or new selection.
7610 need_refresh
[0] = true;
7611 rect
[0] = BlockToDeviceRect(
7612 wxGridCellCoords( oldTop
, oldLeft
),
7613 wxGridCellCoords( oldBottom
, leftCol
- 1 ) );
7616 if ( oldTop
< topRow
)
7618 // Refresh the newly selected or deselected
7619 // area above the old or new selection.
7620 need_refresh
[1] = true;
7621 rect
[1] = BlockToDeviceRect(
7622 wxGridCellCoords( oldTop
, leftCol
),
7623 wxGridCellCoords( topRow
- 1, rightCol
) );
7626 if ( oldRight
> rightCol
)
7628 // Refresh the newly selected or deselected
7629 // area to the right of the old or new selection.
7630 need_refresh
[2] = true;
7631 rect
[2] = BlockToDeviceRect(
7632 wxGridCellCoords( oldTop
, rightCol
+ 1 ),
7633 wxGridCellCoords( oldBottom
, oldRight
) );
7636 if ( oldBottom
> bottomRow
)
7638 // Refresh the newly selected or deselected
7639 // area below the old or new selection.
7640 need_refresh
[3] = true;
7641 rect
[3] = BlockToDeviceRect(
7642 wxGridCellCoords( bottomRow
+ 1, leftCol
),
7643 wxGridCellCoords( oldBottom
, rightCol
) );
7646 // various Refresh() calls
7647 for (i
= 0; i
< 4; i
++ )
7648 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
7649 m_gridWin
->Refresh( false, &(rect
[i
]) );
7653 m_selectedBlockTopLeft
= updateTopLeft
;
7654 m_selectedBlockBottomRight
= updateBottomRight
;
7658 // ------ functions to get/send data (see also public functions)
7661 bool wxGrid::GetModelValues()
7663 // Hide the editor, so it won't hide a changed value.
7664 HideCellEditControl();
7668 // all we need to do is repaint the grid
7670 m_gridWin
->Refresh();
7677 bool wxGrid::SetModelValues()
7681 // Disable the editor, so it won't hide a changed value.
7682 // Do we also want to save the current value of the editor first?
7684 DisableCellEditControl();
7688 for ( row
= 0; row
< m_numRows
; row
++ )
7690 for ( col
= 0; col
< m_numCols
; col
++ )
7692 m_table
->SetValue( row
, col
, GetCellValue(row
, col
) );
7702 // Note - this function only draws cells that are in the list of
7703 // exposed cells (usually set from the update region by
7704 // CalcExposedCells)
7706 void wxGrid::DrawGridCellArea( wxDC
& dc
, const wxGridCellCoordsArray
& cells
)
7708 if ( !m_numRows
|| !m_numCols
)
7711 int i
, numCells
= cells
.GetCount();
7712 int row
, col
, cell_rows
, cell_cols
;
7713 wxGridCellCoordsArray redrawCells
;
7715 for ( i
= numCells
- 1; i
>= 0; i
-- )
7717 row
= cells
[i
].GetRow();
7718 col
= cells
[i
].GetCol();
7719 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7721 // If this cell is part of a multicell block, find owner for repaint
7722 if ( cell_rows
<= 0 || cell_cols
<= 0 )
7724 wxGridCellCoords
cell( row
+ cell_rows
, col
+ cell_cols
);
7725 bool marked
= false;
7726 for ( int j
= 0; j
< numCells
; j
++ )
7728 if ( cell
== cells
[j
] )
7737 int count
= redrawCells
.GetCount();
7738 for (int j
= 0; j
< count
; j
++)
7740 if ( cell
== redrawCells
[j
] )
7748 redrawCells
.Add( cell
);
7751 // don't bother drawing this cell
7755 // If this cell is empty, find cell to left that might want to overflow
7756 if (m_table
&& m_table
->IsEmptyCell(row
, col
))
7758 for ( int l
= 0; l
< cell_rows
; l
++ )
7760 // find a cell in this row to leave already marked for repaint
7762 for (int k
= 0; k
< int(redrawCells
.GetCount()); k
++)
7763 if ((redrawCells
[k
].GetCol() < left
) &&
7764 (redrawCells
[k
].GetRow() == row
))
7766 left
= redrawCells
[k
].GetCol();
7770 left
= 0; // oh well
7772 for (int j
= col
- 1; j
>= left
; j
--)
7774 if (!m_table
->IsEmptyCell(row
+ l
, j
))
7776 if (GetCellOverflow(row
+ l
, j
))
7778 wxGridCellCoords
cell(row
+ l
, j
);
7779 bool marked
= false;
7781 for (int k
= 0; k
< numCells
; k
++)
7783 if ( cell
== cells
[k
] )
7792 int count
= redrawCells
.GetCount();
7793 for (int k
= 0; k
< count
; k
++)
7795 if ( cell
== redrawCells
[k
] )
7802 redrawCells
.Add( cell
);
7811 DrawCell( dc
, cells
[i
] );
7814 numCells
= redrawCells
.GetCount();
7816 for ( i
= numCells
- 1; i
>= 0; i
-- )
7818 DrawCell( dc
, redrawCells
[i
] );
7822 void wxGrid::DrawGridSpace( wxDC
& dc
)
7825 m_gridWin
->GetClientSize( &cw
, &ch
);
7828 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
7830 int rightCol
= m_numCols
> 0 ? GetColRight(GetColAt( m_numCols
- 1 )) : 0;
7831 int bottomRow
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) : 0;
7833 if ( right
> rightCol
|| bottom
> bottomRow
)
7836 CalcUnscrolledPosition( 0, 0, &left
, &top
);
7838 dc
.SetBrush(GetDefaultCellBackgroundColour());
7839 dc
.SetPen( *wxTRANSPARENT_PEN
);
7841 if ( right
> rightCol
)
7843 dc
.DrawRectangle( rightCol
, top
, right
- rightCol
, ch
);
7846 if ( bottom
> bottomRow
)
7848 dc
.DrawRectangle( left
, bottomRow
, cw
, bottom
- bottomRow
);
7853 void wxGrid::DrawCell( wxDC
& dc
, const wxGridCellCoords
& coords
)
7855 int row
= coords
.GetRow();
7856 int col
= coords
.GetCol();
7858 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7861 // we draw the cell border ourselves
7862 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7864 bool isCurrent
= coords
== m_currentCellCoords
;
7866 wxRect rect
= CellToRect( row
, col
);
7868 // if the editor is shown, we should use it and not the renderer
7869 // Note: However, only if it is really _shown_, i.e. not hidden!
7870 if ( isCurrent
&& IsCellEditControlShown() )
7872 // NB: this "#if..." is temporary and fixes a problem where the
7873 // edit control is erased by this code after being rendered.
7874 // On wxMac (QD build only), the cell editor is a wxTextCntl and is rendered
7875 // implicitly, causing this out-of order render.
7876 #if !defined(__WXMAC__)
7877 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
7878 editor
->PaintBackground(rect
, attr
);
7884 // but all the rest is drawn by the cell renderer and hence may be customized
7885 wxGridCellRenderer
*renderer
= attr
->GetRenderer(this, row
, col
);
7886 renderer
->Draw(*this, *attr
, dc
, rect
, row
, col
, IsInSelection(coords
));
7893 void wxGrid::DrawCellHighlight( wxDC
& dc
, const wxGridCellAttr
*attr
)
7895 // don't show highlight when the grid doesn't have focus
7899 int row
= m_currentCellCoords
.GetRow();
7900 int col
= m_currentCellCoords
.GetCol();
7902 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7905 wxRect rect
= CellToRect(row
, col
);
7907 // hmmm... what could we do here to show that the cell is disabled?
7908 // for now, I just draw a thinner border than for the other ones, but
7909 // it doesn't look really good
7911 int penWidth
= attr
->IsReadOnly() ? m_cellHighlightROPenWidth
: m_cellHighlightPenWidth
;
7915 // The center of the drawn line is where the position/width/height of
7916 // the rectangle is actually at (on wxMSW at least), so the
7917 // size of the rectangle is reduced to compensate for the thickness of
7918 // the line. If this is too strange on non-wxMSW platforms then
7919 // please #ifdef this appropriately.
7920 rect
.x
+= penWidth
/ 2;
7921 rect
.y
+= penWidth
/ 2;
7922 rect
.width
-= penWidth
- 1;
7923 rect
.height
-= penWidth
- 1;
7925 // Now draw the rectangle
7926 // use the cellHighlightColour if the cell is inside a selection, this
7927 // will ensure the cell is always visible.
7928 dc
.SetPen(wxPen(IsInSelection(row
,col
) ? m_selectionForeground
7929 : m_cellHighlightColour
,
7931 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
7932 dc
.DrawRectangle(rect
);
7936 wxPen
wxGrid::GetDefaultGridLinePen()
7938 return wxPen(GetGridLineColour());
7941 wxPen
wxGrid::GetRowGridLinePen(int WXUNUSED(row
))
7943 return GetDefaultGridLinePen();
7946 wxPen
wxGrid::GetColGridLinePen(int WXUNUSED(col
))
7948 return GetDefaultGridLinePen();
7951 void wxGrid::DrawCellBorder( wxDC
& dc
, const wxGridCellCoords
& coords
)
7953 int row
= coords
.GetRow();
7954 int col
= coords
.GetCol();
7955 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7959 wxRect rect
= CellToRect( row
, col
);
7961 // right hand border
7962 dc
.SetPen( GetColGridLinePen(col
) );
7963 dc
.DrawLine( rect
.x
+ rect
.width
, rect
.y
,
7964 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
+ 1 );
7967 dc
.SetPen( GetRowGridLinePen(row
) );
7968 dc
.DrawLine( rect
.x
, rect
.y
+ rect
.height
,
7969 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
);
7972 void wxGrid::DrawHighlight(wxDC
& dc
, const wxGridCellCoordsArray
& cells
)
7974 // This if block was previously in wxGrid::OnPaint but that doesn't
7975 // seem to get called under wxGTK - MB
7977 if ( m_currentCellCoords
== wxGridNoCellCoords
&&
7978 m_numRows
&& m_numCols
)
7980 m_currentCellCoords
.Set(0, 0);
7983 if ( IsCellEditControlShown() )
7985 // don't show highlight when the edit control is shown
7989 // if the active cell was repainted, repaint its highlight too because it
7990 // might have been damaged by the grid lines
7991 size_t count
= cells
.GetCount();
7992 for ( size_t n
= 0; n
< count
; n
++ )
7994 wxGridCellCoords cell
= cells
[n
];
7996 // If we are using attributes, then we may have just exposed another
7997 // cell in a partially-visible merged cluster of cells. If the "anchor"
7998 // (upper left) cell of this merged cluster is the cell indicated by
7999 // m_currentCellCoords, then we need to refresh the cell highlight even
8000 // though the "anchor" itself is not part of our update segment.
8001 if ( CanHaveAttributes() )
8005 GetCellSize(cell
.GetRow(), cell
.GetCol(), &rows
, &cols
);
8008 cell
.SetRow(cell
.GetRow() + rows
);
8011 cell
.SetCol(cell
.GetCol() + cols
);
8014 if ( cell
== m_currentCellCoords
)
8016 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
8017 DrawCellHighlight(dc
, attr
);
8025 // This is used to redraw all grid lines e.g. when the grid line colour
8028 void wxGrid::DrawAllGridLines( wxDC
& dc
, const wxRegion
& WXUNUSED(reg
) )
8030 if ( !m_gridLinesEnabled
)
8033 int top
, bottom
, left
, right
;
8036 m_gridWin
->GetClientSize(&cw
, &ch
);
8037 CalcUnscrolledPosition( 0, 0, &left
, &top
);
8038 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
8040 // avoid drawing grid lines past the last row and col
8041 if ( m_gridLinesClipHorz
)
8046 const int lastColRight
= GetColRight(GetColAt(m_numCols
- 1));
8047 if ( right
> lastColRight
)
8048 right
= lastColRight
;
8051 if ( m_gridLinesClipVert
)
8056 const int lastRowBottom
= GetRowBottom(m_numRows
- 1);
8057 if ( bottom
> lastRowBottom
)
8058 bottom
= lastRowBottom
;
8061 // no gridlines inside multicells, clip them out
8062 int leftCol
= GetColPos( internalXToCol(left
) );
8063 int topRow
= internalYToRow(top
);
8064 int rightCol
= GetColPos( internalXToCol(right
) );
8065 int bottomRow
= internalYToRow(bottom
);
8067 wxRegion
clippedcells(0, 0, cw
, ch
);
8069 int cell_rows
, cell_cols
;
8072 for ( int j
= topRow
; j
<= bottomRow
; j
++ )
8074 for ( int colPos
= leftCol
; colPos
<= rightCol
; colPos
++ )
8076 int i
= GetColAt( colPos
);
8078 GetCellSize( j
, i
, &cell_rows
, &cell_cols
);
8079 if ((cell_rows
> 1) || (cell_cols
> 1))
8081 rect
= CellToRect(j
,i
);
8082 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
8083 clippedcells
.Subtract(rect
);
8085 else if ((cell_rows
< 0) || (cell_cols
< 0))
8087 rect
= CellToRect(j
+ cell_rows
, i
+ cell_cols
);
8088 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
8089 clippedcells
.Subtract(rect
);
8094 dc
.SetDeviceClippingRegion( clippedcells
);
8097 // horizontal grid lines
8098 for ( int i
= internalYToRow(top
); i
< m_numRows
; i
++ )
8100 int bot
= GetRowBottom(i
) - 1;
8107 dc
.SetPen( GetRowGridLinePen(i
) );
8108 dc
.DrawLine( left
, bot
, right
, bot
);
8112 // vertical grid lines
8113 for ( int colPos
= leftCol
; colPos
< m_numCols
; colPos
++ )
8115 int i
= GetColAt( colPos
);
8117 int colRight
= GetColRight(i
);
8119 if (GetLayoutDirection() != wxLayout_RightToLeft
)
8123 if ( colRight
> right
)
8126 if ( colRight
>= left
)
8128 dc
.SetPen( GetColGridLinePen(i
) );
8129 dc
.DrawLine( colRight
, top
, colRight
, bottom
);
8133 dc
.DestroyClippingRegion();
8136 void wxGrid::DrawRowLabels( wxDC
& dc
, const wxArrayInt
& rows
)
8141 const size_t numLabels
= rows
.GetCount();
8142 for ( size_t i
= 0; i
< numLabels
; i
++ )
8144 DrawRowLabel( dc
, rows
[i
] );
8148 void wxGrid::DrawRowLabel( wxDC
& dc
, int row
)
8150 if ( GetRowHeight(row
) <= 0 || m_rowLabelWidth
<= 0 )
8155 int rowTop
= GetRowTop(row
),
8156 rowBottom
= GetRowBottom(row
) - 1;
8158 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW
)));
8159 dc
.DrawLine( m_rowLabelWidth
- 1, rowTop
, m_rowLabelWidth
- 1, rowBottom
);
8160 dc
.DrawLine( 0, rowTop
, 0, rowBottom
);
8161 dc
.DrawLine( 0, rowBottom
, m_rowLabelWidth
, rowBottom
);
8163 dc
.SetPen( *wxWHITE_PEN
);
8164 dc
.DrawLine( 1, rowTop
, 1, rowBottom
);
8165 dc
.DrawLine( 1, rowTop
, m_rowLabelWidth
- 1, rowTop
);
8167 dc
.SetBackgroundMode( wxBRUSHSTYLE_TRANSPARENT
);
8168 dc
.SetTextForeground( GetLabelTextColour() );
8169 dc
.SetFont( GetLabelFont() );
8172 GetRowLabelAlignment( &hAlign
, &vAlign
);
8175 rect
.SetY( GetRowTop(row
) + 2 );
8176 rect
.SetWidth( m_rowLabelWidth
- 4 );
8177 rect
.SetHeight( GetRowHeight(row
) - 4 );
8178 DrawTextRectangle( dc
, GetRowLabelValue( row
), rect
, hAlign
, vAlign
);
8181 void wxGrid::UseNativeColHeader(bool native
)
8183 if ( native
== m_useNativeHeader
)
8187 m_useNativeHeader
= native
;
8189 CreateColumnWindow();
8191 if ( m_useNativeHeader
)
8192 GetGridColHeader()->SetColumnCount(m_numCols
);
8196 void wxGrid::SetUseNativeColLabels( bool native
)
8198 wxASSERT_MSG( !m_useNativeHeader
,
8199 "doesn't make sense when using native header" );
8201 m_nativeColumnLabels
= native
;
8204 int height
= wxRendererNative::Get().GetHeaderButtonHeight( this );
8205 SetColLabelSize( height
);
8208 GetColLabelWindow()->Refresh();
8209 m_cornerLabelWin
->Refresh();
8212 void wxGrid::DrawColLabels( wxDC
& dc
,const wxArrayInt
& cols
)
8217 const size_t numLabels
= cols
.GetCount();
8218 for ( size_t i
= 0; i
< numLabels
; i
++ )
8220 DrawColLabel( dc
, cols
[i
] );
8224 void wxGrid::DrawCornerLabel(wxDC
& dc
)
8226 if ( m_nativeColumnLabels
)
8228 wxRect
rect(wxSize(m_rowLabelWidth
, m_colLabelHeight
));
8231 wxRendererNative::Get().DrawHeaderButton(m_cornerLabelWin
, dc
, rect
, 0);
8235 dc
.SetPen(wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW
)));
8236 dc
.DrawLine( m_rowLabelWidth
- 1, m_colLabelHeight
- 1,
8237 m_rowLabelWidth
- 1, 0 );
8238 dc
.DrawLine( m_rowLabelWidth
- 1, m_colLabelHeight
- 1,
8239 0, m_colLabelHeight
- 1 );
8240 dc
.DrawLine( 0, 0, m_rowLabelWidth
, 0 );
8241 dc
.DrawLine( 0, 0, 0, m_colLabelHeight
);
8243 dc
.SetPen( *wxWHITE_PEN
);
8244 dc
.DrawLine( 1, 1, m_rowLabelWidth
- 1, 1 );
8245 dc
.DrawLine( 1, 1, 1, m_colLabelHeight
- 1 );
8249 void wxGrid::DrawColLabel(wxDC
& dc
, int col
)
8251 if ( GetColWidth(col
) <= 0 || m_colLabelHeight
<= 0 )
8254 int colLeft
= GetColLeft(col
);
8256 wxRect
rect(colLeft
, 0, GetColWidth(col
), m_colLabelHeight
);
8258 if ( m_nativeColumnLabels
)
8260 wxRendererNative::Get().DrawHeaderButton
8262 GetColLabelWindow(),
8267 ? IsSortOrderAscending()
8268 ? wxHDR_SORT_ICON_UP
8269 : wxHDR_SORT_ICON_DOWN
8270 : wxHDR_SORT_ICON_NONE
8275 int colRight
= GetColRight(col
) - 1;
8277 dc
.SetPen(wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW
)));
8278 dc
.DrawLine( colRight
, 0,
8279 colRight
, m_colLabelHeight
- 1 );
8280 dc
.DrawLine( colLeft
, 0,
8282 dc
.DrawLine( colLeft
, m_colLabelHeight
- 1,
8283 colRight
+ 1, m_colLabelHeight
- 1 );
8285 dc
.SetPen( *wxWHITE_PEN
);
8286 dc
.DrawLine( colLeft
, 1, colLeft
, m_colLabelHeight
- 1 );
8287 dc
.DrawLine( colLeft
, 1, colRight
, 1 );
8290 dc
.SetBackgroundMode( wxBRUSHSTYLE_TRANSPARENT
);
8291 dc
.SetTextForeground( GetLabelTextColour() );
8292 dc
.SetFont( GetLabelFont() );
8295 GetColLabelAlignment( &hAlign
, &vAlign
);
8296 const int orient
= GetColLabelTextOrientation();
8299 DrawTextRectangle(dc
, GetColLabelValue(col
), rect
, hAlign
, vAlign
, orient
);
8302 // TODO: these 2 functions should be replaced with wxDC::DrawLabel() to which
8303 // we just have to add textOrientation support
8304 void wxGrid::DrawTextRectangle( wxDC
& dc
,
8305 const wxString
& value
,
8309 int textOrientation
)
8311 wxArrayString lines
;
8313 StringToLines( value
, lines
);
8315 DrawTextRectangle(dc
, lines
, rect
, horizAlign
, vertAlign
, textOrientation
);
8318 void wxGrid::DrawTextRectangle(wxDC
& dc
,
8319 const wxArrayString
& lines
,
8323 int textOrientation
)
8325 if ( lines
.empty() )
8328 wxDCClipper
clip(dc
, rect
);
8333 if ( textOrientation
== wxHORIZONTAL
)
8334 GetTextBoxSize( dc
, lines
, &textWidth
, &textHeight
);
8336 GetTextBoxSize( dc
, lines
, &textHeight
, &textWidth
);
8340 switch ( vertAlign
)
8342 case wxALIGN_BOTTOM
:
8343 if ( textOrientation
== wxHORIZONTAL
)
8344 y
= rect
.y
+ (rect
.height
- textHeight
- 1);
8346 x
= rect
.x
+ rect
.width
- textWidth
;
8349 case wxALIGN_CENTRE
:
8350 if ( textOrientation
== wxHORIZONTAL
)
8351 y
= rect
.y
+ ((rect
.height
- textHeight
) / 2);
8353 x
= rect
.x
+ ((rect
.width
- textWidth
) / 2);
8358 if ( textOrientation
== wxHORIZONTAL
)
8365 // Align each line of a multi-line label
8366 size_t nLines
= lines
.GetCount();
8367 for ( size_t l
= 0; l
< nLines
; l
++ )
8369 const wxString
& line
= lines
[l
];
8373 *(textOrientation
== wxHORIZONTAL
? &y
: &x
) += dc
.GetCharHeight();
8377 wxCoord lineWidth
= 0,
8379 dc
.GetTextExtent(line
, &lineWidth
, &lineHeight
);
8381 switch ( horizAlign
)
8384 if ( textOrientation
== wxHORIZONTAL
)
8385 x
= rect
.x
+ (rect
.width
- lineWidth
- 1);
8387 y
= rect
.y
+ lineWidth
+ 1;
8390 case wxALIGN_CENTRE
:
8391 if ( textOrientation
== wxHORIZONTAL
)
8392 x
= rect
.x
+ ((rect
.width
- lineWidth
) / 2);
8394 y
= rect
.y
+ rect
.height
- ((rect
.height
- lineWidth
) / 2);
8399 if ( textOrientation
== wxHORIZONTAL
)
8402 y
= rect
.y
+ rect
.height
- 1;
8406 if ( textOrientation
== wxHORIZONTAL
)
8408 dc
.DrawText( line
, x
, y
);
8413 dc
.DrawRotatedText( line
, x
, y
, 90.0 );
8419 // Split multi-line text up into an array of strings.
8420 // Any existing contents of the string array are preserved.
8422 // TODO: refactor wxTextFile::Read() and reuse the same code from here
8423 void wxGrid::StringToLines( const wxString
& value
, wxArrayString
& lines
) const
8427 wxString eol
= wxTextFile::GetEOL( wxTextFileType_Unix
);
8428 wxString tVal
= wxTextFile::Translate( value
, wxTextFileType_Unix
);
8430 while ( startPos
< (int)tVal
.length() )
8432 pos
= tVal
.Mid(startPos
).Find( eol
);
8437 else if ( pos
== 0 )
8439 lines
.Add( wxEmptyString
);
8443 lines
.Add( tVal
.Mid(startPos
, pos
) );
8446 startPos
+= pos
+ 1;
8449 if ( startPos
< (int)tVal
.length() )
8451 lines
.Add( tVal
.Mid( startPos
) );
8455 void wxGrid::GetTextBoxSize( const wxDC
& dc
,
8456 const wxArrayString
& lines
,
8457 long *width
, long *height
) const
8461 wxCoord lineW
= 0, lineH
= 0;
8464 for ( i
= 0; i
< lines
.GetCount(); i
++ )
8466 dc
.GetTextExtent( lines
[i
], &lineW
, &lineH
);
8467 w
= wxMax( w
, lineW
);
8476 // ------ Batch processing.
8478 void wxGrid::EndBatch()
8480 if ( m_batchCount
> 0 )
8483 if ( !m_batchCount
)
8486 m_rowLabelWin
->Refresh();
8487 m_colWindow
->Refresh();
8488 m_cornerLabelWin
->Refresh();
8489 m_gridWin
->Refresh();
8494 // Use this, rather than wxWindow::Refresh(), to force an immediate
8495 // repainting of the grid. Has no effect if you are already inside a
8496 // BeginBatch / EndBatch block.
8498 void wxGrid::ForceRefresh()
8504 bool wxGrid::Enable(bool enable
)
8506 if ( !wxScrolledWindow::Enable(enable
) )
8509 // redraw in the new state
8510 m_gridWin
->Refresh();
8516 // ------ Edit control functions
8519 void wxGrid::EnableEditing( bool edit
)
8521 if ( edit
!= m_editable
)
8524 EnableCellEditControl(edit
);
8529 void wxGrid::EnableCellEditControl( bool enable
)
8534 if ( enable
!= m_cellEditCtrlEnabled
)
8538 if ( SendEvent(wxEVT_GRID_EDITOR_SHOWN
) == -1 )
8541 // this should be checked by the caller!
8542 wxASSERT_MSG( CanEnableCellControl(), _T("can't enable editing for this cell!") );
8544 // do it before ShowCellEditControl()
8545 m_cellEditCtrlEnabled
= enable
;
8547 ShowCellEditControl();
8551 //FIXME:add veto support
8552 SendEvent(wxEVT_GRID_EDITOR_HIDDEN
);
8554 HideCellEditControl();
8555 SaveEditControlValue();
8557 // do it after HideCellEditControl()
8558 m_cellEditCtrlEnabled
= enable
;
8563 bool wxGrid::IsCurrentCellReadOnly() const
8566 wxGridCellAttr
* attr
= ((wxGrid
*)this)->GetCellAttr(m_currentCellCoords
);
8567 bool readonly
= attr
->IsReadOnly();
8573 bool wxGrid::CanEnableCellControl() const
8575 return m_editable
&& (m_currentCellCoords
!= wxGridNoCellCoords
) &&
8576 !IsCurrentCellReadOnly();
8579 bool wxGrid::IsCellEditControlEnabled() const
8581 // the cell edit control might be disable for all cells or just for the
8582 // current one if it's read only
8583 return m_cellEditCtrlEnabled
? !IsCurrentCellReadOnly() : false;
8586 bool wxGrid::IsCellEditControlShown() const
8588 bool isShown
= false;
8590 if ( m_cellEditCtrlEnabled
)
8592 int row
= m_currentCellCoords
.GetRow();
8593 int col
= m_currentCellCoords
.GetCol();
8594 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8595 wxGridCellEditor
* editor
= attr
->GetEditor((wxGrid
*) this, row
, col
);
8600 if ( editor
->IsCreated() )
8602 isShown
= editor
->GetControl()->IsShown();
8612 void wxGrid::ShowCellEditControl()
8614 if ( IsCellEditControlEnabled() )
8616 if ( !IsVisible( m_currentCellCoords
, false ) )
8618 m_cellEditCtrlEnabled
= false;
8623 wxRect rect
= CellToRect( m_currentCellCoords
);
8624 int row
= m_currentCellCoords
.GetRow();
8625 int col
= m_currentCellCoords
.GetCol();
8627 // if this is part of a multicell, find owner (topleft)
8628 int cell_rows
, cell_cols
;
8629 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
8630 if ( cell_rows
<= 0 || cell_cols
<= 0 )
8634 m_currentCellCoords
.SetRow( row
);
8635 m_currentCellCoords
.SetCol( col
);
8638 // erase the highlight and the cell contents because the editor
8639 // might not cover the entire cell
8640 wxClientDC
dc( m_gridWin
);
8642 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8643 dc
.SetBrush(wxBrush(attr
->GetBackgroundColour()));
8644 dc
.SetPen(*wxTRANSPARENT_PEN
);
8645 dc
.DrawRectangle(rect
);
8647 // convert to scrolled coords
8648 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
8654 // cell is shifted by one pixel
8655 // However, don't allow x or y to become negative
8656 // since the SetSize() method interprets that as
8663 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
8664 if ( !editor
->IsCreated() )
8666 editor
->Create(m_gridWin
, wxID_ANY
,
8667 new wxGridCellEditorEvtHandler(this, editor
));
8669 wxGridEditorCreatedEvent
evt(GetId(),
8670 wxEVT_GRID_EDITOR_CREATED
,
8674 editor
->GetControl());
8675 GetEventHandler()->ProcessEvent(evt
);
8678 // resize editor to overflow into righthand cells if allowed
8679 int maxWidth
= rect
.width
;
8680 wxString value
= GetCellValue(row
, col
);
8681 if ( (value
!= wxEmptyString
) && (attr
->GetOverflow()) )
8684 GetTextExtent(value
, &maxWidth
, &y
, NULL
, NULL
, &attr
->GetFont());
8685 if (maxWidth
< rect
.width
)
8686 maxWidth
= rect
.width
;
8689 int client_right
= m_gridWin
->GetClientSize().GetWidth();
8690 if (rect
.x
+ maxWidth
> client_right
)
8691 maxWidth
= client_right
- rect
.x
;
8693 if ((maxWidth
> rect
.width
) && (col
< m_numCols
) && m_table
)
8695 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
8696 // may have changed earlier
8697 for (int i
= col
+ cell_cols
; i
< m_numCols
; i
++)
8700 GetCellSize( row
, i
, &c_rows
, &c_cols
);
8702 // looks weird going over a multicell
8703 if (m_table
->IsEmptyCell( row
, i
) &&
8704 (rect
.width
< maxWidth
) && (c_rows
== 1))
8706 rect
.width
+= GetColWidth( i
);
8712 if (rect
.GetRight() > client_right
)
8713 rect
.SetRight( client_right
- 1 );
8716 editor
->SetCellAttr( attr
);
8717 editor
->SetSize( rect
);
8719 editor
->GetControl()->Move(
8720 editor
->GetControl()->GetPosition().x
+ nXMove
,
8721 editor
->GetControl()->GetPosition().y
);
8722 editor
->Show( true, attr
);
8724 // recalc dimensions in case we need to
8725 // expand the scrolled window to account for editor
8728 editor
->BeginEdit(row
, col
, this);
8729 editor
->SetCellAttr(NULL
);
8737 void wxGrid::HideCellEditControl()
8739 if ( IsCellEditControlEnabled() )
8741 int row
= m_currentCellCoords
.GetRow();
8742 int col
= m_currentCellCoords
.GetCol();
8744 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8745 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
8746 const bool editorHadFocus
= editor
->GetControl()->HasFocus();
8747 editor
->Show( false );
8751 // return the focus to the grid itself if the editor had it
8753 // note that we must not do this unconditionally to avoid stealing
8754 // focus from the window which just received it if we are hiding the
8755 // editor precisely because we lost focus
8756 if ( editorHadFocus
)
8757 m_gridWin
->SetFocus();
8759 // refresh whole row to the right
8760 wxRect
rect( CellToRect(row
, col
) );
8761 CalcScrolledPosition(rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
8762 rect
.width
= m_gridWin
->GetClientSize().GetWidth() - rect
.x
;
8765 // ensure that the pixels under the focus ring get refreshed as well
8766 rect
.Inflate(10, 10);
8769 m_gridWin
->Refresh( false, &rect
);
8773 void wxGrid::SaveEditControlValue()
8775 if ( IsCellEditControlEnabled() )
8777 int row
= m_currentCellCoords
.GetRow();
8778 int col
= m_currentCellCoords
.GetCol();
8780 wxString oldval
= GetCellValue(row
, col
);
8782 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8783 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
8784 bool changed
= editor
->EndEdit(row
, col
, this);
8791 if ( SendEvent(wxEVT_GRID_CELL_CHANGE
) == -1 )
8793 // Event has been vetoed, set the data back.
8794 SetCellValue(row
, col
, oldval
);
8801 // ------ Grid location functions
8802 // Note that all of these functions work with the logical coordinates of
8803 // grid cells and labels so you will need to convert from device
8804 // coordinates for mouse events etc.
8807 wxGridCellCoords
wxGrid::XYToCell(int x
, int y
) const
8809 int row
= YToRow(y
);
8810 int col
= XToCol(x
);
8812 return row
== -1 || col
== -1 ? wxGridNoCellCoords
8813 : wxGridCellCoords(row
, col
);
8816 // compute row or column from some (unscrolled) coordinate value, using either
8817 // m_defaultRowHeight/m_defaultColWidth or binary search on array of
8818 // m_rowBottoms/m_colRights to do it quickly (linear search shouldn't be used
8820 int wxGrid::PosToLinePos(int coord
,
8822 const wxGridOperations
& oper
) const
8824 const int numLines
= oper
.GetNumberOfLines(this);
8827 return clipToMinMax
&& numLines
> 0 ? 0 : wxNOT_FOUND
;
8829 const int defaultLineSize
= oper
.GetDefaultLineSize(this);
8830 wxCHECK_MSG( defaultLineSize
, -1, "can't have 0 default line size" );
8832 int maxPos
= coord
/ defaultLineSize
,
8835 // check for the simplest case: if we have no explicit line sizes
8836 // configured, then we already know the line this position falls in
8837 const wxArrayInt
& lineEnds
= oper
.GetLineEnds(this);
8838 if ( lineEnds
.empty() )
8840 if ( maxPos
< numLines
)
8843 return clipToMinMax
? numLines
- 1 : -1;
8847 // adjust maxPos before starting the binary search
8848 if ( maxPos
>= numLines
)
8850 maxPos
= numLines
- 1;
8854 if ( coord
>= lineEnds
[oper
.GetLineAt(this, maxPos
)])
8857 const int minDist
= oper
.GetMinimalAcceptableLineSize(this);
8859 maxPos
= coord
/ minDist
;
8861 maxPos
= numLines
- 1;
8864 if ( maxPos
>= numLines
)
8865 maxPos
= numLines
- 1;
8868 // check if the position is beyond the last column
8869 const int lineAtMaxPos
= oper
.GetLineAt(this, maxPos
);
8870 if ( coord
>= lineEnds
[lineAtMaxPos
] )
8871 return clipToMinMax
? maxPos
: -1;
8873 // or before the first one
8874 const int lineAt0
= oper
.GetLineAt(this, 0);
8875 if ( coord
< lineEnds
[lineAt0
] )
8879 // finally do perform the binary search
8880 while ( minPos
< maxPos
)
8882 wxCHECK_MSG( lineEnds
[oper
.GetLineAt(this, minPos
)] <= coord
&&
8883 coord
< lineEnds
[oper
.GetLineAt(this, maxPos
)],
8885 "wxGrid: internal error in PosToLinePos()" );
8887 if ( coord
>= lineEnds
[oper
.GetLineAt(this, maxPos
- 1)] )
8892 const int median
= minPos
+ (maxPos
- minPos
+ 1) / 2;
8893 if ( coord
< lineEnds
[oper
.GetLineAt(this, median
)] )
8903 wxGrid::PosToLine(int coord
,
8905 const wxGridOperations
& oper
) const
8907 int pos
= PosToLinePos(coord
, clipToMinMax
, oper
);
8909 return pos
== wxNOT_FOUND
? wxNOT_FOUND
: oper
.GetLineAt(this, pos
);
8912 int wxGrid::YToRow(int y
, bool clipToMinMax
) const
8914 return PosToLine(y
, clipToMinMax
, wxGridRowOperations());
8917 int wxGrid::XToCol(int x
, bool clipToMinMax
) const
8919 return PosToLine(x
, clipToMinMax
, wxGridColumnOperations());
8922 int wxGrid::XToPos(int x
) const
8924 return PosToLinePos(x
, true /* clip */, wxGridColumnOperations());
8927 // return the row number that that the y coord is near the edge of, or -1 if
8928 // not near an edge.
8930 // coords can only possibly be near an edge if
8931 // (a) the row/column is large enough to still allow for an "inner" area
8932 // that is _not_ near the edge (i.e., if the height/width is smaller
8933 // than WXGRID_LABEL_EDGE_ZONE, coords are _never_ considered to be
8936 // (b) resizing rows/columns (the thing for which edge detection is
8937 // relevant at all) is enabled.
8939 int wxGrid::PosToEdgeOfLine(int pos
, const wxGridOperations
& oper
) const
8941 if ( !oper
.CanResizeLines(this) )
8944 const int line
= oper
.PosToLine(this, pos
, true);
8946 if ( oper
.GetLineSize(this, line
) > WXGRID_LABEL_EDGE_ZONE
)
8948 // We know that we are in this line, test whether we are close enough
8949 // to start or end border, respectively.
8950 if ( abs(oper
.GetLineEndPos(this, line
) - pos
) < WXGRID_LABEL_EDGE_ZONE
)
8952 else if ( line
> 0 &&
8953 pos
- oper
.GetLineStartPos(this,
8954 line
) < WXGRID_LABEL_EDGE_ZONE
)
8961 int wxGrid::YToEdgeOfRow(int y
) const
8963 return PosToEdgeOfLine(y
, wxGridRowOperations());
8966 int wxGrid::XToEdgeOfCol(int x
) const
8968 return PosToEdgeOfLine(x
, wxGridColumnOperations());
8971 wxRect
wxGrid::CellToRect( int row
, int col
) const
8973 wxRect
rect( -1, -1, -1, -1 );
8975 if ( row
>= 0 && row
< m_numRows
&&
8976 col
>= 0 && col
< m_numCols
)
8978 int i
, cell_rows
, cell_cols
;
8979 rect
.width
= rect
.height
= 0;
8980 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
8981 // if negative then find multicell owner
8986 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
8988 rect
.x
= GetColLeft(col
);
8989 rect
.y
= GetRowTop(row
);
8990 for (i
=col
; i
< col
+ cell_cols
; i
++)
8991 rect
.width
+= GetColWidth(i
);
8992 for (i
=row
; i
< row
+ cell_rows
; i
++)
8993 rect
.height
+= GetRowHeight(i
);
8996 // if grid lines are enabled, then the area of the cell is a bit smaller
8997 if (m_gridLinesEnabled
)
9006 bool wxGrid::IsVisible( int row
, int col
, bool wholeCellVisible
) const
9008 // get the cell rectangle in logical coords
9010 wxRect
r( CellToRect( row
, col
) );
9012 // convert to device coords
9014 int left
, top
, right
, bottom
;
9015 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
9016 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
9018 // check against the client area of the grid window
9020 m_gridWin
->GetClientSize( &cw
, &ch
);
9022 if ( wholeCellVisible
)
9024 // is the cell wholly visible ?
9025 return ( left
>= 0 && right
<= cw
&&
9026 top
>= 0 && bottom
<= ch
);
9030 // is the cell partly visible ?
9032 return ( ((left
>= 0 && left
< cw
) || (right
> 0 && right
<= cw
)) &&
9033 ((top
>= 0 && top
< ch
) || (bottom
> 0 && bottom
<= ch
)) );
9037 // make the specified cell location visible by doing a minimal amount
9040 void wxGrid::MakeCellVisible( int row
, int col
)
9043 int xpos
= -1, ypos
= -1;
9045 if ( row
>= 0 && row
< m_numRows
&&
9046 col
>= 0 && col
< m_numCols
)
9048 // get the cell rectangle in logical coords
9049 wxRect
r( CellToRect( row
, col
) );
9051 // convert to device coords
9052 int left
, top
, right
, bottom
;
9053 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
9054 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
9057 m_gridWin
->GetClientSize( &cw
, &ch
);
9063 else if ( bottom
> ch
)
9065 int h
= r
.GetHeight();
9067 for ( i
= row
- 1; i
>= 0; i
-- )
9069 int rowHeight
= GetRowHeight(i
);
9070 if ( h
+ rowHeight
> ch
)
9077 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
9078 // have rounding errors (this is important, because if we do,
9079 // we might not scroll at all and some cells won't be redrawn)
9081 // Sometimes GRID_SCROLL_LINE / 2 is not enough,
9082 // so just add a full scroll unit...
9083 ypos
+= m_scrollLineY
;
9086 // special handling for wide cells - show always left part of the cell!
9087 // Otherwise, e.g. when stepping from row to row, it would jump between
9088 // left and right part of the cell on every step!
9090 if ( left
< 0 || (right
- left
) >= cw
)
9094 else if ( right
> cw
)
9096 // position the view so that the cell is on the right
9098 CalcUnscrolledPosition(0, 0, &x0
, &y0
);
9099 xpos
= x0
+ (right
- cw
);
9101 // see comment for ypos above
9102 xpos
+= m_scrollLineX
;
9105 if ( xpos
!= -1 || ypos
!= -1 )
9108 xpos
/= m_scrollLineX
;
9110 ypos
/= m_scrollLineY
;
9111 Scroll( xpos
, ypos
);
9118 // ------ Grid cursor movement functions
9122 wxGrid::DoMoveCursor(bool expandSelection
,
9123 const wxGridDirectionOperations
& diroper
)
9125 if ( m_currentCellCoords
== wxGridNoCellCoords
)
9128 if ( expandSelection
)
9130 wxGridCellCoords coords
= m_selectedBlockCorner
;
9131 if ( coords
== wxGridNoCellCoords
)
9132 coords
= m_currentCellCoords
;
9134 if ( diroper
.IsAtBoundary(coords
) )
9137 diroper
.Advance(coords
);
9139 UpdateBlockBeingSelected(m_currentCellCoords
, coords
);
9141 else // don't expand selection
9145 if ( diroper
.IsAtBoundary(m_currentCellCoords
) )
9148 wxGridCellCoords coords
= m_currentCellCoords
;
9149 diroper
.Advance(coords
);
9157 bool wxGrid::MoveCursorUp(bool expandSelection
)
9159 return DoMoveCursor(expandSelection
,
9160 wxGridBackwardOperations(this, wxGridRowOperations()));
9163 bool wxGrid::MoveCursorDown(bool expandSelection
)
9165 return DoMoveCursor(expandSelection
,
9166 wxGridForwardOperations(this, wxGridRowOperations()));
9169 bool wxGrid::MoveCursorLeft(bool expandSelection
)
9171 return DoMoveCursor(expandSelection
,
9172 wxGridBackwardOperations(this, wxGridColumnOperations()));
9175 bool wxGrid::MoveCursorRight(bool expandSelection
)
9177 return DoMoveCursor(expandSelection
,
9178 wxGridForwardOperations(this, wxGridColumnOperations()));
9181 bool wxGrid::DoMoveCursorByPage(const wxGridDirectionOperations
& diroper
)
9183 if ( m_currentCellCoords
== wxGridNoCellCoords
)
9186 if ( diroper
.IsAtBoundary(m_currentCellCoords
) )
9189 const int oldRow
= m_currentCellCoords
.GetRow();
9190 int newRow
= diroper
.MoveByPixelDistance(oldRow
, m_gridWin
->GetClientSize().y
);
9191 if ( newRow
== oldRow
)
9193 wxGridCellCoords
coords(m_currentCellCoords
);
9194 diroper
.Advance(coords
);
9195 newRow
= coords
.GetRow();
9198 GoToCell(newRow
, m_currentCellCoords
.GetCol());
9203 bool wxGrid::MovePageUp()
9205 return DoMoveCursorByPage(
9206 wxGridBackwardOperations(this, wxGridRowOperations()));
9209 bool wxGrid::MovePageDown()
9211 return DoMoveCursorByPage(
9212 wxGridForwardOperations(this, wxGridRowOperations()));
9215 // helper of DoMoveCursorByBlock(): advance the cell coordinates using diroper
9216 // until we find a non-empty cell or reach the grid end
9218 wxGrid::AdvanceToNextNonEmpty(wxGridCellCoords
& coords
,
9219 const wxGridDirectionOperations
& diroper
)
9221 while ( !diroper
.IsAtBoundary(coords
) )
9223 diroper
.Advance(coords
);
9224 if ( !m_table
->IsEmpty(coords
) )
9230 wxGrid::DoMoveCursorByBlock(bool expandSelection
,
9231 const wxGridDirectionOperations
& diroper
)
9233 if ( !m_table
|| m_currentCellCoords
== wxGridNoCellCoords
)
9236 if ( diroper
.IsAtBoundary(m_currentCellCoords
) )
9239 wxGridCellCoords
coords(m_currentCellCoords
);
9240 if ( m_table
->IsEmpty(coords
) )
9242 // we are in an empty cell: find the next block of non-empty cells
9243 AdvanceToNextNonEmpty(coords
, diroper
);
9245 else // current cell is not empty
9247 diroper
.Advance(coords
);
9248 if ( m_table
->IsEmpty(coords
) )
9250 // we started at the end of a block, find the next one
9251 AdvanceToNextNonEmpty(coords
, diroper
);
9253 else // we're in a middle of a block
9255 // go to the end of it, i.e. find the last cell before the next
9257 while ( !diroper
.IsAtBoundary(coords
) )
9259 wxGridCellCoords
coordsNext(coords
);
9260 diroper
.Advance(coordsNext
);
9261 if ( m_table
->IsEmpty(coordsNext
) )
9264 coords
= coordsNext
;
9269 if ( expandSelection
)
9271 UpdateBlockBeingSelected(m_currentCellCoords
, coords
);
9282 bool wxGrid::MoveCursorUpBlock(bool expandSelection
)
9284 return DoMoveCursorByBlock(
9286 wxGridBackwardOperations(this, wxGridRowOperations())
9290 bool wxGrid::MoveCursorDownBlock( bool expandSelection
)
9292 return DoMoveCursorByBlock(
9294 wxGridForwardOperations(this, wxGridRowOperations())
9298 bool wxGrid::MoveCursorLeftBlock( bool expandSelection
)
9300 return DoMoveCursorByBlock(
9302 wxGridBackwardOperations(this, wxGridColumnOperations())
9306 bool wxGrid::MoveCursorRightBlock( bool expandSelection
)
9308 return DoMoveCursorByBlock(
9310 wxGridForwardOperations(this, wxGridColumnOperations())
9315 // ------ Label values and formatting
9318 void wxGrid::GetRowLabelAlignment( int *horiz
, int *vert
) const
9321 *horiz
= m_rowLabelHorizAlign
;
9323 *vert
= m_rowLabelVertAlign
;
9326 void wxGrid::GetColLabelAlignment( int *horiz
, int *vert
) const
9329 *horiz
= m_colLabelHorizAlign
;
9331 *vert
= m_colLabelVertAlign
;
9334 int wxGrid::GetColLabelTextOrientation() const
9336 return m_colLabelTextOrientation
;
9339 wxString
wxGrid::GetRowLabelValue( int row
) const
9343 return m_table
->GetRowLabelValue( row
);
9353 wxString
wxGrid::GetColLabelValue( int col
) const
9357 return m_table
->GetColLabelValue( col
);
9367 void wxGrid::SetRowLabelSize( int width
)
9369 wxASSERT( width
>= 0 || width
== wxGRID_AUTOSIZE
);
9371 if ( width
== wxGRID_AUTOSIZE
)
9373 width
= CalcColOrRowLabelAreaMinSize(wxGRID_ROW
);
9376 if ( width
!= m_rowLabelWidth
)
9380 m_rowLabelWin
->Show( false );
9381 m_cornerLabelWin
->Show( false );
9383 else if ( m_rowLabelWidth
== 0 )
9385 m_rowLabelWin
->Show( true );
9386 if ( m_colLabelHeight
> 0 )
9387 m_cornerLabelWin
->Show( true );
9390 m_rowLabelWidth
= width
;
9392 wxScrolledWindow::Refresh( true );
9396 void wxGrid::SetColLabelSize( int height
)
9398 wxASSERT( height
>=0 || height
== wxGRID_AUTOSIZE
);
9400 if ( height
== wxGRID_AUTOSIZE
)
9402 height
= CalcColOrRowLabelAreaMinSize(wxGRID_COLUMN
);
9405 if ( height
!= m_colLabelHeight
)
9409 m_colWindow
->Show( false );
9410 m_cornerLabelWin
->Show( false );
9412 else if ( m_colLabelHeight
== 0 )
9414 m_colWindow
->Show( true );
9415 if ( m_rowLabelWidth
> 0 )
9416 m_cornerLabelWin
->Show( true );
9419 m_colLabelHeight
= height
;
9421 wxScrolledWindow::Refresh( true );
9425 void wxGrid::SetLabelBackgroundColour( const wxColour
& colour
)
9427 if ( m_labelBackgroundColour
!= colour
)
9429 m_labelBackgroundColour
= colour
;
9430 m_rowLabelWin
->SetBackgroundColour( colour
);
9431 m_colWindow
->SetBackgroundColour( colour
);
9432 m_cornerLabelWin
->SetBackgroundColour( colour
);
9434 if ( !GetBatchCount() )
9436 m_rowLabelWin
->Refresh();
9437 m_colWindow
->Refresh();
9438 m_cornerLabelWin
->Refresh();
9443 void wxGrid::SetLabelTextColour( const wxColour
& colour
)
9445 if ( m_labelTextColour
!= colour
)
9447 m_labelTextColour
= colour
;
9448 if ( !GetBatchCount() )
9450 m_rowLabelWin
->Refresh();
9451 m_colWindow
->Refresh();
9456 void wxGrid::SetLabelFont( const wxFont
& font
)
9459 if ( !GetBatchCount() )
9461 m_rowLabelWin
->Refresh();
9462 m_colWindow
->Refresh();
9466 void wxGrid::SetRowLabelAlignment( int horiz
, int vert
)
9468 // allow old (incorrect) defs to be used
9471 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
9472 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
9473 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
9478 case wxTOP
: vert
= wxALIGN_TOP
; break;
9479 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
9480 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
9483 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
9485 m_rowLabelHorizAlign
= horiz
;
9488 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
9490 m_rowLabelVertAlign
= vert
;
9493 if ( !GetBatchCount() )
9495 m_rowLabelWin
->Refresh();
9499 void wxGrid::SetColLabelAlignment( int horiz
, int vert
)
9501 // allow old (incorrect) defs to be used
9504 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
9505 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
9506 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
9511 case wxTOP
: vert
= wxALIGN_TOP
; break;
9512 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
9513 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
9516 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
9518 m_colLabelHorizAlign
= horiz
;
9521 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
9523 m_colLabelVertAlign
= vert
;
9526 if ( !GetBatchCount() )
9528 m_colWindow
->Refresh();
9532 // Note: under MSW, the default column label font must be changed because it
9533 // does not support vertical printing
9535 // Example: wxFont font(9, wxSWISS, wxNORMAL, wxBOLD);
9536 // pGrid->SetLabelFont(font);
9537 // pGrid->SetColLabelTextOrientation(wxVERTICAL);
9539 void wxGrid::SetColLabelTextOrientation( int textOrientation
)
9541 if ( textOrientation
== wxHORIZONTAL
|| textOrientation
== wxVERTICAL
)
9542 m_colLabelTextOrientation
= textOrientation
;
9544 if ( !GetBatchCount() )
9545 m_colWindow
->Refresh();
9548 void wxGrid::SetRowLabelValue( int row
, const wxString
& s
)
9552 m_table
->SetRowLabelValue( row
, s
);
9553 if ( !GetBatchCount() )
9555 wxRect rect
= CellToRect( row
, 0 );
9556 if ( rect
.height
> 0 )
9558 CalcScrolledPosition(0, rect
.y
, &rect
.x
, &rect
.y
);
9560 rect
.width
= m_rowLabelWidth
;
9561 m_rowLabelWin
->Refresh( true, &rect
);
9567 void wxGrid::SetColLabelValue( int col
, const wxString
& s
)
9571 m_table
->SetColLabelValue( col
, s
);
9572 if ( !GetBatchCount() )
9574 if ( m_useNativeHeader
)
9576 GetGridColHeader()->UpdateColumn(col
);
9580 wxRect rect
= CellToRect( 0, col
);
9581 if ( rect
.width
> 0 )
9583 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &rect
.y
);
9585 rect
.height
= m_colLabelHeight
;
9586 GetColLabelWindow()->Refresh( true, &rect
);
9593 void wxGrid::SetGridLineColour( const wxColour
& colour
)
9595 if ( m_gridLineColour
!= colour
)
9597 m_gridLineColour
= colour
;
9599 if ( GridLinesEnabled() )
9604 void wxGrid::SetCellHighlightColour( const wxColour
& colour
)
9606 if ( m_cellHighlightColour
!= colour
)
9608 m_cellHighlightColour
= colour
;
9610 wxClientDC
dc( m_gridWin
);
9612 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
9613 DrawCellHighlight(dc
, attr
);
9618 void wxGrid::SetCellHighlightPenWidth(int width
)
9620 if (m_cellHighlightPenWidth
!= width
)
9622 m_cellHighlightPenWidth
= width
;
9624 // Just redrawing the cell highlight is not enough since that won't
9625 // make any visible change if the the thickness is getting smaller.
9626 int row
= m_currentCellCoords
.GetRow();
9627 int col
= m_currentCellCoords
.GetCol();
9628 if ( row
== -1 || col
== -1 || GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
9631 wxRect rect
= CellToRect(row
, col
);
9632 m_gridWin
->Refresh(true, &rect
);
9636 void wxGrid::SetCellHighlightROPenWidth(int width
)
9638 if (m_cellHighlightROPenWidth
!= width
)
9640 m_cellHighlightROPenWidth
= width
;
9642 // Just redrawing the cell highlight is not enough since that won't
9643 // make any visible change if the the thickness is getting smaller.
9644 int row
= m_currentCellCoords
.GetRow();
9645 int col
= m_currentCellCoords
.GetCol();
9646 if ( row
== -1 || col
== -1 ||
9647 GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
9650 wxRect rect
= CellToRect(row
, col
);
9651 m_gridWin
->Refresh(true, &rect
);
9655 void wxGrid::RedrawGridLines()
9657 // the lines will be redrawn when the window is thawn
9658 if ( GetBatchCount() )
9661 if ( GridLinesEnabled() )
9663 wxClientDC
dc( m_gridWin
);
9665 DrawAllGridLines( dc
, wxRegion() );
9667 else // remove the grid lines
9669 m_gridWin
->Refresh();
9673 void wxGrid::EnableGridLines( bool enable
)
9675 if ( enable
!= m_gridLinesEnabled
)
9677 m_gridLinesEnabled
= enable
;
9683 void wxGrid::DoClipGridLines(bool& var
, bool clip
)
9689 if ( GridLinesEnabled() )
9694 int wxGrid::GetDefaultRowSize() const
9696 return m_defaultRowHeight
;
9699 int wxGrid::GetRowSize( int row
) const
9701 wxCHECK_MSG( row
>= 0 && row
< m_numRows
, 0, _T("invalid row index") );
9703 return GetRowHeight(row
);
9706 int wxGrid::GetDefaultColSize() const
9708 return m_defaultColWidth
;
9711 int wxGrid::GetColSize( int col
) const
9713 wxCHECK_MSG( col
>= 0 && col
< m_numCols
, 0, _T("invalid column index") );
9715 return GetColWidth(col
);
9718 // ============================================================================
9719 // access to the grid attributes: each of them has a default value in the grid
9720 // itself and may be overidden on a per-cell basis
9721 // ============================================================================
9723 // ----------------------------------------------------------------------------
9724 // setting default attributes
9725 // ----------------------------------------------------------------------------
9727 void wxGrid::SetDefaultCellBackgroundColour( const wxColour
& col
)
9729 m_defaultCellAttr
->SetBackgroundColour(col
);
9731 m_gridWin
->SetBackgroundColour(col
);
9735 void wxGrid::SetDefaultCellTextColour( const wxColour
& col
)
9737 m_defaultCellAttr
->SetTextColour(col
);
9740 void wxGrid::SetDefaultCellAlignment( int horiz
, int vert
)
9742 m_defaultCellAttr
->SetAlignment(horiz
, vert
);
9745 void wxGrid::SetDefaultCellOverflow( bool allow
)
9747 m_defaultCellAttr
->SetOverflow(allow
);
9750 void wxGrid::SetDefaultCellFont( const wxFont
& font
)
9752 m_defaultCellAttr
->SetFont(font
);
9755 // For editors and renderers the type registry takes precedence over the
9756 // default attr, so we need to register the new editor/renderer for the string
9757 // data type in order to make setting a default editor/renderer appear to
9760 void wxGrid::SetDefaultRenderer(wxGridCellRenderer
*renderer
)
9762 RegisterDataType(wxGRID_VALUE_STRING
,
9764 GetDefaultEditorForType(wxGRID_VALUE_STRING
));
9767 void wxGrid::SetDefaultEditor(wxGridCellEditor
*editor
)
9769 RegisterDataType(wxGRID_VALUE_STRING
,
9770 GetDefaultRendererForType(wxGRID_VALUE_STRING
),
9774 // ----------------------------------------------------------------------------
9775 // access to the default attributes
9776 // ----------------------------------------------------------------------------
9778 wxColour
wxGrid::GetDefaultCellBackgroundColour() const
9780 return m_defaultCellAttr
->GetBackgroundColour();
9783 wxColour
wxGrid::GetDefaultCellTextColour() const
9785 return m_defaultCellAttr
->GetTextColour();
9788 wxFont
wxGrid::GetDefaultCellFont() const
9790 return m_defaultCellAttr
->GetFont();
9793 void wxGrid::GetDefaultCellAlignment( int *horiz
, int *vert
) const
9795 m_defaultCellAttr
->GetAlignment(horiz
, vert
);
9798 bool wxGrid::GetDefaultCellOverflow() const
9800 return m_defaultCellAttr
->GetOverflow();
9803 wxGridCellRenderer
*wxGrid::GetDefaultRenderer() const
9805 return m_defaultCellAttr
->GetRenderer(NULL
, 0, 0);
9808 wxGridCellEditor
*wxGrid::GetDefaultEditor() const
9810 return m_defaultCellAttr
->GetEditor(NULL
, 0, 0);
9813 // ----------------------------------------------------------------------------
9814 // access to cell attributes
9815 // ----------------------------------------------------------------------------
9817 wxColour
wxGrid::GetCellBackgroundColour(int row
, int col
) const
9819 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9820 wxColour colour
= attr
->GetBackgroundColour();
9826 wxColour
wxGrid::GetCellTextColour( int row
, int col
) const
9828 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9829 wxColour colour
= attr
->GetTextColour();
9835 wxFont
wxGrid::GetCellFont( int row
, int col
) const
9837 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9838 wxFont font
= attr
->GetFont();
9844 void wxGrid::GetCellAlignment( int row
, int col
, int *horiz
, int *vert
) const
9846 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9847 attr
->GetAlignment(horiz
, vert
);
9851 bool wxGrid::GetCellOverflow( int row
, int col
) const
9853 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9854 bool allow
= attr
->GetOverflow();
9860 void wxGrid::GetCellSize( int row
, int col
, int *num_rows
, int *num_cols
) const
9862 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9863 attr
->GetSize( num_rows
, num_cols
);
9867 wxGridCellRenderer
* wxGrid::GetCellRenderer(int row
, int col
) const
9869 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9870 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
9876 wxGridCellEditor
* wxGrid::GetCellEditor(int row
, int col
) const
9878 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9879 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
9885 bool wxGrid::IsReadOnly(int row
, int col
) const
9887 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9888 bool isReadOnly
= attr
->IsReadOnly();
9894 // ----------------------------------------------------------------------------
9895 // attribute support: cache, automatic provider creation, ...
9896 // ----------------------------------------------------------------------------
9898 bool wxGrid::CanHaveAttributes() const
9905 return m_table
->CanHaveAttributes();
9908 void wxGrid::ClearAttrCache()
9910 if ( m_attrCache
.row
!= -1 )
9912 wxGridCellAttr
*oldAttr
= m_attrCache
.attr
;
9913 m_attrCache
.attr
= NULL
;
9914 m_attrCache
.row
= -1;
9915 // wxSafeDecRec(...) might cause event processing that accesses
9916 // the cached attribute, if one exists (e.g. by deleting the
9917 // editor stored within the attribute). Therefore it is important
9918 // to invalidate the cache before calling wxSafeDecRef!
9919 wxSafeDecRef(oldAttr
);
9923 void wxGrid::CacheAttr(int row
, int col
, wxGridCellAttr
*attr
) const
9927 wxGrid
*self
= (wxGrid
*)this; // const_cast
9929 self
->ClearAttrCache();
9930 self
->m_attrCache
.row
= row
;
9931 self
->m_attrCache
.col
= col
;
9932 self
->m_attrCache
.attr
= attr
;
9937 bool wxGrid::LookupAttr(int row
, int col
, wxGridCellAttr
**attr
) const
9939 if ( row
== m_attrCache
.row
&& col
== m_attrCache
.col
)
9941 *attr
= m_attrCache
.attr
;
9942 wxSafeIncRef(m_attrCache
.attr
);
9944 #ifdef DEBUG_ATTR_CACHE
9945 gs_nAttrCacheHits
++;
9952 #ifdef DEBUG_ATTR_CACHE
9953 gs_nAttrCacheMisses
++;
9960 wxGridCellAttr
*wxGrid::GetCellAttr(int row
, int col
) const
9962 wxGridCellAttr
*attr
= NULL
;
9963 // Additional test to avoid looking at the cache e.g. for
9964 // wxNoCellCoords, as this will confuse memory management.
9967 if ( !LookupAttr(row
, col
, &attr
) )
9969 attr
= m_table
? m_table
->GetAttr(row
, col
, wxGridCellAttr::Any
)
9971 CacheAttr(row
, col
, attr
);
9977 attr
->SetDefAttr(m_defaultCellAttr
);
9981 attr
= m_defaultCellAttr
;
9988 wxGridCellAttr
*wxGrid::GetOrCreateCellAttr(int row
, int col
) const
9990 wxGridCellAttr
*attr
= NULL
;
9991 bool canHave
= ((wxGrid
*)this)->CanHaveAttributes();
9993 wxCHECK_MSG( canHave
, attr
, _T("Cell attributes not allowed"));
9994 wxCHECK_MSG( m_table
, attr
, _T("must have a table") );
9996 attr
= m_table
->GetAttr(row
, col
, wxGridCellAttr::Cell
);
9999 attr
= new wxGridCellAttr(m_defaultCellAttr
);
10001 // artificially inc the ref count to match DecRef() in caller
10003 m_table
->SetAttr(attr
, row
, col
);
10009 // ----------------------------------------------------------------------------
10010 // setting column attributes (wrappers around SetColAttr)
10011 // ----------------------------------------------------------------------------
10013 void wxGrid::SetColFormatBool(int col
)
10015 SetColFormatCustom(col
, wxGRID_VALUE_BOOL
);
10018 void wxGrid::SetColFormatNumber(int col
)
10020 SetColFormatCustom(col
, wxGRID_VALUE_NUMBER
);
10023 void wxGrid::SetColFormatFloat(int col
, int width
, int precision
)
10025 wxString typeName
= wxGRID_VALUE_FLOAT
;
10026 if ( (width
!= -1) || (precision
!= -1) )
10028 typeName
<< _T(':') << width
<< _T(',') << precision
;
10031 SetColFormatCustom(col
, typeName
);
10034 void wxGrid::SetColFormatCustom(int col
, const wxString
& typeName
)
10036 wxGridCellAttr
*attr
= m_table
->GetAttr(-1, col
, wxGridCellAttr::Col
);
10038 attr
= new wxGridCellAttr
;
10039 wxGridCellRenderer
*renderer
= GetDefaultRendererForType(typeName
);
10040 attr
->SetRenderer(renderer
);
10041 wxGridCellEditor
*editor
= GetDefaultEditorForType(typeName
);
10042 attr
->SetEditor(editor
);
10044 SetColAttr(col
, attr
);
10048 // ----------------------------------------------------------------------------
10049 // setting cell attributes: this is forwarded to the table
10050 // ----------------------------------------------------------------------------
10052 void wxGrid::SetAttr(int row
, int col
, wxGridCellAttr
*attr
)
10054 if ( CanHaveAttributes() )
10056 m_table
->SetAttr(attr
, row
, col
);
10061 wxSafeDecRef(attr
);
10065 void wxGrid::SetRowAttr(int row
, wxGridCellAttr
*attr
)
10067 if ( CanHaveAttributes() )
10069 m_table
->SetRowAttr(attr
, row
);
10074 wxSafeDecRef(attr
);
10078 void wxGrid::SetColAttr(int col
, wxGridCellAttr
*attr
)
10080 if ( CanHaveAttributes() )
10082 m_table
->SetColAttr(attr
, col
);
10087 wxSafeDecRef(attr
);
10091 void wxGrid::SetCellBackgroundColour( int row
, int col
, const wxColour
& colour
)
10093 if ( CanHaveAttributes() )
10095 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10096 attr
->SetBackgroundColour(colour
);
10101 void wxGrid::SetCellTextColour( int row
, int col
, const wxColour
& colour
)
10103 if ( CanHaveAttributes() )
10105 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10106 attr
->SetTextColour(colour
);
10111 void wxGrid::SetCellFont( int row
, int col
, const wxFont
& font
)
10113 if ( CanHaveAttributes() )
10115 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10116 attr
->SetFont(font
);
10121 void wxGrid::SetCellAlignment( int row
, int col
, int horiz
, int vert
)
10123 if ( CanHaveAttributes() )
10125 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10126 attr
->SetAlignment(horiz
, vert
);
10131 void wxGrid::SetCellOverflow( int row
, int col
, bool allow
)
10133 if ( CanHaveAttributes() )
10135 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10136 attr
->SetOverflow(allow
);
10141 void wxGrid::SetCellSize( int row
, int col
, int num_rows
, int num_cols
)
10143 if ( CanHaveAttributes() )
10145 int cell_rows
, cell_cols
;
10147 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10148 attr
->GetSize(&cell_rows
, &cell_cols
);
10149 attr
->SetSize(num_rows
, num_cols
);
10152 // Cannot set the size of a cell to 0 or negative values
10153 // While it is perfectly legal to do that, this function cannot
10154 // handle all the possibilies, do it by hand by getting the CellAttr.
10155 // You can only set the size of a cell to 1,1 or greater with this fn
10156 wxASSERT_MSG( !((cell_rows
< 1) || (cell_cols
< 1)),
10157 wxT("wxGrid::SetCellSize setting cell size that is already part of another cell"));
10158 wxASSERT_MSG( !((num_rows
< 1) || (num_cols
< 1)),
10159 wxT("wxGrid::SetCellSize setting cell size to < 1"));
10161 // if this was already a multicell then "turn off" the other cells first
10162 if ((cell_rows
> 1) || (cell_cols
> 1))
10165 for (j
=row
; j
< row
+ cell_rows
; j
++)
10167 for (i
=col
; i
< col
+ cell_cols
; i
++)
10169 if ((i
!= col
) || (j
!= row
))
10171 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
10172 attr_stub
->SetSize( 1, 1 );
10173 attr_stub
->DecRef();
10179 // mark the cells that will be covered by this cell to
10180 // negative or zero values to point back at this cell
10181 if (((num_rows
> 1) || (num_cols
> 1)) && (num_rows
>= 1) && (num_cols
>= 1))
10184 for (j
=row
; j
< row
+ num_rows
; j
++)
10186 for (i
=col
; i
< col
+ num_cols
; i
++)
10188 if ((i
!= col
) || (j
!= row
))
10190 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
10191 attr_stub
->SetSize( row
- j
, col
- i
);
10192 attr_stub
->DecRef();
10200 void wxGrid::SetCellRenderer(int row
, int col
, wxGridCellRenderer
*renderer
)
10202 if ( CanHaveAttributes() )
10204 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10205 attr
->SetRenderer(renderer
);
10210 void wxGrid::SetCellEditor(int row
, int col
, wxGridCellEditor
* editor
)
10212 if ( CanHaveAttributes() )
10214 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10215 attr
->SetEditor(editor
);
10220 void wxGrid::SetReadOnly(int row
, int col
, bool isReadOnly
)
10222 if ( CanHaveAttributes() )
10224 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10225 attr
->SetReadOnly(isReadOnly
);
10230 // ----------------------------------------------------------------------------
10231 // Data type registration
10232 // ----------------------------------------------------------------------------
10234 void wxGrid::RegisterDataType(const wxString
& typeName
,
10235 wxGridCellRenderer
* renderer
,
10236 wxGridCellEditor
* editor
)
10238 m_typeRegistry
->RegisterDataType(typeName
, renderer
, editor
);
10242 wxGridCellEditor
* wxGrid::GetDefaultEditorForCell(int row
, int col
) const
10244 wxString typeName
= m_table
->GetTypeName(row
, col
);
10245 return GetDefaultEditorForType(typeName
);
10248 wxGridCellRenderer
* wxGrid::GetDefaultRendererForCell(int row
, int col
) const
10250 wxString typeName
= m_table
->GetTypeName(row
, col
);
10251 return GetDefaultRendererForType(typeName
);
10254 wxGridCellEditor
* wxGrid::GetDefaultEditorForType(const wxString
& typeName
) const
10256 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
10257 if ( index
== wxNOT_FOUND
)
10259 wxFAIL_MSG(wxString::Format(wxT("Unknown data type name [%s]"), typeName
.c_str()));
10264 return m_typeRegistry
->GetEditor(index
);
10267 wxGridCellRenderer
* wxGrid::GetDefaultRendererForType(const wxString
& typeName
) const
10269 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
10270 if ( index
== wxNOT_FOUND
)
10272 wxFAIL_MSG(wxString::Format(wxT("Unknown data type name [%s]"), typeName
.c_str()));
10277 return m_typeRegistry
->GetRenderer(index
);
10280 // ----------------------------------------------------------------------------
10282 // ----------------------------------------------------------------------------
10284 void wxGrid::EnableDragRowSize( bool enable
)
10286 m_canDragRowSize
= enable
;
10289 void wxGrid::EnableDragColSize( bool enable
)
10291 m_canDragColSize
= enable
;
10294 void wxGrid::EnableDragGridSize( bool enable
)
10296 m_canDragGridSize
= enable
;
10299 void wxGrid::EnableDragCell( bool enable
)
10301 m_canDragCell
= enable
;
10304 void wxGrid::SetDefaultRowSize( int height
, bool resizeExistingRows
)
10306 m_defaultRowHeight
= wxMax( height
, m_minAcceptableRowHeight
);
10308 if ( resizeExistingRows
)
10310 // since we are resizing all rows to the default row size,
10311 // we can simply clear the row heights and row bottoms
10312 // arrays (which also allows us to take advantage of
10313 // some speed optimisations)
10314 m_rowHeights
.Empty();
10315 m_rowBottoms
.Empty();
10316 if ( !GetBatchCount() )
10321 void wxGrid::SetRowSize( int row
, int height
)
10323 wxCHECK_RET( row
>= 0 && row
< m_numRows
, _T("invalid row index") );
10325 // if < 0 then calculate new height from label
10329 wxArrayString lines
;
10330 wxClientDC
dc(m_rowLabelWin
);
10331 dc
.SetFont(GetLabelFont());
10332 StringToLines(GetRowLabelValue( row
), lines
);
10333 GetTextBoxSize( dc
, lines
, &w
, &h
);
10334 //check that it is not less than the minimal height
10335 height
= wxMax(h
, GetRowMinimalAcceptableHeight());
10338 // See comment in SetColSize
10339 if ( height
< GetRowMinimalAcceptableHeight())
10342 if ( m_rowHeights
.IsEmpty() )
10344 // need to really create the array
10348 int h
= wxMax( 0, height
);
10349 int diff
= h
- m_rowHeights
[row
];
10351 m_rowHeights
[row
] = h
;
10352 for ( int i
= row
; i
< m_numRows
; i
++ )
10354 m_rowBottoms
[i
] += diff
;
10357 if ( !GetBatchCount() )
10361 void wxGrid::SetDefaultColSize( int width
, bool resizeExistingCols
)
10363 // we dont allow zero default column width
10364 m_defaultColWidth
= wxMax( wxMax( width
, m_minAcceptableColWidth
), 1 );
10366 if ( resizeExistingCols
)
10368 // since we are resizing all columns to the default column size,
10369 // we can simply clear the col widths and col rights
10370 // arrays (which also allows us to take advantage of
10371 // some speed optimisations)
10372 m_colWidths
.Empty();
10373 m_colRights
.Empty();
10374 if ( !GetBatchCount() )
10379 void wxGrid::SetColSize( int col
, int width
)
10381 wxCHECK_RET( col
>= 0 && col
< m_numCols
, _T("invalid column index") );
10383 // if < 0 then calculate new width from label
10387 wxArrayString lines
;
10388 wxClientDC
dc(m_colWindow
);
10389 dc
.SetFont(GetLabelFont());
10390 StringToLines(GetColLabelValue(col
), lines
);
10391 if ( GetColLabelTextOrientation() == wxHORIZONTAL
)
10392 GetTextBoxSize( dc
, lines
, &w
, &h
);
10394 GetTextBoxSize( dc
, lines
, &h
, &w
);
10396 //check that it is not less than the minimal width
10397 width
= wxMax(width
, GetColMinimalAcceptableWidth());
10400 // we intentionally don't test whether the width is less than
10401 // GetColMinimalWidth() here but we do compare it with
10402 // GetColMinimalAcceptableWidth() as otherwise things currently break (see
10403 // #651) -- and we also always allow the width of 0 as it has the special
10404 // sense of hiding the column
10405 if ( width
> 0 && width
< GetColMinimalAcceptableWidth() )
10408 if ( m_colWidths
.IsEmpty() )
10410 // need to really create the array
10414 const int diff
= width
- m_colWidths
[col
];
10415 m_colWidths
[col
] = width
;
10416 if ( m_useNativeHeader
)
10417 GetGridColHeader()->UpdateColumn(col
);
10418 //else: will be refreshed when the header is redrawn
10420 for ( int colPos
= GetColPos(col
); colPos
< m_numCols
; colPos
++ )
10422 m_colRights
[GetColAt(colPos
)] += diff
;
10425 if ( !GetBatchCount() )
10432 void wxGrid::SetColMinimalWidth( int col
, int width
)
10434 if (width
> GetColMinimalAcceptableWidth())
10436 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)col
;
10437 m_colMinWidths
[key
] = width
;
10441 void wxGrid::SetRowMinimalHeight( int row
, int width
)
10443 if (width
> GetRowMinimalAcceptableHeight())
10445 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)row
;
10446 m_rowMinHeights
[key
] = width
;
10450 int wxGrid::GetColMinimalWidth(int col
) const
10452 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)col
;
10453 wxLongToLongHashMap::const_iterator it
= m_colMinWidths
.find(key
);
10455 return it
!= m_colMinWidths
.end() ? (int)it
->second
: m_minAcceptableColWidth
;
10458 int wxGrid::GetRowMinimalHeight(int row
) const
10460 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)row
;
10461 wxLongToLongHashMap::const_iterator it
= m_rowMinHeights
.find(key
);
10463 return it
!= m_rowMinHeights
.end() ? (int)it
->second
: m_minAcceptableRowHeight
;
10466 void wxGrid::SetColMinimalAcceptableWidth( int width
)
10468 // We do allow a width of 0 since this gives us
10469 // an easy way to temporarily hiding columns.
10471 m_minAcceptableColWidth
= width
;
10474 void wxGrid::SetRowMinimalAcceptableHeight( int height
)
10476 // We do allow a height of 0 since this gives us
10477 // an easy way to temporarily hiding rows.
10479 m_minAcceptableRowHeight
= height
;
10482 int wxGrid::GetColMinimalAcceptableWidth() const
10484 return m_minAcceptableColWidth
;
10487 int wxGrid::GetRowMinimalAcceptableHeight() const
10489 return m_minAcceptableRowHeight
;
10492 // ----------------------------------------------------------------------------
10494 // ----------------------------------------------------------------------------
10497 wxGrid::AutoSizeColOrRow(int colOrRow
, bool setAsMin
, wxGridDirection direction
)
10499 const bool column
= direction
== wxGRID_COLUMN
;
10501 wxClientDC
dc(m_gridWin
);
10503 // cancel editing of cell
10504 HideCellEditControl();
10505 SaveEditControlValue();
10507 // init both of them to avoid compiler warnings, even if we only need one
10515 wxCoord extent
, extentMax
= 0;
10516 int max
= column
? m_numRows
: m_numCols
;
10517 for ( int rowOrCol
= 0; rowOrCol
< max
; rowOrCol
++ )
10524 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
10525 wxGridCellRenderer
*renderer
= attr
->GetRenderer(this, row
, col
);
10528 wxSize size
= renderer
->GetBestSize(*this, *attr
, dc
, row
, col
);
10529 extent
= column
? size
.x
: size
.y
;
10530 if ( extent
> extentMax
)
10531 extentMax
= extent
;
10533 renderer
->DecRef();
10539 // now also compare with the column label extent
10541 dc
.SetFont( GetLabelFont() );
10545 dc
.GetMultiLineTextExtent( GetColLabelValue(col
), &w
, &h
);
10546 if ( GetColLabelTextOrientation() == wxVERTICAL
)
10550 dc
.GetMultiLineTextExtent( GetRowLabelValue(row
), &w
, &h
);
10552 extent
= column
? w
: h
;
10553 if ( extent
> extentMax
)
10554 extentMax
= extent
;
10558 // empty column - give default extent (notice that if extentMax is less
10559 // than default extent but != 0, it's OK)
10560 extentMax
= column
? m_defaultColWidth
: m_defaultRowHeight
;
10565 // leave some space around text
10573 // Ensure automatic width is not less than minimal width. See the
10574 // comment in SetColSize() for explanation of why this isn't done
10575 // in SetColSize().
10577 extentMax
= wxMax(extentMax
, GetColMinimalWidth(col
));
10579 SetColSize( col
, extentMax
);
10580 if ( !GetBatchCount() )
10582 if ( m_useNativeHeader
)
10584 GetGridColHeader()->UpdateColumn(col
);
10589 m_gridWin
->GetClientSize( &cw
, &ch
);
10590 wxRect
rect ( CellToRect( 0, col
) );
10592 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
10593 rect
.width
= cw
- rect
.x
;
10594 rect
.height
= m_colLabelHeight
;
10595 GetColLabelWindow()->Refresh( true, &rect
);
10601 // Ensure automatic width is not less than minimal height. See the
10602 // comment in SetColSize() for explanation of why this isn't done
10603 // in SetRowSize().
10605 extentMax
= wxMax(extentMax
, GetRowMinimalHeight(row
));
10607 SetRowSize(row
, extentMax
);
10608 if ( !GetBatchCount() )
10611 m_gridWin
->GetClientSize( &cw
, &ch
);
10612 wxRect
rect( CellToRect( row
, 0 ) );
10614 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
10615 rect
.width
= m_rowLabelWidth
;
10616 rect
.height
= ch
- rect
.y
;
10617 m_rowLabelWin
->Refresh( true, &rect
);
10624 SetColMinimalWidth(col
, extentMax
);
10626 SetRowMinimalHeight(row
, extentMax
);
10630 wxCoord
wxGrid::CalcColOrRowLabelAreaMinSize(wxGridDirection direction
)
10632 // calculate size for the rows or columns?
10633 const bool calcRows
= direction
== wxGRID_ROW
;
10635 wxClientDC
dc(calcRows
? GetGridRowLabelWindow()
10636 : GetGridColLabelWindow());
10637 dc
.SetFont(GetLabelFont());
10639 // which dimension should we take into account for calculations?
10641 // for columns, the text can be only horizontal so it's easy but for rows
10642 // we also have to take into account the text orientation
10644 useWidth
= calcRows
|| (GetColLabelTextOrientation() == wxVERTICAL
);
10646 wxArrayString lines
;
10647 wxCoord extentMax
= 0;
10649 const int numRowsOrCols
= calcRows
? m_numRows
: m_numCols
;
10650 for ( int rowOrCol
= 0; rowOrCol
< numRowsOrCols
; rowOrCol
++ )
10654 wxString label
= calcRows
? GetRowLabelValue(rowOrCol
)
10655 : GetColLabelValue(rowOrCol
);
10656 StringToLines(label
, lines
);
10659 GetTextBoxSize(dc
, lines
, &w
, &h
);
10661 const wxCoord extent
= useWidth
? w
: h
;
10662 if ( extent
> extentMax
)
10663 extentMax
= extent
;
10668 // empty column - give default extent (notice that if extentMax is less
10669 // than default extent but != 0, it's OK)
10670 extentMax
= calcRows
? GetDefaultRowLabelSize()
10671 : GetDefaultColLabelSize();
10674 // leave some space around text (taken from AutoSizeColOrRow)
10683 int wxGrid::SetOrCalcColumnSizes(bool calcOnly
, bool setAsMin
)
10685 int width
= m_rowLabelWidth
;
10687 wxGridUpdateLocker locker
;
10689 locker
.Create(this);
10691 for ( int col
= 0; col
< m_numCols
; col
++ )
10694 AutoSizeColumn(col
, setAsMin
);
10696 width
+= GetColWidth(col
);
10702 int wxGrid::SetOrCalcRowSizes(bool calcOnly
, bool setAsMin
)
10704 int height
= m_colLabelHeight
;
10706 wxGridUpdateLocker locker
;
10708 locker
.Create(this);
10710 for ( int row
= 0; row
< m_numRows
; row
++ )
10713 AutoSizeRow(row
, setAsMin
);
10715 height
+= GetRowHeight(row
);
10721 void wxGrid::AutoSize()
10723 wxGridUpdateLocker
locker(this);
10725 wxSize
size(SetOrCalcColumnSizes(false) - m_rowLabelWidth
+ m_extraWidth
,
10726 SetOrCalcRowSizes(false) - m_colLabelHeight
+ m_extraHeight
);
10728 // we know that we're not going to have scrollbars so disable them now to
10729 // avoid trouble in SetClientSize() which can otherwise set the correct
10730 // client size but also leave space for (not needed any more) scrollbars
10731 SetScrollbars(0, 0, 0, 0, 0, 0, true);
10733 // restore the scroll rate parameters overwritten by SetScrollbars()
10734 SetScrollRate(m_scrollLineX
, m_scrollLineY
);
10736 SetClientSize(size
.x
+ m_rowLabelWidth
, size
.y
+ m_colLabelHeight
);
10739 void wxGrid::AutoSizeRowLabelSize( int row
)
10741 // Hide the edit control, so it
10742 // won't interfere with drag-shrinking.
10743 if ( IsCellEditControlShown() )
10745 HideCellEditControl();
10746 SaveEditControlValue();
10749 // autosize row height depending on label text
10750 SetRowSize(row
, -1);
10754 void wxGrid::AutoSizeColLabelSize( int col
)
10756 // Hide the edit control, so it
10757 // won't interfere with drag-shrinking.
10758 if ( IsCellEditControlShown() )
10760 HideCellEditControl();
10761 SaveEditControlValue();
10764 // autosize column width depending on label text
10765 SetColSize(col
, -1);
10769 wxSize
wxGrid::DoGetBestSize() const
10771 wxGrid
*self
= (wxGrid
*)this; // const_cast
10773 // we do the same as in AutoSize() here with the exception that we don't
10774 // change the column/row sizes, only calculate them
10775 wxSize
size(self
->SetOrCalcColumnSizes(true) - m_rowLabelWidth
+ m_extraWidth
,
10776 self
->SetOrCalcRowSizes(true) - m_colLabelHeight
+ m_extraHeight
);
10778 // NOTE: This size should be cached, but first we need to add calls to
10779 // InvalidateBestSize everywhere that could change the results of this
10781 // CacheBestSize(size);
10783 return wxSize(size
.x
+ m_rowLabelWidth
, size
.y
+ m_colLabelHeight
)
10784 + GetWindowBorderSize();
10792 wxPen
& wxGrid::GetDividerPen() const
10797 // ----------------------------------------------------------------------------
10798 // cell value accessor functions
10799 // ----------------------------------------------------------------------------
10801 void wxGrid::SetCellValue( int row
, int col
, const wxString
& s
)
10805 m_table
->SetValue( row
, col
, s
);
10806 if ( !GetBatchCount() )
10809 wxRect
rect( CellToRect( row
, col
) );
10811 rect
.width
= m_gridWin
->GetClientSize().GetWidth();
10812 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
10813 m_gridWin
->Refresh( false, &rect
);
10816 if ( m_currentCellCoords
.GetRow() == row
&&
10817 m_currentCellCoords
.GetCol() == col
&&
10818 IsCellEditControlShown())
10819 // Note: If we are using IsCellEditControlEnabled,
10820 // this interacts badly with calling SetCellValue from
10821 // an EVT_GRID_CELL_CHANGE handler.
10823 HideCellEditControl();
10824 ShowCellEditControl(); // will reread data from table
10829 // ----------------------------------------------------------------------------
10830 // block, row and column selection
10831 // ----------------------------------------------------------------------------
10833 void wxGrid::SelectRow( int row
, bool addToSelected
)
10835 if ( !m_selection
)
10838 if ( !addToSelected
)
10841 m_selection
->SelectRow(row
);
10844 void wxGrid::SelectCol( int col
, bool addToSelected
)
10846 if ( !m_selection
)
10849 if ( !addToSelected
)
10852 m_selection
->SelectCol(col
);
10855 void wxGrid::SelectBlock(int topRow
, int leftCol
, int bottomRow
, int rightCol
,
10856 bool addToSelected
)
10858 if ( !m_selection
)
10861 if ( !addToSelected
)
10864 m_selection
->SelectBlock(topRow
, leftCol
, bottomRow
, rightCol
);
10867 void wxGrid::SelectAll()
10869 if ( m_numRows
> 0 && m_numCols
> 0 )
10872 m_selection
->SelectBlock( 0, 0, m_numRows
- 1, m_numCols
- 1 );
10876 // ----------------------------------------------------------------------------
10877 // cell, row and col deselection
10878 // ----------------------------------------------------------------------------
10880 void wxGrid::DeselectLine(int line
, const wxGridOperations
& oper
)
10882 if ( !m_selection
)
10885 const wxGridSelectionModes mode
= m_selection
->GetSelectionMode();
10886 if ( mode
== oper
.GetSelectionMode() )
10888 const wxGridCellCoords
c(oper
.MakeCoords(line
, 0));
10889 if ( m_selection
->IsInSelection(c
) )
10890 m_selection
->ToggleCellSelection(c
);
10892 else if ( mode
!= oper
.Dual().GetSelectionMode() )
10894 const int nOther
= oper
.Dual().GetNumberOfLines(this);
10895 for ( int i
= 0; i
< nOther
; i
++ )
10897 const wxGridCellCoords
c(oper
.MakeCoords(line
, i
));
10898 if ( m_selection
->IsInSelection(c
) )
10899 m_selection
->ToggleCellSelection(c
);
10902 //else: can only select orthogonal lines so no lines in this direction
10903 // could have been selected anyhow
10906 void wxGrid::DeselectRow(int row
)
10908 DeselectLine(row
, wxGridRowOperations());
10911 void wxGrid::DeselectCol(int col
)
10913 DeselectLine(col
, wxGridColumnOperations());
10916 void wxGrid::DeselectCell( int row
, int col
)
10918 if ( m_selection
&& m_selection
->IsInSelection(row
, col
) )
10919 m_selection
->ToggleCellSelection(row
, col
);
10922 bool wxGrid::IsSelection() const
10924 return ( m_selection
&& (m_selection
->IsSelection() ||
10925 ( m_selectedBlockTopLeft
!= wxGridNoCellCoords
&&
10926 m_selectedBlockBottomRight
!= wxGridNoCellCoords
) ) );
10929 bool wxGrid::IsInSelection( int row
, int col
) const
10931 return ( m_selection
&& (m_selection
->IsInSelection( row
, col
) ||
10932 ( row
>= m_selectedBlockTopLeft
.GetRow() &&
10933 col
>= m_selectedBlockTopLeft
.GetCol() &&
10934 row
<= m_selectedBlockBottomRight
.GetRow() &&
10935 col
<= m_selectedBlockBottomRight
.GetCol() )) );
10938 wxGridCellCoordsArray
wxGrid::GetSelectedCells() const
10942 wxGridCellCoordsArray a
;
10946 return m_selection
->m_cellSelection
;
10949 wxGridCellCoordsArray
wxGrid::GetSelectionBlockTopLeft() const
10953 wxGridCellCoordsArray a
;
10957 return m_selection
->m_blockSelectionTopLeft
;
10960 wxGridCellCoordsArray
wxGrid::GetSelectionBlockBottomRight() const
10964 wxGridCellCoordsArray a
;
10968 return m_selection
->m_blockSelectionBottomRight
;
10971 wxArrayInt
wxGrid::GetSelectedRows() const
10979 return m_selection
->m_rowSelection
;
10982 wxArrayInt
wxGrid::GetSelectedCols() const
10990 return m_selection
->m_colSelection
;
10993 void wxGrid::ClearSelection()
10995 wxRect r1
= BlockToDeviceRect(m_selectedBlockTopLeft
,
10996 m_selectedBlockBottomRight
);
10997 wxRect r2
= BlockToDeviceRect(m_currentCellCoords
,
10998 m_selectedBlockCorner
);
11000 m_selectedBlockTopLeft
=
11001 m_selectedBlockBottomRight
=
11002 m_selectedBlockCorner
= wxGridNoCellCoords
;
11004 Refresh( false, &r1
);
11005 Refresh( false, &r2
);
11008 m_selection
->ClearSelection();
11011 // This function returns the rectangle that encloses the given block
11012 // in device coords clipped to the client size of the grid window.
11014 wxRect
wxGrid::BlockToDeviceRect( const wxGridCellCoords
& topLeft
,
11015 const wxGridCellCoords
& bottomRight
) const
11018 wxRect tempCellRect
= CellToRect(topLeft
);
11019 if ( tempCellRect
!= wxGridNoCellRect
)
11021 resultRect
= tempCellRect
;
11025 resultRect
= wxRect(0, 0, 0, 0);
11028 tempCellRect
= CellToRect(bottomRight
);
11029 if ( tempCellRect
!= wxGridNoCellRect
)
11031 resultRect
+= tempCellRect
;
11035 // If both inputs were "wxGridNoCellRect," then there's nothing to do.
11036 return wxGridNoCellRect
;
11039 // Ensure that left/right and top/bottom pairs are in order.
11040 int left
= resultRect
.GetLeft();
11041 int top
= resultRect
.GetTop();
11042 int right
= resultRect
.GetRight();
11043 int bottom
= resultRect
.GetBottom();
11045 int leftCol
= topLeft
.GetCol();
11046 int topRow
= topLeft
.GetRow();
11047 int rightCol
= bottomRight
.GetCol();
11048 int bottomRow
= bottomRight
.GetRow();
11057 leftCol
= rightCol
;
11068 topRow
= bottomRow
;
11072 // The following loop is ONLY necessary to detect and handle merged cells.
11074 m_gridWin
->GetClientSize( &cw
, &ch
);
11076 // Get the origin coordinates: notice that they will be negative if the
11077 // grid is scrolled downwards/to the right.
11078 int gridOriginX
= 0;
11079 int gridOriginY
= 0;
11080 CalcScrolledPosition(gridOriginX
, gridOriginY
, &gridOriginX
, &gridOriginY
);
11082 int onScreenLeftmostCol
= internalXToCol(-gridOriginX
);
11083 int onScreenUppermostRow
= internalYToRow(-gridOriginY
);
11085 int onScreenRightmostCol
= internalXToCol(-gridOriginX
+ cw
);
11086 int onScreenBottommostRow
= internalYToRow(-gridOriginY
+ ch
);
11088 // Bound our loop so that we only examine the portion of the selected block
11089 // that is shown on screen. Therefore, we compare the Top-Left block values
11090 // to the Top-Left screen values, and the Bottom-Right block values to the
11091 // Bottom-Right screen values, choosing appropriately.
11092 const int visibleTopRow
= wxMax(topRow
, onScreenUppermostRow
);
11093 const int visibleBottomRow
= wxMin(bottomRow
, onScreenBottommostRow
);
11094 const int visibleLeftCol
= wxMax(leftCol
, onScreenLeftmostCol
);
11095 const int visibleRightCol
= wxMin(rightCol
, onScreenRightmostCol
);
11097 for ( int j
= visibleTopRow
; j
<= visibleBottomRow
; j
++ )
11099 for ( int i
= visibleLeftCol
; i
<= visibleRightCol
; i
++ )
11101 if ( (j
== visibleTopRow
) || (j
== visibleBottomRow
) ||
11102 (i
== visibleLeftCol
) || (i
== visibleRightCol
) )
11104 tempCellRect
= CellToRect( j
, i
);
11106 if (tempCellRect
.x
< left
)
11107 left
= tempCellRect
.x
;
11108 if (tempCellRect
.y
< top
)
11109 top
= tempCellRect
.y
;
11110 if (tempCellRect
.x
+ tempCellRect
.width
> right
)
11111 right
= tempCellRect
.x
+ tempCellRect
.width
;
11112 if (tempCellRect
.y
+ tempCellRect
.height
> bottom
)
11113 bottom
= tempCellRect
.y
+ tempCellRect
.height
;
11117 i
= visibleRightCol
; // jump over inner cells.
11122 // Convert to scrolled coords
11123 CalcScrolledPosition( left
, top
, &left
, &top
);
11124 CalcScrolledPosition( right
, bottom
, &right
, &bottom
);
11126 if (right
< 0 || bottom
< 0 || left
> cw
|| top
> ch
)
11127 return wxRect(0,0,0,0);
11129 resultRect
.SetLeft( wxMax(0, left
) );
11130 resultRect
.SetTop( wxMax(0, top
) );
11131 resultRect
.SetRight( wxMin(cw
, right
) );
11132 resultRect
.SetBottom( wxMin(ch
, bottom
) );
11137 // ----------------------------------------------------------------------------
11139 // ----------------------------------------------------------------------------
11141 #if wxUSE_DRAG_AND_DROP
11143 // this allow setting drop target directly on wxGrid
11144 void wxGrid::SetDropTarget(wxDropTarget
*dropTarget
)
11146 GetGridWindow()->SetDropTarget(dropTarget
);
11149 #endif // wxUSE_DRAG_AND_DROP
11151 // ----------------------------------------------------------------------------
11152 // grid event classes
11153 // ----------------------------------------------------------------------------
11155 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent
, wxNotifyEvent
)
11157 wxGridEvent::wxGridEvent( int id
, wxEventType type
, wxObject
* obj
,
11158 int row
, int col
, int x
, int y
, bool sel
,
11159 bool control
, bool shift
, bool alt
, bool meta
)
11160 : wxNotifyEvent( type
, id
),
11161 wxKeyboardState(control
, shift
, alt
, meta
)
11163 Init(row
, col
, x
, y
, sel
);
11165 SetEventObject(obj
);
11168 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent
, wxNotifyEvent
)
11170 wxGridSizeEvent::wxGridSizeEvent( int id
, wxEventType type
, wxObject
* obj
,
11171 int rowOrCol
, int x
, int y
,
11172 bool control
, bool shift
, bool alt
, bool meta
)
11173 : wxNotifyEvent( type
, id
),
11174 wxKeyboardState(control
, shift
, alt
, meta
)
11176 Init(rowOrCol
, x
, y
);
11178 SetEventObject(obj
);
11182 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent
, wxNotifyEvent
)
11184 wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id
, wxEventType type
, wxObject
* obj
,
11185 const wxGridCellCoords
& topLeft
,
11186 const wxGridCellCoords
& bottomRight
,
11187 bool sel
, bool control
,
11188 bool shift
, bool alt
, bool meta
)
11189 : wxNotifyEvent( type
, id
),
11190 wxKeyboardState(control
, shift
, alt
, meta
)
11192 Init(topLeft
, bottomRight
, sel
);
11194 SetEventObject(obj
);
11198 IMPLEMENT_DYNAMIC_CLASS(wxGridEditorCreatedEvent
, wxCommandEvent
)
11200 wxGridEditorCreatedEvent::wxGridEditorCreatedEvent(int id
, wxEventType type
,
11201 wxObject
* obj
, int row
,
11202 int col
, wxControl
* ctrl
)
11203 : wxCommandEvent(type
, id
)
11205 SetEventObject(obj
);
11211 #endif // wxUSE_GRID