]> git.saurik.com Git - wxWidgets.git/blame - src/generic/grid.cpp
Clear is also expected to clear the text
[wxWidgets.git] / src / generic / grid.cpp
CommitLineData
2796cce3 1///////////////////////////////////////////////////////////////////////////
faa94f3e 2// Name: src/generic/grid.cpp
f85afd4e
MB
3// Purpose: wxGrid and related classes
4// Author: Michael Bedward (based on code by Julian Smart, Robin Dunn)
d4175745 5// Modified by: Robin Dunn, Vadim Zeitlin, Santiago Palacios
f85afd4e
MB
6// Created: 1/08/1999
7// RCS-ID: $Id$
8// Copyright: (c) Michael Bedward (mbedward@ozemail.com.au)
65571936 9// Licence: wxWindows licence
f85afd4e
MB
10/////////////////////////////////////////////////////////////////////////////
11
bec70262
VZ
12/*
13 TODO:
14
15 - Replace use of wxINVERT with wxOverlay
16 - Make Begin/EndBatch() the same as the generic Freeze/Thaw()
10a4531d
VZ
17 - Review the column reordering code, it's a mess.
18 - Implement row reordering after dealing with the columns.
bec70262
VZ
19 */
20
95427194 21// For compilers that support precompilation, includes "wx/wx.h".
4d85bcd1
JS
22#include "wx/wxprec.h"
23
f85afd4e
MB
24#ifdef __BORLANDC__
25 #pragma hdrstop
26#endif
27
27b92ca4
VZ
28#if wxUSE_GRID
29
4c44eb66
PC
30#include "wx/grid.h"
31
f85afd4e
MB
32#ifndef WX_PRECOMP
33 #include "wx/utils.h"
34 #include "wx/dcclient.h"
35 #include "wx/settings.h"
36 #include "wx/log.h"
508011ce
VZ
37 #include "wx/textctrl.h"
38 #include "wx/checkbox.h"
4ee5fc9c 39 #include "wx/combobox.h"
816be743 40 #include "wx/valtext.h"
60d876f3 41 #include "wx/intl.h"
c77a6796 42 #include "wx/math.h"
2a673eb1 43 #include "wx/listbox.h"
f85afd4e
MB
44#endif
45
cb5df486 46#include "wx/textfile.h"
816be743 47#include "wx/spinctrl.h"
c4608a8a 48#include "wx/tokenzr.h"
4d1bc39c 49#include "wx/renderer.h"
ad805b9e 50#include "wx/headerctrl.h"
82edfbe7 51#include "wx/hashset.h"
6d004f67 52
b5808881 53#include "wx/generic/gridsel.h"
29efc6e4
FM
54#include "wx/generic/gridctrl.h"
55#include "wx/generic/grideditors.h"
56#include "wx/generic/private/grid.h"
07296f0b 57
23318a53 58const char wxGridNameStr[] = "grid";
4c44eb66 59
0b7e6e7d
SN
60#if defined(__WXMOTIF__)
61 #define WXUNUSED_MOTIF(identifier) WXUNUSED(identifier)
c78b3acd 62#else
0b7e6e7d 63 #define WXUNUSED_MOTIF(identifier) identifier
c78b3acd
SN
64#endif
65
66#if defined(__WXGTK__)
67 #define WXUNUSED_GTK(identifier) WXUNUSED(identifier)
68#else
69 #define WXUNUSED_GTK(identifier) identifier
70#endif
71
3f8e5072
JS
72// Required for wxIs... functions
73#include <ctype.h>
74
7a7fa93b 75WX_DECLARE_HASH_SET_WITH_DECL_PTR(int, wxIntegerHash, wxIntegerEqual,
e4c8592e 76 wxGridFixedIndicesSet, class WXDLLIMPEXP_ADV);
82edfbe7 77
29efc6e4 78
b99be8fb 79// ----------------------------------------------------------------------------
29efc6e4 80// globals
b99be8fb
VZ
81// ----------------------------------------------------------------------------
82
670d0177
VZ
83namespace
84{
85
29efc6e4
FM
86//#define DEBUG_ATTR_CACHE
87#ifdef DEBUG_ATTR_CACHE
88 static size_t gs_nAttrCacheHits = 0;
89 static size_t gs_nAttrCacheMisses = 0;
90#endif
758cbedf 91
670d0177
VZ
92// this struct simply combines together the default header renderers
93//
94// as the renderers ctors are trivial, there is no problem with making them
95// globals
96struct DefaultHeaderRenderers
97{
98 wxGridColumnHeaderRendererDefault colRenderer;
99 wxGridRowHeaderRendererDefault rowRenderer;
100 wxGridCornerHeaderRendererDefault cornerRenderer;
101} gs_defaultHeaderRenderers;
102
103} // anonymous namespace
104
29efc6e4
FM
105// ----------------------------------------------------------------------------
106// constants
107// ----------------------------------------------------------------------------
6f292345 108
29efc6e4
FM
109wxGridCellCoords wxGridNoCellCoords( -1, -1 );
110wxRect wxGridNoCellRect( -1, -1, -1, -1 );
6f292345 111
29efc6e4
FM
112namespace
113{
b99be8fb 114
29efc6e4
FM
115// scroll line size
116const size_t GRID_SCROLL_LINE_X = 15;
117const size_t GRID_SCROLL_LINE_Y = GRID_SCROLL_LINE_X;
96ca74cd 118
29efc6e4
FM
119// the size of hash tables used a bit everywhere (the max number of elements
120// in these hash tables is the number of rows/columns)
121const int GRID_HASH_SIZE = 100;
2e9a6788 122
29efc6e4
FM
123// the minimal distance in pixels the mouse needs to move to start a drag
124// operation
125const int DRAG_SENSITIVITY = 3;
b99be8fb 126
29efc6e4 127} // anonymous namespace
b99be8fb
VZ
128
129#include "wx/arrimpl.cpp"
130
131WX_DEFINE_OBJARRAY(wxGridCellCoordsArray)
132WX_DEFINE_OBJARRAY(wxGridCellWithAttrArray)
133
0f442030
RR
134// ----------------------------------------------------------------------------
135// events
136// ----------------------------------------------------------------------------
137
9b11752c
VZ
138wxDEFINE_EVENT( wxEVT_GRID_CELL_LEFT_CLICK, wxGridEvent );
139wxDEFINE_EVENT( wxEVT_GRID_CELL_RIGHT_CLICK, wxGridEvent );
140wxDEFINE_EVENT( wxEVT_GRID_CELL_LEFT_DCLICK, wxGridEvent );
141wxDEFINE_EVENT( wxEVT_GRID_CELL_RIGHT_DCLICK, wxGridEvent );
142wxDEFINE_EVENT( wxEVT_GRID_CELL_BEGIN_DRAG, wxGridEvent );
143wxDEFINE_EVENT( wxEVT_GRID_LABEL_LEFT_CLICK, wxGridEvent );
144wxDEFINE_EVENT( wxEVT_GRID_LABEL_RIGHT_CLICK, wxGridEvent );
145wxDEFINE_EVENT( wxEVT_GRID_LABEL_LEFT_DCLICK, wxGridEvent );
146wxDEFINE_EVENT( wxEVT_GRID_LABEL_RIGHT_DCLICK, wxGridEvent );
147wxDEFINE_EVENT( wxEVT_GRID_ROW_SIZE, wxGridSizeEvent );
148wxDEFINE_EVENT( wxEVT_GRID_COL_SIZE, wxGridSizeEvent );
6f58f3d7 149wxDEFINE_EVENT( wxEVT_GRID_COL_AUTO_SIZE, wxGridSizeEvent );
9b11752c
VZ
150wxDEFINE_EVENT( wxEVT_GRID_COL_MOVE, wxGridEvent );
151wxDEFINE_EVENT( wxEVT_GRID_COL_SORT, wxGridEvent );
152wxDEFINE_EVENT( wxEVT_GRID_RANGE_SELECT, wxGridRangeSelectEvent );
153wxDEFINE_EVENT( wxEVT_GRID_CELL_CHANGING, wxGridEvent );
154wxDEFINE_EVENT( wxEVT_GRID_CELL_CHANGED, wxGridEvent );
155wxDEFINE_EVENT( wxEVT_GRID_SELECT_CELL, wxGridEvent );
156wxDEFINE_EVENT( wxEVT_GRID_EDITOR_SHOWN, wxGridEvent );
157wxDEFINE_EVENT( wxEVT_GRID_EDITOR_HIDDEN, wxGridEvent );
158wxDEFINE_EVENT( wxEVT_GRID_EDITOR_CREATED, wxGridEditorCreatedEvent );
1dc17bca 159wxDEFINE_EVENT( wxEVT_GRID_TABBING, wxGridEvent );
0b190b0f 160
db2c0468
SC
161// ----------------------------------------------------------------------------
162// private helpers
163// ----------------------------------------------------------------------------
164
165namespace
166{
167
168 // ensure that first is less or equal to second, swapping the values if
169 // necessary
170 void EnsureFirstLessThanSecond(int& first, int& second)
171 {
172 if ( first > second )
173 wxSwap(first, second);
174 }
175
176} // anonymous namespace
177
29efc6e4
FM
178// ============================================================================
179// implementation
180// ============================================================================
65e4e78e 181
29efc6e4 182IMPLEMENT_ABSTRACT_CLASS(wxGridCellEditorEvtHandler, wxEvtHandler)
816be743 183
29efc6e4
FM
184BEGIN_EVENT_TABLE( wxGridCellEditorEvtHandler, wxEvtHandler )
185 EVT_KILL_FOCUS( wxGridCellEditorEvtHandler::OnKillFocus )
186 EVT_KEY_DOWN( wxGridCellEditorEvtHandler::OnKeyDown )
187 EVT_CHAR( wxGridCellEditorEvtHandler::OnChar )
188END_EVENT_TABLE()
816be743 189
29efc6e4
FM
190BEGIN_EVENT_TABLE(wxGridHeaderCtrl, wxHeaderCtrl)
191 EVT_HEADER_CLICK(wxID_ANY, wxGridHeaderCtrl::OnClick)
4a07d706
VZ
192 EVT_HEADER_DCLICK(wxID_ANY, wxGridHeaderCtrl::OnDoubleClick)
193 EVT_HEADER_RIGHT_CLICK(wxID_ANY, wxGridHeaderCtrl::OnRightClick)
816be743 194
29efc6e4
FM
195 EVT_HEADER_BEGIN_RESIZE(wxID_ANY, wxGridHeaderCtrl::OnBeginResize)
196 EVT_HEADER_RESIZING(wxID_ANY, wxGridHeaderCtrl::OnResizing)
197 EVT_HEADER_END_RESIZE(wxID_ANY, wxGridHeaderCtrl::OnEndResize)
816be743 198
29efc6e4
FM
199 EVT_HEADER_BEGIN_REORDER(wxID_ANY, wxGridHeaderCtrl::OnBeginReorder)
200 EVT_HEADER_END_REORDER(wxID_ANY, wxGridHeaderCtrl::OnEndReorder)
201END_EVENT_TABLE()
816be743 202
29efc6e4 203wxGridOperations& wxGridRowOperations::Dual() const
65e4e78e 204{
29efc6e4
FM
205 static wxGridColumnOperations s_colOper;
206
207 return s_colOper;
816be743
VZ
208}
209
29efc6e4 210wxGridOperations& wxGridColumnOperations::Dual() const
0b190b0f 211{
29efc6e4 212 static wxGridRowOperations s_rowOper;
2f024384 213
29efc6e4 214 return s_rowOper;
0b190b0f
VZ
215}
216
508011ce 217// ----------------------------------------------------------------------------
29efc6e4
FM
218// wxGridCellWorker is an (almost) empty common base class for
219// wxGridCellRenderer and wxGridCellEditor managing ref counting
508011ce
VZ
220// ----------------------------------------------------------------------------
221
29efc6e4 222void wxGridCellWorker::SetParameters(const wxString& WXUNUSED(params))
65e4e78e 223{
29efc6e4 224 // nothing to do
65e4e78e
VZ
225}
226
29efc6e4 227wxGridCellWorker::~wxGridCellWorker()
65e4e78e 228{
508011ce
VZ
229}
230
ba9574c3
VZ
231// ----------------------------------------------------------------------------
232// wxGridHeaderLabelsRenderer and related classes
233// ----------------------------------------------------------------------------
234
235void wxGridHeaderLabelsRenderer::DrawLabel(const wxGrid& grid,
236 wxDC& dc,
237 const wxString& value,
238 const wxRect& rect,
239 int horizAlign,
240 int vertAlign,
241 int textOrientation) const
242{
243 dc.SetBackgroundMode(wxBRUSHSTYLE_TRANSPARENT);
244 dc.SetTextForeground(grid.GetLabelTextColour());
245 dc.SetFont(grid.GetLabelFont());
246 grid.DrawTextRectangle(dc, value, rect, horizAlign, vertAlign, textOrientation);
247}
248
249
250void wxGridRowHeaderRendererDefault::DrawBorder(const wxGrid& WXUNUSED(grid),
251 wxDC& dc,
252 wxRect& rect) const
253{
254 dc.SetPen(wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW)));
255 dc.DrawLine(rect.GetRight(), rect.GetTop(),
256 rect.GetRight(), rect.GetBottom());
257 dc.DrawLine(rect.GetLeft(), rect.GetTop(),
258 rect.GetLeft(), rect.GetBottom());
259 dc.DrawLine(rect.GetLeft(), rect.GetBottom(),
260 rect.GetRight() + 1, rect.GetBottom());
261
262 dc.SetPen(*wxWHITE_PEN);
263 dc.DrawLine(rect.GetLeft() + 1, rect.GetTop(),
264 rect.GetLeft() + 1, rect.GetBottom());
265 dc.DrawLine(rect.GetLeft() + 1, rect.GetTop(),
266 rect.GetRight(), rect.GetTop());
267
268 rect.Deflate(2);
269}
270
271void wxGridColumnHeaderRendererDefault::DrawBorder(const wxGrid& WXUNUSED(grid),
272 wxDC& dc,
273 wxRect& rect) const
274{
275 dc.SetPen(wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW)));
276 dc.DrawLine(rect.GetRight(), rect.GetTop(),
277 rect.GetRight(), rect.GetBottom());
278 dc.DrawLine(rect.GetLeft(), rect.GetTop(),
279 rect.GetRight(), rect.GetTop());
280 dc.DrawLine(rect.GetLeft(), rect.GetBottom(),
281 rect.GetRight() + 1, rect.GetBottom());
282
283 dc.SetPen(*wxWHITE_PEN);
284 dc.DrawLine(rect.GetLeft(), rect.GetTop() + 1,
285 rect.GetLeft(), rect.GetBottom());
286 dc.DrawLine(rect.GetLeft(), rect.GetTop() + 1,
287 rect.GetRight(), rect.GetTop() + 1);
288
289 rect.Deflate(2);
290}
291
292void wxGridCornerHeaderRendererDefault::DrawBorder(const wxGrid& WXUNUSED(grid),
293 wxDC& dc,
294 wxRect& rect) const
295{
296 dc.SetPen(wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW)));
297 dc.DrawLine(rect.GetRight() - 1, rect.GetBottom() - 1,
298 rect.GetRight() - 1, rect.GetTop());
299 dc.DrawLine(rect.GetRight() - 1, rect.GetBottom() - 1,
300 rect.GetLeft(), rect.GetBottom() - 1);
301 dc.DrawLine(rect.GetLeft(), rect.GetTop(),
302 rect.GetRight(), rect.GetTop());
303 dc.DrawLine(rect.GetLeft(), rect.GetTop(),
304 rect.GetLeft(), rect.GetBottom());
305
306 dc.SetPen(*wxWHITE_PEN);
307 dc.DrawLine(rect.GetLeft() + 1, rect.GetTop() + 1,
308 rect.GetRight() - 1, rect.GetTop() + 1);
309 dc.DrawLine(rect.GetLeft() + 1, rect.GetTop() + 1,
310 rect.GetLeft() + 1, rect.GetBottom() - 1);
311
312 rect.Deflate(2);
313}
314
2796cce3
RD
315// ----------------------------------------------------------------------------
316// wxGridCellAttr
317// ----------------------------------------------------------------------------
318
1df4050d
VZ
319void wxGridCellAttr::Init(wxGridCellAttr *attrDefault)
320{
1df4050d
VZ
321 m_isReadOnly = Unset;
322
323 m_renderer = NULL;
324 m_editor = NULL;
325
326 m_attrkind = wxGridCellAttr::Cell;
327
27f35b66 328 m_sizeRows = m_sizeCols = 1;
b63fce94 329 m_overflow = UnsetOverflow;
27f35b66 330
1df4050d
VZ
331 SetDefAttr(attrDefault);
332}
333
39bcce60 334wxGridCellAttr *wxGridCellAttr::Clone() const
a68c1246 335{
1df4050d
VZ
336 wxGridCellAttr *attr = new wxGridCellAttr(m_defGridAttr);
337
a68c1246
VZ
338 if ( HasTextColour() )
339 attr->SetTextColour(GetTextColour());
340 if ( HasBackgroundColour() )
341 attr->SetBackgroundColour(GetBackgroundColour());
342 if ( HasFont() )
343 attr->SetFont(GetFont());
344 if ( HasAlignment() )
345 attr->SetAlignment(m_hAlign, m_vAlign);
346
27f35b66
SN
347 attr->SetSize( m_sizeRows, m_sizeCols );
348
a68c1246
VZ
349 if ( m_renderer )
350 {
351 attr->SetRenderer(m_renderer);
39bcce60 352 m_renderer->IncRef();
a68c1246
VZ
353 }
354 if ( m_editor )
355 {
356 attr->SetEditor(m_editor);
39bcce60 357 m_editor->IncRef();
a68c1246
VZ
358 }
359
360 if ( IsReadOnly() )
361 attr->SetReadOnly();
362
dfd7c082 363 attr->SetOverflow( m_overflow == Overflow );
19d7140e
VZ
364 attr->SetKind( m_attrkind );
365
a68c1246
VZ
366 return attr;
367}
368
19d7140e
VZ
369void wxGridCellAttr::MergeWith(wxGridCellAttr *mergefrom)
370{
371 if ( !HasTextColour() && mergefrom->HasTextColour() )
372 SetTextColour(mergefrom->GetTextColour());
373 if ( !HasBackgroundColour() && mergefrom->HasBackgroundColour() )
374 SetBackgroundColour(mergefrom->GetBackgroundColour());
375 if ( !HasFont() && mergefrom->HasFont() )
376 SetFont(mergefrom->GetFont());
4db6714b
KH
377 if ( !HasAlignment() && mergefrom->HasAlignment() )
378 {
19d7140e
VZ
379 int hAlign, vAlign;
380 mergefrom->GetAlignment( &hAlign, &vAlign);
381 SetAlignment(hAlign, vAlign);
382 }
3100c3db
RD
383 if ( !HasSize() && mergefrom->HasSize() )
384 mergefrom->GetSize( &m_sizeRows, &m_sizeCols );
27f35b66 385
19d7140e
VZ
386 // Directly access member functions as GetRender/Editor don't just return
387 // m_renderer/m_editor
388 //
389 // Maybe add support for merge of Render and Editor?
390 if (!HasRenderer() && mergefrom->HasRenderer() )
bf7945ce 391 {
19d7140e
VZ
392 m_renderer = mergefrom->m_renderer;
393 m_renderer->IncRef();
394 }
395 if ( !HasEditor() && mergefrom->HasEditor() )
396 {
397 m_editor = mergefrom->m_editor;
398 m_editor->IncRef();
399 }
2f024384 400 if ( !HasReadWriteMode() && mergefrom->HasReadWriteMode() )
19d7140e
VZ
401 SetReadOnly(mergefrom->IsReadOnly());
402
2f024384 403 if (!HasOverflowMode() && mergefrom->HasOverflowMode() )
ff699386 404 SetOverflow(mergefrom->GetOverflow());
ef5df12b 405
19d7140e
VZ
406 SetDefAttr(mergefrom->m_defGridAttr);
407}
408
27f35b66
SN
409void wxGridCellAttr::SetSize(int num_rows, int num_cols)
410{
411 // The size of a cell is normally 1,1
412
413 // If this cell is larger (2,2) then this is the top left cell
414 // the other cells that will be covered (lower right cells) must be
415 // set to negative or zero values such that
416 // row + num_rows of the covered cell points to the larger cell (this cell)
417 // same goes for the col + num_cols.
418
419 // Size of 0,0 is NOT valid, neither is <=0 and any positive value
420
2f024384
DS
421 wxASSERT_MSG( (!((num_rows > 0) && (num_cols <= 0)) ||
422 !((num_rows <= 0) && (num_cols > 0)) ||
423 !((num_rows == 0) && (num_cols == 0))),
e75e8815 424 wxT("wxGridCellAttr::SetSize only takes two positive values or negative/zero values"));
27f35b66
SN
425
426 m_sizeRows = num_rows;
427 m_sizeCols = num_cols;
428}
429
2796cce3
RD
430const wxColour& wxGridCellAttr::GetTextColour() const
431{
432 if (HasTextColour())
508011ce 433 {
2796cce3 434 return m_colText;
508011ce 435 }
0926b2fc 436 else if (m_defGridAttr && m_defGridAttr != this)
508011ce 437 {
2796cce3 438 return m_defGridAttr->GetTextColour();
508011ce
VZ
439 }
440 else
441 {
2796cce3
RD
442 wxFAIL_MSG(wxT("Missing default cell attribute"));
443 return wxNullColour;
444 }
445}
446
2796cce3
RD
447const wxColour& wxGridCellAttr::GetBackgroundColour() const
448{
449 if (HasBackgroundColour())
2f024384 450 {
2796cce3 451 return m_colBack;
2f024384 452 }
0926b2fc 453 else if (m_defGridAttr && m_defGridAttr != this)
2f024384 454 {
2796cce3 455 return m_defGridAttr->GetBackgroundColour();
2f024384 456 }
508011ce
VZ
457 else
458 {
2796cce3
RD
459 wxFAIL_MSG(wxT("Missing default cell attribute"));
460 return wxNullColour;
461 }
462}
463
2796cce3
RD
464const wxFont& wxGridCellAttr::GetFont() const
465{
466 if (HasFont())
2f024384 467 {
2796cce3 468 return m_font;
2f024384 469 }
0926b2fc 470 else if (m_defGridAttr && m_defGridAttr != this)
2f024384 471 {
2796cce3 472 return m_defGridAttr->GetFont();
2f024384 473 }
508011ce
VZ
474 else
475 {
2796cce3
RD
476 wxFAIL_MSG(wxT("Missing default cell attribute"));
477 return wxNullFont;
478 }
479}
480
2796cce3
RD
481void wxGridCellAttr::GetAlignment(int *hAlign, int *vAlign) const
482{
508011ce
VZ
483 if (HasAlignment())
484 {
4db6714b
KH
485 if ( hAlign )
486 *hAlign = m_hAlign;
487 if ( vAlign )
488 *vAlign = m_vAlign;
2796cce3 489 }
0926b2fc 490 else if (m_defGridAttr && m_defGridAttr != this)
c2f5b920 491 {
2796cce3 492 m_defGridAttr->GetAlignment(hAlign, vAlign);
c2f5b920 493 }
508011ce
VZ
494 else
495 {
2796cce3
RD
496 wxFAIL_MSG(wxT("Missing default cell attribute"));
497 }
498}
499
cfbc15ee
VZ
500void wxGridCellAttr::GetNonDefaultAlignment(int *hAlign, int *vAlign) const
501{
502 if ( hAlign && m_hAlign != wxALIGN_INVALID )
503 *hAlign = m_hAlign;
504
505 if ( vAlign && m_vAlign != wxALIGN_INVALID )
506 *vAlign = m_vAlign;
507}
508
27f35b66
SN
509void wxGridCellAttr::GetSize( int *num_rows, int *num_cols ) const
510{
4db6714b
KH
511 if ( num_rows )
512 *num_rows = m_sizeRows;
513 if ( num_cols )
514 *num_cols = m_sizeCols;
27f35b66 515}
2796cce3 516
f2d76237 517// GetRenderer and GetEditor use a slightly different decision path about
28a77bc4
RD
518// which attribute to use. If a non-default attr object has one then it is
519// used, otherwise the default editor or renderer is fetched from the grid and
520// used. It should be the default for the data type of the cell. If it is
521// NULL (because the table has a type that the grid does not have in its
c2f5b920 522// registry), then the grid's default editor or renderer is used.
28a77bc4 523
ef316e23 524wxGridCellRenderer* wxGridCellAttr::GetRenderer(const wxGrid* grid, int row, int col) const
28a77bc4 525{
c2f5b920 526 wxGridCellRenderer *renderer = NULL;
28a77bc4 527
3cf883a2 528 if ( m_renderer && this != m_defGridAttr )
0b190b0f 529 {
3cf883a2
VZ
530 // use the cells renderer if it has one
531 renderer = m_renderer;
532 renderer->IncRef();
0b190b0f 533 }
2f024384 534 else // no non-default cell renderer
0b190b0f 535 {
3cf883a2
VZ
536 // get default renderer for the data type
537 if ( grid )
538 {
539 // GetDefaultRendererForCell() will do IncRef() for us
540 renderer = grid->GetDefaultRendererForCell(row, col);
541 }
0b190b0f 542
c2f5b920 543 if ( renderer == NULL )
3cf883a2 544 {
c2f5b920 545 if ( (m_defGridAttr != NULL) && (m_defGridAttr != this) )
3cf883a2
VZ
546 {
547 // if we still don't have one then use the grid default
548 // (no need for IncRef() here neither)
549 renderer = m_defGridAttr->GetRenderer(NULL, 0, 0);
550 }
551 else // default grid attr
552 {
553 // use m_renderer which we had decided not to use initially
554 renderer = m_renderer;
555 if ( renderer )
556 renderer->IncRef();
557 }
558 }
0b190b0f 559 }
28a77bc4 560
3cf883a2
VZ
561 // we're supposed to always find something
562 wxASSERT_MSG(renderer, wxT("Missing default cell renderer"));
28a77bc4
RD
563
564 return renderer;
2796cce3
RD
565}
566
3cf883a2 567// same as above, except for s/renderer/editor/g
ef316e23 568wxGridCellEditor* wxGridCellAttr::GetEditor(const wxGrid* grid, int row, int col) const
07296f0b 569{
c2f5b920 570 wxGridCellEditor *editor = NULL;
0b190b0f 571
3cf883a2 572 if ( m_editor && this != m_defGridAttr )
0b190b0f 573 {
3cf883a2
VZ
574 // use the cells editor if it has one
575 editor = m_editor;
576 editor->IncRef();
0b190b0f 577 }
3cf883a2 578 else // no non default cell editor
0b190b0f 579 {
3cf883a2
VZ
580 // get default editor for the data type
581 if ( grid )
582 {
583 // GetDefaultEditorForCell() will do IncRef() for us
584 editor = grid->GetDefaultEditorForCell(row, col);
585 }
3cf883a2 586
c2f5b920 587 if ( editor == NULL )
3cf883a2 588 {
c2f5b920 589 if ( (m_defGridAttr != NULL) && (m_defGridAttr != this) )
3cf883a2
VZ
590 {
591 // if we still don't have one then use the grid default
592 // (no need for IncRef() here neither)
593 editor = m_defGridAttr->GetEditor(NULL, 0, 0);
594 }
595 else // default grid attr
596 {
597 // use m_editor which we had decided not to use initially
598 editor = m_editor;
599 if ( editor )
600 editor->IncRef();
601 }
602 }
0b190b0f 603 }
28a77bc4 604
3cf883a2
VZ
605 // we're supposed to always find something
606 wxASSERT_MSG(editor, wxT("Missing default cell editor"));
607
28a77bc4 608 return editor;
07296f0b
RD
609}
610
b99be8fb 611// ----------------------------------------------------------------------------
758cbedf 612// wxGridCellAttrData
b99be8fb
VZ
613// ----------------------------------------------------------------------------
614
758cbedf 615void wxGridCellAttrData::SetAttr(wxGridCellAttr *attr, int row, int col)
b99be8fb 616{
96ca74cd
SN
617 // Note: contrary to wxGridRowOrColAttrData::SetAttr, we must not
618 // touch attribute's reference counting explicitly, since this
619 // is managed by class wxGridCellWithAttr
b99be8fb
VZ
620 int n = FindIndex(row, col);
621 if ( n == wxNOT_FOUND )
622 {
a70517e9
VZ
623 if ( attr )
624 {
625 // add the attribute
626 m_attrs.Add(new wxGridCellWithAttr(row, col, attr));
627 }
628 //else: nothing to do
b99be8fb 629 }
6f292345 630 else // we already have an attribute for this cell
b99be8fb
VZ
631 {
632 if ( attr )
633 {
634 // change the attribute
96ca74cd 635 m_attrs[(size_t)n].ChangeAttr(attr);
b99be8fb
VZ
636 }
637 else
638 {
639 // remove this attribute
640 m_attrs.RemoveAt((size_t)n);
641 }
642 }
b99be8fb
VZ
643}
644
758cbedf 645wxGridCellAttr *wxGridCellAttrData::GetAttr(int row, int col) const
b99be8fb 646{
10a4531d 647 wxGridCellAttr *attr = NULL;
b99be8fb
VZ
648
649 int n = FindIndex(row, col);
650 if ( n != wxNOT_FOUND )
651 {
2e9a6788
VZ
652 attr = m_attrs[(size_t)n].attr;
653 attr->IncRef();
b99be8fb
VZ
654 }
655
656 return attr;
657}
658
4d60017a
SN
659void wxGridCellAttrData::UpdateAttrRows( size_t pos, int numRows )
660{
661 size_t count = m_attrs.GetCount();
662 for ( size_t n = 0; n < count; n++ )
663 {
664 wxGridCellCoords& coords = m_attrs[n].coords;
d1c0b4f9
VZ
665 wxCoord row = coords.GetRow();
666 if ((size_t)row >= pos)
667 {
668 if (numRows > 0)
669 {
670 // If rows inserted, include row counter where necessary
671 coords.SetRow(row + numRows);
672 }
673 else if (numRows < 0)
674 {
675 // If rows deleted ...
676 if ((size_t)row >= pos - numRows)
677 {
678 // ...either decrement row counter (if row still exists)...
679 coords.SetRow(row + numRows);
680 }
681 else
682 {
683 // ...or remove the attribute
01dd42b6 684 m_attrs.RemoveAt(n);
4db6714b
KH
685 n--;
686 count--;
d1c0b4f9
VZ
687 }
688 }
4d60017a
SN
689 }
690 }
691}
692
693void wxGridCellAttrData::UpdateAttrCols( size_t pos, int numCols )
694{
695 size_t count = m_attrs.GetCount();
696 for ( size_t n = 0; n < count; n++ )
697 {
698 wxGridCellCoords& coords = m_attrs[n].coords;
d1c0b4f9
VZ
699 wxCoord col = coords.GetCol();
700 if ( (size_t)col >= pos )
701 {
702 if ( numCols > 0 )
703 {
704 // If rows inserted, include row counter where necessary
705 coords.SetCol(col + numCols);
706 }
707 else if (numCols < 0)
708 {
709 // If rows deleted ...
710 if ((size_t)col >= pos - numCols)
711 {
712 // ...either decrement row counter (if row still exists)...
713 coords.SetCol(col + numCols);
714 }
715 else
716 {
717 // ...or remove the attribute
01dd42b6 718 m_attrs.RemoveAt(n);
2f024384
DS
719 n--;
720 count--;
d1c0b4f9
VZ
721 }
722 }
4d60017a
SN
723 }
724 }
725}
726
758cbedf 727int wxGridCellAttrData::FindIndex(int row, int col) const
b99be8fb
VZ
728{
729 size_t count = m_attrs.GetCount();
730 for ( size_t n = 0; n < count; n++ )
731 {
732 const wxGridCellCoords& coords = m_attrs[n].coords;
733 if ( (coords.GetRow() == row) && (coords.GetCol() == col) )
734 {
735 return n;
736 }
737 }
738
739 return wxNOT_FOUND;
740}
741
758cbedf
VZ
742// ----------------------------------------------------------------------------
743// wxGridRowOrColAttrData
744// ----------------------------------------------------------------------------
745
746wxGridRowOrColAttrData::~wxGridRowOrColAttrData()
747{
b4a980f4 748 size_t count = m_attrs.GetCount();
758cbedf
VZ
749 for ( size_t n = 0; n < count; n++ )
750 {
751 m_attrs[n]->DecRef();
752 }
753}
754
755wxGridCellAttr *wxGridRowOrColAttrData::GetAttr(int rowOrCol) const
756{
10a4531d 757 wxGridCellAttr *attr = NULL;
758cbedf
VZ
758
759 int n = m_rowsOrCols.Index(rowOrCol);
760 if ( n != wxNOT_FOUND )
761 {
762 attr = m_attrs[(size_t)n];
763 attr->IncRef();
764 }
765
766 return attr;
767}
768
769void wxGridRowOrColAttrData::SetAttr(wxGridCellAttr *attr, int rowOrCol)
770{
a95e38c0
VZ
771 int i = m_rowsOrCols.Index(rowOrCol);
772 if ( i == wxNOT_FOUND )
758cbedf 773 {
62d249cb
VZ
774 if ( attr )
775 {
4a53f701 776 // store the new attribute, taking its ownership
62d249cb
VZ
777 m_rowsOrCols.Add(rowOrCol);
778 m_attrs.Add(attr);
779 }
780 // nothing to remove
758cbedf 781 }
4a53f701 782 else // we have an attribute for this row or column
758cbedf 783 {
a95e38c0 784 size_t n = (size_t)i;
4a53f701
VZ
785
786 // notice that this code works correctly even when the old attribute is
787 // the same as the new one: as we own of it, we must call DecRef() on
788 // it in any case and this won't result in destruction of the new
789 // attribute if it's the same as old one because it must have ref count
790 // of at least 2 to be passed to us while we keep a reference to it too
791 m_attrs[n]->DecRef();
792
758cbedf
VZ
793 if ( attr )
794 {
4a53f701 795 // replace the attribute with the new one
a95e38c0 796 m_attrs[n] = attr;
758cbedf 797 }
4a53f701 798 else // remove the attribute
758cbedf 799 {
a95e38c0
VZ
800 m_rowsOrCols.RemoveAt(n);
801 m_attrs.RemoveAt(n);
758cbedf
VZ
802 }
803 }
804}
805
4d60017a
SN
806void wxGridRowOrColAttrData::UpdateAttrRowsOrCols( size_t pos, int numRowsOrCols )
807{
808 size_t count = m_attrs.GetCount();
809 for ( size_t n = 0; n < count; n++ )
810 {
811 int & rowOrCol = m_rowsOrCols[n];
d1c0b4f9
VZ
812 if ( (size_t)rowOrCol >= pos )
813 {
814 if ( numRowsOrCols > 0 )
815 {
816 // If rows inserted, include row counter where necessary
817 rowOrCol += numRowsOrCols;
818 }
819 else if ( numRowsOrCols < 0)
820 {
821 // If rows deleted, either decrement row counter (if row still exists)
822 if ((size_t)rowOrCol >= pos - numRowsOrCols)
823 rowOrCol += numRowsOrCols;
824 else
825 {
01dd42b6
VZ
826 m_rowsOrCols.RemoveAt(n);
827 m_attrs[n]->DecRef();
828 m_attrs.RemoveAt(n);
4db6714b
KH
829 n--;
830 count--;
d1c0b4f9
VZ
831 }
832 }
4d60017a
SN
833 }
834 }
835}
836
b99be8fb
VZ
837// ----------------------------------------------------------------------------
838// wxGridCellAttrProvider
839// ----------------------------------------------------------------------------
840
841wxGridCellAttrProvider::wxGridCellAttrProvider()
842{
10a4531d 843 m_data = NULL;
b99be8fb
VZ
844}
845
846wxGridCellAttrProvider::~wxGridCellAttrProvider()
847{
848 delete m_data;
849}
850
851void wxGridCellAttrProvider::InitData()
852{
853 m_data = new wxGridCellAttrProviderData;
854}
855
19d7140e
VZ
856wxGridCellAttr *wxGridCellAttrProvider::GetAttr(int row, int col,
857 wxGridCellAttr::wxAttrKind kind ) const
b99be8fb 858{
10a4531d 859 wxGridCellAttr *attr = NULL;
758cbedf
VZ
860 if ( m_data )
861 {
962a48f6 862 switch (kind)
758cbedf 863 {
19d7140e 864 case (wxGridCellAttr::Any):
962a48f6
DS
865 // Get cached merge attributes.
866 // Currently not used as no cache implemented as not mutable
19d7140e 867 // attr = m_data->m_mergeAttr.GetAttr(row, col);
4db6714b 868 if (!attr)
19d7140e 869 {
962a48f6
DS
870 // Basically implement old version.
871 // Also check merge cache, so we don't have to re-merge every time..
999836aa
VZ
872 wxGridCellAttr *attrcell = m_data->m_cellAttrs.GetAttr(row, col);
873 wxGridCellAttr *attrrow = m_data->m_rowAttrs.GetAttr(row);
874 wxGridCellAttr *attrcol = m_data->m_colAttrs.GetAttr(col);
19d7140e 875
4db6714b
KH
876 if ((attrcell != attrrow) && (attrrow != attrcol) && (attrcell != attrcol))
877 {
2d0c2e79 878 // Two or more are non NULL
19d7140e
VZ
879 attr = new wxGridCellAttr;
880 attr->SetKind(wxGridCellAttr::Merged);
881
962a48f6 882 // Order is important..
4db6714b
KH
883 if (attrcell)
884 {
19d7140e
VZ
885 attr->MergeWith(attrcell);
886 attrcell->DecRef();
887 }
4db6714b
KH
888 if (attrcol)
889 {
19d7140e
VZ
890 attr->MergeWith(attrcol);
891 attrcol->DecRef();
892 }
4db6714b
KH
893 if (attrrow)
894 {
19d7140e
VZ
895 attr->MergeWith(attrrow);
896 attrrow->DecRef();
897 }
962a48f6
DS
898
899 // store merge attr if cache implemented
19d7140e
VZ
900 //attr->IncRef();
901 //m_data->m_mergeAttr.SetAttr(attr, row, col);
902 }
903 else
2d0c2e79 904 {
19d7140e 905 // one or none is non null return it or null.
4db6714b
KH
906 if (attrrow)
907 attr = attrrow;
908 if (attrcol)
2d0c2e79 909 {
962a48f6 910 if (attr)
2d0c2e79
RD
911 attr->DecRef();
912 attr = attrcol;
913 }
4db6714b 914 if (attrcell)
2d0c2e79 915 {
4db6714b 916 if (attr)
2d0c2e79
RD
917 attr->DecRef();
918 attr = attrcell;
919 }
19d7140e 920 }
29efc6e4
FM
921 }
922 break;
f2d76237 923
29efc6e4
FM
924 case (wxGridCellAttr::Cell):
925 attr = m_data->m_cellAttrs.GetAttr(row, col);
926 break;
927
928 case (wxGridCellAttr::Col):
929 attr = m_data->m_colAttrs.GetAttr(col);
930 break;
931
932 case (wxGridCellAttr::Row):
933 attr = m_data->m_rowAttrs.GetAttr(row);
934 break;
935
936 default:
937 // unused as yet...
938 // (wxGridCellAttr::Default):
939 // (wxGridCellAttr::Merged):
940 break;
941 }
c4608a8a
VZ
942 }
943
29efc6e4 944 return attr;
c4608a8a
VZ
945}
946
29efc6e4
FM
947void wxGridCellAttrProvider::SetAttr(wxGridCellAttr *attr,
948 int row, int col)
c4608a8a 949{
29efc6e4
FM
950 if ( !m_data )
951 InitData();
c4608a8a 952
29efc6e4
FM
953 m_data->m_cellAttrs.SetAttr(attr, row, col);
954}
c4608a8a 955
29efc6e4
FM
956void wxGridCellAttrProvider::SetRowAttr(wxGridCellAttr *attr, int row)
957{
958 if ( !m_data )
959 InitData();
c4608a8a 960
29efc6e4
FM
961 m_data->m_rowAttrs.SetAttr(attr, row);
962}
c4608a8a 963
29efc6e4
FM
964void wxGridCellAttrProvider::SetColAttr(wxGridCellAttr *attr, int col)
965{
966 if ( !m_data )
967 InitData();
f2d76237 968
29efc6e4 969 m_data->m_colAttrs.SetAttr(attr, col);
f2d76237
RD
970}
971
29efc6e4 972void wxGridCellAttrProvider::UpdateAttrRows( size_t pos, int numRows )
f2d76237 973{
29efc6e4
FM
974 if ( m_data )
975 {
976 m_data->m_cellAttrs.UpdateAttrRows( pos, numRows );
2f024384 977
29efc6e4
FM
978 m_data->m_rowAttrs.UpdateAttrRowsOrCols( pos, numRows );
979 }
f2d76237
RD
980}
981
29efc6e4 982void wxGridCellAttrProvider::UpdateAttrCols( size_t pos, int numCols )
f2d76237 983{
29efc6e4
FM
984 if ( m_data )
985 {
986 m_data->m_cellAttrs.UpdateAttrCols( pos, numCols );
2f024384 987
29efc6e4
FM
988 m_data->m_colAttrs.UpdateAttrRowsOrCols( pos, numCols );
989 }
f2d76237
RD
990}
991
670d0177
VZ
992const wxGridColumnHeaderRenderer&
993wxGridCellAttrProvider::GetColumnHeaderRenderer(int WXUNUSED(col))
994{
995 return gs_defaultHeaderRenderers.colRenderer;
996}
997
998const wxGridRowHeaderRenderer&
999wxGridCellAttrProvider::GetRowHeaderRenderer(int WXUNUSED(row))
1000{
1001 return gs_defaultHeaderRenderers.rowRenderer;
1002}
1003
1004const wxGridCornerHeaderRenderer& wxGridCellAttrProvider::GetCornerRenderer()
1005{
1006 return gs_defaultHeaderRenderers.cornerRenderer;
1007}
1008
758cbedf
VZ
1009// ----------------------------------------------------------------------------
1010// wxGridTableBase
1011// ----------------------------------------------------------------------------
1012
f85afd4e
MB
1013IMPLEMENT_ABSTRACT_CLASS( wxGridTableBase, wxObject )
1014
f85afd4e 1015wxGridTableBase::wxGridTableBase()
f85afd4e 1016{
10a4531d
VZ
1017 m_view = NULL;
1018 m_attrProvider = NULL;
f85afd4e
MB
1019}
1020
1021wxGridTableBase::~wxGridTableBase()
1022{
b99be8fb
VZ
1023 delete m_attrProvider;
1024}
1025
1026void wxGridTableBase::SetAttrProvider(wxGridCellAttrProvider *attrProvider)
1027{
1028 delete m_attrProvider;
1029 m_attrProvider = attrProvider;
f85afd4e
MB
1030}
1031
f2d76237
RD
1032bool wxGridTableBase::CanHaveAttributes()
1033{
1034 if ( ! GetAttrProvider() )
1035 {
1036 // use the default attr provider by default
1037 SetAttrProvider(new wxGridCellAttrProvider);
1038 }
2f024384 1039
ca65c044 1040 return true;
f2d76237
RD
1041}
1042
19d7140e 1043wxGridCellAttr *wxGridTableBase::GetAttr(int row, int col, wxGridCellAttr::wxAttrKind kind)
b99be8fb
VZ
1044{
1045 if ( m_attrProvider )
19d7140e 1046 return m_attrProvider->GetAttr(row, col, kind);
b99be8fb 1047 else
10a4531d 1048 return NULL;
b99be8fb
VZ
1049}
1050
758cbedf 1051void wxGridTableBase::SetAttr(wxGridCellAttr* attr, int row, int col)
b99be8fb
VZ
1052{
1053 if ( m_attrProvider )
1054 {
6f292345
VZ
1055 if ( attr )
1056 attr->SetKind(wxGridCellAttr::Cell);
b99be8fb
VZ
1057 m_attrProvider->SetAttr(attr, row, col);
1058 }
1059 else
1060 {
1061 // as we take ownership of the pointer and don't store it, we must
1062 // free it now
39bcce60 1063 wxSafeDecRef(attr);
b99be8fb
VZ
1064 }
1065}
1066
758cbedf
VZ
1067void wxGridTableBase::SetRowAttr(wxGridCellAttr *attr, int row)
1068{
1069 if ( m_attrProvider )
1070 {
19d7140e 1071 attr->SetKind(wxGridCellAttr::Row);
758cbedf
VZ
1072 m_attrProvider->SetRowAttr(attr, row);
1073 }
1074 else
1075 {
1076 // as we take ownership of the pointer and don't store it, we must
1077 // free it now
39bcce60 1078 wxSafeDecRef(attr);
758cbedf
VZ
1079 }
1080}
1081
1082void wxGridTableBase::SetColAttr(wxGridCellAttr *attr, int col)
1083{
1084 if ( m_attrProvider )
1085 {
19d7140e 1086 attr->SetKind(wxGridCellAttr::Col);
758cbedf
VZ
1087 m_attrProvider->SetColAttr(attr, col);
1088 }
1089 else
1090 {
1091 // as we take ownership of the pointer and don't store it, we must
1092 // free it now
39bcce60 1093 wxSafeDecRef(attr);
758cbedf
VZ
1094 }
1095}
1096
aa5e1f75
SN
1097bool wxGridTableBase::InsertRows( size_t WXUNUSED(pos),
1098 size_t WXUNUSED(numRows) )
f85afd4e 1099{
f6bcfd97 1100 wxFAIL_MSG( wxT("Called grid table class function InsertRows\nbut your derived table class does not override this function") );
8f177c8e 1101
ca65c044 1102 return false;
f85afd4e
MB
1103}
1104
aa5e1f75 1105bool wxGridTableBase::AppendRows( size_t WXUNUSED(numRows) )
f85afd4e 1106{
f6bcfd97 1107 wxFAIL_MSG( wxT("Called grid table class function AppendRows\nbut your derived table class does not override this function"));
8f177c8e 1108
ca65c044 1109 return false;
f85afd4e
MB
1110}
1111
aa5e1f75
SN
1112bool wxGridTableBase::DeleteRows( size_t WXUNUSED(pos),
1113 size_t WXUNUSED(numRows) )
f85afd4e 1114{
f6bcfd97 1115 wxFAIL_MSG( wxT("Called grid table class function DeleteRows\nbut your derived table class does not override this function"));
8f177c8e 1116
ca65c044 1117 return false;
f85afd4e
MB
1118}
1119
aa5e1f75
SN
1120bool wxGridTableBase::InsertCols( size_t WXUNUSED(pos),
1121 size_t WXUNUSED(numCols) )
f85afd4e 1122{
f6bcfd97 1123 wxFAIL_MSG( wxT("Called grid table class function InsertCols\nbut your derived table class does not override this function"));
8f177c8e 1124
ca65c044 1125 return false;
f85afd4e
MB
1126}
1127
aa5e1f75 1128bool wxGridTableBase::AppendCols( size_t WXUNUSED(numCols) )
f85afd4e 1129{
f6bcfd97 1130 wxFAIL_MSG(wxT("Called grid table class function AppendCols\nbut your derived table class does not override this function"));
8f177c8e 1131
ca65c044 1132 return false;
f85afd4e
MB
1133}
1134
aa5e1f75
SN
1135bool wxGridTableBase::DeleteCols( size_t WXUNUSED(pos),
1136 size_t WXUNUSED(numCols) )
f85afd4e 1137{
f6bcfd97 1138 wxFAIL_MSG( wxT("Called grid table class function DeleteCols\nbut your derived table class does not override this function"));
8f177c8e 1139
ca65c044 1140 return false;
f85afd4e
MB
1141}
1142
f85afd4e
MB
1143wxString wxGridTableBase::GetRowLabelValue( int row )
1144{
1145 wxString s;
93763ad5 1146
2f024384
DS
1147 // RD: Starting the rows at zero confuses users,
1148 // no matter how much it makes sense to us geeks.
1149 s << row + 1;
1150
f85afd4e
MB
1151 return s;
1152}
1153
1154wxString wxGridTableBase::GetColLabelValue( int col )
1155{
1156 // default col labels are:
1157 // cols 0 to 25 : A-Z
1158 // cols 26 to 675 : AA-ZZ
1159 // etc.
1160
1161 wxString s;
1162 unsigned int i, n;
1163 for ( n = 1; ; n++ )
1164 {
9a83f860 1165 s += (wxChar) (wxT('A') + (wxChar)(col % 26));
2f024384 1166 col = col / 26 - 1;
4db6714b
KH
1167 if ( col < 0 )
1168 break;
f85afd4e
MB
1169 }
1170
1171 // reverse the string...
1172 wxString s2;
3d59537f 1173 for ( i = 0; i < n; i++ )
f85afd4e 1174 {
2f024384 1175 s2 += s[n - i - 1];
f85afd4e
MB
1176 }
1177
1178 return s2;
1179}
1180
f2d76237
RD
1181wxString wxGridTableBase::GetTypeName( int WXUNUSED(row), int WXUNUSED(col) )
1182{
816be743 1183 return wxGRID_VALUE_STRING;
f2d76237
RD
1184}
1185
1186bool wxGridTableBase::CanGetValueAs( int WXUNUSED(row), int WXUNUSED(col),
1187 const wxString& typeName )
1188{
816be743 1189 return typeName == wxGRID_VALUE_STRING;
f2d76237
RD
1190}
1191
1192bool wxGridTableBase::CanSetValueAs( int row, int col, const wxString& typeName )
1193{
1194 return CanGetValueAs(row, col, typeName);
1195}
1196
1197long wxGridTableBase::GetValueAsLong( int WXUNUSED(row), int WXUNUSED(col) )
1198{
1199 return 0;
1200}
1201
1202double wxGridTableBase::GetValueAsDouble( int WXUNUSED(row), int WXUNUSED(col) )
1203{
1204 return 0.0;
1205}
1206
1207bool wxGridTableBase::GetValueAsBool( int WXUNUSED(row), int WXUNUSED(col) )
1208{
ca65c044 1209 return false;
f2d76237
RD
1210}
1211
1212void wxGridTableBase::SetValueAsLong( int WXUNUSED(row), int WXUNUSED(col),
1213 long WXUNUSED(value) )
1214{
1215}
1216
1217void wxGridTableBase::SetValueAsDouble( int WXUNUSED(row), int WXUNUSED(col),
1218 double WXUNUSED(value) )
1219{
1220}
1221
1222void wxGridTableBase::SetValueAsBool( int WXUNUSED(row), int WXUNUSED(col),
1223 bool WXUNUSED(value) )
1224{
1225}
1226
f2d76237
RD
1227void* wxGridTableBase::GetValueAsCustom( int WXUNUSED(row), int WXUNUSED(col),
1228 const wxString& WXUNUSED(typeName) )
1229{
1230 return NULL;
1231}
1232
1233void wxGridTableBase::SetValueAsCustom( int WXUNUSED(row), int WXUNUSED(col),
1234 const wxString& WXUNUSED(typeName),
1235 void* WXUNUSED(value) )
1236{
1237}
1238
f85afd4e
MB
1239//////////////////////////////////////////////////////////////////////
1240//
1241// Message class for the grid table to send requests and notifications
1242// to the grid view
1243//
1244
1245wxGridTableMessage::wxGridTableMessage()
1246{
10a4531d 1247 m_table = NULL;
f85afd4e
MB
1248 m_id = -1;
1249 m_comInt1 = -1;
1250 m_comInt2 = -1;
1251}
1252
1253wxGridTableMessage::wxGridTableMessage( wxGridTableBase *table, int id,
1254 int commandInt1, int commandInt2 )
1255{
1256 m_table = table;
1257 m_id = id;
1258 m_comInt1 = commandInt1;
1259 m_comInt2 = commandInt2;
1260}
1261
f85afd4e
MB
1262//////////////////////////////////////////////////////////////////////
1263//
1264// A basic grid table for string data. An object of this class will
1265// created by wxGrid if you don't specify an alternative table class.
1266//
1267
223d09f6 1268WX_DEFINE_OBJARRAY(wxGridStringArray)
f85afd4e
MB
1269
1270IMPLEMENT_DYNAMIC_CLASS( wxGridStringTable, wxGridTableBase )
1271
1272wxGridStringTable::wxGridStringTable()
1273 : wxGridTableBase()
1274{
5c3313a9 1275 m_numCols = 0;
f85afd4e
MB
1276}
1277
1278wxGridStringTable::wxGridStringTable( int numRows, int numCols )
1279 : wxGridTableBase()
1280{
5c3313a9
VZ
1281 m_numCols = numCols;
1282
f85afd4e
MB
1283 m_data.Alloc( numRows );
1284
1285 wxArrayString sa;
1286 sa.Alloc( numCols );
27f35b66 1287 sa.Add( wxEmptyString, numCols );
8f177c8e 1288
27f35b66 1289 m_data.Add( sa, numRows );
f85afd4e
MB
1290}
1291
f85afd4e
MB
1292wxString wxGridStringTable::GetValue( int row, int col )
1293{
57e9abd6
VZ
1294 wxCHECK_MSG( (row >= 0 && row < GetNumberRows()) &&
1295 (col >= 0 && col < GetNumberCols()),
3e13956a 1296 wxEmptyString,
9a83f860 1297 wxT("invalid row or column index in wxGridStringTable") );
af547d51 1298
f85afd4e
MB
1299 return m_data[row][col];
1300}
1301
f2d76237 1302void wxGridStringTable::SetValue( int row, int col, const wxString& value )
f85afd4e 1303{
57e9abd6
VZ
1304 wxCHECK_RET( (row >= 0 && row < GetNumberRows()) &&
1305 (col >= 0 && col < GetNumberCols()),
9a83f860 1306 wxT("invalid row or column index in wxGridStringTable") );
af547d51 1307
f2d76237 1308 m_data[row][col] = value;
f85afd4e
MB
1309}
1310
f85afd4e
MB
1311void wxGridStringTable::Clear()
1312{
1313 int row, col;
1314 int numRows, numCols;
8f177c8e 1315
f85afd4e
MB
1316 numRows = m_data.GetCount();
1317 if ( numRows > 0 )
1318 {
1319 numCols = m_data[0].GetCount();
1320
3d59537f 1321 for ( row = 0; row < numRows; row++ )
f85afd4e 1322 {
3d59537f 1323 for ( col = 0; col < numCols; col++ )
f85afd4e
MB
1324 {
1325 m_data[row][col] = wxEmptyString;
1326 }
1327 }
1328 }
1329}
1330
f85afd4e
MB
1331bool wxGridStringTable::InsertRows( size_t pos, size_t numRows )
1332{
f85afd4e 1333 size_t curNumRows = m_data.GetCount();
f6bcfd97
BP
1334 size_t curNumCols = ( curNumRows > 0 ? m_data[0].GetCount() :
1335 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
8f177c8e 1336
f85afd4e
MB
1337 if ( pos >= curNumRows )
1338 {
1339 return AppendRows( numRows );
1340 }
8f177c8e 1341
f85afd4e
MB
1342 wxArrayString sa;
1343 sa.Alloc( curNumCols );
27f35b66
SN
1344 sa.Add( wxEmptyString, curNumCols );
1345 m_data.Insert( sa, pos, numRows );
2f024384 1346
f85afd4e
MB
1347 if ( GetView() )
1348 {
1349 wxGridTableMessage msg( this,
1350 wxGRIDTABLE_NOTIFY_ROWS_INSERTED,
1351 pos,
1352 numRows );
8f177c8e 1353
f85afd4e
MB
1354 GetView()->ProcessTableMessage( msg );
1355 }
1356
ca65c044 1357 return true;
f85afd4e
MB
1358}
1359
1360bool wxGridStringTable::AppendRows( size_t numRows )
1361{
f85afd4e 1362 size_t curNumRows = m_data.GetCount();
4db6714b
KH
1363 size_t curNumCols = ( curNumRows > 0
1364 ? m_data[0].GetCount()
1365 : ( GetView() ? GetView()->GetNumberCols() : 0 ) );
8f177c8e 1366
f85afd4e
MB
1367 wxArrayString sa;
1368 if ( curNumCols > 0 )
1369 {
1370 sa.Alloc( curNumCols );
27f35b66 1371 sa.Add( wxEmptyString, curNumCols );
f85afd4e 1372 }
8f177c8e 1373
27f35b66 1374 m_data.Add( sa, numRows );
f85afd4e
MB
1375
1376 if ( GetView() )
1377 {
1378 wxGridTableMessage msg( this,
1379 wxGRIDTABLE_NOTIFY_ROWS_APPENDED,
1380 numRows );
8f177c8e 1381
f85afd4e
MB
1382 GetView()->ProcessTableMessage( msg );
1383 }
1384
ca65c044 1385 return true;
f85afd4e
MB
1386}
1387
1388bool wxGridStringTable::DeleteRows( size_t pos, size_t numRows )
1389{
f85afd4e 1390 size_t curNumRows = m_data.GetCount();
8f177c8e 1391
f85afd4e
MB
1392 if ( pos >= curNumRows )
1393 {
e91d2033
VZ
1394 wxFAIL_MSG( wxString::Format
1395 (
1396 wxT("Called wxGridStringTable::DeleteRows(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu rows"),
1397 (unsigned long)pos,
1398 (unsigned long)numRows,
1399 (unsigned long)curNumRows
1400 ) );
1401
ca65c044 1402 return false;
f85afd4e
MB
1403 }
1404
1405 if ( numRows > curNumRows - pos )
1406 {
1407 numRows = curNumRows - pos;
1408 }
8f177c8e 1409
f85afd4e
MB
1410 if ( numRows >= curNumRows )
1411 {
d57ad377 1412 m_data.Clear();
f85afd4e
MB
1413 }
1414 else
1415 {
27f35b66 1416 m_data.RemoveAt( pos, numRows );
f85afd4e 1417 }
4db6714b 1418
f85afd4e
MB
1419 if ( GetView() )
1420 {
1421 wxGridTableMessage msg( this,
1422 wxGRIDTABLE_NOTIFY_ROWS_DELETED,
1423 pos,
1424 numRows );
8f177c8e 1425
f85afd4e
MB
1426 GetView()->ProcessTableMessage( msg );
1427 }
1428
ca65c044 1429 return true;
f85afd4e
MB
1430}
1431
1432bool wxGridStringTable::InsertCols( size_t pos, size_t numCols )
1433{
1434 size_t row, col;
1435
1436 size_t curNumRows = m_data.GetCount();
4db6714b
KH
1437 size_t curNumCols = ( curNumRows > 0
1438 ? m_data[0].GetCount()
1439 : ( GetView() ? GetView()->GetNumberCols() : 0 ) );
8f177c8e 1440
f85afd4e
MB
1441 if ( pos >= curNumCols )
1442 {
1443 return AppendCols( numCols );
1444 }
1445
d4175745
VZ
1446 if ( !m_colLabels.IsEmpty() )
1447 {
1448 m_colLabels.Insert( wxEmptyString, pos, numCols );
1449
1450 size_t i;
1451 for ( i = pos; i < pos + numCols; i++ )
1452 m_colLabels[i] = wxGridTableBase::GetColLabelValue( i );
1453 }
1454
3d59537f 1455 for ( row = 0; row < curNumRows; row++ )
f85afd4e 1456 {
3d59537f 1457 for ( col = pos; col < pos + numCols; col++ )
f85afd4e
MB
1458 {
1459 m_data[row].Insert( wxEmptyString, col );
1460 }
1461 }
4db6714b 1462
5c3313a9
VZ
1463 m_numCols += numCols;
1464
f85afd4e
MB
1465 if ( GetView() )
1466 {
1467 wxGridTableMessage msg( this,
1468 wxGRIDTABLE_NOTIFY_COLS_INSERTED,
1469 pos,
1470 numCols );
8f177c8e 1471
f85afd4e
MB
1472 GetView()->ProcessTableMessage( msg );
1473 }
1474
ca65c044 1475 return true;
f85afd4e
MB
1476}
1477
1478bool wxGridStringTable::AppendCols( size_t numCols )
1479{
27f35b66 1480 size_t row;
f85afd4e
MB
1481
1482 size_t curNumRows = m_data.GetCount();
2f024384 1483
3d59537f 1484 for ( row = 0; row < curNumRows; row++ )
f85afd4e 1485 {
27f35b66 1486 m_data[row].Add( wxEmptyString, numCols );
f85afd4e
MB
1487 }
1488
5c3313a9
VZ
1489 m_numCols += numCols;
1490
f85afd4e
MB
1491 if ( GetView() )
1492 {
1493 wxGridTableMessage msg( this,
1494 wxGRIDTABLE_NOTIFY_COLS_APPENDED,
1495 numCols );
8f177c8e 1496
f85afd4e
MB
1497 GetView()->ProcessTableMessage( msg );
1498 }
1499
ca65c044 1500 return true;
f85afd4e
MB
1501}
1502
1503bool wxGridStringTable::DeleteCols( size_t pos, size_t numCols )
1504{
27f35b66 1505 size_t row;
f85afd4e
MB
1506
1507 size_t curNumRows = m_data.GetCount();
f6bcfd97
BP
1508 size_t curNumCols = ( curNumRows > 0 ? m_data[0].GetCount() :
1509 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
8f177c8e 1510
f85afd4e
MB
1511 if ( pos >= curNumCols )
1512 {
e91d2033
VZ
1513 wxFAIL_MSG( wxString::Format
1514 (
1515 wxT("Called wxGridStringTable::DeleteCols(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu cols"),
1516 (unsigned long)pos,
1517 (unsigned long)numCols,
1518 (unsigned long)curNumCols
1519 ) );
ca65c044 1520 return false;
f85afd4e
MB
1521 }
1522
d4175745
VZ
1523 int colID;
1524 if ( GetView() )
1525 colID = GetView()->GetColAt( pos );
1526 else
1527 colID = pos;
1528
1529 if ( numCols > curNumCols - colID )
1530 {
1531 numCols = curNumCols - colID;
1532 }
1533
1534 if ( !m_colLabels.IsEmpty() )
f85afd4e 1535 {
b2df5ddf
VZ
1536 // m_colLabels stores just as many elements as it needs, e.g. if only
1537 // the label of the first column had been set it would have only one
1538 // element and not numCols, so account for it
d32fb5f9 1539 int numRemaining = m_colLabels.size() - colID;
84484a8d
RD
1540 if (numRemaining > 0)
1541 m_colLabels.RemoveAt( colID, wxMin(numCols, numRemaining) );
f85afd4e
MB
1542 }
1543
5c3313a9 1544 if ( numCols >= curNumCols )
f85afd4e 1545 {
5c3313a9 1546 for ( row = 0; row < curNumRows; row++ )
f85afd4e 1547 {
dcdce64e 1548 m_data[row].Clear();
f85afd4e 1549 }
5c3313a9
VZ
1550
1551 m_numCols = 0;
1552 }
1553 else // something will be left
1554 {
1555 for ( row = 0; row < curNumRows; row++ )
f85afd4e 1556 {
d4175745 1557 m_data[row].RemoveAt( colID, numCols );
f85afd4e 1558 }
5c3313a9
VZ
1559
1560 m_numCols -= numCols;
f85afd4e 1561 }
4db6714b 1562
f85afd4e
MB
1563 if ( GetView() )
1564 {
1565 wxGridTableMessage msg( this,
1566 wxGRIDTABLE_NOTIFY_COLS_DELETED,
1567 pos,
1568 numCols );
8f177c8e 1569
f85afd4e
MB
1570 GetView()->ProcessTableMessage( msg );
1571 }
1572
ca65c044 1573 return true;
f85afd4e
MB
1574}
1575
1576wxString wxGridStringTable::GetRowLabelValue( int row )
1577{
1578 if ( row > (int)(m_rowLabels.GetCount()) - 1 )
1579 {
1580 // using default label
1581 //
1582 return wxGridTableBase::GetRowLabelValue( row );
1583 }
1584 else
1585 {
2f024384 1586 return m_rowLabels[row];
f85afd4e
MB
1587 }
1588}
1589
1590wxString wxGridStringTable::GetColLabelValue( int col )
1591{
1592 if ( col > (int)(m_colLabels.GetCount()) - 1 )
1593 {
1594 // using default label
1595 //
1596 return wxGridTableBase::GetColLabelValue( col );
1597 }
1598 else
1599 {
2f024384 1600 return m_colLabels[col];
f85afd4e
MB
1601 }
1602}
1603
1604void wxGridStringTable::SetRowLabelValue( int row, const wxString& value )
1605{
1606 if ( row > (int)(m_rowLabels.GetCount()) - 1 )
1607 {
1608 int n = m_rowLabels.GetCount();
1609 int i;
2f024384 1610
3d59537f 1611 for ( i = n; i <= row; i++ )
f85afd4e
MB
1612 {
1613 m_rowLabels.Add( wxGridTableBase::GetRowLabelValue(i) );
1614 }
1615 }
1616
1617 m_rowLabels[row] = value;
1618}
1619
1620void wxGridStringTable::SetColLabelValue( int col, const wxString& value )
1621{
1622 if ( col > (int)(m_colLabels.GetCount()) - 1 )
1623 {
1624 int n = m_colLabels.GetCount();
1625 int i;
2f024384 1626
3d59537f 1627 for ( i = n; i <= col; i++ )
f85afd4e
MB
1628 {
1629 m_colLabels.Add( wxGridTableBase::GetColLabelValue(i) );
1630 }
1631 }
1632
1633 m_colLabels[col] = value;
1634}
1635
1636
f85afd4e 1637//////////////////////////////////////////////////////////////////////
2d66e025
MB
1638//////////////////////////////////////////////////////////////////////
1639
86033c4b
VZ
1640BEGIN_EVENT_TABLE(wxGridSubwindow, wxWindow)
1641 EVT_MOUSE_CAPTURE_LOST(wxGridSubwindow::OnMouseCaptureLost)
1642END_EVENT_TABLE()
1643
1644void wxGridSubwindow::OnMouseCaptureLost(wxMouseCaptureLostEvent& WXUNUSED(event))
1645{
1646 m_owner->CancelMouseCapture();
1647}
1648
86033c4b 1649BEGIN_EVENT_TABLE( wxGridRowLabelWindow, wxGridSubwindow )
2d66e025 1650 EVT_PAINT( wxGridRowLabelWindow::OnPaint )
a9339fe2 1651 EVT_MOUSEWHEEL( wxGridRowLabelWindow::OnMouseWheel )
2d66e025 1652 EVT_MOUSE_EVENTS( wxGridRowLabelWindow::OnMouseEvent )
2d66e025
MB
1653END_EVENT_TABLE()
1654
aa5e1f75 1655void wxGridRowLabelWindow::OnPaint( wxPaintEvent& WXUNUSED(event) )
2d66e025
MB
1656{
1657 wxPaintDC dc(this);
1658
1659 // NO - don't do this because it will set both the x and y origin
1660 // coords to match the parent scrolled window and we just want to
1661 // set the y coord - MB
1662 //
1663 // m_owner->PrepareDC( dc );
60ff3b99 1664
790ad94f 1665 int x, y;
2d66e025 1666 m_owner->CalcUnscrolledPosition( 0, 0, &x, &y );
615b7e6a
RR
1667 wxPoint pt = dc.GetDeviceOrigin();
1668 dc.SetDeviceOrigin( pt.x, pt.y-y );
60ff3b99 1669
d10f4bf9 1670 wxArrayInt rows = m_owner->CalcRowLabelsExposed( GetUpdateRegion() );
a9339fe2 1671 m_owner->DrawRowLabels( dc, rows );
2d66e025
MB
1672}
1673
2d66e025
MB
1674void wxGridRowLabelWindow::OnMouseEvent( wxMouseEvent& event )
1675{
1676 m_owner->ProcessRowLabelMouseEvent( event );
1677}
1678
b51c3f27
RD
1679void wxGridRowLabelWindow::OnMouseWheel( wxMouseEvent& event )
1680{
b5e9cbb9
FM
1681 if (!m_owner->GetEventHandler()->ProcessEvent( event ))
1682 event.Skip();
b51c3f27
RD
1683}
1684
2d66e025
MB
1685//////////////////////////////////////////////////////////////////////
1686
86033c4b 1687BEGIN_EVENT_TABLE( wxGridColLabelWindow, wxGridSubwindow )
2d66e025 1688 EVT_PAINT( wxGridColLabelWindow::OnPaint )
a9339fe2 1689 EVT_MOUSEWHEEL( wxGridColLabelWindow::OnMouseWheel )
2d66e025 1690 EVT_MOUSE_EVENTS( wxGridColLabelWindow::OnMouseEvent )
2d66e025
MB
1691END_EVENT_TABLE()
1692
aa5e1f75 1693void wxGridColLabelWindow::OnPaint( wxPaintEvent& WXUNUSED(event) )
2d66e025
MB
1694{
1695 wxPaintDC dc(this);
1696
1697 // NO - don't do this because it will set both the x and y origin
1698 // coords to match the parent scrolled window and we just want to
1699 // set the x coord - MB
1700 //
1701 // m_owner->PrepareDC( dc );
60ff3b99 1702
790ad94f 1703 int x, y;
2d66e025 1704 m_owner->CalcUnscrolledPosition( 0, 0, &x, &y );
615b7e6a
RR
1705 wxPoint pt = dc.GetDeviceOrigin();
1706 if (GetLayoutDirection() == wxLayout_RightToLeft)
1707 dc.SetDeviceOrigin( pt.x+x, pt.y );
1708 else
1709 dc.SetDeviceOrigin( pt.x-x, pt.y );
2d66e025 1710
d10f4bf9 1711 wxArrayInt cols = m_owner->CalcColLabelsExposed( GetUpdateRegion() );
a9339fe2 1712 m_owner->DrawColLabels( dc, cols );
2d66e025
MB
1713}
1714
2d66e025
MB
1715void wxGridColLabelWindow::OnMouseEvent( wxMouseEvent& event )
1716{
1717 m_owner->ProcessColLabelMouseEvent( event );
1718}
1719
b51c3f27
RD
1720void wxGridColLabelWindow::OnMouseWheel( wxMouseEvent& event )
1721{
b5e9cbb9
FM
1722 if (!m_owner->GetEventHandler()->ProcessEvent( event ))
1723 event.Skip();
b51c3f27
RD
1724}
1725
2d66e025
MB
1726//////////////////////////////////////////////////////////////////////
1727
86033c4b 1728BEGIN_EVENT_TABLE( wxGridCornerLabelWindow, wxGridSubwindow )
a9339fe2 1729 EVT_MOUSEWHEEL( wxGridCornerLabelWindow::OnMouseWheel )
2d66e025 1730 EVT_MOUSE_EVENTS( wxGridCornerLabelWindow::OnMouseEvent )
a9339fe2 1731 EVT_PAINT( wxGridCornerLabelWindow::OnPaint )
2d66e025
MB
1732END_EVENT_TABLE()
1733
d2fdd8d2
RR
1734void wxGridCornerLabelWindow::OnPaint( wxPaintEvent& WXUNUSED(event) )
1735{
1736 wxPaintDC dc(this);
b99be8fb 1737
ff72f628 1738 m_owner->DrawCornerLabel(dc);
d2fdd8d2
RR
1739}
1740
2d66e025
MB
1741void wxGridCornerLabelWindow::OnMouseEvent( wxMouseEvent& event )
1742{
1743 m_owner->ProcessCornerLabelMouseEvent( event );
1744}
1745
b51c3f27
RD
1746void wxGridCornerLabelWindow::OnMouseWheel( wxMouseEvent& event )
1747{
b5e9cbb9
FM
1748 if (!m_owner->GetEventHandler()->ProcessEvent(event))
1749 event.Skip();
b51c3f27
RD
1750}
1751
f85afd4e
MB
1752//////////////////////////////////////////////////////////////////////
1753
86033c4b 1754BEGIN_EVENT_TABLE( wxGridWindow, wxGridSubwindow )
2d66e025 1755 EVT_PAINT( wxGridWindow::OnPaint )
a9339fe2 1756 EVT_MOUSEWHEEL( wxGridWindow::OnMouseWheel )
2d66e025
MB
1757 EVT_MOUSE_EVENTS( wxGridWindow::OnMouseEvent )
1758 EVT_KEY_DOWN( wxGridWindow::OnKeyDown )
f6bcfd97 1759 EVT_KEY_UP( wxGridWindow::OnKeyUp )
a9339fe2 1760 EVT_CHAR( wxGridWindow::OnChar )
80acaf25
JS
1761 EVT_SET_FOCUS( wxGridWindow::OnFocus )
1762 EVT_KILL_FOCUS( wxGridWindow::OnFocus )
2796cce3 1763 EVT_ERASE_BACKGROUND( wxGridWindow::OnEraseBackground )
2d66e025
MB
1764END_EVENT_TABLE()
1765
2d66e025
MB
1766void wxGridWindow::OnPaint( wxPaintEvent &WXUNUSED(event) )
1767{
1768 wxPaintDC dc( this );
1769 m_owner->PrepareDC( dc );
796df70a 1770 wxRegion reg = GetUpdateRegion();
ccdee36f
DS
1771 wxGridCellCoordsArray dirtyCells = m_owner->CalcCellsExposed( reg );
1772 m_owner->DrawGridCellArea( dc, dirtyCells );
2f024384 1773
9f7aee01
VZ
1774 m_owner->DrawGridSpace( dc );
1775
796df70a 1776 m_owner->DrawAllGridLines( dc, reg );
2f024384 1777
ccdee36f 1778 m_owner->DrawHighlight( dc, dirtyCells );
2d66e025
MB
1779}
1780
779e28da
VZ
1781void wxGrid::Render( wxDC& dc,
1782 const wxPoint& position,
1783 const wxSize& size,
1784 const wxGridCellCoords& topLeft,
1785 const wxGridCellCoords& bottomRight,
1786 int style )
1787{
1788 wxCHECK_RET( bottomRight.GetCol() < GetNumberCols(),
1789 "Invalid right column" );
1790 wxCHECK_RET( bottomRight.GetRow() < GetNumberRows(),
1791 "Invalid bottom row" );
1792
1793 // store user settings and reset later
1794
1795 // remove grid selection, don't paint selection colour
1796 // unless we have wxGRID_DRAW_SELECTION
1797 // block selections are the only ones catered for here
1798 wxGridCellCoordsArray selectedCells;
1799 bool hasSelection = IsSelection();
1800 if ( hasSelection && !( style & wxGRID_DRAW_SELECTION ) )
1801 {
1802 selectedCells = GetSelectionBlockTopLeft();
1803 // non block selections may not have a bottom right
1804 if ( GetSelectionBlockBottomRight().size() )
1805 selectedCells.Add( GetSelectionBlockBottomRight()[ 0 ] );
1806
1807 ClearSelection();
1808 }
1809
1810 // store user device origin
1811 wxCoord userOriginX, userOriginY;
1812 dc.GetDeviceOrigin( &userOriginX, &userOriginY );
1813
1814 // store user scale
1815 double scaleUserX, scaleUserY;
1816 dc.GetUserScale( &scaleUserX, &scaleUserY );
1817
1818 // set defaults if necessary
f00acdb8
VZ
1819 wxGridCellCoords leftTop( topLeft ), rightBottom( bottomRight );
1820 if ( leftTop.GetCol() < 0 )
1821 leftTop.SetCol(0);
1822 if ( leftTop.GetRow() < 0 )
1823 leftTop.SetRow(0);
1824 if ( rightBottom.GetCol() < 0 )
1825 rightBottom.SetCol(GetNumberCols() - 1);
1826 if ( rightBottom.GetRow() < 0 )
1827 rightBottom.SetRow(GetNumberRows() - 1);
1828
1829 // get grid offset, size and cell parameters
1830 wxPoint pointOffSet;
1831 wxSize sizeGrid;
779e28da
VZ
1832 wxGridCellCoordsArray renderCells;
1833 wxArrayInt arrayCols;
1834 wxArrayInt arrayRows;
779e28da 1835
f00acdb8
VZ
1836 GetRenderSizes( leftTop, rightBottom,
1837 pointOffSet, sizeGrid,
1838 renderCells,
1839 arrayCols, arrayRows );
779e28da
VZ
1840
1841 // add headers/labels to dimensions
1842 if ( style & wxGRID_DRAW_ROWS_HEADER )
f00acdb8 1843 sizeGrid.x += GetRowLabelSize();
779e28da 1844 if ( style & wxGRID_DRAW_COLS_HEADER )
f00acdb8 1845 sizeGrid.y += GetColLabelSize();
779e28da 1846
f00acdb8
VZ
1847 // get render start position in logical units
1848 wxPoint positionRender = GetRenderPosition( dc, position );
779e28da 1849
779e28da
VZ
1850 wxCoord originX = dc.LogicalToDeviceX( positionRender.x );
1851 wxCoord originY = dc.LogicalToDeviceY( positionRender.y );
1852
1853 dc.SetDeviceOrigin( originX, originY );
1854
f00acdb8 1855 SetRenderScale( dc, positionRender, size, sizeGrid );
779e28da
VZ
1856
1857 // draw row headers at specified origin
1858 if ( GetRowLabelSize() > 0 && ( style & wxGRID_DRAW_ROWS_HEADER ) )
1859 {
1860 if ( style & wxGRID_DRAW_COLS_HEADER )
1861 {
1862 DrawCornerLabel( dc ); // do only if both col and row labels drawn
1863 originY += dc.LogicalToDeviceYRel( GetColLabelSize() );
1864 }
1865
f00acdb8 1866 originY -= dc.LogicalToDeviceYRel( pointOffSet.y );
779e28da
VZ
1867 dc.SetDeviceOrigin( originX, originY );
1868
1869 DrawRowLabels( dc, arrayRows );
1870
1871 // reset for columns
1872 if ( style & wxGRID_DRAW_COLS_HEADER )
1873 originY -= dc.LogicalToDeviceYRel( GetColLabelSize() );
1874
f00acdb8 1875 originY += dc.LogicalToDeviceYRel( pointOffSet.y );
779e28da
VZ
1876 // X offset so we don't overwrite row labels
1877 originX += dc.LogicalToDeviceXRel( GetRowLabelSize() );
1878 }
1879
1880 // subtract col offset where startcol > 0
f00acdb8 1881 originX -= dc.LogicalToDeviceXRel( pointOffSet.x );
779e28da
VZ
1882 // no y offset for col labels, they are at the Y origin
1883
1884 // draw column labels
1885 if ( style & wxGRID_DRAW_COLS_HEADER )
1886 {
1887 dc.SetDeviceOrigin( originX, originY );
1888 DrawColLabels( dc, arrayCols );
1889 // don't overwrite the labels, increment originY
1890 originY += dc.LogicalToDeviceYRel( GetColLabelSize() );
1891 }
1892
1893 // set device origin to draw grid cells and lines
f00acdb8 1894 originY -= dc.LogicalToDeviceYRel( pointOffSet.y );
779e28da
VZ
1895 dc.SetDeviceOrigin( originX, originY );
1896
1897 // draw cell area background
1898 dc.SetBrush( GetDefaultCellBackgroundColour() );
1899 dc.SetPen( *wxTRANSPARENT_PEN );
1900 // subtract headers from grid area dimensions
f00acdb8 1901 wxSize sizeCells( sizeGrid );
779e28da 1902 if ( style & wxGRID_DRAW_ROWS_HEADER )
f00acdb8 1903 sizeCells.x -= GetRowLabelSize();
779e28da 1904 if ( style & wxGRID_DRAW_COLS_HEADER )
f00acdb8 1905 sizeCells.y -= GetColLabelSize();
779e28da 1906
f00acdb8 1907 dc.DrawRectangle( pointOffSet, sizeCells );
779e28da
VZ
1908
1909 // draw cells
1910 DrawGridCellArea( dc, renderCells );
1911
f00acdb8 1912 // draw grid lines
779e28da
VZ
1913 if ( style & wxGRID_DRAW_CELL_LINES )
1914 {
f00acdb8
VZ
1915 wxRegion regionClip( pointOffSet.x, pointOffSet.y,
1916 sizeCells.x, sizeCells.y );
779e28da
VZ
1917
1918 DrawRangeGridLines(dc, regionClip, renderCells[0], renderCells.Last());
1919 }
1920
1921 // draw render rectangle bounding lines
f00acdb8
VZ
1922 DoRenderBox( dc, style,
1923 pointOffSet, sizeCells,
1924 leftTop, rightBottom );
779e28da
VZ
1925
1926 // restore user setings
1927 dc.SetDeviceOrigin( userOriginX, userOriginY );
1928 dc.SetUserScale( scaleUserX, scaleUserY );
1929
1930 if ( selectedCells.size() && !( style & wxGRID_DRAW_SELECTION ) )
1931 {
1932 SelectBlock( selectedCells[ 0 ].GetRow(),
1933 selectedCells[ 0 ].GetCol(),
1934 selectedCells[ selectedCells.size() -1 ].GetRow(),
1935 selectedCells[ selectedCells.size() -1 ].GetCol() );
1936 }
1937}
1938
1939void
1940wxGrid::SetRenderScale(wxDC& dc,
1941 const wxPoint& pos, const wxSize& size,
f00acdb8 1942 const wxSize& sizeGrid )
779e28da
VZ
1943{
1944 double scaleX, scaleY;
1945 wxSize sizeTemp;
1946
8e2c6c3f
VZ
1947 if ( size.GetWidth() != wxDefaultSize.GetWidth() ) // size.x was specified
1948 sizeTemp.SetWidth( size.GetWidth() );
779e28da 1949 else
8e2c6c3f 1950 sizeTemp.SetWidth( dc.DeviceToLogicalXRel( dc.GetSize().GetWidth() )
779e28da
VZ
1951 - pos.x );
1952
8e2c6c3f
VZ
1953 if ( size.GetHeight() != wxDefaultSize.GetHeight() ) // size.y was specified
1954 sizeTemp.SetHeight( size.GetHeight() );
779e28da 1955 else
8e2c6c3f 1956 sizeTemp.SetHeight( dc.DeviceToLogicalYRel( dc.GetSize().GetHeight() )
779e28da
VZ
1957 - pos.y );
1958
8e2c6c3f
VZ
1959 scaleX = (double)( (double) sizeTemp.GetWidth() / (double) sizeGrid.GetWidth() );
1960 scaleY = (double)( (double) sizeTemp.GetHeight() / (double) sizeGrid.GetHeight() );
779e28da
VZ
1961
1962 dc.SetUserScale( wxMin( scaleX, scaleY), wxMin( scaleX, scaleY ) );
1963}
1964
f00acdb8
VZ
1965// get grid rendered size, origin offset and fill cell arrays
1966void wxGrid::GetRenderSizes( const wxGridCellCoords& topLeft,
1967 const wxGridCellCoords& bottomRight,
1968 wxPoint& pointOffSet, wxSize& sizeGrid,
1969 wxGridCellCoordsArray& renderCells,
1970 wxArrayInt& arrayCols, wxArrayInt& arrayRows )
1971{
1972 pointOffSet.x = 0;
1973 pointOffSet.y = 0;
1974 sizeGrid.SetWidth( 0 );
1975 sizeGrid.SetHeight( 0 );
1976
1977 int col, row;
1978
1979 wxGridSizesInfo sizeinfo = GetColSizes();
1980 for ( col = 0; col <= bottomRight.GetCol(); col++ )
1981 {
1982 if ( col < topLeft.GetCol() )
1983 {
1984 pointOffSet.x += sizeinfo.GetSize( col );
1985 }
1986 else
1987 {
1988 for ( row = topLeft.GetRow(); row <= bottomRight.GetRow(); row++ )
1989 {
1990 renderCells.Add( wxGridCellCoords( row, col ));
1991 arrayRows.Add( row ); // column labels rendered in DrawColLabels
1992 }
1993 arrayCols.Add( col ); // row labels rendered in DrawRowLabels
1994 sizeGrid.x += sizeinfo.GetSize( col );
1995 }
1996 }
1997
1998 sizeinfo = GetRowSizes();
1999 for ( row = 0; row <= bottomRight.GetRow(); row++ )
2000 {
2001 if ( row < topLeft.GetRow() )
2002 pointOffSet.y += sizeinfo.GetSize( row );
2003 else
2004 sizeGrid.y += sizeinfo.GetSize( row );
2005 }
2006}
2007
2008// get render start position
2009// if position not specified use dc draw extents MaxX and MaxY
2010wxPoint wxGrid::GetRenderPosition( wxDC& dc, const wxPoint& position )
2011{
2012 wxPoint positionRender( position );
2013
2014 if ( !positionRender.IsFullySpecified() )
2015 {
2016 if ( positionRender.x == wxDefaultPosition.x )
2017 positionRender.x = dc.MaxX();
2018
2019 if ( positionRender.y == wxDefaultPosition.y )
2020 positionRender.y = dc.MaxY();
2021 }
2022
2023 return positionRender;
2024}
2025
2026// draw render rectangle bounding lines
2027// useful where there is multi cell row or col clipping and no cell border
2028void wxGrid::DoRenderBox( wxDC& dc, const int& style,
2029 const wxPoint& pointOffSet,
2030 const wxSize& sizeCells,
2031 const wxGridCellCoords& topLeft,
2032 const wxGridCellCoords& bottomRight )
2033{
2034 if ( !( style & wxGRID_DRAW_BOX_RECT ) )
2035 return;
2036
2037 int bottom = pointOffSet.y + sizeCells.GetY(),
2038 right = pointOffSet.x + sizeCells.GetX() - 1;
2039
2040 // horiz top line if we are not drawing column header/labels
2041 if ( !( style & wxGRID_DRAW_COLS_HEADER ) )
2042 {
2043 int left = pointOffSet.x;
2044 left += ( style & wxGRID_DRAW_COLS_HEADER )
2045 ? - GetRowLabelSize() : 0;
2046 dc.SetPen( GetRowGridLinePen( topLeft.GetRow() ) );
2047 dc.DrawLine( left,
2048 pointOffSet.y,
2049 right,
2050 pointOffSet.y );
2051 }
2052
2053 // horiz bottom line
2054 dc.SetPen( GetRowGridLinePen( bottomRight.GetRow() ) );
2055 dc.DrawLine( pointOffSet.x, bottom - 1, right, bottom - 1 );
2056
2057 // left vertical line if we are not drawing row header/labels
2058 if ( !( style & wxGRID_DRAW_ROWS_HEADER ) )
2059 {
2060 int top = pointOffSet.y;
2061 top += ( style & wxGRID_DRAW_COLS_HEADER )
2062 ? - GetColLabelSize() : 0;
2063 dc.SetPen( GetColGridLinePen( topLeft.GetCol() ) );
2064 dc.DrawLine( pointOffSet.x -1,
2065 top,
2066 pointOffSet.x - 1,
2067 bottom - 1 );
2068 }
2069
2070 // right vertical line
2071 dc.SetPen( GetColGridLinePen( bottomRight.GetCol() ) );
2072 dc.DrawLine( right, pointOffSet.y, right, bottom - 1 );
2073}
2074
2d66e025
MB
2075void wxGridWindow::ScrollWindow( int dx, int dy, const wxRect *rect )
2076{
59ddac01 2077 wxWindow::ScrollWindow( dx, dy, rect );
ad805b9e
VZ
2078 m_owner->GetGridRowLabelWindow()->ScrollWindow( 0, dy, rect );
2079 m_owner->GetGridColLabelWindow()->ScrollWindow( dx, 0, rect );
2d66e025
MB
2080}
2081
2d66e025
MB
2082void wxGridWindow::OnMouseEvent( wxMouseEvent& event )
2083{
33e9fc54
RD
2084 if (event.ButtonDown(wxMOUSE_BTN_LEFT) && FindFocus() != this)
2085 SetFocus();
902725ee 2086
2d66e025
MB
2087 m_owner->ProcessGridCellMouseEvent( event );
2088}
2089
b51c3f27
RD
2090void wxGridWindow::OnMouseWheel( wxMouseEvent& event )
2091{
b5e9cbb9
FM
2092 if (!m_owner->GetEventHandler()->ProcessEvent( event ))
2093 event.Skip();
b51c3f27 2094}
2d66e025 2095
f6bcfd97 2096// This seems to be required for wxMotif/wxGTK otherwise the mouse
2d66e025
MB
2097// cursor must be in the cell edit control to get key events
2098//
2099void wxGridWindow::OnKeyDown( wxKeyEvent& event )
2100{
4db6714b
KH
2101 if ( !m_owner->GetEventHandler()->ProcessEvent( event ) )
2102 event.Skip();
2d66e025 2103}
f85afd4e 2104
f6bcfd97
BP
2105void wxGridWindow::OnKeyUp( wxKeyEvent& event )
2106{
4db6714b
KH
2107 if ( !m_owner->GetEventHandler()->ProcessEvent( event ) )
2108 event.Skip();
f6bcfd97 2109}
7c8a8ad5 2110
63e2147c
RD
2111void wxGridWindow::OnChar( wxKeyEvent& event )
2112{
4db6714b
KH
2113 if ( !m_owner->GetEventHandler()->ProcessEvent( event ) )
2114 event.Skip();
63e2147c
RD
2115}
2116
aa5e1f75 2117void wxGridWindow::OnEraseBackground( wxEraseEvent& WXUNUSED(event) )
8dd4f536 2118{
8dd4f536 2119}
025562fe 2120
80acaf25
JS
2121void wxGridWindow::OnFocus(wxFocusEvent& event)
2122{
760be3f7
VS
2123 // and if we have any selection, it has to be repainted, because it
2124 // uses different colour when the grid is not focused:
2125 if ( m_owner->IsSelection() )
2126 {
2127 Refresh();
2128 }
2129 else
2130 {
2131 // NB: Note that this code is in "else" branch only because the other
2132 // branch refreshes everything and so there's no point in calling
2133 // Refresh() again, *not* because it should only be done if
2134 // !IsSelection(). If the above code is ever optimized to refresh
2135 // only selected area, this needs to be moved out of the "else"
2136 // branch so that it's always executed.
2137
2138 // current cell cursor {dis,re}appears on focus change:
2139 const wxGridCellCoords cursorCoords(m_owner->GetGridCursorRow(),
2140 m_owner->GetGridCursorCol());
2141 const wxRect cursor =
2142 m_owner->BlockToDeviceRect(cursorCoords, cursorCoords);
2143 Refresh(true, &cursor);
2144 }
2145
80acaf25
JS
2146 if ( !m_owner->GetEventHandler()->ProcessEvent( event ) )
2147 event.Skip();
2148}
2d66e025 2149
d4175745 2150#define internalXToCol(x) XToCol(x, true)
bec70262 2151#define internalYToRow(y) YToRow(y, true)
ccdee36f 2152
33188aa4 2153/////////////////////////////////////////////////////////////////////
07296f0b 2154
2d66e025 2155BEGIN_EVENT_TABLE( wxGrid, wxScrolledWindow )
f85afd4e
MB
2156 EVT_PAINT( wxGrid::OnPaint )
2157 EVT_SIZE( wxGrid::OnSize )
f85afd4e 2158 EVT_KEY_DOWN( wxGrid::OnKeyDown )
f6bcfd97 2159 EVT_KEY_UP( wxGrid::OnKeyUp )
63e2147c 2160 EVT_CHAR ( wxGrid::OnChar )
2796cce3 2161 EVT_ERASE_BACKGROUND( wxGrid::OnEraseBackground )
add6e919 2162 EVT_COMMAND(wxID_ANY, wxEVT_GRID_HIDE_EDITOR, wxGrid::OnHideEditor )
f85afd4e 2163END_EVENT_TABLE()
8f177c8e 2164
b0a877ec
SC
2165bool wxGrid::Create(wxWindow *parent, wxWindowID id,
2166 const wxPoint& pos, const wxSize& size,
2167 long style, const wxString& name)
2168{
2169 if (!wxScrolledWindow::Create(parent, id, pos, size,
c2f5b920 2170 style | wxWANTS_CHARS, name))
ca65c044 2171 return false;
b0a877ec 2172
2f024384
DS
2173 m_colMinWidths = wxLongToLongHashMap(GRID_HASH_SIZE);
2174 m_rowMinHeights = wxLongToLongHashMap(GRID_HASH_SIZE);
b0a877ec 2175
2f024384 2176 Create();
170acdc9 2177 SetInitialSize(size);
b93aafab 2178 CalcDimensions();
b0a877ec 2179
ca65c044 2180 return true;
b0a877ec
SC
2181}
2182
58dd5b3b
MB
2183wxGrid::~wxGrid()
2184{
e882d288
VZ
2185 if ( m_winCapture )
2186 m_winCapture->ReleaseMouse();
2187
b09b3048
VZ
2188 // Ensure that the editor control is destroyed before the grid is,
2189 // otherwise we crash later when the editor tries to do something with the
2190 // half destroyed grid
2191 HideCellEditControl();
2192
606b005f
JS
2193 // Must do this or ~wxScrollHelper will pop the wrong event handler
2194 SetTargetWindow(this);
0a976765 2195 ClearAttrCache();
39bcce60 2196 wxSafeDecRef(m_defaultCellAttr);
0a976765
VZ
2197
2198#ifdef DEBUG_ATTR_CACHE
2199 size_t total = gs_nAttrCacheHits + gs_nAttrCacheMisses;
9a83f860 2200 wxPrintf(wxT("wxGrid attribute cache statistics: "
0a976765
VZ
2201 "total: %u, hits: %u (%u%%)\n"),
2202 total, gs_nAttrCacheHits,
2203 total ? (gs_nAttrCacheHits*100) / total : 0);
2204#endif
2205
86020f7e
VZ
2206 // if we own the table, just delete it, otherwise at least don't leave it
2207 // with dangling view pointer
2208 if ( m_ownTable )
2796cce3 2209 delete m_table;
ecc7aa82 2210 else if ( m_table && m_table->GetView() == this )
86020f7e 2211 m_table->SetView(NULL);
f2d76237
RD
2212
2213 delete m_typeRegistry;
b5808881 2214 delete m_selection;
82edfbe7
VZ
2215
2216 delete m_setFixedRows;
2217 delete m_setFixedCols;
58dd5b3b
MB
2218}
2219
58dd5b3b
MB
2220//
2221// ----- internal init and update functions
2222//
2223
9950649c
RD
2224// NOTE: If using the default visual attributes works everywhere then this can
2225// be removed as well as the #else cases below.
2226#define _USE_VISATTR 0
2227
58dd5b3b 2228void wxGrid::Create()
f0102d2a 2229{
3d59537f
DS
2230 // create the type registry
2231 m_typeRegistry = new wxGridTypeRegistry;
2c9a89e0 2232
ca65c044 2233 m_cellEditCtrlEnabled = false;
4634a5d6 2234
ccd970b1 2235 m_defaultCellAttr = new wxGridCellAttr();
f2d76237
RD
2236
2237 // Set default cell attributes
ccd970b1 2238 m_defaultCellAttr->SetDefAttr(m_defaultCellAttr);
19d7140e 2239 m_defaultCellAttr->SetKind(wxGridCellAttr::Default);
f2d76237 2240 m_defaultCellAttr->SetFont(GetFont());
4c7277db 2241 m_defaultCellAttr->SetAlignment(wxALIGN_LEFT, wxALIGN_TOP);
9950649c
RD
2242 m_defaultCellAttr->SetRenderer(new wxGridCellStringRenderer);
2243 m_defaultCellAttr->SetEditor(new wxGridCellTextEditor);
2244
2245#if _USE_VISATTR
2246 wxVisualAttributes gva = wxListBox::GetClassDefaultAttributes();
2247 wxVisualAttributes lva = wxPanel::GetClassDefaultAttributes();
2248
2249 m_defaultCellAttr->SetTextColour(gva.colFg);
2250 m_defaultCellAttr->SetBackgroundColour(gva.colBg);
ca65c044 2251
9950649c 2252#else
f2d76237 2253 m_defaultCellAttr->SetTextColour(
a756f210 2254 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT));
f2d76237 2255 m_defaultCellAttr->SetBackgroundColour(
a756f210 2256 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW));
9950649c 2257#endif
2796cce3 2258
4634a5d6
MB
2259 m_numRows = 0;
2260 m_numCols = 0;
2261 m_currentCellCoords = wxGridNoCellCoords;
b99be8fb 2262
f2d76237 2263 // subwindow components that make up the wxGrid
ad805b9e
VZ
2264 m_rowLabelWin = new wxGridRowLabelWindow(this);
2265 CreateColumnWindow();
2266 m_cornerLabelWin = new wxGridCornerLabelWindow(this);
2267 m_gridWin = new wxGridWindow( this );
2d66e025
MB
2268
2269 SetTargetWindow( m_gridWin );
6f36917b 2270
9950649c
RD
2271#if _USE_VISATTR
2272 wxColour gfg = gva.colFg;
2273 wxColour gbg = gva.colBg;
2274 wxColour lfg = lva.colFg;
2275 wxColour lbg = lva.colBg;
2276#else
2277 wxColour gfg = wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT );
2278 wxColour gbg = wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOW );
2279 wxColour lfg = wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT );
2280 wxColour lbg = wxSystemSettings::GetColour( wxSYS_COLOUR_BTNFACE );
2281#endif
2f024384 2282
fa47d7a7
VS
2283 m_cornerLabelWin->SetOwnForegroundColour(lfg);
2284 m_cornerLabelWin->SetOwnBackgroundColour(lbg);
2285 m_rowLabelWin->SetOwnForegroundColour(lfg);
2286 m_rowLabelWin->SetOwnBackgroundColour(lbg);
ad805b9e
VZ
2287 m_colWindow->SetOwnForegroundColour(lfg);
2288 m_colWindow->SetOwnBackgroundColour(lbg);
fa47d7a7
VS
2289
2290 m_gridWin->SetOwnForegroundColour(gfg);
2291 m_gridWin->SetOwnBackgroundColour(gbg);
ca65c044 2292
ad805b9e
VZ
2293 m_labelBackgroundColour = m_rowLabelWin->GetBackgroundColour();
2294 m_labelTextColour = m_rowLabelWin->GetForegroundColour();
2295
2296 // now that we have the grid window, use its font to compute the default
2297 // row height
2298 m_defaultRowHeight = m_gridWin->GetCharHeight();
2299#if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl()
2300 m_defaultRowHeight += 8;
2301#else
2302 m_defaultRowHeight += 4;
2303#endif
2304
2305}
2306
2307void wxGrid::CreateColumnWindow()
2308{
2309 if ( m_useNativeHeader )
2310 {
2311 m_colWindow = new wxGridHeaderCtrl(this);
2312 m_colLabelHeight = m_colWindow->GetBestSize().y;
2313 }
2314 else // draw labels ourselves
2315 {
2316 m_colWindow = new wxGridColLabelWindow(this);
2317 m_colLabelHeight = WXGRID_DEFAULT_COL_LABEL_HEIGHT;
2318 }
2d66e025 2319}
f85afd4e 2320
b5808881 2321bool wxGrid::CreateGrid( int numRows, int numCols,
6b992f7d 2322 wxGridSelectionModes selmode )
2d66e025 2323{
f6bcfd97 2324 wxCHECK_MSG( !m_created,
ca65c044 2325 false,
f6bcfd97
BP
2326 wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
2327
6b992f7d 2328 return SetTable(new wxGridStringTable(numRows, numCols), true, selmode);
2796cce3
RD
2329}
2330
6b992f7d 2331void wxGrid::SetSelectionMode(wxGridSelectionModes selmode)
f1567cdd 2332{
6f36917b
VZ
2333 wxCHECK_RET( m_created,
2334 wxT("Called wxGrid::SetSelectionMode() before calling CreateGrid()") );
2335
2336 m_selection->SetSelectionMode( selmode );
f1567cdd
SN
2337}
2338
aa5b8857
SN
2339wxGrid::wxGridSelectionModes wxGrid::GetSelectionMode() const
2340{
6b992f7d 2341 wxCHECK_MSG( m_created, wxGridSelectCells,
aa5b8857
SN
2342 wxT("Called wxGrid::GetSelectionMode() before calling CreateGrid()") );
2343
2344 return m_selection->GetSelectionMode();
2345}
2346
6b992f7d
VZ
2347bool
2348wxGrid::SetTable(wxGridTableBase *table,
2349 bool takeOwnership,
2350 wxGrid::wxGridSelectionModes selmode )
2796cce3 2351{
a7dde52f 2352 bool checkSelection = false;
2796cce3
RD
2353 if ( m_created )
2354 {
3e13956a 2355 // stop all processing
ca65c044 2356 m_created = false;
86c7378f 2357
c71b2126 2358 if (m_table)
86c7378f 2359 {
a7dde52f
SN
2360 m_table->SetView(0);
2361 if( m_ownTable )
2362 delete m_table;
5b061713 2363 m_table = NULL;
86c7378f 2364 }
2f024384 2365
5276b0a5 2366 wxDELETE(m_selection);
a7dde52f
SN
2367
2368 m_ownTable = false;
2f024384
DS
2369 m_numRows = 0;
2370 m_numCols = 0;
a7dde52f
SN
2371 checkSelection = true;
2372
2373 // kill row and column size arrays
2374 m_colWidths.Empty();
2375 m_colRights.Empty();
2376 m_rowHeights.Empty();
2377 m_rowBottoms.Empty();
233a54f6 2378 }
2f024384 2379
233a54f6 2380 if (table)
2796cce3
RD
2381 {
2382 m_numRows = table->GetNumberRows();
2383 m_numCols = table->GetNumberCols();
2384
ad805b9e 2385 if ( m_useNativeHeader )
3039ade9 2386 GetGridColHeader()->SetColumnCount(m_numCols);
ad805b9e 2387
2796cce3
RD
2388 m_table = table;
2389 m_table->SetView( this );
8fc856de 2390 m_ownTable = takeOwnership;
043d16b2 2391 m_selection = new wxGridSelection( this, selmode );
a7dde52f
SN
2392 if (checkSelection)
2393 {
2394 // If the newly set table is smaller than the
2395 // original one current cell and selection regions
2396 // might be invalid,
8a3e536c 2397 m_selectedBlockCorner = wxGridNoCellCoords;
c71b2126 2398 m_currentCellCoords =
a7dde52f
SN
2399 wxGridCellCoords(wxMin(m_numRows, m_currentCellCoords.GetRow()),
2400 wxMin(m_numCols, m_currentCellCoords.GetCol()));
8a3e536c
VZ
2401 if (m_selectedBlockTopLeft.GetRow() >= m_numRows ||
2402 m_selectedBlockTopLeft.GetCol() >= m_numCols)
a7dde52f 2403 {
8a3e536c
VZ
2404 m_selectedBlockTopLeft = wxGridNoCellCoords;
2405 m_selectedBlockBottomRight = wxGridNoCellCoords;
a7dde52f
SN
2406 }
2407 else
8a3e536c 2408 m_selectedBlockBottomRight =
a7dde52f 2409 wxGridCellCoords(wxMin(m_numRows,
8a3e536c 2410 m_selectedBlockBottomRight.GetRow()),
a7dde52f 2411 wxMin(m_numCols,
8a3e536c 2412 m_selectedBlockBottomRight.GetCol()));
a7dde52f 2413 }
6f36917b
VZ
2414 CalcDimensions();
2415
ca65c044 2416 m_created = true;
2d66e025 2417 }
f85afd4e 2418
1f89d869
VZ
2419 InvalidateBestSize();
2420
2d66e025 2421 return m_created;
f85afd4e
MB
2422}
2423
ad805b9e 2424void wxGrid::Init()
f9549841
VZ
2425{
2426 m_created = false;
2427
2428 m_cornerLabelWin = NULL;
2429 m_rowLabelWin = NULL;
ad805b9e 2430 m_colWindow = NULL;
f9549841
VZ
2431 m_gridWin = NULL;
2432
2433 m_table = NULL;
2434 m_ownTable = false;
2435
2436 m_selection = NULL;
2437 m_defaultCellAttr = NULL;
2438 m_typeRegistry = NULL;
2439 m_winCapture = NULL;
f9549841 2440
f85afd4e
MB
2441 m_rowLabelWidth = WXGRID_DEFAULT_ROW_LABEL_WIDTH;
2442 m_colLabelHeight = WXGRID_DEFAULT_COL_LABEL_HEIGHT;
2443
82edfbe7
VZ
2444 m_setFixedRows =
2445 m_setFixedCols = NULL;
2446
0a976765
VZ
2447 // init attr cache
2448 m_attrCache.row = -1;
2b5f62a0
VZ
2449 m_attrCache.col = -1;
2450 m_attrCache.attr = NULL;
0a976765 2451
ff72f628 2452 m_labelFont = GetFont();
52d6f640 2453 m_labelFont.SetWeight( wxBOLD );
8f177c8e 2454
73145b0e 2455 m_rowLabelHorizAlign = wxALIGN_CENTRE;
4c7277db 2456 m_rowLabelVertAlign = wxALIGN_CENTRE;
f85afd4e 2457
4c7277db 2458 m_colLabelHorizAlign = wxALIGN_CENTRE;
73145b0e 2459 m_colLabelVertAlign = wxALIGN_CENTRE;
d43851f7 2460 m_colLabelTextOrientation = wxHORIZONTAL;
f85afd4e 2461
f85afd4e 2462 m_defaultColWidth = WXGRID_DEFAULT_COL_WIDTH;
ad805b9e 2463 m_defaultRowHeight = 0; // this will be initialized after creation
1f1ce288 2464
b8d24d4e
RG
2465 m_minAcceptableColWidth = WXGRID_MIN_COL_WIDTH;
2466 m_minAcceptableRowHeight = WXGRID_MIN_ROW_HEIGHT;
2467
73145b0e 2468 m_gridLineColour = wxColour( 192,192,192 );
ca65c044 2469 m_gridLinesEnabled = true;
9f7aee01
VZ
2470 m_gridLinesClipHorz =
2471 m_gridLinesClipVert = true;
73145b0e 2472 m_cellHighlightColour = *wxBLACK;
bf7945ce 2473 m_cellHighlightPenWidth = 2;
d2520c85 2474 m_cellHighlightROPenWidth = 1;
8f177c8e 2475
d4175745
VZ
2476 m_canDragColMove = false;
2477
58dd5b3b 2478 m_cursorMode = WXGRID_CURSOR_SELECT_CELL;
10a4531d 2479 m_winCapture = NULL;
ca65c044
WS
2480 m_canDragRowSize = true;
2481 m_canDragColSize = true;
2482 m_canDragGridSize = true;
79dbea21 2483 m_canDragCell = false;
f85afd4e
MB
2484 m_dragLastPos = -1;
2485 m_dragRowOrCol = -1;
ca65c044 2486 m_isDragging = false;
07296f0b 2487 m_startDragPos = wxDefaultPosition;
ad805b9e 2488
11393d29
VZ
2489 m_sortCol = wxNOT_FOUND;
2490 m_sortIsAscending = true;
2491
ad805b9e 2492 m_useNativeHeader =
71cf399f 2493 m_nativeColumnLabels = false;
f85afd4e 2494
ca65c044 2495 m_waitForSlowClick = false;
025562fe 2496
f85afd4e
MB
2497 m_rowResizeCursor = wxCursor( wxCURSOR_SIZENS );
2498 m_colResizeCursor = wxCursor( wxCURSOR_SIZEWE );
2499
2500 m_currentCellCoords = wxGridNoCellCoords;
f85afd4e 2501
ad805b9e
VZ
2502 m_selectedBlockTopLeft =
2503 m_selectedBlockBottomRight =
2504 m_selectedBlockCorner = wxGridNoCellCoords;
b524b5c6 2505
d43851f7
JS
2506 m_selectionBackground = wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT);
2507 m_selectionForeground = wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT);
8f177c8e 2508
ca65c044 2509 m_editable = true; // default for whole grid
f85afd4e 2510
ca65c044 2511 m_inOnKeyDown = false;
2d66e025 2512 m_batchCount = 0;
3c79cf49 2513
266e8367 2514 m_extraWidth =
526dbb95 2515 m_extraHeight = 0;
608754c4 2516
fd76c6a7
VZ
2517 // we can't call SetScrollRate() as the window isn't created yet but OTOH
2518 // we don't need to call it neither as the scroll position is (0, 0) right
2519 // now anyhow, so just set the parameters directly
2520 m_xScrollPixelsPerLine = GRID_SCROLL_LINE_X;
2521 m_yScrollPixelsPerLine = GRID_SCROLL_LINE_Y;
1dc17bca
VZ
2522
2523 m_tabBehaviour = Tab_Stop;
7c1cb261
VZ
2524}
2525
2526// ----------------------------------------------------------------------------
2527// the idea is to call these functions only when necessary because they create
2528// quite big arrays which eat memory mostly unnecessary - in particular, if
2529// default widths/heights are used for all rows/columns, we may not use these
2530// arrays at all
2531//
0ed3b812
VZ
2532// with some extra code, it should be possible to only store the widths/heights
2533// different from default ones (resulting in space savings for huge grids) but
2534// this is not done currently
7c1cb261
VZ
2535// ----------------------------------------------------------------------------
2536
2537void wxGrid::InitRowHeights()
2538{
2539 m_rowHeights.Empty();
2540 m_rowBottoms.Empty();
2541
2542 m_rowHeights.Alloc( m_numRows );
2543 m_rowBottoms.Alloc( m_numRows );
2544
27f35b66
SN
2545 m_rowHeights.Add( m_defaultRowHeight, m_numRows );
2546
0ed3b812 2547 int rowBottom = 0;
3d59537f 2548 for ( int i = 0; i < m_numRows; i++ )
7c1cb261 2549 {
7c1cb261
VZ
2550 rowBottom += m_defaultRowHeight;
2551 m_rowBottoms.Add( rowBottom );
2552 }
2553}
2554
2555void wxGrid::InitColWidths()
2556{
2557 m_colWidths.Empty();
2558 m_colRights.Empty();
2559
2560 m_colWidths.Alloc( m_numCols );
2561 m_colRights.Alloc( m_numCols );
27f35b66
SN
2562
2563 m_colWidths.Add( m_defaultColWidth, m_numCols );
2564
3d59537f 2565 for ( int i = 0; i < m_numCols; i++ )
7c1cb261 2566 {
e822d1bd 2567 int colRight = ( GetColPos( i ) + 1 ) * m_defaultColWidth;
7c1cb261
VZ
2568 m_colRights.Add( colRight );
2569 }
2570}
2571
2572int wxGrid::GetColWidth(int col) const
2573{
30496905
VZ
2574 if ( m_colWidths.IsEmpty() )
2575 return m_defaultColWidth;
2576
2577 // a negative width indicates a hidden column
2578 return m_colWidths[col] > 0 ? m_colWidths[col] : 0;
7c1cb261
VZ
2579}
2580
2581int wxGrid::GetColLeft(int col) const
2582{
30496905
VZ
2583 if ( m_colRights.IsEmpty() )
2584 return GetColPos( col ) * m_defaultColWidth;
2585
2586 return m_colRights[col] - GetColWidth(col);
7c1cb261
VZ
2587}
2588
2589int wxGrid::GetColRight(int col) const
2590{
d4175745 2591 return m_colRights.IsEmpty() ? (GetColPos( col ) + 1) * m_defaultColWidth
7c1cb261
VZ
2592 : m_colRights[col];
2593}
2594
2595int wxGrid::GetRowHeight(int row) const
2596{
30496905
VZ
2597 // no custom heights / hidden rows
2598 if ( m_rowHeights.IsEmpty() )
2599 return m_defaultRowHeight;
2600
2601 // a negative height indicates a hidden row
2602 return m_rowHeights[row] > 0 ? m_rowHeights[row] : 0;
7c1cb261 2603}
2d66e025 2604
7c1cb261
VZ
2605int wxGrid::GetRowTop(int row) const
2606{
30496905
VZ
2607 if ( m_rowBottoms.IsEmpty() )
2608 return row * m_defaultRowHeight;
2609
2610 return m_rowBottoms[row] - GetRowHeight(row);
f85afd4e
MB
2611}
2612
7c1cb261
VZ
2613int wxGrid::GetRowBottom(int row) const
2614{
2615 return m_rowBottoms.IsEmpty() ? (row + 1) * m_defaultRowHeight
2616 : m_rowBottoms[row];
2617}
f85afd4e
MB
2618
2619void wxGrid::CalcDimensions()
2620{
0ed3b812
VZ
2621 // compute the size of the scrollable area
2622 int w = m_numCols > 0 ? GetColRight(GetColAt(m_numCols - 1)) : 0;
2623 int h = m_numRows > 0 ? GetRowBottom(m_numRows - 1) : 0;
60ff3b99 2624
0ed3b812
VZ
2625 w += m_extraWidth;
2626 h += m_extraHeight;
faec5a43 2627
73145b0e 2628 // take into account editor if shown
4db6714b 2629 if ( IsCellEditControlShown() )
73145b0e 2630 {
3d59537f
DS
2631 int w2, h2;
2632 int r = m_currentCellCoords.GetRow();
2633 int c = m_currentCellCoords.GetCol();
2634 int x = GetColLeft(c);
2635 int y = GetRowTop(r);
2636
2637 // how big is the editor
2638 wxGridCellAttr* attr = GetCellAttr(r, c);
2639 wxGridCellEditor* editor = attr->GetEditor(this, r, c);
2640 editor->GetControl()->GetSize(&w2, &h2);
2641 w2 += x;
2642 h2 += y;
2643 if ( w2 > w )
2644 w = w2;
2645 if ( h2 > h )
2646 h = h2;
2647 editor->DecRef();
2648 attr->DecRef();
73145b0e
JS
2649 }
2650
faec5a43
SN
2651 // preserve (more or less) the previous position
2652 int x, y;
2653 GetViewStart( &x, &y );
97a9929e 2654
c92ed9f7 2655 // ensure the position is valid for the new scroll ranges
7b519e5e 2656 if ( x >= w )
c92ed9f7 2657 x = wxMax( w - 1, 0 );
7b519e5e 2658 if ( y >= h )
c92ed9f7 2659 y = wxMax( h - 1, 0 );
faec5a43 2660
ace8d849 2661 // update the virtual size and refresh the scrollbars to reflect it
f1ff7df0
VZ
2662 m_gridWin->SetVirtualSize(w, h);
2663 Scroll(x, y);
ace8d849 2664 AdjustScrollbars();
12314291
VZ
2665
2666 // if our OnSize() hadn't been called (it would if we have scrollbars), we
2667 // still must reposition the children
2668 CalcWindowSizes();
f85afd4e
MB
2669}
2670
69367c56
VZ
2671wxSize wxGrid::GetSizeAvailableForScrollTarget(const wxSize& size)
2672{
2673 wxSize sizeGridWin(size);
2674 sizeGridWin.x -= m_rowLabelWidth;
2675 sizeGridWin.y -= m_colLabelHeight;
2676
2677 return sizeGridWin;
2678}
2679
7807d81c
MB
2680void wxGrid::CalcWindowSizes()
2681{
b0a877ec
SC
2682 // escape if the window is has not been fully created yet
2683
2684 if ( m_cornerLabelWin == NULL )
2f024384 2685 return;
b0a877ec 2686
7807d81c
MB
2687 int cw, ch;
2688 GetClientSize( &cw, &ch );
b99be8fb 2689
c1841ac2
VZ
2690 // the grid may be too small to have enough space for the labels yet, don't
2691 // size the windows to negative sizes in this case
2692 int gw = cw - m_rowLabelWidth;
2693 int gh = ch - m_colLabelHeight;
2694 if (gw < 0)
2695 gw = 0;
2696 if (gh < 0)
2697 gh = 0;
2698
6d308072 2699 if ( m_cornerLabelWin && m_cornerLabelWin->IsShown() )
7807d81c
MB
2700 m_cornerLabelWin->SetSize( 0, 0, m_rowLabelWidth, m_colLabelHeight );
2701
ad805b9e
VZ
2702 if ( m_colWindow && m_colWindow->IsShown() )
2703 m_colWindow->SetSize( m_rowLabelWidth, 0, gw, m_colLabelHeight );
7807d81c 2704
6d308072 2705 if ( m_rowLabelWin && m_rowLabelWin->IsShown() )
c1841ac2 2706 m_rowLabelWin->SetSize( 0, m_colLabelHeight, m_rowLabelWidth, gh );
7807d81c 2707
6d308072 2708 if ( m_gridWin && m_gridWin->IsShown() )
c1841ac2 2709 m_gridWin->SetSize( m_rowLabelWidth, m_colLabelHeight, gw, gh );
7807d81c
MB
2710}
2711
3d59537f
DS
2712// this is called when the grid table sends a message
2713// to indicate that it has been redimensioned
f85afd4e
MB
2714//
2715bool wxGrid::Redimension( wxGridTableMessage& msg )
2716{
2717 int i;
ca65c044 2718 bool result = false;
8f177c8e 2719
a6794685
SN
2720 // Clear the attribute cache as the attribute might refer to a different
2721 // cell than stored in the cache after adding/removing rows/columns.
2722 ClearAttrCache();
2f024384 2723
7e48d7d9
SN
2724 // By the same reasoning, the editor should be dismissed if columns are
2725 // added or removed. And for consistency, it should IMHO always be
2726 // removed, not only if the cell "underneath" it actually changes.
2727 // For now, I intentionally do not save the editor's content as the
2728 // cell it might want to save that stuff to might no longer exist.
bca7bfc8 2729 HideCellEditControl();
2f024384 2730
f85afd4e
MB
2731 switch ( msg.GetId() )
2732 {
2733 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED:
2734 {
2735 size_t pos = msg.GetCommandInt();
2736 int numRows = msg.GetCommandInt2();
f6bcfd97 2737
f85afd4e 2738 m_numRows += numRows;
2d66e025 2739
f6bcfd97
BP
2740 if ( !m_rowHeights.IsEmpty() )
2741 {
27f35b66
SN
2742 m_rowHeights.Insert( m_defaultRowHeight, pos, numRows );
2743 m_rowBottoms.Insert( 0, pos, numRows );
f6bcfd97
BP
2744
2745 int bottom = 0;
2f024384
DS
2746 if ( pos > 0 )
2747 bottom = m_rowBottoms[pos - 1];
60ff3b99 2748
3d59537f 2749 for ( i = pos; i < m_numRows; i++ )
f6bcfd97
BP
2750 {
2751 bottom += m_rowHeights[i];
2752 m_rowBottoms[i] = bottom;
2753 }
2754 }
2f024384 2755
f6bcfd97
BP
2756 if ( m_currentCellCoords == wxGridNoCellCoords )
2757 {
2758 // if we have just inserted cols into an empty grid the current
2759 // cell will be undefined...
2760 //
2761 SetCurrentCell( 0, 0 );
2762 }
3f3dc2ef
VZ
2763
2764 if ( m_selection )
2765 m_selection->UpdateRows( pos, numRows );
f6bcfd97
BP
2766 wxGridCellAttrProvider * attrProvider = m_table->GetAttrProvider();
2767 if (attrProvider)
2768 attrProvider->UpdateAttrRows( pos, numRows );
2769
2770 if ( !GetBatchCount() )
2d66e025 2771 {
f6bcfd97
BP
2772 CalcDimensions();
2773 m_rowLabelWin->Refresh();
2d66e025 2774 }
f85afd4e 2775 }
ca65c044 2776 result = true;
f6bcfd97 2777 break;
f85afd4e
MB
2778
2779 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED:
2780 {
2781 int numRows = msg.GetCommandInt();
2d66e025 2782 int oldNumRows = m_numRows;
f85afd4e 2783 m_numRows += numRows;
2d66e025 2784
f6bcfd97
BP
2785 if ( !m_rowHeights.IsEmpty() )
2786 {
27f35b66
SN
2787 m_rowHeights.Add( m_defaultRowHeight, numRows );
2788 m_rowBottoms.Add( 0, numRows );
60ff3b99 2789
f6bcfd97 2790 int bottom = 0;
2f024384
DS
2791 if ( oldNumRows > 0 )
2792 bottom = m_rowBottoms[oldNumRows - 1];
f6bcfd97 2793
3d59537f 2794 for ( i = oldNumRows; i < m_numRows; i++ )
f6bcfd97
BP
2795 {
2796 bottom += m_rowHeights[i];
2797 m_rowBottoms[i] = bottom;
2798 }
2799 }
2f024384 2800
f6bcfd97
BP
2801 if ( m_currentCellCoords == wxGridNoCellCoords )
2802 {
2803 // if we have just inserted cols into an empty grid the current
2804 // cell will be undefined...
2805 //
2806 SetCurrentCell( 0, 0 );
2807 }
2f024384 2808
f6bcfd97 2809 if ( !GetBatchCount() )
2d66e025 2810 {
f6bcfd97
BP
2811 CalcDimensions();
2812 m_rowLabelWin->Refresh();
2d66e025 2813 }
f85afd4e 2814 }
ca65c044 2815 result = true;
f6bcfd97 2816 break;
f85afd4e
MB
2817
2818 case wxGRIDTABLE_NOTIFY_ROWS_DELETED:
2819 {
2820 size_t pos = msg.GetCommandInt();
2821 int numRows = msg.GetCommandInt2();
f85afd4e
MB
2822 m_numRows -= numRows;
2823
f6bcfd97 2824 if ( !m_rowHeights.IsEmpty() )
f85afd4e 2825 {
27f35b66
SN
2826 m_rowHeights.RemoveAt( pos, numRows );
2827 m_rowBottoms.RemoveAt( pos, numRows );
2d66e025
MB
2828
2829 int h = 0;
3d59537f 2830 for ( i = 0; i < m_numRows; i++ )
2d66e025
MB
2831 {
2832 h += m_rowHeights[i];
2833 m_rowBottoms[i] = h;
2834 }
f85afd4e 2835 }
3d59537f 2836
f6bcfd97
BP
2837 if ( !m_numRows )
2838 {
2839 m_currentCellCoords = wxGridNoCellCoords;
2840 }
2841 else
2842 {
2843 if ( m_currentCellCoords.GetRow() >= m_numRows )
2844 m_currentCellCoords.Set( 0, 0 );
2845 }
3f3dc2ef
VZ
2846
2847 if ( m_selection )
2848 m_selection->UpdateRows( pos, -((int)numRows) );
f6bcfd97 2849 wxGridCellAttrProvider * attrProvider = m_table->GetAttrProvider();
4db6714b
KH
2850 if (attrProvider)
2851 {
f6bcfd97 2852 attrProvider->UpdateAttrRows( pos, -((int)numRows) );
3d59537f 2853
84912ef8
RD
2854// ifdef'd out following patch from Paul Gammans
2855#if 0
3ca6a5f0 2856 // No need to touch column attributes, unless we
f6bcfd97
BP
2857 // removed _all_ rows, in this case, we remove
2858 // all column attributes.
2859 // I hate to do this here, but the
2860 // needed data is not available inside UpdateAttrRows.
2861 if ( !GetNumberRows() )
2862 attrProvider->UpdateAttrCols( 0, -GetNumberCols() );
84912ef8 2863#endif
f6bcfd97 2864 }
ccdee36f 2865
f6bcfd97
BP
2866 if ( !GetBatchCount() )
2867 {
2868 CalcDimensions();
2869 m_rowLabelWin->Refresh();
2870 }
f85afd4e 2871 }
ca65c044 2872 result = true;
f6bcfd97 2873 break;
f85afd4e
MB
2874
2875 case wxGRIDTABLE_NOTIFY_COLS_INSERTED:
2876 {
2877 size_t pos = msg.GetCommandInt();
2878 int numCols = msg.GetCommandInt2();
f85afd4e 2879 m_numCols += numCols;
2d66e025 2880
ad805b9e 2881 if ( m_useNativeHeader )
3039ade9 2882 GetGridColHeader()->SetColumnCount(m_numCols);
ad805b9e 2883
d4175745
VZ
2884 if ( !m_colAt.IsEmpty() )
2885 {
2886 //Shift the column IDs
d4175745
VZ
2887 for ( i = 0; i < m_numCols - numCols; i++ )
2888 {
2889 if ( m_colAt[i] >= (int)pos )
2890 m_colAt[i] += numCols;
2891 }
2892
2893 m_colAt.Insert( pos, pos, numCols );
2894
2895 //Set the new columns' positions
2896 for ( i = pos + 1; i < (int)pos + numCols; i++ )
2897 {
2898 m_colAt[i] = i;
2899 }
2900 }
2901
f6bcfd97
BP
2902 if ( !m_colWidths.IsEmpty() )
2903 {
27f35b66
SN
2904 m_colWidths.Insert( m_defaultColWidth, pos, numCols );
2905 m_colRights.Insert( 0, pos, numCols );
f6bcfd97
BP
2906
2907 int right = 0;
2f024384 2908 if ( pos > 0 )
d4175745 2909 right = m_colRights[GetColAt( pos - 1 )];
60ff3b99 2910
d4175745
VZ
2911 int colPos;
2912 for ( colPos = pos; colPos < m_numCols; colPos++ )
f6bcfd97 2913 {
d4175745
VZ
2914 i = GetColAt( colPos );
2915
f6bcfd97
BP
2916 right += m_colWidths[i];
2917 m_colRights[i] = right;
2918 }
2919 }
2f024384 2920
f6bcfd97 2921 if ( m_currentCellCoords == wxGridNoCellCoords )
2d66e025 2922 {
f6bcfd97
BP
2923 // if we have just inserted cols into an empty grid the current
2924 // cell will be undefined...
2925 //
2926 SetCurrentCell( 0, 0 );
2d66e025 2927 }
3f3dc2ef
VZ
2928
2929 if ( m_selection )
2930 m_selection->UpdateCols( pos, numCols );
f6bcfd97
BP
2931 wxGridCellAttrProvider * attrProvider = m_table->GetAttrProvider();
2932 if (attrProvider)
2933 attrProvider->UpdateAttrCols( pos, numCols );
2934 if ( !GetBatchCount() )
2935 {
2936 CalcDimensions();
ad805b9e 2937 m_colWindow->Refresh();
f6bcfd97 2938 }
f85afd4e 2939 }
ca65c044 2940 result = true;
f6bcfd97 2941 break;
f85afd4e
MB
2942
2943 case wxGRIDTABLE_NOTIFY_COLS_APPENDED:
2944 {
2945 int numCols = msg.GetCommandInt();
2d66e025 2946 int oldNumCols = m_numCols;
f85afd4e 2947 m_numCols += numCols;
d4175745
VZ
2948
2949 if ( !m_colAt.IsEmpty() )
2950 {
2951 m_colAt.Add( 0, numCols );
2952
2953 //Set the new columns' positions
d4175745
VZ
2954 for ( i = oldNumCols; i < m_numCols; i++ )
2955 {
2956 m_colAt[i] = i;
2957 }
2958 }
2959
f6bcfd97
BP
2960 if ( !m_colWidths.IsEmpty() )
2961 {
27f35b66
SN
2962 m_colWidths.Add( m_defaultColWidth, numCols );
2963 m_colRights.Add( 0, numCols );
2d66e025 2964
f6bcfd97 2965 int right = 0;
2f024384 2966 if ( oldNumCols > 0 )
d4175745 2967 right = m_colRights[GetColAt( oldNumCols - 1 )];
60ff3b99 2968
d4175745
VZ
2969 int colPos;
2970 for ( colPos = oldNumCols; colPos < m_numCols; colPos++ )
f6bcfd97 2971 {
d4175745
VZ
2972 i = GetColAt( colPos );
2973
f6bcfd97
BP
2974 right += m_colWidths[i];
2975 m_colRights[i] = right;
2976 }
2977 }
2f024384 2978
a2d65663
VZ
2979 // Notice that this must be called after updating m_colWidths above
2980 // as the native grid control will check whether the new columns
2981 // are shown which results in accessing m_colWidths array.
2982 if ( m_useNativeHeader )
2983 GetGridColHeader()->SetColumnCount(m_numCols);
2984
f6bcfd97 2985 if ( m_currentCellCoords == wxGridNoCellCoords )
2d66e025 2986 {
f6bcfd97
BP
2987 // if we have just inserted cols into an empty grid the current
2988 // cell will be undefined...
2989 //
2990 SetCurrentCell( 0, 0 );
2991 }
2992 if ( !GetBatchCount() )
2993 {
2994 CalcDimensions();
ad805b9e 2995 m_colWindow->Refresh();
2d66e025 2996 }
f85afd4e 2997 }
ca65c044 2998 result = true;
f6bcfd97 2999 break;
f85afd4e
MB
3000
3001 case wxGRIDTABLE_NOTIFY_COLS_DELETED:
3002 {
3003 size_t pos = msg.GetCommandInt();
3004 int numCols = msg.GetCommandInt2();
f85afd4e 3005 m_numCols -= numCols;
ad805b9e 3006 if ( m_useNativeHeader )
3039ade9 3007 GetGridColHeader()->SetColumnCount(m_numCols);
f85afd4e 3008
d4175745
VZ
3009 if ( !m_colAt.IsEmpty() )
3010 {
3011 int colID = GetColAt( pos );
3012
3013 m_colAt.RemoveAt( pos, numCols );
3014
3015 //Shift the column IDs
3016 int colPos;
3017 for ( colPos = 0; colPos < m_numCols; colPos++ )
3018 {
3019 if ( m_colAt[colPos] > colID )
3020 m_colAt[colPos] -= numCols;
3021 }
3022 }
3023
f6bcfd97 3024 if ( !m_colWidths.IsEmpty() )
f85afd4e 3025 {
27f35b66
SN
3026 m_colWidths.RemoveAt( pos, numCols );
3027 m_colRights.RemoveAt( pos, numCols );
2d66e025
MB
3028
3029 int w = 0;
d4175745
VZ
3030 int colPos;
3031 for ( colPos = 0; colPos < m_numCols; colPos++ )
2d66e025 3032 {
d4175745
VZ
3033 i = GetColAt( colPos );
3034
2d66e025
MB
3035 w += m_colWidths[i];
3036 m_colRights[i] = w;
3037 }
f85afd4e 3038 }
2f024384 3039
f6bcfd97
BP
3040 if ( !m_numCols )
3041 {
3042 m_currentCellCoords = wxGridNoCellCoords;
3043 }
3044 else
3045 {
3046 if ( m_currentCellCoords.GetCol() >= m_numCols )
3047 m_currentCellCoords.Set( 0, 0 );
3048 }
3f3dc2ef
VZ
3049
3050 if ( m_selection )
3051 m_selection->UpdateCols( pos, -((int)numCols) );
f6bcfd97 3052 wxGridCellAttrProvider * attrProvider = m_table->GetAttrProvider();
4db6714b
KH
3053 if (attrProvider)
3054 {
f6bcfd97 3055 attrProvider->UpdateAttrCols( pos, -((int)numCols) );
ccdee36f 3056
84912ef8
RD
3057// ifdef'd out following patch from Paul Gammans
3058#if 0
f6bcfd97
BP
3059 // No need to touch row attributes, unless we
3060 // removed _all_ columns, in this case, we remove
3061 // all row attributes.
3062 // I hate to do this here, but the
3063 // needed data is not available inside UpdateAttrCols.
3064 if ( !GetNumberCols() )
3065 attrProvider->UpdateAttrRows( 0, -GetNumberRows() );
84912ef8 3066#endif
f6bcfd97 3067 }
ccdee36f 3068
f6bcfd97
BP
3069 if ( !GetBatchCount() )
3070 {
3071 CalcDimensions();
ad805b9e 3072 m_colWindow->Refresh();
f6bcfd97 3073 }
f85afd4e 3074 }
ca65c044 3075 result = true;
f6bcfd97 3076 break;
f85afd4e
MB
3077 }
3078
a6b40a9a
VZ
3079 InvalidateBestSize();
3080
f6bcfd97
BP
3081 if (result && !GetBatchCount() )
3082 m_gridWin->Refresh();
2f024384 3083
f6bcfd97 3084 return result;
f85afd4e
MB
3085}
3086
ef316e23 3087wxArrayInt wxGrid::CalcRowLabelsExposed( const wxRegion& reg ) const
f85afd4e 3088{
2d66e025
MB
3089 wxRegionIterator iter( reg );
3090 wxRect r;
f85afd4e 3091
275c4ae4
RD
3092 wxArrayInt rowlabels;
3093
2d66e025
MB
3094 int top, bottom;
3095 while ( iter )
f85afd4e 3096 {
2d66e025 3097 r = iter.GetRect();
f85afd4e 3098
2d66e025
MB
3099 // TODO: remove this when we can...
3100 // There is a bug in wxMotif that gives garbage update
3101 // rectangles if you jump-scroll a long way by clicking the
3102 // scrollbar with middle button. This is a work-around
3103 //
3104#if defined(__WXMOTIF__)
3105 int cw, ch;
3106 m_gridWin->GetClientSize( &cw, &ch );
56b6cf26
DS
3107 if ( r.GetTop() > ch )
3108 r.SetTop( 0 );
2d66e025
MB
3109 r.SetBottom( wxMin( r.GetBottom(), ch ) );
3110#endif
f85afd4e 3111
2d66e025
MB
3112 // logical bounds of update region
3113 //
3114 int dummy;
3115 CalcUnscrolledPosition( 0, r.GetTop(), &dummy, &top );
3116 CalcUnscrolledPosition( 0, r.GetBottom(), &dummy, &bottom );
3117
3118 // find the row labels within these bounds
3119 //
3120 int row;
3d59537f 3121 for ( row = internalYToRow(top); row < m_numRows; row++ )
2d66e025 3122 {
7c1cb261
VZ
3123 if ( GetRowBottom(row) < top )
3124 continue;
2d66e025 3125
6d55126d 3126 if ( GetRowTop(row) > bottom )
7c1cb261 3127 break;
60ff3b99 3128
d10f4bf9 3129 rowlabels.Add( row );
2d66e025 3130 }
60ff3b99 3131
60d8e886 3132 ++iter;
f85afd4e 3133 }
d10f4bf9
VZ
3134
3135 return rowlabels;
f85afd4e
MB
3136}
3137
ef316e23 3138wxArrayInt wxGrid::CalcColLabelsExposed( const wxRegion& reg ) const
f85afd4e 3139{
2d66e025
MB
3140 wxRegionIterator iter( reg );
3141 wxRect r;
f85afd4e 3142
d10f4bf9 3143 wxArrayInt colLabels;
f85afd4e 3144
2d66e025
MB
3145 int left, right;
3146 while ( iter )
f85afd4e 3147 {
2d66e025 3148 r = iter.GetRect();
f85afd4e 3149
2d66e025
MB
3150 // TODO: remove this when we can...
3151 // There is a bug in wxMotif that gives garbage update
3152 // rectangles if you jump-scroll a long way by clicking the
3153 // scrollbar with middle button. This is a work-around
3154 //
3155#if defined(__WXMOTIF__)
3156 int cw, ch;
3157 m_gridWin->GetClientSize( &cw, &ch );
56b6cf26
DS
3158 if ( r.GetLeft() > cw )
3159 r.SetLeft( 0 );
2d66e025
MB
3160 r.SetRight( wxMin( r.GetRight(), cw ) );
3161#endif
3162
3163 // logical bounds of update region
3164 //
3165 int dummy;
3166 CalcUnscrolledPosition( r.GetLeft(), 0, &left, &dummy );
3167 CalcUnscrolledPosition( r.GetRight(), 0, &right, &dummy );
3168
3169 // find the cells within these bounds
3170 //
3171 int col;
d4175745
VZ
3172 int colPos;
3173 for ( colPos = GetColPos( internalXToCol(left) ); colPos < m_numCols; colPos++ )
2d66e025 3174 {
d4175745
VZ
3175 col = GetColAt( colPos );
3176
7c1cb261
VZ
3177 if ( GetColRight(col) < left )
3178 continue;
60ff3b99 3179
7c1cb261
VZ
3180 if ( GetColLeft(col) > right )
3181 break;
2d66e025 3182
d10f4bf9 3183 colLabels.Add( col );
2d66e025 3184 }
60ff3b99 3185
60d8e886 3186 ++iter;
f85afd4e 3187 }
2f024384 3188
d10f4bf9 3189 return colLabels;
f85afd4e
MB
3190}
3191
ef316e23 3192wxGridCellCoordsArray wxGrid::CalcCellsExposed( const wxRegion& reg ) const
f85afd4e 3193{
2d66e025 3194 wxRect r;
f85afd4e 3195
d10f4bf9 3196 wxGridCellCoordsArray cellsExposed;
f85afd4e 3197
2d66e025 3198 int left, top, right, bottom;
9e57072a 3199 for ( wxRegionIterator iter(reg); iter; ++iter )
2d66e025
MB
3200 {
3201 r = iter.GetRect();
f85afd4e 3202
9e57072a
VZ
3203 // Skip 0-height cells, they're invisible anyhow, don't waste time
3204 // getting their rectangles and so on.
3205 if ( !r.GetHeight() )
3206 continue;
3207
2d66e025
MB
3208 // TODO: remove this when we can...
3209 // There is a bug in wxMotif that gives garbage update
3210 // rectangles if you jump-scroll a long way by clicking the
3211 // scrollbar with middle button. This is a work-around
3212 //
3213#if defined(__WXMOTIF__)
f85afd4e 3214 int cw, ch;
2d66e025
MB
3215 m_gridWin->GetClientSize( &cw, &ch );
3216 if ( r.GetTop() > ch ) r.SetTop( 0 );
3217 if ( r.GetLeft() > cw ) r.SetLeft( 0 );
3218 r.SetRight( wxMin( r.GetRight(), cw ) );
3219 r.SetBottom( wxMin( r.GetBottom(), ch ) );
3220#endif
8f177c8e 3221
2d66e025
MB
3222 // logical bounds of update region
3223 //
3224 CalcUnscrolledPosition( r.GetLeft(), r.GetTop(), &left, &top );
3225 CalcUnscrolledPosition( r.GetRight(), r.GetBottom(), &right, &bottom );
f85afd4e 3226
2d66e025 3227 // find the cells within these bounds
cd4f6f5f
VZ
3228 wxArrayInt cols;
3229 for ( int row = internalYToRow(top); row < m_numRows; row++ )
f85afd4e 3230 {
7c1cb261
VZ
3231 if ( GetRowBottom(row) <= top )
3232 continue;
f85afd4e 3233
7c1cb261
VZ
3234 if ( GetRowTop(row) > bottom )
3235 break;
60ff3b99 3236
cd4f6f5f
VZ
3237 // add all dirty cells in this row: notice that the columns which
3238 // are dirty don't depend on the row so we compute them only once
3239 // for the first dirty row and then reuse for all the next ones
3240 if ( cols.empty() )
2d66e025 3241 {
cd4f6f5f
VZ
3242 // do determine the dirty columns
3243 for ( int pos = XToPos(left); pos <= XToPos(right); pos++ )
3244 cols.push_back(GetColAt(pos));
d4175745 3245
cd4f6f5f
VZ
3246 // if there are no dirty columns at all, nothing to do
3247 if ( cols.empty() )
7c1cb261 3248 break;
2d66e025 3249 }
cd4f6f5f
VZ
3250
3251 const size_t count = cols.size();
3252 for ( size_t n = 0; n < count; n++ )
3253 cellsExposed.Add(wxGridCellCoords(row, cols[n]));
2d66e025 3254 }
f85afd4e 3255 }
d10f4bf9
VZ
3256
3257 return cellsExposed;
f85afd4e
MB
3258}
3259
3260
2d66e025 3261void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent& event )
f85afd4e 3262{
2d66e025
MB
3263 int x, y, row;
3264 wxPoint pos( event.GetPosition() );
3265 CalcUnscrolledPosition( pos.x, pos.y, &x, &y );
60ff3b99 3266
2d66e025 3267 if ( event.Dragging() )
f85afd4e 3268 {
426b2d87
JS
3269 if (!m_isDragging)
3270 {
ca65c044 3271 m_isDragging = true;
426b2d87
JS
3272 m_rowLabelWin->CaptureMouse();
3273 }
8f177c8e 3274
2d66e025 3275 if ( event.LeftIsDown() )
f85afd4e 3276 {
962a48f6 3277 switch ( m_cursorMode )
f85afd4e 3278 {
f85afd4e
MB
3279 case WXGRID_CURSOR_RESIZE_ROW:
3280 {
2d66e025
MB
3281 int cw, ch, left, dummy;
3282 m_gridWin->GetClientSize( &cw, &ch );
3283 CalcUnscrolledPosition( 0, 0, &left, &dummy );
60ff3b99 3284
2d66e025
MB
3285 wxClientDC dc( m_gridWin );
3286 PrepareDC( dc );
af547d51
VZ
3287 y = wxMax( y,
3288 GetRowTop(m_dragRowOrCol) +
3289 GetRowMinimalHeight(m_dragRowOrCol) );
d2fdd8d2 3290 dc.SetLogicalFunction(wxINVERT);
f85afd4e
MB
3291 if ( m_dragLastPos >= 0 )
3292 {
2d66e025 3293 dc.DrawLine( left, m_dragLastPos, left+cw, m_dragLastPos );
f85afd4e 3294 }
2d66e025
MB
3295 dc.DrawLine( left, y, left+cw, y );
3296 m_dragLastPos = y;
f85afd4e
MB
3297 }
3298 break;
3299
3300 case WXGRID_CURSOR_SELECT_ROW:
902725ee 3301 {
e32352cf 3302 if ( (row = YToRow( y )) >= 0 )
aa5e1f75 3303 {
3f3dc2ef 3304 if ( m_selection )
8b5f6d9d 3305 m_selection->SelectRow(row, event);
f85afd4e 3306 }
902725ee
WS
3307 }
3308 break;
e2b42eeb
VZ
3309
3310 // default label to suppress warnings about "enumeration value
3311 // 'xxx' not handled in switch
3312 default:
3313 break;
f85afd4e
MB
3314 }
3315 }
3316 return;
3317 }
3318
426b2d87
JS
3319 if ( m_isDragging && (event.Entering() || event.Leaving()) )
3320 return;
8f177c8e 3321
426b2d87
JS
3322 if (m_isDragging)
3323 {
ccdee36f
DS
3324 if (m_rowLabelWin->HasCapture())
3325 m_rowLabelWin->ReleaseMouse();
ca65c044 3326 m_isDragging = false;
426b2d87 3327 }
60ff3b99 3328
6d004f67
MB
3329 // ------------ Entering or leaving the window
3330 //
3331 if ( event.Entering() || event.Leaving() )
3332 {
e2b42eeb 3333 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_rowLabelWin);
6d004f67
MB
3334 }
3335
2d66e025 3336 // ------------ Left button pressed
f85afd4e 3337 //
6d004f67 3338 else if ( event.LeftDown() )
f85afd4e 3339 {
82edfbe7
VZ
3340 row = YToEdgeOfRow(y);
3341 if ( row != wxNOT_FOUND && CanDragRowSize(row) )
3342 {
3343 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW, m_rowLabelWin);
3344 }
3345 else // not a request to start resizing
f85afd4e 3346 {
2d66e025 3347 row = YToRow(y);
2f024384 3348 if ( row >= 0 &&
b54ba671 3349 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK, row, -1, event ) )
f85afd4e 3350 {
2b01fd49 3351 if ( !event.ShiftDown() && !event.CmdDown() )
aa5e1f75 3352 ClearSelection();
64e15340 3353 if ( m_selection )
3f3dc2ef
VZ
3354 {
3355 if ( event.ShiftDown() )
3356 {
8b5f6d9d
VZ
3357 m_selection->SelectBlock
3358 (
3359 m_currentCellCoords.GetRow(), 0,
3360 row, GetNumberCols() - 1,
3361 event
3362 );
3f3dc2ef
VZ
3363 }
3364 else
3365 {
8b5f6d9d 3366 m_selection->SelectRow(row, event);
3f3dc2ef
VZ
3367 }
3368 }
3369
e2b42eeb 3370 ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW, m_rowLabelWin);
f85afd4e 3371 }
2d66e025 3372 }
2d66e025 3373 }
f85afd4e 3374
2d66e025
MB
3375 // ------------ Left double click
3376 //
3377 else if (event.LeftDClick() )
3378 {
4e115ed2 3379 row = YToEdgeOfRow(y);
82edfbe7
VZ
3380 if ( row != wxNOT_FOUND && CanDragRowSize(row) )
3381 {
3382 // adjust row height depending on label text
27bb2c8c
VZ
3383 //
3384 // TODO: generate RESIZING event, see #10754
82edfbe7
VZ
3385 AutoSizeRowLabelSize( row );
3386
c5c1ea96 3387 SendGridSizeEvent(wxEVT_GRID_ROW_SIZE, row, -1, event);
27bb2c8c 3388
82edfbe7
VZ
3389 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, GetColLabelWindow());
3390 m_dragLastPos = -1;
3391 }
d13b34d3 3392 else // not on row separator or it's not resizable
2d66e025
MB
3393 {
3394 row = YToRow(y);
a967f048 3395 if ( row >=0 &&
ca65c044
WS
3396 !SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK, row, -1, event ) )
3397 {
a967f048 3398 // no default action at the moment
ca65c044 3399 }
f85afd4e
MB
3400 }
3401 }
60ff3b99 3402
2d66e025 3403 // ------------ Left button released
f85afd4e 3404 //
2d66e025 3405 else if ( event.LeftUp() )
f85afd4e 3406 {
2d66e025 3407 if ( m_cursorMode == WXGRID_CURSOR_RESIZE_ROW )
27bb2c8c 3408 DoEndDragResizeRow(event);
f85afd4e 3409
e2b42eeb
VZ
3410 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_rowLabelWin);
3411 m_dragLastPos = -1;
2d66e025 3412 }
f85afd4e 3413
2d66e025
MB
3414 // ------------ Right button down
3415 //
3416 else if ( event.RightDown() )
3417 {
3418 row = YToRow(y);
ef5df12b 3419 if ( row >=0 &&
ca65c044 3420 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK, row, -1, event ) )
2d66e025
MB
3421 {
3422 // no default action at the moment
f85afd4e
MB
3423 }
3424 }
60ff3b99 3425
2d66e025 3426 // ------------ Right double click
f85afd4e 3427 //
2d66e025
MB
3428 else if ( event.RightDClick() )
3429 {
3430 row = YToRow(y);
a967f048 3431 if ( row >= 0 &&
ca65c044 3432 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK, row, -1, event ) )
2d66e025
MB
3433 {
3434 // no default action at the moment
3435 }
3436 }
60ff3b99 3437
2d66e025 3438 // ------------ No buttons down and mouse moving
f85afd4e 3439 //
2d66e025 3440 else if ( event.Moving() )
f85afd4e 3441 {
2d66e025 3442 m_dragRowOrCol = YToEdgeOfRow( y );
82edfbe7 3443 if ( m_dragRowOrCol != wxNOT_FOUND )
8f177c8e 3444 {
2d66e025 3445 if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
8f177c8e 3446 {
82edfbe7 3447 if ( CanDragRowSize(m_dragRowOrCol) )
ca65c044 3448 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW, m_rowLabelWin, false);
8f177c8e 3449 }
2d66e025 3450 }
6d004f67 3451 else if ( m_cursorMode != WXGRID_CURSOR_SELECT_CELL )
2d66e025 3452 {
ca65c044 3453 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_rowLabelWin, false);
8f177c8e 3454 }
f85afd4e 3455 }
2d66e025
MB
3456}
3457
11393d29
VZ
3458void wxGrid::UpdateColumnSortingIndicator(int col)
3459{
3460 wxCHECK_RET( col != wxNOT_FOUND, "invalid column index" );
3461
3462 if ( m_useNativeHeader )
3039ade9 3463 GetGridColHeader()->UpdateColumn(col);
11393d29
VZ
3464 else if ( m_nativeColumnLabels )
3465 m_colWindow->Refresh();
3466 //else: sorting indicator display not yet implemented in grid version
3467}
3468
3469void wxGrid::SetSortingColumn(int col, bool ascending)
3470{
3471 if ( col == m_sortCol )
3472 {
3473 // we are already using this column for sorting (or not sorting at all)
3474 // but we might still change the sorting order, check for it
3475 if ( m_sortCol != wxNOT_FOUND && ascending != m_sortIsAscending )
3476 {
3477 m_sortIsAscending = ascending;
3478
3479 UpdateColumnSortingIndicator(m_sortCol);
3480 }
3481 }
3482 else // we're changing the column used for sorting
3483 {
3484 const int sortColOld = m_sortCol;
3485
3486 // change it before updating the column as we want GetSortingColumn()
3487 // to return the correct new value
3488 m_sortCol = col;
3489
3490 if ( sortColOld != wxNOT_FOUND )
3491 UpdateColumnSortingIndicator(sortColOld);
3492
3493 if ( m_sortCol != wxNOT_FOUND )
3494 {
3495 m_sortIsAscending = ascending;
3496 UpdateColumnSortingIndicator(m_sortCol);
3497 }
3498 }
3499}
3500
3501void wxGrid::DoColHeaderClick(int col)
3502{
3503 // we consider that the grid was resorted if this event is processed and
3504 // not vetoed
3505 if ( SendEvent(wxEVT_GRID_COL_SORT, -1, col) == 1 )
3506 {
3507 SetSortingColumn(col, IsSortingBy(col) ? !m_sortIsAscending : true);
3508 Refresh();
3509 }
3510}
3511
ad805b9e
VZ
3512void wxGrid::DoStartResizeCol(int col)
3513{
3514 m_dragRowOrCol = col;
3515 m_dragLastPos = -1;
3516 DoUpdateResizeColWidth(GetColWidth(m_dragRowOrCol));
3517}
3518
3519void wxGrid::DoUpdateResizeCol(int x)
3520{
3521 int cw, ch, dummy, top;
3522 m_gridWin->GetClientSize( &cw, &ch );
3523 CalcUnscrolledPosition( 0, 0, &dummy, &top );
3524
3525 wxClientDC dc( m_gridWin );
3526 PrepareDC( dc );
3527
3528 x = wxMax( x, GetColLeft(m_dragRowOrCol) + GetColMinimalWidth(m_dragRowOrCol));
3529 dc.SetLogicalFunction(wxINVERT);
3530 if ( m_dragLastPos >= 0 )
3531 {
3532 dc.DrawLine( m_dragLastPos, top, m_dragLastPos, top + ch );
3533 }
3534 dc.DrawLine( x, top, x, top + ch );
3535 m_dragLastPos = x;
3536}
3537
3538void wxGrid::DoUpdateResizeColWidth(int w)
3539{
3540 DoUpdateResizeCol(GetColLeft(m_dragRowOrCol) + w);
3541}
3542
2d66e025
MB
3543void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent& event )
3544{
7d1214cd
PC
3545 int x;
3546 CalcUnscrolledPosition( event.GetPosition().x, 0, &x, NULL );
60ff3b99 3547
11393d29 3548 int col = XToCol(x);
2d66e025 3549 if ( event.Dragging() )
f85afd4e 3550 {
fe77cf60
JS
3551 if (!m_isDragging)
3552 {
ca65c044 3553 m_isDragging = true;
ad805b9e 3554 GetColLabelWindow()->CaptureMouse();
d4175745 3555
11393d29
VZ
3556 if ( m_cursorMode == WXGRID_CURSOR_MOVE_COL && col != -1 )
3557 DoStartMoveCol(col);
fe77cf60 3558 }
8f177c8e 3559
2d66e025 3560 if ( event.LeftIsDown() )
8f177c8e 3561 {
962a48f6 3562 switch ( m_cursorMode )
8f177c8e 3563 {
2d66e025 3564 case WXGRID_CURSOR_RESIZE_COL:
ad805b9e 3565 DoUpdateResizeCol(x);
2d66e025 3566 break;
f85afd4e 3567
2d66e025 3568 case WXGRID_CURSOR_SELECT_COL:
902725ee 3569 {
11393d29 3570 if ( col != -1 )
aa5e1f75 3571 {
3f3dc2ef 3572 if ( m_selection )
8b5f6d9d 3573 m_selection->SelectCol(col, event);
2d66e025 3574 }
902725ee
WS
3575 }
3576 break;
e2b42eeb 3577
d4175745
VZ
3578 case WXGRID_CURSOR_MOVE_COL:
3579 {
cd68daf5
VZ
3580 int posNew = XToPos(x);
3581 int colNew = GetColAt(posNew);
d4175745 3582
cd68daf5 3583 // determine the position of the drop marker
d4175745 3584 int markerX;
cd68daf5
VZ
3585 if ( x >= GetColLeft(colNew) + (GetColWidth(colNew) / 2) )
3586 markerX = GetColRight(colNew);
d4175745 3587 else
cd68daf5 3588 markerX = GetColLeft(colNew);
d4175745
VZ
3589
3590 if ( markerX != m_dragLastPos )
3591 {
ad805b9e 3592 wxClientDC dc( GetColLabelWindow() );
1eab9659 3593 DoPrepareDC(dc);
d4175745
VZ
3594
3595 int cw, ch;
ad805b9e 3596 GetColLabelWindow()->GetClientSize( &cw, &ch );
d4175745
VZ
3597
3598 markerX++;
3599
3600 //Clean up the last indicator
3601 if ( m_dragLastPos >= 0 )
3602 {
ad805b9e 3603 wxPen pen( GetColLabelWindow()->GetBackgroundColour(), 2 );
d4175745
VZ
3604 dc.SetPen(pen);
3605 dc.DrawLine( m_dragLastPos + 1, 0, m_dragLastPos + 1, ch );
3606 dc.SetPen(wxNullPen);
3607
3608 if ( XToCol( m_dragLastPos ) != -1 )
3609 DrawColLabel( dc, XToCol( m_dragLastPos ) );
3610 }
3611
87c819f9 3612 const wxColour *color;
d4175745 3613 //Moving to the same place? Don't draw a marker
cd68daf5 3614 if ( colNew == m_dragRowOrCol )
87c819f9
VZ
3615 color = wxLIGHT_GREY;
3616 else
3617 color = wxBLUE;
d4175745
VZ
3618
3619 //Draw the marker
87c819f9 3620 wxPen pen( *color, 2 );
d4175745
VZ
3621 dc.SetPen(pen);
3622
3623 dc.DrawLine( markerX, 0, markerX, ch );
3624
3625 dc.SetPen(wxNullPen);
3626
3627 m_dragLastPos = markerX - 1;
3628 }
3629 }
3630 break;
3631
e2b42eeb
VZ
3632 // default label to suppress warnings about "enumeration value
3633 // 'xxx' not handled in switch
3634 default:
3635 break;
2d66e025 3636 }
f85afd4e 3637 }
2d66e025 3638 return;
f85afd4e 3639 }
2d66e025 3640
fe77cf60
JS
3641 if ( m_isDragging && (event.Entering() || event.Leaving()) )
3642 return;
2d66e025 3643
fe77cf60
JS
3644 if (m_isDragging)
3645 {
ad805b9e
VZ
3646 if (GetColLabelWindow()->HasCapture())
3647 GetColLabelWindow()->ReleaseMouse();
ca65c044 3648 m_isDragging = false;
fe77cf60 3649 }
60ff3b99 3650
6d004f67
MB
3651 // ------------ Entering or leaving the window
3652 //
3653 if ( event.Entering() || event.Leaving() )
3654 {
ad805b9e 3655 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, GetColLabelWindow());
6d004f67
MB
3656 }
3657
2d66e025 3658 // ------------ Left button pressed
f85afd4e 3659 //
6d004f67 3660 else if ( event.LeftDown() )
f85afd4e 3661 {
7d1214cd
PC
3662 int colEdge = XToEdgeOfCol(x);
3663 if ( colEdge != wxNOT_FOUND && CanDragColSize(colEdge) )
8f177c8e 3664 {
82edfbe7
VZ
3665 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL, GetColLabelWindow());
3666 }
3667 else // not a request to start resizing
3668 {
2f024384 3669 if ( col >= 0 &&
b54ba671 3670 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK, -1, col, event ) )
8f177c8e 3671 {
d4175745 3672 if ( m_canDragColMove )
3f3dc2ef 3673 {
d4175745 3674 //Show button as pressed
ad805b9e 3675 wxClientDC dc( GetColLabelWindow() );
d4175745
VZ
3676 int colLeft = GetColLeft( col );
3677 int colRight = GetColRight( col ) - 1;
ad805b9e 3678 dc.SetPen( wxPen( GetColLabelWindow()->GetBackgroundColour(), 1 ) );
d4175745
VZ
3679 dc.DrawLine( colLeft, 1, colLeft, m_colLabelHeight-1 );
3680 dc.DrawLine( colLeft, 1, colRight, 1 );
3681
ad805b9e 3682 ChangeCursorMode(WXGRID_CURSOR_MOVE_COL, GetColLabelWindow());
d4175745
VZ
3683 }
3684 else
3685 {
3686 if ( !event.ShiftDown() && !event.CmdDown() )
3687 ClearSelection();
3688 if ( m_selection )
3f3dc2ef 3689 {
d4175745
VZ
3690 if ( event.ShiftDown() )
3691 {
8b5f6d9d
VZ
3692 m_selection->SelectBlock
3693 (
3694 0, m_currentCellCoords.GetCol(),
3695 GetNumberRows() - 1, col,
3696 event
3697 );
d4175745
VZ
3698 }
3699 else
3700 {
8b5f6d9d 3701 m_selection->SelectCol(col, event);
d4175745 3702 }
3f3dc2ef 3703 }
3f3dc2ef 3704
ad805b9e 3705 ChangeCursorMode(WXGRID_CURSOR_SELECT_COL, GetColLabelWindow());
d4175745 3706 }
f85afd4e 3707 }
2d66e025 3708 }
2d66e025 3709 }
f85afd4e 3710
2d66e025
MB
3711 // ------------ Left double click
3712 //
3713 if ( event.LeftDClick() )
3714 {
11393d29
VZ
3715 const int colEdge = XToEdgeOfCol(x);
3716 if ( colEdge == -1 )
2d66e025 3717 {
a967f048
RG
3718 if ( col >= 0 &&
3719 ! SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK, -1, col, event ) )
3720 {
ca65c044 3721 // no default action at the moment
a967f048 3722 }
2d66e025 3723 }
d43851f7
JS
3724 else
3725 {
3726 // adjust column width depending on label text
27bb2c8c
VZ
3727 //
3728 // TODO: generate RESIZING event, see #10754
6f58f3d7
VZ
3729 if ( !SendGridSizeEvent(wxEVT_GRID_COL_AUTO_SIZE, -1, colEdge, event) )
3730 AutoSizeColLabelSize( colEdge );
d43851f7 3731
c5c1ea96 3732 SendGridSizeEvent(wxEVT_GRID_COL_SIZE, -1, colEdge, event);
27bb2c8c 3733
ad805b9e 3734 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, GetColLabelWindow());
ccdee36f 3735 m_dragLastPos = -1;
d43851f7 3736 }
2d66e025 3737 }
60ff3b99 3738
2d66e025
MB
3739 // ------------ Left button released
3740 //
3741 else if ( event.LeftUp() )
3742 {
d4175745 3743 switch ( m_cursorMode )
2d66e025 3744 {
d4175745 3745 case WXGRID_CURSOR_RESIZE_COL:
27bb2c8c 3746 DoEndDragResizeCol(event);
852b6c3c 3747 break;
d4175745
VZ
3748
3749 case WXGRID_CURSOR_MOVE_COL:
11393d29 3750 if ( m_dragLastPos == -1 || col == m_dragRowOrCol )
cd68daf5 3751 {
11393d29
VZ
3752 // the column didn't actually move anywhere
3753 if ( col != -1 )
3754 DoColHeaderClick(col);
cd68daf5
VZ
3755 m_colWindow->Refresh(); // "unpress" the column
3756 }
3757 else
3758 {
7c331059
VZ
3759 // get the position of the column we're over
3760 int pos = XToPos(x);
3761
3762 // we may need to adjust the drop position but don't bother
3763 // checking for it if we can't anyhow
3764 if ( pos > 1 )
3765 {
3766 // also find the index of the column we're over: notice
3767 // that the existing "col" variable may be invalid but
3768 // we need a valid one here
3769 const int colValid = GetColAt(pos);
3770
3771 // if we're on the "near" (usually left but right in
3772 // RTL case) part of the column, the actual position we
3773 // should be placed in is actually the one before it
18c0369b 3774 bool onNearPart;
7c331059
VZ
3775 const int middle = GetColLeft(colValid) +
3776 GetColWidth(colValid)/2;
3777 if ( GetLayoutDirection() == wxLayout_LeftToRight )
18c0369b 3778 onNearPart = (x <= middle);
7c331059 3779 else // wxLayout_RightToLeft
18c0369b 3780 onNearPart = (x > middle);
7c331059 3781
18c0369b 3782 if ( onNearPart )
7c331059
VZ
3783 pos--;
3784 }
3785
3786 DoEndMoveCol(pos);
cd68daf5 3787 }
852b6c3c
VZ
3788 break;
3789
3790 case WXGRID_CURSOR_SELECT_COL:
3791 case WXGRID_CURSOR_SELECT_CELL:
3792 case WXGRID_CURSOR_RESIZE_ROW:
3793 case WXGRID_CURSOR_SELECT_ROW:
11393d29
VZ
3794 if ( col != -1 )
3795 DoColHeaderClick(col);
852b6c3c 3796 break;
2d66e025 3797 }
f85afd4e 3798
ad805b9e 3799 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, GetColLabelWindow());
ccdee36f 3800 m_dragLastPos = -1;
60ff3b99
VZ
3801 }
3802
2d66e025
MB
3803 // ------------ Right button down
3804 //
3805 else if ( event.RightDown() )
3806 {
a967f048 3807 if ( col >= 0 &&
ca65c044 3808 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK, -1, col, event ) )
2d66e025
MB
3809 {
3810 // no default action at the moment
f85afd4e
MB
3811 }
3812 }
60ff3b99 3813
2d66e025 3814 // ------------ Right double click
f85afd4e 3815 //
2d66e025
MB
3816 else if ( event.RightDClick() )
3817 {
a967f048 3818 if ( col >= 0 &&
ca65c044 3819 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK, -1, col, event ) )
2d66e025
MB
3820 {
3821 // no default action at the moment
3822 }
3823 }
60ff3b99 3824
2d66e025 3825 // ------------ No buttons down and mouse moving
f85afd4e 3826 //
2d66e025 3827 else if ( event.Moving() )
f85afd4e 3828 {
2d66e025
MB
3829 m_dragRowOrCol = XToEdgeOfCol( x );
3830 if ( m_dragRowOrCol >= 0 )
f85afd4e 3831 {
2d66e025 3832 if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
f85afd4e 3833 {
82edfbe7 3834 if ( CanDragColSize(m_dragRowOrCol) )
ad805b9e 3835 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL, GetColLabelWindow(), false);
f85afd4e 3836 }
2d66e025 3837 }
6d004f67 3838 else if ( m_cursorMode != WXGRID_CURSOR_SELECT_CELL )
2d66e025 3839 {
ad805b9e 3840 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, GetColLabelWindow(), false);
8f177c8e 3841 }
f85afd4e
MB
3842 }
3843}
3844
2d66e025 3845void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent& event )
f85afd4e 3846{
2d66e025 3847 if ( event.LeftDown() )
f85afd4e 3848 {
2d66e025
MB
3849 // indicate corner label by having both row and
3850 // col args == -1
f85afd4e 3851 //
b54ba671 3852 if ( !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK, -1, -1, event ) )
2d66e025
MB
3853 {
3854 SelectAll();
3855 }
f85afd4e 3856 }
2d66e025
MB
3857 else if ( event.LeftDClick() )
3858 {
b54ba671 3859 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK, -1, -1, event );
2d66e025 3860 }
2d66e025 3861 else if ( event.RightDown() )
f85afd4e 3862 {
b54ba671 3863 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK, -1, -1, event ) )
f85afd4e 3864 {
2d66e025
MB
3865 // no default action at the moment
3866 }
3867 }
2d66e025
MB
3868 else if ( event.RightDClick() )
3869 {
b54ba671 3870 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK, -1, -1, event ) )
2d66e025
MB
3871 {
3872 // no default action at the moment
3873 }
3874 }
3875}
f85afd4e 3876
86033c4b
VZ
3877void wxGrid::CancelMouseCapture()
3878{
3879 // cancel operation currently in progress, whatever it is
3880 if ( m_winCapture )
3881 {
3882 m_isDragging = false;
8a3e536c
VZ
3883 m_startDragPos = wxDefaultPosition;
3884
86033c4b
VZ
3885 m_cursorMode = WXGRID_CURSOR_SELECT_CELL;
3886 m_winCapture->SetCursor( *wxSTANDARD_CURSOR );
3887 m_winCapture = NULL;
3888
3889 // remove traces of whatever we drew on screen
3890 Refresh();
3891 }
3892}
3893
e2b42eeb
VZ
3894void wxGrid::ChangeCursorMode(CursorMode mode,
3895 wxWindow *win,
3896 bool captureMouse)
3897{
711f12ef 3898#if wxUSE_LOG_TRACE
a243da29 3899 static const wxChar *const cursorModes[] =
e2b42eeb 3900 {
9a83f860
VZ
3901 wxT("SELECT_CELL"),
3902 wxT("RESIZE_ROW"),
3903 wxT("RESIZE_COL"),
3904 wxT("SELECT_ROW"),
3905 wxT("SELECT_COL"),
3906 wxT("MOVE_COL"),
e2b42eeb
VZ
3907 };
3908
9a83f860
VZ
3909 wxLogTrace(wxT("grid"),
3910 wxT("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
3911 win == m_colWindow ? wxT("colLabelWin")
3912 : win ? wxT("rowLabelWin")
3913 : wxT("gridWin"),
e2b42eeb 3914 cursorModes[m_cursorMode], cursorModes[mode]);
711f12ef 3915#endif // wxUSE_LOG_TRACE
e2b42eeb 3916
faec5a43
SN
3917 if ( mode == m_cursorMode &&
3918 win == m_winCapture &&
3919 captureMouse == (m_winCapture != NULL))
e2b42eeb
VZ
3920 return;
3921
3922 if ( !win )
3923 {
3924 // by default use the grid itself
3925 win = m_gridWin;
3926 }
3927
3928 if ( m_winCapture )
3929 {
e882d288 3930 m_winCapture->ReleaseMouse();
10a4531d 3931 m_winCapture = NULL;
e2b42eeb
VZ
3932 }
3933
3934 m_cursorMode = mode;
3935
3936 switch ( m_cursorMode )
3937 {
3938 case WXGRID_CURSOR_RESIZE_ROW:
3939 win->SetCursor( m_rowResizeCursor );
3940 break;
3941
3942 case WXGRID_CURSOR_RESIZE_COL:
3943 win->SetCursor( m_colResizeCursor );
3944 break;
3945
d4175745
VZ
3946 case WXGRID_CURSOR_MOVE_COL:
3947 win->SetCursor( wxCursor(wxCURSOR_HAND) );
3948 break;
3949
e2b42eeb
VZ
3950 default:
3951 win->SetCursor( *wxSTANDARD_CURSOR );
2f024384 3952 break;
e2b42eeb
VZ
3953 }
3954
3955 // we need to capture mouse when resizing
3956 bool resize = m_cursorMode == WXGRID_CURSOR_RESIZE_ROW ||
3957 m_cursorMode == WXGRID_CURSOR_RESIZE_COL;
3958
3959 if ( captureMouse && resize )
3960 {
3961 win->CaptureMouse();
3962 m_winCapture = win;
3963 }
3964}
8f177c8e 3965
8a3e536c
VZ
3966// ----------------------------------------------------------------------------
3967// grid mouse event processing
3968// ----------------------------------------------------------------------------
60ff3b99 3969
0cbcb12d 3970bool
8a3e536c
VZ
3971wxGrid::DoGridCellDrag(wxMouseEvent& event,
3972 const wxGridCellCoords& coords,
3973 bool isFirstDrag)
3974{
0cbcb12d
SC
3975 bool performDefault = true ;
3976
8a3e536c 3977 if ( coords == wxGridNoCellCoords )
0cbcb12d 3978 return performDefault; // we're outside any valid cell
60ff3b99 3979
8a3e536c
VZ
3980 // Hide the edit control, so it won't interfere with drag-shrinking.
3981 if ( IsCellEditControlShown() )
27f35b66 3982 {
8a3e536c
VZ
3983 HideCellEditControl();
3984 SaveEditControlValue();
27f35b66
SN
3985 }
3986
8a3e536c 3987 switch ( event.GetModifiers() )
2d66e025 3988 {
3bc6d534 3989 case wxMOD_CONTROL:
8a3e536c
VZ
3990 if ( m_selectedBlockCorner == wxGridNoCellCoords)
3991 m_selectedBlockCorner = coords;
3992 UpdateBlockBeingSelected(m_selectedBlockCorner, coords);
3993 break;
07296f0b 3994
8a3e536c
VZ
3995 case wxMOD_NONE:
3996 if ( CanDragCell() )
2d66e025 3997 {
8a3e536c 3998 if ( isFirstDrag )
79dbea21 3999 {
8a3e536c
VZ
4000 if ( m_selectedBlockCorner == wxGridNoCellCoords)
4001 m_selectedBlockCorner = coords;
07296f0b 4002
0cbcb12d 4003 // if event is handled by user code, no further processing
c118fa5f 4004 if ( SendEvent(wxEVT_GRID_CELL_BEGIN_DRAG, coords, event) != 0 )
0cbcb12d
SC
4005 performDefault = false;
4006
4007 return performDefault;
07296f0b 4008 }
2d66e025 4009 }
25107357 4010
8a3e536c
VZ
4011 UpdateBlockBeingSelected(m_currentCellCoords, coords);
4012 break;
c71b2126 4013
8a3e536c
VZ
4014 default:
4015 // we don't handle the other key modifiers
4016 event.Skip();
4017 }
0cbcb12d
SC
4018
4019 return performDefault;
8a3e536c 4020}
8f177c8e 4021
8a3e536c
VZ
4022void wxGrid::DoGridLineDrag(wxMouseEvent& event, const wxGridOperations& oper)
4023{
4024 wxClientDC dc(m_gridWin);
4025 PrepareDC(dc);
4026 dc.SetLogicalFunction(wxINVERT);
e2b42eeb 4027
8a3e536c
VZ
4028 const wxRect rectWin(CalcUnscrolledPosition(wxPoint(0, 0)),
4029 m_gridWin->GetClientSize());
4030
4031 // erase the previously drawn line, if any
4032 if ( m_dragLastPos >= 0 )
4033 oper.DrawParallelLineInRect(dc, rectWin, m_dragLastPos);
4034
4035 // we need the vertical position for rows and horizontal for columns here
4036 m_dragLastPos = oper.Dual().Select(CalcUnscrolledPosition(event.GetPosition()));
4037
4038 // don't allow resizing beneath the minimal size
4039 const int posMin = oper.GetLineStartPos(this, m_dragRowOrCol) +
4040 oper.GetMinimalLineSize(this, m_dragRowOrCol);
4041 if ( m_dragLastPos < posMin )
4042 m_dragLastPos = posMin;
4043
4044 // and draw it at the new position
4045 oper.DrawParallelLineInRect(dc, rectWin, m_dragLastPos);
4046}
4047
4048void wxGrid::DoGridDragEvent(wxMouseEvent& event, const wxGridCellCoords& coords)
4049{
4050 if ( !m_isDragging )
4051 {
4052 // Don't start doing anything until the mouse has been dragged far
4053 // enough
4054 const wxPoint& pt = event.GetPosition();
4055 if ( m_startDragPos == wxDefaultPosition )
4056 {
4057 m_startDragPos = pt;
4058 return;
6d004f67 4059 }
e2b42eeb 4060
8a3e536c
VZ
4061 if ( abs(m_startDragPos.x - pt.x) <= DRAG_SENSITIVITY &&
4062 abs(m_startDragPos.y - pt.y) <= DRAG_SENSITIVITY )
4063 return;
2d66e025 4064 }
66242c80 4065
8a3e536c
VZ
4066 const bool isFirstDrag = !m_isDragging;
4067 m_isDragging = true;
07296f0b 4068
8a3e536c 4069 switch ( m_cursorMode )
a5777624 4070 {
8a3e536c 4071 case WXGRID_CURSOR_SELECT_CELL:
0cbcb12d
SC
4072 // no further handling if handled by user
4073 if ( DoGridCellDrag(event, coords, isFirstDrag) == false )
4074 return;
8a3e536c
VZ
4075 break;
4076
4077 case WXGRID_CURSOR_RESIZE_ROW:
4078 DoGridLineDrag(event, wxGridRowOperations());
4079 break;
4080
4081 case WXGRID_CURSOR_RESIZE_COL:
4082 DoGridLineDrag(event, wxGridColumnOperations());
4083 break;
4084
4085 default:
4086 event.Skip();
a5777624 4087 }
e2b42eeb 4088
8a3e536c 4089 if ( isFirstDrag )
f6bcfd97 4090 {
98e95650
VZ
4091 wxASSERT_MSG( !m_winCapture, "shouldn't capture the mouse twice" );
4092
8a3e536c
VZ
4093 m_winCapture = m_gridWin;
4094 m_winCapture->CaptureMouse();
4095 }
4096}
a5777624 4097
8a3e536c
VZ
4098void
4099wxGrid::DoGridCellLeftDown(wxMouseEvent& event,
4100 const wxGridCellCoords& coords,
4101 const wxPoint& pos)
4102{
4103 if ( SendEvent(wxEVT_GRID_CELL_LEFT_CLICK, coords, event) )
4104 {
4105 // event handled by user code, no need to do anything here
4106 return;
a5777624 4107 }
f85afd4e 4108
8a3e536c
VZ
4109 if ( !event.CmdDown() )
4110 ClearSelection();
4111
4112 if ( event.ShiftDown() )
4113 {
4114 if ( m_selection )
4115 {
8b5f6d9d 4116 m_selection->SelectBlock(m_currentCellCoords, coords, event);
8a3e536c
VZ
4117 m_selectedBlockCorner = coords;
4118 }
4119 }
4120 else if ( XToEdgeOfCol(pos.x) < 0 && YToEdgeOfRow(pos.y) < 0 )
a5777624
RD
4121 {
4122 DisableCellEditControl();
8a3e536c 4123 MakeCellVisible( coords );
a5777624 4124
8a3e536c 4125 if ( event.CmdDown() )
58dd5b3b 4126 {
8a3e536c 4127 if ( m_selection )
1ef49ab5 4128 {
8b5f6d9d 4129 m_selection->ToggleCellSelection(coords, event);
1ef49ab5 4130 }
8b5f6d9d 4131
8a3e536c
VZ
4132 m_selectedBlockTopLeft = wxGridNoCellCoords;
4133 m_selectedBlockBottomRight = wxGridNoCellCoords;
4134 m_selectedBlockCorner = coords;
4135 }
4136 else
4137 {
e196db7b
VZ
4138 if ( m_selection )
4139 {
4140 // In row or column selection mode just clicking on the cell
4141 // should select the row or column containing it: this is more
4142 // convenient for the kinds of controls that use such selection
4143 // mode and is compatible with 2.8 behaviour (see #12062).
4144 switch ( m_selection->GetSelectionMode() )
4145 {
4146 case wxGridSelectCells:
4147 case wxGridSelectRowsOrColumns:
4148 // nothing to do in these cases
4149 break;
4150
4151 case wxGridSelectRows:
4152 m_selection->SelectRow(coords.GetRow());
4153 break;
4154
4155 case wxGridSelectColumns:
4156 m_selection->SelectCol(coords.GetCol());
4157 break;
4158 }
4159 }
4160
8a3e536c
VZ
4161 m_waitForSlowClick = m_currentCellCoords == coords &&
4162 coords != wxGridNoCellCoords;
4163 SetCurrentCell( coords );
58dd5b3b 4164 }
a5777624 4165 }
8a3e536c 4166}
f85afd4e 4167
8a3e536c
VZ
4168void
4169wxGrid::DoGridCellLeftDClick(wxMouseEvent& event,
4170 const wxGridCellCoords& coords,
4171 const wxPoint& pos)
4172{
4173 if ( XToEdgeOfCol(pos.x) < 0 && YToEdgeOfRow(pos.y) < 0 )
a5777624 4174 {
8a3e536c 4175 if ( !SendEvent(wxEVT_GRID_CELL_LEFT_DCLICK, coords, event) )
f85afd4e 4176 {
8a3e536c
VZ
4177 // we want double click to select a cell and start editing
4178 // (i.e. to behave in same way as sequence of two slow clicks):
4179 m_waitForSlowClick = true;
4180 }
4181 }
4182}
932b55d0 4183
8a3e536c
VZ
4184void
4185wxGrid::DoGridCellLeftUp(wxMouseEvent& event, const wxGridCellCoords& coords)
4186{
4187 if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
4188 {
4189 if (m_winCapture)
4190 {
e882d288 4191 m_winCapture->ReleaseMouse();
8a3e536c
VZ
4192 m_winCapture = NULL;
4193 }
932b55d0 4194
8a3e536c
VZ
4195 if ( coords == m_currentCellCoords && m_waitForSlowClick && CanEnableCellControl() )
4196 {
4197 ClearSelection();
4198 EnableCellEditControl();
3f3dc2ef 4199
8a3e536c
VZ
4200 wxGridCellAttr *attr = GetCellAttr(coords);
4201 wxGridCellEditor *editor = attr->GetEditor(this, coords.GetRow(), coords.GetCol());
4202 editor->StartingClick();
4203 editor->DecRef();
4204 attr->DecRef();
2d66e025 4205
8a3e536c 4206 m_waitForSlowClick = false;
a5777624 4207 }
8a3e536c
VZ
4208 else if ( m_selectedBlockTopLeft != wxGridNoCellCoords &&
4209 m_selectedBlockBottomRight != wxGridNoCellCoords )
a5777624 4210 {
8a3e536c
VZ
4211 if ( m_selection )
4212 {
4213 m_selection->SelectBlock( m_selectedBlockTopLeft,
4214 m_selectedBlockBottomRight,
8b5f6d9d 4215 event );
8a3e536c 4216 }
e2b42eeb 4217
8a3e536c
VZ
4218 m_selectedBlockTopLeft = wxGridNoCellCoords;
4219 m_selectedBlockBottomRight = wxGridNoCellCoords;
e2b42eeb 4220
8a3e536c
VZ
4221 // Show the edit control, if it has been hidden for
4222 // drag-shrinking.
4223 ShowCellEditControl();
58dd5b3b 4224 }
8a3e536c
VZ
4225 }
4226 else if ( m_cursorMode == WXGRID_CURSOR_RESIZE_ROW )
4227 {
4228 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL);
27bb2c8c 4229 DoEndDragResizeRow(event);
a5777624 4230 }
8a3e536c
VZ
4231 else if ( m_cursorMode == WXGRID_CURSOR_RESIZE_COL )
4232 {
4233 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL);
27bb2c8c 4234 DoEndDragResizeCol(event);
8a3e536c
VZ
4235 }
4236
4237 m_dragLastPos = -1;
4238}
4239
4240void
4241wxGrid::DoGridMouseMoveEvent(wxMouseEvent& WXUNUSED(event),
4242 const wxGridCellCoords& coords,
4243 const wxPoint& pos)
4244{
4245 if ( coords.GetRow() < 0 || coords.GetCol() < 0 )
a5777624 4246 {
8a3e536c
VZ
4247 // out of grid cell area
4248 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL);
4249 return;
a5777624 4250 }
2d66e025 4251
8a3e536c
VZ
4252 int dragRow = YToEdgeOfRow( pos.y );
4253 int dragCol = XToEdgeOfCol( pos.x );
4254
4255 // Dragging on the corner of a cell to resize in both
4256 // directions is not implemented yet...
a5777624 4257 //
8a3e536c 4258 if ( dragRow >= 0 && dragCol >= 0 )
a5777624 4259 {
8a3e536c
VZ
4260 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL);
4261 return;
4262 }
4263
82edfbe7 4264 if ( dragRow >= 0 && CanDragGridSize() && CanDragRowSize(dragRow) )
8a3e536c 4265 {
8a3e536c 4266 if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
f85afd4e 4267 {
82edfbe7
VZ
4268 m_dragRowOrCol = dragRow;
4269 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW, NULL, false);
60ff3b99 4270 }
a5777624 4271 }
ad805b9e
VZ
4272 // When using the native header window we can only resize the columns by
4273 // dragging the dividers in it because we can't make it enter into the
4274 // column resizing mode programmatically
82edfbe7
VZ
4275 else if ( dragCol >= 0 && !m_useNativeHeader &&
4276 CanDragGridSize() && CanDragColSize(dragCol) )
8a3e536c 4277 {
8a3e536c
VZ
4278 if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
4279 {
82edfbe7
VZ
4280 m_dragRowOrCol = dragCol;
4281 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL, NULL, false);
8a3e536c
VZ
4282 }
4283 }
4284 else // Neither on a row or col edge
a5777624 4285 {
8a3e536c 4286 if ( m_cursorMode != WXGRID_CURSOR_SELECT_CELL )
d57ad377 4287 {
d57ad377 4288 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL);
d57ad377 4289 }
8a3e536c
VZ
4290 }
4291}
d57ad377 4292
8a3e536c
VZ
4293void wxGrid::ProcessGridCellMouseEvent(wxMouseEvent& event)
4294{
98e95650
VZ
4295 if ( event.Entering() || event.Leaving() )
4296 {
4297 // we don't care about these events but we must not reset m_isDragging
4298 // if they happen so return before anything else is done
4299 event.Skip();
4300 return;
4301 }
4302
8a3e536c 4303 const wxPoint pos = CalcUnscrolledPosition(event.GetPosition());
790cc417 4304
8a3e536c
VZ
4305 // coordinates of the cell under mouse
4306 wxGridCellCoords coords = XYToCell(pos);
6d004f67 4307
8a3e536c
VZ
4308 int cell_rows, cell_cols;
4309 GetCellSize( coords.GetRow(), coords.GetCol(), &cell_rows, &cell_cols );
4310 if ( (cell_rows < 0) || (cell_cols < 0) )
4311 {
4312 coords.SetRow(coords.GetRow() + cell_rows);
4313 coords.SetCol(coords.GetCol() + cell_cols);
4314 }
6d004f67 4315
8a3e536c
VZ
4316 if ( event.Dragging() )
4317 {
4318 if ( event.LeftIsDown() )
4319 DoGridDragEvent(event, coords);
4320 else
4321 event.Skip();
4322 return;
4323 }
4324
4325 m_isDragging = false;
4326 m_startDragPos = wxDefaultPosition;
4327
8a3e536c
VZ
4328 // deal with various button presses
4329 if ( event.IsButton() )
4330 {
4331 if ( coords != wxGridNoCellCoords )
a5777624 4332 {
8a3e536c 4333 DisableCellEditControl();
e2b42eeb 4334
8a3e536c
VZ
4335 if ( event.LeftDown() )
4336 DoGridCellLeftDown(event, coords, pos);
4337 else if ( event.LeftDClick() )
4338 DoGridCellLeftDClick(event, coords, pos);
4339 else if ( event.RightDown() )
4340 SendEvent(wxEVT_GRID_CELL_RIGHT_CLICK, coords, event);
4341 else if ( event.RightDClick() )
4342 SendEvent(wxEVT_GRID_CELL_RIGHT_DCLICK, coords, event);
6d004f67 4343 }
8a3e536c
VZ
4344
4345 // this one should be called even if we're not over any cell
4346 if ( event.LeftUp() )
a5777624 4347 {
8a3e536c 4348 DoGridCellLeftUp(event, coords);
a5777624
RD
4349 }
4350 }
8a3e536c
VZ
4351 else if ( event.Moving() )
4352 {
4353 DoGridMouseMoveEvent(event, coords, pos);
4354 }
4355 else // unknown mouse event?
4356 {
4357 event.Skip();
4358 }
6d004f67
MB
4359}
4360
27bb2c8c
VZ
4361// this function returns true only if the size really changed
4362bool wxGrid::DoEndDragResizeLine(const wxGridOperations& oper)
6d004f67 4363{
bec70262 4364 if ( m_dragLastPos == -1 )
27bb2c8c 4365 return false;
6d004f67 4366
bec70262 4367 const wxGridOperations& doper = oper.Dual();
6d004f67 4368
bec70262 4369 const wxSize size = m_gridWin->GetClientSize();
e2b42eeb 4370
bec70262 4371 const wxPoint ptOrigin = CalcUnscrolledPosition(wxPoint(0, 0));
ccdee36f 4372
bec70262
VZ
4373 // erase the last line we drew
4374 wxClientDC dc(m_gridWin);
4375 PrepareDC(dc);
4376 dc.SetLogicalFunction(wxINVERT);
6d004f67 4377
bec70262
VZ
4378 const int posLineStart = oper.Select(ptOrigin);
4379 const int posLineEnd = oper.Select(ptOrigin) + oper.Select(size);
6d004f67 4380
bec70262 4381 oper.DrawParallelLine(dc, posLineStart, posLineEnd, m_dragLastPos);
6d004f67 4382
bec70262
VZ
4383 // temporarily hide the edit control before resizing
4384 HideCellEditControl();
4385 SaveEditControlValue();
4386
4387 // do resize the line
4388 const int lineStart = oper.GetLineStartPos(this, m_dragRowOrCol);
27bb2c8c 4389 const int lineSizeOld = oper.GetLineSize(this, m_dragRowOrCol);
bec70262
VZ
4390 oper.SetLineSize(this, m_dragRowOrCol,
4391 wxMax(m_dragLastPos - lineStart,
4392 oper.GetMinimalLineSize(this, m_dragRowOrCol)));
27bb2c8c
VZ
4393 const bool
4394 sizeChanged = oper.GetLineSize(this, m_dragRowOrCol) != lineSizeOld;
bec70262 4395
ad805b9e
VZ
4396 m_dragLastPos = -1;
4397
bec70262
VZ
4398 // refresh now if we're not frozen
4399 if ( !GetBatchCount() )
6d004f67 4400 {
bec70262
VZ
4401 // we need to refresh everything beyond the resized line in the header
4402 // window
6d004f67 4403
bec70262
VZ
4404 // get the position from which to refresh in the other direction
4405 wxRect rect(CellToRect(oper.MakeCoords(m_dragRowOrCol, 0)));
4406 rect.SetPosition(CalcScrolledPosition(rect.GetPosition()));
6d004f67 4407
bec70262
VZ
4408 // we only need the ordinate (for rows) or abscissa (for columns) here,
4409 // and need to cover the entire window in the other direction
4410 oper.Select(rect) = 0;
6d004f67 4411
bec70262
VZ
4412 wxRect rectHeader(rect.GetPosition(),
4413 oper.MakeSize
4414 (
4415 oper.GetHeaderWindowSize(this),
4416 doper.Select(size) - doper.Select(rect)
4417 ));
4418
4419 oper.GetHeaderWindow(this)->Refresh(true, &rectHeader);
4420
4421
4422 // also refresh the grid window: extend the rectangle
4423 if ( m_table )
6d004f67 4424 {
bec70262 4425 oper.SelectSize(rect) = oper.Select(size);
2f024384 4426
bec70262 4427 int subtractLines = 0;
7d1214cd
PC
4428 int line = doper.PosToLine(this, posLineStart);
4429 if ( line >= 0 )
27f35b66 4430 {
bec70262
VZ
4431 // ensure that if we have a multi-cell block we redraw all of
4432 // it by increasing the refresh area to cover it entirely if a
4433 // part of it is affected
13350621 4434 const int lineEnd = doper.PosToLine(this, posLineEnd, true);
7d1214cd 4435 for ( ; line < lineEnd; line++ )
27f35b66 4436 {
bec70262
VZ
4437 int cellLines = oper.Select(
4438 GetCellSize(oper.MakeCoords(m_dragRowOrCol, line)));
4439 if ( cellLines < subtractLines )
4440 subtractLines = cellLines;
27f35b66
SN
4441 }
4442 }
2f024384 4443
bec70262
VZ
4444 int startPos =
4445 oper.GetLineStartPos(this, m_dragRowOrCol + subtractLines);
4446 startPos = doper.CalcScrolledPosition(this, startPos);
4447
4448 doper.Select(rect) = startPos;
4449 doper.SelectSize(rect) = doper.Select(size) - startPos;
e2b42eeb 4450
bec70262
VZ
4451 m_gridWin->Refresh(false, &rect);
4452 }
f85afd4e 4453 }
bec70262
VZ
4454
4455 // show the edit control back again
4456 ShowCellEditControl();
27bb2c8c
VZ
4457
4458 return sizeChanged;
bec70262
VZ
4459}
4460
27bb2c8c 4461void wxGrid::DoEndDragResizeRow(const wxMouseEvent& event)
bec70262 4462{
27bb2c8c
VZ
4463 // TODO: generate RESIZING event, see #10754
4464
4465 if ( DoEndDragResizeLine(wxGridRowOperations()) )
c5c1ea96 4466 SendGridSizeEvent(wxEVT_GRID_ROW_SIZE, m_dragRowOrCol, -1, event);
bec70262
VZ
4467}
4468
27bb2c8c 4469void wxGrid::DoEndDragResizeCol(const wxMouseEvent& event)
bec70262 4470{
27bb2c8c 4471 // TODO: generate RESIZING event, see #10754
ad805b9e 4472
27bb2c8c 4473 if ( DoEndDragResizeLine(wxGridColumnOperations()) )
c5c1ea96 4474 SendGridSizeEvent(wxEVT_GRID_COL_SIZE, -1, m_dragRowOrCol, event);
f85afd4e
MB
4475}
4476
cd68daf5 4477void wxGrid::DoStartMoveCol(int col)
d4175745 4478{
cd68daf5
VZ
4479 m_dragRowOrCol = col;
4480}
d4175745 4481
cd68daf5
VZ
4482void wxGrid::DoEndMoveCol(int pos)
4483{
4484 wxASSERT_MSG( m_dragRowOrCol != -1, "no matching DoStartMoveCol?" );
4485
4486 if ( SendEvent(wxEVT_GRID_COL_MOVE, -1, m_dragRowOrCol) != -1 )
4487 SetColPos(m_dragRowOrCol, pos);
4488 //else: vetoed by user
d4175745 4489
cd68daf5 4490 m_dragRowOrCol = -1;
d4175745
VZ
4491}
4492
4797b014 4493void wxGrid::RefreshAfterColPosChange()
009c7216 4494{
4797b014
VZ
4495 // recalculate the column rights as the column positions have changed,
4496 // unless we calculate them dynamically because all columns widths are the
4497 // same and it's easy to do
4498 if ( !m_colWidths.empty() )
009c7216 4499 {
4797b014
VZ
4500 int colRight = 0;
4501 for ( int colPos = 0; colPos < m_numCols; colPos++ )
4502 {
4503 int colID = GetColAt( colPos );
4504
4505 colRight += m_colWidths[colID];
4506 m_colRights[colID] = colRight;
4507 }
4508 }
009c7216 4509
4797b014
VZ
4510 // and make the changes visible
4511 if ( m_useNativeHeader )
4512 {
4513 if ( m_colAt.empty() )
3039ade9 4514 GetGridColHeader()->ResetColumnsOrder();
4797b014 4515 else
3039ade9 4516 GetGridColHeader()->SetColumnsOrder(m_colAt);
4797b014
VZ
4517 }
4518 else
4519 {
4520 m_colWindow->Refresh();
009c7216 4521 }
4797b014 4522 m_gridWin->Refresh();
009c7216
VZ
4523}
4524
31ec8b4e
VZ
4525void wxGrid::SetColumnsOrder(const wxArrayInt& order)
4526{
4527 m_colAt = order;
4528
4529 RefreshAfterColPosChange();
4530}
4531
3169a8e8 4532void wxGrid::SetColPos(int idx, int pos)
d4175745 4533{
1bb74626 4534 // we're going to need m_colAt now, initialize it if needed
3169a8e8 4535 if ( m_colAt.empty() )
d4175745 4536 {
3169a8e8
VZ
4537 m_colAt.reserve(m_numCols);
4538 for ( int i = 0; i < m_numCols; i++ )
4539 m_colAt.push_back(i);
d4175745
VZ
4540 }
4541
1bb74626 4542 wxHeaderCtrl::MoveColumnInOrderArray(m_colAt, idx, pos);
d4175745 4543
4797b014 4544 RefreshAfterColPosChange();
d4175745
VZ
4545}
4546
009c7216
VZ
4547void wxGrid::ResetColPos()
4548{
4549 m_colAt.clear();
da5a641f 4550
4797b014 4551 RefreshAfterColPosChange();
009c7216 4552}
d4175745
VZ
4553
4554void wxGrid::EnableDragColMove( bool enable )
4555{
4556 if ( m_canDragColMove == enable )
4557 return;
4558
009c7216 4559 if ( m_useNativeHeader )
d4175745 4560 {
009c7216 4561 // update all columns to make them [not] reorderable
3039ade9 4562 GetGridColHeader()->SetColumnCount(m_numCols);
009c7216 4563 }
d4175745 4564
009c7216 4565 m_canDragColMove = enable;
d4175745 4566
009c7216
VZ
4567 // we use to call ResetColPos() from here if !enable but this doesn't seem
4568 // right as it would mean there would be no way to "freeze" the current
4569 // columns order by disabling moving them after putting them in the desired
4570 // order, whereas now you can always call ResetColPos() manually if needed
d4175745
VZ
4571}
4572
4573
2d66e025
MB
4574//
4575// ------ interaction with data model
4576//
4577bool wxGrid::ProcessTableMessage( wxGridTableMessage& msg )
f85afd4e 4578{
2d66e025 4579 switch ( msg.GetId() )
17732cec 4580 {
2d66e025
MB
4581 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES:
4582 return GetModelValues();
17732cec 4583
2d66e025
MB
4584 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES:
4585 return SetModelValues();
f85afd4e 4586
2d66e025
MB
4587 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED:
4588 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED:
4589 case wxGRIDTABLE_NOTIFY_ROWS_DELETED:
4590 case wxGRIDTABLE_NOTIFY_COLS_INSERTED:
4591 case wxGRIDTABLE_NOTIFY_COLS_APPENDED:
4592 case wxGRIDTABLE_NOTIFY_COLS_DELETED:
4593 return Redimension( msg );
4594
4595 default:
ca65c044 4596 return false;
f85afd4e 4597 }
2d66e025 4598}
f85afd4e 4599
2d66e025 4600// The behaviour of this function depends on the grid table class
5b061713 4601// Clear() function. For the default wxGridStringTable class the
10a4531d 4602// behaviour is to replace all cell contents with wxEmptyString but
2d66e025
MB
4603// not to change the number of rows or cols.
4604//
4605void wxGrid::ClearGrid()
4606{
4607 if ( m_table )
f85afd4e 4608 {
4cfa5de6
RD
4609 if (IsCellEditControlEnabled())
4610 DisableCellEditControl();
4611
2d66e025 4612 m_table->Clear();
5b061713 4613 if (!GetBatchCount())
a9339fe2 4614 m_gridWin->Refresh();
f85afd4e
MB
4615 }
4616}
4617
10a4531d
VZ
4618bool
4619wxGrid::DoModifyLines(bool (wxGridTableBase::*funcModify)(size_t, size_t),
4620 int pos, int num, bool WXUNUSED(updateLabels) )
f85afd4e 4621{
10a4531d 4622 wxCHECK_MSG( m_created, false, "must finish creating the grid first" );
f85afd4e 4623
10a4531d 4624 if ( !m_table )
ca65c044 4625 return false;
f85afd4e 4626
10a4531d
VZ
4627 if ( IsCellEditControlEnabled() )
4628 DisableCellEditControl();
b7fff980 4629
10a4531d 4630 return (m_table->*funcModify)(pos, num);
2f024384 4631
10a4531d
VZ
4632 // the table will have sent the results of the insert row
4633 // operation to this view object as a grid table message
f85afd4e
MB
4634}
4635
10a4531d
VZ
4636bool
4637wxGrid::DoAppendLines(bool (wxGridTableBase::*funcAppend)(size_t),
4638 int num, bool WXUNUSED(updateLabels))
f85afd4e 4639{
10a4531d 4640 wxCHECK_MSG( m_created, false, "must finish creating the grid first" );
2f024384 4641
10a4531d 4642 if ( !m_table )
ca65c044 4643 return false;
f85afd4e 4644
10a4531d 4645 return (m_table->*funcAppend)(num);
f85afd4e
MB
4646}
4647
27bb2c8c
VZ
4648// ----------------------------------------------------------------------------
4649// event generation helpers
4650// ----------------------------------------------------------------------------
4651
6f58f3d7 4652bool
c5c1ea96 4653wxGrid::SendGridSizeEvent(wxEventType type,
27bb2c8c
VZ
4654 int row, int col,
4655 const wxMouseEvent& mouseEv)
4656{
4657 int rowOrCol = row == -1 ? col : row;
4658
4659 wxGridSizeEvent gridEvt( GetId(),
4660 type,
4661 this,
4662 rowOrCol,
4663 mouseEv.GetX() + GetRowLabelSize(),
4664 mouseEv.GetY() + GetColLabelSize(),
4665 mouseEv);
4666
6f58f3d7 4667 return GetEventHandler()->ProcessEvent(gridEvt);
27bb2c8c 4668}
8f177c8e 4669
8a3e536c
VZ
4670// Generate a grid event based on a mouse event and return:
4671// -1 if the event was vetoed
4672// +1 if the event was processed (but not vetoed)
4673// 0 if the event wasn't handled
4674int
aced1133 4675wxGrid::SendEvent(const wxEventType type,
8a3e536c 4676 int row, int col,
27bb2c8c 4677 const wxMouseEvent& mouseEv)
2d66e025 4678{
2f024384 4679 bool claimed, vetoed;
fe7b9ed6 4680
27bb2c8c 4681 if ( type == wxEVT_GRID_RANGE_SELECT )
97a9929e
VZ
4682 {
4683 // Right now, it should _never_ end up here!
4684 wxGridRangeSelectEvent gridEvt( GetId(),
4685 type,
4686 this,
8a3e536c
VZ
4687 m_selectedBlockTopLeft,
4688 m_selectedBlockBottomRight,
ca65c044 4689 true,
8b5f6d9d 4690 mouseEv);
97a9929e
VZ
4691
4692 claimed = GetEventHandler()->ProcessEvent(gridEvt);
4693 vetoed = !gridEvt.IsAllowed();
4694 }
2b73a34e
RD
4695 else if ( type == wxEVT_GRID_LABEL_LEFT_CLICK ||
4696 type == wxEVT_GRID_LABEL_LEFT_DCLICK ||
4697 type == wxEVT_GRID_LABEL_RIGHT_CLICK ||
4698 type == wxEVT_GRID_LABEL_RIGHT_DCLICK )
4699 {
4700 wxPoint pos = mouseEv.GetPosition();
4701
4702 if ( mouseEv.GetEventObject() == GetGridRowLabelWindow() )
4703 pos.y += GetColLabelSize();
4704 if ( mouseEv.GetEventObject() == GetGridColLabelWindow() )
4705 pos.x += GetRowLabelSize();
c71b2126 4706
2b73a34e
RD
4707 wxGridEvent gridEvt( GetId(),
4708 type,
4709 this,
4710 row, col,
4711 pos.x,
4712 pos.y,
4713 false,
8b5f6d9d 4714 mouseEv);
2b73a34e
RD
4715 claimed = GetEventHandler()->ProcessEvent(gridEvt);
4716 vetoed = !gridEvt.IsAllowed();
4717 }
fe7b9ed6 4718 else
97a9929e
VZ
4719 {
4720 wxGridEvent gridEvt( GetId(),
4721 type,
4722 this,
4723 row, col,
4724 mouseEv.GetX() + GetRowLabelSize(),
4725 mouseEv.GetY() + GetColLabelSize(),
ca65c044 4726 false,
8b5f6d9d 4727 mouseEv);
0cbcb12d
SC
4728
4729 if ( type == wxEVT_GRID_CELL_BEGIN_DRAG )
4730 {
4731 // by default the dragging is not supported, the user code must
4732 // explicitly allow the event for it to take place
4733 gridEvt.Veto();
4734 }
4735
97a9929e
VZ
4736 claimed = GetEventHandler()->ProcessEvent(gridEvt);
4737 vetoed = !gridEvt.IsAllowed();
4738 }
4739
4740 // A Veto'd event may not be `claimed' so test this first
4db6714b
KH
4741 if (vetoed)
4742 return -1;
4743
97a9929e 4744 return claimed ? 1 : 0;
f85afd4e
MB
4745}
4746
8a3e536c 4747// Generate a grid event of specified type, return value same as above
f85afd4e 4748//
763163a8
VZ
4749int
4750wxGrid::SendEvent(const wxEventType type, int row, int col, const wxString& s)
f85afd4e 4751{
27bb2c8c
VZ
4752 wxGridEvent gridEvt( GetId(), type, this, row, col );
4753 gridEvt.SetString(s);
8f177c8e 4754
27bb2c8c 4755 const bool claimed = GetEventHandler()->ProcessEvent(gridEvt);
fe7b9ed6 4756
97a9929e 4757 // A Veto'd event may not be `claimed' so test this first
27bb2c8c 4758 if ( !gridEvt.IsAllowed() )
4db6714b
KH
4759 return -1;
4760
97a9929e 4761 return claimed ? 1 : 0;
f85afd4e
MB
4762}
4763
2d66e025 4764void wxGrid::OnPaint( wxPaintEvent& WXUNUSED(event) )
f85afd4e 4765{
3d59537f
DS
4766 // needed to prevent zillions of paint events on MSW
4767 wxPaintDC dc(this);
8f177c8e 4768}
f85afd4e 4769
bca7bfc8 4770void wxGrid::Refresh(bool eraseb, const wxRect* rect)
27f35b66 4771{
ad603bf7
VZ
4772 // Don't do anything if between Begin/EndBatch...
4773 // EndBatch() will do all this on the last nested one anyway.
f9549841 4774 if ( m_created && !GetBatchCount() )
27f35b66 4775 {
bca7bfc8 4776 // Refresh to get correct scrolled position:
4db6714b 4777 wxScrolledWindow::Refresh(eraseb, rect);
27f35b66 4778
27f35b66
SN
4779 if (rect)
4780 {
bca7bfc8
SN
4781 int rect_x, rect_y, rectWidth, rectHeight;
4782 int width_label, width_cell, height_label, height_cell;
4783 int x, y;
4784
2f024384 4785 // Copy rectangle can get scroll offsets..
bca7bfc8
SN
4786 rect_x = rect->GetX();
4787 rect_y = rect->GetY();
4788 rectWidth = rect->GetWidth();
4789 rectHeight = rect->GetHeight();
27f35b66 4790
bca7bfc8 4791 width_label = m_rowLabelWidth - rect_x;
4db6714b
KH
4792 if (width_label > rectWidth)
4793 width_label = rectWidth;
27f35b66 4794
bca7bfc8 4795 height_label = m_colLabelHeight - rect_y;
a9339fe2
DS
4796 if (height_label > rectHeight)
4797 height_label = rectHeight;
27f35b66 4798
bca7bfc8
SN
4799 if (rect_x > m_rowLabelWidth)
4800 {
4801 x = rect_x - m_rowLabelWidth;
4802 width_cell = rectWidth;
4803 }
4804 else
4805 {
4806 x = 0;
4807 width_cell = rectWidth - (m_rowLabelWidth - rect_x);
4808 }
4809
4810 if (rect_y > m_colLabelHeight)
4811 {
4812 y = rect_y - m_colLabelHeight;
4813 height_cell = rectHeight;
4814 }
4815 else
4816 {
4817 y = 0;
4818 height_cell = rectHeight - (m_colLabelHeight - rect_y);
4819 }
4820
4821 // Paint corner label part intersecting rect.
4822 if ( width_label > 0 && height_label > 0 )
4823 {
4824 wxRect anotherrect(rect_x, rect_y, width_label, height_label);
4825 m_cornerLabelWin->Refresh(eraseb, &anotherrect);
4826 }
4827
4828 // Paint col labels part intersecting rect.
4829 if ( width_cell > 0 && height_label > 0 )
4830 {
4831 wxRect anotherrect(x, rect_y, width_cell, height_label);
ad805b9e 4832 m_colWindow->Refresh(eraseb, &anotherrect);
bca7bfc8
SN
4833 }
4834
4835 // Paint row labels part intersecting rect.
4836 if ( width_label > 0 && height_cell > 0 )
4837 {
4838 wxRect anotherrect(rect_x, y, width_label, height_cell);
4839 m_rowLabelWin->Refresh(eraseb, &anotherrect);
4840 }
4841
4842 // Paint cell area part intersecting rect.
4843 if ( width_cell > 0 && height_cell > 0 )
4844 {
4845 wxRect anotherrect(x, y, width_cell, height_cell);
4846 m_gridWin->Refresh(eraseb, &anotherrect);
4847 }
4848 }
4849 else
4850 {
4851 m_cornerLabelWin->Refresh(eraseb, NULL);
ad805b9e 4852 m_colWindow->Refresh(eraseb, NULL);
bca7bfc8
SN
4853 m_rowLabelWin->Refresh(eraseb, NULL);
4854 m_gridWin->Refresh(eraseb, NULL);
4855 }
27f35b66
SN
4856 }
4857}
f85afd4e 4858
c71b2126 4859void wxGrid::OnSize(wxSizeEvent& WXUNUSED(event))
f85afd4e 4860{
b93aafab
JS
4861 if (m_targetWindow != this) // check whether initialisation has been done
4862 {
f1ff7df0
VZ
4863 // reposition our children windows
4864 CalcWindowSizes();
b93aafab 4865 }
f85afd4e
MB
4866}
4867
2d66e025 4868void wxGrid::OnKeyDown( wxKeyEvent& event )
f85afd4e 4869{
2d66e025 4870 if ( m_inOnKeyDown )
f85afd4e 4871 {
2d66e025
MB
4872 // shouldn't be here - we are going round in circles...
4873 //
07296f0b 4874 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") );
f85afd4e
MB
4875 }
4876
ca65c044 4877 m_inOnKeyDown = true;
f85afd4e 4878
2d66e025 4879 // propagate the event up and see if it gets processed
2d66e025
MB
4880 wxWindow *parent = GetParent();
4881 wxKeyEvent keyEvt( event );
4882 keyEvt.SetEventObject( parent );
4883
4884 if ( !parent->GetEventHandler()->ProcessEvent( keyEvt ) )
f85afd4e 4885 {
bcb614b3
RR
4886 if (GetLayoutDirection() == wxLayout_RightToLeft)
4887 {
4888 if (event.GetKeyCode() == WXK_RIGHT)
4889 event.m_keyCode = WXK_LEFT;
4890 else if (event.GetKeyCode() == WXK_LEFT)
4891 event.m_keyCode = WXK_RIGHT;
4892 }
c71b2126 4893
2d66e025 4894 // try local handlers
12a3f227 4895 switch ( event.GetKeyCode() )
2d66e025
MB
4896 {
4897 case WXK_UP:
4898 if ( event.ControlDown() )
5c8fc7c1 4899 MoveCursorUpBlock( event.ShiftDown() );
2d66e025 4900 else
5c8fc7c1 4901 MoveCursorUp( event.ShiftDown() );
2d66e025 4902 break;
f85afd4e 4903
2d66e025
MB
4904 case WXK_DOWN:
4905 if ( event.ControlDown() )
5c8fc7c1 4906 MoveCursorDownBlock( event.ShiftDown() );
2d66e025 4907 else
5c8fc7c1 4908 MoveCursorDown( event.ShiftDown() );
2d66e025 4909 break;
8f177c8e 4910
2d66e025
MB
4911 case WXK_LEFT:
4912 if ( event.ControlDown() )
5c8fc7c1 4913 MoveCursorLeftBlock( event.ShiftDown() );
2d66e025 4914 else
5c8fc7c1 4915 MoveCursorLeft( event.ShiftDown() );
2d66e025
MB
4916 break;
4917
4918 case WXK_RIGHT:
4919 if ( event.ControlDown() )
5c8fc7c1 4920 MoveCursorRightBlock( event.ShiftDown() );
2d66e025 4921 else
5c8fc7c1 4922 MoveCursorRight( event.ShiftDown() );
2d66e025 4923 break;
b99be8fb 4924
2d66e025 4925 case WXK_RETURN:
a4f7bf58 4926 case WXK_NUMPAD_ENTER:
58dd5b3b
MB
4927 if ( event.ControlDown() )
4928 {
4929 event.Skip(); // to let the edit control have the return
4930 }
4931 else
4932 {
f6bcfd97
BP
4933 if ( GetGridCursorRow() < GetNumberRows()-1 )
4934 {
4935 MoveCursorDown( event.ShiftDown() );
4936 }
4937 else
4938 {
4939 // at the bottom of a column
13f6e9e8 4940 DisableCellEditControl();
f6bcfd97 4941 }
58dd5b3b 4942 }
2d66e025
MB
4943 break;
4944
5c8fc7c1 4945 case WXK_ESCAPE:
e32352cf 4946 ClearSelection();
5c8fc7c1
SN
4947 break;
4948
2c9a89e0 4949 case WXK_TAB:
f6bcfd97 4950 {
1dc17bca
VZ
4951 // send an event to the grid's parents for custom handling
4952 wxGridEvent gridEvt(GetId(), wxEVT_GRID_TABBING, this,
4953 GetGridCursorRow(), GetGridCursorCol(),
4954 -1, -1, false, event);
4955 if ( ProcessWindowEvent(gridEvt) )
f6bcfd97 4956 {
1dc17bca
VZ
4957 // the event has been handled so no need for more processing
4958 break;
f6bcfd97
BP
4959 }
4960 }
1dc17bca 4961 DoGridProcessTab( event );
2c9a89e0
RD
4962 break;
4963
2d66e025 4964 case WXK_HOME:
ad178a65
VZ
4965 GoToCell(event.ControlDown() ? 0
4966 : m_currentCellCoords.GetRow(),
4967 0);
2d66e025
MB
4968 break;
4969
4970 case WXK_END:
ad178a65
VZ
4971 GoToCell(event.ControlDown() ? m_numRows - 1
4972 : m_currentCellCoords.GetRow(),
4973 m_numCols - 1);
2d66e025
MB
4974 break;
4975
faa94f3e 4976 case WXK_PAGEUP:
2d66e025
MB
4977 MovePageUp();
4978 break;
4979
faa94f3e 4980 case WXK_PAGEDOWN:
2d66e025
MB
4981 MovePageDown();
4982 break;
4983
07296f0b 4984 case WXK_SPACE:
32b4e9ec
VZ
4985 // Ctrl-Space selects the current column, Shift-Space -- the
4986 // current row and Ctrl-Shift-Space -- everything
4987 switch ( m_selection ? event.GetModifiers() : wxMOD_NONE )
aa5e1f75 4988 {
32b4e9ec
VZ
4989 case wxMOD_CONTROL:
4990 m_selection->SelectCol(m_currentCellCoords.GetCol());
4991 break;
ccdee36f 4992
32b4e9ec
VZ
4993 case wxMOD_SHIFT:
4994 m_selection->SelectRow(m_currentCellCoords.GetRow());
4995 break;
4996
4997 case wxMOD_CONTROL | wxMOD_SHIFT:
4998 m_selection->SelectBlock(0, 0,
4999 m_numRows - 1, m_numCols - 1);
5000 break;
5001
5002 case wxMOD_NONE:
5003 if ( !IsEditable() )
5004 {
5005 MoveCursorRight(false);
5006 break;
5007 }
5008 //else: fall through
5009
5010 default:
5011 event.Skip();
5012 }
ccdee36f 5013 break;
07296f0b 5014
2d66e025 5015 default:
63e2147c 5016 event.Skip();
025562fe 5017 break;
2d66e025 5018 }
f85afd4e
MB
5019 }
5020
ca65c044 5021 m_inOnKeyDown = false;
f85afd4e
MB
5022}
5023
f6bcfd97
BP
5024void wxGrid::OnKeyUp( wxKeyEvent& event )
5025{
5026 // try local handlers
5027 //
12a3f227 5028 if ( event.GetKeyCode() == WXK_SHIFT )
f6bcfd97 5029 {
8a3e536c
VZ
5030 if ( m_selectedBlockTopLeft != wxGridNoCellCoords &&
5031 m_selectedBlockBottomRight != wxGridNoCellCoords )
3f3dc2ef
VZ
5032 {
5033 if ( m_selection )
5034 {
a9339fe2 5035 m_selection->SelectBlock(
8a3e536c
VZ
5036 m_selectedBlockTopLeft,
5037 m_selectedBlockBottomRight,
8b5f6d9d 5038 event);
3f3dc2ef
VZ
5039 }
5040 }
5041
8a3e536c
VZ
5042 m_selectedBlockTopLeft = wxGridNoCellCoords;
5043 m_selectedBlockBottomRight = wxGridNoCellCoords;
5044 m_selectedBlockCorner = wxGridNoCellCoords;
f6bcfd97
BP
5045 }
5046}
5047
63e2147c
RD
5048void wxGrid::OnChar( wxKeyEvent& event )
5049{
5050 // is it possible to edit the current cell at all?
5051 if ( !IsCellEditControlEnabled() && CanEnableCellControl() )
5052 {
5053 // yes, now check whether the cells editor accepts the key
5054 int row = m_currentCellCoords.GetRow();
5055 int col = m_currentCellCoords.GetCol();
2f024384 5056 wxGridCellAttr *attr = GetCellAttr(row, col);
63e2147c
RD
5057 wxGridCellEditor *editor = attr->GetEditor(this, row, col);
5058
5059 // <F2> is special and will always start editing, for
5060 // other keys - ask the editor itself
5061 if ( (event.GetKeyCode() == WXK_F2 && !event.HasModifiers())
5062 || editor->IsAcceptedKey(event) )
5063 {
5064 // ensure cell is visble
5065 MakeCellVisible(row, col);
5066 EnableCellEditControl();
5067
5068 // a problem can arise if the cell is not completely
5069 // visible (even after calling MakeCellVisible the
5070 // control is not created and calling StartingKey will
5071 // crash the app
046d682f 5072 if ( event.GetKeyCode() != WXK_F2 && editor->IsCreated() && m_cellEditCtrlEnabled )
63e2147c
RD
5073 editor->StartingKey(event);
5074 }
5075 else
5076 {
5077 event.Skip();
5078 }
5079
5080 editor->DecRef();
5081 attr->DecRef();
5082 }
5083 else
5084 {
5085 event.Skip();
5086 }
5087}
5088
2796cce3 5089void wxGrid::OnEraseBackground(wxEraseEvent&)
508011ce
VZ
5090{
5091}
07296f0b 5092
1dc17bca
VZ
5093void wxGrid::DoGridProcessTab(wxKeyboardState& kbdState)
5094{
5095 const bool isForwardTab = !kbdState.ShiftDown();
5096
5097 // TAB processing only changes when we are at the borders of the grid, so
5098 // let's first handle the common behaviour when we are not at the border.
5099 if ( isForwardTab )
5100 {
5101 if ( GetGridCursorCol() < GetNumberCols() - 1 )
5102 {
5103 MoveCursorRight( false );
5104 return;
5105 }
5106 }
5107 else // going back
5108 {
5109 if ( GetGridCursorCol() )
5110 {
5111 MoveCursorLeft( false );
5112 return;
5113 }
5114 }
5115
5116
5117 // We only get here if the cursor is at the border of the grid, apply the
5118 // configured behaviour.
5119 switch ( m_tabBehaviour )
5120 {
5121 case Tab_Stop:
5122 // Nothing special to do, we remain at the current cell.
5123 break;
5124
5125 case Tab_Wrap:
5126 // Go to the beginning of the next or the end of the previous row.
5127 if ( isForwardTab )
5128 {
5129 if ( GetGridCursorRow() < GetNumberRows() - 1 )
5130 {
5131 GoToCell( GetGridCursorRow() + 1, 0 );
5132 return;
5133 }
5134 }
5135 else
5136 {
5137 if ( GetGridCursorRow() > 0 )
5138 {
5139 GoToCell( GetGridCursorRow() - 1, GetNumberCols() - 1 );
5140 return;
5141 }
5142 }
5143 break;
5144
5145 case Tab_Leave:
5146 if ( Navigate( isForwardTab ? wxNavigationKeyEvent::IsForward
5147 : wxNavigationKeyEvent::IsBackward ) )
5148 return;
5149 break;
5150 }
5151
5152 // If we remain in this cell, stop editing it if we were doing so.
5153 DisableCellEditControl();
5154}
5155
8a3e536c 5156bool wxGrid::SetCurrentCell( const wxGridCellCoords& coords )
66242c80 5157{
8a3e536c 5158 if ( SendEvent(wxEVT_GRID_SELECT_CELL, coords) == -1 )
f6bcfd97 5159 {
8a3e536c
VZ
5160 // the event has been vetoed - do nothing
5161 return false;
f6bcfd97
BP
5162 }
5163
9553702e 5164#if !defined(__WXMAC__)
bee19958
DS
5165 wxClientDC dc( m_gridWin );
5166 PrepareDC( dc );
5d38a5f3 5167#endif
f6bcfd97 5168
2a7750d9 5169 if ( m_currentCellCoords != wxGridNoCellCoords )
2d66e025 5170 {
b54ba671 5171 DisableCellEditControl();
07296f0b 5172
ca65c044 5173 if ( IsVisible( m_currentCellCoords, false ) )
f6bcfd97
BP
5174 {
5175 wxRect r;
bee19958 5176 r = BlockToDeviceRect( m_currentCellCoords, m_currentCellCoords );
f6bcfd97
BP
5177 if ( !m_gridLinesEnabled )
5178 {
5179 r.x--;
5180 r.y--;
5181 r.width++;
5182 r.height++;
5183 }
d1c0b4f9 5184
3ed884a0 5185 wxGridCellCoordsArray cells = CalcCellsExposed( r );
84912ef8 5186
f6bcfd97
BP
5187 // Otherwise refresh redraws the highlight!
5188 m_currentCellCoords = coords;
275c4ae4 5189
9553702e 5190#if defined(__WXMAC__)
5d38a5f3
JS
5191 m_gridWin->Refresh(true /*, & r */);
5192#else
bee19958 5193 DrawGridCellArea( dc, cells );
f6bcfd97 5194 DrawAllGridLines( dc, r );
5d38a5f3 5195#endif
f6bcfd97 5196 }
66242c80 5197 }
8f177c8e 5198
2d66e025
MB
5199 m_currentCellCoords = coords;
5200
bee19958 5201 wxGridCellAttr *attr = GetCellAttr( coords );
76c66f19 5202#if !defined(__WXMAC__)
bee19958 5203 DrawCellHighlight( dc, attr );
5d38a5f3 5204#endif
2a7750d9 5205 attr->DecRef();
8a3e536c
VZ
5206
5207 return true;
66242c80
MB
5208}
5209
8a3e536c
VZ
5210void
5211wxGrid::UpdateBlockBeingSelected(int topRow, int leftCol,
5212 int bottomRow, int rightCol)
c9097836 5213{
4445d035
VZ
5214 MakeCellVisible(m_selectedBlockCorner);
5215 m_selectedBlockCorner = wxGridCellCoords(bottomRow, rightCol);
5216
3f3dc2ef 5217 if ( m_selection )
c9097836 5218 {
8a3e536c 5219 switch ( m_selection->GetSelectionMode() )
3f3dc2ef 5220 {
8a3e536c
VZ
5221 default:
5222 wxFAIL_MSG( "unknown selection mode" );
5223 // fall through
5224
5225 case wxGridSelectCells:
5226 // arbitrary blocks selection allowed so just use the cell
5227 // coordinates as is
5228 break;
5229
5230 case wxGridSelectRows:
5231 // only full rows selection allowd, ensure that we do select
5232 // full rows
5233 leftCol = 0;
5234 rightCol = GetNumberCols() - 1;
5235 break;
5236
5237 case wxGridSelectColumns:
5238 // same as above but for columns
5239 topRow = 0;
5240 bottomRow = GetNumberRows() - 1;
5241 break;
5242
5243 case wxGridSelectRowsOrColumns:
5244 // in this mode we can select only full rows or full columns so
5245 // it doesn't make sense to select blocks at all (and we can't
5246 // extend the block because there is no preferred direction, we
5247 // could only extend it to cover the entire grid but this is
5248 // not useful)
5249 return;
3f3dc2ef 5250 }
c9097836 5251 }
3f3dc2ef 5252
1372f8cc
VZ
5253 EnsureFirstLessThanSecond(topRow, bottomRow);
5254 EnsureFirstLessThanSecond(leftCol, rightCol);
c9097836 5255
8a3e536c
VZ
5256 wxGridCellCoords updateTopLeft = wxGridCellCoords(topRow, leftCol),
5257 updateBottomRight = wxGridCellCoords(bottomRow, rightCol);
c9097836 5258
3ed884a0 5259 // First the case that we selected a completely new area
8a3e536c
VZ
5260 if ( m_selectedBlockTopLeft == wxGridNoCellCoords ||
5261 m_selectedBlockBottomRight == wxGridNoCellCoords )
3ed884a0
SN
5262 {
5263 wxRect rect;
5264 rect = BlockToDeviceRect( wxGridCellCoords ( topRow, leftCol ),
5265 wxGridCellCoords ( bottomRow, rightCol ) );
ca65c044 5266 m_gridWin->Refresh( false, &rect );
3ed884a0 5267 }
2f024384 5268
3ed884a0 5269 // Now handle changing an existing selection area.
8a3e536c
VZ
5270 else if ( m_selectedBlockTopLeft != updateTopLeft ||
5271 m_selectedBlockBottomRight != updateBottomRight )
c9097836
MB
5272 {
5273 // Compute two optimal update rectangles:
5274 // Either one rectangle is a real subset of the
5275 // other, or they are (almost) disjoint!
5276 wxRect rect[4];
5277 bool need_refresh[4];
5278 need_refresh[0] =
5279 need_refresh[1] =
5280 need_refresh[2] =
ca65c044 5281 need_refresh[3] = false;
c9097836
MB
5282 int i;
5283
5284 // Store intermediate values
8a3e536c
VZ
5285 wxCoord oldLeft = m_selectedBlockTopLeft.GetCol();
5286 wxCoord oldTop = m_selectedBlockTopLeft.GetRow();
5287 wxCoord oldRight = m_selectedBlockBottomRight.GetCol();
5288 wxCoord oldBottom = m_selectedBlockBottomRight.GetRow();
c9097836
MB
5289
5290 // Determine the outer/inner coordinates.
1372f8cc
VZ
5291 EnsureFirstLessThanSecond(oldLeft, leftCol);
5292 EnsureFirstLessThanSecond(oldTop, topRow);
5293 EnsureFirstLessThanSecond(rightCol, oldRight);
5294 EnsureFirstLessThanSecond(bottomRow, oldBottom);
c9097836
MB
5295
5296 // Now, either the stuff marked old is the outer
5297 // rectangle or we don't have a situation where one
5298 // is contained in the other.
5299
5300 if ( oldLeft < leftCol )
5301 {
3ed884a0
SN
5302 // Refresh the newly selected or deselected
5303 // area to the left of the old or new selection.
ca65c044 5304 need_refresh[0] = true;
a9339fe2
DS
5305 rect[0] = BlockToDeviceRect(
5306 wxGridCellCoords( oldTop, oldLeft ),
5307 wxGridCellCoords( oldBottom, leftCol - 1 ) );
c9097836
MB
5308 }
5309
2f024384 5310 if ( oldTop < topRow )
c9097836 5311 {
3ed884a0
SN
5312 // Refresh the newly selected or deselected
5313 // area above the old or new selection.
ca65c044 5314 need_refresh[1] = true;
2f024384
DS
5315 rect[1] = BlockToDeviceRect(
5316 wxGridCellCoords( oldTop, leftCol ),
5317 wxGridCellCoords( topRow - 1, rightCol ) );
c9097836
MB
5318 }
5319
5320 if ( oldRight > rightCol )
5321 {
3ed884a0
SN
5322 // Refresh the newly selected or deselected
5323 // area to the right of the old or new selection.
ca65c044 5324 need_refresh[2] = true;
2f024384 5325 rect[2] = BlockToDeviceRect(
a9339fe2
DS
5326 wxGridCellCoords( oldTop, rightCol + 1 ),
5327 wxGridCellCoords( oldBottom, oldRight ) );
c9097836
MB
5328 }
5329
5330 if ( oldBottom > bottomRow )
5331 {
3ed884a0
SN
5332 // Refresh the newly selected or deselected
5333 // area below the old or new selection.
ca65c044 5334 need_refresh[3] = true;
2f024384 5335 rect[3] = BlockToDeviceRect(
a9339fe2
DS
5336 wxGridCellCoords( bottomRow + 1, leftCol ),
5337 wxGridCellCoords( oldBottom, rightCol ) );
c9097836
MB
5338 }
5339
c9097836
MB
5340 // various Refresh() calls
5341 for (i = 0; i < 4; i++ )
5342 if ( need_refresh[i] && rect[i] != wxGridNoCellRect )
ca65c044 5343 m_gridWin->Refresh( false, &(rect[i]) );
c9097836 5344 }
2f024384
DS
5345
5346 // change selection
8a3e536c
VZ
5347 m_selectedBlockTopLeft = updateTopLeft;
5348 m_selectedBlockBottomRight = updateBottomRight;
c9097836
MB
5349}
5350
2d66e025
MB
5351//
5352// ------ functions to get/send data (see also public functions)
5353//
5354
5355bool wxGrid::GetModelValues()
66242c80 5356{
bca7bfc8
SN
5357 // Hide the editor, so it won't hide a changed value.
5358 HideCellEditControl();
c6707d16 5359
2d66e025 5360 if ( m_table )
66242c80 5361 {
2d66e025 5362 // all we need to do is repaint the grid
66242c80 5363 //
2d66e025 5364 m_gridWin->Refresh();
ca65c044 5365 return true;
66242c80 5366 }
8f177c8e 5367
ca65c044 5368 return false;
66242c80
MB
5369}
5370
2d66e025 5371bool wxGrid::SetModelValues()
f85afd4e 5372{
2d66e025 5373 int row, col;
8f177c8e 5374
c6707d16
SN
5375 // Disable the editor, so it won't hide a changed value.
5376 // Do we also want to save the current value of the editor first?
5377 // I think so ...
c6707d16
SN
5378 DisableCellEditControl();
5379
2d66e025
MB
5380 if ( m_table )
5381 {
56b6cf26 5382 for ( row = 0; row < m_numRows; row++ )
f85afd4e 5383 {
56b6cf26 5384 for ( col = 0; col < m_numCols; col++ )
f85afd4e 5385 {
2d66e025 5386 m_table->SetValue( row, col, GetCellValue(row, col) );
f85afd4e
MB
5387 }
5388 }
8f177c8e 5389
ca65c044 5390 return true;
f85afd4e
MB
5391 }
5392
ca65c044 5393 return false;
f85afd4e
MB
5394}
5395
2d66e025
MB
5396// Note - this function only draws cells that are in the list of
5397// exposed cells (usually set from the update region by
5398// CalcExposedCells)
5399//
d10f4bf9 5400void wxGrid::DrawGridCellArea( wxDC& dc, const wxGridCellCoordsArray& cells )
f85afd4e 5401{
4db6714b
KH
5402 if ( !m_numRows || !m_numCols )
5403 return;
60ff3b99 5404
dc1f566f 5405 int i, numCells = cells.GetCount();
27f35b66
SN
5406 int row, col, cell_rows, cell_cols;
5407 wxGridCellCoordsArray redrawCells;
60ff3b99 5408
a9339fe2 5409 for ( i = numCells - 1; i >= 0; i-- )
f85afd4e 5410 {
27f35b66
SN
5411 row = cells[i].GetRow();
5412 col = cells[i].GetCol();
5413 GetCellSize( row, col, &cell_rows, &cell_cols );
5414
5415 // If this cell is part of a multicell block, find owner for repaint
5416 if ( cell_rows <= 0 || cell_cols <= 0 )
5417 {
a9339fe2 5418 wxGridCellCoords cell( row + cell_rows, col + cell_cols );
ca65c044 5419 bool marked = false;
56b6cf26 5420 for ( int j = 0; j < numCells; j++ )
27f35b66
SN
5421 {
5422 if ( cell == cells[j] )
5423 {
ca65c044 5424 marked = true;
3ed884a0 5425 break;
27f35b66
SN
5426 }
5427 }
2f024384 5428
27f35b66
SN
5429 if (!marked)
5430 {
5431 int count = redrawCells.GetCount();
dc1f566f 5432 for (int j = 0; j < count; j++)
27f35b66
SN
5433 {
5434 if ( cell == redrawCells[j] )
5435 {
ca65c044 5436 marked = true;
27f35b66
SN
5437 break;
5438 }
5439 }
2f024384 5440
4db6714b
KH
5441 if (!marked)
5442 redrawCells.Add( cell );
27f35b66 5443 }
2f024384
DS
5444
5445 // don't bother drawing this cell
5446 continue;
27f35b66
SN
5447 }
5448
5449 // If this cell is empty, find cell to left that might want to overflow
5450 if (m_table && m_table->IsEmptyCell(row, col))
5451 {
dc1f566f 5452 for ( int l = 0; l < cell_rows; l++ )
27f35b66 5453 {
56b6cf26 5454 // find a cell in this row to leave already marked for repaint
dc1f566f
SN
5455 int left = col;
5456 for (int k = 0; k < int(redrawCells.GetCount()); k++)
5457 if ((redrawCells[k].GetCol() < left) &&
5458 (redrawCells[k].GetRow() == row))
2f024384 5459 {
4db6714b 5460 left = redrawCells[k].GetCol();
2f024384 5461 }
dc1f566f 5462
4db6714b
KH
5463 if (left == col)
5464 left = 0; // oh well
dc1f566f 5465
2f024384 5466 for (int j = col - 1; j >= left; j--)
27f35b66 5467 {
2f024384 5468 if (!m_table->IsEmptyCell(row + l, j))
27f35b66 5469 {
2f024384 5470 if (GetCellOverflow(row + l, j))
27f35b66 5471 {
2f024384 5472 wxGridCellCoords cell(row + l, j);
ca65c044 5473 bool marked = false;
27f35b66 5474
dc1f566f 5475 for (int k = 0; k < numCells; k++)
27f35b66
SN
5476 {
5477 if ( cell == cells[k] )
5478 {
ca65c044 5479 marked = true;
27f35b66
SN
5480 break;
5481 }
5482 }
4db6714b 5483
27f35b66
SN
5484 if (!marked)
5485 {
5486 int count = redrawCells.GetCount();
dc1f566f 5487 for (int k = 0; k < count; k++)
27f35b66
SN
5488 {
5489 if ( cell == redrawCells[k] )
5490 {
ca65c044 5491 marked = true;
27f35b66
SN
5492 break;
5493 }
5494 }
4db6714b
KH
5495 if (!marked)
5496 redrawCells.Add( cell );
27f35b66
SN
5497 }
5498 }
5499 break;
5500 }
5501 }
5502 }
5503 }
4db6714b 5504
d10f4bf9 5505 DrawCell( dc, cells[i] );
2d66e025 5506 }
27f35b66
SN
5507
5508 numCells = redrawCells.GetCount();
5509
56b6cf26 5510 for ( i = numCells - 1; i >= 0; i-- )
27f35b66
SN
5511 {
5512 DrawCell( dc, redrawCells[i] );
5513 }
2d66e025 5514}
8f177c8e 5515
7c8a8ad5
MB
5516void wxGrid::DrawGridSpace( wxDC& dc )
5517{
f6bcfd97
BP
5518 int cw, ch;
5519 m_gridWin->GetClientSize( &cw, &ch );
7c8a8ad5 5520
f6bcfd97
BP
5521 int right, bottom;
5522 CalcUnscrolledPosition( cw, ch, &right, &bottom );
7c8a8ad5 5523
d4175745 5524 int rightCol = m_numCols > 0 ? GetColRight(GetColAt( m_numCols - 1 )) : 0;
2f024384 5525 int bottomRow = m_numRows > 0 ? GetRowBottom(m_numRows - 1) : 0;
7c8a8ad5 5526
f6bcfd97
BP
5527 if ( right > rightCol || bottom > bottomRow )
5528 {
5529 int left, top;
5530 CalcUnscrolledPosition( 0, 0, &left, &top );
7c8a8ad5 5531
ff72f628 5532 dc.SetBrush(GetDefaultCellBackgroundColour());
f6bcfd97 5533 dc.SetPen( *wxTRANSPARENT_PEN );
7c8a8ad5 5534
f6bcfd97
BP
5535 if ( right > rightCol )
5536 {
a9339fe2 5537 dc.DrawRectangle( rightCol, top, right - rightCol, ch );
f6bcfd97
BP
5538 }
5539
5540 if ( bottom > bottomRow )
5541 {
a9339fe2 5542 dc.DrawRectangle( left, bottomRow, cw, bottom - bottomRow );
f6bcfd97
BP
5543 }
5544 }
7c8a8ad5
MB
5545}
5546
2d66e025
MB
5547void wxGrid::DrawCell( wxDC& dc, const wxGridCellCoords& coords )
5548{
ab79958a
VZ
5549 int row = coords.GetRow();
5550 int col = coords.GetCol();
60ff3b99 5551
7c1cb261 5552 if ( GetColWidth(col) <= 0 || GetRowHeight(row) <= 0 )
ab79958a
VZ
5553 return;
5554
5555 // we draw the cell border ourselves
189d0213
VZ
5556 wxGridCellAttr* attr = GetCellAttr(row, col);
5557
5558 bool isCurrent = coords == m_currentCellCoords;
508011ce 5559
f6bcfd97 5560 wxRect rect = CellToRect( row, col );
f85afd4e 5561
189d0213 5562 // if the editor is shown, we should use it and not the renderer
f6bcfd97
BP
5563 // Note: However, only if it is really _shown_, i.e. not hidden!
5564 if ( isCurrent && IsCellEditControlShown() )
189d0213 5565 {
a9339fe2 5566 // NB: this "#if..." is temporary and fixes a problem where the
962a48f6
DS
5567 // edit control is erased by this code after being rendered.
5568 // On wxMac (QD build only), the cell editor is a wxTextCntl and is rendered
5569 // implicitly, causing this out-of order render.
5d38a5f3 5570#if !defined(__WXMAC__)
0b190b0f 5571 wxGridCellEditor *editor = attr->GetEditor(this, row, col);
ccc04025 5572 editor->PaintBackground(dc, rect, *attr);
0b190b0f 5573 editor->DecRef();
962a48f6 5574#endif
189d0213
VZ
5575 }
5576 else
5577 {
a9339fe2 5578 // but all the rest is drawn by the cell renderer and hence may be customized
0b190b0f
VZ
5579 wxGridCellRenderer *renderer = attr->GetRenderer(this, row, col);
5580 renderer->Draw(*this, *attr, dc, rect, row, col, IsInSelection(coords));
5581 renderer->DecRef();
189d0213 5582 }
07296f0b 5583
283b7808
VZ
5584 attr->DecRef();
5585}
07296f0b 5586
283b7808 5587void wxGrid::DrawCellHighlight( wxDC& dc, const wxGridCellAttr *attr )
07296f0b 5588{
760be3f7
VS
5589 // don't show highlight when the grid doesn't have focus
5590 if ( !HasFocus() )
5591 return;
5592
07296f0b
RD
5593 int row = m_currentCellCoords.GetRow();
5594 int col = m_currentCellCoords.GetCol();
5595
7c1cb261 5596 if ( GetColWidth(col) <= 0 || GetRowHeight(row) <= 0 )
07296f0b
RD
5597 return;
5598
f6bcfd97 5599 wxRect rect = CellToRect(row, col);
07296f0b 5600
b3a7510d
VZ
5601 // hmmm... what could we do here to show that the cell is disabled?
5602 // for now, I just draw a thinner border than for the other ones, but
5603 // it doesn't look really good
b3a7510d 5604
d2520c85
RD
5605 int penWidth = attr->IsReadOnly() ? m_cellHighlightROPenWidth : m_cellHighlightPenWidth;
5606
27f35b66
SN
5607 if (penWidth > 0)
5608 {
56b6cf26
DS
5609 // The center of the drawn line is where the position/width/height of
5610 // the rectangle is actually at (on wxMSW at least), so the
5611 // size of the rectangle is reduced to compensate for the thickness of
5612 // the line. If this is too strange on non-wxMSW platforms then
d2520c85 5613 // please #ifdef this appropriately.
4db6714b
KH
5614 rect.x += penWidth / 2;
5615 rect.y += penWidth / 2;
5616 rect.width -= penWidth - 1;
5617 rect.height -= penWidth - 1;
d2520c85 5618
d2520c85 5619 // Now draw the rectangle
73145b0e
JS
5620 // use the cellHighlightColour if the cell is inside a selection, this
5621 // will ensure the cell is always visible.
ff72f628
VZ
5622 dc.SetPen(wxPen(IsInSelection(row,col) ? m_selectionForeground
5623 : m_cellHighlightColour,
5624 penWidth));
d2520c85
RD
5625 dc.SetBrush(*wxTRANSPARENT_BRUSH);
5626 dc.DrawRectangle(rect);
5627 }
2d66e025 5628}
f85afd4e 5629
3d3f3e37
VZ
5630wxPen wxGrid::GetDefaultGridLinePen()
5631{
ff72f628 5632 return wxPen(GetGridLineColour());
3d3f3e37
VZ
5633}
5634
5635wxPen wxGrid::GetRowGridLinePen(int WXUNUSED(row))
5636{
5637 return GetDefaultGridLinePen();
5638}
5639
5640wxPen wxGrid::GetColGridLinePen(int WXUNUSED(col))
5641{
5642 return GetDefaultGridLinePen();
5643}
5644
2d66e025
MB
5645void wxGrid::DrawCellBorder( wxDC& dc, const wxGridCellCoords& coords )
5646{
2d66e025
MB
5647 int row = coords.GetRow();
5648 int col = coords.GetCol();
7c1cb261
VZ
5649 if ( GetColWidth(col) <= 0 || GetRowHeight(row) <= 0 )
5650 return;
5651
60ff3b99 5652
27f35b66
SN
5653 wxRect rect = CellToRect( row, col );
5654
2d66e025 5655 // right hand border
3d3f3e37 5656 dc.SetPen( GetColGridLinePen(col) );
27f35b66
SN
5657 dc.DrawLine( rect.x + rect.width, rect.y,
5658 rect.x + rect.width, rect.y + rect.height + 1 );
2d66e025
MB
5659
5660 // bottom border
3d3f3e37 5661 dc.SetPen( GetRowGridLinePen(row) );
2f024384 5662 dc.DrawLine( rect.x, rect.y + rect.height,
27f35b66 5663 rect.x + rect.width, rect.y + rect.height);
f85afd4e
MB
5664}
5665
2f024384 5666void wxGrid::DrawHighlight(wxDC& dc, const wxGridCellCoordsArray& cells)
b3a7510d 5667{
2a7750d9
MB
5668 // This if block was previously in wxGrid::OnPaint but that doesn't
5669 // seem to get called under wxGTK - MB
5670 //
2f024384 5671 if ( m_currentCellCoords == wxGridNoCellCoords &&
2a7750d9
MB
5672 m_numRows && m_numCols )
5673 {
5674 m_currentCellCoords.Set(0, 0);
5675 }
5676
f6bcfd97 5677 if ( IsCellEditControlShown() )
99306db2
VZ
5678 {
5679 // don't show highlight when the edit control is shown
5680 return;
5681 }
5682
b3a7510d
VZ
5683 // if the active cell was repainted, repaint its highlight too because it
5684 // might have been damaged by the grid lines
d10f4bf9 5685 size_t count = cells.GetCount();
b3a7510d
VZ
5686 for ( size_t n = 0; n < count; n++ )
5687 {
54181a33
VZ
5688 wxGridCellCoords cell = cells[n];
5689
5690 // If we are using attributes, then we may have just exposed another
5691 // cell in a partially-visible merged cluster of cells. If the "anchor"
5692 // (upper left) cell of this merged cluster is the cell indicated by
5693 // m_currentCellCoords, then we need to refresh the cell highlight even
5694 // though the "anchor" itself is not part of our update segment.
5695 if ( CanHaveAttributes() )
5696 {
5697 int rows = 0,
5698 cols = 0;
5699 GetCellSize(cell.GetRow(), cell.GetCol(), &rows, &cols);
5700
5701 if ( rows < 0 )
5702 cell.SetRow(cell.GetRow() + rows);
5703
5704 if ( cols < 0 )
5705 cell.SetCol(cell.GetCol() + cols);
5706 }
5707
5708 if ( cell == m_currentCellCoords )
b3a7510d
VZ
5709 {
5710 wxGridCellAttr* attr = GetCellAttr(m_currentCellCoords);
5711 DrawCellHighlight(dc, attr);
5712 attr->DecRef();
5713
5714 break;
5715 }
5716 }
5717}
2d66e025 5718
b55d57aa
VZ
5719// Used by wxGrid::Render() to draw the grid lines only for the cells in the
5720// specified range.
5721void
5722wxGrid::DrawRangeGridLines(wxDC& dc,
5723 const wxRegion& reg,
5724 const wxGridCellCoords& topLeft,
5725 const wxGridCellCoords& bottomRight)
5726{
5727 if ( !m_gridLinesEnabled )
5728 return;
5729
5730 int top, left, width, height;
5731 reg.GetBox( left, top, width, height );
5732
5733 // create a clipping region
5734 wxRegion clippedcells( dc.LogicalToDeviceX( left ),
5735 dc.LogicalToDeviceY( top ),
5736 dc.LogicalToDeviceXRel( width ),
5737 dc.LogicalToDeviceYRel( height ) );
5738
5739 // subtract multi cell span area from clipping region for lines
5740 wxRect rect;
5741 for ( int row = topLeft.GetRow(); row <= bottomRight.GetRow(); row++ )
5742 {
5743 for ( int col = topLeft.GetCol(); col <= bottomRight.GetCol(); col++ )
5744 {
5745 int cell_rows, cell_cols;
5746 GetCellSize( row, col, &cell_rows, &cell_cols );
5747 if ( cell_rows > 1 || cell_cols > 1 ) // multi cell
5748 {
5749 rect = CellToRect( row, col );
5750 // cater for scaling
5751 // device origin already set in ::Render() for x, y
5752 rect.x = dc.LogicalToDeviceX( rect.x );
5753 rect.y = dc.LogicalToDeviceY( rect.y );
5754 rect.width = dc.LogicalToDeviceXRel( rect.width );
5755 rect.height = dc.LogicalToDeviceYRel( rect.height ) - 1;
5756 clippedcells.Subtract( rect );
5757 }
5758 else if ( cell_rows < 0 || cell_cols < 0 ) // part of multicell
5759 {
5760 rect = CellToRect( row + cell_rows, col + cell_cols );
5761 rect.x = dc.LogicalToDeviceX( rect.x );
5762 rect.y = dc.LogicalToDeviceY( rect.y );
5763 rect.width = dc.LogicalToDeviceXRel( rect.width );
5764 rect.height = dc.LogicalToDeviceYRel( rect.height ) - 1;
5765 clippedcells.Subtract( rect );
5766 }
5767 }
5768 }
5769
5770 dc.SetDeviceClippingRegion( clippedcells );
5771
5772 DoDrawGridLines(dc,
5773 top, left, top + height, left + width,
5774 topLeft.GetRow(), topLeft.GetCol(),
5775 bottomRight.GetRow(), bottomRight.GetCol());
5776
5777 dc.DestroyClippingRegion();
5778}
5779
2d66e025
MB
5780// This is used to redraw all grid lines e.g. when the grid line colour
5781// has been changed
5782//
33ac7e6f 5783void wxGrid::DrawAllGridLines( wxDC& dc, const wxRegion & WXUNUSED(reg) )
f85afd4e 5784{
9f7aee01 5785 if ( !m_gridLinesEnabled )
4db6714b 5786 return;
f85afd4e 5787
2d66e025 5788 int top, bottom, left, right;
796df70a 5789
ff72f628
VZ
5790 int cw, ch;
5791 m_gridWin->GetClientSize(&cw, &ch);
5792 CalcUnscrolledPosition( 0, 0, &left, &top );
5793 CalcUnscrolledPosition( cw, ch, &right, &bottom );
f85afd4e 5794
9496deb5 5795 // avoid drawing grid lines past the last row and col
9f7aee01
VZ
5796 if ( m_gridLinesClipHorz )
5797 {
5798 if ( !m_numCols )
5799 return;
5800
5801 const int lastColRight = GetColRight(GetColAt(m_numCols - 1));
5802 if ( right > lastColRight )
5803 right = lastColRight;
5804 }
5805
5806 if ( m_gridLinesClipVert )
5807 {
5808 if ( !m_numRows )
5809 return;
5810
5811 const int lastRowBottom = GetRowBottom(m_numRows - 1);
5812 if ( bottom > lastRowBottom )
5813 bottom = lastRowBottom;
5814 }
9496deb5 5815
27f35b66 5816 // no gridlines inside multicells, clip them out
d4175745 5817 int leftCol = GetColPos( internalXToCol(left) );
a9339fe2 5818 int topRow = internalYToRow(top);
d4175745 5819 int rightCol = GetColPos( internalXToCol(right) );
a967f048 5820 int bottomRow = internalYToRow(bottom);
27f35b66 5821
c03bf0c7 5822 wxRegion clippedcells(0, 0, cw, ch);
27f35b66 5823
ff72f628 5824 int cell_rows, cell_cols;
a967f048 5825 wxRect rect;
27f35b66 5826
ff72f628 5827 for ( int j = topRow; j <= bottomRow; j++ )
a967f048 5828 {
ff72f628 5829 for ( int colPos = leftCol; colPos <= rightCol; colPos++ )
27f35b66 5830 {
ff72f628 5831 int i = GetColAt( colPos );
d4175745 5832
a967f048
RG
5833 GetCellSize( j, i, &cell_rows, &cell_cols );
5834 if ((cell_rows > 1) || (cell_cols > 1))
27f35b66 5835 {
a967f048
RG
5836 rect = CellToRect(j,i);
5837 CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
5838 clippedcells.Subtract(rect);
5839 }
5840 else if ((cell_rows < 0) || (cell_cols < 0))
5841 {
a9339fe2 5842 rect = CellToRect(j + cell_rows, i + cell_cols);
a967f048
RG
5843 CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
5844 clippedcells.Subtract(rect);
27f35b66
SN
5845 }
5846 }
5847 }
a9339fe2 5848
c1bc8d9f 5849 dc.SetDeviceClippingRegion( clippedcells );
27f35b66 5850
f31d3faf
VZ
5851 DoDrawGridLines(dc,
5852 top, left, bottom, right,
5853 topRow, leftCol, m_numRows, m_numCols);
f85afd4e 5854
f31d3faf
VZ
5855 dc.DestroyClippingRegion();
5856}
5857
5858void
5859wxGrid::DoDrawGridLines(wxDC& dc,
5860 int top, int left,
5861 int bottom, int right,
5862 int topRow, int leftCol,
5863 int bottomRow, int rightCol)
5864{
2d66e025 5865 // horizontal grid lines
f31d3faf 5866 for ( int i = topRow; i < bottomRow; i++ )
f85afd4e 5867 {
6d55126d 5868 int bot = GetRowBottom(i) - 1;
7c1cb261 5869
6d55126d 5870 if ( bot > bottom )
2d66e025 5871 break;
7c1cb261 5872
6d55126d 5873 if ( bot >= top )
2d66e025 5874 {
3d3f3e37 5875 dc.SetPen( GetRowGridLinePen(i) );
6d55126d 5876 dc.DrawLine( left, bot, right, bot );
2d66e025 5877 }
f85afd4e
MB
5878 }
5879
2d66e025 5880 // vertical grid lines
f31d3faf 5881 for ( int colPos = leftCol; colPos < rightCol; colPos++ )
f85afd4e 5882 {
ff72f628 5883 int i = GetColAt( colPos );
d4175745 5884
2121eb69
RR
5885 int colRight = GetColRight(i);
5886#ifdef __WXGTK__
5887 if (GetLayoutDirection() != wxLayout_RightToLeft)
5888#endif
5889 colRight--;
5890
7c1cb261 5891 if ( colRight > right )
2d66e025 5892 break;
7c1cb261
VZ
5893
5894 if ( colRight >= left )
2d66e025 5895 {
3d3f3e37 5896 dc.SetPen( GetColGridLinePen(i) );
7c1cb261 5897 dc.DrawLine( colRight, top, colRight, bottom );
2d66e025
MB
5898 }
5899 }
5900}
f85afd4e 5901
c2f5b920 5902void wxGrid::DrawRowLabels( wxDC& dc, const wxArrayInt& rows)
2d66e025 5903{
4db6714b
KH
5904 if ( !m_numRows )
5905 return;
60ff3b99 5906
ff72f628
VZ
5907 const size_t numLabels = rows.GetCount();
5908 for ( size_t i = 0; i < numLabels; i++ )
2d66e025 5909 {
d10f4bf9 5910 DrawRowLabel( dc, rows[i] );
60ff3b99 5911 }
f85afd4e
MB
5912}
5913
2d66e025 5914void wxGrid::DrawRowLabel( wxDC& dc, int row )
f85afd4e 5915{
659af826 5916 if ( GetRowHeight(row) <= 0 || m_rowLabelWidth <= 0 )
7c1cb261 5917 return;
60ff3b99 5918
670d0177
VZ
5919 wxGridCellAttrProvider * const
5920 attrProvider = m_table ? m_table->GetAttrProvider() : NULL;
7a0a6cc8
VZ
5921
5922 // notice that an explicit static_cast is needed to avoid a compilation
5923 // error with VC7.1 which, for some reason, tries to instantiate (abstract)
5924 // wxGridRowHeaderRenderer class without it
ba9574c3 5925 const wxGridRowHeaderRenderer&
670d0177 5926 rend = attrProvider ? attrProvider->GetRowHeaderRenderer(row)
7a0a6cc8
VZ
5927 : static_cast<const wxGridRowHeaderRenderer&>
5928 (gs_defaultHeaderRenderers.rowRenderer);
5929
ba9574c3
VZ
5930 wxRect rect(0, GetRowTop(row), m_rowLabelWidth, GetRowHeight(row));
5931 rend.DrawBorder(*this, dc, rect);
8f177c8e 5932
f85afd4e 5933 int hAlign, vAlign;
ba9574c3 5934 GetRowLabelAlignment(&hAlign, &vAlign);
60ff3b99 5935
ba9574c3
VZ
5936 rend.DrawLabel(*this, dc, GetRowLabelValue(row),
5937 rect, hAlign, vAlign, wxHORIZONTAL);
f85afd4e
MB
5938}
5939
ad805b9e
VZ
5940void wxGrid::UseNativeColHeader(bool native)
5941{
5942 if ( native == m_useNativeHeader )
5943 return;
5944
5945 delete m_colWindow;
5946 m_useNativeHeader = native;
5947
5948 CreateColumnWindow();
5949
5950 if ( m_useNativeHeader )
3039ade9 5951 GetGridColHeader()->SetColumnCount(m_numCols);
ad805b9e
VZ
5952 CalcWindowSizes();
5953}
5954
71cf399f
RR
5955void wxGrid::SetUseNativeColLabels( bool native )
5956{
ad805b9e
VZ
5957 wxASSERT_MSG( !m_useNativeHeader,
5958 "doesn't make sense when using native header" );
5959
71cf399f
RR
5960 m_nativeColumnLabels = native;
5961 if (native)
5962 {
5963 int height = wxRendererNative::Get().GetHeaderButtonHeight( this );
5964 SetColLabelSize( height );
5965 }
76c66f19 5966
ad805b9e 5967 GetColLabelWindow()->Refresh();
ff72f628 5968 m_cornerLabelWin->Refresh();
71cf399f
RR
5969}
5970
d10f4bf9 5971void wxGrid::DrawColLabels( wxDC& dc,const wxArrayInt& cols )
f85afd4e 5972{
4db6714b
KH
5973 if ( !m_numCols )
5974 return;
60ff3b99 5975
ff72f628
VZ
5976 const size_t numLabels = cols.GetCount();
5977 for ( size_t i = 0; i < numLabels; i++ )
f85afd4e 5978 {
d10f4bf9 5979 DrawColLabel( dc, cols[i] );
60ff3b99 5980 }
f85afd4e
MB
5981}
5982
ff72f628
VZ
5983void wxGrid::DrawCornerLabel(wxDC& dc)
5984{
ba9574c3
VZ
5985 wxRect rect(wxSize(m_rowLabelWidth, m_colLabelHeight));
5986
ff72f628
VZ
5987 if ( m_nativeColumnLabels )
5988 {
ff72f628
VZ
5989 rect.Deflate(1);
5990
5991 wxRendererNative::Get().DrawHeaderButton(m_cornerLabelWin, dc, rect, 0);
5992 }
5993 else
5994 {
ba9574c3
VZ
5995 rect.width++;
5996 rect.height++;
5997
670d0177
VZ
5998 wxGridCellAttrProvider * const
5999 attrProvider = m_table ? m_table->GetAttrProvider() : NULL;
ba9574c3 6000 const wxGridCornerHeaderRenderer&
670d0177 6001 rend = attrProvider ? attrProvider->GetCornerRenderer()
7a0a6cc8
VZ
6002 : static_cast<wxGridCornerHeaderRenderer&>
6003 (gs_defaultHeaderRenderers.cornerRenderer);
ff72f628 6004
ba9574c3 6005 rend.DrawBorder(*this, dc, rect);
ff72f628
VZ
6006 }
6007}
6008
6009void wxGrid::DrawColLabel(wxDC& dc, int col)
f85afd4e 6010{
659af826 6011 if ( GetColWidth(col) <= 0 || m_colLabelHeight <= 0 )
7c1cb261 6012 return;
60ff3b99 6013
4d1bc39c 6014 int colLeft = GetColLeft(col);
ec157c8f 6015
ff72f628 6016 wxRect rect(colLeft, 0, GetColWidth(col), m_colLabelHeight);
670d0177
VZ
6017 wxGridCellAttrProvider * const
6018 attrProvider = m_table ? m_table->GetAttrProvider() : NULL;
ba9574c3 6019 const wxGridColumnHeaderRenderer&
670d0177 6020 rend = attrProvider ? attrProvider->GetColumnHeaderRenderer(col)
7a0a6cc8
VZ
6021 : static_cast<wxGridColumnHeaderRenderer&>
6022 (gs_defaultHeaderRenderers.colRenderer);
2f024384 6023
ff72f628 6024 if ( m_nativeColumnLabels )
71cf399f 6025 {
11393d29
VZ
6026 wxRendererNative::Get().DrawHeaderButton
6027 (
6028 GetColLabelWindow(),
6029 dc,
6030 rect,
6031 0,
6032 IsSortingBy(col)
6033 ? IsSortOrderAscending()
6034 ? wxHDR_SORT_ICON_UP
6035 : wxHDR_SORT_ICON_DOWN
6036 : wxHDR_SORT_ICON_NONE
6037 );
ba9574c3 6038 rect.Deflate(2);
71cf399f
RR
6039 }
6040 else
6041 {
b5e97a17
VZ
6042 // It is reported that we need to erase the background to avoid display
6043 // artefacts, see #12055.
6044 wxDCBrushChanger setBrush(dc, m_colWindow->GetBackgroundColour());
6045 dc.DrawRectangle(rect);
6046
ba9574c3 6047 rend.DrawBorder(*this, dc, rect);
71cf399f 6048 }
2f024384 6049
ff72f628 6050 int hAlign, vAlign;
ba9574c3 6051 GetColLabelAlignment(&hAlign, &vAlign);
ff72f628 6052 const int orient = GetColLabelTextOrientation();
60ff3b99 6053
ba9574c3 6054 rend.DrawLabel(*this, dc, GetColLabelValue(col), rect, hAlign, vAlign, orient);
f85afd4e
MB
6055}
6056
ff72f628
VZ
6057// TODO: these 2 functions should be replaced with wxDC::DrawLabel() to which
6058// we just have to add textOrientation support
2d66e025
MB
6059void wxGrid::DrawTextRectangle( wxDC& dc,
6060 const wxString& value,
6061 const wxRect& rect,
6062 int horizAlign,
d43851f7 6063 int vertAlign,
ba9574c3 6064 int textOrientation ) const
f85afd4e 6065{
2d66e025 6066 wxArrayString lines;
ef5df12b 6067
2d66e025 6068 StringToLines( value, lines );
ef5df12b 6069
ff72f628 6070 DrawTextRectangle(dc, lines, rect, horizAlign, vertAlign, textOrientation);
d10f4bf9
VZ
6071}
6072
4330c974 6073void wxGrid::DrawTextRectangle(wxDC& dc,
038a5591
JS
6074 const wxArrayString& lines,
6075 const wxRect& rect,
6076 int horizAlign,
6077 int vertAlign,
ba9574c3 6078 int textOrientation) const
d10f4bf9 6079{
4330c974
VZ
6080 if ( lines.empty() )
6081 return;
ef5df12b 6082
4330c974 6083 wxDCClipper clip(dc, rect);
ef5df12b 6084
4330c974
VZ
6085 long textWidth,
6086 textHeight;
ef5df12b 6087
4330c974
VZ
6088 if ( textOrientation == wxHORIZONTAL )
6089 GetTextBoxSize( dc, lines, &textWidth, &textHeight );
6090 else
6091 GetTextBoxSize( dc, lines, &textHeight, &textWidth );
ef5df12b 6092
4330c974
VZ
6093 int x = 0,
6094 y = 0;
6095 switch ( vertAlign )
6096 {
73145b0e 6097 case wxALIGN_BOTTOM:
4db6714b 6098 if ( textOrientation == wxHORIZONTAL )
038a5591 6099 y = rect.y + (rect.height - textHeight - 1);
d43851f7 6100 else
038a5591
JS
6101 x = rect.x + rect.width - textWidth;
6102 break;
ef5df12b 6103
73145b0e 6104 case wxALIGN_CENTRE:
4db6714b 6105 if ( textOrientation == wxHORIZONTAL )
a9339fe2 6106 y = rect.y + ((rect.height - textHeight) / 2);
d43851f7 6107 else
a9339fe2 6108 x = rect.x + ((rect.width - textWidth) / 2);
038a5591 6109 break;
ef5df12b 6110
73145b0e
JS
6111 case wxALIGN_TOP:
6112 default:
4db6714b 6113 if ( textOrientation == wxHORIZONTAL )
038a5591 6114 y = rect.y + 1;
d43851f7 6115 else
038a5591 6116 x = rect.x + 1;
73145b0e 6117 break;
4330c974 6118 }
ef5df12b 6119
4330c974
VZ
6120 // Align each line of a multi-line label
6121 size_t nLines = lines.GetCount();
6122 for ( size_t l = 0; l < nLines; l++ )
6123 {
6124 const wxString& line = lines[l];
ef5df12b 6125
9b7d3c09
VZ
6126 if ( line.empty() )
6127 {
6128 *(textOrientation == wxHORIZONTAL ? &y : &x) += dc.GetCharHeight();
6129 continue;
6130 }
6131
ad2633bd 6132 wxCoord lineWidth = 0,
c2b2c10e 6133 lineHeight = 0;
4330c974
VZ
6134 dc.GetTextExtent(line, &lineWidth, &lineHeight);
6135
6136 switch ( horizAlign )
6137 {
038a5591 6138 case wxALIGN_RIGHT:
4db6714b 6139 if ( textOrientation == wxHORIZONTAL )
038a5591
JS
6140 x = rect.x + (rect.width - lineWidth - 1);
6141 else
6142 y = rect.y + lineWidth + 1;
6143 break;
ef5df12b 6144
038a5591 6145 case wxALIGN_CENTRE:
4db6714b 6146 if ( textOrientation == wxHORIZONTAL )
2f024384 6147 x = rect.x + ((rect.width - lineWidth) / 2);
038a5591 6148 else
2f024384 6149 y = rect.y + rect.height - ((rect.height - lineWidth) / 2);
038a5591 6150 break;
ef5df12b 6151
038a5591
JS
6152 case wxALIGN_LEFT:
6153 default:
4db6714b 6154 if ( textOrientation == wxHORIZONTAL )
038a5591
JS
6155 x = rect.x + 1;
6156 else
6157 y = rect.y + rect.height - 1;
6158 break;
4330c974 6159 }
ef5df12b 6160
4330c974
VZ
6161 if ( textOrientation == wxHORIZONTAL )
6162 {
6163 dc.DrawText( line, x, y );
6164 y += lineHeight;
6165 }
6166 else
6167 {
6168 dc.DrawRotatedText( line, x, y, 90.0 );
6169 x += lineHeight;
038a5591 6170 }
f85afd4e 6171 }
f85afd4e
MB
6172}
6173
2f024384
DS
6174// Split multi-line text up into an array of strings.
6175// Any existing contents of the string array are preserved.
f85afd4e 6176//
696f3e9d 6177// TODO: refactor wxTextFile::Read() and reuse the same code from here
ef316e23 6178void wxGrid::StringToLines( const wxString& value, wxArrayString& lines ) const
f85afd4e 6179{
f85afd4e
MB
6180 int startPos = 0;
6181 int pos;
6d004f67 6182 wxString eol = wxTextFile::GetEOL( wxTextFileType_Unix );
2433bb2e 6183 wxString tVal = wxTextFile::Translate( value, wxTextFileType_Unix );
e2b42eeb 6184
faa94f3e 6185 while ( startPos < (int)tVal.length() )
f85afd4e 6186 {
2433bb2e 6187 pos = tVal.Mid(startPos).Find( eol );
f85afd4e
MB
6188 if ( pos < 0 )
6189 {
6190 break;
6191 }
6192 else if ( pos == 0 )
6193 {
6194 lines.Add( wxEmptyString );
6195 }
6196 else
6197 {
696f3e9d 6198 lines.Add( tVal.Mid(startPos, pos) );
f85afd4e 6199 }
a9339fe2 6200
4db6714b 6201 startPos += pos + 1;
f85afd4e 6202 }
4db6714b 6203
2eafd712 6204 if ( startPos < (int)tVal.length() )
f85afd4e 6205 {
696f3e9d 6206 lines.Add( tVal.Mid( startPos ) );
f85afd4e
MB
6207 }
6208}
6209
fbfb8bcc 6210void wxGrid::GetTextBoxSize( const wxDC& dc,
d10f4bf9 6211 const wxArrayString& lines,
ef316e23 6212 long *width, long *height ) const
f85afd4e 6213{
ad2633bd
RR
6214 wxCoord w = 0;
6215 wxCoord h = 0;
6216 wxCoord lineW = 0, lineH = 0;
f85afd4e 6217
b540eb2b 6218 size_t i;
56b6cf26 6219 for ( i = 0; i < lines.GetCount(); i++ )
f85afd4e
MB
6220 {
6221 dc.GetTextExtent( lines[i], &lineW, &lineH );
6222 w = wxMax( w, lineW );
6223 h += lineH;
6224 }
6225
6226 *width = w;
6227 *height = h;
6228}
6229
f6bcfd97
BP
6230//
6231// ------ Batch processing.
6232//
6233void wxGrid::EndBatch()
6234{
6235 if ( m_batchCount > 0 )
6236 {
6237 m_batchCount--;
6238 if ( !m_batchCount )
6239 {
6240 CalcDimensions();
6241 m_rowLabelWin->Refresh();
ad805b9e 6242 m_colWindow->Refresh();
f6bcfd97
BP
6243 m_cornerLabelWin->Refresh();
6244 m_gridWin->Refresh();
6245 }
6246 }
6247}
f85afd4e 6248
d8232393
MB
6249// Use this, rather than wxWindow::Refresh(), to force an immediate
6250// repainting of the grid. Has no effect if you are already inside a
6251// BeginBatch / EndBatch block.
6252//
6253void wxGrid::ForceRefresh()
6254{
6255 BeginBatch();
6256 EndBatch();
6257}
6258
bf646121
VZ
6259bool wxGrid::Enable(bool enable)
6260{
6261 if ( !wxScrolledWindow::Enable(enable) )
6262 return false;
6263
6264 // redraw in the new state
6265 m_gridWin->Refresh();
6266
6267 return true;
6268}
d8232393 6269
f85afd4e 6270//
2d66e025 6271// ------ Edit control functions
f85afd4e
MB
6272//
6273
2d66e025 6274void wxGrid::EnableEditing( bool edit )
f85afd4e 6275{
2d66e025 6276 if ( edit != m_editable )
f85afd4e 6277 {
4db6714b
KH
6278 if (!edit)
6279 EnableCellEditControl(edit);
2d66e025 6280 m_editable = edit;
f85afd4e 6281 }
f85afd4e
MB
6282}
6283
2d66e025 6284void wxGrid::EnableCellEditControl( bool enable )
f85afd4e 6285{
2c9a89e0
RD
6286 if (! m_editable)
6287 return;
6288
2c9a89e0
RD
6289 if ( enable != m_cellEditCtrlEnabled )
6290 {
dcfe4c3d 6291 if ( enable )
f85afd4e 6292 {
8a3e536c 6293 if ( SendEvent(wxEVT_GRID_EDITOR_SHOWN) == -1 )
97a9929e 6294 return;
fe7b9ed6 6295
97a9929e 6296 // this should be checked by the caller!
9a83f860 6297 wxASSERT_MSG( CanEnableCellControl(), wxT("can't enable editing for this cell!") );
b54ba671
VZ
6298
6299 // do it before ShowCellEditControl()
dcfe4c3d 6300 m_cellEditCtrlEnabled = enable;
b54ba671 6301
2d66e025 6302 ShowCellEditControl();
2d66e025
MB
6303 }
6304 else
6305 {
8a3e536c 6306 SendEvent(wxEVT_GRID_EDITOR_HIDDEN);
fe7b9ed6 6307
97a9929e 6308 HideCellEditControl();
2d66e025 6309 SaveEditControlValue();
b54ba671
VZ
6310
6311 // do it after HideCellEditControl()
dcfe4c3d 6312 m_cellEditCtrlEnabled = enable;
f85afd4e 6313 }
f85afd4e 6314 }
f85afd4e 6315}
f85afd4e 6316
b54ba671 6317bool wxGrid::IsCurrentCellReadOnly() const
283b7808 6318{
f48a1159
VZ
6319 wxGridCellAttr*
6320 attr = const_cast<wxGrid *>(this)->GetCellAttr(m_currentCellCoords);
b54ba671
VZ
6321 bool readonly = attr->IsReadOnly();
6322 attr->DecRef();
283b7808 6323
b54ba671
VZ
6324 return readonly;
6325}
283b7808 6326
b54ba671
VZ
6327bool wxGrid::CanEnableCellControl() const
6328{
20c84410
SN
6329 return m_editable && (m_currentCellCoords != wxGridNoCellCoords) &&
6330 !IsCurrentCellReadOnly();
b54ba671
VZ
6331}
6332
6333bool wxGrid::IsCellEditControlEnabled() const
6334{
6335 // the cell edit control might be disable for all cells or just for the
6336 // current one if it's read only
ca65c044 6337 return m_cellEditCtrlEnabled ? !IsCurrentCellReadOnly() : false;
283b7808
VZ
6338}
6339
f6bcfd97
BP
6340bool wxGrid::IsCellEditControlShown() const
6341{
ca65c044 6342 bool isShown = false;
f6bcfd97
BP
6343
6344 if ( m_cellEditCtrlEnabled )
6345 {
6346 int row = m_currentCellCoords.GetRow();
6347 int col = m_currentCellCoords.GetCol();
6348 wxGridCellAttr* attr = GetCellAttr(row, col);
6349 wxGridCellEditor* editor = attr->GetEditor((wxGrid*) this, row, col);
6350 attr->DecRef();
6351
6352 if ( editor )
6353 {
6354 if ( editor->IsCreated() )
6355 {
6356 isShown = editor->GetControl()->IsShown();
6357 }
6358
6359 editor->DecRef();
6360 }
6361 }
6362
6363 return isShown;
6364}
6365
2d66e025 6366void wxGrid::ShowCellEditControl()
f85afd4e 6367{
2d66e025 6368 if ( IsCellEditControlEnabled() )
f85afd4e 6369 {
7db713ae 6370 if ( !IsVisible( m_currentCellCoords, false ) )
2d66e025 6371 {
ca65c044 6372 m_cellEditCtrlEnabled = false;
2d66e025
MB
6373 return;
6374 }
6375 else
6376 {
2c9a89e0
RD
6377 wxRect rect = CellToRect( m_currentCellCoords );
6378 int row = m_currentCellCoords.GetRow();
6379 int col = m_currentCellCoords.GetCol();
2d66e025 6380
27f35b66
SN
6381 // if this is part of a multicell, find owner (topleft)
6382 int cell_rows, cell_cols;
6383 GetCellSize( row, col, &cell_rows, &cell_cols );
6384 if ( cell_rows <= 0 || cell_cols <= 0 )
6385 {
6386 row += cell_rows;
6387 col += cell_cols;
6388 m_currentCellCoords.SetRow( row );
6389 m_currentCellCoords.SetCol( col );
6390 }
6391
99306db2
VZ
6392 // erase the highlight and the cell contents because the editor
6393 // might not cover the entire cell
6394 wxClientDC dc( m_gridWin );
6395 PrepareDC( dc );
2c03d771 6396 wxGridCellAttr* attr = GetCellAttr(row, col);
ff72f628 6397 dc.SetBrush(wxBrush(attr->GetBackgroundColour()));
99306db2
VZ
6398 dc.SetPen(*wxTRANSPARENT_PEN);
6399 dc.DrawRectangle(rect);
d2fdd8d2 6400
6f706ee0
VZ
6401 // convert to scrolled coords
6402 CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
6403
6404 int nXMove = 0;
6405 if (rect.x < 0)
6406 nXMove = rect.x;
6407
99306db2 6408 // cell is shifted by one pixel
68c5a31c 6409 // However, don't allow x or y to become negative
70e8d961 6410 // since the SetSize() method interprets that as
68c5a31c
SN
6411 // "don't change."
6412 if (rect.x > 0)
6413 rect.x--;
6414 if (rect.y > 0)
6415 rect.y--;
f0102d2a 6416
28a77bc4 6417 wxGridCellEditor* editor = attr->GetEditor(this, row, col);
3da93aae
VZ
6418 if ( !editor->IsCreated() )
6419 {
ca65c044 6420 editor->Create(m_gridWin, wxID_ANY,
2c9a89e0 6421 new wxGridCellEditorEvtHandler(this, editor));
bf7945ce
RD
6422
6423 wxGridEditorCreatedEvent evt(GetId(),
6424 wxEVT_GRID_EDITOR_CREATED,
6425 this,
6426 row,
6427 col,
6428 editor->GetControl());
6429 GetEventHandler()->ProcessEvent(evt);
2d66e025
MB
6430 }
6431
27f35b66 6432 // resize editor to overflow into righthand cells if allowed
39b80349 6433 int maxWidth = rect.width;
27f35b66
SN
6434 wxString value = GetCellValue(row, col);
6435 if ( (value != wxEmptyString) && (attr->GetOverflow()) )
6436 {
39b80349 6437 int y;
2f024384
DS
6438 GetTextExtent(value, &maxWidth, &y, NULL, NULL, &attr->GetFont());
6439 if (maxWidth < rect.width)
6440 maxWidth = rect.width;
39b80349 6441 }
4db6714b 6442
39b80349 6443 int client_right = m_gridWin->GetClientSize().GetWidth();
2f024384 6444 if (rect.x + maxWidth > client_right)
2b5f62a0 6445 maxWidth = client_right - rect.x;
39b80349 6446
2b5f62a0
VZ
6447 if ((maxWidth > rect.width) && (col < m_numCols) && m_table)
6448 {
39b80349 6449 GetCellSize( row, col, &cell_rows, &cell_cols );
2b5f62a0 6450 // may have changed earlier
2f024384 6451 for (int i = col + cell_cols; i < m_numCols; i++)
2b5f62a0 6452 {
39b80349
SN
6453 int c_rows, c_cols;
6454 GetCellSize( row, i, &c_rows, &c_cols );
2f024384 6455
2b5f62a0 6456 // looks weird going over a multicell
bee19958 6457 if (m_table->IsEmptyCell( row, i ) &&
2b5f62a0 6458 (rect.width < maxWidth) && (c_rows == 1))
2f024384 6459 {
bee19958 6460 rect.width += GetColWidth( i );
2f024384 6461 }
2b5f62a0
VZ
6462 else
6463 break;
6464 }
4db6714b 6465
39b80349 6466 if (rect.GetRight() > client_right)
bee19958 6467 rect.SetRight( client_right - 1 );
27f35b66 6468 }
76c66f19 6469
bee19958 6470 editor->SetCellAttr( attr );
2c9a89e0 6471 editor->SetSize( rect );
e1a66d9a
RD
6472 if (nXMove != 0)
6473 editor->GetControl()->Move(
6474 editor->GetControl()->GetPosition().x + nXMove,
6475 editor->GetControl()->GetPosition().y );
ca65c044 6476 editor->Show( true, attr );
04418332
VZ
6477
6478 // recalc dimensions in case we need to
6479 // expand the scrolled window to account for editor
73145b0e 6480 CalcDimensions();
99306db2 6481
3da93aae 6482 editor->BeginEdit(row, col, this);
1bd71df9 6483 editor->SetCellAttr(NULL);
0b190b0f
VZ
6484
6485 editor->DecRef();
2c9a89e0 6486 attr->DecRef();
2b5f62a0 6487 }
f85afd4e
MB
6488 }
6489}
6490
2d66e025 6491void wxGrid::HideCellEditControl()
f85afd4e 6492{
2d66e025 6493 if ( IsCellEditControlEnabled() )
f85afd4e 6494 {
2c9a89e0
RD
6495 int row = m_currentCellCoords.GetRow();
6496 int col = m_currentCellCoords.GetCol();
6497
bee19958 6498 wxGridCellAttr *attr = GetCellAttr(row, col);
0b190b0f 6499 wxGridCellEditor *editor = attr->GetEditor(this, row, col);
7d277ac0 6500 const bool editorHadFocus = editor->GetControl()->HasFocus();
ca65c044 6501 editor->Show( false );
0b190b0f 6502 editor->DecRef();
2c9a89e0 6503 attr->DecRef();
2fb3e528 6504
7d277ac0
VZ
6505 // return the focus to the grid itself if the editor had it
6506 //
6507 // note that we must not do this unconditionally to avoid stealing
6508 // focus from the window which just received it if we are hiding the
6509 // editor precisely because we lost focus
6510 if ( editorHadFocus )
6511 m_gridWin->SetFocus();
2fb3e528 6512
27f35b66
SN
6513 // refresh whole row to the right
6514 wxRect rect( CellToRect(row, col) );
6515 CalcScrolledPosition(rect.x, rect.y, &rect.x, &rect.y );
6516 rect.width = m_gridWin->GetClientSize().GetWidth() - rect.x;
2f024384 6517
7d75e6c6
RD
6518#ifdef __WXMAC__
6519 // ensure that the pixels under the focus ring get refreshed as well
4db6714b 6520 rect.Inflate(10, 10);
7d75e6c6 6521#endif
2f024384 6522
ca65c044 6523 m_gridWin->Refresh( false, &rect );
f85afd4e 6524 }
2d66e025 6525}
8f177c8e 6526
2d66e025
MB
6527void wxGrid::SaveEditControlValue()
6528{
3da93aae
VZ
6529 if ( IsCellEditControlEnabled() )
6530 {
2c9a89e0
RD
6531 int row = m_currentCellCoords.GetRow();
6532 int col = m_currentCellCoords.GetCol();
8f177c8e 6533
4db6714b 6534 wxString oldval = GetCellValue(row, col);
fe7b9ed6 6535
3da93aae 6536 wxGridCellAttr* attr = GetCellAttr(row, col);
28a77bc4 6537 wxGridCellEditor* editor = attr->GetEditor(this, row, col);
2d66e025 6538
763163a8 6539 wxString newval;
78e78812 6540 bool changed = editor->EndEdit(row, col, this, oldval, &newval);
2d66e025 6541
763163a8 6542 if ( changed && SendEvent(wxEVT_GRID_CELL_CHANGING, newval) != -1 )
3da93aae 6543 {
763163a8
VZ
6544 editor->ApplyEdit(row, col, this);
6545
6546 // for compatibility reasons dating back to wx 2.8 when this event
6547 // was called wxEVT_GRID_CELL_CHANGE and wxEVT_GRID_CELL_CHANGING
6548 // didn't exist we allow vetoing this one too
6549 if ( SendEvent(wxEVT_GRID_CELL_CHANGED, oldval) == -1 )
4db6714b 6550 {
97a9929e 6551 // Event has been vetoed, set the data back.
4db6714b 6552 SetCellValue(row, col, oldval);
97a9929e 6553 }
2d66e025 6554 }
763163a8
VZ
6555
6556 editor->DecRef();
6557 attr->DecRef();
f85afd4e
MB
6558 }
6559}
6560
add6e919
VZ
6561void wxGrid::OnHideEditor(wxCommandEvent& WXUNUSED(event))
6562{
6563 DisableCellEditControl();
6564}
6565
2d66e025 6566//
60ff3b99
VZ
6567// ------ Grid location functions
6568// Note that all of these functions work with the logical coordinates of
2d66e025
MB
6569// grid cells and labels so you will need to convert from device
6570// coordinates for mouse events etc.
6571//
6572
8a3e536c 6573wxGridCellCoords wxGrid::XYToCell(int x, int y) const
f85afd4e 6574{
58dd5b3b
MB
6575 int row = YToRow(y);
6576 int col = XToCol(x);
6577
8a3e536c
VZ
6578 return row == -1 || col == -1 ? wxGridNoCellCoords
6579 : wxGridCellCoords(row, col);
2d66e025 6580}
f85afd4e 6581
bec70262
VZ
6582// compute row or column from some (unscrolled) coordinate value, using either
6583// m_defaultRowHeight/m_defaultColWidth or binary search on array of
5bce3d57
VZ
6584// m_rowBottoms/m_colRights to do it quickly in O(log n) time.
6585// NOTE: This may not work correctly for reordered columns.
cd4f6f5f
VZ
6586int wxGrid::PosToLinePos(int coord,
6587 bool clipToMinMax,
6588 const wxGridOperations& oper) const
2d66e025 6589{
bec70262 6590 const int numLines = oper.GetNumberOfLines(this);
a967f048 6591
bec70262 6592 if ( coord < 0 )
cd4f6f5f 6593 return clipToMinMax && numLines > 0 ? 0 : wxNOT_FOUND;
a967f048 6594
bec70262
VZ
6595 const int defaultLineSize = oper.GetDefaultLineSize(this);
6596 wxCHECK_MSG( defaultLineSize, -1, "can't have 0 default line size" );
d57ad377 6597
bec70262
VZ
6598 int maxPos = coord / defaultLineSize,
6599 minPos = 0;
8f177c8e 6600
bec70262
VZ
6601 // check for the simplest case: if we have no explicit line sizes
6602 // configured, then we already know the line this position falls in
6603 const wxArrayInt& lineEnds = oper.GetLineEnds(this);
6604 if ( lineEnds.empty() )
f85afd4e 6605 {
bec70262
VZ
6606 if ( maxPos < numLines )
6607 return maxPos;
4db6714b 6608
bec70262 6609 return clipToMinMax ? numLines - 1 : -1;
f85afd4e 6610 }
4db6714b 6611
2d66e025 6612
5bce3d57
VZ
6613 // binary search is quite efficient and we can't really make any assumptions
6614 // on where to start here since row and columns could be of size 0 if they are
6615 // hidden. While this could be made more efficient, some profiling will be
6616 // necessary to determine if it really is a performance bottleneck
6617 maxPos = numLines - 1;
d4175745 6618
bec70262
VZ
6619 // check if the position is beyond the last column
6620 const int lineAtMaxPos = oper.GetLineAt(this, maxPos);
6621 if ( coord >= lineEnds[lineAtMaxPos] )
cd4f6f5f 6622 return clipToMinMax ? maxPos : -1;
d4175745 6623
bec70262
VZ
6624 // or before the first one
6625 const int lineAt0 = oper.GetLineAt(this, 0);
6626 if ( coord < lineEnds[lineAt0] )
cd4f6f5f 6627 return 0;
d4175745 6628
bec70262
VZ
6629
6630 // finally do perform the binary search
6631 while ( minPos < maxPos )
d4175745 6632 {
bec70262
VZ
6633 wxCHECK_MSG( lineEnds[oper.GetLineAt(this, minPos)] <= coord &&
6634 coord < lineEnds[oper.GetLineAt(this, maxPos)],
6635 -1,
cd4f6f5f 6636 "wxGrid: internal error in PosToLinePos()" );
d4175745 6637
bec70262 6638 if ( coord >= lineEnds[oper.GetLineAt(this, maxPos - 1)] )
cd4f6f5f 6639 return maxPos;
d4175745
VZ
6640 else
6641 maxPos--;
bec70262
VZ
6642
6643 const int median = minPos + (maxPos - minPos + 1) / 2;
6644 if ( coord < lineEnds[oper.GetLineAt(this, median)] )
d4175745
VZ
6645 maxPos = median;
6646 else
6647 minPos = median;
6648 }
bec70262 6649
cd4f6f5f
VZ
6650 return maxPos;
6651}
6652
6653int
6654wxGrid::PosToLine(int coord,
6655 bool clipToMinMax,
6656 const wxGridOperations& oper) const
6657{
6658 int pos = PosToLinePos(coord, clipToMinMax, oper);
6659
6660 return pos == wxNOT_FOUND ? wxNOT_FOUND : oper.GetLineAt(this, pos);
bec70262
VZ
6661}
6662
6663int wxGrid::YToRow(int y, bool clipToMinMax) const
6664{
6665 return PosToLine(y, clipToMinMax, wxGridRowOperations());
6666}
6667
6668int wxGrid::XToCol(int x, bool clipToMinMax) const
6669{
6670 return PosToLine(x, clipToMinMax, wxGridColumnOperations());
2d66e025 6671}
8f177c8e 6672
cd4f6f5f
VZ
6673int wxGrid::XToPos(int x) const
6674{
6675 return PosToLinePos(x, true /* clip */, wxGridColumnOperations());
6676}
6677
71b619d7 6678// return the row/col number such that the pos is near the edge of, or -1 if
10a4531d
VZ
6679// not near an edge.
6680//
82edfbe7
VZ
6681// notice that position can only possibly be near an edge if the row/column is
6682// large enough to still allow for an "inner" area that is _not_ near the edge
6683// (i.e., if the height/width is smaller than WXGRID_LABEL_EDGE_ZONE, pos will
6684// _never_ be considered to be near the edge).
10a4531d 6685int wxGrid::PosToEdgeOfLine(int pos, const wxGridOperations& oper) const
2d66e025 6686{
71b619d7
VZ
6687 // Get the bottom or rightmost line that could match.
6688 int line = oper.PosToLine(this, pos, true);
10a4531d 6689
6df84c5c
VZ
6690 if ( oper.GetLineSize(this, line) > WXGRID_LABEL_EDGE_ZONE )
6691 {
6692 // We know that we are in this line, test whether we are close enough
6693 // to start or end border, respectively.
6694 if ( abs(oper.GetLineEndPos(this, line) - pos) < WXGRID_LABEL_EDGE_ZONE )
6695 return line;
6696 else if ( line > 0 &&
6697 pos - oper.GetLineStartPos(this,
6698 line) < WXGRID_LABEL_EDGE_ZONE )
6699 {
6700 // We need to find the previous visible line, so skip all the
6701 // hidden (of size 0) ones.
6702 do
6703 {
6704 line = oper.GetLineBefore(this, line);
6705 }
6706 while ( line >= 0 && oper.GetLineSize(this, line) == 0 );
2d66e025 6707
6df84c5c
VZ
6708 // It can possibly be -1 here.
6709 return line;
6710 }
6711 }
71b619d7 6712
2d66e025
MB
6713 return -1;
6714}
6715
10a4531d 6716int wxGrid::YToEdgeOfRow(int y) const
2d66e025 6717{
10a4531d
VZ
6718 return PosToEdgeOfLine(y, wxGridRowOperations());
6719}
2d66e025 6720
10a4531d
VZ
6721int wxGrid::XToEdgeOfCol(int x) const
6722{
6723 return PosToEdgeOfLine(x, wxGridColumnOperations());
f85afd4e
MB
6724}
6725
ef316e23 6726wxRect wxGrid::CellToRect( int row, int col ) const
f85afd4e 6727{
2d66e025 6728 wxRect rect( -1, -1, -1, -1 );
f85afd4e 6729
2f024384
DS
6730 if ( row >= 0 && row < m_numRows &&
6731 col >= 0 && col < m_numCols )
f85afd4e 6732 {
27f35b66
SN
6733 int i, cell_rows, cell_cols;
6734 rect.width = rect.height = 0;
6735 GetCellSize( row, col, &cell_rows, &cell_cols );
6736 // if negative then find multicell owner
2f024384
DS
6737 if (cell_rows < 0)
6738 row += cell_rows;
6739 if (cell_cols < 0)
6740 col += cell_cols;
27f35b66
SN
6741 GetCellSize( row, col, &cell_rows, &cell_cols );
6742
7c1cb261
VZ
6743 rect.x = GetColLeft(col);
6744 rect.y = GetRowTop(row);
2f024384
DS
6745 for (i=col; i < col + cell_cols; i++)
6746 rect.width += GetColWidth(i);
6747 for (i=row; i < row + cell_rows; i++)
27f35b66 6748 rect.height += GetRowHeight(i);
f85afd4e 6749
efc49a21
VZ
6750 // if grid lines are enabled, then the area of the cell is a bit smaller
6751 if (m_gridLinesEnabled)
6752 {
6753 rect.width -= 1;
6754 rect.height -= 1;
6755 }
f6bcfd97 6756 }
4db6714b 6757
2d66e025
MB
6758 return rect;
6759}
6760
ef316e23 6761bool wxGrid::IsVisible( int row, int col, bool wholeCellVisible ) const
2d66e025
MB
6762{
6763 // get the cell rectangle in logical coords
6764 //
6765 wxRect r( CellToRect( row, col ) );
60ff3b99 6766
2d66e025
MB
6767 // convert to device coords
6768 //
6769 int left, top, right, bottom;
6770 CalcScrolledPosition( r.GetLeft(), r.GetTop(), &left, &top );
6771 CalcScrolledPosition( r.GetRight(), r.GetBottom(), &right, &bottom );
60ff3b99 6772
2d66e025 6773 // check against the client area of the grid window
2d66e025
MB
6774 int cw, ch;
6775 m_gridWin->GetClientSize( &cw, &ch );
60ff3b99 6776
2d66e025 6777 if ( wholeCellVisible )
f85afd4e 6778 {
2d66e025 6779 // is the cell wholly visible ?
2f024384
DS
6780 return ( left >= 0 && right <= cw &&
6781 top >= 0 && bottom <= ch );
2d66e025
MB
6782 }
6783 else
6784 {
6785 // is the cell partly visible ?
6786 //
2f024384
DS
6787 return ( ((left >= 0 && left < cw) || (right > 0 && right <= cw)) &&
6788 ((top >= 0 && top < ch) || (bottom > 0 && bottom <= ch)) );
2d66e025
MB
6789 }
6790}
6791
2d66e025
MB
6792// make the specified cell location visible by doing a minimal amount
6793// of scrolling
6794//
6795void wxGrid::MakeCellVisible( int row, int col )
6796{
6797 int i;
60ff3b99 6798 int xpos = -1, ypos = -1;
2d66e025 6799
2f024384
DS
6800 if ( row >= 0 && row < m_numRows &&
6801 col >= 0 && col < m_numCols )
2d66e025
MB
6802 {
6803 // get the cell rectangle in logical coords
2d66e025 6804 wxRect r( CellToRect( row, col ) );
60ff3b99 6805
2d66e025 6806 // convert to device coords
2d66e025
MB
6807 int left, top, right, bottom;
6808 CalcScrolledPosition( r.GetLeft(), r.GetTop(), &left, &top );
6809 CalcScrolledPosition( r.GetRight(), r.GetBottom(), &right, &bottom );
60ff3b99 6810
2d66e025
MB
6811 int cw, ch;
6812 m_gridWin->GetClientSize( &cw, &ch );
60ff3b99 6813
2d66e025 6814 if ( top < 0 )
3f296516 6815 {
2d66e025 6816 ypos = r.GetTop();
3f296516 6817 }
2d66e025
MB
6818 else if ( bottom > ch )
6819 {
6820 int h = r.GetHeight();
6821 ypos = r.GetTop();
56b6cf26 6822 for ( i = row - 1; i >= 0; i-- )
2d66e025 6823 {
7c1cb261
VZ
6824 int rowHeight = GetRowHeight(i);
6825 if ( h + rowHeight > ch )
6826 break;
2d66e025 6827
7c1cb261
VZ
6828 h += rowHeight;
6829 ypos -= rowHeight;
2d66e025 6830 }
f0102d2a
VZ
6831
6832 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
56b6cf26
DS
6833 // have rounding errors (this is important, because if we do,
6834 // we might not scroll at all and some cells won't be redrawn)
275c4ae4 6835 //
56b6cf26
DS
6836 // Sometimes GRID_SCROLL_LINE / 2 is not enough,
6837 // so just add a full scroll unit...
fd76c6a7 6838 ypos += m_yScrollPixelsPerLine;
2d66e025
MB
6839 }
6840
7db713ae 6841 // special handling for wide cells - show always left part of the cell!
faa94f3e 6842 // Otherwise, e.g. when stepping from row to row, it would jump between
7db713ae
JS
6843 // left and right part of the cell on every step!
6844// if ( left < 0 )
ccdee36f 6845 if ( left < 0 || (right - left) >= cw )
2d66e025
MB
6846 {
6847 xpos = r.GetLeft();
6848 }
6849 else if ( right > cw )
6850 {
73145b0e
JS
6851 // position the view so that the cell is on the right
6852 int x0, y0;
6853 CalcUnscrolledPosition(0, 0, &x0, &y0);
6854 xpos = x0 + (right - cw);
f0102d2a
VZ
6855
6856 // see comment for ypos above
fd76c6a7 6857 xpos += m_xScrollPixelsPerLine;
2d66e025
MB
6858 }
6859
ccdee36f 6860 if ( xpos != -1 || ypos != -1 )
2d66e025 6861 {
97a9929e 6862 if ( xpos != -1 )
fd76c6a7 6863 xpos /= m_xScrollPixelsPerLine;
97a9929e 6864 if ( ypos != -1 )
fd76c6a7 6865 ypos /= m_yScrollPixelsPerLine;
2d66e025
MB
6866 Scroll( xpos, ypos );
6867 AdjustScrollbars();
6868 }
6869 }
6870}
6871
2d66e025
MB
6872//
6873// ------ Grid cursor movement functions
6874//
6875
10a4531d
VZ
6876bool
6877wxGrid::DoMoveCursor(bool expandSelection,
6878 const wxGridDirectionOperations& diroper)
2d66e025 6879{
10a4531d
VZ
6880 if ( m_currentCellCoords == wxGridNoCellCoords )
6881 return false;
6882
6883 if ( expandSelection )
2d66e025 6884 {
8a3e536c
VZ
6885 wxGridCellCoords coords = m_selectedBlockCorner;
6886 if ( coords == wxGridNoCellCoords )
6887 coords = m_currentCellCoords;
4db6714b 6888
8a3e536c 6889 if ( diroper.IsAtBoundary(coords) )
10a4531d 6890 return false;
2d66e025 6891
8a3e536c 6892 diroper.Advance(coords);
2d66e025 6893
8a3e536c 6894 UpdateBlockBeingSelected(m_currentCellCoords, coords);
10a4531d 6895 }
8a3e536c 6896 else // don't expand selection
f85afd4e 6897 {
8a3e536c
VZ
6898 ClearSelection();
6899
10a4531d 6900 if ( diroper.IsAtBoundary(m_currentCellCoords) )
ca65c044 6901 return false;
4db6714b 6902
10a4531d
VZ
6903 wxGridCellCoords coords = m_currentCellCoords;
6904 diroper.Advance(coords);
8a3e536c
VZ
6905
6906 GoToCell(coords);
f85afd4e 6907 }
2d66e025 6908
10a4531d 6909 return true;
f85afd4e
MB
6910}
6911
10a4531d 6912bool wxGrid::MoveCursorUp(bool expandSelection)
f85afd4e 6913{
10a4531d
VZ
6914 return DoMoveCursor(expandSelection,
6915 wxGridBackwardOperations(this, wxGridRowOperations()));
2d66e025
MB
6916}
6917
10a4531d 6918bool wxGrid::MoveCursorDown(bool expandSelection)
2d66e025 6919{
10a4531d
VZ
6920 return DoMoveCursor(expandSelection,
6921 wxGridForwardOperations(this, wxGridRowOperations()));
6922}
4db6714b 6923
10a4531d
VZ
6924bool wxGrid::MoveCursorLeft(bool expandSelection)
6925{
6926 return DoMoveCursor(expandSelection,
6927 wxGridBackwardOperations(this, wxGridColumnOperations()));
6928}
8f177c8e 6929
10a4531d
VZ
6930bool wxGrid::MoveCursorRight(bool expandSelection)
6931{
6932 return DoMoveCursor(expandSelection,
6933 wxGridForwardOperations(this, wxGridColumnOperations()));
2d66e025
MB
6934}
6935
10a4531d 6936bool wxGrid::DoMoveCursorByPage(const wxGridDirectionOperations& diroper)
2d66e025 6937{
4db6714b
KH
6938 if ( m_currentCellCoords == wxGridNoCellCoords )
6939 return false;
60ff3b99 6940
10a4531d
VZ
6941 if ( diroper.IsAtBoundary(m_currentCellCoords) )
6942 return false;
ef5df12b 6943
10a4531d
VZ
6944 const int oldRow = m_currentCellCoords.GetRow();
6945 int newRow = diroper.MoveByPixelDistance(oldRow, m_gridWin->GetClientSize().y);
6946 if ( newRow == oldRow )
6947 {
6948 wxGridCellCoords coords(m_currentCellCoords);
6949 diroper.Advance(coords);
6950 newRow = coords.GetRow();
6951 }
8f177c8e 6952
8a3e536c 6953 GoToCell(newRow, m_currentCellCoords.GetCol());
60ff3b99 6954
10a4531d
VZ
6955 return true;
6956}
2d66e025 6957
10a4531d
VZ
6958bool wxGrid::MovePageUp()
6959{
6960 return DoMoveCursorByPage(
6961 wxGridBackwardOperations(this, wxGridRowOperations()));
2d66e025
MB
6962}
6963
6964bool wxGrid::MovePageDown()
6965{
10a4531d 6966 return DoMoveCursorByPage(
d188378e 6967 wxGridForwardOperations(this, wxGridRowOperations()));
10a4531d 6968}
60ff3b99 6969
10a4531d
VZ
6970// helper of DoMoveCursorByBlock(): advance the cell coordinates using diroper
6971// until we find a non-empty cell or reach the grid end
6972void
6973wxGrid::AdvanceToNextNonEmpty(wxGridCellCoords& coords,
6974 const wxGridDirectionOperations& diroper)
6975{
6976 while ( !diroper.IsAtBoundary(coords) )
f85afd4e 6977 {
10a4531d
VZ
6978 diroper.Advance(coords);
6979 if ( !m_table->IsEmpty(coords) )
6980 break;
f85afd4e
MB
6981 }
6982}
8f177c8e 6983
10a4531d
VZ
6984bool
6985wxGrid::DoMoveCursorByBlock(bool expandSelection,
6986 const wxGridDirectionOperations& diroper)
2d66e025 6987{
10a4531d
VZ
6988 if ( !m_table || m_currentCellCoords == wxGridNoCellCoords )
6989 return false;
f85afd4e 6990
10a4531d
VZ
6991 if ( diroper.IsAtBoundary(m_currentCellCoords) )
6992 return false;
4db6714b 6993
10a4531d
VZ
6994 wxGridCellCoords coords(m_currentCellCoords);
6995 if ( m_table->IsEmpty(coords) )
6996 {
6997 // we are in an empty cell: find the next block of non-empty cells
6998 AdvanceToNextNonEmpty(coords, diroper);
2d66e025 6999 }
10a4531d 7000 else // current cell is not empty
f85afd4e 7001 {
10a4531d
VZ
7002 diroper.Advance(coords);
7003 if ( m_table->IsEmpty(coords) )
2d66e025 7004 {
10a4531d
VZ
7005 // we started at the end of a block, find the next one
7006 AdvanceToNextNonEmpty(coords, diroper);
2d66e025 7007 }
10a4531d 7008 else // we're in a middle of a block
2d66e025 7009 {
10a4531d
VZ
7010 // go to the end of it, i.e. find the last cell before the next
7011 // empty one
7012 while ( !diroper.IsAtBoundary(coords) )
2d66e025 7013 {
10a4531d
VZ
7014 wxGridCellCoords coordsNext(coords);
7015 diroper.Advance(coordsNext);
7016 if ( m_table->IsEmpty(coordsNext) )
2d66e025 7017 break;
2d66e025 7018
10a4531d
VZ
7019 coords = coordsNext;
7020 }
aa5e1f75 7021 }
10a4531d 7022 }
2d66e025 7023
10a4531d
VZ
7024 if ( expandSelection )
7025 {
8a3e536c 7026 UpdateBlockBeingSelected(m_currentCellCoords, coords);
10a4531d
VZ
7027 }
7028 else
7029 {
7030 ClearSelection();
8a3e536c 7031 GoToCell(coords);
f85afd4e 7032 }
f85afd4e 7033
10a4531d 7034 return true;
2d66e025 7035}
f85afd4e 7036
10a4531d 7037bool wxGrid::MoveCursorUpBlock(bool expandSelection)
f85afd4e 7038{
10a4531d
VZ
7039 return DoMoveCursorByBlock(
7040 expandSelection,
7041 wxGridBackwardOperations(this, wxGridRowOperations())
7042 );
7043}
8f177c8e 7044
10a4531d
VZ
7045bool wxGrid::MoveCursorDownBlock( bool expandSelection )
7046{
7047 return DoMoveCursorByBlock(
7048 expandSelection,
7049 wxGridForwardOperations(this, wxGridRowOperations())
7050 );
7051}
2d66e025 7052
10a4531d
VZ
7053bool wxGrid::MoveCursorLeftBlock( bool expandSelection )
7054{
7055 return DoMoveCursorByBlock(
7056 expandSelection,
7057 wxGridBackwardOperations(this, wxGridColumnOperations())
7058 );
f85afd4e
MB
7059}
7060
5c8fc7c1 7061bool wxGrid::MoveCursorRightBlock( bool expandSelection )
f85afd4e 7062{
10a4531d
VZ
7063 return DoMoveCursorByBlock(
7064 expandSelection,
7065 wxGridForwardOperations(this, wxGridColumnOperations())
7066 );
f85afd4e
MB
7067}
7068
f85afd4e 7069//
2d66e025 7070// ------ Label values and formatting
f85afd4e
MB
7071//
7072
ef316e23 7073void wxGrid::GetRowLabelAlignment( int *horiz, int *vert ) const
f85afd4e 7074{
2f024384
DS
7075 if ( horiz )
7076 *horiz = m_rowLabelHorizAlign;
7077 if ( vert )
7078 *vert = m_rowLabelVertAlign;
f85afd4e
MB
7079}
7080
ef316e23 7081void wxGrid::GetColLabelAlignment( int *horiz, int *vert ) const
f85afd4e 7082{
2f024384
DS
7083 if ( horiz )
7084 *horiz = m_colLabelHorizAlign;
7085 if ( vert )
7086 *vert = m_colLabelVertAlign;
f85afd4e
MB
7087}
7088
ef316e23 7089int wxGrid::GetColLabelTextOrientation() const
d43851f7
JS
7090{
7091 return m_colLabelTextOrientation;
7092}
7093
ef316e23 7094wxString wxGrid::GetRowLabelValue( int row ) const
f85afd4e
MB
7095{
7096 if ( m_table )
7097 {
7098 return m_table->GetRowLabelValue( row );
7099 }
7100 else
7101 {
7102 wxString s;
7103 s << row;
7104 return s;
7105 }
7106}
7107
ef316e23 7108wxString wxGrid::GetColLabelValue( int col ) const
f85afd4e
MB
7109{
7110 if ( m_table )
7111 {
7112 return m_table->GetColLabelValue( col );
7113 }
7114 else
7115 {
7116 wxString s;
7117 s << col;
7118 return s;
7119 }
7120}
7121
7122void wxGrid::SetRowLabelSize( int width )
7123{
733f486a
VZ
7124 wxASSERT( width >= 0 || width == wxGRID_AUTOSIZE );
7125
7126 if ( width == wxGRID_AUTOSIZE )
7127 {
7128 width = CalcColOrRowLabelAreaMinSize(wxGRID_ROW);
7129 }
7130
2e8cd977
MB
7131 if ( width != m_rowLabelWidth )
7132 {
2e8cd977
MB
7133 if ( width == 0 )
7134 {
ca65c044
WS
7135 m_rowLabelWin->Show( false );
7136 m_cornerLabelWin->Show( false );
2e8cd977 7137 }
7807d81c 7138 else if ( m_rowLabelWidth == 0 )
2e8cd977 7139 {
ca65c044 7140 m_rowLabelWin->Show( true );
2f024384
DS
7141 if ( m_colLabelHeight > 0 )
7142 m_cornerLabelWin->Show( true );
2e8cd977 7143 }
b99be8fb 7144
2e8cd977 7145 m_rowLabelWidth = width;
a6b40a9a 7146 InvalidateBestSize();
7807d81c 7147 CalcWindowSizes();
ca65c044 7148 wxScrolledWindow::Refresh( true );
2e8cd977 7149 }
f85afd4e
MB
7150}
7151
7152void wxGrid::SetColLabelSize( int height )
7153{
733f486a
VZ
7154 wxASSERT( height >=0 || height == wxGRID_AUTOSIZE );
7155
7156 if ( height == wxGRID_AUTOSIZE )
7157 {
7158 height = CalcColOrRowLabelAreaMinSize(wxGRID_COLUMN);
7159 }
7160
2e8cd977
MB
7161 if ( height != m_colLabelHeight )
7162 {
2e8cd977
MB
7163 if ( height == 0 )
7164 {
ad805b9e 7165 m_colWindow->Show( false );
ca65c044 7166 m_cornerLabelWin->Show( false );
2e8cd977 7167 }
7807d81c 7168 else if ( m_colLabelHeight == 0 )
2e8cd977 7169 {
ad805b9e 7170 m_colWindow->Show( true );
a9339fe2
DS
7171 if ( m_rowLabelWidth > 0 )
7172 m_cornerLabelWin->Show( true );
2e8cd977 7173 }
7807d81c 7174
2e8cd977 7175 m_colLabelHeight = height;
a6b40a9a 7176 InvalidateBestSize();
7807d81c 7177 CalcWindowSizes();
ca65c044 7178 wxScrolledWindow::Refresh( true );
2e8cd977 7179 }
f85afd4e
MB
7180}
7181
7182void wxGrid::SetLabelBackgroundColour( const wxColour& colour )
7183{
2d66e025
MB
7184 if ( m_labelBackgroundColour != colour )
7185 {
7186 m_labelBackgroundColour = colour;
7187 m_rowLabelWin->SetBackgroundColour( colour );
ad805b9e 7188 m_colWindow->SetBackgroundColour( colour );
2d66e025
MB
7189 m_cornerLabelWin->SetBackgroundColour( colour );
7190
7191 if ( !GetBatchCount() )
7192 {
7193 m_rowLabelWin->Refresh();
ad805b9e 7194 m_colWindow->Refresh();
2d66e025
MB
7195 m_cornerLabelWin->Refresh();
7196 }
7197 }
f85afd4e
MB
7198}
7199
7200void wxGrid::SetLabelTextColour( const wxColour& colour )
7201{
2d66e025
MB
7202 if ( m_labelTextColour != colour )
7203 {
7204 m_labelTextColour = colour;
7205 if ( !GetBatchCount() )
7206 {
7207 m_rowLabelWin->Refresh();
ad805b9e 7208 m_colWindow->Refresh();
2d66e025
MB
7209 }
7210 }
f85afd4e
MB
7211}
7212
7213void wxGrid::SetLabelFont( const wxFont& font )
7214{
7215 m_labelFont = font;
2d66e025
MB
7216 if ( !GetBatchCount() )
7217 {
7218 m_rowLabelWin->Refresh();
ad805b9e 7219 m_colWindow->Refresh();
2d66e025 7220 }
f85afd4e
MB
7221}
7222
7223void wxGrid::SetRowLabelAlignment( int horiz, int vert )
7224{
4c7277db
MB
7225 // allow old (incorrect) defs to be used
7226 switch ( horiz )
7227 {
7228 case wxLEFT: horiz = wxALIGN_LEFT; break;
7229 case wxRIGHT: horiz = wxALIGN_RIGHT; break;
7230 case wxCENTRE: horiz = wxALIGN_CENTRE; break;
7231 }
84912ef8 7232
4c7277db
MB
7233 switch ( vert )
7234 {
7235 case wxTOP: vert = wxALIGN_TOP; break;
7236 case wxBOTTOM: vert = wxALIGN_BOTTOM; break;
7237 case wxCENTRE: vert = wxALIGN_CENTRE; break;
7238 }
84912ef8 7239
4c7277db 7240 if ( horiz == wxALIGN_LEFT || horiz == wxALIGN_CENTRE || horiz == wxALIGN_RIGHT )
f85afd4e
MB
7241 {
7242 m_rowLabelHorizAlign = horiz;
7243 }
8f177c8e 7244
4c7277db 7245 if ( vert == wxALIGN_TOP || vert == wxALIGN_CENTRE || vert == wxALIGN_BOTTOM )
f85afd4e
MB
7246 {
7247 m_rowLabelVertAlign = vert;
7248 }
7249
2d66e025
MB
7250 if ( !GetBatchCount() )
7251 {
7252 m_rowLabelWin->Refresh();
60ff3b99 7253 }
f85afd4e
MB
7254}
7255
7256void wxGrid::SetColLabelAlignment( int horiz, int vert )
7257{
4c7277db
MB
7258 // allow old (incorrect) defs to be used
7259 switch ( horiz )
7260 {
7261 case wxLEFT: horiz = wxALIGN_LEFT; break;
7262 case wxRIGHT: horiz = wxALIGN_RIGHT; break;
7263 case wxCENTRE: horiz = wxALIGN_CENTRE; break;
7264 }
84912ef8 7265
4c7277db
MB
7266 switch ( vert )
7267 {
7268 case wxTOP: vert = wxALIGN_TOP; break;
7269 case wxBOTTOM: vert = wxALIGN_BOTTOM; break;
7270 case wxCENTRE: vert = wxALIGN_CENTRE; break;
7271 }
84912ef8 7272
4c7277db 7273 if ( horiz == wxALIGN_LEFT || horiz == wxALIGN_CENTRE || horiz == wxALIGN_RIGHT )
f85afd4e
MB
7274 {
7275 m_colLabelHorizAlign = horiz;
7276 }
8f177c8e 7277
4c7277db 7278 if ( vert == wxALIGN_TOP || vert == wxALIGN_CENTRE || vert == wxALIGN_BOTTOM )
f85afd4e
MB
7279 {
7280 m_colLabelVertAlign = vert;
7281 }
7282
2d66e025
MB
7283 if ( !GetBatchCount() )
7284 {
ad805b9e 7285 m_colWindow->Refresh();
60ff3b99 7286 }
f85afd4e
MB
7287}
7288
ca65c044
WS
7289// Note: under MSW, the default column label font must be changed because it
7290// does not support vertical printing
d43851f7
JS
7291//
7292// Example: wxFont font(9, wxSWISS, wxNORMAL, wxBOLD);
ca65c044
WS
7293// pGrid->SetLabelFont(font);
7294// pGrid->SetColLabelTextOrientation(wxVERTICAL);
d43851f7
JS
7295//
7296void wxGrid::SetColLabelTextOrientation( int textOrientation )
7297{
4db6714b 7298 if ( textOrientation == wxHORIZONTAL || textOrientation == wxVERTICAL )
d43851f7 7299 m_colLabelTextOrientation = textOrientation;
d43851f7
JS
7300
7301 if ( !GetBatchCount() )
ad805b9e 7302 m_colWindow->Refresh();
d43851f7
JS
7303}
7304
f85afd4e
MB
7305void wxGrid::SetRowLabelValue( int row, const wxString& s )
7306{
7307 if ( m_table )
7308 {
7309 m_table->SetRowLabelValue( row, s );
2d66e025
MB
7310 if ( !GetBatchCount() )
7311 {
ccdee36f 7312 wxRect rect = CellToRect( row, 0 );
70c7a608
SN
7313 if ( rect.height > 0 )
7314 {
cb309039 7315 CalcScrolledPosition(0, rect.y, &rect.x, &rect.y);
b1944ebc 7316 rect.x = 0;
70c7a608 7317 rect.width = m_rowLabelWidth;
ca65c044 7318 m_rowLabelWin->Refresh( true, &rect );
70c7a608 7319 }
2d66e025 7320 }
f85afd4e
MB
7321 }
7322}
7323
7324void wxGrid::SetColLabelValue( int col, const wxString& s )
7325{
7326 if ( m_table )
7327 {
7328 m_table->SetColLabelValue( col, s );
2d66e025
MB
7329 if ( !GetBatchCount() )
7330 {
ad805b9e 7331 if ( m_useNativeHeader )
70c7a608 7332 {
3039ade9 7333 GetGridColHeader()->UpdateColumn(col);
ad805b9e
VZ
7334 }
7335 else
7336 {
7337 wxRect rect = CellToRect( 0, col );
7338 if ( rect.width > 0 )
7339 {
7340 CalcScrolledPosition(rect.x, 0, &rect.x, &rect.y);
7341 rect.y = 0;
7342 rect.height = m_colLabelHeight;
7343 GetColLabelWindow()->Refresh( true, &rect );
7344 }
70c7a608 7345 }
2d66e025 7346 }
f85afd4e
MB
7347 }
7348}
7349
7350void wxGrid::SetGridLineColour( const wxColour& colour )
7351{
2d66e025
MB
7352 if ( m_gridLineColour != colour )
7353 {
7354 m_gridLineColour = colour;
60ff3b99 7355
9f7aee01
VZ
7356 if ( GridLinesEnabled() )
7357 RedrawGridLines();
2d66e025 7358 }
f85afd4e
MB
7359}
7360
f6bcfd97
BP
7361void wxGrid::SetCellHighlightColour( const wxColour& colour )
7362{
7363 if ( m_cellHighlightColour != colour )
7364 {
7365 m_cellHighlightColour = colour;
7366
7367 wxClientDC dc( m_gridWin );
7368 PrepareDC( dc );
7369 wxGridCellAttr* attr = GetCellAttr(m_currentCellCoords);
7370 DrawCellHighlight(dc, attr);
7371 attr->DecRef();
7372 }
7373}
7374
d2520c85
RD
7375void wxGrid::SetCellHighlightPenWidth(int width)
7376{
4db6714b
KH
7377 if (m_cellHighlightPenWidth != width)
7378 {
d2520c85
RD
7379 m_cellHighlightPenWidth = width;
7380
7381 // Just redrawing the cell highlight is not enough since that won't
4c51a665 7382 // make any visible change if the thickness is getting smaller.
d2520c85
RD
7383 int row = m_currentCellCoords.GetRow();
7384 int col = m_currentCellCoords.GetCol();
1b3b96d8 7385 if ( row == -1 || col == -1 || GetColWidth(col) <= 0 || GetRowHeight(row) <= 0 )
d2520c85 7386 return;
2f024384 7387
d2520c85 7388 wxRect rect = CellToRect(row, col);
ca65c044 7389 m_gridWin->Refresh(true, &rect);
d2520c85
RD
7390 }
7391}
7392
7393void wxGrid::SetCellHighlightROPenWidth(int width)
7394{
4db6714b
KH
7395 if (m_cellHighlightROPenWidth != width)
7396 {
d2520c85
RD
7397 m_cellHighlightROPenWidth = width;
7398
7399 // Just redrawing the cell highlight is not enough since that won't
4c51a665 7400 // make any visible change if the thickness is getting smaller.
d2520c85
RD
7401 int row = m_currentCellCoords.GetRow();
7402 int col = m_currentCellCoords.GetCol();
76c66f19
VZ
7403 if ( row == -1 || col == -1 ||
7404 GetColWidth(col) <= 0 || GetRowHeight(row) <= 0 )
d2520c85 7405 return;
ccdee36f 7406
d2520c85 7407 wxRect rect = CellToRect(row, col);
ca65c044 7408 m_gridWin->Refresh(true, &rect);
d2520c85
RD
7409 }
7410}
7411
9f7aee01
VZ
7412void wxGrid::RedrawGridLines()
7413{
7414 // the lines will be redrawn when the window is thawn
7415 if ( GetBatchCount() )
7416 return;
7417
7418 if ( GridLinesEnabled() )
7419 {
7420 wxClientDC dc( m_gridWin );
7421 PrepareDC( dc );
7422 DrawAllGridLines( dc, wxRegion() );
7423 }
7424 else // remove the grid lines
7425 {
7426 m_gridWin->Refresh();
7427 }
7428}
7429
f85afd4e
MB
7430void wxGrid::EnableGridLines( bool enable )
7431{
7432 if ( enable != m_gridLinesEnabled )
7433 {
7434 m_gridLinesEnabled = enable;
2d66e025 7435
9f7aee01
VZ
7436 RedrawGridLines();
7437 }
7438}
7439
7440void wxGrid::DoClipGridLines(bool& var, bool clip)
7441{
7442 if ( clip != var )
7443 {
7444 var = clip;
7445
7446 if ( GridLinesEnabled() )
7447 RedrawGridLines();
f85afd4e
MB
7448 }
7449}
7450
ef316e23 7451int wxGrid::GetDefaultRowSize() const
f85afd4e
MB
7452{
7453 return m_defaultRowHeight;
7454}
7455
ef316e23 7456int wxGrid::GetRowSize( int row ) const
f85afd4e 7457{
9a83f860 7458 wxCHECK_MSG( row >= 0 && row < m_numRows, 0, wxT("invalid row index") );
b99be8fb 7459
7c1cb261 7460 return GetRowHeight(row);
f85afd4e
MB
7461}
7462
ef316e23 7463int wxGrid::GetDefaultColSize() const
f85afd4e
MB
7464{
7465 return m_defaultColWidth;
7466}
7467
ef316e23 7468int wxGrid::GetColSize( int col ) const
f85afd4e 7469{
9a83f860 7470 wxCHECK_MSG( col >= 0 && col < m_numCols, 0, wxT("invalid column index") );
b99be8fb 7471
7c1cb261 7472 return GetColWidth(col);
f85afd4e
MB
7473}
7474
2e9a6788
VZ
7475// ============================================================================
7476// access to the grid attributes: each of them has a default value in the grid
7477// itself and may be overidden on a per-cell basis
7478// ============================================================================
7479
7480// ----------------------------------------------------------------------------
7481// setting default attributes
7482// ----------------------------------------------------------------------------
7483
7484void wxGrid::SetDefaultCellBackgroundColour( const wxColour& col )
7485{
2796cce3 7486 m_defaultCellAttr->SetBackgroundColour(col);
c916e13b
RR
7487#ifdef __WXGTK__
7488 m_gridWin->SetBackgroundColour(col);
7489#endif
2e9a6788
VZ
7490}
7491
7492void wxGrid::SetDefaultCellTextColour( const wxColour& col )
7493{
2796cce3 7494 m_defaultCellAttr->SetTextColour(col);
2e9a6788
VZ
7495}
7496
7497void wxGrid::SetDefaultCellAlignment( int horiz, int vert )
7498{
2796cce3 7499 m_defaultCellAttr->SetAlignment(horiz, vert);
2e9a6788
VZ
7500}
7501
27f35b66
SN
7502void wxGrid::SetDefaultCellOverflow( bool allow )
7503{
7504 m_defaultCellAttr->SetOverflow(allow);
7505}
7506
2e9a6788
VZ
7507void wxGrid::SetDefaultCellFont( const wxFont& font )
7508{
2796cce3
RD
7509 m_defaultCellAttr->SetFont(font);
7510}
7511
ca63e8e9
RD
7512// For editors and renderers the type registry takes precedence over the
7513// default attr, so we need to register the new editor/renderer for the string
7514// data type in order to make setting a default editor/renderer appear to
7515// work correctly.
7516
0ba143c9
RD
7517void wxGrid::SetDefaultRenderer(wxGridCellRenderer *renderer)
7518{
ca63e8e9
RD
7519 RegisterDataType(wxGRID_VALUE_STRING,
7520 renderer,
7521 GetDefaultEditorForType(wxGRID_VALUE_STRING));
0ba143c9 7522}
2e9a6788 7523
0ba143c9
RD
7524void wxGrid::SetDefaultEditor(wxGridCellEditor *editor)
7525{
ca63e8e9
RD
7526 RegisterDataType(wxGRID_VALUE_STRING,
7527 GetDefaultRendererForType(wxGRID_VALUE_STRING),
42841dfc 7528 editor);
0ba143c9 7529}
9b4aede2 7530
2e9a6788 7531// ----------------------------------------------------------------------------
ef316e23 7532// access to the default attributes
2e9a6788
VZ
7533// ----------------------------------------------------------------------------
7534
ef316e23 7535wxColour wxGrid::GetDefaultCellBackgroundColour() const
f85afd4e 7536{
2796cce3 7537 return m_defaultCellAttr->GetBackgroundColour();
f85afd4e
MB
7538}
7539
ef316e23 7540wxColour wxGrid::GetDefaultCellTextColour() const
2e9a6788 7541{
2796cce3 7542 return m_defaultCellAttr->GetTextColour();
2e9a6788
VZ
7543}
7544
ef316e23 7545wxFont wxGrid::GetDefaultCellFont() const
2e9a6788 7546{
2796cce3 7547 return m_defaultCellAttr->GetFont();
2e9a6788
VZ
7548}
7549
ef316e23 7550void wxGrid::GetDefaultCellAlignment( int *horiz, int *vert ) const
2e9a6788 7551{
2796cce3 7552 m_defaultCellAttr->GetAlignment(horiz, vert);
2e9a6788
VZ
7553}
7554
ef316e23 7555bool wxGrid::GetDefaultCellOverflow() const
27f35b66
SN
7556{
7557 return m_defaultCellAttr->GetOverflow();
7558}
7559
0ba143c9
RD
7560wxGridCellRenderer *wxGrid::GetDefaultRenderer() const
7561{
0b190b0f 7562 return m_defaultCellAttr->GetRenderer(NULL, 0, 0);
0ba143c9 7563}
ab79958a 7564
0ba143c9
RD
7565wxGridCellEditor *wxGrid::GetDefaultEditor() const
7566{
4db6714b 7567 return m_defaultCellAttr->GetEditor(NULL, 0, 0);
0ba143c9 7568}
9b4aede2 7569
2e9a6788
VZ
7570// ----------------------------------------------------------------------------
7571// access to cell attributes
7572// ----------------------------------------------------------------------------
7573
ef316e23 7574wxColour wxGrid::GetCellBackgroundColour(int row, int col) const
f85afd4e 7575{
0a976765 7576 wxGridCellAttr *attr = GetCellAttr(row, col);
2796cce3 7577 wxColour colour = attr->GetBackgroundColour();
39bcce60 7578 attr->DecRef();
2f024384 7579
b99be8fb 7580 return colour;
f85afd4e
MB
7581}
7582
ef316e23 7583wxColour wxGrid::GetCellTextColour( int row, int col ) const
f85afd4e 7584{
0a976765 7585 wxGridCellAttr *attr = GetCellAttr(row, col);
2796cce3 7586 wxColour colour = attr->GetTextColour();
39bcce60 7587 attr->DecRef();
2f024384 7588
b99be8fb 7589 return colour;
f85afd4e
MB
7590}
7591
ef316e23 7592wxFont wxGrid::GetCellFont( int row, int col ) const
f85afd4e 7593{
0a976765 7594 wxGridCellAttr *attr = GetCellAttr(row, col);
2796cce3 7595 wxFont font = attr->GetFont();
39bcce60 7596 attr->DecRef();
2f024384 7597
b99be8fb 7598 return font;
f85afd4e
MB
7599}
7600
ef316e23 7601void wxGrid::GetCellAlignment( int row, int col, int *horiz, int *vert ) const
f85afd4e 7602{
0a976765 7603 wxGridCellAttr *attr = GetCellAttr(row, col);
2796cce3 7604 attr->GetAlignment(horiz, vert);
39bcce60 7605 attr->DecRef();
2e9a6788
VZ
7606}
7607
ef316e23 7608bool wxGrid::GetCellOverflow( int row, int col ) const
27f35b66
SN
7609{
7610 wxGridCellAttr *attr = GetCellAttr(row, col);
7611 bool allow = attr->GetOverflow();
7612 attr->DecRef();
4db6714b 7613
27f35b66
SN
7614 return allow;
7615}
7616
ea99e8e3
VZ
7617wxGrid::CellSpan
7618wxGrid::GetCellSize( int row, int col, int *num_rows, int *num_cols ) const
27f35b66
SN
7619{
7620 wxGridCellAttr *attr = GetCellAttr(row, col);
7621 attr->GetSize( num_rows, num_cols );
7622 attr->DecRef();
ea99e8e3
VZ
7623
7624 if ( *num_rows == 1 && *num_cols == 1 )
7625 return CellSpan_None; // just a normal cell
7626
7627 if ( *num_rows < 0 || *num_cols < 0 )
7628 return CellSpan_Inside; // covered by a multi-span cell
7629
7630 // this cell spans multiple cells to its right/bottom
7631 return CellSpan_Main;
27f35b66
SN
7632}
7633
ef316e23 7634wxGridCellRenderer* wxGrid::GetCellRenderer(int row, int col) const
2796cce3
RD
7635{
7636 wxGridCellAttr* attr = GetCellAttr(row, col);
28a77bc4 7637 wxGridCellRenderer* renderer = attr->GetRenderer(this, row, col);
2796cce3 7638 attr->DecRef();
0b190b0f 7639
2796cce3
RD
7640 return renderer;
7641}
7642
ef316e23 7643wxGridCellEditor* wxGrid::GetCellEditor(int row, int col) const
9b4aede2
RD
7644{
7645 wxGridCellAttr* attr = GetCellAttr(row, col);
28a77bc4 7646 wxGridCellEditor* editor = attr->GetEditor(this, row, col);
9b4aede2 7647 attr->DecRef();
0b190b0f 7648
9b4aede2
RD
7649 return editor;
7650}
7651
283b7808
VZ
7652bool wxGrid::IsReadOnly(int row, int col) const
7653{
7654 wxGridCellAttr* attr = GetCellAttr(row, col);
7655 bool isReadOnly = attr->IsReadOnly();
7656 attr->DecRef();
4db6714b 7657
283b7808
VZ
7658 return isReadOnly;
7659}
7660
2e9a6788 7661// ----------------------------------------------------------------------------
758cbedf 7662// attribute support: cache, automatic provider creation, ...
2e9a6788
VZ
7663// ----------------------------------------------------------------------------
7664
ef316e23 7665bool wxGrid::CanHaveAttributes() const
2e9a6788
VZ
7666{
7667 if ( !m_table )
7668 {
ca65c044 7669 return false;
2e9a6788
VZ
7670 }
7671
f2d76237 7672 return m_table->CanHaveAttributes();
2e9a6788
VZ
7673}
7674
0a976765
VZ
7675void wxGrid::ClearAttrCache()
7676{
7677 if ( m_attrCache.row != -1 )
7678 {
54181a33 7679 wxGridCellAttr *oldAttr = m_attrCache.attr;
19d7140e 7680 m_attrCache.attr = NULL;
0a976765 7681 m_attrCache.row = -1;
1506cc66
SN
7682 // wxSafeDecRec(...) might cause event processing that accesses
7683 // the cached attribute, if one exists (e.g. by deleting the
7684 // editor stored within the attribute). Therefore it is important
ace8d849 7685 // to invalidate the cache before calling wxSafeDecRef!
1506cc66 7686 wxSafeDecRef(oldAttr);
0a976765
VZ
7687 }
7688}
7689
b99450ee
VZ
7690void wxGrid::RefreshAttr(int row, int col)
7691{
7692 if ( m_attrCache.row == row && m_attrCache.col == col )
7693 ClearAttrCache();
7694}
7695
7696
0a976765
VZ
7697void wxGrid::CacheAttr(int row, int col, wxGridCellAttr *attr) const
7698{
2b5f62a0
VZ
7699 if ( attr != NULL )
7700 {
f48a1159 7701 wxGrid * const self = const_cast<wxGrid *>(this);
0a976765 7702
2b5f62a0
VZ
7703 self->ClearAttrCache();
7704 self->m_attrCache.row = row;
7705 self->m_attrCache.col = col;
7706 self->m_attrCache.attr = attr;
7707 wxSafeIncRef(attr);
7708 }
0a976765
VZ
7709}
7710
7711bool wxGrid::LookupAttr(int row, int col, wxGridCellAttr **attr) const
7712{
7713 if ( row == m_attrCache.row && col == m_attrCache.col )
7714 {
7715 *attr = m_attrCache.attr;
39bcce60 7716 wxSafeIncRef(m_attrCache.attr);
0a976765
VZ
7717
7718#ifdef DEBUG_ATTR_CACHE
7719 gs_nAttrCacheHits++;
7720#endif
7721
ca65c044 7722 return true;
0a976765
VZ
7723 }
7724 else
7725 {
7726#ifdef DEBUG_ATTR_CACHE
7727 gs_nAttrCacheMisses++;
7728#endif
4db6714b 7729
ca65c044 7730 return false;
0a976765
VZ
7731 }
7732}
7733
2e9a6788
VZ
7734wxGridCellAttr *wxGrid::GetCellAttr(int row, int col) const
7735{
a373d23b
SN
7736 wxGridCellAttr *attr = NULL;
7737 // Additional test to avoid looking at the cache e.g. for
7738 // wxNoCellCoords, as this will confuse memory management.
7739 if ( row >= 0 )
7740 {
3ed884a0
SN
7741 if ( !LookupAttr(row, col, &attr) )
7742 {
c2f5b920 7743 attr = m_table ? m_table->GetAttr(row, col, wxGridCellAttr::Any)
10a4531d 7744 : NULL;
3ed884a0
SN
7745 CacheAttr(row, col, attr);
7746 }
0a976765 7747 }
4db6714b 7748
508011ce
VZ
7749 if (attr)
7750 {
2796cce3 7751 attr->SetDefAttr(m_defaultCellAttr);
508011ce
VZ
7752 }
7753 else
7754 {
2796cce3
RD
7755 attr = m_defaultCellAttr;
7756 attr->IncRef();
7757 }
2e9a6788 7758
0a976765
VZ
7759 return attr;
7760}
7761
7762wxGridCellAttr *wxGrid::GetOrCreateCellAttr(int row, int col) const
7763{
10a4531d 7764 wxGridCellAttr *attr = NULL;
71e60f70 7765 bool canHave = ((wxGrid*)this)->CanHaveAttributes();
0a976765 7766
9a83f860
VZ
7767 wxCHECK_MSG( canHave, attr, wxT("Cell attributes not allowed"));
7768 wxCHECK_MSG( m_table, attr, wxT("must have a table") );
1df4050d
VZ
7769
7770 attr = m_table->GetAttr(row, col, wxGridCellAttr::Cell);
7771 if ( !attr )
7772 {
7773 attr = new wxGridCellAttr(m_defaultCellAttr);
7774
7775 // artificially inc the ref count to match DecRef() in caller
7776 attr->IncRef();
7777 m_table->SetAttr(attr, row, col);
7778 }
0a976765 7779
2e9a6788
VZ
7780 return attr;
7781}
7782
0b190b0f
VZ
7783// ----------------------------------------------------------------------------
7784// setting column attributes (wrappers around SetColAttr)
7785// ----------------------------------------------------------------------------
7786
7787void wxGrid::SetColFormatBool(int col)
7788{
7789 SetColFormatCustom(col, wxGRID_VALUE_BOOL);
7790}
7791
7792void wxGrid::SetColFormatNumber(int col)
7793{
7794 SetColFormatCustom(col, wxGRID_VALUE_NUMBER);
7795}
7796
7797void wxGrid::SetColFormatFloat(int col, int width, int precision)
7798{
7799 wxString typeName = wxGRID_VALUE_FLOAT;
7800 if ( (width != -1) || (precision != -1) )
7801 {
9a83f860 7802 typeName << wxT(':') << width << wxT(',') << precision;
0b190b0f
VZ
7803 }
7804
7805 SetColFormatCustom(col, typeName);
7806}
7807
7808void wxGrid::SetColFormatCustom(int col, const wxString& typeName)
7809{
999836aa 7810 wxGridCellAttr *attr = m_table->GetAttr(-1, col, wxGridCellAttr::Col );
4db6714b 7811 if (!attr)
19d7140e 7812 attr = new wxGridCellAttr;
0b190b0f
VZ
7813 wxGridCellRenderer *renderer = GetDefaultRendererForType(typeName);
7814 attr->SetRenderer(renderer);
54181a33
VZ
7815 wxGridCellEditor *editor = GetDefaultEditorForType(typeName);
7816 attr->SetEditor(editor);
0b190b0f
VZ
7817
7818 SetColAttr(col, attr);
19d7140e 7819
0b190b0f
VZ
7820}
7821
758cbedf
VZ
7822// ----------------------------------------------------------------------------
7823// setting cell attributes: this is forwarded to the table
7824// ----------------------------------------------------------------------------
7825
27f35b66
SN
7826void wxGrid::SetAttr(int row, int col, wxGridCellAttr *attr)
7827{
7828 if ( CanHaveAttributes() )
7829 {
7830 m_table->SetAttr(attr, row, col);
7831 ClearAttrCache();
7832 }
7833 else
7834 {
7835 wxSafeDecRef(attr);
7836 }
7837}
7838
758cbedf
VZ
7839void wxGrid::SetRowAttr(int row, wxGridCellAttr *attr)
7840{
7841 if ( CanHaveAttributes() )
7842 {
7843 m_table->SetRowAttr(attr, row);
19d7140e 7844 ClearAttrCache();
758cbedf
VZ
7845 }
7846 else
7847 {
39bcce60 7848 wxSafeDecRef(attr);
758cbedf
VZ
7849 }
7850}
7851
7852void wxGrid::SetColAttr(int col, wxGridCellAttr *attr)
7853{
7854 if ( CanHaveAttributes() )
7855 {
7856 m_table->SetColAttr(attr, col);
19d7140e 7857 ClearAttrCache();
758cbedf
VZ
7858 }
7859 else
7860 {
39bcce60 7861 wxSafeDecRef(attr);
758cbedf
VZ
7862 }
7863}
7864
2e9a6788
VZ
7865void wxGrid::SetCellBackgroundColour( int row, int col, const wxColour& colour )
7866{
7867 if ( CanHaveAttributes() )
7868 {
0a976765 7869 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
2e9a6788
VZ
7870 attr->SetBackgroundColour(colour);
7871 attr->DecRef();
7872 }
7873}
7874
7875void wxGrid::SetCellTextColour( int row, int col, const wxColour& colour )
7876{
7877 if ( CanHaveAttributes() )
7878 {
0a976765 7879 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
2e9a6788
VZ
7880 attr->SetTextColour(colour);
7881 attr->DecRef();
7882 }
7883}
7884
7885void wxGrid::SetCellFont( int row, int col, const wxFont& font )
7886{
7887 if ( CanHaveAttributes() )
7888 {
0a976765 7889 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
2e9a6788
VZ
7890 attr->SetFont(font);
7891 attr->DecRef();
7892 }
7893}
7894
7895void wxGrid::SetCellAlignment( int row, int col, int horiz, int vert )
7896{
7897 if ( CanHaveAttributes() )
7898 {
0a976765 7899 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
2e9a6788
VZ
7900 attr->SetAlignment(horiz, vert);
7901 attr->DecRef();
ab79958a
VZ
7902 }
7903}
7904
27f35b66
SN
7905void wxGrid::SetCellOverflow( int row, int col, bool allow )
7906{
7907 if ( CanHaveAttributes() )
7908 {
7909 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
7910 attr->SetOverflow(allow);
7911 attr->DecRef();
7912 }
7913}
7914
7915void wxGrid::SetCellSize( int row, int col, int num_rows, int num_cols )
7916{
7917 if ( CanHaveAttributes() )
7918 {
7919 int cell_rows, cell_cols;
7920
7921 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
7922 attr->GetSize(&cell_rows, &cell_cols);
7923 attr->SetSize(num_rows, num_cols);
7924 attr->DecRef();
7925
7926 // Cannot set the size of a cell to 0 or negative values
7927 // While it is perfectly legal to do that, this function cannot
7928 // handle all the possibilies, do it by hand by getting the CellAttr.
7929 // You can only set the size of a cell to 1,1 or greater with this fn
7930 wxASSERT_MSG( !((cell_rows < 1) || (cell_cols < 1)),
7931 wxT("wxGrid::SetCellSize setting cell size that is already part of another cell"));
7932 wxASSERT_MSG( !((num_rows < 1) || (num_cols < 1)),
7933 wxT("wxGrid::SetCellSize setting cell size to < 1"));
7934
7935 // if this was already a multicell then "turn off" the other cells first
a6b2078d 7936 if ((cell_rows > 1) || (cell_cols > 1))
27f35b66
SN
7937 {
7938 int i, j;
2f024384 7939 for (j=row; j < row + cell_rows; j++)
27f35b66 7940 {
2f024384 7941 for (i=col; i < col + cell_cols; i++)
27f35b66
SN
7942 {
7943 if ((i != col) || (j != row))
7944 {
7945 wxGridCellAttr *attr_stub = GetOrCreateCellAttr(j, i);
7946 attr_stub->SetSize( 1, 1 );
7947 attr_stub->DecRef();
7948 }
7949 }
7950 }
7951 }
7952
7953 // mark the cells that will be covered by this cell to
7954 // negative or zero values to point back at this cell
7955 if (((num_rows > 1) || (num_cols > 1)) && (num_rows >= 1) && (num_cols >= 1))
7956 {
7957 int i, j;
2f024384 7958 for (j=row; j < row + num_rows; j++)
27f35b66 7959 {
2f024384 7960 for (i=col; i < col + num_cols; i++)
27f35b66
SN
7961 {
7962 if ((i != col) || (j != row))
7963 {
7964 wxGridCellAttr *attr_stub = GetOrCreateCellAttr(j, i);
2f024384 7965 attr_stub->SetSize( row - j, col - i );
27f35b66
SN
7966 attr_stub->DecRef();
7967 }
7968 }
7969 }
7970 }
7971 }
7972}
7973
ab79958a
VZ
7974void wxGrid::SetCellRenderer(int row, int col, wxGridCellRenderer *renderer)
7975{
7976 if ( CanHaveAttributes() )
7977 {
0a976765 7978 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
ab79958a
VZ
7979 attr->SetRenderer(renderer);
7980 attr->DecRef();
9b4aede2
RD
7981 }
7982}
7983
7984void wxGrid::SetCellEditor(int row, int col, wxGridCellEditor* editor)
7985{
7986 if ( CanHaveAttributes() )
7987 {
7988 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
7989 attr->SetEditor(editor);
7990 attr->DecRef();
283b7808
VZ
7991 }
7992}
7993
7994void wxGrid::SetReadOnly(int row, int col, bool isReadOnly)
7995{
7996 if ( CanHaveAttributes() )
7997 {
7998 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
7999 attr->SetReadOnly(isReadOnly);
8000 attr->DecRef();
2e9a6788 8001 }
f85afd4e
MB
8002}
8003
f2d76237
RD
8004// ----------------------------------------------------------------------------
8005// Data type registration
8006// ----------------------------------------------------------------------------
8007
8008void wxGrid::RegisterDataType(const wxString& typeName,
8009 wxGridCellRenderer* renderer,
8010 wxGridCellEditor* editor)
8011{
8012 m_typeRegistry->RegisterDataType(typeName, renderer, editor);
8013}
8014
8015
a9339fe2 8016wxGridCellEditor * wxGrid::GetDefaultEditorForCell(int row, int col) const
f2d76237
RD
8017{
8018 wxString typeName = m_table->GetTypeName(row, col);
8019 return GetDefaultEditorForType(typeName);
8020}
8021
a9339fe2 8022wxGridCellRenderer * wxGrid::GetDefaultRendererForCell(int row, int col) const
f2d76237
RD
8023{
8024 wxString typeName = m_table->GetTypeName(row, col);
8025 return GetDefaultRendererForType(typeName);
8026}
8027
a9339fe2 8028wxGridCellEditor * wxGrid::GetDefaultEditorForType(const wxString& typeName) const
f2d76237 8029{
c4608a8a 8030 int index = m_typeRegistry->FindOrCloneDataType(typeName);
0b190b0f
VZ
8031 if ( index == wxNOT_FOUND )
8032 {
767e0835 8033 wxFAIL_MSG(wxString::Format(wxT("Unknown data type name [%s]"), typeName.c_str()));
0b190b0f 8034
f2d76237
RD
8035 return NULL;
8036 }
0b190b0f 8037
f2d76237
RD
8038 return m_typeRegistry->GetEditor(index);
8039}
8040
a9339fe2 8041wxGridCellRenderer * wxGrid::GetDefaultRendererForType(const wxString& typeName) const
f2d76237 8042{
c4608a8a 8043 int index = m_typeRegistry->FindOrCloneDataType(typeName);
0b190b0f
VZ
8044 if ( index == wxNOT_FOUND )
8045 {
767e0835 8046 wxFAIL_MSG(wxString::Format(wxT("Unknown data type name [%s]"), typeName.c_str()));
0b190b0f 8047
c4608a8a 8048 return NULL;
e72b4213 8049 }
0b190b0f 8050
c4608a8a 8051 return m_typeRegistry->GetRenderer(index);
f2d76237
RD
8052}
8053
2e9a6788
VZ
8054// ----------------------------------------------------------------------------
8055// row/col size
8056// ----------------------------------------------------------------------------
8057
82edfbe7
VZ
8058void wxGrid::DoDisableLineResize(int line, wxGridFixedIndicesSet *& setFixed)
8059{
8060 if ( !setFixed )
8061 {
8062 setFixed = new wxGridFixedIndicesSet;
8063 }
8064
8065 setFixed->insert(line);
8066}
8067
8068bool
8069wxGrid::DoCanResizeLine(int line, const wxGridFixedIndicesSet *setFixed) const
8070{
8071 return !setFixed || !setFixed->count(line);
8072}
8073
6e8524b1
MB
8074void wxGrid::EnableDragRowSize( bool enable )
8075{
8076 m_canDragRowSize = enable;
8077}
8078
6e8524b1
MB
8079void wxGrid::EnableDragColSize( bool enable )
8080{
8081 m_canDragColSize = enable;
8082}
8083
4cfa5de6
RD
8084void wxGrid::EnableDragGridSize( bool enable )
8085{
8086 m_canDragGridSize = enable;
8087}
8088
79dbea21
RD
8089void wxGrid::EnableDragCell( bool enable )
8090{
8091 m_canDragCell = enable;
8092}
6e8524b1 8093
f85afd4e
MB
8094void wxGrid::SetDefaultRowSize( int height, bool resizeExistingRows )
8095{
b8d24d4e 8096 m_defaultRowHeight = wxMax( height, m_minAcceptableRowHeight );
f85afd4e
MB
8097
8098 if ( resizeExistingRows )
8099 {
b1da8107
SN
8100 // since we are resizing all rows to the default row size,
8101 // we can simply clear the row heights and row bottoms
8102 // arrays (which also allows us to take advantage of
8103 // some speed optimisations)
8104 m_rowHeights.Empty();
8105 m_rowBottoms.Empty();
edb89f7e
VZ
8106 if ( !GetBatchCount() )
8107 CalcDimensions();
f85afd4e
MB
8108 }
8109}
8110
30496905
VZ
8111namespace
8112{
8113
8114// This is a common part of SetRowSize() and SetColSize() which takes care of
8115// updating the height/width of a row/column depending on its current value and
8116// the new one.
8117//
8118// Returns the difference between the new and the old size.
8119int UpdateRowOrColSize(int& sizeCurrent, int sizeNew)
8120{
8121 // On input here sizeCurrent can be negative if it's currently hidden (the
8122 // real size is its absolute value then). And sizeNew can be 0 to indicate
8123 // that the row/column should be hidden or -1 to indicate that it should be
8124 // shown again.
8125
8126 if ( sizeNew < 0 )
8127 {
8128 // We're showing back a previously hidden row/column.
8129 wxASSERT_MSG( sizeNew == -1, wxS("New size must be positive or -1.") );
8130
2328f468
VZ
8131 // If it's already visible, simply do nothing.
8132 if ( sizeCurrent >= 0 )
8133 return 0;
30496905 8134
2328f468 8135 // Otherwise show it by restoring its old size.
30496905
VZ
8136 sizeCurrent = -sizeCurrent;
8137
8138 // This is positive which is correct.
8139 return sizeCurrent;
8140 }
8141 else if ( sizeNew == 0 )
8142 {
8143 // We're hiding a row/column.
30496905 8144
2328f468
VZ
8145 // If it's already hidden, simply do nothing.
8146 if ( sizeCurrent <= 0 )
8147 return 0;
8148
8149 // Otherwise hide it and also remember the shown size to be able to
8150 // restore it later.
30496905
VZ
8151 sizeCurrent = -sizeCurrent;
8152
8153 // This is negative which is correct.
8154 return sizeCurrent;
8155 }
8156 else // We're just changing the row/column size.
8157 {
8158 // Here it could have been hidden or not previously.
8159 const int sizeOld = sizeCurrent < 0 ? 0 : sizeCurrent;
8160
8161 sizeCurrent = sizeNew;
8162
8163 return sizeCurrent - sizeOld;
8164 }
8165}
8166
8167} // anonymous namespace
8168
f85afd4e
MB
8169void wxGrid::SetRowSize( int row, int height )
8170{
30496905
VZ
8171 // See comment in SetColSize
8172 if ( height > 0 && height < GetRowMinimalAcceptableHeight())
8173 return;
60ff3b99 8174
30496905 8175 // The value of -1 is special and means to fit the height to the row label.
d929eddf
VZ
8176 // As with the columns, ignore attempts to auto-size the hidden rows.
8177 if ( height == -1 && GetRowHeight(row) != 0 )
ad7502d8
SN
8178 {
8179 long w, h;
8180 wxArrayString lines;
8181 wxClientDC dc(m_rowLabelWin);
8182 dc.SetFont(GetLabelFont());
8183 StringToLines(GetRowLabelValue( row ), lines);
ace8d849
VZ
8184 GetTextBoxSize( dc, lines, &w, &h );
8185 //check that it is not less than the minimal height
8186 height = wxMax(h, GetRowMinimalAcceptableHeight());
ad7502d8
SN
8187 }
8188
30496905
VZ
8189 DoSetRowSize(row, height);
8190}
8191
8192void wxGrid::DoSetRowSize( int row, int height )
8193{
8194 wxCHECK_RET( row >= 0 && row < m_numRows, wxT("invalid row index") );
b8d24d4e 8195
7c1cb261
VZ
8196 if ( m_rowHeights.IsEmpty() )
8197 {
8198 // need to really create the array
8199 InitRowHeights();
8200 }
60ff3b99 8201
30496905
VZ
8202 const int diff = UpdateRowOrColSize(m_rowHeights[row], height);
8203 if ( !diff )
8204 return;
60ff3b99 8205
0ed3b812 8206 for ( int i = row; i < m_numRows; i++ )
f85afd4e 8207 {
b99be8fb 8208 m_rowBottoms[i] += diff;
f85afd4e 8209 }
2f024384 8210
a6b40a9a
VZ
8211 InvalidateBestSize();
8212
faec5a43 8213 if ( !GetBatchCount() )
ce84ef6a 8214 {
faec5a43 8215 CalcDimensions();
ce84ef6a
VZ
8216 Refresh();
8217 }
f85afd4e
MB
8218}
8219
8220void wxGrid::SetDefaultColSize( int width, bool resizeExistingCols )
8221{
ef316e23
VZ
8222 // we dont allow zero default column width
8223 m_defaultColWidth = wxMax( wxMax( width, m_minAcceptableColWidth ), 1 );
f85afd4e
MB
8224
8225 if ( resizeExistingCols )
8226 {
b1da8107
SN
8227 // since we are resizing all columns to the default column size,
8228 // we can simply clear the col widths and col rights
8229 // arrays (which also allows us to take advantage of
8230 // some speed optimisations)
8231 m_colWidths.Empty();
8232 m_colRights.Empty();
edb89f7e
VZ
8233 if ( !GetBatchCount() )
8234 CalcDimensions();
f85afd4e
MB
8235 }
8236}
8237
8238void wxGrid::SetColSize( int col, int width )
8239{
30496905
VZ
8240 // we intentionally don't test whether the width is less than
8241 // GetColMinimalWidth() here but we do compare it with
8242 // GetColMinimalAcceptableWidth() as otherwise things currently break (see
8243 // #651) -- and we also always allow the width of 0 as it has the special
8244 // sense of hiding the column
8245 if ( width > 0 && width < GetColMinimalAcceptableWidth() )
8246 return;
60ff3b99 8247
30496905 8248 // The value of -1 is special and means to fit the width to the column label.
d929eddf
VZ
8249 //
8250 // Notice that we currently don't support auto-sizing hidden columns (we
8251 // could, but it's not clear whether this is really needed and it would
8252 // make the code more complex), and for them passing -1 simply means to
8253 // show the column back using its old size.
8254 if ( width == -1 && GetColWidth(col) != 0 )
ad7502d8
SN
8255 {
8256 long w, h;
8257 wxArrayString lines;
ad805b9e 8258 wxClientDC dc(m_colWindow);
ad7502d8
SN
8259 dc.SetFont(GetLabelFont());
8260 StringToLines(GetColLabelValue(col), lines);
8261 if ( GetColLabelTextOrientation() == wxHORIZONTAL )
8262 GetTextBoxSize( dc, lines, &w, &h );
8263 else
8264 GetTextBoxSize( dc, lines, &h, &w );
8265 width = w + 6;
ace8d849 8266 //check that it is not less than the minimal width
54181a33 8267 width = wxMax(width, GetColMinimalAcceptableWidth());
ad7502d8
SN
8268 }
8269
30496905
VZ
8270 DoSetColSize(col, width);
8271}
8272
8273void wxGrid::DoSetColSize( int col, int width )
8274{
8275 wxCHECK_RET( col >= 0 && col < m_numCols, wxT("invalid column index") );
3e13956a 8276
7c1cb261
VZ
8277 if ( m_colWidths.IsEmpty() )
8278 {
8279 // need to really create the array
8280 InitColWidths();
8281 }
f85afd4e 8282
30496905
VZ
8283 const int diff = UpdateRowOrColSize(m_colWidths[col], width);
8284 if ( !diff )
8285 return;
8286
009c7216 8287 if ( m_useNativeHeader )
3039ade9 8288 GetGridColHeader()->UpdateColumn(col);
009c7216 8289 //else: will be refreshed when the header is redrawn
60ff3b99 8290
0ed3b812 8291 for ( int colPos = GetColPos(col); colPos < m_numCols; colPos++ )
f85afd4e 8292 {
0ed3b812 8293 m_colRights[GetColAt(colPos)] += diff;
f85afd4e 8294 }
962a48f6 8295
a6b40a9a
VZ
8296 InvalidateBestSize();
8297
faec5a43 8298 if ( !GetBatchCount() )
ad805b9e 8299 {
faec5a43 8300 CalcDimensions();
ad805b9e
VZ
8301 Refresh();
8302 }
f85afd4e
MB
8303}
8304
43947979
VZ
8305void wxGrid::SetColMinimalWidth( int col, int width )
8306{
4db6714b
KH
8307 if (width > GetColMinimalAcceptableWidth())
8308 {
c6fbe2f0 8309 wxLongToLongHashMap::key_type key = (wxLongToLongHashMap::key_type)col;
8253f2e0 8310 m_colMinWidths[key] = width;
b8d24d4e 8311 }
af547d51
VZ
8312}
8313
8314void wxGrid::SetRowMinimalHeight( int row, int width )
8315{
4db6714b
KH
8316 if (width > GetRowMinimalAcceptableHeight())
8317 {
c6fbe2f0 8318 wxLongToLongHashMap::key_type key = (wxLongToLongHashMap::key_type)row;
8253f2e0 8319 m_rowMinHeights[key] = width;
b8d24d4e 8320 }
43947979
VZ
8321}
8322
8323int wxGrid::GetColMinimalWidth(int col) const
8324{
c6fbe2f0 8325 wxLongToLongHashMap::key_type key = (wxLongToLongHashMap::key_type)col;
8253f2e0 8326 wxLongToLongHashMap::const_iterator it = m_colMinWidths.find(key);
962a48f6 8327
ba8c1601 8328 return it != m_colMinWidths.end() ? (int)it->second : m_minAcceptableColWidth;
af547d51
VZ
8329}
8330
8331int wxGrid::GetRowMinimalHeight(int row) const
8332{
c6fbe2f0 8333 wxLongToLongHashMap::key_type key = (wxLongToLongHashMap::key_type)row;
8253f2e0 8334 wxLongToLongHashMap::const_iterator it = m_rowMinHeights.find(key);
962a48f6 8335
ba8c1601 8336 return it != m_rowMinHeights.end() ? (int)it->second : m_minAcceptableRowHeight;
b8d24d4e
RG
8337}
8338
8339void wxGrid::SetColMinimalAcceptableWidth( int width )
8340{
20c84410 8341 // We do allow a width of 0 since this gives us
962a48f6
DS
8342 // an easy way to temporarily hiding columns.
8343 if ( width >= 0 )
8344 m_minAcceptableColWidth = width;
b8d24d4e
RG
8345}
8346
8347void wxGrid::SetRowMinimalAcceptableHeight( int height )
8348{
20c84410 8349 // We do allow a height of 0 since this gives us
962a48f6
DS
8350 // an easy way to temporarily hiding rows.
8351 if ( height >= 0 )
8352 m_minAcceptableRowHeight = height;
17a1ebd1 8353}
b8d24d4e
RG
8354
8355int wxGrid::GetColMinimalAcceptableWidth() const
8356{
8357 return m_minAcceptableColWidth;
8358}
8359
8360int wxGrid::GetRowMinimalAcceptableHeight() const
8361{
8362 return m_minAcceptableRowHeight;
43947979
VZ
8363}
8364
57c086ef
VZ
8365// ----------------------------------------------------------------------------
8366// auto sizing
8367// ----------------------------------------------------------------------------
8368
733f486a
VZ
8369void
8370wxGrid::AutoSizeColOrRow(int colOrRow, bool setAsMin, wxGridDirection direction)
65e4e78e 8371{
733f486a
VZ
8372 const bool column = direction == wxGRID_COLUMN;
8373
6922252d
VZ
8374 // We don't support auto-sizing hidden rows or columns, this doesn't seem
8375 // to make much sense.
8376 if ( column )
8377 {
8378 if ( GetColWidth(colOrRow) == 0 )
8379 return;
8380 }
8381 else
8382 {
8383 if ( GetRowHeight(colOrRow) == 0 )
8384 return;
8385 }
8386
65e4e78e
VZ
8387 wxClientDC dc(m_gridWin);
8388
962a48f6 8389 // cancel editing of cell
13f6e9e8
RG
8390 HideCellEditControl();
8391 SaveEditControlValue();
8392
68abd97d
VZ
8393 // initialize both of them just to avoid compiler warnings even if only
8394 // really needs to be initialized here
8395 int row,
8396 col;
8397 if ( column )
8398 {
8399 row = -1;
8400 col = colOrRow;
8401 }
8402 else
8403 {
8404 row = colOrRow;
a95e38c0 8405 col = -1;
68abd97d 8406 }
af547d51
VZ
8407
8408 wxCoord extent, extentMax = 0;
8409 int max = column ? m_numRows : m_numCols;
39bcce60 8410 for ( int rowOrCol = 0; rowOrCol < max; rowOrCol++ )
65e4e78e 8411 {
af547d51 8412 if ( column )
bc7d2e9d 8413 {
af547d51 8414 row = rowOrCol;
bc7d2e9d
VZ
8415 col = colOrRow;
8416 }
6922252d 8417 else
bc7d2e9d
VZ
8418 {
8419 row = colOrRow;
af547d51 8420 col = rowOrCol;
bc7d2e9d 8421 }
774f6b31
VZ
8422
8423 // we need to account for the cells spanning multiple columns/rows:
8424 // while they may need a lot of space, they don't need all of it in
8425 // this column/row
8426 int numRows, numCols;
8427 const CellSpan span = GetCellSize(row, col, &numRows, &numCols);
8428 if ( span == CellSpan_Inside )
8429 {
8430 // we need to get the size of the main cell, not of a cell hidden
8431 // by it
8432 row += numRows;
8433 col += numCols;
8434
8435 // get the size of the main cell too
8436 GetCellSize(row, col, &numRows, &numCols);
8437 }
af547d51 8438
bc7d2e9d 8439 // get cell ( main cell if CellSpan_Inside ) renderer best size
2f024384
DS
8440 wxGridCellAttr *attr = GetCellAttr(row, col);
8441 wxGridCellRenderer *renderer = attr->GetRenderer(this, row, col);
65e4e78e
VZ
8442 if ( renderer )
8443 {
af547d51
VZ
8444 wxSize size = renderer->GetBestSize(*this, *attr, dc, row, col);
8445 extent = column ? size.x : size.y;
774f6b31
VZ
8446
8447 if ( span != CellSpan_None )
8448 {
8449 // we spread the size of a spanning cell over all the cells it
8450 // covers evenly -- this is probably not ideal but we can't
8451 // really do much better here
8452 //
8453 // notice that numCols and numRows are never 0 as they
8454 // correspond to the size of the main cell of the span and not
8455 // of the cell inside it
8456 extent /= column ? numCols : numRows;
8457 }
8458
af547d51 8459 if ( extent > extentMax )
af547d51 8460 extentMax = extent;
0b190b0f
VZ
8461
8462 renderer->DecRef();
65e4e78e
VZ
8463 }
8464
8465 attr->DecRef();
8466 }
8467
af547d51
VZ
8468 // now also compare with the column label extent
8469 wxCoord w, h;
65e4e78e 8470 dc.SetFont( GetLabelFont() );
294d195c
MB
8471
8472 if ( column )
d43851f7 8473 {
bc7d2e9d 8474 dc.GetMultiLineTextExtent( GetColLabelValue(colOrRow), &w, &h );
4db6714b 8475 if ( GetColLabelTextOrientation() == wxVERTICAL )
d43851f7
JS
8476 w = h;
8477 }
294d195c 8478 else
bc7d2e9d 8479 dc.GetMultiLineTextExtent( GetRowLabelValue(colOrRow), &w, &h );
294d195c 8480
af547d51
VZ
8481 extent = column ? w : h;
8482 if ( extent > extentMax )
af547d51 8483 extentMax = extent;
65e4e78e 8484
af547d51 8485 if ( !extentMax )
65e4e78e 8486 {
af547d51 8487 // empty column - give default extent (notice that if extentMax is less
2f024384 8488 // than default extent but != 0, it's OK)
af547d51 8489 extentMax = column ? m_defaultColWidth : m_defaultRowHeight;
65e4e78e
VZ
8490 }
8491 else
8492 {
a95e38c0 8493 if ( column )
a95e38c0
VZ
8494 // leave some space around text
8495 extentMax += 10;
f6bcfd97 8496 else
f6bcfd97 8497 extentMax += 6;
65e4e78e
VZ
8498 }
8499
edb89f7e
VZ
8500 if ( column )
8501 {
a01cfc08
VS
8502 // Ensure automatic width is not less than minimal width. See the
8503 // comment in SetColSize() for explanation of why this isn't done
8504 // in SetColSize().
8505 if ( !setAsMin )
bc7d2e9d 8506 extentMax = wxMax(extentMax, GetColMinimalWidth(colOrRow));
a01cfc08 8507
bc7d2e9d 8508 SetColSize( colOrRow, extentMax );
faec5a43
SN
8509 if ( !GetBatchCount() )
8510 {
ad805b9e
VZ
8511 if ( m_useNativeHeader )
8512 {
bc7d2e9d 8513 GetGridColHeader()->UpdateColumn(colOrRow);
ad805b9e
VZ
8514 }
8515 else
8516 {
8517 int cw, ch, dummy;
8518 m_gridWin->GetClientSize( &cw, &ch );
bc7d2e9d 8519 wxRect rect ( CellToRect( 0, colOrRow ) );
ad805b9e
VZ
8520 rect.y = 0;
8521 CalcScrolledPosition(rect.x, 0, &rect.x, &dummy);
8522 rect.width = cw - rect.x;
8523 rect.height = m_colLabelHeight;
8524 GetColLabelWindow()->Refresh( true, &rect );
8525 }
edb89f7e
VZ
8526 }
8527 }
8528 else
8529 {
a01cfc08
VS
8530 // Ensure automatic width is not less than minimal height. See the
8531 // comment in SetColSize() for explanation of why this isn't done
8532 // in SetRowSize().
8533 if ( !setAsMin )
bc7d2e9d 8534 extentMax = wxMax(extentMax, GetRowMinimalHeight(colOrRow));
a01cfc08 8535
bc7d2e9d 8536 SetRowSize(colOrRow, extentMax);
faec5a43
SN
8537 if ( !GetBatchCount() )
8538 {
edb89f7e
VZ
8539 int cw, ch, dummy;
8540 m_gridWin->GetClientSize( &cw, &ch );
bc7d2e9d 8541 wxRect rect( CellToRect( colOrRow, 0 ) );
edb89f7e
VZ
8542 rect.x = 0;
8543 CalcScrolledPosition(0, rect.y, &dummy, &rect.y);
8544 rect.width = m_rowLabelWidth;
faec5a43 8545 rect.height = ch - rect.y;
ca65c044 8546 m_rowLabelWin->Refresh( true, &rect );
edb89f7e 8547 }
faec5a43 8548 }
2f024384 8549
65e4e78e
VZ
8550 if ( setAsMin )
8551 {
af547d51 8552 if ( column )
bc7d2e9d 8553 SetColMinimalWidth(colOrRow, extentMax);
af547d51 8554 else
bc7d2e9d 8555 SetRowMinimalHeight(colOrRow, extentMax);
65e4e78e
VZ
8556 }
8557}
8558
733f486a
VZ
8559wxCoord wxGrid::CalcColOrRowLabelAreaMinSize(wxGridDirection direction)
8560{
8561 // calculate size for the rows or columns?
8562 const bool calcRows = direction == wxGRID_ROW;
8563
8564 wxClientDC dc(calcRows ? GetGridRowLabelWindow()
8565 : GetGridColLabelWindow());
8566 dc.SetFont(GetLabelFont());
8567
8568 // which dimension should we take into account for calculations?
8569 //
8570 // for columns, the text can be only horizontal so it's easy but for rows
8571 // we also have to take into account the text orientation
8572 const bool
8573 useWidth = calcRows || (GetColLabelTextOrientation() == wxVERTICAL);
8574
8575 wxArrayString lines;
8576 wxCoord extentMax = 0;
8577
8578 const int numRowsOrCols = calcRows ? m_numRows : m_numCols;
8579 for ( int rowOrCol = 0; rowOrCol < numRowsOrCols; rowOrCol++ )
8580 {
8581 lines.Clear();
add4bb40
VS
8582
8583 wxString label = calcRows ? GetRowLabelValue(rowOrCol)
8584 : GetColLabelValue(rowOrCol);
8585 StringToLines(label, lines);
733f486a
VZ
8586
8587 long w, h;
8588 GetTextBoxSize(dc, lines, &w, &h);
8589
8590 const wxCoord extent = useWidth ? w : h;
8591 if ( extent > extentMax )
8592 extentMax = extent;
8593 }
8594
8595 if ( !extentMax )
8596 {
8597 // empty column - give default extent (notice that if extentMax is less
8598 // than default extent but != 0, it's OK)
8599 extentMax = calcRows ? GetDefaultRowLabelSize()
8600 : GetDefaultColLabelSize();
8601 }
8602
8603 // leave some space around text (taken from AutoSizeColOrRow)
8604 if ( calcRows )
8605 extentMax += 10;
8606 else
8607 extentMax += 6;
8608
8609 return extentMax;
8610}
8611
266e8367 8612int wxGrid::SetOrCalcColumnSizes(bool calcOnly, bool setAsMin)
65e4e78e 8613{
57c086ef
VZ
8614 int width = m_rowLabelWidth;
8615
b62f94ff
VZ
8616 wxGridUpdateLocker locker;
8617 if(!calcOnly)
8618 locker.Create(this);
97a9929e 8619
65e4e78e
VZ
8620 for ( int col = 0; col < m_numCols; col++ )
8621 {
266e8367 8622 if ( !calcOnly )
266e8367 8623 AutoSizeColumn(col, setAsMin);
57c086ef
VZ
8624
8625 width += GetColWidth(col);
65e4e78e 8626 }
97a9929e 8627
266e8367 8628 return width;
65e4e78e
VZ
8629}
8630
266e8367 8631int wxGrid::SetOrCalcRowSizes(bool calcOnly, bool setAsMin)
57c086ef
VZ
8632{
8633 int height = m_colLabelHeight;
8634
b62f94ff
VZ
8635 wxGridUpdateLocker locker;
8636 if(!calcOnly)
8637 locker.Create(this);
97a9929e 8638
57c086ef
VZ
8639 for ( int row = 0; row < m_numRows; row++ )
8640 {
af547d51 8641 if ( !calcOnly )
af547d51 8642 AutoSizeRow(row, setAsMin);
57c086ef
VZ
8643
8644 height += GetRowHeight(row);
8645 }
97a9929e 8646
266e8367 8647 return height;
57c086ef
VZ
8648}
8649
8650void wxGrid::AutoSize()
8651{
b62f94ff 8652 wxGridUpdateLocker locker(this);
97a9929e 8653
0ed3b812
VZ
8654 wxSize size(SetOrCalcColumnSizes(false) - m_rowLabelWidth + m_extraWidth,
8655 SetOrCalcRowSizes(false) - m_colLabelHeight + m_extraHeight);
97a9929e 8656
0ed3b812
VZ
8657 // we know that we're not going to have scrollbars so disable them now to
8658 // avoid trouble in SetClientSize() which can otherwise set the correct
8659 // client size but also leave space for (not needed any more) scrollbars
fd76c6a7
VZ
8660 SetScrollbars(m_xScrollPixelsPerLine, m_yScrollPixelsPerLine,
8661 0, 0, 0, 0, true);
72b0a1de
VZ
8662
8663 SetClientSize(size.x + m_rowLabelWidth, size.y + m_colLabelHeight);
266e8367
VZ
8664}
8665
d43851f7
JS
8666void wxGrid::AutoSizeRowLabelSize( int row )
8667{
d43851f7 8668 // Hide the edit control, so it
4db6714b
KH
8669 // won't interfere with drag-shrinking.
8670 if ( IsCellEditControlShown() )
d43851f7
JS
8671 {
8672 HideCellEditControl();
8673 SaveEditControlValue();
8674 }
8675
8676 // autosize row height depending on label text
ad7502d8 8677 SetRowSize(row, -1);
30496905 8678
d43851f7
JS
8679 ForceRefresh();
8680}
8681
8682void wxGrid::AutoSizeColLabelSize( int col )
8683{
d43851f7 8684 // Hide the edit control, so it
c2f5b920 8685 // won't interfere with drag-shrinking.
4db6714b 8686 if ( IsCellEditControlShown() )
d43851f7
JS
8687 {
8688 HideCellEditControl();
8689 SaveEditControlValue();
8690 }
8691
8692 // autosize column width depending on label text
ad7502d8 8693 SetColSize(col, -1);
30496905 8694
d43851f7
JS
8695 ForceRefresh();
8696}
8697
266e8367
VZ
8698wxSize wxGrid::DoGetBestSize() const
8699{
f48a1159 8700 wxGrid * const self = const_cast<wxGrid *>(this);
266e8367 8701
dc4689ef
VZ
8702 // we do the same as in AutoSize() here with the exception that we don't
8703 // change the column/row sizes, only calculate them
8704 wxSize size(self->SetOrCalcColumnSizes(true) - m_rowLabelWidth + m_extraWidth,
8705 self->SetOrCalcRowSizes(true) - m_colLabelHeight + m_extraHeight);
962a48f6 8706
72b0a1de 8707 return wxSize(size.x + m_rowLabelWidth, size.y + m_colLabelHeight)
dc4689ef 8708 + GetWindowBorderSize();
266e8367
VZ
8709}
8710
8711void wxGrid::Fit()
8712{
8713 AutoSize();
57c086ef
VZ
8714}
8715
7bc57fd9 8716#if WXWIN_COMPATIBILITY_2_8
6fc0f38f
SN
8717wxPen& wxGrid::GetDividerPen() const
8718{
8719 return wxNullPen;
8720}
7bc57fd9 8721#endif // WXWIN_COMPATIBILITY_2_8
6fc0f38f 8722
57c086ef
VZ
8723// ----------------------------------------------------------------------------
8724// cell value accessor functions
8725// ----------------------------------------------------------------------------
f85afd4e
MB
8726
8727void wxGrid::SetCellValue( int row, int col, const wxString& s )
8728{
8729 if ( m_table )
8730 {
f6bcfd97 8731 m_table->SetValue( row, col, s );
2d66e025
MB
8732 if ( !GetBatchCount() )
8733 {
27f35b66
SN
8734 int dummy;
8735 wxRect rect( CellToRect( row, col ) );
8736 rect.x = 0;
8737 rect.width = m_gridWin->GetClientSize().GetWidth();
8738 CalcScrolledPosition(0, rect.y, &dummy, &rect.y);
ca65c044 8739 m_gridWin->Refresh( false, &rect );
2d66e025 8740 }
60ff3b99 8741
f85afd4e 8742 if ( m_currentCellCoords.GetRow() == row &&
4cfa5de6 8743 m_currentCellCoords.GetCol() == col &&
f6bcfd97
BP
8744 IsCellEditControlShown())
8745 // Note: If we are using IsCellEditControlEnabled,
8746 // this interacts badly with calling SetCellValue from
8747 // an EVT_GRID_CELL_CHANGE handler.
f85afd4e 8748 {
4cfa5de6
RD
8749 HideCellEditControl();
8750 ShowCellEditControl(); // will reread data from table
f85afd4e 8751 }
f85afd4e
MB
8752 }
8753}
8754
962a48f6 8755// ----------------------------------------------------------------------------
2f024384 8756// block, row and column selection
962a48f6 8757// ----------------------------------------------------------------------------
f85afd4e
MB
8758
8759void wxGrid::SelectRow( int row, bool addToSelected )
8760{
8b5f6d9d
VZ
8761 if ( !m_selection )
8762 return;
8763
8764 if ( !addToSelected )
e32352cf 8765 ClearSelection();
70c7a608 8766
8b5f6d9d 8767 m_selection->SelectRow(row);
f85afd4e
MB
8768}
8769
f85afd4e
MB
8770void wxGrid::SelectCol( int col, bool addToSelected )
8771{
8b5f6d9d
VZ
8772 if ( !m_selection )
8773 return;
8774
8775 if ( !addToSelected )
e32352cf 8776 ClearSelection();
f85afd4e 8777
8b5f6d9d 8778 m_selection->SelectCol(col);
f85afd4e
MB
8779}
8780
8b5f6d9d
VZ
8781void wxGrid::SelectBlock(int topRow, int leftCol, int bottomRow, int rightCol,
8782 bool addToSelected)
f85afd4e 8783{
8b5f6d9d
VZ
8784 if ( !m_selection )
8785 return;
8786
8787 if ( !addToSelected )
c9097836 8788 ClearSelection();
f85afd4e 8789
8b5f6d9d 8790 m_selection->SelectBlock(topRow, leftCol, bottomRow, rightCol);
f85afd4e
MB
8791}
8792
8793void wxGrid::SelectAll()
8794{
f74d0b57 8795 if ( m_numRows > 0 && m_numCols > 0 )
3f3dc2ef
VZ
8796 {
8797 if ( m_selection )
ccdee36f 8798 m_selection->SelectBlock( 0, 0, m_numRows - 1, m_numCols - 1 );
3f3dc2ef 8799 }
b5808881 8800}
f85afd4e 8801
962a48f6
DS
8802// ----------------------------------------------------------------------------
8803// cell, row and col deselection
8804// ----------------------------------------------------------------------------
f7b4b343 8805
bec70262 8806void wxGrid::DeselectLine(int line, const wxGridOperations& oper)
f7b4b343 8807{
3f3dc2ef
VZ
8808 if ( !m_selection )
8809 return;
8810
bec70262 8811 const wxGridSelectionModes mode = m_selection->GetSelectionMode();
475be9ce
VZ
8812 if ( mode == oper.GetSelectionMode() ||
8813 mode == wxGrid::wxGridSelectRowsOrColumns )
f7b4b343 8814 {
bec70262
VZ
8815 const wxGridCellCoords c(oper.MakeCoords(line, 0));
8816 if ( m_selection->IsInSelection(c) )
8817 m_selection->ToggleCellSelection(c);
ffdd3c98 8818 }
bec70262 8819 else if ( mode != oper.Dual().GetSelectionMode() )
f7b4b343 8820 {
bec70262
VZ
8821 const int nOther = oper.Dual().GetNumberOfLines(this);
8822 for ( int i = 0; i < nOther; i++ )
f7b4b343 8823 {
bec70262
VZ
8824 const wxGridCellCoords c(oper.MakeCoords(line, i));
8825 if ( m_selection->IsInSelection(c) )
8826 m_selection->ToggleCellSelection(c);
f7b4b343
VZ
8827 }
8828 }
bec70262
VZ
8829 //else: can only select orthogonal lines so no lines in this direction
8830 // could have been selected anyhow
f7b4b343
VZ
8831}
8832
bec70262 8833void wxGrid::DeselectRow(int row)
f7b4b343 8834{
bec70262
VZ
8835 DeselectLine(row, wxGridRowOperations());
8836}
3f3dc2ef 8837
bec70262
VZ
8838void wxGrid::DeselectCol(int col)
8839{
8840 DeselectLine(col, wxGridColumnOperations());
f7b4b343
VZ
8841}
8842
8843void wxGrid::DeselectCell( int row, int col )
8844{
3f3dc2ef 8845 if ( m_selection && m_selection->IsInSelection(row, col) )
f7b4b343
VZ
8846 m_selection->ToggleCellSelection(row, col);
8847}
8848
ef316e23 8849bool wxGrid::IsSelection() const
b5808881 8850{
3f3dc2ef 8851 return ( m_selection && (m_selection->IsSelection() ||
8a3e536c
VZ
8852 ( m_selectedBlockTopLeft != wxGridNoCellCoords &&
8853 m_selectedBlockBottomRight != wxGridNoCellCoords) ) );
f85afd4e
MB
8854}
8855
84035150 8856bool wxGrid::IsInSelection( int row, int col ) const
b5808881 8857{
3f3dc2ef 8858 return ( m_selection && (m_selection->IsInSelection( row, col ) ||
8a3e536c
VZ
8859 ( row >= m_selectedBlockTopLeft.GetRow() &&
8860 col >= m_selectedBlockTopLeft.GetCol() &&
8861 row <= m_selectedBlockBottomRight.GetRow() &&
8862 col <= m_selectedBlockBottomRight.GetCol() )) );
b5808881 8863}
f85afd4e 8864
aa5b8857
SN
8865wxGridCellCoordsArray wxGrid::GetSelectedCells() const
8866{
4db6714b
KH
8867 if (!m_selection)
8868 {
8869 wxGridCellCoordsArray a;
8870 return a;
8871 }
8872
aa5b8857
SN
8873 return m_selection->m_cellSelection;
8874}
4db6714b 8875
aa5b8857
SN
8876wxGridCellCoordsArray wxGrid::GetSelectionBlockTopLeft() const
8877{
4db6714b
KH
8878 if (!m_selection)
8879 {
8880 wxGridCellCoordsArray a;
8881 return a;
8882 }
8883
aa5b8857
SN
8884 return m_selection->m_blockSelectionTopLeft;
8885}
4db6714b 8886
aa5b8857
SN
8887wxGridCellCoordsArray wxGrid::GetSelectionBlockBottomRight() const
8888{
4db6714b
KH
8889 if (!m_selection)
8890 {
8891 wxGridCellCoordsArray a;
8892 return a;
8893 }
8894
a8de8190 8895 return m_selection->m_blockSelectionBottomRight;
aa5b8857 8896}
4db6714b 8897
aa5b8857
SN
8898wxArrayInt wxGrid::GetSelectedRows() const
8899{
4db6714b
KH
8900 if (!m_selection)
8901 {
8902 wxArrayInt a;
8903 return a;
8904 }
8905
aa5b8857
SN
8906 return m_selection->m_rowSelection;
8907}
4db6714b 8908
aa5b8857
SN
8909wxArrayInt wxGrid::GetSelectedCols() const
8910{
4db6714b
KH
8911 if (!m_selection)
8912 {
8913 wxArrayInt a;
8914 return a;
8915 }
8916
aa5b8857
SN
8917 return m_selection->m_colSelection;
8918}
8919
f85afd4e
MB
8920void wxGrid::ClearSelection()
8921{
8a3e536c
VZ
8922 wxRect r1 = BlockToDeviceRect(m_selectedBlockTopLeft,
8923 m_selectedBlockBottomRight);
8924 wxRect r2 = BlockToDeviceRect(m_currentCellCoords,
8925 m_selectedBlockCorner);
8926
8927 m_selectedBlockTopLeft =
8928 m_selectedBlockBottomRight =
8929 m_selectedBlockCorner = wxGridNoCellCoords;
8930
efc49a21
VZ
8931 if ( !r1.IsEmpty() )
8932 RefreshRect(r1, false);
8933 if ( !r2.IsEmpty() )
8934 RefreshRect(r2, false);
8a3e536c 8935
3f3dc2ef
VZ
8936 if ( m_selection )
8937 m_selection->ClearSelection();
8f177c8e 8938}
f85afd4e 8939
da6af900 8940// This function returns the rectangle that encloses the given block
2d66e025
MB
8941// in device coords clipped to the client size of the grid window.
8942//
731330ec
VZ
8943wxRect wxGrid::BlockToDeviceRect( const wxGridCellCoords& topLeft,
8944 const wxGridCellCoords& bottomRight ) const
f85afd4e 8945{
731330ec
VZ
8946 wxRect resultRect;
8947 wxRect tempCellRect = CellToRect(topLeft);
8948 if ( tempCellRect != wxGridNoCellRect )
f85afd4e 8949 {
731330ec 8950 resultRect = tempCellRect;
58dd5b3b
MB
8951 }
8952 else
8953 {
731330ec 8954 resultRect = wxRect(0, 0, 0, 0);
58dd5b3b 8955 }
60ff3b99 8956
731330ec
VZ
8957 tempCellRect = CellToRect(bottomRight);
8958 if ( tempCellRect != wxGridNoCellRect )
58dd5b3b 8959 {
731330ec 8960 resultRect += tempCellRect;
2d66e025
MB
8961 }
8962 else
8963 {
731330ec 8964 // If both inputs were "wxGridNoCellRect," then there's nothing to do.
2d66e025 8965 return wxGridNoCellRect;
f85afd4e
MB
8966 }
8967
731330ec
VZ
8968 // Ensure that left/right and top/bottom pairs are in order.
8969 int left = resultRect.GetLeft();
8970 int top = resultRect.GetTop();
8971 int right = resultRect.GetRight();
8972 int bottom = resultRect.GetBottom();
27f35b66
SN
8973
8974 int leftCol = topLeft.GetCol();
8975 int topRow = topLeft.GetRow();
8976 int rightCol = bottomRight.GetCol();
8977 int bottomRow = bottomRight.GetRow();
8978
3ed884a0
SN
8979 if (left > right)
8980 {
731330ec 8981 int tmp = left;
3ed884a0 8982 left = right;
731330ec
VZ
8983 right = tmp;
8984
8985 tmp = leftCol;
4db6714b 8986 leftCol = rightCol;
731330ec 8987 rightCol = tmp;
3ed884a0
SN
8988 }
8989
8990 if (top > bottom)
8991 {
731330ec 8992 int tmp = top;
3ed884a0 8993 top = bottom;
731330ec
VZ
8994 bottom = tmp;
8995
8996 tmp = topRow;
3ed884a0 8997 topRow = bottomRow;
731330ec 8998 bottomRow = tmp;
3ed884a0
SN
8999 }
9000
731330ec
VZ
9001 // The following loop is ONLY necessary to detect and handle merged cells.
9002 int cw, ch;
9003 m_gridWin->GetClientSize( &cw, &ch );
9004
9005 // Get the origin coordinates: notice that they will be negative if the
9006 // grid is scrolled downwards/to the right.
9007 int gridOriginX = 0;
9008 int gridOriginY = 0;
9009 CalcScrolledPosition(gridOriginX, gridOriginY, &gridOriginX, &gridOriginY);
9010
9011 int onScreenLeftmostCol = internalXToCol(-gridOriginX);
9012 int onScreenUppermostRow = internalYToRow(-gridOriginY);
9013
9014 int onScreenRightmostCol = internalXToCol(-gridOriginX + cw);
9015 int onScreenBottommostRow = internalYToRow(-gridOriginY + ch);
9016
9017 // Bound our loop so that we only examine the portion of the selected block
9018 // that is shown on screen. Therefore, we compare the Top-Left block values
9019 // to the Top-Left screen values, and the Bottom-Right block values to the
9020 // Bottom-Right screen values, choosing appropriately.
9021 const int visibleTopRow = wxMax(topRow, onScreenUppermostRow);
9022 const int visibleBottomRow = wxMin(bottomRow, onScreenBottommostRow);
9023 const int visibleLeftCol = wxMax(leftCol, onScreenLeftmostCol);
9024 const int visibleRightCol = wxMin(rightCol, onScreenRightmostCol);
9025
9026 for ( int j = visibleTopRow; j <= visibleBottomRow; j++ )
27f35b66 9027 {
731330ec 9028 for ( int i = visibleLeftCol; i <= visibleRightCol; i++ )
27f35b66 9029 {
731330ec
VZ
9030 if ( (j == visibleTopRow) || (j == visibleBottomRow) ||
9031 (i == visibleLeftCol) || (i == visibleRightCol) )
27f35b66 9032 {
731330ec 9033 tempCellRect = CellToRect( j, i );
27f35b66 9034
731330ec
VZ
9035 if (tempCellRect.x < left)
9036 left = tempCellRect.x;
9037 if (tempCellRect.y < top)
9038 top = tempCellRect.y;
9039 if (tempCellRect.x + tempCellRect.width > right)
9040 right = tempCellRect.x + tempCellRect.width;
9041 if (tempCellRect.y + tempCellRect.height > bottom)
9042 bottom = tempCellRect.y + tempCellRect.height;
27f35b66 9043 }
4db6714b
KH
9044 else
9045 {
731330ec 9046 i = visibleRightCol; // jump over inner cells.
4db6714b 9047 }
27f35b66
SN
9048 }
9049 }
9050
731330ec 9051 // Convert to scrolled coords
27f35b66
SN
9052 CalcScrolledPosition( left, top, &left, &top );
9053 CalcScrolledPosition( right, bottom, &right, &bottom );
58dd5b3b 9054
f6bcfd97 9055 if (right < 0 || bottom < 0 || left > cw || top > ch)
c47addef 9056 return wxRect(0,0,0,0);
f6bcfd97 9057
731330ec
VZ
9058 resultRect.SetLeft( wxMax(0, left) );
9059 resultRect.SetTop( wxMax(0, top) );
9060 resultRect.SetRight( wxMin(cw, right) );
9061 resultRect.SetBottom( wxMin(ch, bottom) );
58dd5b3b 9062
731330ec 9063 return resultRect;
f85afd4e
MB
9064}
9065
574e1c5a
VZ
9066void wxGrid::DoSetSizes(const wxGridSizesInfo& sizeInfo,
9067 const wxGridOperations& oper)
9068{
9069 BeginBatch();
9070 oper.SetDefaultLineSize(this, sizeInfo.m_sizeDefault, true);
9071 const int numLines = oper.GetNumberOfLines(this);
9072 for ( int i = 0; i < numLines; i++ )
9073 {
9074 int size = sizeInfo.GetSize(i);
9075 if ( size != sizeInfo.m_sizeDefault)
9076 oper.SetLineSize(this, i, size);
9077 }
9078 EndBatch();
9079}
9080
9081void wxGrid::SetColSizes(const wxGridSizesInfo& sizeInfo)
9082{
9083 DoSetSizes(sizeInfo, wxGridColumnOperations());
9084}
9085
9086void wxGrid::SetRowSizes(const wxGridSizesInfo& sizeInfo)
9087{
9088 DoSetSizes(sizeInfo, wxGridRowOperations());
9089}
9090
9091wxGridSizesInfo::wxGridSizesInfo(int defSize, const wxArrayInt& allSizes)
9092{
9093 m_sizeDefault = defSize;
9094 for ( size_t i = 0; i < allSizes.size(); i++ )
9095 {
9096 if ( allSizes[i] != defSize )
9097 m_customSizes[i] = allSizes[i];
9098 }
9099}
9100
9101int wxGridSizesInfo::GetSize(unsigned pos) const
9102{
9103 wxUnsignedToIntHashMap::const_iterator it = m_customSizes.find(pos);
9104
30496905
VZ
9105 // if it's not found return the default
9106 if ( it == m_customSizes.end() )
9107 return m_sizeDefault;
9108
9109 // otherwise return 0 if it's hidden, currently there is no way to get
9110 // its size before it had been hidden
9111 if ( it->second < 0 )
9112 return 0;
9113
9114 return it->second;
574e1c5a
VZ
9115}
9116
1edce33f
VZ
9117// ----------------------------------------------------------------------------
9118// drop target
9119// ----------------------------------------------------------------------------
9120
9121#if wxUSE_DRAG_AND_DROP
9122
9123// this allow setting drop target directly on wxGrid
9124void wxGrid::SetDropTarget(wxDropTarget *dropTarget)
9125{
9126 GetGridWindow()->SetDropTarget(dropTarget);
9127}
9128
9129#endif // wxUSE_DRAG_AND_DROP
9130
962a48f6
DS
9131// ----------------------------------------------------------------------------
9132// grid event classes
9133// ----------------------------------------------------------------------------
f85afd4e 9134
bf7945ce 9135IMPLEMENT_DYNAMIC_CLASS( wxGridEvent, wxNotifyEvent )
f85afd4e
MB
9136
9137wxGridEvent::wxGridEvent( int id, wxEventType type, wxObject* obj,
5c8fc7c1 9138 int row, int col, int x, int y, bool sel,
f85afd4e 9139 bool control, bool shift, bool alt, bool meta )
8b5f6d9d
VZ
9140 : wxNotifyEvent( type, id ),
9141 wxKeyboardState(control, shift, alt, meta)
f85afd4e 9142{
8b5f6d9d 9143 Init(row, col, x, y, sel);
8f177c8e 9144
f85afd4e
MB
9145 SetEventObject(obj);
9146}
9147
bf7945ce 9148IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent, wxNotifyEvent )
f85afd4e
MB
9149
9150wxGridSizeEvent::wxGridSizeEvent( int id, wxEventType type, wxObject* obj,
9151 int rowOrCol, int x, int y,
9152 bool control, bool shift, bool alt, bool meta )
8b5f6d9d
VZ
9153 : wxNotifyEvent( type, id ),
9154 wxKeyboardState(control, shift, alt, meta)
f85afd4e 9155{
8b5f6d9d 9156 Init(rowOrCol, x, y);
8f177c8e 9157
f85afd4e
MB
9158 SetEventObject(obj);
9159}
9160
9161
bf7945ce 9162IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent, wxNotifyEvent )
f85afd4e
MB
9163
9164wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id, wxEventType type, wxObject* obj,
8f177c8e
VZ
9165 const wxGridCellCoords& topLeft,
9166 const wxGridCellCoords& bottomRight,
5c8fc7c1
SN
9167 bool sel, bool control,
9168 bool shift, bool alt, bool meta )
8b5f6d9d
VZ
9169 : wxNotifyEvent( type, id ),
9170 wxKeyboardState(control, shift, alt, meta)
9171{
9172 Init(topLeft, bottomRight, sel);
f85afd4e
MB
9173
9174 SetEventObject(obj);
9175}
9176
9177
bf7945ce
RD
9178IMPLEMENT_DYNAMIC_CLASS(wxGridEditorCreatedEvent, wxCommandEvent)
9179
9180wxGridEditorCreatedEvent::wxGridEditorCreatedEvent(int id, wxEventType type,
9181 wxObject* obj, int row,
9182 int col, wxControl* ctrl)
9183 : wxCommandEvent(type, id)
9184{
9185 SetEventObject(obj);
9186 m_row = row;
9187 m_col = col;
9188 m_ctrl = ctrl;
9189}
9190
29efc6e4
FM
9191
9192// ----------------------------------------------------------------------------
9193// wxGridTypeRegistry
9194// ----------------------------------------------------------------------------
9195
9196wxGridTypeRegistry::~wxGridTypeRegistry()
9197{
9198 size_t count = m_typeinfo.GetCount();
9199 for ( size_t i = 0; i < count; i++ )
9200 delete m_typeinfo[i];
9201}
9202
9203void wxGridTypeRegistry::RegisterDataType(const wxString& typeName,
9204 wxGridCellRenderer* renderer,
9205 wxGridCellEditor* editor)
9206{
9207 wxGridDataTypeInfo* info = new wxGridDataTypeInfo(typeName, renderer, editor);
9208
9209 // is it already registered?
9210 int loc = FindRegisteredDataType(typeName);
9211 if ( loc != wxNOT_FOUND )
9212 {
9213 delete m_typeinfo[loc];
9214 m_typeinfo[loc] = info;
9215 }
9216 else
9217 {
9218 m_typeinfo.Add(info);
9219 }
9220}
9221
9222int wxGridTypeRegistry::FindRegisteredDataType(const wxString& typeName)
9223{
9224 size_t count = m_typeinfo.GetCount();
9225 for ( size_t i = 0; i < count; i++ )
9226 {
9227 if ( typeName == m_typeinfo[i]->m_typeName )
9228 {
9229 return i;
9230 }
9231 }
9232
9233 return wxNOT_FOUND;
9234}
9235
9236int wxGridTypeRegistry::FindDataType(const wxString& typeName)
9237{
9238 int index = FindRegisteredDataType(typeName);
9239 if ( index == wxNOT_FOUND )
9240 {
9241 // check whether this is one of the standard ones, in which case
9242 // register it "on the fly"
9243#if wxUSE_TEXTCTRL
9244 if ( typeName == wxGRID_VALUE_STRING )
9245 {
9246 RegisterDataType(wxGRID_VALUE_STRING,
9247 new wxGridCellStringRenderer,
9248 new wxGridCellTextEditor);
9249 }
9250 else
9251#endif // wxUSE_TEXTCTRL
9252#if wxUSE_CHECKBOX
9253 if ( typeName == wxGRID_VALUE_BOOL )
9254 {
9255 RegisterDataType(wxGRID_VALUE_BOOL,
9256 new wxGridCellBoolRenderer,
9257 new wxGridCellBoolEditor);
9258 }
9259 else
9260#endif // wxUSE_CHECKBOX
9261#if wxUSE_TEXTCTRL
9262 if ( typeName == wxGRID_VALUE_NUMBER )
9263 {
9264 RegisterDataType(wxGRID_VALUE_NUMBER,
9265 new wxGridCellNumberRenderer,
9266 new wxGridCellNumberEditor);
9267 }
9268 else if ( typeName == wxGRID_VALUE_FLOAT )
9269 {
9270 RegisterDataType(wxGRID_VALUE_FLOAT,
9271 new wxGridCellFloatRenderer,
9272 new wxGridCellFloatEditor);
9273 }
9274 else
9275#endif // wxUSE_TEXTCTRL
9276#if wxUSE_COMBOBOX
9277 if ( typeName == wxGRID_VALUE_CHOICE )
9278 {
9279 RegisterDataType(wxGRID_VALUE_CHOICE,
9280 new wxGridCellStringRenderer,
9281 new wxGridCellChoiceEditor);
9282 }
9283 else
9284#endif // wxUSE_COMBOBOX
9285 {
9286 return wxNOT_FOUND;
9287 }
9288
9289 // we get here only if just added the entry for this type, so return
9290 // the last index
9291 index = m_typeinfo.GetCount() - 1;
9292 }
9293
9294 return index;
9295}
9296
9297int wxGridTypeRegistry::FindOrCloneDataType(const wxString& typeName)
9298{
9299 int index = FindDataType(typeName);
9300 if ( index == wxNOT_FOUND )
9301 {
9302 // the first part of the typename is the "real" type, anything after ':'
9303 // are the parameters for the renderer
9a83f860 9304 index = FindDataType(typeName.BeforeFirst(wxT(':')));
29efc6e4
FM
9305 if ( index == wxNOT_FOUND )
9306 {
9307 return wxNOT_FOUND;
9308 }
9309
9310 wxGridCellRenderer *renderer = GetRenderer(index);
9311 wxGridCellRenderer *rendererOld = renderer;
9312 renderer = renderer->Clone();
9313 rendererOld->DecRef();
9314
9315 wxGridCellEditor *editor = GetEditor(index);
9316 wxGridCellEditor *editorOld = editor;
9317 editor = editor->Clone();
9318 editorOld->DecRef();
9319
9320 // do it even if there are no parameters to reset them to defaults
9a83f860 9321 wxString params = typeName.AfterFirst(wxT(':'));
29efc6e4
FM
9322 renderer->SetParameters(params);
9323 editor->SetParameters(params);
9324
9325 // register the new typename
9326 RegisterDataType(typeName, renderer, editor);
9327
9328 // we just registered it, it's the last one
9329 index = m_typeinfo.GetCount() - 1;
9330 }
9331
9332 return index;
9333}
9334
9335wxGridCellRenderer* wxGridTypeRegistry::GetRenderer(int index)
9336{
9337 wxGridCellRenderer* renderer = m_typeinfo[index]->m_renderer;
9338 if (renderer)
9339 renderer->IncRef();
9340
9341 return renderer;
9342}
9343
9344wxGridCellEditor* wxGridTypeRegistry::GetEditor(int index)
9345{
9346 wxGridCellEditor* editor = m_typeinfo[index]->m_editor;
9347 if (editor)
9348 editor->IncRef();
9349
9350 return editor;
9351}
9352
27b92ca4 9353#endif // wxUSE_GRID