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 wxGridCellAttrProviderData
;
58 class WXDLLEXPORT wxGridRowLabelWindow
;
59 class WXDLLEXPORT wxGridColLabelWindow
;
60 class WXDLLEXPORT wxGridCornerLabelWindow
;
61 class WXDLLEXPORT wxGridWindow
;
62 class WXDLLEXPORT wxGrid
;
64 // ----------------------------------------------------------------------------
65 // wxGridCellAttr: this class can be used to alter the cells appearance in
66 // the grid by changing their colour/font/... from default. An object of this
67 // class may be returned by wxGridTable::GetAttr().
68 // ----------------------------------------------------------------------------
70 class WXDLLEXPORT wxGridCellAttr
80 wxGridCellAttr(const wxColour
& colText
,
81 const wxColour
& colBack
,
85 : m_colText(colText
), m_colBack(colBack
), m_font(font
)
88 SetAlignment(hAlign
, vAlign
);
91 // default copy ctor ok
93 // this class is ref counted: it is created with ref count of 1, so
94 // calling DecRef() once will delete it. Calling IncRef() allows to lock
95 // it until the matching DecRef() is called
96 void IncRef() { m_nRef
++; }
97 void DecRef() { if ( !--m_nRef
) delete this; }
98 void SafeDecRef() { if ( this ) DecRef(); }
101 void SetTextColour(const wxColour
& colText
) { m_colText
= colText
; }
102 void SetBackgroundColour(const wxColour
& colBack
) { m_colBack
= colBack
; }
103 void SetFont(const wxFont
& font
) { m_font
= font
; }
104 void SetAlignment(int hAlign
, int vAlign
)
111 bool HasTextColour() const { return m_colText
.Ok(); }
112 bool HasBackgroundColour() const { return m_colBack
.Ok(); }
113 bool HasFont() const { return m_font
.Ok(); }
114 bool HasAlignment() const { return m_hAlign
|| m_vAlign
; }
116 const wxColour
& GetTextColour() const { return m_colText
; }
117 const wxColour
& GetBackgroundColour() const { return m_colBack
; }
118 const wxFont
& GetFont() const { return m_font
; }
119 void GetAlignment(int *hAlign
, int *vAlign
)
121 if ( hAlign
) *hAlign
= m_hAlign
;
122 if ( vAlign
) *vAlign
= m_vAlign
;
126 // the common part of all ctors
127 void Init() { m_nRef
= 1; }
129 // the dtor is private because only DecRef() can delete us
130 ~wxGridCellAttr() { }
132 // the ref count - when it goes to 0, we die
141 // suppress the stupid gcc warning about the class having private dtor and
143 friend class wxGridCellAttrDummyFriend
;
146 // ----------------------------------------------------------------------------
147 // wxGridCellAttrProvider: class used by wxGridTableBase to retrieve/store the
149 // ----------------------------------------------------------------------------
151 // implementation note: we separate it from wxGridTableBase because we wish to
152 // avoid deriving a new table class if possible, and sometimes it will be
153 // enough to just derive another wxGridCellAttrProvider instead
155 class WXDLLEXPORT wxGridCellAttrProvider
158 wxGridCellAttrProvider();
159 virtual ~wxGridCellAttrProvider();
161 // DecRef() must be called on the returned pointer
162 virtual wxGridCellAttr
*GetAttr(int row
, int col
) const;
164 // takes ownership of the pointer, don't call DecRef() on it
165 virtual void SetAttr(wxGridCellAttr
*attr
, int row
, int col
);
170 wxGridCellAttrProviderData
*m_data
;
173 //////////////////////////////////////////////////////////////////////
175 // Grid table classes
177 //////////////////////////////////////////////////////////////////////
180 class WXDLLEXPORT wxGridTableBase
: public wxObject
184 virtual ~wxGridTableBase();
186 // You must override these functions in a derived table class
188 virtual long GetNumberRows() = 0;
189 virtual long GetNumberCols() = 0;
190 virtual wxString
GetValue( int row
, int col
) = 0;
191 virtual void SetValue( int row
, int col
, const wxString
& s
) = 0;
192 virtual bool IsEmptyCell( int row
, int col
) = 0;
194 // Overriding these is optional
196 virtual void SetView( wxGrid
*grid
) { m_view
= grid
; }
197 virtual wxGrid
* GetView() const { return m_view
; }
199 virtual void Clear() {}
200 virtual bool InsertRows( size_t pos
= 0, size_t numRows
= 1 );
201 virtual bool AppendRows( size_t numRows
= 1 );
202 virtual bool DeleteRows( size_t pos
= 0, size_t numRows
= 1 );
203 virtual bool InsertCols( size_t pos
= 0, size_t numCols
= 1 );
204 virtual bool AppendCols( size_t numCols
= 1 );
205 virtual bool DeleteCols( size_t pos
= 0, size_t numCols
= 1 );
207 virtual wxString
GetRowLabelValue( int row
);
208 virtual wxString
GetColLabelValue( int col
);
209 virtual void SetRowLabelValue( int WXUNUSED(row
), const wxString
& ) {}
210 virtual void SetColLabelValue( int WXUNUSED(col
), const wxString
& ) {}
212 // Attribute handling
215 // give us the attr provider to use - we take ownership of the pointer
216 void SetAttrProvider(wxGridCellAttrProvider
*attrProvider
);
218 // get the currently used attr provider (may be NULL)
219 wxGridCellAttrProvider
*GetAttrProvider() const { return m_attrProvider
; }
221 // by default forwarded to wxGridCellAttrProvider if any. May be
222 // overridden to handle attributes directly in this class.
223 virtual wxGridCellAttr
*GetAttr( int row
, int col
);
225 // takes ownership of the pointer
226 virtual void SetAttr(wxGridCellAttr
*attr
, int row
, int col
);
230 wxGridCellAttrProvider
*m_attrProvider
;
232 DECLARE_ABSTRACT_CLASS( wxGridTableBase
);
236 // ----------------------------------------------------------------------------
237 // wxGridTableMessage
238 // ----------------------------------------------------------------------------
240 // IDs for messages sent from grid table to view
242 enum wxGridTableRequest
244 wxGRIDTABLE_REQUEST_VIEW_GET_VALUES
= 2000,
245 wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES
,
246 wxGRIDTABLE_NOTIFY_ROWS_INSERTED
,
247 wxGRIDTABLE_NOTIFY_ROWS_APPENDED
,
248 wxGRIDTABLE_NOTIFY_ROWS_DELETED
,
249 wxGRIDTABLE_NOTIFY_COLS_INSERTED
,
250 wxGRIDTABLE_NOTIFY_COLS_APPENDED
,
251 wxGRIDTABLE_NOTIFY_COLS_DELETED
254 class WXDLLEXPORT wxGridTableMessage
257 wxGridTableMessage();
258 wxGridTableMessage( wxGridTableBase
*table
, int id
,
262 void SetTableObject( wxGridTableBase
*table
) { m_table
= table
; }
263 wxGridTableBase
* GetTableObject() const { return m_table
; }
264 void SetId( int id
) { m_id
= id
; }
265 int GetId() { return m_id
; }
266 void SetCommandInt( int comInt1
) { m_comInt1
= comInt1
; }
267 int GetCommandInt() { return m_comInt1
; }
268 void SetCommandInt2( int comInt2
) { m_comInt2
= comInt2
; }
269 int GetCommandInt2() { return m_comInt2
; }
272 wxGridTableBase
*m_table
;
280 // ------ wxGridStringArray
281 // A 2-dimensional array of strings for data values
284 WX_DECLARE_EXPORTED_OBJARRAY(wxArrayString
, wxGridStringArray
);
288 // ------ wxGridStringTable
290 // Simplest type of data table for a grid for small tables of strings
291 // that are stored in memory
294 class WXDLLEXPORT wxGridStringTable
: public wxGridTableBase
298 wxGridStringTable( int numRows
, int numCols
);
299 ~wxGridStringTable();
301 // these are pure virtual in wxGridTableBase
303 long GetNumberRows();
304 long GetNumberCols();
305 wxString
GetValue( int row
, int col
);
306 void SetValue( int row
, int col
, const wxString
& s
);
307 bool IsEmptyCell( int row
, int col
);
309 // overridden functions from wxGridTableBase
312 bool InsertRows( size_t pos
= 0, size_t numRows
= 1 );
313 bool AppendRows( size_t numRows
= 1 );
314 bool DeleteRows( size_t pos
= 0, size_t numRows
= 1 );
315 bool InsertCols( size_t pos
= 0, size_t numCols
= 1 );
316 bool AppendCols( size_t numCols
= 1 );
317 bool DeleteCols( size_t pos
= 0, size_t numCols
= 1 );
319 void SetRowLabelValue( int row
, const wxString
& );
320 void SetColLabelValue( int col
, const wxString
& );
321 wxString
GetRowLabelValue( int row
);
322 wxString
GetColLabelValue( int col
);
325 wxGridStringArray m_data
;
327 // These only get used if you set your own labels, otherwise the
328 // GetRow/ColLabelValue functions return wxGridTableBase defaults
330 wxArrayString m_rowLabels
;
331 wxArrayString m_colLabels
;
333 DECLARE_DYNAMIC_CLASS( wxGridStringTable
)
338 //////////////////////////////////////////////////////////////////////
342 //////////////////////////////////////////////////////////////////////
344 class WXDLLEXPORT wxGridCellCoords
347 wxGridCellCoords() { m_row
= m_col
= -1; }
348 wxGridCellCoords( int r
, int c
) { m_row
= r
; m_col
= c
; }
350 // default copy ctor is ok
352 long GetRow() const { return m_row
; }
353 void SetRow( long n
) { m_row
= n
; }
354 long GetCol() const { return m_col
; }
355 void SetCol( long n
) { m_col
= n
; }
356 void Set( long row
, long col
) { m_row
= row
; m_col
= col
; }
358 wxGridCellCoords
& operator=( const wxGridCellCoords
& other
)
360 if ( &other
!= this )
368 bool operator==( const wxGridCellCoords
& other
)
370 return (m_row
== other
.m_row
&& m_col
== other
.m_col
);
373 bool operator!=( const wxGridCellCoords
& other
)
375 return (m_row
!= other
.m_row
|| m_col
!= other
.m_col
);
380 return (m_row
== -1 && m_col
== -1 );
389 // For comparisons...
391 extern wxGridCellCoords wxGridNoCellCoords
;
392 extern wxRect wxGridNoCellRect
;
394 // An array of cell coords...
396 WX_DECLARE_EXPORTED_OBJARRAY(wxGridCellCoords
, wxGridCellCoordsArray
);
400 // This set of classes is to provide for the use of different types of
401 // cell edit controls in the grid while avoiding the wx class info
402 // system in deference to wxPython
404 class WXDLLEXPORT wxGridTextCtrl
: public wxTextCtrl
408 wxGridTextCtrl( wxWindow
*,
412 const wxString
& value
= wxEmptyString
,
413 const wxPoint
& pos
= wxDefaultPosition
,
414 const wxSize
& size
= wxDefaultSize
,
417 void SetStartValue( const wxString
& );
418 wxString
GetStartValue() { return startValue
; }
423 // TRUE for controls placed over cells,
424 // FALSE for a control on a grid control panel
425 bool m_isCellControl
;
429 void OnKeyDown( wxKeyEvent
& );
431 DECLARE_DYNAMIC_CLASS( wxGridTextCtrl
)
432 DECLARE_EVENT_TABLE()
435 // ----------------------------------------------------------------------------
437 // ----------------------------------------------------------------------------
439 class WXDLLEXPORT wxGrid
: public wxScrolledWindow
444 m_table
= (wxGridTableBase
*) NULL
;
445 m_gridWin
= (wxGridWindow
*) NULL
;
446 m_rowLabelWin
= (wxGridRowLabelWindow
*) NULL
;
447 m_colLabelWin
= (wxGridColLabelWindow
*) NULL
;
448 m_cornerLabelWin
= (wxGridCornerLabelWindow
*) NULL
;
449 m_cellEditCtrl
= (wxWindow
*) NULL
;
452 wxGrid( wxWindow
*parent
,
454 const wxPoint
& pos
= wxDefaultPosition
,
455 const wxSize
& size
= wxDefaultSize
,
457 const wxString
& name
= wxPanelNameStr
);
461 bool CreateGrid( int numRows
, int numCols
);
464 // ------ grid dimensions
466 int GetNumberRows() { return m_numRows
; }
467 int GetNumberCols() { return m_numCols
; }
470 // ------ display update functions
472 void CalcRowLabelsExposed( wxRegion
& reg
);
474 void CalcColLabelsExposed( wxRegion
& reg
);
475 void CalcCellsExposed( wxRegion
& reg
);
478 // ------ event handlers
480 void ProcessRowLabelMouseEvent( wxMouseEvent
& event
);
481 void ProcessColLabelMouseEvent( wxMouseEvent
& event
);
482 void ProcessCornerLabelMouseEvent( wxMouseEvent
& event
);
483 void ProcessGridCellMouseEvent( wxMouseEvent
& event
);
484 bool ProcessTableMessage( wxGridTableMessage
& );
486 void DoEndDragResizeRow();
487 void DoEndDragResizeCol();
489 wxGridTableBase
* GetTable() const { return m_table
; }
490 void SetTable( wxGridTableBase
*table
) { m_table
= table
; }
493 bool InsertRows( int pos
= 0, int numRows
= 1, bool updateLabels
=TRUE
);
494 bool AppendRows( int numRows
= 1, bool updateLabels
=TRUE
);
495 bool DeleteRows( int pos
= 0, int numRows
= 1, bool updateLabels
=TRUE
);
496 bool InsertCols( int pos
= 0, int numCols
= 1, bool updateLabels
=TRUE
);
497 bool AppendCols( int numCols
= 1, bool updateLabels
=TRUE
);
498 bool DeleteCols( int pos
= 0, int numCols
= 1, bool updateLabels
=TRUE
);
500 void DrawGridCellArea( wxDC
& dc
);
501 void DrawCellBorder( wxDC
& dc
, const wxGridCellCoords
& );
502 void DrawAllGridLines( wxDC
& dc
, const wxRegion
& reg
);
503 void DrawCell( wxDC
& dc
, const wxGridCellCoords
& );
504 void DrawCellBackground( wxDC
& dc
, const wxGridCellCoords
& );
505 void DrawCellValue( wxDC
& dc
, const wxGridCellCoords
& );
507 void DrawRowLabels( wxDC
& dc
);
508 void DrawRowLabel( wxDC
& dc
, int row
);
510 void DrawColLabels( wxDC
& dc
);
511 void DrawColLabel( wxDC
& dc
, int col
);
514 // ------ Cell text drawing functions
516 void DrawTextRectangle( wxDC
& dc
, const wxString
&, const wxRect
&,
517 int horizontalAlignment
= wxLEFT
,
518 int verticalAlignment
= wxTOP
);
520 // Split a string containing newline chararcters into an array of
521 // strings and return the number of lines
523 void StringToLines( const wxString
& value
, wxArrayString
& lines
);
525 void GetTextBoxSize( wxDC
& dc
,
526 wxArrayString
& lines
,
527 long *width
, long *height
);
531 // Code that does a lot of grid modification can be enclosed
532 // between BeginBatch() and EndBatch() calls to avoid screen
535 void BeginBatch() { m_batchCount
++; }
536 void EndBatch() { if ( m_batchCount
> 0 ) m_batchCount
--; }
537 int GetBatchCount() { return m_batchCount
; }
540 // ------ edit control functions
542 bool IsEditable() { return m_editable
; }
543 void EnableEditing( bool edit
);
545 #if 0 // at the moment the cell edit control is always active
546 void EnableCellEditControl( bool enable
);
549 bool IsCellEditControlEnabled()
550 { return (m_cellEditCtrl
&& m_cellEditCtrlEnabled
); }
552 void ShowCellEditControl();
553 void HideCellEditControl();
554 void SetEditControlValue( const wxString
& s
= wxEmptyString
);
555 void SaveEditControlValue();
558 // ------ grid location functions
559 // Note that all of these functions work with the logical coordinates of
560 // grid cells and labels so you will need to convert from device
561 // coordinates for mouse events etc.
563 void XYToCell( int x
, int y
, wxGridCellCoords
& );
567 int YToEdgeOfRow( int y
);
568 int XToEdgeOfCol( int x
);
570 wxRect
CellToRect( int row
, int col
);
571 wxRect
CellToRect( const wxGridCellCoords
& coords
)
572 { return CellToRect( coords
.GetRow(), coords
.GetCol() ); }
574 int GetGridCursorRow() { return m_currentCellCoords
.GetRow(); }
575 int GetGridCursorCol() { return m_currentCellCoords
.GetCol(); }
577 // check to see if a cell is either wholly visible (the default arg) or
578 // at least partially visible in the grid window
580 bool IsVisible( int row
, int col
, bool wholeCellVisible
= TRUE
);
581 bool IsVisible( const wxGridCellCoords
& coords
, bool wholeCellVisible
= TRUE
)
582 { return IsVisible( coords
.GetRow(), coords
.GetCol(), wholeCellVisible
); }
583 void MakeCellVisible( int row
, int col
);
584 void MakeCellVisible( const wxGridCellCoords
& coords
)
585 { MakeCellVisible( coords
.GetRow(), coords
.GetCol() ); }
588 // ------ grid cursor movement functions
590 void SetGridCursor( int row
, int col
)
591 { SetCurrentCell( wxGridCellCoords(row
, col
) ); }
594 bool MoveCursorDown();
595 bool MoveCursorLeft();
596 bool MoveCursorRight();
599 bool MoveCursorUpBlock();
600 bool MoveCursorDownBlock();
601 bool MoveCursorLeftBlock();
602 bool MoveCursorRightBlock();
605 // ------ label and gridline formatting
607 int GetDefaultRowLabelSize() { return WXGRID_DEFAULT_ROW_LABEL_WIDTH
; }
608 int GetRowLabelSize() { return m_rowLabelWidth
; }
609 int GetDefaultColLabelSize() { return WXGRID_DEFAULT_COL_LABEL_HEIGHT
; }
610 int GetColLabelSize() { return m_colLabelHeight
; }
611 wxColour
GetLabelBackgroundColour() { return m_labelBackgroundColour
; }
612 wxColour
GetLabelTextColour() { return m_labelTextColour
; }
613 wxFont
GetLabelFont() { return m_labelFont
; }
614 void GetRowLabelAlignment( int *horiz
, int *vert
);
615 void GetColLabelAlignment( int *horiz
, int *vert
);
616 wxString
GetRowLabelValue( int row
);
617 wxString
GetColLabelValue( int col
);
618 wxColour
GetGridLineColour() { return m_gridLineColour
; }
620 void SetRowLabelSize( int width
);
621 void SetColLabelSize( int height
);
622 void SetLabelBackgroundColour( const wxColour
& );
623 void SetLabelTextColour( const wxColour
& );
624 void SetLabelFont( const wxFont
& );
625 void SetRowLabelAlignment( int horiz
, int vert
);
626 void SetColLabelAlignment( int horiz
, int vert
);
627 void SetRowLabelValue( int row
, const wxString
& );
628 void SetColLabelValue( int col
, const wxString
& );
629 void SetGridLineColour( const wxColour
& );
631 void EnableGridLines( bool enable
= TRUE
);
632 bool GridLinesEnabled() { return m_gridLinesEnabled
; }
635 // ------ row and col formatting
637 int GetDefaultRowSize();
638 int GetRowSize( int row
);
639 int GetDefaultColSize();
640 int GetColSize( int col
);
641 wxColour
GetDefaultCellBackgroundColour();
642 wxColour
GetCellBackgroundColour( int row
, int col
);
643 wxColour
GetDefaultCellTextColour();
644 wxColour
GetCellTextColour( int row
, int col
);
645 wxFont
GetDefaultCellFont();
646 wxFont
GetCellFont( int row
, int col
);
647 void GetDefaultCellAlignment( int *horiz
, int *vert
);
648 void GetCellAlignment( int row
, int col
, int *horiz
, int *vert
);
650 void SetDefaultRowSize( int height
, bool resizeExistingRows
= FALSE
);
651 void SetRowSize( int row
, int height
);
652 void SetDefaultColSize( int width
, bool resizeExistingCols
= FALSE
);
654 void SetColSize( int col
, int width
);
655 void SetDefaultCellBackgroundColour( const wxColour
& );
656 void SetCellBackgroundColour( int row
, int col
, const wxColour
& );
657 void SetDefaultCellTextColour( const wxColour
& );
659 void SetCellTextColour( int row
, int col
, const wxColour
& );
660 void SetDefaultCellFont( const wxFont
& );
661 void SetCellFont( int row
, int col
, const wxFont
& );
662 void SetDefaultCellAlignment( int horiz
, int vert
);
663 void SetCellAlignment( int row
, int col
, int horiz
, int vert
);
666 // ------ cell value accessors
668 wxString
GetCellValue( int row
, int col
)
672 return m_table
->GetValue( row
, col
);
676 return wxEmptyString
;
680 wxString
GetCellValue( const wxGridCellCoords
& coords
)
681 { return GetCellValue( coords
.GetRow(), coords
.GetCol() ); }
683 void SetCellValue( int row
, int col
, const wxString
& s
);
684 void SetCellValue( const wxGridCellCoords
& coords
, const wxString
& s
)
685 { SetCellValue( coords
.GetRow(), coords
.GetCol(), s
); }
689 // ------ selections of blocks of cells
691 void SelectRow( int row
, bool addToSelected
= FALSE
);
692 void SelectCol( int col
, bool addToSelected
= FALSE
);
694 void SelectBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
);
696 void SelectBlock( const wxGridCellCoords
& topLeft
,
697 const wxGridCellCoords
& bottomRight
)
698 { SelectBlock( topLeft
.GetRow(), topLeft
.GetCol(),
699 bottomRight
.GetRow(), bottomRight
.GetCol() ); }
704 { return ( m_selectedTopLeft
!= wxGridNoCellCoords
&&
705 m_selectedBottomRight
!= wxGridNoCellCoords
);
708 void ClearSelection();
710 bool IsInSelection( int row
, int col
)
711 { return ( IsSelection() &&
712 row
>= m_selectedTopLeft
.GetRow() &&
713 col
>= m_selectedTopLeft
.GetCol() &&
714 row
<= m_selectedBottomRight
.GetRow() &&
715 col
<= m_selectedBottomRight
.GetCol() );
718 bool IsInSelection( const wxGridCellCoords
& coords
)
719 { return IsInSelection( coords
.GetRow(), coords
.GetCol() ); }
721 void GetSelection( int* topRow
, int* leftCol
, int* bottomRow
, int* rightCol
)
723 // these will all be -1 if there is no selected block
725 *topRow
= m_selectedTopLeft
.GetRow();
726 *leftCol
= m_selectedTopLeft
.GetCol();
727 *bottomRow
= m_selectedBottomRight
.GetRow();
728 *rightCol
= m_selectedBottomRight
.GetCol();
732 // This function returns the rectangle that encloses the block of cells
733 // limited by TopLeft and BottomRight cell in device coords and clipped
734 // to the client size of the grid window.
736 wxRect
BlockToDeviceRect( const wxGridCellCoords
& topLeft
,
737 const wxGridCellCoords
& bottomRight
);
739 // This function returns the rectangle that encloses the selected cells
740 // in device coords and clipped to the client size of the grid window.
742 wxRect
SelectionToDeviceRect()
744 return BlockToDeviceRect( m_selectedTopLeft
,
745 m_selectedBottomRight
);
749 // ------ For compatibility with previous wxGrid only...
751 // ************************************************
752 // ** Don't use these in new code because they **
753 // ** are liable to disappear in a future **
755 // ************************************************
758 wxGrid( wxWindow
*parent
,
759 int x
= -1, int y
= -1, int w
= -1, int h
= -1,
761 const wxString
& name
= wxPanelNameStr
)
762 : wxScrolledWindow( parent
, -1, wxPoint(x
,y
), wxSize(w
,h
), style
, name
)
767 void SetCellValue( const wxString
& val
, int row
, int col
)
768 { SetCellValue( row
, col
, val
); }
770 void UpdateDimensions()
771 { CalcDimensions(); }
773 int GetRows() { return GetNumberRows(); }
774 int GetCols() { return GetNumberCols(); }
775 int GetCursorRow() { return GetGridCursorRow(); }
776 int GetCursorColumn() { return GetGridCursorCol(); }
778 int GetScrollPosX() { return 0; }
779 int GetScrollPosY() { return 0; }
781 void SetScrollX( int x
) { }
782 void SetScrollY( int y
) { }
784 void SetColumnWidth( int col
, int width
)
785 { SetColSize( col
, width
); }
787 int GetColumnWidth( int col
)
788 { return GetColSize( col
); }
790 void SetRowHeight( int row
, int height
)
791 { SetRowSize( row
, height
); }
793 int GetRowHeight( int row
)
794 { return GetRowSize( row
); }
796 int GetViewHeight() // returned num whole rows visible
799 int GetViewWidth() // returned num whole cols visible
802 void SetLabelSize( int orientation
, int sz
)
804 if ( orientation
== wxHORIZONTAL
)
805 SetColLabelSize( sz
);
807 SetRowLabelSize( sz
);
810 int GetLabelSize( int orientation
)
812 if ( orientation
== wxHORIZONTAL
)
813 return GetColLabelSize();
815 return GetRowLabelSize();
818 void SetLabelAlignment( int orientation
, int align
)
820 if ( orientation
== wxHORIZONTAL
)
821 SetColLabelAlignment( align
, -1 );
823 SetRowLabelAlignment( align
, -1 );
826 int GetLabelAlignment( int orientation
, int WXUNUSED(align
) )
829 if ( orientation
== wxHORIZONTAL
)
831 GetColLabelAlignment( &h
, &v
);
836 GetRowLabelAlignment( &h
, &v
);
841 void SetLabelValue( int orientation
, const wxString
& val
, int pos
)
843 if ( orientation
== wxHORIZONTAL
)
844 SetColLabelValue( pos
, val
);
846 SetRowLabelValue( pos
, val
);
849 wxString
GetLabelValue( int orientation
, int pos
)
851 if ( orientation
== wxHORIZONTAL
)
852 return GetColLabelValue( pos
);
854 return GetRowLabelValue( pos
);
857 wxFont
GetCellTextFont() const
858 { return m_defaultCellFont
; }
860 wxFont
GetCellTextFont(int WXUNUSED(row
), int WXUNUSED(col
)) const
861 { return m_defaultCellFont
; }
863 void SetCellTextFont(const wxFont
& fnt
)
864 { SetDefaultCellFont( fnt
); }
866 void SetCellTextFont(const wxFont
& fnt
, int row
, int col
)
867 { SetCellFont( row
, col
, fnt
); }
869 void SetCellTextColour(const wxColour
& val
, int row
, int col
)
870 { SetCellTextColour( row
, col
, val
); }
872 void SetCellTextColour(const wxColour
& col
)
873 { SetDefaultCellTextColour( col
); }
875 void SetCellBackgroundColour(const wxColour
& col
)
876 { SetDefaultCellBackgroundColour( col
); }
878 void SetCellBackgroundColour(const wxColour
& colour
, int row
, int col
)
879 { SetCellBackgroundColour( row
, col
, colour
); }
881 bool GetEditable() { return IsEditable(); }
882 void SetEditable( bool edit
= TRUE
) { EnableEditing( edit
); }
883 bool GetEditInPlace() { return IsCellEditControlEnabled(); }
885 void SetEditInPlace(bool edit
= TRUE
) { }
887 void SetCellAlignment( int align
, int row
, int col
)
888 { SetCellAlignment(row
, col
, align
, wxCENTER
); }
889 void SetCellAlignment( int WXUNUSED(align
) ) {}
890 void SetCellBitmap(wxBitmap
*WXUNUSED(bitmap
), int WXUNUSED(row
), int WXUNUSED(col
))
892 void SetDividerPen(const wxPen
& WXUNUSED(pen
)) { }
893 wxPen
& GetDividerPen() const { return wxNullPen
; }
894 void OnActivate(bool WXUNUSED(active
)) {}
896 // ******** End of compatibility functions **********
900 // ------ control IDs
901 enum { wxGRID_CELLCTRL
= 2000,
904 // ------ control types
905 enum { wxGRID_TEXTCTRL
= 2100,
914 wxGridWindow
*m_gridWin
;
915 wxGridRowLabelWindow
*m_rowLabelWin
;
916 wxGridColLabelWindow
*m_colLabelWin
;
917 wxGridCornerLabelWindow
*m_cornerLabelWin
;
919 wxGridTableBase
*m_table
;
929 wxGridCellCoords m_currentCellCoords
;
931 wxGridCellCoords m_selectedTopLeft
;
932 wxGridCellCoords m_selectedBottomRight
;
934 int m_defaultRowHeight
;
935 wxArrayInt m_rowHeights
;
936 wxArrayInt m_rowBottoms
;
938 int m_defaultColWidth
;
939 wxArrayInt m_colWidths
;
940 wxArrayInt m_colRights
;
943 int m_colLabelHeight
;
945 wxColour m_labelBackgroundColour
;
946 wxColour m_labelTextColour
;
949 int m_rowLabelHorizAlign
;
950 int m_rowLabelVertAlign
;
951 int m_colLabelHorizAlign
;
952 int m_colLabelVertAlign
;
954 bool m_defaultRowLabelValues
;
955 bool m_defaultColLabelValues
;
957 wxColour m_gridLineColour
;
958 bool m_gridLinesEnabled
;
960 // default cell attributes
961 wxFont m_defaultCellFont
;
962 int m_defaultCellHAlign
,
965 // do we have some place to store attributes in?
966 bool CanHaveAttributes();
968 // returns the attribute we may modify in place: a new one if this cell
969 // doesn't have any yet or the existing one if it does
971 // DecRef() must be called on the returned pointer, as usual
972 wxGridCellAttr
*GetCellAttr(int row
, int col
) const;
974 wxGridCellCoordsArray m_cellsExposed
;
975 wxArrayInt m_rowsExposed
;
976 wxArrayInt m_colsExposed
;
977 wxArrayInt m_rowLabelsExposed
;
978 wxArrayInt m_colLabelsExposed
;
985 WXGRID_CURSOR_SELECT_CELL
,
986 WXGRID_CURSOR_RESIZE_ROW
,
987 WXGRID_CURSOR_RESIZE_COL
,
988 WXGRID_CURSOR_SELECT_ROW
,
989 WXGRID_CURSOR_SELECT_COL
992 // this method not only sets m_cursorMode but also sets the correct cursor
993 // for the given mode and, if captureMouse is not FALSE releases the mouse
994 // if it was captured and captures it if it must be captured
996 // for this to work, you should always use it and not set m_cursorMode
998 void ChangeCursorMode(CursorMode mode
,
999 wxWindow
*win
= (wxWindow
*)NULL
,
1000 bool captureMouse
= TRUE
);
1002 wxWindow
*m_winCapture
; // the window which captured the mouse
1003 CursorMode m_cursorMode
;
1009 wxGridCellCoords m_selectionStart
;
1011 wxCursor m_rowResizeCursor
;
1012 wxCursor m_colResizeCursor
;
1014 bool m_editable
; // applies to whole grid
1015 int m_editCtrlType
; // for current cell
1016 wxWindow
* m_cellEditCtrl
;
1017 bool m_cellEditCtrlEnabled
;
1022 void CalcDimensions();
1023 void CalcWindowSizes();
1024 bool Redimension( wxGridTableMessage
& );
1027 bool SendEvent( const wxEventType
,
1031 bool SendEvent( const wxEventType
,
1035 void OnPaint( wxPaintEvent
& );
1036 void OnSize( wxSizeEvent
& );
1037 void OnKeyDown( wxKeyEvent
& );
1040 void SetCurrentCell( const wxGridCellCoords
& coords
);
1041 void SetCurrentCell( int row
, int col
)
1042 { SetCurrentCell( wxGridCellCoords(row
, col
) ); }
1045 // ------ functions to get/send data (see also public functions)
1047 bool GetModelValues();
1048 bool SetModelValues();
1051 DECLARE_DYNAMIC_CLASS( wxGrid
)
1052 DECLARE_EVENT_TABLE()
1060 // ------ Grid event class and event types
1063 class WXDLLEXPORT wxGridEvent
: public wxNotifyEvent
1067 : wxNotifyEvent(), m_row(-1), m_col(-1), m_x(-1), m_y(-1),
1068 m_control(0), m_meta(0), m_shift(0), m_alt(0)
1072 wxGridEvent(int id
, wxEventType type
, wxObject
* obj
,
1073 int row
=-1, int col
=-1, int x
=-1, int y
=-1,
1074 bool control
=FALSE
, bool shift
=FALSE
, bool alt
=FALSE
, bool meta
=FALSE
);
1076 virtual int GetRow() { return m_row
; }
1077 virtual int GetCol() { return m_col
; }
1078 wxPoint
GetPosition() { return wxPoint( m_x
, m_y
); }
1079 bool ControlDown() { return m_control
; }
1080 bool MetaDown() { return m_meta
; }
1081 bool ShiftDown() { return m_shift
; }
1082 bool AltDown() { return m_alt
; }
1094 DECLARE_DYNAMIC_CLASS(wxGridEvent
)
1098 class WXDLLEXPORT wxGridSizeEvent
: public wxNotifyEvent
1102 : wxNotifyEvent(), m_rowOrCol(-1), m_x(-1), m_y(-1),
1103 m_control(0), m_meta(0), m_shift(0), m_alt(0)
1107 wxGridSizeEvent(int id
, wxEventType type
, wxObject
* obj
,
1108 int rowOrCol
=-1, int x
=-1, int y
=-1,
1109 bool control
=FALSE
, bool shift
=FALSE
, bool alt
=FALSE
, bool meta
=FALSE
);
1111 int GetRowOrCol() { return m_rowOrCol
; }
1112 wxPoint
GetPosition() { return wxPoint( m_x
, m_y
); }
1113 bool ControlDown() { return m_control
; }
1114 bool MetaDown() { return m_meta
; }
1115 bool ShiftDown() { return m_shift
; }
1116 bool AltDown() { return m_alt
; }
1127 DECLARE_DYNAMIC_CLASS(wxGridSizeEvent
)
1131 class WXDLLEXPORT wxGridRangeSelectEvent
: public wxNotifyEvent
1134 wxGridRangeSelectEvent()
1137 m_topLeft
= wxGridNoCellCoords
;
1138 m_bottomRight
= wxGridNoCellCoords
;
1145 wxGridRangeSelectEvent(int id
, wxEventType type
, wxObject
* obj
,
1146 const wxGridCellCoords
& topLeft
,
1147 const wxGridCellCoords
& bottomRight
,
1148 bool control
=FALSE
, bool shift
=FALSE
,
1149 bool alt
=FALSE
, bool meta
=FALSE
);
1151 wxGridCellCoords
GetTopLeftCoords() { return m_topLeft
; }
1152 wxGridCellCoords
GetBottomRightCoords() { return m_bottomRight
; }
1153 int GetTopRow() { return m_topLeft
.GetRow(); }
1154 int GetBottomRow() { return m_bottomRight
.GetRow(); }
1155 int GetLeftCol() { return m_topLeft
.GetCol(); }
1156 int GetRightCol() { return m_bottomRight
.GetCol(); }
1157 bool ControlDown() { return m_control
; }
1158 bool MetaDown() { return m_meta
; }
1159 bool ShiftDown() { return m_shift
; }
1160 bool AltDown() { return m_alt
; }
1163 wxGridCellCoords m_topLeft
;
1164 wxGridCellCoords m_bottomRight
;
1170 DECLARE_DYNAMIC_CLASS(wxGridRangeSelectEvent
)
1174 const wxEventType EVT_GRID_CELL_LEFT_CLICK
= wxEVT_FIRST
+ 1580;
1175 const wxEventType EVT_GRID_CELL_RIGHT_CLICK
= wxEVT_FIRST
+ 1581;
1176 const wxEventType EVT_GRID_CELL_LEFT_DCLICK
= wxEVT_FIRST
+ 1582;
1177 const wxEventType EVT_GRID_CELL_RIGHT_DCLICK
= wxEVT_FIRST
+ 1583;
1178 const wxEventType EVT_GRID_LABEL_LEFT_CLICK
= wxEVT_FIRST
+ 1584;
1179 const wxEventType EVT_GRID_LABEL_RIGHT_CLICK
= wxEVT_FIRST
+ 1585;
1180 const wxEventType EVT_GRID_LABEL_LEFT_DCLICK
= wxEVT_FIRST
+ 1586;
1181 const wxEventType EVT_GRID_LABEL_RIGHT_DCLICK
= wxEVT_FIRST
+ 1587;
1182 const wxEventType EVT_GRID_ROW_SIZE
= wxEVT_FIRST
+ 1588;
1183 const wxEventType EVT_GRID_COL_SIZE
= wxEVT_FIRST
+ 1589;
1184 const wxEventType EVT_GRID_RANGE_SELECT
= wxEVT_FIRST
+ 1590;
1185 const wxEventType EVT_GRID_CELL_CHANGE
= wxEVT_FIRST
+ 1591;
1186 const wxEventType EVT_GRID_SELECT_CELL
= wxEVT_FIRST
+ 1592;
1189 typedef void (wxEvtHandler
::*wxGridEventFunction
)(wxGridEvent
&);
1190 typedef void (wxEvtHandler
::*wxGridSizeEventFunction
)(wxGridSizeEvent
&);
1191 typedef void (wxEvtHandler
::*wxGridRangeSelectEventFunction
)(wxGridRangeSelectEvent
&);
1193 #define EVT_GRID_CELL_LEFT_CLICK(fn) { EVT_GRID_CELL_LEFT_CLICK, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridEventFunction) &fn, NULL },
1194 #define EVT_GRID_CELL_RIGHT_CLICK(fn) { EVT_GRID_CELL_RIGHT_CLICK, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridEventFunction) &fn, NULL },
1195 #define EVT_GRID_CELL_LEFT_DCLICK(fn) { EVT_GRID_CELL_LEFT_DCLICK, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridEventFunction) &fn, NULL },
1196 #define EVT_GRID_CELL_RIGHT_DCLICK(fn) { EVT_GRID_CELL_RIGHT_DCLICK, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridEventFunction) &fn, NULL },
1197 #define EVT_GRID_LABEL_LEFT_CLICK(fn) { EVT_GRID_LABEL_LEFT_CLICK, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridEventFunction) &fn, NULL },
1198 #define EVT_GRID_LABEL_RIGHT_CLICK(fn) { EVT_GRID_LABEL_RIGHT_CLICK, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridEventFunction) &fn, NULL },
1199 #define EVT_GRID_LABEL_LEFT_DCLICK(fn) { EVT_GRID_LABEL_LEFT_DCLICK, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridEventFunction) &fn, NULL },
1200 #define EVT_GRID_LABEL_RIGHT_DCLICK(fn) { EVT_GRID_LABEL_RIGHT_DCLICK, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridEventFunction) &fn, NULL },
1201 #define EVT_GRID_ROW_SIZE(fn) { EVT_GRID_ROW_SIZE, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridSizeEventFunction) &fn, NULL },
1202 #define EVT_GRID_COL_SIZE(fn) { EVT_GRID_COL_SIZE, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridSizeEventFunction) &fn, NULL },
1203 #define EVT_GRID_RANGE_SELECT(fn) { EVT_GRID_RANGE_SELECT, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridRangeSelectEventFunction) &fn, NULL },
1204 #define EVT_GRID_CELL_CHANGE(fn) { EVT_GRID_CELL_CHANGE, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridEventFunction) &fn, NULL },
1205 #define EVT_GRID_SELECT_CELL(fn) { EVT_GRID_SELECT_CELL, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridEventFunction) &fn, NULL },
1208 #if 0 // TODO: implement these ? others ?
1210 const wxEventType EVT_GRID_CREATE_CELL
= wxEVT_FIRST
+ 1576;
1211 const wxEventType EVT_GRID_CHANGE_LABELS
= wxEVT_FIRST
+ 1577;
1212 const wxEventType EVT_GRID_CHANGE_SEL_LABEL
= wxEVT_FIRST
+ 1578;
1214 #define EVT_GRID_CREATE_CELL(fn) { EVT_GRID_CREATE_CELL, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridEventFunction) &fn, NULL },
1215 #define EVT_GRID_CHANGE_LABELS(fn) { EVT_GRID_CHANGE_LABELS, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridEventFunction) &fn, NULL },
1216 #define EVT_GRID_CHANGE_SEL_LABEL(fn) { EVT_GRID_CHANGE_SEL_LABEL, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridEventFunction) &fn, NULL },
1220 #endif // #ifndef __WXGRID_H__
1222 #endif // ifndef wxUSE_NEW_GRID