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 // the default implementation is reasonably efficient for the generic case,
204 // but you might still wish to implement your own for some specific situations
205 // if you have performance problems with the stock one
206 class WXDLLEXPORT wxGridCellAttrProvider
209 wxGridCellAttrProvider();
210 virtual ~wxGridCellAttrProvider();
212 // DecRef() must be called on the returned pointer
213 virtual wxGridCellAttr
*GetAttr(int row
, int col
) const;
215 // all these functions take ownership of the pointer, don't call DecRef()
217 virtual void SetAttr(wxGridCellAttr
*attr
, int row
, int col
);
218 virtual void SetRowAttr(wxGridCellAttr
*attr
, int row
);
219 virtual void SetColAttr(wxGridCellAttr
*attr
, int col
);
224 wxGridCellAttrProviderData
*m_data
;
227 //////////////////////////////////////////////////////////////////////
229 // Grid table classes
231 //////////////////////////////////////////////////////////////////////
234 class WXDLLEXPORT wxGridTableBase
: public wxObject
238 virtual ~wxGridTableBase();
240 // You must override these functions in a derived table class
242 virtual long GetNumberRows() = 0;
243 virtual long GetNumberCols() = 0;
244 virtual wxString
GetValue( int row
, int col
) = 0;
245 virtual void SetValue( int row
, int col
, const wxString
& s
) = 0;
246 virtual bool IsEmptyCell( int row
, int col
) = 0;
248 // Overriding these is optional
250 virtual void SetView( wxGrid
*grid
) { m_view
= grid
; }
251 virtual wxGrid
* GetView() const { return m_view
; }
253 virtual void Clear() {}
254 virtual bool InsertRows( size_t pos
= 0, size_t numRows
= 1 );
255 virtual bool AppendRows( size_t numRows
= 1 );
256 virtual bool DeleteRows( size_t pos
= 0, size_t numRows
= 1 );
257 virtual bool InsertCols( size_t pos
= 0, size_t numCols
= 1 );
258 virtual bool AppendCols( size_t numCols
= 1 );
259 virtual bool DeleteCols( size_t pos
= 0, size_t numCols
= 1 );
261 virtual wxString
GetRowLabelValue( int row
);
262 virtual wxString
GetColLabelValue( int col
);
263 virtual void SetRowLabelValue( int WXUNUSED(row
), const wxString
& ) {}
264 virtual void SetColLabelValue( int WXUNUSED(col
), const wxString
& ) {}
266 // Attribute handling
269 // give us the attr provider to use - we take ownership of the pointer
270 void SetAttrProvider(wxGridCellAttrProvider
*attrProvider
);
272 // get the currently used attr provider (may be NULL)
273 wxGridCellAttrProvider
*GetAttrProvider() const { return m_attrProvider
; }
275 // by default forwarded to wxGridCellAttrProvider if any. May be
276 // overridden to handle attributes directly in this class.
277 virtual wxGridCellAttr
*GetAttr( int row
, int col
);
279 // these functions take ownership of the pointer
280 virtual void SetAttr(wxGridCellAttr
* attr
, int row
, int col
);
281 virtual void SetRowAttr(wxGridCellAttr
*attr
, int row
);
282 virtual void SetColAttr(wxGridCellAttr
*attr
, int col
);
286 wxGridCellAttrProvider
*m_attrProvider
;
288 DECLARE_ABSTRACT_CLASS( wxGridTableBase
);
292 // ----------------------------------------------------------------------------
293 // wxGridTableMessage
294 // ----------------------------------------------------------------------------
296 // IDs for messages sent from grid table to view
298 enum wxGridTableRequest
300 wxGRIDTABLE_REQUEST_VIEW_GET_VALUES
= 2000,
301 wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES
,
302 wxGRIDTABLE_NOTIFY_ROWS_INSERTED
,
303 wxGRIDTABLE_NOTIFY_ROWS_APPENDED
,
304 wxGRIDTABLE_NOTIFY_ROWS_DELETED
,
305 wxGRIDTABLE_NOTIFY_COLS_INSERTED
,
306 wxGRIDTABLE_NOTIFY_COLS_APPENDED
,
307 wxGRIDTABLE_NOTIFY_COLS_DELETED
310 class WXDLLEXPORT wxGridTableMessage
313 wxGridTableMessage();
314 wxGridTableMessage( wxGridTableBase
*table
, int id
,
318 void SetTableObject( wxGridTableBase
*table
) { m_table
= table
; }
319 wxGridTableBase
* GetTableObject() const { return m_table
; }
320 void SetId( int id
) { m_id
= id
; }
321 int GetId() { return m_id
; }
322 void SetCommandInt( int comInt1
) { m_comInt1
= comInt1
; }
323 int GetCommandInt() { return m_comInt1
; }
324 void SetCommandInt2( int comInt2
) { m_comInt2
= comInt2
; }
325 int GetCommandInt2() { return m_comInt2
; }
328 wxGridTableBase
*m_table
;
336 // ------ wxGridStringArray
337 // A 2-dimensional array of strings for data values
340 WX_DECLARE_EXPORTED_OBJARRAY(wxArrayString
, wxGridStringArray
);
344 // ------ wxGridStringTable
346 // Simplest type of data table for a grid for small tables of strings
347 // that are stored in memory
350 class WXDLLEXPORT wxGridStringTable
: public wxGridTableBase
354 wxGridStringTable( int numRows
, int numCols
);
355 ~wxGridStringTable();
357 // these are pure virtual in wxGridTableBase
359 long GetNumberRows();
360 long GetNumberCols();
361 wxString
GetValue( int row
, int col
);
362 void SetValue( int row
, int col
, const wxString
& s
);
363 bool IsEmptyCell( int row
, int col
);
365 // overridden functions from wxGridTableBase
368 bool InsertRows( size_t pos
= 0, size_t numRows
= 1 );
369 bool AppendRows( size_t numRows
= 1 );
370 bool DeleteRows( size_t pos
= 0, size_t numRows
= 1 );
371 bool InsertCols( size_t pos
= 0, size_t numCols
= 1 );
372 bool AppendCols( size_t numCols
= 1 );
373 bool DeleteCols( size_t pos
= 0, size_t numCols
= 1 );
375 void SetRowLabelValue( int row
, const wxString
& );
376 void SetColLabelValue( int col
, const wxString
& );
377 wxString
GetRowLabelValue( int row
);
378 wxString
GetColLabelValue( int col
);
381 wxGridStringArray m_data
;
383 // These only get used if you set your own labels, otherwise the
384 // GetRow/ColLabelValue functions return wxGridTableBase defaults
386 wxArrayString m_rowLabels
;
387 wxArrayString m_colLabels
;
389 DECLARE_DYNAMIC_CLASS( wxGridStringTable
)
394 //////////////////////////////////////////////////////////////////////
398 //////////////////////////////////////////////////////////////////////
400 class WXDLLEXPORT wxGridCellCoords
403 wxGridCellCoords() { m_row
= m_col
= -1; }
404 wxGridCellCoords( int r
, int c
) { m_row
= r
; m_col
= c
; }
406 // default copy ctor is ok
408 long GetRow() const { return m_row
; }
409 void SetRow( long n
) { m_row
= n
; }
410 long GetCol() const { return m_col
; }
411 void SetCol( long n
) { m_col
= n
; }
412 void Set( long row
, long col
) { m_row
= row
; m_col
= col
; }
414 wxGridCellCoords
& operator=( const wxGridCellCoords
& other
)
416 if ( &other
!= this )
424 bool operator==( const wxGridCellCoords
& other
)
426 return (m_row
== other
.m_row
&& m_col
== other
.m_col
);
429 bool operator!=( const wxGridCellCoords
& other
)
431 return (m_row
!= other
.m_row
|| m_col
!= other
.m_col
);
436 return (m_row
== -1 && m_col
== -1 );
445 // For comparisons...
447 extern wxGridCellCoords wxGridNoCellCoords
;
448 extern wxRect wxGridNoCellRect
;
450 // An array of cell coords...
452 WX_DECLARE_EXPORTED_OBJARRAY(wxGridCellCoords
, wxGridCellCoordsArray
);
456 // This set of classes is to provide for the use of different types of
457 // cell edit controls in the grid while avoiding the wx class info
458 // system in deference to wxPython
460 class WXDLLEXPORT wxGridTextCtrl
: public wxTextCtrl
464 wxGridTextCtrl( wxWindow
*,
468 const wxString
& value
= wxEmptyString
,
469 const wxPoint
& pos
= wxDefaultPosition
,
470 const wxSize
& size
= wxDefaultSize
,
473 void SetStartValue( const wxString
& );
474 wxString
GetStartValue() { return startValue
; }
479 // TRUE for controls placed over cells,
480 // FALSE for a control on a grid control panel
481 bool m_isCellControl
;
485 void OnKeyDown( wxKeyEvent
& );
487 DECLARE_DYNAMIC_CLASS( wxGridTextCtrl
)
488 DECLARE_EVENT_TABLE()
491 // ----------------------------------------------------------------------------
493 // ----------------------------------------------------------------------------
495 class WXDLLEXPORT wxGrid
: public wxScrolledWindow
500 m_table
= (wxGridTableBase
*) NULL
;
501 m_gridWin
= (wxGridWindow
*) NULL
;
502 m_rowLabelWin
= (wxGridRowLabelWindow
*) NULL
;
503 m_colLabelWin
= (wxGridColLabelWindow
*) NULL
;
504 m_cornerLabelWin
= (wxGridCornerLabelWindow
*) NULL
;
505 m_cellEditCtrl
= (wxWindow
*) NULL
;
508 wxGrid( wxWindow
*parent
,
510 const wxPoint
& pos
= wxDefaultPosition
,
511 const wxSize
& size
= wxDefaultSize
,
513 const wxString
& name
= wxPanelNameStr
);
517 bool CreateGrid( int numRows
, int numCols
);
520 // ------ grid dimensions
522 int GetNumberRows() { return m_numRows
; }
523 int GetNumberCols() { return m_numCols
; }
526 // ------ display update functions
528 void CalcRowLabelsExposed( wxRegion
& reg
);
530 void CalcColLabelsExposed( wxRegion
& reg
);
531 void CalcCellsExposed( wxRegion
& reg
);
534 // ------ event handlers
536 void ProcessRowLabelMouseEvent( wxMouseEvent
& event
);
537 void ProcessColLabelMouseEvent( wxMouseEvent
& event
);
538 void ProcessCornerLabelMouseEvent( wxMouseEvent
& event
);
539 void ProcessGridCellMouseEvent( wxMouseEvent
& event
);
540 bool ProcessTableMessage( wxGridTableMessage
& );
542 void DoEndDragResizeRow();
543 void DoEndDragResizeCol();
545 wxGridTableBase
* GetTable() const { return m_table
; }
546 void SetTable( wxGridTableBase
*table
) { m_table
= table
; }
549 bool InsertRows( int pos
= 0, int numRows
= 1, bool updateLabels
=TRUE
);
550 bool AppendRows( int numRows
= 1, bool updateLabels
=TRUE
);
551 bool DeleteRows( int pos
= 0, int numRows
= 1, bool updateLabels
=TRUE
);
552 bool InsertCols( int pos
= 0, int numCols
= 1, bool updateLabels
=TRUE
);
553 bool AppendCols( int numCols
= 1, bool updateLabels
=TRUE
);
554 bool DeleteCols( int pos
= 0, int numCols
= 1, bool updateLabels
=TRUE
);
556 void DrawGridCellArea( wxDC
& dc
);
557 void DrawCellBorder( wxDC
& dc
, const wxGridCellCoords
& );
558 void DrawAllGridLines( wxDC
& dc
, const wxRegion
& reg
);
559 void DrawCell( wxDC
& dc
, const wxGridCellCoords
& );
561 void DrawRowLabels( wxDC
& dc
);
562 void DrawRowLabel( wxDC
& dc
, int row
);
564 void DrawColLabels( wxDC
& dc
);
565 void DrawColLabel( wxDC
& dc
, int col
);
568 // ------ Cell text drawing functions
570 void DrawTextRectangle( wxDC
& dc
, const wxString
&, const wxRect
&,
571 int horizontalAlignment
= wxLEFT
,
572 int verticalAlignment
= wxTOP
);
574 // Split a string containing newline chararcters into an array of
575 // strings and return the number of lines
577 void StringToLines( const wxString
& value
, wxArrayString
& lines
);
579 void GetTextBoxSize( wxDC
& dc
,
580 wxArrayString
& lines
,
581 long *width
, long *height
);
585 // Code that does a lot of grid modification can be enclosed
586 // between BeginBatch() and EndBatch() calls to avoid screen
589 void BeginBatch() { m_batchCount
++; }
590 void EndBatch() { if ( m_batchCount
> 0 ) m_batchCount
--; }
591 int GetBatchCount() { return m_batchCount
; }
594 // ------ edit control functions
596 bool IsEditable() { return m_editable
; }
597 void EnableEditing( bool edit
);
599 #if 0 // at the moment the cell edit control is always active
600 void EnableCellEditControl( bool enable
);
603 bool IsCellEditControlEnabled()
604 { return (m_cellEditCtrl
&& m_cellEditCtrlEnabled
); }
606 void ShowCellEditControl();
607 void HideCellEditControl();
608 void SetEditControlValue( const wxString
& s
= wxEmptyString
);
609 void SaveEditControlValue();
612 // ------ grid location functions
613 // Note that all of these functions work with the logical coordinates of
614 // grid cells and labels so you will need to convert from device
615 // coordinates for mouse events etc.
617 void XYToCell( int x
, int y
, wxGridCellCoords
& );
621 int YToEdgeOfRow( int y
);
622 int XToEdgeOfCol( int x
);
624 wxRect
CellToRect( int row
, int col
);
625 wxRect
CellToRect( const wxGridCellCoords
& coords
)
626 { return CellToRect( coords
.GetRow(), coords
.GetCol() ); }
628 int GetGridCursorRow() { return m_currentCellCoords
.GetRow(); }
629 int GetGridCursorCol() { return m_currentCellCoords
.GetCol(); }
631 // check to see if a cell is either wholly visible (the default arg) or
632 // at least partially visible in the grid window
634 bool IsVisible( int row
, int col
, bool wholeCellVisible
= TRUE
);
635 bool IsVisible( const wxGridCellCoords
& coords
, bool wholeCellVisible
= TRUE
)
636 { return IsVisible( coords
.GetRow(), coords
.GetCol(), wholeCellVisible
); }
637 void MakeCellVisible( int row
, int col
);
638 void MakeCellVisible( const wxGridCellCoords
& coords
)
639 { MakeCellVisible( coords
.GetRow(), coords
.GetCol() ); }
642 // ------ grid cursor movement functions
644 void SetGridCursor( int row
, int col
)
645 { SetCurrentCell( wxGridCellCoords(row
, col
) ); }
648 bool MoveCursorDown();
649 bool MoveCursorLeft();
650 bool MoveCursorRight();
653 bool MoveCursorUpBlock();
654 bool MoveCursorDownBlock();
655 bool MoveCursorLeftBlock();
656 bool MoveCursorRightBlock();
659 // ------ label and gridline formatting
661 int GetDefaultRowLabelSize() { return WXGRID_DEFAULT_ROW_LABEL_WIDTH
; }
662 int GetRowLabelSize() { return m_rowLabelWidth
; }
663 int GetDefaultColLabelSize() { return WXGRID_DEFAULT_COL_LABEL_HEIGHT
; }
664 int GetColLabelSize() { return m_colLabelHeight
; }
665 wxColour
GetLabelBackgroundColour() { return m_labelBackgroundColour
; }
666 wxColour
GetLabelTextColour() { return m_labelTextColour
; }
667 wxFont
GetLabelFont() { return m_labelFont
; }
668 void GetRowLabelAlignment( int *horiz
, int *vert
);
669 void GetColLabelAlignment( int *horiz
, int *vert
);
670 wxString
GetRowLabelValue( int row
);
671 wxString
GetColLabelValue( int col
);
672 wxColour
GetGridLineColour() { return m_gridLineColour
; }
674 void SetRowLabelSize( int width
);
675 void SetColLabelSize( int height
);
676 void SetLabelBackgroundColour( const wxColour
& );
677 void SetLabelTextColour( const wxColour
& );
678 void SetLabelFont( const wxFont
& );
679 void SetRowLabelAlignment( int horiz
, int vert
);
680 void SetColLabelAlignment( int horiz
, int vert
);
681 void SetRowLabelValue( int row
, const wxString
& );
682 void SetColLabelValue( int col
, const wxString
& );
683 void SetGridLineColour( const wxColour
& );
685 // this sets the specified attribute for all cells in this row/col
686 void SetRowAttr(int row
, wxGridCellAttr
*attr
);
687 void SetColAttr(int col
, wxGridCellAttr
*attr
);
689 void EnableGridLines( bool enable
= TRUE
);
690 bool GridLinesEnabled() { return m_gridLinesEnabled
; }
692 // ------ row and col formatting
694 int GetDefaultRowSize();
695 int GetRowSize( int row
);
696 int GetDefaultColSize();
697 int GetColSize( int col
);
698 wxColour
GetDefaultCellBackgroundColour();
699 wxColour
GetCellBackgroundColour( int row
, int col
);
700 wxColour
GetDefaultCellTextColour();
701 wxColour
GetCellTextColour( int row
, int col
);
702 wxFont
GetDefaultCellFont();
703 wxFont
GetCellFont( int row
, int col
);
704 void GetDefaultCellAlignment( int *horiz
, int *vert
);
705 void GetCellAlignment( int row
, int col
, int *horiz
, int *vert
);
707 void SetDefaultRowSize( int height
, bool resizeExistingRows
= FALSE
);
708 void SetRowSize( int row
, int height
);
709 void SetDefaultColSize( int width
, bool resizeExistingCols
= FALSE
);
711 void SetColSize( int col
, int width
);
712 void SetDefaultCellBackgroundColour( const wxColour
& );
713 void SetCellBackgroundColour( int row
, int col
, const wxColour
& );
714 void SetDefaultCellTextColour( const wxColour
& );
716 void SetCellTextColour( int row
, int col
, const wxColour
& );
717 void SetDefaultCellFont( const wxFont
& );
718 void SetCellFont( int row
, int col
, const wxFont
& );
719 void SetDefaultCellAlignment( int horiz
, int vert
);
720 void SetCellAlignment( int row
, int col
, int horiz
, int vert
);
722 // takes ownership of the pointer
723 void SetDefaultRenderer(wxGridCellRenderer
*renderer
)
724 { delete m_defaultRenderer
; m_defaultRenderer
= renderer
; }
725 wxGridCellRenderer
*GetDefaultRenderer() const
726 { return m_defaultRenderer
; }
728 void SetCellRenderer(int row
, int col
, wxGridCellRenderer
*renderer
);
730 // ------ cell value accessors
732 wxString
GetCellValue( int row
, int col
)
736 return m_table
->GetValue( row
, col
);
740 return wxEmptyString
;
744 wxString
GetCellValue( const wxGridCellCoords
& coords
)
745 { return GetCellValue( coords
.GetRow(), coords
.GetCol() ); }
747 void SetCellValue( int row
, int col
, const wxString
& s
);
748 void SetCellValue( const wxGridCellCoords
& coords
, const wxString
& s
)
749 { SetCellValue( coords
.GetRow(), coords
.GetCol(), s
); }
753 // ------ selections of blocks of cells
755 void SelectRow( int row
, bool addToSelected
= FALSE
);
756 void SelectCol( int col
, bool addToSelected
= FALSE
);
758 void SelectBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
);
760 void SelectBlock( const wxGridCellCoords
& topLeft
,
761 const wxGridCellCoords
& bottomRight
)
762 { SelectBlock( topLeft
.GetRow(), topLeft
.GetCol(),
763 bottomRight
.GetRow(), bottomRight
.GetCol() ); }
768 { return ( m_selectedTopLeft
!= wxGridNoCellCoords
&&
769 m_selectedBottomRight
!= wxGridNoCellCoords
);
772 void ClearSelection();
774 bool IsInSelection( int row
, int col
)
775 { return ( IsSelection() &&
776 row
>= m_selectedTopLeft
.GetRow() &&
777 col
>= m_selectedTopLeft
.GetCol() &&
778 row
<= m_selectedBottomRight
.GetRow() &&
779 col
<= m_selectedBottomRight
.GetCol() );
782 bool IsInSelection( const wxGridCellCoords
& coords
)
783 { return IsInSelection( coords
.GetRow(), coords
.GetCol() ); }
785 void GetSelection( int* topRow
, int* leftCol
, int* bottomRow
, int* rightCol
)
787 // these will all be -1 if there is no selected block
789 *topRow
= m_selectedTopLeft
.GetRow();
790 *leftCol
= m_selectedTopLeft
.GetCol();
791 *bottomRow
= m_selectedBottomRight
.GetRow();
792 *rightCol
= m_selectedBottomRight
.GetCol();
796 // This function returns the rectangle that encloses the block of cells
797 // limited by TopLeft and BottomRight cell in device coords and clipped
798 // to the client size of the grid window.
800 wxRect
BlockToDeviceRect( const wxGridCellCoords
& topLeft
,
801 const wxGridCellCoords
& bottomRight
);
803 // This function returns the rectangle that encloses the selected cells
804 // in device coords and clipped to the client size of the grid window.
806 wxRect
SelectionToDeviceRect()
808 return BlockToDeviceRect( m_selectedTopLeft
,
809 m_selectedBottomRight
);
813 // ------ For compatibility with previous wxGrid only...
815 // ************************************************
816 // ** Don't use these in new code because they **
817 // ** are liable to disappear in a future **
819 // ************************************************
822 wxGrid( wxWindow
*parent
,
823 int x
= -1, int y
= -1, int w
= -1, int h
= -1,
825 const wxString
& name
= wxPanelNameStr
)
826 : wxScrolledWindow( parent
, -1, wxPoint(x
,y
), wxSize(w
,h
), style
, name
)
831 void SetCellValue( const wxString
& val
, int row
, int col
)
832 { SetCellValue( row
, col
, val
); }
834 void UpdateDimensions()
835 { CalcDimensions(); }
837 int GetRows() { return GetNumberRows(); }
838 int GetCols() { return GetNumberCols(); }
839 int GetCursorRow() { return GetGridCursorRow(); }
840 int GetCursorColumn() { return GetGridCursorCol(); }
842 int GetScrollPosX() { return 0; }
843 int GetScrollPosY() { return 0; }
845 void SetScrollX( int x
) { }
846 void SetScrollY( int y
) { }
848 void SetColumnWidth( int col
, int width
)
849 { SetColSize( col
, width
); }
851 int GetColumnWidth( int col
)
852 { return GetColSize( col
); }
854 void SetRowHeight( int row
, int height
)
855 { SetRowSize( row
, height
); }
857 int GetRowHeight( int row
)
858 { return GetRowSize( row
); }
860 int GetViewHeight() // returned num whole rows visible
863 int GetViewWidth() // returned num whole cols visible
866 void SetLabelSize( int orientation
, int sz
)
868 if ( orientation
== wxHORIZONTAL
)
869 SetColLabelSize( sz
);
871 SetRowLabelSize( sz
);
874 int GetLabelSize( int orientation
)
876 if ( orientation
== wxHORIZONTAL
)
877 return GetColLabelSize();
879 return GetRowLabelSize();
882 void SetLabelAlignment( int orientation
, int align
)
884 if ( orientation
== wxHORIZONTAL
)
885 SetColLabelAlignment( align
, -1 );
887 SetRowLabelAlignment( align
, -1 );
890 int GetLabelAlignment( int orientation
, int WXUNUSED(align
) )
893 if ( orientation
== wxHORIZONTAL
)
895 GetColLabelAlignment( &h
, &v
);
900 GetRowLabelAlignment( &h
, &v
);
905 void SetLabelValue( int orientation
, const wxString
& val
, int pos
)
907 if ( orientation
== wxHORIZONTAL
)
908 SetColLabelValue( pos
, val
);
910 SetRowLabelValue( pos
, val
);
913 wxString
GetLabelValue( int orientation
, int pos
)
915 if ( orientation
== wxHORIZONTAL
)
916 return GetColLabelValue( pos
);
918 return GetRowLabelValue( pos
);
921 wxFont
GetCellTextFont() const
922 { return m_defaultCellFont
; }
924 wxFont
GetCellTextFont(int WXUNUSED(row
), int WXUNUSED(col
)) const
925 { return m_defaultCellFont
; }
927 void SetCellTextFont(const wxFont
& fnt
)
928 { SetDefaultCellFont( fnt
); }
930 void SetCellTextFont(const wxFont
& fnt
, int row
, int col
)
931 { SetCellFont( row
, col
, fnt
); }
933 void SetCellTextColour(const wxColour
& val
, int row
, int col
)
934 { SetCellTextColour( row
, col
, val
); }
936 void SetCellTextColour(const wxColour
& col
)
937 { SetDefaultCellTextColour( col
); }
939 void SetCellBackgroundColour(const wxColour
& col
)
940 { SetDefaultCellBackgroundColour( col
); }
942 void SetCellBackgroundColour(const wxColour
& colour
, int row
, int col
)
943 { SetCellBackgroundColour( row
, col
, colour
); }
945 bool GetEditable() { return IsEditable(); }
946 void SetEditable( bool edit
= TRUE
) { EnableEditing( edit
); }
947 bool GetEditInPlace() { return IsCellEditControlEnabled(); }
949 void SetEditInPlace(bool edit
= TRUE
) { }
951 void SetCellAlignment( int align
, int row
, int col
)
952 { SetCellAlignment(row
, col
, align
, wxCENTER
); }
953 void SetCellAlignment( int WXUNUSED(align
) ) {}
954 void SetCellBitmap(wxBitmap
*WXUNUSED(bitmap
), int WXUNUSED(row
), int WXUNUSED(col
))
956 void SetDividerPen(const wxPen
& WXUNUSED(pen
)) { }
957 wxPen
& GetDividerPen() const { return wxNullPen
; }
958 void OnActivate(bool WXUNUSED(active
)) {}
960 // ******** End of compatibility functions **********
964 // ------ control IDs
965 enum { wxGRID_CELLCTRL
= 2000,
968 // ------ control types
969 enum { wxGRID_TEXTCTRL
= 2100,
978 wxGridWindow
*m_gridWin
;
979 wxGridRowLabelWindow
*m_rowLabelWin
;
980 wxGridColLabelWindow
*m_colLabelWin
;
981 wxGridCornerLabelWindow
*m_cornerLabelWin
;
983 wxGridTableBase
*m_table
;
993 wxGridCellCoords m_currentCellCoords
;
995 wxGridCellCoords m_selectedTopLeft
;
996 wxGridCellCoords m_selectedBottomRight
;
998 int m_defaultRowHeight
;
999 wxArrayInt m_rowHeights
;
1000 wxArrayInt m_rowBottoms
;
1002 int m_defaultColWidth
;
1003 wxArrayInt m_colWidths
;
1004 wxArrayInt m_colRights
;
1006 int m_rowLabelWidth
;
1007 int m_colLabelHeight
;
1009 wxColour m_labelBackgroundColour
;
1010 wxColour m_labelTextColour
;
1013 int m_rowLabelHorizAlign
;
1014 int m_rowLabelVertAlign
;
1015 int m_colLabelHorizAlign
;
1016 int m_colLabelVertAlign
;
1018 bool m_defaultRowLabelValues
;
1019 bool m_defaultColLabelValues
;
1021 wxColour m_gridLineColour
;
1022 bool m_gridLinesEnabled
;
1024 // get the renderer for the given cell - if it has no special one, the
1025 // default one will be returned, never NULL
1026 wxGridCellRenderer
*GetCellRenderer(int row
, int col
);
1028 wxGridCellRenderer
*m_defaultRenderer
;
1030 // default cell attributes
1031 wxFont m_defaultCellFont
;
1032 int m_defaultCellHAlign
,
1033 m_defaultCellVAlign
;
1035 // do we have some place to store attributes in?
1036 bool CanHaveAttributes();
1038 // returns the attribute we may modify in place: a new one if this cell
1039 // doesn't have any yet or the existing one if it does
1041 // DecRef() must be called on the returned pointer, as usual
1042 wxGridCellAttr
*GetOrCreateCellAttr(int row
, int col
) const;
1044 // cell attribute cache (currently we only cache 1, may be will do
1045 // more/better later)
1049 wxGridCellAttr
*attr
;
1052 // invalidates the attribute cache
1053 void ClearAttrCache();
1055 // adds an attribute to cache
1056 void CacheAttr(int row
, int col
, wxGridCellAttr
*attr
) const;
1058 // looks for an attr in cache, returns TRUE if found
1059 bool LookupAttr(int row
, int col
, wxGridCellAttr
**attr
) const;
1061 // looks for the attr in cache, if not found asks the table and caches the
1063 wxGridCellAttr
*GetCellAttr(int row
, int col
) const;
1065 wxGridCellCoordsArray m_cellsExposed
;
1066 wxArrayInt m_rowsExposed
;
1067 wxArrayInt m_colsExposed
;
1068 wxArrayInt m_rowLabelsExposed
;
1069 wxArrayInt m_colLabelsExposed
;
1076 WXGRID_CURSOR_SELECT_CELL
,
1077 WXGRID_CURSOR_RESIZE_ROW
,
1078 WXGRID_CURSOR_RESIZE_COL
,
1079 WXGRID_CURSOR_SELECT_ROW
,
1080 WXGRID_CURSOR_SELECT_COL
1083 // this method not only sets m_cursorMode but also sets the correct cursor
1084 // for the given mode and, if captureMouse is not FALSE releases the mouse
1085 // if it was captured and captures it if it must be captured
1087 // for this to work, you should always use it and not set m_cursorMode
1089 void ChangeCursorMode(CursorMode mode
,
1090 wxWindow
*win
= (wxWindow
*)NULL
,
1091 bool captureMouse
= TRUE
);
1093 wxWindow
*m_winCapture
; // the window which captured the mouse
1094 CursorMode m_cursorMode
;
1100 wxGridCellCoords m_selectionStart
;
1102 wxCursor m_rowResizeCursor
;
1103 wxCursor m_colResizeCursor
;
1105 bool m_editable
; // applies to whole grid
1106 int m_editCtrlType
; // for current cell
1107 wxWindow
* m_cellEditCtrl
;
1108 bool m_cellEditCtrlEnabled
;
1113 void CalcDimensions();
1114 void CalcWindowSizes();
1115 bool Redimension( wxGridTableMessage
& );
1118 bool SendEvent( const wxEventType
,
1122 bool SendEvent( const wxEventType
,
1126 void OnPaint( wxPaintEvent
& );
1127 void OnSize( wxSizeEvent
& );
1128 void OnKeyDown( wxKeyEvent
& );
1131 void SetCurrentCell( const wxGridCellCoords
& coords
);
1132 void SetCurrentCell( int row
, int col
)
1133 { SetCurrentCell( wxGridCellCoords(row
, col
) ); }
1136 // ------ functions to get/send data (see also public functions)
1138 bool GetModelValues();
1139 bool SetModelValues();
1142 DECLARE_DYNAMIC_CLASS( wxGrid
)
1143 DECLARE_EVENT_TABLE()
1151 // ------ Grid event class and event types
1154 class WXDLLEXPORT wxGridEvent
: public wxNotifyEvent
1158 : wxNotifyEvent(), m_row(-1), m_col(-1), m_x(-1), m_y(-1),
1159 m_control(0), m_meta(0), m_shift(0), m_alt(0)
1163 wxGridEvent(int id
, wxEventType type
, wxObject
* obj
,
1164 int row
=-1, int col
=-1, int x
=-1, int y
=-1,
1165 bool control
=FALSE
, bool shift
=FALSE
, bool alt
=FALSE
, bool meta
=FALSE
);
1167 virtual int GetRow() { return m_row
; }
1168 virtual int GetCol() { return m_col
; }
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
; }
1185 DECLARE_DYNAMIC_CLASS(wxGridEvent
)
1189 class WXDLLEXPORT wxGridSizeEvent
: public wxNotifyEvent
1193 : wxNotifyEvent(), m_rowOrCol(-1), m_x(-1), m_y(-1),
1194 m_control(0), m_meta(0), m_shift(0), m_alt(0)
1198 wxGridSizeEvent(int id
, wxEventType type
, wxObject
* obj
,
1199 int rowOrCol
=-1, int x
=-1, int y
=-1,
1200 bool control
=FALSE
, bool shift
=FALSE
, bool alt
=FALSE
, bool meta
=FALSE
);
1202 int GetRowOrCol() { return m_rowOrCol
; }
1203 wxPoint
GetPosition() { return wxPoint( m_x
, m_y
); }
1204 bool ControlDown() { return m_control
; }
1205 bool MetaDown() { return m_meta
; }
1206 bool ShiftDown() { return m_shift
; }
1207 bool AltDown() { return m_alt
; }
1218 DECLARE_DYNAMIC_CLASS(wxGridSizeEvent
)
1222 class WXDLLEXPORT wxGridRangeSelectEvent
: public wxNotifyEvent
1225 wxGridRangeSelectEvent()
1228 m_topLeft
= wxGridNoCellCoords
;
1229 m_bottomRight
= wxGridNoCellCoords
;
1236 wxGridRangeSelectEvent(int id
, wxEventType type
, wxObject
* obj
,
1237 const wxGridCellCoords
& topLeft
,
1238 const wxGridCellCoords
& bottomRight
,
1239 bool control
=FALSE
, bool shift
=FALSE
,
1240 bool alt
=FALSE
, bool meta
=FALSE
);
1242 wxGridCellCoords
GetTopLeftCoords() { return m_topLeft
; }
1243 wxGridCellCoords
GetBottomRightCoords() { return m_bottomRight
; }
1244 int GetTopRow() { return m_topLeft
.GetRow(); }
1245 int GetBottomRow() { return m_bottomRight
.GetRow(); }
1246 int GetLeftCol() { return m_topLeft
.GetCol(); }
1247 int GetRightCol() { return m_bottomRight
.GetCol(); }
1248 bool ControlDown() { return m_control
; }
1249 bool MetaDown() { return m_meta
; }
1250 bool ShiftDown() { return m_shift
; }
1251 bool AltDown() { return m_alt
; }
1254 wxGridCellCoords m_topLeft
;
1255 wxGridCellCoords m_bottomRight
;
1261 DECLARE_DYNAMIC_CLASS(wxGridRangeSelectEvent
)
1265 const wxEventType EVT_GRID_CELL_LEFT_CLICK
= wxEVT_FIRST
+ 1580;
1266 const wxEventType EVT_GRID_CELL_RIGHT_CLICK
= wxEVT_FIRST
+ 1581;
1267 const wxEventType EVT_GRID_CELL_LEFT_DCLICK
= wxEVT_FIRST
+ 1582;
1268 const wxEventType EVT_GRID_CELL_RIGHT_DCLICK
= wxEVT_FIRST
+ 1583;
1269 const wxEventType EVT_GRID_LABEL_LEFT_CLICK
= wxEVT_FIRST
+ 1584;
1270 const wxEventType EVT_GRID_LABEL_RIGHT_CLICK
= wxEVT_FIRST
+ 1585;
1271 const wxEventType EVT_GRID_LABEL_LEFT_DCLICK
= wxEVT_FIRST
+ 1586;
1272 const wxEventType EVT_GRID_LABEL_RIGHT_DCLICK
= wxEVT_FIRST
+ 1587;
1273 const wxEventType EVT_GRID_ROW_SIZE
= wxEVT_FIRST
+ 1588;
1274 const wxEventType EVT_GRID_COL_SIZE
= wxEVT_FIRST
+ 1589;
1275 const wxEventType EVT_GRID_RANGE_SELECT
= wxEVT_FIRST
+ 1590;
1276 const wxEventType EVT_GRID_CELL_CHANGE
= wxEVT_FIRST
+ 1591;
1277 const wxEventType EVT_GRID_SELECT_CELL
= wxEVT_FIRST
+ 1592;
1280 typedef void (wxEvtHandler::*wxGridEventFunction
)(wxGridEvent
&);
1281 typedef void (wxEvtHandler::*wxGridSizeEventFunction
)(wxGridSizeEvent
&);
1282 typedef void (wxEvtHandler::*wxGridRangeSelectEventFunction
)(wxGridRangeSelectEvent
&);
1284 #define EVT_GRID_CELL_LEFT_CLICK(fn) { EVT_GRID_CELL_LEFT_CLICK, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridEventFunction) &fn, NULL },
1285 #define EVT_GRID_CELL_RIGHT_CLICK(fn) { EVT_GRID_CELL_RIGHT_CLICK, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridEventFunction) &fn, NULL },
1286 #define EVT_GRID_CELL_LEFT_DCLICK(fn) { EVT_GRID_CELL_LEFT_DCLICK, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridEventFunction) &fn, NULL },
1287 #define EVT_GRID_CELL_RIGHT_DCLICK(fn) { EVT_GRID_CELL_RIGHT_DCLICK, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridEventFunction) &fn, NULL },
1288 #define EVT_GRID_LABEL_LEFT_CLICK(fn) { EVT_GRID_LABEL_LEFT_CLICK, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridEventFunction) &fn, NULL },
1289 #define EVT_GRID_LABEL_RIGHT_CLICK(fn) { EVT_GRID_LABEL_RIGHT_CLICK, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridEventFunction) &fn, NULL },
1290 #define EVT_GRID_LABEL_LEFT_DCLICK(fn) { EVT_GRID_LABEL_LEFT_DCLICK, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridEventFunction) &fn, NULL },
1291 #define EVT_GRID_LABEL_RIGHT_DCLICK(fn) { EVT_GRID_LABEL_RIGHT_DCLICK, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridEventFunction) &fn, NULL },
1292 #define EVT_GRID_ROW_SIZE(fn) { EVT_GRID_ROW_SIZE, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridSizeEventFunction) &fn, NULL },
1293 #define EVT_GRID_COL_SIZE(fn) { EVT_GRID_COL_SIZE, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridSizeEventFunction) &fn, NULL },
1294 #define EVT_GRID_RANGE_SELECT(fn) { EVT_GRID_RANGE_SELECT, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridRangeSelectEventFunction) &fn, NULL },
1295 #define EVT_GRID_CELL_CHANGE(fn) { EVT_GRID_CELL_CHANGE, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridEventFunction) &fn, NULL },
1296 #define EVT_GRID_SELECT_CELL(fn) { EVT_GRID_SELECT_CELL, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridEventFunction) &fn, NULL },
1299 #if 0 // TODO: implement these ? others ?
1301 const wxEventType EVT_GRID_CREATE_CELL
= wxEVT_FIRST
+ 1576;
1302 const wxEventType EVT_GRID_CHANGE_LABELS
= wxEVT_FIRST
+ 1577;
1303 const wxEventType EVT_GRID_CHANGE_SEL_LABEL
= wxEVT_FIRST
+ 1578;
1305 #define EVT_GRID_CREATE_CELL(fn) { EVT_GRID_CREATE_CELL, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridEventFunction) &fn, NULL },
1306 #define EVT_GRID_CHANGE_LABELS(fn) { EVT_GRID_CHANGE_LABELS, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridEventFunction) &fn, NULL },
1307 #define EVT_GRID_CHANGE_SEL_LABEL(fn) { EVT_GRID_CHANGE_SEL_LABEL, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridEventFunction) &fn, NULL },
1311 #endif // #ifndef __WXGRID_H__
1313 #endif // ifndef wxUSE_NEW_GRID