1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxGenericGrid
4 // Author: Julian Smart
5 // Modified by: Michael Bedward
6 // Added edit in place facility, 20 April 1999
7 // Added cursor key control, 29 Jun 1999
11 // Licence: wxWindows licence
12 /////////////////////////////////////////////////////////////////////////////
18 #pragma interface "gridg.h"
26 #include "wx/string.h"
27 #include "wx/scrolbar.h"
30 #define wxGRID_DEFAULT_EDIT_WIDTH 300
31 #define wxGRID_DEFAULT_EDIT_HEIGHT 27
32 #define wxGRID_DEFAULT_EDIT_X 2
33 #define wxGRID_DEFAULT_EDIT_Y 1
34 #define wxGRID_DEFAULT_SHEET_TOP 31
35 #define wxGRID_DEFAULT_SHEET_LEFT 0
36 #define wxGRID_DEFAULT_CELL_HEIGHT 20
37 #define wxGRID_DEFAULT_CELL_WIDTH 80
38 #define wxGRID_DEFAULT_VERTICAL_LABEL_WIDTH 40
39 #define wxGRID_DEFAULT_HORIZONAL_LABEL_HEIGHT 20
41 #define WXGENERIC_GRID_VERSION 0.5
43 class WXDLLEXPORT wxGridEvent
;
44 class WXDLLEXPORT wxGridCell
;
46 class WXDLLEXPORT wxGenericGrid
: public wxPanel
48 DECLARE_DYNAMIC_CLASS(wxGenericGrid
)
53 wxGenericGrid(wxWindow
*parent
, int x
, int y
, int width
, int height
, long style
= 0, char *name
= "grid")
55 Create(parent
, -1, wxPoint(x
, y
), wxSize(width
, height
), style
, name
);
57 wxGenericGrid(wxWindow
*parent
, wxWindowID id
, const wxPoint
& pos
, const wxSize
& size
, long style
= 0, const wxString
& name
= "grid")
59 Create(parent
, id
, pos
, size
, style
, name
);
63 bool Create(wxWindow
*parent
, wxWindowID
, const wxPoint
& pos
, const wxSize
& size
, long style
= 0, const wxString
& name
= "grid");
65 bool CreateGrid(int nRows
, int nCols
, wxString
**cellValues
= (wxString
**) NULL
, short *widths
= (short *) NULL
,
66 short defaultWidth
= wxGRID_DEFAULT_CELL_WIDTH
, short defaultHeight
= wxGRID_DEFAULT_CELL_HEIGHT
);
67 void PaintGrid(wxDC
& dc
);
69 virtual wxGridCell
*GetCell(int row
, int col
) const;
70 wxGridCell
***GetCells() const { return m_gridCells
; }
71 bool InsertCols(int pos
= 0, int n
= 1, bool updateLabels
= TRUE
);
72 bool InsertRows(int pos
= 0, int n
= 1, bool updateLabels
= TRUE
);
73 bool AppendCols(int n
= 1, bool updateLabels
= TRUE
);
74 bool AppendRows(int n
= 1, bool updateLabels
= TRUE
);
75 bool DeleteCols(int pos
= 0, int n
= 1, bool updateLabels
= TRUE
);
76 bool DeleteRows(int pos
= 0, int n
= 1, bool updateLabels
= TRUE
);
79 void SetCellValue(const wxString
& val
, int row
, int col
);
80 wxString
& GetCellValue(int row
, int col
) const;
81 void SetCellAlignment(int flag
, int row
, int col
);
82 void SetCellAlignment(int flag
);
83 int GetCellAlignment(int row
, int col
) const;
84 int GetCellAlignment() const;
85 void SetCellTextColour(const wxColour
& val
, int row
, int col
);
86 void SetCellTextColour(const wxColour
& col
);
87 wxColour
& GetCellTextColour(int row
, int col
) const;
88 wxColour
& GetCellTextColour() const { return (wxColour
&) m_cellTextColour
; }
89 void SetCellBackgroundColour(const wxColour
& col
);
90 void SetCellBackgroundColour(const wxColour
& colour
, int row
, int col
);
91 wxColour
& GetCellBackgroundColour() const { return (wxColour
&) m_cellBackgroundColour
; }
92 wxColour
& GetCellBackgroundColour(int row
, int col
) const;
93 wxFont
& GetCellTextFont() const { return (wxFont
&) m_cellTextFont
; }
94 wxFont
& GetCellTextFont(int row
, int col
) const;
95 void SetCellTextFont(const wxFont
& fnt
);
96 void SetCellTextFont(const wxFont
& fnt
, int row
, int col
);
97 wxBitmap
*GetCellBitmap(int row
, int col
) const;
98 void SetCellBitmap(wxBitmap
*bitmap
, int row
, int col
);
99 void *SetCellData(void *data
, int row
, int col
);
100 void *GetCellData(int row
, int col
);
103 void SetColumnWidth(int col
, int width
);
104 int GetColumnWidth(int col
) const;
105 void SetRowHeight(int row
, int height
);
106 int GetRowHeight(int row
) const;
107 int GetViewHeight() const { return m_viewHeight
; }
108 int GetViewWidth() const { return m_viewWidth
; }
111 void SetLabelSize(int orientation
, int sz
);
112 int GetLabelSize(int orientation
) const;
113 void SetLabelAlignment(int orientation
, int alignment
);
114 int GetLabelAlignment(int orientation
) const;
115 wxGridCell
*GetLabelCell(int orientation
, int pos
) const;
116 void SetLabelValue(int orientation
, const wxString
& val
, int pos
);
117 wxString
& GetLabelValue(int orientation
, int pos
) const;
118 void SetLabelTextColour(const wxColour
& colour
);
119 void SetLabelBackgroundColour(const wxColour
& colour
);
120 wxColour
& GetLabelTextColour() const { return (wxColour
&) m_labelTextColour
; }
121 wxColour
& GetLabelBackgroundColour() { return (wxColour
&) m_labelBackgroundColour
; }
122 wxFont
& GetLabelTextFont() { return (wxFont
&) m_labelTextFont
; }
123 void SetLabelTextFont(const wxFont
& fnt
) { m_labelTextFont
= fnt
; }
125 // Miscellaneous accessors
126 int GetCursorRow() const { return m_wCursorRow
; }
127 int GetCursorColumn() const { return m_wCursorColumn
; }
128 void SetGridCursor(int row
, int col
);
129 int GetRows() const { return m_totalRows
; }
130 int GetCols() const { return m_totalCols
; }
131 int GetScrollPosX() const { return m_scrollPosX
; }
132 int GetScrollPosY() const { return m_scrollPosY
; }
133 void SetScrollPosX(int pos
) { m_scrollPosX
= pos
; }
134 void SetScrollPosY(int pos
) { m_scrollPosY
= pos
; }
135 wxTextCtrl
*GetTextItem() const { return m_textItem
; }
136 wxScrollBar
*GetHorizScrollBar() const { return m_hScrollBar
; }
137 wxScrollBar
*GetVertScrollBar() const { return m_vScrollBar
; }
138 bool GetEditable() const { return m_editable
; }
139 void SetEditable(bool edit
);
141 bool GetEditInPlace() const { return m_editInPlace
; }
142 void SetEditInPlace(bool edit
= TRUE
);
144 wxRect
& GetCurrentRect() const { return (wxRect
&) m_currentRect
; }
145 bool CurrentCellVisible() const { return m_currentRectVisible
; }
146 void SetDividerPen(const wxPen
& pen
) { m_divisionPen
= pen
; }
147 wxPen
& GetDividerPen() const { return (wxPen
&) m_divisionPen
; }
149 // High-level event handling
150 // Override e.g. to check value of current cell; but call
151 // base member for default processing.
152 virtual void OnSelectCellImplementation(wxDC
*dc
, int row
, int col
);
154 virtual void OnSelectCell(int WXUNUSED(row
), int WXUNUSED(col
)) {};
155 void _OnSelectCell(wxGridEvent
& event
);
157 // Override to create your own class of grid cell
158 virtual wxGridCell
*OnCreateCell();
159 void _OnCreateCell(wxGridEvent
& event
);
161 // Override to change labels e.g. creation of grid, inserting/deleting a row/col.
162 // By default, auto-labels the grid.
163 virtual void OnChangeLabels();
164 void _OnChangeLabels(wxGridEvent
& event
);
166 // Override to change the label of the edit field when selecting a cell
167 // By default, sets it to e.g. A12
168 virtual void OnChangeSelectionLabel();
169 void _OnChangeSelectionLabel(wxGridEvent
& event
);
171 // Override for event processing
172 virtual void OnCellChange(int WXUNUSED(row
), int WXUNUSED(col
)) {};
173 virtual void OnCellLeftClick(int WXUNUSED(row
), int WXUNUSED(col
), int WXUNUSED(x
), int WXUNUSED(y
), bool WXUNUSED(control
), bool WXUNUSED(shift
)) {};
174 virtual void OnCellRightClick(int WXUNUSED(row
), int WXUNUSED(col
), int WXUNUSED(x
), int WXUNUSED(y
), bool WXUNUSED(control
), bool WXUNUSED(shift
)) {};
175 virtual void OnLabelLeftClick(int WXUNUSED(row
), int WXUNUSED(col
), int WXUNUSED(x
), int WXUNUSED(y
), bool WXUNUSED(control
), bool WXUNUSED(shift
)) {};
176 virtual void OnLabelRightClick(int WXUNUSED(row
), int WXUNUSED(col
), int WXUNUSED(x
), int WXUNUSED(y
), bool WXUNUSED(control
), bool WXUNUSED(shift
)) {};
178 void _OnCellChange(wxGridEvent
& event
);
179 void _OnCellLeftClick(wxGridEvent
& event
);
180 void _OnCellRightClick(wxGridEvent
& event
);
181 void _OnLabelLeftClick(wxGridEvent
& event
);
182 void _OnLabelRightClick(wxGridEvent
& event
);
184 // Activation: call from wxFrame::OnActivate
185 void OnActivate(bool active
);
188 void AdjustScrollbars();
189 void UpdateDimensions();
191 void SetCurrentRect (int Row
, int Column
, int canvasW
= -1, int canvasH
= -1);
192 void HighlightCell(wxDC
*dc
, bool doHighlight
);
194 void SetGridClippingRegion(wxDC
*dc
);
196 virtual bool CellHitTest(int x
, int y
, int *row
, int *col
);
197 virtual bool LabelSashHitTest(int x
, int y
, int *orientation
, int *rowOrCol
, int *startPos
);
198 virtual bool LabelHitTest(int x
, int y
, int *row
, int *col
);
201 virtual void DrawLabelAreas(wxDC
*dc
);
202 virtual void DrawEditableArea(wxDC
*dc
);
203 virtual void DrawGridLines(wxDC
*dc
);
204 virtual void DrawColumnLabels(wxDC
*dc
);
205 virtual void DrawColumnLabel(wxDC
*dc
, wxRect
*rect
, int col
);
206 virtual void DrawRowLabels(wxDC
*dc
);
207 virtual void DrawRowLabel(wxDC
*dc
, wxRect
*rect
, int row
);
208 virtual void DrawCells(wxDC
*dc
);
209 virtual void DrawCellValue(wxDC
*dc
, wxRect
*rect
, int row
, int col
);
210 virtual void DrawCellBackground(wxDC
*dc
, wxRect
*rect
, int row
, int col
);
211 virtual void DrawTextRect(wxDC
*dc
, const wxString
& text
, wxRect
*rect
, int flag
);
212 virtual void DrawBitmapRect(wxDC
*dc
, wxBitmap
*bitmap
, wxRect
*rect
, int flag
);
214 // Refresh cell and optionally set the text field
215 void RefreshCell(int row
, int col
, bool setText
= FALSE
);
217 // Don't refresh within the outer pair of these.
218 void BeginBatch() { m_batchCount
++; }
219 void EndBatch() { m_batchCount
--; }
220 int GetBatchCount() { return m_batchCount
; }
222 // implementation from now on
224 void OnPaint(wxPaintEvent
& event
);
225 void OnEraseBackground(wxEraseEvent
& event
);
226 void OnMouseEvent(wxMouseEvent
& event
);
227 void OnSize(wxSizeEvent
& event
);
228 void OnText(wxCommandEvent
& ev
);
229 void OnTextEnter(wxCommandEvent
& ev
);
230 void OnTextInPlace(wxCommandEvent
& ev
);
231 void OnTextInPlaceEnter(wxCommandEvent
& ev
);
232 void OnGridScroll(wxScrollEvent
& ev
);
235 wxPanel
* m_editingPanel
; // Contains the text control
236 wxTextCtrl
* m_textItem
;
237 wxTextCtrl
* m_inPlaceTextItem
;
239 wxScrollBar
* m_hScrollBar
;
240 wxScrollBar
* m_vScrollBar
;
243 wxRect m_currentRect
;
244 bool m_currentRectVisible
;
245 wxGridCell
*** m_gridCells
;
246 wxGridCell
** m_rowLabelCells
;
247 wxGridCell
** m_colLabelCells
;
252 bool m_inOnTextInPlace
;
258 // Row and column we're currently looking at
265 int m_rightOfSheet
; // Calculated from m_colWidths
266 int m_bottomOfSheet
; // Calculated from m_rowHeights
267 int m_totalGridWidth
; // Total 'virtual' size
268 int m_totalGridHeight
;
269 int m_viewHeight
; // Number of rows displayed
270 int m_viewWidth
; // Number of columns displayed
271 int m_cellHeight
; // For now, a default
272 int m_verticalLabelWidth
;
273 int m_horizontalLabelHeight
;
274 int m_verticalLabelAlignment
;
275 int m_horizontalLabelAlignment
;
277 short* m_colWidths
; // Dynamically allocated
278 short* m_rowHeights
; // Dynamically allocated
279 int m_scrollWidth
; // Vert. scroll width, horiz. scroll height
282 wxColour m_cellTextColour
;
283 wxColour m_cellBackgroundColour
;
284 wxFont m_cellTextFont
;
285 wxColour m_labelTextColour
;
286 wxColour m_labelBackgroundColour
;
287 wxBrush m_labelBackgroundBrush
;
288 wxFont m_labelTextFont
;
290 wxPen m_highlightPen
;
291 wxBitmap
* m_doubleBufferingBitmap
;
293 // Position of Edit control
294 wxRect m_editControlPosition
;
299 int m_dragStartPosition
;
300 int m_dragLastPosition
;
301 wxCursor m_horizontalSashCursor
;
302 wxCursor m_verticalSashCursor
;
304 // To avoid multiple refreshes, use Begin/EndBatch
307 DECLARE_EVENT_TABLE()
310 #define wxGRID_TEXT_CTRL 2000
311 #define wxGRID_HSCROLL 2001
312 #define wxGRID_VSCROLL 2002
313 #define wxGRID_EDIT_IN_PLACE_TEXT_CTRL 2003
315 class WXDLLEXPORT wxGridCell
: public wxObject
321 wxColour backgroundColour
;
322 wxBrush backgroundBrush
;
323 wxBitmap
* cellBitmap
;
324 void* cellData
; // intended for additional data associated with a cell
327 wxGridCell(wxGenericGrid
*window
= (wxGenericGrid
*) NULL
);
330 virtual wxString
& GetTextValue() const { return (wxString
&) textValue
; }
331 virtual void SetTextValue(const wxString
& str
) { textValue
= str
; }
332 wxFont
& GetFont() const { return (wxFont
&) font
; }
333 void SetFont(const wxFont
& f
) { font
= f
; }
334 wxColour
& GetTextColour() const { return (wxColour
&) textColour
; }
335 void SetTextColour(const wxColour
& colour
) { textColour
= colour
; }
336 wxColour
& GetBackgroundColour() const { return (wxColour
&) backgroundColour
; }
337 void SetBackgroundColour(const wxColour
& colour
);
338 wxBrush
& GetBackgroundBrush() const { return (wxBrush
&) backgroundBrush
; }
339 void SetBackgroundBrush(const wxBrush
& brush
) { backgroundBrush
= brush
; }
340 int GetAlignment() const { return alignment
; }
341 void SetAlignment(int align
) { alignment
= align
; }
342 wxBitmap
*GetCellBitmap() const { return cellBitmap
; }
343 void SetCellBitmap(wxBitmap
*bitmap
) { cellBitmap
= bitmap
; }
345 void *SetCellData(void *data
) { void *rc
= cellData
; cellData
= data
; return rc
; }
346 void *GetCellData() const { return cellData
; }
349 class WXDLLEXPORT wxGrid
: public wxGenericGrid
352 wxGrid() : wxGenericGrid() { }
353 wxGrid(wxWindow
*parent
, int x
=-1, int y
=-1, int width
=-1, int height
=-1,
354 long style
=0, char *name
= "gridWindow")
355 : wxGenericGrid(parent
, x
, y
, width
, height
, style
, name
)
358 wxGrid(wxWindow
*parent
, wxWindowID id
, const wxPoint
& pos
,
359 const wxSize
& size
, long style
= 0, const wxString
& name
= "grid")
360 : wxGenericGrid(parent
, id
, pos
, size
, style
, name
)
366 class WXDLLEXPORT wxGridEvent
: public wxCommandEvent
368 DECLARE_DYNAMIC_CLASS(wxGridEvent
)
372 : wxCommandEvent(), m_row(-1), m_col(-1), m_x(-1), m_y(-1),
373 m_control(0), m_shift(0), m_cell(0)
377 wxGridEvent(int id
, wxEventType type
, wxObject
* obj
,
378 int row
=-1, int col
=-1, int x
=-1, int y
=-1,
379 bool control
=FALSE
, bool shift
=FALSE
)
380 : wxCommandEvent(type
, id
), m_row(row
), m_col(col
), m_x(x
), m_y(y
),
381 m_control(control
), m_shift(shift
), m_cell(0)
395 int GetRow() const { return m_row
; }
396 int GetCol() const { return m_col
; }
397 wxPoint
GetPosition() const { return wxPoint( m_x
, m_y
); }
398 wxGridCell
* GetCell() const { return m_cell
; }
399 bool ControlDown() const { return m_control
; }
400 bool ShiftDown() const { return m_shift
; }
403 const wxEventType wxEVT_GRID_SELECT_CELL
= wxEVT_FIRST
+ 1575;
404 const wxEventType wxEVT_GRID_CREATE_CELL
= wxEVT_FIRST
+ 1576;
405 const wxEventType wxEVT_GRID_CHANGE_LABELS
= wxEVT_FIRST
+ 1577;
406 const wxEventType wxEVT_GRID_CHANGE_SEL_LABEL
= wxEVT_FIRST
+ 1578;
407 const wxEventType wxEVT_GRID_CELL_CHANGE
= wxEVT_FIRST
+ 1579;
408 const wxEventType wxEVT_GRID_CELL_LCLICK
= wxEVT_FIRST
+ 1580;
409 const wxEventType wxEVT_GRID_CELL_RCLICK
= wxEVT_FIRST
+ 1581;
410 const wxEventType wxEVT_GRID_LABEL_LCLICK
= wxEVT_FIRST
+ 1582;
411 const wxEventType wxEVT_GRID_LABEL_RCLICK
= wxEVT_FIRST
+ 1583;
414 typedef void (wxEvtHandler::*wxGridEventFunction
)(wxGridEvent
&);
416 #define EVT_GRID_SELECT_CELL(fn) DECLARE_EVENT_TABLE_ENTRY(wxEVT_GRID_SELECT_CELL, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridEventFunction) &fn, NULL),
417 #define EVT_GRID_CREATE_CELL(fn) DECLARE_EVENT_TABLE_ENTRY(wxEVT_GRID_CREATE_CELL, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridEventFunction) &fn, NULL),
418 #define EVT_GRID_CHANGE_LABELS(fn) DECLARE_EVENT_TABLE_ENTRY(wxEVT_GRID_CHANGE_LABELS, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridEventFunction) &fn, NULL),
419 #define EVT_GRID_CHANGE_SEL_LABEL(fn) DECLARE_EVENT_TABLE_ENTRY(wxEVT_GRID_CHANGE_SEL_LABEL, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridEventFunction) &fn, NULL),
420 #define EVT_GRID_CELL_CHANGE(fn) DECLARE_EVENT_TABLE_ENTRY(wxEVT_GRID_CELL_CHANGE, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridEventFunction) &fn, NULL),
421 #define EVT_GRID_CELL_LCLICK(fn) DECLARE_EVENT_TABLE_ENTRY(wxEVT_GRID_CELL_LCLICK, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridEventFunction) &fn, NULL),
422 #define EVT_GRID_CELL_RCLICK(fn) DECLARE_EVENT_TABLE_ENTRY(wxEVT_GRID_CELL_RCLICK, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridEventFunction) &fn, NULL),
423 #define EVT_GRID_LABEL_LCLICK(fn) DECLARE_EVENT_TABLE_ENTRY(wxEVT_GRID_LABEL_LCLICK, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridEventFunction) &fn, NULL),
424 #define EVT_GRID_LABEL_RCLICK(fn) DECLARE_EVENT_TABLE_ENTRY(wxEVT_GRID_LABEL_RCLICK, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridEventFunction) &fn, NULL),
428 #endif // __GRIDH_G__