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
43 #define wxRIGHT 0x0800
46 #define WXGENERIC_GRID_VERSION 0.5
48 class WXDLLEXPORT wxGridEvent
;
49 class WXDLLEXPORT wxGridCell
;
51 class WXDLLEXPORT wxGenericGrid
: public wxPanel
53 DECLARE_DYNAMIC_CLASS(wxGenericGrid
)
58 wxGenericGrid(wxWindow
*parent
, int x
, int y
, int width
, int height
, long style
= 0, char *name
= "grid")
60 Create(parent
, -1, wxPoint(x
, y
), wxSize(width
, height
), style
, name
);
62 wxGenericGrid(wxWindow
*parent
, wxWindowID id
, const wxPoint
& pos
, const wxSize
& size
, long style
= 0, const wxString
& name
= "grid")
64 Create(parent
, id
, pos
, size
, style
, name
);
68 bool Create(wxWindow
*parent
, wxWindowID
, const wxPoint
& pos
, const wxSize
& size
, long style
= 0, const wxString
& name
= "grid");
70 bool CreateGrid(int nRows
, int nCols
, wxString
**cellValues
= (wxString
**) NULL
, short *widths
= (short *) NULL
,
71 short defaultWidth
= wxGRID_DEFAULT_CELL_WIDTH
, short defaultHeight
= wxGRID_DEFAULT_CELL_HEIGHT
);
72 void PaintGrid(wxDC
& dc
);
74 virtual wxGridCell
*GetCell(int row
, int col
) const;
75 wxGridCell
***GetCells() const { return m_gridCells
; }
76 bool InsertCols(int pos
= 0, int n
= 1, bool updateLabels
= TRUE
);
77 bool InsertRows(int pos
= 0, int n
= 1, bool updateLabels
= TRUE
);
78 bool AppendCols(int n
= 1, bool updateLabels
= TRUE
);
79 bool AppendRows(int n
= 1, bool updateLabels
= TRUE
);
80 bool DeleteCols(int pos
= 0, int n
= 1, bool updateLabels
= TRUE
);
81 bool DeleteRows(int pos
= 0, int n
= 1, bool updateLabels
= TRUE
);
84 void SetCellValue(const wxString
& val
, int row
, int col
);
85 wxString
& GetCellValue(int row
, int col
) const;
86 void SetCellAlignment(int flag
, int row
, int col
);
87 void SetCellAlignment(int flag
);
88 int GetCellAlignment(int row
, int col
) const;
89 int GetCellAlignment() const;
90 void SetCellTextColour(const wxColour
& val
, int row
, int col
);
91 void SetCellTextColour(const wxColour
& col
);
92 wxColour
& GetCellTextColour(int row
, int col
) const;
93 wxColour
& GetCellTextColour() const { return (wxColour
&) m_cellTextColour
; }
94 void SetCellBackgroundColour(const wxColour
& col
);
95 void SetCellBackgroundColour(const wxColour
& colour
, int row
, int col
);
96 wxColour
& GetCellBackgroundColour() const { return (wxColour
&) m_cellBackgroundColour
; }
97 wxColour
& GetCellBackgroundColour(int row
, int col
) const;
98 wxFont
& GetCellTextFont() const { return (wxFont
&) m_cellTextFont
; }
99 wxFont
& GetCellTextFont(int row
, int col
) const;
100 void SetCellTextFont(const wxFont
& fnt
);
101 void SetCellTextFont(const wxFont
& fnt
, int row
, int col
);
102 wxBitmap
*GetCellBitmap(int row
, int col
) const;
103 void SetCellBitmap(wxBitmap
*bitmap
, int row
, int col
);
106 void SetColumnWidth(int col
, int width
);
107 int GetColumnWidth(int col
) const;
108 void SetRowHeight(int row
, int height
);
109 int GetRowHeight(int row
) const;
112 void SetLabelSize(int orientation
, int sz
);
113 int GetLabelSize(int orientation
) const;
114 void SetLabelAlignment(int orientation
, int alignment
);
115 int GetLabelAlignment(int orientation
) const;
116 wxGridCell
*GetLabelCell(int orientation
, int pos
) const;
117 void SetLabelValue(int orientation
, const wxString
& val
, int pos
);
118 wxString
& GetLabelValue(int orientation
, int pos
) const;
119 void SetLabelTextColour(const wxColour
& colour
);
120 void SetLabelBackgroundColour(const wxColour
& colour
);
121 wxColour
& GetLabelTextColour() const { return (wxColour
&) m_labelTextColour
; }
122 wxColour
& GetLabelBackgroundColour() { return (wxColour
&) m_labelBackgroundColour
; }
123 wxFont
& GetLabelTextFont() { return (wxFont
&) m_labelTextFont
; }
124 void SetLabelTextFont(const wxFont
& fnt
) { m_labelTextFont
= fnt
; }
126 // Miscellaneous accessors
127 int GetCursorRow() const { return m_wCursorRow
; }
128 int GetCursorColumn() const { return m_wCursorColumn
; }
129 void SetGridCursor(int row
, int col
);
130 int GetRows() const { return m_totalRows
; }
131 int GetCols() const { return m_totalCols
; }
132 int GetScrollPosX() const { return m_scrollPosX
; }
133 int GetScrollPosY() const { return m_scrollPosY
; }
134 void SetScrollPosX(int pos
) { m_scrollPosX
= pos
; }
135 void SetScrollPosY(int pos
) { m_scrollPosY
= pos
; }
136 wxTextCtrl
*GetTextItem() const { return m_textItem
; }
137 wxScrollBar
*GetHorizScrollBar() const { return m_hScrollBar
; }
138 wxScrollBar
*GetVertScrollBar() const { return m_vScrollBar
; }
139 bool GetEditable() const { return m_editable
; }
140 void SetEditable(bool edit
);
142 bool GetEditInPlace() const { return m_editInPlace
; }
143 void SetEditInPlace(bool edit
= TRUE
);
145 wxRect
& GetCurrentRect() const { return (wxRect
&) m_currentRect
; }
146 bool CurrentCellVisible() const { return m_currentRectVisible
; }
147 void SetDividerPen(const wxPen
& pen
) { m_divisionPen
= pen
; }
148 wxPen
& GetDividerPen() const { return (wxPen
&) m_divisionPen
; }
150 // High-level event handling
151 // Override e.g. to check value of current cell; but call
152 // base member for default processing.
153 virtual void OnSelectCellImplementation(wxDC
*dc
, int row
, int col
);
155 virtual void OnSelectCell(int WXUNUSED(row
), int WXUNUSED(col
)) {};
156 void _OnSelectCell(wxGridEvent
& event
);
158 // Override to create your own class of grid cell
159 virtual wxGridCell
*OnCreateCell();
160 void _OnCreateCell(wxGridEvent
& event
);
162 // Override to change labels e.g. creation of grid, inserting/deleting a row/col.
163 // By default, auto-labels the grid.
164 virtual void OnChangeLabels();
165 void _OnChangeLabels(wxGridEvent
& event
);
167 // Override to change the label of the edit field when selecting a cell
168 // By default, sets it to e.g. A12
169 virtual void OnChangeSelectionLabel();
170 void _OnChangeSelectionLabel(wxGridEvent
& event
);
172 // Override for event processing
173 virtual void OnCellChange(int WXUNUSED(row
), int WXUNUSED(col
)) {};
174 virtual void OnCellLeftClick(int WXUNUSED(row
), int WXUNUSED(col
), int WXUNUSED(x
), int WXUNUSED(y
), bool WXUNUSED(control
), bool WXUNUSED(shift
)) {};
175 virtual void OnCellRightClick(int WXUNUSED(row
), int WXUNUSED(col
), int WXUNUSED(x
), int WXUNUSED(y
), bool WXUNUSED(control
), bool WXUNUSED(shift
)) {};
176 virtual void OnLabelLeftClick(int WXUNUSED(row
), int WXUNUSED(col
), int WXUNUSED(x
), int WXUNUSED(y
), bool WXUNUSED(control
), bool WXUNUSED(shift
)) {};
177 virtual void OnLabelRightClick(int WXUNUSED(row
), int WXUNUSED(col
), int WXUNUSED(x
), int WXUNUSED(y
), bool WXUNUSED(control
), bool WXUNUSED(shift
)) {};
179 void _OnCellChange(wxGridEvent
& event
);
180 void _OnCellLeftClick(wxGridEvent
& event
);
181 void _OnCellRightClick(wxGridEvent
& event
);
182 void _OnLabelLeftClick(wxGridEvent
& event
);
183 void _OnLabelRightClick(wxGridEvent
& event
);
185 // Activation: call from wxFrame::OnActivate
186 void OnActivate(bool active
);
189 void AdjustScrollbars();
190 void UpdateDimensions();
192 void SetCurrentRect (int Row
, int Column
, int canvasW
= -1, int canvasH
= -1);
193 void HighlightCell (wxDC
*dc
);
195 void SetGridClippingRegion(wxDC
*dc
);
197 virtual bool CellHitTest(int x
, int y
, int *row
, int *col
);
198 virtual bool LabelSashHitTest(int x
, int y
, int *orientation
, int *rowOrCol
, int *startPos
);
199 virtual bool LabelHitTest(int x
, int y
, int *row
, int *col
);
202 virtual void DrawLabelAreas(wxDC
*dc
);
203 virtual void DrawEditableArea(wxDC
*dc
);
204 virtual void DrawGridLines(wxDC
*dc
);
205 virtual void DrawColumnLabels(wxDC
*dc
);
206 virtual void DrawColumnLabel(wxDC
*dc
, wxRect
*rect
, int col
);
207 virtual void DrawRowLabels(wxDC
*dc
);
208 virtual void DrawRowLabel(wxDC
*dc
, wxRect
*rect
, int row
);
209 virtual void DrawCells(wxDC
*dc
);
210 virtual void DrawCellValue(wxDC
*dc
, wxRect
*rect
, int row
, int col
);
211 virtual void DrawCellBackground(wxDC
*dc
, wxRect
*rect
, int row
, int col
);
212 virtual void DrawTextRect(wxDC
*dc
, const wxString
& text
, wxRect
*rect
, int flag
);
213 virtual void DrawBitmapRect(wxDC
*dc
, wxBitmap
*bitmap
, wxRect
*rect
, int flag
);
215 // Refresh cell and optionally set the text field
216 void RefreshCell(int row
, int col
, bool setText
= FALSE
);
218 // Don't refresh within the outer pair of these.
219 void BeginBatch() { m_batchCount
++; }
220 void EndBatch() { m_batchCount
--; }
221 int GetBatchCount() { return m_batchCount
; }
223 // implementation from now on
225 void OnPaint(wxPaintEvent
& event
);
226 void OnEraseBackground(wxEraseEvent
& event
);
227 void OnMouseEvent(wxMouseEvent
& event
);
228 void OnSize(wxSizeEvent
& event
);
229 void OnText(wxCommandEvent
& ev
);
230 void OnTextEnter(wxCommandEvent
& ev
);
231 void OnTextInPlace(wxCommandEvent
& ev
);
232 void OnTextInPlaceEnter(wxCommandEvent
& ev
);
233 void OnGridScroll(wxScrollEvent
& ev
);
236 wxPanel
* m_editingPanel
; // Contains the text control
237 wxTextCtrl
* m_textItem
;
238 wxTextCtrl
* m_inPlaceTextItem
;
240 wxScrollBar
* m_hScrollBar
;
241 wxScrollBar
* m_vScrollBar
;
244 wxRect m_currentRect
;
245 bool m_currentRectVisible
;
246 wxGridCell
*** m_gridCells
;
247 wxGridCell
** m_rowLabelCells
;
248 wxGridCell
** m_colLabelCells
;
253 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_cellHeight
; // For now, a default
270 int m_verticalLabelWidth
;
271 int m_horizontalLabelHeight
;
272 int m_verticalLabelAlignment
;
273 int m_horizontalLabelAlignment
;
275 short* m_colWidths
; // Dynamically allocated
276 short* m_rowHeights
; // Dynamically allocated
277 int m_scrollWidth
; // Vert. scroll width, horiz. scroll height
280 wxColour m_cellTextColour
;
281 wxColour m_cellBackgroundColour
;
282 wxFont m_cellTextFont
;
283 wxColour m_labelTextColour
;
284 wxColour m_labelBackgroundColour
;
285 wxBrush m_labelBackgroundBrush
;
286 wxFont m_labelTextFont
;
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
;
323 wxGridCell(wxGenericGrid
*window
= (wxGenericGrid
*) NULL
);
326 virtual wxString
& GetTextValue() const { return (wxString
&) textValue
; }
327 virtual void SetTextValue(const wxString
& str
) { textValue
= str
; }
328 wxFont
& GetFont() const { return (wxFont
&) font
; }
329 void SetFont(const wxFont
& f
) { font
= f
; }
330 wxColour
& GetTextColour() const { return (wxColour
&) textColour
; }
331 void SetTextColour(const wxColour
& colour
) { textColour
= colour
; }
332 wxColour
& GetBackgroundColour() const { return (wxColour
&) backgroundColour
; }
333 void SetBackgroundColour(const wxColour
& colour
);
334 wxBrush
& GetBackgroundBrush() const { return (wxBrush
&) backgroundBrush
; }
335 void SetBackgroundBrush(const wxBrush
& brush
) { backgroundBrush
= brush
; }
336 int GetAlignment() const { return alignment
; }
337 void SetAlignment(int align
) { alignment
= align
; }
338 wxBitmap
*GetCellBitmap() const { return cellBitmap
; }
339 void SetCellBitmap(wxBitmap
*bitmap
) { cellBitmap
= bitmap
; }
342 class WXDLLEXPORT wxGrid
: public wxGenericGrid
345 wxGrid() : wxGenericGrid() { }
346 wxGrid(wxWindow
*parent
, int x
=-1, int y
=-1, int width
=-1, int height
=-1,
347 long style
=0, char *name
= "gridWindow")
348 : wxGenericGrid(parent
, x
, y
, width
, height
, style
, name
)
353 class WXDLLEXPORT wxGridEvent
: public wxCommandEvent
355 DECLARE_DYNAMIC_CLASS(wxGridEvent
)
359 : wxCommandEvent(), m_row(-1), m_col(-1), m_x(-1), m_y(-1),
360 m_control(0), m_shift(0), m_cell(0)
364 wxGridEvent(int id
, wxEventType type
, wxObject
* obj
,
365 int row
=-1, int col
=-1, int x
=-1, int y
=-1,
366 bool control
=FALSE
, bool shift
=FALSE
)
367 : wxCommandEvent(type
, id
), m_row(row
), m_col(col
), m_x(x
), m_y(y
),
368 m_control(control
), m_shift(shift
), m_cell(0)
383 const wxEventType wxEVT_GRID_SELECT_CELL
= wxEVT_FIRST
+ 1575;
384 const wxEventType wxEVT_GRID_CREATE_CELL
= wxEVT_FIRST
+ 1576;
385 const wxEventType wxEVT_GRID_CHANGE_LABELS
= wxEVT_FIRST
+ 1577;
386 const wxEventType wxEVT_GRID_CHANGE_SEL_LABEL
= wxEVT_FIRST
+ 1578;
387 const wxEventType wxEVT_GRID_CELL_CHANGE
= wxEVT_FIRST
+ 1579;
388 const wxEventType wxEVT_GRID_CELL_LCLICK
= wxEVT_FIRST
+ 1580;
389 const wxEventType wxEVT_GRID_CELL_RCLICK
= wxEVT_FIRST
+ 1581;
390 const wxEventType wxEVT_GRID_LABEL_LCLICK
= wxEVT_FIRST
+ 1582;
391 const wxEventType wxEVT_GRID_LABEL_RCLICK
= wxEVT_FIRST
+ 1583;
394 typedef void (wxEvtHandler::*wxGridEventFunction
)(wxGridEvent
&);
396 #define EVT_GRID_SELECT_CELL(fn) { wxEVT_GRID_SELECT_CELL, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridEventFunction) &fn, NULL },
397 #define EVT_GRID_CREATE_CELL(fn) { wxEVT_GRID_CREATE_CELL, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridEventFunction) &fn, NULL },
398 #define EVT_GRID_CHANGE_LABELS(fn) { wxEVT_GRID_CHANGE_LABELS, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridEventFunction) &fn, NULL },
399 #define EVT_GRID_CHANGE_SEL_LABEL(fn) { wxEVT_GRID_CHANGE_SEL_LABEL, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridEventFunction) &fn, NULL },
400 #define EVT_GRID_CELL_CHANGE(fn) { wxEVT_GRID_CELL_CHANGE, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridEventFunction) &fn, NULL },
401 #define EVT_GRID_CELL_LCLICK(fn) { wxEVT_GRID_CELL_LCLICK, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridEventFunction) &fn, NULL },
402 #define EVT_GRID_CELL_RCLICK(fn) { wxEVT_GRID_CELL_RCLICK, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridEventFunction) &fn, NULL },
403 #define EVT_GRID_LABEL_LCLICK(fn) { wxEVT_GRID_LABEL_LCLICK, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridEventFunction) &fn, NULL },
404 #define EVT_GRID_LABEL_RCLICK(fn) { wxEVT_GRID_LABEL_RCLICK, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridEventFunction) &fn, NULL },
406 #endif // __GRIDH_G__