]>
Commit | Line | Data |
---|---|---|
c801d85f KB |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: gridg.cpp | |
3 | // Purpose: wxGenericGrid | |
4 | // Author: Julian Smart | |
5 | // Modified by: | |
6 | // Created: 04/01/98 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Julian Smart and Markus Holzem | |
9 | // Licence: wxWindows license | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | #ifdef __GNUG__ | |
13 | #pragma implementation "gridg.h" | |
14 | #pragma interface | |
15 | #endif | |
16 | ||
17 | // For compilers that support precompilation, includes "wx/wx.h". | |
18 | #include "wx/wxprec.h" | |
19 | ||
20 | #ifdef __BORLANDC__ | |
21 | #pragma hdrstop | |
22 | #endif | |
23 | ||
24 | #ifndef WX_PRECOMP | |
25 | #include "wx/wx.h" | |
26 | #endif | |
27 | ||
28 | #include <string.h> | |
29 | ||
30 | #include "wx/string.h" | |
31 | #include "wx/generic/gridg.h" | |
32 | #include "wx/settings.h" | |
33 | ||
da938cfd | 34 | // Set to zero to use no double-buffering |
2049ba38 | 35 | #ifdef __WXMSW__ |
47d67540 | 36 | #define wxUSE_DOUBLE_BUFFERING 1 |
da938cfd | 37 | #else |
47d67540 | 38 | #define wxUSE_DOUBLE_BUFFERING 0 |
da938cfd | 39 | #endif |
c801d85f KB |
40 | |
41 | #define wxGRID_DRAG_NONE 0 | |
42 | #define wxGRID_DRAG_LEFT_RIGHT 1 | |
43 | #define wxGRID_DRAG_UP_DOWN 2 | |
44 | ||
45 | IMPLEMENT_DYNAMIC_CLASS(wxGenericGrid, wxPanel) | |
46 | ||
47 | BEGIN_EVENT_TABLE(wxGenericGrid, wxPanel) | |
8aa04e8b JS |
48 | EVT_SIZE(wxGenericGrid::OnSize) |
49 | EVT_PAINT(wxGenericGrid::OnPaint) | |
50 | EVT_ERASE_BACKGROUND(wxGenericGrid::OnEraseBackground) | |
51 | EVT_MOUSE_EVENTS(wxGenericGrid::OnMouseEvent) | |
c801d85f KB |
52 | EVT_TEXT(wxGRID_TEXT_CTRL, wxGenericGrid::OnText) |
53 | EVT_COMMAND_SCROLL(wxGRID_HSCROLL, wxGenericGrid::OnGridScroll) | |
54 | EVT_COMMAND_SCROLL(wxGRID_VSCROLL, wxGenericGrid::OnGridScroll) | |
55 | END_EVENT_TABLE() | |
56 | ||
c801d85f KB |
57 | wxGenericGrid::wxGenericGrid(void) |
58 | { | |
da938cfd | 59 | m_batchCount = 0; |
c67daf87 UR |
60 | m_hScrollBar = (wxScrollBar *) NULL; |
61 | m_vScrollBar = (wxScrollBar *) NULL; | |
da938cfd JS |
62 | m_cellTextColour = *wxBLACK; |
63 | m_cellBackgroundColour = *wxWHITE; | |
64 | m_labelTextColour = *wxBLACK; | |
903f689b RR |
65 | // m_labelBackgroundColour = *wxLIGHT_GREY; |
66 | m_labelBackgroundColour = wxSystemSettings::GetSystemColour( wxSYS_COLOUR_BTNFACE ); | |
c67daf87 UR |
67 | m_labelBackgroundBrush = (wxBrush *) NULL; |
68 | m_labelTextFont = (wxFont *) NULL; | |
69 | m_cellTextFont = (wxFont *) NULL; | |
70 | m_textItem = (wxTextCtrl *) NULL; | |
da938cfd JS |
71 | m_currentRectVisible = FALSE; |
72 | m_editable = TRUE; | |
c801d85f | 73 | #if defined(__WIN95__) |
da938cfd | 74 | m_scrollWidth = wxSystemSettings::GetSystemMetric(wxSYS_VSCROLL_X); |
c801d85f | 75 | #else |
da938cfd | 76 | m_scrollWidth = 16; |
c801d85f | 77 | #endif |
da938cfd JS |
78 | m_dragStatus = wxGRID_DRAG_NONE; |
79 | m_dragRowOrCol = 0; | |
80 | m_dragStartPosition = 0; | |
81 | m_dragLastPosition = 0; | |
c67daf87 | 82 | m_divisionPen = (wxPen *) NULL; |
da938cfd JS |
83 | m_leftOfSheet = wxGRID_DEFAULT_SHEET_LEFT; |
84 | m_topOfSheet = wxGRID_DEFAULT_SHEET_TOP; | |
85 | m_cellHeight = wxGRID_DEFAULT_CELL_HEIGHT; | |
86 | m_totalGridWidth = 0; | |
87 | m_totalGridHeight = 0; | |
c67daf87 UR |
88 | m_colWidths = (short *) NULL; |
89 | m_rowHeights = (short *) NULL; | |
da938cfd JS |
90 | m_verticalLabelWidth = wxGRID_DEFAULT_VERTICAL_LABEL_WIDTH; |
91 | m_horizontalLabelHeight = wxGRID_DEFAULT_HORIZONAL_LABEL_HEIGHT; | |
92 | m_verticalLabelAlignment = wxCENTRE; | |
93 | m_horizontalLabelAlignment = wxCENTRE; | |
94 | m_editControlPosition.x = wxGRID_DEFAULT_EDIT_X; | |
95 | m_editControlPosition.y = wxGRID_DEFAULT_EDIT_Y; | |
96 | m_editControlPosition.width = wxGRID_DEFAULT_EDIT_WIDTH; | |
97 | m_editControlPosition.height = wxGRID_DEFAULT_EDIT_HEIGHT; | |
98 | m_wCursorRow = 0; | |
99 | m_wCursorColumn = 0; | |
100 | m_scrollPosX = 0; | |
101 | m_scrollPosY = 0; | |
102 | m_editCreated = FALSE; | |
103 | m_totalRows = 0; | |
104 | m_totalCols = 0; | |
c67daf87 UR |
105 | m_gridCells = (wxGridCell ***) NULL; |
106 | m_rowLabelCells = (wxGridCell **) NULL; | |
107 | m_colLabelCells = (wxGridCell **) NULL; | |
108 | m_textItem = (wxTextCtrl *) NULL; | |
109 | m_horizontalSashCursor = (wxCursor *) NULL; | |
110 | m_verticalSashCursor = (wxCursor *) NULL; | |
da938cfd JS |
111 | } |
112 | ||
113 | bool wxGenericGrid::Create(wxWindow *parent, wxWindowID id, const wxPoint& pos, const wxSize& size, | |
114 | long style, const wxString& name) | |
115 | { | |
116 | m_batchCount = 0; | |
c67daf87 UR |
117 | m_editingPanel = (wxPanel *) NULL; |
118 | m_hScrollBar = (wxScrollBar *) NULL; | |
119 | m_vScrollBar = (wxScrollBar *) NULL; | |
120 | m_horizontalSashCursor = (wxCursor *) NULL; | |
121 | m_verticalSashCursor = (wxCursor *) NULL; | |
da938cfd JS |
122 | m_cellTextColour = *wxBLACK; |
123 | m_cellBackgroundColour = *wxWHITE; | |
124 | m_labelTextColour = *wxBLACK; | |
903f689b RR |
125 | // m_labelBackgroundColour = *wxLIGHT_GREY; |
126 | m_labelBackgroundColour = wxSystemSettings::GetSystemColour( wxSYS_COLOUR_BTNFACE ); | |
c67daf87 | 127 | m_labelBackgroundBrush = (wxBrush *) NULL; |
da938cfd JS |
128 | m_labelTextFont = wxTheFontList->FindOrCreateFont(10, wxSWISS, wxNORMAL, wxBOLD); |
129 | m_cellTextFont = wxTheFontList->FindOrCreateFont(10, wxSWISS, wxNORMAL, wxNORMAL); | |
c67daf87 | 130 | m_textItem = (wxTextCtrl *) NULL; |
da938cfd JS |
131 | m_currentRectVisible = FALSE; |
132 | m_editable = TRUE; | |
c801d85f | 133 | #if defined(__WIN95__) |
da938cfd | 134 | m_scrollWidth = wxSystemSettings::GetSystemMetric(wxSYS_VSCROLL_X); |
c801d85f | 135 | #else |
da938cfd | 136 | m_scrollWidth = 16; |
c801d85f | 137 | #endif |
da938cfd JS |
138 | m_dragStatus = wxGRID_DRAG_NONE; |
139 | m_dragRowOrCol = 0; | |
140 | m_dragStartPosition = 0; | |
141 | m_dragLastPosition = 0; | |
142 | m_divisionPen = wxThePenList->FindOrCreatePen("LIGHT GREY", 1, wxSOLID); | |
c67daf87 | 143 | m_doubleBufferingBitmap = (wxBitmap *) NULL; |
c801d85f | 144 | |
da938cfd | 145 | if (!m_horizontalSashCursor) |
c801d85f | 146 | { |
da938cfd JS |
147 | m_horizontalSashCursor = new wxCursor(wxCURSOR_SIZEWE); |
148 | m_verticalSashCursor = new wxCursor(wxCURSOR_SIZENS); | |
c801d85f KB |
149 | } |
150 | ||
da938cfd JS |
151 | SetLabelBackgroundColour(m_labelBackgroundColour); |
152 | ||
153 | m_leftOfSheet = wxGRID_DEFAULT_SHEET_LEFT; | |
154 | m_topOfSheet = wxGRID_DEFAULT_SHEET_TOP; | |
155 | m_cellHeight = wxGRID_DEFAULT_CELL_HEIGHT; | |
156 | m_totalGridWidth = 0; | |
157 | m_totalGridHeight = 0; | |
c67daf87 UR |
158 | m_colWidths = (short *) NULL; |
159 | m_rowHeights = (short *) NULL; | |
da938cfd JS |
160 | |
161 | m_verticalLabelWidth = wxGRID_DEFAULT_VERTICAL_LABEL_WIDTH; | |
162 | m_horizontalLabelHeight = wxGRID_DEFAULT_HORIZONAL_LABEL_HEIGHT; | |
163 | m_verticalLabelAlignment = wxCENTRE; | |
164 | m_horizontalLabelAlignment = wxCENTRE; | |
165 | m_editControlPosition.x = wxGRID_DEFAULT_EDIT_X; | |
166 | m_editControlPosition.y = wxGRID_DEFAULT_EDIT_Y; | |
167 | m_editControlPosition.width = wxGRID_DEFAULT_EDIT_WIDTH; | |
168 | m_editControlPosition.height = wxGRID_DEFAULT_EDIT_HEIGHT; | |
169 | ||
170 | m_wCursorRow = 0; | |
171 | m_wCursorColumn = 0; | |
172 | ||
173 | m_scrollPosX = 0; | |
174 | m_scrollPosY = 0; | |
c801d85f KB |
175 | |
176 | /* Store the rect. coordinates for the current cell */ | |
da938cfd | 177 | SetCurrentRect(m_wCursorRow, m_wCursorColumn); |
c801d85f | 178 | |
da938cfd | 179 | m_editCreated = FALSE; |
c801d85f | 180 | |
da938cfd JS |
181 | m_totalRows = 0; |
182 | m_totalCols = 0; | |
c67daf87 UR |
183 | m_gridCells = (wxGridCell ***) NULL; |
184 | m_rowLabelCells = (wxGridCell **) NULL; | |
185 | m_colLabelCells = (wxGridCell **) NULL; | |
186 | m_textItem = (wxTextCtrl *) NULL; | |
c801d85f KB |
187 | |
188 | wxPanel::Create(parent, id, pos, size, style, name); | |
189 | ||
da938cfd JS |
190 | m_editingPanel = new wxPanel(this); |
191 | ||
192 | m_textItem = new wxTextCtrl(m_editingPanel, wxGRID_TEXT_CTRL, "", | |
193 | wxPoint(m_editControlPosition.x, m_editControlPosition.y), wxSize(m_editControlPosition.width, -1), | |
c801d85f | 194 | 0); |
da938cfd JS |
195 | m_textItem->Show(TRUE); |
196 | m_textItem->SetFocus(); | |
c801d85f KB |
197 | int controlW, controlH; |
198 | ||
da938cfd JS |
199 | m_textItem->GetSize(&controlW, &controlH); |
200 | m_editControlPosition.height = controlH; | |
c801d85f | 201 | |
da938cfd JS |
202 | m_topOfSheet = m_editControlPosition.y + controlH + 2; |
203 | ||
204 | m_editCreated = TRUE; | |
205 | ||
206 | m_hScrollBar = new wxScrollBar(this, wxGRID_HSCROLL, wxPoint(0, 0), wxSize(20, 100), wxHORIZONTAL); | |
207 | m_vScrollBar = new wxScrollBar(this, wxGRID_VSCROLL, wxPoint(0, 0), wxSize(100, 20), wxVERTICAL); | |
c801d85f KB |
208 | |
209 | return TRUE; | |
210 | } | |
211 | ||
212 | wxGenericGrid::~wxGenericGrid(void) | |
213 | { | |
214 | ClearGrid(); | |
215 | } | |
216 | ||
217 | void wxGenericGrid::ClearGrid(void) | |
218 | { | |
219 | int i,j; | |
da938cfd | 220 | if (m_gridCells) |
c801d85f | 221 | { |
da938cfd | 222 | for (i = 0; i < m_totalRows; i++) |
c801d85f | 223 | { |
da938cfd JS |
224 | for (j = 0; j < m_totalCols; j++) |
225 | if (m_gridCells[i][j]) | |
226 | delete m_gridCells[i][j]; | |
227 | delete[] m_gridCells[i]; | |
c801d85f | 228 | } |
da938cfd | 229 | delete[] m_gridCells; |
c67daf87 | 230 | m_gridCells = (wxGridCell ***) NULL; |
da938cfd JS |
231 | } |
232 | if (m_colWidths) | |
233 | delete[] m_colWidths; | |
c67daf87 | 234 | m_colWidths = (short *) NULL; |
da938cfd JS |
235 | if (m_rowHeights) |
236 | delete[] m_rowHeights; | |
c67daf87 | 237 | m_rowHeights = (short *) NULL; |
da938cfd JS |
238 | |
239 | if (m_rowLabelCells) | |
c801d85f | 240 | { |
da938cfd JS |
241 | for (i = 0; i < m_totalRows; i++) |
242 | delete m_rowLabelCells[i]; | |
243 | delete[] m_rowLabelCells; | |
c67daf87 | 244 | m_rowLabelCells = (wxGridCell **) NULL; |
c801d85f | 245 | } |
da938cfd | 246 | if (m_colLabelCells) |
c801d85f | 247 | { |
da938cfd JS |
248 | for (i = 0; i < m_totalCols; i++) |
249 | delete m_colLabelCells[i]; | |
250 | delete[] m_colLabelCells; | |
c67daf87 | 251 | m_colLabelCells = (wxGridCell **) NULL; |
da938cfd JS |
252 | } |
253 | if (m_doubleBufferingBitmap) | |
254 | { | |
255 | delete m_doubleBufferingBitmap; | |
c67daf87 | 256 | m_doubleBufferingBitmap = (wxBitmap *) NULL; |
c801d85f KB |
257 | } |
258 | } | |
259 | ||
260 | bool wxGenericGrid::CreateGrid(int nRows, int nCols, wxString **cellValues, short *widths, | |
261 | short defaultWidth, short defaultHeight) | |
262 | { | |
da938cfd JS |
263 | m_totalRows = nRows; |
264 | m_totalCols = nCols; | |
c801d85f KB |
265 | |
266 | int i,j; | |
da938cfd JS |
267 | m_colWidths = new short[nCols]; |
268 | m_rowHeights = new short[nRows]; | |
c801d85f KB |
269 | for (i = 0; i < nCols; i++) |
270 | if (widths) | |
da938cfd | 271 | m_colWidths[i] = widths[i]; |
c801d85f | 272 | else |
da938cfd | 273 | m_colWidths[i] = defaultWidth; |
c801d85f | 274 | for (i = 0; i < nRows; i++) |
da938cfd JS |
275 | m_rowHeights[i] = defaultHeight; |
276 | ||
277 | m_gridCells = new wxGridCell **[nRows]; | |
278 | ||
c801d85f | 279 | for (i = 0; i < nRows; i++) |
da938cfd JS |
280 | m_gridCells[i] = new wxGridCell *[nCols]; |
281 | ||
c801d85f KB |
282 | for (i = 0; i < nRows; i++) |
283 | for (j = 0; j < nCols; j++) | |
284 | if (cellValues) | |
285 | { | |
da938cfd JS |
286 | m_gridCells[i][j] = OnCreateCell(); |
287 | m_gridCells[i][j]->SetTextValue(cellValues[i][j]); | |
c801d85f KB |
288 | } |
289 | else | |
c67daf87 | 290 | m_gridCells[i][j] = (wxGridCell *) NULL; |
da938cfd JS |
291 | |
292 | m_rowLabelCells = new wxGridCell *[nRows]; | |
c801d85f | 293 | for (i = 0; i < nRows; i++) |
da938cfd JS |
294 | m_rowLabelCells[i] = new wxGridCell(this); |
295 | m_colLabelCells = new wxGridCell *[nCols]; | |
c801d85f | 296 | for (i = 0; i < nCols; i++) |
da938cfd | 297 | m_colLabelCells[i] = new wxGridCell(this); |
c801d85f | 298 | |
da938cfd | 299 | m_wCursorRow = m_wCursorColumn = 0; |
c801d85f KB |
300 | SetCurrentRect(0, 0); |
301 | ||
302 | // Need to determine various dimensions | |
303 | UpdateDimensions(); | |
304 | ||
305 | // Number of 'lines' | |
da938cfd | 306 | int objectSizeX = m_totalCols; |
c801d85f | 307 | int pageSizeX = 1; |
da938cfd | 308 | int viewLengthX = m_totalCols; |
34138703 JS |
309 | |
310 | /* | |
da938cfd JS |
311 | m_hScrollBar->SetViewLength(viewLengthX); |
312 | m_hScrollBar->SetObjectLength(objectSizeX); | |
313 | m_hScrollBar->SetPageSize(pageSizeX); | |
34138703 JS |
314 | */ |
315 | m_hScrollBar->SetScrollbar(m_hScrollBar->GetPosition(), pageSizeX, objectSizeX, viewLengthX); | |
c801d85f | 316 | |
da938cfd | 317 | int objectSizeY = m_totalRows; |
c801d85f | 318 | int pageSizeY = 1; |
da938cfd | 319 | int viewLengthY = m_totalRows; |
c801d85f | 320 | |
34138703 | 321 | /* |
da938cfd JS |
322 | m_vScrollBar->SetViewLength(viewLengthY); |
323 | m_vScrollBar->SetObjectLength(objectSizeY); | |
324 | m_vScrollBar->SetPageSize(pageSizeY); | |
34138703 JS |
325 | */ |
326 | ||
327 | m_vScrollBar->SetScrollbar(m_vScrollBar->GetPosition(), pageSizeY, objectSizeY, viewLengthY); | |
c801d85f KB |
328 | |
329 | AdjustScrollbars(); | |
330 | ||
331 | OnChangeLabels(); | |
332 | OnChangeSelectionLabel(); | |
333 | ||
334 | return TRUE; | |
335 | } | |
336 | ||
337 | // Need to determine various dimensions | |
338 | void wxGenericGrid::UpdateDimensions(void) | |
339 | { | |
340 | int canvasWidth, canvasHeight; | |
341 | GetSize(&canvasWidth, &canvasHeight); | |
342 | ||
da938cfd | 343 | if (m_editCreated && m_editable) |
c801d85f KB |
344 | { |
345 | int controlW, controlH; | |
346 | GetTextItem()->GetSize(&controlW, &controlH); | |
da938cfd | 347 | m_topOfSheet = m_editControlPosition.y + controlH + 2; |
c801d85f KB |
348 | } |
349 | else | |
da938cfd JS |
350 | m_topOfSheet = 0; |
351 | m_rightOfSheet = m_leftOfSheet + m_verticalLabelWidth; | |
c801d85f | 352 | int i; |
da938cfd | 353 | for (i = m_scrollPosX; i < m_totalCols; i++) |
c801d85f | 354 | { |
da938cfd | 355 | if (m_rightOfSheet > canvasWidth) |
c801d85f KB |
356 | break; |
357 | else | |
da938cfd | 358 | m_rightOfSheet += m_colWidths[i]; |
c801d85f | 359 | } |
da938cfd JS |
360 | m_bottomOfSheet = m_topOfSheet + m_horizontalLabelHeight; |
361 | for (i = m_scrollPosY; i < m_totalRows; i++) | |
c801d85f | 362 | { |
da938cfd | 363 | if (m_bottomOfSheet > canvasHeight) |
c801d85f KB |
364 | break; |
365 | else | |
da938cfd | 366 | m_bottomOfSheet += m_rowHeights[i]; |
c801d85f KB |
367 | } |
368 | ||
da938cfd JS |
369 | m_totalGridWidth = m_leftOfSheet + m_verticalLabelWidth; |
370 | for (i = 0; i < m_totalCols; i++) | |
c801d85f | 371 | { |
da938cfd | 372 | m_totalGridWidth += m_colWidths[i]; |
c801d85f | 373 | } |
da938cfd JS |
374 | m_totalGridHeight = m_topOfSheet + m_horizontalLabelHeight; |
375 | for (i = 0; i < m_totalRows; i++) | |
c801d85f | 376 | { |
da938cfd | 377 | m_totalGridHeight += m_rowHeights[i]; |
c801d85f KB |
378 | } |
379 | } | |
380 | ||
381 | wxGridCell *wxGenericGrid::GetCell(int row, int col) | |
382 | { | |
da938cfd | 383 | if (!m_gridCells) |
c67daf87 | 384 | return (wxGridCell *) NULL; |
c801d85f | 385 | |
da938cfd | 386 | if ((row >= m_totalRows) || (col >= m_totalCols)) |
c67daf87 | 387 | return (wxGridCell *) NULL; |
c801d85f | 388 | |
da938cfd | 389 | wxGridCell *cell = m_gridCells[row][col]; |
c801d85f KB |
390 | if (!cell) |
391 | { | |
da938cfd JS |
392 | m_gridCells[row][col] = OnCreateCell(); |
393 | return m_gridCells[row][col]; | |
c801d85f KB |
394 | } |
395 | else | |
396 | return cell; | |
397 | } | |
398 | ||
399 | void wxGenericGrid::SetGridClippingRegion(wxDC *dc) | |
400 | { | |
da938cfd JS |
401 | int m_scrollWidthHoriz = 0; |
402 | int m_scrollWidthVert = 0; | |
c801d85f KB |
403 | int cw, ch; |
404 | GetClientSize(&cw, &ch); | |
405 | ||
da938cfd JS |
406 | if (m_hScrollBar && m_hScrollBar->IsShown()) |
407 | m_scrollWidthHoriz = m_scrollWidth; | |
408 | if (m_vScrollBar && m_vScrollBar->IsShown()) | |
409 | m_scrollWidthVert = m_scrollWidth; | |
c801d85f KB |
410 | |
411 | // Don't paint over the scrollbars | |
da938cfd JS |
412 | dc->SetClippingRegion(m_leftOfSheet, m_topOfSheet, |
413 | cw - m_scrollWidthVert - m_leftOfSheet, ch - m_scrollWidthHoriz - m_topOfSheet); | |
c801d85f KB |
414 | } |
415 | ||
416 | void wxGenericGrid::OnPaint(wxPaintEvent& WXUNUSED(event)) | |
417 | { | |
da938cfd JS |
418 | int w, h; |
419 | GetClientSize(&w, &h); | |
c801d85f | 420 | |
47d67540 | 421 | bool useDoubleBuffering = (bool) wxUSE_DOUBLE_BUFFERING; |
da938cfd JS |
422 | if (useDoubleBuffering) |
423 | { | |
424 | // Reuse the old bitmap if possible | |
c801d85f | 425 | |
da938cfd JS |
426 | if (!m_doubleBufferingBitmap || |
427 | (m_doubleBufferingBitmap->GetWidth() < w || m_doubleBufferingBitmap->GetHeight() < h)) | |
428 | { | |
429 | if (m_doubleBufferingBitmap) | |
430 | delete m_doubleBufferingBitmap; | |
431 | m_doubleBufferingBitmap = new wxBitmap(w, h); | |
432 | } | |
433 | if (!m_doubleBufferingBitmap || !m_doubleBufferingBitmap->Ok()) | |
434 | { | |
435 | // If we couldn't create a new bitmap, perhaps because resources were low, | |
436 | // then don't complain, just don't double-buffer | |
437 | if (m_doubleBufferingBitmap) | |
438 | delete m_doubleBufferingBitmap; | |
c67daf87 | 439 | m_doubleBufferingBitmap = (wxBitmap *) NULL; |
da938cfd JS |
440 | useDoubleBuffering = FALSE; |
441 | } | |
442 | } | |
c801d85f | 443 | |
da938cfd JS |
444 | if (useDoubleBuffering) |
445 | { | |
446 | wxPaintDC paintDC(this); | |
447 | wxMemoryDC dc(& paintDC); | |
5260b1c5 | 448 | dc.SelectObject(* m_doubleBufferingBitmap); |
c801d85f | 449 | |
da938cfd | 450 | PaintGrid(dc); |
c801d85f | 451 | |
da938cfd JS |
452 | int vertScrollBarWidth = m_scrollWidth; |
453 | int horizScrollBarHeight = m_scrollWidth; | |
454 | if (m_vScrollBar && !m_vScrollBar->IsShown()) | |
455 | vertScrollBarWidth = 0; | |
456 | if (m_hScrollBar && !m_hScrollBar->IsShown()) | |
457 | horizScrollBarHeight = 0; | |
c801d85f | 458 | |
da938cfd JS |
459 | paintDC.Blit(m_leftOfSheet, m_topOfSheet, w - vertScrollBarWidth - m_leftOfSheet, h - horizScrollBarHeight - m_topOfSheet, |
460 | &dc, m_leftOfSheet, m_topOfSheet, wxCOPY); | |
c801d85f | 461 | |
da938cfd JS |
462 | dc.SelectObject(wxNullBitmap); |
463 | } | |
464 | else | |
465 | { | |
466 | wxPaintDC dc(this); | |
467 | PaintGrid(dc); | |
468 | } | |
469 | } | |
470 | ||
471 | void wxGenericGrid::PaintGrid(wxDC& dc) | |
472 | { | |
473 | dc.BeginDrawing(); | |
474 | dc.SetOptimization(FALSE); | |
475 | ||
476 | SetGridClippingRegion(& dc); | |
c801d85f | 477 | |
da938cfd JS |
478 | DrawLabelAreas(& dc); |
479 | ||
480 | DrawEditableArea(& dc); | |
481 | DrawColumnLabels(& dc); | |
482 | DrawRowLabels(& dc); | |
483 | DrawCells(& dc); | |
484 | DrawGridLines(& dc); | |
485 | ||
486 | /* Hilight present cell */ | |
487 | SetCurrentRect(m_wCursorRow, m_wCursorColumn); | |
488 | if (m_currentRectVisible) | |
489 | HighlightCell(& dc); | |
490 | ||
491 | dc.DestroyClippingRegion(); | |
492 | dc.SetOptimization(TRUE); | |
493 | dc.EndDrawing(); | |
494 | } | |
495 | ||
496 | // Erase (some of) the background. | |
497 | // Currently, a Windows-only optimisation. | |
e3e65dac | 498 | void wxGenericGrid::OnEraseBackground(wxEraseEvent& WXUNUSED(event) ) |
da938cfd JS |
499 | { |
500 | wxClientDC dc(this); | |
501 | dc.BeginDrawing(); | |
502 | dc.SetOptimization(FALSE); | |
503 | ||
504 | int w, h; | |
505 | GetClientSize(& w, & h); | |
506 | dc.SetBrush(*wxLIGHT_GREY_BRUSH); | |
507 | dc.SetPen(*wxLIGHT_GREY_PEN); | |
508 | ||
509 | if (m_hScrollBar && m_hScrollBar->IsShown() && m_vScrollBar && m_vScrollBar->IsShown()) | |
510 | { | |
511 | dc.DrawRectangle(w - m_scrollWidth, h - m_scrollWidth, m_scrollWidth, m_scrollWidth); | |
512 | } | |
513 | ||
514 | dc.SetOptimization(TRUE); | |
515 | dc.EndDrawing(); | |
c801d85f KB |
516 | } |
517 | ||
da938cfd | 518 | |
c801d85f KB |
519 | void wxGenericGrid::DrawLabelAreas(wxDC *dc) |
520 | { | |
521 | int cw, ch; | |
522 | GetClientSize(&cw, &ch); | |
523 | ||
524 | dc->SetPen(*wxTRANSPARENT_PEN); | |
525 | // dc->SetBrush(*dc->GetBackground()); | |
526 | ||
527 | // Should blank out any area which isn't going to be painted over. | |
da938cfd JS |
528 | // dc->DrawRectangle(m_leftOfSheet, m_bottomOfSheet, cw - m_leftOfSheet, ch - m_bottomOfSheet); |
529 | // dc->DrawRectangle(m_rightOfSheet, m_topOfSheet, cw - m_rightOfSheet, ch - m_topOfSheet); | |
c801d85f KB |
530 | |
531 | // Paint the label areas | |
da938cfd JS |
532 | dc->SetBrush(*m_labelBackgroundBrush); |
533 | // dc->DrawRectangle(m_leftOfSheet, m_topOfSheet, m_rightOfSheet - m_leftOfSheet + 1, m_horizontalLabelHeight + 1); | |
534 | dc->DrawRectangle(m_leftOfSheet, m_topOfSheet, cw-m_leftOfSheet, m_horizontalLabelHeight + 1); | |
535 | // dc->DrawRectangle(m_leftOfSheet, m_topOfSheet, m_verticalLabelWidth + 1, m_bottomOfSheet - m_topOfSheet + 1); | |
536 | dc->DrawRectangle(m_leftOfSheet, m_topOfSheet, m_verticalLabelWidth + 1, ch-m_topOfSheet); | |
c801d85f KB |
537 | } |
538 | ||
539 | void wxGenericGrid::DrawEditableArea(wxDC *dc) | |
540 | { | |
541 | int cw, ch; | |
542 | GetClientSize(&cw, &ch); | |
543 | ||
544 | dc->SetPen(*wxTRANSPARENT_PEN); | |
da938cfd JS |
545 | dc->SetBrush(*wxTheBrushList->FindOrCreateBrush(m_cellBackgroundColour, wxSOLID)); |
546 | // dc->DrawRectangle(m_leftOfSheet+m_verticalLabelWidth, m_topOfSheet+m_horizontalLabelHeight, | |
547 | // m_rightOfSheet-(m_leftOfSheet+m_verticalLabelWidth) + 1, m_bottomOfSheet - (m_topOfSheet+m_horizontalLabelHeight) + 1); | |
548 | dc->DrawRectangle(m_leftOfSheet+m_verticalLabelWidth, m_topOfSheet+m_horizontalLabelHeight, | |
549 | cw-(m_leftOfSheet+m_verticalLabelWidth), ch - (m_topOfSheet+m_horizontalLabelHeight)); | |
c801d85f KB |
550 | } |
551 | ||
552 | void wxGenericGrid::DrawGridLines(wxDC *dc) | |
553 | { | |
554 | int cw, ch; | |
555 | GetClientSize(&cw, &ch); | |
556 | ||
557 | int i; | |
558 | ||
da938cfd | 559 | if (m_divisionPen) |
c801d85f | 560 | { |
da938cfd | 561 | dc->SetPen(*m_divisionPen); |
c801d85f | 562 | |
da938cfd | 563 | int heightCount = m_topOfSheet + m_horizontalLabelHeight; |
c801d85f KB |
564 | |
565 | // Draw horizontal grey lines for cells | |
da938cfd | 566 | for (i = m_scrollPosY; i < (m_totalRows+1); i++) |
c801d85f KB |
567 | { |
568 | if (heightCount > ch) | |
569 | break; | |
570 | else | |
571 | { | |
da938cfd | 572 | dc->DrawLine(m_leftOfSheet, heightCount, |
c801d85f | 573 | cw, heightCount); |
da938cfd JS |
574 | if (i < m_totalRows) |
575 | heightCount += m_rowHeights[i]; | |
c801d85f KB |
576 | } |
577 | } | |
578 | } | |
579 | ||
da938cfd | 580 | if (m_verticalLabelWidth > 0) |
c801d85f KB |
581 | { |
582 | dc->SetPen(*wxBLACK_PEN); | |
583 | ||
584 | // Draw horizontal black lines for row labels | |
da938cfd JS |
585 | int heightCount = m_topOfSheet + m_horizontalLabelHeight; |
586 | for (i = m_scrollPosY; i < (m_totalRows+1); i++) | |
c801d85f KB |
587 | { |
588 | if (heightCount > ch) | |
589 | break; | |
590 | else | |
591 | { | |
da938cfd JS |
592 | dc->DrawLine(m_leftOfSheet, heightCount, |
593 | m_verticalLabelWidth, heightCount); | |
594 | if (i < m_totalRows) | |
595 | heightCount += m_rowHeights[i]; | |
c801d85f KB |
596 | } |
597 | } | |
598 | // Draw a black vertical line for row number cells | |
da938cfd JS |
599 | dc->DrawLine(m_leftOfSheet + m_verticalLabelWidth, m_topOfSheet, |
600 | m_leftOfSheet + m_verticalLabelWidth, ch); | |
c801d85f | 601 | // First vertical line |
da938cfd | 602 | dc->DrawLine(m_leftOfSheet, m_topOfSheet, m_leftOfSheet, ch); |
c801d85f KB |
603 | |
604 | dc->SetPen(*wxWHITE_PEN); | |
605 | ||
606 | // Draw highlights on row labels | |
da938cfd JS |
607 | heightCount = m_topOfSheet + m_horizontalLabelHeight; |
608 | for (i = m_scrollPosY; i < m_totalRows; i++) | |
c801d85f KB |
609 | { |
610 | if (heightCount > ch) | |
611 | break; | |
612 | else | |
613 | { | |
da938cfd JS |
614 | dc->DrawLine(m_leftOfSheet+1, heightCount+1, |
615 | m_verticalLabelWidth, heightCount+1); | |
616 | dc->DrawLine(m_leftOfSheet+1, heightCount+1, | |
617 | m_leftOfSheet+1, heightCount + m_rowHeights[i] - 1); | |
618 | heightCount += m_rowHeights[i]; | |
c801d85f KB |
619 | } |
620 | } | |
621 | // Last one - down to the floor. | |
da938cfd JS |
622 | dc->DrawLine(m_leftOfSheet+1, heightCount+1, |
623 | m_verticalLabelWidth, heightCount+1); | |
624 | dc->DrawLine(m_leftOfSheet+1, heightCount+1, | |
625 | m_leftOfSheet+1, ch); | |
c801d85f KB |
626 | |
627 | } | |
628 | ||
da938cfd | 629 | if (m_divisionPen) |
c801d85f | 630 | { |
da938cfd | 631 | dc->SetPen(*m_divisionPen); |
c801d85f KB |
632 | |
633 | // Draw vertical grey lines for cells | |
da938cfd JS |
634 | int widthCount = m_leftOfSheet + m_verticalLabelWidth; |
635 | for (i = m_scrollPosX; i < m_totalCols; i++) | |
c801d85f KB |
636 | { |
637 | if (widthCount > cw) | |
638 | break; | |
639 | else | |
640 | { | |
641 | // Skip the first one | |
da938cfd | 642 | if (i != m_scrollPosX) |
c801d85f | 643 | { |
da938cfd JS |
644 | dc->DrawLine(widthCount, m_topOfSheet + m_horizontalLabelHeight, |
645 | widthCount, m_bottomOfSheet); | |
c801d85f | 646 | } |
da938cfd | 647 | widthCount += m_colWidths[i]; |
c801d85f KB |
648 | } |
649 | } | |
650 | // Last one | |
da938cfd JS |
651 | dc->DrawLine(widthCount, m_topOfSheet + m_horizontalLabelHeight, |
652 | widthCount, m_bottomOfSheet); | |
c801d85f KB |
653 | } |
654 | ||
655 | dc->SetPen(*wxBLACK_PEN); | |
656 | ||
657 | // Draw two black horizontal lines for column number cells | |
658 | dc->DrawLine( | |
da938cfd JS |
659 | m_leftOfSheet, m_topOfSheet, |
660 | cw, m_topOfSheet); | |
661 | dc->DrawLine(m_leftOfSheet, m_topOfSheet + m_horizontalLabelHeight, | |
662 | cw, m_topOfSheet + m_horizontalLabelHeight); | |
c801d85f | 663 | |
da938cfd | 664 | if (m_horizontalLabelHeight > 0) |
c801d85f | 665 | { |
da938cfd JS |
666 | int widthCount = m_leftOfSheet + m_verticalLabelWidth; |
667 | ||
c801d85f | 668 | // Draw black vertical lines for column number cells |
da938cfd | 669 | for (i = m_scrollPosX; i < m_totalCols; i++) |
c801d85f KB |
670 | { |
671 | if (widthCount > cw) | |
672 | break; | |
673 | else | |
674 | { | |
da938cfd JS |
675 | dc->DrawLine(widthCount, m_topOfSheet, |
676 | widthCount, m_topOfSheet + m_horizontalLabelHeight); | |
677 | widthCount += m_colWidths[i]; | |
c801d85f KB |
678 | } |
679 | } | |
680 | ||
681 | // Last one | |
da938cfd JS |
682 | dc->DrawLine(widthCount, m_topOfSheet, |
683 | widthCount, m_topOfSheet + m_horizontalLabelHeight); | |
c801d85f KB |
684 | |
685 | // Draw highlights | |
686 | dc->SetPen(*wxWHITE_PEN); | |
da938cfd JS |
687 | widthCount = m_leftOfSheet + m_verticalLabelWidth; |
688 | ||
689 | for (i = m_scrollPosX; i < m_totalCols; i++) | |
c801d85f KB |
690 | { |
691 | if (widthCount > cw) | |
692 | break; | |
693 | else | |
694 | { | |
da938cfd JS |
695 | dc->DrawLine(widthCount+1, m_topOfSheet+1, |
696 | widthCount+m_colWidths[i], m_topOfSheet+1); | |
697 | dc->DrawLine(widthCount+1, m_topOfSheet+1, | |
698 | widthCount+1, m_topOfSheet+m_horizontalLabelHeight); | |
699 | widthCount += m_colWidths[i]; | |
c801d85f KB |
700 | } |
701 | } | |
702 | // Last one - to the right side of the canvas. | |
da938cfd JS |
703 | dc->DrawLine(widthCount+1, m_topOfSheet+1, |
704 | cw, m_topOfSheet+1); | |
705 | dc->DrawLine(widthCount+1, m_topOfSheet+1, | |
706 | widthCount+1, m_topOfSheet+m_horizontalLabelHeight); | |
c801d85f KB |
707 | |
708 | } | |
709 | } | |
710 | ||
711 | void wxGenericGrid::DrawColumnLabels(wxDC *dc) | |
712 | { | |
713 | int cw, ch; | |
714 | GetClientSize(&cw, &ch); | |
715 | ||
da938cfd | 716 | if (m_horizontalLabelHeight == 0) |
c801d85f KB |
717 | return; |
718 | ||
719 | int i; | |
720 | wxRectangle rect; | |
721 | ||
722 | // Draw letters for columns | |
da938cfd JS |
723 | rect.y = m_topOfSheet + 1; |
724 | rect.height = m_horizontalLabelHeight - 1; | |
c801d85f | 725 | |
da938cfd | 726 | dc->SetTextBackground(m_labelBackgroundColour); |
c801d85f | 727 | dc->SetBackgroundMode(wxTRANSPARENT); |
da938cfd | 728 | // dc->SetTextForeground(m_labelTextColour); |
c801d85f | 729 | |
da938cfd JS |
730 | int widthCount = m_leftOfSheet + m_verticalLabelWidth; |
731 | for (i = m_scrollPosX; i < m_totalCols; i++) | |
c801d85f KB |
732 | { |
733 | if (widthCount > cw) | |
734 | break; | |
735 | else | |
736 | { | |
737 | rect.x = 1 + widthCount; | |
da938cfd | 738 | rect.width = m_colWidths[i]; |
c801d85f KB |
739 | DrawColumnLabel(dc, &rect, i); |
740 | ||
da938cfd | 741 | widthCount += m_colWidths[i]; |
c801d85f KB |
742 | } |
743 | } | |
744 | } | |
745 | ||
746 | void wxGenericGrid::DrawColumnLabel(wxDC *dc, wxRectangle *rect, int col) | |
747 | { | |
748 | wxGridCell *cell = GetLabelCell(wxHORIZONTAL, col); | |
749 | if (cell) | |
750 | { | |
751 | wxRectangle rect2; | |
752 | rect2 = *rect; | |
753 | rect2.x += 3; | |
754 | rect2.y += 2; | |
755 | rect2.width -= 5; | |
756 | rect2.height -= 4; | |
757 | dc->SetTextForeground(GetLabelTextColour()); | |
758 | dc->SetFont(*GetLabelTextFont()); | |
759 | if ( !cell->GetTextValue().IsNull() ) | |
760 | DrawTextRect(dc, cell->GetTextValue(), &rect2, GetLabelAlignment(wxHORIZONTAL)); | |
761 | } | |
762 | } | |
763 | ||
764 | void wxGenericGrid::DrawRowLabels(wxDC *dc) | |
765 | { | |
766 | int cw, ch; | |
767 | GetClientSize(&cw, &ch); | |
768 | ||
da938cfd | 769 | if (m_verticalLabelWidth == 0) |
c801d85f KB |
770 | return; |
771 | ||
772 | int i; | |
773 | wxRectangle rect; | |
774 | ||
775 | // Draw numbers for rows | |
da938cfd JS |
776 | rect.x = m_leftOfSheet; |
777 | rect.width = m_verticalLabelWidth; | |
c801d85f | 778 | |
da938cfd | 779 | int heightCount = m_topOfSheet + m_horizontalLabelHeight; |
c801d85f | 780 | |
da938cfd | 781 | dc->SetTextBackground(m_labelBackgroundColour); |
c801d85f KB |
782 | dc->SetBackgroundMode(wxTRANSPARENT); |
783 | ||
da938cfd | 784 | for (i = m_scrollPosY; i < m_totalRows; i++) |
c801d85f KB |
785 | { |
786 | if (heightCount > ch) | |
787 | break; | |
788 | else | |
789 | { | |
790 | rect.y = 1 + heightCount; | |
da938cfd | 791 | rect.height = m_rowHeights[i]; |
c801d85f KB |
792 | DrawRowLabel(dc, &rect, i); |
793 | ||
da938cfd | 794 | heightCount += m_rowHeights[i]; |
c801d85f KB |
795 | } |
796 | } | |
797 | } | |
798 | ||
799 | void wxGenericGrid::DrawRowLabel(wxDC *dc, wxRectangle *rect, int row) | |
800 | { | |
801 | wxGridCell *cell = GetLabelCell(wxVERTICAL, row); | |
802 | if (cell) | |
803 | { | |
804 | wxRectangle rect2; | |
805 | rect2 = *rect; | |
806 | rect2.x += 3; | |
807 | rect2.y += 2; | |
808 | rect2.width -= 5; | |
809 | rect2.height -= 4; | |
810 | dc->SetTextForeground(GetLabelTextColour()); | |
811 | dc->SetFont(*GetLabelTextFont()); | |
812 | if ( !cell->GetTextValue().IsNull() ) | |
813 | DrawTextRect(dc, cell->GetTextValue(), &rect2, GetLabelAlignment(wxVERTICAL)); | |
814 | } | |
815 | } | |
816 | ||
817 | void wxGenericGrid::DrawCells(wxDC *dc) | |
818 | { | |
819 | int cw, ch; | |
820 | GetClientSize(&cw, &ch); | |
821 | ||
822 | int i,j; | |
823 | ||
824 | // Draw value corresponding to each cell | |
da938cfd | 825 | for (i = m_scrollPosY; i < m_totalRows; i++) |
c801d85f | 826 | { |
da938cfd | 827 | for (j = m_scrollPosX; j < m_totalCols; j++) |
c801d85f KB |
828 | { |
829 | SetCurrentRect(i, j, cw, ch); | |
da938cfd | 830 | if (m_currentRectVisible) |
c801d85f | 831 | { |
da938cfd JS |
832 | DrawCellBackground(dc, &m_currentRect, i, j); |
833 | DrawCellValue(dc, &m_currentRect, i, j); | |
c801d85f | 834 | } |
da938cfd | 835 | if (m_currentRect.x > cw) |
c801d85f KB |
836 | break; |
837 | } | |
da938cfd | 838 | if (m_currentRect.y > ch) |
c801d85f KB |
839 | break; |
840 | } | |
841 | dc->SetBackgroundMode(wxSOLID); | |
842 | dc->SetPen(*wxBLACK_PEN); | |
843 | } | |
844 | ||
845 | void wxGenericGrid::DrawCellBackground(wxDC *dc, wxRectangle *rect, int row, int col) | |
846 | { | |
847 | wxGridCell *cell = GetCell(row, col); | |
848 | if (cell) | |
849 | { | |
850 | dc->SetBrush(*cell->GetBackgroundBrush()); | |
851 | dc->SetPen(*wxTRANSPARENT_PEN); | |
903f689b | 852 | |
34138703 | 853 | #if 0 // In wxWin 2.0 the dc code is exact. RR. |
2049ba38 | 854 | #ifdef __WXMOTIF__ |
c801d85f KB |
855 | dc->DrawRectangle(rect->x+1, rect->y+1, rect->width-1, rect->height-1); |
856 | #else | |
857 | dc->DrawRectangle(rect->x+1, rect->y+1, rect->width, rect->height); | |
858 | #endif | |
903f689b RR |
859 | #endif |
860 | ||
861 | dc->DrawRectangle(rect->x+1, rect->y+1, rect->width-1, rect->height-1); | |
862 | ||
c801d85f KB |
863 | dc->SetPen(*wxBLACK_PEN); |
864 | } | |
865 | } | |
866 | ||
867 | void wxGenericGrid::DrawCellValue(wxDC *dc, wxRectangle *rect, int row, int col) | |
868 | { | |
869 | wxGridCell *cell = GetCell(row, col); | |
870 | if (cell) | |
871 | { | |
872 | wxBitmap *bitmap = cell->GetCellBitmap(); | |
873 | wxRectangle rect2; | |
874 | rect2 = *rect; | |
875 | rect2.x += 3; | |
876 | rect2.y += 2; | |
877 | rect2.width -= 5; | |
878 | rect2.height -= 4; | |
879 | ||
880 | if (bitmap) | |
881 | { | |
882 | DrawBitmapRect(dc, bitmap, &rect2, cell->GetAlignment()); | |
883 | } | |
884 | else | |
885 | { | |
886 | dc->SetBackgroundMode(wxTRANSPARENT); | |
887 | dc->SetTextForeground(cell->GetTextColour()); | |
888 | dc->SetFont(*cell->GetFont()); | |
889 | ||
890 | if ( !cell->GetTextValue().IsNull() ) | |
891 | DrawTextRect(dc, cell->GetTextValue(), &rect2, cell->GetAlignment()); | |
892 | } | |
893 | } | |
894 | } | |
895 | ||
896 | void wxGenericGrid::AdjustScrollbars(void) | |
897 | { | |
898 | int cw, ch; | |
899 | GetClientSize(&cw, &ch); | |
900 | ||
c801d85f KB |
901 | // We find the view size by seeing how many rows/cols fit on |
902 | // the current view. | |
903 | // BUT... this means that the scrollbar should be adjusted every time | |
904 | // it's scrolled, as well as when sized, because with variable size rows/cols, | |
905 | // the number of rows/col visible on the view differs according to what bit | |
906 | // you're looking at. The object length is always the same, but the | |
907 | // view length differs. | |
908 | ||
909 | // Since this may not be known until the end of this function, we should probably call AdjustScrollbars | |
910 | // twice. | |
da938cfd JS |
911 | int vertScrollBarWidth = m_scrollWidth; |
912 | int horizScrollBarHeight = m_scrollWidth; | |
913 | if (m_vScrollBar && !m_vScrollBar->IsShown()) | |
c801d85f | 914 | vertScrollBarWidth = 0; |
da938cfd | 915 | if (m_hScrollBar && !m_hScrollBar->IsShown()) |
c801d85f KB |
916 | horizScrollBarHeight = 0; |
917 | ||
918 | int noHorizSteps = 0; | |
919 | int noVertSteps = 0; | |
920 | ||
386af6a2 | 921 | if (m_totalGridWidth + vertScrollBarWidth <= cw) |
c801d85f KB |
922 | noHorizSteps = 0; |
923 | else | |
924 | { | |
925 | noHorizSteps = 0; | |
926 | int widthCount = 0; | |
c801d85f KB |
927 | |
928 | int i; | |
929 | int nx = 0; | |
da938cfd | 930 | for (i = m_scrollPosX ; i < m_totalCols; i++) |
c801d85f | 931 | { |
da938cfd | 932 | widthCount += m_colWidths[i]; |
c801d85f KB |
933 | // A partial bit doesn't count, we still have to scroll to see the |
934 | // rest of it | |
da938cfd | 935 | if (widthCount + m_leftOfSheet + m_verticalLabelWidth > (cw-vertScrollBarWidth)) |
c801d85f KB |
936 | break; |
937 | else | |
938 | nx ++; | |
939 | ||
940 | } | |
941 | ||
942 | noHorizSteps += nx; | |
943 | } | |
386af6a2 | 944 | if (m_totalGridHeight + horizScrollBarHeight <= ch) |
c801d85f KB |
945 | noVertSteps = 0; |
946 | else | |
947 | { | |
948 | noVertSteps = 0; | |
949 | int heightCount = 0; | |
c801d85f KB |
950 | |
951 | int i; | |
952 | int ny = 0; | |
da938cfd | 953 | for (i = m_scrollPosY ; i < m_totalRows; i++) |
c801d85f | 954 | { |
da938cfd | 955 | heightCount += m_rowHeights[i]; |
c801d85f KB |
956 | // A partial bit doesn't count, we still have to scroll to see the |
957 | // rest of it | |
da938cfd | 958 | if (heightCount + m_topOfSheet + m_horizontalLabelHeight > (ch-horizScrollBarHeight)) |
c801d85f KB |
959 | break; |
960 | else | |
961 | ny ++; | |
962 | } | |
963 | ||
964 | noVertSteps += ny; | |
965 | } | |
966 | ||
386af6a2 | 967 | if (m_totalGridWidth + vertScrollBarWidth <= cw) |
c801d85f | 968 | { |
da938cfd JS |
969 | if ( m_hScrollBar ) |
970 | m_hScrollBar->Show(FALSE); | |
c801d85f KB |
971 | SetScrollPosX(0); |
972 | } | |
973 | else | |
974 | { | |
8aa04e8b JS |
975 | if ( m_hScrollBar ) |
976 | m_hScrollBar->Show(TRUE); | |
c801d85f KB |
977 | } |
978 | ||
386af6a2 | 979 | if (m_totalGridHeight + horizScrollBarHeight <= ch) |
c801d85f | 980 | { |
8aa04e8b JS |
981 | if ( m_vScrollBar ) |
982 | m_vScrollBar->Show(FALSE); | |
983 | SetScrollPosY(0); | |
c801d85f KB |
984 | } |
985 | else | |
986 | { | |
8aa04e8b JS |
987 | if ( m_vScrollBar ) |
988 | m_vScrollBar->Show(TRUE); | |
c801d85f KB |
989 | } |
990 | ||
da938cfd | 991 | UpdateDimensions(); // Necessary in case m_scrollPosX/Y changed |
c801d85f | 992 | |
da938cfd JS |
993 | vertScrollBarWidth = m_scrollWidth; |
994 | horizScrollBarHeight = m_scrollWidth; | |
995 | if (m_vScrollBar && !m_vScrollBar->IsShown()) | |
c801d85f | 996 | vertScrollBarWidth = 0; |
da938cfd | 997 | if (m_hScrollBar && !m_hScrollBar->IsShown()) |
c801d85f KB |
998 | horizScrollBarHeight = 0; |
999 | ||
da938cfd | 1000 | if (m_hScrollBar) |
c801d85f KB |
1001 | { |
1002 | int nCols = GetCols(); | |
8aa04e8b | 1003 | m_hScrollBar->SetScrollbar(m_hScrollBar->GetPosition(), wxMax(noHorizSteps, 1), (noHorizSteps == 0) ? 1 : nCols, wxMax(noHorizSteps, 1)); |
c801d85f | 1004 | |
903f689b | 1005 | m_hScrollBar->SetSize(m_leftOfSheet, ch - m_scrollWidth -2, |
da938cfd | 1006 | cw - vertScrollBarWidth - m_leftOfSheet, m_scrollWidth); |
c801d85f KB |
1007 | } |
1008 | ||
da938cfd | 1009 | if (m_vScrollBar) |
c801d85f KB |
1010 | { |
1011 | int nRows = GetRows(); | |
c801d85f | 1012 | |
8aa04e8b | 1013 | m_vScrollBar->SetScrollbar(m_vScrollBar->GetPosition(), wxMax(noVertSteps, 1), (noVertSteps == 0) ? 1 : nRows, wxMax(noVertSteps, 1)); |
da938cfd JS |
1014 | m_vScrollBar->SetSize(cw - m_scrollWidth, m_topOfSheet, |
1015 | m_scrollWidth, ch - m_topOfSheet - horizScrollBarHeight); | |
c801d85f KB |
1016 | } |
1017 | } | |
1018 | ||
1019 | void wxGenericGrid::OnSize(wxSizeEvent& WXUNUSED(event) ) | |
1020 | { | |
da938cfd | 1021 | if (!m_vScrollBar || !m_hScrollBar) |
c801d85f KB |
1022 | return; |
1023 | ||
1024 | AdjustScrollbars(); | |
1025 | ||
1026 | int cw, ch; | |
1027 | GetClientSize(&cw, &ch); | |
1028 | ||
da938cfd | 1029 | if (m_editCreated && m_editingPanel && GetTextItem() && GetTextItem()->IsShown()) |
c801d85f | 1030 | { |
da938cfd JS |
1031 | m_editingPanel->SetSize(0, 0, cw, m_editControlPosition.height + m_editControlPosition.y + 2); |
1032 | GetTextItem()->SetSize(m_editControlPosition.x, m_editControlPosition.y, | |
1033 | cw - m_editControlPosition.x, m_editControlPosition.height); | |
c801d85f KB |
1034 | } |
1035 | } | |
1036 | ||
1037 | bool wxGenericGrid::CellHitTest(int x, int y, int *row, int *col) | |
1038 | { | |
1039 | // Find the selected cell and call OnSelectCell | |
da938cfd JS |
1040 | if (x >= (m_leftOfSheet + m_verticalLabelWidth) && y >= (m_topOfSheet + m_horizontalLabelHeight) && |
1041 | x <= m_rightOfSheet && y <= m_bottomOfSheet) | |
c801d85f KB |
1042 | { |
1043 | // Calculate the cell number from x and y | |
da938cfd JS |
1044 | x -= (m_verticalLabelWidth + m_leftOfSheet); |
1045 | y -= (m_topOfSheet + m_horizontalLabelHeight); | |
c801d85f KB |
1046 | |
1047 | int i; | |
1048 | ||
1049 | // Now we need to do a hit test for which row we're on | |
1050 | int currentHeight = 0; | |
da938cfd | 1051 | for (i = m_scrollPosY; i < m_totalRows; i++) |
c801d85f | 1052 | { |
da938cfd | 1053 | if (y >= currentHeight && y <= (currentHeight + m_rowHeights[i])) |
c801d85f KB |
1054 | { |
1055 | *row = i; | |
1056 | break; | |
1057 | } | |
da938cfd | 1058 | currentHeight += m_rowHeights[i]; |
c801d85f KB |
1059 | } |
1060 | ||
1061 | // Now we need to do a hit test for which column we're on | |
1062 | int currentWidth = 0; | |
da938cfd | 1063 | for (i = m_scrollPosX; i < m_totalCols; i++) |
c801d85f | 1064 | { |
da938cfd | 1065 | if (x >= currentWidth && x <= (currentWidth + m_colWidths[i])) |
c801d85f KB |
1066 | { |
1067 | *col = i; | |
1068 | break; | |
1069 | } | |
da938cfd | 1070 | currentWidth += m_colWidths[i]; |
c801d85f KB |
1071 | } |
1072 | return TRUE; | |
1073 | } | |
1074 | return FALSE; | |
1075 | } | |
1076 | ||
1077 | bool wxGenericGrid::LabelSashHitTest(int x, int y, int *orientation, int *rowOrCol, int *startPos) | |
1078 | { | |
1079 | int i; | |
1080 | int tolerance = 3; | |
1081 | ||
da938cfd JS |
1082 | if (x >= (m_leftOfSheet + m_verticalLabelWidth) && y >= m_topOfSheet && |
1083 | x <= m_rightOfSheet && y <= (m_topOfSheet + m_horizontalLabelHeight)) | |
c801d85f KB |
1084 | { |
1085 | // We may be on a column label sash. | |
da938cfd JS |
1086 | int currentWidth = m_leftOfSheet + m_verticalLabelWidth; |
1087 | for (i = m_scrollPosX; i < m_totalCols; i++) | |
c801d85f | 1088 | { |
da938cfd | 1089 | if (x >= (currentWidth + m_colWidths[i] - tolerance) && x <= (currentWidth + m_colWidths[i] + tolerance)) |
c801d85f KB |
1090 | { |
1091 | *orientation = wxHORIZONTAL; | |
1092 | *rowOrCol = i; | |
1093 | *startPos = currentWidth; | |
1094 | return TRUE; | |
1095 | } | |
da938cfd | 1096 | currentWidth += m_colWidths[i]; |
c801d85f KB |
1097 | } |
1098 | return FALSE; | |
1099 | } | |
da938cfd JS |
1100 | else if (x >= m_leftOfSheet && y >= (m_topOfSheet + m_horizontalLabelHeight) && |
1101 | x <= (m_leftOfSheet + m_verticalLabelWidth) && y <= m_bottomOfSheet) | |
c801d85f KB |
1102 | { |
1103 | // We may be on a row label sash. | |
da938cfd JS |
1104 | int currentHeight = m_topOfSheet + m_horizontalLabelHeight; |
1105 | for (i = m_scrollPosY; i < m_totalRows; i++) | |
c801d85f | 1106 | { |
da938cfd | 1107 | if (y >= (currentHeight + m_rowHeights[i] - tolerance) && y <= (currentHeight + m_rowHeights[i] + tolerance)) |
c801d85f KB |
1108 | { |
1109 | *orientation = wxVERTICAL; | |
1110 | *rowOrCol = i; | |
1111 | *startPos = currentHeight; | |
1112 | return TRUE; | |
1113 | } | |
da938cfd | 1114 | currentHeight += m_rowHeights[i]; |
c801d85f KB |
1115 | } |
1116 | return FALSE; | |
1117 | } | |
1118 | return FALSE; | |
1119 | } | |
1120 | ||
1121 | bool wxGenericGrid::LabelHitTest(int x, int y, int *row, int *col) | |
1122 | { | |
1123 | // Find the selected label | |
da938cfd JS |
1124 | if (x >= m_leftOfSheet && y >= m_topOfSheet && |
1125 | x <= m_rightOfSheet && y <= m_bottomOfSheet) | |
c801d85f KB |
1126 | { |
1127 | // Calculate the cell number from x and y | |
da938cfd JS |
1128 | x -= m_leftOfSheet; |
1129 | y -= m_topOfSheet; | |
c801d85f KB |
1130 | |
1131 | int i; | |
1132 | ||
1133 | // Now we need to do a hit test for which row we're on | |
da938cfd JS |
1134 | int currentHeight = m_horizontalLabelHeight; |
1135 | for (i = m_scrollPosY; i < m_totalRows; i++) | |
c801d85f | 1136 | { |
da938cfd | 1137 | if (y >= currentHeight && y <= (currentHeight + m_rowHeights[i])) |
c801d85f KB |
1138 | { |
1139 | *row = i; | |
1140 | break; | |
1141 | } | |
da938cfd | 1142 | currentHeight += m_rowHeights[i]; |
c801d85f | 1143 | } |
da938cfd | 1144 | if (y >= 0 && y <= m_horizontalLabelHeight) |
c801d85f KB |
1145 | { |
1146 | *row = -1; | |
1147 | } | |
1148 | ||
1149 | // Now we need to do a hit test for which column we're on | |
da938cfd JS |
1150 | int currentWidth = m_verticalLabelWidth; |
1151 | for (i = m_scrollPosX; i < m_totalCols; i++) | |
c801d85f | 1152 | { |
da938cfd | 1153 | if (x >= currentWidth && x <= (currentWidth + m_colWidths[i])) |
c801d85f KB |
1154 | { |
1155 | *col = i; | |
1156 | break; | |
1157 | } | |
da938cfd | 1158 | currentWidth += m_colWidths[i]; |
c801d85f | 1159 | } |
da938cfd | 1160 | if (x >= 0 && x <= m_verticalLabelWidth) |
c801d85f KB |
1161 | { |
1162 | *col = -1; | |
1163 | } | |
1164 | ||
1165 | if ((*col == -1) || (*row == -1)) | |
1166 | { | |
1167 | return TRUE; | |
1168 | } | |
1169 | } | |
1170 | return FALSE; | |
1171 | } | |
1172 | ||
1173 | void wxGenericGrid::OnMouseEvent(wxMouseEvent& ev) | |
1174 | { | |
1175 | if (ev.LeftDown()) | |
1176 | { | |
1177 | wxClientDC dc(this); | |
1178 | dc.BeginDrawing(); | |
1179 | ||
1180 | int row, col; | |
1181 | if (CellHitTest((int)ev.GetX(), (int)ev.GetY(), &row, &col)) | |
1182 | { | |
1183 | OnSelectCellImplementation(& dc, row, col); | |
1184 | OnCellLeftClick(row, col, (int)ev.GetX(), (int)ev.GetY(), ev.ControlDown(), ev.ShiftDown()); | |
1185 | } | |
1186 | if (LabelHitTest((int)ev.GetX(), (int)ev.GetY(), &row, &col)) | |
1187 | { | |
1188 | OnLabelLeftClick(row, col, (int)ev.GetX(), (int)ev.GetY(), ev.ControlDown(), ev.ShiftDown()); | |
1189 | } | |
1190 | dc.EndDrawing(); | |
1191 | } | |
1192 | else if (ev.Dragging() && ev.LeftIsDown()) | |
1193 | { | |
da938cfd | 1194 | switch (m_dragStatus) |
c801d85f KB |
1195 | { |
1196 | case wxGRID_DRAG_NONE: | |
1197 | { | |
1198 | int orientation; | |
da938cfd | 1199 | if (LabelSashHitTest((int)ev.GetX(), (int)ev.GetY(), &orientation, &m_dragRowOrCol, &m_dragStartPosition)) |
c801d85f KB |
1200 | { |
1201 | if (orientation == wxHORIZONTAL) | |
1202 | { | |
da938cfd JS |
1203 | m_dragStatus = wxGRID_DRAG_LEFT_RIGHT; |
1204 | SetCursor(*m_horizontalSashCursor); | |
1205 | m_dragLastPosition = (int)ev.GetX(); | |
c801d85f KB |
1206 | } |
1207 | else | |
1208 | { | |
da938cfd JS |
1209 | m_dragStatus = wxGRID_DRAG_UP_DOWN; |
1210 | SetCursor(*m_verticalSashCursor); | |
1211 | m_dragLastPosition = (int)ev.GetY(); | |
c801d85f KB |
1212 | } |
1213 | wxClientDC dc(this); | |
1214 | dc.BeginDrawing(); | |
1215 | dc.SetLogicalFunction(wxINVERT); | |
1216 | if (orientation == wxHORIZONTAL) | |
da938cfd | 1217 | dc.DrawLine((int)ev.GetX(), m_topOfSheet, (int)ev.GetX(), m_bottomOfSheet); |
c801d85f | 1218 | else |
da938cfd | 1219 | dc.DrawLine(m_leftOfSheet, (int)ev.GetY(), m_rightOfSheet, (int)ev.GetY()); |
c801d85f KB |
1220 | dc.EndDrawing(); |
1221 | ||
1222 | CaptureMouse(); | |
1223 | } | |
1224 | break; | |
1225 | } | |
1226 | case wxGRID_DRAG_LEFT_RIGHT: | |
1227 | { | |
1228 | wxClientDC dc(this); | |
1229 | dc.BeginDrawing(); | |
1230 | dc.SetLogicalFunction(wxINVERT); | |
da938cfd | 1231 | dc.DrawLine(m_dragLastPosition, m_topOfSheet, m_dragLastPosition, m_bottomOfSheet); |
c801d85f | 1232 | |
da938cfd | 1233 | dc.DrawLine((int)ev.GetX(), m_topOfSheet, (int)ev.GetX(), m_bottomOfSheet); |
c801d85f KB |
1234 | dc.EndDrawing(); |
1235 | ||
da938cfd JS |
1236 | m_dragLastPosition = (int)ev.GetX(); |
1237 | SetCursor(*m_horizontalSashCursor); | |
c801d85f KB |
1238 | break; |
1239 | } | |
1240 | case wxGRID_DRAG_UP_DOWN: | |
1241 | { | |
1242 | wxClientDC dc(this); | |
1243 | dc.BeginDrawing(); | |
1244 | dc.SetLogicalFunction(wxINVERT); | |
da938cfd | 1245 | dc.DrawLine(m_leftOfSheet, m_dragLastPosition, m_rightOfSheet, m_dragLastPosition); |
c801d85f | 1246 | |
da938cfd | 1247 | dc.DrawLine(m_leftOfSheet, (int)ev.GetY(), m_rightOfSheet, (int)ev.GetY()); |
c801d85f KB |
1248 | dc.EndDrawing(); |
1249 | ||
da938cfd JS |
1250 | m_dragLastPosition = (int)ev.GetY(); |
1251 | SetCursor(*m_verticalSashCursor); | |
c801d85f KB |
1252 | break; |
1253 | } | |
1254 | } | |
1255 | } | |
1256 | else if (ev.Moving()) | |
1257 | { | |
1258 | int rowOrCol, orientation, startPos; | |
1259 | if (LabelSashHitTest((int)ev.GetX(), (int)ev.GetY(), &orientation, &rowOrCol, &startPos)) | |
1260 | { | |
1261 | if (orientation == wxHORIZONTAL) | |
da938cfd | 1262 | SetCursor(*m_horizontalSashCursor); |
c801d85f | 1263 | else |
da938cfd | 1264 | SetCursor(*m_verticalSashCursor); |
c801d85f KB |
1265 | } |
1266 | else | |
1267 | SetCursor(*wxSTANDARD_CURSOR); | |
1268 | } | |
1269 | else if (ev.LeftUp()) | |
1270 | { | |
da938cfd | 1271 | switch (m_dragStatus) |
c801d85f KB |
1272 | { |
1273 | case wxGRID_DRAG_LEFT_RIGHT: | |
1274 | { | |
1275 | wxClientDC dc(this); | |
1276 | dc.BeginDrawing(); | |
1277 | dc.SetLogicalFunction(wxINVERT); | |
da938cfd | 1278 | dc.DrawLine(m_dragLastPosition, m_topOfSheet, m_dragLastPosition, m_bottomOfSheet); |
c801d85f KB |
1279 | dc.SetLogicalFunction(wxCOPY); |
1280 | dc.EndDrawing(); | |
1281 | ||
1282 | ReleaseMouse(); | |
da938cfd | 1283 | if (ev.GetX() > m_dragStartPosition) |
c801d85f | 1284 | { |
da938cfd | 1285 | m_colWidths[m_dragRowOrCol] = (short)(ev.GetX() - m_dragStartPosition); |
c801d85f KB |
1286 | UpdateDimensions(); |
1287 | AdjustScrollbars(); | |
1288 | Refresh(); | |
1289 | } | |
1290 | SetCursor(*wxSTANDARD_CURSOR); | |
1291 | int cw, ch; | |
1292 | GetClientSize(&cw, &ch); | |
1293 | wxSizeEvent evt; | |
1294 | OnSize(evt); | |
1295 | break; | |
1296 | } | |
1297 | case wxGRID_DRAG_UP_DOWN: | |
1298 | { | |
1299 | wxClientDC dc(this); | |
1300 | dc.BeginDrawing(); | |
1301 | dc.SetLogicalFunction(wxINVERT); | |
da938cfd | 1302 | dc.DrawLine(m_leftOfSheet, m_dragLastPosition, m_rightOfSheet, m_dragLastPosition); |
c801d85f KB |
1303 | dc.SetLogicalFunction(wxCOPY); |
1304 | dc.EndDrawing(); | |
1305 | ||
1306 | ReleaseMouse(); | |
da938cfd | 1307 | if (ev.GetY() > m_dragStartPosition) |
c801d85f | 1308 | { |
da938cfd | 1309 | m_rowHeights[m_dragRowOrCol] = (short)(ev.GetY() - m_dragStartPosition); |
c801d85f KB |
1310 | UpdateDimensions(); |
1311 | AdjustScrollbars(); | |
1312 | Refresh(); | |
1313 | } | |
1314 | SetCursor(*wxSTANDARD_CURSOR); | |
1315 | break; | |
1316 | } | |
1317 | } | |
da938cfd | 1318 | m_dragStatus = wxGRID_DRAG_NONE; |
c801d85f KB |
1319 | } |
1320 | else if (ev.RightDown()) | |
1321 | { | |
1322 | int row, col; | |
1323 | if (CellHitTest((int)ev.GetX(), (int)ev.GetY(), &row, &col)) | |
1324 | { | |
1325 | OnCellRightClick(row, col, (int)ev.GetX(), (int)ev.GetY(), ev.ControlDown(), ev.ShiftDown()); | |
1326 | } | |
1327 | if (LabelHitTest((int)ev.GetX(), (int)ev.GetY(), &row, &col)) | |
1328 | { | |
1329 | OnLabelRightClick(row, col, (int)ev.GetX(), (int)ev.GetY(), ev.ControlDown(), ev.ShiftDown()); | |
1330 | } | |
1331 | } | |
1332 | } | |
1333 | ||
1334 | void wxGenericGrid::OnSelectCellImplementation(wxDC *dc, int row, int col) | |
1335 | { | |
da938cfd JS |
1336 | m_wCursorColumn = col; |
1337 | m_wCursorRow = row; | |
1338 | ||
c801d85f KB |
1339 | OnChangeSelectionLabel(); |
1340 | ||
1341 | SetGridClippingRegion(dc); | |
1342 | ||
1343 | // Remove the highlight from the old cell | |
da938cfd | 1344 | if (m_currentRectVisible) |
c801d85f KB |
1345 | HighlightCell(dc); |
1346 | ||
1347 | // Highlight the new cell and copy its content to the edit control | |
da938cfd JS |
1348 | SetCurrentRect(m_wCursorRow, m_wCursorColumn); |
1349 | wxGridCell *cell = GetCell(m_wCursorRow, m_wCursorColumn); | |
c801d85f KB |
1350 | if (cell) |
1351 | { | |
1352 | if ( cell->GetTextValue().IsNull() ) | |
da938cfd | 1353 | m_textItem->SetValue(""); |
c801d85f | 1354 | else |
da938cfd | 1355 | m_textItem->SetValue(cell->GetTextValue()); |
c801d85f KB |
1356 | } |
1357 | ||
1358 | SetGridClippingRegion(dc); | |
1359 | ||
1360 | // Why isn't this needed for Windows?? | |
1361 | // Probably because of the SetValue?? | |
903f689b | 1362 | // Arrrrrgh. This isn't needed anywhere, of course. RR. |
2049ba38 | 1363 | #ifndef __WXMSW__ |
903f689b | 1364 | // HighlightCell(dc); |
c801d85f KB |
1365 | #endif |
1366 | dc->DestroyClippingRegion(); | |
1367 | ||
1368 | OnSelectCell(row, col); | |
1369 | } | |
1370 | ||
1371 | wxGridCell *wxGenericGrid::OnCreateCell(void) | |
1372 | { | |
1373 | return new wxGridCell(this); | |
1374 | } | |
1375 | ||
1376 | void wxGenericGrid::OnChangeLabels(void) | |
1377 | { | |
1378 | char buf[100]; | |
1379 | int i; | |
da938cfd | 1380 | for (i = 0; i < m_totalRows; i++) |
c801d85f KB |
1381 | { |
1382 | sprintf(buf, "%d", i+1); | |
1383 | SetLabelValue(wxVERTICAL, buf, i); | |
1384 | } | |
1385 | // A...Z,AA...ZZ,AAA...ZZZ, etc. | |
da938cfd | 1386 | for (i = 0; i < m_totalCols; i++) |
c801d85f KB |
1387 | { |
1388 | int j; | |
1389 | int noTimes = (i/26 + 1); | |
1390 | int ch = (i % 26) + 65; | |
1391 | buf[0] = 0; | |
1392 | for (j = 0; j < noTimes; j++) | |
1393 | { | |
1394 | char buf2[20]; | |
1395 | sprintf(buf2, "%c", (char)ch); | |
1396 | strcat(buf, buf2); | |
1397 | } | |
1398 | SetLabelValue(wxHORIZONTAL, buf, i); | |
1399 | } | |
1400 | } | |
1401 | ||
1402 | void wxGenericGrid::OnChangeSelectionLabel(void) | |
1403 | { | |
1404 | if (!GetEditable()) | |
1405 | return; | |
1406 | ||
1407 | wxString rowLabel(GetLabelValue(wxVERTICAL, GetCursorRow())); | |
1408 | wxString colLabel(GetLabelValue(wxHORIZONTAL, GetCursorColumn())); | |
1409 | ||
1410 | wxString newLabel = colLabel + rowLabel; | |
1411 | if ((newLabel.Length() > 0) && (newLabel.Length() <= 8) && GetTextItem()) | |
1412 | { | |
1413 | // GetTextItem()->SetLabel(newLabel); | |
1414 | } | |
1415 | } | |
1416 | ||
1417 | void wxGenericGrid::HighlightCell(wxDC *dc) | |
1418 | { | |
1419 | dc->SetLogicalFunction(wxINVERT); | |
903f689b | 1420 | |
c801d85f | 1421 | // Top |
903f689b RR |
1422 | dc->DrawLine( m_currentRect.x + 1, |
1423 | m_currentRect.y + 1, | |
1424 | m_currentRect.x + m_currentRect.width - 1, | |
1425 | m_currentRect.y + 1); | |
c801d85f | 1426 | // Right |
903f689b RR |
1427 | dc->DrawLine( m_currentRect.x + m_currentRect.width - 1, |
1428 | m_currentRect.y + 1, | |
1429 | m_currentRect.x + m_currentRect.width - 1, | |
1430 | m_currentRect.y +m_currentRect.height - 1 ); | |
c801d85f | 1431 | // Bottom |
903f689b RR |
1432 | dc->DrawLine( m_currentRect.x + m_currentRect.width - 1, |
1433 | m_currentRect.y + m_currentRect.height - 1, | |
1434 | m_currentRect.x + 1, | |
1435 | m_currentRect.y + m_currentRect.height - 1); | |
c801d85f | 1436 | // Left |
903f689b RR |
1437 | dc->DrawLine( m_currentRect.x + 1, |
1438 | m_currentRect.y + m_currentRect.height - 1, | |
1439 | m_currentRect.x + 1, | |
1440 | m_currentRect.y + 1); | |
c801d85f KB |
1441 | |
1442 | dc->SetLogicalFunction(wxCOPY); | |
1443 | } | |
1444 | ||
1445 | void wxGenericGrid::DrawCellText(void) | |
1446 | { | |
da938cfd | 1447 | if (!m_currentRectVisible) |
c801d85f KB |
1448 | return; |
1449 | ||
1450 | wxGridCell *cell = GetCell(GetCursorRow(), GetCursorColumn()); | |
1451 | if (!cell) | |
1452 | return; | |
1453 | ||
1454 | static char szEdit[300]; | |
1455 | ||
1456 | wxClientDC dc(this); | |
1457 | dc.BeginDrawing(); | |
1458 | ||
1459 | SetGridClippingRegion(& dc); | |
1460 | ||
1461 | dc.SetBackgroundMode(wxTRANSPARENT); | |
1462 | dc.SetBrush(*cell->GetBackgroundBrush()); | |
1463 | ||
da938cfd JS |
1464 | strcpy(szEdit, m_textItem->GetValue()); |
1465 | ||
c801d85f | 1466 | wxRectangle rect; |
da938cfd | 1467 | rect = m_currentRect; |
c801d85f KB |
1468 | rect.x += 3; |
1469 | rect.y += 2; | |
1470 | rect.width -= 5; | |
1471 | rect.height -= 4; | |
1472 | ||
1473 | DrawTextRect(& dc, " ", &rect, wxLEFT); | |
1474 | DrawTextRect(& dc, szEdit, &rect, cell->GetAlignment()); | |
1475 | ||
1476 | dc.DestroyClippingRegion(); | |
1477 | ||
1478 | dc.SetBackgroundMode(wxSOLID); | |
1479 | ||
1480 | dc.EndDrawing(); | |
1481 | } | |
1482 | ||
1483 | void wxGenericGrid::SetCurrentRect(int Row, int Column, int canvasW, int canvasH) | |
1484 | { | |
da938cfd | 1485 | int currentWidth = m_leftOfSheet + m_verticalLabelWidth; |
c801d85f | 1486 | int i; |
da938cfd JS |
1487 | for (i = m_scrollPosX; i < Column; i++) |
1488 | currentWidth += m_colWidths[i]; | |
c801d85f | 1489 | |
da938cfd JS |
1490 | int currentHeight = m_topOfSheet + m_horizontalLabelHeight; |
1491 | for (i = m_scrollPosY; i < Row; i++) | |
1492 | currentHeight += m_rowHeights[i]; | |
c801d85f | 1493 | |
da938cfd JS |
1494 | m_currentRect.x = currentWidth; |
1495 | m_currentRect.y = currentHeight; | |
1496 | m_currentRect.width = m_colWidths ? (m_colWidths[Column]) : 0; | |
1497 | m_currentRect.height = m_rowHeights ? (m_rowHeights[Row]) : 0; | |
1498 | ||
1499 | if (Row < m_scrollPosY || Column < m_scrollPosX) | |
1500 | m_currentRectVisible = FALSE; | |
1501 | else if ((canvasW != -1 && canvasH != -1) && (m_currentRect.x > canvasW || m_currentRect.y > canvasH)) | |
1502 | m_currentRectVisible = FALSE; | |
1503 | else m_currentRectVisible = TRUE; | |
c801d85f KB |
1504 | } |
1505 | ||
1506 | static bool wxRectIntersection(wxRectangle *rect1, wxRectangle *rect2, wxRectangle *rect3) | |
1507 | { | |
1508 | int x2_1 = rect1->x + rect1->width; | |
1509 | int y2_1 = rect1->y + rect1->height; | |
1510 | ||
1511 | int x2_2 = rect2->x + rect2->width; | |
1512 | int y2_2 = rect2->y + rect2->height; | |
1513 | ||
1514 | int x2_3, y2_3; | |
1515 | ||
1516 | // Check for intersection | |
1517 | if ((rect1->x > x2_2) || (rect2->x > x2_1) || | |
1518 | (rect1->y > y2_2) || (rect2->y > y2_1)) | |
1519 | { | |
1520 | // No intersection | |
1521 | rect3->x = rect3->y = rect3->width = rect3->height = 0; | |
1522 | return FALSE; | |
1523 | } | |
1524 | ||
1525 | if (rect1->x > rect2->x) | |
1526 | rect3->x = rect1->x; | |
1527 | else | |
1528 | rect3->x = rect2->x; | |
1529 | if (rect1->y > rect2->y) | |
1530 | rect3->y = rect1->y; | |
1531 | else | |
1532 | rect3->y = rect2->y; | |
1533 | ||
1534 | if (x2_1 > x2_2) | |
1535 | x2_3 = x2_2; | |
1536 | else | |
1537 | x2_3 = x2_1; | |
1538 | if (y2_1 > y2_2) | |
1539 | y2_3 = y2_2; | |
1540 | else | |
1541 | y2_3 = y2_1; | |
1542 | ||
1543 | rect3->width = (int)(x2_3 - rect3->x); | |
1544 | rect3->height = (int)(y2_3 - rect3->y); | |
1545 | return TRUE; | |
1546 | } | |
1547 | ||
1548 | void wxGenericGrid::DrawTextRect(wxDC *dc, const wxString& text, wxRectangle *rect, int flag) | |
1549 | { | |
1550 | dc->BeginDrawing(); | |
1551 | ||
1552 | // Ultimately, this functionality should be built into wxWindows, | |
1553 | // and optimized for each platform. E.g. on Windows, use DrawText | |
1554 | // passing a clipping rectangle, so that the wxWindows clipping region | |
1555 | // does not have to be used to implement this. | |
1556 | ||
1557 | // If we're already clipping, we need to find the intersection | |
1558 | // between current clipping area and text clipping area. | |
1559 | ||
1560 | wxRectangle clipRect; | |
1561 | wxRectangle clipRect2; | |
1562 | long clipX, clipY, clipW, clipH; | |
1563 | dc->GetClippingBox(&clipX, &clipY, &clipW, &clipH); | |
1564 | clipRect.x = (int)clipX; clipRect.y = (int)clipY; | |
1565 | clipRect.width = (int)clipW; clipRect.height = (int)clipH; | |
1566 | ||
1567 | bool alreadyClipping = TRUE; | |
1568 | ||
1569 | if (clipRect.x == 0 && clipRect.y == 0 && clipRect.width == 0 && clipRect.height == 0) | |
1570 | { | |
1571 | alreadyClipping = FALSE; | |
1572 | clipRect2.x = rect->x; clipRect2.y = rect->y; | |
1573 | clipRect2.width = rect->width; clipRect2.height = rect->height; | |
1574 | } | |
1575 | else | |
1576 | { | |
1577 | // Find intersection. | |
1578 | if (!wxRectIntersection(rect, &clipRect, &clipRect2)) | |
1579 | return; | |
1580 | } | |
1581 | ||
1582 | if (alreadyClipping) | |
1583 | dc->DestroyClippingRegion(); | |
1584 | ||
1585 | dc->SetClippingRegion(clipRect2.x, clipRect2.y, clipRect2.width, clipRect2.height); | |
1586 | long textWidth, textHeight; | |
1587 | ||
1588 | dc->GetTextExtent(text, &textWidth, &textHeight); | |
1589 | ||
1590 | // Do alignment | |
1591 | float x,y; | |
1592 | switch (flag) | |
1593 | { | |
1594 | case wxRIGHT: | |
1595 | { | |
1596 | x = (rect->x + rect->width - textWidth - 1.0); | |
1597 | y = (rect->y + (rect->height - textHeight)/2.0); | |
1598 | break; | |
1599 | } | |
1600 | case wxCENTRE: | |
1601 | { | |
1602 | x = (rect->x + (rect->width - textWidth)/2.0); | |
1603 | y = (rect->y + (rect->height - textHeight)/2.0); | |
1604 | break; | |
1605 | } | |
1606 | case wxLEFT: | |
1607 | default: | |
1608 | { | |
1609 | x = (rect->x + 1.0); | |
1610 | y = (rect->y + (rect->height - textHeight)/2.0); | |
1611 | break; | |
1612 | } | |
1613 | } | |
1614 | dc->DrawText(text, (long)x, (long)y ); | |
1615 | ||
1616 | dc->DestroyClippingRegion(); | |
1617 | ||
1618 | // Restore old clipping | |
1619 | if (alreadyClipping) | |
1620 | dc->SetClippingRegion(clipRect.x, clipRect.y, clipRect.width, clipRect.height); | |
1621 | ||
1622 | dc->EndDrawing(); | |
1623 | } | |
1624 | ||
1625 | void wxGenericGrid::DrawBitmapRect(wxDC *dc, wxBitmap *bitmap, wxRectangle *rect, int flag) | |
1626 | { | |
1627 | dc->BeginDrawing(); | |
1628 | ||
1629 | // Ultimately, this functionality should be built into wxWindows, | |
1630 | // and optimized for each platform. E.g. on Windows, use DrawText | |
1631 | // passing a clipping rectangle, so that the wxWindows clipping region | |
1632 | // does not have to be used to implement this. | |
1633 | ||
1634 | // If we're already clipping, we need to find the intersection | |
1635 | // between current clipping area and text clipping area. | |
1636 | ||
1637 | wxRectangle clipRect; | |
1638 | wxRectangle clipRect2; | |
1639 | long clipX, clipY, clipW, clipH; | |
1640 | dc->GetClippingBox(&clipX, &clipY, &clipW, &clipH); | |
1641 | clipRect.x = (int)clipX; clipRect.y = (int)clipY; | |
1642 | clipRect.width = (int)clipW; clipRect.height = (int)clipH; | |
1643 | ||
1644 | bool alreadyClipping = TRUE; | |
1645 | ||
1646 | if (clipRect.x == 0 && clipRect.y == 0 && clipRect.width == 0 && clipRect.height == 0) | |
1647 | { | |
1648 | alreadyClipping = FALSE; | |
1649 | clipRect2.x = rect->x; clipRect2.y = rect->y; | |
1650 | clipRect2.width = rect->width; clipRect2.height = rect->height; | |
1651 | } | |
1652 | else | |
1653 | { | |
1654 | // Find intersection. | |
1655 | if (!wxRectIntersection(rect, &clipRect, &clipRect2)) | |
1656 | return; | |
1657 | } | |
1658 | ||
1659 | if (alreadyClipping) | |
1660 | dc->DestroyClippingRegion(); | |
1661 | ||
1662 | dc->SetClippingRegion(clipRect2.x, clipRect2.y, clipRect2.width, clipRect2.height); | |
1663 | float bitmapWidth, bitmapHeight; | |
1664 | ||
1665 | bitmapWidth = bitmap->GetWidth(); | |
1666 | bitmapHeight = bitmap->GetHeight(); | |
1667 | ||
1668 | // Do alignment | |
1669 | long x,y; | |
1670 | switch (flag) | |
1671 | { | |
1672 | case wxRIGHT: | |
1673 | { | |
1674 | x = (long)(rect->x + rect->width - bitmapWidth - 1); | |
1675 | y = (long)(rect->y + (rect->height - bitmapHeight)/2.0); | |
1676 | break; | |
1677 | } | |
1678 | case wxCENTRE: | |
1679 | { | |
1680 | x = (long)(rect->x + (rect->width - bitmapWidth)/2.0); | |
1681 | y = (long)(rect->y + (rect->height - bitmapHeight)/2.0); | |
1682 | break; | |
1683 | } | |
1684 | case wxLEFT: | |
1685 | default: | |
1686 | { | |
1687 | x = (long)(rect->x + 1); | |
1688 | y = (long)(rect->y + (rect->height - bitmapHeight)/2.0); | |
1689 | break; | |
1690 | } | |
1691 | } | |
1692 | wxMemoryDC dcTemp; | |
1693 | dcTemp.SelectObject(*bitmap); | |
1694 | ||
1695 | dc->Blit( (long)x, (long)y, (long)bitmapWidth, (long)bitmapHeight, &dcTemp, 0, 0); | |
1696 | dcTemp.SelectObject(wxNullBitmap); | |
1697 | ||
1698 | dc->DestroyClippingRegion(); | |
1699 | ||
1700 | // Restore old clipping | |
1701 | if (alreadyClipping) | |
1702 | dc->SetClippingRegion(clipRect.x, clipRect.y, clipRect.width, clipRect.height); | |
1703 | ||
1704 | dc->EndDrawing(); | |
1705 | } | |
1706 | ||
1707 | void wxGenericGrid::OnActivate(bool active) | |
1708 | { | |
1709 | if (active) | |
1710 | { | |
1711 | // Edit control should always have the focus | |
1712 | if (GetTextItem() && GetEditable()) | |
1713 | { | |
1714 | GetTextItem()->SetFocus(); | |
1715 | wxGridCell *cell = GetCell(GetCursorRow(), GetCursorColumn()); | |
1716 | if (cell) | |
1717 | GetTextItem()->SetValue(cell->GetTextValue()); | |
1718 | } | |
1719 | } | |
1720 | } | |
1721 | ||
1722 | void wxGenericGrid::SetCellValue(const wxString& val, int row, int col) | |
1723 | { | |
1724 | wxGridCell *cell = GetCell(row, col); | |
1725 | if (cell) | |
1726 | { | |
1727 | cell->SetTextValue(val); | |
1728 | ||
1729 | RefreshCell(row, col, TRUE); | |
1730 | } | |
1731 | } | |
1732 | ||
1733 | void wxGenericGrid::RefreshCell(int row, int col, bool setText) | |
1734 | { | |
1735 | // Don't refresh within a pair of batch brackets | |
1736 | if (GetBatchCount() > 0) | |
1737 | return; | |
1738 | ||
1739 | int cw, ch; | |
1740 | GetClientSize(&cw, &ch); | |
1741 | ||
1742 | SetCurrentRect(row, col, cw, ch); | |
da938cfd | 1743 | if (m_currentRectVisible) |
c801d85f KB |
1744 | { |
1745 | wxGridCell *cell = GetCell(row, col); | |
1746 | ||
1747 | bool currentPos = FALSE; | |
da938cfd | 1748 | if (row == m_wCursorRow && col == m_wCursorColumn && GetTextItem() && GetTextItem()->IsShown() && setText) |
c801d85f KB |
1749 | { |
1750 | GetTextItem()->SetValue(cell->GetTextValue()); | |
1751 | currentPos = TRUE; | |
1752 | } | |
1753 | // Gets refreshed anyway in MSW | |
2049ba38 | 1754 | #ifdef __WXMSW__ |
c801d85f KB |
1755 | if (!currentPos) |
1756 | #endif | |
1757 | { | |
1758 | wxClientDC dc(this); | |
1759 | dc.BeginDrawing(); | |
da938cfd JS |
1760 | DrawCellBackground(& dc, &m_currentRect, row, col); |
1761 | DrawCellValue(& dc, &m_currentRect, row, col); | |
c801d85f KB |
1762 | dc.EndDrawing(); |
1763 | } | |
1764 | } | |
1765 | } | |
1766 | ||
1767 | wxString& wxGenericGrid::GetCellValue(int row, int col) | |
1768 | { | |
1769 | static wxString emptyString(""); | |
1770 | ||
1771 | wxGridCell *cell = GetCell(row, col); | |
1772 | if (cell) | |
1773 | return cell->GetTextValue(); | |
1774 | else | |
1775 | return emptyString; | |
1776 | } | |
1777 | ||
1778 | void wxGenericGrid::SetColumnWidth(int col, int width) | |
1779 | { | |
da938cfd JS |
1780 | if (col <= m_totalCols) |
1781 | m_colWidths[col] = width; | |
c801d85f KB |
1782 | } |
1783 | ||
1784 | int wxGenericGrid::GetColumnWidth(int col) | |
1785 | { | |
da938cfd JS |
1786 | if (col <= m_totalCols) |
1787 | return m_colWidths[col]; | |
c801d85f KB |
1788 | else |
1789 | return 0; | |
1790 | } | |
1791 | ||
1792 | void wxGenericGrid::SetRowHeight(int row, int height) | |
1793 | { | |
da938cfd JS |
1794 | if (row <= m_totalRows) |
1795 | m_rowHeights[row] = height; | |
c801d85f KB |
1796 | } |
1797 | ||
1798 | int wxGenericGrid::GetRowHeight(int row) | |
1799 | { | |
da938cfd JS |
1800 | if (row <= m_totalRows) |
1801 | return m_rowHeights[row]; | |
c801d85f KB |
1802 | else |
1803 | return 0; | |
1804 | } | |
1805 | ||
1806 | void wxGenericGrid::SetLabelSize(int orientation, int sz) | |
1807 | { | |
1808 | if (orientation == wxHORIZONTAL) | |
da938cfd | 1809 | m_horizontalLabelHeight = sz; |
c801d85f | 1810 | else |
da938cfd | 1811 | m_verticalLabelWidth = sz; |
c801d85f KB |
1812 | UpdateDimensions(); |
1813 | SetCurrentRect(GetCursorRow(), GetCursorColumn()); | |
1814 | } | |
1815 | ||
1816 | int wxGenericGrid::GetLabelSize(int orientation) | |
1817 | { | |
1818 | if (orientation == wxHORIZONTAL) | |
da938cfd | 1819 | return m_horizontalLabelHeight; |
c801d85f | 1820 | else |
da938cfd | 1821 | return m_verticalLabelWidth; |
c801d85f KB |
1822 | } |
1823 | ||
1824 | wxGridCell *wxGenericGrid::GetLabelCell(int orientation, int pos) | |
1825 | { | |
1826 | if (orientation == wxHORIZONTAL) | |
1827 | { | |
da938cfd JS |
1828 | if (m_colLabelCells && pos < m_totalCols) |
1829 | return m_colLabelCells[pos]; | |
c801d85f | 1830 | else |
c67daf87 | 1831 | return (wxGridCell *) NULL; |
c801d85f KB |
1832 | } |
1833 | else | |
1834 | { | |
da938cfd JS |
1835 | if (m_rowLabelCells && pos < m_totalRows) |
1836 | return m_rowLabelCells[pos]; | |
c801d85f | 1837 | else |
c67daf87 | 1838 | return (wxGridCell *) NULL; |
c801d85f KB |
1839 | } |
1840 | } | |
1841 | ||
1842 | void wxGenericGrid::SetLabelValue(int orientation, const wxString& val, int pos) | |
1843 | { | |
1844 | wxGridCell *cell = GetLabelCell(orientation, pos); | |
1845 | if (cell) | |
1846 | cell->SetTextValue(val); | |
1847 | } | |
1848 | ||
1849 | wxString& wxGenericGrid::GetLabelValue(int orientation, int pos) | |
1850 | { | |
1851 | static wxString emptyString = ""; | |
1852 | wxGridCell *cell = GetLabelCell(orientation, pos); | |
1853 | if (cell) | |
1854 | return cell->GetTextValue(); | |
1855 | else | |
1856 | return emptyString; | |
1857 | } | |
1858 | ||
1859 | void wxGenericGrid::SetLabelAlignment(int orientation, int align) | |
1860 | { | |
1861 | if (orientation == wxHORIZONTAL) | |
da938cfd | 1862 | m_horizontalLabelAlignment = align; |
c801d85f | 1863 | else |
da938cfd | 1864 | m_verticalLabelAlignment = align; |
c801d85f KB |
1865 | UpdateDimensions(); |
1866 | SetCurrentRect(GetCursorRow(), GetCursorColumn()); | |
1867 | } | |
1868 | ||
1869 | int wxGenericGrid::GetLabelAlignment(int orientation) | |
1870 | { | |
1871 | if (orientation == wxHORIZONTAL) | |
da938cfd | 1872 | return m_horizontalLabelAlignment; |
c801d85f | 1873 | else |
da938cfd | 1874 | return m_verticalLabelAlignment; |
c801d85f KB |
1875 | } |
1876 | ||
1877 | void wxGenericGrid::SetLabelTextColour(const wxColour& colour) | |
1878 | { | |
da938cfd | 1879 | m_labelTextColour = colour; |
c801d85f KB |
1880 | |
1881 | } | |
1882 | ||
1883 | void wxGenericGrid::SetLabelBackgroundColour(const wxColour& colour) | |
1884 | { | |
da938cfd JS |
1885 | m_labelBackgroundColour = colour; |
1886 | m_labelBackgroundBrush = wxTheBrushList->FindOrCreateBrush(m_labelBackgroundColour, wxSOLID); | |
c801d85f KB |
1887 | } |
1888 | ||
1889 | void wxGenericGrid::SetEditable(bool edit) | |
1890 | { | |
da938cfd | 1891 | m_editable = edit; |
c801d85f KB |
1892 | if (edit) |
1893 | { | |
1894 | int controlW, controlH; | |
da938cfd JS |
1895 | m_textItem->GetSize(&controlW, &controlH); |
1896 | m_editControlPosition.height = controlH; | |
1897 | ||
1898 | m_topOfSheet = m_editControlPosition.x + controlH + 2; | |
1899 | if (m_textItem) | |
c801d85f | 1900 | { |
da938cfd JS |
1901 | m_editingPanel->Show(TRUE); |
1902 | m_textItem->Show(TRUE); | |
1903 | m_textItem->SetFocus(); | |
c801d85f KB |
1904 | } |
1905 | } | |
1906 | else | |
1907 | { | |
da938cfd JS |
1908 | m_topOfSheet = 0; |
1909 | if (m_textItem) | |
c801d85f | 1910 | { |
da938cfd JS |
1911 | m_textItem->Show(FALSE); |
1912 | m_editingPanel->Show(FALSE); | |
c801d85f KB |
1913 | } |
1914 | } | |
1915 | UpdateDimensions(); | |
1916 | SetCurrentRect(GetCursorRow(), GetCursorColumn()); | |
1917 | ||
1918 | int cw, ch; | |
1919 | GetClientSize(&cw, &ch); | |
1920 | wxSizeEvent evt; | |
1921 | OnSize(evt); | |
1922 | /* | |
1923 | int cw, ch; | |
da938cfd | 1924 | int m_scrollWidth = 16; |
c801d85f KB |
1925 | GetClientSize(&cw, &ch); |
1926 | ||
da938cfd JS |
1927 | if (m_vScrollBar) |
1928 | m_vScrollBar->SetSize(cw - m_scrollWidth, m_topOfSheet, | |
1929 | m_scrollWidth, ch - m_topOfSheet - m_scrollWidth); | |
c801d85f KB |
1930 | */ |
1931 | } | |
1932 | ||
1933 | void wxGenericGrid::SetCellAlignment(int flag, int row, int col) | |
1934 | { | |
1935 | wxGridCell *cell = GetCell(row, col); | |
1936 | if (cell) | |
1937 | cell->SetAlignment(flag); | |
1938 | } | |
1939 | ||
1940 | int wxGenericGrid::GetCellAlignment(int row, int col) | |
1941 | { | |
1942 | wxGridCell *cell = GetCell(row, col); | |
1943 | if (cell) | |
1944 | return cell->GetAlignment(); | |
1945 | else | |
da938cfd | 1946 | return m_cellAlignment; |
c801d85f KB |
1947 | } |
1948 | ||
1949 | void wxGenericGrid::SetCellAlignment(int flag) | |
1950 | { | |
da938cfd | 1951 | m_cellAlignment = flag; |
c801d85f KB |
1952 | int i,j; |
1953 | for (i = 0; i < GetRows(); i++) | |
1954 | for (j = 0; j < GetCols(); j++) | |
1955 | if (GetCell(i, j)) | |
1956 | GetCell(i, j)->SetAlignment(flag); | |
1957 | } | |
1958 | ||
1959 | int wxGenericGrid::GetCellAlignment(void) | |
1960 | { | |
da938cfd | 1961 | return m_cellAlignment; |
c801d85f KB |
1962 | } |
1963 | ||
1964 | void wxGenericGrid::SetCellBackgroundColour(const wxColour& col) | |
1965 | { | |
da938cfd | 1966 | m_cellBackgroundColour = col; |
c801d85f KB |
1967 | int i,j; |
1968 | for (i = 0; i < GetRows(); i++) | |
1969 | for (j = 0; j < GetCols(); j++) | |
1970 | if (GetCell(i, j)) | |
1971 | GetCell(i, j)->SetBackgroundColour(col); | |
1972 | } | |
1973 | ||
1974 | void wxGenericGrid::SetCellBackgroundColour(const wxColour& val, int row, int col) | |
1975 | { | |
1976 | wxGridCell *cell = GetCell(row, col); | |
1977 | if (cell) | |
1978 | { | |
1979 | cell->SetBackgroundColour(val); | |
1980 | RefreshCell(row, col); | |
1981 | } | |
1982 | } | |
1983 | ||
1984 | wxColour& wxGenericGrid::GetCellBackgroundColour(int row, int col) | |
1985 | { | |
1986 | wxGridCell *cell = GetCell(row, col); | |
1987 | if (cell) | |
1988 | return cell->GetBackgroundColour(); | |
1989 | else | |
da938cfd | 1990 | return m_cellBackgroundColour; |
c801d85f KB |
1991 | } |
1992 | ||
1993 | void wxGenericGrid::SetCellTextColour(const wxColour& val, int row, int col) | |
1994 | { | |
1995 | wxGridCell *cell = GetCell(row, col); | |
1996 | if (cell) | |
1997 | { | |
1998 | cell->SetTextColour(val); | |
1999 | RefreshCell(row, col); | |
2000 | } | |
2001 | } | |
2002 | ||
2003 | void wxGenericGrid::SetCellTextFont(wxFont *fnt, int row, int col) | |
2004 | { | |
2005 | wxGridCell *cell = GetCell(row, col); | |
2006 | if (cell) | |
2007 | { | |
2008 | cell->SetFont(fnt); | |
2009 | RefreshCell(row, col); | |
2010 | } | |
2011 | } | |
2012 | ||
2013 | wxFont *wxGenericGrid::GetCellTextFont(int row, int col) | |
2014 | { | |
2015 | wxGridCell *cell = GetCell(row, col); | |
2016 | if (cell) | |
2017 | return cell->GetFont(); | |
2018 | else | |
da938cfd | 2019 | return m_cellTextFont; |
c801d85f KB |
2020 | } |
2021 | ||
2022 | wxColour& wxGenericGrid::GetCellTextColour(int row, int col) | |
2023 | { | |
2024 | wxGridCell *cell = GetCell(row, col); | |
2025 | if (cell) | |
2026 | return cell->GetTextColour(); | |
2027 | else | |
da938cfd | 2028 | return m_cellTextColour; |
c801d85f KB |
2029 | } |
2030 | ||
2031 | void wxGenericGrid::SetCellTextColour(const wxColour& val) | |
2032 | { | |
da938cfd | 2033 | m_cellTextColour = val; |
c801d85f KB |
2034 | int i,j; |
2035 | for (i = 0; i < GetRows(); i++) | |
2036 | for (j = 0; j < GetCols(); j++) | |
2037 | if (GetCell(i, j)) | |
2038 | GetCell(i, j)->SetTextColour(val); | |
2039 | } | |
2040 | ||
2041 | void wxGenericGrid::SetCellTextFont(wxFont *fnt) | |
2042 | { | |
da938cfd | 2043 | m_cellTextFont = fnt; |
c801d85f KB |
2044 | int i,j; |
2045 | for (i = 0; i < GetRows(); i++) | |
2046 | for (j = 0; j < GetCols(); j++) | |
2047 | if (GetCell(i, j)) | |
2048 | GetCell(i, j)->SetFont(fnt); | |
2049 | } | |
2050 | ||
2051 | void wxGenericGrid::SetCellBitmap(wxBitmap *bitmap, int row, int col) | |
2052 | { | |
2053 | wxGridCell *cell = GetCell(row, col); | |
2054 | if (cell) | |
2055 | { | |
2056 | cell->SetCellBitmap(bitmap); | |
2057 | RefreshCell(row, col); | |
2058 | } | |
2059 | } | |
2060 | ||
2061 | wxBitmap *wxGenericGrid::GetCellBitmap(int row, int col) | |
2062 | { | |
2063 | wxGridCell *cell = GetCell(row, col); | |
2064 | if (cell) | |
2065 | { | |
2066 | return cell->GetCellBitmap(); | |
2067 | } | |
2068 | else | |
c67daf87 | 2069 | return (wxBitmap *) NULL; |
c801d85f KB |
2070 | } |
2071 | ||
2072 | bool wxGenericGrid::InsertCols(int pos, int n, bool updateLabels) | |
2073 | { | |
da938cfd | 2074 | if (pos > m_totalCols) |
c801d85f KB |
2075 | return FALSE; |
2076 | ||
da938cfd | 2077 | if (!m_gridCells) |
c801d85f KB |
2078 | return CreateGrid(1, n); |
2079 | else | |
2080 | { | |
2081 | int i, j; | |
2082 | // Cells | |
da938cfd | 2083 | for (i = 0; i < m_totalRows; i++) |
c801d85f | 2084 | { |
da938cfd JS |
2085 | wxGridCell **cols = m_gridCells[i]; |
2086 | wxGridCell **newCols = new wxGridCell *[m_totalCols + n]; | |
c801d85f KB |
2087 | for (j = 0; j < pos; j++) |
2088 | newCols[j] = cols[j]; | |
2089 | for (j = pos; j < pos + n; j++) | |
2090 | newCols[j] = new wxGridCell(this); | |
da938cfd | 2091 | for (j = pos + n; j < m_totalCols + n; j++) |
c801d85f KB |
2092 | newCols[j] = cols[j - n]; |
2093 | ||
2094 | delete[] cols; | |
da938cfd | 2095 | m_gridCells[i] = newCols; |
c801d85f KB |
2096 | } |
2097 | ||
2098 | // Column widths | |
da938cfd | 2099 | short *newColWidths = new short[m_totalCols + n]; |
c801d85f | 2100 | for (j = 0; j < pos; j++) |
da938cfd | 2101 | newColWidths[j] = m_colWidths[j]; |
c801d85f KB |
2102 | for (j = pos; j < pos + n; j++) |
2103 | newColWidths[j] = wxGRID_DEFAULT_CELL_WIDTH; | |
da938cfd JS |
2104 | for (j = pos + n; j < m_totalCols + n; j++) |
2105 | newColWidths[j] = m_colWidths[j - n]; | |
2106 | delete[] m_colWidths; | |
2107 | m_colWidths = newColWidths; | |
c801d85f KB |
2108 | |
2109 | // Column labels | |
da938cfd | 2110 | wxGridCell **newLabels = new wxGridCell *[m_totalCols + n]; |
c801d85f | 2111 | for (j = 0; j < pos; j++) |
da938cfd | 2112 | newLabels[j] = m_colLabelCells[j]; |
c801d85f KB |
2113 | for (j = pos; j < pos + n; j++) |
2114 | newLabels[j] = new wxGridCell(this); | |
da938cfd JS |
2115 | for (j = pos + n; j < m_totalCols + n; j++) |
2116 | newLabels[j] = m_colLabelCells[j - n]; | |
2117 | ||
2118 | delete[] m_colLabelCells; | |
2119 | m_colLabelCells = newLabels; | |
2120 | ||
2121 | m_totalCols += n; | |
c801d85f KB |
2122 | |
2123 | if (updateLabels) | |
2124 | OnChangeLabels(); | |
2125 | UpdateDimensions(); | |
2126 | AdjustScrollbars(); | |
2127 | return TRUE; | |
2128 | } | |
2129 | } | |
2130 | ||
2131 | bool wxGenericGrid::InsertRows(int pos, int n, bool updateLabels) | |
2132 | { | |
da938cfd | 2133 | if (pos > m_totalRows) |
c801d85f KB |
2134 | return FALSE; |
2135 | ||
da938cfd | 2136 | if (!m_gridCells) |
c801d85f KB |
2137 | return CreateGrid(n, 1); |
2138 | else | |
2139 | { | |
2140 | int i, j; | |
2141 | ||
da938cfd | 2142 | wxGridCell ***rows = new wxGridCell **[m_totalRows + n]; |
c801d85f KB |
2143 | |
2144 | // Cells | |
2145 | for (i = 0; i < pos; i++) | |
da938cfd | 2146 | rows[i] = m_gridCells[i]; |
c801d85f KB |
2147 | |
2148 | for (i = pos; i < pos + n; i++) | |
2149 | { | |
da938cfd JS |
2150 | rows[i] = new wxGridCell *[m_totalCols]; |
2151 | for (j = 0; j < m_totalCols; j++) | |
c801d85f KB |
2152 | rows[i][j] = new wxGridCell(this); |
2153 | } | |
2154 | ||
da938cfd JS |
2155 | for (i = pos + n; i < m_totalRows + n; i++) |
2156 | rows[i] = m_gridCells[i - n]; | |
2157 | ||
2158 | delete[] m_gridCells; | |
2159 | m_gridCells = rows; | |
c801d85f KB |
2160 | |
2161 | // Row heights | |
da938cfd | 2162 | short *newRowHeights = new short[m_totalRows + n]; |
c801d85f | 2163 | for (i = 0; i < pos; i++) |
da938cfd | 2164 | newRowHeights[i] = m_rowHeights[i]; |
c801d85f KB |
2165 | for (i = pos; i < pos + n; i++) |
2166 | newRowHeights[i] = wxGRID_DEFAULT_CELL_HEIGHT; | |
da938cfd JS |
2167 | for (i = pos + n; i < m_totalRows + n; i++) |
2168 | newRowHeights[i] = m_rowHeights[i - n]; | |
2169 | delete[] m_rowHeights; | |
2170 | m_rowHeights = newRowHeights; | |
c801d85f KB |
2171 | |
2172 | // Column labels | |
da938cfd | 2173 | wxGridCell **newLabels = new wxGridCell *[m_totalRows + n]; |
c801d85f | 2174 | for (i = 0; i < pos; i++) |
da938cfd | 2175 | newLabels[i] = m_rowLabelCells[i]; |
c801d85f KB |
2176 | for (i = pos; i < pos + n; i++) |
2177 | newLabels[i] = new wxGridCell(this); | |
da938cfd JS |
2178 | for (i = pos + n; i < m_totalRows + n; i++) |
2179 | newLabels[i] = m_rowLabelCells[i - n]; | |
2180 | ||
2181 | delete[] m_rowLabelCells; | |
2182 | m_rowLabelCells = newLabels; | |
2183 | ||
2184 | m_totalRows += n; | |
c801d85f KB |
2185 | |
2186 | if (updateLabels) | |
2187 | OnChangeLabels(); | |
2188 | UpdateDimensions(); | |
2189 | AdjustScrollbars(); | |
2190 | return TRUE; | |
2191 | } | |
2192 | } | |
2193 | ||
2194 | bool wxGenericGrid::AppendCols(int n, bool updateLabels) | |
2195 | { | |
2196 | return InsertCols(GetCols(), n, updateLabels); | |
2197 | } | |
2198 | ||
2199 | bool wxGenericGrid::AppendRows(int n, bool updateLabels) | |
2200 | { | |
2201 | return InsertRows(GetRows(), n, updateLabels); | |
2202 | } | |
2203 | ||
2204 | bool wxGenericGrid::DeleteRows(int pos, int n, bool updateLabels) | |
2205 | { | |
da938cfd | 2206 | if (pos > m_totalRows) |
c801d85f | 2207 | return FALSE; |
da938cfd | 2208 | if (!m_gridCells) |
c801d85f KB |
2209 | return FALSE; |
2210 | ||
2211 | int i; | |
2212 | ||
da938cfd | 2213 | wxGridCell ***rows = new wxGridCell **[m_totalRows - n]; |
c801d85f KB |
2214 | |
2215 | // Cells | |
2216 | for (i = 0; i < pos; i++) | |
da938cfd | 2217 | rows[i] = m_gridCells[i]; |
c801d85f | 2218 | |
da938cfd JS |
2219 | for (i = pos + n; i < m_totalRows; i++) |
2220 | rows[i-n] = m_gridCells[i]; | |
2221 | ||
2222 | delete[] m_gridCells; | |
2223 | m_gridCells = rows; | |
c801d85f KB |
2224 | |
2225 | // Row heights | |
da938cfd | 2226 | short *newRowHeights = new short[m_totalRows - n]; |
c801d85f | 2227 | for (i = 0; i < pos; i++) |
da938cfd JS |
2228 | newRowHeights[i] = m_rowHeights[i]; |
2229 | for (i = pos + n; i < m_totalRows; i++) | |
2230 | newRowHeights[i-n] = m_rowHeights[i]; | |
2231 | delete[] m_rowHeights; | |
2232 | m_rowHeights = newRowHeights; | |
c801d85f KB |
2233 | |
2234 | // Column labels | |
da938cfd | 2235 | wxGridCell **newLabels = new wxGridCell *[m_totalRows - n]; |
c801d85f | 2236 | for (i = 0; i < pos; i++) |
da938cfd JS |
2237 | newLabels[i] = m_rowLabelCells[i]; |
2238 | for (i = pos + n; i < m_totalRows; i++) | |
2239 | newLabels[i-n] = m_rowLabelCells[i]; | |
2240 | ||
2241 | delete[] m_rowLabelCells; | |
2242 | m_rowLabelCells = newLabels; | |
2243 | ||
2244 | m_totalRows -= n; | |
c801d85f KB |
2245 | |
2246 | if (updateLabels) | |
2247 | OnChangeLabels(); | |
2248 | UpdateDimensions(); | |
2249 | AdjustScrollbars(); | |
2250 | return TRUE; | |
2251 | } | |
2252 | ||
2253 | bool wxGenericGrid::DeleteCols(int pos, int n, bool updateLabels) | |
2254 | { | |
da938cfd | 2255 | if (pos + n > m_totalCols) |
c801d85f | 2256 | return FALSE; |
da938cfd | 2257 | if (!m_gridCells) |
c801d85f KB |
2258 | return FALSE; |
2259 | ||
2260 | int i, j; | |
2261 | ||
2262 | // Cells | |
da938cfd | 2263 | for (i = 0; i < m_totalRows; i++) |
c801d85f | 2264 | { |
da938cfd JS |
2265 | wxGridCell **cols = m_gridCells[i]; |
2266 | wxGridCell **newCols = new wxGridCell *[m_totalCols - n]; | |
c801d85f KB |
2267 | for (j = 0; j < pos; j++) |
2268 | newCols[j] = cols[j]; | |
2269 | for (j = pos; j < pos + n; j++) | |
2270 | delete cols[j]; | |
da938cfd | 2271 | for (j = pos + n; j < m_totalCols; j++) |
c801d85f KB |
2272 | newCols[j-n] = cols[j]; |
2273 | ||
2274 | delete[] cols; | |
da938cfd | 2275 | m_gridCells[i] = newCols; |
c801d85f KB |
2276 | } |
2277 | ||
2278 | // Column widths | |
da938cfd | 2279 | short *newColWidths = new short[m_totalCols - n]; |
c801d85f | 2280 | for (j = 0; j < pos; j++) |
da938cfd JS |
2281 | newColWidths[j] = m_colWidths[j]; |
2282 | for (j = pos + n; j < m_totalCols; j++) | |
2283 | newColWidths[j-n] = m_colWidths[j]; | |
2284 | delete[] m_colWidths; | |
2285 | m_colWidths = newColWidths; | |
c801d85f KB |
2286 | |
2287 | // Column labels | |
da938cfd | 2288 | wxGridCell **newLabels = new wxGridCell *[m_totalCols - n]; |
c801d85f | 2289 | for (j = 0; j < pos; j++) |
da938cfd JS |
2290 | newLabels[j] = m_colLabelCells[j]; |
2291 | for (j = pos + n; j < m_totalCols; j++) | |
2292 | newLabels[j-n] = m_colLabelCells[j]; | |
2293 | ||
2294 | delete[] m_colLabelCells; | |
2295 | m_colLabelCells = newLabels; | |
2296 | ||
2297 | m_totalCols -= n; | |
c801d85f KB |
2298 | |
2299 | if (updateLabels) | |
2300 | OnChangeLabels(); | |
2301 | UpdateDimensions(); | |
2302 | AdjustScrollbars(); | |
2303 | return TRUE; | |
2304 | } | |
2305 | ||
2306 | void wxGenericGrid::SetGridCursor(int row, int col) | |
2307 | { | |
da938cfd | 2308 | if (row >= m_totalRows || col >= m_totalCols) |
c801d85f KB |
2309 | return; |
2310 | ||
2311 | if (row == GetCursorRow() && col == GetCursorColumn()) | |
2312 | return; | |
2313 | ||
2314 | wxClientDC dc(this); | |
2315 | dc.BeginDrawing(); | |
2316 | ||
2317 | SetGridClippingRegion(& dc); | |
2318 | ||
da938cfd | 2319 | if (m_currentRectVisible) |
c801d85f KB |
2320 | HighlightCell(& dc); |
2321 | ||
da938cfd JS |
2322 | m_wCursorRow = row; |
2323 | m_wCursorColumn = col; | |
c801d85f | 2324 | SetCurrentRect(row, col); |
da938cfd | 2325 | if (m_currentRectVisible) |
c801d85f KB |
2326 | HighlightCell(& dc); |
2327 | ||
2328 | dc.DestroyClippingRegion(); | |
2329 | dc.EndDrawing(); | |
2330 | } | |
2331 | ||
2332 | /* | |
2333 | * Grid cell | |
2334 | */ | |
2335 | ||
2336 | wxGridCell::wxGridCell(wxGenericGrid *window) | |
2337 | { | |
c67daf87 UR |
2338 | cellBitmap = (wxBitmap *) NULL; |
2339 | font = (wxFont *) NULL; | |
2340 | backgroundBrush = (wxBrush *) NULL; | |
c801d85f KB |
2341 | if (window) |
2342 | textColour = window->GetCellTextColour(); | |
2343 | else | |
2344 | textColour.Set(0,0,0); | |
2345 | if (window) | |
2346 | backgroundColour = window->GetCellBackgroundColour(); | |
2347 | else | |
2348 | backgroundColour.Set(255,255,255); | |
2349 | ||
2350 | if (window) | |
2351 | font = window->GetCellTextFont(); | |
2352 | else | |
2353 | font = wxTheFontList->FindOrCreateFont(12, wxSWISS, wxNORMAL, wxNORMAL); | |
2354 | ||
2355 | SetBackgroundColour(backgroundColour); | |
2356 | ||
2357 | if (window) | |
2358 | alignment = window->GetCellAlignment(); | |
2359 | else | |
2360 | alignment = wxLEFT; | |
2361 | } | |
2362 | ||
2363 | wxGridCell::~wxGridCell(void) | |
2364 | { | |
2365 | } | |
2366 | ||
2367 | void wxGridCell::SetBackgroundColour(const wxColour& colour) | |
2368 | { | |
2369 | backgroundColour = colour; | |
2370 | backgroundBrush = wxTheBrushList->FindOrCreateBrush(backgroundColour, wxSOLID); | |
2371 | } | |
2372 | ||
2373 | void wxGenericGrid::OnText(wxCommandEvent& WXUNUSED(ev) ) | |
2374 | { | |
2375 | wxGenericGrid *grid = this; | |
2376 | wxGridCell *cell = grid->GetCell(grid->GetCursorRow(), grid->GetCursorColumn()); | |
2377 | if (cell && grid->CurrentCellVisible()) | |
2378 | { | |
2379 | cell->SetTextValue(grid->GetTextItem()->GetValue()); | |
2380 | wxClientDC dc(grid); | |
2381 | ||
2382 | dc.BeginDrawing(); | |
2383 | grid->SetGridClippingRegion(& dc); | |
2384 | grid->DrawCellBackground(& dc, &grid->GetCurrentRect(), grid->GetCursorRow(), grid->GetCursorColumn()); | |
2385 | grid->DrawCellValue(& dc, &grid->GetCurrentRect(), grid->GetCursorRow(), grid->GetCursorColumn()); | |
2386 | grid->HighlightCell(& dc); | |
2387 | dc.DestroyClippingRegion(); | |
2388 | dc.EndDrawing(); | |
2389 | ||
2390 | grid->OnCellChange(grid->GetCursorRow(), grid->GetCursorColumn()); | |
2391 | ||
2392 | // grid->DrawCellText(); | |
2393 | } | |
2394 | } | |
2395 | ||
2396 | void wxGenericGrid::OnGridScroll(wxScrollEvent& ev) | |
2397 | { | |
2398 | static bool inScroll = FALSE; | |
2399 | ||
2400 | if ( inScroll ) | |
2401 | return; | |
2402 | ||
2403 | inScroll = TRUE; | |
2404 | wxGenericGrid *win = this; | |
2405 | ||
2406 | bool change = FALSE; | |
2407 | ||
2408 | if (ev.GetEventObject() == win->GetHorizScrollBar()) | |
2409 | { | |
da938cfd | 2410 | change = (ev.GetPosition() != m_scrollPosX); |
c801d85f KB |
2411 | win->SetScrollPosX(ev.GetPosition()); |
2412 | } | |
2413 | else | |
2414 | { | |
da938cfd | 2415 | change = (ev.GetPosition() != m_scrollPosY); |
c801d85f KB |
2416 | win->SetScrollPosY(ev.GetPosition()); |
2417 | } | |
2418 | ||
2419 | win->UpdateDimensions(); | |
8aa04e8b | 2420 | |
c801d85f KB |
2421 | win->SetCurrentRect(win->GetCursorRow(), win->GetCursorColumn()); |
2422 | ||
2423 | // Because rows and columns can be arbitrary sizes, | |
2424 | // the scrollbars will need to be adjusted to reflect the | |
2425 | // current view. | |
2426 | AdjustScrollbars(); | |
2427 | ||
2428 | if (change) win->Refresh(FALSE); | |
2429 | inScroll = FALSE; | |
2430 | ||
2431 | } |