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