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