1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxGenericGrid
4 // Author: Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
16 #pragma interface "gridg.h"
21 #include "wx/string.h"
22 #include "wx/scrolbar.h"
25 #define wxGRID_DEFAULT_EDIT_WIDTH 300
26 #define wxGRID_DEFAULT_EDIT_HEIGHT 27
27 #define wxGRID_DEFAULT_EDIT_X 2
28 #define wxGRID_DEFAULT_EDIT_Y 1
29 #define wxGRID_DEFAULT_SHEET_TOP 31
30 #define wxGRID_DEFAULT_SHEET_LEFT 0
31 #define wxGRID_DEFAULT_CELL_HEIGHT 20
32 #define wxGRID_DEFAULT_CELL_WIDTH 80
33 #define wxGRID_DEFAULT_VERTICAL_LABEL_WIDTH 40
34 #define wxGRID_DEFAULT_HORIZONAL_LABEL_HEIGHT 20
41 #define wxRIGHT 0x0800
44 #define WXGENERIC_GRID_VERSION 0.5
46 class WXDLLEXPORT wxGridEvent
;
47 class WXDLLEXPORT wxGridCell
;
48 class WXDLLEXPORT wxGenericGrid
: public wxPanel
50 DECLARE_DYNAMIC_CLASS(wxGenericGrid
)
54 inline wxGenericGrid(wxWindow
*parent
, int x
, int y
, int width
, int height
, long style
= 0, char *name
= "grid")
56 Create(parent
, -1, wxPoint(x
, y
), wxSize(width
, height
), style
, name
);
58 inline wxGenericGrid(wxWindow
*parent
, wxWindowID id
, const wxPoint
& pos
, const wxSize
& size
, long style
= 0, const wxString
& name
= "grid")
60 Create(parent
, id
, pos
, size
, style
, name
);
64 void OnPaint(wxPaintEvent
& event
);
65 void OnEraseBackground(wxEraseEvent
& event
);
66 void OnMouseEvent(wxMouseEvent
& event
);
67 void OnSize(wxSizeEvent
& event
);
69 bool Create(wxWindow
*parent
, wxWindowID
, const wxPoint
& pos
, const wxSize
& size
, long style
= 0, const wxString
& name
= "grid");
71 bool CreateGrid(int nRows
, int nCols
, wxString
**cellValues
= (wxString
**) NULL
, short *widths
= (short *) NULL
,
72 short defaultWidth
= wxGRID_DEFAULT_CELL_WIDTH
, short defaultHeight
= wxGRID_DEFAULT_CELL_HEIGHT
);
73 void PaintGrid(wxDC
& dc
);
75 virtual wxGridCell
*GetCell(int row
, int col
) const;
76 inline wxGridCell
***GetCells(void) const { return m_gridCells
; }
77 bool InsertCols(int pos
= 0, int n
= 1, bool updateLabels
= TRUE
);
78 bool InsertRows(int pos
= 0, int n
= 1, bool updateLabels
= TRUE
);
79 bool AppendCols(int n
= 1, bool updateLabels
= TRUE
);
80 bool AppendRows(int n
= 1, bool updateLabels
= TRUE
);
81 bool DeleteCols(int pos
= 0, int n
= 1, bool updateLabels
= TRUE
);
82 bool DeleteRows(int pos
= 0, int n
= 1, bool updateLabels
= TRUE
);
85 void SetCellValue(const wxString
& val
, int row
, int col
);
86 wxString
& GetCellValue(int row
, int col
) const;
87 void SetCellAlignment(int flag
, int row
, int col
);
88 void SetCellAlignment(int flag
);
89 int GetCellAlignment(int row
, int col
) const;
90 int GetCellAlignment(void) const;
91 void SetCellTextColour(const wxColour
& val
, int row
, int col
);
92 void SetCellTextColour(const wxColour
& col
);
93 wxColour
& GetCellTextColour(int row
, int col
) const;
94 inline wxColour
& GetCellTextColour(void) const { return (wxColour
&) m_cellTextColour
; }
95 void SetCellBackgroundColour(const wxColour
& col
);
96 void SetCellBackgroundColour(const wxColour
& colour
, int row
, int col
);
97 inline wxColour
& GetCellBackgroundColour(void) const { return (wxColour
&) m_cellBackgroundColour
; }
98 wxColour
& GetCellBackgroundColour(int row
, int col
) const;
99 inline wxFont
& GetCellTextFont(void) const { return (wxFont
&) m_cellTextFont
; }
100 wxFont
& GetCellTextFont(int row
, int col
) const;
101 void SetCellTextFont(const wxFont
& fnt
);
102 void SetCellTextFont(const wxFont
& fnt
, int row
, int col
);
103 wxBitmap
*GetCellBitmap(int row
, int col
) const;
104 void SetCellBitmap(wxBitmap
*bitmap
, int row
, int col
);
107 void SetColumnWidth(int col
, int width
);
108 int GetColumnWidth(int col
) const;
109 void SetRowHeight(int row
, int height
);
110 int GetRowHeight(int row
) const;
113 void SetLabelSize(int orientation
, int sz
);
114 int GetLabelSize(int orientation
) const;
115 void SetLabelAlignment(int orientation
, int alignment
);
116 int GetLabelAlignment(int orientation
) const;
117 wxGridCell
*GetLabelCell(int orientation
, int pos
) const;
118 void SetLabelValue(int orientation
, const wxString
& val
, int pos
);
119 wxString
& GetLabelValue(int orientation
, int pos
) const;
120 void SetLabelTextColour(const wxColour
& colour
);
121 void SetLabelBackgroundColour(const wxColour
& colour
);
122 inline wxColour
& GetLabelTextColour(void) const { return (wxColour
&) m_labelTextColour
; }
123 inline wxColour
& GetLabelBackgroundColour(void) { return (wxColour
&) m_labelBackgroundColour
; }
124 inline wxFont
& GetLabelTextFont(void) { return (wxFont
&) m_labelTextFont
; }
125 inline void SetLabelTextFont(const wxFont
& fnt
) { m_labelTextFont
= fnt
; }
127 // Miscellaneous accessors
128 inline int GetCursorRow(void) const { return m_wCursorRow
; }
129 inline int GetCursorColumn(void) const { return m_wCursorColumn
; }
130 void SetGridCursor(int row
, int col
);
131 inline int GetRows(void) const { return m_totalRows
; }
132 inline int GetCols(void) const { return m_totalCols
; }
133 inline int GetScrollPosX(void) const { return m_scrollPosX
; }
134 inline int GetScrollPosY(void) const { return m_scrollPosY
; }
135 inline void SetScrollPosX(int pos
) { m_scrollPosX
= pos
; }
136 inline void SetScrollPosY(int pos
) { m_scrollPosY
= pos
; }
137 inline wxTextCtrl
*GetTextItem(void) const { return m_textItem
; }
138 inline wxScrollBar
*GetHorizScrollBar(void) const { return m_hScrollBar
; }
139 inline wxScrollBar
*GetVertScrollBar(void) const { return m_vScrollBar
; }
140 inline bool GetEditable(void) const { return m_editable
; }
141 void SetEditable(bool edit
);
142 inline wxRect
& GetCurrentRect(void) const { return (wxRect
&) m_currentRect
; }
143 inline bool CurrentCellVisible(void) const { return m_currentRectVisible
; }
144 inline void SetDividerPen(const wxPen
& pen
) { m_divisionPen
= pen
; }
145 inline wxPen
& GetDividerPen(void) const { return (wxPen
&) m_divisionPen
; }
147 // High-level event handling
148 // Override e.g. to check value of current cell; but call
149 // base member for default processing.
150 virtual void OnSelectCellImplementation(wxDC
*dc
, int row
, int col
);
152 virtual void OnSelectCell(int WXUNUSED(row
), int WXUNUSED(col
)) {};
153 void _OnSelectCell(wxGridEvent
& event
);
155 // Override to create your own class of grid cell
156 virtual wxGridCell
*OnCreateCell(void);
157 void _OnCreateCell(wxGridEvent
& event
);
159 // Override to change labels e.g. creation of grid, inserting/deleting a row/col.
160 // By default, auto-labels the grid.
161 virtual void OnChangeLabels(void);
162 void _OnChangeLabels(wxGridEvent
& event
);
164 // Override to change the label of the edit field when selecting a cell
165 // By default, sets it to e.g. A12
166 virtual void OnChangeSelectionLabel(void);
167 void _OnChangeSelectionLabel(wxGridEvent
& event
);
169 // Override for event processing
170 virtual void OnCellChange(int WXUNUSED(row
), int WXUNUSED(col
)) {};
171 virtual void OnCellLeftClick(int WXUNUSED(row
), int WXUNUSED(col
), int WXUNUSED(x
), int WXUNUSED(y
), bool WXUNUSED(control
), bool WXUNUSED(shift
)) {};
172 virtual void OnCellRightClick(int WXUNUSED(row
), int WXUNUSED(col
), int WXUNUSED(x
), int WXUNUSED(y
), bool WXUNUSED(control
), bool WXUNUSED(shift
)) {};
173 virtual void OnLabelLeftClick(int WXUNUSED(row
), int WXUNUSED(col
), int WXUNUSED(x
), int WXUNUSED(y
), bool WXUNUSED(control
), bool WXUNUSED(shift
)) {};
174 virtual void OnLabelRightClick(int WXUNUSED(row
), int WXUNUSED(col
), int WXUNUSED(x
), int WXUNUSED(y
), bool WXUNUSED(control
), bool WXUNUSED(shift
)) {};
176 void _OnCellChange(wxGridEvent
& event
);
177 void _OnCellLeftClick(wxGridEvent
& event
);
178 void _OnCellRightClick(wxGridEvent
& event
);
179 void _OnLabelLeftClick(wxGridEvent
& event
);
180 void _OnLabelRightClick(wxGridEvent
& event
);
182 // Activation: call from wxFrame::OnActivate
183 void OnActivate(bool active
);
186 void AdjustScrollbars(void);
187 void UpdateDimensions(void);
191 void SetCurrentRect (int Row
, int Column
, int canvasW
= -1, int canvasH
= -1);
192 void HighlightCell (wxDC
*dc
);
193 void DrawCellText(void);
194 void SetGridClippingRegion(wxDC
*dc
);
195 virtual bool CellHitTest(int x
, int y
, int *row
, int *col
);
196 virtual bool LabelSashHitTest(int x
, int y
, int *orientation
, int *rowOrCol
, int *startPos
);
197 virtual bool LabelHitTest(int x
, int y
, int *row
, int *col
);
199 virtual void DrawLabelAreas(wxDC
*dc
);
200 virtual void DrawEditableArea(wxDC
*dc
);
201 virtual void DrawGridLines(wxDC
*dc
);
202 virtual void DrawColumnLabels(wxDC
*dc
);
203 virtual void DrawColumnLabel(wxDC
*dc
, wxRect
*rect
, int col
);
204 virtual void DrawRowLabels(wxDC
*dc
);
205 virtual void DrawRowLabel(wxDC
*dc
, wxRect
*rect
, int row
);
206 virtual void DrawCells(wxDC
*dc
);
207 virtual void DrawCellValue(wxDC
*dc
, wxRect
*rect
, int row
, int col
);
208 virtual void DrawCellBackground(wxDC
*dc
, wxRect
*rect
, int row
, int col
);
209 virtual void DrawTextRect(wxDC
*dc
, const wxString
& text
, wxRect
*rect
, int flag
);
210 virtual void DrawBitmapRect(wxDC
*dc
, wxBitmap
*bitmap
, wxRect
*rect
, int flag
);
212 // Refresh cell and optionally set the text field
213 void RefreshCell(int row
, int col
, bool setText
= FALSE
);
215 // Don't refresh within the outer pair of these.
216 inline void BeginBatch(void) { m_batchCount
++; }
217 inline void EndBatch(void) { m_batchCount
--; }
218 inline int GetBatchCount(void) { return m_batchCount
; }
220 void OnText(wxCommandEvent
& ev
);
221 void OnGridScroll(wxScrollEvent
& ev
);
224 wxPanel
* m_editingPanel
; // Contains the text control
225 wxTextCtrl
* m_textItem
;
226 wxScrollBar
* m_hScrollBar
;
227 wxScrollBar
* m_vScrollBar
;
230 wxRect m_currentRect
;
231 bool m_currentRectVisible
;
232 wxGridCell
*** m_gridCells
;
233 wxGridCell
** m_rowLabelCells
;
234 wxGridCell
** m_colLabelCells
;
241 // Row and column we're currently looking at
248 int m_rightOfSheet
; // Calculated from m_colWidths
249 int m_bottomOfSheet
; // Calculated from m_rowHeights
250 int m_totalGridWidth
; // Total 'virtual' size
251 int m_totalGridHeight
;
252 int m_cellHeight
; // For now, a default
253 int m_verticalLabelWidth
;
254 int m_horizontalLabelHeight
;
255 int m_verticalLabelAlignment
;
256 int m_horizontalLabelAlignment
;
258 short* m_colWidths
; // Dynamically allocated
259 short* m_rowHeights
; // Dynamically allocated
260 int m_scrollWidth
; // Vert. scroll width, horiz. scroll height
263 wxColour m_cellTextColour
;
264 wxColour m_cellBackgroundColour
;
265 wxFont m_cellTextFont
;
266 wxColour m_labelTextColour
;
267 wxColour m_labelBackgroundColour
;
268 wxBrush m_labelBackgroundBrush
;
269 wxFont m_labelTextFont
;
271 wxBitmap
* m_doubleBufferingBitmap
;
273 // Position of Edit control
274 wxRect m_editControlPosition
;
279 int m_dragStartPosition
;
280 int m_dragLastPosition
;
281 wxCursor m_horizontalSashCursor
;
282 wxCursor m_verticalSashCursor
;
284 // To avoid multiple refreshes, use Begin/EndBatch
287 DECLARE_EVENT_TABLE()
290 #define wxGRID_TEXT_CTRL 2000
291 #define wxGRID_HSCROLL 2001
292 #define wxGRID_VSCROLL 2002
294 class WXDLLEXPORT wxGridCell
: public wxObject
300 wxColour backgroundColour
;
301 wxBrush backgroundBrush
;
302 wxBitmap
* cellBitmap
;
305 wxGridCell(wxGenericGrid
*window
= (wxGenericGrid
*) NULL
);
308 virtual wxString
& GetTextValue(void) const { return (wxString
&) textValue
; }
309 virtual void SetTextValue(const wxString
& str
) { textValue
= str
; }
310 inline wxFont
& GetFont(void) const { return (wxFont
&) font
; }
311 inline void SetFont(const wxFont
& f
) { font
= f
; }
312 inline wxColour
& GetTextColour(void) const { return (wxColour
&) textColour
; }
313 inline void SetTextColour(const wxColour
& colour
) { textColour
= colour
; }
314 inline wxColour
& GetBackgroundColour(void) const { return (wxColour
&) backgroundColour
; }
315 void SetBackgroundColour(const wxColour
& colour
);
316 inline wxBrush
& GetBackgroundBrush(void) const { return (wxBrush
&) backgroundBrush
; }
317 inline void SetBackgroundBrush(const wxBrush
& brush
) { backgroundBrush
= brush
; }
318 inline int GetAlignment(void) const { return alignment
; }
319 inline void SetAlignment(int align
) { alignment
= align
; }
320 inline wxBitmap
*GetCellBitmap(void) const { return cellBitmap
; }
321 inline void SetCellBitmap(wxBitmap
*bitmap
) { cellBitmap
= bitmap
; }
324 class WXDLLEXPORT wxGrid
: public wxGenericGrid
327 wxGrid(void):wxGenericGrid() {}
328 wxGrid(wxWindow
*parent
, int x
=-1, int y
=-1, int width
=-1, int height
=-1,
329 long style
=0, char *name
= "gridWindow"):
330 wxGenericGrid(parent
, x
, y
, width
, height
, style
, name
)
335 class WXDLLEXPORT wxGridEvent
: public wxCommandEvent
{
336 DECLARE_DYNAMIC_CLASS(wxGridEvent
)
339 : wxCommandEvent(), m_row(-1), m_col(-1), m_x(-1), m_y(-1),
340 m_control(0), m_shift(0), m_cell(0)
343 wxGridEvent(int id
, wxEventType type
, wxObject
* obj
,
344 int row
=-1, int col
=-1, int x
=-1, int y
=-1,
345 bool control
=FALSE
, bool shift
=FALSE
)
346 : wxCommandEvent(type
, id
), m_row(row
), m_col(col
), m_x(x
), m_y(y
),
347 m_control(control
), m_shift(shift
), m_cell(0)
362 const wxEventType wxEVT_GRID_SELECT_CELL
= wxEVT_FIRST
+ 1575;
363 const wxEventType wxEVT_GRID_CREATE_CELL
= wxEVT_FIRST
+ 1576;
364 const wxEventType wxEVT_GRID_CHANGE_LABELS
= wxEVT_FIRST
+ 1577;
365 const wxEventType wxEVT_GRID_CHANGE_SEL_LABEL
= wxEVT_FIRST
+ 1578;
366 const wxEventType wxEVT_GRID_CELL_CHANGE
= wxEVT_FIRST
+ 1579;
367 const wxEventType wxEVT_GRID_CELL_LCLICK
= wxEVT_FIRST
+ 1580;
368 const wxEventType wxEVT_GRID_CELL_RCLICK
= wxEVT_FIRST
+ 1581;
369 const wxEventType wxEVT_GRID_LABEL_LCLICK
= wxEVT_FIRST
+ 1582;
370 const wxEventType wxEVT_GRID_LABEL_RCLICK
= wxEVT_FIRST
+ 1583;
373 typedef void (wxEvtHandler::*wxGridEventFunction
)(wxGridEvent
&);
375 #define EVT_GRID_SELECT_CELL(fn) { wxEVT_GRID_SELECT_CELL, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridEventFunction) &fn, NULL },
376 #define EVT_GRID_CREATE_CELL(fn) { wxEVT_GRID_CREATE_CELL, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridEventFunction) &fn, NULL },
377 #define EVT_GRID_CHANGE_LABELS(fn) { wxEVT_GRID_CHANGE_LABELS, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridEventFunction) &fn, NULL },
378 #define EVT_GRID_CHANGE_SEL_LABEL(fn) { wxEVT_GRID_CHANGE_SEL_LABEL, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridEventFunction) &fn, NULL },
379 #define EVT_GRID_CELL_CHANGE(fn) { wxEVT_GRID_CELL_CHANGE, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridEventFunction) &fn, NULL },
380 #define EVT_GRID_CELL_LCLICK(fn) { wxEVT_GRID_CELL_LCLICK, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridEventFunction) &fn, NULL },
381 #define EVT_GRID_CELL_RCLICK(fn) { wxEVT_GRID_CELL_RCLICK, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridEventFunction) &fn, NULL },
382 #define EVT_GRID_LABEL_LCLICK(fn) { wxEVT_GRID_LABEL_LCLICK, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridEventFunction) &fn, NULL },
383 #define EVT_GRID_LABEL_RCLICK(fn) { wxEVT_GRID_LABEL_RCLICK, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridEventFunction) &fn, NULL },