1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxGrid and related classes
4 // Author: Michael Bedward (based on code by Julian Smart, Robin Dunn)
8 // Copyright: (c) Michael Bedward
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
15 #if !defined(wxUSE_NEW_GRID) || !(wxUSE_NEW_GRID)
23 #pragma interface "grid.h"
27 #include "wx/scrolwin.h"
28 #include "wx/string.h"
29 #include "wx/scrolbar.h"
31 #include "wx/textctrl.h"
32 #include "wx/combobox.h"
33 #include "wx/dynarray.h"
36 // Default parameters for wxGrid
38 #define WXGRID_DEFAULT_NUMBER_ROWS 10
39 #define WXGRID_DEFAULT_NUMBER_COLS 10
41 #define WXGRID_DEFAULT_ROW_HEIGHT 25
43 #define WXGRID_DEFAULT_ROW_HEIGHT 30
45 #define WXGRID_DEFAULT_COL_WIDTH 80
46 #define WXGRID_DEFAULT_COL_LABEL_HEIGHT 32
47 #define WXGRID_DEFAULT_ROW_LABEL_WIDTH 82
48 #define WXGRID_LABEL_EDGE_ZONE 5
49 #define WXGRID_MIN_ROW_HEIGHT 15
50 #define WXGRID_MIN_COL_WIDTH 15
51 #define WXGRID_DEFAULT_SCROLLBAR_WIDTH 16
53 // ----------------------------------------------------------------------------
54 // forward declarations
55 // ----------------------------------------------------------------------------
57 class WXDLLEXPORT wxGrid
;
58 class WXDLLEXPORT wxGridCellAttr
;
59 class WXDLLEXPORT wxGridCellAttrProviderData
;
60 class WXDLLEXPORT wxGridColLabelWindow
;
61 class WXDLLEXPORT wxGridCornerLabelWindow
;
62 class WXDLLEXPORT wxGridRowLabelWindow
;
63 class WXDLLEXPORT wxGridTableBase
;
64 class WXDLLEXPORT wxGridWindow
;
66 // ----------------------------------------------------------------------------
67 // wxGridCellRenderer: this class is responsible for actually drawing the cell
68 // in the grid. You may pass it to the wxGridCellAttr (below) to change the
69 // format of one given cell or to wxGrid::SetDefaultRenderer() to change the
70 // view of all cells. This is an ABC, you will normally use one of the
71 // predefined derived classes or derive oyur own class from it.
72 // ----------------------------------------------------------------------------
74 class WXDLLEXPORT wxGridCellRenderer
77 // draw the given cell on the provided DC inside the given rectangle
78 // using the style specified by the attribute and the default or selected
79 // state corresponding to the isSelected value.
81 // this pure virtual function has a default implementation which will
82 // prepare the DC using the given attribute: it will draw the rectangle
83 // with the bg colour from attr and set the text colour and font
84 virtual void Draw(wxGrid
& grid
,
91 // the default renderer for the cells containing string data
92 class WXDLLEXPORT wxGridCellStringRenderer
: public wxGridCellRenderer
96 virtual void Draw(wxGrid
& grid
,
103 // ----------------------------------------------------------------------------
104 // wxGridCellAttr: this class can be used to alter the cells appearance in
105 // the grid by changing their colour/font/... from default. An object of this
106 // class may be returned by wxGridTable::GetAttr().
107 // ----------------------------------------------------------------------------
109 class WXDLLEXPORT wxGridCellAttr
119 wxGridCellAttr(const wxColour
& colText
,
120 const wxColour
& colBack
,
124 : m_colText(colText
), m_colBack(colBack
), m_font(font
)
127 SetAlignment(hAlign
, vAlign
);
130 // default copy ctor ok
132 // this class is ref counted: it is created with ref count of 1, so
133 // calling DecRef() once will delete it. Calling IncRef() allows to lock
134 // it until the matching DecRef() is called
135 void IncRef() { m_nRef
++; }
136 void DecRef() { if ( !--m_nRef
) delete this; }
137 void SafeDecRef() { if ( this ) DecRef(); }
140 void SetTextColour(const wxColour
& colText
) { m_colText
= colText
; }
141 void SetBackgroundColour(const wxColour
& colBack
) { m_colBack
= colBack
; }
142 void SetFont(const wxFont
& font
) { m_font
= font
; }
143 void SetAlignment(int hAlign
, int vAlign
)
149 // takes ownership of the pointer
150 void SetRenderer(wxGridCellRenderer
*renderer
)
151 { delete m_renderer
; m_renderer
= renderer
; }
154 bool HasTextColour() const { return m_colText
.Ok(); }
155 bool HasBackgroundColour() const { return m_colBack
.Ok(); }
156 bool HasFont() const { return m_font
.Ok(); }
157 bool HasAlignment() const { return m_hAlign
|| m_vAlign
; }
159 const wxColour
& GetTextColour() const { return m_colText
; }
160 const wxColour
& GetBackgroundColour() const { return m_colBack
; }
161 const wxFont
& GetFont() const { return m_font
; }
162 void GetAlignment(int *hAlign
, int *vAlign
)
164 if ( hAlign
) *hAlign
= m_hAlign
;
165 if ( vAlign
) *vAlign
= m_vAlign
;
168 wxGridCellRenderer
*GetRenderer() const { return m_renderer
; }
171 // the common part of all ctors
172 void Init() { m_nRef
= 1; m_renderer
= (wxGridCellRenderer
*)NULL
; }
174 // the dtor is private because only DecRef() can delete us
175 ~wxGridCellAttr() { delete m_renderer
; }
177 // the ref count - when it goes to 0, we die
186 wxGridCellRenderer
*m_renderer
;
188 // suppress the stupid gcc warning about the class having private dtor and
190 friend class wxGridCellAttrDummyFriend
;
193 // ----------------------------------------------------------------------------
194 // wxGridCellAttrProvider: class used by wxGridTableBase to retrieve/store the
196 // ----------------------------------------------------------------------------
198 // implementation note: we separate it from wxGridTableBase because we wish to
199 // avoid deriving a new table class if possible, and sometimes it will be
200 // enough to just derive another wxGridCellAttrProvider instead
202 class WXDLLEXPORT wxGridCellAttrProvider
205 wxGridCellAttrProvider();
206 virtual ~wxGridCellAttrProvider();
208 // DecRef() must be called on the returned pointer
209 virtual wxGridCellAttr
*GetAttr(int row
, int col
) const;
211 // takes ownership of the pointer, don't call DecRef() on it
212 virtual void SetAttr(wxGridCellAttr
*attr
, int row
, int col
);
217 wxGridCellAttrProviderData
*m_data
;
220 //////////////////////////////////////////////////////////////////////
222 // Grid table classes
224 //////////////////////////////////////////////////////////////////////
227 class WXDLLEXPORT wxGridTableBase
: public wxObject
231 virtual ~wxGridTableBase();
233 // You must override these functions in a derived table class
235 virtual long GetNumberRows() = 0;
236 virtual long GetNumberCols() = 0;
237 virtual wxString
GetValue( int row
, int col
) = 0;
238 virtual void SetValue( int row
, int col
, const wxString
& s
) = 0;
239 virtual bool IsEmptyCell( int row
, int col
) = 0;
241 // Overriding these is optional
243 virtual void SetView( wxGrid
*grid
) { m_view
= grid
; }
244 virtual wxGrid
* GetView() const { return m_view
; }
246 virtual void Clear() {}
247 virtual bool InsertRows( size_t pos
= 0, size_t numRows
= 1 );
248 virtual bool AppendRows( size_t numRows
= 1 );
249 virtual bool DeleteRows( size_t pos
= 0, size_t numRows
= 1 );
250 virtual bool InsertCols( size_t pos
= 0, size_t numCols
= 1 );
251 virtual bool AppendCols( size_t numCols
= 1 );
252 virtual bool DeleteCols( size_t pos
= 0, size_t numCols
= 1 );
254 virtual wxString
GetRowLabelValue( int row
);
255 virtual wxString
GetColLabelValue( int col
);
256 virtual void SetRowLabelValue( int WXUNUSED(row
), const wxString
& ) {}
257 virtual void SetColLabelValue( int WXUNUSED(col
), const wxString
& ) {}
259 // Attribute handling
262 // give us the attr provider to use - we take ownership of the pointer
263 void SetAttrProvider(wxGridCellAttrProvider
*attrProvider
);
265 // get the currently used attr provider (may be NULL)
266 wxGridCellAttrProvider
*GetAttrProvider() const { return m_attrProvider
; }
268 // by default forwarded to wxGridCellAttrProvider if any. May be
269 // overridden to handle attributes directly in this class.
270 virtual wxGridCellAttr
*GetAttr( int row
, int col
);
272 // takes ownership of the pointer
273 virtual void SetAttr(wxGridCellAttr
*attr
, int row
, int col
);
277 wxGridCellAttrProvider
*m_attrProvider
;
279 DECLARE_ABSTRACT_CLASS( wxGridTableBase
);
283 // ----------------------------------------------------------------------------
284 // wxGridTableMessage
285 // ----------------------------------------------------------------------------
287 // IDs for messages sent from grid table to view
289 enum wxGridTableRequest
291 wxGRIDTABLE_REQUEST_VIEW_GET_VALUES
= 2000,
292 wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES
,
293 wxGRIDTABLE_NOTIFY_ROWS_INSERTED
,
294 wxGRIDTABLE_NOTIFY_ROWS_APPENDED
,
295 wxGRIDTABLE_NOTIFY_ROWS_DELETED
,
296 wxGRIDTABLE_NOTIFY_COLS_INSERTED
,
297 wxGRIDTABLE_NOTIFY_COLS_APPENDED
,
298 wxGRIDTABLE_NOTIFY_COLS_DELETED
301 class WXDLLEXPORT wxGridTableMessage
304 wxGridTableMessage();
305 wxGridTableMessage( wxGridTableBase
*table
, int id
,
309 void SetTableObject( wxGridTableBase
*table
) { m_table
= table
; }
310 wxGridTableBase
* GetTableObject() const { return m_table
; }
311 void SetId( int id
) { m_id
= id
; }
312 int GetId() { return m_id
; }
313 void SetCommandInt( int comInt1
) { m_comInt1
= comInt1
; }
314 int GetCommandInt() { return m_comInt1
; }
315 void SetCommandInt2( int comInt2
) { m_comInt2
= comInt2
; }
316 int GetCommandInt2() { return m_comInt2
; }
319 wxGridTableBase
*m_table
;
327 // ------ wxGridStringArray
328 // A 2-dimensional array of strings for data values
331 WX_DECLARE_EXPORTED_OBJARRAY(wxArrayString
, wxGridStringArray
);
335 // ------ wxGridStringTable
337 // Simplest type of data table for a grid for small tables of strings
338 // that are stored in memory
341 class WXDLLEXPORT wxGridStringTable
: public wxGridTableBase
345 wxGridStringTable( int numRows
, int numCols
);
346 ~wxGridStringTable();
348 // these are pure virtual in wxGridTableBase
350 long GetNumberRows();
351 long GetNumberCols();
352 wxString
GetValue( int row
, int col
);
353 void SetValue( int row
, int col
, const wxString
& s
);
354 bool IsEmptyCell( int row
, int col
);
356 // overridden functions from wxGridTableBase
359 bool InsertRows( size_t pos
= 0, size_t numRows
= 1 );
360 bool AppendRows( size_t numRows
= 1 );
361 bool DeleteRows( size_t pos
= 0, size_t numRows
= 1 );
362 bool InsertCols( size_t pos
= 0, size_t numCols
= 1 );
363 bool AppendCols( size_t numCols
= 1 );
364 bool DeleteCols( size_t pos
= 0, size_t numCols
= 1 );
366 void SetRowLabelValue( int row
, const wxString
& );
367 void SetColLabelValue( int col
, const wxString
& );
368 wxString
GetRowLabelValue( int row
);
369 wxString
GetColLabelValue( int col
);
372 wxGridStringArray m_data
;
374 // These only get used if you set your own labels, otherwise the
375 // GetRow/ColLabelValue functions return wxGridTableBase defaults
377 wxArrayString m_rowLabels
;
378 wxArrayString m_colLabels
;
380 DECLARE_DYNAMIC_CLASS( wxGridStringTable
)
385 //////////////////////////////////////////////////////////////////////
389 //////////////////////////////////////////////////////////////////////
391 class WXDLLEXPORT wxGridCellCoords
394 wxGridCellCoords() { m_row
= m_col
= -1; }
395 wxGridCellCoords( int r
, int c
) { m_row
= r
; m_col
= c
; }
397 // default copy ctor is ok
399 long GetRow() const { return m_row
; }
400 void SetRow( long n
) { m_row
= n
; }
401 long GetCol() const { return m_col
; }
402 void SetCol( long n
) { m_col
= n
; }
403 void Set( long row
, long col
) { m_row
= row
; m_col
= col
; }
405 wxGridCellCoords
& operator=( const wxGridCellCoords
& other
)
407 if ( &other
!= this )
415 bool operator==( const wxGridCellCoords
& other
)
417 return (m_row
== other
.m_row
&& m_col
== other
.m_col
);
420 bool operator!=( const wxGridCellCoords
& other
)
422 return (m_row
!= other
.m_row
|| m_col
!= other
.m_col
);
427 return (m_row
== -1 && m_col
== -1 );
436 // For comparisons...
438 extern wxGridCellCoords wxGridNoCellCoords
;
439 extern wxRect wxGridNoCellRect
;
441 // An array of cell coords...
443 WX_DECLARE_EXPORTED_OBJARRAY(wxGridCellCoords
, wxGridCellCoordsArray
);
447 // This set of classes is to provide for the use of different types of
448 // cell edit controls in the grid while avoiding the wx class info
449 // system in deference to wxPython
451 class WXDLLEXPORT wxGridTextCtrl
: public wxTextCtrl
455 wxGridTextCtrl( wxWindow
*,
459 const wxString
& value
= wxEmptyString
,
460 const wxPoint
& pos
= wxDefaultPosition
,
461 const wxSize
& size
= wxDefaultSize
,
464 void SetStartValue( const wxString
& );
465 wxString
GetStartValue() { return startValue
; }
470 // TRUE for controls placed over cells,
471 // FALSE for a control on a grid control panel
472 bool m_isCellControl
;
476 void OnKeyDown( wxKeyEvent
& );
478 DECLARE_DYNAMIC_CLASS( wxGridTextCtrl
)
479 DECLARE_EVENT_TABLE()
482 // ----------------------------------------------------------------------------
484 // ----------------------------------------------------------------------------
486 class WXDLLEXPORT wxGrid
: public wxScrolledWindow
491 m_table
= (wxGridTableBase
*) NULL
;
492 m_gridWin
= (wxGridWindow
*) NULL
;
493 m_rowLabelWin
= (wxGridRowLabelWindow
*) NULL
;
494 m_colLabelWin
= (wxGridColLabelWindow
*) NULL
;
495 m_cornerLabelWin
= (wxGridCornerLabelWindow
*) NULL
;
496 m_cellEditCtrl
= (wxWindow
*) NULL
;
499 wxGrid( wxWindow
*parent
,
501 const wxPoint
& pos
= wxDefaultPosition
,
502 const wxSize
& size
= wxDefaultSize
,
504 const wxString
& name
= wxPanelNameStr
);
508 bool CreateGrid( int numRows
, int numCols
);
511 // ------ grid dimensions
513 int GetNumberRows() { return m_numRows
; }
514 int GetNumberCols() { return m_numCols
; }
517 // ------ display update functions
519 void CalcRowLabelsExposed( wxRegion
& reg
);
521 void CalcColLabelsExposed( wxRegion
& reg
);
522 void CalcCellsExposed( wxRegion
& reg
);
525 // ------ event handlers
527 void ProcessRowLabelMouseEvent( wxMouseEvent
& event
);
528 void ProcessColLabelMouseEvent( wxMouseEvent
& event
);
529 void ProcessCornerLabelMouseEvent( wxMouseEvent
& event
);
530 void ProcessGridCellMouseEvent( wxMouseEvent
& event
);
531 bool ProcessTableMessage( wxGridTableMessage
& );
533 void DoEndDragResizeRow();
534 void DoEndDragResizeCol();
536 wxGridTableBase
* GetTable() const { return m_table
; }
537 void SetTable( wxGridTableBase
*table
) { m_table
= table
; }
540 bool InsertRows( int pos
= 0, int numRows
= 1, bool updateLabels
=TRUE
);
541 bool AppendRows( int numRows
= 1, bool updateLabels
=TRUE
);
542 bool DeleteRows( int pos
= 0, int numRows
= 1, bool updateLabels
=TRUE
);
543 bool InsertCols( int pos
= 0, int numCols
= 1, bool updateLabels
=TRUE
);
544 bool AppendCols( int numCols
= 1, bool updateLabels
=TRUE
);
545 bool DeleteCols( int pos
= 0, int numCols
= 1, bool updateLabels
=TRUE
);
547 void DrawGridCellArea( wxDC
& dc
);
548 void DrawCellBorder( wxDC
& dc
, const wxGridCellCoords
& );
549 void DrawAllGridLines( wxDC
& dc
, const wxRegion
& reg
);
550 void DrawCell( wxDC
& dc
, const wxGridCellCoords
& );
552 void DrawRowLabels( wxDC
& dc
);
553 void DrawRowLabel( wxDC
& dc
, int row
);
555 void DrawColLabels( wxDC
& dc
);
556 void DrawColLabel( wxDC
& dc
, int col
);
559 // ------ Cell text drawing functions
561 void DrawTextRectangle( wxDC
& dc
, const wxString
&, const wxRect
&,
562 int horizontalAlignment
= wxLEFT
,
563 int verticalAlignment
= wxTOP
);
565 // Split a string containing newline chararcters into an array of
566 // strings and return the number of lines
568 void StringToLines( const wxString
& value
, wxArrayString
& lines
);
570 void GetTextBoxSize( wxDC
& dc
,
571 wxArrayString
& lines
,
572 long *width
, long *height
);
576 // Code that does a lot of grid modification can be enclosed
577 // between BeginBatch() and EndBatch() calls to avoid screen
580 void BeginBatch() { m_batchCount
++; }
581 void EndBatch() { if ( m_batchCount
> 0 ) m_batchCount
--; }
582 int GetBatchCount() { return m_batchCount
; }
585 // ------ edit control functions
587 bool IsEditable() { return m_editable
; }
588 void EnableEditing( bool edit
);
590 #if 0 // at the moment the cell edit control is always active
591 void EnableCellEditControl( bool enable
);
594 bool IsCellEditControlEnabled()
595 { return (m_cellEditCtrl
&& m_cellEditCtrlEnabled
); }
597 void ShowCellEditControl();
598 void HideCellEditControl();
599 void SetEditControlValue( const wxString
& s
= wxEmptyString
);
600 void SaveEditControlValue();
603 // ------ grid location functions
604 // Note that all of these functions work with the logical coordinates of
605 // grid cells and labels so you will need to convert from device
606 // coordinates for mouse events etc.
608 void XYToCell( int x
, int y
, wxGridCellCoords
& );
612 int YToEdgeOfRow( int y
);
613 int XToEdgeOfCol( int x
);
615 wxRect
CellToRect( int row
, int col
);
616 wxRect
CellToRect( const wxGridCellCoords
& coords
)
617 { return CellToRect( coords
.GetRow(), coords
.GetCol() ); }
619 int GetGridCursorRow() { return m_currentCellCoords
.GetRow(); }
620 int GetGridCursorCol() { return m_currentCellCoords
.GetCol(); }
622 // check to see if a cell is either wholly visible (the default arg) or
623 // at least partially visible in the grid window
625 bool IsVisible( int row
, int col
, bool wholeCellVisible
= TRUE
);
626 bool IsVisible( const wxGridCellCoords
& coords
, bool wholeCellVisible
= TRUE
)
627 { return IsVisible( coords
.GetRow(), coords
.GetCol(), wholeCellVisible
); }
628 void MakeCellVisible( int row
, int col
);
629 void MakeCellVisible( const wxGridCellCoords
& coords
)
630 { MakeCellVisible( coords
.GetRow(), coords
.GetCol() ); }
633 // ------ grid cursor movement functions
635 void SetGridCursor( int row
, int col
)
636 { SetCurrentCell( wxGridCellCoords(row
, col
) ); }
639 bool MoveCursorDown();
640 bool MoveCursorLeft();
641 bool MoveCursorRight();
644 bool MoveCursorUpBlock();
645 bool MoveCursorDownBlock();
646 bool MoveCursorLeftBlock();
647 bool MoveCursorRightBlock();
650 // ------ label and gridline formatting
652 int GetDefaultRowLabelSize() { return WXGRID_DEFAULT_ROW_LABEL_WIDTH
; }
653 int GetRowLabelSize() { return m_rowLabelWidth
; }
654 int GetDefaultColLabelSize() { return WXGRID_DEFAULT_COL_LABEL_HEIGHT
; }
655 int GetColLabelSize() { return m_colLabelHeight
; }
656 wxColour
GetLabelBackgroundColour() { return m_labelBackgroundColour
; }
657 wxColour
GetLabelTextColour() { return m_labelTextColour
; }
658 wxFont
GetLabelFont() { return m_labelFont
; }
659 void GetRowLabelAlignment( int *horiz
, int *vert
);
660 void GetColLabelAlignment( int *horiz
, int *vert
);
661 wxString
GetRowLabelValue( int row
);
662 wxString
GetColLabelValue( int col
);
663 wxColour
GetGridLineColour() { return m_gridLineColour
; }
665 void SetRowLabelSize( int width
);
666 void SetColLabelSize( int height
);
667 void SetLabelBackgroundColour( const wxColour
& );
668 void SetLabelTextColour( const wxColour
& );
669 void SetLabelFont( const wxFont
& );
670 void SetRowLabelAlignment( int horiz
, int vert
);
671 void SetColLabelAlignment( int horiz
, int vert
);
672 void SetRowLabelValue( int row
, const wxString
& );
673 void SetColLabelValue( int col
, const wxString
& );
674 void SetGridLineColour( const wxColour
& );
676 void EnableGridLines( bool enable
= TRUE
);
677 bool GridLinesEnabled() { return m_gridLinesEnabled
; }
679 // ------ row and col formatting
681 int GetDefaultRowSize();
682 int GetRowSize( int row
);
683 int GetDefaultColSize();
684 int GetColSize( int col
);
685 wxColour
GetDefaultCellBackgroundColour();
686 wxColour
GetCellBackgroundColour( int row
, int col
);
687 wxColour
GetDefaultCellTextColour();
688 wxColour
GetCellTextColour( int row
, int col
);
689 wxFont
GetDefaultCellFont();
690 wxFont
GetCellFont( int row
, int col
);
691 void GetDefaultCellAlignment( int *horiz
, int *vert
);
692 void GetCellAlignment( int row
, int col
, int *horiz
, int *vert
);
694 void SetDefaultRowSize( int height
, bool resizeExistingRows
= FALSE
);
695 void SetRowSize( int row
, int height
);
696 void SetDefaultColSize( int width
, bool resizeExistingCols
= FALSE
);
698 void SetColSize( int col
, int width
);
699 void SetDefaultCellBackgroundColour( const wxColour
& );
700 void SetCellBackgroundColour( int row
, int col
, const wxColour
& );
701 void SetDefaultCellTextColour( const wxColour
& );
703 void SetCellTextColour( int row
, int col
, const wxColour
& );
704 void SetDefaultCellFont( const wxFont
& );
705 void SetCellFont( int row
, int col
, const wxFont
& );
706 void SetDefaultCellAlignment( int horiz
, int vert
);
707 void SetCellAlignment( int row
, int col
, int horiz
, int vert
);
709 // takes ownership of the pointer
710 void SetDefaultRenderer(wxGridCellRenderer
*renderer
)
711 { delete m_defaultRenderer
; m_defaultRenderer
= renderer
; }
712 wxGridCellRenderer
*GetDefaultRenderer() const
713 { return m_defaultRenderer
; }
715 void SetCellRenderer(int row
, int col
, wxGridCellRenderer
*renderer
);
717 // ------ cell value accessors
719 wxString
GetCellValue( int row
, int col
)
723 return m_table
->GetValue( row
, col
);
727 return wxEmptyString
;
731 wxString
GetCellValue( const wxGridCellCoords
& coords
)
732 { return GetCellValue( coords
.GetRow(), coords
.GetCol() ); }
734 void SetCellValue( int row
, int col
, const wxString
& s
);
735 void SetCellValue( const wxGridCellCoords
& coords
, const wxString
& s
)
736 { SetCellValue( coords
.GetRow(), coords
.GetCol(), s
); }
740 // ------ selections of blocks of cells
742 void SelectRow( int row
, bool addToSelected
= FALSE
);
743 void SelectCol( int col
, bool addToSelected
= FALSE
);
745 void SelectBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
);
747 void SelectBlock( const wxGridCellCoords
& topLeft
,
748 const wxGridCellCoords
& bottomRight
)
749 { SelectBlock( topLeft
.GetRow(), topLeft
.GetCol(),
750 bottomRight
.GetRow(), bottomRight
.GetCol() ); }
755 { return ( m_selectedTopLeft
!= wxGridNoCellCoords
&&
756 m_selectedBottomRight
!= wxGridNoCellCoords
);
759 void ClearSelection();
761 bool IsInSelection( int row
, int col
)
762 { return ( IsSelection() &&
763 row
>= m_selectedTopLeft
.GetRow() &&
764 col
>= m_selectedTopLeft
.GetCol() &&
765 row
<= m_selectedBottomRight
.GetRow() &&
766 col
<= m_selectedBottomRight
.GetCol() );
769 bool IsInSelection( const wxGridCellCoords
& coords
)
770 { return IsInSelection( coords
.GetRow(), coords
.GetCol() ); }
772 void GetSelection( int* topRow
, int* leftCol
, int* bottomRow
, int* rightCol
)
774 // these will all be -1 if there is no selected block
776 *topRow
= m_selectedTopLeft
.GetRow();
777 *leftCol
= m_selectedTopLeft
.GetCol();
778 *bottomRow
= m_selectedBottomRight
.GetRow();
779 *rightCol
= m_selectedBottomRight
.GetCol();
783 // This function returns the rectangle that encloses the block of cells
784 // limited by TopLeft and BottomRight cell in device coords and clipped
785 // to the client size of the grid window.
787 wxRect
BlockToDeviceRect( const wxGridCellCoords
& topLeft
,
788 const wxGridCellCoords
& bottomRight
);
790 // This function returns the rectangle that encloses the selected cells
791 // in device coords and clipped to the client size of the grid window.
793 wxRect
SelectionToDeviceRect()
795 return BlockToDeviceRect( m_selectedTopLeft
,
796 m_selectedBottomRight
);
800 // ------ For compatibility with previous wxGrid only...
802 // ************************************************
803 // ** Don't use these in new code because they **
804 // ** are liable to disappear in a future **
806 // ************************************************
809 wxGrid( wxWindow
*parent
,
810 int x
= -1, int y
= -1, int w
= -1, int h
= -1,
812 const wxString
& name
= wxPanelNameStr
)
813 : wxScrolledWindow( parent
, -1, wxPoint(x
,y
), wxSize(w
,h
), style
, name
)
818 void SetCellValue( const wxString
& val
, int row
, int col
)
819 { SetCellValue( row
, col
, val
); }
821 void UpdateDimensions()
822 { CalcDimensions(); }
824 int GetRows() { return GetNumberRows(); }
825 int GetCols() { return GetNumberCols(); }
826 int GetCursorRow() { return GetGridCursorRow(); }
827 int GetCursorColumn() { return GetGridCursorCol(); }
829 int GetScrollPosX() { return 0; }
830 int GetScrollPosY() { return 0; }
832 void SetScrollX( int x
) { }
833 void SetScrollY( int y
) { }
835 void SetColumnWidth( int col
, int width
)
836 { SetColSize( col
, width
); }
838 int GetColumnWidth( int col
)
839 { return GetColSize( col
); }
841 void SetRowHeight( int row
, int height
)
842 { SetRowSize( row
, height
); }
844 int GetRowHeight( int row
)
845 { return GetRowSize( row
); }
847 int GetViewHeight() // returned num whole rows visible
850 int GetViewWidth() // returned num whole cols visible
853 void SetLabelSize( int orientation
, int sz
)
855 if ( orientation
== wxHORIZONTAL
)
856 SetColLabelSize( sz
);
858 SetRowLabelSize( sz
);
861 int GetLabelSize( int orientation
)
863 if ( orientation
== wxHORIZONTAL
)
864 return GetColLabelSize();
866 return GetRowLabelSize();
869 void SetLabelAlignment( int orientation
, int align
)
871 if ( orientation
== wxHORIZONTAL
)
872 SetColLabelAlignment( align
, -1 );
874 SetRowLabelAlignment( align
, -1 );
877 int GetLabelAlignment( int orientation
, int WXUNUSED(align
) )
880 if ( orientation
== wxHORIZONTAL
)
882 GetColLabelAlignment( &h
, &v
);
887 GetRowLabelAlignment( &h
, &v
);
892 void SetLabelValue( int orientation
, const wxString
& val
, int pos
)
894 if ( orientation
== wxHORIZONTAL
)
895 SetColLabelValue( pos
, val
);
897 SetRowLabelValue( pos
, val
);
900 wxString
GetLabelValue( int orientation
, int pos
)
902 if ( orientation
== wxHORIZONTAL
)
903 return GetColLabelValue( pos
);
905 return GetRowLabelValue( pos
);
908 wxFont
GetCellTextFont() const
909 { return m_defaultCellFont
; }
911 wxFont
GetCellTextFont(int WXUNUSED(row
), int WXUNUSED(col
)) const
912 { return m_defaultCellFont
; }
914 void SetCellTextFont(const wxFont
& fnt
)
915 { SetDefaultCellFont( fnt
); }
917 void SetCellTextFont(const wxFont
& fnt
, int row
, int col
)
918 { SetCellFont( row
, col
, fnt
); }
920 void SetCellTextColour(const wxColour
& val
, int row
, int col
)
921 { SetCellTextColour( row
, col
, val
); }
923 void SetCellTextColour(const wxColour
& col
)
924 { SetDefaultCellTextColour( col
); }
926 void SetCellBackgroundColour(const wxColour
& col
)
927 { SetDefaultCellBackgroundColour( col
); }
929 void SetCellBackgroundColour(const wxColour
& colour
, int row
, int col
)
930 { SetCellBackgroundColour( row
, col
, colour
); }
932 bool GetEditable() { return IsEditable(); }
933 void SetEditable( bool edit
= TRUE
) { EnableEditing( edit
); }
934 bool GetEditInPlace() { return IsCellEditControlEnabled(); }
936 void SetEditInPlace(bool edit
= TRUE
) { }
938 void SetCellAlignment( int align
, int row
, int col
)
939 { SetCellAlignment(row
, col
, align
, wxCENTER
); }
940 void SetCellAlignment( int WXUNUSED(align
) ) {}
941 void SetCellBitmap(wxBitmap
*WXUNUSED(bitmap
), int WXUNUSED(row
), int WXUNUSED(col
))
943 void SetDividerPen(const wxPen
& WXUNUSED(pen
)) { }
944 wxPen
& GetDividerPen() const { return wxNullPen
; }
945 void OnActivate(bool WXUNUSED(active
)) {}
947 // ******** End of compatibility functions **********
951 // ------ control IDs
952 enum { wxGRID_CELLCTRL
= 2000,
955 // ------ control types
956 enum { wxGRID_TEXTCTRL
= 2100,
965 wxGridWindow
*m_gridWin
;
966 wxGridRowLabelWindow
*m_rowLabelWin
;
967 wxGridColLabelWindow
*m_colLabelWin
;
968 wxGridCornerLabelWindow
*m_cornerLabelWin
;
970 wxGridTableBase
*m_table
;
980 wxGridCellCoords m_currentCellCoords
;
982 wxGridCellCoords m_selectedTopLeft
;
983 wxGridCellCoords m_selectedBottomRight
;
985 int m_defaultRowHeight
;
986 wxArrayInt m_rowHeights
;
987 wxArrayInt m_rowBottoms
;
989 int m_defaultColWidth
;
990 wxArrayInt m_colWidths
;
991 wxArrayInt m_colRights
;
994 int m_colLabelHeight
;
996 wxColour m_labelBackgroundColour
;
997 wxColour m_labelTextColour
;
1000 int m_rowLabelHorizAlign
;
1001 int m_rowLabelVertAlign
;
1002 int m_colLabelHorizAlign
;
1003 int m_colLabelVertAlign
;
1005 bool m_defaultRowLabelValues
;
1006 bool m_defaultColLabelValues
;
1008 wxColour m_gridLineColour
;
1009 bool m_gridLinesEnabled
;
1011 // get the renderer for the given cell - if it has no special one, the
1012 // default one will be returned, never NULL
1013 wxGridCellRenderer
*GetCellRenderer(int row
, int col
);
1015 wxGridCellRenderer
*m_defaultRenderer
;
1017 // default cell attributes
1018 wxFont m_defaultCellFont
;
1019 int m_defaultCellHAlign
,
1020 m_defaultCellVAlign
;
1022 // do we have some place to store attributes in?
1023 bool CanHaveAttributes();
1025 // returns the attribute we may modify in place: a new one if this cell
1026 // doesn't have any yet or the existing one if it does
1028 // DecRef() must be called on the returned pointer, as usual
1029 wxGridCellAttr
*GetCellAttr(int row
, int col
) const;
1031 wxGridCellCoordsArray m_cellsExposed
;
1032 wxArrayInt m_rowsExposed
;
1033 wxArrayInt m_colsExposed
;
1034 wxArrayInt m_rowLabelsExposed
;
1035 wxArrayInt m_colLabelsExposed
;
1042 WXGRID_CURSOR_SELECT_CELL
,
1043 WXGRID_CURSOR_RESIZE_ROW
,
1044 WXGRID_CURSOR_RESIZE_COL
,
1045 WXGRID_CURSOR_SELECT_ROW
,
1046 WXGRID_CURSOR_SELECT_COL
1049 // this method not only sets m_cursorMode but also sets the correct cursor
1050 // for the given mode and, if captureMouse is not FALSE releases the mouse
1051 // if it was captured and captures it if it must be captured
1053 // for this to work, you should always use it and not set m_cursorMode
1055 void ChangeCursorMode(CursorMode mode
,
1056 wxWindow
*win
= (wxWindow
*)NULL
,
1057 bool captureMouse
= TRUE
);
1059 wxWindow
*m_winCapture
; // the window which captured the mouse
1060 CursorMode m_cursorMode
;
1066 wxGridCellCoords m_selectionStart
;
1068 wxCursor m_rowResizeCursor
;
1069 wxCursor m_colResizeCursor
;
1071 bool m_editable
; // applies to whole grid
1072 int m_editCtrlType
; // for current cell
1073 wxWindow
* m_cellEditCtrl
;
1074 bool m_cellEditCtrlEnabled
;
1079 void CalcDimensions();
1080 void CalcWindowSizes();
1081 bool Redimension( wxGridTableMessage
& );
1084 bool SendEvent( const wxEventType
,
1088 bool SendEvent( const wxEventType
,
1092 void OnPaint( wxPaintEvent
& );
1093 void OnSize( wxSizeEvent
& );
1094 void OnKeyDown( wxKeyEvent
& );
1097 void SetCurrentCell( const wxGridCellCoords
& coords
);
1098 void SetCurrentCell( int row
, int col
)
1099 { SetCurrentCell( wxGridCellCoords(row
, col
) ); }
1102 // ------ functions to get/send data (see also public functions)
1104 bool GetModelValues();
1105 bool SetModelValues();
1108 DECLARE_DYNAMIC_CLASS( wxGrid
)
1109 DECLARE_EVENT_TABLE()
1117 // ------ Grid event class and event types
1120 class WXDLLEXPORT wxGridEvent
: public wxNotifyEvent
1124 : wxNotifyEvent(), m_row(-1), m_col(-1), m_x(-1), m_y(-1),
1125 m_control(0), m_meta(0), m_shift(0), m_alt(0)
1129 wxGridEvent(int id
, wxEventType type
, wxObject
* obj
,
1130 int row
=-1, int col
=-1, int x
=-1, int y
=-1,
1131 bool control
=FALSE
, bool shift
=FALSE
, bool alt
=FALSE
, bool meta
=FALSE
);
1133 virtual int GetRow() { return m_row
; }
1134 virtual int GetCol() { return m_col
; }
1135 wxPoint
GetPosition() { return wxPoint( m_x
, m_y
); }
1136 bool ControlDown() { return m_control
; }
1137 bool MetaDown() { return m_meta
; }
1138 bool ShiftDown() { return m_shift
; }
1139 bool AltDown() { return m_alt
; }
1151 DECLARE_DYNAMIC_CLASS(wxGridEvent
)
1155 class WXDLLEXPORT wxGridSizeEvent
: public wxNotifyEvent
1159 : wxNotifyEvent(), m_rowOrCol(-1), m_x(-1), m_y(-1),
1160 m_control(0), m_meta(0), m_shift(0), m_alt(0)
1164 wxGridSizeEvent(int id
, wxEventType type
, wxObject
* obj
,
1165 int rowOrCol
=-1, int x
=-1, int y
=-1,
1166 bool control
=FALSE
, bool shift
=FALSE
, bool alt
=FALSE
, bool meta
=FALSE
);
1168 int GetRowOrCol() { return m_rowOrCol
; }
1169 wxPoint
GetPosition() { return wxPoint( m_x
, m_y
); }
1170 bool ControlDown() { return m_control
; }
1171 bool MetaDown() { return m_meta
; }
1172 bool ShiftDown() { return m_shift
; }
1173 bool AltDown() { return m_alt
; }
1184 DECLARE_DYNAMIC_CLASS(wxGridSizeEvent
)
1188 class WXDLLEXPORT wxGridRangeSelectEvent
: public wxNotifyEvent
1191 wxGridRangeSelectEvent()
1194 m_topLeft
= wxGridNoCellCoords
;
1195 m_bottomRight
= wxGridNoCellCoords
;
1202 wxGridRangeSelectEvent(int id
, wxEventType type
, wxObject
* obj
,
1203 const wxGridCellCoords
& topLeft
,
1204 const wxGridCellCoords
& bottomRight
,
1205 bool control
=FALSE
, bool shift
=FALSE
,
1206 bool alt
=FALSE
, bool meta
=FALSE
);
1208 wxGridCellCoords
GetTopLeftCoords() { return m_topLeft
; }
1209 wxGridCellCoords
GetBottomRightCoords() { return m_bottomRight
; }
1210 int GetTopRow() { return m_topLeft
.GetRow(); }
1211 int GetBottomRow() { return m_bottomRight
.GetRow(); }
1212 int GetLeftCol() { return m_topLeft
.GetCol(); }
1213 int GetRightCol() { return m_bottomRight
.GetCol(); }
1214 bool ControlDown() { return m_control
; }
1215 bool MetaDown() { return m_meta
; }
1216 bool ShiftDown() { return m_shift
; }
1217 bool AltDown() { return m_alt
; }
1220 wxGridCellCoords m_topLeft
;
1221 wxGridCellCoords m_bottomRight
;
1227 DECLARE_DYNAMIC_CLASS(wxGridRangeSelectEvent
)
1231 const wxEventType EVT_GRID_CELL_LEFT_CLICK
= wxEVT_FIRST
+ 1580;
1232 const wxEventType EVT_GRID_CELL_RIGHT_CLICK
= wxEVT_FIRST
+ 1581;
1233 const wxEventType EVT_GRID_CELL_LEFT_DCLICK
= wxEVT_FIRST
+ 1582;
1234 const wxEventType EVT_GRID_CELL_RIGHT_DCLICK
= wxEVT_FIRST
+ 1583;
1235 const wxEventType EVT_GRID_LABEL_LEFT_CLICK
= wxEVT_FIRST
+ 1584;
1236 const wxEventType EVT_GRID_LABEL_RIGHT_CLICK
= wxEVT_FIRST
+ 1585;
1237 const wxEventType EVT_GRID_LABEL_LEFT_DCLICK
= wxEVT_FIRST
+ 1586;
1238 const wxEventType EVT_GRID_LABEL_RIGHT_DCLICK
= wxEVT_FIRST
+ 1587;
1239 const wxEventType EVT_GRID_ROW_SIZE
= wxEVT_FIRST
+ 1588;
1240 const wxEventType EVT_GRID_COL_SIZE
= wxEVT_FIRST
+ 1589;
1241 const wxEventType EVT_GRID_RANGE_SELECT
= wxEVT_FIRST
+ 1590;
1242 const wxEventType EVT_GRID_CELL_CHANGE
= wxEVT_FIRST
+ 1591;
1243 const wxEventType EVT_GRID_SELECT_CELL
= wxEVT_FIRST
+ 1592;
1246 typedef void (wxEvtHandler
::*wxGridEventFunction
)(wxGridEvent
&);
1247 typedef void (wxEvtHandler
::*wxGridSizeEventFunction
)(wxGridSizeEvent
&);
1248 typedef void (wxEvtHandler
::*wxGridRangeSelectEventFunction
)(wxGridRangeSelectEvent
&);
1250 #define EVT_GRID_CELL_LEFT_CLICK(fn) { EVT_GRID_CELL_LEFT_CLICK, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridEventFunction) &fn, NULL },
1251 #define EVT_GRID_CELL_RIGHT_CLICK(fn) { EVT_GRID_CELL_RIGHT_CLICK, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridEventFunction) &fn, NULL },
1252 #define EVT_GRID_CELL_LEFT_DCLICK(fn) { EVT_GRID_CELL_LEFT_DCLICK, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridEventFunction) &fn, NULL },
1253 #define EVT_GRID_CELL_RIGHT_DCLICK(fn) { EVT_GRID_CELL_RIGHT_DCLICK, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridEventFunction) &fn, NULL },
1254 #define EVT_GRID_LABEL_LEFT_CLICK(fn) { EVT_GRID_LABEL_LEFT_CLICK, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridEventFunction) &fn, NULL },
1255 #define EVT_GRID_LABEL_RIGHT_CLICK(fn) { EVT_GRID_LABEL_RIGHT_CLICK, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridEventFunction) &fn, NULL },
1256 #define EVT_GRID_LABEL_LEFT_DCLICK(fn) { EVT_GRID_LABEL_LEFT_DCLICK, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridEventFunction) &fn, NULL },
1257 #define EVT_GRID_LABEL_RIGHT_DCLICK(fn) { EVT_GRID_LABEL_RIGHT_DCLICK, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridEventFunction) &fn, NULL },
1258 #define EVT_GRID_ROW_SIZE(fn) { EVT_GRID_ROW_SIZE, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridSizeEventFunction) &fn, NULL },
1259 #define EVT_GRID_COL_SIZE(fn) { EVT_GRID_COL_SIZE, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridSizeEventFunction) &fn, NULL },
1260 #define EVT_GRID_RANGE_SELECT(fn) { EVT_GRID_RANGE_SELECT, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridRangeSelectEventFunction) &fn, NULL },
1261 #define EVT_GRID_CELL_CHANGE(fn) { EVT_GRID_CELL_CHANGE, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridEventFunction) &fn, NULL },
1262 #define EVT_GRID_SELECT_CELL(fn) { EVT_GRID_SELECT_CELL, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridEventFunction) &fn, NULL },
1265 #if 0 // TODO: implement these ? others ?
1267 const wxEventType EVT_GRID_CREATE_CELL
= wxEVT_FIRST
+ 1576;
1268 const wxEventType EVT_GRID_CHANGE_LABELS
= wxEVT_FIRST
+ 1577;
1269 const wxEventType EVT_GRID_CHANGE_SEL_LABEL
= wxEVT_FIRST
+ 1578;
1271 #define EVT_GRID_CREATE_CELL(fn) { EVT_GRID_CREATE_CELL, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridEventFunction) &fn, NULL },
1272 #define EVT_GRID_CHANGE_LABELS(fn) { EVT_GRID_CHANGE_LABELS, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridEventFunction) &fn, NULL },
1273 #define EVT_GRID_CHANGE_SEL_LABEL(fn) { EVT_GRID_CHANGE_SEL_LABEL, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridEventFunction) &fn, NULL },
1277 #endif // #ifndef __WXGRID_H__
1279 #endif // ifndef wxUSE_NEW_GRID