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