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
);
104 void *SetCellData(void *data
, int row
, int col
);
105 void *GetCellData(int row
, int col
);
108 void SetColumnWidth(int col
, int width
);
109 int GetColumnWidth(int col
) const;
110 void SetRowHeight(int row
, int height
);
111 int GetRowHeight(int row
) const;
112 int GetViewHeight() const { return m_viewHeight
; }
113 int GetViewWidth() const { return m_viewWidth
; }
116 void SetLabelSize(int orientation
, int sz
);
117 int GetLabelSize(int orientation
) const;
118 void SetLabelAlignment(int orientation
, int alignment
);
119 int GetLabelAlignment(int orientation
) const;
120 wxGridCell
*GetLabelCell(int orientation
, int pos
) const;
121 void SetLabelValue(int orientation
, const wxString
& val
, int pos
);
122 wxString
& GetLabelValue(int orientation
, int pos
) const;
123 void SetLabelTextColour(const wxColour
& colour
);
124 void SetLabelBackgroundColour(const wxColour
& colour
);
125 wxColour
& GetLabelTextColour() const { return (wxColour
&) m_labelTextColour
; }
126 wxColour
& GetLabelBackgroundColour() { return (wxColour
&) m_labelBackgroundColour
; }
127 wxFont
& GetLabelTextFont() { return (wxFont
&) m_labelTextFont
; }
128 void SetLabelTextFont(const wxFont
& fnt
) { m_labelTextFont
= fnt
; }
130 // Miscellaneous accessors
131 int GetCursorRow() const { return m_wCursorRow
; }
132 int GetCursorColumn() const { return m_wCursorColumn
; }
133 void SetGridCursor(int row
, int col
);
134 int GetRows() const { return m_totalRows
; }
135 int GetCols() const { return m_totalCols
; }
136 int GetScrollPosX() const { return m_scrollPosX
; }
137 int GetScrollPosY() const { return m_scrollPosY
; }
138 void SetScrollPosX(int pos
) { m_scrollPosX
= pos
; }
139 void SetScrollPosY(int pos
) { m_scrollPosY
= pos
; }
140 wxTextCtrl
*GetTextItem() const { return m_textItem
; }
141 wxScrollBar
*GetHorizScrollBar() const { return m_hScrollBar
; }
142 wxScrollBar
*GetVertScrollBar() const { return m_vScrollBar
; }
143 bool GetEditable() const { return m_editable
; }
144 void SetEditable(bool edit
);
146 bool GetEditInPlace() const { return m_editInPlace
; }
147 void SetEditInPlace(bool edit
= TRUE
);
149 wxRect
& GetCurrentRect() const { return (wxRect
&) m_currentRect
; }
150 bool CurrentCellVisible() const { return m_currentRectVisible
; }
151 void SetDividerPen(const wxPen
& pen
) { m_divisionPen
= pen
; }
152 wxPen
& GetDividerPen() const { return (wxPen
&) m_divisionPen
; }
154 // High-level event handling
155 // Override e.g. to check value of current cell; but call
156 // base member for default processing.
157 virtual void OnSelectCellImplementation(wxDC
*dc
, int row
, int col
);
159 virtual void OnSelectCell(int WXUNUSED(row
), int WXUNUSED(col
)) {};
160 void _OnSelectCell(wxGridEvent
& event
);
162 // Override to create your own class of grid cell
163 virtual wxGridCell
*OnCreateCell();
164 void _OnCreateCell(wxGridEvent
& event
);
166 // Override to change labels e.g. creation of grid, inserting/deleting a row/col.
167 // By default, auto-labels the grid.
168 virtual void OnChangeLabels();
169 void _OnChangeLabels(wxGridEvent
& event
);
171 // Override to change the label of the edit field when selecting a cell
172 // By default, sets it to e.g. A12
173 virtual void OnChangeSelectionLabel();
174 void _OnChangeSelectionLabel(wxGridEvent
& event
);
176 // Override for event processing
177 virtual void OnCellChange(int WXUNUSED(row
), int WXUNUSED(col
)) {};
178 virtual void OnCellLeftClick(int WXUNUSED(row
), int WXUNUSED(col
), int WXUNUSED(x
), int WXUNUSED(y
), bool WXUNUSED(control
), bool WXUNUSED(shift
)) {};
179 virtual void OnCellRightClick(int WXUNUSED(row
), int WXUNUSED(col
), int WXUNUSED(x
), int WXUNUSED(y
), bool WXUNUSED(control
), bool WXUNUSED(shift
)) {};
180 virtual void OnLabelLeftClick(int WXUNUSED(row
), int WXUNUSED(col
), int WXUNUSED(x
), int WXUNUSED(y
), bool WXUNUSED(control
), bool WXUNUSED(shift
)) {};
181 virtual void OnLabelRightClick(int WXUNUSED(row
), int WXUNUSED(col
), int WXUNUSED(x
), int WXUNUSED(y
), bool WXUNUSED(control
), bool WXUNUSED(shift
)) {};
183 void _OnCellChange(wxGridEvent
& event
);
184 void _OnCellLeftClick(wxGridEvent
& event
);
185 void _OnCellRightClick(wxGridEvent
& event
);
186 void _OnLabelLeftClick(wxGridEvent
& event
);
187 void _OnLabelRightClick(wxGridEvent
& event
);
189 // Activation: call from wxFrame::OnActivate
190 void OnActivate(bool active
);
193 void AdjustScrollbars();
194 void UpdateDimensions();
196 void SetCurrentRect (int Row
, int Column
, int canvasW
= -1, int canvasH
= -1);
197 void HighlightCell (wxDC
*dc
);
199 void SetGridClippingRegion(wxDC
*dc
);
201 virtual bool CellHitTest(int x
, int y
, int *row
, int *col
);
202 virtual bool LabelSashHitTest(int x
, int y
, int *orientation
, int *rowOrCol
, int *startPos
);
203 virtual bool LabelHitTest(int x
, int y
, int *row
, int *col
);
206 virtual void DrawLabelAreas(wxDC
*dc
);
207 virtual void DrawEditableArea(wxDC
*dc
);
208 virtual void DrawGridLines(wxDC
*dc
);
209 virtual void DrawColumnLabels(wxDC
*dc
);
210 virtual void DrawColumnLabel(wxDC
*dc
, wxRect
*rect
, int col
);
211 virtual void DrawRowLabels(wxDC
*dc
);
212 virtual void DrawRowLabel(wxDC
*dc
, wxRect
*rect
, int row
);
213 virtual void DrawCells(wxDC
*dc
);
214 virtual void DrawCellValue(wxDC
*dc
, wxRect
*rect
, int row
, int col
);
215 virtual void DrawCellBackground(wxDC
*dc
, wxRect
*rect
, int row
, int col
);
216 virtual void DrawTextRect(wxDC
*dc
, const wxString
& text
, wxRect
*rect
, int flag
);
217 virtual void DrawBitmapRect(wxDC
*dc
, wxBitmap
*bitmap
, wxRect
*rect
, int flag
);
219 // Refresh cell and optionally set the text field
220 void RefreshCell(int row
, int col
, bool setText
= FALSE
);
222 // Don't refresh within the outer pair of these.
223 void BeginBatch() { m_batchCount
++; }
224 void EndBatch() { m_batchCount
--; }
225 int GetBatchCount() { return m_batchCount
; }
227 // implementation from now on
229 void OnPaint(wxPaintEvent
& event
);
230 void OnEraseBackground(wxEraseEvent
& event
);
231 void OnMouseEvent(wxMouseEvent
& event
);
232 void OnSize(wxSizeEvent
& event
);
233 void OnText(wxCommandEvent
& ev
);
234 void OnTextEnter(wxCommandEvent
& ev
);
235 void OnTextInPlace(wxCommandEvent
& ev
);
236 void OnTextInPlaceEnter(wxCommandEvent
& ev
);
237 void OnGridScroll(wxScrollEvent
& ev
);
240 wxPanel
* m_editingPanel
; // Contains the text control
241 wxTextCtrl
* m_textItem
;
242 wxTextCtrl
* m_inPlaceTextItem
;
244 wxScrollBar
* m_hScrollBar
;
245 wxScrollBar
* m_vScrollBar
;
248 wxRect m_currentRect
;
249 bool m_currentRectVisible
;
250 wxGridCell
*** m_gridCells
;
251 wxGridCell
** m_rowLabelCells
;
252 wxGridCell
** m_colLabelCells
;
257 bool m_inOnTextInPlace
;
262 // Row and column we're currently looking at
269 int m_rightOfSheet
; // Calculated from m_colWidths
270 int m_bottomOfSheet
; // Calculated from m_rowHeights
271 int m_totalGridWidth
; // Total 'virtual' size
272 int m_totalGridHeight
;
273 int m_viewHeight
; // Number of rows displayed
274 int m_viewWidth
; // Number of columns displayed
275 int m_cellHeight
; // For now, a default
276 int m_verticalLabelWidth
;
277 int m_horizontalLabelHeight
;
278 int m_verticalLabelAlignment
;
279 int m_horizontalLabelAlignment
;
281 short* m_colWidths
; // Dynamically allocated
282 short* m_rowHeights
; // Dynamically allocated
283 int m_scrollWidth
; // Vert. scroll width, horiz. scroll height
286 wxColour m_cellTextColour
;
287 wxColour m_cellBackgroundColour
;
288 wxFont m_cellTextFont
;
289 wxColour m_labelTextColour
;
290 wxColour m_labelBackgroundColour
;
291 wxBrush m_labelBackgroundBrush
;
292 wxFont m_labelTextFont
;
294 wxBitmap
* m_doubleBufferingBitmap
;
296 // Position of Edit control
297 wxRect m_editControlPosition
;
302 int m_dragStartPosition
;
303 int m_dragLastPosition
;
304 wxCursor m_horizontalSashCursor
;
305 wxCursor m_verticalSashCursor
;
307 // To avoid multiple refreshes, use Begin/EndBatch
310 DECLARE_EVENT_TABLE()
313 #define wxGRID_TEXT_CTRL 2000
314 #define wxGRID_HSCROLL 2001
315 #define wxGRID_VSCROLL 2002
316 #define wxGRID_EDIT_IN_PLACE_TEXT_CTRL 2003
318 class WXDLLEXPORT wxGridCell
: public wxObject
324 wxColour backgroundColour
;
325 wxBrush backgroundBrush
;
326 wxBitmap
* cellBitmap
;
327 void* cellData
; // intended for additional data associated with a cell
330 wxGridCell(wxGenericGrid
*window
= (wxGenericGrid
*) NULL
);
333 virtual wxString
& GetTextValue() const { return (wxString
&) textValue
; }
334 virtual void SetTextValue(const wxString
& str
) { textValue
= str
; }
335 wxFont
& GetFont() const { return (wxFont
&) font
; }
336 void SetFont(const wxFont
& f
) { font
= f
; }
337 wxColour
& GetTextColour() const { return (wxColour
&) textColour
; }
338 void SetTextColour(const wxColour
& colour
) { textColour
= colour
; }
339 wxColour
& GetBackgroundColour() const { return (wxColour
&) backgroundColour
; }
340 void SetBackgroundColour(const wxColour
& colour
);
341 wxBrush
& GetBackgroundBrush() const { return (wxBrush
&) backgroundBrush
; }
342 void SetBackgroundBrush(const wxBrush
& brush
) { backgroundBrush
= brush
; }
343 int GetAlignment() const { return alignment
; }
344 void SetAlignment(int align
) { alignment
= align
; }
345 wxBitmap
*GetCellBitmap() const { return cellBitmap
; }
346 void SetCellBitmap(wxBitmap
*bitmap
) { cellBitmap
= bitmap
; }
348 void *SetCellData(void *data
) { void *rc
= cellData
; cellData
= data
; return rc
; }
349 void *GetCellData() const { return cellData
; }
352 class WXDLLEXPORT wxGrid
: public wxGenericGrid
355 wxGrid() : wxGenericGrid() { }
356 wxGrid(wxWindow
*parent
, int x
=-1, int y
=-1, int width
=-1, int height
=-1,
357 long style
=0, char *name
= "gridWindow")
358 : wxGenericGrid(parent
, x
, y
, width
, height
, 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)
393 const wxEventType wxEVT_GRID_SELECT_CELL
= wxEVT_FIRST
+ 1575;
394 const wxEventType wxEVT_GRID_CREATE_CELL
= wxEVT_FIRST
+ 1576;
395 const wxEventType wxEVT_GRID_CHANGE_LABELS
= wxEVT_FIRST
+ 1577;
396 const wxEventType wxEVT_GRID_CHANGE_SEL_LABEL
= wxEVT_FIRST
+ 1578;
397 const wxEventType wxEVT_GRID_CELL_CHANGE
= wxEVT_FIRST
+ 1579;
398 const wxEventType wxEVT_GRID_CELL_LCLICK
= wxEVT_FIRST
+ 1580;
399 const wxEventType wxEVT_GRID_CELL_RCLICK
= wxEVT_FIRST
+ 1581;
400 const wxEventType wxEVT_GRID_LABEL_LCLICK
= wxEVT_FIRST
+ 1582;
401 const wxEventType wxEVT_GRID_LABEL_RCLICK
= wxEVT_FIRST
+ 1583;
404 typedef void (wxEvtHandler::*wxGridEventFunction
)(wxGridEvent
&);
406 #define EVT_GRID_SELECT_CELL(fn) { wxEVT_GRID_SELECT_CELL, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridEventFunction) &fn, NULL },
407 #define EVT_GRID_CREATE_CELL(fn) { wxEVT_GRID_CREATE_CELL, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridEventFunction) &fn, NULL },
408 #define EVT_GRID_CHANGE_LABELS(fn) { wxEVT_GRID_CHANGE_LABELS, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridEventFunction) &fn, NULL },
409 #define EVT_GRID_CHANGE_SEL_LABEL(fn) { wxEVT_GRID_CHANGE_SEL_LABEL, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridEventFunction) &fn, NULL },
410 #define EVT_GRID_CELL_CHANGE(fn) { wxEVT_GRID_CELL_CHANGE, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridEventFunction) &fn, NULL },
411 #define EVT_GRID_CELL_LCLICK(fn) { wxEVT_GRID_CELL_LCLICK, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridEventFunction) &fn, NULL },
412 #define EVT_GRID_CELL_RCLICK(fn) { wxEVT_GRID_CELL_RCLICK, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridEventFunction) &fn, NULL },
413 #define EVT_GRID_LABEL_LCLICK(fn) { wxEVT_GRID_LABEL_LCLICK, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridEventFunction) &fn, NULL },
414 #define EVT_GRID_LABEL_RCLICK(fn) { wxEVT_GRID_LABEL_RCLICK, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridEventFunction) &fn, NULL },
416 #endif // __GRIDH_G__