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"
51 #include "wx/generic/gridsel.h"
53 const wxChar wxGridNameStr
[] = wxT("grid");
55 #if defined(__WXMOTIF__)
56 #define WXUNUSED_MOTIF(identifier) WXUNUSED(identifier)
58 #define WXUNUSED_MOTIF(identifier) identifier
61 #if defined(__WXGTK__)
62 #define WXUNUSED_GTK(identifier) WXUNUSED(identifier)
64 #define WXUNUSED_GTK(identifier) identifier
67 // Required for wxIs... functions
70 // ----------------------------------------------------------------------------
72 // ----------------------------------------------------------------------------
74 WX_DEFINE_ARRAY_WITH_DECL_PTR(wxGridCellAttr
*, wxArrayAttrs
,
75 class WXDLLIMPEXP_ADV
);
77 struct wxGridCellWithAttr
79 wxGridCellWithAttr(int row
, int col
, wxGridCellAttr
*attr_
)
80 : coords(row
, col
), attr(attr_
)
85 wxGridCellWithAttr(const wxGridCellWithAttr
& other
)
86 : coords(other
.coords
),
92 wxGridCellWithAttr
& operator=(const wxGridCellWithAttr
& other
)
94 coords
= other
.coords
;
95 if (attr
!= other
.attr
)
104 void ChangeAttr(wxGridCellAttr
* new_attr
)
106 if (attr
!= new_attr
)
108 // "Delete" (i.e. DecRef) the old attribute.
111 // Take ownership of the new attribute, i.e. no IncRef.
115 ~wxGridCellWithAttr()
120 wxGridCellCoords coords
;
121 wxGridCellAttr
*attr
;
124 WX_DECLARE_OBJARRAY_WITH_DECL(wxGridCellWithAttr
, wxGridCellWithAttrArray
,
125 class WXDLLIMPEXP_ADV
);
127 #include "wx/arrimpl.cpp"
129 WX_DEFINE_OBJARRAY(wxGridCellCoordsArray
)
130 WX_DEFINE_OBJARRAY(wxGridCellWithAttrArray
)
132 // ----------------------------------------------------------------------------
134 // ----------------------------------------------------------------------------
136 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_LEFT_CLICK
)
137 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_RIGHT_CLICK
)
138 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_LEFT_DCLICK
)
139 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_RIGHT_DCLICK
)
140 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_BEGIN_DRAG
)
141 DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_LEFT_CLICK
)
142 DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_RIGHT_CLICK
)
143 DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_LEFT_DCLICK
)
144 DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_RIGHT_DCLICK
)
145 DEFINE_EVENT_TYPE(wxEVT_GRID_ROW_SIZE
)
146 DEFINE_EVENT_TYPE(wxEVT_GRID_COL_SIZE
)
147 DEFINE_EVENT_TYPE(wxEVT_GRID_COL_MOVE
)
148 DEFINE_EVENT_TYPE(wxEVT_GRID_RANGE_SELECT
)
149 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_CHANGE
)
150 DEFINE_EVENT_TYPE(wxEVT_GRID_SELECT_CELL
)
151 DEFINE_EVENT_TYPE(wxEVT_GRID_EDITOR_SHOWN
)
152 DEFINE_EVENT_TYPE(wxEVT_GRID_EDITOR_HIDDEN
)
153 DEFINE_EVENT_TYPE(wxEVT_GRID_EDITOR_CREATED
)
155 // ----------------------------------------------------------------------------
157 // ----------------------------------------------------------------------------
159 // common base class for various grid subwindows
160 class WXDLLIMPEXP_ADV wxGridSubwindow
: public wxWindow
163 wxGridSubwindow() { m_owner
= NULL
; }
164 wxGridSubwindow(wxGrid
*owner
,
168 int additionalStyle
= 0,
169 const wxString
& name
= wxPanelNameStr
)
170 : wxWindow(owner
, id
, pos
, size
,
171 wxBORDER_NONE
| additionalStyle
,
177 virtual bool AcceptsFocus() const { return false; }
179 wxGrid
*GetOwner() { return m_owner
; }
182 void OnMouseCaptureLost(wxMouseCaptureLostEvent
& event
);
186 DECLARE_EVENT_TABLE()
187 DECLARE_NO_COPY_CLASS(wxGridSubwindow
)
190 class WXDLLIMPEXP_ADV wxGridRowLabelWindow
: public wxGridSubwindow
193 wxGridRowLabelWindow() { }
194 wxGridRowLabelWindow( wxGrid
*parent
, wxWindowID id
,
195 const wxPoint
&pos
, const wxSize
&size
);
198 void OnPaint( wxPaintEvent
& event
);
199 void OnMouseEvent( wxMouseEvent
& event
);
200 void OnMouseWheel( wxMouseEvent
& event
);
202 DECLARE_DYNAMIC_CLASS(wxGridRowLabelWindow
)
203 DECLARE_EVENT_TABLE()
204 DECLARE_NO_COPY_CLASS(wxGridRowLabelWindow
)
208 class WXDLLIMPEXP_ADV wxGridColLabelWindow
: public wxGridSubwindow
211 wxGridColLabelWindow() { }
212 wxGridColLabelWindow( wxGrid
*parent
, wxWindowID id
,
213 const wxPoint
&pos
, const wxSize
&size
);
216 void OnPaint( wxPaintEvent
& event
);
217 void OnMouseEvent( wxMouseEvent
& event
);
218 void OnMouseWheel( wxMouseEvent
& event
);
220 DECLARE_DYNAMIC_CLASS(wxGridColLabelWindow
)
221 DECLARE_EVENT_TABLE()
222 DECLARE_NO_COPY_CLASS(wxGridColLabelWindow
)
226 class WXDLLIMPEXP_ADV wxGridCornerLabelWindow
: public wxGridSubwindow
229 wxGridCornerLabelWindow() { }
230 wxGridCornerLabelWindow( wxGrid
*parent
, wxWindowID id
,
231 const wxPoint
&pos
, const wxSize
&size
);
234 void OnMouseEvent( wxMouseEvent
& event
);
235 void OnMouseWheel( wxMouseEvent
& event
);
236 void OnPaint( wxPaintEvent
& event
);
238 DECLARE_DYNAMIC_CLASS(wxGridCornerLabelWindow
)
239 DECLARE_EVENT_TABLE()
240 DECLARE_NO_COPY_CLASS(wxGridCornerLabelWindow
)
243 class WXDLLIMPEXP_ADV wxGridWindow
: public wxGridSubwindow
248 m_rowLabelWin
= NULL
;
249 m_colLabelWin
= NULL
;
252 wxGridWindow( wxGrid
*parent
,
253 wxGridRowLabelWindow
*rowLblWin
,
254 wxGridColLabelWindow
*colLblWin
,
255 wxWindowID id
, const wxPoint
&pos
, const wxSize
&size
);
257 void ScrollWindow( int dx
, int dy
, const wxRect
*rect
);
259 virtual bool AcceptsFocus() const { return true; }
262 wxGridRowLabelWindow
*m_rowLabelWin
;
263 wxGridColLabelWindow
*m_colLabelWin
;
265 void OnPaint( wxPaintEvent
&event
);
266 void OnMouseWheel( wxMouseEvent
& event
);
267 void OnMouseEvent( wxMouseEvent
& event
);
268 void OnKeyDown( wxKeyEvent
& );
269 void OnKeyUp( wxKeyEvent
& );
270 void OnChar( wxKeyEvent
& );
271 void OnEraseBackground( wxEraseEvent
& );
272 void OnFocus( wxFocusEvent
& );
274 DECLARE_DYNAMIC_CLASS(wxGridWindow
)
275 DECLARE_EVENT_TABLE()
276 DECLARE_NO_COPY_CLASS(wxGridWindow
)
280 class wxGridCellEditorEvtHandler
: public wxEvtHandler
283 wxGridCellEditorEvtHandler(wxGrid
* grid
, wxGridCellEditor
* editor
)
290 void OnKillFocus(wxFocusEvent
& event
);
291 void OnKeyDown(wxKeyEvent
& event
);
292 void OnChar(wxKeyEvent
& event
);
294 void SetInSetFocus(bool inSetFocus
) { m_inSetFocus
= inSetFocus
; }
298 wxGridCellEditor
*m_editor
;
300 // Work around the fact that a focus kill event can be sent to
301 // a combobox within a set focus event.
304 DECLARE_EVENT_TABLE()
305 DECLARE_DYNAMIC_CLASS(wxGridCellEditorEvtHandler
)
306 DECLARE_NO_COPY_CLASS(wxGridCellEditorEvtHandler
)
310 IMPLEMENT_ABSTRACT_CLASS(wxGridCellEditorEvtHandler
, wxEvtHandler
)
312 BEGIN_EVENT_TABLE( wxGridCellEditorEvtHandler
, wxEvtHandler
)
313 EVT_KILL_FOCUS( wxGridCellEditorEvtHandler::OnKillFocus
)
314 EVT_KEY_DOWN( wxGridCellEditorEvtHandler::OnKeyDown
)
315 EVT_CHAR( wxGridCellEditorEvtHandler::OnChar
)
319 // ----------------------------------------------------------------------------
320 // the internal data representation used by wxGridCellAttrProvider
321 // ----------------------------------------------------------------------------
323 // this class stores attributes set for cells
324 class WXDLLIMPEXP_ADV wxGridCellAttrData
327 void SetAttr(wxGridCellAttr
*attr
, int row
, int col
);
328 wxGridCellAttr
*GetAttr(int row
, int col
) const;
329 void UpdateAttrRows( size_t pos
, int numRows
);
330 void UpdateAttrCols( size_t pos
, int numCols
);
333 // searches for the attr for given cell, returns wxNOT_FOUND if not found
334 int FindIndex(int row
, int col
) const;
336 wxGridCellWithAttrArray m_attrs
;
339 // this class stores attributes set for rows or columns
340 class WXDLLIMPEXP_ADV wxGridRowOrColAttrData
343 // empty ctor to suppress warnings
344 wxGridRowOrColAttrData() {}
345 ~wxGridRowOrColAttrData();
347 void SetAttr(wxGridCellAttr
*attr
, int rowOrCol
);
348 wxGridCellAttr
*GetAttr(int rowOrCol
) const;
349 void UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
);
352 wxArrayInt m_rowsOrCols
;
353 wxArrayAttrs m_attrs
;
356 // NB: this is just a wrapper around 3 objects: one which stores cell
357 // attributes, and 2 others for row/col ones
358 class WXDLLIMPEXP_ADV wxGridCellAttrProviderData
361 wxGridCellAttrData m_cellAttrs
;
362 wxGridRowOrColAttrData m_rowAttrs
,
367 // ----------------------------------------------------------------------------
368 // data structures used for the data type registry
369 // ----------------------------------------------------------------------------
371 struct wxGridDataTypeInfo
373 wxGridDataTypeInfo(const wxString
& typeName
,
374 wxGridCellRenderer
* renderer
,
375 wxGridCellEditor
* editor
)
376 : m_typeName(typeName
), m_renderer(renderer
), m_editor(editor
)
379 ~wxGridDataTypeInfo()
381 wxSafeDecRef(m_renderer
);
382 wxSafeDecRef(m_editor
);
386 wxGridCellRenderer
* m_renderer
;
387 wxGridCellEditor
* m_editor
;
389 DECLARE_NO_COPY_CLASS(wxGridDataTypeInfo
)
393 WX_DEFINE_ARRAY_WITH_DECL_PTR(wxGridDataTypeInfo
*, wxGridDataTypeInfoArray
,
394 class WXDLLIMPEXP_ADV
);
397 class WXDLLIMPEXP_ADV wxGridTypeRegistry
400 wxGridTypeRegistry() {}
401 ~wxGridTypeRegistry();
403 void RegisterDataType(const wxString
& typeName
,
404 wxGridCellRenderer
* renderer
,
405 wxGridCellEditor
* editor
);
407 // find one of already registered data types
408 int FindRegisteredDataType(const wxString
& typeName
);
410 // try to FindRegisteredDataType(), if this fails and typeName is one of
411 // standard typenames, register it and return its index
412 int FindDataType(const wxString
& typeName
);
414 // try to FindDataType(), if it fails see if it is not one of already
415 // registered data types with some params in which case clone the
416 // registered data type and set params for it
417 int FindOrCloneDataType(const wxString
& typeName
);
419 wxGridCellRenderer
* GetRenderer(int index
);
420 wxGridCellEditor
* GetEditor(int index
);
423 wxGridDataTypeInfoArray m_typeinfo
;
426 // ----------------------------------------------------------------------------
427 // operations classes abstracting the difference between operating on rows and
429 // ----------------------------------------------------------------------------
431 // This class allows to write a function only once because by using its methods
432 // it will apply to both columns and rows.
434 // This is an abstract interface definition, the two concrete implementations
435 // below should be used when working with rows and columns respectively.
436 class wxGridOperations
439 // Returns the operations in the other direction, i.e. wxGridRowOperations
440 // if this object is a wxGridColumnOperations and vice versa.
441 virtual wxGridOperations
& Dual() const = 0;
443 // Return the number of rows or columns.
444 virtual int GetNumberOfLines(const wxGrid
*grid
) const = 0;
446 // Return the selection mode which allows selecting rows or columns.
447 virtual wxGrid::wxGridSelectionModes
GetSelectionMode() const = 0;
449 // Make a wxGridCellCoords from the given components: thisDir is row or
450 // column and otherDir is column or row
451 virtual wxGridCellCoords
MakeCoords(int thisDir
, int otherDir
) const = 0;
453 // Calculate the scrolled position of the given abscissa or ordinate.
454 virtual int CalcScrolledPosition(wxGrid
*grid
, int pos
) const = 0;
456 // Selects the horizontal or vertical component from the given object.
457 virtual int Select(const wxGridCellCoords
& coords
) const = 0;
458 virtual int Select(const wxPoint
& pt
) const = 0;
459 virtual int Select(const wxSize
& sz
) const = 0;
460 virtual int Select(const wxRect
& r
) const = 0;
461 virtual int& Select(wxRect
& r
) const = 0;
463 // Returns width or height of the rectangle
464 virtual int& SelectSize(wxRect
& r
) const = 0;
466 // Make a wxSize such that Select() applied to it returns first component
467 virtual wxSize
MakeSize(int first
, int second
) const = 0;
469 // Sets the row or column component of the given cell coordinates
470 virtual void Set(wxGridCellCoords
& coords
, int line
) const = 0;
473 // Draws a line parallel to the row or column, i.e. horizontal or vertical:
474 // pos is the vertical or horizontal position of the line and start and end
475 // are the coordinates of the line extremities in the other direction
477 DrawParallelLine(wxDC
& dc
, int start
, int end
, int pos
) const = 0;
480 // Return the row or column at the given pixel coordinate.
482 PosToLine(const wxGrid
*grid
, int pos
, bool clip
= false) const = 0;
484 // Get the top/left position, in pixels, of the given row or column
485 virtual int GetLineStartPos(const wxGrid
*grid
, int line
) const = 0;
487 // Get the bottom/right position, in pixels, of the given row or column
488 virtual int GetLineEndPos(const wxGrid
*grid
, int line
) const = 0;
490 // Get the height/width of the given row/column
491 virtual int GetLineSize(const wxGrid
*grid
, int line
) const = 0;
493 // Get wxGrid::m_rowBottoms/m_colRights array
494 virtual const wxArrayInt
& GetLineEnds(const wxGrid
*grid
) const = 0;
496 // Get default height row height or column width
497 virtual int GetDefaultLineSize(const wxGrid
*grid
) const = 0;
499 // Return the minimal acceptable row height or column width
500 virtual int GetMinimalAcceptableLineSize(const wxGrid
*grid
) const = 0;
502 // Return the minimal row height or column width
503 virtual int GetMinimalLineSize(const wxGrid
*grid
, int line
) const = 0;
505 // Set the row height or column width
506 virtual void SetLineSize(wxGrid
*grid
, int line
, int size
) const = 0;
508 // True if rows/columns can be resized by user
509 virtual bool CanResizeLines(const wxGrid
*grid
) const = 0;
512 // Return the index of the line at the given position
514 // NB: currently this is always identity for the rows as reordering is only
515 // implemented for the lines
516 virtual int GetLineAt(const wxGrid
*grid
, int line
) const = 0;
519 // Get the row or column label window
520 virtual wxWindow
*GetHeaderWindow(wxGrid
*grid
) const = 0;
522 // Get the width or height of the row or column label window
523 virtual int GetHeaderWindowSize(wxGrid
*grid
) const = 0;
526 // This class is never used polymorphically but give it a virtual dtor
527 // anyhow to suppress g++ complaints about it
528 virtual ~wxGridOperations() { }
531 class wxGridRowOperations
: public wxGridOperations
534 virtual wxGridOperations
& Dual() const;
536 virtual int GetNumberOfLines(const wxGrid
*grid
) const
537 { return grid
->GetNumberRows(); }
539 virtual wxGrid::wxGridSelectionModes
GetSelectionMode() const
540 { return wxGrid::wxGridSelectRows
; }
542 virtual wxGridCellCoords
MakeCoords(int thisDir
, int otherDir
) const
543 { return wxGridCellCoords(thisDir
, otherDir
); }
545 virtual int CalcScrolledPosition(wxGrid
*grid
, int pos
) const
546 { return grid
->CalcScrolledPosition(wxPoint(pos
, 0)).x
; }
548 virtual int Select(const wxGridCellCoords
& c
) const { return c
.GetRow(); }
549 virtual int Select(const wxPoint
& pt
) const { return pt
.x
; }
550 virtual int Select(const wxSize
& sz
) const { return sz
.x
; }
551 virtual int Select(const wxRect
& r
) const { return r
.x
; }
552 virtual int& Select(wxRect
& r
) const { return r
.x
; }
553 virtual int& SelectSize(wxRect
& r
) const { return r
.width
; }
554 virtual wxSize
MakeSize(int first
, int second
) const
555 { return wxSize(first
, second
); }
556 virtual void Set(wxGridCellCoords
& coords
, int line
) const
557 { coords
.SetRow(line
); }
559 virtual void DrawParallelLine(wxDC
& dc
, int start
, int end
, int pos
) const
560 { dc
.DrawLine(start
, pos
, end
, pos
); }
562 virtual int PosToLine(const wxGrid
*grid
, int pos
, bool clip
= false) const
563 { return grid
->YToRow(pos
, clip
); }
564 virtual int GetLineStartPos(const wxGrid
*grid
, int line
) const
565 { return grid
->GetRowTop(line
); }
566 virtual int GetLineEndPos(const wxGrid
*grid
, int line
) const
567 { return grid
->GetRowBottom(line
); }
568 virtual int GetLineSize(const wxGrid
*grid
, int line
) const
569 { return grid
->GetRowHeight(line
); }
570 virtual const wxArrayInt
& GetLineEnds(const wxGrid
*grid
) const
571 { return grid
->m_rowBottoms
; }
572 virtual int GetDefaultLineSize(const wxGrid
*grid
) const
573 { return grid
->GetDefaultRowSize(); }
574 virtual int GetMinimalAcceptableLineSize(const wxGrid
*grid
) const
575 { return grid
->GetRowMinimalAcceptableHeight(); }
576 virtual int GetMinimalLineSize(const wxGrid
*grid
, int line
) const
577 { return grid
->GetRowMinimalHeight(line
); }
578 virtual void SetLineSize(wxGrid
*grid
, int line
, int size
) const
579 { grid
->SetRowSize(line
, size
); }
580 virtual bool CanResizeLines(const wxGrid
*grid
) const
581 { return grid
->CanDragRowSize(); }
583 virtual int GetLineAt(const wxGrid
* WXUNUSED(grid
), int line
) const
584 { return line
; } // TODO: implement row reordering
586 virtual wxWindow
*GetHeaderWindow(wxGrid
*grid
) const
587 { return grid
->GetGridRowLabelWindow(); }
588 virtual int GetHeaderWindowSize(wxGrid
*grid
) const
589 { return grid
->GetRowLabelSize(); }
592 class wxGridColumnOperations
: public wxGridOperations
595 virtual wxGridOperations
& Dual() const;
597 virtual int GetNumberOfLines(const wxGrid
*grid
) const
598 { return grid
->GetNumberCols(); }
600 virtual wxGrid::wxGridSelectionModes
GetSelectionMode() const
601 { return wxGrid::wxGridSelectColumns
; }
603 virtual wxGridCellCoords
MakeCoords(int thisDir
, int otherDir
) const
604 { return wxGridCellCoords(otherDir
, thisDir
); }
606 virtual int CalcScrolledPosition(wxGrid
*grid
, int pos
) const
607 { return grid
->CalcScrolledPosition(wxPoint(0, pos
)).y
; }
609 virtual int Select(const wxGridCellCoords
& c
) const { return c
.GetCol(); }
610 virtual int Select(const wxPoint
& pt
) const { return pt
.y
; }
611 virtual int Select(const wxSize
& sz
) const { return sz
.y
; }
612 virtual int Select(const wxRect
& r
) const { return r
.y
; }
613 virtual int& Select(wxRect
& r
) const { return r
.y
; }
614 virtual int& SelectSize(wxRect
& r
) const { return r
.height
; }
615 virtual wxSize
MakeSize(int first
, int second
) const
616 { return wxSize(second
, first
); }
617 virtual void Set(wxGridCellCoords
& coords
, int line
) const
618 { coords
.SetCol(line
); }
620 virtual void DrawParallelLine(wxDC
& dc
, int start
, int end
, int pos
) const
621 { dc
.DrawLine(pos
, start
, pos
, end
); }
623 virtual int PosToLine(const wxGrid
*grid
, int pos
, bool clip
= false) const
624 { return grid
->XToCol(pos
, clip
); }
625 virtual int GetLineStartPos(const wxGrid
*grid
, int line
) const
626 { return grid
->GetColLeft(line
); }
627 virtual int GetLineEndPos(const wxGrid
*grid
, int line
) const
628 { return grid
->GetColRight(line
); }
629 virtual int GetLineSize(const wxGrid
*grid
, int line
) const
630 { return grid
->GetColWidth(line
); }
631 virtual const wxArrayInt
& GetLineEnds(const wxGrid
*grid
) const
632 { return grid
->m_colRights
; }
633 virtual int GetDefaultLineSize(const wxGrid
*grid
) const
634 { return grid
->GetDefaultColSize(); }
635 virtual int GetMinimalAcceptableLineSize(const wxGrid
*grid
) const
636 { return grid
->GetColMinimalAcceptableWidth(); }
637 virtual int GetMinimalLineSize(const wxGrid
*grid
, int line
) const
638 { return grid
->GetColMinimalWidth(line
); }
639 virtual void SetLineSize(wxGrid
*grid
, int line
, int size
) const
640 { grid
->SetColSize(line
, size
); }
641 virtual bool CanResizeLines(const wxGrid
*grid
) const
642 { return grid
->CanDragColSize(); }
644 virtual int GetLineAt(const wxGrid
*grid
, int line
) const
645 { return grid
->GetColAt(line
); }
647 virtual wxWindow
*GetHeaderWindow(wxGrid
*grid
) const
648 { return grid
->GetGridColLabelWindow(); }
649 virtual int GetHeaderWindowSize(wxGrid
*grid
) const
650 { return grid
->GetColLabelSize(); }
653 wxGridOperations
& wxGridRowOperations::Dual() const
655 static wxGridColumnOperations s_colOper
;
660 wxGridOperations
& wxGridColumnOperations::Dual() const
662 static wxGridRowOperations s_rowOper
;
667 // This class abstracts the difference between operations going forward
668 // (down/right) and backward (up/left) and allows to use the same code for
669 // functions which differ only in the direction of grid traversal
671 // Like wxGridOperations it's an ABC with two concrete subclasses below. Unlike
672 // it, this is a normal object and not just a function dispatch table and has a
675 // Note: the explanation of this discrepancy is the existence of (very useful)
676 // Dual() method in wxGridOperations which forces us to make wxGridOperations a
677 // function dispatcher only.
678 class wxGridDirectionOperations
681 // The oper parameter to ctor selects whether we work with rows or columns
682 wxGridDirectionOperations(wxGrid
*grid
, const wxGridOperations
& oper
)
688 // Check if the component of this point in our direction is at the
689 // boundary, i.e. is the first/last row/column
690 virtual bool IsAtBoundary(const wxGridCellCoords
& coords
) const = 0;
692 // Increment the component of this point in our direction
693 virtual void Advance(wxGridCellCoords
& coords
) const = 0;
695 // Find the line at the given distance, in pixels, away from this one
696 // (this uses clipping, i.e. anything after the last line is counted as the
697 // last one and anything before the first one as 0)
698 virtual int MoveByPixelDistance(int line
, int distance
) const = 0;
700 // This class is never used polymorphically but give it a virtual dtor
701 // anyhow to suppress g++ complaints about it
702 virtual ~wxGridDirectionOperations() { }
705 wxGrid
* const m_grid
;
706 const wxGridOperations
& m_oper
;
709 class wxGridBackwardOperations
: public wxGridDirectionOperations
712 wxGridBackwardOperations(wxGrid
*grid
, const wxGridOperations
& oper
)
713 : wxGridDirectionOperations(grid
, oper
)
717 virtual bool IsAtBoundary(const wxGridCellCoords
& coords
) const
719 wxASSERT_MSG( m_oper
.Select(coords
) >= 0, "invalid row/column" );
721 return m_oper
.Select(coords
) == 0;
724 virtual void Advance(wxGridCellCoords
& coords
) const
726 wxASSERT( !IsAtBoundary(coords
) );
728 m_oper
.Set(coords
, m_oper
.Select(coords
) - 1);
731 virtual int MoveByPixelDistance(int line
, int distance
) const
733 int pos
= m_oper
.GetLineStartPos(m_grid
, line
);
734 return m_oper
.PosToLine(m_grid
, pos
- distance
+ 1, true);
738 class wxGridForwardOperations
: public wxGridDirectionOperations
741 wxGridForwardOperations(wxGrid
*grid
, const wxGridOperations
& oper
)
742 : wxGridDirectionOperations(grid
, oper
),
743 m_numLines(oper
.GetNumberOfLines(grid
))
747 virtual bool IsAtBoundary(const wxGridCellCoords
& coords
) const
749 wxASSERT_MSG( m_oper
.Select(coords
) < m_numLines
, "invalid row/column" );
751 return m_oper
.Select(coords
) == m_numLines
- 1;
754 virtual void Advance(wxGridCellCoords
& coords
) const
756 wxASSERT( !IsAtBoundary(coords
) );
758 m_oper
.Set(coords
, m_oper
.Select(coords
) + 1);
761 virtual int MoveByPixelDistance(int line
, int distance
) const
763 int pos
= m_oper
.GetLineStartPos(m_grid
, line
);
764 return m_oper
.PosToLine(m_grid
, pos
+ distance
, true);
768 const int m_numLines
;
771 // ----------------------------------------------------------------------------
773 // ----------------------------------------------------------------------------
775 //#define DEBUG_ATTR_CACHE
776 #ifdef DEBUG_ATTR_CACHE
777 static size_t gs_nAttrCacheHits
= 0;
778 static size_t gs_nAttrCacheMisses
= 0;
781 // ----------------------------------------------------------------------------
783 // ----------------------------------------------------------------------------
785 wxGridCellCoords
wxGridNoCellCoords( -1, -1 );
786 wxRect
wxGridNoCellRect( -1, -1, -1, -1 );
789 static const size_t GRID_SCROLL_LINE_X
= 15;
790 static const size_t GRID_SCROLL_LINE_Y
= GRID_SCROLL_LINE_X
;
792 // the size of hash tables used a bit everywhere (the max number of elements
793 // in these hash tables is the number of rows/columns)
794 static const int GRID_HASH_SIZE
= 100;
796 // ----------------------------------------------------------------------------
798 // ----------------------------------------------------------------------------
803 // ensure that first is less or equal to second, swapping the values if
805 void EnsureFirstLessThanSecond(int& first
, int& second
)
807 if ( first
> second
)
808 wxSwap(first
, second
);
811 } // anonymous namespace
813 // ============================================================================
815 // ============================================================================
817 // ----------------------------------------------------------------------------
819 // ----------------------------------------------------------------------------
821 wxGridCellEditor::wxGridCellEditor()
827 wxGridCellEditor::~wxGridCellEditor()
832 void wxGridCellEditor::Create(wxWindow
* WXUNUSED(parent
),
833 wxWindowID
WXUNUSED(id
),
834 wxEvtHandler
* evtHandler
)
837 m_control
->PushEventHandler(evtHandler
);
840 void wxGridCellEditor::PaintBackground(const wxRect
& rectCell
,
841 wxGridCellAttr
*attr
)
843 // erase the background because we might not fill the cell
844 wxClientDC
dc(m_control
->GetParent());
845 wxGridWindow
* gridWindow
= wxDynamicCast(m_control
->GetParent(), wxGridWindow
);
847 gridWindow
->GetOwner()->PrepareDC(dc
);
849 dc
.SetPen(*wxTRANSPARENT_PEN
);
850 dc
.SetBrush(wxBrush(attr
->GetBackgroundColour()));
851 dc
.DrawRectangle(rectCell
);
853 // redraw the control we just painted over
854 m_control
->Refresh();
857 void wxGridCellEditor::Destroy()
861 m_control
->PopEventHandler( true /* delete it*/ );
863 m_control
->Destroy();
868 void wxGridCellEditor::Show(bool show
, wxGridCellAttr
*attr
)
870 wxASSERT_MSG(m_control
, wxT("The wxGridCellEditor must be created first!"));
872 m_control
->Show(show
);
876 // set the colours/fonts if we have any
879 m_colFgOld
= m_control
->GetForegroundColour();
880 m_control
->SetForegroundColour(attr
->GetTextColour());
882 m_colBgOld
= m_control
->GetBackgroundColour();
883 m_control
->SetBackgroundColour(attr
->GetBackgroundColour());
885 // Workaround for GTK+1 font setting problem on some platforms
886 #if !defined(__WXGTK__) || defined(__WXGTK20__)
887 m_fontOld
= m_control
->GetFont();
888 m_control
->SetFont(attr
->GetFont());
891 // can't do anything more in the base class version, the other
892 // attributes may only be used by the derived classes
897 // restore the standard colours fonts
898 if ( m_colFgOld
.Ok() )
900 m_control
->SetForegroundColour(m_colFgOld
);
901 m_colFgOld
= wxNullColour
;
904 if ( m_colBgOld
.Ok() )
906 m_control
->SetBackgroundColour(m_colBgOld
);
907 m_colBgOld
= wxNullColour
;
910 // Workaround for GTK+1 font setting problem on some platforms
911 #if !defined(__WXGTK__) || defined(__WXGTK20__)
912 if ( m_fontOld
.Ok() )
914 m_control
->SetFont(m_fontOld
);
915 m_fontOld
= wxNullFont
;
921 void wxGridCellEditor::SetSize(const wxRect
& rect
)
923 wxASSERT_MSG(m_control
, wxT("The wxGridCellEditor must be created first!"));
925 m_control
->SetSize(rect
, wxSIZE_ALLOW_MINUS_ONE
);
928 void wxGridCellEditor::HandleReturn(wxKeyEvent
& event
)
933 bool wxGridCellEditor::IsAcceptedKey(wxKeyEvent
& event
)
935 bool ctrl
= event
.ControlDown();
936 bool alt
= event
.AltDown();
939 // On the Mac the Alt key is more like shift and is used for entry of
940 // valid characters, so check for Ctrl and Meta instead.
941 alt
= event
.MetaDown();
944 // Assume it's not a valid char if ctrl or alt is down, but if both are
945 // down then it may be because of an AltGr key combination, so let them
946 // through in that case.
947 if ((ctrl
|| alt
) && !(ctrl
&& alt
))
954 // If it's a F-Key or other special key then it shouldn't start the
956 if (event
.GetKeyCode() >= WXK_START
)
960 // if the unicode key code is not really a unicode character (it may
961 // be a function key or etc., the platforms appear to always give us a
962 // small value in this case) then fallback to the ASCII key code but
963 // don't do anything for function keys or etc.
964 key
= event
.GetUnicodeKey();
967 key
= event
.GetKeyCode();
968 keyOk
= (key
<= 127);
971 key
= event
.GetKeyCode();
972 keyOk
= (key
<= 255);
978 void wxGridCellEditor::StartingKey(wxKeyEvent
& event
)
983 void wxGridCellEditor::StartingClick()
989 // ----------------------------------------------------------------------------
990 // wxGridCellTextEditor
991 // ----------------------------------------------------------------------------
993 wxGridCellTextEditor::wxGridCellTextEditor()
998 void wxGridCellTextEditor::Create(wxWindow
* parent
,
1000 wxEvtHandler
* evtHandler
)
1002 DoCreate(parent
, id
, evtHandler
);
1005 void wxGridCellTextEditor::DoCreate(wxWindow
* parent
,
1007 wxEvtHandler
* evtHandler
,
1010 style
|= wxTE_PROCESS_ENTER
| wxTE_PROCESS_TAB
| wxNO_BORDER
;
1012 m_control
= new wxTextCtrl(parent
, id
, wxEmptyString
,
1013 wxDefaultPosition
, wxDefaultSize
,
1016 // set max length allowed in the textctrl, if the parameter was set
1017 if ( m_maxChars
!= 0 )
1019 Text()->SetMaxLength(m_maxChars
);
1022 wxGridCellEditor::Create(parent
, id
, evtHandler
);
1025 void wxGridCellTextEditor::PaintBackground(const wxRect
& WXUNUSED(rectCell
),
1026 wxGridCellAttr
* WXUNUSED(attr
))
1028 // as we fill the entire client area,
1029 // don't do anything here to minimize flicker
1032 void wxGridCellTextEditor::SetSize(const wxRect
& rectOrig
)
1034 wxRect
rect(rectOrig
);
1036 // Make the edit control large enough to allow for internal margins
1038 // TODO: remove this if the text ctrl sizing is improved esp. for unix
1040 #if defined(__WXGTK__)
1048 #elif defined(__WXMSW__)
1062 int extra_x
= ( rect
.x
> 2 ) ? 2 : 1;
1063 int extra_y
= ( rect
.y
> 2 ) ? 2 : 1;
1065 #if defined(__WXMOTIF__)
1070 rect
.SetLeft( wxMax(0, rect
.x
- extra_x
) );
1071 rect
.SetTop( wxMax(0, rect
.y
- extra_y
) );
1072 rect
.SetRight( rect
.GetRight() + 2 * extra_x
);
1073 rect
.SetBottom( rect
.GetBottom() + 2 * extra_y
);
1076 wxGridCellEditor::SetSize(rect
);
1079 void wxGridCellTextEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1081 wxASSERT_MSG(m_control
, wxT("The wxGridCellEditor must be created first!"));
1083 m_startValue
= grid
->GetTable()->GetValue(row
, col
);
1085 DoBeginEdit(m_startValue
);
1088 void wxGridCellTextEditor::DoBeginEdit(const wxString
& startValue
)
1090 Text()->SetValue(startValue
);
1091 Text()->SetInsertionPointEnd();
1092 Text()->SetSelection(-1, -1);
1096 bool wxGridCellTextEditor::EndEdit(int row
, int col
, wxGrid
* grid
)
1098 wxASSERT_MSG(m_control
, wxT("The wxGridCellEditor must be created first!"));
1100 bool changed
= false;
1101 wxString value
= Text()->GetValue();
1102 if (value
!= m_startValue
)
1106 grid
->GetTable()->SetValue(row
, col
, value
);
1108 m_startValue
= wxEmptyString
;
1110 // No point in setting the text of the hidden control
1111 //Text()->SetValue(m_startValue);
1116 void wxGridCellTextEditor::Reset()
1118 wxASSERT_MSG(m_control
, wxT("The wxGridCellEditor must be created first!"));
1120 DoReset(m_startValue
);
1123 void wxGridCellTextEditor::DoReset(const wxString
& startValue
)
1125 Text()->SetValue(startValue
);
1126 Text()->SetInsertionPointEnd();
1129 bool wxGridCellTextEditor::IsAcceptedKey(wxKeyEvent
& event
)
1131 return wxGridCellEditor::IsAcceptedKey(event
);
1134 void wxGridCellTextEditor::StartingKey(wxKeyEvent
& event
)
1136 // Since this is now happening in the EVT_CHAR event EmulateKeyPress is no
1137 // longer an appropriate way to get the character into the text control.
1138 // Do it ourselves instead. We know that if we get this far that we have
1139 // a valid character, so not a whole lot of testing needs to be done.
1141 wxTextCtrl
* tc
= Text();
1146 ch
= event
.GetUnicodeKey();
1148 ch
= (wxChar
)event
.GetKeyCode();
1150 ch
= (wxChar
)event
.GetKeyCode();
1156 // delete the character at the cursor
1157 pos
= tc
->GetInsertionPoint();
1158 if (pos
< tc
->GetLastPosition())
1159 tc
->Remove(pos
, pos
+ 1);
1163 // delete the character before the cursor
1164 pos
= tc
->GetInsertionPoint();
1166 tc
->Remove(pos
- 1, pos
);
1175 void wxGridCellTextEditor::HandleReturn( wxKeyEvent
&
1176 WXUNUSED_GTK(WXUNUSED_MOTIF(event
)) )
1178 #if defined(__WXMOTIF__) || defined(__WXGTK__)
1179 // wxMotif needs a little extra help...
1180 size_t pos
= (size_t)( Text()->GetInsertionPoint() );
1181 wxString
s( Text()->GetValue() );
1182 s
= s
.Left(pos
) + wxT("\n") + s
.Mid(pos
);
1183 Text()->SetValue(s
);
1184 Text()->SetInsertionPoint( pos
);
1186 // the other ports can handle a Return key press
1192 void wxGridCellTextEditor::SetParameters(const wxString
& params
)
1202 if ( params
.ToLong(&tmp
) )
1204 m_maxChars
= (size_t)tmp
;
1208 wxLogDebug( _T("Invalid wxGridCellTextEditor parameter string '%s' ignored"), params
.c_str() );
1213 // return the value in the text control
1214 wxString
wxGridCellTextEditor::GetValue() const
1216 return Text()->GetValue();
1219 // ----------------------------------------------------------------------------
1220 // wxGridCellNumberEditor
1221 // ----------------------------------------------------------------------------
1223 wxGridCellNumberEditor::wxGridCellNumberEditor(int min
, int max
)
1229 void wxGridCellNumberEditor::Create(wxWindow
* parent
,
1231 wxEvtHandler
* evtHandler
)
1236 // create a spin ctrl
1237 m_control
= new wxSpinCtrl(parent
, wxID_ANY
, wxEmptyString
,
1238 wxDefaultPosition
, wxDefaultSize
,
1242 wxGridCellEditor::Create(parent
, id
, evtHandler
);
1247 // just a text control
1248 wxGridCellTextEditor::Create(parent
, id
, evtHandler
);
1250 #if wxUSE_VALIDATORS
1251 Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC
));
1256 void wxGridCellNumberEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1258 // first get the value
1259 wxGridTableBase
*table
= grid
->GetTable();
1260 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
1262 m_valueOld
= table
->GetValueAsLong(row
, col
);
1267 wxString sValue
= table
->GetValue(row
, col
);
1268 if (! sValue
.ToLong(&m_valueOld
) && ! sValue
.empty())
1270 wxFAIL_MSG( _T("this cell doesn't have numeric value") );
1278 Spin()->SetValue((int)m_valueOld
);
1284 DoBeginEdit(GetString());
1288 bool wxGridCellNumberEditor::EndEdit(int row
, int col
,
1297 value
= Spin()->GetValue();
1298 if ( value
== m_valueOld
)
1301 text
.Printf(wxT("%ld"), value
);
1303 else // using unconstrained input
1304 #endif // wxUSE_SPINCTRL
1306 const wxString
textOld(grid
->GetCellValue(row
, col
));
1307 text
= Text()->GetValue();
1310 if ( textOld
.empty() )
1313 else // non-empty text now (maybe 0)
1315 if ( !text
.ToLong(&value
) )
1318 // if value == m_valueOld == 0 but old text was "" and new one is
1319 // "0" something still did change
1320 if ( value
== m_valueOld
&& (value
|| !textOld
.empty()) )
1325 wxGridTableBase
* const table
= grid
->GetTable();
1326 if ( table
->CanSetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
1327 table
->SetValueAsLong(row
, col
, value
);
1329 table
->SetValue(row
, col
, text
);
1334 void wxGridCellNumberEditor::Reset()
1339 Spin()->SetValue((int)m_valueOld
);
1344 DoReset(GetString());
1348 bool wxGridCellNumberEditor::IsAcceptedKey(wxKeyEvent
& event
)
1350 if ( wxGridCellEditor::IsAcceptedKey(event
) )
1352 int keycode
= event
.GetKeyCode();
1353 if ( (keycode
< 128) &&
1354 (wxIsdigit(keycode
) || keycode
== '+' || keycode
== '-'))
1363 void wxGridCellNumberEditor::StartingKey(wxKeyEvent
& event
)
1365 int keycode
= event
.GetKeyCode();
1368 if ( wxIsdigit(keycode
) || keycode
== '+' || keycode
== '-')
1370 wxGridCellTextEditor::StartingKey(event
);
1372 // skip Skip() below
1379 if ( wxIsdigit(keycode
) )
1381 wxSpinCtrl
* spin
= (wxSpinCtrl
*)m_control
;
1382 spin
->SetValue(keycode
- '0');
1383 spin
->SetSelection(1,1);
1392 void wxGridCellNumberEditor::SetParameters(const wxString
& params
)
1403 if ( params
.BeforeFirst(_T(',')).ToLong(&tmp
) )
1407 if ( params
.AfterFirst(_T(',')).ToLong(&tmp
) )
1411 // skip the error message below
1416 wxLogDebug(_T("Invalid wxGridCellNumberEditor parameter string '%s' ignored"), params
.c_str());
1420 // return the value in the spin control if it is there (the text control otherwise)
1421 wxString
wxGridCellNumberEditor::GetValue() const
1428 long value
= Spin()->GetValue();
1429 s
.Printf(wxT("%ld"), value
);
1434 s
= Text()->GetValue();
1440 // ----------------------------------------------------------------------------
1441 // wxGridCellFloatEditor
1442 // ----------------------------------------------------------------------------
1444 wxGridCellFloatEditor::wxGridCellFloatEditor(int width
, int precision
)
1447 m_precision
= precision
;
1450 void wxGridCellFloatEditor::Create(wxWindow
* parent
,
1452 wxEvtHandler
* evtHandler
)
1454 wxGridCellTextEditor::Create(parent
, id
, evtHandler
);
1456 #if wxUSE_VALIDATORS
1457 Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC
));
1461 void wxGridCellFloatEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1463 // first get the value
1464 wxGridTableBase
* const table
= grid
->GetTable();
1465 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
1467 m_valueOld
= table
->GetValueAsDouble(row
, col
);
1473 const wxString value
= table
->GetValue(row
, col
);
1474 if ( !value
.empty() )
1476 if ( !value
.ToDouble(&m_valueOld
) )
1478 wxFAIL_MSG( _T("this cell doesn't have float value") );
1484 DoBeginEdit(GetString());
1487 bool wxGridCellFloatEditor::EndEdit(int row
, int col
, wxGrid
* grid
)
1489 const wxString
text(Text()->GetValue()),
1490 textOld(grid
->GetCellValue(row
, col
));
1493 if ( !text
.empty() )
1495 if ( !text
.ToDouble(&value
) )
1498 else // new value is empty string
1500 if ( textOld
.empty() )
1501 return false; // nothing changed
1506 // the test for empty strings ensures that we don't skip the value setting
1507 // when "" is replaced by "0" or vice versa as "" numeric value is also 0.
1508 if ( wxIsSameDouble(value
, m_valueOld
) && !text
.empty() && !textOld
.empty() )
1509 return false; // nothing changed
1511 wxGridTableBase
* const table
= grid
->GetTable();
1513 if ( table
->CanSetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
1514 table
->SetValueAsDouble(row
, col
, value
);
1516 table
->SetValue(row
, col
, text
);
1521 void wxGridCellFloatEditor::Reset()
1523 DoReset(GetString());
1526 void wxGridCellFloatEditor::StartingKey(wxKeyEvent
& event
)
1528 int keycode
= event
.GetKeyCode();
1530 tmpbuf
[0] = (char) keycode
;
1532 wxString
strbuf(tmpbuf
, *wxConvCurrent
);
1535 bool is_decimal_point
= ( strbuf
==
1536 wxLocale::GetInfo(wxLOCALE_DECIMAL_POINT
, wxLOCALE_CAT_NUMBER
) );
1538 bool is_decimal_point
= ( strbuf
== _T(".") );
1541 if ( wxIsdigit(keycode
) || keycode
== '+' || keycode
== '-'
1542 || is_decimal_point
)
1544 wxGridCellTextEditor::StartingKey(event
);
1546 // skip Skip() below
1553 void wxGridCellFloatEditor::SetParameters(const wxString
& params
)
1564 if ( params
.BeforeFirst(_T(',')).ToLong(&tmp
) )
1568 if ( params
.AfterFirst(_T(',')).ToLong(&tmp
) )
1570 m_precision
= (int)tmp
;
1572 // skip the error message below
1577 wxLogDebug(_T("Invalid wxGridCellFloatEditor parameter string '%s' ignored"), params
.c_str());
1581 wxString
wxGridCellFloatEditor::GetString() const
1584 if ( m_precision
== -1 && m_width
!= -1)
1586 // default precision
1587 fmt
.Printf(_T("%%%d.f"), m_width
);
1589 else if ( m_precision
!= -1 && m_width
== -1)
1592 fmt
.Printf(_T("%%.%df"), m_precision
);
1594 else if ( m_precision
!= -1 && m_width
!= -1 )
1596 fmt
.Printf(_T("%%%d.%df"), m_width
, m_precision
);
1600 // default width/precision
1604 return wxString::Format(fmt
, m_valueOld
);
1607 bool wxGridCellFloatEditor::IsAcceptedKey(wxKeyEvent
& event
)
1609 if ( wxGridCellEditor::IsAcceptedKey(event
) )
1611 const int keycode
= event
.GetKeyCode();
1612 if ( isascii(keycode
) )
1615 tmpbuf
[0] = (char) keycode
;
1617 wxString
strbuf(tmpbuf
, *wxConvCurrent
);
1620 const wxString decimalPoint
=
1621 wxLocale::GetInfo(wxLOCALE_DECIMAL_POINT
, wxLOCALE_CAT_NUMBER
);
1623 const wxString
decimalPoint(_T('.'));
1626 // accept digits, 'e' as in '1e+6', also '-', '+', and '.'
1627 if ( wxIsdigit(keycode
) ||
1628 tolower(keycode
) == 'e' ||
1629 keycode
== decimalPoint
||
1641 #endif // wxUSE_TEXTCTRL
1645 // ----------------------------------------------------------------------------
1646 // wxGridCellBoolEditor
1647 // ----------------------------------------------------------------------------
1649 // the default values for GetValue()
1650 wxString
wxGridCellBoolEditor::ms_stringValues
[2] = { _T(""), _T("1") };
1652 void wxGridCellBoolEditor::Create(wxWindow
* parent
,
1654 wxEvtHandler
* evtHandler
)
1656 m_control
= new wxCheckBox(parent
, id
, wxEmptyString
,
1657 wxDefaultPosition
, wxDefaultSize
,
1660 wxGridCellEditor::Create(parent
, id
, evtHandler
);
1663 void wxGridCellBoolEditor::SetSize(const wxRect
& r
)
1665 bool resize
= false;
1666 wxSize size
= m_control
->GetSize();
1667 wxCoord minSize
= wxMin(r
.width
, r
.height
);
1669 // check if the checkbox is not too big/small for this cell
1670 wxSize sizeBest
= m_control
->GetBestSize();
1671 if ( !(size
== sizeBest
) )
1673 // reset to default size if it had been made smaller
1679 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
1681 // leave 1 pixel margin
1682 size
.x
= size
.y
= minSize
- 2;
1689 m_control
->SetSize(size
);
1692 // position it in the centre of the rectangle (TODO: support alignment?)
1694 #if defined(__WXGTK__) || defined (__WXMOTIF__)
1695 // the checkbox without label still has some space to the right in wxGTK,
1696 // so shift it to the right
1698 #elif defined(__WXMSW__)
1699 // here too, but in other way
1704 int hAlign
= wxALIGN_CENTRE
;
1705 int vAlign
= wxALIGN_CENTRE
;
1707 GetCellAttr()->GetAlignment(& hAlign
, & vAlign
);
1710 if (hAlign
== wxALIGN_LEFT
)
1718 y
= r
.y
+ r
.height
/ 2 - size
.y
/ 2;
1720 else if (hAlign
== wxALIGN_RIGHT
)
1722 x
= r
.x
+ r
.width
- size
.x
- 2;
1723 y
= r
.y
+ r
.height
/ 2 - size
.y
/ 2;
1725 else if (hAlign
== wxALIGN_CENTRE
)
1727 x
= r
.x
+ r
.width
/ 2 - size
.x
/ 2;
1728 y
= r
.y
+ r
.height
/ 2 - size
.y
/ 2;
1731 m_control
->Move(x
, y
);
1734 void wxGridCellBoolEditor::Show(bool show
, wxGridCellAttr
*attr
)
1736 m_control
->Show(show
);
1740 wxColour colBg
= attr
? attr
->GetBackgroundColour() : *wxLIGHT_GREY
;
1741 CBox()->SetBackgroundColour(colBg
);
1745 void wxGridCellBoolEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1747 wxASSERT_MSG(m_control
,
1748 wxT("The wxGridCellEditor must be created first!"));
1750 if (grid
->GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
))
1752 m_startValue
= grid
->GetTable()->GetValueAsBool(row
, col
);
1756 wxString
cellval( grid
->GetTable()->GetValue(row
, col
) );
1758 if ( cellval
== ms_stringValues
[false] )
1759 m_startValue
= false;
1760 else if ( cellval
== ms_stringValues
[true] )
1761 m_startValue
= true;
1764 // do not try to be smart here and convert it to true or false
1765 // because we'll still overwrite it with something different and
1766 // this risks to be very surprising for the user code, let them
1768 wxFAIL_MSG( _T("invalid value for a cell with bool editor!") );
1772 CBox()->SetValue(m_startValue
);
1776 bool wxGridCellBoolEditor::EndEdit(int row
, int col
,
1779 wxASSERT_MSG(m_control
,
1780 wxT("The wxGridCellEditor must be created first!"));
1782 bool changed
= false;
1783 bool value
= CBox()->GetValue();
1784 if ( value
!= m_startValue
)
1789 wxGridTableBase
* const table
= grid
->GetTable();
1790 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
) )
1791 table
->SetValueAsBool(row
, col
, value
);
1793 table
->SetValue(row
, col
, GetValue());
1799 void wxGridCellBoolEditor::Reset()
1801 wxASSERT_MSG(m_control
,
1802 wxT("The wxGridCellEditor must be created first!"));
1804 CBox()->SetValue(m_startValue
);
1807 void wxGridCellBoolEditor::StartingClick()
1809 CBox()->SetValue(!CBox()->GetValue());
1812 bool wxGridCellBoolEditor::IsAcceptedKey(wxKeyEvent
& event
)
1814 if ( wxGridCellEditor::IsAcceptedKey(event
) )
1816 int keycode
= event
.GetKeyCode();
1829 void wxGridCellBoolEditor::StartingKey(wxKeyEvent
& event
)
1831 int keycode
= event
.GetKeyCode();
1835 CBox()->SetValue(!CBox()->GetValue());
1839 CBox()->SetValue(true);
1843 CBox()->SetValue(false);
1848 wxString
wxGridCellBoolEditor::GetValue() const
1850 return ms_stringValues
[CBox()->GetValue()];
1854 wxGridCellBoolEditor::UseStringValues(const wxString
& valueTrue
,
1855 const wxString
& valueFalse
)
1857 ms_stringValues
[false] = valueFalse
;
1858 ms_stringValues
[true] = valueTrue
;
1862 wxGridCellBoolEditor::IsTrueValue(const wxString
& value
)
1864 return value
== ms_stringValues
[true];
1867 #endif // wxUSE_CHECKBOX
1871 // ----------------------------------------------------------------------------
1872 // wxGridCellChoiceEditor
1873 // ----------------------------------------------------------------------------
1875 wxGridCellChoiceEditor::wxGridCellChoiceEditor(const wxArrayString
& choices
,
1877 : m_choices(choices
),
1878 m_allowOthers(allowOthers
) { }
1880 wxGridCellChoiceEditor::wxGridCellChoiceEditor(size_t count
,
1881 const wxString choices
[],
1883 : m_allowOthers(allowOthers
)
1887 m_choices
.Alloc(count
);
1888 for ( size_t n
= 0; n
< count
; n
++ )
1890 m_choices
.Add(choices
[n
]);
1895 wxGridCellEditor
*wxGridCellChoiceEditor::Clone() const
1897 wxGridCellChoiceEditor
*editor
= new wxGridCellChoiceEditor
;
1898 editor
->m_allowOthers
= m_allowOthers
;
1899 editor
->m_choices
= m_choices
;
1904 void wxGridCellChoiceEditor::Create(wxWindow
* parent
,
1906 wxEvtHandler
* evtHandler
)
1908 int style
= wxTE_PROCESS_ENTER
|
1912 if ( !m_allowOthers
)
1913 style
|= wxCB_READONLY
;
1914 m_control
= new wxComboBox(parent
, id
, wxEmptyString
,
1915 wxDefaultPosition
, wxDefaultSize
,
1919 wxGridCellEditor::Create(parent
, id
, evtHandler
);
1922 void wxGridCellChoiceEditor::PaintBackground(const wxRect
& rectCell
,
1923 wxGridCellAttr
* attr
)
1925 // as we fill the entire client area, don't do anything here to minimize
1928 // TODO: It doesn't actually fill the client area since the height of a
1929 // combo always defaults to the standard. Until someone has time to
1930 // figure out the right rectangle to paint, just do it the normal way.
1931 wxGridCellEditor::PaintBackground(rectCell
, attr
);
1934 void wxGridCellChoiceEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1936 wxASSERT_MSG(m_control
,
1937 wxT("The wxGridCellEditor must be created first!"));
1939 wxGridCellEditorEvtHandler
* evtHandler
= NULL
;
1941 evtHandler
= wxDynamicCast(m_control
->GetEventHandler(), wxGridCellEditorEvtHandler
);
1943 // Don't immediately end if we get a kill focus event within BeginEdit
1945 evtHandler
->SetInSetFocus(true);
1947 m_startValue
= grid
->GetTable()->GetValue(row
, col
);
1949 Reset(); // this updates combo box to correspond to m_startValue
1951 Combo()->SetFocus();
1955 // When dropping down the menu, a kill focus event
1956 // happens after this point, so we can't reset the flag yet.
1957 #if !defined(__WXGTK20__)
1958 evtHandler
->SetInSetFocus(false);
1963 bool wxGridCellChoiceEditor::EndEdit(int row
, int col
,
1966 wxString value
= Combo()->GetValue();
1967 if ( value
== m_startValue
)
1970 grid
->GetTable()->SetValue(row
, col
, value
);
1975 void wxGridCellChoiceEditor::Reset()
1979 Combo()->SetValue(m_startValue
);
1980 Combo()->SetInsertionPointEnd();
1982 else // the combobox is read-only
1984 // find the right position, or default to the first if not found
1985 int pos
= Combo()->FindString(m_startValue
);
1986 if (pos
== wxNOT_FOUND
)
1988 Combo()->SetSelection(pos
);
1992 void wxGridCellChoiceEditor::SetParameters(const wxString
& params
)
2002 wxStringTokenizer
tk(params
, _T(','));
2003 while ( tk
.HasMoreTokens() )
2005 m_choices
.Add(tk
.GetNextToken());
2009 // return the value in the text control
2010 wxString
wxGridCellChoiceEditor::GetValue() const
2012 return Combo()->GetValue();
2015 #endif // wxUSE_COMBOBOX
2017 // ----------------------------------------------------------------------------
2018 // wxGridCellEditorEvtHandler
2019 // ----------------------------------------------------------------------------
2021 void wxGridCellEditorEvtHandler::OnKillFocus(wxFocusEvent
& event
)
2023 // Don't disable the cell if we're just starting to edit it
2028 m_grid
->DisableCellEditControl();
2033 void wxGridCellEditorEvtHandler::OnKeyDown(wxKeyEvent
& event
)
2035 switch ( event
.GetKeyCode() )
2039 m_grid
->DisableCellEditControl();
2043 m_grid
->GetEventHandler()->ProcessEvent( event
);
2047 case WXK_NUMPAD_ENTER
:
2048 if (!m_grid
->GetEventHandler()->ProcessEvent(event
))
2049 m_editor
->HandleReturn(event
);
2058 void wxGridCellEditorEvtHandler::OnChar(wxKeyEvent
& event
)
2060 int row
= m_grid
->GetGridCursorRow();
2061 int col
= m_grid
->GetGridCursorCol();
2062 wxRect rect
= m_grid
->CellToRect( row
, col
);
2064 m_grid
->GetGridWindow()->GetClientSize( &cw
, &ch
);
2066 // if cell width is smaller than grid client area, cell is wholly visible
2067 bool wholeCellVisible
= (rect
.GetWidth() < cw
);
2069 switch ( event
.GetKeyCode() )
2074 case WXK_NUMPAD_ENTER
:
2079 if ( wholeCellVisible
)
2081 // no special processing needed...
2086 // do special processing for partly visible cell...
2088 // get the widths of all cells previous to this one
2090 for ( int i
= 0; i
< col
; i
++ )
2092 colXPos
+= m_grid
->GetColSize(i
);
2095 int xUnit
= 1, yUnit
= 1;
2096 m_grid
->GetScrollPixelsPerUnit(&xUnit
, &yUnit
);
2099 m_grid
->Scroll(colXPos
/ xUnit
- 1, m_grid
->GetScrollPos(wxVERTICAL
));
2103 m_grid
->Scroll(colXPos
/ xUnit
, m_grid
->GetScrollPos(wxVERTICAL
));
2111 if ( wholeCellVisible
)
2113 // no special processing needed...
2118 // do special processing for partly visible cell...
2121 wxString value
= m_grid
->GetCellValue(row
, col
);
2122 if ( wxEmptyString
!= value
)
2124 // get width of cell CONTENTS (text)
2126 wxFont font
= m_grid
->GetCellFont(row
, col
);
2127 m_grid
->GetTextExtent(value
, &textWidth
, &y
, NULL
, NULL
, &font
);
2129 // try to RIGHT align the text by scrolling
2130 int client_right
= m_grid
->GetGridWindow()->GetClientSize().GetWidth();
2132 // (m_grid->GetScrollLineX()*2) is a factor for not scrolling to far,
2133 // otherwise the last part of the cell content might be hidden below the scroll bar
2134 // FIXME: maybe there is a more suitable correction?
2135 textWidth
-= (client_right
- (m_grid
->GetScrollLineX() * 2));
2136 if ( textWidth
< 0 )
2142 // get the widths of all cells previous to this one
2144 for ( int i
= 0; i
< col
; i
++ )
2146 colXPos
+= m_grid
->GetColSize(i
);
2149 // and add the (modified) text width of the cell contents
2150 // as we'd like to see the last part of the cell contents
2151 colXPos
+= textWidth
;
2153 int xUnit
= 1, yUnit
= 1;
2154 m_grid
->GetScrollPixelsPerUnit(&xUnit
, &yUnit
);
2155 m_grid
->Scroll(colXPos
/ xUnit
- 1, m_grid
->GetScrollPos(wxVERTICAL
));
2166 // ----------------------------------------------------------------------------
2167 // wxGridCellWorker is an (almost) empty common base class for
2168 // wxGridCellRenderer and wxGridCellEditor managing ref counting
2169 // ----------------------------------------------------------------------------
2171 void wxGridCellWorker::SetParameters(const wxString
& WXUNUSED(params
))
2176 wxGridCellWorker::~wxGridCellWorker()
2180 // ============================================================================
2182 // ============================================================================
2184 // ----------------------------------------------------------------------------
2185 // wxGridCellRenderer
2186 // ----------------------------------------------------------------------------
2188 void wxGridCellRenderer::Draw(wxGrid
& grid
,
2189 wxGridCellAttr
& attr
,
2192 int WXUNUSED(row
), int WXUNUSED(col
),
2195 dc
.SetBackgroundMode( wxBRUSHSTYLE_SOLID
);
2198 if ( grid
.IsEnabled() )
2202 if ( grid
.HasFocus() )
2203 clr
= grid
.GetSelectionBackground();
2205 clr
= wxSystemSettings::GetColour(wxSYS_COLOUR_BTNSHADOW
);
2209 clr
= attr
.GetBackgroundColour();
2212 else // grey out fields if the grid is disabled
2214 clr
= wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
);
2218 dc
.SetPen( *wxTRANSPARENT_PEN
);
2219 dc
.DrawRectangle(rect
);
2222 // ----------------------------------------------------------------------------
2223 // wxGridCellStringRenderer
2224 // ----------------------------------------------------------------------------
2226 void wxGridCellStringRenderer::SetTextColoursAndFont(const wxGrid
& grid
,
2227 const wxGridCellAttr
& attr
,
2231 dc
.SetBackgroundMode( wxBRUSHSTYLE_TRANSPARENT
);
2233 // TODO some special colours for attr.IsReadOnly() case?
2235 // different coloured text when the grid is disabled
2236 if ( grid
.IsEnabled() )
2241 if ( grid
.HasFocus() )
2242 clr
= grid
.GetSelectionBackground();
2244 clr
= wxSystemSettings::GetColour(wxSYS_COLOUR_BTNSHADOW
);
2245 dc
.SetTextBackground( clr
);
2246 dc
.SetTextForeground( grid
.GetSelectionForeground() );
2250 dc
.SetTextBackground( attr
.GetBackgroundColour() );
2251 dc
.SetTextForeground( attr
.GetTextColour() );
2256 dc
.SetTextBackground(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
));
2257 dc
.SetTextForeground(wxSystemSettings::GetColour(wxSYS_COLOUR_GRAYTEXT
));
2260 dc
.SetFont( attr
.GetFont() );
2263 wxSize
wxGridCellStringRenderer::DoGetBestSize(const wxGridCellAttr
& attr
,
2265 const wxString
& text
)
2267 wxCoord x
= 0, y
= 0, max_x
= 0;
2268 dc
.SetFont(attr
.GetFont());
2269 wxStringTokenizer
tk(text
, _T('\n'));
2270 while ( tk
.HasMoreTokens() )
2272 dc
.GetTextExtent(tk
.GetNextToken(), &x
, &y
);
2273 max_x
= wxMax(max_x
, x
);
2276 y
*= 1 + text
.Freq(wxT('\n')); // multiply by the number of lines.
2278 return wxSize(max_x
, y
);
2281 wxSize
wxGridCellStringRenderer::GetBestSize(wxGrid
& grid
,
2282 wxGridCellAttr
& attr
,
2286 return DoGetBestSize(attr
, dc
, grid
.GetCellValue(row
, col
));
2289 void wxGridCellStringRenderer::Draw(wxGrid
& grid
,
2290 wxGridCellAttr
& attr
,
2292 const wxRect
& rectCell
,
2296 wxRect rect
= rectCell
;
2299 // erase only this cells background, overflow cells should have been erased
2300 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
2303 attr
.GetAlignment(&hAlign
, &vAlign
);
2305 int overflowCols
= 0;
2307 if (attr
.GetOverflow())
2309 int cols
= grid
.GetNumberCols();
2310 int best_width
= GetBestSize(grid
,attr
,dc
,row
,col
).GetWidth();
2311 int cell_rows
, cell_cols
;
2312 attr
.GetSize( &cell_rows
, &cell_cols
); // shouldn't get here if <= 0
2313 if ((best_width
> rectCell
.width
) && (col
< cols
) && grid
.GetTable())
2315 int i
, c_cols
, c_rows
;
2316 for (i
= col
+cell_cols
; i
< cols
; i
++)
2318 bool is_empty
= true;
2319 for (int j
=row
; j
< row
+ cell_rows
; j
++)
2321 // check w/ anchor cell for multicell block
2322 grid
.GetCellSize(j
, i
, &c_rows
, &c_cols
);
2325 if (!grid
.GetTable()->IsEmptyCell(j
+ c_rows
, i
))
2334 rect
.width
+= grid
.GetColSize(i
);
2342 if (rect
.width
>= best_width
)
2346 overflowCols
= i
- col
- cell_cols
+ 1;
2347 if (overflowCols
>= cols
)
2348 overflowCols
= cols
- 1;
2351 if (overflowCols
> 0) // redraw overflow cells w/ proper hilight
2353 hAlign
= wxALIGN_LEFT
; // if oveflowed then it's left aligned
2355 clip
.x
+= rectCell
.width
;
2356 // draw each overflow cell individually
2357 int col_end
= col
+ cell_cols
+ overflowCols
;
2358 if (col_end
>= grid
.GetNumberCols())
2359 col_end
= grid
.GetNumberCols() - 1;
2360 for (int i
= col
+ cell_cols
; i
<= col_end
; i
++)
2362 clip
.width
= grid
.GetColSize(i
) - 1;
2363 dc
.DestroyClippingRegion();
2364 dc
.SetClippingRegion(clip
);
2366 SetTextColoursAndFont(grid
, attr
, dc
,
2367 grid
.IsInSelection(row
,i
));
2369 grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
),
2370 rect
, hAlign
, vAlign
);
2371 clip
.x
+= grid
.GetColSize(i
) - 1;
2377 dc
.DestroyClippingRegion();
2381 // now we only have to draw the text
2382 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
2384 grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
),
2385 rect
, hAlign
, vAlign
);
2388 // ----------------------------------------------------------------------------
2389 // wxGridCellNumberRenderer
2390 // ----------------------------------------------------------------------------
2392 wxString
wxGridCellNumberRenderer::GetString(const wxGrid
& grid
, int row
, int col
)
2394 wxGridTableBase
*table
= grid
.GetTable();
2396 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
2398 text
.Printf(_T("%ld"), table
->GetValueAsLong(row
, col
));
2402 text
= table
->GetValue(row
, col
);
2408 void wxGridCellNumberRenderer::Draw(wxGrid
& grid
,
2409 wxGridCellAttr
& attr
,
2411 const wxRect
& rectCell
,
2415 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
2417 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
2419 // draw the text right aligned by default
2421 attr
.GetAlignment(&hAlign
, &vAlign
);
2422 hAlign
= wxALIGN_RIGHT
;
2424 wxRect rect
= rectCell
;
2427 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
2430 wxSize
wxGridCellNumberRenderer::GetBestSize(wxGrid
& grid
,
2431 wxGridCellAttr
& attr
,
2435 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
2438 // ----------------------------------------------------------------------------
2439 // wxGridCellFloatRenderer
2440 // ----------------------------------------------------------------------------
2442 wxGridCellFloatRenderer::wxGridCellFloatRenderer(int width
, int precision
)
2445 SetPrecision(precision
);
2448 wxGridCellRenderer
*wxGridCellFloatRenderer::Clone() const
2450 wxGridCellFloatRenderer
*renderer
= new wxGridCellFloatRenderer
;
2451 renderer
->m_width
= m_width
;
2452 renderer
->m_precision
= m_precision
;
2453 renderer
->m_format
= m_format
;
2458 wxString
wxGridCellFloatRenderer::GetString(const wxGrid
& grid
, int row
, int col
)
2460 wxGridTableBase
*table
= grid
.GetTable();
2465 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
2467 val
= table
->GetValueAsDouble(row
, col
);
2472 text
= table
->GetValue(row
, col
);
2473 hasDouble
= text
.ToDouble(&val
);
2480 if ( m_width
== -1 )
2482 if ( m_precision
== -1 )
2484 // default width/precision
2485 m_format
= _T("%f");
2489 m_format
.Printf(_T("%%.%df"), m_precision
);
2492 else if ( m_precision
== -1 )
2494 // default precision
2495 m_format
.Printf(_T("%%%d.f"), m_width
);
2499 m_format
.Printf(_T("%%%d.%df"), m_width
, m_precision
);
2503 text
.Printf(m_format
, val
);
2506 //else: text already contains the string
2511 void wxGridCellFloatRenderer::Draw(wxGrid
& grid
,
2512 wxGridCellAttr
& attr
,
2514 const wxRect
& rectCell
,
2518 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
2520 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
2522 // draw the text right aligned by default
2524 attr
.GetAlignment(&hAlign
, &vAlign
);
2525 hAlign
= wxALIGN_RIGHT
;
2527 wxRect rect
= rectCell
;
2530 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
2533 wxSize
wxGridCellFloatRenderer::GetBestSize(wxGrid
& grid
,
2534 wxGridCellAttr
& attr
,
2538 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
2541 void wxGridCellFloatRenderer::SetParameters(const wxString
& params
)
2545 // reset to defaults
2551 wxString tmp
= params
.BeforeFirst(_T(','));
2555 if ( tmp
.ToLong(&width
) )
2557 SetWidth((int)width
);
2561 wxLogDebug(_T("Invalid wxGridCellFloatRenderer width parameter string '%s ignored"), params
.c_str());
2565 tmp
= params
.AfterFirst(_T(','));
2569 if ( tmp
.ToLong(&precision
) )
2571 SetPrecision((int)precision
);
2575 wxLogDebug(_T("Invalid wxGridCellFloatRenderer precision parameter string '%s ignored"), params
.c_str());
2581 // ----------------------------------------------------------------------------
2582 // wxGridCellBoolRenderer
2583 // ----------------------------------------------------------------------------
2585 wxSize
wxGridCellBoolRenderer::ms_sizeCheckMark
;
2587 // FIXME these checkbox size calculations are really ugly...
2589 // between checkmark and box
2590 static const wxCoord wxGRID_CHECKMARK_MARGIN
= 2;
2592 wxSize
wxGridCellBoolRenderer::GetBestSize(wxGrid
& grid
,
2593 wxGridCellAttr
& WXUNUSED(attr
),
2598 // compute it only once (no locks for MT safeness in GUI thread...)
2599 if ( !ms_sizeCheckMark
.x
)
2601 // get checkbox size
2602 wxCheckBox
*checkbox
= new wxCheckBox(&grid
, wxID_ANY
, wxEmptyString
);
2603 wxSize size
= checkbox
->GetBestSize();
2604 wxCoord checkSize
= size
.y
+ 2 * wxGRID_CHECKMARK_MARGIN
;
2606 #if defined(__WXMOTIF__)
2607 checkSize
-= size
.y
/ 2;
2612 ms_sizeCheckMark
.x
= ms_sizeCheckMark
.y
= checkSize
;
2615 return ms_sizeCheckMark
;
2618 void wxGridCellBoolRenderer::Draw(wxGrid
& grid
,
2619 wxGridCellAttr
& attr
,
2625 wxGridCellRenderer::Draw(grid
, attr
, dc
, rect
, row
, col
, isSelected
);
2627 // draw a check mark in the centre (ignoring alignment - TODO)
2628 wxSize size
= GetBestSize(grid
, attr
, dc
, row
, col
);
2630 // don't draw outside the cell
2631 wxCoord minSize
= wxMin(rect
.width
, rect
.height
);
2632 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
2634 // and even leave (at least) 1 pixel margin
2635 size
.x
= size
.y
= minSize
;
2638 // draw a border around checkmark
2640 attr
.GetAlignment(&hAlign
, &vAlign
);
2643 if (hAlign
== wxALIGN_CENTRE
)
2645 rectBorder
.x
= rect
.x
+ rect
.width
/ 2 - size
.x
/ 2;
2646 rectBorder
.y
= rect
.y
+ rect
.height
/ 2 - size
.y
/ 2;
2647 rectBorder
.width
= size
.x
;
2648 rectBorder
.height
= size
.y
;
2650 else if (hAlign
== wxALIGN_LEFT
)
2652 rectBorder
.x
= rect
.x
+ 2;
2653 rectBorder
.y
= rect
.y
+ rect
.height
/ 2 - size
.y
/ 2;
2654 rectBorder
.width
= size
.x
;
2655 rectBorder
.height
= size
.y
;
2657 else if (hAlign
== wxALIGN_RIGHT
)
2659 rectBorder
.x
= rect
.x
+ rect
.width
- size
.x
- 2;
2660 rectBorder
.y
= rect
.y
+ rect
.height
/ 2 - size
.y
/ 2;
2661 rectBorder
.width
= size
.x
;
2662 rectBorder
.height
= size
.y
;
2666 if ( grid
.GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
) )
2668 value
= grid
.GetTable()->GetValueAsBool(row
, col
);
2672 wxString
cellval( grid
.GetTable()->GetValue(row
, col
) );
2673 value
= wxGridCellBoolEditor::IsTrueValue(cellval
);
2678 flags
|= wxCONTROL_CHECKED
;
2680 wxRendererNative::Get().DrawCheckBox( &grid
, dc
, rectBorder
, flags
);
2683 // ----------------------------------------------------------------------------
2685 // ----------------------------------------------------------------------------
2687 void wxGridCellAttr::Init(wxGridCellAttr
*attrDefault
)
2691 m_isReadOnly
= Unset
;
2696 m_attrkind
= wxGridCellAttr::Cell
;
2698 m_sizeRows
= m_sizeCols
= 1;
2699 m_overflow
= UnsetOverflow
;
2701 SetDefAttr(attrDefault
);
2704 wxGridCellAttr
*wxGridCellAttr::Clone() const
2706 wxGridCellAttr
*attr
= new wxGridCellAttr(m_defGridAttr
);
2708 if ( HasTextColour() )
2709 attr
->SetTextColour(GetTextColour());
2710 if ( HasBackgroundColour() )
2711 attr
->SetBackgroundColour(GetBackgroundColour());
2713 attr
->SetFont(GetFont());
2714 if ( HasAlignment() )
2715 attr
->SetAlignment(m_hAlign
, m_vAlign
);
2717 attr
->SetSize( m_sizeRows
, m_sizeCols
);
2721 attr
->SetRenderer(m_renderer
);
2722 m_renderer
->IncRef();
2726 attr
->SetEditor(m_editor
);
2731 attr
->SetReadOnly();
2733 attr
->SetOverflow( m_overflow
== Overflow
);
2734 attr
->SetKind( m_attrkind
);
2739 void wxGridCellAttr::MergeWith(wxGridCellAttr
*mergefrom
)
2741 if ( !HasTextColour() && mergefrom
->HasTextColour() )
2742 SetTextColour(mergefrom
->GetTextColour());
2743 if ( !HasBackgroundColour() && mergefrom
->HasBackgroundColour() )
2744 SetBackgroundColour(mergefrom
->GetBackgroundColour());
2745 if ( !HasFont() && mergefrom
->HasFont() )
2746 SetFont(mergefrom
->GetFont());
2747 if ( !HasAlignment() && mergefrom
->HasAlignment() )
2750 mergefrom
->GetAlignment( &hAlign
, &vAlign
);
2751 SetAlignment(hAlign
, vAlign
);
2753 if ( !HasSize() && mergefrom
->HasSize() )
2754 mergefrom
->GetSize( &m_sizeRows
, &m_sizeCols
);
2756 // Directly access member functions as GetRender/Editor don't just return
2757 // m_renderer/m_editor
2759 // Maybe add support for merge of Render and Editor?
2760 if (!HasRenderer() && mergefrom
->HasRenderer() )
2762 m_renderer
= mergefrom
->m_renderer
;
2763 m_renderer
->IncRef();
2765 if ( !HasEditor() && mergefrom
->HasEditor() )
2767 m_editor
= mergefrom
->m_editor
;
2770 if ( !HasReadWriteMode() && mergefrom
->HasReadWriteMode() )
2771 SetReadOnly(mergefrom
->IsReadOnly());
2773 if (!HasOverflowMode() && mergefrom
->HasOverflowMode() )
2774 SetOverflow(mergefrom
->GetOverflow());
2776 SetDefAttr(mergefrom
->m_defGridAttr
);
2779 void wxGridCellAttr::SetSize(int num_rows
, int num_cols
)
2781 // The size of a cell is normally 1,1
2783 // If this cell is larger (2,2) then this is the top left cell
2784 // the other cells that will be covered (lower right cells) must be
2785 // set to negative or zero values such that
2786 // row + num_rows of the covered cell points to the larger cell (this cell)
2787 // same goes for the col + num_cols.
2789 // Size of 0,0 is NOT valid, neither is <=0 and any positive value
2791 wxASSERT_MSG( (!((num_rows
> 0) && (num_cols
<= 0)) ||
2792 !((num_rows
<= 0) && (num_cols
> 0)) ||
2793 !((num_rows
== 0) && (num_cols
== 0))),
2794 wxT("wxGridCellAttr::SetSize only takes two postive values or negative/zero values"));
2796 m_sizeRows
= num_rows
;
2797 m_sizeCols
= num_cols
;
2800 const wxColour
& wxGridCellAttr::GetTextColour() const
2802 if (HasTextColour())
2806 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2808 return m_defGridAttr
->GetTextColour();
2812 wxFAIL_MSG(wxT("Missing default cell attribute"));
2813 return wxNullColour
;
2817 const wxColour
& wxGridCellAttr::GetBackgroundColour() const
2819 if (HasBackgroundColour())
2823 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2825 return m_defGridAttr
->GetBackgroundColour();
2829 wxFAIL_MSG(wxT("Missing default cell attribute"));
2830 return wxNullColour
;
2834 const wxFont
& wxGridCellAttr::GetFont() const
2840 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2842 return m_defGridAttr
->GetFont();
2846 wxFAIL_MSG(wxT("Missing default cell attribute"));
2851 void wxGridCellAttr::GetAlignment(int *hAlign
, int *vAlign
) const
2860 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2862 m_defGridAttr
->GetAlignment(hAlign
, vAlign
);
2866 wxFAIL_MSG(wxT("Missing default cell attribute"));
2870 void wxGridCellAttr::GetSize( int *num_rows
, int *num_cols
) const
2873 *num_rows
= m_sizeRows
;
2875 *num_cols
= m_sizeCols
;
2878 // GetRenderer and GetEditor use a slightly different decision path about
2879 // which attribute to use. If a non-default attr object has one then it is
2880 // used, otherwise the default editor or renderer is fetched from the grid and
2881 // used. It should be the default for the data type of the cell. If it is
2882 // NULL (because the table has a type that the grid does not have in its
2883 // registry), then the grid's default editor or renderer is used.
2885 wxGridCellRenderer
* wxGridCellAttr::GetRenderer(const wxGrid
* grid
, int row
, int col
) const
2887 wxGridCellRenderer
*renderer
= NULL
;
2889 if ( m_renderer
&& this != m_defGridAttr
)
2891 // use the cells renderer if it has one
2892 renderer
= m_renderer
;
2895 else // no non-default cell renderer
2897 // get default renderer for the data type
2900 // GetDefaultRendererForCell() will do IncRef() for us
2901 renderer
= grid
->GetDefaultRendererForCell(row
, col
);
2904 if ( renderer
== NULL
)
2906 if ( (m_defGridAttr
!= NULL
) && (m_defGridAttr
!= this) )
2908 // if we still don't have one then use the grid default
2909 // (no need for IncRef() here neither)
2910 renderer
= m_defGridAttr
->GetRenderer(NULL
, 0, 0);
2912 else // default grid attr
2914 // use m_renderer which we had decided not to use initially
2915 renderer
= m_renderer
;
2922 // we're supposed to always find something
2923 wxASSERT_MSG(renderer
, wxT("Missing default cell renderer"));
2928 // same as above, except for s/renderer/editor/g
2929 wxGridCellEditor
* wxGridCellAttr::GetEditor(const wxGrid
* grid
, int row
, int col
) const
2931 wxGridCellEditor
*editor
= NULL
;
2933 if ( m_editor
&& this != m_defGridAttr
)
2935 // use the cells editor if it has one
2939 else // no non default cell editor
2941 // get default editor for the data type
2944 // GetDefaultEditorForCell() will do IncRef() for us
2945 editor
= grid
->GetDefaultEditorForCell(row
, col
);
2948 if ( editor
== NULL
)
2950 if ( (m_defGridAttr
!= NULL
) && (m_defGridAttr
!= this) )
2952 // if we still don't have one then use the grid default
2953 // (no need for IncRef() here neither)
2954 editor
= m_defGridAttr
->GetEditor(NULL
, 0, 0);
2956 else // default grid attr
2958 // use m_editor which we had decided not to use initially
2966 // we're supposed to always find something
2967 wxASSERT_MSG(editor
, wxT("Missing default cell editor"));
2972 // ----------------------------------------------------------------------------
2973 // wxGridCellAttrData
2974 // ----------------------------------------------------------------------------
2976 void wxGridCellAttrData::SetAttr(wxGridCellAttr
*attr
, int row
, int col
)
2978 // Note: contrary to wxGridRowOrColAttrData::SetAttr, we must not
2979 // touch attribute's reference counting explicitly, since this
2980 // is managed by class wxGridCellWithAttr
2981 int n
= FindIndex(row
, col
);
2982 if ( n
== wxNOT_FOUND
)
2986 // add the attribute
2987 m_attrs
.Add(new wxGridCellWithAttr(row
, col
, attr
));
2989 //else: nothing to do
2991 else // we already have an attribute for this cell
2995 // change the attribute
2996 m_attrs
[(size_t)n
].ChangeAttr(attr
);
3000 // remove this attribute
3001 m_attrs
.RemoveAt((size_t)n
);
3006 wxGridCellAttr
*wxGridCellAttrData::GetAttr(int row
, int col
) const
3008 wxGridCellAttr
*attr
= NULL
;
3010 int n
= FindIndex(row
, col
);
3011 if ( n
!= wxNOT_FOUND
)
3013 attr
= m_attrs
[(size_t)n
].attr
;
3020 void wxGridCellAttrData::UpdateAttrRows( size_t pos
, int numRows
)
3022 size_t count
= m_attrs
.GetCount();
3023 for ( size_t n
= 0; n
< count
; n
++ )
3025 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
3026 wxCoord row
= coords
.GetRow();
3027 if ((size_t)row
>= pos
)
3031 // If rows inserted, include row counter where necessary
3032 coords
.SetRow(row
+ numRows
);
3034 else if (numRows
< 0)
3036 // If rows deleted ...
3037 if ((size_t)row
>= pos
- numRows
)
3039 // ...either decrement row counter (if row still exists)...
3040 coords
.SetRow(row
+ numRows
);
3044 // ...or remove the attribute
3045 m_attrs
.RemoveAt(n
);
3054 void wxGridCellAttrData::UpdateAttrCols( size_t pos
, int numCols
)
3056 size_t count
= m_attrs
.GetCount();
3057 for ( size_t n
= 0; n
< count
; n
++ )
3059 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
3060 wxCoord col
= coords
.GetCol();
3061 if ( (size_t)col
>= pos
)
3065 // If rows inserted, include row counter where necessary
3066 coords
.SetCol(col
+ numCols
);
3068 else if (numCols
< 0)
3070 // If rows deleted ...
3071 if ((size_t)col
>= pos
- numCols
)
3073 // ...either decrement row counter (if row still exists)...
3074 coords
.SetCol(col
+ numCols
);
3078 // ...or remove the attribute
3079 m_attrs
.RemoveAt(n
);
3088 int wxGridCellAttrData::FindIndex(int row
, int col
) const
3090 size_t count
= m_attrs
.GetCount();
3091 for ( size_t n
= 0; n
< count
; n
++ )
3093 const wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
3094 if ( (coords
.GetRow() == row
) && (coords
.GetCol() == col
) )
3103 // ----------------------------------------------------------------------------
3104 // wxGridRowOrColAttrData
3105 // ----------------------------------------------------------------------------
3107 wxGridRowOrColAttrData::~wxGridRowOrColAttrData()
3109 size_t count
= m_attrs
.GetCount();
3110 for ( size_t n
= 0; n
< count
; n
++ )
3112 m_attrs
[n
]->DecRef();
3116 wxGridCellAttr
*wxGridRowOrColAttrData::GetAttr(int rowOrCol
) const
3118 wxGridCellAttr
*attr
= NULL
;
3120 int n
= m_rowsOrCols
.Index(rowOrCol
);
3121 if ( n
!= wxNOT_FOUND
)
3123 attr
= m_attrs
[(size_t)n
];
3130 void wxGridRowOrColAttrData::SetAttr(wxGridCellAttr
*attr
, int rowOrCol
)
3132 int i
= m_rowsOrCols
.Index(rowOrCol
);
3133 if ( i
== wxNOT_FOUND
)
3137 // add the attribute - no need to do anything to reference count
3138 // since we take ownership of the attribute.
3139 m_rowsOrCols
.Add(rowOrCol
);
3142 // nothing to remove
3146 size_t n
= (size_t)i
;
3147 if ( m_attrs
[n
] == attr
)
3152 // change the attribute, handling reference count manually,
3153 // taking ownership of the new attribute.
3154 m_attrs
[n
]->DecRef();
3159 // remove this attribute, handling reference count manually
3160 m_attrs
[n
]->DecRef();
3161 m_rowsOrCols
.RemoveAt(n
);
3162 m_attrs
.RemoveAt(n
);
3167 void wxGridRowOrColAttrData::UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
)
3169 size_t count
= m_attrs
.GetCount();
3170 for ( size_t n
= 0; n
< count
; n
++ )
3172 int & rowOrCol
= m_rowsOrCols
[n
];
3173 if ( (size_t)rowOrCol
>= pos
)
3175 if ( numRowsOrCols
> 0 )
3177 // If rows inserted, include row counter where necessary
3178 rowOrCol
+= numRowsOrCols
;
3180 else if ( numRowsOrCols
< 0)
3182 // If rows deleted, either decrement row counter (if row still exists)
3183 if ((size_t)rowOrCol
>= pos
- numRowsOrCols
)
3184 rowOrCol
+= numRowsOrCols
;
3187 m_rowsOrCols
.RemoveAt(n
);
3188 m_attrs
[n
]->DecRef();
3189 m_attrs
.RemoveAt(n
);
3198 // ----------------------------------------------------------------------------
3199 // wxGridCellAttrProvider
3200 // ----------------------------------------------------------------------------
3202 wxGridCellAttrProvider::wxGridCellAttrProvider()
3207 wxGridCellAttrProvider::~wxGridCellAttrProvider()
3212 void wxGridCellAttrProvider::InitData()
3214 m_data
= new wxGridCellAttrProviderData
;
3217 wxGridCellAttr
*wxGridCellAttrProvider::GetAttr(int row
, int col
,
3218 wxGridCellAttr::wxAttrKind kind
) const
3220 wxGridCellAttr
*attr
= NULL
;
3225 case (wxGridCellAttr::Any
):
3226 // Get cached merge attributes.
3227 // Currently not used as no cache implemented as not mutable
3228 // attr = m_data->m_mergeAttr.GetAttr(row, col);
3231 // Basically implement old version.
3232 // Also check merge cache, so we don't have to re-merge every time..
3233 wxGridCellAttr
*attrcell
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
3234 wxGridCellAttr
*attrrow
= m_data
->m_rowAttrs
.GetAttr(row
);
3235 wxGridCellAttr
*attrcol
= m_data
->m_colAttrs
.GetAttr(col
);
3237 if ((attrcell
!= attrrow
) && (attrrow
!= attrcol
) && (attrcell
!= attrcol
))
3239 // Two or more are non NULL
3240 attr
= new wxGridCellAttr
;
3241 attr
->SetKind(wxGridCellAttr::Merged
);
3243 // Order is important..
3246 attr
->MergeWith(attrcell
);
3251 attr
->MergeWith(attrcol
);
3256 attr
->MergeWith(attrrow
);
3260 // store merge attr if cache implemented
3262 //m_data->m_mergeAttr.SetAttr(attr, row, col);
3266 // one or none is non null return it or null.
3285 case (wxGridCellAttr::Cell
):
3286 attr
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
3289 case (wxGridCellAttr::Col
):
3290 attr
= m_data
->m_colAttrs
.GetAttr(col
);
3293 case (wxGridCellAttr::Row
):
3294 attr
= m_data
->m_rowAttrs
.GetAttr(row
);
3299 // (wxGridCellAttr::Default):
3300 // (wxGridCellAttr::Merged):
3308 void wxGridCellAttrProvider::SetAttr(wxGridCellAttr
*attr
,
3314 m_data
->m_cellAttrs
.SetAttr(attr
, row
, col
);
3317 void wxGridCellAttrProvider::SetRowAttr(wxGridCellAttr
*attr
, int row
)
3322 m_data
->m_rowAttrs
.SetAttr(attr
, row
);
3325 void wxGridCellAttrProvider::SetColAttr(wxGridCellAttr
*attr
, int col
)
3330 m_data
->m_colAttrs
.SetAttr(attr
, col
);
3333 void wxGridCellAttrProvider::UpdateAttrRows( size_t pos
, int numRows
)
3337 m_data
->m_cellAttrs
.UpdateAttrRows( pos
, numRows
);
3339 m_data
->m_rowAttrs
.UpdateAttrRowsOrCols( pos
, numRows
);
3343 void wxGridCellAttrProvider::UpdateAttrCols( size_t pos
, int numCols
)
3347 m_data
->m_cellAttrs
.UpdateAttrCols( pos
, numCols
);
3349 m_data
->m_colAttrs
.UpdateAttrRowsOrCols( pos
, numCols
);
3353 // ----------------------------------------------------------------------------
3354 // wxGridTypeRegistry
3355 // ----------------------------------------------------------------------------
3357 wxGridTypeRegistry::~wxGridTypeRegistry()
3359 size_t count
= m_typeinfo
.GetCount();
3360 for ( size_t i
= 0; i
< count
; i
++ )
3361 delete m_typeinfo
[i
];
3364 void wxGridTypeRegistry::RegisterDataType(const wxString
& typeName
,
3365 wxGridCellRenderer
* renderer
,
3366 wxGridCellEditor
* editor
)
3368 wxGridDataTypeInfo
* info
= new wxGridDataTypeInfo(typeName
, renderer
, editor
);
3370 // is it already registered?
3371 int loc
= FindRegisteredDataType(typeName
);
3372 if ( loc
!= wxNOT_FOUND
)
3374 delete m_typeinfo
[loc
];
3375 m_typeinfo
[loc
] = info
;
3379 m_typeinfo
.Add(info
);
3383 int wxGridTypeRegistry::FindRegisteredDataType(const wxString
& typeName
)
3385 size_t count
= m_typeinfo
.GetCount();
3386 for ( size_t i
= 0; i
< count
; i
++ )
3388 if ( typeName
== m_typeinfo
[i
]->m_typeName
)
3397 int wxGridTypeRegistry::FindDataType(const wxString
& typeName
)
3399 int index
= FindRegisteredDataType(typeName
);
3400 if ( index
== wxNOT_FOUND
)
3402 // check whether this is one of the standard ones, in which case
3403 // register it "on the fly"
3405 if ( typeName
== wxGRID_VALUE_STRING
)
3407 RegisterDataType(wxGRID_VALUE_STRING
,
3408 new wxGridCellStringRenderer
,
3409 new wxGridCellTextEditor
);
3412 #endif // wxUSE_TEXTCTRL
3414 if ( typeName
== wxGRID_VALUE_BOOL
)
3416 RegisterDataType(wxGRID_VALUE_BOOL
,
3417 new wxGridCellBoolRenderer
,
3418 new wxGridCellBoolEditor
);
3421 #endif // wxUSE_CHECKBOX
3423 if ( typeName
== wxGRID_VALUE_NUMBER
)
3425 RegisterDataType(wxGRID_VALUE_NUMBER
,
3426 new wxGridCellNumberRenderer
,
3427 new wxGridCellNumberEditor
);
3429 else if ( typeName
== wxGRID_VALUE_FLOAT
)
3431 RegisterDataType(wxGRID_VALUE_FLOAT
,
3432 new wxGridCellFloatRenderer
,
3433 new wxGridCellFloatEditor
);
3436 #endif // wxUSE_TEXTCTRL
3438 if ( typeName
== wxGRID_VALUE_CHOICE
)
3440 RegisterDataType(wxGRID_VALUE_CHOICE
,
3441 new wxGridCellStringRenderer
,
3442 new wxGridCellChoiceEditor
);
3445 #endif // wxUSE_COMBOBOX
3450 // we get here only if just added the entry for this type, so return
3452 index
= m_typeinfo
.GetCount() - 1;
3458 int wxGridTypeRegistry::FindOrCloneDataType(const wxString
& typeName
)
3460 int index
= FindDataType(typeName
);
3461 if ( index
== wxNOT_FOUND
)
3463 // the first part of the typename is the "real" type, anything after ':'
3464 // are the parameters for the renderer
3465 index
= FindDataType(typeName
.BeforeFirst(_T(':')));
3466 if ( index
== wxNOT_FOUND
)
3471 wxGridCellRenderer
*renderer
= GetRenderer(index
);
3472 wxGridCellRenderer
*rendererOld
= renderer
;
3473 renderer
= renderer
->Clone();
3474 rendererOld
->DecRef();
3476 wxGridCellEditor
*editor
= GetEditor(index
);
3477 wxGridCellEditor
*editorOld
= editor
;
3478 editor
= editor
->Clone();
3479 editorOld
->DecRef();
3481 // do it even if there are no parameters to reset them to defaults
3482 wxString params
= typeName
.AfterFirst(_T(':'));
3483 renderer
->SetParameters(params
);
3484 editor
->SetParameters(params
);
3486 // register the new typename
3487 RegisterDataType(typeName
, renderer
, editor
);
3489 // we just registered it, it's the last one
3490 index
= m_typeinfo
.GetCount() - 1;
3496 wxGridCellRenderer
* wxGridTypeRegistry::GetRenderer(int index
)
3498 wxGridCellRenderer
* renderer
= m_typeinfo
[index
]->m_renderer
;
3505 wxGridCellEditor
* wxGridTypeRegistry::GetEditor(int index
)
3507 wxGridCellEditor
* editor
= m_typeinfo
[index
]->m_editor
;
3514 // ----------------------------------------------------------------------------
3516 // ----------------------------------------------------------------------------
3518 IMPLEMENT_ABSTRACT_CLASS( wxGridTableBase
, wxObject
)
3520 wxGridTableBase::wxGridTableBase()
3523 m_attrProvider
= NULL
;
3526 wxGridTableBase::~wxGridTableBase()
3528 delete m_attrProvider
;
3531 void wxGridTableBase::SetAttrProvider(wxGridCellAttrProvider
*attrProvider
)
3533 delete m_attrProvider
;
3534 m_attrProvider
= attrProvider
;
3537 bool wxGridTableBase::CanHaveAttributes()
3539 if ( ! GetAttrProvider() )
3541 // use the default attr provider by default
3542 SetAttrProvider(new wxGridCellAttrProvider
);
3548 wxGridCellAttr
*wxGridTableBase::GetAttr(int row
, int col
, wxGridCellAttr::wxAttrKind kind
)
3550 if ( m_attrProvider
)
3551 return m_attrProvider
->GetAttr(row
, col
, kind
);
3556 void wxGridTableBase::SetAttr(wxGridCellAttr
* attr
, int row
, int col
)
3558 if ( m_attrProvider
)
3561 attr
->SetKind(wxGridCellAttr::Cell
);
3562 m_attrProvider
->SetAttr(attr
, row
, col
);
3566 // as we take ownership of the pointer and don't store it, we must
3572 void wxGridTableBase::SetRowAttr(wxGridCellAttr
*attr
, int row
)
3574 if ( m_attrProvider
)
3576 attr
->SetKind(wxGridCellAttr::Row
);
3577 m_attrProvider
->SetRowAttr(attr
, row
);
3581 // as we take ownership of the pointer and don't store it, we must
3587 void wxGridTableBase::SetColAttr(wxGridCellAttr
*attr
, int col
)
3589 if ( m_attrProvider
)
3591 attr
->SetKind(wxGridCellAttr::Col
);
3592 m_attrProvider
->SetColAttr(attr
, col
);
3596 // as we take ownership of the pointer and don't store it, we must
3602 bool wxGridTableBase::InsertRows( size_t WXUNUSED(pos
),
3603 size_t WXUNUSED(numRows
) )
3605 wxFAIL_MSG( wxT("Called grid table class function InsertRows\nbut your derived table class does not override this function") );
3610 bool wxGridTableBase::AppendRows( size_t WXUNUSED(numRows
) )
3612 wxFAIL_MSG( wxT("Called grid table class function AppendRows\nbut your derived table class does not override this function"));
3617 bool wxGridTableBase::DeleteRows( size_t WXUNUSED(pos
),
3618 size_t WXUNUSED(numRows
) )
3620 wxFAIL_MSG( wxT("Called grid table class function DeleteRows\nbut your derived table class does not override this function"));
3625 bool wxGridTableBase::InsertCols( size_t WXUNUSED(pos
),
3626 size_t WXUNUSED(numCols
) )
3628 wxFAIL_MSG( wxT("Called grid table class function InsertCols\nbut your derived table class does not override this function"));
3633 bool wxGridTableBase::AppendCols( size_t WXUNUSED(numCols
) )
3635 wxFAIL_MSG(wxT("Called grid table class function AppendCols\nbut your derived table class does not override this function"));
3640 bool wxGridTableBase::DeleteCols( size_t WXUNUSED(pos
),
3641 size_t WXUNUSED(numCols
) )
3643 wxFAIL_MSG( wxT("Called grid table class function DeleteCols\nbut your derived table class does not override this function"));
3648 wxString
wxGridTableBase::GetRowLabelValue( int row
)
3652 // RD: Starting the rows at zero confuses users,
3653 // no matter how much it makes sense to us geeks.
3659 wxString
wxGridTableBase::GetColLabelValue( int col
)
3661 // default col labels are:
3662 // cols 0 to 25 : A-Z
3663 // cols 26 to 675 : AA-ZZ
3668 for ( n
= 1; ; n
++ )
3670 s
+= (wxChar
) (_T('A') + (wxChar
)(col
% 26));
3676 // reverse the string...
3678 for ( i
= 0; i
< n
; i
++ )
3686 wxString
wxGridTableBase::GetTypeName( int WXUNUSED(row
), int WXUNUSED(col
) )
3688 return wxGRID_VALUE_STRING
;
3691 bool wxGridTableBase::CanGetValueAs( int WXUNUSED(row
), int WXUNUSED(col
),
3692 const wxString
& typeName
)
3694 return typeName
== wxGRID_VALUE_STRING
;
3697 bool wxGridTableBase::CanSetValueAs( int row
, int col
, const wxString
& typeName
)
3699 return CanGetValueAs(row
, col
, typeName
);
3702 long wxGridTableBase::GetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
) )
3707 double wxGridTableBase::GetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
) )
3712 bool wxGridTableBase::GetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
) )
3717 void wxGridTableBase::SetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
),
3718 long WXUNUSED(value
) )
3722 void wxGridTableBase::SetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
),
3723 double WXUNUSED(value
) )
3727 void wxGridTableBase::SetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
),
3728 bool WXUNUSED(value
) )
3732 void* wxGridTableBase::GetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
3733 const wxString
& WXUNUSED(typeName
) )
3738 void wxGridTableBase::SetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
3739 const wxString
& WXUNUSED(typeName
),
3740 void* WXUNUSED(value
) )
3744 //////////////////////////////////////////////////////////////////////
3746 // Message class for the grid table to send requests and notifications
3750 wxGridTableMessage::wxGridTableMessage()
3758 wxGridTableMessage::wxGridTableMessage( wxGridTableBase
*table
, int id
,
3759 int commandInt1
, int commandInt2
)
3763 m_comInt1
= commandInt1
;
3764 m_comInt2
= commandInt2
;
3767 //////////////////////////////////////////////////////////////////////
3769 // A basic grid table for string data. An object of this class will
3770 // created by wxGrid if you don't specify an alternative table class.
3773 WX_DEFINE_OBJARRAY(wxGridStringArray
)
3775 IMPLEMENT_DYNAMIC_CLASS( wxGridStringTable
, wxGridTableBase
)
3777 wxGridStringTable::wxGridStringTable()
3782 wxGridStringTable::wxGridStringTable( int numRows
, int numCols
)
3785 m_data
.Alloc( numRows
);
3788 sa
.Alloc( numCols
);
3789 sa
.Add( wxEmptyString
, numCols
);
3791 m_data
.Add( sa
, numRows
);
3794 wxGridStringTable::~wxGridStringTable()
3798 int wxGridStringTable::GetNumberRows()
3800 return m_data
.GetCount();
3803 int wxGridStringTable::GetNumberCols()
3805 if ( m_data
.GetCount() > 0 )
3806 return m_data
[0].GetCount();
3811 wxString
wxGridStringTable::GetValue( int row
, int col
)
3813 wxCHECK_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3815 _T("invalid row or column index in wxGridStringTable") );
3817 return m_data
[row
][col
];
3820 void wxGridStringTable::SetValue( int row
, int col
, const wxString
& value
)
3822 wxCHECK_RET( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3823 _T("invalid row or column index in wxGridStringTable") );
3825 m_data
[row
][col
] = value
;
3828 bool wxGridStringTable::IsEmptyCell( int row
, int col
)
3830 wxCHECK_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3832 _T("invalid row or column index in wxGridStringTable") );
3834 return (m_data
[row
][col
] == wxEmptyString
);
3837 void wxGridStringTable::Clear()
3840 int numRows
, numCols
;
3842 numRows
= m_data
.GetCount();
3845 numCols
= m_data
[0].GetCount();
3847 for ( row
= 0; row
< numRows
; row
++ )
3849 for ( col
= 0; col
< numCols
; col
++ )
3851 m_data
[row
][col
] = wxEmptyString
;
3857 bool wxGridStringTable::InsertRows( size_t pos
, size_t numRows
)
3859 size_t curNumRows
= m_data
.GetCount();
3860 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3861 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3863 if ( pos
>= curNumRows
)
3865 return AppendRows( numRows
);
3869 sa
.Alloc( curNumCols
);
3870 sa
.Add( wxEmptyString
, curNumCols
);
3871 m_data
.Insert( sa
, pos
, numRows
);
3875 wxGridTableMessage
msg( this,
3876 wxGRIDTABLE_NOTIFY_ROWS_INSERTED
,
3880 GetView()->ProcessTableMessage( msg
);
3886 bool wxGridStringTable::AppendRows( size_t numRows
)
3888 size_t curNumRows
= m_data
.GetCount();
3889 size_t curNumCols
= ( curNumRows
> 0
3890 ? m_data
[0].GetCount()
3891 : ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3894 if ( curNumCols
> 0 )
3896 sa
.Alloc( curNumCols
);
3897 sa
.Add( wxEmptyString
, curNumCols
);
3900 m_data
.Add( sa
, numRows
);
3904 wxGridTableMessage
msg( this,
3905 wxGRIDTABLE_NOTIFY_ROWS_APPENDED
,
3908 GetView()->ProcessTableMessage( msg
);
3914 bool wxGridStringTable::DeleteRows( size_t pos
, size_t numRows
)
3916 size_t curNumRows
= m_data
.GetCount();
3918 if ( pos
>= curNumRows
)
3920 wxFAIL_MSG( wxString::Format
3922 wxT("Called wxGridStringTable::DeleteRows(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu rows"),
3924 (unsigned long)numRows
,
3925 (unsigned long)curNumRows
3931 if ( numRows
> curNumRows
- pos
)
3933 numRows
= curNumRows
- pos
;
3936 if ( numRows
>= curNumRows
)
3942 m_data
.RemoveAt( pos
, numRows
);
3947 wxGridTableMessage
msg( this,
3948 wxGRIDTABLE_NOTIFY_ROWS_DELETED
,
3952 GetView()->ProcessTableMessage( msg
);
3958 bool wxGridStringTable::InsertCols( size_t pos
, size_t numCols
)
3962 size_t curNumRows
= m_data
.GetCount();
3963 size_t curNumCols
= ( curNumRows
> 0
3964 ? m_data
[0].GetCount()
3965 : ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3967 if ( pos
>= curNumCols
)
3969 return AppendCols( numCols
);
3972 if ( !m_colLabels
.IsEmpty() )
3974 m_colLabels
.Insert( wxEmptyString
, pos
, numCols
);
3977 for ( i
= pos
; i
< pos
+ numCols
; i
++ )
3978 m_colLabels
[i
] = wxGridTableBase::GetColLabelValue( i
);
3981 for ( row
= 0; row
< curNumRows
; row
++ )
3983 for ( col
= pos
; col
< pos
+ numCols
; col
++ )
3985 m_data
[row
].Insert( wxEmptyString
, col
);
3991 wxGridTableMessage
msg( this,
3992 wxGRIDTABLE_NOTIFY_COLS_INSERTED
,
3996 GetView()->ProcessTableMessage( msg
);
4002 bool wxGridStringTable::AppendCols( size_t numCols
)
4006 size_t curNumRows
= m_data
.GetCount();
4008 for ( row
= 0; row
< curNumRows
; row
++ )
4010 m_data
[row
].Add( wxEmptyString
, numCols
);
4015 wxGridTableMessage
msg( this,
4016 wxGRIDTABLE_NOTIFY_COLS_APPENDED
,
4019 GetView()->ProcessTableMessage( msg
);
4025 bool wxGridStringTable::DeleteCols( size_t pos
, size_t numCols
)
4029 size_t curNumRows
= m_data
.GetCount();
4030 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
4031 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
4033 if ( pos
>= curNumCols
)
4035 wxFAIL_MSG( wxString::Format
4037 wxT("Called wxGridStringTable::DeleteCols(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu cols"),
4039 (unsigned long)numCols
,
4040 (unsigned long)curNumCols
4047 colID
= GetView()->GetColAt( pos
);
4051 if ( numCols
> curNumCols
- colID
)
4053 numCols
= curNumCols
- colID
;
4056 if ( !m_colLabels
.IsEmpty() )
4058 // m_colLabels stores just as many elements as it needs, e.g. if only
4059 // the label of the first column had been set it would have only one
4060 // element and not numCols, so account for it
4061 int nToRm
= m_colLabels
.size() - colID
;
4063 m_colLabels
.RemoveAt( colID
, nToRm
);
4066 for ( row
= 0; row
< curNumRows
; row
++ )
4068 if ( numCols
>= curNumCols
)
4070 m_data
[row
].Clear();
4074 m_data
[row
].RemoveAt( colID
, numCols
);
4080 wxGridTableMessage
msg( this,
4081 wxGRIDTABLE_NOTIFY_COLS_DELETED
,
4085 GetView()->ProcessTableMessage( msg
);
4091 wxString
wxGridStringTable::GetRowLabelValue( int row
)
4093 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
4095 // using default label
4097 return wxGridTableBase::GetRowLabelValue( row
);
4101 return m_rowLabels
[row
];
4105 wxString
wxGridStringTable::GetColLabelValue( int col
)
4107 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
4109 // using default label
4111 return wxGridTableBase::GetColLabelValue( col
);
4115 return m_colLabels
[col
];
4119 void wxGridStringTable::SetRowLabelValue( int row
, const wxString
& value
)
4121 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
4123 int n
= m_rowLabels
.GetCount();
4126 for ( i
= n
; i
<= row
; i
++ )
4128 m_rowLabels
.Add( wxGridTableBase::GetRowLabelValue(i
) );
4132 m_rowLabels
[row
] = value
;
4135 void wxGridStringTable::SetColLabelValue( int col
, const wxString
& value
)
4137 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
4139 int n
= m_colLabels
.GetCount();
4142 for ( i
= n
; i
<= col
; i
++ )
4144 m_colLabels
.Add( wxGridTableBase::GetColLabelValue(i
) );
4148 m_colLabels
[col
] = value
;
4152 //////////////////////////////////////////////////////////////////////
4153 //////////////////////////////////////////////////////////////////////
4155 BEGIN_EVENT_TABLE(wxGridSubwindow
, wxWindow
)
4156 EVT_MOUSE_CAPTURE_LOST(wxGridSubwindow::OnMouseCaptureLost
)
4159 void wxGridSubwindow::OnMouseCaptureLost(wxMouseCaptureLostEvent
& WXUNUSED(event
))
4161 m_owner
->CancelMouseCapture();
4164 IMPLEMENT_DYNAMIC_CLASS( wxGridRowLabelWindow
, wxWindow
)
4166 BEGIN_EVENT_TABLE( wxGridRowLabelWindow
, wxGridSubwindow
)
4167 EVT_PAINT( wxGridRowLabelWindow::OnPaint
)
4168 EVT_MOUSEWHEEL( wxGridRowLabelWindow::OnMouseWheel
)
4169 EVT_MOUSE_EVENTS( wxGridRowLabelWindow::OnMouseEvent
)
4172 wxGridRowLabelWindow::wxGridRowLabelWindow( wxGrid
*parent
,
4174 const wxPoint
&pos
, const wxSize
&size
)
4175 : wxGridSubwindow(parent
, id
, pos
, size
)
4180 void wxGridRowLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
4184 // NO - don't do this because it will set both the x and y origin
4185 // coords to match the parent scrolled window and we just want to
4186 // set the y coord - MB
4188 // m_owner->PrepareDC( dc );
4191 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
4192 wxPoint pt
= dc
.GetDeviceOrigin();
4193 dc
.SetDeviceOrigin( pt
.x
, pt
.y
-y
);
4195 wxArrayInt rows
= m_owner
->CalcRowLabelsExposed( GetUpdateRegion() );
4196 m_owner
->DrawRowLabels( dc
, rows
);
4199 void wxGridRowLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
4201 m_owner
->ProcessRowLabelMouseEvent( event
);
4204 void wxGridRowLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
4206 m_owner
->GetEventHandler()->ProcessEvent( event
);
4209 //////////////////////////////////////////////////////////////////////
4211 IMPLEMENT_DYNAMIC_CLASS( wxGridColLabelWindow
, wxWindow
)
4213 BEGIN_EVENT_TABLE( wxGridColLabelWindow
, wxGridSubwindow
)
4214 EVT_PAINT( wxGridColLabelWindow::OnPaint
)
4215 EVT_MOUSEWHEEL( wxGridColLabelWindow::OnMouseWheel
)
4216 EVT_MOUSE_EVENTS( wxGridColLabelWindow::OnMouseEvent
)
4219 wxGridColLabelWindow::wxGridColLabelWindow( wxGrid
*parent
,
4221 const wxPoint
&pos
, const wxSize
&size
)
4222 : wxGridSubwindow(parent
, id
, pos
, size
)
4227 void wxGridColLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
4231 // NO - don't do this because it will set both the x and y origin
4232 // coords to match the parent scrolled window and we just want to
4233 // set the x coord - MB
4235 // m_owner->PrepareDC( dc );
4238 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
4239 wxPoint pt
= dc
.GetDeviceOrigin();
4240 if (GetLayoutDirection() == wxLayout_RightToLeft
)
4241 dc
.SetDeviceOrigin( pt
.x
+x
, pt
.y
);
4243 dc
.SetDeviceOrigin( pt
.x
-x
, pt
.y
);
4245 wxArrayInt cols
= m_owner
->CalcColLabelsExposed( GetUpdateRegion() );
4246 m_owner
->DrawColLabels( dc
, cols
);
4249 void wxGridColLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
4251 m_owner
->ProcessColLabelMouseEvent( event
);
4254 void wxGridColLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
4256 m_owner
->GetEventHandler()->ProcessEvent( event
);
4259 //////////////////////////////////////////////////////////////////////
4261 IMPLEMENT_DYNAMIC_CLASS( wxGridCornerLabelWindow
, wxWindow
)
4263 BEGIN_EVENT_TABLE( wxGridCornerLabelWindow
, wxGridSubwindow
)
4264 EVT_MOUSEWHEEL( wxGridCornerLabelWindow::OnMouseWheel
)
4265 EVT_MOUSE_EVENTS( wxGridCornerLabelWindow::OnMouseEvent
)
4266 EVT_PAINT( wxGridCornerLabelWindow::OnPaint
)
4269 wxGridCornerLabelWindow::wxGridCornerLabelWindow( wxGrid
*parent
,
4272 const wxSize
& size
)
4273 : wxGridSubwindow(parent
, id
, pos
, size
)
4278 void wxGridCornerLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
4282 m_owner
->DrawCornerLabel(dc
);
4285 void wxGridCornerLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
4287 m_owner
->ProcessCornerLabelMouseEvent( event
);
4290 void wxGridCornerLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
4292 m_owner
->GetEventHandler()->ProcessEvent(event
);
4295 //////////////////////////////////////////////////////////////////////
4297 IMPLEMENT_DYNAMIC_CLASS( wxGridWindow
, wxWindow
)
4299 BEGIN_EVENT_TABLE( wxGridWindow
, wxGridSubwindow
)
4300 EVT_PAINT( wxGridWindow::OnPaint
)
4301 EVT_MOUSEWHEEL( wxGridWindow::OnMouseWheel
)
4302 EVT_MOUSE_EVENTS( wxGridWindow::OnMouseEvent
)
4303 EVT_KEY_DOWN( wxGridWindow::OnKeyDown
)
4304 EVT_KEY_UP( wxGridWindow::OnKeyUp
)
4305 EVT_CHAR( wxGridWindow::OnChar
)
4306 EVT_SET_FOCUS( wxGridWindow::OnFocus
)
4307 EVT_KILL_FOCUS( wxGridWindow::OnFocus
)
4308 EVT_ERASE_BACKGROUND( wxGridWindow::OnEraseBackground
)
4311 wxGridWindow::wxGridWindow( wxGrid
*parent
,
4312 wxGridRowLabelWindow
*rowLblWin
,
4313 wxGridColLabelWindow
*colLblWin
,
4316 const wxSize
&size
)
4317 : wxGridSubwindow(parent
, id
, pos
, size
,
4318 wxWANTS_CHARS
| wxCLIP_CHILDREN
,
4319 wxT("grid window") )
4322 m_rowLabelWin
= rowLblWin
;
4323 m_colLabelWin
= colLblWin
;
4326 void wxGridWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
4328 wxPaintDC
dc( this );
4329 m_owner
->PrepareDC( dc
);
4330 wxRegion reg
= GetUpdateRegion();
4331 wxGridCellCoordsArray dirtyCells
= m_owner
->CalcCellsExposed( reg
);
4332 m_owner
->DrawGridCellArea( dc
, dirtyCells
);
4334 m_owner
->DrawAllGridLines( dc
, reg
);
4336 m_owner
->DrawGridSpace( dc
);
4337 m_owner
->DrawHighlight( dc
, dirtyCells
);
4340 void wxGridWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
4342 wxWindow::ScrollWindow( dx
, dy
, rect
);
4343 m_rowLabelWin
->ScrollWindow( 0, dy
, rect
);
4344 m_colLabelWin
->ScrollWindow( dx
, 0, rect
);
4347 void wxGridWindow::OnMouseEvent( wxMouseEvent
& event
)
4349 if (event
.ButtonDown(wxMOUSE_BTN_LEFT
) && FindFocus() != this)
4352 m_owner
->ProcessGridCellMouseEvent( event
);
4355 void wxGridWindow::OnMouseWheel( wxMouseEvent
& event
)
4357 m_owner
->GetEventHandler()->ProcessEvent( event
);
4360 // This seems to be required for wxMotif/wxGTK otherwise the mouse
4361 // cursor must be in the cell edit control to get key events
4363 void wxGridWindow::OnKeyDown( wxKeyEvent
& event
)
4365 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
4369 void wxGridWindow::OnKeyUp( wxKeyEvent
& event
)
4371 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
4375 void wxGridWindow::OnChar( wxKeyEvent
& event
)
4377 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
4381 void wxGridWindow::OnEraseBackground( wxEraseEvent
& WXUNUSED(event
) )
4385 void wxGridWindow::OnFocus(wxFocusEvent
& event
)
4387 // and if we have any selection, it has to be repainted, because it
4388 // uses different colour when the grid is not focused:
4389 if ( m_owner
->IsSelection() )
4395 // NB: Note that this code is in "else" branch only because the other
4396 // branch refreshes everything and so there's no point in calling
4397 // Refresh() again, *not* because it should only be done if
4398 // !IsSelection(). If the above code is ever optimized to refresh
4399 // only selected area, this needs to be moved out of the "else"
4400 // branch so that it's always executed.
4402 // current cell cursor {dis,re}appears on focus change:
4403 const wxGridCellCoords
cursorCoords(m_owner
->GetGridCursorRow(),
4404 m_owner
->GetGridCursorCol());
4405 const wxRect cursor
=
4406 m_owner
->BlockToDeviceRect(cursorCoords
, cursorCoords
);
4407 Refresh(true, &cursor
);
4410 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
4414 #define internalXToCol(x) XToCol(x, true)
4415 #define internalYToRow(y) YToRow(y, true)
4417 /////////////////////////////////////////////////////////////////////
4419 #if wxUSE_EXTENDED_RTTI
4420 WX_DEFINE_FLAGS( wxGridStyle
)
4422 wxBEGIN_FLAGS( wxGridStyle
)
4423 // new style border flags, we put them first to
4424 // use them for streaming out
4425 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
4426 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
4427 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
4428 wxFLAGS_MEMBER(wxBORDER_RAISED
)
4429 wxFLAGS_MEMBER(wxBORDER_STATIC
)
4430 wxFLAGS_MEMBER(wxBORDER_NONE
)
4432 // old style border flags
4433 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
4434 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
4435 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
4436 wxFLAGS_MEMBER(wxRAISED_BORDER
)
4437 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
4438 wxFLAGS_MEMBER(wxBORDER
)
4440 // standard window styles
4441 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
4442 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
4443 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
4444 wxFLAGS_MEMBER(wxWANTS_CHARS
)
4445 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE
)
4446 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
4447 wxFLAGS_MEMBER(wxVSCROLL
)
4448 wxFLAGS_MEMBER(wxHSCROLL
)
4450 wxEND_FLAGS( wxGridStyle
)
4452 IMPLEMENT_DYNAMIC_CLASS_XTI(wxGrid
, wxScrolledWindow
,"wx/grid.h")
4454 wxBEGIN_PROPERTIES_TABLE(wxGrid
)
4455 wxHIDE_PROPERTY( Children
)
4456 wxPROPERTY_FLAGS( WindowStyle
, wxGridStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
4457 wxEND_PROPERTIES_TABLE()
4459 wxBEGIN_HANDLERS_TABLE(wxGrid
)
4460 wxEND_HANDLERS_TABLE()
4462 wxCONSTRUCTOR_5( wxGrid
, wxWindow
* , Parent
, wxWindowID
, Id
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
4465 TODO : Expose more information of a list's layout, etc. via appropriate objects (e.g., NotebookPageInfo)
4468 IMPLEMENT_DYNAMIC_CLASS( wxGrid
, wxScrolledWindow
)
4471 BEGIN_EVENT_TABLE( wxGrid
, wxScrolledWindow
)
4472 EVT_PAINT( wxGrid::OnPaint
)
4473 EVT_SIZE( wxGrid::OnSize
)
4474 EVT_KEY_DOWN( wxGrid::OnKeyDown
)
4475 EVT_KEY_UP( wxGrid::OnKeyUp
)
4476 EVT_CHAR ( wxGrid::OnChar
)
4477 EVT_ERASE_BACKGROUND( wxGrid::OnEraseBackground
)
4485 wxGrid::wxGrid( wxWindow
*parent
,
4490 const wxString
& name
)
4493 Create(parent
, id
, pos
, size
, style
, name
);
4496 bool wxGrid::Create(wxWindow
*parent
, wxWindowID id
,
4497 const wxPoint
& pos
, const wxSize
& size
,
4498 long style
, const wxString
& name
)
4500 if (!wxScrolledWindow::Create(parent
, id
, pos
, size
,
4501 style
| wxWANTS_CHARS
, name
))
4504 m_colMinWidths
= wxLongToLongHashMap(GRID_HASH_SIZE
);
4505 m_rowMinHeights
= wxLongToLongHashMap(GRID_HASH_SIZE
);
4508 SetInitialSize(size
);
4509 SetScrollRate(m_scrollLineX
, m_scrollLineY
);
4517 // Must do this or ~wxScrollHelper will pop the wrong event handler
4518 SetTargetWindow(this);
4520 wxSafeDecRef(m_defaultCellAttr
);
4522 #ifdef DEBUG_ATTR_CACHE
4523 size_t total
= gs_nAttrCacheHits
+ gs_nAttrCacheMisses
;
4524 wxPrintf(_T("wxGrid attribute cache statistics: "
4525 "total: %u, hits: %u (%u%%)\n"),
4526 total
, gs_nAttrCacheHits
,
4527 total
? (gs_nAttrCacheHits
*100) / total
: 0);
4530 // if we own the table, just delete it, otherwise at least don't leave it
4531 // with dangling view pointer
4534 else if ( m_table
&& m_table
->GetView() == this )
4535 m_table
->SetView(NULL
);
4537 delete m_typeRegistry
;
4542 // ----- internal init and update functions
4545 // NOTE: If using the default visual attributes works everywhere then this can
4546 // be removed as well as the #else cases below.
4547 #define _USE_VISATTR 0
4549 void wxGrid::Create()
4551 // create the type registry
4552 m_typeRegistry
= new wxGridTypeRegistry
;
4554 m_cellEditCtrlEnabled
= false;
4556 m_defaultCellAttr
= new wxGridCellAttr();
4558 // Set default cell attributes
4559 m_defaultCellAttr
->SetDefAttr(m_defaultCellAttr
);
4560 m_defaultCellAttr
->SetKind(wxGridCellAttr::Default
);
4561 m_defaultCellAttr
->SetFont(GetFont());
4562 m_defaultCellAttr
->SetAlignment(wxALIGN_LEFT
, wxALIGN_TOP
);
4563 m_defaultCellAttr
->SetRenderer(new wxGridCellStringRenderer
);
4564 m_defaultCellAttr
->SetEditor(new wxGridCellTextEditor
);
4567 wxVisualAttributes gva
= wxListBox::GetClassDefaultAttributes();
4568 wxVisualAttributes lva
= wxPanel::GetClassDefaultAttributes();
4570 m_defaultCellAttr
->SetTextColour(gva
.colFg
);
4571 m_defaultCellAttr
->SetBackgroundColour(gva
.colBg
);
4574 m_defaultCellAttr
->SetTextColour(
4575 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
4576 m_defaultCellAttr
->SetBackgroundColour(
4577 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
4582 m_currentCellCoords
= wxGridNoCellCoords
;
4584 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
4585 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
4587 // subwindow components that make up the wxGrid
4588 m_rowLabelWin
= new wxGridRowLabelWindow( this,
4593 m_colLabelWin
= new wxGridColLabelWindow( this,
4598 m_cornerLabelWin
= new wxGridCornerLabelWindow( this,
4603 m_gridWin
= new wxGridWindow( this,
4610 SetTargetWindow( m_gridWin
);
4613 wxColour gfg
= gva
.colFg
;
4614 wxColour gbg
= gva
.colBg
;
4615 wxColour lfg
= lva
.colFg
;
4616 wxColour lbg
= lva
.colBg
;
4618 wxColour gfg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT
);
4619 wxColour gbg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOW
);
4620 wxColour lfg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT
);
4621 wxColour lbg
= wxSystemSettings::GetColour( wxSYS_COLOUR_BTNFACE
);
4624 m_cornerLabelWin
->SetOwnForegroundColour(lfg
);
4625 m_cornerLabelWin
->SetOwnBackgroundColour(lbg
);
4626 m_rowLabelWin
->SetOwnForegroundColour(lfg
);
4627 m_rowLabelWin
->SetOwnBackgroundColour(lbg
);
4628 m_colLabelWin
->SetOwnForegroundColour(lfg
);
4629 m_colLabelWin
->SetOwnBackgroundColour(lbg
);
4631 m_gridWin
->SetOwnForegroundColour(gfg
);
4632 m_gridWin
->SetOwnBackgroundColour(gbg
);
4637 bool wxGrid::CreateGrid( int numRows
, int numCols
,
4638 wxGridSelectionModes selmode
)
4640 wxCHECK_MSG( !m_created
,
4642 wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
4644 return SetTable(new wxGridStringTable(numRows
, numCols
), true, selmode
);
4647 void wxGrid::SetSelectionMode(wxGridSelectionModes selmode
)
4649 wxCHECK_RET( m_created
,
4650 wxT("Called wxGrid::SetSelectionMode() before calling CreateGrid()") );
4652 m_selection
->SetSelectionMode( selmode
);
4655 wxGrid::wxGridSelectionModes
wxGrid::GetSelectionMode() const
4657 wxCHECK_MSG( m_created
, wxGridSelectCells
,
4658 wxT("Called wxGrid::GetSelectionMode() before calling CreateGrid()") );
4660 return m_selection
->GetSelectionMode();
4664 wxGrid::SetTable(wxGridTableBase
*table
,
4666 wxGrid::wxGridSelectionModes selmode
)
4668 bool checkSelection
= false;
4671 // stop all processing
4676 m_table
->SetView(0);
4688 checkSelection
= true;
4690 // kill row and column size arrays
4691 m_colWidths
.Empty();
4692 m_colRights
.Empty();
4693 m_rowHeights
.Empty();
4694 m_rowBottoms
.Empty();
4699 m_numRows
= table
->GetNumberRows();
4700 m_numCols
= table
->GetNumberCols();
4703 m_table
->SetView( this );
4704 m_ownTable
= takeOwnership
;
4705 m_selection
= new wxGridSelection( this, selmode
);
4708 // If the newly set table is smaller than the
4709 // original one current cell and selection regions
4710 // might be invalid,
4711 m_selectingKeyboard
= wxGridNoCellCoords
;
4712 m_currentCellCoords
=
4713 wxGridCellCoords(wxMin(m_numRows
, m_currentCellCoords
.GetRow()),
4714 wxMin(m_numCols
, m_currentCellCoords
.GetCol()));
4715 if (m_selectingTopLeft
.GetRow() >= m_numRows
||
4716 m_selectingTopLeft
.GetCol() >= m_numCols
)
4718 m_selectingTopLeft
= wxGridNoCellCoords
;
4719 m_selectingBottomRight
= wxGridNoCellCoords
;
4722 m_selectingBottomRight
=
4723 wxGridCellCoords(wxMin(m_numRows
,
4724 m_selectingBottomRight
.GetRow()),
4726 m_selectingBottomRight
.GetCol()));
4736 void wxGrid::InitVars()
4740 m_cornerLabelWin
= NULL
;
4741 m_rowLabelWin
= NULL
;
4742 m_colLabelWin
= NULL
;
4749 m_defaultCellAttr
= NULL
;
4750 m_typeRegistry
= NULL
;
4751 m_winCapture
= NULL
;
4756 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
4757 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
4759 if ( m_rowLabelWin
)
4761 m_labelBackgroundColour
= m_rowLabelWin
->GetBackgroundColour();
4765 m_labelBackgroundColour
= *wxWHITE
;
4768 m_labelTextColour
= *wxBLACK
;
4771 m_attrCache
.row
= -1;
4772 m_attrCache
.col
= -1;
4773 m_attrCache
.attr
= NULL
;
4775 m_labelFont
= GetFont();
4776 m_labelFont
.SetWeight( wxBOLD
);
4778 m_rowLabelHorizAlign
= wxALIGN_CENTRE
;
4779 m_rowLabelVertAlign
= wxALIGN_CENTRE
;
4781 m_colLabelHorizAlign
= wxALIGN_CENTRE
;
4782 m_colLabelVertAlign
= wxALIGN_CENTRE
;
4783 m_colLabelTextOrientation
= wxHORIZONTAL
;
4785 m_defaultColWidth
= WXGRID_DEFAULT_COL_WIDTH
;
4786 m_defaultRowHeight
= m_gridWin
->GetCharHeight();
4788 m_minAcceptableColWidth
= WXGRID_MIN_COL_WIDTH
;
4789 m_minAcceptableRowHeight
= WXGRID_MIN_ROW_HEIGHT
;
4791 #if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl()
4792 m_defaultRowHeight
+= 8;
4794 m_defaultRowHeight
+= 4;
4797 m_gridLineColour
= wxColour( 192,192,192 );
4798 m_gridLinesEnabled
= true;
4799 m_cellHighlightColour
= *wxBLACK
;
4800 m_cellHighlightPenWidth
= 2;
4801 m_cellHighlightROPenWidth
= 1;
4803 m_canDragColMove
= false;
4805 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
4806 m_winCapture
= NULL
;
4807 m_canDragRowSize
= true;
4808 m_canDragColSize
= true;
4809 m_canDragGridSize
= true;
4810 m_canDragCell
= false;
4812 m_dragRowOrCol
= -1;
4813 m_isDragging
= false;
4814 m_startDragPos
= wxDefaultPosition
;
4815 m_nativeColumnLabels
= false;
4817 m_waitForSlowClick
= false;
4819 m_rowResizeCursor
= wxCursor( wxCURSOR_SIZENS
);
4820 m_colResizeCursor
= wxCursor( wxCURSOR_SIZEWE
);
4822 m_currentCellCoords
= wxGridNoCellCoords
;
4826 m_selectionBackground
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
);
4827 m_selectionForeground
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
4829 m_editable
= true; // default for whole grid
4831 m_inOnKeyDown
= false;
4837 m_scrollLineX
= GRID_SCROLL_LINE_X
;
4838 m_scrollLineY
= GRID_SCROLL_LINE_Y
;
4841 // ----------------------------------------------------------------------------
4842 // the idea is to call these functions only when necessary because they create
4843 // quite big arrays which eat memory mostly unnecessary - in particular, if
4844 // default widths/heights are used for all rows/columns, we may not use these
4847 // with some extra code, it should be possible to only store the widths/heights
4848 // different from default ones (resulting in space savings for huge grids) but
4849 // this is not done currently
4850 // ----------------------------------------------------------------------------
4852 void wxGrid::InitRowHeights()
4854 m_rowHeights
.Empty();
4855 m_rowBottoms
.Empty();
4857 m_rowHeights
.Alloc( m_numRows
);
4858 m_rowBottoms
.Alloc( m_numRows
);
4860 m_rowHeights
.Add( m_defaultRowHeight
, m_numRows
);
4863 for ( int i
= 0; i
< m_numRows
; i
++ )
4865 rowBottom
+= m_defaultRowHeight
;
4866 m_rowBottoms
.Add( rowBottom
);
4870 void wxGrid::InitColWidths()
4872 m_colWidths
.Empty();
4873 m_colRights
.Empty();
4875 m_colWidths
.Alloc( m_numCols
);
4876 m_colRights
.Alloc( m_numCols
);
4878 m_colWidths
.Add( m_defaultColWidth
, m_numCols
);
4881 for ( int i
= 0; i
< m_numCols
; i
++ )
4883 colRight
= ( GetColPos( i
) + 1 ) * m_defaultColWidth
;
4884 m_colRights
.Add( colRight
);
4888 int wxGrid::GetColWidth(int col
) const
4890 return m_colWidths
.IsEmpty() ? m_defaultColWidth
: m_colWidths
[col
];
4893 int wxGrid::GetColLeft(int col
) const
4895 return m_colRights
.IsEmpty() ? GetColPos( col
) * m_defaultColWidth
4896 : m_colRights
[col
] - m_colWidths
[col
];
4899 int wxGrid::GetColRight(int col
) const
4901 return m_colRights
.IsEmpty() ? (GetColPos( col
) + 1) * m_defaultColWidth
4905 int wxGrid::GetRowHeight(int row
) const
4907 return m_rowHeights
.IsEmpty() ? m_defaultRowHeight
: m_rowHeights
[row
];
4910 int wxGrid::GetRowTop(int row
) const
4912 return m_rowBottoms
.IsEmpty() ? row
* m_defaultRowHeight
4913 : m_rowBottoms
[row
] - m_rowHeights
[row
];
4916 int wxGrid::GetRowBottom(int row
) const
4918 return m_rowBottoms
.IsEmpty() ? (row
+ 1) * m_defaultRowHeight
4919 : m_rowBottoms
[row
];
4922 void wxGrid::CalcDimensions()
4924 // compute the size of the scrollable area
4925 int w
= m_numCols
> 0 ? GetColRight(GetColAt(m_numCols
- 1)) : 0;
4926 int h
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) : 0;
4931 // take into account editor if shown
4932 if ( IsCellEditControlShown() )
4935 int r
= m_currentCellCoords
.GetRow();
4936 int c
= m_currentCellCoords
.GetCol();
4937 int x
= GetColLeft(c
);
4938 int y
= GetRowTop(r
);
4940 // how big is the editor
4941 wxGridCellAttr
* attr
= GetCellAttr(r
, c
);
4942 wxGridCellEditor
* editor
= attr
->GetEditor(this, r
, c
);
4943 editor
->GetControl()->GetSize(&w2
, &h2
);
4954 // preserve (more or less) the previous position
4956 GetViewStart( &x
, &y
);
4958 // ensure the position is valid for the new scroll ranges
4960 x
= wxMax( w
- 1, 0 );
4962 y
= wxMax( h
- 1, 0 );
4964 // update the virtual size and refresh the scrollbars to reflect it
4965 m_gridWin
->SetVirtualSize(w
, h
);
4969 // if our OnSize() hadn't been called (it would if we have scrollbars), we
4970 // still must reposition the children
4974 wxSize
wxGrid::GetSizeAvailableForScrollTarget(const wxSize
& size
)
4976 wxSize
sizeGridWin(size
);
4977 sizeGridWin
.x
-= m_rowLabelWidth
;
4978 sizeGridWin
.y
-= m_colLabelHeight
;
4983 void wxGrid::CalcWindowSizes()
4985 // escape if the window is has not been fully created yet
4987 if ( m_cornerLabelWin
== NULL
)
4991 GetClientSize( &cw
, &ch
);
4993 // the grid may be too small to have enough space for the labels yet, don't
4994 // size the windows to negative sizes in this case
4995 int gw
= cw
- m_rowLabelWidth
;
4996 int gh
= ch
- m_colLabelHeight
;
5002 if ( m_cornerLabelWin
&& m_cornerLabelWin
->IsShown() )
5003 m_cornerLabelWin
->SetSize( 0, 0, m_rowLabelWidth
, m_colLabelHeight
);
5005 if ( m_colLabelWin
&& m_colLabelWin
->IsShown() )
5006 m_colLabelWin
->SetSize( m_rowLabelWidth
, 0, gw
, m_colLabelHeight
);
5008 if ( m_rowLabelWin
&& m_rowLabelWin
->IsShown() )
5009 m_rowLabelWin
->SetSize( 0, m_colLabelHeight
, m_rowLabelWidth
, gh
);
5011 if ( m_gridWin
&& m_gridWin
->IsShown() )
5012 m_gridWin
->SetSize( m_rowLabelWidth
, m_colLabelHeight
, gw
, gh
);
5015 // this is called when the grid table sends a message
5016 // to indicate that it has been redimensioned
5018 bool wxGrid::Redimension( wxGridTableMessage
& msg
)
5021 bool result
= false;
5023 // Clear the attribute cache as the attribute might refer to a different
5024 // cell than stored in the cache after adding/removing rows/columns.
5027 // By the same reasoning, the editor should be dismissed if columns are
5028 // added or removed. And for consistency, it should IMHO always be
5029 // removed, not only if the cell "underneath" it actually changes.
5030 // For now, I intentionally do not save the editor's content as the
5031 // cell it might want to save that stuff to might no longer exist.
5032 HideCellEditControl();
5035 // if we were using the default widths/heights so far, we must change them
5037 if ( m_colWidths
.IsEmpty() )
5042 if ( m_rowHeights
.IsEmpty() )
5048 switch ( msg
.GetId() )
5050 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
5052 size_t pos
= msg
.GetCommandInt();
5053 int numRows
= msg
.GetCommandInt2();
5055 m_numRows
+= numRows
;
5057 if ( !m_rowHeights
.IsEmpty() )
5059 m_rowHeights
.Insert( m_defaultRowHeight
, pos
, numRows
);
5060 m_rowBottoms
.Insert( 0, pos
, numRows
);
5064 bottom
= m_rowBottoms
[pos
- 1];
5066 for ( i
= pos
; i
< m_numRows
; i
++ )
5068 bottom
+= m_rowHeights
[i
];
5069 m_rowBottoms
[i
] = bottom
;
5073 if ( m_currentCellCoords
== wxGridNoCellCoords
)
5075 // if we have just inserted cols into an empty grid the current
5076 // cell will be undefined...
5078 SetCurrentCell( 0, 0 );
5082 m_selection
->UpdateRows( pos
, numRows
);
5083 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
5085 attrProvider
->UpdateAttrRows( pos
, numRows
);
5087 if ( !GetBatchCount() )
5090 m_rowLabelWin
->Refresh();
5096 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
5098 int numRows
= msg
.GetCommandInt();
5099 int oldNumRows
= m_numRows
;
5100 m_numRows
+= numRows
;
5102 if ( !m_rowHeights
.IsEmpty() )
5104 m_rowHeights
.Add( m_defaultRowHeight
, numRows
);
5105 m_rowBottoms
.Add( 0, numRows
);
5108 if ( oldNumRows
> 0 )
5109 bottom
= m_rowBottoms
[oldNumRows
- 1];
5111 for ( i
= oldNumRows
; i
< m_numRows
; i
++ )
5113 bottom
+= m_rowHeights
[i
];
5114 m_rowBottoms
[i
] = bottom
;
5118 if ( m_currentCellCoords
== wxGridNoCellCoords
)
5120 // if we have just inserted cols into an empty grid the current
5121 // cell will be undefined...
5123 SetCurrentCell( 0, 0 );
5126 if ( !GetBatchCount() )
5129 m_rowLabelWin
->Refresh();
5135 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
5137 size_t pos
= msg
.GetCommandInt();
5138 int numRows
= msg
.GetCommandInt2();
5139 m_numRows
-= numRows
;
5141 if ( !m_rowHeights
.IsEmpty() )
5143 m_rowHeights
.RemoveAt( pos
, numRows
);
5144 m_rowBottoms
.RemoveAt( pos
, numRows
);
5147 for ( i
= 0; i
< m_numRows
; i
++ )
5149 h
+= m_rowHeights
[i
];
5150 m_rowBottoms
[i
] = h
;
5156 m_currentCellCoords
= wxGridNoCellCoords
;
5160 if ( m_currentCellCoords
.GetRow() >= m_numRows
)
5161 m_currentCellCoords
.Set( 0, 0 );
5165 m_selection
->UpdateRows( pos
, -((int)numRows
) );
5166 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
5169 attrProvider
->UpdateAttrRows( pos
, -((int)numRows
) );
5171 // ifdef'd out following patch from Paul Gammans
5173 // No need to touch column attributes, unless we
5174 // removed _all_ rows, in this case, we remove
5175 // all column attributes.
5176 // I hate to do this here, but the
5177 // needed data is not available inside UpdateAttrRows.
5178 if ( !GetNumberRows() )
5179 attrProvider
->UpdateAttrCols( 0, -GetNumberCols() );
5183 if ( !GetBatchCount() )
5186 m_rowLabelWin
->Refresh();
5192 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
5194 size_t pos
= msg
.GetCommandInt();
5195 int numCols
= msg
.GetCommandInt2();
5196 m_numCols
+= numCols
;
5198 if ( !m_colAt
.IsEmpty() )
5200 //Shift the column IDs
5202 for ( i
= 0; i
< m_numCols
- numCols
; i
++ )
5204 if ( m_colAt
[i
] >= (int)pos
)
5205 m_colAt
[i
] += numCols
;
5208 m_colAt
.Insert( pos
, pos
, numCols
);
5210 //Set the new columns' positions
5211 for ( i
= pos
+ 1; i
< (int)pos
+ numCols
; i
++ )
5217 if ( !m_colWidths
.IsEmpty() )
5219 m_colWidths
.Insert( m_defaultColWidth
, pos
, numCols
);
5220 m_colRights
.Insert( 0, pos
, numCols
);
5224 right
= m_colRights
[GetColAt( pos
- 1 )];
5227 for ( colPos
= pos
; colPos
< m_numCols
; colPos
++ )
5229 i
= GetColAt( colPos
);
5231 right
+= m_colWidths
[i
];
5232 m_colRights
[i
] = right
;
5236 if ( m_currentCellCoords
== wxGridNoCellCoords
)
5238 // if we have just inserted cols into an empty grid the current
5239 // cell will be undefined...
5241 SetCurrentCell( 0, 0 );
5245 m_selection
->UpdateCols( pos
, numCols
);
5246 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
5248 attrProvider
->UpdateAttrCols( pos
, numCols
);
5249 if ( !GetBatchCount() )
5252 m_colLabelWin
->Refresh();
5258 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
5260 int numCols
= msg
.GetCommandInt();
5261 int oldNumCols
= m_numCols
;
5262 m_numCols
+= numCols
;
5264 if ( !m_colAt
.IsEmpty() )
5266 m_colAt
.Add( 0, numCols
);
5268 //Set the new columns' positions
5270 for ( i
= oldNumCols
; i
< m_numCols
; i
++ )
5276 if ( !m_colWidths
.IsEmpty() )
5278 m_colWidths
.Add( m_defaultColWidth
, numCols
);
5279 m_colRights
.Add( 0, numCols
);
5282 if ( oldNumCols
> 0 )
5283 right
= m_colRights
[GetColAt( oldNumCols
- 1 )];
5286 for ( colPos
= oldNumCols
; colPos
< m_numCols
; colPos
++ )
5288 i
= GetColAt( colPos
);
5290 right
+= m_colWidths
[i
];
5291 m_colRights
[i
] = right
;
5295 if ( m_currentCellCoords
== wxGridNoCellCoords
)
5297 // if we have just inserted cols into an empty grid the current
5298 // cell will be undefined...
5300 SetCurrentCell( 0, 0 );
5302 if ( !GetBatchCount() )
5305 m_colLabelWin
->Refresh();
5311 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
5313 size_t pos
= msg
.GetCommandInt();
5314 int numCols
= msg
.GetCommandInt2();
5315 m_numCols
-= numCols
;
5317 if ( !m_colAt
.IsEmpty() )
5319 int colID
= GetColAt( pos
);
5321 m_colAt
.RemoveAt( pos
, numCols
);
5323 //Shift the column IDs
5325 for ( colPos
= 0; colPos
< m_numCols
; colPos
++ )
5327 if ( m_colAt
[colPos
] > colID
)
5328 m_colAt
[colPos
] -= numCols
;
5332 if ( !m_colWidths
.IsEmpty() )
5334 m_colWidths
.RemoveAt( pos
, numCols
);
5335 m_colRights
.RemoveAt( pos
, numCols
);
5339 for ( colPos
= 0; colPos
< m_numCols
; colPos
++ )
5341 i
= GetColAt( colPos
);
5343 w
+= m_colWidths
[i
];
5350 m_currentCellCoords
= wxGridNoCellCoords
;
5354 if ( m_currentCellCoords
.GetCol() >= m_numCols
)
5355 m_currentCellCoords
.Set( 0, 0 );
5359 m_selection
->UpdateCols( pos
, -((int)numCols
) );
5360 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
5363 attrProvider
->UpdateAttrCols( pos
, -((int)numCols
) );
5365 // ifdef'd out following patch from Paul Gammans
5367 // No need to touch row attributes, unless we
5368 // removed _all_ columns, in this case, we remove
5369 // all row attributes.
5370 // I hate to do this here, but the
5371 // needed data is not available inside UpdateAttrCols.
5372 if ( !GetNumberCols() )
5373 attrProvider
->UpdateAttrRows( 0, -GetNumberRows() );
5377 if ( !GetBatchCount() )
5380 m_colLabelWin
->Refresh();
5387 if (result
&& !GetBatchCount() )
5388 m_gridWin
->Refresh();
5393 wxArrayInt
wxGrid::CalcRowLabelsExposed( const wxRegion
& reg
) const
5395 wxRegionIterator
iter( reg
);
5398 wxArrayInt rowlabels
;
5405 // TODO: remove this when we can...
5406 // There is a bug in wxMotif that gives garbage update
5407 // rectangles if you jump-scroll a long way by clicking the
5408 // scrollbar with middle button. This is a work-around
5410 #if defined(__WXMOTIF__)
5412 m_gridWin
->GetClientSize( &cw
, &ch
);
5413 if ( r
.GetTop() > ch
)
5415 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
5418 // logical bounds of update region
5421 CalcUnscrolledPosition( 0, r
.GetTop(), &dummy
, &top
);
5422 CalcUnscrolledPosition( 0, r
.GetBottom(), &dummy
, &bottom
);
5424 // find the row labels within these bounds
5427 for ( row
= internalYToRow(top
); row
< m_numRows
; row
++ )
5429 if ( GetRowBottom(row
) < top
)
5432 if ( GetRowTop(row
) > bottom
)
5435 rowlabels
.Add( row
);
5444 wxArrayInt
wxGrid::CalcColLabelsExposed( const wxRegion
& reg
) const
5446 wxRegionIterator
iter( reg
);
5449 wxArrayInt colLabels
;
5456 // TODO: remove this when we can...
5457 // There is a bug in wxMotif that gives garbage update
5458 // rectangles if you jump-scroll a long way by clicking the
5459 // scrollbar with middle button. This is a work-around
5461 #if defined(__WXMOTIF__)
5463 m_gridWin
->GetClientSize( &cw
, &ch
);
5464 if ( r
.GetLeft() > cw
)
5466 r
.SetRight( wxMin( r
.GetRight(), cw
) );
5469 // logical bounds of update region
5472 CalcUnscrolledPosition( r
.GetLeft(), 0, &left
, &dummy
);
5473 CalcUnscrolledPosition( r
.GetRight(), 0, &right
, &dummy
);
5475 // find the cells within these bounds
5479 for ( colPos
= GetColPos( internalXToCol(left
) ); colPos
< m_numCols
; colPos
++ )
5481 col
= GetColAt( colPos
);
5483 if ( GetColRight(col
) < left
)
5486 if ( GetColLeft(col
) > right
)
5489 colLabels
.Add( col
);
5498 wxGridCellCoordsArray
wxGrid::CalcCellsExposed( const wxRegion
& reg
) const
5500 wxRegionIterator
iter( reg
);
5503 wxGridCellCoordsArray cellsExposed
;
5505 int left
, top
, right
, bottom
;
5510 // TODO: remove this when we can...
5511 // There is a bug in wxMotif that gives garbage update
5512 // rectangles if you jump-scroll a long way by clicking the
5513 // scrollbar with middle button. This is a work-around
5515 #if defined(__WXMOTIF__)
5517 m_gridWin
->GetClientSize( &cw
, &ch
);
5518 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
5519 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
5520 r
.SetRight( wxMin( r
.GetRight(), cw
) );
5521 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
5524 // logical bounds of update region
5526 CalcUnscrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
5527 CalcUnscrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
5529 // find the cells within these bounds
5532 for ( row
= internalYToRow(top
); row
< m_numRows
; row
++ )
5534 if ( GetRowBottom(row
) <= top
)
5537 if ( GetRowTop(row
) > bottom
)
5541 for ( colPos
= GetColPos( internalXToCol(left
) ); colPos
< m_numCols
; colPos
++ )
5543 col
= GetColAt( colPos
);
5545 if ( GetColRight(col
) <= left
)
5548 if ( GetColLeft(col
) > right
)
5551 cellsExposed
.Add( wxGridCellCoords( row
, col
) );
5558 return cellsExposed
;
5562 void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent
& event
)
5565 wxPoint
pos( event
.GetPosition() );
5566 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
5568 if ( event
.Dragging() )
5572 m_isDragging
= true;
5573 m_rowLabelWin
->CaptureMouse();
5576 if ( event
.LeftIsDown() )
5578 switch ( m_cursorMode
)
5580 case WXGRID_CURSOR_RESIZE_ROW
:
5582 int cw
, ch
, left
, dummy
;
5583 m_gridWin
->GetClientSize( &cw
, &ch
);
5584 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
5586 wxClientDC
dc( m_gridWin
);
5589 GetRowTop(m_dragRowOrCol
) +
5590 GetRowMinimalHeight(m_dragRowOrCol
) );
5591 dc
.SetLogicalFunction(wxINVERT
);
5592 if ( m_dragLastPos
>= 0 )
5594 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
5596 dc
.DrawLine( left
, y
, left
+cw
, y
);
5601 case WXGRID_CURSOR_SELECT_ROW
:
5603 if ( (row
= YToRow( y
)) >= 0 )
5607 m_selection
->SelectRow( row
,
5608 event
.ControlDown(),
5617 // default label to suppress warnings about "enumeration value
5618 // 'xxx' not handled in switch
5626 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
5631 if (m_rowLabelWin
->HasCapture())
5632 m_rowLabelWin
->ReleaseMouse();
5633 m_isDragging
= false;
5636 // ------------ Entering or leaving the window
5638 if ( event
.Entering() || event
.Leaving() )
5640 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
5643 // ------------ Left button pressed
5645 else if ( event
.LeftDown() )
5647 // don't send a label click event for a hit on the
5648 // edge of the row label - this is probably the user
5649 // wanting to resize the row
5651 if ( YToEdgeOfRow(y
) < 0 )
5655 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, row
, -1, event
) )
5657 if ( !event
.ShiftDown() && !event
.CmdDown() )
5661 if ( event
.ShiftDown() )
5663 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
5666 GetNumberCols() - 1,
5667 event
.ControlDown(),
5674 m_selection
->SelectRow( row
,
5675 event
.ControlDown(),
5682 ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW
, m_rowLabelWin
);
5687 // starting to drag-resize a row
5688 if ( CanDragRowSize() )
5689 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
);
5693 // ------------ Left double click
5695 else if (event
.LeftDClick() )
5697 row
= YToEdgeOfRow(y
);
5702 !SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, row
, -1, event
) )
5704 // no default action at the moment
5709 // adjust row height depending on label text
5710 AutoSizeRowLabelSize( row
);
5712 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5717 // ------------ Left button released
5719 else if ( event
.LeftUp() )
5721 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
5723 DoEndDragResizeRow();
5725 // Note: we are ending the event *after* doing
5726 // default processing in this case
5728 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
5731 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
5735 // ------------ Right button down
5737 else if ( event
.RightDown() )
5741 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, row
, -1, event
) )
5743 // no default action at the moment
5747 // ------------ Right double click
5749 else if ( event
.RightDClick() )
5753 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, row
, -1, event
) )
5755 // no default action at the moment
5759 // ------------ No buttons down and mouse moving
5761 else if ( event
.Moving() )
5763 m_dragRowOrCol
= YToEdgeOfRow( y
);
5764 if ( m_dragRowOrCol
>= 0 )
5766 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5768 // don't capture the mouse yet
5769 if ( CanDragRowSize() )
5770 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
, false);
5773 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5775 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
, false);
5780 void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent
& event
)
5783 wxPoint
pos( event
.GetPosition() );
5784 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
5786 if ( event
.Dragging() )
5790 m_isDragging
= true;
5791 m_colLabelWin
->CaptureMouse();
5793 if ( m_cursorMode
== WXGRID_CURSOR_MOVE_COL
)
5794 m_dragRowOrCol
= XToCol( x
);
5797 if ( event
.LeftIsDown() )
5799 switch ( m_cursorMode
)
5801 case WXGRID_CURSOR_RESIZE_COL
:
5803 int cw
, ch
, dummy
, top
;
5804 m_gridWin
->GetClientSize( &cw
, &ch
);
5805 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
5807 wxClientDC
dc( m_gridWin
);
5810 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
5811 GetColMinimalWidth(m_dragRowOrCol
));
5812 dc
.SetLogicalFunction(wxINVERT
);
5813 if ( m_dragLastPos
>= 0 )
5815 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ ch
);
5817 dc
.DrawLine( x
, top
, x
, top
+ ch
);
5822 case WXGRID_CURSOR_SELECT_COL
:
5824 if ( (col
= XToCol( x
)) >= 0 )
5828 m_selection
->SelectCol( col
,
5829 event
.ControlDown(),
5838 case WXGRID_CURSOR_MOVE_COL
:
5841 m_moveToCol
= GetColAt( 0 );
5843 m_moveToCol
= XToCol( x
);
5847 if ( m_moveToCol
< 0 )
5848 markerX
= GetColRight( GetColAt( m_numCols
- 1 ) );
5849 else if ( x
>= (GetColLeft( m_moveToCol
) + (GetColWidth(m_moveToCol
) / 2)) )
5851 m_moveToCol
= GetColAt( GetColPos( m_moveToCol
) + 1 );
5852 if ( m_moveToCol
< 0 )
5853 markerX
= GetColRight( GetColAt( m_numCols
- 1 ) );
5855 markerX
= GetColLeft( m_moveToCol
);
5858 markerX
= GetColLeft( m_moveToCol
);
5860 if ( markerX
!= m_dragLastPos
)
5862 wxClientDC
dc( m_colLabelWin
);
5866 m_colLabelWin
->GetClientSize( &cw
, &ch
);
5870 //Clean up the last indicator
5871 if ( m_dragLastPos
>= 0 )
5873 wxPen
pen( m_colLabelWin
->GetBackgroundColour(), 2 );
5875 dc
.DrawLine( m_dragLastPos
+ 1, 0, m_dragLastPos
+ 1, ch
);
5876 dc
.SetPen(wxNullPen
);
5878 if ( XToCol( m_dragLastPos
) != -1 )
5879 DrawColLabel( dc
, XToCol( m_dragLastPos
) );
5882 const wxColour
*color
;
5883 //Moving to the same place? Don't draw a marker
5884 if ( (m_moveToCol
== m_dragRowOrCol
)
5885 || (GetColPos( m_moveToCol
) == GetColPos( m_dragRowOrCol
) + 1)
5886 || (m_moveToCol
< 0 && m_dragRowOrCol
== GetColAt( m_numCols
- 1 )))
5887 color
= wxLIGHT_GREY
;
5892 wxPen
pen( *color
, 2 );
5895 dc
.DrawLine( markerX
, 0, markerX
, ch
);
5897 dc
.SetPen(wxNullPen
);
5899 m_dragLastPos
= markerX
- 1;
5904 // default label to suppress warnings about "enumeration value
5905 // 'xxx' not handled in switch
5913 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
5918 if (m_colLabelWin
->HasCapture())
5919 m_colLabelWin
->ReleaseMouse();
5920 m_isDragging
= false;
5923 // ------------ Entering or leaving the window
5925 if ( event
.Entering() || event
.Leaving() )
5927 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5930 // ------------ Left button pressed
5932 else if ( event
.LeftDown() )
5934 // don't send a label click event for a hit on the
5935 // edge of the col label - this is probably the user
5936 // wanting to resize the col
5938 if ( XToEdgeOfCol(x
) < 0 )
5942 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, col
, event
) )
5944 if ( m_canDragColMove
)
5946 //Show button as pressed
5947 wxClientDC
dc( m_colLabelWin
);
5948 int colLeft
= GetColLeft( col
);
5949 int colRight
= GetColRight( col
) - 1;
5950 dc
.SetPen( wxPen( m_colLabelWin
->GetBackgroundColour(), 1 ) );
5951 dc
.DrawLine( colLeft
, 1, colLeft
, m_colLabelHeight
-1 );
5952 dc
.DrawLine( colLeft
, 1, colRight
, 1 );
5954 ChangeCursorMode(WXGRID_CURSOR_MOVE_COL
, m_colLabelWin
);
5958 if ( !event
.ShiftDown() && !event
.CmdDown() )
5962 if ( event
.ShiftDown() )
5964 m_selection
->SelectBlock( 0,
5965 m_currentCellCoords
.GetCol(),
5966 GetNumberRows() - 1, col
,
5967 event
.ControlDown(),
5974 m_selection
->SelectCol( col
,
5975 event
.ControlDown(),
5982 ChangeCursorMode(WXGRID_CURSOR_SELECT_COL
, m_colLabelWin
);
5988 // starting to drag-resize a col
5990 if ( CanDragColSize() )
5991 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
);
5995 // ------------ Left double click
5997 if ( event
.LeftDClick() )
5999 col
= XToEdgeOfCol(x
);
6004 ! SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, col
, event
) )
6006 // no default action at the moment
6011 // adjust column width depending on label text
6012 AutoSizeColLabelSize( col
);
6014 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
6019 // ------------ Left button released
6021 else if ( event
.LeftUp() )
6023 switch ( m_cursorMode
)
6025 case WXGRID_CURSOR_RESIZE_COL
:
6026 DoEndDragResizeCol();
6028 // Note: we are ending the event *after* doing
6029 // default processing in this case
6031 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
6034 case WXGRID_CURSOR_MOVE_COL
:
6037 SendEvent( wxEVT_GRID_COL_MOVE
, -1, m_dragRowOrCol
, event
);
6040 case WXGRID_CURSOR_SELECT_COL
:
6041 case WXGRID_CURSOR_SELECT_CELL
:
6042 case WXGRID_CURSOR_RESIZE_ROW
:
6043 case WXGRID_CURSOR_SELECT_ROW
:
6044 // nothing to do (?)
6048 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
6052 // ------------ Right button down
6054 else if ( event
.RightDown() )
6058 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, col
, event
) )
6060 // no default action at the moment
6064 // ------------ Right double click
6066 else if ( event
.RightDClick() )
6070 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, col
, event
) )
6072 // no default action at the moment
6076 // ------------ No buttons down and mouse moving
6078 else if ( event
.Moving() )
6080 m_dragRowOrCol
= XToEdgeOfCol( x
);
6081 if ( m_dragRowOrCol
>= 0 )
6083 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
6085 // don't capture the cursor yet
6086 if ( CanDragColSize() )
6087 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
, false);
6090 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
6092 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
, false);
6097 void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent
& event
)
6099 if ( event
.LeftDown() )
6101 // indicate corner label by having both row and
6104 if ( !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, -1, event
) )
6109 else if ( event
.LeftDClick() )
6111 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, -1, event
);
6113 else if ( event
.RightDown() )
6115 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, -1, event
) )
6117 // no default action at the moment
6120 else if ( event
.RightDClick() )
6122 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, -1, event
) )
6124 // no default action at the moment
6129 void wxGrid::CancelMouseCapture()
6131 // cancel operation currently in progress, whatever it is
6134 m_isDragging
= false;
6135 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
6136 m_winCapture
->SetCursor( *wxSTANDARD_CURSOR
);
6137 m_winCapture
= NULL
;
6139 // remove traces of whatever we drew on screen
6144 void wxGrid::ChangeCursorMode(CursorMode mode
,
6149 static const wxChar
*cursorModes
[] =
6159 wxLogTrace(_T("grid"),
6160 _T("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
6161 win
== m_colLabelWin
? _T("colLabelWin")
6162 : win
? _T("rowLabelWin")
6164 cursorModes
[m_cursorMode
], cursorModes
[mode
]);
6167 if ( mode
== m_cursorMode
&&
6168 win
== m_winCapture
&&
6169 captureMouse
== (m_winCapture
!= NULL
))
6174 // by default use the grid itself
6180 if (m_winCapture
->HasCapture())
6181 m_winCapture
->ReleaseMouse();
6182 m_winCapture
= NULL
;
6185 m_cursorMode
= mode
;
6187 switch ( m_cursorMode
)
6189 case WXGRID_CURSOR_RESIZE_ROW
:
6190 win
->SetCursor( m_rowResizeCursor
);
6193 case WXGRID_CURSOR_RESIZE_COL
:
6194 win
->SetCursor( m_colResizeCursor
);
6197 case WXGRID_CURSOR_MOVE_COL
:
6198 win
->SetCursor( wxCursor(wxCURSOR_HAND
) );
6202 win
->SetCursor( *wxSTANDARD_CURSOR
);
6206 // we need to capture mouse when resizing
6207 bool resize
= m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
||
6208 m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
;
6210 if ( captureMouse
&& resize
)
6212 win
->CaptureMouse();
6217 void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent
& event
)
6220 wxPoint
pos( event
.GetPosition() );
6221 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
6223 wxGridCellCoords coords
;
6224 XYToCell( x
, y
, coords
);
6226 int cell_rows
, cell_cols
;
6227 bool isFirstDrag
= !m_isDragging
;
6228 GetCellSize( coords
.GetRow(), coords
.GetCol(), &cell_rows
, &cell_cols
);
6229 if ((cell_rows
< 0) || (cell_cols
< 0))
6231 coords
.SetRow(coords
.GetRow() + cell_rows
);
6232 coords
.SetCol(coords
.GetCol() + cell_cols
);
6235 if ( event
.Dragging() )
6237 //wxLogDebug("pos(%d, %d) coords(%d, %d)", pos.x, pos.y, coords.GetRow(), coords.GetCol());
6239 // Don't start doing anything until the mouse has been dragged at
6240 // least 3 pixels in any direction...
6243 if (m_startDragPos
== wxDefaultPosition
)
6245 m_startDragPos
= pos
;
6248 if (abs(m_startDragPos
.x
- pos
.x
) < 4 && abs(m_startDragPos
.y
- pos
.y
) < 4)
6252 m_isDragging
= true;
6253 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
6255 // Hide the edit control, so it
6256 // won't interfere with drag-shrinking.
6257 if ( IsCellEditControlShown() )
6259 HideCellEditControl();
6260 SaveEditControlValue();
6263 if ( coords
!= wxGridNoCellCoords
)
6265 if ( event
.CmdDown() )
6267 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
6268 m_selectingKeyboard
= coords
;
6269 HighlightBlock( m_selectingKeyboard
, coords
);
6271 else if ( CanDragCell() )
6275 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
6276 m_selectingKeyboard
= coords
;
6278 SendEvent( wxEVT_GRID_CELL_BEGIN_DRAG
,
6287 if ( !IsSelection() )
6289 HighlightBlock( coords
, coords
);
6293 HighlightBlock( m_currentCellCoords
, coords
);
6297 if (! IsVisible(coords
))
6299 MakeCellVisible(coords
);
6300 // TODO: need to introduce a delay or something here. The
6301 // scrolling is way too fast, at least under MSW and GTK.
6304 // Have we captured the mouse yet?
6307 m_winCapture
= m_gridWin
;
6308 m_winCapture
->CaptureMouse();
6313 else if ( event
.LeftIsDown() &&
6314 m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
6316 int cw
, ch
, left
, dummy
;
6317 m_gridWin
->GetClientSize( &cw
, &ch
);
6318 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
6320 wxClientDC
dc( m_gridWin
);
6322 y
= wxMax( y
, GetRowTop(m_dragRowOrCol
) +
6323 GetRowMinimalHeight(m_dragRowOrCol
) );
6324 dc
.SetLogicalFunction(wxINVERT
);
6325 if ( m_dragLastPos
>= 0 )
6327 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
6329 dc
.DrawLine( left
, y
, left
+cw
, y
);
6332 else if ( event
.LeftIsDown() &&
6333 m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
6335 int cw
, ch
, dummy
, top
;
6336 m_gridWin
->GetClientSize( &cw
, &ch
);
6337 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
6339 wxClientDC
dc( m_gridWin
);
6341 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
6342 GetColMinimalWidth(m_dragRowOrCol
) );
6343 dc
.SetLogicalFunction(wxINVERT
);
6344 if ( m_dragLastPos
>= 0 )
6346 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ ch
);
6348 dc
.DrawLine( x
, top
, x
, top
+ ch
);
6355 m_isDragging
= false;
6356 m_startDragPos
= wxDefaultPosition
;
6358 // VZ: if we do this, the mode is reset to WXGRID_CURSOR_SELECT_CELL
6359 // immediately after it becomes WXGRID_CURSOR_RESIZE_ROW/COL under
6362 if ( event
.Entering() || event
.Leaving() )
6364 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6365 m_gridWin
->SetCursor( *wxSTANDARD_CURSOR
);
6370 // ------------ Left button pressed
6372 if ( event
.LeftDown() && coords
!= wxGridNoCellCoords
)
6374 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_CLICK
,
6379 if ( !event
.CmdDown() )
6381 if ( event
.ShiftDown() )
6385 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
6386 m_currentCellCoords
.GetCol(),
6389 event
.ControlDown(),
6395 else if ( XToEdgeOfCol(x
) < 0 &&
6396 YToEdgeOfRow(y
) < 0 )
6398 DisableCellEditControl();
6399 MakeCellVisible( coords
);
6401 if ( event
.CmdDown() )
6405 m_selection
->ToggleCellSelection( coords
.GetRow(),
6407 event
.ControlDown(),
6412 m_selectingTopLeft
= wxGridNoCellCoords
;
6413 m_selectingBottomRight
= wxGridNoCellCoords
;
6414 m_selectingKeyboard
= coords
;
6418 m_waitForSlowClick
= m_currentCellCoords
== coords
&&
6419 coords
!= wxGridNoCellCoords
;
6420 SetCurrentCell( coords
);
6426 // ------------ Left double click
6428 else if ( event
.LeftDClick() && coords
!= wxGridNoCellCoords
)
6430 DisableCellEditControl();
6432 if ( XToEdgeOfCol(x
) < 0 && YToEdgeOfRow(y
) < 0 )
6434 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_DCLICK
,
6439 // we want double click to select a cell and start editing
6440 // (i.e. to behave in same way as sequence of two slow clicks):
6441 m_waitForSlowClick
= true;
6446 // ------------ Left button released
6448 else if ( event
.LeftUp() )
6450 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
6454 if (m_winCapture
->HasCapture())
6455 m_winCapture
->ReleaseMouse();
6456 m_winCapture
= NULL
;
6459 if ( coords
== m_currentCellCoords
&& m_waitForSlowClick
&& CanEnableCellControl() )
6462 EnableCellEditControl();
6464 wxGridCellAttr
*attr
= GetCellAttr(coords
);
6465 wxGridCellEditor
*editor
= attr
->GetEditor(this, coords
.GetRow(), coords
.GetCol());
6466 editor
->StartingClick();
6470 m_waitForSlowClick
= false;
6472 else if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
6473 m_selectingBottomRight
!= wxGridNoCellCoords
)
6477 m_selection
->SelectBlock( m_selectingTopLeft
.GetRow(),
6478 m_selectingTopLeft
.GetCol(),
6479 m_selectingBottomRight
.GetRow(),
6480 m_selectingBottomRight
.GetCol(),
6481 event
.ControlDown(),
6487 m_selectingTopLeft
= wxGridNoCellCoords
;
6488 m_selectingBottomRight
= wxGridNoCellCoords
;
6490 // Show the edit control, if it has been hidden for
6492 ShowCellEditControl();
6495 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
6497 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6498 DoEndDragResizeRow();
6500 // Note: we are ending the event *after* doing
6501 // default processing in this case
6503 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
6505 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
6507 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6508 DoEndDragResizeCol();
6510 // Note: we are ending the event *after* doing
6511 // default processing in this case
6513 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
6519 // ------------ Right button down
6521 else if ( event
.RightDown() && coords
!= wxGridNoCellCoords
)
6523 DisableCellEditControl();
6524 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_CLICK
,
6529 // no default action at the moment
6533 // ------------ Right double click
6535 else if ( event
.RightDClick() && coords
!= wxGridNoCellCoords
)
6537 DisableCellEditControl();
6538 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_DCLICK
,
6543 // no default action at the moment
6547 // ------------ Moving and no button action
6549 else if ( event
.Moving() && !event
.IsButton() )
6551 if ( coords
.GetRow() < 0 || coords
.GetCol() < 0 )
6553 // out of grid cell area
6554 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6558 int dragRow
= YToEdgeOfRow( y
);
6559 int dragCol
= XToEdgeOfCol( x
);
6561 // Dragging on the corner of a cell to resize in both
6562 // directions is not implemented yet...
6564 if ( dragRow
>= 0 && dragCol
>= 0 )
6566 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6572 m_dragRowOrCol
= dragRow
;
6574 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
6576 if ( CanDragRowSize() && CanDragGridSize() )
6577 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, NULL
, false);
6580 else if ( dragCol
>= 0 )
6582 m_dragRowOrCol
= dragCol
;
6584 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
6586 if ( CanDragColSize() && CanDragGridSize() )
6587 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, NULL
, false);
6590 else // Neither on a row or col edge
6592 if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
6594 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6600 void wxGrid::DoEndDragResizeLine(const wxGridOperations
& oper
)
6602 if ( m_dragLastPos
== -1 )
6605 const wxGridOperations
& doper
= oper
.Dual();
6607 const wxSize size
= m_gridWin
->GetClientSize();
6609 const wxPoint ptOrigin
= CalcUnscrolledPosition(wxPoint(0, 0));
6611 // erase the last line we drew
6612 wxClientDC
dc(m_gridWin
);
6614 dc
.SetLogicalFunction(wxINVERT
);
6616 const int posLineStart
= oper
.Select(ptOrigin
);
6617 const int posLineEnd
= oper
.Select(ptOrigin
) + oper
.Select(size
);
6619 oper
.DrawParallelLine(dc
, posLineStart
, posLineEnd
, m_dragLastPos
);
6621 // temporarily hide the edit control before resizing
6622 HideCellEditControl();
6623 SaveEditControlValue();
6625 // do resize the line
6626 const int lineStart
= oper
.GetLineStartPos(this, m_dragRowOrCol
);
6627 oper
.SetLineSize(this, m_dragRowOrCol
,
6628 wxMax(m_dragLastPos
- lineStart
,
6629 oper
.GetMinimalLineSize(this, m_dragRowOrCol
)));
6631 // refresh now if we're not frozen
6632 if ( !GetBatchCount() )
6634 // we need to refresh everything beyond the resized line in the header
6637 // get the position from which to refresh in the other direction
6638 wxRect
rect(CellToRect(oper
.MakeCoords(m_dragRowOrCol
, 0)));
6639 rect
.SetPosition(CalcScrolledPosition(rect
.GetPosition()));
6641 // we only need the ordinate (for rows) or abscissa (for columns) here,
6642 // and need to cover the entire window in the other direction
6643 oper
.Select(rect
) = 0;
6645 wxRect
rectHeader(rect
.GetPosition(),
6648 oper
.GetHeaderWindowSize(this),
6649 doper
.Select(size
) - doper
.Select(rect
)
6652 oper
.GetHeaderWindow(this)->Refresh(true, &rectHeader
);
6655 // also refresh the grid window: extend the rectangle
6658 oper
.SelectSize(rect
) = oper
.Select(size
);
6660 int subtractLines
= 0;
6661 const int lineStart
= oper
.PosToLine(this, posLineStart
);
6662 if ( lineStart
>= 0 )
6664 // ensure that if we have a multi-cell block we redraw all of
6665 // it by increasing the refresh area to cover it entirely if a
6666 // part of it is affected
6667 const int lineEnd
= oper
.PosToLine(this, posLineEnd
, true);
6668 for ( int line
= lineStart
; line
< lineEnd
; line
++ )
6670 int cellLines
= oper
.Select(
6671 GetCellSize(oper
.MakeCoords(m_dragRowOrCol
, line
)));
6672 if ( cellLines
< subtractLines
)
6673 subtractLines
= cellLines
;
6678 oper
.GetLineStartPos(this, m_dragRowOrCol
+ subtractLines
);
6679 startPos
= doper
.CalcScrolledPosition(this, startPos
);
6681 doper
.Select(rect
) = startPos
;
6682 doper
.SelectSize(rect
) = doper
.Select(size
) - startPos
;
6684 m_gridWin
->Refresh(false, &rect
);
6688 // show the edit control back again
6689 ShowCellEditControl();
6692 void wxGrid::DoEndDragResizeRow()
6694 DoEndDragResizeLine(wxGridRowOperations());
6697 void wxGrid::DoEndDragResizeCol()
6699 DoEndDragResizeLine(wxGridColumnOperations());
6702 void wxGrid::DoEndDragMoveCol()
6704 //The user clicked on the column but didn't actually drag
6705 if ( m_dragLastPos
< 0 )
6707 m_colLabelWin
->Refresh(); //Do this to "unpress" the column
6712 if ( m_moveToCol
== -1 )
6713 newPos
= m_numCols
- 1;
6716 newPos
= GetColPos( m_moveToCol
);
6717 if ( newPos
> GetColPos( m_dragRowOrCol
) )
6721 SetColPos( m_dragRowOrCol
, newPos
);
6724 void wxGrid::SetColPos( int colID
, int newPos
)
6726 if ( m_colAt
.IsEmpty() )
6728 m_colAt
.Alloc( m_numCols
);
6731 for ( i
= 0; i
< m_numCols
; i
++ )
6737 int oldPos
= GetColPos( colID
);
6739 //Reshuffle the m_colAt array
6740 if ( newPos
> oldPos
)
6743 for ( i
= oldPos
; i
< newPos
; i
++ )
6745 m_colAt
[i
] = m_colAt
[i
+1];
6751 for ( i
= oldPos
; i
> newPos
; i
-- )
6753 m_colAt
[i
] = m_colAt
[i
-1];
6757 m_colAt
[newPos
] = colID
;
6759 //Recalculate the column rights
6760 if ( !m_colWidths
.IsEmpty() )
6764 for ( colPos
= 0; colPos
< m_numCols
; colPos
++ )
6766 int colID
= GetColAt( colPos
);
6768 colRight
+= m_colWidths
[colID
];
6769 m_colRights
[colID
] = colRight
;
6773 m_colLabelWin
->Refresh();
6774 m_gridWin
->Refresh();
6779 void wxGrid::EnableDragColMove( bool enable
)
6781 if ( m_canDragColMove
== enable
)
6784 m_canDragColMove
= enable
;
6786 if ( !m_canDragColMove
)
6790 //Recalculate the column rights
6791 if ( !m_colWidths
.IsEmpty() )
6795 for ( colPos
= 0; colPos
< m_numCols
; colPos
++ )
6797 colRight
+= m_colWidths
[colPos
];
6798 m_colRights
[colPos
] = colRight
;
6802 m_colLabelWin
->Refresh();
6803 m_gridWin
->Refresh();
6809 // ------ interaction with data model
6811 bool wxGrid::ProcessTableMessage( wxGridTableMessage
& msg
)
6813 switch ( msg
.GetId() )
6815 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES
:
6816 return GetModelValues();
6818 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES
:
6819 return SetModelValues();
6821 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
6822 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
6823 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
6824 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
6825 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
6826 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
6827 return Redimension( msg
);
6834 // The behaviour of this function depends on the grid table class
6835 // Clear() function. For the default wxGridStringTable class the
6836 // behaviour is to replace all cell contents with wxEmptyString but
6837 // not to change the number of rows or cols.
6839 void wxGrid::ClearGrid()
6843 if (IsCellEditControlEnabled())
6844 DisableCellEditControl();
6847 if (!GetBatchCount())
6848 m_gridWin
->Refresh();
6853 wxGrid::DoModifyLines(bool (wxGridTableBase::*funcModify
)(size_t, size_t),
6854 int pos
, int num
, bool WXUNUSED(updateLabels
) )
6856 wxCHECK_MSG( m_created
, false, "must finish creating the grid first" );
6861 if ( IsCellEditControlEnabled() )
6862 DisableCellEditControl();
6864 return (m_table
->*funcModify
)(pos
, num
);
6866 // the table will have sent the results of the insert row
6867 // operation to this view object as a grid table message
6871 wxGrid::DoAppendLines(bool (wxGridTableBase::*funcAppend
)(size_t),
6872 int num
, bool WXUNUSED(updateLabels
))
6874 wxCHECK_MSG( m_created
, false, "must finish creating the grid first" );
6879 return (m_table
->*funcAppend
)(num
);
6883 // ----- event handlers
6886 // Generate a grid event based on a mouse event and
6887 // return the result of ProcessEvent()
6889 int wxGrid::SendEvent( const wxEventType type
,
6891 wxMouseEvent
& mouseEv
)
6893 bool claimed
, vetoed
;
6895 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
6897 int rowOrCol
= (row
== -1 ? col
: row
);
6899 wxGridSizeEvent
gridEvt( GetId(),
6903 mouseEv
.GetX() + GetRowLabelSize(),
6904 mouseEv
.GetY() + GetColLabelSize(),
6905 mouseEv
.ControlDown(),
6906 mouseEv
.ShiftDown(),
6908 mouseEv
.MetaDown() );
6910 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6911 vetoed
= !gridEvt
.IsAllowed();
6913 else if ( type
== wxEVT_GRID_RANGE_SELECT
)
6915 // Right now, it should _never_ end up here!
6916 wxGridRangeSelectEvent
gridEvt( GetId(),
6920 m_selectingBottomRight
,
6922 mouseEv
.ControlDown(),
6923 mouseEv
.ShiftDown(),
6925 mouseEv
.MetaDown() );
6927 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6928 vetoed
= !gridEvt
.IsAllowed();
6930 else if ( type
== wxEVT_GRID_LABEL_LEFT_CLICK
||
6931 type
== wxEVT_GRID_LABEL_LEFT_DCLICK
||
6932 type
== wxEVT_GRID_LABEL_RIGHT_CLICK
||
6933 type
== wxEVT_GRID_LABEL_RIGHT_DCLICK
)
6935 wxPoint pos
= mouseEv
.GetPosition();
6937 if ( mouseEv
.GetEventObject() == GetGridRowLabelWindow() )
6938 pos
.y
+= GetColLabelSize();
6939 if ( mouseEv
.GetEventObject() == GetGridColLabelWindow() )
6940 pos
.x
+= GetRowLabelSize();
6942 wxGridEvent
gridEvt( GetId(),
6949 mouseEv
.ControlDown(),
6950 mouseEv
.ShiftDown(),
6952 mouseEv
.MetaDown() );
6953 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6954 vetoed
= !gridEvt
.IsAllowed();
6958 wxGridEvent
gridEvt( GetId(),
6962 mouseEv
.GetX() + GetRowLabelSize(),
6963 mouseEv
.GetY() + GetColLabelSize(),
6965 mouseEv
.ControlDown(),
6966 mouseEv
.ShiftDown(),
6968 mouseEv
.MetaDown() );
6969 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6970 vetoed
= !gridEvt
.IsAllowed();
6973 // A Veto'd event may not be `claimed' so test this first
6977 return claimed
? 1 : 0;
6980 // Generate a grid event of specified type and return the result
6981 // of ProcessEvent().
6983 int wxGrid::SendEvent( const wxEventType type
,
6986 bool claimed
, vetoed
;
6988 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
6990 int rowOrCol
= (row
== -1 ? col
: row
);
6992 wxGridSizeEvent
gridEvt( GetId(), type
, this, rowOrCol
);
6994 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6995 vetoed
= !gridEvt
.IsAllowed();
6999 wxGridEvent
gridEvt( GetId(), type
, this, row
, col
);
7001 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
7002 vetoed
= !gridEvt
.IsAllowed();
7005 // A Veto'd event may not be `claimed' so test this first
7009 return claimed
? 1 : 0;
7012 void wxGrid::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
7014 // needed to prevent zillions of paint events on MSW
7018 void wxGrid::Refresh(bool eraseb
, const wxRect
* rect
)
7020 // Don't do anything if between Begin/EndBatch...
7021 // EndBatch() will do all this on the last nested one anyway.
7022 if ( m_created
&& !GetBatchCount() )
7024 // Refresh to get correct scrolled position:
7025 wxScrolledWindow::Refresh(eraseb
, rect
);
7029 int rect_x
, rect_y
, rectWidth
, rectHeight
;
7030 int width_label
, width_cell
, height_label
, height_cell
;
7033 // Copy rectangle can get scroll offsets..
7034 rect_x
= rect
->GetX();
7035 rect_y
= rect
->GetY();
7036 rectWidth
= rect
->GetWidth();
7037 rectHeight
= rect
->GetHeight();
7039 width_label
= m_rowLabelWidth
- rect_x
;
7040 if (width_label
> rectWidth
)
7041 width_label
= rectWidth
;
7043 height_label
= m_colLabelHeight
- rect_y
;
7044 if (height_label
> rectHeight
)
7045 height_label
= rectHeight
;
7047 if (rect_x
> m_rowLabelWidth
)
7049 x
= rect_x
- m_rowLabelWidth
;
7050 width_cell
= rectWidth
;
7055 width_cell
= rectWidth
- (m_rowLabelWidth
- rect_x
);
7058 if (rect_y
> m_colLabelHeight
)
7060 y
= rect_y
- m_colLabelHeight
;
7061 height_cell
= rectHeight
;
7066 height_cell
= rectHeight
- (m_colLabelHeight
- rect_y
);
7069 // Paint corner label part intersecting rect.
7070 if ( width_label
> 0 && height_label
> 0 )
7072 wxRect
anotherrect(rect_x
, rect_y
, width_label
, height_label
);
7073 m_cornerLabelWin
->Refresh(eraseb
, &anotherrect
);
7076 // Paint col labels part intersecting rect.
7077 if ( width_cell
> 0 && height_label
> 0 )
7079 wxRect
anotherrect(x
, rect_y
, width_cell
, height_label
);
7080 m_colLabelWin
->Refresh(eraseb
, &anotherrect
);
7083 // Paint row labels part intersecting rect.
7084 if ( width_label
> 0 && height_cell
> 0 )
7086 wxRect
anotherrect(rect_x
, y
, width_label
, height_cell
);
7087 m_rowLabelWin
->Refresh(eraseb
, &anotherrect
);
7090 // Paint cell area part intersecting rect.
7091 if ( width_cell
> 0 && height_cell
> 0 )
7093 wxRect
anotherrect(x
, y
, width_cell
, height_cell
);
7094 m_gridWin
->Refresh(eraseb
, &anotherrect
);
7099 m_cornerLabelWin
->Refresh(eraseb
, NULL
);
7100 m_colLabelWin
->Refresh(eraseb
, NULL
);
7101 m_rowLabelWin
->Refresh(eraseb
, NULL
);
7102 m_gridWin
->Refresh(eraseb
, NULL
);
7107 void wxGrid::OnSize(wxSizeEvent
& WXUNUSED(event
))
7109 if (m_targetWindow
!= this) // check whether initialisation has been done
7111 // reposition our children windows
7116 void wxGrid::OnKeyDown( wxKeyEvent
& event
)
7118 if ( m_inOnKeyDown
)
7120 // shouldn't be here - we are going round in circles...
7122 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") );
7125 m_inOnKeyDown
= true;
7127 // propagate the event up and see if it gets processed
7128 wxWindow
*parent
= GetParent();
7129 wxKeyEvent
keyEvt( event
);
7130 keyEvt
.SetEventObject( parent
);
7132 if ( !parent
->GetEventHandler()->ProcessEvent( keyEvt
) )
7134 if (GetLayoutDirection() == wxLayout_RightToLeft
)
7136 if (event
.GetKeyCode() == WXK_RIGHT
)
7137 event
.m_keyCode
= WXK_LEFT
;
7138 else if (event
.GetKeyCode() == WXK_LEFT
)
7139 event
.m_keyCode
= WXK_RIGHT
;
7142 // try local handlers
7143 switch ( event
.GetKeyCode() )
7146 if ( event
.ControlDown() )
7147 MoveCursorUpBlock( event
.ShiftDown() );
7149 MoveCursorUp( event
.ShiftDown() );
7153 if ( event
.ControlDown() )
7154 MoveCursorDownBlock( event
.ShiftDown() );
7156 MoveCursorDown( event
.ShiftDown() );
7160 if ( event
.ControlDown() )
7161 MoveCursorLeftBlock( event
.ShiftDown() );
7163 MoveCursorLeft( event
.ShiftDown() );
7167 if ( event
.ControlDown() )
7168 MoveCursorRightBlock( event
.ShiftDown() );
7170 MoveCursorRight( event
.ShiftDown() );
7174 case WXK_NUMPAD_ENTER
:
7175 if ( event
.ControlDown() )
7177 event
.Skip(); // to let the edit control have the return
7181 if ( GetGridCursorRow() < GetNumberRows()-1 )
7183 MoveCursorDown( event
.ShiftDown() );
7187 // at the bottom of a column
7188 DisableCellEditControl();
7198 if (event
.ShiftDown())
7200 if ( GetGridCursorCol() > 0 )
7202 MoveCursorLeft( false );
7207 DisableCellEditControl();
7212 if ( GetGridCursorCol() < GetNumberCols() - 1 )
7214 MoveCursorRight( false );
7219 DisableCellEditControl();
7225 if ( event
.ControlDown() )
7227 MakeCellVisible( 0, 0 );
7228 SetCurrentCell( 0, 0 );
7237 if ( event
.ControlDown() )
7239 MakeCellVisible( m_numRows
- 1, m_numCols
- 1 );
7240 SetCurrentCell( m_numRows
- 1, m_numCols
- 1 );
7257 // Ctrl-Space selects the current column, Shift-Space -- the
7258 // current row and Ctrl-Shift-Space -- everything
7259 switch ( m_selection
? event
.GetModifiers() : wxMOD_NONE
)
7262 m_selection
->SelectCol(m_currentCellCoords
.GetCol());
7266 m_selection
->SelectRow(m_currentCellCoords
.GetRow());
7269 case wxMOD_CONTROL
| wxMOD_SHIFT
:
7270 m_selection
->SelectBlock(0, 0,
7271 m_numRows
- 1, m_numCols
- 1);
7275 if ( !IsEditable() )
7277 MoveCursorRight(false);
7280 //else: fall through
7293 m_inOnKeyDown
= false;
7296 void wxGrid::OnKeyUp( wxKeyEvent
& event
)
7298 // try local handlers
7300 if ( event
.GetKeyCode() == WXK_SHIFT
)
7302 if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
7303 m_selectingBottomRight
!= wxGridNoCellCoords
)
7307 m_selection
->SelectBlock(
7308 m_selectingTopLeft
.GetRow(),
7309 m_selectingTopLeft
.GetCol(),
7310 m_selectingBottomRight
.GetRow(),
7311 m_selectingBottomRight
.GetCol(),
7312 event
.ControlDown(),
7319 m_selectingTopLeft
= wxGridNoCellCoords
;
7320 m_selectingBottomRight
= wxGridNoCellCoords
;
7321 m_selectingKeyboard
= wxGridNoCellCoords
;
7325 void wxGrid::OnChar( wxKeyEvent
& event
)
7327 // is it possible to edit the current cell at all?
7328 if ( !IsCellEditControlEnabled() && CanEnableCellControl() )
7330 // yes, now check whether the cells editor accepts the key
7331 int row
= m_currentCellCoords
.GetRow();
7332 int col
= m_currentCellCoords
.GetCol();
7333 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7334 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
7336 // <F2> is special and will always start editing, for
7337 // other keys - ask the editor itself
7338 if ( (event
.GetKeyCode() == WXK_F2
&& !event
.HasModifiers())
7339 || editor
->IsAcceptedKey(event
) )
7341 // ensure cell is visble
7342 MakeCellVisible(row
, col
);
7343 EnableCellEditControl();
7345 // a problem can arise if the cell is not completely
7346 // visible (even after calling MakeCellVisible the
7347 // control is not created and calling StartingKey will
7349 if ( event
.GetKeyCode() != WXK_F2
&& editor
->IsCreated() && m_cellEditCtrlEnabled
)
7350 editor
->StartingKey(event
);
7366 void wxGrid::OnEraseBackground(wxEraseEvent
&)
7370 void wxGrid::SetCurrentCell( const wxGridCellCoords
& coords
)
7372 if ( SendEvent( wxEVT_GRID_SELECT_CELL
, coords
.GetRow(), coords
.GetCol() ) )
7374 // the event has been intercepted - do nothing
7378 #if !defined(__WXMAC__)
7379 wxClientDC
dc( m_gridWin
);
7383 if ( m_currentCellCoords
!= wxGridNoCellCoords
)
7385 DisableCellEditControl();
7387 if ( IsVisible( m_currentCellCoords
, false ) )
7390 r
= BlockToDeviceRect( m_currentCellCoords
, m_currentCellCoords
);
7391 if ( !m_gridLinesEnabled
)
7399 wxGridCellCoordsArray cells
= CalcCellsExposed( r
);
7401 // Otherwise refresh redraws the highlight!
7402 m_currentCellCoords
= coords
;
7404 #if defined(__WXMAC__)
7405 m_gridWin
->Refresh(true /*, & r */);
7407 DrawGridCellArea( dc
, cells
);
7408 DrawAllGridLines( dc
, r
);
7413 m_currentCellCoords
= coords
;
7415 wxGridCellAttr
*attr
= GetCellAttr( coords
);
7416 #if !defined(__WXMAC__)
7417 DrawCellHighlight( dc
, attr
);
7422 void wxGrid::HighlightBlock(int topRow
, int leftCol
, int bottomRow
, int rightCol
)
7424 wxGridCellCoords updateTopLeft
, updateBottomRight
;
7428 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows
)
7431 rightCol
= GetNumberCols() - 1;
7433 else if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns
)
7436 bottomRow
= GetNumberRows() - 1;
7440 EnsureFirstLessThanSecond(topRow
, bottomRow
);
7441 EnsureFirstLessThanSecond(leftCol
, rightCol
);
7443 updateTopLeft
= wxGridCellCoords( topRow
, leftCol
);
7444 updateBottomRight
= wxGridCellCoords( bottomRow
, rightCol
);
7446 // First the case that we selected a completely new area
7447 if ( m_selectingTopLeft
== wxGridNoCellCoords
||
7448 m_selectingBottomRight
== wxGridNoCellCoords
)
7451 rect
= BlockToDeviceRect( wxGridCellCoords ( topRow
, leftCol
),
7452 wxGridCellCoords ( bottomRow
, rightCol
) );
7453 m_gridWin
->Refresh( false, &rect
);
7456 // Now handle changing an existing selection area.
7457 else if ( m_selectingTopLeft
!= updateTopLeft
||
7458 m_selectingBottomRight
!= updateBottomRight
)
7460 // Compute two optimal update rectangles:
7461 // Either one rectangle is a real subset of the
7462 // other, or they are (almost) disjoint!
7464 bool need_refresh
[4];
7468 need_refresh
[3] = false;
7471 // Store intermediate values
7472 wxCoord oldLeft
= m_selectingTopLeft
.GetCol();
7473 wxCoord oldTop
= m_selectingTopLeft
.GetRow();
7474 wxCoord oldRight
= m_selectingBottomRight
.GetCol();
7475 wxCoord oldBottom
= m_selectingBottomRight
.GetRow();
7477 // Determine the outer/inner coordinates.
7478 EnsureFirstLessThanSecond(oldLeft
, leftCol
);
7479 EnsureFirstLessThanSecond(oldTop
, topRow
);
7480 EnsureFirstLessThanSecond(rightCol
, oldRight
);
7481 EnsureFirstLessThanSecond(bottomRow
, oldBottom
);
7483 // Now, either the stuff marked old is the outer
7484 // rectangle or we don't have a situation where one
7485 // is contained in the other.
7487 if ( oldLeft
< leftCol
)
7489 // Refresh the newly selected or deselected
7490 // area to the left of the old or new selection.
7491 need_refresh
[0] = true;
7492 rect
[0] = BlockToDeviceRect(
7493 wxGridCellCoords( oldTop
, oldLeft
),
7494 wxGridCellCoords( oldBottom
, leftCol
- 1 ) );
7497 if ( oldTop
< topRow
)
7499 // Refresh the newly selected or deselected
7500 // area above the old or new selection.
7501 need_refresh
[1] = true;
7502 rect
[1] = BlockToDeviceRect(
7503 wxGridCellCoords( oldTop
, leftCol
),
7504 wxGridCellCoords( topRow
- 1, rightCol
) );
7507 if ( oldRight
> rightCol
)
7509 // Refresh the newly selected or deselected
7510 // area to the right of the old or new selection.
7511 need_refresh
[2] = true;
7512 rect
[2] = BlockToDeviceRect(
7513 wxGridCellCoords( oldTop
, rightCol
+ 1 ),
7514 wxGridCellCoords( oldBottom
, oldRight
) );
7517 if ( oldBottom
> bottomRow
)
7519 // Refresh the newly selected or deselected
7520 // area below the old or new selection.
7521 need_refresh
[3] = true;
7522 rect
[3] = BlockToDeviceRect(
7523 wxGridCellCoords( bottomRow
+ 1, leftCol
),
7524 wxGridCellCoords( oldBottom
, rightCol
) );
7527 // various Refresh() calls
7528 for (i
= 0; i
< 4; i
++ )
7529 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
7530 m_gridWin
->Refresh( false, &(rect
[i
]) );
7534 m_selectingTopLeft
= updateTopLeft
;
7535 m_selectingBottomRight
= updateBottomRight
;
7539 // ------ functions to get/send data (see also public functions)
7542 bool wxGrid::GetModelValues()
7544 // Hide the editor, so it won't hide a changed value.
7545 HideCellEditControl();
7549 // all we need to do is repaint the grid
7551 m_gridWin
->Refresh();
7558 bool wxGrid::SetModelValues()
7562 // Disable the editor, so it won't hide a changed value.
7563 // Do we also want to save the current value of the editor first?
7565 DisableCellEditControl();
7569 for ( row
= 0; row
< m_numRows
; row
++ )
7571 for ( col
= 0; col
< m_numCols
; col
++ )
7573 m_table
->SetValue( row
, col
, GetCellValue(row
, col
) );
7583 // Note - this function only draws cells that are in the list of
7584 // exposed cells (usually set from the update region by
7585 // CalcExposedCells)
7587 void wxGrid::DrawGridCellArea( wxDC
& dc
, const wxGridCellCoordsArray
& cells
)
7589 if ( !m_numRows
|| !m_numCols
)
7592 int i
, numCells
= cells
.GetCount();
7593 int row
, col
, cell_rows
, cell_cols
;
7594 wxGridCellCoordsArray redrawCells
;
7596 for ( i
= numCells
- 1; i
>= 0; i
-- )
7598 row
= cells
[i
].GetRow();
7599 col
= cells
[i
].GetCol();
7600 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7602 // If this cell is part of a multicell block, find owner for repaint
7603 if ( cell_rows
<= 0 || cell_cols
<= 0 )
7605 wxGridCellCoords
cell( row
+ cell_rows
, col
+ cell_cols
);
7606 bool marked
= false;
7607 for ( int j
= 0; j
< numCells
; j
++ )
7609 if ( cell
== cells
[j
] )
7618 int count
= redrawCells
.GetCount();
7619 for (int j
= 0; j
< count
; j
++)
7621 if ( cell
== redrawCells
[j
] )
7629 redrawCells
.Add( cell
);
7632 // don't bother drawing this cell
7636 // If this cell is empty, find cell to left that might want to overflow
7637 if (m_table
&& m_table
->IsEmptyCell(row
, col
))
7639 for ( int l
= 0; l
< cell_rows
; l
++ )
7641 // find a cell in this row to leave already marked for repaint
7643 for (int k
= 0; k
< int(redrawCells
.GetCount()); k
++)
7644 if ((redrawCells
[k
].GetCol() < left
) &&
7645 (redrawCells
[k
].GetRow() == row
))
7647 left
= redrawCells
[k
].GetCol();
7651 left
= 0; // oh well
7653 for (int j
= col
- 1; j
>= left
; j
--)
7655 if (!m_table
->IsEmptyCell(row
+ l
, j
))
7657 if (GetCellOverflow(row
+ l
, j
))
7659 wxGridCellCoords
cell(row
+ l
, j
);
7660 bool marked
= false;
7662 for (int k
= 0; k
< numCells
; k
++)
7664 if ( cell
== cells
[k
] )
7673 int count
= redrawCells
.GetCount();
7674 for (int k
= 0; k
< count
; k
++)
7676 if ( cell
== redrawCells
[k
] )
7683 redrawCells
.Add( cell
);
7692 DrawCell( dc
, cells
[i
] );
7695 numCells
= redrawCells
.GetCount();
7697 for ( i
= numCells
- 1; i
>= 0; i
-- )
7699 DrawCell( dc
, redrawCells
[i
] );
7703 void wxGrid::DrawGridSpace( wxDC
& dc
)
7706 m_gridWin
->GetClientSize( &cw
, &ch
);
7709 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
7711 int rightCol
= m_numCols
> 0 ? GetColRight(GetColAt( m_numCols
- 1 )) : 0;
7712 int bottomRow
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) : 0;
7714 if ( right
> rightCol
|| bottom
> bottomRow
)
7717 CalcUnscrolledPosition( 0, 0, &left
, &top
);
7719 dc
.SetBrush(GetDefaultCellBackgroundColour());
7720 dc
.SetPen( *wxTRANSPARENT_PEN
);
7722 if ( right
> rightCol
)
7724 dc
.DrawRectangle( rightCol
, top
, right
- rightCol
, ch
);
7727 if ( bottom
> bottomRow
)
7729 dc
.DrawRectangle( left
, bottomRow
, cw
, bottom
- bottomRow
);
7734 void wxGrid::DrawCell( wxDC
& dc
, const wxGridCellCoords
& coords
)
7736 int row
= coords
.GetRow();
7737 int col
= coords
.GetCol();
7739 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7742 // we draw the cell border ourselves
7743 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7745 bool isCurrent
= coords
== m_currentCellCoords
;
7747 wxRect rect
= CellToRect( row
, col
);
7749 // if the editor is shown, we should use it and not the renderer
7750 // Note: However, only if it is really _shown_, i.e. not hidden!
7751 if ( isCurrent
&& IsCellEditControlShown() )
7753 // NB: this "#if..." is temporary and fixes a problem where the
7754 // edit control is erased by this code after being rendered.
7755 // On wxMac (QD build only), the cell editor is a wxTextCntl and is rendered
7756 // implicitly, causing this out-of order render.
7757 #if !defined(__WXMAC__)
7758 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
7759 editor
->PaintBackground(rect
, attr
);
7765 // but all the rest is drawn by the cell renderer and hence may be customized
7766 wxGridCellRenderer
*renderer
= attr
->GetRenderer(this, row
, col
);
7767 renderer
->Draw(*this, *attr
, dc
, rect
, row
, col
, IsInSelection(coords
));
7774 void wxGrid::DrawCellHighlight( wxDC
& dc
, const wxGridCellAttr
*attr
)
7776 // don't show highlight when the grid doesn't have focus
7780 int row
= m_currentCellCoords
.GetRow();
7781 int col
= m_currentCellCoords
.GetCol();
7783 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7786 wxRect rect
= CellToRect(row
, col
);
7788 // hmmm... what could we do here to show that the cell is disabled?
7789 // for now, I just draw a thinner border than for the other ones, but
7790 // it doesn't look really good
7792 int penWidth
= attr
->IsReadOnly() ? m_cellHighlightROPenWidth
: m_cellHighlightPenWidth
;
7796 // The center of the drawn line is where the position/width/height of
7797 // the rectangle is actually at (on wxMSW at least), so the
7798 // size of the rectangle is reduced to compensate for the thickness of
7799 // the line. If this is too strange on non-wxMSW platforms then
7800 // please #ifdef this appropriately.
7801 rect
.x
+= penWidth
/ 2;
7802 rect
.y
+= penWidth
/ 2;
7803 rect
.width
-= penWidth
- 1;
7804 rect
.height
-= penWidth
- 1;
7806 // Now draw the rectangle
7807 // use the cellHighlightColour if the cell is inside a selection, this
7808 // will ensure the cell is always visible.
7809 dc
.SetPen(wxPen(IsInSelection(row
,col
) ? m_selectionForeground
7810 : m_cellHighlightColour
,
7812 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
7813 dc
.DrawRectangle(rect
);
7817 wxPen
wxGrid::GetDefaultGridLinePen()
7819 return wxPen(GetGridLineColour());
7822 wxPen
wxGrid::GetRowGridLinePen(int WXUNUSED(row
))
7824 return GetDefaultGridLinePen();
7827 wxPen
wxGrid::GetColGridLinePen(int WXUNUSED(col
))
7829 return GetDefaultGridLinePen();
7832 void wxGrid::DrawCellBorder( wxDC
& dc
, const wxGridCellCoords
& coords
)
7834 int row
= coords
.GetRow();
7835 int col
= coords
.GetCol();
7836 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7840 wxRect rect
= CellToRect( row
, col
);
7842 // right hand border
7843 dc
.SetPen( GetColGridLinePen(col
) );
7844 dc
.DrawLine( rect
.x
+ rect
.width
, rect
.y
,
7845 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
+ 1 );
7848 dc
.SetPen( GetRowGridLinePen(row
) );
7849 dc
.DrawLine( rect
.x
, rect
.y
+ rect
.height
,
7850 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
);
7853 void wxGrid::DrawHighlight(wxDC
& dc
, const wxGridCellCoordsArray
& cells
)
7855 // This if block was previously in wxGrid::OnPaint but that doesn't
7856 // seem to get called under wxGTK - MB
7858 if ( m_currentCellCoords
== wxGridNoCellCoords
&&
7859 m_numRows
&& m_numCols
)
7861 m_currentCellCoords
.Set(0, 0);
7864 if ( IsCellEditControlShown() )
7866 // don't show highlight when the edit control is shown
7870 // if the active cell was repainted, repaint its highlight too because it
7871 // might have been damaged by the grid lines
7872 size_t count
= cells
.GetCount();
7873 for ( size_t n
= 0; n
< count
; n
++ )
7875 wxGridCellCoords cell
= cells
[n
];
7877 // If we are using attributes, then we may have just exposed another
7878 // cell in a partially-visible merged cluster of cells. If the "anchor"
7879 // (upper left) cell of this merged cluster is the cell indicated by
7880 // m_currentCellCoords, then we need to refresh the cell highlight even
7881 // though the "anchor" itself is not part of our update segment.
7882 if ( CanHaveAttributes() )
7886 GetCellSize(cell
.GetRow(), cell
.GetCol(), &rows
, &cols
);
7889 cell
.SetRow(cell
.GetRow() + rows
);
7892 cell
.SetCol(cell
.GetCol() + cols
);
7895 if ( cell
== m_currentCellCoords
)
7897 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
7898 DrawCellHighlight(dc
, attr
);
7906 // This is used to redraw all grid lines e.g. when the grid line colour
7909 void wxGrid::DrawAllGridLines( wxDC
& dc
, const wxRegion
& WXUNUSED(reg
) )
7911 if ( !m_gridLinesEnabled
|| !m_numRows
|| !m_numCols
)
7914 int top
, bottom
, left
, right
;
7917 m_gridWin
->GetClientSize(&cw
, &ch
);
7918 CalcUnscrolledPosition( 0, 0, &left
, &top
);
7919 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
7921 // avoid drawing grid lines past the last row and col
7923 right
= wxMin( right
, GetColRight(GetColAt( m_numCols
- 1 )) );
7924 bottom
= wxMin( bottom
, GetRowBottom(m_numRows
- 1) );
7926 // no gridlines inside multicells, clip them out
7927 int leftCol
= GetColPos( internalXToCol(left
) );
7928 int topRow
= internalYToRow(top
);
7929 int rightCol
= GetColPos( internalXToCol(right
) );
7930 int bottomRow
= internalYToRow(bottom
);
7932 wxRegion
clippedcells(0, 0, cw
, ch
);
7934 int cell_rows
, cell_cols
;
7937 for ( int j
= topRow
; j
<= bottomRow
; j
++ )
7939 for ( int colPos
= leftCol
; colPos
<= rightCol
; colPos
++ )
7941 int i
= GetColAt( colPos
);
7943 GetCellSize( j
, i
, &cell_rows
, &cell_cols
);
7944 if ((cell_rows
> 1) || (cell_cols
> 1))
7946 rect
= CellToRect(j
,i
);
7947 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7948 clippedcells
.Subtract(rect
);
7950 else if ((cell_rows
< 0) || (cell_cols
< 0))
7952 rect
= CellToRect(j
+ cell_rows
, i
+ cell_cols
);
7953 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7954 clippedcells
.Subtract(rect
);
7959 dc
.SetDeviceClippingRegion( clippedcells
);
7962 // horizontal grid lines
7963 for ( int i
= internalYToRow(top
); i
< m_numRows
; i
++ )
7965 int bot
= GetRowBottom(i
) - 1;
7972 dc
.SetPen( GetRowGridLinePen(i
) );
7973 dc
.DrawLine( left
, bot
, right
, bot
);
7977 // vertical grid lines
7978 for ( int colPos
= leftCol
; colPos
< m_numCols
; colPos
++ )
7980 int i
= GetColAt( colPos
);
7982 int colRight
= GetColRight(i
);
7984 if (GetLayoutDirection() != wxLayout_RightToLeft
)
7988 if ( colRight
> right
)
7991 if ( colRight
>= left
)
7993 dc
.SetPen( GetColGridLinePen(i
) );
7994 dc
.DrawLine( colRight
, top
, colRight
, bottom
);
7998 dc
.DestroyClippingRegion();
8001 void wxGrid::DrawRowLabels( wxDC
& dc
, const wxArrayInt
& rows
)
8006 const size_t numLabels
= rows
.GetCount();
8007 for ( size_t i
= 0; i
< numLabels
; i
++ )
8009 DrawRowLabel( dc
, rows
[i
] );
8013 void wxGrid::DrawRowLabel( wxDC
& dc
, int row
)
8015 if ( GetRowHeight(row
) <= 0 || m_rowLabelWidth
<= 0 )
8020 int rowTop
= GetRowTop(row
),
8021 rowBottom
= GetRowBottom(row
) - 1;
8023 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW
)));
8024 dc
.DrawLine( m_rowLabelWidth
- 1, rowTop
, m_rowLabelWidth
- 1, rowBottom
);
8025 dc
.DrawLine( 0, rowTop
, 0, rowBottom
);
8026 dc
.DrawLine( 0, rowBottom
, m_rowLabelWidth
, rowBottom
);
8028 dc
.SetPen( *wxWHITE_PEN
);
8029 dc
.DrawLine( 1, rowTop
, 1, rowBottom
);
8030 dc
.DrawLine( 1, rowTop
, m_rowLabelWidth
- 1, rowTop
);
8032 dc
.SetBackgroundMode( wxBRUSHSTYLE_TRANSPARENT
);
8033 dc
.SetTextForeground( GetLabelTextColour() );
8034 dc
.SetFont( GetLabelFont() );
8037 GetRowLabelAlignment( &hAlign
, &vAlign
);
8040 rect
.SetY( GetRowTop(row
) + 2 );
8041 rect
.SetWidth( m_rowLabelWidth
- 4 );
8042 rect
.SetHeight( GetRowHeight(row
) - 4 );
8043 DrawTextRectangle( dc
, GetRowLabelValue( row
), rect
, hAlign
, vAlign
);
8046 void wxGrid::SetUseNativeColLabels( bool native
)
8048 m_nativeColumnLabels
= native
;
8051 int height
= wxRendererNative::Get().GetHeaderButtonHeight( this );
8052 SetColLabelSize( height
);
8055 m_colLabelWin
->Refresh();
8056 m_cornerLabelWin
->Refresh();
8059 void wxGrid::DrawColLabels( wxDC
& dc
,const wxArrayInt
& cols
)
8064 const size_t numLabels
= cols
.GetCount();
8065 for ( size_t i
= 0; i
< numLabels
; i
++ )
8067 DrawColLabel( dc
, cols
[i
] );
8071 void wxGrid::DrawCornerLabel(wxDC
& dc
)
8073 if ( m_nativeColumnLabels
)
8075 wxRect
rect(wxSize(m_rowLabelWidth
, m_colLabelHeight
));
8078 wxRendererNative::Get().DrawHeaderButton(m_cornerLabelWin
, dc
, rect
, 0);
8082 dc
.SetPen(wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW
)));
8083 dc
.DrawLine( m_rowLabelWidth
- 1, m_colLabelHeight
- 1,
8084 m_rowLabelWidth
- 1, 0 );
8085 dc
.DrawLine( m_rowLabelWidth
- 1, m_colLabelHeight
- 1,
8086 0, m_colLabelHeight
- 1 );
8087 dc
.DrawLine( 0, 0, m_rowLabelWidth
, 0 );
8088 dc
.DrawLine( 0, 0, 0, m_colLabelHeight
);
8090 dc
.SetPen( *wxWHITE_PEN
);
8091 dc
.DrawLine( 1, 1, m_rowLabelWidth
- 1, 1 );
8092 dc
.DrawLine( 1, 1, 1, m_colLabelHeight
- 1 );
8096 void wxGrid::DrawColLabel(wxDC
& dc
, int col
)
8098 if ( GetColWidth(col
) <= 0 || m_colLabelHeight
<= 0 )
8101 int colLeft
= GetColLeft(col
);
8103 wxRect
rect(colLeft
, 0, GetColWidth(col
), m_colLabelHeight
);
8105 if ( m_nativeColumnLabels
)
8107 wxRendererNative::Get().DrawHeaderButton(m_colLabelWin
, dc
, rect
, 0);
8111 int colRight
= GetColRight(col
) - 1;
8113 dc
.SetPen(wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW
)));
8114 dc
.DrawLine( colRight
, 0,
8115 colRight
, m_colLabelHeight
- 1 );
8116 dc
.DrawLine( colLeft
, 0,
8118 dc
.DrawLine( colLeft
, m_colLabelHeight
- 1,
8119 colRight
+ 1, m_colLabelHeight
- 1 );
8121 dc
.SetPen( *wxWHITE_PEN
);
8122 dc
.DrawLine( colLeft
, 1, colLeft
, m_colLabelHeight
- 1 );
8123 dc
.DrawLine( colLeft
, 1, colRight
, 1 );
8126 dc
.SetBackgroundMode( wxBRUSHSTYLE_TRANSPARENT
);
8127 dc
.SetTextForeground( GetLabelTextColour() );
8128 dc
.SetFont( GetLabelFont() );
8131 GetColLabelAlignment( &hAlign
, &vAlign
);
8132 const int orient
= GetColLabelTextOrientation();
8135 DrawTextRectangle(dc
, GetColLabelValue(col
), rect
, hAlign
, vAlign
, orient
);
8138 // TODO: these 2 functions should be replaced with wxDC::DrawLabel() to which
8139 // we just have to add textOrientation support
8140 void wxGrid::DrawTextRectangle( wxDC
& dc
,
8141 const wxString
& value
,
8145 int textOrientation
)
8147 wxArrayString lines
;
8149 StringToLines( value
, lines
);
8151 DrawTextRectangle(dc
, lines
, rect
, horizAlign
, vertAlign
, textOrientation
);
8154 void wxGrid::DrawTextRectangle(wxDC
& dc
,
8155 const wxArrayString
& lines
,
8159 int textOrientation
)
8161 if ( lines
.empty() )
8164 wxDCClipper
clip(dc
, rect
);
8169 if ( textOrientation
== wxHORIZONTAL
)
8170 GetTextBoxSize( dc
, lines
, &textWidth
, &textHeight
);
8172 GetTextBoxSize( dc
, lines
, &textHeight
, &textWidth
);
8176 switch ( vertAlign
)
8178 case wxALIGN_BOTTOM
:
8179 if ( textOrientation
== wxHORIZONTAL
)
8180 y
= rect
.y
+ (rect
.height
- textHeight
- 1);
8182 x
= rect
.x
+ rect
.width
- textWidth
;
8185 case wxALIGN_CENTRE
:
8186 if ( textOrientation
== wxHORIZONTAL
)
8187 y
= rect
.y
+ ((rect
.height
- textHeight
) / 2);
8189 x
= rect
.x
+ ((rect
.width
- textWidth
) / 2);
8194 if ( textOrientation
== wxHORIZONTAL
)
8201 // Align each line of a multi-line label
8202 size_t nLines
= lines
.GetCount();
8203 for ( size_t l
= 0; l
< nLines
; l
++ )
8205 const wxString
& line
= lines
[l
];
8209 *(textOrientation
== wxHORIZONTAL
? &y
: &x
) += dc
.GetCharHeight();
8213 wxCoord lineWidth
= 0,
8215 dc
.GetTextExtent(line
, &lineWidth
, &lineHeight
);
8217 switch ( horizAlign
)
8220 if ( textOrientation
== wxHORIZONTAL
)
8221 x
= rect
.x
+ (rect
.width
- lineWidth
- 1);
8223 y
= rect
.y
+ lineWidth
+ 1;
8226 case wxALIGN_CENTRE
:
8227 if ( textOrientation
== wxHORIZONTAL
)
8228 x
= rect
.x
+ ((rect
.width
- lineWidth
) / 2);
8230 y
= rect
.y
+ rect
.height
- ((rect
.height
- lineWidth
) / 2);
8235 if ( textOrientation
== wxHORIZONTAL
)
8238 y
= rect
.y
+ rect
.height
- 1;
8242 if ( textOrientation
== wxHORIZONTAL
)
8244 dc
.DrawText( line
, x
, y
);
8249 dc
.DrawRotatedText( line
, x
, y
, 90.0 );
8255 // Split multi-line text up into an array of strings.
8256 // Any existing contents of the string array are preserved.
8258 // TODO: refactor wxTextFile::Read() and reuse the same code from here
8259 void wxGrid::StringToLines( const wxString
& value
, wxArrayString
& lines
) const
8263 wxString eol
= wxTextFile::GetEOL( wxTextFileType_Unix
);
8264 wxString tVal
= wxTextFile::Translate( value
, wxTextFileType_Unix
);
8266 while ( startPos
< (int)tVal
.length() )
8268 pos
= tVal
.Mid(startPos
).Find( eol
);
8273 else if ( pos
== 0 )
8275 lines
.Add( wxEmptyString
);
8279 lines
.Add( tVal
.Mid(startPos
, pos
) );
8282 startPos
+= pos
+ 1;
8285 if ( startPos
< (int)tVal
.length() )
8287 lines
.Add( tVal
.Mid( startPos
) );
8291 void wxGrid::GetTextBoxSize( const wxDC
& dc
,
8292 const wxArrayString
& lines
,
8293 long *width
, long *height
) const
8297 wxCoord lineW
= 0, lineH
= 0;
8300 for ( i
= 0; i
< lines
.GetCount(); i
++ )
8302 dc
.GetTextExtent( lines
[i
], &lineW
, &lineH
);
8303 w
= wxMax( w
, lineW
);
8312 // ------ Batch processing.
8314 void wxGrid::EndBatch()
8316 if ( m_batchCount
> 0 )
8319 if ( !m_batchCount
)
8322 m_rowLabelWin
->Refresh();
8323 m_colLabelWin
->Refresh();
8324 m_cornerLabelWin
->Refresh();
8325 m_gridWin
->Refresh();
8330 // Use this, rather than wxWindow::Refresh(), to force an immediate
8331 // repainting of the grid. Has no effect if you are already inside a
8332 // BeginBatch / EndBatch block.
8334 void wxGrid::ForceRefresh()
8340 bool wxGrid::Enable(bool enable
)
8342 if ( !wxScrolledWindow::Enable(enable
) )
8345 // redraw in the new state
8346 m_gridWin
->Refresh();
8352 // ------ Edit control functions
8355 void wxGrid::EnableEditing( bool edit
)
8357 if ( edit
!= m_editable
)
8360 EnableCellEditControl(edit
);
8365 void wxGrid::EnableCellEditControl( bool enable
)
8370 if ( enable
!= m_cellEditCtrlEnabled
)
8374 if (SendEvent( wxEVT_GRID_EDITOR_SHOWN
) <0)
8377 // this should be checked by the caller!
8378 wxASSERT_MSG( CanEnableCellControl(), _T("can't enable editing for this cell!") );
8380 // do it before ShowCellEditControl()
8381 m_cellEditCtrlEnabled
= enable
;
8383 ShowCellEditControl();
8387 //FIXME:add veto support
8388 SendEvent( wxEVT_GRID_EDITOR_HIDDEN
);
8390 HideCellEditControl();
8391 SaveEditControlValue();
8393 // do it after HideCellEditControl()
8394 m_cellEditCtrlEnabled
= enable
;
8399 bool wxGrid::IsCurrentCellReadOnly() const
8402 wxGridCellAttr
* attr
= ((wxGrid
*)this)->GetCellAttr(m_currentCellCoords
);
8403 bool readonly
= attr
->IsReadOnly();
8409 bool wxGrid::CanEnableCellControl() const
8411 return m_editable
&& (m_currentCellCoords
!= wxGridNoCellCoords
) &&
8412 !IsCurrentCellReadOnly();
8415 bool wxGrid::IsCellEditControlEnabled() const
8417 // the cell edit control might be disable for all cells or just for the
8418 // current one if it's read only
8419 return m_cellEditCtrlEnabled
? !IsCurrentCellReadOnly() : false;
8422 bool wxGrid::IsCellEditControlShown() const
8424 bool isShown
= false;
8426 if ( m_cellEditCtrlEnabled
)
8428 int row
= m_currentCellCoords
.GetRow();
8429 int col
= m_currentCellCoords
.GetCol();
8430 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8431 wxGridCellEditor
* editor
= attr
->GetEditor((wxGrid
*) this, row
, col
);
8436 if ( editor
->IsCreated() )
8438 isShown
= editor
->GetControl()->IsShown();
8448 void wxGrid::ShowCellEditControl()
8450 if ( IsCellEditControlEnabled() )
8452 if ( !IsVisible( m_currentCellCoords
, false ) )
8454 m_cellEditCtrlEnabled
= false;
8459 wxRect rect
= CellToRect( m_currentCellCoords
);
8460 int row
= m_currentCellCoords
.GetRow();
8461 int col
= m_currentCellCoords
.GetCol();
8463 // if this is part of a multicell, find owner (topleft)
8464 int cell_rows
, cell_cols
;
8465 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
8466 if ( cell_rows
<= 0 || cell_cols
<= 0 )
8470 m_currentCellCoords
.SetRow( row
);
8471 m_currentCellCoords
.SetCol( col
);
8474 // erase the highlight and the cell contents because the editor
8475 // might not cover the entire cell
8476 wxClientDC
dc( m_gridWin
);
8478 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8479 dc
.SetBrush(wxBrush(attr
->GetBackgroundColour()));
8480 dc
.SetPen(*wxTRANSPARENT_PEN
);
8481 dc
.DrawRectangle(rect
);
8483 // convert to scrolled coords
8484 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
8490 // cell is shifted by one pixel
8491 // However, don't allow x or y to become negative
8492 // since the SetSize() method interprets that as
8499 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
8500 if ( !editor
->IsCreated() )
8502 editor
->Create(m_gridWin
, wxID_ANY
,
8503 new wxGridCellEditorEvtHandler(this, editor
));
8505 wxGridEditorCreatedEvent
evt(GetId(),
8506 wxEVT_GRID_EDITOR_CREATED
,
8510 editor
->GetControl());
8511 GetEventHandler()->ProcessEvent(evt
);
8514 // resize editor to overflow into righthand cells if allowed
8515 int maxWidth
= rect
.width
;
8516 wxString value
= GetCellValue(row
, col
);
8517 if ( (value
!= wxEmptyString
) && (attr
->GetOverflow()) )
8520 GetTextExtent(value
, &maxWidth
, &y
, NULL
, NULL
, &attr
->GetFont());
8521 if (maxWidth
< rect
.width
)
8522 maxWidth
= rect
.width
;
8525 int client_right
= m_gridWin
->GetClientSize().GetWidth();
8526 if (rect
.x
+ maxWidth
> client_right
)
8527 maxWidth
= client_right
- rect
.x
;
8529 if ((maxWidth
> rect
.width
) && (col
< m_numCols
) && m_table
)
8531 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
8532 // may have changed earlier
8533 for (int i
= col
+ cell_cols
; i
< m_numCols
; i
++)
8536 GetCellSize( row
, i
, &c_rows
, &c_cols
);
8538 // looks weird going over a multicell
8539 if (m_table
->IsEmptyCell( row
, i
) &&
8540 (rect
.width
< maxWidth
) && (c_rows
== 1))
8542 rect
.width
+= GetColWidth( i
);
8548 if (rect
.GetRight() > client_right
)
8549 rect
.SetRight( client_right
- 1 );
8552 editor
->SetCellAttr( attr
);
8553 editor
->SetSize( rect
);
8555 editor
->GetControl()->Move(
8556 editor
->GetControl()->GetPosition().x
+ nXMove
,
8557 editor
->GetControl()->GetPosition().y
);
8558 editor
->Show( true, attr
);
8560 // recalc dimensions in case we need to
8561 // expand the scrolled window to account for editor
8564 editor
->BeginEdit(row
, col
, this);
8565 editor
->SetCellAttr(NULL
);
8573 void wxGrid::HideCellEditControl()
8575 if ( IsCellEditControlEnabled() )
8577 int row
= m_currentCellCoords
.GetRow();
8578 int col
= m_currentCellCoords
.GetCol();
8580 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8581 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
8582 const bool editorHadFocus
= editor
->GetControl()->HasFocus();
8583 editor
->Show( false );
8587 // return the focus to the grid itself if the editor had it
8589 // note that we must not do this unconditionally to avoid stealing
8590 // focus from the window which just received it if we are hiding the
8591 // editor precisely because we lost focus
8592 if ( editorHadFocus
)
8593 m_gridWin
->SetFocus();
8595 // refresh whole row to the right
8596 wxRect
rect( CellToRect(row
, col
) );
8597 CalcScrolledPosition(rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
8598 rect
.width
= m_gridWin
->GetClientSize().GetWidth() - rect
.x
;
8601 // ensure that the pixels under the focus ring get refreshed as well
8602 rect
.Inflate(10, 10);
8605 m_gridWin
->Refresh( false, &rect
);
8609 void wxGrid::SaveEditControlValue()
8611 if ( IsCellEditControlEnabled() )
8613 int row
= m_currentCellCoords
.GetRow();
8614 int col
= m_currentCellCoords
.GetCol();
8616 wxString oldval
= GetCellValue(row
, col
);
8618 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8619 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
8620 bool changed
= editor
->EndEdit(row
, col
, this);
8627 if ( SendEvent( wxEVT_GRID_CELL_CHANGE
,
8628 m_currentCellCoords
.GetRow(),
8629 m_currentCellCoords
.GetCol() ) < 0 )
8631 // Event has been vetoed, set the data back.
8632 SetCellValue(row
, col
, oldval
);
8639 // ------ Grid location functions
8640 // Note that all of these functions work with the logical coordinates of
8641 // grid cells and labels so you will need to convert from device
8642 // coordinates for mouse events etc.
8645 void wxGrid::XYToCell( int x
, int y
, wxGridCellCoords
& coords
) const
8647 int row
= YToRow(y
);
8648 int col
= XToCol(x
);
8650 if ( row
== -1 || col
== -1 )
8652 coords
= wxGridNoCellCoords
;
8656 coords
.Set( row
, col
);
8660 // compute row or column from some (unscrolled) coordinate value, using either
8661 // m_defaultRowHeight/m_defaultColWidth or binary search on array of
8662 // m_rowBottoms/m_colRights to do it quickly (linear search shouldn't be used
8665 wxGrid::PosToLine(int coord
,
8667 const wxGridOperations
& oper
) const
8669 const int numLines
= oper
.GetNumberOfLines(this);
8672 return clipToMinMax
&& numLines
> 0 ? oper
.GetLineAt(this, 0) : -1;
8674 const int defaultLineSize
= oper
.GetDefaultLineSize(this);
8675 wxCHECK_MSG( defaultLineSize
, -1, "can't have 0 default line size" );
8677 int maxPos
= coord
/ defaultLineSize
,
8680 // check for the simplest case: if we have no explicit line sizes
8681 // configured, then we already know the line this position falls in
8682 const wxArrayInt
& lineEnds
= oper
.GetLineEnds(this);
8683 if ( lineEnds
.empty() )
8685 if ( maxPos
< numLines
)
8688 return clipToMinMax
? numLines
- 1 : -1;
8692 // adjust maxPos before starting the binary search
8693 if ( maxPos
>= numLines
)
8695 maxPos
= numLines
- 1;
8699 if ( coord
>= lineEnds
[oper
.GetLineAt(this, maxPos
)])
8702 const int minDist
= oper
.GetMinimalAcceptableLineSize(this);
8704 maxPos
= coord
/ minDist
;
8706 maxPos
= numLines
- 1;
8709 if ( maxPos
>= numLines
)
8710 maxPos
= numLines
- 1;
8713 // check if the position is beyond the last column
8714 const int lineAtMaxPos
= oper
.GetLineAt(this, maxPos
);
8715 if ( coord
>= lineEnds
[lineAtMaxPos
] )
8716 return clipToMinMax
? lineAtMaxPos
: -1;
8718 // or before the first one
8719 const int lineAt0
= oper
.GetLineAt(this, 0);
8720 if ( coord
< lineEnds
[lineAt0
] )
8724 // finally do perform the binary search
8725 while ( minPos
< maxPos
)
8727 wxCHECK_MSG( lineEnds
[oper
.GetLineAt(this, minPos
)] <= coord
&&
8728 coord
< lineEnds
[oper
.GetLineAt(this, maxPos
)],
8730 "wxGrid: internal error in PosToLine()" );
8732 if ( coord
>= lineEnds
[oper
.GetLineAt(this, maxPos
- 1)] )
8733 return oper
.GetLineAt(this, maxPos
);
8737 const int median
= minPos
+ (maxPos
- minPos
+ 1) / 2;
8738 if ( coord
< lineEnds
[oper
.GetLineAt(this, median
)] )
8744 return oper
.GetLineAt(this, maxPos
);
8747 int wxGrid::YToRow(int y
, bool clipToMinMax
) const
8749 return PosToLine(y
, clipToMinMax
, wxGridRowOperations());
8752 int wxGrid::XToCol(int x
, bool clipToMinMax
) const
8754 return PosToLine(x
, clipToMinMax
, wxGridColumnOperations());
8757 // return the row number that that the y coord is near the edge of, or -1 if
8758 // not near an edge.
8760 // coords can only possibly be near an edge if
8761 // (a) the row/column is large enough to still allow for an "inner" area
8762 // that is _not_ near the edge (i.e., if the height/width is smaller
8763 // than WXGRID_LABEL_EDGE_ZONE, coords are _never_ considered to be
8766 // (b) resizing rows/columns (the thing for which edge detection is
8767 // relevant at all) is enabled.
8769 int wxGrid::PosToEdgeOfLine(int pos
, const wxGridOperations
& oper
) const
8771 if ( !oper
.CanResizeLines(this) )
8774 const int line
= oper
.PosToLine(this, pos
, true);
8776 if ( oper
.GetLineSize(this, line
) > WXGRID_LABEL_EDGE_ZONE
)
8778 // We know that we are in this line, test whether we are close enough
8779 // to start or end border, respectively.
8780 if ( abs(oper
.GetLineEndPos(this, line
) - pos
) < WXGRID_LABEL_EDGE_ZONE
)
8782 else if ( line
> 0 &&
8783 pos
- oper
.GetLineStartPos(this,
8784 line
) < WXGRID_LABEL_EDGE_ZONE
)
8791 int wxGrid::YToEdgeOfRow(int y
) const
8793 return PosToEdgeOfLine(y
, wxGridRowOperations());
8796 int wxGrid::XToEdgeOfCol(int x
) const
8798 return PosToEdgeOfLine(x
, wxGridColumnOperations());
8801 wxRect
wxGrid::CellToRect( int row
, int col
) const
8803 wxRect
rect( -1, -1, -1, -1 );
8805 if ( row
>= 0 && row
< m_numRows
&&
8806 col
>= 0 && col
< m_numCols
)
8808 int i
, cell_rows
, cell_cols
;
8809 rect
.width
= rect
.height
= 0;
8810 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
8811 // if negative then find multicell owner
8816 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
8818 rect
.x
= GetColLeft(col
);
8819 rect
.y
= GetRowTop(row
);
8820 for (i
=col
; i
< col
+ cell_cols
; i
++)
8821 rect
.width
+= GetColWidth(i
);
8822 for (i
=row
; i
< row
+ cell_rows
; i
++)
8823 rect
.height
+= GetRowHeight(i
);
8826 // if grid lines are enabled, then the area of the cell is a bit smaller
8827 if (m_gridLinesEnabled
)
8836 bool wxGrid::IsVisible( int row
, int col
, bool wholeCellVisible
) const
8838 // get the cell rectangle in logical coords
8840 wxRect
r( CellToRect( row
, col
) );
8842 // convert to device coords
8844 int left
, top
, right
, bottom
;
8845 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
8846 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
8848 // check against the client area of the grid window
8850 m_gridWin
->GetClientSize( &cw
, &ch
);
8852 if ( wholeCellVisible
)
8854 // is the cell wholly visible ?
8855 return ( left
>= 0 && right
<= cw
&&
8856 top
>= 0 && bottom
<= ch
);
8860 // is the cell partly visible ?
8862 return ( ((left
>= 0 && left
< cw
) || (right
> 0 && right
<= cw
)) &&
8863 ((top
>= 0 && top
< ch
) || (bottom
> 0 && bottom
<= ch
)) );
8867 // make the specified cell location visible by doing a minimal amount
8870 void wxGrid::MakeCellVisible( int row
, int col
)
8873 int xpos
= -1, ypos
= -1;
8875 if ( row
>= 0 && row
< m_numRows
&&
8876 col
>= 0 && col
< m_numCols
)
8878 // get the cell rectangle in logical coords
8879 wxRect
r( CellToRect( row
, col
) );
8881 // convert to device coords
8882 int left
, top
, right
, bottom
;
8883 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
8884 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
8887 m_gridWin
->GetClientSize( &cw
, &ch
);
8893 else if ( bottom
> ch
)
8895 int h
= r
.GetHeight();
8897 for ( i
= row
- 1; i
>= 0; i
-- )
8899 int rowHeight
= GetRowHeight(i
);
8900 if ( h
+ rowHeight
> ch
)
8907 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
8908 // have rounding errors (this is important, because if we do,
8909 // we might not scroll at all and some cells won't be redrawn)
8911 // Sometimes GRID_SCROLL_LINE / 2 is not enough,
8912 // so just add a full scroll unit...
8913 ypos
+= m_scrollLineY
;
8916 // special handling for wide cells - show always left part of the cell!
8917 // Otherwise, e.g. when stepping from row to row, it would jump between
8918 // left and right part of the cell on every step!
8920 if ( left
< 0 || (right
- left
) >= cw
)
8924 else if ( right
> cw
)
8926 // position the view so that the cell is on the right
8928 CalcUnscrolledPosition(0, 0, &x0
, &y0
);
8929 xpos
= x0
+ (right
- cw
);
8931 // see comment for ypos above
8932 xpos
+= m_scrollLineX
;
8935 if ( xpos
!= -1 || ypos
!= -1 )
8938 xpos
/= m_scrollLineX
;
8940 ypos
/= m_scrollLineY
;
8941 Scroll( xpos
, ypos
);
8948 // ------ Grid cursor movement functions
8952 wxGrid::DoMoveCursor(bool expandSelection
,
8953 const wxGridDirectionOperations
& diroper
)
8955 if ( m_currentCellCoords
== wxGridNoCellCoords
)
8958 if ( expandSelection
)
8960 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8961 m_selectingKeyboard
= m_currentCellCoords
;
8963 if ( diroper
.IsAtBoundary(m_selectingKeyboard
) )
8966 diroper
.Advance(m_selectingKeyboard
);
8968 MakeCellVisible(m_selectingKeyboard
);
8969 HighlightBlock(m_currentCellCoords
, m_selectingKeyboard
);
8973 if ( diroper
.IsAtBoundary(m_currentCellCoords
) )
8978 wxGridCellCoords coords
= m_currentCellCoords
;
8979 diroper
.Advance(coords
);
8980 MakeCellVisible(coords
);
8981 SetCurrentCell(coords
);
8987 bool wxGrid::MoveCursorUp(bool expandSelection
)
8989 return DoMoveCursor(expandSelection
,
8990 wxGridBackwardOperations(this, wxGridRowOperations()));
8993 bool wxGrid::MoveCursorDown(bool expandSelection
)
8995 return DoMoveCursor(expandSelection
,
8996 wxGridForwardOperations(this, wxGridRowOperations()));
8999 bool wxGrid::MoveCursorLeft(bool expandSelection
)
9001 return DoMoveCursor(expandSelection
,
9002 wxGridBackwardOperations(this, wxGridColumnOperations()));
9005 bool wxGrid::MoveCursorRight(bool expandSelection
)
9007 return DoMoveCursor(expandSelection
,
9008 wxGridForwardOperations(this, wxGridColumnOperations()));
9011 bool wxGrid::DoMoveCursorByPage(const wxGridDirectionOperations
& diroper
)
9013 if ( m_currentCellCoords
== wxGridNoCellCoords
)
9016 if ( diroper
.IsAtBoundary(m_currentCellCoords
) )
9019 const int oldRow
= m_currentCellCoords
.GetRow();
9020 int newRow
= diroper
.MoveByPixelDistance(oldRow
, m_gridWin
->GetClientSize().y
);
9021 if ( newRow
== oldRow
)
9023 wxGridCellCoords
coords(m_currentCellCoords
);
9024 diroper
.Advance(coords
);
9025 newRow
= coords
.GetRow();
9028 MakeCellVisible(newRow
, m_currentCellCoords
.GetCol());
9029 SetCurrentCell(newRow
, m_currentCellCoords
.GetCol());
9034 bool wxGrid::MovePageUp()
9036 return DoMoveCursorByPage(
9037 wxGridBackwardOperations(this, wxGridRowOperations()));
9040 bool wxGrid::MovePageDown()
9042 return DoMoveCursorByPage(
9043 wxGridForwardOperations(this, wxGridColumnOperations()));
9046 // helper of DoMoveCursorByBlock(): advance the cell coordinates using diroper
9047 // until we find a non-empty cell or reach the grid end
9049 wxGrid::AdvanceToNextNonEmpty(wxGridCellCoords
& coords
,
9050 const wxGridDirectionOperations
& diroper
)
9052 while ( !diroper
.IsAtBoundary(coords
) )
9054 diroper
.Advance(coords
);
9055 if ( !m_table
->IsEmpty(coords
) )
9061 wxGrid::DoMoveCursorByBlock(bool expandSelection
,
9062 const wxGridDirectionOperations
& diroper
)
9064 if ( !m_table
|| m_currentCellCoords
== wxGridNoCellCoords
)
9067 if ( diroper
.IsAtBoundary(m_currentCellCoords
) )
9070 wxGridCellCoords
coords(m_currentCellCoords
);
9071 if ( m_table
->IsEmpty(coords
) )
9073 // we are in an empty cell: find the next block of non-empty cells
9074 AdvanceToNextNonEmpty(coords
, diroper
);
9076 else // current cell is not empty
9078 diroper
.Advance(coords
);
9079 if ( m_table
->IsEmpty(coords
) )
9081 // we started at the end of a block, find the next one
9082 AdvanceToNextNonEmpty(coords
, diroper
);
9084 else // we're in a middle of a block
9086 // go to the end of it, i.e. find the last cell before the next
9088 while ( !diroper
.IsAtBoundary(coords
) )
9090 wxGridCellCoords
coordsNext(coords
);
9091 diroper
.Advance(coordsNext
);
9092 if ( m_table
->IsEmpty(coordsNext
) )
9095 coords
= coordsNext
;
9100 MakeCellVisible(coords
);
9101 if ( expandSelection
)
9103 m_selectingKeyboard
= coords
;
9104 HighlightBlock(m_currentCellCoords
, m_selectingKeyboard
);
9109 SetCurrentCell(coords
);
9115 bool wxGrid::MoveCursorUpBlock(bool expandSelection
)
9117 return DoMoveCursorByBlock(
9119 wxGridBackwardOperations(this, wxGridRowOperations())
9123 bool wxGrid::MoveCursorDownBlock( bool expandSelection
)
9125 return DoMoveCursorByBlock(
9127 wxGridForwardOperations(this, wxGridRowOperations())
9131 bool wxGrid::MoveCursorLeftBlock( bool expandSelection
)
9133 return DoMoveCursorByBlock(
9135 wxGridBackwardOperations(this, wxGridColumnOperations())
9139 bool wxGrid::MoveCursorRightBlock( bool expandSelection
)
9141 return DoMoveCursorByBlock(
9143 wxGridForwardOperations(this, wxGridColumnOperations())
9148 // ------ Label values and formatting
9151 void wxGrid::GetRowLabelAlignment( int *horiz
, int *vert
) const
9154 *horiz
= m_rowLabelHorizAlign
;
9156 *vert
= m_rowLabelVertAlign
;
9159 void wxGrid::GetColLabelAlignment( int *horiz
, int *vert
) const
9162 *horiz
= m_colLabelHorizAlign
;
9164 *vert
= m_colLabelVertAlign
;
9167 int wxGrid::GetColLabelTextOrientation() const
9169 return m_colLabelTextOrientation
;
9172 wxString
wxGrid::GetRowLabelValue( int row
) const
9176 return m_table
->GetRowLabelValue( row
);
9186 wxString
wxGrid::GetColLabelValue( int col
) const
9190 return m_table
->GetColLabelValue( col
);
9200 void wxGrid::SetRowLabelSize( int width
)
9202 wxASSERT( width
>= 0 || width
== wxGRID_AUTOSIZE
);
9204 if ( width
== wxGRID_AUTOSIZE
)
9206 width
= CalcColOrRowLabelAreaMinSize(wxGRID_ROW
);
9209 if ( width
!= m_rowLabelWidth
)
9213 m_rowLabelWin
->Show( false );
9214 m_cornerLabelWin
->Show( false );
9216 else if ( m_rowLabelWidth
== 0 )
9218 m_rowLabelWin
->Show( true );
9219 if ( m_colLabelHeight
> 0 )
9220 m_cornerLabelWin
->Show( true );
9223 m_rowLabelWidth
= width
;
9225 wxScrolledWindow::Refresh( true );
9229 void wxGrid::SetColLabelSize( int height
)
9231 wxASSERT( height
>=0 || height
== wxGRID_AUTOSIZE
);
9233 if ( height
== wxGRID_AUTOSIZE
)
9235 height
= CalcColOrRowLabelAreaMinSize(wxGRID_COLUMN
);
9238 if ( height
!= m_colLabelHeight
)
9242 m_colLabelWin
->Show( false );
9243 m_cornerLabelWin
->Show( false );
9245 else if ( m_colLabelHeight
== 0 )
9247 m_colLabelWin
->Show( true );
9248 if ( m_rowLabelWidth
> 0 )
9249 m_cornerLabelWin
->Show( true );
9252 m_colLabelHeight
= height
;
9254 wxScrolledWindow::Refresh( true );
9258 void wxGrid::SetLabelBackgroundColour( const wxColour
& colour
)
9260 if ( m_labelBackgroundColour
!= colour
)
9262 m_labelBackgroundColour
= colour
;
9263 m_rowLabelWin
->SetBackgroundColour( colour
);
9264 m_colLabelWin
->SetBackgroundColour( colour
);
9265 m_cornerLabelWin
->SetBackgroundColour( colour
);
9267 if ( !GetBatchCount() )
9269 m_rowLabelWin
->Refresh();
9270 m_colLabelWin
->Refresh();
9271 m_cornerLabelWin
->Refresh();
9276 void wxGrid::SetLabelTextColour( const wxColour
& colour
)
9278 if ( m_labelTextColour
!= colour
)
9280 m_labelTextColour
= colour
;
9281 if ( !GetBatchCount() )
9283 m_rowLabelWin
->Refresh();
9284 m_colLabelWin
->Refresh();
9289 void wxGrid::SetLabelFont( const wxFont
& font
)
9292 if ( !GetBatchCount() )
9294 m_rowLabelWin
->Refresh();
9295 m_colLabelWin
->Refresh();
9299 void wxGrid::SetRowLabelAlignment( int horiz
, int vert
)
9301 // allow old (incorrect) defs to be used
9304 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
9305 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
9306 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
9311 case wxTOP
: vert
= wxALIGN_TOP
; break;
9312 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
9313 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
9316 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
9318 m_rowLabelHorizAlign
= horiz
;
9321 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
9323 m_rowLabelVertAlign
= vert
;
9326 if ( !GetBatchCount() )
9328 m_rowLabelWin
->Refresh();
9332 void wxGrid::SetColLabelAlignment( int horiz
, int vert
)
9334 // allow old (incorrect) defs to be used
9337 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
9338 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
9339 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
9344 case wxTOP
: vert
= wxALIGN_TOP
; break;
9345 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
9346 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
9349 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
9351 m_colLabelHorizAlign
= horiz
;
9354 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
9356 m_colLabelVertAlign
= vert
;
9359 if ( !GetBatchCount() )
9361 m_colLabelWin
->Refresh();
9365 // Note: under MSW, the default column label font must be changed because it
9366 // does not support vertical printing
9368 // Example: wxFont font(9, wxSWISS, wxNORMAL, wxBOLD);
9369 // pGrid->SetLabelFont(font);
9370 // pGrid->SetColLabelTextOrientation(wxVERTICAL);
9372 void wxGrid::SetColLabelTextOrientation( int textOrientation
)
9374 if ( textOrientation
== wxHORIZONTAL
|| textOrientation
== wxVERTICAL
)
9375 m_colLabelTextOrientation
= textOrientation
;
9377 if ( !GetBatchCount() )
9378 m_colLabelWin
->Refresh();
9381 void wxGrid::SetRowLabelValue( int row
, const wxString
& s
)
9385 m_table
->SetRowLabelValue( row
, s
);
9386 if ( !GetBatchCount() )
9388 wxRect rect
= CellToRect( row
, 0 );
9389 if ( rect
.height
> 0 )
9391 CalcScrolledPosition(0, rect
.y
, &rect
.x
, &rect
.y
);
9393 rect
.width
= m_rowLabelWidth
;
9394 m_rowLabelWin
->Refresh( true, &rect
);
9400 void wxGrid::SetColLabelValue( int col
, const wxString
& s
)
9404 m_table
->SetColLabelValue( col
, s
);
9405 if ( !GetBatchCount() )
9407 wxRect rect
= CellToRect( 0, col
);
9408 if ( rect
.width
> 0 )
9410 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &rect
.y
);
9412 rect
.height
= m_colLabelHeight
;
9413 m_colLabelWin
->Refresh( true, &rect
);
9419 void wxGrid::SetGridLineColour( const wxColour
& colour
)
9421 if ( m_gridLineColour
!= colour
)
9423 m_gridLineColour
= colour
;
9425 wxClientDC
dc( m_gridWin
);
9427 DrawAllGridLines( dc
, wxRegion() );
9431 void wxGrid::SetCellHighlightColour( const wxColour
& colour
)
9433 if ( m_cellHighlightColour
!= colour
)
9435 m_cellHighlightColour
= colour
;
9437 wxClientDC
dc( m_gridWin
);
9439 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
9440 DrawCellHighlight(dc
, attr
);
9445 void wxGrid::SetCellHighlightPenWidth(int width
)
9447 if (m_cellHighlightPenWidth
!= width
)
9449 m_cellHighlightPenWidth
= width
;
9451 // Just redrawing the cell highlight is not enough since that won't
9452 // make any visible change if the the thickness is getting smaller.
9453 int row
= m_currentCellCoords
.GetRow();
9454 int col
= m_currentCellCoords
.GetCol();
9455 if ( row
== -1 || col
== -1 || GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
9458 wxRect rect
= CellToRect(row
, col
);
9459 m_gridWin
->Refresh(true, &rect
);
9463 void wxGrid::SetCellHighlightROPenWidth(int width
)
9465 if (m_cellHighlightROPenWidth
!= width
)
9467 m_cellHighlightROPenWidth
= width
;
9469 // Just redrawing the cell highlight is not enough since that won't
9470 // make any visible change if the the thickness is getting smaller.
9471 int row
= m_currentCellCoords
.GetRow();
9472 int col
= m_currentCellCoords
.GetCol();
9473 if ( row
== -1 || col
== -1 ||
9474 GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
9477 wxRect rect
= CellToRect(row
, col
);
9478 m_gridWin
->Refresh(true, &rect
);
9482 void wxGrid::EnableGridLines( bool enable
)
9484 if ( enable
!= m_gridLinesEnabled
)
9486 m_gridLinesEnabled
= enable
;
9488 if ( !GetBatchCount() )
9492 wxClientDC
dc( m_gridWin
);
9494 DrawAllGridLines( dc
, wxRegion() );
9498 m_gridWin
->Refresh();
9504 int wxGrid::GetDefaultRowSize() const
9506 return m_defaultRowHeight
;
9509 int wxGrid::GetRowSize( int row
) const
9511 wxCHECK_MSG( row
>= 0 && row
< m_numRows
, 0, _T("invalid row index") );
9513 return GetRowHeight(row
);
9516 int wxGrid::GetDefaultColSize() const
9518 return m_defaultColWidth
;
9521 int wxGrid::GetColSize( int col
) const
9523 wxCHECK_MSG( col
>= 0 && col
< m_numCols
, 0, _T("invalid column index") );
9525 return GetColWidth(col
);
9528 // ============================================================================
9529 // access to the grid attributes: each of them has a default value in the grid
9530 // itself and may be overidden on a per-cell basis
9531 // ============================================================================
9533 // ----------------------------------------------------------------------------
9534 // setting default attributes
9535 // ----------------------------------------------------------------------------
9537 void wxGrid::SetDefaultCellBackgroundColour( const wxColour
& col
)
9539 m_defaultCellAttr
->SetBackgroundColour(col
);
9541 m_gridWin
->SetBackgroundColour(col
);
9545 void wxGrid::SetDefaultCellTextColour( const wxColour
& col
)
9547 m_defaultCellAttr
->SetTextColour(col
);
9550 void wxGrid::SetDefaultCellAlignment( int horiz
, int vert
)
9552 m_defaultCellAttr
->SetAlignment(horiz
, vert
);
9555 void wxGrid::SetDefaultCellOverflow( bool allow
)
9557 m_defaultCellAttr
->SetOverflow(allow
);
9560 void wxGrid::SetDefaultCellFont( const wxFont
& font
)
9562 m_defaultCellAttr
->SetFont(font
);
9565 // For editors and renderers the type registry takes precedence over the
9566 // default attr, so we need to register the new editor/renderer for the string
9567 // data type in order to make setting a default editor/renderer appear to
9570 void wxGrid::SetDefaultRenderer(wxGridCellRenderer
*renderer
)
9572 RegisterDataType(wxGRID_VALUE_STRING
,
9574 GetDefaultEditorForType(wxGRID_VALUE_STRING
));
9577 void wxGrid::SetDefaultEditor(wxGridCellEditor
*editor
)
9579 RegisterDataType(wxGRID_VALUE_STRING
,
9580 GetDefaultRendererForType(wxGRID_VALUE_STRING
),
9584 // ----------------------------------------------------------------------------
9585 // access to the default attributes
9586 // ----------------------------------------------------------------------------
9588 wxColour
wxGrid::GetDefaultCellBackgroundColour() const
9590 return m_defaultCellAttr
->GetBackgroundColour();
9593 wxColour
wxGrid::GetDefaultCellTextColour() const
9595 return m_defaultCellAttr
->GetTextColour();
9598 wxFont
wxGrid::GetDefaultCellFont() const
9600 return m_defaultCellAttr
->GetFont();
9603 void wxGrid::GetDefaultCellAlignment( int *horiz
, int *vert
) const
9605 m_defaultCellAttr
->GetAlignment(horiz
, vert
);
9608 bool wxGrid::GetDefaultCellOverflow() const
9610 return m_defaultCellAttr
->GetOverflow();
9613 wxGridCellRenderer
*wxGrid::GetDefaultRenderer() const
9615 return m_defaultCellAttr
->GetRenderer(NULL
, 0, 0);
9618 wxGridCellEditor
*wxGrid::GetDefaultEditor() const
9620 return m_defaultCellAttr
->GetEditor(NULL
, 0, 0);
9623 // ----------------------------------------------------------------------------
9624 // access to cell attributes
9625 // ----------------------------------------------------------------------------
9627 wxColour
wxGrid::GetCellBackgroundColour(int row
, int col
) const
9629 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9630 wxColour colour
= attr
->GetBackgroundColour();
9636 wxColour
wxGrid::GetCellTextColour( int row
, int col
) const
9638 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9639 wxColour colour
= attr
->GetTextColour();
9645 wxFont
wxGrid::GetCellFont( int row
, int col
) const
9647 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9648 wxFont font
= attr
->GetFont();
9654 void wxGrid::GetCellAlignment( int row
, int col
, int *horiz
, int *vert
) const
9656 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9657 attr
->GetAlignment(horiz
, vert
);
9661 bool wxGrid::GetCellOverflow( int row
, int col
) const
9663 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9664 bool allow
= attr
->GetOverflow();
9670 void wxGrid::GetCellSize( int row
, int col
, int *num_rows
, int *num_cols
) const
9672 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9673 attr
->GetSize( num_rows
, num_cols
);
9677 wxGridCellRenderer
* wxGrid::GetCellRenderer(int row
, int col
) const
9679 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9680 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
9686 wxGridCellEditor
* wxGrid::GetCellEditor(int row
, int col
) const
9688 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9689 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
9695 bool wxGrid::IsReadOnly(int row
, int col
) const
9697 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9698 bool isReadOnly
= attr
->IsReadOnly();
9704 // ----------------------------------------------------------------------------
9705 // attribute support: cache, automatic provider creation, ...
9706 // ----------------------------------------------------------------------------
9708 bool wxGrid::CanHaveAttributes() const
9715 return m_table
->CanHaveAttributes();
9718 void wxGrid::ClearAttrCache()
9720 if ( m_attrCache
.row
!= -1 )
9722 wxGridCellAttr
*oldAttr
= m_attrCache
.attr
;
9723 m_attrCache
.attr
= NULL
;
9724 m_attrCache
.row
= -1;
9725 // wxSafeDecRec(...) might cause event processing that accesses
9726 // the cached attribute, if one exists (e.g. by deleting the
9727 // editor stored within the attribute). Therefore it is important
9728 // to invalidate the cache before calling wxSafeDecRef!
9729 wxSafeDecRef(oldAttr
);
9733 void wxGrid::CacheAttr(int row
, int col
, wxGridCellAttr
*attr
) const
9737 wxGrid
*self
= (wxGrid
*)this; // const_cast
9739 self
->ClearAttrCache();
9740 self
->m_attrCache
.row
= row
;
9741 self
->m_attrCache
.col
= col
;
9742 self
->m_attrCache
.attr
= attr
;
9747 bool wxGrid::LookupAttr(int row
, int col
, wxGridCellAttr
**attr
) const
9749 if ( row
== m_attrCache
.row
&& col
== m_attrCache
.col
)
9751 *attr
= m_attrCache
.attr
;
9752 wxSafeIncRef(m_attrCache
.attr
);
9754 #ifdef DEBUG_ATTR_CACHE
9755 gs_nAttrCacheHits
++;
9762 #ifdef DEBUG_ATTR_CACHE
9763 gs_nAttrCacheMisses
++;
9770 wxGridCellAttr
*wxGrid::GetCellAttr(int row
, int col
) const
9772 wxGridCellAttr
*attr
= NULL
;
9773 // Additional test to avoid looking at the cache e.g. for
9774 // wxNoCellCoords, as this will confuse memory management.
9777 if ( !LookupAttr(row
, col
, &attr
) )
9779 attr
= m_table
? m_table
->GetAttr(row
, col
, wxGridCellAttr::Any
)
9781 CacheAttr(row
, col
, attr
);
9787 attr
->SetDefAttr(m_defaultCellAttr
);
9791 attr
= m_defaultCellAttr
;
9798 wxGridCellAttr
*wxGrid::GetOrCreateCellAttr(int row
, int col
) const
9800 wxGridCellAttr
*attr
= NULL
;
9801 bool canHave
= ((wxGrid
*)this)->CanHaveAttributes();
9803 wxCHECK_MSG( canHave
, attr
, _T("Cell attributes not allowed"));
9804 wxCHECK_MSG( m_table
, attr
, _T("must have a table") );
9806 attr
= m_table
->GetAttr(row
, col
, wxGridCellAttr::Cell
);
9809 attr
= new wxGridCellAttr(m_defaultCellAttr
);
9811 // artificially inc the ref count to match DecRef() in caller
9813 m_table
->SetAttr(attr
, row
, col
);
9819 // ----------------------------------------------------------------------------
9820 // setting column attributes (wrappers around SetColAttr)
9821 // ----------------------------------------------------------------------------
9823 void wxGrid::SetColFormatBool(int col
)
9825 SetColFormatCustom(col
, wxGRID_VALUE_BOOL
);
9828 void wxGrid::SetColFormatNumber(int col
)
9830 SetColFormatCustom(col
, wxGRID_VALUE_NUMBER
);
9833 void wxGrid::SetColFormatFloat(int col
, int width
, int precision
)
9835 wxString typeName
= wxGRID_VALUE_FLOAT
;
9836 if ( (width
!= -1) || (precision
!= -1) )
9838 typeName
<< _T(':') << width
<< _T(',') << precision
;
9841 SetColFormatCustom(col
, typeName
);
9844 void wxGrid::SetColFormatCustom(int col
, const wxString
& typeName
)
9846 wxGridCellAttr
*attr
= m_table
->GetAttr(-1, col
, wxGridCellAttr::Col
);
9848 attr
= new wxGridCellAttr
;
9849 wxGridCellRenderer
*renderer
= GetDefaultRendererForType(typeName
);
9850 attr
->SetRenderer(renderer
);
9851 wxGridCellEditor
*editor
= GetDefaultEditorForType(typeName
);
9852 attr
->SetEditor(editor
);
9854 SetColAttr(col
, attr
);
9858 // ----------------------------------------------------------------------------
9859 // setting cell attributes: this is forwarded to the table
9860 // ----------------------------------------------------------------------------
9862 void wxGrid::SetAttr(int row
, int col
, wxGridCellAttr
*attr
)
9864 if ( CanHaveAttributes() )
9866 m_table
->SetAttr(attr
, row
, col
);
9875 void wxGrid::SetRowAttr(int row
, wxGridCellAttr
*attr
)
9877 if ( CanHaveAttributes() )
9879 m_table
->SetRowAttr(attr
, row
);
9888 void wxGrid::SetColAttr(int col
, wxGridCellAttr
*attr
)
9890 if ( CanHaveAttributes() )
9892 m_table
->SetColAttr(attr
, col
);
9901 void wxGrid::SetCellBackgroundColour( int row
, int col
, const wxColour
& colour
)
9903 if ( CanHaveAttributes() )
9905 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9906 attr
->SetBackgroundColour(colour
);
9911 void wxGrid::SetCellTextColour( int row
, int col
, const wxColour
& colour
)
9913 if ( CanHaveAttributes() )
9915 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9916 attr
->SetTextColour(colour
);
9921 void wxGrid::SetCellFont( int row
, int col
, const wxFont
& font
)
9923 if ( CanHaveAttributes() )
9925 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9926 attr
->SetFont(font
);
9931 void wxGrid::SetCellAlignment( int row
, int col
, int horiz
, int vert
)
9933 if ( CanHaveAttributes() )
9935 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9936 attr
->SetAlignment(horiz
, vert
);
9941 void wxGrid::SetCellOverflow( int row
, int col
, bool allow
)
9943 if ( CanHaveAttributes() )
9945 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9946 attr
->SetOverflow(allow
);
9951 void wxGrid::SetCellSize( int row
, int col
, int num_rows
, int num_cols
)
9953 if ( CanHaveAttributes() )
9955 int cell_rows
, cell_cols
;
9957 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9958 attr
->GetSize(&cell_rows
, &cell_cols
);
9959 attr
->SetSize(num_rows
, num_cols
);
9962 // Cannot set the size of a cell to 0 or negative values
9963 // While it is perfectly legal to do that, this function cannot
9964 // handle all the possibilies, do it by hand by getting the CellAttr.
9965 // You can only set the size of a cell to 1,1 or greater with this fn
9966 wxASSERT_MSG( !((cell_rows
< 1) || (cell_cols
< 1)),
9967 wxT("wxGrid::SetCellSize setting cell size that is already part of another cell"));
9968 wxASSERT_MSG( !((num_rows
< 1) || (num_cols
< 1)),
9969 wxT("wxGrid::SetCellSize setting cell size to < 1"));
9971 // if this was already a multicell then "turn off" the other cells first
9972 if ((cell_rows
> 1) || (cell_cols
> 1))
9975 for (j
=row
; j
< row
+ cell_rows
; j
++)
9977 for (i
=col
; i
< col
+ cell_cols
; i
++)
9979 if ((i
!= col
) || (j
!= row
))
9981 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
9982 attr_stub
->SetSize( 1, 1 );
9983 attr_stub
->DecRef();
9989 // mark the cells that will be covered by this cell to
9990 // negative or zero values to point back at this cell
9991 if (((num_rows
> 1) || (num_cols
> 1)) && (num_rows
>= 1) && (num_cols
>= 1))
9994 for (j
=row
; j
< row
+ num_rows
; j
++)
9996 for (i
=col
; i
< col
+ num_cols
; i
++)
9998 if ((i
!= col
) || (j
!= row
))
10000 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
10001 attr_stub
->SetSize( row
- j
, col
- i
);
10002 attr_stub
->DecRef();
10010 void wxGrid::SetCellRenderer(int row
, int col
, wxGridCellRenderer
*renderer
)
10012 if ( CanHaveAttributes() )
10014 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10015 attr
->SetRenderer(renderer
);
10020 void wxGrid::SetCellEditor(int row
, int col
, wxGridCellEditor
* editor
)
10022 if ( CanHaveAttributes() )
10024 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10025 attr
->SetEditor(editor
);
10030 void wxGrid::SetReadOnly(int row
, int col
, bool isReadOnly
)
10032 if ( CanHaveAttributes() )
10034 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10035 attr
->SetReadOnly(isReadOnly
);
10040 // ----------------------------------------------------------------------------
10041 // Data type registration
10042 // ----------------------------------------------------------------------------
10044 void wxGrid::RegisterDataType(const wxString
& typeName
,
10045 wxGridCellRenderer
* renderer
,
10046 wxGridCellEditor
* editor
)
10048 m_typeRegistry
->RegisterDataType(typeName
, renderer
, editor
);
10052 wxGridCellEditor
* wxGrid::GetDefaultEditorForCell(int row
, int col
) const
10054 wxString typeName
= m_table
->GetTypeName(row
, col
);
10055 return GetDefaultEditorForType(typeName
);
10058 wxGridCellRenderer
* wxGrid::GetDefaultRendererForCell(int row
, int col
) const
10060 wxString typeName
= m_table
->GetTypeName(row
, col
);
10061 return GetDefaultRendererForType(typeName
);
10064 wxGridCellEditor
* wxGrid::GetDefaultEditorForType(const wxString
& typeName
) const
10066 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
10067 if ( index
== wxNOT_FOUND
)
10069 wxFAIL_MSG(wxString::Format(wxT("Unknown data type name [%s]"), typeName
.c_str()));
10074 return m_typeRegistry
->GetEditor(index
);
10077 wxGridCellRenderer
* wxGrid::GetDefaultRendererForType(const wxString
& typeName
) const
10079 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
10080 if ( index
== wxNOT_FOUND
)
10082 wxFAIL_MSG(wxString::Format(wxT("Unknown data type name [%s]"), typeName
.c_str()));
10087 return m_typeRegistry
->GetRenderer(index
);
10090 // ----------------------------------------------------------------------------
10092 // ----------------------------------------------------------------------------
10094 void wxGrid::EnableDragRowSize( bool enable
)
10096 m_canDragRowSize
= enable
;
10099 void wxGrid::EnableDragColSize( bool enable
)
10101 m_canDragColSize
= enable
;
10104 void wxGrid::EnableDragGridSize( bool enable
)
10106 m_canDragGridSize
= enable
;
10109 void wxGrid::EnableDragCell( bool enable
)
10111 m_canDragCell
= enable
;
10114 void wxGrid::SetDefaultRowSize( int height
, bool resizeExistingRows
)
10116 m_defaultRowHeight
= wxMax( height
, m_minAcceptableRowHeight
);
10118 if ( resizeExistingRows
)
10120 // since we are resizing all rows to the default row size,
10121 // we can simply clear the row heights and row bottoms
10122 // arrays (which also allows us to take advantage of
10123 // some speed optimisations)
10124 m_rowHeights
.Empty();
10125 m_rowBottoms
.Empty();
10126 if ( !GetBatchCount() )
10131 void wxGrid::SetRowSize( int row
, int height
)
10133 wxCHECK_RET( row
>= 0 && row
< m_numRows
, _T("invalid row index") );
10135 // if < 0 then calculate new height from label
10139 wxArrayString lines
;
10140 wxClientDC
dc(m_rowLabelWin
);
10141 dc
.SetFont(GetLabelFont());
10142 StringToLines(GetRowLabelValue( row
), lines
);
10143 GetTextBoxSize( dc
, lines
, &w
, &h
);
10144 //check that it is not less than the minimal height
10145 height
= wxMax(h
, GetRowMinimalAcceptableHeight());
10148 // See comment in SetColSize
10149 if ( height
< GetRowMinimalAcceptableHeight())
10152 if ( m_rowHeights
.IsEmpty() )
10154 // need to really create the array
10158 int h
= wxMax( 0, height
);
10159 int diff
= h
- m_rowHeights
[row
];
10161 m_rowHeights
[row
] = h
;
10162 for ( int i
= row
; i
< m_numRows
; i
++ )
10164 m_rowBottoms
[i
] += diff
;
10167 if ( !GetBatchCount() )
10171 void wxGrid::SetDefaultColSize( int width
, bool resizeExistingCols
)
10173 // we dont allow zero default column width
10174 m_defaultColWidth
= wxMax( wxMax( width
, m_minAcceptableColWidth
), 1 );
10176 if ( resizeExistingCols
)
10178 // since we are resizing all columns to the default column size,
10179 // we can simply clear the col widths and col rights
10180 // arrays (which also allows us to take advantage of
10181 // some speed optimisations)
10182 m_colWidths
.Empty();
10183 m_colRights
.Empty();
10184 if ( !GetBatchCount() )
10189 void wxGrid::SetColSize( int col
, int width
)
10191 wxCHECK_RET( col
>= 0 && col
< m_numCols
, _T("invalid column index") );
10193 // if < 0 then calculate new width from label
10197 wxArrayString lines
;
10198 wxClientDC
dc(m_colLabelWin
);
10199 dc
.SetFont(GetLabelFont());
10200 StringToLines(GetColLabelValue(col
), lines
);
10201 if ( GetColLabelTextOrientation() == wxHORIZONTAL
)
10202 GetTextBoxSize( dc
, lines
, &w
, &h
);
10204 GetTextBoxSize( dc
, lines
, &h
, &w
);
10206 //check that it is not less than the minimal width
10207 width
= wxMax(width
, GetColMinimalAcceptableWidth());
10210 // should we check that it's bigger than GetColMinimalWidth(col) here?
10212 // No, because it is reasonable to assume the library user know's
10213 // what he is doing. However we should test against the weaker
10214 // constraint of minimalAcceptableWidth, as this breaks rendering
10216 // This test then fixes sf.net bug #645734
10218 if ( width
< GetColMinimalAcceptableWidth() )
10221 if ( m_colWidths
.IsEmpty() )
10223 // need to really create the array
10227 int w
= wxMax( 0, width
);
10228 int diff
= w
- m_colWidths
[col
];
10229 m_colWidths
[col
] = w
;
10231 for ( int colPos
= GetColPos(col
); colPos
< m_numCols
; colPos
++ )
10233 m_colRights
[GetColAt(colPos
)] += diff
;
10236 if ( !GetBatchCount() )
10240 void wxGrid::SetColMinimalWidth( int col
, int width
)
10242 if (width
> GetColMinimalAcceptableWidth())
10244 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)col
;
10245 m_colMinWidths
[key
] = width
;
10249 void wxGrid::SetRowMinimalHeight( int row
, int width
)
10251 if (width
> GetRowMinimalAcceptableHeight())
10253 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)row
;
10254 m_rowMinHeights
[key
] = width
;
10258 int wxGrid::GetColMinimalWidth(int col
) const
10260 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)col
;
10261 wxLongToLongHashMap::const_iterator it
= m_colMinWidths
.find(key
);
10263 return it
!= m_colMinWidths
.end() ? (int)it
->second
: m_minAcceptableColWidth
;
10266 int wxGrid::GetRowMinimalHeight(int row
) const
10268 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)row
;
10269 wxLongToLongHashMap::const_iterator it
= m_rowMinHeights
.find(key
);
10271 return it
!= m_rowMinHeights
.end() ? (int)it
->second
: m_minAcceptableRowHeight
;
10274 void wxGrid::SetColMinimalAcceptableWidth( int width
)
10276 // We do allow a width of 0 since this gives us
10277 // an easy way to temporarily hiding columns.
10279 m_minAcceptableColWidth
= width
;
10282 void wxGrid::SetRowMinimalAcceptableHeight( int height
)
10284 // We do allow a height of 0 since this gives us
10285 // an easy way to temporarily hiding rows.
10287 m_minAcceptableRowHeight
= height
;
10290 int wxGrid::GetColMinimalAcceptableWidth() const
10292 return m_minAcceptableColWidth
;
10295 int wxGrid::GetRowMinimalAcceptableHeight() const
10297 return m_minAcceptableRowHeight
;
10300 // ----------------------------------------------------------------------------
10302 // ----------------------------------------------------------------------------
10305 wxGrid::AutoSizeColOrRow(int colOrRow
, bool setAsMin
, wxGridDirection direction
)
10307 const bool column
= direction
== wxGRID_COLUMN
;
10309 wxClientDC
dc(m_gridWin
);
10311 // cancel editing of cell
10312 HideCellEditControl();
10313 SaveEditControlValue();
10315 // init both of them to avoid compiler warnings, even if we only need one
10323 wxCoord extent
, extentMax
= 0;
10324 int max
= column
? m_numRows
: m_numCols
;
10325 for ( int rowOrCol
= 0; rowOrCol
< max
; rowOrCol
++ )
10332 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
10333 wxGridCellRenderer
*renderer
= attr
->GetRenderer(this, row
, col
);
10336 wxSize size
= renderer
->GetBestSize(*this, *attr
, dc
, row
, col
);
10337 extent
= column
? size
.x
: size
.y
;
10338 if ( extent
> extentMax
)
10339 extentMax
= extent
;
10341 renderer
->DecRef();
10347 // now also compare with the column label extent
10349 dc
.SetFont( GetLabelFont() );
10353 dc
.GetMultiLineTextExtent( GetColLabelValue(col
), &w
, &h
);
10354 if ( GetColLabelTextOrientation() == wxVERTICAL
)
10358 dc
.GetMultiLineTextExtent( GetRowLabelValue(row
), &w
, &h
);
10360 extent
= column
? w
: h
;
10361 if ( extent
> extentMax
)
10362 extentMax
= extent
;
10366 // empty column - give default extent (notice that if extentMax is less
10367 // than default extent but != 0, it's OK)
10368 extentMax
= column
? m_defaultColWidth
: m_defaultRowHeight
;
10373 // leave some space around text
10381 // Ensure automatic width is not less than minimal width. See the
10382 // comment in SetColSize() for explanation of why this isn't done
10383 // in SetColSize().
10385 extentMax
= wxMax(extentMax
, GetColMinimalWidth(col
));
10387 SetColSize( col
, extentMax
);
10388 if ( !GetBatchCount() )
10391 m_gridWin
->GetClientSize( &cw
, &ch
);
10392 wxRect
rect ( CellToRect( 0, col
) );
10394 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
10395 rect
.width
= cw
- rect
.x
;
10396 rect
.height
= m_colLabelHeight
;
10397 m_colLabelWin
->Refresh( true, &rect
);
10402 // Ensure automatic width is not less than minimal height. See the
10403 // comment in SetColSize() for explanation of why this isn't done
10404 // in SetRowSize().
10406 extentMax
= wxMax(extentMax
, GetRowMinimalHeight(row
));
10408 SetRowSize(row
, extentMax
);
10409 if ( !GetBatchCount() )
10412 m_gridWin
->GetClientSize( &cw
, &ch
);
10413 wxRect
rect( CellToRect( row
, 0 ) );
10415 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
10416 rect
.width
= m_rowLabelWidth
;
10417 rect
.height
= ch
- rect
.y
;
10418 m_rowLabelWin
->Refresh( true, &rect
);
10425 SetColMinimalWidth(col
, extentMax
);
10427 SetRowMinimalHeight(row
, extentMax
);
10431 wxCoord
wxGrid::CalcColOrRowLabelAreaMinSize(wxGridDirection direction
)
10433 // calculate size for the rows or columns?
10434 const bool calcRows
= direction
== wxGRID_ROW
;
10436 wxClientDC
dc(calcRows
? GetGridRowLabelWindow()
10437 : GetGridColLabelWindow());
10438 dc
.SetFont(GetLabelFont());
10440 // which dimension should we take into account for calculations?
10442 // for columns, the text can be only horizontal so it's easy but for rows
10443 // we also have to take into account the text orientation
10445 useWidth
= calcRows
|| (GetColLabelTextOrientation() == wxVERTICAL
);
10447 wxArrayString lines
;
10448 wxCoord extentMax
= 0;
10450 const int numRowsOrCols
= calcRows
? m_numRows
: m_numCols
;
10451 for ( int rowOrCol
= 0; rowOrCol
< numRowsOrCols
; rowOrCol
++ )
10455 wxString label
= calcRows
? GetRowLabelValue(rowOrCol
)
10456 : GetColLabelValue(rowOrCol
);
10457 StringToLines(label
, lines
);
10460 GetTextBoxSize(dc
, lines
, &w
, &h
);
10462 const wxCoord extent
= useWidth
? w
: h
;
10463 if ( extent
> extentMax
)
10464 extentMax
= extent
;
10469 // empty column - give default extent (notice that if extentMax is less
10470 // than default extent but != 0, it's OK)
10471 extentMax
= calcRows
? GetDefaultRowLabelSize()
10472 : GetDefaultColLabelSize();
10475 // leave some space around text (taken from AutoSizeColOrRow)
10484 int wxGrid::SetOrCalcColumnSizes(bool calcOnly
, bool setAsMin
)
10486 int width
= m_rowLabelWidth
;
10488 wxGridUpdateLocker locker
;
10490 locker
.Create(this);
10492 for ( int col
= 0; col
< m_numCols
; col
++ )
10495 AutoSizeColumn(col
, setAsMin
);
10497 width
+= GetColWidth(col
);
10503 int wxGrid::SetOrCalcRowSizes(bool calcOnly
, bool setAsMin
)
10505 int height
= m_colLabelHeight
;
10507 wxGridUpdateLocker locker
;
10509 locker
.Create(this);
10511 for ( int row
= 0; row
< m_numRows
; row
++ )
10514 AutoSizeRow(row
, setAsMin
);
10516 height
+= GetRowHeight(row
);
10522 void wxGrid::AutoSize()
10524 wxGridUpdateLocker
locker(this);
10526 wxSize
size(SetOrCalcColumnSizes(false) - m_rowLabelWidth
+ m_extraWidth
,
10527 SetOrCalcRowSizes(false) - m_colLabelHeight
+ m_extraHeight
);
10529 // we know that we're not going to have scrollbars so disable them now to
10530 // avoid trouble in SetClientSize() which can otherwise set the correct
10531 // client size but also leave space for (not needed any more) scrollbars
10532 SetScrollbars(0, 0, 0, 0, 0, 0, true);
10534 // restore the scroll rate parameters overwritten by SetScrollbars()
10535 SetScrollRate(m_scrollLineX
, m_scrollLineY
);
10537 SetClientSize(size
.x
+ m_rowLabelWidth
, size
.y
+ m_colLabelHeight
);
10540 void wxGrid::AutoSizeRowLabelSize( int row
)
10542 // Hide the edit control, so it
10543 // won't interfere with drag-shrinking.
10544 if ( IsCellEditControlShown() )
10546 HideCellEditControl();
10547 SaveEditControlValue();
10550 // autosize row height depending on label text
10551 SetRowSize(row
, -1);
10555 void wxGrid::AutoSizeColLabelSize( int col
)
10557 // Hide the edit control, so it
10558 // won't interfere with drag-shrinking.
10559 if ( IsCellEditControlShown() )
10561 HideCellEditControl();
10562 SaveEditControlValue();
10565 // autosize column width depending on label text
10566 SetColSize(col
, -1);
10570 wxSize
wxGrid::DoGetBestSize() const
10572 wxGrid
*self
= (wxGrid
*)this; // const_cast
10574 // we do the same as in AutoSize() here with the exception that we don't
10575 // change the column/row sizes, only calculate them
10576 wxSize
size(self
->SetOrCalcColumnSizes(true) - m_rowLabelWidth
+ m_extraWidth
,
10577 self
->SetOrCalcRowSizes(true) - m_colLabelHeight
+ m_extraHeight
);
10579 // NOTE: This size should be cached, but first we need to add calls to
10580 // InvalidateBestSize everywhere that could change the results of this
10582 // CacheBestSize(size);
10584 return wxSize(size
.x
+ m_rowLabelWidth
, size
.y
+ m_colLabelHeight
)
10585 + GetWindowBorderSize();
10593 wxPen
& wxGrid::GetDividerPen() const
10598 // ----------------------------------------------------------------------------
10599 // cell value accessor functions
10600 // ----------------------------------------------------------------------------
10602 void wxGrid::SetCellValue( int row
, int col
, const wxString
& s
)
10606 m_table
->SetValue( row
, col
, s
);
10607 if ( !GetBatchCount() )
10610 wxRect
rect( CellToRect( row
, col
) );
10612 rect
.width
= m_gridWin
->GetClientSize().GetWidth();
10613 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
10614 m_gridWin
->Refresh( false, &rect
);
10617 if ( m_currentCellCoords
.GetRow() == row
&&
10618 m_currentCellCoords
.GetCol() == col
&&
10619 IsCellEditControlShown())
10620 // Note: If we are using IsCellEditControlEnabled,
10621 // this interacts badly with calling SetCellValue from
10622 // an EVT_GRID_CELL_CHANGE handler.
10624 HideCellEditControl();
10625 ShowCellEditControl(); // will reread data from table
10630 // ----------------------------------------------------------------------------
10631 // block, row and column selection
10632 // ----------------------------------------------------------------------------
10634 void wxGrid::SelectRow( int row
, bool addToSelected
)
10636 if ( IsSelection() && !addToSelected
)
10640 m_selection
->SelectRow( row
, false, addToSelected
);
10643 void wxGrid::SelectCol( int col
, bool addToSelected
)
10645 if ( IsSelection() && !addToSelected
)
10649 m_selection
->SelectCol( col
, false, addToSelected
);
10652 void wxGrid::SelectBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
,
10653 bool addToSelected
)
10655 if ( IsSelection() && !addToSelected
)
10659 m_selection
->SelectBlock( topRow
, leftCol
, bottomRow
, rightCol
,
10660 false, addToSelected
);
10663 void wxGrid::SelectAll()
10665 if ( m_numRows
> 0 && m_numCols
> 0 )
10668 m_selection
->SelectBlock( 0, 0, m_numRows
- 1, m_numCols
- 1 );
10672 // ----------------------------------------------------------------------------
10673 // cell, row and col deselection
10674 // ----------------------------------------------------------------------------
10676 void wxGrid::DeselectLine(int line
, const wxGridOperations
& oper
)
10678 if ( !m_selection
)
10681 const wxGridSelectionModes mode
= m_selection
->GetSelectionMode();
10682 if ( mode
== oper
.GetSelectionMode() )
10684 const wxGridCellCoords
c(oper
.MakeCoords(line
, 0));
10685 if ( m_selection
->IsInSelection(c
) )
10686 m_selection
->ToggleCellSelection(c
);
10688 else if ( mode
!= oper
.Dual().GetSelectionMode() )
10690 const int nOther
= oper
.Dual().GetNumberOfLines(this);
10691 for ( int i
= 0; i
< nOther
; i
++ )
10693 const wxGridCellCoords
c(oper
.MakeCoords(line
, i
));
10694 if ( m_selection
->IsInSelection(c
) )
10695 m_selection
->ToggleCellSelection(c
);
10698 //else: can only select orthogonal lines so no lines in this direction
10699 // could have been selected anyhow
10702 void wxGrid::DeselectRow(int row
)
10704 DeselectLine(row
, wxGridRowOperations());
10707 void wxGrid::DeselectCol(int col
)
10709 DeselectLine(col
, wxGridColumnOperations());
10712 void wxGrid::DeselectCell( int row
, int col
)
10714 if ( m_selection
&& m_selection
->IsInSelection(row
, col
) )
10715 m_selection
->ToggleCellSelection(row
, col
);
10718 bool wxGrid::IsSelection() const
10720 return ( m_selection
&& (m_selection
->IsSelection() ||
10721 ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
10722 m_selectingBottomRight
!= wxGridNoCellCoords
) ) );
10725 bool wxGrid::IsInSelection( int row
, int col
) const
10727 return ( m_selection
&& (m_selection
->IsInSelection( row
, col
) ||
10728 ( row
>= m_selectingTopLeft
.GetRow() &&
10729 col
>= m_selectingTopLeft
.GetCol() &&
10730 row
<= m_selectingBottomRight
.GetRow() &&
10731 col
<= m_selectingBottomRight
.GetCol() )) );
10734 wxGridCellCoordsArray
wxGrid::GetSelectedCells() const
10738 wxGridCellCoordsArray a
;
10742 return m_selection
->m_cellSelection
;
10745 wxGridCellCoordsArray
wxGrid::GetSelectionBlockTopLeft() const
10749 wxGridCellCoordsArray a
;
10753 return m_selection
->m_blockSelectionTopLeft
;
10756 wxGridCellCoordsArray
wxGrid::GetSelectionBlockBottomRight() const
10760 wxGridCellCoordsArray a
;
10764 return m_selection
->m_blockSelectionBottomRight
;
10767 wxArrayInt
wxGrid::GetSelectedRows() const
10775 return m_selection
->m_rowSelection
;
10778 wxArrayInt
wxGrid::GetSelectedCols() const
10786 return m_selection
->m_colSelection
;
10789 void wxGrid::ClearSelection()
10791 wxRect r1
= BlockToDeviceRect( m_selectingTopLeft
, m_selectingBottomRight
);
10792 wxRect r2
= BlockToDeviceRect( m_currentCellCoords
, m_selectingKeyboard
);
10793 m_selectingTopLeft
=
10794 m_selectingBottomRight
=
10795 m_selectingKeyboard
= wxGridNoCellCoords
;
10796 Refresh( false, &r1
);
10797 Refresh( false, &r2
);
10799 m_selection
->ClearSelection();
10802 // This function returns the rectangle that encloses the given block
10803 // in device coords clipped to the client size of the grid window.
10805 wxRect
wxGrid::BlockToDeviceRect( const wxGridCellCoords
& topLeft
,
10806 const wxGridCellCoords
& bottomRight
) const
10809 wxRect tempCellRect
= CellToRect(topLeft
);
10810 if ( tempCellRect
!= wxGridNoCellRect
)
10812 resultRect
= tempCellRect
;
10816 resultRect
= wxRect(0, 0, 0, 0);
10819 tempCellRect
= CellToRect(bottomRight
);
10820 if ( tempCellRect
!= wxGridNoCellRect
)
10822 resultRect
+= tempCellRect
;
10826 // If both inputs were "wxGridNoCellRect," then there's nothing to do.
10827 return wxGridNoCellRect
;
10830 // Ensure that left/right and top/bottom pairs are in order.
10831 int left
= resultRect
.GetLeft();
10832 int top
= resultRect
.GetTop();
10833 int right
= resultRect
.GetRight();
10834 int bottom
= resultRect
.GetBottom();
10836 int leftCol
= topLeft
.GetCol();
10837 int topRow
= topLeft
.GetRow();
10838 int rightCol
= bottomRight
.GetCol();
10839 int bottomRow
= bottomRight
.GetRow();
10848 leftCol
= rightCol
;
10859 topRow
= bottomRow
;
10863 // The following loop is ONLY necessary to detect and handle merged cells.
10865 m_gridWin
->GetClientSize( &cw
, &ch
);
10867 // Get the origin coordinates: notice that they will be negative if the
10868 // grid is scrolled downwards/to the right.
10869 int gridOriginX
= 0;
10870 int gridOriginY
= 0;
10871 CalcScrolledPosition(gridOriginX
, gridOriginY
, &gridOriginX
, &gridOriginY
);
10873 int onScreenLeftmostCol
= internalXToCol(-gridOriginX
);
10874 int onScreenUppermostRow
= internalYToRow(-gridOriginY
);
10876 int onScreenRightmostCol
= internalXToCol(-gridOriginX
+ cw
);
10877 int onScreenBottommostRow
= internalYToRow(-gridOriginY
+ ch
);
10879 // Bound our loop so that we only examine the portion of the selected block
10880 // that is shown on screen. Therefore, we compare the Top-Left block values
10881 // to the Top-Left screen values, and the Bottom-Right block values to the
10882 // Bottom-Right screen values, choosing appropriately.
10883 const int visibleTopRow
= wxMax(topRow
, onScreenUppermostRow
);
10884 const int visibleBottomRow
= wxMin(bottomRow
, onScreenBottommostRow
);
10885 const int visibleLeftCol
= wxMax(leftCol
, onScreenLeftmostCol
);
10886 const int visibleRightCol
= wxMin(rightCol
, onScreenRightmostCol
);
10888 for ( int j
= visibleTopRow
; j
<= visibleBottomRow
; j
++ )
10890 for ( int i
= visibleLeftCol
; i
<= visibleRightCol
; i
++ )
10892 if ( (j
== visibleTopRow
) || (j
== visibleBottomRow
) ||
10893 (i
== visibleLeftCol
) || (i
== visibleRightCol
) )
10895 tempCellRect
= CellToRect( j
, i
);
10897 if (tempCellRect
.x
< left
)
10898 left
= tempCellRect
.x
;
10899 if (tempCellRect
.y
< top
)
10900 top
= tempCellRect
.y
;
10901 if (tempCellRect
.x
+ tempCellRect
.width
> right
)
10902 right
= tempCellRect
.x
+ tempCellRect
.width
;
10903 if (tempCellRect
.y
+ tempCellRect
.height
> bottom
)
10904 bottom
= tempCellRect
.y
+ tempCellRect
.height
;
10908 i
= visibleRightCol
; // jump over inner cells.
10913 // Convert to scrolled coords
10914 CalcScrolledPosition( left
, top
, &left
, &top
);
10915 CalcScrolledPosition( right
, bottom
, &right
, &bottom
);
10917 if (right
< 0 || bottom
< 0 || left
> cw
|| top
> ch
)
10918 return wxRect(0,0,0,0);
10920 resultRect
.SetLeft( wxMax(0, left
) );
10921 resultRect
.SetTop( wxMax(0, top
) );
10922 resultRect
.SetRight( wxMin(cw
, right
) );
10923 resultRect
.SetBottom( wxMin(ch
, bottom
) );
10928 // ----------------------------------------------------------------------------
10930 // ----------------------------------------------------------------------------
10932 #if wxUSE_DRAG_AND_DROP
10934 // this allow setting drop target directly on wxGrid
10935 void wxGrid::SetDropTarget(wxDropTarget
*dropTarget
)
10937 GetGridWindow()->SetDropTarget(dropTarget
);
10940 #endif // wxUSE_DRAG_AND_DROP
10942 // ----------------------------------------------------------------------------
10943 // grid event classes
10944 // ----------------------------------------------------------------------------
10946 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent
, wxNotifyEvent
)
10948 wxGridEvent::wxGridEvent( int id
, wxEventType type
, wxObject
* obj
,
10949 int row
, int col
, int x
, int y
, bool sel
,
10950 bool control
, bool shift
, bool alt
, bool meta
)
10951 : wxNotifyEvent( type
, id
)
10958 m_control
= control
;
10963 SetEventObject(obj
);
10967 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent
, wxNotifyEvent
)
10969 wxGridSizeEvent::wxGridSizeEvent( int id
, wxEventType type
, wxObject
* obj
,
10970 int rowOrCol
, int x
, int y
,
10971 bool control
, bool shift
, bool alt
, bool meta
)
10972 : wxNotifyEvent( type
, id
)
10974 m_rowOrCol
= rowOrCol
;
10977 m_control
= control
;
10982 SetEventObject(obj
);
10986 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent
, wxNotifyEvent
)
10988 wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id
, wxEventType type
, wxObject
* obj
,
10989 const wxGridCellCoords
& topLeft
,
10990 const wxGridCellCoords
& bottomRight
,
10991 bool sel
, bool control
,
10992 bool shift
, bool alt
, bool meta
)
10993 : wxNotifyEvent( type
, id
)
10995 m_topLeft
= topLeft
;
10996 m_bottomRight
= bottomRight
;
10998 m_control
= control
;
11003 SetEventObject(obj
);
11007 IMPLEMENT_DYNAMIC_CLASS(wxGridEditorCreatedEvent
, wxCommandEvent
)
11009 wxGridEditorCreatedEvent::wxGridEditorCreatedEvent(int id
, wxEventType type
,
11010 wxObject
* obj
, int row
,
11011 int col
, wxControl
* ctrl
)
11012 : wxCommandEvent(type
, id
)
11014 SetEventObject(obj
);
11020 #endif // wxUSE_GRID