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