]> git.saurik.com Git - wxWidgets.git/blob - include/wx/generic/gridg.h
Converted the virtual-methods-callbacks into real events, leaving the
[wxWidgets.git] / include / wx / generic / gridg.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: gridg.h
3 // Purpose: wxGenericGrid
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 01/02/97
7 // RCS-ID: $Id$
8 // Copyright: (c)
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifndef __GRIDH_G__
13 #define __GRIDH_G__
14
15 #ifdef __GNUG__
16 #pragma interface "gridg.h"
17 #endif
18
19 #include "wx/defs.h"
20 #include "wx/panel.h"
21 #include "wx/string.h"
22 #include "wx/scrolbar.h"
23 #include "wx/event.h"
24
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
35
36 #ifndef wxLEFT
37 #define wxLEFT 0x0400
38 #endif
39
40 #ifndef wxRIGHT
41 #define wxRIGHT 0x0800
42 #endif
43
44 #define WXGENERIC_GRID_VERSION 0.5
45
46 class WXDLLEXPORT wxGridEvent;
47 class WXDLLEXPORT wxGridCell;
48 class WXDLLEXPORT wxGenericGrid: public wxPanel
49 {
50 DECLARE_DYNAMIC_CLASS(wxGenericGrid)
51 public:
52 wxGenericGrid(void);
53
54 inline wxGenericGrid(wxWindow *parent, int x, int y, int width, int height, long style = 0, char *name = "grid")
55 {
56 Create(parent, -1, wxPoint(x, y), wxSize(width, height), style, name);
57 }
58 inline wxGenericGrid(wxWindow *parent, wxWindowID id, const wxPoint& pos, const wxSize& size, long style = 0, const wxString& name = "grid")
59 {
60 Create(parent, id, pos, size, style, name);
61 }
62 ~wxGenericGrid(void);
63
64 void OnPaint(wxPaintEvent& event);
65 void OnEraseBackground(wxEraseEvent& event);
66 void OnMouseEvent(wxMouseEvent& event);
67 void OnSize(wxSizeEvent& event);
68
69 bool Create(wxWindow *parent, wxWindowID, const wxPoint& pos, const wxSize& size, long style = 0, const wxString& name = "grid");
70
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);
74 void ClearGrid(void);
75 virtual wxGridCell *GetCell(int row, int col);
76 inline wxGridCell ***GetCells(void) { 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);
83
84 // Cell accessors
85 void SetCellValue(const wxString& val, int row, int col);
86 wxString& GetCellValue(int row, int col);
87 void SetCellAlignment(int flag, int row, int col);
88 void SetCellAlignment(int flag);
89 int GetCellAlignment(int row, int col);
90 int GetCellAlignment(void);
91 void SetCellTextColour(const wxColour& val, int row, int col);
92 void SetCellTextColour(const wxColour& col);
93 wxColour& GetCellTextColour(int row, int col);
94 inline wxColour& GetCellTextColour(void) { return m_cellTextColour; }
95 void SetCellBackgroundColour(const wxColour& col);
96 void SetCellBackgroundColour(const wxColour& colour, int row, int col);
97 inline wxColour& GetCellBackgroundColour(void) { return m_cellBackgroundColour; }
98 wxColour& GetCellBackgroundColour(int row, int col);
99 inline wxFont *GetCellTextFont(void) { return m_cellTextFont; }
100 wxFont *GetCellTextFont(int row, int col);
101 void SetCellTextFont(wxFont *fnt);
102 void SetCellTextFont(wxFont *fnt, int row, int col);
103 wxBitmap *GetCellBitmap(int row, int col);
104 void SetCellBitmap(wxBitmap *bitmap, int row, int col);
105
106 // Size accessors
107 void SetColumnWidth(int col, int width);
108 int GetColumnWidth(int col);
109 void SetRowHeight(int row, int height);
110 int GetRowHeight(int row);
111
112 // Label accessors
113 void SetLabelSize(int orientation, int sz);
114 int GetLabelSize(int orientation);
115 void SetLabelAlignment(int orientation, int alignment);
116 int GetLabelAlignment(int orientation);
117 wxGridCell *GetLabelCell(int orientation, int pos);
118 void SetLabelValue(int orientation, const wxString& val, int pos);
119 wxString& GetLabelValue(int orientation, int pos);
120 void SetLabelTextColour(const wxColour& colour);
121 void SetLabelBackgroundColour(const wxColour& colour);
122 inline wxColour& GetLabelTextColour(void) { return m_labelTextColour; }
123 inline wxColour& GetLabelBackgroundColour(void) { return m_labelBackgroundColour; }
124 inline wxFont *GetLabelTextFont(void) { return m_labelTextFont; }
125 inline void SetLabelTextFont(wxFont *fnt) { m_labelTextFont = fnt; }
126
127 // Miscellaneous accessors
128 inline int GetCursorRow(void) { return m_wCursorRow; }
129 inline int GetCursorColumn(void) { return m_wCursorColumn; }
130 void SetGridCursor(int row, int col);
131 inline int GetRows(void) { return m_totalRows; }
132 inline int GetCols(void) { return m_totalCols; }
133 inline int GetScrollPosX(void) { return m_scrollPosX; }
134 inline int GetScrollPosY(void) { 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) { return m_textItem; }
138 inline wxScrollBar *GetHorizScrollBar(void) { return m_hScrollBar; }
139 inline wxScrollBar *GetVertScrollBar(void) { return m_vScrollBar; }
140 inline bool GetEditable(void) { return m_editable; }
141 void SetEditable(bool edit);
142 inline wxRectangle& GetCurrentRect(void) { return m_currentRect; }
143 inline bool CurrentCellVisible(void) { return m_currentRectVisible; }
144 inline void SetDividerPen(wxPen *pen) { m_divisionPen = pen; }
145 inline wxPen *GetDividerPen(void) { return m_divisionPen; }
146
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);
151
152 virtual void OnSelectCell(int WXUNUSED(row), int WXUNUSED(col)) {};
153 virtual void _OnSelectCell(wxGridEvent& event);
154
155 // Override to create your own class of grid cell
156 virtual wxGridCell *OnCreateCell(void);
157 virtual void _OnCreateCell(wxGridEvent& event);
158
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 virtual void _OnChangeLabels(wxGridEvent& event);
163
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 virtual void _OnChangeSelectionLabel(wxGridEvent& event);
168
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)) {};
175
176 virtual void _OnCellChange(wxGridEvent& event);
177 virtual void _OnCellLeftClick(wxGridEvent& event);
178 virtual void _OnCellRightClick(wxGridEvent& event);
179 virtual void _OnLabelLeftClick(wxGridEvent& event);
180 virtual void _OnLabelRightClick(wxGridEvent& event);
181
182
183 // Activation: call from wxFrame::OnActivate
184 void OnActivate(bool active);
185
186 // Miscellaneous
187 void AdjustScrollbars(void);
188 void UpdateDimensions(void);
189
190 /* INTERNAL
191 */
192 void SetCurrentRect (int Row, int Column, int canvasW = -1, int canvasH = -1);
193 void HighlightCell (wxDC *dc);
194 void DrawCellText(void);
195 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);
199 // Painting
200 virtual void DrawLabelAreas(wxDC *dc);
201 virtual void DrawEditableArea(wxDC *dc);
202 virtual void DrawGridLines(wxDC *dc);
203 virtual void DrawColumnLabels(wxDC *dc);
204 virtual void DrawColumnLabel(wxDC *dc, wxRectangle *rect, int col);
205 virtual void DrawRowLabels(wxDC *dc);
206 virtual void DrawRowLabel(wxDC *dc, wxRectangle *rect, int row);
207 virtual void DrawCells(wxDC *dc);
208 virtual void DrawCellValue(wxDC *dc, wxRectangle *rect, int row, int col);
209 virtual void DrawCellBackground(wxDC *dc, wxRectangle *rect, int row, int col);
210 virtual void DrawTextRect(wxDC *dc, const wxString& text, wxRectangle *rect, int flag);
211 virtual void DrawBitmapRect(wxDC *dc, wxBitmap *bitmap, wxRectangle *rect, int flag);
212
213 // Refresh cell and optionally set the text field
214 void RefreshCell(int row, int col, bool setText = FALSE);
215
216 // Don't refresh within the outer pair of these.
217 inline void BeginBatch(void) { m_batchCount ++; }
218 inline void EndBatch(void) { m_batchCount --; }
219 inline int GetBatchCount(void) { return m_batchCount; }
220
221 void OnText(wxCommandEvent& ev);
222 void OnGridScroll(wxScrollEvent& ev);
223
224 protected:
225 wxPanel* m_editingPanel; // Contains the text control
226 wxTextCtrl* m_textItem;
227 wxScrollBar* m_hScrollBar;
228 wxScrollBar* m_vScrollBar;
229 int m_wCursorRow;
230 int m_wCursorColumn;
231 wxRectangle m_currentRect;
232 bool m_currentRectVisible;
233 wxGridCell*** m_gridCells;
234 wxGridCell** m_rowLabelCells;
235 wxGridCell** m_colLabelCells;
236 bool m_editCreated;
237 bool m_editable;
238
239 int m_totalRows;
240 int m_totalCols;
241
242 // Row and column we're currently looking at
243 int m_scrollPosX;
244 int m_scrollPosY;
245
246 // Dimensions
247 int m_leftOfSheet;
248 int m_topOfSheet;
249 int m_rightOfSheet; // Calculated from m_colWidths
250 int m_bottomOfSheet; // Calculated from m_rowHeights
251 int m_totalGridWidth; // Total 'virtual' size
252 int m_totalGridHeight;
253 int m_cellHeight; // For now, a default
254 int m_verticalLabelWidth;
255 int m_horizontalLabelHeight;
256 int m_verticalLabelAlignment;
257 int m_horizontalLabelAlignment;
258 int m_cellAlignment;
259 short* m_colWidths; // Dynamically allocated
260 short* m_rowHeights; // Dynamically allocated
261 int m_scrollWidth; // Vert. scroll width, horiz. scroll height
262
263 // Colours
264 wxColour m_cellTextColour;
265 wxColour m_cellBackgroundColour;
266 wxFont* m_cellTextFont;
267 wxColour m_labelTextColour;
268 wxColour m_labelBackgroundColour;
269 wxBrush* m_labelBackgroundBrush;
270 wxFont* m_labelTextFont;
271 wxPen* m_divisionPen;
272 wxBitmap* m_doubleBufferingBitmap;
273
274 // Position of Edit control
275 wxRectangle m_editControlPosition;
276
277 // Drag status
278 int m_dragStatus;
279 int m_dragRowOrCol;
280 int m_dragStartPosition;
281 int m_dragLastPosition;
282 wxCursor* m_horizontalSashCursor;
283 wxCursor* m_verticalSashCursor;
284
285 // To avoid multiple refreshes, use Begin/EndBatch
286 int m_batchCount;
287
288 DECLARE_EVENT_TABLE()
289 };
290
291 #define wxGRID_TEXT_CTRL 2000
292 #define wxGRID_HSCROLL 2001
293 #define wxGRID_VSCROLL 2002
294
295 class WXDLLEXPORT wxGridCell: public wxObject
296 {
297 public:
298 wxString textValue;
299 wxFont *font;
300 wxColour textColour;
301 wxColour backgroundColour;
302 wxBrush *backgroundBrush;
303 wxBitmap *cellBitmap;
304 int alignment;
305
306 wxGridCell(wxGenericGrid *window = (wxGenericGrid *) NULL);
307 ~wxGridCell(void);
308
309 virtual wxString& GetTextValue(void) { return textValue; }
310 virtual void SetTextValue(const wxString& str) { textValue = str; }
311 inline wxFont *GetFont(void) { return font; }
312 inline void SetFont(wxFont *f) { font = f; }
313 inline wxColour& GetTextColour(void) { return textColour; }
314 inline void SetTextColour(const wxColour& colour) { textColour = colour; }
315 inline wxColour& GetBackgroundColour(void) { return backgroundColour; }
316 void SetBackgroundColour(const wxColour& colour);
317 inline wxBrush *GetBackgroundBrush(void) { return backgroundBrush; }
318 inline void SetBackgroundBrush(wxBrush *brush) { backgroundBrush = brush; }
319 inline int GetAlignment(void) { return alignment; }
320 inline void SetAlignment(int align) { alignment = align; }
321 inline wxBitmap *GetCellBitmap(void) { return cellBitmap; }
322 inline void SetCellBitmap(wxBitmap *bitmap) { cellBitmap = bitmap; }
323 };
324
325 class WXDLLEXPORT wxGrid: public wxGenericGrid
326 {
327 public:
328 wxGrid(void):wxGenericGrid() {}
329 wxGrid(wxWindow *parent, int x=-1, int y=-1, int width=-1, int height=-1,
330 long style=0, char *name = "gridWindow"):
331 wxGenericGrid(parent, x, y, width, height, style, name)
332 {
333 }
334 };
335
336
337
338
339 class WXDLLEXPORT wxGridEvent : public wxCommandEvent {
340 DECLARE_DYNAMIC_CLASS(wxGridEvent)
341 public:
342 wxGridEvent()
343 : wxCommandEvent(), m_row(-1), m_col(-1), m_x(-1), m_y(-1),
344 m_control(0), m_shift(0), m_cell(0)
345 {}
346
347 wxGridEvent(int id, wxEventType type, wxObject* obj,
348 int row=-1, int col=-1, int x=-1, int y=-1,
349 bool control=FALSE, bool shift=FALSE)
350 : wxCommandEvent(type, id), m_row(row), m_col(col), m_x(x), m_y(y),
351 m_control(control), m_shift(shift), m_cell(0)
352 {
353 SetEventObject(obj);
354 }
355
356
357 int m_row;
358 int m_col;
359 int m_x;
360 int m_y;
361 bool m_control;
362 bool m_shift;
363 wxGridCell* m_cell;
364 };
365
366 const wxEventType wxEVT_GRID_SELECT_CELL = wxEVT_FIRST + 1575;
367 const wxEventType wxEVT_GRID_CREATE_CELL = wxEVT_FIRST + 1576;
368 const wxEventType wxEVT_GRID_CHANGE_LABELS = wxEVT_FIRST + 1577;
369 const wxEventType wxEVT_GRID_CHANGE_SEL_LABEL = wxEVT_FIRST + 1578;
370 const wxEventType wxEVT_GRID_CELL_CHANGE = wxEVT_FIRST + 1579;
371 const wxEventType wxEVT_GRID_CELL_LCLICK = wxEVT_FIRST + 1580;
372 const wxEventType wxEVT_GRID_CELL_RCLICK = wxEVT_FIRST + 1581;
373 const wxEventType wxEVT_GRID_LABEL_LCLICK = wxEVT_FIRST + 1582;
374 const wxEventType wxEVT_GRID_LABEL_RCLICK = wxEVT_FIRST + 1583;
375
376
377 typedef void (wxEvtHandler::*wxGridEventFunction)(wxGridEvent&);
378
379 #define EVT_GRID_SELECT_CELL(fn) { wxEVT_GRID_SELECT_CELL, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridEventFunction) &fn, NULL },
380 #define EVT_GRID_CREATE_CELL(fn) { wxEVT_GRID_CREATE_CELL, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridEventFunction) &fn, NULL },
381 #define EVT_GRID_CHANGE_LABELS(fn) { wxEVT_GRID_CHANGE_LABELS, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridEventFunction) &fn, NULL },
382 #define EVT_GRID_CHANGE_SEL_LABEL(fn) { wxEVT_GRID_CHANGE_SEL_LABEL, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridEventFunction) &fn, NULL },
383 #define EVT_GRID_CELL_CHANGE(fn) { wxEVT_GRID_CELL_CHANGE, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridEventFunction) &fn, NULL },
384 #define EVT_GRID_CELL_LCLICK(fn) { wxEVT_GRID_CELL_LCLICK, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridEventFunction) &fn, NULL },
385 #define EVT_GRID_CELL_RCLICK(fn) { wxEVT_GRID_CELL_RCLICK, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridEventFunction) &fn, NULL },
386 #define EVT_GRID_LABEL_LCLICK(fn) { wxEVT_GRID_LABEL_LCLICK, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridEventFunction) &fn, NULL },
387 #define EVT_GRID_LABEL_RCLICK(fn) { wxEVT_GRID_LABEL_RCLICK, -1, -1, (wxObjectEventFunction) (wxEventFunction) (wxGridEventFunction) &fn, NULL },
388
389 #endif
390