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"
24 #define wxGRID_DEFAULT_EDIT_WIDTH 300
25 #define wxGRID_DEFAULT_EDIT_HEIGHT 27
26 #define wxGRID_DEFAULT_EDIT_X 5
27 #define wxGRID_DEFAULT_EDIT_Y 1
28 #define wxGRID_DEFAULT_SHEET_TOP 31
29 #define wxGRID_DEFAULT_SHEET_LEFT 0
30 #define wxGRID_DEFAULT_CELL_HEIGHT 20
31 #define wxGRID_DEFAULT_CELL_WIDTH 80
32 #define wxGRID_DEFAULT_VERTICAL_LABEL_WIDTH 40
33 #define wxGRID_DEFAULT_HORIZONAL_LABEL_HEIGHT 20
40 #define wxRIGHT 0x0800
43 #define WXGENERIC_GRID_VERSION 0.4
45 class WXDLLEXPORT wxGridCell
;
46 class WXDLLEXPORT wxGenericGrid
: public wxPanel
48 DECLARE_DYNAMIC_CLASS(wxGenericGrid
)
51 wxScrollBar
*hScrollBar
;
52 wxScrollBar
*vScrollBar
;
55 wxRectangle CurrentRect
;
56 bool currentRectVisible
;
57 wxGridCell
***gridCells
;
58 wxGridCell
**rowLabelCells
;
59 wxGridCell
**colLabelCells
;
66 // Row and column we're currently looking at
73 int rightOfSheet
; // Calculated from colWidths
74 int bottomOfSheet
; // Calculated from rowHeights
75 int totalGridWidth
; // Total 'virtual' size
77 int cellHeight
; // For now, a default
78 int verticalLabelWidth
;
79 int horizontalLabelHeight
;
80 int verticalLabelAlignment
;
81 int horizontalLabelAlignment
;
83 short *colWidths
; // Dynamically allocated
84 short *rowHeights
; // Dynamically allocated
85 int scrollWidth
; // Vert. scroll width, horiz. scroll height
88 wxColour cellTextColour
;
89 wxColour cellBackgroundColour
;
91 wxColour labelTextColour
;
92 wxColour labelBackgroundColour
;
93 wxBrush
*labelBackgroundBrush
;
94 wxFont
*labelTextFont
;
97 // Position of Edit control
98 wxRectangle editControlPosition
;
103 int dragStartPosition
;
104 int dragLastPosition
;
105 static wxCursor
*horizontalSashCursor
;
106 static wxCursor
*verticalSashCursor
;
108 // Don't refresh whilst this is > 0
114 inline wxGenericGrid(wxWindow
*parent
, int x
, int y
, int width
, int height
, const long style
= 0, char *name
= "grid")
116 Create(parent
, -1, wxPoint(x
, y
), wxSize(width
, height
), style
, name
);
118 inline wxGenericGrid(wxWindow
*parent
, const wxWindowID id
, const wxPoint
& pos
, const wxSize
& size
, const long style
= 0, const wxString
& name
= "grid")
120 Create(parent
, id
, pos
, size
, style
, name
);
122 ~wxGenericGrid(void);
124 void OnPaint(wxPaintEvent
& event
);
125 void OnMouseEvent(wxMouseEvent
& event
);
126 void OnSize(wxSizeEvent
& event
);
128 bool Create(wxWindow
*parent
, const wxWindowID
, const wxPoint
& pos
, const wxSize
& size
, const long style
= 0, const wxString
& name
= "grid");
130 bool CreateGrid(int nRows
, int nCols
, wxString
**cellValues
= NULL
, short *widths
= NULL
,
131 short defaultWidth
= wxGRID_DEFAULT_CELL_WIDTH
, short defaultHeight
= wxGRID_DEFAULT_CELL_HEIGHT
);
132 void ClearGrid(void);
133 virtual wxGridCell
*GetCell(int row
, int col
);
134 inline wxGridCell
***GetCells(void) { return gridCells
; }
135 bool InsertCols(int pos
= 0, int n
= 1, bool updateLabels
= TRUE
);
136 bool InsertRows(int pos
= 0, int n
= 1, bool updateLabels
= TRUE
);
137 bool AppendCols(int n
= 1, bool updateLabels
= TRUE
);
138 bool AppendRows(int n
= 1, bool updateLabels
= TRUE
);
139 bool DeleteCols(int pos
= 0, int n
= 1, bool updateLabels
= TRUE
);
140 bool DeleteRows(int pos
= 0, int n
= 1, bool updateLabels
= TRUE
);
143 void SetCellValue(const wxString
& val
, int row
, int col
);
144 wxString
& GetCellValue(int row
, int col
);
145 void SetCellAlignment(int flag
, int row
, int col
);
146 void SetCellAlignment(int flag
);
147 int GetCellAlignment(int row
, int col
);
148 int GetCellAlignment(void);
149 void SetCellTextColour(const wxColour
& val
, int row
, int col
);
150 void SetCellTextColour(const wxColour
& col
);
151 wxColour
& GetCellTextColour(int row
, int col
);
152 inline wxColour
& GetCellTextColour(void) { return cellTextColour
; }
153 void SetCellBackgroundColour(const wxColour
& col
);
154 void SetCellBackgroundColour(const wxColour
& colour
, int row
, int col
);
155 inline wxColour
& GetCellBackgroundColour(void) { return cellBackgroundColour
; }
156 wxColour
& GetCellBackgroundColour(int row
, int col
);
157 inline wxFont
*GetCellTextFont(void) { return cellTextFont
; }
158 wxFont
*GetCellTextFont(int row
, int col
);
159 void SetCellTextFont(wxFont
*fnt
);
160 void SetCellTextFont(wxFont
*fnt
, int row
, int col
);
161 wxBitmap
*GetCellBitmap(int row
, int col
);
162 void SetCellBitmap(wxBitmap
*bitmap
, int row
, int col
);
165 void SetColumnWidth(int col
, int width
);
166 int GetColumnWidth(int col
);
167 void SetRowHeight(int row
, int height
);
168 int GetRowHeight(int row
);
171 void SetLabelSize(int orientation
, int sz
);
172 int GetLabelSize(int orientation
);
173 void SetLabelAlignment(int orientation
, int alignment
);
174 int GetLabelAlignment(int orientation
);
175 wxGridCell
*GetLabelCell(int orientation
, int pos
);
176 void SetLabelValue(int orientation
, const wxString
& val
, int pos
);
177 wxString
& GetLabelValue(int orientation
, int pos
);
178 void SetLabelTextColour(const wxColour
& colour
);
179 void SetLabelBackgroundColour(const wxColour
& colour
);
180 inline wxColour
& GetLabelTextColour(void) { return labelTextColour
; }
181 inline wxColour
& GetLabelBackgroundColour(void) { return labelBackgroundColour
; }
182 inline wxFont
*GetLabelTextFont(void) { return labelTextFont
; }
183 inline void SetLabelTextFont(wxFont
*fnt
) { labelTextFont
= fnt
; }
185 // Miscellaneous accessors
186 inline int GetCursorRow(void) { return wCursorRow
; }
187 inline int GetCursorColumn(void) { return wCursorColumn
; }
188 void SetGridCursor(int row
, int col
);
189 inline int GetRows(void) { return totalRows
; }
190 inline int GetCols(void) { return totalCols
; }
191 inline int GetScrollPosX(void) { return scrollPosX
; }
192 inline int GetScrollPosY(void) { return scrollPosY
; }
193 inline void SetScrollPosX(int pos
) { scrollPosX
= pos
; }
194 inline void SetScrollPosY(int pos
) { scrollPosY
= pos
; }
195 inline wxTextCtrl
*GetTextItem(void) { return textItem
; }
196 inline wxScrollBar
*GetHorizScrollBar(void) { return hScrollBar
; }
197 inline wxScrollBar
*GetVertScrollBar(void) { return vScrollBar
; }
198 inline bool GetEditable(void) { return editable
; }
199 void SetEditable(bool edit
);
200 inline wxRectangle
& GetCurrentRect(void) { return CurrentRect
; }
201 inline bool CurrentCellVisible(void) { return currentRectVisible
; }
202 inline void SetDividerPen(wxPen
*pen
) { divisionPen
= pen
; }
203 inline wxPen
*GetDividerPen(void) { return divisionPen
; }
205 // High-level event handling
206 // Override e.g. to check value of current cell; but call
207 // base member for default processing.
208 virtual void OnSelectCellImplementation(wxDC
*dc
, int row
, int col
);
210 virtual void OnSelectCell(int WXUNUSED(row
), int WXUNUSED(col
)) {};
212 // Override to create your own class of grid cell
213 virtual wxGridCell
*OnCreateCell(void);
215 // Override to change labels e.g. creation of grid, inserting/deleting a row/col.
216 // By default, auto-labels the grid.
217 virtual void OnChangeLabels(void);
219 // Override to change the label of the edit field when selecting a cell
220 // By default, sets it to e.g. A12
221 virtual void OnChangeSelectionLabel(void);
223 // Override for event processing
224 virtual void OnCellChange(int WXUNUSED(row
), int WXUNUSED(col
)) {};
225 virtual void OnCellLeftClick(int WXUNUSED(row
), int WXUNUSED(col
), int WXUNUSED(x
), int WXUNUSED(y
), bool WXUNUSED(control
), bool WXUNUSED(shift
)) {};
226 virtual void OnCellRightClick(int WXUNUSED(row
), int WXUNUSED(col
), int WXUNUSED(x
), int WXUNUSED(y
), bool WXUNUSED(control
), bool WXUNUSED(shift
)) {};
227 virtual void OnLabelLeftClick(int WXUNUSED(row
), int WXUNUSED(col
), int WXUNUSED(x
), int WXUNUSED(y
), bool WXUNUSED(control
), bool WXUNUSED(shift
)) {};
228 virtual void OnLabelRightClick(int WXUNUSED(row
), int WXUNUSED(col
), int WXUNUSED(x
), int WXUNUSED(y
), bool WXUNUSED(control
), bool WXUNUSED(shift
)) {};
230 // Activation: call from wxFrame::OnActivate
231 void OnActivate(bool active
);
234 void AdjustScrollbars(void);
235 void UpdateDimensions(void);
239 void SetCurrentRect (int Row
, int Column
, int canvasW
= -1, int canvasH
= -1);
240 void HighlightCell (wxDC
*dc
);
241 void DrawCellText(void);
242 void SetGridClippingRegion(wxDC
*dc
);
243 virtual bool CellHitTest(int x
, int y
, int *row
, int *col
);
244 virtual bool LabelSashHitTest(int x
, int y
, int *orientation
, int *rowOrCol
, int *startPos
);
245 virtual bool LabelHitTest(int x
, int y
, int *row
, int *col
);
247 virtual void DrawLabelAreas(wxDC
*dc
);
248 virtual void DrawEditableArea(wxDC
*dc
);
249 virtual void DrawGridLines(wxDC
*dc
);
250 virtual void DrawColumnLabels(wxDC
*dc
);
251 virtual void DrawColumnLabel(wxDC
*dc
, wxRectangle
*rect
, int col
);
252 virtual void DrawRowLabels(wxDC
*dc
);
253 virtual void DrawRowLabel(wxDC
*dc
, wxRectangle
*rect
, int row
);
254 virtual void DrawCells(wxDC
*dc
);
255 virtual void DrawCellValue(wxDC
*dc
, wxRectangle
*rect
, int row
, int col
);
256 virtual void DrawCellBackground(wxDC
*dc
, wxRectangle
*rect
, int row
, int col
);
257 virtual void DrawTextRect(wxDC
*dc
, const wxString
& text
, wxRectangle
*rect
, int flag
);
258 virtual void DrawBitmapRect(wxDC
*dc
, wxBitmap
*bitmap
, wxRectangle
*rect
, int flag
);
260 // Refresh cell and optionally set the text field
261 void RefreshCell(int row
, int col
, bool setText
= FALSE
);
263 // Don't refresh within the outer pair of these.
264 inline void BeginBatch(void) { batchCount
++; }
265 inline void EndBatch(void) { batchCount
--; }
266 inline int GetBatchCount(void) { return batchCount
; }
268 void OnText(wxCommandEvent
& ev
);
269 void OnGridScroll(wxScrollEvent
& ev
);
271 DECLARE_EVENT_TABLE()
274 #define wxGRID_TEXT_CTRL 2000
275 #define wxGRID_HSCROLL 2001
276 #define wxGRID_VSCROLL 2002
278 class WXDLLEXPORT wxGridCell
: public wxObject
284 wxColour backgroundColour
;
285 wxBrush
*backgroundBrush
;
286 wxBitmap
*cellBitmap
;
289 wxGridCell(wxGenericGrid
*window
= NULL
);
292 virtual wxString
& GetTextValue(void) { return textValue
; }
293 virtual void SetTextValue(const wxString
& str
) { textValue
= str
; }
294 inline wxFont
*GetFont(void) { return font
; }
295 inline void SetFont(wxFont
*f
) { font
= f
; }
296 inline wxColour
& GetTextColour(void) { return textColour
; }
297 inline void SetTextColour(const wxColour
& colour
) { textColour
= colour
; }
298 inline wxColour
& GetBackgroundColour(void) { return backgroundColour
; }
299 void SetBackgroundColour(const wxColour
& colour
);
300 inline wxBrush
*GetBackgroundBrush(void) { return backgroundBrush
; }
301 inline int GetAlignment(void) { return alignment
; }
302 inline void SetAlignment(int align
) { alignment
= align
; }
303 inline wxBitmap
*GetCellBitmap(void) { return cellBitmap
; }
304 inline void SetCellBitmap(wxBitmap
*bitmap
) { cellBitmap
= bitmap
; }
307 class WXDLLEXPORT wxGrid
: public wxGenericGrid
310 wxGrid(void):wxGenericGrid() {}
311 wxGrid(wxWindow
*parent
, int x
=-1, int y
=-1, int width
=-1, int height
=-1,
312 long style
=0, char *name
= "gridWindow"):
313 wxGenericGrid(parent
, x
, y
, width
, height
, style
, name
)