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