1 /////////////////////////////////////////////////////////////////////////////
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
9 // Added keyboard navigation, client data, other fixes
12 // Copyright: (c) Julian Smart and Markus Holzem
13 // Licence: wxWindows license
14 /////////////////////////////////////////////////////////////////////////////
17 #pragma implementation "gridg.h"
21 // For compilers that support precompilation, includes "wx/wx.h".
22 #include "wx/wxprec.h"
30 #include "wx/dcclient.h"
31 #include "wx/dcmemory.h"
32 #include "wx/textctrl.h"
33 #include "wx/settings.h"
38 #include "wx/string.h"
40 #include "wx/generic/gridg.h"
42 // Set to zero to use no double-buffering
44 #define wxUSE_DOUBLE_BUFFERING 1
46 #define wxUSE_DOUBLE_BUFFERING 0
49 #define wxGRID_DRAG_NONE 0
50 #define wxGRID_DRAG_LEFT_RIGHT 1
51 #define wxGRID_DRAG_UP_DOWN 2
53 IMPLEMENT_DYNAMIC_CLASS(wxGenericGrid
, wxPanel
)
54 IMPLEMENT_DYNAMIC_CLASS(wxGridEvent
, wxEvent
)
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
)
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
)
82 wxGenericGrid::wxGenericGrid()
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
;
101 m_editInPlace
= TRUE
;
102 m_inOnTextInPlace
= FALSE
;
104 #if defined(__WIN95__)
105 m_scrollWidth
= wxSystemSettings::GetSystemMetric(wxSYS_VSCROLL_X
);
106 #elif defined(__WXGTK__)
107 m_scrollWidth
= wxSystemSettings::GetSystemMetric(wxSYS_VSCROLL_X
);
111 m_dragStatus
= wxGRID_DRAG_NONE
;
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
;
135 m_editCreated
= FALSE
;
138 m_gridCells
= (wxGridCell
***) NULL
;
139 m_rowLabelCells
= (wxGridCell
**) NULL
;
140 m_colLabelCells
= (wxGridCell
**) NULL
;
141 m_textItem
= (wxTextCtrl
*) NULL
;
144 bool wxGenericGrid::Create(wxWindow
*parent
,
149 const wxString
& name
)
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
;
168 #if defined(__WIN95__)
169 m_scrollWidth
= wxSystemSettings::GetSystemMetric(wxSYS_VSCROLL_X
);
170 #elif defined(__WXGTK__)
171 m_scrollWidth
= wxSystemSettings::GetSystemMetric(wxSYS_VSCROLL_X
);
175 m_dragStatus
= wxGRID_DRAG_NONE
;
177 m_dragStartPosition
= 0;
178 m_dragLastPosition
= 0;
179 m_divisionPen
= * wxThePenList
->FindOrCreatePen("LIGHT GREY", 1, wxSOLID
);
180 m_doubleBufferingBitmap
= (wxBitmap
*) NULL
;
182 if (!m_horizontalSashCursor
.Ok())
184 m_horizontalSashCursor
= wxCursor(wxCURSOR_SIZEWE
);
185 m_verticalSashCursor
= wxCursor(wxCURSOR_SIZENS
);
188 SetLabelBackgroundColour(m_labelBackgroundColour
);
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
;
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
;
213 /* Store the rect. coordinates for the current cell */
214 SetCurrentRect(m_wCursorRow
, m_wCursorColumn
);
216 m_editCreated
= FALSE
;
220 m_gridCells
= (wxGridCell
***) NULL
;
221 m_rowLabelCells
= (wxGridCell
**) NULL
;
222 m_colLabelCells
= (wxGridCell
**) NULL
;
223 m_textItem
= (wxTextCtrl
*) NULL
;
225 wxPanel::Create(parent
, id
, pos
, size
, style
, name
);
227 m_editingPanel
= new wxPanel(this);
229 m_textItem
= new wxTextCtrl(m_editingPanel
, wxGRID_TEXT_CTRL
, "",
230 wxPoint(m_editControlPosition
.x
, m_editControlPosition
.y
),
231 wxSize(m_editControlPosition
.width
, -1),
233 m_textItem
->Show(TRUE
);
234 m_textItem
->SetFocus();
235 int controlW
, controlH
;
237 m_textItem
->GetSize(&controlW
, &controlH
);
238 m_editControlPosition
.height
= controlH
;
240 m_topOfSheet
= m_editControlPosition
.y
+ controlH
+ 2;
242 m_editCreated
= TRUE
;
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
);
247 // SetSize(pos.x, pos.y, size.x, size.y);
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();
259 wxGenericGrid::~wxGenericGrid()
264 void wxGenericGrid::ClearGrid()
269 for (i
= 0; i
< m_totalRows
; i
++)
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
];
276 delete[] m_gridCells
;
277 m_gridCells
= (wxGridCell
***) NULL
;
280 delete[] m_colWidths
;
281 m_colWidths
= (short *) NULL
;
283 delete[] m_rowHeights
;
284 m_rowHeights
= (short *) NULL
;
288 for (i
= 0; i
< m_totalRows
; i
++)
289 delete m_rowLabelCells
[i
];
290 delete[] m_rowLabelCells
;
291 m_rowLabelCells
= (wxGridCell
**) NULL
;
295 for (i
= 0; i
< m_totalCols
; i
++)
296 delete m_colLabelCells
[i
];
297 delete[] m_colLabelCells
;
298 m_colLabelCells
= (wxGridCell
**) NULL
;
300 if (m_doubleBufferingBitmap
)
302 delete m_doubleBufferingBitmap
;
303 m_doubleBufferingBitmap
= (wxBitmap
*) NULL
;
307 bool wxGenericGrid::CreateGrid(int nRows
, int nCols
, wxString
**cellValues
, short *widths
,
308 short defaultWidth
, short defaultHeight
)
314 m_colWidths
= new short[nCols
];
315 m_rowHeights
= new short[nRows
];
316 for (i
= 0; i
< nCols
; i
++)
318 m_colWidths
[i
] = widths
[i
];
320 m_colWidths
[i
] = defaultWidth
;
321 for (i
= 0; i
< nRows
; i
++)
322 m_rowHeights
[i
] = defaultHeight
;
324 m_gridCells
= new wxGridCell
**[nRows
];
326 for (i
= 0; i
< nRows
; i
++)
327 m_gridCells
[i
] = new wxGridCell
*[nCols
];
329 for (i
= 0; i
< nRows
; i
++)
330 for (j
= 0; j
< nCols
; j
++)
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
]);
340 m_gridCells
[i
][j
] = (wxGridCell
*) NULL
;
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);
349 m_wCursorRow
= m_wCursorColumn
= 0;
350 SetCurrentRect(0, 0);
352 // Need to determine various dimensions
356 int objectSizeX
= m_totalCols
;
358 int viewLengthX
= m_totalCols
;
361 m_hScrollBar->SetViewLength(viewLengthX);
362 m_hScrollBar->SetObjectLength(objectSizeX);
363 m_hScrollBar->SetPageSize(pageSizeX);
365 m_hScrollBar
->SetScrollbar(m_hScrollBar
->GetThumbPosition(), pageSizeX
, objectSizeX
, viewLengthX
);
367 int objectSizeY
= m_totalRows
;
369 int viewLengthY
= m_totalRows
;
372 m_vScrollBar->SetViewLength(viewLengthY);
373 m_vScrollBar->SetObjectLength(objectSizeY);
374 m_vScrollBar->SetPageSize(pageSizeY);
377 m_vScrollBar
->SetScrollbar(m_vScrollBar
->GetThumbPosition(), pageSizeY
, objectSizeY
, viewLengthY
);
382 wxGridEvent
g_evt(GetId(), wxEVT_GRID_CHANGE_LABELS
, this);
383 GetEventHandler()->ProcessEvent(g_evt
);
385 //OnChangeSelectionLabel();
386 wxGridEvent
g_evt2(GetId(), wxEVT_GRID_CHANGE_SEL_LABEL
, this);
387 GetEventHandler()->ProcessEvent(g_evt2
);
392 // Need to determine various dimensions
393 void wxGenericGrid::UpdateDimensions()
395 int canvasWidth
, canvasHeight
;
396 GetSize(&canvasWidth
, &canvasHeight
);
398 if (m_editCreated
&& m_editable
)
400 int controlW
, controlH
;
401 GetTextItem()->GetSize(&controlW
, &controlH
);
402 m_topOfSheet
= m_editControlPosition
.y
+ controlH
+ 2;
406 m_rightOfSheet
= m_leftOfSheet
+ m_verticalLabelWidth
;
408 for (i
= m_scrollPosX
; i
< m_totalCols
; i
++)
410 if (m_rightOfSheet
> canvasWidth
)
413 m_rightOfSheet
+= m_colWidths
[i
];
415 m_bottomOfSheet
= m_topOfSheet
+ m_horizontalLabelHeight
;
416 for (i
= m_scrollPosY
; i
< m_totalRows
; i
++)
418 if (m_bottomOfSheet
> canvasHeight
)
421 m_bottomOfSheet
+= m_rowHeights
[i
];
424 m_totalGridWidth
= m_leftOfSheet
+ m_verticalLabelWidth
;
425 for (i
= 0; i
< m_totalCols
; i
++)
427 m_totalGridWidth
+= m_colWidths
[i
];
429 m_totalGridHeight
= m_topOfSheet
+ m_horizontalLabelHeight
;
430 for (i
= 0; i
< m_totalRows
; i
++)
432 m_totalGridHeight
+= m_rowHeights
[i
];
436 wxGridCell
*wxGenericGrid::GetCell(int row
, int col
) const
439 return (wxGridCell
*) NULL
;
441 if ((row
>= m_totalRows
) || (col
>= m_totalCols
))
442 return (wxGridCell
*) NULL
;
444 wxGridCell
*cell
= m_gridCells
[row
][col
];
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
];
457 void wxGenericGrid::SetGridClippingRegion(wxDC
*dc
)
459 int m_scrollWidthHoriz
= 0;
460 int m_scrollWidthVert
= 0;
462 GetClientSize(&cw
, &ch
);
464 if (m_hScrollBar
&& m_hScrollBar
->IsShown())
465 m_scrollWidthHoriz
= m_scrollWidth
;
466 if (m_vScrollBar
&& m_vScrollBar
->IsShown())
467 m_scrollWidthVert
= m_scrollWidth
;
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
);
474 void wxGenericGrid::OnPaint(wxPaintEvent
& WXUNUSED(event
))
477 GetClientSize(&w
, &h
);
479 bool useDoubleBuffering
= (bool) wxUSE_DOUBLE_BUFFERING
;
480 if (useDoubleBuffering
)
482 // Reuse the old bitmap if possible
484 if (!m_doubleBufferingBitmap
||
485 (m_doubleBufferingBitmap
->GetWidth() < w
|| m_doubleBufferingBitmap
->GetHeight() < h
))
487 if (m_doubleBufferingBitmap
)
488 delete m_doubleBufferingBitmap
;
489 m_doubleBufferingBitmap
= new wxBitmap(w
, h
);
491 if (!m_doubleBufferingBitmap
|| !m_doubleBufferingBitmap
->Ok())
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
;
502 if (useDoubleBuffering
)
504 wxPaintDC
paintDC(this);
505 wxMemoryDC
dc(& paintDC
);
506 dc
.SelectObject(* m_doubleBufferingBitmap
);
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;
517 paintDC
.Blit(m_leftOfSheet
, m_topOfSheet
, w
- vertScrollBarWidth
- m_leftOfSheet
, h
- horizScrollBarHeight
- m_topOfSheet
,
518 &dc
, m_leftOfSheet
, m_topOfSheet
, wxCOPY
);
520 dc
.SelectObject(wxNullBitmap
);
529 void wxGenericGrid::PaintGrid(wxDC
& dc
)
532 dc
.SetOptimization(FALSE
);
534 SetGridClippingRegion(& dc
);
536 DrawLabelAreas(& dc
);
538 DrawEditableArea(& dc
);
539 DrawColumnLabels(& dc
);
544 /* Hilight present cell */
545 SetCurrentRect(m_wCursorRow
, m_wCursorColumn
);
546 if (m_currentRectVisible
&& !(m_editable
&& m_editInPlace
))
549 dc
.DestroyClippingRegion();
550 dc
.SetOptimization(TRUE
);
554 // Erase (some of) the background.
555 // Currently, a Windows-only optimisation.
556 void wxGenericGrid::OnEraseBackground(wxEraseEvent
& WXUNUSED(event
) )
560 dc
.SetOptimization(FALSE
);
563 GetClientSize(& w
, & h
);
564 dc
.SetBrush(*wxLIGHT_GREY_BRUSH
);
565 dc
.SetPen(*wxLIGHT_GREY_PEN
);
567 if (m_hScrollBar
&& m_hScrollBar
->IsShown() && m_vScrollBar
&& m_vScrollBar
->IsShown())
569 dc
.DrawRectangle(w
- m_scrollWidth
, h
- m_scrollWidth
, m_scrollWidth
, m_scrollWidth
);
572 dc
.SetOptimization(TRUE
);
577 void wxGenericGrid::DrawLabelAreas(wxDC
*dc
)
580 GetClientSize(&cw
, &ch
);
582 dc
->SetPen(*wxTRANSPARENT_PEN
);
583 // dc->SetBrush(*dc->GetBackground());
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);
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
);
597 void wxGenericGrid::DrawEditableArea(wxDC
*dc
)
600 GetClientSize(&cw
, &ch
);
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
));
610 void wxGenericGrid::DrawGridLines(wxDC
*dc
)
613 GetClientSize(&cw
, &ch
);
617 if (m_divisionPen
.Ok())
619 dc
->SetPen(m_divisionPen
);
621 int heightCount
= m_topOfSheet
+ m_horizontalLabelHeight
;
623 // Draw horizontal grey lines for cells
624 for (i
= m_scrollPosY
; i
< (m_totalRows
+1); i
++)
626 if (heightCount
> ch
)
630 dc
->DrawLine(m_leftOfSheet
, heightCount
,
633 heightCount
+= m_rowHeights
[i
];
638 if (m_verticalLabelWidth
> 0)
640 dc
->SetPen(*wxBLACK_PEN
);
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
++)
646 if (heightCount
> ch
)
650 dc
->DrawLine(m_leftOfSheet
, heightCount
,
651 m_verticalLabelWidth
, heightCount
);
653 heightCount
+= m_rowHeights
[i
];
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
);
662 dc
->SetPen(*wxWHITE_PEN
);
664 // Draw highlights on row labels
665 heightCount
= m_topOfSheet
+ m_horizontalLabelHeight
;
666 for (i
= m_scrollPosY
; i
< m_totalRows
; i
++)
668 if (heightCount
> ch
)
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
];
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
);
687 if (m_divisionPen
.Ok())
689 dc
->SetPen(m_divisionPen
);
691 // Draw vertical grey lines for cells
692 int widthCount
= m_leftOfSheet
+ m_verticalLabelWidth
;
693 for (i
= m_scrollPosX
; i
< m_totalCols
; i
++)
699 // Skip the first one
700 if (i
!= m_scrollPosX
)
702 dc
->DrawLine(widthCount
, m_topOfSheet
+ m_horizontalLabelHeight
,
703 widthCount
, m_bottomOfSheet
);
705 widthCount
+= m_colWidths
[i
];
709 dc
->DrawLine(widthCount
, m_topOfSheet
+ m_horizontalLabelHeight
,
710 widthCount
, m_bottomOfSheet
);
713 dc
->SetPen(*wxBLACK_PEN
);
715 // Draw two black horizontal lines for column number cells
717 m_leftOfSheet
, m_topOfSheet
,
719 dc
->DrawLine(m_leftOfSheet
, m_topOfSheet
+ m_horizontalLabelHeight
,
720 cw
, m_topOfSheet
+ m_horizontalLabelHeight
);
722 if (m_horizontalLabelHeight
> 0)
724 int widthCount
= m_leftOfSheet
+ m_verticalLabelWidth
;
726 // Draw black vertical lines for column number cells
727 for (i
= m_scrollPosX
; i
< m_totalCols
; i
++)
733 dc
->DrawLine(widthCount
, m_topOfSheet
,
734 widthCount
, m_topOfSheet
+ m_horizontalLabelHeight
);
735 widthCount
+= m_colWidths
[i
];
740 dc
->DrawLine(widthCount
, m_topOfSheet
,
741 widthCount
, m_topOfSheet
+ m_horizontalLabelHeight
);
744 dc
->SetPen(*wxWHITE_PEN
);
745 widthCount
= m_leftOfSheet
+ m_verticalLabelWidth
;
747 for (i
= m_scrollPosX
; i
< m_totalCols
; i
++)
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
];
760 // Last one - to the right side of the canvas.
761 dc
->DrawLine(widthCount
+1, m_topOfSheet
+1,
763 dc
->DrawLine(widthCount
+1, m_topOfSheet
+1,
764 widthCount
+1, m_topOfSheet
+m_horizontalLabelHeight
);
769 void wxGenericGrid::DrawColumnLabels(wxDC
*dc
)
772 GetClientSize(&cw
, &ch
);
774 if (m_horizontalLabelHeight
== 0)
780 // Draw letters for columns
781 rect
.y
= m_topOfSheet
+ 1;
782 rect
.height
= m_horizontalLabelHeight
- 1;
784 dc
->SetTextBackground(m_labelBackgroundColour
);
785 dc
->SetBackgroundMode(wxTRANSPARENT
);
786 // dc->SetTextForeground(m_labelTextColour);
788 int widthCount
= m_leftOfSheet
+ m_verticalLabelWidth
;
789 for (i
= m_scrollPosX
; i
< m_totalCols
; i
++)
795 rect
.x
= 1 + widthCount
;
796 rect
.width
= m_colWidths
[i
];
797 DrawColumnLabel(dc
, &rect
, i
);
799 widthCount
+= m_colWidths
[i
];
804 void wxGenericGrid::DrawColumnLabel(wxDC
*dc
, wxRect
*rect
, int col
)
806 wxGridCell
*cell
= GetLabelCell(wxHORIZONTAL
, col
);
815 dc
->SetTextForeground(GetLabelTextColour());
816 dc
->SetFont(GetLabelTextFont());
817 if ( !cell
->GetTextValue().IsNull() )
818 DrawTextRect(dc
, cell
->GetTextValue(), &rect2
, GetLabelAlignment(wxHORIZONTAL
));
822 void wxGenericGrid::DrawRowLabels(wxDC
*dc
)
825 GetClientSize(&cw
, &ch
);
827 if (m_verticalLabelWidth
== 0)
833 // Draw numbers for rows
834 rect
.x
= m_leftOfSheet
;
835 rect
.width
= m_verticalLabelWidth
;
837 int heightCount
= m_topOfSheet
+ m_horizontalLabelHeight
;
839 dc
->SetTextBackground(m_labelBackgroundColour
);
840 dc
->SetBackgroundMode(wxTRANSPARENT
);
842 for (i
= m_scrollPosY
; i
< m_totalRows
; i
++)
844 if (heightCount
> ch
)
848 rect
.y
= 1 + heightCount
;
849 rect
.height
= m_rowHeights
[i
];
850 DrawRowLabel(dc
, &rect
, i
);
852 heightCount
+= m_rowHeights
[i
];
857 void wxGenericGrid::DrawRowLabel(wxDC
*dc
, wxRect
*rect
, int row
)
859 wxGridCell
*cell
= GetLabelCell(wxVERTICAL
, row
);
868 dc
->SetTextForeground(GetLabelTextColour());
869 dc
->SetFont(GetLabelTextFont());
870 if ( !cell
->GetTextValue().IsNull() )
871 DrawTextRect(dc
, cell
->GetTextValue(), &rect2
, GetLabelAlignment(wxVERTICAL
));
875 void wxGenericGrid::DrawCells(wxDC
*dc
)
878 GetClientSize(&cw
, &ch
);
882 // Draw value corresponding to each cell
883 for (i
= m_scrollPosY
; i
< m_totalRows
; i
++)
885 for (j
= m_scrollPosX
; j
< m_totalCols
; j
++)
887 SetCurrentRect(i
, j
, cw
, ch
);
888 if (m_currentRectVisible
)
890 DrawCellBackground(dc
, &m_currentRect
, i
, j
);
891 DrawCellValue(dc
, &m_currentRect
, i
, j
);
893 if (m_currentRect
.x
> cw
)
896 if (m_currentRect
.y
> ch
)
899 dc
->SetBackgroundMode(wxSOLID
);
900 dc
->SetPen(*wxBLACK_PEN
);
903 void wxGenericGrid::DrawCellBackground(wxDC
*dc
, wxRect
*rect
, int row
, int col
)
905 wxGridCell
*cell
= GetCell(row
, col
);
908 dc
->SetBrush(cell
->GetBackgroundBrush());
909 dc
->SetPen(*wxTRANSPARENT_PEN
);
911 #if 0 // In wxWin 2.0 the dc code is exact. RR.
913 dc
->DrawRectangle(rect
->x
+1, rect
->y
+1, rect
->width
-1, rect
->height
-1);
915 dc
->DrawRectangle(rect
->x
+1, rect
->y
+1, rect
->width
, rect
->height
);
919 dc
->DrawRectangle(rect
->x
+1, rect
->y
+1, rect
->width
-1, rect
->height
-1);
921 dc
->SetPen(*wxBLACK_PEN
);
925 void wxGenericGrid::DrawCellValue(wxDC
*dc
, wxRect
*rect
, int row
, int col
)
927 wxGridCell
*cell
= GetCell(row
, col
);
930 wxBitmap
*bitmap
= cell
->GetCellBitmap();
940 DrawBitmapRect(dc
, bitmap
, &rect2
, cell
->GetAlignment());
944 dc
->SetBackgroundMode(wxTRANSPARENT
);
945 dc
->SetTextForeground(cell
->GetTextColour());
946 dc
->SetFont(cell
->GetFont());
948 if ( !cell
->GetTextValue().IsNull() )
949 DrawTextRect(dc
, cell
->GetTextValue(), &rect2
, cell
->GetAlignment());
954 void wxGenericGrid::AdjustScrollbars()
957 GetClientSize(&cw
, &ch
);
959 // We find the view size by seeing how many rows/cols fit on
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.
967 // Since this may not be known until the end of this function, we should probably call AdjustScrollbars
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;
976 int noHorizSteps
= 0;
979 if (m_totalGridWidth
+ vertScrollBarWidth
> cw
)
985 for (i
= m_scrollPosX
; i
< m_totalCols
; i
++)
987 widthCount
+= m_colWidths
[i
];
988 // A partial bit doesn't count, we still have to scroll to see the
990 if (widthCount
+ m_leftOfSheet
+ m_verticalLabelWidth
> (cw
-vertScrollBarWidth
))
998 m_viewWidth
= noHorizSteps
;
1000 if (m_totalGridHeight
+ horizScrollBarHeight
> ch
)
1002 int heightCount
= 0;
1006 for (i
= m_scrollPosY
; i
< m_totalRows
; i
++)
1008 heightCount
+= m_rowHeights
[i
];
1009 // A partial bit doesn't count, we still have to scroll to see the
1011 if (heightCount
+ m_topOfSheet
+ m_horizontalLabelHeight
> (ch
-horizScrollBarHeight
))
1020 m_viewHeight
= noVertSteps
;
1022 if (m_totalGridWidth
+ vertScrollBarWidth
<= cw
)
1025 m_hScrollBar
->Show(FALSE
);
1031 m_hScrollBar
->Show(TRUE
);
1034 if (m_totalGridHeight
+ horizScrollBarHeight
<= ch
)
1037 m_vScrollBar
->Show(FALSE
);
1043 m_vScrollBar
->Show(TRUE
);
1046 UpdateDimensions(); // Necessary in case m_scrollPosX/Y changed
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;
1055 if (m_hScrollBar
&& m_hScrollBar
->IsShown())
1057 int nCols
= GetCols();
1058 m_hScrollBar
->SetScrollbar(m_hScrollBar
->GetThumbPosition(), wxMax(noHorizSteps
, 1), (noHorizSteps
== 0) ? 1 : nCols
, wxMax(noHorizSteps
, 1));
1061 m_hScrollBar->SetSize(m_leftOfSheet, ch - m_scrollWidth -2, // why -2 ? Robert.
1062 cw - vertScrollBarWidth - m_leftOfSheet, m_scrollWidth);
1064 m_hScrollBar
->SetSize(m_leftOfSheet
, ch
- m_scrollWidth
,
1065 cw
- vertScrollBarWidth
- m_leftOfSheet
, m_scrollWidth
);
1069 if (m_vScrollBar
&& m_vScrollBar
->IsShown())
1071 int nRows
= GetRows();
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
);
1079 void wxGenericGrid::OnSize(wxSizeEvent
& WXUNUSED(event
) )
1081 if (!m_vScrollBar
|| !m_hScrollBar
)
1087 GetClientSize(&cw
, &ch
);
1089 if (m_editCreated
&& m_editingPanel
&& GetTextItem() && GetTextItem()->IsShown())
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
);
1097 bool wxGenericGrid::CellHitTest(int x
, int y
, int *row
, int *col
)
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
)
1103 // Calculate the cell number from x and y
1104 x
-= (m_verticalLabelWidth
+ m_leftOfSheet
);
1105 y
-= (m_topOfSheet
+ m_horizontalLabelHeight
);
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
++)
1113 if (y
>= currentHeight
&& y
<= (currentHeight
+ m_rowHeights
[i
]))
1118 currentHeight
+= m_rowHeights
[i
];
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
++)
1125 if (x
>= currentWidth
&& x
<= (currentWidth
+ m_colWidths
[i
]))
1130 currentWidth
+= m_colWidths
[i
];
1137 bool wxGenericGrid::LabelSashHitTest(int x
, int y
, int *orientation
, int *rowOrCol
, int *startPos
)
1142 if (x
>= (m_leftOfSheet
+ m_verticalLabelWidth
) && y
>= m_topOfSheet
&&
1143 x
<= m_rightOfSheet
&& y
<= (m_topOfSheet
+ m_horizontalLabelHeight
))
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
++)
1149 if (x
>= (currentWidth
+ m_colWidths
[i
] - tolerance
) && x
<= (currentWidth
+ m_colWidths
[i
] + tolerance
))
1151 *orientation
= wxHORIZONTAL
;
1153 *startPos
= currentWidth
;
1156 currentWidth
+= m_colWidths
[i
];
1160 else if (x
>= m_leftOfSheet
&& y
>= (m_topOfSheet
+ m_horizontalLabelHeight
) &&
1161 x
<= (m_leftOfSheet
+ m_verticalLabelWidth
) && y
<= m_bottomOfSheet
)
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
++)
1167 if (y
>= (currentHeight
+ m_rowHeights
[i
] - tolerance
) && y
<= (currentHeight
+ m_rowHeights
[i
] + tolerance
))
1169 *orientation
= wxVERTICAL
;
1171 *startPos
= currentHeight
;
1174 currentHeight
+= m_rowHeights
[i
];
1181 bool wxGenericGrid::LabelHitTest(int x
, int y
, int *row
, int *col
)
1183 // Find the selected label
1184 if (x
>= m_leftOfSheet
&& y
>= m_topOfSheet
&&
1185 x
<= m_rightOfSheet
&& y
<= m_bottomOfSheet
)
1187 // Calculate the cell number from x and y
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
++)
1197 if (y
>= currentHeight
&& y
<= (currentHeight
+ m_rowHeights
[i
]))
1202 currentHeight
+= m_rowHeights
[i
];
1204 if (y
>= 0 && y
<= m_horizontalLabelHeight
)
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
++)
1213 if (x
>= currentWidth
&& x
<= (currentWidth
+ m_colWidths
[i
]))
1218 currentWidth
+= m_colWidths
[i
];
1220 if (x
>= 0 && x
<= m_verticalLabelWidth
)
1225 if ((*col
== -1) || (*row
== -1))
1233 void wxGenericGrid::OnMouseEvent(wxMouseEvent
& ev
)
1237 wxClientDC
dc(this);
1241 if (CellHitTest((int)ev
.GetX(), (int)ev
.GetY(), &row
, &col
))
1243 OnSelectCellImplementation(& dc
, row
, col
);
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
);
1252 if (LabelHitTest((int)ev
.GetX(), (int)ev
.GetY(), &row
, &col
))
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
);
1263 else if (ev
.Dragging() && ev
.LeftIsDown())
1265 switch (m_dragStatus
)
1267 case wxGRID_DRAG_NONE
:
1270 if (LabelSashHitTest((int)ev
.GetX(), (int)ev
.GetY(), &orientation
, &m_dragRowOrCol
, &m_dragStartPosition
))
1272 if (orientation
== wxHORIZONTAL
)
1274 m_dragStatus
= wxGRID_DRAG_LEFT_RIGHT
;
1275 SetCursor(m_horizontalSashCursor
);
1276 m_dragLastPosition
= (int)ev
.GetX();
1280 m_dragStatus
= wxGRID_DRAG_UP_DOWN
;
1281 SetCursor(m_verticalSashCursor
);
1282 m_dragLastPosition
= (int)ev
.GetY();
1284 wxClientDC
dc(this);
1286 dc
.SetLogicalFunction(wxINVERT
);
1287 if (orientation
== wxHORIZONTAL
)
1288 dc
.DrawLine((int)ev
.GetX(), m_topOfSheet
, (int)ev
.GetX(), m_bottomOfSheet
);
1290 dc
.DrawLine(m_leftOfSheet
, (int)ev
.GetY(), m_rightOfSheet
, (int)ev
.GetY());
1297 case wxGRID_DRAG_LEFT_RIGHT
:
1299 wxClientDC
dc(this);
1301 dc
.SetLogicalFunction(wxINVERT
);
1302 dc
.DrawLine(m_dragLastPosition
, m_topOfSheet
, m_dragLastPosition
, m_bottomOfSheet
);
1304 dc
.DrawLine((int)ev
.GetX(), m_topOfSheet
, (int)ev
.GetX(), m_bottomOfSheet
);
1307 m_dragLastPosition
= (int)ev
.GetX();
1308 SetCursor(m_horizontalSashCursor
);
1311 case wxGRID_DRAG_UP_DOWN
:
1313 wxClientDC
dc(this);
1315 dc
.SetLogicalFunction(wxINVERT
);
1316 dc
.DrawLine(m_leftOfSheet
, m_dragLastPosition
, m_rightOfSheet
, m_dragLastPosition
);
1318 dc
.DrawLine(m_leftOfSheet
, (int)ev
.GetY(), m_rightOfSheet
, (int)ev
.GetY());
1321 m_dragLastPosition
= (int)ev
.GetY();
1322 SetCursor(m_verticalSashCursor
);
1327 else if (ev
.Moving())
1329 int rowOrCol
, orientation
, startPos
;
1330 if (LabelSashHitTest((int)ev
.GetX(), (int)ev
.GetY(), &orientation
, &rowOrCol
, &startPos
))
1332 if (orientation
== wxHORIZONTAL
)
1333 SetCursor(m_horizontalSashCursor
);
1335 SetCursor(m_verticalSashCursor
);
1338 SetCursor(*wxSTANDARD_CURSOR
);
1340 else if (ev
.LeftUp())
1342 switch (m_dragStatus
)
1344 case wxGRID_DRAG_LEFT_RIGHT
:
1346 wxClientDC
dc(this);
1348 dc
.SetLogicalFunction(wxINVERT
);
1349 dc
.DrawLine(m_dragLastPosition
, m_topOfSheet
, m_dragLastPosition
, m_bottomOfSheet
);
1350 dc
.SetLogicalFunction(wxCOPY
);
1354 if (ev
.GetX() > m_dragStartPosition
)
1356 m_colWidths
[m_dragRowOrCol
] = (short)(ev
.GetX() - m_dragStartPosition
);
1361 SetCursor(*wxSTANDARD_CURSOR
);
1363 GetClientSize(&cw
, &ch
);
1368 case wxGRID_DRAG_UP_DOWN
:
1370 wxClientDC
dc(this);
1372 dc
.SetLogicalFunction(wxINVERT
);
1373 dc
.DrawLine(m_leftOfSheet
, m_dragLastPosition
, m_rightOfSheet
, m_dragLastPosition
);
1374 dc
.SetLogicalFunction(wxCOPY
);
1378 if (ev
.GetY() > m_dragStartPosition
)
1380 m_rowHeights
[m_dragRowOrCol
] = (short)(ev
.GetY() - m_dragStartPosition
);
1385 SetCursor(*wxSTANDARD_CURSOR
);
1389 m_dragStatus
= wxGRID_DRAG_NONE
;
1391 else if (ev
.RightDown())
1394 if (CellHitTest((int)ev
.GetX(), (int)ev
.GetY(), &row
, &col
))
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
);
1403 if (LabelHitTest((int)ev
.GetX(), (int)ev
.GetY(), &row
, &col
))
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
);
1414 void wxGenericGrid::OnSelectCellImplementation(wxDC
*dc
, int row
, int col
)
1416 m_wCursorColumn
= col
;
1419 //OnChangeSelectionLabel();
1420 wxGridEvent
g_evt(GetId(), wxEVT_GRID_CHANGE_SEL_LABEL
, this);
1421 GetEventHandler()->ProcessEvent(g_evt
);
1423 SetGridClippingRegion(dc
);
1425 // Remove the highlight from the old cell
1426 if ( m_currentRectVisible
&& !(m_editable
&& m_editInPlace
) )
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
);
1437 if ( cell
->GetTextValue().IsNull() )
1438 m_textItem
->SetValue("");
1440 m_textItem
->SetValue(cell
->GetTextValue());
1443 SetGridClippingRegion(dc
);
1446 if ( m_editable
&& m_editInPlace
)
1448 m_inPlaceTextItem
->SetSize( m_currentRect
.x
-2, m_currentRect
.y
-2,
1449 m_currentRect
.width
+4, m_currentRect
.height
+4 );
1453 if ( cell
->GetTextValue().IsNull() )
1455 m_inPlaceTextItem
->SetValue( "" );
1459 m_inPlaceTextItem
->SetFont( cell
->GetFont() );
1460 m_inPlaceTextItem
->SetValue( cell
->GetTextValue() );
1464 m_inPlaceTextItem
->Show(TRUE
);
1465 m_inPlaceTextItem
->SetFocus();
1469 // 1) Why isn't this needed for Windows??
1470 // Probably because of the SetValue?? JS.
1471 // 2) Arrrrrgh. This isn't needed anywhere,
1472 // of course. One hour of debugging... RR.
1474 // 3) It *is* needed for Motif - michael
1477 if ( !(m_editable
&& m_editInPlace
) )
1482 dc
->DestroyClippingRegion();
1484 OnSelectCell(row
, col
);
1485 wxGridEvent
g_evt2(GetId(), wxEVT_GRID_SELECT_CELL
, this, row
, col
);
1486 GetEventHandler()->ProcessEvent(g_evt2
);
1489 wxGridCell
*wxGenericGrid::OnCreateCell()
1491 return new wxGridCell(this);
1494 void wxGenericGrid::OnChangeLabels()
1498 for (i
= 0; i
< m_totalRows
; i
++)
1500 sprintf(buf
, "%d", i
+1);
1501 SetLabelValue(wxVERTICAL
, buf
, i
);
1503 // A...Z,AA...ZZ,AAA...ZZZ, etc.
1504 for (i
= 0; i
< m_totalCols
; i
++)
1507 int noTimes
= (i
/26 + 1);
1508 int ch
= (i
% 26) + 65;
1510 for (j
= 0; j
< noTimes
; j
++)
1513 sprintf(buf2
, "%c", (char)ch
);
1516 SetLabelValue(wxHORIZONTAL
, buf
, i
);
1520 void wxGenericGrid::OnChangeSelectionLabel()
1525 wxString
rowLabel(GetLabelValue(wxVERTICAL
, GetCursorRow()));
1526 wxString
colLabel(GetLabelValue(wxHORIZONTAL
, GetCursorColumn()));
1528 wxString newLabel
= colLabel
+ rowLabel
;
1529 if ((newLabel
.Length() > 0) && (newLabel
.Length() <= 8) && GetTextItem())
1531 // GetTextItem()->SetLabel(newLabel);
1535 void wxGenericGrid::HighlightCell(wxDC
*dc
)
1537 dc
->SetLogicalFunction(wxINVERT
);
1540 dc
->DrawLine( m_currentRect
.x
+ 1,
1541 m_currentRect
.y
+ 1,
1542 m_currentRect
.x
+ m_currentRect
.width
- 1,
1543 m_currentRect
.y
+ 1);
1545 dc
->DrawLine( m_currentRect
.x
+ m_currentRect
.width
- 1,
1546 m_currentRect
.y
+ 1,
1547 m_currentRect
.x
+ m_currentRect
.width
- 1,
1548 m_currentRect
.y
+m_currentRect
.height
- 1 );
1550 dc
->DrawLine( m_currentRect
.x
+ m_currentRect
.width
- 1,
1551 m_currentRect
.y
+ m_currentRect
.height
- 1,
1552 m_currentRect
.x
+ 1,
1553 m_currentRect
.y
+ m_currentRect
.height
- 1);
1555 dc
->DrawLine( m_currentRect
.x
+ 1,
1556 m_currentRect
.y
+ m_currentRect
.height
- 1,
1557 m_currentRect
.x
+ 1,
1558 m_currentRect
.y
+ 1);
1560 dc
->SetLogicalFunction(wxCOPY
);
1563 void wxGenericGrid::DrawCellText()
1565 if (!m_currentRectVisible
)
1568 wxGridCell
*cell
= GetCell(GetCursorRow(), GetCursorColumn());
1572 wxClientDC
dc(this);
1575 SetGridClippingRegion(& dc
);
1577 dc
.SetBackgroundMode(wxTRANSPARENT
);
1578 dc
.SetBrush(cell
->GetBackgroundBrush());
1580 wxString editValue
= m_textItem
->GetValue();
1583 rect
= m_currentRect
;
1589 // FIXME: what's this string of spaces supposed to represent?
1590 DrawTextRect(& dc
, " ", &rect
, wxLEFT
);
1591 DrawTextRect(& dc
, editValue
, &rect
, cell
->GetAlignment());
1593 dc
.DestroyClippingRegion();
1595 dc
.SetBackgroundMode(wxSOLID
);
1600 void wxGenericGrid::SetCurrentRect(int Row
, int Column
, int canvasW
, int canvasH
)
1602 int currentWidth
= m_leftOfSheet
+ m_verticalLabelWidth
;
1604 for (i
= m_scrollPosX
; i
< Column
; i
++)
1605 currentWidth
+= m_colWidths
[i
];
1607 int currentHeight
= m_topOfSheet
+ m_horizontalLabelHeight
;
1608 for (i
= m_scrollPosY
; i
< Row
; i
++)
1609 currentHeight
+= m_rowHeights
[i
];
1611 m_currentRect
.x
= currentWidth
;
1612 m_currentRect
.y
= currentHeight
;
1613 m_currentRect
.width
= m_colWidths
? (m_colWidths
[Column
]) : 0;
1614 m_currentRect
.height
= m_rowHeights
? (m_rowHeights
[Row
]) : 0;
1616 if (Row
< m_scrollPosY
|| Column
< m_scrollPosX
)
1617 m_currentRectVisible
= FALSE
;
1618 else if ((canvasW
!= -1 && canvasH
!= -1) && (m_currentRect
.x
> canvasW
|| m_currentRect
.y
> canvasH
))
1619 m_currentRectVisible
= FALSE
;
1620 else m_currentRectVisible
= TRUE
;
1623 static bool wxRectIntersection(wxRect
*rect1
, wxRect
*rect2
, wxRect
*rect3
)
1625 int x2_1
= rect1
->x
+ rect1
->width
;
1626 int y2_1
= rect1
->y
+ rect1
->height
;
1628 int x2_2
= rect2
->x
+ rect2
->width
;
1629 int y2_2
= rect2
->y
+ rect2
->height
;
1633 // Check for intersection
1634 if ((rect1
->x
> x2_2
) || (rect2
->x
> x2_1
) ||
1635 (rect1
->y
> y2_2
) || (rect2
->y
> y2_1
))
1638 rect3
->x
= rect3
->y
= rect3
->width
= rect3
->height
= 0;
1642 if (rect1
->x
> rect2
->x
)
1643 rect3
->x
= rect1
->x
;
1645 rect3
->x
= rect2
->x
;
1646 if (rect1
->y
> rect2
->y
)
1647 rect3
->y
= rect1
->y
;
1649 rect3
->y
= rect2
->y
;
1660 rect3
->width
= (int)(x2_3
- rect3
->x
);
1661 rect3
->height
= (int)(y2_3
- rect3
->y
);
1665 void wxGenericGrid::DrawTextRect(wxDC
*dc
, const wxString
& text
, wxRect
*rect
, int flag
)
1669 // Ultimately, this functionality should be built into wxWindows,
1670 // and optimized for each platform. E.g. on Windows, use DrawText
1671 // passing a clipping rectangle, so that the wxWindows clipping region
1672 // does not have to be used to implement this.
1674 // If we're already clipping, we need to find the intersection
1675 // between current clipping area and text clipping area.
1679 long clipX
, clipY
, clipW
, clipH
;
1680 dc
->GetClippingBox(&clipX
, &clipY
, &clipW
, &clipH
);
1681 clipRect
.x
= (int)clipX
; clipRect
.y
= (int)clipY
;
1682 clipRect
.width
= (int)clipW
; clipRect
.height
= (int)clipH
;
1684 bool alreadyClipping
= TRUE
;
1686 if (clipRect
.x
== 0 && clipRect
.y
== 0 && clipRect
.width
== 0 && clipRect
.height
== 0)
1688 alreadyClipping
= FALSE
;
1689 clipRect2
.x
= rect
->x
; clipRect2
.y
= rect
->y
;
1690 clipRect2
.width
= rect
->width
; clipRect2
.height
= rect
->height
;
1694 // Find intersection.
1695 if (!wxRectIntersection(rect
, &clipRect
, &clipRect2
))
1699 if (alreadyClipping
)
1700 dc
->DestroyClippingRegion();
1702 dc
->SetClippingRegion(clipRect2
.x
, clipRect2
.y
, clipRect2
.width
, clipRect2
.height
);
1703 long textWidth
, textHeight
;
1705 dc
->GetTextExtent(text
, &textWidth
, &textHeight
);
1713 x
= (rect
->x
+ rect
->width
- textWidth
- 1.0);
1714 y
= (rect
->y
+ (rect
->height
- textHeight
)/2.0);
1719 x
= (rect
->x
+ (rect
->width
- textWidth
)/2.0);
1720 y
= (rect
->y
+ (rect
->height
- textHeight
)/2.0);
1726 x
= (rect
->x
+ 1.0);
1727 y
= (rect
->y
+ (rect
->height
- textHeight
)/2.0);
1731 dc
->DrawText(text
, (long)x
, (long)y
);
1733 dc
->DestroyClippingRegion();
1735 // Restore old clipping
1736 if (alreadyClipping
)
1737 dc
->SetClippingRegion(clipRect
.x
, clipRect
.y
, clipRect
.width
, clipRect
.height
);
1742 void wxGenericGrid::DrawBitmapRect(wxDC
*dc
, wxBitmap
*bitmap
, wxRect
*rect
, int flag
)
1746 // Ultimately, this functionality should be built into wxWindows,
1747 // and optimized for each platform. E.g. on Windows, use DrawText
1748 // passing a clipping rectangle, so that the wxWindows clipping region
1749 // does not have to be used to implement this.
1751 // If we're already clipping, we need to find the intersection
1752 // between current clipping area and text clipping area.
1756 long clipX
, clipY
, clipW
, clipH
;
1757 dc
->GetClippingBox(&clipX
, &clipY
, &clipW
, &clipH
);
1758 clipRect
.x
= (int)clipX
; clipRect
.y
= (int)clipY
;
1759 clipRect
.width
= (int)clipW
; clipRect
.height
= (int)clipH
;
1761 bool alreadyClipping
= TRUE
;
1763 if (clipRect
.x
== 0 && clipRect
.y
== 0 && clipRect
.width
== 0 && clipRect
.height
== 0)
1765 alreadyClipping
= FALSE
;
1766 clipRect2
.x
= rect
->x
; clipRect2
.y
= rect
->y
;
1767 clipRect2
.width
= rect
->width
; clipRect2
.height
= rect
->height
;
1771 // Find intersection.
1772 if (!wxRectIntersection(rect
, &clipRect
, &clipRect2
))
1776 if (alreadyClipping
)
1777 dc
->DestroyClippingRegion();
1779 dc
->SetClippingRegion(clipRect2
.x
, clipRect2
.y
, clipRect2
.width
, clipRect2
.height
);
1780 float bitmapWidth
, bitmapHeight
;
1782 bitmapWidth
= bitmap
->GetWidth();
1783 bitmapHeight
= bitmap
->GetHeight();
1791 x
= (long)(rect
->x
+ rect
->width
- bitmapWidth
- 1);
1792 y
= (long)(rect
->y
+ (rect
->height
- bitmapHeight
)/2.0);
1797 x
= (long)(rect
->x
+ (rect
->width
- bitmapWidth
)/2.0);
1798 y
= (long)(rect
->y
+ (rect
->height
- bitmapHeight
)/2.0);
1804 x
= (long)(rect
->x
+ 1);
1805 y
= (long)(rect
->y
+ (rect
->height
- bitmapHeight
)/2.0);
1810 dcTemp
.SelectObject(*bitmap
);
1812 dc
->Blit( (long)x
, (long)y
, (long)bitmapWidth
, (long)bitmapHeight
, &dcTemp
, 0, 0);
1813 dcTemp
.SelectObject(wxNullBitmap
);
1815 dc
->DestroyClippingRegion();
1817 // Restore old clipping
1818 if (alreadyClipping
)
1819 dc
->SetClippingRegion(clipRect
.x
, clipRect
.y
, clipRect
.width
, clipRect
.height
);
1824 void wxGenericGrid::OnActivate(bool active
)
1828 // Edit control should always have the focus
1829 if (GetTextItem() && GetEditable())
1831 GetTextItem()->SetFocus();
1832 wxGridCell
*cell
= GetCell(GetCursorRow(), GetCursorColumn());
1834 GetTextItem()->SetValue(cell
->GetTextValue());
1839 void wxGenericGrid::SetCellValue(const wxString
& val
, int row
, int col
)
1841 wxGridCell
*cell
= GetCell(row
, col
);
1844 cell
->SetTextValue(val
);
1846 RefreshCell(row
, col
, TRUE
);
1850 void wxGenericGrid::RefreshCell(int row
, int col
, bool setText
)
1852 // Don't refresh within a pair of batch brackets
1853 if (GetBatchCount() > 0)
1857 GetClientSize(&cw
, &ch
);
1859 SetCurrentRect(row
, col
, cw
, ch
);
1860 if (m_currentRectVisible
)
1862 wxGridCell
*cell
= GetCell(row
, col
);
1864 bool currentPos
= FALSE
;
1865 if (row
== m_wCursorRow
&& col
== m_wCursorColumn
&& GetTextItem() && GetTextItem()->IsShown() && setText
)
1867 GetTextItem()->SetValue(cell
->GetTextValue());
1870 // Gets refreshed anyway in MSW
1875 wxClientDC
dc(this);
1877 DrawCellBackground(& dc
, &m_currentRect
, row
, col
);
1878 DrawCellValue(& dc
, &m_currentRect
, row
, col
);
1884 wxString
& wxGenericGrid::GetCellValue(int row
, int col
) const
1886 static wxString
emptyString("");
1888 wxGridCell
*cell
= GetCell(row
, col
);
1890 return cell
->GetTextValue();
1895 void wxGenericGrid::SetColumnWidth(int col
, int width
)
1897 if (col
<= m_totalCols
)
1898 m_colWidths
[col
] = width
;
1901 int wxGenericGrid::GetColumnWidth(int col
) const
1903 if (col
<= m_totalCols
)
1904 return m_colWidths
[col
];
1909 void wxGenericGrid::SetRowHeight(int row
, int height
)
1911 if (row
<= m_totalRows
)
1912 m_rowHeights
[row
] = height
;
1915 int wxGenericGrid::GetRowHeight(int row
) const
1917 if (row
<= m_totalRows
)
1918 return m_rowHeights
[row
];
1923 void wxGenericGrid::SetLabelSize(int orientation
, int sz
)
1925 if (orientation
== wxHORIZONTAL
)
1926 m_horizontalLabelHeight
= sz
;
1928 m_verticalLabelWidth
= sz
;
1930 SetCurrentRect(GetCursorRow(), GetCursorColumn());
1933 int wxGenericGrid::GetLabelSize(int orientation
) const
1935 if (orientation
== wxHORIZONTAL
)
1936 return m_horizontalLabelHeight
;
1938 return m_verticalLabelWidth
;
1941 wxGridCell
*wxGenericGrid::GetLabelCell(int orientation
, int pos
) const
1943 if (orientation
== wxHORIZONTAL
)
1945 if (m_colLabelCells
&& pos
< m_totalCols
)
1946 return m_colLabelCells
[pos
];
1948 return (wxGridCell
*) NULL
;
1952 if (m_rowLabelCells
&& pos
< m_totalRows
)
1953 return m_rowLabelCells
[pos
];
1955 return (wxGridCell
*) NULL
;
1959 void wxGenericGrid::SetLabelValue(int orientation
, const wxString
& val
, int pos
)
1961 wxGridCell
*cell
= GetLabelCell(orientation
, pos
);
1963 cell
->SetTextValue(val
);
1966 wxString
& wxGenericGrid::GetLabelValue(int orientation
, int pos
) const
1968 static wxString emptyString
= "";
1969 wxGridCell
*cell
= GetLabelCell(orientation
, pos
);
1971 return cell
->GetTextValue();
1976 void wxGenericGrid::SetLabelAlignment(int orientation
, int align
)
1978 if (orientation
== wxHORIZONTAL
)
1979 m_horizontalLabelAlignment
= align
;
1981 m_verticalLabelAlignment
= align
;
1983 SetCurrentRect(GetCursorRow(), GetCursorColumn());
1986 int wxGenericGrid::GetLabelAlignment(int orientation
) const
1988 if (orientation
== wxHORIZONTAL
)
1989 return m_horizontalLabelAlignment
;
1991 return m_verticalLabelAlignment
;
1994 void wxGenericGrid::SetLabelTextColour(const wxColour
& colour
)
1996 m_labelTextColour
= colour
;
2000 void wxGenericGrid::SetLabelBackgroundColour(const wxColour
& colour
)
2002 m_labelBackgroundColour
= colour
;
2003 m_labelBackgroundBrush
= * wxTheBrushList
->FindOrCreateBrush(m_labelBackgroundColour
, wxSOLID
);
2006 void wxGenericGrid::SetEditable(bool edit
)
2011 int controlW
, controlH
;
2012 m_textItem
->GetSize(&controlW
, &controlH
);
2013 m_editControlPosition
.height
= controlH
;
2015 m_topOfSheet
= m_editControlPosition
.x
+ controlH
+ 2;
2018 m_editingPanel
->Show(TRUE
);
2019 m_textItem
->Show(TRUE
);
2020 m_textItem
->SetFocus();
2023 if (m_inPlaceTextItem
)
2025 m_inPlaceTextItem
->Show(TRUE
);
2026 m_inPlaceTextItem
->SetFocus();
2034 m_textItem
->Show(FALSE
);
2035 m_editingPanel
->Show(FALSE
);
2038 if ( m_inPlaceTextItem
)
2040 m_inPlaceTextItem
->Show(FALSE
);
2044 SetCurrentRect(GetCursorRow(), GetCursorColumn());
2047 GetClientSize(&cw
, &ch
);
2052 int m_scrollWidth = 16;
2053 GetClientSize(&cw, &ch);
2056 m_vScrollBar->SetSize(cw - m_scrollWidth, m_topOfSheet,
2057 m_scrollWidth, ch - m_topOfSheet - m_scrollWidth);
2062 void wxGenericGrid::SetEditInPlace(bool edit
)
2064 if ( m_editInPlace
!= edit
)
2066 m_editInPlace
= edit
;
2068 if ( m_editInPlace
) // switched on
2070 if ( m_currentRectVisible
&& m_editable
)
2072 m_inPlaceTextItem
->SetSize( m_currentRect
.x
-2, m_currentRect
.y
-2,
2073 m_currentRect
.width
+4, m_currentRect
.height
+4 );
2075 wxGridCell
*cell
= GetCell(m_wCursorRow
, m_wCursorColumn
);
2079 if ( cell
->GetTextValue().IsNull() )
2081 m_inPlaceTextItem
->SetValue( "" );
2085 m_inPlaceTextItem
->SetFont( cell
->GetFont() );
2086 m_inPlaceTextItem
->SetValue( cell
->GetTextValue() );
2090 m_inPlaceTextItem
->Show( TRUE
);
2091 m_inPlaceTextItem
->SetFocus();
2094 else // switched off
2096 m_inPlaceTextItem
->Show( FALSE
);
2102 void wxGenericGrid::SetCellAlignment(int flag
, int row
, int col
)
2104 wxGridCell
*cell
= GetCell(row
, col
);
2106 cell
->SetAlignment(flag
);
2109 int wxGenericGrid::GetCellAlignment(int row
, int col
) const
2111 wxGridCell
*cell
= GetCell(row
, col
);
2113 return cell
->GetAlignment();
2115 return m_cellAlignment
;
2118 void wxGenericGrid::SetCellAlignment(int flag
)
2120 m_cellAlignment
= flag
;
2122 for (i
= 0; i
< GetRows(); i
++)
2123 for (j
= 0; j
< GetCols(); j
++)
2125 GetCell(i
, j
)->SetAlignment(flag
);
2128 int wxGenericGrid::GetCellAlignment(void) const
2130 return m_cellAlignment
;
2133 void wxGenericGrid::SetCellBackgroundColour(const wxColour
& col
)
2135 m_cellBackgroundColour
= col
;
2137 for (i
= 0; i
< GetRows(); i
++)
2138 for (j
= 0; j
< GetCols(); j
++)
2140 GetCell(i
, j
)->SetBackgroundColour(col
);
2143 void wxGenericGrid::SetCellBackgroundColour(const wxColour
& val
, int row
, int col
)
2145 wxGridCell
*cell
= GetCell(row
, col
);
2148 cell
->SetBackgroundColour(val
);
2149 RefreshCell(row
, col
);
2153 wxColour
& wxGenericGrid::GetCellBackgroundColour(int row
, int col
) const
2155 wxGridCell
*cell
= GetCell(row
, col
);
2157 return cell
->GetBackgroundColour();
2159 return (wxColour
&) m_cellBackgroundColour
;
2162 void wxGenericGrid::SetCellTextColour(const wxColour
& val
, int row
, int col
)
2164 wxGridCell
*cell
= GetCell(row
, col
);
2167 cell
->SetTextColour(val
);
2168 RefreshCell(row
, col
);
2172 void wxGenericGrid::SetCellTextFont(const wxFont
& fnt
, int row
, int col
)
2174 wxGridCell
*cell
= GetCell(row
, col
);
2178 RefreshCell(row
, col
);
2182 wxFont
& wxGenericGrid::GetCellTextFont(int row
, int col
) const
2184 wxGridCell
*cell
= GetCell(row
, col
);
2186 return (wxFont
&) cell
->GetFont();
2188 return (wxFont
&) m_cellTextFont
;
2191 wxColour
& wxGenericGrid::GetCellTextColour(int row
, int col
) const
2193 wxGridCell
*cell
= GetCell(row
, col
);
2195 return (wxColour
&) cell
->GetTextColour();
2197 return (wxColour
&) m_cellTextColour
;
2200 void wxGenericGrid::SetCellTextColour(const wxColour
& val
)
2202 m_cellTextColour
= val
;
2204 for (i
= 0; i
< GetRows(); i
++)
2205 for (j
= 0; j
< GetCols(); j
++)
2207 GetCell(i
, j
)->SetTextColour(val
);
2210 void wxGenericGrid::SetCellTextFont(const wxFont
& fnt
)
2212 m_cellTextFont
= fnt
;
2214 for (i
= 0; i
< GetRows(); i
++)
2215 for (j
= 0; j
< GetCols(); j
++)
2217 GetCell(i
, j
)->SetFont(fnt
);
2220 void wxGenericGrid::SetCellBitmap(wxBitmap
*bitmap
, int row
, int col
)
2222 wxGridCell
*cell
= GetCell(row
, col
);
2225 cell
->SetCellBitmap(bitmap
);
2226 RefreshCell(row
, col
);
2230 wxBitmap
*wxGenericGrid::GetCellBitmap(int row
, int col
) const
2232 wxGridCell
*cell
= GetCell(row
, col
);
2235 return cell
->GetCellBitmap();
2238 return (wxBitmap
*) NULL
;
2241 bool wxGenericGrid::InsertCols(int pos
, int n
, bool updateLabels
)
2243 if (pos
> m_totalCols
)
2247 return CreateGrid(1, n
);
2252 for (i
= 0; i
< m_totalRows
; i
++)
2254 wxGridCell
**cols
= m_gridCells
[i
];
2255 wxGridCell
**newCols
= new wxGridCell
*[m_totalCols
+ n
];
2256 for (j
= 0; j
< pos
; j
++)
2257 newCols
[j
] = cols
[j
];
2258 for (j
= pos
; j
< pos
+ n
; j
++)
2259 newCols
[j
] = new wxGridCell(this);
2260 for (j
= pos
+ n
; j
< m_totalCols
+ n
; j
++)
2261 newCols
[j
] = cols
[j
- n
];
2264 m_gridCells
[i
] = newCols
;
2268 short *newColWidths
= new short[m_totalCols
+ n
];
2269 for (j
= 0; j
< pos
; j
++)
2270 newColWidths
[j
] = m_colWidths
[j
];
2271 for (j
= pos
; j
< pos
+ n
; j
++)
2272 newColWidths
[j
] = wxGRID_DEFAULT_CELL_WIDTH
;
2273 for (j
= pos
+ n
; j
< m_totalCols
+ n
; j
++)
2274 newColWidths
[j
] = m_colWidths
[j
- n
];
2275 delete[] m_colWidths
;
2276 m_colWidths
= newColWidths
;
2279 wxGridCell
**newLabels
= new wxGridCell
*[m_totalCols
+ n
];
2280 for (j
= 0; j
< pos
; j
++)
2281 newLabels
[j
] = m_colLabelCells
[j
];
2282 for (j
= pos
; j
< pos
+ n
; j
++)
2283 newLabels
[j
] = new wxGridCell(this);
2284 for (j
= pos
+ n
; j
< m_totalCols
+ n
; j
++)
2285 newLabels
[j
] = m_colLabelCells
[j
- n
];
2287 delete[] m_colLabelCells
;
2288 m_colLabelCells
= newLabels
;
2294 wxGridEvent
g_evt(GetId(), wxEVT_GRID_CHANGE_LABELS
, this);
2295 GetEventHandler()->ProcessEvent(g_evt
);
2304 bool wxGenericGrid::InsertRows(int pos
, int n
, bool updateLabels
)
2306 if (pos
> m_totalRows
)
2310 return CreateGrid(n
, 1);
2315 wxGridCell
***rows
= new wxGridCell
**[m_totalRows
+ n
];
2318 for (i
= 0; i
< pos
; i
++)
2319 rows
[i
] = m_gridCells
[i
];
2321 for (i
= pos
; i
< pos
+ n
; i
++)
2323 rows
[i
] = new wxGridCell
*[m_totalCols
];
2324 for (j
= 0; j
< m_totalCols
; j
++)
2325 rows
[i
][j
] = new wxGridCell(this);
2328 for (i
= pos
+ n
; i
< m_totalRows
+ n
; i
++)
2329 rows
[i
] = m_gridCells
[i
- n
];
2331 delete[] m_gridCells
;
2335 short *newRowHeights
= new short[m_totalRows
+ n
];
2336 for (i
= 0; i
< pos
; i
++)
2337 newRowHeights
[i
] = m_rowHeights
[i
];
2338 for (i
= pos
; i
< pos
+ n
; i
++)
2339 newRowHeights
[i
] = wxGRID_DEFAULT_CELL_HEIGHT
;
2340 for (i
= pos
+ n
; i
< m_totalRows
+ n
; i
++)
2341 newRowHeights
[i
] = m_rowHeights
[i
- n
];
2342 delete[] m_rowHeights
;
2343 m_rowHeights
= newRowHeights
;
2346 wxGridCell
**newLabels
= new wxGridCell
*[m_totalRows
+ n
];
2347 for (i
= 0; i
< pos
; i
++)
2348 newLabels
[i
] = m_rowLabelCells
[i
];
2349 for (i
= pos
; i
< pos
+ n
; i
++)
2350 newLabels
[i
] = new wxGridCell(this);
2351 for (i
= pos
+ n
; i
< m_totalRows
+ n
; i
++)
2352 newLabels
[i
] = m_rowLabelCells
[i
- n
];
2354 delete[] m_rowLabelCells
;
2355 m_rowLabelCells
= newLabels
;
2361 wxGridEvent
g_evt(GetId(), wxEVT_GRID_CHANGE_LABELS
, this);
2362 GetEventHandler()->ProcessEvent(g_evt
);
2371 bool wxGenericGrid::AppendCols(int n
, bool updateLabels
)
2373 return InsertCols(GetCols(), n
, updateLabels
);
2376 bool wxGenericGrid::AppendRows(int n
, bool updateLabels
)
2378 return InsertRows(GetRows(), n
, updateLabels
);
2381 bool wxGenericGrid::DeleteRows(int pos
, int n
, bool updateLabels
)
2383 if (pos
> m_totalRows
)
2390 wxGridCell
***rows
= new wxGridCell
**[m_totalRows
- n
];
2393 for (i
= 0; i
< pos
; i
++)
2394 rows
[i
] = m_gridCells
[i
];
2396 for (i
= pos
+ n
; i
< m_totalRows
; i
++)
2397 rows
[i
-n
] = m_gridCells
[i
];
2399 delete[] m_gridCells
;
2403 short *newRowHeights
= new short[m_totalRows
- n
];
2404 for (i
= 0; i
< pos
; i
++)
2405 newRowHeights
[i
] = m_rowHeights
[i
];
2406 for (i
= pos
+ n
; i
< m_totalRows
; i
++)
2407 newRowHeights
[i
-n
] = m_rowHeights
[i
];
2408 delete[] m_rowHeights
;
2409 m_rowHeights
= newRowHeights
;
2412 wxGridCell
**newLabels
= new wxGridCell
*[m_totalRows
- n
];
2413 for (i
= 0; i
< pos
; i
++)
2414 newLabels
[i
] = m_rowLabelCells
[i
];
2415 for (i
= pos
+ n
; i
< m_totalRows
; i
++)
2416 newLabels
[i
-n
] = m_rowLabelCells
[i
];
2418 delete[] m_rowLabelCells
;
2419 m_rowLabelCells
= newLabels
;
2425 wxGridEvent
g_evt(GetId(), wxEVT_GRID_CHANGE_LABELS
, this);
2426 GetEventHandler()->ProcessEvent(g_evt
);
2433 bool wxGenericGrid::DeleteCols(int pos
, int n
, bool updateLabels
)
2435 if (pos
+ n
> m_totalCols
)
2443 for (i
= 0; i
< m_totalRows
; i
++)
2445 wxGridCell
**cols
= m_gridCells
[i
];
2446 wxGridCell
**newCols
= new wxGridCell
*[m_totalCols
- n
];
2447 for (j
= 0; j
< pos
; j
++)
2448 newCols
[j
] = cols
[j
];
2449 for (j
= pos
; j
< pos
+ n
; j
++)
2451 for (j
= pos
+ n
; j
< m_totalCols
; j
++)
2452 newCols
[j
-n
] = cols
[j
];
2455 m_gridCells
[i
] = newCols
;
2459 short *newColWidths
= new short[m_totalCols
- n
];
2460 for (j
= 0; j
< pos
; j
++)
2461 newColWidths
[j
] = m_colWidths
[j
];
2462 for (j
= pos
+ n
; j
< m_totalCols
; j
++)
2463 newColWidths
[j
-n
] = m_colWidths
[j
];
2464 delete[] m_colWidths
;
2465 m_colWidths
= newColWidths
;
2468 wxGridCell
**newLabels
= new wxGridCell
*[m_totalCols
- n
];
2469 for (j
= 0; j
< pos
; j
++)
2470 newLabels
[j
] = m_colLabelCells
[j
];
2471 for (j
= pos
+ n
; j
< m_totalCols
; j
++)
2472 newLabels
[j
-n
] = m_colLabelCells
[j
];
2474 delete[] m_colLabelCells
;
2475 m_colLabelCells
= newLabels
;
2481 wxGridEvent
g_evt(GetId(), wxEVT_GRID_CHANGE_LABELS
, this);
2482 GetEventHandler()->ProcessEvent(g_evt
);
2489 void wxGenericGrid::SetGridCursor(int row
, int col
)
2491 if (row
>= m_totalRows
|| col
>= m_totalCols
)
2494 if (row
== GetCursorRow() && col
== GetCursorColumn())
2497 wxClientDC
dc(this);
2500 SetGridClippingRegion(& dc
);
2502 if (m_currentRectVisible
&& !(m_editable
&& m_editInPlace
) )
2503 HighlightCell(& dc
);
2506 m_wCursorColumn
= col
;
2509 GetClientSize(&cw
, &ch
);
2511 SetCurrentRect(row
, col
, cw
, ch
);
2513 if (m_currentRectVisible
&& !(m_editable
&& m_editInPlace
) )
2514 HighlightCell(& dc
);
2516 dc
.DestroyClippingRegion();
2520 // ----------------------------------------------------------------------------
2522 // ----------------------------------------------------------------------------
2524 wxGridCell::wxGridCell(wxGenericGrid
*window
)
2526 cellBitmap
= (wxBitmap
*) NULL
;
2528 backgroundBrush
= wxNullBrush
;
2530 textColour
= window
->GetCellTextColour();
2532 textColour
.Set(0,0,0);
2534 backgroundColour
= window
->GetCellBackgroundColour();
2536 backgroundColour
.Set(255,255,255);
2539 font
= window
->GetCellTextFont();
2541 font
= * wxTheFontList
->FindOrCreateFont(12, wxSWISS
, wxNORMAL
, wxNORMAL
);
2543 SetBackgroundColour(backgroundColour
);
2546 alignment
= window
->GetCellAlignment();
2550 cellData
= (void *)NULL
;
2553 wxGridCell::~wxGridCell()
2557 void wxGridCell::SetBackgroundColour(const wxColour
& colour
)
2559 backgroundColour
= colour
;
2560 backgroundBrush
= * wxTheBrushList
->FindOrCreateBrush(backgroundColour
, wxSOLID
);
2563 void wxGenericGrid::OnText(wxCommandEvent
& WXUNUSED(ev
) )
2565 // michael - added this conditional to prevent change to
2566 // grid cell text when edit control is hidden but still has
2571 wxGenericGrid
*grid
= this;
2572 wxGridCell
*cell
= grid
->GetCell(grid
->GetCursorRow(), grid
->GetCursorColumn());
2573 if (cell
&& grid
->CurrentCellVisible())
2575 cell
->SetTextValue(grid
->GetTextItem()->GetValue());
2576 if ( m_editInPlace
&& !m_inOnTextInPlace
)
2578 m_inPlaceTextItem
->SetValue( grid
->GetTextItem()->GetValue() );
2581 wxClientDC
dc(grid
);
2584 grid
->SetGridClippingRegion(& dc
);
2585 grid
->DrawCellBackground(& dc
, &grid
->GetCurrentRect(), grid
->GetCursorRow(), grid
->GetCursorColumn());
2586 grid
->DrawCellValue(& dc
, &grid
->GetCurrentRect(), grid
->GetCursorRow(), grid
->GetCursorColumn());
2587 if ( !(m_editable
&& m_editInPlace
) ) grid
->HighlightCell(& dc
);
2588 dc
.DestroyClippingRegion();
2591 //grid->OnCellChange(grid->GetCursorRow(), grid->GetCursorColumn());
2592 wxGridEvent
g_evt(GetId(), wxEVT_GRID_CELL_CHANGE
, grid
,
2593 grid
->GetCursorRow(), grid
->GetCursorColumn());
2594 GetEventHandler()->ProcessEvent(g_evt
);
2596 // grid->DrawCellText();
2601 void wxGenericGrid::OnTextEnter(wxCommandEvent
& WXUNUSED(ev
) )
2603 // move the cursor down the current row (if possible)
2604 // when the enter key has been pressed
2608 if ( GetCursorRow() < GetRows()-1 )
2610 wxClientDC
dc( this );
2612 OnSelectCellImplementation(& dc
,
2614 GetCursorColumn() );
2620 void wxGenericGrid::OnTextInPlace(wxCommandEvent
& ev
)
2624 wxGenericGrid
*grid
= this;
2625 wxGridCell
*cell
= grid
->GetCell(grid
->GetCursorRow(), grid
->GetCursorColumn());
2626 if (cell
&& grid
->CurrentCellVisible())
2628 m_inOnTextInPlace
= TRUE
;
2629 grid
->GetTextItem()->SetValue( m_inPlaceTextItem
->GetValue() );
2631 m_inOnTextInPlace
= FALSE
;
2636 void wxGenericGrid::OnTextInPlaceEnter(wxCommandEvent
& WXUNUSED(ev
) )
2638 // move the cursor down the current row (if possible)
2639 // when the enter key has been pressed
2643 if ( GetCursorRow() < GetRows()-1 )
2645 wxClientDC
dc( this );
2647 OnSelectCellImplementation(& dc
,
2649 GetCursorColumn() );
2655 void wxGenericGrid::OnGridScroll(wxScrollEvent
& ev
)
2657 static bool inScroll
= FALSE
;
2662 if ( m_editInPlace
) m_inPlaceTextItem
->Show(FALSE
);
2665 wxGenericGrid
*win
= this;
2667 bool change
= FALSE
;
2669 if (ev
.GetEventObject() == win
->GetHorizScrollBar())
2671 change
= (ev
.GetPosition() != m_scrollPosX
);
2672 win
->SetScrollPosX(ev
.GetPosition());
2676 change
= (ev
.GetPosition() != m_scrollPosY
);
2677 win
->SetScrollPosY(ev
.GetPosition());
2680 win
->UpdateDimensions();
2682 win
->SetCurrentRect(win
->GetCursorRow(), win
->GetCursorColumn());
2684 // Because rows and columns can be arbitrary sizes,
2685 // the scrollbars will need to be adjusted to reflect the
2689 if (change
) win
->Refresh(FALSE
);
2691 if ( m_editInPlace
&& m_currentRectVisible
)
2693 m_inPlaceTextItem
->SetSize( m_currentRect
.x
-2, m_currentRect
.y
-2,
2694 m_currentRect
.width
+4, m_currentRect
.height
+4 );
2695 m_inPlaceTextItem
->Show( TRUE
);
2696 m_inPlaceTextItem
->SetFocus();
2704 //----------------------------------------------------------------------
2705 // Default wxGridEvent handlers
2706 // (just redirect to the pre-existing virtual methods)
2708 void wxGenericGrid::_OnSelectCell(wxGridEvent
& ev
)
2710 OnSelectCell(ev
.m_row
, ev
.m_col
);
2713 void wxGenericGrid::_OnCreateCell(wxGridEvent
& ev
)
2715 ev
.m_cell
= OnCreateCell();
2718 void wxGenericGrid::_OnChangeLabels(wxGridEvent
& WXUNUSED(ev
))
2723 void wxGenericGrid::_OnChangeSelectionLabel(wxGridEvent
& WXUNUSED(ev
))
2725 OnChangeSelectionLabel();
2728 void wxGenericGrid::_OnCellChange(wxGridEvent
& ev
)
2730 OnCellChange(ev
.m_row
, ev
.m_col
);
2733 void wxGenericGrid::_OnCellLeftClick(wxGridEvent
& ev
)
2735 OnCellLeftClick(ev
.m_row
, ev
.m_col
, ev
.m_x
, ev
.m_y
, ev
.m_control
, ev
.m_shift
);
2738 void wxGenericGrid::_OnCellRightClick(wxGridEvent
& ev
)
2740 OnCellRightClick(ev
.m_row
, ev
.m_col
, ev
.m_x
, ev
.m_y
, ev
.m_control
, ev
.m_shift
);
2743 void wxGenericGrid::_OnLabelLeftClick(wxGridEvent
& ev
)
2745 OnLabelLeftClick(ev
.m_row
, ev
.m_col
, ev
.m_x
, ev
.m_y
, ev
.m_control
, ev
.m_shift
);
2748 void wxGenericGrid::_OnLabelRightClick(wxGridEvent
& ev
)
2750 OnLabelRightClick(ev
.m_row
, ev
.m_col
, ev
.m_x
, ev
.m_y
, ev
.m_control
, ev
.m_shift
);
2753 void *wxGenericGrid::SetCellData(void *data
, int row
, int col
)
2757 wxGridCell
*cell
= GetCell(row
, col
);
2759 rc
= cell
->SetCellData(data
);
2764 void *wxGenericGrid::GetCellData(int row
, int col
)
2768 wxGridCell
*cell
= GetCell(row
, col
);
2770 rc
= cell
->GetCellData();