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