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