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