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()
19 // For compilers that support precompilation, includes "wx/wx.h".
20 #include "wx/wxprec.h"
32 #include "wx/dcclient.h"
33 #include "wx/settings.h"
35 #include "wx/textctrl.h"
36 #include "wx/checkbox.h"
37 #include "wx/combobox.h"
38 #include "wx/valtext.h"
41 #include "wx/listbox.h"
44 #include "wx/textfile.h"
45 #include "wx/spinctrl.h"
46 #include "wx/tokenzr.h"
47 #include "wx/renderer.h"
49 #include "wx/generic/gridsel.h"
51 const wxChar wxGridNameStr
[] = wxT("grid");
53 #if defined(__WXMOTIF__)
54 #define WXUNUSED_MOTIF(identifier) WXUNUSED(identifier)
56 #define WXUNUSED_MOTIF(identifier) identifier
59 #if defined(__WXGTK__)
60 #define WXUNUSED_GTK(identifier) WXUNUSED(identifier)
62 #define WXUNUSED_GTK(identifier) identifier
65 // Required for wxIs... functions
68 // ----------------------------------------------------------------------------
70 // ----------------------------------------------------------------------------
72 WX_DEFINE_ARRAY_WITH_DECL_PTR(wxGridCellAttr
*, wxArrayAttrs
,
73 class WXDLLIMPEXP_ADV
);
75 struct wxGridCellWithAttr
77 wxGridCellWithAttr(int row
, int col
, wxGridCellAttr
*attr_
)
78 : coords(row
, col
), attr(attr_
)
83 wxGridCellWithAttr(const wxGridCellWithAttr
& other
)
84 : coords(other
.coords
),
90 wxGridCellWithAttr
& operator=(const wxGridCellWithAttr
& other
)
92 coords
= other
.coords
;
93 if (attr
!= other
.attr
)
102 void ChangeAttr(wxGridCellAttr
* new_attr
)
104 if (attr
!= new_attr
)
106 // "Delete" (i.e. DecRef) the old attribute.
109 // Take ownership of the new attribute, i.e. no IncRef.
113 ~wxGridCellWithAttr()
118 wxGridCellCoords coords
;
119 wxGridCellAttr
*attr
;
122 WX_DECLARE_OBJARRAY_WITH_DECL(wxGridCellWithAttr
, wxGridCellWithAttrArray
,
123 class WXDLLIMPEXP_ADV
);
125 #include "wx/arrimpl.cpp"
127 WX_DEFINE_OBJARRAY(wxGridCellCoordsArray
)
128 WX_DEFINE_OBJARRAY(wxGridCellWithAttrArray
)
130 // ----------------------------------------------------------------------------
132 // ----------------------------------------------------------------------------
134 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_LEFT_CLICK
)
135 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_RIGHT_CLICK
)
136 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_LEFT_DCLICK
)
137 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_RIGHT_DCLICK
)
138 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_BEGIN_DRAG
)
139 DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_LEFT_CLICK
)
140 DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_RIGHT_CLICK
)
141 DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_LEFT_DCLICK
)
142 DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_RIGHT_DCLICK
)
143 DEFINE_EVENT_TYPE(wxEVT_GRID_ROW_SIZE
)
144 DEFINE_EVENT_TYPE(wxEVT_GRID_COL_SIZE
)
145 DEFINE_EVENT_TYPE(wxEVT_GRID_COL_MOVE
)
146 DEFINE_EVENT_TYPE(wxEVT_GRID_RANGE_SELECT
)
147 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_CHANGE
)
148 DEFINE_EVENT_TYPE(wxEVT_GRID_SELECT_CELL
)
149 DEFINE_EVENT_TYPE(wxEVT_GRID_EDITOR_SHOWN
)
150 DEFINE_EVENT_TYPE(wxEVT_GRID_EDITOR_HIDDEN
)
151 DEFINE_EVENT_TYPE(wxEVT_GRID_EDITOR_CREATED
)
153 // ----------------------------------------------------------------------------
155 // ----------------------------------------------------------------------------
157 // common base class for various grid subwindows
158 class WXDLLIMPEXP_ADV wxGridSubwindow
: public wxWindow
161 wxGridSubwindow() { m_owner
= NULL
; }
162 wxGridSubwindow(wxGrid
*owner
,
166 int additionalStyle
= 0,
167 const wxString
& name
= wxPanelNameStr
)
168 : wxWindow(owner
, id
, pos
, size
,
169 wxBORDER_NONE
| additionalStyle
,
175 virtual bool AcceptsFocus() const { return false; }
177 wxGrid
*GetOwner() { return m_owner
; }
180 void OnMouseCaptureLost(wxMouseCaptureLostEvent
& event
);
184 DECLARE_EVENT_TABLE()
185 DECLARE_NO_COPY_CLASS(wxGridSubwindow
)
188 class WXDLLIMPEXP_ADV wxGridRowLabelWindow
: public wxGridSubwindow
191 wxGridRowLabelWindow() { }
192 wxGridRowLabelWindow( wxGrid
*parent
, wxWindowID id
,
193 const wxPoint
&pos
, const wxSize
&size
);
196 void OnPaint( wxPaintEvent
& event
);
197 void OnMouseEvent( wxMouseEvent
& event
);
198 void OnMouseWheel( wxMouseEvent
& event
);
200 DECLARE_DYNAMIC_CLASS(wxGridRowLabelWindow
)
201 DECLARE_EVENT_TABLE()
202 DECLARE_NO_COPY_CLASS(wxGridRowLabelWindow
)
206 class WXDLLIMPEXP_ADV wxGridColLabelWindow
: public wxGridSubwindow
209 wxGridColLabelWindow() { }
210 wxGridColLabelWindow( wxGrid
*parent
, wxWindowID id
,
211 const wxPoint
&pos
, const wxSize
&size
);
214 void OnPaint( wxPaintEvent
& event
);
215 void OnMouseEvent( wxMouseEvent
& event
);
216 void OnMouseWheel( wxMouseEvent
& event
);
218 DECLARE_DYNAMIC_CLASS(wxGridColLabelWindow
)
219 DECLARE_EVENT_TABLE()
220 DECLARE_NO_COPY_CLASS(wxGridColLabelWindow
)
224 class WXDLLIMPEXP_ADV wxGridCornerLabelWindow
: public wxGridSubwindow
227 wxGridCornerLabelWindow() { }
228 wxGridCornerLabelWindow( wxGrid
*parent
, wxWindowID id
,
229 const wxPoint
&pos
, const wxSize
&size
);
232 void OnMouseEvent( wxMouseEvent
& event
);
233 void OnMouseWheel( wxMouseEvent
& event
);
234 void OnPaint( wxPaintEvent
& event
);
236 DECLARE_DYNAMIC_CLASS(wxGridCornerLabelWindow
)
237 DECLARE_EVENT_TABLE()
238 DECLARE_NO_COPY_CLASS(wxGridCornerLabelWindow
)
241 class WXDLLIMPEXP_ADV wxGridWindow
: public wxGridSubwindow
246 m_rowLabelWin
= NULL
;
247 m_colLabelWin
= NULL
;
250 wxGridWindow( wxGrid
*parent
,
251 wxGridRowLabelWindow
*rowLblWin
,
252 wxGridColLabelWindow
*colLblWin
,
253 wxWindowID id
, const wxPoint
&pos
, const wxSize
&size
);
255 void ScrollWindow( int dx
, int dy
, const wxRect
*rect
);
257 virtual bool AcceptsFocus() const { return true; }
260 wxGridRowLabelWindow
*m_rowLabelWin
;
261 wxGridColLabelWindow
*m_colLabelWin
;
263 void OnPaint( wxPaintEvent
&event
);
264 void OnMouseWheel( wxMouseEvent
& event
);
265 void OnMouseEvent( wxMouseEvent
& event
);
266 void OnKeyDown( wxKeyEvent
& );
267 void OnKeyUp( wxKeyEvent
& );
268 void OnChar( wxKeyEvent
& );
269 void OnEraseBackground( wxEraseEvent
& );
270 void OnFocus( wxFocusEvent
& );
272 DECLARE_DYNAMIC_CLASS(wxGridWindow
)
273 DECLARE_EVENT_TABLE()
274 DECLARE_NO_COPY_CLASS(wxGridWindow
)
278 class wxGridCellEditorEvtHandler
: public wxEvtHandler
281 wxGridCellEditorEvtHandler(wxGrid
* grid
, wxGridCellEditor
* editor
)
288 void OnKillFocus(wxFocusEvent
& event
);
289 void OnKeyDown(wxKeyEvent
& event
);
290 void OnChar(wxKeyEvent
& event
);
292 void SetInSetFocus(bool inSetFocus
) { m_inSetFocus
= inSetFocus
; }
296 wxGridCellEditor
*m_editor
;
298 // Work around the fact that a focus kill event can be sent to
299 // a combobox within a set focus event.
302 DECLARE_EVENT_TABLE()
303 DECLARE_DYNAMIC_CLASS(wxGridCellEditorEvtHandler
)
304 DECLARE_NO_COPY_CLASS(wxGridCellEditorEvtHandler
)
308 IMPLEMENT_ABSTRACT_CLASS(wxGridCellEditorEvtHandler
, wxEvtHandler
)
310 BEGIN_EVENT_TABLE( wxGridCellEditorEvtHandler
, wxEvtHandler
)
311 EVT_KILL_FOCUS( wxGridCellEditorEvtHandler::OnKillFocus
)
312 EVT_KEY_DOWN( wxGridCellEditorEvtHandler::OnKeyDown
)
313 EVT_CHAR( wxGridCellEditorEvtHandler::OnChar
)
317 // ----------------------------------------------------------------------------
318 // the internal data representation used by wxGridCellAttrProvider
319 // ----------------------------------------------------------------------------
321 // this class stores attributes set for cells
322 class WXDLLIMPEXP_ADV wxGridCellAttrData
325 void SetAttr(wxGridCellAttr
*attr
, int row
, int col
);
326 wxGridCellAttr
*GetAttr(int row
, int col
) const;
327 void UpdateAttrRows( size_t pos
, int numRows
);
328 void UpdateAttrCols( size_t pos
, int numCols
);
331 // searches for the attr for given cell, returns wxNOT_FOUND if not found
332 int FindIndex(int row
, int col
) const;
334 wxGridCellWithAttrArray m_attrs
;
337 // this class stores attributes set for rows or columns
338 class WXDLLIMPEXP_ADV wxGridRowOrColAttrData
341 // empty ctor to suppress warnings
342 wxGridRowOrColAttrData() {}
343 ~wxGridRowOrColAttrData();
345 void SetAttr(wxGridCellAttr
*attr
, int rowOrCol
);
346 wxGridCellAttr
*GetAttr(int rowOrCol
) const;
347 void UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
);
350 wxArrayInt m_rowsOrCols
;
351 wxArrayAttrs m_attrs
;
354 // NB: this is just a wrapper around 3 objects: one which stores cell
355 // attributes, and 2 others for row/col ones
356 class WXDLLIMPEXP_ADV wxGridCellAttrProviderData
359 wxGridCellAttrData m_cellAttrs
;
360 wxGridRowOrColAttrData m_rowAttrs
,
365 // ----------------------------------------------------------------------------
366 // data structures used for the data type registry
367 // ----------------------------------------------------------------------------
369 struct wxGridDataTypeInfo
371 wxGridDataTypeInfo(const wxString
& typeName
,
372 wxGridCellRenderer
* renderer
,
373 wxGridCellEditor
* editor
)
374 : m_typeName(typeName
), m_renderer(renderer
), m_editor(editor
)
377 ~wxGridDataTypeInfo()
379 wxSafeDecRef(m_renderer
);
380 wxSafeDecRef(m_editor
);
384 wxGridCellRenderer
* m_renderer
;
385 wxGridCellEditor
* m_editor
;
387 DECLARE_NO_COPY_CLASS(wxGridDataTypeInfo
)
391 WX_DEFINE_ARRAY_WITH_DECL_PTR(wxGridDataTypeInfo
*, wxGridDataTypeInfoArray
,
392 class WXDLLIMPEXP_ADV
);
395 class WXDLLIMPEXP_ADV wxGridTypeRegistry
398 wxGridTypeRegistry() {}
399 ~wxGridTypeRegistry();
401 void RegisterDataType(const wxString
& typeName
,
402 wxGridCellRenderer
* renderer
,
403 wxGridCellEditor
* editor
);
405 // find one of already registered data types
406 int FindRegisteredDataType(const wxString
& typeName
);
408 // try to FindRegisteredDataType(), if this fails and typeName is one of
409 // standard typenames, register it and return its index
410 int FindDataType(const wxString
& typeName
);
412 // try to FindDataType(), if it fails see if it is not one of already
413 // registered data types with some params in which case clone the
414 // registered data type and set params for it
415 int FindOrCloneDataType(const wxString
& typeName
);
417 wxGridCellRenderer
* GetRenderer(int index
);
418 wxGridCellEditor
* GetEditor(int index
);
421 wxGridDataTypeInfoArray m_typeinfo
;
424 // ----------------------------------------------------------------------------
425 // operations classes abstracting the difference between operating on rows and
427 // ----------------------------------------------------------------------------
429 // This class allows to write a function only once because by using its methods
430 // it will apply to both columns and rows.
432 // This is an abstract interface definition, the two concrete implementations
433 // below should be used when working with rows and columns respectively.
434 class wxGridOperations
437 // Returns the operations in the other direction, i.e. wxGridRowOperations
438 // if this object is a wxGridColumnOperations and vice versa.
439 virtual wxGridOperations
& Dual() const = 0;
441 // Return the number of rows or columns.
442 virtual int GetNumberOfLines(const wxGrid
*grid
) const = 0;
444 // Return the selection mode which allows selecting rows or columns.
445 virtual wxGrid::wxGridSelectionModes
GetSelectionMode() const = 0;
447 // Make a wxGridCellCoords from the given components: thisDir is row or
448 // column and otherDir is column or row
449 virtual wxGridCellCoords
MakeCoords(int thisDir
, int otherDir
) const = 0;
451 // Calculate the scrolled position of the given abscissa or ordinate.
452 virtual int CalcScrolledPosition(wxGrid
*grid
, int pos
) const = 0;
454 // Selects the horizontal or vertical component from the given object.
455 virtual int Select(const wxPoint
& pt
) const = 0;
456 virtual int Select(const wxSize
& sz
) const = 0;
457 virtual int Select(const wxRect
& r
) const = 0;
458 virtual int& Select(wxRect
& r
) const = 0;
460 // Returns width or height of the rectangle
461 virtual int& SelectSize(wxRect
& r
) const = 0;
463 // Make a wxSize such that Select() applied to it returns first component
464 virtual wxSize
MakeSize(int first
, int second
) const = 0;
467 // Draws a line parallel to the row or column, i.e. horizontal or vertical:
468 // pos is the vertical or horizontal position of the line and start and end
469 // are the coordinates of the line extremities in the other direction
471 DrawParallelLine(wxDC
& dc
, int start
, int end
, int pos
) const = 0;
474 // Return the row or column at the given pixel coordinate.
475 virtual int PosToLine(wxGrid
*grid
, int pos
, bool clip
= false) const = 0;
477 // Get the top/left position, in pixels, of the given row or column
478 virtual int GetLineStartPos(const wxGrid
*grid
, int line
) const = 0;
480 // Get wxGrid::m_rowBottoms/m_colRights array
481 virtual const wxArrayInt
& GetLineEnds(const wxGrid
*grid
) const = 0;
483 // Get default height row height or column width
484 virtual int GetDefaultLineSize(const wxGrid
*grid
) const = 0;
486 // Return the minimal acceptable row height or column width
487 virtual int GetMinimalAcceptableLineSize(const wxGrid
*grid
) const = 0;
489 // Return the minimal row height or column width
490 virtual int GetMinimalLineSize(const wxGrid
*grid
, int line
) const = 0;
492 // Set the row height or column width
493 virtual void SetLineSize(wxGrid
*grid
, int line
, int size
) const = 0;
496 // Return the index of the line at the given position
498 // NB: currently this is always identity for the rows as reordering is only
499 // implemented for the lines
500 virtual int GetLineAt(const wxGrid
*grid
, int line
) const = 0;
503 // Get the row or column label window
504 virtual wxWindow
*GetHeaderWindow(wxGrid
*grid
) const = 0;
506 // Get the width or height of the row or column label window
507 virtual int GetHeaderWindowSize(wxGrid
*grid
) const = 0;
510 class wxGridRowOperations
: public wxGridOperations
513 virtual wxGridOperations
& Dual() const;
515 virtual int GetNumberOfLines(const wxGrid
*grid
) const
516 { return grid
->GetNumberRows(); }
518 virtual wxGrid::wxGridSelectionModes
GetSelectionMode() const
519 { return wxGrid::wxGridSelectRows
; }
521 virtual wxGridCellCoords
MakeCoords(int thisDir
, int otherDir
) const
522 { return wxGridCellCoords(thisDir
, otherDir
); }
524 virtual int CalcScrolledPosition(wxGrid
*grid
, int pos
) const
525 { return grid
->CalcScrolledPosition(wxPoint(pos
, 0)).x
; }
527 virtual int Select(const wxPoint
& pt
) const { return pt
.x
; }
528 virtual int Select(const wxSize
& sz
) const { return sz
.x
; }
529 virtual int Select(const wxRect
& r
) const { return r
.x
; }
530 virtual int& Select(wxRect
& r
) const { return r
.x
; }
531 virtual int& SelectSize(wxRect
& r
) const { return r
.width
; }
532 virtual wxSize
MakeSize(int first
, int second
) const
533 { return wxSize(first
, second
); }
535 virtual void DrawParallelLine(wxDC
& dc
, int start
, int end
, int pos
) const
536 { dc
.DrawLine(start
, pos
, end
, pos
); }
538 virtual int PosToLine(wxGrid
*grid
, int pos
, bool clip
= false) const
539 { return grid
->YToRow(pos
, clip
); }
540 virtual int GetLineStartPos(const wxGrid
*grid
, int line
) const
541 { return grid
->GetRowTop(line
); }
542 virtual const wxArrayInt
& GetLineEnds(const wxGrid
*grid
) const
543 { return grid
->m_rowBottoms
; }
544 virtual int GetDefaultLineSize(const wxGrid
*grid
) const
545 { return grid
->GetDefaultRowSize(); }
546 virtual int GetMinimalAcceptableLineSize(const wxGrid
*grid
) const
547 { return grid
->GetRowMinimalAcceptableHeight(); }
548 virtual int GetMinimalLineSize(const wxGrid
*grid
, int line
) const
549 { return grid
->GetRowMinimalHeight(line
); }
550 virtual void SetLineSize(wxGrid
*grid
, int line
, int size
) const
551 { grid
->SetRowSize(line
, size
); }
553 virtual int GetLineAt(const wxGrid
* WXUNUSED(grid
), int line
) const
554 { return line
; } // TODO: implement row reordering
556 virtual wxWindow
*GetHeaderWindow(wxGrid
*grid
) const
557 { return grid
->GetGridRowLabelWindow(); }
558 virtual int GetHeaderWindowSize(wxGrid
*grid
) const
559 { return grid
->GetRowLabelSize(); }
562 class wxGridColumnOperations
: public wxGridOperations
565 virtual wxGridOperations
& Dual() const;
567 virtual int GetNumberOfLines(const wxGrid
*grid
) const
568 { return grid
->GetNumberCols(); }
570 virtual wxGrid::wxGridSelectionModes
GetSelectionMode() const
571 { return wxGrid::wxGridSelectColumns
; }
573 virtual wxGridCellCoords
MakeCoords(int thisDir
, int otherDir
) const
574 { return wxGridCellCoords(otherDir
, thisDir
); }
576 virtual int CalcScrolledPosition(wxGrid
*grid
, int pos
) const
577 { return grid
->CalcScrolledPosition(wxPoint(0, pos
)).y
; }
579 virtual int Select(const wxPoint
& pt
) const { return pt
.y
; }
580 virtual int Select(const wxSize
& sz
) const { return sz
.y
; }
581 virtual int Select(const wxRect
& r
) const { return r
.y
; }
582 virtual int& Select(wxRect
& r
) const { return r
.y
; }
583 virtual int& SelectSize(wxRect
& r
) const { return r
.height
; }
584 virtual wxSize
MakeSize(int first
, int second
) const
585 { return wxSize(second
, first
); }
587 virtual void DrawParallelLine(wxDC
& dc
, int start
, int end
, int pos
) const
588 { dc
.DrawLine(pos
, start
, pos
, end
); }
590 virtual int PosToLine(wxGrid
*grid
, int pos
, bool clip
= false) const
591 { return grid
->XToCol(pos
, clip
); }
592 virtual int GetLineStartPos(const wxGrid
*grid
, int line
) const
593 { return grid
->GetColLeft(line
); }
594 virtual const wxArrayInt
& GetLineEnds(const wxGrid
*grid
) const
595 { return grid
->m_colRights
; }
596 virtual int GetDefaultLineSize(const wxGrid
*grid
) const
597 { return grid
->GetDefaultColSize(); }
598 virtual int GetMinimalAcceptableLineSize(const wxGrid
*grid
) const
599 { return grid
->GetColMinimalAcceptableWidth(); }
600 virtual int GetMinimalLineSize(const wxGrid
*grid
, int line
) const
601 { return grid
->GetColMinimalWidth(line
); }
602 virtual void SetLineSize(wxGrid
*grid
, int line
, int size
) const
603 { grid
->SetColSize(line
, size
); }
605 virtual int GetLineAt(const wxGrid
*grid
, int line
) const
606 { return grid
->GetColAt(line
); }
608 virtual wxWindow
*GetHeaderWindow(wxGrid
*grid
) const
609 { return grid
->GetGridColLabelWindow(); }
610 virtual int GetHeaderWindowSize(wxGrid
*grid
) const
611 { return grid
->GetColLabelSize(); }
614 wxGridOperations
& wxGridRowOperations::Dual() const
616 static wxGridColumnOperations s_colOper
;
621 wxGridOperations
& wxGridColumnOperations::Dual() const
623 static wxGridRowOperations s_rowOper
;
629 // ----------------------------------------------------------------------------
631 // ----------------------------------------------------------------------------
633 //#define DEBUG_ATTR_CACHE
634 #ifdef DEBUG_ATTR_CACHE
635 static size_t gs_nAttrCacheHits
= 0;
636 static size_t gs_nAttrCacheMisses
= 0;
639 // ----------------------------------------------------------------------------
641 // ----------------------------------------------------------------------------
643 wxGridCellCoords
wxGridNoCellCoords( -1, -1 );
644 wxRect
wxGridNoCellRect( -1, -1, -1, -1 );
647 static const size_t GRID_SCROLL_LINE_X
= 15;
648 static const size_t GRID_SCROLL_LINE_Y
= GRID_SCROLL_LINE_X
;
650 // the size of hash tables used a bit everywhere (the max number of elements
651 // in these hash tables is the number of rows/columns)
652 static const int GRID_HASH_SIZE
= 100;
654 // ----------------------------------------------------------------------------
656 // ----------------------------------------------------------------------------
661 // ensure that first is less or equal to second, swapping the values if
663 void EnsureFirstLessThanSecond(int& first
, int& second
)
665 if ( first
> second
)
666 wxSwap(first
, second
);
669 } // anonymous namespace
671 // ============================================================================
673 // ============================================================================
675 // ----------------------------------------------------------------------------
677 // ----------------------------------------------------------------------------
679 wxGridCellEditor::wxGridCellEditor()
685 wxGridCellEditor::~wxGridCellEditor()
690 void wxGridCellEditor::Create(wxWindow
* WXUNUSED(parent
),
691 wxWindowID
WXUNUSED(id
),
692 wxEvtHandler
* evtHandler
)
695 m_control
->PushEventHandler(evtHandler
);
698 void wxGridCellEditor::PaintBackground(const wxRect
& rectCell
,
699 wxGridCellAttr
*attr
)
701 // erase the background because we might not fill the cell
702 wxClientDC
dc(m_control
->GetParent());
703 wxGridWindow
* gridWindow
= wxDynamicCast(m_control
->GetParent(), wxGridWindow
);
705 gridWindow
->GetOwner()->PrepareDC(dc
);
707 dc
.SetPen(*wxTRANSPARENT_PEN
);
708 dc
.SetBrush(wxBrush(attr
->GetBackgroundColour()));
709 dc
.DrawRectangle(rectCell
);
711 // redraw the control we just painted over
712 m_control
->Refresh();
715 void wxGridCellEditor::Destroy()
719 m_control
->PopEventHandler( true /* delete it*/ );
721 m_control
->Destroy();
726 void wxGridCellEditor::Show(bool show
, wxGridCellAttr
*attr
)
728 wxASSERT_MSG(m_control
, wxT("The wxGridCellEditor must be created first!"));
730 m_control
->Show(show
);
734 // set the colours/fonts if we have any
737 m_colFgOld
= m_control
->GetForegroundColour();
738 m_control
->SetForegroundColour(attr
->GetTextColour());
740 m_colBgOld
= m_control
->GetBackgroundColour();
741 m_control
->SetBackgroundColour(attr
->GetBackgroundColour());
743 // Workaround for GTK+1 font setting problem on some platforms
744 #if !defined(__WXGTK__) || defined(__WXGTK20__)
745 m_fontOld
= m_control
->GetFont();
746 m_control
->SetFont(attr
->GetFont());
749 // can't do anything more in the base class version, the other
750 // attributes may only be used by the derived classes
755 // restore the standard colours fonts
756 if ( m_colFgOld
.Ok() )
758 m_control
->SetForegroundColour(m_colFgOld
);
759 m_colFgOld
= wxNullColour
;
762 if ( m_colBgOld
.Ok() )
764 m_control
->SetBackgroundColour(m_colBgOld
);
765 m_colBgOld
= wxNullColour
;
768 // Workaround for GTK+1 font setting problem on some platforms
769 #if !defined(__WXGTK__) || defined(__WXGTK20__)
770 if ( m_fontOld
.Ok() )
772 m_control
->SetFont(m_fontOld
);
773 m_fontOld
= wxNullFont
;
779 void wxGridCellEditor::SetSize(const wxRect
& rect
)
781 wxASSERT_MSG(m_control
, wxT("The wxGridCellEditor must be created first!"));
783 m_control
->SetSize(rect
, wxSIZE_ALLOW_MINUS_ONE
);
786 void wxGridCellEditor::HandleReturn(wxKeyEvent
& event
)
791 bool wxGridCellEditor::IsAcceptedKey(wxKeyEvent
& event
)
793 bool ctrl
= event
.ControlDown();
794 bool alt
= event
.AltDown();
797 // On the Mac the Alt key is more like shift and is used for entry of
798 // valid characters, so check for Ctrl and Meta instead.
799 alt
= event
.MetaDown();
802 // Assume it's not a valid char if ctrl or alt is down, but if both are
803 // down then it may be because of an AltGr key combination, so let them
804 // through in that case.
805 if ((ctrl
|| alt
) && !(ctrl
&& alt
))
812 // If it's a F-Key or other special key then it shouldn't start the
814 if (event
.GetKeyCode() >= WXK_START
)
818 // if the unicode key code is not really a unicode character (it may
819 // be a function key or etc., the platforms appear to always give us a
820 // small value in this case) then fallback to the ASCII key code but
821 // don't do anything for function keys or etc.
822 key
= event
.GetUnicodeKey();
825 key
= event
.GetKeyCode();
826 keyOk
= (key
<= 127);
829 key
= event
.GetKeyCode();
830 keyOk
= (key
<= 255);
836 void wxGridCellEditor::StartingKey(wxKeyEvent
& event
)
841 void wxGridCellEditor::StartingClick()
847 // ----------------------------------------------------------------------------
848 // wxGridCellTextEditor
849 // ----------------------------------------------------------------------------
851 wxGridCellTextEditor::wxGridCellTextEditor()
856 void wxGridCellTextEditor::Create(wxWindow
* parent
,
858 wxEvtHandler
* evtHandler
)
860 DoCreate(parent
, id
, evtHandler
);
863 void wxGridCellTextEditor::DoCreate(wxWindow
* parent
,
865 wxEvtHandler
* evtHandler
,
868 style
|= wxTE_PROCESS_ENTER
| wxTE_PROCESS_TAB
| wxNO_BORDER
;
870 m_control
= new wxTextCtrl(parent
, id
, wxEmptyString
,
871 wxDefaultPosition
, wxDefaultSize
,
874 // set max length allowed in the textctrl, if the parameter was set
875 if ( m_maxChars
!= 0 )
877 Text()->SetMaxLength(m_maxChars
);
880 wxGridCellEditor::Create(parent
, id
, evtHandler
);
883 void wxGridCellTextEditor::PaintBackground(const wxRect
& WXUNUSED(rectCell
),
884 wxGridCellAttr
* WXUNUSED(attr
))
886 // as we fill the entire client area,
887 // don't do anything here to minimize flicker
890 void wxGridCellTextEditor::SetSize(const wxRect
& rectOrig
)
892 wxRect
rect(rectOrig
);
894 // Make the edit control large enough to allow for internal margins
896 // TODO: remove this if the text ctrl sizing is improved esp. for unix
898 #if defined(__WXGTK__)
906 #elif defined(__WXMSW__)
920 int extra_x
= ( rect
.x
> 2 ) ? 2 : 1;
921 int extra_y
= ( rect
.y
> 2 ) ? 2 : 1;
923 #if defined(__WXMOTIF__)
928 rect
.SetLeft( wxMax(0, rect
.x
- extra_x
) );
929 rect
.SetTop( wxMax(0, rect
.y
- extra_y
) );
930 rect
.SetRight( rect
.GetRight() + 2 * extra_x
);
931 rect
.SetBottom( rect
.GetBottom() + 2 * extra_y
);
934 wxGridCellEditor::SetSize(rect
);
937 void wxGridCellTextEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
939 wxASSERT_MSG(m_control
, wxT("The wxGridCellEditor must be created first!"));
941 m_startValue
= grid
->GetTable()->GetValue(row
, col
);
943 DoBeginEdit(m_startValue
);
946 void wxGridCellTextEditor::DoBeginEdit(const wxString
& startValue
)
948 Text()->SetValue(startValue
);
949 Text()->SetInsertionPointEnd();
950 Text()->SetSelection(-1, -1);
954 bool wxGridCellTextEditor::EndEdit(int row
, int col
, wxGrid
* grid
)
956 wxASSERT_MSG(m_control
, wxT("The wxGridCellEditor must be created first!"));
958 bool changed
= false;
959 wxString value
= Text()->GetValue();
960 if (value
!= m_startValue
)
964 grid
->GetTable()->SetValue(row
, col
, value
);
966 m_startValue
= wxEmptyString
;
968 // No point in setting the text of the hidden control
969 //Text()->SetValue(m_startValue);
974 void wxGridCellTextEditor::Reset()
976 wxASSERT_MSG(m_control
, wxT("The wxGridCellEditor must be created first!"));
978 DoReset(m_startValue
);
981 void wxGridCellTextEditor::DoReset(const wxString
& startValue
)
983 Text()->SetValue(startValue
);
984 Text()->SetInsertionPointEnd();
987 bool wxGridCellTextEditor::IsAcceptedKey(wxKeyEvent
& event
)
989 return wxGridCellEditor::IsAcceptedKey(event
);
992 void wxGridCellTextEditor::StartingKey(wxKeyEvent
& event
)
994 // Since this is now happening in the EVT_CHAR event EmulateKeyPress is no
995 // longer an appropriate way to get the character into the text control.
996 // Do it ourselves instead. We know that if we get this far that we have
997 // a valid character, so not a whole lot of testing needs to be done.
999 wxTextCtrl
* tc
= Text();
1004 ch
= event
.GetUnicodeKey();
1006 ch
= (wxChar
)event
.GetKeyCode();
1008 ch
= (wxChar
)event
.GetKeyCode();
1014 // delete the character at the cursor
1015 pos
= tc
->GetInsertionPoint();
1016 if (pos
< tc
->GetLastPosition())
1017 tc
->Remove(pos
, pos
+ 1);
1021 // delete the character before the cursor
1022 pos
= tc
->GetInsertionPoint();
1024 tc
->Remove(pos
- 1, pos
);
1033 void wxGridCellTextEditor::HandleReturn( wxKeyEvent
&
1034 WXUNUSED_GTK(WXUNUSED_MOTIF(event
)) )
1036 #if defined(__WXMOTIF__) || defined(__WXGTK__)
1037 // wxMotif needs a little extra help...
1038 size_t pos
= (size_t)( Text()->GetInsertionPoint() );
1039 wxString
s( Text()->GetValue() );
1040 s
= s
.Left(pos
) + wxT("\n") + s
.Mid(pos
);
1041 Text()->SetValue(s
);
1042 Text()->SetInsertionPoint( pos
);
1044 // the other ports can handle a Return key press
1050 void wxGridCellTextEditor::SetParameters(const wxString
& params
)
1060 if ( params
.ToLong(&tmp
) )
1062 m_maxChars
= (size_t)tmp
;
1066 wxLogDebug( _T("Invalid wxGridCellTextEditor parameter string '%s' ignored"), params
.c_str() );
1071 // return the value in the text control
1072 wxString
wxGridCellTextEditor::GetValue() const
1074 return Text()->GetValue();
1077 // ----------------------------------------------------------------------------
1078 // wxGridCellNumberEditor
1079 // ----------------------------------------------------------------------------
1081 wxGridCellNumberEditor::wxGridCellNumberEditor(int min
, int max
)
1087 void wxGridCellNumberEditor::Create(wxWindow
* parent
,
1089 wxEvtHandler
* evtHandler
)
1094 // create a spin ctrl
1095 m_control
= new wxSpinCtrl(parent
, wxID_ANY
, wxEmptyString
,
1096 wxDefaultPosition
, wxDefaultSize
,
1100 wxGridCellEditor::Create(parent
, id
, evtHandler
);
1105 // just a text control
1106 wxGridCellTextEditor::Create(parent
, id
, evtHandler
);
1108 #if wxUSE_VALIDATORS
1109 Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC
));
1114 void wxGridCellNumberEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1116 // first get the value
1117 wxGridTableBase
*table
= grid
->GetTable();
1118 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
1120 m_valueOld
= table
->GetValueAsLong(row
, col
);
1125 wxString sValue
= table
->GetValue(row
, col
);
1126 if (! sValue
.ToLong(&m_valueOld
) && ! sValue
.empty())
1128 wxFAIL_MSG( _T("this cell doesn't have numeric value") );
1136 Spin()->SetValue((int)m_valueOld
);
1142 DoBeginEdit(GetString());
1146 bool wxGridCellNumberEditor::EndEdit(int row
, int col
,
1155 value
= Spin()->GetValue();
1156 if ( value
== m_valueOld
)
1159 text
.Printf(wxT("%ld"), value
);
1161 else // using unconstrained input
1162 #endif // wxUSE_SPINCTRL
1164 const wxString
textOld(grid
->GetCellValue(row
, col
));
1165 text
= Text()->GetValue();
1168 if ( textOld
.empty() )
1171 else // non-empty text now (maybe 0)
1173 if ( !text
.ToLong(&value
) )
1176 // if value == m_valueOld == 0 but old text was "" and new one is
1177 // "0" something still did change
1178 if ( value
== m_valueOld
&& (value
|| !textOld
.empty()) )
1183 wxGridTableBase
* const table
= grid
->GetTable();
1184 if ( table
->CanSetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
1185 table
->SetValueAsLong(row
, col
, value
);
1187 table
->SetValue(row
, col
, text
);
1192 void wxGridCellNumberEditor::Reset()
1197 Spin()->SetValue((int)m_valueOld
);
1202 DoReset(GetString());
1206 bool wxGridCellNumberEditor::IsAcceptedKey(wxKeyEvent
& event
)
1208 if ( wxGridCellEditor::IsAcceptedKey(event
) )
1210 int keycode
= event
.GetKeyCode();
1211 if ( (keycode
< 128) &&
1212 (wxIsdigit(keycode
) || keycode
== '+' || keycode
== '-'))
1221 void wxGridCellNumberEditor::StartingKey(wxKeyEvent
& event
)
1223 int keycode
= event
.GetKeyCode();
1226 if ( wxIsdigit(keycode
) || keycode
== '+' || keycode
== '-')
1228 wxGridCellTextEditor::StartingKey(event
);
1230 // skip Skip() below
1237 if ( wxIsdigit(keycode
) )
1239 wxSpinCtrl
* spin
= (wxSpinCtrl
*)m_control
;
1240 spin
->SetValue(keycode
- '0');
1241 spin
->SetSelection(1,1);
1250 void wxGridCellNumberEditor::SetParameters(const wxString
& params
)
1261 if ( params
.BeforeFirst(_T(',')).ToLong(&tmp
) )
1265 if ( params
.AfterFirst(_T(',')).ToLong(&tmp
) )
1269 // skip the error message below
1274 wxLogDebug(_T("Invalid wxGridCellNumberEditor parameter string '%s' ignored"), params
.c_str());
1278 // return the value in the spin control if it is there (the text control otherwise)
1279 wxString
wxGridCellNumberEditor::GetValue() const
1286 long value
= Spin()->GetValue();
1287 s
.Printf(wxT("%ld"), value
);
1292 s
= Text()->GetValue();
1298 // ----------------------------------------------------------------------------
1299 // wxGridCellFloatEditor
1300 // ----------------------------------------------------------------------------
1302 wxGridCellFloatEditor::wxGridCellFloatEditor(int width
, int precision
)
1305 m_precision
= precision
;
1308 void wxGridCellFloatEditor::Create(wxWindow
* parent
,
1310 wxEvtHandler
* evtHandler
)
1312 wxGridCellTextEditor::Create(parent
, id
, evtHandler
);
1314 #if wxUSE_VALIDATORS
1315 Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC
));
1319 void wxGridCellFloatEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1321 // first get the value
1322 wxGridTableBase
* const table
= grid
->GetTable();
1323 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
1325 m_valueOld
= table
->GetValueAsDouble(row
, col
);
1331 const wxString value
= table
->GetValue(row
, col
);
1332 if ( !value
.empty() )
1334 if ( !value
.ToDouble(&m_valueOld
) )
1336 wxFAIL_MSG( _T("this cell doesn't have float value") );
1342 DoBeginEdit(GetString());
1345 bool wxGridCellFloatEditor::EndEdit(int row
, int col
, wxGrid
* grid
)
1347 const wxString
text(Text()->GetValue()),
1348 textOld(grid
->GetCellValue(row
, col
));
1351 if ( !text
.empty() )
1353 if ( !text
.ToDouble(&value
) )
1356 else // new value is empty string
1358 if ( textOld
.empty() )
1359 return false; // nothing changed
1364 // the test for empty strings ensures that we don't skip the value setting
1365 // when "" is replaced by "0" or vice versa as "" numeric value is also 0.
1366 if ( wxIsSameDouble(value
, m_valueOld
) && !text
.empty() && !textOld
.empty() )
1367 return false; // nothing changed
1369 wxGridTableBase
* const table
= grid
->GetTable();
1371 if ( table
->CanSetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
1372 table
->SetValueAsDouble(row
, col
, value
);
1374 table
->SetValue(row
, col
, text
);
1379 void wxGridCellFloatEditor::Reset()
1381 DoReset(GetString());
1384 void wxGridCellFloatEditor::StartingKey(wxKeyEvent
& event
)
1386 int keycode
= event
.GetKeyCode();
1388 tmpbuf
[0] = (char) keycode
;
1390 wxString
strbuf(tmpbuf
, *wxConvCurrent
);
1393 bool is_decimal_point
= ( strbuf
==
1394 wxLocale::GetInfo(wxLOCALE_DECIMAL_POINT
, wxLOCALE_CAT_NUMBER
) );
1396 bool is_decimal_point
= ( strbuf
== _T(".") );
1399 if ( wxIsdigit(keycode
) || keycode
== '+' || keycode
== '-'
1400 || is_decimal_point
)
1402 wxGridCellTextEditor::StartingKey(event
);
1404 // skip Skip() below
1411 void wxGridCellFloatEditor::SetParameters(const wxString
& params
)
1422 if ( params
.BeforeFirst(_T(',')).ToLong(&tmp
) )
1426 if ( params
.AfterFirst(_T(',')).ToLong(&tmp
) )
1428 m_precision
= (int)tmp
;
1430 // skip the error message below
1435 wxLogDebug(_T("Invalid wxGridCellFloatEditor parameter string '%s' ignored"), params
.c_str());
1439 wxString
wxGridCellFloatEditor::GetString() const
1442 if ( m_precision
== -1 && m_width
!= -1)
1444 // default precision
1445 fmt
.Printf(_T("%%%d.f"), m_width
);
1447 else if ( m_precision
!= -1 && m_width
== -1)
1450 fmt
.Printf(_T("%%.%df"), m_precision
);
1452 else if ( m_precision
!= -1 && m_width
!= -1 )
1454 fmt
.Printf(_T("%%%d.%df"), m_width
, m_precision
);
1458 // default width/precision
1462 return wxString::Format(fmt
, m_valueOld
);
1465 bool wxGridCellFloatEditor::IsAcceptedKey(wxKeyEvent
& event
)
1467 if ( wxGridCellEditor::IsAcceptedKey(event
) )
1469 const int keycode
= event
.GetKeyCode();
1470 if ( isascii(keycode
) )
1473 tmpbuf
[0] = (char) keycode
;
1475 wxString
strbuf(tmpbuf
, *wxConvCurrent
);
1478 const wxString decimalPoint
=
1479 wxLocale::GetInfo(wxLOCALE_DECIMAL_POINT
, wxLOCALE_CAT_NUMBER
);
1481 const wxString
decimalPoint(_T('.'));
1484 // accept digits, 'e' as in '1e+6', also '-', '+', and '.'
1485 if ( wxIsdigit(keycode
) ||
1486 tolower(keycode
) == 'e' ||
1487 keycode
== decimalPoint
||
1499 #endif // wxUSE_TEXTCTRL
1503 // ----------------------------------------------------------------------------
1504 // wxGridCellBoolEditor
1505 // ----------------------------------------------------------------------------
1507 // the default values for GetValue()
1508 wxString
wxGridCellBoolEditor::ms_stringValues
[2] = { _T(""), _T("1") };
1510 void wxGridCellBoolEditor::Create(wxWindow
* parent
,
1512 wxEvtHandler
* evtHandler
)
1514 m_control
= new wxCheckBox(parent
, id
, wxEmptyString
,
1515 wxDefaultPosition
, wxDefaultSize
,
1518 wxGridCellEditor::Create(parent
, id
, evtHandler
);
1521 void wxGridCellBoolEditor::SetSize(const wxRect
& r
)
1523 bool resize
= false;
1524 wxSize size
= m_control
->GetSize();
1525 wxCoord minSize
= wxMin(r
.width
, r
.height
);
1527 // check if the checkbox is not too big/small for this cell
1528 wxSize sizeBest
= m_control
->GetBestSize();
1529 if ( !(size
== sizeBest
) )
1531 // reset to default size if it had been made smaller
1537 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
1539 // leave 1 pixel margin
1540 size
.x
= size
.y
= minSize
- 2;
1547 m_control
->SetSize(size
);
1550 // position it in the centre of the rectangle (TODO: support alignment?)
1552 #if defined(__WXGTK__) || defined (__WXMOTIF__)
1553 // the checkbox without label still has some space to the right in wxGTK,
1554 // so shift it to the right
1556 #elif defined(__WXMSW__)
1557 // here too, but in other way
1562 int hAlign
= wxALIGN_CENTRE
;
1563 int vAlign
= wxALIGN_CENTRE
;
1565 GetCellAttr()->GetAlignment(& hAlign
, & vAlign
);
1568 if (hAlign
== wxALIGN_LEFT
)
1576 y
= r
.y
+ r
.height
/ 2 - size
.y
/ 2;
1578 else if (hAlign
== wxALIGN_RIGHT
)
1580 x
= r
.x
+ r
.width
- size
.x
- 2;
1581 y
= r
.y
+ r
.height
/ 2 - size
.y
/ 2;
1583 else if (hAlign
== wxALIGN_CENTRE
)
1585 x
= r
.x
+ r
.width
/ 2 - size
.x
/ 2;
1586 y
= r
.y
+ r
.height
/ 2 - size
.y
/ 2;
1589 m_control
->Move(x
, y
);
1592 void wxGridCellBoolEditor::Show(bool show
, wxGridCellAttr
*attr
)
1594 m_control
->Show(show
);
1598 wxColour colBg
= attr
? attr
->GetBackgroundColour() : *wxLIGHT_GREY
;
1599 CBox()->SetBackgroundColour(colBg
);
1603 void wxGridCellBoolEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1605 wxASSERT_MSG(m_control
,
1606 wxT("The wxGridCellEditor must be created first!"));
1608 if (grid
->GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
))
1610 m_startValue
= grid
->GetTable()->GetValueAsBool(row
, col
);
1614 wxString
cellval( grid
->GetTable()->GetValue(row
, col
) );
1616 if ( cellval
== ms_stringValues
[false] )
1617 m_startValue
= false;
1618 else if ( cellval
== ms_stringValues
[true] )
1619 m_startValue
= true;
1622 // do not try to be smart here and convert it to true or false
1623 // because we'll still overwrite it with something different and
1624 // this risks to be very surprising for the user code, let them
1626 wxFAIL_MSG( _T("invalid value for a cell with bool editor!") );
1630 CBox()->SetValue(m_startValue
);
1634 bool wxGridCellBoolEditor::EndEdit(int row
, int col
,
1637 wxASSERT_MSG(m_control
,
1638 wxT("The wxGridCellEditor must be created first!"));
1640 bool changed
= false;
1641 bool value
= CBox()->GetValue();
1642 if ( value
!= m_startValue
)
1647 wxGridTableBase
* const table
= grid
->GetTable();
1648 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
) )
1649 table
->SetValueAsBool(row
, col
, value
);
1651 table
->SetValue(row
, col
, GetValue());
1657 void wxGridCellBoolEditor::Reset()
1659 wxASSERT_MSG(m_control
,
1660 wxT("The wxGridCellEditor must be created first!"));
1662 CBox()->SetValue(m_startValue
);
1665 void wxGridCellBoolEditor::StartingClick()
1667 CBox()->SetValue(!CBox()->GetValue());
1670 bool wxGridCellBoolEditor::IsAcceptedKey(wxKeyEvent
& event
)
1672 if ( wxGridCellEditor::IsAcceptedKey(event
) )
1674 int keycode
= event
.GetKeyCode();
1687 void wxGridCellBoolEditor::StartingKey(wxKeyEvent
& event
)
1689 int keycode
= event
.GetKeyCode();
1693 CBox()->SetValue(!CBox()->GetValue());
1697 CBox()->SetValue(true);
1701 CBox()->SetValue(false);
1706 wxString
wxGridCellBoolEditor::GetValue() const
1708 return ms_stringValues
[CBox()->GetValue()];
1712 wxGridCellBoolEditor::UseStringValues(const wxString
& valueTrue
,
1713 const wxString
& valueFalse
)
1715 ms_stringValues
[false] = valueFalse
;
1716 ms_stringValues
[true] = valueTrue
;
1720 wxGridCellBoolEditor::IsTrueValue(const wxString
& value
)
1722 return value
== ms_stringValues
[true];
1725 #endif // wxUSE_CHECKBOX
1729 // ----------------------------------------------------------------------------
1730 // wxGridCellChoiceEditor
1731 // ----------------------------------------------------------------------------
1733 wxGridCellChoiceEditor::wxGridCellChoiceEditor(const wxArrayString
& choices
,
1735 : m_choices(choices
),
1736 m_allowOthers(allowOthers
) { }
1738 wxGridCellChoiceEditor::wxGridCellChoiceEditor(size_t count
,
1739 const wxString choices
[],
1741 : m_allowOthers(allowOthers
)
1745 m_choices
.Alloc(count
);
1746 for ( size_t n
= 0; n
< count
; n
++ )
1748 m_choices
.Add(choices
[n
]);
1753 wxGridCellEditor
*wxGridCellChoiceEditor::Clone() const
1755 wxGridCellChoiceEditor
*editor
= new wxGridCellChoiceEditor
;
1756 editor
->m_allowOthers
= m_allowOthers
;
1757 editor
->m_choices
= m_choices
;
1762 void wxGridCellChoiceEditor::Create(wxWindow
* parent
,
1764 wxEvtHandler
* evtHandler
)
1766 int style
= wxTE_PROCESS_ENTER
|
1770 if ( !m_allowOthers
)
1771 style
|= wxCB_READONLY
;
1772 m_control
= new wxComboBox(parent
, id
, wxEmptyString
,
1773 wxDefaultPosition
, wxDefaultSize
,
1777 wxGridCellEditor::Create(parent
, id
, evtHandler
);
1780 void wxGridCellChoiceEditor::PaintBackground(const wxRect
& rectCell
,
1781 wxGridCellAttr
* attr
)
1783 // as we fill the entire client area, don't do anything here to minimize
1786 // TODO: It doesn't actually fill the client area since the height of a
1787 // combo always defaults to the standard. Until someone has time to
1788 // figure out the right rectangle to paint, just do it the normal way.
1789 wxGridCellEditor::PaintBackground(rectCell
, attr
);
1792 void wxGridCellChoiceEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1794 wxASSERT_MSG(m_control
,
1795 wxT("The wxGridCellEditor must be created first!"));
1797 wxGridCellEditorEvtHandler
* evtHandler
= NULL
;
1799 evtHandler
= wxDynamicCast(m_control
->GetEventHandler(), wxGridCellEditorEvtHandler
);
1801 // Don't immediately end if we get a kill focus event within BeginEdit
1803 evtHandler
->SetInSetFocus(true);
1805 m_startValue
= grid
->GetTable()->GetValue(row
, col
);
1807 Reset(); // this updates combo box to correspond to m_startValue
1809 Combo()->SetFocus();
1813 // When dropping down the menu, a kill focus event
1814 // happens after this point, so we can't reset the flag yet.
1815 #if !defined(__WXGTK20__)
1816 evtHandler
->SetInSetFocus(false);
1821 bool wxGridCellChoiceEditor::EndEdit(int row
, int col
,
1824 wxString value
= Combo()->GetValue();
1825 if ( value
== m_startValue
)
1828 grid
->GetTable()->SetValue(row
, col
, value
);
1833 void wxGridCellChoiceEditor::Reset()
1837 Combo()->SetValue(m_startValue
);
1838 Combo()->SetInsertionPointEnd();
1840 else // the combobox is read-only
1842 // find the right position, or default to the first if not found
1843 int pos
= Combo()->FindString(m_startValue
);
1844 if (pos
== wxNOT_FOUND
)
1846 Combo()->SetSelection(pos
);
1850 void wxGridCellChoiceEditor::SetParameters(const wxString
& params
)
1860 wxStringTokenizer
tk(params
, _T(','));
1861 while ( tk
.HasMoreTokens() )
1863 m_choices
.Add(tk
.GetNextToken());
1867 // return the value in the text control
1868 wxString
wxGridCellChoiceEditor::GetValue() const
1870 return Combo()->GetValue();
1873 #endif // wxUSE_COMBOBOX
1875 // ----------------------------------------------------------------------------
1876 // wxGridCellEditorEvtHandler
1877 // ----------------------------------------------------------------------------
1879 void wxGridCellEditorEvtHandler::OnKillFocus(wxFocusEvent
& event
)
1881 // Don't disable the cell if we're just starting to edit it
1886 m_grid
->DisableCellEditControl();
1891 void wxGridCellEditorEvtHandler::OnKeyDown(wxKeyEvent
& event
)
1893 switch ( event
.GetKeyCode() )
1897 m_grid
->DisableCellEditControl();
1901 m_grid
->GetEventHandler()->ProcessEvent( event
);
1905 case WXK_NUMPAD_ENTER
:
1906 if (!m_grid
->GetEventHandler()->ProcessEvent(event
))
1907 m_editor
->HandleReturn(event
);
1916 void wxGridCellEditorEvtHandler::OnChar(wxKeyEvent
& event
)
1918 int row
= m_grid
->GetGridCursorRow();
1919 int col
= m_grid
->GetGridCursorCol();
1920 wxRect rect
= m_grid
->CellToRect( row
, col
);
1922 m_grid
->GetGridWindow()->GetClientSize( &cw
, &ch
);
1924 // if cell width is smaller than grid client area, cell is wholly visible
1925 bool wholeCellVisible
= (rect
.GetWidth() < cw
);
1927 switch ( event
.GetKeyCode() )
1932 case WXK_NUMPAD_ENTER
:
1937 if ( wholeCellVisible
)
1939 // no special processing needed...
1944 // do special processing for partly visible cell...
1946 // get the widths of all cells previous to this one
1948 for ( int i
= 0; i
< col
; i
++ )
1950 colXPos
+= m_grid
->GetColSize(i
);
1953 int xUnit
= 1, yUnit
= 1;
1954 m_grid
->GetScrollPixelsPerUnit(&xUnit
, &yUnit
);
1957 m_grid
->Scroll(colXPos
/ xUnit
- 1, m_grid
->GetScrollPos(wxVERTICAL
));
1961 m_grid
->Scroll(colXPos
/ xUnit
, m_grid
->GetScrollPos(wxVERTICAL
));
1969 if ( wholeCellVisible
)
1971 // no special processing needed...
1976 // do special processing for partly visible cell...
1979 wxString value
= m_grid
->GetCellValue(row
, col
);
1980 if ( wxEmptyString
!= value
)
1982 // get width of cell CONTENTS (text)
1984 wxFont font
= m_grid
->GetCellFont(row
, col
);
1985 m_grid
->GetTextExtent(value
, &textWidth
, &y
, NULL
, NULL
, &font
);
1987 // try to RIGHT align the text by scrolling
1988 int client_right
= m_grid
->GetGridWindow()->GetClientSize().GetWidth();
1990 // (m_grid->GetScrollLineX()*2) is a factor for not scrolling to far,
1991 // otherwise the last part of the cell content might be hidden below the scroll bar
1992 // FIXME: maybe there is a more suitable correction?
1993 textWidth
-= (client_right
- (m_grid
->GetScrollLineX() * 2));
1994 if ( textWidth
< 0 )
2000 // get the widths of all cells previous to this one
2002 for ( int i
= 0; i
< col
; i
++ )
2004 colXPos
+= m_grid
->GetColSize(i
);
2007 // and add the (modified) text width of the cell contents
2008 // as we'd like to see the last part of the cell contents
2009 colXPos
+= textWidth
;
2011 int xUnit
= 1, yUnit
= 1;
2012 m_grid
->GetScrollPixelsPerUnit(&xUnit
, &yUnit
);
2013 m_grid
->Scroll(colXPos
/ xUnit
- 1, m_grid
->GetScrollPos(wxVERTICAL
));
2024 // ----------------------------------------------------------------------------
2025 // wxGridCellWorker is an (almost) empty common base class for
2026 // wxGridCellRenderer and wxGridCellEditor managing ref counting
2027 // ----------------------------------------------------------------------------
2029 void wxGridCellWorker::SetParameters(const wxString
& WXUNUSED(params
))
2034 wxGridCellWorker::~wxGridCellWorker()
2038 // ============================================================================
2040 // ============================================================================
2042 // ----------------------------------------------------------------------------
2043 // wxGridCellRenderer
2044 // ----------------------------------------------------------------------------
2046 void wxGridCellRenderer::Draw(wxGrid
& grid
,
2047 wxGridCellAttr
& attr
,
2050 int WXUNUSED(row
), int WXUNUSED(col
),
2053 dc
.SetBackgroundMode( wxBRUSHSTYLE_SOLID
);
2056 if ( grid
.IsEnabled() )
2060 if ( grid
.HasFocus() )
2061 clr
= grid
.GetSelectionBackground();
2063 clr
= wxSystemSettings::GetColour(wxSYS_COLOUR_BTNSHADOW
);
2067 clr
= attr
.GetBackgroundColour();
2070 else // grey out fields if the grid is disabled
2072 clr
= wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
);
2076 dc
.SetPen( *wxTRANSPARENT_PEN
);
2077 dc
.DrawRectangle(rect
);
2080 // ----------------------------------------------------------------------------
2081 // wxGridCellStringRenderer
2082 // ----------------------------------------------------------------------------
2084 void wxGridCellStringRenderer::SetTextColoursAndFont(const wxGrid
& grid
,
2085 const wxGridCellAttr
& attr
,
2089 dc
.SetBackgroundMode( wxBRUSHSTYLE_TRANSPARENT
);
2091 // TODO some special colours for attr.IsReadOnly() case?
2093 // different coloured text when the grid is disabled
2094 if ( grid
.IsEnabled() )
2099 if ( grid
.HasFocus() )
2100 clr
= grid
.GetSelectionBackground();
2102 clr
= wxSystemSettings::GetColour(wxSYS_COLOUR_BTNSHADOW
);
2103 dc
.SetTextBackground( clr
);
2104 dc
.SetTextForeground( grid
.GetSelectionForeground() );
2108 dc
.SetTextBackground( attr
.GetBackgroundColour() );
2109 dc
.SetTextForeground( attr
.GetTextColour() );
2114 dc
.SetTextBackground(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
));
2115 dc
.SetTextForeground(wxSystemSettings::GetColour(wxSYS_COLOUR_GRAYTEXT
));
2118 dc
.SetFont( attr
.GetFont() );
2121 wxSize
wxGridCellStringRenderer::DoGetBestSize(const wxGridCellAttr
& attr
,
2123 const wxString
& text
)
2125 wxCoord x
= 0, y
= 0, max_x
= 0;
2126 dc
.SetFont(attr
.GetFont());
2127 wxStringTokenizer
tk(text
, _T('\n'));
2128 while ( tk
.HasMoreTokens() )
2130 dc
.GetTextExtent(tk
.GetNextToken(), &x
, &y
);
2131 max_x
= wxMax(max_x
, x
);
2134 y
*= 1 + text
.Freq(wxT('\n')); // multiply by the number of lines.
2136 return wxSize(max_x
, y
);
2139 wxSize
wxGridCellStringRenderer::GetBestSize(wxGrid
& grid
,
2140 wxGridCellAttr
& attr
,
2144 return DoGetBestSize(attr
, dc
, grid
.GetCellValue(row
, col
));
2147 void wxGridCellStringRenderer::Draw(wxGrid
& grid
,
2148 wxGridCellAttr
& attr
,
2150 const wxRect
& rectCell
,
2154 wxRect rect
= rectCell
;
2157 // erase only this cells background, overflow cells should have been erased
2158 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
2161 attr
.GetAlignment(&hAlign
, &vAlign
);
2163 int overflowCols
= 0;
2165 if (attr
.GetOverflow())
2167 int cols
= grid
.GetNumberCols();
2168 int best_width
= GetBestSize(grid
,attr
,dc
,row
,col
).GetWidth();
2169 int cell_rows
, cell_cols
;
2170 attr
.GetSize( &cell_rows
, &cell_cols
); // shouldn't get here if <= 0
2171 if ((best_width
> rectCell
.width
) && (col
< cols
) && grid
.GetTable())
2173 int i
, c_cols
, c_rows
;
2174 for (i
= col
+cell_cols
; i
< cols
; i
++)
2176 bool is_empty
= true;
2177 for (int j
=row
; j
< row
+ cell_rows
; j
++)
2179 // check w/ anchor cell for multicell block
2180 grid
.GetCellSize(j
, i
, &c_rows
, &c_cols
);
2183 if (!grid
.GetTable()->IsEmptyCell(j
+ c_rows
, i
))
2192 rect
.width
+= grid
.GetColSize(i
);
2200 if (rect
.width
>= best_width
)
2204 overflowCols
= i
- col
- cell_cols
+ 1;
2205 if (overflowCols
>= cols
)
2206 overflowCols
= cols
- 1;
2209 if (overflowCols
> 0) // redraw overflow cells w/ proper hilight
2211 hAlign
= wxALIGN_LEFT
; // if oveflowed then it's left aligned
2213 clip
.x
+= rectCell
.width
;
2214 // draw each overflow cell individually
2215 int col_end
= col
+ cell_cols
+ overflowCols
;
2216 if (col_end
>= grid
.GetNumberCols())
2217 col_end
= grid
.GetNumberCols() - 1;
2218 for (int i
= col
+ cell_cols
; i
<= col_end
; i
++)
2220 clip
.width
= grid
.GetColSize(i
) - 1;
2221 dc
.DestroyClippingRegion();
2222 dc
.SetClippingRegion(clip
);
2224 SetTextColoursAndFont(grid
, attr
, dc
,
2225 grid
.IsInSelection(row
,i
));
2227 grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
),
2228 rect
, hAlign
, vAlign
);
2229 clip
.x
+= grid
.GetColSize(i
) - 1;
2235 dc
.DestroyClippingRegion();
2239 // now we only have to draw the text
2240 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
2242 grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
),
2243 rect
, hAlign
, vAlign
);
2246 // ----------------------------------------------------------------------------
2247 // wxGridCellNumberRenderer
2248 // ----------------------------------------------------------------------------
2250 wxString
wxGridCellNumberRenderer::GetString(const wxGrid
& grid
, int row
, int col
)
2252 wxGridTableBase
*table
= grid
.GetTable();
2254 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
2256 text
.Printf(_T("%ld"), table
->GetValueAsLong(row
, col
));
2260 text
= table
->GetValue(row
, col
);
2266 void wxGridCellNumberRenderer::Draw(wxGrid
& grid
,
2267 wxGridCellAttr
& attr
,
2269 const wxRect
& rectCell
,
2273 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
2275 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
2277 // draw the text right aligned by default
2279 attr
.GetAlignment(&hAlign
, &vAlign
);
2280 hAlign
= wxALIGN_RIGHT
;
2282 wxRect rect
= rectCell
;
2285 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
2288 wxSize
wxGridCellNumberRenderer::GetBestSize(wxGrid
& grid
,
2289 wxGridCellAttr
& attr
,
2293 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
2296 // ----------------------------------------------------------------------------
2297 // wxGridCellFloatRenderer
2298 // ----------------------------------------------------------------------------
2300 wxGridCellFloatRenderer::wxGridCellFloatRenderer(int width
, int precision
)
2303 SetPrecision(precision
);
2306 wxGridCellRenderer
*wxGridCellFloatRenderer::Clone() const
2308 wxGridCellFloatRenderer
*renderer
= new wxGridCellFloatRenderer
;
2309 renderer
->m_width
= m_width
;
2310 renderer
->m_precision
= m_precision
;
2311 renderer
->m_format
= m_format
;
2316 wxString
wxGridCellFloatRenderer::GetString(const wxGrid
& grid
, int row
, int col
)
2318 wxGridTableBase
*table
= grid
.GetTable();
2323 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
2325 val
= table
->GetValueAsDouble(row
, col
);
2330 text
= table
->GetValue(row
, col
);
2331 hasDouble
= text
.ToDouble(&val
);
2338 if ( m_width
== -1 )
2340 if ( m_precision
== -1 )
2342 // default width/precision
2343 m_format
= _T("%f");
2347 m_format
.Printf(_T("%%.%df"), m_precision
);
2350 else if ( m_precision
== -1 )
2352 // default precision
2353 m_format
.Printf(_T("%%%d.f"), m_width
);
2357 m_format
.Printf(_T("%%%d.%df"), m_width
, m_precision
);
2361 text
.Printf(m_format
, val
);
2364 //else: text already contains the string
2369 void wxGridCellFloatRenderer::Draw(wxGrid
& grid
,
2370 wxGridCellAttr
& attr
,
2372 const wxRect
& rectCell
,
2376 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
2378 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
2380 // draw the text right aligned by default
2382 attr
.GetAlignment(&hAlign
, &vAlign
);
2383 hAlign
= wxALIGN_RIGHT
;
2385 wxRect rect
= rectCell
;
2388 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
2391 wxSize
wxGridCellFloatRenderer::GetBestSize(wxGrid
& grid
,
2392 wxGridCellAttr
& attr
,
2396 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
2399 void wxGridCellFloatRenderer::SetParameters(const wxString
& params
)
2403 // reset to defaults
2409 wxString tmp
= params
.BeforeFirst(_T(','));
2413 if ( tmp
.ToLong(&width
) )
2415 SetWidth((int)width
);
2419 wxLogDebug(_T("Invalid wxGridCellFloatRenderer width parameter string '%s ignored"), params
.c_str());
2423 tmp
= params
.AfterFirst(_T(','));
2427 if ( tmp
.ToLong(&precision
) )
2429 SetPrecision((int)precision
);
2433 wxLogDebug(_T("Invalid wxGridCellFloatRenderer precision parameter string '%s ignored"), params
.c_str());
2439 // ----------------------------------------------------------------------------
2440 // wxGridCellBoolRenderer
2441 // ----------------------------------------------------------------------------
2443 wxSize
wxGridCellBoolRenderer::ms_sizeCheckMark
;
2445 // FIXME these checkbox size calculations are really ugly...
2447 // between checkmark and box
2448 static const wxCoord wxGRID_CHECKMARK_MARGIN
= 2;
2450 wxSize
wxGridCellBoolRenderer::GetBestSize(wxGrid
& grid
,
2451 wxGridCellAttr
& WXUNUSED(attr
),
2456 // compute it only once (no locks for MT safeness in GUI thread...)
2457 if ( !ms_sizeCheckMark
.x
)
2459 // get checkbox size
2460 wxCheckBox
*checkbox
= new wxCheckBox(&grid
, wxID_ANY
, wxEmptyString
);
2461 wxSize size
= checkbox
->GetBestSize();
2462 wxCoord checkSize
= size
.y
+ 2 * wxGRID_CHECKMARK_MARGIN
;
2464 #if defined(__WXMOTIF__)
2465 checkSize
-= size
.y
/ 2;
2470 ms_sizeCheckMark
.x
= ms_sizeCheckMark
.y
= checkSize
;
2473 return ms_sizeCheckMark
;
2476 void wxGridCellBoolRenderer::Draw(wxGrid
& grid
,
2477 wxGridCellAttr
& attr
,
2483 wxGridCellRenderer::Draw(grid
, attr
, dc
, rect
, row
, col
, isSelected
);
2485 // draw a check mark in the centre (ignoring alignment - TODO)
2486 wxSize size
= GetBestSize(grid
, attr
, dc
, row
, col
);
2488 // don't draw outside the cell
2489 wxCoord minSize
= wxMin(rect
.width
, rect
.height
);
2490 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
2492 // and even leave (at least) 1 pixel margin
2493 size
.x
= size
.y
= minSize
;
2496 // draw a border around checkmark
2498 attr
.GetAlignment(&hAlign
, &vAlign
);
2501 if (hAlign
== wxALIGN_CENTRE
)
2503 rectBorder
.x
= rect
.x
+ rect
.width
/ 2 - size
.x
/ 2;
2504 rectBorder
.y
= rect
.y
+ rect
.height
/ 2 - size
.y
/ 2;
2505 rectBorder
.width
= size
.x
;
2506 rectBorder
.height
= size
.y
;
2508 else if (hAlign
== wxALIGN_LEFT
)
2510 rectBorder
.x
= rect
.x
+ 2;
2511 rectBorder
.y
= rect
.y
+ rect
.height
/ 2 - size
.y
/ 2;
2512 rectBorder
.width
= size
.x
;
2513 rectBorder
.height
= size
.y
;
2515 else if (hAlign
== wxALIGN_RIGHT
)
2517 rectBorder
.x
= rect
.x
+ rect
.width
- size
.x
- 2;
2518 rectBorder
.y
= rect
.y
+ rect
.height
/ 2 - size
.y
/ 2;
2519 rectBorder
.width
= size
.x
;
2520 rectBorder
.height
= size
.y
;
2524 if ( grid
.GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
) )
2526 value
= grid
.GetTable()->GetValueAsBool(row
, col
);
2530 wxString
cellval( grid
.GetTable()->GetValue(row
, col
) );
2531 value
= wxGridCellBoolEditor::IsTrueValue(cellval
);
2536 flags
|= wxCONTROL_CHECKED
;
2538 wxRendererNative::Get().DrawCheckBox( &grid
, dc
, rectBorder
, flags
);
2541 // ----------------------------------------------------------------------------
2543 // ----------------------------------------------------------------------------
2545 void wxGridCellAttr::Init(wxGridCellAttr
*attrDefault
)
2549 m_isReadOnly
= Unset
;
2554 m_attrkind
= wxGridCellAttr::Cell
;
2556 m_sizeRows
= m_sizeCols
= 1;
2557 m_overflow
= UnsetOverflow
;
2559 SetDefAttr(attrDefault
);
2562 wxGridCellAttr
*wxGridCellAttr::Clone() const
2564 wxGridCellAttr
*attr
= new wxGridCellAttr(m_defGridAttr
);
2566 if ( HasTextColour() )
2567 attr
->SetTextColour(GetTextColour());
2568 if ( HasBackgroundColour() )
2569 attr
->SetBackgroundColour(GetBackgroundColour());
2571 attr
->SetFont(GetFont());
2572 if ( HasAlignment() )
2573 attr
->SetAlignment(m_hAlign
, m_vAlign
);
2575 attr
->SetSize( m_sizeRows
, m_sizeCols
);
2579 attr
->SetRenderer(m_renderer
);
2580 m_renderer
->IncRef();
2584 attr
->SetEditor(m_editor
);
2589 attr
->SetReadOnly();
2591 attr
->SetOverflow( m_overflow
== Overflow
);
2592 attr
->SetKind( m_attrkind
);
2597 void wxGridCellAttr::MergeWith(wxGridCellAttr
*mergefrom
)
2599 if ( !HasTextColour() && mergefrom
->HasTextColour() )
2600 SetTextColour(mergefrom
->GetTextColour());
2601 if ( !HasBackgroundColour() && mergefrom
->HasBackgroundColour() )
2602 SetBackgroundColour(mergefrom
->GetBackgroundColour());
2603 if ( !HasFont() && mergefrom
->HasFont() )
2604 SetFont(mergefrom
->GetFont());
2605 if ( !HasAlignment() && mergefrom
->HasAlignment() )
2608 mergefrom
->GetAlignment( &hAlign
, &vAlign
);
2609 SetAlignment(hAlign
, vAlign
);
2611 if ( !HasSize() && mergefrom
->HasSize() )
2612 mergefrom
->GetSize( &m_sizeRows
, &m_sizeCols
);
2614 // Directly access member functions as GetRender/Editor don't just return
2615 // m_renderer/m_editor
2617 // Maybe add support for merge of Render and Editor?
2618 if (!HasRenderer() && mergefrom
->HasRenderer() )
2620 m_renderer
= mergefrom
->m_renderer
;
2621 m_renderer
->IncRef();
2623 if ( !HasEditor() && mergefrom
->HasEditor() )
2625 m_editor
= mergefrom
->m_editor
;
2628 if ( !HasReadWriteMode() && mergefrom
->HasReadWriteMode() )
2629 SetReadOnly(mergefrom
->IsReadOnly());
2631 if (!HasOverflowMode() && mergefrom
->HasOverflowMode() )
2632 SetOverflow(mergefrom
->GetOverflow());
2634 SetDefAttr(mergefrom
->m_defGridAttr
);
2637 void wxGridCellAttr::SetSize(int num_rows
, int num_cols
)
2639 // The size of a cell is normally 1,1
2641 // If this cell is larger (2,2) then this is the top left cell
2642 // the other cells that will be covered (lower right cells) must be
2643 // set to negative or zero values such that
2644 // row + num_rows of the covered cell points to the larger cell (this cell)
2645 // same goes for the col + num_cols.
2647 // Size of 0,0 is NOT valid, neither is <=0 and any positive value
2649 wxASSERT_MSG( (!((num_rows
> 0) && (num_cols
<= 0)) ||
2650 !((num_rows
<= 0) && (num_cols
> 0)) ||
2651 !((num_rows
== 0) && (num_cols
== 0))),
2652 wxT("wxGridCellAttr::SetSize only takes two postive values or negative/zero values"));
2654 m_sizeRows
= num_rows
;
2655 m_sizeCols
= num_cols
;
2658 const wxColour
& wxGridCellAttr::GetTextColour() const
2660 if (HasTextColour())
2664 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2666 return m_defGridAttr
->GetTextColour();
2670 wxFAIL_MSG(wxT("Missing default cell attribute"));
2671 return wxNullColour
;
2675 const wxColour
& wxGridCellAttr::GetBackgroundColour() const
2677 if (HasBackgroundColour())
2681 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2683 return m_defGridAttr
->GetBackgroundColour();
2687 wxFAIL_MSG(wxT("Missing default cell attribute"));
2688 return wxNullColour
;
2692 const wxFont
& wxGridCellAttr::GetFont() const
2698 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2700 return m_defGridAttr
->GetFont();
2704 wxFAIL_MSG(wxT("Missing default cell attribute"));
2709 void wxGridCellAttr::GetAlignment(int *hAlign
, int *vAlign
) const
2718 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2720 m_defGridAttr
->GetAlignment(hAlign
, vAlign
);
2724 wxFAIL_MSG(wxT("Missing default cell attribute"));
2728 void wxGridCellAttr::GetSize( int *num_rows
, int *num_cols
) const
2731 *num_rows
= m_sizeRows
;
2733 *num_cols
= m_sizeCols
;
2736 // GetRenderer and GetEditor use a slightly different decision path about
2737 // which attribute to use. If a non-default attr object has one then it is
2738 // used, otherwise the default editor or renderer is fetched from the grid and
2739 // used. It should be the default for the data type of the cell. If it is
2740 // NULL (because the table has a type that the grid does not have in its
2741 // registry), then the grid's default editor or renderer is used.
2743 wxGridCellRenderer
* wxGridCellAttr::GetRenderer(const wxGrid
* grid
, int row
, int col
) const
2745 wxGridCellRenderer
*renderer
= NULL
;
2747 if ( m_renderer
&& this != m_defGridAttr
)
2749 // use the cells renderer if it has one
2750 renderer
= m_renderer
;
2753 else // no non-default cell renderer
2755 // get default renderer for the data type
2758 // GetDefaultRendererForCell() will do IncRef() for us
2759 renderer
= grid
->GetDefaultRendererForCell(row
, col
);
2762 if ( renderer
== NULL
)
2764 if ( (m_defGridAttr
!= NULL
) && (m_defGridAttr
!= this) )
2766 // if we still don't have one then use the grid default
2767 // (no need for IncRef() here neither)
2768 renderer
= m_defGridAttr
->GetRenderer(NULL
, 0, 0);
2770 else // default grid attr
2772 // use m_renderer which we had decided not to use initially
2773 renderer
= m_renderer
;
2780 // we're supposed to always find something
2781 wxASSERT_MSG(renderer
, wxT("Missing default cell renderer"));
2786 // same as above, except for s/renderer/editor/g
2787 wxGridCellEditor
* wxGridCellAttr::GetEditor(const wxGrid
* grid
, int row
, int col
) const
2789 wxGridCellEditor
*editor
= NULL
;
2791 if ( m_editor
&& this != m_defGridAttr
)
2793 // use the cells editor if it has one
2797 else // no non default cell editor
2799 // get default editor for the data type
2802 // GetDefaultEditorForCell() will do IncRef() for us
2803 editor
= grid
->GetDefaultEditorForCell(row
, col
);
2806 if ( editor
== NULL
)
2808 if ( (m_defGridAttr
!= NULL
) && (m_defGridAttr
!= this) )
2810 // if we still don't have one then use the grid default
2811 // (no need for IncRef() here neither)
2812 editor
= m_defGridAttr
->GetEditor(NULL
, 0, 0);
2814 else // default grid attr
2816 // use m_editor which we had decided not to use initially
2824 // we're supposed to always find something
2825 wxASSERT_MSG(editor
, wxT("Missing default cell editor"));
2830 // ----------------------------------------------------------------------------
2831 // wxGridCellAttrData
2832 // ----------------------------------------------------------------------------
2834 void wxGridCellAttrData::SetAttr(wxGridCellAttr
*attr
, int row
, int col
)
2836 // Note: contrary to wxGridRowOrColAttrData::SetAttr, we must not
2837 // touch attribute's reference counting explicitly, since this
2838 // is managed by class wxGridCellWithAttr
2839 int n
= FindIndex(row
, col
);
2840 if ( n
== wxNOT_FOUND
)
2844 // add the attribute
2845 m_attrs
.Add(new wxGridCellWithAttr(row
, col
, attr
));
2847 //else: nothing to do
2849 else // we already have an attribute for this cell
2853 // change the attribute
2854 m_attrs
[(size_t)n
].ChangeAttr(attr
);
2858 // remove this attribute
2859 m_attrs
.RemoveAt((size_t)n
);
2864 wxGridCellAttr
*wxGridCellAttrData::GetAttr(int row
, int col
) const
2866 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2868 int n
= FindIndex(row
, col
);
2869 if ( n
!= wxNOT_FOUND
)
2871 attr
= m_attrs
[(size_t)n
].attr
;
2878 void wxGridCellAttrData::UpdateAttrRows( size_t pos
, int numRows
)
2880 size_t count
= m_attrs
.GetCount();
2881 for ( size_t n
= 0; n
< count
; n
++ )
2883 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2884 wxCoord row
= coords
.GetRow();
2885 if ((size_t)row
>= pos
)
2889 // If rows inserted, include row counter where necessary
2890 coords
.SetRow(row
+ numRows
);
2892 else if (numRows
< 0)
2894 // If rows deleted ...
2895 if ((size_t)row
>= pos
- numRows
)
2897 // ...either decrement row counter (if row still exists)...
2898 coords
.SetRow(row
+ numRows
);
2902 // ...or remove the attribute
2903 m_attrs
.RemoveAt(n
);
2912 void wxGridCellAttrData::UpdateAttrCols( size_t pos
, int numCols
)
2914 size_t count
= m_attrs
.GetCount();
2915 for ( size_t n
= 0; n
< count
; n
++ )
2917 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2918 wxCoord col
= coords
.GetCol();
2919 if ( (size_t)col
>= pos
)
2923 // If rows inserted, include row counter where necessary
2924 coords
.SetCol(col
+ numCols
);
2926 else if (numCols
< 0)
2928 // If rows deleted ...
2929 if ((size_t)col
>= pos
- numCols
)
2931 // ...either decrement row counter (if row still exists)...
2932 coords
.SetCol(col
+ numCols
);
2936 // ...or remove the attribute
2937 m_attrs
.RemoveAt(n
);
2946 int wxGridCellAttrData::FindIndex(int row
, int col
) const
2948 size_t count
= m_attrs
.GetCount();
2949 for ( size_t n
= 0; n
< count
; n
++ )
2951 const wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2952 if ( (coords
.GetRow() == row
) && (coords
.GetCol() == col
) )
2961 // ----------------------------------------------------------------------------
2962 // wxGridRowOrColAttrData
2963 // ----------------------------------------------------------------------------
2965 wxGridRowOrColAttrData::~wxGridRowOrColAttrData()
2967 size_t count
= m_attrs
.GetCount();
2968 for ( size_t n
= 0; n
< count
; n
++ )
2970 m_attrs
[n
]->DecRef();
2974 wxGridCellAttr
*wxGridRowOrColAttrData::GetAttr(int rowOrCol
) const
2976 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2978 int n
= m_rowsOrCols
.Index(rowOrCol
);
2979 if ( n
!= wxNOT_FOUND
)
2981 attr
= m_attrs
[(size_t)n
];
2988 void wxGridRowOrColAttrData::SetAttr(wxGridCellAttr
*attr
, int rowOrCol
)
2990 int i
= m_rowsOrCols
.Index(rowOrCol
);
2991 if ( i
== wxNOT_FOUND
)
2995 // add the attribute - no need to do anything to reference count
2996 // since we take ownership of the attribute.
2997 m_rowsOrCols
.Add(rowOrCol
);
3000 // nothing to remove
3004 size_t n
= (size_t)i
;
3005 if ( m_attrs
[n
] == attr
)
3010 // change the attribute, handling reference count manually,
3011 // taking ownership of the new attribute.
3012 m_attrs
[n
]->DecRef();
3017 // remove this attribute, handling reference count manually
3018 m_attrs
[n
]->DecRef();
3019 m_rowsOrCols
.RemoveAt(n
);
3020 m_attrs
.RemoveAt(n
);
3025 void wxGridRowOrColAttrData::UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
)
3027 size_t count
= m_attrs
.GetCount();
3028 for ( size_t n
= 0; n
< count
; n
++ )
3030 int & rowOrCol
= m_rowsOrCols
[n
];
3031 if ( (size_t)rowOrCol
>= pos
)
3033 if ( numRowsOrCols
> 0 )
3035 // If rows inserted, include row counter where necessary
3036 rowOrCol
+= numRowsOrCols
;
3038 else if ( numRowsOrCols
< 0)
3040 // If rows deleted, either decrement row counter (if row still exists)
3041 if ((size_t)rowOrCol
>= pos
- numRowsOrCols
)
3042 rowOrCol
+= numRowsOrCols
;
3045 m_rowsOrCols
.RemoveAt(n
);
3046 m_attrs
[n
]->DecRef();
3047 m_attrs
.RemoveAt(n
);
3056 // ----------------------------------------------------------------------------
3057 // wxGridCellAttrProvider
3058 // ----------------------------------------------------------------------------
3060 wxGridCellAttrProvider::wxGridCellAttrProvider()
3062 m_data
= (wxGridCellAttrProviderData
*)NULL
;
3065 wxGridCellAttrProvider::~wxGridCellAttrProvider()
3070 void wxGridCellAttrProvider::InitData()
3072 m_data
= new wxGridCellAttrProviderData
;
3075 wxGridCellAttr
*wxGridCellAttrProvider::GetAttr(int row
, int col
,
3076 wxGridCellAttr::wxAttrKind kind
) const
3078 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
3083 case (wxGridCellAttr::Any
):
3084 // Get cached merge attributes.
3085 // Currently not used as no cache implemented as not mutable
3086 // attr = m_data->m_mergeAttr.GetAttr(row, col);
3089 // Basically implement old version.
3090 // Also check merge cache, so we don't have to re-merge every time..
3091 wxGridCellAttr
*attrcell
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
3092 wxGridCellAttr
*attrrow
= m_data
->m_rowAttrs
.GetAttr(row
);
3093 wxGridCellAttr
*attrcol
= m_data
->m_colAttrs
.GetAttr(col
);
3095 if ((attrcell
!= attrrow
) && (attrrow
!= attrcol
) && (attrcell
!= attrcol
))
3097 // Two or more are non NULL
3098 attr
= new wxGridCellAttr
;
3099 attr
->SetKind(wxGridCellAttr::Merged
);
3101 // Order is important..
3104 attr
->MergeWith(attrcell
);
3109 attr
->MergeWith(attrcol
);
3114 attr
->MergeWith(attrrow
);
3118 // store merge attr if cache implemented
3120 //m_data->m_mergeAttr.SetAttr(attr, row, col);
3124 // one or none is non null return it or null.
3143 case (wxGridCellAttr::Cell
):
3144 attr
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
3147 case (wxGridCellAttr::Col
):
3148 attr
= m_data
->m_colAttrs
.GetAttr(col
);
3151 case (wxGridCellAttr::Row
):
3152 attr
= m_data
->m_rowAttrs
.GetAttr(row
);
3157 // (wxGridCellAttr::Default):
3158 // (wxGridCellAttr::Merged):
3166 void wxGridCellAttrProvider::SetAttr(wxGridCellAttr
*attr
,
3172 m_data
->m_cellAttrs
.SetAttr(attr
, row
, col
);
3175 void wxGridCellAttrProvider::SetRowAttr(wxGridCellAttr
*attr
, int row
)
3180 m_data
->m_rowAttrs
.SetAttr(attr
, row
);
3183 void wxGridCellAttrProvider::SetColAttr(wxGridCellAttr
*attr
, int col
)
3188 m_data
->m_colAttrs
.SetAttr(attr
, col
);
3191 void wxGridCellAttrProvider::UpdateAttrRows( size_t pos
, int numRows
)
3195 m_data
->m_cellAttrs
.UpdateAttrRows( pos
, numRows
);
3197 m_data
->m_rowAttrs
.UpdateAttrRowsOrCols( pos
, numRows
);
3201 void wxGridCellAttrProvider::UpdateAttrCols( size_t pos
, int numCols
)
3205 m_data
->m_cellAttrs
.UpdateAttrCols( pos
, numCols
);
3207 m_data
->m_colAttrs
.UpdateAttrRowsOrCols( pos
, numCols
);
3211 // ----------------------------------------------------------------------------
3212 // wxGridTypeRegistry
3213 // ----------------------------------------------------------------------------
3215 wxGridTypeRegistry::~wxGridTypeRegistry()
3217 size_t count
= m_typeinfo
.GetCount();
3218 for ( size_t i
= 0; i
< count
; i
++ )
3219 delete m_typeinfo
[i
];
3222 void wxGridTypeRegistry::RegisterDataType(const wxString
& typeName
,
3223 wxGridCellRenderer
* renderer
,
3224 wxGridCellEditor
* editor
)
3226 wxGridDataTypeInfo
* info
= new wxGridDataTypeInfo(typeName
, renderer
, editor
);
3228 // is it already registered?
3229 int loc
= FindRegisteredDataType(typeName
);
3230 if ( loc
!= wxNOT_FOUND
)
3232 delete m_typeinfo
[loc
];
3233 m_typeinfo
[loc
] = info
;
3237 m_typeinfo
.Add(info
);
3241 int wxGridTypeRegistry::FindRegisteredDataType(const wxString
& typeName
)
3243 size_t count
= m_typeinfo
.GetCount();
3244 for ( size_t i
= 0; i
< count
; i
++ )
3246 if ( typeName
== m_typeinfo
[i
]->m_typeName
)
3255 int wxGridTypeRegistry::FindDataType(const wxString
& typeName
)
3257 int index
= FindRegisteredDataType(typeName
);
3258 if ( index
== wxNOT_FOUND
)
3260 // check whether this is one of the standard ones, in which case
3261 // register it "on the fly"
3263 if ( typeName
== wxGRID_VALUE_STRING
)
3265 RegisterDataType(wxGRID_VALUE_STRING
,
3266 new wxGridCellStringRenderer
,
3267 new wxGridCellTextEditor
);
3270 #endif // wxUSE_TEXTCTRL
3272 if ( typeName
== wxGRID_VALUE_BOOL
)
3274 RegisterDataType(wxGRID_VALUE_BOOL
,
3275 new wxGridCellBoolRenderer
,
3276 new wxGridCellBoolEditor
);
3279 #endif // wxUSE_CHECKBOX
3281 if ( typeName
== wxGRID_VALUE_NUMBER
)
3283 RegisterDataType(wxGRID_VALUE_NUMBER
,
3284 new wxGridCellNumberRenderer
,
3285 new wxGridCellNumberEditor
);
3287 else if ( typeName
== wxGRID_VALUE_FLOAT
)
3289 RegisterDataType(wxGRID_VALUE_FLOAT
,
3290 new wxGridCellFloatRenderer
,
3291 new wxGridCellFloatEditor
);
3294 #endif // wxUSE_TEXTCTRL
3296 if ( typeName
== wxGRID_VALUE_CHOICE
)
3298 RegisterDataType(wxGRID_VALUE_CHOICE
,
3299 new wxGridCellStringRenderer
,
3300 new wxGridCellChoiceEditor
);
3303 #endif // wxUSE_COMBOBOX
3308 // we get here only if just added the entry for this type, so return
3310 index
= m_typeinfo
.GetCount() - 1;
3316 int wxGridTypeRegistry::FindOrCloneDataType(const wxString
& typeName
)
3318 int index
= FindDataType(typeName
);
3319 if ( index
== wxNOT_FOUND
)
3321 // the first part of the typename is the "real" type, anything after ':'
3322 // are the parameters for the renderer
3323 index
= FindDataType(typeName
.BeforeFirst(_T(':')));
3324 if ( index
== wxNOT_FOUND
)
3329 wxGridCellRenderer
*renderer
= GetRenderer(index
);
3330 wxGridCellRenderer
*rendererOld
= renderer
;
3331 renderer
= renderer
->Clone();
3332 rendererOld
->DecRef();
3334 wxGridCellEditor
*editor
= GetEditor(index
);
3335 wxGridCellEditor
*editorOld
= editor
;
3336 editor
= editor
->Clone();
3337 editorOld
->DecRef();
3339 // do it even if there are no parameters to reset them to defaults
3340 wxString params
= typeName
.AfterFirst(_T(':'));
3341 renderer
->SetParameters(params
);
3342 editor
->SetParameters(params
);
3344 // register the new typename
3345 RegisterDataType(typeName
, renderer
, editor
);
3347 // we just registered it, it's the last one
3348 index
= m_typeinfo
.GetCount() - 1;
3354 wxGridCellRenderer
* wxGridTypeRegistry::GetRenderer(int index
)
3356 wxGridCellRenderer
* renderer
= m_typeinfo
[index
]->m_renderer
;
3363 wxGridCellEditor
* wxGridTypeRegistry::GetEditor(int index
)
3365 wxGridCellEditor
* editor
= m_typeinfo
[index
]->m_editor
;
3372 // ----------------------------------------------------------------------------
3374 // ----------------------------------------------------------------------------
3376 IMPLEMENT_ABSTRACT_CLASS( wxGridTableBase
, wxObject
)
3378 wxGridTableBase::wxGridTableBase()
3380 m_view
= (wxGrid
*) NULL
;
3381 m_attrProvider
= (wxGridCellAttrProvider
*) NULL
;
3384 wxGridTableBase::~wxGridTableBase()
3386 delete m_attrProvider
;
3389 void wxGridTableBase::SetAttrProvider(wxGridCellAttrProvider
*attrProvider
)
3391 delete m_attrProvider
;
3392 m_attrProvider
= attrProvider
;
3395 bool wxGridTableBase::CanHaveAttributes()
3397 if ( ! GetAttrProvider() )
3399 // use the default attr provider by default
3400 SetAttrProvider(new wxGridCellAttrProvider
);
3406 wxGridCellAttr
*wxGridTableBase::GetAttr(int row
, int col
, wxGridCellAttr::wxAttrKind kind
)
3408 if ( m_attrProvider
)
3409 return m_attrProvider
->GetAttr(row
, col
, kind
);
3411 return (wxGridCellAttr
*)NULL
;
3414 void wxGridTableBase::SetAttr(wxGridCellAttr
* attr
, int row
, int col
)
3416 if ( m_attrProvider
)
3419 attr
->SetKind(wxGridCellAttr::Cell
);
3420 m_attrProvider
->SetAttr(attr
, row
, col
);
3424 // as we take ownership of the pointer and don't store it, we must
3430 void wxGridTableBase::SetRowAttr(wxGridCellAttr
*attr
, int row
)
3432 if ( m_attrProvider
)
3434 attr
->SetKind(wxGridCellAttr::Row
);
3435 m_attrProvider
->SetRowAttr(attr
, row
);
3439 // as we take ownership of the pointer and don't store it, we must
3445 void wxGridTableBase::SetColAttr(wxGridCellAttr
*attr
, int col
)
3447 if ( m_attrProvider
)
3449 attr
->SetKind(wxGridCellAttr::Col
);
3450 m_attrProvider
->SetColAttr(attr
, col
);
3454 // as we take ownership of the pointer and don't store it, we must
3460 bool wxGridTableBase::InsertRows( size_t WXUNUSED(pos
),
3461 size_t WXUNUSED(numRows
) )
3463 wxFAIL_MSG( wxT("Called grid table class function InsertRows\nbut your derived table class does not override this function") );
3468 bool wxGridTableBase::AppendRows( size_t WXUNUSED(numRows
) )
3470 wxFAIL_MSG( wxT("Called grid table class function AppendRows\nbut your derived table class does not override this function"));
3475 bool wxGridTableBase::DeleteRows( size_t WXUNUSED(pos
),
3476 size_t WXUNUSED(numRows
) )
3478 wxFAIL_MSG( wxT("Called grid table class function DeleteRows\nbut your derived table class does not override this function"));
3483 bool wxGridTableBase::InsertCols( size_t WXUNUSED(pos
),
3484 size_t WXUNUSED(numCols
) )
3486 wxFAIL_MSG( wxT("Called grid table class function InsertCols\nbut your derived table class does not override this function"));
3491 bool wxGridTableBase::AppendCols( size_t WXUNUSED(numCols
) )
3493 wxFAIL_MSG(wxT("Called grid table class function AppendCols\nbut your derived table class does not override this function"));
3498 bool wxGridTableBase::DeleteCols( size_t WXUNUSED(pos
),
3499 size_t WXUNUSED(numCols
) )
3501 wxFAIL_MSG( wxT("Called grid table class function DeleteCols\nbut your derived table class does not override this function"));
3506 wxString
wxGridTableBase::GetRowLabelValue( int row
)
3510 // RD: Starting the rows at zero confuses users,
3511 // no matter how much it makes sense to us geeks.
3517 wxString
wxGridTableBase::GetColLabelValue( int col
)
3519 // default col labels are:
3520 // cols 0 to 25 : A-Z
3521 // cols 26 to 675 : AA-ZZ
3526 for ( n
= 1; ; n
++ )
3528 s
+= (wxChar
) (_T('A') + (wxChar
)(col
% 26));
3534 // reverse the string...
3536 for ( i
= 0; i
< n
; i
++ )
3544 wxString
wxGridTableBase::GetTypeName( int WXUNUSED(row
), int WXUNUSED(col
) )
3546 return wxGRID_VALUE_STRING
;
3549 bool wxGridTableBase::CanGetValueAs( int WXUNUSED(row
), int WXUNUSED(col
),
3550 const wxString
& typeName
)
3552 return typeName
== wxGRID_VALUE_STRING
;
3555 bool wxGridTableBase::CanSetValueAs( int row
, int col
, const wxString
& typeName
)
3557 return CanGetValueAs(row
, col
, typeName
);
3560 long wxGridTableBase::GetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
) )
3565 double wxGridTableBase::GetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
) )
3570 bool wxGridTableBase::GetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
) )
3575 void wxGridTableBase::SetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
),
3576 long WXUNUSED(value
) )
3580 void wxGridTableBase::SetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
),
3581 double WXUNUSED(value
) )
3585 void wxGridTableBase::SetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
),
3586 bool WXUNUSED(value
) )
3590 void* wxGridTableBase::GetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
3591 const wxString
& WXUNUSED(typeName
) )
3596 void wxGridTableBase::SetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
3597 const wxString
& WXUNUSED(typeName
),
3598 void* WXUNUSED(value
) )
3602 //////////////////////////////////////////////////////////////////////
3604 // Message class for the grid table to send requests and notifications
3608 wxGridTableMessage::wxGridTableMessage()
3610 m_table
= (wxGridTableBase
*) NULL
;
3616 wxGridTableMessage::wxGridTableMessage( wxGridTableBase
*table
, int id
,
3617 int commandInt1
, int commandInt2
)
3621 m_comInt1
= commandInt1
;
3622 m_comInt2
= commandInt2
;
3625 //////////////////////////////////////////////////////////////////////
3627 // A basic grid table for string data. An object of this class will
3628 // created by wxGrid if you don't specify an alternative table class.
3631 WX_DEFINE_OBJARRAY(wxGridStringArray
)
3633 IMPLEMENT_DYNAMIC_CLASS( wxGridStringTable
, wxGridTableBase
)
3635 wxGridStringTable::wxGridStringTable()
3640 wxGridStringTable::wxGridStringTable( int numRows
, int numCols
)
3643 m_data
.Alloc( numRows
);
3646 sa
.Alloc( numCols
);
3647 sa
.Add( wxEmptyString
, numCols
);
3649 m_data
.Add( sa
, numRows
);
3652 wxGridStringTable::~wxGridStringTable()
3656 int wxGridStringTable::GetNumberRows()
3658 return m_data
.GetCount();
3661 int wxGridStringTable::GetNumberCols()
3663 if ( m_data
.GetCount() > 0 )
3664 return m_data
[0].GetCount();
3669 wxString
wxGridStringTable::GetValue( int row
, int col
)
3671 wxCHECK_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3673 _T("invalid row or column index in wxGridStringTable") );
3675 return m_data
[row
][col
];
3678 void wxGridStringTable::SetValue( int row
, int col
, const wxString
& value
)
3680 wxCHECK_RET( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3681 _T("invalid row or column index in wxGridStringTable") );
3683 m_data
[row
][col
] = value
;
3686 bool wxGridStringTable::IsEmptyCell( int row
, int col
)
3688 wxCHECK_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3690 _T("invalid row or column index in wxGridStringTable") );
3692 return (m_data
[row
][col
] == wxEmptyString
);
3695 void wxGridStringTable::Clear()
3698 int numRows
, numCols
;
3700 numRows
= m_data
.GetCount();
3703 numCols
= m_data
[0].GetCount();
3705 for ( row
= 0; row
< numRows
; row
++ )
3707 for ( col
= 0; col
< numCols
; col
++ )
3709 m_data
[row
][col
] = wxEmptyString
;
3715 bool wxGridStringTable::InsertRows( size_t pos
, size_t numRows
)
3717 size_t curNumRows
= m_data
.GetCount();
3718 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3719 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3721 if ( pos
>= curNumRows
)
3723 return AppendRows( numRows
);
3727 sa
.Alloc( curNumCols
);
3728 sa
.Add( wxEmptyString
, curNumCols
);
3729 m_data
.Insert( sa
, pos
, numRows
);
3733 wxGridTableMessage
msg( this,
3734 wxGRIDTABLE_NOTIFY_ROWS_INSERTED
,
3738 GetView()->ProcessTableMessage( msg
);
3744 bool wxGridStringTable::AppendRows( size_t numRows
)
3746 size_t curNumRows
= m_data
.GetCount();
3747 size_t curNumCols
= ( curNumRows
> 0
3748 ? m_data
[0].GetCount()
3749 : ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3752 if ( curNumCols
> 0 )
3754 sa
.Alloc( curNumCols
);
3755 sa
.Add( wxEmptyString
, curNumCols
);
3758 m_data
.Add( sa
, numRows
);
3762 wxGridTableMessage
msg( this,
3763 wxGRIDTABLE_NOTIFY_ROWS_APPENDED
,
3766 GetView()->ProcessTableMessage( msg
);
3772 bool wxGridStringTable::DeleteRows( size_t pos
, size_t numRows
)
3774 size_t curNumRows
= m_data
.GetCount();
3776 if ( pos
>= curNumRows
)
3778 wxFAIL_MSG( wxString::Format
3780 wxT("Called wxGridStringTable::DeleteRows(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu rows"),
3782 (unsigned long)numRows
,
3783 (unsigned long)curNumRows
3789 if ( numRows
> curNumRows
- pos
)
3791 numRows
= curNumRows
- pos
;
3794 if ( numRows
>= curNumRows
)
3800 m_data
.RemoveAt( pos
, numRows
);
3805 wxGridTableMessage
msg( this,
3806 wxGRIDTABLE_NOTIFY_ROWS_DELETED
,
3810 GetView()->ProcessTableMessage( msg
);
3816 bool wxGridStringTable::InsertCols( size_t pos
, size_t numCols
)
3820 size_t curNumRows
= m_data
.GetCount();
3821 size_t curNumCols
= ( curNumRows
> 0
3822 ? m_data
[0].GetCount()
3823 : ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3825 if ( pos
>= curNumCols
)
3827 return AppendCols( numCols
);
3830 if ( !m_colLabels
.IsEmpty() )
3832 m_colLabels
.Insert( wxEmptyString
, pos
, numCols
);
3835 for ( i
= pos
; i
< pos
+ numCols
; i
++ )
3836 m_colLabels
[i
] = wxGridTableBase::GetColLabelValue( i
);
3839 for ( row
= 0; row
< curNumRows
; row
++ )
3841 for ( col
= pos
; col
< pos
+ numCols
; col
++ )
3843 m_data
[row
].Insert( wxEmptyString
, col
);
3849 wxGridTableMessage
msg( this,
3850 wxGRIDTABLE_NOTIFY_COLS_INSERTED
,
3854 GetView()->ProcessTableMessage( msg
);
3860 bool wxGridStringTable::AppendCols( size_t numCols
)
3864 size_t curNumRows
= m_data
.GetCount();
3866 for ( row
= 0; row
< curNumRows
; row
++ )
3868 m_data
[row
].Add( wxEmptyString
, numCols
);
3873 wxGridTableMessage
msg( this,
3874 wxGRIDTABLE_NOTIFY_COLS_APPENDED
,
3877 GetView()->ProcessTableMessage( msg
);
3883 bool wxGridStringTable::DeleteCols( size_t pos
, size_t numCols
)
3887 size_t curNumRows
= m_data
.GetCount();
3888 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3889 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3891 if ( pos
>= curNumCols
)
3893 wxFAIL_MSG( wxString::Format
3895 wxT("Called wxGridStringTable::DeleteCols(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu cols"),
3897 (unsigned long)numCols
,
3898 (unsigned long)curNumCols
3905 colID
= GetView()->GetColAt( pos
);
3909 if ( numCols
> curNumCols
- colID
)
3911 numCols
= curNumCols
- colID
;
3914 if ( !m_colLabels
.IsEmpty() )
3916 // m_colLabels stores just as many elements as it needs, e.g. if only
3917 // the label of the first column had been set it would have only one
3918 // element and not numCols, so account for it
3919 int nToRm
= m_colLabels
.size() - colID
;
3921 m_colLabels
.RemoveAt( colID
, nToRm
);
3924 for ( row
= 0; row
< curNumRows
; row
++ )
3926 if ( numCols
>= curNumCols
)
3928 m_data
[row
].Clear();
3932 m_data
[row
].RemoveAt( colID
, numCols
);
3938 wxGridTableMessage
msg( this,
3939 wxGRIDTABLE_NOTIFY_COLS_DELETED
,
3943 GetView()->ProcessTableMessage( msg
);
3949 wxString
wxGridStringTable::GetRowLabelValue( int row
)
3951 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
3953 // using default label
3955 return wxGridTableBase::GetRowLabelValue( row
);
3959 return m_rowLabels
[row
];
3963 wxString
wxGridStringTable::GetColLabelValue( int col
)
3965 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
3967 // using default label
3969 return wxGridTableBase::GetColLabelValue( col
);
3973 return m_colLabels
[col
];
3977 void wxGridStringTable::SetRowLabelValue( int row
, const wxString
& value
)
3979 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
3981 int n
= m_rowLabels
.GetCount();
3984 for ( i
= n
; i
<= row
; i
++ )
3986 m_rowLabels
.Add( wxGridTableBase::GetRowLabelValue(i
) );
3990 m_rowLabels
[row
] = value
;
3993 void wxGridStringTable::SetColLabelValue( int col
, const wxString
& value
)
3995 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
3997 int n
= m_colLabels
.GetCount();
4000 for ( i
= n
; i
<= col
; i
++ )
4002 m_colLabels
.Add( wxGridTableBase::GetColLabelValue(i
) );
4006 m_colLabels
[col
] = value
;
4010 //////////////////////////////////////////////////////////////////////
4011 //////////////////////////////////////////////////////////////////////
4013 BEGIN_EVENT_TABLE(wxGridSubwindow
, wxWindow
)
4014 EVT_MOUSE_CAPTURE_LOST(wxGridSubwindow::OnMouseCaptureLost
)
4017 void wxGridSubwindow::OnMouseCaptureLost(wxMouseCaptureLostEvent
& WXUNUSED(event
))
4019 m_owner
->CancelMouseCapture();
4022 IMPLEMENT_DYNAMIC_CLASS( wxGridRowLabelWindow
, wxWindow
)
4024 BEGIN_EVENT_TABLE( wxGridRowLabelWindow
, wxGridSubwindow
)
4025 EVT_PAINT( wxGridRowLabelWindow::OnPaint
)
4026 EVT_MOUSEWHEEL( wxGridRowLabelWindow::OnMouseWheel
)
4027 EVT_MOUSE_EVENTS( wxGridRowLabelWindow::OnMouseEvent
)
4030 wxGridRowLabelWindow::wxGridRowLabelWindow( wxGrid
*parent
,
4032 const wxPoint
&pos
, const wxSize
&size
)
4033 : wxGridSubwindow(parent
, id
, pos
, size
)
4038 void wxGridRowLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
4042 // NO - don't do this because it will set both the x and y origin
4043 // coords to match the parent scrolled window and we just want to
4044 // set the y coord - MB
4046 // m_owner->PrepareDC( dc );
4049 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
4050 wxPoint pt
= dc
.GetDeviceOrigin();
4051 dc
.SetDeviceOrigin( pt
.x
, pt
.y
-y
);
4053 wxArrayInt rows
= m_owner
->CalcRowLabelsExposed( GetUpdateRegion() );
4054 m_owner
->DrawRowLabels( dc
, rows
);
4057 void wxGridRowLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
4059 m_owner
->ProcessRowLabelMouseEvent( event
);
4062 void wxGridRowLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
4064 m_owner
->GetEventHandler()->ProcessEvent( event
);
4067 //////////////////////////////////////////////////////////////////////
4069 IMPLEMENT_DYNAMIC_CLASS( wxGridColLabelWindow
, wxWindow
)
4071 BEGIN_EVENT_TABLE( wxGridColLabelWindow
, wxGridSubwindow
)
4072 EVT_PAINT( wxGridColLabelWindow::OnPaint
)
4073 EVT_MOUSEWHEEL( wxGridColLabelWindow::OnMouseWheel
)
4074 EVT_MOUSE_EVENTS( wxGridColLabelWindow::OnMouseEvent
)
4077 wxGridColLabelWindow::wxGridColLabelWindow( wxGrid
*parent
,
4079 const wxPoint
&pos
, const wxSize
&size
)
4080 : wxGridSubwindow(parent
, id
, pos
, size
)
4085 void wxGridColLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
4089 // NO - don't do this because it will set both the x and y origin
4090 // coords to match the parent scrolled window and we just want to
4091 // set the x coord - MB
4093 // m_owner->PrepareDC( dc );
4096 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
4097 wxPoint pt
= dc
.GetDeviceOrigin();
4098 if (GetLayoutDirection() == wxLayout_RightToLeft
)
4099 dc
.SetDeviceOrigin( pt
.x
+x
, pt
.y
);
4101 dc
.SetDeviceOrigin( pt
.x
-x
, pt
.y
);
4103 wxArrayInt cols
= m_owner
->CalcColLabelsExposed( GetUpdateRegion() );
4104 m_owner
->DrawColLabels( dc
, cols
);
4107 void wxGridColLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
4109 m_owner
->ProcessColLabelMouseEvent( event
);
4112 void wxGridColLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
4114 m_owner
->GetEventHandler()->ProcessEvent( event
);
4117 //////////////////////////////////////////////////////////////////////
4119 IMPLEMENT_DYNAMIC_CLASS( wxGridCornerLabelWindow
, wxWindow
)
4121 BEGIN_EVENT_TABLE( wxGridCornerLabelWindow
, wxGridSubwindow
)
4122 EVT_MOUSEWHEEL( wxGridCornerLabelWindow::OnMouseWheel
)
4123 EVT_MOUSE_EVENTS( wxGridCornerLabelWindow::OnMouseEvent
)
4124 EVT_PAINT( wxGridCornerLabelWindow::OnPaint
)
4127 wxGridCornerLabelWindow::wxGridCornerLabelWindow( wxGrid
*parent
,
4130 const wxSize
& size
)
4131 : wxGridSubwindow(parent
, id
, pos
, size
)
4136 void wxGridCornerLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
4140 m_owner
->DrawCornerLabel(dc
);
4143 void wxGridCornerLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
4145 m_owner
->ProcessCornerLabelMouseEvent( event
);
4148 void wxGridCornerLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
4150 m_owner
->GetEventHandler()->ProcessEvent(event
);
4153 //////////////////////////////////////////////////////////////////////
4155 IMPLEMENT_DYNAMIC_CLASS( wxGridWindow
, wxWindow
)
4157 BEGIN_EVENT_TABLE( wxGridWindow
, wxGridSubwindow
)
4158 EVT_PAINT( wxGridWindow::OnPaint
)
4159 EVT_MOUSEWHEEL( wxGridWindow::OnMouseWheel
)
4160 EVT_MOUSE_EVENTS( wxGridWindow::OnMouseEvent
)
4161 EVT_KEY_DOWN( wxGridWindow::OnKeyDown
)
4162 EVT_KEY_UP( wxGridWindow::OnKeyUp
)
4163 EVT_CHAR( wxGridWindow::OnChar
)
4164 EVT_SET_FOCUS( wxGridWindow::OnFocus
)
4165 EVT_KILL_FOCUS( wxGridWindow::OnFocus
)
4166 EVT_ERASE_BACKGROUND( wxGridWindow::OnEraseBackground
)
4169 wxGridWindow::wxGridWindow( wxGrid
*parent
,
4170 wxGridRowLabelWindow
*rowLblWin
,
4171 wxGridColLabelWindow
*colLblWin
,
4174 const wxSize
&size
)
4175 : wxGridSubwindow(parent
, id
, pos
, size
,
4176 wxWANTS_CHARS
| wxCLIP_CHILDREN
,
4177 wxT("grid window") )
4180 m_rowLabelWin
= rowLblWin
;
4181 m_colLabelWin
= colLblWin
;
4184 void wxGridWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
4186 wxPaintDC
dc( this );
4187 m_owner
->PrepareDC( dc
);
4188 wxRegion reg
= GetUpdateRegion();
4189 wxGridCellCoordsArray dirtyCells
= m_owner
->CalcCellsExposed( reg
);
4190 m_owner
->DrawGridCellArea( dc
, dirtyCells
);
4192 m_owner
->DrawAllGridLines( dc
, reg
);
4194 m_owner
->DrawGridSpace( dc
);
4195 m_owner
->DrawHighlight( dc
, dirtyCells
);
4198 void wxGridWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
4200 wxWindow::ScrollWindow( dx
, dy
, rect
);
4201 m_rowLabelWin
->ScrollWindow( 0, dy
, rect
);
4202 m_colLabelWin
->ScrollWindow( dx
, 0, rect
);
4205 void wxGridWindow::OnMouseEvent( wxMouseEvent
& event
)
4207 if (event
.ButtonDown(wxMOUSE_BTN_LEFT
) && FindFocus() != this)
4210 m_owner
->ProcessGridCellMouseEvent( event
);
4213 void wxGridWindow::OnMouseWheel( wxMouseEvent
& event
)
4215 m_owner
->GetEventHandler()->ProcessEvent( event
);
4218 // This seems to be required for wxMotif/wxGTK otherwise the mouse
4219 // cursor must be in the cell edit control to get key events
4221 void wxGridWindow::OnKeyDown( wxKeyEvent
& event
)
4223 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
4227 void wxGridWindow::OnKeyUp( wxKeyEvent
& event
)
4229 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
4233 void wxGridWindow::OnChar( wxKeyEvent
& event
)
4235 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
4239 void wxGridWindow::OnEraseBackground( wxEraseEvent
& WXUNUSED(event
) )
4243 void wxGridWindow::OnFocus(wxFocusEvent
& event
)
4245 // and if we have any selection, it has to be repainted, because it
4246 // uses different colour when the grid is not focused:
4247 if ( m_owner
->IsSelection() )
4253 // NB: Note that this code is in "else" branch only because the other
4254 // branch refreshes everything and so there's no point in calling
4255 // Refresh() again, *not* because it should only be done if
4256 // !IsSelection(). If the above code is ever optimized to refresh
4257 // only selected area, this needs to be moved out of the "else"
4258 // branch so that it's always executed.
4260 // current cell cursor {dis,re}appears on focus change:
4261 const wxGridCellCoords
cursorCoords(m_owner
->GetGridCursorRow(),
4262 m_owner
->GetGridCursorCol());
4263 const wxRect cursor
=
4264 m_owner
->BlockToDeviceRect(cursorCoords
, cursorCoords
);
4265 Refresh(true, &cursor
);
4268 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
4272 #define internalXToCol(x) XToCol(x, true)
4273 #define internalYToRow(y) YToRow(y, true)
4275 /////////////////////////////////////////////////////////////////////
4277 #if wxUSE_EXTENDED_RTTI
4278 WX_DEFINE_FLAGS( wxGridStyle
)
4280 wxBEGIN_FLAGS( wxGridStyle
)
4281 // new style border flags, we put them first to
4282 // use them for streaming out
4283 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
4284 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
4285 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
4286 wxFLAGS_MEMBER(wxBORDER_RAISED
)
4287 wxFLAGS_MEMBER(wxBORDER_STATIC
)
4288 wxFLAGS_MEMBER(wxBORDER_NONE
)
4290 // old style border flags
4291 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
4292 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
4293 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
4294 wxFLAGS_MEMBER(wxRAISED_BORDER
)
4295 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
4296 wxFLAGS_MEMBER(wxBORDER
)
4298 // standard window styles
4299 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
4300 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
4301 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
4302 wxFLAGS_MEMBER(wxWANTS_CHARS
)
4303 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE
)
4304 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
4305 wxFLAGS_MEMBER(wxVSCROLL
)
4306 wxFLAGS_MEMBER(wxHSCROLL
)
4308 wxEND_FLAGS( wxGridStyle
)
4310 IMPLEMENT_DYNAMIC_CLASS_XTI(wxGrid
, wxScrolledWindow
,"wx/grid.h")
4312 wxBEGIN_PROPERTIES_TABLE(wxGrid
)
4313 wxHIDE_PROPERTY( Children
)
4314 wxPROPERTY_FLAGS( WindowStyle
, wxGridStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
4315 wxEND_PROPERTIES_TABLE()
4317 wxBEGIN_HANDLERS_TABLE(wxGrid
)
4318 wxEND_HANDLERS_TABLE()
4320 wxCONSTRUCTOR_5( wxGrid
, wxWindow
* , Parent
, wxWindowID
, Id
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
4323 TODO : Expose more information of a list's layout, etc. via appropriate objects (e.g., NotebookPageInfo)
4326 IMPLEMENT_DYNAMIC_CLASS( wxGrid
, wxScrolledWindow
)
4329 BEGIN_EVENT_TABLE( wxGrid
, wxScrolledWindow
)
4330 EVT_PAINT( wxGrid::OnPaint
)
4331 EVT_SIZE( wxGrid::OnSize
)
4332 EVT_KEY_DOWN( wxGrid::OnKeyDown
)
4333 EVT_KEY_UP( wxGrid::OnKeyUp
)
4334 EVT_CHAR ( wxGrid::OnChar
)
4335 EVT_ERASE_BACKGROUND( wxGrid::OnEraseBackground
)
4343 wxGrid::wxGrid( wxWindow
*parent
,
4348 const wxString
& name
)
4351 Create(parent
, id
, pos
, size
, style
, name
);
4354 bool wxGrid::Create(wxWindow
*parent
, wxWindowID id
,
4355 const wxPoint
& pos
, const wxSize
& size
,
4356 long style
, const wxString
& name
)
4358 if (!wxScrolledWindow::Create(parent
, id
, pos
, size
,
4359 style
| wxWANTS_CHARS
, name
))
4362 m_colMinWidths
= wxLongToLongHashMap(GRID_HASH_SIZE
);
4363 m_rowMinHeights
= wxLongToLongHashMap(GRID_HASH_SIZE
);
4366 SetInitialSize(size
);
4367 SetScrollRate(m_scrollLineX
, m_scrollLineY
);
4375 // Must do this or ~wxScrollHelper will pop the wrong event handler
4376 SetTargetWindow(this);
4378 wxSafeDecRef(m_defaultCellAttr
);
4380 #ifdef DEBUG_ATTR_CACHE
4381 size_t total
= gs_nAttrCacheHits
+ gs_nAttrCacheMisses
;
4382 wxPrintf(_T("wxGrid attribute cache statistics: "
4383 "total: %u, hits: %u (%u%%)\n"),
4384 total
, gs_nAttrCacheHits
,
4385 total
? (gs_nAttrCacheHits
*100) / total
: 0);
4388 // if we own the table, just delete it, otherwise at least don't leave it
4389 // with dangling view pointer
4392 else if ( m_table
&& m_table
->GetView() == this )
4393 m_table
->SetView(NULL
);
4395 delete m_typeRegistry
;
4400 // ----- internal init and update functions
4403 // NOTE: If using the default visual attributes works everywhere then this can
4404 // be removed as well as the #else cases below.
4405 #define _USE_VISATTR 0
4407 void wxGrid::Create()
4409 // create the type registry
4410 m_typeRegistry
= new wxGridTypeRegistry
;
4412 m_cellEditCtrlEnabled
= false;
4414 m_defaultCellAttr
= new wxGridCellAttr();
4416 // Set default cell attributes
4417 m_defaultCellAttr
->SetDefAttr(m_defaultCellAttr
);
4418 m_defaultCellAttr
->SetKind(wxGridCellAttr::Default
);
4419 m_defaultCellAttr
->SetFont(GetFont());
4420 m_defaultCellAttr
->SetAlignment(wxALIGN_LEFT
, wxALIGN_TOP
);
4421 m_defaultCellAttr
->SetRenderer(new wxGridCellStringRenderer
);
4422 m_defaultCellAttr
->SetEditor(new wxGridCellTextEditor
);
4425 wxVisualAttributes gva
= wxListBox::GetClassDefaultAttributes();
4426 wxVisualAttributes lva
= wxPanel::GetClassDefaultAttributes();
4428 m_defaultCellAttr
->SetTextColour(gva
.colFg
);
4429 m_defaultCellAttr
->SetBackgroundColour(gva
.colBg
);
4432 m_defaultCellAttr
->SetTextColour(
4433 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
4434 m_defaultCellAttr
->SetBackgroundColour(
4435 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
4440 m_currentCellCoords
= wxGridNoCellCoords
;
4442 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
4443 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
4445 // subwindow components that make up the wxGrid
4446 m_rowLabelWin
= new wxGridRowLabelWindow( this,
4451 m_colLabelWin
= new wxGridColLabelWindow( this,
4456 m_cornerLabelWin
= new wxGridCornerLabelWindow( this,
4461 m_gridWin
= new wxGridWindow( this,
4468 SetTargetWindow( m_gridWin
);
4471 wxColour gfg
= gva
.colFg
;
4472 wxColour gbg
= gva
.colBg
;
4473 wxColour lfg
= lva
.colFg
;
4474 wxColour lbg
= lva
.colBg
;
4476 wxColour gfg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT
);
4477 wxColour gbg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOW
);
4478 wxColour lfg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT
);
4479 wxColour lbg
= wxSystemSettings::GetColour( wxSYS_COLOUR_BTNFACE
);
4482 m_cornerLabelWin
->SetOwnForegroundColour(lfg
);
4483 m_cornerLabelWin
->SetOwnBackgroundColour(lbg
);
4484 m_rowLabelWin
->SetOwnForegroundColour(lfg
);
4485 m_rowLabelWin
->SetOwnBackgroundColour(lbg
);
4486 m_colLabelWin
->SetOwnForegroundColour(lfg
);
4487 m_colLabelWin
->SetOwnBackgroundColour(lbg
);
4489 m_gridWin
->SetOwnForegroundColour(gfg
);
4490 m_gridWin
->SetOwnBackgroundColour(gbg
);
4495 bool wxGrid::CreateGrid( int numRows
, int numCols
,
4496 wxGridSelectionModes selmode
)
4498 wxCHECK_MSG( !m_created
,
4500 wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
4502 return SetTable(new wxGridStringTable(numRows
, numCols
), true, selmode
);
4505 void wxGrid::SetSelectionMode(wxGridSelectionModes selmode
)
4507 wxCHECK_RET( m_created
,
4508 wxT("Called wxGrid::SetSelectionMode() before calling CreateGrid()") );
4510 m_selection
->SetSelectionMode( selmode
);
4513 wxGrid::wxGridSelectionModes
wxGrid::GetSelectionMode() const
4515 wxCHECK_MSG( m_created
, wxGridSelectCells
,
4516 wxT("Called wxGrid::GetSelectionMode() before calling CreateGrid()") );
4518 return m_selection
->GetSelectionMode();
4522 wxGrid::SetTable(wxGridTableBase
*table
,
4524 wxGrid::wxGridSelectionModes selmode
)
4526 bool checkSelection
= false;
4529 // stop all processing
4534 m_table
->SetView(0);
4546 checkSelection
= true;
4548 // kill row and column size arrays
4549 m_colWidths
.Empty();
4550 m_colRights
.Empty();
4551 m_rowHeights
.Empty();
4552 m_rowBottoms
.Empty();
4557 m_numRows
= table
->GetNumberRows();
4558 m_numCols
= table
->GetNumberCols();
4561 m_table
->SetView( this );
4562 m_ownTable
= takeOwnership
;
4563 m_selection
= new wxGridSelection( this, selmode
);
4566 // If the newly set table is smaller than the
4567 // original one current cell and selection regions
4568 // might be invalid,
4569 m_selectingKeyboard
= wxGridNoCellCoords
;
4570 m_currentCellCoords
=
4571 wxGridCellCoords(wxMin(m_numRows
, m_currentCellCoords
.GetRow()),
4572 wxMin(m_numCols
, m_currentCellCoords
.GetCol()));
4573 if (m_selectingTopLeft
.GetRow() >= m_numRows
||
4574 m_selectingTopLeft
.GetCol() >= m_numCols
)
4576 m_selectingTopLeft
= wxGridNoCellCoords
;
4577 m_selectingBottomRight
= wxGridNoCellCoords
;
4580 m_selectingBottomRight
=
4581 wxGridCellCoords(wxMin(m_numRows
,
4582 m_selectingBottomRight
.GetRow()),
4584 m_selectingBottomRight
.GetCol()));
4594 void wxGrid::InitVars()
4598 m_cornerLabelWin
= NULL
;
4599 m_rowLabelWin
= NULL
;
4600 m_colLabelWin
= NULL
;
4607 m_defaultCellAttr
= NULL
;
4608 m_typeRegistry
= NULL
;
4609 m_winCapture
= NULL
;
4614 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
4615 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
4617 if ( m_rowLabelWin
)
4619 m_labelBackgroundColour
= m_rowLabelWin
->GetBackgroundColour();
4623 m_labelBackgroundColour
= *wxWHITE
;
4626 m_labelTextColour
= *wxBLACK
;
4629 m_attrCache
.row
= -1;
4630 m_attrCache
.col
= -1;
4631 m_attrCache
.attr
= NULL
;
4633 m_labelFont
= GetFont();
4634 m_labelFont
.SetWeight( wxBOLD
);
4636 m_rowLabelHorizAlign
= wxALIGN_CENTRE
;
4637 m_rowLabelVertAlign
= wxALIGN_CENTRE
;
4639 m_colLabelHorizAlign
= wxALIGN_CENTRE
;
4640 m_colLabelVertAlign
= wxALIGN_CENTRE
;
4641 m_colLabelTextOrientation
= wxHORIZONTAL
;
4643 m_defaultColWidth
= WXGRID_DEFAULT_COL_WIDTH
;
4644 m_defaultRowHeight
= m_gridWin
->GetCharHeight();
4646 m_minAcceptableColWidth
= WXGRID_MIN_COL_WIDTH
;
4647 m_minAcceptableRowHeight
= WXGRID_MIN_ROW_HEIGHT
;
4649 #if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl()
4650 m_defaultRowHeight
+= 8;
4652 m_defaultRowHeight
+= 4;
4655 m_gridLineColour
= wxColour( 192,192,192 );
4656 m_gridLinesEnabled
= true;
4657 m_cellHighlightColour
= *wxBLACK
;
4658 m_cellHighlightPenWidth
= 2;
4659 m_cellHighlightROPenWidth
= 1;
4661 m_canDragColMove
= false;
4663 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
4664 m_winCapture
= (wxWindow
*)NULL
;
4665 m_canDragRowSize
= true;
4666 m_canDragColSize
= true;
4667 m_canDragGridSize
= true;
4668 m_canDragCell
= false;
4670 m_dragRowOrCol
= -1;
4671 m_isDragging
= false;
4672 m_startDragPos
= wxDefaultPosition
;
4673 m_nativeColumnLabels
= false;
4675 m_waitForSlowClick
= false;
4677 m_rowResizeCursor
= wxCursor( wxCURSOR_SIZENS
);
4678 m_colResizeCursor
= wxCursor( wxCURSOR_SIZEWE
);
4680 m_currentCellCoords
= wxGridNoCellCoords
;
4684 m_selectionBackground
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
);
4685 m_selectionForeground
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
4687 m_editable
= true; // default for whole grid
4689 m_inOnKeyDown
= false;
4695 m_scrollLineX
= GRID_SCROLL_LINE_X
;
4696 m_scrollLineY
= GRID_SCROLL_LINE_Y
;
4699 // ----------------------------------------------------------------------------
4700 // the idea is to call these functions only when necessary because they create
4701 // quite big arrays which eat memory mostly unnecessary - in particular, if
4702 // default widths/heights are used for all rows/columns, we may not use these
4705 // with some extra code, it should be possible to only store the widths/heights
4706 // different from default ones (resulting in space savings for huge grids) but
4707 // this is not done currently
4708 // ----------------------------------------------------------------------------
4710 void wxGrid::InitRowHeights()
4712 m_rowHeights
.Empty();
4713 m_rowBottoms
.Empty();
4715 m_rowHeights
.Alloc( m_numRows
);
4716 m_rowBottoms
.Alloc( m_numRows
);
4718 m_rowHeights
.Add( m_defaultRowHeight
, m_numRows
);
4721 for ( int i
= 0; i
< m_numRows
; i
++ )
4723 rowBottom
+= m_defaultRowHeight
;
4724 m_rowBottoms
.Add( rowBottom
);
4728 void wxGrid::InitColWidths()
4730 m_colWidths
.Empty();
4731 m_colRights
.Empty();
4733 m_colWidths
.Alloc( m_numCols
);
4734 m_colRights
.Alloc( m_numCols
);
4736 m_colWidths
.Add( m_defaultColWidth
, m_numCols
);
4739 for ( int i
= 0; i
< m_numCols
; i
++ )
4741 colRight
= ( GetColPos( i
) + 1 ) * m_defaultColWidth
;
4742 m_colRights
.Add( colRight
);
4746 int wxGrid::GetColWidth(int col
) const
4748 return m_colWidths
.IsEmpty() ? m_defaultColWidth
: m_colWidths
[col
];
4751 int wxGrid::GetColLeft(int col
) const
4753 return m_colRights
.IsEmpty() ? GetColPos( col
) * m_defaultColWidth
4754 : m_colRights
[col
] - m_colWidths
[col
];
4757 int wxGrid::GetColRight(int col
) const
4759 return m_colRights
.IsEmpty() ? (GetColPos( col
) + 1) * m_defaultColWidth
4763 int wxGrid::GetRowHeight(int row
) const
4765 return m_rowHeights
.IsEmpty() ? m_defaultRowHeight
: m_rowHeights
[row
];
4768 int wxGrid::GetRowTop(int row
) const
4770 return m_rowBottoms
.IsEmpty() ? row
* m_defaultRowHeight
4771 : m_rowBottoms
[row
] - m_rowHeights
[row
];
4774 int wxGrid::GetRowBottom(int row
) const
4776 return m_rowBottoms
.IsEmpty() ? (row
+ 1) * m_defaultRowHeight
4777 : m_rowBottoms
[row
];
4780 void wxGrid::CalcDimensions()
4782 // compute the size of the scrollable area
4783 int w
= m_numCols
> 0 ? GetColRight(GetColAt(m_numCols
- 1)) : 0;
4784 int h
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) : 0;
4789 // take into account editor if shown
4790 if ( IsCellEditControlShown() )
4793 int r
= m_currentCellCoords
.GetRow();
4794 int c
= m_currentCellCoords
.GetCol();
4795 int x
= GetColLeft(c
);
4796 int y
= GetRowTop(r
);
4798 // how big is the editor
4799 wxGridCellAttr
* attr
= GetCellAttr(r
, c
);
4800 wxGridCellEditor
* editor
= attr
->GetEditor(this, r
, c
);
4801 editor
->GetControl()->GetSize(&w2
, &h2
);
4812 // preserve (more or less) the previous position
4814 GetViewStart( &x
, &y
);
4816 // ensure the position is valid for the new scroll ranges
4818 x
= wxMax( w
- 1, 0 );
4820 y
= wxMax( h
- 1, 0 );
4822 // update the virtual size and refresh the scrollbars to reflect it
4823 m_gridWin
->SetVirtualSize(w
, h
);
4827 // if our OnSize() hadn't been called (it would if we have scrollbars), we
4828 // still must reposition the children
4832 wxSize
wxGrid::GetSizeAvailableForScrollTarget(const wxSize
& size
)
4834 wxSize
sizeGridWin(size
);
4835 sizeGridWin
.x
-= m_rowLabelWidth
;
4836 sizeGridWin
.y
-= m_colLabelHeight
;
4841 void wxGrid::CalcWindowSizes()
4843 // escape if the window is has not been fully created yet
4845 if ( m_cornerLabelWin
== NULL
)
4849 GetClientSize( &cw
, &ch
);
4851 // the grid may be too small to have enough space for the labels yet, don't
4852 // size the windows to negative sizes in this case
4853 int gw
= cw
- m_rowLabelWidth
;
4854 int gh
= ch
- m_colLabelHeight
;
4860 if ( m_cornerLabelWin
&& m_cornerLabelWin
->IsShown() )
4861 m_cornerLabelWin
->SetSize( 0, 0, m_rowLabelWidth
, m_colLabelHeight
);
4863 if ( m_colLabelWin
&& m_colLabelWin
->IsShown() )
4864 m_colLabelWin
->SetSize( m_rowLabelWidth
, 0, gw
, m_colLabelHeight
);
4866 if ( m_rowLabelWin
&& m_rowLabelWin
->IsShown() )
4867 m_rowLabelWin
->SetSize( 0, m_colLabelHeight
, m_rowLabelWidth
, gh
);
4869 if ( m_gridWin
&& m_gridWin
->IsShown() )
4870 m_gridWin
->SetSize( m_rowLabelWidth
, m_colLabelHeight
, gw
, gh
);
4873 // this is called when the grid table sends a message
4874 // to indicate that it has been redimensioned
4876 bool wxGrid::Redimension( wxGridTableMessage
& msg
)
4879 bool result
= false;
4881 // Clear the attribute cache as the attribute might refer to a different
4882 // cell than stored in the cache after adding/removing rows/columns.
4885 // By the same reasoning, the editor should be dismissed if columns are
4886 // added or removed. And for consistency, it should IMHO always be
4887 // removed, not only if the cell "underneath" it actually changes.
4888 // For now, I intentionally do not save the editor's content as the
4889 // cell it might want to save that stuff to might no longer exist.
4890 HideCellEditControl();
4893 // if we were using the default widths/heights so far, we must change them
4895 if ( m_colWidths
.IsEmpty() )
4900 if ( m_rowHeights
.IsEmpty() )
4906 switch ( msg
.GetId() )
4908 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
4910 size_t pos
= msg
.GetCommandInt();
4911 int numRows
= msg
.GetCommandInt2();
4913 m_numRows
+= numRows
;
4915 if ( !m_rowHeights
.IsEmpty() )
4917 m_rowHeights
.Insert( m_defaultRowHeight
, pos
, numRows
);
4918 m_rowBottoms
.Insert( 0, pos
, numRows
);
4922 bottom
= m_rowBottoms
[pos
- 1];
4924 for ( i
= pos
; i
< m_numRows
; i
++ )
4926 bottom
+= m_rowHeights
[i
];
4927 m_rowBottoms
[i
] = bottom
;
4931 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4933 // if we have just inserted cols into an empty grid the current
4934 // cell will be undefined...
4936 SetCurrentCell( 0, 0 );
4940 m_selection
->UpdateRows( pos
, numRows
);
4941 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4943 attrProvider
->UpdateAttrRows( pos
, numRows
);
4945 if ( !GetBatchCount() )
4948 m_rowLabelWin
->Refresh();
4954 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
4956 int numRows
= msg
.GetCommandInt();
4957 int oldNumRows
= m_numRows
;
4958 m_numRows
+= numRows
;
4960 if ( !m_rowHeights
.IsEmpty() )
4962 m_rowHeights
.Add( m_defaultRowHeight
, numRows
);
4963 m_rowBottoms
.Add( 0, numRows
);
4966 if ( oldNumRows
> 0 )
4967 bottom
= m_rowBottoms
[oldNumRows
- 1];
4969 for ( i
= oldNumRows
; i
< m_numRows
; i
++ )
4971 bottom
+= m_rowHeights
[i
];
4972 m_rowBottoms
[i
] = bottom
;
4976 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4978 // if we have just inserted cols into an empty grid the current
4979 // cell will be undefined...
4981 SetCurrentCell( 0, 0 );
4984 if ( !GetBatchCount() )
4987 m_rowLabelWin
->Refresh();
4993 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
4995 size_t pos
= msg
.GetCommandInt();
4996 int numRows
= msg
.GetCommandInt2();
4997 m_numRows
-= numRows
;
4999 if ( !m_rowHeights
.IsEmpty() )
5001 m_rowHeights
.RemoveAt( pos
, numRows
);
5002 m_rowBottoms
.RemoveAt( pos
, numRows
);
5005 for ( i
= 0; i
< m_numRows
; i
++ )
5007 h
+= m_rowHeights
[i
];
5008 m_rowBottoms
[i
] = h
;
5014 m_currentCellCoords
= wxGridNoCellCoords
;
5018 if ( m_currentCellCoords
.GetRow() >= m_numRows
)
5019 m_currentCellCoords
.Set( 0, 0 );
5023 m_selection
->UpdateRows( pos
, -((int)numRows
) );
5024 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
5027 attrProvider
->UpdateAttrRows( pos
, -((int)numRows
) );
5029 // ifdef'd out following patch from Paul Gammans
5031 // No need to touch column attributes, unless we
5032 // removed _all_ rows, in this case, we remove
5033 // all column attributes.
5034 // I hate to do this here, but the
5035 // needed data is not available inside UpdateAttrRows.
5036 if ( !GetNumberRows() )
5037 attrProvider
->UpdateAttrCols( 0, -GetNumberCols() );
5041 if ( !GetBatchCount() )
5044 m_rowLabelWin
->Refresh();
5050 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
5052 size_t pos
= msg
.GetCommandInt();
5053 int numCols
= msg
.GetCommandInt2();
5054 m_numCols
+= numCols
;
5056 if ( !m_colAt
.IsEmpty() )
5058 //Shift the column IDs
5060 for ( i
= 0; i
< m_numCols
- numCols
; i
++ )
5062 if ( m_colAt
[i
] >= (int)pos
)
5063 m_colAt
[i
] += numCols
;
5066 m_colAt
.Insert( pos
, pos
, numCols
);
5068 //Set the new columns' positions
5069 for ( i
= pos
+ 1; i
< (int)pos
+ numCols
; i
++ )
5075 if ( !m_colWidths
.IsEmpty() )
5077 m_colWidths
.Insert( m_defaultColWidth
, pos
, numCols
);
5078 m_colRights
.Insert( 0, pos
, numCols
);
5082 right
= m_colRights
[GetColAt( pos
- 1 )];
5085 for ( colPos
= pos
; colPos
< m_numCols
; colPos
++ )
5087 i
= GetColAt( colPos
);
5089 right
+= m_colWidths
[i
];
5090 m_colRights
[i
] = right
;
5094 if ( m_currentCellCoords
== wxGridNoCellCoords
)
5096 // if we have just inserted cols into an empty grid the current
5097 // cell will be undefined...
5099 SetCurrentCell( 0, 0 );
5103 m_selection
->UpdateCols( pos
, numCols
);
5104 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
5106 attrProvider
->UpdateAttrCols( pos
, numCols
);
5107 if ( !GetBatchCount() )
5110 m_colLabelWin
->Refresh();
5116 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
5118 int numCols
= msg
.GetCommandInt();
5119 int oldNumCols
= m_numCols
;
5120 m_numCols
+= numCols
;
5122 if ( !m_colAt
.IsEmpty() )
5124 m_colAt
.Add( 0, numCols
);
5126 //Set the new columns' positions
5128 for ( i
= oldNumCols
; i
< m_numCols
; i
++ )
5134 if ( !m_colWidths
.IsEmpty() )
5136 m_colWidths
.Add( m_defaultColWidth
, numCols
);
5137 m_colRights
.Add( 0, numCols
);
5140 if ( oldNumCols
> 0 )
5141 right
= m_colRights
[GetColAt( oldNumCols
- 1 )];
5144 for ( colPos
= oldNumCols
; colPos
< m_numCols
; colPos
++ )
5146 i
= GetColAt( colPos
);
5148 right
+= m_colWidths
[i
];
5149 m_colRights
[i
] = right
;
5153 if ( m_currentCellCoords
== wxGridNoCellCoords
)
5155 // if we have just inserted cols into an empty grid the current
5156 // cell will be undefined...
5158 SetCurrentCell( 0, 0 );
5160 if ( !GetBatchCount() )
5163 m_colLabelWin
->Refresh();
5169 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
5171 size_t pos
= msg
.GetCommandInt();
5172 int numCols
= msg
.GetCommandInt2();
5173 m_numCols
-= numCols
;
5175 if ( !m_colAt
.IsEmpty() )
5177 int colID
= GetColAt( pos
);
5179 m_colAt
.RemoveAt( pos
, numCols
);
5181 //Shift the column IDs
5183 for ( colPos
= 0; colPos
< m_numCols
; colPos
++ )
5185 if ( m_colAt
[colPos
] > colID
)
5186 m_colAt
[colPos
] -= numCols
;
5190 if ( !m_colWidths
.IsEmpty() )
5192 m_colWidths
.RemoveAt( pos
, numCols
);
5193 m_colRights
.RemoveAt( pos
, numCols
);
5197 for ( colPos
= 0; colPos
< m_numCols
; colPos
++ )
5199 i
= GetColAt( colPos
);
5201 w
+= m_colWidths
[i
];
5208 m_currentCellCoords
= wxGridNoCellCoords
;
5212 if ( m_currentCellCoords
.GetCol() >= m_numCols
)
5213 m_currentCellCoords
.Set( 0, 0 );
5217 m_selection
->UpdateCols( pos
, -((int)numCols
) );
5218 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
5221 attrProvider
->UpdateAttrCols( pos
, -((int)numCols
) );
5223 // ifdef'd out following patch from Paul Gammans
5225 // No need to touch row attributes, unless we
5226 // removed _all_ columns, in this case, we remove
5227 // all row attributes.
5228 // I hate to do this here, but the
5229 // needed data is not available inside UpdateAttrCols.
5230 if ( !GetNumberCols() )
5231 attrProvider
->UpdateAttrRows( 0, -GetNumberRows() );
5235 if ( !GetBatchCount() )
5238 m_colLabelWin
->Refresh();
5245 if (result
&& !GetBatchCount() )
5246 m_gridWin
->Refresh();
5251 wxArrayInt
wxGrid::CalcRowLabelsExposed( const wxRegion
& reg
) const
5253 wxRegionIterator
iter( reg
);
5256 wxArrayInt rowlabels
;
5263 // TODO: remove this when we can...
5264 // There is a bug in wxMotif that gives garbage update
5265 // rectangles if you jump-scroll a long way by clicking the
5266 // scrollbar with middle button. This is a work-around
5268 #if defined(__WXMOTIF__)
5270 m_gridWin
->GetClientSize( &cw
, &ch
);
5271 if ( r
.GetTop() > ch
)
5273 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
5276 // logical bounds of update region
5279 CalcUnscrolledPosition( 0, r
.GetTop(), &dummy
, &top
);
5280 CalcUnscrolledPosition( 0, r
.GetBottom(), &dummy
, &bottom
);
5282 // find the row labels within these bounds
5285 for ( row
= internalYToRow(top
); row
< m_numRows
; row
++ )
5287 if ( GetRowBottom(row
) < top
)
5290 if ( GetRowTop(row
) > bottom
)
5293 rowlabels
.Add( row
);
5302 wxArrayInt
wxGrid::CalcColLabelsExposed( const wxRegion
& reg
) const
5304 wxRegionIterator
iter( reg
);
5307 wxArrayInt colLabels
;
5314 // TODO: remove this when we can...
5315 // There is a bug in wxMotif that gives garbage update
5316 // rectangles if you jump-scroll a long way by clicking the
5317 // scrollbar with middle button. This is a work-around
5319 #if defined(__WXMOTIF__)
5321 m_gridWin
->GetClientSize( &cw
, &ch
);
5322 if ( r
.GetLeft() > cw
)
5324 r
.SetRight( wxMin( r
.GetRight(), cw
) );
5327 // logical bounds of update region
5330 CalcUnscrolledPosition( r
.GetLeft(), 0, &left
, &dummy
);
5331 CalcUnscrolledPosition( r
.GetRight(), 0, &right
, &dummy
);
5333 // find the cells within these bounds
5337 for ( colPos
= GetColPos( internalXToCol(left
) ); colPos
< m_numCols
; colPos
++ )
5339 col
= GetColAt( colPos
);
5341 if ( GetColRight(col
) < left
)
5344 if ( GetColLeft(col
) > right
)
5347 colLabels
.Add( col
);
5356 wxGridCellCoordsArray
wxGrid::CalcCellsExposed( const wxRegion
& reg
) const
5358 wxRegionIterator
iter( reg
);
5361 wxGridCellCoordsArray cellsExposed
;
5363 int left
, top
, right
, bottom
;
5368 // TODO: remove this when we can...
5369 // There is a bug in wxMotif that gives garbage update
5370 // rectangles if you jump-scroll a long way by clicking the
5371 // scrollbar with middle button. This is a work-around
5373 #if defined(__WXMOTIF__)
5375 m_gridWin
->GetClientSize( &cw
, &ch
);
5376 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
5377 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
5378 r
.SetRight( wxMin( r
.GetRight(), cw
) );
5379 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
5382 // logical bounds of update region
5384 CalcUnscrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
5385 CalcUnscrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
5387 // find the cells within these bounds
5390 for ( row
= internalYToRow(top
); row
< m_numRows
; row
++ )
5392 if ( GetRowBottom(row
) <= top
)
5395 if ( GetRowTop(row
) > bottom
)
5399 for ( colPos
= GetColPos( internalXToCol(left
) ); colPos
< m_numCols
; colPos
++ )
5401 col
= GetColAt( colPos
);
5403 if ( GetColRight(col
) <= left
)
5406 if ( GetColLeft(col
) > right
)
5409 cellsExposed
.Add( wxGridCellCoords( row
, col
) );
5416 return cellsExposed
;
5420 void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent
& event
)
5423 wxPoint
pos( event
.GetPosition() );
5424 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
5426 if ( event
.Dragging() )
5430 m_isDragging
= true;
5431 m_rowLabelWin
->CaptureMouse();
5434 if ( event
.LeftIsDown() )
5436 switch ( m_cursorMode
)
5438 case WXGRID_CURSOR_RESIZE_ROW
:
5440 int cw
, ch
, left
, dummy
;
5441 m_gridWin
->GetClientSize( &cw
, &ch
);
5442 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
5444 wxClientDC
dc( m_gridWin
);
5447 GetRowTop(m_dragRowOrCol
) +
5448 GetRowMinimalHeight(m_dragRowOrCol
) );
5449 dc
.SetLogicalFunction(wxINVERT
);
5450 if ( m_dragLastPos
>= 0 )
5452 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
5454 dc
.DrawLine( left
, y
, left
+cw
, y
);
5459 case WXGRID_CURSOR_SELECT_ROW
:
5461 if ( (row
= YToRow( y
)) >= 0 )
5465 m_selection
->SelectRow( row
,
5466 event
.ControlDown(),
5475 // default label to suppress warnings about "enumeration value
5476 // 'xxx' not handled in switch
5484 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
5489 if (m_rowLabelWin
->HasCapture())
5490 m_rowLabelWin
->ReleaseMouse();
5491 m_isDragging
= false;
5494 // ------------ Entering or leaving the window
5496 if ( event
.Entering() || event
.Leaving() )
5498 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
5501 // ------------ Left button pressed
5503 else if ( event
.LeftDown() )
5505 // don't send a label click event for a hit on the
5506 // edge of the row label - this is probably the user
5507 // wanting to resize the row
5509 if ( YToEdgeOfRow(y
) < 0 )
5513 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, row
, -1, event
) )
5515 if ( !event
.ShiftDown() && !event
.CmdDown() )
5519 if ( event
.ShiftDown() )
5521 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
5524 GetNumberCols() - 1,
5525 event
.ControlDown(),
5532 m_selection
->SelectRow( row
,
5533 event
.ControlDown(),
5540 ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW
, m_rowLabelWin
);
5545 // starting to drag-resize a row
5546 if ( CanDragRowSize() )
5547 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
);
5551 // ------------ Left double click
5553 else if (event
.LeftDClick() )
5555 row
= YToEdgeOfRow(y
);
5560 !SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, row
, -1, event
) )
5562 // no default action at the moment
5567 // adjust row height depending on label text
5568 AutoSizeRowLabelSize( row
);
5570 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5575 // ------------ Left button released
5577 else if ( event
.LeftUp() )
5579 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
5581 DoEndDragResizeRow();
5583 // Note: we are ending the event *after* doing
5584 // default processing in this case
5586 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
5589 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
5593 // ------------ Right button down
5595 else if ( event
.RightDown() )
5599 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, row
, -1, event
) )
5601 // no default action at the moment
5605 // ------------ Right double click
5607 else if ( event
.RightDClick() )
5611 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, row
, -1, event
) )
5613 // no default action at the moment
5617 // ------------ No buttons down and mouse moving
5619 else if ( event
.Moving() )
5621 m_dragRowOrCol
= YToEdgeOfRow( y
);
5622 if ( m_dragRowOrCol
>= 0 )
5624 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5626 // don't capture the mouse yet
5627 if ( CanDragRowSize() )
5628 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
, false);
5631 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5633 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
, false);
5638 void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent
& event
)
5641 wxPoint
pos( event
.GetPosition() );
5642 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
5644 if ( event
.Dragging() )
5648 m_isDragging
= true;
5649 m_colLabelWin
->CaptureMouse();
5651 if ( m_cursorMode
== WXGRID_CURSOR_MOVE_COL
)
5652 m_dragRowOrCol
= XToCol( x
);
5655 if ( event
.LeftIsDown() )
5657 switch ( m_cursorMode
)
5659 case WXGRID_CURSOR_RESIZE_COL
:
5661 int cw
, ch
, dummy
, top
;
5662 m_gridWin
->GetClientSize( &cw
, &ch
);
5663 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
5665 wxClientDC
dc( m_gridWin
);
5668 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
5669 GetColMinimalWidth(m_dragRowOrCol
));
5670 dc
.SetLogicalFunction(wxINVERT
);
5671 if ( m_dragLastPos
>= 0 )
5673 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ ch
);
5675 dc
.DrawLine( x
, top
, x
, top
+ ch
);
5680 case WXGRID_CURSOR_SELECT_COL
:
5682 if ( (col
= XToCol( x
)) >= 0 )
5686 m_selection
->SelectCol( col
,
5687 event
.ControlDown(),
5696 case WXGRID_CURSOR_MOVE_COL
:
5699 m_moveToCol
= GetColAt( 0 );
5701 m_moveToCol
= XToCol( x
);
5705 if ( m_moveToCol
< 0 )
5706 markerX
= GetColRight( GetColAt( m_numCols
- 1 ) );
5707 else if ( x
>= (GetColLeft( m_moveToCol
) + (GetColWidth(m_moveToCol
) / 2)) )
5709 m_moveToCol
= GetColAt( GetColPos( m_moveToCol
) + 1 );
5710 if ( m_moveToCol
< 0 )
5711 markerX
= GetColRight( GetColAt( m_numCols
- 1 ) );
5713 markerX
= GetColLeft( m_moveToCol
);
5716 markerX
= GetColLeft( m_moveToCol
);
5718 if ( markerX
!= m_dragLastPos
)
5720 wxClientDC
dc( m_colLabelWin
);
5724 m_colLabelWin
->GetClientSize( &cw
, &ch
);
5728 //Clean up the last indicator
5729 if ( m_dragLastPos
>= 0 )
5731 wxPen
pen( m_colLabelWin
->GetBackgroundColour(), 2 );
5733 dc
.DrawLine( m_dragLastPos
+ 1, 0, m_dragLastPos
+ 1, ch
);
5734 dc
.SetPen(wxNullPen
);
5736 if ( XToCol( m_dragLastPos
) != -1 )
5737 DrawColLabel( dc
, XToCol( m_dragLastPos
) );
5740 const wxColour
*color
;
5741 //Moving to the same place? Don't draw a marker
5742 if ( (m_moveToCol
== m_dragRowOrCol
)
5743 || (GetColPos( m_moveToCol
) == GetColPos( m_dragRowOrCol
) + 1)
5744 || (m_moveToCol
< 0 && m_dragRowOrCol
== GetColAt( m_numCols
- 1 )))
5745 color
= wxLIGHT_GREY
;
5750 wxPen
pen( *color
, 2 );
5753 dc
.DrawLine( markerX
, 0, markerX
, ch
);
5755 dc
.SetPen(wxNullPen
);
5757 m_dragLastPos
= markerX
- 1;
5762 // default label to suppress warnings about "enumeration value
5763 // 'xxx' not handled in switch
5771 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
5776 if (m_colLabelWin
->HasCapture())
5777 m_colLabelWin
->ReleaseMouse();
5778 m_isDragging
= false;
5781 // ------------ Entering or leaving the window
5783 if ( event
.Entering() || event
.Leaving() )
5785 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5788 // ------------ Left button pressed
5790 else if ( event
.LeftDown() )
5792 // don't send a label click event for a hit on the
5793 // edge of the col label - this is probably the user
5794 // wanting to resize the col
5796 if ( XToEdgeOfCol(x
) < 0 )
5800 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, col
, event
) )
5802 if ( m_canDragColMove
)
5804 //Show button as pressed
5805 wxClientDC
dc( m_colLabelWin
);
5806 int colLeft
= GetColLeft( col
);
5807 int colRight
= GetColRight( col
) - 1;
5808 dc
.SetPen( wxPen( m_colLabelWin
->GetBackgroundColour(), 1 ) );
5809 dc
.DrawLine( colLeft
, 1, colLeft
, m_colLabelHeight
-1 );
5810 dc
.DrawLine( colLeft
, 1, colRight
, 1 );
5812 ChangeCursorMode(WXGRID_CURSOR_MOVE_COL
, m_colLabelWin
);
5816 if ( !event
.ShiftDown() && !event
.CmdDown() )
5820 if ( event
.ShiftDown() )
5822 m_selection
->SelectBlock( 0,
5823 m_currentCellCoords
.GetCol(),
5824 GetNumberRows() - 1, col
,
5825 event
.ControlDown(),
5832 m_selection
->SelectCol( col
,
5833 event
.ControlDown(),
5840 ChangeCursorMode(WXGRID_CURSOR_SELECT_COL
, m_colLabelWin
);
5846 // starting to drag-resize a col
5848 if ( CanDragColSize() )
5849 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
);
5853 // ------------ Left double click
5855 if ( event
.LeftDClick() )
5857 col
= XToEdgeOfCol(x
);
5862 ! SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, col
, event
) )
5864 // no default action at the moment
5869 // adjust column width depending on label text
5870 AutoSizeColLabelSize( col
);
5872 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5877 // ------------ Left button released
5879 else if ( event
.LeftUp() )
5881 switch ( m_cursorMode
)
5883 case WXGRID_CURSOR_RESIZE_COL
:
5884 DoEndDragResizeCol();
5886 // Note: we are ending the event *after* doing
5887 // default processing in this case
5889 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
5892 case WXGRID_CURSOR_MOVE_COL
:
5895 SendEvent( wxEVT_GRID_COL_MOVE
, -1, m_dragRowOrCol
, event
);
5898 case WXGRID_CURSOR_SELECT_COL
:
5899 case WXGRID_CURSOR_SELECT_CELL
:
5900 case WXGRID_CURSOR_RESIZE_ROW
:
5901 case WXGRID_CURSOR_SELECT_ROW
:
5902 // nothing to do (?)
5906 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5910 // ------------ Right button down
5912 else if ( event
.RightDown() )
5916 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, col
, event
) )
5918 // no default action at the moment
5922 // ------------ Right double click
5924 else if ( event
.RightDClick() )
5928 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, col
, event
) )
5930 // no default action at the moment
5934 // ------------ No buttons down and mouse moving
5936 else if ( event
.Moving() )
5938 m_dragRowOrCol
= XToEdgeOfCol( x
);
5939 if ( m_dragRowOrCol
>= 0 )
5941 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5943 // don't capture the cursor yet
5944 if ( CanDragColSize() )
5945 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
, false);
5948 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5950 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
, false);
5955 void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent
& event
)
5957 if ( event
.LeftDown() )
5959 // indicate corner label by having both row and
5962 if ( !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, -1, event
) )
5967 else if ( event
.LeftDClick() )
5969 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, -1, event
);
5971 else if ( event
.RightDown() )
5973 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, -1, event
) )
5975 // no default action at the moment
5978 else if ( event
.RightDClick() )
5980 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, -1, event
) )
5982 // no default action at the moment
5987 void wxGrid::CancelMouseCapture()
5989 // cancel operation currently in progress, whatever it is
5992 m_isDragging
= false;
5993 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
5994 m_winCapture
->SetCursor( *wxSTANDARD_CURSOR
);
5995 m_winCapture
= NULL
;
5997 // remove traces of whatever we drew on screen
6002 void wxGrid::ChangeCursorMode(CursorMode mode
,
6007 static const wxChar
*cursorModes
[] =
6017 wxLogTrace(_T("grid"),
6018 _T("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
6019 win
== m_colLabelWin
? _T("colLabelWin")
6020 : win
? _T("rowLabelWin")
6022 cursorModes
[m_cursorMode
], cursorModes
[mode
]);
6025 if ( mode
== m_cursorMode
&&
6026 win
== m_winCapture
&&
6027 captureMouse
== (m_winCapture
!= NULL
))
6032 // by default use the grid itself
6038 if (m_winCapture
->HasCapture())
6039 m_winCapture
->ReleaseMouse();
6040 m_winCapture
= (wxWindow
*)NULL
;
6043 m_cursorMode
= mode
;
6045 switch ( m_cursorMode
)
6047 case WXGRID_CURSOR_RESIZE_ROW
:
6048 win
->SetCursor( m_rowResizeCursor
);
6051 case WXGRID_CURSOR_RESIZE_COL
:
6052 win
->SetCursor( m_colResizeCursor
);
6055 case WXGRID_CURSOR_MOVE_COL
:
6056 win
->SetCursor( wxCursor(wxCURSOR_HAND
) );
6060 win
->SetCursor( *wxSTANDARD_CURSOR
);
6064 // we need to capture mouse when resizing
6065 bool resize
= m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
||
6066 m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
;
6068 if ( captureMouse
&& resize
)
6070 win
->CaptureMouse();
6075 void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent
& event
)
6078 wxPoint
pos( event
.GetPosition() );
6079 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
6081 wxGridCellCoords coords
;
6082 XYToCell( x
, y
, coords
);
6084 int cell_rows
, cell_cols
;
6085 bool isFirstDrag
= !m_isDragging
;
6086 GetCellSize( coords
.GetRow(), coords
.GetCol(), &cell_rows
, &cell_cols
);
6087 if ((cell_rows
< 0) || (cell_cols
< 0))
6089 coords
.SetRow(coords
.GetRow() + cell_rows
);
6090 coords
.SetCol(coords
.GetCol() + cell_cols
);
6093 if ( event
.Dragging() )
6095 //wxLogDebug("pos(%d, %d) coords(%d, %d)", pos.x, pos.y, coords.GetRow(), coords.GetCol());
6097 // Don't start doing anything until the mouse has been dragged at
6098 // least 3 pixels in any direction...
6101 if (m_startDragPos
== wxDefaultPosition
)
6103 m_startDragPos
= pos
;
6106 if (abs(m_startDragPos
.x
- pos
.x
) < 4 && abs(m_startDragPos
.y
- pos
.y
) < 4)
6110 m_isDragging
= true;
6111 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
6113 // Hide the edit control, so it
6114 // won't interfere with drag-shrinking.
6115 if ( IsCellEditControlShown() )
6117 HideCellEditControl();
6118 SaveEditControlValue();
6121 if ( coords
!= wxGridNoCellCoords
)
6123 if ( event
.CmdDown() )
6125 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
6126 m_selectingKeyboard
= coords
;
6127 HighlightBlock( m_selectingKeyboard
, coords
);
6129 else if ( CanDragCell() )
6133 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
6134 m_selectingKeyboard
= coords
;
6136 SendEvent( wxEVT_GRID_CELL_BEGIN_DRAG
,
6145 if ( !IsSelection() )
6147 HighlightBlock( coords
, coords
);
6151 HighlightBlock( m_currentCellCoords
, coords
);
6155 if (! IsVisible(coords
))
6157 MakeCellVisible(coords
);
6158 // TODO: need to introduce a delay or something here. The
6159 // scrolling is way too fast, at least under MSW and GTK.
6162 // Have we captured the mouse yet?
6165 m_winCapture
= m_gridWin
;
6166 m_winCapture
->CaptureMouse();
6171 else if ( event
.LeftIsDown() &&
6172 m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
6174 int cw
, ch
, left
, dummy
;
6175 m_gridWin
->GetClientSize( &cw
, &ch
);
6176 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
6178 wxClientDC
dc( m_gridWin
);
6180 y
= wxMax( y
, GetRowTop(m_dragRowOrCol
) +
6181 GetRowMinimalHeight(m_dragRowOrCol
) );
6182 dc
.SetLogicalFunction(wxINVERT
);
6183 if ( m_dragLastPos
>= 0 )
6185 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
6187 dc
.DrawLine( left
, y
, left
+cw
, y
);
6190 else if ( event
.LeftIsDown() &&
6191 m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
6193 int cw
, ch
, dummy
, top
;
6194 m_gridWin
->GetClientSize( &cw
, &ch
);
6195 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
6197 wxClientDC
dc( m_gridWin
);
6199 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
6200 GetColMinimalWidth(m_dragRowOrCol
) );
6201 dc
.SetLogicalFunction(wxINVERT
);
6202 if ( m_dragLastPos
>= 0 )
6204 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ ch
);
6206 dc
.DrawLine( x
, top
, x
, top
+ ch
);
6213 m_isDragging
= false;
6214 m_startDragPos
= wxDefaultPosition
;
6216 // VZ: if we do this, the mode is reset to WXGRID_CURSOR_SELECT_CELL
6217 // immediately after it becomes WXGRID_CURSOR_RESIZE_ROW/COL under
6220 if ( event
.Entering() || event
.Leaving() )
6222 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6223 m_gridWin
->SetCursor( *wxSTANDARD_CURSOR
);
6228 // ------------ Left button pressed
6230 if ( event
.LeftDown() && coords
!= wxGridNoCellCoords
)
6232 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_CLICK
,
6237 if ( !event
.CmdDown() )
6239 if ( event
.ShiftDown() )
6243 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
6244 m_currentCellCoords
.GetCol(),
6247 event
.ControlDown(),
6253 else if ( XToEdgeOfCol(x
) < 0 &&
6254 YToEdgeOfRow(y
) < 0 )
6256 DisableCellEditControl();
6257 MakeCellVisible( coords
);
6259 if ( event
.CmdDown() )
6263 m_selection
->ToggleCellSelection( coords
.GetRow(),
6265 event
.ControlDown(),
6270 m_selectingTopLeft
= wxGridNoCellCoords
;
6271 m_selectingBottomRight
= wxGridNoCellCoords
;
6272 m_selectingKeyboard
= coords
;
6276 m_waitForSlowClick
= m_currentCellCoords
== coords
&&
6277 coords
!= wxGridNoCellCoords
;
6278 SetCurrentCell( coords
);
6284 // ------------ Left double click
6286 else if ( event
.LeftDClick() && coords
!= wxGridNoCellCoords
)
6288 DisableCellEditControl();
6290 if ( XToEdgeOfCol(x
) < 0 && YToEdgeOfRow(y
) < 0 )
6292 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_DCLICK
,
6297 // we want double click to select a cell and start editing
6298 // (i.e. to behave in same way as sequence of two slow clicks):
6299 m_waitForSlowClick
= true;
6304 // ------------ Left button released
6306 else if ( event
.LeftUp() )
6308 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
6312 if (m_winCapture
->HasCapture())
6313 m_winCapture
->ReleaseMouse();
6314 m_winCapture
= NULL
;
6317 if ( coords
== m_currentCellCoords
&& m_waitForSlowClick
&& CanEnableCellControl() )
6320 EnableCellEditControl();
6322 wxGridCellAttr
*attr
= GetCellAttr(coords
);
6323 wxGridCellEditor
*editor
= attr
->GetEditor(this, coords
.GetRow(), coords
.GetCol());
6324 editor
->StartingClick();
6328 m_waitForSlowClick
= false;
6330 else if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
6331 m_selectingBottomRight
!= wxGridNoCellCoords
)
6335 m_selection
->SelectBlock( m_selectingTopLeft
.GetRow(),
6336 m_selectingTopLeft
.GetCol(),
6337 m_selectingBottomRight
.GetRow(),
6338 m_selectingBottomRight
.GetCol(),
6339 event
.ControlDown(),
6345 m_selectingTopLeft
= wxGridNoCellCoords
;
6346 m_selectingBottomRight
= wxGridNoCellCoords
;
6348 // Show the edit control, if it has been hidden for
6350 ShowCellEditControl();
6353 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
6355 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6356 DoEndDragResizeRow();
6358 // Note: we are ending the event *after* doing
6359 // default processing in this case
6361 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
6363 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
6365 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6366 DoEndDragResizeCol();
6368 // Note: we are ending the event *after* doing
6369 // default processing in this case
6371 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
6377 // ------------ Right button down
6379 else if ( event
.RightDown() && coords
!= wxGridNoCellCoords
)
6381 DisableCellEditControl();
6382 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_CLICK
,
6387 // no default action at the moment
6391 // ------------ Right double click
6393 else if ( event
.RightDClick() && coords
!= wxGridNoCellCoords
)
6395 DisableCellEditControl();
6396 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_DCLICK
,
6401 // no default action at the moment
6405 // ------------ Moving and no button action
6407 else if ( event
.Moving() && !event
.IsButton() )
6409 if ( coords
.GetRow() < 0 || coords
.GetCol() < 0 )
6411 // out of grid cell area
6412 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6416 int dragRow
= YToEdgeOfRow( y
);
6417 int dragCol
= XToEdgeOfCol( x
);
6419 // Dragging on the corner of a cell to resize in both
6420 // directions is not implemented yet...
6422 if ( dragRow
>= 0 && dragCol
>= 0 )
6424 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6430 m_dragRowOrCol
= dragRow
;
6432 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
6434 if ( CanDragRowSize() && CanDragGridSize() )
6435 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, NULL
, false);
6438 else if ( dragCol
>= 0 )
6440 m_dragRowOrCol
= dragCol
;
6442 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
6444 if ( CanDragColSize() && CanDragGridSize() )
6445 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, NULL
, false);
6448 else // Neither on a row or col edge
6450 if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
6452 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6458 void wxGrid::DoEndDragResizeLine(const wxGridOperations
& oper
)
6460 if ( m_dragLastPos
== -1 )
6463 const wxGridOperations
& doper
= oper
.Dual();
6465 const wxSize size
= m_gridWin
->GetClientSize();
6467 const wxPoint ptOrigin
= CalcUnscrolledPosition(wxPoint(0, 0));
6469 // erase the last line we drew
6470 wxClientDC
dc(m_gridWin
);
6472 dc
.SetLogicalFunction(wxINVERT
);
6474 const int posLineStart
= oper
.Select(ptOrigin
);
6475 const int posLineEnd
= oper
.Select(ptOrigin
) + oper
.Select(size
);
6477 oper
.DrawParallelLine(dc
, posLineStart
, posLineEnd
, m_dragLastPos
);
6479 // temporarily hide the edit control before resizing
6480 HideCellEditControl();
6481 SaveEditControlValue();
6483 // do resize the line
6484 const int lineStart
= oper
.GetLineStartPos(this, m_dragRowOrCol
);
6485 oper
.SetLineSize(this, m_dragRowOrCol
,
6486 wxMax(m_dragLastPos
- lineStart
,
6487 oper
.GetMinimalLineSize(this, m_dragRowOrCol
)));
6489 // refresh now if we're not frozen
6490 if ( !GetBatchCount() )
6492 // we need to refresh everything beyond the resized line in the header
6495 // get the position from which to refresh in the other direction
6496 wxRect
rect(CellToRect(oper
.MakeCoords(m_dragRowOrCol
, 0)));
6497 rect
.SetPosition(CalcScrolledPosition(rect
.GetPosition()));
6499 // we only need the ordinate (for rows) or abscissa (for columns) here,
6500 // and need to cover the entire window in the other direction
6501 oper
.Select(rect
) = 0;
6503 wxRect
rectHeader(rect
.GetPosition(),
6506 oper
.GetHeaderWindowSize(this),
6507 doper
.Select(size
) - doper
.Select(rect
)
6510 oper
.GetHeaderWindow(this)->Refresh(true, &rectHeader
);
6513 // also refresh the grid window: extend the rectangle
6516 oper
.SelectSize(rect
) = oper
.Select(size
);
6518 int subtractLines
= 0;
6519 const int lineStart
= oper
.PosToLine(this, posLineStart
);
6520 if ( lineStart
>= 0 )
6522 // ensure that if we have a multi-cell block we redraw all of
6523 // it by increasing the refresh area to cover it entirely if a
6524 // part of it is affected
6525 const int lineEnd
= oper
.PosToLine(this, posLineEnd
, true);
6526 for ( int line
= lineStart
; line
< lineEnd
; line
++ )
6528 int cellLines
= oper
.Select(
6529 GetCellSize(oper
.MakeCoords(m_dragRowOrCol
, line
)));
6530 if ( cellLines
< subtractLines
)
6531 subtractLines
= cellLines
;
6536 oper
.GetLineStartPos(this, m_dragRowOrCol
+ subtractLines
);
6537 startPos
= doper
.CalcScrolledPosition(this, startPos
);
6539 doper
.Select(rect
) = startPos
;
6540 doper
.SelectSize(rect
) = doper
.Select(size
) - startPos
;
6542 m_gridWin
->Refresh(false, &rect
);
6546 // show the edit control back again
6547 ShowCellEditControl();
6550 void wxGrid::DoEndDragResizeRow()
6552 DoEndDragResizeLine(wxGridRowOperations());
6555 void wxGrid::DoEndDragResizeCol()
6557 DoEndDragResizeLine(wxGridColumnOperations());
6560 void wxGrid::DoEndDragMoveCol()
6562 //The user clicked on the column but didn't actually drag
6563 if ( m_dragLastPos
< 0 )
6565 m_colLabelWin
->Refresh(); //Do this to "unpress" the column
6570 if ( m_moveToCol
== -1 )
6571 newPos
= m_numCols
- 1;
6574 newPos
= GetColPos( m_moveToCol
);
6575 if ( newPos
> GetColPos( m_dragRowOrCol
) )
6579 SetColPos( m_dragRowOrCol
, newPos
);
6582 void wxGrid::SetColPos( int colID
, int newPos
)
6584 if ( m_colAt
.IsEmpty() )
6586 m_colAt
.Alloc( m_numCols
);
6589 for ( i
= 0; i
< m_numCols
; i
++ )
6595 int oldPos
= GetColPos( colID
);
6597 //Reshuffle the m_colAt array
6598 if ( newPos
> oldPos
)
6601 for ( i
= oldPos
; i
< newPos
; i
++ )
6603 m_colAt
[i
] = m_colAt
[i
+1];
6609 for ( i
= oldPos
; i
> newPos
; i
-- )
6611 m_colAt
[i
] = m_colAt
[i
-1];
6615 m_colAt
[newPos
] = colID
;
6617 //Recalculate the column rights
6618 if ( !m_colWidths
.IsEmpty() )
6622 for ( colPos
= 0; colPos
< m_numCols
; colPos
++ )
6624 int colID
= GetColAt( colPos
);
6626 colRight
+= m_colWidths
[colID
];
6627 m_colRights
[colID
] = colRight
;
6631 m_colLabelWin
->Refresh();
6632 m_gridWin
->Refresh();
6637 void wxGrid::EnableDragColMove( bool enable
)
6639 if ( m_canDragColMove
== enable
)
6642 m_canDragColMove
= enable
;
6644 if ( !m_canDragColMove
)
6648 //Recalculate the column rights
6649 if ( !m_colWidths
.IsEmpty() )
6653 for ( colPos
= 0; colPos
< m_numCols
; colPos
++ )
6655 colRight
+= m_colWidths
[colPos
];
6656 m_colRights
[colPos
] = colRight
;
6660 m_colLabelWin
->Refresh();
6661 m_gridWin
->Refresh();
6667 // ------ interaction with data model
6669 bool wxGrid::ProcessTableMessage( wxGridTableMessage
& msg
)
6671 switch ( msg
.GetId() )
6673 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES
:
6674 return GetModelValues();
6676 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES
:
6677 return SetModelValues();
6679 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
6680 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
6681 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
6682 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
6683 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
6684 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
6685 return Redimension( msg
);
6692 // The behaviour of this function depends on the grid table class
6693 // Clear() function. For the default wxGridStringTable class the
6694 // behavious is to replace all cell contents with wxEmptyString but
6695 // not to change the number of rows or cols.
6697 void wxGrid::ClearGrid()
6701 if (IsCellEditControlEnabled())
6702 DisableCellEditControl();
6705 if (!GetBatchCount())
6706 m_gridWin
->Refresh();
6710 bool wxGrid::InsertRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
6714 wxFAIL_MSG( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") );
6720 if (IsCellEditControlEnabled())
6721 DisableCellEditControl();
6723 bool done
= m_table
->InsertRows( pos
, numRows
);
6726 // the table will have sent the results of the insert row
6727 // operation to this view object as a grid table message
6733 bool wxGrid::AppendRows( int numRows
, bool WXUNUSED(updateLabels
) )
6737 wxFAIL_MSG( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") );
6743 bool done
= m_table
&& m_table
->AppendRows( numRows
);
6746 // the table will have sent the results of the append row
6747 // operation to this view object as a grid table message
6753 bool wxGrid::DeleteRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
6757 wxFAIL_MSG( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") );
6763 if (IsCellEditControlEnabled())
6764 DisableCellEditControl();
6766 bool done
= m_table
->DeleteRows( pos
, numRows
);
6768 // the table will have sent the results of the delete row
6769 // operation to this view object as a grid table message
6775 bool wxGrid::InsertCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
6779 wxFAIL_MSG( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") );
6785 if (IsCellEditControlEnabled())
6786 DisableCellEditControl();
6788 bool done
= m_table
->InsertCols( pos
, numCols
);
6790 // the table will have sent the results of the insert col
6791 // operation to this view object as a grid table message
6797 bool wxGrid::AppendCols( int numCols
, bool WXUNUSED(updateLabels
) )
6801 wxFAIL_MSG( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") );
6807 bool done
= m_table
->AppendCols( numCols
);
6809 // the table will have sent the results of the append col
6810 // operation to this view object as a grid table message
6816 bool wxGrid::DeleteCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
6820 wxFAIL_MSG( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") );
6826 if (IsCellEditControlEnabled())
6827 DisableCellEditControl();
6829 bool done
= m_table
->DeleteCols( pos
, numCols
);
6831 // the table will have sent the results of the delete col
6832 // operation to this view object as a grid table message
6839 // ----- event handlers
6842 // Generate a grid event based on a mouse event and
6843 // return the result of ProcessEvent()
6845 int wxGrid::SendEvent( const wxEventType type
,
6847 wxMouseEvent
& mouseEv
)
6849 bool claimed
, vetoed
;
6851 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
6853 int rowOrCol
= (row
== -1 ? col
: row
);
6855 wxGridSizeEvent
gridEvt( GetId(),
6859 mouseEv
.GetX() + GetRowLabelSize(),
6860 mouseEv
.GetY() + GetColLabelSize(),
6861 mouseEv
.ControlDown(),
6862 mouseEv
.ShiftDown(),
6864 mouseEv
.MetaDown() );
6866 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6867 vetoed
= !gridEvt
.IsAllowed();
6869 else if ( type
== wxEVT_GRID_RANGE_SELECT
)
6871 // Right now, it should _never_ end up here!
6872 wxGridRangeSelectEvent
gridEvt( GetId(),
6876 m_selectingBottomRight
,
6878 mouseEv
.ControlDown(),
6879 mouseEv
.ShiftDown(),
6881 mouseEv
.MetaDown() );
6883 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6884 vetoed
= !gridEvt
.IsAllowed();
6886 else if ( type
== wxEVT_GRID_LABEL_LEFT_CLICK
||
6887 type
== wxEVT_GRID_LABEL_LEFT_DCLICK
||
6888 type
== wxEVT_GRID_LABEL_RIGHT_CLICK
||
6889 type
== wxEVT_GRID_LABEL_RIGHT_DCLICK
)
6891 wxPoint pos
= mouseEv
.GetPosition();
6893 if ( mouseEv
.GetEventObject() == GetGridRowLabelWindow() )
6894 pos
.y
+= GetColLabelSize();
6895 if ( mouseEv
.GetEventObject() == GetGridColLabelWindow() )
6896 pos
.x
+= GetRowLabelSize();
6898 wxGridEvent
gridEvt( GetId(),
6905 mouseEv
.ControlDown(),
6906 mouseEv
.ShiftDown(),
6908 mouseEv
.MetaDown() );
6909 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6910 vetoed
= !gridEvt
.IsAllowed();
6914 wxGridEvent
gridEvt( GetId(),
6918 mouseEv
.GetX() + GetRowLabelSize(),
6919 mouseEv
.GetY() + GetColLabelSize(),
6921 mouseEv
.ControlDown(),
6922 mouseEv
.ShiftDown(),
6924 mouseEv
.MetaDown() );
6925 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6926 vetoed
= !gridEvt
.IsAllowed();
6929 // A Veto'd event may not be `claimed' so test this first
6933 return claimed
? 1 : 0;
6936 // Generate a grid event of specified type and return the result
6937 // of ProcessEvent().
6939 int wxGrid::SendEvent( const wxEventType type
,
6942 bool claimed
, vetoed
;
6944 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
6946 int rowOrCol
= (row
== -1 ? col
: row
);
6948 wxGridSizeEvent
gridEvt( GetId(), type
, this, rowOrCol
);
6950 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6951 vetoed
= !gridEvt
.IsAllowed();
6955 wxGridEvent
gridEvt( GetId(), type
, this, row
, col
);
6957 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6958 vetoed
= !gridEvt
.IsAllowed();
6961 // A Veto'd event may not be `claimed' so test this first
6965 return claimed
? 1 : 0;
6968 void wxGrid::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
6970 // needed to prevent zillions of paint events on MSW
6974 void wxGrid::Refresh(bool eraseb
, const wxRect
* rect
)
6976 // Don't do anything if between Begin/EndBatch...
6977 // EndBatch() will do all this on the last nested one anyway.
6978 if ( m_created
&& !GetBatchCount() )
6980 // Refresh to get correct scrolled position:
6981 wxScrolledWindow::Refresh(eraseb
, rect
);
6985 int rect_x
, rect_y
, rectWidth
, rectHeight
;
6986 int width_label
, width_cell
, height_label
, height_cell
;
6989 // Copy rectangle can get scroll offsets..
6990 rect_x
= rect
->GetX();
6991 rect_y
= rect
->GetY();
6992 rectWidth
= rect
->GetWidth();
6993 rectHeight
= rect
->GetHeight();
6995 width_label
= m_rowLabelWidth
- rect_x
;
6996 if (width_label
> rectWidth
)
6997 width_label
= rectWidth
;
6999 height_label
= m_colLabelHeight
- rect_y
;
7000 if (height_label
> rectHeight
)
7001 height_label
= rectHeight
;
7003 if (rect_x
> m_rowLabelWidth
)
7005 x
= rect_x
- m_rowLabelWidth
;
7006 width_cell
= rectWidth
;
7011 width_cell
= rectWidth
- (m_rowLabelWidth
- rect_x
);
7014 if (rect_y
> m_colLabelHeight
)
7016 y
= rect_y
- m_colLabelHeight
;
7017 height_cell
= rectHeight
;
7022 height_cell
= rectHeight
- (m_colLabelHeight
- rect_y
);
7025 // Paint corner label part intersecting rect.
7026 if ( width_label
> 0 && height_label
> 0 )
7028 wxRect
anotherrect(rect_x
, rect_y
, width_label
, height_label
);
7029 m_cornerLabelWin
->Refresh(eraseb
, &anotherrect
);
7032 // Paint col labels part intersecting rect.
7033 if ( width_cell
> 0 && height_label
> 0 )
7035 wxRect
anotherrect(x
, rect_y
, width_cell
, height_label
);
7036 m_colLabelWin
->Refresh(eraseb
, &anotherrect
);
7039 // Paint row labels part intersecting rect.
7040 if ( width_label
> 0 && height_cell
> 0 )
7042 wxRect
anotherrect(rect_x
, y
, width_label
, height_cell
);
7043 m_rowLabelWin
->Refresh(eraseb
, &anotherrect
);
7046 // Paint cell area part intersecting rect.
7047 if ( width_cell
> 0 && height_cell
> 0 )
7049 wxRect
anotherrect(x
, y
, width_cell
, height_cell
);
7050 m_gridWin
->Refresh(eraseb
, &anotherrect
);
7055 m_cornerLabelWin
->Refresh(eraseb
, NULL
);
7056 m_colLabelWin
->Refresh(eraseb
, NULL
);
7057 m_rowLabelWin
->Refresh(eraseb
, NULL
);
7058 m_gridWin
->Refresh(eraseb
, NULL
);
7063 void wxGrid::OnSize(wxSizeEvent
& WXUNUSED(event
))
7065 if (m_targetWindow
!= this) // check whether initialisation has been done
7067 // reposition our children windows
7072 void wxGrid::OnKeyDown( wxKeyEvent
& event
)
7074 if ( m_inOnKeyDown
)
7076 // shouldn't be here - we are going round in circles...
7078 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") );
7081 m_inOnKeyDown
= true;
7083 // propagate the event up and see if it gets processed
7084 wxWindow
*parent
= GetParent();
7085 wxKeyEvent
keyEvt( event
);
7086 keyEvt
.SetEventObject( parent
);
7088 if ( !parent
->GetEventHandler()->ProcessEvent( keyEvt
) )
7090 if (GetLayoutDirection() == wxLayout_RightToLeft
)
7092 if (event
.GetKeyCode() == WXK_RIGHT
)
7093 event
.m_keyCode
= WXK_LEFT
;
7094 else if (event
.GetKeyCode() == WXK_LEFT
)
7095 event
.m_keyCode
= WXK_RIGHT
;
7098 // try local handlers
7099 switch ( event
.GetKeyCode() )
7102 if ( event
.ControlDown() )
7103 MoveCursorUpBlock( event
.ShiftDown() );
7105 MoveCursorUp( event
.ShiftDown() );
7109 if ( event
.ControlDown() )
7110 MoveCursorDownBlock( event
.ShiftDown() );
7112 MoveCursorDown( event
.ShiftDown() );
7116 if ( event
.ControlDown() )
7117 MoveCursorLeftBlock( event
.ShiftDown() );
7119 MoveCursorLeft( event
.ShiftDown() );
7123 if ( event
.ControlDown() )
7124 MoveCursorRightBlock( event
.ShiftDown() );
7126 MoveCursorRight( event
.ShiftDown() );
7130 case WXK_NUMPAD_ENTER
:
7131 if ( event
.ControlDown() )
7133 event
.Skip(); // to let the edit control have the return
7137 if ( GetGridCursorRow() < GetNumberRows()-1 )
7139 MoveCursorDown( event
.ShiftDown() );
7143 // at the bottom of a column
7144 DisableCellEditControl();
7154 if (event
.ShiftDown())
7156 if ( GetGridCursorCol() > 0 )
7158 MoveCursorLeft( false );
7163 DisableCellEditControl();
7168 if ( GetGridCursorCol() < GetNumberCols() - 1 )
7170 MoveCursorRight( false );
7175 DisableCellEditControl();
7181 if ( event
.ControlDown() )
7183 MakeCellVisible( 0, 0 );
7184 SetCurrentCell( 0, 0 );
7193 if ( event
.ControlDown() )
7195 MakeCellVisible( m_numRows
- 1, m_numCols
- 1 );
7196 SetCurrentCell( m_numRows
- 1, m_numCols
- 1 );
7213 // Ctrl-Space selects the current column, Shift-Space -- the
7214 // current row and Ctrl-Shift-Space -- everything
7215 switch ( m_selection
? event
.GetModifiers() : wxMOD_NONE
)
7218 m_selection
->SelectCol(m_currentCellCoords
.GetCol());
7222 m_selection
->SelectRow(m_currentCellCoords
.GetRow());
7225 case wxMOD_CONTROL
| wxMOD_SHIFT
:
7226 m_selection
->SelectBlock(0, 0,
7227 m_numRows
- 1, m_numCols
- 1);
7231 if ( !IsEditable() )
7233 MoveCursorRight(false);
7236 //else: fall through
7249 m_inOnKeyDown
= false;
7252 void wxGrid::OnKeyUp( wxKeyEvent
& event
)
7254 // try local handlers
7256 if ( event
.GetKeyCode() == WXK_SHIFT
)
7258 if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
7259 m_selectingBottomRight
!= wxGridNoCellCoords
)
7263 m_selection
->SelectBlock(
7264 m_selectingTopLeft
.GetRow(),
7265 m_selectingTopLeft
.GetCol(),
7266 m_selectingBottomRight
.GetRow(),
7267 m_selectingBottomRight
.GetCol(),
7268 event
.ControlDown(),
7275 m_selectingTopLeft
= wxGridNoCellCoords
;
7276 m_selectingBottomRight
= wxGridNoCellCoords
;
7277 m_selectingKeyboard
= wxGridNoCellCoords
;
7281 void wxGrid::OnChar( wxKeyEvent
& event
)
7283 // is it possible to edit the current cell at all?
7284 if ( !IsCellEditControlEnabled() && CanEnableCellControl() )
7286 // yes, now check whether the cells editor accepts the key
7287 int row
= m_currentCellCoords
.GetRow();
7288 int col
= m_currentCellCoords
.GetCol();
7289 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7290 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
7292 // <F2> is special and will always start editing, for
7293 // other keys - ask the editor itself
7294 if ( (event
.GetKeyCode() == WXK_F2
&& !event
.HasModifiers())
7295 || editor
->IsAcceptedKey(event
) )
7297 // ensure cell is visble
7298 MakeCellVisible(row
, col
);
7299 EnableCellEditControl();
7301 // a problem can arise if the cell is not completely
7302 // visible (even after calling MakeCellVisible the
7303 // control is not created and calling StartingKey will
7305 if ( event
.GetKeyCode() != WXK_F2
&& editor
->IsCreated() && m_cellEditCtrlEnabled
)
7306 editor
->StartingKey(event
);
7322 void wxGrid::OnEraseBackground(wxEraseEvent
&)
7326 void wxGrid::SetCurrentCell( const wxGridCellCoords
& coords
)
7328 if ( SendEvent( wxEVT_GRID_SELECT_CELL
, coords
.GetRow(), coords
.GetCol() ) )
7330 // the event has been intercepted - do nothing
7334 #if !defined(__WXMAC__)
7335 wxClientDC
dc( m_gridWin
);
7339 if ( m_currentCellCoords
!= wxGridNoCellCoords
)
7341 DisableCellEditControl();
7343 if ( IsVisible( m_currentCellCoords
, false ) )
7346 r
= BlockToDeviceRect( m_currentCellCoords
, m_currentCellCoords
);
7347 if ( !m_gridLinesEnabled
)
7355 wxGridCellCoordsArray cells
= CalcCellsExposed( r
);
7357 // Otherwise refresh redraws the highlight!
7358 m_currentCellCoords
= coords
;
7360 #if defined(__WXMAC__)
7361 m_gridWin
->Refresh(true /*, & r */);
7363 DrawGridCellArea( dc
, cells
);
7364 DrawAllGridLines( dc
, r
);
7369 m_currentCellCoords
= coords
;
7371 wxGridCellAttr
*attr
= GetCellAttr( coords
);
7372 #if !defined(__WXMAC__)
7373 DrawCellHighlight( dc
, attr
);
7378 void wxGrid::HighlightBlock(int topRow
, int leftCol
, int bottomRow
, int rightCol
)
7380 wxGridCellCoords updateTopLeft
, updateBottomRight
;
7384 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows
)
7387 rightCol
= GetNumberCols() - 1;
7389 else if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns
)
7392 bottomRow
= GetNumberRows() - 1;
7396 EnsureFirstLessThanSecond(topRow
, bottomRow
);
7397 EnsureFirstLessThanSecond(leftCol
, rightCol
);
7399 updateTopLeft
= wxGridCellCoords( topRow
, leftCol
);
7400 updateBottomRight
= wxGridCellCoords( bottomRow
, rightCol
);
7402 // First the case that we selected a completely new area
7403 if ( m_selectingTopLeft
== wxGridNoCellCoords
||
7404 m_selectingBottomRight
== wxGridNoCellCoords
)
7407 rect
= BlockToDeviceRect( wxGridCellCoords ( topRow
, leftCol
),
7408 wxGridCellCoords ( bottomRow
, rightCol
) );
7409 m_gridWin
->Refresh( false, &rect
);
7412 // Now handle changing an existing selection area.
7413 else if ( m_selectingTopLeft
!= updateTopLeft
||
7414 m_selectingBottomRight
!= updateBottomRight
)
7416 // Compute two optimal update rectangles:
7417 // Either one rectangle is a real subset of the
7418 // other, or they are (almost) disjoint!
7420 bool need_refresh
[4];
7424 need_refresh
[3] = false;
7427 // Store intermediate values
7428 wxCoord oldLeft
= m_selectingTopLeft
.GetCol();
7429 wxCoord oldTop
= m_selectingTopLeft
.GetRow();
7430 wxCoord oldRight
= m_selectingBottomRight
.GetCol();
7431 wxCoord oldBottom
= m_selectingBottomRight
.GetRow();
7433 // Determine the outer/inner coordinates.
7434 EnsureFirstLessThanSecond(oldLeft
, leftCol
);
7435 EnsureFirstLessThanSecond(oldTop
, topRow
);
7436 EnsureFirstLessThanSecond(rightCol
, oldRight
);
7437 EnsureFirstLessThanSecond(bottomRow
, oldBottom
);
7439 // Now, either the stuff marked old is the outer
7440 // rectangle or we don't have a situation where one
7441 // is contained in the other.
7443 if ( oldLeft
< leftCol
)
7445 // Refresh the newly selected or deselected
7446 // area to the left of the old or new selection.
7447 need_refresh
[0] = true;
7448 rect
[0] = BlockToDeviceRect(
7449 wxGridCellCoords( oldTop
, oldLeft
),
7450 wxGridCellCoords( oldBottom
, leftCol
- 1 ) );
7453 if ( oldTop
< topRow
)
7455 // Refresh the newly selected or deselected
7456 // area above the old or new selection.
7457 need_refresh
[1] = true;
7458 rect
[1] = BlockToDeviceRect(
7459 wxGridCellCoords( oldTop
, leftCol
),
7460 wxGridCellCoords( topRow
- 1, rightCol
) );
7463 if ( oldRight
> rightCol
)
7465 // Refresh the newly selected or deselected
7466 // area to the right of the old or new selection.
7467 need_refresh
[2] = true;
7468 rect
[2] = BlockToDeviceRect(
7469 wxGridCellCoords( oldTop
, rightCol
+ 1 ),
7470 wxGridCellCoords( oldBottom
, oldRight
) );
7473 if ( oldBottom
> bottomRow
)
7475 // Refresh the newly selected or deselected
7476 // area below the old or new selection.
7477 need_refresh
[3] = true;
7478 rect
[3] = BlockToDeviceRect(
7479 wxGridCellCoords( bottomRow
+ 1, leftCol
),
7480 wxGridCellCoords( oldBottom
, rightCol
) );
7483 // various Refresh() calls
7484 for (i
= 0; i
< 4; i
++ )
7485 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
7486 m_gridWin
->Refresh( false, &(rect
[i
]) );
7490 m_selectingTopLeft
= updateTopLeft
;
7491 m_selectingBottomRight
= updateBottomRight
;
7495 // ------ functions to get/send data (see also public functions)
7498 bool wxGrid::GetModelValues()
7500 // Hide the editor, so it won't hide a changed value.
7501 HideCellEditControl();
7505 // all we need to do is repaint the grid
7507 m_gridWin
->Refresh();
7514 bool wxGrid::SetModelValues()
7518 // Disable the editor, so it won't hide a changed value.
7519 // Do we also want to save the current value of the editor first?
7521 DisableCellEditControl();
7525 for ( row
= 0; row
< m_numRows
; row
++ )
7527 for ( col
= 0; col
< m_numCols
; col
++ )
7529 m_table
->SetValue( row
, col
, GetCellValue(row
, col
) );
7539 // Note - this function only draws cells that are in the list of
7540 // exposed cells (usually set from the update region by
7541 // CalcExposedCells)
7543 void wxGrid::DrawGridCellArea( wxDC
& dc
, const wxGridCellCoordsArray
& cells
)
7545 if ( !m_numRows
|| !m_numCols
)
7548 int i
, numCells
= cells
.GetCount();
7549 int row
, col
, cell_rows
, cell_cols
;
7550 wxGridCellCoordsArray redrawCells
;
7552 for ( i
= numCells
- 1; i
>= 0; i
-- )
7554 row
= cells
[i
].GetRow();
7555 col
= cells
[i
].GetCol();
7556 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7558 // If this cell is part of a multicell block, find owner for repaint
7559 if ( cell_rows
<= 0 || cell_cols
<= 0 )
7561 wxGridCellCoords
cell( row
+ cell_rows
, col
+ cell_cols
);
7562 bool marked
= false;
7563 for ( int j
= 0; j
< numCells
; j
++ )
7565 if ( cell
== cells
[j
] )
7574 int count
= redrawCells
.GetCount();
7575 for (int j
= 0; j
< count
; j
++)
7577 if ( cell
== redrawCells
[j
] )
7585 redrawCells
.Add( cell
);
7588 // don't bother drawing this cell
7592 // If this cell is empty, find cell to left that might want to overflow
7593 if (m_table
&& m_table
->IsEmptyCell(row
, col
))
7595 for ( int l
= 0; l
< cell_rows
; l
++ )
7597 // find a cell in this row to leave already marked for repaint
7599 for (int k
= 0; k
< int(redrawCells
.GetCount()); k
++)
7600 if ((redrawCells
[k
].GetCol() < left
) &&
7601 (redrawCells
[k
].GetRow() == row
))
7603 left
= redrawCells
[k
].GetCol();
7607 left
= 0; // oh well
7609 for (int j
= col
- 1; j
>= left
; j
--)
7611 if (!m_table
->IsEmptyCell(row
+ l
, j
))
7613 if (GetCellOverflow(row
+ l
, j
))
7615 wxGridCellCoords
cell(row
+ l
, j
);
7616 bool marked
= false;
7618 for (int k
= 0; k
< numCells
; k
++)
7620 if ( cell
== cells
[k
] )
7629 int count
= redrawCells
.GetCount();
7630 for (int k
= 0; k
< count
; k
++)
7632 if ( cell
== redrawCells
[k
] )
7639 redrawCells
.Add( cell
);
7648 DrawCell( dc
, cells
[i
] );
7651 numCells
= redrawCells
.GetCount();
7653 for ( i
= numCells
- 1; i
>= 0; i
-- )
7655 DrawCell( dc
, redrawCells
[i
] );
7659 void wxGrid::DrawGridSpace( wxDC
& dc
)
7662 m_gridWin
->GetClientSize( &cw
, &ch
);
7665 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
7667 int rightCol
= m_numCols
> 0 ? GetColRight(GetColAt( m_numCols
- 1 )) : 0;
7668 int bottomRow
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) : 0;
7670 if ( right
> rightCol
|| bottom
> bottomRow
)
7673 CalcUnscrolledPosition( 0, 0, &left
, &top
);
7675 dc
.SetBrush(GetDefaultCellBackgroundColour());
7676 dc
.SetPen( *wxTRANSPARENT_PEN
);
7678 if ( right
> rightCol
)
7680 dc
.DrawRectangle( rightCol
, top
, right
- rightCol
, ch
);
7683 if ( bottom
> bottomRow
)
7685 dc
.DrawRectangle( left
, bottomRow
, cw
, bottom
- bottomRow
);
7690 void wxGrid::DrawCell( wxDC
& dc
, const wxGridCellCoords
& coords
)
7692 int row
= coords
.GetRow();
7693 int col
= coords
.GetCol();
7695 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7698 // we draw the cell border ourselves
7699 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7701 bool isCurrent
= coords
== m_currentCellCoords
;
7703 wxRect rect
= CellToRect( row
, col
);
7705 // if the editor is shown, we should use it and not the renderer
7706 // Note: However, only if it is really _shown_, i.e. not hidden!
7707 if ( isCurrent
&& IsCellEditControlShown() )
7709 // NB: this "#if..." is temporary and fixes a problem where the
7710 // edit control is erased by this code after being rendered.
7711 // On wxMac (QD build only), the cell editor is a wxTextCntl and is rendered
7712 // implicitly, causing this out-of order render.
7713 #if !defined(__WXMAC__)
7714 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
7715 editor
->PaintBackground(rect
, attr
);
7721 // but all the rest is drawn by the cell renderer and hence may be customized
7722 wxGridCellRenderer
*renderer
= attr
->GetRenderer(this, row
, col
);
7723 renderer
->Draw(*this, *attr
, dc
, rect
, row
, col
, IsInSelection(coords
));
7730 void wxGrid::DrawCellHighlight( wxDC
& dc
, const wxGridCellAttr
*attr
)
7732 // don't show highlight when the grid doesn't have focus
7736 int row
= m_currentCellCoords
.GetRow();
7737 int col
= m_currentCellCoords
.GetCol();
7739 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7742 wxRect rect
= CellToRect(row
, col
);
7744 // hmmm... what could we do here to show that the cell is disabled?
7745 // for now, I just draw a thinner border than for the other ones, but
7746 // it doesn't look really good
7748 int penWidth
= attr
->IsReadOnly() ? m_cellHighlightROPenWidth
: m_cellHighlightPenWidth
;
7752 // The center of the drawn line is where the position/width/height of
7753 // the rectangle is actually at (on wxMSW at least), so the
7754 // size of the rectangle is reduced to compensate for the thickness of
7755 // the line. If this is too strange on non-wxMSW platforms then
7756 // please #ifdef this appropriately.
7757 rect
.x
+= penWidth
/ 2;
7758 rect
.y
+= penWidth
/ 2;
7759 rect
.width
-= penWidth
- 1;
7760 rect
.height
-= penWidth
- 1;
7762 // Now draw the rectangle
7763 // use the cellHighlightColour if the cell is inside a selection, this
7764 // will ensure the cell is always visible.
7765 dc
.SetPen(wxPen(IsInSelection(row
,col
) ? m_selectionForeground
7766 : m_cellHighlightColour
,
7768 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
7769 dc
.DrawRectangle(rect
);
7773 // VZ: my experiments with 3D borders...
7775 // how to properly set colours for arbitrary bg?
7776 wxCoord x1
= rect
.x
,
7778 x2
= rect
.x
+ rect
.width
- 1,
7779 y2
= rect
.y
+ rect
.height
- 1;
7781 dc
.SetPen(*wxWHITE_PEN
);
7782 dc
.DrawLine(x1
, y1
, x2
, y1
);
7783 dc
.DrawLine(x1
, y1
, x1
, y2
);
7785 dc
.DrawLine(x1
+ 1, y2
- 1, x2
- 1, y2
- 1);
7786 dc
.DrawLine(x2
- 1, y1
+ 1, x2
- 1, y2
);
7788 dc
.SetPen(*wxBLACK_PEN
);
7789 dc
.DrawLine(x1
, y2
, x2
, y2
);
7790 dc
.DrawLine(x2
, y1
, x2
, y2
+ 1);
7794 wxPen
wxGrid::GetDefaultGridLinePen()
7796 return wxPen(GetGridLineColour());
7799 wxPen
wxGrid::GetRowGridLinePen(int WXUNUSED(row
))
7801 return GetDefaultGridLinePen();
7804 wxPen
wxGrid::GetColGridLinePen(int WXUNUSED(col
))
7806 return GetDefaultGridLinePen();
7809 void wxGrid::DrawCellBorder( wxDC
& dc
, const wxGridCellCoords
& coords
)
7811 int row
= coords
.GetRow();
7812 int col
= coords
.GetCol();
7813 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7817 wxRect rect
= CellToRect( row
, col
);
7819 // right hand border
7820 dc
.SetPen( GetColGridLinePen(col
) );
7821 dc
.DrawLine( rect
.x
+ rect
.width
, rect
.y
,
7822 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
+ 1 );
7825 dc
.SetPen( GetRowGridLinePen(row
) );
7826 dc
.DrawLine( rect
.x
, rect
.y
+ rect
.height
,
7827 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
);
7830 void wxGrid::DrawHighlight(wxDC
& dc
, const wxGridCellCoordsArray
& cells
)
7832 // This if block was previously in wxGrid::OnPaint but that doesn't
7833 // seem to get called under wxGTK - MB
7835 if ( m_currentCellCoords
== wxGridNoCellCoords
&&
7836 m_numRows
&& m_numCols
)
7838 m_currentCellCoords
.Set(0, 0);
7841 if ( IsCellEditControlShown() )
7843 // don't show highlight when the edit control is shown
7847 // if the active cell was repainted, repaint its highlight too because it
7848 // might have been damaged by the grid lines
7849 size_t count
= cells
.GetCount();
7850 for ( size_t n
= 0; n
< count
; n
++ )
7852 wxGridCellCoords cell
= cells
[n
];
7854 // If we are using attributes, then we may have just exposed another
7855 // cell in a partially-visible merged cluster of cells. If the "anchor"
7856 // (upper left) cell of this merged cluster is the cell indicated by
7857 // m_currentCellCoords, then we need to refresh the cell highlight even
7858 // though the "anchor" itself is not part of our update segment.
7859 if ( CanHaveAttributes() )
7863 GetCellSize(cell
.GetRow(), cell
.GetCol(), &rows
, &cols
);
7866 cell
.SetRow(cell
.GetRow() + rows
);
7869 cell
.SetCol(cell
.GetCol() + cols
);
7872 if ( cell
== m_currentCellCoords
)
7874 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
7875 DrawCellHighlight(dc
, attr
);
7883 // This is used to redraw all grid lines e.g. when the grid line colour
7886 void wxGrid::DrawAllGridLines( wxDC
& dc
, const wxRegion
& WXUNUSED(reg
) )
7888 if ( !m_gridLinesEnabled
|| !m_numRows
|| !m_numCols
)
7891 int top
, bottom
, left
, right
;
7894 m_gridWin
->GetClientSize(&cw
, &ch
);
7895 CalcUnscrolledPosition( 0, 0, &left
, &top
);
7896 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
7898 // avoid drawing grid lines past the last row and col
7900 right
= wxMin( right
, GetColRight(GetColAt( m_numCols
- 1 )) );
7901 bottom
= wxMin( bottom
, GetRowBottom(m_numRows
- 1) );
7903 // no gridlines inside multicells, clip them out
7904 int leftCol
= GetColPos( internalXToCol(left
) );
7905 int topRow
= internalYToRow(top
);
7906 int rightCol
= GetColPos( internalXToCol(right
) );
7907 int bottomRow
= internalYToRow(bottom
);
7909 wxRegion
clippedcells(0, 0, cw
, ch
);
7911 int cell_rows
, cell_cols
;
7914 for ( int j
= topRow
; j
<= bottomRow
; j
++ )
7916 for ( int colPos
= leftCol
; colPos
<= rightCol
; colPos
++ )
7918 int i
= GetColAt( colPos
);
7920 GetCellSize( j
, i
, &cell_rows
, &cell_cols
);
7921 if ((cell_rows
> 1) || (cell_cols
> 1))
7923 rect
= CellToRect(j
,i
);
7924 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7925 clippedcells
.Subtract(rect
);
7927 else if ((cell_rows
< 0) || (cell_cols
< 0))
7929 rect
= CellToRect(j
+ cell_rows
, i
+ cell_cols
);
7930 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7931 clippedcells
.Subtract(rect
);
7936 dc
.SetDeviceClippingRegion( clippedcells
);
7939 // horizontal grid lines
7940 for ( int i
= internalYToRow(top
); i
< m_numRows
; i
++ )
7942 int bot
= GetRowBottom(i
) - 1;
7949 dc
.SetPen( GetRowGridLinePen(i
) );
7950 dc
.DrawLine( left
, bot
, right
, bot
);
7954 // vertical grid lines
7955 for ( int colPos
= leftCol
; colPos
< m_numCols
; colPos
++ )
7957 int i
= GetColAt( colPos
);
7959 int colRight
= GetColRight(i
);
7961 if (GetLayoutDirection() != wxLayout_RightToLeft
)
7965 if ( colRight
> right
)
7968 if ( colRight
>= left
)
7970 dc
.SetPen( GetColGridLinePen(i
) );
7971 dc
.DrawLine( colRight
, top
, colRight
, bottom
);
7975 dc
.DestroyClippingRegion();
7978 void wxGrid::DrawRowLabels( wxDC
& dc
, const wxArrayInt
& rows
)
7983 const size_t numLabels
= rows
.GetCount();
7984 for ( size_t i
= 0; i
< numLabels
; i
++ )
7986 DrawRowLabel( dc
, rows
[i
] );
7990 void wxGrid::DrawRowLabel( wxDC
& dc
, int row
)
7992 if ( GetRowHeight(row
) <= 0 || m_rowLabelWidth
<= 0 )
7997 int rowTop
= GetRowTop(row
),
7998 rowBottom
= GetRowBottom(row
) - 1;
8000 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW
)));
8001 dc
.DrawLine( m_rowLabelWidth
- 1, rowTop
, m_rowLabelWidth
- 1, rowBottom
);
8002 dc
.DrawLine( 0, rowTop
, 0, rowBottom
);
8003 dc
.DrawLine( 0, rowBottom
, m_rowLabelWidth
, rowBottom
);
8005 dc
.SetPen( *wxWHITE_PEN
);
8006 dc
.DrawLine( 1, rowTop
, 1, rowBottom
);
8007 dc
.DrawLine( 1, rowTop
, m_rowLabelWidth
- 1, rowTop
);
8009 dc
.SetBackgroundMode( wxBRUSHSTYLE_TRANSPARENT
);
8010 dc
.SetTextForeground( GetLabelTextColour() );
8011 dc
.SetFont( GetLabelFont() );
8014 GetRowLabelAlignment( &hAlign
, &vAlign
);
8017 rect
.SetY( GetRowTop(row
) + 2 );
8018 rect
.SetWidth( m_rowLabelWidth
- 4 );
8019 rect
.SetHeight( GetRowHeight(row
) - 4 );
8020 DrawTextRectangle( dc
, GetRowLabelValue( row
), rect
, hAlign
, vAlign
);
8023 void wxGrid::SetUseNativeColLabels( bool native
)
8025 m_nativeColumnLabels
= native
;
8028 int height
= wxRendererNative::Get().GetHeaderButtonHeight( this );
8029 SetColLabelSize( height
);
8032 m_colLabelWin
->Refresh();
8033 m_cornerLabelWin
->Refresh();
8036 void wxGrid::DrawColLabels( wxDC
& dc
,const wxArrayInt
& cols
)
8041 const size_t numLabels
= cols
.GetCount();
8042 for ( size_t i
= 0; i
< numLabels
; i
++ )
8044 DrawColLabel( dc
, cols
[i
] );
8048 void wxGrid::DrawCornerLabel(wxDC
& dc
)
8050 if ( m_nativeColumnLabels
)
8052 wxRect
rect(wxSize(m_rowLabelWidth
, m_colLabelHeight
));
8055 wxRendererNative::Get().DrawHeaderButton(m_cornerLabelWin
, dc
, rect
, 0);
8059 dc
.SetPen(wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW
)));
8060 dc
.DrawLine( m_rowLabelWidth
- 1, m_colLabelHeight
- 1,
8061 m_rowLabelWidth
- 1, 0 );
8062 dc
.DrawLine( m_rowLabelWidth
- 1, m_colLabelHeight
- 1,
8063 0, m_colLabelHeight
- 1 );
8064 dc
.DrawLine( 0, 0, m_rowLabelWidth
, 0 );
8065 dc
.DrawLine( 0, 0, 0, m_colLabelHeight
);
8067 dc
.SetPen( *wxWHITE_PEN
);
8068 dc
.DrawLine( 1, 1, m_rowLabelWidth
- 1, 1 );
8069 dc
.DrawLine( 1, 1, 1, m_colLabelHeight
- 1 );
8073 void wxGrid::DrawColLabel(wxDC
& dc
, int col
)
8075 if ( GetColWidth(col
) <= 0 || m_colLabelHeight
<= 0 )
8078 int colLeft
= GetColLeft(col
);
8080 wxRect
rect(colLeft
, 0, GetColWidth(col
), m_colLabelHeight
);
8082 if ( m_nativeColumnLabels
)
8084 wxRendererNative::Get().DrawHeaderButton(m_colLabelWin
, dc
, rect
, 0);
8088 int colRight
= GetColRight(col
) - 1;
8090 dc
.SetPen(wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW
)));
8091 dc
.DrawLine( colRight
, 0,
8092 colRight
, m_colLabelHeight
- 1 );
8093 dc
.DrawLine( colLeft
, 0,
8095 dc
.DrawLine( colLeft
, m_colLabelHeight
- 1,
8096 colRight
+ 1, m_colLabelHeight
- 1 );
8098 dc
.SetPen( *wxWHITE_PEN
);
8099 dc
.DrawLine( colLeft
, 1, colLeft
, m_colLabelHeight
- 1 );
8100 dc
.DrawLine( colLeft
, 1, colRight
, 1 );
8103 dc
.SetBackgroundMode( wxBRUSHSTYLE_TRANSPARENT
);
8104 dc
.SetTextForeground( GetLabelTextColour() );
8105 dc
.SetFont( GetLabelFont() );
8108 GetColLabelAlignment( &hAlign
, &vAlign
);
8109 const int orient
= GetColLabelTextOrientation();
8112 DrawTextRectangle(dc
, GetColLabelValue(col
), rect
, hAlign
, vAlign
, orient
);
8115 // TODO: these 2 functions should be replaced with wxDC::DrawLabel() to which
8116 // we just have to add textOrientation support
8117 void wxGrid::DrawTextRectangle( wxDC
& dc
,
8118 const wxString
& value
,
8122 int textOrientation
)
8124 wxArrayString lines
;
8126 StringToLines( value
, lines
);
8128 DrawTextRectangle(dc
, lines
, rect
, horizAlign
, vertAlign
, textOrientation
);
8131 void wxGrid::DrawTextRectangle(wxDC
& dc
,
8132 const wxArrayString
& lines
,
8136 int textOrientation
)
8138 if ( lines
.empty() )
8141 wxDCClipper
clip(dc
, rect
);
8146 if ( textOrientation
== wxHORIZONTAL
)
8147 GetTextBoxSize( dc
, lines
, &textWidth
, &textHeight
);
8149 GetTextBoxSize( dc
, lines
, &textHeight
, &textWidth
);
8153 switch ( vertAlign
)
8155 case wxALIGN_BOTTOM
:
8156 if ( textOrientation
== wxHORIZONTAL
)
8157 y
= rect
.y
+ (rect
.height
- textHeight
- 1);
8159 x
= rect
.x
+ rect
.width
- textWidth
;
8162 case wxALIGN_CENTRE
:
8163 if ( textOrientation
== wxHORIZONTAL
)
8164 y
= rect
.y
+ ((rect
.height
- textHeight
) / 2);
8166 x
= rect
.x
+ ((rect
.width
- textWidth
) / 2);
8171 if ( textOrientation
== wxHORIZONTAL
)
8178 // Align each line of a multi-line label
8179 size_t nLines
= lines
.GetCount();
8180 for ( size_t l
= 0; l
< nLines
; l
++ )
8182 const wxString
& line
= lines
[l
];
8186 *(textOrientation
== wxHORIZONTAL
? &y
: &x
) += dc
.GetCharHeight();
8190 wxCoord lineWidth
= 0,
8192 dc
.GetTextExtent(line
, &lineWidth
, &lineHeight
);
8194 switch ( horizAlign
)
8197 if ( textOrientation
== wxHORIZONTAL
)
8198 x
= rect
.x
+ (rect
.width
- lineWidth
- 1);
8200 y
= rect
.y
+ lineWidth
+ 1;
8203 case wxALIGN_CENTRE
:
8204 if ( textOrientation
== wxHORIZONTAL
)
8205 x
= rect
.x
+ ((rect
.width
- lineWidth
) / 2);
8207 y
= rect
.y
+ rect
.height
- ((rect
.height
- lineWidth
) / 2);
8212 if ( textOrientation
== wxHORIZONTAL
)
8215 y
= rect
.y
+ rect
.height
- 1;
8219 if ( textOrientation
== wxHORIZONTAL
)
8221 dc
.DrawText( line
, x
, y
);
8226 dc
.DrawRotatedText( line
, x
, y
, 90.0 );
8232 // Split multi-line text up into an array of strings.
8233 // Any existing contents of the string array are preserved.
8235 // TODO: refactor wxTextFile::Read() and reuse the same code from here
8236 void wxGrid::StringToLines( const wxString
& value
, wxArrayString
& lines
) const
8240 wxString eol
= wxTextFile::GetEOL( wxTextFileType_Unix
);
8241 wxString tVal
= wxTextFile::Translate( value
, wxTextFileType_Unix
);
8243 while ( startPos
< (int)tVal
.length() )
8245 pos
= tVal
.Mid(startPos
).Find( eol
);
8250 else if ( pos
== 0 )
8252 lines
.Add( wxEmptyString
);
8256 lines
.Add( tVal
.Mid(startPos
, pos
) );
8259 startPos
+= pos
+ 1;
8262 if ( startPos
< (int)tVal
.length() )
8264 lines
.Add( tVal
.Mid( startPos
) );
8268 void wxGrid::GetTextBoxSize( const wxDC
& dc
,
8269 const wxArrayString
& lines
,
8270 long *width
, long *height
) const
8274 wxCoord lineW
= 0, lineH
= 0;
8277 for ( i
= 0; i
< lines
.GetCount(); i
++ )
8279 dc
.GetTextExtent( lines
[i
], &lineW
, &lineH
);
8280 w
= wxMax( w
, lineW
);
8289 // ------ Batch processing.
8291 void wxGrid::EndBatch()
8293 if ( m_batchCount
> 0 )
8296 if ( !m_batchCount
)
8299 m_rowLabelWin
->Refresh();
8300 m_colLabelWin
->Refresh();
8301 m_cornerLabelWin
->Refresh();
8302 m_gridWin
->Refresh();
8307 // Use this, rather than wxWindow::Refresh(), to force an immediate
8308 // repainting of the grid. Has no effect if you are already inside a
8309 // BeginBatch / EndBatch block.
8311 void wxGrid::ForceRefresh()
8317 bool wxGrid::Enable(bool enable
)
8319 if ( !wxScrolledWindow::Enable(enable
) )
8322 // redraw in the new state
8323 m_gridWin
->Refresh();
8329 // ------ Edit control functions
8332 void wxGrid::EnableEditing( bool edit
)
8334 // TODO: improve this ?
8336 if ( edit
!= m_editable
)
8339 EnableCellEditControl(edit
);
8344 void wxGrid::EnableCellEditControl( bool enable
)
8349 if ( enable
!= m_cellEditCtrlEnabled
)
8353 if (SendEvent( wxEVT_GRID_EDITOR_SHOWN
) <0)
8356 // this should be checked by the caller!
8357 wxASSERT_MSG( CanEnableCellControl(), _T("can't enable editing for this cell!") );
8359 // do it before ShowCellEditControl()
8360 m_cellEditCtrlEnabled
= enable
;
8362 ShowCellEditControl();
8366 //FIXME:add veto support
8367 SendEvent( wxEVT_GRID_EDITOR_HIDDEN
);
8369 HideCellEditControl();
8370 SaveEditControlValue();
8372 // do it after HideCellEditControl()
8373 m_cellEditCtrlEnabled
= enable
;
8378 bool wxGrid::IsCurrentCellReadOnly() const
8381 wxGridCellAttr
* attr
= ((wxGrid
*)this)->GetCellAttr(m_currentCellCoords
);
8382 bool readonly
= attr
->IsReadOnly();
8388 bool wxGrid::CanEnableCellControl() const
8390 return m_editable
&& (m_currentCellCoords
!= wxGridNoCellCoords
) &&
8391 !IsCurrentCellReadOnly();
8394 bool wxGrid::IsCellEditControlEnabled() const
8396 // the cell edit control might be disable for all cells or just for the
8397 // current one if it's read only
8398 return m_cellEditCtrlEnabled
? !IsCurrentCellReadOnly() : false;
8401 bool wxGrid::IsCellEditControlShown() const
8403 bool isShown
= false;
8405 if ( m_cellEditCtrlEnabled
)
8407 int row
= m_currentCellCoords
.GetRow();
8408 int col
= m_currentCellCoords
.GetCol();
8409 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8410 wxGridCellEditor
* editor
= attr
->GetEditor((wxGrid
*) this, row
, col
);
8415 if ( editor
->IsCreated() )
8417 isShown
= editor
->GetControl()->IsShown();
8427 void wxGrid::ShowCellEditControl()
8429 if ( IsCellEditControlEnabled() )
8431 if ( !IsVisible( m_currentCellCoords
, false ) )
8433 m_cellEditCtrlEnabled
= false;
8438 wxRect rect
= CellToRect( m_currentCellCoords
);
8439 int row
= m_currentCellCoords
.GetRow();
8440 int col
= m_currentCellCoords
.GetCol();
8442 // if this is part of a multicell, find owner (topleft)
8443 int cell_rows
, cell_cols
;
8444 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
8445 if ( cell_rows
<= 0 || cell_cols
<= 0 )
8449 m_currentCellCoords
.SetRow( row
);
8450 m_currentCellCoords
.SetCol( col
);
8453 // erase the highlight and the cell contents because the editor
8454 // might not cover the entire cell
8455 wxClientDC
dc( m_gridWin
);
8457 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8458 dc
.SetBrush(wxBrush(attr
->GetBackgroundColour()));
8459 dc
.SetPen(*wxTRANSPARENT_PEN
);
8460 dc
.DrawRectangle(rect
);
8462 // convert to scrolled coords
8463 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
8469 // cell is shifted by one pixel
8470 // However, don't allow x or y to become negative
8471 // since the SetSize() method interprets that as
8478 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
8479 if ( !editor
->IsCreated() )
8481 editor
->Create(m_gridWin
, wxID_ANY
,
8482 new wxGridCellEditorEvtHandler(this, editor
));
8484 wxGridEditorCreatedEvent
evt(GetId(),
8485 wxEVT_GRID_EDITOR_CREATED
,
8489 editor
->GetControl());
8490 GetEventHandler()->ProcessEvent(evt
);
8493 // resize editor to overflow into righthand cells if allowed
8494 int maxWidth
= rect
.width
;
8495 wxString value
= GetCellValue(row
, col
);
8496 if ( (value
!= wxEmptyString
) && (attr
->GetOverflow()) )
8499 GetTextExtent(value
, &maxWidth
, &y
, NULL
, NULL
, &attr
->GetFont());
8500 if (maxWidth
< rect
.width
)
8501 maxWidth
= rect
.width
;
8504 int client_right
= m_gridWin
->GetClientSize().GetWidth();
8505 if (rect
.x
+ maxWidth
> client_right
)
8506 maxWidth
= client_right
- rect
.x
;
8508 if ((maxWidth
> rect
.width
) && (col
< m_numCols
) && m_table
)
8510 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
8511 // may have changed earlier
8512 for (int i
= col
+ cell_cols
; i
< m_numCols
; i
++)
8515 GetCellSize( row
, i
, &c_rows
, &c_cols
);
8517 // looks weird going over a multicell
8518 if (m_table
->IsEmptyCell( row
, i
) &&
8519 (rect
.width
< maxWidth
) && (c_rows
== 1))
8521 rect
.width
+= GetColWidth( i
);
8527 if (rect
.GetRight() > client_right
)
8528 rect
.SetRight( client_right
- 1 );
8531 editor
->SetCellAttr( attr
);
8532 editor
->SetSize( rect
);
8534 editor
->GetControl()->Move(
8535 editor
->GetControl()->GetPosition().x
+ nXMove
,
8536 editor
->GetControl()->GetPosition().y
);
8537 editor
->Show( true, attr
);
8539 // recalc dimensions in case we need to
8540 // expand the scrolled window to account for editor
8543 editor
->BeginEdit(row
, col
, this);
8544 editor
->SetCellAttr(NULL
);
8552 void wxGrid::HideCellEditControl()
8554 if ( IsCellEditControlEnabled() )
8556 int row
= m_currentCellCoords
.GetRow();
8557 int col
= m_currentCellCoords
.GetCol();
8559 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8560 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
8561 const bool editorHadFocus
= editor
->GetControl()->HasFocus();
8562 editor
->Show( false );
8566 // return the focus to the grid itself if the editor had it
8568 // note that we must not do this unconditionally to avoid stealing
8569 // focus from the window which just received it if we are hiding the
8570 // editor precisely because we lost focus
8571 if ( editorHadFocus
)
8572 m_gridWin
->SetFocus();
8574 // refresh whole row to the right
8575 wxRect
rect( CellToRect(row
, col
) );
8576 CalcScrolledPosition(rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
8577 rect
.width
= m_gridWin
->GetClientSize().GetWidth() - rect
.x
;
8580 // ensure that the pixels under the focus ring get refreshed as well
8581 rect
.Inflate(10, 10);
8584 m_gridWin
->Refresh( false, &rect
);
8588 void wxGrid::SaveEditControlValue()
8590 if ( IsCellEditControlEnabled() )
8592 int row
= m_currentCellCoords
.GetRow();
8593 int col
= m_currentCellCoords
.GetCol();
8595 wxString oldval
= GetCellValue(row
, col
);
8597 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8598 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
8599 bool changed
= editor
->EndEdit(row
, col
, this);
8606 if ( SendEvent( wxEVT_GRID_CELL_CHANGE
,
8607 m_currentCellCoords
.GetRow(),
8608 m_currentCellCoords
.GetCol() ) < 0 )
8610 // Event has been vetoed, set the data back.
8611 SetCellValue(row
, col
, oldval
);
8618 // ------ Grid location functions
8619 // Note that all of these functions work with the logical coordinates of
8620 // grid cells and labels so you will need to convert from device
8621 // coordinates for mouse events etc.
8624 void wxGrid::XYToCell( int x
, int y
, wxGridCellCoords
& coords
) const
8626 int row
= YToRow(y
);
8627 int col
= XToCol(x
);
8629 if ( row
== -1 || col
== -1 )
8631 coords
= wxGridNoCellCoords
;
8635 coords
.Set( row
, col
);
8639 // compute row or column from some (unscrolled) coordinate value, using either
8640 // m_defaultRowHeight/m_defaultColWidth or binary search on array of
8641 // m_rowBottoms/m_colRights to do it quickly (linear search shouldn't be used
8644 wxGrid::PosToLine(int coord
,
8646 const wxGridOperations
& oper
) const
8648 const int numLines
= oper
.GetNumberOfLines(this);
8651 return clipToMinMax
&& numLines
> 0 ? oper
.GetLineAt(this, 0) : -1;
8653 const int defaultLineSize
= oper
.GetDefaultLineSize(this);
8654 wxCHECK_MSG( defaultLineSize
, -1, "can't have 0 default line size" );
8656 int maxPos
= coord
/ defaultLineSize
,
8659 // check for the simplest case: if we have no explicit line sizes
8660 // configured, then we already know the line this position falls in
8661 const wxArrayInt
& lineEnds
= oper
.GetLineEnds(this);
8662 if ( lineEnds
.empty() )
8664 if ( maxPos
< numLines
)
8667 return clipToMinMax
? numLines
- 1 : -1;
8671 // adjust maxPos before starting the binary search
8672 if ( maxPos
>= numLines
)
8674 maxPos
= numLines
- 1;
8678 if ( coord
>= lineEnds
[oper
.GetLineAt(this, maxPos
)])
8681 const int minDist
= oper
.GetMinimalAcceptableLineSize(this);
8683 maxPos
= coord
/ minDist
;
8685 maxPos
= numLines
- 1;
8688 if ( maxPos
>= numLines
)
8689 maxPos
= numLines
- 1;
8692 // check if the position is beyond the last column
8693 const int lineAtMaxPos
= oper
.GetLineAt(this, maxPos
);
8694 if ( coord
>= lineEnds
[lineAtMaxPos
] )
8695 return clipToMinMax
? lineAtMaxPos
: -1;
8697 // or before the first one
8698 const int lineAt0
= oper
.GetLineAt(this, 0);
8699 if ( coord
< lineEnds
[lineAt0
] )
8703 // finally do perform the binary search
8704 while ( minPos
< maxPos
)
8706 wxCHECK_MSG( lineEnds
[oper
.GetLineAt(this, minPos
)] <= coord
&&
8707 coord
< lineEnds
[oper
.GetLineAt(this, maxPos
)],
8709 "wxGrid: internal error in PosToLine()" );
8711 if ( coord
>= lineEnds
[oper
.GetLineAt(this, maxPos
- 1)] )
8712 return oper
.GetLineAt(this, maxPos
);
8716 const int median
= minPos
+ (maxPos
- minPos
+ 1) / 2;
8717 if ( coord
< lineEnds
[oper
.GetLineAt(this, median
)] )
8723 return oper
.GetLineAt(this, maxPos
);
8726 int wxGrid::YToRow(int y
, bool clipToMinMax
) const
8728 return PosToLine(y
, clipToMinMax
, wxGridRowOperations());
8731 int wxGrid::XToCol(int x
, bool clipToMinMax
) const
8733 return PosToLine(x
, clipToMinMax
, wxGridColumnOperations());
8736 // return the row number that that the y coord is near
8737 // the edge of, or -1 if not near an edge.
8738 // coords can only possibly be near an edge if
8739 // (a) the row/column is large enough to still allow for an "inner" area
8740 // that is _not_ nead the edge (i.e., if the height/width is smaller
8741 // than WXGRID_LABEL_EDGE_ZONE, coords are _never_ considered to be
8744 // (b) resizing rows/columns (the thing for which edge detection is
8745 // relevant at all) is enabled.
8747 int wxGrid::YToEdgeOfRow( int y
) const
8750 i
= internalYToRow(y
);
8752 if ( GetRowHeight(i
) > WXGRID_LABEL_EDGE_ZONE
&& CanDragRowSize() )
8754 // We know that we are in row i, test whether we are
8755 // close enough to lower or upper border, respectively.
8756 if ( abs(GetRowBottom(i
) - y
) < WXGRID_LABEL_EDGE_ZONE
)
8758 else if ( i
> 0 && y
- GetRowTop(i
) < WXGRID_LABEL_EDGE_ZONE
)
8765 // return the col number that that the x coord is near the edge of, or
8766 // -1 if not near an edge
8767 // See comment at YToEdgeOfRow for conditions on edge detection.
8769 int wxGrid::XToEdgeOfCol( int x
) const
8772 i
= internalXToCol(x
);
8774 if ( GetColWidth(i
) > WXGRID_LABEL_EDGE_ZONE
&& CanDragColSize() )
8776 // We know that we are in column i; test whether we are
8777 // close enough to right or left border, respectively.
8778 if ( abs(GetColRight(i
) - x
) < WXGRID_LABEL_EDGE_ZONE
)
8780 else if ( i
> 0 && x
- GetColLeft(i
) < WXGRID_LABEL_EDGE_ZONE
)
8787 wxRect
wxGrid::CellToRect( int row
, int col
) const
8789 wxRect
rect( -1, -1, -1, -1 );
8791 if ( row
>= 0 && row
< m_numRows
&&
8792 col
>= 0 && col
< m_numCols
)
8794 int i
, cell_rows
, cell_cols
;
8795 rect
.width
= rect
.height
= 0;
8796 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
8797 // if negative then find multicell owner
8802 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
8804 rect
.x
= GetColLeft(col
);
8805 rect
.y
= GetRowTop(row
);
8806 for (i
=col
; i
< col
+ cell_cols
; i
++)
8807 rect
.width
+= GetColWidth(i
);
8808 for (i
=row
; i
< row
+ cell_rows
; i
++)
8809 rect
.height
+= GetRowHeight(i
);
8812 // if grid lines are enabled, then the area of the cell is a bit smaller
8813 if (m_gridLinesEnabled
)
8822 bool wxGrid::IsVisible( int row
, int col
, bool wholeCellVisible
) const
8824 // get the cell rectangle in logical coords
8826 wxRect
r( CellToRect( row
, col
) );
8828 // convert to device coords
8830 int left
, top
, right
, bottom
;
8831 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
8832 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
8834 // check against the client area of the grid window
8836 m_gridWin
->GetClientSize( &cw
, &ch
);
8838 if ( wholeCellVisible
)
8840 // is the cell wholly visible ?
8841 return ( left
>= 0 && right
<= cw
&&
8842 top
>= 0 && bottom
<= ch
);
8846 // is the cell partly visible ?
8848 return ( ((left
>= 0 && left
< cw
) || (right
> 0 && right
<= cw
)) &&
8849 ((top
>= 0 && top
< ch
) || (bottom
> 0 && bottom
<= ch
)) );
8853 // make the specified cell location visible by doing a minimal amount
8856 void wxGrid::MakeCellVisible( int row
, int col
)
8859 int xpos
= -1, ypos
= -1;
8861 if ( row
>= 0 && row
< m_numRows
&&
8862 col
>= 0 && col
< m_numCols
)
8864 // get the cell rectangle in logical coords
8865 wxRect
r( CellToRect( row
, col
) );
8867 // convert to device coords
8868 int left
, top
, right
, bottom
;
8869 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
8870 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
8873 m_gridWin
->GetClientSize( &cw
, &ch
);
8879 else if ( bottom
> ch
)
8881 int h
= r
.GetHeight();
8883 for ( i
= row
- 1; i
>= 0; i
-- )
8885 int rowHeight
= GetRowHeight(i
);
8886 if ( h
+ rowHeight
> ch
)
8893 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
8894 // have rounding errors (this is important, because if we do,
8895 // we might not scroll at all and some cells won't be redrawn)
8897 // Sometimes GRID_SCROLL_LINE / 2 is not enough,
8898 // so just add a full scroll unit...
8899 ypos
+= m_scrollLineY
;
8902 // special handling for wide cells - show always left part of the cell!
8903 // Otherwise, e.g. when stepping from row to row, it would jump between
8904 // left and right part of the cell on every step!
8906 if ( left
< 0 || (right
- left
) >= cw
)
8910 else if ( right
> cw
)
8912 // position the view so that the cell is on the right
8914 CalcUnscrolledPosition(0, 0, &x0
, &y0
);
8915 xpos
= x0
+ (right
- cw
);
8917 // see comment for ypos above
8918 xpos
+= m_scrollLineX
;
8921 if ( xpos
!= -1 || ypos
!= -1 )
8924 xpos
/= m_scrollLineX
;
8926 ypos
/= m_scrollLineY
;
8927 Scroll( xpos
, ypos
);
8934 // ------ Grid cursor movement functions
8937 bool wxGrid::MoveCursorUp( bool expandSelection
)
8939 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8940 m_currentCellCoords
.GetRow() >= 0 )
8942 if ( expandSelection
)
8944 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8945 m_selectingKeyboard
= m_currentCellCoords
;
8946 if ( m_selectingKeyboard
.GetRow() > 0 )
8948 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() - 1 );
8949 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8950 m_selectingKeyboard
.GetCol() );
8951 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8954 else if ( m_currentCellCoords
.GetRow() > 0 )
8956 int row
= m_currentCellCoords
.GetRow() - 1;
8957 int col
= m_currentCellCoords
.GetCol();
8959 MakeCellVisible( row
, col
);
8960 SetCurrentCell( row
, col
);
8971 bool wxGrid::MoveCursorDown( bool expandSelection
)
8973 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8974 m_currentCellCoords
.GetRow() < m_numRows
)
8976 if ( expandSelection
)
8978 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8979 m_selectingKeyboard
= m_currentCellCoords
;
8980 if ( m_selectingKeyboard
.GetRow() < m_numRows
- 1 )
8982 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() + 1 );
8983 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8984 m_selectingKeyboard
.GetCol() );
8985 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8988 else if ( m_currentCellCoords
.GetRow() < m_numRows
- 1 )
8990 int row
= m_currentCellCoords
.GetRow() + 1;
8991 int col
= m_currentCellCoords
.GetCol();
8993 MakeCellVisible( row
, col
);
8994 SetCurrentCell( row
, col
);
9005 bool wxGrid::MoveCursorLeft( bool expandSelection
)
9007 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
9008 m_currentCellCoords
.GetCol() >= 0 )
9010 if ( expandSelection
)
9012 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
9013 m_selectingKeyboard
= m_currentCellCoords
;
9014 if ( m_selectingKeyboard
.GetCol() > 0 )
9016 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() - 1 );
9017 MakeCellVisible( m_selectingKeyboard
.GetRow(),
9018 m_selectingKeyboard
.GetCol() );
9019 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
9022 else if ( GetColPos( m_currentCellCoords
.GetCol() ) > 0 )
9024 int row
= m_currentCellCoords
.GetRow();
9025 int col
= GetColAt( GetColPos( m_currentCellCoords
.GetCol() ) - 1 );
9028 MakeCellVisible( row
, col
);
9029 SetCurrentCell( row
, col
);
9040 bool wxGrid::MoveCursorRight( bool expandSelection
)
9042 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
9043 m_currentCellCoords
.GetCol() < m_numCols
)
9045 if ( expandSelection
)
9047 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
9048 m_selectingKeyboard
= m_currentCellCoords
;
9049 if ( m_selectingKeyboard
.GetCol() < m_numCols
- 1 )
9051 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() + 1 );
9052 MakeCellVisible( m_selectingKeyboard
.GetRow(),
9053 m_selectingKeyboard
.GetCol() );
9054 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
9057 else if ( GetColPos( m_currentCellCoords
.GetCol() ) < m_numCols
- 1 )
9059 int row
= m_currentCellCoords
.GetRow();
9060 int col
= GetColAt( GetColPos( m_currentCellCoords
.GetCol() ) + 1 );
9063 MakeCellVisible( row
, col
);
9064 SetCurrentCell( row
, col
);
9075 bool wxGrid::MovePageUp()
9077 if ( m_currentCellCoords
== wxGridNoCellCoords
)
9080 int row
= m_currentCellCoords
.GetRow();
9084 m_gridWin
->GetClientSize( &cw
, &ch
);
9086 int y
= GetRowTop(row
);
9087 int newRow
= internalYToRow( y
- ch
+ 1 );
9089 if ( newRow
== row
)
9091 // row > 0, so newRow can never be less than 0 here.
9095 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
9096 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
9104 bool wxGrid::MovePageDown()
9106 if ( m_currentCellCoords
== wxGridNoCellCoords
)
9109 int row
= m_currentCellCoords
.GetRow();
9110 if ( (row
+ 1) < m_numRows
)
9113 m_gridWin
->GetClientSize( &cw
, &ch
);
9115 int y
= GetRowTop(row
);
9116 int newRow
= internalYToRow( y
+ ch
);
9117 if ( newRow
== row
)
9119 // row < m_numRows, so newRow can't overflow here.
9123 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
9124 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
9132 bool wxGrid::MoveCursorUpBlock( bool expandSelection
)
9135 m_currentCellCoords
!= wxGridNoCellCoords
&&
9136 m_currentCellCoords
.GetRow() > 0 )
9138 int row
= m_currentCellCoords
.GetRow();
9139 int col
= m_currentCellCoords
.GetCol();
9141 if ( m_table
->IsEmptyCell(row
, col
) )
9143 // starting in an empty cell: find the next block of
9149 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9153 else if ( m_table
->IsEmptyCell(row
- 1, col
) )
9155 // starting at the top of a block: find the next block
9161 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9167 // starting within a block: find the top of the block
9172 if ( m_table
->IsEmptyCell(row
, col
) )
9180 MakeCellVisible( row
, col
);
9181 if ( expandSelection
)
9183 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
9184 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
9189 SetCurrentCell( row
, col
);
9198 bool wxGrid::MoveCursorDownBlock( bool expandSelection
)
9201 m_currentCellCoords
!= wxGridNoCellCoords
&&
9202 m_currentCellCoords
.GetRow() < m_numRows
- 1 )
9204 int row
= m_currentCellCoords
.GetRow();
9205 int col
= m_currentCellCoords
.GetCol();
9207 if ( m_table
->IsEmptyCell(row
, col
) )
9209 // starting in an empty cell: find the next block of
9212 while ( row
< m_numRows
- 1 )
9215 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9219 else if ( m_table
->IsEmptyCell(row
+ 1, col
) )
9221 // starting at the bottom of a block: find the next block
9224 while ( row
< m_numRows
- 1 )
9227 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9233 // starting within a block: find the bottom of the block
9235 while ( row
< m_numRows
- 1 )
9238 if ( m_table
->IsEmptyCell(row
, col
) )
9246 MakeCellVisible( row
, col
);
9247 if ( expandSelection
)
9249 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
9250 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
9255 SetCurrentCell( row
, col
);
9264 bool wxGrid::MoveCursorLeftBlock( bool expandSelection
)
9267 m_currentCellCoords
!= wxGridNoCellCoords
&&
9268 m_currentCellCoords
.GetCol() > 0 )
9270 int row
= m_currentCellCoords
.GetRow();
9271 int col
= m_currentCellCoords
.GetCol();
9273 if ( m_table
->IsEmptyCell(row
, col
) )
9275 // starting in an empty cell: find the next block of
9281 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9285 else if ( m_table
->IsEmptyCell(row
, col
- 1) )
9287 // starting at the left of a block: find the next block
9293 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9299 // starting within a block: find the left of the block
9304 if ( m_table
->IsEmptyCell(row
, col
) )
9312 MakeCellVisible( row
, col
);
9313 if ( expandSelection
)
9315 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
9316 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
9321 SetCurrentCell( row
, col
);
9330 bool wxGrid::MoveCursorRightBlock( bool expandSelection
)
9333 m_currentCellCoords
!= wxGridNoCellCoords
&&
9334 m_currentCellCoords
.GetCol() < m_numCols
- 1 )
9336 int row
= m_currentCellCoords
.GetRow();
9337 int col
= m_currentCellCoords
.GetCol();
9339 if ( m_table
->IsEmptyCell(row
, col
) )
9341 // starting in an empty cell: find the next block of
9344 while ( col
< m_numCols
- 1 )
9347 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9351 else if ( m_table
->IsEmptyCell(row
, col
+ 1) )
9353 // starting at the right of a block: find the next block
9356 while ( col
< m_numCols
- 1 )
9359 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9365 // starting within a block: find the right of the block
9367 while ( col
< m_numCols
- 1 )
9370 if ( m_table
->IsEmptyCell(row
, col
) )
9378 MakeCellVisible( row
, col
);
9379 if ( expandSelection
)
9381 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
9382 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
9387 SetCurrentCell( row
, col
);
9397 // ------ Label values and formatting
9400 void wxGrid::GetRowLabelAlignment( int *horiz
, int *vert
) const
9403 *horiz
= m_rowLabelHorizAlign
;
9405 *vert
= m_rowLabelVertAlign
;
9408 void wxGrid::GetColLabelAlignment( int *horiz
, int *vert
) const
9411 *horiz
= m_colLabelHorizAlign
;
9413 *vert
= m_colLabelVertAlign
;
9416 int wxGrid::GetColLabelTextOrientation() const
9418 return m_colLabelTextOrientation
;
9421 wxString
wxGrid::GetRowLabelValue( int row
) const
9425 return m_table
->GetRowLabelValue( row
);
9435 wxString
wxGrid::GetColLabelValue( int col
) const
9439 return m_table
->GetColLabelValue( col
);
9449 void wxGrid::SetRowLabelSize( int width
)
9451 wxASSERT( width
>= 0 || width
== wxGRID_AUTOSIZE
);
9453 if ( width
== wxGRID_AUTOSIZE
)
9455 width
= CalcColOrRowLabelAreaMinSize(wxGRID_ROW
);
9458 if ( width
!= m_rowLabelWidth
)
9462 m_rowLabelWin
->Show( false );
9463 m_cornerLabelWin
->Show( false );
9465 else if ( m_rowLabelWidth
== 0 )
9467 m_rowLabelWin
->Show( true );
9468 if ( m_colLabelHeight
> 0 )
9469 m_cornerLabelWin
->Show( true );
9472 m_rowLabelWidth
= width
;
9474 wxScrolledWindow::Refresh( true );
9478 void wxGrid::SetColLabelSize( int height
)
9480 wxASSERT( height
>=0 || height
== wxGRID_AUTOSIZE
);
9482 if ( height
== wxGRID_AUTOSIZE
)
9484 height
= CalcColOrRowLabelAreaMinSize(wxGRID_COLUMN
);
9487 if ( height
!= m_colLabelHeight
)
9491 m_colLabelWin
->Show( false );
9492 m_cornerLabelWin
->Show( false );
9494 else if ( m_colLabelHeight
== 0 )
9496 m_colLabelWin
->Show( true );
9497 if ( m_rowLabelWidth
> 0 )
9498 m_cornerLabelWin
->Show( true );
9501 m_colLabelHeight
= height
;
9503 wxScrolledWindow::Refresh( true );
9507 void wxGrid::SetLabelBackgroundColour( const wxColour
& colour
)
9509 if ( m_labelBackgroundColour
!= colour
)
9511 m_labelBackgroundColour
= colour
;
9512 m_rowLabelWin
->SetBackgroundColour( colour
);
9513 m_colLabelWin
->SetBackgroundColour( colour
);
9514 m_cornerLabelWin
->SetBackgroundColour( colour
);
9516 if ( !GetBatchCount() )
9518 m_rowLabelWin
->Refresh();
9519 m_colLabelWin
->Refresh();
9520 m_cornerLabelWin
->Refresh();
9525 void wxGrid::SetLabelTextColour( const wxColour
& colour
)
9527 if ( m_labelTextColour
!= colour
)
9529 m_labelTextColour
= colour
;
9530 if ( !GetBatchCount() )
9532 m_rowLabelWin
->Refresh();
9533 m_colLabelWin
->Refresh();
9538 void wxGrid::SetLabelFont( const wxFont
& font
)
9541 if ( !GetBatchCount() )
9543 m_rowLabelWin
->Refresh();
9544 m_colLabelWin
->Refresh();
9548 void wxGrid::SetRowLabelAlignment( int horiz
, int vert
)
9550 // allow old (incorrect) defs to be used
9553 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
9554 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
9555 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
9560 case wxTOP
: vert
= wxALIGN_TOP
; break;
9561 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
9562 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
9565 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
9567 m_rowLabelHorizAlign
= horiz
;
9570 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
9572 m_rowLabelVertAlign
= vert
;
9575 if ( !GetBatchCount() )
9577 m_rowLabelWin
->Refresh();
9581 void wxGrid::SetColLabelAlignment( int horiz
, int vert
)
9583 // allow old (incorrect) defs to be used
9586 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
9587 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
9588 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
9593 case wxTOP
: vert
= wxALIGN_TOP
; break;
9594 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
9595 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
9598 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
9600 m_colLabelHorizAlign
= horiz
;
9603 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
9605 m_colLabelVertAlign
= vert
;
9608 if ( !GetBatchCount() )
9610 m_colLabelWin
->Refresh();
9614 // Note: under MSW, the default column label font must be changed because it
9615 // does not support vertical printing
9617 // Example: wxFont font(9, wxSWISS, wxNORMAL, wxBOLD);
9618 // pGrid->SetLabelFont(font);
9619 // pGrid->SetColLabelTextOrientation(wxVERTICAL);
9621 void wxGrid::SetColLabelTextOrientation( int textOrientation
)
9623 if ( textOrientation
== wxHORIZONTAL
|| textOrientation
== wxVERTICAL
)
9624 m_colLabelTextOrientation
= textOrientation
;
9626 if ( !GetBatchCount() )
9627 m_colLabelWin
->Refresh();
9630 void wxGrid::SetRowLabelValue( int row
, const wxString
& s
)
9634 m_table
->SetRowLabelValue( row
, s
);
9635 if ( !GetBatchCount() )
9637 wxRect rect
= CellToRect( row
, 0 );
9638 if ( rect
.height
> 0 )
9640 CalcScrolledPosition(0, rect
.y
, &rect
.x
, &rect
.y
);
9642 rect
.width
= m_rowLabelWidth
;
9643 m_rowLabelWin
->Refresh( true, &rect
);
9649 void wxGrid::SetColLabelValue( int col
, const wxString
& s
)
9653 m_table
->SetColLabelValue( col
, s
);
9654 if ( !GetBatchCount() )
9656 wxRect rect
= CellToRect( 0, col
);
9657 if ( rect
.width
> 0 )
9659 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &rect
.y
);
9661 rect
.height
= m_colLabelHeight
;
9662 m_colLabelWin
->Refresh( true, &rect
);
9668 void wxGrid::SetGridLineColour( const wxColour
& colour
)
9670 if ( m_gridLineColour
!= colour
)
9672 m_gridLineColour
= colour
;
9674 wxClientDC
dc( m_gridWin
);
9676 DrawAllGridLines( dc
, wxRegion() );
9680 void wxGrid::SetCellHighlightColour( const wxColour
& colour
)
9682 if ( m_cellHighlightColour
!= colour
)
9684 m_cellHighlightColour
= colour
;
9686 wxClientDC
dc( m_gridWin
);
9688 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
9689 DrawCellHighlight(dc
, attr
);
9694 void wxGrid::SetCellHighlightPenWidth(int width
)
9696 if (m_cellHighlightPenWidth
!= width
)
9698 m_cellHighlightPenWidth
= width
;
9700 // Just redrawing the cell highlight is not enough since that won't
9701 // make any visible change if the the thickness is getting smaller.
9702 int row
= m_currentCellCoords
.GetRow();
9703 int col
= m_currentCellCoords
.GetCol();
9704 if ( row
== -1 || col
== -1 || GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
9707 wxRect rect
= CellToRect(row
, col
);
9708 m_gridWin
->Refresh(true, &rect
);
9712 void wxGrid::SetCellHighlightROPenWidth(int width
)
9714 if (m_cellHighlightROPenWidth
!= width
)
9716 m_cellHighlightROPenWidth
= width
;
9718 // Just redrawing the cell highlight is not enough since that won't
9719 // make any visible change if the the thickness is getting smaller.
9720 int row
= m_currentCellCoords
.GetRow();
9721 int col
= m_currentCellCoords
.GetCol();
9722 if ( row
== -1 || col
== -1 ||
9723 GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
9726 wxRect rect
= CellToRect(row
, col
);
9727 m_gridWin
->Refresh(true, &rect
);
9731 void wxGrid::EnableGridLines( bool enable
)
9733 if ( enable
!= m_gridLinesEnabled
)
9735 m_gridLinesEnabled
= enable
;
9737 if ( !GetBatchCount() )
9741 wxClientDC
dc( m_gridWin
);
9743 DrawAllGridLines( dc
, wxRegion() );
9747 m_gridWin
->Refresh();
9753 int wxGrid::GetDefaultRowSize() const
9755 return m_defaultRowHeight
;
9758 int wxGrid::GetRowSize( int row
) const
9760 wxCHECK_MSG( row
>= 0 && row
< m_numRows
, 0, _T("invalid row index") );
9762 return GetRowHeight(row
);
9765 int wxGrid::GetDefaultColSize() const
9767 return m_defaultColWidth
;
9770 int wxGrid::GetColSize( int col
) const
9772 wxCHECK_MSG( col
>= 0 && col
< m_numCols
, 0, _T("invalid column index") );
9774 return GetColWidth(col
);
9777 // ============================================================================
9778 // access to the grid attributes: each of them has a default value in the grid
9779 // itself and may be overidden on a per-cell basis
9780 // ============================================================================
9782 // ----------------------------------------------------------------------------
9783 // setting default attributes
9784 // ----------------------------------------------------------------------------
9786 void wxGrid::SetDefaultCellBackgroundColour( const wxColour
& col
)
9788 m_defaultCellAttr
->SetBackgroundColour(col
);
9790 m_gridWin
->SetBackgroundColour(col
);
9794 void wxGrid::SetDefaultCellTextColour( const wxColour
& col
)
9796 m_defaultCellAttr
->SetTextColour(col
);
9799 void wxGrid::SetDefaultCellAlignment( int horiz
, int vert
)
9801 m_defaultCellAttr
->SetAlignment(horiz
, vert
);
9804 void wxGrid::SetDefaultCellOverflow( bool allow
)
9806 m_defaultCellAttr
->SetOverflow(allow
);
9809 void wxGrid::SetDefaultCellFont( const wxFont
& font
)
9811 m_defaultCellAttr
->SetFont(font
);
9814 // For editors and renderers the type registry takes precedence over the
9815 // default attr, so we need to register the new editor/renderer for the string
9816 // data type in order to make setting a default editor/renderer appear to
9819 void wxGrid::SetDefaultRenderer(wxGridCellRenderer
*renderer
)
9821 RegisterDataType(wxGRID_VALUE_STRING
,
9823 GetDefaultEditorForType(wxGRID_VALUE_STRING
));
9826 void wxGrid::SetDefaultEditor(wxGridCellEditor
*editor
)
9828 RegisterDataType(wxGRID_VALUE_STRING
,
9829 GetDefaultRendererForType(wxGRID_VALUE_STRING
),
9833 // ----------------------------------------------------------------------------
9834 // access to the default attributes
9835 // ----------------------------------------------------------------------------
9837 wxColour
wxGrid::GetDefaultCellBackgroundColour() const
9839 return m_defaultCellAttr
->GetBackgroundColour();
9842 wxColour
wxGrid::GetDefaultCellTextColour() const
9844 return m_defaultCellAttr
->GetTextColour();
9847 wxFont
wxGrid::GetDefaultCellFont() const
9849 return m_defaultCellAttr
->GetFont();
9852 void wxGrid::GetDefaultCellAlignment( int *horiz
, int *vert
) const
9854 m_defaultCellAttr
->GetAlignment(horiz
, vert
);
9857 bool wxGrid::GetDefaultCellOverflow() const
9859 return m_defaultCellAttr
->GetOverflow();
9862 wxGridCellRenderer
*wxGrid::GetDefaultRenderer() const
9864 return m_defaultCellAttr
->GetRenderer(NULL
, 0, 0);
9867 wxGridCellEditor
*wxGrid::GetDefaultEditor() const
9869 return m_defaultCellAttr
->GetEditor(NULL
, 0, 0);
9872 // ----------------------------------------------------------------------------
9873 // access to cell attributes
9874 // ----------------------------------------------------------------------------
9876 wxColour
wxGrid::GetCellBackgroundColour(int row
, int col
) const
9878 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9879 wxColour colour
= attr
->GetBackgroundColour();
9885 wxColour
wxGrid::GetCellTextColour( int row
, int col
) const
9887 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9888 wxColour colour
= attr
->GetTextColour();
9894 wxFont
wxGrid::GetCellFont( int row
, int col
) const
9896 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9897 wxFont font
= attr
->GetFont();
9903 void wxGrid::GetCellAlignment( int row
, int col
, int *horiz
, int *vert
) const
9905 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9906 attr
->GetAlignment(horiz
, vert
);
9910 bool wxGrid::GetCellOverflow( int row
, int col
) const
9912 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9913 bool allow
= attr
->GetOverflow();
9919 void wxGrid::GetCellSize( int row
, int col
, int *num_rows
, int *num_cols
) const
9921 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9922 attr
->GetSize( num_rows
, num_cols
);
9926 wxGridCellRenderer
* wxGrid::GetCellRenderer(int row
, int col
) const
9928 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9929 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
9935 wxGridCellEditor
* wxGrid::GetCellEditor(int row
, int col
) const
9937 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9938 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
9944 bool wxGrid::IsReadOnly(int row
, int col
) const
9946 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9947 bool isReadOnly
= attr
->IsReadOnly();
9953 // ----------------------------------------------------------------------------
9954 // attribute support: cache, automatic provider creation, ...
9955 // ----------------------------------------------------------------------------
9957 bool wxGrid::CanHaveAttributes() const
9964 return m_table
->CanHaveAttributes();
9967 void wxGrid::ClearAttrCache()
9969 if ( m_attrCache
.row
!= -1 )
9971 wxGridCellAttr
*oldAttr
= m_attrCache
.attr
;
9972 m_attrCache
.attr
= NULL
;
9973 m_attrCache
.row
= -1;
9974 // wxSafeDecRec(...) might cause event processing that accesses
9975 // the cached attribute, if one exists (e.g. by deleting the
9976 // editor stored within the attribute). Therefore it is important
9977 // to invalidate the cache before calling wxSafeDecRef!
9978 wxSafeDecRef(oldAttr
);
9982 void wxGrid::CacheAttr(int row
, int col
, wxGridCellAttr
*attr
) const
9986 wxGrid
*self
= (wxGrid
*)this; // const_cast
9988 self
->ClearAttrCache();
9989 self
->m_attrCache
.row
= row
;
9990 self
->m_attrCache
.col
= col
;
9991 self
->m_attrCache
.attr
= attr
;
9996 bool wxGrid::LookupAttr(int row
, int col
, wxGridCellAttr
**attr
) const
9998 if ( row
== m_attrCache
.row
&& col
== m_attrCache
.col
)
10000 *attr
= m_attrCache
.attr
;
10001 wxSafeIncRef(m_attrCache
.attr
);
10003 #ifdef DEBUG_ATTR_CACHE
10004 gs_nAttrCacheHits
++;
10011 #ifdef DEBUG_ATTR_CACHE
10012 gs_nAttrCacheMisses
++;
10019 wxGridCellAttr
*wxGrid::GetCellAttr(int row
, int col
) const
10021 wxGridCellAttr
*attr
= NULL
;
10022 // Additional test to avoid looking at the cache e.g. for
10023 // wxNoCellCoords, as this will confuse memory management.
10026 if ( !LookupAttr(row
, col
, &attr
) )
10028 attr
= m_table
? m_table
->GetAttr(row
, col
, wxGridCellAttr::Any
)
10029 : (wxGridCellAttr
*)NULL
;
10030 CacheAttr(row
, col
, attr
);
10036 attr
->SetDefAttr(m_defaultCellAttr
);
10040 attr
= m_defaultCellAttr
;
10047 wxGridCellAttr
*wxGrid::GetOrCreateCellAttr(int row
, int col
) const
10049 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
10050 bool canHave
= ((wxGrid
*)this)->CanHaveAttributes();
10052 wxCHECK_MSG( canHave
, attr
, _T("Cell attributes not allowed"));
10053 wxCHECK_MSG( m_table
, attr
, _T("must have a table") );
10055 attr
= m_table
->GetAttr(row
, col
, wxGridCellAttr::Cell
);
10058 attr
= new wxGridCellAttr(m_defaultCellAttr
);
10060 // artificially inc the ref count to match DecRef() in caller
10062 m_table
->SetAttr(attr
, row
, col
);
10068 // ----------------------------------------------------------------------------
10069 // setting column attributes (wrappers around SetColAttr)
10070 // ----------------------------------------------------------------------------
10072 void wxGrid::SetColFormatBool(int col
)
10074 SetColFormatCustom(col
, wxGRID_VALUE_BOOL
);
10077 void wxGrid::SetColFormatNumber(int col
)
10079 SetColFormatCustom(col
, wxGRID_VALUE_NUMBER
);
10082 void wxGrid::SetColFormatFloat(int col
, int width
, int precision
)
10084 wxString typeName
= wxGRID_VALUE_FLOAT
;
10085 if ( (width
!= -1) || (precision
!= -1) )
10087 typeName
<< _T(':') << width
<< _T(',') << precision
;
10090 SetColFormatCustom(col
, typeName
);
10093 void wxGrid::SetColFormatCustom(int col
, const wxString
& typeName
)
10095 wxGridCellAttr
*attr
= m_table
->GetAttr(-1, col
, wxGridCellAttr::Col
);
10097 attr
= new wxGridCellAttr
;
10098 wxGridCellRenderer
*renderer
= GetDefaultRendererForType(typeName
);
10099 attr
->SetRenderer(renderer
);
10100 wxGridCellEditor
*editor
= GetDefaultEditorForType(typeName
);
10101 attr
->SetEditor(editor
);
10103 SetColAttr(col
, attr
);
10107 // ----------------------------------------------------------------------------
10108 // setting cell attributes: this is forwarded to the table
10109 // ----------------------------------------------------------------------------
10111 void wxGrid::SetAttr(int row
, int col
, wxGridCellAttr
*attr
)
10113 if ( CanHaveAttributes() )
10115 m_table
->SetAttr(attr
, row
, col
);
10120 wxSafeDecRef(attr
);
10124 void wxGrid::SetRowAttr(int row
, wxGridCellAttr
*attr
)
10126 if ( CanHaveAttributes() )
10128 m_table
->SetRowAttr(attr
, row
);
10133 wxSafeDecRef(attr
);
10137 void wxGrid::SetColAttr(int col
, wxGridCellAttr
*attr
)
10139 if ( CanHaveAttributes() )
10141 m_table
->SetColAttr(attr
, col
);
10146 wxSafeDecRef(attr
);
10150 void wxGrid::SetCellBackgroundColour( int row
, int col
, const wxColour
& colour
)
10152 if ( CanHaveAttributes() )
10154 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10155 attr
->SetBackgroundColour(colour
);
10160 void wxGrid::SetCellTextColour( int row
, int col
, const wxColour
& colour
)
10162 if ( CanHaveAttributes() )
10164 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10165 attr
->SetTextColour(colour
);
10170 void wxGrid::SetCellFont( int row
, int col
, const wxFont
& font
)
10172 if ( CanHaveAttributes() )
10174 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10175 attr
->SetFont(font
);
10180 void wxGrid::SetCellAlignment( int row
, int col
, int horiz
, int vert
)
10182 if ( CanHaveAttributes() )
10184 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10185 attr
->SetAlignment(horiz
, vert
);
10190 void wxGrid::SetCellOverflow( int row
, int col
, bool allow
)
10192 if ( CanHaveAttributes() )
10194 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10195 attr
->SetOverflow(allow
);
10200 void wxGrid::SetCellSize( int row
, int col
, int num_rows
, int num_cols
)
10202 if ( CanHaveAttributes() )
10204 int cell_rows
, cell_cols
;
10206 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10207 attr
->GetSize(&cell_rows
, &cell_cols
);
10208 attr
->SetSize(num_rows
, num_cols
);
10211 // Cannot set the size of a cell to 0 or negative values
10212 // While it is perfectly legal to do that, this function cannot
10213 // handle all the possibilies, do it by hand by getting the CellAttr.
10214 // You can only set the size of a cell to 1,1 or greater with this fn
10215 wxASSERT_MSG( !((cell_rows
< 1) || (cell_cols
< 1)),
10216 wxT("wxGrid::SetCellSize setting cell size that is already part of another cell"));
10217 wxASSERT_MSG( !((num_rows
< 1) || (num_cols
< 1)),
10218 wxT("wxGrid::SetCellSize setting cell size to < 1"));
10220 // if this was already a multicell then "turn off" the other cells first
10221 if ((cell_rows
> 1) || (cell_cols
> 1))
10224 for (j
=row
; j
< row
+ cell_rows
; j
++)
10226 for (i
=col
; i
< col
+ cell_cols
; i
++)
10228 if ((i
!= col
) || (j
!= row
))
10230 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
10231 attr_stub
->SetSize( 1, 1 );
10232 attr_stub
->DecRef();
10238 // mark the cells that will be covered by this cell to
10239 // negative or zero values to point back at this cell
10240 if (((num_rows
> 1) || (num_cols
> 1)) && (num_rows
>= 1) && (num_cols
>= 1))
10243 for (j
=row
; j
< row
+ num_rows
; j
++)
10245 for (i
=col
; i
< col
+ num_cols
; i
++)
10247 if ((i
!= col
) || (j
!= row
))
10249 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
10250 attr_stub
->SetSize( row
- j
, col
- i
);
10251 attr_stub
->DecRef();
10259 void wxGrid::SetCellRenderer(int row
, int col
, wxGridCellRenderer
*renderer
)
10261 if ( CanHaveAttributes() )
10263 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10264 attr
->SetRenderer(renderer
);
10269 void wxGrid::SetCellEditor(int row
, int col
, wxGridCellEditor
* editor
)
10271 if ( CanHaveAttributes() )
10273 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10274 attr
->SetEditor(editor
);
10279 void wxGrid::SetReadOnly(int row
, int col
, bool isReadOnly
)
10281 if ( CanHaveAttributes() )
10283 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10284 attr
->SetReadOnly(isReadOnly
);
10289 // ----------------------------------------------------------------------------
10290 // Data type registration
10291 // ----------------------------------------------------------------------------
10293 void wxGrid::RegisterDataType(const wxString
& typeName
,
10294 wxGridCellRenderer
* renderer
,
10295 wxGridCellEditor
* editor
)
10297 m_typeRegistry
->RegisterDataType(typeName
, renderer
, editor
);
10301 wxGridCellEditor
* wxGrid::GetDefaultEditorForCell(int row
, int col
) const
10303 wxString typeName
= m_table
->GetTypeName(row
, col
);
10304 return GetDefaultEditorForType(typeName
);
10307 wxGridCellRenderer
* wxGrid::GetDefaultRendererForCell(int row
, int col
) const
10309 wxString typeName
= m_table
->GetTypeName(row
, col
);
10310 return GetDefaultRendererForType(typeName
);
10313 wxGridCellEditor
* wxGrid::GetDefaultEditorForType(const wxString
& typeName
) const
10315 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
10316 if ( index
== wxNOT_FOUND
)
10318 wxFAIL_MSG(wxString::Format(wxT("Unknown data type name [%s]"), typeName
.c_str()));
10323 return m_typeRegistry
->GetEditor(index
);
10326 wxGridCellRenderer
* wxGrid::GetDefaultRendererForType(const wxString
& typeName
) const
10328 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
10329 if ( index
== wxNOT_FOUND
)
10331 wxFAIL_MSG(wxString::Format(wxT("Unknown data type name [%s]"), typeName
.c_str()));
10336 return m_typeRegistry
->GetRenderer(index
);
10339 // ----------------------------------------------------------------------------
10341 // ----------------------------------------------------------------------------
10343 void wxGrid::EnableDragRowSize( bool enable
)
10345 m_canDragRowSize
= enable
;
10348 void wxGrid::EnableDragColSize( bool enable
)
10350 m_canDragColSize
= enable
;
10353 void wxGrid::EnableDragGridSize( bool enable
)
10355 m_canDragGridSize
= enable
;
10358 void wxGrid::EnableDragCell( bool enable
)
10360 m_canDragCell
= enable
;
10363 void wxGrid::SetDefaultRowSize( int height
, bool resizeExistingRows
)
10365 m_defaultRowHeight
= wxMax( height
, m_minAcceptableRowHeight
);
10367 if ( resizeExistingRows
)
10369 // since we are resizing all rows to the default row size,
10370 // we can simply clear the row heights and row bottoms
10371 // arrays (which also allows us to take advantage of
10372 // some speed optimisations)
10373 m_rowHeights
.Empty();
10374 m_rowBottoms
.Empty();
10375 if ( !GetBatchCount() )
10380 void wxGrid::SetRowSize( int row
, int height
)
10382 wxCHECK_RET( row
>= 0 && row
< m_numRows
, _T("invalid row index") );
10384 // if < 0 then calculate new height from label
10388 wxArrayString lines
;
10389 wxClientDC
dc(m_rowLabelWin
);
10390 dc
.SetFont(GetLabelFont());
10391 StringToLines(GetRowLabelValue( row
), lines
);
10392 GetTextBoxSize( dc
, lines
, &w
, &h
);
10393 //check that it is not less than the minimal height
10394 height
= wxMax(h
, GetRowMinimalAcceptableHeight());
10397 // See comment in SetColSize
10398 if ( height
< GetRowMinimalAcceptableHeight())
10401 if ( m_rowHeights
.IsEmpty() )
10403 // need to really create the array
10407 int h
= wxMax( 0, height
);
10408 int diff
= h
- m_rowHeights
[row
];
10410 m_rowHeights
[row
] = h
;
10411 for ( int i
= row
; i
< m_numRows
; i
++ )
10413 m_rowBottoms
[i
] += diff
;
10416 if ( !GetBatchCount() )
10420 void wxGrid::SetDefaultColSize( int width
, bool resizeExistingCols
)
10422 // we dont allow zero default column width
10423 m_defaultColWidth
= wxMax( wxMax( width
, m_minAcceptableColWidth
), 1 );
10425 if ( resizeExistingCols
)
10427 // since we are resizing all columns to the default column size,
10428 // we can simply clear the col widths and col rights
10429 // arrays (which also allows us to take advantage of
10430 // some speed optimisations)
10431 m_colWidths
.Empty();
10432 m_colRights
.Empty();
10433 if ( !GetBatchCount() )
10438 void wxGrid::SetColSize( int col
, int width
)
10440 wxCHECK_RET( col
>= 0 && col
< m_numCols
, _T("invalid column index") );
10442 // if < 0 then calculate new width from label
10446 wxArrayString lines
;
10447 wxClientDC
dc(m_colLabelWin
);
10448 dc
.SetFont(GetLabelFont());
10449 StringToLines(GetColLabelValue(col
), lines
);
10450 if ( GetColLabelTextOrientation() == wxHORIZONTAL
)
10451 GetTextBoxSize( dc
, lines
, &w
, &h
);
10453 GetTextBoxSize( dc
, lines
, &h
, &w
);
10455 //check that it is not less than the minimal width
10456 width
= wxMax(width
, GetColMinimalAcceptableWidth());
10459 // should we check that it's bigger than GetColMinimalWidth(col) here?
10461 // No, because it is reasonable to assume the library user know's
10462 // what he is doing. However we should test against the weaker
10463 // constraint of minimalAcceptableWidth, as this breaks rendering
10465 // This test then fixes sf.net bug #645734
10467 if ( width
< GetColMinimalAcceptableWidth() )
10470 if ( m_colWidths
.IsEmpty() )
10472 // need to really create the array
10476 int w
= wxMax( 0, width
);
10477 int diff
= w
- m_colWidths
[col
];
10478 m_colWidths
[col
] = w
;
10480 for ( int colPos
= GetColPos(col
); colPos
< m_numCols
; colPos
++ )
10482 m_colRights
[GetColAt(colPos
)] += diff
;
10485 if ( !GetBatchCount() )
10489 void wxGrid::SetColMinimalWidth( int col
, int width
)
10491 if (width
> GetColMinimalAcceptableWidth())
10493 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)col
;
10494 m_colMinWidths
[key
] = width
;
10498 void wxGrid::SetRowMinimalHeight( int row
, int width
)
10500 if (width
> GetRowMinimalAcceptableHeight())
10502 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)row
;
10503 m_rowMinHeights
[key
] = width
;
10507 int wxGrid::GetColMinimalWidth(int col
) const
10509 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)col
;
10510 wxLongToLongHashMap::const_iterator it
= m_colMinWidths
.find(key
);
10512 return it
!= m_colMinWidths
.end() ? (int)it
->second
: m_minAcceptableColWidth
;
10515 int wxGrid::GetRowMinimalHeight(int row
) const
10517 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)row
;
10518 wxLongToLongHashMap::const_iterator it
= m_rowMinHeights
.find(key
);
10520 return it
!= m_rowMinHeights
.end() ? (int)it
->second
: m_minAcceptableRowHeight
;
10523 void wxGrid::SetColMinimalAcceptableWidth( int width
)
10525 // We do allow a width of 0 since this gives us
10526 // an easy way to temporarily hiding columns.
10528 m_minAcceptableColWidth
= width
;
10531 void wxGrid::SetRowMinimalAcceptableHeight( int height
)
10533 // We do allow a height of 0 since this gives us
10534 // an easy way to temporarily hiding rows.
10536 m_minAcceptableRowHeight
= height
;
10539 int wxGrid::GetColMinimalAcceptableWidth() const
10541 return m_minAcceptableColWidth
;
10544 int wxGrid::GetRowMinimalAcceptableHeight() const
10546 return m_minAcceptableRowHeight
;
10549 // ----------------------------------------------------------------------------
10551 // ----------------------------------------------------------------------------
10554 wxGrid::AutoSizeColOrRow(int colOrRow
, bool setAsMin
, wxGridDirection direction
)
10556 const bool column
= direction
== wxGRID_COLUMN
;
10558 wxClientDC
dc(m_gridWin
);
10560 // cancel editing of cell
10561 HideCellEditControl();
10562 SaveEditControlValue();
10564 // init both of them to avoid compiler warnings, even if we only need one
10572 wxCoord extent
, extentMax
= 0;
10573 int max
= column
? m_numRows
: m_numCols
;
10574 for ( int rowOrCol
= 0; rowOrCol
< max
; rowOrCol
++ )
10581 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
10582 wxGridCellRenderer
*renderer
= attr
->GetRenderer(this, row
, col
);
10585 wxSize size
= renderer
->GetBestSize(*this, *attr
, dc
, row
, col
);
10586 extent
= column
? size
.x
: size
.y
;
10587 if ( extent
> extentMax
)
10588 extentMax
= extent
;
10590 renderer
->DecRef();
10596 // now also compare with the column label extent
10598 dc
.SetFont( GetLabelFont() );
10602 dc
.GetMultiLineTextExtent( GetColLabelValue(col
), &w
, &h
);
10603 if ( GetColLabelTextOrientation() == wxVERTICAL
)
10607 dc
.GetMultiLineTextExtent( GetRowLabelValue(row
), &w
, &h
);
10609 extent
= column
? w
: h
;
10610 if ( extent
> extentMax
)
10611 extentMax
= extent
;
10615 // empty column - give default extent (notice that if extentMax is less
10616 // than default extent but != 0, it's OK)
10617 extentMax
= column
? m_defaultColWidth
: m_defaultRowHeight
;
10622 // leave some space around text
10630 // Ensure automatic width is not less than minimal width. See the
10631 // comment in SetColSize() for explanation of why this isn't done
10632 // in SetColSize().
10634 extentMax
= wxMax(extentMax
, GetColMinimalWidth(col
));
10636 SetColSize( col
, extentMax
);
10637 if ( !GetBatchCount() )
10640 m_gridWin
->GetClientSize( &cw
, &ch
);
10641 wxRect
rect ( CellToRect( 0, col
) );
10643 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
10644 rect
.width
= cw
- rect
.x
;
10645 rect
.height
= m_colLabelHeight
;
10646 m_colLabelWin
->Refresh( true, &rect
);
10651 // Ensure automatic width is not less than minimal height. See the
10652 // comment in SetColSize() for explanation of why this isn't done
10653 // in SetRowSize().
10655 extentMax
= wxMax(extentMax
, GetRowMinimalHeight(row
));
10657 SetRowSize(row
, extentMax
);
10658 if ( !GetBatchCount() )
10661 m_gridWin
->GetClientSize( &cw
, &ch
);
10662 wxRect
rect( CellToRect( row
, 0 ) );
10664 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
10665 rect
.width
= m_rowLabelWidth
;
10666 rect
.height
= ch
- rect
.y
;
10667 m_rowLabelWin
->Refresh( true, &rect
);
10674 SetColMinimalWidth(col
, extentMax
);
10676 SetRowMinimalHeight(row
, extentMax
);
10680 wxCoord
wxGrid::CalcColOrRowLabelAreaMinSize(wxGridDirection direction
)
10682 // calculate size for the rows or columns?
10683 const bool calcRows
= direction
== wxGRID_ROW
;
10685 wxClientDC
dc(calcRows
? GetGridRowLabelWindow()
10686 : GetGridColLabelWindow());
10687 dc
.SetFont(GetLabelFont());
10689 // which dimension should we take into account for calculations?
10691 // for columns, the text can be only horizontal so it's easy but for rows
10692 // we also have to take into account the text orientation
10694 useWidth
= calcRows
|| (GetColLabelTextOrientation() == wxVERTICAL
);
10696 wxArrayString lines
;
10697 wxCoord extentMax
= 0;
10699 const int numRowsOrCols
= calcRows
? m_numRows
: m_numCols
;
10700 for ( int rowOrCol
= 0; rowOrCol
< numRowsOrCols
; rowOrCol
++ )
10704 wxString label
= calcRows
? GetRowLabelValue(rowOrCol
)
10705 : GetColLabelValue(rowOrCol
);
10706 StringToLines(label
, lines
);
10709 GetTextBoxSize(dc
, lines
, &w
, &h
);
10711 const wxCoord extent
= useWidth
? w
: h
;
10712 if ( extent
> extentMax
)
10713 extentMax
= extent
;
10718 // empty column - give default extent (notice that if extentMax is less
10719 // than default extent but != 0, it's OK)
10720 extentMax
= calcRows
? GetDefaultRowLabelSize()
10721 : GetDefaultColLabelSize();
10724 // leave some space around text (taken from AutoSizeColOrRow)
10733 int wxGrid::SetOrCalcColumnSizes(bool calcOnly
, bool setAsMin
)
10735 int width
= m_rowLabelWidth
;
10737 wxGridUpdateLocker locker
;
10739 locker
.Create(this);
10741 for ( int col
= 0; col
< m_numCols
; col
++ )
10744 AutoSizeColumn(col
, setAsMin
);
10746 width
+= GetColWidth(col
);
10752 int wxGrid::SetOrCalcRowSizes(bool calcOnly
, bool setAsMin
)
10754 int height
= m_colLabelHeight
;
10756 wxGridUpdateLocker locker
;
10758 locker
.Create(this);
10760 for ( int row
= 0; row
< m_numRows
; row
++ )
10763 AutoSizeRow(row
, setAsMin
);
10765 height
+= GetRowHeight(row
);
10771 void wxGrid::AutoSize()
10773 wxGridUpdateLocker
locker(this);
10775 wxSize
size(SetOrCalcColumnSizes(false) - m_rowLabelWidth
+ m_extraWidth
,
10776 SetOrCalcRowSizes(false) - m_colLabelHeight
+ m_extraHeight
);
10778 // we know that we're not going to have scrollbars so disable them now to
10779 // avoid trouble in SetClientSize() which can otherwise set the correct
10780 // client size but also leave space for (not needed any more) scrollbars
10781 SetScrollbars(0, 0, 0, 0, 0, 0, true);
10783 // restore the scroll rate parameters overwritten by SetScrollbars()
10784 SetScrollRate(m_scrollLineX
, m_scrollLineY
);
10786 SetClientSize(size
.x
+ m_rowLabelWidth
, size
.y
+ m_colLabelHeight
);
10789 void wxGrid::AutoSizeRowLabelSize( int row
)
10791 // Hide the edit control, so it
10792 // won't interfere with drag-shrinking.
10793 if ( IsCellEditControlShown() )
10795 HideCellEditControl();
10796 SaveEditControlValue();
10799 // autosize row height depending on label text
10800 SetRowSize(row
, -1);
10804 void wxGrid::AutoSizeColLabelSize( int col
)
10806 // Hide the edit control, so it
10807 // won't interfere with drag-shrinking.
10808 if ( IsCellEditControlShown() )
10810 HideCellEditControl();
10811 SaveEditControlValue();
10814 // autosize column width depending on label text
10815 SetColSize(col
, -1);
10819 wxSize
wxGrid::DoGetBestSize() const
10821 wxGrid
*self
= (wxGrid
*)this; // const_cast
10823 // we do the same as in AutoSize() here with the exception that we don't
10824 // change the column/row sizes, only calculate them
10825 wxSize
size(self
->SetOrCalcColumnSizes(true) - m_rowLabelWidth
+ m_extraWidth
,
10826 self
->SetOrCalcRowSizes(true) - m_colLabelHeight
+ m_extraHeight
);
10828 // NOTE: This size should be cached, but first we need to add calls to
10829 // InvalidateBestSize everywhere that could change the results of this
10831 // CacheBestSize(size);
10833 return wxSize(size
.x
+ m_rowLabelWidth
, size
.y
+ m_colLabelHeight
)
10834 + GetWindowBorderSize();
10842 wxPen
& wxGrid::GetDividerPen() const
10847 // ----------------------------------------------------------------------------
10848 // cell value accessor functions
10849 // ----------------------------------------------------------------------------
10851 void wxGrid::SetCellValue( int row
, int col
, const wxString
& s
)
10855 m_table
->SetValue( row
, col
, s
);
10856 if ( !GetBatchCount() )
10859 wxRect
rect( CellToRect( row
, col
) );
10861 rect
.width
= m_gridWin
->GetClientSize().GetWidth();
10862 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
10863 m_gridWin
->Refresh( false, &rect
);
10866 if ( m_currentCellCoords
.GetRow() == row
&&
10867 m_currentCellCoords
.GetCol() == col
&&
10868 IsCellEditControlShown())
10869 // Note: If we are using IsCellEditControlEnabled,
10870 // this interacts badly with calling SetCellValue from
10871 // an EVT_GRID_CELL_CHANGE handler.
10873 HideCellEditControl();
10874 ShowCellEditControl(); // will reread data from table
10879 // ----------------------------------------------------------------------------
10880 // block, row and column selection
10881 // ----------------------------------------------------------------------------
10883 void wxGrid::SelectRow( int row
, bool addToSelected
)
10885 if ( IsSelection() && !addToSelected
)
10889 m_selection
->SelectRow( row
, false, addToSelected
);
10892 void wxGrid::SelectCol( int col
, bool addToSelected
)
10894 if ( IsSelection() && !addToSelected
)
10898 m_selection
->SelectCol( col
, false, addToSelected
);
10901 void wxGrid::SelectBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
,
10902 bool addToSelected
)
10904 if ( IsSelection() && !addToSelected
)
10908 m_selection
->SelectBlock( topRow
, leftCol
, bottomRow
, rightCol
,
10909 false, addToSelected
);
10912 void wxGrid::SelectAll()
10914 if ( m_numRows
> 0 && m_numCols
> 0 )
10917 m_selection
->SelectBlock( 0, 0, m_numRows
- 1, m_numCols
- 1 );
10921 // ----------------------------------------------------------------------------
10922 // cell, row and col deselection
10923 // ----------------------------------------------------------------------------
10925 void wxGrid::DeselectLine(int line
, const wxGridOperations
& oper
)
10927 if ( !m_selection
)
10930 const wxGridSelectionModes mode
= m_selection
->GetSelectionMode();
10931 if ( mode
== oper
.GetSelectionMode() )
10933 const wxGridCellCoords
c(oper
.MakeCoords(line
, 0));
10934 if ( m_selection
->IsInSelection(c
) )
10935 m_selection
->ToggleCellSelection(c
);
10937 else if ( mode
!= oper
.Dual().GetSelectionMode() )
10939 const int nOther
= oper
.Dual().GetNumberOfLines(this);
10940 for ( int i
= 0; i
< nOther
; i
++ )
10942 const wxGridCellCoords
c(oper
.MakeCoords(line
, i
));
10943 if ( m_selection
->IsInSelection(c
) )
10944 m_selection
->ToggleCellSelection(c
);
10947 //else: can only select orthogonal lines so no lines in this direction
10948 // could have been selected anyhow
10951 void wxGrid::DeselectRow(int row
)
10953 DeselectLine(row
, wxGridRowOperations());
10956 void wxGrid::DeselectCol(int col
)
10958 DeselectLine(col
, wxGridColumnOperations());
10961 void wxGrid::DeselectCell( int row
, int col
)
10963 if ( m_selection
&& m_selection
->IsInSelection(row
, col
) )
10964 m_selection
->ToggleCellSelection(row
, col
);
10967 bool wxGrid::IsSelection() const
10969 return ( m_selection
&& (m_selection
->IsSelection() ||
10970 ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
10971 m_selectingBottomRight
!= wxGridNoCellCoords
) ) );
10974 bool wxGrid::IsInSelection( int row
, int col
) const
10976 return ( m_selection
&& (m_selection
->IsInSelection( row
, col
) ||
10977 ( row
>= m_selectingTopLeft
.GetRow() &&
10978 col
>= m_selectingTopLeft
.GetCol() &&
10979 row
<= m_selectingBottomRight
.GetRow() &&
10980 col
<= m_selectingBottomRight
.GetCol() )) );
10983 wxGridCellCoordsArray
wxGrid::GetSelectedCells() const
10987 wxGridCellCoordsArray a
;
10991 return m_selection
->m_cellSelection
;
10994 wxGridCellCoordsArray
wxGrid::GetSelectionBlockTopLeft() const
10998 wxGridCellCoordsArray a
;
11002 return m_selection
->m_blockSelectionTopLeft
;
11005 wxGridCellCoordsArray
wxGrid::GetSelectionBlockBottomRight() const
11009 wxGridCellCoordsArray a
;
11013 return m_selection
->m_blockSelectionBottomRight
;
11016 wxArrayInt
wxGrid::GetSelectedRows() const
11024 return m_selection
->m_rowSelection
;
11027 wxArrayInt
wxGrid::GetSelectedCols() const
11035 return m_selection
->m_colSelection
;
11038 void wxGrid::ClearSelection()
11040 wxRect r1
= BlockToDeviceRect( m_selectingTopLeft
, m_selectingBottomRight
);
11041 wxRect r2
= BlockToDeviceRect( m_currentCellCoords
, m_selectingKeyboard
);
11042 m_selectingTopLeft
=
11043 m_selectingBottomRight
=
11044 m_selectingKeyboard
= wxGridNoCellCoords
;
11045 Refresh( false, &r1
);
11046 Refresh( false, &r2
);
11048 m_selection
->ClearSelection();
11051 // This function returns the rectangle that encloses the given block
11052 // in device coords clipped to the client size of the grid window.
11054 wxRect
wxGrid::BlockToDeviceRect( const wxGridCellCoords
& topLeft
,
11055 const wxGridCellCoords
& bottomRight
) const
11058 wxRect tempCellRect
= CellToRect(topLeft
);
11059 if ( tempCellRect
!= wxGridNoCellRect
)
11061 resultRect
= tempCellRect
;
11065 resultRect
= wxRect(0, 0, 0, 0);
11068 tempCellRect
= CellToRect(bottomRight
);
11069 if ( tempCellRect
!= wxGridNoCellRect
)
11071 resultRect
+= tempCellRect
;
11075 // If both inputs were "wxGridNoCellRect," then there's nothing to do.
11076 return wxGridNoCellRect
;
11079 // Ensure that left/right and top/bottom pairs are in order.
11080 int left
= resultRect
.GetLeft();
11081 int top
= resultRect
.GetTop();
11082 int right
= resultRect
.GetRight();
11083 int bottom
= resultRect
.GetBottom();
11085 int leftCol
= topLeft
.GetCol();
11086 int topRow
= topLeft
.GetRow();
11087 int rightCol
= bottomRight
.GetCol();
11088 int bottomRow
= bottomRight
.GetRow();
11097 leftCol
= rightCol
;
11108 topRow
= bottomRow
;
11112 // The following loop is ONLY necessary to detect and handle merged cells.
11114 m_gridWin
->GetClientSize( &cw
, &ch
);
11116 // Get the origin coordinates: notice that they will be negative if the
11117 // grid is scrolled downwards/to the right.
11118 int gridOriginX
= 0;
11119 int gridOriginY
= 0;
11120 CalcScrolledPosition(gridOriginX
, gridOriginY
, &gridOriginX
, &gridOriginY
);
11122 int onScreenLeftmostCol
= internalXToCol(-gridOriginX
);
11123 int onScreenUppermostRow
= internalYToRow(-gridOriginY
);
11125 int onScreenRightmostCol
= internalXToCol(-gridOriginX
+ cw
);
11126 int onScreenBottommostRow
= internalYToRow(-gridOriginY
+ ch
);
11128 // Bound our loop so that we only examine the portion of the selected block
11129 // that is shown on screen. Therefore, we compare the Top-Left block values
11130 // to the Top-Left screen values, and the Bottom-Right block values to the
11131 // Bottom-Right screen values, choosing appropriately.
11132 const int visibleTopRow
= wxMax(topRow
, onScreenUppermostRow
);
11133 const int visibleBottomRow
= wxMin(bottomRow
, onScreenBottommostRow
);
11134 const int visibleLeftCol
= wxMax(leftCol
, onScreenLeftmostCol
);
11135 const int visibleRightCol
= wxMin(rightCol
, onScreenRightmostCol
);
11137 for ( int j
= visibleTopRow
; j
<= visibleBottomRow
; j
++ )
11139 for ( int i
= visibleLeftCol
; i
<= visibleRightCol
; i
++ )
11141 if ( (j
== visibleTopRow
) || (j
== visibleBottomRow
) ||
11142 (i
== visibleLeftCol
) || (i
== visibleRightCol
) )
11144 tempCellRect
= CellToRect( j
, i
);
11146 if (tempCellRect
.x
< left
)
11147 left
= tempCellRect
.x
;
11148 if (tempCellRect
.y
< top
)
11149 top
= tempCellRect
.y
;
11150 if (tempCellRect
.x
+ tempCellRect
.width
> right
)
11151 right
= tempCellRect
.x
+ tempCellRect
.width
;
11152 if (tempCellRect
.y
+ tempCellRect
.height
> bottom
)
11153 bottom
= tempCellRect
.y
+ tempCellRect
.height
;
11157 i
= visibleRightCol
; // jump over inner cells.
11162 // Convert to scrolled coords
11163 CalcScrolledPosition( left
, top
, &left
, &top
);
11164 CalcScrolledPosition( right
, bottom
, &right
, &bottom
);
11166 if (right
< 0 || bottom
< 0 || left
> cw
|| top
> ch
)
11167 return wxRect(0,0,0,0);
11169 resultRect
.SetLeft( wxMax(0, left
) );
11170 resultRect
.SetTop( wxMax(0, top
) );
11171 resultRect
.SetRight( wxMin(cw
, right
) );
11172 resultRect
.SetBottom( wxMin(ch
, bottom
) );
11177 // ----------------------------------------------------------------------------
11179 // ----------------------------------------------------------------------------
11181 #if wxUSE_DRAG_AND_DROP
11183 // this allow setting drop target directly on wxGrid
11184 void wxGrid::SetDropTarget(wxDropTarget
*dropTarget
)
11186 GetGridWindow()->SetDropTarget(dropTarget
);
11189 #endif // wxUSE_DRAG_AND_DROP
11191 // ----------------------------------------------------------------------------
11192 // grid event classes
11193 // ----------------------------------------------------------------------------
11195 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent
, wxNotifyEvent
)
11197 wxGridEvent::wxGridEvent( int id
, wxEventType type
, wxObject
* obj
,
11198 int row
, int col
, int x
, int y
, bool sel
,
11199 bool control
, bool shift
, bool alt
, bool meta
)
11200 : wxNotifyEvent( type
, id
)
11207 m_control
= control
;
11212 SetEventObject(obj
);
11216 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent
, wxNotifyEvent
)
11218 wxGridSizeEvent::wxGridSizeEvent( int id
, wxEventType type
, wxObject
* obj
,
11219 int rowOrCol
, int x
, int y
,
11220 bool control
, bool shift
, bool alt
, bool meta
)
11221 : wxNotifyEvent( type
, id
)
11223 m_rowOrCol
= rowOrCol
;
11226 m_control
= control
;
11231 SetEventObject(obj
);
11235 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent
, wxNotifyEvent
)
11237 wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id
, wxEventType type
, wxObject
* obj
,
11238 const wxGridCellCoords
& topLeft
,
11239 const wxGridCellCoords
& bottomRight
,
11240 bool sel
, bool control
,
11241 bool shift
, bool alt
, bool meta
)
11242 : wxNotifyEvent( type
, id
)
11244 m_topLeft
= topLeft
;
11245 m_bottomRight
= bottomRight
;
11247 m_control
= control
;
11252 SetEventObject(obj
);
11256 IMPLEMENT_DYNAMIC_CLASS(wxGridEditorCreatedEvent
, wxCommandEvent
)
11258 wxGridEditorCreatedEvent::wxGridEditorCreatedEvent(int id
, wxEventType type
,
11259 wxObject
* obj
, int row
,
11260 int col
, wxControl
* ctrl
)
11261 : wxCommandEvent(type
, id
)
11263 SetEventObject(obj
);
11269 #endif // wxUSE_GRID