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