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
);
225 // SetSize(pos.x, pos.y, size.x, size.y);
230 wxGenericGrid::~wxGenericGrid(void)
235 void wxGenericGrid::ClearGrid(void)
240 for (i
= 0; i
< m_totalRows
; i
++)
242 for (j
= 0; j
< m_totalCols
; j
++)
243 if (m_gridCells
[i
][j
])
244 delete m_gridCells
[i
][j
];
245 delete[] m_gridCells
[i
];
247 delete[] m_gridCells
;
248 m_gridCells
= (wxGridCell
***) NULL
;
251 delete[] m_colWidths
;
252 m_colWidths
= (short *) NULL
;
254 delete[] m_rowHeights
;
255 m_rowHeights
= (short *) NULL
;
259 for (i
= 0; i
< m_totalRows
; i
++)
260 delete m_rowLabelCells
[i
];
261 delete[] m_rowLabelCells
;
262 m_rowLabelCells
= (wxGridCell
**) NULL
;
266 for (i
= 0; i
< m_totalCols
; i
++)
267 delete m_colLabelCells
[i
];
268 delete[] m_colLabelCells
;
269 m_colLabelCells
= (wxGridCell
**) NULL
;
271 if (m_doubleBufferingBitmap
)
273 delete m_doubleBufferingBitmap
;
274 m_doubleBufferingBitmap
= (wxBitmap
*) NULL
;
278 bool wxGenericGrid::CreateGrid(int nRows
, int nCols
, wxString
**cellValues
, short *widths
,
279 short defaultWidth
, short defaultHeight
)
285 m_colWidths
= new short[nCols
];
286 m_rowHeights
= new short[nRows
];
287 for (i
= 0; i
< nCols
; i
++)
289 m_colWidths
[i
] = widths
[i
];
291 m_colWidths
[i
] = defaultWidth
;
292 for (i
= 0; i
< nRows
; i
++)
293 m_rowHeights
[i
] = defaultHeight
;
295 m_gridCells
= new wxGridCell
**[nRows
];
297 for (i
= 0; i
< nRows
; i
++)
298 m_gridCells
[i
] = new wxGridCell
*[nCols
];
300 for (i
= 0; i
< nRows
; i
++)
301 for (j
= 0; j
< nCols
; j
++)
304 //m_gridCells[i][j] = OnCreateCell();
305 wxGridEvent
g_evt(GetId(), wxEVT_GRID_CREATE_CELL
, this, i
, j
);
306 GetEventHandler()->ProcessEvent(g_evt
);
307 m_gridCells
[i
][j
] = g_evt
.m_cell
;
308 m_gridCells
[i
][j
]->SetTextValue(cellValues
[i
][j
]);
311 m_gridCells
[i
][j
] = (wxGridCell
*) NULL
;
313 m_rowLabelCells
= new wxGridCell
*[nRows
];
314 for (i
= 0; i
< nRows
; i
++)
315 m_rowLabelCells
[i
] = new wxGridCell(this);
316 m_colLabelCells
= new wxGridCell
*[nCols
];
317 for (i
= 0; i
< nCols
; i
++)
318 m_colLabelCells
[i
] = new wxGridCell(this);
320 m_wCursorRow
= m_wCursorColumn
= 0;
321 SetCurrentRect(0, 0);
323 // Need to determine various dimensions
327 int objectSizeX
= m_totalCols
;
329 int viewLengthX
= m_totalCols
;
332 m_hScrollBar->SetViewLength(viewLengthX);
333 m_hScrollBar->SetObjectLength(objectSizeX);
334 m_hScrollBar->SetPageSize(pageSizeX);
336 m_hScrollBar
->SetScrollbar(m_hScrollBar
->GetThumbPosition(), pageSizeX
, objectSizeX
, viewLengthX
);
338 int objectSizeY
= m_totalRows
;
340 int viewLengthY
= m_totalRows
;
343 m_vScrollBar->SetViewLength(viewLengthY);
344 m_vScrollBar->SetObjectLength(objectSizeY);
345 m_vScrollBar->SetPageSize(pageSizeY);
348 m_vScrollBar
->SetScrollbar(m_vScrollBar
->GetThumbPosition(), pageSizeY
, objectSizeY
, viewLengthY
);
353 wxGridEvent
g_evt(GetId(), wxEVT_GRID_CHANGE_LABELS
, this);
354 GetEventHandler()->ProcessEvent(g_evt
);
356 //OnChangeSelectionLabel();
357 wxGridEvent
g_evt2(GetId(), wxEVT_GRID_CHANGE_SEL_LABEL
, this);
358 GetEventHandler()->ProcessEvent(g_evt2
);
363 // Need to determine various dimensions
364 void wxGenericGrid::UpdateDimensions(void)
366 int canvasWidth
, canvasHeight
;
367 GetSize(&canvasWidth
, &canvasHeight
);
369 if (m_editCreated
&& m_editable
)
371 int controlW
, controlH
;
372 GetTextItem()->GetSize(&controlW
, &controlH
);
373 m_topOfSheet
= m_editControlPosition
.y
+ controlH
+ 2;
377 m_rightOfSheet
= m_leftOfSheet
+ m_verticalLabelWidth
;
379 for (i
= m_scrollPosX
; i
< m_totalCols
; i
++)
381 if (m_rightOfSheet
> canvasWidth
)
384 m_rightOfSheet
+= m_colWidths
[i
];
386 m_bottomOfSheet
= m_topOfSheet
+ m_horizontalLabelHeight
;
387 for (i
= m_scrollPosY
; i
< m_totalRows
; i
++)
389 if (m_bottomOfSheet
> canvasHeight
)
392 m_bottomOfSheet
+= m_rowHeights
[i
];
395 m_totalGridWidth
= m_leftOfSheet
+ m_verticalLabelWidth
;
396 for (i
= 0; i
< m_totalCols
; i
++)
398 m_totalGridWidth
+= m_colWidths
[i
];
400 m_totalGridHeight
= m_topOfSheet
+ m_horizontalLabelHeight
;
401 for (i
= 0; i
< m_totalRows
; i
++)
403 m_totalGridHeight
+= m_rowHeights
[i
];
407 wxGridCell
*wxGenericGrid::GetCell(int row
, int col
) const
410 return (wxGridCell
*) NULL
;
412 if ((row
>= m_totalRows
) || (col
>= m_totalCols
))
413 return (wxGridCell
*) NULL
;
415 wxGridCell
*cell
= m_gridCells
[row
][col
];
418 // m_gridCells[row][col] = OnCreateCell();
419 wxGridEvent
g_evt(GetId(), wxEVT_GRID_CREATE_CELL
, (wxGenericGrid
*) this, row
, col
);
420 GetEventHandler()->ProcessEvent(g_evt
);
421 m_gridCells
[row
][col
] = g_evt
.m_cell
;
422 return m_gridCells
[row
][col
];
428 void wxGenericGrid::SetGridClippingRegion(wxDC
*dc
)
430 int m_scrollWidthHoriz
= 0;
431 int m_scrollWidthVert
= 0;
433 GetClientSize(&cw
, &ch
);
435 if (m_hScrollBar
&& m_hScrollBar
->IsShown())
436 m_scrollWidthHoriz
= m_scrollWidth
;
437 if (m_vScrollBar
&& m_vScrollBar
->IsShown())
438 m_scrollWidthVert
= m_scrollWidth
;
440 // Don't paint over the scrollbars
441 dc
->SetClippingRegion(m_leftOfSheet
, m_topOfSheet
,
442 cw
- m_scrollWidthVert
- m_leftOfSheet
, ch
- m_scrollWidthHoriz
- m_topOfSheet
);
445 void wxGenericGrid::OnPaint(wxPaintEvent
& WXUNUSED(event
))
448 GetClientSize(&w
, &h
);
450 bool useDoubleBuffering
= (bool) wxUSE_DOUBLE_BUFFERING
;
451 if (useDoubleBuffering
)
453 // Reuse the old bitmap if possible
455 if (!m_doubleBufferingBitmap
||
456 (m_doubleBufferingBitmap
->GetWidth() < w
|| m_doubleBufferingBitmap
->GetHeight() < h
))
458 if (m_doubleBufferingBitmap
)
459 delete m_doubleBufferingBitmap
;
460 m_doubleBufferingBitmap
= new wxBitmap(w
, h
);
462 if (!m_doubleBufferingBitmap
|| !m_doubleBufferingBitmap
->Ok())
464 // If we couldn't create a new bitmap, perhaps because resources were low,
465 // then don't complain, just don't double-buffer
466 if (m_doubleBufferingBitmap
)
467 delete m_doubleBufferingBitmap
;
468 m_doubleBufferingBitmap
= (wxBitmap
*) NULL
;
469 useDoubleBuffering
= FALSE
;
473 if (useDoubleBuffering
)
475 wxPaintDC
paintDC(this);
476 wxMemoryDC
dc(& paintDC
);
477 dc
.SelectObject(* m_doubleBufferingBitmap
);
481 int vertScrollBarWidth
= m_scrollWidth
;
482 int horizScrollBarHeight
= m_scrollWidth
;
483 if (m_vScrollBar
&& !m_vScrollBar
->IsShown())
484 vertScrollBarWidth
= 0;
485 if (m_hScrollBar
&& !m_hScrollBar
->IsShown())
486 horizScrollBarHeight
= 0;
488 paintDC
.Blit(m_leftOfSheet
, m_topOfSheet
, w
- vertScrollBarWidth
- m_leftOfSheet
, h
- horizScrollBarHeight
- m_topOfSheet
,
489 &dc
, m_leftOfSheet
, m_topOfSheet
, wxCOPY
);
491 dc
.SelectObject(wxNullBitmap
);
500 void wxGenericGrid::PaintGrid(wxDC
& dc
)
503 dc
.SetOptimization(FALSE
);
505 SetGridClippingRegion(& dc
);
507 DrawLabelAreas(& dc
);
509 DrawEditableArea(& dc
);
510 DrawColumnLabels(& dc
);
515 /* Hilight present cell */
516 SetCurrentRect(m_wCursorRow
, m_wCursorColumn
);
517 if (m_currentRectVisible
)
520 dc
.DestroyClippingRegion();
521 dc
.SetOptimization(TRUE
);
525 // Erase (some of) the background.
526 // Currently, a Windows-only optimisation.
527 void wxGenericGrid::OnEraseBackground(wxEraseEvent
& WXUNUSED(event
) )
531 dc
.SetOptimization(FALSE
);
534 GetClientSize(& w
, & h
);
535 dc
.SetBrush(*wxLIGHT_GREY_BRUSH
);
536 dc
.SetPen(*wxLIGHT_GREY_PEN
);
538 if (m_hScrollBar
&& m_hScrollBar
->IsShown() && m_vScrollBar
&& m_vScrollBar
->IsShown())
540 dc
.DrawRectangle(w
- m_scrollWidth
, h
- m_scrollWidth
, m_scrollWidth
, m_scrollWidth
);
543 dc
.SetOptimization(TRUE
);
548 void wxGenericGrid::DrawLabelAreas(wxDC
*dc
)
551 GetClientSize(&cw
, &ch
);
553 dc
->SetPen(*wxTRANSPARENT_PEN
);
554 // dc->SetBrush(*dc->GetBackground());
556 // Should blank out any area which isn't going to be painted over.
557 // dc->DrawRectangle(m_leftOfSheet, m_bottomOfSheet, cw - m_leftOfSheet, ch - m_bottomOfSheet);
558 // dc->DrawRectangle(m_rightOfSheet, m_topOfSheet, cw - m_rightOfSheet, ch - m_topOfSheet);
560 // Paint the label areas
561 dc
->SetBrush(m_labelBackgroundBrush
);
562 // dc->DrawRectangle(m_leftOfSheet, m_topOfSheet, m_rightOfSheet - m_leftOfSheet + 1, m_horizontalLabelHeight + 1);
563 dc
->DrawRectangle(m_leftOfSheet
, m_topOfSheet
, cw
-m_leftOfSheet
, m_horizontalLabelHeight
+ 1);
564 // dc->DrawRectangle(m_leftOfSheet, m_topOfSheet, m_verticalLabelWidth + 1, m_bottomOfSheet - m_topOfSheet + 1);
565 dc
->DrawRectangle(m_leftOfSheet
, m_topOfSheet
, m_verticalLabelWidth
+ 1, ch
-m_topOfSheet
);
568 void wxGenericGrid::DrawEditableArea(wxDC
*dc
)
571 GetClientSize(&cw
, &ch
);
573 dc
->SetPen(*wxTRANSPARENT_PEN
);
574 dc
->SetBrush(*wxTheBrushList
->FindOrCreateBrush(m_cellBackgroundColour
, wxSOLID
));
575 // dc->DrawRectangle(m_leftOfSheet+m_verticalLabelWidth, m_topOfSheet+m_horizontalLabelHeight,
576 // m_rightOfSheet-(m_leftOfSheet+m_verticalLabelWidth) + 1, m_bottomOfSheet - (m_topOfSheet+m_horizontalLabelHeight) + 1);
577 dc
->DrawRectangle(m_leftOfSheet
+m_verticalLabelWidth
, m_topOfSheet
+m_horizontalLabelHeight
,
578 cw
-(m_leftOfSheet
+m_verticalLabelWidth
), ch
- (m_topOfSheet
+m_horizontalLabelHeight
));
581 void wxGenericGrid::DrawGridLines(wxDC
*dc
)
584 GetClientSize(&cw
, &ch
);
588 if (m_divisionPen
.Ok())
590 dc
->SetPen(m_divisionPen
);
592 int heightCount
= m_topOfSheet
+ m_horizontalLabelHeight
;
594 // Draw horizontal grey lines for cells
595 for (i
= m_scrollPosY
; i
< (m_totalRows
+1); i
++)
597 if (heightCount
> ch
)
601 dc
->DrawLine(m_leftOfSheet
, heightCount
,
604 heightCount
+= m_rowHeights
[i
];
609 if (m_verticalLabelWidth
> 0)
611 dc
->SetPen(*wxBLACK_PEN
);
613 // Draw horizontal black lines for row labels
614 int heightCount
= m_topOfSheet
+ m_horizontalLabelHeight
;
615 for (i
= m_scrollPosY
; i
< (m_totalRows
+1); i
++)
617 if (heightCount
> ch
)
621 dc
->DrawLine(m_leftOfSheet
, heightCount
,
622 m_verticalLabelWidth
, heightCount
);
624 heightCount
+= m_rowHeights
[i
];
627 // Draw a black vertical line for row number cells
628 dc
->DrawLine(m_leftOfSheet
+ m_verticalLabelWidth
, m_topOfSheet
,
629 m_leftOfSheet
+ m_verticalLabelWidth
, ch
);
630 // First vertical line
631 dc
->DrawLine(m_leftOfSheet
, m_topOfSheet
, m_leftOfSheet
, ch
);
633 dc
->SetPen(*wxWHITE_PEN
);
635 // Draw highlights on row labels
636 heightCount
= m_topOfSheet
+ m_horizontalLabelHeight
;
637 for (i
= m_scrollPosY
; i
< m_totalRows
; i
++)
639 if (heightCount
> ch
)
643 dc
->DrawLine(m_leftOfSheet
+1, heightCount
+1,
644 m_verticalLabelWidth
, heightCount
+1);
645 dc
->DrawLine(m_leftOfSheet
+1, heightCount
+1,
646 m_leftOfSheet
+1, heightCount
+ m_rowHeights
[i
] - 1);
647 heightCount
+= m_rowHeights
[i
];
650 // Last one - down to the floor.
651 dc
->DrawLine(m_leftOfSheet
+1, heightCount
+1,
652 m_verticalLabelWidth
, heightCount
+1);
653 dc
->DrawLine(m_leftOfSheet
+1, heightCount
+1,
654 m_leftOfSheet
+1, ch
);
658 if (m_divisionPen
.Ok())
660 dc
->SetPen(m_divisionPen
);
662 // Draw vertical grey lines for cells
663 int widthCount
= m_leftOfSheet
+ m_verticalLabelWidth
;
664 for (i
= m_scrollPosX
; i
< m_totalCols
; i
++)
670 // Skip the first one
671 if (i
!= m_scrollPosX
)
673 dc
->DrawLine(widthCount
, m_topOfSheet
+ m_horizontalLabelHeight
,
674 widthCount
, m_bottomOfSheet
);
676 widthCount
+= m_colWidths
[i
];
680 dc
->DrawLine(widthCount
, m_topOfSheet
+ m_horizontalLabelHeight
,
681 widthCount
, m_bottomOfSheet
);
684 dc
->SetPen(*wxBLACK_PEN
);
686 // Draw two black horizontal lines for column number cells
688 m_leftOfSheet
, m_topOfSheet
,
690 dc
->DrawLine(m_leftOfSheet
, m_topOfSheet
+ m_horizontalLabelHeight
,
691 cw
, m_topOfSheet
+ m_horizontalLabelHeight
);
693 if (m_horizontalLabelHeight
> 0)
695 int widthCount
= m_leftOfSheet
+ m_verticalLabelWidth
;
697 // Draw black vertical lines for column number cells
698 for (i
= m_scrollPosX
; i
< m_totalCols
; i
++)
704 dc
->DrawLine(widthCount
, m_topOfSheet
,
705 widthCount
, m_topOfSheet
+ m_horizontalLabelHeight
);
706 widthCount
+= m_colWidths
[i
];
711 dc
->DrawLine(widthCount
, m_topOfSheet
,
712 widthCount
, m_topOfSheet
+ m_horizontalLabelHeight
);
715 dc
->SetPen(*wxWHITE_PEN
);
716 widthCount
= m_leftOfSheet
+ m_verticalLabelWidth
;
718 for (i
= m_scrollPosX
; i
< m_totalCols
; i
++)
724 dc
->DrawLine(widthCount
+1, m_topOfSheet
+1,
725 widthCount
+m_colWidths
[i
], m_topOfSheet
+1);
726 dc
->DrawLine(widthCount
+1, m_topOfSheet
+1,
727 widthCount
+1, m_topOfSheet
+m_horizontalLabelHeight
);
728 widthCount
+= m_colWidths
[i
];
731 // Last one - to the right side of the canvas.
732 dc
->DrawLine(widthCount
+1, m_topOfSheet
+1,
734 dc
->DrawLine(widthCount
+1, m_topOfSheet
+1,
735 widthCount
+1, m_topOfSheet
+m_horizontalLabelHeight
);
740 void wxGenericGrid::DrawColumnLabels(wxDC
*dc
)
743 GetClientSize(&cw
, &ch
);
745 if (m_horizontalLabelHeight
== 0)
751 // Draw letters for columns
752 rect
.y
= m_topOfSheet
+ 1;
753 rect
.height
= m_horizontalLabelHeight
- 1;
755 dc
->SetTextBackground(m_labelBackgroundColour
);
756 dc
->SetBackgroundMode(wxTRANSPARENT
);
757 // dc->SetTextForeground(m_labelTextColour);
759 int widthCount
= m_leftOfSheet
+ m_verticalLabelWidth
;
760 for (i
= m_scrollPosX
; i
< m_totalCols
; i
++)
766 rect
.x
= 1 + widthCount
;
767 rect
.width
= m_colWidths
[i
];
768 DrawColumnLabel(dc
, &rect
, i
);
770 widthCount
+= m_colWidths
[i
];
775 void wxGenericGrid::DrawColumnLabel(wxDC
*dc
, wxRect
*rect
, int col
)
777 wxGridCell
*cell
= GetLabelCell(wxHORIZONTAL
, col
);
786 dc
->SetTextForeground(GetLabelTextColour());
787 dc
->SetFont(GetLabelTextFont());
788 if ( !cell
->GetTextValue().IsNull() )
789 DrawTextRect(dc
, cell
->GetTextValue(), &rect2
, GetLabelAlignment(wxHORIZONTAL
));
793 void wxGenericGrid::DrawRowLabels(wxDC
*dc
)
796 GetClientSize(&cw
, &ch
);
798 if (m_verticalLabelWidth
== 0)
804 // Draw numbers for rows
805 rect
.x
= m_leftOfSheet
;
806 rect
.width
= m_verticalLabelWidth
;
808 int heightCount
= m_topOfSheet
+ m_horizontalLabelHeight
;
810 dc
->SetTextBackground(m_labelBackgroundColour
);
811 dc
->SetBackgroundMode(wxTRANSPARENT
);
813 for (i
= m_scrollPosY
; i
< m_totalRows
; i
++)
815 if (heightCount
> ch
)
819 rect
.y
= 1 + heightCount
;
820 rect
.height
= m_rowHeights
[i
];
821 DrawRowLabel(dc
, &rect
, i
);
823 heightCount
+= m_rowHeights
[i
];
828 void wxGenericGrid::DrawRowLabel(wxDC
*dc
, wxRect
*rect
, int row
)
830 wxGridCell
*cell
= GetLabelCell(wxVERTICAL
, row
);
839 dc
->SetTextForeground(GetLabelTextColour());
840 dc
->SetFont(GetLabelTextFont());
841 if ( !cell
->GetTextValue().IsNull() )
842 DrawTextRect(dc
, cell
->GetTextValue(), &rect2
, GetLabelAlignment(wxVERTICAL
));
846 void wxGenericGrid::DrawCells(wxDC
*dc
)
849 GetClientSize(&cw
, &ch
);
853 // Draw value corresponding to each cell
854 for (i
= m_scrollPosY
; i
< m_totalRows
; i
++)
856 for (j
= m_scrollPosX
; j
< m_totalCols
; j
++)
858 SetCurrentRect(i
, j
, cw
, ch
);
859 if (m_currentRectVisible
)
861 DrawCellBackground(dc
, &m_currentRect
, i
, j
);
862 DrawCellValue(dc
, &m_currentRect
, i
, j
);
864 if (m_currentRect
.x
> cw
)
867 if (m_currentRect
.y
> ch
)
870 dc
->SetBackgroundMode(wxSOLID
);
871 dc
->SetPen(*wxBLACK_PEN
);
874 void wxGenericGrid::DrawCellBackground(wxDC
*dc
, wxRect
*rect
, int row
, int col
)
876 wxGridCell
*cell
= GetCell(row
, col
);
879 dc
->SetBrush(cell
->GetBackgroundBrush());
880 dc
->SetPen(*wxTRANSPARENT_PEN
);
882 #if 0 // In wxWin 2.0 the dc code is exact. RR.
884 dc
->DrawRectangle(rect
->x
+1, rect
->y
+1, rect
->width
-1, rect
->height
-1);
886 dc
->DrawRectangle(rect
->x
+1, rect
->y
+1, rect
->width
, rect
->height
);
890 dc
->DrawRectangle(rect
->x
+1, rect
->y
+1, rect
->width
-1, rect
->height
-1);
892 dc
->SetPen(*wxBLACK_PEN
);
896 void wxGenericGrid::DrawCellValue(wxDC
*dc
, wxRect
*rect
, int row
, int col
)
898 wxGridCell
*cell
= GetCell(row
, col
);
901 wxBitmap
*bitmap
= cell
->GetCellBitmap();
911 DrawBitmapRect(dc
, bitmap
, &rect2
, cell
->GetAlignment());
915 dc
->SetBackgroundMode(wxTRANSPARENT
);
916 dc
->SetTextForeground(cell
->GetTextColour());
917 dc
->SetFont(cell
->GetFont());
919 if ( !cell
->GetTextValue().IsNull() )
920 DrawTextRect(dc
, cell
->GetTextValue(), &rect2
, cell
->GetAlignment());
925 void wxGenericGrid::AdjustScrollbars(void)
928 GetClientSize(&cw
, &ch
);
930 // We find the view size by seeing how many rows/cols fit on
932 // BUT... this means that the scrollbar should be adjusted every time
933 // it's scrolled, as well as when sized, because with variable size rows/cols,
934 // the number of rows/col visible on the view differs according to what bit
935 // you're looking at. The object length is always the same, but the
936 // view length differs.
938 // Since this may not be known until the end of this function, we should probably call AdjustScrollbars
940 int vertScrollBarWidth
= m_scrollWidth
;
941 int horizScrollBarHeight
= m_scrollWidth
;
942 if (m_vScrollBar
&& !m_vScrollBar
->IsShown())
943 vertScrollBarWidth
= 0;
944 if (m_hScrollBar
&& !m_hScrollBar
->IsShown())
945 horizScrollBarHeight
= 0;
947 int noHorizSteps
= 0;
950 if (m_totalGridWidth
+ vertScrollBarWidth
<= cw
)
959 for (i
= m_scrollPosX
; i
< m_totalCols
; i
++)
961 widthCount
+= m_colWidths
[i
];
962 // A partial bit doesn't count, we still have to scroll to see the
964 if (widthCount
+ m_leftOfSheet
+ m_verticalLabelWidth
> (cw
-vertScrollBarWidth
))
973 if (m_totalGridHeight
+ horizScrollBarHeight
<= ch
)
982 for (i
= m_scrollPosY
; i
< m_totalRows
; i
++)
984 heightCount
+= m_rowHeights
[i
];
985 // A partial bit doesn't count, we still have to scroll to see the
987 if (heightCount
+ m_topOfSheet
+ m_horizontalLabelHeight
> (ch
-horizScrollBarHeight
))
996 if (m_totalGridWidth
+ vertScrollBarWidth
<= cw
)
999 m_hScrollBar
->Show(FALSE
);
1005 m_hScrollBar
->Show(TRUE
);
1008 if (m_totalGridHeight
+ horizScrollBarHeight
<= ch
)
1011 m_vScrollBar
->Show(FALSE
);
1017 m_vScrollBar
->Show(TRUE
);
1020 UpdateDimensions(); // Necessary in case m_scrollPosX/Y changed
1022 vertScrollBarWidth
= m_scrollWidth
;
1023 horizScrollBarHeight
= m_scrollWidth
;
1024 if (m_vScrollBar
&& !m_vScrollBar
->IsShown())
1025 vertScrollBarWidth
= 0;
1026 if (m_hScrollBar
&& !m_hScrollBar
->IsShown())
1027 horizScrollBarHeight
= 0;
1029 if (m_hScrollBar
&& m_hScrollBar
->IsShown())
1031 int nCols
= GetCols();
1032 m_hScrollBar
->SetScrollbar(m_hScrollBar
->GetThumbPosition(), wxMax(noHorizSteps
, 1), (noHorizSteps
== 0) ? 1 : nCols
, wxMax(noHorizSteps
, 1));
1035 m_hScrollBar->SetSize(m_leftOfSheet, ch - m_scrollWidth -2, // why -2 ? Robert.
1036 cw - vertScrollBarWidth - m_leftOfSheet, m_scrollWidth);
1038 m_hScrollBar
->SetSize(m_leftOfSheet
, ch
- m_scrollWidth
,
1039 cw
- vertScrollBarWidth
- m_leftOfSheet
, m_scrollWidth
);
1043 if (m_vScrollBar
&& m_vScrollBar
->IsShown())
1045 int nRows
= GetRows();
1047 m_vScrollBar
->SetScrollbar(m_vScrollBar
->GetThumbPosition(), wxMax(noVertSteps
, 1), (noVertSteps
== 0) ? 1 : nRows
, wxMax(noVertSteps
, 1));
1048 m_vScrollBar
->SetSize(cw
- m_scrollWidth
, m_topOfSheet
,
1049 m_scrollWidth
, ch
- m_topOfSheet
- horizScrollBarHeight
);
1053 void wxGenericGrid::OnSize(wxSizeEvent
& WXUNUSED(event
) )
1055 if (!m_vScrollBar
|| !m_hScrollBar
)
1061 GetClientSize(&cw
, &ch
);
1063 if (m_editCreated
&& m_editingPanel
&& GetTextItem() && GetTextItem()->IsShown())
1065 m_editingPanel
->SetSize(0, 0, cw
, m_editControlPosition
.height
+ m_editControlPosition
.y
+ 2);
1066 GetTextItem()->SetSize(m_editControlPosition
.x
, m_editControlPosition
.y
,
1067 cw
- 2*m_editControlPosition
.x
, m_editControlPosition
.height
);
1071 bool wxGenericGrid::CellHitTest(int x
, int y
, int *row
, int *col
)
1073 // Find the selected cell and call OnSelectCell
1074 if (x
>= (m_leftOfSheet
+ m_verticalLabelWidth
) && y
>= (m_topOfSheet
+ m_horizontalLabelHeight
) &&
1075 x
<= m_rightOfSheet
&& y
<= m_bottomOfSheet
)
1077 // Calculate the cell number from x and y
1078 x
-= (m_verticalLabelWidth
+ m_leftOfSheet
);
1079 y
-= (m_topOfSheet
+ m_horizontalLabelHeight
);
1083 // Now we need to do a hit test for which row we're on
1084 int currentHeight
= 0;
1085 for (i
= m_scrollPosY
; i
< m_totalRows
; i
++)
1087 if (y
>= currentHeight
&& y
<= (currentHeight
+ m_rowHeights
[i
]))
1092 currentHeight
+= m_rowHeights
[i
];
1095 // Now we need to do a hit test for which column we're on
1096 int currentWidth
= 0;
1097 for (i
= m_scrollPosX
; i
< m_totalCols
; i
++)
1099 if (x
>= currentWidth
&& x
<= (currentWidth
+ m_colWidths
[i
]))
1104 currentWidth
+= m_colWidths
[i
];
1111 bool wxGenericGrid::LabelSashHitTest(int x
, int y
, int *orientation
, int *rowOrCol
, int *startPos
)
1116 if (x
>= (m_leftOfSheet
+ m_verticalLabelWidth
) && y
>= m_topOfSheet
&&
1117 x
<= m_rightOfSheet
&& y
<= (m_topOfSheet
+ m_horizontalLabelHeight
))
1119 // We may be on a column label sash.
1120 int currentWidth
= m_leftOfSheet
+ m_verticalLabelWidth
;
1121 for (i
= m_scrollPosX
; i
< m_totalCols
; i
++)
1123 if (x
>= (currentWidth
+ m_colWidths
[i
] - tolerance
) && x
<= (currentWidth
+ m_colWidths
[i
] + tolerance
))
1125 *orientation
= wxHORIZONTAL
;
1127 *startPos
= currentWidth
;
1130 currentWidth
+= m_colWidths
[i
];
1134 else if (x
>= m_leftOfSheet
&& y
>= (m_topOfSheet
+ m_horizontalLabelHeight
) &&
1135 x
<= (m_leftOfSheet
+ m_verticalLabelWidth
) && y
<= m_bottomOfSheet
)
1137 // We may be on a row label sash.
1138 int currentHeight
= m_topOfSheet
+ m_horizontalLabelHeight
;
1139 for (i
= m_scrollPosY
; i
< m_totalRows
; i
++)
1141 if (y
>= (currentHeight
+ m_rowHeights
[i
] - tolerance
) && y
<= (currentHeight
+ m_rowHeights
[i
] + tolerance
))
1143 *orientation
= wxVERTICAL
;
1145 *startPos
= currentHeight
;
1148 currentHeight
+= m_rowHeights
[i
];
1155 bool wxGenericGrid::LabelHitTest(int x
, int y
, int *row
, int *col
)
1157 // Find the selected label
1158 if (x
>= m_leftOfSheet
&& y
>= m_topOfSheet
&&
1159 x
<= m_rightOfSheet
&& y
<= m_bottomOfSheet
)
1161 // Calculate the cell number from x and y
1167 // Now we need to do a hit test for which row we're on
1168 int currentHeight
= m_horizontalLabelHeight
;
1169 for (i
= m_scrollPosY
; i
< m_totalRows
; i
++)
1171 if (y
>= currentHeight
&& y
<= (currentHeight
+ m_rowHeights
[i
]))
1176 currentHeight
+= m_rowHeights
[i
];
1178 if (y
>= 0 && y
<= m_horizontalLabelHeight
)
1183 // Now we need to do a hit test for which column we're on
1184 int currentWidth
= m_verticalLabelWidth
;
1185 for (i
= m_scrollPosX
; i
< m_totalCols
; i
++)
1187 if (x
>= currentWidth
&& x
<= (currentWidth
+ m_colWidths
[i
]))
1192 currentWidth
+= m_colWidths
[i
];
1194 if (x
>= 0 && x
<= m_verticalLabelWidth
)
1199 if ((*col
== -1) || (*row
== -1))
1207 void wxGenericGrid::OnMouseEvent(wxMouseEvent
& ev
)
1211 wxClientDC
dc(this);
1215 if (CellHitTest((int)ev
.GetX(), (int)ev
.GetY(), &row
, &col
))
1217 OnSelectCellImplementation(& dc
, row
, col
);
1219 //OnCellLeftClick(row, col, (int)ev.GetX(), (int)ev.GetY(), ev.ControlDown(), ev.ShiftDown());
1220 wxGridEvent
g_evt(GetId(), wxEVT_GRID_CELL_LCLICK
, this,
1221 row
, col
, (int)ev
.GetX(), (int)ev
.GetY(),
1222 ev
.ControlDown(), ev
.ShiftDown());
1223 GetEventHandler()->ProcessEvent(g_evt
);
1226 if (LabelHitTest((int)ev
.GetX(), (int)ev
.GetY(), &row
, &col
))
1228 //OnLabelLeftClick(row, col, (int)ev.GetX(), (int)ev.GetY(), ev.ControlDown(), ev.ShiftDown());
1229 wxGridEvent
g_evt(GetId(), wxEVT_GRID_LABEL_LCLICK
, this,
1230 row
, col
, (int)ev
.GetX(), (int)ev
.GetY(),
1231 ev
.ControlDown(), ev
.ShiftDown());
1232 GetEventHandler()->ProcessEvent(g_evt
);
1237 else if (ev
.Dragging() && ev
.LeftIsDown())
1239 switch (m_dragStatus
)
1241 case wxGRID_DRAG_NONE
:
1244 if (LabelSashHitTest((int)ev
.GetX(), (int)ev
.GetY(), &orientation
, &m_dragRowOrCol
, &m_dragStartPosition
))
1246 if (orientation
== wxHORIZONTAL
)
1248 m_dragStatus
= wxGRID_DRAG_LEFT_RIGHT
;
1249 SetCursor(m_horizontalSashCursor
);
1250 m_dragLastPosition
= (int)ev
.GetX();
1254 m_dragStatus
= wxGRID_DRAG_UP_DOWN
;
1255 SetCursor(m_verticalSashCursor
);
1256 m_dragLastPosition
= (int)ev
.GetY();
1258 wxClientDC
dc(this);
1260 dc
.SetLogicalFunction(wxINVERT
);
1261 if (orientation
== wxHORIZONTAL
)
1262 dc
.DrawLine((int)ev
.GetX(), m_topOfSheet
, (int)ev
.GetX(), m_bottomOfSheet
);
1264 dc
.DrawLine(m_leftOfSheet
, (int)ev
.GetY(), m_rightOfSheet
, (int)ev
.GetY());
1271 case wxGRID_DRAG_LEFT_RIGHT
:
1273 wxClientDC
dc(this);
1275 dc
.SetLogicalFunction(wxINVERT
);
1276 dc
.DrawLine(m_dragLastPosition
, m_topOfSheet
, m_dragLastPosition
, m_bottomOfSheet
);
1278 dc
.DrawLine((int)ev
.GetX(), m_topOfSheet
, (int)ev
.GetX(), m_bottomOfSheet
);
1281 m_dragLastPosition
= (int)ev
.GetX();
1282 SetCursor(m_horizontalSashCursor
);
1285 case wxGRID_DRAG_UP_DOWN
:
1287 wxClientDC
dc(this);
1289 dc
.SetLogicalFunction(wxINVERT
);
1290 dc
.DrawLine(m_leftOfSheet
, m_dragLastPosition
, m_rightOfSheet
, m_dragLastPosition
);
1292 dc
.DrawLine(m_leftOfSheet
, (int)ev
.GetY(), m_rightOfSheet
, (int)ev
.GetY());
1295 m_dragLastPosition
= (int)ev
.GetY();
1296 SetCursor(m_verticalSashCursor
);
1301 else if (ev
.Moving())
1303 int rowOrCol
, orientation
, startPos
;
1304 if (LabelSashHitTest((int)ev
.GetX(), (int)ev
.GetY(), &orientation
, &rowOrCol
, &startPos
))
1306 if (orientation
== wxHORIZONTAL
)
1307 SetCursor(m_horizontalSashCursor
);
1309 SetCursor(m_verticalSashCursor
);
1312 SetCursor(*wxSTANDARD_CURSOR
);
1314 else if (ev
.LeftUp())
1316 switch (m_dragStatus
)
1318 case wxGRID_DRAG_LEFT_RIGHT
:
1320 wxClientDC
dc(this);
1322 dc
.SetLogicalFunction(wxINVERT
);
1323 dc
.DrawLine(m_dragLastPosition
, m_topOfSheet
, m_dragLastPosition
, m_bottomOfSheet
);
1324 dc
.SetLogicalFunction(wxCOPY
);
1328 if (ev
.GetX() > m_dragStartPosition
)
1330 m_colWidths
[m_dragRowOrCol
] = (short)(ev
.GetX() - m_dragStartPosition
);
1335 SetCursor(*wxSTANDARD_CURSOR
);
1337 GetClientSize(&cw
, &ch
);
1342 case wxGRID_DRAG_UP_DOWN
:
1344 wxClientDC
dc(this);
1346 dc
.SetLogicalFunction(wxINVERT
);
1347 dc
.DrawLine(m_leftOfSheet
, m_dragLastPosition
, m_rightOfSheet
, m_dragLastPosition
);
1348 dc
.SetLogicalFunction(wxCOPY
);
1352 if (ev
.GetY() > m_dragStartPosition
)
1354 m_rowHeights
[m_dragRowOrCol
] = (short)(ev
.GetY() - m_dragStartPosition
);
1359 SetCursor(*wxSTANDARD_CURSOR
);
1363 m_dragStatus
= wxGRID_DRAG_NONE
;
1365 else if (ev
.RightDown())
1368 if (CellHitTest((int)ev
.GetX(), (int)ev
.GetY(), &row
, &col
))
1370 //OnCellRightClick(row, col, (int)ev.GetX(), (int)ev.GetY(), ev.ControlDown(), ev.ShiftDown());
1371 wxGridEvent
g_evt(GetId(), wxEVT_GRID_CELL_RCLICK
, this,
1372 row
, col
, (int)ev
.GetX(), (int)ev
.GetY(),
1373 ev
.ControlDown(), ev
.ShiftDown());
1374 GetEventHandler()->ProcessEvent(g_evt
);
1377 if (LabelHitTest((int)ev
.GetX(), (int)ev
.GetY(), &row
, &col
))
1379 //OnLabelRightClick(row, col, (int)ev.GetX(), (int)ev.GetY(), ev.ControlDown(), ev.ShiftDown());
1380 wxGridEvent
g_evt(GetId(), wxEVT_GRID_LABEL_RCLICK
, this,
1381 row
, col
, (int)ev
.GetX(), (int)ev
.GetY(),
1382 ev
.ControlDown(), ev
.ShiftDown());
1383 GetEventHandler()->ProcessEvent(g_evt
);
1388 void wxGenericGrid::OnSelectCellImplementation(wxDC
*dc
, int row
, int col
)
1390 m_wCursorColumn
= col
;
1393 //OnChangeSelectionLabel();
1394 wxGridEvent
g_evt(GetId(), wxEVT_GRID_CHANGE_SEL_LABEL
, this);
1395 GetEventHandler()->ProcessEvent(g_evt
);
1397 SetGridClippingRegion(dc
);
1399 // Remove the highlight from the old cell
1400 if (m_currentRectVisible
)
1403 // Highlight the new cell and copy its content to the edit control
1404 SetCurrentRect(m_wCursorRow
, m_wCursorColumn
);
1405 wxGridCell
*cell
= GetCell(m_wCursorRow
, m_wCursorColumn
);
1408 if ( cell
->GetTextValue().IsNull() )
1409 m_textItem
->SetValue("");
1411 m_textItem
->SetValue(cell
->GetTextValue());
1414 SetGridClippingRegion(dc
);
1416 // 1) Why isn't this needed for Windows??
1417 // Probably because of the SetValue?? JS.
1418 // 2) Arrrrrgh. This isn't needed anywhere,
1419 // of course. One hour of debugging... RR.
1420 // Put back for Motif only on advice of Michael Bedward
1425 dc
->DestroyClippingRegion();
1427 //OnSelectCell(row, col);
1428 wxGridEvent
g_evt2(GetId(), wxEVT_GRID_SELECT_CELL
, this, row
, col
);
1429 GetEventHandler()->ProcessEvent(g_evt2
);
1432 wxGridCell
*wxGenericGrid::OnCreateCell(void)
1434 return new wxGridCell(this);
1437 void wxGenericGrid::OnChangeLabels(void)
1441 for (i
= 0; i
< m_totalRows
; i
++)
1443 sprintf(buf
, "%d", i
+1);
1444 SetLabelValue(wxVERTICAL
, buf
, i
);
1446 // A...Z,AA...ZZ,AAA...ZZZ, etc.
1447 for (i
= 0; i
< m_totalCols
; i
++)
1450 int noTimes
= (i
/26 + 1);
1451 int ch
= (i
% 26) + 65;
1453 for (j
= 0; j
< noTimes
; j
++)
1456 sprintf(buf2
, "%c", (char)ch
);
1459 SetLabelValue(wxHORIZONTAL
, buf
, i
);
1463 void wxGenericGrid::OnChangeSelectionLabel(void)
1468 wxString
rowLabel(GetLabelValue(wxVERTICAL
, GetCursorRow()));
1469 wxString
colLabel(GetLabelValue(wxHORIZONTAL
, GetCursorColumn()));
1471 wxString newLabel
= colLabel
+ rowLabel
;
1472 if ((newLabel
.Length() > 0) && (newLabel
.Length() <= 8) && GetTextItem())
1474 // GetTextItem()->SetLabel(newLabel);
1478 void wxGenericGrid::HighlightCell(wxDC
*dc
)
1480 dc
->SetLogicalFunction(wxINVERT
);
1483 dc
->DrawLine( m_currentRect
.x
+ 1,
1484 m_currentRect
.y
+ 1,
1485 m_currentRect
.x
+ m_currentRect
.width
- 1,
1486 m_currentRect
.y
+ 1);
1488 dc
->DrawLine( m_currentRect
.x
+ m_currentRect
.width
- 1,
1489 m_currentRect
.y
+ 1,
1490 m_currentRect
.x
+ m_currentRect
.width
- 1,
1491 m_currentRect
.y
+m_currentRect
.height
- 1 );
1493 dc
->DrawLine( m_currentRect
.x
+ m_currentRect
.width
- 1,
1494 m_currentRect
.y
+ m_currentRect
.height
- 1,
1495 m_currentRect
.x
+ 1,
1496 m_currentRect
.y
+ m_currentRect
.height
- 1);
1498 dc
->DrawLine( m_currentRect
.x
+ 1,
1499 m_currentRect
.y
+ m_currentRect
.height
- 1,
1500 m_currentRect
.x
+ 1,
1501 m_currentRect
.y
+ 1);
1503 dc
->SetLogicalFunction(wxCOPY
);
1506 void wxGenericGrid::DrawCellText(void)
1508 if (!m_currentRectVisible
)
1511 wxGridCell
*cell
= GetCell(GetCursorRow(), GetCursorColumn());
1515 static char szEdit
[300];
1517 wxClientDC
dc(this);
1520 SetGridClippingRegion(& dc
);
1522 dc
.SetBackgroundMode(wxTRANSPARENT
);
1523 dc
.SetBrush(cell
->GetBackgroundBrush());
1525 strcpy(szEdit
, m_textItem
->GetValue());
1528 rect
= m_currentRect
;
1534 DrawTextRect(& dc
, " ", &rect
, wxLEFT
);
1535 DrawTextRect(& dc
, szEdit
, &rect
, cell
->GetAlignment());
1537 dc
.DestroyClippingRegion();
1539 dc
.SetBackgroundMode(wxSOLID
);
1544 void wxGenericGrid::SetCurrentRect(int Row
, int Column
, int canvasW
, int canvasH
)
1546 int currentWidth
= m_leftOfSheet
+ m_verticalLabelWidth
;
1548 for (i
= m_scrollPosX
; i
< Column
; i
++)
1549 currentWidth
+= m_colWidths
[i
];
1551 int currentHeight
= m_topOfSheet
+ m_horizontalLabelHeight
;
1552 for (i
= m_scrollPosY
; i
< Row
; i
++)
1553 currentHeight
+= m_rowHeights
[i
];
1555 m_currentRect
.x
= currentWidth
;
1556 m_currentRect
.y
= currentHeight
;
1557 m_currentRect
.width
= m_colWidths
? (m_colWidths
[Column
]) : 0;
1558 m_currentRect
.height
= m_rowHeights
? (m_rowHeights
[Row
]) : 0;
1560 if (Row
< m_scrollPosY
|| Column
< m_scrollPosX
)
1561 m_currentRectVisible
= FALSE
;
1562 else if ((canvasW
!= -1 && canvasH
!= -1) && (m_currentRect
.x
> canvasW
|| m_currentRect
.y
> canvasH
))
1563 m_currentRectVisible
= FALSE
;
1564 else m_currentRectVisible
= TRUE
;
1567 static bool wxRectIntersection(wxRect
*rect1
, wxRect
*rect2
, wxRect
*rect3
)
1569 int x2_1
= rect1
->x
+ rect1
->width
;
1570 int y2_1
= rect1
->y
+ rect1
->height
;
1572 int x2_2
= rect2
->x
+ rect2
->width
;
1573 int y2_2
= rect2
->y
+ rect2
->height
;
1577 // Check for intersection
1578 if ((rect1
->x
> x2_2
) || (rect2
->x
> x2_1
) ||
1579 (rect1
->y
> y2_2
) || (rect2
->y
> y2_1
))
1582 rect3
->x
= rect3
->y
= rect3
->width
= rect3
->height
= 0;
1586 if (rect1
->x
> rect2
->x
)
1587 rect3
->x
= rect1
->x
;
1589 rect3
->x
= rect2
->x
;
1590 if (rect1
->y
> rect2
->y
)
1591 rect3
->y
= rect1
->y
;
1593 rect3
->y
= rect2
->y
;
1604 rect3
->width
= (int)(x2_3
- rect3
->x
);
1605 rect3
->height
= (int)(y2_3
- rect3
->y
);
1609 void wxGenericGrid::DrawTextRect(wxDC
*dc
, const wxString
& text
, wxRect
*rect
, int flag
)
1613 // Ultimately, this functionality should be built into wxWindows,
1614 // and optimized for each platform. E.g. on Windows, use DrawText
1615 // passing a clipping rectangle, so that the wxWindows clipping region
1616 // does not have to be used to implement this.
1618 // If we're already clipping, we need to find the intersection
1619 // between current clipping area and text clipping area.
1623 long clipX
, clipY
, clipW
, clipH
;
1624 dc
->GetClippingBox(&clipX
, &clipY
, &clipW
, &clipH
);
1625 clipRect
.x
= (int)clipX
; clipRect
.y
= (int)clipY
;
1626 clipRect
.width
= (int)clipW
; clipRect
.height
= (int)clipH
;
1628 bool alreadyClipping
= TRUE
;
1630 if (clipRect
.x
== 0 && clipRect
.y
== 0 && clipRect
.width
== 0 && clipRect
.height
== 0)
1632 alreadyClipping
= FALSE
;
1633 clipRect2
.x
= rect
->x
; clipRect2
.y
= rect
->y
;
1634 clipRect2
.width
= rect
->width
; clipRect2
.height
= rect
->height
;
1638 // Find intersection.
1639 if (!wxRectIntersection(rect
, &clipRect
, &clipRect2
))
1643 if (alreadyClipping
)
1644 dc
->DestroyClippingRegion();
1646 dc
->SetClippingRegion(clipRect2
.x
, clipRect2
.y
, clipRect2
.width
, clipRect2
.height
);
1647 long textWidth
, textHeight
;
1649 dc
->GetTextExtent(text
, &textWidth
, &textHeight
);
1657 x
= (rect
->x
+ rect
->width
- textWidth
- 1.0);
1658 y
= (rect
->y
+ (rect
->height
- textHeight
)/2.0);
1663 x
= (rect
->x
+ (rect
->width
- textWidth
)/2.0);
1664 y
= (rect
->y
+ (rect
->height
- textHeight
)/2.0);
1670 x
= (rect
->x
+ 1.0);
1671 y
= (rect
->y
+ (rect
->height
- textHeight
)/2.0);
1675 dc
->DrawText(text
, (long)x
, (long)y
);
1677 dc
->DestroyClippingRegion();
1679 // Restore old clipping
1680 if (alreadyClipping
)
1681 dc
->SetClippingRegion(clipRect
.x
, clipRect
.y
, clipRect
.width
, clipRect
.height
);
1686 void wxGenericGrid::DrawBitmapRect(wxDC
*dc
, wxBitmap
*bitmap
, wxRect
*rect
, int flag
)
1690 // Ultimately, this functionality should be built into wxWindows,
1691 // and optimized for each platform. E.g. on Windows, use DrawText
1692 // passing a clipping rectangle, so that the wxWindows clipping region
1693 // does not have to be used to implement this.
1695 // If we're already clipping, we need to find the intersection
1696 // between current clipping area and text clipping area.
1700 long clipX
, clipY
, clipW
, clipH
;
1701 dc
->GetClippingBox(&clipX
, &clipY
, &clipW
, &clipH
);
1702 clipRect
.x
= (int)clipX
; clipRect
.y
= (int)clipY
;
1703 clipRect
.width
= (int)clipW
; clipRect
.height
= (int)clipH
;
1705 bool alreadyClipping
= TRUE
;
1707 if (clipRect
.x
== 0 && clipRect
.y
== 0 && clipRect
.width
== 0 && clipRect
.height
== 0)
1709 alreadyClipping
= FALSE
;
1710 clipRect2
.x
= rect
->x
; clipRect2
.y
= rect
->y
;
1711 clipRect2
.width
= rect
->width
; clipRect2
.height
= rect
->height
;
1715 // Find intersection.
1716 if (!wxRectIntersection(rect
, &clipRect
, &clipRect2
))
1720 if (alreadyClipping
)
1721 dc
->DestroyClippingRegion();
1723 dc
->SetClippingRegion(clipRect2
.x
, clipRect2
.y
, clipRect2
.width
, clipRect2
.height
);
1724 float bitmapWidth
, bitmapHeight
;
1726 bitmapWidth
= bitmap
->GetWidth();
1727 bitmapHeight
= bitmap
->GetHeight();
1735 x
= (long)(rect
->x
+ rect
->width
- bitmapWidth
- 1);
1736 y
= (long)(rect
->y
+ (rect
->height
- bitmapHeight
)/2.0);
1741 x
= (long)(rect
->x
+ (rect
->width
- bitmapWidth
)/2.0);
1742 y
= (long)(rect
->y
+ (rect
->height
- bitmapHeight
)/2.0);
1748 x
= (long)(rect
->x
+ 1);
1749 y
= (long)(rect
->y
+ (rect
->height
- bitmapHeight
)/2.0);
1754 dcTemp
.SelectObject(*bitmap
);
1756 dc
->Blit( (long)x
, (long)y
, (long)bitmapWidth
, (long)bitmapHeight
, &dcTemp
, 0, 0);
1757 dcTemp
.SelectObject(wxNullBitmap
);
1759 dc
->DestroyClippingRegion();
1761 // Restore old clipping
1762 if (alreadyClipping
)
1763 dc
->SetClippingRegion(clipRect
.x
, clipRect
.y
, clipRect
.width
, clipRect
.height
);
1768 void wxGenericGrid::OnActivate(bool active
)
1772 // Edit control should always have the focus
1773 if (GetTextItem() && GetEditable())
1775 GetTextItem()->SetFocus();
1776 wxGridCell
*cell
= GetCell(GetCursorRow(), GetCursorColumn());
1778 GetTextItem()->SetValue(cell
->GetTextValue());
1783 void wxGenericGrid::SetCellValue(const wxString
& val
, int row
, int col
)
1785 wxGridCell
*cell
= GetCell(row
, col
);
1788 cell
->SetTextValue(val
);
1790 RefreshCell(row
, col
, TRUE
);
1794 void wxGenericGrid::RefreshCell(int row
, int col
, bool setText
)
1796 // Don't refresh within a pair of batch brackets
1797 if (GetBatchCount() > 0)
1801 GetClientSize(&cw
, &ch
);
1803 SetCurrentRect(row
, col
, cw
, ch
);
1804 if (m_currentRectVisible
)
1806 wxGridCell
*cell
= GetCell(row
, col
);
1808 bool currentPos
= FALSE
;
1809 if (row
== m_wCursorRow
&& col
== m_wCursorColumn
&& GetTextItem() && GetTextItem()->IsShown() && setText
)
1811 GetTextItem()->SetValue(cell
->GetTextValue());
1814 // Gets refreshed anyway in MSW
1819 wxClientDC
dc(this);
1821 DrawCellBackground(& dc
, &m_currentRect
, row
, col
);
1822 DrawCellValue(& dc
, &m_currentRect
, row
, col
);
1828 wxString
& wxGenericGrid::GetCellValue(int row
, int col
) const
1830 static wxString
emptyString("");
1832 wxGridCell
*cell
= GetCell(row
, col
);
1834 return cell
->GetTextValue();
1839 void wxGenericGrid::SetColumnWidth(int col
, int width
)
1841 if (col
<= m_totalCols
)
1842 m_colWidths
[col
] = width
;
1845 int wxGenericGrid::GetColumnWidth(int col
) const
1847 if (col
<= m_totalCols
)
1848 return m_colWidths
[col
];
1853 void wxGenericGrid::SetRowHeight(int row
, int height
)
1855 if (row
<= m_totalRows
)
1856 m_rowHeights
[row
] = height
;
1859 int wxGenericGrid::GetRowHeight(int row
) const
1861 if (row
<= m_totalRows
)
1862 return m_rowHeights
[row
];
1867 void wxGenericGrid::SetLabelSize(int orientation
, int sz
)
1869 if (orientation
== wxHORIZONTAL
)
1870 m_horizontalLabelHeight
= sz
;
1872 m_verticalLabelWidth
= sz
;
1874 SetCurrentRect(GetCursorRow(), GetCursorColumn());
1877 int wxGenericGrid::GetLabelSize(int orientation
) const
1879 if (orientation
== wxHORIZONTAL
)
1880 return m_horizontalLabelHeight
;
1882 return m_verticalLabelWidth
;
1885 wxGridCell
*wxGenericGrid::GetLabelCell(int orientation
, int pos
) const
1887 if (orientation
== wxHORIZONTAL
)
1889 if (m_colLabelCells
&& pos
< m_totalCols
)
1890 return m_colLabelCells
[pos
];
1892 return (wxGridCell
*) NULL
;
1896 if (m_rowLabelCells
&& pos
< m_totalRows
)
1897 return m_rowLabelCells
[pos
];
1899 return (wxGridCell
*) NULL
;
1903 void wxGenericGrid::SetLabelValue(int orientation
, const wxString
& val
, int pos
)
1905 wxGridCell
*cell
= GetLabelCell(orientation
, pos
);
1907 cell
->SetTextValue(val
);
1910 wxString
& wxGenericGrid::GetLabelValue(int orientation
, int pos
) const
1912 static wxString emptyString
= "";
1913 wxGridCell
*cell
= GetLabelCell(orientation
, pos
);
1915 return cell
->GetTextValue();
1920 void wxGenericGrid::SetLabelAlignment(int orientation
, int align
)
1922 if (orientation
== wxHORIZONTAL
)
1923 m_horizontalLabelAlignment
= align
;
1925 m_verticalLabelAlignment
= align
;
1927 SetCurrentRect(GetCursorRow(), GetCursorColumn());
1930 int wxGenericGrid::GetLabelAlignment(int orientation
) const
1932 if (orientation
== wxHORIZONTAL
)
1933 return m_horizontalLabelAlignment
;
1935 return m_verticalLabelAlignment
;
1938 void wxGenericGrid::SetLabelTextColour(const wxColour
& colour
)
1940 m_labelTextColour
= colour
;
1944 void wxGenericGrid::SetLabelBackgroundColour(const wxColour
& colour
)
1946 m_labelBackgroundColour
= colour
;
1947 m_labelBackgroundBrush
= * wxTheBrushList
->FindOrCreateBrush(m_labelBackgroundColour
, wxSOLID
);
1950 void wxGenericGrid::SetEditable(bool edit
)
1955 int controlW
, controlH
;
1956 m_textItem
->GetSize(&controlW
, &controlH
);
1957 m_editControlPosition
.height
= controlH
;
1959 m_topOfSheet
= m_editControlPosition
.x
+ controlH
+ 2;
1962 m_editingPanel
->Show(TRUE
);
1963 m_textItem
->Show(TRUE
);
1964 m_textItem
->SetFocus();
1972 m_textItem
->Show(FALSE
);
1973 m_editingPanel
->Show(FALSE
);
1977 SetCurrentRect(GetCursorRow(), GetCursorColumn());
1980 GetClientSize(&cw
, &ch
);
1985 int m_scrollWidth = 16;
1986 GetClientSize(&cw, &ch);
1989 m_vScrollBar->SetSize(cw - m_scrollWidth, m_topOfSheet,
1990 m_scrollWidth, ch - m_topOfSheet - m_scrollWidth);
1994 void wxGenericGrid::SetCellAlignment(int flag
, int row
, int col
)
1996 wxGridCell
*cell
= GetCell(row
, col
);
1998 cell
->SetAlignment(flag
);
2001 int wxGenericGrid::GetCellAlignment(int row
, int col
) const
2003 wxGridCell
*cell
= GetCell(row
, col
);
2005 return cell
->GetAlignment();
2007 return m_cellAlignment
;
2010 void wxGenericGrid::SetCellAlignment(int flag
)
2012 m_cellAlignment
= flag
;
2014 for (i
= 0; i
< GetRows(); i
++)
2015 for (j
= 0; j
< GetCols(); j
++)
2017 GetCell(i
, j
)->SetAlignment(flag
);
2020 int wxGenericGrid::GetCellAlignment(void) const
2022 return m_cellAlignment
;
2025 void wxGenericGrid::SetCellBackgroundColour(const wxColour
& col
)
2027 m_cellBackgroundColour
= col
;
2029 for (i
= 0; i
< GetRows(); i
++)
2030 for (j
= 0; j
< GetCols(); j
++)
2032 GetCell(i
, j
)->SetBackgroundColour(col
);
2035 void wxGenericGrid::SetCellBackgroundColour(const wxColour
& val
, int row
, int col
)
2037 wxGridCell
*cell
= GetCell(row
, col
);
2040 cell
->SetBackgroundColour(val
);
2041 RefreshCell(row
, col
);
2045 wxColour
& wxGenericGrid::GetCellBackgroundColour(int row
, int col
) const
2047 wxGridCell
*cell
= GetCell(row
, col
);
2049 return cell
->GetBackgroundColour();
2051 return (wxColour
&) m_cellBackgroundColour
;
2054 void wxGenericGrid::SetCellTextColour(const wxColour
& val
, int row
, int col
)
2056 wxGridCell
*cell
= GetCell(row
, col
);
2059 cell
->SetTextColour(val
);
2060 RefreshCell(row
, col
);
2064 void wxGenericGrid::SetCellTextFont(const wxFont
& fnt
, int row
, int col
)
2066 wxGridCell
*cell
= GetCell(row
, col
);
2070 RefreshCell(row
, col
);
2074 wxFont
& wxGenericGrid::GetCellTextFont(int row
, int col
) const
2076 wxGridCell
*cell
= GetCell(row
, col
);
2078 return (wxFont
&) cell
->GetFont();
2080 return (wxFont
&) m_cellTextFont
;
2083 wxColour
& wxGenericGrid::GetCellTextColour(int row
, int col
) const
2085 wxGridCell
*cell
= GetCell(row
, col
);
2087 return (wxColour
&) cell
->GetTextColour();
2089 return (wxColour
&) m_cellTextColour
;
2092 void wxGenericGrid::SetCellTextColour(const wxColour
& val
)
2094 m_cellTextColour
= val
;
2096 for (i
= 0; i
< GetRows(); i
++)
2097 for (j
= 0; j
< GetCols(); j
++)
2099 GetCell(i
, j
)->SetTextColour(val
);
2102 void wxGenericGrid::SetCellTextFont(const wxFont
& fnt
)
2104 m_cellTextFont
= fnt
;
2106 for (i
= 0; i
< GetRows(); i
++)
2107 for (j
= 0; j
< GetCols(); j
++)
2109 GetCell(i
, j
)->SetFont(fnt
);
2112 void wxGenericGrid::SetCellBitmap(wxBitmap
*bitmap
, int row
, int col
)
2114 wxGridCell
*cell
= GetCell(row
, col
);
2117 cell
->SetCellBitmap(bitmap
);
2118 RefreshCell(row
, col
);
2122 wxBitmap
*wxGenericGrid::GetCellBitmap(int row
, int col
) const
2124 wxGridCell
*cell
= GetCell(row
, col
);
2127 return cell
->GetCellBitmap();
2130 return (wxBitmap
*) NULL
;
2133 bool wxGenericGrid::InsertCols(int pos
, int n
, bool updateLabels
)
2135 if (pos
> m_totalCols
)
2139 return CreateGrid(1, n
);
2144 for (i
= 0; i
< m_totalRows
; i
++)
2146 wxGridCell
**cols
= m_gridCells
[i
];
2147 wxGridCell
**newCols
= new wxGridCell
*[m_totalCols
+ n
];
2148 for (j
= 0; j
< pos
; j
++)
2149 newCols
[j
] = cols
[j
];
2150 for (j
= pos
; j
< pos
+ n
; j
++)
2151 newCols
[j
] = new wxGridCell(this);
2152 for (j
= pos
+ n
; j
< m_totalCols
+ n
; j
++)
2153 newCols
[j
] = cols
[j
- n
];
2156 m_gridCells
[i
] = newCols
;
2160 short *newColWidths
= new short[m_totalCols
+ n
];
2161 for (j
= 0; j
< pos
; j
++)
2162 newColWidths
[j
] = m_colWidths
[j
];
2163 for (j
= pos
; j
< pos
+ n
; j
++)
2164 newColWidths
[j
] = wxGRID_DEFAULT_CELL_WIDTH
;
2165 for (j
= pos
+ n
; j
< m_totalCols
+ n
; j
++)
2166 newColWidths
[j
] = m_colWidths
[j
- n
];
2167 delete[] m_colWidths
;
2168 m_colWidths
= newColWidths
;
2171 wxGridCell
**newLabels
= new wxGridCell
*[m_totalCols
+ n
];
2172 for (j
= 0; j
< pos
; j
++)
2173 newLabels
[j
] = m_colLabelCells
[j
];
2174 for (j
= pos
; j
< pos
+ n
; j
++)
2175 newLabels
[j
] = new wxGridCell(this);
2176 for (j
= pos
+ n
; j
< m_totalCols
+ n
; j
++)
2177 newLabels
[j
] = m_colLabelCells
[j
- n
];
2179 delete[] m_colLabelCells
;
2180 m_colLabelCells
= newLabels
;
2186 wxGridEvent
g_evt(GetId(), wxEVT_GRID_CHANGE_LABELS
, this);
2187 GetEventHandler()->ProcessEvent(g_evt
);
2196 bool wxGenericGrid::InsertRows(int pos
, int n
, bool updateLabels
)
2198 if (pos
> m_totalRows
)
2202 return CreateGrid(n
, 1);
2207 wxGridCell
***rows
= new wxGridCell
**[m_totalRows
+ n
];
2210 for (i
= 0; i
< pos
; i
++)
2211 rows
[i
] = m_gridCells
[i
];
2213 for (i
= pos
; i
< pos
+ n
; i
++)
2215 rows
[i
] = new wxGridCell
*[m_totalCols
];
2216 for (j
= 0; j
< m_totalCols
; j
++)
2217 rows
[i
][j
] = new wxGridCell(this);
2220 for (i
= pos
+ n
; i
< m_totalRows
+ n
; i
++)
2221 rows
[i
] = m_gridCells
[i
- n
];
2223 delete[] m_gridCells
;
2227 short *newRowHeights
= new short[m_totalRows
+ n
];
2228 for (i
= 0; i
< pos
; i
++)
2229 newRowHeights
[i
] = m_rowHeights
[i
];
2230 for (i
= pos
; i
< pos
+ n
; i
++)
2231 newRowHeights
[i
] = wxGRID_DEFAULT_CELL_HEIGHT
;
2232 for (i
= pos
+ n
; i
< m_totalRows
+ n
; i
++)
2233 newRowHeights
[i
] = m_rowHeights
[i
- n
];
2234 delete[] m_rowHeights
;
2235 m_rowHeights
= newRowHeights
;
2238 wxGridCell
**newLabels
= new wxGridCell
*[m_totalRows
+ n
];
2239 for (i
= 0; i
< pos
; i
++)
2240 newLabels
[i
] = m_rowLabelCells
[i
];
2241 for (i
= pos
; i
< pos
+ n
; i
++)
2242 newLabels
[i
] = new wxGridCell(this);
2243 for (i
= pos
+ n
; i
< m_totalRows
+ n
; i
++)
2244 newLabels
[i
] = m_rowLabelCells
[i
- n
];
2246 delete[] m_rowLabelCells
;
2247 m_rowLabelCells
= newLabels
;
2253 wxGridEvent
g_evt(GetId(), wxEVT_GRID_CHANGE_LABELS
, this);
2254 GetEventHandler()->ProcessEvent(g_evt
);
2263 bool wxGenericGrid::AppendCols(int n
, bool updateLabels
)
2265 return InsertCols(GetCols(), n
, updateLabels
);
2268 bool wxGenericGrid::AppendRows(int n
, bool updateLabels
)
2270 return InsertRows(GetRows(), n
, updateLabels
);
2273 bool wxGenericGrid::DeleteRows(int pos
, int n
, bool updateLabels
)
2275 if (pos
> m_totalRows
)
2282 wxGridCell
***rows
= new wxGridCell
**[m_totalRows
- n
];
2285 for (i
= 0; i
< pos
; i
++)
2286 rows
[i
] = m_gridCells
[i
];
2288 for (i
= pos
+ n
; i
< m_totalRows
; i
++)
2289 rows
[i
-n
] = m_gridCells
[i
];
2291 delete[] m_gridCells
;
2295 short *newRowHeights
= new short[m_totalRows
- n
];
2296 for (i
= 0; i
< pos
; i
++)
2297 newRowHeights
[i
] = m_rowHeights
[i
];
2298 for (i
= pos
+ n
; i
< m_totalRows
; i
++)
2299 newRowHeights
[i
-n
] = m_rowHeights
[i
];
2300 delete[] m_rowHeights
;
2301 m_rowHeights
= newRowHeights
;
2304 wxGridCell
**newLabels
= new wxGridCell
*[m_totalRows
- n
];
2305 for (i
= 0; i
< pos
; i
++)
2306 newLabels
[i
] = m_rowLabelCells
[i
];
2307 for (i
= pos
+ n
; i
< m_totalRows
; i
++)
2308 newLabels
[i
-n
] = m_rowLabelCells
[i
];
2310 delete[] m_rowLabelCells
;
2311 m_rowLabelCells
= newLabels
;
2317 wxGridEvent
g_evt(GetId(), wxEVT_GRID_CHANGE_LABELS
, this);
2318 GetEventHandler()->ProcessEvent(g_evt
);
2325 bool wxGenericGrid::DeleteCols(int pos
, int n
, bool updateLabels
)
2327 if (pos
+ n
> m_totalCols
)
2335 for (i
= 0; i
< m_totalRows
; i
++)
2337 wxGridCell
**cols
= m_gridCells
[i
];
2338 wxGridCell
**newCols
= new wxGridCell
*[m_totalCols
- n
];
2339 for (j
= 0; j
< pos
; j
++)
2340 newCols
[j
] = cols
[j
];
2341 for (j
= pos
; j
< pos
+ n
; j
++)
2343 for (j
= pos
+ n
; j
< m_totalCols
; j
++)
2344 newCols
[j
-n
] = cols
[j
];
2347 m_gridCells
[i
] = newCols
;
2351 short *newColWidths
= new short[m_totalCols
- n
];
2352 for (j
= 0; j
< pos
; j
++)
2353 newColWidths
[j
] = m_colWidths
[j
];
2354 for (j
= pos
+ n
; j
< m_totalCols
; j
++)
2355 newColWidths
[j
-n
] = m_colWidths
[j
];
2356 delete[] m_colWidths
;
2357 m_colWidths
= newColWidths
;
2360 wxGridCell
**newLabels
= new wxGridCell
*[m_totalCols
- n
];
2361 for (j
= 0; j
< pos
; j
++)
2362 newLabels
[j
] = m_colLabelCells
[j
];
2363 for (j
= pos
+ n
; j
< m_totalCols
; j
++)
2364 newLabels
[j
-n
] = m_colLabelCells
[j
];
2366 delete[] m_colLabelCells
;
2367 m_colLabelCells
= newLabels
;
2373 wxGridEvent
g_evt(GetId(), wxEVT_GRID_CHANGE_LABELS
, this);
2374 GetEventHandler()->ProcessEvent(g_evt
);
2381 void wxGenericGrid::SetGridCursor(int row
, int col
)
2383 if (row
>= m_totalRows
|| col
>= m_totalCols
)
2386 if (row
== GetCursorRow() && col
== GetCursorColumn())
2389 wxClientDC
dc(this);
2392 SetGridClippingRegion(& dc
);
2394 if (m_currentRectVisible
)
2395 HighlightCell(& dc
);
2398 m_wCursorColumn
= col
;
2401 GetClientSize(&cw
, &ch
);
2403 SetCurrentRect(row
, col
, cw
, ch
);
2405 if (m_currentRectVisible
)
2406 HighlightCell(& dc
);
2408 dc
.DestroyClippingRegion();
2416 wxGridCell::wxGridCell(wxGenericGrid
*window
)
2418 cellBitmap
= (wxBitmap
*) NULL
;
2420 backgroundBrush
= wxNullBrush
;
2422 textColour
= window
->GetCellTextColour();
2424 textColour
.Set(0,0,0);
2426 backgroundColour
= window
->GetCellBackgroundColour();
2428 backgroundColour
.Set(255,255,255);
2431 font
= window
->GetCellTextFont();
2433 font
= * wxTheFontList
->FindOrCreateFont(12, wxSWISS
, wxNORMAL
, wxNORMAL
);
2435 SetBackgroundColour(backgroundColour
);
2438 alignment
= window
->GetCellAlignment();
2443 wxGridCell::~wxGridCell(void)
2447 void wxGridCell::SetBackgroundColour(const wxColour
& colour
)
2449 backgroundColour
= colour
;
2450 backgroundBrush
= * wxTheBrushList
->FindOrCreateBrush(backgroundColour
, wxSOLID
);
2453 void wxGenericGrid::OnText(wxCommandEvent
& WXUNUSED(ev
) )
2455 wxGenericGrid
*grid
= this;
2456 wxGridCell
*cell
= grid
->GetCell(grid
->GetCursorRow(), grid
->GetCursorColumn());
2457 if (cell
&& grid
->CurrentCellVisible())
2459 cell
->SetTextValue(grid
->GetTextItem()->GetValue());
2460 wxClientDC
dc(grid
);
2463 grid
->SetGridClippingRegion(& dc
);
2464 grid
->DrawCellBackground(& dc
, &grid
->GetCurrentRect(), grid
->GetCursorRow(), grid
->GetCursorColumn());
2465 grid
->DrawCellValue(& dc
, &grid
->GetCurrentRect(), grid
->GetCursorRow(), grid
->GetCursorColumn());
2466 grid
->HighlightCell(& dc
);
2467 dc
.DestroyClippingRegion();
2470 //grid->OnCellChange(grid->GetCursorRow(), grid->GetCursorColumn());
2471 wxGridEvent
g_evt(GetId(), wxEVT_GRID_CELL_CHANGE
, grid
,
2472 grid
->GetCursorRow(), grid
->GetCursorColumn());
2473 GetEventHandler()->ProcessEvent(g_evt
);
2475 // grid->DrawCellText();
2479 void wxGenericGrid::OnGridScroll(wxScrollEvent
& ev
)
2481 static bool inScroll
= FALSE
;
2487 wxGenericGrid
*win
= this;
2489 bool change
= FALSE
;
2491 if (ev
.GetEventObject() == win
->GetHorizScrollBar())
2493 change
= (ev
.GetPosition() != m_scrollPosX
);
2494 win
->SetScrollPosX(ev
.GetPosition());
2498 change
= (ev
.GetPosition() != m_scrollPosY
);
2499 win
->SetScrollPosY(ev
.GetPosition());
2502 win
->UpdateDimensions();
2504 win
->SetCurrentRect(win
->GetCursorRow(), win
->GetCursorColumn());
2506 // Because rows and columns can be arbitrary sizes,
2507 // the scrollbars will need to be adjusted to reflect the
2511 if (change
) win
->Refresh(FALSE
);
2517 //----------------------------------------------------------------------
2518 // Default wxGridEvent handlers
2519 // (just redirect to the pre-existing virtual methods)
2521 void wxGenericGrid::_OnSelectCell(wxGridEvent
& ev
)
2523 OnSelectCell(ev
.m_row
, ev
.m_col
);
2526 void wxGenericGrid::_OnCreateCell(wxGridEvent
& ev
)
2528 ev
.m_cell
= OnCreateCell();
2531 void wxGenericGrid::_OnChangeLabels(wxGridEvent
& WXUNUSED(ev
))
2536 void wxGenericGrid::_OnChangeSelectionLabel(wxGridEvent
& WXUNUSED(ev
))
2538 OnChangeSelectionLabel();
2541 void wxGenericGrid::_OnCellChange(wxGridEvent
& ev
)
2543 OnCellChange(ev
.m_row
, ev
.m_col
);
2546 void wxGenericGrid::_OnCellLeftClick(wxGridEvent
& ev
)
2548 OnCellLeftClick(ev
.m_row
, ev
.m_col
, ev
.m_x
, ev
.m_y
, ev
.m_control
, ev
.m_shift
);
2551 void wxGenericGrid::_OnCellRightClick(wxGridEvent
& ev
)
2553 OnCellRightClick(ev
.m_row
, ev
.m_col
, ev
.m_x
, ev
.m_y
, ev
.m_control
, ev
.m_shift
);
2556 void wxGenericGrid::_OnLabelLeftClick(wxGridEvent
& ev
)
2558 OnLabelLeftClick(ev
.m_row
, ev
.m_col
, ev
.m_x
, ev
.m_y
, ev
.m_control
, ev
.m_shift
);
2561 void wxGenericGrid::_OnLabelRightClick(wxGridEvent
& ev
)
2563 OnLabelRightClick(ev
.m_row
, ev
.m_col
, ev
.m_x
, ev
.m_y
, ev
.m_control
, ev
.m_shift
);