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 SafeIncRef() { if ( this ) IncRef(); }
138 void SafeDecRef() { if ( this ) DecRef(); }
141 void SetTextColour(const wxColour
& colText
) { m_colText
= colText
; }
142 void SetBackgroundColour(const wxColour
& colBack
) { m_colBack
= colBack
; }
143 void SetFont(const wxFont
& font
) { m_font
= font
; }
144 void SetAlignment(int hAlign
, int vAlign
)
150 // takes ownership of the pointer
151 void SetRenderer(wxGridCellRenderer
*renderer
)
152 { delete m_renderer
; m_renderer
= renderer
; }
155 bool HasTextColour() const { return m_colText
.Ok(); }
156 bool HasBackgroundColour() const { return m_colBack
.Ok(); }
157 bool HasFont() const { return m_font
.Ok(); }
158 bool HasAlignment() const { return m_hAlign
|| m_vAlign
; }
160 const wxColour
& GetTextColour() const { return m_colText
; }
161 const wxColour
& GetBackgroundColour() const { return m_colBack
; }
162 const wxFont
& GetFont() const { return m_font
; }
163 void GetAlignment(int *hAlign
, int *vAlign
)
165 if ( hAlign
) *hAlign
= m_hAlign
;
166 if ( vAlign
) *vAlign
= m_vAlign
;
169 wxGridCellRenderer
*GetRenderer() const { return m_renderer
; }
172 // the common part of all ctors
173 void Init() { m_nRef
= 1; m_renderer
= (wxGridCellRenderer
*)NULL
; }
175 // the dtor is private because only DecRef() can delete us
176 ~wxGridCellAttr() { delete m_renderer
; }
178 // the ref count - when it goes to 0, we die
187 wxGridCellRenderer
*m_renderer
;
189 // suppress the stupid gcc warning about the class having private dtor and
191 friend class wxGridCellAttrDummyFriend
;
194 // ----------------------------------------------------------------------------
195 // wxGridCellAttrProvider: class used by wxGridTableBase to retrieve/store the
197 // ----------------------------------------------------------------------------
199 // implementation note: we separate it from wxGridTableBase because we wish to
200 // avoid deriving a new table class if possible, and sometimes it will be
201 // enough to just derive another wxGridCellAttrProvider instead
203 class WXDLLEXPORT wxGridCellAttrProvider
206 wxGridCellAttrProvider();
207 virtual ~wxGridCellAttrProvider();
209 // DecRef() must be called on the returned pointer
210 virtual wxGridCellAttr
*GetAttr(int row
, int col
) const;
212 // takes ownership of the pointer, don't call DecRef() on it
213 virtual void SetAttr(wxGridCellAttr
*attr
, int row
, int col
);
218 wxGridCellAttrProviderData
*m_data
;
221 //////////////////////////////////////////////////////////////////////
223 // Grid table classes
225 //////////////////////////////////////////////////////////////////////
228 class WXDLLEXPORT wxGridTableBase
: public wxObject
232 virtual ~wxGridTableBase();
234 // You must override these functions in a derived table class
236 virtual long GetNumberRows() = 0;
237 virtual long GetNumberCols() = 0;
238 virtual wxString
GetValue( int row
, int col
) = 0;
239 virtual void SetValue( int row
, int col
, const wxString
& s
) = 0;
240 virtual bool IsEmptyCell( int row
, int col
) = 0;
242 // Overriding these is optional
244 virtual void SetView( wxGrid
*grid
) { m_view
= grid
; }
245 virtual wxGrid
* GetView() const { return m_view
; }
247 virtual void Clear() {}
248 virtual bool InsertRows( size_t pos
= 0, size_t numRows
= 1 );
249 virtual bool AppendRows( size_t numRows
= 1 );
250 virtual bool DeleteRows( size_t pos
= 0, size_t numRows
= 1 );
251 virtual bool InsertCols( size_t pos
= 0, size_t numCols
= 1 );
252 virtual bool AppendCols( size_t numCols
= 1 );
253 virtual bool DeleteCols( size_t pos
= 0, size_t numCols
= 1 );
255 virtual wxString
GetRowLabelValue( int row
);
256 virtual wxString
GetColLabelValue( int col
);
257 virtual void SetRowLabelValue( int WXUNUSED(row
), const wxString
& ) {}
258 virtual void SetColLabelValue( int WXUNUSED(col
), const wxString
& ) {}
260 // Attribute handling
263 // give us the attr provider to use - we take ownership of the pointer
264 void SetAttrProvider(wxGridCellAttrProvider
*attrProvider
);
266 // get the currently used attr provider (may be NULL)
267 wxGridCellAttrProvider
*GetAttrProvider() const { return m_attrProvider
; }
269 // by default forwarded to wxGridCellAttrProvider if any. May be
270 // overridden to handle attributes directly in this class.
271 virtual wxGridCellAttr
*GetAttr( int row
, int col
);
273 // takes ownership of the pointer
274 virtual void SetAttr(wxGridCellAttr
*attr
, int row
, int col
);
278 wxGridCellAttrProvider
*m_attrProvider
;
280 DECLARE_ABSTRACT_CLASS( wxGridTableBase
);
284 // ----------------------------------------------------------------------------
285 // wxGridTableMessage
286 // ----------------------------------------------------------------------------
288 // IDs for messages sent from grid table to view
290 enum wxGridTableRequest
292 wxGRIDTABLE_REQUEST_VIEW_GET_VALUES
= 2000,
293 wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES
,
294 wxGRIDTABLE_NOTIFY_ROWS_INSERTED
,
295 wxGRIDTABLE_NOTIFY_ROWS_APPENDED
,
296 wxGRIDTABLE_NOTIFY_ROWS_DELETED
,
297 wxGRIDTABLE_NOTIFY_COLS_INSERTED
,
298 wxGRIDTABLE_NOTIFY_COLS_APPENDED
,
299 wxGRIDTABLE_NOTIFY_COLS_DELETED
302 class WXDLLEXPORT wxGridTableMessage
305 wxGridTableMessage();
306 wxGridTableMessage( wxGridTableBase
*table
, int id
,
310 void SetTableObject( wxGridTableBase
*table
) { m_table
= table
; }
311 wxGridTableBase
* GetTableObject() const { return m_table
; }
312 void SetId( int id
) { m_id
= id
; }
313 int GetId() { return m_id
; }
314 void SetCommandInt( int comInt1
) { m_comInt1
= comInt1
; }
315 int GetCommandInt() { return m_comInt1
; }
316 void SetCommandInt2( int comInt2
) { m_comInt2
= comInt2
; }
317 int GetCommandInt2() { return m_comInt2
; }
320 wxGridTableBase
*m_table
;
328 // ------ wxGridStringArray
329 // A 2-dimensional array of strings for data values
332 WX_DECLARE_EXPORTED_OBJARRAY(wxArrayString
, wxGridStringArray
);
336 // ------ wxGridStringTable
338 // Simplest type of data table for a grid for small tables of strings
339 // that are stored in memory
342 class WXDLLEXPORT wxGridStringTable
: public wxGridTableBase
346 wxGridStringTable( int numRows
, int numCols
);
347 ~wxGridStringTable();
349 // these are pure virtual in wxGridTableBase
351 long GetNumberRows();
352 long GetNumberCols();
353 wxString
GetValue( int row
, int col
);
354 void SetValue( int row
, int col
, const wxString
& s
);
355 bool IsEmptyCell( int row
, int col
);
357 // overridden functions from wxGridTableBase
360 bool InsertRows( size_t pos
= 0, size_t numRows
= 1 );
361 bool AppendRows( size_t numRows
= 1 );
362 bool DeleteRows( size_t pos
= 0, size_t numRows
= 1 );
363 bool InsertCols( size_t pos
= 0, size_t numCols
= 1 );
364 bool AppendCols( size_t numCols
= 1 );
365 bool DeleteCols( size_t pos
= 0, size_t numCols
= 1 );
367 void SetRowLabelValue( int row
, const wxString
& );
368 void SetColLabelValue( int col
, const wxString
& );
369 wxString
GetRowLabelValue( int row
);
370 wxString
GetColLabelValue( int col
);
373 wxGridStringArray m_data
;
375 // These only get used if you set your own labels, otherwise the
376 // GetRow/ColLabelValue functions return wxGridTableBase defaults
378 wxArrayString m_rowLabels
;
379 wxArrayString m_colLabels
;
381 DECLARE_DYNAMIC_CLASS( wxGridStringTable
)
386 //////////////////////////////////////////////////////////////////////
390 //////////////////////////////////////////////////////////////////////
392 class WXDLLEXPORT wxGridCellCoords
395 wxGridCellCoords() { m_row
= m_col
= -1; }
396 wxGridCellCoords( int r
, int c
) { m_row
= r
; m_col
= c
; }
398 // default copy ctor is ok
400 long GetRow() const { return m_row
; }
401 void SetRow( long n
) { m_row
= n
; }
402 long GetCol() const { return m_col
; }
403 void SetCol( long n
) { m_col
= n
; }
404 void Set( long row
, long col
) { m_row
= row
; m_col
= col
; }
406 wxGridCellCoords
& operator=( const wxGridCellCoords
& other
)
408 if ( &other
!= this )
416 bool operator==( const wxGridCellCoords
& other
)
418 return (m_row
== other
.m_row
&& m_col
== other
.m_col
);
421 bool operator!=( const wxGridCellCoords
& other
)
423 return (m_row
!= other
.m_row
|| m_col
!= other
.m_col
);
428 return (m_row
== -1 && m_col
== -1 );
437 // For comparisons...
439 extern wxGridCellCoords wxGridNoCellCoords
;
440 extern wxRect wxGridNoCellRect
;
442 // An array of cell coords...
444 WX_DECLARE_EXPORTED_OBJARRAY(wxGridCellCoords
, wxGridCellCoordsArray
);
448 // This set of classes is to provide for the use of different types of
449 // cell edit controls in the grid while avoiding the wx class info
450 // system in deference to wxPython
452 class WXDLLEXPORT wxGridTextCtrl
: public wxTextCtrl
456 wxGridTextCtrl( wxWindow
*,
460 const wxString
& value
= wxEmptyString
,
461 const wxPoint
& pos
= wxDefaultPosition
,
462 const wxSize
& size
= wxDefaultSize
,
465 void SetStartValue( const wxString
& );
466 wxString
GetStartValue() { return startValue
; }
471 // TRUE for controls placed over cells,
472 // FALSE for a control on a grid control panel
473 bool m_isCellControl
;
477 void OnKeyDown( wxKeyEvent
& );
479 DECLARE_DYNAMIC_CLASS( wxGridTextCtrl
)
480 DECLARE_EVENT_TABLE()
483 // ----------------------------------------------------------------------------
485 // ----------------------------------------------------------------------------
487 class WXDLLEXPORT wxGrid
: public wxScrolledWindow
492 m_table
= (wxGridTableBase
*) NULL
;
493 m_gridWin
= (wxGridWindow
*) NULL
;
494 m_rowLabelWin
= (wxGridRowLabelWindow
*) NULL
;
495 m_colLabelWin
= (wxGridColLabelWindow
*) NULL
;
496 m_cornerLabelWin
= (wxGridCornerLabelWindow
*) NULL
;
497 m_cellEditCtrl
= (wxWindow
*) NULL
;
500 wxGrid( wxWindow
*parent
,
502 const wxPoint
& pos
= wxDefaultPosition
,
503 const wxSize
& size
= wxDefaultSize
,
505 const wxString
& name
= wxPanelNameStr
);
509 bool CreateGrid( int numRows
, int numCols
);
512 // ------ grid dimensions
514 int GetNumberRows() { return m_numRows
; }
515 int GetNumberCols() { return m_numCols
; }
518 // ------ display update functions
520 void CalcRowLabelsExposed( wxRegion
& reg
);
522 void CalcColLabelsExposed( wxRegion
& reg
);
523 void CalcCellsExposed( wxRegion
& reg
);
526 // ------ event handlers
528 void ProcessRowLabelMouseEvent( wxMouseEvent
& event
);
529 void ProcessColLabelMouseEvent( wxMouseEvent
& event
);
530 void ProcessCornerLabelMouseEvent( wxMouseEvent
& event
);
531 void ProcessGridCellMouseEvent( wxMouseEvent
& event
);
532 bool ProcessTableMessage( wxGridTableMessage
& );
534 void DoEndDragResizeRow();
535 void DoEndDragResizeCol();
537 wxGridTableBase
* GetTable() const { return m_table
; }
538 void SetTable( wxGridTableBase
*table
) { m_table
= table
; }
541 bool InsertRows( int pos
= 0, int numRows
= 1, bool updateLabels
=TRUE
);
542 bool AppendRows( int numRows
= 1, bool updateLabels
=TRUE
);
543 bool DeleteRows( int pos
= 0, int numRows
= 1, bool updateLabels
=TRUE
);
544 bool InsertCols( int pos
= 0, int numCols
= 1, bool updateLabels
=TRUE
);
545 bool AppendCols( int numCols
= 1, bool updateLabels
=TRUE
);
546 bool DeleteCols( int pos
= 0, int numCols
= 1, bool updateLabels
=TRUE
);
548 void DrawGridCellArea( wxDC
& dc
);
549 void DrawCellBorder( wxDC
& dc
, const wxGridCellCoords
& );
550 void DrawAllGridLines( wxDC
& dc
, const wxRegion
& reg
);
551 void DrawCell( wxDC
& dc
, const wxGridCellCoords
& );
553 void DrawRowLabels( wxDC
& dc
);
554 void DrawRowLabel( wxDC
& dc
, int row
);
556 void DrawColLabels( wxDC
& dc
);
557 void DrawColLabel( wxDC
& dc
, int col
);
560 // ------ Cell text drawing functions
562 void DrawTextRectangle( wxDC
& dc
, const wxString
&, const wxRect
&,
563 int horizontalAlignment
= wxLEFT
,
564 int verticalAlignment
= wxTOP
);
566 // Split a string containing newline chararcters into an array of
567 // strings and return the number of lines
569 void StringToLines( const wxString
& value
, wxArrayString
& lines
);
571 void GetTextBoxSize( wxDC
& dc
,
572 wxArrayString
& lines
,
573 long *width
, long *height
);
577 // Code that does a lot of grid modification can be enclosed
578 // between BeginBatch() and EndBatch() calls to avoid screen
581 void BeginBatch() { m_batchCount
++; }
582 void EndBatch() { if ( m_batchCount
> 0 ) m_batchCount
--; }
583 int GetBatchCount() { return m_batchCount
; }
586 // ------ edit control functions
588 bool IsEditable() { return m_editable
; }
589 void EnableEditing( bool edit
);
591 #if 0 // at the moment the cell edit control is always active
592 void EnableCellEditControl( bool enable
);
595 bool IsCellEditControlEnabled()
596 { return (m_cellEditCtrl
&& m_cellEditCtrlEnabled
); }
598 void ShowCellEditControl();
599 void HideCellEditControl();
600 void SetEditControlValue( const wxString
& s
= wxEmptyString
);
601 void SaveEditControlValue();
604 // ------ grid location functions
605 // Note that all of these functions work with the logical coordinates of
606 // grid cells and labels so you will need to convert from device
607 // coordinates for mouse events etc.
609 void XYToCell( int x
, int y
, wxGridCellCoords
& );
613 int YToEdgeOfRow( int y
);
614 int XToEdgeOfCol( int x
);
616 wxRect
CellToRect( int row
, int col
);
617 wxRect
CellToRect( const wxGridCellCoords
& coords
)
618 { return CellToRect( coords
.GetRow(), coords
.GetCol() ); }
620 int GetGridCursorRow() { return m_currentCellCoords
.GetRow(); }
621 int GetGridCursorCol() { return m_currentCellCoords
.GetCol(); }
623 // check to see if a cell is either wholly visible (the default arg) or
624 // at least partially visible in the grid window
626 bool IsVisible( int row
, int col
, bool wholeCellVisible
= TRUE
);
627 bool IsVisible( const wxGridCellCoords
& coords
, bool wholeCellVisible
= TRUE
)
628 { return IsVisible( coords
.GetRow(), coords
.GetCol(), wholeCellVisible
); }
629 void MakeCellVisible( int row
, int col
);
630 void MakeCellVisible( const wxGridCellCoords
& coords
)
631 { MakeCellVisible( coords
.GetRow(), coords
.GetCol() ); }
634 // ------ grid cursor movement functions
636 void SetGridCursor( int row
, int col
)
637 { SetCurrentCell( wxGridCellCoords(row
, col
) ); }
640 bool MoveCursorDown();
641 bool MoveCursorLeft();
642 bool MoveCursorRight();
645 bool MoveCursorUpBlock();
646 bool MoveCursorDownBlock();
647 bool MoveCursorLeftBlock();
648 bool MoveCursorRightBlock();
651 // ------ label and gridline formatting
653 int GetDefaultRowLabelSize() { return WXGRID_DEFAULT_ROW_LABEL_WIDTH
; }
654 int GetRowLabelSize() { return m_rowLabelWidth
; }
655 int GetDefaultColLabelSize() { return WXGRID_DEFAULT_COL_LABEL_HEIGHT
; }
656 int GetColLabelSize() { return m_colLabelHeight
; }
657 wxColour
GetLabelBackgroundColour() { return m_labelBackgroundColour
; }
658 wxColour
GetLabelTextColour() { return m_labelTextColour
; }
659 wxFont
GetLabelFont() { return m_labelFont
; }
660 void GetRowLabelAlignment( int *horiz
, int *vert
);
661 void GetColLabelAlignment( int *horiz
, int *vert
);
662 wxString
GetRowLabelValue( int row
);
663 wxString
GetColLabelValue( int col
);
664 wxColour
GetGridLineColour() { return m_gridLineColour
; }
666 void SetRowLabelSize( int width
);
667 void SetColLabelSize( int height
);
668 void SetLabelBackgroundColour( const wxColour
& );
669 void SetLabelTextColour( const wxColour
& );
670 void SetLabelFont( const wxFont
& );
671 void SetRowLabelAlignment( int horiz
, int vert
);
672 void SetColLabelAlignment( int horiz
, int vert
);
673 void SetRowLabelValue( int row
, const wxString
& );
674 void SetColLabelValue( int col
, const wxString
& );
675 void SetGridLineColour( const wxColour
& );
677 void EnableGridLines( bool enable
= TRUE
);
678 bool GridLinesEnabled() { return m_gridLinesEnabled
; }
680 // ------ row and col formatting
682 int GetDefaultRowSize();
683 int GetRowSize( int row
);
684 int GetDefaultColSize();
685 int GetColSize( int col
);
686 wxColour
GetDefaultCellBackgroundColour();
687 wxColour
GetCellBackgroundColour( int row
, int col
);
688 wxColour
GetDefaultCellTextColour();
689 wxColour
GetCellTextColour( int row
, int col
);
690 wxFont
GetDefaultCellFont();
691 wxFont
GetCellFont( int row
, int col
);
692 void GetDefaultCellAlignment( int *horiz
, int *vert
);
693 void GetCellAlignment( int row
, int col
, int *horiz
, int *vert
);
695 void SetDefaultRowSize( int height
, bool resizeExistingRows
= FALSE
);
696 void SetRowSize( int row
, int height
);
697 void SetDefaultColSize( int width
, bool resizeExistingCols
= FALSE
);
699 void SetColSize( int col
, int width
);
700 void SetDefaultCellBackgroundColour( const wxColour
& );
701 void SetCellBackgroundColour( int row
, int col
, const wxColour
& );
702 void SetDefaultCellTextColour( const wxColour
& );
704 void SetCellTextColour( int row
, int col
, const wxColour
& );
705 void SetDefaultCellFont( const wxFont
& );
706 void SetCellFont( int row
, int col
, const wxFont
& );
707 void SetDefaultCellAlignment( int horiz
, int vert
);
708 void SetCellAlignment( int row
, int col
, int horiz
, int vert
);
710 // takes ownership of the pointer
711 void SetDefaultRenderer(wxGridCellRenderer
*renderer
)
712 { delete m_defaultRenderer
; m_defaultRenderer
= renderer
; }
713 wxGridCellRenderer
*GetDefaultRenderer() const
714 { return m_defaultRenderer
; }
716 void SetCellRenderer(int row
, int col
, wxGridCellRenderer
*renderer
);
718 // ------ cell value accessors
720 wxString
GetCellValue( int row
, int col
)
724 return m_table
->GetValue( row
, col
);
728 return wxEmptyString
;
732 wxString
GetCellValue( const wxGridCellCoords
& coords
)
733 { return GetCellValue( coords
.GetRow(), coords
.GetCol() ); }
735 void SetCellValue( int row
, int col
, const wxString
& s
);
736 void SetCellValue( const wxGridCellCoords
& coords
, const wxString
& s
)
737 { SetCellValue( coords
.GetRow(), coords
.GetCol(), s
); }
741 // ------ selections of blocks of cells
743 void SelectRow( int row
, bool addToSelected
= FALSE
);
744 void SelectCol( int col
, bool addToSelected
= FALSE
);
746 void SelectBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
);
748 void SelectBlock( const wxGridCellCoords
& topLeft
,
749 const wxGridCellCoords
& bottomRight
)
750 { SelectBlock( topLeft
.GetRow(), topLeft
.GetCol(),
751 bottomRight
.GetRow(), bottomRight
.GetCol() ); }
756 { return ( m_selectedTopLeft
!= wxGridNoCellCoords
&&
757 m_selectedBottomRight
!= wxGridNoCellCoords
);
760 void ClearSelection();
762 bool IsInSelection( int row
, int col
)
763 { return ( IsSelection() &&
764 row
>= m_selectedTopLeft
.GetRow() &&
765 col
>= m_selectedTopLeft
.GetCol() &&
766 row
<= m_selectedBottomRight
.GetRow() &&
767 col
<= m_selectedBottomRight
.GetCol() );
770 bool IsInSelection( const wxGridCellCoords
& coords
)
771 { return IsInSelection( coords
.GetRow(), coords
.GetCol() ); }
773 void GetSelection( int* topRow
, int* leftCol
, int* bottomRow
, int* rightCol
)
775 // these will all be -1 if there is no selected block
777 *topRow
= m_selectedTopLeft
.GetRow();
778 *leftCol
= m_selectedTopLeft
.GetCol();
779 *bottomRow
= m_selectedBottomRight
.GetRow();
780 *rightCol
= m_selectedBottomRight
.GetCol();
784 // This function returns the rectangle that encloses the block of cells
785 // limited by TopLeft and BottomRight cell in device coords and clipped
786 // to the client size of the grid window.
788 wxRect
BlockToDeviceRect( const wxGridCellCoords
& topLeft
,
789 const wxGridCellCoords
& bottomRight
);
791 // This function returns the rectangle that encloses the selected cells
792 // in device coords and clipped to the client size of the grid window.
794 wxRect
SelectionToDeviceRect()
796 return BlockToDeviceRect( m_selectedTopLeft
,
797 m_selectedBottomRight
);
801 // ------ For compatibility with previous wxGrid only...
803 // ************************************************
804 // ** Don't use these in new code because they **
805 // ** are liable to disappear in a future **
807 // ************************************************
810 wxGrid( wxWindow
*parent
,
811 int x
= -1, int y
= -1, int w
= -1, int h
= -1,
813 const wxString
& name
= wxPanelNameStr
)
814 : wxScrolledWindow( parent
, -1, wxPoint(x
,y
), wxSize(w
,h
), style
, name
)
819 void SetCellValue( const wxString
& val
, int row
, int col
)
820 { SetCellValue( row
, col
, val
); }
822 void UpdateDimensions()
823 { CalcDimensions(); }
825 int GetRows() { return GetNumberRows(); }
826 int GetCols() { return GetNumberCols(); }
827 int GetCursorRow() { return GetGridCursorRow(); }
828 int GetCursorColumn() { return GetGridCursorCol(); }
830 int GetScrollPosX() { return 0; }
831 int GetScrollPosY() { return 0; }
833 void SetScrollX( int x
) { }
834 void SetScrollY( int y
) { }
836 void SetColumnWidth( int col
, int width
)
837 { SetColSize( col
, width
); }
839 int GetColumnWidth( int col
)
840 { return GetColSize( col
); }
842 void SetRowHeight( int row
, int height
)
843 { SetRowSize( row
, height
); }
845 int GetRowHeight( int row
)
846 { return GetRowSize( row
); }
848 int GetViewHeight() // returned num whole rows visible
851 int GetViewWidth() // returned num whole cols visible
854 void SetLabelSize( int orientation
, int sz
)
856 if ( orientation
== wxHORIZONTAL
)
857 SetColLabelSize( sz
);
859 SetRowLabelSize( sz
);
862 int GetLabelSize( int orientation
)
864 if ( orientation
== wxHORIZONTAL
)
865 return GetColLabelSize();
867 return GetRowLabelSize();
870 void SetLabelAlignment( int orientation
, int align
)
872 if ( orientation
== wxHORIZONTAL
)
873 SetColLabelAlignment( align
, -1 );
875 SetRowLabelAlignment( align
, -1 );
878 int GetLabelAlignment( int orientation
, int WXUNUSED(align
) )
881 if ( orientation
== wxHORIZONTAL
)
883 GetColLabelAlignment( &h
, &v
);
888 GetRowLabelAlignment( &h
, &v
);
893 void SetLabelValue( int orientation
, const wxString
& val
, int pos
)
895 if ( orientation
== wxHORIZONTAL
)
896 SetColLabelValue( pos
, val
);
898 SetRowLabelValue( pos
, val
);
901 wxString
GetLabelValue( int orientation
, int pos
)
903 if ( orientation
== wxHORIZONTAL
)
904 return GetColLabelValue( pos
);
906 return GetRowLabelValue( pos
);
909 wxFont
GetCellTextFont() const
910 { return m_defaultCellFont
; }
912 wxFont
GetCellTextFont(int WXUNUSED(row
), int WXUNUSED(col
)) const
913 { return m_defaultCellFont
; }
915 void SetCellTextFont(const wxFont
& fnt
)
916 { SetDefaultCellFont( fnt
); }
918 void SetCellTextFont(const wxFont
& fnt
, int row
, int col
)
919 { SetCellFont( row
, col
, fnt
); }
921 void SetCellTextColour(const wxColour
& val
, int row
, int col
)
922 { SetCellTextColour( row
, col
, val
); }
924 void SetCellTextColour(const wxColour
& col
)
925 { SetDefaultCellTextColour( col
); }
927 void SetCellBackgroundColour(const wxColour
& col
)
928 { SetDefaultCellBackgroundColour( col
); }
930 void SetCellBackgroundColour(const wxColour
& colour
, int row
, int col
)
931 { SetCellBackgroundColour( row
, col
, colour
); }
933 bool GetEditable() { return IsEditable(); }
934 void SetEditable( bool edit
= TRUE
) { EnableEditing( edit
); }
935 bool GetEditInPlace() { return IsCellEditControlEnabled(); }
937 void SetEditInPlace(bool edit
= TRUE
) { }
939 void SetCellAlignment( int align
, int row
, int col
)
940 { SetCellAlignment(row
, col
, align
, wxCENTER
); }
941 void SetCellAlignment( int WXUNUSED(align
) ) {}
942 void SetCellBitmap(wxBitmap
*WXUNUSED(bitmap
), int WXUNUSED(row
), int WXUNUSED(col
))
944 void SetDividerPen(const wxPen
& WXUNUSED(pen
)) { }
945 wxPen
& GetDividerPen() const { return wxNullPen
; }
946 void OnActivate(bool WXUNUSED(active
)) {}
948 // ******** End of compatibility functions **********
952 // ------ control IDs
953 enum { wxGRID_CELLCTRL
= 2000,
956 // ------ control types
957 enum { wxGRID_TEXTCTRL
= 2100,
966 wxGridWindow
*m_gridWin
;
967 wxGridRowLabelWindow
*m_rowLabelWin
;
968 wxGridColLabelWindow
*m_colLabelWin
;
969 wxGridCornerLabelWindow
*m_cornerLabelWin
;
971 wxGridTableBase
*m_table
;
981 wxGridCellCoords m_currentCellCoords
;
983 wxGridCellCoords m_selectedTopLeft
;
984 wxGridCellCoords m_selectedBottomRight
;
986 int m_defaultRowHeight
;
987 wxArrayInt m_rowHeights
;
988 wxArrayInt m_rowBottoms
;
990 int m_defaultColWidth
;
991 wxArrayInt m_colWidths
;
992 wxArrayInt m_colRights
;
995 int m_colLabelHeight
;
997 wxColour m_labelBackgroundColour
;
998 wxColour m_labelTextColour
;
1001 int m_rowLabelHorizAlign
;
1002 int m_rowLabelVertAlign
;
1003 int m_colLabelHorizAlign
;
1004 int m_colLabelVertAlign
;
1006 bool m_defaultRowLabelValues
;
1007 bool m_defaultColLabelValues
;
1009 wxColour m_gridLineColour
;
1010 bool m_gridLinesEnabled
;
1012 // get the renderer for the given cell - if it has no special one, the
1013 // default one will be returned, never NULL
1014 wxGridCellRenderer
*GetCellRenderer(int row
, int col
);
1016 wxGridCellRenderer
*m_defaultRenderer
;
1018 // default cell attributes
1019 wxFont m_defaultCellFont
;
1020 int m_defaultCellHAlign
,
1021 m_defaultCellVAlign
;
1023 // do we have some place to store attributes in?
1024 bool CanHaveAttributes();
1026 // returns the attribute we may modify in place: a new one if this cell
1027 // doesn't have any yet or the existing one if it does
1029 // DecRef() must be called on the returned pointer, as usual
1030 wxGridCellAttr
*GetOrCreateCellAttr(int row
, int col
) const;
1032 // cell attribute cache (currently we only cache 1, may be will do
1033 // more/better later)
1037 wxGridCellAttr
*attr
;
1040 // invalidates the attribute cache
1041 void ClearAttrCache();
1043 // adds an attribute to cache
1044 void CacheAttr(int row
, int col
, wxGridCellAttr
*attr
) const;
1046 // looks for an attr in cache, returns TRUE if found
1047 bool LookupAttr(int row
, int col
, wxGridCellAttr
**attr
) const;
1049 // looks for the attr in cache, if not found asks the table and caches the
1051 wxGridCellAttr
*GetCellAttr(int row
, int col
) const;
1053 wxGridCellCoordsArray m_cellsExposed
;
1054 wxArrayInt m_rowsExposed
;
1055 wxArrayInt m_colsExposed
;
1056 wxArrayInt m_rowLabelsExposed
;
1057 wxArrayInt m_colLabelsExposed
;
1064 WXGRID_CURSOR_SELECT_CELL
,
1065 WXGRID_CURSOR_RESIZE_ROW
,
1066 WXGRID_CURSOR_RESIZE_COL
,
1067 WXGRID_CURSOR_SELECT_ROW
,
1068 WXGRID_CURSOR_SELECT_COL
1071 // this method not only sets m_cursorMode but also sets the correct cursor
1072 // for the given mode and, if captureMouse is not FALSE releases the mouse
1073 // if it was captured and captures it if it must be captured
1075 // for this to work, you should always use it and not set m_cursorMode
1077 void ChangeCursorMode(CursorMode mode
,
1078 wxWindow
*win
= (wxWindow
*)NULL
,
1079 bool captureMouse
= TRUE
);
1081 wxWindow
*m_winCapture
; // the window which captured the mouse
1082 CursorMode m_cursorMode
;
1088 wxGridCellCoords m_selectionStart
;
1090 wxCursor m_rowResizeCursor
;
1091 wxCursor m_colResizeCursor
;
1093 bool m_editable
; // applies to whole grid
1094 int m_editCtrlType
; // for current cell
1095 wxWindow
* m_cellEditCtrl
;
1096 bool m_cellEditCtrlEnabled
;
1101 void CalcDimensions();
1102 void CalcWindowSizes();
1103 bool Redimension( wxGridTableMessage
& );
1106 bool SendEvent( const wxEventType
,
1110 bool SendEvent( const wxEventType
,
1114 void OnPaint( wxPaintEvent
& );
1115 void OnSize( wxSizeEvent
& );
1116 void OnKeyDown( wxKeyEvent
& );
1119 void SetCurrentCell( const wxGridCellCoords
& coords
);
1120 void SetCurrentCell( int row
, int col
)
1121 { SetCurrentCell( wxGridCellCoords(row
, col
) ); }
1124 // ------ functions to get/send data (see also public functions)
1126 bool GetModelValues();
1127 bool SetModelValues();
1130 DECLARE_DYNAMIC_CLASS( wxGrid
)
1131 DECLARE_EVENT_TABLE()
1139 // ------ Grid event class and event types
1142 class WXDLLEXPORT wxGridEvent
: public wxNotifyEvent
1146 : wxNotifyEvent(), m_row(-1), m_col(-1), m_x(-1), m_y(-1),
1147 m_control(0), m_meta(0), m_shift(0), m_alt(0)
1151 wxGridEvent(int id
, wxEventType type
, wxObject
* obj
,
1152 int row
=-1, int col
=-1, int x
=-1, int y
=-1,
1153 bool control
=FALSE
, bool shift
=FALSE
, bool alt
=FALSE
, bool meta
=FALSE
);
1155 virtual int GetRow() { return m_row
; }
1156 virtual int GetCol() { return m_col
; }
1157 wxPoint
GetPosition() { return wxPoint( m_x
, m_y
); }
1158 bool ControlDown() { return m_control
; }
1159 bool MetaDown() { return m_meta
; }
1160 bool ShiftDown() { return m_shift
; }
1161 bool AltDown() { return m_alt
; }
1173 DECLARE_DYNAMIC_CLASS(wxGridEvent
)
1177 class WXDLLEXPORT wxGridSizeEvent
: public wxNotifyEvent
1181 : wxNotifyEvent(), m_rowOrCol(-1), m_x(-1), m_y(-1),
1182 m_control(0), m_meta(0), m_shift(0), m_alt(0)
1186 wxGridSizeEvent(int id
, wxEventType type
, wxObject
* obj
,
1187 int rowOrCol
=-1, int x
=-1, int y
=-1,
1188 bool control
=FALSE
, bool shift
=FALSE
, bool alt
=FALSE
, bool meta
=FALSE
);
1190 int GetRowOrCol() { return m_rowOrCol
; }
1191 wxPoint
GetPosition() { return wxPoint( m_x
, m_y
); }
1192 bool ControlDown() { return m_control
; }
1193 bool MetaDown() { return m_meta
; }
1194 bool ShiftDown() { return m_shift
; }
1195 bool AltDown() { return m_alt
; }
1206 DECLARE_DYNAMIC_CLASS(wxGridSizeEvent
)
1210 class WXDLLEXPORT wxGridRangeSelectEvent
: public wxNotifyEvent
1213 wxGridRangeSelectEvent()
1216 m_topLeft
= wxGridNoCellCoords
;
1217 m_bottomRight
= wxGridNoCellCoords
;
1224 wxGridRangeSelectEvent(int id
, wxEventType type
, wxObject
* obj
,
1225 const wxGridCellCoords
& topLeft
,
1226 const wxGridCellCoords
& bottomRight
,
1227 bool control
=FALSE
, bool shift
=FALSE
,
1228 bool alt
=FALSE
, bool meta
=FALSE
);
1230 wxGridCellCoords
GetTopLeftCoords() { return m_topLeft
; }
1231 wxGridCellCoords
GetBottomRightCoords() { return m_bottomRight
; }
1232 int GetTopRow() { return m_topLeft
.GetRow(); }
1233 int GetBottomRow() { return m_bottomRight
.GetRow(); }
1234 int GetLeftCol() { return m_topLeft
.GetCol(); }
1235 int GetRightCol() { return m_bottomRight
.GetCol(); }
1236 bool ControlDown() { return m_control
; }
1237 bool MetaDown() { return m_meta
; }
1238 bool ShiftDown() { return m_shift
; }
1239 bool AltDown() { return m_alt
; }
1242 wxGridCellCoords m_topLeft
;
1243 wxGridCellCoords m_bottomRight
;
1249 DECLARE_DYNAMIC_CLASS(wxGridRangeSelectEvent
)
1253 const wxEventType EVT_GRID_CELL_LEFT_CLICK
= wxEVT_FIRST
+ 1580;
1254 const wxEventType EVT_GRID_CELL_RIGHT_CLICK
= wxEVT_FIRST
+ 1581;
1255 const wxEventType EVT_GRID_CELL_LEFT_DCLICK
= wxEVT_FIRST
+ 1582;
1256 const wxEventType EVT_GRID_CELL_RIGHT_DCLICK
= wxEVT_FIRST
+ 1583;
1257 const wxEventType EVT_GRID_LABEL_LEFT_CLICK
= wxEVT_FIRST
+ 1584;
1258 const wxEventType EVT_GRID_LABEL_RIGHT_CLICK
= wxEVT_FIRST
+ 1585;
1259 const wxEventType EVT_GRID_LABEL_LEFT_DCLICK
= wxEVT_FIRST
+ 1586;
1260 const wxEventType EVT_GRID_LABEL_RIGHT_DCLICK
= wxEVT_FIRST
+ 1587;
1261 const wxEventType EVT_GRID_ROW_SIZE
= wxEVT_FIRST
+ 1588;
1262 const wxEventType EVT_GRID_COL_SIZE
= wxEVT_FIRST
+ 1589;
1263 const wxEventType EVT_GRID_RANGE_SELECT
= wxEVT_FIRST
+ 1590;
1264 const wxEventType EVT_GRID_CELL_CHANGE
= wxEVT_FIRST
+ 1591;
1265 const wxEventType EVT_GRID_SELECT_CELL
= wxEVT_FIRST
+ 1592;
1268 typedef void (wxEvtHandler
::*wxGridEventFunction
)(wxGridEvent
&);
1269 typedef void (wxEvtHandler
::*wxGridSizeEventFunction
)(wxGridSizeEvent
&);
1270 typedef void (wxEvtHandler
::*wxGridRangeSelectEventFunction
)(wxGridRangeSelectEvent
&);
1272 #define EVT_GRID_CELL_LEFT_CLICK(fn) { EVT_GRID_CELL_LEFT_CLICK, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridEventFunction) &fn, NULL },
1273 #define EVT_GRID_CELL_RIGHT_CLICK(fn) { EVT_GRID_CELL_RIGHT_CLICK, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridEventFunction) &fn, NULL },
1274 #define EVT_GRID_CELL_LEFT_DCLICK(fn) { EVT_GRID_CELL_LEFT_DCLICK, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridEventFunction) &fn, NULL },
1275 #define EVT_GRID_CELL_RIGHT_DCLICK(fn) { EVT_GRID_CELL_RIGHT_DCLICK, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridEventFunction) &fn, NULL },
1276 #define EVT_GRID_LABEL_LEFT_CLICK(fn) { EVT_GRID_LABEL_LEFT_CLICK, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridEventFunction) &fn, NULL },
1277 #define EVT_GRID_LABEL_RIGHT_CLICK(fn) { EVT_GRID_LABEL_RIGHT_CLICK, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridEventFunction) &fn, NULL },
1278 #define EVT_GRID_LABEL_LEFT_DCLICK(fn) { EVT_GRID_LABEL_LEFT_DCLICK, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridEventFunction) &fn, NULL },
1279 #define EVT_GRID_LABEL_RIGHT_DCLICK(fn) { EVT_GRID_LABEL_RIGHT_DCLICK, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridEventFunction) &fn, NULL },
1280 #define EVT_GRID_ROW_SIZE(fn) { EVT_GRID_ROW_SIZE, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridSizeEventFunction) &fn, NULL },
1281 #define EVT_GRID_COL_SIZE(fn) { EVT_GRID_COL_SIZE, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridSizeEventFunction) &fn, NULL },
1282 #define EVT_GRID_RANGE_SELECT(fn) { EVT_GRID_RANGE_SELECT, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridRangeSelectEventFunction) &fn, NULL },
1283 #define EVT_GRID_CELL_CHANGE(fn) { EVT_GRID_CELL_CHANGE, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridEventFunction) &fn, NULL },
1284 #define EVT_GRID_SELECT_CELL(fn) { EVT_GRID_SELECT_CELL, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridEventFunction) &fn, NULL },
1287 #if 0 // TODO: implement these ? others ?
1289 const wxEventType EVT_GRID_CREATE_CELL
= wxEVT_FIRST
+ 1576;
1290 const wxEventType EVT_GRID_CHANGE_LABELS
= wxEVT_FIRST
+ 1577;
1291 const wxEventType EVT_GRID_CHANGE_SEL_LABEL
= wxEVT_FIRST
+ 1578;
1293 #define EVT_GRID_CREATE_CELL(fn) { EVT_GRID_CREATE_CELL, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridEventFunction) &fn, NULL },
1294 #define EVT_GRID_CHANGE_LABELS(fn) { EVT_GRID_CHANGE_LABELS, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridEventFunction) &fn, NULL },
1295 #define EVT_GRID_CHANGE_SEL_LABEL(fn) { EVT_GRID_CHANGE_SEL_LABEL, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridEventFunction) &fn, NULL },
1299 #endif // #ifndef __WXGRID_H__
1301 #endif // ifndef wxUSE_NEW_GRID