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"
23 #include "wx/string.h"
24 #include "wx/scrolbar.h"
27 #define wxGRID_DEFAULT_EDIT_WIDTH 300
28 #define wxGRID_DEFAULT_EDIT_HEIGHT 27
29 #define wxGRID_DEFAULT_EDIT_X 2
30 #define wxGRID_DEFAULT_EDIT_Y 1
31 #define wxGRID_DEFAULT_SHEET_TOP 31
32 #define wxGRID_DEFAULT_SHEET_LEFT 0
33 #define wxGRID_DEFAULT_CELL_HEIGHT 20
34 #define wxGRID_DEFAULT_CELL_WIDTH 80
35 #define wxGRID_DEFAULT_VERTICAL_LABEL_WIDTH 40
36 #define wxGRID_DEFAULT_HORIZONAL_LABEL_HEIGHT 20
38 #define WXGENERIC_GRID_VERSION 0.5
40 class WXDLLEXPORT wxGridEvent
;
41 class WXDLLEXPORT wxGridCell
;
43 class WXDLLEXPORT wxGenericGrid
: public wxPanel
45 DECLARE_DYNAMIC_CLASS(wxGenericGrid
)
50 wxGenericGrid(wxWindow
*parent
, int x
, int y
, int width
, int height
, long style
= 0, char *name
= "grid")
52 Create(parent
, -1, wxPoint(x
, y
), wxSize(width
, height
), style
, name
);
54 wxGenericGrid(wxWindow
*parent
, wxWindowID id
, const wxPoint
& pos
, const wxSize
& size
, long style
= 0, const wxString
& name
= "grid")
56 Create(parent
, id
, pos
, size
, style
, name
);
60 bool Create(wxWindow
*parent
, wxWindowID
, const wxPoint
& pos
, const wxSize
& size
, long style
= 0, const wxString
& name
= "grid");
62 bool CreateGrid(int nRows
, int nCols
, wxString
**cellValues
= (wxString
**) NULL
, short *widths
= (short *) NULL
,
63 short defaultWidth
= wxGRID_DEFAULT_CELL_WIDTH
, short defaultHeight
= wxGRID_DEFAULT_CELL_HEIGHT
);
64 void PaintGrid(wxDC
& dc
);
66 virtual wxGridCell
*GetCell(int row
, int col
) const;
67 wxGridCell
***GetCells() const { return m_gridCells
; }
68 bool InsertCols(int pos
= 0, int n
= 1, bool updateLabels
= TRUE
);
69 bool InsertRows(int pos
= 0, int n
= 1, bool updateLabels
= TRUE
);
70 bool AppendCols(int n
= 1, bool updateLabels
= TRUE
);
71 bool AppendRows(int n
= 1, bool updateLabels
= TRUE
);
72 bool DeleteCols(int pos
= 0, int n
= 1, bool updateLabels
= TRUE
);
73 bool DeleteRows(int pos
= 0, int n
= 1, bool updateLabels
= TRUE
);
76 void SetCellValue(const wxString
& val
, int row
, int col
);
77 wxString
& GetCellValue(int row
, int col
) const;
78 void SetCellAlignment(int flag
, int row
, int col
);
79 void SetCellAlignment(int flag
);
80 int GetCellAlignment(int row
, int col
) const;
81 int GetCellAlignment() const;
82 void SetCellTextColour(const wxColour
& val
, int row
, int col
);
83 void SetCellTextColour(const wxColour
& col
);
84 wxColour
& GetCellTextColour(int row
, int col
) const;
85 wxColour
& GetCellTextColour() const { return (wxColour
&) m_cellTextColour
; }
86 void SetCellBackgroundColour(const wxColour
& col
);
87 void SetCellBackgroundColour(const wxColour
& colour
, int row
, int col
);
88 wxColour
& GetCellBackgroundColour() const { return (wxColour
&) m_cellBackgroundColour
; }
89 wxColour
& GetCellBackgroundColour(int row
, int col
) const;
90 wxFont
& GetCellTextFont() const { return (wxFont
&) m_cellTextFont
; }
91 wxFont
& GetCellTextFont(int row
, int col
) const;
92 void SetCellTextFont(const wxFont
& fnt
);
93 void SetCellTextFont(const wxFont
& fnt
, int row
, int col
);
94 wxBitmap
*GetCellBitmap(int row
, int col
) const;
95 void SetCellBitmap(wxBitmap
*bitmap
, int row
, int col
);
96 void *SetCellData(void *data
, int row
, int col
);
97 void *GetCellData(int row
, int col
);
100 void SetColumnWidth(int col
, int width
);
101 int GetColumnWidth(int col
) const;
102 void SetRowHeight(int row
, int height
);
103 int GetRowHeight(int row
) const;
104 int GetViewHeight() const { return m_viewHeight
; }
105 int GetViewWidth() const { return m_viewWidth
; }
108 void SetLabelSize(int orientation
, int sz
);
109 int GetLabelSize(int orientation
) const;
110 void SetLabelAlignment(int orientation
, int alignment
);
111 int GetLabelAlignment(int orientation
) const;
112 wxGridCell
*GetLabelCell(int orientation
, int pos
) const;
113 void SetLabelValue(int orientation
, const wxString
& val
, int pos
);
114 wxString
& GetLabelValue(int orientation
, int pos
) const;
115 void SetLabelTextColour(const wxColour
& colour
);
116 void SetLabelBackgroundColour(const wxColour
& colour
);
117 wxColour
& GetLabelTextColour() const { return (wxColour
&) m_labelTextColour
; }
118 wxColour
& GetLabelBackgroundColour() { return (wxColour
&) m_labelBackgroundColour
; }
119 wxFont
& GetLabelTextFont() { return (wxFont
&) m_labelTextFont
; }
120 void SetLabelTextFont(const wxFont
& fnt
) { m_labelTextFont
= fnt
; }
122 // Miscellaneous accessors
123 int GetCursorRow() const { return m_wCursorRow
; }
124 int GetCursorColumn() const { return m_wCursorColumn
; }
125 void SetGridCursor(int row
, int col
);
126 int GetRows() const { return m_totalRows
; }
127 int GetCols() const { return m_totalCols
; }
128 int GetScrollPosX() const { return m_scrollPosX
; }
129 int GetScrollPosY() const { return m_scrollPosY
; }
130 void SetScrollPosX(int pos
) { m_scrollPosX
= pos
; }
131 void SetScrollPosY(int pos
) { m_scrollPosY
= pos
; }
132 wxTextCtrl
*GetTextItem() const { return m_textItem
; }
133 wxScrollBar
*GetHorizScrollBar() const { return m_hScrollBar
; }
134 wxScrollBar
*GetVertScrollBar() const { return m_vScrollBar
; }
135 bool GetEditable() const { return m_editable
; }
136 void SetEditable(bool edit
);
138 bool GetEditInPlace() const { return m_editInPlace
; }
139 void SetEditInPlace(bool edit
= TRUE
);
141 wxRect
& GetCurrentRect() const { return (wxRect
&) m_currentRect
; }
142 bool CurrentCellVisible() const { return m_currentRectVisible
; }
143 void SetDividerPen(const wxPen
& pen
) { m_divisionPen
= pen
; }
144 wxPen
& GetDividerPen() const { return (wxPen
&) m_divisionPen
; }
146 // High-level event handling
147 // Override e.g. to check value of current cell; but call
148 // base member for default processing.
149 virtual void OnSelectCellImplementation(wxDC
*dc
, int row
, int col
);
151 virtual void OnSelectCell(int WXUNUSED(row
), int WXUNUSED(col
)) {};
152 void _OnSelectCell(wxGridEvent
& event
);
154 // Override to create your own class of grid cell
155 virtual wxGridCell
*OnCreateCell();
156 void _OnCreateCell(wxGridEvent
& event
);
158 // Override to change labels e.g. creation of grid, inserting/deleting a row/col.
159 // By default, auto-labels the grid.
160 virtual void OnChangeLabels();
161 void _OnChangeLabels(wxGridEvent
& event
);
163 // Override to change the label of the edit field when selecting a cell
164 // By default, sets it to e.g. A12
165 virtual void OnChangeSelectionLabel();
166 void _OnChangeSelectionLabel(wxGridEvent
& event
);
168 // Override for event processing
169 virtual void OnCellChange(int WXUNUSED(row
), int WXUNUSED(col
)) {};
170 virtual void OnCellLeftClick(int WXUNUSED(row
), int WXUNUSED(col
), int WXUNUSED(x
), int WXUNUSED(y
), bool WXUNUSED(control
), bool WXUNUSED(shift
)) {};
171 virtual void OnCellRightClick(int WXUNUSED(row
), int WXUNUSED(col
), int WXUNUSED(x
), int WXUNUSED(y
), bool WXUNUSED(control
), bool WXUNUSED(shift
)) {};
172 virtual void OnLabelLeftClick(int WXUNUSED(row
), int WXUNUSED(col
), int WXUNUSED(x
), int WXUNUSED(y
), bool WXUNUSED(control
), bool WXUNUSED(shift
)) {};
173 virtual void OnLabelRightClick(int WXUNUSED(row
), int WXUNUSED(col
), int WXUNUSED(x
), int WXUNUSED(y
), bool WXUNUSED(control
), bool WXUNUSED(shift
)) {};
175 void _OnCellChange(wxGridEvent
& event
);
176 void _OnCellLeftClick(wxGridEvent
& event
);
177 void _OnCellRightClick(wxGridEvent
& event
);
178 void _OnLabelLeftClick(wxGridEvent
& event
);
179 void _OnLabelRightClick(wxGridEvent
& event
);
181 // Activation: call from wxFrame::OnActivate
182 void OnActivate(bool active
);
185 void AdjustScrollbars();
186 void UpdateDimensions();
188 void SetCurrentRect (int Row
, int Column
, int canvasW
= -1, int canvasH
= -1);
189 void HighlightCell(wxDC
*dc
, bool doHighlight
);
191 void SetGridClippingRegion(wxDC
*dc
);
193 virtual bool CellHitTest(int x
, int y
, int *row
, int *col
);
194 virtual bool LabelSashHitTest(int x
, int y
, int *orientation
, int *rowOrCol
, int *startPos
);
195 virtual bool LabelHitTest(int x
, int y
, int *row
, int *col
);
198 virtual void DrawLabelAreas(wxDC
*dc
);
199 virtual void DrawEditableArea(wxDC
*dc
);
200 virtual void DrawGridLines(wxDC
*dc
);
201 virtual void DrawColumnLabels(wxDC
*dc
);
202 virtual void DrawColumnLabel(wxDC
*dc
, wxRect
*rect
, int col
);
203 virtual void DrawRowLabels(wxDC
*dc
);
204 virtual void DrawRowLabel(wxDC
*dc
, wxRect
*rect
, int row
);
205 virtual void DrawCells(wxDC
*dc
);
206 virtual void DrawCellValue(wxDC
*dc
, wxRect
*rect
, int row
, int col
);
207 virtual void DrawCellBackground(wxDC
*dc
, wxRect
*rect
, int row
, int col
);
208 virtual void DrawTextRect(wxDC
*dc
, const wxString
& text
, wxRect
*rect
, int flag
);
209 virtual void DrawBitmapRect(wxDC
*dc
, wxBitmap
*bitmap
, wxRect
*rect
, int flag
);
211 // Refresh cell and optionally set the text field
212 void RefreshCell(int row
, int col
, bool setText
= FALSE
);
214 // Don't refresh within the outer pair of these.
215 void BeginBatch() { m_batchCount
++; }
216 void EndBatch() { m_batchCount
--; }
217 int GetBatchCount() { return m_batchCount
; }
219 // implementation from now on
221 void OnPaint(wxPaintEvent
& event
);
222 void OnEraseBackground(wxEraseEvent
& event
);
223 void OnMouseEvent(wxMouseEvent
& event
);
224 void OnSize(wxSizeEvent
& event
);
225 void OnText(wxCommandEvent
& ev
);
226 void OnTextEnter(wxCommandEvent
& ev
);
227 void OnTextInPlace(wxCommandEvent
& ev
);
228 void OnTextInPlaceEnter(wxCommandEvent
& ev
);
229 void OnGridScroll(wxScrollEvent
& ev
);
232 wxPanel
* m_editingPanel
; // Contains the text control
233 wxTextCtrl
* m_textItem
;
234 wxTextCtrl
* m_inPlaceTextItem
;
236 wxScrollBar
* m_hScrollBar
;
237 wxScrollBar
* m_vScrollBar
;
240 wxRect m_currentRect
;
241 bool m_currentRectVisible
;
242 wxGridCell
*** m_gridCells
;
243 wxGridCell
** m_rowLabelCells
;
244 wxGridCell
** m_colLabelCells
;
249 bool m_inOnTextInPlace
;
255 // Row and column we're currently looking at
262 int m_rightOfSheet
; // Calculated from m_colWidths
263 int m_bottomOfSheet
; // Calculated from m_rowHeights
264 int m_totalGridWidth
; // Total 'virtual' size
265 int m_totalGridHeight
;
266 int m_viewHeight
; // Number of rows displayed
267 int m_viewWidth
; // Number of columns displayed
268 int m_cellHeight
; // For now, a default
269 int m_verticalLabelWidth
;
270 int m_horizontalLabelHeight
;
271 int m_verticalLabelAlignment
;
272 int m_horizontalLabelAlignment
;
274 short* m_colWidths
; // Dynamically allocated
275 short* m_rowHeights
; // Dynamically allocated
276 int m_scrollWidth
; // Vert. scroll width, horiz. scroll height
279 wxColour m_cellTextColour
;
280 wxColour m_cellBackgroundColour
;
281 wxFont m_cellTextFont
;
282 wxColour m_labelTextColour
;
283 wxColour m_labelBackgroundColour
;
284 wxBrush m_labelBackgroundBrush
;
285 wxFont m_labelTextFont
;
287 wxPen m_highlightPen
;
288 wxBitmap
* m_doubleBufferingBitmap
;
290 // Position of Edit control
291 wxRect m_editControlPosition
;
296 int m_dragStartPosition
;
297 int m_dragLastPosition
;
298 wxCursor m_horizontalSashCursor
;
299 wxCursor m_verticalSashCursor
;
301 // To avoid multiple refreshes, use Begin/EndBatch
304 DECLARE_EVENT_TABLE()
307 #define wxGRID_TEXT_CTRL 2000
308 #define wxGRID_HSCROLL 2001
309 #define wxGRID_VSCROLL 2002
310 #define wxGRID_EDIT_IN_PLACE_TEXT_CTRL 2003
312 class WXDLLEXPORT wxGridCell
: public wxObject
318 wxColour backgroundColour
;
319 wxBrush backgroundBrush
;
320 wxBitmap
* cellBitmap
;
321 void* cellData
; // intended for additional data associated with a cell
324 wxGridCell(wxGenericGrid
*window
= (wxGenericGrid
*) NULL
);
327 virtual wxString
& GetTextValue() const { return (wxString
&) textValue
; }
328 virtual void SetTextValue(const wxString
& str
) { textValue
= str
; }
329 wxFont
& GetFont() const { return (wxFont
&) font
; }
330 void SetFont(const wxFont
& f
) { font
= f
; }
331 wxColour
& GetTextColour() const { return (wxColour
&) textColour
; }
332 void SetTextColour(const wxColour
& colour
) { textColour
= colour
; }
333 wxColour
& GetBackgroundColour() const { return (wxColour
&) backgroundColour
; }
334 void SetBackgroundColour(const wxColour
& colour
);
335 wxBrush
& GetBackgroundBrush() const { return (wxBrush
&) backgroundBrush
; }
336 void SetBackgroundBrush(const wxBrush
& brush
) { backgroundBrush
= brush
; }
337 int GetAlignment() const { return alignment
; }
338 void SetAlignment(int align
) { alignment
= align
; }
339 wxBitmap
*GetCellBitmap() const { return cellBitmap
; }
340 void SetCellBitmap(wxBitmap
*bitmap
) { cellBitmap
= bitmap
; }
342 void *SetCellData(void *data
) { void *rc
= cellData
; cellData
= data
; return rc
; }
343 void *GetCellData() const { return cellData
; }
346 class WXDLLEXPORT wxGrid
: public wxGenericGrid
349 wxGrid() : wxGenericGrid() { }
350 wxGrid(wxWindow
*parent
, int x
=-1, int y
=-1, int width
=-1, int height
=-1,
351 long style
=0, char *name
= "gridWindow")
352 : wxGenericGrid(parent
, x
, y
, width
, height
, style
, name
)
355 wxGrid(wxWindow
*parent
, wxWindowID id
, const wxPoint
& pos
,
356 const wxSize
& size
, long style
= 0, const wxString
& name
= "grid")
357 : wxGenericGrid(parent
, id
, pos
, size
, style
, name
)
363 class WXDLLEXPORT wxGridEvent
: public wxCommandEvent
365 DECLARE_DYNAMIC_CLASS(wxGridEvent
)
369 : wxCommandEvent(), m_row(-1), m_col(-1), m_x(-1), m_y(-1),
370 m_control(0), m_shift(0), m_cell(0)
374 wxGridEvent(int id
, wxEventType type
, wxObject
* obj
,
375 int row
=-1, int col
=-1, int x
=-1, int y
=-1,
376 bool control
=FALSE
, bool shift
=FALSE
)
377 : wxCommandEvent(type
, id
), m_row(row
), m_col(col
), m_x(x
), m_y(y
),
378 m_control(control
), m_shift(shift
), m_cell(0)
392 int GetRow() const { return m_row
; }
393 int GetCol() const { return m_col
; }
394 wxPoint
GetPosition() const { return wxPoint( m_x
, m_y
); }
395 wxGridCell
* GetCell() const { return m_cell
; }
396 bool ControlDown() const { return m_control
; }
397 bool ShiftDown() const { return m_shift
; }
400 const wxEventType wxEVT_GRID_SELECT_CELL
= wxEVT_FIRST
+ 1575;
401 const wxEventType wxEVT_GRID_CREATE_CELL
= wxEVT_FIRST
+ 1576;
402 const wxEventType wxEVT_GRID_CHANGE_LABELS
= wxEVT_FIRST
+ 1577;
403 const wxEventType wxEVT_GRID_CHANGE_SEL_LABEL
= wxEVT_FIRST
+ 1578;
404 const wxEventType wxEVT_GRID_CELL_CHANGE
= wxEVT_FIRST
+ 1579;
405 const wxEventType wxEVT_GRID_CELL_LCLICK
= wxEVT_FIRST
+ 1580;
406 const wxEventType wxEVT_GRID_CELL_RCLICK
= wxEVT_FIRST
+ 1581;
407 const wxEventType wxEVT_GRID_LABEL_LCLICK
= wxEVT_FIRST
+ 1582;
408 const wxEventType wxEVT_GRID_LABEL_RCLICK
= wxEVT_FIRST
+ 1583;
411 typedef void (wxEvtHandler::*wxGridEventFunction
)(wxGridEvent
&);
413 #define EVT_GRID_SELECT_CELL(fn) { wxEVT_GRID_SELECT_CELL, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridEventFunction) &fn, NULL },
414 #define EVT_GRID_CREATE_CELL(fn) { wxEVT_GRID_CREATE_CELL, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridEventFunction) &fn, NULL },
415 #define EVT_GRID_CHANGE_LABELS(fn) { wxEVT_GRID_CHANGE_LABELS, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridEventFunction) &fn, NULL },
416 #define EVT_GRID_CHANGE_SEL_LABEL(fn) { wxEVT_GRID_CHANGE_SEL_LABEL, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridEventFunction) &fn, NULL },
417 #define EVT_GRID_CELL_CHANGE(fn) { wxEVT_GRID_CELL_CHANGE, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridEventFunction) &fn, NULL },
418 #define EVT_GRID_CELL_LCLICK(fn) { wxEVT_GRID_CELL_LCLICK, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridEventFunction) &fn, NULL },
419 #define EVT_GRID_CELL_RCLICK(fn) { wxEVT_GRID_CELL_RCLICK, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridEventFunction) &fn, NULL },
420 #define EVT_GRID_LABEL_LCLICK(fn) { wxEVT_GRID_LABEL_LCLICK, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridEventFunction) &fn, NULL },
421 #define EVT_GRID_LABEL_RCLICK(fn) { wxEVT_GRID_LABEL_RCLICK, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridEventFunction) &fn, NULL },
423 #endif // __GRIDH_G__