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 horizontal or vertical 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;
479 // Draw a horizontal or vertical line across the given rectangle
480 // (this is implemented in terms of above and uses Select() to extract
481 // start and end from the given rectangle)
482 void DrawParallelLineInRect(wxDC
& dc
, const wxRect
& rect
, int pos
) const
484 const int posStart
= Select(rect
.GetPosition());
485 DrawParallelLine(dc
, posStart
, posStart
+ Select(rect
.GetSize()), pos
);
489 // Return the row or column at the given pixel coordinate.
491 PosToLine(const wxGrid
*grid
, int pos
, bool clip
= false) const = 0;
493 // Get the top/left position, in pixels, of the given row or column
494 virtual int GetLineStartPos(const wxGrid
*grid
, int line
) const = 0;
496 // Get the bottom/right position, in pixels, of the given row or column
497 virtual int GetLineEndPos(const wxGrid
*grid
, int line
) const = 0;
499 // Get the height/width of the given row/column
500 virtual int GetLineSize(const wxGrid
*grid
, int line
) const = 0;
502 // Get wxGrid::m_rowBottoms/m_colRights array
503 virtual const wxArrayInt
& GetLineEnds(const wxGrid
*grid
) const = 0;
505 // Get default height row height or column width
506 virtual int GetDefaultLineSize(const wxGrid
*grid
) const = 0;
508 // Return the minimal acceptable row height or column width
509 virtual int GetMinimalAcceptableLineSize(const wxGrid
*grid
) const = 0;
511 // Return the minimal row height or column width
512 virtual int GetMinimalLineSize(const wxGrid
*grid
, int line
) const = 0;
514 // Set the row height or column width
515 virtual void SetLineSize(wxGrid
*grid
, int line
, int size
) const = 0;
517 // True if rows/columns can be resized by user
518 virtual bool CanResizeLines(const wxGrid
*grid
) const = 0;
521 // Return the index of the line at the given position
523 // NB: currently this is always identity for the rows as reordering is only
524 // implemented for the lines
525 virtual int GetLineAt(const wxGrid
*grid
, int line
) const = 0;
528 // Get the row or column label window
529 virtual wxWindow
*GetHeaderWindow(wxGrid
*grid
) const = 0;
531 // Get the width or height of the row or column label window
532 virtual int GetHeaderWindowSize(wxGrid
*grid
) const = 0;
535 // This class is never used polymorphically but give it a virtual dtor
536 // anyhow to suppress g++ complaints about it
537 virtual ~wxGridOperations() { }
540 class wxGridRowOperations
: public wxGridOperations
543 virtual wxGridOperations
& Dual() const;
545 virtual int GetNumberOfLines(const wxGrid
*grid
) const
546 { return grid
->GetNumberRows(); }
548 virtual wxGrid::wxGridSelectionModes
GetSelectionMode() const
549 { return wxGrid::wxGridSelectRows
; }
551 virtual wxGridCellCoords
MakeCoords(int thisDir
, int otherDir
) const
552 { return wxGridCellCoords(thisDir
, otherDir
); }
554 virtual int CalcScrolledPosition(wxGrid
*grid
, int pos
) const
555 { return grid
->CalcScrolledPosition(wxPoint(pos
, 0)).x
; }
557 virtual int Select(const wxGridCellCoords
& c
) const { return c
.GetRow(); }
558 virtual int Select(const wxPoint
& pt
) const { return pt
.x
; }
559 virtual int Select(const wxSize
& sz
) const { return sz
.x
; }
560 virtual int Select(const wxRect
& r
) const { return r
.x
; }
561 virtual int& Select(wxRect
& r
) const { return r
.x
; }
562 virtual int& SelectSize(wxRect
& r
) const { return r
.width
; }
563 virtual wxSize
MakeSize(int first
, int second
) const
564 { return wxSize(first
, second
); }
565 virtual void Set(wxGridCellCoords
& coords
, int line
) const
566 { coords
.SetRow(line
); }
568 virtual void DrawParallelLine(wxDC
& dc
, int start
, int end
, int pos
) const
569 { dc
.DrawLine(start
, pos
, end
, pos
); }
571 virtual int PosToLine(const wxGrid
*grid
, int pos
, bool clip
= false) const
572 { return grid
->YToRow(pos
, clip
); }
573 virtual int GetLineStartPos(const wxGrid
*grid
, int line
) const
574 { return grid
->GetRowTop(line
); }
575 virtual int GetLineEndPos(const wxGrid
*grid
, int line
) const
576 { return grid
->GetRowBottom(line
); }
577 virtual int GetLineSize(const wxGrid
*grid
, int line
) const
578 { return grid
->GetRowHeight(line
); }
579 virtual const wxArrayInt
& GetLineEnds(const wxGrid
*grid
) const
580 { return grid
->m_rowBottoms
; }
581 virtual int GetDefaultLineSize(const wxGrid
*grid
) const
582 { return grid
->GetDefaultRowSize(); }
583 virtual int GetMinimalAcceptableLineSize(const wxGrid
*grid
) const
584 { return grid
->GetRowMinimalAcceptableHeight(); }
585 virtual int GetMinimalLineSize(const wxGrid
*grid
, int line
) const
586 { return grid
->GetRowMinimalHeight(line
); }
587 virtual void SetLineSize(wxGrid
*grid
, int line
, int size
) const
588 { grid
->SetRowSize(line
, size
); }
589 virtual bool CanResizeLines(const wxGrid
*grid
) const
590 { return grid
->CanDragRowSize(); }
592 virtual int GetLineAt(const wxGrid
* WXUNUSED(grid
), int line
) const
593 { return line
; } // TODO: implement row reordering
595 virtual wxWindow
*GetHeaderWindow(wxGrid
*grid
) const
596 { return grid
->GetGridRowLabelWindow(); }
597 virtual int GetHeaderWindowSize(wxGrid
*grid
) const
598 { return grid
->GetRowLabelSize(); }
601 class wxGridColumnOperations
: public wxGridOperations
604 virtual wxGridOperations
& Dual() const;
606 virtual int GetNumberOfLines(const wxGrid
*grid
) const
607 { return grid
->GetNumberCols(); }
609 virtual wxGrid::wxGridSelectionModes
GetSelectionMode() const
610 { return wxGrid::wxGridSelectColumns
; }
612 virtual wxGridCellCoords
MakeCoords(int thisDir
, int otherDir
) const
613 { return wxGridCellCoords(otherDir
, thisDir
); }
615 virtual int CalcScrolledPosition(wxGrid
*grid
, int pos
) const
616 { return grid
->CalcScrolledPosition(wxPoint(0, pos
)).y
; }
618 virtual int Select(const wxGridCellCoords
& c
) const { return c
.GetCol(); }
619 virtual int Select(const wxPoint
& pt
) const { return pt
.y
; }
620 virtual int Select(const wxSize
& sz
) const { return sz
.y
; }
621 virtual int Select(const wxRect
& r
) const { return r
.y
; }
622 virtual int& Select(wxRect
& r
) const { return r
.y
; }
623 virtual int& SelectSize(wxRect
& r
) const { return r
.height
; }
624 virtual wxSize
MakeSize(int first
, int second
) const
625 { return wxSize(second
, first
); }
626 virtual void Set(wxGridCellCoords
& coords
, int line
) const
627 { coords
.SetCol(line
); }
629 virtual void DrawParallelLine(wxDC
& dc
, int start
, int end
, int pos
) const
630 { dc
.DrawLine(pos
, start
, pos
, end
); }
632 virtual int PosToLine(const wxGrid
*grid
, int pos
, bool clip
= false) const
633 { return grid
->XToCol(pos
, clip
); }
634 virtual int GetLineStartPos(const wxGrid
*grid
, int line
) const
635 { return grid
->GetColLeft(line
); }
636 virtual int GetLineEndPos(const wxGrid
*grid
, int line
) const
637 { return grid
->GetColRight(line
); }
638 virtual int GetLineSize(const wxGrid
*grid
, int line
) const
639 { return grid
->GetColWidth(line
); }
640 virtual const wxArrayInt
& GetLineEnds(const wxGrid
*grid
) const
641 { return grid
->m_colRights
; }
642 virtual int GetDefaultLineSize(const wxGrid
*grid
) const
643 { return grid
->GetDefaultColSize(); }
644 virtual int GetMinimalAcceptableLineSize(const wxGrid
*grid
) const
645 { return grid
->GetColMinimalAcceptableWidth(); }
646 virtual int GetMinimalLineSize(const wxGrid
*grid
, int line
) const
647 { return grid
->GetColMinimalWidth(line
); }
648 virtual void SetLineSize(wxGrid
*grid
, int line
, int size
) const
649 { grid
->SetColSize(line
, size
); }
650 virtual bool CanResizeLines(const wxGrid
*grid
) const
651 { return grid
->CanDragColSize(); }
653 virtual int GetLineAt(const wxGrid
*grid
, int line
) const
654 { return grid
->GetColAt(line
); }
656 virtual wxWindow
*GetHeaderWindow(wxGrid
*grid
) const
657 { return grid
->GetGridColLabelWindow(); }
658 virtual int GetHeaderWindowSize(wxGrid
*grid
) const
659 { return grid
->GetColLabelSize(); }
662 wxGridOperations
& wxGridRowOperations::Dual() const
664 static wxGridColumnOperations s_colOper
;
669 wxGridOperations
& wxGridColumnOperations::Dual() const
671 static wxGridRowOperations s_rowOper
;
676 // This class abstracts the difference between operations going forward
677 // (down/right) and backward (up/left) and allows to use the same code for
678 // functions which differ only in the direction of grid traversal
680 // Like wxGridOperations it's an ABC with two concrete subclasses below. Unlike
681 // it, this is a normal object and not just a function dispatch table and has a
684 // Note: the explanation of this discrepancy is the existence of (very useful)
685 // Dual() method in wxGridOperations which forces us to make wxGridOperations a
686 // function dispatcher only.
687 class wxGridDirectionOperations
690 // The oper parameter to ctor selects whether we work with rows or columns
691 wxGridDirectionOperations(wxGrid
*grid
, const wxGridOperations
& oper
)
697 // Check if the component of this point in our direction is at the
698 // boundary, i.e. is the first/last row/column
699 virtual bool IsAtBoundary(const wxGridCellCoords
& coords
) const = 0;
701 // Increment the component of this point in our direction
702 virtual void Advance(wxGridCellCoords
& coords
) const = 0;
704 // Find the line at the given distance, in pixels, away from this one
705 // (this uses clipping, i.e. anything after the last line is counted as the
706 // last one and anything before the first one as 0)
707 virtual int MoveByPixelDistance(int line
, int distance
) const = 0;
709 // This class is never used polymorphically but give it a virtual dtor
710 // anyhow to suppress g++ complaints about it
711 virtual ~wxGridDirectionOperations() { }
714 wxGrid
* const m_grid
;
715 const wxGridOperations
& m_oper
;
718 class wxGridBackwardOperations
: public wxGridDirectionOperations
721 wxGridBackwardOperations(wxGrid
*grid
, const wxGridOperations
& oper
)
722 : wxGridDirectionOperations(grid
, oper
)
726 virtual bool IsAtBoundary(const wxGridCellCoords
& coords
) const
728 wxASSERT_MSG( m_oper
.Select(coords
) >= 0, "invalid row/column" );
730 return m_oper
.Select(coords
) == 0;
733 virtual void Advance(wxGridCellCoords
& coords
) const
735 wxASSERT( !IsAtBoundary(coords
) );
737 m_oper
.Set(coords
, m_oper
.Select(coords
) - 1);
740 virtual int MoveByPixelDistance(int line
, int distance
) const
742 int pos
= m_oper
.GetLineStartPos(m_grid
, line
);
743 return m_oper
.PosToLine(m_grid
, pos
- distance
+ 1, true);
747 class wxGridForwardOperations
: public wxGridDirectionOperations
750 wxGridForwardOperations(wxGrid
*grid
, const wxGridOperations
& oper
)
751 : wxGridDirectionOperations(grid
, oper
),
752 m_numLines(oper
.GetNumberOfLines(grid
))
756 virtual bool IsAtBoundary(const wxGridCellCoords
& coords
) const
758 wxASSERT_MSG( m_oper
.Select(coords
) < m_numLines
, "invalid row/column" );
760 return m_oper
.Select(coords
) == m_numLines
- 1;
763 virtual void Advance(wxGridCellCoords
& coords
) const
765 wxASSERT( !IsAtBoundary(coords
) );
767 m_oper
.Set(coords
, m_oper
.Select(coords
) + 1);
770 virtual int MoveByPixelDistance(int line
, int distance
) const
772 int pos
= m_oper
.GetLineStartPos(m_grid
, line
);
773 return m_oper
.PosToLine(m_grid
, pos
+ distance
, true);
777 const int m_numLines
;
780 // ----------------------------------------------------------------------------
782 // ----------------------------------------------------------------------------
784 //#define DEBUG_ATTR_CACHE
785 #ifdef DEBUG_ATTR_CACHE
786 static size_t gs_nAttrCacheHits
= 0;
787 static size_t gs_nAttrCacheMisses
= 0;
790 // ----------------------------------------------------------------------------
792 // ----------------------------------------------------------------------------
794 wxGridCellCoords
wxGridNoCellCoords( -1, -1 );
795 wxRect
wxGridNoCellRect( -1, -1, -1, -1 );
801 const size_t GRID_SCROLL_LINE_X
= 15;
802 const size_t GRID_SCROLL_LINE_Y
= GRID_SCROLL_LINE_X
;
804 // the size of hash tables used a bit everywhere (the max number of elements
805 // in these hash tables is the number of rows/columns)
806 const int GRID_HASH_SIZE
= 100;
808 // the minimal distance in pixels the mouse needs to move to start a drag
810 const int DRAG_SENSITIVITY
= 3;
812 } // anonymous namespace
814 // ----------------------------------------------------------------------------
816 // ----------------------------------------------------------------------------
821 // ensure that first is less or equal to second, swapping the values if
823 void EnsureFirstLessThanSecond(int& first
, int& second
)
825 if ( first
> second
)
826 wxSwap(first
, second
);
829 } // anonymous namespace
831 // ============================================================================
833 // ============================================================================
835 // ----------------------------------------------------------------------------
837 // ----------------------------------------------------------------------------
839 wxGridCellEditor::wxGridCellEditor()
845 wxGridCellEditor::~wxGridCellEditor()
850 void wxGridCellEditor::Create(wxWindow
* WXUNUSED(parent
),
851 wxWindowID
WXUNUSED(id
),
852 wxEvtHandler
* evtHandler
)
855 m_control
->PushEventHandler(evtHandler
);
858 void wxGridCellEditor::PaintBackground(const wxRect
& rectCell
,
859 wxGridCellAttr
*attr
)
861 // erase the background because we might not fill the cell
862 wxClientDC
dc(m_control
->GetParent());
863 wxGridWindow
* gridWindow
= wxDynamicCast(m_control
->GetParent(), wxGridWindow
);
865 gridWindow
->GetOwner()->PrepareDC(dc
);
867 dc
.SetPen(*wxTRANSPARENT_PEN
);
868 dc
.SetBrush(wxBrush(attr
->GetBackgroundColour()));
869 dc
.DrawRectangle(rectCell
);
871 // redraw the control we just painted over
872 m_control
->Refresh();
875 void wxGridCellEditor::Destroy()
879 m_control
->PopEventHandler( true /* delete it*/ );
881 m_control
->Destroy();
886 void wxGridCellEditor::Show(bool show
, wxGridCellAttr
*attr
)
888 wxASSERT_MSG(m_control
, wxT("The wxGridCellEditor must be created first!"));
890 m_control
->Show(show
);
894 // set the colours/fonts if we have any
897 m_colFgOld
= m_control
->GetForegroundColour();
898 m_control
->SetForegroundColour(attr
->GetTextColour());
900 m_colBgOld
= m_control
->GetBackgroundColour();
901 m_control
->SetBackgroundColour(attr
->GetBackgroundColour());
903 // Workaround for GTK+1 font setting problem on some platforms
904 #if !defined(__WXGTK__) || defined(__WXGTK20__)
905 m_fontOld
= m_control
->GetFont();
906 m_control
->SetFont(attr
->GetFont());
909 // can't do anything more in the base class version, the other
910 // attributes may only be used by the derived classes
915 // restore the standard colours fonts
916 if ( m_colFgOld
.Ok() )
918 m_control
->SetForegroundColour(m_colFgOld
);
919 m_colFgOld
= wxNullColour
;
922 if ( m_colBgOld
.Ok() )
924 m_control
->SetBackgroundColour(m_colBgOld
);
925 m_colBgOld
= wxNullColour
;
928 // Workaround for GTK+1 font setting problem on some platforms
929 #if !defined(__WXGTK__) || defined(__WXGTK20__)
930 if ( m_fontOld
.Ok() )
932 m_control
->SetFont(m_fontOld
);
933 m_fontOld
= wxNullFont
;
939 void wxGridCellEditor::SetSize(const wxRect
& rect
)
941 wxASSERT_MSG(m_control
, wxT("The wxGridCellEditor must be created first!"));
943 m_control
->SetSize(rect
, wxSIZE_ALLOW_MINUS_ONE
);
946 void wxGridCellEditor::HandleReturn(wxKeyEvent
& event
)
951 bool wxGridCellEditor::IsAcceptedKey(wxKeyEvent
& event
)
953 bool ctrl
= event
.ControlDown();
954 bool alt
= event
.AltDown();
957 // On the Mac the Alt key is more like shift and is used for entry of
958 // valid characters, so check for Ctrl and Meta instead.
959 alt
= event
.MetaDown();
962 // Assume it's not a valid char if ctrl or alt is down, but if both are
963 // down then it may be because of an AltGr key combination, so let them
964 // through in that case.
965 if ((ctrl
|| alt
) && !(ctrl
&& alt
))
972 // If it's a F-Key or other special key then it shouldn't start the
974 if (event
.GetKeyCode() >= WXK_START
)
978 // if the unicode key code is not really a unicode character (it may
979 // be a function key or etc., the platforms appear to always give us a
980 // small value in this case) then fallback to the ASCII key code but
981 // don't do anything for function keys or etc.
982 key
= event
.GetUnicodeKey();
985 key
= event
.GetKeyCode();
986 keyOk
= (key
<= 127);
989 key
= event
.GetKeyCode();
990 keyOk
= (key
<= 255);
996 void wxGridCellEditor::StartingKey(wxKeyEvent
& event
)
1001 void wxGridCellEditor::StartingClick()
1007 // ----------------------------------------------------------------------------
1008 // wxGridCellTextEditor
1009 // ----------------------------------------------------------------------------
1011 wxGridCellTextEditor::wxGridCellTextEditor()
1016 void wxGridCellTextEditor::Create(wxWindow
* parent
,
1018 wxEvtHandler
* evtHandler
)
1020 DoCreate(parent
, id
, evtHandler
);
1023 void wxGridCellTextEditor::DoCreate(wxWindow
* parent
,
1025 wxEvtHandler
* evtHandler
,
1028 style
|= wxTE_PROCESS_ENTER
| wxTE_PROCESS_TAB
| wxNO_BORDER
;
1030 m_control
= new wxTextCtrl(parent
, id
, wxEmptyString
,
1031 wxDefaultPosition
, wxDefaultSize
,
1034 // set max length allowed in the textctrl, if the parameter was set
1035 if ( m_maxChars
!= 0 )
1037 Text()->SetMaxLength(m_maxChars
);
1040 wxGridCellEditor::Create(parent
, id
, evtHandler
);
1043 void wxGridCellTextEditor::PaintBackground(const wxRect
& WXUNUSED(rectCell
),
1044 wxGridCellAttr
* WXUNUSED(attr
))
1046 // as we fill the entire client area,
1047 // don't do anything here to minimize flicker
1050 void wxGridCellTextEditor::SetSize(const wxRect
& rectOrig
)
1052 wxRect
rect(rectOrig
);
1054 // Make the edit control large enough to allow for internal margins
1056 // TODO: remove this if the text ctrl sizing is improved esp. for unix
1058 #if defined(__WXGTK__)
1066 #elif defined(__WXMSW__)
1080 int extra_x
= ( rect
.x
> 2 ) ? 2 : 1;
1081 int extra_y
= ( rect
.y
> 2 ) ? 2 : 1;
1083 #if defined(__WXMOTIF__)
1088 rect
.SetLeft( wxMax(0, rect
.x
- extra_x
) );
1089 rect
.SetTop( wxMax(0, rect
.y
- extra_y
) );
1090 rect
.SetRight( rect
.GetRight() + 2 * extra_x
);
1091 rect
.SetBottom( rect
.GetBottom() + 2 * extra_y
);
1094 wxGridCellEditor::SetSize(rect
);
1097 void wxGridCellTextEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1099 wxASSERT_MSG(m_control
, wxT("The wxGridCellEditor must be created first!"));
1101 m_startValue
= grid
->GetTable()->GetValue(row
, col
);
1103 DoBeginEdit(m_startValue
);
1106 void wxGridCellTextEditor::DoBeginEdit(const wxString
& startValue
)
1108 Text()->SetValue(startValue
);
1109 Text()->SetInsertionPointEnd();
1110 Text()->SetSelection(-1, -1);
1114 bool wxGridCellTextEditor::EndEdit(int row
, int col
, wxGrid
* grid
)
1116 wxASSERT_MSG(m_control
, wxT("The wxGridCellEditor must be created first!"));
1118 bool changed
= false;
1119 wxString value
= Text()->GetValue();
1120 if (value
!= m_startValue
)
1124 grid
->GetTable()->SetValue(row
, col
, value
);
1126 m_startValue
= wxEmptyString
;
1128 // No point in setting the text of the hidden control
1129 //Text()->SetValue(m_startValue);
1134 void wxGridCellTextEditor::Reset()
1136 wxASSERT_MSG(m_control
, wxT("The wxGridCellEditor must be created first!"));
1138 DoReset(m_startValue
);
1141 void wxGridCellTextEditor::DoReset(const wxString
& startValue
)
1143 Text()->SetValue(startValue
);
1144 Text()->SetInsertionPointEnd();
1147 bool wxGridCellTextEditor::IsAcceptedKey(wxKeyEvent
& event
)
1149 return wxGridCellEditor::IsAcceptedKey(event
);
1152 void wxGridCellTextEditor::StartingKey(wxKeyEvent
& event
)
1154 // Since this is now happening in the EVT_CHAR event EmulateKeyPress is no
1155 // longer an appropriate way to get the character into the text control.
1156 // Do it ourselves instead. We know that if we get this far that we have
1157 // a valid character, so not a whole lot of testing needs to be done.
1159 wxTextCtrl
* tc
= Text();
1164 ch
= event
.GetUnicodeKey();
1166 ch
= (wxChar
)event
.GetKeyCode();
1168 ch
= (wxChar
)event
.GetKeyCode();
1174 // delete the character at the cursor
1175 pos
= tc
->GetInsertionPoint();
1176 if (pos
< tc
->GetLastPosition())
1177 tc
->Remove(pos
, pos
+ 1);
1181 // delete the character before the cursor
1182 pos
= tc
->GetInsertionPoint();
1184 tc
->Remove(pos
- 1, pos
);
1193 void wxGridCellTextEditor::HandleReturn( wxKeyEvent
&
1194 WXUNUSED_GTK(WXUNUSED_MOTIF(event
)) )
1196 #if defined(__WXMOTIF__) || defined(__WXGTK__)
1197 // wxMotif needs a little extra help...
1198 size_t pos
= (size_t)( Text()->GetInsertionPoint() );
1199 wxString
s( Text()->GetValue() );
1200 s
= s
.Left(pos
) + wxT("\n") + s
.Mid(pos
);
1201 Text()->SetValue(s
);
1202 Text()->SetInsertionPoint( pos
);
1204 // the other ports can handle a Return key press
1210 void wxGridCellTextEditor::SetParameters(const wxString
& params
)
1220 if ( params
.ToLong(&tmp
) )
1222 m_maxChars
= (size_t)tmp
;
1226 wxLogDebug( _T("Invalid wxGridCellTextEditor parameter string '%s' ignored"), params
.c_str() );
1231 // return the value in the text control
1232 wxString
wxGridCellTextEditor::GetValue() const
1234 return Text()->GetValue();
1237 // ----------------------------------------------------------------------------
1238 // wxGridCellNumberEditor
1239 // ----------------------------------------------------------------------------
1241 wxGridCellNumberEditor::wxGridCellNumberEditor(int min
, int max
)
1247 void wxGridCellNumberEditor::Create(wxWindow
* parent
,
1249 wxEvtHandler
* evtHandler
)
1254 // create a spin ctrl
1255 m_control
= new wxSpinCtrl(parent
, wxID_ANY
, wxEmptyString
,
1256 wxDefaultPosition
, wxDefaultSize
,
1260 wxGridCellEditor::Create(parent
, id
, evtHandler
);
1265 // just a text control
1266 wxGridCellTextEditor::Create(parent
, id
, evtHandler
);
1268 #if wxUSE_VALIDATORS
1269 Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC
));
1274 void wxGridCellNumberEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1276 // first get the value
1277 wxGridTableBase
*table
= grid
->GetTable();
1278 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
1280 m_valueOld
= table
->GetValueAsLong(row
, col
);
1285 wxString sValue
= table
->GetValue(row
, col
);
1286 if (! sValue
.ToLong(&m_valueOld
) && ! sValue
.empty())
1288 wxFAIL_MSG( _T("this cell doesn't have numeric value") );
1296 Spin()->SetValue((int)m_valueOld
);
1302 DoBeginEdit(GetString());
1306 bool wxGridCellNumberEditor::EndEdit(int row
, int col
,
1315 value
= Spin()->GetValue();
1316 if ( value
== m_valueOld
)
1319 text
.Printf(wxT("%ld"), value
);
1321 else // using unconstrained input
1322 #endif // wxUSE_SPINCTRL
1324 const wxString
textOld(grid
->GetCellValue(row
, col
));
1325 text
= Text()->GetValue();
1328 if ( textOld
.empty() )
1331 else // non-empty text now (maybe 0)
1333 if ( !text
.ToLong(&value
) )
1336 // if value == m_valueOld == 0 but old text was "" and new one is
1337 // "0" something still did change
1338 if ( value
== m_valueOld
&& (value
|| !textOld
.empty()) )
1343 wxGridTableBase
* const table
= grid
->GetTable();
1344 if ( table
->CanSetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
1345 table
->SetValueAsLong(row
, col
, value
);
1347 table
->SetValue(row
, col
, text
);
1352 void wxGridCellNumberEditor::Reset()
1357 Spin()->SetValue((int)m_valueOld
);
1362 DoReset(GetString());
1366 bool wxGridCellNumberEditor::IsAcceptedKey(wxKeyEvent
& event
)
1368 if ( wxGridCellEditor::IsAcceptedKey(event
) )
1370 int keycode
= event
.GetKeyCode();
1371 if ( (keycode
< 128) &&
1372 (wxIsdigit(keycode
) || keycode
== '+' || keycode
== '-'))
1381 void wxGridCellNumberEditor::StartingKey(wxKeyEvent
& event
)
1383 int keycode
= event
.GetKeyCode();
1386 if ( wxIsdigit(keycode
) || keycode
== '+' || keycode
== '-')
1388 wxGridCellTextEditor::StartingKey(event
);
1390 // skip Skip() below
1397 if ( wxIsdigit(keycode
) )
1399 wxSpinCtrl
* spin
= (wxSpinCtrl
*)m_control
;
1400 spin
->SetValue(keycode
- '0');
1401 spin
->SetSelection(1,1);
1410 void wxGridCellNumberEditor::SetParameters(const wxString
& params
)
1421 if ( params
.BeforeFirst(_T(',')).ToLong(&tmp
) )
1425 if ( params
.AfterFirst(_T(',')).ToLong(&tmp
) )
1429 // skip the error message below
1434 wxLogDebug(_T("Invalid wxGridCellNumberEditor parameter string '%s' ignored"), params
.c_str());
1438 // return the value in the spin control if it is there (the text control otherwise)
1439 wxString
wxGridCellNumberEditor::GetValue() const
1446 long value
= Spin()->GetValue();
1447 s
.Printf(wxT("%ld"), value
);
1452 s
= Text()->GetValue();
1458 // ----------------------------------------------------------------------------
1459 // wxGridCellFloatEditor
1460 // ----------------------------------------------------------------------------
1462 wxGridCellFloatEditor::wxGridCellFloatEditor(int width
, int precision
)
1465 m_precision
= precision
;
1468 void wxGridCellFloatEditor::Create(wxWindow
* parent
,
1470 wxEvtHandler
* evtHandler
)
1472 wxGridCellTextEditor::Create(parent
, id
, evtHandler
);
1474 #if wxUSE_VALIDATORS
1475 Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC
));
1479 void wxGridCellFloatEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1481 // first get the value
1482 wxGridTableBase
* const table
= grid
->GetTable();
1483 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
1485 m_valueOld
= table
->GetValueAsDouble(row
, col
);
1491 const wxString value
= table
->GetValue(row
, col
);
1492 if ( !value
.empty() )
1494 if ( !value
.ToDouble(&m_valueOld
) )
1496 wxFAIL_MSG( _T("this cell doesn't have float value") );
1502 DoBeginEdit(GetString());
1505 bool wxGridCellFloatEditor::EndEdit(int row
, int col
, wxGrid
* grid
)
1507 const wxString
text(Text()->GetValue()),
1508 textOld(grid
->GetCellValue(row
, col
));
1511 if ( !text
.empty() )
1513 if ( !text
.ToDouble(&value
) )
1516 else // new value is empty string
1518 if ( textOld
.empty() )
1519 return false; // nothing changed
1524 // the test for empty strings ensures that we don't skip the value setting
1525 // when "" is replaced by "0" or vice versa as "" numeric value is also 0.
1526 if ( wxIsSameDouble(value
, m_valueOld
) && !text
.empty() && !textOld
.empty() )
1527 return false; // nothing changed
1529 wxGridTableBase
* const table
= grid
->GetTable();
1531 if ( table
->CanSetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
1532 table
->SetValueAsDouble(row
, col
, value
);
1534 table
->SetValue(row
, col
, text
);
1539 void wxGridCellFloatEditor::Reset()
1541 DoReset(GetString());
1544 void wxGridCellFloatEditor::StartingKey(wxKeyEvent
& event
)
1546 int keycode
= event
.GetKeyCode();
1548 tmpbuf
[0] = (char) keycode
;
1550 wxString
strbuf(tmpbuf
, *wxConvCurrent
);
1553 bool is_decimal_point
= ( strbuf
==
1554 wxLocale::GetInfo(wxLOCALE_DECIMAL_POINT
, wxLOCALE_CAT_NUMBER
) );
1556 bool is_decimal_point
= ( strbuf
== _T(".") );
1559 if ( wxIsdigit(keycode
) || keycode
== '+' || keycode
== '-'
1560 || is_decimal_point
)
1562 wxGridCellTextEditor::StartingKey(event
);
1564 // skip Skip() below
1571 void wxGridCellFloatEditor::SetParameters(const wxString
& params
)
1582 if ( params
.BeforeFirst(_T(',')).ToLong(&tmp
) )
1586 if ( params
.AfterFirst(_T(',')).ToLong(&tmp
) )
1588 m_precision
= (int)tmp
;
1590 // skip the error message below
1595 wxLogDebug(_T("Invalid wxGridCellFloatEditor parameter string '%s' ignored"), params
.c_str());
1599 wxString
wxGridCellFloatEditor::GetString() const
1602 if ( m_precision
== -1 && m_width
!= -1)
1604 // default precision
1605 fmt
.Printf(_T("%%%d.f"), m_width
);
1607 else if ( m_precision
!= -1 && m_width
== -1)
1610 fmt
.Printf(_T("%%.%df"), m_precision
);
1612 else if ( m_precision
!= -1 && m_width
!= -1 )
1614 fmt
.Printf(_T("%%%d.%df"), m_width
, m_precision
);
1618 // default width/precision
1622 return wxString::Format(fmt
, m_valueOld
);
1625 bool wxGridCellFloatEditor::IsAcceptedKey(wxKeyEvent
& event
)
1627 if ( wxGridCellEditor::IsAcceptedKey(event
) )
1629 const int keycode
= event
.GetKeyCode();
1630 if ( isascii(keycode
) )
1633 tmpbuf
[0] = (char) keycode
;
1635 wxString
strbuf(tmpbuf
, *wxConvCurrent
);
1638 const wxString decimalPoint
=
1639 wxLocale::GetInfo(wxLOCALE_DECIMAL_POINT
, wxLOCALE_CAT_NUMBER
);
1641 const wxString
decimalPoint(_T('.'));
1644 // accept digits, 'e' as in '1e+6', also '-', '+', and '.'
1645 if ( wxIsdigit(keycode
) ||
1646 tolower(keycode
) == 'e' ||
1647 keycode
== decimalPoint
||
1659 #endif // wxUSE_TEXTCTRL
1663 // ----------------------------------------------------------------------------
1664 // wxGridCellBoolEditor
1665 // ----------------------------------------------------------------------------
1667 // the default values for GetValue()
1668 wxString
wxGridCellBoolEditor::ms_stringValues
[2] = { _T(""), _T("1") };
1670 void wxGridCellBoolEditor::Create(wxWindow
* parent
,
1672 wxEvtHandler
* evtHandler
)
1674 m_control
= new wxCheckBox(parent
, id
, wxEmptyString
,
1675 wxDefaultPosition
, wxDefaultSize
,
1678 wxGridCellEditor::Create(parent
, id
, evtHandler
);
1681 void wxGridCellBoolEditor::SetSize(const wxRect
& r
)
1683 bool resize
= false;
1684 wxSize size
= m_control
->GetSize();
1685 wxCoord minSize
= wxMin(r
.width
, r
.height
);
1687 // check if the checkbox is not too big/small for this cell
1688 wxSize sizeBest
= m_control
->GetBestSize();
1689 if ( !(size
== sizeBest
) )
1691 // reset to default size if it had been made smaller
1697 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
1699 // leave 1 pixel margin
1700 size
.x
= size
.y
= minSize
- 2;
1707 m_control
->SetSize(size
);
1710 // position it in the centre of the rectangle (TODO: support alignment?)
1712 #if defined(__WXGTK__) || defined (__WXMOTIF__)
1713 // the checkbox without label still has some space to the right in wxGTK,
1714 // so shift it to the right
1716 #elif defined(__WXMSW__)
1717 // here too, but in other way
1722 int hAlign
= wxALIGN_CENTRE
;
1723 int vAlign
= wxALIGN_CENTRE
;
1725 GetCellAttr()->GetAlignment(& hAlign
, & vAlign
);
1728 if (hAlign
== wxALIGN_LEFT
)
1736 y
= r
.y
+ r
.height
/ 2 - size
.y
/ 2;
1738 else if (hAlign
== wxALIGN_RIGHT
)
1740 x
= r
.x
+ r
.width
- size
.x
- 2;
1741 y
= r
.y
+ r
.height
/ 2 - size
.y
/ 2;
1743 else if (hAlign
== wxALIGN_CENTRE
)
1745 x
= r
.x
+ r
.width
/ 2 - size
.x
/ 2;
1746 y
= r
.y
+ r
.height
/ 2 - size
.y
/ 2;
1749 m_control
->Move(x
, y
);
1752 void wxGridCellBoolEditor::Show(bool show
, wxGridCellAttr
*attr
)
1754 m_control
->Show(show
);
1758 wxColour colBg
= attr
? attr
->GetBackgroundColour() : *wxLIGHT_GREY
;
1759 CBox()->SetBackgroundColour(colBg
);
1763 void wxGridCellBoolEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1765 wxASSERT_MSG(m_control
,
1766 wxT("The wxGridCellEditor must be created first!"));
1768 if (grid
->GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
))
1770 m_startValue
= grid
->GetTable()->GetValueAsBool(row
, col
);
1774 wxString
cellval( grid
->GetTable()->GetValue(row
, col
) );
1776 if ( cellval
== ms_stringValues
[false] )
1777 m_startValue
= false;
1778 else if ( cellval
== ms_stringValues
[true] )
1779 m_startValue
= true;
1782 // do not try to be smart here and convert it to true or false
1783 // because we'll still overwrite it with something different and
1784 // this risks to be very surprising for the user code, let them
1786 wxFAIL_MSG( _T("invalid value for a cell with bool editor!") );
1790 CBox()->SetValue(m_startValue
);
1794 bool wxGridCellBoolEditor::EndEdit(int row
, int col
,
1797 wxASSERT_MSG(m_control
,
1798 wxT("The wxGridCellEditor must be created first!"));
1800 bool changed
= false;
1801 bool value
= CBox()->GetValue();
1802 if ( value
!= m_startValue
)
1807 wxGridTableBase
* const table
= grid
->GetTable();
1808 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
) )
1809 table
->SetValueAsBool(row
, col
, value
);
1811 table
->SetValue(row
, col
, GetValue());
1817 void wxGridCellBoolEditor::Reset()
1819 wxASSERT_MSG(m_control
,
1820 wxT("The wxGridCellEditor must be created first!"));
1822 CBox()->SetValue(m_startValue
);
1825 void wxGridCellBoolEditor::StartingClick()
1827 CBox()->SetValue(!CBox()->GetValue());
1830 bool wxGridCellBoolEditor::IsAcceptedKey(wxKeyEvent
& event
)
1832 if ( wxGridCellEditor::IsAcceptedKey(event
) )
1834 int keycode
= event
.GetKeyCode();
1847 void wxGridCellBoolEditor::StartingKey(wxKeyEvent
& event
)
1849 int keycode
= event
.GetKeyCode();
1853 CBox()->SetValue(!CBox()->GetValue());
1857 CBox()->SetValue(true);
1861 CBox()->SetValue(false);
1866 wxString
wxGridCellBoolEditor::GetValue() const
1868 return ms_stringValues
[CBox()->GetValue()];
1872 wxGridCellBoolEditor::UseStringValues(const wxString
& valueTrue
,
1873 const wxString
& valueFalse
)
1875 ms_stringValues
[false] = valueFalse
;
1876 ms_stringValues
[true] = valueTrue
;
1880 wxGridCellBoolEditor::IsTrueValue(const wxString
& value
)
1882 return value
== ms_stringValues
[true];
1885 #endif // wxUSE_CHECKBOX
1889 // ----------------------------------------------------------------------------
1890 // wxGridCellChoiceEditor
1891 // ----------------------------------------------------------------------------
1893 wxGridCellChoiceEditor::wxGridCellChoiceEditor(const wxArrayString
& choices
,
1895 : m_choices(choices
),
1896 m_allowOthers(allowOthers
) { }
1898 wxGridCellChoiceEditor::wxGridCellChoiceEditor(size_t count
,
1899 const wxString choices
[],
1901 : m_allowOthers(allowOthers
)
1905 m_choices
.Alloc(count
);
1906 for ( size_t n
= 0; n
< count
; n
++ )
1908 m_choices
.Add(choices
[n
]);
1913 wxGridCellEditor
*wxGridCellChoiceEditor::Clone() const
1915 wxGridCellChoiceEditor
*editor
= new wxGridCellChoiceEditor
;
1916 editor
->m_allowOthers
= m_allowOthers
;
1917 editor
->m_choices
= m_choices
;
1922 void wxGridCellChoiceEditor::Create(wxWindow
* parent
,
1924 wxEvtHandler
* evtHandler
)
1926 int style
= wxTE_PROCESS_ENTER
|
1930 if ( !m_allowOthers
)
1931 style
|= wxCB_READONLY
;
1932 m_control
= new wxComboBox(parent
, id
, wxEmptyString
,
1933 wxDefaultPosition
, wxDefaultSize
,
1937 wxGridCellEditor::Create(parent
, id
, evtHandler
);
1940 void wxGridCellChoiceEditor::PaintBackground(const wxRect
& rectCell
,
1941 wxGridCellAttr
* attr
)
1943 // as we fill the entire client area, don't do anything here to minimize
1946 // TODO: It doesn't actually fill the client area since the height of a
1947 // combo always defaults to the standard. Until someone has time to
1948 // figure out the right rectangle to paint, just do it the normal way.
1949 wxGridCellEditor::PaintBackground(rectCell
, attr
);
1952 void wxGridCellChoiceEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1954 wxASSERT_MSG(m_control
,
1955 wxT("The wxGridCellEditor must be created first!"));
1957 wxGridCellEditorEvtHandler
* evtHandler
= NULL
;
1959 evtHandler
= wxDynamicCast(m_control
->GetEventHandler(), wxGridCellEditorEvtHandler
);
1961 // Don't immediately end if we get a kill focus event within BeginEdit
1963 evtHandler
->SetInSetFocus(true);
1965 m_startValue
= grid
->GetTable()->GetValue(row
, col
);
1967 Reset(); // this updates combo box to correspond to m_startValue
1969 Combo()->SetFocus();
1973 // When dropping down the menu, a kill focus event
1974 // happens after this point, so we can't reset the flag yet.
1975 #if !defined(__WXGTK20__)
1976 evtHandler
->SetInSetFocus(false);
1981 bool wxGridCellChoiceEditor::EndEdit(int row
, int col
,
1984 wxString value
= Combo()->GetValue();
1985 if ( value
== m_startValue
)
1988 grid
->GetTable()->SetValue(row
, col
, value
);
1993 void wxGridCellChoiceEditor::Reset()
1997 Combo()->SetValue(m_startValue
);
1998 Combo()->SetInsertionPointEnd();
2000 else // the combobox is read-only
2002 // find the right position, or default to the first if not found
2003 int pos
= Combo()->FindString(m_startValue
);
2004 if (pos
== wxNOT_FOUND
)
2006 Combo()->SetSelection(pos
);
2010 void wxGridCellChoiceEditor::SetParameters(const wxString
& params
)
2020 wxStringTokenizer
tk(params
, _T(','));
2021 while ( tk
.HasMoreTokens() )
2023 m_choices
.Add(tk
.GetNextToken());
2027 // return the value in the text control
2028 wxString
wxGridCellChoiceEditor::GetValue() const
2030 return Combo()->GetValue();
2033 #endif // wxUSE_COMBOBOX
2035 // ----------------------------------------------------------------------------
2036 // wxGridCellEditorEvtHandler
2037 // ----------------------------------------------------------------------------
2039 void wxGridCellEditorEvtHandler::OnKillFocus(wxFocusEvent
& event
)
2041 // Don't disable the cell if we're just starting to edit it
2046 m_grid
->DisableCellEditControl();
2051 void wxGridCellEditorEvtHandler::OnKeyDown(wxKeyEvent
& event
)
2053 switch ( event
.GetKeyCode() )
2057 m_grid
->DisableCellEditControl();
2061 m_grid
->GetEventHandler()->ProcessEvent( event
);
2065 case WXK_NUMPAD_ENTER
:
2066 if (!m_grid
->GetEventHandler()->ProcessEvent(event
))
2067 m_editor
->HandleReturn(event
);
2076 void wxGridCellEditorEvtHandler::OnChar(wxKeyEvent
& event
)
2078 int row
= m_grid
->GetGridCursorRow();
2079 int col
= m_grid
->GetGridCursorCol();
2080 wxRect rect
= m_grid
->CellToRect( row
, col
);
2082 m_grid
->GetGridWindow()->GetClientSize( &cw
, &ch
);
2084 // if cell width is smaller than grid client area, cell is wholly visible
2085 bool wholeCellVisible
= (rect
.GetWidth() < cw
);
2087 switch ( event
.GetKeyCode() )
2092 case WXK_NUMPAD_ENTER
:
2097 if ( wholeCellVisible
)
2099 // no special processing needed...
2104 // do special processing for partly visible cell...
2106 // get the widths of all cells previous to this one
2108 for ( int i
= 0; i
< col
; i
++ )
2110 colXPos
+= m_grid
->GetColSize(i
);
2113 int xUnit
= 1, yUnit
= 1;
2114 m_grid
->GetScrollPixelsPerUnit(&xUnit
, &yUnit
);
2117 m_grid
->Scroll(colXPos
/ xUnit
- 1, m_grid
->GetScrollPos(wxVERTICAL
));
2121 m_grid
->Scroll(colXPos
/ xUnit
, m_grid
->GetScrollPos(wxVERTICAL
));
2129 if ( wholeCellVisible
)
2131 // no special processing needed...
2136 // do special processing for partly visible cell...
2139 wxString value
= m_grid
->GetCellValue(row
, col
);
2140 if ( wxEmptyString
!= value
)
2142 // get width of cell CONTENTS (text)
2144 wxFont font
= m_grid
->GetCellFont(row
, col
);
2145 m_grid
->GetTextExtent(value
, &textWidth
, &y
, NULL
, NULL
, &font
);
2147 // try to RIGHT align the text by scrolling
2148 int client_right
= m_grid
->GetGridWindow()->GetClientSize().GetWidth();
2150 // (m_grid->GetScrollLineX()*2) is a factor for not scrolling to far,
2151 // otherwise the last part of the cell content might be hidden below the scroll bar
2152 // FIXME: maybe there is a more suitable correction?
2153 textWidth
-= (client_right
- (m_grid
->GetScrollLineX() * 2));
2154 if ( textWidth
< 0 )
2160 // get the widths of all cells previous to this one
2162 for ( int i
= 0; i
< col
; i
++ )
2164 colXPos
+= m_grid
->GetColSize(i
);
2167 // and add the (modified) text width of the cell contents
2168 // as we'd like to see the last part of the cell contents
2169 colXPos
+= textWidth
;
2171 int xUnit
= 1, yUnit
= 1;
2172 m_grid
->GetScrollPixelsPerUnit(&xUnit
, &yUnit
);
2173 m_grid
->Scroll(colXPos
/ xUnit
- 1, m_grid
->GetScrollPos(wxVERTICAL
));
2184 // ----------------------------------------------------------------------------
2185 // wxGridCellWorker is an (almost) empty common base class for
2186 // wxGridCellRenderer and wxGridCellEditor managing ref counting
2187 // ----------------------------------------------------------------------------
2189 void wxGridCellWorker::SetParameters(const wxString
& WXUNUSED(params
))
2194 wxGridCellWorker::~wxGridCellWorker()
2198 // ============================================================================
2200 // ============================================================================
2202 // ----------------------------------------------------------------------------
2203 // wxGridCellRenderer
2204 // ----------------------------------------------------------------------------
2206 void wxGridCellRenderer::Draw(wxGrid
& grid
,
2207 wxGridCellAttr
& attr
,
2210 int WXUNUSED(row
), int WXUNUSED(col
),
2213 dc
.SetBackgroundMode( wxBRUSHSTYLE_SOLID
);
2216 if ( grid
.IsEnabled() )
2220 if ( grid
.HasFocus() )
2221 clr
= grid
.GetSelectionBackground();
2223 clr
= wxSystemSettings::GetColour(wxSYS_COLOUR_BTNSHADOW
);
2227 clr
= attr
.GetBackgroundColour();
2230 else // grey out fields if the grid is disabled
2232 clr
= wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
);
2236 dc
.SetPen( *wxTRANSPARENT_PEN
);
2237 dc
.DrawRectangle(rect
);
2240 // ----------------------------------------------------------------------------
2241 // wxGridCellStringRenderer
2242 // ----------------------------------------------------------------------------
2244 void wxGridCellStringRenderer::SetTextColoursAndFont(const wxGrid
& grid
,
2245 const wxGridCellAttr
& attr
,
2249 dc
.SetBackgroundMode( wxBRUSHSTYLE_TRANSPARENT
);
2251 // TODO some special colours for attr.IsReadOnly() case?
2253 // different coloured text when the grid is disabled
2254 if ( grid
.IsEnabled() )
2259 if ( grid
.HasFocus() )
2260 clr
= grid
.GetSelectionBackground();
2262 clr
= wxSystemSettings::GetColour(wxSYS_COLOUR_BTNSHADOW
);
2263 dc
.SetTextBackground( clr
);
2264 dc
.SetTextForeground( grid
.GetSelectionForeground() );
2268 dc
.SetTextBackground( attr
.GetBackgroundColour() );
2269 dc
.SetTextForeground( attr
.GetTextColour() );
2274 dc
.SetTextBackground(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
));
2275 dc
.SetTextForeground(wxSystemSettings::GetColour(wxSYS_COLOUR_GRAYTEXT
));
2278 dc
.SetFont( attr
.GetFont() );
2281 wxSize
wxGridCellStringRenderer::DoGetBestSize(const wxGridCellAttr
& attr
,
2283 const wxString
& text
)
2285 wxCoord x
= 0, y
= 0, max_x
= 0;
2286 dc
.SetFont(attr
.GetFont());
2287 wxStringTokenizer
tk(text
, _T('\n'));
2288 while ( tk
.HasMoreTokens() )
2290 dc
.GetTextExtent(tk
.GetNextToken(), &x
, &y
);
2291 max_x
= wxMax(max_x
, x
);
2294 y
*= 1 + text
.Freq(wxT('\n')); // multiply by the number of lines.
2296 return wxSize(max_x
, y
);
2299 wxSize
wxGridCellStringRenderer::GetBestSize(wxGrid
& grid
,
2300 wxGridCellAttr
& attr
,
2304 return DoGetBestSize(attr
, dc
, grid
.GetCellValue(row
, col
));
2307 void wxGridCellStringRenderer::Draw(wxGrid
& grid
,
2308 wxGridCellAttr
& attr
,
2310 const wxRect
& rectCell
,
2314 wxRect rect
= rectCell
;
2317 // erase only this cells background, overflow cells should have been erased
2318 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
2321 attr
.GetAlignment(&hAlign
, &vAlign
);
2323 int overflowCols
= 0;
2325 if (attr
.GetOverflow())
2327 int cols
= grid
.GetNumberCols();
2328 int best_width
= GetBestSize(grid
,attr
,dc
,row
,col
).GetWidth();
2329 int cell_rows
, cell_cols
;
2330 attr
.GetSize( &cell_rows
, &cell_cols
); // shouldn't get here if <= 0
2331 if ((best_width
> rectCell
.width
) && (col
< cols
) && grid
.GetTable())
2333 int i
, c_cols
, c_rows
;
2334 for (i
= col
+cell_cols
; i
< cols
; i
++)
2336 bool is_empty
= true;
2337 for (int j
=row
; j
< row
+ cell_rows
; j
++)
2339 // check w/ anchor cell for multicell block
2340 grid
.GetCellSize(j
, i
, &c_rows
, &c_cols
);
2343 if (!grid
.GetTable()->IsEmptyCell(j
+ c_rows
, i
))
2352 rect
.width
+= grid
.GetColSize(i
);
2360 if (rect
.width
>= best_width
)
2364 overflowCols
= i
- col
- cell_cols
+ 1;
2365 if (overflowCols
>= cols
)
2366 overflowCols
= cols
- 1;
2369 if (overflowCols
> 0) // redraw overflow cells w/ proper hilight
2371 hAlign
= wxALIGN_LEFT
; // if oveflowed then it's left aligned
2373 clip
.x
+= rectCell
.width
;
2374 // draw each overflow cell individually
2375 int col_end
= col
+ cell_cols
+ overflowCols
;
2376 if (col_end
>= grid
.GetNumberCols())
2377 col_end
= grid
.GetNumberCols() - 1;
2378 for (int i
= col
+ cell_cols
; i
<= col_end
; i
++)
2380 clip
.width
= grid
.GetColSize(i
) - 1;
2381 dc
.DestroyClippingRegion();
2382 dc
.SetClippingRegion(clip
);
2384 SetTextColoursAndFont(grid
, attr
, dc
,
2385 grid
.IsInSelection(row
,i
));
2387 grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
),
2388 rect
, hAlign
, vAlign
);
2389 clip
.x
+= grid
.GetColSize(i
) - 1;
2395 dc
.DestroyClippingRegion();
2399 // now we only have to draw the text
2400 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
2402 grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
),
2403 rect
, hAlign
, vAlign
);
2406 // ----------------------------------------------------------------------------
2407 // wxGridCellNumberRenderer
2408 // ----------------------------------------------------------------------------
2410 wxString
wxGridCellNumberRenderer::GetString(const wxGrid
& grid
, int row
, int col
)
2412 wxGridTableBase
*table
= grid
.GetTable();
2414 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
2416 text
.Printf(_T("%ld"), table
->GetValueAsLong(row
, col
));
2420 text
= table
->GetValue(row
, col
);
2426 void wxGridCellNumberRenderer::Draw(wxGrid
& grid
,
2427 wxGridCellAttr
& attr
,
2429 const wxRect
& rectCell
,
2433 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
2435 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
2437 // draw the text right aligned by default
2439 attr
.GetAlignment(&hAlign
, &vAlign
);
2440 hAlign
= wxALIGN_RIGHT
;
2442 wxRect rect
= rectCell
;
2445 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
2448 wxSize
wxGridCellNumberRenderer::GetBestSize(wxGrid
& grid
,
2449 wxGridCellAttr
& attr
,
2453 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
2456 // ----------------------------------------------------------------------------
2457 // wxGridCellFloatRenderer
2458 // ----------------------------------------------------------------------------
2460 wxGridCellFloatRenderer::wxGridCellFloatRenderer(int width
, int precision
)
2463 SetPrecision(precision
);
2466 wxGridCellRenderer
*wxGridCellFloatRenderer::Clone() const
2468 wxGridCellFloatRenderer
*renderer
= new wxGridCellFloatRenderer
;
2469 renderer
->m_width
= m_width
;
2470 renderer
->m_precision
= m_precision
;
2471 renderer
->m_format
= m_format
;
2476 wxString
wxGridCellFloatRenderer::GetString(const wxGrid
& grid
, int row
, int col
)
2478 wxGridTableBase
*table
= grid
.GetTable();
2483 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
2485 val
= table
->GetValueAsDouble(row
, col
);
2490 text
= table
->GetValue(row
, col
);
2491 hasDouble
= text
.ToDouble(&val
);
2498 if ( m_width
== -1 )
2500 if ( m_precision
== -1 )
2502 // default width/precision
2503 m_format
= _T("%f");
2507 m_format
.Printf(_T("%%.%df"), m_precision
);
2510 else if ( m_precision
== -1 )
2512 // default precision
2513 m_format
.Printf(_T("%%%d.f"), m_width
);
2517 m_format
.Printf(_T("%%%d.%df"), m_width
, m_precision
);
2521 text
.Printf(m_format
, val
);
2524 //else: text already contains the string
2529 void wxGridCellFloatRenderer::Draw(wxGrid
& grid
,
2530 wxGridCellAttr
& attr
,
2532 const wxRect
& rectCell
,
2536 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
2538 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
2540 // draw the text right aligned by default
2542 attr
.GetAlignment(&hAlign
, &vAlign
);
2543 hAlign
= wxALIGN_RIGHT
;
2545 wxRect rect
= rectCell
;
2548 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
2551 wxSize
wxGridCellFloatRenderer::GetBestSize(wxGrid
& grid
,
2552 wxGridCellAttr
& attr
,
2556 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
2559 void wxGridCellFloatRenderer::SetParameters(const wxString
& params
)
2563 // reset to defaults
2569 wxString tmp
= params
.BeforeFirst(_T(','));
2573 if ( tmp
.ToLong(&width
) )
2575 SetWidth((int)width
);
2579 wxLogDebug(_T("Invalid wxGridCellFloatRenderer width parameter string '%s ignored"), params
.c_str());
2583 tmp
= params
.AfterFirst(_T(','));
2587 if ( tmp
.ToLong(&precision
) )
2589 SetPrecision((int)precision
);
2593 wxLogDebug(_T("Invalid wxGridCellFloatRenderer precision parameter string '%s ignored"), params
.c_str());
2599 // ----------------------------------------------------------------------------
2600 // wxGridCellBoolRenderer
2601 // ----------------------------------------------------------------------------
2603 wxSize
wxGridCellBoolRenderer::ms_sizeCheckMark
;
2605 // FIXME these checkbox size calculations are really ugly...
2607 // between checkmark and box
2608 static const wxCoord wxGRID_CHECKMARK_MARGIN
= 2;
2610 wxSize
wxGridCellBoolRenderer::GetBestSize(wxGrid
& grid
,
2611 wxGridCellAttr
& WXUNUSED(attr
),
2616 // compute it only once (no locks for MT safeness in GUI thread...)
2617 if ( !ms_sizeCheckMark
.x
)
2619 // get checkbox size
2620 wxCheckBox
*checkbox
= new wxCheckBox(&grid
, wxID_ANY
, wxEmptyString
);
2621 wxSize size
= checkbox
->GetBestSize();
2622 wxCoord checkSize
= size
.y
+ 2 * wxGRID_CHECKMARK_MARGIN
;
2624 #if defined(__WXMOTIF__)
2625 checkSize
-= size
.y
/ 2;
2630 ms_sizeCheckMark
.x
= ms_sizeCheckMark
.y
= checkSize
;
2633 return ms_sizeCheckMark
;
2636 void wxGridCellBoolRenderer::Draw(wxGrid
& grid
,
2637 wxGridCellAttr
& attr
,
2643 wxGridCellRenderer::Draw(grid
, attr
, dc
, rect
, row
, col
, isSelected
);
2645 // draw a check mark in the centre (ignoring alignment - TODO)
2646 wxSize size
= GetBestSize(grid
, attr
, dc
, row
, col
);
2648 // don't draw outside the cell
2649 wxCoord minSize
= wxMin(rect
.width
, rect
.height
);
2650 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
2652 // and even leave (at least) 1 pixel margin
2653 size
.x
= size
.y
= minSize
;
2656 // draw a border around checkmark
2658 attr
.GetAlignment(&hAlign
, &vAlign
);
2661 if (hAlign
== wxALIGN_CENTRE
)
2663 rectBorder
.x
= rect
.x
+ rect
.width
/ 2 - size
.x
/ 2;
2664 rectBorder
.y
= rect
.y
+ rect
.height
/ 2 - size
.y
/ 2;
2665 rectBorder
.width
= size
.x
;
2666 rectBorder
.height
= size
.y
;
2668 else if (hAlign
== wxALIGN_LEFT
)
2670 rectBorder
.x
= rect
.x
+ 2;
2671 rectBorder
.y
= rect
.y
+ rect
.height
/ 2 - size
.y
/ 2;
2672 rectBorder
.width
= size
.x
;
2673 rectBorder
.height
= size
.y
;
2675 else if (hAlign
== wxALIGN_RIGHT
)
2677 rectBorder
.x
= rect
.x
+ rect
.width
- size
.x
- 2;
2678 rectBorder
.y
= rect
.y
+ rect
.height
/ 2 - size
.y
/ 2;
2679 rectBorder
.width
= size
.x
;
2680 rectBorder
.height
= size
.y
;
2684 if ( grid
.GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
) )
2686 value
= grid
.GetTable()->GetValueAsBool(row
, col
);
2690 wxString
cellval( grid
.GetTable()->GetValue(row
, col
) );
2691 value
= wxGridCellBoolEditor::IsTrueValue(cellval
);
2696 flags
|= wxCONTROL_CHECKED
;
2698 wxRendererNative::Get().DrawCheckBox( &grid
, dc
, rectBorder
, flags
);
2701 // ----------------------------------------------------------------------------
2703 // ----------------------------------------------------------------------------
2705 void wxGridCellAttr::Init(wxGridCellAttr
*attrDefault
)
2709 m_isReadOnly
= Unset
;
2714 m_attrkind
= wxGridCellAttr::Cell
;
2716 m_sizeRows
= m_sizeCols
= 1;
2717 m_overflow
= UnsetOverflow
;
2719 SetDefAttr(attrDefault
);
2722 wxGridCellAttr
*wxGridCellAttr::Clone() const
2724 wxGridCellAttr
*attr
= new wxGridCellAttr(m_defGridAttr
);
2726 if ( HasTextColour() )
2727 attr
->SetTextColour(GetTextColour());
2728 if ( HasBackgroundColour() )
2729 attr
->SetBackgroundColour(GetBackgroundColour());
2731 attr
->SetFont(GetFont());
2732 if ( HasAlignment() )
2733 attr
->SetAlignment(m_hAlign
, m_vAlign
);
2735 attr
->SetSize( m_sizeRows
, m_sizeCols
);
2739 attr
->SetRenderer(m_renderer
);
2740 m_renderer
->IncRef();
2744 attr
->SetEditor(m_editor
);
2749 attr
->SetReadOnly();
2751 attr
->SetOverflow( m_overflow
== Overflow
);
2752 attr
->SetKind( m_attrkind
);
2757 void wxGridCellAttr::MergeWith(wxGridCellAttr
*mergefrom
)
2759 if ( !HasTextColour() && mergefrom
->HasTextColour() )
2760 SetTextColour(mergefrom
->GetTextColour());
2761 if ( !HasBackgroundColour() && mergefrom
->HasBackgroundColour() )
2762 SetBackgroundColour(mergefrom
->GetBackgroundColour());
2763 if ( !HasFont() && mergefrom
->HasFont() )
2764 SetFont(mergefrom
->GetFont());
2765 if ( !HasAlignment() && mergefrom
->HasAlignment() )
2768 mergefrom
->GetAlignment( &hAlign
, &vAlign
);
2769 SetAlignment(hAlign
, vAlign
);
2771 if ( !HasSize() && mergefrom
->HasSize() )
2772 mergefrom
->GetSize( &m_sizeRows
, &m_sizeCols
);
2774 // Directly access member functions as GetRender/Editor don't just return
2775 // m_renderer/m_editor
2777 // Maybe add support for merge of Render and Editor?
2778 if (!HasRenderer() && mergefrom
->HasRenderer() )
2780 m_renderer
= mergefrom
->m_renderer
;
2781 m_renderer
->IncRef();
2783 if ( !HasEditor() && mergefrom
->HasEditor() )
2785 m_editor
= mergefrom
->m_editor
;
2788 if ( !HasReadWriteMode() && mergefrom
->HasReadWriteMode() )
2789 SetReadOnly(mergefrom
->IsReadOnly());
2791 if (!HasOverflowMode() && mergefrom
->HasOverflowMode() )
2792 SetOverflow(mergefrom
->GetOverflow());
2794 SetDefAttr(mergefrom
->m_defGridAttr
);
2797 void wxGridCellAttr::SetSize(int num_rows
, int num_cols
)
2799 // The size of a cell is normally 1,1
2801 // If this cell is larger (2,2) then this is the top left cell
2802 // the other cells that will be covered (lower right cells) must be
2803 // set to negative or zero values such that
2804 // row + num_rows of the covered cell points to the larger cell (this cell)
2805 // same goes for the col + num_cols.
2807 // Size of 0,0 is NOT valid, neither is <=0 and any positive value
2809 wxASSERT_MSG( (!((num_rows
> 0) && (num_cols
<= 0)) ||
2810 !((num_rows
<= 0) && (num_cols
> 0)) ||
2811 !((num_rows
== 0) && (num_cols
== 0))),
2812 wxT("wxGridCellAttr::SetSize only takes two postive values or negative/zero values"));
2814 m_sizeRows
= num_rows
;
2815 m_sizeCols
= num_cols
;
2818 const wxColour
& wxGridCellAttr::GetTextColour() const
2820 if (HasTextColour())
2824 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2826 return m_defGridAttr
->GetTextColour();
2830 wxFAIL_MSG(wxT("Missing default cell attribute"));
2831 return wxNullColour
;
2835 const wxColour
& wxGridCellAttr::GetBackgroundColour() const
2837 if (HasBackgroundColour())
2841 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2843 return m_defGridAttr
->GetBackgroundColour();
2847 wxFAIL_MSG(wxT("Missing default cell attribute"));
2848 return wxNullColour
;
2852 const wxFont
& wxGridCellAttr::GetFont() const
2858 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2860 return m_defGridAttr
->GetFont();
2864 wxFAIL_MSG(wxT("Missing default cell attribute"));
2869 void wxGridCellAttr::GetAlignment(int *hAlign
, int *vAlign
) const
2878 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2880 m_defGridAttr
->GetAlignment(hAlign
, vAlign
);
2884 wxFAIL_MSG(wxT("Missing default cell attribute"));
2888 void wxGridCellAttr::GetSize( int *num_rows
, int *num_cols
) const
2891 *num_rows
= m_sizeRows
;
2893 *num_cols
= m_sizeCols
;
2896 // GetRenderer and GetEditor use a slightly different decision path about
2897 // which attribute to use. If a non-default attr object has one then it is
2898 // used, otherwise the default editor or renderer is fetched from the grid and
2899 // used. It should be the default for the data type of the cell. If it is
2900 // NULL (because the table has a type that the grid does not have in its
2901 // registry), then the grid's default editor or renderer is used.
2903 wxGridCellRenderer
* wxGridCellAttr::GetRenderer(const wxGrid
* grid
, int row
, int col
) const
2905 wxGridCellRenderer
*renderer
= NULL
;
2907 if ( m_renderer
&& this != m_defGridAttr
)
2909 // use the cells renderer if it has one
2910 renderer
= m_renderer
;
2913 else // no non-default cell renderer
2915 // get default renderer for the data type
2918 // GetDefaultRendererForCell() will do IncRef() for us
2919 renderer
= grid
->GetDefaultRendererForCell(row
, col
);
2922 if ( renderer
== NULL
)
2924 if ( (m_defGridAttr
!= NULL
) && (m_defGridAttr
!= this) )
2926 // if we still don't have one then use the grid default
2927 // (no need for IncRef() here neither)
2928 renderer
= m_defGridAttr
->GetRenderer(NULL
, 0, 0);
2930 else // default grid attr
2932 // use m_renderer which we had decided not to use initially
2933 renderer
= m_renderer
;
2940 // we're supposed to always find something
2941 wxASSERT_MSG(renderer
, wxT("Missing default cell renderer"));
2946 // same as above, except for s/renderer/editor/g
2947 wxGridCellEditor
* wxGridCellAttr::GetEditor(const wxGrid
* grid
, int row
, int col
) const
2949 wxGridCellEditor
*editor
= NULL
;
2951 if ( m_editor
&& this != m_defGridAttr
)
2953 // use the cells editor if it has one
2957 else // no non default cell editor
2959 // get default editor for the data type
2962 // GetDefaultEditorForCell() will do IncRef() for us
2963 editor
= grid
->GetDefaultEditorForCell(row
, col
);
2966 if ( editor
== NULL
)
2968 if ( (m_defGridAttr
!= NULL
) && (m_defGridAttr
!= this) )
2970 // if we still don't have one then use the grid default
2971 // (no need for IncRef() here neither)
2972 editor
= m_defGridAttr
->GetEditor(NULL
, 0, 0);
2974 else // default grid attr
2976 // use m_editor which we had decided not to use initially
2984 // we're supposed to always find something
2985 wxASSERT_MSG(editor
, wxT("Missing default cell editor"));
2990 // ----------------------------------------------------------------------------
2991 // wxGridCellAttrData
2992 // ----------------------------------------------------------------------------
2994 void wxGridCellAttrData::SetAttr(wxGridCellAttr
*attr
, int row
, int col
)
2996 // Note: contrary to wxGridRowOrColAttrData::SetAttr, we must not
2997 // touch attribute's reference counting explicitly, since this
2998 // is managed by class wxGridCellWithAttr
2999 int n
= FindIndex(row
, col
);
3000 if ( n
== wxNOT_FOUND
)
3004 // add the attribute
3005 m_attrs
.Add(new wxGridCellWithAttr(row
, col
, attr
));
3007 //else: nothing to do
3009 else // we already have an attribute for this cell
3013 // change the attribute
3014 m_attrs
[(size_t)n
].ChangeAttr(attr
);
3018 // remove this attribute
3019 m_attrs
.RemoveAt((size_t)n
);
3024 wxGridCellAttr
*wxGridCellAttrData::GetAttr(int row
, int col
) const
3026 wxGridCellAttr
*attr
= NULL
;
3028 int n
= FindIndex(row
, col
);
3029 if ( n
!= wxNOT_FOUND
)
3031 attr
= m_attrs
[(size_t)n
].attr
;
3038 void wxGridCellAttrData::UpdateAttrRows( size_t pos
, int numRows
)
3040 size_t count
= m_attrs
.GetCount();
3041 for ( size_t n
= 0; n
< count
; n
++ )
3043 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
3044 wxCoord row
= coords
.GetRow();
3045 if ((size_t)row
>= pos
)
3049 // If rows inserted, include row counter where necessary
3050 coords
.SetRow(row
+ numRows
);
3052 else if (numRows
< 0)
3054 // If rows deleted ...
3055 if ((size_t)row
>= pos
- numRows
)
3057 // ...either decrement row counter (if row still exists)...
3058 coords
.SetRow(row
+ numRows
);
3062 // ...or remove the attribute
3063 m_attrs
.RemoveAt(n
);
3072 void wxGridCellAttrData::UpdateAttrCols( size_t pos
, int numCols
)
3074 size_t count
= m_attrs
.GetCount();
3075 for ( size_t n
= 0; n
< count
; n
++ )
3077 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
3078 wxCoord col
= coords
.GetCol();
3079 if ( (size_t)col
>= pos
)
3083 // If rows inserted, include row counter where necessary
3084 coords
.SetCol(col
+ numCols
);
3086 else if (numCols
< 0)
3088 // If rows deleted ...
3089 if ((size_t)col
>= pos
- numCols
)
3091 // ...either decrement row counter (if row still exists)...
3092 coords
.SetCol(col
+ numCols
);
3096 // ...or remove the attribute
3097 m_attrs
.RemoveAt(n
);
3106 int wxGridCellAttrData::FindIndex(int row
, int col
) const
3108 size_t count
= m_attrs
.GetCount();
3109 for ( size_t n
= 0; n
< count
; n
++ )
3111 const wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
3112 if ( (coords
.GetRow() == row
) && (coords
.GetCol() == col
) )
3121 // ----------------------------------------------------------------------------
3122 // wxGridRowOrColAttrData
3123 // ----------------------------------------------------------------------------
3125 wxGridRowOrColAttrData::~wxGridRowOrColAttrData()
3127 size_t count
= m_attrs
.GetCount();
3128 for ( size_t n
= 0; n
< count
; n
++ )
3130 m_attrs
[n
]->DecRef();
3134 wxGridCellAttr
*wxGridRowOrColAttrData::GetAttr(int rowOrCol
) const
3136 wxGridCellAttr
*attr
= NULL
;
3138 int n
= m_rowsOrCols
.Index(rowOrCol
);
3139 if ( n
!= wxNOT_FOUND
)
3141 attr
= m_attrs
[(size_t)n
];
3148 void wxGridRowOrColAttrData::SetAttr(wxGridCellAttr
*attr
, int rowOrCol
)
3150 int i
= m_rowsOrCols
.Index(rowOrCol
);
3151 if ( i
== wxNOT_FOUND
)
3155 // add the attribute - no need to do anything to reference count
3156 // since we take ownership of the attribute.
3157 m_rowsOrCols
.Add(rowOrCol
);
3160 // nothing to remove
3164 size_t n
= (size_t)i
;
3165 if ( m_attrs
[n
] == attr
)
3170 // change the attribute, handling reference count manually,
3171 // taking ownership of the new attribute.
3172 m_attrs
[n
]->DecRef();
3177 // remove this attribute, handling reference count manually
3178 m_attrs
[n
]->DecRef();
3179 m_rowsOrCols
.RemoveAt(n
);
3180 m_attrs
.RemoveAt(n
);
3185 void wxGridRowOrColAttrData::UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
)
3187 size_t count
= m_attrs
.GetCount();
3188 for ( size_t n
= 0; n
< count
; n
++ )
3190 int & rowOrCol
= m_rowsOrCols
[n
];
3191 if ( (size_t)rowOrCol
>= pos
)
3193 if ( numRowsOrCols
> 0 )
3195 // If rows inserted, include row counter where necessary
3196 rowOrCol
+= numRowsOrCols
;
3198 else if ( numRowsOrCols
< 0)
3200 // If rows deleted, either decrement row counter (if row still exists)
3201 if ((size_t)rowOrCol
>= pos
- numRowsOrCols
)
3202 rowOrCol
+= numRowsOrCols
;
3205 m_rowsOrCols
.RemoveAt(n
);
3206 m_attrs
[n
]->DecRef();
3207 m_attrs
.RemoveAt(n
);
3216 // ----------------------------------------------------------------------------
3217 // wxGridCellAttrProvider
3218 // ----------------------------------------------------------------------------
3220 wxGridCellAttrProvider::wxGridCellAttrProvider()
3225 wxGridCellAttrProvider::~wxGridCellAttrProvider()
3230 void wxGridCellAttrProvider::InitData()
3232 m_data
= new wxGridCellAttrProviderData
;
3235 wxGridCellAttr
*wxGridCellAttrProvider::GetAttr(int row
, int col
,
3236 wxGridCellAttr::wxAttrKind kind
) const
3238 wxGridCellAttr
*attr
= NULL
;
3243 case (wxGridCellAttr::Any
):
3244 // Get cached merge attributes.
3245 // Currently not used as no cache implemented as not mutable
3246 // attr = m_data->m_mergeAttr.GetAttr(row, col);
3249 // Basically implement old version.
3250 // Also check merge cache, so we don't have to re-merge every time..
3251 wxGridCellAttr
*attrcell
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
3252 wxGridCellAttr
*attrrow
= m_data
->m_rowAttrs
.GetAttr(row
);
3253 wxGridCellAttr
*attrcol
= m_data
->m_colAttrs
.GetAttr(col
);
3255 if ((attrcell
!= attrrow
) && (attrrow
!= attrcol
) && (attrcell
!= attrcol
))
3257 // Two or more are non NULL
3258 attr
= new wxGridCellAttr
;
3259 attr
->SetKind(wxGridCellAttr::Merged
);
3261 // Order is important..
3264 attr
->MergeWith(attrcell
);
3269 attr
->MergeWith(attrcol
);
3274 attr
->MergeWith(attrrow
);
3278 // store merge attr if cache implemented
3280 //m_data->m_mergeAttr.SetAttr(attr, row, col);
3284 // one or none is non null return it or null.
3303 case (wxGridCellAttr::Cell
):
3304 attr
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
3307 case (wxGridCellAttr::Col
):
3308 attr
= m_data
->m_colAttrs
.GetAttr(col
);
3311 case (wxGridCellAttr::Row
):
3312 attr
= m_data
->m_rowAttrs
.GetAttr(row
);
3317 // (wxGridCellAttr::Default):
3318 // (wxGridCellAttr::Merged):
3326 void wxGridCellAttrProvider::SetAttr(wxGridCellAttr
*attr
,
3332 m_data
->m_cellAttrs
.SetAttr(attr
, row
, col
);
3335 void wxGridCellAttrProvider::SetRowAttr(wxGridCellAttr
*attr
, int row
)
3340 m_data
->m_rowAttrs
.SetAttr(attr
, row
);
3343 void wxGridCellAttrProvider::SetColAttr(wxGridCellAttr
*attr
, int col
)
3348 m_data
->m_colAttrs
.SetAttr(attr
, col
);
3351 void wxGridCellAttrProvider::UpdateAttrRows( size_t pos
, int numRows
)
3355 m_data
->m_cellAttrs
.UpdateAttrRows( pos
, numRows
);
3357 m_data
->m_rowAttrs
.UpdateAttrRowsOrCols( pos
, numRows
);
3361 void wxGridCellAttrProvider::UpdateAttrCols( size_t pos
, int numCols
)
3365 m_data
->m_cellAttrs
.UpdateAttrCols( pos
, numCols
);
3367 m_data
->m_colAttrs
.UpdateAttrRowsOrCols( pos
, numCols
);
3371 // ----------------------------------------------------------------------------
3372 // wxGridTypeRegistry
3373 // ----------------------------------------------------------------------------
3375 wxGridTypeRegistry::~wxGridTypeRegistry()
3377 size_t count
= m_typeinfo
.GetCount();
3378 for ( size_t i
= 0; i
< count
; i
++ )
3379 delete m_typeinfo
[i
];
3382 void wxGridTypeRegistry::RegisterDataType(const wxString
& typeName
,
3383 wxGridCellRenderer
* renderer
,
3384 wxGridCellEditor
* editor
)
3386 wxGridDataTypeInfo
* info
= new wxGridDataTypeInfo(typeName
, renderer
, editor
);
3388 // is it already registered?
3389 int loc
= FindRegisteredDataType(typeName
);
3390 if ( loc
!= wxNOT_FOUND
)
3392 delete m_typeinfo
[loc
];
3393 m_typeinfo
[loc
] = info
;
3397 m_typeinfo
.Add(info
);
3401 int wxGridTypeRegistry::FindRegisteredDataType(const wxString
& typeName
)
3403 size_t count
= m_typeinfo
.GetCount();
3404 for ( size_t i
= 0; i
< count
; i
++ )
3406 if ( typeName
== m_typeinfo
[i
]->m_typeName
)
3415 int wxGridTypeRegistry::FindDataType(const wxString
& typeName
)
3417 int index
= FindRegisteredDataType(typeName
);
3418 if ( index
== wxNOT_FOUND
)
3420 // check whether this is one of the standard ones, in which case
3421 // register it "on the fly"
3423 if ( typeName
== wxGRID_VALUE_STRING
)
3425 RegisterDataType(wxGRID_VALUE_STRING
,
3426 new wxGridCellStringRenderer
,
3427 new wxGridCellTextEditor
);
3430 #endif // wxUSE_TEXTCTRL
3432 if ( typeName
== wxGRID_VALUE_BOOL
)
3434 RegisterDataType(wxGRID_VALUE_BOOL
,
3435 new wxGridCellBoolRenderer
,
3436 new wxGridCellBoolEditor
);
3439 #endif // wxUSE_CHECKBOX
3441 if ( typeName
== wxGRID_VALUE_NUMBER
)
3443 RegisterDataType(wxGRID_VALUE_NUMBER
,
3444 new wxGridCellNumberRenderer
,
3445 new wxGridCellNumberEditor
);
3447 else if ( typeName
== wxGRID_VALUE_FLOAT
)
3449 RegisterDataType(wxGRID_VALUE_FLOAT
,
3450 new wxGridCellFloatRenderer
,
3451 new wxGridCellFloatEditor
);
3454 #endif // wxUSE_TEXTCTRL
3456 if ( typeName
== wxGRID_VALUE_CHOICE
)
3458 RegisterDataType(wxGRID_VALUE_CHOICE
,
3459 new wxGridCellStringRenderer
,
3460 new wxGridCellChoiceEditor
);
3463 #endif // wxUSE_COMBOBOX
3468 // we get here only if just added the entry for this type, so return
3470 index
= m_typeinfo
.GetCount() - 1;
3476 int wxGridTypeRegistry::FindOrCloneDataType(const wxString
& typeName
)
3478 int index
= FindDataType(typeName
);
3479 if ( index
== wxNOT_FOUND
)
3481 // the first part of the typename is the "real" type, anything after ':'
3482 // are the parameters for the renderer
3483 index
= FindDataType(typeName
.BeforeFirst(_T(':')));
3484 if ( index
== wxNOT_FOUND
)
3489 wxGridCellRenderer
*renderer
= GetRenderer(index
);
3490 wxGridCellRenderer
*rendererOld
= renderer
;
3491 renderer
= renderer
->Clone();
3492 rendererOld
->DecRef();
3494 wxGridCellEditor
*editor
= GetEditor(index
);
3495 wxGridCellEditor
*editorOld
= editor
;
3496 editor
= editor
->Clone();
3497 editorOld
->DecRef();
3499 // do it even if there are no parameters to reset them to defaults
3500 wxString params
= typeName
.AfterFirst(_T(':'));
3501 renderer
->SetParameters(params
);
3502 editor
->SetParameters(params
);
3504 // register the new typename
3505 RegisterDataType(typeName
, renderer
, editor
);
3507 // we just registered it, it's the last one
3508 index
= m_typeinfo
.GetCount() - 1;
3514 wxGridCellRenderer
* wxGridTypeRegistry::GetRenderer(int index
)
3516 wxGridCellRenderer
* renderer
= m_typeinfo
[index
]->m_renderer
;
3523 wxGridCellEditor
* wxGridTypeRegistry::GetEditor(int index
)
3525 wxGridCellEditor
* editor
= m_typeinfo
[index
]->m_editor
;
3532 // ----------------------------------------------------------------------------
3534 // ----------------------------------------------------------------------------
3536 IMPLEMENT_ABSTRACT_CLASS( wxGridTableBase
, wxObject
)
3538 wxGridTableBase::wxGridTableBase()
3541 m_attrProvider
= NULL
;
3544 wxGridTableBase::~wxGridTableBase()
3546 delete m_attrProvider
;
3549 void wxGridTableBase::SetAttrProvider(wxGridCellAttrProvider
*attrProvider
)
3551 delete m_attrProvider
;
3552 m_attrProvider
= attrProvider
;
3555 bool wxGridTableBase::CanHaveAttributes()
3557 if ( ! GetAttrProvider() )
3559 // use the default attr provider by default
3560 SetAttrProvider(new wxGridCellAttrProvider
);
3566 wxGridCellAttr
*wxGridTableBase::GetAttr(int row
, int col
, wxGridCellAttr::wxAttrKind kind
)
3568 if ( m_attrProvider
)
3569 return m_attrProvider
->GetAttr(row
, col
, kind
);
3574 void wxGridTableBase::SetAttr(wxGridCellAttr
* attr
, int row
, int col
)
3576 if ( m_attrProvider
)
3579 attr
->SetKind(wxGridCellAttr::Cell
);
3580 m_attrProvider
->SetAttr(attr
, row
, col
);
3584 // as we take ownership of the pointer and don't store it, we must
3590 void wxGridTableBase::SetRowAttr(wxGridCellAttr
*attr
, int row
)
3592 if ( m_attrProvider
)
3594 attr
->SetKind(wxGridCellAttr::Row
);
3595 m_attrProvider
->SetRowAttr(attr
, row
);
3599 // as we take ownership of the pointer and don't store it, we must
3605 void wxGridTableBase::SetColAttr(wxGridCellAttr
*attr
, int col
)
3607 if ( m_attrProvider
)
3609 attr
->SetKind(wxGridCellAttr::Col
);
3610 m_attrProvider
->SetColAttr(attr
, col
);
3614 // as we take ownership of the pointer and don't store it, we must
3620 bool wxGridTableBase::InsertRows( size_t WXUNUSED(pos
),
3621 size_t WXUNUSED(numRows
) )
3623 wxFAIL_MSG( wxT("Called grid table class function InsertRows\nbut your derived table class does not override this function") );
3628 bool wxGridTableBase::AppendRows( size_t WXUNUSED(numRows
) )
3630 wxFAIL_MSG( wxT("Called grid table class function AppendRows\nbut your derived table class does not override this function"));
3635 bool wxGridTableBase::DeleteRows( size_t WXUNUSED(pos
),
3636 size_t WXUNUSED(numRows
) )
3638 wxFAIL_MSG( wxT("Called grid table class function DeleteRows\nbut your derived table class does not override this function"));
3643 bool wxGridTableBase::InsertCols( size_t WXUNUSED(pos
),
3644 size_t WXUNUSED(numCols
) )
3646 wxFAIL_MSG( wxT("Called grid table class function InsertCols\nbut your derived table class does not override this function"));
3651 bool wxGridTableBase::AppendCols( size_t WXUNUSED(numCols
) )
3653 wxFAIL_MSG(wxT("Called grid table class function AppendCols\nbut your derived table class does not override this function"));
3658 bool wxGridTableBase::DeleteCols( size_t WXUNUSED(pos
),
3659 size_t WXUNUSED(numCols
) )
3661 wxFAIL_MSG( wxT("Called grid table class function DeleteCols\nbut your derived table class does not override this function"));
3666 wxString
wxGridTableBase::GetRowLabelValue( int row
)
3670 // RD: Starting the rows at zero confuses users,
3671 // no matter how much it makes sense to us geeks.
3677 wxString
wxGridTableBase::GetColLabelValue( int col
)
3679 // default col labels are:
3680 // cols 0 to 25 : A-Z
3681 // cols 26 to 675 : AA-ZZ
3686 for ( n
= 1; ; n
++ )
3688 s
+= (wxChar
) (_T('A') + (wxChar
)(col
% 26));
3694 // reverse the string...
3696 for ( i
= 0; i
< n
; i
++ )
3704 wxString
wxGridTableBase::GetTypeName( int WXUNUSED(row
), int WXUNUSED(col
) )
3706 return wxGRID_VALUE_STRING
;
3709 bool wxGridTableBase::CanGetValueAs( int WXUNUSED(row
), int WXUNUSED(col
),
3710 const wxString
& typeName
)
3712 return typeName
== wxGRID_VALUE_STRING
;
3715 bool wxGridTableBase::CanSetValueAs( int row
, int col
, const wxString
& typeName
)
3717 return CanGetValueAs(row
, col
, typeName
);
3720 long wxGridTableBase::GetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
) )
3725 double wxGridTableBase::GetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
) )
3730 bool wxGridTableBase::GetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
) )
3735 void wxGridTableBase::SetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
),
3736 long WXUNUSED(value
) )
3740 void wxGridTableBase::SetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
),
3741 double WXUNUSED(value
) )
3745 void wxGridTableBase::SetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
),
3746 bool WXUNUSED(value
) )
3750 void* wxGridTableBase::GetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
3751 const wxString
& WXUNUSED(typeName
) )
3756 void wxGridTableBase::SetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
3757 const wxString
& WXUNUSED(typeName
),
3758 void* WXUNUSED(value
) )
3762 //////////////////////////////////////////////////////////////////////
3764 // Message class for the grid table to send requests and notifications
3768 wxGridTableMessage::wxGridTableMessage()
3776 wxGridTableMessage::wxGridTableMessage( wxGridTableBase
*table
, int id
,
3777 int commandInt1
, int commandInt2
)
3781 m_comInt1
= commandInt1
;
3782 m_comInt2
= commandInt2
;
3785 //////////////////////////////////////////////////////////////////////
3787 // A basic grid table for string data. An object of this class will
3788 // created by wxGrid if you don't specify an alternative table class.
3791 WX_DEFINE_OBJARRAY(wxGridStringArray
)
3793 IMPLEMENT_DYNAMIC_CLASS( wxGridStringTable
, wxGridTableBase
)
3795 wxGridStringTable::wxGridStringTable()
3800 wxGridStringTable::wxGridStringTable( int numRows
, int numCols
)
3803 m_data
.Alloc( numRows
);
3806 sa
.Alloc( numCols
);
3807 sa
.Add( wxEmptyString
, numCols
);
3809 m_data
.Add( sa
, numRows
);
3812 wxGridStringTable::~wxGridStringTable()
3816 int wxGridStringTable::GetNumberRows()
3818 return m_data
.GetCount();
3821 int wxGridStringTable::GetNumberCols()
3823 if ( m_data
.GetCount() > 0 )
3824 return m_data
[0].GetCount();
3829 wxString
wxGridStringTable::GetValue( int row
, int col
)
3831 wxCHECK_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3833 _T("invalid row or column index in wxGridStringTable") );
3835 return m_data
[row
][col
];
3838 void wxGridStringTable::SetValue( int row
, int col
, const wxString
& value
)
3840 wxCHECK_RET( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3841 _T("invalid row or column index in wxGridStringTable") );
3843 m_data
[row
][col
] = value
;
3846 bool wxGridStringTable::IsEmptyCell( int row
, int col
)
3848 wxCHECK_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3850 _T("invalid row or column index in wxGridStringTable") );
3852 return (m_data
[row
][col
] == wxEmptyString
);
3855 void wxGridStringTable::Clear()
3858 int numRows
, numCols
;
3860 numRows
= m_data
.GetCount();
3863 numCols
= m_data
[0].GetCount();
3865 for ( row
= 0; row
< numRows
; row
++ )
3867 for ( col
= 0; col
< numCols
; col
++ )
3869 m_data
[row
][col
] = wxEmptyString
;
3875 bool wxGridStringTable::InsertRows( size_t pos
, size_t numRows
)
3877 size_t curNumRows
= m_data
.GetCount();
3878 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3879 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3881 if ( pos
>= curNumRows
)
3883 return AppendRows( numRows
);
3887 sa
.Alloc( curNumCols
);
3888 sa
.Add( wxEmptyString
, curNumCols
);
3889 m_data
.Insert( sa
, pos
, numRows
);
3893 wxGridTableMessage
msg( this,
3894 wxGRIDTABLE_NOTIFY_ROWS_INSERTED
,
3898 GetView()->ProcessTableMessage( msg
);
3904 bool wxGridStringTable::AppendRows( size_t numRows
)
3906 size_t curNumRows
= m_data
.GetCount();
3907 size_t curNumCols
= ( curNumRows
> 0
3908 ? m_data
[0].GetCount()
3909 : ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3912 if ( curNumCols
> 0 )
3914 sa
.Alloc( curNumCols
);
3915 sa
.Add( wxEmptyString
, curNumCols
);
3918 m_data
.Add( sa
, numRows
);
3922 wxGridTableMessage
msg( this,
3923 wxGRIDTABLE_NOTIFY_ROWS_APPENDED
,
3926 GetView()->ProcessTableMessage( msg
);
3932 bool wxGridStringTable::DeleteRows( size_t pos
, size_t numRows
)
3934 size_t curNumRows
= m_data
.GetCount();
3936 if ( pos
>= curNumRows
)
3938 wxFAIL_MSG( wxString::Format
3940 wxT("Called wxGridStringTable::DeleteRows(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu rows"),
3942 (unsigned long)numRows
,
3943 (unsigned long)curNumRows
3949 if ( numRows
> curNumRows
- pos
)
3951 numRows
= curNumRows
- pos
;
3954 if ( numRows
>= curNumRows
)
3960 m_data
.RemoveAt( pos
, numRows
);
3965 wxGridTableMessage
msg( this,
3966 wxGRIDTABLE_NOTIFY_ROWS_DELETED
,
3970 GetView()->ProcessTableMessage( msg
);
3976 bool wxGridStringTable::InsertCols( size_t pos
, size_t numCols
)
3980 size_t curNumRows
= m_data
.GetCount();
3981 size_t curNumCols
= ( curNumRows
> 0
3982 ? m_data
[0].GetCount()
3983 : ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3985 if ( pos
>= curNumCols
)
3987 return AppendCols( numCols
);
3990 if ( !m_colLabels
.IsEmpty() )
3992 m_colLabels
.Insert( wxEmptyString
, pos
, numCols
);
3995 for ( i
= pos
; i
< pos
+ numCols
; i
++ )
3996 m_colLabels
[i
] = wxGridTableBase::GetColLabelValue( i
);
3999 for ( row
= 0; row
< curNumRows
; row
++ )
4001 for ( col
= pos
; col
< pos
+ numCols
; col
++ )
4003 m_data
[row
].Insert( wxEmptyString
, col
);
4009 wxGridTableMessage
msg( this,
4010 wxGRIDTABLE_NOTIFY_COLS_INSERTED
,
4014 GetView()->ProcessTableMessage( msg
);
4020 bool wxGridStringTable::AppendCols( size_t numCols
)
4024 size_t curNumRows
= m_data
.GetCount();
4026 for ( row
= 0; row
< curNumRows
; row
++ )
4028 m_data
[row
].Add( wxEmptyString
, numCols
);
4033 wxGridTableMessage
msg( this,
4034 wxGRIDTABLE_NOTIFY_COLS_APPENDED
,
4037 GetView()->ProcessTableMessage( msg
);
4043 bool wxGridStringTable::DeleteCols( size_t pos
, size_t numCols
)
4047 size_t curNumRows
= m_data
.GetCount();
4048 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
4049 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
4051 if ( pos
>= curNumCols
)
4053 wxFAIL_MSG( wxString::Format
4055 wxT("Called wxGridStringTable::DeleteCols(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu cols"),
4057 (unsigned long)numCols
,
4058 (unsigned long)curNumCols
4065 colID
= GetView()->GetColAt( pos
);
4069 if ( numCols
> curNumCols
- colID
)
4071 numCols
= curNumCols
- colID
;
4074 if ( !m_colLabels
.IsEmpty() )
4076 // m_colLabels stores just as many elements as it needs, e.g. if only
4077 // the label of the first column had been set it would have only one
4078 // element and not numCols, so account for it
4079 int nToRm
= m_colLabels
.size() - colID
;
4081 m_colLabels
.RemoveAt( colID
, nToRm
);
4084 for ( row
= 0; row
< curNumRows
; row
++ )
4086 if ( numCols
>= curNumCols
)
4088 m_data
[row
].Clear();
4092 m_data
[row
].RemoveAt( colID
, numCols
);
4098 wxGridTableMessage
msg( this,
4099 wxGRIDTABLE_NOTIFY_COLS_DELETED
,
4103 GetView()->ProcessTableMessage( msg
);
4109 wxString
wxGridStringTable::GetRowLabelValue( int row
)
4111 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
4113 // using default label
4115 return wxGridTableBase::GetRowLabelValue( row
);
4119 return m_rowLabels
[row
];
4123 wxString
wxGridStringTable::GetColLabelValue( int col
)
4125 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
4127 // using default label
4129 return wxGridTableBase::GetColLabelValue( col
);
4133 return m_colLabels
[col
];
4137 void wxGridStringTable::SetRowLabelValue( int row
, const wxString
& value
)
4139 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
4141 int n
= m_rowLabels
.GetCount();
4144 for ( i
= n
; i
<= row
; i
++ )
4146 m_rowLabels
.Add( wxGridTableBase::GetRowLabelValue(i
) );
4150 m_rowLabels
[row
] = value
;
4153 void wxGridStringTable::SetColLabelValue( int col
, const wxString
& value
)
4155 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
4157 int n
= m_colLabels
.GetCount();
4160 for ( i
= n
; i
<= col
; i
++ )
4162 m_colLabels
.Add( wxGridTableBase::GetColLabelValue(i
) );
4166 m_colLabels
[col
] = value
;
4170 //////////////////////////////////////////////////////////////////////
4171 //////////////////////////////////////////////////////////////////////
4173 BEGIN_EVENT_TABLE(wxGridSubwindow
, wxWindow
)
4174 EVT_MOUSE_CAPTURE_LOST(wxGridSubwindow::OnMouseCaptureLost
)
4177 void wxGridSubwindow::OnMouseCaptureLost(wxMouseCaptureLostEvent
& WXUNUSED(event
))
4179 m_owner
->CancelMouseCapture();
4182 IMPLEMENT_DYNAMIC_CLASS( wxGridRowLabelWindow
, wxWindow
)
4184 BEGIN_EVENT_TABLE( wxGridRowLabelWindow
, wxGridSubwindow
)
4185 EVT_PAINT( wxGridRowLabelWindow::OnPaint
)
4186 EVT_MOUSEWHEEL( wxGridRowLabelWindow::OnMouseWheel
)
4187 EVT_MOUSE_EVENTS( wxGridRowLabelWindow::OnMouseEvent
)
4190 wxGridRowLabelWindow::wxGridRowLabelWindow( wxGrid
*parent
,
4192 const wxPoint
&pos
, const wxSize
&size
)
4193 : wxGridSubwindow(parent
, id
, pos
, size
)
4198 void wxGridRowLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
4202 // NO - don't do this because it will set both the x and y origin
4203 // coords to match the parent scrolled window and we just want to
4204 // set the y coord - MB
4206 // m_owner->PrepareDC( dc );
4209 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
4210 wxPoint pt
= dc
.GetDeviceOrigin();
4211 dc
.SetDeviceOrigin( pt
.x
, pt
.y
-y
);
4213 wxArrayInt rows
= m_owner
->CalcRowLabelsExposed( GetUpdateRegion() );
4214 m_owner
->DrawRowLabels( dc
, rows
);
4217 void wxGridRowLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
4219 m_owner
->ProcessRowLabelMouseEvent( event
);
4222 void wxGridRowLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
4224 m_owner
->GetEventHandler()->ProcessEvent( event
);
4227 //////////////////////////////////////////////////////////////////////
4229 IMPLEMENT_DYNAMIC_CLASS( wxGridColLabelWindow
, wxWindow
)
4231 BEGIN_EVENT_TABLE( wxGridColLabelWindow
, wxGridSubwindow
)
4232 EVT_PAINT( wxGridColLabelWindow::OnPaint
)
4233 EVT_MOUSEWHEEL( wxGridColLabelWindow::OnMouseWheel
)
4234 EVT_MOUSE_EVENTS( wxGridColLabelWindow::OnMouseEvent
)
4237 wxGridColLabelWindow::wxGridColLabelWindow( wxGrid
*parent
,
4239 const wxPoint
&pos
, const wxSize
&size
)
4240 : wxGridSubwindow(parent
, id
, pos
, size
)
4245 void wxGridColLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
4249 // NO - don't do this because it will set both the x and y origin
4250 // coords to match the parent scrolled window and we just want to
4251 // set the x coord - MB
4253 // m_owner->PrepareDC( dc );
4256 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
4257 wxPoint pt
= dc
.GetDeviceOrigin();
4258 if (GetLayoutDirection() == wxLayout_RightToLeft
)
4259 dc
.SetDeviceOrigin( pt
.x
+x
, pt
.y
);
4261 dc
.SetDeviceOrigin( pt
.x
-x
, pt
.y
);
4263 wxArrayInt cols
= m_owner
->CalcColLabelsExposed( GetUpdateRegion() );
4264 m_owner
->DrawColLabels( dc
, cols
);
4267 void wxGridColLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
4269 m_owner
->ProcessColLabelMouseEvent( event
);
4272 void wxGridColLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
4274 m_owner
->GetEventHandler()->ProcessEvent( event
);
4277 //////////////////////////////////////////////////////////////////////
4279 IMPLEMENT_DYNAMIC_CLASS( wxGridCornerLabelWindow
, wxWindow
)
4281 BEGIN_EVENT_TABLE( wxGridCornerLabelWindow
, wxGridSubwindow
)
4282 EVT_MOUSEWHEEL( wxGridCornerLabelWindow::OnMouseWheel
)
4283 EVT_MOUSE_EVENTS( wxGridCornerLabelWindow::OnMouseEvent
)
4284 EVT_PAINT( wxGridCornerLabelWindow::OnPaint
)
4287 wxGridCornerLabelWindow::wxGridCornerLabelWindow( wxGrid
*parent
,
4290 const wxSize
& size
)
4291 : wxGridSubwindow(parent
, id
, pos
, size
)
4296 void wxGridCornerLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
4300 m_owner
->DrawCornerLabel(dc
);
4303 void wxGridCornerLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
4305 m_owner
->ProcessCornerLabelMouseEvent( event
);
4308 void wxGridCornerLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
4310 m_owner
->GetEventHandler()->ProcessEvent(event
);
4313 //////////////////////////////////////////////////////////////////////
4315 IMPLEMENT_DYNAMIC_CLASS( wxGridWindow
, wxWindow
)
4317 BEGIN_EVENT_TABLE( wxGridWindow
, wxGridSubwindow
)
4318 EVT_PAINT( wxGridWindow::OnPaint
)
4319 EVT_MOUSEWHEEL( wxGridWindow::OnMouseWheel
)
4320 EVT_MOUSE_EVENTS( wxGridWindow::OnMouseEvent
)
4321 EVT_KEY_DOWN( wxGridWindow::OnKeyDown
)
4322 EVT_KEY_UP( wxGridWindow::OnKeyUp
)
4323 EVT_CHAR( wxGridWindow::OnChar
)
4324 EVT_SET_FOCUS( wxGridWindow::OnFocus
)
4325 EVT_KILL_FOCUS( wxGridWindow::OnFocus
)
4326 EVT_ERASE_BACKGROUND( wxGridWindow::OnEraseBackground
)
4329 wxGridWindow::wxGridWindow( wxGrid
*parent
,
4330 wxGridRowLabelWindow
*rowLblWin
,
4331 wxGridColLabelWindow
*colLblWin
,
4334 const wxSize
&size
)
4335 : wxGridSubwindow(parent
, id
, pos
, size
,
4336 wxWANTS_CHARS
| wxCLIP_CHILDREN
,
4337 wxT("grid window") )
4340 m_rowLabelWin
= rowLblWin
;
4341 m_colLabelWin
= colLblWin
;
4344 void wxGridWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
4346 wxPaintDC
dc( this );
4347 m_owner
->PrepareDC( dc
);
4348 wxRegion reg
= GetUpdateRegion();
4349 wxGridCellCoordsArray dirtyCells
= m_owner
->CalcCellsExposed( reg
);
4350 m_owner
->DrawGridCellArea( dc
, dirtyCells
);
4352 m_owner
->DrawGridSpace( dc
);
4354 m_owner
->DrawAllGridLines( dc
, reg
);
4356 m_owner
->DrawHighlight( dc
, dirtyCells
);
4359 void wxGridWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
4361 wxWindow::ScrollWindow( dx
, dy
, rect
);
4362 m_rowLabelWin
->ScrollWindow( 0, dy
, rect
);
4363 m_colLabelWin
->ScrollWindow( dx
, 0, rect
);
4366 void wxGridWindow::OnMouseEvent( wxMouseEvent
& event
)
4368 if (event
.ButtonDown(wxMOUSE_BTN_LEFT
) && FindFocus() != this)
4371 m_owner
->ProcessGridCellMouseEvent( event
);
4374 void wxGridWindow::OnMouseWheel( wxMouseEvent
& event
)
4376 m_owner
->GetEventHandler()->ProcessEvent( event
);
4379 // This seems to be required for wxMotif/wxGTK otherwise the mouse
4380 // cursor must be in the cell edit control to get key events
4382 void wxGridWindow::OnKeyDown( wxKeyEvent
& event
)
4384 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
4388 void wxGridWindow::OnKeyUp( wxKeyEvent
& event
)
4390 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
4394 void wxGridWindow::OnChar( wxKeyEvent
& event
)
4396 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
4400 void wxGridWindow::OnEraseBackground( wxEraseEvent
& WXUNUSED(event
) )
4404 void wxGridWindow::OnFocus(wxFocusEvent
& event
)
4406 // and if we have any selection, it has to be repainted, because it
4407 // uses different colour when the grid is not focused:
4408 if ( m_owner
->IsSelection() )
4414 // NB: Note that this code is in "else" branch only because the other
4415 // branch refreshes everything and so there's no point in calling
4416 // Refresh() again, *not* because it should only be done if
4417 // !IsSelection(). If the above code is ever optimized to refresh
4418 // only selected area, this needs to be moved out of the "else"
4419 // branch so that it's always executed.
4421 // current cell cursor {dis,re}appears on focus change:
4422 const wxGridCellCoords
cursorCoords(m_owner
->GetGridCursorRow(),
4423 m_owner
->GetGridCursorCol());
4424 const wxRect cursor
=
4425 m_owner
->BlockToDeviceRect(cursorCoords
, cursorCoords
);
4426 Refresh(true, &cursor
);
4429 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
4433 #define internalXToCol(x) XToCol(x, true)
4434 #define internalYToRow(y) YToRow(y, true)
4436 /////////////////////////////////////////////////////////////////////
4438 #if wxUSE_EXTENDED_RTTI
4439 WX_DEFINE_FLAGS( wxGridStyle
)
4441 wxBEGIN_FLAGS( wxGridStyle
)
4442 // new style border flags, we put them first to
4443 // use them for streaming out
4444 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
4445 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
4446 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
4447 wxFLAGS_MEMBER(wxBORDER_RAISED
)
4448 wxFLAGS_MEMBER(wxBORDER_STATIC
)
4449 wxFLAGS_MEMBER(wxBORDER_NONE
)
4451 // old style border flags
4452 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
4453 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
4454 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
4455 wxFLAGS_MEMBER(wxRAISED_BORDER
)
4456 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
4457 wxFLAGS_MEMBER(wxBORDER
)
4459 // standard window styles
4460 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
4461 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
4462 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
4463 wxFLAGS_MEMBER(wxWANTS_CHARS
)
4464 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE
)
4465 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
4466 wxFLAGS_MEMBER(wxVSCROLL
)
4467 wxFLAGS_MEMBER(wxHSCROLL
)
4469 wxEND_FLAGS( wxGridStyle
)
4471 IMPLEMENT_DYNAMIC_CLASS_XTI(wxGrid
, wxScrolledWindow
,"wx/grid.h")
4473 wxBEGIN_PROPERTIES_TABLE(wxGrid
)
4474 wxHIDE_PROPERTY( Children
)
4475 wxPROPERTY_FLAGS( WindowStyle
, wxGridStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
4476 wxEND_PROPERTIES_TABLE()
4478 wxBEGIN_HANDLERS_TABLE(wxGrid
)
4479 wxEND_HANDLERS_TABLE()
4481 wxCONSTRUCTOR_5( wxGrid
, wxWindow
* , Parent
, wxWindowID
, Id
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
4484 TODO : Expose more information of a list's layout, etc. via appropriate objects (e.g., NotebookPageInfo)
4487 IMPLEMENT_DYNAMIC_CLASS( wxGrid
, wxScrolledWindow
)
4490 BEGIN_EVENT_TABLE( wxGrid
, wxScrolledWindow
)
4491 EVT_PAINT( wxGrid::OnPaint
)
4492 EVT_SIZE( wxGrid::OnSize
)
4493 EVT_KEY_DOWN( wxGrid::OnKeyDown
)
4494 EVT_KEY_UP( wxGrid::OnKeyUp
)
4495 EVT_CHAR ( wxGrid::OnChar
)
4496 EVT_ERASE_BACKGROUND( wxGrid::OnEraseBackground
)
4504 wxGrid::wxGrid( wxWindow
*parent
,
4509 const wxString
& name
)
4512 Create(parent
, id
, pos
, size
, style
, name
);
4515 bool wxGrid::Create(wxWindow
*parent
, wxWindowID id
,
4516 const wxPoint
& pos
, const wxSize
& size
,
4517 long style
, const wxString
& name
)
4519 if (!wxScrolledWindow::Create(parent
, id
, pos
, size
,
4520 style
| wxWANTS_CHARS
, name
))
4523 m_colMinWidths
= wxLongToLongHashMap(GRID_HASH_SIZE
);
4524 m_rowMinHeights
= wxLongToLongHashMap(GRID_HASH_SIZE
);
4527 SetInitialSize(size
);
4528 SetScrollRate(m_scrollLineX
, m_scrollLineY
);
4536 // Ensure that the editor control is destroyed before the grid is,
4537 // otherwise we crash later when the editor tries to do something with the
4538 // half destroyed grid
4539 HideCellEditControl();
4541 // Must do this or ~wxScrollHelper will pop the wrong event handler
4542 SetTargetWindow(this);
4544 wxSafeDecRef(m_defaultCellAttr
);
4546 #ifdef DEBUG_ATTR_CACHE
4547 size_t total
= gs_nAttrCacheHits
+ gs_nAttrCacheMisses
;
4548 wxPrintf(_T("wxGrid attribute cache statistics: "
4549 "total: %u, hits: %u (%u%%)\n"),
4550 total
, gs_nAttrCacheHits
,
4551 total
? (gs_nAttrCacheHits
*100) / total
: 0);
4554 // if we own the table, just delete it, otherwise at least don't leave it
4555 // with dangling view pointer
4558 else if ( m_table
&& m_table
->GetView() == this )
4559 m_table
->SetView(NULL
);
4561 delete m_typeRegistry
;
4566 // ----- internal init and update functions
4569 // NOTE: If using the default visual attributes works everywhere then this can
4570 // be removed as well as the #else cases below.
4571 #define _USE_VISATTR 0
4573 void wxGrid::Create()
4575 // create the type registry
4576 m_typeRegistry
= new wxGridTypeRegistry
;
4578 m_cellEditCtrlEnabled
= false;
4580 m_defaultCellAttr
= new wxGridCellAttr();
4582 // Set default cell attributes
4583 m_defaultCellAttr
->SetDefAttr(m_defaultCellAttr
);
4584 m_defaultCellAttr
->SetKind(wxGridCellAttr::Default
);
4585 m_defaultCellAttr
->SetFont(GetFont());
4586 m_defaultCellAttr
->SetAlignment(wxALIGN_LEFT
, wxALIGN_TOP
);
4587 m_defaultCellAttr
->SetRenderer(new wxGridCellStringRenderer
);
4588 m_defaultCellAttr
->SetEditor(new wxGridCellTextEditor
);
4591 wxVisualAttributes gva
= wxListBox::GetClassDefaultAttributes();
4592 wxVisualAttributes lva
= wxPanel::GetClassDefaultAttributes();
4594 m_defaultCellAttr
->SetTextColour(gva
.colFg
);
4595 m_defaultCellAttr
->SetBackgroundColour(gva
.colBg
);
4598 m_defaultCellAttr
->SetTextColour(
4599 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
4600 m_defaultCellAttr
->SetBackgroundColour(
4601 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
4606 m_currentCellCoords
= wxGridNoCellCoords
;
4608 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
4609 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
4611 // subwindow components that make up the wxGrid
4612 m_rowLabelWin
= new wxGridRowLabelWindow( this,
4617 m_colLabelWin
= new wxGridColLabelWindow( this,
4622 m_cornerLabelWin
= new wxGridCornerLabelWindow( this,
4627 m_gridWin
= new wxGridWindow( this,
4634 SetTargetWindow( m_gridWin
);
4637 wxColour gfg
= gva
.colFg
;
4638 wxColour gbg
= gva
.colBg
;
4639 wxColour lfg
= lva
.colFg
;
4640 wxColour lbg
= lva
.colBg
;
4642 wxColour gfg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT
);
4643 wxColour gbg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOW
);
4644 wxColour lfg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT
);
4645 wxColour lbg
= wxSystemSettings::GetColour( wxSYS_COLOUR_BTNFACE
);
4648 m_cornerLabelWin
->SetOwnForegroundColour(lfg
);
4649 m_cornerLabelWin
->SetOwnBackgroundColour(lbg
);
4650 m_rowLabelWin
->SetOwnForegroundColour(lfg
);
4651 m_rowLabelWin
->SetOwnBackgroundColour(lbg
);
4652 m_colLabelWin
->SetOwnForegroundColour(lfg
);
4653 m_colLabelWin
->SetOwnBackgroundColour(lbg
);
4655 m_gridWin
->SetOwnForegroundColour(gfg
);
4656 m_gridWin
->SetOwnBackgroundColour(gbg
);
4661 bool wxGrid::CreateGrid( int numRows
, int numCols
,
4662 wxGridSelectionModes selmode
)
4664 wxCHECK_MSG( !m_created
,
4666 wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
4668 return SetTable(new wxGridStringTable(numRows
, numCols
), true, selmode
);
4671 void wxGrid::SetSelectionMode(wxGridSelectionModes selmode
)
4673 wxCHECK_RET( m_created
,
4674 wxT("Called wxGrid::SetSelectionMode() before calling CreateGrid()") );
4676 m_selection
->SetSelectionMode( selmode
);
4679 wxGrid::wxGridSelectionModes
wxGrid::GetSelectionMode() const
4681 wxCHECK_MSG( m_created
, wxGridSelectCells
,
4682 wxT("Called wxGrid::GetSelectionMode() before calling CreateGrid()") );
4684 return m_selection
->GetSelectionMode();
4688 wxGrid::SetTable(wxGridTableBase
*table
,
4690 wxGrid::wxGridSelectionModes selmode
)
4692 bool checkSelection
= false;
4695 // stop all processing
4700 m_table
->SetView(0);
4712 checkSelection
= true;
4714 // kill row and column size arrays
4715 m_colWidths
.Empty();
4716 m_colRights
.Empty();
4717 m_rowHeights
.Empty();
4718 m_rowBottoms
.Empty();
4723 m_numRows
= table
->GetNumberRows();
4724 m_numCols
= table
->GetNumberCols();
4727 m_table
->SetView( this );
4728 m_ownTable
= takeOwnership
;
4729 m_selection
= new wxGridSelection( this, selmode
);
4732 // If the newly set table is smaller than the
4733 // original one current cell and selection regions
4734 // might be invalid,
4735 m_selectedBlockCorner
= wxGridNoCellCoords
;
4736 m_currentCellCoords
=
4737 wxGridCellCoords(wxMin(m_numRows
, m_currentCellCoords
.GetRow()),
4738 wxMin(m_numCols
, m_currentCellCoords
.GetCol()));
4739 if (m_selectedBlockTopLeft
.GetRow() >= m_numRows
||
4740 m_selectedBlockTopLeft
.GetCol() >= m_numCols
)
4742 m_selectedBlockTopLeft
= wxGridNoCellCoords
;
4743 m_selectedBlockBottomRight
= wxGridNoCellCoords
;
4746 m_selectedBlockBottomRight
=
4747 wxGridCellCoords(wxMin(m_numRows
,
4748 m_selectedBlockBottomRight
.GetRow()),
4750 m_selectedBlockBottomRight
.GetCol()));
4760 void wxGrid::InitVars()
4764 m_cornerLabelWin
= NULL
;
4765 m_rowLabelWin
= NULL
;
4766 m_colLabelWin
= NULL
;
4773 m_defaultCellAttr
= NULL
;
4774 m_typeRegistry
= NULL
;
4775 m_winCapture
= NULL
;
4780 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
4781 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
4783 if ( m_rowLabelWin
)
4785 m_labelBackgroundColour
= m_rowLabelWin
->GetBackgroundColour();
4789 m_labelBackgroundColour
= *wxWHITE
;
4792 m_labelTextColour
= *wxBLACK
;
4795 m_attrCache
.row
= -1;
4796 m_attrCache
.col
= -1;
4797 m_attrCache
.attr
= NULL
;
4799 m_labelFont
= GetFont();
4800 m_labelFont
.SetWeight( wxBOLD
);
4802 m_rowLabelHorizAlign
= wxALIGN_CENTRE
;
4803 m_rowLabelVertAlign
= wxALIGN_CENTRE
;
4805 m_colLabelHorizAlign
= wxALIGN_CENTRE
;
4806 m_colLabelVertAlign
= wxALIGN_CENTRE
;
4807 m_colLabelTextOrientation
= wxHORIZONTAL
;
4809 m_defaultColWidth
= WXGRID_DEFAULT_COL_WIDTH
;
4810 m_defaultRowHeight
= m_gridWin
->GetCharHeight();
4812 m_minAcceptableColWidth
= WXGRID_MIN_COL_WIDTH
;
4813 m_minAcceptableRowHeight
= WXGRID_MIN_ROW_HEIGHT
;
4815 #if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl()
4816 m_defaultRowHeight
+= 8;
4818 m_defaultRowHeight
+= 4;
4821 m_gridLineColour
= wxColour( 192,192,192 );
4822 m_gridLinesEnabled
= true;
4823 m_gridLinesClipHorz
=
4824 m_gridLinesClipVert
= true;
4825 m_cellHighlightColour
= *wxBLACK
;
4826 m_cellHighlightPenWidth
= 2;
4827 m_cellHighlightROPenWidth
= 1;
4829 m_canDragColMove
= false;
4831 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
4832 m_winCapture
= NULL
;
4833 m_canDragRowSize
= true;
4834 m_canDragColSize
= true;
4835 m_canDragGridSize
= true;
4836 m_canDragCell
= false;
4838 m_dragRowOrCol
= -1;
4839 m_isDragging
= false;
4840 m_startDragPos
= wxDefaultPosition
;
4841 m_nativeColumnLabels
= false;
4843 m_waitForSlowClick
= false;
4845 m_rowResizeCursor
= wxCursor( wxCURSOR_SIZENS
);
4846 m_colResizeCursor
= wxCursor( wxCURSOR_SIZEWE
);
4848 m_currentCellCoords
= wxGridNoCellCoords
;
4852 m_selectionBackground
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
);
4853 m_selectionForeground
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
4855 m_editable
= true; // default for whole grid
4857 m_inOnKeyDown
= false;
4863 m_scrollLineX
= GRID_SCROLL_LINE_X
;
4864 m_scrollLineY
= GRID_SCROLL_LINE_Y
;
4867 // ----------------------------------------------------------------------------
4868 // the idea is to call these functions only when necessary because they create
4869 // quite big arrays which eat memory mostly unnecessary - in particular, if
4870 // default widths/heights are used for all rows/columns, we may not use these
4873 // with some extra code, it should be possible to only store the widths/heights
4874 // different from default ones (resulting in space savings for huge grids) but
4875 // this is not done currently
4876 // ----------------------------------------------------------------------------
4878 void wxGrid::InitRowHeights()
4880 m_rowHeights
.Empty();
4881 m_rowBottoms
.Empty();
4883 m_rowHeights
.Alloc( m_numRows
);
4884 m_rowBottoms
.Alloc( m_numRows
);
4886 m_rowHeights
.Add( m_defaultRowHeight
, m_numRows
);
4889 for ( int i
= 0; i
< m_numRows
; i
++ )
4891 rowBottom
+= m_defaultRowHeight
;
4892 m_rowBottoms
.Add( rowBottom
);
4896 void wxGrid::InitColWidths()
4898 m_colWidths
.Empty();
4899 m_colRights
.Empty();
4901 m_colWidths
.Alloc( m_numCols
);
4902 m_colRights
.Alloc( m_numCols
);
4904 m_colWidths
.Add( m_defaultColWidth
, m_numCols
);
4907 for ( int i
= 0; i
< m_numCols
; i
++ )
4909 colRight
= ( GetColPos( i
) + 1 ) * m_defaultColWidth
;
4910 m_colRights
.Add( colRight
);
4914 int wxGrid::GetColWidth(int col
) const
4916 return m_colWidths
.IsEmpty() ? m_defaultColWidth
: m_colWidths
[col
];
4919 int wxGrid::GetColLeft(int col
) const
4921 return m_colRights
.IsEmpty() ? GetColPos( col
) * m_defaultColWidth
4922 : m_colRights
[col
] - m_colWidths
[col
];
4925 int wxGrid::GetColRight(int col
) const
4927 return m_colRights
.IsEmpty() ? (GetColPos( col
) + 1) * m_defaultColWidth
4931 int wxGrid::GetRowHeight(int row
) const
4933 return m_rowHeights
.IsEmpty() ? m_defaultRowHeight
: m_rowHeights
[row
];
4936 int wxGrid::GetRowTop(int row
) const
4938 return m_rowBottoms
.IsEmpty() ? row
* m_defaultRowHeight
4939 : m_rowBottoms
[row
] - m_rowHeights
[row
];
4942 int wxGrid::GetRowBottom(int row
) const
4944 return m_rowBottoms
.IsEmpty() ? (row
+ 1) * m_defaultRowHeight
4945 : m_rowBottoms
[row
];
4948 void wxGrid::CalcDimensions()
4950 // compute the size of the scrollable area
4951 int w
= m_numCols
> 0 ? GetColRight(GetColAt(m_numCols
- 1)) : 0;
4952 int h
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) : 0;
4957 // take into account editor if shown
4958 if ( IsCellEditControlShown() )
4961 int r
= m_currentCellCoords
.GetRow();
4962 int c
= m_currentCellCoords
.GetCol();
4963 int x
= GetColLeft(c
);
4964 int y
= GetRowTop(r
);
4966 // how big is the editor
4967 wxGridCellAttr
* attr
= GetCellAttr(r
, c
);
4968 wxGridCellEditor
* editor
= attr
->GetEditor(this, r
, c
);
4969 editor
->GetControl()->GetSize(&w2
, &h2
);
4980 // preserve (more or less) the previous position
4982 GetViewStart( &x
, &y
);
4984 // ensure the position is valid for the new scroll ranges
4986 x
= wxMax( w
- 1, 0 );
4988 y
= wxMax( h
- 1, 0 );
4990 // update the virtual size and refresh the scrollbars to reflect it
4991 m_gridWin
->SetVirtualSize(w
, h
);
4995 // if our OnSize() hadn't been called (it would if we have scrollbars), we
4996 // still must reposition the children
5000 wxSize
wxGrid::GetSizeAvailableForScrollTarget(const wxSize
& size
)
5002 wxSize
sizeGridWin(size
);
5003 sizeGridWin
.x
-= m_rowLabelWidth
;
5004 sizeGridWin
.y
-= m_colLabelHeight
;
5009 void wxGrid::CalcWindowSizes()
5011 // escape if the window is has not been fully created yet
5013 if ( m_cornerLabelWin
== NULL
)
5017 GetClientSize( &cw
, &ch
);
5019 // the grid may be too small to have enough space for the labels yet, don't
5020 // size the windows to negative sizes in this case
5021 int gw
= cw
- m_rowLabelWidth
;
5022 int gh
= ch
- m_colLabelHeight
;
5028 if ( m_cornerLabelWin
&& m_cornerLabelWin
->IsShown() )
5029 m_cornerLabelWin
->SetSize( 0, 0, m_rowLabelWidth
, m_colLabelHeight
);
5031 if ( m_colLabelWin
&& m_colLabelWin
->IsShown() )
5032 m_colLabelWin
->SetSize( m_rowLabelWidth
, 0, gw
, m_colLabelHeight
);
5034 if ( m_rowLabelWin
&& m_rowLabelWin
->IsShown() )
5035 m_rowLabelWin
->SetSize( 0, m_colLabelHeight
, m_rowLabelWidth
, gh
);
5037 if ( m_gridWin
&& m_gridWin
->IsShown() )
5038 m_gridWin
->SetSize( m_rowLabelWidth
, m_colLabelHeight
, gw
, gh
);
5041 // this is called when the grid table sends a message
5042 // to indicate that it has been redimensioned
5044 bool wxGrid::Redimension( wxGridTableMessage
& msg
)
5047 bool result
= false;
5049 // Clear the attribute cache as the attribute might refer to a different
5050 // cell than stored in the cache after adding/removing rows/columns.
5053 // By the same reasoning, the editor should be dismissed if columns are
5054 // added or removed. And for consistency, it should IMHO always be
5055 // removed, not only if the cell "underneath" it actually changes.
5056 // For now, I intentionally do not save the editor's content as the
5057 // cell it might want to save that stuff to might no longer exist.
5058 HideCellEditControl();
5061 // if we were using the default widths/heights so far, we must change them
5063 if ( m_colWidths
.IsEmpty() )
5068 if ( m_rowHeights
.IsEmpty() )
5074 switch ( msg
.GetId() )
5076 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
5078 size_t pos
= msg
.GetCommandInt();
5079 int numRows
= msg
.GetCommandInt2();
5081 m_numRows
+= numRows
;
5083 if ( !m_rowHeights
.IsEmpty() )
5085 m_rowHeights
.Insert( m_defaultRowHeight
, pos
, numRows
);
5086 m_rowBottoms
.Insert( 0, pos
, numRows
);
5090 bottom
= m_rowBottoms
[pos
- 1];
5092 for ( i
= pos
; i
< m_numRows
; i
++ )
5094 bottom
+= m_rowHeights
[i
];
5095 m_rowBottoms
[i
] = bottom
;
5099 if ( m_currentCellCoords
== wxGridNoCellCoords
)
5101 // if we have just inserted cols into an empty grid the current
5102 // cell will be undefined...
5104 SetCurrentCell( 0, 0 );
5108 m_selection
->UpdateRows( pos
, numRows
);
5109 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
5111 attrProvider
->UpdateAttrRows( pos
, numRows
);
5113 if ( !GetBatchCount() )
5116 m_rowLabelWin
->Refresh();
5122 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
5124 int numRows
= msg
.GetCommandInt();
5125 int oldNumRows
= m_numRows
;
5126 m_numRows
+= numRows
;
5128 if ( !m_rowHeights
.IsEmpty() )
5130 m_rowHeights
.Add( m_defaultRowHeight
, numRows
);
5131 m_rowBottoms
.Add( 0, numRows
);
5134 if ( oldNumRows
> 0 )
5135 bottom
= m_rowBottoms
[oldNumRows
- 1];
5137 for ( i
= oldNumRows
; i
< m_numRows
; i
++ )
5139 bottom
+= m_rowHeights
[i
];
5140 m_rowBottoms
[i
] = bottom
;
5144 if ( m_currentCellCoords
== wxGridNoCellCoords
)
5146 // if we have just inserted cols into an empty grid the current
5147 // cell will be undefined...
5149 SetCurrentCell( 0, 0 );
5152 if ( !GetBatchCount() )
5155 m_rowLabelWin
->Refresh();
5161 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
5163 size_t pos
= msg
.GetCommandInt();
5164 int numRows
= msg
.GetCommandInt2();
5165 m_numRows
-= numRows
;
5167 if ( !m_rowHeights
.IsEmpty() )
5169 m_rowHeights
.RemoveAt( pos
, numRows
);
5170 m_rowBottoms
.RemoveAt( pos
, numRows
);
5173 for ( i
= 0; i
< m_numRows
; i
++ )
5175 h
+= m_rowHeights
[i
];
5176 m_rowBottoms
[i
] = h
;
5182 m_currentCellCoords
= wxGridNoCellCoords
;
5186 if ( m_currentCellCoords
.GetRow() >= m_numRows
)
5187 m_currentCellCoords
.Set( 0, 0 );
5191 m_selection
->UpdateRows( pos
, -((int)numRows
) );
5192 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
5195 attrProvider
->UpdateAttrRows( pos
, -((int)numRows
) );
5197 // ifdef'd out following patch from Paul Gammans
5199 // No need to touch column attributes, unless we
5200 // removed _all_ rows, in this case, we remove
5201 // all column attributes.
5202 // I hate to do this here, but the
5203 // needed data is not available inside UpdateAttrRows.
5204 if ( !GetNumberRows() )
5205 attrProvider
->UpdateAttrCols( 0, -GetNumberCols() );
5209 if ( !GetBatchCount() )
5212 m_rowLabelWin
->Refresh();
5218 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
5220 size_t pos
= msg
.GetCommandInt();
5221 int numCols
= msg
.GetCommandInt2();
5222 m_numCols
+= numCols
;
5224 if ( !m_colAt
.IsEmpty() )
5226 //Shift the column IDs
5228 for ( i
= 0; i
< m_numCols
- numCols
; i
++ )
5230 if ( m_colAt
[i
] >= (int)pos
)
5231 m_colAt
[i
] += numCols
;
5234 m_colAt
.Insert( pos
, pos
, numCols
);
5236 //Set the new columns' positions
5237 for ( i
= pos
+ 1; i
< (int)pos
+ numCols
; i
++ )
5243 if ( !m_colWidths
.IsEmpty() )
5245 m_colWidths
.Insert( m_defaultColWidth
, pos
, numCols
);
5246 m_colRights
.Insert( 0, pos
, numCols
);
5250 right
= m_colRights
[GetColAt( pos
- 1 )];
5253 for ( colPos
= pos
; colPos
< m_numCols
; colPos
++ )
5255 i
= GetColAt( colPos
);
5257 right
+= m_colWidths
[i
];
5258 m_colRights
[i
] = right
;
5262 if ( m_currentCellCoords
== wxGridNoCellCoords
)
5264 // if we have just inserted cols into an empty grid the current
5265 // cell will be undefined...
5267 SetCurrentCell( 0, 0 );
5271 m_selection
->UpdateCols( pos
, numCols
);
5272 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
5274 attrProvider
->UpdateAttrCols( pos
, numCols
);
5275 if ( !GetBatchCount() )
5278 m_colLabelWin
->Refresh();
5284 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
5286 int numCols
= msg
.GetCommandInt();
5287 int oldNumCols
= m_numCols
;
5288 m_numCols
+= numCols
;
5290 if ( !m_colAt
.IsEmpty() )
5292 m_colAt
.Add( 0, numCols
);
5294 //Set the new columns' positions
5296 for ( i
= oldNumCols
; i
< m_numCols
; i
++ )
5302 if ( !m_colWidths
.IsEmpty() )
5304 m_colWidths
.Add( m_defaultColWidth
, numCols
);
5305 m_colRights
.Add( 0, numCols
);
5308 if ( oldNumCols
> 0 )
5309 right
= m_colRights
[GetColAt( oldNumCols
- 1 )];
5312 for ( colPos
= oldNumCols
; colPos
< m_numCols
; colPos
++ )
5314 i
= GetColAt( colPos
);
5316 right
+= m_colWidths
[i
];
5317 m_colRights
[i
] = right
;
5321 if ( m_currentCellCoords
== wxGridNoCellCoords
)
5323 // if we have just inserted cols into an empty grid the current
5324 // cell will be undefined...
5326 SetCurrentCell( 0, 0 );
5328 if ( !GetBatchCount() )
5331 m_colLabelWin
->Refresh();
5337 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
5339 size_t pos
= msg
.GetCommandInt();
5340 int numCols
= msg
.GetCommandInt2();
5341 m_numCols
-= numCols
;
5343 if ( !m_colAt
.IsEmpty() )
5345 int colID
= GetColAt( pos
);
5347 m_colAt
.RemoveAt( pos
, numCols
);
5349 //Shift the column IDs
5351 for ( colPos
= 0; colPos
< m_numCols
; colPos
++ )
5353 if ( m_colAt
[colPos
] > colID
)
5354 m_colAt
[colPos
] -= numCols
;
5358 if ( !m_colWidths
.IsEmpty() )
5360 m_colWidths
.RemoveAt( pos
, numCols
);
5361 m_colRights
.RemoveAt( pos
, numCols
);
5365 for ( colPos
= 0; colPos
< m_numCols
; colPos
++ )
5367 i
= GetColAt( colPos
);
5369 w
+= m_colWidths
[i
];
5376 m_currentCellCoords
= wxGridNoCellCoords
;
5380 if ( m_currentCellCoords
.GetCol() >= m_numCols
)
5381 m_currentCellCoords
.Set( 0, 0 );
5385 m_selection
->UpdateCols( pos
, -((int)numCols
) );
5386 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
5389 attrProvider
->UpdateAttrCols( pos
, -((int)numCols
) );
5391 // ifdef'd out following patch from Paul Gammans
5393 // No need to touch row attributes, unless we
5394 // removed _all_ columns, in this case, we remove
5395 // all row attributes.
5396 // I hate to do this here, but the
5397 // needed data is not available inside UpdateAttrCols.
5398 if ( !GetNumberCols() )
5399 attrProvider
->UpdateAttrRows( 0, -GetNumberRows() );
5403 if ( !GetBatchCount() )
5406 m_colLabelWin
->Refresh();
5413 if (result
&& !GetBatchCount() )
5414 m_gridWin
->Refresh();
5419 wxArrayInt
wxGrid::CalcRowLabelsExposed( const wxRegion
& reg
) const
5421 wxRegionIterator
iter( reg
);
5424 wxArrayInt rowlabels
;
5431 // TODO: remove this when we can...
5432 // There is a bug in wxMotif that gives garbage update
5433 // rectangles if you jump-scroll a long way by clicking the
5434 // scrollbar with middle button. This is a work-around
5436 #if defined(__WXMOTIF__)
5438 m_gridWin
->GetClientSize( &cw
, &ch
);
5439 if ( r
.GetTop() > ch
)
5441 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
5444 // logical bounds of update region
5447 CalcUnscrolledPosition( 0, r
.GetTop(), &dummy
, &top
);
5448 CalcUnscrolledPosition( 0, r
.GetBottom(), &dummy
, &bottom
);
5450 // find the row labels within these bounds
5453 for ( row
= internalYToRow(top
); row
< m_numRows
; row
++ )
5455 if ( GetRowBottom(row
) < top
)
5458 if ( GetRowTop(row
) > bottom
)
5461 rowlabels
.Add( row
);
5470 wxArrayInt
wxGrid::CalcColLabelsExposed( const wxRegion
& reg
) const
5472 wxRegionIterator
iter( reg
);
5475 wxArrayInt colLabels
;
5482 // TODO: remove this when we can...
5483 // There is a bug in wxMotif that gives garbage update
5484 // rectangles if you jump-scroll a long way by clicking the
5485 // scrollbar with middle button. This is a work-around
5487 #if defined(__WXMOTIF__)
5489 m_gridWin
->GetClientSize( &cw
, &ch
);
5490 if ( r
.GetLeft() > cw
)
5492 r
.SetRight( wxMin( r
.GetRight(), cw
) );
5495 // logical bounds of update region
5498 CalcUnscrolledPosition( r
.GetLeft(), 0, &left
, &dummy
);
5499 CalcUnscrolledPosition( r
.GetRight(), 0, &right
, &dummy
);
5501 // find the cells within these bounds
5505 for ( colPos
= GetColPos( internalXToCol(left
) ); colPos
< m_numCols
; colPos
++ )
5507 col
= GetColAt( colPos
);
5509 if ( GetColRight(col
) < left
)
5512 if ( GetColLeft(col
) > right
)
5515 colLabels
.Add( col
);
5524 wxGridCellCoordsArray
wxGrid::CalcCellsExposed( const wxRegion
& reg
) const
5526 wxRegionIterator
iter( reg
);
5529 wxGridCellCoordsArray cellsExposed
;
5531 int left
, top
, right
, bottom
;
5536 // TODO: remove this when we can...
5537 // There is a bug in wxMotif that gives garbage update
5538 // rectangles if you jump-scroll a long way by clicking the
5539 // scrollbar with middle button. This is a work-around
5541 #if defined(__WXMOTIF__)
5543 m_gridWin
->GetClientSize( &cw
, &ch
);
5544 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
5545 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
5546 r
.SetRight( wxMin( r
.GetRight(), cw
) );
5547 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
5550 // logical bounds of update region
5552 CalcUnscrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
5553 CalcUnscrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
5555 // find the cells within these bounds
5558 for ( row
= internalYToRow(top
); row
< m_numRows
; row
++ )
5560 if ( GetRowBottom(row
) <= top
)
5563 if ( GetRowTop(row
) > bottom
)
5567 for ( colPos
= GetColPos( internalXToCol(left
) ); colPos
< m_numCols
; colPos
++ )
5569 col
= GetColAt( colPos
);
5571 if ( GetColRight(col
) <= left
)
5574 if ( GetColLeft(col
) > right
)
5577 cellsExposed
.Add( wxGridCellCoords( row
, col
) );
5584 return cellsExposed
;
5588 void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent
& event
)
5591 wxPoint
pos( event
.GetPosition() );
5592 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
5594 if ( event
.Dragging() )
5598 m_isDragging
= true;
5599 m_rowLabelWin
->CaptureMouse();
5602 if ( event
.LeftIsDown() )
5604 switch ( m_cursorMode
)
5606 case WXGRID_CURSOR_RESIZE_ROW
:
5608 int cw
, ch
, left
, dummy
;
5609 m_gridWin
->GetClientSize( &cw
, &ch
);
5610 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
5612 wxClientDC
dc( m_gridWin
);
5615 GetRowTop(m_dragRowOrCol
) +
5616 GetRowMinimalHeight(m_dragRowOrCol
) );
5617 dc
.SetLogicalFunction(wxINVERT
);
5618 if ( m_dragLastPos
>= 0 )
5620 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
5622 dc
.DrawLine( left
, y
, left
+cw
, y
);
5627 case WXGRID_CURSOR_SELECT_ROW
:
5629 if ( (row
= YToRow( y
)) >= 0 )
5632 m_selection
->SelectRow(row
, event
);
5637 // default label to suppress warnings about "enumeration value
5638 // 'xxx' not handled in switch
5646 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
5651 if (m_rowLabelWin
->HasCapture())
5652 m_rowLabelWin
->ReleaseMouse();
5653 m_isDragging
= false;
5656 // ------------ Entering or leaving the window
5658 if ( event
.Entering() || event
.Leaving() )
5660 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
5663 // ------------ Left button pressed
5665 else if ( event
.LeftDown() )
5667 // don't send a label click event for a hit on the
5668 // edge of the row label - this is probably the user
5669 // wanting to resize the row
5671 if ( YToEdgeOfRow(y
) < 0 )
5675 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, row
, -1, event
) )
5677 if ( !event
.ShiftDown() && !event
.CmdDown() )
5681 if ( event
.ShiftDown() )
5683 m_selection
->SelectBlock
5685 m_currentCellCoords
.GetRow(), 0,
5686 row
, GetNumberCols() - 1,
5692 m_selection
->SelectRow(row
, event
);
5696 ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW
, m_rowLabelWin
);
5701 // starting to drag-resize a row
5702 if ( CanDragRowSize() )
5703 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
);
5707 // ------------ Left double click
5709 else if (event
.LeftDClick() )
5711 row
= YToEdgeOfRow(y
);
5716 !SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, row
, -1, event
) )
5718 // no default action at the moment
5723 // adjust row height depending on label text
5724 AutoSizeRowLabelSize( row
);
5726 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5731 // ------------ Left button released
5733 else if ( event
.LeftUp() )
5735 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
5737 DoEndDragResizeRow();
5739 // Note: we are ending the event *after* doing
5740 // default processing in this case
5742 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
5745 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
5749 // ------------ Right button down
5751 else if ( event
.RightDown() )
5755 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, row
, -1, event
) )
5757 // no default action at the moment
5761 // ------------ Right double click
5763 else if ( event
.RightDClick() )
5767 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, row
, -1, event
) )
5769 // no default action at the moment
5773 // ------------ No buttons down and mouse moving
5775 else if ( event
.Moving() )
5777 m_dragRowOrCol
= YToEdgeOfRow( y
);
5778 if ( m_dragRowOrCol
>= 0 )
5780 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5782 // don't capture the mouse yet
5783 if ( CanDragRowSize() )
5784 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
, false);
5787 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5789 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
, false);
5794 void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent
& event
)
5797 wxPoint
pos( event
.GetPosition() );
5798 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
5800 if ( event
.Dragging() )
5804 m_isDragging
= true;
5805 m_colLabelWin
->CaptureMouse();
5807 if ( m_cursorMode
== WXGRID_CURSOR_MOVE_COL
)
5808 m_dragRowOrCol
= XToCol( x
);
5811 if ( event
.LeftIsDown() )
5813 switch ( m_cursorMode
)
5815 case WXGRID_CURSOR_RESIZE_COL
:
5817 int cw
, ch
, dummy
, top
;
5818 m_gridWin
->GetClientSize( &cw
, &ch
);
5819 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
5821 wxClientDC
dc( m_gridWin
);
5824 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
5825 GetColMinimalWidth(m_dragRowOrCol
));
5826 dc
.SetLogicalFunction(wxINVERT
);
5827 if ( m_dragLastPos
>= 0 )
5829 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ ch
);
5831 dc
.DrawLine( x
, top
, x
, top
+ ch
);
5836 case WXGRID_CURSOR_SELECT_COL
:
5838 if ( (col
= XToCol( x
)) >= 0 )
5841 m_selection
->SelectCol(col
, event
);
5846 case WXGRID_CURSOR_MOVE_COL
:
5849 m_moveToCol
= GetColAt( 0 );
5851 m_moveToCol
= XToCol( x
);
5855 if ( m_moveToCol
< 0 )
5856 markerX
= GetColRight( GetColAt( m_numCols
- 1 ) );
5857 else if ( x
>= (GetColLeft( m_moveToCol
) + (GetColWidth(m_moveToCol
) / 2)) )
5859 m_moveToCol
= GetColAt( GetColPos( m_moveToCol
) + 1 );
5860 if ( m_moveToCol
< 0 )
5861 markerX
= GetColRight( GetColAt( m_numCols
- 1 ) );
5863 markerX
= GetColLeft( m_moveToCol
);
5866 markerX
= GetColLeft( m_moveToCol
);
5868 if ( markerX
!= m_dragLastPos
)
5870 wxClientDC
dc( m_colLabelWin
);
5874 m_colLabelWin
->GetClientSize( &cw
, &ch
);
5878 //Clean up the last indicator
5879 if ( m_dragLastPos
>= 0 )
5881 wxPen
pen( m_colLabelWin
->GetBackgroundColour(), 2 );
5883 dc
.DrawLine( m_dragLastPos
+ 1, 0, m_dragLastPos
+ 1, ch
);
5884 dc
.SetPen(wxNullPen
);
5886 if ( XToCol( m_dragLastPos
) != -1 )
5887 DrawColLabel( dc
, XToCol( m_dragLastPos
) );
5890 const wxColour
*color
;
5891 //Moving to the same place? Don't draw a marker
5892 if ( (m_moveToCol
== m_dragRowOrCol
)
5893 || (GetColPos( m_moveToCol
) == GetColPos( m_dragRowOrCol
) + 1)
5894 || (m_moveToCol
< 0 && m_dragRowOrCol
== GetColAt( m_numCols
- 1 )))
5895 color
= wxLIGHT_GREY
;
5900 wxPen
pen( *color
, 2 );
5903 dc
.DrawLine( markerX
, 0, markerX
, ch
);
5905 dc
.SetPen(wxNullPen
);
5907 m_dragLastPos
= markerX
- 1;
5912 // default label to suppress warnings about "enumeration value
5913 // 'xxx' not handled in switch
5921 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
5926 if (m_colLabelWin
->HasCapture())
5927 m_colLabelWin
->ReleaseMouse();
5928 m_isDragging
= false;
5931 // ------------ Entering or leaving the window
5933 if ( event
.Entering() || event
.Leaving() )
5935 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5938 // ------------ Left button pressed
5940 else if ( event
.LeftDown() )
5942 // don't send a label click event for a hit on the
5943 // edge of the col label - this is probably the user
5944 // wanting to resize the col
5946 if ( XToEdgeOfCol(x
) < 0 )
5950 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, col
, event
) )
5952 if ( m_canDragColMove
)
5954 //Show button as pressed
5955 wxClientDC
dc( m_colLabelWin
);
5956 int colLeft
= GetColLeft( col
);
5957 int colRight
= GetColRight( col
) - 1;
5958 dc
.SetPen( wxPen( m_colLabelWin
->GetBackgroundColour(), 1 ) );
5959 dc
.DrawLine( colLeft
, 1, colLeft
, m_colLabelHeight
-1 );
5960 dc
.DrawLine( colLeft
, 1, colRight
, 1 );
5962 ChangeCursorMode(WXGRID_CURSOR_MOVE_COL
, m_colLabelWin
);
5966 if ( !event
.ShiftDown() && !event
.CmdDown() )
5970 if ( event
.ShiftDown() )
5972 m_selection
->SelectBlock
5974 0, m_currentCellCoords
.GetCol(),
5975 GetNumberRows() - 1, col
,
5981 m_selection
->SelectCol(col
, event
);
5985 ChangeCursorMode(WXGRID_CURSOR_SELECT_COL
, m_colLabelWin
);
5991 // starting to drag-resize a col
5993 if ( CanDragColSize() )
5994 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
);
5998 // ------------ Left double click
6000 if ( event
.LeftDClick() )
6002 col
= XToEdgeOfCol(x
);
6007 ! SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, col
, event
) )
6009 // no default action at the moment
6014 // adjust column width depending on label text
6015 AutoSizeColLabelSize( col
);
6017 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
6022 // ------------ Left button released
6024 else if ( event
.LeftUp() )
6026 switch ( m_cursorMode
)
6028 case WXGRID_CURSOR_RESIZE_COL
:
6029 DoEndDragResizeCol();
6031 // Note: we are ending the event *after* doing
6032 // default processing in this case
6034 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
6037 case WXGRID_CURSOR_MOVE_COL
:
6040 SendEvent( wxEVT_GRID_COL_MOVE
, -1, m_dragRowOrCol
, event
);
6043 case WXGRID_CURSOR_SELECT_COL
:
6044 case WXGRID_CURSOR_SELECT_CELL
:
6045 case WXGRID_CURSOR_RESIZE_ROW
:
6046 case WXGRID_CURSOR_SELECT_ROW
:
6047 // nothing to do (?)
6051 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
6055 // ------------ Right button down
6057 else if ( event
.RightDown() )
6061 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, col
, event
) )
6063 // no default action at the moment
6067 // ------------ Right double click
6069 else if ( event
.RightDClick() )
6073 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, col
, event
) )
6075 // no default action at the moment
6079 // ------------ No buttons down and mouse moving
6081 else if ( event
.Moving() )
6083 m_dragRowOrCol
= XToEdgeOfCol( x
);
6084 if ( m_dragRowOrCol
>= 0 )
6086 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
6088 // don't capture the cursor yet
6089 if ( CanDragColSize() )
6090 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
, false);
6093 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
6095 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
, false);
6100 void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent
& event
)
6102 if ( event
.LeftDown() )
6104 // indicate corner label by having both row and
6107 if ( !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, -1, event
) )
6112 else if ( event
.LeftDClick() )
6114 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, -1, event
);
6116 else if ( event
.RightDown() )
6118 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, -1, event
) )
6120 // no default action at the moment
6123 else if ( event
.RightDClick() )
6125 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, -1, event
) )
6127 // no default action at the moment
6132 void wxGrid::CancelMouseCapture()
6134 // cancel operation currently in progress, whatever it is
6137 m_isDragging
= false;
6138 m_startDragPos
= wxDefaultPosition
;
6140 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
6141 m_winCapture
->SetCursor( *wxSTANDARD_CURSOR
);
6142 m_winCapture
= NULL
;
6144 // remove traces of whatever we drew on screen
6149 void wxGrid::ChangeCursorMode(CursorMode mode
,
6154 static const wxChar
*cursorModes
[] =
6164 wxLogTrace(_T("grid"),
6165 _T("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
6166 win
== m_colLabelWin
? _T("colLabelWin")
6167 : win
? _T("rowLabelWin")
6169 cursorModes
[m_cursorMode
], cursorModes
[mode
]);
6172 if ( mode
== m_cursorMode
&&
6173 win
== m_winCapture
&&
6174 captureMouse
== (m_winCapture
!= NULL
))
6179 // by default use the grid itself
6185 if (m_winCapture
->HasCapture())
6186 m_winCapture
->ReleaseMouse();
6187 m_winCapture
= NULL
;
6190 m_cursorMode
= mode
;
6192 switch ( m_cursorMode
)
6194 case WXGRID_CURSOR_RESIZE_ROW
:
6195 win
->SetCursor( m_rowResizeCursor
);
6198 case WXGRID_CURSOR_RESIZE_COL
:
6199 win
->SetCursor( m_colResizeCursor
);
6202 case WXGRID_CURSOR_MOVE_COL
:
6203 win
->SetCursor( wxCursor(wxCURSOR_HAND
) );
6207 win
->SetCursor( *wxSTANDARD_CURSOR
);
6211 // we need to capture mouse when resizing
6212 bool resize
= m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
||
6213 m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
;
6215 if ( captureMouse
&& resize
)
6217 win
->CaptureMouse();
6222 // ----------------------------------------------------------------------------
6223 // grid mouse event processing
6224 // ----------------------------------------------------------------------------
6227 wxGrid::DoGridCellDrag(wxMouseEvent
& event
,
6228 const wxGridCellCoords
& coords
,
6231 if ( coords
== wxGridNoCellCoords
)
6232 return; // we're outside any valid cell
6234 // Hide the edit control, so it won't interfere with drag-shrinking.
6235 if ( IsCellEditControlShown() )
6237 HideCellEditControl();
6238 SaveEditControlValue();
6241 switch ( event
.GetModifiers() )
6244 if ( m_selectedBlockCorner
== wxGridNoCellCoords
)
6245 m_selectedBlockCorner
= coords
;
6246 UpdateBlockBeingSelected(m_selectedBlockCorner
, coords
);
6250 if ( CanDragCell() )
6254 if ( m_selectedBlockCorner
== wxGridNoCellCoords
)
6255 m_selectedBlockCorner
= coords
;
6257 SendEvent(wxEVT_GRID_CELL_BEGIN_DRAG
, coords
, event
);
6262 UpdateBlockBeingSelected(m_currentCellCoords
, coords
);
6266 // we don't handle the other key modifiers
6271 void wxGrid::DoGridLineDrag(wxMouseEvent
& event
, const wxGridOperations
& oper
)
6273 wxClientDC
dc(m_gridWin
);
6275 dc
.SetLogicalFunction(wxINVERT
);
6277 const wxRect
rectWin(CalcUnscrolledPosition(wxPoint(0, 0)),
6278 m_gridWin
->GetClientSize());
6280 // erase the previously drawn line, if any
6281 if ( m_dragLastPos
>= 0 )
6282 oper
.DrawParallelLineInRect(dc
, rectWin
, m_dragLastPos
);
6284 // we need the vertical position for rows and horizontal for columns here
6285 m_dragLastPos
= oper
.Dual().Select(CalcUnscrolledPosition(event
.GetPosition()));
6287 // don't allow resizing beneath the minimal size
6288 const int posMin
= oper
.GetLineStartPos(this, m_dragRowOrCol
) +
6289 oper
.GetMinimalLineSize(this, m_dragRowOrCol
);
6290 if ( m_dragLastPos
< posMin
)
6291 m_dragLastPos
= posMin
;
6293 // and draw it at the new position
6294 oper
.DrawParallelLineInRect(dc
, rectWin
, m_dragLastPos
);
6297 void wxGrid::DoGridDragEvent(wxMouseEvent
& event
, const wxGridCellCoords
& coords
)
6299 if ( !m_isDragging
)
6301 // Don't start doing anything until the mouse has been dragged far
6303 const wxPoint
& pt
= event
.GetPosition();
6304 if ( m_startDragPos
== wxDefaultPosition
)
6306 m_startDragPos
= pt
;
6310 if ( abs(m_startDragPos
.x
- pt
.x
) <= DRAG_SENSITIVITY
&&
6311 abs(m_startDragPos
.y
- pt
.y
) <= DRAG_SENSITIVITY
)
6315 const bool isFirstDrag
= !m_isDragging
;
6316 m_isDragging
= true;
6318 switch ( m_cursorMode
)
6320 case WXGRID_CURSOR_SELECT_CELL
:
6321 DoGridCellDrag(event
, coords
, isFirstDrag
);
6324 case WXGRID_CURSOR_RESIZE_ROW
:
6325 DoGridLineDrag(event
, wxGridRowOperations());
6328 case WXGRID_CURSOR_RESIZE_COL
:
6329 DoGridLineDrag(event
, wxGridColumnOperations());
6338 m_winCapture
= m_gridWin
;
6339 m_winCapture
->CaptureMouse();
6344 wxGrid::DoGridCellLeftDown(wxMouseEvent
& event
,
6345 const wxGridCellCoords
& coords
,
6348 if ( SendEvent(wxEVT_GRID_CELL_LEFT_CLICK
, coords
, event
) )
6350 // event handled by user code, no need to do anything here
6354 if ( !event
.CmdDown() )
6357 if ( event
.ShiftDown() )
6361 m_selection
->SelectBlock(m_currentCellCoords
, coords
, event
);
6362 m_selectedBlockCorner
= coords
;
6365 else if ( XToEdgeOfCol(pos
.x
) < 0 && YToEdgeOfRow(pos
.y
) < 0 )
6367 DisableCellEditControl();
6368 MakeCellVisible( coords
);
6370 if ( event
.CmdDown() )
6374 m_selection
->ToggleCellSelection(coords
, event
);
6377 m_selectedBlockTopLeft
= wxGridNoCellCoords
;
6378 m_selectedBlockBottomRight
= wxGridNoCellCoords
;
6379 m_selectedBlockCorner
= coords
;
6383 m_waitForSlowClick
= m_currentCellCoords
== coords
&&
6384 coords
!= wxGridNoCellCoords
;
6385 SetCurrentCell( coords
);
6391 wxGrid::DoGridCellLeftDClick(wxMouseEvent
& event
,
6392 const wxGridCellCoords
& coords
,
6395 if ( XToEdgeOfCol(pos
.x
) < 0 && YToEdgeOfRow(pos
.y
) < 0 )
6397 if ( !SendEvent(wxEVT_GRID_CELL_LEFT_DCLICK
, coords
, event
) )
6399 // we want double click to select a cell and start editing
6400 // (i.e. to behave in same way as sequence of two slow clicks):
6401 m_waitForSlowClick
= true;
6407 wxGrid::DoGridCellLeftUp(wxMouseEvent
& event
, const wxGridCellCoords
& coords
)
6409 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
6413 if (m_winCapture
->HasCapture())
6414 m_winCapture
->ReleaseMouse();
6415 m_winCapture
= NULL
;
6418 if ( coords
== m_currentCellCoords
&& m_waitForSlowClick
&& CanEnableCellControl() )
6421 EnableCellEditControl();
6423 wxGridCellAttr
*attr
= GetCellAttr(coords
);
6424 wxGridCellEditor
*editor
= attr
->GetEditor(this, coords
.GetRow(), coords
.GetCol());
6425 editor
->StartingClick();
6429 m_waitForSlowClick
= false;
6431 else if ( m_selectedBlockTopLeft
!= wxGridNoCellCoords
&&
6432 m_selectedBlockBottomRight
!= wxGridNoCellCoords
)
6436 m_selection
->SelectBlock( m_selectedBlockTopLeft
,
6437 m_selectedBlockBottomRight
,
6441 m_selectedBlockTopLeft
= wxGridNoCellCoords
;
6442 m_selectedBlockBottomRight
= wxGridNoCellCoords
;
6444 // Show the edit control, if it has been hidden for
6446 ShowCellEditControl();
6449 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
6451 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6452 DoEndDragResizeRow();
6454 // Note: we are ending the event *after* doing
6455 // default processing in this case
6457 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
6459 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
6461 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6462 DoEndDragResizeCol();
6464 // Note: we are ending the event *after* doing
6465 // default processing in this case
6467 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
6474 wxGrid::DoGridMouseMoveEvent(wxMouseEvent
& WXUNUSED(event
),
6475 const wxGridCellCoords
& coords
,
6478 if ( coords
.GetRow() < 0 || coords
.GetCol() < 0 )
6480 // out of grid cell area
6481 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6485 int dragRow
= YToEdgeOfRow( pos
.y
);
6486 int dragCol
= XToEdgeOfCol( pos
.x
);
6488 // Dragging on the corner of a cell to resize in both
6489 // directions is not implemented yet...
6491 if ( dragRow
>= 0 && dragCol
>= 0 )
6493 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6499 m_dragRowOrCol
= dragRow
;
6501 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
6503 if ( CanDragRowSize() && CanDragGridSize() )
6504 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, NULL
, false);
6507 else if ( dragCol
>= 0 )
6509 m_dragRowOrCol
= dragCol
;
6511 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
6513 if ( CanDragColSize() && CanDragGridSize() )
6514 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, NULL
, false);
6517 else // Neither on a row or col edge
6519 if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
6521 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6526 void wxGrid::ProcessGridCellMouseEvent(wxMouseEvent
& event
)
6528 const wxPoint pos
= CalcUnscrolledPosition(event
.GetPosition());
6530 // coordinates of the cell under mouse
6531 wxGridCellCoords coords
= XYToCell(pos
);
6533 int cell_rows
, cell_cols
;
6534 GetCellSize( coords
.GetRow(), coords
.GetCol(), &cell_rows
, &cell_cols
);
6535 if ( (cell_rows
< 0) || (cell_cols
< 0) )
6537 coords
.SetRow(coords
.GetRow() + cell_rows
);
6538 coords
.SetCol(coords
.GetCol() + cell_cols
);
6541 if ( event
.Dragging() )
6543 if ( event
.LeftIsDown() )
6544 DoGridDragEvent(event
, coords
);
6550 m_isDragging
= false;
6551 m_startDragPos
= wxDefaultPosition
;
6553 // VZ: if we do this, the mode is reset to WXGRID_CURSOR_SELECT_CELL
6554 // immediately after it becomes WXGRID_CURSOR_RESIZE_ROW/COL under
6557 if ( event
.Entering() || event
.Leaving() )
6559 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6560 m_gridWin
->SetCursor( *wxSTANDARD_CURSOR
);
6564 // deal with various button presses
6565 if ( event
.IsButton() )
6567 if ( coords
!= wxGridNoCellCoords
)
6569 DisableCellEditControl();
6571 if ( event
.LeftDown() )
6572 DoGridCellLeftDown(event
, coords
, pos
);
6573 else if ( event
.LeftDClick() )
6574 DoGridCellLeftDClick(event
, coords
, pos
);
6575 else if ( event
.RightDown() )
6576 SendEvent(wxEVT_GRID_CELL_RIGHT_CLICK
, coords
, event
);
6577 else if ( event
.RightDClick() )
6578 SendEvent(wxEVT_GRID_CELL_RIGHT_DCLICK
, coords
, event
);
6581 // this one should be called even if we're not over any cell
6582 if ( event
.LeftUp() )
6584 DoGridCellLeftUp(event
, coords
);
6587 else if ( event
.Moving() )
6589 DoGridMouseMoveEvent(event
, coords
, pos
);
6591 else // unknown mouse event?
6597 void wxGrid::DoEndDragResizeLine(const wxGridOperations
& oper
)
6599 if ( m_dragLastPos
== -1 )
6602 const wxGridOperations
& doper
= oper
.Dual();
6604 const wxSize size
= m_gridWin
->GetClientSize();
6606 const wxPoint ptOrigin
= CalcUnscrolledPosition(wxPoint(0, 0));
6608 // erase the last line we drew
6609 wxClientDC
dc(m_gridWin
);
6611 dc
.SetLogicalFunction(wxINVERT
);
6613 const int posLineStart
= oper
.Select(ptOrigin
);
6614 const int posLineEnd
= oper
.Select(ptOrigin
) + oper
.Select(size
);
6616 oper
.DrawParallelLine(dc
, posLineStart
, posLineEnd
, m_dragLastPos
);
6618 // temporarily hide the edit control before resizing
6619 HideCellEditControl();
6620 SaveEditControlValue();
6622 // do resize the line
6623 const int lineStart
= oper
.GetLineStartPos(this, m_dragRowOrCol
);
6624 oper
.SetLineSize(this, m_dragRowOrCol
,
6625 wxMax(m_dragLastPos
- lineStart
,
6626 oper
.GetMinimalLineSize(this, m_dragRowOrCol
)));
6628 // refresh now if we're not frozen
6629 if ( !GetBatchCount() )
6631 // we need to refresh everything beyond the resized line in the header
6634 // get the position from which to refresh in the other direction
6635 wxRect
rect(CellToRect(oper
.MakeCoords(m_dragRowOrCol
, 0)));
6636 rect
.SetPosition(CalcScrolledPosition(rect
.GetPosition()));
6638 // we only need the ordinate (for rows) or abscissa (for columns) here,
6639 // and need to cover the entire window in the other direction
6640 oper
.Select(rect
) = 0;
6642 wxRect
rectHeader(rect
.GetPosition(),
6645 oper
.GetHeaderWindowSize(this),
6646 doper
.Select(size
) - doper
.Select(rect
)
6649 oper
.GetHeaderWindow(this)->Refresh(true, &rectHeader
);
6652 // also refresh the grid window: extend the rectangle
6655 oper
.SelectSize(rect
) = oper
.Select(size
);
6657 int subtractLines
= 0;
6658 const int lineStart
= oper
.PosToLine(this, posLineStart
);
6659 if ( lineStart
>= 0 )
6661 // ensure that if we have a multi-cell block we redraw all of
6662 // it by increasing the refresh area to cover it entirely if a
6663 // part of it is affected
6664 const int lineEnd
= oper
.PosToLine(this, posLineEnd
, true);
6665 for ( int line
= lineStart
; line
< lineEnd
; line
++ )
6667 int cellLines
= oper
.Select(
6668 GetCellSize(oper
.MakeCoords(m_dragRowOrCol
, line
)));
6669 if ( cellLines
< subtractLines
)
6670 subtractLines
= cellLines
;
6675 oper
.GetLineStartPos(this, m_dragRowOrCol
+ subtractLines
);
6676 startPos
= doper
.CalcScrolledPosition(this, startPos
);
6678 doper
.Select(rect
) = startPos
;
6679 doper
.SelectSize(rect
) = doper
.Select(size
) - startPos
;
6681 m_gridWin
->Refresh(false, &rect
);
6685 // show the edit control back again
6686 ShowCellEditControl();
6689 void wxGrid::DoEndDragResizeRow()
6691 DoEndDragResizeLine(wxGridRowOperations());
6694 void wxGrid::DoEndDragResizeCol()
6696 DoEndDragResizeLine(wxGridColumnOperations());
6699 void wxGrid::DoEndDragMoveCol()
6701 //The user clicked on the column but didn't actually drag
6702 if ( m_dragLastPos
< 0 )
6704 m_colLabelWin
->Refresh(); //Do this to "unpress" the column
6709 if ( m_moveToCol
== -1 )
6710 newPos
= m_numCols
- 1;
6713 newPos
= GetColPos( m_moveToCol
);
6714 if ( newPos
> GetColPos( m_dragRowOrCol
) )
6718 SetColPos( m_dragRowOrCol
, newPos
);
6721 void wxGrid::SetColPos( int colID
, int newPos
)
6723 if ( m_colAt
.IsEmpty() )
6725 m_colAt
.Alloc( m_numCols
);
6728 for ( i
= 0; i
< m_numCols
; i
++ )
6734 int oldPos
= GetColPos( colID
);
6736 //Reshuffle the m_colAt array
6737 if ( newPos
> oldPos
)
6740 for ( i
= oldPos
; i
< newPos
; i
++ )
6742 m_colAt
[i
] = m_colAt
[i
+1];
6748 for ( i
= oldPos
; i
> newPos
; i
-- )
6750 m_colAt
[i
] = m_colAt
[i
-1];
6754 m_colAt
[newPos
] = colID
;
6756 //Recalculate the column rights
6757 if ( !m_colWidths
.IsEmpty() )
6761 for ( colPos
= 0; colPos
< m_numCols
; colPos
++ )
6763 int colID
= GetColAt( colPos
);
6765 colRight
+= m_colWidths
[colID
];
6766 m_colRights
[colID
] = colRight
;
6770 m_colLabelWin
->Refresh();
6771 m_gridWin
->Refresh();
6776 void wxGrid::EnableDragColMove( bool enable
)
6778 if ( m_canDragColMove
== enable
)
6781 m_canDragColMove
= enable
;
6783 if ( !m_canDragColMove
)
6787 //Recalculate the column rights
6788 if ( !m_colWidths
.IsEmpty() )
6792 for ( colPos
= 0; colPos
< m_numCols
; colPos
++ )
6794 colRight
+= m_colWidths
[colPos
];
6795 m_colRights
[colPos
] = colRight
;
6799 m_colLabelWin
->Refresh();
6800 m_gridWin
->Refresh();
6806 // ------ interaction with data model
6808 bool wxGrid::ProcessTableMessage( wxGridTableMessage
& msg
)
6810 switch ( msg
.GetId() )
6812 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES
:
6813 return GetModelValues();
6815 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES
:
6816 return SetModelValues();
6818 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
6819 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
6820 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
6821 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
6822 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
6823 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
6824 return Redimension( msg
);
6831 // The behaviour of this function depends on the grid table class
6832 // Clear() function. For the default wxGridStringTable class the
6833 // behaviour is to replace all cell contents with wxEmptyString but
6834 // not to change the number of rows or cols.
6836 void wxGrid::ClearGrid()
6840 if (IsCellEditControlEnabled())
6841 DisableCellEditControl();
6844 if (!GetBatchCount())
6845 m_gridWin
->Refresh();
6850 wxGrid::DoModifyLines(bool (wxGridTableBase::*funcModify
)(size_t, size_t),
6851 int pos
, int num
, bool WXUNUSED(updateLabels
) )
6853 wxCHECK_MSG( m_created
, false, "must finish creating the grid first" );
6858 if ( IsCellEditControlEnabled() )
6859 DisableCellEditControl();
6861 return (m_table
->*funcModify
)(pos
, num
);
6863 // the table will have sent the results of the insert row
6864 // operation to this view object as a grid table message
6868 wxGrid::DoAppendLines(bool (wxGridTableBase::*funcAppend
)(size_t),
6869 int num
, bool WXUNUSED(updateLabels
))
6871 wxCHECK_MSG( m_created
, false, "must finish creating the grid first" );
6876 return (m_table
->*funcAppend
)(num
);
6880 // ----- event handlers
6883 // Generate a grid event based on a mouse event and return:
6884 // -1 if the event was vetoed
6885 // +1 if the event was processed (but not vetoed)
6886 // 0 if the event wasn't handled
6888 wxGrid::SendEvent(const wxEventType type
,
6890 wxMouseEvent
& mouseEv
)
6892 bool claimed
, vetoed
;
6894 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
6896 int rowOrCol
= (row
== -1 ? col
: row
);
6898 wxGridSizeEvent
gridEvt( GetId(),
6902 mouseEv
.GetX() + GetRowLabelSize(),
6903 mouseEv
.GetY() + GetColLabelSize(),
6906 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6907 vetoed
= !gridEvt
.IsAllowed();
6909 else if ( type
== wxEVT_GRID_RANGE_SELECT
)
6911 // Right now, it should _never_ end up here!
6912 wxGridRangeSelectEvent
gridEvt( GetId(),
6915 m_selectedBlockTopLeft
,
6916 m_selectedBlockBottomRight
,
6920 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6921 vetoed
= !gridEvt
.IsAllowed();
6923 else if ( type
== wxEVT_GRID_LABEL_LEFT_CLICK
||
6924 type
== wxEVT_GRID_LABEL_LEFT_DCLICK
||
6925 type
== wxEVT_GRID_LABEL_RIGHT_CLICK
||
6926 type
== wxEVT_GRID_LABEL_RIGHT_DCLICK
)
6928 wxPoint pos
= mouseEv
.GetPosition();
6930 if ( mouseEv
.GetEventObject() == GetGridRowLabelWindow() )
6931 pos
.y
+= GetColLabelSize();
6932 if ( mouseEv
.GetEventObject() == GetGridColLabelWindow() )
6933 pos
.x
+= GetRowLabelSize();
6935 wxGridEvent
gridEvt( GetId(),
6943 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6944 vetoed
= !gridEvt
.IsAllowed();
6948 wxGridEvent
gridEvt( GetId(),
6952 mouseEv
.GetX() + GetRowLabelSize(),
6953 mouseEv
.GetY() + GetColLabelSize(),
6956 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6957 vetoed
= !gridEvt
.IsAllowed();
6960 // A Veto'd event may not be `claimed' so test this first
6964 return claimed
? 1 : 0;
6967 // Generate a grid event of specified type, return value same as above
6969 int wxGrid::SendEvent(const wxEventType type
, int row
, int col
)
6971 bool claimed
, vetoed
;
6973 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
6975 int rowOrCol
= (row
== -1 ? col
: row
);
6977 wxGridSizeEvent
gridEvt( GetId(), type
, this, rowOrCol
);
6979 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6980 vetoed
= !gridEvt
.IsAllowed();
6984 wxGridEvent
gridEvt( GetId(), type
, this, row
, col
);
6986 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6987 vetoed
= !gridEvt
.IsAllowed();
6990 // A Veto'd event may not be `claimed' so test this first
6994 return claimed
? 1 : 0;
6997 void wxGrid::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
6999 // needed to prevent zillions of paint events on MSW
7003 void wxGrid::Refresh(bool eraseb
, const wxRect
* rect
)
7005 // Don't do anything if between Begin/EndBatch...
7006 // EndBatch() will do all this on the last nested one anyway.
7007 if ( m_created
&& !GetBatchCount() )
7009 // Refresh to get correct scrolled position:
7010 wxScrolledWindow::Refresh(eraseb
, rect
);
7014 int rect_x
, rect_y
, rectWidth
, rectHeight
;
7015 int width_label
, width_cell
, height_label
, height_cell
;
7018 // Copy rectangle can get scroll offsets..
7019 rect_x
= rect
->GetX();
7020 rect_y
= rect
->GetY();
7021 rectWidth
= rect
->GetWidth();
7022 rectHeight
= rect
->GetHeight();
7024 width_label
= m_rowLabelWidth
- rect_x
;
7025 if (width_label
> rectWidth
)
7026 width_label
= rectWidth
;
7028 height_label
= m_colLabelHeight
- rect_y
;
7029 if (height_label
> rectHeight
)
7030 height_label
= rectHeight
;
7032 if (rect_x
> m_rowLabelWidth
)
7034 x
= rect_x
- m_rowLabelWidth
;
7035 width_cell
= rectWidth
;
7040 width_cell
= rectWidth
- (m_rowLabelWidth
- rect_x
);
7043 if (rect_y
> m_colLabelHeight
)
7045 y
= rect_y
- m_colLabelHeight
;
7046 height_cell
= rectHeight
;
7051 height_cell
= rectHeight
- (m_colLabelHeight
- rect_y
);
7054 // Paint corner label part intersecting rect.
7055 if ( width_label
> 0 && height_label
> 0 )
7057 wxRect
anotherrect(rect_x
, rect_y
, width_label
, height_label
);
7058 m_cornerLabelWin
->Refresh(eraseb
, &anotherrect
);
7061 // Paint col labels part intersecting rect.
7062 if ( width_cell
> 0 && height_label
> 0 )
7064 wxRect
anotherrect(x
, rect_y
, width_cell
, height_label
);
7065 m_colLabelWin
->Refresh(eraseb
, &anotherrect
);
7068 // Paint row labels part intersecting rect.
7069 if ( width_label
> 0 && height_cell
> 0 )
7071 wxRect
anotherrect(rect_x
, y
, width_label
, height_cell
);
7072 m_rowLabelWin
->Refresh(eraseb
, &anotherrect
);
7075 // Paint cell area part intersecting rect.
7076 if ( width_cell
> 0 && height_cell
> 0 )
7078 wxRect
anotherrect(x
, y
, width_cell
, height_cell
);
7079 m_gridWin
->Refresh(eraseb
, &anotherrect
);
7084 m_cornerLabelWin
->Refresh(eraseb
, NULL
);
7085 m_colLabelWin
->Refresh(eraseb
, NULL
);
7086 m_rowLabelWin
->Refresh(eraseb
, NULL
);
7087 m_gridWin
->Refresh(eraseb
, NULL
);
7092 void wxGrid::OnSize(wxSizeEvent
& WXUNUSED(event
))
7094 if (m_targetWindow
!= this) // check whether initialisation has been done
7096 // reposition our children windows
7101 void wxGrid::OnKeyDown( wxKeyEvent
& event
)
7103 if ( m_inOnKeyDown
)
7105 // shouldn't be here - we are going round in circles...
7107 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") );
7110 m_inOnKeyDown
= true;
7112 // propagate the event up and see if it gets processed
7113 wxWindow
*parent
= GetParent();
7114 wxKeyEvent
keyEvt( event
);
7115 keyEvt
.SetEventObject( parent
);
7117 if ( !parent
->GetEventHandler()->ProcessEvent( keyEvt
) )
7119 if (GetLayoutDirection() == wxLayout_RightToLeft
)
7121 if (event
.GetKeyCode() == WXK_RIGHT
)
7122 event
.m_keyCode
= WXK_LEFT
;
7123 else if (event
.GetKeyCode() == WXK_LEFT
)
7124 event
.m_keyCode
= WXK_RIGHT
;
7127 // try local handlers
7128 switch ( event
.GetKeyCode() )
7131 if ( event
.ControlDown() )
7132 MoveCursorUpBlock( event
.ShiftDown() );
7134 MoveCursorUp( event
.ShiftDown() );
7138 if ( event
.ControlDown() )
7139 MoveCursorDownBlock( event
.ShiftDown() );
7141 MoveCursorDown( event
.ShiftDown() );
7145 if ( event
.ControlDown() )
7146 MoveCursorLeftBlock( event
.ShiftDown() );
7148 MoveCursorLeft( event
.ShiftDown() );
7152 if ( event
.ControlDown() )
7153 MoveCursorRightBlock( event
.ShiftDown() );
7155 MoveCursorRight( event
.ShiftDown() );
7159 case WXK_NUMPAD_ENTER
:
7160 if ( event
.ControlDown() )
7162 event
.Skip(); // to let the edit control have the return
7166 if ( GetGridCursorRow() < GetNumberRows()-1 )
7168 MoveCursorDown( event
.ShiftDown() );
7172 // at the bottom of a column
7173 DisableCellEditControl();
7183 if (event
.ShiftDown())
7185 if ( GetGridCursorCol() > 0 )
7187 MoveCursorLeft( false );
7192 DisableCellEditControl();
7197 if ( GetGridCursorCol() < GetNumberCols() - 1 )
7199 MoveCursorRight( false );
7204 DisableCellEditControl();
7210 if ( event
.ControlDown() )
7221 if ( event
.ControlDown() )
7223 GoToCell(m_numRows
- 1, m_numCols
- 1);
7240 // Ctrl-Space selects the current column, Shift-Space -- the
7241 // current row and Ctrl-Shift-Space -- everything
7242 switch ( m_selection
? event
.GetModifiers() : wxMOD_NONE
)
7245 m_selection
->SelectCol(m_currentCellCoords
.GetCol());
7249 m_selection
->SelectRow(m_currentCellCoords
.GetRow());
7252 case wxMOD_CONTROL
| wxMOD_SHIFT
:
7253 m_selection
->SelectBlock(0, 0,
7254 m_numRows
- 1, m_numCols
- 1);
7258 if ( !IsEditable() )
7260 MoveCursorRight(false);
7263 //else: fall through
7276 m_inOnKeyDown
= false;
7279 void wxGrid::OnKeyUp( wxKeyEvent
& event
)
7281 // try local handlers
7283 if ( event
.GetKeyCode() == WXK_SHIFT
)
7285 if ( m_selectedBlockTopLeft
!= wxGridNoCellCoords
&&
7286 m_selectedBlockBottomRight
!= wxGridNoCellCoords
)
7290 m_selection
->SelectBlock(
7291 m_selectedBlockTopLeft
,
7292 m_selectedBlockBottomRight
,
7297 m_selectedBlockTopLeft
= wxGridNoCellCoords
;
7298 m_selectedBlockBottomRight
= wxGridNoCellCoords
;
7299 m_selectedBlockCorner
= wxGridNoCellCoords
;
7303 void wxGrid::OnChar( wxKeyEvent
& event
)
7305 // is it possible to edit the current cell at all?
7306 if ( !IsCellEditControlEnabled() && CanEnableCellControl() )
7308 // yes, now check whether the cells editor accepts the key
7309 int row
= m_currentCellCoords
.GetRow();
7310 int col
= m_currentCellCoords
.GetCol();
7311 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7312 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
7314 // <F2> is special and will always start editing, for
7315 // other keys - ask the editor itself
7316 if ( (event
.GetKeyCode() == WXK_F2
&& !event
.HasModifiers())
7317 || editor
->IsAcceptedKey(event
) )
7319 // ensure cell is visble
7320 MakeCellVisible(row
, col
);
7321 EnableCellEditControl();
7323 // a problem can arise if the cell is not completely
7324 // visible (even after calling MakeCellVisible the
7325 // control is not created and calling StartingKey will
7327 if ( event
.GetKeyCode() != WXK_F2
&& editor
->IsCreated() && m_cellEditCtrlEnabled
)
7328 editor
->StartingKey(event
);
7344 void wxGrid::OnEraseBackground(wxEraseEvent
&)
7348 bool wxGrid::SetCurrentCell( const wxGridCellCoords
& coords
)
7350 if ( SendEvent(wxEVT_GRID_SELECT_CELL
, coords
) == -1 )
7352 // the event has been vetoed - do nothing
7356 #if !defined(__WXMAC__)
7357 wxClientDC
dc( m_gridWin
);
7361 if ( m_currentCellCoords
!= wxGridNoCellCoords
)
7363 DisableCellEditControl();
7365 if ( IsVisible( m_currentCellCoords
, false ) )
7368 r
= BlockToDeviceRect( m_currentCellCoords
, m_currentCellCoords
);
7369 if ( !m_gridLinesEnabled
)
7377 wxGridCellCoordsArray cells
= CalcCellsExposed( r
);
7379 // Otherwise refresh redraws the highlight!
7380 m_currentCellCoords
= coords
;
7382 #if defined(__WXMAC__)
7383 m_gridWin
->Refresh(true /*, & r */);
7385 DrawGridCellArea( dc
, cells
);
7386 DrawAllGridLines( dc
, r
);
7391 m_currentCellCoords
= coords
;
7393 wxGridCellAttr
*attr
= GetCellAttr( coords
);
7394 #if !defined(__WXMAC__)
7395 DrawCellHighlight( dc
, attr
);
7403 wxGrid::UpdateBlockBeingSelected(int topRow
, int leftCol
,
7404 int bottomRow
, int rightCol
)
7408 switch ( m_selection
->GetSelectionMode() )
7411 wxFAIL_MSG( "unknown selection mode" );
7414 case wxGridSelectCells
:
7415 // arbitrary blocks selection allowed so just use the cell
7416 // coordinates as is
7419 case wxGridSelectRows
:
7420 // only full rows selection allowd, ensure that we do select
7423 rightCol
= GetNumberCols() - 1;
7426 case wxGridSelectColumns
:
7427 // same as above but for columns
7429 bottomRow
= GetNumberRows() - 1;
7432 case wxGridSelectRowsOrColumns
:
7433 // in this mode we can select only full rows or full columns so
7434 // it doesn't make sense to select blocks at all (and we can't
7435 // extend the block because there is no preferred direction, we
7436 // could only extend it to cover the entire grid but this is
7442 m_selectedBlockCorner
= wxGridCellCoords(bottomRow
, rightCol
);
7443 MakeCellVisible(m_selectedBlockCorner
);
7445 EnsureFirstLessThanSecond(topRow
, bottomRow
);
7446 EnsureFirstLessThanSecond(leftCol
, rightCol
);
7448 wxGridCellCoords updateTopLeft
= wxGridCellCoords(topRow
, leftCol
),
7449 updateBottomRight
= wxGridCellCoords(bottomRow
, rightCol
);
7451 // First the case that we selected a completely new area
7452 if ( m_selectedBlockTopLeft
== wxGridNoCellCoords
||
7453 m_selectedBlockBottomRight
== wxGridNoCellCoords
)
7456 rect
= BlockToDeviceRect( wxGridCellCoords ( topRow
, leftCol
),
7457 wxGridCellCoords ( bottomRow
, rightCol
) );
7458 m_gridWin
->Refresh( false, &rect
);
7461 // Now handle changing an existing selection area.
7462 else if ( m_selectedBlockTopLeft
!= updateTopLeft
||
7463 m_selectedBlockBottomRight
!= updateBottomRight
)
7465 // Compute two optimal update rectangles:
7466 // Either one rectangle is a real subset of the
7467 // other, or they are (almost) disjoint!
7469 bool need_refresh
[4];
7473 need_refresh
[3] = false;
7476 // Store intermediate values
7477 wxCoord oldLeft
= m_selectedBlockTopLeft
.GetCol();
7478 wxCoord oldTop
= m_selectedBlockTopLeft
.GetRow();
7479 wxCoord oldRight
= m_selectedBlockBottomRight
.GetCol();
7480 wxCoord oldBottom
= m_selectedBlockBottomRight
.GetRow();
7482 // Determine the outer/inner coordinates.
7483 EnsureFirstLessThanSecond(oldLeft
, leftCol
);
7484 EnsureFirstLessThanSecond(oldTop
, topRow
);
7485 EnsureFirstLessThanSecond(rightCol
, oldRight
);
7486 EnsureFirstLessThanSecond(bottomRow
, oldBottom
);
7488 // Now, either the stuff marked old is the outer
7489 // rectangle or we don't have a situation where one
7490 // is contained in the other.
7492 if ( oldLeft
< leftCol
)
7494 // Refresh the newly selected or deselected
7495 // area to the left of the old or new selection.
7496 need_refresh
[0] = true;
7497 rect
[0] = BlockToDeviceRect(
7498 wxGridCellCoords( oldTop
, oldLeft
),
7499 wxGridCellCoords( oldBottom
, leftCol
- 1 ) );
7502 if ( oldTop
< topRow
)
7504 // Refresh the newly selected or deselected
7505 // area above the old or new selection.
7506 need_refresh
[1] = true;
7507 rect
[1] = BlockToDeviceRect(
7508 wxGridCellCoords( oldTop
, leftCol
),
7509 wxGridCellCoords( topRow
- 1, rightCol
) );
7512 if ( oldRight
> rightCol
)
7514 // Refresh the newly selected or deselected
7515 // area to the right of the old or new selection.
7516 need_refresh
[2] = true;
7517 rect
[2] = BlockToDeviceRect(
7518 wxGridCellCoords( oldTop
, rightCol
+ 1 ),
7519 wxGridCellCoords( oldBottom
, oldRight
) );
7522 if ( oldBottom
> bottomRow
)
7524 // Refresh the newly selected or deselected
7525 // area below the old or new selection.
7526 need_refresh
[3] = true;
7527 rect
[3] = BlockToDeviceRect(
7528 wxGridCellCoords( bottomRow
+ 1, leftCol
),
7529 wxGridCellCoords( oldBottom
, rightCol
) );
7532 // various Refresh() calls
7533 for (i
= 0; i
< 4; i
++ )
7534 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
7535 m_gridWin
->Refresh( false, &(rect
[i
]) );
7539 m_selectedBlockTopLeft
= updateTopLeft
;
7540 m_selectedBlockBottomRight
= updateBottomRight
;
7544 // ------ functions to get/send data (see also public functions)
7547 bool wxGrid::GetModelValues()
7549 // Hide the editor, so it won't hide a changed value.
7550 HideCellEditControl();
7554 // all we need to do is repaint the grid
7556 m_gridWin
->Refresh();
7563 bool wxGrid::SetModelValues()
7567 // Disable the editor, so it won't hide a changed value.
7568 // Do we also want to save the current value of the editor first?
7570 DisableCellEditControl();
7574 for ( row
= 0; row
< m_numRows
; row
++ )
7576 for ( col
= 0; col
< m_numCols
; col
++ )
7578 m_table
->SetValue( row
, col
, GetCellValue(row
, col
) );
7588 // Note - this function only draws cells that are in the list of
7589 // exposed cells (usually set from the update region by
7590 // CalcExposedCells)
7592 void wxGrid::DrawGridCellArea( wxDC
& dc
, const wxGridCellCoordsArray
& cells
)
7594 if ( !m_numRows
|| !m_numCols
)
7597 int i
, numCells
= cells
.GetCount();
7598 int row
, col
, cell_rows
, cell_cols
;
7599 wxGridCellCoordsArray redrawCells
;
7601 for ( i
= numCells
- 1; i
>= 0; i
-- )
7603 row
= cells
[i
].GetRow();
7604 col
= cells
[i
].GetCol();
7605 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7607 // If this cell is part of a multicell block, find owner for repaint
7608 if ( cell_rows
<= 0 || cell_cols
<= 0 )
7610 wxGridCellCoords
cell( row
+ cell_rows
, col
+ cell_cols
);
7611 bool marked
= false;
7612 for ( int j
= 0; j
< numCells
; j
++ )
7614 if ( cell
== cells
[j
] )
7623 int count
= redrawCells
.GetCount();
7624 for (int j
= 0; j
< count
; j
++)
7626 if ( cell
== redrawCells
[j
] )
7634 redrawCells
.Add( cell
);
7637 // don't bother drawing this cell
7641 // If this cell is empty, find cell to left that might want to overflow
7642 if (m_table
&& m_table
->IsEmptyCell(row
, col
))
7644 for ( int l
= 0; l
< cell_rows
; l
++ )
7646 // find a cell in this row to leave already marked for repaint
7648 for (int k
= 0; k
< int(redrawCells
.GetCount()); k
++)
7649 if ((redrawCells
[k
].GetCol() < left
) &&
7650 (redrawCells
[k
].GetRow() == row
))
7652 left
= redrawCells
[k
].GetCol();
7656 left
= 0; // oh well
7658 for (int j
= col
- 1; j
>= left
; j
--)
7660 if (!m_table
->IsEmptyCell(row
+ l
, j
))
7662 if (GetCellOverflow(row
+ l
, j
))
7664 wxGridCellCoords
cell(row
+ l
, j
);
7665 bool marked
= false;
7667 for (int k
= 0; k
< numCells
; k
++)
7669 if ( cell
== cells
[k
] )
7678 int count
= redrawCells
.GetCount();
7679 for (int k
= 0; k
< count
; k
++)
7681 if ( cell
== redrawCells
[k
] )
7688 redrawCells
.Add( cell
);
7697 DrawCell( dc
, cells
[i
] );
7700 numCells
= redrawCells
.GetCount();
7702 for ( i
= numCells
- 1; i
>= 0; i
-- )
7704 DrawCell( dc
, redrawCells
[i
] );
7708 void wxGrid::DrawGridSpace( wxDC
& dc
)
7711 m_gridWin
->GetClientSize( &cw
, &ch
);
7714 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
7716 int rightCol
= m_numCols
> 0 ? GetColRight(GetColAt( m_numCols
- 1 )) : 0;
7717 int bottomRow
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) : 0;
7719 if ( right
> rightCol
|| bottom
> bottomRow
)
7722 CalcUnscrolledPosition( 0, 0, &left
, &top
);
7724 dc
.SetBrush(GetDefaultCellBackgroundColour());
7725 dc
.SetPen( *wxTRANSPARENT_PEN
);
7727 if ( right
> rightCol
)
7729 dc
.DrawRectangle( rightCol
, top
, right
- rightCol
, ch
);
7732 if ( bottom
> bottomRow
)
7734 dc
.DrawRectangle( left
, bottomRow
, cw
, bottom
- bottomRow
);
7739 void wxGrid::DrawCell( wxDC
& dc
, const wxGridCellCoords
& coords
)
7741 int row
= coords
.GetRow();
7742 int col
= coords
.GetCol();
7744 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7747 // we draw the cell border ourselves
7748 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7750 bool isCurrent
= coords
== m_currentCellCoords
;
7752 wxRect rect
= CellToRect( row
, col
);
7754 // if the editor is shown, we should use it and not the renderer
7755 // Note: However, only if it is really _shown_, i.e. not hidden!
7756 if ( isCurrent
&& IsCellEditControlShown() )
7758 // NB: this "#if..." is temporary and fixes a problem where the
7759 // edit control is erased by this code after being rendered.
7760 // On wxMac (QD build only), the cell editor is a wxTextCntl and is rendered
7761 // implicitly, causing this out-of order render.
7762 #if !defined(__WXMAC__)
7763 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
7764 editor
->PaintBackground(rect
, attr
);
7770 // but all the rest is drawn by the cell renderer and hence may be customized
7771 wxGridCellRenderer
*renderer
= attr
->GetRenderer(this, row
, col
);
7772 renderer
->Draw(*this, *attr
, dc
, rect
, row
, col
, IsInSelection(coords
));
7779 void wxGrid::DrawCellHighlight( wxDC
& dc
, const wxGridCellAttr
*attr
)
7781 // don't show highlight when the grid doesn't have focus
7785 int row
= m_currentCellCoords
.GetRow();
7786 int col
= m_currentCellCoords
.GetCol();
7788 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7791 wxRect rect
= CellToRect(row
, col
);
7793 // hmmm... what could we do here to show that the cell is disabled?
7794 // for now, I just draw a thinner border than for the other ones, but
7795 // it doesn't look really good
7797 int penWidth
= attr
->IsReadOnly() ? m_cellHighlightROPenWidth
: m_cellHighlightPenWidth
;
7801 // The center of the drawn line is where the position/width/height of
7802 // the rectangle is actually at (on wxMSW at least), so the
7803 // size of the rectangle is reduced to compensate for the thickness of
7804 // the line. If this is too strange on non-wxMSW platforms then
7805 // please #ifdef this appropriately.
7806 rect
.x
+= penWidth
/ 2;
7807 rect
.y
+= penWidth
/ 2;
7808 rect
.width
-= penWidth
- 1;
7809 rect
.height
-= penWidth
- 1;
7811 // Now draw the rectangle
7812 // use the cellHighlightColour if the cell is inside a selection, this
7813 // will ensure the cell is always visible.
7814 dc
.SetPen(wxPen(IsInSelection(row
,col
) ? m_selectionForeground
7815 : m_cellHighlightColour
,
7817 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
7818 dc
.DrawRectangle(rect
);
7822 wxPen
wxGrid::GetDefaultGridLinePen()
7824 return wxPen(GetGridLineColour());
7827 wxPen
wxGrid::GetRowGridLinePen(int WXUNUSED(row
))
7829 return GetDefaultGridLinePen();
7832 wxPen
wxGrid::GetColGridLinePen(int WXUNUSED(col
))
7834 return GetDefaultGridLinePen();
7837 void wxGrid::DrawCellBorder( wxDC
& dc
, const wxGridCellCoords
& coords
)
7839 int row
= coords
.GetRow();
7840 int col
= coords
.GetCol();
7841 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7845 wxRect rect
= CellToRect( row
, col
);
7847 // right hand border
7848 dc
.SetPen( GetColGridLinePen(col
) );
7849 dc
.DrawLine( rect
.x
+ rect
.width
, rect
.y
,
7850 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
+ 1 );
7853 dc
.SetPen( GetRowGridLinePen(row
) );
7854 dc
.DrawLine( rect
.x
, rect
.y
+ rect
.height
,
7855 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
);
7858 void wxGrid::DrawHighlight(wxDC
& dc
, const wxGridCellCoordsArray
& cells
)
7860 // This if block was previously in wxGrid::OnPaint but that doesn't
7861 // seem to get called under wxGTK - MB
7863 if ( m_currentCellCoords
== wxGridNoCellCoords
&&
7864 m_numRows
&& m_numCols
)
7866 m_currentCellCoords
.Set(0, 0);
7869 if ( IsCellEditControlShown() )
7871 // don't show highlight when the edit control is shown
7875 // if the active cell was repainted, repaint its highlight too because it
7876 // might have been damaged by the grid lines
7877 size_t count
= cells
.GetCount();
7878 for ( size_t n
= 0; n
< count
; n
++ )
7880 wxGridCellCoords cell
= cells
[n
];
7882 // If we are using attributes, then we may have just exposed another
7883 // cell in a partially-visible merged cluster of cells. If the "anchor"
7884 // (upper left) cell of this merged cluster is the cell indicated by
7885 // m_currentCellCoords, then we need to refresh the cell highlight even
7886 // though the "anchor" itself is not part of our update segment.
7887 if ( CanHaveAttributes() )
7891 GetCellSize(cell
.GetRow(), cell
.GetCol(), &rows
, &cols
);
7894 cell
.SetRow(cell
.GetRow() + rows
);
7897 cell
.SetCol(cell
.GetCol() + cols
);
7900 if ( cell
== m_currentCellCoords
)
7902 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
7903 DrawCellHighlight(dc
, attr
);
7911 // This is used to redraw all grid lines e.g. when the grid line colour
7914 void wxGrid::DrawAllGridLines( wxDC
& dc
, const wxRegion
& WXUNUSED(reg
) )
7916 if ( !m_gridLinesEnabled
)
7919 int top
, bottom
, left
, right
;
7922 m_gridWin
->GetClientSize(&cw
, &ch
);
7923 CalcUnscrolledPosition( 0, 0, &left
, &top
);
7924 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
7926 // avoid drawing grid lines past the last row and col
7927 if ( m_gridLinesClipHorz
)
7932 const int lastColRight
= GetColRight(GetColAt(m_numCols
- 1));
7933 if ( right
> lastColRight
)
7934 right
= lastColRight
;
7937 if ( m_gridLinesClipVert
)
7942 const int lastRowBottom
= GetRowBottom(m_numRows
- 1);
7943 if ( bottom
> lastRowBottom
)
7944 bottom
= lastRowBottom
;
7947 // no gridlines inside multicells, clip them out
7948 int leftCol
= GetColPos( internalXToCol(left
) );
7949 int topRow
= internalYToRow(top
);
7950 int rightCol
= GetColPos( internalXToCol(right
) );
7951 int bottomRow
= internalYToRow(bottom
);
7953 wxRegion
clippedcells(0, 0, cw
, ch
);
7955 int cell_rows
, cell_cols
;
7958 for ( int j
= topRow
; j
<= bottomRow
; j
++ )
7960 for ( int colPos
= leftCol
; colPos
<= rightCol
; colPos
++ )
7962 int i
= GetColAt( colPos
);
7964 GetCellSize( j
, i
, &cell_rows
, &cell_cols
);
7965 if ((cell_rows
> 1) || (cell_cols
> 1))
7967 rect
= CellToRect(j
,i
);
7968 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7969 clippedcells
.Subtract(rect
);
7971 else if ((cell_rows
< 0) || (cell_cols
< 0))
7973 rect
= CellToRect(j
+ cell_rows
, i
+ cell_cols
);
7974 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7975 clippedcells
.Subtract(rect
);
7980 dc
.SetDeviceClippingRegion( clippedcells
);
7983 // horizontal grid lines
7984 for ( int i
= internalYToRow(top
); i
< m_numRows
; i
++ )
7986 int bot
= GetRowBottom(i
) - 1;
7993 dc
.SetPen( GetRowGridLinePen(i
) );
7994 dc
.DrawLine( left
, bot
, right
, bot
);
7998 // vertical grid lines
7999 for ( int colPos
= leftCol
; colPos
< m_numCols
; colPos
++ )
8001 int i
= GetColAt( colPos
);
8003 int colRight
= GetColRight(i
);
8005 if (GetLayoutDirection() != wxLayout_RightToLeft
)
8009 if ( colRight
> right
)
8012 if ( colRight
>= left
)
8014 dc
.SetPen( GetColGridLinePen(i
) );
8015 dc
.DrawLine( colRight
, top
, colRight
, bottom
);
8019 dc
.DestroyClippingRegion();
8022 void wxGrid::DrawRowLabels( wxDC
& dc
, const wxArrayInt
& rows
)
8027 const size_t numLabels
= rows
.GetCount();
8028 for ( size_t i
= 0; i
< numLabels
; i
++ )
8030 DrawRowLabel( dc
, rows
[i
] );
8034 void wxGrid::DrawRowLabel( wxDC
& dc
, int row
)
8036 if ( GetRowHeight(row
) <= 0 || m_rowLabelWidth
<= 0 )
8041 int rowTop
= GetRowTop(row
),
8042 rowBottom
= GetRowBottom(row
) - 1;
8044 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW
)));
8045 dc
.DrawLine( m_rowLabelWidth
- 1, rowTop
, m_rowLabelWidth
- 1, rowBottom
);
8046 dc
.DrawLine( 0, rowTop
, 0, rowBottom
);
8047 dc
.DrawLine( 0, rowBottom
, m_rowLabelWidth
, rowBottom
);
8049 dc
.SetPen( *wxWHITE_PEN
);
8050 dc
.DrawLine( 1, rowTop
, 1, rowBottom
);
8051 dc
.DrawLine( 1, rowTop
, m_rowLabelWidth
- 1, rowTop
);
8053 dc
.SetBackgroundMode( wxBRUSHSTYLE_TRANSPARENT
);
8054 dc
.SetTextForeground( GetLabelTextColour() );
8055 dc
.SetFont( GetLabelFont() );
8058 GetRowLabelAlignment( &hAlign
, &vAlign
);
8061 rect
.SetY( GetRowTop(row
) + 2 );
8062 rect
.SetWidth( m_rowLabelWidth
- 4 );
8063 rect
.SetHeight( GetRowHeight(row
) - 4 );
8064 DrawTextRectangle( dc
, GetRowLabelValue( row
), rect
, hAlign
, vAlign
);
8067 void wxGrid::SetUseNativeColLabels( bool native
)
8069 m_nativeColumnLabels
= native
;
8072 int height
= wxRendererNative::Get().GetHeaderButtonHeight( this );
8073 SetColLabelSize( height
);
8076 m_colLabelWin
->Refresh();
8077 m_cornerLabelWin
->Refresh();
8080 void wxGrid::DrawColLabels( wxDC
& dc
,const wxArrayInt
& cols
)
8085 const size_t numLabels
= cols
.GetCount();
8086 for ( size_t i
= 0; i
< numLabels
; i
++ )
8088 DrawColLabel( dc
, cols
[i
] );
8092 void wxGrid::DrawCornerLabel(wxDC
& dc
)
8094 if ( m_nativeColumnLabels
)
8096 wxRect
rect(wxSize(m_rowLabelWidth
, m_colLabelHeight
));
8099 wxRendererNative::Get().DrawHeaderButton(m_cornerLabelWin
, dc
, rect
, 0);
8103 dc
.SetPen(wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW
)));
8104 dc
.DrawLine( m_rowLabelWidth
- 1, m_colLabelHeight
- 1,
8105 m_rowLabelWidth
- 1, 0 );
8106 dc
.DrawLine( m_rowLabelWidth
- 1, m_colLabelHeight
- 1,
8107 0, m_colLabelHeight
- 1 );
8108 dc
.DrawLine( 0, 0, m_rowLabelWidth
, 0 );
8109 dc
.DrawLine( 0, 0, 0, m_colLabelHeight
);
8111 dc
.SetPen( *wxWHITE_PEN
);
8112 dc
.DrawLine( 1, 1, m_rowLabelWidth
- 1, 1 );
8113 dc
.DrawLine( 1, 1, 1, m_colLabelHeight
- 1 );
8117 void wxGrid::DrawColLabel(wxDC
& dc
, int col
)
8119 if ( GetColWidth(col
) <= 0 || m_colLabelHeight
<= 0 )
8122 int colLeft
= GetColLeft(col
);
8124 wxRect
rect(colLeft
, 0, GetColWidth(col
), m_colLabelHeight
);
8126 if ( m_nativeColumnLabels
)
8128 wxRendererNative::Get().DrawHeaderButton(m_colLabelWin
, dc
, rect
, 0);
8132 int colRight
= GetColRight(col
) - 1;
8134 dc
.SetPen(wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW
)));
8135 dc
.DrawLine( colRight
, 0,
8136 colRight
, m_colLabelHeight
- 1 );
8137 dc
.DrawLine( colLeft
, 0,
8139 dc
.DrawLine( colLeft
, m_colLabelHeight
- 1,
8140 colRight
+ 1, m_colLabelHeight
- 1 );
8142 dc
.SetPen( *wxWHITE_PEN
);
8143 dc
.DrawLine( colLeft
, 1, colLeft
, m_colLabelHeight
- 1 );
8144 dc
.DrawLine( colLeft
, 1, colRight
, 1 );
8147 dc
.SetBackgroundMode( wxBRUSHSTYLE_TRANSPARENT
);
8148 dc
.SetTextForeground( GetLabelTextColour() );
8149 dc
.SetFont( GetLabelFont() );
8152 GetColLabelAlignment( &hAlign
, &vAlign
);
8153 const int orient
= GetColLabelTextOrientation();
8156 DrawTextRectangle(dc
, GetColLabelValue(col
), rect
, hAlign
, vAlign
, orient
);
8159 // TODO: these 2 functions should be replaced with wxDC::DrawLabel() to which
8160 // we just have to add textOrientation support
8161 void wxGrid::DrawTextRectangle( wxDC
& dc
,
8162 const wxString
& value
,
8166 int textOrientation
)
8168 wxArrayString lines
;
8170 StringToLines( value
, lines
);
8172 DrawTextRectangle(dc
, lines
, rect
, horizAlign
, vertAlign
, textOrientation
);
8175 void wxGrid::DrawTextRectangle(wxDC
& dc
,
8176 const wxArrayString
& lines
,
8180 int textOrientation
)
8182 if ( lines
.empty() )
8185 wxDCClipper
clip(dc
, rect
);
8190 if ( textOrientation
== wxHORIZONTAL
)
8191 GetTextBoxSize( dc
, lines
, &textWidth
, &textHeight
);
8193 GetTextBoxSize( dc
, lines
, &textHeight
, &textWidth
);
8197 switch ( vertAlign
)
8199 case wxALIGN_BOTTOM
:
8200 if ( textOrientation
== wxHORIZONTAL
)
8201 y
= rect
.y
+ (rect
.height
- textHeight
- 1);
8203 x
= rect
.x
+ rect
.width
- textWidth
;
8206 case wxALIGN_CENTRE
:
8207 if ( textOrientation
== wxHORIZONTAL
)
8208 y
= rect
.y
+ ((rect
.height
- textHeight
) / 2);
8210 x
= rect
.x
+ ((rect
.width
- textWidth
) / 2);
8215 if ( textOrientation
== wxHORIZONTAL
)
8222 // Align each line of a multi-line label
8223 size_t nLines
= lines
.GetCount();
8224 for ( size_t l
= 0; l
< nLines
; l
++ )
8226 const wxString
& line
= lines
[l
];
8230 *(textOrientation
== wxHORIZONTAL
? &y
: &x
) += dc
.GetCharHeight();
8234 wxCoord lineWidth
= 0,
8236 dc
.GetTextExtent(line
, &lineWidth
, &lineHeight
);
8238 switch ( horizAlign
)
8241 if ( textOrientation
== wxHORIZONTAL
)
8242 x
= rect
.x
+ (rect
.width
- lineWidth
- 1);
8244 y
= rect
.y
+ lineWidth
+ 1;
8247 case wxALIGN_CENTRE
:
8248 if ( textOrientation
== wxHORIZONTAL
)
8249 x
= rect
.x
+ ((rect
.width
- lineWidth
) / 2);
8251 y
= rect
.y
+ rect
.height
- ((rect
.height
- lineWidth
) / 2);
8256 if ( textOrientation
== wxHORIZONTAL
)
8259 y
= rect
.y
+ rect
.height
- 1;
8263 if ( textOrientation
== wxHORIZONTAL
)
8265 dc
.DrawText( line
, x
, y
);
8270 dc
.DrawRotatedText( line
, x
, y
, 90.0 );
8276 // Split multi-line text up into an array of strings.
8277 // Any existing contents of the string array are preserved.
8279 // TODO: refactor wxTextFile::Read() and reuse the same code from here
8280 void wxGrid::StringToLines( const wxString
& value
, wxArrayString
& lines
) const
8284 wxString eol
= wxTextFile::GetEOL( wxTextFileType_Unix
);
8285 wxString tVal
= wxTextFile::Translate( value
, wxTextFileType_Unix
);
8287 while ( startPos
< (int)tVal
.length() )
8289 pos
= tVal
.Mid(startPos
).Find( eol
);
8294 else if ( pos
== 0 )
8296 lines
.Add( wxEmptyString
);
8300 lines
.Add( tVal
.Mid(startPos
, pos
) );
8303 startPos
+= pos
+ 1;
8306 if ( startPos
< (int)tVal
.length() )
8308 lines
.Add( tVal
.Mid( startPos
) );
8312 void wxGrid::GetTextBoxSize( const wxDC
& dc
,
8313 const wxArrayString
& lines
,
8314 long *width
, long *height
) const
8318 wxCoord lineW
= 0, lineH
= 0;
8321 for ( i
= 0; i
< lines
.GetCount(); i
++ )
8323 dc
.GetTextExtent( lines
[i
], &lineW
, &lineH
);
8324 w
= wxMax( w
, lineW
);
8333 // ------ Batch processing.
8335 void wxGrid::EndBatch()
8337 if ( m_batchCount
> 0 )
8340 if ( !m_batchCount
)
8343 m_rowLabelWin
->Refresh();
8344 m_colLabelWin
->Refresh();
8345 m_cornerLabelWin
->Refresh();
8346 m_gridWin
->Refresh();
8351 // Use this, rather than wxWindow::Refresh(), to force an immediate
8352 // repainting of the grid. Has no effect if you are already inside a
8353 // BeginBatch / EndBatch block.
8355 void wxGrid::ForceRefresh()
8361 bool wxGrid::Enable(bool enable
)
8363 if ( !wxScrolledWindow::Enable(enable
) )
8366 // redraw in the new state
8367 m_gridWin
->Refresh();
8373 // ------ Edit control functions
8376 void wxGrid::EnableEditing( bool edit
)
8378 if ( edit
!= m_editable
)
8381 EnableCellEditControl(edit
);
8386 void wxGrid::EnableCellEditControl( bool enable
)
8391 if ( enable
!= m_cellEditCtrlEnabled
)
8395 if ( SendEvent(wxEVT_GRID_EDITOR_SHOWN
) == -1 )
8398 // this should be checked by the caller!
8399 wxASSERT_MSG( CanEnableCellControl(), _T("can't enable editing for this cell!") );
8401 // do it before ShowCellEditControl()
8402 m_cellEditCtrlEnabled
= enable
;
8404 ShowCellEditControl();
8408 //FIXME:add veto support
8409 SendEvent(wxEVT_GRID_EDITOR_HIDDEN
);
8411 HideCellEditControl();
8412 SaveEditControlValue();
8414 // do it after HideCellEditControl()
8415 m_cellEditCtrlEnabled
= enable
;
8420 bool wxGrid::IsCurrentCellReadOnly() const
8423 wxGridCellAttr
* attr
= ((wxGrid
*)this)->GetCellAttr(m_currentCellCoords
);
8424 bool readonly
= attr
->IsReadOnly();
8430 bool wxGrid::CanEnableCellControl() const
8432 return m_editable
&& (m_currentCellCoords
!= wxGridNoCellCoords
) &&
8433 !IsCurrentCellReadOnly();
8436 bool wxGrid::IsCellEditControlEnabled() const
8438 // the cell edit control might be disable for all cells or just for the
8439 // current one if it's read only
8440 return m_cellEditCtrlEnabled
? !IsCurrentCellReadOnly() : false;
8443 bool wxGrid::IsCellEditControlShown() const
8445 bool isShown
= false;
8447 if ( m_cellEditCtrlEnabled
)
8449 int row
= m_currentCellCoords
.GetRow();
8450 int col
= m_currentCellCoords
.GetCol();
8451 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8452 wxGridCellEditor
* editor
= attr
->GetEditor((wxGrid
*) this, row
, col
);
8457 if ( editor
->IsCreated() )
8459 isShown
= editor
->GetControl()->IsShown();
8469 void wxGrid::ShowCellEditControl()
8471 if ( IsCellEditControlEnabled() )
8473 if ( !IsVisible( m_currentCellCoords
, false ) )
8475 m_cellEditCtrlEnabled
= false;
8480 wxRect rect
= CellToRect( m_currentCellCoords
);
8481 int row
= m_currentCellCoords
.GetRow();
8482 int col
= m_currentCellCoords
.GetCol();
8484 // if this is part of a multicell, find owner (topleft)
8485 int cell_rows
, cell_cols
;
8486 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
8487 if ( cell_rows
<= 0 || cell_cols
<= 0 )
8491 m_currentCellCoords
.SetRow( row
);
8492 m_currentCellCoords
.SetCol( col
);
8495 // erase the highlight and the cell contents because the editor
8496 // might not cover the entire cell
8497 wxClientDC
dc( m_gridWin
);
8499 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8500 dc
.SetBrush(wxBrush(attr
->GetBackgroundColour()));
8501 dc
.SetPen(*wxTRANSPARENT_PEN
);
8502 dc
.DrawRectangle(rect
);
8504 // convert to scrolled coords
8505 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
8511 // cell is shifted by one pixel
8512 // However, don't allow x or y to become negative
8513 // since the SetSize() method interprets that as
8520 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
8521 if ( !editor
->IsCreated() )
8523 editor
->Create(m_gridWin
, wxID_ANY
,
8524 new wxGridCellEditorEvtHandler(this, editor
));
8526 wxGridEditorCreatedEvent
evt(GetId(),
8527 wxEVT_GRID_EDITOR_CREATED
,
8531 editor
->GetControl());
8532 GetEventHandler()->ProcessEvent(evt
);
8535 // resize editor to overflow into righthand cells if allowed
8536 int maxWidth
= rect
.width
;
8537 wxString value
= GetCellValue(row
, col
);
8538 if ( (value
!= wxEmptyString
) && (attr
->GetOverflow()) )
8541 GetTextExtent(value
, &maxWidth
, &y
, NULL
, NULL
, &attr
->GetFont());
8542 if (maxWidth
< rect
.width
)
8543 maxWidth
= rect
.width
;
8546 int client_right
= m_gridWin
->GetClientSize().GetWidth();
8547 if (rect
.x
+ maxWidth
> client_right
)
8548 maxWidth
= client_right
- rect
.x
;
8550 if ((maxWidth
> rect
.width
) && (col
< m_numCols
) && m_table
)
8552 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
8553 // may have changed earlier
8554 for (int i
= col
+ cell_cols
; i
< m_numCols
; i
++)
8557 GetCellSize( row
, i
, &c_rows
, &c_cols
);
8559 // looks weird going over a multicell
8560 if (m_table
->IsEmptyCell( row
, i
) &&
8561 (rect
.width
< maxWidth
) && (c_rows
== 1))
8563 rect
.width
+= GetColWidth( i
);
8569 if (rect
.GetRight() > client_right
)
8570 rect
.SetRight( client_right
- 1 );
8573 editor
->SetCellAttr( attr
);
8574 editor
->SetSize( rect
);
8576 editor
->GetControl()->Move(
8577 editor
->GetControl()->GetPosition().x
+ nXMove
,
8578 editor
->GetControl()->GetPosition().y
);
8579 editor
->Show( true, attr
);
8581 // recalc dimensions in case we need to
8582 // expand the scrolled window to account for editor
8585 editor
->BeginEdit(row
, col
, this);
8586 editor
->SetCellAttr(NULL
);
8594 void wxGrid::HideCellEditControl()
8596 if ( IsCellEditControlEnabled() )
8598 int row
= m_currentCellCoords
.GetRow();
8599 int col
= m_currentCellCoords
.GetCol();
8601 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8602 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
8603 const bool editorHadFocus
= editor
->GetControl()->HasFocus();
8604 editor
->Show( false );
8608 // return the focus to the grid itself if the editor had it
8610 // note that we must not do this unconditionally to avoid stealing
8611 // focus from the window which just received it if we are hiding the
8612 // editor precisely because we lost focus
8613 if ( editorHadFocus
)
8614 m_gridWin
->SetFocus();
8616 // refresh whole row to the right
8617 wxRect
rect( CellToRect(row
, col
) );
8618 CalcScrolledPosition(rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
8619 rect
.width
= m_gridWin
->GetClientSize().GetWidth() - rect
.x
;
8622 // ensure that the pixels under the focus ring get refreshed as well
8623 rect
.Inflate(10, 10);
8626 m_gridWin
->Refresh( false, &rect
);
8630 void wxGrid::SaveEditControlValue()
8632 if ( IsCellEditControlEnabled() )
8634 int row
= m_currentCellCoords
.GetRow();
8635 int col
= m_currentCellCoords
.GetCol();
8637 wxString oldval
= GetCellValue(row
, col
);
8639 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8640 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
8641 bool changed
= editor
->EndEdit(row
, col
, this);
8648 if ( SendEvent(wxEVT_GRID_CELL_CHANGE
) == -1 )
8650 // Event has been vetoed, set the data back.
8651 SetCellValue(row
, col
, oldval
);
8658 // ------ Grid location functions
8659 // Note that all of these functions work with the logical coordinates of
8660 // grid cells and labels so you will need to convert from device
8661 // coordinates for mouse events etc.
8664 wxGridCellCoords
wxGrid::XYToCell(int x
, int y
) const
8666 int row
= YToRow(y
);
8667 int col
= XToCol(x
);
8669 return row
== -1 || col
== -1 ? wxGridNoCellCoords
8670 : wxGridCellCoords(row
, col
);
8673 // compute row or column from some (unscrolled) coordinate value, using either
8674 // m_defaultRowHeight/m_defaultColWidth or binary search on array of
8675 // m_rowBottoms/m_colRights to do it quickly (linear search shouldn't be used
8678 wxGrid::PosToLine(int coord
,
8680 const wxGridOperations
& oper
) const
8682 const int numLines
= oper
.GetNumberOfLines(this);
8685 return clipToMinMax
&& numLines
> 0 ? oper
.GetLineAt(this, 0) : -1;
8687 const int defaultLineSize
= oper
.GetDefaultLineSize(this);
8688 wxCHECK_MSG( defaultLineSize
, -1, "can't have 0 default line size" );
8690 int maxPos
= coord
/ defaultLineSize
,
8693 // check for the simplest case: if we have no explicit line sizes
8694 // configured, then we already know the line this position falls in
8695 const wxArrayInt
& lineEnds
= oper
.GetLineEnds(this);
8696 if ( lineEnds
.empty() )
8698 if ( maxPos
< numLines
)
8701 return clipToMinMax
? numLines
- 1 : -1;
8705 // adjust maxPos before starting the binary search
8706 if ( maxPos
>= numLines
)
8708 maxPos
= numLines
- 1;
8712 if ( coord
>= lineEnds
[oper
.GetLineAt(this, maxPos
)])
8715 const int minDist
= oper
.GetMinimalAcceptableLineSize(this);
8717 maxPos
= coord
/ minDist
;
8719 maxPos
= numLines
- 1;
8722 if ( maxPos
>= numLines
)
8723 maxPos
= numLines
- 1;
8726 // check if the position is beyond the last column
8727 const int lineAtMaxPos
= oper
.GetLineAt(this, maxPos
);
8728 if ( coord
>= lineEnds
[lineAtMaxPos
] )
8729 return clipToMinMax
? lineAtMaxPos
: -1;
8731 // or before the first one
8732 const int lineAt0
= oper
.GetLineAt(this, 0);
8733 if ( coord
< lineEnds
[lineAt0
] )
8737 // finally do perform the binary search
8738 while ( minPos
< maxPos
)
8740 wxCHECK_MSG( lineEnds
[oper
.GetLineAt(this, minPos
)] <= coord
&&
8741 coord
< lineEnds
[oper
.GetLineAt(this, maxPos
)],
8743 "wxGrid: internal error in PosToLine()" );
8745 if ( coord
>= lineEnds
[oper
.GetLineAt(this, maxPos
- 1)] )
8746 return oper
.GetLineAt(this, maxPos
);
8750 const int median
= minPos
+ (maxPos
- minPos
+ 1) / 2;
8751 if ( coord
< lineEnds
[oper
.GetLineAt(this, median
)] )
8757 return oper
.GetLineAt(this, maxPos
);
8760 int wxGrid::YToRow(int y
, bool clipToMinMax
) const
8762 return PosToLine(y
, clipToMinMax
, wxGridRowOperations());
8765 int wxGrid::XToCol(int x
, bool clipToMinMax
) const
8767 return PosToLine(x
, clipToMinMax
, wxGridColumnOperations());
8770 // return the row number that that the y coord is near the edge of, or -1 if
8771 // not near an edge.
8773 // coords can only possibly be near an edge if
8774 // (a) the row/column is large enough to still allow for an "inner" area
8775 // that is _not_ near the edge (i.e., if the height/width is smaller
8776 // than WXGRID_LABEL_EDGE_ZONE, coords are _never_ considered to be
8779 // (b) resizing rows/columns (the thing for which edge detection is
8780 // relevant at all) is enabled.
8782 int wxGrid::PosToEdgeOfLine(int pos
, const wxGridOperations
& oper
) const
8784 if ( !oper
.CanResizeLines(this) )
8787 const int line
= oper
.PosToLine(this, pos
, true);
8789 if ( oper
.GetLineSize(this, line
) > WXGRID_LABEL_EDGE_ZONE
)
8791 // We know that we are in this line, test whether we are close enough
8792 // to start or end border, respectively.
8793 if ( abs(oper
.GetLineEndPos(this, line
) - pos
) < WXGRID_LABEL_EDGE_ZONE
)
8795 else if ( line
> 0 &&
8796 pos
- oper
.GetLineStartPos(this,
8797 line
) < WXGRID_LABEL_EDGE_ZONE
)
8804 int wxGrid::YToEdgeOfRow(int y
) const
8806 return PosToEdgeOfLine(y
, wxGridRowOperations());
8809 int wxGrid::XToEdgeOfCol(int x
) const
8811 return PosToEdgeOfLine(x
, wxGridColumnOperations());
8814 wxRect
wxGrid::CellToRect( int row
, int col
) const
8816 wxRect
rect( -1, -1, -1, -1 );
8818 if ( row
>= 0 && row
< m_numRows
&&
8819 col
>= 0 && col
< m_numCols
)
8821 int i
, cell_rows
, cell_cols
;
8822 rect
.width
= rect
.height
= 0;
8823 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
8824 // if negative then find multicell owner
8829 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
8831 rect
.x
= GetColLeft(col
);
8832 rect
.y
= GetRowTop(row
);
8833 for (i
=col
; i
< col
+ cell_cols
; i
++)
8834 rect
.width
+= GetColWidth(i
);
8835 for (i
=row
; i
< row
+ cell_rows
; i
++)
8836 rect
.height
+= GetRowHeight(i
);
8839 // if grid lines are enabled, then the area of the cell is a bit smaller
8840 if (m_gridLinesEnabled
)
8849 bool wxGrid::IsVisible( int row
, int col
, bool wholeCellVisible
) const
8851 // get the cell rectangle in logical coords
8853 wxRect
r( CellToRect( row
, col
) );
8855 // convert to device coords
8857 int left
, top
, right
, bottom
;
8858 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
8859 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
8861 // check against the client area of the grid window
8863 m_gridWin
->GetClientSize( &cw
, &ch
);
8865 if ( wholeCellVisible
)
8867 // is the cell wholly visible ?
8868 return ( left
>= 0 && right
<= cw
&&
8869 top
>= 0 && bottom
<= ch
);
8873 // is the cell partly visible ?
8875 return ( ((left
>= 0 && left
< cw
) || (right
> 0 && right
<= cw
)) &&
8876 ((top
>= 0 && top
< ch
) || (bottom
> 0 && bottom
<= ch
)) );
8880 // make the specified cell location visible by doing a minimal amount
8883 void wxGrid::MakeCellVisible( int row
, int col
)
8886 int xpos
= -1, ypos
= -1;
8888 if ( row
>= 0 && row
< m_numRows
&&
8889 col
>= 0 && col
< m_numCols
)
8891 // get the cell rectangle in logical coords
8892 wxRect
r( CellToRect( row
, col
) );
8894 // convert to device coords
8895 int left
, top
, right
, bottom
;
8896 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
8897 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
8900 m_gridWin
->GetClientSize( &cw
, &ch
);
8906 else if ( bottom
> ch
)
8908 int h
= r
.GetHeight();
8910 for ( i
= row
- 1; i
>= 0; i
-- )
8912 int rowHeight
= GetRowHeight(i
);
8913 if ( h
+ rowHeight
> ch
)
8920 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
8921 // have rounding errors (this is important, because if we do,
8922 // we might not scroll at all and some cells won't be redrawn)
8924 // Sometimes GRID_SCROLL_LINE / 2 is not enough,
8925 // so just add a full scroll unit...
8926 ypos
+= m_scrollLineY
;
8929 // special handling for wide cells - show always left part of the cell!
8930 // Otherwise, e.g. when stepping from row to row, it would jump between
8931 // left and right part of the cell on every step!
8933 if ( left
< 0 || (right
- left
) >= cw
)
8937 else if ( right
> cw
)
8939 // position the view so that the cell is on the right
8941 CalcUnscrolledPosition(0, 0, &x0
, &y0
);
8942 xpos
= x0
+ (right
- cw
);
8944 // see comment for ypos above
8945 xpos
+= m_scrollLineX
;
8948 if ( xpos
!= -1 || ypos
!= -1 )
8951 xpos
/= m_scrollLineX
;
8953 ypos
/= m_scrollLineY
;
8954 Scroll( xpos
, ypos
);
8961 // ------ Grid cursor movement functions
8965 wxGrid::DoMoveCursor(bool expandSelection
,
8966 const wxGridDirectionOperations
& diroper
)
8968 if ( m_currentCellCoords
== wxGridNoCellCoords
)
8971 if ( expandSelection
)
8973 wxGridCellCoords coords
= m_selectedBlockCorner
;
8974 if ( coords
== wxGridNoCellCoords
)
8975 coords
= m_currentCellCoords
;
8977 if ( diroper
.IsAtBoundary(coords
) )
8980 diroper
.Advance(coords
);
8982 UpdateBlockBeingSelected(m_currentCellCoords
, coords
);
8984 else // don't expand selection
8988 if ( diroper
.IsAtBoundary(m_currentCellCoords
) )
8991 wxGridCellCoords coords
= m_currentCellCoords
;
8992 diroper
.Advance(coords
);
9000 bool wxGrid::MoveCursorUp(bool expandSelection
)
9002 return DoMoveCursor(expandSelection
,
9003 wxGridBackwardOperations(this, wxGridRowOperations()));
9006 bool wxGrid::MoveCursorDown(bool expandSelection
)
9008 return DoMoveCursor(expandSelection
,
9009 wxGridForwardOperations(this, wxGridRowOperations()));
9012 bool wxGrid::MoveCursorLeft(bool expandSelection
)
9014 return DoMoveCursor(expandSelection
,
9015 wxGridBackwardOperations(this, wxGridColumnOperations()));
9018 bool wxGrid::MoveCursorRight(bool expandSelection
)
9020 return DoMoveCursor(expandSelection
,
9021 wxGridForwardOperations(this, wxGridColumnOperations()));
9024 bool wxGrid::DoMoveCursorByPage(const wxGridDirectionOperations
& diroper
)
9026 if ( m_currentCellCoords
== wxGridNoCellCoords
)
9029 if ( diroper
.IsAtBoundary(m_currentCellCoords
) )
9032 const int oldRow
= m_currentCellCoords
.GetRow();
9033 int newRow
= diroper
.MoveByPixelDistance(oldRow
, m_gridWin
->GetClientSize().y
);
9034 if ( newRow
== oldRow
)
9036 wxGridCellCoords
coords(m_currentCellCoords
);
9037 diroper
.Advance(coords
);
9038 newRow
= coords
.GetRow();
9041 GoToCell(newRow
, m_currentCellCoords
.GetCol());
9046 bool wxGrid::MovePageUp()
9048 return DoMoveCursorByPage(
9049 wxGridBackwardOperations(this, wxGridRowOperations()));
9052 bool wxGrid::MovePageDown()
9054 return DoMoveCursorByPage(
9055 wxGridForwardOperations(this, wxGridColumnOperations()));
9058 // helper of DoMoveCursorByBlock(): advance the cell coordinates using diroper
9059 // until we find a non-empty cell or reach the grid end
9061 wxGrid::AdvanceToNextNonEmpty(wxGridCellCoords
& coords
,
9062 const wxGridDirectionOperations
& diroper
)
9064 while ( !diroper
.IsAtBoundary(coords
) )
9066 diroper
.Advance(coords
);
9067 if ( !m_table
->IsEmpty(coords
) )
9073 wxGrid::DoMoveCursorByBlock(bool expandSelection
,
9074 const wxGridDirectionOperations
& diroper
)
9076 if ( !m_table
|| m_currentCellCoords
== wxGridNoCellCoords
)
9079 if ( diroper
.IsAtBoundary(m_currentCellCoords
) )
9082 wxGridCellCoords
coords(m_currentCellCoords
);
9083 if ( m_table
->IsEmpty(coords
) )
9085 // we are in an empty cell: find the next block of non-empty cells
9086 AdvanceToNextNonEmpty(coords
, diroper
);
9088 else // current cell is not empty
9090 diroper
.Advance(coords
);
9091 if ( m_table
->IsEmpty(coords
) )
9093 // we started at the end of a block, find the next one
9094 AdvanceToNextNonEmpty(coords
, diroper
);
9096 else // we're in a middle of a block
9098 // go to the end of it, i.e. find the last cell before the next
9100 while ( !diroper
.IsAtBoundary(coords
) )
9102 wxGridCellCoords
coordsNext(coords
);
9103 diroper
.Advance(coordsNext
);
9104 if ( m_table
->IsEmpty(coordsNext
) )
9107 coords
= coordsNext
;
9112 if ( expandSelection
)
9114 UpdateBlockBeingSelected(m_currentCellCoords
, coords
);
9125 bool wxGrid::MoveCursorUpBlock(bool expandSelection
)
9127 return DoMoveCursorByBlock(
9129 wxGridBackwardOperations(this, wxGridRowOperations())
9133 bool wxGrid::MoveCursorDownBlock( bool expandSelection
)
9135 return DoMoveCursorByBlock(
9137 wxGridForwardOperations(this, wxGridRowOperations())
9141 bool wxGrid::MoveCursorLeftBlock( bool expandSelection
)
9143 return DoMoveCursorByBlock(
9145 wxGridBackwardOperations(this, wxGridColumnOperations())
9149 bool wxGrid::MoveCursorRightBlock( bool expandSelection
)
9151 return DoMoveCursorByBlock(
9153 wxGridForwardOperations(this, wxGridColumnOperations())
9158 // ------ Label values and formatting
9161 void wxGrid::GetRowLabelAlignment( int *horiz
, int *vert
) const
9164 *horiz
= m_rowLabelHorizAlign
;
9166 *vert
= m_rowLabelVertAlign
;
9169 void wxGrid::GetColLabelAlignment( int *horiz
, int *vert
) const
9172 *horiz
= m_colLabelHorizAlign
;
9174 *vert
= m_colLabelVertAlign
;
9177 int wxGrid::GetColLabelTextOrientation() const
9179 return m_colLabelTextOrientation
;
9182 wxString
wxGrid::GetRowLabelValue( int row
) const
9186 return m_table
->GetRowLabelValue( row
);
9196 wxString
wxGrid::GetColLabelValue( int col
) const
9200 return m_table
->GetColLabelValue( col
);
9210 void wxGrid::SetRowLabelSize( int width
)
9212 wxASSERT( width
>= 0 || width
== wxGRID_AUTOSIZE
);
9214 if ( width
== wxGRID_AUTOSIZE
)
9216 width
= CalcColOrRowLabelAreaMinSize(wxGRID_ROW
);
9219 if ( width
!= m_rowLabelWidth
)
9223 m_rowLabelWin
->Show( false );
9224 m_cornerLabelWin
->Show( false );
9226 else if ( m_rowLabelWidth
== 0 )
9228 m_rowLabelWin
->Show( true );
9229 if ( m_colLabelHeight
> 0 )
9230 m_cornerLabelWin
->Show( true );
9233 m_rowLabelWidth
= width
;
9235 wxScrolledWindow::Refresh( true );
9239 void wxGrid::SetColLabelSize( int height
)
9241 wxASSERT( height
>=0 || height
== wxGRID_AUTOSIZE
);
9243 if ( height
== wxGRID_AUTOSIZE
)
9245 height
= CalcColOrRowLabelAreaMinSize(wxGRID_COLUMN
);
9248 if ( height
!= m_colLabelHeight
)
9252 m_colLabelWin
->Show( false );
9253 m_cornerLabelWin
->Show( false );
9255 else if ( m_colLabelHeight
== 0 )
9257 m_colLabelWin
->Show( true );
9258 if ( m_rowLabelWidth
> 0 )
9259 m_cornerLabelWin
->Show( true );
9262 m_colLabelHeight
= height
;
9264 wxScrolledWindow::Refresh( true );
9268 void wxGrid::SetLabelBackgroundColour( const wxColour
& colour
)
9270 if ( m_labelBackgroundColour
!= colour
)
9272 m_labelBackgroundColour
= colour
;
9273 m_rowLabelWin
->SetBackgroundColour( colour
);
9274 m_colLabelWin
->SetBackgroundColour( colour
);
9275 m_cornerLabelWin
->SetBackgroundColour( colour
);
9277 if ( !GetBatchCount() )
9279 m_rowLabelWin
->Refresh();
9280 m_colLabelWin
->Refresh();
9281 m_cornerLabelWin
->Refresh();
9286 void wxGrid::SetLabelTextColour( const wxColour
& colour
)
9288 if ( m_labelTextColour
!= colour
)
9290 m_labelTextColour
= colour
;
9291 if ( !GetBatchCount() )
9293 m_rowLabelWin
->Refresh();
9294 m_colLabelWin
->Refresh();
9299 void wxGrid::SetLabelFont( const wxFont
& font
)
9302 if ( !GetBatchCount() )
9304 m_rowLabelWin
->Refresh();
9305 m_colLabelWin
->Refresh();
9309 void wxGrid::SetRowLabelAlignment( int horiz
, int vert
)
9311 // allow old (incorrect) defs to be used
9314 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
9315 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
9316 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
9321 case wxTOP
: vert
= wxALIGN_TOP
; break;
9322 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
9323 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
9326 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
9328 m_rowLabelHorizAlign
= horiz
;
9331 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
9333 m_rowLabelVertAlign
= vert
;
9336 if ( !GetBatchCount() )
9338 m_rowLabelWin
->Refresh();
9342 void wxGrid::SetColLabelAlignment( int horiz
, int vert
)
9344 // allow old (incorrect) defs to be used
9347 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
9348 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
9349 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
9354 case wxTOP
: vert
= wxALIGN_TOP
; break;
9355 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
9356 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
9359 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
9361 m_colLabelHorizAlign
= horiz
;
9364 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
9366 m_colLabelVertAlign
= vert
;
9369 if ( !GetBatchCount() )
9371 m_colLabelWin
->Refresh();
9375 // Note: under MSW, the default column label font must be changed because it
9376 // does not support vertical printing
9378 // Example: wxFont font(9, wxSWISS, wxNORMAL, wxBOLD);
9379 // pGrid->SetLabelFont(font);
9380 // pGrid->SetColLabelTextOrientation(wxVERTICAL);
9382 void wxGrid::SetColLabelTextOrientation( int textOrientation
)
9384 if ( textOrientation
== wxHORIZONTAL
|| textOrientation
== wxVERTICAL
)
9385 m_colLabelTextOrientation
= textOrientation
;
9387 if ( !GetBatchCount() )
9388 m_colLabelWin
->Refresh();
9391 void wxGrid::SetRowLabelValue( int row
, const wxString
& s
)
9395 m_table
->SetRowLabelValue( row
, s
);
9396 if ( !GetBatchCount() )
9398 wxRect rect
= CellToRect( row
, 0 );
9399 if ( rect
.height
> 0 )
9401 CalcScrolledPosition(0, rect
.y
, &rect
.x
, &rect
.y
);
9403 rect
.width
= m_rowLabelWidth
;
9404 m_rowLabelWin
->Refresh( true, &rect
);
9410 void wxGrid::SetColLabelValue( int col
, const wxString
& s
)
9414 m_table
->SetColLabelValue( col
, s
);
9415 if ( !GetBatchCount() )
9417 wxRect rect
= CellToRect( 0, col
);
9418 if ( rect
.width
> 0 )
9420 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &rect
.y
);
9422 rect
.height
= m_colLabelHeight
;
9423 m_colLabelWin
->Refresh( true, &rect
);
9429 void wxGrid::SetGridLineColour( const wxColour
& colour
)
9431 if ( m_gridLineColour
!= colour
)
9433 m_gridLineColour
= colour
;
9435 if ( GridLinesEnabled() )
9440 void wxGrid::SetCellHighlightColour( const wxColour
& colour
)
9442 if ( m_cellHighlightColour
!= colour
)
9444 m_cellHighlightColour
= colour
;
9446 wxClientDC
dc( m_gridWin
);
9448 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
9449 DrawCellHighlight(dc
, attr
);
9454 void wxGrid::SetCellHighlightPenWidth(int width
)
9456 if (m_cellHighlightPenWidth
!= width
)
9458 m_cellHighlightPenWidth
= width
;
9460 // Just redrawing the cell highlight is not enough since that won't
9461 // make any visible change if the the thickness is getting smaller.
9462 int row
= m_currentCellCoords
.GetRow();
9463 int col
= m_currentCellCoords
.GetCol();
9464 if ( row
== -1 || col
== -1 || GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
9467 wxRect rect
= CellToRect(row
, col
);
9468 m_gridWin
->Refresh(true, &rect
);
9472 void wxGrid::SetCellHighlightROPenWidth(int width
)
9474 if (m_cellHighlightROPenWidth
!= width
)
9476 m_cellHighlightROPenWidth
= width
;
9478 // Just redrawing the cell highlight is not enough since that won't
9479 // make any visible change if the the thickness is getting smaller.
9480 int row
= m_currentCellCoords
.GetRow();
9481 int col
= m_currentCellCoords
.GetCol();
9482 if ( row
== -1 || col
== -1 ||
9483 GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
9486 wxRect rect
= CellToRect(row
, col
);
9487 m_gridWin
->Refresh(true, &rect
);
9491 void wxGrid::RedrawGridLines()
9493 // the lines will be redrawn when the window is thawn
9494 if ( GetBatchCount() )
9497 if ( GridLinesEnabled() )
9499 wxClientDC
dc( m_gridWin
);
9501 DrawAllGridLines( dc
, wxRegion() );
9503 else // remove the grid lines
9505 m_gridWin
->Refresh();
9509 void wxGrid::EnableGridLines( bool enable
)
9511 if ( enable
!= m_gridLinesEnabled
)
9513 m_gridLinesEnabled
= enable
;
9519 void wxGrid::DoClipGridLines(bool& var
, bool clip
)
9525 if ( GridLinesEnabled() )
9530 int wxGrid::GetDefaultRowSize() const
9532 return m_defaultRowHeight
;
9535 int wxGrid::GetRowSize( int row
) const
9537 wxCHECK_MSG( row
>= 0 && row
< m_numRows
, 0, _T("invalid row index") );
9539 return GetRowHeight(row
);
9542 int wxGrid::GetDefaultColSize() const
9544 return m_defaultColWidth
;
9547 int wxGrid::GetColSize( int col
) const
9549 wxCHECK_MSG( col
>= 0 && col
< m_numCols
, 0, _T("invalid column index") );
9551 return GetColWidth(col
);
9554 // ============================================================================
9555 // access to the grid attributes: each of them has a default value in the grid
9556 // itself and may be overidden on a per-cell basis
9557 // ============================================================================
9559 // ----------------------------------------------------------------------------
9560 // setting default attributes
9561 // ----------------------------------------------------------------------------
9563 void wxGrid::SetDefaultCellBackgroundColour( const wxColour
& col
)
9565 m_defaultCellAttr
->SetBackgroundColour(col
);
9567 m_gridWin
->SetBackgroundColour(col
);
9571 void wxGrid::SetDefaultCellTextColour( const wxColour
& col
)
9573 m_defaultCellAttr
->SetTextColour(col
);
9576 void wxGrid::SetDefaultCellAlignment( int horiz
, int vert
)
9578 m_defaultCellAttr
->SetAlignment(horiz
, vert
);
9581 void wxGrid::SetDefaultCellOverflow( bool allow
)
9583 m_defaultCellAttr
->SetOverflow(allow
);
9586 void wxGrid::SetDefaultCellFont( const wxFont
& font
)
9588 m_defaultCellAttr
->SetFont(font
);
9591 // For editors and renderers the type registry takes precedence over the
9592 // default attr, so we need to register the new editor/renderer for the string
9593 // data type in order to make setting a default editor/renderer appear to
9596 void wxGrid::SetDefaultRenderer(wxGridCellRenderer
*renderer
)
9598 RegisterDataType(wxGRID_VALUE_STRING
,
9600 GetDefaultEditorForType(wxGRID_VALUE_STRING
));
9603 void wxGrid::SetDefaultEditor(wxGridCellEditor
*editor
)
9605 RegisterDataType(wxGRID_VALUE_STRING
,
9606 GetDefaultRendererForType(wxGRID_VALUE_STRING
),
9610 // ----------------------------------------------------------------------------
9611 // access to the default attributes
9612 // ----------------------------------------------------------------------------
9614 wxColour
wxGrid::GetDefaultCellBackgroundColour() const
9616 return m_defaultCellAttr
->GetBackgroundColour();
9619 wxColour
wxGrid::GetDefaultCellTextColour() const
9621 return m_defaultCellAttr
->GetTextColour();
9624 wxFont
wxGrid::GetDefaultCellFont() const
9626 return m_defaultCellAttr
->GetFont();
9629 void wxGrid::GetDefaultCellAlignment( int *horiz
, int *vert
) const
9631 m_defaultCellAttr
->GetAlignment(horiz
, vert
);
9634 bool wxGrid::GetDefaultCellOverflow() const
9636 return m_defaultCellAttr
->GetOverflow();
9639 wxGridCellRenderer
*wxGrid::GetDefaultRenderer() const
9641 return m_defaultCellAttr
->GetRenderer(NULL
, 0, 0);
9644 wxGridCellEditor
*wxGrid::GetDefaultEditor() const
9646 return m_defaultCellAttr
->GetEditor(NULL
, 0, 0);
9649 // ----------------------------------------------------------------------------
9650 // access to cell attributes
9651 // ----------------------------------------------------------------------------
9653 wxColour
wxGrid::GetCellBackgroundColour(int row
, int col
) const
9655 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9656 wxColour colour
= attr
->GetBackgroundColour();
9662 wxColour
wxGrid::GetCellTextColour( int row
, int col
) const
9664 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9665 wxColour colour
= attr
->GetTextColour();
9671 wxFont
wxGrid::GetCellFont( int row
, int col
) const
9673 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9674 wxFont font
= attr
->GetFont();
9680 void wxGrid::GetCellAlignment( int row
, int col
, int *horiz
, int *vert
) const
9682 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9683 attr
->GetAlignment(horiz
, vert
);
9687 bool wxGrid::GetCellOverflow( int row
, int col
) const
9689 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9690 bool allow
= attr
->GetOverflow();
9696 void wxGrid::GetCellSize( int row
, int col
, int *num_rows
, int *num_cols
) const
9698 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9699 attr
->GetSize( num_rows
, num_cols
);
9703 wxGridCellRenderer
* wxGrid::GetCellRenderer(int row
, int col
) const
9705 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9706 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
9712 wxGridCellEditor
* wxGrid::GetCellEditor(int row
, int col
) const
9714 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9715 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
9721 bool wxGrid::IsReadOnly(int row
, int col
) const
9723 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9724 bool isReadOnly
= attr
->IsReadOnly();
9730 // ----------------------------------------------------------------------------
9731 // attribute support: cache, automatic provider creation, ...
9732 // ----------------------------------------------------------------------------
9734 bool wxGrid::CanHaveAttributes() const
9741 return m_table
->CanHaveAttributes();
9744 void wxGrid::ClearAttrCache()
9746 if ( m_attrCache
.row
!= -1 )
9748 wxGridCellAttr
*oldAttr
= m_attrCache
.attr
;
9749 m_attrCache
.attr
= NULL
;
9750 m_attrCache
.row
= -1;
9751 // wxSafeDecRec(...) might cause event processing that accesses
9752 // the cached attribute, if one exists (e.g. by deleting the
9753 // editor stored within the attribute). Therefore it is important
9754 // to invalidate the cache before calling wxSafeDecRef!
9755 wxSafeDecRef(oldAttr
);
9759 void wxGrid::CacheAttr(int row
, int col
, wxGridCellAttr
*attr
) const
9763 wxGrid
*self
= (wxGrid
*)this; // const_cast
9765 self
->ClearAttrCache();
9766 self
->m_attrCache
.row
= row
;
9767 self
->m_attrCache
.col
= col
;
9768 self
->m_attrCache
.attr
= attr
;
9773 bool wxGrid::LookupAttr(int row
, int col
, wxGridCellAttr
**attr
) const
9775 if ( row
== m_attrCache
.row
&& col
== m_attrCache
.col
)
9777 *attr
= m_attrCache
.attr
;
9778 wxSafeIncRef(m_attrCache
.attr
);
9780 #ifdef DEBUG_ATTR_CACHE
9781 gs_nAttrCacheHits
++;
9788 #ifdef DEBUG_ATTR_CACHE
9789 gs_nAttrCacheMisses
++;
9796 wxGridCellAttr
*wxGrid::GetCellAttr(int row
, int col
) const
9798 wxGridCellAttr
*attr
= NULL
;
9799 // Additional test to avoid looking at the cache e.g. for
9800 // wxNoCellCoords, as this will confuse memory management.
9803 if ( !LookupAttr(row
, col
, &attr
) )
9805 attr
= m_table
? m_table
->GetAttr(row
, col
, wxGridCellAttr::Any
)
9807 CacheAttr(row
, col
, attr
);
9813 attr
->SetDefAttr(m_defaultCellAttr
);
9817 attr
= m_defaultCellAttr
;
9824 wxGridCellAttr
*wxGrid::GetOrCreateCellAttr(int row
, int col
) const
9826 wxGridCellAttr
*attr
= NULL
;
9827 bool canHave
= ((wxGrid
*)this)->CanHaveAttributes();
9829 wxCHECK_MSG( canHave
, attr
, _T("Cell attributes not allowed"));
9830 wxCHECK_MSG( m_table
, attr
, _T("must have a table") );
9832 attr
= m_table
->GetAttr(row
, col
, wxGridCellAttr::Cell
);
9835 attr
= new wxGridCellAttr(m_defaultCellAttr
);
9837 // artificially inc the ref count to match DecRef() in caller
9839 m_table
->SetAttr(attr
, row
, col
);
9845 // ----------------------------------------------------------------------------
9846 // setting column attributes (wrappers around SetColAttr)
9847 // ----------------------------------------------------------------------------
9849 void wxGrid::SetColFormatBool(int col
)
9851 SetColFormatCustom(col
, wxGRID_VALUE_BOOL
);
9854 void wxGrid::SetColFormatNumber(int col
)
9856 SetColFormatCustom(col
, wxGRID_VALUE_NUMBER
);
9859 void wxGrid::SetColFormatFloat(int col
, int width
, int precision
)
9861 wxString typeName
= wxGRID_VALUE_FLOAT
;
9862 if ( (width
!= -1) || (precision
!= -1) )
9864 typeName
<< _T(':') << width
<< _T(',') << precision
;
9867 SetColFormatCustom(col
, typeName
);
9870 void wxGrid::SetColFormatCustom(int col
, const wxString
& typeName
)
9872 wxGridCellAttr
*attr
= m_table
->GetAttr(-1, col
, wxGridCellAttr::Col
);
9874 attr
= new wxGridCellAttr
;
9875 wxGridCellRenderer
*renderer
= GetDefaultRendererForType(typeName
);
9876 attr
->SetRenderer(renderer
);
9877 wxGridCellEditor
*editor
= GetDefaultEditorForType(typeName
);
9878 attr
->SetEditor(editor
);
9880 SetColAttr(col
, attr
);
9884 // ----------------------------------------------------------------------------
9885 // setting cell attributes: this is forwarded to the table
9886 // ----------------------------------------------------------------------------
9888 void wxGrid::SetAttr(int row
, int col
, wxGridCellAttr
*attr
)
9890 if ( CanHaveAttributes() )
9892 m_table
->SetAttr(attr
, row
, col
);
9901 void wxGrid::SetRowAttr(int row
, wxGridCellAttr
*attr
)
9903 if ( CanHaveAttributes() )
9905 m_table
->SetRowAttr(attr
, row
);
9914 void wxGrid::SetColAttr(int col
, wxGridCellAttr
*attr
)
9916 if ( CanHaveAttributes() )
9918 m_table
->SetColAttr(attr
, col
);
9927 void wxGrid::SetCellBackgroundColour( int row
, int col
, const wxColour
& colour
)
9929 if ( CanHaveAttributes() )
9931 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9932 attr
->SetBackgroundColour(colour
);
9937 void wxGrid::SetCellTextColour( int row
, int col
, const wxColour
& colour
)
9939 if ( CanHaveAttributes() )
9941 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9942 attr
->SetTextColour(colour
);
9947 void wxGrid::SetCellFont( int row
, int col
, const wxFont
& font
)
9949 if ( CanHaveAttributes() )
9951 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9952 attr
->SetFont(font
);
9957 void wxGrid::SetCellAlignment( int row
, int col
, int horiz
, int vert
)
9959 if ( CanHaveAttributes() )
9961 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9962 attr
->SetAlignment(horiz
, vert
);
9967 void wxGrid::SetCellOverflow( int row
, int col
, bool allow
)
9969 if ( CanHaveAttributes() )
9971 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9972 attr
->SetOverflow(allow
);
9977 void wxGrid::SetCellSize( int row
, int col
, int num_rows
, int num_cols
)
9979 if ( CanHaveAttributes() )
9981 int cell_rows
, cell_cols
;
9983 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9984 attr
->GetSize(&cell_rows
, &cell_cols
);
9985 attr
->SetSize(num_rows
, num_cols
);
9988 // Cannot set the size of a cell to 0 or negative values
9989 // While it is perfectly legal to do that, this function cannot
9990 // handle all the possibilies, do it by hand by getting the CellAttr.
9991 // You can only set the size of a cell to 1,1 or greater with this fn
9992 wxASSERT_MSG( !((cell_rows
< 1) || (cell_cols
< 1)),
9993 wxT("wxGrid::SetCellSize setting cell size that is already part of another cell"));
9994 wxASSERT_MSG( !((num_rows
< 1) || (num_cols
< 1)),
9995 wxT("wxGrid::SetCellSize setting cell size to < 1"));
9997 // if this was already a multicell then "turn off" the other cells first
9998 if ((cell_rows
> 1) || (cell_cols
> 1))
10001 for (j
=row
; j
< row
+ cell_rows
; j
++)
10003 for (i
=col
; i
< col
+ cell_cols
; i
++)
10005 if ((i
!= col
) || (j
!= row
))
10007 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
10008 attr_stub
->SetSize( 1, 1 );
10009 attr_stub
->DecRef();
10015 // mark the cells that will be covered by this cell to
10016 // negative or zero values to point back at this cell
10017 if (((num_rows
> 1) || (num_cols
> 1)) && (num_rows
>= 1) && (num_cols
>= 1))
10020 for (j
=row
; j
< row
+ num_rows
; j
++)
10022 for (i
=col
; i
< col
+ num_cols
; i
++)
10024 if ((i
!= col
) || (j
!= row
))
10026 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
10027 attr_stub
->SetSize( row
- j
, col
- i
);
10028 attr_stub
->DecRef();
10036 void wxGrid::SetCellRenderer(int row
, int col
, wxGridCellRenderer
*renderer
)
10038 if ( CanHaveAttributes() )
10040 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10041 attr
->SetRenderer(renderer
);
10046 void wxGrid::SetCellEditor(int row
, int col
, wxGridCellEditor
* editor
)
10048 if ( CanHaveAttributes() )
10050 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10051 attr
->SetEditor(editor
);
10056 void wxGrid::SetReadOnly(int row
, int col
, bool isReadOnly
)
10058 if ( CanHaveAttributes() )
10060 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10061 attr
->SetReadOnly(isReadOnly
);
10066 // ----------------------------------------------------------------------------
10067 // Data type registration
10068 // ----------------------------------------------------------------------------
10070 void wxGrid::RegisterDataType(const wxString
& typeName
,
10071 wxGridCellRenderer
* renderer
,
10072 wxGridCellEditor
* editor
)
10074 m_typeRegistry
->RegisterDataType(typeName
, renderer
, editor
);
10078 wxGridCellEditor
* wxGrid::GetDefaultEditorForCell(int row
, int col
) const
10080 wxString typeName
= m_table
->GetTypeName(row
, col
);
10081 return GetDefaultEditorForType(typeName
);
10084 wxGridCellRenderer
* wxGrid::GetDefaultRendererForCell(int row
, int col
) const
10086 wxString typeName
= m_table
->GetTypeName(row
, col
);
10087 return GetDefaultRendererForType(typeName
);
10090 wxGridCellEditor
* wxGrid::GetDefaultEditorForType(const wxString
& typeName
) const
10092 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
10093 if ( index
== wxNOT_FOUND
)
10095 wxFAIL_MSG(wxString::Format(wxT("Unknown data type name [%s]"), typeName
.c_str()));
10100 return m_typeRegistry
->GetEditor(index
);
10103 wxGridCellRenderer
* wxGrid::GetDefaultRendererForType(const wxString
& typeName
) const
10105 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
10106 if ( index
== wxNOT_FOUND
)
10108 wxFAIL_MSG(wxString::Format(wxT("Unknown data type name [%s]"), typeName
.c_str()));
10113 return m_typeRegistry
->GetRenderer(index
);
10116 // ----------------------------------------------------------------------------
10118 // ----------------------------------------------------------------------------
10120 void wxGrid::EnableDragRowSize( bool enable
)
10122 m_canDragRowSize
= enable
;
10125 void wxGrid::EnableDragColSize( bool enable
)
10127 m_canDragColSize
= enable
;
10130 void wxGrid::EnableDragGridSize( bool enable
)
10132 m_canDragGridSize
= enable
;
10135 void wxGrid::EnableDragCell( bool enable
)
10137 m_canDragCell
= enable
;
10140 void wxGrid::SetDefaultRowSize( int height
, bool resizeExistingRows
)
10142 m_defaultRowHeight
= wxMax( height
, m_minAcceptableRowHeight
);
10144 if ( resizeExistingRows
)
10146 // since we are resizing all rows to the default row size,
10147 // we can simply clear the row heights and row bottoms
10148 // arrays (which also allows us to take advantage of
10149 // some speed optimisations)
10150 m_rowHeights
.Empty();
10151 m_rowBottoms
.Empty();
10152 if ( !GetBatchCount() )
10157 void wxGrid::SetRowSize( int row
, int height
)
10159 wxCHECK_RET( row
>= 0 && row
< m_numRows
, _T("invalid row index") );
10161 // if < 0 then calculate new height from label
10165 wxArrayString lines
;
10166 wxClientDC
dc(m_rowLabelWin
);
10167 dc
.SetFont(GetLabelFont());
10168 StringToLines(GetRowLabelValue( row
), lines
);
10169 GetTextBoxSize( dc
, lines
, &w
, &h
);
10170 //check that it is not less than the minimal height
10171 height
= wxMax(h
, GetRowMinimalAcceptableHeight());
10174 // See comment in SetColSize
10175 if ( height
< GetRowMinimalAcceptableHeight())
10178 if ( m_rowHeights
.IsEmpty() )
10180 // need to really create the array
10184 int h
= wxMax( 0, height
);
10185 int diff
= h
- m_rowHeights
[row
];
10187 m_rowHeights
[row
] = h
;
10188 for ( int i
= row
; i
< m_numRows
; i
++ )
10190 m_rowBottoms
[i
] += diff
;
10193 if ( !GetBatchCount() )
10197 void wxGrid::SetDefaultColSize( int width
, bool resizeExistingCols
)
10199 // we dont allow zero default column width
10200 m_defaultColWidth
= wxMax( wxMax( width
, m_minAcceptableColWidth
), 1 );
10202 if ( resizeExistingCols
)
10204 // since we are resizing all columns to the default column size,
10205 // we can simply clear the col widths and col rights
10206 // arrays (which also allows us to take advantage of
10207 // some speed optimisations)
10208 m_colWidths
.Empty();
10209 m_colRights
.Empty();
10210 if ( !GetBatchCount() )
10215 void wxGrid::SetColSize( int col
, int width
)
10217 wxCHECK_RET( col
>= 0 && col
< m_numCols
, _T("invalid column index") );
10219 // if < 0 then calculate new width from label
10223 wxArrayString lines
;
10224 wxClientDC
dc(m_colLabelWin
);
10225 dc
.SetFont(GetLabelFont());
10226 StringToLines(GetColLabelValue(col
), lines
);
10227 if ( GetColLabelTextOrientation() == wxHORIZONTAL
)
10228 GetTextBoxSize( dc
, lines
, &w
, &h
);
10230 GetTextBoxSize( dc
, lines
, &h
, &w
);
10232 //check that it is not less than the minimal width
10233 width
= wxMax(width
, GetColMinimalAcceptableWidth());
10236 // should we check that it's bigger than GetColMinimalWidth(col) here?
10238 // No, because it is reasonable to assume the library user know's
10239 // what he is doing. However we should test against the weaker
10240 // constraint of minimalAcceptableWidth, as this breaks rendering
10242 // This test then fixes sf.net bug #645734
10244 if ( width
< GetColMinimalAcceptableWidth() )
10247 if ( m_colWidths
.IsEmpty() )
10249 // need to really create the array
10253 int w
= wxMax( 0, width
);
10254 int diff
= w
- m_colWidths
[col
];
10255 m_colWidths
[col
] = w
;
10257 for ( int colPos
= GetColPos(col
); colPos
< m_numCols
; colPos
++ )
10259 m_colRights
[GetColAt(colPos
)] += diff
;
10262 if ( !GetBatchCount() )
10266 void wxGrid::SetColMinimalWidth( int col
, int width
)
10268 if (width
> GetColMinimalAcceptableWidth())
10270 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)col
;
10271 m_colMinWidths
[key
] = width
;
10275 void wxGrid::SetRowMinimalHeight( int row
, int width
)
10277 if (width
> GetRowMinimalAcceptableHeight())
10279 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)row
;
10280 m_rowMinHeights
[key
] = width
;
10284 int wxGrid::GetColMinimalWidth(int col
) const
10286 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)col
;
10287 wxLongToLongHashMap::const_iterator it
= m_colMinWidths
.find(key
);
10289 return it
!= m_colMinWidths
.end() ? (int)it
->second
: m_minAcceptableColWidth
;
10292 int wxGrid::GetRowMinimalHeight(int row
) const
10294 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)row
;
10295 wxLongToLongHashMap::const_iterator it
= m_rowMinHeights
.find(key
);
10297 return it
!= m_rowMinHeights
.end() ? (int)it
->second
: m_minAcceptableRowHeight
;
10300 void wxGrid::SetColMinimalAcceptableWidth( int width
)
10302 // We do allow a width of 0 since this gives us
10303 // an easy way to temporarily hiding columns.
10305 m_minAcceptableColWidth
= width
;
10308 void wxGrid::SetRowMinimalAcceptableHeight( int height
)
10310 // We do allow a height of 0 since this gives us
10311 // an easy way to temporarily hiding rows.
10313 m_minAcceptableRowHeight
= height
;
10316 int wxGrid::GetColMinimalAcceptableWidth() const
10318 return m_minAcceptableColWidth
;
10321 int wxGrid::GetRowMinimalAcceptableHeight() const
10323 return m_minAcceptableRowHeight
;
10326 // ----------------------------------------------------------------------------
10328 // ----------------------------------------------------------------------------
10331 wxGrid::AutoSizeColOrRow(int colOrRow
, bool setAsMin
, wxGridDirection direction
)
10333 const bool column
= direction
== wxGRID_COLUMN
;
10335 wxClientDC
dc(m_gridWin
);
10337 // cancel editing of cell
10338 HideCellEditControl();
10339 SaveEditControlValue();
10341 // init both of them to avoid compiler warnings, even if we only need one
10349 wxCoord extent
, extentMax
= 0;
10350 int max
= column
? m_numRows
: m_numCols
;
10351 for ( int rowOrCol
= 0; rowOrCol
< max
; rowOrCol
++ )
10358 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
10359 wxGridCellRenderer
*renderer
= attr
->GetRenderer(this, row
, col
);
10362 wxSize size
= renderer
->GetBestSize(*this, *attr
, dc
, row
, col
);
10363 extent
= column
? size
.x
: size
.y
;
10364 if ( extent
> extentMax
)
10365 extentMax
= extent
;
10367 renderer
->DecRef();
10373 // now also compare with the column label extent
10375 dc
.SetFont( GetLabelFont() );
10379 dc
.GetMultiLineTextExtent( GetColLabelValue(col
), &w
, &h
);
10380 if ( GetColLabelTextOrientation() == wxVERTICAL
)
10384 dc
.GetMultiLineTextExtent( GetRowLabelValue(row
), &w
, &h
);
10386 extent
= column
? w
: h
;
10387 if ( extent
> extentMax
)
10388 extentMax
= extent
;
10392 // empty column - give default extent (notice that if extentMax is less
10393 // than default extent but != 0, it's OK)
10394 extentMax
= column
? m_defaultColWidth
: m_defaultRowHeight
;
10399 // leave some space around text
10407 // Ensure automatic width is not less than minimal width. See the
10408 // comment in SetColSize() for explanation of why this isn't done
10409 // in SetColSize().
10411 extentMax
= wxMax(extentMax
, GetColMinimalWidth(col
));
10413 SetColSize( col
, extentMax
);
10414 if ( !GetBatchCount() )
10417 m_gridWin
->GetClientSize( &cw
, &ch
);
10418 wxRect
rect ( CellToRect( 0, col
) );
10420 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
10421 rect
.width
= cw
- rect
.x
;
10422 rect
.height
= m_colLabelHeight
;
10423 m_colLabelWin
->Refresh( true, &rect
);
10428 // Ensure automatic width is not less than minimal height. See the
10429 // comment in SetColSize() for explanation of why this isn't done
10430 // in SetRowSize().
10432 extentMax
= wxMax(extentMax
, GetRowMinimalHeight(row
));
10434 SetRowSize(row
, extentMax
);
10435 if ( !GetBatchCount() )
10438 m_gridWin
->GetClientSize( &cw
, &ch
);
10439 wxRect
rect( CellToRect( row
, 0 ) );
10441 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
10442 rect
.width
= m_rowLabelWidth
;
10443 rect
.height
= ch
- rect
.y
;
10444 m_rowLabelWin
->Refresh( true, &rect
);
10451 SetColMinimalWidth(col
, extentMax
);
10453 SetRowMinimalHeight(row
, extentMax
);
10457 wxCoord
wxGrid::CalcColOrRowLabelAreaMinSize(wxGridDirection direction
)
10459 // calculate size for the rows or columns?
10460 const bool calcRows
= direction
== wxGRID_ROW
;
10462 wxClientDC
dc(calcRows
? GetGridRowLabelWindow()
10463 : GetGridColLabelWindow());
10464 dc
.SetFont(GetLabelFont());
10466 // which dimension should we take into account for calculations?
10468 // for columns, the text can be only horizontal so it's easy but for rows
10469 // we also have to take into account the text orientation
10471 useWidth
= calcRows
|| (GetColLabelTextOrientation() == wxVERTICAL
);
10473 wxArrayString lines
;
10474 wxCoord extentMax
= 0;
10476 const int numRowsOrCols
= calcRows
? m_numRows
: m_numCols
;
10477 for ( int rowOrCol
= 0; rowOrCol
< numRowsOrCols
; rowOrCol
++ )
10481 wxString label
= calcRows
? GetRowLabelValue(rowOrCol
)
10482 : GetColLabelValue(rowOrCol
);
10483 StringToLines(label
, lines
);
10486 GetTextBoxSize(dc
, lines
, &w
, &h
);
10488 const wxCoord extent
= useWidth
? w
: h
;
10489 if ( extent
> extentMax
)
10490 extentMax
= extent
;
10495 // empty column - give default extent (notice that if extentMax is less
10496 // than default extent but != 0, it's OK)
10497 extentMax
= calcRows
? GetDefaultRowLabelSize()
10498 : GetDefaultColLabelSize();
10501 // leave some space around text (taken from AutoSizeColOrRow)
10510 int wxGrid::SetOrCalcColumnSizes(bool calcOnly
, bool setAsMin
)
10512 int width
= m_rowLabelWidth
;
10514 wxGridUpdateLocker locker
;
10516 locker
.Create(this);
10518 for ( int col
= 0; col
< m_numCols
; col
++ )
10521 AutoSizeColumn(col
, setAsMin
);
10523 width
+= GetColWidth(col
);
10529 int wxGrid::SetOrCalcRowSizes(bool calcOnly
, bool setAsMin
)
10531 int height
= m_colLabelHeight
;
10533 wxGridUpdateLocker locker
;
10535 locker
.Create(this);
10537 for ( int row
= 0; row
< m_numRows
; row
++ )
10540 AutoSizeRow(row
, setAsMin
);
10542 height
+= GetRowHeight(row
);
10548 void wxGrid::AutoSize()
10550 wxGridUpdateLocker
locker(this);
10552 wxSize
size(SetOrCalcColumnSizes(false) - m_rowLabelWidth
+ m_extraWidth
,
10553 SetOrCalcRowSizes(false) - m_colLabelHeight
+ m_extraHeight
);
10555 // we know that we're not going to have scrollbars so disable them now to
10556 // avoid trouble in SetClientSize() which can otherwise set the correct
10557 // client size but also leave space for (not needed any more) scrollbars
10558 SetScrollbars(0, 0, 0, 0, 0, 0, true);
10560 // restore the scroll rate parameters overwritten by SetScrollbars()
10561 SetScrollRate(m_scrollLineX
, m_scrollLineY
);
10563 SetClientSize(size
.x
+ m_rowLabelWidth
, size
.y
+ m_colLabelHeight
);
10566 void wxGrid::AutoSizeRowLabelSize( int row
)
10568 // Hide the edit control, so it
10569 // won't interfere with drag-shrinking.
10570 if ( IsCellEditControlShown() )
10572 HideCellEditControl();
10573 SaveEditControlValue();
10576 // autosize row height depending on label text
10577 SetRowSize(row
, -1);
10581 void wxGrid::AutoSizeColLabelSize( int col
)
10583 // Hide the edit control, so it
10584 // won't interfere with drag-shrinking.
10585 if ( IsCellEditControlShown() )
10587 HideCellEditControl();
10588 SaveEditControlValue();
10591 // autosize column width depending on label text
10592 SetColSize(col
, -1);
10596 wxSize
wxGrid::DoGetBestSize() const
10598 wxGrid
*self
= (wxGrid
*)this; // const_cast
10600 // we do the same as in AutoSize() here with the exception that we don't
10601 // change the column/row sizes, only calculate them
10602 wxSize
size(self
->SetOrCalcColumnSizes(true) - m_rowLabelWidth
+ m_extraWidth
,
10603 self
->SetOrCalcRowSizes(true) - m_colLabelHeight
+ m_extraHeight
);
10605 // NOTE: This size should be cached, but first we need to add calls to
10606 // InvalidateBestSize everywhere that could change the results of this
10608 // CacheBestSize(size);
10610 return wxSize(size
.x
+ m_rowLabelWidth
, size
.y
+ m_colLabelHeight
)
10611 + GetWindowBorderSize();
10619 wxPen
& wxGrid::GetDividerPen() const
10624 // ----------------------------------------------------------------------------
10625 // cell value accessor functions
10626 // ----------------------------------------------------------------------------
10628 void wxGrid::SetCellValue( int row
, int col
, const wxString
& s
)
10632 m_table
->SetValue( row
, col
, s
);
10633 if ( !GetBatchCount() )
10636 wxRect
rect( CellToRect( row
, col
) );
10638 rect
.width
= m_gridWin
->GetClientSize().GetWidth();
10639 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
10640 m_gridWin
->Refresh( false, &rect
);
10643 if ( m_currentCellCoords
.GetRow() == row
&&
10644 m_currentCellCoords
.GetCol() == col
&&
10645 IsCellEditControlShown())
10646 // Note: If we are using IsCellEditControlEnabled,
10647 // this interacts badly with calling SetCellValue from
10648 // an EVT_GRID_CELL_CHANGE handler.
10650 HideCellEditControl();
10651 ShowCellEditControl(); // will reread data from table
10656 // ----------------------------------------------------------------------------
10657 // block, row and column selection
10658 // ----------------------------------------------------------------------------
10660 void wxGrid::SelectRow( int row
, bool addToSelected
)
10662 if ( !m_selection
)
10665 if ( !addToSelected
)
10668 m_selection
->SelectRow(row
);
10671 void wxGrid::SelectCol( int col
, bool addToSelected
)
10673 if ( !m_selection
)
10676 if ( !addToSelected
)
10679 m_selection
->SelectCol(col
);
10682 void wxGrid::SelectBlock(int topRow
, int leftCol
, int bottomRow
, int rightCol
,
10683 bool addToSelected
)
10685 if ( !m_selection
)
10688 if ( !addToSelected
)
10691 m_selection
->SelectBlock(topRow
, leftCol
, bottomRow
, rightCol
);
10694 void wxGrid::SelectAll()
10696 if ( m_numRows
> 0 && m_numCols
> 0 )
10699 m_selection
->SelectBlock( 0, 0, m_numRows
- 1, m_numCols
- 1 );
10703 // ----------------------------------------------------------------------------
10704 // cell, row and col deselection
10705 // ----------------------------------------------------------------------------
10707 void wxGrid::DeselectLine(int line
, const wxGridOperations
& oper
)
10709 if ( !m_selection
)
10712 const wxGridSelectionModes mode
= m_selection
->GetSelectionMode();
10713 if ( mode
== oper
.GetSelectionMode() )
10715 const wxGridCellCoords
c(oper
.MakeCoords(line
, 0));
10716 if ( m_selection
->IsInSelection(c
) )
10717 m_selection
->ToggleCellSelection(c
);
10719 else if ( mode
!= oper
.Dual().GetSelectionMode() )
10721 const int nOther
= oper
.Dual().GetNumberOfLines(this);
10722 for ( int i
= 0; i
< nOther
; i
++ )
10724 const wxGridCellCoords
c(oper
.MakeCoords(line
, i
));
10725 if ( m_selection
->IsInSelection(c
) )
10726 m_selection
->ToggleCellSelection(c
);
10729 //else: can only select orthogonal lines so no lines in this direction
10730 // could have been selected anyhow
10733 void wxGrid::DeselectRow(int row
)
10735 DeselectLine(row
, wxGridRowOperations());
10738 void wxGrid::DeselectCol(int col
)
10740 DeselectLine(col
, wxGridColumnOperations());
10743 void wxGrid::DeselectCell( int row
, int col
)
10745 if ( m_selection
&& m_selection
->IsInSelection(row
, col
) )
10746 m_selection
->ToggleCellSelection(row
, col
);
10749 bool wxGrid::IsSelection() const
10751 return ( m_selection
&& (m_selection
->IsSelection() ||
10752 ( m_selectedBlockTopLeft
!= wxGridNoCellCoords
&&
10753 m_selectedBlockBottomRight
!= wxGridNoCellCoords
) ) );
10756 bool wxGrid::IsInSelection( int row
, int col
) const
10758 return ( m_selection
&& (m_selection
->IsInSelection( row
, col
) ||
10759 ( row
>= m_selectedBlockTopLeft
.GetRow() &&
10760 col
>= m_selectedBlockTopLeft
.GetCol() &&
10761 row
<= m_selectedBlockBottomRight
.GetRow() &&
10762 col
<= m_selectedBlockBottomRight
.GetCol() )) );
10765 wxGridCellCoordsArray
wxGrid::GetSelectedCells() const
10769 wxGridCellCoordsArray a
;
10773 return m_selection
->m_cellSelection
;
10776 wxGridCellCoordsArray
wxGrid::GetSelectionBlockTopLeft() const
10780 wxGridCellCoordsArray a
;
10784 return m_selection
->m_blockSelectionTopLeft
;
10787 wxGridCellCoordsArray
wxGrid::GetSelectionBlockBottomRight() const
10791 wxGridCellCoordsArray a
;
10795 return m_selection
->m_blockSelectionBottomRight
;
10798 wxArrayInt
wxGrid::GetSelectedRows() const
10806 return m_selection
->m_rowSelection
;
10809 wxArrayInt
wxGrid::GetSelectedCols() const
10817 return m_selection
->m_colSelection
;
10820 void wxGrid::ClearSelection()
10822 wxRect r1
= BlockToDeviceRect(m_selectedBlockTopLeft
,
10823 m_selectedBlockBottomRight
);
10824 wxRect r2
= BlockToDeviceRect(m_currentCellCoords
,
10825 m_selectedBlockCorner
);
10827 m_selectedBlockTopLeft
=
10828 m_selectedBlockBottomRight
=
10829 m_selectedBlockCorner
= wxGridNoCellCoords
;
10831 Refresh( false, &r1
);
10832 Refresh( false, &r2
);
10835 m_selection
->ClearSelection();
10838 // This function returns the rectangle that encloses the given block
10839 // in device coords clipped to the client size of the grid window.
10841 wxRect
wxGrid::BlockToDeviceRect( const wxGridCellCoords
& topLeft
,
10842 const wxGridCellCoords
& bottomRight
) const
10845 wxRect tempCellRect
= CellToRect(topLeft
);
10846 if ( tempCellRect
!= wxGridNoCellRect
)
10848 resultRect
= tempCellRect
;
10852 resultRect
= wxRect(0, 0, 0, 0);
10855 tempCellRect
= CellToRect(bottomRight
);
10856 if ( tempCellRect
!= wxGridNoCellRect
)
10858 resultRect
+= tempCellRect
;
10862 // If both inputs were "wxGridNoCellRect," then there's nothing to do.
10863 return wxGridNoCellRect
;
10866 // Ensure that left/right and top/bottom pairs are in order.
10867 int left
= resultRect
.GetLeft();
10868 int top
= resultRect
.GetTop();
10869 int right
= resultRect
.GetRight();
10870 int bottom
= resultRect
.GetBottom();
10872 int leftCol
= topLeft
.GetCol();
10873 int topRow
= topLeft
.GetRow();
10874 int rightCol
= bottomRight
.GetCol();
10875 int bottomRow
= bottomRight
.GetRow();
10884 leftCol
= rightCol
;
10895 topRow
= bottomRow
;
10899 // The following loop is ONLY necessary to detect and handle merged cells.
10901 m_gridWin
->GetClientSize( &cw
, &ch
);
10903 // Get the origin coordinates: notice that they will be negative if the
10904 // grid is scrolled downwards/to the right.
10905 int gridOriginX
= 0;
10906 int gridOriginY
= 0;
10907 CalcScrolledPosition(gridOriginX
, gridOriginY
, &gridOriginX
, &gridOriginY
);
10909 int onScreenLeftmostCol
= internalXToCol(-gridOriginX
);
10910 int onScreenUppermostRow
= internalYToRow(-gridOriginY
);
10912 int onScreenRightmostCol
= internalXToCol(-gridOriginX
+ cw
);
10913 int onScreenBottommostRow
= internalYToRow(-gridOriginY
+ ch
);
10915 // Bound our loop so that we only examine the portion of the selected block
10916 // that is shown on screen. Therefore, we compare the Top-Left block values
10917 // to the Top-Left screen values, and the Bottom-Right block values to the
10918 // Bottom-Right screen values, choosing appropriately.
10919 const int visibleTopRow
= wxMax(topRow
, onScreenUppermostRow
);
10920 const int visibleBottomRow
= wxMin(bottomRow
, onScreenBottommostRow
);
10921 const int visibleLeftCol
= wxMax(leftCol
, onScreenLeftmostCol
);
10922 const int visibleRightCol
= wxMin(rightCol
, onScreenRightmostCol
);
10924 for ( int j
= visibleTopRow
; j
<= visibleBottomRow
; j
++ )
10926 for ( int i
= visibleLeftCol
; i
<= visibleRightCol
; i
++ )
10928 if ( (j
== visibleTopRow
) || (j
== visibleBottomRow
) ||
10929 (i
== visibleLeftCol
) || (i
== visibleRightCol
) )
10931 tempCellRect
= CellToRect( j
, i
);
10933 if (tempCellRect
.x
< left
)
10934 left
= tempCellRect
.x
;
10935 if (tempCellRect
.y
< top
)
10936 top
= tempCellRect
.y
;
10937 if (tempCellRect
.x
+ tempCellRect
.width
> right
)
10938 right
= tempCellRect
.x
+ tempCellRect
.width
;
10939 if (tempCellRect
.y
+ tempCellRect
.height
> bottom
)
10940 bottom
= tempCellRect
.y
+ tempCellRect
.height
;
10944 i
= visibleRightCol
; // jump over inner cells.
10949 // Convert to scrolled coords
10950 CalcScrolledPosition( left
, top
, &left
, &top
);
10951 CalcScrolledPosition( right
, bottom
, &right
, &bottom
);
10953 if (right
< 0 || bottom
< 0 || left
> cw
|| top
> ch
)
10954 return wxRect(0,0,0,0);
10956 resultRect
.SetLeft( wxMax(0, left
) );
10957 resultRect
.SetTop( wxMax(0, top
) );
10958 resultRect
.SetRight( wxMin(cw
, right
) );
10959 resultRect
.SetBottom( wxMin(ch
, bottom
) );
10964 // ----------------------------------------------------------------------------
10966 // ----------------------------------------------------------------------------
10968 #if wxUSE_DRAG_AND_DROP
10970 // this allow setting drop target directly on wxGrid
10971 void wxGrid::SetDropTarget(wxDropTarget
*dropTarget
)
10973 GetGridWindow()->SetDropTarget(dropTarget
);
10976 #endif // wxUSE_DRAG_AND_DROP
10978 // ----------------------------------------------------------------------------
10979 // grid event classes
10980 // ----------------------------------------------------------------------------
10982 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent
, wxNotifyEvent
)
10984 wxGridEvent::wxGridEvent( int id
, wxEventType type
, wxObject
* obj
,
10985 int row
, int col
, int x
, int y
, bool sel
,
10986 bool control
, bool shift
, bool alt
, bool meta
)
10987 : wxNotifyEvent( type
, id
),
10988 wxKeyboardState(control
, shift
, alt
, meta
)
10990 Init(row
, col
, x
, y
, sel
);
10992 SetEventObject(obj
);
10995 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent
, wxNotifyEvent
)
10997 wxGridSizeEvent::wxGridSizeEvent( int id
, wxEventType type
, wxObject
* obj
,
10998 int rowOrCol
, int x
, int y
,
10999 bool control
, bool shift
, bool alt
, bool meta
)
11000 : wxNotifyEvent( type
, id
),
11001 wxKeyboardState(control
, shift
, alt
, meta
)
11003 Init(rowOrCol
, x
, y
);
11005 SetEventObject(obj
);
11009 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent
, wxNotifyEvent
)
11011 wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id
, wxEventType type
, wxObject
* obj
,
11012 const wxGridCellCoords
& topLeft
,
11013 const wxGridCellCoords
& bottomRight
,
11014 bool sel
, bool control
,
11015 bool shift
, bool alt
, bool meta
)
11016 : wxNotifyEvent( type
, id
),
11017 wxKeyboardState(control
, shift
, alt
, meta
)
11019 Init(topLeft
, bottomRight
, sel
);
11021 SetEventObject(obj
);
11025 IMPLEMENT_DYNAMIC_CLASS(wxGridEditorCreatedEvent
, wxCommandEvent
)
11027 wxGridEditorCreatedEvent::wxGridEditorCreatedEvent(int id
, wxEventType type
,
11028 wxObject
* obj
, int row
,
11029 int col
, wxControl
* ctrl
)
11030 : wxCommandEvent(type
, id
)
11032 SetEventObject(obj
);
11038 #endif // wxUSE_GRID