]> git.saurik.com Git - wxWidgets.git/blame - src/generic/grid.cpp
Fixed sorting and commiting support for native virtual wxListCtrl, although leaving...
[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
95427194 12// For compilers that support precompilation, includes "wx/wx.h".
4d85bcd1
JS
13#include "wx/wxprec.h"
14
f85afd4e
MB
15#ifdef __BORLANDC__
16 #pragma hdrstop
17#endif
18
27b92ca4
VZ
19#if wxUSE_GRID
20
f85afd4e
MB
21#ifndef WX_PRECOMP
22 #include "wx/utils.h"
23 #include "wx/dcclient.h"
24 #include "wx/settings.h"
25 #include "wx/log.h"
508011ce
VZ
26 #include "wx/textctrl.h"
27 #include "wx/checkbox.h"
4ee5fc9c 28 #include "wx/combobox.h"
816be743 29 #include "wx/valtext.h"
60d876f3 30 #include "wx/intl.h"
c77a6796 31 #include "wx/math.h"
2a673eb1 32 #include "wx/listbox.h"
f85afd4e
MB
33#endif
34
cb5df486 35#include "wx/textfile.h"
816be743 36#include "wx/spinctrl.h"
c4608a8a 37#include "wx/tokenzr.h"
4d1bc39c 38#include "wx/renderer.h"
6d004f67 39
07296f0b 40#include "wx/grid.h"
b5808881 41#include "wx/generic/gridsel.h"
07296f0b 42
0b7e6e7d
SN
43#if defined(__WXMOTIF__)
44 #define WXUNUSED_MOTIF(identifier) WXUNUSED(identifier)
c78b3acd 45#else
0b7e6e7d 46 #define WXUNUSED_MOTIF(identifier) identifier
c78b3acd
SN
47#endif
48
49#if defined(__WXGTK__)
50 #define WXUNUSED_GTK(identifier) WXUNUSED(identifier)
51#else
52 #define WXUNUSED_GTK(identifier) identifier
53#endif
54
3f8e5072
JS
55// Required for wxIs... functions
56#include <ctype.h>
57
b99be8fb 58// ----------------------------------------------------------------------------
758cbedf 59// array classes
b99be8fb
VZ
60// ----------------------------------------------------------------------------
61
d5d29b8a 62WX_DEFINE_ARRAY_WITH_DECL_PTR(wxGridCellAttr *, wxArrayAttrs,
160ba750 63 class WXDLLIMPEXP_ADV);
758cbedf 64
b99be8fb
VZ
65struct wxGridCellWithAttr
66{
2e9a6788
VZ
67 wxGridCellWithAttr(int row, int col, wxGridCellAttr *attr_)
68 : coords(row, col), attr(attr_)
b99be8fb
VZ
69 {
70 }
71
2e9a6788
VZ
72 ~wxGridCellWithAttr()
73 {
74 attr->DecRef();
75 }
76
b99be8fb 77 wxGridCellCoords coords;
2e9a6788 78 wxGridCellAttr *attr;
22f3361e
VZ
79
80// Cannot do this:
81// DECLARE_NO_COPY_CLASS(wxGridCellWithAttr)
82// without rewriting the macros, which require a public copy constructor.
b99be8fb
VZ
83};
84
160ba750
VS
85WX_DECLARE_OBJARRAY_WITH_DECL(wxGridCellWithAttr, wxGridCellWithAttrArray,
86 class WXDLLIMPEXP_ADV);
b99be8fb
VZ
87
88#include "wx/arrimpl.cpp"
89
90WX_DEFINE_OBJARRAY(wxGridCellCoordsArray)
91WX_DEFINE_OBJARRAY(wxGridCellWithAttrArray)
92
0f442030
RR
93// ----------------------------------------------------------------------------
94// events
95// ----------------------------------------------------------------------------
96
2e4df4bf
VZ
97DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_LEFT_CLICK)
98DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_RIGHT_CLICK)
99DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_LEFT_DCLICK)
100DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_RIGHT_DCLICK)
79dbea21 101DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_BEGIN_DRAG)
2e4df4bf
VZ
102DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_LEFT_CLICK)
103DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_RIGHT_CLICK)
104DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_LEFT_DCLICK)
105DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_RIGHT_DCLICK)
106DEFINE_EVENT_TYPE(wxEVT_GRID_ROW_SIZE)
107DEFINE_EVENT_TYPE(wxEVT_GRID_COL_SIZE)
d4175745 108DEFINE_EVENT_TYPE(wxEVT_GRID_COL_MOVE)
2e4df4bf
VZ
109DEFINE_EVENT_TYPE(wxEVT_GRID_RANGE_SELECT)
110DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_CHANGE)
111DEFINE_EVENT_TYPE(wxEVT_GRID_SELECT_CELL)
112DEFINE_EVENT_TYPE(wxEVT_GRID_EDITOR_SHOWN)
113DEFINE_EVENT_TYPE(wxEVT_GRID_EDITOR_HIDDEN)
bf7945ce 114DEFINE_EVENT_TYPE(wxEVT_GRID_EDITOR_CREATED)
0f442030 115
b99be8fb
VZ
116// ----------------------------------------------------------------------------
117// private classes
118// ----------------------------------------------------------------------------
119
12f190b0 120class WXDLLIMPEXP_ADV wxGridRowLabelWindow : public wxWindow
b99be8fb
VZ
121{
122public:
123 wxGridRowLabelWindow() { m_owner = (wxGrid *)NULL; }
124 wxGridRowLabelWindow( wxGrid *parent, wxWindowID id,
125 const wxPoint &pos, const wxSize &size );
126
127private:
128 wxGrid *m_owner;
129
130 void OnPaint( wxPaintEvent& event );
131 void OnMouseEvent( wxMouseEvent& event );
b51c3f27 132 void OnMouseWheel( wxMouseEvent& event );
b99be8fb 133 void OnKeyDown( wxKeyEvent& event );
f6bcfd97 134 void OnKeyUp( wxKeyEvent& );
63e2147c 135 void OnChar( wxKeyEvent& );
b99be8fb
VZ
136
137 DECLARE_DYNAMIC_CLASS(wxGridRowLabelWindow)
138 DECLARE_EVENT_TABLE()
22f3361e 139 DECLARE_NO_COPY_CLASS(wxGridRowLabelWindow)
b99be8fb
VZ
140};
141
142
12f190b0 143class WXDLLIMPEXP_ADV wxGridColLabelWindow : public wxWindow
b99be8fb
VZ
144{
145public:
146 wxGridColLabelWindow() { m_owner = (wxGrid *)NULL; }
147 wxGridColLabelWindow( wxGrid *parent, wxWindowID id,
148 const wxPoint &pos, const wxSize &size );
149
150private:
151 wxGrid *m_owner;
152
a9339fe2 153 void OnPaint( wxPaintEvent& event );
b99be8fb 154 void OnMouseEvent( wxMouseEvent& event );
b51c3f27 155 void OnMouseWheel( wxMouseEvent& event );
b99be8fb 156 void OnKeyDown( wxKeyEvent& event );
f6bcfd97 157 void OnKeyUp( wxKeyEvent& );
63e2147c 158 void OnChar( wxKeyEvent& );
b99be8fb
VZ
159
160 DECLARE_DYNAMIC_CLASS(wxGridColLabelWindow)
161 DECLARE_EVENT_TABLE()
22f3361e 162 DECLARE_NO_COPY_CLASS(wxGridColLabelWindow)
b99be8fb
VZ
163};
164
165
12f190b0 166class WXDLLIMPEXP_ADV wxGridCornerLabelWindow : public wxWindow
b99be8fb
VZ
167{
168public:
169 wxGridCornerLabelWindow() { m_owner = (wxGrid *)NULL; }
170 wxGridCornerLabelWindow( wxGrid *parent, wxWindowID id,
171 const wxPoint &pos, const wxSize &size );
172
173private:
174 wxGrid *m_owner;
175
176 void OnMouseEvent( wxMouseEvent& event );
b51c3f27 177 void OnMouseWheel( wxMouseEvent& event );
b99be8fb 178 void OnKeyDown( wxKeyEvent& event );
f6bcfd97 179 void OnKeyUp( wxKeyEvent& );
63e2147c 180 void OnChar( wxKeyEvent& );
b99be8fb
VZ
181 void OnPaint( wxPaintEvent& event );
182
183 DECLARE_DYNAMIC_CLASS(wxGridCornerLabelWindow)
184 DECLARE_EVENT_TABLE()
22f3361e 185 DECLARE_NO_COPY_CLASS(wxGridCornerLabelWindow)
b99be8fb
VZ
186};
187
12f190b0 188class WXDLLIMPEXP_ADV wxGridWindow : public wxWindow
b99be8fb
VZ
189{
190public:
191 wxGridWindow()
192 {
c2f5b920
DS
193 m_owner = NULL;
194 m_rowLabelWin = NULL;
195 m_colLabelWin = NULL;
b99be8fb
VZ
196 }
197
198 wxGridWindow( wxGrid *parent,
199 wxGridRowLabelWindow *rowLblWin,
200 wxGridColLabelWindow *colLblWin,
201 wxWindowID id, const wxPoint &pos, const wxSize &size );
d3c7fc99 202 virtual ~wxGridWindow() {}
b99be8fb
VZ
203
204 void ScrollWindow( int dx, int dy, const wxRect *rect );
205
b819b854
JS
206 wxGrid* GetOwner() { return m_owner; }
207
b99be8fb
VZ
208private:
209 wxGrid *m_owner;
210 wxGridRowLabelWindow *m_rowLabelWin;
211 wxGridColLabelWindow *m_colLabelWin;
212
213 void OnPaint( wxPaintEvent &event );
b51c3f27 214 void OnMouseWheel( wxMouseEvent& event );
b99be8fb
VZ
215 void OnMouseEvent( wxMouseEvent& event );
216 void OnKeyDown( wxKeyEvent& );
f6bcfd97 217 void OnKeyUp( wxKeyEvent& );
63e2147c 218 void OnChar( wxKeyEvent& );
2796cce3 219 void OnEraseBackground( wxEraseEvent& );
80acaf25 220 void OnFocus( wxFocusEvent& );
b99be8fb
VZ
221
222 DECLARE_DYNAMIC_CLASS(wxGridWindow)
223 DECLARE_EVENT_TABLE()
22f3361e 224 DECLARE_NO_COPY_CLASS(wxGridWindow)
b99be8fb
VZ
225};
226
2796cce3 227
2796cce3
RD
228class wxGridCellEditorEvtHandler : public wxEvtHandler
229{
230public:
2796cce3 231 wxGridCellEditorEvtHandler(wxGrid* grid, wxGridCellEditor* editor)
140954fd 232 : m_grid(grid),
08dd04d0
JS
233 m_editor(editor),
234 m_inSetFocus(false)
140954fd
VZ
235 {
236 }
2796cce3 237
140954fd 238 void OnKillFocus(wxFocusEvent& event);
2796cce3 239 void OnKeyDown(wxKeyEvent& event);
fb0de762 240 void OnChar(wxKeyEvent& event);
2796cce3 241
08dd04d0
JS
242 void SetInSetFocus(bool inSetFocus) { m_inSetFocus = inSetFocus; }
243
2796cce3 244private:
2f024384
DS
245 wxGrid *m_grid;
246 wxGridCellEditor *m_editor;
140954fd 247
08dd04d0
JS
248 // Work around the fact that a focus kill event can be sent to
249 // a combobox within a set focus event.
250 bool m_inSetFocus;
7448de8d 251
2796cce3 252 DECLARE_EVENT_TABLE()
140954fd 253 DECLARE_DYNAMIC_CLASS(wxGridCellEditorEvtHandler)
22f3361e 254 DECLARE_NO_COPY_CLASS(wxGridCellEditorEvtHandler)
2796cce3
RD
255};
256
257
140954fd
VZ
258IMPLEMENT_ABSTRACT_CLASS(wxGridCellEditorEvtHandler, wxEvtHandler)
259
2796cce3 260BEGIN_EVENT_TABLE( wxGridCellEditorEvtHandler, wxEvtHandler )
140954fd 261 EVT_KILL_FOCUS( wxGridCellEditorEvtHandler::OnKillFocus )
2796cce3 262 EVT_KEY_DOWN( wxGridCellEditorEvtHandler::OnKeyDown )
fb0de762 263 EVT_CHAR( wxGridCellEditorEvtHandler::OnChar )
2796cce3
RD
264END_EVENT_TABLE()
265
266
758cbedf 267// ----------------------------------------------------------------------------
b99be8fb 268// the internal data representation used by wxGridCellAttrProvider
758cbedf
VZ
269// ----------------------------------------------------------------------------
270
271// this class stores attributes set for cells
12f190b0 272class WXDLLIMPEXP_ADV wxGridCellAttrData
b99be8fb
VZ
273{
274public:
2e9a6788 275 void SetAttr(wxGridCellAttr *attr, int row, int col);
b99be8fb 276 wxGridCellAttr *GetAttr(int row, int col) const;
4d60017a
SN
277 void UpdateAttrRows( size_t pos, int numRows );
278 void UpdateAttrCols( size_t pos, int numCols );
b99be8fb
VZ
279
280private:
281 // searches for the attr for given cell, returns wxNOT_FOUND if not found
282 int FindIndex(int row, int col) const;
283
284 wxGridCellWithAttrArray m_attrs;
285};
286
758cbedf 287// this class stores attributes set for rows or columns
12f190b0 288class WXDLLIMPEXP_ADV wxGridRowOrColAttrData
758cbedf
VZ
289{
290public:
ee6694a7 291 // empty ctor to suppress warnings
2f024384 292 wxGridRowOrColAttrData() {}
758cbedf
VZ
293 ~wxGridRowOrColAttrData();
294
295 void SetAttr(wxGridCellAttr *attr, int rowOrCol);
296 wxGridCellAttr *GetAttr(int rowOrCol) const;
4d60017a 297 void UpdateAttrRowsOrCols( size_t pos, int numRowsOrCols );
758cbedf
VZ
298
299private:
300 wxArrayInt m_rowsOrCols;
301 wxArrayAttrs m_attrs;
302};
303
304// NB: this is just a wrapper around 3 objects: one which stores cell
305// attributes, and 2 others for row/col ones
12f190b0 306class WXDLLIMPEXP_ADV wxGridCellAttrProviderData
758cbedf
VZ
307{
308public:
309 wxGridCellAttrData m_cellAttrs;
310 wxGridRowOrColAttrData m_rowAttrs,
311 m_colAttrs;
312};
313
f2d76237
RD
314
315// ----------------------------------------------------------------------------
316// data structures used for the data type registry
317// ----------------------------------------------------------------------------
318
b94ae1ea
VZ
319struct wxGridDataTypeInfo
320{
f2d76237
RD
321 wxGridDataTypeInfo(const wxString& typeName,
322 wxGridCellRenderer* renderer,
323 wxGridCellEditor* editor)
324 : m_typeName(typeName), m_renderer(renderer), m_editor(editor)
2f024384 325 {}
f2d76237 326
39bcce60
VZ
327 ~wxGridDataTypeInfo()
328 {
329 wxSafeDecRef(m_renderer);
330 wxSafeDecRef(m_editor);
331 }
f2d76237
RD
332
333 wxString m_typeName;
334 wxGridCellRenderer* m_renderer;
335 wxGridCellEditor* m_editor;
22f3361e
VZ
336
337 DECLARE_NO_COPY_CLASS(wxGridDataTypeInfo)
f2d76237
RD
338};
339
340
d5d29b8a 341WX_DEFINE_ARRAY_WITH_DECL_PTR(wxGridDataTypeInfo*, wxGridDataTypeInfoArray,
160ba750 342 class WXDLLIMPEXP_ADV);
f2d76237
RD
343
344
12f190b0 345class WXDLLIMPEXP_ADV wxGridTypeRegistry
b94ae1ea 346{
f2d76237 347public:
c78b3acd 348 wxGridTypeRegistry() {}
f2d76237 349 ~wxGridTypeRegistry();
b94ae1ea 350
f2d76237
RD
351 void RegisterDataType(const wxString& typeName,
352 wxGridCellRenderer* renderer,
353 wxGridCellEditor* editor);
c4608a8a
VZ
354
355 // find one of already registered data types
356 int FindRegisteredDataType(const wxString& typeName);
357
358 // try to FindRegisteredDataType(), if this fails and typeName is one of
359 // standard typenames, register it and return its index
f2d76237 360 int FindDataType(const wxString& typeName);
c4608a8a
VZ
361
362 // try to FindDataType(), if it fails see if it is not one of already
363 // registered data types with some params in which case clone the
364 // registered data type and set params for it
365 int FindOrCloneDataType(const wxString& typeName);
366
f2d76237
RD
367 wxGridCellRenderer* GetRenderer(int index);
368 wxGridCellEditor* GetEditor(int index);
369
370private:
371 wxGridDataTypeInfoArray m_typeinfo;
372};
373
a9339fe2 374
b99be8fb
VZ
375// ----------------------------------------------------------------------------
376// conditional compilation
377// ----------------------------------------------------------------------------
378
9496deb5
MB
379#ifndef WXGRID_DRAW_LINES
380#define WXGRID_DRAW_LINES 1
796df70a
SN
381#endif
382
0a976765
VZ
383// ----------------------------------------------------------------------------
384// globals
385// ----------------------------------------------------------------------------
386
387//#define DEBUG_ATTR_CACHE
388#ifdef DEBUG_ATTR_CACHE
389 static size_t gs_nAttrCacheHits = 0;
390 static size_t gs_nAttrCacheMisses = 0;
2f024384 391#endif
f85afd4e 392
43947979
VZ
393// ----------------------------------------------------------------------------
394// constants
395// ----------------------------------------------------------------------------
396
f85afd4e 397wxGridCellCoords wxGridNoCellCoords( -1, -1 );
2f024384 398wxRect wxGridNoCellRect( -1, -1, -1, -1 );
f85afd4e 399
f0102d2a 400// scroll line size
faec5a43
SN
401// TODO: this doesn't work at all, grid cells have different sizes and approx
402// calculations don't work as because of the size mismatch scrollbars
403// sometimes fail to be shown when they should be or vice versa
b51c3f27
RD
404//
405// The scroll bars may be a little flakey once in a while, but that is
406// surely much less horrible than having scroll lines of only 1!!!
407// -- Robin
97a9929e
VZ
408//
409// Well, it's still seriously broken so it might be better but needs
410// fixing anyhow
411// -- Vadim
412static const size_t GRID_SCROLL_LINE_X = 15; // 1;
413static const size_t GRID_SCROLL_LINE_Y = GRID_SCROLL_LINE_X;
f85afd4e 414
43947979
VZ
415// the size of hash tables used a bit everywhere (the max number of elements
416// in these hash tables is the number of rows/columns)
417static const int GRID_HASH_SIZE = 100;
418
608754c4 419#if 0
97a9929e
VZ
420// ----------------------------------------------------------------------------
421// private functions
422// ----------------------------------------------------------------------------
423
d0eb7e56 424static inline int GetScrollX(int x)
97a9929e
VZ
425{
426 return (x + GRID_SCROLL_LINE_X - 1) / GRID_SCROLL_LINE_X;
427}
428
d0eb7e56 429static inline int GetScrollY(int y)
97a9929e
VZ
430{
431 return (y + GRID_SCROLL_LINE_Y - 1) / GRID_SCROLL_LINE_Y;
432}
608754c4 433#endif
97a9929e 434
ab79958a
VZ
435// ============================================================================
436// implementation
437// ============================================================================
438
2796cce3
RD
439// ----------------------------------------------------------------------------
440// wxGridCellEditor
441// ----------------------------------------------------------------------------
442
443wxGridCellEditor::wxGridCellEditor()
444{
445 m_control = NULL;
1bd71df9 446 m_attr = NULL;
2796cce3
RD
447}
448
2796cce3
RD
449wxGridCellEditor::~wxGridCellEditor()
450{
451 Destroy();
452}
453
508011ce
VZ
454void wxGridCellEditor::Create(wxWindow* WXUNUSED(parent),
455 wxWindowID WXUNUSED(id),
456 wxEvtHandler* evtHandler)
457{
189d0213 458 if ( evtHandler )
508011ce
VZ
459 m_control->PushEventHandler(evtHandler);
460}
2796cce3 461
189d0213
VZ
462void wxGridCellEditor::PaintBackground(const wxRect& rectCell,
463 wxGridCellAttr *attr)
464{
465 // erase the background because we might not fill the cell
466 wxClientDC dc(m_control->GetParent());
b819b854
JS
467 wxGridWindow* gridWindow = wxDynamicCast(m_control->GetParent(), wxGridWindow);
468 if (gridWindow)
469 gridWindow->GetOwner()->PrepareDC(dc);
ef5df12b 470
189d0213
VZ
471 dc.SetPen(*wxTRANSPARENT_PEN);
472 dc.SetBrush(wxBrush(attr->GetBackgroundColour(), wxSOLID));
473 dc.DrawRectangle(rectCell);
474
475 // redraw the control we just painted over
476 m_control->Refresh();
477}
478
2796cce3
RD
479void wxGridCellEditor::Destroy()
480{
508011ce
VZ
481 if (m_control)
482 {
a9339fe2 483 m_control->PopEventHandler( true /* delete it*/ );
b94ae1ea 484
2796cce3
RD
485 m_control->Destroy();
486 m_control = NULL;
487 }
488}
489
3da93aae 490void wxGridCellEditor::Show(bool show, wxGridCellAttr *attr)
2796cce3 491{
2f024384
DS
492 wxASSERT_MSG(m_control, wxT("The wxGridCellEditor must be created first!"));
493
2796cce3 494 m_control->Show(show);
3da93aae
VZ
495
496 if ( show )
497 {
498 // set the colours/fonts if we have any
499 if ( attr )
500 {
f2d76237
RD
501 m_colFgOld = m_control->GetForegroundColour();
502 m_control->SetForegroundColour(attr->GetTextColour());
3da93aae 503
f2d76237
RD
504 m_colBgOld = m_control->GetBackgroundColour();
505 m_control->SetBackgroundColour(attr->GetBackgroundColour());
3da93aae 506
0e871ad0 507// Workaround for GTK+1 font setting problem on some platforms
ea2d542c 508#if !defined(__WXGTK__) || defined(__WXGTK20__)
f2d76237
RD
509 m_fontOld = m_control->GetFont();
510 m_control->SetFont(attr->GetFont());
ea2d542c 511#endif
a9339fe2 512
3da93aae
VZ
513 // can't do anything more in the base class version, the other
514 // attributes may only be used by the derived classes
515 }
516 }
517 else
518 {
519 // restore the standard colours fonts
520 if ( m_colFgOld.Ok() )
521 {
522 m_control->SetForegroundColour(m_colFgOld);
523 m_colFgOld = wxNullColour;
524 }
525
526 if ( m_colBgOld.Ok() )
527 {
528 m_control->SetBackgroundColour(m_colBgOld);
529 m_colBgOld = wxNullColour;
530 }
2f024384 531
0e871ad0 532// Workaround for GTK+1 font setting problem on some platforms
ea2d542c 533#if !defined(__WXGTK__) || defined(__WXGTK20__)
3da93aae
VZ
534 if ( m_fontOld.Ok() )
535 {
536 m_control->SetFont(m_fontOld);
537 m_fontOld = wxNullFont;
538 }
ea2d542c 539#endif
3da93aae 540 }
2796cce3
RD
541}
542
543void wxGridCellEditor::SetSize(const wxRect& rect)
544{
2f024384
DS
545 wxASSERT_MSG(m_control, wxT("The wxGridCellEditor must be created first!"));
546
28a77bc4 547 m_control->SetSize(rect, wxSIZE_ALLOW_MINUS_ONE);
2796cce3
RD
548}
549
550void wxGridCellEditor::HandleReturn(wxKeyEvent& event)
551{
552 event.Skip();
553}
554
f6bcfd97
BP
555bool wxGridCellEditor::IsAcceptedKey(wxKeyEvent& event)
556{
63e2147c
RD
557 bool ctrl = event.ControlDown();
558 bool alt = event.AltDown();
2f024384 559
63e2147c
RD
560#ifdef __WXMAC__
561 // On the Mac the Alt key is more like shift and is used for entry of
562 // valid characters, so check for Ctrl and Meta instead.
563 alt = event.MetaDown();
564#endif
565
566 // Assume it's not a valid char if ctrl or alt is down, but if both are
567 // down then it may be because of an AltGr key combination, so let them
568 // through in that case.
569 if ((ctrl || alt) && !(ctrl && alt))
570 return false;
902725ee 571
2f024384 572 int key = 0;
63e2147c
RD
573 bool keyOk = true;
574
e1a66d9a
RD
575#ifdef __WXGTK20__
576 // If it's a F-Key or other special key then it shouldn't start the
577 // editor.
578 if (event.GetKeyCode() >= WXK_START)
579 return false;
580#endif
2f024384 581#if wxUSE_UNICODE
63e2147c
RD
582 // if the unicode key code is not really a unicode character (it may
583 // be a function key or etc., the platforms appear to always give us a
2f024384 584 // small value in this case) then fallback to the ASCII key code but
63e2147c 585 // don't do anything for function keys or etc.
2f024384 586 key = event.GetUnicodeKey();
63e2147c
RD
587 if (key <= 127)
588 {
589 key = event.GetKeyCode();
590 keyOk = (key <= 127);
591 }
2f024384
DS
592#else
593 key = event.GetKeyCode();
594 keyOk = (key <= 255);
595#endif
596
63e2147c 597 return keyOk;
f6bcfd97 598}
2796cce3 599
2c9a89e0
RD
600void wxGridCellEditor::StartingKey(wxKeyEvent& event)
601{
e195a54c
VZ
602 event.Skip();
603}
2c9a89e0 604
e195a54c
VZ
605void wxGridCellEditor::StartingClick()
606{
b54ba671 607}
2c9a89e0 608
3a8c693a
VZ
609#if wxUSE_TEXTCTRL
610
b54ba671
VZ
611// ----------------------------------------------------------------------------
612// wxGridCellTextEditor
613// ----------------------------------------------------------------------------
2c9a89e0 614
2796cce3
RD
615wxGridCellTextEditor::wxGridCellTextEditor()
616{
c4608a8a 617 m_maxChars = 0;
2796cce3
RD
618}
619
620void wxGridCellTextEditor::Create(wxWindow* parent,
621 wxWindowID id,
2796cce3
RD
622 wxEvtHandler* evtHandler)
623{
508011ce 624 m_control = new wxTextCtrl(parent, id, wxEmptyString,
2c9a89e0 625 wxDefaultPosition, wxDefaultSize
2796cce3 626#if defined(__WXMSW__)
d4175745 627 , wxTE_PROCESS_TAB | wxTE_AUTO_SCROLL | wxNO_BORDER
2796cce3 628#endif
508011ce 629 );
2796cce3 630
46a5010a
RD
631 // set max length allowed in the textctrl, if the parameter was set
632 if (m_maxChars != 0)
633 {
634 ((wxTextCtrl*)m_control)->SetMaxLength(m_maxChars);
635 }
c4608a8a 636
508011ce 637 wxGridCellEditor::Create(parent, id, evtHandler);
2796cce3
RD
638}
639
189d0213
VZ
640void wxGridCellTextEditor::PaintBackground(const wxRect& WXUNUSED(rectCell),
641 wxGridCellAttr * WXUNUSED(attr))
642{
a9339fe2
DS
643 // as we fill the entire client area,
644 // don't do anything here to minimize flicker
189d0213 645}
2796cce3 646
99306db2
VZ
647void wxGridCellTextEditor::SetSize(const wxRect& rectOrig)
648{
649 wxRect rect(rectOrig);
650
2f024384 651 // Make the edit control large enough to allow for internal margins
99306db2 652 //
2f024384 653 // TODO: remove this if the text ctrl sizing is improved esp. for unix
99306db2
VZ
654 //
655#if defined(__WXGTK__)
b0e282b3
RR
656 if (rect.x != 0)
657 {
658 rect.x += 1;
659 rect.y += 1;
660 rect.width -= 1;
661 rect.height -= 1;
662 }
d4175745
VZ
663#elif defined(__WXMSW__)
664 if ( rect.x == 0 )
665 rect.x += 2;
666 else
667 rect.x += 3;
84912ef8 668
d4175745
VZ
669 if ( rect.y == 0 )
670 rect.y += 2;
671 else
672 rect.y += 3;
673
674 rect.width -= 2;
675 rect.height -= 2;
a0948e27 676#else
d4175745 677 int extra_x = ( rect.x > 2 ) ? 2 : 1;
2f024384 678 int extra_y = ( rect.y > 2 ) ? 2 : 1;
a0948e27 679
d4175745
VZ
680 #if defined(__WXMOTIF__)
681 extra_x *= 2;
682 extra_y *= 2;
683 #endif
2f024384 684
cb105ad4
SN
685 rect.SetLeft( wxMax(0, rect.x - extra_x) );
686 rect.SetTop( wxMax(0, rect.y - extra_y) );
2f024384
DS
687 rect.SetRight( rect.GetRight() + 2 * extra_x );
688 rect.SetBottom( rect.GetBottom() + 2 * extra_y );
d4175745 689#endif
99306db2
VZ
690
691 wxGridCellEditor::SetSize(rect);
692}
693
3da93aae 694void wxGridCellTextEditor::BeginEdit(int row, int col, wxGrid* grid)
2796cce3 695{
2f024384 696 wxASSERT_MSG(m_control, wxT("The wxGridCellEditor must be created first!"));
2796cce3
RD
697
698 m_startValue = grid->GetTable()->GetValue(row, col);
816be743
VZ
699
700 DoBeginEdit(m_startValue);
701}
702
703void wxGridCellTextEditor::DoBeginEdit(const wxString& startValue)
704{
705 Text()->SetValue(startValue);
b54ba671 706 Text()->SetInsertionPointEnd();
2f024384 707 Text()->SetSelection(-1, -1);
b54ba671 708 Text()->SetFocus();
2796cce3
RD
709}
710
ccdee36f 711bool wxGridCellTextEditor::EndEdit(int row, int col, wxGrid* grid)
2796cce3 712{
2f024384 713 wxASSERT_MSG(m_control, wxT("The wxGridCellEditor must be created first!"));
2796cce3 714
ca65c044 715 bool changed = false;
b54ba671 716 wxString value = Text()->GetValue();
2796cce3 717 if (value != m_startValue)
ca65c044 718 changed = true;
2796cce3
RD
719
720 if (changed)
721 grid->GetTable()->SetValue(row, col, value);
2c9a89e0 722
3da93aae 723 m_startValue = wxEmptyString;
a9339fe2 724
7b519e5e
JS
725 // No point in setting the text of the hidden control
726 //Text()->SetValue(m_startValue);
2796cce3
RD
727
728 return changed;
729}
730
2796cce3
RD
731void wxGridCellTextEditor::Reset()
732{
2f024384 733 wxASSERT_MSG(m_control, wxT("The wxGridCellEditor must be created first!"));
2796cce3 734
816be743
VZ
735 DoReset(m_startValue);
736}
737
738void wxGridCellTextEditor::DoReset(const wxString& startValue)
739{
740 Text()->SetValue(startValue);
b54ba671 741 Text()->SetInsertionPointEnd();
2796cce3
RD
742}
743
f6bcfd97
BP
744bool wxGridCellTextEditor::IsAcceptedKey(wxKeyEvent& event)
745{
63e2147c 746 return wxGridCellEditor::IsAcceptedKey(event);
f6bcfd97
BP
747}
748
2c9a89e0
RD
749void wxGridCellTextEditor::StartingKey(wxKeyEvent& event)
750{
63e2147c
RD
751 // Since this is now happening in the EVT_CHAR event EmulateKeyPress is no
752 // longer an appropriate way to get the character into the text control.
753 // Do it ourselves instead. We know that if we get this far that we have
754 // a valid character, so not a whole lot of testing needs to be done.
755
756 wxTextCtrl* tc = Text();
757 wxChar ch;
758 long pos;
902725ee 759
63e2147c
RD
760#if wxUSE_UNICODE
761 ch = event.GetUnicodeKey();
762 if (ch <= 127)
6f0d2cee 763 ch = (wxChar)event.GetKeyCode();
63e2147c 764#else
6f0d2cee 765 ch = (wxChar)event.GetKeyCode();
63e2147c 766#endif
2f024384 767
63e2147c 768 switch (ch)
f6bcfd97 769 {
63e2147c
RD
770 case WXK_DELETE:
771 // delete the character at the cursor
772 pos = tc->GetInsertionPoint();
773 if (pos < tc->GetLastPosition())
2f024384 774 tc->Remove(pos, pos + 1);
63e2147c
RD
775 break;
776
777 case WXK_BACK:
778 // delete the character before the cursor
779 pos = tc->GetInsertionPoint();
780 if (pos > 0)
2f024384 781 tc->Remove(pos - 1, pos);
63e2147c
RD
782 break;
783
784 default:
785 tc->WriteText(ch);
786 break;
f6bcfd97 787 }
b54ba671 788}
2c9a89e0 789
c78b3acd 790void wxGridCellTextEditor::HandleReturn( wxKeyEvent&
0b7e6e7d 791 WXUNUSED_GTK(WXUNUSED_MOTIF(event)) )
2796cce3
RD
792{
793#if defined(__WXMOTIF__) || defined(__WXGTK__)
794 // wxMotif needs a little extra help...
6fc0f38f 795 size_t pos = (size_t)( Text()->GetInsertionPoint() );
b54ba671 796 wxString s( Text()->GetValue() );
8dd8f875 797 s = s.Left(pos) + wxT("\n") + s.Mid(pos);
b54ba671
VZ
798 Text()->SetValue(s);
799 Text()->SetInsertionPoint( pos );
2796cce3
RD
800#else
801 // the other ports can handle a Return key press
802 //
803 event.Skip();
804#endif
805}
806
c4608a8a
VZ
807void wxGridCellTextEditor::SetParameters(const wxString& params)
808{
809 if ( !params )
810 {
811 // reset to default
812 m_maxChars = 0;
813 }
814 else
815 {
816 long tmp;
c2f5b920 817 if ( params.ToLong(&tmp) )
c4608a8a 818 {
c2f5b920 819 m_maxChars = (size_t)tmp;
c4608a8a
VZ
820 }
821 else
822 {
c2f5b920 823 wxLogDebug( _T("Invalid wxGridCellTextEditor parameter string '%s' ignored"), params.c_str() );
c4608a8a
VZ
824 }
825 }
826}
827
73145b0e
JS
828// return the value in the text control
829wxString wxGridCellTextEditor::GetValue() const
830{
2f024384 831 return Text()->GetValue();
73145b0e
JS
832}
833
816be743
VZ
834// ----------------------------------------------------------------------------
835// wxGridCellNumberEditor
836// ----------------------------------------------------------------------------
837
838wxGridCellNumberEditor::wxGridCellNumberEditor(int min, int max)
839{
840 m_min = min;
841 m_max = max;
842}
843
844void wxGridCellNumberEditor::Create(wxWindow* parent,
845 wxWindowID id,
846 wxEvtHandler* evtHandler)
847{
0e871ad0 848#if wxUSE_SPINCTRL
816be743
VZ
849 if ( HasRange() )
850 {
851 // create a spin ctrl
ca65c044 852 m_control = new wxSpinCtrl(parent, wxID_ANY, wxEmptyString,
816be743
VZ
853 wxDefaultPosition, wxDefaultSize,
854 wxSP_ARROW_KEYS,
855 m_min, m_max);
856
857 wxGridCellEditor::Create(parent, id, evtHandler);
858 }
859 else
0e871ad0 860#endif
816be743
VZ
861 {
862 // just a text control
863 wxGridCellTextEditor::Create(parent, id, evtHandler);
864
865#if wxUSE_VALIDATORS
85bc0351 866 Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC));
c2f5b920 867#endif
816be743
VZ
868 }
869}
870
871void wxGridCellNumberEditor::BeginEdit(int row, int col, wxGrid* grid)
872{
873 // first get the value
874 wxGridTableBase *table = grid->GetTable();
875 if ( table->CanGetValueAs(row, col, wxGRID_VALUE_NUMBER) )
876 {
877 m_valueOld = table->GetValueAsLong(row, col);
878 }
879 else
880 {
8a60ff0a 881 m_valueOld = 0;
a5777624 882 wxString sValue = table->GetValue(row, col);
0e871ad0 883 if (! sValue.ToLong(&m_valueOld) && ! sValue.empty())
a5777624
RD
884 {
885 wxFAIL_MSG( _T("this cell doesn't have numeric value") );
886 return;
887 }
816be743
VZ
888 }
889
0e871ad0 890#if wxUSE_SPINCTRL
816be743
VZ
891 if ( HasRange() )
892 {
4a64bee4 893 Spin()->SetValue((int)m_valueOld);
f6bcfd97 894 Spin()->SetFocus();
816be743
VZ
895 }
896 else
0e871ad0 897#endif
816be743
VZ
898 {
899 DoBeginEdit(GetString());
900 }
901}
902
3324d5f5 903bool wxGridCellNumberEditor::EndEdit(int row, int col,
816be743
VZ
904 wxGrid* grid)
905{
906 bool changed;
8a60ff0a
RD
907 long value = 0;
908 wxString text;
816be743 909
0e871ad0 910#if wxUSE_SPINCTRL
816be743
VZ
911 if ( HasRange() )
912 {
913 value = Spin()->GetValue();
914 changed = value != m_valueOld;
8a60ff0a
RD
915 if (changed)
916 text = wxString::Format(wxT("%ld"), value);
816be743
VZ
917 }
918 else
0e871ad0 919#endif
816be743 920 {
8a60ff0a 921 text = Text()->GetValue();
0e871ad0 922 changed = (text.empty() || text.ToLong(&value)) && (value != m_valueOld);
816be743
VZ
923 }
924
925 if ( changed )
926 {
a5777624
RD
927 if (grid->GetTable()->CanSetValueAs(row, col, wxGRID_VALUE_NUMBER))
928 grid->GetTable()->SetValueAsLong(row, col, value);
929 else
8a60ff0a 930 grid->GetTable()->SetValue(row, col, text);
816be743
VZ
931 }
932
933 return changed;
934}
935
936void wxGridCellNumberEditor::Reset()
937{
0e871ad0 938#if wxUSE_SPINCTRL
816be743
VZ
939 if ( HasRange() )
940 {
4a64bee4 941 Spin()->SetValue((int)m_valueOld);
816be743
VZ
942 }
943 else
0e871ad0 944#endif
816be743
VZ
945 {
946 DoReset(GetString());
947 }
948}
949
f6bcfd97
BP
950bool wxGridCellNumberEditor::IsAcceptedKey(wxKeyEvent& event)
951{
952 if ( wxGridCellEditor::IsAcceptedKey(event) )
953 {
954 int keycode = event.GetKeyCode();
63e2147c
RD
955 if ( (keycode < 128) &&
956 (wxIsdigit(keycode) || keycode == '+' || keycode == '-'))
f6bcfd97 957 {
63e2147c 958 return true;
f6bcfd97
BP
959 }
960 }
961
ca65c044 962 return false;
f6bcfd97
BP
963}
964
816be743
VZ
965void wxGridCellNumberEditor::StartingKey(wxKeyEvent& event)
966{
eb5e42b6 967 int keycode = event.GetKeyCode();
816be743
VZ
968 if ( !HasRange() )
969 {
63e2147c 970 if ( wxIsdigit(keycode) || keycode == '+' || keycode == '-')
816be743
VZ
971 {
972 wxGridCellTextEditor::StartingKey(event);
973
974 // skip Skip() below
975 return;
976 }
977 }
eb5e42b6
RD
978#if wxUSE_SPINCTRL
979 else
980 {
981 if ( wxIsdigit(keycode) )
982 {
983 wxSpinCtrl* spin = (wxSpinCtrl*)m_control;
984 spin->SetValue(keycode - '0');
985 spin->SetSelection(1,1);
986 return;
987 }
988 }
989#endif
2f024384 990
816be743
VZ
991 event.Skip();
992}
9c4ba614 993
c4608a8a
VZ
994void wxGridCellNumberEditor::SetParameters(const wxString& params)
995{
996 if ( !params )
997 {
998 // reset to default
999 m_min =
1000 m_max = -1;
1001 }
1002 else
1003 {
1004 long tmp;
1005 if ( params.BeforeFirst(_T(',')).ToLong(&tmp) )
1006 {
1007 m_min = (int)tmp;
1008
1009 if ( params.AfterFirst(_T(',')).ToLong(&tmp) )
1010 {
1011 m_max = (int)tmp;
1012
1013 // skip the error message below
1014 return;
1015 }
1016 }
1017
f6bcfd97 1018 wxLogDebug(_T("Invalid wxGridCellNumberEditor parameter string '%s' ignored"), params.c_str());
c4608a8a
VZ
1019 }
1020}
1021
73145b0e
JS
1022// return the value in the spin control if it is there (the text control otherwise)
1023wxString wxGridCellNumberEditor::GetValue() const
1024{
0e871ad0
WS
1025 wxString s;
1026
1027#if wxUSE_SPINCTRL
4db6714b 1028 if ( HasRange() )
0e871ad0
WS
1029 {
1030 long value = Spin()->GetValue();
1031 s.Printf(wxT("%ld"), value);
1032 }
1033 else
1034#endif
1035 {
1036 s = Text()->GetValue();
1037 }
1038
1039 return s;
73145b0e
JS
1040}
1041
816be743
VZ
1042// ----------------------------------------------------------------------------
1043// wxGridCellFloatEditor
1044// ----------------------------------------------------------------------------
1045
f6bcfd97
BP
1046wxGridCellFloatEditor::wxGridCellFloatEditor(int width, int precision)
1047{
1048 m_width = width;
1049 m_precision = precision;
1050}
1051
816be743
VZ
1052void wxGridCellFloatEditor::Create(wxWindow* parent,
1053 wxWindowID id,
1054 wxEvtHandler* evtHandler)
1055{
1056 wxGridCellTextEditor::Create(parent, id, evtHandler);
1057
1058#if wxUSE_VALIDATORS
85bc0351 1059 Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC));
c2f5b920 1060#endif
816be743
VZ
1061}
1062
1063void wxGridCellFloatEditor::BeginEdit(int row, int col, wxGrid* grid)
1064{
1065 // first get the value
1066 wxGridTableBase *table = grid->GetTable();
1067 if ( table->CanGetValueAs(row, col, wxGRID_VALUE_FLOAT) )
1068 {
1069 m_valueOld = table->GetValueAsDouble(row, col);
1070 }
1071 else
1072 {
8a60ff0a 1073 m_valueOld = 0.0;
a5777624 1074 wxString sValue = table->GetValue(row, col);
0e871ad0 1075 if (! sValue.ToDouble(&m_valueOld) && ! sValue.empty())
a5777624
RD
1076 {
1077 wxFAIL_MSG( _T("this cell doesn't have float value") );
1078 return;
1079 }
816be743
VZ
1080 }
1081
1082 DoBeginEdit(GetString());
1083}
1084
3324d5f5 1085bool wxGridCellFloatEditor::EndEdit(int row, int col,
816be743
VZ
1086 wxGrid* grid)
1087{
8a60ff0a
RD
1088 double value = 0.0;
1089 wxString text(Text()->GetValue());
1090
c77a6796
VZ
1091 if ( (text.empty() || text.ToDouble(&value)) &&
1092 !wxIsSameDouble(value, m_valueOld) )
816be743 1093 {
a5777624
RD
1094 if (grid->GetTable()->CanSetValueAs(row, col, wxGRID_VALUE_FLOAT))
1095 grid->GetTable()->SetValueAsDouble(row, col, value);
1096 else
8a60ff0a 1097 grid->GetTable()->SetValue(row, col, text);
816be743 1098
ca65c044 1099 return true;
816be743 1100 }
2f024384 1101
ca65c044 1102 return false;
816be743
VZ
1103}
1104
1105void wxGridCellFloatEditor::Reset()
1106{
1107 DoReset(GetString());
1108}
1109
1110void wxGridCellFloatEditor::StartingKey(wxKeyEvent& event)
1111{
12a3f227 1112 int keycode = event.GetKeyCode();
3fe73755
SN
1113 char tmpbuf[2];
1114 tmpbuf[0] = (char) keycode;
1115 tmpbuf[1] = '\0';
42841dfc 1116 wxString strbuf(tmpbuf, *wxConvCurrent);
2f024384 1117
902725ee 1118#if wxUSE_INTL
42841dfc 1119 bool is_decimal_point = ( strbuf ==
63e2147c 1120 wxLocale::GetInfo(wxLOCALE_DECIMAL_POINT, wxLOCALE_CAT_NUMBER) );
5335e9c4
MW
1121#else
1122 bool is_decimal_point = ( strbuf == _T(".") );
1123#endif
2f024384 1124
63e2147c
RD
1125 if ( wxIsdigit(keycode) || keycode == '+' || keycode == '-'
1126 || is_decimal_point )
816be743
VZ
1127 {
1128 wxGridCellTextEditor::StartingKey(event);
1129
1130 // skip Skip() below
1131 return;
1132 }
1133
1134 event.Skip();
1135}
1136
f6bcfd97
BP
1137void wxGridCellFloatEditor::SetParameters(const wxString& params)
1138{
1139 if ( !params )
1140 {
1141 // reset to default
1142 m_width =
1143 m_precision = -1;
1144 }
1145 else
1146 {
1147 long tmp;
1148 if ( params.BeforeFirst(_T(',')).ToLong(&tmp) )
1149 {
1150 m_width = (int)tmp;
1151
1152 if ( params.AfterFirst(_T(',')).ToLong(&tmp) )
1153 {
1154 m_precision = (int)tmp;
1155
1156 // skip the error message below
1157 return;
1158 }
1159 }
1160
1161 wxLogDebug(_T("Invalid wxGridCellFloatEditor parameter string '%s' ignored"), params.c_str());
1162 }
1163}
1164
1165wxString wxGridCellFloatEditor::GetString() const
1166{
1167 wxString fmt;
fe4cb4f5 1168 if ( m_precision == -1 && m_width != -1)
f6bcfd97
BP
1169 {
1170 // default precision
ec53826c 1171 fmt.Printf(_T("%%%d.f"), m_width);
f6bcfd97 1172 }
fe4cb4f5
JS
1173 else if ( m_precision != -1 && m_width == -1)
1174 {
1175 // default width
1176 fmt.Printf(_T("%%.%df"), m_precision);
1177 }
1178 else if ( m_precision != -1 && m_width != -1 )
f6bcfd97 1179 {
ec53826c 1180 fmt.Printf(_T("%%%d.%df"), m_width, m_precision);
f6bcfd97 1181 }
fe4cb4f5
JS
1182 else
1183 {
1184 // default width/precision
1185 fmt = _T("%f");
1186 }
f6bcfd97
BP
1187
1188 return wxString::Format(fmt, m_valueOld);
1189}
1190
1191bool wxGridCellFloatEditor::IsAcceptedKey(wxKeyEvent& event)
1192{
1193 if ( wxGridCellEditor::IsAcceptedKey(event) )
1194 {
7b34da9b
VZ
1195 const int keycode = event.GetKeyCode();
1196 if ( isascii(keycode) )
1197 {
1198 char tmpbuf[2];
1199 tmpbuf[0] = (char) keycode;
1200 tmpbuf[1] = '\0';
1201 wxString strbuf(tmpbuf, *wxConvCurrent);
2f024384 1202
902725ee 1203#if wxUSE_INTL
7b34da9b
VZ
1204 const wxString decimalPoint =
1205 wxLocale::GetInfo(wxLOCALE_DECIMAL_POINT, wxLOCALE_CAT_NUMBER);
5335e9c4 1206#else
7b34da9b 1207 const wxString decimalPoint(_T('.'));
5335e9c4 1208#endif
2f024384 1209
7b34da9b
VZ
1210 // accept digits, 'e' as in '1e+6', also '-', '+', and '.'
1211 if ( wxIsdigit(keycode) ||
1212 tolower(keycode) == 'e' ||
1213 keycode == decimalPoint ||
1214 keycode == '+' ||
1215 keycode == '-' )
1216 {
1217 return true;
1218 }
2f024384 1219 }
f6bcfd97
BP
1220 }
1221
ca65c044 1222 return false;
f6bcfd97
BP
1223}
1224
3a8c693a
VZ
1225#endif // wxUSE_TEXTCTRL
1226
1227#if wxUSE_CHECKBOX
1228
508011ce
VZ
1229// ----------------------------------------------------------------------------
1230// wxGridCellBoolEditor
1231// ----------------------------------------------------------------------------
1232
1233void wxGridCellBoolEditor::Create(wxWindow* parent,
1234 wxWindowID id,
1235 wxEvtHandler* evtHandler)
1236{
1237 m_control = new wxCheckBox(parent, id, wxEmptyString,
1238 wxDefaultPosition, wxDefaultSize,
1239 wxNO_BORDER);
1240
1241 wxGridCellEditor::Create(parent, id, evtHandler);
1242}
1243
1244void wxGridCellBoolEditor::SetSize(const wxRect& r)
1245{
ca65c044 1246 bool resize = false;
b94ae1ea
VZ
1247 wxSize size = m_control->GetSize();
1248 wxCoord minSize = wxMin(r.width, r.height);
1249
1250 // check if the checkbox is not too big/small for this cell
1251 wxSize sizeBest = m_control->GetBestSize();
1252 if ( !(size == sizeBest) )
1253 {
1254 // reset to default size if it had been made smaller
1255 size = sizeBest;
1256
ca65c044 1257 resize = true;
b94ae1ea
VZ
1258 }
1259
1260 if ( size.x >= minSize || size.y >= minSize )
1261 {
1262 // leave 1 pixel margin
1263 size.x = size.y = minSize - 2;
1264
ca65c044 1265 resize = true;
b94ae1ea
VZ
1266 }
1267
1268 if ( resize )
1269 {
1270 m_control->SetSize(size);
1271 }
1272
508011ce 1273 // position it in the centre of the rectangle (TODO: support alignment?)
508011ce 1274
b94ae1ea 1275#if defined(__WXGTK__) || defined (__WXMOTIF__)
508011ce
VZ
1276 // the checkbox without label still has some space to the right in wxGTK,
1277 // so shift it to the right
b94ae1ea
VZ
1278 size.x -= 8;
1279#elif defined(__WXMSW__)
a95e38c0
VZ
1280 // here too, but in other way
1281 size.x += 1;
b94ae1ea
VZ
1282 size.y -= 2;
1283#endif
508011ce 1284
1bd71df9
JS
1285 int hAlign = wxALIGN_CENTRE;
1286 int vAlign = wxALIGN_CENTRE;
1287 if (GetCellAttr())
1288 GetCellAttr()->GetAlignment(& hAlign, & vAlign);
52d6f640 1289
1bd71df9
JS
1290 int x = 0, y = 0;
1291 if (hAlign == wxALIGN_LEFT)
1292 {
1293 x = r.x + 2;
2f024384 1294
1bd71df9
JS
1295#ifdef __WXMSW__
1296 x += 2;
52d6f640 1297#endif
2f024384
DS
1298
1299 y = r.y + r.height / 2 - size.y / 2;
1bd71df9
JS
1300 }
1301 else if (hAlign == wxALIGN_RIGHT)
1302 {
1303 x = r.x + r.width - size.x - 2;
2f024384 1304 y = r.y + r.height / 2 - size.y / 2;
1bd71df9
JS
1305 }
1306 else if (hAlign == wxALIGN_CENTRE)
1307 {
2f024384
DS
1308 x = r.x + r.width / 2 - size.x / 2;
1309 y = r.y + r.height / 2 - size.y / 2;
1bd71df9 1310 }
52d6f640 1311
1bd71df9 1312 m_control->Move(x, y);
508011ce
VZ
1313}
1314
1315void wxGridCellBoolEditor::Show(bool show, wxGridCellAttr *attr)
1316{
99306db2
VZ
1317 m_control->Show(show);
1318
189d0213 1319 if ( show )
508011ce 1320 {
189d0213
VZ
1321 wxColour colBg = attr ? attr->GetBackgroundColour() : *wxLIGHT_GREY;
1322 CBox()->SetBackgroundColour(colBg);
508011ce 1323 }
508011ce
VZ
1324}
1325
1326void wxGridCellBoolEditor::BeginEdit(int row, int col, wxGrid* grid)
1327{
1328 wxASSERT_MSG(m_control,
c2f5b920 1329 wxT("The wxGridCellEditor must be created first!"));
508011ce 1330
28a77bc4 1331 if (grid->GetTable()->CanGetValueAs(row, col, wxGRID_VALUE_BOOL))
2f024384 1332 {
f2d76237 1333 m_startValue = grid->GetTable()->GetValueAsBool(row, col);
2f024384 1334 }
f2d76237 1335 else
695a3263
MB
1336 {
1337 wxString cellval( grid->GetTable()->GetValue(row, col) );
8dd8f875 1338 m_startValue = !( !cellval || (cellval == wxT("0")) );
695a3263 1339 }
2f024384 1340
508011ce
VZ
1341 CBox()->SetValue(m_startValue);
1342 CBox()->SetFocus();
1343}
1344
1345bool wxGridCellBoolEditor::EndEdit(int row, int col,
508011ce
VZ
1346 wxGrid* grid)
1347{
1348 wxASSERT_MSG(m_control,
c2f5b920 1349 wxT("The wxGridCellEditor must be created first!"));
508011ce 1350
ca65c044 1351 bool changed = false;
508011ce
VZ
1352 bool value = CBox()->GetValue();
1353 if ( value != m_startValue )
ca65c044 1354 changed = true;
508011ce
VZ
1355
1356 if ( changed )
1357 {
28a77bc4 1358 if (grid->GetTable()->CanGetValueAs(row, col, wxGRID_VALUE_BOOL))
f2d76237
RD
1359 grid->GetTable()->SetValueAsBool(row, col, value);
1360 else
1361 grid->GetTable()->SetValue(row, col, value ? _T("1") : wxEmptyString);
508011ce
VZ
1362 }
1363
1364 return changed;
1365}
1366
1367void wxGridCellBoolEditor::Reset()
1368{
1369 wxASSERT_MSG(m_control,
c2f5b920 1370 wxT("The wxGridCellEditor must be created first!"));
508011ce
VZ
1371
1372 CBox()->SetValue(m_startValue);
1373}
1374
e195a54c 1375void wxGridCellBoolEditor::StartingClick()
508011ce 1376{
e195a54c 1377 CBox()->SetValue(!CBox()->GetValue());
508011ce
VZ
1378}
1379
f6bcfd97
BP
1380bool wxGridCellBoolEditor::IsAcceptedKey(wxKeyEvent& event)
1381{
1382 if ( wxGridCellEditor::IsAcceptedKey(event) )
1383 {
1384 int keycode = event.GetKeyCode();
1385 switch ( keycode )
1386 {
f6bcfd97
BP
1387 case WXK_SPACE:
1388 case '+':
1389 case '-':
ca65c044 1390 return true;
f6bcfd97
BP
1391 }
1392 }
1393
ca65c044 1394 return false;
f6bcfd97 1395}
04418332 1396
63e2147c
RD
1397void wxGridCellBoolEditor::StartingKey(wxKeyEvent& event)
1398{
1399 int keycode = event.GetKeyCode();
1400 switch ( keycode )
1401 {
1402 case WXK_SPACE:
1403 CBox()->SetValue(!CBox()->GetValue());
1404 break;
902725ee 1405
63e2147c
RD
1406 case '+':
1407 CBox()->SetValue(true);
1408 break;
902725ee 1409
63e2147c
RD
1410 case '-':
1411 CBox()->SetValue(false);
1412 break;
1413 }
1414}
1415
1416
73145b0e
JS
1417// return the value as "1" for true and the empty string for false
1418wxString wxGridCellBoolEditor::GetValue() const
1419{
1420 bool bSet = CBox()->GetValue();
04418332 1421 return bSet ? _T("1") : wxEmptyString;
73145b0e 1422}
f6bcfd97 1423
3a8c693a
VZ
1424#endif // wxUSE_CHECKBOX
1425
1426#if wxUSE_COMBOBOX
1427
4ee5fc9c
VZ
1428// ----------------------------------------------------------------------------
1429// wxGridCellChoiceEditor
1430// ----------------------------------------------------------------------------
1431
7db33cc3
MB
1432wxGridCellChoiceEditor::wxGridCellChoiceEditor(const wxArrayString& choices,
1433 bool allowOthers)
1434 : m_choices(choices),
1435 m_allowOthers(allowOthers) { }
1436
4ee5fc9c 1437wxGridCellChoiceEditor::wxGridCellChoiceEditor(size_t count,
f6bcfd97 1438 const wxString choices[],
4ee5fc9c
VZ
1439 bool allowOthers)
1440 : m_allowOthers(allowOthers)
1441{
c4608a8a 1442 if ( count )
4ee5fc9c 1443 {
c4608a8a
VZ
1444 m_choices.Alloc(count);
1445 for ( size_t n = 0; n < count; n++ )
1446 {
1447 m_choices.Add(choices[n]);
1448 }
4ee5fc9c
VZ
1449 }
1450}
1451
c4608a8a
VZ
1452wxGridCellEditor *wxGridCellChoiceEditor::Clone() const
1453{
1454 wxGridCellChoiceEditor *editor = new wxGridCellChoiceEditor;
1455 editor->m_allowOthers = m_allowOthers;
1456 editor->m_choices = m_choices;
1457
1458 return editor;
1459}
1460
4ee5fc9c
VZ
1461void wxGridCellChoiceEditor::Create(wxWindow* parent,
1462 wxWindowID id,
1463 wxEvtHandler* evtHandler)
1464{
4ee5fc9c
VZ
1465 m_control = new wxComboBox(parent, id, wxEmptyString,
1466 wxDefaultPosition, wxDefaultSize,
584ad2a3 1467 m_choices,
4ee5fc9c
VZ
1468 m_allowOthers ? 0 : wxCB_READONLY);
1469
4ee5fc9c
VZ
1470 wxGridCellEditor::Create(parent, id, evtHandler);
1471}
1472
a5777624
RD
1473void wxGridCellChoiceEditor::PaintBackground(const wxRect& rectCell,
1474 wxGridCellAttr * attr)
4ee5fc9c
VZ
1475{
1476 // as we fill the entire client area, don't do anything here to minimize
1477 // flicker
a5777624
RD
1478
1479 // TODO: It doesn't actually fill the client area since the height of a
c2f5b920
DS
1480 // combo always defaults to the standard. Until someone has time to
1481 // figure out the right rectangle to paint, just do it the normal way.
a5777624 1482 wxGridCellEditor::PaintBackground(rectCell, attr);
4ee5fc9c
VZ
1483}
1484
1485void wxGridCellChoiceEditor::BeginEdit(int row, int col, wxGrid* grid)
1486{
1487 wxASSERT_MSG(m_control,
c2f5b920 1488 wxT("The wxGridCellEditor must be created first!"));
4ee5fc9c 1489
08dd04d0
JS
1490 wxGridCellEditorEvtHandler* evtHandler = NULL;
1491 if (m_control)
1492 evtHandler = wxDynamicCast(m_control->GetEventHandler(), wxGridCellEditorEvtHandler);
1493
1494 // Don't immediately end if we get a kill focus event within BeginEdit
1495 if (evtHandler)
1496 evtHandler->SetInSetFocus(true);
1497
4ee5fc9c
VZ
1498 m_startValue = grid->GetTable()->GetValue(row, col);
1499
2b5f62a0 1500 if (m_allowOthers)
2f024384 1501 {
2b5f62a0 1502 Combo()->SetValue(m_startValue);
2f024384 1503 }
2b5f62a0 1504 else
28a77bc4 1505 {
2b5f62a0
VZ
1506 // find the right position, or default to the first if not found
1507 int pos = Combo()->FindString(m_startValue);
902725ee 1508 if (pos == wxNOT_FOUND)
2b5f62a0
VZ
1509 pos = 0;
1510 Combo()->SetSelection(pos);
28a77bc4 1511 }
2f024384 1512
4ee5fc9c
VZ
1513 Combo()->SetInsertionPointEnd();
1514 Combo()->SetFocus();
08dd04d0
JS
1515
1516 if (evtHandler)
46cbb21e
JS
1517 {
1518 // When dropping down the menu, a kill focus event
1519 // happens after this point, so we can't reset the flag yet.
1520#if !defined(__WXGTK20__)
08dd04d0 1521 evtHandler->SetInSetFocus(false);
46cbb21e
JS
1522#endif
1523 }
4ee5fc9c
VZ
1524}
1525
28a77bc4 1526bool wxGridCellChoiceEditor::EndEdit(int row, int col,
4ee5fc9c
VZ
1527 wxGrid* grid)
1528{
1529 wxString value = Combo()->GetValue();
faffacec
VZ
1530 if ( value == m_startValue )
1531 return false;
4ee5fc9c 1532
faffacec 1533 grid->GetTable()->SetValue(row, col, value);
4ee5fc9c 1534
faffacec 1535 return true;
4ee5fc9c
VZ
1536}
1537
1538void wxGridCellChoiceEditor::Reset()
1539{
1540 Combo()->SetValue(m_startValue);
1541 Combo()->SetInsertionPointEnd();
1542}
1543
c4608a8a
VZ
1544void wxGridCellChoiceEditor::SetParameters(const wxString& params)
1545{
1546 if ( !params )
1547 {
1548 // what can we do?
1549 return;
1550 }
1551
1552 m_choices.Empty();
1553
1554 wxStringTokenizer tk(params, _T(','));
1555 while ( tk.HasMoreTokens() )
1556 {
1557 m_choices.Add(tk.GetNextToken());
1558 }
1559}
1560
73145b0e
JS
1561// return the value in the text control
1562wxString wxGridCellChoiceEditor::GetValue() const
1563{
1564 return Combo()->GetValue();
1565}
04418332 1566
3a8c693a
VZ
1567#endif // wxUSE_COMBOBOX
1568
508011ce
VZ
1569// ----------------------------------------------------------------------------
1570// wxGridCellEditorEvtHandler
1571// ----------------------------------------------------------------------------
2796cce3 1572
140954fd
VZ
1573void wxGridCellEditorEvtHandler::OnKillFocus(wxFocusEvent& event)
1574{
08dd04d0
JS
1575 // Don't disable the cell if we're just starting to edit it
1576 if (m_inSetFocus)
1577 return;
1578
140954fd
VZ
1579 // accept changes
1580 m_grid->DisableCellEditControl();
1581
1582 event.Skip();
1583}
1584
2796cce3
RD
1585void wxGridCellEditorEvtHandler::OnKeyDown(wxKeyEvent& event)
1586{
12a3f227 1587 switch ( event.GetKeyCode() )
2796cce3
RD
1588 {
1589 case WXK_ESCAPE:
1590 m_editor->Reset();
b54ba671 1591 m_grid->DisableCellEditControl();
2796cce3
RD
1592 break;
1593
2c9a89e0 1594 case WXK_TAB:
b51c3f27 1595 m_grid->GetEventHandler()->ProcessEvent( event );
9b4aede2
RD
1596 break;
1597
2796cce3 1598 case WXK_RETURN:
faec5a43
SN
1599 case WXK_NUMPAD_ENTER:
1600 if (!m_grid->GetEventHandler()->ProcessEvent(event))
2796cce3
RD
1601 m_editor->HandleReturn(event);
1602 break;
1603
2796cce3
RD
1604 default:
1605 event.Skip();
2f024384 1606 break;
2796cce3
RD
1607 }
1608}
1609
fb0de762
RD
1610void wxGridCellEditorEvtHandler::OnChar(wxKeyEvent& event)
1611{
7db713ae
JS
1612 int row = m_grid->GetGridCursorRow();
1613 int col = m_grid->GetGridCursorCol();
1614 wxRect rect = m_grid->CellToRect( row, col );
1615 int cw, ch;
1616 m_grid->GetGridWindow()->GetClientSize( &cw, &ch );
2f024384 1617
7db713ae
JS
1618 // if cell width is smaller than grid client area, cell is wholly visible
1619 bool wholeCellVisible = (rect.GetWidth() < cw);
1620
12a3f227 1621 switch ( event.GetKeyCode() )
fb0de762
RD
1622 {
1623 case WXK_ESCAPE:
1624 case WXK_TAB:
1625 case WXK_RETURN:
a4f7bf58 1626 case WXK_NUMPAD_ENTER:
fb0de762
RD
1627 break;
1628
7db713ae
JS
1629 case WXK_HOME:
1630 {
2f024384 1631 if ( wholeCellVisible )
7db713ae
JS
1632 {
1633 // no special processing needed...
1634 event.Skip();
1635 break;
1636 }
1637
1638 // do special processing for partly visible cell...
1639
1640 // get the widths of all cells previous to this one
1641 int colXPos = 0;
faa94f3e 1642 for ( int i = 0; i < col; i++ )
7db713ae
JS
1643 {
1644 colXPos += m_grid->GetColSize(i);
1645 }
1646
1647 int xUnit = 1, yUnit = 1;
1648 m_grid->GetScrollPixelsPerUnit(&xUnit, &yUnit);
1649 if (col != 0)
1650 {
2f024384 1651 m_grid->Scroll(colXPos / xUnit - 1, m_grid->GetScrollPos(wxVERTICAL));
7db713ae
JS
1652 }
1653 else
1654 {
2f024384 1655 m_grid->Scroll(colXPos / xUnit, m_grid->GetScrollPos(wxVERTICAL));
7db713ae
JS
1656 }
1657 event.Skip();
1658 break;
1659 }
c2f5b920 1660
7db713ae
JS
1661 case WXK_END:
1662 {
2f024384 1663 if ( wholeCellVisible )
7db713ae
JS
1664 {
1665 // no special processing needed...
1666 event.Skip();
1667 break;
1668 }
1669
1670 // do special processing for partly visible cell...
1671
1672 int textWidth = 0;
1673 wxString value = m_grid->GetCellValue(row, col);
1674 if ( wxEmptyString != value )
1675 {
1676 // get width of cell CONTENTS (text)
1677 int y;
1678 wxFont font = m_grid->GetCellFont(row, col);
1679 m_grid->GetTextExtent(value, &textWidth, &y, NULL, NULL, &font);
2f024384 1680
7db713ae
JS
1681 // try to RIGHT align the text by scrolling
1682 int client_right = m_grid->GetGridWindow()->GetClientSize().GetWidth();
2f024384 1683
7db713ae
JS
1684 // (m_grid->GetScrollLineX()*2) is a factor for not scrolling to far,
1685 // otherwise the last part of the cell content might be hidden below the scroll bar
1686 // FIXME: maybe there is a more suitable correction?
2f024384 1687 textWidth -= (client_right - (m_grid->GetScrollLineX() * 2));
7db713ae
JS
1688 if ( textWidth < 0 )
1689 {
1690 textWidth = 0;
1691 }
1692 }
1693
1694 // get the widths of all cells previous to this one
1695 int colXPos = 0;
faa94f3e 1696 for ( int i = 0; i < col; i++ )
7db713ae
JS
1697 {
1698 colXPos += m_grid->GetColSize(i);
1699 }
2f024384 1700
7db713ae
JS
1701 // and add the (modified) text width of the cell contents
1702 // as we'd like to see the last part of the cell contents
1703 colXPos += textWidth;
1704
1705 int xUnit = 1, yUnit = 1;
1706 m_grid->GetScrollPixelsPerUnit(&xUnit, &yUnit);
ccdee36f 1707 m_grid->Scroll(colXPos / xUnit - 1, m_grid->GetScrollPos(wxVERTICAL));
7db713ae
JS
1708 event.Skip();
1709 break;
1710 }
1711
fb0de762
RD
1712 default:
1713 event.Skip();
c2f5b920 1714 break;
fb0de762
RD
1715 }
1716}
1717
c4608a8a
VZ
1718// ----------------------------------------------------------------------------
1719// wxGridCellWorker is an (almost) empty common base class for
1720// wxGridCellRenderer and wxGridCellEditor managing ref counting
1721// ----------------------------------------------------------------------------
1722
1723void wxGridCellWorker::SetParameters(const wxString& WXUNUSED(params))
1724{
1725 // nothing to do
1726}
1727
1728wxGridCellWorker::~wxGridCellWorker()
1729{
1730}
1731
508011ce
VZ
1732// ============================================================================
1733// renderer classes
1734// ============================================================================
1735
ab79958a
VZ
1736// ----------------------------------------------------------------------------
1737// wxGridCellRenderer
1738// ----------------------------------------------------------------------------
1739
1740void wxGridCellRenderer::Draw(wxGrid& grid,
2796cce3 1741 wxGridCellAttr& attr,
ab79958a
VZ
1742 wxDC& dc,
1743 const wxRect& rect,
c78b3acd 1744 int WXUNUSED(row), int WXUNUSED(col),
ab79958a
VZ
1745 bool isSelected)
1746{
1747 dc.SetBackgroundMode( wxSOLID );
1748
04418332 1749 // grey out fields if the grid is disabled
4db6714b 1750 if ( grid.IsEnabled() )
ab79958a 1751 {
ec157c8f
WS
1752 if ( isSelected )
1753 {
1754 dc.SetBrush( wxBrush(grid.GetSelectionBackground(), wxSOLID) );
1755 }
1756 else
1757 {
1758 dc.SetBrush( wxBrush(attr.GetBackgroundColour(), wxSOLID) );
1759 }
52d6f640 1760 }
ab79958a
VZ
1761 else
1762 {
ec157c8f 1763 dc.SetBrush(wxBrush(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE), wxSOLID));
ab79958a
VZ
1764 }
1765
1766 dc.SetPen( *wxTRANSPARENT_PEN );
ab79958a
VZ
1767 dc.DrawRectangle(rect);
1768}
1769
508011ce
VZ
1770// ----------------------------------------------------------------------------
1771// wxGridCellStringRenderer
1772// ----------------------------------------------------------------------------
1773
fbfb8bcc
VZ
1774void wxGridCellStringRenderer::SetTextColoursAndFont(const wxGrid& grid,
1775 const wxGridCellAttr& attr,
816be743
VZ
1776 wxDC& dc,
1777 bool isSelected)
ab79958a 1778{
ab79958a
VZ
1779 dc.SetBackgroundMode( wxTRANSPARENT );
1780
283b7808
VZ
1781 // TODO some special colours for attr.IsReadOnly() case?
1782
04418332 1783 // different coloured text when the grid is disabled
4db6714b 1784 if ( grid.IsEnabled() )
ab79958a 1785 {
c2f5b920
DS
1786 if ( isSelected )
1787 {
1788 dc.SetTextBackground( grid.GetSelectionBackground() );
1789 dc.SetTextForeground( grid.GetSelectionForeground() );
1790 }
1791 else
1792 {
1793 dc.SetTextBackground( attr.GetBackgroundColour() );
1794 dc.SetTextForeground( attr.GetTextColour() );
1795 }
ab79958a
VZ
1796 }
1797 else
1798 {
c2f5b920
DS
1799 dc.SetTextBackground(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE));
1800 dc.SetTextForeground(wxSystemSettings::GetColour(wxSYS_COLOUR_GRAYTEXT));
ab79958a 1801 }
816be743 1802
2796cce3 1803 dc.SetFont( attr.GetFont() );
816be743
VZ
1804}
1805
fbfb8bcc 1806wxSize wxGridCellStringRenderer::DoGetBestSize(const wxGridCellAttr& attr,
65e4e78e
VZ
1807 wxDC& dc,
1808 const wxString& text)
1809{
f6bcfd97 1810 wxCoord x = 0, y = 0, max_x = 0;
65e4e78e 1811 dc.SetFont(attr.GetFont());
f6bcfd97
BP
1812 wxStringTokenizer tk(text, _T('\n'));
1813 while ( tk.HasMoreTokens() )
1814 {
1815 dc.GetTextExtent(tk.GetNextToken(), &x, &y);
1816 max_x = wxMax(max_x, x);
1817 }
1818
1819 y *= 1 + text.Freq(wxT('\n')); // multiply by the number of lines.
65e4e78e 1820
f6bcfd97 1821 return wxSize(max_x, y);
65e4e78e
VZ
1822}
1823
1824wxSize wxGridCellStringRenderer::GetBestSize(wxGrid& grid,
1825 wxGridCellAttr& attr,
1826 wxDC& dc,
1827 int row, int col)
1828{
1829 return DoGetBestSize(attr, dc, grid.GetCellValue(row, col));
1830}
1831
816be743
VZ
1832void wxGridCellStringRenderer::Draw(wxGrid& grid,
1833 wxGridCellAttr& attr,
1834 wxDC& dc,
1835 const wxRect& rectCell,
1836 int row, int col,
1837 bool isSelected)
1838{
27f35b66 1839 wxRect rect = rectCell;
dc1f566f
SN
1840 rect.Inflate(-1);
1841
1842 // erase only this cells background, overflow cells should have been erased
1843 wxGridCellRenderer::Draw(grid, attr, dc, rectCell, row, col, isSelected);
1844
1845 int hAlign, vAlign;
1846 attr.GetAlignment(&hAlign, &vAlign);
27f35b66 1847
39b80349
SN
1848 int overflowCols = 0;
1849
27f35b66
SN
1850 if (attr.GetOverflow())
1851 {
1852 int cols = grid.GetNumberCols();
1853 int best_width = GetBestSize(grid,attr,dc,row,col).GetWidth();
1854 int cell_rows, cell_cols;
2f024384 1855 attr.GetSize( &cell_rows, &cell_cols ); // shouldn't get here if <= 0
27f35b66
SN
1856 if ((best_width > rectCell.width) && (col < cols) && grid.GetTable())
1857 {
1858 int i, c_cols, c_rows;
1859 for (i = col+cell_cols; i < cols; i++)
1860 {
ca65c044 1861 bool is_empty = true;
2f024384 1862 for (int j=row; j < row + cell_rows; j++)
27f35b66 1863 {
39b80349 1864 // check w/ anchor cell for multicell block
ef4d6ce8 1865 grid.GetCellSize(j, i, &c_rows, &c_cols);
2f024384
DS
1866 if (c_rows > 0)
1867 c_rows = 0;
1868 if (!grid.GetTable()->IsEmptyCell(j + c_rows, i))
39b80349 1869 {
ca65c044 1870 is_empty = false;
ef4d6ce8 1871 break;
39b80349 1872 }
27f35b66 1873 }
2f024384 1874
39b80349 1875 if (is_empty)
c2f5b920 1876 {
39b80349 1877 rect.width += grid.GetColSize(i);
c2f5b920 1878 }
dc1f566f
SN
1879 else
1880 {
1881 i--;
1882 break;
1883 }
2f024384 1884
4db6714b
KH
1885 if (rect.width >= best_width)
1886 break;
2b5f62a0 1887 }
2f024384 1888
39b80349 1889 overflowCols = i - col - cell_cols + 1;
4db6714b
KH
1890 if (overflowCols >= cols)
1891 overflowCols = cols - 1;
39b80349 1892 }
27f35b66 1893
dc1f566f
SN
1894 if (overflowCols > 0) // redraw overflow cells w/ proper hilight
1895 {
39b80349 1896 hAlign = wxALIGN_LEFT; // if oveflowed then it's left aligned
2b5f62a0 1897 wxRect clip = rect;
39b80349 1898 clip.x += rectCell.width;
dc1f566f 1899 // draw each overflow cell individually
c2f5b920 1900 int col_end = col + cell_cols + overflowCols;
dc1f566f 1901 if (col_end >= grid.GetNumberCols())
2b5f62a0 1902 col_end = grid.GetNumberCols() - 1;
c2f5b920 1903 for (int i = col + cell_cols; i <= col_end; i++)
dc1f566f 1904 {
dc1f566f
SN
1905 clip.width = grid.GetColSize(i) - 1;
1906 dc.DestroyClippingRegion();
1907 dc.SetClippingRegion(clip);
39b80349
SN
1908
1909 SetTextColoursAndFont(grid, attr, dc,
2b5f62a0 1910 grid.IsInSelection(row,i));
39b80349 1911
dc1f566f 1912 grid.DrawTextRectangle(dc, grid.GetCellValue(row, col),
2b5f62a0 1913 rect, hAlign, vAlign);
39b80349 1914 clip.x += grid.GetColSize(i) - 1;
dc1f566f 1915 }
ab79958a 1916
39b80349 1917 rect = rectCell;
2b5f62a0 1918 rect.Inflate(-1);
dc1f566f 1919 rect.width++;
39b80349 1920 dc.DestroyClippingRegion();
dc1f566f 1921 }
39b80349 1922 }
ab79958a 1923
dc1f566f
SN
1924 // now we only have to draw the text
1925 SetTextColoursAndFont(grid, attr, dc, isSelected);
39b80349 1926
ab79958a
VZ
1927 grid.DrawTextRectangle(dc, grid.GetCellValue(row, col),
1928 rect, hAlign, vAlign);
1929}
1930
65e4e78e
VZ
1931// ----------------------------------------------------------------------------
1932// wxGridCellNumberRenderer
1933// ----------------------------------------------------------------------------
1934
fbfb8bcc 1935wxString wxGridCellNumberRenderer::GetString(const wxGrid& grid, int row, int col)
65e4e78e
VZ
1936{
1937 wxGridTableBase *table = grid.GetTable();
1938 wxString text;
1939 if ( table->CanGetValueAs(row, col, wxGRID_VALUE_NUMBER) )
1940 {
1941 text.Printf(_T("%ld"), table->GetValueAsLong(row, col));
1942 }
9c4ba614
VZ
1943 else
1944 {
1945 text = table->GetValue(row, col);
1946 }
65e4e78e
VZ
1947
1948 return text;
1949}
1950
816be743
VZ
1951void wxGridCellNumberRenderer::Draw(wxGrid& grid,
1952 wxGridCellAttr& attr,
1953 wxDC& dc,
1954 const wxRect& rectCell,
1955 int row, int col,
1956 bool isSelected)
1957{
1958 wxGridCellRenderer::Draw(grid, attr, dc, rectCell, row, col, isSelected);
1959
1960 SetTextColoursAndFont(grid, attr, dc, isSelected);
1961
1962 // draw the text right aligned by default
1963 int hAlign, vAlign;
1964 attr.GetAlignment(&hAlign, &vAlign);
4c7277db 1965 hAlign = wxALIGN_RIGHT;
816be743
VZ
1966
1967 wxRect rect = rectCell;
1968 rect.Inflate(-1);
1969
65e4e78e
VZ
1970 grid.DrawTextRectangle(dc, GetString(grid, row, col), rect, hAlign, vAlign);
1971}
816be743 1972
65e4e78e
VZ
1973wxSize wxGridCellNumberRenderer::GetBestSize(wxGrid& grid,
1974 wxGridCellAttr& attr,
1975 wxDC& dc,
1976 int row, int col)
1977{
1978 return DoGetBestSize(attr, dc, GetString(grid, row, col));
816be743
VZ
1979}
1980
1981// ----------------------------------------------------------------------------
1982// wxGridCellFloatRenderer
1983// ----------------------------------------------------------------------------
1984
1985wxGridCellFloatRenderer::wxGridCellFloatRenderer(int width, int precision)
1986{
1987 SetWidth(width);
1988 SetPrecision(precision);
1989}
1990
e72b4213
VZ
1991wxGridCellRenderer *wxGridCellFloatRenderer::Clone() const
1992{
1993 wxGridCellFloatRenderer *renderer = new wxGridCellFloatRenderer;
1994 renderer->m_width = m_width;
1995 renderer->m_precision = m_precision;
1996 renderer->m_format = m_format;
1997
1998 return renderer;
1999}
2000
fbfb8bcc 2001wxString wxGridCellFloatRenderer::GetString(const wxGrid& grid, int row, int col)
65e4e78e
VZ
2002{
2003 wxGridTableBase *table = grid.GetTable();
0b190b0f
VZ
2004
2005 bool hasDouble;
2006 double val;
65e4e78e
VZ
2007 wxString text;
2008 if ( table->CanGetValueAs(row, col, wxGRID_VALUE_FLOAT) )
2009 {
0b190b0f 2010 val = table->GetValueAsDouble(row, col);
ca65c044 2011 hasDouble = true;
65e4e78e 2012 }
9c4ba614
VZ
2013 else
2014 {
2015 text = table->GetValue(row, col);
0b190b0f 2016 hasDouble = text.ToDouble(&val);
9c4ba614 2017 }
65e4e78e 2018
0b190b0f
VZ
2019 if ( hasDouble )
2020 {
2021 if ( !m_format )
2022 {
2023 if ( m_width == -1 )
2024 {
19d7140e
VZ
2025 if ( m_precision == -1 )
2026 {
2b5f62a0
VZ
2027 // default width/precision
2028 m_format = _T("%f");
2029 }
19d7140e
VZ
2030 else
2031 {
2032 m_format.Printf(_T("%%.%df"), m_precision);
2033 }
0b190b0f
VZ
2034 }
2035 else if ( m_precision == -1 )
2036 {
2037 // default precision
2038 m_format.Printf(_T("%%%d.f"), m_width);
2039 }
2040 else
2041 {
2042 m_format.Printf(_T("%%%d.%df"), m_width, m_precision);
2043 }
2044 }
2045
2046 text.Printf(m_format, val);
19d7140e 2047
0b190b0f
VZ
2048 }
2049 //else: text already contains the string
2050
65e4e78e
VZ
2051 return text;
2052}
2053
816be743
VZ
2054void wxGridCellFloatRenderer::Draw(wxGrid& grid,
2055 wxGridCellAttr& attr,
2056 wxDC& dc,
2057 const wxRect& rectCell,
2058 int row, int col,
2059 bool isSelected)
2060{
2061 wxGridCellRenderer::Draw(grid, attr, dc, rectCell, row, col, isSelected);
2062
2063 SetTextColoursAndFont(grid, attr, dc, isSelected);
2064
2065 // draw the text right aligned by default
2066 int hAlign, vAlign;
2067 attr.GetAlignment(&hAlign, &vAlign);
4c7277db 2068 hAlign = wxALIGN_RIGHT;
816be743
VZ
2069
2070 wxRect rect = rectCell;
2071 rect.Inflate(-1);
2072
65e4e78e
VZ
2073 grid.DrawTextRectangle(dc, GetString(grid, row, col), rect, hAlign, vAlign);
2074}
816be743 2075
65e4e78e
VZ
2076wxSize wxGridCellFloatRenderer::GetBestSize(wxGrid& grid,
2077 wxGridCellAttr& attr,
2078 wxDC& dc,
2079 int row, int col)
2080{
2081 return DoGetBestSize(attr, dc, GetString(grid, row, col));
816be743
VZ
2082}
2083
0b190b0f
VZ
2084void wxGridCellFloatRenderer::SetParameters(const wxString& params)
2085{
0b190b0f
VZ
2086 if ( !params )
2087 {
2088 // reset to defaults
2089 SetWidth(-1);
2090 SetPrecision(-1);
2091 }
2092 else
2093 {
2094 wxString tmp = params.BeforeFirst(_T(','));
ec157c8f 2095 if ( !tmp.empty() )
0b190b0f
VZ
2096 {
2097 long width;
19d7140e 2098 if ( tmp.ToLong(&width) )
0b190b0f 2099 {
19d7140e 2100 SetWidth((int)width);
0b190b0f
VZ
2101 }
2102 else
2103 {
19d7140e
VZ
2104 wxLogDebug(_T("Invalid wxGridCellFloatRenderer width parameter string '%s ignored"), params.c_str());
2105 }
19d7140e 2106 }
2f024384 2107
7448de8d
WS
2108 tmp = params.AfterFirst(_T(','));
2109 if ( !tmp.empty() )
2110 {
2111 long precision;
19d7140e 2112 if ( tmp.ToLong(&precision) )
7448de8d 2113 {
19d7140e 2114 SetPrecision((int)precision);
7448de8d
WS
2115 }
2116 else
2117 {
19d7140e 2118 wxLogDebug(_T("Invalid wxGridCellFloatRenderer precision parameter string '%s ignored"), params.c_str());
7448de8d 2119 }
0b190b0f
VZ
2120 }
2121 }
2122}
2123
508011ce
VZ
2124// ----------------------------------------------------------------------------
2125// wxGridCellBoolRenderer
2126// ----------------------------------------------------------------------------
2127
65e4e78e 2128wxSize wxGridCellBoolRenderer::ms_sizeCheckMark;
508011ce 2129
b94ae1ea
VZ
2130// FIXME these checkbox size calculations are really ugly...
2131
65e4e78e 2132// between checkmark and box
a95e38c0 2133static const wxCoord wxGRID_CHECKMARK_MARGIN = 2;
508011ce 2134
65e4e78e
VZ
2135wxSize wxGridCellBoolRenderer::GetBestSize(wxGrid& grid,
2136 wxGridCellAttr& WXUNUSED(attr),
2137 wxDC& WXUNUSED(dc),
2138 int WXUNUSED(row),
2139 int WXUNUSED(col))
2140{
2141 // compute it only once (no locks for MT safeness in GUI thread...)
2142 if ( !ms_sizeCheckMark.x )
297da4ba 2143 {
65e4e78e 2144 // get checkbox size
ca65c044 2145 wxCheckBox *checkbox = new wxCheckBox(&grid, wxID_ANY, wxEmptyString);
297da4ba 2146 wxSize size = checkbox->GetBestSize();
2f024384 2147 wxCoord checkSize = size.y + 2 * wxGRID_CHECKMARK_MARGIN;
297da4ba 2148
65e4e78e 2149 // FIXME wxGTK::wxCheckBox::GetBestSize() gives "wrong" result
69d8f612 2150#if defined(__WXGTK__) || defined(__WXMOTIF__)
65e4e78e 2151 checkSize -= size.y / 2;
297da4ba
VZ
2152#endif
2153
2154 delete checkbox;
65e4e78e
VZ
2155
2156 ms_sizeCheckMark.x = ms_sizeCheckMark.y = checkSize;
297da4ba
VZ
2157 }
2158
65e4e78e
VZ
2159 return ms_sizeCheckMark;
2160}
2161
2162void wxGridCellBoolRenderer::Draw(wxGrid& grid,
2163 wxGridCellAttr& attr,
2164 wxDC& dc,
2165 const wxRect& rect,
2166 int row, int col,
2167 bool isSelected)
2168{
2169 wxGridCellRenderer::Draw(grid, attr, dc, rect, row, col, isSelected);
2170
297da4ba 2171 // draw a check mark in the centre (ignoring alignment - TODO)
65e4e78e 2172 wxSize size = GetBestSize(grid, attr, dc, row, col);
b94ae1ea
VZ
2173
2174 // don't draw outside the cell
2175 wxCoord minSize = wxMin(rect.width, rect.height);
2176 if ( size.x >= minSize || size.y >= minSize )
2177 {
2178 // and even leave (at least) 1 pixel margin
2179 size.x = size.y = minSize - 2;
2180 }
2181
2182 // draw a border around checkmark
1bd71df9 2183 int vAlign, hAlign;
c2f5b920 2184 attr.GetAlignment(&hAlign, &vAlign);
52d6f640 2185
a95e38c0 2186 wxRect rectBorder;
1bd71df9
JS
2187 if (hAlign == wxALIGN_CENTRE)
2188 {
2f024384
DS
2189 rectBorder.x = rect.x + rect.width / 2 - size.x / 2;
2190 rectBorder.y = rect.y + rect.height / 2 - size.y / 2;
1bd71df9
JS
2191 rectBorder.width = size.x;
2192 rectBorder.height = size.y;
2193 }
2194 else if (hAlign == wxALIGN_LEFT)
2195 {
2196 rectBorder.x = rect.x + 2;
2f024384 2197 rectBorder.y = rect.y + rect.height / 2 - size.y / 2;
1bd71df9 2198 rectBorder.width = size.x;
52d6f640 2199 rectBorder.height = size.y;
1bd71df9
JS
2200 }
2201 else if (hAlign == wxALIGN_RIGHT)
2202 {
2203 rectBorder.x = rect.x + rect.width - size.x - 2;
2f024384 2204 rectBorder.y = rect.y + rect.height / 2 - size.y / 2;
1bd71df9 2205 rectBorder.width = size.x;
52d6f640 2206 rectBorder.height = size.y;
1bd71df9 2207 }
b94ae1ea 2208
f2d76237 2209 bool value;
b94ae1ea 2210 if ( grid.GetTable()->CanGetValueAs(row, col, wxGRID_VALUE_BOOL) )
c2f5b920 2211 {
f2d76237 2212 value = grid.GetTable()->GetValueAsBool(row, col);
c2f5b920 2213 }
f2d76237 2214 else
695a3263
MB
2215 {
2216 wxString cellval( grid.GetTable()->GetValue(row, col) );
8dd8f875 2217 value = !( !cellval || (cellval == wxT("0")) );
695a3263 2218 }
f2d76237
RD
2219
2220 if ( value )
508011ce 2221 {
a95e38c0 2222 wxRect rectMark = rectBorder;
2f024384 2223
a95e38c0
VZ
2224#ifdef __WXMSW__
2225 // MSW DrawCheckMark() is weird (and should probably be changed...)
2f024384 2226 rectMark.Inflate(-wxGRID_CHECKMARK_MARGIN / 2);
a95e38c0
VZ
2227 rectMark.x++;
2228 rectMark.y++;
2f024384 2229#else
a95e38c0 2230 rectMark.Inflate(-wxGRID_CHECKMARK_MARGIN);
2f024384 2231#endif
a95e38c0 2232
508011ce
VZ
2233 dc.SetTextForeground(attr.GetTextColour());
2234 dc.DrawCheckMark(rectMark);
2235 }
a95e38c0
VZ
2236
2237 dc.SetBrush(*wxTRANSPARENT_BRUSH);
2238 dc.SetPen(wxPen(attr.GetTextColour(), 1, wxSOLID));
2239 dc.DrawRectangle(rectBorder);
508011ce
VZ
2240}
2241
2796cce3
RD
2242// ----------------------------------------------------------------------------
2243// wxGridCellAttr
2244// ----------------------------------------------------------------------------
2245
1df4050d
VZ
2246void wxGridCellAttr::Init(wxGridCellAttr *attrDefault)
2247{
2248 m_nRef = 1;
2249
2250 m_isReadOnly = Unset;
2251
2252 m_renderer = NULL;
2253 m_editor = NULL;
2254
2255 m_attrkind = wxGridCellAttr::Cell;
2256
27f35b66 2257 m_sizeRows = m_sizeCols = 1;
b63fce94 2258 m_overflow = UnsetOverflow;
27f35b66 2259
1df4050d
VZ
2260 SetDefAttr(attrDefault);
2261}
2262
39bcce60 2263wxGridCellAttr *wxGridCellAttr::Clone() const
a68c1246 2264{
1df4050d
VZ
2265 wxGridCellAttr *attr = new wxGridCellAttr(m_defGridAttr);
2266
a68c1246
VZ
2267 if ( HasTextColour() )
2268 attr->SetTextColour(GetTextColour());
2269 if ( HasBackgroundColour() )
2270 attr->SetBackgroundColour(GetBackgroundColour());
2271 if ( HasFont() )
2272 attr->SetFont(GetFont());
2273 if ( HasAlignment() )
2274 attr->SetAlignment(m_hAlign, m_vAlign);
2275
27f35b66
SN
2276 attr->SetSize( m_sizeRows, m_sizeCols );
2277
a68c1246
VZ
2278 if ( m_renderer )
2279 {
2280 attr->SetRenderer(m_renderer);
39bcce60 2281 m_renderer->IncRef();
a68c1246
VZ
2282 }
2283 if ( m_editor )
2284 {
2285 attr->SetEditor(m_editor);
39bcce60 2286 m_editor->IncRef();
a68c1246
VZ
2287 }
2288
2289 if ( IsReadOnly() )
2290 attr->SetReadOnly();
2291
19d7140e
VZ
2292 attr->SetKind( m_attrkind );
2293
a68c1246
VZ
2294 return attr;
2295}
2296
19d7140e
VZ
2297void wxGridCellAttr::MergeWith(wxGridCellAttr *mergefrom)
2298{
2299 if ( !HasTextColour() && mergefrom->HasTextColour() )
2300 SetTextColour(mergefrom->GetTextColour());
2301 if ( !HasBackgroundColour() && mergefrom->HasBackgroundColour() )
2302 SetBackgroundColour(mergefrom->GetBackgroundColour());
2303 if ( !HasFont() && mergefrom->HasFont() )
2304 SetFont(mergefrom->GetFont());
4db6714b
KH
2305 if ( !HasAlignment() && mergefrom->HasAlignment() )
2306 {
19d7140e
VZ
2307 int hAlign, vAlign;
2308 mergefrom->GetAlignment( &hAlign, &vAlign);
2309 SetAlignment(hAlign, vAlign);
2310 }
3100c3db
RD
2311 if ( !HasSize() && mergefrom->HasSize() )
2312 mergefrom->GetSize( &m_sizeRows, &m_sizeCols );
27f35b66 2313
19d7140e
VZ
2314 // Directly access member functions as GetRender/Editor don't just return
2315 // m_renderer/m_editor
2316 //
2317 // Maybe add support for merge of Render and Editor?
2318 if (!HasRenderer() && mergefrom->HasRenderer() )
bf7945ce 2319 {
19d7140e
VZ
2320 m_renderer = mergefrom->m_renderer;
2321 m_renderer->IncRef();
2322 }
2323 if ( !HasEditor() && mergefrom->HasEditor() )
2324 {
2325 m_editor = mergefrom->m_editor;
2326 m_editor->IncRef();
2327 }
2f024384 2328 if ( !HasReadWriteMode() && mergefrom->HasReadWriteMode() )
19d7140e
VZ
2329 SetReadOnly(mergefrom->IsReadOnly());
2330
2f024384 2331 if (!HasOverflowMode() && mergefrom->HasOverflowMode() )
ff699386 2332 SetOverflow(mergefrom->GetOverflow());
ef5df12b 2333
19d7140e
VZ
2334 SetDefAttr(mergefrom->m_defGridAttr);
2335}
2336
27f35b66
SN
2337void wxGridCellAttr::SetSize(int num_rows, int num_cols)
2338{
2339 // The size of a cell is normally 1,1
2340
2341 // If this cell is larger (2,2) then this is the top left cell
2342 // the other cells that will be covered (lower right cells) must be
2343 // set to negative or zero values such that
2344 // row + num_rows of the covered cell points to the larger cell (this cell)
2345 // same goes for the col + num_cols.
2346
2347 // Size of 0,0 is NOT valid, neither is <=0 and any positive value
2348
2f024384
DS
2349 wxASSERT_MSG( (!((num_rows > 0) && (num_cols <= 0)) ||
2350 !((num_rows <= 0) && (num_cols > 0)) ||
2351 !((num_rows == 0) && (num_cols == 0))),
27f35b66
SN
2352 wxT("wxGridCellAttr::SetSize only takes two postive values or negative/zero values"));
2353
2354 m_sizeRows = num_rows;
2355 m_sizeCols = num_cols;
2356}
2357
2796cce3
RD
2358const wxColour& wxGridCellAttr::GetTextColour() const
2359{
2360 if (HasTextColour())
508011ce 2361 {
2796cce3 2362 return m_colText;
508011ce 2363 }
0926b2fc 2364 else if (m_defGridAttr && m_defGridAttr != this)
508011ce 2365 {
2796cce3 2366 return m_defGridAttr->GetTextColour();
508011ce
VZ
2367 }
2368 else
2369 {
2796cce3
RD
2370 wxFAIL_MSG(wxT("Missing default cell attribute"));
2371 return wxNullColour;
2372 }
2373}
2374
2796cce3
RD
2375const wxColour& wxGridCellAttr::GetBackgroundColour() const
2376{
2377 if (HasBackgroundColour())
2f024384 2378 {
2796cce3 2379 return m_colBack;
2f024384 2380 }
0926b2fc 2381 else if (m_defGridAttr && m_defGridAttr != this)
2f024384 2382 {
2796cce3 2383 return m_defGridAttr->GetBackgroundColour();
2f024384 2384 }
508011ce
VZ
2385 else
2386 {
2796cce3
RD
2387 wxFAIL_MSG(wxT("Missing default cell attribute"));
2388 return wxNullColour;
2389 }
2390}
2391
2796cce3
RD
2392const wxFont& wxGridCellAttr::GetFont() const
2393{
2394 if (HasFont())
2f024384 2395 {
2796cce3 2396 return m_font;
2f024384 2397 }
0926b2fc 2398 else if (m_defGridAttr && m_defGridAttr != this)
2f024384 2399 {
2796cce3 2400 return m_defGridAttr->GetFont();
2f024384 2401 }
508011ce
VZ
2402 else
2403 {
2796cce3
RD
2404 wxFAIL_MSG(wxT("Missing default cell attribute"));
2405 return wxNullFont;
2406 }
2407}
2408
2796cce3
RD
2409void wxGridCellAttr::GetAlignment(int *hAlign, int *vAlign) const
2410{
508011ce
VZ
2411 if (HasAlignment())
2412 {
4db6714b
KH
2413 if ( hAlign )
2414 *hAlign = m_hAlign;
2415 if ( vAlign )
2416 *vAlign = m_vAlign;
2796cce3 2417 }
0926b2fc 2418 else if (m_defGridAttr && m_defGridAttr != this)
c2f5b920 2419 {
2796cce3 2420 m_defGridAttr->GetAlignment(hAlign, vAlign);
c2f5b920 2421 }
508011ce
VZ
2422 else
2423 {
2796cce3
RD
2424 wxFAIL_MSG(wxT("Missing default cell attribute"));
2425 }
2426}
2427
27f35b66
SN
2428void wxGridCellAttr::GetSize( int *num_rows, int *num_cols ) const
2429{
4db6714b
KH
2430 if ( num_rows )
2431 *num_rows = m_sizeRows;
2432 if ( num_cols )
2433 *num_cols = m_sizeCols;
27f35b66 2434}
2796cce3 2435
f2d76237 2436// GetRenderer and GetEditor use a slightly different decision path about
28a77bc4
RD
2437// which attribute to use. If a non-default attr object has one then it is
2438// used, otherwise the default editor or renderer is fetched from the grid and
2439// used. It should be the default for the data type of the cell. If it is
2440// NULL (because the table has a type that the grid does not have in its
c2f5b920 2441// registry), then the grid's default editor or renderer is used.
28a77bc4
RD
2442
2443wxGridCellRenderer* wxGridCellAttr::GetRenderer(wxGrid* grid, int row, int col) const
2444{
c2f5b920 2445 wxGridCellRenderer *renderer = NULL;
28a77bc4 2446
3cf883a2 2447 if ( m_renderer && this != m_defGridAttr )
0b190b0f 2448 {
3cf883a2
VZ
2449 // use the cells renderer if it has one
2450 renderer = m_renderer;
2451 renderer->IncRef();
0b190b0f 2452 }
2f024384 2453 else // no non-default cell renderer
0b190b0f 2454 {
3cf883a2
VZ
2455 // get default renderer for the data type
2456 if ( grid )
2457 {
2458 // GetDefaultRendererForCell() will do IncRef() for us
2459 renderer = grid->GetDefaultRendererForCell(row, col);
2460 }
0b190b0f 2461
c2f5b920 2462 if ( renderer == NULL )
3cf883a2 2463 {
c2f5b920 2464 if ( (m_defGridAttr != NULL) && (m_defGridAttr != this) )
3cf883a2
VZ
2465 {
2466 // if we still don't have one then use the grid default
2467 // (no need for IncRef() here neither)
2468 renderer = m_defGridAttr->GetRenderer(NULL, 0, 0);
2469 }
2470 else // default grid attr
2471 {
2472 // use m_renderer which we had decided not to use initially
2473 renderer = m_renderer;
2474 if ( renderer )
2475 renderer->IncRef();
2476 }
2477 }
0b190b0f 2478 }
28a77bc4 2479
3cf883a2
VZ
2480 // we're supposed to always find something
2481 wxASSERT_MSG(renderer, wxT("Missing default cell renderer"));
28a77bc4
RD
2482
2483 return renderer;
2796cce3
RD
2484}
2485
3cf883a2 2486// same as above, except for s/renderer/editor/g
28a77bc4 2487wxGridCellEditor* wxGridCellAttr::GetEditor(wxGrid* grid, int row, int col) const
07296f0b 2488{
c2f5b920 2489 wxGridCellEditor *editor = NULL;
0b190b0f 2490
3cf883a2 2491 if ( m_editor && this != m_defGridAttr )
0b190b0f 2492 {
3cf883a2
VZ
2493 // use the cells editor if it has one
2494 editor = m_editor;
2495 editor->IncRef();
0b190b0f 2496 }
3cf883a2 2497 else // no non default cell editor
0b190b0f 2498 {
3cf883a2
VZ
2499 // get default editor for the data type
2500 if ( grid )
2501 {
2502 // GetDefaultEditorForCell() will do IncRef() for us
2503 editor = grid->GetDefaultEditorForCell(row, col);
2504 }
3cf883a2 2505
c2f5b920 2506 if ( editor == NULL )
3cf883a2 2507 {
c2f5b920 2508 if ( (m_defGridAttr != NULL) && (m_defGridAttr != this) )
3cf883a2
VZ
2509 {
2510 // if we still don't have one then use the grid default
2511 // (no need for IncRef() here neither)
2512 editor = m_defGridAttr->GetEditor(NULL, 0, 0);
2513 }
2514 else // default grid attr
2515 {
2516 // use m_editor which we had decided not to use initially
2517 editor = m_editor;
2518 if ( editor )
2519 editor->IncRef();
2520 }
2521 }
0b190b0f 2522 }
28a77bc4 2523
3cf883a2
VZ
2524 // we're supposed to always find something
2525 wxASSERT_MSG(editor, wxT("Missing default cell editor"));
2526
28a77bc4 2527 return editor;
07296f0b
RD
2528}
2529
b99be8fb 2530// ----------------------------------------------------------------------------
758cbedf 2531// wxGridCellAttrData
b99be8fb
VZ
2532// ----------------------------------------------------------------------------
2533
758cbedf 2534void wxGridCellAttrData::SetAttr(wxGridCellAttr *attr, int row, int col)
b99be8fb
VZ
2535{
2536 int n = FindIndex(row, col);
2537 if ( n == wxNOT_FOUND )
2538 {
2539 // add the attribute
2540 m_attrs.Add(new wxGridCellWithAttr(row, col, attr));
2541 }
2542 else
2543 {
6f36917b
VZ
2544 // free the old attribute
2545 m_attrs[(size_t)n].attr->DecRef();
2546
b99be8fb
VZ
2547 if ( attr )
2548 {
2549 // change the attribute
2e9a6788 2550 m_attrs[(size_t)n].attr = attr;
b99be8fb
VZ
2551 }
2552 else
2553 {
2554 // remove this attribute
2555 m_attrs.RemoveAt((size_t)n);
2556 }
2557 }
b99be8fb
VZ
2558}
2559
758cbedf 2560wxGridCellAttr *wxGridCellAttrData::GetAttr(int row, int col) const
b99be8fb
VZ
2561{
2562 wxGridCellAttr *attr = (wxGridCellAttr *)NULL;
2563
2564 int n = FindIndex(row, col);
2565 if ( n != wxNOT_FOUND )
2566 {
2e9a6788
VZ
2567 attr = m_attrs[(size_t)n].attr;
2568 attr->IncRef();
b99be8fb
VZ
2569 }
2570
2571 return attr;
2572}
2573
4d60017a
SN
2574void wxGridCellAttrData::UpdateAttrRows( size_t pos, int numRows )
2575{
2576 size_t count = m_attrs.GetCount();
2577 for ( size_t n = 0; n < count; n++ )
2578 {
2579 wxGridCellCoords& coords = m_attrs[n].coords;
d1c0b4f9
VZ
2580 wxCoord row = coords.GetRow();
2581 if ((size_t)row >= pos)
2582 {
2583 if (numRows > 0)
2584 {
2585 // If rows inserted, include row counter where necessary
2586 coords.SetRow(row + numRows);
2587 }
2588 else if (numRows < 0)
2589 {
2590 // If rows deleted ...
2591 if ((size_t)row >= pos - numRows)
2592 {
2593 // ...either decrement row counter (if row still exists)...
2594 coords.SetRow(row + numRows);
2595 }
2596 else
2597 {
2598 // ...or remove the attribute
d3e9dd94
SN
2599 // No need to DecRef the attribute itself since this is
2600 // done be wxGridCellWithAttr's destructor!
01dd42b6 2601 m_attrs.RemoveAt(n);
4db6714b
KH
2602 n--;
2603 count--;
d1c0b4f9
VZ
2604 }
2605 }
4d60017a
SN
2606 }
2607 }
2608}
2609
2610void wxGridCellAttrData::UpdateAttrCols( size_t pos, int numCols )
2611{
2612 size_t count = m_attrs.GetCount();
2613 for ( size_t n = 0; n < count; n++ )
2614 {
2615 wxGridCellCoords& coords = m_attrs[n].coords;
d1c0b4f9
VZ
2616 wxCoord col = coords.GetCol();
2617 if ( (size_t)col >= pos )
2618 {
2619 if ( numCols > 0 )
2620 {
2621 // If rows inserted, include row counter where necessary
2622 coords.SetCol(col + numCols);
2623 }
2624 else if (numCols < 0)
2625 {
2626 // If rows deleted ...
2627 if ((size_t)col >= pos - numCols)
2628 {
2629 // ...either decrement row counter (if row still exists)...
2630 coords.SetCol(col + numCols);
2631 }
2632 else
2633 {
2634 // ...or remove the attribute
d3e9dd94
SN
2635 // No need to DecRef the attribute itself since this is
2636 // done be wxGridCellWithAttr's destructor!
01dd42b6 2637 m_attrs.RemoveAt(n);
2f024384
DS
2638 n--;
2639 count--;
d1c0b4f9
VZ
2640 }
2641 }
4d60017a
SN
2642 }
2643 }
2644}
2645
758cbedf 2646int wxGridCellAttrData::FindIndex(int row, int col) const
b99be8fb
VZ
2647{
2648 size_t count = m_attrs.GetCount();
2649 for ( size_t n = 0; n < count; n++ )
2650 {
2651 const wxGridCellCoords& coords = m_attrs[n].coords;
2652 if ( (coords.GetRow() == row) && (coords.GetCol() == col) )
2653 {
2654 return n;
2655 }
2656 }
2657
2658 return wxNOT_FOUND;
2659}
2660
758cbedf
VZ
2661// ----------------------------------------------------------------------------
2662// wxGridRowOrColAttrData
2663// ----------------------------------------------------------------------------
2664
2665wxGridRowOrColAttrData::~wxGridRowOrColAttrData()
2666{
2667 size_t count = m_attrs.Count();
2668 for ( size_t n = 0; n < count; n++ )
2669 {
2670 m_attrs[n]->DecRef();
2671 }
2672}
2673
2674wxGridCellAttr *wxGridRowOrColAttrData::GetAttr(int rowOrCol) const
2675{
2676 wxGridCellAttr *attr = (wxGridCellAttr *)NULL;
2677
2678 int n = m_rowsOrCols.Index(rowOrCol);
2679 if ( n != wxNOT_FOUND )
2680 {
2681 attr = m_attrs[(size_t)n];
2682 attr->IncRef();
2683 }
2684
2685 return attr;
2686}
2687
2688void wxGridRowOrColAttrData::SetAttr(wxGridCellAttr *attr, int rowOrCol)
2689{
a95e38c0
VZ
2690 int i = m_rowsOrCols.Index(rowOrCol);
2691 if ( i == wxNOT_FOUND )
758cbedf
VZ
2692 {
2693 // add the attribute
2694 m_rowsOrCols.Add(rowOrCol);
2695 m_attrs.Add(attr);
2696 }
2697 else
2698 {
a95e38c0 2699 size_t n = (size_t)i;
758cbedf
VZ
2700 if ( attr )
2701 {
2702 // change the attribute
a95e38c0
VZ
2703 m_attrs[n]->DecRef();
2704 m_attrs[n] = attr;
758cbedf
VZ
2705 }
2706 else
2707 {
2708 // remove this attribute
a95e38c0
VZ
2709 m_attrs[n]->DecRef();
2710 m_rowsOrCols.RemoveAt(n);
2711 m_attrs.RemoveAt(n);
758cbedf
VZ
2712 }
2713 }
2714}
2715
4d60017a
SN
2716void wxGridRowOrColAttrData::UpdateAttrRowsOrCols( size_t pos, int numRowsOrCols )
2717{
2718 size_t count = m_attrs.GetCount();
2719 for ( size_t n = 0; n < count; n++ )
2720 {
2721 int & rowOrCol = m_rowsOrCols[n];
d1c0b4f9
VZ
2722 if ( (size_t)rowOrCol >= pos )
2723 {
2724 if ( numRowsOrCols > 0 )
2725 {
2726 // If rows inserted, include row counter where necessary
2727 rowOrCol += numRowsOrCols;
2728 }
2729 else if ( numRowsOrCols < 0)
2730 {
2731 // If rows deleted, either decrement row counter (if row still exists)
2732 if ((size_t)rowOrCol >= pos - numRowsOrCols)
2733 rowOrCol += numRowsOrCols;
2734 else
2735 {
01dd42b6
VZ
2736 m_rowsOrCols.RemoveAt(n);
2737 m_attrs[n]->DecRef();
2738 m_attrs.RemoveAt(n);
4db6714b
KH
2739 n--;
2740 count--;
d1c0b4f9
VZ
2741 }
2742 }
4d60017a
SN
2743 }
2744 }
2745}
2746
b99be8fb
VZ
2747// ----------------------------------------------------------------------------
2748// wxGridCellAttrProvider
2749// ----------------------------------------------------------------------------
2750
2751wxGridCellAttrProvider::wxGridCellAttrProvider()
2752{
2753 m_data = (wxGridCellAttrProviderData *)NULL;
2754}
2755
2756wxGridCellAttrProvider::~wxGridCellAttrProvider()
2757{
2758 delete m_data;
2759}
2760
2761void wxGridCellAttrProvider::InitData()
2762{
2763 m_data = new wxGridCellAttrProviderData;
2764}
2765
19d7140e
VZ
2766wxGridCellAttr *wxGridCellAttrProvider::GetAttr(int row, int col,
2767 wxGridCellAttr::wxAttrKind kind ) const
b99be8fb 2768{
758cbedf
VZ
2769 wxGridCellAttr *attr = (wxGridCellAttr *)NULL;
2770 if ( m_data )
2771 {
962a48f6 2772 switch (kind)
758cbedf 2773 {
19d7140e 2774 case (wxGridCellAttr::Any):
962a48f6
DS
2775 // Get cached merge attributes.
2776 // Currently not used as no cache implemented as not mutable
19d7140e 2777 // attr = m_data->m_mergeAttr.GetAttr(row, col);
4db6714b 2778 if (!attr)
19d7140e 2779 {
962a48f6
DS
2780 // Basically implement old version.
2781 // Also check merge cache, so we don't have to re-merge every time..
999836aa
VZ
2782 wxGridCellAttr *attrcell = m_data->m_cellAttrs.GetAttr(row, col);
2783 wxGridCellAttr *attrrow = m_data->m_rowAttrs.GetAttr(row);
2784 wxGridCellAttr *attrcol = m_data->m_colAttrs.GetAttr(col);
19d7140e 2785
4db6714b
KH
2786 if ((attrcell != attrrow) && (attrrow != attrcol) && (attrcell != attrcol))
2787 {
2d0c2e79 2788 // Two or more are non NULL
19d7140e
VZ
2789 attr = new wxGridCellAttr;
2790 attr->SetKind(wxGridCellAttr::Merged);
2791
962a48f6 2792 // Order is important..
4db6714b
KH
2793 if (attrcell)
2794 {
19d7140e
VZ
2795 attr->MergeWith(attrcell);
2796 attrcell->DecRef();
2797 }
4db6714b
KH
2798 if (attrcol)
2799 {
19d7140e
VZ
2800 attr->MergeWith(attrcol);
2801 attrcol->DecRef();
2802 }
4db6714b
KH
2803 if (attrrow)
2804 {
19d7140e
VZ
2805 attr->MergeWith(attrrow);
2806 attrrow->DecRef();
2807 }
962a48f6
DS
2808
2809 // store merge attr if cache implemented
19d7140e
VZ
2810 //attr->IncRef();
2811 //m_data->m_mergeAttr.SetAttr(attr, row, col);
2812 }
2813 else
2d0c2e79 2814 {
19d7140e 2815 // one or none is non null return it or null.
4db6714b
KH
2816 if (attrrow)
2817 attr = attrrow;
2818 if (attrcol)
2d0c2e79 2819 {
962a48f6 2820 if (attr)
2d0c2e79
RD
2821 attr->DecRef();
2822 attr = attrcol;
2823 }
4db6714b 2824 if (attrcell)
2d0c2e79 2825 {
4db6714b 2826 if (attr)
2d0c2e79
RD
2827 attr->DecRef();
2828 attr = attrcell;
2829 }
19d7140e
VZ
2830 }
2831 }
2f024384 2832 break;
4db6714b 2833
19d7140e
VZ
2834 case (wxGridCellAttr::Cell):
2835 attr = m_data->m_cellAttrs.GetAttr(row, col);
2f024384 2836 break;
4db6714b 2837
19d7140e 2838 case (wxGridCellAttr::Col):
2d0c2e79 2839 attr = m_data->m_colAttrs.GetAttr(col);
2f024384 2840 break;
4db6714b 2841
19d7140e 2842 case (wxGridCellAttr::Row):
2d0c2e79 2843 attr = m_data->m_rowAttrs.GetAttr(row);
2f024384 2844 break;
4db6714b 2845
19d7140e
VZ
2846 default:
2847 // unused as yet...
2848 // (wxGridCellAttr::Default):
2849 // (wxGridCellAttr::Merged):
2f024384 2850 break;
758cbedf
VZ
2851 }
2852 }
2f024384 2853
758cbedf 2854 return attr;
b99be8fb
VZ
2855}
2856
2e9a6788 2857void wxGridCellAttrProvider::SetAttr(wxGridCellAttr *attr,
b99be8fb
VZ
2858 int row, int col)
2859{
2860 if ( !m_data )
2861 InitData();
2862
758cbedf
VZ
2863 m_data->m_cellAttrs.SetAttr(attr, row, col);
2864}
2865
2866void wxGridCellAttrProvider::SetRowAttr(wxGridCellAttr *attr, int row)
2867{
2868 if ( !m_data )
2869 InitData();
2870
2871 m_data->m_rowAttrs.SetAttr(attr, row);
2872}
2873
2874void wxGridCellAttrProvider::SetColAttr(wxGridCellAttr *attr, int col)
2875{
2876 if ( !m_data )
2877 InitData();
2878
2879 m_data->m_colAttrs.SetAttr(attr, col);
b99be8fb
VZ
2880}
2881
4d60017a
SN
2882void wxGridCellAttrProvider::UpdateAttrRows( size_t pos, int numRows )
2883{
2884 if ( m_data )
2885 {
2886 m_data->m_cellAttrs.UpdateAttrRows( pos, numRows );
2887
d1c0b4f9 2888 m_data->m_rowAttrs.UpdateAttrRowsOrCols( pos, numRows );
4d60017a
SN
2889 }
2890}
2891
2892void wxGridCellAttrProvider::UpdateAttrCols( size_t pos, int numCols )
2893{
2894 if ( m_data )
2895 {
2896 m_data->m_cellAttrs.UpdateAttrCols( pos, numCols );
2897
d1c0b4f9 2898 m_data->m_colAttrs.UpdateAttrRowsOrCols( pos, numCols );
4d60017a
SN
2899 }
2900}
2901
f2d76237
RD
2902// ----------------------------------------------------------------------------
2903// wxGridTypeRegistry
2904// ----------------------------------------------------------------------------
2905
2906wxGridTypeRegistry::~wxGridTypeRegistry()
2907{
b94ae1ea
VZ
2908 size_t count = m_typeinfo.Count();
2909 for ( size_t i = 0; i < count; i++ )
f2d76237
RD
2910 delete m_typeinfo[i];
2911}
2912
f2d76237
RD
2913void wxGridTypeRegistry::RegisterDataType(const wxString& typeName,
2914 wxGridCellRenderer* renderer,
2915 wxGridCellEditor* editor)
2916{
f2d76237
RD
2917 wxGridDataTypeInfo* info = new wxGridDataTypeInfo(typeName, renderer, editor);
2918
2919 // is it already registered?
c4608a8a 2920 int loc = FindRegisteredDataType(typeName);
39bcce60
VZ
2921 if ( loc != wxNOT_FOUND )
2922 {
f2d76237
RD
2923 delete m_typeinfo[loc];
2924 m_typeinfo[loc] = info;
2925 }
39bcce60
VZ
2926 else
2927 {
f2d76237
RD
2928 m_typeinfo.Add(info);
2929 }
2930}
2931
c4608a8a
VZ
2932int wxGridTypeRegistry::FindRegisteredDataType(const wxString& typeName)
2933{
2934 size_t count = m_typeinfo.GetCount();
2935 for ( size_t i = 0; i < count; i++ )
2936 {
2937 if ( typeName == m_typeinfo[i]->m_typeName )
2938 {
2939 return i;
2940 }
2941 }
2942
2943 return wxNOT_FOUND;
2944}
2945
f2d76237
RD
2946int wxGridTypeRegistry::FindDataType(const wxString& typeName)
2947{
c4608a8a
VZ
2948 int index = FindRegisteredDataType(typeName);
2949 if ( index == wxNOT_FOUND )
2950 {
2951 // check whether this is one of the standard ones, in which case
2952 // register it "on the fly"
3a8c693a 2953#if wxUSE_TEXTCTRL
c4608a8a
VZ
2954 if ( typeName == wxGRID_VALUE_STRING )
2955 {
2956 RegisterDataType(wxGRID_VALUE_STRING,
2957 new wxGridCellStringRenderer,
2958 new wxGridCellTextEditor);
4db6714b
KH
2959 }
2960 else
3a8c693a
VZ
2961#endif // wxUSE_TEXTCTRL
2962#if wxUSE_CHECKBOX
2963 if ( typeName == wxGRID_VALUE_BOOL )
c4608a8a
VZ
2964 {
2965 RegisterDataType(wxGRID_VALUE_BOOL,
2966 new wxGridCellBoolRenderer,
2967 new wxGridCellBoolEditor);
4db6714b
KH
2968 }
2969 else
3a8c693a
VZ
2970#endif // wxUSE_CHECKBOX
2971#if wxUSE_TEXTCTRL
2972 if ( typeName == wxGRID_VALUE_NUMBER )
c4608a8a
VZ
2973 {
2974 RegisterDataType(wxGRID_VALUE_NUMBER,
2975 new wxGridCellNumberRenderer,
2976 new wxGridCellNumberEditor);
2977 }
2978 else if ( typeName == wxGRID_VALUE_FLOAT )
2979 {
2980 RegisterDataType(wxGRID_VALUE_FLOAT,
2981 new wxGridCellFloatRenderer,
2982 new wxGridCellFloatEditor);
4db6714b
KH
2983 }
2984 else
3a8c693a
VZ
2985#endif // wxUSE_TEXTCTRL
2986#if wxUSE_COMBOBOX
2987 if ( typeName == wxGRID_VALUE_CHOICE )
c4608a8a
VZ
2988 {
2989 RegisterDataType(wxGRID_VALUE_CHOICE,
2990 new wxGridCellStringRenderer,
2991 new wxGridCellChoiceEditor);
4db6714b
KH
2992 }
2993 else
3a8c693a 2994#endif // wxUSE_COMBOBOX
c4608a8a
VZ
2995 {
2996 return wxNOT_FOUND;
2997 }
f2d76237 2998
c4608a8a
VZ
2999 // we get here only if just added the entry for this type, so return
3000 // the last index
3001 index = m_typeinfo.GetCount() - 1;
3002 }
3003
3004 return index;
3005}
3006
3007int wxGridTypeRegistry::FindOrCloneDataType(const wxString& typeName)
3008{
3009 int index = FindDataType(typeName);
3010 if ( index == wxNOT_FOUND )
3011 {
3012 // the first part of the typename is the "real" type, anything after ':'
3013 // are the parameters for the renderer
3014 index = FindDataType(typeName.BeforeFirst(_T(':')));
3015 if ( index == wxNOT_FOUND )
3016 {
3017 return wxNOT_FOUND;
f2d76237 3018 }
c4608a8a
VZ
3019
3020 wxGridCellRenderer *renderer = GetRenderer(index);
3021 wxGridCellRenderer *rendererOld = renderer;
3022 renderer = renderer->Clone();
3023 rendererOld->DecRef();
3024
3025 wxGridCellEditor *editor = GetEditor(index);
3026 wxGridCellEditor *editorOld = editor;
3027 editor = editor->Clone();
3028 editorOld->DecRef();
3029
3030 // do it even if there are no parameters to reset them to defaults
3031 wxString params = typeName.AfterFirst(_T(':'));
3032 renderer->SetParameters(params);
3033 editor->SetParameters(params);
3034
3035 // register the new typename
c4608a8a
VZ
3036 RegisterDataType(typeName, renderer, editor);
3037
3038 // we just registered it, it's the last one
3039 index = m_typeinfo.GetCount() - 1;
f2d76237
RD
3040 }
3041
c4608a8a 3042 return index;
f2d76237
RD
3043}
3044
3045wxGridCellRenderer* wxGridTypeRegistry::GetRenderer(int index)
3046{
3047 wxGridCellRenderer* renderer = m_typeinfo[index]->m_renderer;
faec5a43
SN
3048 if (renderer)
3049 renderer->IncRef();
2f024384 3050
f2d76237
RD
3051 return renderer;
3052}
3053
0b190b0f 3054wxGridCellEditor* wxGridTypeRegistry::GetEditor(int index)
f2d76237
RD
3055{
3056 wxGridCellEditor* editor = m_typeinfo[index]->m_editor;
faec5a43
SN
3057 if (editor)
3058 editor->IncRef();
2f024384 3059
f2d76237
RD
3060 return editor;
3061}
3062
758cbedf
VZ
3063// ----------------------------------------------------------------------------
3064// wxGridTableBase
3065// ----------------------------------------------------------------------------
3066
f85afd4e
MB
3067IMPLEMENT_ABSTRACT_CLASS( wxGridTableBase, wxObject )
3068
f85afd4e 3069wxGridTableBase::wxGridTableBase()
f85afd4e
MB
3070{
3071 m_view = (wxGrid *) NULL;
b99be8fb 3072 m_attrProvider = (wxGridCellAttrProvider *) NULL;
f85afd4e
MB
3073}
3074
3075wxGridTableBase::~wxGridTableBase()
3076{
b99be8fb
VZ
3077 delete m_attrProvider;
3078}
3079
3080void wxGridTableBase::SetAttrProvider(wxGridCellAttrProvider *attrProvider)
3081{
3082 delete m_attrProvider;
3083 m_attrProvider = attrProvider;
f85afd4e
MB
3084}
3085
f2d76237
RD
3086bool wxGridTableBase::CanHaveAttributes()
3087{
3088 if ( ! GetAttrProvider() )
3089 {
3090 // use the default attr provider by default
3091 SetAttrProvider(new wxGridCellAttrProvider);
3092 }
2f024384 3093
ca65c044 3094 return true;
f2d76237
RD
3095}
3096
19d7140e 3097wxGridCellAttr *wxGridTableBase::GetAttr(int row, int col, wxGridCellAttr::wxAttrKind kind)
b99be8fb
VZ
3098{
3099 if ( m_attrProvider )
19d7140e 3100 return m_attrProvider->GetAttr(row, col, kind);
b99be8fb
VZ
3101 else
3102 return (wxGridCellAttr *)NULL;
3103}
3104
758cbedf 3105void wxGridTableBase::SetAttr(wxGridCellAttr* attr, int row, int col)
b99be8fb
VZ
3106{
3107 if ( m_attrProvider )
3108 {
19d7140e 3109 attr->SetKind(wxGridCellAttr::Cell);
b99be8fb
VZ
3110 m_attrProvider->SetAttr(attr, row, col);
3111 }
3112 else
3113 {
3114 // as we take ownership of the pointer and don't store it, we must
3115 // free it now
39bcce60 3116 wxSafeDecRef(attr);
b99be8fb
VZ
3117 }
3118}
3119
758cbedf
VZ
3120void wxGridTableBase::SetRowAttr(wxGridCellAttr *attr, int row)
3121{
3122 if ( m_attrProvider )
3123 {
19d7140e 3124 attr->SetKind(wxGridCellAttr::Row);
758cbedf
VZ
3125 m_attrProvider->SetRowAttr(attr, row);
3126 }
3127 else
3128 {
3129 // as we take ownership of the pointer and don't store it, we must
3130 // free it now
39bcce60 3131 wxSafeDecRef(attr);
758cbedf
VZ
3132 }
3133}
3134
3135void wxGridTableBase::SetColAttr(wxGridCellAttr *attr, int col)
3136{
3137 if ( m_attrProvider )
3138 {
19d7140e 3139 attr->SetKind(wxGridCellAttr::Col);
758cbedf
VZ
3140 m_attrProvider->SetColAttr(attr, col);
3141 }
3142 else
3143 {
3144 // as we take ownership of the pointer and don't store it, we must
3145 // free it now
39bcce60 3146 wxSafeDecRef(attr);
758cbedf
VZ
3147 }
3148}
3149
aa5e1f75
SN
3150bool wxGridTableBase::InsertRows( size_t WXUNUSED(pos),
3151 size_t WXUNUSED(numRows) )
f85afd4e 3152{
f6bcfd97 3153 wxFAIL_MSG( wxT("Called grid table class function InsertRows\nbut your derived table class does not override this function") );
8f177c8e 3154
ca65c044 3155 return false;
f85afd4e
MB
3156}
3157
aa5e1f75 3158bool wxGridTableBase::AppendRows( size_t WXUNUSED(numRows) )
f85afd4e 3159{
f6bcfd97 3160 wxFAIL_MSG( wxT("Called grid table class function AppendRows\nbut your derived table class does not override this function"));
8f177c8e 3161
ca65c044 3162 return false;
f85afd4e
MB
3163}
3164
aa5e1f75
SN
3165bool wxGridTableBase::DeleteRows( size_t WXUNUSED(pos),
3166 size_t WXUNUSED(numRows) )
f85afd4e 3167{
f6bcfd97 3168 wxFAIL_MSG( wxT("Called grid table class function DeleteRows\nbut your derived table class does not override this function"));
8f177c8e 3169
ca65c044 3170 return false;
f85afd4e
MB
3171}
3172
aa5e1f75
SN
3173bool wxGridTableBase::InsertCols( size_t WXUNUSED(pos),
3174 size_t WXUNUSED(numCols) )
f85afd4e 3175{
f6bcfd97 3176 wxFAIL_MSG( wxT("Called grid table class function InsertCols\nbut your derived table class does not override this function"));
8f177c8e 3177
ca65c044 3178 return false;
f85afd4e
MB
3179}
3180
aa5e1f75 3181bool wxGridTableBase::AppendCols( size_t WXUNUSED(numCols) )
f85afd4e 3182{
f6bcfd97 3183 wxFAIL_MSG(wxT("Called grid table class function AppendCols\nbut your derived table class does not override this function"));
8f177c8e 3184
ca65c044 3185 return false;
f85afd4e
MB
3186}
3187
aa5e1f75
SN
3188bool wxGridTableBase::DeleteCols( size_t WXUNUSED(pos),
3189 size_t WXUNUSED(numCols) )
f85afd4e 3190{
f6bcfd97 3191 wxFAIL_MSG( wxT("Called grid table class function DeleteCols\nbut your derived table class does not override this function"));
8f177c8e 3192
ca65c044 3193 return false;
f85afd4e
MB
3194}
3195
f85afd4e
MB
3196wxString wxGridTableBase::GetRowLabelValue( int row )
3197{
3198 wxString s;
93763ad5 3199
2f024384
DS
3200 // RD: Starting the rows at zero confuses users,
3201 // no matter how much it makes sense to us geeks.
3202 s << row + 1;
3203
f85afd4e
MB
3204 return s;
3205}
3206
3207wxString wxGridTableBase::GetColLabelValue( int col )
3208{
3209 // default col labels are:
3210 // cols 0 to 25 : A-Z
3211 // cols 26 to 675 : AA-ZZ
3212 // etc.
3213
3214 wxString s;
3215 unsigned int i, n;
3216 for ( n = 1; ; n++ )
3217 {
2f024384
DS
3218 s += (wxChar) (_T('A') + (wxChar)(col % 26));
3219 col = col / 26 - 1;
4db6714b
KH
3220 if ( col < 0 )
3221 break;
f85afd4e
MB
3222 }
3223
3224 // reverse the string...
3225 wxString s2;
3d59537f 3226 for ( i = 0; i < n; i++ )
f85afd4e 3227 {
2f024384 3228 s2 += s[n - i - 1];
f85afd4e
MB
3229 }
3230
3231 return s2;
3232}
3233
f2d76237
RD
3234wxString wxGridTableBase::GetTypeName( int WXUNUSED(row), int WXUNUSED(col) )
3235{
816be743 3236 return wxGRID_VALUE_STRING;
f2d76237
RD
3237}
3238
3239bool wxGridTableBase::CanGetValueAs( int WXUNUSED(row), int WXUNUSED(col),
3240 const wxString& typeName )
3241{
816be743 3242 return typeName == wxGRID_VALUE_STRING;
f2d76237
RD
3243}
3244
3245bool wxGridTableBase::CanSetValueAs( int row, int col, const wxString& typeName )
3246{
3247 return CanGetValueAs(row, col, typeName);
3248}
3249
3250long wxGridTableBase::GetValueAsLong( int WXUNUSED(row), int WXUNUSED(col) )
3251{
3252 return 0;
3253}
3254
3255double wxGridTableBase::GetValueAsDouble( int WXUNUSED(row), int WXUNUSED(col) )
3256{
3257 return 0.0;
3258}
3259
3260bool wxGridTableBase::GetValueAsBool( int WXUNUSED(row), int WXUNUSED(col) )
3261{
ca65c044 3262 return false;
f2d76237
RD
3263}
3264
3265void wxGridTableBase::SetValueAsLong( int WXUNUSED(row), int WXUNUSED(col),
3266 long WXUNUSED(value) )
3267{
3268}
3269
3270void wxGridTableBase::SetValueAsDouble( int WXUNUSED(row), int WXUNUSED(col),
3271 double WXUNUSED(value) )
3272{
3273}
3274
3275void wxGridTableBase::SetValueAsBool( int WXUNUSED(row), int WXUNUSED(col),
3276 bool WXUNUSED(value) )
3277{
3278}
3279
f2d76237
RD
3280void* wxGridTableBase::GetValueAsCustom( int WXUNUSED(row), int WXUNUSED(col),
3281 const wxString& WXUNUSED(typeName) )
3282{
3283 return NULL;
3284}
3285
3286void wxGridTableBase::SetValueAsCustom( int WXUNUSED(row), int WXUNUSED(col),
3287 const wxString& WXUNUSED(typeName),
3288 void* WXUNUSED(value) )
3289{
3290}
3291
f85afd4e
MB
3292//////////////////////////////////////////////////////////////////////
3293//
3294// Message class for the grid table to send requests and notifications
3295// to the grid view
3296//
3297
3298wxGridTableMessage::wxGridTableMessage()
3299{
3300 m_table = (wxGridTableBase *) NULL;
3301 m_id = -1;
3302 m_comInt1 = -1;
3303 m_comInt2 = -1;
3304}
3305
3306wxGridTableMessage::wxGridTableMessage( wxGridTableBase *table, int id,
3307 int commandInt1, int commandInt2 )
3308{
3309 m_table = table;
3310 m_id = id;
3311 m_comInt1 = commandInt1;
3312 m_comInt2 = commandInt2;
3313}
3314
f85afd4e
MB
3315//////////////////////////////////////////////////////////////////////
3316//
3317// A basic grid table for string data. An object of this class will
3318// created by wxGrid if you don't specify an alternative table class.
3319//
3320
223d09f6 3321WX_DEFINE_OBJARRAY(wxGridStringArray)
f85afd4e
MB
3322
3323IMPLEMENT_DYNAMIC_CLASS( wxGridStringTable, wxGridTableBase )
3324
3325wxGridStringTable::wxGridStringTable()
3326 : wxGridTableBase()
3327{
3328}
3329
3330wxGridStringTable::wxGridStringTable( int numRows, int numCols )
3331 : wxGridTableBase()
3332{
f85afd4e
MB
3333 m_data.Alloc( numRows );
3334
3335 wxArrayString sa;
3336 sa.Alloc( numCols );
27f35b66 3337 sa.Add( wxEmptyString, numCols );
8f177c8e 3338
27f35b66 3339 m_data.Add( sa, numRows );
f85afd4e
MB
3340}
3341
3342wxGridStringTable::~wxGridStringTable()
3343{
3344}
3345
e32352cf 3346int wxGridStringTable::GetNumberRows()
f85afd4e
MB
3347{
3348 return m_data.GetCount();
3349}
3350
e32352cf 3351int wxGridStringTable::GetNumberCols()
f85afd4e
MB
3352{
3353 if ( m_data.GetCount() > 0 )
3354 return m_data[0].GetCount();
3355 else
3356 return 0;
3357}
3358
3359wxString wxGridStringTable::GetValue( int row, int col )
3360{
3e13956a
RD
3361 wxCHECK_MSG( (row < GetNumberRows()) && (col < GetNumberCols()),
3362 wxEmptyString,
3363 _T("invalid row or column index in wxGridStringTable") );
af547d51 3364
f85afd4e
MB
3365 return m_data[row][col];
3366}
3367
f2d76237 3368void wxGridStringTable::SetValue( int row, int col, const wxString& value )
f85afd4e 3369{
3e13956a
RD
3370 wxCHECK_RET( (row < GetNumberRows()) && (col < GetNumberCols()),
3371 _T("invalid row or column index in wxGridStringTable") );
af547d51 3372
f2d76237 3373 m_data[row][col] = value;
f85afd4e
MB
3374}
3375
3376bool wxGridStringTable::IsEmptyCell( int row, int col )
3377{
3e13956a
RD
3378 wxCHECK_MSG( (row < GetNumberRows()) && (col < GetNumberCols()),
3379 true,
af547d51
VZ
3380 _T("invalid row or column index in wxGridStringTable") );
3381
f85afd4e
MB
3382 return (m_data[row][col] == wxEmptyString);
3383}
3384
f85afd4e
MB
3385void wxGridStringTable::Clear()
3386{
3387 int row, col;
3388 int numRows, numCols;
8f177c8e 3389
f85afd4e
MB
3390 numRows = m_data.GetCount();
3391 if ( numRows > 0 )
3392 {
3393 numCols = m_data[0].GetCount();
3394
3d59537f 3395 for ( row = 0; row < numRows; row++ )
f85afd4e 3396 {
3d59537f 3397 for ( col = 0; col < numCols; col++ )
f85afd4e
MB
3398 {
3399 m_data[row][col] = wxEmptyString;
3400 }
3401 }
3402 }
3403}
3404
f85afd4e
MB
3405bool wxGridStringTable::InsertRows( size_t pos, size_t numRows )
3406{
f85afd4e 3407 size_t curNumRows = m_data.GetCount();
f6bcfd97
BP
3408 size_t curNumCols = ( curNumRows > 0 ? m_data[0].GetCount() :
3409 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
8f177c8e 3410
f85afd4e
MB
3411 if ( pos >= curNumRows )
3412 {
3413 return AppendRows( numRows );
3414 }
8f177c8e 3415
f85afd4e
MB
3416 wxArrayString sa;
3417 sa.Alloc( curNumCols );
27f35b66
SN
3418 sa.Add( wxEmptyString, curNumCols );
3419 m_data.Insert( sa, pos, numRows );
2f024384 3420
f85afd4e
MB
3421 if ( GetView() )
3422 {
3423 wxGridTableMessage msg( this,
3424 wxGRIDTABLE_NOTIFY_ROWS_INSERTED,
3425 pos,
3426 numRows );
8f177c8e 3427
f85afd4e
MB
3428 GetView()->ProcessTableMessage( msg );
3429 }
3430
ca65c044 3431 return true;
f85afd4e
MB
3432}
3433
3434bool wxGridStringTable::AppendRows( size_t numRows )
3435{
f85afd4e 3436 size_t curNumRows = m_data.GetCount();
4db6714b
KH
3437 size_t curNumCols = ( curNumRows > 0
3438 ? m_data[0].GetCount()
3439 : ( GetView() ? GetView()->GetNumberCols() : 0 ) );
8f177c8e 3440
f85afd4e
MB
3441 wxArrayString sa;
3442 if ( curNumCols > 0 )
3443 {
3444 sa.Alloc( curNumCols );
27f35b66 3445 sa.Add( wxEmptyString, curNumCols );
f85afd4e 3446 }
8f177c8e 3447
27f35b66 3448 m_data.Add( sa, numRows );
f85afd4e
MB
3449
3450 if ( GetView() )
3451 {
3452 wxGridTableMessage msg( this,
3453 wxGRIDTABLE_NOTIFY_ROWS_APPENDED,
3454 numRows );
8f177c8e 3455
f85afd4e
MB
3456 GetView()->ProcessTableMessage( msg );
3457 }
3458
ca65c044 3459 return true;
f85afd4e
MB
3460}
3461
3462bool wxGridStringTable::DeleteRows( size_t pos, size_t numRows )
3463{
f85afd4e 3464 size_t curNumRows = m_data.GetCount();
8f177c8e 3465
f85afd4e
MB
3466 if ( pos >= curNumRows )
3467 {
e91d2033
VZ
3468 wxFAIL_MSG( wxString::Format
3469 (
3470 wxT("Called wxGridStringTable::DeleteRows(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu rows"),
3471 (unsigned long)pos,
3472 (unsigned long)numRows,
3473 (unsigned long)curNumRows
3474 ) );
3475
ca65c044 3476 return false;
f85afd4e
MB
3477 }
3478
3479 if ( numRows > curNumRows - pos )
3480 {
3481 numRows = curNumRows - pos;
3482 }
8f177c8e 3483
f85afd4e
MB
3484 if ( numRows >= curNumRows )
3485 {
d57ad377 3486 m_data.Clear();
f85afd4e
MB
3487 }
3488 else
3489 {
27f35b66 3490 m_data.RemoveAt( pos, numRows );
f85afd4e 3491 }
4db6714b 3492
f85afd4e
MB
3493 if ( GetView() )
3494 {
3495 wxGridTableMessage msg( this,
3496 wxGRIDTABLE_NOTIFY_ROWS_DELETED,
3497 pos,
3498 numRows );
8f177c8e 3499
f85afd4e
MB
3500 GetView()->ProcessTableMessage( msg );
3501 }
3502
ca65c044 3503 return true;
f85afd4e
MB
3504}
3505
3506bool wxGridStringTable::InsertCols( size_t pos, size_t numCols )
3507{
3508 size_t row, col;
3509
3510 size_t curNumRows = m_data.GetCount();
4db6714b
KH
3511 size_t curNumCols = ( curNumRows > 0
3512 ? m_data[0].GetCount()
3513 : ( GetView() ? GetView()->GetNumberCols() : 0 ) );
8f177c8e 3514
f85afd4e
MB
3515 if ( pos >= curNumCols )
3516 {
3517 return AppendCols( numCols );
3518 }
3519
d4175745
VZ
3520 if ( !m_colLabels.IsEmpty() )
3521 {
3522 m_colLabels.Insert( wxEmptyString, pos, numCols );
3523
3524 size_t i;
3525 for ( i = pos; i < pos + numCols; i++ )
3526 m_colLabels[i] = wxGridTableBase::GetColLabelValue( i );
3527 }
3528
3d59537f 3529 for ( row = 0; row < curNumRows; row++ )
f85afd4e 3530 {
3d59537f 3531 for ( col = pos; col < pos + numCols; col++ )
f85afd4e
MB
3532 {
3533 m_data[row].Insert( wxEmptyString, col );
3534 }
3535 }
4db6714b 3536
f85afd4e
MB
3537 if ( GetView() )
3538 {
3539 wxGridTableMessage msg( this,
3540 wxGRIDTABLE_NOTIFY_COLS_INSERTED,
3541 pos,
3542 numCols );
8f177c8e 3543
f85afd4e
MB
3544 GetView()->ProcessTableMessage( msg );
3545 }
3546
ca65c044 3547 return true;
f85afd4e
MB
3548}
3549
3550bool wxGridStringTable::AppendCols( size_t numCols )
3551{
27f35b66 3552 size_t row;
f85afd4e
MB
3553
3554 size_t curNumRows = m_data.GetCount();
2f024384 3555
f6bcfd97 3556#if 0
f85afd4e
MB
3557 if ( !curNumRows )
3558 {
3559 // TODO: something better than this ?
3560 //
f6bcfd97 3561 wxFAIL_MSG( wxT("Unable to append cols to a grid table with no rows.\nCall AppendRows() first") );
ca65c044 3562 return false;
f85afd4e 3563 }
f6bcfd97 3564#endif
8f177c8e 3565
3d59537f 3566 for ( row = 0; row < curNumRows; row++ )
f85afd4e 3567 {
27f35b66 3568 m_data[row].Add( wxEmptyString, numCols );
f85afd4e
MB
3569 }
3570
3571 if ( GetView() )
3572 {
3573 wxGridTableMessage msg( this,
3574 wxGRIDTABLE_NOTIFY_COLS_APPENDED,
3575 numCols );
8f177c8e 3576
f85afd4e
MB
3577 GetView()->ProcessTableMessage( msg );
3578 }
3579
ca65c044 3580 return true;
f85afd4e
MB
3581}
3582
3583bool wxGridStringTable::DeleteCols( size_t pos, size_t numCols )
3584{
27f35b66 3585 size_t row;
f85afd4e
MB
3586
3587 size_t curNumRows = m_data.GetCount();
f6bcfd97
BP
3588 size_t curNumCols = ( curNumRows > 0 ? m_data[0].GetCount() :
3589 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
8f177c8e 3590
f85afd4e
MB
3591 if ( pos >= curNumCols )
3592 {
e91d2033
VZ
3593 wxFAIL_MSG( wxString::Format
3594 (
3595 wxT("Called wxGridStringTable::DeleteCols(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu cols"),
3596 (unsigned long)pos,
3597 (unsigned long)numCols,
3598 (unsigned long)curNumCols
3599 ) );
ca65c044 3600 return false;
f85afd4e
MB
3601 }
3602
d4175745
VZ
3603 int colID;
3604 if ( GetView() )
3605 colID = GetView()->GetColAt( pos );
3606 else
3607 colID = pos;
3608
3609 if ( numCols > curNumCols - colID )
3610 {
3611 numCols = curNumCols - colID;
3612 }
3613
3614 if ( !m_colLabels.IsEmpty() )
f85afd4e 3615 {
d4175745 3616 m_colLabels.RemoveAt( colID, numCols );
f85afd4e
MB
3617 }
3618
3d59537f 3619 for ( row = 0; row < curNumRows; row++ )
f85afd4e
MB
3620 {
3621 if ( numCols >= curNumCols )
3622 {
dcdce64e 3623 m_data[row].Clear();
f85afd4e
MB
3624 }
3625 else
3626 {
d4175745 3627 m_data[row].RemoveAt( colID, numCols );
f85afd4e
MB
3628 }
3629 }
4db6714b 3630
f85afd4e
MB
3631 if ( GetView() )
3632 {
3633 wxGridTableMessage msg( this,
3634 wxGRIDTABLE_NOTIFY_COLS_DELETED,
3635 pos,
3636 numCols );
8f177c8e 3637
f85afd4e
MB
3638 GetView()->ProcessTableMessage( msg );
3639 }
3640
ca65c044 3641 return true;
f85afd4e
MB
3642}
3643
3644wxString wxGridStringTable::GetRowLabelValue( int row )
3645{
3646 if ( row > (int)(m_rowLabels.GetCount()) - 1 )
3647 {
3648 // using default label
3649 //
3650 return wxGridTableBase::GetRowLabelValue( row );
3651 }
3652 else
3653 {
2f024384 3654 return m_rowLabels[row];
f85afd4e
MB
3655 }
3656}
3657
3658wxString wxGridStringTable::GetColLabelValue( int col )
3659{
3660 if ( col > (int)(m_colLabels.GetCount()) - 1 )
3661 {
3662 // using default label
3663 //
3664 return wxGridTableBase::GetColLabelValue( col );
3665 }
3666 else
3667 {
2f024384 3668 return m_colLabels[col];
f85afd4e
MB
3669 }
3670}
3671
3672void wxGridStringTable::SetRowLabelValue( int row, const wxString& value )
3673{
3674 if ( row > (int)(m_rowLabels.GetCount()) - 1 )
3675 {
3676 int n = m_rowLabels.GetCount();
3677 int i;
2f024384 3678
3d59537f 3679 for ( i = n; i <= row; i++ )
f85afd4e
MB
3680 {
3681 m_rowLabels.Add( wxGridTableBase::GetRowLabelValue(i) );
3682 }
3683 }
3684
3685 m_rowLabels[row] = value;
3686}
3687
3688void wxGridStringTable::SetColLabelValue( int col, const wxString& value )
3689{
3690 if ( col > (int)(m_colLabels.GetCount()) - 1 )
3691 {
3692 int n = m_colLabels.GetCount();
3693 int i;
2f024384 3694
3d59537f 3695 for ( i = n; i <= col; i++ )
f85afd4e
MB
3696 {
3697 m_colLabels.Add( wxGridTableBase::GetColLabelValue(i) );
3698 }
3699 }
3700
3701 m_colLabels[col] = value;
3702}
3703
3704
f85afd4e 3705//////////////////////////////////////////////////////////////////////
2d66e025
MB
3706//////////////////////////////////////////////////////////////////////
3707
3708IMPLEMENT_DYNAMIC_CLASS( wxGridRowLabelWindow, wxWindow )
3709
3710BEGIN_EVENT_TABLE( wxGridRowLabelWindow, wxWindow )
3711 EVT_PAINT( wxGridRowLabelWindow::OnPaint )
a9339fe2 3712 EVT_MOUSEWHEEL( wxGridRowLabelWindow::OnMouseWheel )
2d66e025
MB
3713 EVT_MOUSE_EVENTS( wxGridRowLabelWindow::OnMouseEvent )
3714 EVT_KEY_DOWN( wxGridRowLabelWindow::OnKeyDown )
f6bcfd97 3715 EVT_KEY_UP( wxGridRowLabelWindow::OnKeyUp )
a9339fe2 3716 EVT_CHAR( wxGridRowLabelWindow::OnChar )
2d66e025
MB
3717END_EVENT_TABLE()
3718
60ff3b99
VZ
3719wxGridRowLabelWindow::wxGridRowLabelWindow( wxGrid *parent,
3720 wxWindowID id,
2d66e025 3721 const wxPoint &pos, const wxSize &size )
a9339fe2 3722 : wxWindow( parent, id, pos, size, wxWANTS_CHARS | wxBORDER_NONE | wxFULL_REPAINT_ON_RESIZE )
2d66e025
MB
3723{
3724 m_owner = parent;
3725}
3726
aa5e1f75 3727void wxGridRowLabelWindow::OnPaint( wxPaintEvent& WXUNUSED(event) )
2d66e025
MB
3728{
3729 wxPaintDC dc(this);
3730
3731 // NO - don't do this because it will set both the x and y origin
3732 // coords to match the parent scrolled window and we just want to
3733 // set the y coord - MB
3734 //
3735 // m_owner->PrepareDC( dc );
60ff3b99 3736
790ad94f 3737 int x, y;
2d66e025 3738 m_owner->CalcUnscrolledPosition( 0, 0, &x, &y );
615b7e6a
RR
3739 wxPoint pt = dc.GetDeviceOrigin();
3740 dc.SetDeviceOrigin( pt.x, pt.y-y );
60ff3b99 3741
d10f4bf9 3742 wxArrayInt rows = m_owner->CalcRowLabelsExposed( GetUpdateRegion() );
a9339fe2 3743 m_owner->DrawRowLabels( dc, rows );
2d66e025
MB
3744}
3745
2d66e025
MB
3746void wxGridRowLabelWindow::OnMouseEvent( wxMouseEvent& event )
3747{
3748 m_owner->ProcessRowLabelMouseEvent( event );
3749}
3750
b51c3f27
RD
3751void wxGridRowLabelWindow::OnMouseWheel( wxMouseEvent& event )
3752{
a9339fe2 3753 m_owner->GetEventHandler()->ProcessEvent( event );
b51c3f27
RD
3754}
3755
2d66e025
MB
3756// This seems to be required for wxMotif otherwise the mouse
3757// cursor must be in the cell edit control to get key events
3758//
3759void wxGridRowLabelWindow::OnKeyDown( wxKeyEvent& event )
3760{
2f024384
DS
3761 if ( !m_owner->GetEventHandler()->ProcessEvent( event ) )
3762 event.Skip();
2d66e025
MB
3763}
3764
f6bcfd97
BP
3765void wxGridRowLabelWindow::OnKeyUp( wxKeyEvent& event )
3766{
2f024384
DS
3767 if ( !m_owner->GetEventHandler()->ProcessEvent( event ) )
3768 event.Skip();
f6bcfd97
BP
3769}
3770
63e2147c
RD
3771void wxGridRowLabelWindow::OnChar( wxKeyEvent& event )
3772{
2f024384
DS
3773 if ( !m_owner->GetEventHandler()->ProcessEvent( event ) )
3774 event.Skip();
63e2147c
RD
3775}
3776
2d66e025
MB
3777//////////////////////////////////////////////////////////////////////
3778
3779IMPLEMENT_DYNAMIC_CLASS( wxGridColLabelWindow, wxWindow )
3780
3781BEGIN_EVENT_TABLE( wxGridColLabelWindow, wxWindow )
3782 EVT_PAINT( wxGridColLabelWindow::OnPaint )
a9339fe2 3783 EVT_MOUSEWHEEL( wxGridColLabelWindow::OnMouseWheel )
2d66e025
MB
3784 EVT_MOUSE_EVENTS( wxGridColLabelWindow::OnMouseEvent )
3785 EVT_KEY_DOWN( wxGridColLabelWindow::OnKeyDown )
f6bcfd97 3786 EVT_KEY_UP( wxGridColLabelWindow::OnKeyUp )
a9339fe2 3787 EVT_CHAR( wxGridColLabelWindow::OnChar )
2d66e025
MB
3788END_EVENT_TABLE()
3789
60ff3b99
VZ
3790wxGridColLabelWindow::wxGridColLabelWindow( wxGrid *parent,
3791 wxWindowID id,
2d66e025 3792 const wxPoint &pos, const wxSize &size )
a9339fe2 3793 : wxWindow( parent, id, pos, size, wxWANTS_CHARS | wxBORDER_NONE | wxFULL_REPAINT_ON_RESIZE )
2d66e025
MB
3794{
3795 m_owner = parent;
3796}
3797
aa5e1f75 3798void wxGridColLabelWindow::OnPaint( wxPaintEvent& WXUNUSED(event) )
2d66e025
MB
3799{
3800 wxPaintDC dc(this);
3801
3802 // NO - don't do this because it will set both the x and y origin
3803 // coords to match the parent scrolled window and we just want to
3804 // set the x coord - MB
3805 //
3806 // m_owner->PrepareDC( dc );
60ff3b99 3807
790ad94f 3808 int x, y;
2d66e025 3809 m_owner->CalcUnscrolledPosition( 0, 0, &x, &y );
615b7e6a
RR
3810 wxPoint pt = dc.GetDeviceOrigin();
3811 if (GetLayoutDirection() == wxLayout_RightToLeft)
3812 dc.SetDeviceOrigin( pt.x+x, pt.y );
3813 else
3814 dc.SetDeviceOrigin( pt.x-x, pt.y );
2d66e025 3815
d10f4bf9 3816 wxArrayInt cols = m_owner->CalcColLabelsExposed( GetUpdateRegion() );
a9339fe2 3817 m_owner->DrawColLabels( dc, cols );
2d66e025
MB
3818}
3819
2d66e025
MB
3820void wxGridColLabelWindow::OnMouseEvent( wxMouseEvent& event )
3821{
3822 m_owner->ProcessColLabelMouseEvent( event );
3823}
3824
b51c3f27
RD
3825void wxGridColLabelWindow::OnMouseWheel( wxMouseEvent& event )
3826{
a9339fe2 3827 m_owner->GetEventHandler()->ProcessEvent( event );
b51c3f27
RD
3828}
3829
2d66e025
MB
3830// This seems to be required for wxMotif otherwise the mouse
3831// cursor must be in the cell edit control to get key events
3832//
3833void wxGridColLabelWindow::OnKeyDown( wxKeyEvent& event )
3834{
4db6714b
KH
3835 if ( !m_owner->GetEventHandler()->ProcessEvent( event ) )
3836 event.Skip();
2d66e025
MB
3837}
3838
f6bcfd97
BP
3839void wxGridColLabelWindow::OnKeyUp( wxKeyEvent& event )
3840{
4db6714b
KH
3841 if ( !m_owner->GetEventHandler()->ProcessEvent( event ) )
3842 event.Skip();
f6bcfd97
BP
3843}
3844
63e2147c
RD
3845void wxGridColLabelWindow::OnChar( wxKeyEvent& event )
3846{
4db6714b
KH
3847 if ( !m_owner->GetEventHandler()->ProcessEvent( event ) )
3848 event.Skip();
63e2147c 3849}
2d66e025 3850
2d66e025
MB
3851//////////////////////////////////////////////////////////////////////
3852
3853IMPLEMENT_DYNAMIC_CLASS( wxGridCornerLabelWindow, wxWindow )
3854
3855BEGIN_EVENT_TABLE( wxGridCornerLabelWindow, wxWindow )
a9339fe2 3856 EVT_MOUSEWHEEL( wxGridCornerLabelWindow::OnMouseWheel )
2d66e025 3857 EVT_MOUSE_EVENTS( wxGridCornerLabelWindow::OnMouseEvent )
a9339fe2 3858 EVT_PAINT( wxGridCornerLabelWindow::OnPaint )
2d66e025 3859 EVT_KEY_DOWN( wxGridCornerLabelWindow::OnKeyDown )
f6bcfd97 3860 EVT_KEY_UP( wxGridCornerLabelWindow::OnKeyUp )
a9339fe2 3861 EVT_CHAR( wxGridCornerLabelWindow::OnChar )
2d66e025
MB
3862END_EVENT_TABLE()
3863
60ff3b99
VZ
3864wxGridCornerLabelWindow::wxGridCornerLabelWindow( wxGrid *parent,
3865 wxWindowID id,
2d66e025 3866 const wxPoint &pos, const wxSize &size )
56b6cf26 3867 : wxWindow( parent, id, pos, size, wxWANTS_CHARS | wxBORDER_NONE | wxFULL_REPAINT_ON_RESIZE )
2d66e025
MB
3868{
3869 m_owner = parent;
3870}
3871
d2fdd8d2
RR
3872void wxGridCornerLabelWindow::OnPaint( wxPaintEvent& WXUNUSED(event) )
3873{
3874 wxPaintDC dc(this);
b99be8fb 3875
d2fdd8d2
RR
3876 int client_height = 0;
3877 int client_width = 0;
3878 GetClientSize( &client_width, &client_height );
b99be8fb 3879
11850ff3
VZ
3880 // VZ: any reason for this ifdef? (FIXME)
3881#ifdef __WXGTK__
4d1bc39c
RR
3882 wxRect rect;
3883 rect.SetX( 1 );
3884 rect.SetY( 1 );
3885 rect.SetWidth( client_width - 2 );
3886 rect.SetHeight( client_height - 2 );
ec157c8f 3887
4d1bc39c 3888 wxRendererNative::Get().DrawHeaderButton( this, dc, rect, 0 );
11850ff3 3889#else // !__WXGTK__
d4175745 3890 dc.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW), 1, wxSOLID) );
ccdee36f
DS
3891 dc.DrawLine( client_width - 1, client_height - 1, client_width - 1, 0 );
3892 dc.DrawLine( client_width - 1, client_height - 1, 0, client_height - 1 );
d2fdd8d2
RR
3893 dc.DrawLine( 0, 0, client_width, 0 );
3894 dc.DrawLine( 0, 0, 0, client_height );
73145b0e
JS
3895
3896 dc.SetPen( *wxWHITE_PEN );
ccdee36f
DS
3897 dc.DrawLine( 1, 1, client_width - 1, 1 );
3898 dc.DrawLine( 1, 1, 1, client_height - 1 );
3899#endif
d2fdd8d2
RR
3900}
3901
2d66e025
MB
3902void wxGridCornerLabelWindow::OnMouseEvent( wxMouseEvent& event )
3903{
3904 m_owner->ProcessCornerLabelMouseEvent( event );
3905}
3906
b51c3f27
RD
3907void wxGridCornerLabelWindow::OnMouseWheel( wxMouseEvent& event )
3908{
3909 m_owner->GetEventHandler()->ProcessEvent(event);
3910}
3911
2d66e025
MB
3912// This seems to be required for wxMotif otherwise the mouse
3913// cursor must be in the cell edit control to get key events
3914//
3915void wxGridCornerLabelWindow::OnKeyDown( wxKeyEvent& event )
3916{
2f024384
DS
3917 if ( !m_owner->GetEventHandler()->ProcessEvent( event ) )
3918 event.Skip();
2d66e025
MB
3919}
3920
f6bcfd97
BP
3921void wxGridCornerLabelWindow::OnKeyUp( wxKeyEvent& event )
3922{
2f024384
DS
3923 if ( !m_owner->GetEventHandler()->ProcessEvent( event ) )
3924 event.Skip();
f6bcfd97
BP
3925}
3926
63e2147c
RD
3927void wxGridCornerLabelWindow::OnChar( wxKeyEvent& event )
3928{
2f024384
DS
3929 if ( !m_owner->GetEventHandler()->ProcessEvent( event ) )
3930 event.Skip();
63e2147c 3931}
2d66e025 3932
f85afd4e
MB
3933//////////////////////////////////////////////////////////////////////
3934
59ddac01 3935IMPLEMENT_DYNAMIC_CLASS( wxGridWindow, wxWindow )
2d66e025 3936
59ddac01 3937BEGIN_EVENT_TABLE( wxGridWindow, wxWindow )
2d66e025 3938 EVT_PAINT( wxGridWindow::OnPaint )
a9339fe2 3939 EVT_MOUSEWHEEL( wxGridWindow::OnMouseWheel )
2d66e025
MB
3940 EVT_MOUSE_EVENTS( wxGridWindow::OnMouseEvent )
3941 EVT_KEY_DOWN( wxGridWindow::OnKeyDown )
f6bcfd97 3942 EVT_KEY_UP( wxGridWindow::OnKeyUp )
a9339fe2 3943 EVT_CHAR( wxGridWindow::OnChar )
80acaf25
JS
3944 EVT_SET_FOCUS( wxGridWindow::OnFocus )
3945 EVT_KILL_FOCUS( wxGridWindow::OnFocus )
2796cce3 3946 EVT_ERASE_BACKGROUND( wxGridWindow::OnEraseBackground )
2d66e025
MB
3947END_EVENT_TABLE()
3948
60ff3b99
VZ
3949wxGridWindow::wxGridWindow( wxGrid *parent,
3950 wxGridRowLabelWindow *rowLblWin,
2d66e025 3951 wxGridColLabelWindow *colLblWin,
04418332
VZ
3952 wxWindowID id,
3953 const wxPoint &pos,
3954 const wxSize &size )
ccdee36f
DS
3955 : wxWindow(
3956 parent, id, pos, size,
3957 wxWANTS_CHARS | wxBORDER_NONE | wxCLIP_CHILDREN | wxFULL_REPAINT_ON_RESIZE,
3958 wxT("grid window") )
2d66e025
MB
3959{
3960 m_owner = parent;
3961 m_rowLabelWin = rowLblWin;
3962 m_colLabelWin = colLblWin;
2d66e025
MB
3963}
3964
2d66e025
MB
3965void wxGridWindow::OnPaint( wxPaintEvent &WXUNUSED(event) )
3966{
3967 wxPaintDC dc( this );
3968 m_owner->PrepareDC( dc );
796df70a 3969 wxRegion reg = GetUpdateRegion();
ccdee36f
DS
3970 wxGridCellCoordsArray dirtyCells = m_owner->CalcCellsExposed( reg );
3971 m_owner->DrawGridCellArea( dc, dirtyCells );
2f024384 3972
9496deb5 3973#if WXGRID_DRAW_LINES
796df70a
SN
3974 m_owner->DrawAllGridLines( dc, reg );
3975#endif
2f024384 3976
a5777624 3977 m_owner->DrawGridSpace( dc );
ccdee36f 3978 m_owner->DrawHighlight( dc, dirtyCells );
2d66e025
MB
3979}
3980
2d66e025
MB
3981void wxGridWindow::ScrollWindow( int dx, int dy, const wxRect *rect )
3982{
59ddac01 3983 wxWindow::ScrollWindow( dx, dy, rect );
2d66e025
MB
3984 m_rowLabelWin->ScrollWindow( 0, dy, rect );
3985 m_colLabelWin->ScrollWindow( dx, 0, rect );
3986}
3987
2d66e025
MB
3988void wxGridWindow::OnMouseEvent( wxMouseEvent& event )
3989{
33e9fc54
RD
3990 if (event.ButtonDown(wxMOUSE_BTN_LEFT) && FindFocus() != this)
3991 SetFocus();
902725ee 3992
2d66e025
MB
3993 m_owner->ProcessGridCellMouseEvent( event );
3994}
3995
b51c3f27
RD
3996void wxGridWindow::OnMouseWheel( wxMouseEvent& event )
3997{
a9339fe2 3998 m_owner->GetEventHandler()->ProcessEvent( event );
b51c3f27 3999}
2d66e025 4000
f6bcfd97 4001// This seems to be required for wxMotif/wxGTK otherwise the mouse
2d66e025
MB
4002// cursor must be in the cell edit control to get key events
4003//
4004void wxGridWindow::OnKeyDown( wxKeyEvent& event )
4005{
4db6714b
KH
4006 if ( !m_owner->GetEventHandler()->ProcessEvent( event ) )
4007 event.Skip();
2d66e025 4008}
f85afd4e 4009
f6bcfd97
BP
4010void wxGridWindow::OnKeyUp( wxKeyEvent& event )
4011{
4db6714b
KH
4012 if ( !m_owner->GetEventHandler()->ProcessEvent( event ) )
4013 event.Skip();
f6bcfd97 4014}
7c8a8ad5 4015
63e2147c
RD
4016void wxGridWindow::OnChar( wxKeyEvent& event )
4017{
4db6714b
KH
4018 if ( !m_owner->GetEventHandler()->ProcessEvent( event ) )
4019 event.Skip();
63e2147c
RD
4020}
4021
aa5e1f75 4022void wxGridWindow::OnEraseBackground( wxEraseEvent& WXUNUSED(event) )
8dd4f536 4023{
8dd4f536 4024}
025562fe 4025
80acaf25
JS
4026void wxGridWindow::OnFocus(wxFocusEvent& event)
4027{
4028 if ( !m_owner->GetEventHandler()->ProcessEvent( event ) )
4029 event.Skip();
4030}
2d66e025
MB
4031
4032//////////////////////////////////////////////////////////////////////
4033
33188aa4
SN
4034// Internal Helper function for computing row or column from some
4035// (unscrolled) coordinate value, using either
70e8d961 4036// m_defaultRowHeight/m_defaultColWidth or binary search on array
33188aa4
SN
4037// of m_rowBottoms/m_ColRights to speed up the search!
4038
4039// Internal helper macros for simpler use of that function
4040
4041static int CoordToRowOrCol(int coord, int defaultDist, int minDist,
64e15340 4042 const wxArrayInt& BorderArray, int nMax,
a967f048 4043 bool clipToMinMax);
33188aa4 4044
d4175745 4045#define internalXToCol(x) XToCol(x, true)
33188aa4 4046#define internalYToRow(y) CoordToRowOrCol(y, m_defaultRowHeight, \
b8d24d4e 4047 m_minAcceptableRowHeight, \
ca65c044 4048 m_rowBottoms, m_numRows, true)
ccdee36f 4049
33188aa4 4050/////////////////////////////////////////////////////////////////////
07296f0b 4051
b0a877ec 4052#if wxUSE_EXTENDED_RTTI
73c36334
JS
4053WX_DEFINE_FLAGS( wxGridStyle )
4054
3ff066a4 4055wxBEGIN_FLAGS( wxGridStyle )
73c36334
JS
4056 // new style border flags, we put them first to
4057 // use them for streaming out
3ff066a4
SC
4058 wxFLAGS_MEMBER(wxBORDER_SIMPLE)
4059 wxFLAGS_MEMBER(wxBORDER_SUNKEN)
4060 wxFLAGS_MEMBER(wxBORDER_DOUBLE)
4061 wxFLAGS_MEMBER(wxBORDER_RAISED)
4062 wxFLAGS_MEMBER(wxBORDER_STATIC)
4063 wxFLAGS_MEMBER(wxBORDER_NONE)
ca65c044 4064
73c36334 4065 // old style border flags
3ff066a4
SC
4066 wxFLAGS_MEMBER(wxSIMPLE_BORDER)
4067 wxFLAGS_MEMBER(wxSUNKEN_BORDER)
4068 wxFLAGS_MEMBER(wxDOUBLE_BORDER)
4069 wxFLAGS_MEMBER(wxRAISED_BORDER)
4070 wxFLAGS_MEMBER(wxSTATIC_BORDER)
cb0afb26 4071 wxFLAGS_MEMBER(wxBORDER)
73c36334
JS
4072
4073 // standard window styles
3ff066a4
SC
4074 wxFLAGS_MEMBER(wxTAB_TRAVERSAL)
4075 wxFLAGS_MEMBER(wxCLIP_CHILDREN)
4076 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW)
4077 wxFLAGS_MEMBER(wxWANTS_CHARS)
cb0afb26 4078 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE)
ccdee36f 4079 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB)
3ff066a4
SC
4080 wxFLAGS_MEMBER(wxVSCROLL)
4081 wxFLAGS_MEMBER(wxHSCROLL)
73c36334 4082
3ff066a4 4083wxEND_FLAGS( wxGridStyle )
73c36334 4084
b0a877ec
SC
4085IMPLEMENT_DYNAMIC_CLASS_XTI(wxGrid, wxScrolledWindow,"wx/grid.h")
4086
3ff066a4
SC
4087wxBEGIN_PROPERTIES_TABLE(wxGrid)
4088 wxHIDE_PROPERTY( Children )
af498247 4089 wxPROPERTY_FLAGS( WindowStyle , wxGridStyle , long , SetWindowStyleFlag , GetWindowStyleFlag , EMPTY_MACROVALUE, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
3ff066a4 4090wxEND_PROPERTIES_TABLE()
b0a877ec 4091
3ff066a4
SC
4092wxBEGIN_HANDLERS_TABLE(wxGrid)
4093wxEND_HANDLERS_TABLE()
b0a877ec 4094
ca65c044 4095wxCONSTRUCTOR_5( wxGrid , wxWindow* , Parent , wxWindowID , Id , wxPoint , Position , wxSize , Size , long , WindowStyle )
b0a877ec
SC
4096
4097/*
ccdee36f 4098 TODO : Expose more information of a list's layout, etc. via appropriate objects (e.g., NotebookPageInfo)
b0a877ec
SC
4099*/
4100#else
2d66e025 4101IMPLEMENT_DYNAMIC_CLASS( wxGrid, wxScrolledWindow )
b0a877ec 4102#endif
2d66e025
MB
4103
4104BEGIN_EVENT_TABLE( wxGrid, wxScrolledWindow )
f85afd4e
MB
4105 EVT_PAINT( wxGrid::OnPaint )
4106 EVT_SIZE( wxGrid::OnSize )
f85afd4e 4107 EVT_KEY_DOWN( wxGrid::OnKeyDown )
f6bcfd97 4108 EVT_KEY_UP( wxGrid::OnKeyUp )
63e2147c 4109 EVT_CHAR ( wxGrid::OnChar )
2796cce3 4110 EVT_ERASE_BACKGROUND( wxGrid::OnEraseBackground )
f85afd4e 4111END_EVENT_TABLE()
8f177c8e 4112
b0a877ec
SC
4113wxGrid::wxGrid()
4114{
4115 // in order to make sure that a size event is not
4116 // trigerred in a unfinished state
2f024384
DS
4117 m_cornerLabelWin = NULL;
4118 m_rowLabelWin = NULL;
4119 m_colLabelWin = NULL;
4120 m_gridWin = NULL;
b0a877ec
SC
4121}
4122
2d66e025
MB
4123wxGrid::wxGrid( wxWindow *parent,
4124 wxWindowID id,
4125 const wxPoint& pos,
4126 const wxSize& size,
4127 long style,
4128 const wxString& name )
ebd773c6 4129 : wxScrolledWindow( parent, id, pos, size, (style | wxWANTS_CHARS), name ),
af547d51
VZ
4130 m_colMinWidths(GRID_HASH_SIZE),
4131 m_rowMinHeights(GRID_HASH_SIZE)
2d66e025
MB
4132{
4133 Create();
5a9b107d 4134 SetBestFittingSize(size);
58dd5b3b
MB
4135}
4136
b0a877ec
SC
4137bool wxGrid::Create(wxWindow *parent, wxWindowID id,
4138 const wxPoint& pos, const wxSize& size,
4139 long style, const wxString& name)
4140{
4141 if (!wxScrolledWindow::Create(parent, id, pos, size,
c2f5b920 4142 style | wxWANTS_CHARS, name))
ca65c044 4143 return false;
b0a877ec 4144
2f024384
DS
4145 m_colMinWidths = wxLongToLongHashMap(GRID_HASH_SIZE);
4146 m_rowMinHeights = wxLongToLongHashMap(GRID_HASH_SIZE);
b0a877ec 4147
2f024384 4148 Create();
5a9b107d 4149 SetBestFittingSize(size);
b0a877ec 4150
ca65c044 4151 return true;
b0a877ec
SC
4152}
4153
58dd5b3b
MB
4154wxGrid::~wxGrid()
4155{
606b005f
JS
4156 // Must do this or ~wxScrollHelper will pop the wrong event handler
4157 SetTargetWindow(this);
0a976765 4158 ClearAttrCache();
39bcce60 4159 wxSafeDecRef(m_defaultCellAttr);
0a976765
VZ
4160
4161#ifdef DEBUG_ATTR_CACHE
4162 size_t total = gs_nAttrCacheHits + gs_nAttrCacheMisses;
4163 wxPrintf(_T("wxGrid attribute cache statistics: "
4164 "total: %u, hits: %u (%u%%)\n"),
4165 total, gs_nAttrCacheHits,
4166 total ? (gs_nAttrCacheHits*100) / total : 0);
4167#endif
4168
2796cce3
RD
4169 if (m_ownTable)
4170 delete m_table;
f2d76237
RD
4171
4172 delete m_typeRegistry;
b5808881 4173 delete m_selection;
58dd5b3b
MB
4174}
4175
58dd5b3b
MB
4176//
4177// ----- internal init and update functions
4178//
4179
9950649c
RD
4180// NOTE: If using the default visual attributes works everywhere then this can
4181// be removed as well as the #else cases below.
4182#define _USE_VISATTR 0
4183
58dd5b3b 4184void wxGrid::Create()
f0102d2a 4185{
3d59537f
DS
4186 // set to true by CreateGrid
4187 m_created = false;
4634a5d6 4188
3d59537f
DS
4189 // create the type registry
4190 m_typeRegistry = new wxGridTypeRegistry;
4191 m_selection = NULL;
4192
4193 m_table = (wxGridTableBase *) NULL;
4194 m_ownTable = false;
2c9a89e0 4195
ca65c044 4196 m_cellEditCtrlEnabled = false;
4634a5d6 4197
ccd970b1 4198 m_defaultCellAttr = new wxGridCellAttr();
f2d76237
RD
4199
4200 // Set default cell attributes
ccd970b1 4201 m_defaultCellAttr->SetDefAttr(m_defaultCellAttr);
19d7140e 4202 m_defaultCellAttr->SetKind(wxGridCellAttr::Default);
f2d76237 4203 m_defaultCellAttr->SetFont(GetFont());
4c7277db 4204 m_defaultCellAttr->SetAlignment(wxALIGN_LEFT, wxALIGN_TOP);
9950649c
RD
4205 m_defaultCellAttr->SetRenderer(new wxGridCellStringRenderer);
4206 m_defaultCellAttr->SetEditor(new wxGridCellTextEditor);
4207
4208#if _USE_VISATTR
4209 wxVisualAttributes gva = wxListBox::GetClassDefaultAttributes();
4210 wxVisualAttributes lva = wxPanel::GetClassDefaultAttributes();
4211
4212 m_defaultCellAttr->SetTextColour(gva.colFg);
4213 m_defaultCellAttr->SetBackgroundColour(gva.colBg);
ca65c044 4214
9950649c 4215#else
f2d76237 4216 m_defaultCellAttr->SetTextColour(
a756f210 4217 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT));
f2d76237 4218 m_defaultCellAttr->SetBackgroundColour(
a756f210 4219 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW));
9950649c 4220#endif
2796cce3 4221
4634a5d6
MB
4222 m_numRows = 0;
4223 m_numCols = 0;
4224 m_currentCellCoords = wxGridNoCellCoords;
b99be8fb 4225
18f9565d
MB
4226 m_rowLabelWidth = WXGRID_DEFAULT_ROW_LABEL_WIDTH;
4227 m_colLabelHeight = WXGRID_DEFAULT_COL_LABEL_HEIGHT;
2d66e025 4228
f2d76237 4229 // subwindow components that make up the wxGrid
60ff3b99 4230 m_rowLabelWin = new wxGridRowLabelWindow( this,
ca65c044 4231 wxID_ANY,
18f9565d
MB
4232 wxDefaultPosition,
4233 wxDefaultSize );
2d66e025
MB
4234
4235 m_colLabelWin = new wxGridColLabelWindow( this,
ca65c044 4236 wxID_ANY,
18f9565d
MB
4237 wxDefaultPosition,
4238 wxDefaultSize );
60ff3b99 4239
3d59537f
DS
4240 m_cornerLabelWin = new wxGridCornerLabelWindow( this,
4241 wxID_ANY,
4242 wxDefaultPosition,
4243 wxDefaultSize );
4244
60ff3b99
VZ
4245 m_gridWin = new wxGridWindow( this,
4246 m_rowLabelWin,
4247 m_colLabelWin,
ca65c044 4248 wxID_ANY,
18f9565d 4249 wxDefaultPosition,
2d66e025
MB
4250 wxDefaultSize );
4251
4252 SetTargetWindow( m_gridWin );
6f36917b 4253
9950649c
RD
4254#if _USE_VISATTR
4255 wxColour gfg = gva.colFg;
4256 wxColour gbg = gva.colBg;
4257 wxColour lfg = lva.colFg;
4258 wxColour lbg = lva.colBg;
4259#else
4260 wxColour gfg = wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT );
4261 wxColour gbg = wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOW );
4262 wxColour lfg = wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT );
4263 wxColour lbg = wxSystemSettings::GetColour( wxSYS_COLOUR_BTNFACE );
4264#endif
2f024384 4265
fa47d7a7
VS
4266 m_cornerLabelWin->SetOwnForegroundColour(lfg);
4267 m_cornerLabelWin->SetOwnBackgroundColour(lbg);
4268 m_rowLabelWin->SetOwnForegroundColour(lfg);
4269 m_rowLabelWin->SetOwnBackgroundColour(lbg);
4270 m_colLabelWin->SetOwnForegroundColour(lfg);
4271 m_colLabelWin->SetOwnBackgroundColour(lbg);
4272
4273 m_gridWin->SetOwnForegroundColour(gfg);
4274 m_gridWin->SetOwnBackgroundColour(gbg);
ca65c044 4275
6f36917b 4276 Init();
2d66e025 4277}
f85afd4e 4278
b5808881
SN
4279bool wxGrid::CreateGrid( int numRows, int numCols,
4280 wxGrid::wxGridSelectionModes selmode )
2d66e025 4281{
f6bcfd97 4282 wxCHECK_MSG( !m_created,
ca65c044 4283 false,
f6bcfd97
BP
4284 wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
4285
4286 m_numRows = numRows;
4287 m_numCols = numCols;
4288
4289 m_table = new wxGridStringTable( m_numRows, m_numCols );
4290 m_table->SetView( this );
ca65c044 4291 m_ownTable = true;
f6bcfd97 4292 m_selection = new wxGridSelection( this, selmode );
6f36917b
VZ
4293
4294 CalcDimensions();
4295
ca65c044 4296 m_created = true;
2d66e025 4297
2796cce3
RD
4298 return m_created;
4299}
4300
f1567cdd
SN
4301void wxGrid::SetSelectionMode(wxGrid::wxGridSelectionModes selmode)
4302{
6f36917b
VZ
4303 wxCHECK_RET( m_created,
4304 wxT("Called wxGrid::SetSelectionMode() before calling CreateGrid()") );
4305
4306 m_selection->SetSelectionMode( selmode );
f1567cdd
SN
4307}
4308
aa5b8857
SN
4309wxGrid::wxGridSelectionModes wxGrid::GetSelectionMode() const
4310{
4311 wxCHECK_MSG( m_created, wxGrid::wxGridSelectCells,
4312 wxT("Called wxGrid::GetSelectionMode() before calling CreateGrid()") );
4313
4314 return m_selection->GetSelectionMode();
4315}
4316
043d16b2
SN
4317bool wxGrid::SetTable( wxGridTableBase *table, bool takeOwnership,
4318 wxGrid::wxGridSelectionModes selmode )
2796cce3
RD
4319{
4320 if ( m_created )
4321 {
3e13956a 4322 // stop all processing
ca65c044 4323 m_created = false;
86c7378f
SN
4324
4325 if (m_ownTable)
4326 {
5b061713
DS
4327 wxGridTableBase *t = m_table;
4328 m_table = NULL;
3e13956a 4329 delete t;
86c7378f 4330 }
2f024384 4331
3e13956a
RD
4332 delete m_selection;
4333
5b061713
DS
4334 m_table = NULL;
4335 m_selection = NULL;
2f024384
DS
4336 m_numRows = 0;
4337 m_numCols = 0;
233a54f6 4338 }
2f024384 4339
233a54f6 4340 if (table)
2796cce3
RD
4341 {
4342 m_numRows = table->GetNumberRows();
4343 m_numCols = table->GetNumberCols();
4344
4345 m_table = table;
4346 m_table->SetView( this );
8fc856de 4347 m_ownTable = takeOwnership;
043d16b2 4348 m_selection = new wxGridSelection( this, selmode );
6f36917b
VZ
4349
4350 CalcDimensions();
4351
ca65c044 4352 m_created = true;
2d66e025 4353 }
f85afd4e 4354
2d66e025 4355 return m_created;
f85afd4e
MB
4356}
4357
4358void wxGrid::Init()
4359{
f85afd4e
MB
4360 m_rowLabelWidth = WXGRID_DEFAULT_ROW_LABEL_WIDTH;
4361 m_colLabelHeight = WXGRID_DEFAULT_COL_LABEL_HEIGHT;
4362
60ff3b99
VZ
4363 if ( m_rowLabelWin )
4364 {
4365 m_labelBackgroundColour = m_rowLabelWin->GetBackgroundColour();
4366 }
4367 else
4368 {
b0fa2187 4369 m_labelBackgroundColour = *wxWHITE;
60ff3b99
VZ
4370 }
4371
b0fa2187 4372 m_labelTextColour = *wxBLACK;
f85afd4e 4373
0a976765
VZ
4374 // init attr cache
4375 m_attrCache.row = -1;
2b5f62a0
VZ
4376 m_attrCache.col = -1;
4377 m_attrCache.attr = NULL;
0a976765 4378
f85afd4e
MB
4379 // TODO: something better than this ?
4380 //
4381 m_labelFont = this->GetFont();
52d6f640 4382 m_labelFont.SetWeight( wxBOLD );
8f177c8e 4383
73145b0e 4384 m_rowLabelHorizAlign = wxALIGN_CENTRE;
4c7277db 4385 m_rowLabelVertAlign = wxALIGN_CENTRE;
f85afd4e 4386
4c7277db 4387 m_colLabelHorizAlign = wxALIGN_CENTRE;
73145b0e 4388 m_colLabelVertAlign = wxALIGN_CENTRE;
d43851f7 4389 m_colLabelTextOrientation = wxHORIZONTAL;
f85afd4e 4390
f85afd4e 4391 m_defaultColWidth = WXGRID_DEFAULT_COL_WIDTH;
1f1ce288
MB
4392 m_defaultRowHeight = m_gridWin->GetCharHeight();
4393
b8d24d4e
RG
4394 m_minAcceptableColWidth = WXGRID_MIN_COL_WIDTH;
4395 m_minAcceptableRowHeight = WXGRID_MIN_ROW_HEIGHT;
4396
d2fdd8d2 4397#if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl()
1f1ce288
MB
4398 m_defaultRowHeight += 8;
4399#else
4400 m_defaultRowHeight += 4;
4401#endif
4402
73145b0e 4403 m_gridLineColour = wxColour( 192,192,192 );
ca65c044 4404 m_gridLinesEnabled = true;
73145b0e 4405 m_cellHighlightColour = *wxBLACK;
bf7945ce 4406 m_cellHighlightPenWidth = 2;
d2520c85 4407 m_cellHighlightROPenWidth = 1;
8f177c8e 4408
d4175745
VZ
4409 m_canDragColMove = false;
4410
58dd5b3b 4411 m_cursorMode = WXGRID_CURSOR_SELECT_CELL;
e2b42eeb 4412 m_winCapture = (wxWindow *)NULL;
ca65c044
WS
4413 m_canDragRowSize = true;
4414 m_canDragColSize = true;
4415 m_canDragGridSize = true;
79dbea21 4416 m_canDragCell = false;
f85afd4e
MB
4417 m_dragLastPos = -1;
4418 m_dragRowOrCol = -1;
ca65c044 4419 m_isDragging = false;
07296f0b 4420 m_startDragPos = wxDefaultPosition;
f85afd4e 4421
ca65c044 4422 m_waitForSlowClick = false;
025562fe 4423
f85afd4e
MB
4424 m_rowResizeCursor = wxCursor( wxCURSOR_SIZENS );
4425 m_colResizeCursor = wxCursor( wxCURSOR_SIZEWE );
4426
4427 m_currentCellCoords = wxGridNoCellCoords;
f85afd4e 4428
b524b5c6
VZ
4429 ClearSelection();
4430
d43851f7
JS
4431 m_selectionBackground = wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT);
4432 m_selectionForeground = wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT);
8f177c8e 4433
ca65c044 4434 m_editable = true; // default for whole grid
f85afd4e 4435
ca65c044 4436 m_inOnKeyDown = false;
2d66e025 4437 m_batchCount = 0;
3c79cf49 4438
266e8367 4439 m_extraWidth =
526dbb95 4440 m_extraHeight = 0;
608754c4
JS
4441
4442 m_scrollLineX = GRID_SCROLL_LINE_X;
4443 m_scrollLineY = GRID_SCROLL_LINE_Y;
7c1cb261
VZ
4444}
4445
4446// ----------------------------------------------------------------------------
4447// the idea is to call these functions only when necessary because they create
4448// quite big arrays which eat memory mostly unnecessary - in particular, if
4449// default widths/heights are used for all rows/columns, we may not use these
4450// arrays at all
4451//
4452// with some extra code, it should be possible to only store the
4453// widths/heights different from default ones but this will be done later...
4454// ----------------------------------------------------------------------------
4455
4456void wxGrid::InitRowHeights()
4457{
4458 m_rowHeights.Empty();
4459 m_rowBottoms.Empty();
4460
4461 m_rowHeights.Alloc( m_numRows );
4462 m_rowBottoms.Alloc( m_numRows );
4463
4464 int rowBottom = 0;
2b5f62a0 4465
27f35b66
SN
4466 m_rowHeights.Add( m_defaultRowHeight, m_numRows );
4467
3d59537f 4468 for ( int i = 0; i < m_numRows; i++ )
7c1cb261 4469 {
7c1cb261
VZ
4470 rowBottom += m_defaultRowHeight;
4471 m_rowBottoms.Add( rowBottom );
4472 }
4473}
4474
4475void wxGrid::InitColWidths()
4476{
4477 m_colWidths.Empty();
4478 m_colRights.Empty();
4479
4480 m_colWidths.Alloc( m_numCols );
4481 m_colRights.Alloc( m_numCols );
4482 int colRight = 0;
27f35b66
SN
4483
4484 m_colWidths.Add( m_defaultColWidth, m_numCols );
4485
3d59537f 4486 for ( int i = 0; i < m_numCols; i++ )
7c1cb261 4487 {
d4175745 4488 colRight = ( GetColPos( i ) + 1 ) * m_defaultColWidth;
7c1cb261
VZ
4489 m_colRights.Add( colRight );
4490 }
4491}
4492
4493int wxGrid::GetColWidth(int col) const
4494{
4495 return m_colWidths.IsEmpty() ? m_defaultColWidth : m_colWidths[col];
4496}
4497
4498int wxGrid::GetColLeft(int col) const
4499{
d4175745 4500 return m_colRights.IsEmpty() ? GetColPos( col ) * m_defaultColWidth
7c1cb261
VZ
4501 : m_colRights[col] - m_colWidths[col];
4502}
4503
4504int wxGrid::GetColRight(int col) const
4505{
d4175745 4506 return m_colRights.IsEmpty() ? (GetColPos( col ) + 1) * m_defaultColWidth
7c1cb261
VZ
4507 : m_colRights[col];
4508}
4509
4510int wxGrid::GetRowHeight(int row) const
4511{
4512 return m_rowHeights.IsEmpty() ? m_defaultRowHeight : m_rowHeights[row];
4513}
2d66e025 4514
7c1cb261
VZ
4515int wxGrid::GetRowTop(int row) const
4516{
4517 return m_rowBottoms.IsEmpty() ? row * m_defaultRowHeight
4518 : m_rowBottoms[row] - m_rowHeights[row];
f85afd4e
MB
4519}
4520
7c1cb261
VZ
4521int wxGrid::GetRowBottom(int row) const
4522{
4523 return m_rowBottoms.IsEmpty() ? (row + 1) * m_defaultRowHeight
4524 : m_rowBottoms[row];
4525}
f85afd4e
MB
4526
4527void wxGrid::CalcDimensions()
4528{
f85afd4e 4529 int cw, ch;
2d66e025 4530 GetClientSize( &cw, &ch );
f85afd4e 4531
faec5a43 4532 if ( m_rowLabelWin->IsShown() )
ae1d0c6c
VZ
4533 cw -= m_rowLabelWidth;
4534 if ( m_colLabelWin->IsShown() )
faec5a43 4535 ch -= m_colLabelHeight;
60ff3b99 4536
faec5a43 4537 // grid total size
d4175745 4538 int w = m_numCols > 0 ? GetColRight(GetColAt( m_numCols - 1 )) + m_extraWidth + 1 : 0;
faec5a43
SN
4539 int h = m_numRows > 0 ? GetRowBottom(m_numRows - 1) + m_extraHeight + 1 : 0;
4540
73145b0e 4541 // take into account editor if shown
4db6714b 4542 if ( IsCellEditControlShown() )
73145b0e 4543 {
3d59537f
DS
4544 int w2, h2;
4545 int r = m_currentCellCoords.GetRow();
4546 int c = m_currentCellCoords.GetCol();
4547 int x = GetColLeft(c);
4548 int y = GetRowTop(r);
4549
4550 // how big is the editor
4551 wxGridCellAttr* attr = GetCellAttr(r, c);
4552 wxGridCellEditor* editor = attr->GetEditor(this, r, c);
4553 editor->GetControl()->GetSize(&w2, &h2);
4554 w2 += x;
4555 h2 += y;
4556 if ( w2 > w )
4557 w = w2;
4558 if ( h2 > h )
4559 h = h2;
4560 editor->DecRef();
4561 attr->DecRef();
73145b0e
JS
4562 }
4563
faec5a43
SN
4564 // preserve (more or less) the previous position
4565 int x, y;
4566 GetViewStart( &x, &y );
97a9929e 4567
c92ed9f7 4568 // ensure the position is valid for the new scroll ranges
7b519e5e 4569 if ( x >= w )
c92ed9f7 4570 x = wxMax( w - 1, 0 );
7b519e5e 4571 if ( y >= h )
c92ed9f7 4572 y = wxMax( h - 1, 0 );
faec5a43
SN
4573
4574 // do set scrollbar parameters
675a9c0d 4575 SetScrollbars( m_scrollLineX, m_scrollLineY,
97a9929e
VZ
4576 GetScrollX(w), GetScrollY(h), x, y,
4577 GetBatchCount() != 0);
12314291
VZ
4578
4579 // if our OnSize() hadn't been called (it would if we have scrollbars), we
4580 // still must reposition the children
4581 CalcWindowSizes();
f85afd4e
MB
4582}
4583
7807d81c
MB
4584void wxGrid::CalcWindowSizes()
4585{
b0a877ec
SC
4586 // escape if the window is has not been fully created yet
4587
4588 if ( m_cornerLabelWin == NULL )
2f024384 4589 return;
b0a877ec 4590
7807d81c
MB
4591 int cw, ch;
4592 GetClientSize( &cw, &ch );
b99be8fb 4593
6d308072 4594 if ( m_cornerLabelWin && m_cornerLabelWin->IsShown() )
7807d81c
MB
4595 m_cornerLabelWin->SetSize( 0, 0, m_rowLabelWidth, m_colLabelHeight );
4596
c2f5b920
DS
4597 if ( m_colLabelWin && m_colLabelWin->IsShown() )
4598 m_colLabelWin->SetSize( m_rowLabelWidth, 0, cw - m_rowLabelWidth, m_colLabelHeight );
7807d81c 4599
6d308072 4600 if ( m_rowLabelWin && m_rowLabelWin->IsShown() )
c2f5b920 4601 m_rowLabelWin->SetSize( 0, m_colLabelHeight, m_rowLabelWidth, ch - m_colLabelHeight );
7807d81c 4602
6d308072 4603 if ( m_gridWin && m_gridWin->IsShown() )
c2f5b920 4604 m_gridWin->SetSize( m_rowLabelWidth, m_colLabelHeight, cw - m_rowLabelWidth, ch - m_colLabelHeight );
7807d81c
MB
4605}
4606
3d59537f
DS
4607// this is called when the grid table sends a message
4608// to indicate that it has been redimensioned
f85afd4e
MB
4609//
4610bool wxGrid::Redimension( wxGridTableMessage& msg )
4611{
4612 int i;
ca65c044 4613 bool result = false;
8f177c8e 4614
a6794685
SN
4615 // Clear the attribute cache as the attribute might refer to a different
4616 // cell than stored in the cache after adding/removing rows/columns.
4617 ClearAttrCache();
2f024384 4618
7e48d7d9
SN
4619 // By the same reasoning, the editor should be dismissed if columns are
4620 // added or removed. And for consistency, it should IMHO always be
4621 // removed, not only if the cell "underneath" it actually changes.
4622 // For now, I intentionally do not save the editor's content as the
4623 // cell it might want to save that stuff to might no longer exist.
bca7bfc8 4624 HideCellEditControl();
2f024384 4625
f6bcfd97 4626#if 0
7c1cb261
VZ
4627 // if we were using the default widths/heights so far, we must change them
4628 // now
4629 if ( m_colWidths.IsEmpty() )
4630 {
4631 InitColWidths();
4632 }
4633
4634 if ( m_rowHeights.IsEmpty() )
4635 {
4636 InitRowHeights();
4637 }
f6bcfd97 4638#endif
7c1cb261 4639
f85afd4e
MB
4640 switch ( msg.GetId() )
4641 {
4642 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED:
4643 {
4644 size_t pos = msg.GetCommandInt();
4645 int numRows = msg.GetCommandInt2();
f6bcfd97 4646
f85afd4e 4647 m_numRows += numRows;
2d66e025 4648
f6bcfd97
BP
4649 if ( !m_rowHeights.IsEmpty() )
4650 {
27f35b66
SN
4651 m_rowHeights.Insert( m_defaultRowHeight, pos, numRows );
4652 m_rowBottoms.Insert( 0, pos, numRows );
f6bcfd97
BP
4653
4654 int bottom = 0;
2f024384
DS
4655 if ( pos > 0 )
4656 bottom = m_rowBottoms[pos - 1];
60ff3b99 4657
3d59537f 4658 for ( i = pos; i < m_numRows; i++ )
f6bcfd97
BP
4659 {
4660 bottom += m_rowHeights[i];
4661 m_rowBottoms[i] = bottom;
4662 }
4663 }
2f024384 4664
f6bcfd97
BP
4665 if ( m_currentCellCoords == wxGridNoCellCoords )
4666 {
4667 // if we have just inserted cols into an empty grid the current
4668 // cell will be undefined...
4669 //
4670 SetCurrentCell( 0, 0 );
4671 }
3f3dc2ef
VZ
4672
4673 if ( m_selection )
4674 m_selection->UpdateRows( pos, numRows );
f6bcfd97
BP
4675 wxGridCellAttrProvider * attrProvider = m_table->GetAttrProvider();
4676 if (attrProvider)
4677 attrProvider->UpdateAttrRows( pos, numRows );
4678
4679 if ( !GetBatchCount() )
2d66e025 4680 {
f6bcfd97
BP
4681 CalcDimensions();
4682 m_rowLabelWin->Refresh();
2d66e025 4683 }
f85afd4e 4684 }
ca65c044 4685 result = true;
f6bcfd97 4686 break;
f85afd4e
MB
4687
4688 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED:
4689 {
4690 int numRows = msg.GetCommandInt();
2d66e025 4691 int oldNumRows = m_numRows;
f85afd4e 4692 m_numRows += numRows;
2d66e025 4693
f6bcfd97
BP
4694 if ( !m_rowHeights.IsEmpty() )
4695 {
27f35b66
SN
4696 m_rowHeights.Add( m_defaultRowHeight, numRows );
4697 m_rowBottoms.Add( 0, numRows );
60ff3b99 4698
f6bcfd97 4699 int bottom = 0;
2f024384
DS
4700 if ( oldNumRows > 0 )
4701 bottom = m_rowBottoms[oldNumRows - 1];
f6bcfd97 4702
3d59537f 4703 for ( i = oldNumRows; i < m_numRows; i++ )
f6bcfd97
BP
4704 {
4705 bottom += m_rowHeights[i];
4706 m_rowBottoms[i] = bottom;
4707 }
4708 }
2f024384 4709
f6bcfd97
BP
4710 if ( m_currentCellCoords == wxGridNoCellCoords )
4711 {
4712 // if we have just inserted cols into an empty grid the current
4713 // cell will be undefined...
4714 //
4715 SetCurrentCell( 0, 0 );
4716 }
2f024384 4717
f6bcfd97 4718 if ( !GetBatchCount() )
2d66e025 4719 {
f6bcfd97
BP
4720 CalcDimensions();
4721 m_rowLabelWin->Refresh();
2d66e025 4722 }
f85afd4e 4723 }
ca65c044 4724 result = true;
f6bcfd97 4725 break;
f85afd4e
MB
4726
4727 case wxGRIDTABLE_NOTIFY_ROWS_DELETED:
4728 {
4729 size_t pos = msg.GetCommandInt();
4730 int numRows = msg.GetCommandInt2();
f85afd4e
MB
4731 m_numRows -= numRows;
4732
f6bcfd97 4733 if ( !m_rowHeights.IsEmpty() )
f85afd4e 4734 {
27f35b66
SN
4735 m_rowHeights.RemoveAt( pos, numRows );
4736 m_rowBottoms.RemoveAt( pos, numRows );
2d66e025
MB
4737
4738 int h = 0;
3d59537f 4739 for ( i = 0; i < m_numRows; i++ )
2d66e025
MB
4740 {
4741 h += m_rowHeights[i];
4742 m_rowBottoms[i] = h;
4743 }
f85afd4e 4744 }
3d59537f 4745
f6bcfd97
BP
4746 if ( !m_numRows )
4747 {
4748 m_currentCellCoords = wxGridNoCellCoords;
4749 }
4750 else
4751 {
4752 if ( m_currentCellCoords.GetRow() >= m_numRows )
4753 m_currentCellCoords.Set( 0, 0 );
4754 }
3f3dc2ef
VZ
4755
4756 if ( m_selection )
4757 m_selection->UpdateRows( pos, -((int)numRows) );
f6bcfd97 4758 wxGridCellAttrProvider * attrProvider = m_table->GetAttrProvider();
4db6714b
KH
4759 if (attrProvider)
4760 {
f6bcfd97 4761 attrProvider->UpdateAttrRows( pos, -((int)numRows) );
3d59537f 4762
84912ef8
RD
4763// ifdef'd out following patch from Paul Gammans
4764#if 0
3ca6a5f0 4765 // No need to touch column attributes, unless we
f6bcfd97
BP
4766 // removed _all_ rows, in this case, we remove
4767 // all column attributes.
4768 // I hate to do this here, but the
4769 // needed data is not available inside UpdateAttrRows.
4770 if ( !GetNumberRows() )
4771 attrProvider->UpdateAttrCols( 0, -GetNumberCols() );
84912ef8 4772#endif
f6bcfd97 4773 }
ccdee36f 4774
f6bcfd97
BP
4775 if ( !GetBatchCount() )
4776 {
4777 CalcDimensions();
4778 m_rowLabelWin->Refresh();
4779 }
f85afd4e 4780 }
ca65c044 4781 result = true;
f6bcfd97 4782 break;
f85afd4e
MB
4783
4784 case wxGRIDTABLE_NOTIFY_COLS_INSERTED:
4785 {
4786 size_t pos = msg.GetCommandInt();
4787 int numCols = msg.GetCommandInt2();
f85afd4e 4788 m_numCols += numCols;
2d66e025 4789
d4175745
VZ
4790 if ( !m_colAt.IsEmpty() )
4791 {
4792 //Shift the column IDs
4793 int i;
4794 for ( i = 0; i < m_numCols - numCols; i++ )
4795 {
4796 if ( m_colAt[i] >= (int)pos )
4797 m_colAt[i] += numCols;
4798 }
4799
4800 m_colAt.Insert( pos, pos, numCols );
4801
4802 //Set the new columns' positions
4803 for ( i = pos + 1; i < (int)pos + numCols; i++ )
4804 {
4805 m_colAt[i] = i;
4806 }
4807 }
4808
f6bcfd97
BP
4809 if ( !m_colWidths.IsEmpty() )
4810 {
27f35b66
SN
4811 m_colWidths.Insert( m_defaultColWidth, pos, numCols );
4812 m_colRights.Insert( 0, pos, numCols );
f6bcfd97
BP
4813
4814 int right = 0;
2f024384 4815 if ( pos > 0 )
d4175745 4816 right = m_colRights[GetColAt( pos - 1 )];
60ff3b99 4817
d4175745
VZ
4818 int colPos;
4819 for ( colPos = pos; colPos < m_numCols; colPos++ )
f6bcfd97 4820 {
d4175745
VZ
4821 i = GetColAt( colPos );
4822
f6bcfd97
BP
4823 right += m_colWidths[i];
4824 m_colRights[i] = right;
4825 }
4826 }
2f024384 4827
f6bcfd97 4828 if ( m_currentCellCoords == wxGridNoCellCoords )
2d66e025 4829 {
f6bcfd97
BP
4830 // if we have just inserted cols into an empty grid the current
4831 // cell will be undefined...
4832 //
4833 SetCurrentCell( 0, 0 );
2d66e025 4834 }
3f3dc2ef
VZ
4835
4836 if ( m_selection )
4837 m_selection->UpdateCols( pos, numCols );
f6bcfd97
BP
4838 wxGridCellAttrProvider * attrProvider = m_table->GetAttrProvider();
4839 if (attrProvider)
4840 attrProvider->UpdateAttrCols( pos, numCols );
4841 if ( !GetBatchCount() )
4842 {
4843 CalcDimensions();
4844 m_colLabelWin->Refresh();
4845 }
f85afd4e 4846 }
ca65c044 4847 result = true;
f6bcfd97 4848 break;
f85afd4e
MB
4849
4850 case wxGRIDTABLE_NOTIFY_COLS_APPENDED:
4851 {
4852 int numCols = msg.GetCommandInt();
2d66e025 4853 int oldNumCols = m_numCols;
f85afd4e 4854 m_numCols += numCols;
d4175745
VZ
4855
4856 if ( !m_colAt.IsEmpty() )
4857 {
4858 m_colAt.Add( 0, numCols );
4859
4860 //Set the new columns' positions
4861 int i;
4862 for ( i = oldNumCols; i < m_numCols; i++ )
4863 {
4864 m_colAt[i] = i;
4865 }
4866 }
4867
f6bcfd97
BP
4868 if ( !m_colWidths.IsEmpty() )
4869 {
27f35b66
SN
4870 m_colWidths.Add( m_defaultColWidth, numCols );
4871 m_colRights.Add( 0, numCols );
2d66e025 4872
f6bcfd97 4873 int right = 0;
2f024384 4874 if ( oldNumCols > 0 )
d4175745 4875 right = m_colRights[GetColAt( oldNumCols - 1 )];
60ff3b99 4876
d4175745
VZ
4877 int colPos;
4878 for ( colPos = oldNumCols; colPos < m_numCols; colPos++ )
f6bcfd97 4879 {
d4175745
VZ
4880 i = GetColAt( colPos );
4881
f6bcfd97
BP
4882 right += m_colWidths[i];
4883 m_colRights[i] = right;
4884 }
4885 }
2f024384 4886
f6bcfd97 4887 if ( m_currentCellCoords == wxGridNoCellCoords )
2d66e025 4888 {
f6bcfd97
BP
4889 // if we have just inserted cols into an empty grid the current
4890 // cell will be undefined...
4891 //
4892 SetCurrentCell( 0, 0 );
4893 }
4894 if ( !GetBatchCount() )
4895 {
4896 CalcDimensions();
4897 m_colLabelWin->Refresh();
2d66e025 4898 }
f85afd4e 4899 }
ca65c044 4900 result = true;
f6bcfd97 4901 break;
f85afd4e
MB
4902
4903 case wxGRIDTABLE_NOTIFY_COLS_DELETED:
4904 {
4905 size_t pos = msg.GetCommandInt();
4906 int numCols = msg.GetCommandInt2();
f85afd4e 4907 m_numCols -= numCols;
f85afd4e 4908
d4175745
VZ
4909 if ( !m_colAt.IsEmpty() )
4910 {
4911 int colID = GetColAt( pos );
4912
4913 m_colAt.RemoveAt( pos, numCols );
4914
4915 //Shift the column IDs
4916 int colPos;
4917 for ( colPos = 0; colPos < m_numCols; colPos++ )
4918 {
4919 if ( m_colAt[colPos] > colID )
4920 m_colAt[colPos] -= numCols;
4921 }
4922 }
4923
f6bcfd97 4924 if ( !m_colWidths.IsEmpty() )
f85afd4e 4925 {
27f35b66
SN
4926 m_colWidths.RemoveAt( pos, numCols );
4927 m_colRights.RemoveAt( pos, numCols );
2d66e025
MB
4928
4929 int w = 0;
d4175745
VZ
4930 int colPos;
4931 for ( colPos = 0; colPos < m_numCols; colPos++ )
2d66e025 4932 {
d4175745
VZ
4933 i = GetColAt( colPos );
4934
2d66e025
MB
4935 w += m_colWidths[i];
4936 m_colRights[i] = w;
4937 }
f85afd4e 4938 }
2f024384 4939
f6bcfd97
BP
4940 if ( !m_numCols )
4941 {
4942 m_currentCellCoords = wxGridNoCellCoords;
4943 }
4944 else
4945 {
4946 if ( m_currentCellCoords.GetCol() >= m_numCols )
4947 m_currentCellCoords.Set( 0, 0 );
4948 }
3f3dc2ef
VZ
4949
4950 if ( m_selection )
4951 m_selection->UpdateCols( pos, -((int)numCols) );
f6bcfd97 4952 wxGridCellAttrProvider * attrProvider = m_table->GetAttrProvider();
4db6714b
KH
4953 if (attrProvider)
4954 {
f6bcfd97 4955 attrProvider->UpdateAttrCols( pos, -((int)numCols) );
ccdee36f 4956
84912ef8
RD
4957// ifdef'd out following patch from Paul Gammans
4958#if 0
f6bcfd97
BP
4959 // No need to touch row attributes, unless we
4960 // removed _all_ columns, in this case, we remove
4961 // all row attributes.
4962 // I hate to do this here, but the
4963 // needed data is not available inside UpdateAttrCols.
4964 if ( !GetNumberCols() )
4965 attrProvider->UpdateAttrRows( 0, -GetNumberRows() );
84912ef8 4966#endif
f6bcfd97 4967 }
ccdee36f 4968
f6bcfd97
BP
4969 if ( !GetBatchCount() )
4970 {
4971 CalcDimensions();
4972 m_colLabelWin->Refresh();
4973 }
f85afd4e 4974 }
ca65c044 4975 result = true;
f6bcfd97 4976 break;
f85afd4e
MB
4977 }
4978
f6bcfd97
BP
4979 if (result && !GetBatchCount() )
4980 m_gridWin->Refresh();
2f024384 4981
f6bcfd97 4982 return result;
f85afd4e
MB
4983}
4984
d10f4bf9 4985wxArrayInt wxGrid::CalcRowLabelsExposed( const wxRegion& reg )
f85afd4e 4986{
2d66e025
MB
4987 wxRegionIterator iter( reg );
4988 wxRect r;
f85afd4e 4989
275c4ae4
RD
4990 wxArrayInt rowlabels;
4991
2d66e025
MB
4992 int top, bottom;
4993 while ( iter )
f85afd4e 4994 {
2d66e025 4995 r = iter.GetRect();
f85afd4e 4996
2d66e025
MB
4997 // TODO: remove this when we can...
4998 // There is a bug in wxMotif that gives garbage update
4999 // rectangles if you jump-scroll a long way by clicking the
5000 // scrollbar with middle button. This is a work-around
5001 //
5002#if defined(__WXMOTIF__)
5003 int cw, ch;
5004 m_gridWin->GetClientSize( &cw, &ch );
56b6cf26
DS
5005 if ( r.GetTop() > ch )
5006 r.SetTop( 0 );
2d66e025
MB
5007 r.SetBottom( wxMin( r.GetBottom(), ch ) );
5008#endif
f85afd4e 5009
2d66e025
MB
5010 // logical bounds of update region
5011 //
5012 int dummy;
5013 CalcUnscrolledPosition( 0, r.GetTop(), &dummy, &top );
5014 CalcUnscrolledPosition( 0, r.GetBottom(), &dummy, &bottom );
5015
5016 // find the row labels within these bounds
5017 //
5018 int row;
3d59537f 5019 for ( row = internalYToRow(top); row < m_numRows; row++ )
2d66e025 5020 {
7c1cb261
VZ
5021 if ( GetRowBottom(row) < top )
5022 continue;
2d66e025 5023
6d55126d 5024 if ( GetRowTop(row) > bottom )
7c1cb261 5025 break;
60ff3b99 5026
d10f4bf9 5027 rowlabels.Add( row );
2d66e025 5028 }
60ff3b99 5029
60d8e886 5030 ++iter;
f85afd4e 5031 }
d10f4bf9
VZ
5032
5033 return rowlabels;
f85afd4e
MB
5034}
5035
d10f4bf9 5036wxArrayInt wxGrid::CalcColLabelsExposed( const wxRegion& reg )
f85afd4e 5037{
2d66e025
MB
5038 wxRegionIterator iter( reg );
5039 wxRect r;
f85afd4e 5040
d10f4bf9 5041 wxArrayInt colLabels;
f85afd4e 5042
2d66e025
MB
5043 int left, right;
5044 while ( iter )
f85afd4e 5045 {
2d66e025 5046 r = iter.GetRect();
f85afd4e 5047
2d66e025
MB
5048 // TODO: remove this when we can...
5049 // There is a bug in wxMotif that gives garbage update
5050 // rectangles if you jump-scroll a long way by clicking the
5051 // scrollbar with middle button. This is a work-around
5052 //
5053#if defined(__WXMOTIF__)
5054 int cw, ch;
5055 m_gridWin->GetClientSize( &cw, &ch );
56b6cf26
DS
5056 if ( r.GetLeft() > cw )
5057 r.SetLeft( 0 );
2d66e025
MB
5058 r.SetRight( wxMin( r.GetRight(), cw ) );
5059#endif
5060
5061 // logical bounds of update region
5062 //
5063 int dummy;
5064 CalcUnscrolledPosition( r.GetLeft(), 0, &left, &dummy );
5065 CalcUnscrolledPosition( r.GetRight(), 0, &right, &dummy );
5066
5067 // find the cells within these bounds
5068 //
5069 int col;
d4175745
VZ
5070 int colPos;
5071 for ( colPos = GetColPos( internalXToCol(left) ); colPos < m_numCols; colPos++ )
2d66e025 5072 {
d4175745
VZ
5073 col = GetColAt( colPos );
5074
7c1cb261
VZ
5075 if ( GetColRight(col) < left )
5076 continue;
60ff3b99 5077
7c1cb261
VZ
5078 if ( GetColLeft(col) > right )
5079 break;
2d66e025 5080
d10f4bf9 5081 colLabels.Add( col );
2d66e025 5082 }
60ff3b99 5083
60d8e886 5084 ++iter;
f85afd4e 5085 }
2f024384 5086
d10f4bf9 5087 return colLabels;
f85afd4e
MB
5088}
5089
d10f4bf9 5090wxGridCellCoordsArray wxGrid::CalcCellsExposed( const wxRegion& reg )
f85afd4e 5091{
2d66e025
MB
5092 wxRegionIterator iter( reg );
5093 wxRect r;
f85afd4e 5094
d10f4bf9 5095 wxGridCellCoordsArray cellsExposed;
f85afd4e 5096
2d66e025
MB
5097 int left, top, right, bottom;
5098 while ( iter )
5099 {
5100 r = iter.GetRect();
f85afd4e 5101
2d66e025
MB
5102 // TODO: remove this when we can...
5103 // There is a bug in wxMotif that gives garbage update
5104 // rectangles if you jump-scroll a long way by clicking the
5105 // scrollbar with middle button. This is a work-around
5106 //
5107#if defined(__WXMOTIF__)
f85afd4e 5108 int cw, ch;
2d66e025
MB
5109 m_gridWin->GetClientSize( &cw, &ch );
5110 if ( r.GetTop() > ch ) r.SetTop( 0 );
5111 if ( r.GetLeft() > cw ) r.SetLeft( 0 );
5112 r.SetRight( wxMin( r.GetRight(), cw ) );
5113 r.SetBottom( wxMin( r.GetBottom(), ch ) );
5114#endif
8f177c8e 5115
2d66e025
MB
5116 // logical bounds of update region
5117 //
5118 CalcUnscrolledPosition( r.GetLeft(), r.GetTop(), &left, &top );
5119 CalcUnscrolledPosition( r.GetRight(), r.GetBottom(), &right, &bottom );
f85afd4e 5120
2d66e025 5121 // find the cells within these bounds
f85afd4e 5122 //
2d66e025 5123 int row, col;
3d59537f 5124 for ( row = internalYToRow(top); row < m_numRows; row++ )
f85afd4e 5125 {
7c1cb261
VZ
5126 if ( GetRowBottom(row) <= top )
5127 continue;
f85afd4e 5128
7c1cb261
VZ
5129 if ( GetRowTop(row) > bottom )
5130 break;
60ff3b99 5131
d4175745
VZ
5132 int colPos;
5133 for ( colPos = GetColPos( internalXToCol(left) ); colPos < m_numCols; colPos++ )
2d66e025 5134 {
d4175745
VZ
5135 col = GetColAt( colPos );
5136
7c1cb261
VZ
5137 if ( GetColRight(col) <= left )
5138 continue;
60ff3b99 5139
7c1cb261
VZ
5140 if ( GetColLeft(col) > right )
5141 break;
60ff3b99 5142
d10f4bf9 5143 cellsExposed.Add( wxGridCellCoords( row, col ) );
2d66e025
MB
5144 }
5145 }
60ff3b99 5146
60d8e886 5147 ++iter;
f85afd4e 5148 }
d10f4bf9
VZ
5149
5150 return cellsExposed;
f85afd4e
MB
5151}
5152
5153
2d66e025 5154void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent& event )
f85afd4e 5155{
2d66e025
MB
5156 int x, y, row;
5157 wxPoint pos( event.GetPosition() );
5158 CalcUnscrolledPosition( pos.x, pos.y, &x, &y );
60ff3b99 5159
2d66e025 5160 if ( event.Dragging() )
f85afd4e 5161 {
426b2d87
JS
5162 if (!m_isDragging)
5163 {
ca65c044 5164 m_isDragging = true;
426b2d87
JS
5165 m_rowLabelWin->CaptureMouse();
5166 }
8f177c8e 5167
2d66e025 5168 if ( event.LeftIsDown() )
f85afd4e 5169 {
962a48f6 5170 switch ( m_cursorMode )
f85afd4e 5171 {
f85afd4e
MB
5172 case WXGRID_CURSOR_RESIZE_ROW:
5173 {
2d66e025
MB
5174 int cw, ch, left, dummy;
5175 m_gridWin->GetClientSize( &cw, &ch );
5176 CalcUnscrolledPosition( 0, 0, &left, &dummy );
60ff3b99 5177
2d66e025
MB
5178 wxClientDC dc( m_gridWin );
5179 PrepareDC( dc );
af547d51
VZ
5180 y = wxMax( y,
5181 GetRowTop(m_dragRowOrCol) +
5182 GetRowMinimalHeight(m_dragRowOrCol) );
d2fdd8d2 5183 dc.SetLogicalFunction(wxINVERT);
f85afd4e
MB
5184 if ( m_dragLastPos >= 0 )
5185 {
2d66e025 5186 dc.DrawLine( left, m_dragLastPos, left+cw, m_dragLastPos );
f85afd4e 5187 }
2d66e025
MB
5188 dc.DrawLine( left, y, left+cw, y );
5189 m_dragLastPos = y;
f85afd4e
MB
5190 }
5191 break;
5192
5193 case WXGRID_CURSOR_SELECT_ROW:
902725ee 5194 {
e32352cf 5195 if ( (row = YToRow( y )) >= 0 )
aa5e1f75 5196 {
3f3dc2ef
VZ
5197 if ( m_selection )
5198 {
5199 m_selection->SelectRow( row,
5200 event.ControlDown(),
5201 event.ShiftDown(),
5202 event.AltDown(),
5203 event.MetaDown() );
5204 }
f85afd4e 5205 }
902725ee
WS
5206 }
5207 break;
e2b42eeb
VZ
5208
5209 // default label to suppress warnings about "enumeration value
5210 // 'xxx' not handled in switch
5211 default:
5212 break;
f85afd4e
MB
5213 }
5214 }
5215 return;
5216 }
5217
426b2d87
JS
5218 if ( m_isDragging && (event.Entering() || event.Leaving()) )
5219 return;
8f177c8e 5220
426b2d87
JS
5221 if (m_isDragging)
5222 {
ccdee36f
DS
5223 if (m_rowLabelWin->HasCapture())
5224 m_rowLabelWin->ReleaseMouse();
ca65c044 5225 m_isDragging = false;
426b2d87 5226 }
60ff3b99 5227
6d004f67
MB
5228 // ------------ Entering or leaving the window
5229 //
5230 if ( event.Entering() || event.Leaving() )
5231 {
e2b42eeb 5232 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_rowLabelWin);
6d004f67
MB
5233 }
5234
2d66e025 5235 // ------------ Left button pressed
f85afd4e 5236 //
6d004f67 5237 else if ( event.LeftDown() )
f85afd4e 5238 {
2d66e025
MB
5239 // don't send a label click event for a hit on the
5240 // edge of the row label - this is probably the user
5241 // wanting to resize the row
5242 //
5243 if ( YToEdgeOfRow(y) < 0 )
f85afd4e 5244 {
2d66e025 5245 row = YToRow(y);
2f024384 5246 if ( row >= 0 &&
b54ba671 5247 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK, row, -1, event ) )
f85afd4e 5248 {
2b01fd49 5249 if ( !event.ShiftDown() && !event.CmdDown() )
aa5e1f75 5250 ClearSelection();
64e15340 5251 if ( m_selection )
3f3dc2ef
VZ
5252 {
5253 if ( event.ShiftDown() )
5254 {
5255 m_selection->SelectBlock( m_currentCellCoords.GetRow(),
5256 0,
5257 row,
5258 GetNumberCols() - 1,
5259 event.ControlDown(),
5260 event.ShiftDown(),
5261 event.AltDown(),
5262 event.MetaDown() );
5263 }
5264 else
5265 {
5266 m_selection->SelectRow( row,
5267 event.ControlDown(),
5268 event.ShiftDown(),
5269 event.AltDown(),
5270 event.MetaDown() );
5271 }
5272 }
5273
e2b42eeb 5274 ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW, m_rowLabelWin);
f85afd4e 5275 }
2d66e025
MB
5276 }
5277 else
5278 {
5279 // starting to drag-resize a row
6e8524b1
MB
5280 if ( CanDragRowSize() )
5281 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW, m_rowLabelWin);
2d66e025
MB
5282 }
5283 }
f85afd4e 5284
2d66e025
MB
5285 // ------------ Left double click
5286 //
5287 else if (event.LeftDClick() )
5288 {
4e115ed2 5289 row = YToEdgeOfRow(y);
d43851f7 5290 if ( row < 0 )
2d66e025
MB
5291 {
5292 row = YToRow(y);
a967f048 5293 if ( row >=0 &&
ca65c044
WS
5294 !SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK, row, -1, event ) )
5295 {
a967f048 5296 // no default action at the moment
ca65c044 5297 }
f85afd4e 5298 }
d43851f7
JS
5299 else
5300 {
5301 // adjust row height depending on label text
5302 AutoSizeRowLabelSize( row );
5303
5304 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_colLabelWin);
ccdee36f 5305 m_dragLastPos = -1;
d43851f7 5306 }
f85afd4e 5307 }
60ff3b99 5308
2d66e025 5309 // ------------ Left button released
f85afd4e 5310 //
2d66e025 5311 else if ( event.LeftUp() )
f85afd4e 5312 {
2d66e025 5313 if ( m_cursorMode == WXGRID_CURSOR_RESIZE_ROW )
f85afd4e 5314 {
6d004f67 5315 DoEndDragResizeRow();
60ff3b99 5316
6d004f67
MB
5317 // Note: we are ending the event *after* doing
5318 // default processing in this case
5319 //
b54ba671 5320 SendEvent( wxEVT_GRID_ROW_SIZE, m_dragRowOrCol, -1, event );
2d66e025 5321 }
f85afd4e 5322
e2b42eeb
VZ
5323 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_rowLabelWin);
5324 m_dragLastPos = -1;
2d66e025 5325 }
f85afd4e 5326
2d66e025
MB
5327 // ------------ Right button down
5328 //
5329 else if ( event.RightDown() )
5330 {
5331 row = YToRow(y);
ef5df12b 5332 if ( row >=0 &&
ca65c044 5333 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK, row, -1, event ) )
2d66e025
MB
5334 {
5335 // no default action at the moment
f85afd4e
MB
5336 }
5337 }
60ff3b99 5338
2d66e025 5339 // ------------ Right double click
f85afd4e 5340 //
2d66e025
MB
5341 else if ( event.RightDClick() )
5342 {
5343 row = YToRow(y);
a967f048 5344 if ( row >= 0 &&
ca65c044 5345 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK, row, -1, event ) )
2d66e025
MB
5346 {
5347 // no default action at the moment
5348 }
5349 }
60ff3b99 5350
2d66e025 5351 // ------------ No buttons down and mouse moving
f85afd4e 5352 //
2d66e025 5353 else if ( event.Moving() )
f85afd4e 5354 {
2d66e025
MB
5355 m_dragRowOrCol = YToEdgeOfRow( y );
5356 if ( m_dragRowOrCol >= 0 )
8f177c8e 5357 {
2d66e025 5358 if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
8f177c8e 5359 {
e2b42eeb 5360 // don't capture the mouse yet
6e8524b1 5361 if ( CanDragRowSize() )
ca65c044 5362 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW, m_rowLabelWin, false);
8f177c8e 5363 }
2d66e025 5364 }
6d004f67 5365 else if ( m_cursorMode != WXGRID_CURSOR_SELECT_CELL )
2d66e025 5366 {
ca65c044 5367 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_rowLabelWin, false);
8f177c8e 5368 }
f85afd4e 5369 }
2d66e025
MB
5370}
5371
2d66e025
MB
5372void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent& event )
5373{
5374 int x, y, col;
5375 wxPoint pos( event.GetPosition() );
5376 CalcUnscrolledPosition( pos.x, pos.y, &x, &y );
60ff3b99 5377
2d66e025 5378 if ( event.Dragging() )
f85afd4e 5379 {
fe77cf60
JS
5380 if (!m_isDragging)
5381 {
ca65c044 5382 m_isDragging = true;
fe77cf60 5383 m_colLabelWin->CaptureMouse();
d4175745
VZ
5384
5385 if ( m_cursorMode == WXGRID_CURSOR_MOVE_COL )
5386 m_dragRowOrCol = XToCol( x );
fe77cf60 5387 }
8f177c8e 5388
2d66e025 5389 if ( event.LeftIsDown() )
8f177c8e 5390 {
962a48f6 5391 switch ( m_cursorMode )
8f177c8e 5392 {
2d66e025 5393 case WXGRID_CURSOR_RESIZE_COL:
8f177c8e 5394 {
2d66e025
MB
5395 int cw, ch, dummy, top;
5396 m_gridWin->GetClientSize( &cw, &ch );
5397 CalcUnscrolledPosition( 0, 0, &dummy, &top );
60ff3b99 5398
2d66e025
MB
5399 wxClientDC dc( m_gridWin );
5400 PrepareDC( dc );
43947979
VZ
5401
5402 x = wxMax( x, GetColLeft(m_dragRowOrCol) +
5403 GetColMinimalWidth(m_dragRowOrCol));
d2fdd8d2 5404 dc.SetLogicalFunction(wxINVERT);
2d66e025
MB
5405 if ( m_dragLastPos >= 0 )
5406 {
ccdee36f 5407 dc.DrawLine( m_dragLastPos, top, m_dragLastPos, top + ch );
2d66e025 5408 }
ccdee36f 5409 dc.DrawLine( x, top, x, top + ch );
2d66e025 5410 m_dragLastPos = x;
f85afd4e 5411 }
2d66e025 5412 break;
f85afd4e 5413
2d66e025 5414 case WXGRID_CURSOR_SELECT_COL:
902725ee 5415 {
e32352cf 5416 if ( (col = XToCol( x )) >= 0 )
aa5e1f75 5417 {
3f3dc2ef
VZ
5418 if ( m_selection )
5419 {
5420 m_selection->SelectCol( col,
5421 event.ControlDown(),
5422 event.ShiftDown(),
5423 event.AltDown(),
5424 event.MetaDown() );
5425 }
2d66e025 5426 }
902725ee
WS
5427 }
5428 break;
e2b42eeb 5429
d4175745
VZ
5430 case WXGRID_CURSOR_MOVE_COL:
5431 {
5432 if ( x < 0 )
5433 m_moveToCol = GetColAt( 0 );
5434 else
5435 m_moveToCol = XToCol( x );
5436
5437 int markerX;
5438
5439 if ( m_moveToCol < 0 )
5440 markerX = GetColRight( GetColAt( m_numCols - 1 ) );
5441 else
5442 markerX = GetColLeft( m_moveToCol );
5443
5444 if ( markerX != m_dragLastPos )
5445 {
5446 wxClientDC dc( m_colLabelWin );
5447
5448 int cw, ch;
5449 m_colLabelWin->GetClientSize( &cw, &ch );
5450
5451 markerX++;
5452
5453 //Clean up the last indicator
5454 if ( m_dragLastPos >= 0 )
5455 {
5456 wxPen pen( m_colLabelWin->GetBackgroundColour(), 2 );
5457 dc.SetPen(pen);
5458 dc.DrawLine( m_dragLastPos + 1, 0, m_dragLastPos + 1, ch );
5459 dc.SetPen(wxNullPen);
5460
5461 if ( XToCol( m_dragLastPos ) != -1 )
5462 DrawColLabel( dc, XToCol( m_dragLastPos ) );
5463 }
5464
5465 //Moving to the same place? Don't draw a marker
5466 if ( (m_moveToCol == m_dragRowOrCol)
5467 || (GetColPos( m_moveToCol ) == GetColPos( m_dragRowOrCol ) + 1)
5468 || (m_moveToCol < 0 && m_dragRowOrCol == GetColAt( m_numCols - 1 )))
5469 {
5470 m_dragLastPos = -1;
5471 return;
5472 }
5473
5474 //Draw the marker
5475 wxPen pen( *wxBLUE, 2 );
5476 dc.SetPen(pen);
5477
5478 dc.DrawLine( markerX, 0, markerX, ch );
5479
5480 dc.SetPen(wxNullPen);
5481
5482 m_dragLastPos = markerX - 1;
5483 }
5484 }
5485 break;
5486
e2b42eeb
VZ
5487 // default label to suppress warnings about "enumeration value
5488 // 'xxx' not handled in switch
5489 default:
5490 break;
2d66e025 5491 }
f85afd4e 5492 }
2d66e025 5493 return;
f85afd4e 5494 }
2d66e025 5495
fe77cf60
JS
5496 if ( m_isDragging && (event.Entering() || event.Leaving()) )
5497 return;
2d66e025 5498
fe77cf60
JS
5499 if (m_isDragging)
5500 {
ccdee36f
DS
5501 if (m_colLabelWin->HasCapture())
5502 m_colLabelWin->ReleaseMouse();
ca65c044 5503 m_isDragging = false;
fe77cf60 5504 }
60ff3b99 5505
6d004f67
MB
5506 // ------------ Entering or leaving the window
5507 //
5508 if ( event.Entering() || event.Leaving() )
5509 {
e2b42eeb 5510 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_colLabelWin);
6d004f67
MB
5511 }
5512
2d66e025 5513 // ------------ Left button pressed
f85afd4e 5514 //
6d004f67 5515 else if ( event.LeftDown() )
f85afd4e 5516 {
2d66e025
MB
5517 // don't send a label click event for a hit on the
5518 // edge of the col label - this is probably the user
5519 // wanting to resize the col
5520 //
5521 if ( XToEdgeOfCol(x) < 0 )
8f177c8e 5522 {
2d66e025 5523 col = XToCol(x);
2f024384 5524 if ( col >= 0 &&
b54ba671 5525 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK, -1, col, event ) )
8f177c8e 5526 {
d4175745 5527 if ( m_canDragColMove )
3f3dc2ef 5528 {
d4175745
VZ
5529 //Show button as pressed
5530 wxClientDC dc( m_colLabelWin );
5531 int colLeft = GetColLeft( col );
5532 int colRight = GetColRight( col ) - 1;
5533 dc.SetPen( wxPen( m_colLabelWin->GetBackgroundColour(), 1 ) );
5534 dc.DrawLine( colLeft, 1, colLeft, m_colLabelHeight-1 );
5535 dc.DrawLine( colLeft, 1, colRight, 1 );
5536
5537 ChangeCursorMode(WXGRID_CURSOR_MOVE_COL, m_colLabelWin);
5538 }
5539 else
5540 {
5541 if ( !event.ShiftDown() && !event.CmdDown() )
5542 ClearSelection();
5543 if ( m_selection )
3f3dc2ef 5544 {
d4175745
VZ
5545 if ( event.ShiftDown() )
5546 {
5547 m_selection->SelectBlock( 0,
5548 m_currentCellCoords.GetCol(),
5549 GetNumberRows() - 1, col,
5550 event.ControlDown(),
5551 event.ShiftDown(),
5552 event.AltDown(),
5553 event.MetaDown() );
5554 }
5555 else
5556 {
5557 m_selection->SelectCol( col,
5558 event.ControlDown(),
5559 event.ShiftDown(),
5560 event.AltDown(),
5561 event.MetaDown() );
5562 }
3f3dc2ef 5563 }
3f3dc2ef 5564
d4175745
VZ
5565 ChangeCursorMode(WXGRID_CURSOR_SELECT_COL, m_colLabelWin);
5566 }
f85afd4e 5567 }
2d66e025
MB
5568 }
5569 else
5570 {
5571 // starting to drag-resize a col
5572 //
6e8524b1
MB
5573 if ( CanDragColSize() )
5574 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL, m_colLabelWin);
2d66e025
MB
5575 }
5576 }
f85afd4e 5577
2d66e025
MB
5578 // ------------ Left double click
5579 //
5580 if ( event.LeftDClick() )
5581 {
4e115ed2 5582 col = XToEdgeOfCol(x);
d43851f7 5583 if ( col < 0 )
2d66e025
MB
5584 {
5585 col = XToCol(x);
a967f048
RG
5586 if ( col >= 0 &&
5587 ! SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK, -1, col, event ) )
5588 {
ca65c044 5589 // no default action at the moment
a967f048 5590 }
2d66e025 5591 }
d43851f7
JS
5592 else
5593 {
5594 // adjust column width depending on label text
5595 AutoSizeColLabelSize( col );
5596
5597 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_colLabelWin);
ccdee36f 5598 m_dragLastPos = -1;
d43851f7 5599 }
2d66e025 5600 }
60ff3b99 5601
2d66e025
MB
5602 // ------------ Left button released
5603 //
5604 else if ( event.LeftUp() )
5605 {
d4175745 5606 switch ( m_cursorMode )
2d66e025 5607 {
d4175745 5608 case WXGRID_CURSOR_RESIZE_COL:
d4175745 5609 DoEndDragResizeCol();
e2b42eeb 5610
d4175745
VZ
5611 // Note: we are ending the event *after* doing
5612 // default processing in this case
5613 //
5614 SendEvent( wxEVT_GRID_COL_SIZE, -1, m_dragRowOrCol, event );
852b6c3c 5615 break;
d4175745
VZ
5616
5617 case WXGRID_CURSOR_MOVE_COL:
d4175745
VZ
5618 DoEndDragMoveCol();
5619
5620 SendEvent( wxEVT_GRID_COL_MOVE, -1, m_dragRowOrCol, event );
852b6c3c
VZ
5621 break;
5622
5623 case WXGRID_CURSOR_SELECT_COL:
5624 case WXGRID_CURSOR_SELECT_CELL:
5625 case WXGRID_CURSOR_RESIZE_ROW:
5626 case WXGRID_CURSOR_SELECT_ROW:
5627 // nothing to do (?)
5628 break;
2d66e025 5629 }
f85afd4e 5630
e2b42eeb 5631 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_colLabelWin);
ccdee36f 5632 m_dragLastPos = -1;
60ff3b99
VZ
5633 }
5634
2d66e025
MB
5635 // ------------ Right button down
5636 //
5637 else if ( event.RightDown() )
5638 {
5639 col = XToCol(x);
a967f048 5640 if ( col >= 0 &&
ca65c044 5641 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK, -1, col, event ) )
2d66e025
MB
5642 {
5643 // no default action at the moment
f85afd4e
MB
5644 }
5645 }
60ff3b99 5646
2d66e025 5647 // ------------ Right double click
f85afd4e 5648 //
2d66e025
MB
5649 else if ( event.RightDClick() )
5650 {
5651 col = XToCol(x);
a967f048 5652 if ( col >= 0 &&
ca65c044 5653 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK, -1, col, event ) )
2d66e025
MB
5654 {
5655 // no default action at the moment
5656 }
5657 }
60ff3b99 5658
2d66e025 5659 // ------------ No buttons down and mouse moving
f85afd4e 5660 //
2d66e025 5661 else if ( event.Moving() )
f85afd4e 5662 {
2d66e025
MB
5663 m_dragRowOrCol = XToEdgeOfCol( x );
5664 if ( m_dragRowOrCol >= 0 )
f85afd4e 5665 {
2d66e025 5666 if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
f85afd4e 5667 {
e2b42eeb 5668 // don't capture the cursor yet
6e8524b1 5669 if ( CanDragColSize() )
ca65c044 5670 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL, m_colLabelWin, false);
f85afd4e 5671 }
2d66e025 5672 }
6d004f67 5673 else if ( m_cursorMode != WXGRID_CURSOR_SELECT_CELL )
2d66e025 5674 {
ca65c044 5675 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_colLabelWin, false);
8f177c8e 5676 }
f85afd4e
MB
5677 }
5678}
5679
2d66e025 5680void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent& event )
f85afd4e 5681{
2d66e025 5682 if ( event.LeftDown() )
f85afd4e 5683 {
2d66e025
MB
5684 // indicate corner label by having both row and
5685 // col args == -1
f85afd4e 5686 //
b54ba671 5687 if ( !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK, -1, -1, event ) )
2d66e025
MB
5688 {
5689 SelectAll();
5690 }
f85afd4e 5691 }
2d66e025
MB
5692 else if ( event.LeftDClick() )
5693 {
b54ba671 5694 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK, -1, -1, event );
2d66e025 5695 }
2d66e025 5696 else if ( event.RightDown() )
f85afd4e 5697 {
b54ba671 5698 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK, -1, -1, event ) )
f85afd4e 5699 {
2d66e025
MB
5700 // no default action at the moment
5701 }
5702 }
2d66e025
MB
5703 else if ( event.RightDClick() )
5704 {
b54ba671 5705 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK, -1, -1, event ) )
2d66e025
MB
5706 {
5707 // no default action at the moment
5708 }
5709 }
5710}
f85afd4e 5711
e2b42eeb
VZ
5712void wxGrid::ChangeCursorMode(CursorMode mode,
5713 wxWindow *win,
5714 bool captureMouse)
5715{
5716#ifdef __WXDEBUG__
5717 static const wxChar *cursorModes[] =
5718 {
5719 _T("SELECT_CELL"),
5720 _T("RESIZE_ROW"),
5721 _T("RESIZE_COL"),
5722 _T("SELECT_ROW"),
d4175745
VZ
5723 _T("SELECT_COL"),
5724 _T("MOVE_COL"),
e2b42eeb
VZ
5725 };
5726
181bfffd
VZ
5727 wxLogTrace(_T("grid"),
5728 _T("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
e2b42eeb
VZ
5729 win == m_colLabelWin ? _T("colLabelWin")
5730 : win ? _T("rowLabelWin")
5731 : _T("gridWin"),
5732 cursorModes[m_cursorMode], cursorModes[mode]);
2f024384 5733#endif
e2b42eeb 5734
faec5a43
SN
5735 if ( mode == m_cursorMode &&
5736 win == m_winCapture &&
5737 captureMouse == (m_winCapture != NULL))
e2b42eeb
VZ
5738 return;
5739
5740 if ( !win )
5741 {
5742 // by default use the grid itself
5743 win = m_gridWin;
5744 }
5745
5746 if ( m_winCapture )
5747 {
2f024384
DS
5748 if (m_winCapture->HasCapture())
5749 m_winCapture->ReleaseMouse();
e2b42eeb
VZ
5750 m_winCapture = (wxWindow *)NULL;
5751 }
5752
5753 m_cursorMode = mode;
5754
5755 switch ( m_cursorMode )
5756 {
5757 case WXGRID_CURSOR_RESIZE_ROW:
5758 win->SetCursor( m_rowResizeCursor );
5759 break;
5760
5761 case WXGRID_CURSOR_RESIZE_COL:
5762 win->SetCursor( m_colResizeCursor );
5763 break;
5764
d4175745
VZ
5765 case WXGRID_CURSOR_MOVE_COL:
5766 win->SetCursor( wxCursor(wxCURSOR_HAND) );
5767 break;
5768
e2b42eeb
VZ
5769 default:
5770 win->SetCursor( *wxSTANDARD_CURSOR );
2f024384 5771 break;
e2b42eeb
VZ
5772 }
5773
5774 // we need to capture mouse when resizing
5775 bool resize = m_cursorMode == WXGRID_CURSOR_RESIZE_ROW ||
5776 m_cursorMode == WXGRID_CURSOR_RESIZE_COL;
5777
5778 if ( captureMouse && resize )
5779 {
5780 win->CaptureMouse();
5781 m_winCapture = win;
5782 }
5783}
8f177c8e 5784
2d66e025
MB
5785void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent& event )
5786{
5787 int x, y;
5788 wxPoint pos( event.GetPosition() );
5789 CalcUnscrolledPosition( pos.x, pos.y, &x, &y );
60ff3b99 5790
2d66e025
MB
5791 wxGridCellCoords coords;
5792 XYToCell( x, y, coords );
60ff3b99 5793
27f35b66 5794 int cell_rows, cell_cols;
79dbea21 5795 bool isFirstDrag = !m_isDragging;
27f35b66
SN
5796 GetCellSize( coords.GetRow(), coords.GetCol(), &cell_rows, &cell_cols );
5797 if ((cell_rows < 0) || (cell_cols < 0))
5798 {
5799 coords.SetRow(coords.GetRow() + cell_rows);
5800 coords.SetCol(coords.GetCol() + cell_cols);
5801 }
5802
2d66e025
MB
5803 if ( event.Dragging() )
5804 {
07296f0b
RD
5805 //wxLogDebug("pos(%d, %d) coords(%d, %d)", pos.x, pos.y, coords.GetRow(), coords.GetCol());
5806
962a48f6 5807 // Don't start doing anything until the mouse has been dragged at
07296f0b 5808 // least 3 pixels in any direction...
508011ce
VZ
5809 if (! m_isDragging)
5810 {
5811 if (m_startDragPos == wxDefaultPosition)
5812 {
07296f0b
RD
5813 m_startDragPos = pos;
5814 return;
5815 }
5816 if (abs(m_startDragPos.x - pos.x) < 4 && abs(m_startDragPos.y - pos.y) < 4)
5817 return;
5818 }
5819
ca65c044 5820 m_isDragging = true;
2d66e025
MB
5821 if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
5822 {
f0102d2a 5823 // Hide the edit control, so it
4db6714b 5824 // won't interfere with drag-shrinking.
f6bcfd97 5825 if ( IsCellEditControlShown() )
a5777624 5826 {
f0102d2a 5827 HideCellEditControl();
a5777624
RD
5828 SaveEditControlValue();
5829 }
07296f0b
RD
5830
5831 // Have we captured the mouse yet?
508011ce
VZ
5832 if (! m_winCapture)
5833 {
07296f0b
RD
5834 m_winCapture = m_gridWin;
5835 m_winCapture->CaptureMouse();
5836 }
5837
2d66e025
MB
5838 if ( coords != wxGridNoCellCoords )
5839 {
2b01fd49 5840 if ( event.CmdDown() )
aa5e1f75
SN
5841 {
5842 if ( m_selectingKeyboard == wxGridNoCellCoords)
5843 m_selectingKeyboard = coords;
a9339fe2 5844 HighlightBlock( m_selectingKeyboard, coords );
aa5e1f75 5845 }
79dbea21
RD
5846 else if ( CanDragCell() )
5847 {
5848 if ( isFirstDrag )
5849 {
5850 if ( m_selectingKeyboard == wxGridNoCellCoords)
5851 m_selectingKeyboard = coords;
5852
5853 SendEvent( wxEVT_GRID_CELL_BEGIN_DRAG,
5854 coords.GetRow(),
5855 coords.GetCol(),
5856 event );
5857 }
5858 }
aa5e1f75
SN
5859 else
5860 {
5861 if ( !IsSelection() )
5862 {
c9097836 5863 HighlightBlock( coords, coords );
aa5e1f75
SN
5864 }
5865 else
5866 {
c9097836 5867 HighlightBlock( m_currentCellCoords, coords );
aa5e1f75 5868 }
f85afd4e 5869 }
07296f0b 5870
508011ce
VZ
5871 if (! IsVisible(coords))
5872 {
07296f0b
RD
5873 MakeCellVisible(coords);
5874 // TODO: need to introduce a delay or something here. The
e32352cf 5875 // scrolling is way to fast, at least on MSW - also on GTK.
07296f0b 5876 }
2d66e025
MB
5877 }
5878 }
6d004f67
MB
5879 else if ( m_cursorMode == WXGRID_CURSOR_RESIZE_ROW )
5880 {
5881 int cw, ch, left, dummy;
5882 m_gridWin->GetClientSize( &cw, &ch );
5883 CalcUnscrolledPosition( 0, 0, &left, &dummy );
8f177c8e 5884
6d004f67
MB
5885 wxClientDC dc( m_gridWin );
5886 PrepareDC( dc );
a95e38c0
VZ
5887 y = wxMax( y, GetRowTop(m_dragRowOrCol) +
5888 GetRowMinimalHeight(m_dragRowOrCol) );
6d004f67
MB
5889 dc.SetLogicalFunction(wxINVERT);
5890 if ( m_dragLastPos >= 0 )
5891 {
5892 dc.DrawLine( left, m_dragLastPos, left+cw, m_dragLastPos );
5893 }
5894 dc.DrawLine( left, y, left+cw, y );
5895 m_dragLastPos = y;
5896 }
5897 else if ( m_cursorMode == WXGRID_CURSOR_RESIZE_COL )
5898 {
5899 int cw, ch, dummy, top;
5900 m_gridWin->GetClientSize( &cw, &ch );
5901 CalcUnscrolledPosition( 0, 0, &dummy, &top );
e2b42eeb 5902
6d004f67
MB
5903 wxClientDC dc( m_gridWin );
5904 PrepareDC( dc );
43947979
VZ
5905 x = wxMax( x, GetColLeft(m_dragRowOrCol) +
5906 GetColMinimalWidth(m_dragRowOrCol) );
6d004f67
MB
5907 dc.SetLogicalFunction(wxINVERT);
5908 if ( m_dragLastPos >= 0 )
5909 {
a9339fe2 5910 dc.DrawLine( m_dragLastPos, top, m_dragLastPos, top + ch );
6d004f67 5911 }
a9339fe2 5912 dc.DrawLine( x, top, x, top + ch );
6d004f67
MB
5913 m_dragLastPos = x;
5914 }
e2b42eeb 5915
2d66e025
MB
5916 return;
5917 }
66242c80 5918
ca65c044 5919 m_isDragging = false;
07296f0b
RD
5920 m_startDragPos = wxDefaultPosition;
5921
a5777624
RD
5922 // VZ: if we do this, the mode is reset to WXGRID_CURSOR_SELECT_CELL
5923 // immediately after it becomes WXGRID_CURSOR_RESIZE_ROW/COL under
5924 // wxGTK
e2b42eeb 5925#if 0
a5777624
RD
5926 if ( event.Entering() || event.Leaving() )
5927 {
5928 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL);
5929 m_gridWin->SetCursor( *wxSTANDARD_CURSOR );
5930 }
5931 else
e2b42eeb
VZ
5932#endif // 0
5933
a5777624
RD
5934 // ------------ Left button pressed
5935 //
5936 if ( event.LeftDown() && coords != wxGridNoCellCoords )
f6bcfd97
BP
5937 {
5938 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_CLICK,
5939 coords.GetRow(),
5940 coords.GetCol(),
5941 event ) )
a5777624 5942 {
2b01fd49 5943 if ( !event.CmdDown() )
f6bcfd97
BP
5944 ClearSelection();
5945 if ( event.ShiftDown() )
5946 {
3f3dc2ef
VZ
5947 if ( m_selection )
5948 {
5949 m_selection->SelectBlock( m_currentCellCoords.GetRow(),
5950 m_currentCellCoords.GetCol(),
5951 coords.GetRow(),
5952 coords.GetCol(),
5953 event.ControlDown(),
5954 event.ShiftDown(),
5955 event.AltDown(),
5956 event.MetaDown() );
5957 }
f6bcfd97 5958 }
2f024384 5959 else if ( XToEdgeOfCol(x) < 0 &&
f6bcfd97 5960 YToEdgeOfRow(y) < 0 )
58dd5b3b 5961 {
a5777624
RD
5962 DisableCellEditControl();
5963 MakeCellVisible( coords );
5964
2b01fd49 5965 if ( event.CmdDown() )
58dd5b3b 5966 {
73145b0e
JS
5967 if ( m_selection )
5968 {
5969 m_selection->ToggleCellSelection( coords.GetRow(),
5970 coords.GetCol(),
5971 event.ControlDown(),
5972 event.ShiftDown(),
5973 event.AltDown(),
5974 event.MetaDown() );
5975 }
5976 m_selectingTopLeft = wxGridNoCellCoords;
5977 m_selectingBottomRight = wxGridNoCellCoords;
5978 m_selectingKeyboard = coords;
a5777624
RD
5979 }
5980 else
5981 {
73145b0e
JS
5982 m_waitForSlowClick = m_currentCellCoords == coords && coords != wxGridNoCellCoords;
5983 SetCurrentCell( coords );
5984 if ( m_selection )
aa5e1f75 5985 {
73145b0e
JS
5986 if ( m_selection->GetSelectionMode() !=
5987 wxGrid::wxGridSelectCells )
3f3dc2ef 5988 {
73145b0e 5989 HighlightBlock( coords, coords );
3f3dc2ef 5990 }
aa5e1f75 5991 }
58dd5b3b
MB
5992 }
5993 }
f85afd4e 5994 }
a5777624 5995 }
f85afd4e 5996
a5777624
RD
5997 // ------------ Left double click
5998 //
5999 else if ( event.LeftDClick() && coords != wxGridNoCellCoords )
6000 {
6001 DisableCellEditControl();
6002
2f024384 6003 if ( XToEdgeOfCol(x) < 0 && YToEdgeOfRow(y) < 0 )
58dd5b3b 6004 {
1ef49ab5
VS
6005 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_DCLICK,
6006 coords.GetRow(),
6007 coords.GetCol(),
6008 event ) )
6009 {
6010 // we want double click to select a cell and start editing
6011 // (i.e. to behave in same way as sequence of two slow clicks):
6012 m_waitForSlowClick = true;
6013 }
58dd5b3b 6014 }
a5777624 6015 }
f85afd4e 6016
a5777624
RD
6017 // ------------ Left button released
6018 //
6019 else if ( event.LeftUp() )
6020 {
6021 if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
f85afd4e 6022 {
f40f9976
JS
6023 if (m_winCapture)
6024 {
2f024384
DS
6025 if (m_winCapture->HasCapture())
6026 m_winCapture->ReleaseMouse();
f40f9976
JS
6027 m_winCapture = NULL;
6028 }
6029
bee19958 6030 if ( coords == m_currentCellCoords && m_waitForSlowClick && CanEnableCellControl() )
932b55d0
JS
6031 {
6032 ClearSelection();
6033 EnableCellEditControl();
6034
2f024384 6035 wxGridCellAttr *attr = GetCellAttr(coords);
932b55d0
JS
6036 wxGridCellEditor *editor = attr->GetEditor(this, coords.GetRow(), coords.GetCol());
6037 editor->StartingClick();
6038 editor->DecRef();
6039 attr->DecRef();
6040
ca65c044 6041 m_waitForSlowClick = false;
932b55d0
JS
6042 }
6043 else if ( m_selectingTopLeft != wxGridNoCellCoords &&
b5808881 6044 m_selectingBottomRight != wxGridNoCellCoords )
52068ea5 6045 {
3f3dc2ef
VZ
6046 if ( m_selection )
6047 {
6048 m_selection->SelectBlock( m_selectingTopLeft.GetRow(),
6049 m_selectingTopLeft.GetCol(),
6050 m_selectingBottomRight.GetRow(),
6051 m_selectingBottomRight.GetCol(),
6052 event.ControlDown(),
6053 event.ShiftDown(),
6054 event.AltDown(),
6055 event.MetaDown() );
6056 }
6057
5c8fc7c1
SN
6058 m_selectingTopLeft = wxGridNoCellCoords;
6059 m_selectingBottomRight = wxGridNoCellCoords;
2d66e025 6060
73145b0e
JS
6061 // Show the edit control, if it has been hidden for
6062 // drag-shrinking.
6063 ShowCellEditControl();
6064 }
a5777624
RD
6065 }
6066 else if ( m_cursorMode == WXGRID_CURSOR_RESIZE_ROW )
6067 {
6068 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL);
6069 DoEndDragResizeRow();
e2b42eeb 6070
a5777624
RD
6071 // Note: we are ending the event *after* doing
6072 // default processing in this case
6073 //
6074 SendEvent( wxEVT_GRID_ROW_SIZE, m_dragRowOrCol, -1, event );
6075 }
6076 else if ( m_cursorMode == WXGRID_CURSOR_RESIZE_COL )
6077 {
6078 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL);
6079 DoEndDragResizeCol();
e2b42eeb 6080
a5777624
RD
6081 // Note: we are ending the event *after* doing
6082 // default processing in this case
6083 //
6084 SendEvent( wxEVT_GRID_COL_SIZE, -1, m_dragRowOrCol, event );
58dd5b3b 6085 }
f85afd4e 6086
a5777624
RD
6087 m_dragLastPos = -1;
6088 }
6089
a5777624
RD
6090 // ------------ Right button down
6091 //
6092 else if ( event.RightDown() && coords != wxGridNoCellCoords )
6093 {
6094 DisableCellEditControl();
6095 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_CLICK,
6096 coords.GetRow(),
6097 coords.GetCol(),
6098 event ) )
f85afd4e 6099 {
a5777624 6100 // no default action at the moment
60ff3b99 6101 }
a5777624 6102 }
2d66e025 6103
a5777624
RD
6104 // ------------ Right double click
6105 //
6106 else if ( event.RightDClick() && coords != wxGridNoCellCoords )
6107 {
6108 DisableCellEditControl();
6109 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_DCLICK,
6110 coords.GetRow(),
6111 coords.GetCol(),
6112 event ) )
f85afd4e 6113 {
a5777624 6114 // no default action at the moment
60ff3b99 6115 }
a5777624
RD
6116 }
6117
6118 // ------------ Moving and no button action
6119 //
6120 else if ( event.Moving() && !event.IsButton() )
6121 {
4db6714b 6122 if ( coords.GetRow() < 0 || coords.GetCol() < 0 )
d57ad377
SN
6123 {
6124 // out of grid cell area
6125 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL);
6126 return;
6127 }
6128
a5777624
RD
6129 int dragRow = YToEdgeOfRow( y );
6130 int dragCol = XToEdgeOfCol( x );
790cc417 6131
a5777624
RD
6132 // Dragging on the corner of a cell to resize in both
6133 // directions is not implemented yet...
790cc417 6134 //
91894db3 6135 if ( dragRow >= 0 && dragCol >= 0 )
790cc417 6136 {
a5777624
RD
6137 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL);
6138 return;
6139 }
6d004f67 6140
d57ad377 6141 if ( dragRow >= 0 )
a5777624
RD
6142 {
6143 m_dragRowOrCol = dragRow;
6d004f67 6144
a5777624 6145 if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
6d004f67 6146 {
a5777624
RD
6147 if ( CanDragRowSize() && CanDragGridSize() )
6148 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW);
6d004f67 6149 }
a5777624 6150 }
91894db3 6151 else if ( dragCol >= 0 )
a5777624
RD
6152 {
6153 m_dragRowOrCol = dragCol;
e2b42eeb 6154
a5777624 6155 if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
6d004f67 6156 {
a5777624
RD
6157 if ( CanDragColSize() && CanDragGridSize() )
6158 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL);
6d004f67
MB
6159 }
6160 }
91894db3 6161 else // Neither on a row or col edge
a5777624 6162 {
91894db3
VZ
6163 if ( m_cursorMode != WXGRID_CURSOR_SELECT_CELL )
6164 {
6165 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL);
6166 }
a5777624
RD
6167 }
6168 }
6d004f67
MB
6169}
6170
6d004f67
MB
6171void wxGrid::DoEndDragResizeRow()
6172{
6173 if ( m_dragLastPos >= 0 )
6174 {
6175 // erase the last line and resize the row
6176 //
6177 int cw, ch, left, dummy;
6178 m_gridWin->GetClientSize( &cw, &ch );
6179 CalcUnscrolledPosition( 0, 0, &left, &dummy );
6180
6181 wxClientDC dc( m_gridWin );
6182 PrepareDC( dc );
6183 dc.SetLogicalFunction( wxINVERT );
a9339fe2 6184 dc.DrawLine( left, m_dragLastPos, left + cw, m_dragLastPos );
6d004f67 6185 HideCellEditControl();
a5777624 6186 SaveEditControlValue();
6d004f67 6187
7c1cb261 6188 int rowTop = GetRowTop(m_dragRowOrCol);
6d004f67 6189 SetRowSize( m_dragRowOrCol,
b8d24d4e 6190 wxMax( m_dragLastPos - rowTop, m_minAcceptableRowHeight ) );
e2b42eeb 6191
6d004f67
MB
6192 if ( !GetBatchCount() )
6193 {
6194 // Only needed to get the correct rect.y:
6195 wxRect rect ( CellToRect( m_dragRowOrCol, 0 ) );
6196 rect.x = 0;
6197 CalcScrolledPosition(0, rect.y, &dummy, &rect.y);
6198 rect.width = m_rowLabelWidth;
6199 rect.height = ch - rect.y;
ca65c044 6200 m_rowLabelWin->Refresh( true, &rect );
6d004f67 6201 rect.width = cw;
ccdee36f 6202
27f35b66
SN
6203 // if there is a multicell block, paint all of it
6204 if (m_table)
6205 {
6206 int i, cell_rows, cell_cols, subtract_rows = 0;
6207 int leftCol = XToCol(left);
a9339fe2 6208 int rightCol = internalXToCol(left + cw);
27f35b66
SN
6209 if (leftCol >= 0)
6210 {
27f35b66
SN
6211 for (i=leftCol; i<rightCol; i++)
6212 {
6213 GetCellSize(m_dragRowOrCol, i, &cell_rows, &cell_cols);
6214 if (cell_rows < subtract_rows)
6215 subtract_rows = cell_rows;
6216 }
6217 rect.y = GetRowTop(m_dragRowOrCol + subtract_rows);
6218 CalcScrolledPosition(0, rect.y, &dummy, &rect.y);
6219 rect.height = ch - rect.y;
6220 }
6221 }
ca65c044 6222 m_gridWin->Refresh( false, &rect );
6d004f67
MB
6223 }
6224
6225 ShowCellEditControl();
6226 }
6227}
6228
6229
6230void wxGrid::DoEndDragResizeCol()
6231{
6232 if ( m_dragLastPos >= 0 )
6233 {
6234 // erase the last line and resize the col
6235 //
6236 int cw, ch, dummy, top;
6237 m_gridWin->GetClientSize( &cw, &ch );
6238 CalcUnscrolledPosition( 0, 0, &dummy, &top );
6239
6240 wxClientDC dc( m_gridWin );
6241 PrepareDC( dc );
6242 dc.SetLogicalFunction( wxINVERT );
a9339fe2 6243 dc.DrawLine( m_dragLastPos, top, m_dragLastPos, top + ch );
6d004f67 6244 HideCellEditControl();
a5777624 6245 SaveEditControlValue();
6d004f67 6246
7c1cb261 6247 int colLeft = GetColLeft(m_dragRowOrCol);
6d004f67 6248 SetColSize( m_dragRowOrCol,
43947979
VZ
6249 wxMax( m_dragLastPos - colLeft,
6250 GetColMinimalWidth(m_dragRowOrCol) ) );
6d004f67
MB
6251
6252 if ( !GetBatchCount() )
6253 {
6254 // Only needed to get the correct rect.x:
6255 wxRect rect ( CellToRect( 0, m_dragRowOrCol ) );
6256 rect.y = 0;
6257 CalcScrolledPosition(rect.x, 0, &rect.x, &dummy);
6258 rect.width = cw - rect.x;
6259 rect.height = m_colLabelHeight;
ca65c044 6260 m_colLabelWin->Refresh( true, &rect );
6d004f67 6261 rect.height = ch;
2f024384 6262
27f35b66
SN
6263 // if there is a multicell block, paint all of it
6264 if (m_table)
6265 {
6266 int i, cell_rows, cell_cols, subtract_cols = 0;
6267 int topRow = YToRow(top);
a9339fe2 6268 int bottomRow = internalYToRow(top + cw);
27f35b66
SN
6269 if (topRow >= 0)
6270 {
27f35b66
SN
6271 for (i=topRow; i<bottomRow; i++)
6272 {
6273 GetCellSize(i, m_dragRowOrCol, &cell_rows, &cell_cols);
6274 if (cell_cols < subtract_cols)
6275 subtract_cols = cell_cols;
6276 }
a9339fe2 6277
27f35b66
SN
6278 rect.x = GetColLeft(m_dragRowOrCol + subtract_cols);
6279 CalcScrolledPosition(rect.x, 0, &rect.x, &dummy);
6280 rect.width = cw - rect.x;
6281 }
6282 }
2f024384 6283
ca65c044 6284 m_gridWin->Refresh( false, &rect );
790cc417 6285 }
e2b42eeb 6286
6d004f67 6287 ShowCellEditControl();
f85afd4e 6288 }
f85afd4e
MB
6289}
6290
d4175745
VZ
6291void wxGrid::DoEndDragMoveCol()
6292{
6293 //The user clicked on the column but didn't actually drag
6294 if ( m_dragLastPos < 0 )
6295 {
6296 m_colLabelWin->Refresh(); //Do this to "unpress" the column
6297 return;
6298 }
6299
6300 int newPos;
6301 if ( m_moveToCol == -1 )
6302 newPos = m_numCols - 1;
6303 else
6304 {
6305 newPos = GetColPos( m_moveToCol );
6306 if ( newPos > GetColPos( m_dragRowOrCol ) )
6307 newPos--;
6308 }
6309
6310 SetColPos( m_dragRowOrCol, newPos );
6311}
6312
6313void wxGrid::SetColPos( int colID, int newPos )
6314{
6315 if ( m_colAt.IsEmpty() )
6316 {
6317 m_colAt.Alloc( m_numCols );
6318
6319 int i;
6320 for ( i = 0; i < m_numCols; i++ )
6321 {
6322 m_colAt.Add( i );
6323 }
6324 }
6325
6326 int oldPos = GetColPos( colID );
6327
6328 //Reshuffle the m_colAt array
6329 if ( newPos > oldPos )
6330 {
6331 int i;
6332 for ( i = oldPos; i < newPos; i++ )
6333 {
6334 m_colAt[i] = m_colAt[i+1];
6335 }
6336 }
6337 else
6338 {
6339 int i;
6340 for ( i = oldPos; i > newPos; i-- )
6341 {
6342 m_colAt[i] = m_colAt[i-1];
6343 }
6344 }
6345
6346 m_colAt[newPos] = colID;
6347
6348 //Recalculate the column rights
6349 if ( !m_colWidths.IsEmpty() )
6350 {
6351 int colRight = 0;
6352 int colPos;
6353 for ( colPos = 0; colPos < m_numCols; colPos++ )
6354 {
6355 int colID = GetColAt( colPos );
6356
6357 colRight += m_colWidths[colID];
6358 m_colRights[colID] = colRight;
6359 }
6360 }
6361
6362 m_colLabelWin->Refresh();
6363 m_gridWin->Refresh();
6364}
6365
6366
6367
6368void wxGrid::EnableDragColMove( bool enable )
6369{
6370 if ( m_canDragColMove == enable )
6371 return;
6372
6373 m_canDragColMove = enable;
6374
6375 if ( !m_canDragColMove )
6376 {
6377 m_colAt.Clear();
6378
6379 //Recalculate the column rights
6380 if ( !m_colWidths.IsEmpty() )
6381 {
6382 int colRight = 0;
6383 int colPos;
6384 for ( colPos = 0; colPos < m_numCols; colPos++ )
6385 {
6386 colRight += m_colWidths[colPos];
6387 m_colRights[colPos] = colRight;
6388 }
6389 }
6390
6391 m_colLabelWin->Refresh();
6392 m_gridWin->Refresh();
6393 }
6394}
6395
6396
2d66e025
MB
6397//
6398// ------ interaction with data model
6399//
6400bool wxGrid::ProcessTableMessage( wxGridTableMessage& msg )
f85afd4e 6401{
2d66e025 6402 switch ( msg.GetId() )
17732cec 6403 {
2d66e025
MB
6404 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES:
6405 return GetModelValues();
17732cec 6406
2d66e025
MB
6407 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES:
6408 return SetModelValues();
f85afd4e 6409
2d66e025
MB
6410 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED:
6411 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED:
6412 case wxGRIDTABLE_NOTIFY_ROWS_DELETED:
6413 case wxGRIDTABLE_NOTIFY_COLS_INSERTED:
6414 case wxGRIDTABLE_NOTIFY_COLS_APPENDED:
6415 case wxGRIDTABLE_NOTIFY_COLS_DELETED:
6416 return Redimension( msg );
6417
6418 default:
ca65c044 6419 return false;
f85afd4e 6420 }
2d66e025 6421}
f85afd4e 6422
2d66e025 6423// The behaviour of this function depends on the grid table class
5b061713 6424// Clear() function. For the default wxGridStringTable class the
2d66e025
MB
6425// behavious is to replace all cell contents with wxEmptyString but
6426// not to change the number of rows or cols.
6427//
6428void wxGrid::ClearGrid()
6429{
6430 if ( m_table )
f85afd4e 6431 {
4cfa5de6
RD
6432 if (IsCellEditControlEnabled())
6433 DisableCellEditControl();
6434
2d66e025 6435 m_table->Clear();
5b061713 6436 if (!GetBatchCount())
a9339fe2 6437 m_gridWin->Refresh();
f85afd4e
MB
6438 }
6439}
6440
2d66e025 6441bool wxGrid::InsertRows( int pos, int numRows, bool WXUNUSED(updateLabels) )
f85afd4e 6442{
2d66e025 6443 // TODO: something with updateLabels flag
8f177c8e 6444
2d66e025 6445 if ( !m_created )
f85afd4e 6446 {
bd3c6758 6447 wxFAIL_MSG( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") );
ca65c044 6448 return false;
2d66e025 6449 }
8f177c8e 6450
2d66e025
MB
6451 if ( m_table )
6452 {
b7fff980 6453 if (IsCellEditControlEnabled())
b54ba671 6454 DisableCellEditControl();
b7fff980 6455
4ea3479c 6456 bool done = m_table->InsertRows( pos, numRows );
4ea3479c 6457 return done;
f85afd4e 6458
2d66e025
MB
6459 // the table will have sent the results of the insert row
6460 // operation to this view object as a grid table message
f85afd4e 6461 }
a9339fe2 6462
ca65c044 6463 return false;
f85afd4e
MB
6464}
6465
2d66e025 6466bool wxGrid::AppendRows( int numRows, bool WXUNUSED(updateLabels) )
f85afd4e 6467{
2d66e025
MB
6468 // TODO: something with updateLabels flag
6469
6470 if ( !m_created )
f85afd4e 6471 {
bd3c6758 6472 wxFAIL_MSG( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") );
ca65c044 6473 return false;
2d66e025
MB
6474 }
6475
4ea3479c
JS
6476 if ( m_table )
6477 {
6478 bool done = m_table && m_table->AppendRows( numRows );
4ea3479c 6479 return done;
a9339fe2 6480
4ea3479c
JS
6481 // the table will have sent the results of the append row
6482 // operation to this view object as a grid table message
6483 }
a9339fe2 6484
ca65c044 6485 return false;
f85afd4e
MB
6486}
6487
2d66e025 6488bool wxGrid::DeleteRows( int pos, int numRows, bool WXUNUSED(updateLabels) )
f85afd4e 6489{
2d66e025
MB
6490 // TODO: something with updateLabels flag
6491
6492 if ( !m_created )
f85afd4e 6493 {
bd3c6758 6494 wxFAIL_MSG( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") );
ca65c044 6495 return false;
2d66e025
MB
6496 }
6497
b7fff980 6498 if ( m_table )
2d66e025 6499 {
b7fff980 6500 if (IsCellEditControlEnabled())
b54ba671 6501 DisableCellEditControl();
f85afd4e 6502
4ea3479c 6503 bool done = m_table->DeleteRows( pos, numRows );
4ea3479c 6504 return done;
f6bcfd97
BP
6505 // the table will have sent the results of the delete row
6506 // operation to this view object as a grid table message
2d66e025 6507 }
a9339fe2 6508
ca65c044 6509 return false;
2d66e025 6510}
f85afd4e 6511
2d66e025
MB
6512bool wxGrid::InsertCols( int pos, int numCols, bool WXUNUSED(updateLabels) )
6513{
6514 // TODO: something with updateLabels flag
8f177c8e 6515
2d66e025
MB
6516 if ( !m_created )
6517 {
bd3c6758 6518 wxFAIL_MSG( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") );
ca65c044 6519 return false;
2d66e025 6520 }
f85afd4e 6521
2d66e025
MB
6522 if ( m_table )
6523 {
b7fff980 6524 if (IsCellEditControlEnabled())
b54ba671 6525 DisableCellEditControl();
b7fff980 6526
4ea3479c 6527 bool done = m_table->InsertCols( pos, numCols );
4ea3479c 6528 return done;
2d66e025
MB
6529 // the table will have sent the results of the insert col
6530 // operation to this view object as a grid table message
f85afd4e 6531 }
2f024384 6532
ca65c044 6533 return false;
f85afd4e
MB
6534}
6535
2d66e025 6536bool wxGrid::AppendCols( int numCols, bool WXUNUSED(updateLabels) )
f85afd4e 6537{
2d66e025
MB
6538 // TODO: something with updateLabels flag
6539
6540 if ( !m_created )
f85afd4e 6541 {
bd3c6758 6542 wxFAIL_MSG( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") );
ca65c044 6543 return false;
2d66e025 6544 }
f85afd4e 6545
4ea3479c
JS
6546 if ( m_table )
6547 {
6548 bool done = m_table->AppendCols( numCols );
4ea3479c
JS
6549 return done;
6550 // the table will have sent the results of the append col
6551 // operation to this view object as a grid table message
6552 }
2f024384 6553
ca65c044 6554 return false;
f85afd4e
MB
6555}
6556
2d66e025 6557bool wxGrid::DeleteCols( int pos, int numCols, bool WXUNUSED(updateLabels) )
f85afd4e 6558{
2d66e025 6559 // TODO: something with updateLabels flag
8f177c8e 6560
2d66e025 6561 if ( !m_created )
f85afd4e 6562 {
bd3c6758 6563 wxFAIL_MSG( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") );
ca65c044 6564 return false;
f85afd4e
MB
6565 }
6566
b7fff980 6567 if ( m_table )
2d66e025 6568 {
b7fff980 6569 if (IsCellEditControlEnabled())
b54ba671 6570 DisableCellEditControl();
8f177c8e 6571
4ea3479c 6572 bool done = m_table->DeleteCols( pos, numCols );
4ea3479c 6573 return done;
f6bcfd97
BP
6574 // the table will have sent the results of the delete col
6575 // operation to this view object as a grid table message
f85afd4e 6576 }
2f024384 6577
ca65c044 6578 return false;
f85afd4e
MB
6579}
6580
2d66e025
MB
6581//
6582// ----- event handlers
f85afd4e 6583//
8f177c8e 6584
2d66e025
MB
6585// Generate a grid event based on a mouse event and
6586// return the result of ProcessEvent()
6587//
fe7b9ed6 6588int wxGrid::SendEvent( const wxEventType type,
2d66e025
MB
6589 int row, int col,
6590 wxMouseEvent& mouseEv )
6591{
2f024384 6592 bool claimed, vetoed;
fe7b9ed6 6593
97a9929e
VZ
6594 if ( type == wxEVT_GRID_ROW_SIZE || type == wxEVT_GRID_COL_SIZE )
6595 {
6596 int rowOrCol = (row == -1 ? col : row);
6597
6598 wxGridSizeEvent gridEvt( GetId(),
6599 type,
6600 this,
6601 rowOrCol,
6602 mouseEv.GetX() + GetRowLabelSize(),
6603 mouseEv.GetY() + GetColLabelSize(),
6604 mouseEv.ControlDown(),
6605 mouseEv.ShiftDown(),
6606 mouseEv.AltDown(),
6607 mouseEv.MetaDown() );
6608
6609 claimed = GetEventHandler()->ProcessEvent(gridEvt);
6610 vetoed = !gridEvt.IsAllowed();
6611 }
fe7b9ed6 6612 else if ( type == wxEVT_GRID_RANGE_SELECT )
97a9929e
VZ
6613 {
6614 // Right now, it should _never_ end up here!
6615 wxGridRangeSelectEvent gridEvt( GetId(),
6616 type,
6617 this,
6618 m_selectingTopLeft,
6619 m_selectingBottomRight,
ca65c044 6620 true,
97a9929e
VZ
6621 mouseEv.ControlDown(),
6622 mouseEv.ShiftDown(),
6623 mouseEv.AltDown(),
6624 mouseEv.MetaDown() );
6625
6626 claimed = GetEventHandler()->ProcessEvent(gridEvt);
6627 vetoed = !gridEvt.IsAllowed();
6628 }
2b73a34e
RD
6629 else if ( type == wxEVT_GRID_LABEL_LEFT_CLICK ||
6630 type == wxEVT_GRID_LABEL_LEFT_DCLICK ||
6631 type == wxEVT_GRID_LABEL_RIGHT_CLICK ||
6632 type == wxEVT_GRID_LABEL_RIGHT_DCLICK )
6633 {
6634 wxPoint pos = mouseEv.GetPosition();
6635
6636 if ( mouseEv.GetEventObject() == GetGridRowLabelWindow() )
6637 pos.y += GetColLabelSize();
6638 if ( mouseEv.GetEventObject() == GetGridColLabelWindow() )
6639 pos.x += GetRowLabelSize();
6640
6641 wxGridEvent gridEvt( GetId(),
6642 type,
6643 this,
6644 row, col,
6645 pos.x,
6646 pos.y,
6647 false,
6648 mouseEv.ControlDown(),
6649 mouseEv.ShiftDown(),
6650 mouseEv.AltDown(),
6651 mouseEv.MetaDown() );
6652 claimed = GetEventHandler()->ProcessEvent(gridEvt);
6653 vetoed = !gridEvt.IsAllowed();
6654 }
fe7b9ed6 6655 else
97a9929e
VZ
6656 {
6657 wxGridEvent gridEvt( GetId(),
6658 type,
6659 this,
6660 row, col,
6661 mouseEv.GetX() + GetRowLabelSize(),
6662 mouseEv.GetY() + GetColLabelSize(),
ca65c044 6663 false,
97a9929e
VZ
6664 mouseEv.ControlDown(),
6665 mouseEv.ShiftDown(),
6666 mouseEv.AltDown(),
6667 mouseEv.MetaDown() );
6668 claimed = GetEventHandler()->ProcessEvent(gridEvt);
6669 vetoed = !gridEvt.IsAllowed();
6670 }
6671
6672 // A Veto'd event may not be `claimed' so test this first
4db6714b
KH
6673 if (vetoed)
6674 return -1;
6675
97a9929e 6676 return claimed ? 1 : 0;
f85afd4e
MB
6677}
6678
2d66e025
MB
6679// Generate a grid event of specified type and return the result
6680// of ProcessEvent().
f85afd4e 6681//
fe7b9ed6 6682int wxGrid::SendEvent( const wxEventType type,
2d66e025 6683 int row, int col )
f85afd4e 6684{
a9339fe2 6685 bool claimed, vetoed;
fe7b9ed6 6686
b54ba671 6687 if ( type == wxEVT_GRID_ROW_SIZE || type == wxEVT_GRID_COL_SIZE )
f85afd4e 6688 {
2d66e025 6689 int rowOrCol = (row == -1 ? col : row);
f85afd4e 6690
a9339fe2 6691 wxGridSizeEvent gridEvt( GetId(), type, this, rowOrCol );
2d66e025 6692
fe7b9ed6
VZ
6693 claimed = GetEventHandler()->ProcessEvent(gridEvt);
6694 vetoed = !gridEvt.IsAllowed();
2d66e025
MB
6695 }
6696 else
6697 {
a9339fe2 6698 wxGridEvent gridEvt( GetId(), type, this, row, col );
8f177c8e 6699
fe7b9ed6
VZ
6700 claimed = GetEventHandler()->ProcessEvent(gridEvt);
6701 vetoed = !gridEvt.IsAllowed();
6702 }
6703
97a9929e 6704 // A Veto'd event may not be `claimed' so test this first
4db6714b
KH
6705 if (vetoed)
6706 return -1;
6707
97a9929e 6708 return claimed ? 1 : 0;
f85afd4e
MB
6709}
6710
2d66e025 6711void wxGrid::OnPaint( wxPaintEvent& WXUNUSED(event) )
f85afd4e 6712{
3d59537f
DS
6713 // needed to prevent zillions of paint events on MSW
6714 wxPaintDC dc(this);
8f177c8e 6715}
f85afd4e 6716
bca7bfc8 6717void wxGrid::Refresh(bool eraseb, const wxRect* rect)
27f35b66 6718{
ad603bf7
VZ
6719 // Don't do anything if between Begin/EndBatch...
6720 // EndBatch() will do all this on the last nested one anyway.
27f35b66
SN
6721 if (! GetBatchCount())
6722 {
bca7bfc8 6723 // Refresh to get correct scrolled position:
4db6714b 6724 wxScrolledWindow::Refresh(eraseb, rect);
27f35b66 6725
27f35b66
SN
6726 if (rect)
6727 {
bca7bfc8
SN
6728 int rect_x, rect_y, rectWidth, rectHeight;
6729 int width_label, width_cell, height_label, height_cell;
6730 int x, y;
6731
2f024384 6732 // Copy rectangle can get scroll offsets..
bca7bfc8
SN
6733 rect_x = rect->GetX();
6734 rect_y = rect->GetY();
6735 rectWidth = rect->GetWidth();
6736 rectHeight = rect->GetHeight();
27f35b66 6737
bca7bfc8 6738 width_label = m_rowLabelWidth - rect_x;
4db6714b
KH
6739 if (width_label > rectWidth)
6740 width_label = rectWidth;
27f35b66 6741
bca7bfc8 6742 height_label = m_colLabelHeight - rect_y;
a9339fe2
DS
6743 if (height_label > rectHeight)
6744 height_label = rectHeight;
27f35b66 6745
bca7bfc8
SN
6746 if (rect_x > m_rowLabelWidth)
6747 {
6748 x = rect_x - m_rowLabelWidth;
6749 width_cell = rectWidth;
6750 }
6751 else
6752 {
6753 x = 0;
6754 width_cell = rectWidth - (m_rowLabelWidth - rect_x);
6755 }
6756
6757 if (rect_y > m_colLabelHeight)
6758 {
6759 y = rect_y - m_colLabelHeight;
6760 height_cell = rectHeight;
6761 }
6762 else
6763 {
6764 y = 0;
6765 height_cell = rectHeight - (m_colLabelHeight - rect_y);
6766 }
6767
6768 // Paint corner label part intersecting rect.
6769 if ( width_label > 0 && height_label > 0 )
6770 {
6771 wxRect anotherrect(rect_x, rect_y, width_label, height_label);
6772 m_cornerLabelWin->Refresh(eraseb, &anotherrect);
6773 }
6774
6775 // Paint col labels part intersecting rect.
6776 if ( width_cell > 0 && height_label > 0 )
6777 {
6778 wxRect anotherrect(x, rect_y, width_cell, height_label);
6779 m_colLabelWin->Refresh(eraseb, &anotherrect);
6780 }
6781
6782 // Paint row labels part intersecting rect.
6783 if ( width_label > 0 && height_cell > 0 )
6784 {
6785 wxRect anotherrect(rect_x, y, width_label, height_cell);
6786 m_rowLabelWin->Refresh(eraseb, &anotherrect);
6787 }
6788
6789 // Paint cell area part intersecting rect.
6790 if ( width_cell > 0 && height_cell > 0 )
6791 {
6792 wxRect anotherrect(x, y, width_cell, height_cell);
6793 m_gridWin->Refresh(eraseb, &anotherrect);
6794 }
6795 }
6796 else
6797 {
6798 m_cornerLabelWin->Refresh(eraseb, NULL);
2b5f62a0 6799 m_colLabelWin->Refresh(eraseb, NULL);
bca7bfc8
SN
6800 m_rowLabelWin->Refresh(eraseb, NULL);
6801 m_gridWin->Refresh(eraseb, NULL);
6802 }
27f35b66
SN
6803 }
6804}
f85afd4e 6805
97a9929e 6806void wxGrid::OnSize( wxSizeEvent& event )
f85afd4e 6807{
97a9929e 6808 // position the child windows
7807d81c 6809 CalcWindowSizes();
97a9929e
VZ
6810
6811 // don't call CalcDimensions() from here, the base class handles the size
6812 // changes itself
6813 event.Skip();
f85afd4e
MB
6814}
6815
2d66e025 6816void wxGrid::OnKeyDown( wxKeyEvent& event )
f85afd4e 6817{
2d66e025 6818 if ( m_inOnKeyDown )
f85afd4e 6819 {
2d66e025
MB
6820 // shouldn't be here - we are going round in circles...
6821 //
07296f0b 6822 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") );
f85afd4e
MB
6823 }
6824
ca65c044 6825 m_inOnKeyDown = true;
f85afd4e 6826
2d66e025 6827 // propagate the event up and see if it gets processed
2d66e025
MB
6828 wxWindow *parent = GetParent();
6829 wxKeyEvent keyEvt( event );
6830 keyEvt.SetEventObject( parent );
6831
6832 if ( !parent->GetEventHandler()->ProcessEvent( keyEvt ) )
f85afd4e 6833 {
bcb614b3
RR
6834 if (GetLayoutDirection() == wxLayout_RightToLeft)
6835 {
6836 if (event.GetKeyCode() == WXK_RIGHT)
6837 event.m_keyCode = WXK_LEFT;
6838 else if (event.GetKeyCode() == WXK_LEFT)
6839 event.m_keyCode = WXK_RIGHT;
6840 }
6841
2d66e025 6842 // try local handlers
12a3f227 6843 switch ( event.GetKeyCode() )
2d66e025
MB
6844 {
6845 case WXK_UP:
6846 if ( event.ControlDown() )
5c8fc7c1 6847 MoveCursorUpBlock( event.ShiftDown() );
2d66e025 6848 else
5c8fc7c1 6849 MoveCursorUp( event.ShiftDown() );
2d66e025 6850 break;
f85afd4e 6851
2d66e025
MB
6852 case WXK_DOWN:
6853 if ( event.ControlDown() )
5c8fc7c1 6854 MoveCursorDownBlock( event.ShiftDown() );
2d66e025 6855 else
5c8fc7c1 6856 MoveCursorDown( event.ShiftDown() );
2d66e025 6857 break;
8f177c8e 6858
2d66e025
MB
6859 case WXK_LEFT:
6860 if ( event.ControlDown() )
5c8fc7c1 6861 MoveCursorLeftBlock( event.ShiftDown() );
2d66e025 6862 else
5c8fc7c1 6863 MoveCursorLeft( event.ShiftDown() );
2d66e025
MB
6864 break;
6865
6866 case WXK_RIGHT:
6867 if ( event.ControlDown() )
5c8fc7c1 6868 MoveCursorRightBlock( event.ShiftDown() );
2d66e025 6869 else
5c8fc7c1 6870 MoveCursorRight( event.ShiftDown() );
2d66e025 6871 break;
b99be8fb 6872
2d66e025 6873 case WXK_RETURN:
a4f7bf58 6874 case WXK_NUMPAD_ENTER:
58dd5b3b
MB
6875 if ( event.ControlDown() )
6876 {
6877 event.Skip(); // to let the edit control have the return
6878 }
6879 else
6880 {
f6bcfd97
BP
6881 if ( GetGridCursorRow() < GetNumberRows()-1 )
6882 {
6883 MoveCursorDown( event.ShiftDown() );
6884 }
6885 else
6886 {
6887 // at the bottom of a column
13f6e9e8 6888 DisableCellEditControl();
f6bcfd97 6889 }
58dd5b3b 6890 }
2d66e025
MB
6891 break;
6892
5c8fc7c1 6893 case WXK_ESCAPE:
e32352cf 6894 ClearSelection();
5c8fc7c1
SN
6895 break;
6896
2c9a89e0
RD
6897 case WXK_TAB:
6898 if (event.ShiftDown())
f6bcfd97
BP
6899 {
6900 if ( GetGridCursorCol() > 0 )
6901 {
ca65c044 6902 MoveCursorLeft( false );
f6bcfd97
BP
6903 }
6904 else
6905 {
6906 // at left of grid
13f6e9e8 6907 DisableCellEditControl();
f6bcfd97
BP
6908 }
6909 }
2c9a89e0 6910 else
f6bcfd97 6911 {
ccdee36f 6912 if ( GetGridCursorCol() < GetNumberCols() - 1 )
f6bcfd97 6913 {
ca65c044 6914 MoveCursorRight( false );
f6bcfd97
BP
6915 }
6916 else
6917 {
6918 // at right of grid
13f6e9e8 6919 DisableCellEditControl();
f6bcfd97
BP
6920 }
6921 }
2c9a89e0
RD
6922 break;
6923
2d66e025
MB
6924 case WXK_HOME:
6925 if ( event.ControlDown() )
6926 {
6927 MakeCellVisible( 0, 0 );
6928 SetCurrentCell( 0, 0 );
6929 }
6930 else
6931 {
6932 event.Skip();
6933 }
6934 break;
6935
6936 case WXK_END:
6937 if ( event.ControlDown() )
6938 {
a9339fe2
DS
6939 MakeCellVisible( m_numRows - 1, m_numCols - 1 );
6940 SetCurrentCell( m_numRows - 1, m_numCols - 1 );
2d66e025
MB
6941 }
6942 else
6943 {
6944 event.Skip();
6945 }
6946 break;
6947
faa94f3e 6948 case WXK_PAGEUP:
2d66e025
MB
6949 MovePageUp();
6950 break;
6951
faa94f3e 6952 case WXK_PAGEDOWN:
2d66e025
MB
6953 MovePageDown();
6954 break;
6955
07296f0b 6956 case WXK_SPACE:
aa5e1f75
SN
6957 if ( event.ControlDown() )
6958 {
3f3dc2ef
VZ
6959 if ( m_selection )
6960 {
a9339fe2
DS
6961 m_selection->ToggleCellSelection(
6962 m_currentCellCoords.GetRow(),
6963 m_currentCellCoords.GetCol(),
6964 event.ControlDown(),
6965 event.ShiftDown(),
6966 event.AltDown(),
6967 event.MetaDown() );
3f3dc2ef 6968 }
aa5e1f75
SN
6969 break;
6970 }
ccdee36f 6971
07296f0b 6972 if ( !IsEditable() )
ca65c044 6973 MoveCursorRight( false );
ccdee36f
DS
6974 else
6975 event.Skip();
6976 break;
07296f0b 6977
2d66e025 6978 default:
63e2147c 6979 event.Skip();
025562fe 6980 break;
2d66e025 6981 }
f85afd4e
MB
6982 }
6983
ca65c044 6984 m_inOnKeyDown = false;
f85afd4e
MB
6985}
6986
f6bcfd97
BP
6987void wxGrid::OnKeyUp( wxKeyEvent& event )
6988{
6989 // try local handlers
6990 //
12a3f227 6991 if ( event.GetKeyCode() == WXK_SHIFT )
f6bcfd97
BP
6992 {
6993 if ( m_selectingTopLeft != wxGridNoCellCoords &&
6994 m_selectingBottomRight != wxGridNoCellCoords )
3f3dc2ef
VZ
6995 {
6996 if ( m_selection )
6997 {
a9339fe2
DS
6998 m_selection->SelectBlock(
6999 m_selectingTopLeft.GetRow(),
7000 m_selectingTopLeft.GetCol(),
7001 m_selectingBottomRight.GetRow(),
7002 m_selectingBottomRight.GetCol(),
7003 event.ControlDown(),
7004 true,
7005 event.AltDown(),
7006 event.MetaDown() );
3f3dc2ef
VZ
7007 }
7008 }
7009
f6bcfd97
BP
7010 m_selectingTopLeft = wxGridNoCellCoords;
7011 m_selectingBottomRight = wxGridNoCellCoords;
7012 m_selectingKeyboard = wxGridNoCellCoords;
7013 }
7014}
7015
63e2147c
RD
7016void wxGrid::OnChar( wxKeyEvent& event )
7017{
7018 // is it possible to edit the current cell at all?
7019 if ( !IsCellEditControlEnabled() && CanEnableCellControl() )
7020 {
7021 // yes, now check whether the cells editor accepts the key
7022 int row = m_currentCellCoords.GetRow();
7023 int col = m_currentCellCoords.GetCol();
2f024384 7024 wxGridCellAttr *attr = GetCellAttr(row, col);
63e2147c
RD
7025 wxGridCellEditor *editor = attr->GetEditor(this, row, col);
7026
7027 // <F2> is special and will always start editing, for
7028 // other keys - ask the editor itself
7029 if ( (event.GetKeyCode() == WXK_F2 && !event.HasModifiers())
7030 || editor->IsAcceptedKey(event) )
7031 {
7032 // ensure cell is visble
7033 MakeCellVisible(row, col);
7034 EnableCellEditControl();
7035
7036 // a problem can arise if the cell is not completely
7037 // visible (even after calling MakeCellVisible the
7038 // control is not created and calling StartingKey will
7039 // crash the app
046d682f 7040 if ( event.GetKeyCode() != WXK_F2 && editor->IsCreated() && m_cellEditCtrlEnabled )
63e2147c
RD
7041 editor->StartingKey(event);
7042 }
7043 else
7044 {
7045 event.Skip();
7046 }
7047
7048 editor->DecRef();
7049 attr->DecRef();
7050 }
7051 else
7052 {
7053 event.Skip();
7054 }
7055}
7056
2796cce3 7057void wxGrid::OnEraseBackground(wxEraseEvent&)
508011ce
VZ
7058{
7059}
07296f0b 7060
2d66e025 7061void wxGrid::SetCurrentCell( const wxGridCellCoords& coords )
66242c80 7062{
f6bcfd97
BP
7063 if ( SendEvent( wxEVT_GRID_SELECT_CELL, coords.GetRow(), coords.GetCol() ) )
7064 {
7065 // the event has been intercepted - do nothing
7066 return;
7067 }
7068
bee19958
DS
7069 wxClientDC dc( m_gridWin );
7070 PrepareDC( dc );
f6bcfd97 7071
2a7750d9 7072 if ( m_currentCellCoords != wxGridNoCellCoords )
2d66e025 7073 {
b54ba671 7074 DisableCellEditControl();
07296f0b 7075
ca65c044 7076 if ( IsVisible( m_currentCellCoords, false ) )
f6bcfd97
BP
7077 {
7078 wxRect r;
bee19958 7079 r = BlockToDeviceRect( m_currentCellCoords, m_currentCellCoords );
f6bcfd97
BP
7080 if ( !m_gridLinesEnabled )
7081 {
7082 r.x--;
7083 r.y--;
7084 r.width++;
7085 r.height++;
7086 }
d1c0b4f9 7087
3ed884a0 7088 wxGridCellCoordsArray cells = CalcCellsExposed( r );
84912ef8 7089
f6bcfd97
BP
7090 // Otherwise refresh redraws the highlight!
7091 m_currentCellCoords = coords;
275c4ae4 7092
bee19958 7093 DrawGridCellArea( dc, cells );
f6bcfd97
BP
7094 DrawAllGridLines( dc, r );
7095 }
66242c80 7096 }
8f177c8e 7097
2d66e025
MB
7098 m_currentCellCoords = coords;
7099
bee19958
DS
7100 wxGridCellAttr *attr = GetCellAttr( coords );
7101 DrawCellHighlight( dc, attr );
2a7750d9 7102 attr->DecRef();
66242c80
MB
7103}
7104
c9097836
MB
7105void wxGrid::HighlightBlock( int topRow, int leftCol, int bottomRow, int rightCol )
7106{
7107 int temp;
7108 wxGridCellCoords updateTopLeft, updateBottomRight;
7109
3f3dc2ef 7110 if ( m_selection )
c9097836 7111 {
3f3dc2ef
VZ
7112 if ( m_selection->GetSelectionMode() == wxGrid::wxGridSelectRows )
7113 {
7114 leftCol = 0;
7115 rightCol = GetNumberCols() - 1;
7116 }
7117 else if ( m_selection->GetSelectionMode() == wxGrid::wxGridSelectColumns )
7118 {
7119 topRow = 0;
7120 bottomRow = GetNumberRows() - 1;
7121 }
c9097836 7122 }
3f3dc2ef 7123
c9097836
MB
7124 if ( topRow > bottomRow )
7125 {
7126 temp = topRow;
7127 topRow = bottomRow;
7128 bottomRow = temp;
7129 }
7130
7131 if ( leftCol > rightCol )
7132 {
7133 temp = leftCol;
7134 leftCol = rightCol;
7135 rightCol = temp;
7136 }
7137
7138 updateTopLeft = wxGridCellCoords( topRow, leftCol );
7139 updateBottomRight = wxGridCellCoords( bottomRow, rightCol );
7140
3ed884a0
SN
7141 // First the case that we selected a completely new area
7142 if ( m_selectingTopLeft == wxGridNoCellCoords ||
7143 m_selectingBottomRight == wxGridNoCellCoords )
7144 {
7145 wxRect rect;
7146 rect = BlockToDeviceRect( wxGridCellCoords ( topRow, leftCol ),
7147 wxGridCellCoords ( bottomRow, rightCol ) );
ca65c044 7148 m_gridWin->Refresh( false, &rect );
3ed884a0 7149 }
2f024384 7150
3ed884a0
SN
7151 // Now handle changing an existing selection area.
7152 else if ( m_selectingTopLeft != updateTopLeft ||
7153 m_selectingBottomRight != updateBottomRight )
c9097836
MB
7154 {
7155 // Compute two optimal update rectangles:
7156 // Either one rectangle is a real subset of the
7157 // other, or they are (almost) disjoint!
7158 wxRect rect[4];
7159 bool need_refresh[4];
7160 need_refresh[0] =
7161 need_refresh[1] =
7162 need_refresh[2] =
ca65c044 7163 need_refresh[3] = false;
c9097836
MB
7164 int i;
7165
7166 // Store intermediate values
a9339fe2
DS
7167 wxCoord oldLeft = m_selectingTopLeft.GetCol();
7168 wxCoord oldTop = m_selectingTopLeft.GetRow();
7169 wxCoord oldRight = m_selectingBottomRight.GetCol();
c9097836
MB
7170 wxCoord oldBottom = m_selectingBottomRight.GetRow();
7171
7172 // Determine the outer/inner coordinates.
7173 if (oldLeft > leftCol)
7174 {
7175 temp = oldLeft;
7176 oldLeft = leftCol;
7177 leftCol = temp;
7178 }
7179 if (oldTop > topRow )
7180 {
7181 temp = oldTop;
7182 oldTop = topRow;
7183 topRow = temp;
7184 }
7185 if (oldRight < rightCol )
7186 {
7187 temp = oldRight;
7188 oldRight = rightCol;
7189 rightCol = temp;
7190 }
7191 if (oldBottom < bottomRow)
7192 {
7193 temp = oldBottom;
7194 oldBottom = bottomRow;
7195 bottomRow = temp;
7196 }
7197
7198 // Now, either the stuff marked old is the outer
7199 // rectangle or we don't have a situation where one
7200 // is contained in the other.
7201
7202 if ( oldLeft < leftCol )
7203 {
3ed884a0
SN
7204 // Refresh the newly selected or deselected
7205 // area to the left of the old or new selection.
ca65c044 7206 need_refresh[0] = true;
a9339fe2
DS
7207 rect[0] = BlockToDeviceRect(
7208 wxGridCellCoords( oldTop, oldLeft ),
7209 wxGridCellCoords( oldBottom, leftCol - 1 ) );
c9097836
MB
7210 }
7211
2f024384 7212 if ( oldTop < topRow )
c9097836 7213 {
3ed884a0
SN
7214 // Refresh the newly selected or deselected
7215 // area above the old or new selection.
ca65c044 7216 need_refresh[1] = true;
2f024384
DS
7217 rect[1] = BlockToDeviceRect(
7218 wxGridCellCoords( oldTop, leftCol ),
7219 wxGridCellCoords( topRow - 1, rightCol ) );
c9097836
MB
7220 }
7221
7222 if ( oldRight > rightCol )
7223 {
3ed884a0
SN
7224 // Refresh the newly selected or deselected
7225 // area to the right of the old or new selection.
ca65c044 7226 need_refresh[2] = true;
2f024384 7227 rect[2] = BlockToDeviceRect(
a9339fe2
DS
7228 wxGridCellCoords( oldTop, rightCol + 1 ),
7229 wxGridCellCoords( oldBottom, oldRight ) );
c9097836
MB
7230 }
7231
7232 if ( oldBottom > bottomRow )
7233 {
3ed884a0
SN
7234 // Refresh the newly selected or deselected
7235 // area below the old or new selection.
ca65c044 7236 need_refresh[3] = true;
2f024384 7237 rect[3] = BlockToDeviceRect(
a9339fe2
DS
7238 wxGridCellCoords( bottomRow + 1, leftCol ),
7239 wxGridCellCoords( oldBottom, rightCol ) );
c9097836
MB
7240 }
7241
c9097836
MB
7242 // various Refresh() calls
7243 for (i = 0; i < 4; i++ )
7244 if ( need_refresh[i] && rect[i] != wxGridNoCellRect )
ca65c044 7245 m_gridWin->Refresh( false, &(rect[i]) );
c9097836 7246 }
2f024384
DS
7247
7248 // change selection
3ed884a0
SN
7249 m_selectingTopLeft = updateTopLeft;
7250 m_selectingBottomRight = updateBottomRight;
c9097836
MB
7251}
7252
2d66e025
MB
7253//
7254// ------ functions to get/send data (see also public functions)
7255//
7256
7257bool wxGrid::GetModelValues()
66242c80 7258{
bca7bfc8
SN
7259 // Hide the editor, so it won't hide a changed value.
7260 HideCellEditControl();
c6707d16 7261
2d66e025 7262 if ( m_table )
66242c80 7263 {
2d66e025 7264 // all we need to do is repaint the grid
66242c80 7265 //
2d66e025 7266 m_gridWin->Refresh();
ca65c044 7267 return true;
66242c80 7268 }
8f177c8e 7269
ca65c044 7270 return false;
66242c80
MB
7271}
7272
2d66e025 7273bool wxGrid::SetModelValues()
f85afd4e 7274{
2d66e025 7275 int row, col;
8f177c8e 7276
c6707d16
SN
7277 // Disable the editor, so it won't hide a changed value.
7278 // Do we also want to save the current value of the editor first?
7279 // I think so ...
c6707d16
SN
7280 DisableCellEditControl();
7281
2d66e025
MB
7282 if ( m_table )
7283 {
56b6cf26 7284 for ( row = 0; row < m_numRows; row++ )
f85afd4e 7285 {
56b6cf26 7286 for ( col = 0; col < m_numCols; col++ )
f85afd4e 7287 {
2d66e025 7288 m_table->SetValue( row, col, GetCellValue(row, col) );
f85afd4e
MB
7289 }
7290 }
8f177c8e 7291
ca65c044 7292 return true;
f85afd4e
MB
7293 }
7294
ca65c044 7295 return false;
f85afd4e
MB
7296}
7297
2d66e025
MB
7298// Note - this function only draws cells that are in the list of
7299// exposed cells (usually set from the update region by
7300// CalcExposedCells)
7301//
d10f4bf9 7302void wxGrid::DrawGridCellArea( wxDC& dc, const wxGridCellCoordsArray& cells )
f85afd4e 7303{
4db6714b
KH
7304 if ( !m_numRows || !m_numCols )
7305 return;
60ff3b99 7306
dc1f566f 7307 int i, numCells = cells.GetCount();
27f35b66
SN
7308 int row, col, cell_rows, cell_cols;
7309 wxGridCellCoordsArray redrawCells;
60ff3b99 7310
a9339fe2 7311 for ( i = numCells - 1; i >= 0; i-- )
f85afd4e 7312 {
27f35b66
SN
7313 row = cells[i].GetRow();
7314 col = cells[i].GetCol();
7315 GetCellSize( row, col, &cell_rows, &cell_cols );
7316
7317 // If this cell is part of a multicell block, find owner for repaint
7318 if ( cell_rows <= 0 || cell_cols <= 0 )
7319 {
a9339fe2 7320 wxGridCellCoords cell( row + cell_rows, col + cell_cols );
ca65c044 7321 bool marked = false;
56b6cf26 7322 for ( int j = 0; j < numCells; j++ )
27f35b66
SN
7323 {
7324 if ( cell == cells[j] )
7325 {
ca65c044 7326 marked = true;
3ed884a0 7327 break;
27f35b66
SN
7328 }
7329 }
2f024384 7330
27f35b66
SN
7331 if (!marked)
7332 {
7333 int count = redrawCells.GetCount();
dc1f566f 7334 for (int j = 0; j < count; j++)
27f35b66
SN
7335 {
7336 if ( cell == redrawCells[j] )
7337 {
ca65c044 7338 marked = true;
27f35b66
SN
7339 break;
7340 }
7341 }
2f024384 7342
4db6714b
KH
7343 if (!marked)
7344 redrawCells.Add( cell );
27f35b66 7345 }
2f024384
DS
7346
7347 // don't bother drawing this cell
7348 continue;
27f35b66
SN
7349 }
7350
7351 // If this cell is empty, find cell to left that might want to overflow
7352 if (m_table && m_table->IsEmptyCell(row, col))
7353 {
dc1f566f 7354 for ( int l = 0; l < cell_rows; l++ )
27f35b66 7355 {
56b6cf26 7356 // find a cell in this row to leave already marked for repaint
dc1f566f
SN
7357 int left = col;
7358 for (int k = 0; k < int(redrawCells.GetCount()); k++)
7359 if ((redrawCells[k].GetCol() < left) &&
7360 (redrawCells[k].GetRow() == row))
2f024384 7361 {
4db6714b 7362 left = redrawCells[k].GetCol();
2f024384 7363 }
dc1f566f 7364
4db6714b
KH
7365 if (left == col)
7366 left = 0; // oh well
dc1f566f 7367
2f024384 7368 for (int j = col - 1; j >= left; j--)
27f35b66 7369 {
2f024384 7370 if (!m_table->IsEmptyCell(row + l, j))
27f35b66 7371 {
2f024384 7372 if (GetCellOverflow(row + l, j))
27f35b66 7373 {
2f024384 7374 wxGridCellCoords cell(row + l, j);
ca65c044 7375 bool marked = false;
27f35b66 7376
dc1f566f 7377 for (int k = 0; k < numCells; k++)
27f35b66
SN
7378 {
7379 if ( cell == cells[k] )
7380 {
ca65c044 7381 marked = true;
27f35b66
SN
7382 break;
7383 }
7384 }
4db6714b 7385
27f35b66
SN
7386 if (!marked)
7387 {
7388 int count = redrawCells.GetCount();
dc1f566f 7389 for (int k = 0; k < count; k++)
27f35b66
SN
7390 {
7391 if ( cell == redrawCells[k] )
7392 {
ca65c044 7393 marked = true;
27f35b66
SN
7394 break;
7395 }
7396 }
4db6714b
KH
7397 if (!marked)
7398 redrawCells.Add( cell );
27f35b66
SN
7399 }
7400 }
7401 break;
7402 }
7403 }
7404 }
7405 }
4db6714b 7406
d10f4bf9 7407 DrawCell( dc, cells[i] );
2d66e025 7408 }
27f35b66
SN
7409
7410 numCells = redrawCells.GetCount();
7411
56b6cf26 7412 for ( i = numCells - 1; i >= 0; i-- )
27f35b66
SN
7413 {
7414 DrawCell( dc, redrawCells[i] );
7415 }
2d66e025 7416}
8f177c8e 7417
7c8a8ad5
MB
7418void wxGrid::DrawGridSpace( wxDC& dc )
7419{
f6bcfd97
BP
7420 int cw, ch;
7421 m_gridWin->GetClientSize( &cw, &ch );
7c8a8ad5 7422
f6bcfd97
BP
7423 int right, bottom;
7424 CalcUnscrolledPosition( cw, ch, &right, &bottom );
7c8a8ad5 7425
d4175745 7426 int rightCol = m_numCols > 0 ? GetColRight(GetColAt( m_numCols - 1 )) : 0;
2f024384 7427 int bottomRow = m_numRows > 0 ? GetRowBottom(m_numRows - 1) : 0;
7c8a8ad5 7428
f6bcfd97
BP
7429 if ( right > rightCol || bottom > bottomRow )
7430 {
7431 int left, top;
7432 CalcUnscrolledPosition( 0, 0, &left, &top );
7c8a8ad5 7433
f6bcfd97
BP
7434 dc.SetBrush( wxBrush(GetDefaultCellBackgroundColour(), wxSOLID) );
7435 dc.SetPen( *wxTRANSPARENT_PEN );
7c8a8ad5 7436
f6bcfd97
BP
7437 if ( right > rightCol )
7438 {
a9339fe2 7439 dc.DrawRectangle( rightCol, top, right - rightCol, ch );
f6bcfd97
BP
7440 }
7441
7442 if ( bottom > bottomRow )
7443 {
a9339fe2 7444 dc.DrawRectangle( left, bottomRow, cw, bottom - bottomRow );
f6bcfd97
BP
7445 }
7446 }
7c8a8ad5
MB
7447}
7448
2d66e025
MB
7449void wxGrid::DrawCell( wxDC& dc, const wxGridCellCoords& coords )
7450{
ab79958a
VZ
7451 int row = coords.GetRow();
7452 int col = coords.GetCol();
60ff3b99 7453
7c1cb261 7454 if ( GetColWidth(col) <= 0 || GetRowHeight(row) <= 0 )
ab79958a
VZ
7455 return;
7456
7457 // we draw the cell border ourselves
9496deb5 7458#if !WXGRID_DRAW_LINES
2d66e025
MB
7459 if ( m_gridLinesEnabled )
7460 DrawCellBorder( dc, coords );
796df70a 7461#endif
f85afd4e 7462
189d0213
VZ
7463 wxGridCellAttr* attr = GetCellAttr(row, col);
7464
7465 bool isCurrent = coords == m_currentCellCoords;
508011ce 7466
f6bcfd97 7467 wxRect rect = CellToRect( row, col );
f85afd4e 7468
189d0213 7469 // if the editor is shown, we should use it and not the renderer
f6bcfd97
BP
7470 // Note: However, only if it is really _shown_, i.e. not hidden!
7471 if ( isCurrent && IsCellEditControlShown() )
189d0213 7472 {
a9339fe2 7473 // NB: this "#if..." is temporary and fixes a problem where the
962a48f6
DS
7474 // edit control is erased by this code after being rendered.
7475 // On wxMac (QD build only), the cell editor is a wxTextCntl and is rendered
7476 // implicitly, causing this out-of order render.
7477#if !defined(__WXMAC__) || wxMAC_USE_CORE_GRAPHICS
0b190b0f
VZ
7478 wxGridCellEditor *editor = attr->GetEditor(this, row, col);
7479 editor->PaintBackground(rect, attr);
7480 editor->DecRef();
962a48f6 7481#endif
189d0213
VZ
7482 }
7483 else
7484 {
a9339fe2 7485 // but all the rest is drawn by the cell renderer and hence may be customized
0b190b0f
VZ
7486 wxGridCellRenderer *renderer = attr->GetRenderer(this, row, col);
7487 renderer->Draw(*this, *attr, dc, rect, row, col, IsInSelection(coords));
7488 renderer->DecRef();
189d0213 7489 }
07296f0b 7490
283b7808
VZ
7491 attr->DecRef();
7492}
07296f0b 7493
283b7808 7494void wxGrid::DrawCellHighlight( wxDC& dc, const wxGridCellAttr *attr )
07296f0b
RD
7495{
7496 int row = m_currentCellCoords.GetRow();
7497 int col = m_currentCellCoords.GetCol();
7498
7c1cb261 7499 if ( GetColWidth(col) <= 0 || GetRowHeight(row) <= 0 )
07296f0b
RD
7500 return;
7501
f6bcfd97 7502 wxRect rect = CellToRect(row, col);
07296f0b 7503
b3a7510d
VZ
7504 // hmmm... what could we do here to show that the cell is disabled?
7505 // for now, I just draw a thinner border than for the other ones, but
7506 // it doesn't look really good
b3a7510d 7507
d2520c85
RD
7508 int penWidth = attr->IsReadOnly() ? m_cellHighlightROPenWidth : m_cellHighlightPenWidth;
7509
27f35b66
SN
7510 if (penWidth > 0)
7511 {
56b6cf26
DS
7512 // The center of the drawn line is where the position/width/height of
7513 // the rectangle is actually at (on wxMSW at least), so the
7514 // size of the rectangle is reduced to compensate for the thickness of
7515 // the line. If this is too strange on non-wxMSW platforms then
d2520c85 7516 // please #ifdef this appropriately.
4db6714b
KH
7517 rect.x += penWidth / 2;
7518 rect.y += penWidth / 2;
7519 rect.width -= penWidth - 1;
7520 rect.height -= penWidth - 1;
d2520c85 7521
d2520c85 7522 // Now draw the rectangle
73145b0e
JS
7523 // use the cellHighlightColour if the cell is inside a selection, this
7524 // will ensure the cell is always visible.
4db6714b 7525 dc.SetPen(wxPen(IsInSelection(row,col) ? m_selectionForeground : m_cellHighlightColour, penWidth, wxSOLID));
d2520c85
RD
7526 dc.SetBrush(*wxTRANSPARENT_BRUSH);
7527 dc.DrawRectangle(rect);
7528 }
07296f0b 7529
283b7808 7530#if 0
2f024384 7531 // VZ: my experiments with 3D borders...
283b7808 7532
b3a7510d 7533 // how to properly set colours for arbitrary bg?
283b7808
VZ
7534 wxCoord x1 = rect.x,
7535 y1 = rect.y,
4db6714b
KH
7536 x2 = rect.x + rect.width - 1,
7537 y2 = rect.y + rect.height - 1;
283b7808
VZ
7538
7539 dc.SetPen(*wxWHITE_PEN);
00cf1208
RR
7540 dc.DrawLine(x1, y1, x2, y1);
7541 dc.DrawLine(x1, y1, x1, y2);
283b7808 7542
283b7808 7543 dc.DrawLine(x1 + 1, y2 - 1, x2 - 1, y2 - 1);
2f024384 7544 dc.DrawLine(x2 - 1, y1 + 1, x2 - 1, y2);
283b7808
VZ
7545
7546 dc.SetPen(*wxBLACK_PEN);
7547 dc.DrawLine(x1, y2, x2, y2);
2f024384 7548 dc.DrawLine(x2, y1, x2, y2 + 1);
a9339fe2 7549#endif
2d66e025 7550}
f85afd4e 7551
3d3f3e37
VZ
7552wxPen wxGrid::GetDefaultGridLinePen()
7553{
7554 return wxPen(GetGridLineColour(), 1, wxSOLID);
7555}
7556
7557wxPen wxGrid::GetRowGridLinePen(int WXUNUSED(row))
7558{
7559 return GetDefaultGridLinePen();
7560}
7561
7562wxPen wxGrid::GetColGridLinePen(int WXUNUSED(col))
7563{
7564 return GetDefaultGridLinePen();
7565}
7566
2d66e025
MB
7567void wxGrid::DrawCellBorder( wxDC& dc, const wxGridCellCoords& coords )
7568{
2d66e025
MB
7569 int row = coords.GetRow();
7570 int col = coords.GetCol();
7c1cb261
VZ
7571 if ( GetColWidth(col) <= 0 || GetRowHeight(row) <= 0 )
7572 return;
7573
60ff3b99 7574
27f35b66
SN
7575 wxRect rect = CellToRect( row, col );
7576
2d66e025 7577 // right hand border
3d3f3e37 7578 dc.SetPen( GetColGridLinePen(col) );
27f35b66
SN
7579 dc.DrawLine( rect.x + rect.width, rect.y,
7580 rect.x + rect.width, rect.y + rect.height + 1 );
2d66e025
MB
7581
7582 // bottom border
3d3f3e37 7583 dc.SetPen( GetRowGridLinePen(row) );
2f024384 7584 dc.DrawLine( rect.x, rect.y + rect.height,
27f35b66 7585 rect.x + rect.width, rect.y + rect.height);
f85afd4e
MB
7586}
7587
2f024384 7588void wxGrid::DrawHighlight(wxDC& dc, const wxGridCellCoordsArray& cells)
b3a7510d 7589{
2a7750d9
MB
7590 // This if block was previously in wxGrid::OnPaint but that doesn't
7591 // seem to get called under wxGTK - MB
7592 //
2f024384 7593 if ( m_currentCellCoords == wxGridNoCellCoords &&
2a7750d9
MB
7594 m_numRows && m_numCols )
7595 {
7596 m_currentCellCoords.Set(0, 0);
7597 }
7598
f6bcfd97 7599 if ( IsCellEditControlShown() )
99306db2
VZ
7600 {
7601 // don't show highlight when the edit control is shown
7602 return;
7603 }
7604
b3a7510d
VZ
7605 // if the active cell was repainted, repaint its highlight too because it
7606 // might have been damaged by the grid lines
d10f4bf9 7607 size_t count = cells.GetCount();
b3a7510d
VZ
7608 for ( size_t n = 0; n < count; n++ )
7609 {
d10f4bf9 7610 if ( cells[n] == m_currentCellCoords )
b3a7510d
VZ
7611 {
7612 wxGridCellAttr* attr = GetCellAttr(m_currentCellCoords);
7613 DrawCellHighlight(dc, attr);
7614 attr->DecRef();
7615
7616 break;
7617 }
7618 }
7619}
2d66e025 7620
2d66e025
MB
7621// TODO: remove this ???
7622// This is used to redraw all grid lines e.g. when the grid line colour
7623// has been changed
7624//
33ac7e6f 7625void wxGrid::DrawAllGridLines( wxDC& dc, const wxRegion & WXUNUSED(reg) )
f85afd4e 7626{
27f35b66
SN
7627#if !WXGRID_DRAW_LINES
7628 return;
7629#endif
7630
afd0f084 7631 if ( !m_gridLinesEnabled || !m_numRows || !m_numCols )
4db6714b 7632 return;
f85afd4e 7633
2d66e025 7634 int top, bottom, left, right;
796df70a 7635
f6bcfd97 7636#if 0 //#ifndef __WXGTK__
508011ce
VZ
7637 if (reg.IsEmpty())
7638 {
796df70a
SN
7639 int cw, ch;
7640 m_gridWin->GetClientSize(&cw, &ch);
7641
7642 // virtual coords of visible area
7643 //
7644 CalcUnscrolledPosition( 0, 0, &left, &top );
7645 CalcUnscrolledPosition( cw, ch, &right, &bottom );
7646 }
508011ce
VZ
7647 else
7648 {
796df70a
SN
7649 wxCoord x, y, w, h;
7650 reg.GetBox(x, y, w, h);
7651 CalcUnscrolledPosition( x, y, &left, &top );
7652 CalcUnscrolledPosition( x + w, y + h, &right, &bottom );
7653 }
8e217128
RR
7654#else
7655 int cw, ch;
7656 m_gridWin->GetClientSize(&cw, &ch);
7657 CalcUnscrolledPosition( 0, 0, &left, &top );
7658 CalcUnscrolledPosition( cw, ch, &right, &bottom );
7659#endif
f85afd4e 7660
9496deb5
MB
7661 // avoid drawing grid lines past the last row and col
7662 //
d4175745 7663 right = wxMin( right, GetColRight(GetColAt( m_numCols - 1 )) );
7c1cb261 7664 bottom = wxMin( bottom, GetRowBottom(m_numRows - 1) );
9496deb5 7665
27f35b66 7666 // no gridlines inside multicells, clip them out
d4175745 7667 int leftCol = GetColPos( internalXToCol(left) );
a9339fe2 7668 int topRow = internalYToRow(top);
d4175745 7669 int rightCol = GetColPos( internalXToCol(right) );
a967f048 7670 int bottomRow = internalYToRow(bottom);
27f35b66 7671
c03bf0c7
SC
7672#ifndef __WXMAC__
7673 // CS: I don't know why suddenly unscrolled coordinates are used for clipping
7674 wxRegion clippedcells(0, 0, cw, ch);
27f35b66 7675
a967f048
RG
7676 int i, j, cell_rows, cell_cols;
7677 wxRect rect;
27f35b66 7678
a967f048
RG
7679 for (j=topRow; j<bottomRow; j++)
7680 {
d4175745
VZ
7681 int colPos;
7682 for (colPos=leftCol; colPos<rightCol; colPos++)
27f35b66 7683 {
d4175745
VZ
7684 i = GetColAt( colPos );
7685
a967f048
RG
7686 GetCellSize( j, i, &cell_rows, &cell_cols );
7687 if ((cell_rows > 1) || (cell_cols > 1))
27f35b66 7688 {
a967f048
RG
7689 rect = CellToRect(j,i);
7690 CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
7691 clippedcells.Subtract(rect);
7692 }
7693 else if ((cell_rows < 0) || (cell_cols < 0))
7694 {
a9339fe2 7695 rect = CellToRect(j + cell_rows, i + cell_cols);
a967f048
RG
7696 CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
7697 clippedcells.Subtract(rect);
27f35b66
SN
7698 }
7699 }
7700 }
c03bf0c7 7701#else
c2f5b920 7702 wxRegion clippedcells( left, top, right - left, bottom - top );
c03bf0c7
SC
7703
7704 int i, j, cell_rows, cell_cols;
7705 wxRect rect;
7706
7707 for (j=topRow; j<bottomRow; j++)
7708 {
7709 for (i=leftCol; i<rightCol; i++)
7710 {
7711 GetCellSize( j, i, &cell_rows, &cell_cols );
7712 if ((cell_rows > 1) || (cell_cols > 1))
7713 {
2f024384 7714 rect = CellToRect(j, i);
c03bf0c7
SC
7715 clippedcells.Subtract(rect);
7716 }
7717 else if ((cell_rows < 0) || (cell_cols < 0))
7718 {
2f024384 7719 rect = CellToRect(j + cell_rows, i + cell_cols);
c03bf0c7
SC
7720 clippedcells.Subtract(rect);
7721 }
7722 }
7723 }
7724#endif
a9339fe2 7725
27f35b66
SN
7726 dc.SetClippingRegion( clippedcells );
7727
f85afd4e 7728
2d66e025 7729 // horizontal grid lines
f85afd4e 7730 //
a967f048 7731 // already declared above - int i;
33188aa4 7732 for ( i = internalYToRow(top); i < m_numRows; i++ )
f85afd4e 7733 {
6d55126d 7734 int bot = GetRowBottom(i) - 1;
7c1cb261 7735
6d55126d 7736 if ( bot > bottom )
2d66e025
MB
7737 {
7738 break;
7739 }
7c1cb261 7740
6d55126d 7741 if ( bot >= top )
2d66e025 7742 {
3d3f3e37 7743 dc.SetPen( GetRowGridLinePen(i) );
6d55126d 7744 dc.DrawLine( left, bot, right, bot );
2d66e025 7745 }
f85afd4e
MB
7746 }
7747
2d66e025
MB
7748 // vertical grid lines
7749 //
d4175745
VZ
7750 int colPos;
7751 for ( colPos = leftCol; colPos < m_numCols; colPos++ )
f85afd4e 7752 {
d4175745
VZ
7753 i = GetColAt( colPos );
7754
2121eb69
RR
7755 int colRight = GetColRight(i);
7756#ifdef __WXGTK__
7757 if (GetLayoutDirection() != wxLayout_RightToLeft)
7758#endif
7759 colRight--;
7760
7c1cb261 7761 if ( colRight > right )
2d66e025
MB
7762 {
7763 break;
7764 }
7c1cb261
VZ
7765
7766 if ( colRight >= left )
2d66e025 7767 {
3d3f3e37 7768 dc.SetPen( GetColGridLinePen(i) );
7c1cb261 7769 dc.DrawLine( colRight, top, colRight, bottom );
2d66e025
MB
7770 }
7771 }
a9339fe2 7772
27f35b66 7773 dc.DestroyClippingRegion();
2d66e025 7774}
f85afd4e 7775
c2f5b920 7776void wxGrid::DrawRowLabels( wxDC& dc, const wxArrayInt& rows)
2d66e025 7777{
4db6714b
KH
7778 if ( !m_numRows )
7779 return;
60ff3b99 7780
2d66e025 7781 size_t i;
d10f4bf9 7782 size_t numLabels = rows.GetCount();
60ff3b99 7783
2f024384 7784 for ( i = 0; i < numLabels; i++ )
2d66e025 7785 {
d10f4bf9 7786 DrawRowLabel( dc, rows[i] );
60ff3b99 7787 }
f85afd4e
MB
7788}
7789
2d66e025 7790void wxGrid::DrawRowLabel( wxDC& dc, int row )
f85afd4e 7791{
659af826 7792 if ( GetRowHeight(row) <= 0 || m_rowLabelWidth <= 0 )
7c1cb261 7793 return;
60ff3b99 7794
4d1bc39c 7795 wxRect rect;
2f024384 7796
c2651b0a 7797#ifdef __WXGTK20__
4d1bc39c
RR
7798 rect.SetX( 1 );
7799 rect.SetY( GetRowTop(row) + 1 );
7800 rect.SetWidth( m_rowLabelWidth - 2 );
7801 rect.SetHeight( GetRowHeight(row) - 2 );
ec157c8f 7802
4d1bc39c 7803 CalcScrolledPosition( 0, rect.y, NULL, &rect.y );
ec157c8f 7804
4d1bc39c
RR
7805 wxWindowDC *win_dc = (wxWindowDC*) &dc;
7806
7807 wxRendererNative::Get().DrawHeaderButton( win_dc->m_owner, dc, rect, 0 );
7808#else
7c1cb261
VZ
7809 int rowTop = GetRowTop(row),
7810 rowBottom = GetRowBottom(row) - 1;
b99be8fb 7811
d4175745 7812 dc.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW), 1, wxSOLID) );
a9339fe2 7813 dc.DrawLine( m_rowLabelWidth - 1, rowTop, m_rowLabelWidth - 1, rowBottom );
73145b0e 7814 dc.DrawLine( 0, rowTop, 0, rowBottom );
73145b0e 7815 dc.DrawLine( 0, rowBottom, m_rowLabelWidth, rowBottom );
b99be8fb 7816
2d66e025 7817 dc.SetPen( *wxWHITE_PEN );
73145b0e 7818 dc.DrawLine( 1, rowTop, 1, rowBottom );
a9339fe2 7819 dc.DrawLine( 1, rowTop, m_rowLabelWidth - 1, rowTop );
4d1bc39c 7820#endif
2f024384 7821
f85afd4e 7822 dc.SetBackgroundMode( wxTRANSPARENT );
f85afd4e
MB
7823 dc.SetTextForeground( GetLabelTextColour() );
7824 dc.SetFont( GetLabelFont() );
8f177c8e 7825
f85afd4e 7826 int hAlign, vAlign;
2d66e025 7827 GetRowLabelAlignment( &hAlign, &vAlign );
60ff3b99 7828
2d66e025 7829 rect.SetX( 2 );
7c1cb261 7830 rect.SetY( GetRowTop(row) + 2 );
2d66e025 7831 rect.SetWidth( m_rowLabelWidth - 4 );
7c1cb261 7832 rect.SetHeight( GetRowHeight(row) - 4 );
60ff3b99 7833 DrawTextRectangle( dc, GetRowLabelValue( row ), rect, hAlign, vAlign );
f85afd4e
MB
7834}
7835
d10f4bf9 7836void wxGrid::DrawColLabels( wxDC& dc,const wxArrayInt& cols )
f85afd4e 7837{
4db6714b
KH
7838 if ( !m_numCols )
7839 return;
60ff3b99 7840
2d66e025 7841 size_t i;
d10f4bf9 7842 size_t numLabels = cols.GetCount();
60ff3b99 7843
56b6cf26 7844 for ( i = 0; i < numLabels; i++ )
f85afd4e 7845 {
d10f4bf9 7846 DrawColLabel( dc, cols[i] );
60ff3b99 7847 }
f85afd4e
MB
7848}
7849
2d66e025 7850void wxGrid::DrawColLabel( wxDC& dc, int col )
f85afd4e 7851{
659af826 7852 if ( GetColWidth(col) <= 0 || m_colLabelHeight <= 0 )
7c1cb261 7853 return;
60ff3b99 7854
4d1bc39c 7855 int colLeft = GetColLeft(col);
ec157c8f 7856
4d1bc39c 7857 wxRect rect;
2f024384 7858
c2651b0a 7859#ifdef __WXGTK20__
4d1bc39c
RR
7860 rect.SetX( colLeft + 1 );
7861 rect.SetY( 1 );
7862 rect.SetWidth( GetColWidth(col) - 2 );
7863 rect.SetHeight( m_colLabelHeight - 2 );
ec157c8f 7864
4d1bc39c
RR
7865 wxWindowDC *win_dc = (wxWindowDC*) &dc;
7866
7867 wxRendererNative::Get().DrawHeaderButton( win_dc->m_owner, dc, rect, 0 );
7868#else
7869 int colRight = GetColRight(col) - 1;
b99be8fb 7870
d4175745 7871 dc.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW), 1, wxSOLID) );
ccdee36f 7872 dc.DrawLine( colRight, 0, colRight, m_colLabelHeight - 1 );
73145b0e 7873 dc.DrawLine( colLeft, 0, colRight, 0 );
ccdee36f
DS
7874 dc.DrawLine( colLeft, m_colLabelHeight - 1,
7875 colRight + 1, m_colLabelHeight - 1 );
b99be8fb 7876
f85afd4e 7877 dc.SetPen( *wxWHITE_PEN );
ccdee36f 7878 dc.DrawLine( colLeft, 1, colLeft, m_colLabelHeight - 1 );
73145b0e 7879 dc.DrawLine( colLeft, 1, colRight, 1 );
4d1bc39c 7880#endif
2f024384 7881
b0d6ff2f
MB
7882 dc.SetBackgroundMode( wxTRANSPARENT );
7883 dc.SetTextForeground( GetLabelTextColour() );
7884 dc.SetFont( GetLabelFont() );
8f177c8e 7885
d43851f7 7886 int hAlign, vAlign, orient;
2d66e025 7887 GetColLabelAlignment( &hAlign, &vAlign );
d43851f7 7888 orient = GetColLabelTextOrientation();
60ff3b99 7889
7c1cb261 7890 rect.SetX( colLeft + 2 );
2d66e025 7891 rect.SetY( 2 );
7c1cb261 7892 rect.SetWidth( GetColWidth(col) - 4 );
2d66e025 7893 rect.SetHeight( m_colLabelHeight - 4 );
d43851f7 7894 DrawTextRectangle( dc, GetColLabelValue( col ), rect, hAlign, vAlign, orient );
f85afd4e
MB
7895}
7896
2d66e025
MB
7897void wxGrid::DrawTextRectangle( wxDC& dc,
7898 const wxString& value,
7899 const wxRect& rect,
7900 int horizAlign,
d43851f7
JS
7901 int vertAlign,
7902 int textOrientation )
f85afd4e 7903{
2d66e025 7904 wxArrayString lines;
ef5df12b 7905
2d66e025 7906 StringToLines( value, lines );
ef5df12b 7907
2f024384 7908 // Forward to new API.
a9339fe2 7909 DrawTextRectangle( dc,
038a5591
JS
7910 lines,
7911 rect,
7912 horizAlign,
7913 vertAlign,
7914 textOrientation );
d10f4bf9
VZ
7915}
7916
4330c974
VZ
7917// VZ: this should be replaced with wxDC::DrawLabel() to which we just have to
7918// add textOrientation support
7919void wxGrid::DrawTextRectangle(wxDC& dc,
038a5591
JS
7920 const wxArrayString& lines,
7921 const wxRect& rect,
7922 int horizAlign,
7923 int vertAlign,
4330c974 7924 int textOrientation)
d10f4bf9 7925{
4330c974
VZ
7926 if ( lines.empty() )
7927 return;
ef5df12b 7928
4330c974 7929 wxDCClipper clip(dc, rect);
ef5df12b 7930
4330c974
VZ
7931 long textWidth,
7932 textHeight;
ef5df12b 7933
4330c974
VZ
7934 if ( textOrientation == wxHORIZONTAL )
7935 GetTextBoxSize( dc, lines, &textWidth, &textHeight );
7936 else
7937 GetTextBoxSize( dc, lines, &textHeight, &textWidth );
ef5df12b 7938
4330c974
VZ
7939 int x = 0,
7940 y = 0;
7941 switch ( vertAlign )
7942 {
73145b0e 7943 case wxALIGN_BOTTOM:
4db6714b 7944 if ( textOrientation == wxHORIZONTAL )
038a5591 7945 y = rect.y + (rect.height - textHeight - 1);
d43851f7 7946 else
038a5591
JS
7947 x = rect.x + rect.width - textWidth;
7948 break;
ef5df12b 7949
73145b0e 7950 case wxALIGN_CENTRE:
4db6714b 7951 if ( textOrientation == wxHORIZONTAL )
a9339fe2 7952 y = rect.y + ((rect.height - textHeight) / 2);
d43851f7 7953 else
a9339fe2 7954 x = rect.x + ((rect.width - textWidth) / 2);
038a5591 7955 break;
ef5df12b 7956
73145b0e
JS
7957 case wxALIGN_TOP:
7958 default:
4db6714b 7959 if ( textOrientation == wxHORIZONTAL )
038a5591 7960 y = rect.y + 1;
d43851f7 7961 else
038a5591 7962 x = rect.x + 1;
73145b0e 7963 break;
4330c974 7964 }
ef5df12b 7965
4330c974
VZ
7966 // Align each line of a multi-line label
7967 size_t nLines = lines.GetCount();
7968 for ( size_t l = 0; l < nLines; l++ )
7969 {
7970 const wxString& line = lines[l];
ef5df12b 7971
9b7d3c09
VZ
7972 if ( line.empty() )
7973 {
7974 *(textOrientation == wxHORIZONTAL ? &y : &x) += dc.GetCharHeight();
7975 continue;
7976 }
7977
c2b2c10e
MW
7978 long lineWidth = 0,
7979 lineHeight = 0;
4330c974
VZ
7980 dc.GetTextExtent(line, &lineWidth, &lineHeight);
7981
7982 switch ( horizAlign )
7983 {
038a5591 7984 case wxALIGN_RIGHT:
4db6714b 7985 if ( textOrientation == wxHORIZONTAL )
038a5591
JS
7986 x = rect.x + (rect.width - lineWidth - 1);
7987 else
7988 y = rect.y + lineWidth + 1;
7989 break;
ef5df12b 7990
038a5591 7991 case wxALIGN_CENTRE:
4db6714b 7992 if ( textOrientation == wxHORIZONTAL )
2f024384 7993 x = rect.x + ((rect.width - lineWidth) / 2);
038a5591 7994 else
2f024384 7995 y = rect.y + rect.height - ((rect.height - lineWidth) / 2);
038a5591 7996 break;
ef5df12b 7997
038a5591
JS
7998 case wxALIGN_LEFT:
7999 default:
4db6714b 8000 if ( textOrientation == wxHORIZONTAL )
038a5591
JS
8001 x = rect.x + 1;
8002 else
8003 y = rect.y + rect.height - 1;
8004 break;
4330c974 8005 }
ef5df12b 8006
4330c974
VZ
8007 if ( textOrientation == wxHORIZONTAL )
8008 {
8009 dc.DrawText( line, x, y );
8010 y += lineHeight;
8011 }
8012 else
8013 {
8014 dc.DrawRotatedText( line, x, y, 90.0 );
8015 x += lineHeight;
038a5591 8016 }
f85afd4e 8017 }
f85afd4e
MB
8018}
8019
2f024384
DS
8020// Split multi-line text up into an array of strings.
8021// Any existing contents of the string array are preserved.
f85afd4e
MB
8022//
8023void wxGrid::StringToLines( const wxString& value, wxArrayString& lines )
8024{
f85afd4e
MB
8025 int startPos = 0;
8026 int pos;
6d004f67 8027 wxString eol = wxTextFile::GetEOL( wxTextFileType_Unix );
2433bb2e 8028 wxString tVal = wxTextFile::Translate( value, wxTextFileType_Unix );
e2b42eeb 8029
faa94f3e 8030 while ( startPos < (int)tVal.length() )
f85afd4e 8031 {
2433bb2e 8032 pos = tVal.Mid(startPos).Find( eol );
f85afd4e
MB
8033 if ( pos < 0 )
8034 {
8035 break;
8036 }
8037 else if ( pos == 0 )
8038 {
8039 lines.Add( wxEmptyString );
8040 }
8041 else
8042 {
2433bb2e 8043 lines.Add( value.Mid(startPos, pos) );
f85afd4e 8044 }
a9339fe2 8045
4db6714b 8046 startPos += pos + 1;
f85afd4e 8047 }
4db6714b 8048
faa94f3e 8049 if ( startPos < (int)value.length() )
f85afd4e
MB
8050 {
8051 lines.Add( value.Mid( startPos ) );
8052 }
8053}
8054
fbfb8bcc 8055void wxGrid::GetTextBoxSize( const wxDC& dc,
d10f4bf9 8056 const wxArrayString& lines,
f85afd4e
MB
8057 long *width, long *height )
8058{
8059 long w = 0;
8060 long h = 0;
8d7eaf91 8061 long lineW = 0, lineH = 0;
f85afd4e 8062
b540eb2b 8063 size_t i;
56b6cf26 8064 for ( i = 0; i < lines.GetCount(); i++ )
f85afd4e
MB
8065 {
8066 dc.GetTextExtent( lines[i], &lineW, &lineH );
8067 w = wxMax( w, lineW );
8068 h += lineH;
8069 }
8070
8071 *width = w;
8072 *height = h;
8073}
8074
f6bcfd97
BP
8075//
8076// ------ Batch processing.
8077//
8078void wxGrid::EndBatch()
8079{
8080 if ( m_batchCount > 0 )
8081 {
8082 m_batchCount--;
8083 if ( !m_batchCount )
8084 {
8085 CalcDimensions();
8086 m_rowLabelWin->Refresh();
8087 m_colLabelWin->Refresh();
8088 m_cornerLabelWin->Refresh();
8089 m_gridWin->Refresh();
8090 }
8091 }
8092}
f85afd4e 8093
d8232393
MB
8094// Use this, rather than wxWindow::Refresh(), to force an immediate
8095// repainting of the grid. Has no effect if you are already inside a
8096// BeginBatch / EndBatch block.
8097//
8098void wxGrid::ForceRefresh()
8099{
8100 BeginBatch();
8101 EndBatch();
8102}
8103
bf646121
VZ
8104bool wxGrid::Enable(bool enable)
8105{
8106 if ( !wxScrolledWindow::Enable(enable) )
8107 return false;
8108
8109 // redraw in the new state
8110 m_gridWin->Refresh();
8111
8112 return true;
8113}
d8232393 8114
f85afd4e 8115//
2d66e025 8116// ------ Edit control functions
f85afd4e
MB
8117//
8118
2d66e025 8119void wxGrid::EnableEditing( bool edit )
f85afd4e 8120{
2d66e025
MB
8121 // TODO: improve this ?
8122 //
8123 if ( edit != m_editable )
f85afd4e 8124 {
4db6714b
KH
8125 if (!edit)
8126 EnableCellEditControl(edit);
2d66e025 8127 m_editable = edit;
f85afd4e 8128 }
f85afd4e
MB
8129}
8130
2d66e025 8131void wxGrid::EnableCellEditControl( bool enable )
f85afd4e 8132{
2c9a89e0
RD
8133 if (! m_editable)
8134 return;
8135
2c9a89e0
RD
8136 if ( enable != m_cellEditCtrlEnabled )
8137 {
dcfe4c3d 8138 if ( enable )
f85afd4e 8139 {
97a9929e
VZ
8140 if (SendEvent( wxEVT_GRID_EDITOR_SHOWN) <0)
8141 return;
fe7b9ed6 8142
97a9929e 8143 // this should be checked by the caller!
a9339fe2 8144 wxASSERT_MSG( CanEnableCellControl(), _T("can't enable editing for this cell!") );
b54ba671
VZ
8145
8146 // do it before ShowCellEditControl()
dcfe4c3d 8147 m_cellEditCtrlEnabled = enable;
b54ba671 8148
2d66e025 8149 ShowCellEditControl();
2d66e025
MB
8150 }
8151 else
8152 {
97a9929e 8153 //FIXME:add veto support
a9339fe2 8154 SendEvent( wxEVT_GRID_EDITOR_HIDDEN );
fe7b9ed6 8155
97a9929e 8156 HideCellEditControl();
2d66e025 8157 SaveEditControlValue();
b54ba671
VZ
8158
8159 // do it after HideCellEditControl()
dcfe4c3d 8160 m_cellEditCtrlEnabled = enable;
f85afd4e 8161 }
f85afd4e 8162 }
f85afd4e 8163}
f85afd4e 8164
b54ba671 8165bool wxGrid::IsCurrentCellReadOnly() const
283b7808 8166{
b54ba671
VZ
8167 // const_cast
8168 wxGridCellAttr* attr = ((wxGrid *)this)->GetCellAttr(m_currentCellCoords);
8169 bool readonly = attr->IsReadOnly();
8170 attr->DecRef();
283b7808 8171
b54ba671
VZ
8172 return readonly;
8173}
283b7808 8174
b54ba671
VZ
8175bool wxGrid::CanEnableCellControl() const
8176{
20c84410
SN
8177 return m_editable && (m_currentCellCoords != wxGridNoCellCoords) &&
8178 !IsCurrentCellReadOnly();
b54ba671
VZ
8179}
8180
8181bool wxGrid::IsCellEditControlEnabled() const
8182{
8183 // the cell edit control might be disable for all cells or just for the
8184 // current one if it's read only
ca65c044 8185 return m_cellEditCtrlEnabled ? !IsCurrentCellReadOnly() : false;
283b7808
VZ
8186}
8187
f6bcfd97
BP
8188bool wxGrid::IsCellEditControlShown() const
8189{
ca65c044 8190 bool isShown = false;
f6bcfd97
BP
8191
8192 if ( m_cellEditCtrlEnabled )
8193 {
8194 int row = m_currentCellCoords.GetRow();
8195 int col = m_currentCellCoords.GetCol();
8196 wxGridCellAttr* attr = GetCellAttr(row, col);
8197 wxGridCellEditor* editor = attr->GetEditor((wxGrid*) this, row, col);
8198 attr->DecRef();
8199
8200 if ( editor )
8201 {
8202 if ( editor->IsCreated() )
8203 {
8204 isShown = editor->GetControl()->IsShown();
8205 }
8206
8207 editor->DecRef();
8208 }
8209 }
8210
8211 return isShown;
8212}
8213
2d66e025 8214void wxGrid::ShowCellEditControl()
f85afd4e 8215{
2d66e025 8216 if ( IsCellEditControlEnabled() )
f85afd4e 8217 {
7db713ae 8218 if ( !IsVisible( m_currentCellCoords, false ) )
2d66e025 8219 {
ca65c044 8220 m_cellEditCtrlEnabled = false;
2d66e025
MB
8221 return;
8222 }
8223 else
8224 {
2c9a89e0
RD
8225 wxRect rect = CellToRect( m_currentCellCoords );
8226 int row = m_currentCellCoords.GetRow();
8227 int col = m_currentCellCoords.GetCol();
2d66e025 8228
27f35b66
SN
8229 // if this is part of a multicell, find owner (topleft)
8230 int cell_rows, cell_cols;
8231 GetCellSize( row, col, &cell_rows, &cell_cols );
8232 if ( cell_rows <= 0 || cell_cols <= 0 )
8233 {
8234 row += cell_rows;
8235 col += cell_cols;
8236 m_currentCellCoords.SetRow( row );
8237 m_currentCellCoords.SetCol( col );
8238 }
8239
99306db2
VZ
8240 // erase the highlight and the cell contents because the editor
8241 // might not cover the entire cell
8242 wxClientDC dc( m_gridWin );
8243 PrepareDC( dc );
d4175745 8244 dc.SetBrush(wxBrush(GetCellAttr(row, col)->GetBackgroundColour(), wxSOLID));
99306db2
VZ
8245 dc.SetPen(*wxTRANSPARENT_PEN);
8246 dc.DrawRectangle(rect);
d2fdd8d2 8247
6f706ee0
VZ
8248 // convert to scrolled coords
8249 CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
8250
8251 int nXMove = 0;
8252 if (rect.x < 0)
8253 nXMove = rect.x;
8254
99306db2 8255 // cell is shifted by one pixel
68c5a31c 8256 // However, don't allow x or y to become negative
70e8d961 8257 // since the SetSize() method interprets that as
68c5a31c
SN
8258 // "don't change."
8259 if (rect.x > 0)
8260 rect.x--;
8261 if (rect.y > 0)
8262 rect.y--;
f0102d2a 8263
508011ce 8264 wxGridCellAttr* attr = GetCellAttr(row, col);
28a77bc4 8265 wxGridCellEditor* editor = attr->GetEditor(this, row, col);
3da93aae
VZ
8266 if ( !editor->IsCreated() )
8267 {
ca65c044 8268 editor->Create(m_gridWin, wxID_ANY,
2c9a89e0 8269 new wxGridCellEditorEvtHandler(this, editor));
bf7945ce
RD
8270
8271 wxGridEditorCreatedEvent evt(GetId(),
8272 wxEVT_GRID_EDITOR_CREATED,
8273 this,
8274 row,
8275 col,
8276 editor->GetControl());
8277 GetEventHandler()->ProcessEvent(evt);
2d66e025
MB
8278 }
8279
27f35b66 8280 // resize editor to overflow into righthand cells if allowed
39b80349 8281 int maxWidth = rect.width;
27f35b66
SN
8282 wxString value = GetCellValue(row, col);
8283 if ( (value != wxEmptyString) && (attr->GetOverflow()) )
8284 {
39b80349 8285 int y;
2f024384
DS
8286 GetTextExtent(value, &maxWidth, &y, NULL, NULL, &attr->GetFont());
8287 if (maxWidth < rect.width)
8288 maxWidth = rect.width;
39b80349 8289 }
4db6714b 8290
39b80349 8291 int client_right = m_gridWin->GetClientSize().GetWidth();
2f024384 8292 if (rect.x + maxWidth > client_right)
2b5f62a0 8293 maxWidth = client_right - rect.x;
39b80349 8294
2b5f62a0
VZ
8295 if ((maxWidth > rect.width) && (col < m_numCols) && m_table)
8296 {
39b80349 8297 GetCellSize( row, col, &cell_rows, &cell_cols );
2b5f62a0 8298 // may have changed earlier
2f024384 8299 for (int i = col + cell_cols; i < m_numCols; i++)
2b5f62a0 8300 {
39b80349
SN
8301 int c_rows, c_cols;
8302 GetCellSize( row, i, &c_rows, &c_cols );
2f024384 8303
2b5f62a0 8304 // looks weird going over a multicell
bee19958 8305 if (m_table->IsEmptyCell( row, i ) &&
2b5f62a0 8306 (rect.width < maxWidth) && (c_rows == 1))
2f024384 8307 {
bee19958 8308 rect.width += GetColWidth( i );
2f024384 8309 }
2b5f62a0
VZ
8310 else
8311 break;
8312 }
4db6714b 8313
39b80349 8314 if (rect.GetRight() > client_right)
bee19958 8315 rect.SetRight( client_right - 1 );
27f35b66 8316 }
73145b0e 8317
bee19958 8318 editor->SetCellAttr( attr );
2c9a89e0 8319 editor->SetSize( rect );
e1a66d9a
RD
8320 if (nXMove != 0)
8321 editor->GetControl()->Move(
8322 editor->GetControl()->GetPosition().x + nXMove,
8323 editor->GetControl()->GetPosition().y );
ca65c044 8324 editor->Show( true, attr );
04418332
VZ
8325
8326 // recalc dimensions in case we need to
8327 // expand the scrolled window to account for editor
73145b0e 8328 CalcDimensions();
99306db2 8329
3da93aae 8330 editor->BeginEdit(row, col, this);
1bd71df9 8331 editor->SetCellAttr(NULL);
0b190b0f
VZ
8332
8333 editor->DecRef();
2c9a89e0 8334 attr->DecRef();
2b5f62a0 8335 }
f85afd4e
MB
8336 }
8337}
8338
2d66e025 8339void wxGrid::HideCellEditControl()
f85afd4e 8340{
2d66e025 8341 if ( IsCellEditControlEnabled() )
f85afd4e 8342 {
2c9a89e0
RD
8343 int row = m_currentCellCoords.GetRow();
8344 int col = m_currentCellCoords.GetCol();
8345
bee19958 8346 wxGridCellAttr *attr = GetCellAttr(row, col);
0b190b0f 8347 wxGridCellEditor *editor = attr->GetEditor(this, row, col);
ca65c044 8348 editor->Show( false );
0b190b0f 8349 editor->DecRef();
2c9a89e0 8350 attr->DecRef();
2fb3e528 8351
48962b9f 8352 m_gridWin->SetFocus();
2fb3e528 8353
27f35b66
SN
8354 // refresh whole row to the right
8355 wxRect rect( CellToRect(row, col) );
8356 CalcScrolledPosition(rect.x, rect.y, &rect.x, &rect.y );
8357 rect.width = m_gridWin->GetClientSize().GetWidth() - rect.x;
2f024384 8358
7d75e6c6
RD
8359#ifdef __WXMAC__
8360 // ensure that the pixels under the focus ring get refreshed as well
4db6714b 8361 rect.Inflate(10, 10);
7d75e6c6 8362#endif
2f024384 8363
ca65c044 8364 m_gridWin->Refresh( false, &rect );
f85afd4e 8365 }
2d66e025 8366}
8f177c8e 8367
2d66e025
MB
8368void wxGrid::SaveEditControlValue()
8369{
3da93aae
VZ
8370 if ( IsCellEditControlEnabled() )
8371 {
2c9a89e0
RD
8372 int row = m_currentCellCoords.GetRow();
8373 int col = m_currentCellCoords.GetCol();
8f177c8e 8374
4db6714b 8375 wxString oldval = GetCellValue(row, col);
fe7b9ed6 8376
3da93aae 8377 wxGridCellAttr* attr = GetCellAttr(row, col);
28a77bc4 8378 wxGridCellEditor* editor = attr->GetEditor(this, row, col);
3324d5f5 8379 bool changed = editor->EndEdit(row, col, this);
2d66e025 8380
0b190b0f 8381 editor->DecRef();
2c9a89e0 8382 attr->DecRef();
2d66e025 8383
3da93aae
VZ
8384 if (changed)
8385 {
fe7b9ed6 8386 if ( SendEvent( wxEVT_GRID_CELL_CHANGE,
2d66e025 8387 m_currentCellCoords.GetRow(),
4db6714b
KH
8388 m_currentCellCoords.GetCol() ) < 0 )
8389 {
97a9929e 8390 // Event has been vetoed, set the data back.
4db6714b 8391 SetCellValue(row, col, oldval);
97a9929e 8392 }
2d66e025 8393 }
f85afd4e
MB
8394 }
8395}
8396
2d66e025 8397//
60ff3b99
VZ
8398// ------ Grid location functions
8399// Note that all of these functions work with the logical coordinates of
2d66e025
MB
8400// grid cells and labels so you will need to convert from device
8401// coordinates for mouse events etc.
8402//
8403
8404void wxGrid::XYToCell( int x, int y, wxGridCellCoords& coords )
f85afd4e 8405{
58dd5b3b
MB
8406 int row = YToRow(y);
8407 int col = XToCol(x);
8408
a9339fe2 8409 if ( row == -1 || col == -1 )
58dd5b3b
MB
8410 {
8411 coords = wxGridNoCellCoords;
8412 }
8413 else
8414 {
8415 coords.Set( row, col );
8416 }
2d66e025 8417}
f85afd4e 8418
b1da8107
SN
8419// Internal Helper function for computing row or column from some
8420// (unscrolled) coordinate value, using either
70e8d961 8421// m_defaultRowHeight/m_defaultColWidth or binary search on array
b1da8107
SN
8422// of m_rowBottoms/m_ColRights to speed up the search!
8423
8424static int CoordToRowOrCol(int coord, int defaultDist, int minDist,
64e15340 8425 const wxArrayInt& BorderArray, int nMax,
a967f048 8426 bool clipToMinMax)
2d66e025 8427{
a967f048
RG
8428 if (coord < 0)
8429 return clipToMinMax && (nMax > 0) ? 0 : -1;
8430
b1da8107
SN
8431 if (!defaultDist)
8432 defaultDist = 1;
a967f048 8433
1af546bf
VZ
8434 size_t i_max = coord / defaultDist,
8435 i_min = 0;
d57ad377 8436
b1da8107
SN
8437 if (BorderArray.IsEmpty())
8438 {
4db6714b 8439 if ((int) i_max < nMax)
64e15340 8440 return i_max;
a967f048 8441 return clipToMinMax ? nMax - 1 : -1;
b1da8107 8442 }
8f177c8e 8443
b1da8107 8444 if ( i_max >= BorderArray.GetCount())
2f024384 8445 {
b1da8107 8446 i_max = BorderArray.GetCount() - 1;
2f024384 8447 }
70e8d961 8448 else
f85afd4e 8449 {
b1da8107
SN
8450 if ( coord >= BorderArray[i_max])
8451 {
8452 i_min = i_max;
20c84410
SN
8453 if (minDist)
8454 i_max = coord / minDist;
8455 else
8456 i_max = BorderArray.GetCount() - 1;
b1da8107 8457 }
4db6714b 8458
b1da8107
SN
8459 if ( i_max >= BorderArray.GetCount())
8460 i_max = BorderArray.GetCount() - 1;
f85afd4e 8461 }
4db6714b 8462
33188aa4 8463 if ( coord >= BorderArray[i_max])
a967f048 8464 return clipToMinMax ? (int)i_max : -1;
68c5a31c
SN
8465 if ( coord < BorderArray[0] )
8466 return 0;
2d66e025 8467
68c5a31c 8468 while ( i_max - i_min > 0 )
b1da8107
SN
8469 {
8470 wxCHECK_MSG(BorderArray[i_min] <= coord && coord < BorderArray[i_max],
33188aa4 8471 0, _T("wxGrid: internal error in CoordToRowOrCol"));
b1da8107 8472 if (coord >= BorderArray[ i_max - 1])
b1da8107 8473 return i_max;
b1da8107
SN
8474 else
8475 i_max--;
8476 int median = i_min + (i_max - i_min + 1) / 2;
8477 if (coord < BorderArray[median])
8478 i_max = median;
8479 else
8480 i_min = median;
8481 }
4db6714b 8482
b1da8107 8483 return i_max;
f85afd4e
MB
8484}
8485
b1da8107 8486int wxGrid::YToRow( int y )
f85afd4e 8487{
b1da8107 8488 return CoordToRowOrCol(y, m_defaultRowHeight,
ca65c044 8489 m_minAcceptableRowHeight, m_rowBottoms, m_numRows, false);
b1da8107 8490}
8f177c8e 8491
d4175745 8492int wxGrid::XToCol( int x, bool clipToMinMax )
b1da8107 8493{
d4175745
VZ
8494 if (x < 0)
8495 return clipToMinMax && (m_numCols > 0) ? GetColAt( 0 ) : -1;
8496
8497 if (!m_defaultColWidth)
8498 m_defaultColWidth = 1;
8499
8500 int maxPos = x / m_defaultColWidth;
8501 int minPos = 0;
8502
8503 if (m_colRights.IsEmpty())
8504 {
8505 if(maxPos < m_numCols)
8506 return GetColAt( maxPos );
8507 return clipToMinMax ? GetColAt( m_numCols - 1 ) : -1;
8508 }
8509
8510 if ( maxPos >= m_numCols)
8511 maxPos = m_numCols - 1;
8512 else
8513 {
8514 if ( x >= m_colRights[GetColAt( maxPos )])
8515 {
8516 minPos = maxPos;
8517 if (m_minAcceptableColWidth)
8518 maxPos = x / m_minAcceptableColWidth;
8519 else
8520 maxPos = m_numCols - 1;
8521 }
8522 if ( maxPos >= m_numCols)
8523 maxPos = m_numCols - 1;
8524 }
8525
8526 //X is beyond the last column
8527 if ( x >= m_colRights[GetColAt( maxPos )])
8528 return clipToMinMax ? GetColAt( maxPos ) : -1;
8529
8530 //X is before the first column
8531 if ( x < m_colRights[GetColAt( 0 )] )
8532 return GetColAt( 0 );
8533
8534 //Perform a binary search
8535 while ( maxPos - minPos > 0 )
8536 {
8537 wxCHECK_MSG(m_colRights[GetColAt( minPos )] <= x && x < m_colRights[GetColAt( maxPos )],
8538 0, _T("wxGrid: internal error in XToCol"));
8539
8540 if (x >= m_colRights[GetColAt( maxPos - 1 )])
8541 return GetColAt( maxPos );
8542 else
8543 maxPos--;
8544 int median = minPos + (maxPos - minPos + 1) / 2;
8545 if (x < m_colRights[GetColAt( median )])
8546 maxPos = median;
8547 else
8548 minPos = median;
8549 }
8550 return GetColAt( maxPos );
2d66e025 8551}
8f177c8e 8552
be2e4015
SN
8553// return the row number that that the y coord is near
8554// the edge of, or -1 if not near an edge.
8555// coords can only possibly be near an edge if
8556// (a) the row/column is large enough to still allow for an "inner" area
8557// that is _not_ nead the edge (i.e., if the height/width is smaller
8558// than WXGRID_LABEL_EDGE_ZONE, coords are _never_ considered to be
8559// near the edge).
8560// and
8561// (b) resizing rows/columns (the thing for which edge detection is
8562// relevant at all) is enabled.
2d66e025
MB
8563//
8564int wxGrid::YToEdgeOfRow( int y )
8565{
33188aa4
SN
8566 int i;
8567 i = internalYToRow(y);
8568
be2e4015 8569 if ( GetRowHeight(i) > WXGRID_LABEL_EDGE_ZONE && CanDragRowSize() )
2d66e025 8570 {
33188aa4
SN
8571 // We know that we are in row i, test whether we are
8572 // close enough to lower or upper border, respectively.
8573 if ( abs(GetRowBottom(i) - y) < WXGRID_LABEL_EDGE_ZONE )
8574 return i;
4db6714b 8575 else if ( i > 0 && y - GetRowTop(i) < WXGRID_LABEL_EDGE_ZONE )
33188aa4 8576 return i - 1;
f85afd4e 8577 }
2d66e025
MB
8578
8579 return -1;
8580}
8581
2d66e025
MB
8582// return the col number that that the x coord is near the edge of, or
8583// -1 if not near an edge
be2e4015 8584// See comment at YToEdgeOfRow for conditions on edge detection.
2d66e025
MB
8585//
8586int wxGrid::XToEdgeOfCol( int x )
8587{
33188aa4
SN
8588 int i;
8589 i = internalXToCol(x);
8590
be2e4015 8591 if ( GetColWidth(i) > WXGRID_LABEL_EDGE_ZONE && CanDragColSize() )
f85afd4e 8592 {
a9339fe2 8593 // We know that we are in column i; test whether we are
33188aa4
SN
8594 // close enough to right or left border, respectively.
8595 if ( abs(GetColRight(i) - x) < WXGRID_LABEL_EDGE_ZONE )
8596 return i;
4db6714b 8597 else if ( i > 0 && x - GetColLeft(i) < WXGRID_LABEL_EDGE_ZONE )
33188aa4 8598 return i - 1;
f85afd4e 8599 }
2d66e025
MB
8600
8601 return -1;
f85afd4e
MB
8602}
8603
2d66e025 8604wxRect wxGrid::CellToRect( int row, int col )
f85afd4e 8605{
2d66e025 8606 wxRect rect( -1, -1, -1, -1 );
f85afd4e 8607
2f024384
DS
8608 if ( row >= 0 && row < m_numRows &&
8609 col >= 0 && col < m_numCols )
f85afd4e 8610 {
27f35b66
SN
8611 int i, cell_rows, cell_cols;
8612 rect.width = rect.height = 0;
8613 GetCellSize( row, col, &cell_rows, &cell_cols );
8614 // if negative then find multicell owner
2f024384
DS
8615 if (cell_rows < 0)
8616 row += cell_rows;
8617 if (cell_cols < 0)
8618 col += cell_cols;
27f35b66
SN
8619 GetCellSize( row, col, &cell_rows, &cell_cols );
8620
7c1cb261
VZ
8621 rect.x = GetColLeft(col);
8622 rect.y = GetRowTop(row);
2f024384
DS
8623 for (i=col; i < col + cell_cols; i++)
8624 rect.width += GetColWidth(i);
8625 for (i=row; i < row + cell_rows; i++)
27f35b66 8626 rect.height += GetRowHeight(i);
f85afd4e
MB
8627 }
8628
f6bcfd97 8629 // if grid lines are enabled, then the area of the cell is a bit smaller
4db6714b
KH
8630 if (m_gridLinesEnabled)
8631 {
f6bcfd97
BP
8632 rect.width -= 1;
8633 rect.height -= 1;
8634 }
4db6714b 8635
2d66e025
MB
8636 return rect;
8637}
8638
2d66e025
MB
8639bool wxGrid::IsVisible( int row, int col, bool wholeCellVisible )
8640{
8641 // get the cell rectangle in logical coords
8642 //
8643 wxRect r( CellToRect( row, col ) );
60ff3b99 8644
2d66e025
MB
8645 // convert to device coords
8646 //
8647 int left, top, right, bottom;
8648 CalcScrolledPosition( r.GetLeft(), r.GetTop(), &left, &top );
8649 CalcScrolledPosition( r.GetRight(), r.GetBottom(), &right, &bottom );
60ff3b99 8650
2d66e025 8651 // check against the client area of the grid window
2d66e025
MB
8652 int cw, ch;
8653 m_gridWin->GetClientSize( &cw, &ch );
60ff3b99 8654
2d66e025 8655 if ( wholeCellVisible )
f85afd4e 8656 {
2d66e025 8657 // is the cell wholly visible ?
2f024384
DS
8658 return ( left >= 0 && right <= cw &&
8659 top >= 0 && bottom <= ch );
2d66e025
MB
8660 }
8661 else
8662 {
8663 // is the cell partly visible ?
8664 //
2f024384
DS
8665 return ( ((left >= 0 && left < cw) || (right > 0 && right <= cw)) &&
8666 ((top >= 0 && top < ch) || (bottom > 0 && bottom <= ch)) );
2d66e025
MB
8667 }
8668}
8669
2d66e025
MB
8670// make the specified cell location visible by doing a minimal amount
8671// of scrolling
8672//
8673void wxGrid::MakeCellVisible( int row, int col )
8674{
8675 int i;
60ff3b99 8676 int xpos = -1, ypos = -1;
2d66e025 8677
2f024384
DS
8678 if ( row >= 0 && row < m_numRows &&
8679 col >= 0 && col < m_numCols )
2d66e025
MB
8680 {
8681 // get the cell rectangle in logical coords
2d66e025 8682 wxRect r( CellToRect( row, col ) );
60ff3b99 8683
2d66e025 8684 // convert to device coords
2d66e025
MB
8685 int left, top, right, bottom;
8686 CalcScrolledPosition( r.GetLeft(), r.GetTop(), &left, &top );
8687 CalcScrolledPosition( r.GetRight(), r.GetBottom(), &right, &bottom );
60ff3b99 8688
2d66e025
MB
8689 int cw, ch;
8690 m_gridWin->GetClientSize( &cw, &ch );
60ff3b99 8691
2d66e025 8692 if ( top < 0 )
3f296516 8693 {
2d66e025 8694 ypos = r.GetTop();
3f296516 8695 }
2d66e025
MB
8696 else if ( bottom > ch )
8697 {
8698 int h = r.GetHeight();
8699 ypos = r.GetTop();
56b6cf26 8700 for ( i = row - 1; i >= 0; i-- )
2d66e025 8701 {
7c1cb261
VZ
8702 int rowHeight = GetRowHeight(i);
8703 if ( h + rowHeight > ch )
8704 break;
2d66e025 8705
7c1cb261
VZ
8706 h += rowHeight;
8707 ypos -= rowHeight;
2d66e025 8708 }
f0102d2a
VZ
8709
8710 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
56b6cf26
DS
8711 // have rounding errors (this is important, because if we do,
8712 // we might not scroll at all and some cells won't be redrawn)
275c4ae4 8713 //
56b6cf26
DS
8714 // Sometimes GRID_SCROLL_LINE / 2 is not enough,
8715 // so just add a full scroll unit...
675a9c0d 8716 ypos += m_scrollLineY;
2d66e025
MB
8717 }
8718
7db713ae 8719 // special handling for wide cells - show always left part of the cell!
faa94f3e 8720 // Otherwise, e.g. when stepping from row to row, it would jump between
7db713ae
JS
8721 // left and right part of the cell on every step!
8722// if ( left < 0 )
ccdee36f 8723 if ( left < 0 || (right - left) >= cw )
2d66e025
MB
8724 {
8725 xpos = r.GetLeft();
8726 }
8727 else if ( right > cw )
8728 {
73145b0e
JS
8729 // position the view so that the cell is on the right
8730 int x0, y0;
8731 CalcUnscrolledPosition(0, 0, &x0, &y0);
8732 xpos = x0 + (right - cw);
f0102d2a
VZ
8733
8734 // see comment for ypos above
675a9c0d 8735 xpos += m_scrollLineX;
2d66e025
MB
8736 }
8737
ccdee36f 8738 if ( xpos != -1 || ypos != -1 )
2d66e025 8739 {
97a9929e 8740 if ( xpos != -1 )
675a9c0d 8741 xpos /= m_scrollLineX;
97a9929e 8742 if ( ypos != -1 )
675a9c0d 8743 ypos /= m_scrollLineY;
2d66e025
MB
8744 Scroll( xpos, ypos );
8745 AdjustScrollbars();
8746 }
8747 }
8748}
8749
2d66e025
MB
8750//
8751// ------ Grid cursor movement functions
8752//
8753
5c8fc7c1 8754bool wxGrid::MoveCursorUp( bool expandSelection )
2d66e025 8755{
2f024384 8756 if ( m_currentCellCoords != wxGridNoCellCoords &&
f6bcfd97 8757 m_currentCellCoords.GetRow() >= 0 )
2d66e025 8758 {
bee19958 8759 if ( expandSelection )
d95b0c2b
SN
8760 {
8761 if ( m_selectingKeyboard == wxGridNoCellCoords )
8762 m_selectingKeyboard = m_currentCellCoords;
f6bcfd97
BP
8763 if ( m_selectingKeyboard.GetRow() > 0 )
8764 {
8765 m_selectingKeyboard.SetRow( m_selectingKeyboard.GetRow() - 1 );
8766 MakeCellVisible( m_selectingKeyboard.GetRow(),
8767 m_selectingKeyboard.GetCol() );
c9097836 8768 HighlightBlock( m_currentCellCoords, m_selectingKeyboard );
f6bcfd97 8769 }
d95b0c2b 8770 }
f6bcfd97 8771 else if ( m_currentCellCoords.GetRow() > 0 )
aa5e1f75 8772 {
962a48f6
DS
8773 int row = m_currentCellCoords.GetRow() - 1;
8774 int col = m_currentCellCoords.GetCol();
aa5e1f75 8775 ClearSelection();
962a48f6
DS
8776 MakeCellVisible( row, col );
8777 SetCurrentCell( row, col );
aa5e1f75 8778 }
f6bcfd97 8779 else
ca65c044 8780 return false;
4db6714b 8781
ca65c044 8782 return true;
f85afd4e 8783 }
2d66e025 8784
ca65c044 8785 return false;
2d66e025
MB
8786}
8787
5c8fc7c1 8788bool wxGrid::MoveCursorDown( bool expandSelection )
2d66e025 8789{
2f024384 8790 if ( m_currentCellCoords != wxGridNoCellCoords &&
f6bcfd97 8791 m_currentCellCoords.GetRow() < m_numRows )
f85afd4e 8792 {
5c8fc7c1 8793 if ( expandSelection )
d95b0c2b
SN
8794 {
8795 if ( m_selectingKeyboard == wxGridNoCellCoords )
8796 m_selectingKeyboard = m_currentCellCoords;
ccdee36f 8797 if ( m_selectingKeyboard.GetRow() < m_numRows - 1 )
f6bcfd97
BP
8798 {
8799 m_selectingKeyboard.SetRow( m_selectingKeyboard.GetRow() + 1 );
8800 MakeCellVisible( m_selectingKeyboard.GetRow(),
8801 m_selectingKeyboard.GetCol() );
c9097836 8802 HighlightBlock( m_currentCellCoords, m_selectingKeyboard );
f6bcfd97 8803 }
d95b0c2b 8804 }
f6bcfd97 8805 else if ( m_currentCellCoords.GetRow() < m_numRows - 1 )
aa5e1f75 8806 {
962a48f6
DS
8807 int row = m_currentCellCoords.GetRow() + 1;
8808 int col = m_currentCellCoords.GetCol();
aa5e1f75 8809 ClearSelection();
962a48f6
DS
8810 MakeCellVisible( row, col );
8811 SetCurrentCell( row, col );
aa5e1f75 8812 }
f6bcfd97 8813 else
ca65c044 8814 return false;
4db6714b 8815
ca65c044 8816 return true;
f85afd4e 8817 }
2d66e025 8818
ca65c044 8819 return false;
f85afd4e
MB
8820}
8821
5c8fc7c1 8822bool wxGrid::MoveCursorLeft( bool expandSelection )
f85afd4e 8823{
2f024384 8824 if ( m_currentCellCoords != wxGridNoCellCoords &&
f6bcfd97 8825 m_currentCellCoords.GetCol() >= 0 )
2d66e025 8826 {
5c8fc7c1 8827 if ( expandSelection )
d95b0c2b
SN
8828 {
8829 if ( m_selectingKeyboard == wxGridNoCellCoords )
8830 m_selectingKeyboard = m_currentCellCoords;
f6bcfd97
BP
8831 if ( m_selectingKeyboard.GetCol() > 0 )
8832 {
8833 m_selectingKeyboard.SetCol( m_selectingKeyboard.GetCol() - 1 );
8834 MakeCellVisible( m_selectingKeyboard.GetRow(),
8835 m_selectingKeyboard.GetCol() );
c9097836 8836 HighlightBlock( m_currentCellCoords, m_selectingKeyboard );
f6bcfd97 8837 }
d95b0c2b 8838 }
d4175745 8839 else if ( GetColPos( m_currentCellCoords.GetCol() ) > 0 )
aa5e1f75 8840 {
962a48f6 8841 int row = m_currentCellCoords.GetRow();
d4175745 8842 int col = GetColAt( GetColPos( m_currentCellCoords.GetCol() ) - 1 );
aa5e1f75 8843 ClearSelection();
d4175745 8844
962a48f6
DS
8845 MakeCellVisible( row, col );
8846 SetCurrentCell( row, col );
aa5e1f75 8847 }
f6bcfd97 8848 else
ca65c044 8849 return false;
4db6714b 8850
ca65c044 8851 return true;
2d66e025
MB
8852 }
8853
ca65c044 8854 return false;
2d66e025
MB
8855}
8856
5c8fc7c1 8857bool wxGrid::MoveCursorRight( bool expandSelection )
2d66e025 8858{
2f024384 8859 if ( m_currentCellCoords != wxGridNoCellCoords &&
f6bcfd97 8860 m_currentCellCoords.GetCol() < m_numCols )
f85afd4e 8861 {
5c8fc7c1 8862 if ( expandSelection )
d95b0c2b
SN
8863 {
8864 if ( m_selectingKeyboard == wxGridNoCellCoords )
8865 m_selectingKeyboard = m_currentCellCoords;
f6bcfd97
BP
8866 if ( m_selectingKeyboard.GetCol() < m_numCols - 1 )
8867 {
8868 m_selectingKeyboard.SetCol( m_selectingKeyboard.GetCol() + 1 );
8869 MakeCellVisible( m_selectingKeyboard.GetRow(),
8870 m_selectingKeyboard.GetCol() );
c9097836 8871 HighlightBlock( m_currentCellCoords, m_selectingKeyboard );
f6bcfd97 8872 }
d95b0c2b 8873 }
d4175745 8874 else if ( GetColPos( m_currentCellCoords.GetCol() ) < m_numCols - 1 )
aa5e1f75 8875 {
962a48f6 8876 int row = m_currentCellCoords.GetRow();
d4175745 8877 int col = GetColAt( GetColPos( m_currentCellCoords.GetCol() ) + 1 );
aa5e1f75 8878 ClearSelection();
d4175745 8879
962a48f6
DS
8880 MakeCellVisible( row, col );
8881 SetCurrentCell( row, col );
aa5e1f75 8882 }
f6bcfd97 8883 else
ca65c044 8884 return false;
4db6714b 8885
ca65c044 8886 return true;
f85afd4e 8887 }
8f177c8e 8888
ca65c044 8889 return false;
2d66e025
MB
8890}
8891
2d66e025
MB
8892bool wxGrid::MovePageUp()
8893{
4db6714b
KH
8894 if ( m_currentCellCoords == wxGridNoCellCoords )
8895 return false;
60ff3b99 8896
2d66e025
MB
8897 int row = m_currentCellCoords.GetRow();
8898 if ( row > 0 )
f85afd4e 8899 {
2d66e025
MB
8900 int cw, ch;
8901 m_gridWin->GetClientSize( &cw, &ch );
60ff3b99 8902
7c1cb261 8903 int y = GetRowTop(row);
a967f048 8904 int newRow = internalYToRow( y - ch + 1 );
ef5df12b 8905
a967f048 8906 if ( newRow == row )
2d66e025 8907 {
c2f5b920 8908 // row > 0, so newRow can never be less than 0 here.
2d66e025
MB
8909 newRow = row - 1;
8910 }
8f177c8e 8911
2d66e025
MB
8912 MakeCellVisible( newRow, m_currentCellCoords.GetCol() );
8913 SetCurrentCell( newRow, m_currentCellCoords.GetCol() );
60ff3b99 8914
ca65c044 8915 return true;
f85afd4e 8916 }
2d66e025 8917
ca65c044 8918 return false;
2d66e025
MB
8919}
8920
8921bool wxGrid::MovePageDown()
8922{
4db6714b
KH
8923 if ( m_currentCellCoords == wxGridNoCellCoords )
8924 return false;
60ff3b99 8925
2d66e025 8926 int row = m_currentCellCoords.GetRow();
ccdee36f 8927 if ( (row + 1) < m_numRows )
f85afd4e 8928 {
2d66e025
MB
8929 int cw, ch;
8930 m_gridWin->GetClientSize( &cw, &ch );
60ff3b99 8931
7c1cb261 8932 int y = GetRowTop(row);
a967f048
RG
8933 int newRow = internalYToRow( y + ch );
8934 if ( newRow == row )
2d66e025 8935 {
c2f5b920 8936 // row < m_numRows, so newRow can't overflow here.
ca65c044 8937 newRow = row + 1;
2d66e025
MB
8938 }
8939
8940 MakeCellVisible( newRow, m_currentCellCoords.GetCol() );
8941 SetCurrentCell( newRow, m_currentCellCoords.GetCol() );
60ff3b99 8942
ca65c044 8943 return true;
f85afd4e 8944 }
2d66e025 8945
ca65c044 8946 return false;
f85afd4e 8947}
8f177c8e 8948
5c8fc7c1 8949bool wxGrid::MoveCursorUpBlock( bool expandSelection )
2d66e025
MB
8950{
8951 if ( m_table &&
2f024384 8952 m_currentCellCoords != wxGridNoCellCoords &&
2d66e025
MB
8953 m_currentCellCoords.GetRow() > 0 )
8954 {
8955 int row = m_currentCellCoords.GetRow();
8956 int col = m_currentCellCoords.GetCol();
8957
8958 if ( m_table->IsEmptyCell(row, col) )
8959 {
8960 // starting in an empty cell: find the next block of
8961 // non-empty cells
8962 //
8963 while ( row > 0 )
8964 {
2f024384 8965 row--;
4db6714b
KH
8966 if ( !(m_table->IsEmptyCell(row, col)) )
8967 break;
2d66e025
MB
8968 }
8969 }
2f024384 8970 else if ( m_table->IsEmptyCell(row - 1, col) )
2d66e025
MB
8971 {
8972 // starting at the top of a block: find the next block
8973 //
8974 row--;
8975 while ( row > 0 )
8976 {
2f024384 8977 row--;
4db6714b
KH
8978 if ( !(m_table->IsEmptyCell(row, col)) )
8979 break;
2d66e025
MB
8980 }
8981 }
8982 else
8983 {
8984 // starting within a block: find the top of the block
8985 //
8986 while ( row > 0 )
8987 {
2f024384 8988 row--;
2d66e025
MB
8989 if ( m_table->IsEmptyCell(row, col) )
8990 {
2f024384 8991 row++;
2d66e025
MB
8992 break;
8993 }
8994 }
8995 }
f85afd4e 8996
2d66e025 8997 MakeCellVisible( row, col );
5c8fc7c1 8998 if ( expandSelection )
d95b0c2b
SN
8999 {
9000 m_selectingKeyboard = wxGridCellCoords( row, col );
c9097836 9001 HighlightBlock( m_currentCellCoords, m_selectingKeyboard );
d95b0c2b
SN
9002 }
9003 else
aa5e1f75
SN
9004 {
9005 ClearSelection();
9006 SetCurrentCell( row, col );
9007 }
4db6714b 9008
ca65c044 9009 return true;
2d66e025 9010 }
f85afd4e 9011
ca65c044 9012 return false;
2d66e025 9013}
f85afd4e 9014
5c8fc7c1 9015bool wxGrid::MoveCursorDownBlock( bool expandSelection )
f85afd4e 9016{
2d66e025 9017 if ( m_table &&
2f024384
DS
9018 m_currentCellCoords != wxGridNoCellCoords &&
9019 m_currentCellCoords.GetRow() < m_numRows - 1 )
f85afd4e 9020 {
2d66e025
MB
9021 int row = m_currentCellCoords.GetRow();
9022 int col = m_currentCellCoords.GetCol();
9023
9024 if ( m_table->IsEmptyCell(row, col) )
9025 {
9026 // starting in an empty cell: find the next block of
9027 // non-empty cells
9028 //
2f024384 9029 while ( row < m_numRows - 1 )
2d66e025 9030 {
2f024384 9031 row++;
4db6714b
KH
9032 if ( !(m_table->IsEmptyCell(row, col)) )
9033 break;
2d66e025
MB
9034 }
9035 }
2f024384 9036 else if ( m_table->IsEmptyCell(row + 1, col) )
2d66e025
MB
9037 {
9038 // starting at the bottom of a block: find the next block
9039 //
9040 row++;
2f024384 9041 while ( row < m_numRows - 1 )
2d66e025 9042 {
2f024384 9043 row++;
4db6714b
KH
9044 if ( !(m_table->IsEmptyCell(row, col)) )
9045 break;
2d66e025
MB
9046 }
9047 }
9048 else
9049 {
9050 // starting within a block: find the bottom of the block
9051 //
2f024384 9052 while ( row < m_numRows - 1 )
2d66e025 9053 {
2f024384 9054 row++;
2d66e025
MB
9055 if ( m_table->IsEmptyCell(row, col) )
9056 {
2f024384 9057 row--;
2d66e025
MB
9058 break;
9059 }
9060 }
9061 }
9062
9063 MakeCellVisible( row, col );
5c8fc7c1 9064 if ( expandSelection )
d95b0c2b
SN
9065 {
9066 m_selectingKeyboard = wxGridCellCoords( row, col );
c9097836 9067 HighlightBlock( m_currentCellCoords, m_selectingKeyboard );
d95b0c2b
SN
9068 }
9069 else
aa5e1f75
SN
9070 {
9071 ClearSelection();
9072 SetCurrentCell( row, col );
9073 }
2d66e025 9074
ca65c044 9075 return true;
f85afd4e 9076 }
f85afd4e 9077
ca65c044 9078 return false;
2d66e025 9079}
f85afd4e 9080
5c8fc7c1 9081bool wxGrid::MoveCursorLeftBlock( bool expandSelection )
f85afd4e 9082{
2d66e025 9083 if ( m_table &&
2f024384 9084 m_currentCellCoords != wxGridNoCellCoords &&
2d66e025 9085 m_currentCellCoords.GetCol() > 0 )
f85afd4e 9086 {
2d66e025
MB
9087 int row = m_currentCellCoords.GetRow();
9088 int col = m_currentCellCoords.GetCol();
9089
9090 if ( m_table->IsEmptyCell(row, col) )
9091 {
9092 // starting in an empty cell: find the next block of
9093 // non-empty cells
9094 //
9095 while ( col > 0 )
9096 {
2f024384 9097 col--;
4db6714b
KH
9098 if ( !(m_table->IsEmptyCell(row, col)) )
9099 break;
2d66e025
MB
9100 }
9101 }
2f024384 9102 else if ( m_table->IsEmptyCell(row, col - 1) )
2d66e025
MB
9103 {
9104 // starting at the left of a block: find the next block
9105 //
9106 col--;
9107 while ( col > 0 )
9108 {
2f024384 9109 col--;
4db6714b
KH
9110 if ( !(m_table->IsEmptyCell(row, col)) )
9111 break;
2d66e025
MB
9112 }
9113 }
9114 else
9115 {
9116 // starting within a block: find the left of the block
9117 //
9118 while ( col > 0 )
9119 {
2f024384 9120 col--;
2d66e025
MB
9121 if ( m_table->IsEmptyCell(row, col) )
9122 {
2f024384 9123 col++;
2d66e025
MB
9124 break;
9125 }
9126 }
9127 }
f85afd4e 9128
2d66e025 9129 MakeCellVisible( row, col );
5c8fc7c1 9130 if ( expandSelection )
d95b0c2b
SN
9131 {
9132 m_selectingKeyboard = wxGridCellCoords( row, col );
c9097836 9133 HighlightBlock( m_currentCellCoords, m_selectingKeyboard );
d95b0c2b
SN
9134 }
9135 else
aa5e1f75
SN
9136 {
9137 ClearSelection();
9138 SetCurrentCell( row, col );
9139 }
8f177c8e 9140
ca65c044 9141 return true;
f85afd4e 9142 }
2d66e025 9143
ca65c044 9144 return false;
f85afd4e
MB
9145}
9146
5c8fc7c1 9147bool wxGrid::MoveCursorRightBlock( bool expandSelection )
f85afd4e 9148{
2d66e025 9149 if ( m_table &&
2f024384
DS
9150 m_currentCellCoords != wxGridNoCellCoords &&
9151 m_currentCellCoords.GetCol() < m_numCols - 1 )
f85afd4e 9152 {
2d66e025
MB
9153 int row = m_currentCellCoords.GetRow();
9154 int col = m_currentCellCoords.GetCol();
8f177c8e 9155
2d66e025
MB
9156 if ( m_table->IsEmptyCell(row, col) )
9157 {
9158 // starting in an empty cell: find the next block of
9159 // non-empty cells
9160 //
2f024384 9161 while ( col < m_numCols - 1 )
2d66e025 9162 {
2f024384 9163 col++;
4db6714b
KH
9164 if ( !(m_table->IsEmptyCell(row, col)) )
9165 break;
2d66e025
MB
9166 }
9167 }
2f024384 9168 else if ( m_table->IsEmptyCell(row, col + 1) )
2d66e025
MB
9169 {
9170 // starting at the right of a block: find the next block
9171 //
9172 col++;
2f024384 9173 while ( col < m_numCols - 1 )
2d66e025 9174 {
2f024384 9175 col++;
4db6714b
KH
9176 if ( !(m_table->IsEmptyCell(row, col)) )
9177 break;
2d66e025
MB
9178 }
9179 }
9180 else
9181 {
9182 // starting within a block: find the right of the block
9183 //
2f024384 9184 while ( col < m_numCols - 1 )
2d66e025 9185 {
2f024384 9186 col++;
2d66e025
MB
9187 if ( m_table->IsEmptyCell(row, col) )
9188 {
2f024384 9189 col--;
2d66e025
MB
9190 break;
9191 }
9192 }
9193 }
8f177c8e 9194
2d66e025 9195 MakeCellVisible( row, col );
5c8fc7c1 9196 if ( expandSelection )
d95b0c2b
SN
9197 {
9198 m_selectingKeyboard = wxGridCellCoords( row, col );
c9097836 9199 HighlightBlock( m_currentCellCoords, m_selectingKeyboard );
d95b0c2b
SN
9200 }
9201 else
aa5e1f75
SN
9202 {
9203 ClearSelection();
9204 SetCurrentCell( row, col );
9205 }
f85afd4e 9206
ca65c044 9207 return true;
f85afd4e 9208 }
2d66e025 9209
ca65c044 9210 return false;
f85afd4e
MB
9211}
9212
f85afd4e 9213//
2d66e025 9214// ------ Label values and formatting
f85afd4e
MB
9215//
9216
9217void wxGrid::GetRowLabelAlignment( int *horiz, int *vert )
9218{
2f024384
DS
9219 if ( horiz )
9220 *horiz = m_rowLabelHorizAlign;
9221 if ( vert )
9222 *vert = m_rowLabelVertAlign;
f85afd4e
MB
9223}
9224
9225void wxGrid::GetColLabelAlignment( int *horiz, int *vert )
9226{
2f024384
DS
9227 if ( horiz )
9228 *horiz = m_colLabelHorizAlign;
9229 if ( vert )
9230 *vert = m_colLabelVertAlign;
f85afd4e
MB
9231}
9232
d43851f7
JS
9233int wxGrid::GetColLabelTextOrientation()
9234{
9235 return m_colLabelTextOrientation;
9236}
9237
f85afd4e
MB
9238wxString wxGrid::GetRowLabelValue( int row )
9239{
9240 if ( m_table )
9241 {
9242 return m_table->GetRowLabelValue( row );
9243 }
9244 else
9245 {
9246 wxString s;
9247 s << row;
9248 return s;
9249 }
9250}
9251
9252wxString wxGrid::GetColLabelValue( int col )
9253{
9254 if ( m_table )
9255 {
9256 return m_table->GetColLabelValue( col );
9257 }
9258 else
9259 {
9260 wxString s;
9261 s << col;
9262 return s;
9263 }
9264}
9265
9266void wxGrid::SetRowLabelSize( int width )
9267{
2e8cd977
MB
9268 width = wxMax( width, 0 );
9269 if ( width != m_rowLabelWidth )
9270 {
2e8cd977
MB
9271 if ( width == 0 )
9272 {
ca65c044
WS
9273 m_rowLabelWin->Show( false );
9274 m_cornerLabelWin->Show( false );
2e8cd977 9275 }
7807d81c 9276 else if ( m_rowLabelWidth == 0 )
2e8cd977 9277 {
ca65c044 9278 m_rowLabelWin->Show( true );
2f024384
DS
9279 if ( m_colLabelHeight > 0 )
9280 m_cornerLabelWin->Show( true );
2e8cd977 9281 }
b99be8fb 9282
2e8cd977 9283 m_rowLabelWidth = width;
7807d81c 9284 CalcWindowSizes();
ca65c044 9285 wxScrolledWindow::Refresh( true );
2e8cd977 9286 }
f85afd4e
MB
9287}
9288
9289void wxGrid::SetColLabelSize( int height )
9290{
7807d81c 9291 height = wxMax( height, 0 );
2e8cd977
MB
9292 if ( height != m_colLabelHeight )
9293 {
2e8cd977
MB
9294 if ( height == 0 )
9295 {
ca65c044
WS
9296 m_colLabelWin->Show( false );
9297 m_cornerLabelWin->Show( false );
2e8cd977 9298 }
7807d81c 9299 else if ( m_colLabelHeight == 0 )
2e8cd977 9300 {
ca65c044 9301 m_colLabelWin->Show( true );
a9339fe2
DS
9302 if ( m_rowLabelWidth > 0 )
9303 m_cornerLabelWin->Show( true );
2e8cd977 9304 }
7807d81c 9305
2e8cd977 9306 m_colLabelHeight = height;
7807d81c 9307 CalcWindowSizes();
ca65c044 9308 wxScrolledWindow::Refresh( true );
2e8cd977 9309 }
f85afd4e
MB
9310}
9311
9312void wxGrid::SetLabelBackgroundColour( const wxColour& colour )
9313{
2d66e025
MB
9314 if ( m_labelBackgroundColour != colour )
9315 {
9316 m_labelBackgroundColour = colour;
9317 m_rowLabelWin->SetBackgroundColour( colour );
9318 m_colLabelWin->SetBackgroundColour( colour );
9319 m_cornerLabelWin->SetBackgroundColour( colour );
9320
9321 if ( !GetBatchCount() )
9322 {
9323 m_rowLabelWin->Refresh();
9324 m_colLabelWin->Refresh();
9325 m_cornerLabelWin->Refresh();
9326 }
9327 }
f85afd4e
MB
9328}
9329
9330void wxGrid::SetLabelTextColour( const wxColour& colour )
9331{
2d66e025
MB
9332 if ( m_labelTextColour != colour )
9333 {
9334 m_labelTextColour = colour;
9335 if ( !GetBatchCount() )
9336 {
9337 m_rowLabelWin->Refresh();
9338 m_colLabelWin->Refresh();
9339 }
9340 }
f85afd4e
MB
9341}
9342
9343void wxGrid::SetLabelFont( const wxFont& font )
9344{
9345 m_labelFont = font;
2d66e025
MB
9346 if ( !GetBatchCount() )
9347 {
9348 m_rowLabelWin->Refresh();
9349 m_colLabelWin->Refresh();
9350 }
f85afd4e
MB
9351}
9352
9353void wxGrid::SetRowLabelAlignment( int horiz, int vert )
9354{
4c7277db
MB
9355 // allow old (incorrect) defs to be used
9356 switch ( horiz )
9357 {
9358 case wxLEFT: horiz = wxALIGN_LEFT; break;
9359 case wxRIGHT: horiz = wxALIGN_RIGHT; break;
9360 case wxCENTRE: horiz = wxALIGN_CENTRE; break;
9361 }
84912ef8 9362
4c7277db
MB
9363 switch ( vert )
9364 {
9365 case wxTOP: vert = wxALIGN_TOP; break;
9366 case wxBOTTOM: vert = wxALIGN_BOTTOM; break;
9367 case wxCENTRE: vert = wxALIGN_CENTRE; break;
9368 }
84912ef8 9369
4c7277db 9370 if ( horiz == wxALIGN_LEFT || horiz == wxALIGN_CENTRE || horiz == wxALIGN_RIGHT )
f85afd4e
MB
9371 {
9372 m_rowLabelHorizAlign = horiz;
9373 }
8f177c8e 9374
4c7277db 9375 if ( vert == wxALIGN_TOP || vert == wxALIGN_CENTRE || vert == wxALIGN_BOTTOM )
f85afd4e
MB
9376 {
9377 m_rowLabelVertAlign = vert;
9378 }
9379
2d66e025
MB
9380 if ( !GetBatchCount() )
9381 {
9382 m_rowLabelWin->Refresh();
60ff3b99 9383 }
f85afd4e
MB
9384}
9385
9386void wxGrid::SetColLabelAlignment( int horiz, int vert )
9387{
4c7277db
MB
9388 // allow old (incorrect) defs to be used
9389 switch ( horiz )
9390 {
9391 case wxLEFT: horiz = wxALIGN_LEFT; break;
9392 case wxRIGHT: horiz = wxALIGN_RIGHT; break;
9393 case wxCENTRE: horiz = wxALIGN_CENTRE; break;
9394 }
84912ef8 9395
4c7277db
MB
9396 switch ( vert )
9397 {
9398 case wxTOP: vert = wxALIGN_TOP; break;
9399 case wxBOTTOM: vert = wxALIGN_BOTTOM; break;
9400 case wxCENTRE: vert = wxALIGN_CENTRE; break;
9401 }
84912ef8 9402
4c7277db 9403 if ( horiz == wxALIGN_LEFT || horiz == wxALIGN_CENTRE || horiz == wxALIGN_RIGHT )
f85afd4e
MB
9404 {
9405 m_colLabelHorizAlign = horiz;
9406 }
8f177c8e 9407
4c7277db 9408 if ( vert == wxALIGN_TOP || vert == wxALIGN_CENTRE || vert == wxALIGN_BOTTOM )
f85afd4e
MB
9409 {
9410 m_colLabelVertAlign = vert;
9411 }
9412
2d66e025
MB
9413 if ( !GetBatchCount() )
9414 {
2d66e025 9415 m_colLabelWin->Refresh();
60ff3b99 9416 }
f85afd4e
MB
9417}
9418
ca65c044
WS
9419// Note: under MSW, the default column label font must be changed because it
9420// does not support vertical printing
d43851f7
JS
9421//
9422// Example: wxFont font(9, wxSWISS, wxNORMAL, wxBOLD);
ca65c044
WS
9423// pGrid->SetLabelFont(font);
9424// pGrid->SetColLabelTextOrientation(wxVERTICAL);
d43851f7
JS
9425//
9426void wxGrid::SetColLabelTextOrientation( int textOrientation )
9427{
4db6714b 9428 if ( textOrientation == wxHORIZONTAL || textOrientation == wxVERTICAL )
d43851f7 9429 m_colLabelTextOrientation = textOrientation;
d43851f7
JS
9430
9431 if ( !GetBatchCount() )
d43851f7 9432 m_colLabelWin->Refresh();
d43851f7
JS
9433}
9434
f85afd4e
MB
9435void wxGrid::SetRowLabelValue( int row, const wxString& s )
9436{
9437 if ( m_table )
9438 {
9439 m_table->SetRowLabelValue( row, s );
2d66e025
MB
9440 if ( !GetBatchCount() )
9441 {
ccdee36f 9442 wxRect rect = CellToRect( row, 0 );
70c7a608
SN
9443 if ( rect.height > 0 )
9444 {
cb309039 9445 CalcScrolledPosition(0, rect.y, &rect.x, &rect.y);
b1944ebc 9446 rect.x = 0;
70c7a608 9447 rect.width = m_rowLabelWidth;
ca65c044 9448 m_rowLabelWin->Refresh( true, &rect );
70c7a608 9449 }
2d66e025 9450 }
f85afd4e
MB
9451 }
9452}
9453
9454void wxGrid::SetColLabelValue( int col, const wxString& s )
9455{
9456 if ( m_table )
9457 {
9458 m_table->SetColLabelValue( col, s );
2d66e025
MB
9459 if ( !GetBatchCount() )
9460 {
70c7a608
SN
9461 wxRect rect = CellToRect( 0, col );
9462 if ( rect.width > 0 )
9463 {
cb309039 9464 CalcScrolledPosition(rect.x, 0, &rect.x, &rect.y);
b1944ebc 9465 rect.y = 0;
70c7a608 9466 rect.height = m_colLabelHeight;
ca65c044 9467 m_colLabelWin->Refresh( true, &rect );
70c7a608 9468 }
2d66e025 9469 }
f85afd4e
MB
9470 }
9471}
9472
9473void wxGrid::SetGridLineColour( const wxColour& colour )
9474{
2d66e025
MB
9475 if ( m_gridLineColour != colour )
9476 {
9477 m_gridLineColour = colour;
60ff3b99 9478
2d66e025
MB
9479 wxClientDC dc( m_gridWin );
9480 PrepareDC( dc );
c6a51dcd 9481 DrawAllGridLines( dc, wxRegion() );
2d66e025 9482 }
f85afd4e
MB
9483}
9484
f6bcfd97
BP
9485void wxGrid::SetCellHighlightColour( const wxColour& colour )
9486{
9487 if ( m_cellHighlightColour != colour )
9488 {
9489 m_cellHighlightColour = colour;
9490
9491 wxClientDC dc( m_gridWin );
9492 PrepareDC( dc );
9493 wxGridCellAttr* attr = GetCellAttr(m_currentCellCoords);
9494 DrawCellHighlight(dc, attr);
9495 attr->DecRef();
9496 }
9497}
9498
d2520c85
RD
9499void wxGrid::SetCellHighlightPenWidth(int width)
9500{
4db6714b
KH
9501 if (m_cellHighlightPenWidth != width)
9502 {
d2520c85
RD
9503 m_cellHighlightPenWidth = width;
9504
9505 // Just redrawing the cell highlight is not enough since that won't
9506 // make any visible change if the the thickness is getting smaller.
9507 int row = m_currentCellCoords.GetRow();
9508 int col = m_currentCellCoords.GetCol();
9509 if ( GetColWidth(col) <= 0 || GetRowHeight(row) <= 0 )
9510 return;
2f024384 9511
d2520c85 9512 wxRect rect = CellToRect(row, col);
ca65c044 9513 m_gridWin->Refresh(true, &rect);
d2520c85
RD
9514 }
9515}
9516
9517void wxGrid::SetCellHighlightROPenWidth(int width)
9518{
4db6714b
KH
9519 if (m_cellHighlightROPenWidth != width)
9520 {
d2520c85
RD
9521 m_cellHighlightROPenWidth = width;
9522
9523 // Just redrawing the cell highlight is not enough since that won't
9524 // make any visible change if the the thickness is getting smaller.
9525 int row = m_currentCellCoords.GetRow();
9526 int col = m_currentCellCoords.GetCol();
9527 if ( GetColWidth(col) <= 0 || GetRowHeight(row) <= 0 )
9528 return;
ccdee36f 9529
d2520c85 9530 wxRect rect = CellToRect(row, col);
ca65c044 9531 m_gridWin->Refresh(true, &rect);
d2520c85
RD
9532 }
9533}
9534
f85afd4e
MB
9535void wxGrid::EnableGridLines( bool enable )
9536{
9537 if ( enable != m_gridLinesEnabled )
9538 {
9539 m_gridLinesEnabled = enable;
2d66e025
MB
9540
9541 if ( !GetBatchCount() )
9542 {
9543 if ( enable )
9544 {
9545 wxClientDC dc( m_gridWin );
9546 PrepareDC( dc );
c6a51dcd 9547 DrawAllGridLines( dc, wxRegion() );
2d66e025
MB
9548 }
9549 else
9550 {
9551 m_gridWin->Refresh();
9552 }
9553 }
f85afd4e
MB
9554 }
9555}
9556
f85afd4e
MB
9557int wxGrid::GetDefaultRowSize()
9558{
9559 return m_defaultRowHeight;
9560}
9561
9562int wxGrid::GetRowSize( int row )
9563{
b99be8fb
VZ
9564 wxCHECK_MSG( row >= 0 && row < m_numRows, 0, _T("invalid row index") );
9565
7c1cb261 9566 return GetRowHeight(row);
f85afd4e
MB
9567}
9568
9569int wxGrid::GetDefaultColSize()
9570{
9571 return m_defaultColWidth;
9572}
9573
9574int wxGrid::GetColSize( int col )
9575{
b99be8fb
VZ
9576 wxCHECK_MSG( col >= 0 && col < m_numCols, 0, _T("invalid column index") );
9577
7c1cb261 9578 return GetColWidth(col);
f85afd4e
MB
9579}
9580
2e9a6788
VZ
9581// ============================================================================
9582// access to the grid attributes: each of them has a default value in the grid
9583// itself and may be overidden on a per-cell basis
9584// ============================================================================
9585
9586// ----------------------------------------------------------------------------
9587// setting default attributes
9588// ----------------------------------------------------------------------------
9589
9590void wxGrid::SetDefaultCellBackgroundColour( const wxColour& col )
9591{
2796cce3 9592 m_defaultCellAttr->SetBackgroundColour(col);
c916e13b
RR
9593#ifdef __WXGTK__
9594 m_gridWin->SetBackgroundColour(col);
9595#endif
2e9a6788
VZ
9596}
9597
9598void wxGrid::SetDefaultCellTextColour( const wxColour& col )
9599{
2796cce3 9600 m_defaultCellAttr->SetTextColour(col);
2e9a6788
VZ
9601}
9602
9603void wxGrid::SetDefaultCellAlignment( int horiz, int vert )
9604{
2796cce3 9605 m_defaultCellAttr->SetAlignment(horiz, vert);
2e9a6788
VZ
9606}
9607
27f35b66
SN
9608void wxGrid::SetDefaultCellOverflow( bool allow )
9609{
9610 m_defaultCellAttr->SetOverflow(allow);
9611}
9612
2e9a6788
VZ
9613void wxGrid::SetDefaultCellFont( const wxFont& font )
9614{
2796cce3
RD
9615 m_defaultCellAttr->SetFont(font);
9616}
9617
ca63e8e9
RD
9618// For editors and renderers the type registry takes precedence over the
9619// default attr, so we need to register the new editor/renderer for the string
9620// data type in order to make setting a default editor/renderer appear to
9621// work correctly.
9622
0ba143c9
RD
9623void wxGrid::SetDefaultRenderer(wxGridCellRenderer *renderer)
9624{
ca63e8e9
RD
9625 RegisterDataType(wxGRID_VALUE_STRING,
9626 renderer,
9627 GetDefaultEditorForType(wxGRID_VALUE_STRING));
0ba143c9 9628}
2e9a6788 9629
0ba143c9
RD
9630void wxGrid::SetDefaultEditor(wxGridCellEditor *editor)
9631{
ca63e8e9
RD
9632 RegisterDataType(wxGRID_VALUE_STRING,
9633 GetDefaultRendererForType(wxGRID_VALUE_STRING),
42841dfc 9634 editor);
0ba143c9 9635}
9b4aede2 9636
2e9a6788
VZ
9637// ----------------------------------------------------------------------------
9638// access to the default attrbiutes
9639// ----------------------------------------------------------------------------
9640
f85afd4e
MB
9641wxColour wxGrid::GetDefaultCellBackgroundColour()
9642{
2796cce3 9643 return m_defaultCellAttr->GetBackgroundColour();
f85afd4e
MB
9644}
9645
2e9a6788
VZ
9646wxColour wxGrid::GetDefaultCellTextColour()
9647{
2796cce3 9648 return m_defaultCellAttr->GetTextColour();
2e9a6788
VZ
9649}
9650
9651wxFont wxGrid::GetDefaultCellFont()
9652{
2796cce3 9653 return m_defaultCellAttr->GetFont();
2e9a6788
VZ
9654}
9655
9656void wxGrid::GetDefaultCellAlignment( int *horiz, int *vert )
9657{
2796cce3 9658 m_defaultCellAttr->GetAlignment(horiz, vert);
2e9a6788
VZ
9659}
9660
27f35b66
SN
9661bool wxGrid::GetDefaultCellOverflow()
9662{
9663 return m_defaultCellAttr->GetOverflow();
9664}
9665
0ba143c9
RD
9666wxGridCellRenderer *wxGrid::GetDefaultRenderer() const
9667{
0b190b0f 9668 return m_defaultCellAttr->GetRenderer(NULL, 0, 0);
0ba143c9 9669}
ab79958a 9670
0ba143c9
RD
9671wxGridCellEditor *wxGrid::GetDefaultEditor() const
9672{
4db6714b 9673 return m_defaultCellAttr->GetEditor(NULL, 0, 0);
0ba143c9 9674}
9b4aede2 9675
2e9a6788
VZ
9676// ----------------------------------------------------------------------------
9677// access to cell attributes
9678// ----------------------------------------------------------------------------
9679
b99be8fb 9680wxColour wxGrid::GetCellBackgroundColour(int row, int col)
f85afd4e 9681{
0a976765 9682 wxGridCellAttr *attr = GetCellAttr(row, col);
2796cce3 9683 wxColour colour = attr->GetBackgroundColour();
39bcce60 9684 attr->DecRef();
2f024384 9685
b99be8fb 9686 return colour;
f85afd4e
MB
9687}
9688
b99be8fb 9689wxColour wxGrid::GetCellTextColour( int row, int col )
f85afd4e 9690{
0a976765 9691 wxGridCellAttr *attr = GetCellAttr(row, col);
2796cce3 9692 wxColour colour = attr->GetTextColour();
39bcce60 9693 attr->DecRef();
2f024384 9694
b99be8fb 9695 return colour;
f85afd4e
MB
9696}
9697
b99be8fb 9698wxFont wxGrid::GetCellFont( int row, int col )
f85afd4e 9699{
0a976765 9700 wxGridCellAttr *attr = GetCellAttr(row, col);
2796cce3 9701 wxFont font = attr->GetFont();
39bcce60 9702 attr->DecRef();
2f024384 9703
b99be8fb 9704 return font;
f85afd4e
MB
9705}
9706
b99be8fb 9707void wxGrid::GetCellAlignment( int row, int col, int *horiz, int *vert )
f85afd4e 9708{
0a976765 9709 wxGridCellAttr *attr = GetCellAttr(row, col);
2796cce3 9710 attr->GetAlignment(horiz, vert);
39bcce60 9711 attr->DecRef();
2e9a6788
VZ
9712}
9713
27f35b66
SN
9714bool wxGrid::GetCellOverflow( int row, int col )
9715{
9716 wxGridCellAttr *attr = GetCellAttr(row, col);
9717 bool allow = attr->GetOverflow();
9718 attr->DecRef();
4db6714b 9719
27f35b66
SN
9720 return allow;
9721}
9722
9723void wxGrid::GetCellSize( int row, int col, int *num_rows, int *num_cols )
9724{
9725 wxGridCellAttr *attr = GetCellAttr(row, col);
9726 attr->GetSize( num_rows, num_cols );
9727 attr->DecRef();
9728}
9729
2796cce3
RD
9730wxGridCellRenderer* wxGrid::GetCellRenderer(int row, int col)
9731{
9732 wxGridCellAttr* attr = GetCellAttr(row, col);
28a77bc4 9733 wxGridCellRenderer* renderer = attr->GetRenderer(this, row, col);
2796cce3 9734 attr->DecRef();
0b190b0f 9735
2796cce3
RD
9736 return renderer;
9737}
9738
9b4aede2
RD
9739wxGridCellEditor* wxGrid::GetCellEditor(int row, int col)
9740{
9741 wxGridCellAttr* attr = GetCellAttr(row, col);
28a77bc4 9742 wxGridCellEditor* editor = attr->GetEditor(this, row, col);
9b4aede2 9743 attr->DecRef();
0b190b0f 9744
9b4aede2
RD
9745 return editor;
9746}
9747
283b7808
VZ
9748bool wxGrid::IsReadOnly(int row, int col) const
9749{
9750 wxGridCellAttr* attr = GetCellAttr(row, col);
9751 bool isReadOnly = attr->IsReadOnly();
9752 attr->DecRef();
4db6714b 9753
283b7808
VZ
9754 return isReadOnly;
9755}
9756
2e9a6788 9757// ----------------------------------------------------------------------------
758cbedf 9758// attribute support: cache, automatic provider creation, ...
2e9a6788
VZ
9759// ----------------------------------------------------------------------------
9760
9761bool wxGrid::CanHaveAttributes()
9762{
9763 if ( !m_table )
9764 {
ca65c044 9765 return false;
2e9a6788
VZ
9766 }
9767
f2d76237 9768 return m_table->CanHaveAttributes();
2e9a6788
VZ
9769}
9770
0a976765
VZ
9771void wxGrid::ClearAttrCache()
9772{
9773 if ( m_attrCache.row != -1 )
9774 {
39bcce60 9775 wxSafeDecRef(m_attrCache.attr);
19d7140e 9776 m_attrCache.attr = NULL;
0a976765
VZ
9777 m_attrCache.row = -1;
9778 }
9779}
9780
9781void wxGrid::CacheAttr(int row, int col, wxGridCellAttr *attr) const
9782{
2b5f62a0
VZ
9783 if ( attr != NULL )
9784 {
9785 wxGrid *self = (wxGrid *)this; // const_cast
0a976765 9786
2b5f62a0
VZ
9787 self->ClearAttrCache();
9788 self->m_attrCache.row = row;
9789 self->m_attrCache.col = col;
9790 self->m_attrCache.attr = attr;
9791 wxSafeIncRef(attr);
9792 }
0a976765
VZ
9793}
9794
9795bool wxGrid::LookupAttr(int row, int col, wxGridCellAttr **attr) const
9796{
9797 if ( row == m_attrCache.row && col == m_attrCache.col )
9798 {
9799 *attr = m_attrCache.attr;
39bcce60 9800 wxSafeIncRef(m_attrCache.attr);
0a976765
VZ
9801
9802#ifdef DEBUG_ATTR_CACHE
9803 gs_nAttrCacheHits++;
9804#endif
9805
ca65c044 9806 return true;
0a976765
VZ
9807 }
9808 else
9809 {
9810#ifdef DEBUG_ATTR_CACHE
9811 gs_nAttrCacheMisses++;
9812#endif
4db6714b 9813
ca65c044 9814 return false;
0a976765
VZ
9815 }
9816}
9817
2e9a6788
VZ
9818wxGridCellAttr *wxGrid::GetCellAttr(int row, int col) const
9819{
a373d23b
SN
9820 wxGridCellAttr *attr = NULL;
9821 // Additional test to avoid looking at the cache e.g. for
9822 // wxNoCellCoords, as this will confuse memory management.
9823 if ( row >= 0 )
9824 {
3ed884a0
SN
9825 if ( !LookupAttr(row, col, &attr) )
9826 {
c2f5b920 9827 attr = m_table ? m_table->GetAttr(row, col, wxGridCellAttr::Any)
3ed884a0
SN
9828 : (wxGridCellAttr *)NULL;
9829 CacheAttr(row, col, attr);
9830 }
0a976765 9831 }
4db6714b 9832
508011ce
VZ
9833 if (attr)
9834 {
2796cce3 9835 attr->SetDefAttr(m_defaultCellAttr);
508011ce
VZ
9836 }
9837 else
9838 {
2796cce3
RD
9839 attr = m_defaultCellAttr;
9840 attr->IncRef();
9841 }
2e9a6788 9842
0a976765
VZ
9843 return attr;
9844}
9845
9846wxGridCellAttr *wxGrid::GetOrCreateCellAttr(int row, int col) const
9847{
19d7140e 9848 wxGridCellAttr *attr = (wxGridCellAttr *)NULL;
71e60f70 9849 bool canHave = ((wxGrid*)this)->CanHaveAttributes();
0a976765 9850
71e60f70
RD
9851 wxCHECK_MSG( canHave, attr, _T("Cell attributes not allowed"));
9852 wxCHECK_MSG( m_table, attr, _T("must have a table") );
1df4050d
VZ
9853
9854 attr = m_table->GetAttr(row, col, wxGridCellAttr::Cell);
9855 if ( !attr )
9856 {
9857 attr = new wxGridCellAttr(m_defaultCellAttr);
9858
9859 // artificially inc the ref count to match DecRef() in caller
9860 attr->IncRef();
9861 m_table->SetAttr(attr, row, col);
9862 }
0a976765 9863
2e9a6788
VZ
9864 return attr;
9865}
9866
0b190b0f
VZ
9867// ----------------------------------------------------------------------------
9868// setting column attributes (wrappers around SetColAttr)
9869// ----------------------------------------------------------------------------
9870
9871void wxGrid::SetColFormatBool(int col)
9872{
9873 SetColFormatCustom(col, wxGRID_VALUE_BOOL);
9874}
9875
9876void wxGrid::SetColFormatNumber(int col)
9877{
9878 SetColFormatCustom(col, wxGRID_VALUE_NUMBER);
9879}
9880
9881void wxGrid::SetColFormatFloat(int col, int width, int precision)
9882{
9883 wxString typeName = wxGRID_VALUE_FLOAT;
9884 if ( (width != -1) || (precision != -1) )
9885 {
9886 typeName << _T(':') << width << _T(',') << precision;
9887 }
9888
9889 SetColFormatCustom(col, typeName);
9890}
9891
9892void wxGrid::SetColFormatCustom(int col, const wxString& typeName)
9893{
999836aa 9894 wxGridCellAttr *attr = m_table->GetAttr(-1, col, wxGridCellAttr::Col );
4db6714b 9895 if (!attr)
19d7140e 9896 attr = new wxGridCellAttr;
0b190b0f
VZ
9897 wxGridCellRenderer *renderer = GetDefaultRendererForType(typeName);
9898 attr->SetRenderer(renderer);
9899
9900 SetColAttr(col, attr);
19d7140e 9901
0b190b0f
VZ
9902}
9903
758cbedf
VZ
9904// ----------------------------------------------------------------------------
9905// setting cell attributes: this is forwarded to the table
9906// ----------------------------------------------------------------------------
9907
27f35b66
SN
9908void wxGrid::SetAttr(int row, int col, wxGridCellAttr *attr)
9909{
9910 if ( CanHaveAttributes() )
9911 {
9912 m_table->SetAttr(attr, row, col);
9913 ClearAttrCache();
9914 }
9915 else
9916 {
9917 wxSafeDecRef(attr);
9918 }
9919}
9920
758cbedf
VZ
9921void wxGrid::SetRowAttr(int row, wxGridCellAttr *attr)
9922{
9923 if ( CanHaveAttributes() )
9924 {
9925 m_table->SetRowAttr(attr, row);
19d7140e 9926 ClearAttrCache();
758cbedf
VZ
9927 }
9928 else
9929 {
39bcce60 9930 wxSafeDecRef(attr);
758cbedf
VZ
9931 }
9932}
9933
9934void wxGrid::SetColAttr(int col, wxGridCellAttr *attr)
9935{
9936 if ( CanHaveAttributes() )
9937 {
9938 m_table->SetColAttr(attr, col);
19d7140e 9939 ClearAttrCache();
758cbedf
VZ
9940 }
9941 else
9942 {
39bcce60 9943 wxSafeDecRef(attr);
758cbedf
VZ
9944 }
9945}
9946
2e9a6788
VZ
9947void wxGrid::SetCellBackgroundColour( int row, int col, const wxColour& colour )
9948{
9949 if ( CanHaveAttributes() )
9950 {
0a976765 9951 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
2e9a6788
VZ
9952 attr->SetBackgroundColour(colour);
9953 attr->DecRef();
9954 }
9955}
9956
9957void wxGrid::SetCellTextColour( int row, int col, const wxColour& colour )
9958{
9959 if ( CanHaveAttributes() )
9960 {
0a976765 9961 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
2e9a6788
VZ
9962 attr->SetTextColour(colour);
9963 attr->DecRef();
9964 }
9965}
9966
9967void wxGrid::SetCellFont( int row, int col, const wxFont& font )
9968{
9969 if ( CanHaveAttributes() )
9970 {
0a976765 9971 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
2e9a6788
VZ
9972 attr->SetFont(font);
9973 attr->DecRef();
9974 }
9975}
9976
9977void wxGrid::SetCellAlignment( int row, int col, int horiz, int vert )
9978{
9979 if ( CanHaveAttributes() )
9980 {
0a976765 9981 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
2e9a6788
VZ
9982 attr->SetAlignment(horiz, vert);
9983 attr->DecRef();
ab79958a
VZ
9984 }
9985}
9986
27f35b66
SN
9987void wxGrid::SetCellOverflow( int row, int col, bool allow )
9988{
9989 if ( CanHaveAttributes() )
9990 {
9991 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
9992 attr->SetOverflow(allow);
9993 attr->DecRef();
9994 }
9995}
9996
9997void wxGrid::SetCellSize( int row, int col, int num_rows, int num_cols )
9998{
9999 if ( CanHaveAttributes() )
10000 {
10001 int cell_rows, cell_cols;
10002
10003 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
10004 attr->GetSize(&cell_rows, &cell_cols);
10005 attr->SetSize(num_rows, num_cols);
10006 attr->DecRef();
10007
10008 // Cannot set the size of a cell to 0 or negative values
10009 // While it is perfectly legal to do that, this function cannot
10010 // handle all the possibilies, do it by hand by getting the CellAttr.
10011 // You can only set the size of a cell to 1,1 or greater with this fn
10012 wxASSERT_MSG( !((cell_rows < 1) || (cell_cols < 1)),
10013 wxT("wxGrid::SetCellSize setting cell size that is already part of another cell"));
10014 wxASSERT_MSG( !((num_rows < 1) || (num_cols < 1)),
10015 wxT("wxGrid::SetCellSize setting cell size to < 1"));
10016
10017 // if this was already a multicell then "turn off" the other cells first
10018 if ((cell_rows > 1) || (cell_rows > 1))
10019 {
10020 int i, j;
2f024384 10021 for (j=row; j < row + cell_rows; j++)
27f35b66 10022 {
2f024384 10023 for (i=col; i < col + cell_cols; i++)
27f35b66
SN
10024 {
10025 if ((i != col) || (j != row))
10026 {
10027 wxGridCellAttr *attr_stub = GetOrCreateCellAttr(j, i);
10028 attr_stub->SetSize( 1, 1 );
10029 attr_stub->DecRef();
10030 }
10031 }
10032 }
10033 }
10034
10035 // mark the cells that will be covered by this cell to
10036 // negative or zero values to point back at this cell
10037 if (((num_rows > 1) || (num_cols > 1)) && (num_rows >= 1) && (num_cols >= 1))
10038 {
10039 int i, j;
2f024384 10040 for (j=row; j < row + num_rows; j++)
27f35b66 10041 {
2f024384 10042 for (i=col; i < col + num_cols; i++)
27f35b66
SN
10043 {
10044 if ((i != col) || (j != row))
10045 {
10046 wxGridCellAttr *attr_stub = GetOrCreateCellAttr(j, i);
2f024384 10047 attr_stub->SetSize( row - j, col - i );
27f35b66
SN
10048 attr_stub->DecRef();
10049 }
10050 }
10051 }
10052 }
10053 }
10054}
10055
ab79958a
VZ
10056void wxGrid::SetCellRenderer(int row, int col, wxGridCellRenderer *renderer)
10057{
10058 if ( CanHaveAttributes() )
10059 {
0a976765 10060 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
ab79958a
VZ
10061 attr->SetRenderer(renderer);
10062 attr->DecRef();
9b4aede2
RD
10063 }
10064}
10065
10066void wxGrid::SetCellEditor(int row, int col, wxGridCellEditor* editor)
10067{
10068 if ( CanHaveAttributes() )
10069 {
10070 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
10071 attr->SetEditor(editor);
10072 attr->DecRef();
283b7808
VZ
10073 }
10074}
10075
10076void wxGrid::SetReadOnly(int row, int col, bool isReadOnly)
10077{
10078 if ( CanHaveAttributes() )
10079 {
10080 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
10081 attr->SetReadOnly(isReadOnly);
10082 attr->DecRef();
2e9a6788 10083 }
f85afd4e
MB
10084}
10085
f2d76237
RD
10086// ----------------------------------------------------------------------------
10087// Data type registration
10088// ----------------------------------------------------------------------------
10089
10090void wxGrid::RegisterDataType(const wxString& typeName,
10091 wxGridCellRenderer* renderer,
10092 wxGridCellEditor* editor)
10093{
10094 m_typeRegistry->RegisterDataType(typeName, renderer, editor);
10095}
10096
10097
a9339fe2 10098wxGridCellEditor * wxGrid::GetDefaultEditorForCell(int row, int col) const
f2d76237
RD
10099{
10100 wxString typeName = m_table->GetTypeName(row, col);
10101 return GetDefaultEditorForType(typeName);
10102}
10103
a9339fe2 10104wxGridCellRenderer * wxGrid::GetDefaultRendererForCell(int row, int col) const
f2d76237
RD
10105{
10106 wxString typeName = m_table->GetTypeName(row, col);
10107 return GetDefaultRendererForType(typeName);
10108}
10109
a9339fe2 10110wxGridCellEditor * wxGrid::GetDefaultEditorForType(const wxString& typeName) const
f2d76237 10111{
c4608a8a 10112 int index = m_typeRegistry->FindOrCloneDataType(typeName);
0b190b0f
VZ
10113 if ( index == wxNOT_FOUND )
10114 {
f87ab3cc 10115 wxString errStr;
4db6714b
KH
10116
10117 errStr.Printf(wxT("Unknown data type name [%s]"), typeName.c_str());
10118 wxFAIL_MSG(errStr.c_str());
0b190b0f 10119
f2d76237
RD
10120 return NULL;
10121 }
0b190b0f 10122
f2d76237
RD
10123 return m_typeRegistry->GetEditor(index);
10124}
10125
a9339fe2 10126wxGridCellRenderer * wxGrid::GetDefaultRendererForType(const wxString& typeName) const
f2d76237 10127{
c4608a8a 10128 int index = m_typeRegistry->FindOrCloneDataType(typeName);
0b190b0f
VZ
10129 if ( index == wxNOT_FOUND )
10130 {
2f024384 10131 wxString errStr;
4db6714b
KH
10132
10133 errStr.Printf(wxT("Unknown data type name [%s]"), typeName.c_str());
10134 wxFAIL_MSG(errStr.c_str());
0b190b0f 10135
c4608a8a 10136 return NULL;
e72b4213 10137 }
0b190b0f 10138
c4608a8a 10139 return m_typeRegistry->GetRenderer(index);
f2d76237
RD
10140}
10141
2e9a6788
VZ
10142// ----------------------------------------------------------------------------
10143// row/col size
10144// ----------------------------------------------------------------------------
10145
6e8524b1
MB
10146void wxGrid::EnableDragRowSize( bool enable )
10147{
10148 m_canDragRowSize = enable;
10149}
10150
6e8524b1
MB
10151void wxGrid::EnableDragColSize( bool enable )
10152{
10153 m_canDragColSize = enable;
10154}
10155
4cfa5de6
RD
10156void wxGrid::EnableDragGridSize( bool enable )
10157{
10158 m_canDragGridSize = enable;
10159}
10160
79dbea21
RD
10161void wxGrid::EnableDragCell( bool enable )
10162{
10163 m_canDragCell = enable;
10164}
6e8524b1 10165
f85afd4e
MB
10166void wxGrid::SetDefaultRowSize( int height, bool resizeExistingRows )
10167{
b8d24d4e 10168 m_defaultRowHeight = wxMax( height, m_minAcceptableRowHeight );
f85afd4e
MB
10169
10170 if ( resizeExistingRows )
10171 {
b1da8107
SN
10172 // since we are resizing all rows to the default row size,
10173 // we can simply clear the row heights and row bottoms
10174 // arrays (which also allows us to take advantage of
10175 // some speed optimisations)
10176 m_rowHeights.Empty();
10177 m_rowBottoms.Empty();
edb89f7e
VZ
10178 if ( !GetBatchCount() )
10179 CalcDimensions();
f85afd4e
MB
10180 }
10181}
10182
10183void wxGrid::SetRowSize( int row, int height )
10184{
b99be8fb 10185 wxCHECK_RET( row >= 0 && row < m_numRows, _T("invalid row index") );
60ff3b99 10186
b4bfd0fa 10187 // See comment in SetColSize
4db6714b
KH
10188 if ( height < GetRowMinimalAcceptableHeight())
10189 return;
b8d24d4e 10190
7c1cb261
VZ
10191 if ( m_rowHeights.IsEmpty() )
10192 {
10193 // need to really create the array
10194 InitRowHeights();
10195 }
60ff3b99 10196
b99be8fb
VZ
10197 int h = wxMax( 0, height );
10198 int diff = h - m_rowHeights[row];
60ff3b99 10199
b99be8fb 10200 m_rowHeights[row] = h;
7c1cb261 10201 int i;
56b6cf26 10202 for ( i = row; i < m_numRows; i++ )
f85afd4e 10203 {
b99be8fb 10204 m_rowBottoms[i] += diff;
f85afd4e 10205 }
2f024384 10206
faec5a43
SN
10207 if ( !GetBatchCount() )
10208 CalcDimensions();
f85afd4e
MB
10209}
10210
10211void wxGrid::SetDefaultColSize( int width, bool resizeExistingCols )
10212{
b8d24d4e 10213 m_defaultColWidth = wxMax( width, m_minAcceptableColWidth );
f85afd4e
MB
10214
10215 if ( resizeExistingCols )
10216 {
b1da8107
SN
10217 // since we are resizing all columns to the default column size,
10218 // we can simply clear the col widths and col rights
10219 // arrays (which also allows us to take advantage of
10220 // some speed optimisations)
10221 m_colWidths.Empty();
10222 m_colRights.Empty();
edb89f7e
VZ
10223 if ( !GetBatchCount() )
10224 CalcDimensions();
f85afd4e
MB
10225 }
10226}
10227
10228void wxGrid::SetColSize( int col, int width )
10229{
b99be8fb 10230 wxCHECK_RET( col >= 0 && col < m_numCols, _T("invalid column index") );
60ff3b99 10231
43947979 10232 // should we check that it's bigger than GetColMinimalWidth(col) here?
b4bfd0fa
RG
10233 // (VZ)
10234 // No, because it is reasonable to assume the library user know's
d4175745 10235 // what he is doing. However we should test against the weaker
ccdee36f 10236 // constraint of minimalAcceptableWidth, as this breaks rendering
3e13956a 10237 //
b4bfd0fa 10238 // This test then fixes sf.net bug #645734
3e13956a 10239
962a48f6 10240 if ( width < GetColMinimalAcceptableWidth() )
4db6714b 10241 return;
3e13956a 10242
7c1cb261
VZ
10243 if ( m_colWidths.IsEmpty() )
10244 {
10245 // need to really create the array
10246 InitColWidths();
10247 }
f85afd4e 10248
56b6cf26 10249 // if < 0 then calculate new width from label
4db6714b 10250 if ( width < 0 )
73145b0e 10251 {
56b6cf26
DS
10252 long w, h;
10253 wxArrayString lines;
10254 wxClientDC dc(m_colLabelWin);
10255 dc.SetFont(GetLabelFont());
10256 StringToLines(GetColLabelValue(col), lines);
10257 GetTextBoxSize(dc, lines, &w, &h);
10258 width = w + 6;
73145b0e 10259 }
962a48f6 10260
b99be8fb
VZ
10261 int w = wxMax( 0, width );
10262 int diff = w - m_colWidths[col];
10263 m_colWidths[col] = w;
60ff3b99 10264
7c1cb261 10265 int i;
d4175745
VZ
10266 int colPos;
10267 for ( colPos = GetColPos( col ); colPos < m_numCols; colPos++ )
f85afd4e 10268 {
d4175745 10269 i = GetColAt( colPos );
b99be8fb 10270 m_colRights[i] += diff;
f85afd4e 10271 }
962a48f6 10272
faec5a43
SN
10273 if ( !GetBatchCount() )
10274 CalcDimensions();
f85afd4e
MB
10275}
10276
43947979
VZ
10277void wxGrid::SetColMinimalWidth( int col, int width )
10278{
4db6714b
KH
10279 if (width > GetColMinimalAcceptableWidth())
10280 {
c6fbe2f0 10281 wxLongToLongHashMap::key_type key = (wxLongToLongHashMap::key_type)col;
8253f2e0 10282 m_colMinWidths[key] = width;
b8d24d4e 10283 }
af547d51
VZ
10284}
10285
10286void wxGrid::SetRowMinimalHeight( int row, int width )
10287{
4db6714b
KH
10288 if (width > GetRowMinimalAcceptableHeight())
10289 {
c6fbe2f0 10290 wxLongToLongHashMap::key_type key = (wxLongToLongHashMap::key_type)row;
8253f2e0 10291 m_rowMinHeights[key] = width;
b8d24d4e 10292 }
43947979
VZ
10293}
10294
10295int wxGrid::GetColMinimalWidth(int col) const
10296{
c6fbe2f0 10297 wxLongToLongHashMap::key_type key = (wxLongToLongHashMap::key_type)col;
8253f2e0 10298 wxLongToLongHashMap::const_iterator it = m_colMinWidths.find(key);
962a48f6 10299
ba8c1601 10300 return it != m_colMinWidths.end() ? (int)it->second : m_minAcceptableColWidth;
af547d51
VZ
10301}
10302
10303int wxGrid::GetRowMinimalHeight(int row) const
10304{
c6fbe2f0 10305 wxLongToLongHashMap::key_type key = (wxLongToLongHashMap::key_type)row;
8253f2e0 10306 wxLongToLongHashMap::const_iterator it = m_rowMinHeights.find(key);
962a48f6 10307
ba8c1601 10308 return it != m_rowMinHeights.end() ? (int)it->second : m_minAcceptableRowHeight;
b8d24d4e
RG
10309}
10310
10311void wxGrid::SetColMinimalAcceptableWidth( int width )
10312{
20c84410 10313 // We do allow a width of 0 since this gives us
962a48f6
DS
10314 // an easy way to temporarily hiding columns.
10315 if ( width >= 0 )
10316 m_minAcceptableColWidth = width;
b8d24d4e
RG
10317}
10318
10319void wxGrid::SetRowMinimalAcceptableHeight( int height )
10320{
20c84410 10321 // We do allow a height of 0 since this gives us
962a48f6
DS
10322 // an easy way to temporarily hiding rows.
10323 if ( height >= 0 )
10324 m_minAcceptableRowHeight = height;
17a1ebd1 10325}
b8d24d4e
RG
10326
10327int wxGrid::GetColMinimalAcceptableWidth() const
10328{
10329 return m_minAcceptableColWidth;
10330}
10331
10332int wxGrid::GetRowMinimalAcceptableHeight() const
10333{
10334 return m_minAcceptableRowHeight;
43947979
VZ
10335}
10336
57c086ef
VZ
10337// ----------------------------------------------------------------------------
10338// auto sizing
10339// ----------------------------------------------------------------------------
10340
af547d51 10341void wxGrid::AutoSizeColOrRow( int colOrRow, bool setAsMin, bool column )
65e4e78e
VZ
10342{
10343 wxClientDC dc(m_gridWin);
10344
962a48f6 10345 // cancel editing of cell
13f6e9e8
RG
10346 HideCellEditControl();
10347 SaveEditControlValue();
10348
962a48f6 10349 // init both of them to avoid compiler warnings, even if we only need one
a95e38c0
VZ
10350 int row = -1,
10351 col = -1;
af547d51
VZ
10352 if ( column )
10353 col = colOrRow;
10354 else
10355 row = colOrRow;
10356
10357 wxCoord extent, extentMax = 0;
10358 int max = column ? m_numRows : m_numCols;
39bcce60 10359 for ( int rowOrCol = 0; rowOrCol < max; rowOrCol++ )
65e4e78e 10360 {
af547d51
VZ
10361 if ( column )
10362 row = rowOrCol;
10363 else
10364 col = rowOrCol;
10365
2f024384
DS
10366 wxGridCellAttr *attr = GetCellAttr(row, col);
10367 wxGridCellRenderer *renderer = attr->GetRenderer(this, row, col);
65e4e78e
VZ
10368 if ( renderer )
10369 {
af547d51
VZ
10370 wxSize size = renderer->GetBestSize(*this, *attr, dc, row, col);
10371 extent = column ? size.x : size.y;
10372 if ( extent > extentMax )
af547d51 10373 extentMax = extent;
0b190b0f
VZ
10374
10375 renderer->DecRef();
65e4e78e
VZ
10376 }
10377
10378 attr->DecRef();
10379 }
10380
af547d51
VZ
10381 // now also compare with the column label extent
10382 wxCoord w, h;
65e4e78e 10383 dc.SetFont( GetLabelFont() );
294d195c
MB
10384
10385 if ( column )
d43851f7 10386 {
9d4b8a5d 10387 dc.GetMultiLineTextExtent( GetColLabelValue(col), &w, &h );
4db6714b 10388 if ( GetColLabelTextOrientation() == wxVERTICAL )
d43851f7
JS
10389 w = h;
10390 }
294d195c 10391 else
9d4b8a5d 10392 dc.GetMultiLineTextExtent( GetRowLabelValue(row), &w, &h );
294d195c 10393
af547d51
VZ
10394 extent = column ? w : h;
10395 if ( extent > extentMax )
af547d51 10396 extentMax = extent;
65e4e78e 10397
af547d51 10398 if ( !extentMax )
65e4e78e 10399 {
af547d51 10400 // empty column - give default extent (notice that if extentMax is less
2f024384 10401 // than default extent but != 0, it's OK)
af547d51 10402 extentMax = column ? m_defaultColWidth : m_defaultRowHeight;
65e4e78e
VZ
10403 }
10404 else
10405 {
a95e38c0 10406 if ( column )
a95e38c0
VZ
10407 // leave some space around text
10408 extentMax += 10;
f6bcfd97 10409 else
f6bcfd97 10410 extentMax += 6;
65e4e78e
VZ
10411 }
10412
edb89f7e
VZ
10413 if ( column )
10414 {
962a48f6 10415 SetColSize( col, extentMax );
faec5a43
SN
10416 if ( !GetBatchCount() )
10417 {
edb89f7e
VZ
10418 int cw, ch, dummy;
10419 m_gridWin->GetClientSize( &cw, &ch );
10420 wxRect rect ( CellToRect( 0, col ) );
10421 rect.y = 0;
10422 CalcScrolledPosition(rect.x, 0, &rect.x, &dummy);
10423 rect.width = cw - rect.x;
10424 rect.height = m_colLabelHeight;
ca65c044 10425 m_colLabelWin->Refresh( true, &rect );
edb89f7e
VZ
10426 }
10427 }
10428 else
10429 {
39bcce60 10430 SetRowSize(row, extentMax);
faec5a43
SN
10431 if ( !GetBatchCount() )
10432 {
edb89f7e
VZ
10433 int cw, ch, dummy;
10434 m_gridWin->GetClientSize( &cw, &ch );
ccdee36f 10435 wxRect rect( CellToRect( row, 0 ) );
edb89f7e
VZ
10436 rect.x = 0;
10437 CalcScrolledPosition(0, rect.y, &dummy, &rect.y);
10438 rect.width = m_rowLabelWidth;
faec5a43 10439 rect.height = ch - rect.y;
ca65c044 10440 m_rowLabelWin->Refresh( true, &rect );
edb89f7e 10441 }
faec5a43 10442 }
2f024384 10443
65e4e78e
VZ
10444 if ( setAsMin )
10445 {
af547d51
VZ
10446 if ( column )
10447 SetColMinimalWidth(col, extentMax);
10448 else
39bcce60 10449 SetRowMinimalHeight(row, extentMax);
65e4e78e
VZ
10450 }
10451}
10452
266e8367 10453int wxGrid::SetOrCalcColumnSizes(bool calcOnly, bool setAsMin)
65e4e78e 10454{
57c086ef
VZ
10455 int width = m_rowLabelWidth;
10456
faec5a43
SN
10457 if ( !calcOnly )
10458 BeginBatch();
97a9929e 10459
65e4e78e
VZ
10460 for ( int col = 0; col < m_numCols; col++ )
10461 {
266e8367 10462 if ( !calcOnly )
266e8367 10463 AutoSizeColumn(col, setAsMin);
57c086ef
VZ
10464
10465 width += GetColWidth(col);
65e4e78e 10466 }
97a9929e 10467
faec5a43
SN
10468 if ( !calcOnly )
10469 EndBatch();
97a9929e 10470
266e8367 10471 return width;
65e4e78e
VZ
10472}
10473
266e8367 10474int wxGrid::SetOrCalcRowSizes(bool calcOnly, bool setAsMin)
57c086ef
VZ
10475{
10476 int height = m_colLabelHeight;
10477
faec5a43
SN
10478 if ( !calcOnly )
10479 BeginBatch();
97a9929e 10480
57c086ef
VZ
10481 for ( int row = 0; row < m_numRows; row++ )
10482 {
af547d51 10483 if ( !calcOnly )
af547d51 10484 AutoSizeRow(row, setAsMin);
57c086ef
VZ
10485
10486 height += GetRowHeight(row);
10487 }
97a9929e 10488
faec5a43
SN
10489 if ( !calcOnly )
10490 EndBatch();
97a9929e 10491
266e8367 10492 return height;
57c086ef
VZ
10493}
10494
10495void wxGrid::AutoSize()
10496{
97a9929e
VZ
10497 BeginBatch();
10498
ca65c044 10499 wxSize size(SetOrCalcColumnSizes(false), SetOrCalcRowSizes(false));
97a9929e
VZ
10500
10501 // round up the size to a multiple of scroll step - this ensures that we
10502 // won't get the scrollbars if we're sized exactly to this width
2b5f62a0
VZ
10503 // CalcDimension adds m_extraWidth + 1 etc. to calculate the necessary
10504 // scrollbar steps
2f024384
DS
10505 wxSize sizeFit(
10506 GetScrollX(size.x + m_extraWidth + 1) * m_scrollLineX,
a9339fe2 10507 GetScrollY(size.y + m_extraHeight + 1) * m_scrollLineY );
97a9929e 10508
2b5f62a0 10509 // distribute the extra space between the columns/rows to avoid having
97a9929e 10510 // extra white space
2b5f62a0
VZ
10511
10512 // Remove the extra m_extraWidth + 1 added above
10513 wxCoord diff = sizeFit.x - size.x + (m_extraWidth + 1);
10514 if ( diff && m_numCols )
97a9929e
VZ
10515 {
10516 // try to resize the columns uniformly
10517 wxCoord diffPerCol = diff / m_numCols;
10518 if ( diffPerCol )
10519 {
10520 for ( int col = 0; col < m_numCols; col++ )
10521 {
10522 SetColSize(col, GetColWidth(col) + diffPerCol);
10523 }
10524 }
10525
10526 // add remaining amount to the last columns
10527 diff -= diffPerCol * m_numCols;
10528 if ( diff )
10529 {
10530 for ( int col = m_numCols - 1; col >= m_numCols - diff; col-- )
10531 {
10532 SetColSize(col, GetColWidth(col) + 1);
10533 }
10534 }
10535 }
10536
10537 // same for rows
2b5f62a0
VZ
10538 diff = sizeFit.y - size.y - (m_extraHeight + 1);
10539 if ( diff && m_numRows )
97a9929e
VZ
10540 {
10541 // try to resize the columns uniformly
10542 wxCoord diffPerRow = diff / m_numRows;
10543 if ( diffPerRow )
10544 {
10545 for ( int row = 0; row < m_numRows; row++ )
10546 {
10547 SetRowSize(row, GetRowHeight(row) + diffPerRow);
10548 }
10549 }
10550
10551 // add remaining amount to the last rows
10552 diff -= diffPerRow * m_numRows;
10553 if ( diff )
10554 {
10555 for ( int row = m_numRows - 1; row >= m_numRows - diff; row-- )
10556 {
10557 SetRowSize(row, GetRowHeight(row) + 1);
10558 }
10559 }
10560 }
10561
10562 EndBatch();
10563
10564 SetClientSize(sizeFit);
266e8367
VZ
10565}
10566
d43851f7
JS
10567void wxGrid::AutoSizeRowLabelSize( int row )
10568{
10569 wxArrayString lines;
10570 long w, h;
10571
10572 // Hide the edit control, so it
4db6714b
KH
10573 // won't interfere with drag-shrinking.
10574 if ( IsCellEditControlShown() )
d43851f7
JS
10575 {
10576 HideCellEditControl();
10577 SaveEditControlValue();
10578 }
10579
10580 // autosize row height depending on label text
10581 StringToLines( GetRowLabelValue( row ), lines );
10582 wxClientDC dc( m_rowLabelWin );
ccdee36f 10583 GetTextBoxSize( dc, lines, &w, &h );
4db6714b 10584 if ( h < m_defaultRowHeight )
d43851f7
JS
10585 h = m_defaultRowHeight;
10586 SetRowSize(row, h);
10587 ForceRefresh();
10588}
10589
10590void wxGrid::AutoSizeColLabelSize( int col )
10591{
10592 wxArrayString lines;
10593 long w, h;
10594
10595 // Hide the edit control, so it
c2f5b920 10596 // won't interfere with drag-shrinking.
4db6714b 10597 if ( IsCellEditControlShown() )
d43851f7
JS
10598 {
10599 HideCellEditControl();
10600 SaveEditControlValue();
10601 }
10602
10603 // autosize column width depending on label text
10604 StringToLines( GetColLabelValue( col ), lines );
10605 wxClientDC dc( m_colLabelWin );
4db6714b 10606 if ( GetColLabelTextOrientation() == wxHORIZONTAL )
2f024384 10607 GetTextBoxSize( dc, lines, &w, &h );
d43851f7 10608 else
2f024384 10609 GetTextBoxSize( dc, lines, &h, &w );
4db6714b 10610 if ( w < m_defaultColWidth )
d43851f7
JS
10611 w = m_defaultColWidth;
10612 SetColSize(col, w);
10613 ForceRefresh();
10614}
10615
266e8367
VZ
10616wxSize wxGrid::DoGetBestSize() const
10617{
10618 // don't set sizes, only calculate them
10619 wxGrid *self = (wxGrid *)this; // const_cast
10620
84035150 10621 int width, height;
ca65c044
WS
10622 width = self->SetOrCalcColumnSizes(true);
10623 height = self->SetOrCalcRowSizes(true);
84035150 10624
4db6714b
KH
10625 if (!width)
10626 width = 100;
10627 if (!height)
10628 height = 80;
42841dfc 10629
4db6714b 10630 // Round up to a multiple the scroll rate
c2f5b920 10631 // NOTE: this still doesn't get rid of the scrollbars;
4db6714b 10632 // is there any magic incantation for that?
6d308072
RD
10633 int xpu, ypu;
10634 GetScrollPixelsPerUnit(&xpu, &ypu);
b39a5dc4
RD
10635 if (xpu)
10636 width += 1 + xpu - (width % xpu);
10637 if (ypu)
10638 height += 1 + ypu - (height % ypu);
42841dfc 10639
6d308072 10640 // limit to 1/4 of the screen size
84035150 10641 int maxwidth, maxheight;
4db6714b 10642 wxDisplaySize( &maxwidth, &maxheight );
6d308072 10643 maxwidth /= 2;
42841dfc 10644 maxheight /= 2;
4db6714b
KH
10645 if ( width > maxwidth )
10646 width = maxwidth;
10647 if ( height > maxheight )
10648 height = maxheight;
42841dfc 10649
6d308072 10650 wxSize best(width, height);
962a48f6 10651
6d308072
RD
10652 // NOTE: This size should be cached, but first we need to add calls to
10653 // InvalidateBestSize everywhere that could change the results of this
10654 // calculation.
10655 // CacheBestSize(size);
962a48f6 10656
6d308072 10657 return best;
266e8367
VZ
10658}
10659
10660void wxGrid::Fit()
10661{
10662 AutoSize();
57c086ef
VZ
10663}
10664
6fc0f38f
SN
10665wxPen& wxGrid::GetDividerPen() const
10666{
10667 return wxNullPen;
10668}
10669
57c086ef
VZ
10670// ----------------------------------------------------------------------------
10671// cell value accessor functions
10672// ----------------------------------------------------------------------------
f85afd4e
MB
10673
10674void wxGrid::SetCellValue( int row, int col, const wxString& s )
10675{
10676 if ( m_table )
10677 {
f6bcfd97 10678 m_table->SetValue( row, col, s );
2d66e025
MB
10679 if ( !GetBatchCount() )
10680 {
27f35b66
SN
10681 int dummy;
10682 wxRect rect( CellToRect( row, col ) );
10683 rect.x = 0;
10684 rect.width = m_gridWin->GetClientSize().GetWidth();
10685 CalcScrolledPosition(0, rect.y, &dummy, &rect.y);
ca65c044 10686 m_gridWin->Refresh( false, &rect );
2d66e025 10687 }
60ff3b99 10688
f85afd4e 10689 if ( m_currentCellCoords.GetRow() == row &&
4cfa5de6 10690 m_currentCellCoords.GetCol() == col &&
f6bcfd97
BP
10691 IsCellEditControlShown())
10692 // Note: If we are using IsCellEditControlEnabled,
10693 // this interacts badly with calling SetCellValue from
10694 // an EVT_GRID_CELL_CHANGE handler.
f85afd4e 10695 {
4cfa5de6
RD
10696 HideCellEditControl();
10697 ShowCellEditControl(); // will reread data from table
f85afd4e 10698 }
f85afd4e
MB
10699 }
10700}
10701
962a48f6 10702// ----------------------------------------------------------------------------
2f024384 10703// block, row and column selection
962a48f6 10704// ----------------------------------------------------------------------------
f85afd4e
MB
10705
10706void wxGrid::SelectRow( int row, bool addToSelected )
10707{
b5808881 10708 if ( IsSelection() && !addToSelected )
e32352cf 10709 ClearSelection();
70c7a608 10710
3f3dc2ef 10711 if ( m_selection )
ca65c044 10712 m_selection->SelectRow( row, false, addToSelected );
f85afd4e
MB
10713}
10714
f85afd4e
MB
10715void wxGrid::SelectCol( int col, bool addToSelected )
10716{
b5808881 10717 if ( IsSelection() && !addToSelected )
e32352cf 10718 ClearSelection();
f85afd4e 10719
3f3dc2ef 10720 if ( m_selection )
ca65c044 10721 m_selection->SelectCol( col, false, addToSelected );
f85afd4e
MB
10722}
10723
84912ef8 10724void wxGrid::SelectBlock( int topRow, int leftCol, int bottomRow, int rightCol,
c9097836 10725 bool addToSelected )
f85afd4e 10726{
c9097836
MB
10727 if ( IsSelection() && !addToSelected )
10728 ClearSelection();
f85afd4e 10729
3f3dc2ef
VZ
10730 if ( m_selection )
10731 m_selection->SelectBlock( topRow, leftCol, bottomRow, rightCol,
ca65c044 10732 false, addToSelected );
f85afd4e
MB
10733}
10734
10735void wxGrid::SelectAll()
10736{
f74d0b57 10737 if ( m_numRows > 0 && m_numCols > 0 )
3f3dc2ef
VZ
10738 {
10739 if ( m_selection )
ccdee36f 10740 m_selection->SelectBlock( 0, 0, m_numRows - 1, m_numCols - 1 );
3f3dc2ef 10741 }
b5808881 10742}
f85afd4e 10743
962a48f6
DS
10744// ----------------------------------------------------------------------------
10745// cell, row and col deselection
10746// ----------------------------------------------------------------------------
f7b4b343
VZ
10747
10748void wxGrid::DeselectRow( int row )
10749{
3f3dc2ef
VZ
10750 if ( !m_selection )
10751 return;
10752
f7b4b343
VZ
10753 if ( m_selection->GetSelectionMode() == wxGrid::wxGridSelectRows )
10754 {
10755 if ( m_selection->IsInSelection(row, 0 ) )
2f024384 10756 m_selection->ToggleCellSelection(row, 0);
ffdd3c98 10757 }
f7b4b343
VZ
10758 else
10759 {
10760 int nCols = GetNumberCols();
2f024384 10761 for ( int i = 0; i < nCols; i++ )
f7b4b343
VZ
10762 {
10763 if ( m_selection->IsInSelection(row, i ) )
2f024384 10764 m_selection->ToggleCellSelection(row, i);
f7b4b343
VZ
10765 }
10766 }
10767}
10768
10769void wxGrid::DeselectCol( int col )
10770{
3f3dc2ef
VZ
10771 if ( !m_selection )
10772 return;
10773
f7b4b343
VZ
10774 if ( m_selection->GetSelectionMode() == wxGrid::wxGridSelectColumns )
10775 {
10776 if ( m_selection->IsInSelection(0, col ) )
2f024384 10777 m_selection->ToggleCellSelection(0, col);
f7b4b343
VZ
10778 }
10779 else
10780 {
10781 int nRows = GetNumberRows();
2f024384 10782 for ( int i = 0; i < nRows; i++ )
f7b4b343
VZ
10783 {
10784 if ( m_selection->IsInSelection(i, col ) )
10785 m_selection->ToggleCellSelection(i, col);
10786 }
10787 }
10788}
10789
10790void wxGrid::DeselectCell( int row, int col )
10791{
3f3dc2ef 10792 if ( m_selection && m_selection->IsInSelection(row, col) )
f7b4b343
VZ
10793 m_selection->ToggleCellSelection(row, col);
10794}
10795
b5808881
SN
10796bool wxGrid::IsSelection()
10797{
3f3dc2ef 10798 return ( m_selection && (m_selection->IsSelection() ||
b5808881 10799 ( m_selectingTopLeft != wxGridNoCellCoords &&
3f3dc2ef 10800 m_selectingBottomRight != wxGridNoCellCoords) ) );
f85afd4e
MB
10801}
10802
84035150 10803bool wxGrid::IsInSelection( int row, int col ) const
b5808881 10804{
3f3dc2ef 10805 return ( m_selection && (m_selection->IsInSelection( row, col ) ||
b5808881
SN
10806 ( row >= m_selectingTopLeft.GetRow() &&
10807 col >= m_selectingTopLeft.GetCol() &&
10808 row <= m_selectingBottomRight.GetRow() &&
3f3dc2ef 10809 col <= m_selectingBottomRight.GetCol() )) );
b5808881 10810}
f85afd4e 10811
aa5b8857
SN
10812wxGridCellCoordsArray wxGrid::GetSelectedCells() const
10813{
4db6714b
KH
10814 if (!m_selection)
10815 {
10816 wxGridCellCoordsArray a;
10817 return a;
10818 }
10819
aa5b8857
SN
10820 return m_selection->m_cellSelection;
10821}
4db6714b 10822
aa5b8857
SN
10823wxGridCellCoordsArray wxGrid::GetSelectionBlockTopLeft() const
10824{
4db6714b
KH
10825 if (!m_selection)
10826 {
10827 wxGridCellCoordsArray a;
10828 return a;
10829 }
10830
aa5b8857
SN
10831 return m_selection->m_blockSelectionTopLeft;
10832}
4db6714b 10833
aa5b8857
SN
10834wxGridCellCoordsArray wxGrid::GetSelectionBlockBottomRight() const
10835{
4db6714b
KH
10836 if (!m_selection)
10837 {
10838 wxGridCellCoordsArray a;
10839 return a;
10840 }
10841
a8de8190 10842 return m_selection->m_blockSelectionBottomRight;
aa5b8857 10843}
4db6714b 10844
aa5b8857
SN
10845wxArrayInt wxGrid::GetSelectedRows() const
10846{
4db6714b
KH
10847 if (!m_selection)
10848 {
10849 wxArrayInt a;
10850 return a;
10851 }
10852
aa5b8857
SN
10853 return m_selection->m_rowSelection;
10854}
4db6714b 10855
aa5b8857
SN
10856wxArrayInt wxGrid::GetSelectedCols() const
10857{
4db6714b
KH
10858 if (!m_selection)
10859 {
10860 wxArrayInt a;
10861 return a;
10862 }
10863
aa5b8857
SN
10864 return m_selection->m_colSelection;
10865}
10866
f85afd4e
MB
10867void wxGrid::ClearSelection()
10868{
b524b5c6
VZ
10869 m_selectingTopLeft =
10870 m_selectingBottomRight =
10871 m_selectingKeyboard = wxGridNoCellCoords;
3f3dc2ef
VZ
10872 if ( m_selection )
10873 m_selection->ClearSelection();
8f177c8e 10874}
f85afd4e 10875
da6af900 10876// This function returns the rectangle that encloses the given block
2d66e025
MB
10877// in device coords clipped to the client size of the grid window.
10878//
58dd5b3b
MB
10879wxRect wxGrid::BlockToDeviceRect( const wxGridCellCoords &topLeft,
10880 const wxGridCellCoords &bottomRight )
f85afd4e 10881{
58dd5b3b 10882 wxRect rect( wxGridNoCellRect );
f85afd4e
MB
10883 wxRect cellRect;
10884
58dd5b3b
MB
10885 cellRect = CellToRect( topLeft );
10886 if ( cellRect != wxGridNoCellRect )
f85afd4e 10887 {
58dd5b3b
MB
10888 rect = cellRect;
10889 }
10890 else
10891 {
962a48f6 10892 rect = wxRect(0, 0, 0, 0);
58dd5b3b 10893 }
60ff3b99 10894
58dd5b3b
MB
10895 cellRect = CellToRect( bottomRight );
10896 if ( cellRect != wxGridNoCellRect )
10897 {
10898 rect += cellRect;
2d66e025
MB
10899 }
10900 else
10901 {
10902 return wxGridNoCellRect;
f85afd4e
MB
10903 }
10904
27f35b66
SN
10905 int i, j;
10906 int left = rect.GetLeft();
10907 int top = rect.GetTop();
10908 int right = rect.GetRight();
10909 int bottom = rect.GetBottom();
10910
10911 int leftCol = topLeft.GetCol();
10912 int topRow = topLeft.GetRow();
10913 int rightCol = bottomRight.GetCol();
10914 int bottomRow = bottomRight.GetRow();
10915
3ed884a0
SN
10916 if (left > right)
10917 {
10918 i = left;
10919 left = right;
10920 right = i;
10921 i = leftCol;
4db6714b 10922 leftCol = rightCol;
3ed884a0
SN
10923 rightCol = i;
10924 }
10925
10926 if (top > bottom)
10927 {
10928 i = top;
10929 top = bottom;
10930 bottom = i;
10931 i = topRow;
10932 topRow = bottomRow;
10933 bottomRow = i;
10934 }
10935
27f35b66
SN
10936 for ( j = topRow; j <= bottomRow; j++ )
10937 {
10938 for ( i = leftCol; i <= rightCol; i++ )
10939 {
4db6714b 10940 if ((j == topRow) || (j == bottomRow) || (i == leftCol) || (i == rightCol))
27f35b66
SN
10941 {
10942 cellRect = CellToRect( j, i );
10943
10944 if (cellRect.x < left)
10945 left = cellRect.x;
10946 if (cellRect.y < top)
10947 top = cellRect.y;
10948 if (cellRect.x + cellRect.width > right)
10949 right = cellRect.x + cellRect.width;
10950 if (cellRect.y + cellRect.height > bottom)
10951 bottom = cellRect.y + cellRect.height;
10952 }
4db6714b
KH
10953 else
10954 {
10955 i = rightCol; // jump over inner cells.
10956 }
27f35b66
SN
10957 }
10958 }
10959
58dd5b3b
MB
10960 // convert to scrolled coords
10961 //
27f35b66
SN
10962 CalcScrolledPosition( left, top, &left, &top );
10963 CalcScrolledPosition( right, bottom, &right, &bottom );
58dd5b3b
MB
10964
10965 int cw, ch;
10966 m_gridWin->GetClientSize( &cw, &ch );
10967
f6bcfd97 10968 if (right < 0 || bottom < 0 || left > cw || top > ch)
c47addef 10969 return wxRect(0,0,0,0);
f6bcfd97 10970
58dd5b3b
MB
10971 rect.SetLeft( wxMax(0, left) );
10972 rect.SetTop( wxMax(0, top) );
10973 rect.SetRight( wxMin(cw, right) );
10974 rect.SetBottom( wxMin(ch, bottom) );
10975
f85afd4e
MB
10976 return rect;
10977}
10978
962a48f6
DS
10979// ----------------------------------------------------------------------------
10980// grid event classes
10981// ----------------------------------------------------------------------------
f85afd4e 10982
bf7945ce 10983IMPLEMENT_DYNAMIC_CLASS( wxGridEvent, wxNotifyEvent )
f85afd4e
MB
10984
10985wxGridEvent::wxGridEvent( int id, wxEventType type, wxObject* obj,
5c8fc7c1 10986 int row, int col, int x, int y, bool sel,
f85afd4e
MB
10987 bool control, bool shift, bool alt, bool meta )
10988 : wxNotifyEvent( type, id )
10989{
10990 m_row = row;
10991 m_col = col;
10992 m_x = x;
10993 m_y = y;
5c8fc7c1 10994 m_selecting = sel;
f85afd4e
MB
10995 m_control = control;
10996 m_shift = shift;
10997 m_alt = alt;
10998 m_meta = meta;
8f177c8e 10999
f85afd4e
MB
11000 SetEventObject(obj);
11001}
11002
11003
bf7945ce 11004IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent, wxNotifyEvent )
f85afd4e
MB
11005
11006wxGridSizeEvent::wxGridSizeEvent( int id, wxEventType type, wxObject* obj,
11007 int rowOrCol, int x, int y,
11008 bool control, bool shift, bool alt, bool meta )
11009 : wxNotifyEvent( type, id )
11010{
11011 m_rowOrCol = rowOrCol;
11012 m_x = x;
11013 m_y = y;
11014 m_control = control;
11015 m_shift = shift;
11016 m_alt = alt;
11017 m_meta = meta;
8f177c8e 11018
f85afd4e
MB
11019 SetEventObject(obj);
11020}
11021
11022
bf7945ce 11023IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent, wxNotifyEvent )
f85afd4e
MB
11024
11025wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id, wxEventType type, wxObject* obj,
8f177c8e
VZ
11026 const wxGridCellCoords& topLeft,
11027 const wxGridCellCoords& bottomRight,
5c8fc7c1
SN
11028 bool sel, bool control,
11029 bool shift, bool alt, bool meta )
8f177c8e 11030 : wxNotifyEvent( type, id )
f85afd4e 11031{
2f024384 11032 m_topLeft = topLeft;
f85afd4e 11033 m_bottomRight = bottomRight;
2f024384
DS
11034 m_selecting = sel;
11035 m_control = control;
11036 m_shift = shift;
11037 m_alt = alt;
11038 m_meta = meta;
f85afd4e
MB
11039
11040 SetEventObject(obj);
11041}
11042
11043
bf7945ce
RD
11044IMPLEMENT_DYNAMIC_CLASS(wxGridEditorCreatedEvent, wxCommandEvent)
11045
11046wxGridEditorCreatedEvent::wxGridEditorCreatedEvent(int id, wxEventType type,
11047 wxObject* obj, int row,
11048 int col, wxControl* ctrl)
11049 : wxCommandEvent(type, id)
11050{
11051 SetEventObject(obj);
11052 m_row = row;
11053 m_col = col;
11054 m_ctrl = ctrl;
11055}
11056
27b92ca4 11057#endif // wxUSE_GRID