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