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