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