]> git.saurik.com Git - wxWidgets.git/blame - src/generic/grid.cpp
Ensure there is valid context for DrawRectangle
[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 )
f85afd4e 2162END_EVENT_TABLE()
8f177c8e 2163
b0a877ec
SC
2164bool wxGrid::Create(wxWindow *parent, wxWindowID id,
2165 const wxPoint& pos, const wxSize& size,
2166 long style, const wxString& name)
2167{
2168 if (!wxScrolledWindow::Create(parent, id, pos, size,
c2f5b920 2169 style | wxWANTS_CHARS, name))
ca65c044 2170 return false;
b0a877ec 2171
2f024384
DS
2172 m_colMinWidths = wxLongToLongHashMap(GRID_HASH_SIZE);
2173 m_rowMinHeights = wxLongToLongHashMap(GRID_HASH_SIZE);
b0a877ec 2174
2f024384 2175 Create();
170acdc9 2176 SetInitialSize(size);
b93aafab 2177 CalcDimensions();
b0a877ec 2178
ca65c044 2179 return true;
b0a877ec
SC
2180}
2181
58dd5b3b
MB
2182wxGrid::~wxGrid()
2183{
e882d288
VZ
2184 if ( m_winCapture )
2185 m_winCapture->ReleaseMouse();
2186
b09b3048
VZ
2187 // Ensure that the editor control is destroyed before the grid is,
2188 // otherwise we crash later when the editor tries to do something with the
2189 // half destroyed grid
2190 HideCellEditControl();
2191
606b005f
JS
2192 // Must do this or ~wxScrollHelper will pop the wrong event handler
2193 SetTargetWindow(this);
0a976765 2194 ClearAttrCache();
39bcce60 2195 wxSafeDecRef(m_defaultCellAttr);
0a976765
VZ
2196
2197#ifdef DEBUG_ATTR_CACHE
2198 size_t total = gs_nAttrCacheHits + gs_nAttrCacheMisses;
9a83f860 2199 wxPrintf(wxT("wxGrid attribute cache statistics: "
0a976765
VZ
2200 "total: %u, hits: %u (%u%%)\n"),
2201 total, gs_nAttrCacheHits,
2202 total ? (gs_nAttrCacheHits*100) / total : 0);
2203#endif
2204
86020f7e
VZ
2205 // if we own the table, just delete it, otherwise at least don't leave it
2206 // with dangling view pointer
2207 if ( m_ownTable )
2796cce3 2208 delete m_table;
ecc7aa82 2209 else if ( m_table && m_table->GetView() == this )
86020f7e 2210 m_table->SetView(NULL);
f2d76237
RD
2211
2212 delete m_typeRegistry;
b5808881 2213 delete m_selection;
82edfbe7
VZ
2214
2215 delete m_setFixedRows;
2216 delete m_setFixedCols;
58dd5b3b
MB
2217}
2218
58dd5b3b
MB
2219//
2220// ----- internal init and update functions
2221//
2222
9950649c
RD
2223// NOTE: If using the default visual attributes works everywhere then this can
2224// be removed as well as the #else cases below.
2225#define _USE_VISATTR 0
2226
58dd5b3b 2227void wxGrid::Create()
f0102d2a 2228{
3d59537f
DS
2229 // create the type registry
2230 m_typeRegistry = new wxGridTypeRegistry;
2c9a89e0 2231
ca65c044 2232 m_cellEditCtrlEnabled = false;
4634a5d6 2233
ccd970b1 2234 m_defaultCellAttr = new wxGridCellAttr();
f2d76237
RD
2235
2236 // Set default cell attributes
ccd970b1 2237 m_defaultCellAttr->SetDefAttr(m_defaultCellAttr);
19d7140e 2238 m_defaultCellAttr->SetKind(wxGridCellAttr::Default);
f2d76237 2239 m_defaultCellAttr->SetFont(GetFont());
4c7277db 2240 m_defaultCellAttr->SetAlignment(wxALIGN_LEFT, wxALIGN_TOP);
9950649c
RD
2241 m_defaultCellAttr->SetRenderer(new wxGridCellStringRenderer);
2242 m_defaultCellAttr->SetEditor(new wxGridCellTextEditor);
2243
2244#if _USE_VISATTR
2245 wxVisualAttributes gva = wxListBox::GetClassDefaultAttributes();
2246 wxVisualAttributes lva = wxPanel::GetClassDefaultAttributes();
2247
2248 m_defaultCellAttr->SetTextColour(gva.colFg);
2249 m_defaultCellAttr->SetBackgroundColour(gva.colBg);
ca65c044 2250
9950649c 2251#else
f2d76237 2252 m_defaultCellAttr->SetTextColour(
a756f210 2253 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT));
f2d76237 2254 m_defaultCellAttr->SetBackgroundColour(
a756f210 2255 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW));
9950649c 2256#endif
2796cce3 2257
4634a5d6
MB
2258 m_numRows = 0;
2259 m_numCols = 0;
2260 m_currentCellCoords = wxGridNoCellCoords;
b99be8fb 2261
f2d76237 2262 // subwindow components that make up the wxGrid
ad805b9e
VZ
2263 m_rowLabelWin = new wxGridRowLabelWindow(this);
2264 CreateColumnWindow();
2265 m_cornerLabelWin = new wxGridCornerLabelWindow(this);
2266 m_gridWin = new wxGridWindow( this );
2d66e025
MB
2267
2268 SetTargetWindow( m_gridWin );
6f36917b 2269
9950649c
RD
2270#if _USE_VISATTR
2271 wxColour gfg = gva.colFg;
2272 wxColour gbg = gva.colBg;
2273 wxColour lfg = lva.colFg;
2274 wxColour lbg = lva.colBg;
2275#else
2276 wxColour gfg = wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT );
2277 wxColour gbg = wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOW );
2278 wxColour lfg = wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT );
2279 wxColour lbg = wxSystemSettings::GetColour( wxSYS_COLOUR_BTNFACE );
2280#endif
2f024384 2281
fa47d7a7
VS
2282 m_cornerLabelWin->SetOwnForegroundColour(lfg);
2283 m_cornerLabelWin->SetOwnBackgroundColour(lbg);
2284 m_rowLabelWin->SetOwnForegroundColour(lfg);
2285 m_rowLabelWin->SetOwnBackgroundColour(lbg);
ad805b9e
VZ
2286 m_colWindow->SetOwnForegroundColour(lfg);
2287 m_colWindow->SetOwnBackgroundColour(lbg);
fa47d7a7
VS
2288
2289 m_gridWin->SetOwnForegroundColour(gfg);
2290 m_gridWin->SetOwnBackgroundColour(gbg);
ca65c044 2291
ad805b9e
VZ
2292 m_labelBackgroundColour = m_rowLabelWin->GetBackgroundColour();
2293 m_labelTextColour = m_rowLabelWin->GetForegroundColour();
2294
2295 // now that we have the grid window, use its font to compute the default
2296 // row height
2297 m_defaultRowHeight = m_gridWin->GetCharHeight();
2298#if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl()
2299 m_defaultRowHeight += 8;
2300#else
2301 m_defaultRowHeight += 4;
2302#endif
2303
2304}
2305
2306void wxGrid::CreateColumnWindow()
2307{
2308 if ( m_useNativeHeader )
2309 {
2310 m_colWindow = new wxGridHeaderCtrl(this);
2311 m_colLabelHeight = m_colWindow->GetBestSize().y;
2312 }
2313 else // draw labels ourselves
2314 {
2315 m_colWindow = new wxGridColLabelWindow(this);
2316 m_colLabelHeight = WXGRID_DEFAULT_COL_LABEL_HEIGHT;
2317 }
2d66e025 2318}
f85afd4e 2319
b5808881 2320bool wxGrid::CreateGrid( int numRows, int numCols,
6b992f7d 2321 wxGridSelectionModes selmode )
2d66e025 2322{
f6bcfd97 2323 wxCHECK_MSG( !m_created,
ca65c044 2324 false,
f6bcfd97
BP
2325 wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
2326
6b992f7d 2327 return SetTable(new wxGridStringTable(numRows, numCols), true, selmode);
2796cce3
RD
2328}
2329
6b992f7d 2330void wxGrid::SetSelectionMode(wxGridSelectionModes selmode)
f1567cdd 2331{
6f36917b
VZ
2332 wxCHECK_RET( m_created,
2333 wxT("Called wxGrid::SetSelectionMode() before calling CreateGrid()") );
2334
2335 m_selection->SetSelectionMode( selmode );
f1567cdd
SN
2336}
2337
aa5b8857
SN
2338wxGrid::wxGridSelectionModes wxGrid::GetSelectionMode() const
2339{
6b992f7d 2340 wxCHECK_MSG( m_created, wxGridSelectCells,
aa5b8857
SN
2341 wxT("Called wxGrid::GetSelectionMode() before calling CreateGrid()") );
2342
2343 return m_selection->GetSelectionMode();
2344}
2345
6b992f7d
VZ
2346bool
2347wxGrid::SetTable(wxGridTableBase *table,
2348 bool takeOwnership,
2349 wxGrid::wxGridSelectionModes selmode )
2796cce3 2350{
a7dde52f 2351 bool checkSelection = false;
2796cce3
RD
2352 if ( m_created )
2353 {
3e13956a 2354 // stop all processing
ca65c044 2355 m_created = false;
86c7378f 2356
c71b2126 2357 if (m_table)
86c7378f 2358 {
a7dde52f
SN
2359 m_table->SetView(0);
2360 if( m_ownTable )
2361 delete m_table;
5b061713 2362 m_table = NULL;
86c7378f 2363 }
2f024384 2364
5276b0a5 2365 wxDELETE(m_selection);
a7dde52f
SN
2366
2367 m_ownTable = false;
2f024384
DS
2368 m_numRows = 0;
2369 m_numCols = 0;
a7dde52f
SN
2370 checkSelection = true;
2371
2372 // kill row and column size arrays
2373 m_colWidths.Empty();
2374 m_colRights.Empty();
2375 m_rowHeights.Empty();
2376 m_rowBottoms.Empty();
233a54f6 2377 }
2f024384 2378
233a54f6 2379 if (table)
2796cce3
RD
2380 {
2381 m_numRows = table->GetNumberRows();
2382 m_numCols = table->GetNumberCols();
2383
ad805b9e 2384 if ( m_useNativeHeader )
3039ade9 2385 GetGridColHeader()->SetColumnCount(m_numCols);
ad805b9e 2386
2796cce3
RD
2387 m_table = table;
2388 m_table->SetView( this );
8fc856de 2389 m_ownTable = takeOwnership;
043d16b2 2390 m_selection = new wxGridSelection( this, selmode );
a7dde52f
SN
2391 if (checkSelection)
2392 {
2393 // If the newly set table is smaller than the
2394 // original one current cell and selection regions
2395 // might be invalid,
8a3e536c 2396 m_selectedBlockCorner = wxGridNoCellCoords;
c71b2126 2397 m_currentCellCoords =
a7dde52f
SN
2398 wxGridCellCoords(wxMin(m_numRows, m_currentCellCoords.GetRow()),
2399 wxMin(m_numCols, m_currentCellCoords.GetCol()));
8a3e536c
VZ
2400 if (m_selectedBlockTopLeft.GetRow() >= m_numRows ||
2401 m_selectedBlockTopLeft.GetCol() >= m_numCols)
a7dde52f 2402 {
8a3e536c
VZ
2403 m_selectedBlockTopLeft = wxGridNoCellCoords;
2404 m_selectedBlockBottomRight = wxGridNoCellCoords;
a7dde52f
SN
2405 }
2406 else
8a3e536c 2407 m_selectedBlockBottomRight =
a7dde52f 2408 wxGridCellCoords(wxMin(m_numRows,
8a3e536c 2409 m_selectedBlockBottomRight.GetRow()),
a7dde52f 2410 wxMin(m_numCols,
8a3e536c 2411 m_selectedBlockBottomRight.GetCol()));
a7dde52f 2412 }
6f36917b
VZ
2413 CalcDimensions();
2414
ca65c044 2415 m_created = true;
2d66e025 2416 }
f85afd4e 2417
1f89d869
VZ
2418 InvalidateBestSize();
2419
2d66e025 2420 return m_created;
f85afd4e
MB
2421}
2422
ad805b9e 2423void wxGrid::Init()
f9549841
VZ
2424{
2425 m_created = false;
2426
2427 m_cornerLabelWin = NULL;
2428 m_rowLabelWin = NULL;
ad805b9e 2429 m_colWindow = NULL;
f9549841
VZ
2430 m_gridWin = NULL;
2431
2432 m_table = NULL;
2433 m_ownTable = false;
2434
2435 m_selection = NULL;
2436 m_defaultCellAttr = NULL;
2437 m_typeRegistry = NULL;
2438 m_winCapture = NULL;
f9549841 2439
f85afd4e
MB
2440 m_rowLabelWidth = WXGRID_DEFAULT_ROW_LABEL_WIDTH;
2441 m_colLabelHeight = WXGRID_DEFAULT_COL_LABEL_HEIGHT;
2442
82edfbe7
VZ
2443 m_setFixedRows =
2444 m_setFixedCols = NULL;
2445
0a976765
VZ
2446 // init attr cache
2447 m_attrCache.row = -1;
2b5f62a0
VZ
2448 m_attrCache.col = -1;
2449 m_attrCache.attr = NULL;
0a976765 2450
ff72f628 2451 m_labelFont = GetFont();
52d6f640 2452 m_labelFont.SetWeight( wxBOLD );
8f177c8e 2453
73145b0e 2454 m_rowLabelHorizAlign = wxALIGN_CENTRE;
4c7277db 2455 m_rowLabelVertAlign = wxALIGN_CENTRE;
f85afd4e 2456
4c7277db 2457 m_colLabelHorizAlign = wxALIGN_CENTRE;
73145b0e 2458 m_colLabelVertAlign = wxALIGN_CENTRE;
d43851f7 2459 m_colLabelTextOrientation = wxHORIZONTAL;
f85afd4e 2460
f85afd4e 2461 m_defaultColWidth = WXGRID_DEFAULT_COL_WIDTH;
ad805b9e 2462 m_defaultRowHeight = 0; // this will be initialized after creation
1f1ce288 2463
b8d24d4e
RG
2464 m_minAcceptableColWidth = WXGRID_MIN_COL_WIDTH;
2465 m_minAcceptableRowHeight = WXGRID_MIN_ROW_HEIGHT;
2466
73145b0e 2467 m_gridLineColour = wxColour( 192,192,192 );
ca65c044 2468 m_gridLinesEnabled = true;
9f7aee01
VZ
2469 m_gridLinesClipHorz =
2470 m_gridLinesClipVert = true;
73145b0e 2471 m_cellHighlightColour = *wxBLACK;
bf7945ce 2472 m_cellHighlightPenWidth = 2;
d2520c85 2473 m_cellHighlightROPenWidth = 1;
8f177c8e 2474
d4175745
VZ
2475 m_canDragColMove = false;
2476
58dd5b3b 2477 m_cursorMode = WXGRID_CURSOR_SELECT_CELL;
10a4531d 2478 m_winCapture = NULL;
ca65c044
WS
2479 m_canDragRowSize = true;
2480 m_canDragColSize = true;
2481 m_canDragGridSize = true;
79dbea21 2482 m_canDragCell = false;
f85afd4e
MB
2483 m_dragLastPos = -1;
2484 m_dragRowOrCol = -1;
ca65c044 2485 m_isDragging = false;
07296f0b 2486 m_startDragPos = wxDefaultPosition;
ad805b9e 2487
11393d29
VZ
2488 m_sortCol = wxNOT_FOUND;
2489 m_sortIsAscending = true;
2490
ad805b9e 2491 m_useNativeHeader =
71cf399f 2492 m_nativeColumnLabels = false;
f85afd4e 2493
ca65c044 2494 m_waitForSlowClick = false;
025562fe 2495
f85afd4e
MB
2496 m_rowResizeCursor = wxCursor( wxCURSOR_SIZENS );
2497 m_colResizeCursor = wxCursor( wxCURSOR_SIZEWE );
2498
2499 m_currentCellCoords = wxGridNoCellCoords;
f85afd4e 2500
ad805b9e
VZ
2501 m_selectedBlockTopLeft =
2502 m_selectedBlockBottomRight =
2503 m_selectedBlockCorner = wxGridNoCellCoords;
b524b5c6 2504
d43851f7
JS
2505 m_selectionBackground = wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT);
2506 m_selectionForeground = wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT);
8f177c8e 2507
ca65c044 2508 m_editable = true; // default for whole grid
f85afd4e 2509
ca65c044 2510 m_inOnKeyDown = false;
2d66e025 2511 m_batchCount = 0;
3c79cf49 2512
266e8367 2513 m_extraWidth =
526dbb95 2514 m_extraHeight = 0;
608754c4 2515
fd76c6a7
VZ
2516 // we can't call SetScrollRate() as the window isn't created yet but OTOH
2517 // we don't need to call it neither as the scroll position is (0, 0) right
2518 // now anyhow, so just set the parameters directly
2519 m_xScrollPixelsPerLine = GRID_SCROLL_LINE_X;
2520 m_yScrollPixelsPerLine = GRID_SCROLL_LINE_Y;
1dc17bca
VZ
2521
2522 m_tabBehaviour = Tab_Stop;
7c1cb261
VZ
2523}
2524
2525// ----------------------------------------------------------------------------
2526// the idea is to call these functions only when necessary because they create
2527// quite big arrays which eat memory mostly unnecessary - in particular, if
2528// default widths/heights are used for all rows/columns, we may not use these
2529// arrays at all
2530//
0ed3b812
VZ
2531// with some extra code, it should be possible to only store the widths/heights
2532// different from default ones (resulting in space savings for huge grids) but
2533// this is not done currently
7c1cb261
VZ
2534// ----------------------------------------------------------------------------
2535
2536void wxGrid::InitRowHeights()
2537{
2538 m_rowHeights.Empty();
2539 m_rowBottoms.Empty();
2540
2541 m_rowHeights.Alloc( m_numRows );
2542 m_rowBottoms.Alloc( m_numRows );
2543
27f35b66
SN
2544 m_rowHeights.Add( m_defaultRowHeight, m_numRows );
2545
0ed3b812 2546 int rowBottom = 0;
3d59537f 2547 for ( int i = 0; i < m_numRows; i++ )
7c1cb261 2548 {
7c1cb261
VZ
2549 rowBottom += m_defaultRowHeight;
2550 m_rowBottoms.Add( rowBottom );
2551 }
2552}
2553
2554void wxGrid::InitColWidths()
2555{
2556 m_colWidths.Empty();
2557 m_colRights.Empty();
2558
2559 m_colWidths.Alloc( m_numCols );
2560 m_colRights.Alloc( m_numCols );
27f35b66
SN
2561
2562 m_colWidths.Add( m_defaultColWidth, m_numCols );
2563
3d59537f 2564 for ( int i = 0; i < m_numCols; i++ )
7c1cb261 2565 {
e822d1bd 2566 int colRight = ( GetColPos( i ) + 1 ) * m_defaultColWidth;
7c1cb261
VZ
2567 m_colRights.Add( colRight );
2568 }
2569}
2570
2571int wxGrid::GetColWidth(int col) const
2572{
30496905
VZ
2573 if ( m_colWidths.IsEmpty() )
2574 return m_defaultColWidth;
2575
2576 // a negative width indicates a hidden column
2577 return m_colWidths[col] > 0 ? m_colWidths[col] : 0;
7c1cb261
VZ
2578}
2579
2580int wxGrid::GetColLeft(int col) const
2581{
30496905
VZ
2582 if ( m_colRights.IsEmpty() )
2583 return GetColPos( col ) * m_defaultColWidth;
2584
2585 return m_colRights[col] - GetColWidth(col);
7c1cb261
VZ
2586}
2587
2588int wxGrid::GetColRight(int col) const
2589{
d4175745 2590 return m_colRights.IsEmpty() ? (GetColPos( col ) + 1) * m_defaultColWidth
7c1cb261
VZ
2591 : m_colRights[col];
2592}
2593
2594int wxGrid::GetRowHeight(int row) const
2595{
30496905
VZ
2596 // no custom heights / hidden rows
2597 if ( m_rowHeights.IsEmpty() )
2598 return m_defaultRowHeight;
2599
2600 // a negative height indicates a hidden row
2601 return m_rowHeights[row] > 0 ? m_rowHeights[row] : 0;
7c1cb261 2602}
2d66e025 2603
7c1cb261
VZ
2604int wxGrid::GetRowTop(int row) const
2605{
30496905
VZ
2606 if ( m_rowBottoms.IsEmpty() )
2607 return row * m_defaultRowHeight;
2608
2609 return m_rowBottoms[row] - GetRowHeight(row);
f85afd4e
MB
2610}
2611
7c1cb261
VZ
2612int wxGrid::GetRowBottom(int row) const
2613{
2614 return m_rowBottoms.IsEmpty() ? (row + 1) * m_defaultRowHeight
2615 : m_rowBottoms[row];
2616}
f85afd4e
MB
2617
2618void wxGrid::CalcDimensions()
2619{
0ed3b812
VZ
2620 // compute the size of the scrollable area
2621 int w = m_numCols > 0 ? GetColRight(GetColAt(m_numCols - 1)) : 0;
2622 int h = m_numRows > 0 ? GetRowBottom(m_numRows - 1) : 0;
60ff3b99 2623
0ed3b812
VZ
2624 w += m_extraWidth;
2625 h += m_extraHeight;
faec5a43 2626
73145b0e 2627 // take into account editor if shown
4db6714b 2628 if ( IsCellEditControlShown() )
73145b0e 2629 {
3d59537f
DS
2630 int w2, h2;
2631 int r = m_currentCellCoords.GetRow();
2632 int c = m_currentCellCoords.GetCol();
2633 int x = GetColLeft(c);
2634 int y = GetRowTop(r);
2635
2636 // how big is the editor
2637 wxGridCellAttr* attr = GetCellAttr(r, c);
2638 wxGridCellEditor* editor = attr->GetEditor(this, r, c);
2639 editor->GetControl()->GetSize(&w2, &h2);
2640 w2 += x;
2641 h2 += y;
2642 if ( w2 > w )
2643 w = w2;
2644 if ( h2 > h )
2645 h = h2;
2646 editor->DecRef();
2647 attr->DecRef();
73145b0e
JS
2648 }
2649
faec5a43
SN
2650 // preserve (more or less) the previous position
2651 int x, y;
2652 GetViewStart( &x, &y );
97a9929e 2653
c92ed9f7 2654 // ensure the position is valid for the new scroll ranges
7b519e5e 2655 if ( x >= w )
c92ed9f7 2656 x = wxMax( w - 1, 0 );
7b519e5e 2657 if ( y >= h )
c92ed9f7 2658 y = wxMax( h - 1, 0 );
faec5a43 2659
ace8d849 2660 // update the virtual size and refresh the scrollbars to reflect it
f1ff7df0
VZ
2661 m_gridWin->SetVirtualSize(w, h);
2662 Scroll(x, y);
ace8d849 2663 AdjustScrollbars();
12314291
VZ
2664
2665 // if our OnSize() hadn't been called (it would if we have scrollbars), we
2666 // still must reposition the children
2667 CalcWindowSizes();
f85afd4e
MB
2668}
2669
69367c56
VZ
2670wxSize wxGrid::GetSizeAvailableForScrollTarget(const wxSize& size)
2671{
2672 wxSize sizeGridWin(size);
2673 sizeGridWin.x -= m_rowLabelWidth;
2674 sizeGridWin.y -= m_colLabelHeight;
2675
2676 return sizeGridWin;
2677}
2678
7807d81c
MB
2679void wxGrid::CalcWindowSizes()
2680{
b0a877ec
SC
2681 // escape if the window is has not been fully created yet
2682
2683 if ( m_cornerLabelWin == NULL )
2f024384 2684 return;
b0a877ec 2685
7807d81c
MB
2686 int cw, ch;
2687 GetClientSize( &cw, &ch );
b99be8fb 2688
c1841ac2
VZ
2689 // the grid may be too small to have enough space for the labels yet, don't
2690 // size the windows to negative sizes in this case
2691 int gw = cw - m_rowLabelWidth;
2692 int gh = ch - m_colLabelHeight;
2693 if (gw < 0)
2694 gw = 0;
2695 if (gh < 0)
2696 gh = 0;
2697
6d308072 2698 if ( m_cornerLabelWin && m_cornerLabelWin->IsShown() )
7807d81c
MB
2699 m_cornerLabelWin->SetSize( 0, 0, m_rowLabelWidth, m_colLabelHeight );
2700
ad805b9e
VZ
2701 if ( m_colWindow && m_colWindow->IsShown() )
2702 m_colWindow->SetSize( m_rowLabelWidth, 0, gw, m_colLabelHeight );
7807d81c 2703
6d308072 2704 if ( m_rowLabelWin && m_rowLabelWin->IsShown() )
c1841ac2 2705 m_rowLabelWin->SetSize( 0, m_colLabelHeight, m_rowLabelWidth, gh );
7807d81c 2706
6d308072 2707 if ( m_gridWin && m_gridWin->IsShown() )
c1841ac2 2708 m_gridWin->SetSize( m_rowLabelWidth, m_colLabelHeight, gw, gh );
7807d81c
MB
2709}
2710
3d59537f
DS
2711// this is called when the grid table sends a message
2712// to indicate that it has been redimensioned
f85afd4e
MB
2713//
2714bool wxGrid::Redimension( wxGridTableMessage& msg )
2715{
2716 int i;
ca65c044 2717 bool result = false;
8f177c8e 2718
a6794685
SN
2719 // Clear the attribute cache as the attribute might refer to a different
2720 // cell than stored in the cache after adding/removing rows/columns.
2721 ClearAttrCache();
2f024384 2722
7e48d7d9
SN
2723 // By the same reasoning, the editor should be dismissed if columns are
2724 // added or removed. And for consistency, it should IMHO always be
2725 // removed, not only if the cell "underneath" it actually changes.
2726 // For now, I intentionally do not save the editor's content as the
2727 // cell it might want to save that stuff to might no longer exist.
bca7bfc8 2728 HideCellEditControl();
2f024384 2729
f85afd4e
MB
2730 switch ( msg.GetId() )
2731 {
2732 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED:
2733 {
2734 size_t pos = msg.GetCommandInt();
2735 int numRows = msg.GetCommandInt2();
f6bcfd97 2736
f85afd4e 2737 m_numRows += numRows;
2d66e025 2738
f6bcfd97
BP
2739 if ( !m_rowHeights.IsEmpty() )
2740 {
27f35b66
SN
2741 m_rowHeights.Insert( m_defaultRowHeight, pos, numRows );
2742 m_rowBottoms.Insert( 0, pos, numRows );
f6bcfd97
BP
2743
2744 int bottom = 0;
2f024384
DS
2745 if ( pos > 0 )
2746 bottom = m_rowBottoms[pos - 1];
60ff3b99 2747
3d59537f 2748 for ( i = pos; i < m_numRows; i++ )
f6bcfd97
BP
2749 {
2750 bottom += m_rowHeights[i];
2751 m_rowBottoms[i] = bottom;
2752 }
2753 }
2f024384 2754
f6bcfd97
BP
2755 if ( m_currentCellCoords == wxGridNoCellCoords )
2756 {
2757 // if we have just inserted cols into an empty grid the current
2758 // cell will be undefined...
2759 //
2760 SetCurrentCell( 0, 0 );
2761 }
3f3dc2ef
VZ
2762
2763 if ( m_selection )
2764 m_selection->UpdateRows( pos, numRows );
f6bcfd97
BP
2765 wxGridCellAttrProvider * attrProvider = m_table->GetAttrProvider();
2766 if (attrProvider)
2767 attrProvider->UpdateAttrRows( pos, numRows );
2768
2769 if ( !GetBatchCount() )
2d66e025 2770 {
f6bcfd97
BP
2771 CalcDimensions();
2772 m_rowLabelWin->Refresh();
2d66e025 2773 }
f85afd4e 2774 }
ca65c044 2775 result = true;
f6bcfd97 2776 break;
f85afd4e
MB
2777
2778 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED:
2779 {
2780 int numRows = msg.GetCommandInt();
2d66e025 2781 int oldNumRows = m_numRows;
f85afd4e 2782 m_numRows += numRows;
2d66e025 2783
f6bcfd97
BP
2784 if ( !m_rowHeights.IsEmpty() )
2785 {
27f35b66
SN
2786 m_rowHeights.Add( m_defaultRowHeight, numRows );
2787 m_rowBottoms.Add( 0, numRows );
60ff3b99 2788
f6bcfd97 2789 int bottom = 0;
2f024384
DS
2790 if ( oldNumRows > 0 )
2791 bottom = m_rowBottoms[oldNumRows - 1];
f6bcfd97 2792
3d59537f 2793 for ( i = oldNumRows; i < m_numRows; i++ )
f6bcfd97
BP
2794 {
2795 bottom += m_rowHeights[i];
2796 m_rowBottoms[i] = bottom;
2797 }
2798 }
2f024384 2799
f6bcfd97
BP
2800 if ( m_currentCellCoords == wxGridNoCellCoords )
2801 {
2802 // if we have just inserted cols into an empty grid the current
2803 // cell will be undefined...
2804 //
2805 SetCurrentCell( 0, 0 );
2806 }
2f024384 2807
f6bcfd97 2808 if ( !GetBatchCount() )
2d66e025 2809 {
f6bcfd97
BP
2810 CalcDimensions();
2811 m_rowLabelWin->Refresh();
2d66e025 2812 }
f85afd4e 2813 }
ca65c044 2814 result = true;
f6bcfd97 2815 break;
f85afd4e
MB
2816
2817 case wxGRIDTABLE_NOTIFY_ROWS_DELETED:
2818 {
2819 size_t pos = msg.GetCommandInt();
2820 int numRows = msg.GetCommandInt2();
f85afd4e
MB
2821 m_numRows -= numRows;
2822
f6bcfd97 2823 if ( !m_rowHeights.IsEmpty() )
f85afd4e 2824 {
27f35b66
SN
2825 m_rowHeights.RemoveAt( pos, numRows );
2826 m_rowBottoms.RemoveAt( pos, numRows );
2d66e025
MB
2827
2828 int h = 0;
3d59537f 2829 for ( i = 0; i < m_numRows; i++ )
2d66e025
MB
2830 {
2831 h += m_rowHeights[i];
2832 m_rowBottoms[i] = h;
2833 }
f85afd4e 2834 }
3d59537f 2835
f6bcfd97
BP
2836 if ( !m_numRows )
2837 {
2838 m_currentCellCoords = wxGridNoCellCoords;
2839 }
2840 else
2841 {
2842 if ( m_currentCellCoords.GetRow() >= m_numRows )
2843 m_currentCellCoords.Set( 0, 0 );
2844 }
3f3dc2ef
VZ
2845
2846 if ( m_selection )
2847 m_selection->UpdateRows( pos, -((int)numRows) );
f6bcfd97 2848 wxGridCellAttrProvider * attrProvider = m_table->GetAttrProvider();
4db6714b
KH
2849 if (attrProvider)
2850 {
f6bcfd97 2851 attrProvider->UpdateAttrRows( pos, -((int)numRows) );
3d59537f 2852
84912ef8
RD
2853// ifdef'd out following patch from Paul Gammans
2854#if 0
3ca6a5f0 2855 // No need to touch column attributes, unless we
f6bcfd97
BP
2856 // removed _all_ rows, in this case, we remove
2857 // all column attributes.
2858 // I hate to do this here, but the
2859 // needed data is not available inside UpdateAttrRows.
2860 if ( !GetNumberRows() )
2861 attrProvider->UpdateAttrCols( 0, -GetNumberCols() );
84912ef8 2862#endif
f6bcfd97 2863 }
ccdee36f 2864
f6bcfd97
BP
2865 if ( !GetBatchCount() )
2866 {
2867 CalcDimensions();
2868 m_rowLabelWin->Refresh();
2869 }
f85afd4e 2870 }
ca65c044 2871 result = true;
f6bcfd97 2872 break;
f85afd4e
MB
2873
2874 case wxGRIDTABLE_NOTIFY_COLS_INSERTED:
2875 {
2876 size_t pos = msg.GetCommandInt();
2877 int numCols = msg.GetCommandInt2();
f85afd4e 2878 m_numCols += numCols;
2d66e025 2879
ad805b9e 2880 if ( m_useNativeHeader )
3039ade9 2881 GetGridColHeader()->SetColumnCount(m_numCols);
ad805b9e 2882
d4175745
VZ
2883 if ( !m_colAt.IsEmpty() )
2884 {
2885 //Shift the column IDs
d4175745
VZ
2886 for ( i = 0; i < m_numCols - numCols; i++ )
2887 {
2888 if ( m_colAt[i] >= (int)pos )
2889 m_colAt[i] += numCols;
2890 }
2891
2892 m_colAt.Insert( pos, pos, numCols );
2893
2894 //Set the new columns' positions
2895 for ( i = pos + 1; i < (int)pos + numCols; i++ )
2896 {
2897 m_colAt[i] = i;
2898 }
2899 }
2900
f6bcfd97
BP
2901 if ( !m_colWidths.IsEmpty() )
2902 {
27f35b66
SN
2903 m_colWidths.Insert( m_defaultColWidth, pos, numCols );
2904 m_colRights.Insert( 0, pos, numCols );
f6bcfd97
BP
2905
2906 int right = 0;
2f024384 2907 if ( pos > 0 )
d4175745 2908 right = m_colRights[GetColAt( pos - 1 )];
60ff3b99 2909
d4175745
VZ
2910 int colPos;
2911 for ( colPos = pos; colPos < m_numCols; colPos++ )
f6bcfd97 2912 {
d4175745
VZ
2913 i = GetColAt( colPos );
2914
f6bcfd97
BP
2915 right += m_colWidths[i];
2916 m_colRights[i] = right;
2917 }
2918 }
2f024384 2919
f6bcfd97 2920 if ( m_currentCellCoords == wxGridNoCellCoords )
2d66e025 2921 {
f6bcfd97
BP
2922 // if we have just inserted cols into an empty grid the current
2923 // cell will be undefined...
2924 //
2925 SetCurrentCell( 0, 0 );
2d66e025 2926 }
3f3dc2ef
VZ
2927
2928 if ( m_selection )
2929 m_selection->UpdateCols( pos, numCols );
f6bcfd97
BP
2930 wxGridCellAttrProvider * attrProvider = m_table->GetAttrProvider();
2931 if (attrProvider)
2932 attrProvider->UpdateAttrCols( pos, numCols );
2933 if ( !GetBatchCount() )
2934 {
2935 CalcDimensions();
ad805b9e 2936 m_colWindow->Refresh();
f6bcfd97 2937 }
f85afd4e 2938 }
ca65c044 2939 result = true;
f6bcfd97 2940 break;
f85afd4e
MB
2941
2942 case wxGRIDTABLE_NOTIFY_COLS_APPENDED:
2943 {
2944 int numCols = msg.GetCommandInt();
2d66e025 2945 int oldNumCols = m_numCols;
f85afd4e 2946 m_numCols += numCols;
ad805b9e 2947 if ( m_useNativeHeader )
3039ade9 2948 GetGridColHeader()->SetColumnCount(m_numCols);
d4175745
VZ
2949
2950 if ( !m_colAt.IsEmpty() )
2951 {
2952 m_colAt.Add( 0, numCols );
2953
2954 //Set the new columns' positions
d4175745
VZ
2955 for ( i = oldNumCols; i < m_numCols; i++ )
2956 {
2957 m_colAt[i] = i;
2958 }
2959 }
2960
f6bcfd97
BP
2961 if ( !m_colWidths.IsEmpty() )
2962 {
27f35b66
SN
2963 m_colWidths.Add( m_defaultColWidth, numCols );
2964 m_colRights.Add( 0, numCols );
2d66e025 2965
f6bcfd97 2966 int right = 0;
2f024384 2967 if ( oldNumCols > 0 )
d4175745 2968 right = m_colRights[GetColAt( oldNumCols - 1 )];
60ff3b99 2969
d4175745
VZ
2970 int colPos;
2971 for ( colPos = oldNumCols; colPos < m_numCols; colPos++ )
f6bcfd97 2972 {
d4175745
VZ
2973 i = GetColAt( colPos );
2974
f6bcfd97
BP
2975 right += m_colWidths[i];
2976 m_colRights[i] = right;
2977 }
2978 }
2f024384 2979
f6bcfd97 2980 if ( m_currentCellCoords == wxGridNoCellCoords )
2d66e025 2981 {
f6bcfd97
BP
2982 // if we have just inserted cols into an empty grid the current
2983 // cell will be undefined...
2984 //
2985 SetCurrentCell( 0, 0 );
2986 }
2987 if ( !GetBatchCount() )
2988 {
2989 CalcDimensions();
ad805b9e 2990 m_colWindow->Refresh();
2d66e025 2991 }
f85afd4e 2992 }
ca65c044 2993 result = true;
f6bcfd97 2994 break;
f85afd4e
MB
2995
2996 case wxGRIDTABLE_NOTIFY_COLS_DELETED:
2997 {
2998 size_t pos = msg.GetCommandInt();
2999 int numCols = msg.GetCommandInt2();
f85afd4e 3000 m_numCols -= numCols;
ad805b9e 3001 if ( m_useNativeHeader )
3039ade9 3002 GetGridColHeader()->SetColumnCount(m_numCols);
f85afd4e 3003
d4175745
VZ
3004 if ( !m_colAt.IsEmpty() )
3005 {
3006 int colID = GetColAt( pos );
3007
3008 m_colAt.RemoveAt( pos, numCols );
3009
3010 //Shift the column IDs
3011 int colPos;
3012 for ( colPos = 0; colPos < m_numCols; colPos++ )
3013 {
3014 if ( m_colAt[colPos] > colID )
3015 m_colAt[colPos] -= numCols;
3016 }
3017 }
3018
f6bcfd97 3019 if ( !m_colWidths.IsEmpty() )
f85afd4e 3020 {
27f35b66
SN
3021 m_colWidths.RemoveAt( pos, numCols );
3022 m_colRights.RemoveAt( pos, numCols );
2d66e025
MB
3023
3024 int w = 0;
d4175745
VZ
3025 int colPos;
3026 for ( colPos = 0; colPos < m_numCols; colPos++ )
2d66e025 3027 {
d4175745
VZ
3028 i = GetColAt( colPos );
3029
2d66e025
MB
3030 w += m_colWidths[i];
3031 m_colRights[i] = w;
3032 }
f85afd4e 3033 }
2f024384 3034
f6bcfd97
BP
3035 if ( !m_numCols )
3036 {
3037 m_currentCellCoords = wxGridNoCellCoords;
3038 }
3039 else
3040 {
3041 if ( m_currentCellCoords.GetCol() >= m_numCols )
3042 m_currentCellCoords.Set( 0, 0 );
3043 }
3f3dc2ef
VZ
3044
3045 if ( m_selection )
3046 m_selection->UpdateCols( pos, -((int)numCols) );
f6bcfd97 3047 wxGridCellAttrProvider * attrProvider = m_table->GetAttrProvider();
4db6714b
KH
3048 if (attrProvider)
3049 {
f6bcfd97 3050 attrProvider->UpdateAttrCols( pos, -((int)numCols) );
ccdee36f 3051
84912ef8
RD
3052// ifdef'd out following patch from Paul Gammans
3053#if 0
f6bcfd97
BP
3054 // No need to touch row attributes, unless we
3055 // removed _all_ columns, in this case, we remove
3056 // all row attributes.
3057 // I hate to do this here, but the
3058 // needed data is not available inside UpdateAttrCols.
3059 if ( !GetNumberCols() )
3060 attrProvider->UpdateAttrRows( 0, -GetNumberRows() );
84912ef8 3061#endif
f6bcfd97 3062 }
ccdee36f 3063
f6bcfd97
BP
3064 if ( !GetBatchCount() )
3065 {
3066 CalcDimensions();
ad805b9e 3067 m_colWindow->Refresh();
f6bcfd97 3068 }
f85afd4e 3069 }
ca65c044 3070 result = true;
f6bcfd97 3071 break;
f85afd4e
MB
3072 }
3073
a6b40a9a
VZ
3074 InvalidateBestSize();
3075
f6bcfd97
BP
3076 if (result && !GetBatchCount() )
3077 m_gridWin->Refresh();
2f024384 3078
f6bcfd97 3079 return result;
f85afd4e
MB
3080}
3081
ef316e23 3082wxArrayInt wxGrid::CalcRowLabelsExposed( const wxRegion& reg ) const
f85afd4e 3083{
2d66e025
MB
3084 wxRegionIterator iter( reg );
3085 wxRect r;
f85afd4e 3086
275c4ae4
RD
3087 wxArrayInt rowlabels;
3088
2d66e025
MB
3089 int top, bottom;
3090 while ( iter )
f85afd4e 3091 {
2d66e025 3092 r = iter.GetRect();
f85afd4e 3093
2d66e025
MB
3094 // TODO: remove this when we can...
3095 // There is a bug in wxMotif that gives garbage update
3096 // rectangles if you jump-scroll a long way by clicking the
3097 // scrollbar with middle button. This is a work-around
3098 //
3099#if defined(__WXMOTIF__)
3100 int cw, ch;
3101 m_gridWin->GetClientSize( &cw, &ch );
56b6cf26
DS
3102 if ( r.GetTop() > ch )
3103 r.SetTop( 0 );
2d66e025
MB
3104 r.SetBottom( wxMin( r.GetBottom(), ch ) );
3105#endif
f85afd4e 3106
2d66e025
MB
3107 // logical bounds of update region
3108 //
3109 int dummy;
3110 CalcUnscrolledPosition( 0, r.GetTop(), &dummy, &top );
3111 CalcUnscrolledPosition( 0, r.GetBottom(), &dummy, &bottom );
3112
3113 // find the row labels within these bounds
3114 //
3115 int row;
3d59537f 3116 for ( row = internalYToRow(top); row < m_numRows; row++ )
2d66e025 3117 {
7c1cb261
VZ
3118 if ( GetRowBottom(row) < top )
3119 continue;
2d66e025 3120
6d55126d 3121 if ( GetRowTop(row) > bottom )
7c1cb261 3122 break;
60ff3b99 3123
d10f4bf9 3124 rowlabels.Add( row );
2d66e025 3125 }
60ff3b99 3126
60d8e886 3127 ++iter;
f85afd4e 3128 }
d10f4bf9
VZ
3129
3130 return rowlabels;
f85afd4e
MB
3131}
3132
ef316e23 3133wxArrayInt wxGrid::CalcColLabelsExposed( const wxRegion& reg ) const
f85afd4e 3134{
2d66e025
MB
3135 wxRegionIterator iter( reg );
3136 wxRect r;
f85afd4e 3137
d10f4bf9 3138 wxArrayInt colLabels;
f85afd4e 3139
2d66e025
MB
3140 int left, right;
3141 while ( iter )
f85afd4e 3142 {
2d66e025 3143 r = iter.GetRect();
f85afd4e 3144
2d66e025
MB
3145 // TODO: remove this when we can...
3146 // There is a bug in wxMotif that gives garbage update
3147 // rectangles if you jump-scroll a long way by clicking the
3148 // scrollbar with middle button. This is a work-around
3149 //
3150#if defined(__WXMOTIF__)
3151 int cw, ch;
3152 m_gridWin->GetClientSize( &cw, &ch );
56b6cf26
DS
3153 if ( r.GetLeft() > cw )
3154 r.SetLeft( 0 );
2d66e025
MB
3155 r.SetRight( wxMin( r.GetRight(), cw ) );
3156#endif
3157
3158 // logical bounds of update region
3159 //
3160 int dummy;
3161 CalcUnscrolledPosition( r.GetLeft(), 0, &left, &dummy );
3162 CalcUnscrolledPosition( r.GetRight(), 0, &right, &dummy );
3163
3164 // find the cells within these bounds
3165 //
3166 int col;
d4175745
VZ
3167 int colPos;
3168 for ( colPos = GetColPos( internalXToCol(left) ); colPos < m_numCols; colPos++ )
2d66e025 3169 {
d4175745
VZ
3170 col = GetColAt( colPos );
3171
7c1cb261
VZ
3172 if ( GetColRight(col) < left )
3173 continue;
60ff3b99 3174
7c1cb261
VZ
3175 if ( GetColLeft(col) > right )
3176 break;
2d66e025 3177
d10f4bf9 3178 colLabels.Add( col );
2d66e025 3179 }
60ff3b99 3180
60d8e886 3181 ++iter;
f85afd4e 3182 }
2f024384 3183
d10f4bf9 3184 return colLabels;
f85afd4e
MB
3185}
3186
ef316e23 3187wxGridCellCoordsArray wxGrid::CalcCellsExposed( const wxRegion& reg ) const
f85afd4e 3188{
2d66e025 3189 wxRect r;
f85afd4e 3190
d10f4bf9 3191 wxGridCellCoordsArray cellsExposed;
f85afd4e 3192
2d66e025 3193 int left, top, right, bottom;
9e57072a 3194 for ( wxRegionIterator iter(reg); iter; ++iter )
2d66e025
MB
3195 {
3196 r = iter.GetRect();
f85afd4e 3197
9e57072a
VZ
3198 // Skip 0-height cells, they're invisible anyhow, don't waste time
3199 // getting their rectangles and so on.
3200 if ( !r.GetHeight() )
3201 continue;
3202
2d66e025
MB
3203 // TODO: remove this when we can...
3204 // There is a bug in wxMotif that gives garbage update
3205 // rectangles if you jump-scroll a long way by clicking the
3206 // scrollbar with middle button. This is a work-around
3207 //
3208#if defined(__WXMOTIF__)
f85afd4e 3209 int cw, ch;
2d66e025
MB
3210 m_gridWin->GetClientSize( &cw, &ch );
3211 if ( r.GetTop() > ch ) r.SetTop( 0 );
3212 if ( r.GetLeft() > cw ) r.SetLeft( 0 );
3213 r.SetRight( wxMin( r.GetRight(), cw ) );
3214 r.SetBottom( wxMin( r.GetBottom(), ch ) );
3215#endif
8f177c8e 3216
2d66e025
MB
3217 // logical bounds of update region
3218 //
3219 CalcUnscrolledPosition( r.GetLeft(), r.GetTop(), &left, &top );
3220 CalcUnscrolledPosition( r.GetRight(), r.GetBottom(), &right, &bottom );
f85afd4e 3221
2d66e025 3222 // find the cells within these bounds
cd4f6f5f
VZ
3223 wxArrayInt cols;
3224 for ( int row = internalYToRow(top); row < m_numRows; row++ )
f85afd4e 3225 {
7c1cb261
VZ
3226 if ( GetRowBottom(row) <= top )
3227 continue;
f85afd4e 3228
7c1cb261
VZ
3229 if ( GetRowTop(row) > bottom )
3230 break;
60ff3b99 3231
cd4f6f5f
VZ
3232 // add all dirty cells in this row: notice that the columns which
3233 // are dirty don't depend on the row so we compute them only once
3234 // for the first dirty row and then reuse for all the next ones
3235 if ( cols.empty() )
2d66e025 3236 {
cd4f6f5f
VZ
3237 // do determine the dirty columns
3238 for ( int pos = XToPos(left); pos <= XToPos(right); pos++ )
3239 cols.push_back(GetColAt(pos));
d4175745 3240
cd4f6f5f
VZ
3241 // if there are no dirty columns at all, nothing to do
3242 if ( cols.empty() )
7c1cb261 3243 break;
2d66e025 3244 }
cd4f6f5f
VZ
3245
3246 const size_t count = cols.size();
3247 for ( size_t n = 0; n < count; n++ )
3248 cellsExposed.Add(wxGridCellCoords(row, cols[n]));
2d66e025 3249 }
f85afd4e 3250 }
d10f4bf9
VZ
3251
3252 return cellsExposed;
f85afd4e
MB
3253}
3254
3255
2d66e025 3256void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent& event )
f85afd4e 3257{
2d66e025
MB
3258 int x, y, row;
3259 wxPoint pos( event.GetPosition() );
3260 CalcUnscrolledPosition( pos.x, pos.y, &x, &y );
60ff3b99 3261
2d66e025 3262 if ( event.Dragging() )
f85afd4e 3263 {
426b2d87
JS
3264 if (!m_isDragging)
3265 {
ca65c044 3266 m_isDragging = true;
426b2d87
JS
3267 m_rowLabelWin->CaptureMouse();
3268 }
8f177c8e 3269
2d66e025 3270 if ( event.LeftIsDown() )
f85afd4e 3271 {
962a48f6 3272 switch ( m_cursorMode )
f85afd4e 3273 {
f85afd4e
MB
3274 case WXGRID_CURSOR_RESIZE_ROW:
3275 {
2d66e025
MB
3276 int cw, ch, left, dummy;
3277 m_gridWin->GetClientSize( &cw, &ch );
3278 CalcUnscrolledPosition( 0, 0, &left, &dummy );
60ff3b99 3279
2d66e025
MB
3280 wxClientDC dc( m_gridWin );
3281 PrepareDC( dc );
af547d51
VZ
3282 y = wxMax( y,
3283 GetRowTop(m_dragRowOrCol) +
3284 GetRowMinimalHeight(m_dragRowOrCol) );
d2fdd8d2 3285 dc.SetLogicalFunction(wxINVERT);
f85afd4e
MB
3286 if ( m_dragLastPos >= 0 )
3287 {
2d66e025 3288 dc.DrawLine( left, m_dragLastPos, left+cw, m_dragLastPos );
f85afd4e 3289 }
2d66e025
MB
3290 dc.DrawLine( left, y, left+cw, y );
3291 m_dragLastPos = y;
f85afd4e
MB
3292 }
3293 break;
3294
3295 case WXGRID_CURSOR_SELECT_ROW:
902725ee 3296 {
e32352cf 3297 if ( (row = YToRow( y )) >= 0 )
aa5e1f75 3298 {
3f3dc2ef 3299 if ( m_selection )
8b5f6d9d 3300 m_selection->SelectRow(row, event);
f85afd4e 3301 }
902725ee
WS
3302 }
3303 break;
e2b42eeb
VZ
3304
3305 // default label to suppress warnings about "enumeration value
3306 // 'xxx' not handled in switch
3307 default:
3308 break;
f85afd4e
MB
3309 }
3310 }
3311 return;
3312 }
3313
426b2d87
JS
3314 if ( m_isDragging && (event.Entering() || event.Leaving()) )
3315 return;
8f177c8e 3316
426b2d87
JS
3317 if (m_isDragging)
3318 {
ccdee36f
DS
3319 if (m_rowLabelWin->HasCapture())
3320 m_rowLabelWin->ReleaseMouse();
ca65c044 3321 m_isDragging = false;
426b2d87 3322 }
60ff3b99 3323
6d004f67
MB
3324 // ------------ Entering or leaving the window
3325 //
3326 if ( event.Entering() || event.Leaving() )
3327 {
e2b42eeb 3328 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_rowLabelWin);
6d004f67
MB
3329 }
3330
2d66e025 3331 // ------------ Left button pressed
f85afd4e 3332 //
6d004f67 3333 else if ( event.LeftDown() )
f85afd4e 3334 {
82edfbe7
VZ
3335 row = YToEdgeOfRow(y);
3336 if ( row != wxNOT_FOUND && CanDragRowSize(row) )
3337 {
3338 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW, m_rowLabelWin);
3339 }
3340 else // not a request to start resizing
f85afd4e 3341 {
2d66e025 3342 row = YToRow(y);
2f024384 3343 if ( row >= 0 &&
b54ba671 3344 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK, row, -1, event ) )
f85afd4e 3345 {
2b01fd49 3346 if ( !event.ShiftDown() && !event.CmdDown() )
aa5e1f75 3347 ClearSelection();
64e15340 3348 if ( m_selection )
3f3dc2ef
VZ
3349 {
3350 if ( event.ShiftDown() )
3351 {
8b5f6d9d
VZ
3352 m_selection->SelectBlock
3353 (
3354 m_currentCellCoords.GetRow(), 0,
3355 row, GetNumberCols() - 1,
3356 event
3357 );
3f3dc2ef
VZ
3358 }
3359 else
3360 {
8b5f6d9d 3361 m_selection->SelectRow(row, event);
3f3dc2ef
VZ
3362 }
3363 }
3364
e2b42eeb 3365 ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW, m_rowLabelWin);
f85afd4e 3366 }
2d66e025 3367 }
2d66e025 3368 }
f85afd4e 3369
2d66e025
MB
3370 // ------------ Left double click
3371 //
3372 else if (event.LeftDClick() )
3373 {
4e115ed2 3374 row = YToEdgeOfRow(y);
82edfbe7
VZ
3375 if ( row != wxNOT_FOUND && CanDragRowSize(row) )
3376 {
3377 // adjust row height depending on label text
27bb2c8c
VZ
3378 //
3379 // TODO: generate RESIZING event, see #10754
82edfbe7
VZ
3380 AutoSizeRowLabelSize( row );
3381
c5c1ea96 3382 SendGridSizeEvent(wxEVT_GRID_ROW_SIZE, row, -1, event);
27bb2c8c 3383
82edfbe7
VZ
3384 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, GetColLabelWindow());
3385 m_dragLastPos = -1;
3386 }
d13b34d3 3387 else // not on row separator or it's not resizable
2d66e025
MB
3388 {
3389 row = YToRow(y);
a967f048 3390 if ( row >=0 &&
ca65c044
WS
3391 !SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK, row, -1, event ) )
3392 {
a967f048 3393 // no default action at the moment
ca65c044 3394 }
f85afd4e
MB
3395 }
3396 }
60ff3b99 3397
2d66e025 3398 // ------------ Left button released
f85afd4e 3399 //
2d66e025 3400 else if ( event.LeftUp() )
f85afd4e 3401 {
2d66e025 3402 if ( m_cursorMode == WXGRID_CURSOR_RESIZE_ROW )
27bb2c8c 3403 DoEndDragResizeRow(event);
f85afd4e 3404
e2b42eeb
VZ
3405 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_rowLabelWin);
3406 m_dragLastPos = -1;
2d66e025 3407 }
f85afd4e 3408
2d66e025
MB
3409 // ------------ Right button down
3410 //
3411 else if ( event.RightDown() )
3412 {
3413 row = YToRow(y);
ef5df12b 3414 if ( row >=0 &&
ca65c044 3415 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK, row, -1, event ) )
2d66e025
MB
3416 {
3417 // no default action at the moment
f85afd4e
MB
3418 }
3419 }
60ff3b99 3420
2d66e025 3421 // ------------ Right double click
f85afd4e 3422 //
2d66e025
MB
3423 else if ( event.RightDClick() )
3424 {
3425 row = YToRow(y);
a967f048 3426 if ( row >= 0 &&
ca65c044 3427 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK, row, -1, event ) )
2d66e025
MB
3428 {
3429 // no default action at the moment
3430 }
3431 }
60ff3b99 3432
2d66e025 3433 // ------------ No buttons down and mouse moving
f85afd4e 3434 //
2d66e025 3435 else if ( event.Moving() )
f85afd4e 3436 {
2d66e025 3437 m_dragRowOrCol = YToEdgeOfRow( y );
82edfbe7 3438 if ( m_dragRowOrCol != wxNOT_FOUND )
8f177c8e 3439 {
2d66e025 3440 if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
8f177c8e 3441 {
82edfbe7 3442 if ( CanDragRowSize(m_dragRowOrCol) )
ca65c044 3443 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW, m_rowLabelWin, false);
8f177c8e 3444 }
2d66e025 3445 }
6d004f67 3446 else if ( m_cursorMode != WXGRID_CURSOR_SELECT_CELL )
2d66e025 3447 {
ca65c044 3448 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_rowLabelWin, false);
8f177c8e 3449 }
f85afd4e 3450 }
2d66e025
MB
3451}
3452
11393d29
VZ
3453void wxGrid::UpdateColumnSortingIndicator(int col)
3454{
3455 wxCHECK_RET( col != wxNOT_FOUND, "invalid column index" );
3456
3457 if ( m_useNativeHeader )
3039ade9 3458 GetGridColHeader()->UpdateColumn(col);
11393d29
VZ
3459 else if ( m_nativeColumnLabels )
3460 m_colWindow->Refresh();
3461 //else: sorting indicator display not yet implemented in grid version
3462}
3463
3464void wxGrid::SetSortingColumn(int col, bool ascending)
3465{
3466 if ( col == m_sortCol )
3467 {
3468 // we are already using this column for sorting (or not sorting at all)
3469 // but we might still change the sorting order, check for it
3470 if ( m_sortCol != wxNOT_FOUND && ascending != m_sortIsAscending )
3471 {
3472 m_sortIsAscending = ascending;
3473
3474 UpdateColumnSortingIndicator(m_sortCol);
3475 }
3476 }
3477 else // we're changing the column used for sorting
3478 {
3479 const int sortColOld = m_sortCol;
3480
3481 // change it before updating the column as we want GetSortingColumn()
3482 // to return the correct new value
3483 m_sortCol = col;
3484
3485 if ( sortColOld != wxNOT_FOUND )
3486 UpdateColumnSortingIndicator(sortColOld);
3487
3488 if ( m_sortCol != wxNOT_FOUND )
3489 {
3490 m_sortIsAscending = ascending;
3491 UpdateColumnSortingIndicator(m_sortCol);
3492 }
3493 }
3494}
3495
3496void wxGrid::DoColHeaderClick(int col)
3497{
3498 // we consider that the grid was resorted if this event is processed and
3499 // not vetoed
3500 if ( SendEvent(wxEVT_GRID_COL_SORT, -1, col) == 1 )
3501 {
3502 SetSortingColumn(col, IsSortingBy(col) ? !m_sortIsAscending : true);
3503 Refresh();
3504 }
3505}
3506
ad805b9e
VZ
3507void wxGrid::DoStartResizeCol(int col)
3508{
3509 m_dragRowOrCol = col;
3510 m_dragLastPos = -1;
3511 DoUpdateResizeColWidth(GetColWidth(m_dragRowOrCol));
3512}
3513
3514void wxGrid::DoUpdateResizeCol(int x)
3515{
3516 int cw, ch, dummy, top;
3517 m_gridWin->GetClientSize( &cw, &ch );
3518 CalcUnscrolledPosition( 0, 0, &dummy, &top );
3519
3520 wxClientDC dc( m_gridWin );
3521 PrepareDC( dc );
3522
3523 x = wxMax( x, GetColLeft(m_dragRowOrCol) + GetColMinimalWidth(m_dragRowOrCol));
3524 dc.SetLogicalFunction(wxINVERT);
3525 if ( m_dragLastPos >= 0 )
3526 {
3527 dc.DrawLine( m_dragLastPos, top, m_dragLastPos, top + ch );
3528 }
3529 dc.DrawLine( x, top, x, top + ch );
3530 m_dragLastPos = x;
3531}
3532
3533void wxGrid::DoUpdateResizeColWidth(int w)
3534{
3535 DoUpdateResizeCol(GetColLeft(m_dragRowOrCol) + w);
3536}
3537
2d66e025
MB
3538void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent& event )
3539{
7d1214cd
PC
3540 int x;
3541 CalcUnscrolledPosition( event.GetPosition().x, 0, &x, NULL );
60ff3b99 3542
11393d29 3543 int col = XToCol(x);
2d66e025 3544 if ( event.Dragging() )
f85afd4e 3545 {
fe77cf60
JS
3546 if (!m_isDragging)
3547 {
ca65c044 3548 m_isDragging = true;
ad805b9e 3549 GetColLabelWindow()->CaptureMouse();
d4175745 3550
11393d29
VZ
3551 if ( m_cursorMode == WXGRID_CURSOR_MOVE_COL && col != -1 )
3552 DoStartMoveCol(col);
fe77cf60 3553 }
8f177c8e 3554
2d66e025 3555 if ( event.LeftIsDown() )
8f177c8e 3556 {
962a48f6 3557 switch ( m_cursorMode )
8f177c8e 3558 {
2d66e025 3559 case WXGRID_CURSOR_RESIZE_COL:
ad805b9e 3560 DoUpdateResizeCol(x);
2d66e025 3561 break;
f85afd4e 3562
2d66e025 3563 case WXGRID_CURSOR_SELECT_COL:
902725ee 3564 {
11393d29 3565 if ( col != -1 )
aa5e1f75 3566 {
3f3dc2ef 3567 if ( m_selection )
8b5f6d9d 3568 m_selection->SelectCol(col, event);
2d66e025 3569 }
902725ee
WS
3570 }
3571 break;
e2b42eeb 3572
d4175745
VZ
3573 case WXGRID_CURSOR_MOVE_COL:
3574 {
cd68daf5
VZ
3575 int posNew = XToPos(x);
3576 int colNew = GetColAt(posNew);
d4175745 3577
cd68daf5 3578 // determine the position of the drop marker
d4175745 3579 int markerX;
cd68daf5
VZ
3580 if ( x >= GetColLeft(colNew) + (GetColWidth(colNew) / 2) )
3581 markerX = GetColRight(colNew);
d4175745 3582 else
cd68daf5 3583 markerX = GetColLeft(colNew);
d4175745
VZ
3584
3585 if ( markerX != m_dragLastPos )
3586 {
ad805b9e 3587 wxClientDC dc( GetColLabelWindow() );
1eab9659 3588 DoPrepareDC(dc);
d4175745
VZ
3589
3590 int cw, ch;
ad805b9e 3591 GetColLabelWindow()->GetClientSize( &cw, &ch );
d4175745
VZ
3592
3593 markerX++;
3594
3595 //Clean up the last indicator
3596 if ( m_dragLastPos >= 0 )
3597 {
ad805b9e 3598 wxPen pen( GetColLabelWindow()->GetBackgroundColour(), 2 );
d4175745
VZ
3599 dc.SetPen(pen);
3600 dc.DrawLine( m_dragLastPos + 1, 0, m_dragLastPos + 1, ch );
3601 dc.SetPen(wxNullPen);
3602
3603 if ( XToCol( m_dragLastPos ) != -1 )
3604 DrawColLabel( dc, XToCol( m_dragLastPos ) );
3605 }
3606
87c819f9 3607 const wxColour *color;
d4175745 3608 //Moving to the same place? Don't draw a marker
cd68daf5 3609 if ( colNew == m_dragRowOrCol )
87c819f9
VZ
3610 color = wxLIGHT_GREY;
3611 else
3612 color = wxBLUE;
d4175745
VZ
3613
3614 //Draw the marker
87c819f9 3615 wxPen pen( *color, 2 );
d4175745
VZ
3616 dc.SetPen(pen);
3617
3618 dc.DrawLine( markerX, 0, markerX, ch );
3619
3620 dc.SetPen(wxNullPen);
3621
3622 m_dragLastPos = markerX - 1;
3623 }
3624 }
3625 break;
3626
e2b42eeb
VZ
3627 // default label to suppress warnings about "enumeration value
3628 // 'xxx' not handled in switch
3629 default:
3630 break;
2d66e025 3631 }
f85afd4e 3632 }
2d66e025 3633 return;
f85afd4e 3634 }
2d66e025 3635
fe77cf60
JS
3636 if ( m_isDragging && (event.Entering() || event.Leaving()) )
3637 return;
2d66e025 3638
fe77cf60
JS
3639 if (m_isDragging)
3640 {
ad805b9e
VZ
3641 if (GetColLabelWindow()->HasCapture())
3642 GetColLabelWindow()->ReleaseMouse();
ca65c044 3643 m_isDragging = false;
fe77cf60 3644 }
60ff3b99 3645
6d004f67
MB
3646 // ------------ Entering or leaving the window
3647 //
3648 if ( event.Entering() || event.Leaving() )
3649 {
ad805b9e 3650 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, GetColLabelWindow());
6d004f67
MB
3651 }
3652
2d66e025 3653 // ------------ Left button pressed
f85afd4e 3654 //
6d004f67 3655 else if ( event.LeftDown() )
f85afd4e 3656 {
7d1214cd
PC
3657 int colEdge = XToEdgeOfCol(x);
3658 if ( colEdge != wxNOT_FOUND && CanDragColSize(colEdge) )
8f177c8e 3659 {
82edfbe7
VZ
3660 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL, GetColLabelWindow());
3661 }
3662 else // not a request to start resizing
3663 {
2f024384 3664 if ( col >= 0 &&
b54ba671 3665 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK, -1, col, event ) )
8f177c8e 3666 {
d4175745 3667 if ( m_canDragColMove )
3f3dc2ef 3668 {
d4175745 3669 //Show button as pressed
ad805b9e 3670 wxClientDC dc( GetColLabelWindow() );
d4175745
VZ
3671 int colLeft = GetColLeft( col );
3672 int colRight = GetColRight( col ) - 1;
ad805b9e 3673 dc.SetPen( wxPen( GetColLabelWindow()->GetBackgroundColour(), 1 ) );
d4175745
VZ
3674 dc.DrawLine( colLeft, 1, colLeft, m_colLabelHeight-1 );
3675 dc.DrawLine( colLeft, 1, colRight, 1 );
3676
ad805b9e 3677 ChangeCursorMode(WXGRID_CURSOR_MOVE_COL, GetColLabelWindow());
d4175745
VZ
3678 }
3679 else
3680 {
3681 if ( !event.ShiftDown() && !event.CmdDown() )
3682 ClearSelection();
3683 if ( m_selection )
3f3dc2ef 3684 {
d4175745
VZ
3685 if ( event.ShiftDown() )
3686 {
8b5f6d9d
VZ
3687 m_selection->SelectBlock
3688 (
3689 0, m_currentCellCoords.GetCol(),
3690 GetNumberRows() - 1, col,
3691 event
3692 );
d4175745
VZ
3693 }
3694 else
3695 {
8b5f6d9d 3696 m_selection->SelectCol(col, event);
d4175745 3697 }
3f3dc2ef 3698 }
3f3dc2ef 3699
ad805b9e 3700 ChangeCursorMode(WXGRID_CURSOR_SELECT_COL, GetColLabelWindow());
d4175745 3701 }
f85afd4e 3702 }
2d66e025 3703 }
2d66e025 3704 }
f85afd4e 3705
2d66e025
MB
3706 // ------------ Left double click
3707 //
3708 if ( event.LeftDClick() )
3709 {
11393d29
VZ
3710 const int colEdge = XToEdgeOfCol(x);
3711 if ( colEdge == -1 )
2d66e025 3712 {
a967f048
RG
3713 if ( col >= 0 &&
3714 ! SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK, -1, col, event ) )
3715 {
ca65c044 3716 // no default action at the moment
a967f048 3717 }
2d66e025 3718 }
d43851f7
JS
3719 else
3720 {
3721 // adjust column width depending on label text
27bb2c8c
VZ
3722 //
3723 // TODO: generate RESIZING event, see #10754
6f58f3d7
VZ
3724 if ( !SendGridSizeEvent(wxEVT_GRID_COL_AUTO_SIZE, -1, colEdge, event) )
3725 AutoSizeColLabelSize( colEdge );
d43851f7 3726
c5c1ea96 3727 SendGridSizeEvent(wxEVT_GRID_COL_SIZE, -1, colEdge, event);
27bb2c8c 3728
ad805b9e 3729 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, GetColLabelWindow());
ccdee36f 3730 m_dragLastPos = -1;
d43851f7 3731 }
2d66e025 3732 }
60ff3b99 3733
2d66e025
MB
3734 // ------------ Left button released
3735 //
3736 else if ( event.LeftUp() )
3737 {
d4175745 3738 switch ( m_cursorMode )
2d66e025 3739 {
d4175745 3740 case WXGRID_CURSOR_RESIZE_COL:
27bb2c8c 3741 DoEndDragResizeCol(event);
852b6c3c 3742 break;
d4175745
VZ
3743
3744 case WXGRID_CURSOR_MOVE_COL:
11393d29 3745 if ( m_dragLastPos == -1 || col == m_dragRowOrCol )
cd68daf5 3746 {
11393d29
VZ
3747 // the column didn't actually move anywhere
3748 if ( col != -1 )
3749 DoColHeaderClick(col);
cd68daf5
VZ
3750 m_colWindow->Refresh(); // "unpress" the column
3751 }
3752 else
3753 {
7c331059
VZ
3754 // get the position of the column we're over
3755 int pos = XToPos(x);
3756
3757 // we may need to adjust the drop position but don't bother
3758 // checking for it if we can't anyhow
3759 if ( pos > 1 )
3760 {
3761 // also find the index of the column we're over: notice
3762 // that the existing "col" variable may be invalid but
3763 // we need a valid one here
3764 const int colValid = GetColAt(pos);
3765
3766 // if we're on the "near" (usually left but right in
3767 // RTL case) part of the column, the actual position we
3768 // should be placed in is actually the one before it
18c0369b 3769 bool onNearPart;
7c331059
VZ
3770 const int middle = GetColLeft(colValid) +
3771 GetColWidth(colValid)/2;
3772 if ( GetLayoutDirection() == wxLayout_LeftToRight )
18c0369b 3773 onNearPart = (x <= middle);
7c331059 3774 else // wxLayout_RightToLeft
18c0369b 3775 onNearPart = (x > middle);
7c331059 3776
18c0369b 3777 if ( onNearPart )
7c331059
VZ
3778 pos--;
3779 }
3780
3781 DoEndMoveCol(pos);
cd68daf5 3782 }
852b6c3c
VZ
3783 break;
3784
3785 case WXGRID_CURSOR_SELECT_COL:
3786 case WXGRID_CURSOR_SELECT_CELL:
3787 case WXGRID_CURSOR_RESIZE_ROW:
3788 case WXGRID_CURSOR_SELECT_ROW:
11393d29
VZ
3789 if ( col != -1 )
3790 DoColHeaderClick(col);
852b6c3c 3791 break;
2d66e025 3792 }
f85afd4e 3793
ad805b9e 3794 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, GetColLabelWindow());
ccdee36f 3795 m_dragLastPos = -1;
60ff3b99
VZ
3796 }
3797
2d66e025
MB
3798 // ------------ Right button down
3799 //
3800 else if ( event.RightDown() )
3801 {
a967f048 3802 if ( col >= 0 &&
ca65c044 3803 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK, -1, col, event ) )
2d66e025
MB
3804 {
3805 // no default action at the moment
f85afd4e
MB
3806 }
3807 }
60ff3b99 3808
2d66e025 3809 // ------------ Right double click
f85afd4e 3810 //
2d66e025
MB
3811 else if ( event.RightDClick() )
3812 {
a967f048 3813 if ( col >= 0 &&
ca65c044 3814 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK, -1, col, event ) )
2d66e025
MB
3815 {
3816 // no default action at the moment
3817 }
3818 }
60ff3b99 3819
2d66e025 3820 // ------------ No buttons down and mouse moving
f85afd4e 3821 //
2d66e025 3822 else if ( event.Moving() )
f85afd4e 3823 {
2d66e025
MB
3824 m_dragRowOrCol = XToEdgeOfCol( x );
3825 if ( m_dragRowOrCol >= 0 )
f85afd4e 3826 {
2d66e025 3827 if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
f85afd4e 3828 {
82edfbe7 3829 if ( CanDragColSize(m_dragRowOrCol) )
ad805b9e 3830 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL, GetColLabelWindow(), false);
f85afd4e 3831 }
2d66e025 3832 }
6d004f67 3833 else if ( m_cursorMode != WXGRID_CURSOR_SELECT_CELL )
2d66e025 3834 {
ad805b9e 3835 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, GetColLabelWindow(), false);
8f177c8e 3836 }
f85afd4e
MB
3837 }
3838}
3839
2d66e025 3840void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent& event )
f85afd4e 3841{
2d66e025 3842 if ( event.LeftDown() )
f85afd4e 3843 {
2d66e025
MB
3844 // indicate corner label by having both row and
3845 // col args == -1
f85afd4e 3846 //
b54ba671 3847 if ( !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK, -1, -1, event ) )
2d66e025
MB
3848 {
3849 SelectAll();
3850 }
f85afd4e 3851 }
2d66e025
MB
3852 else if ( event.LeftDClick() )
3853 {
b54ba671 3854 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK, -1, -1, event );
2d66e025 3855 }
2d66e025 3856 else if ( event.RightDown() )
f85afd4e 3857 {
b54ba671 3858 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK, -1, -1, event ) )
f85afd4e 3859 {
2d66e025
MB
3860 // no default action at the moment
3861 }
3862 }
2d66e025
MB
3863 else if ( event.RightDClick() )
3864 {
b54ba671 3865 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK, -1, -1, event ) )
2d66e025
MB
3866 {
3867 // no default action at the moment
3868 }
3869 }
3870}
f85afd4e 3871
86033c4b
VZ
3872void wxGrid::CancelMouseCapture()
3873{
3874 // cancel operation currently in progress, whatever it is
3875 if ( m_winCapture )
3876 {
3877 m_isDragging = false;
8a3e536c
VZ
3878 m_startDragPos = wxDefaultPosition;
3879
86033c4b
VZ
3880 m_cursorMode = WXGRID_CURSOR_SELECT_CELL;
3881 m_winCapture->SetCursor( *wxSTANDARD_CURSOR );
3882 m_winCapture = NULL;
3883
3884 // remove traces of whatever we drew on screen
3885 Refresh();
3886 }
3887}
3888
e2b42eeb
VZ
3889void wxGrid::ChangeCursorMode(CursorMode mode,
3890 wxWindow *win,
3891 bool captureMouse)
3892{
711f12ef 3893#if wxUSE_LOG_TRACE
a243da29 3894 static const wxChar *const cursorModes[] =
e2b42eeb 3895 {
9a83f860
VZ
3896 wxT("SELECT_CELL"),
3897 wxT("RESIZE_ROW"),
3898 wxT("RESIZE_COL"),
3899 wxT("SELECT_ROW"),
3900 wxT("SELECT_COL"),
3901 wxT("MOVE_COL"),
e2b42eeb
VZ
3902 };
3903
9a83f860
VZ
3904 wxLogTrace(wxT("grid"),
3905 wxT("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
3906 win == m_colWindow ? wxT("colLabelWin")
3907 : win ? wxT("rowLabelWin")
3908 : wxT("gridWin"),
e2b42eeb 3909 cursorModes[m_cursorMode], cursorModes[mode]);
711f12ef 3910#endif // wxUSE_LOG_TRACE
e2b42eeb 3911
faec5a43
SN
3912 if ( mode == m_cursorMode &&
3913 win == m_winCapture &&
3914 captureMouse == (m_winCapture != NULL))
e2b42eeb
VZ
3915 return;
3916
3917 if ( !win )
3918 {
3919 // by default use the grid itself
3920 win = m_gridWin;
3921 }
3922
3923 if ( m_winCapture )
3924 {
e882d288 3925 m_winCapture->ReleaseMouse();
10a4531d 3926 m_winCapture = NULL;
e2b42eeb
VZ
3927 }
3928
3929 m_cursorMode = mode;
3930
3931 switch ( m_cursorMode )
3932 {
3933 case WXGRID_CURSOR_RESIZE_ROW:
3934 win->SetCursor( m_rowResizeCursor );
3935 break;
3936
3937 case WXGRID_CURSOR_RESIZE_COL:
3938 win->SetCursor( m_colResizeCursor );
3939 break;
3940
d4175745
VZ
3941 case WXGRID_CURSOR_MOVE_COL:
3942 win->SetCursor( wxCursor(wxCURSOR_HAND) );
3943 break;
3944
e2b42eeb
VZ
3945 default:
3946 win->SetCursor( *wxSTANDARD_CURSOR );
2f024384 3947 break;
e2b42eeb
VZ
3948 }
3949
3950 // we need to capture mouse when resizing
3951 bool resize = m_cursorMode == WXGRID_CURSOR_RESIZE_ROW ||
3952 m_cursorMode == WXGRID_CURSOR_RESIZE_COL;
3953
3954 if ( captureMouse && resize )
3955 {
3956 win->CaptureMouse();
3957 m_winCapture = win;
3958 }
3959}
8f177c8e 3960
8a3e536c
VZ
3961// ----------------------------------------------------------------------------
3962// grid mouse event processing
3963// ----------------------------------------------------------------------------
60ff3b99 3964
0cbcb12d 3965bool
8a3e536c
VZ
3966wxGrid::DoGridCellDrag(wxMouseEvent& event,
3967 const wxGridCellCoords& coords,
3968 bool isFirstDrag)
3969{
0cbcb12d
SC
3970 bool performDefault = true ;
3971
8a3e536c 3972 if ( coords == wxGridNoCellCoords )
0cbcb12d 3973 return performDefault; // we're outside any valid cell
60ff3b99 3974
8a3e536c
VZ
3975 // Hide the edit control, so it won't interfere with drag-shrinking.
3976 if ( IsCellEditControlShown() )
27f35b66 3977 {
8a3e536c
VZ
3978 HideCellEditControl();
3979 SaveEditControlValue();
27f35b66
SN
3980 }
3981
8a3e536c 3982 switch ( event.GetModifiers() )
2d66e025 3983 {
3bc6d534 3984 case wxMOD_CONTROL:
8a3e536c
VZ
3985 if ( m_selectedBlockCorner == wxGridNoCellCoords)
3986 m_selectedBlockCorner = coords;
3987 UpdateBlockBeingSelected(m_selectedBlockCorner, coords);
3988 break;
07296f0b 3989
8a3e536c
VZ
3990 case wxMOD_NONE:
3991 if ( CanDragCell() )
2d66e025 3992 {
8a3e536c 3993 if ( isFirstDrag )
79dbea21 3994 {
8a3e536c
VZ
3995 if ( m_selectedBlockCorner == wxGridNoCellCoords)
3996 m_selectedBlockCorner = coords;
07296f0b 3997
0cbcb12d 3998 // if event is handled by user code, no further processing
c118fa5f 3999 if ( SendEvent(wxEVT_GRID_CELL_BEGIN_DRAG, coords, event) != 0 )
0cbcb12d
SC
4000 performDefault = false;
4001
4002 return performDefault;
07296f0b 4003 }
2d66e025 4004 }
25107357 4005
8a3e536c
VZ
4006 UpdateBlockBeingSelected(m_currentCellCoords, coords);
4007 break;
c71b2126 4008
8a3e536c
VZ
4009 default:
4010 // we don't handle the other key modifiers
4011 event.Skip();
4012 }
0cbcb12d
SC
4013
4014 return performDefault;
8a3e536c 4015}
8f177c8e 4016
8a3e536c
VZ
4017void wxGrid::DoGridLineDrag(wxMouseEvent& event, const wxGridOperations& oper)
4018{
4019 wxClientDC dc(m_gridWin);
4020 PrepareDC(dc);
4021 dc.SetLogicalFunction(wxINVERT);
e2b42eeb 4022
8a3e536c
VZ
4023 const wxRect rectWin(CalcUnscrolledPosition(wxPoint(0, 0)),
4024 m_gridWin->GetClientSize());
4025
4026 // erase the previously drawn line, if any
4027 if ( m_dragLastPos >= 0 )
4028 oper.DrawParallelLineInRect(dc, rectWin, m_dragLastPos);
4029
4030 // we need the vertical position for rows and horizontal for columns here
4031 m_dragLastPos = oper.Dual().Select(CalcUnscrolledPosition(event.GetPosition()));
4032
4033 // don't allow resizing beneath the minimal size
4034 const int posMin = oper.GetLineStartPos(this, m_dragRowOrCol) +
4035 oper.GetMinimalLineSize(this, m_dragRowOrCol);
4036 if ( m_dragLastPos < posMin )
4037 m_dragLastPos = posMin;
4038
4039 // and draw it at the new position
4040 oper.DrawParallelLineInRect(dc, rectWin, m_dragLastPos);
4041}
4042
4043void wxGrid::DoGridDragEvent(wxMouseEvent& event, const wxGridCellCoords& coords)
4044{
4045 if ( !m_isDragging )
4046 {
4047 // Don't start doing anything until the mouse has been dragged far
4048 // enough
4049 const wxPoint& pt = event.GetPosition();
4050 if ( m_startDragPos == wxDefaultPosition )
4051 {
4052 m_startDragPos = pt;
4053 return;
6d004f67 4054 }
e2b42eeb 4055
8a3e536c
VZ
4056 if ( abs(m_startDragPos.x - pt.x) <= DRAG_SENSITIVITY &&
4057 abs(m_startDragPos.y - pt.y) <= DRAG_SENSITIVITY )
4058 return;
2d66e025 4059 }
66242c80 4060
8a3e536c
VZ
4061 const bool isFirstDrag = !m_isDragging;
4062 m_isDragging = true;
07296f0b 4063
8a3e536c 4064 switch ( m_cursorMode )
a5777624 4065 {
8a3e536c 4066 case WXGRID_CURSOR_SELECT_CELL:
0cbcb12d
SC
4067 // no further handling if handled by user
4068 if ( DoGridCellDrag(event, coords, isFirstDrag) == false )
4069 return;
8a3e536c
VZ
4070 break;
4071
4072 case WXGRID_CURSOR_RESIZE_ROW:
4073 DoGridLineDrag(event, wxGridRowOperations());
4074 break;
4075
4076 case WXGRID_CURSOR_RESIZE_COL:
4077 DoGridLineDrag(event, wxGridColumnOperations());
4078 break;
4079
4080 default:
4081 event.Skip();
a5777624 4082 }
e2b42eeb 4083
8a3e536c 4084 if ( isFirstDrag )
f6bcfd97 4085 {
98e95650
VZ
4086 wxASSERT_MSG( !m_winCapture, "shouldn't capture the mouse twice" );
4087
8a3e536c
VZ
4088 m_winCapture = m_gridWin;
4089 m_winCapture->CaptureMouse();
4090 }
4091}
a5777624 4092
8a3e536c
VZ
4093void
4094wxGrid::DoGridCellLeftDown(wxMouseEvent& event,
4095 const wxGridCellCoords& coords,
4096 const wxPoint& pos)
4097{
4098 if ( SendEvent(wxEVT_GRID_CELL_LEFT_CLICK, coords, event) )
4099 {
4100 // event handled by user code, no need to do anything here
4101 return;
a5777624 4102 }
f85afd4e 4103
8a3e536c
VZ
4104 if ( !event.CmdDown() )
4105 ClearSelection();
4106
4107 if ( event.ShiftDown() )
4108 {
4109 if ( m_selection )
4110 {
8b5f6d9d 4111 m_selection->SelectBlock(m_currentCellCoords, coords, event);
8a3e536c
VZ
4112 m_selectedBlockCorner = coords;
4113 }
4114 }
4115 else if ( XToEdgeOfCol(pos.x) < 0 && YToEdgeOfRow(pos.y) < 0 )
a5777624
RD
4116 {
4117 DisableCellEditControl();
8a3e536c 4118 MakeCellVisible( coords );
a5777624 4119
8a3e536c 4120 if ( event.CmdDown() )
58dd5b3b 4121 {
8a3e536c 4122 if ( m_selection )
1ef49ab5 4123 {
8b5f6d9d 4124 m_selection->ToggleCellSelection(coords, event);
1ef49ab5 4125 }
8b5f6d9d 4126
8a3e536c
VZ
4127 m_selectedBlockTopLeft = wxGridNoCellCoords;
4128 m_selectedBlockBottomRight = wxGridNoCellCoords;
4129 m_selectedBlockCorner = coords;
4130 }
4131 else
4132 {
e196db7b
VZ
4133 if ( m_selection )
4134 {
4135 // In row or column selection mode just clicking on the cell
4136 // should select the row or column containing it: this is more
4137 // convenient for the kinds of controls that use such selection
4138 // mode and is compatible with 2.8 behaviour (see #12062).
4139 switch ( m_selection->GetSelectionMode() )
4140 {
4141 case wxGridSelectCells:
4142 case wxGridSelectRowsOrColumns:
4143 // nothing to do in these cases
4144 break;
4145
4146 case wxGridSelectRows:
4147 m_selection->SelectRow(coords.GetRow());
4148 break;
4149
4150 case wxGridSelectColumns:
4151 m_selection->SelectCol(coords.GetCol());
4152 break;
4153 }
4154 }
4155
8a3e536c
VZ
4156 m_waitForSlowClick = m_currentCellCoords == coords &&
4157 coords != wxGridNoCellCoords;
4158 SetCurrentCell( coords );
58dd5b3b 4159 }
a5777624 4160 }
8a3e536c 4161}
f85afd4e 4162
8a3e536c
VZ
4163void
4164wxGrid::DoGridCellLeftDClick(wxMouseEvent& event,
4165 const wxGridCellCoords& coords,
4166 const wxPoint& pos)
4167{
4168 if ( XToEdgeOfCol(pos.x) < 0 && YToEdgeOfRow(pos.y) < 0 )
a5777624 4169 {
8a3e536c 4170 if ( !SendEvent(wxEVT_GRID_CELL_LEFT_DCLICK, coords, event) )
f85afd4e 4171 {
8a3e536c
VZ
4172 // we want double click to select a cell and start editing
4173 // (i.e. to behave in same way as sequence of two slow clicks):
4174 m_waitForSlowClick = true;
4175 }
4176 }
4177}
932b55d0 4178
8a3e536c
VZ
4179void
4180wxGrid::DoGridCellLeftUp(wxMouseEvent& event, const wxGridCellCoords& coords)
4181{
4182 if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
4183 {
4184 if (m_winCapture)
4185 {
e882d288 4186 m_winCapture->ReleaseMouse();
8a3e536c
VZ
4187 m_winCapture = NULL;
4188 }
932b55d0 4189
8a3e536c
VZ
4190 if ( coords == m_currentCellCoords && m_waitForSlowClick && CanEnableCellControl() )
4191 {
4192 ClearSelection();
4193 EnableCellEditControl();
3f3dc2ef 4194
8a3e536c
VZ
4195 wxGridCellAttr *attr = GetCellAttr(coords);
4196 wxGridCellEditor *editor = attr->GetEditor(this, coords.GetRow(), coords.GetCol());
4197 editor->StartingClick();
4198 editor->DecRef();
4199 attr->DecRef();
2d66e025 4200
8a3e536c 4201 m_waitForSlowClick = false;
a5777624 4202 }
8a3e536c
VZ
4203 else if ( m_selectedBlockTopLeft != wxGridNoCellCoords &&
4204 m_selectedBlockBottomRight != wxGridNoCellCoords )
a5777624 4205 {
8a3e536c
VZ
4206 if ( m_selection )
4207 {
4208 m_selection->SelectBlock( m_selectedBlockTopLeft,
4209 m_selectedBlockBottomRight,
8b5f6d9d 4210 event );
8a3e536c 4211 }
e2b42eeb 4212
8a3e536c
VZ
4213 m_selectedBlockTopLeft = wxGridNoCellCoords;
4214 m_selectedBlockBottomRight = wxGridNoCellCoords;
e2b42eeb 4215
8a3e536c
VZ
4216 // Show the edit control, if it has been hidden for
4217 // drag-shrinking.
4218 ShowCellEditControl();
58dd5b3b 4219 }
8a3e536c
VZ
4220 }
4221 else if ( m_cursorMode == WXGRID_CURSOR_RESIZE_ROW )
4222 {
4223 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL);
27bb2c8c 4224 DoEndDragResizeRow(event);
a5777624 4225 }
8a3e536c
VZ
4226 else if ( m_cursorMode == WXGRID_CURSOR_RESIZE_COL )
4227 {
4228 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL);
27bb2c8c 4229 DoEndDragResizeCol(event);
8a3e536c
VZ
4230 }
4231
4232 m_dragLastPos = -1;
4233}
4234
4235void
4236wxGrid::DoGridMouseMoveEvent(wxMouseEvent& WXUNUSED(event),
4237 const wxGridCellCoords& coords,
4238 const wxPoint& pos)
4239{
4240 if ( coords.GetRow() < 0 || coords.GetCol() < 0 )
a5777624 4241 {
8a3e536c
VZ
4242 // out of grid cell area
4243 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL);
4244 return;
a5777624 4245 }
2d66e025 4246
8a3e536c
VZ
4247 int dragRow = YToEdgeOfRow( pos.y );
4248 int dragCol = XToEdgeOfCol( pos.x );
4249
4250 // Dragging on the corner of a cell to resize in both
4251 // directions is not implemented yet...
a5777624 4252 //
8a3e536c 4253 if ( dragRow >= 0 && dragCol >= 0 )
a5777624 4254 {
8a3e536c
VZ
4255 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL);
4256 return;
4257 }
4258
82edfbe7 4259 if ( dragRow >= 0 && CanDragGridSize() && CanDragRowSize(dragRow) )
8a3e536c 4260 {
8a3e536c 4261 if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
f85afd4e 4262 {
82edfbe7
VZ
4263 m_dragRowOrCol = dragRow;
4264 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW, NULL, false);
60ff3b99 4265 }
a5777624 4266 }
ad805b9e
VZ
4267 // When using the native header window we can only resize the columns by
4268 // dragging the dividers in it because we can't make it enter into the
4269 // column resizing mode programmatically
82edfbe7
VZ
4270 else if ( dragCol >= 0 && !m_useNativeHeader &&
4271 CanDragGridSize() && CanDragColSize(dragCol) )
8a3e536c 4272 {
8a3e536c
VZ
4273 if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
4274 {
82edfbe7
VZ
4275 m_dragRowOrCol = dragCol;
4276 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL, NULL, false);
8a3e536c
VZ
4277 }
4278 }
4279 else // Neither on a row or col edge
a5777624 4280 {
8a3e536c 4281 if ( m_cursorMode != WXGRID_CURSOR_SELECT_CELL )
d57ad377 4282 {
d57ad377 4283 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL);
d57ad377 4284 }
8a3e536c
VZ
4285 }
4286}
d57ad377 4287
8a3e536c
VZ
4288void wxGrid::ProcessGridCellMouseEvent(wxMouseEvent& event)
4289{
98e95650
VZ
4290 if ( event.Entering() || event.Leaving() )
4291 {
4292 // we don't care about these events but we must not reset m_isDragging
4293 // if they happen so return before anything else is done
4294 event.Skip();
4295 return;
4296 }
4297
8a3e536c 4298 const wxPoint pos = CalcUnscrolledPosition(event.GetPosition());
790cc417 4299
8a3e536c
VZ
4300 // coordinates of the cell under mouse
4301 wxGridCellCoords coords = XYToCell(pos);
6d004f67 4302
8a3e536c
VZ
4303 int cell_rows, cell_cols;
4304 GetCellSize( coords.GetRow(), coords.GetCol(), &cell_rows, &cell_cols );
4305 if ( (cell_rows < 0) || (cell_cols < 0) )
4306 {
4307 coords.SetRow(coords.GetRow() + cell_rows);
4308 coords.SetCol(coords.GetCol() + cell_cols);
4309 }
6d004f67 4310
8a3e536c
VZ
4311 if ( event.Dragging() )
4312 {
4313 if ( event.LeftIsDown() )
4314 DoGridDragEvent(event, coords);
4315 else
4316 event.Skip();
4317 return;
4318 }
4319
4320 m_isDragging = false;
4321 m_startDragPos = wxDefaultPosition;
4322
8a3e536c
VZ
4323 // deal with various button presses
4324 if ( event.IsButton() )
4325 {
4326 if ( coords != wxGridNoCellCoords )
a5777624 4327 {
8a3e536c 4328 DisableCellEditControl();
e2b42eeb 4329
8a3e536c
VZ
4330 if ( event.LeftDown() )
4331 DoGridCellLeftDown(event, coords, pos);
4332 else if ( event.LeftDClick() )
4333 DoGridCellLeftDClick(event, coords, pos);
4334 else if ( event.RightDown() )
4335 SendEvent(wxEVT_GRID_CELL_RIGHT_CLICK, coords, event);
4336 else if ( event.RightDClick() )
4337 SendEvent(wxEVT_GRID_CELL_RIGHT_DCLICK, coords, event);
6d004f67 4338 }
8a3e536c
VZ
4339
4340 // this one should be called even if we're not over any cell
4341 if ( event.LeftUp() )
a5777624 4342 {
8a3e536c 4343 DoGridCellLeftUp(event, coords);
a5777624
RD
4344 }
4345 }
8a3e536c
VZ
4346 else if ( event.Moving() )
4347 {
4348 DoGridMouseMoveEvent(event, coords, pos);
4349 }
4350 else // unknown mouse event?
4351 {
4352 event.Skip();
4353 }
6d004f67
MB
4354}
4355
27bb2c8c
VZ
4356// this function returns true only if the size really changed
4357bool wxGrid::DoEndDragResizeLine(const wxGridOperations& oper)
6d004f67 4358{
bec70262 4359 if ( m_dragLastPos == -1 )
27bb2c8c 4360 return false;
6d004f67 4361
bec70262 4362 const wxGridOperations& doper = oper.Dual();
6d004f67 4363
bec70262 4364 const wxSize size = m_gridWin->GetClientSize();
e2b42eeb 4365
bec70262 4366 const wxPoint ptOrigin = CalcUnscrolledPosition(wxPoint(0, 0));
ccdee36f 4367
bec70262
VZ
4368 // erase the last line we drew
4369 wxClientDC dc(m_gridWin);
4370 PrepareDC(dc);
4371 dc.SetLogicalFunction(wxINVERT);
6d004f67 4372
bec70262
VZ
4373 const int posLineStart = oper.Select(ptOrigin);
4374 const int posLineEnd = oper.Select(ptOrigin) + oper.Select(size);
6d004f67 4375
bec70262 4376 oper.DrawParallelLine(dc, posLineStart, posLineEnd, m_dragLastPos);
6d004f67 4377
bec70262
VZ
4378 // temporarily hide the edit control before resizing
4379 HideCellEditControl();
4380 SaveEditControlValue();
4381
4382 // do resize the line
4383 const int lineStart = oper.GetLineStartPos(this, m_dragRowOrCol);
27bb2c8c 4384 const int lineSizeOld = oper.GetLineSize(this, m_dragRowOrCol);
bec70262
VZ
4385 oper.SetLineSize(this, m_dragRowOrCol,
4386 wxMax(m_dragLastPos - lineStart,
4387 oper.GetMinimalLineSize(this, m_dragRowOrCol)));
27bb2c8c
VZ
4388 const bool
4389 sizeChanged = oper.GetLineSize(this, m_dragRowOrCol) != lineSizeOld;
bec70262 4390
ad805b9e
VZ
4391 m_dragLastPos = -1;
4392
bec70262
VZ
4393 // refresh now if we're not frozen
4394 if ( !GetBatchCount() )
6d004f67 4395 {
bec70262
VZ
4396 // we need to refresh everything beyond the resized line in the header
4397 // window
6d004f67 4398
bec70262
VZ
4399 // get the position from which to refresh in the other direction
4400 wxRect rect(CellToRect(oper.MakeCoords(m_dragRowOrCol, 0)));
4401 rect.SetPosition(CalcScrolledPosition(rect.GetPosition()));
6d004f67 4402
bec70262
VZ
4403 // we only need the ordinate (for rows) or abscissa (for columns) here,
4404 // and need to cover the entire window in the other direction
4405 oper.Select(rect) = 0;
6d004f67 4406
bec70262
VZ
4407 wxRect rectHeader(rect.GetPosition(),
4408 oper.MakeSize
4409 (
4410 oper.GetHeaderWindowSize(this),
4411 doper.Select(size) - doper.Select(rect)
4412 ));
4413
4414 oper.GetHeaderWindow(this)->Refresh(true, &rectHeader);
4415
4416
4417 // also refresh the grid window: extend the rectangle
4418 if ( m_table )
6d004f67 4419 {
bec70262 4420 oper.SelectSize(rect) = oper.Select(size);
2f024384 4421
bec70262 4422 int subtractLines = 0;
7d1214cd
PC
4423 int line = doper.PosToLine(this, posLineStart);
4424 if ( line >= 0 )
27f35b66 4425 {
bec70262
VZ
4426 // ensure that if we have a multi-cell block we redraw all of
4427 // it by increasing the refresh area to cover it entirely if a
4428 // part of it is affected
13350621 4429 const int lineEnd = doper.PosToLine(this, posLineEnd, true);
7d1214cd 4430 for ( ; line < lineEnd; line++ )
27f35b66 4431 {
bec70262
VZ
4432 int cellLines = oper.Select(
4433 GetCellSize(oper.MakeCoords(m_dragRowOrCol, line)));
4434 if ( cellLines < subtractLines )
4435 subtractLines = cellLines;
27f35b66
SN
4436 }
4437 }
2f024384 4438
bec70262
VZ
4439 int startPos =
4440 oper.GetLineStartPos(this, m_dragRowOrCol + subtractLines);
4441 startPos = doper.CalcScrolledPosition(this, startPos);
4442
4443 doper.Select(rect) = startPos;
4444 doper.SelectSize(rect) = doper.Select(size) - startPos;
e2b42eeb 4445
bec70262
VZ
4446 m_gridWin->Refresh(false, &rect);
4447 }
f85afd4e 4448 }
bec70262
VZ
4449
4450 // show the edit control back again
4451 ShowCellEditControl();
27bb2c8c
VZ
4452
4453 return sizeChanged;
bec70262
VZ
4454}
4455
27bb2c8c 4456void wxGrid::DoEndDragResizeRow(const wxMouseEvent& event)
bec70262 4457{
27bb2c8c
VZ
4458 // TODO: generate RESIZING event, see #10754
4459
4460 if ( DoEndDragResizeLine(wxGridRowOperations()) )
c5c1ea96 4461 SendGridSizeEvent(wxEVT_GRID_ROW_SIZE, m_dragRowOrCol, -1, event);
bec70262
VZ
4462}
4463
27bb2c8c 4464void wxGrid::DoEndDragResizeCol(const wxMouseEvent& event)
bec70262 4465{
27bb2c8c 4466 // TODO: generate RESIZING event, see #10754
ad805b9e 4467
27bb2c8c 4468 if ( DoEndDragResizeLine(wxGridColumnOperations()) )
c5c1ea96 4469 SendGridSizeEvent(wxEVT_GRID_COL_SIZE, -1, m_dragRowOrCol, event);
f85afd4e
MB
4470}
4471
cd68daf5 4472void wxGrid::DoStartMoveCol(int col)
d4175745 4473{
cd68daf5
VZ
4474 m_dragRowOrCol = col;
4475}
d4175745 4476
cd68daf5
VZ
4477void wxGrid::DoEndMoveCol(int pos)
4478{
4479 wxASSERT_MSG( m_dragRowOrCol != -1, "no matching DoStartMoveCol?" );
4480
4481 if ( SendEvent(wxEVT_GRID_COL_MOVE, -1, m_dragRowOrCol) != -1 )
4482 SetColPos(m_dragRowOrCol, pos);
4483 //else: vetoed by user
d4175745 4484
cd68daf5 4485 m_dragRowOrCol = -1;
d4175745
VZ
4486}
4487
4797b014 4488void wxGrid::RefreshAfterColPosChange()
009c7216 4489{
4797b014
VZ
4490 // recalculate the column rights as the column positions have changed,
4491 // unless we calculate them dynamically because all columns widths are the
4492 // same and it's easy to do
4493 if ( !m_colWidths.empty() )
009c7216 4494 {
4797b014
VZ
4495 int colRight = 0;
4496 for ( int colPos = 0; colPos < m_numCols; colPos++ )
4497 {
4498 int colID = GetColAt( colPos );
4499
4500 colRight += m_colWidths[colID];
4501 m_colRights[colID] = colRight;
4502 }
4503 }
009c7216 4504
4797b014
VZ
4505 // and make the changes visible
4506 if ( m_useNativeHeader )
4507 {
4508 if ( m_colAt.empty() )
3039ade9 4509 GetGridColHeader()->ResetColumnsOrder();
4797b014 4510 else
3039ade9 4511 GetGridColHeader()->SetColumnsOrder(m_colAt);
4797b014
VZ
4512 }
4513 else
4514 {
4515 m_colWindow->Refresh();
009c7216 4516 }
4797b014 4517 m_gridWin->Refresh();
009c7216
VZ
4518}
4519
31ec8b4e
VZ
4520void wxGrid::SetColumnsOrder(const wxArrayInt& order)
4521{
4522 m_colAt = order;
4523
4524 RefreshAfterColPosChange();
4525}
4526
3169a8e8 4527void wxGrid::SetColPos(int idx, int pos)
d4175745 4528{
1bb74626 4529 // we're going to need m_colAt now, initialize it if needed
3169a8e8 4530 if ( m_colAt.empty() )
d4175745 4531 {
3169a8e8
VZ
4532 m_colAt.reserve(m_numCols);
4533 for ( int i = 0; i < m_numCols; i++ )
4534 m_colAt.push_back(i);
d4175745
VZ
4535 }
4536
1bb74626 4537 wxHeaderCtrl::MoveColumnInOrderArray(m_colAt, idx, pos);
d4175745 4538
4797b014 4539 RefreshAfterColPosChange();
d4175745
VZ
4540}
4541
009c7216
VZ
4542void wxGrid::ResetColPos()
4543{
4544 m_colAt.clear();
da5a641f 4545
4797b014 4546 RefreshAfterColPosChange();
009c7216 4547}
d4175745
VZ
4548
4549void wxGrid::EnableDragColMove( bool enable )
4550{
4551 if ( m_canDragColMove == enable )
4552 return;
4553
009c7216 4554 if ( m_useNativeHeader )
d4175745 4555 {
009c7216 4556 // update all columns to make them [not] reorderable
3039ade9 4557 GetGridColHeader()->SetColumnCount(m_numCols);
009c7216 4558 }
d4175745 4559
009c7216 4560 m_canDragColMove = enable;
d4175745 4561
009c7216
VZ
4562 // we use to call ResetColPos() from here if !enable but this doesn't seem
4563 // right as it would mean there would be no way to "freeze" the current
4564 // columns order by disabling moving them after putting them in the desired
4565 // order, whereas now you can always call ResetColPos() manually if needed
d4175745
VZ
4566}
4567
4568
2d66e025
MB
4569//
4570// ------ interaction with data model
4571//
4572bool wxGrid::ProcessTableMessage( wxGridTableMessage& msg )
f85afd4e 4573{
2d66e025 4574 switch ( msg.GetId() )
17732cec 4575 {
2d66e025
MB
4576 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES:
4577 return GetModelValues();
17732cec 4578
2d66e025
MB
4579 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES:
4580 return SetModelValues();
f85afd4e 4581
2d66e025
MB
4582 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED:
4583 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED:
4584 case wxGRIDTABLE_NOTIFY_ROWS_DELETED:
4585 case wxGRIDTABLE_NOTIFY_COLS_INSERTED:
4586 case wxGRIDTABLE_NOTIFY_COLS_APPENDED:
4587 case wxGRIDTABLE_NOTIFY_COLS_DELETED:
4588 return Redimension( msg );
4589
4590 default:
ca65c044 4591 return false;
f85afd4e 4592 }
2d66e025 4593}
f85afd4e 4594
2d66e025 4595// The behaviour of this function depends on the grid table class
5b061713 4596// Clear() function. For the default wxGridStringTable class the
10a4531d 4597// behaviour is to replace all cell contents with wxEmptyString but
2d66e025
MB
4598// not to change the number of rows or cols.
4599//
4600void wxGrid::ClearGrid()
4601{
4602 if ( m_table )
f85afd4e 4603 {
4cfa5de6
RD
4604 if (IsCellEditControlEnabled())
4605 DisableCellEditControl();
4606
2d66e025 4607 m_table->Clear();
5b061713 4608 if (!GetBatchCount())
a9339fe2 4609 m_gridWin->Refresh();
f85afd4e
MB
4610 }
4611}
4612
10a4531d
VZ
4613bool
4614wxGrid::DoModifyLines(bool (wxGridTableBase::*funcModify)(size_t, size_t),
4615 int pos, int num, bool WXUNUSED(updateLabels) )
f85afd4e 4616{
10a4531d 4617 wxCHECK_MSG( m_created, false, "must finish creating the grid first" );
f85afd4e 4618
10a4531d 4619 if ( !m_table )
ca65c044 4620 return false;
f85afd4e 4621
10a4531d
VZ
4622 if ( IsCellEditControlEnabled() )
4623 DisableCellEditControl();
b7fff980 4624
10a4531d 4625 return (m_table->*funcModify)(pos, num);
2f024384 4626
10a4531d
VZ
4627 // the table will have sent the results of the insert row
4628 // operation to this view object as a grid table message
f85afd4e
MB
4629}
4630
10a4531d
VZ
4631bool
4632wxGrid::DoAppendLines(bool (wxGridTableBase::*funcAppend)(size_t),
4633 int num, bool WXUNUSED(updateLabels))
f85afd4e 4634{
10a4531d 4635 wxCHECK_MSG( m_created, false, "must finish creating the grid first" );
2f024384 4636
10a4531d 4637 if ( !m_table )
ca65c044 4638 return false;
f85afd4e 4639
10a4531d 4640 return (m_table->*funcAppend)(num);
f85afd4e
MB
4641}
4642
27bb2c8c
VZ
4643// ----------------------------------------------------------------------------
4644// event generation helpers
4645// ----------------------------------------------------------------------------
4646
6f58f3d7 4647bool
c5c1ea96 4648wxGrid::SendGridSizeEvent(wxEventType type,
27bb2c8c
VZ
4649 int row, int col,
4650 const wxMouseEvent& mouseEv)
4651{
4652 int rowOrCol = row == -1 ? col : row;
4653
4654 wxGridSizeEvent gridEvt( GetId(),
4655 type,
4656 this,
4657 rowOrCol,
4658 mouseEv.GetX() + GetRowLabelSize(),
4659 mouseEv.GetY() + GetColLabelSize(),
4660 mouseEv);
4661
6f58f3d7 4662 return GetEventHandler()->ProcessEvent(gridEvt);
27bb2c8c 4663}
8f177c8e 4664
8a3e536c
VZ
4665// Generate a grid event based on a mouse event and return:
4666// -1 if the event was vetoed
4667// +1 if the event was processed (but not vetoed)
4668// 0 if the event wasn't handled
4669int
aced1133 4670wxGrid::SendEvent(const wxEventType type,
8a3e536c 4671 int row, int col,
27bb2c8c 4672 const wxMouseEvent& mouseEv)
2d66e025 4673{
2f024384 4674 bool claimed, vetoed;
fe7b9ed6 4675
27bb2c8c 4676 if ( type == wxEVT_GRID_RANGE_SELECT )
97a9929e
VZ
4677 {
4678 // Right now, it should _never_ end up here!
4679 wxGridRangeSelectEvent gridEvt( GetId(),
4680 type,
4681 this,
8a3e536c
VZ
4682 m_selectedBlockTopLeft,
4683 m_selectedBlockBottomRight,
ca65c044 4684 true,
8b5f6d9d 4685 mouseEv);
97a9929e
VZ
4686
4687 claimed = GetEventHandler()->ProcessEvent(gridEvt);
4688 vetoed = !gridEvt.IsAllowed();
4689 }
2b73a34e
RD
4690 else if ( type == wxEVT_GRID_LABEL_LEFT_CLICK ||
4691 type == wxEVT_GRID_LABEL_LEFT_DCLICK ||
4692 type == wxEVT_GRID_LABEL_RIGHT_CLICK ||
4693 type == wxEVT_GRID_LABEL_RIGHT_DCLICK )
4694 {
4695 wxPoint pos = mouseEv.GetPosition();
4696
4697 if ( mouseEv.GetEventObject() == GetGridRowLabelWindow() )
4698 pos.y += GetColLabelSize();
4699 if ( mouseEv.GetEventObject() == GetGridColLabelWindow() )
4700 pos.x += GetRowLabelSize();
c71b2126 4701
2b73a34e
RD
4702 wxGridEvent gridEvt( GetId(),
4703 type,
4704 this,
4705 row, col,
4706 pos.x,
4707 pos.y,
4708 false,
8b5f6d9d 4709 mouseEv);
2b73a34e
RD
4710 claimed = GetEventHandler()->ProcessEvent(gridEvt);
4711 vetoed = !gridEvt.IsAllowed();
4712 }
fe7b9ed6 4713 else
97a9929e
VZ
4714 {
4715 wxGridEvent gridEvt( GetId(),
4716 type,
4717 this,
4718 row, col,
4719 mouseEv.GetX() + GetRowLabelSize(),
4720 mouseEv.GetY() + GetColLabelSize(),
ca65c044 4721 false,
8b5f6d9d 4722 mouseEv);
0cbcb12d
SC
4723
4724 if ( type == wxEVT_GRID_CELL_BEGIN_DRAG )
4725 {
4726 // by default the dragging is not supported, the user code must
4727 // explicitly allow the event for it to take place
4728 gridEvt.Veto();
4729 }
4730
97a9929e
VZ
4731 claimed = GetEventHandler()->ProcessEvent(gridEvt);
4732 vetoed = !gridEvt.IsAllowed();
4733 }
4734
4735 // A Veto'd event may not be `claimed' so test this first
4db6714b
KH
4736 if (vetoed)
4737 return -1;
4738
97a9929e 4739 return claimed ? 1 : 0;
f85afd4e
MB
4740}
4741
8a3e536c 4742// Generate a grid event of specified type, return value same as above
f85afd4e 4743//
763163a8
VZ
4744int
4745wxGrid::SendEvent(const wxEventType type, int row, int col, const wxString& s)
f85afd4e 4746{
27bb2c8c
VZ
4747 wxGridEvent gridEvt( GetId(), type, this, row, col );
4748 gridEvt.SetString(s);
8f177c8e 4749
27bb2c8c 4750 const bool claimed = GetEventHandler()->ProcessEvent(gridEvt);
fe7b9ed6 4751
97a9929e 4752 // A Veto'd event may not be `claimed' so test this first
27bb2c8c 4753 if ( !gridEvt.IsAllowed() )
4db6714b
KH
4754 return -1;
4755
97a9929e 4756 return claimed ? 1 : 0;
f85afd4e
MB
4757}
4758
2d66e025 4759void wxGrid::OnPaint( wxPaintEvent& WXUNUSED(event) )
f85afd4e 4760{
3d59537f
DS
4761 // needed to prevent zillions of paint events on MSW
4762 wxPaintDC dc(this);
8f177c8e 4763}
f85afd4e 4764
bca7bfc8 4765void wxGrid::Refresh(bool eraseb, const wxRect* rect)
27f35b66 4766{
ad603bf7
VZ
4767 // Don't do anything if between Begin/EndBatch...
4768 // EndBatch() will do all this on the last nested one anyway.
f9549841 4769 if ( m_created && !GetBatchCount() )
27f35b66 4770 {
bca7bfc8 4771 // Refresh to get correct scrolled position:
4db6714b 4772 wxScrolledWindow::Refresh(eraseb, rect);
27f35b66 4773
27f35b66
SN
4774 if (rect)
4775 {
bca7bfc8
SN
4776 int rect_x, rect_y, rectWidth, rectHeight;
4777 int width_label, width_cell, height_label, height_cell;
4778 int x, y;
4779
2f024384 4780 // Copy rectangle can get scroll offsets..
bca7bfc8
SN
4781 rect_x = rect->GetX();
4782 rect_y = rect->GetY();
4783 rectWidth = rect->GetWidth();
4784 rectHeight = rect->GetHeight();
27f35b66 4785
bca7bfc8 4786 width_label = m_rowLabelWidth - rect_x;
4db6714b
KH
4787 if (width_label > rectWidth)
4788 width_label = rectWidth;
27f35b66 4789
bca7bfc8 4790 height_label = m_colLabelHeight - rect_y;
a9339fe2
DS
4791 if (height_label > rectHeight)
4792 height_label = rectHeight;
27f35b66 4793
bca7bfc8
SN
4794 if (rect_x > m_rowLabelWidth)
4795 {
4796 x = rect_x - m_rowLabelWidth;
4797 width_cell = rectWidth;
4798 }
4799 else
4800 {
4801 x = 0;
4802 width_cell = rectWidth - (m_rowLabelWidth - rect_x);
4803 }
4804
4805 if (rect_y > m_colLabelHeight)
4806 {
4807 y = rect_y - m_colLabelHeight;
4808 height_cell = rectHeight;
4809 }
4810 else
4811 {
4812 y = 0;
4813 height_cell = rectHeight - (m_colLabelHeight - rect_y);
4814 }
4815
4816 // Paint corner label part intersecting rect.
4817 if ( width_label > 0 && height_label > 0 )
4818 {
4819 wxRect anotherrect(rect_x, rect_y, width_label, height_label);
4820 m_cornerLabelWin->Refresh(eraseb, &anotherrect);
4821 }
4822
4823 // Paint col labels part intersecting rect.
4824 if ( width_cell > 0 && height_label > 0 )
4825 {
4826 wxRect anotherrect(x, rect_y, width_cell, height_label);
ad805b9e 4827 m_colWindow->Refresh(eraseb, &anotherrect);
bca7bfc8
SN
4828 }
4829
4830 // Paint row labels part intersecting rect.
4831 if ( width_label > 0 && height_cell > 0 )
4832 {
4833 wxRect anotherrect(rect_x, y, width_label, height_cell);
4834 m_rowLabelWin->Refresh(eraseb, &anotherrect);
4835 }
4836
4837 // Paint cell area part intersecting rect.
4838 if ( width_cell > 0 && height_cell > 0 )
4839 {
4840 wxRect anotherrect(x, y, width_cell, height_cell);
4841 m_gridWin->Refresh(eraseb, &anotherrect);
4842 }
4843 }
4844 else
4845 {
4846 m_cornerLabelWin->Refresh(eraseb, NULL);
ad805b9e 4847 m_colWindow->Refresh(eraseb, NULL);
bca7bfc8
SN
4848 m_rowLabelWin->Refresh(eraseb, NULL);
4849 m_gridWin->Refresh(eraseb, NULL);
4850 }
27f35b66
SN
4851 }
4852}
f85afd4e 4853
c71b2126 4854void wxGrid::OnSize(wxSizeEvent& WXUNUSED(event))
f85afd4e 4855{
b93aafab
JS
4856 if (m_targetWindow != this) // check whether initialisation has been done
4857 {
f1ff7df0
VZ
4858 // reposition our children windows
4859 CalcWindowSizes();
b93aafab 4860 }
f85afd4e
MB
4861}
4862
2d66e025 4863void wxGrid::OnKeyDown( wxKeyEvent& event )
f85afd4e 4864{
2d66e025 4865 if ( m_inOnKeyDown )
f85afd4e 4866 {
2d66e025
MB
4867 // shouldn't be here - we are going round in circles...
4868 //
07296f0b 4869 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") );
f85afd4e
MB
4870 }
4871
ca65c044 4872 m_inOnKeyDown = true;
f85afd4e 4873
2d66e025 4874 // propagate the event up and see if it gets processed
2d66e025
MB
4875 wxWindow *parent = GetParent();
4876 wxKeyEvent keyEvt( event );
4877 keyEvt.SetEventObject( parent );
4878
4879 if ( !parent->GetEventHandler()->ProcessEvent( keyEvt ) )
f85afd4e 4880 {
bcb614b3
RR
4881 if (GetLayoutDirection() == wxLayout_RightToLeft)
4882 {
4883 if (event.GetKeyCode() == WXK_RIGHT)
4884 event.m_keyCode = WXK_LEFT;
4885 else if (event.GetKeyCode() == WXK_LEFT)
4886 event.m_keyCode = WXK_RIGHT;
4887 }
c71b2126 4888
2d66e025 4889 // try local handlers
12a3f227 4890 switch ( event.GetKeyCode() )
2d66e025
MB
4891 {
4892 case WXK_UP:
4893 if ( event.ControlDown() )
5c8fc7c1 4894 MoveCursorUpBlock( event.ShiftDown() );
2d66e025 4895 else
5c8fc7c1 4896 MoveCursorUp( event.ShiftDown() );
2d66e025 4897 break;
f85afd4e 4898
2d66e025
MB
4899 case WXK_DOWN:
4900 if ( event.ControlDown() )
5c8fc7c1 4901 MoveCursorDownBlock( event.ShiftDown() );
2d66e025 4902 else
5c8fc7c1 4903 MoveCursorDown( event.ShiftDown() );
2d66e025 4904 break;
8f177c8e 4905
2d66e025
MB
4906 case WXK_LEFT:
4907 if ( event.ControlDown() )
5c8fc7c1 4908 MoveCursorLeftBlock( event.ShiftDown() );
2d66e025 4909 else
5c8fc7c1 4910 MoveCursorLeft( event.ShiftDown() );
2d66e025
MB
4911 break;
4912
4913 case WXK_RIGHT:
4914 if ( event.ControlDown() )
5c8fc7c1 4915 MoveCursorRightBlock( event.ShiftDown() );
2d66e025 4916 else
5c8fc7c1 4917 MoveCursorRight( event.ShiftDown() );
2d66e025 4918 break;
b99be8fb 4919
2d66e025 4920 case WXK_RETURN:
a4f7bf58 4921 case WXK_NUMPAD_ENTER:
58dd5b3b
MB
4922 if ( event.ControlDown() )
4923 {
4924 event.Skip(); // to let the edit control have the return
4925 }
4926 else
4927 {
f6bcfd97
BP
4928 if ( GetGridCursorRow() < GetNumberRows()-1 )
4929 {
4930 MoveCursorDown( event.ShiftDown() );
4931 }
4932 else
4933 {
4934 // at the bottom of a column
13f6e9e8 4935 DisableCellEditControl();
f6bcfd97 4936 }
58dd5b3b 4937 }
2d66e025
MB
4938 break;
4939
5c8fc7c1 4940 case WXK_ESCAPE:
e32352cf 4941 ClearSelection();
5c8fc7c1
SN
4942 break;
4943
2c9a89e0 4944 case WXK_TAB:
f6bcfd97 4945 {
1dc17bca
VZ
4946 // send an event to the grid's parents for custom handling
4947 wxGridEvent gridEvt(GetId(), wxEVT_GRID_TABBING, this,
4948 GetGridCursorRow(), GetGridCursorCol(),
4949 -1, -1, false, event);
4950 if ( ProcessWindowEvent(gridEvt) )
f6bcfd97 4951 {
1dc17bca
VZ
4952 // the event has been handled so no need for more processing
4953 break;
f6bcfd97
BP
4954 }
4955 }
1dc17bca 4956 DoGridProcessTab( event );
2c9a89e0
RD
4957 break;
4958
2d66e025 4959 case WXK_HOME:
ad178a65
VZ
4960 GoToCell(event.ControlDown() ? 0
4961 : m_currentCellCoords.GetRow(),
4962 0);
2d66e025
MB
4963 break;
4964
4965 case WXK_END:
ad178a65
VZ
4966 GoToCell(event.ControlDown() ? m_numRows - 1
4967 : m_currentCellCoords.GetRow(),
4968 m_numCols - 1);
2d66e025
MB
4969 break;
4970
faa94f3e 4971 case WXK_PAGEUP:
2d66e025
MB
4972 MovePageUp();
4973 break;
4974
faa94f3e 4975 case WXK_PAGEDOWN:
2d66e025
MB
4976 MovePageDown();
4977 break;
4978
07296f0b 4979 case WXK_SPACE:
32b4e9ec
VZ
4980 // Ctrl-Space selects the current column, Shift-Space -- the
4981 // current row and Ctrl-Shift-Space -- everything
4982 switch ( m_selection ? event.GetModifiers() : wxMOD_NONE )
aa5e1f75 4983 {
32b4e9ec
VZ
4984 case wxMOD_CONTROL:
4985 m_selection->SelectCol(m_currentCellCoords.GetCol());
4986 break;
ccdee36f 4987
32b4e9ec
VZ
4988 case wxMOD_SHIFT:
4989 m_selection->SelectRow(m_currentCellCoords.GetRow());
4990 break;
4991
4992 case wxMOD_CONTROL | wxMOD_SHIFT:
4993 m_selection->SelectBlock(0, 0,
4994 m_numRows - 1, m_numCols - 1);
4995 break;
4996
4997 case wxMOD_NONE:
4998 if ( !IsEditable() )
4999 {
5000 MoveCursorRight(false);
5001 break;
5002 }
5003 //else: fall through
5004
5005 default:
5006 event.Skip();
5007 }
ccdee36f 5008 break;
07296f0b 5009
2d66e025 5010 default:
63e2147c 5011 event.Skip();
025562fe 5012 break;
2d66e025 5013 }
f85afd4e
MB
5014 }
5015
ca65c044 5016 m_inOnKeyDown = false;
f85afd4e
MB
5017}
5018
f6bcfd97
BP
5019void wxGrid::OnKeyUp( wxKeyEvent& event )
5020{
5021 // try local handlers
5022 //
12a3f227 5023 if ( event.GetKeyCode() == WXK_SHIFT )
f6bcfd97 5024 {
8a3e536c
VZ
5025 if ( m_selectedBlockTopLeft != wxGridNoCellCoords &&
5026 m_selectedBlockBottomRight != wxGridNoCellCoords )
3f3dc2ef
VZ
5027 {
5028 if ( m_selection )
5029 {
a9339fe2 5030 m_selection->SelectBlock(
8a3e536c
VZ
5031 m_selectedBlockTopLeft,
5032 m_selectedBlockBottomRight,
8b5f6d9d 5033 event);
3f3dc2ef
VZ
5034 }
5035 }
5036
8a3e536c
VZ
5037 m_selectedBlockTopLeft = wxGridNoCellCoords;
5038 m_selectedBlockBottomRight = wxGridNoCellCoords;
5039 m_selectedBlockCorner = wxGridNoCellCoords;
f6bcfd97
BP
5040 }
5041}
5042
63e2147c
RD
5043void wxGrid::OnChar( wxKeyEvent& event )
5044{
5045 // is it possible to edit the current cell at all?
5046 if ( !IsCellEditControlEnabled() && CanEnableCellControl() )
5047 {
5048 // yes, now check whether the cells editor accepts the key
5049 int row = m_currentCellCoords.GetRow();
5050 int col = m_currentCellCoords.GetCol();
2f024384 5051 wxGridCellAttr *attr = GetCellAttr(row, col);
63e2147c
RD
5052 wxGridCellEditor *editor = attr->GetEditor(this, row, col);
5053
5054 // <F2> is special and will always start editing, for
5055 // other keys - ask the editor itself
5056 if ( (event.GetKeyCode() == WXK_F2 && !event.HasModifiers())
5057 || editor->IsAcceptedKey(event) )
5058 {
5059 // ensure cell is visble
5060 MakeCellVisible(row, col);
5061 EnableCellEditControl();
5062
5063 // a problem can arise if the cell is not completely
5064 // visible (even after calling MakeCellVisible the
5065 // control is not created and calling StartingKey will
5066 // crash the app
046d682f 5067 if ( event.GetKeyCode() != WXK_F2 && editor->IsCreated() && m_cellEditCtrlEnabled )
63e2147c
RD
5068 editor->StartingKey(event);
5069 }
5070 else
5071 {
5072 event.Skip();
5073 }
5074
5075 editor->DecRef();
5076 attr->DecRef();
5077 }
5078 else
5079 {
5080 event.Skip();
5081 }
5082}
5083
2796cce3 5084void wxGrid::OnEraseBackground(wxEraseEvent&)
508011ce
VZ
5085{
5086}
07296f0b 5087
1dc17bca
VZ
5088void wxGrid::DoGridProcessTab(wxKeyboardState& kbdState)
5089{
5090 const bool isForwardTab = !kbdState.ShiftDown();
5091
5092 // TAB processing only changes when we are at the borders of the grid, so
5093 // let's first handle the common behaviour when we are not at the border.
5094 if ( isForwardTab )
5095 {
5096 if ( GetGridCursorCol() < GetNumberCols() - 1 )
5097 {
5098 MoveCursorRight( false );
5099 return;
5100 }
5101 }
5102 else // going back
5103 {
5104 if ( GetGridCursorCol() )
5105 {
5106 MoveCursorLeft( false );
5107 return;
5108 }
5109 }
5110
5111
5112 // We only get here if the cursor is at the border of the grid, apply the
5113 // configured behaviour.
5114 switch ( m_tabBehaviour )
5115 {
5116 case Tab_Stop:
5117 // Nothing special to do, we remain at the current cell.
5118 break;
5119
5120 case Tab_Wrap:
5121 // Go to the beginning of the next or the end of the previous row.
5122 if ( isForwardTab )
5123 {
5124 if ( GetGridCursorRow() < GetNumberRows() - 1 )
5125 {
5126 GoToCell( GetGridCursorRow() + 1, 0 );
5127 return;
5128 }
5129 }
5130 else
5131 {
5132 if ( GetGridCursorRow() > 0 )
5133 {
5134 GoToCell( GetGridCursorRow() - 1, GetNumberCols() - 1 );
5135 return;
5136 }
5137 }
5138 break;
5139
5140 case Tab_Leave:
5141 if ( Navigate( isForwardTab ? wxNavigationKeyEvent::IsForward
5142 : wxNavigationKeyEvent::IsBackward ) )
5143 return;
5144 break;
5145 }
5146
5147 // If we remain in this cell, stop editing it if we were doing so.
5148 DisableCellEditControl();
5149}
5150
8a3e536c 5151bool wxGrid::SetCurrentCell( const wxGridCellCoords& coords )
66242c80 5152{
8a3e536c 5153 if ( SendEvent(wxEVT_GRID_SELECT_CELL, coords) == -1 )
f6bcfd97 5154 {
8a3e536c
VZ
5155 // the event has been vetoed - do nothing
5156 return false;
f6bcfd97
BP
5157 }
5158
9553702e 5159#if !defined(__WXMAC__)
bee19958
DS
5160 wxClientDC dc( m_gridWin );
5161 PrepareDC( dc );
5d38a5f3 5162#endif
f6bcfd97 5163
2a7750d9 5164 if ( m_currentCellCoords != wxGridNoCellCoords )
2d66e025 5165 {
b54ba671 5166 DisableCellEditControl();
07296f0b 5167
ca65c044 5168 if ( IsVisible( m_currentCellCoords, false ) )
f6bcfd97
BP
5169 {
5170 wxRect r;
bee19958 5171 r = BlockToDeviceRect( m_currentCellCoords, m_currentCellCoords );
f6bcfd97
BP
5172 if ( !m_gridLinesEnabled )
5173 {
5174 r.x--;
5175 r.y--;
5176 r.width++;
5177 r.height++;
5178 }
d1c0b4f9 5179
3ed884a0 5180 wxGridCellCoordsArray cells = CalcCellsExposed( r );
84912ef8 5181
f6bcfd97
BP
5182 // Otherwise refresh redraws the highlight!
5183 m_currentCellCoords = coords;
275c4ae4 5184
9553702e 5185#if defined(__WXMAC__)
5d38a5f3
JS
5186 m_gridWin->Refresh(true /*, & r */);
5187#else
bee19958 5188 DrawGridCellArea( dc, cells );
f6bcfd97 5189 DrawAllGridLines( dc, r );
5d38a5f3 5190#endif
f6bcfd97 5191 }
66242c80 5192 }
8f177c8e 5193
2d66e025
MB
5194 m_currentCellCoords = coords;
5195
bee19958 5196 wxGridCellAttr *attr = GetCellAttr( coords );
76c66f19 5197#if !defined(__WXMAC__)
bee19958 5198 DrawCellHighlight( dc, attr );
5d38a5f3 5199#endif
2a7750d9 5200 attr->DecRef();
8a3e536c
VZ
5201
5202 return true;
66242c80
MB
5203}
5204
8a3e536c
VZ
5205void
5206wxGrid::UpdateBlockBeingSelected(int topRow, int leftCol,
5207 int bottomRow, int rightCol)
c9097836 5208{
4445d035
VZ
5209 MakeCellVisible(m_selectedBlockCorner);
5210 m_selectedBlockCorner = wxGridCellCoords(bottomRow, rightCol);
5211
3f3dc2ef 5212 if ( m_selection )
c9097836 5213 {
8a3e536c 5214 switch ( m_selection->GetSelectionMode() )
3f3dc2ef 5215 {
8a3e536c
VZ
5216 default:
5217 wxFAIL_MSG( "unknown selection mode" );
5218 // fall through
5219
5220 case wxGridSelectCells:
5221 // arbitrary blocks selection allowed so just use the cell
5222 // coordinates as is
5223 break;
5224
5225 case wxGridSelectRows:
5226 // only full rows selection allowd, ensure that we do select
5227 // full rows
5228 leftCol = 0;
5229 rightCol = GetNumberCols() - 1;
5230 break;
5231
5232 case wxGridSelectColumns:
5233 // same as above but for columns
5234 topRow = 0;
5235 bottomRow = GetNumberRows() - 1;
5236 break;
5237
5238 case wxGridSelectRowsOrColumns:
5239 // in this mode we can select only full rows or full columns so
5240 // it doesn't make sense to select blocks at all (and we can't
5241 // extend the block because there is no preferred direction, we
5242 // could only extend it to cover the entire grid but this is
5243 // not useful)
5244 return;
3f3dc2ef 5245 }
c9097836 5246 }
3f3dc2ef 5247
1372f8cc
VZ
5248 EnsureFirstLessThanSecond(topRow, bottomRow);
5249 EnsureFirstLessThanSecond(leftCol, rightCol);
c9097836 5250
8a3e536c
VZ
5251 wxGridCellCoords updateTopLeft = wxGridCellCoords(topRow, leftCol),
5252 updateBottomRight = wxGridCellCoords(bottomRow, rightCol);
c9097836 5253
3ed884a0 5254 // First the case that we selected a completely new area
8a3e536c
VZ
5255 if ( m_selectedBlockTopLeft == wxGridNoCellCoords ||
5256 m_selectedBlockBottomRight == wxGridNoCellCoords )
3ed884a0
SN
5257 {
5258 wxRect rect;
5259 rect = BlockToDeviceRect( wxGridCellCoords ( topRow, leftCol ),
5260 wxGridCellCoords ( bottomRow, rightCol ) );
ca65c044 5261 m_gridWin->Refresh( false, &rect );
3ed884a0 5262 }
2f024384 5263
3ed884a0 5264 // Now handle changing an existing selection area.
8a3e536c
VZ
5265 else if ( m_selectedBlockTopLeft != updateTopLeft ||
5266 m_selectedBlockBottomRight != updateBottomRight )
c9097836
MB
5267 {
5268 // Compute two optimal update rectangles:
5269 // Either one rectangle is a real subset of the
5270 // other, or they are (almost) disjoint!
5271 wxRect rect[4];
5272 bool need_refresh[4];
5273 need_refresh[0] =
5274 need_refresh[1] =
5275 need_refresh[2] =
ca65c044 5276 need_refresh[3] = false;
c9097836
MB
5277 int i;
5278
5279 // Store intermediate values
8a3e536c
VZ
5280 wxCoord oldLeft = m_selectedBlockTopLeft.GetCol();
5281 wxCoord oldTop = m_selectedBlockTopLeft.GetRow();
5282 wxCoord oldRight = m_selectedBlockBottomRight.GetCol();
5283 wxCoord oldBottom = m_selectedBlockBottomRight.GetRow();
c9097836
MB
5284
5285 // Determine the outer/inner coordinates.
1372f8cc
VZ
5286 EnsureFirstLessThanSecond(oldLeft, leftCol);
5287 EnsureFirstLessThanSecond(oldTop, topRow);
5288 EnsureFirstLessThanSecond(rightCol, oldRight);
5289 EnsureFirstLessThanSecond(bottomRow, oldBottom);
c9097836
MB
5290
5291 // Now, either the stuff marked old is the outer
5292 // rectangle or we don't have a situation where one
5293 // is contained in the other.
5294
5295 if ( oldLeft < leftCol )
5296 {
3ed884a0
SN
5297 // Refresh the newly selected or deselected
5298 // area to the left of the old or new selection.
ca65c044 5299 need_refresh[0] = true;
a9339fe2
DS
5300 rect[0] = BlockToDeviceRect(
5301 wxGridCellCoords( oldTop, oldLeft ),
5302 wxGridCellCoords( oldBottom, leftCol - 1 ) );
c9097836
MB
5303 }
5304
2f024384 5305 if ( oldTop < topRow )
c9097836 5306 {
3ed884a0
SN
5307 // Refresh the newly selected or deselected
5308 // area above the old or new selection.
ca65c044 5309 need_refresh[1] = true;
2f024384
DS
5310 rect[1] = BlockToDeviceRect(
5311 wxGridCellCoords( oldTop, leftCol ),
5312 wxGridCellCoords( topRow - 1, rightCol ) );
c9097836
MB
5313 }
5314
5315 if ( oldRight > rightCol )
5316 {
3ed884a0
SN
5317 // Refresh the newly selected or deselected
5318 // area to the right of the old or new selection.
ca65c044 5319 need_refresh[2] = true;
2f024384 5320 rect[2] = BlockToDeviceRect(
a9339fe2
DS
5321 wxGridCellCoords( oldTop, rightCol + 1 ),
5322 wxGridCellCoords( oldBottom, oldRight ) );
c9097836
MB
5323 }
5324
5325 if ( oldBottom > bottomRow )
5326 {
3ed884a0
SN
5327 // Refresh the newly selected or deselected
5328 // area below the old or new selection.
ca65c044 5329 need_refresh[3] = true;
2f024384 5330 rect[3] = BlockToDeviceRect(
a9339fe2
DS
5331 wxGridCellCoords( bottomRow + 1, leftCol ),
5332 wxGridCellCoords( oldBottom, rightCol ) );
c9097836
MB
5333 }
5334
c9097836
MB
5335 // various Refresh() calls
5336 for (i = 0; i < 4; i++ )
5337 if ( need_refresh[i] && rect[i] != wxGridNoCellRect )
ca65c044 5338 m_gridWin->Refresh( false, &(rect[i]) );
c9097836 5339 }
2f024384
DS
5340
5341 // change selection
8a3e536c
VZ
5342 m_selectedBlockTopLeft = updateTopLeft;
5343 m_selectedBlockBottomRight = updateBottomRight;
c9097836
MB
5344}
5345
2d66e025
MB
5346//
5347// ------ functions to get/send data (see also public functions)
5348//
5349
5350bool wxGrid::GetModelValues()
66242c80 5351{
bca7bfc8
SN
5352 // Hide the editor, so it won't hide a changed value.
5353 HideCellEditControl();
c6707d16 5354
2d66e025 5355 if ( m_table )
66242c80 5356 {
2d66e025 5357 // all we need to do is repaint the grid
66242c80 5358 //
2d66e025 5359 m_gridWin->Refresh();
ca65c044 5360 return true;
66242c80 5361 }
8f177c8e 5362
ca65c044 5363 return false;
66242c80
MB
5364}
5365
2d66e025 5366bool wxGrid::SetModelValues()
f85afd4e 5367{
2d66e025 5368 int row, col;
8f177c8e 5369
c6707d16
SN
5370 // Disable the editor, so it won't hide a changed value.
5371 // Do we also want to save the current value of the editor first?
5372 // I think so ...
c6707d16
SN
5373 DisableCellEditControl();
5374
2d66e025
MB
5375 if ( m_table )
5376 {
56b6cf26 5377 for ( row = 0; row < m_numRows; row++ )
f85afd4e 5378 {
56b6cf26 5379 for ( col = 0; col < m_numCols; col++ )
f85afd4e 5380 {
2d66e025 5381 m_table->SetValue( row, col, GetCellValue(row, col) );
f85afd4e
MB
5382 }
5383 }
8f177c8e 5384
ca65c044 5385 return true;
f85afd4e
MB
5386 }
5387
ca65c044 5388 return false;
f85afd4e
MB
5389}
5390
2d66e025
MB
5391// Note - this function only draws cells that are in the list of
5392// exposed cells (usually set from the update region by
5393// CalcExposedCells)
5394//
d10f4bf9 5395void wxGrid::DrawGridCellArea( wxDC& dc, const wxGridCellCoordsArray& cells )
f85afd4e 5396{
4db6714b
KH
5397 if ( !m_numRows || !m_numCols )
5398 return;
60ff3b99 5399
dc1f566f 5400 int i, numCells = cells.GetCount();
27f35b66
SN
5401 int row, col, cell_rows, cell_cols;
5402 wxGridCellCoordsArray redrawCells;
60ff3b99 5403
a9339fe2 5404 for ( i = numCells - 1; i >= 0; i-- )
f85afd4e 5405 {
27f35b66
SN
5406 row = cells[i].GetRow();
5407 col = cells[i].GetCol();
5408 GetCellSize( row, col, &cell_rows, &cell_cols );
5409
5410 // If this cell is part of a multicell block, find owner for repaint
5411 if ( cell_rows <= 0 || cell_cols <= 0 )
5412 {
a9339fe2 5413 wxGridCellCoords cell( row + cell_rows, col + cell_cols );
ca65c044 5414 bool marked = false;
56b6cf26 5415 for ( int j = 0; j < numCells; j++ )
27f35b66
SN
5416 {
5417 if ( cell == cells[j] )
5418 {
ca65c044 5419 marked = true;
3ed884a0 5420 break;
27f35b66
SN
5421 }
5422 }
2f024384 5423
27f35b66
SN
5424 if (!marked)
5425 {
5426 int count = redrawCells.GetCount();
dc1f566f 5427 for (int j = 0; j < count; j++)
27f35b66
SN
5428 {
5429 if ( cell == redrawCells[j] )
5430 {
ca65c044 5431 marked = true;
27f35b66
SN
5432 break;
5433 }
5434 }
2f024384 5435
4db6714b
KH
5436 if (!marked)
5437 redrawCells.Add( cell );
27f35b66 5438 }
2f024384
DS
5439
5440 // don't bother drawing this cell
5441 continue;
27f35b66
SN
5442 }
5443
5444 // If this cell is empty, find cell to left that might want to overflow
5445 if (m_table && m_table->IsEmptyCell(row, col))
5446 {
dc1f566f 5447 for ( int l = 0; l < cell_rows; l++ )
27f35b66 5448 {
56b6cf26 5449 // find a cell in this row to leave already marked for repaint
dc1f566f
SN
5450 int left = col;
5451 for (int k = 0; k < int(redrawCells.GetCount()); k++)
5452 if ((redrawCells[k].GetCol() < left) &&
5453 (redrawCells[k].GetRow() == row))
2f024384 5454 {
4db6714b 5455 left = redrawCells[k].GetCol();
2f024384 5456 }
dc1f566f 5457
4db6714b
KH
5458 if (left == col)
5459 left = 0; // oh well
dc1f566f 5460
2f024384 5461 for (int j = col - 1; j >= left; j--)
27f35b66 5462 {
2f024384 5463 if (!m_table->IsEmptyCell(row + l, j))
27f35b66 5464 {
2f024384 5465 if (GetCellOverflow(row + l, j))
27f35b66 5466 {
2f024384 5467 wxGridCellCoords cell(row + l, j);
ca65c044 5468 bool marked = false;
27f35b66 5469
dc1f566f 5470 for (int k = 0; k < numCells; k++)
27f35b66
SN
5471 {
5472 if ( cell == cells[k] )
5473 {
ca65c044 5474 marked = true;
27f35b66
SN
5475 break;
5476 }
5477 }
4db6714b 5478
27f35b66
SN
5479 if (!marked)
5480 {
5481 int count = redrawCells.GetCount();
dc1f566f 5482 for (int k = 0; k < count; k++)
27f35b66
SN
5483 {
5484 if ( cell == redrawCells[k] )
5485 {
ca65c044 5486 marked = true;
27f35b66
SN
5487 break;
5488 }
5489 }
4db6714b
KH
5490 if (!marked)
5491 redrawCells.Add( cell );
27f35b66
SN
5492 }
5493 }
5494 break;
5495 }
5496 }
5497 }
5498 }
4db6714b 5499
d10f4bf9 5500 DrawCell( dc, cells[i] );
2d66e025 5501 }
27f35b66
SN
5502
5503 numCells = redrawCells.GetCount();
5504
56b6cf26 5505 for ( i = numCells - 1; i >= 0; i-- )
27f35b66
SN
5506 {
5507 DrawCell( dc, redrawCells[i] );
5508 }
2d66e025 5509}
8f177c8e 5510
7c8a8ad5
MB
5511void wxGrid::DrawGridSpace( wxDC& dc )
5512{
f6bcfd97
BP
5513 int cw, ch;
5514 m_gridWin->GetClientSize( &cw, &ch );
7c8a8ad5 5515
f6bcfd97
BP
5516 int right, bottom;
5517 CalcUnscrolledPosition( cw, ch, &right, &bottom );
7c8a8ad5 5518
d4175745 5519 int rightCol = m_numCols > 0 ? GetColRight(GetColAt( m_numCols - 1 )) : 0;
2f024384 5520 int bottomRow = m_numRows > 0 ? GetRowBottom(m_numRows - 1) : 0;
7c8a8ad5 5521
f6bcfd97
BP
5522 if ( right > rightCol || bottom > bottomRow )
5523 {
5524 int left, top;
5525 CalcUnscrolledPosition( 0, 0, &left, &top );
7c8a8ad5 5526
ff72f628 5527 dc.SetBrush(GetDefaultCellBackgroundColour());
f6bcfd97 5528 dc.SetPen( *wxTRANSPARENT_PEN );
7c8a8ad5 5529
f6bcfd97
BP
5530 if ( right > rightCol )
5531 {
a9339fe2 5532 dc.DrawRectangle( rightCol, top, right - rightCol, ch );
f6bcfd97
BP
5533 }
5534
5535 if ( bottom > bottomRow )
5536 {
a9339fe2 5537 dc.DrawRectangle( left, bottomRow, cw, bottom - bottomRow );
f6bcfd97
BP
5538 }
5539 }
7c8a8ad5
MB
5540}
5541
2d66e025
MB
5542void wxGrid::DrawCell( wxDC& dc, const wxGridCellCoords& coords )
5543{
ab79958a
VZ
5544 int row = coords.GetRow();
5545 int col = coords.GetCol();
60ff3b99 5546
7c1cb261 5547 if ( GetColWidth(col) <= 0 || GetRowHeight(row) <= 0 )
ab79958a
VZ
5548 return;
5549
5550 // we draw the cell border ourselves
189d0213
VZ
5551 wxGridCellAttr* attr = GetCellAttr(row, col);
5552
5553 bool isCurrent = coords == m_currentCellCoords;
508011ce 5554
f6bcfd97 5555 wxRect rect = CellToRect( row, col );
f85afd4e 5556
189d0213 5557 // if the editor is shown, we should use it and not the renderer
f6bcfd97
BP
5558 // Note: However, only if it is really _shown_, i.e. not hidden!
5559 if ( isCurrent && IsCellEditControlShown() )
189d0213 5560 {
a9339fe2 5561 // NB: this "#if..." is temporary and fixes a problem where the
962a48f6
DS
5562 // edit control is erased by this code after being rendered.
5563 // On wxMac (QD build only), the cell editor is a wxTextCntl and is rendered
5564 // implicitly, causing this out-of order render.
5d38a5f3 5565#if !defined(__WXMAC__)
0b190b0f 5566 wxGridCellEditor *editor = attr->GetEditor(this, row, col);
ccc04025 5567 editor->PaintBackground(dc, rect, *attr);
0b190b0f 5568 editor->DecRef();
962a48f6 5569#endif
189d0213
VZ
5570 }
5571 else
5572 {
a9339fe2 5573 // but all the rest is drawn by the cell renderer and hence may be customized
0b190b0f
VZ
5574 wxGridCellRenderer *renderer = attr->GetRenderer(this, row, col);
5575 renderer->Draw(*this, *attr, dc, rect, row, col, IsInSelection(coords));
5576 renderer->DecRef();
189d0213 5577 }
07296f0b 5578
283b7808
VZ
5579 attr->DecRef();
5580}
07296f0b 5581
283b7808 5582void wxGrid::DrawCellHighlight( wxDC& dc, const wxGridCellAttr *attr )
07296f0b 5583{
760be3f7
VS
5584 // don't show highlight when the grid doesn't have focus
5585 if ( !HasFocus() )
5586 return;
5587
07296f0b
RD
5588 int row = m_currentCellCoords.GetRow();
5589 int col = m_currentCellCoords.GetCol();
5590
7c1cb261 5591 if ( GetColWidth(col) <= 0 || GetRowHeight(row) <= 0 )
07296f0b
RD
5592 return;
5593
f6bcfd97 5594 wxRect rect = CellToRect(row, col);
07296f0b 5595
b3a7510d
VZ
5596 // hmmm... what could we do here to show that the cell is disabled?
5597 // for now, I just draw a thinner border than for the other ones, but
5598 // it doesn't look really good
b3a7510d 5599
d2520c85
RD
5600 int penWidth = attr->IsReadOnly() ? m_cellHighlightROPenWidth : m_cellHighlightPenWidth;
5601
27f35b66
SN
5602 if (penWidth > 0)
5603 {
56b6cf26
DS
5604 // The center of the drawn line is where the position/width/height of
5605 // the rectangle is actually at (on wxMSW at least), so the
5606 // size of the rectangle is reduced to compensate for the thickness of
5607 // the line. If this is too strange on non-wxMSW platforms then
d2520c85 5608 // please #ifdef this appropriately.
4db6714b
KH
5609 rect.x += penWidth / 2;
5610 rect.y += penWidth / 2;
5611 rect.width -= penWidth - 1;
5612 rect.height -= penWidth - 1;
d2520c85 5613
d2520c85 5614 // Now draw the rectangle
73145b0e
JS
5615 // use the cellHighlightColour if the cell is inside a selection, this
5616 // will ensure the cell is always visible.
ff72f628
VZ
5617 dc.SetPen(wxPen(IsInSelection(row,col) ? m_selectionForeground
5618 : m_cellHighlightColour,
5619 penWidth));
d2520c85
RD
5620 dc.SetBrush(*wxTRANSPARENT_BRUSH);
5621 dc.DrawRectangle(rect);
5622 }
2d66e025 5623}
f85afd4e 5624
3d3f3e37
VZ
5625wxPen wxGrid::GetDefaultGridLinePen()
5626{
ff72f628 5627 return wxPen(GetGridLineColour());
3d3f3e37
VZ
5628}
5629
5630wxPen wxGrid::GetRowGridLinePen(int WXUNUSED(row))
5631{
5632 return GetDefaultGridLinePen();
5633}
5634
5635wxPen wxGrid::GetColGridLinePen(int WXUNUSED(col))
5636{
5637 return GetDefaultGridLinePen();
5638}
5639
2d66e025
MB
5640void wxGrid::DrawCellBorder( wxDC& dc, const wxGridCellCoords& coords )
5641{
2d66e025
MB
5642 int row = coords.GetRow();
5643 int col = coords.GetCol();
7c1cb261
VZ
5644 if ( GetColWidth(col) <= 0 || GetRowHeight(row) <= 0 )
5645 return;
5646
60ff3b99 5647
27f35b66
SN
5648 wxRect rect = CellToRect( row, col );
5649
2d66e025 5650 // right hand border
3d3f3e37 5651 dc.SetPen( GetColGridLinePen(col) );
27f35b66
SN
5652 dc.DrawLine( rect.x + rect.width, rect.y,
5653 rect.x + rect.width, rect.y + rect.height + 1 );
2d66e025
MB
5654
5655 // bottom border
3d3f3e37 5656 dc.SetPen( GetRowGridLinePen(row) );
2f024384 5657 dc.DrawLine( rect.x, rect.y + rect.height,
27f35b66 5658 rect.x + rect.width, rect.y + rect.height);
f85afd4e
MB
5659}
5660
2f024384 5661void wxGrid::DrawHighlight(wxDC& dc, const wxGridCellCoordsArray& cells)
b3a7510d 5662{
2a7750d9
MB
5663 // This if block was previously in wxGrid::OnPaint but that doesn't
5664 // seem to get called under wxGTK - MB
5665 //
2f024384 5666 if ( m_currentCellCoords == wxGridNoCellCoords &&
2a7750d9
MB
5667 m_numRows && m_numCols )
5668 {
5669 m_currentCellCoords.Set(0, 0);
5670 }
5671
f6bcfd97 5672 if ( IsCellEditControlShown() )
99306db2
VZ
5673 {
5674 // don't show highlight when the edit control is shown
5675 return;
5676 }
5677
b3a7510d
VZ
5678 // if the active cell was repainted, repaint its highlight too because it
5679 // might have been damaged by the grid lines
d10f4bf9 5680 size_t count = cells.GetCount();
b3a7510d
VZ
5681 for ( size_t n = 0; n < count; n++ )
5682 {
54181a33
VZ
5683 wxGridCellCoords cell = cells[n];
5684
5685 // If we are using attributes, then we may have just exposed another
5686 // cell in a partially-visible merged cluster of cells. If the "anchor"
5687 // (upper left) cell of this merged cluster is the cell indicated by
5688 // m_currentCellCoords, then we need to refresh the cell highlight even
5689 // though the "anchor" itself is not part of our update segment.
5690 if ( CanHaveAttributes() )
5691 {
5692 int rows = 0,
5693 cols = 0;
5694 GetCellSize(cell.GetRow(), cell.GetCol(), &rows, &cols);
5695
5696 if ( rows < 0 )
5697 cell.SetRow(cell.GetRow() + rows);
5698
5699 if ( cols < 0 )
5700 cell.SetCol(cell.GetCol() + cols);
5701 }
5702
5703 if ( cell == m_currentCellCoords )
b3a7510d
VZ
5704 {
5705 wxGridCellAttr* attr = GetCellAttr(m_currentCellCoords);
5706 DrawCellHighlight(dc, attr);
5707 attr->DecRef();
5708
5709 break;
5710 }
5711 }
5712}
2d66e025 5713
b55d57aa
VZ
5714// Used by wxGrid::Render() to draw the grid lines only for the cells in the
5715// specified range.
5716void
5717wxGrid::DrawRangeGridLines(wxDC& dc,
5718 const wxRegion& reg,
5719 const wxGridCellCoords& topLeft,
5720 const wxGridCellCoords& bottomRight)
5721{
5722 if ( !m_gridLinesEnabled )
5723 return;
5724
5725 int top, left, width, height;
5726 reg.GetBox( left, top, width, height );
5727
5728 // create a clipping region
5729 wxRegion clippedcells( dc.LogicalToDeviceX( left ),
5730 dc.LogicalToDeviceY( top ),
5731 dc.LogicalToDeviceXRel( width ),
5732 dc.LogicalToDeviceYRel( height ) );
5733
5734 // subtract multi cell span area from clipping region for lines
5735 wxRect rect;
5736 for ( int row = topLeft.GetRow(); row <= bottomRight.GetRow(); row++ )
5737 {
5738 for ( int col = topLeft.GetCol(); col <= bottomRight.GetCol(); col++ )
5739 {
5740 int cell_rows, cell_cols;
5741 GetCellSize( row, col, &cell_rows, &cell_cols );
5742 if ( cell_rows > 1 || cell_cols > 1 ) // multi cell
5743 {
5744 rect = CellToRect( row, col );
5745 // cater for scaling
5746 // device origin already set in ::Render() for x, y
5747 rect.x = dc.LogicalToDeviceX( rect.x );
5748 rect.y = dc.LogicalToDeviceY( rect.y );
5749 rect.width = dc.LogicalToDeviceXRel( rect.width );
5750 rect.height = dc.LogicalToDeviceYRel( rect.height ) - 1;
5751 clippedcells.Subtract( rect );
5752 }
5753 else if ( cell_rows < 0 || cell_cols < 0 ) // part of multicell
5754 {
5755 rect = CellToRect( row + cell_rows, col + cell_cols );
5756 rect.x = dc.LogicalToDeviceX( rect.x );
5757 rect.y = dc.LogicalToDeviceY( rect.y );
5758 rect.width = dc.LogicalToDeviceXRel( rect.width );
5759 rect.height = dc.LogicalToDeviceYRel( rect.height ) - 1;
5760 clippedcells.Subtract( rect );
5761 }
5762 }
5763 }
5764
5765 dc.SetDeviceClippingRegion( clippedcells );
5766
5767 DoDrawGridLines(dc,
5768 top, left, top + height, left + width,
5769 topLeft.GetRow(), topLeft.GetCol(),
5770 bottomRight.GetRow(), bottomRight.GetCol());
5771
5772 dc.DestroyClippingRegion();
5773}
5774
2d66e025
MB
5775// This is used to redraw all grid lines e.g. when the grid line colour
5776// has been changed
5777//
33ac7e6f 5778void wxGrid::DrawAllGridLines( wxDC& dc, const wxRegion & WXUNUSED(reg) )
f85afd4e 5779{
9f7aee01 5780 if ( !m_gridLinesEnabled )
4db6714b 5781 return;
f85afd4e 5782
2d66e025 5783 int top, bottom, left, right;
796df70a 5784
ff72f628
VZ
5785 int cw, ch;
5786 m_gridWin->GetClientSize(&cw, &ch);
5787 CalcUnscrolledPosition( 0, 0, &left, &top );
5788 CalcUnscrolledPosition( cw, ch, &right, &bottom );
f85afd4e 5789
9496deb5 5790 // avoid drawing grid lines past the last row and col
9f7aee01
VZ
5791 if ( m_gridLinesClipHorz )
5792 {
5793 if ( !m_numCols )
5794 return;
5795
5796 const int lastColRight = GetColRight(GetColAt(m_numCols - 1));
5797 if ( right > lastColRight )
5798 right = lastColRight;
5799 }
5800
5801 if ( m_gridLinesClipVert )
5802 {
5803 if ( !m_numRows )
5804 return;
5805
5806 const int lastRowBottom = GetRowBottom(m_numRows - 1);
5807 if ( bottom > lastRowBottom )
5808 bottom = lastRowBottom;
5809 }
9496deb5 5810
27f35b66 5811 // no gridlines inside multicells, clip them out
d4175745 5812 int leftCol = GetColPos( internalXToCol(left) );
a9339fe2 5813 int topRow = internalYToRow(top);
d4175745 5814 int rightCol = GetColPos( internalXToCol(right) );
a967f048 5815 int bottomRow = internalYToRow(bottom);
27f35b66 5816
c03bf0c7 5817 wxRegion clippedcells(0, 0, cw, ch);
27f35b66 5818
ff72f628 5819 int cell_rows, cell_cols;
a967f048 5820 wxRect rect;
27f35b66 5821
ff72f628 5822 for ( int j = topRow; j <= bottomRow; j++ )
a967f048 5823 {
ff72f628 5824 for ( int colPos = leftCol; colPos <= rightCol; colPos++ )
27f35b66 5825 {
ff72f628 5826 int i = GetColAt( colPos );
d4175745 5827
a967f048
RG
5828 GetCellSize( j, i, &cell_rows, &cell_cols );
5829 if ((cell_rows > 1) || (cell_cols > 1))
27f35b66 5830 {
a967f048
RG
5831 rect = CellToRect(j,i);
5832 CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
5833 clippedcells.Subtract(rect);
5834 }
5835 else if ((cell_rows < 0) || (cell_cols < 0))
5836 {
a9339fe2 5837 rect = CellToRect(j + cell_rows, i + cell_cols);
a967f048
RG
5838 CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
5839 clippedcells.Subtract(rect);
27f35b66
SN
5840 }
5841 }
5842 }
a9339fe2 5843
c1bc8d9f 5844 dc.SetDeviceClippingRegion( clippedcells );
27f35b66 5845
f31d3faf
VZ
5846 DoDrawGridLines(dc,
5847 top, left, bottom, right,
5848 topRow, leftCol, m_numRows, m_numCols);
f85afd4e 5849
f31d3faf
VZ
5850 dc.DestroyClippingRegion();
5851}
5852
5853void
5854wxGrid::DoDrawGridLines(wxDC& dc,
5855 int top, int left,
5856 int bottom, int right,
5857 int topRow, int leftCol,
5858 int bottomRow, int rightCol)
5859{
2d66e025 5860 // horizontal grid lines
f31d3faf 5861 for ( int i = topRow; i < bottomRow; i++ )
f85afd4e 5862 {
6d55126d 5863 int bot = GetRowBottom(i) - 1;
7c1cb261 5864
6d55126d 5865 if ( bot > bottom )
2d66e025 5866 break;
7c1cb261 5867
6d55126d 5868 if ( bot >= top )
2d66e025 5869 {
3d3f3e37 5870 dc.SetPen( GetRowGridLinePen(i) );
6d55126d 5871 dc.DrawLine( left, bot, right, bot );
2d66e025 5872 }
f85afd4e
MB
5873 }
5874
2d66e025 5875 // vertical grid lines
f31d3faf 5876 for ( int colPos = leftCol; colPos < rightCol; colPos++ )
f85afd4e 5877 {
ff72f628 5878 int i = GetColAt( colPos );
d4175745 5879
2121eb69
RR
5880 int colRight = GetColRight(i);
5881#ifdef __WXGTK__
5882 if (GetLayoutDirection() != wxLayout_RightToLeft)
5883#endif
5884 colRight--;
5885
7c1cb261 5886 if ( colRight > right )
2d66e025 5887 break;
7c1cb261
VZ
5888
5889 if ( colRight >= left )
2d66e025 5890 {
3d3f3e37 5891 dc.SetPen( GetColGridLinePen(i) );
7c1cb261 5892 dc.DrawLine( colRight, top, colRight, bottom );
2d66e025
MB
5893 }
5894 }
5895}
f85afd4e 5896
c2f5b920 5897void wxGrid::DrawRowLabels( wxDC& dc, const wxArrayInt& rows)
2d66e025 5898{
4db6714b
KH
5899 if ( !m_numRows )
5900 return;
60ff3b99 5901
ff72f628
VZ
5902 const size_t numLabels = rows.GetCount();
5903 for ( size_t i = 0; i < numLabels; i++ )
2d66e025 5904 {
d10f4bf9 5905 DrawRowLabel( dc, rows[i] );
60ff3b99 5906 }
f85afd4e
MB
5907}
5908
2d66e025 5909void wxGrid::DrawRowLabel( wxDC& dc, int row )
f85afd4e 5910{
659af826 5911 if ( GetRowHeight(row) <= 0 || m_rowLabelWidth <= 0 )
7c1cb261 5912 return;
60ff3b99 5913
670d0177
VZ
5914 wxGridCellAttrProvider * const
5915 attrProvider = m_table ? m_table->GetAttrProvider() : NULL;
7a0a6cc8
VZ
5916
5917 // notice that an explicit static_cast is needed to avoid a compilation
5918 // error with VC7.1 which, for some reason, tries to instantiate (abstract)
5919 // wxGridRowHeaderRenderer class without it
ba9574c3 5920 const wxGridRowHeaderRenderer&
670d0177 5921 rend = attrProvider ? attrProvider->GetRowHeaderRenderer(row)
7a0a6cc8
VZ
5922 : static_cast<const wxGridRowHeaderRenderer&>
5923 (gs_defaultHeaderRenderers.rowRenderer);
5924
ba9574c3
VZ
5925 wxRect rect(0, GetRowTop(row), m_rowLabelWidth, GetRowHeight(row));
5926 rend.DrawBorder(*this, dc, rect);
8f177c8e 5927
f85afd4e 5928 int hAlign, vAlign;
ba9574c3 5929 GetRowLabelAlignment(&hAlign, &vAlign);
60ff3b99 5930
ba9574c3
VZ
5931 rend.DrawLabel(*this, dc, GetRowLabelValue(row),
5932 rect, hAlign, vAlign, wxHORIZONTAL);
f85afd4e
MB
5933}
5934
ad805b9e
VZ
5935void wxGrid::UseNativeColHeader(bool native)
5936{
5937 if ( native == m_useNativeHeader )
5938 return;
5939
5940 delete m_colWindow;
5941 m_useNativeHeader = native;
5942
5943 CreateColumnWindow();
5944
5945 if ( m_useNativeHeader )
3039ade9 5946 GetGridColHeader()->SetColumnCount(m_numCols);
ad805b9e
VZ
5947 CalcWindowSizes();
5948}
5949
71cf399f
RR
5950void wxGrid::SetUseNativeColLabels( bool native )
5951{
ad805b9e
VZ
5952 wxASSERT_MSG( !m_useNativeHeader,
5953 "doesn't make sense when using native header" );
5954
71cf399f
RR
5955 m_nativeColumnLabels = native;
5956 if (native)
5957 {
5958 int height = wxRendererNative::Get().GetHeaderButtonHeight( this );
5959 SetColLabelSize( height );
5960 }
76c66f19 5961
ad805b9e 5962 GetColLabelWindow()->Refresh();
ff72f628 5963 m_cornerLabelWin->Refresh();
71cf399f
RR
5964}
5965
d10f4bf9 5966void wxGrid::DrawColLabels( wxDC& dc,const wxArrayInt& cols )
f85afd4e 5967{
4db6714b
KH
5968 if ( !m_numCols )
5969 return;
60ff3b99 5970
ff72f628
VZ
5971 const size_t numLabels = cols.GetCount();
5972 for ( size_t i = 0; i < numLabels; i++ )
f85afd4e 5973 {
d10f4bf9 5974 DrawColLabel( dc, cols[i] );
60ff3b99 5975 }
f85afd4e
MB
5976}
5977
ff72f628
VZ
5978void wxGrid::DrawCornerLabel(wxDC& dc)
5979{
ba9574c3
VZ
5980 wxRect rect(wxSize(m_rowLabelWidth, m_colLabelHeight));
5981
ff72f628
VZ
5982 if ( m_nativeColumnLabels )
5983 {
ff72f628
VZ
5984 rect.Deflate(1);
5985
5986 wxRendererNative::Get().DrawHeaderButton(m_cornerLabelWin, dc, rect, 0);
5987 }
5988 else
5989 {
ba9574c3
VZ
5990 rect.width++;
5991 rect.height++;
5992
670d0177
VZ
5993 wxGridCellAttrProvider * const
5994 attrProvider = m_table ? m_table->GetAttrProvider() : NULL;
ba9574c3 5995 const wxGridCornerHeaderRenderer&
670d0177 5996 rend = attrProvider ? attrProvider->GetCornerRenderer()
7a0a6cc8
VZ
5997 : static_cast<wxGridCornerHeaderRenderer&>
5998 (gs_defaultHeaderRenderers.cornerRenderer);
ff72f628 5999
ba9574c3 6000 rend.DrawBorder(*this, dc, rect);
ff72f628
VZ
6001 }
6002}
6003
6004void wxGrid::DrawColLabel(wxDC& dc, int col)
f85afd4e 6005{
659af826 6006 if ( GetColWidth(col) <= 0 || m_colLabelHeight <= 0 )
7c1cb261 6007 return;
60ff3b99 6008
4d1bc39c 6009 int colLeft = GetColLeft(col);
ec157c8f 6010
ff72f628 6011 wxRect rect(colLeft, 0, GetColWidth(col), m_colLabelHeight);
670d0177
VZ
6012 wxGridCellAttrProvider * const
6013 attrProvider = m_table ? m_table->GetAttrProvider() : NULL;
ba9574c3 6014 const wxGridColumnHeaderRenderer&
670d0177 6015 rend = attrProvider ? attrProvider->GetColumnHeaderRenderer(col)
7a0a6cc8
VZ
6016 : static_cast<wxGridColumnHeaderRenderer&>
6017 (gs_defaultHeaderRenderers.colRenderer);
2f024384 6018
ff72f628 6019 if ( m_nativeColumnLabels )
71cf399f 6020 {
11393d29
VZ
6021 wxRendererNative::Get().DrawHeaderButton
6022 (
6023 GetColLabelWindow(),
6024 dc,
6025 rect,
6026 0,
6027 IsSortingBy(col)
6028 ? IsSortOrderAscending()
6029 ? wxHDR_SORT_ICON_UP
6030 : wxHDR_SORT_ICON_DOWN
6031 : wxHDR_SORT_ICON_NONE
6032 );
ba9574c3 6033 rect.Deflate(2);
71cf399f
RR
6034 }
6035 else
6036 {
b5e97a17
VZ
6037 // It is reported that we need to erase the background to avoid display
6038 // artefacts, see #12055.
6039 wxDCBrushChanger setBrush(dc, m_colWindow->GetBackgroundColour());
6040 dc.DrawRectangle(rect);
6041
ba9574c3 6042 rend.DrawBorder(*this, dc, rect);
71cf399f 6043 }
2f024384 6044
ff72f628 6045 int hAlign, vAlign;
ba9574c3 6046 GetColLabelAlignment(&hAlign, &vAlign);
ff72f628 6047 const int orient = GetColLabelTextOrientation();
60ff3b99 6048
ba9574c3 6049 rend.DrawLabel(*this, dc, GetColLabelValue(col), rect, hAlign, vAlign, orient);
f85afd4e
MB
6050}
6051
ff72f628
VZ
6052// TODO: these 2 functions should be replaced with wxDC::DrawLabel() to which
6053// we just have to add textOrientation support
2d66e025
MB
6054void wxGrid::DrawTextRectangle( wxDC& dc,
6055 const wxString& value,
6056 const wxRect& rect,
6057 int horizAlign,
d43851f7 6058 int vertAlign,
ba9574c3 6059 int textOrientation ) const
f85afd4e 6060{
2d66e025 6061 wxArrayString lines;
ef5df12b 6062
2d66e025 6063 StringToLines( value, lines );
ef5df12b 6064
ff72f628 6065 DrawTextRectangle(dc, lines, rect, horizAlign, vertAlign, textOrientation);
d10f4bf9
VZ
6066}
6067
4330c974 6068void wxGrid::DrawTextRectangle(wxDC& dc,
038a5591
JS
6069 const wxArrayString& lines,
6070 const wxRect& rect,
6071 int horizAlign,
6072 int vertAlign,
ba9574c3 6073 int textOrientation) const
d10f4bf9 6074{
4330c974
VZ
6075 if ( lines.empty() )
6076 return;
ef5df12b 6077
4330c974 6078 wxDCClipper clip(dc, rect);
ef5df12b 6079
4330c974
VZ
6080 long textWidth,
6081 textHeight;
ef5df12b 6082
4330c974
VZ
6083 if ( textOrientation == wxHORIZONTAL )
6084 GetTextBoxSize( dc, lines, &textWidth, &textHeight );
6085 else
6086 GetTextBoxSize( dc, lines, &textHeight, &textWidth );
ef5df12b 6087
4330c974
VZ
6088 int x = 0,
6089 y = 0;
6090 switch ( vertAlign )
6091 {
73145b0e 6092 case wxALIGN_BOTTOM:
4db6714b 6093 if ( textOrientation == wxHORIZONTAL )
038a5591 6094 y = rect.y + (rect.height - textHeight - 1);
d43851f7 6095 else
038a5591
JS
6096 x = rect.x + rect.width - textWidth;
6097 break;
ef5df12b 6098
73145b0e 6099 case wxALIGN_CENTRE:
4db6714b 6100 if ( textOrientation == wxHORIZONTAL )
a9339fe2 6101 y = rect.y + ((rect.height - textHeight) / 2);
d43851f7 6102 else
a9339fe2 6103 x = rect.x + ((rect.width - textWidth) / 2);
038a5591 6104 break;
ef5df12b 6105
73145b0e
JS
6106 case wxALIGN_TOP:
6107 default:
4db6714b 6108 if ( textOrientation == wxHORIZONTAL )
038a5591 6109 y = rect.y + 1;
d43851f7 6110 else
038a5591 6111 x = rect.x + 1;
73145b0e 6112 break;
4330c974 6113 }
ef5df12b 6114
4330c974
VZ
6115 // Align each line of a multi-line label
6116 size_t nLines = lines.GetCount();
6117 for ( size_t l = 0; l < nLines; l++ )
6118 {
6119 const wxString& line = lines[l];
ef5df12b 6120
9b7d3c09
VZ
6121 if ( line.empty() )
6122 {
6123 *(textOrientation == wxHORIZONTAL ? &y : &x) += dc.GetCharHeight();
6124 continue;
6125 }
6126
ad2633bd 6127 wxCoord lineWidth = 0,
c2b2c10e 6128 lineHeight = 0;
4330c974
VZ
6129 dc.GetTextExtent(line, &lineWidth, &lineHeight);
6130
6131 switch ( horizAlign )
6132 {
038a5591 6133 case wxALIGN_RIGHT:
4db6714b 6134 if ( textOrientation == wxHORIZONTAL )
038a5591
JS
6135 x = rect.x + (rect.width - lineWidth - 1);
6136 else
6137 y = rect.y + lineWidth + 1;
6138 break;
ef5df12b 6139
038a5591 6140 case wxALIGN_CENTRE:
4db6714b 6141 if ( textOrientation == wxHORIZONTAL )
2f024384 6142 x = rect.x + ((rect.width - lineWidth) / 2);
038a5591 6143 else
2f024384 6144 y = rect.y + rect.height - ((rect.height - lineWidth) / 2);
038a5591 6145 break;
ef5df12b 6146
038a5591
JS
6147 case wxALIGN_LEFT:
6148 default:
4db6714b 6149 if ( textOrientation == wxHORIZONTAL )
038a5591
JS
6150 x = rect.x + 1;
6151 else
6152 y = rect.y + rect.height - 1;
6153 break;
4330c974 6154 }
ef5df12b 6155
4330c974
VZ
6156 if ( textOrientation == wxHORIZONTAL )
6157 {
6158 dc.DrawText( line, x, y );
6159 y += lineHeight;
6160 }
6161 else
6162 {
6163 dc.DrawRotatedText( line, x, y, 90.0 );
6164 x += lineHeight;
038a5591 6165 }
f85afd4e 6166 }
f85afd4e
MB
6167}
6168
2f024384
DS
6169// Split multi-line text up into an array of strings.
6170// Any existing contents of the string array are preserved.
f85afd4e 6171//
696f3e9d 6172// TODO: refactor wxTextFile::Read() and reuse the same code from here
ef316e23 6173void wxGrid::StringToLines( const wxString& value, wxArrayString& lines ) const
f85afd4e 6174{
f85afd4e
MB
6175 int startPos = 0;
6176 int pos;
6d004f67 6177 wxString eol = wxTextFile::GetEOL( wxTextFileType_Unix );
2433bb2e 6178 wxString tVal = wxTextFile::Translate( value, wxTextFileType_Unix );
e2b42eeb 6179
faa94f3e 6180 while ( startPos < (int)tVal.length() )
f85afd4e 6181 {
2433bb2e 6182 pos = tVal.Mid(startPos).Find( eol );
f85afd4e
MB
6183 if ( pos < 0 )
6184 {
6185 break;
6186 }
6187 else if ( pos == 0 )
6188 {
6189 lines.Add( wxEmptyString );
6190 }
6191 else
6192 {
696f3e9d 6193 lines.Add( tVal.Mid(startPos, pos) );
f85afd4e 6194 }
a9339fe2 6195
4db6714b 6196 startPos += pos + 1;
f85afd4e 6197 }
4db6714b 6198
2eafd712 6199 if ( startPos < (int)tVal.length() )
f85afd4e 6200 {
696f3e9d 6201 lines.Add( tVal.Mid( startPos ) );
f85afd4e
MB
6202 }
6203}
6204
fbfb8bcc 6205void wxGrid::GetTextBoxSize( const wxDC& dc,
d10f4bf9 6206 const wxArrayString& lines,
ef316e23 6207 long *width, long *height ) const
f85afd4e 6208{
ad2633bd
RR
6209 wxCoord w = 0;
6210 wxCoord h = 0;
6211 wxCoord lineW = 0, lineH = 0;
f85afd4e 6212
b540eb2b 6213 size_t i;
56b6cf26 6214 for ( i = 0; i < lines.GetCount(); i++ )
f85afd4e
MB
6215 {
6216 dc.GetTextExtent( lines[i], &lineW, &lineH );
6217 w = wxMax( w, lineW );
6218 h += lineH;
6219 }
6220
6221 *width = w;
6222 *height = h;
6223}
6224
f6bcfd97
BP
6225//
6226// ------ Batch processing.
6227//
6228void wxGrid::EndBatch()
6229{
6230 if ( m_batchCount > 0 )
6231 {
6232 m_batchCount--;
6233 if ( !m_batchCount )
6234 {
6235 CalcDimensions();
6236 m_rowLabelWin->Refresh();
ad805b9e 6237 m_colWindow->Refresh();
f6bcfd97
BP
6238 m_cornerLabelWin->Refresh();
6239 m_gridWin->Refresh();
6240 }
6241 }
6242}
f85afd4e 6243
d8232393
MB
6244// Use this, rather than wxWindow::Refresh(), to force an immediate
6245// repainting of the grid. Has no effect if you are already inside a
6246// BeginBatch / EndBatch block.
6247//
6248void wxGrid::ForceRefresh()
6249{
6250 BeginBatch();
6251 EndBatch();
6252}
6253
bf646121
VZ
6254bool wxGrid::Enable(bool enable)
6255{
6256 if ( !wxScrolledWindow::Enable(enable) )
6257 return false;
6258
6259 // redraw in the new state
6260 m_gridWin->Refresh();
6261
6262 return true;
6263}
d8232393 6264
f85afd4e 6265//
2d66e025 6266// ------ Edit control functions
f85afd4e
MB
6267//
6268
2d66e025 6269void wxGrid::EnableEditing( bool edit )
f85afd4e 6270{
2d66e025 6271 if ( edit != m_editable )
f85afd4e 6272 {
4db6714b
KH
6273 if (!edit)
6274 EnableCellEditControl(edit);
2d66e025 6275 m_editable = edit;
f85afd4e 6276 }
f85afd4e
MB
6277}
6278
2d66e025 6279void wxGrid::EnableCellEditControl( bool enable )
f85afd4e 6280{
2c9a89e0
RD
6281 if (! m_editable)
6282 return;
6283
2c9a89e0
RD
6284 if ( enable != m_cellEditCtrlEnabled )
6285 {
dcfe4c3d 6286 if ( enable )
f85afd4e 6287 {
8a3e536c 6288 if ( SendEvent(wxEVT_GRID_EDITOR_SHOWN) == -1 )
97a9929e 6289 return;
fe7b9ed6 6290
97a9929e 6291 // this should be checked by the caller!
9a83f860 6292 wxASSERT_MSG( CanEnableCellControl(), wxT("can't enable editing for this cell!") );
b54ba671
VZ
6293
6294 // do it before ShowCellEditControl()
dcfe4c3d 6295 m_cellEditCtrlEnabled = enable;
b54ba671 6296
2d66e025 6297 ShowCellEditControl();
2d66e025
MB
6298 }
6299 else
6300 {
8a3e536c 6301 SendEvent(wxEVT_GRID_EDITOR_HIDDEN);
fe7b9ed6 6302
97a9929e 6303 HideCellEditControl();
2d66e025 6304 SaveEditControlValue();
b54ba671
VZ
6305
6306 // do it after HideCellEditControl()
dcfe4c3d 6307 m_cellEditCtrlEnabled = enable;
f85afd4e 6308 }
f85afd4e 6309 }
f85afd4e 6310}
f85afd4e 6311
b54ba671 6312bool wxGrid::IsCurrentCellReadOnly() const
283b7808 6313{
f48a1159
VZ
6314 wxGridCellAttr*
6315 attr = const_cast<wxGrid *>(this)->GetCellAttr(m_currentCellCoords);
b54ba671
VZ
6316 bool readonly = attr->IsReadOnly();
6317 attr->DecRef();
283b7808 6318
b54ba671
VZ
6319 return readonly;
6320}
283b7808 6321
b54ba671
VZ
6322bool wxGrid::CanEnableCellControl() const
6323{
20c84410
SN
6324 return m_editable && (m_currentCellCoords != wxGridNoCellCoords) &&
6325 !IsCurrentCellReadOnly();
b54ba671
VZ
6326}
6327
6328bool wxGrid::IsCellEditControlEnabled() const
6329{
6330 // the cell edit control might be disable for all cells or just for the
6331 // current one if it's read only
ca65c044 6332 return m_cellEditCtrlEnabled ? !IsCurrentCellReadOnly() : false;
283b7808
VZ
6333}
6334
f6bcfd97
BP
6335bool wxGrid::IsCellEditControlShown() const
6336{
ca65c044 6337 bool isShown = false;
f6bcfd97
BP
6338
6339 if ( m_cellEditCtrlEnabled )
6340 {
6341 int row = m_currentCellCoords.GetRow();
6342 int col = m_currentCellCoords.GetCol();
6343 wxGridCellAttr* attr = GetCellAttr(row, col);
6344 wxGridCellEditor* editor = attr->GetEditor((wxGrid*) this, row, col);
6345 attr->DecRef();
6346
6347 if ( editor )
6348 {
6349 if ( editor->IsCreated() )
6350 {
6351 isShown = editor->GetControl()->IsShown();
6352 }
6353
6354 editor->DecRef();
6355 }
6356 }
6357
6358 return isShown;
6359}
6360
2d66e025 6361void wxGrid::ShowCellEditControl()
f85afd4e 6362{
2d66e025 6363 if ( IsCellEditControlEnabled() )
f85afd4e 6364 {
7db713ae 6365 if ( !IsVisible( m_currentCellCoords, false ) )
2d66e025 6366 {
ca65c044 6367 m_cellEditCtrlEnabled = false;
2d66e025
MB
6368 return;
6369 }
6370 else
6371 {
2c9a89e0
RD
6372 wxRect rect = CellToRect( m_currentCellCoords );
6373 int row = m_currentCellCoords.GetRow();
6374 int col = m_currentCellCoords.GetCol();
2d66e025 6375
27f35b66
SN
6376 // if this is part of a multicell, find owner (topleft)
6377 int cell_rows, cell_cols;
6378 GetCellSize( row, col, &cell_rows, &cell_cols );
6379 if ( cell_rows <= 0 || cell_cols <= 0 )
6380 {
6381 row += cell_rows;
6382 col += cell_cols;
6383 m_currentCellCoords.SetRow( row );
6384 m_currentCellCoords.SetCol( col );
6385 }
6386
99306db2
VZ
6387 // erase the highlight and the cell contents because the editor
6388 // might not cover the entire cell
6389 wxClientDC dc( m_gridWin );
6390 PrepareDC( dc );
2c03d771 6391 wxGridCellAttr* attr = GetCellAttr(row, col);
ff72f628 6392 dc.SetBrush(wxBrush(attr->GetBackgroundColour()));
99306db2
VZ
6393 dc.SetPen(*wxTRANSPARENT_PEN);
6394 dc.DrawRectangle(rect);
d2fdd8d2 6395
6f706ee0
VZ
6396 // convert to scrolled coords
6397 CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
6398
6399 int nXMove = 0;
6400 if (rect.x < 0)
6401 nXMove = rect.x;
6402
99306db2 6403 // cell is shifted by one pixel
68c5a31c 6404 // However, don't allow x or y to become negative
70e8d961 6405 // since the SetSize() method interprets that as
68c5a31c
SN
6406 // "don't change."
6407 if (rect.x > 0)
6408 rect.x--;
6409 if (rect.y > 0)
6410 rect.y--;
f0102d2a 6411
28a77bc4 6412 wxGridCellEditor* editor = attr->GetEditor(this, row, col);
3da93aae
VZ
6413 if ( !editor->IsCreated() )
6414 {
ca65c044 6415 editor->Create(m_gridWin, wxID_ANY,
2c9a89e0 6416 new wxGridCellEditorEvtHandler(this, editor));
bf7945ce
RD
6417
6418 wxGridEditorCreatedEvent evt(GetId(),
6419 wxEVT_GRID_EDITOR_CREATED,
6420 this,
6421 row,
6422 col,
6423 editor->GetControl());
6424 GetEventHandler()->ProcessEvent(evt);
2d66e025
MB
6425 }
6426
27f35b66 6427 // resize editor to overflow into righthand cells if allowed
39b80349 6428 int maxWidth = rect.width;
27f35b66
SN
6429 wxString value = GetCellValue(row, col);
6430 if ( (value != wxEmptyString) && (attr->GetOverflow()) )
6431 {
39b80349 6432 int y;
2f024384
DS
6433 GetTextExtent(value, &maxWidth, &y, NULL, NULL, &attr->GetFont());
6434 if (maxWidth < rect.width)
6435 maxWidth = rect.width;
39b80349 6436 }
4db6714b 6437
39b80349 6438 int client_right = m_gridWin->GetClientSize().GetWidth();
2f024384 6439 if (rect.x + maxWidth > client_right)
2b5f62a0 6440 maxWidth = client_right - rect.x;
39b80349 6441
2b5f62a0
VZ
6442 if ((maxWidth > rect.width) && (col < m_numCols) && m_table)
6443 {
39b80349 6444 GetCellSize( row, col, &cell_rows, &cell_cols );
2b5f62a0 6445 // may have changed earlier
2f024384 6446 for (int i = col + cell_cols; i < m_numCols; i++)
2b5f62a0 6447 {
39b80349
SN
6448 int c_rows, c_cols;
6449 GetCellSize( row, i, &c_rows, &c_cols );
2f024384 6450
2b5f62a0 6451 // looks weird going over a multicell
bee19958 6452 if (m_table->IsEmptyCell( row, i ) &&
2b5f62a0 6453 (rect.width < maxWidth) && (c_rows == 1))
2f024384 6454 {
bee19958 6455 rect.width += GetColWidth( i );
2f024384 6456 }
2b5f62a0
VZ
6457 else
6458 break;
6459 }
4db6714b 6460
39b80349 6461 if (rect.GetRight() > client_right)
bee19958 6462 rect.SetRight( client_right - 1 );
27f35b66 6463 }
76c66f19 6464
bee19958 6465 editor->SetCellAttr( attr );
2c9a89e0 6466 editor->SetSize( rect );
e1a66d9a
RD
6467 if (nXMove != 0)
6468 editor->GetControl()->Move(
6469 editor->GetControl()->GetPosition().x + nXMove,
6470 editor->GetControl()->GetPosition().y );
ca65c044 6471 editor->Show( true, attr );
04418332
VZ
6472
6473 // recalc dimensions in case we need to
6474 // expand the scrolled window to account for editor
73145b0e 6475 CalcDimensions();
99306db2 6476
3da93aae 6477 editor->BeginEdit(row, col, this);
1bd71df9 6478 editor->SetCellAttr(NULL);
0b190b0f
VZ
6479
6480 editor->DecRef();
2c9a89e0 6481 attr->DecRef();
2b5f62a0 6482 }
f85afd4e
MB
6483 }
6484}
6485
2d66e025 6486void wxGrid::HideCellEditControl()
f85afd4e 6487{
2d66e025 6488 if ( IsCellEditControlEnabled() )
f85afd4e 6489 {
2c9a89e0
RD
6490 int row = m_currentCellCoords.GetRow();
6491 int col = m_currentCellCoords.GetCol();
6492
bee19958 6493 wxGridCellAttr *attr = GetCellAttr(row, col);
0b190b0f 6494 wxGridCellEditor *editor = attr->GetEditor(this, row, col);
7d277ac0 6495 const bool editorHadFocus = editor->GetControl()->HasFocus();
ca65c044 6496 editor->Show( false );
0b190b0f 6497 editor->DecRef();
2c9a89e0 6498 attr->DecRef();
2fb3e528 6499
7d277ac0
VZ
6500 // return the focus to the grid itself if the editor had it
6501 //
6502 // note that we must not do this unconditionally to avoid stealing
6503 // focus from the window which just received it if we are hiding the
6504 // editor precisely because we lost focus
6505 if ( editorHadFocus )
6506 m_gridWin->SetFocus();
2fb3e528 6507
27f35b66
SN
6508 // refresh whole row to the right
6509 wxRect rect( CellToRect(row, col) );
6510 CalcScrolledPosition(rect.x, rect.y, &rect.x, &rect.y );
6511 rect.width = m_gridWin->GetClientSize().GetWidth() - rect.x;
2f024384 6512
7d75e6c6
RD
6513#ifdef __WXMAC__
6514 // ensure that the pixels under the focus ring get refreshed as well
4db6714b 6515 rect.Inflate(10, 10);
7d75e6c6 6516#endif
2f024384 6517
ca65c044 6518 m_gridWin->Refresh( false, &rect );
f85afd4e 6519 }
2d66e025 6520}
8f177c8e 6521
2d66e025
MB
6522void wxGrid::SaveEditControlValue()
6523{
3da93aae
VZ
6524 if ( IsCellEditControlEnabled() )
6525 {
2c9a89e0
RD
6526 int row = m_currentCellCoords.GetRow();
6527 int col = m_currentCellCoords.GetCol();
8f177c8e 6528
4db6714b 6529 wxString oldval = GetCellValue(row, col);
fe7b9ed6 6530
3da93aae 6531 wxGridCellAttr* attr = GetCellAttr(row, col);
28a77bc4 6532 wxGridCellEditor* editor = attr->GetEditor(this, row, col);
2d66e025 6533
763163a8 6534 wxString newval;
78e78812 6535 bool changed = editor->EndEdit(row, col, this, oldval, &newval);
2d66e025 6536
763163a8 6537 if ( changed && SendEvent(wxEVT_GRID_CELL_CHANGING, newval) != -1 )
3da93aae 6538 {
763163a8
VZ
6539 editor->ApplyEdit(row, col, this);
6540
6541 // for compatibility reasons dating back to wx 2.8 when this event
6542 // was called wxEVT_GRID_CELL_CHANGE and wxEVT_GRID_CELL_CHANGING
6543 // didn't exist we allow vetoing this one too
6544 if ( SendEvent(wxEVT_GRID_CELL_CHANGED, oldval) == -1 )
4db6714b 6545 {
97a9929e 6546 // Event has been vetoed, set the data back.
4db6714b 6547 SetCellValue(row, col, oldval);
97a9929e 6548 }
2d66e025 6549 }
763163a8
VZ
6550
6551 editor->DecRef();
6552 attr->DecRef();
f85afd4e
MB
6553 }
6554}
6555
2d66e025 6556//
60ff3b99
VZ
6557// ------ Grid location functions
6558// Note that all of these functions work with the logical coordinates of
2d66e025
MB
6559// grid cells and labels so you will need to convert from device
6560// coordinates for mouse events etc.
6561//
6562
8a3e536c 6563wxGridCellCoords wxGrid::XYToCell(int x, int y) const
f85afd4e 6564{
58dd5b3b
MB
6565 int row = YToRow(y);
6566 int col = XToCol(x);
6567
8a3e536c
VZ
6568 return row == -1 || col == -1 ? wxGridNoCellCoords
6569 : wxGridCellCoords(row, col);
2d66e025 6570}
f85afd4e 6571
bec70262
VZ
6572// compute row or column from some (unscrolled) coordinate value, using either
6573// m_defaultRowHeight/m_defaultColWidth or binary search on array of
5bce3d57
VZ
6574// m_rowBottoms/m_colRights to do it quickly in O(log n) time.
6575// NOTE: This may not work correctly for reordered columns.
cd4f6f5f
VZ
6576int wxGrid::PosToLinePos(int coord,
6577 bool clipToMinMax,
6578 const wxGridOperations& oper) const
2d66e025 6579{
bec70262 6580 const int numLines = oper.GetNumberOfLines(this);
a967f048 6581
bec70262 6582 if ( coord < 0 )
cd4f6f5f 6583 return clipToMinMax && numLines > 0 ? 0 : wxNOT_FOUND;
a967f048 6584
bec70262
VZ
6585 const int defaultLineSize = oper.GetDefaultLineSize(this);
6586 wxCHECK_MSG( defaultLineSize, -1, "can't have 0 default line size" );
d57ad377 6587
bec70262
VZ
6588 int maxPos = coord / defaultLineSize,
6589 minPos = 0;
8f177c8e 6590
bec70262
VZ
6591 // check for the simplest case: if we have no explicit line sizes
6592 // configured, then we already know the line this position falls in
6593 const wxArrayInt& lineEnds = oper.GetLineEnds(this);
6594 if ( lineEnds.empty() )
f85afd4e 6595 {
bec70262
VZ
6596 if ( maxPos < numLines )
6597 return maxPos;
4db6714b 6598
bec70262 6599 return clipToMinMax ? numLines - 1 : -1;
f85afd4e 6600 }
4db6714b 6601
2d66e025 6602
5bce3d57
VZ
6603 // binary search is quite efficient and we can't really make any assumptions
6604 // on where to start here since row and columns could be of size 0 if they are
6605 // hidden. While this could be made more efficient, some profiling will be
6606 // necessary to determine if it really is a performance bottleneck
6607 maxPos = numLines - 1;
d4175745 6608
bec70262
VZ
6609 // check if the position is beyond the last column
6610 const int lineAtMaxPos = oper.GetLineAt(this, maxPos);
6611 if ( coord >= lineEnds[lineAtMaxPos] )
cd4f6f5f 6612 return clipToMinMax ? maxPos : -1;
d4175745 6613
bec70262
VZ
6614 // or before the first one
6615 const int lineAt0 = oper.GetLineAt(this, 0);
6616 if ( coord < lineEnds[lineAt0] )
cd4f6f5f 6617 return 0;
d4175745 6618
bec70262
VZ
6619
6620 // finally do perform the binary search
6621 while ( minPos < maxPos )
d4175745 6622 {
bec70262
VZ
6623 wxCHECK_MSG( lineEnds[oper.GetLineAt(this, minPos)] <= coord &&
6624 coord < lineEnds[oper.GetLineAt(this, maxPos)],
6625 -1,
cd4f6f5f 6626 "wxGrid: internal error in PosToLinePos()" );
d4175745 6627
bec70262 6628 if ( coord >= lineEnds[oper.GetLineAt(this, maxPos - 1)] )
cd4f6f5f 6629 return maxPos;
d4175745
VZ
6630 else
6631 maxPos--;
bec70262
VZ
6632
6633 const int median = minPos + (maxPos - minPos + 1) / 2;
6634 if ( coord < lineEnds[oper.GetLineAt(this, median)] )
d4175745
VZ
6635 maxPos = median;
6636 else
6637 minPos = median;
6638 }
bec70262 6639
cd4f6f5f
VZ
6640 return maxPos;
6641}
6642
6643int
6644wxGrid::PosToLine(int coord,
6645 bool clipToMinMax,
6646 const wxGridOperations& oper) const
6647{
6648 int pos = PosToLinePos(coord, clipToMinMax, oper);
6649
6650 return pos == wxNOT_FOUND ? wxNOT_FOUND : oper.GetLineAt(this, pos);
bec70262
VZ
6651}
6652
6653int wxGrid::YToRow(int y, bool clipToMinMax) const
6654{
6655 return PosToLine(y, clipToMinMax, wxGridRowOperations());
6656}
6657
6658int wxGrid::XToCol(int x, bool clipToMinMax) const
6659{
6660 return PosToLine(x, clipToMinMax, wxGridColumnOperations());
2d66e025 6661}
8f177c8e 6662
cd4f6f5f
VZ
6663int wxGrid::XToPos(int x) const
6664{
6665 return PosToLinePos(x, true /* clip */, wxGridColumnOperations());
6666}
6667
71b619d7 6668// return the row/col number such that the pos is near the edge of, or -1 if
10a4531d
VZ
6669// not near an edge.
6670//
82edfbe7
VZ
6671// notice that position can only possibly be near an edge if the row/column is
6672// large enough to still allow for an "inner" area that is _not_ near the edge
6673// (i.e., if the height/width is smaller than WXGRID_LABEL_EDGE_ZONE, pos will
6674// _never_ be considered to be near the edge).
10a4531d 6675int wxGrid::PosToEdgeOfLine(int pos, const wxGridOperations& oper) const
2d66e025 6676{
71b619d7
VZ
6677 // Get the bottom or rightmost line that could match.
6678 int line = oper.PosToLine(this, pos, true);
10a4531d 6679
6df84c5c
VZ
6680 if ( oper.GetLineSize(this, line) > WXGRID_LABEL_EDGE_ZONE )
6681 {
6682 // We know that we are in this line, test whether we are close enough
6683 // to start or end border, respectively.
6684 if ( abs(oper.GetLineEndPos(this, line) - pos) < WXGRID_LABEL_EDGE_ZONE )
6685 return line;
6686 else if ( line > 0 &&
6687 pos - oper.GetLineStartPos(this,
6688 line) < WXGRID_LABEL_EDGE_ZONE )
6689 {
6690 // We need to find the previous visible line, so skip all the
6691 // hidden (of size 0) ones.
6692 do
6693 {
6694 line = oper.GetLineBefore(this, line);
6695 }
6696 while ( line >= 0 && oper.GetLineSize(this, line) == 0 );
2d66e025 6697
6df84c5c
VZ
6698 // It can possibly be -1 here.
6699 return line;
6700 }
6701 }
71b619d7 6702
2d66e025
MB
6703 return -1;
6704}
6705
10a4531d 6706int wxGrid::YToEdgeOfRow(int y) const
2d66e025 6707{
10a4531d
VZ
6708 return PosToEdgeOfLine(y, wxGridRowOperations());
6709}
2d66e025 6710
10a4531d
VZ
6711int wxGrid::XToEdgeOfCol(int x) const
6712{
6713 return PosToEdgeOfLine(x, wxGridColumnOperations());
f85afd4e
MB
6714}
6715
ef316e23 6716wxRect wxGrid::CellToRect( int row, int col ) const
f85afd4e 6717{
2d66e025 6718 wxRect rect( -1, -1, -1, -1 );
f85afd4e 6719
2f024384
DS
6720 if ( row >= 0 && row < m_numRows &&
6721 col >= 0 && col < m_numCols )
f85afd4e 6722 {
27f35b66
SN
6723 int i, cell_rows, cell_cols;
6724 rect.width = rect.height = 0;
6725 GetCellSize( row, col, &cell_rows, &cell_cols );
6726 // if negative then find multicell owner
2f024384
DS
6727 if (cell_rows < 0)
6728 row += cell_rows;
6729 if (cell_cols < 0)
6730 col += cell_cols;
27f35b66
SN
6731 GetCellSize( row, col, &cell_rows, &cell_cols );
6732
7c1cb261
VZ
6733 rect.x = GetColLeft(col);
6734 rect.y = GetRowTop(row);
2f024384
DS
6735 for (i=col; i < col + cell_cols; i++)
6736 rect.width += GetColWidth(i);
6737 for (i=row; i < row + cell_rows; i++)
27f35b66 6738 rect.height += GetRowHeight(i);
f85afd4e 6739
efc49a21
VZ
6740 // if grid lines are enabled, then the area of the cell is a bit smaller
6741 if (m_gridLinesEnabled)
6742 {
6743 rect.width -= 1;
6744 rect.height -= 1;
6745 }
f6bcfd97 6746 }
4db6714b 6747
2d66e025
MB
6748 return rect;
6749}
6750
ef316e23 6751bool wxGrid::IsVisible( int row, int col, bool wholeCellVisible ) const
2d66e025
MB
6752{
6753 // get the cell rectangle in logical coords
6754 //
6755 wxRect r( CellToRect( row, col ) );
60ff3b99 6756
2d66e025
MB
6757 // convert to device coords
6758 //
6759 int left, top, right, bottom;
6760 CalcScrolledPosition( r.GetLeft(), r.GetTop(), &left, &top );
6761 CalcScrolledPosition( r.GetRight(), r.GetBottom(), &right, &bottom );
60ff3b99 6762
2d66e025 6763 // check against the client area of the grid window
2d66e025
MB
6764 int cw, ch;
6765 m_gridWin->GetClientSize( &cw, &ch );
60ff3b99 6766
2d66e025 6767 if ( wholeCellVisible )
f85afd4e 6768 {
2d66e025 6769 // is the cell wholly visible ?
2f024384
DS
6770 return ( left >= 0 && right <= cw &&
6771 top >= 0 && bottom <= ch );
2d66e025
MB
6772 }
6773 else
6774 {
6775 // is the cell partly visible ?
6776 //
2f024384
DS
6777 return ( ((left >= 0 && left < cw) || (right > 0 && right <= cw)) &&
6778 ((top >= 0 && top < ch) || (bottom > 0 && bottom <= ch)) );
2d66e025
MB
6779 }
6780}
6781
2d66e025
MB
6782// make the specified cell location visible by doing a minimal amount
6783// of scrolling
6784//
6785void wxGrid::MakeCellVisible( int row, int col )
6786{
6787 int i;
60ff3b99 6788 int xpos = -1, ypos = -1;
2d66e025 6789
2f024384
DS
6790 if ( row >= 0 && row < m_numRows &&
6791 col >= 0 && col < m_numCols )
2d66e025
MB
6792 {
6793 // get the cell rectangle in logical coords
2d66e025 6794 wxRect r( CellToRect( row, col ) );
60ff3b99 6795
2d66e025 6796 // convert to device coords
2d66e025
MB
6797 int left, top, right, bottom;
6798 CalcScrolledPosition( r.GetLeft(), r.GetTop(), &left, &top );
6799 CalcScrolledPosition( r.GetRight(), r.GetBottom(), &right, &bottom );
60ff3b99 6800
2d66e025
MB
6801 int cw, ch;
6802 m_gridWin->GetClientSize( &cw, &ch );
60ff3b99 6803
2d66e025 6804 if ( top < 0 )
3f296516 6805 {
2d66e025 6806 ypos = r.GetTop();
3f296516 6807 }
2d66e025
MB
6808 else if ( bottom > ch )
6809 {
6810 int h = r.GetHeight();
6811 ypos = r.GetTop();
56b6cf26 6812 for ( i = row - 1; i >= 0; i-- )
2d66e025 6813 {
7c1cb261
VZ
6814 int rowHeight = GetRowHeight(i);
6815 if ( h + rowHeight > ch )
6816 break;
2d66e025 6817
7c1cb261
VZ
6818 h += rowHeight;
6819 ypos -= rowHeight;
2d66e025 6820 }
f0102d2a
VZ
6821
6822 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
56b6cf26
DS
6823 // have rounding errors (this is important, because if we do,
6824 // we might not scroll at all and some cells won't be redrawn)
275c4ae4 6825 //
56b6cf26
DS
6826 // Sometimes GRID_SCROLL_LINE / 2 is not enough,
6827 // so just add a full scroll unit...
fd76c6a7 6828 ypos += m_yScrollPixelsPerLine;
2d66e025
MB
6829 }
6830
7db713ae 6831 // special handling for wide cells - show always left part of the cell!
faa94f3e 6832 // Otherwise, e.g. when stepping from row to row, it would jump between
7db713ae
JS
6833 // left and right part of the cell on every step!
6834// if ( left < 0 )
ccdee36f 6835 if ( left < 0 || (right - left) >= cw )
2d66e025
MB
6836 {
6837 xpos = r.GetLeft();
6838 }
6839 else if ( right > cw )
6840 {
73145b0e
JS
6841 // position the view so that the cell is on the right
6842 int x0, y0;
6843 CalcUnscrolledPosition(0, 0, &x0, &y0);
6844 xpos = x0 + (right - cw);
f0102d2a
VZ
6845
6846 // see comment for ypos above
fd76c6a7 6847 xpos += m_xScrollPixelsPerLine;
2d66e025
MB
6848 }
6849
ccdee36f 6850 if ( xpos != -1 || ypos != -1 )
2d66e025 6851 {
97a9929e 6852 if ( xpos != -1 )
fd76c6a7 6853 xpos /= m_xScrollPixelsPerLine;
97a9929e 6854 if ( ypos != -1 )
fd76c6a7 6855 ypos /= m_yScrollPixelsPerLine;
2d66e025
MB
6856 Scroll( xpos, ypos );
6857 AdjustScrollbars();
6858 }
6859 }
6860}
6861
2d66e025
MB
6862//
6863// ------ Grid cursor movement functions
6864//
6865
10a4531d
VZ
6866bool
6867wxGrid::DoMoveCursor(bool expandSelection,
6868 const wxGridDirectionOperations& diroper)
2d66e025 6869{
10a4531d
VZ
6870 if ( m_currentCellCoords == wxGridNoCellCoords )
6871 return false;
6872
6873 if ( expandSelection )
2d66e025 6874 {
8a3e536c
VZ
6875 wxGridCellCoords coords = m_selectedBlockCorner;
6876 if ( coords == wxGridNoCellCoords )
6877 coords = m_currentCellCoords;
4db6714b 6878
8a3e536c 6879 if ( diroper.IsAtBoundary(coords) )
10a4531d 6880 return false;
2d66e025 6881
8a3e536c 6882 diroper.Advance(coords);
2d66e025 6883
8a3e536c 6884 UpdateBlockBeingSelected(m_currentCellCoords, coords);
10a4531d 6885 }
8a3e536c 6886 else // don't expand selection
f85afd4e 6887 {
8a3e536c
VZ
6888 ClearSelection();
6889
10a4531d 6890 if ( diroper.IsAtBoundary(m_currentCellCoords) )
ca65c044 6891 return false;
4db6714b 6892
10a4531d
VZ
6893 wxGridCellCoords coords = m_currentCellCoords;
6894 diroper.Advance(coords);
8a3e536c
VZ
6895
6896 GoToCell(coords);
f85afd4e 6897 }
2d66e025 6898
10a4531d 6899 return true;
f85afd4e
MB
6900}
6901
10a4531d 6902bool wxGrid::MoveCursorUp(bool expandSelection)
f85afd4e 6903{
10a4531d
VZ
6904 return DoMoveCursor(expandSelection,
6905 wxGridBackwardOperations(this, wxGridRowOperations()));
2d66e025
MB
6906}
6907
10a4531d 6908bool wxGrid::MoveCursorDown(bool expandSelection)
2d66e025 6909{
10a4531d
VZ
6910 return DoMoveCursor(expandSelection,
6911 wxGridForwardOperations(this, wxGridRowOperations()));
6912}
4db6714b 6913
10a4531d
VZ
6914bool wxGrid::MoveCursorLeft(bool expandSelection)
6915{
6916 return DoMoveCursor(expandSelection,
6917 wxGridBackwardOperations(this, wxGridColumnOperations()));
6918}
8f177c8e 6919
10a4531d
VZ
6920bool wxGrid::MoveCursorRight(bool expandSelection)
6921{
6922 return DoMoveCursor(expandSelection,
6923 wxGridForwardOperations(this, wxGridColumnOperations()));
2d66e025
MB
6924}
6925
10a4531d 6926bool wxGrid::DoMoveCursorByPage(const wxGridDirectionOperations& diroper)
2d66e025 6927{
4db6714b
KH
6928 if ( m_currentCellCoords == wxGridNoCellCoords )
6929 return false;
60ff3b99 6930
10a4531d
VZ
6931 if ( diroper.IsAtBoundary(m_currentCellCoords) )
6932 return false;
ef5df12b 6933
10a4531d
VZ
6934 const int oldRow = m_currentCellCoords.GetRow();
6935 int newRow = diroper.MoveByPixelDistance(oldRow, m_gridWin->GetClientSize().y);
6936 if ( newRow == oldRow )
6937 {
6938 wxGridCellCoords coords(m_currentCellCoords);
6939 diroper.Advance(coords);
6940 newRow = coords.GetRow();
6941 }
8f177c8e 6942
8a3e536c 6943 GoToCell(newRow, m_currentCellCoords.GetCol());
60ff3b99 6944
10a4531d
VZ
6945 return true;
6946}
2d66e025 6947
10a4531d
VZ
6948bool wxGrid::MovePageUp()
6949{
6950 return DoMoveCursorByPage(
6951 wxGridBackwardOperations(this, wxGridRowOperations()));
2d66e025
MB
6952}
6953
6954bool wxGrid::MovePageDown()
6955{
10a4531d 6956 return DoMoveCursorByPage(
d188378e 6957 wxGridForwardOperations(this, wxGridRowOperations()));
10a4531d 6958}
60ff3b99 6959
10a4531d
VZ
6960// helper of DoMoveCursorByBlock(): advance the cell coordinates using diroper
6961// until we find a non-empty cell or reach the grid end
6962void
6963wxGrid::AdvanceToNextNonEmpty(wxGridCellCoords& coords,
6964 const wxGridDirectionOperations& diroper)
6965{
6966 while ( !diroper.IsAtBoundary(coords) )
f85afd4e 6967 {
10a4531d
VZ
6968 diroper.Advance(coords);
6969 if ( !m_table->IsEmpty(coords) )
6970 break;
f85afd4e
MB
6971 }
6972}
8f177c8e 6973
10a4531d
VZ
6974bool
6975wxGrid::DoMoveCursorByBlock(bool expandSelection,
6976 const wxGridDirectionOperations& diroper)
2d66e025 6977{
10a4531d
VZ
6978 if ( !m_table || m_currentCellCoords == wxGridNoCellCoords )
6979 return false;
f85afd4e 6980
10a4531d
VZ
6981 if ( diroper.IsAtBoundary(m_currentCellCoords) )
6982 return false;
4db6714b 6983
10a4531d
VZ
6984 wxGridCellCoords coords(m_currentCellCoords);
6985 if ( m_table->IsEmpty(coords) )
6986 {
6987 // we are in an empty cell: find the next block of non-empty cells
6988 AdvanceToNextNonEmpty(coords, diroper);
2d66e025 6989 }
10a4531d 6990 else // current cell is not empty
f85afd4e 6991 {
10a4531d
VZ
6992 diroper.Advance(coords);
6993 if ( m_table->IsEmpty(coords) )
2d66e025 6994 {
10a4531d
VZ
6995 // we started at the end of a block, find the next one
6996 AdvanceToNextNonEmpty(coords, diroper);
2d66e025 6997 }
10a4531d 6998 else // we're in a middle of a block
2d66e025 6999 {
10a4531d
VZ
7000 // go to the end of it, i.e. find the last cell before the next
7001 // empty one
7002 while ( !diroper.IsAtBoundary(coords) )
2d66e025 7003 {
10a4531d
VZ
7004 wxGridCellCoords coordsNext(coords);
7005 diroper.Advance(coordsNext);
7006 if ( m_table->IsEmpty(coordsNext) )
2d66e025 7007 break;
2d66e025 7008
10a4531d
VZ
7009 coords = coordsNext;
7010 }
aa5e1f75 7011 }
10a4531d 7012 }
2d66e025 7013
10a4531d
VZ
7014 if ( expandSelection )
7015 {
8a3e536c 7016 UpdateBlockBeingSelected(m_currentCellCoords, coords);
10a4531d
VZ
7017 }
7018 else
7019 {
7020 ClearSelection();
8a3e536c 7021 GoToCell(coords);
f85afd4e 7022 }
f85afd4e 7023
10a4531d 7024 return true;
2d66e025 7025}
f85afd4e 7026
10a4531d 7027bool wxGrid::MoveCursorUpBlock(bool expandSelection)
f85afd4e 7028{
10a4531d
VZ
7029 return DoMoveCursorByBlock(
7030 expandSelection,
7031 wxGridBackwardOperations(this, wxGridRowOperations())
7032 );
7033}
8f177c8e 7034
10a4531d
VZ
7035bool wxGrid::MoveCursorDownBlock( bool expandSelection )
7036{
7037 return DoMoveCursorByBlock(
7038 expandSelection,
7039 wxGridForwardOperations(this, wxGridRowOperations())
7040 );
7041}
2d66e025 7042
10a4531d
VZ
7043bool wxGrid::MoveCursorLeftBlock( bool expandSelection )
7044{
7045 return DoMoveCursorByBlock(
7046 expandSelection,
7047 wxGridBackwardOperations(this, wxGridColumnOperations())
7048 );
f85afd4e
MB
7049}
7050
5c8fc7c1 7051bool wxGrid::MoveCursorRightBlock( bool expandSelection )
f85afd4e 7052{
10a4531d
VZ
7053 return DoMoveCursorByBlock(
7054 expandSelection,
7055 wxGridForwardOperations(this, wxGridColumnOperations())
7056 );
f85afd4e
MB
7057}
7058
f85afd4e 7059//
2d66e025 7060// ------ Label values and formatting
f85afd4e
MB
7061//
7062
ef316e23 7063void wxGrid::GetRowLabelAlignment( int *horiz, int *vert ) const
f85afd4e 7064{
2f024384
DS
7065 if ( horiz )
7066 *horiz = m_rowLabelHorizAlign;
7067 if ( vert )
7068 *vert = m_rowLabelVertAlign;
f85afd4e
MB
7069}
7070
ef316e23 7071void wxGrid::GetColLabelAlignment( int *horiz, int *vert ) const
f85afd4e 7072{
2f024384
DS
7073 if ( horiz )
7074 *horiz = m_colLabelHorizAlign;
7075 if ( vert )
7076 *vert = m_colLabelVertAlign;
f85afd4e
MB
7077}
7078
ef316e23 7079int wxGrid::GetColLabelTextOrientation() const
d43851f7
JS
7080{
7081 return m_colLabelTextOrientation;
7082}
7083
ef316e23 7084wxString wxGrid::GetRowLabelValue( int row ) const
f85afd4e
MB
7085{
7086 if ( m_table )
7087 {
7088 return m_table->GetRowLabelValue( row );
7089 }
7090 else
7091 {
7092 wxString s;
7093 s << row;
7094 return s;
7095 }
7096}
7097
ef316e23 7098wxString wxGrid::GetColLabelValue( int col ) const
f85afd4e
MB
7099{
7100 if ( m_table )
7101 {
7102 return m_table->GetColLabelValue( col );
7103 }
7104 else
7105 {
7106 wxString s;
7107 s << col;
7108 return s;
7109 }
7110}
7111
7112void wxGrid::SetRowLabelSize( int width )
7113{
733f486a
VZ
7114 wxASSERT( width >= 0 || width == wxGRID_AUTOSIZE );
7115
7116 if ( width == wxGRID_AUTOSIZE )
7117 {
7118 width = CalcColOrRowLabelAreaMinSize(wxGRID_ROW);
7119 }
7120
2e8cd977
MB
7121 if ( width != m_rowLabelWidth )
7122 {
2e8cd977
MB
7123 if ( width == 0 )
7124 {
ca65c044
WS
7125 m_rowLabelWin->Show( false );
7126 m_cornerLabelWin->Show( false );
2e8cd977 7127 }
7807d81c 7128 else if ( m_rowLabelWidth == 0 )
2e8cd977 7129 {
ca65c044 7130 m_rowLabelWin->Show( true );
2f024384
DS
7131 if ( m_colLabelHeight > 0 )
7132 m_cornerLabelWin->Show( true );
2e8cd977 7133 }
b99be8fb 7134
2e8cd977 7135 m_rowLabelWidth = width;
a6b40a9a 7136 InvalidateBestSize();
7807d81c 7137 CalcWindowSizes();
ca65c044 7138 wxScrolledWindow::Refresh( true );
2e8cd977 7139 }
f85afd4e
MB
7140}
7141
7142void wxGrid::SetColLabelSize( int height )
7143{
733f486a
VZ
7144 wxASSERT( height >=0 || height == wxGRID_AUTOSIZE );
7145
7146 if ( height == wxGRID_AUTOSIZE )
7147 {
7148 height = CalcColOrRowLabelAreaMinSize(wxGRID_COLUMN);
7149 }
7150
2e8cd977
MB
7151 if ( height != m_colLabelHeight )
7152 {
2e8cd977
MB
7153 if ( height == 0 )
7154 {
ad805b9e 7155 m_colWindow->Show( false );
ca65c044 7156 m_cornerLabelWin->Show( false );
2e8cd977 7157 }
7807d81c 7158 else if ( m_colLabelHeight == 0 )
2e8cd977 7159 {
ad805b9e 7160 m_colWindow->Show( true );
a9339fe2
DS
7161 if ( m_rowLabelWidth > 0 )
7162 m_cornerLabelWin->Show( true );
2e8cd977 7163 }
7807d81c 7164
2e8cd977 7165 m_colLabelHeight = height;
a6b40a9a 7166 InvalidateBestSize();
7807d81c 7167 CalcWindowSizes();
ca65c044 7168 wxScrolledWindow::Refresh( true );
2e8cd977 7169 }
f85afd4e
MB
7170}
7171
7172void wxGrid::SetLabelBackgroundColour( const wxColour& colour )
7173{
2d66e025
MB
7174 if ( m_labelBackgroundColour != colour )
7175 {
7176 m_labelBackgroundColour = colour;
7177 m_rowLabelWin->SetBackgroundColour( colour );
ad805b9e 7178 m_colWindow->SetBackgroundColour( colour );
2d66e025
MB
7179 m_cornerLabelWin->SetBackgroundColour( colour );
7180
7181 if ( !GetBatchCount() )
7182 {
7183 m_rowLabelWin->Refresh();
ad805b9e 7184 m_colWindow->Refresh();
2d66e025
MB
7185 m_cornerLabelWin->Refresh();
7186 }
7187 }
f85afd4e
MB
7188}
7189
7190void wxGrid::SetLabelTextColour( const wxColour& colour )
7191{
2d66e025
MB
7192 if ( m_labelTextColour != colour )
7193 {
7194 m_labelTextColour = colour;
7195 if ( !GetBatchCount() )
7196 {
7197 m_rowLabelWin->Refresh();
ad805b9e 7198 m_colWindow->Refresh();
2d66e025
MB
7199 }
7200 }
f85afd4e
MB
7201}
7202
7203void wxGrid::SetLabelFont( const wxFont& font )
7204{
7205 m_labelFont = font;
2d66e025
MB
7206 if ( !GetBatchCount() )
7207 {
7208 m_rowLabelWin->Refresh();
ad805b9e 7209 m_colWindow->Refresh();
2d66e025 7210 }
f85afd4e
MB
7211}
7212
7213void wxGrid::SetRowLabelAlignment( int horiz, int vert )
7214{
4c7277db
MB
7215 // allow old (incorrect) defs to be used
7216 switch ( horiz )
7217 {
7218 case wxLEFT: horiz = wxALIGN_LEFT; break;
7219 case wxRIGHT: horiz = wxALIGN_RIGHT; break;
7220 case wxCENTRE: horiz = wxALIGN_CENTRE; break;
7221 }
84912ef8 7222
4c7277db
MB
7223 switch ( vert )
7224 {
7225 case wxTOP: vert = wxALIGN_TOP; break;
7226 case wxBOTTOM: vert = wxALIGN_BOTTOM; break;
7227 case wxCENTRE: vert = wxALIGN_CENTRE; break;
7228 }
84912ef8 7229
4c7277db 7230 if ( horiz == wxALIGN_LEFT || horiz == wxALIGN_CENTRE || horiz == wxALIGN_RIGHT )
f85afd4e
MB
7231 {
7232 m_rowLabelHorizAlign = horiz;
7233 }
8f177c8e 7234
4c7277db 7235 if ( vert == wxALIGN_TOP || vert == wxALIGN_CENTRE || vert == wxALIGN_BOTTOM )
f85afd4e
MB
7236 {
7237 m_rowLabelVertAlign = vert;
7238 }
7239
2d66e025
MB
7240 if ( !GetBatchCount() )
7241 {
7242 m_rowLabelWin->Refresh();
60ff3b99 7243 }
f85afd4e
MB
7244}
7245
7246void wxGrid::SetColLabelAlignment( int horiz, int vert )
7247{
4c7277db
MB
7248 // allow old (incorrect) defs to be used
7249 switch ( horiz )
7250 {
7251 case wxLEFT: horiz = wxALIGN_LEFT; break;
7252 case wxRIGHT: horiz = wxALIGN_RIGHT; break;
7253 case wxCENTRE: horiz = wxALIGN_CENTRE; break;
7254 }
84912ef8 7255
4c7277db
MB
7256 switch ( vert )
7257 {
7258 case wxTOP: vert = wxALIGN_TOP; break;
7259 case wxBOTTOM: vert = wxALIGN_BOTTOM; break;
7260 case wxCENTRE: vert = wxALIGN_CENTRE; break;
7261 }
84912ef8 7262
4c7277db 7263 if ( horiz == wxALIGN_LEFT || horiz == wxALIGN_CENTRE || horiz == wxALIGN_RIGHT )
f85afd4e
MB
7264 {
7265 m_colLabelHorizAlign = horiz;
7266 }
8f177c8e 7267
4c7277db 7268 if ( vert == wxALIGN_TOP || vert == wxALIGN_CENTRE || vert == wxALIGN_BOTTOM )
f85afd4e
MB
7269 {
7270 m_colLabelVertAlign = vert;
7271 }
7272
2d66e025
MB
7273 if ( !GetBatchCount() )
7274 {
ad805b9e 7275 m_colWindow->Refresh();
60ff3b99 7276 }
f85afd4e
MB
7277}
7278
ca65c044
WS
7279// Note: under MSW, the default column label font must be changed because it
7280// does not support vertical printing
d43851f7
JS
7281//
7282// Example: wxFont font(9, wxSWISS, wxNORMAL, wxBOLD);
ca65c044
WS
7283// pGrid->SetLabelFont(font);
7284// pGrid->SetColLabelTextOrientation(wxVERTICAL);
d43851f7
JS
7285//
7286void wxGrid::SetColLabelTextOrientation( int textOrientation )
7287{
4db6714b 7288 if ( textOrientation == wxHORIZONTAL || textOrientation == wxVERTICAL )
d43851f7 7289 m_colLabelTextOrientation = textOrientation;
d43851f7
JS
7290
7291 if ( !GetBatchCount() )
ad805b9e 7292 m_colWindow->Refresh();
d43851f7
JS
7293}
7294
f85afd4e
MB
7295void wxGrid::SetRowLabelValue( int row, const wxString& s )
7296{
7297 if ( m_table )
7298 {
7299 m_table->SetRowLabelValue( row, s );
2d66e025
MB
7300 if ( !GetBatchCount() )
7301 {
ccdee36f 7302 wxRect rect = CellToRect( row, 0 );
70c7a608
SN
7303 if ( rect.height > 0 )
7304 {
cb309039 7305 CalcScrolledPosition(0, rect.y, &rect.x, &rect.y);
b1944ebc 7306 rect.x = 0;
70c7a608 7307 rect.width = m_rowLabelWidth;
ca65c044 7308 m_rowLabelWin->Refresh( true, &rect );
70c7a608 7309 }
2d66e025 7310 }
f85afd4e
MB
7311 }
7312}
7313
7314void wxGrid::SetColLabelValue( int col, const wxString& s )
7315{
7316 if ( m_table )
7317 {
7318 m_table->SetColLabelValue( col, s );
2d66e025
MB
7319 if ( !GetBatchCount() )
7320 {
ad805b9e 7321 if ( m_useNativeHeader )
70c7a608 7322 {
3039ade9 7323 GetGridColHeader()->UpdateColumn(col);
ad805b9e
VZ
7324 }
7325 else
7326 {
7327 wxRect rect = CellToRect( 0, col );
7328 if ( rect.width > 0 )
7329 {
7330 CalcScrolledPosition(rect.x, 0, &rect.x, &rect.y);
7331 rect.y = 0;
7332 rect.height = m_colLabelHeight;
7333 GetColLabelWindow()->Refresh( true, &rect );
7334 }
70c7a608 7335 }
2d66e025 7336 }
f85afd4e
MB
7337 }
7338}
7339
7340void wxGrid::SetGridLineColour( const wxColour& colour )
7341{
2d66e025
MB
7342 if ( m_gridLineColour != colour )
7343 {
7344 m_gridLineColour = colour;
60ff3b99 7345
9f7aee01
VZ
7346 if ( GridLinesEnabled() )
7347 RedrawGridLines();
2d66e025 7348 }
f85afd4e
MB
7349}
7350
f6bcfd97
BP
7351void wxGrid::SetCellHighlightColour( const wxColour& colour )
7352{
7353 if ( m_cellHighlightColour != colour )
7354 {
7355 m_cellHighlightColour = colour;
7356
7357 wxClientDC dc( m_gridWin );
7358 PrepareDC( dc );
7359 wxGridCellAttr* attr = GetCellAttr(m_currentCellCoords);
7360 DrawCellHighlight(dc, attr);
7361 attr->DecRef();
7362 }
7363}
7364
d2520c85
RD
7365void wxGrid::SetCellHighlightPenWidth(int width)
7366{
4db6714b
KH
7367 if (m_cellHighlightPenWidth != width)
7368 {
d2520c85
RD
7369 m_cellHighlightPenWidth = width;
7370
7371 // Just redrawing the cell highlight is not enough since that won't
4c51a665 7372 // make any visible change if the thickness is getting smaller.
d2520c85
RD
7373 int row = m_currentCellCoords.GetRow();
7374 int col = m_currentCellCoords.GetCol();
1b3b96d8 7375 if ( row == -1 || col == -1 || GetColWidth(col) <= 0 || GetRowHeight(row) <= 0 )
d2520c85 7376 return;
2f024384 7377
d2520c85 7378 wxRect rect = CellToRect(row, col);
ca65c044 7379 m_gridWin->Refresh(true, &rect);
d2520c85
RD
7380 }
7381}
7382
7383void wxGrid::SetCellHighlightROPenWidth(int width)
7384{
4db6714b
KH
7385 if (m_cellHighlightROPenWidth != width)
7386 {
d2520c85
RD
7387 m_cellHighlightROPenWidth = width;
7388
7389 // Just redrawing the cell highlight is not enough since that won't
4c51a665 7390 // make any visible change if the thickness is getting smaller.
d2520c85
RD
7391 int row = m_currentCellCoords.GetRow();
7392 int col = m_currentCellCoords.GetCol();
76c66f19
VZ
7393 if ( row == -1 || col == -1 ||
7394 GetColWidth(col) <= 0 || GetRowHeight(row) <= 0 )
d2520c85 7395 return;
ccdee36f 7396
d2520c85 7397 wxRect rect = CellToRect(row, col);
ca65c044 7398 m_gridWin->Refresh(true, &rect);
d2520c85
RD
7399 }
7400}
7401
9f7aee01
VZ
7402void wxGrid::RedrawGridLines()
7403{
7404 // the lines will be redrawn when the window is thawn
7405 if ( GetBatchCount() )
7406 return;
7407
7408 if ( GridLinesEnabled() )
7409 {
7410 wxClientDC dc( m_gridWin );
7411 PrepareDC( dc );
7412 DrawAllGridLines( dc, wxRegion() );
7413 }
7414 else // remove the grid lines
7415 {
7416 m_gridWin->Refresh();
7417 }
7418}
7419
f85afd4e
MB
7420void wxGrid::EnableGridLines( bool enable )
7421{
7422 if ( enable != m_gridLinesEnabled )
7423 {
7424 m_gridLinesEnabled = enable;
2d66e025 7425
9f7aee01
VZ
7426 RedrawGridLines();
7427 }
7428}
7429
7430void wxGrid::DoClipGridLines(bool& var, bool clip)
7431{
7432 if ( clip != var )
7433 {
7434 var = clip;
7435
7436 if ( GridLinesEnabled() )
7437 RedrawGridLines();
f85afd4e
MB
7438 }
7439}
7440
ef316e23 7441int wxGrid::GetDefaultRowSize() const
f85afd4e
MB
7442{
7443 return m_defaultRowHeight;
7444}
7445
ef316e23 7446int wxGrid::GetRowSize( int row ) const
f85afd4e 7447{
9a83f860 7448 wxCHECK_MSG( row >= 0 && row < m_numRows, 0, wxT("invalid row index") );
b99be8fb 7449
7c1cb261 7450 return GetRowHeight(row);
f85afd4e
MB
7451}
7452
ef316e23 7453int wxGrid::GetDefaultColSize() const
f85afd4e
MB
7454{
7455 return m_defaultColWidth;
7456}
7457
ef316e23 7458int wxGrid::GetColSize( int col ) const
f85afd4e 7459{
9a83f860 7460 wxCHECK_MSG( col >= 0 && col < m_numCols, 0, wxT("invalid column index") );
b99be8fb 7461
7c1cb261 7462 return GetColWidth(col);
f85afd4e
MB
7463}
7464
2e9a6788
VZ
7465// ============================================================================
7466// access to the grid attributes: each of them has a default value in the grid
7467// itself and may be overidden on a per-cell basis
7468// ============================================================================
7469
7470// ----------------------------------------------------------------------------
7471// setting default attributes
7472// ----------------------------------------------------------------------------
7473
7474void wxGrid::SetDefaultCellBackgroundColour( const wxColour& col )
7475{
2796cce3 7476 m_defaultCellAttr->SetBackgroundColour(col);
c916e13b
RR
7477#ifdef __WXGTK__
7478 m_gridWin->SetBackgroundColour(col);
7479#endif
2e9a6788
VZ
7480}
7481
7482void wxGrid::SetDefaultCellTextColour( const wxColour& col )
7483{
2796cce3 7484 m_defaultCellAttr->SetTextColour(col);
2e9a6788
VZ
7485}
7486
7487void wxGrid::SetDefaultCellAlignment( int horiz, int vert )
7488{
2796cce3 7489 m_defaultCellAttr->SetAlignment(horiz, vert);
2e9a6788
VZ
7490}
7491
27f35b66
SN
7492void wxGrid::SetDefaultCellOverflow( bool allow )
7493{
7494 m_defaultCellAttr->SetOverflow(allow);
7495}
7496
2e9a6788
VZ
7497void wxGrid::SetDefaultCellFont( const wxFont& font )
7498{
2796cce3
RD
7499 m_defaultCellAttr->SetFont(font);
7500}
7501
ca63e8e9
RD
7502// For editors and renderers the type registry takes precedence over the
7503// default attr, so we need to register the new editor/renderer for the string
7504// data type in order to make setting a default editor/renderer appear to
7505// work correctly.
7506
0ba143c9
RD
7507void wxGrid::SetDefaultRenderer(wxGridCellRenderer *renderer)
7508{
ca63e8e9
RD
7509 RegisterDataType(wxGRID_VALUE_STRING,
7510 renderer,
7511 GetDefaultEditorForType(wxGRID_VALUE_STRING));
0ba143c9 7512}
2e9a6788 7513
0ba143c9
RD
7514void wxGrid::SetDefaultEditor(wxGridCellEditor *editor)
7515{
ca63e8e9
RD
7516 RegisterDataType(wxGRID_VALUE_STRING,
7517 GetDefaultRendererForType(wxGRID_VALUE_STRING),
42841dfc 7518 editor);
0ba143c9 7519}
9b4aede2 7520
2e9a6788 7521// ----------------------------------------------------------------------------
ef316e23 7522// access to the default attributes
2e9a6788
VZ
7523// ----------------------------------------------------------------------------
7524
ef316e23 7525wxColour wxGrid::GetDefaultCellBackgroundColour() const
f85afd4e 7526{
2796cce3 7527 return m_defaultCellAttr->GetBackgroundColour();
f85afd4e
MB
7528}
7529
ef316e23 7530wxColour wxGrid::GetDefaultCellTextColour() const
2e9a6788 7531{
2796cce3 7532 return m_defaultCellAttr->GetTextColour();
2e9a6788
VZ
7533}
7534
ef316e23 7535wxFont wxGrid::GetDefaultCellFont() const
2e9a6788 7536{
2796cce3 7537 return m_defaultCellAttr->GetFont();
2e9a6788
VZ
7538}
7539
ef316e23 7540void wxGrid::GetDefaultCellAlignment( int *horiz, int *vert ) const
2e9a6788 7541{
2796cce3 7542 m_defaultCellAttr->GetAlignment(horiz, vert);
2e9a6788
VZ
7543}
7544
ef316e23 7545bool wxGrid::GetDefaultCellOverflow() const
27f35b66
SN
7546{
7547 return m_defaultCellAttr->GetOverflow();
7548}
7549
0ba143c9
RD
7550wxGridCellRenderer *wxGrid::GetDefaultRenderer() const
7551{
0b190b0f 7552 return m_defaultCellAttr->GetRenderer(NULL, 0, 0);
0ba143c9 7553}
ab79958a 7554
0ba143c9
RD
7555wxGridCellEditor *wxGrid::GetDefaultEditor() const
7556{
4db6714b 7557 return m_defaultCellAttr->GetEditor(NULL, 0, 0);
0ba143c9 7558}
9b4aede2 7559
2e9a6788
VZ
7560// ----------------------------------------------------------------------------
7561// access to cell attributes
7562// ----------------------------------------------------------------------------
7563
ef316e23 7564wxColour wxGrid::GetCellBackgroundColour(int row, int col) const
f85afd4e 7565{
0a976765 7566 wxGridCellAttr *attr = GetCellAttr(row, col);
2796cce3 7567 wxColour colour = attr->GetBackgroundColour();
39bcce60 7568 attr->DecRef();
2f024384 7569
b99be8fb 7570 return colour;
f85afd4e
MB
7571}
7572
ef316e23 7573wxColour wxGrid::GetCellTextColour( int row, int col ) const
f85afd4e 7574{
0a976765 7575 wxGridCellAttr *attr = GetCellAttr(row, col);
2796cce3 7576 wxColour colour = attr->GetTextColour();
39bcce60 7577 attr->DecRef();
2f024384 7578
b99be8fb 7579 return colour;
f85afd4e
MB
7580}
7581
ef316e23 7582wxFont wxGrid::GetCellFont( int row, int col ) const
f85afd4e 7583{
0a976765 7584 wxGridCellAttr *attr = GetCellAttr(row, col);
2796cce3 7585 wxFont font = attr->GetFont();
39bcce60 7586 attr->DecRef();
2f024384 7587
b99be8fb 7588 return font;
f85afd4e
MB
7589}
7590
ef316e23 7591void wxGrid::GetCellAlignment( int row, int col, int *horiz, int *vert ) const
f85afd4e 7592{
0a976765 7593 wxGridCellAttr *attr = GetCellAttr(row, col);
2796cce3 7594 attr->GetAlignment(horiz, vert);
39bcce60 7595 attr->DecRef();
2e9a6788
VZ
7596}
7597
ef316e23 7598bool wxGrid::GetCellOverflow( int row, int col ) const
27f35b66
SN
7599{
7600 wxGridCellAttr *attr = GetCellAttr(row, col);
7601 bool allow = attr->GetOverflow();
7602 attr->DecRef();
4db6714b 7603
27f35b66
SN
7604 return allow;
7605}
7606
ea99e8e3
VZ
7607wxGrid::CellSpan
7608wxGrid::GetCellSize( int row, int col, int *num_rows, int *num_cols ) const
27f35b66
SN
7609{
7610 wxGridCellAttr *attr = GetCellAttr(row, col);
7611 attr->GetSize( num_rows, num_cols );
7612 attr->DecRef();
ea99e8e3
VZ
7613
7614 if ( *num_rows == 1 && *num_cols == 1 )
7615 return CellSpan_None; // just a normal cell
7616
7617 if ( *num_rows < 0 || *num_cols < 0 )
7618 return CellSpan_Inside; // covered by a multi-span cell
7619
7620 // this cell spans multiple cells to its right/bottom
7621 return CellSpan_Main;
27f35b66
SN
7622}
7623
ef316e23 7624wxGridCellRenderer* wxGrid::GetCellRenderer(int row, int col) const
2796cce3
RD
7625{
7626 wxGridCellAttr* attr = GetCellAttr(row, col);
28a77bc4 7627 wxGridCellRenderer* renderer = attr->GetRenderer(this, row, col);
2796cce3 7628 attr->DecRef();
0b190b0f 7629
2796cce3
RD
7630 return renderer;
7631}
7632
ef316e23 7633wxGridCellEditor* wxGrid::GetCellEditor(int row, int col) const
9b4aede2
RD
7634{
7635 wxGridCellAttr* attr = GetCellAttr(row, col);
28a77bc4 7636 wxGridCellEditor* editor = attr->GetEditor(this, row, col);
9b4aede2 7637 attr->DecRef();
0b190b0f 7638
9b4aede2
RD
7639 return editor;
7640}
7641
283b7808
VZ
7642bool wxGrid::IsReadOnly(int row, int col) const
7643{
7644 wxGridCellAttr* attr = GetCellAttr(row, col);
7645 bool isReadOnly = attr->IsReadOnly();
7646 attr->DecRef();
4db6714b 7647
283b7808
VZ
7648 return isReadOnly;
7649}
7650
2e9a6788 7651// ----------------------------------------------------------------------------
758cbedf 7652// attribute support: cache, automatic provider creation, ...
2e9a6788
VZ
7653// ----------------------------------------------------------------------------
7654
ef316e23 7655bool wxGrid::CanHaveAttributes() const
2e9a6788
VZ
7656{
7657 if ( !m_table )
7658 {
ca65c044 7659 return false;
2e9a6788
VZ
7660 }
7661
f2d76237 7662 return m_table->CanHaveAttributes();
2e9a6788
VZ
7663}
7664
0a976765
VZ
7665void wxGrid::ClearAttrCache()
7666{
7667 if ( m_attrCache.row != -1 )
7668 {
54181a33 7669 wxGridCellAttr *oldAttr = m_attrCache.attr;
19d7140e 7670 m_attrCache.attr = NULL;
0a976765 7671 m_attrCache.row = -1;
1506cc66
SN
7672 // wxSafeDecRec(...) might cause event processing that accesses
7673 // the cached attribute, if one exists (e.g. by deleting the
7674 // editor stored within the attribute). Therefore it is important
ace8d849 7675 // to invalidate the cache before calling wxSafeDecRef!
1506cc66 7676 wxSafeDecRef(oldAttr);
0a976765
VZ
7677 }
7678}
7679
b99450ee
VZ
7680void wxGrid::RefreshAttr(int row, int col)
7681{
7682 if ( m_attrCache.row == row && m_attrCache.col == col )
7683 ClearAttrCache();
7684}
7685
7686
0a976765
VZ
7687void wxGrid::CacheAttr(int row, int col, wxGridCellAttr *attr) const
7688{
2b5f62a0
VZ
7689 if ( attr != NULL )
7690 {
f48a1159 7691 wxGrid * const self = const_cast<wxGrid *>(this);
0a976765 7692
2b5f62a0
VZ
7693 self->ClearAttrCache();
7694 self->m_attrCache.row = row;
7695 self->m_attrCache.col = col;
7696 self->m_attrCache.attr = attr;
7697 wxSafeIncRef(attr);
7698 }
0a976765
VZ
7699}
7700
7701bool wxGrid::LookupAttr(int row, int col, wxGridCellAttr **attr) const
7702{
7703 if ( row == m_attrCache.row && col == m_attrCache.col )
7704 {
7705 *attr = m_attrCache.attr;
39bcce60 7706 wxSafeIncRef(m_attrCache.attr);
0a976765
VZ
7707
7708#ifdef DEBUG_ATTR_CACHE
7709 gs_nAttrCacheHits++;
7710#endif
7711
ca65c044 7712 return true;
0a976765
VZ
7713 }
7714 else
7715 {
7716#ifdef DEBUG_ATTR_CACHE
7717 gs_nAttrCacheMisses++;
7718#endif
4db6714b 7719
ca65c044 7720 return false;
0a976765
VZ
7721 }
7722}
7723
2e9a6788
VZ
7724wxGridCellAttr *wxGrid::GetCellAttr(int row, int col) const
7725{
a373d23b
SN
7726 wxGridCellAttr *attr = NULL;
7727 // Additional test to avoid looking at the cache e.g. for
7728 // wxNoCellCoords, as this will confuse memory management.
7729 if ( row >= 0 )
7730 {
3ed884a0
SN
7731 if ( !LookupAttr(row, col, &attr) )
7732 {
c2f5b920 7733 attr = m_table ? m_table->GetAttr(row, col, wxGridCellAttr::Any)
10a4531d 7734 : NULL;
3ed884a0
SN
7735 CacheAttr(row, col, attr);
7736 }
0a976765 7737 }
4db6714b 7738
508011ce
VZ
7739 if (attr)
7740 {
2796cce3 7741 attr->SetDefAttr(m_defaultCellAttr);
508011ce
VZ
7742 }
7743 else
7744 {
2796cce3
RD
7745 attr = m_defaultCellAttr;
7746 attr->IncRef();
7747 }
2e9a6788 7748
0a976765
VZ
7749 return attr;
7750}
7751
7752wxGridCellAttr *wxGrid::GetOrCreateCellAttr(int row, int col) const
7753{
10a4531d 7754 wxGridCellAttr *attr = NULL;
71e60f70 7755 bool canHave = ((wxGrid*)this)->CanHaveAttributes();
0a976765 7756
9a83f860
VZ
7757 wxCHECK_MSG( canHave, attr, wxT("Cell attributes not allowed"));
7758 wxCHECK_MSG( m_table, attr, wxT("must have a table") );
1df4050d
VZ
7759
7760 attr = m_table->GetAttr(row, col, wxGridCellAttr::Cell);
7761 if ( !attr )
7762 {
7763 attr = new wxGridCellAttr(m_defaultCellAttr);
7764
7765 // artificially inc the ref count to match DecRef() in caller
7766 attr->IncRef();
7767 m_table->SetAttr(attr, row, col);
7768 }
0a976765 7769
2e9a6788
VZ
7770 return attr;
7771}
7772
0b190b0f
VZ
7773// ----------------------------------------------------------------------------
7774// setting column attributes (wrappers around SetColAttr)
7775// ----------------------------------------------------------------------------
7776
7777void wxGrid::SetColFormatBool(int col)
7778{
7779 SetColFormatCustom(col, wxGRID_VALUE_BOOL);
7780}
7781
7782void wxGrid::SetColFormatNumber(int col)
7783{
7784 SetColFormatCustom(col, wxGRID_VALUE_NUMBER);
7785}
7786
7787void wxGrid::SetColFormatFloat(int col, int width, int precision)
7788{
7789 wxString typeName = wxGRID_VALUE_FLOAT;
7790 if ( (width != -1) || (precision != -1) )
7791 {
9a83f860 7792 typeName << wxT(':') << width << wxT(',') << precision;
0b190b0f
VZ
7793 }
7794
7795 SetColFormatCustom(col, typeName);
7796}
7797
7798void wxGrid::SetColFormatCustom(int col, const wxString& typeName)
7799{
999836aa 7800 wxGridCellAttr *attr = m_table->GetAttr(-1, col, wxGridCellAttr::Col );
4db6714b 7801 if (!attr)
19d7140e 7802 attr = new wxGridCellAttr;
0b190b0f
VZ
7803 wxGridCellRenderer *renderer = GetDefaultRendererForType(typeName);
7804 attr->SetRenderer(renderer);
54181a33
VZ
7805 wxGridCellEditor *editor = GetDefaultEditorForType(typeName);
7806 attr->SetEditor(editor);
0b190b0f
VZ
7807
7808 SetColAttr(col, attr);
19d7140e 7809
0b190b0f
VZ
7810}
7811
758cbedf
VZ
7812// ----------------------------------------------------------------------------
7813// setting cell attributes: this is forwarded to the table
7814// ----------------------------------------------------------------------------
7815
27f35b66
SN
7816void wxGrid::SetAttr(int row, int col, wxGridCellAttr *attr)
7817{
7818 if ( CanHaveAttributes() )
7819 {
7820 m_table->SetAttr(attr, row, col);
7821 ClearAttrCache();
7822 }
7823 else
7824 {
7825 wxSafeDecRef(attr);
7826 }
7827}
7828
758cbedf
VZ
7829void wxGrid::SetRowAttr(int row, wxGridCellAttr *attr)
7830{
7831 if ( CanHaveAttributes() )
7832 {
7833 m_table->SetRowAttr(attr, row);
19d7140e 7834 ClearAttrCache();
758cbedf
VZ
7835 }
7836 else
7837 {
39bcce60 7838 wxSafeDecRef(attr);
758cbedf
VZ
7839 }
7840}
7841
7842void wxGrid::SetColAttr(int col, wxGridCellAttr *attr)
7843{
7844 if ( CanHaveAttributes() )
7845 {
7846 m_table->SetColAttr(attr, col);
19d7140e 7847 ClearAttrCache();
758cbedf
VZ
7848 }
7849 else
7850 {
39bcce60 7851 wxSafeDecRef(attr);
758cbedf
VZ
7852 }
7853}
7854
2e9a6788
VZ
7855void wxGrid::SetCellBackgroundColour( int row, int col, const wxColour& colour )
7856{
7857 if ( CanHaveAttributes() )
7858 {
0a976765 7859 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
2e9a6788
VZ
7860 attr->SetBackgroundColour(colour);
7861 attr->DecRef();
7862 }
7863}
7864
7865void wxGrid::SetCellTextColour( int row, int col, const wxColour& colour )
7866{
7867 if ( CanHaveAttributes() )
7868 {
0a976765 7869 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
2e9a6788
VZ
7870 attr->SetTextColour(colour);
7871 attr->DecRef();
7872 }
7873}
7874
7875void wxGrid::SetCellFont( int row, int col, const wxFont& font )
7876{
7877 if ( CanHaveAttributes() )
7878 {
0a976765 7879 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
2e9a6788
VZ
7880 attr->SetFont(font);
7881 attr->DecRef();
7882 }
7883}
7884
7885void wxGrid::SetCellAlignment( int row, int col, int horiz, int vert )
7886{
7887 if ( CanHaveAttributes() )
7888 {
0a976765 7889 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
2e9a6788
VZ
7890 attr->SetAlignment(horiz, vert);
7891 attr->DecRef();
ab79958a
VZ
7892 }
7893}
7894
27f35b66
SN
7895void wxGrid::SetCellOverflow( int row, int col, bool allow )
7896{
7897 if ( CanHaveAttributes() )
7898 {
7899 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
7900 attr->SetOverflow(allow);
7901 attr->DecRef();
7902 }
7903}
7904
7905void wxGrid::SetCellSize( int row, int col, int num_rows, int num_cols )
7906{
7907 if ( CanHaveAttributes() )
7908 {
7909 int cell_rows, cell_cols;
7910
7911 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
7912 attr->GetSize(&cell_rows, &cell_cols);
7913 attr->SetSize(num_rows, num_cols);
7914 attr->DecRef();
7915
7916 // Cannot set the size of a cell to 0 or negative values
7917 // While it is perfectly legal to do that, this function cannot
7918 // handle all the possibilies, do it by hand by getting the CellAttr.
7919 // You can only set the size of a cell to 1,1 or greater with this fn
7920 wxASSERT_MSG( !((cell_rows < 1) || (cell_cols < 1)),
7921 wxT("wxGrid::SetCellSize setting cell size that is already part of another cell"));
7922 wxASSERT_MSG( !((num_rows < 1) || (num_cols < 1)),
7923 wxT("wxGrid::SetCellSize setting cell size to < 1"));
7924
7925 // if this was already a multicell then "turn off" the other cells first
a6b2078d 7926 if ((cell_rows > 1) || (cell_cols > 1))
27f35b66
SN
7927 {
7928 int i, j;
2f024384 7929 for (j=row; j < row + cell_rows; j++)
27f35b66 7930 {
2f024384 7931 for (i=col; i < col + cell_cols; i++)
27f35b66
SN
7932 {
7933 if ((i != col) || (j != row))
7934 {
7935 wxGridCellAttr *attr_stub = GetOrCreateCellAttr(j, i);
7936 attr_stub->SetSize( 1, 1 );
7937 attr_stub->DecRef();
7938 }
7939 }
7940 }
7941 }
7942
7943 // mark the cells that will be covered by this cell to
7944 // negative or zero values to point back at this cell
7945 if (((num_rows > 1) || (num_cols > 1)) && (num_rows >= 1) && (num_cols >= 1))
7946 {
7947 int i, j;
2f024384 7948 for (j=row; j < row + num_rows; j++)
27f35b66 7949 {
2f024384 7950 for (i=col; i < col + num_cols; i++)
27f35b66
SN
7951 {
7952 if ((i != col) || (j != row))
7953 {
7954 wxGridCellAttr *attr_stub = GetOrCreateCellAttr(j, i);
2f024384 7955 attr_stub->SetSize( row - j, col - i );
27f35b66
SN
7956 attr_stub->DecRef();
7957 }
7958 }
7959 }
7960 }
7961 }
7962}
7963
ab79958a
VZ
7964void wxGrid::SetCellRenderer(int row, int col, wxGridCellRenderer *renderer)
7965{
7966 if ( CanHaveAttributes() )
7967 {
0a976765 7968 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
ab79958a
VZ
7969 attr->SetRenderer(renderer);
7970 attr->DecRef();
9b4aede2
RD
7971 }
7972}
7973
7974void wxGrid::SetCellEditor(int row, int col, wxGridCellEditor* editor)
7975{
7976 if ( CanHaveAttributes() )
7977 {
7978 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
7979 attr->SetEditor(editor);
7980 attr->DecRef();
283b7808
VZ
7981 }
7982}
7983
7984void wxGrid::SetReadOnly(int row, int col, bool isReadOnly)
7985{
7986 if ( CanHaveAttributes() )
7987 {
7988 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
7989 attr->SetReadOnly(isReadOnly);
7990 attr->DecRef();
2e9a6788 7991 }
f85afd4e
MB
7992}
7993
f2d76237
RD
7994// ----------------------------------------------------------------------------
7995// Data type registration
7996// ----------------------------------------------------------------------------
7997
7998void wxGrid::RegisterDataType(const wxString& typeName,
7999 wxGridCellRenderer* renderer,
8000 wxGridCellEditor* editor)
8001{
8002 m_typeRegistry->RegisterDataType(typeName, renderer, editor);
8003}
8004
8005
a9339fe2 8006wxGridCellEditor * wxGrid::GetDefaultEditorForCell(int row, int col) const
f2d76237
RD
8007{
8008 wxString typeName = m_table->GetTypeName(row, col);
8009 return GetDefaultEditorForType(typeName);
8010}
8011
a9339fe2 8012wxGridCellRenderer * wxGrid::GetDefaultRendererForCell(int row, int col) const
f2d76237
RD
8013{
8014 wxString typeName = m_table->GetTypeName(row, col);
8015 return GetDefaultRendererForType(typeName);
8016}
8017
a9339fe2 8018wxGridCellEditor * wxGrid::GetDefaultEditorForType(const wxString& typeName) const
f2d76237 8019{
c4608a8a 8020 int index = m_typeRegistry->FindOrCloneDataType(typeName);
0b190b0f
VZ
8021 if ( index == wxNOT_FOUND )
8022 {
767e0835 8023 wxFAIL_MSG(wxString::Format(wxT("Unknown data type name [%s]"), typeName.c_str()));
0b190b0f 8024
f2d76237
RD
8025 return NULL;
8026 }
0b190b0f 8027
f2d76237
RD
8028 return m_typeRegistry->GetEditor(index);
8029}
8030
a9339fe2 8031wxGridCellRenderer * wxGrid::GetDefaultRendererForType(const wxString& typeName) const
f2d76237 8032{
c4608a8a 8033 int index = m_typeRegistry->FindOrCloneDataType(typeName);
0b190b0f
VZ
8034 if ( index == wxNOT_FOUND )
8035 {
767e0835 8036 wxFAIL_MSG(wxString::Format(wxT("Unknown data type name [%s]"), typeName.c_str()));
0b190b0f 8037
c4608a8a 8038 return NULL;
e72b4213 8039 }
0b190b0f 8040
c4608a8a 8041 return m_typeRegistry->GetRenderer(index);
f2d76237
RD
8042}
8043
2e9a6788
VZ
8044// ----------------------------------------------------------------------------
8045// row/col size
8046// ----------------------------------------------------------------------------
8047
82edfbe7
VZ
8048void wxGrid::DoDisableLineResize(int line, wxGridFixedIndicesSet *& setFixed)
8049{
8050 if ( !setFixed )
8051 {
8052 setFixed = new wxGridFixedIndicesSet;
8053 }
8054
8055 setFixed->insert(line);
8056}
8057
8058bool
8059wxGrid::DoCanResizeLine(int line, const wxGridFixedIndicesSet *setFixed) const
8060{
8061 return !setFixed || !setFixed->count(line);
8062}
8063
6e8524b1
MB
8064void wxGrid::EnableDragRowSize( bool enable )
8065{
8066 m_canDragRowSize = enable;
8067}
8068
6e8524b1
MB
8069void wxGrid::EnableDragColSize( bool enable )
8070{
8071 m_canDragColSize = enable;
8072}
8073
4cfa5de6
RD
8074void wxGrid::EnableDragGridSize( bool enable )
8075{
8076 m_canDragGridSize = enable;
8077}
8078
79dbea21
RD
8079void wxGrid::EnableDragCell( bool enable )
8080{
8081 m_canDragCell = enable;
8082}
6e8524b1 8083
f85afd4e
MB
8084void wxGrid::SetDefaultRowSize( int height, bool resizeExistingRows )
8085{
b8d24d4e 8086 m_defaultRowHeight = wxMax( height, m_minAcceptableRowHeight );
f85afd4e
MB
8087
8088 if ( resizeExistingRows )
8089 {
b1da8107
SN
8090 // since we are resizing all rows to the default row size,
8091 // we can simply clear the row heights and row bottoms
8092 // arrays (which also allows us to take advantage of
8093 // some speed optimisations)
8094 m_rowHeights.Empty();
8095 m_rowBottoms.Empty();
edb89f7e
VZ
8096 if ( !GetBatchCount() )
8097 CalcDimensions();
f85afd4e
MB
8098 }
8099}
8100
30496905
VZ
8101namespace
8102{
8103
8104// This is a common part of SetRowSize() and SetColSize() which takes care of
8105// updating the height/width of a row/column depending on its current value and
8106// the new one.
8107//
8108// Returns the difference between the new and the old size.
8109int UpdateRowOrColSize(int& sizeCurrent, int sizeNew)
8110{
8111 // On input here sizeCurrent can be negative if it's currently hidden (the
8112 // real size is its absolute value then). And sizeNew can be 0 to indicate
8113 // that the row/column should be hidden or -1 to indicate that it should be
8114 // shown again.
8115
8116 if ( sizeNew < 0 )
8117 {
8118 // We're showing back a previously hidden row/column.
8119 wxASSERT_MSG( sizeNew == -1, wxS("New size must be positive or -1.") );
8120
2328f468
VZ
8121 // If it's already visible, simply do nothing.
8122 if ( sizeCurrent >= 0 )
8123 return 0;
30496905 8124
2328f468 8125 // Otherwise show it by restoring its old size.
30496905
VZ
8126 sizeCurrent = -sizeCurrent;
8127
8128 // This is positive which is correct.
8129 return sizeCurrent;
8130 }
8131 else if ( sizeNew == 0 )
8132 {
8133 // We're hiding a row/column.
30496905 8134
2328f468
VZ
8135 // If it's already hidden, simply do nothing.
8136 if ( sizeCurrent <= 0 )
8137 return 0;
8138
8139 // Otherwise hide it and also remember the shown size to be able to
8140 // restore it later.
30496905
VZ
8141 sizeCurrent = -sizeCurrent;
8142
8143 // This is negative which is correct.
8144 return sizeCurrent;
8145 }
8146 else // We're just changing the row/column size.
8147 {
8148 // Here it could have been hidden or not previously.
8149 const int sizeOld = sizeCurrent < 0 ? 0 : sizeCurrent;
8150
8151 sizeCurrent = sizeNew;
8152
8153 return sizeCurrent - sizeOld;
8154 }
8155}
8156
8157} // anonymous namespace
8158
f85afd4e
MB
8159void wxGrid::SetRowSize( int row, int height )
8160{
30496905
VZ
8161 // See comment in SetColSize
8162 if ( height > 0 && height < GetRowMinimalAcceptableHeight())
8163 return;
60ff3b99 8164
30496905
VZ
8165 // The value of -1 is special and means to fit the height to the row label.
8166 if ( height == -1 )
ad7502d8 8167 {
6922252d
VZ
8168 // As with the columns, ignore attempts to auto-size the hidden rows.
8169 if ( GetRowHeight(row) == 0 )
8170 return;
8171
ad7502d8
SN
8172 long w, h;
8173 wxArrayString lines;
8174 wxClientDC dc(m_rowLabelWin);
8175 dc.SetFont(GetLabelFont());
8176 StringToLines(GetRowLabelValue( row ), lines);
ace8d849
VZ
8177 GetTextBoxSize( dc, lines, &w, &h );
8178 //check that it is not less than the minimal height
8179 height = wxMax(h, GetRowMinimalAcceptableHeight());
ad7502d8
SN
8180 }
8181
30496905
VZ
8182 DoSetRowSize(row, height);
8183}
8184
8185void wxGrid::DoSetRowSize( int row, int height )
8186{
8187 wxCHECK_RET( row >= 0 && row < m_numRows, wxT("invalid row index") );
b8d24d4e 8188
7c1cb261
VZ
8189 if ( m_rowHeights.IsEmpty() )
8190 {
8191 // need to really create the array
8192 InitRowHeights();
8193 }
60ff3b99 8194
30496905
VZ
8195 const int diff = UpdateRowOrColSize(m_rowHeights[row], height);
8196 if ( !diff )
8197 return;
60ff3b99 8198
0ed3b812 8199 for ( int i = row; i < m_numRows; i++ )
f85afd4e 8200 {
b99be8fb 8201 m_rowBottoms[i] += diff;
f85afd4e 8202 }
2f024384 8203
a6b40a9a
VZ
8204 InvalidateBestSize();
8205
faec5a43 8206 if ( !GetBatchCount() )
ce84ef6a 8207 {
faec5a43 8208 CalcDimensions();
ce84ef6a
VZ
8209 Refresh();
8210 }
f85afd4e
MB
8211}
8212
8213void wxGrid::SetDefaultColSize( int width, bool resizeExistingCols )
8214{
ef316e23
VZ
8215 // we dont allow zero default column width
8216 m_defaultColWidth = wxMax( wxMax( width, m_minAcceptableColWidth ), 1 );
f85afd4e
MB
8217
8218 if ( resizeExistingCols )
8219 {
b1da8107
SN
8220 // since we are resizing all columns to the default column size,
8221 // we can simply clear the col widths and col rights
8222 // arrays (which also allows us to take advantage of
8223 // some speed optimisations)
8224 m_colWidths.Empty();
8225 m_colRights.Empty();
edb89f7e
VZ
8226 if ( !GetBatchCount() )
8227 CalcDimensions();
f85afd4e
MB
8228 }
8229}
8230
8231void wxGrid::SetColSize( int col, int width )
8232{
30496905
VZ
8233 // we intentionally don't test whether the width is less than
8234 // GetColMinimalWidth() here but we do compare it with
8235 // GetColMinimalAcceptableWidth() as otherwise things currently break (see
8236 // #651) -- and we also always allow the width of 0 as it has the special
8237 // sense of hiding the column
8238 if ( width > 0 && width < GetColMinimalAcceptableWidth() )
8239 return;
60ff3b99 8240
30496905
VZ
8241 // The value of -1 is special and means to fit the width to the column label.
8242 if ( width == -1 )
ad7502d8 8243 {
6922252d
VZ
8244 // We currently don't support auto-sizing hidden columns. We could, but
8245 // it's not clear whether this is really needed and it would make the
8246 // code more complex.
8247 if ( GetColWidth(col) == 0 )
8248 return;
8249
ad7502d8
SN
8250 long w, h;
8251 wxArrayString lines;
ad805b9e 8252 wxClientDC dc(m_colWindow);
ad7502d8
SN
8253 dc.SetFont(GetLabelFont());
8254 StringToLines(GetColLabelValue(col), lines);
8255 if ( GetColLabelTextOrientation() == wxHORIZONTAL )
8256 GetTextBoxSize( dc, lines, &w, &h );
8257 else
8258 GetTextBoxSize( dc, lines, &h, &w );
8259 width = w + 6;
ace8d849 8260 //check that it is not less than the minimal width
54181a33 8261 width = wxMax(width, GetColMinimalAcceptableWidth());
ad7502d8
SN
8262 }
8263
30496905
VZ
8264 DoSetColSize(col, width);
8265}
8266
8267void wxGrid::DoSetColSize( int col, int width )
8268{
8269 wxCHECK_RET( col >= 0 && col < m_numCols, wxT("invalid column index") );
3e13956a 8270
7c1cb261
VZ
8271 if ( m_colWidths.IsEmpty() )
8272 {
8273 // need to really create the array
8274 InitColWidths();
8275 }
f85afd4e 8276
30496905
VZ
8277 const int diff = UpdateRowOrColSize(m_colWidths[col], width);
8278 if ( !diff )
8279 return;
8280
009c7216 8281 if ( m_useNativeHeader )
3039ade9 8282 GetGridColHeader()->UpdateColumn(col);
009c7216 8283 //else: will be refreshed when the header is redrawn
60ff3b99 8284
0ed3b812 8285 for ( int colPos = GetColPos(col); colPos < m_numCols; colPos++ )
f85afd4e 8286 {
0ed3b812 8287 m_colRights[GetColAt(colPos)] += diff;
f85afd4e 8288 }
962a48f6 8289
a6b40a9a
VZ
8290 InvalidateBestSize();
8291
faec5a43 8292 if ( !GetBatchCount() )
ad805b9e 8293 {
faec5a43 8294 CalcDimensions();
ad805b9e
VZ
8295 Refresh();
8296 }
f85afd4e
MB
8297}
8298
43947979
VZ
8299void wxGrid::SetColMinimalWidth( int col, int width )
8300{
4db6714b
KH
8301 if (width > GetColMinimalAcceptableWidth())
8302 {
c6fbe2f0 8303 wxLongToLongHashMap::key_type key = (wxLongToLongHashMap::key_type)col;
8253f2e0 8304 m_colMinWidths[key] = width;
b8d24d4e 8305 }
af547d51
VZ
8306}
8307
8308void wxGrid::SetRowMinimalHeight( int row, int width )
8309{
4db6714b
KH
8310 if (width > GetRowMinimalAcceptableHeight())
8311 {
c6fbe2f0 8312 wxLongToLongHashMap::key_type key = (wxLongToLongHashMap::key_type)row;
8253f2e0 8313 m_rowMinHeights[key] = width;
b8d24d4e 8314 }
43947979
VZ
8315}
8316
8317int wxGrid::GetColMinimalWidth(int col) const
8318{
c6fbe2f0 8319 wxLongToLongHashMap::key_type key = (wxLongToLongHashMap::key_type)col;
8253f2e0 8320 wxLongToLongHashMap::const_iterator it = m_colMinWidths.find(key);
962a48f6 8321
ba8c1601 8322 return it != m_colMinWidths.end() ? (int)it->second : m_minAcceptableColWidth;
af547d51
VZ
8323}
8324
8325int wxGrid::GetRowMinimalHeight(int row) const
8326{
c6fbe2f0 8327 wxLongToLongHashMap::key_type key = (wxLongToLongHashMap::key_type)row;
8253f2e0 8328 wxLongToLongHashMap::const_iterator it = m_rowMinHeights.find(key);
962a48f6 8329
ba8c1601 8330 return it != m_rowMinHeights.end() ? (int)it->second : m_minAcceptableRowHeight;
b8d24d4e
RG
8331}
8332
8333void wxGrid::SetColMinimalAcceptableWidth( int width )
8334{
20c84410 8335 // We do allow a width of 0 since this gives us
962a48f6
DS
8336 // an easy way to temporarily hiding columns.
8337 if ( width >= 0 )
8338 m_minAcceptableColWidth = width;
b8d24d4e
RG
8339}
8340
8341void wxGrid::SetRowMinimalAcceptableHeight( int height )
8342{
20c84410 8343 // We do allow a height of 0 since this gives us
962a48f6
DS
8344 // an easy way to temporarily hiding rows.
8345 if ( height >= 0 )
8346 m_minAcceptableRowHeight = height;
17a1ebd1 8347}
b8d24d4e
RG
8348
8349int wxGrid::GetColMinimalAcceptableWidth() const
8350{
8351 return m_minAcceptableColWidth;
8352}
8353
8354int wxGrid::GetRowMinimalAcceptableHeight() const
8355{
8356 return m_minAcceptableRowHeight;
43947979
VZ
8357}
8358
57c086ef
VZ
8359// ----------------------------------------------------------------------------
8360// auto sizing
8361// ----------------------------------------------------------------------------
8362
733f486a
VZ
8363void
8364wxGrid::AutoSizeColOrRow(int colOrRow, bool setAsMin, wxGridDirection direction)
65e4e78e 8365{
733f486a
VZ
8366 const bool column = direction == wxGRID_COLUMN;
8367
6922252d
VZ
8368 // We don't support auto-sizing hidden rows or columns, this doesn't seem
8369 // to make much sense.
8370 if ( column )
8371 {
8372 if ( GetColWidth(colOrRow) == 0 )
8373 return;
8374 }
8375 else
8376 {
8377 if ( GetRowHeight(colOrRow) == 0 )
8378 return;
8379 }
8380
65e4e78e
VZ
8381 wxClientDC dc(m_gridWin);
8382
962a48f6 8383 // cancel editing of cell
13f6e9e8
RG
8384 HideCellEditControl();
8385 SaveEditControlValue();
8386
68abd97d
VZ
8387 // initialize both of them just to avoid compiler warnings even if only
8388 // really needs to be initialized here
8389 int row,
8390 col;
8391 if ( column )
8392 {
8393 row = -1;
8394 col = colOrRow;
8395 }
8396 else
8397 {
8398 row = colOrRow;
a95e38c0 8399 col = -1;
68abd97d 8400 }
af547d51
VZ
8401
8402 wxCoord extent, extentMax = 0;
8403 int max = column ? m_numRows : m_numCols;
39bcce60 8404 for ( int rowOrCol = 0; rowOrCol < max; rowOrCol++ )
65e4e78e 8405 {
af547d51 8406 if ( column )
bc7d2e9d 8407 {
af547d51 8408 row = rowOrCol;
bc7d2e9d
VZ
8409 col = colOrRow;
8410 }
6922252d 8411 else
bc7d2e9d
VZ
8412 {
8413 row = colOrRow;
af547d51 8414 col = rowOrCol;
bc7d2e9d 8415 }
774f6b31
VZ
8416
8417 // we need to account for the cells spanning multiple columns/rows:
8418 // while they may need a lot of space, they don't need all of it in
8419 // this column/row
8420 int numRows, numCols;
8421 const CellSpan span = GetCellSize(row, col, &numRows, &numCols);
8422 if ( span == CellSpan_Inside )
8423 {
8424 // we need to get the size of the main cell, not of a cell hidden
8425 // by it
8426 row += numRows;
8427 col += numCols;
8428
8429 // get the size of the main cell too
8430 GetCellSize(row, col, &numRows, &numCols);
8431 }
af547d51 8432
bc7d2e9d 8433 // get cell ( main cell if CellSpan_Inside ) renderer best size
2f024384
DS
8434 wxGridCellAttr *attr = GetCellAttr(row, col);
8435 wxGridCellRenderer *renderer = attr->GetRenderer(this, row, col);
65e4e78e
VZ
8436 if ( renderer )
8437 {
af547d51
VZ
8438 wxSize size = renderer->GetBestSize(*this, *attr, dc, row, col);
8439 extent = column ? size.x : size.y;
774f6b31
VZ
8440
8441 if ( span != CellSpan_None )
8442 {
8443 // we spread the size of a spanning cell over all the cells it
8444 // covers evenly -- this is probably not ideal but we can't
8445 // really do much better here
8446 //
8447 // notice that numCols and numRows are never 0 as they
8448 // correspond to the size of the main cell of the span and not
8449 // of the cell inside it
8450 extent /= column ? numCols : numRows;
8451 }
8452
af547d51 8453 if ( extent > extentMax )
af547d51 8454 extentMax = extent;
0b190b0f
VZ
8455
8456 renderer->DecRef();
65e4e78e
VZ
8457 }
8458
8459 attr->DecRef();
8460 }
8461
af547d51
VZ
8462 // now also compare with the column label extent
8463 wxCoord w, h;
65e4e78e 8464 dc.SetFont( GetLabelFont() );
294d195c
MB
8465
8466 if ( column )
d43851f7 8467 {
bc7d2e9d 8468 dc.GetMultiLineTextExtent( GetColLabelValue(colOrRow), &w, &h );
4db6714b 8469 if ( GetColLabelTextOrientation() == wxVERTICAL )
d43851f7
JS
8470 w = h;
8471 }
294d195c 8472 else
bc7d2e9d 8473 dc.GetMultiLineTextExtent( GetRowLabelValue(colOrRow), &w, &h );
294d195c 8474
af547d51
VZ
8475 extent = column ? w : h;
8476 if ( extent > extentMax )
af547d51 8477 extentMax = extent;
65e4e78e 8478
af547d51 8479 if ( !extentMax )
65e4e78e 8480 {
af547d51 8481 // empty column - give default extent (notice that if extentMax is less
2f024384 8482 // than default extent but != 0, it's OK)
af547d51 8483 extentMax = column ? m_defaultColWidth : m_defaultRowHeight;
65e4e78e
VZ
8484 }
8485 else
8486 {
a95e38c0 8487 if ( column )
a95e38c0
VZ
8488 // leave some space around text
8489 extentMax += 10;
f6bcfd97 8490 else
f6bcfd97 8491 extentMax += 6;
65e4e78e
VZ
8492 }
8493
edb89f7e
VZ
8494 if ( column )
8495 {
a01cfc08
VS
8496 // Ensure automatic width is not less than minimal width. See the
8497 // comment in SetColSize() for explanation of why this isn't done
8498 // in SetColSize().
8499 if ( !setAsMin )
bc7d2e9d 8500 extentMax = wxMax(extentMax, GetColMinimalWidth(colOrRow));
a01cfc08 8501
bc7d2e9d 8502 SetColSize( colOrRow, extentMax );
faec5a43
SN
8503 if ( !GetBatchCount() )
8504 {
ad805b9e
VZ
8505 if ( m_useNativeHeader )
8506 {
bc7d2e9d 8507 GetGridColHeader()->UpdateColumn(colOrRow);
ad805b9e
VZ
8508 }
8509 else
8510 {
8511 int cw, ch, dummy;
8512 m_gridWin->GetClientSize( &cw, &ch );
bc7d2e9d 8513 wxRect rect ( CellToRect( 0, colOrRow ) );
ad805b9e
VZ
8514 rect.y = 0;
8515 CalcScrolledPosition(rect.x, 0, &rect.x, &dummy);
8516 rect.width = cw - rect.x;
8517 rect.height = m_colLabelHeight;
8518 GetColLabelWindow()->Refresh( true, &rect );
8519 }
edb89f7e
VZ
8520 }
8521 }
8522 else
8523 {
a01cfc08
VS
8524 // Ensure automatic width is not less than minimal height. See the
8525 // comment in SetColSize() for explanation of why this isn't done
8526 // in SetRowSize().
8527 if ( !setAsMin )
bc7d2e9d 8528 extentMax = wxMax(extentMax, GetRowMinimalHeight(colOrRow));
a01cfc08 8529
bc7d2e9d 8530 SetRowSize(colOrRow, extentMax);
faec5a43
SN
8531 if ( !GetBatchCount() )
8532 {
edb89f7e
VZ
8533 int cw, ch, dummy;
8534 m_gridWin->GetClientSize( &cw, &ch );
bc7d2e9d 8535 wxRect rect( CellToRect( colOrRow, 0 ) );
edb89f7e
VZ
8536 rect.x = 0;
8537 CalcScrolledPosition(0, rect.y, &dummy, &rect.y);
8538 rect.width = m_rowLabelWidth;
faec5a43 8539 rect.height = ch - rect.y;
ca65c044 8540 m_rowLabelWin->Refresh( true, &rect );
edb89f7e 8541 }
faec5a43 8542 }
2f024384 8543
65e4e78e
VZ
8544 if ( setAsMin )
8545 {
af547d51 8546 if ( column )
bc7d2e9d 8547 SetColMinimalWidth(colOrRow, extentMax);
af547d51 8548 else
bc7d2e9d 8549 SetRowMinimalHeight(colOrRow, extentMax);
65e4e78e
VZ
8550 }
8551}
8552
733f486a
VZ
8553wxCoord wxGrid::CalcColOrRowLabelAreaMinSize(wxGridDirection direction)
8554{
8555 // calculate size for the rows or columns?
8556 const bool calcRows = direction == wxGRID_ROW;
8557
8558 wxClientDC dc(calcRows ? GetGridRowLabelWindow()
8559 : GetGridColLabelWindow());
8560 dc.SetFont(GetLabelFont());
8561
8562 // which dimension should we take into account for calculations?
8563 //
8564 // for columns, the text can be only horizontal so it's easy but for rows
8565 // we also have to take into account the text orientation
8566 const bool
8567 useWidth = calcRows || (GetColLabelTextOrientation() == wxVERTICAL);
8568
8569 wxArrayString lines;
8570 wxCoord extentMax = 0;
8571
8572 const int numRowsOrCols = calcRows ? m_numRows : m_numCols;
8573 for ( int rowOrCol = 0; rowOrCol < numRowsOrCols; rowOrCol++ )
8574 {
8575 lines.Clear();
add4bb40
VS
8576
8577 wxString label = calcRows ? GetRowLabelValue(rowOrCol)
8578 : GetColLabelValue(rowOrCol);
8579 StringToLines(label, lines);
733f486a
VZ
8580
8581 long w, h;
8582 GetTextBoxSize(dc, lines, &w, &h);
8583
8584 const wxCoord extent = useWidth ? w : h;
8585 if ( extent > extentMax )
8586 extentMax = extent;
8587 }
8588
8589 if ( !extentMax )
8590 {
8591 // empty column - give default extent (notice that if extentMax is less
8592 // than default extent but != 0, it's OK)
8593 extentMax = calcRows ? GetDefaultRowLabelSize()
8594 : GetDefaultColLabelSize();
8595 }
8596
8597 // leave some space around text (taken from AutoSizeColOrRow)
8598 if ( calcRows )
8599 extentMax += 10;
8600 else
8601 extentMax += 6;
8602
8603 return extentMax;
8604}
8605
266e8367 8606int wxGrid::SetOrCalcColumnSizes(bool calcOnly, bool setAsMin)
65e4e78e 8607{
57c086ef
VZ
8608 int width = m_rowLabelWidth;
8609
b62f94ff
VZ
8610 wxGridUpdateLocker locker;
8611 if(!calcOnly)
8612 locker.Create(this);
97a9929e 8613
65e4e78e
VZ
8614 for ( int col = 0; col < m_numCols; col++ )
8615 {
266e8367 8616 if ( !calcOnly )
266e8367 8617 AutoSizeColumn(col, setAsMin);
57c086ef
VZ
8618
8619 width += GetColWidth(col);
65e4e78e 8620 }
97a9929e 8621
266e8367 8622 return width;
65e4e78e
VZ
8623}
8624
266e8367 8625int wxGrid::SetOrCalcRowSizes(bool calcOnly, bool setAsMin)
57c086ef
VZ
8626{
8627 int height = m_colLabelHeight;
8628
b62f94ff
VZ
8629 wxGridUpdateLocker locker;
8630 if(!calcOnly)
8631 locker.Create(this);
97a9929e 8632
57c086ef
VZ
8633 for ( int row = 0; row < m_numRows; row++ )
8634 {
af547d51 8635 if ( !calcOnly )
af547d51 8636 AutoSizeRow(row, setAsMin);
57c086ef
VZ
8637
8638 height += GetRowHeight(row);
8639 }
97a9929e 8640
266e8367 8641 return height;
57c086ef
VZ
8642}
8643
8644void wxGrid::AutoSize()
8645{
b62f94ff 8646 wxGridUpdateLocker locker(this);
97a9929e 8647
0ed3b812
VZ
8648 wxSize size(SetOrCalcColumnSizes(false) - m_rowLabelWidth + m_extraWidth,
8649 SetOrCalcRowSizes(false) - m_colLabelHeight + m_extraHeight);
97a9929e 8650
0ed3b812
VZ
8651 // we know that we're not going to have scrollbars so disable them now to
8652 // avoid trouble in SetClientSize() which can otherwise set the correct
8653 // client size but also leave space for (not needed any more) scrollbars
fd76c6a7
VZ
8654 SetScrollbars(m_xScrollPixelsPerLine, m_yScrollPixelsPerLine,
8655 0, 0, 0, 0, true);
72b0a1de
VZ
8656
8657 SetClientSize(size.x + m_rowLabelWidth, size.y + m_colLabelHeight);
266e8367
VZ
8658}
8659
d43851f7
JS
8660void wxGrid::AutoSizeRowLabelSize( int row )
8661{
d43851f7 8662 // Hide the edit control, so it
4db6714b
KH
8663 // won't interfere with drag-shrinking.
8664 if ( IsCellEditControlShown() )
d43851f7
JS
8665 {
8666 HideCellEditControl();
8667 SaveEditControlValue();
8668 }
8669
8670 // autosize row height depending on label text
ad7502d8 8671 SetRowSize(row, -1);
30496905 8672
d43851f7
JS
8673 ForceRefresh();
8674}
8675
8676void wxGrid::AutoSizeColLabelSize( int col )
8677{
d43851f7 8678 // Hide the edit control, so it
c2f5b920 8679 // won't interfere with drag-shrinking.
4db6714b 8680 if ( IsCellEditControlShown() )
d43851f7
JS
8681 {
8682 HideCellEditControl();
8683 SaveEditControlValue();
8684 }
8685
8686 // autosize column width depending on label text
ad7502d8 8687 SetColSize(col, -1);
30496905 8688
d43851f7
JS
8689 ForceRefresh();
8690}
8691
266e8367
VZ
8692wxSize wxGrid::DoGetBestSize() const
8693{
f48a1159 8694 wxGrid * const self = const_cast<wxGrid *>(this);
266e8367 8695
dc4689ef
VZ
8696 // we do the same as in AutoSize() here with the exception that we don't
8697 // change the column/row sizes, only calculate them
8698 wxSize size(self->SetOrCalcColumnSizes(true) - m_rowLabelWidth + m_extraWidth,
8699 self->SetOrCalcRowSizes(true) - m_colLabelHeight + m_extraHeight);
962a48f6 8700
72b0a1de 8701 return wxSize(size.x + m_rowLabelWidth, size.y + m_colLabelHeight)
dc4689ef 8702 + GetWindowBorderSize();
266e8367
VZ
8703}
8704
8705void wxGrid::Fit()
8706{
8707 AutoSize();
57c086ef
VZ
8708}
8709
7bc57fd9 8710#if WXWIN_COMPATIBILITY_2_8
6fc0f38f
SN
8711wxPen& wxGrid::GetDividerPen() const
8712{
8713 return wxNullPen;
8714}
7bc57fd9 8715#endif // WXWIN_COMPATIBILITY_2_8
6fc0f38f 8716
57c086ef
VZ
8717// ----------------------------------------------------------------------------
8718// cell value accessor functions
8719// ----------------------------------------------------------------------------
f85afd4e
MB
8720
8721void wxGrid::SetCellValue( int row, int col, const wxString& s )
8722{
8723 if ( m_table )
8724 {
f6bcfd97 8725 m_table->SetValue( row, col, s );
2d66e025
MB
8726 if ( !GetBatchCount() )
8727 {
27f35b66
SN
8728 int dummy;
8729 wxRect rect( CellToRect( row, col ) );
8730 rect.x = 0;
8731 rect.width = m_gridWin->GetClientSize().GetWidth();
8732 CalcScrolledPosition(0, rect.y, &dummy, &rect.y);
ca65c044 8733 m_gridWin->Refresh( false, &rect );
2d66e025 8734 }
60ff3b99 8735
f85afd4e 8736 if ( m_currentCellCoords.GetRow() == row &&
4cfa5de6 8737 m_currentCellCoords.GetCol() == col &&
f6bcfd97
BP
8738 IsCellEditControlShown())
8739 // Note: If we are using IsCellEditControlEnabled,
8740 // this interacts badly with calling SetCellValue from
8741 // an EVT_GRID_CELL_CHANGE handler.
f85afd4e 8742 {
4cfa5de6
RD
8743 HideCellEditControl();
8744 ShowCellEditControl(); // will reread data from table
f85afd4e 8745 }
f85afd4e
MB
8746 }
8747}
8748
962a48f6 8749// ----------------------------------------------------------------------------
2f024384 8750// block, row and column selection
962a48f6 8751// ----------------------------------------------------------------------------
f85afd4e
MB
8752
8753void wxGrid::SelectRow( int row, bool addToSelected )
8754{
8b5f6d9d
VZ
8755 if ( !m_selection )
8756 return;
8757
8758 if ( !addToSelected )
e32352cf 8759 ClearSelection();
70c7a608 8760
8b5f6d9d 8761 m_selection->SelectRow(row);
f85afd4e
MB
8762}
8763
f85afd4e
MB
8764void wxGrid::SelectCol( int col, bool addToSelected )
8765{
8b5f6d9d
VZ
8766 if ( !m_selection )
8767 return;
8768
8769 if ( !addToSelected )
e32352cf 8770 ClearSelection();
f85afd4e 8771
8b5f6d9d 8772 m_selection->SelectCol(col);
f85afd4e
MB
8773}
8774
8b5f6d9d
VZ
8775void wxGrid::SelectBlock(int topRow, int leftCol, int bottomRow, int rightCol,
8776 bool addToSelected)
f85afd4e 8777{
8b5f6d9d
VZ
8778 if ( !m_selection )
8779 return;
8780
8781 if ( !addToSelected )
c9097836 8782 ClearSelection();
f85afd4e 8783
8b5f6d9d 8784 m_selection->SelectBlock(topRow, leftCol, bottomRow, rightCol);
f85afd4e
MB
8785}
8786
8787void wxGrid::SelectAll()
8788{
f74d0b57 8789 if ( m_numRows > 0 && m_numCols > 0 )
3f3dc2ef
VZ
8790 {
8791 if ( m_selection )
ccdee36f 8792 m_selection->SelectBlock( 0, 0, m_numRows - 1, m_numCols - 1 );
3f3dc2ef 8793 }
b5808881 8794}
f85afd4e 8795
962a48f6
DS
8796// ----------------------------------------------------------------------------
8797// cell, row and col deselection
8798// ----------------------------------------------------------------------------
f7b4b343 8799
bec70262 8800void wxGrid::DeselectLine(int line, const wxGridOperations& oper)
f7b4b343 8801{
3f3dc2ef
VZ
8802 if ( !m_selection )
8803 return;
8804
bec70262 8805 const wxGridSelectionModes mode = m_selection->GetSelectionMode();
475be9ce
VZ
8806 if ( mode == oper.GetSelectionMode() ||
8807 mode == wxGrid::wxGridSelectRowsOrColumns )
f7b4b343 8808 {
bec70262
VZ
8809 const wxGridCellCoords c(oper.MakeCoords(line, 0));
8810 if ( m_selection->IsInSelection(c) )
8811 m_selection->ToggleCellSelection(c);
ffdd3c98 8812 }
bec70262 8813 else if ( mode != oper.Dual().GetSelectionMode() )
f7b4b343 8814 {
bec70262
VZ
8815 const int nOther = oper.Dual().GetNumberOfLines(this);
8816 for ( int i = 0; i < nOther; i++ )
f7b4b343 8817 {
bec70262
VZ
8818 const wxGridCellCoords c(oper.MakeCoords(line, i));
8819 if ( m_selection->IsInSelection(c) )
8820 m_selection->ToggleCellSelection(c);
f7b4b343
VZ
8821 }
8822 }
bec70262
VZ
8823 //else: can only select orthogonal lines so no lines in this direction
8824 // could have been selected anyhow
f7b4b343
VZ
8825}
8826
bec70262 8827void wxGrid::DeselectRow(int row)
f7b4b343 8828{
bec70262
VZ
8829 DeselectLine(row, wxGridRowOperations());
8830}
3f3dc2ef 8831
bec70262
VZ
8832void wxGrid::DeselectCol(int col)
8833{
8834 DeselectLine(col, wxGridColumnOperations());
f7b4b343
VZ
8835}
8836
8837void wxGrid::DeselectCell( int row, int col )
8838{
3f3dc2ef 8839 if ( m_selection && m_selection->IsInSelection(row, col) )
f7b4b343
VZ
8840 m_selection->ToggleCellSelection(row, col);
8841}
8842
ef316e23 8843bool wxGrid::IsSelection() const
b5808881 8844{
3f3dc2ef 8845 return ( m_selection && (m_selection->IsSelection() ||
8a3e536c
VZ
8846 ( m_selectedBlockTopLeft != wxGridNoCellCoords &&
8847 m_selectedBlockBottomRight != wxGridNoCellCoords) ) );
f85afd4e
MB
8848}
8849
84035150 8850bool wxGrid::IsInSelection( int row, int col ) const
b5808881 8851{
3f3dc2ef 8852 return ( m_selection && (m_selection->IsInSelection( row, col ) ||
8a3e536c
VZ
8853 ( row >= m_selectedBlockTopLeft.GetRow() &&
8854 col >= m_selectedBlockTopLeft.GetCol() &&
8855 row <= m_selectedBlockBottomRight.GetRow() &&
8856 col <= m_selectedBlockBottomRight.GetCol() )) );
b5808881 8857}
f85afd4e 8858
aa5b8857
SN
8859wxGridCellCoordsArray wxGrid::GetSelectedCells() const
8860{
4db6714b
KH
8861 if (!m_selection)
8862 {
8863 wxGridCellCoordsArray a;
8864 return a;
8865 }
8866
aa5b8857
SN
8867 return m_selection->m_cellSelection;
8868}
4db6714b 8869
aa5b8857
SN
8870wxGridCellCoordsArray wxGrid::GetSelectionBlockTopLeft() const
8871{
4db6714b
KH
8872 if (!m_selection)
8873 {
8874 wxGridCellCoordsArray a;
8875 return a;
8876 }
8877
aa5b8857
SN
8878 return m_selection->m_blockSelectionTopLeft;
8879}
4db6714b 8880
aa5b8857
SN
8881wxGridCellCoordsArray wxGrid::GetSelectionBlockBottomRight() const
8882{
4db6714b
KH
8883 if (!m_selection)
8884 {
8885 wxGridCellCoordsArray a;
8886 return a;
8887 }
8888
a8de8190 8889 return m_selection->m_blockSelectionBottomRight;
aa5b8857 8890}
4db6714b 8891
aa5b8857
SN
8892wxArrayInt wxGrid::GetSelectedRows() const
8893{
4db6714b
KH
8894 if (!m_selection)
8895 {
8896 wxArrayInt a;
8897 return a;
8898 }
8899
aa5b8857
SN
8900 return m_selection->m_rowSelection;
8901}
4db6714b 8902
aa5b8857
SN
8903wxArrayInt wxGrid::GetSelectedCols() const
8904{
4db6714b
KH
8905 if (!m_selection)
8906 {
8907 wxArrayInt a;
8908 return a;
8909 }
8910
aa5b8857
SN
8911 return m_selection->m_colSelection;
8912}
8913
f85afd4e
MB
8914void wxGrid::ClearSelection()
8915{
8a3e536c
VZ
8916 wxRect r1 = BlockToDeviceRect(m_selectedBlockTopLeft,
8917 m_selectedBlockBottomRight);
8918 wxRect r2 = BlockToDeviceRect(m_currentCellCoords,
8919 m_selectedBlockCorner);
8920
8921 m_selectedBlockTopLeft =
8922 m_selectedBlockBottomRight =
8923 m_selectedBlockCorner = wxGridNoCellCoords;
8924
efc49a21
VZ
8925 if ( !r1.IsEmpty() )
8926 RefreshRect(r1, false);
8927 if ( !r2.IsEmpty() )
8928 RefreshRect(r2, false);
8a3e536c 8929
3f3dc2ef
VZ
8930 if ( m_selection )
8931 m_selection->ClearSelection();
8f177c8e 8932}
f85afd4e 8933
da6af900 8934// This function returns the rectangle that encloses the given block
2d66e025
MB
8935// in device coords clipped to the client size of the grid window.
8936//
731330ec
VZ
8937wxRect wxGrid::BlockToDeviceRect( const wxGridCellCoords& topLeft,
8938 const wxGridCellCoords& bottomRight ) const
f85afd4e 8939{
731330ec
VZ
8940 wxRect resultRect;
8941 wxRect tempCellRect = CellToRect(topLeft);
8942 if ( tempCellRect != wxGridNoCellRect )
f85afd4e 8943 {
731330ec 8944 resultRect = tempCellRect;
58dd5b3b
MB
8945 }
8946 else
8947 {
731330ec 8948 resultRect = wxRect(0, 0, 0, 0);
58dd5b3b 8949 }
60ff3b99 8950
731330ec
VZ
8951 tempCellRect = CellToRect(bottomRight);
8952 if ( tempCellRect != wxGridNoCellRect )
58dd5b3b 8953 {
731330ec 8954 resultRect += tempCellRect;
2d66e025
MB
8955 }
8956 else
8957 {
731330ec 8958 // If both inputs were "wxGridNoCellRect," then there's nothing to do.
2d66e025 8959 return wxGridNoCellRect;
f85afd4e
MB
8960 }
8961
731330ec
VZ
8962 // Ensure that left/right and top/bottom pairs are in order.
8963 int left = resultRect.GetLeft();
8964 int top = resultRect.GetTop();
8965 int right = resultRect.GetRight();
8966 int bottom = resultRect.GetBottom();
27f35b66
SN
8967
8968 int leftCol = topLeft.GetCol();
8969 int topRow = topLeft.GetRow();
8970 int rightCol = bottomRight.GetCol();
8971 int bottomRow = bottomRight.GetRow();
8972
3ed884a0
SN
8973 if (left > right)
8974 {
731330ec 8975 int tmp = left;
3ed884a0 8976 left = right;
731330ec
VZ
8977 right = tmp;
8978
8979 tmp = leftCol;
4db6714b 8980 leftCol = rightCol;
731330ec 8981 rightCol = tmp;
3ed884a0
SN
8982 }
8983
8984 if (top > bottom)
8985 {
731330ec 8986 int tmp = top;
3ed884a0 8987 top = bottom;
731330ec
VZ
8988 bottom = tmp;
8989
8990 tmp = topRow;
3ed884a0 8991 topRow = bottomRow;
731330ec 8992 bottomRow = tmp;
3ed884a0
SN
8993 }
8994
731330ec
VZ
8995 // The following loop is ONLY necessary to detect and handle merged cells.
8996 int cw, ch;
8997 m_gridWin->GetClientSize( &cw, &ch );
8998
8999 // Get the origin coordinates: notice that they will be negative if the
9000 // grid is scrolled downwards/to the right.
9001 int gridOriginX = 0;
9002 int gridOriginY = 0;
9003 CalcScrolledPosition(gridOriginX, gridOriginY, &gridOriginX, &gridOriginY);
9004
9005 int onScreenLeftmostCol = internalXToCol(-gridOriginX);
9006 int onScreenUppermostRow = internalYToRow(-gridOriginY);
9007
9008 int onScreenRightmostCol = internalXToCol(-gridOriginX + cw);
9009 int onScreenBottommostRow = internalYToRow(-gridOriginY + ch);
9010
9011 // Bound our loop so that we only examine the portion of the selected block
9012 // that is shown on screen. Therefore, we compare the Top-Left block values
9013 // to the Top-Left screen values, and the Bottom-Right block values to the
9014 // Bottom-Right screen values, choosing appropriately.
9015 const int visibleTopRow = wxMax(topRow, onScreenUppermostRow);
9016 const int visibleBottomRow = wxMin(bottomRow, onScreenBottommostRow);
9017 const int visibleLeftCol = wxMax(leftCol, onScreenLeftmostCol);
9018 const int visibleRightCol = wxMin(rightCol, onScreenRightmostCol);
9019
9020 for ( int j = visibleTopRow; j <= visibleBottomRow; j++ )
27f35b66 9021 {
731330ec 9022 for ( int i = visibleLeftCol; i <= visibleRightCol; i++ )
27f35b66 9023 {
731330ec
VZ
9024 if ( (j == visibleTopRow) || (j == visibleBottomRow) ||
9025 (i == visibleLeftCol) || (i == visibleRightCol) )
27f35b66 9026 {
731330ec 9027 tempCellRect = CellToRect( j, i );
27f35b66 9028
731330ec
VZ
9029 if (tempCellRect.x < left)
9030 left = tempCellRect.x;
9031 if (tempCellRect.y < top)
9032 top = tempCellRect.y;
9033 if (tempCellRect.x + tempCellRect.width > right)
9034 right = tempCellRect.x + tempCellRect.width;
9035 if (tempCellRect.y + tempCellRect.height > bottom)
9036 bottom = tempCellRect.y + tempCellRect.height;
27f35b66 9037 }
4db6714b
KH
9038 else
9039 {
731330ec 9040 i = visibleRightCol; // jump over inner cells.
4db6714b 9041 }
27f35b66
SN
9042 }
9043 }
9044
731330ec 9045 // Convert to scrolled coords
27f35b66
SN
9046 CalcScrolledPosition( left, top, &left, &top );
9047 CalcScrolledPosition( right, bottom, &right, &bottom );
58dd5b3b 9048
f6bcfd97 9049 if (right < 0 || bottom < 0 || left > cw || top > ch)
c47addef 9050 return wxRect(0,0,0,0);
f6bcfd97 9051
731330ec
VZ
9052 resultRect.SetLeft( wxMax(0, left) );
9053 resultRect.SetTop( wxMax(0, top) );
9054 resultRect.SetRight( wxMin(cw, right) );
9055 resultRect.SetBottom( wxMin(ch, bottom) );
58dd5b3b 9056
731330ec 9057 return resultRect;
f85afd4e
MB
9058}
9059
574e1c5a
VZ
9060void wxGrid::DoSetSizes(const wxGridSizesInfo& sizeInfo,
9061 const wxGridOperations& oper)
9062{
9063 BeginBatch();
9064 oper.SetDefaultLineSize(this, sizeInfo.m_sizeDefault, true);
9065 const int numLines = oper.GetNumberOfLines(this);
9066 for ( int i = 0; i < numLines; i++ )
9067 {
9068 int size = sizeInfo.GetSize(i);
9069 if ( size != sizeInfo.m_sizeDefault)
9070 oper.SetLineSize(this, i, size);
9071 }
9072 EndBatch();
9073}
9074
9075void wxGrid::SetColSizes(const wxGridSizesInfo& sizeInfo)
9076{
9077 DoSetSizes(sizeInfo, wxGridColumnOperations());
9078}
9079
9080void wxGrid::SetRowSizes(const wxGridSizesInfo& sizeInfo)
9081{
9082 DoSetSizes(sizeInfo, wxGridRowOperations());
9083}
9084
9085wxGridSizesInfo::wxGridSizesInfo(int defSize, const wxArrayInt& allSizes)
9086{
9087 m_sizeDefault = defSize;
9088 for ( size_t i = 0; i < allSizes.size(); i++ )
9089 {
9090 if ( allSizes[i] != defSize )
9091 m_customSizes[i] = allSizes[i];
9092 }
9093}
9094
9095int wxGridSizesInfo::GetSize(unsigned pos) const
9096{
9097 wxUnsignedToIntHashMap::const_iterator it = m_customSizes.find(pos);
9098
30496905
VZ
9099 // if it's not found return the default
9100 if ( it == m_customSizes.end() )
9101 return m_sizeDefault;
9102
9103 // otherwise return 0 if it's hidden, currently there is no way to get
9104 // its size before it had been hidden
9105 if ( it->second < 0 )
9106 return 0;
9107
9108 return it->second;
574e1c5a
VZ
9109}
9110
1edce33f
VZ
9111// ----------------------------------------------------------------------------
9112// drop target
9113// ----------------------------------------------------------------------------
9114
9115#if wxUSE_DRAG_AND_DROP
9116
9117// this allow setting drop target directly on wxGrid
9118void wxGrid::SetDropTarget(wxDropTarget *dropTarget)
9119{
9120 GetGridWindow()->SetDropTarget(dropTarget);
9121}
9122
9123#endif // wxUSE_DRAG_AND_DROP
9124
962a48f6
DS
9125// ----------------------------------------------------------------------------
9126// grid event classes
9127// ----------------------------------------------------------------------------
f85afd4e 9128
bf7945ce 9129IMPLEMENT_DYNAMIC_CLASS( wxGridEvent, wxNotifyEvent )
f85afd4e
MB
9130
9131wxGridEvent::wxGridEvent( int id, wxEventType type, wxObject* obj,
5c8fc7c1 9132 int row, int col, int x, int y, bool sel,
f85afd4e 9133 bool control, bool shift, bool alt, bool meta )
8b5f6d9d
VZ
9134 : wxNotifyEvent( type, id ),
9135 wxKeyboardState(control, shift, alt, meta)
f85afd4e 9136{
8b5f6d9d 9137 Init(row, col, x, y, sel);
8f177c8e 9138
f85afd4e
MB
9139 SetEventObject(obj);
9140}
9141
bf7945ce 9142IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent, wxNotifyEvent )
f85afd4e
MB
9143
9144wxGridSizeEvent::wxGridSizeEvent( int id, wxEventType type, wxObject* obj,
9145 int rowOrCol, int x, int y,
9146 bool control, bool shift, bool alt, bool meta )
8b5f6d9d
VZ
9147 : wxNotifyEvent( type, id ),
9148 wxKeyboardState(control, shift, alt, meta)
f85afd4e 9149{
8b5f6d9d 9150 Init(rowOrCol, x, y);
8f177c8e 9151
f85afd4e
MB
9152 SetEventObject(obj);
9153}
9154
9155
bf7945ce 9156IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent, wxNotifyEvent )
f85afd4e
MB
9157
9158wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id, wxEventType type, wxObject* obj,
8f177c8e
VZ
9159 const wxGridCellCoords& topLeft,
9160 const wxGridCellCoords& bottomRight,
5c8fc7c1
SN
9161 bool sel, bool control,
9162 bool shift, bool alt, bool meta )
8b5f6d9d
VZ
9163 : wxNotifyEvent( type, id ),
9164 wxKeyboardState(control, shift, alt, meta)
9165{
9166 Init(topLeft, bottomRight, sel);
f85afd4e
MB
9167
9168 SetEventObject(obj);
9169}
9170
9171
bf7945ce
RD
9172IMPLEMENT_DYNAMIC_CLASS(wxGridEditorCreatedEvent, wxCommandEvent)
9173
9174wxGridEditorCreatedEvent::wxGridEditorCreatedEvent(int id, wxEventType type,
9175 wxObject* obj, int row,
9176 int col, wxControl* ctrl)
9177 : wxCommandEvent(type, id)
9178{
9179 SetEventObject(obj);
9180 m_row = row;
9181 m_col = col;
9182 m_ctrl = ctrl;
9183}
9184
29efc6e4
FM
9185
9186// ----------------------------------------------------------------------------
9187// wxGridTypeRegistry
9188// ----------------------------------------------------------------------------
9189
9190wxGridTypeRegistry::~wxGridTypeRegistry()
9191{
9192 size_t count = m_typeinfo.GetCount();
9193 for ( size_t i = 0; i < count; i++ )
9194 delete m_typeinfo[i];
9195}
9196
9197void wxGridTypeRegistry::RegisterDataType(const wxString& typeName,
9198 wxGridCellRenderer* renderer,
9199 wxGridCellEditor* editor)
9200{
9201 wxGridDataTypeInfo* info = new wxGridDataTypeInfo(typeName, renderer, editor);
9202
9203 // is it already registered?
9204 int loc = FindRegisteredDataType(typeName);
9205 if ( loc != wxNOT_FOUND )
9206 {
9207 delete m_typeinfo[loc];
9208 m_typeinfo[loc] = info;
9209 }
9210 else
9211 {
9212 m_typeinfo.Add(info);
9213 }
9214}
9215
9216int wxGridTypeRegistry::FindRegisteredDataType(const wxString& typeName)
9217{
9218 size_t count = m_typeinfo.GetCount();
9219 for ( size_t i = 0; i < count; i++ )
9220 {
9221 if ( typeName == m_typeinfo[i]->m_typeName )
9222 {
9223 return i;
9224 }
9225 }
9226
9227 return wxNOT_FOUND;
9228}
9229
9230int wxGridTypeRegistry::FindDataType(const wxString& typeName)
9231{
9232 int index = FindRegisteredDataType(typeName);
9233 if ( index == wxNOT_FOUND )
9234 {
9235 // check whether this is one of the standard ones, in which case
9236 // register it "on the fly"
9237#if wxUSE_TEXTCTRL
9238 if ( typeName == wxGRID_VALUE_STRING )
9239 {
9240 RegisterDataType(wxGRID_VALUE_STRING,
9241 new wxGridCellStringRenderer,
9242 new wxGridCellTextEditor);
9243 }
9244 else
9245#endif // wxUSE_TEXTCTRL
9246#if wxUSE_CHECKBOX
9247 if ( typeName == wxGRID_VALUE_BOOL )
9248 {
9249 RegisterDataType(wxGRID_VALUE_BOOL,
9250 new wxGridCellBoolRenderer,
9251 new wxGridCellBoolEditor);
9252 }
9253 else
9254#endif // wxUSE_CHECKBOX
9255#if wxUSE_TEXTCTRL
9256 if ( typeName == wxGRID_VALUE_NUMBER )
9257 {
9258 RegisterDataType(wxGRID_VALUE_NUMBER,
9259 new wxGridCellNumberRenderer,
9260 new wxGridCellNumberEditor);
9261 }
9262 else if ( typeName == wxGRID_VALUE_FLOAT )
9263 {
9264 RegisterDataType(wxGRID_VALUE_FLOAT,
9265 new wxGridCellFloatRenderer,
9266 new wxGridCellFloatEditor);
9267 }
9268 else
9269#endif // wxUSE_TEXTCTRL
9270#if wxUSE_COMBOBOX
9271 if ( typeName == wxGRID_VALUE_CHOICE )
9272 {
9273 RegisterDataType(wxGRID_VALUE_CHOICE,
9274 new wxGridCellStringRenderer,
9275 new wxGridCellChoiceEditor);
9276 }
9277 else
9278#endif // wxUSE_COMBOBOX
9279 {
9280 return wxNOT_FOUND;
9281 }
9282
9283 // we get here only if just added the entry for this type, so return
9284 // the last index
9285 index = m_typeinfo.GetCount() - 1;
9286 }
9287
9288 return index;
9289}
9290
9291int wxGridTypeRegistry::FindOrCloneDataType(const wxString& typeName)
9292{
9293 int index = FindDataType(typeName);
9294 if ( index == wxNOT_FOUND )
9295 {
9296 // the first part of the typename is the "real" type, anything after ':'
9297 // are the parameters for the renderer
9a83f860 9298 index = FindDataType(typeName.BeforeFirst(wxT(':')));
29efc6e4
FM
9299 if ( index == wxNOT_FOUND )
9300 {
9301 return wxNOT_FOUND;
9302 }
9303
9304 wxGridCellRenderer *renderer = GetRenderer(index);
9305 wxGridCellRenderer *rendererOld = renderer;
9306 renderer = renderer->Clone();
9307 rendererOld->DecRef();
9308
9309 wxGridCellEditor *editor = GetEditor(index);
9310 wxGridCellEditor *editorOld = editor;
9311 editor = editor->Clone();
9312 editorOld->DecRef();
9313
9314 // do it even if there are no parameters to reset them to defaults
9a83f860 9315 wxString params = typeName.AfterFirst(wxT(':'));
29efc6e4
FM
9316 renderer->SetParameters(params);
9317 editor->SetParameters(params);
9318
9319 // register the new typename
9320 RegisterDataType(typeName, renderer, editor);
9321
9322 // we just registered it, it's the last one
9323 index = m_typeinfo.GetCount() - 1;
9324 }
9325
9326 return index;
9327}
9328
9329wxGridCellRenderer* wxGridTypeRegistry::GetRenderer(int index)
9330{
9331 wxGridCellRenderer* renderer = m_typeinfo[index]->m_renderer;
9332 if (renderer)
9333 renderer->IncRef();
9334
9335 return renderer;
9336}
9337
9338wxGridCellEditor* wxGridTypeRegistry::GetEditor(int index)
9339{
9340 wxGridCellEditor* editor = m_typeinfo[index]->m_editor;
9341 if (editor)
9342 editor->IncRef();
9343
9344 return editor;
9345}
9346
27b92ca4 9347#endif // wxUSE_GRID