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