1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxGenericGrid
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "gridg.h"
17 // For compilers that support precompilation, includes "wx/wx.h".
18 #include "wx/wxprec.h"
30 #include "wx/string.h"
31 #include "wx/generic/gridg.h"
32 #include "wx/settings.h"
34 // Set to zero to use no double-buffering
36 #define wxUSE_DOUBLE_BUFFERING 1
38 #define wxUSE_DOUBLE_BUFFERING 0
41 #define wxGRID_DRAG_NONE 0
42 #define wxGRID_DRAG_LEFT_RIGHT 1
43 #define wxGRID_DRAG_UP_DOWN 2
45 IMPLEMENT_DYNAMIC_CLASS(wxGenericGrid
, wxPanel
)
46 IMPLEMENT_DYNAMIC_CLASS(wxGridEvent
, wxEvent
)
48 BEGIN_EVENT_TABLE(wxGenericGrid
, wxPanel
)
49 EVT_SIZE(wxGenericGrid::OnSize
)
50 EVT_PAINT(wxGenericGrid::OnPaint
)
51 EVT_ERASE_BACKGROUND(wxGenericGrid::OnEraseBackground
)
52 EVT_MOUSE_EVENTS(wxGenericGrid::OnMouseEvent
)
53 EVT_TEXT(wxGRID_TEXT_CTRL
, wxGenericGrid::OnText
)
54 EVT_COMMAND_SCROLL(wxGRID_HSCROLL
, wxGenericGrid::OnGridScroll
)
55 EVT_COMMAND_SCROLL(wxGRID_VSCROLL
, wxGenericGrid::OnGridScroll
)
57 // default wxGridEvent handlers
58 EVT_GRID_SELECT_CELL(wxGenericGrid::_OnSelectCell
)
59 EVT_GRID_CREATE_CELL(wxGenericGrid::_OnCreateCell
)
60 EVT_GRID_CHANGE_LABELS(wxGenericGrid::_OnChangeLabels
)
61 EVT_GRID_CHANGE_SEL_LABEL(wxGenericGrid::_OnChangeSelectionLabel
)
62 EVT_GRID_CELL_CHANGE(wxGenericGrid::_OnCellChange
)
63 EVT_GRID_CELL_LCLICK(wxGenericGrid::_OnCellLeftClick
)
64 EVT_GRID_CELL_RCLICK(wxGenericGrid::_OnCellRightClick
)
65 EVT_GRID_LABEL_LCLICK(wxGenericGrid::_OnLabelLeftClick
)
66 EVT_GRID_LABEL_RCLICK(wxGenericGrid::_OnLabelRightClick
)
73 wxGenericGrid::wxGenericGrid(void)
76 m_hScrollBar
= (wxScrollBar
*) NULL
;
77 m_vScrollBar
= (wxScrollBar
*) NULL
;
78 m_cellTextColour
= *wxBLACK
;
79 m_cellBackgroundColour
= *wxWHITE
;
80 m_labelTextColour
= *wxBLACK
;
81 // m_labelBackgroundColour = *wxLIGHT_GREY;
82 m_labelBackgroundColour
= wxSystemSettings::GetSystemColour( wxSYS_COLOUR_BTNFACE
);
83 m_labelBackgroundBrush
= wxNullBrush
;
84 m_labelTextFont
= wxNullFont
;
85 m_cellTextFont
= wxNullFont
;
86 m_textItem
= (wxTextCtrl
*) NULL
;
87 m_currentRectVisible
= FALSE
;
89 #if defined(__WIN95__)
90 m_scrollWidth
= wxSystemSettings::GetSystemMetric(wxSYS_VSCROLL_X
);
91 #elif defined(__WXGTK__)
92 m_scrollWidth
= wxSystemSettings::GetSystemMetric(wxSYS_VSCROLL_X
);
96 m_dragStatus
= wxGRID_DRAG_NONE
;
98 m_dragStartPosition
= 0;
99 m_dragLastPosition
= 0;
100 m_divisionPen
= wxNullPen
;
101 m_leftOfSheet
= wxGRID_DEFAULT_SHEET_LEFT
;
102 m_topOfSheet
= wxGRID_DEFAULT_SHEET_TOP
;
103 m_cellHeight
= wxGRID_DEFAULT_CELL_HEIGHT
;
104 m_totalGridWidth
= 0;
105 m_totalGridHeight
= 0;
106 m_colWidths
= (short *) NULL
;
107 m_rowHeights
= (short *) NULL
;
108 m_verticalLabelWidth
= wxGRID_DEFAULT_VERTICAL_LABEL_WIDTH
;
109 m_horizontalLabelHeight
= wxGRID_DEFAULT_HORIZONAL_LABEL_HEIGHT
;
110 m_verticalLabelAlignment
= wxCENTRE
;
111 m_horizontalLabelAlignment
= wxCENTRE
;
112 m_editControlPosition
.x
= wxGRID_DEFAULT_EDIT_X
;
113 m_editControlPosition
.y
= wxGRID_DEFAULT_EDIT_Y
;
114 m_editControlPosition
.width
= wxGRID_DEFAULT_EDIT_WIDTH
;
115 m_editControlPosition
.height
= wxGRID_DEFAULT_EDIT_HEIGHT
;
120 m_editCreated
= FALSE
;
123 m_gridCells
= (wxGridCell
***) NULL
;
124 m_rowLabelCells
= (wxGridCell
**) NULL
;
125 m_colLabelCells
= (wxGridCell
**) NULL
;
126 m_textItem
= (wxTextCtrl
*) NULL
;
129 bool wxGenericGrid::Create(wxWindow
*parent
, wxWindowID id
, const wxPoint
& pos
, const wxSize
& size
,
130 long style
, const wxString
& name
)
133 m_editingPanel
= (wxPanel
*) NULL
;
134 m_hScrollBar
= (wxScrollBar
*) NULL
;
135 m_vScrollBar
= (wxScrollBar
*) NULL
;
136 m_cellTextColour
= *wxBLACK
;
137 m_cellBackgroundColour
= *wxWHITE
;
138 m_labelTextColour
= *wxBLACK
;
139 // m_labelBackgroundColour = *wxLIGHT_GREY;
140 m_labelBackgroundColour
= wxSystemSettings::GetSystemColour( wxSYS_COLOUR_BTNFACE
);
141 m_labelBackgroundBrush
= wxNullBrush
;
142 m_labelTextFont
= * wxTheFontList
->FindOrCreateFont(10, wxSWISS
, wxNORMAL
, wxBOLD
);
143 m_cellTextFont
= * wxTheFontList
->FindOrCreateFont(10, wxSWISS
, wxNORMAL
, wxNORMAL
);
144 m_textItem
= (wxTextCtrl
*) NULL
;
145 m_currentRectVisible
= FALSE
;
147 #if defined(__WIN95__)
148 m_scrollWidth
= wxSystemSettings::GetSystemMetric(wxSYS_VSCROLL_X
);
149 #elif defined(__WXGTK__)
150 m_scrollWidth
= wxSystemSettings::GetSystemMetric(wxSYS_VSCROLL_X
);
154 m_dragStatus
= wxGRID_DRAG_NONE
;
156 m_dragStartPosition
= 0;
157 m_dragLastPosition
= 0;
158 m_divisionPen
= * wxThePenList
->FindOrCreatePen("LIGHT GREY", 1, wxSOLID
);
159 m_doubleBufferingBitmap
= (wxBitmap
*) NULL
;
161 if (!m_horizontalSashCursor
.Ok())
163 m_horizontalSashCursor
= wxCursor(wxCURSOR_SIZEWE
);
164 m_verticalSashCursor
= wxCursor(wxCURSOR_SIZENS
);
167 SetLabelBackgroundColour(m_labelBackgroundColour
);
169 m_leftOfSheet
= wxGRID_DEFAULT_SHEET_LEFT
;
170 m_topOfSheet
= wxGRID_DEFAULT_SHEET_TOP
;
171 m_cellHeight
= wxGRID_DEFAULT_CELL_HEIGHT
;
172 m_totalGridWidth
= 0;
173 m_totalGridHeight
= 0;
174 m_colWidths
= (short *) NULL
;
175 m_rowHeights
= (short *) NULL
;
177 m_verticalLabelWidth
= wxGRID_DEFAULT_VERTICAL_LABEL_WIDTH
;
178 m_horizontalLabelHeight
= wxGRID_DEFAULT_HORIZONAL_LABEL_HEIGHT
;
179 m_verticalLabelAlignment
= wxCENTRE
;
180 m_horizontalLabelAlignment
= wxCENTRE
;
181 m_editControlPosition
.x
= wxGRID_DEFAULT_EDIT_X
;
182 m_editControlPosition
.y
= wxGRID_DEFAULT_EDIT_Y
;
183 m_editControlPosition
.width
= wxGRID_DEFAULT_EDIT_WIDTH
;
184 m_editControlPosition
.height
= wxGRID_DEFAULT_EDIT_HEIGHT
;
192 /* Store the rect. coordinates for the current cell */
193 SetCurrentRect(m_wCursorRow
, m_wCursorColumn
);
195 m_editCreated
= FALSE
;
199 m_gridCells
= (wxGridCell
***) NULL
;
200 m_rowLabelCells
= (wxGridCell
**) NULL
;
201 m_colLabelCells
= (wxGridCell
**) NULL
;
202 m_textItem
= (wxTextCtrl
*) NULL
;
204 wxPanel::Create(parent
, id
, pos
, size
, style
, name
);
206 m_editingPanel
= new wxPanel(this);
208 m_textItem
= new wxTextCtrl(m_editingPanel
, wxGRID_TEXT_CTRL
, "",
209 wxPoint(m_editControlPosition
.x
, m_editControlPosition
.y
), wxSize(m_editControlPosition
.width
, -1),
211 m_textItem
->Show(TRUE
);
212 m_textItem
->SetFocus();
213 int controlW
, controlH
;
215 m_textItem
->GetSize(&controlW
, &controlH
);
216 m_editControlPosition
.height
= controlH
;
218 m_topOfSheet
= m_editControlPosition
.y
+ controlH
+ 2;
220 m_editCreated
= TRUE
;
222 m_hScrollBar
= new wxScrollBar(this, wxGRID_HSCROLL
, wxPoint(0, 0), wxSize(20, 100), wxHORIZONTAL
);
223 m_vScrollBar
= new wxScrollBar(this, wxGRID_VSCROLL
, wxPoint(0, 0), wxSize(100, 20), wxVERTICAL
);
228 wxGenericGrid::~wxGenericGrid(void)
233 void wxGenericGrid::ClearGrid(void)
238 for (i
= 0; i
< m_totalRows
; i
++)
240 for (j
= 0; j
< m_totalCols
; j
++)
241 if (m_gridCells
[i
][j
])
242 delete m_gridCells
[i
][j
];
243 delete[] m_gridCells
[i
];
245 delete[] m_gridCells
;
246 m_gridCells
= (wxGridCell
***) NULL
;
249 delete[] m_colWidths
;
250 m_colWidths
= (short *) NULL
;
252 delete[] m_rowHeights
;
253 m_rowHeights
= (short *) NULL
;
257 for (i
= 0; i
< m_totalRows
; i
++)
258 delete m_rowLabelCells
[i
];
259 delete[] m_rowLabelCells
;
260 m_rowLabelCells
= (wxGridCell
**) NULL
;
264 for (i
= 0; i
< m_totalCols
; i
++)
265 delete m_colLabelCells
[i
];
266 delete[] m_colLabelCells
;
267 m_colLabelCells
= (wxGridCell
**) NULL
;
269 if (m_doubleBufferingBitmap
)
271 delete m_doubleBufferingBitmap
;
272 m_doubleBufferingBitmap
= (wxBitmap
*) NULL
;
276 bool wxGenericGrid::CreateGrid(int nRows
, int nCols
, wxString
**cellValues
, short *widths
,
277 short defaultWidth
, short defaultHeight
)
283 m_colWidths
= new short[nCols
];
284 m_rowHeights
= new short[nRows
];
285 for (i
= 0; i
< nCols
; i
++)
287 m_colWidths
[i
] = widths
[i
];
289 m_colWidths
[i
] = defaultWidth
;
290 for (i
= 0; i
< nRows
; i
++)
291 m_rowHeights
[i
] = defaultHeight
;
293 m_gridCells
= new wxGridCell
**[nRows
];
295 for (i
= 0; i
< nRows
; i
++)
296 m_gridCells
[i
] = new wxGridCell
*[nCols
];
298 for (i
= 0; i
< nRows
; i
++)
299 for (j
= 0; j
< nCols
; j
++)
302 //m_gridCells[i][j] = OnCreateCell();
303 wxGridEvent
g_evt(GetId(), wxEVT_GRID_CREATE_CELL
, this, i
, j
);
304 GetEventHandler()->ProcessEvent(g_evt
);
305 m_gridCells
[i
][j
] = g_evt
.m_cell
;
306 m_gridCells
[i
][j
]->SetTextValue(cellValues
[i
][j
]);
309 m_gridCells
[i
][j
] = (wxGridCell
*) NULL
;
311 m_rowLabelCells
= new wxGridCell
*[nRows
];
312 for (i
= 0; i
< nRows
; i
++)
313 m_rowLabelCells
[i
] = new wxGridCell(this);
314 m_colLabelCells
= new wxGridCell
*[nCols
];
315 for (i
= 0; i
< nCols
; i
++)
316 m_colLabelCells
[i
] = new wxGridCell(this);
318 m_wCursorRow
= m_wCursorColumn
= 0;
319 SetCurrentRect(0, 0);
321 // Need to determine various dimensions
325 int objectSizeX
= m_totalCols
;
327 int viewLengthX
= m_totalCols
;
330 m_hScrollBar->SetViewLength(viewLengthX);
331 m_hScrollBar->SetObjectLength(objectSizeX);
332 m_hScrollBar->SetPageSize(pageSizeX);
334 m_hScrollBar
->SetScrollbar(m_hScrollBar
->GetThumbPosition(), pageSizeX
, objectSizeX
, viewLengthX
);
336 int objectSizeY
= m_totalRows
;
338 int viewLengthY
= m_totalRows
;
341 m_vScrollBar->SetViewLength(viewLengthY);
342 m_vScrollBar->SetObjectLength(objectSizeY);
343 m_vScrollBar->SetPageSize(pageSizeY);
346 m_vScrollBar
->SetScrollbar(m_vScrollBar
->GetThumbPosition(), pageSizeY
, objectSizeY
, viewLengthY
);
351 wxGridEvent
g_evt(GetId(), wxEVT_GRID_CHANGE_LABELS
, this);
352 GetEventHandler()->ProcessEvent(g_evt
);
354 //OnChangeSelectionLabel();
355 wxGridEvent
g_evt2(GetId(), wxEVT_GRID_CHANGE_SEL_LABEL
, this);
356 GetEventHandler()->ProcessEvent(g_evt2
);
361 // Need to determine various dimensions
362 void wxGenericGrid::UpdateDimensions(void)
364 int canvasWidth
, canvasHeight
;
365 GetSize(&canvasWidth
, &canvasHeight
);
367 if (m_editCreated
&& m_editable
)
369 int controlW
, controlH
;
370 GetTextItem()->GetSize(&controlW
, &controlH
);
371 m_topOfSheet
= m_editControlPosition
.y
+ controlH
+ 2;
375 m_rightOfSheet
= m_leftOfSheet
+ m_verticalLabelWidth
;
377 for (i
= m_scrollPosX
; i
< m_totalCols
; i
++)
379 if (m_rightOfSheet
> canvasWidth
)
382 m_rightOfSheet
+= m_colWidths
[i
];
384 m_bottomOfSheet
= m_topOfSheet
+ m_horizontalLabelHeight
;
385 for (i
= m_scrollPosY
; i
< m_totalRows
; i
++)
387 if (m_bottomOfSheet
> canvasHeight
)
390 m_bottomOfSheet
+= m_rowHeights
[i
];
393 m_totalGridWidth
= m_leftOfSheet
+ m_verticalLabelWidth
;
394 for (i
= 0; i
< m_totalCols
; i
++)
396 m_totalGridWidth
+= m_colWidths
[i
];
398 m_totalGridHeight
= m_topOfSheet
+ m_horizontalLabelHeight
;
399 for (i
= 0; i
< m_totalRows
; i
++)
401 m_totalGridHeight
+= m_rowHeights
[i
];
405 wxGridCell
*wxGenericGrid::GetCell(int row
, int col
) const
408 return (wxGridCell
*) NULL
;
410 if ((row
>= m_totalRows
) || (col
>= m_totalCols
))
411 return (wxGridCell
*) NULL
;
413 wxGridCell
*cell
= m_gridCells
[row
][col
];
416 // m_gridCells[row][col] = OnCreateCell();
417 wxGridEvent
g_evt(GetId(), wxEVT_GRID_CREATE_CELL
, (wxGenericGrid
*) this, row
, col
);
418 GetEventHandler()->ProcessEvent(g_evt
);
419 m_gridCells
[row
][col
] = g_evt
.m_cell
;
420 return m_gridCells
[row
][col
];
426 void wxGenericGrid::SetGridClippingRegion(wxDC
*dc
)
428 int m_scrollWidthHoriz
= 0;
429 int m_scrollWidthVert
= 0;
431 GetClientSize(&cw
, &ch
);
433 if (m_hScrollBar
&& m_hScrollBar
->IsShown())
434 m_scrollWidthHoriz
= m_scrollWidth
;
435 if (m_vScrollBar
&& m_vScrollBar
->IsShown())
436 m_scrollWidthVert
= m_scrollWidth
;
438 // Don't paint over the scrollbars
439 dc
->SetClippingRegion(m_leftOfSheet
, m_topOfSheet
,
440 cw
- m_scrollWidthVert
- m_leftOfSheet
, ch
- m_scrollWidthHoriz
- m_topOfSheet
);
443 void wxGenericGrid::OnPaint(wxPaintEvent
& WXUNUSED(event
))
446 GetClientSize(&w
, &h
);
448 bool useDoubleBuffering
= (bool) wxUSE_DOUBLE_BUFFERING
;
449 if (useDoubleBuffering
)
451 // Reuse the old bitmap if possible
453 if (!m_doubleBufferingBitmap
||
454 (m_doubleBufferingBitmap
->GetWidth() < w
|| m_doubleBufferingBitmap
->GetHeight() < h
))
456 if (m_doubleBufferingBitmap
)
457 delete m_doubleBufferingBitmap
;
458 m_doubleBufferingBitmap
= new wxBitmap(w
, h
);
460 if (!m_doubleBufferingBitmap
|| !m_doubleBufferingBitmap
->Ok())
462 // If we couldn't create a new bitmap, perhaps because resources were low,
463 // then don't complain, just don't double-buffer
464 if (m_doubleBufferingBitmap
)
465 delete m_doubleBufferingBitmap
;
466 m_doubleBufferingBitmap
= (wxBitmap
*) NULL
;
467 useDoubleBuffering
= FALSE
;
471 if (useDoubleBuffering
)
473 wxPaintDC
paintDC(this);
474 wxMemoryDC
dc(& paintDC
);
475 dc
.SelectObject(* m_doubleBufferingBitmap
);
479 int vertScrollBarWidth
= m_scrollWidth
;
480 int horizScrollBarHeight
= m_scrollWidth
;
481 if (m_vScrollBar
&& !m_vScrollBar
->IsShown())
482 vertScrollBarWidth
= 0;
483 if (m_hScrollBar
&& !m_hScrollBar
->IsShown())
484 horizScrollBarHeight
= 0;
486 paintDC
.Blit(m_leftOfSheet
, m_topOfSheet
, w
- vertScrollBarWidth
- m_leftOfSheet
, h
- horizScrollBarHeight
- m_topOfSheet
,
487 &dc
, m_leftOfSheet
, m_topOfSheet
, wxCOPY
);
489 dc
.SelectObject(wxNullBitmap
);
498 void wxGenericGrid::PaintGrid(wxDC
& dc
)
501 dc
.SetOptimization(FALSE
);
503 SetGridClippingRegion(& dc
);
505 DrawLabelAreas(& dc
);
507 DrawEditableArea(& dc
);
508 DrawColumnLabels(& dc
);
513 /* Hilight present cell */
514 SetCurrentRect(m_wCursorRow
, m_wCursorColumn
);
515 if (m_currentRectVisible
)
518 dc
.DestroyClippingRegion();
519 dc
.SetOptimization(TRUE
);
523 // Erase (some of) the background.
524 // Currently, a Windows-only optimisation.
525 void wxGenericGrid::OnEraseBackground(wxEraseEvent
& WXUNUSED(event
) )
529 dc
.SetOptimization(FALSE
);
532 GetClientSize(& w
, & h
);
533 dc
.SetBrush(*wxLIGHT_GREY_BRUSH
);
534 dc
.SetPen(*wxLIGHT_GREY_PEN
);
536 if (m_hScrollBar
&& m_hScrollBar
->IsShown() && m_vScrollBar
&& m_vScrollBar
->IsShown())
538 dc
.DrawRectangle(w
- m_scrollWidth
, h
- m_scrollWidth
, m_scrollWidth
, m_scrollWidth
);
541 dc
.SetOptimization(TRUE
);
546 void wxGenericGrid::DrawLabelAreas(wxDC
*dc
)
549 GetClientSize(&cw
, &ch
);
551 dc
->SetPen(*wxTRANSPARENT_PEN
);
552 // dc->SetBrush(*dc->GetBackground());
554 // Should blank out any area which isn't going to be painted over.
555 // dc->DrawRectangle(m_leftOfSheet, m_bottomOfSheet, cw - m_leftOfSheet, ch - m_bottomOfSheet);
556 // dc->DrawRectangle(m_rightOfSheet, m_topOfSheet, cw - m_rightOfSheet, ch - m_topOfSheet);
558 // Paint the label areas
559 dc
->SetBrush(m_labelBackgroundBrush
);
560 // dc->DrawRectangle(m_leftOfSheet, m_topOfSheet, m_rightOfSheet - m_leftOfSheet + 1, m_horizontalLabelHeight + 1);
561 dc
->DrawRectangle(m_leftOfSheet
, m_topOfSheet
, cw
-m_leftOfSheet
, m_horizontalLabelHeight
+ 1);
562 // dc->DrawRectangle(m_leftOfSheet, m_topOfSheet, m_verticalLabelWidth + 1, m_bottomOfSheet - m_topOfSheet + 1);
563 dc
->DrawRectangle(m_leftOfSheet
, m_topOfSheet
, m_verticalLabelWidth
+ 1, ch
-m_topOfSheet
);
566 void wxGenericGrid::DrawEditableArea(wxDC
*dc
)
569 GetClientSize(&cw
, &ch
);
571 dc
->SetPen(*wxTRANSPARENT_PEN
);
572 dc
->SetBrush(*wxTheBrushList
->FindOrCreateBrush(m_cellBackgroundColour
, wxSOLID
));
573 // dc->DrawRectangle(m_leftOfSheet+m_verticalLabelWidth, m_topOfSheet+m_horizontalLabelHeight,
574 // m_rightOfSheet-(m_leftOfSheet+m_verticalLabelWidth) + 1, m_bottomOfSheet - (m_topOfSheet+m_horizontalLabelHeight) + 1);
575 dc
->DrawRectangle(m_leftOfSheet
+m_verticalLabelWidth
, m_topOfSheet
+m_horizontalLabelHeight
,
576 cw
-(m_leftOfSheet
+m_verticalLabelWidth
), ch
- (m_topOfSheet
+m_horizontalLabelHeight
));
579 void wxGenericGrid::DrawGridLines(wxDC
*dc
)
582 GetClientSize(&cw
, &ch
);
586 if (m_divisionPen
.Ok())
588 dc
->SetPen(m_divisionPen
);
590 int heightCount
= m_topOfSheet
+ m_horizontalLabelHeight
;
592 // Draw horizontal grey lines for cells
593 for (i
= m_scrollPosY
; i
< (m_totalRows
+1); i
++)
595 if (heightCount
> ch
)
599 dc
->DrawLine(m_leftOfSheet
, heightCount
,
602 heightCount
+= m_rowHeights
[i
];
607 if (m_verticalLabelWidth
> 0)
609 dc
->SetPen(*wxBLACK_PEN
);
611 // Draw horizontal black lines for row labels
612 int heightCount
= m_topOfSheet
+ m_horizontalLabelHeight
;
613 for (i
= m_scrollPosY
; i
< (m_totalRows
+1); i
++)
615 if (heightCount
> ch
)
619 dc
->DrawLine(m_leftOfSheet
, heightCount
,
620 m_verticalLabelWidth
, heightCount
);
622 heightCount
+= m_rowHeights
[i
];
625 // Draw a black vertical line for row number cells
626 dc
->DrawLine(m_leftOfSheet
+ m_verticalLabelWidth
, m_topOfSheet
,
627 m_leftOfSheet
+ m_verticalLabelWidth
, ch
);
628 // First vertical line
629 dc
->DrawLine(m_leftOfSheet
, m_topOfSheet
, m_leftOfSheet
, ch
);
631 dc
->SetPen(*wxWHITE_PEN
);
633 // Draw highlights on row labels
634 heightCount
= m_topOfSheet
+ m_horizontalLabelHeight
;
635 for (i
= m_scrollPosY
; i
< m_totalRows
; i
++)
637 if (heightCount
> ch
)
641 dc
->DrawLine(m_leftOfSheet
+1, heightCount
+1,
642 m_verticalLabelWidth
, heightCount
+1);
643 dc
->DrawLine(m_leftOfSheet
+1, heightCount
+1,
644 m_leftOfSheet
+1, heightCount
+ m_rowHeights
[i
] - 1);
645 heightCount
+= m_rowHeights
[i
];
648 // Last one - down to the floor.
649 dc
->DrawLine(m_leftOfSheet
+1, heightCount
+1,
650 m_verticalLabelWidth
, heightCount
+1);
651 dc
->DrawLine(m_leftOfSheet
+1, heightCount
+1,
652 m_leftOfSheet
+1, ch
);
656 if (m_divisionPen
.Ok())
658 dc
->SetPen(m_divisionPen
);
660 // Draw vertical grey lines for cells
661 int widthCount
= m_leftOfSheet
+ m_verticalLabelWidth
;
662 for (i
= m_scrollPosX
; i
< m_totalCols
; i
++)
668 // Skip the first one
669 if (i
!= m_scrollPosX
)
671 dc
->DrawLine(widthCount
, m_topOfSheet
+ m_horizontalLabelHeight
,
672 widthCount
, m_bottomOfSheet
);
674 widthCount
+= m_colWidths
[i
];
678 dc
->DrawLine(widthCount
, m_topOfSheet
+ m_horizontalLabelHeight
,
679 widthCount
, m_bottomOfSheet
);
682 dc
->SetPen(*wxBLACK_PEN
);
684 // Draw two black horizontal lines for column number cells
686 m_leftOfSheet
, m_topOfSheet
,
688 dc
->DrawLine(m_leftOfSheet
, m_topOfSheet
+ m_horizontalLabelHeight
,
689 cw
, m_topOfSheet
+ m_horizontalLabelHeight
);
691 if (m_horizontalLabelHeight
> 0)
693 int widthCount
= m_leftOfSheet
+ m_verticalLabelWidth
;
695 // Draw black vertical lines for column number cells
696 for (i
= m_scrollPosX
; i
< m_totalCols
; i
++)
702 dc
->DrawLine(widthCount
, m_topOfSheet
,
703 widthCount
, m_topOfSheet
+ m_horizontalLabelHeight
);
704 widthCount
+= m_colWidths
[i
];
709 dc
->DrawLine(widthCount
, m_topOfSheet
,
710 widthCount
, m_topOfSheet
+ m_horizontalLabelHeight
);
713 dc
->SetPen(*wxWHITE_PEN
);
714 widthCount
= m_leftOfSheet
+ m_verticalLabelWidth
;
716 for (i
= m_scrollPosX
; i
< m_totalCols
; i
++)
722 dc
->DrawLine(widthCount
+1, m_topOfSheet
+1,
723 widthCount
+m_colWidths
[i
], m_topOfSheet
+1);
724 dc
->DrawLine(widthCount
+1, m_topOfSheet
+1,
725 widthCount
+1, m_topOfSheet
+m_horizontalLabelHeight
);
726 widthCount
+= m_colWidths
[i
];
729 // Last one - to the right side of the canvas.
730 dc
->DrawLine(widthCount
+1, m_topOfSheet
+1,
732 dc
->DrawLine(widthCount
+1, m_topOfSheet
+1,
733 widthCount
+1, m_topOfSheet
+m_horizontalLabelHeight
);
738 void wxGenericGrid::DrawColumnLabels(wxDC
*dc
)
741 GetClientSize(&cw
, &ch
);
743 if (m_horizontalLabelHeight
== 0)
749 // Draw letters for columns
750 rect
.y
= m_topOfSheet
+ 1;
751 rect
.height
= m_horizontalLabelHeight
- 1;
753 dc
->SetTextBackground(m_labelBackgroundColour
);
754 dc
->SetBackgroundMode(wxTRANSPARENT
);
755 // dc->SetTextForeground(m_labelTextColour);
757 int widthCount
= m_leftOfSheet
+ m_verticalLabelWidth
;
758 for (i
= m_scrollPosX
; i
< m_totalCols
; i
++)
764 rect
.x
= 1 + widthCount
;
765 rect
.width
= m_colWidths
[i
];
766 DrawColumnLabel(dc
, &rect
, i
);
768 widthCount
+= m_colWidths
[i
];
773 void wxGenericGrid::DrawColumnLabel(wxDC
*dc
, wxRect
*rect
, int col
)
775 wxGridCell
*cell
= GetLabelCell(wxHORIZONTAL
, col
);
784 dc
->SetTextForeground(GetLabelTextColour());
785 dc
->SetFont(GetLabelTextFont());
786 if ( !cell
->GetTextValue().IsNull() )
787 DrawTextRect(dc
, cell
->GetTextValue(), &rect2
, GetLabelAlignment(wxHORIZONTAL
));
791 void wxGenericGrid::DrawRowLabels(wxDC
*dc
)
794 GetClientSize(&cw
, &ch
);
796 if (m_verticalLabelWidth
== 0)
802 // Draw numbers for rows
803 rect
.x
= m_leftOfSheet
;
804 rect
.width
= m_verticalLabelWidth
;
806 int heightCount
= m_topOfSheet
+ m_horizontalLabelHeight
;
808 dc
->SetTextBackground(m_labelBackgroundColour
);
809 dc
->SetBackgroundMode(wxTRANSPARENT
);
811 for (i
= m_scrollPosY
; i
< m_totalRows
; i
++)
813 if (heightCount
> ch
)
817 rect
.y
= 1 + heightCount
;
818 rect
.height
= m_rowHeights
[i
];
819 DrawRowLabel(dc
, &rect
, i
);
821 heightCount
+= m_rowHeights
[i
];
826 void wxGenericGrid::DrawRowLabel(wxDC
*dc
, wxRect
*rect
, int row
)
828 wxGridCell
*cell
= GetLabelCell(wxVERTICAL
, row
);
837 dc
->SetTextForeground(GetLabelTextColour());
838 dc
->SetFont(GetLabelTextFont());
839 if ( !cell
->GetTextValue().IsNull() )
840 DrawTextRect(dc
, cell
->GetTextValue(), &rect2
, GetLabelAlignment(wxVERTICAL
));
844 void wxGenericGrid::DrawCells(wxDC
*dc
)
847 GetClientSize(&cw
, &ch
);
851 // Draw value corresponding to each cell
852 for (i
= m_scrollPosY
; i
< m_totalRows
; i
++)
854 for (j
= m_scrollPosX
; j
< m_totalCols
; j
++)
856 SetCurrentRect(i
, j
, cw
, ch
);
857 if (m_currentRectVisible
)
859 DrawCellBackground(dc
, &m_currentRect
, i
, j
);
860 DrawCellValue(dc
, &m_currentRect
, i
, j
);
862 if (m_currentRect
.x
> cw
)
865 if (m_currentRect
.y
> ch
)
868 dc
->SetBackgroundMode(wxSOLID
);
869 dc
->SetPen(*wxBLACK_PEN
);
872 void wxGenericGrid::DrawCellBackground(wxDC
*dc
, wxRect
*rect
, int row
, int col
)
874 wxGridCell
*cell
= GetCell(row
, col
);
877 dc
->SetBrush(cell
->GetBackgroundBrush());
878 dc
->SetPen(*wxTRANSPARENT_PEN
);
880 #if 0 // In wxWin 2.0 the dc code is exact. RR.
882 dc
->DrawRectangle(rect
->x
+1, rect
->y
+1, rect
->width
-1, rect
->height
-1);
884 dc
->DrawRectangle(rect
->x
+1, rect
->y
+1, rect
->width
, rect
->height
);
888 dc
->DrawRectangle(rect
->x
+1, rect
->y
+1, rect
->width
-1, rect
->height
-1);
890 dc
->SetPen(*wxBLACK_PEN
);
894 void wxGenericGrid::DrawCellValue(wxDC
*dc
, wxRect
*rect
, int row
, int col
)
896 wxGridCell
*cell
= GetCell(row
, col
);
899 wxBitmap
*bitmap
= cell
->GetCellBitmap();
909 DrawBitmapRect(dc
, bitmap
, &rect2
, cell
->GetAlignment());
913 dc
->SetBackgroundMode(wxTRANSPARENT
);
914 dc
->SetTextForeground(cell
->GetTextColour());
915 dc
->SetFont(cell
->GetFont());
917 if ( !cell
->GetTextValue().IsNull() )
918 DrawTextRect(dc
, cell
->GetTextValue(), &rect2
, cell
->GetAlignment());
923 void wxGenericGrid::AdjustScrollbars(void)
926 GetClientSize(&cw
, &ch
);
928 // We find the view size by seeing how many rows/cols fit on
930 // BUT... this means that the scrollbar should be adjusted every time
931 // it's scrolled, as well as when sized, because with variable size rows/cols,
932 // the number of rows/col visible on the view differs according to what bit
933 // you're looking at. The object length is always the same, but the
934 // view length differs.
936 // Since this may not be known until the end of this function, we should probably call AdjustScrollbars
938 int vertScrollBarWidth
= m_scrollWidth
;
939 int horizScrollBarHeight
= m_scrollWidth
;
940 if (m_vScrollBar
&& !m_vScrollBar
->IsShown())
941 vertScrollBarWidth
= 0;
942 if (m_hScrollBar
&& !m_hScrollBar
->IsShown())
943 horizScrollBarHeight
= 0;
945 int noHorizSteps
= 0;
948 if (m_totalGridWidth
+ vertScrollBarWidth
<= cw
)
957 for (i
= m_scrollPosX
; i
< m_totalCols
; i
++)
959 widthCount
+= m_colWidths
[i
];
960 // A partial bit doesn't count, we still have to scroll to see the
962 if (widthCount
+ m_leftOfSheet
+ m_verticalLabelWidth
> (cw
-vertScrollBarWidth
))
971 if (m_totalGridHeight
+ horizScrollBarHeight
<= ch
)
980 for (i
= m_scrollPosY
; i
< m_totalRows
; i
++)
982 heightCount
+= m_rowHeights
[i
];
983 // A partial bit doesn't count, we still have to scroll to see the
985 if (heightCount
+ m_topOfSheet
+ m_horizontalLabelHeight
> (ch
-horizScrollBarHeight
))
994 if (m_totalGridWidth
+ vertScrollBarWidth
<= cw
)
997 m_hScrollBar
->Show(FALSE
);
1003 m_hScrollBar
->Show(TRUE
);
1006 if (m_totalGridHeight
+ horizScrollBarHeight
<= ch
)
1009 m_vScrollBar
->Show(FALSE
);
1015 m_vScrollBar
->Show(TRUE
);
1018 UpdateDimensions(); // Necessary in case m_scrollPosX/Y changed
1020 vertScrollBarWidth
= m_scrollWidth
;
1021 horizScrollBarHeight
= m_scrollWidth
;
1022 if (m_vScrollBar
&& !m_vScrollBar
->IsShown())
1023 vertScrollBarWidth
= 0;
1024 if (m_hScrollBar
&& !m_hScrollBar
->IsShown())
1025 horizScrollBarHeight
= 0;
1029 int nCols
= GetCols();
1030 m_hScrollBar
->SetScrollbar(m_hScrollBar
->GetThumbPosition(), wxMax(noHorizSteps
, 1), (noHorizSteps
== 0) ? 1 : nCols
, wxMax(noHorizSteps
, 1));
1033 m_hScrollBar->SetSize(m_leftOfSheet, ch - m_scrollWidth -2, // why -2 ? Robert.
1034 cw - vertScrollBarWidth - m_leftOfSheet, m_scrollWidth);
1036 m_hScrollBar
->SetSize(m_leftOfSheet
, ch
- m_scrollWidth
,
1037 cw
- vertScrollBarWidth
- m_leftOfSheet
, m_scrollWidth
);
1043 int nRows
= GetRows();
1045 m_vScrollBar
->SetScrollbar(m_vScrollBar
->GetThumbPosition(), wxMax(noVertSteps
, 1), (noVertSteps
== 0) ? 1 : nRows
, wxMax(noVertSteps
, 1));
1046 m_vScrollBar
->SetSize(cw
- m_scrollWidth
, m_topOfSheet
,
1047 m_scrollWidth
, ch
- m_topOfSheet
- horizScrollBarHeight
);
1051 void wxGenericGrid::OnSize(wxSizeEvent
& WXUNUSED(event
) )
1053 if (!m_vScrollBar
|| !m_hScrollBar
)
1059 GetClientSize(&cw
, &ch
);
1061 if (m_editCreated
&& m_editingPanel
&& GetTextItem() && GetTextItem()->IsShown())
1063 m_editingPanel
->SetSize(0, 0, cw
, m_editControlPosition
.height
+ m_editControlPosition
.y
+ 2);
1064 GetTextItem()->SetSize(m_editControlPosition
.x
, m_editControlPosition
.y
,
1065 cw
- 2*m_editControlPosition
.x
, m_editControlPosition
.height
);
1069 bool wxGenericGrid::CellHitTest(int x
, int y
, int *row
, int *col
)
1071 // Find the selected cell and call OnSelectCell
1072 if (x
>= (m_leftOfSheet
+ m_verticalLabelWidth
) && y
>= (m_topOfSheet
+ m_horizontalLabelHeight
) &&
1073 x
<= m_rightOfSheet
&& y
<= m_bottomOfSheet
)
1075 // Calculate the cell number from x and y
1076 x
-= (m_verticalLabelWidth
+ m_leftOfSheet
);
1077 y
-= (m_topOfSheet
+ m_horizontalLabelHeight
);
1081 // Now we need to do a hit test for which row we're on
1082 int currentHeight
= 0;
1083 for (i
= m_scrollPosY
; i
< m_totalRows
; i
++)
1085 if (y
>= currentHeight
&& y
<= (currentHeight
+ m_rowHeights
[i
]))
1090 currentHeight
+= m_rowHeights
[i
];
1093 // Now we need to do a hit test for which column we're on
1094 int currentWidth
= 0;
1095 for (i
= m_scrollPosX
; i
< m_totalCols
; i
++)
1097 if (x
>= currentWidth
&& x
<= (currentWidth
+ m_colWidths
[i
]))
1102 currentWidth
+= m_colWidths
[i
];
1109 bool wxGenericGrid::LabelSashHitTest(int x
, int y
, int *orientation
, int *rowOrCol
, int *startPos
)
1114 if (x
>= (m_leftOfSheet
+ m_verticalLabelWidth
) && y
>= m_topOfSheet
&&
1115 x
<= m_rightOfSheet
&& y
<= (m_topOfSheet
+ m_horizontalLabelHeight
))
1117 // We may be on a column label sash.
1118 int currentWidth
= m_leftOfSheet
+ m_verticalLabelWidth
;
1119 for (i
= m_scrollPosX
; i
< m_totalCols
; i
++)
1121 if (x
>= (currentWidth
+ m_colWidths
[i
] - tolerance
) && x
<= (currentWidth
+ m_colWidths
[i
] + tolerance
))
1123 *orientation
= wxHORIZONTAL
;
1125 *startPos
= currentWidth
;
1128 currentWidth
+= m_colWidths
[i
];
1132 else if (x
>= m_leftOfSheet
&& y
>= (m_topOfSheet
+ m_horizontalLabelHeight
) &&
1133 x
<= (m_leftOfSheet
+ m_verticalLabelWidth
) && y
<= m_bottomOfSheet
)
1135 // We may be on a row label sash.
1136 int currentHeight
= m_topOfSheet
+ m_horizontalLabelHeight
;
1137 for (i
= m_scrollPosY
; i
< m_totalRows
; i
++)
1139 if (y
>= (currentHeight
+ m_rowHeights
[i
] - tolerance
) && y
<= (currentHeight
+ m_rowHeights
[i
] + tolerance
))
1141 *orientation
= wxVERTICAL
;
1143 *startPos
= currentHeight
;
1146 currentHeight
+= m_rowHeights
[i
];
1153 bool wxGenericGrid::LabelHitTest(int x
, int y
, int *row
, int *col
)
1155 // Find the selected label
1156 if (x
>= m_leftOfSheet
&& y
>= m_topOfSheet
&&
1157 x
<= m_rightOfSheet
&& y
<= m_bottomOfSheet
)
1159 // Calculate the cell number from x and y
1165 // Now we need to do a hit test for which row we're on
1166 int currentHeight
= m_horizontalLabelHeight
;
1167 for (i
= m_scrollPosY
; i
< m_totalRows
; i
++)
1169 if (y
>= currentHeight
&& y
<= (currentHeight
+ m_rowHeights
[i
]))
1174 currentHeight
+= m_rowHeights
[i
];
1176 if (y
>= 0 && y
<= m_horizontalLabelHeight
)
1181 // Now we need to do a hit test for which column we're on
1182 int currentWidth
= m_verticalLabelWidth
;
1183 for (i
= m_scrollPosX
; i
< m_totalCols
; i
++)
1185 if (x
>= currentWidth
&& x
<= (currentWidth
+ m_colWidths
[i
]))
1190 currentWidth
+= m_colWidths
[i
];
1192 if (x
>= 0 && x
<= m_verticalLabelWidth
)
1197 if ((*col
== -1) || (*row
== -1))
1205 void wxGenericGrid::OnMouseEvent(wxMouseEvent
& ev
)
1209 wxClientDC
dc(this);
1213 if (CellHitTest((int)ev
.GetX(), (int)ev
.GetY(), &row
, &col
))
1215 OnSelectCellImplementation(& dc
, row
, col
);
1217 //OnCellLeftClick(row, col, (int)ev.GetX(), (int)ev.GetY(), ev.ControlDown(), ev.ShiftDown());
1218 wxGridEvent
g_evt(GetId(), wxEVT_GRID_CELL_LCLICK
, this,
1219 row
, col
, (int)ev
.GetX(), (int)ev
.GetY(),
1220 ev
.ControlDown(), ev
.ShiftDown());
1221 GetEventHandler()->ProcessEvent(g_evt
);
1224 if (LabelHitTest((int)ev
.GetX(), (int)ev
.GetY(), &row
, &col
))
1226 //OnLabelLeftClick(row, col, (int)ev.GetX(), (int)ev.GetY(), ev.ControlDown(), ev.ShiftDown());
1227 wxGridEvent
g_evt(GetId(), wxEVT_GRID_LABEL_LCLICK
, this,
1228 row
, col
, (int)ev
.GetX(), (int)ev
.GetY(),
1229 ev
.ControlDown(), ev
.ShiftDown());
1230 GetEventHandler()->ProcessEvent(g_evt
);
1235 else if (ev
.Dragging() && ev
.LeftIsDown())
1237 switch (m_dragStatus
)
1239 case wxGRID_DRAG_NONE
:
1242 if (LabelSashHitTest((int)ev
.GetX(), (int)ev
.GetY(), &orientation
, &m_dragRowOrCol
, &m_dragStartPosition
))
1244 if (orientation
== wxHORIZONTAL
)
1246 m_dragStatus
= wxGRID_DRAG_LEFT_RIGHT
;
1247 SetCursor(m_horizontalSashCursor
);
1248 m_dragLastPosition
= (int)ev
.GetX();
1252 m_dragStatus
= wxGRID_DRAG_UP_DOWN
;
1253 SetCursor(m_verticalSashCursor
);
1254 m_dragLastPosition
= (int)ev
.GetY();
1256 wxClientDC
dc(this);
1258 dc
.SetLogicalFunction(wxINVERT
);
1259 if (orientation
== wxHORIZONTAL
)
1260 dc
.DrawLine((int)ev
.GetX(), m_topOfSheet
, (int)ev
.GetX(), m_bottomOfSheet
);
1262 dc
.DrawLine(m_leftOfSheet
, (int)ev
.GetY(), m_rightOfSheet
, (int)ev
.GetY());
1269 case wxGRID_DRAG_LEFT_RIGHT
:
1271 wxClientDC
dc(this);
1273 dc
.SetLogicalFunction(wxINVERT
);
1274 dc
.DrawLine(m_dragLastPosition
, m_topOfSheet
, m_dragLastPosition
, m_bottomOfSheet
);
1276 dc
.DrawLine((int)ev
.GetX(), m_topOfSheet
, (int)ev
.GetX(), m_bottomOfSheet
);
1279 m_dragLastPosition
= (int)ev
.GetX();
1280 SetCursor(m_horizontalSashCursor
);
1283 case wxGRID_DRAG_UP_DOWN
:
1285 wxClientDC
dc(this);
1287 dc
.SetLogicalFunction(wxINVERT
);
1288 dc
.DrawLine(m_leftOfSheet
, m_dragLastPosition
, m_rightOfSheet
, m_dragLastPosition
);
1290 dc
.DrawLine(m_leftOfSheet
, (int)ev
.GetY(), m_rightOfSheet
, (int)ev
.GetY());
1293 m_dragLastPosition
= (int)ev
.GetY();
1294 SetCursor(m_verticalSashCursor
);
1299 else if (ev
.Moving())
1301 int rowOrCol
, orientation
, startPos
;
1302 if (LabelSashHitTest((int)ev
.GetX(), (int)ev
.GetY(), &orientation
, &rowOrCol
, &startPos
))
1304 if (orientation
== wxHORIZONTAL
)
1305 SetCursor(m_horizontalSashCursor
);
1307 SetCursor(m_verticalSashCursor
);
1310 SetCursor(*wxSTANDARD_CURSOR
);
1312 else if (ev
.LeftUp())
1314 switch (m_dragStatus
)
1316 case wxGRID_DRAG_LEFT_RIGHT
:
1318 wxClientDC
dc(this);
1320 dc
.SetLogicalFunction(wxINVERT
);
1321 dc
.DrawLine(m_dragLastPosition
, m_topOfSheet
, m_dragLastPosition
, m_bottomOfSheet
);
1322 dc
.SetLogicalFunction(wxCOPY
);
1326 if (ev
.GetX() > m_dragStartPosition
)
1328 m_colWidths
[m_dragRowOrCol
] = (short)(ev
.GetX() - m_dragStartPosition
);
1333 SetCursor(*wxSTANDARD_CURSOR
);
1335 GetClientSize(&cw
, &ch
);
1340 case wxGRID_DRAG_UP_DOWN
:
1342 wxClientDC
dc(this);
1344 dc
.SetLogicalFunction(wxINVERT
);
1345 dc
.DrawLine(m_leftOfSheet
, m_dragLastPosition
, m_rightOfSheet
, m_dragLastPosition
);
1346 dc
.SetLogicalFunction(wxCOPY
);
1350 if (ev
.GetY() > m_dragStartPosition
)
1352 m_rowHeights
[m_dragRowOrCol
] = (short)(ev
.GetY() - m_dragStartPosition
);
1357 SetCursor(*wxSTANDARD_CURSOR
);
1361 m_dragStatus
= wxGRID_DRAG_NONE
;
1363 else if (ev
.RightDown())
1366 if (CellHitTest((int)ev
.GetX(), (int)ev
.GetY(), &row
, &col
))
1368 //OnCellRightClick(row, col, (int)ev.GetX(), (int)ev.GetY(), ev.ControlDown(), ev.ShiftDown());
1369 wxGridEvent
g_evt(GetId(), wxEVT_GRID_CELL_RCLICK
, this,
1370 row
, col
, (int)ev
.GetX(), (int)ev
.GetY(),
1371 ev
.ControlDown(), ev
.ShiftDown());
1372 GetEventHandler()->ProcessEvent(g_evt
);
1375 if (LabelHitTest((int)ev
.GetX(), (int)ev
.GetY(), &row
, &col
))
1377 //OnLabelRightClick(row, col, (int)ev.GetX(), (int)ev.GetY(), ev.ControlDown(), ev.ShiftDown());
1378 wxGridEvent
g_evt(GetId(), wxEVT_GRID_LABEL_RCLICK
, this,
1379 row
, col
, (int)ev
.GetX(), (int)ev
.GetY(),
1380 ev
.ControlDown(), ev
.ShiftDown());
1381 GetEventHandler()->ProcessEvent(g_evt
);
1386 void wxGenericGrid::OnSelectCellImplementation(wxDC
*dc
, int row
, int col
)
1388 m_wCursorColumn
= col
;
1391 //OnChangeSelectionLabel();
1392 wxGridEvent
g_evt(GetId(), wxEVT_GRID_CHANGE_SEL_LABEL
, this);
1393 GetEventHandler()->ProcessEvent(g_evt
);
1395 SetGridClippingRegion(dc
);
1397 // Remove the highlight from the old cell
1398 if (m_currentRectVisible
)
1401 // Highlight the new cell and copy its content to the edit control
1402 SetCurrentRect(m_wCursorRow
, m_wCursorColumn
);
1403 wxGridCell
*cell
= GetCell(m_wCursorRow
, m_wCursorColumn
);
1406 if ( cell
->GetTextValue().IsNull() )
1407 m_textItem
->SetValue("");
1409 m_textItem
->SetValue(cell
->GetTextValue());
1412 SetGridClippingRegion(dc
);
1414 // 1) Why isn't this needed for Windows??
1415 // Probably because of the SetValue?? JS.
1416 // 2) Arrrrrgh. This isn't needed anywhere,
1417 // of course. One hour of debugging... RR.
1419 // HighlightCell(dc);
1421 dc
->DestroyClippingRegion();
1423 //OnSelectCell(row, col);
1424 wxGridEvent
g_evt2(GetId(), wxEVT_GRID_SELECT_CELL
, this, row
, col
);
1425 GetEventHandler()->ProcessEvent(g_evt2
);
1428 wxGridCell
*wxGenericGrid::OnCreateCell(void)
1430 return new wxGridCell(this);
1433 void wxGenericGrid::OnChangeLabels(void)
1437 for (i
= 0; i
< m_totalRows
; i
++)
1439 sprintf(buf
, "%d", i
+1);
1440 SetLabelValue(wxVERTICAL
, buf
, i
);
1442 // A...Z,AA...ZZ,AAA...ZZZ, etc.
1443 for (i
= 0; i
< m_totalCols
; i
++)
1446 int noTimes
= (i
/26 + 1);
1447 int ch
= (i
% 26) + 65;
1449 for (j
= 0; j
< noTimes
; j
++)
1452 sprintf(buf2
, "%c", (char)ch
);
1455 SetLabelValue(wxHORIZONTAL
, buf
, i
);
1459 void wxGenericGrid::OnChangeSelectionLabel(void)
1464 wxString
rowLabel(GetLabelValue(wxVERTICAL
, GetCursorRow()));
1465 wxString
colLabel(GetLabelValue(wxHORIZONTAL
, GetCursorColumn()));
1467 wxString newLabel
= colLabel
+ rowLabel
;
1468 if ((newLabel
.Length() > 0) && (newLabel
.Length() <= 8) && GetTextItem())
1470 // GetTextItem()->SetLabel(newLabel);
1474 void wxGenericGrid::HighlightCell(wxDC
*dc
)
1476 dc
->SetLogicalFunction(wxINVERT
);
1479 dc
->DrawLine( m_currentRect
.x
+ 1,
1480 m_currentRect
.y
+ 1,
1481 m_currentRect
.x
+ m_currentRect
.width
- 1,
1482 m_currentRect
.y
+ 1);
1484 dc
->DrawLine( m_currentRect
.x
+ m_currentRect
.width
- 1,
1485 m_currentRect
.y
+ 1,
1486 m_currentRect
.x
+ m_currentRect
.width
- 1,
1487 m_currentRect
.y
+m_currentRect
.height
- 1 );
1489 dc
->DrawLine( m_currentRect
.x
+ m_currentRect
.width
- 1,
1490 m_currentRect
.y
+ m_currentRect
.height
- 1,
1491 m_currentRect
.x
+ 1,
1492 m_currentRect
.y
+ m_currentRect
.height
- 1);
1494 dc
->DrawLine( m_currentRect
.x
+ 1,
1495 m_currentRect
.y
+ m_currentRect
.height
- 1,
1496 m_currentRect
.x
+ 1,
1497 m_currentRect
.y
+ 1);
1499 dc
->SetLogicalFunction(wxCOPY
);
1502 void wxGenericGrid::DrawCellText(void)
1504 if (!m_currentRectVisible
)
1507 wxGridCell
*cell
= GetCell(GetCursorRow(), GetCursorColumn());
1511 static char szEdit
[300];
1513 wxClientDC
dc(this);
1516 SetGridClippingRegion(& dc
);
1518 dc
.SetBackgroundMode(wxTRANSPARENT
);
1519 dc
.SetBrush(cell
->GetBackgroundBrush());
1521 strcpy(szEdit
, m_textItem
->GetValue());
1524 rect
= m_currentRect
;
1530 DrawTextRect(& dc
, " ", &rect
, wxLEFT
);
1531 DrawTextRect(& dc
, szEdit
, &rect
, cell
->GetAlignment());
1533 dc
.DestroyClippingRegion();
1535 dc
.SetBackgroundMode(wxSOLID
);
1540 void wxGenericGrid::SetCurrentRect(int Row
, int Column
, int canvasW
, int canvasH
)
1542 int currentWidth
= m_leftOfSheet
+ m_verticalLabelWidth
;
1544 for (i
= m_scrollPosX
; i
< Column
; i
++)
1545 currentWidth
+= m_colWidths
[i
];
1547 int currentHeight
= m_topOfSheet
+ m_horizontalLabelHeight
;
1548 for (i
= m_scrollPosY
; i
< Row
; i
++)
1549 currentHeight
+= m_rowHeights
[i
];
1551 m_currentRect
.x
= currentWidth
;
1552 m_currentRect
.y
= currentHeight
;
1553 m_currentRect
.width
= m_colWidths
? (m_colWidths
[Column
]) : 0;
1554 m_currentRect
.height
= m_rowHeights
? (m_rowHeights
[Row
]) : 0;
1556 if (Row
< m_scrollPosY
|| Column
< m_scrollPosX
)
1557 m_currentRectVisible
= FALSE
;
1558 else if ((canvasW
!= -1 && canvasH
!= -1) && (m_currentRect
.x
> canvasW
|| m_currentRect
.y
> canvasH
))
1559 m_currentRectVisible
= FALSE
;
1560 else m_currentRectVisible
= TRUE
;
1563 static bool wxRectIntersection(wxRect
*rect1
, wxRect
*rect2
, wxRect
*rect3
)
1565 int x2_1
= rect1
->x
+ rect1
->width
;
1566 int y2_1
= rect1
->y
+ rect1
->height
;
1568 int x2_2
= rect2
->x
+ rect2
->width
;
1569 int y2_2
= rect2
->y
+ rect2
->height
;
1573 // Check for intersection
1574 if ((rect1
->x
> x2_2
) || (rect2
->x
> x2_1
) ||
1575 (rect1
->y
> y2_2
) || (rect2
->y
> y2_1
))
1578 rect3
->x
= rect3
->y
= rect3
->width
= rect3
->height
= 0;
1582 if (rect1
->x
> rect2
->x
)
1583 rect3
->x
= rect1
->x
;
1585 rect3
->x
= rect2
->x
;
1586 if (rect1
->y
> rect2
->y
)
1587 rect3
->y
= rect1
->y
;
1589 rect3
->y
= rect2
->y
;
1600 rect3
->width
= (int)(x2_3
- rect3
->x
);
1601 rect3
->height
= (int)(y2_3
- rect3
->y
);
1605 void wxGenericGrid::DrawTextRect(wxDC
*dc
, const wxString
& text
, wxRect
*rect
, int flag
)
1609 // Ultimately, this functionality should be built into wxWindows,
1610 // and optimized for each platform. E.g. on Windows, use DrawText
1611 // passing a clipping rectangle, so that the wxWindows clipping region
1612 // does not have to be used to implement this.
1614 // If we're already clipping, we need to find the intersection
1615 // between current clipping area and text clipping area.
1619 long clipX
, clipY
, clipW
, clipH
;
1620 dc
->GetClippingBox(&clipX
, &clipY
, &clipW
, &clipH
);
1621 clipRect
.x
= (int)clipX
; clipRect
.y
= (int)clipY
;
1622 clipRect
.width
= (int)clipW
; clipRect
.height
= (int)clipH
;
1624 bool alreadyClipping
= TRUE
;
1626 if (clipRect
.x
== 0 && clipRect
.y
== 0 && clipRect
.width
== 0 && clipRect
.height
== 0)
1628 alreadyClipping
= FALSE
;
1629 clipRect2
.x
= rect
->x
; clipRect2
.y
= rect
->y
;
1630 clipRect2
.width
= rect
->width
; clipRect2
.height
= rect
->height
;
1634 // Find intersection.
1635 if (!wxRectIntersection(rect
, &clipRect
, &clipRect2
))
1639 if (alreadyClipping
)
1640 dc
->DestroyClippingRegion();
1642 dc
->SetClippingRegion(clipRect2
.x
, clipRect2
.y
, clipRect2
.width
, clipRect2
.height
);
1643 long textWidth
, textHeight
;
1645 dc
->GetTextExtent(text
, &textWidth
, &textHeight
);
1653 x
= (rect
->x
+ rect
->width
- textWidth
- 1.0);
1654 y
= (rect
->y
+ (rect
->height
- textHeight
)/2.0);
1659 x
= (rect
->x
+ (rect
->width
- textWidth
)/2.0);
1660 y
= (rect
->y
+ (rect
->height
- textHeight
)/2.0);
1666 x
= (rect
->x
+ 1.0);
1667 y
= (rect
->y
+ (rect
->height
- textHeight
)/2.0);
1671 dc
->DrawText(text
, (long)x
, (long)y
);
1673 dc
->DestroyClippingRegion();
1675 // Restore old clipping
1676 if (alreadyClipping
)
1677 dc
->SetClippingRegion(clipRect
.x
, clipRect
.y
, clipRect
.width
, clipRect
.height
);
1682 void wxGenericGrid::DrawBitmapRect(wxDC
*dc
, wxBitmap
*bitmap
, wxRect
*rect
, int flag
)
1686 // Ultimately, this functionality should be built into wxWindows,
1687 // and optimized for each platform. E.g. on Windows, use DrawText
1688 // passing a clipping rectangle, so that the wxWindows clipping region
1689 // does not have to be used to implement this.
1691 // If we're already clipping, we need to find the intersection
1692 // between current clipping area and text clipping area.
1696 long clipX
, clipY
, clipW
, clipH
;
1697 dc
->GetClippingBox(&clipX
, &clipY
, &clipW
, &clipH
);
1698 clipRect
.x
= (int)clipX
; clipRect
.y
= (int)clipY
;
1699 clipRect
.width
= (int)clipW
; clipRect
.height
= (int)clipH
;
1701 bool alreadyClipping
= TRUE
;
1703 if (clipRect
.x
== 0 && clipRect
.y
== 0 && clipRect
.width
== 0 && clipRect
.height
== 0)
1705 alreadyClipping
= FALSE
;
1706 clipRect2
.x
= rect
->x
; clipRect2
.y
= rect
->y
;
1707 clipRect2
.width
= rect
->width
; clipRect2
.height
= rect
->height
;
1711 // Find intersection.
1712 if (!wxRectIntersection(rect
, &clipRect
, &clipRect2
))
1716 if (alreadyClipping
)
1717 dc
->DestroyClippingRegion();
1719 dc
->SetClippingRegion(clipRect2
.x
, clipRect2
.y
, clipRect2
.width
, clipRect2
.height
);
1720 float bitmapWidth
, bitmapHeight
;
1722 bitmapWidth
= bitmap
->GetWidth();
1723 bitmapHeight
= bitmap
->GetHeight();
1731 x
= (long)(rect
->x
+ rect
->width
- bitmapWidth
- 1);
1732 y
= (long)(rect
->y
+ (rect
->height
- bitmapHeight
)/2.0);
1737 x
= (long)(rect
->x
+ (rect
->width
- bitmapWidth
)/2.0);
1738 y
= (long)(rect
->y
+ (rect
->height
- bitmapHeight
)/2.0);
1744 x
= (long)(rect
->x
+ 1);
1745 y
= (long)(rect
->y
+ (rect
->height
- bitmapHeight
)/2.0);
1750 dcTemp
.SelectObject(*bitmap
);
1752 dc
->Blit( (long)x
, (long)y
, (long)bitmapWidth
, (long)bitmapHeight
, &dcTemp
, 0, 0);
1753 dcTemp
.SelectObject(wxNullBitmap
);
1755 dc
->DestroyClippingRegion();
1757 // Restore old clipping
1758 if (alreadyClipping
)
1759 dc
->SetClippingRegion(clipRect
.x
, clipRect
.y
, clipRect
.width
, clipRect
.height
);
1764 void wxGenericGrid::OnActivate(bool active
)
1768 // Edit control should always have the focus
1769 if (GetTextItem() && GetEditable())
1771 GetTextItem()->SetFocus();
1772 wxGridCell
*cell
= GetCell(GetCursorRow(), GetCursorColumn());
1774 GetTextItem()->SetValue(cell
->GetTextValue());
1779 void wxGenericGrid::SetCellValue(const wxString
& val
, int row
, int col
)
1781 wxGridCell
*cell
= GetCell(row
, col
);
1784 cell
->SetTextValue(val
);
1786 RefreshCell(row
, col
, TRUE
);
1790 void wxGenericGrid::RefreshCell(int row
, int col
, bool setText
)
1792 // Don't refresh within a pair of batch brackets
1793 if (GetBatchCount() > 0)
1797 GetClientSize(&cw
, &ch
);
1799 SetCurrentRect(row
, col
, cw
, ch
);
1800 if (m_currentRectVisible
)
1802 wxGridCell
*cell
= GetCell(row
, col
);
1804 bool currentPos
= FALSE
;
1805 if (row
== m_wCursorRow
&& col
== m_wCursorColumn
&& GetTextItem() && GetTextItem()->IsShown() && setText
)
1807 GetTextItem()->SetValue(cell
->GetTextValue());
1810 // Gets refreshed anyway in MSW
1815 wxClientDC
dc(this);
1817 DrawCellBackground(& dc
, &m_currentRect
, row
, col
);
1818 DrawCellValue(& dc
, &m_currentRect
, row
, col
);
1824 wxString
& wxGenericGrid::GetCellValue(int row
, int col
) const
1826 static wxString
emptyString("");
1828 wxGridCell
*cell
= GetCell(row
, col
);
1830 return cell
->GetTextValue();
1835 void wxGenericGrid::SetColumnWidth(int col
, int width
)
1837 if (col
<= m_totalCols
)
1838 m_colWidths
[col
] = width
;
1841 int wxGenericGrid::GetColumnWidth(int col
) const
1843 if (col
<= m_totalCols
)
1844 return m_colWidths
[col
];
1849 void wxGenericGrid::SetRowHeight(int row
, int height
)
1851 if (row
<= m_totalRows
)
1852 m_rowHeights
[row
] = height
;
1855 int wxGenericGrid::GetRowHeight(int row
) const
1857 if (row
<= m_totalRows
)
1858 return m_rowHeights
[row
];
1863 void wxGenericGrid::SetLabelSize(int orientation
, int sz
)
1865 if (orientation
== wxHORIZONTAL
)
1866 m_horizontalLabelHeight
= sz
;
1868 m_verticalLabelWidth
= sz
;
1870 SetCurrentRect(GetCursorRow(), GetCursorColumn());
1873 int wxGenericGrid::GetLabelSize(int orientation
) const
1875 if (orientation
== wxHORIZONTAL
)
1876 return m_horizontalLabelHeight
;
1878 return m_verticalLabelWidth
;
1881 wxGridCell
*wxGenericGrid::GetLabelCell(int orientation
, int pos
) const
1883 if (orientation
== wxHORIZONTAL
)
1885 if (m_colLabelCells
&& pos
< m_totalCols
)
1886 return m_colLabelCells
[pos
];
1888 return (wxGridCell
*) NULL
;
1892 if (m_rowLabelCells
&& pos
< m_totalRows
)
1893 return m_rowLabelCells
[pos
];
1895 return (wxGridCell
*) NULL
;
1899 void wxGenericGrid::SetLabelValue(int orientation
, const wxString
& val
, int pos
)
1901 wxGridCell
*cell
= GetLabelCell(orientation
, pos
);
1903 cell
->SetTextValue(val
);
1906 wxString
& wxGenericGrid::GetLabelValue(int orientation
, int pos
) const
1908 static wxString emptyString
= "";
1909 wxGridCell
*cell
= GetLabelCell(orientation
, pos
);
1911 return cell
->GetTextValue();
1916 void wxGenericGrid::SetLabelAlignment(int orientation
, int align
)
1918 if (orientation
== wxHORIZONTAL
)
1919 m_horizontalLabelAlignment
= align
;
1921 m_verticalLabelAlignment
= align
;
1923 SetCurrentRect(GetCursorRow(), GetCursorColumn());
1926 int wxGenericGrid::GetLabelAlignment(int orientation
) const
1928 if (orientation
== wxHORIZONTAL
)
1929 return m_horizontalLabelAlignment
;
1931 return m_verticalLabelAlignment
;
1934 void wxGenericGrid::SetLabelTextColour(const wxColour
& colour
)
1936 m_labelTextColour
= colour
;
1940 void wxGenericGrid::SetLabelBackgroundColour(const wxColour
& colour
)
1942 m_labelBackgroundColour
= colour
;
1943 m_labelBackgroundBrush
= * wxTheBrushList
->FindOrCreateBrush(m_labelBackgroundColour
, wxSOLID
);
1946 void wxGenericGrid::SetEditable(bool edit
)
1951 int controlW
, controlH
;
1952 m_textItem
->GetSize(&controlW
, &controlH
);
1953 m_editControlPosition
.height
= controlH
;
1955 m_topOfSheet
= m_editControlPosition
.x
+ controlH
+ 2;
1958 m_editingPanel
->Show(TRUE
);
1959 m_textItem
->Show(TRUE
);
1960 m_textItem
->SetFocus();
1968 m_textItem
->Show(FALSE
);
1969 m_editingPanel
->Show(FALSE
);
1973 SetCurrentRect(GetCursorRow(), GetCursorColumn());
1976 GetClientSize(&cw
, &ch
);
1981 int m_scrollWidth = 16;
1982 GetClientSize(&cw, &ch);
1985 m_vScrollBar->SetSize(cw - m_scrollWidth, m_topOfSheet,
1986 m_scrollWidth, ch - m_topOfSheet - m_scrollWidth);
1990 void wxGenericGrid::SetCellAlignment(int flag
, int row
, int col
)
1992 wxGridCell
*cell
= GetCell(row
, col
);
1994 cell
->SetAlignment(flag
);
1997 int wxGenericGrid::GetCellAlignment(int row
, int col
) const
1999 wxGridCell
*cell
= GetCell(row
, col
);
2001 return cell
->GetAlignment();
2003 return m_cellAlignment
;
2006 void wxGenericGrid::SetCellAlignment(int flag
)
2008 m_cellAlignment
= flag
;
2010 for (i
= 0; i
< GetRows(); i
++)
2011 for (j
= 0; j
< GetCols(); j
++)
2013 GetCell(i
, j
)->SetAlignment(flag
);
2016 int wxGenericGrid::GetCellAlignment(void) const
2018 return m_cellAlignment
;
2021 void wxGenericGrid::SetCellBackgroundColour(const wxColour
& col
)
2023 m_cellBackgroundColour
= col
;
2025 for (i
= 0; i
< GetRows(); i
++)
2026 for (j
= 0; j
< GetCols(); j
++)
2028 GetCell(i
, j
)->SetBackgroundColour(col
);
2031 void wxGenericGrid::SetCellBackgroundColour(const wxColour
& val
, int row
, int col
)
2033 wxGridCell
*cell
= GetCell(row
, col
);
2036 cell
->SetBackgroundColour(val
);
2037 RefreshCell(row
, col
);
2041 wxColour
& wxGenericGrid::GetCellBackgroundColour(int row
, int col
) const
2043 wxGridCell
*cell
= GetCell(row
, col
);
2045 return cell
->GetBackgroundColour();
2047 return (wxColour
&) m_cellBackgroundColour
;
2050 void wxGenericGrid::SetCellTextColour(const wxColour
& val
, int row
, int col
)
2052 wxGridCell
*cell
= GetCell(row
, col
);
2055 cell
->SetTextColour(val
);
2056 RefreshCell(row
, col
);
2060 void wxGenericGrid::SetCellTextFont(const wxFont
& fnt
, int row
, int col
)
2062 wxGridCell
*cell
= GetCell(row
, col
);
2066 RefreshCell(row
, col
);
2070 wxFont
& wxGenericGrid::GetCellTextFont(int row
, int col
) const
2072 wxGridCell
*cell
= GetCell(row
, col
);
2074 return (wxFont
&) cell
->GetFont();
2076 return (wxFont
&) m_cellTextFont
;
2079 wxColour
& wxGenericGrid::GetCellTextColour(int row
, int col
) const
2081 wxGridCell
*cell
= GetCell(row
, col
);
2083 return (wxColour
&) cell
->GetTextColour();
2085 return (wxColour
&) m_cellTextColour
;
2088 void wxGenericGrid::SetCellTextColour(const wxColour
& val
)
2090 m_cellTextColour
= val
;
2092 for (i
= 0; i
< GetRows(); i
++)
2093 for (j
= 0; j
< GetCols(); j
++)
2095 GetCell(i
, j
)->SetTextColour(val
);
2098 void wxGenericGrid::SetCellTextFont(const wxFont
& fnt
)
2100 m_cellTextFont
= fnt
;
2102 for (i
= 0; i
< GetRows(); i
++)
2103 for (j
= 0; j
< GetCols(); j
++)
2105 GetCell(i
, j
)->SetFont(fnt
);
2108 void wxGenericGrid::SetCellBitmap(wxBitmap
*bitmap
, int row
, int col
)
2110 wxGridCell
*cell
= GetCell(row
, col
);
2113 cell
->SetCellBitmap(bitmap
);
2114 RefreshCell(row
, col
);
2118 wxBitmap
*wxGenericGrid::GetCellBitmap(int row
, int col
) const
2120 wxGridCell
*cell
= GetCell(row
, col
);
2123 return cell
->GetCellBitmap();
2126 return (wxBitmap
*) NULL
;
2129 bool wxGenericGrid::InsertCols(int pos
, int n
, bool updateLabels
)
2131 if (pos
> m_totalCols
)
2135 return CreateGrid(1, n
);
2140 for (i
= 0; i
< m_totalRows
; i
++)
2142 wxGridCell
**cols
= m_gridCells
[i
];
2143 wxGridCell
**newCols
= new wxGridCell
*[m_totalCols
+ n
];
2144 for (j
= 0; j
< pos
; j
++)
2145 newCols
[j
] = cols
[j
];
2146 for (j
= pos
; j
< pos
+ n
; j
++)
2147 newCols
[j
] = new wxGridCell(this);
2148 for (j
= pos
+ n
; j
< m_totalCols
+ n
; j
++)
2149 newCols
[j
] = cols
[j
- n
];
2152 m_gridCells
[i
] = newCols
;
2156 short *newColWidths
= new short[m_totalCols
+ n
];
2157 for (j
= 0; j
< pos
; j
++)
2158 newColWidths
[j
] = m_colWidths
[j
];
2159 for (j
= pos
; j
< pos
+ n
; j
++)
2160 newColWidths
[j
] = wxGRID_DEFAULT_CELL_WIDTH
;
2161 for (j
= pos
+ n
; j
< m_totalCols
+ n
; j
++)
2162 newColWidths
[j
] = m_colWidths
[j
- n
];
2163 delete[] m_colWidths
;
2164 m_colWidths
= newColWidths
;
2167 wxGridCell
**newLabels
= new wxGridCell
*[m_totalCols
+ n
];
2168 for (j
= 0; j
< pos
; j
++)
2169 newLabels
[j
] = m_colLabelCells
[j
];
2170 for (j
= pos
; j
< pos
+ n
; j
++)
2171 newLabels
[j
] = new wxGridCell(this);
2172 for (j
= pos
+ n
; j
< m_totalCols
+ n
; j
++)
2173 newLabels
[j
] = m_colLabelCells
[j
- n
];
2175 delete[] m_colLabelCells
;
2176 m_colLabelCells
= newLabels
;
2182 wxGridEvent
g_evt(GetId(), wxEVT_GRID_CHANGE_LABELS
, this);
2183 GetEventHandler()->ProcessEvent(g_evt
);
2192 bool wxGenericGrid::InsertRows(int pos
, int n
, bool updateLabels
)
2194 if (pos
> m_totalRows
)
2198 return CreateGrid(n
, 1);
2203 wxGridCell
***rows
= new wxGridCell
**[m_totalRows
+ n
];
2206 for (i
= 0; i
< pos
; i
++)
2207 rows
[i
] = m_gridCells
[i
];
2209 for (i
= pos
; i
< pos
+ n
; i
++)
2211 rows
[i
] = new wxGridCell
*[m_totalCols
];
2212 for (j
= 0; j
< m_totalCols
; j
++)
2213 rows
[i
][j
] = new wxGridCell(this);
2216 for (i
= pos
+ n
; i
< m_totalRows
+ n
; i
++)
2217 rows
[i
] = m_gridCells
[i
- n
];
2219 delete[] m_gridCells
;
2223 short *newRowHeights
= new short[m_totalRows
+ n
];
2224 for (i
= 0; i
< pos
; i
++)
2225 newRowHeights
[i
] = m_rowHeights
[i
];
2226 for (i
= pos
; i
< pos
+ n
; i
++)
2227 newRowHeights
[i
] = wxGRID_DEFAULT_CELL_HEIGHT
;
2228 for (i
= pos
+ n
; i
< m_totalRows
+ n
; i
++)
2229 newRowHeights
[i
] = m_rowHeights
[i
- n
];
2230 delete[] m_rowHeights
;
2231 m_rowHeights
= newRowHeights
;
2234 wxGridCell
**newLabels
= new wxGridCell
*[m_totalRows
+ n
];
2235 for (i
= 0; i
< pos
; i
++)
2236 newLabels
[i
] = m_rowLabelCells
[i
];
2237 for (i
= pos
; i
< pos
+ n
; i
++)
2238 newLabels
[i
] = new wxGridCell(this);
2239 for (i
= pos
+ n
; i
< m_totalRows
+ n
; i
++)
2240 newLabels
[i
] = m_rowLabelCells
[i
- n
];
2242 delete[] m_rowLabelCells
;
2243 m_rowLabelCells
= newLabels
;
2249 wxGridEvent
g_evt(GetId(), wxEVT_GRID_CHANGE_LABELS
, this);
2250 GetEventHandler()->ProcessEvent(g_evt
);
2259 bool wxGenericGrid::AppendCols(int n
, bool updateLabels
)
2261 return InsertCols(GetCols(), n
, updateLabels
);
2264 bool wxGenericGrid::AppendRows(int n
, bool updateLabels
)
2266 return InsertRows(GetRows(), n
, updateLabels
);
2269 bool wxGenericGrid::DeleteRows(int pos
, int n
, bool updateLabels
)
2271 if (pos
> m_totalRows
)
2278 wxGridCell
***rows
= new wxGridCell
**[m_totalRows
- n
];
2281 for (i
= 0; i
< pos
; i
++)
2282 rows
[i
] = m_gridCells
[i
];
2284 for (i
= pos
+ n
; i
< m_totalRows
; i
++)
2285 rows
[i
-n
] = m_gridCells
[i
];
2287 delete[] m_gridCells
;
2291 short *newRowHeights
= new short[m_totalRows
- n
];
2292 for (i
= 0; i
< pos
; i
++)
2293 newRowHeights
[i
] = m_rowHeights
[i
];
2294 for (i
= pos
+ n
; i
< m_totalRows
; i
++)
2295 newRowHeights
[i
-n
] = m_rowHeights
[i
];
2296 delete[] m_rowHeights
;
2297 m_rowHeights
= newRowHeights
;
2300 wxGridCell
**newLabels
= new wxGridCell
*[m_totalRows
- n
];
2301 for (i
= 0; i
< pos
; i
++)
2302 newLabels
[i
] = m_rowLabelCells
[i
];
2303 for (i
= pos
+ n
; i
< m_totalRows
; i
++)
2304 newLabels
[i
-n
] = m_rowLabelCells
[i
];
2306 delete[] m_rowLabelCells
;
2307 m_rowLabelCells
= newLabels
;
2313 wxGridEvent
g_evt(GetId(), wxEVT_GRID_CHANGE_LABELS
, this);
2314 GetEventHandler()->ProcessEvent(g_evt
);
2321 bool wxGenericGrid::DeleteCols(int pos
, int n
, bool updateLabels
)
2323 if (pos
+ n
> m_totalCols
)
2331 for (i
= 0; i
< m_totalRows
; i
++)
2333 wxGridCell
**cols
= m_gridCells
[i
];
2334 wxGridCell
**newCols
= new wxGridCell
*[m_totalCols
- n
];
2335 for (j
= 0; j
< pos
; j
++)
2336 newCols
[j
] = cols
[j
];
2337 for (j
= pos
; j
< pos
+ n
; j
++)
2339 for (j
= pos
+ n
; j
< m_totalCols
; j
++)
2340 newCols
[j
-n
] = cols
[j
];
2343 m_gridCells
[i
] = newCols
;
2347 short *newColWidths
= new short[m_totalCols
- n
];
2348 for (j
= 0; j
< pos
; j
++)
2349 newColWidths
[j
] = m_colWidths
[j
];
2350 for (j
= pos
+ n
; j
< m_totalCols
; j
++)
2351 newColWidths
[j
-n
] = m_colWidths
[j
];
2352 delete[] m_colWidths
;
2353 m_colWidths
= newColWidths
;
2356 wxGridCell
**newLabels
= new wxGridCell
*[m_totalCols
- n
];
2357 for (j
= 0; j
< pos
; j
++)
2358 newLabels
[j
] = m_colLabelCells
[j
];
2359 for (j
= pos
+ n
; j
< m_totalCols
; j
++)
2360 newLabels
[j
-n
] = m_colLabelCells
[j
];
2362 delete[] m_colLabelCells
;
2363 m_colLabelCells
= newLabels
;
2369 wxGridEvent
g_evt(GetId(), wxEVT_GRID_CHANGE_LABELS
, this);
2370 GetEventHandler()->ProcessEvent(g_evt
);
2377 void wxGenericGrid::SetGridCursor(int row
, int col
)
2379 if (row
>= m_totalRows
|| col
>= m_totalCols
)
2382 if (row
== GetCursorRow() && col
== GetCursorColumn())
2385 wxClientDC
dc(this);
2388 SetGridClippingRegion(& dc
);
2390 if (m_currentRectVisible
)
2391 HighlightCell(& dc
);
2394 m_wCursorColumn
= col
;
2397 GetClientSize(&cw
, &ch
);
2399 SetCurrentRect(row
, col
, cw
, ch
);
2401 if (m_currentRectVisible
)
2402 HighlightCell(& dc
);
2404 dc
.DestroyClippingRegion();
2412 wxGridCell::wxGridCell(wxGenericGrid
*window
)
2414 cellBitmap
= (wxBitmap
*) NULL
;
2416 backgroundBrush
= wxNullBrush
;
2418 textColour
= window
->GetCellTextColour();
2420 textColour
.Set(0,0,0);
2422 backgroundColour
= window
->GetCellBackgroundColour();
2424 backgroundColour
.Set(255,255,255);
2427 font
= window
->GetCellTextFont();
2429 font
= * wxTheFontList
->FindOrCreateFont(12, wxSWISS
, wxNORMAL
, wxNORMAL
);
2431 SetBackgroundColour(backgroundColour
);
2434 alignment
= window
->GetCellAlignment();
2439 wxGridCell::~wxGridCell(void)
2443 void wxGridCell::SetBackgroundColour(const wxColour
& colour
)
2445 backgroundColour
= colour
;
2446 backgroundBrush
= * wxTheBrushList
->FindOrCreateBrush(backgroundColour
, wxSOLID
);
2449 void wxGenericGrid::OnText(wxCommandEvent
& WXUNUSED(ev
) )
2451 wxGenericGrid
*grid
= this;
2452 wxGridCell
*cell
= grid
->GetCell(grid
->GetCursorRow(), grid
->GetCursorColumn());
2453 if (cell
&& grid
->CurrentCellVisible())
2455 cell
->SetTextValue(grid
->GetTextItem()->GetValue());
2456 wxClientDC
dc(grid
);
2459 grid
->SetGridClippingRegion(& dc
);
2460 grid
->DrawCellBackground(& dc
, &grid
->GetCurrentRect(), grid
->GetCursorRow(), grid
->GetCursorColumn());
2461 grid
->DrawCellValue(& dc
, &grid
->GetCurrentRect(), grid
->GetCursorRow(), grid
->GetCursorColumn());
2462 grid
->HighlightCell(& dc
);
2463 dc
.DestroyClippingRegion();
2466 //grid->OnCellChange(grid->GetCursorRow(), grid->GetCursorColumn());
2467 wxGridEvent
g_evt(GetId(), wxEVT_GRID_CELL_CHANGE
, grid
,
2468 grid
->GetCursorRow(), grid
->GetCursorColumn());
2469 GetEventHandler()->ProcessEvent(g_evt
);
2471 // grid->DrawCellText();
2475 void wxGenericGrid::OnGridScroll(wxScrollEvent
& ev
)
2477 static bool inScroll
= FALSE
;
2483 wxGenericGrid
*win
= this;
2485 bool change
= FALSE
;
2487 if (ev
.GetEventObject() == win
->GetHorizScrollBar())
2489 change
= (ev
.GetPosition() != m_scrollPosX
);
2490 win
->SetScrollPosX(ev
.GetPosition());
2494 change
= (ev
.GetPosition() != m_scrollPosY
);
2495 win
->SetScrollPosY(ev
.GetPosition());
2498 win
->UpdateDimensions();
2500 win
->SetCurrentRect(win
->GetCursorRow(), win
->GetCursorColumn());
2502 // Because rows and columns can be arbitrary sizes,
2503 // the scrollbars will need to be adjusted to reflect the
2507 if (change
) win
->Refresh(FALSE
);
2513 //----------------------------------------------------------------------
2514 // Default wxGridEvent handlers
2515 // (just redirect to the pre-existing virtual methods)
2517 void wxGenericGrid::_OnSelectCell(wxGridEvent
& ev
)
2519 OnSelectCell(ev
.m_row
, ev
.m_col
);
2522 void wxGenericGrid::_OnCreateCell(wxGridEvent
& ev
)
2524 ev
.m_cell
= OnCreateCell();
2527 void wxGenericGrid::_OnChangeLabels(wxGridEvent
& WXUNUSED(ev
))
2532 void wxGenericGrid::_OnChangeSelectionLabel(wxGridEvent
& WXUNUSED(ev
))
2534 OnChangeSelectionLabel();
2537 void wxGenericGrid::_OnCellChange(wxGridEvent
& ev
)
2539 OnCellChange(ev
.m_row
, ev
.m_col
);
2542 void wxGenericGrid::_OnCellLeftClick(wxGridEvent
& ev
)
2544 OnCellLeftClick(ev
.m_row
, ev
.m_col
, ev
.m_x
, ev
.m_y
, ev
.m_control
, ev
.m_shift
);
2547 void wxGenericGrid::_OnCellRightClick(wxGridEvent
& ev
)
2549 OnCellRightClick(ev
.m_row
, ev
.m_col
, ev
.m_x
, ev
.m_y
, ev
.m_control
, ev
.m_shift
);
2552 void wxGenericGrid::_OnLabelLeftClick(wxGridEvent
& ev
)
2554 OnLabelLeftClick(ev
.m_row
, ev
.m_col
, ev
.m_x
, ev
.m_y
, ev
.m_control
, ev
.m_shift
);
2557 void wxGenericGrid::_OnLabelRightClick(wxGridEvent
& ev
)
2559 OnLabelRightClick(ev
.m_row
, ev
.m_col
, ev
.m_x
, ev
.m_y
, ev
.m_control
, ev
.m_shift
);