]> git.saurik.com Git - wxWidgets.git/blame - src/generic/grid.cpp
we are always compositing on the supported OS versions
[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 );
4db6714b 202 ~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
MB
3738 m_owner->CalcUnscrolledPosition( 0, 0, &x, &y );
3739 dc.SetDeviceOrigin( 0, -y );
60ff3b99 3740
d10f4bf9 3741 wxArrayInt rows = m_owner->CalcRowLabelsExposed( GetUpdateRegion() );
a9339fe2 3742 m_owner->DrawRowLabels( dc, rows );
2d66e025
MB
3743}
3744
2d66e025
MB
3745void wxGridRowLabelWindow::OnMouseEvent( wxMouseEvent& event )
3746{
3747 m_owner->ProcessRowLabelMouseEvent( event );
3748}
3749
b51c3f27
RD
3750void wxGridRowLabelWindow::OnMouseWheel( wxMouseEvent& event )
3751{
a9339fe2 3752 m_owner->GetEventHandler()->ProcessEvent( event );
b51c3f27
RD
3753}
3754
2d66e025
MB
3755// This seems to be required for wxMotif otherwise the mouse
3756// cursor must be in the cell edit control to get key events
3757//
3758void wxGridRowLabelWindow::OnKeyDown( wxKeyEvent& event )
3759{
2f024384
DS
3760 if ( !m_owner->GetEventHandler()->ProcessEvent( event ) )
3761 event.Skip();
2d66e025
MB
3762}
3763
f6bcfd97
BP
3764void wxGridRowLabelWindow::OnKeyUp( wxKeyEvent& event )
3765{
2f024384
DS
3766 if ( !m_owner->GetEventHandler()->ProcessEvent( event ) )
3767 event.Skip();
f6bcfd97
BP
3768}
3769
63e2147c
RD
3770void wxGridRowLabelWindow::OnChar( wxKeyEvent& event )
3771{
2f024384
DS
3772 if ( !m_owner->GetEventHandler()->ProcessEvent( event ) )
3773 event.Skip();
63e2147c
RD
3774}
3775
2d66e025
MB
3776//////////////////////////////////////////////////////////////////////
3777
3778IMPLEMENT_DYNAMIC_CLASS( wxGridColLabelWindow, wxWindow )
3779
3780BEGIN_EVENT_TABLE( wxGridColLabelWindow, wxWindow )
3781 EVT_PAINT( wxGridColLabelWindow::OnPaint )
a9339fe2 3782 EVT_MOUSEWHEEL( wxGridColLabelWindow::OnMouseWheel )
2d66e025
MB
3783 EVT_MOUSE_EVENTS( wxGridColLabelWindow::OnMouseEvent )
3784 EVT_KEY_DOWN( wxGridColLabelWindow::OnKeyDown )
f6bcfd97 3785 EVT_KEY_UP( wxGridColLabelWindow::OnKeyUp )
a9339fe2 3786 EVT_CHAR( wxGridColLabelWindow::OnChar )
2d66e025
MB
3787END_EVENT_TABLE()
3788
60ff3b99
VZ
3789wxGridColLabelWindow::wxGridColLabelWindow( wxGrid *parent,
3790 wxWindowID id,
2d66e025 3791 const wxPoint &pos, const wxSize &size )
a9339fe2 3792 : wxWindow( parent, id, pos, size, wxWANTS_CHARS | wxBORDER_NONE | wxFULL_REPAINT_ON_RESIZE )
2d66e025
MB
3793{
3794 m_owner = parent;
3795}
3796
aa5e1f75 3797void wxGridColLabelWindow::OnPaint( wxPaintEvent& WXUNUSED(event) )
2d66e025
MB
3798{
3799 wxPaintDC dc(this);
3800
3801 // NO - don't do this because it will set both the x and y origin
3802 // coords to match the parent scrolled window and we just want to
3803 // set the x coord - MB
3804 //
3805 // m_owner->PrepareDC( dc );
60ff3b99 3806
790ad94f 3807 int x, y;
2d66e025
MB
3808 m_owner->CalcUnscrolledPosition( 0, 0, &x, &y );
3809 dc.SetDeviceOrigin( -x, 0 );
3810
d10f4bf9 3811 wxArrayInt cols = m_owner->CalcColLabelsExposed( GetUpdateRegion() );
a9339fe2 3812 m_owner->DrawColLabels( dc, cols );
2d66e025
MB
3813}
3814
2d66e025
MB
3815void wxGridColLabelWindow::OnMouseEvent( wxMouseEvent& event )
3816{
3817 m_owner->ProcessColLabelMouseEvent( event );
3818}
3819
b51c3f27
RD
3820void wxGridColLabelWindow::OnMouseWheel( wxMouseEvent& event )
3821{
a9339fe2 3822 m_owner->GetEventHandler()->ProcessEvent( event );
b51c3f27
RD
3823}
3824
2d66e025
MB
3825// This seems to be required for wxMotif otherwise the mouse
3826// cursor must be in the cell edit control to get key events
3827//
3828void wxGridColLabelWindow::OnKeyDown( wxKeyEvent& event )
3829{
4db6714b
KH
3830 if ( !m_owner->GetEventHandler()->ProcessEvent( event ) )
3831 event.Skip();
2d66e025
MB
3832}
3833
f6bcfd97
BP
3834void wxGridColLabelWindow::OnKeyUp( wxKeyEvent& event )
3835{
4db6714b
KH
3836 if ( !m_owner->GetEventHandler()->ProcessEvent( event ) )
3837 event.Skip();
f6bcfd97
BP
3838}
3839
63e2147c
RD
3840void wxGridColLabelWindow::OnChar( wxKeyEvent& event )
3841{
4db6714b
KH
3842 if ( !m_owner->GetEventHandler()->ProcessEvent( event ) )
3843 event.Skip();
63e2147c 3844}
2d66e025 3845
2d66e025
MB
3846//////////////////////////////////////////////////////////////////////
3847
3848IMPLEMENT_DYNAMIC_CLASS( wxGridCornerLabelWindow, wxWindow )
3849
3850BEGIN_EVENT_TABLE( wxGridCornerLabelWindow, wxWindow )
a9339fe2 3851 EVT_MOUSEWHEEL( wxGridCornerLabelWindow::OnMouseWheel )
2d66e025 3852 EVT_MOUSE_EVENTS( wxGridCornerLabelWindow::OnMouseEvent )
a9339fe2 3853 EVT_PAINT( wxGridCornerLabelWindow::OnPaint )
2d66e025 3854 EVT_KEY_DOWN( wxGridCornerLabelWindow::OnKeyDown )
f6bcfd97 3855 EVT_KEY_UP( wxGridCornerLabelWindow::OnKeyUp )
a9339fe2 3856 EVT_CHAR( wxGridCornerLabelWindow::OnChar )
2d66e025
MB
3857END_EVENT_TABLE()
3858
60ff3b99
VZ
3859wxGridCornerLabelWindow::wxGridCornerLabelWindow( wxGrid *parent,
3860 wxWindowID id,
2d66e025 3861 const wxPoint &pos, const wxSize &size )
56b6cf26 3862 : wxWindow( parent, id, pos, size, wxWANTS_CHARS | wxBORDER_NONE | wxFULL_REPAINT_ON_RESIZE )
2d66e025
MB
3863{
3864 m_owner = parent;
3865}
3866
d2fdd8d2
RR
3867void wxGridCornerLabelWindow::OnPaint( wxPaintEvent& WXUNUSED(event) )
3868{
3869 wxPaintDC dc(this);
b99be8fb 3870
d2fdd8d2
RR
3871 int client_height = 0;
3872 int client_width = 0;
3873 GetClientSize( &client_width, &client_height );
b99be8fb 3874
11850ff3
VZ
3875 // VZ: any reason for this ifdef? (FIXME)
3876#ifdef __WXGTK__
4d1bc39c
RR
3877 wxRect rect;
3878 rect.SetX( 1 );
3879 rect.SetY( 1 );
3880 rect.SetWidth( client_width - 2 );
3881 rect.SetHeight( client_height - 2 );
ec157c8f 3882
4d1bc39c 3883 wxRendererNative::Get().DrawHeaderButton( this, dc, rect, 0 );
11850ff3 3884#else // !__WXGTK__
d4175745 3885 dc.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW), 1, wxSOLID) );
ccdee36f
DS
3886 dc.DrawLine( client_width - 1, client_height - 1, client_width - 1, 0 );
3887 dc.DrawLine( client_width - 1, client_height - 1, 0, client_height - 1 );
d2fdd8d2
RR
3888 dc.DrawLine( 0, 0, client_width, 0 );
3889 dc.DrawLine( 0, 0, 0, client_height );
73145b0e
JS
3890
3891 dc.SetPen( *wxWHITE_PEN );
ccdee36f
DS
3892 dc.DrawLine( 1, 1, client_width - 1, 1 );
3893 dc.DrawLine( 1, 1, 1, client_height - 1 );
3894#endif
d2fdd8d2
RR
3895}
3896
2d66e025
MB
3897void wxGridCornerLabelWindow::OnMouseEvent( wxMouseEvent& event )
3898{
3899 m_owner->ProcessCornerLabelMouseEvent( event );
3900}
3901
b51c3f27
RD
3902void wxGridCornerLabelWindow::OnMouseWheel( wxMouseEvent& event )
3903{
3904 m_owner->GetEventHandler()->ProcessEvent(event);
3905}
3906
2d66e025
MB
3907// This seems to be required for wxMotif otherwise the mouse
3908// cursor must be in the cell edit control to get key events
3909//
3910void wxGridCornerLabelWindow::OnKeyDown( wxKeyEvent& event )
3911{
2f024384
DS
3912 if ( !m_owner->GetEventHandler()->ProcessEvent( event ) )
3913 event.Skip();
2d66e025
MB
3914}
3915
f6bcfd97
BP
3916void wxGridCornerLabelWindow::OnKeyUp( wxKeyEvent& event )
3917{
2f024384
DS
3918 if ( !m_owner->GetEventHandler()->ProcessEvent( event ) )
3919 event.Skip();
f6bcfd97
BP
3920}
3921
63e2147c
RD
3922void wxGridCornerLabelWindow::OnChar( wxKeyEvent& event )
3923{
2f024384
DS
3924 if ( !m_owner->GetEventHandler()->ProcessEvent( event ) )
3925 event.Skip();
63e2147c 3926}
2d66e025 3927
f85afd4e
MB
3928//////////////////////////////////////////////////////////////////////
3929
59ddac01 3930IMPLEMENT_DYNAMIC_CLASS( wxGridWindow, wxWindow )
2d66e025 3931
59ddac01 3932BEGIN_EVENT_TABLE( wxGridWindow, wxWindow )
2d66e025 3933 EVT_PAINT( wxGridWindow::OnPaint )
a9339fe2 3934 EVT_MOUSEWHEEL( wxGridWindow::OnMouseWheel )
2d66e025
MB
3935 EVT_MOUSE_EVENTS( wxGridWindow::OnMouseEvent )
3936 EVT_KEY_DOWN( wxGridWindow::OnKeyDown )
f6bcfd97 3937 EVT_KEY_UP( wxGridWindow::OnKeyUp )
a9339fe2 3938 EVT_CHAR( wxGridWindow::OnChar )
80acaf25
JS
3939 EVT_SET_FOCUS( wxGridWindow::OnFocus )
3940 EVT_KILL_FOCUS( wxGridWindow::OnFocus )
2796cce3 3941 EVT_ERASE_BACKGROUND( wxGridWindow::OnEraseBackground )
2d66e025
MB
3942END_EVENT_TABLE()
3943
60ff3b99
VZ
3944wxGridWindow::wxGridWindow( wxGrid *parent,
3945 wxGridRowLabelWindow *rowLblWin,
2d66e025 3946 wxGridColLabelWindow *colLblWin,
04418332
VZ
3947 wxWindowID id,
3948 const wxPoint &pos,
3949 const wxSize &size )
ccdee36f
DS
3950 : wxWindow(
3951 parent, id, pos, size,
3952 wxWANTS_CHARS | wxBORDER_NONE | wxCLIP_CHILDREN | wxFULL_REPAINT_ON_RESIZE,
3953 wxT("grid window") )
2d66e025
MB
3954{
3955 m_owner = parent;
3956 m_rowLabelWin = rowLblWin;
3957 m_colLabelWin = colLblWin;
2d66e025
MB
3958}
3959
2d66e025
MB
3960void wxGridWindow::OnPaint( wxPaintEvent &WXUNUSED(event) )
3961{
3962 wxPaintDC dc( this );
3963 m_owner->PrepareDC( dc );
796df70a 3964 wxRegion reg = GetUpdateRegion();
ccdee36f
DS
3965 wxGridCellCoordsArray dirtyCells = m_owner->CalcCellsExposed( reg );
3966 m_owner->DrawGridCellArea( dc, dirtyCells );
2f024384 3967
9496deb5 3968#if WXGRID_DRAW_LINES
796df70a
SN
3969 m_owner->DrawAllGridLines( dc, reg );
3970#endif
2f024384 3971
a5777624 3972 m_owner->DrawGridSpace( dc );
ccdee36f 3973 m_owner->DrawHighlight( dc, dirtyCells );
2d66e025
MB
3974}
3975
2d66e025
MB
3976void wxGridWindow::ScrollWindow( int dx, int dy, const wxRect *rect )
3977{
59ddac01 3978 wxWindow::ScrollWindow( dx, dy, rect );
2d66e025
MB
3979 m_rowLabelWin->ScrollWindow( 0, dy, rect );
3980 m_colLabelWin->ScrollWindow( dx, 0, rect );
3981}
3982
2d66e025
MB
3983void wxGridWindow::OnMouseEvent( wxMouseEvent& event )
3984{
33e9fc54
RD
3985 if (event.ButtonDown(wxMOUSE_BTN_LEFT) && FindFocus() != this)
3986 SetFocus();
902725ee 3987
2d66e025
MB
3988 m_owner->ProcessGridCellMouseEvent( event );
3989}
3990
b51c3f27
RD
3991void wxGridWindow::OnMouseWheel( wxMouseEvent& event )
3992{
a9339fe2 3993 m_owner->GetEventHandler()->ProcessEvent( event );
b51c3f27 3994}
2d66e025 3995
f6bcfd97 3996// This seems to be required for wxMotif/wxGTK otherwise the mouse
2d66e025
MB
3997// cursor must be in the cell edit control to get key events
3998//
3999void wxGridWindow::OnKeyDown( wxKeyEvent& event )
4000{
4db6714b
KH
4001 if ( !m_owner->GetEventHandler()->ProcessEvent( event ) )
4002 event.Skip();
2d66e025 4003}
f85afd4e 4004
f6bcfd97
BP
4005void wxGridWindow::OnKeyUp( wxKeyEvent& event )
4006{
4db6714b
KH
4007 if ( !m_owner->GetEventHandler()->ProcessEvent( event ) )
4008 event.Skip();
f6bcfd97 4009}
7c8a8ad5 4010
63e2147c
RD
4011void wxGridWindow::OnChar( wxKeyEvent& event )
4012{
4db6714b
KH
4013 if ( !m_owner->GetEventHandler()->ProcessEvent( event ) )
4014 event.Skip();
63e2147c
RD
4015}
4016
aa5e1f75 4017void wxGridWindow::OnEraseBackground( wxEraseEvent& WXUNUSED(event) )
8dd4f536 4018{
8dd4f536 4019}
025562fe 4020
80acaf25
JS
4021void wxGridWindow::OnFocus(wxFocusEvent& event)
4022{
4023 if ( !m_owner->GetEventHandler()->ProcessEvent( event ) )
4024 event.Skip();
4025}
2d66e025
MB
4026
4027//////////////////////////////////////////////////////////////////////
4028
33188aa4
SN
4029// Internal Helper function for computing row or column from some
4030// (unscrolled) coordinate value, using either
70e8d961 4031// m_defaultRowHeight/m_defaultColWidth or binary search on array
33188aa4
SN
4032// of m_rowBottoms/m_ColRights to speed up the search!
4033
4034// Internal helper macros for simpler use of that function
4035
4036static int CoordToRowOrCol(int coord, int defaultDist, int minDist,
64e15340 4037 const wxArrayInt& BorderArray, int nMax,
a967f048 4038 bool clipToMinMax);
33188aa4 4039
d4175745 4040#define internalXToCol(x) XToCol(x, true)
33188aa4 4041#define internalYToRow(y) CoordToRowOrCol(y, m_defaultRowHeight, \
b8d24d4e 4042 m_minAcceptableRowHeight, \
ca65c044 4043 m_rowBottoms, m_numRows, true)
ccdee36f 4044
33188aa4 4045/////////////////////////////////////////////////////////////////////
07296f0b 4046
b0a877ec 4047#if wxUSE_EXTENDED_RTTI
73c36334
JS
4048WX_DEFINE_FLAGS( wxGridStyle )
4049
3ff066a4 4050wxBEGIN_FLAGS( wxGridStyle )
73c36334
JS
4051 // new style border flags, we put them first to
4052 // use them for streaming out
3ff066a4
SC
4053 wxFLAGS_MEMBER(wxBORDER_SIMPLE)
4054 wxFLAGS_MEMBER(wxBORDER_SUNKEN)
4055 wxFLAGS_MEMBER(wxBORDER_DOUBLE)
4056 wxFLAGS_MEMBER(wxBORDER_RAISED)
4057 wxFLAGS_MEMBER(wxBORDER_STATIC)
4058 wxFLAGS_MEMBER(wxBORDER_NONE)
ca65c044 4059
73c36334 4060 // old style border flags
3ff066a4
SC
4061 wxFLAGS_MEMBER(wxSIMPLE_BORDER)
4062 wxFLAGS_MEMBER(wxSUNKEN_BORDER)
4063 wxFLAGS_MEMBER(wxDOUBLE_BORDER)
4064 wxFLAGS_MEMBER(wxRAISED_BORDER)
4065 wxFLAGS_MEMBER(wxSTATIC_BORDER)
cb0afb26 4066 wxFLAGS_MEMBER(wxBORDER)
73c36334
JS
4067
4068 // standard window styles
3ff066a4
SC
4069 wxFLAGS_MEMBER(wxTAB_TRAVERSAL)
4070 wxFLAGS_MEMBER(wxCLIP_CHILDREN)
4071 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW)
4072 wxFLAGS_MEMBER(wxWANTS_CHARS)
cb0afb26 4073 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE)
ccdee36f 4074 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB)
3ff066a4
SC
4075 wxFLAGS_MEMBER(wxVSCROLL)
4076 wxFLAGS_MEMBER(wxHSCROLL)
73c36334 4077
3ff066a4 4078wxEND_FLAGS( wxGridStyle )
73c36334 4079
b0a877ec
SC
4080IMPLEMENT_DYNAMIC_CLASS_XTI(wxGrid, wxScrolledWindow,"wx/grid.h")
4081
3ff066a4
SC
4082wxBEGIN_PROPERTIES_TABLE(wxGrid)
4083 wxHIDE_PROPERTY( Children )
af498247 4084 wxPROPERTY_FLAGS( WindowStyle , wxGridStyle , long , SetWindowStyleFlag , GetWindowStyleFlag , EMPTY_MACROVALUE, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
3ff066a4 4085wxEND_PROPERTIES_TABLE()
b0a877ec 4086
3ff066a4
SC
4087wxBEGIN_HANDLERS_TABLE(wxGrid)
4088wxEND_HANDLERS_TABLE()
b0a877ec 4089
ca65c044 4090wxCONSTRUCTOR_5( wxGrid , wxWindow* , Parent , wxWindowID , Id , wxPoint , Position , wxSize , Size , long , WindowStyle )
b0a877ec
SC
4091
4092/*
ccdee36f 4093 TODO : Expose more information of a list's layout, etc. via appropriate objects (e.g., NotebookPageInfo)
b0a877ec
SC
4094*/
4095#else
2d66e025 4096IMPLEMENT_DYNAMIC_CLASS( wxGrid, wxScrolledWindow )
b0a877ec 4097#endif
2d66e025
MB
4098
4099BEGIN_EVENT_TABLE( wxGrid, wxScrolledWindow )
f85afd4e
MB
4100 EVT_PAINT( wxGrid::OnPaint )
4101 EVT_SIZE( wxGrid::OnSize )
f85afd4e 4102 EVT_KEY_DOWN( wxGrid::OnKeyDown )
f6bcfd97 4103 EVT_KEY_UP( wxGrid::OnKeyUp )
63e2147c 4104 EVT_CHAR ( wxGrid::OnChar )
2796cce3 4105 EVT_ERASE_BACKGROUND( wxGrid::OnEraseBackground )
f85afd4e 4106END_EVENT_TABLE()
8f177c8e 4107
b0a877ec
SC
4108wxGrid::wxGrid()
4109{
4110 // in order to make sure that a size event is not
4111 // trigerred in a unfinished state
2f024384
DS
4112 m_cornerLabelWin = NULL;
4113 m_rowLabelWin = NULL;
4114 m_colLabelWin = NULL;
4115 m_gridWin = NULL;
b0a877ec
SC
4116}
4117
2d66e025
MB
4118wxGrid::wxGrid( wxWindow *parent,
4119 wxWindowID id,
4120 const wxPoint& pos,
4121 const wxSize& size,
4122 long style,
4123 const wxString& name )
ebd773c6 4124 : wxScrolledWindow( parent, id, pos, size, (style | wxWANTS_CHARS), name ),
af547d51
VZ
4125 m_colMinWidths(GRID_HASH_SIZE),
4126 m_rowMinHeights(GRID_HASH_SIZE)
2d66e025
MB
4127{
4128 Create();
5a9b107d 4129 SetBestFittingSize(size);
58dd5b3b
MB
4130}
4131
b0a877ec
SC
4132bool wxGrid::Create(wxWindow *parent, wxWindowID id,
4133 const wxPoint& pos, const wxSize& size,
4134 long style, const wxString& name)
4135{
4136 if (!wxScrolledWindow::Create(parent, id, pos, size,
c2f5b920 4137 style | wxWANTS_CHARS, name))
ca65c044 4138 return false;
b0a877ec 4139
2f024384
DS
4140 m_colMinWidths = wxLongToLongHashMap(GRID_HASH_SIZE);
4141 m_rowMinHeights = wxLongToLongHashMap(GRID_HASH_SIZE);
b0a877ec 4142
2f024384 4143 Create();
5a9b107d 4144 SetBestFittingSize(size);
b0a877ec 4145
ca65c044 4146 return true;
b0a877ec
SC
4147}
4148
58dd5b3b
MB
4149wxGrid::~wxGrid()
4150{
606b005f
JS
4151 // Must do this or ~wxScrollHelper will pop the wrong event handler
4152 SetTargetWindow(this);
0a976765 4153 ClearAttrCache();
39bcce60 4154 wxSafeDecRef(m_defaultCellAttr);
0a976765
VZ
4155
4156#ifdef DEBUG_ATTR_CACHE
4157 size_t total = gs_nAttrCacheHits + gs_nAttrCacheMisses;
4158 wxPrintf(_T("wxGrid attribute cache statistics: "
4159 "total: %u, hits: %u (%u%%)\n"),
4160 total, gs_nAttrCacheHits,
4161 total ? (gs_nAttrCacheHits*100) / total : 0);
4162#endif
4163
2796cce3
RD
4164 if (m_ownTable)
4165 delete m_table;
f2d76237
RD
4166
4167 delete m_typeRegistry;
b5808881 4168 delete m_selection;
58dd5b3b
MB
4169}
4170
58dd5b3b
MB
4171//
4172// ----- internal init and update functions
4173//
4174
9950649c
RD
4175// NOTE: If using the default visual attributes works everywhere then this can
4176// be removed as well as the #else cases below.
4177#define _USE_VISATTR 0
4178
58dd5b3b 4179void wxGrid::Create()
f0102d2a 4180{
3d59537f
DS
4181 // set to true by CreateGrid
4182 m_created = false;
4634a5d6 4183
3d59537f
DS
4184 // create the type registry
4185 m_typeRegistry = new wxGridTypeRegistry;
4186 m_selection = NULL;
4187
4188 m_table = (wxGridTableBase *) NULL;
4189 m_ownTable = false;
2c9a89e0 4190
ca65c044 4191 m_cellEditCtrlEnabled = false;
4634a5d6 4192
ccd970b1 4193 m_defaultCellAttr = new wxGridCellAttr();
f2d76237
RD
4194
4195 // Set default cell attributes
ccd970b1 4196 m_defaultCellAttr->SetDefAttr(m_defaultCellAttr);
19d7140e 4197 m_defaultCellAttr->SetKind(wxGridCellAttr::Default);
f2d76237 4198 m_defaultCellAttr->SetFont(GetFont());
4c7277db 4199 m_defaultCellAttr->SetAlignment(wxALIGN_LEFT, wxALIGN_TOP);
9950649c
RD
4200 m_defaultCellAttr->SetRenderer(new wxGridCellStringRenderer);
4201 m_defaultCellAttr->SetEditor(new wxGridCellTextEditor);
4202
4203#if _USE_VISATTR
4204 wxVisualAttributes gva = wxListBox::GetClassDefaultAttributes();
4205 wxVisualAttributes lva = wxPanel::GetClassDefaultAttributes();
4206
4207 m_defaultCellAttr->SetTextColour(gva.colFg);
4208 m_defaultCellAttr->SetBackgroundColour(gva.colBg);
ca65c044 4209
9950649c 4210#else
f2d76237 4211 m_defaultCellAttr->SetTextColour(
a756f210 4212 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT));
f2d76237 4213 m_defaultCellAttr->SetBackgroundColour(
a756f210 4214 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW));
9950649c 4215#endif
2796cce3 4216
4634a5d6
MB
4217 m_numRows = 0;
4218 m_numCols = 0;
4219 m_currentCellCoords = wxGridNoCellCoords;
b99be8fb 4220
18f9565d
MB
4221 m_rowLabelWidth = WXGRID_DEFAULT_ROW_LABEL_WIDTH;
4222 m_colLabelHeight = WXGRID_DEFAULT_COL_LABEL_HEIGHT;
2d66e025 4223
f2d76237 4224 // subwindow components that make up the wxGrid
60ff3b99 4225 m_rowLabelWin = new wxGridRowLabelWindow( this,
ca65c044 4226 wxID_ANY,
18f9565d
MB
4227 wxDefaultPosition,
4228 wxDefaultSize );
2d66e025
MB
4229
4230 m_colLabelWin = new wxGridColLabelWindow( this,
ca65c044 4231 wxID_ANY,
18f9565d
MB
4232 wxDefaultPosition,
4233 wxDefaultSize );
60ff3b99 4234
3d59537f
DS
4235 m_cornerLabelWin = new wxGridCornerLabelWindow( this,
4236 wxID_ANY,
4237 wxDefaultPosition,
4238 wxDefaultSize );
4239
60ff3b99
VZ
4240 m_gridWin = new wxGridWindow( this,
4241 m_rowLabelWin,
4242 m_colLabelWin,
ca65c044 4243 wxID_ANY,
18f9565d 4244 wxDefaultPosition,
2d66e025
MB
4245 wxDefaultSize );
4246
4247 SetTargetWindow( m_gridWin );
6f36917b 4248
9950649c
RD
4249#if _USE_VISATTR
4250 wxColour gfg = gva.colFg;
4251 wxColour gbg = gva.colBg;
4252 wxColour lfg = lva.colFg;
4253 wxColour lbg = lva.colBg;
4254#else
4255 wxColour gfg = wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT );
4256 wxColour gbg = wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOW );
4257 wxColour lfg = wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT );
4258 wxColour lbg = wxSystemSettings::GetColour( wxSYS_COLOUR_BTNFACE );
4259#endif
2f024384 4260
fa47d7a7
VS
4261 m_cornerLabelWin->SetOwnForegroundColour(lfg);
4262 m_cornerLabelWin->SetOwnBackgroundColour(lbg);
4263 m_rowLabelWin->SetOwnForegroundColour(lfg);
4264 m_rowLabelWin->SetOwnBackgroundColour(lbg);
4265 m_colLabelWin->SetOwnForegroundColour(lfg);
4266 m_colLabelWin->SetOwnBackgroundColour(lbg);
4267
4268 m_gridWin->SetOwnForegroundColour(gfg);
4269 m_gridWin->SetOwnBackgroundColour(gbg);
ca65c044 4270
6f36917b 4271 Init();
2d66e025 4272}
f85afd4e 4273
b5808881
SN
4274bool wxGrid::CreateGrid( int numRows, int numCols,
4275 wxGrid::wxGridSelectionModes selmode )
2d66e025 4276{
f6bcfd97 4277 wxCHECK_MSG( !m_created,
ca65c044 4278 false,
f6bcfd97
BP
4279 wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
4280
4281 m_numRows = numRows;
4282 m_numCols = numCols;
4283
4284 m_table = new wxGridStringTable( m_numRows, m_numCols );
4285 m_table->SetView( this );
ca65c044 4286 m_ownTable = true;
f6bcfd97 4287 m_selection = new wxGridSelection( this, selmode );
6f36917b
VZ
4288
4289 CalcDimensions();
4290
ca65c044 4291 m_created = true;
2d66e025 4292
2796cce3
RD
4293 return m_created;
4294}
4295
f1567cdd
SN
4296void wxGrid::SetSelectionMode(wxGrid::wxGridSelectionModes selmode)
4297{
6f36917b
VZ
4298 wxCHECK_RET( m_created,
4299 wxT("Called wxGrid::SetSelectionMode() before calling CreateGrid()") );
4300
4301 m_selection->SetSelectionMode( selmode );
f1567cdd
SN
4302}
4303
aa5b8857
SN
4304wxGrid::wxGridSelectionModes wxGrid::GetSelectionMode() const
4305{
4306 wxCHECK_MSG( m_created, wxGrid::wxGridSelectCells,
4307 wxT("Called wxGrid::GetSelectionMode() before calling CreateGrid()") );
4308
4309 return m_selection->GetSelectionMode();
4310}
4311
043d16b2
SN
4312bool wxGrid::SetTable( wxGridTableBase *table, bool takeOwnership,
4313 wxGrid::wxGridSelectionModes selmode )
2796cce3
RD
4314{
4315 if ( m_created )
4316 {
3e13956a 4317 // stop all processing
ca65c044 4318 m_created = false;
86c7378f
SN
4319
4320 if (m_ownTable)
4321 {
5b061713
DS
4322 wxGridTableBase *t = m_table;
4323 m_table = NULL;
3e13956a 4324 delete t;
86c7378f 4325 }
2f024384 4326
3e13956a
RD
4327 delete m_selection;
4328
5b061713
DS
4329 m_table = NULL;
4330 m_selection = NULL;
2f024384
DS
4331 m_numRows = 0;
4332 m_numCols = 0;
233a54f6 4333 }
2f024384 4334
233a54f6 4335 if (table)
2796cce3
RD
4336 {
4337 m_numRows = table->GetNumberRows();
4338 m_numCols = table->GetNumberCols();
4339
4340 m_table = table;
4341 m_table->SetView( this );
8fc856de 4342 m_ownTable = takeOwnership;
043d16b2 4343 m_selection = new wxGridSelection( this, selmode );
6f36917b
VZ
4344
4345 CalcDimensions();
4346
ca65c044 4347 m_created = true;
2d66e025 4348 }
f85afd4e 4349
2d66e025 4350 return m_created;
f85afd4e
MB
4351}
4352
4353void wxGrid::Init()
4354{
f85afd4e
MB
4355 m_rowLabelWidth = WXGRID_DEFAULT_ROW_LABEL_WIDTH;
4356 m_colLabelHeight = WXGRID_DEFAULT_COL_LABEL_HEIGHT;
4357
60ff3b99
VZ
4358 if ( m_rowLabelWin )
4359 {
4360 m_labelBackgroundColour = m_rowLabelWin->GetBackgroundColour();
4361 }
4362 else
4363 {
b0fa2187 4364 m_labelBackgroundColour = *wxWHITE;
60ff3b99
VZ
4365 }
4366
b0fa2187 4367 m_labelTextColour = *wxBLACK;
f85afd4e 4368
0a976765
VZ
4369 // init attr cache
4370 m_attrCache.row = -1;
2b5f62a0
VZ
4371 m_attrCache.col = -1;
4372 m_attrCache.attr = NULL;
0a976765 4373
f85afd4e
MB
4374 // TODO: something better than this ?
4375 //
4376 m_labelFont = this->GetFont();
52d6f640 4377 m_labelFont.SetWeight( wxBOLD );
8f177c8e 4378
73145b0e 4379 m_rowLabelHorizAlign = wxALIGN_CENTRE;
4c7277db 4380 m_rowLabelVertAlign = wxALIGN_CENTRE;
f85afd4e 4381
4c7277db 4382 m_colLabelHorizAlign = wxALIGN_CENTRE;
73145b0e 4383 m_colLabelVertAlign = wxALIGN_CENTRE;
d43851f7 4384 m_colLabelTextOrientation = wxHORIZONTAL;
f85afd4e 4385
f85afd4e 4386 m_defaultColWidth = WXGRID_DEFAULT_COL_WIDTH;
1f1ce288
MB
4387 m_defaultRowHeight = m_gridWin->GetCharHeight();
4388
b8d24d4e
RG
4389 m_minAcceptableColWidth = WXGRID_MIN_COL_WIDTH;
4390 m_minAcceptableRowHeight = WXGRID_MIN_ROW_HEIGHT;
4391
d2fdd8d2 4392#if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl()
1f1ce288
MB
4393 m_defaultRowHeight += 8;
4394#else
4395 m_defaultRowHeight += 4;
4396#endif
4397
73145b0e 4398 m_gridLineColour = wxColour( 192,192,192 );
ca65c044 4399 m_gridLinesEnabled = true;
73145b0e 4400 m_cellHighlightColour = *wxBLACK;
bf7945ce 4401 m_cellHighlightPenWidth = 2;
d2520c85 4402 m_cellHighlightROPenWidth = 1;
8f177c8e 4403
d4175745
VZ
4404 m_canDragColMove = false;
4405
58dd5b3b 4406 m_cursorMode = WXGRID_CURSOR_SELECT_CELL;
e2b42eeb 4407 m_winCapture = (wxWindow *)NULL;
ca65c044
WS
4408 m_canDragRowSize = true;
4409 m_canDragColSize = true;
4410 m_canDragGridSize = true;
79dbea21 4411 m_canDragCell = false;
f85afd4e
MB
4412 m_dragLastPos = -1;
4413 m_dragRowOrCol = -1;
ca65c044 4414 m_isDragging = false;
07296f0b 4415 m_startDragPos = wxDefaultPosition;
f85afd4e 4416
ca65c044 4417 m_waitForSlowClick = false;
025562fe 4418
f85afd4e
MB
4419 m_rowResizeCursor = wxCursor( wxCURSOR_SIZENS );
4420 m_colResizeCursor = wxCursor( wxCURSOR_SIZEWE );
4421
4422 m_currentCellCoords = wxGridNoCellCoords;
f85afd4e 4423
b524b5c6
VZ
4424 ClearSelection();
4425
d43851f7
JS
4426 m_selectionBackground = wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT);
4427 m_selectionForeground = wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT);
8f177c8e 4428
ca65c044 4429 m_editable = true; // default for whole grid
f85afd4e 4430
ca65c044 4431 m_inOnKeyDown = false;
2d66e025 4432 m_batchCount = 0;
3c79cf49 4433
266e8367 4434 m_extraWidth =
526dbb95 4435 m_extraHeight = 0;
608754c4
JS
4436
4437 m_scrollLineX = GRID_SCROLL_LINE_X;
4438 m_scrollLineY = GRID_SCROLL_LINE_Y;
7c1cb261
VZ
4439}
4440
4441// ----------------------------------------------------------------------------
4442// the idea is to call these functions only when necessary because they create
4443// quite big arrays which eat memory mostly unnecessary - in particular, if
4444// default widths/heights are used for all rows/columns, we may not use these
4445// arrays at all
4446//
4447// with some extra code, it should be possible to only store the
4448// widths/heights different from default ones but this will be done later...
4449// ----------------------------------------------------------------------------
4450
4451void wxGrid::InitRowHeights()
4452{
4453 m_rowHeights.Empty();
4454 m_rowBottoms.Empty();
4455
4456 m_rowHeights.Alloc( m_numRows );
4457 m_rowBottoms.Alloc( m_numRows );
4458
4459 int rowBottom = 0;
2b5f62a0 4460
27f35b66
SN
4461 m_rowHeights.Add( m_defaultRowHeight, m_numRows );
4462
3d59537f 4463 for ( int i = 0; i < m_numRows; i++ )
7c1cb261 4464 {
7c1cb261
VZ
4465 rowBottom += m_defaultRowHeight;
4466 m_rowBottoms.Add( rowBottom );
4467 }
4468}
4469
4470void wxGrid::InitColWidths()
4471{
4472 m_colWidths.Empty();
4473 m_colRights.Empty();
4474
4475 m_colWidths.Alloc( m_numCols );
4476 m_colRights.Alloc( m_numCols );
4477 int colRight = 0;
27f35b66
SN
4478
4479 m_colWidths.Add( m_defaultColWidth, m_numCols );
4480
3d59537f 4481 for ( int i = 0; i < m_numCols; i++ )
7c1cb261 4482 {
d4175745 4483 colRight = ( GetColPos( i ) + 1 ) * m_defaultColWidth;
7c1cb261
VZ
4484 m_colRights.Add( colRight );
4485 }
4486}
4487
4488int wxGrid::GetColWidth(int col) const
4489{
4490 return m_colWidths.IsEmpty() ? m_defaultColWidth : m_colWidths[col];
4491}
4492
4493int wxGrid::GetColLeft(int col) const
4494{
d4175745 4495 return m_colRights.IsEmpty() ? GetColPos( col ) * m_defaultColWidth
7c1cb261
VZ
4496 : m_colRights[col] - m_colWidths[col];
4497}
4498
4499int wxGrid::GetColRight(int col) const
4500{
d4175745 4501 return m_colRights.IsEmpty() ? (GetColPos( col ) + 1) * m_defaultColWidth
7c1cb261
VZ
4502 : m_colRights[col];
4503}
4504
4505int wxGrid::GetRowHeight(int row) const
4506{
4507 return m_rowHeights.IsEmpty() ? m_defaultRowHeight : m_rowHeights[row];
4508}
2d66e025 4509
7c1cb261
VZ
4510int wxGrid::GetRowTop(int row) const
4511{
4512 return m_rowBottoms.IsEmpty() ? row * m_defaultRowHeight
4513 : m_rowBottoms[row] - m_rowHeights[row];
f85afd4e
MB
4514}
4515
7c1cb261
VZ
4516int wxGrid::GetRowBottom(int row) const
4517{
4518 return m_rowBottoms.IsEmpty() ? (row + 1) * m_defaultRowHeight
4519 : m_rowBottoms[row];
4520}
f85afd4e
MB
4521
4522void wxGrid::CalcDimensions()
4523{
f85afd4e 4524 int cw, ch;
2d66e025 4525 GetClientSize( &cw, &ch );
f85afd4e 4526
faec5a43 4527 if ( m_rowLabelWin->IsShown() )
ae1d0c6c
VZ
4528 cw -= m_rowLabelWidth;
4529 if ( m_colLabelWin->IsShown() )
faec5a43 4530 ch -= m_colLabelHeight;
60ff3b99 4531
faec5a43 4532 // grid total size
d4175745 4533 int w = m_numCols > 0 ? GetColRight(GetColAt( m_numCols - 1 )) + m_extraWidth + 1 : 0;
faec5a43
SN
4534 int h = m_numRows > 0 ? GetRowBottom(m_numRows - 1) + m_extraHeight + 1 : 0;
4535
73145b0e 4536 // take into account editor if shown
4db6714b 4537 if ( IsCellEditControlShown() )
73145b0e 4538 {
3d59537f
DS
4539 int w2, h2;
4540 int r = m_currentCellCoords.GetRow();
4541 int c = m_currentCellCoords.GetCol();
4542 int x = GetColLeft(c);
4543 int y = GetRowTop(r);
4544
4545 // how big is the editor
4546 wxGridCellAttr* attr = GetCellAttr(r, c);
4547 wxGridCellEditor* editor = attr->GetEditor(this, r, c);
4548 editor->GetControl()->GetSize(&w2, &h2);
4549 w2 += x;
4550 h2 += y;
4551 if ( w2 > w )
4552 w = w2;
4553 if ( h2 > h )
4554 h = h2;
4555 editor->DecRef();
4556 attr->DecRef();
73145b0e
JS
4557 }
4558
faec5a43
SN
4559 // preserve (more or less) the previous position
4560 int x, y;
4561 GetViewStart( &x, &y );
97a9929e 4562
c92ed9f7 4563 // ensure the position is valid for the new scroll ranges
7b519e5e 4564 if ( x >= w )
c92ed9f7 4565 x = wxMax( w - 1, 0 );
7b519e5e 4566 if ( y >= h )
c92ed9f7 4567 y = wxMax( h - 1, 0 );
faec5a43
SN
4568
4569 // do set scrollbar parameters
675a9c0d 4570 SetScrollbars( m_scrollLineX, m_scrollLineY,
97a9929e
VZ
4571 GetScrollX(w), GetScrollY(h), x, y,
4572 GetBatchCount() != 0);
12314291
VZ
4573
4574 // if our OnSize() hadn't been called (it would if we have scrollbars), we
4575 // still must reposition the children
4576 CalcWindowSizes();
f85afd4e
MB
4577}
4578
7807d81c
MB
4579void wxGrid::CalcWindowSizes()
4580{
b0a877ec
SC
4581 // escape if the window is has not been fully created yet
4582
4583 if ( m_cornerLabelWin == NULL )
2f024384 4584 return;
b0a877ec 4585
7807d81c
MB
4586 int cw, ch;
4587 GetClientSize( &cw, &ch );
b99be8fb 4588
6d308072 4589 if ( m_cornerLabelWin && m_cornerLabelWin->IsShown() )
7807d81c
MB
4590 m_cornerLabelWin->SetSize( 0, 0, m_rowLabelWidth, m_colLabelHeight );
4591
c2f5b920
DS
4592 if ( m_colLabelWin && m_colLabelWin->IsShown() )
4593 m_colLabelWin->SetSize( m_rowLabelWidth, 0, cw - m_rowLabelWidth, m_colLabelHeight );
7807d81c 4594
6d308072 4595 if ( m_rowLabelWin && m_rowLabelWin->IsShown() )
c2f5b920 4596 m_rowLabelWin->SetSize( 0, m_colLabelHeight, m_rowLabelWidth, ch - m_colLabelHeight );
7807d81c 4597
6d308072 4598 if ( m_gridWin && m_gridWin->IsShown() )
c2f5b920 4599 m_gridWin->SetSize( m_rowLabelWidth, m_colLabelHeight, cw - m_rowLabelWidth, ch - m_colLabelHeight );
7807d81c
MB
4600}
4601
3d59537f
DS
4602// this is called when the grid table sends a message
4603// to indicate that it has been redimensioned
f85afd4e
MB
4604//
4605bool wxGrid::Redimension( wxGridTableMessage& msg )
4606{
4607 int i;
ca65c044 4608 bool result = false;
8f177c8e 4609
a6794685
SN
4610 // Clear the attribute cache as the attribute might refer to a different
4611 // cell than stored in the cache after adding/removing rows/columns.
4612 ClearAttrCache();
2f024384 4613
7e48d7d9
SN
4614 // By the same reasoning, the editor should be dismissed if columns are
4615 // added or removed. And for consistency, it should IMHO always be
4616 // removed, not only if the cell "underneath" it actually changes.
4617 // For now, I intentionally do not save the editor's content as the
4618 // cell it might want to save that stuff to might no longer exist.
bca7bfc8 4619 HideCellEditControl();
2f024384 4620
f6bcfd97 4621#if 0
7c1cb261
VZ
4622 // if we were using the default widths/heights so far, we must change them
4623 // now
4624 if ( m_colWidths.IsEmpty() )
4625 {
4626 InitColWidths();
4627 }
4628
4629 if ( m_rowHeights.IsEmpty() )
4630 {
4631 InitRowHeights();
4632 }
f6bcfd97 4633#endif
7c1cb261 4634
f85afd4e
MB
4635 switch ( msg.GetId() )
4636 {
4637 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED:
4638 {
4639 size_t pos = msg.GetCommandInt();
4640 int numRows = msg.GetCommandInt2();
f6bcfd97 4641
f85afd4e 4642 m_numRows += numRows;
2d66e025 4643
f6bcfd97
BP
4644 if ( !m_rowHeights.IsEmpty() )
4645 {
27f35b66
SN
4646 m_rowHeights.Insert( m_defaultRowHeight, pos, numRows );
4647 m_rowBottoms.Insert( 0, pos, numRows );
f6bcfd97
BP
4648
4649 int bottom = 0;
2f024384
DS
4650 if ( pos > 0 )
4651 bottom = m_rowBottoms[pos - 1];
60ff3b99 4652
3d59537f 4653 for ( i = pos; i < m_numRows; i++ )
f6bcfd97
BP
4654 {
4655 bottom += m_rowHeights[i];
4656 m_rowBottoms[i] = bottom;
4657 }
4658 }
2f024384 4659
f6bcfd97
BP
4660 if ( m_currentCellCoords == wxGridNoCellCoords )
4661 {
4662 // if we have just inserted cols into an empty grid the current
4663 // cell will be undefined...
4664 //
4665 SetCurrentCell( 0, 0 );
4666 }
3f3dc2ef
VZ
4667
4668 if ( m_selection )
4669 m_selection->UpdateRows( pos, numRows );
f6bcfd97
BP
4670 wxGridCellAttrProvider * attrProvider = m_table->GetAttrProvider();
4671 if (attrProvider)
4672 attrProvider->UpdateAttrRows( pos, numRows );
4673
4674 if ( !GetBatchCount() )
2d66e025 4675 {
f6bcfd97
BP
4676 CalcDimensions();
4677 m_rowLabelWin->Refresh();
2d66e025 4678 }
f85afd4e 4679 }
ca65c044 4680 result = true;
f6bcfd97 4681 break;
f85afd4e
MB
4682
4683 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED:
4684 {
4685 int numRows = msg.GetCommandInt();
2d66e025 4686 int oldNumRows = m_numRows;
f85afd4e 4687 m_numRows += numRows;
2d66e025 4688
f6bcfd97
BP
4689 if ( !m_rowHeights.IsEmpty() )
4690 {
27f35b66
SN
4691 m_rowHeights.Add( m_defaultRowHeight, numRows );
4692 m_rowBottoms.Add( 0, numRows );
60ff3b99 4693
f6bcfd97 4694 int bottom = 0;
2f024384
DS
4695 if ( oldNumRows > 0 )
4696 bottom = m_rowBottoms[oldNumRows - 1];
f6bcfd97 4697
3d59537f 4698 for ( i = oldNumRows; i < m_numRows; i++ )
f6bcfd97
BP
4699 {
4700 bottom += m_rowHeights[i];
4701 m_rowBottoms[i] = bottom;
4702 }
4703 }
2f024384 4704
f6bcfd97
BP
4705 if ( m_currentCellCoords == wxGridNoCellCoords )
4706 {
4707 // if we have just inserted cols into an empty grid the current
4708 // cell will be undefined...
4709 //
4710 SetCurrentCell( 0, 0 );
4711 }
2f024384 4712
f6bcfd97 4713 if ( !GetBatchCount() )
2d66e025 4714 {
f6bcfd97
BP
4715 CalcDimensions();
4716 m_rowLabelWin->Refresh();
2d66e025 4717 }
f85afd4e 4718 }
ca65c044 4719 result = true;
f6bcfd97 4720 break;
f85afd4e
MB
4721
4722 case wxGRIDTABLE_NOTIFY_ROWS_DELETED:
4723 {
4724 size_t pos = msg.GetCommandInt();
4725 int numRows = msg.GetCommandInt2();
f85afd4e
MB
4726 m_numRows -= numRows;
4727
f6bcfd97 4728 if ( !m_rowHeights.IsEmpty() )
f85afd4e 4729 {
27f35b66
SN
4730 m_rowHeights.RemoveAt( pos, numRows );
4731 m_rowBottoms.RemoveAt( pos, numRows );
2d66e025
MB
4732
4733 int h = 0;
3d59537f 4734 for ( i = 0; i < m_numRows; i++ )
2d66e025
MB
4735 {
4736 h += m_rowHeights[i];
4737 m_rowBottoms[i] = h;
4738 }
f85afd4e 4739 }
3d59537f 4740
f6bcfd97
BP
4741 if ( !m_numRows )
4742 {
4743 m_currentCellCoords = wxGridNoCellCoords;
4744 }
4745 else
4746 {
4747 if ( m_currentCellCoords.GetRow() >= m_numRows )
4748 m_currentCellCoords.Set( 0, 0 );
4749 }
3f3dc2ef
VZ
4750
4751 if ( m_selection )
4752 m_selection->UpdateRows( pos, -((int)numRows) );
f6bcfd97 4753 wxGridCellAttrProvider * attrProvider = m_table->GetAttrProvider();
4db6714b
KH
4754 if (attrProvider)
4755 {
f6bcfd97 4756 attrProvider->UpdateAttrRows( pos, -((int)numRows) );
3d59537f 4757
84912ef8
RD
4758// ifdef'd out following patch from Paul Gammans
4759#if 0
3ca6a5f0 4760 // No need to touch column attributes, unless we
f6bcfd97
BP
4761 // removed _all_ rows, in this case, we remove
4762 // all column attributes.
4763 // I hate to do this here, but the
4764 // needed data is not available inside UpdateAttrRows.
4765 if ( !GetNumberRows() )
4766 attrProvider->UpdateAttrCols( 0, -GetNumberCols() );
84912ef8 4767#endif
f6bcfd97 4768 }
ccdee36f 4769
f6bcfd97
BP
4770 if ( !GetBatchCount() )
4771 {
4772 CalcDimensions();
4773 m_rowLabelWin->Refresh();
4774 }
f85afd4e 4775 }
ca65c044 4776 result = true;
f6bcfd97 4777 break;
f85afd4e
MB
4778
4779 case wxGRIDTABLE_NOTIFY_COLS_INSERTED:
4780 {
4781 size_t pos = msg.GetCommandInt();
4782 int numCols = msg.GetCommandInt2();
f85afd4e 4783 m_numCols += numCols;
2d66e025 4784
d4175745
VZ
4785 if ( !m_colAt.IsEmpty() )
4786 {
4787 //Shift the column IDs
4788 int i;
4789 for ( i = 0; i < m_numCols - numCols; i++ )
4790 {
4791 if ( m_colAt[i] >= (int)pos )
4792 m_colAt[i] += numCols;
4793 }
4794
4795 m_colAt.Insert( pos, pos, numCols );
4796
4797 //Set the new columns' positions
4798 for ( i = pos + 1; i < (int)pos + numCols; i++ )
4799 {
4800 m_colAt[i] = i;
4801 }
4802 }
4803
f6bcfd97
BP
4804 if ( !m_colWidths.IsEmpty() )
4805 {
27f35b66
SN
4806 m_colWidths.Insert( m_defaultColWidth, pos, numCols );
4807 m_colRights.Insert( 0, pos, numCols );
f6bcfd97
BP
4808
4809 int right = 0;
2f024384 4810 if ( pos > 0 )
d4175745 4811 right = m_colRights[GetColAt( pos - 1 )];
60ff3b99 4812
d4175745
VZ
4813 int colPos;
4814 for ( colPos = pos; colPos < m_numCols; colPos++ )
f6bcfd97 4815 {
d4175745
VZ
4816 i = GetColAt( colPos );
4817
f6bcfd97
BP
4818 right += m_colWidths[i];
4819 m_colRights[i] = right;
4820 }
4821 }
2f024384 4822
f6bcfd97 4823 if ( m_currentCellCoords == wxGridNoCellCoords )
2d66e025 4824 {
f6bcfd97
BP
4825 // if we have just inserted cols into an empty grid the current
4826 // cell will be undefined...
4827 //
4828 SetCurrentCell( 0, 0 );
2d66e025 4829 }
3f3dc2ef
VZ
4830
4831 if ( m_selection )
4832 m_selection->UpdateCols( pos, numCols );
f6bcfd97
BP
4833 wxGridCellAttrProvider * attrProvider = m_table->GetAttrProvider();
4834 if (attrProvider)
4835 attrProvider->UpdateAttrCols( pos, numCols );
4836 if ( !GetBatchCount() )
4837 {
4838 CalcDimensions();
4839 m_colLabelWin->Refresh();
4840 }
f85afd4e 4841 }
ca65c044 4842 result = true;
f6bcfd97 4843 break;
f85afd4e
MB
4844
4845 case wxGRIDTABLE_NOTIFY_COLS_APPENDED:
4846 {
4847 int numCols = msg.GetCommandInt();
2d66e025 4848 int oldNumCols = m_numCols;
f85afd4e 4849 m_numCols += numCols;
d4175745
VZ
4850
4851 if ( !m_colAt.IsEmpty() )
4852 {
4853 m_colAt.Add( 0, numCols );
4854
4855 //Set the new columns' positions
4856 int i;
4857 for ( i = oldNumCols; i < m_numCols; i++ )
4858 {
4859 m_colAt[i] = i;
4860 }
4861 }
4862
f6bcfd97
BP
4863 if ( !m_colWidths.IsEmpty() )
4864 {
27f35b66
SN
4865 m_colWidths.Add( m_defaultColWidth, numCols );
4866 m_colRights.Add( 0, numCols );
2d66e025 4867
f6bcfd97 4868 int right = 0;
2f024384 4869 if ( oldNumCols > 0 )
d4175745 4870 right = m_colRights[GetColAt( oldNumCols - 1 )];
60ff3b99 4871
d4175745
VZ
4872 int colPos;
4873 for ( colPos = oldNumCols; colPos < m_numCols; colPos++ )
f6bcfd97 4874 {
d4175745
VZ
4875 i = GetColAt( colPos );
4876
f6bcfd97
BP
4877 right += m_colWidths[i];
4878 m_colRights[i] = right;
4879 }
4880 }
2f024384 4881
f6bcfd97 4882 if ( m_currentCellCoords == wxGridNoCellCoords )
2d66e025 4883 {
f6bcfd97
BP
4884 // if we have just inserted cols into an empty grid the current
4885 // cell will be undefined...
4886 //
4887 SetCurrentCell( 0, 0 );
4888 }
4889 if ( !GetBatchCount() )
4890 {
4891 CalcDimensions();
4892 m_colLabelWin->Refresh();
2d66e025 4893 }
f85afd4e 4894 }
ca65c044 4895 result = true;
f6bcfd97 4896 break;
f85afd4e
MB
4897
4898 case wxGRIDTABLE_NOTIFY_COLS_DELETED:
4899 {
4900 size_t pos = msg.GetCommandInt();
4901 int numCols = msg.GetCommandInt2();
f85afd4e 4902 m_numCols -= numCols;
f85afd4e 4903
d4175745
VZ
4904 if ( !m_colAt.IsEmpty() )
4905 {
4906 int colID = GetColAt( pos );
4907
4908 m_colAt.RemoveAt( pos, numCols );
4909
4910 //Shift the column IDs
4911 int colPos;
4912 for ( colPos = 0; colPos < m_numCols; colPos++ )
4913 {
4914 if ( m_colAt[colPos] > colID )
4915 m_colAt[colPos] -= numCols;
4916 }
4917 }
4918
f6bcfd97 4919 if ( !m_colWidths.IsEmpty() )
f85afd4e 4920 {
27f35b66
SN
4921 m_colWidths.RemoveAt( pos, numCols );
4922 m_colRights.RemoveAt( pos, numCols );
2d66e025
MB
4923
4924 int w = 0;
d4175745
VZ
4925 int colPos;
4926 for ( colPos = 0; colPos < m_numCols; colPos++ )
2d66e025 4927 {
d4175745
VZ
4928 i = GetColAt( colPos );
4929
2d66e025
MB
4930 w += m_colWidths[i];
4931 m_colRights[i] = w;
4932 }
f85afd4e 4933 }
2f024384 4934
f6bcfd97
BP
4935 if ( !m_numCols )
4936 {
4937 m_currentCellCoords = wxGridNoCellCoords;
4938 }
4939 else
4940 {
4941 if ( m_currentCellCoords.GetCol() >= m_numCols )
4942 m_currentCellCoords.Set( 0, 0 );
4943 }
3f3dc2ef
VZ
4944
4945 if ( m_selection )
4946 m_selection->UpdateCols( pos, -((int)numCols) );
f6bcfd97 4947 wxGridCellAttrProvider * attrProvider = m_table->GetAttrProvider();
4db6714b
KH
4948 if (attrProvider)
4949 {
f6bcfd97 4950 attrProvider->UpdateAttrCols( pos, -((int)numCols) );
ccdee36f 4951
84912ef8
RD
4952// ifdef'd out following patch from Paul Gammans
4953#if 0
f6bcfd97
BP
4954 // No need to touch row attributes, unless we
4955 // removed _all_ columns, in this case, we remove
4956 // all row attributes.
4957 // I hate to do this here, but the
4958 // needed data is not available inside UpdateAttrCols.
4959 if ( !GetNumberCols() )
4960 attrProvider->UpdateAttrRows( 0, -GetNumberRows() );
84912ef8 4961#endif
f6bcfd97 4962 }
ccdee36f 4963
f6bcfd97
BP
4964 if ( !GetBatchCount() )
4965 {
4966 CalcDimensions();
4967 m_colLabelWin->Refresh();
4968 }
f85afd4e 4969 }
ca65c044 4970 result = true;
f6bcfd97 4971 break;
f85afd4e
MB
4972 }
4973
f6bcfd97
BP
4974 if (result && !GetBatchCount() )
4975 m_gridWin->Refresh();
2f024384 4976
f6bcfd97 4977 return result;
f85afd4e
MB
4978}
4979
d10f4bf9 4980wxArrayInt wxGrid::CalcRowLabelsExposed( const wxRegion& reg )
f85afd4e 4981{
2d66e025
MB
4982 wxRegionIterator iter( reg );
4983 wxRect r;
f85afd4e 4984
275c4ae4
RD
4985 wxArrayInt rowlabels;
4986
2d66e025
MB
4987 int top, bottom;
4988 while ( iter )
f85afd4e 4989 {
2d66e025 4990 r = iter.GetRect();
f85afd4e 4991
2d66e025
MB
4992 // TODO: remove this when we can...
4993 // There is a bug in wxMotif that gives garbage update
4994 // rectangles if you jump-scroll a long way by clicking the
4995 // scrollbar with middle button. This is a work-around
4996 //
4997#if defined(__WXMOTIF__)
4998 int cw, ch;
4999 m_gridWin->GetClientSize( &cw, &ch );
56b6cf26
DS
5000 if ( r.GetTop() > ch )
5001 r.SetTop( 0 );
2d66e025
MB
5002 r.SetBottom( wxMin( r.GetBottom(), ch ) );
5003#endif
f85afd4e 5004
2d66e025
MB
5005 // logical bounds of update region
5006 //
5007 int dummy;
5008 CalcUnscrolledPosition( 0, r.GetTop(), &dummy, &top );
5009 CalcUnscrolledPosition( 0, r.GetBottom(), &dummy, &bottom );
5010
5011 // find the row labels within these bounds
5012 //
5013 int row;
3d59537f 5014 for ( row = internalYToRow(top); row < m_numRows; row++ )
2d66e025 5015 {
7c1cb261
VZ
5016 if ( GetRowBottom(row) < top )
5017 continue;
2d66e025 5018
6d55126d 5019 if ( GetRowTop(row) > bottom )
7c1cb261 5020 break;
60ff3b99 5021
d10f4bf9 5022 rowlabels.Add( row );
2d66e025 5023 }
60ff3b99 5024
60d8e886 5025 ++iter;
f85afd4e 5026 }
d10f4bf9
VZ
5027
5028 return rowlabels;
f85afd4e
MB
5029}
5030
d10f4bf9 5031wxArrayInt wxGrid::CalcColLabelsExposed( const wxRegion& reg )
f85afd4e 5032{
2d66e025
MB
5033 wxRegionIterator iter( reg );
5034 wxRect r;
f85afd4e 5035
d10f4bf9 5036 wxArrayInt colLabels;
f85afd4e 5037
2d66e025
MB
5038 int left, right;
5039 while ( iter )
f85afd4e 5040 {
2d66e025 5041 r = iter.GetRect();
f85afd4e 5042
2d66e025
MB
5043 // TODO: remove this when we can...
5044 // There is a bug in wxMotif that gives garbage update
5045 // rectangles if you jump-scroll a long way by clicking the
5046 // scrollbar with middle button. This is a work-around
5047 //
5048#if defined(__WXMOTIF__)
5049 int cw, ch;
5050 m_gridWin->GetClientSize( &cw, &ch );
56b6cf26
DS
5051 if ( r.GetLeft() > cw )
5052 r.SetLeft( 0 );
2d66e025
MB
5053 r.SetRight( wxMin( r.GetRight(), cw ) );
5054#endif
5055
5056 // logical bounds of update region
5057 //
5058 int dummy;
5059 CalcUnscrolledPosition( r.GetLeft(), 0, &left, &dummy );
5060 CalcUnscrolledPosition( r.GetRight(), 0, &right, &dummy );
5061
5062 // find the cells within these bounds
5063 //
5064 int col;
d4175745
VZ
5065 int colPos;
5066 for ( colPos = GetColPos( internalXToCol(left) ); colPos < m_numCols; colPos++ )
2d66e025 5067 {
d4175745
VZ
5068 col = GetColAt( colPos );
5069
7c1cb261
VZ
5070 if ( GetColRight(col) < left )
5071 continue;
60ff3b99 5072
7c1cb261
VZ
5073 if ( GetColLeft(col) > right )
5074 break;
2d66e025 5075
d10f4bf9 5076 colLabels.Add( col );
2d66e025 5077 }
60ff3b99 5078
60d8e886 5079 ++iter;
f85afd4e 5080 }
2f024384 5081
d10f4bf9 5082 return colLabels;
f85afd4e
MB
5083}
5084
d10f4bf9 5085wxGridCellCoordsArray wxGrid::CalcCellsExposed( const wxRegion& reg )
f85afd4e 5086{
2d66e025
MB
5087 wxRegionIterator iter( reg );
5088 wxRect r;
f85afd4e 5089
d10f4bf9 5090 wxGridCellCoordsArray cellsExposed;
f85afd4e 5091
2d66e025
MB
5092 int left, top, right, bottom;
5093 while ( iter )
5094 {
5095 r = iter.GetRect();
f85afd4e 5096
2d66e025
MB
5097 // TODO: remove this when we can...
5098 // There is a bug in wxMotif that gives garbage update
5099 // rectangles if you jump-scroll a long way by clicking the
5100 // scrollbar with middle button. This is a work-around
5101 //
5102#if defined(__WXMOTIF__)
f85afd4e 5103 int cw, ch;
2d66e025
MB
5104 m_gridWin->GetClientSize( &cw, &ch );
5105 if ( r.GetTop() > ch ) r.SetTop( 0 );
5106 if ( r.GetLeft() > cw ) r.SetLeft( 0 );
5107 r.SetRight( wxMin( r.GetRight(), cw ) );
5108 r.SetBottom( wxMin( r.GetBottom(), ch ) );
5109#endif
8f177c8e 5110
2d66e025
MB
5111 // logical bounds of update region
5112 //
5113 CalcUnscrolledPosition( r.GetLeft(), r.GetTop(), &left, &top );
5114 CalcUnscrolledPosition( r.GetRight(), r.GetBottom(), &right, &bottom );
f85afd4e 5115
2d66e025 5116 // find the cells within these bounds
f85afd4e 5117 //
2d66e025 5118 int row, col;
3d59537f 5119 for ( row = internalYToRow(top); row < m_numRows; row++ )
f85afd4e 5120 {
7c1cb261
VZ
5121 if ( GetRowBottom(row) <= top )
5122 continue;
f85afd4e 5123
7c1cb261
VZ
5124 if ( GetRowTop(row) > bottom )
5125 break;
60ff3b99 5126
d4175745
VZ
5127 int colPos;
5128 for ( colPos = GetColPos( internalXToCol(left) ); colPos < m_numCols; colPos++ )
2d66e025 5129 {
d4175745
VZ
5130 col = GetColAt( colPos );
5131
7c1cb261
VZ
5132 if ( GetColRight(col) <= left )
5133 continue;
60ff3b99 5134
7c1cb261
VZ
5135 if ( GetColLeft(col) > right )
5136 break;
60ff3b99 5137
d10f4bf9 5138 cellsExposed.Add( wxGridCellCoords( row, col ) );
2d66e025
MB
5139 }
5140 }
60ff3b99 5141
60d8e886 5142 ++iter;
f85afd4e 5143 }
d10f4bf9
VZ
5144
5145 return cellsExposed;
f85afd4e
MB
5146}
5147
5148
2d66e025 5149void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent& event )
f85afd4e 5150{
2d66e025
MB
5151 int x, y, row;
5152 wxPoint pos( event.GetPosition() );
5153 CalcUnscrolledPosition( pos.x, pos.y, &x, &y );
60ff3b99 5154
2d66e025 5155 if ( event.Dragging() )
f85afd4e 5156 {
426b2d87
JS
5157 if (!m_isDragging)
5158 {
ca65c044 5159 m_isDragging = true;
426b2d87
JS
5160 m_rowLabelWin->CaptureMouse();
5161 }
8f177c8e 5162
2d66e025 5163 if ( event.LeftIsDown() )
f85afd4e 5164 {
962a48f6 5165 switch ( m_cursorMode )
f85afd4e 5166 {
f85afd4e
MB
5167 case WXGRID_CURSOR_RESIZE_ROW:
5168 {
2d66e025
MB
5169 int cw, ch, left, dummy;
5170 m_gridWin->GetClientSize( &cw, &ch );
5171 CalcUnscrolledPosition( 0, 0, &left, &dummy );
60ff3b99 5172
2d66e025
MB
5173 wxClientDC dc( m_gridWin );
5174 PrepareDC( dc );
af547d51
VZ
5175 y = wxMax( y,
5176 GetRowTop(m_dragRowOrCol) +
5177 GetRowMinimalHeight(m_dragRowOrCol) );
d2fdd8d2 5178 dc.SetLogicalFunction(wxINVERT);
f85afd4e
MB
5179 if ( m_dragLastPos >= 0 )
5180 {
2d66e025 5181 dc.DrawLine( left, m_dragLastPos, left+cw, m_dragLastPos );
f85afd4e 5182 }
2d66e025
MB
5183 dc.DrawLine( left, y, left+cw, y );
5184 m_dragLastPos = y;
f85afd4e
MB
5185 }
5186 break;
5187
5188 case WXGRID_CURSOR_SELECT_ROW:
902725ee 5189 {
e32352cf 5190 if ( (row = YToRow( y )) >= 0 )
aa5e1f75 5191 {
3f3dc2ef
VZ
5192 if ( m_selection )
5193 {
5194 m_selection->SelectRow( row,
5195 event.ControlDown(),
5196 event.ShiftDown(),
5197 event.AltDown(),
5198 event.MetaDown() );
5199 }
f85afd4e 5200 }
902725ee
WS
5201 }
5202 break;
e2b42eeb
VZ
5203
5204 // default label to suppress warnings about "enumeration value
5205 // 'xxx' not handled in switch
5206 default:
5207 break;
f85afd4e
MB
5208 }
5209 }
5210 return;
5211 }
5212
426b2d87
JS
5213 if ( m_isDragging && (event.Entering() || event.Leaving()) )
5214 return;
8f177c8e 5215
426b2d87
JS
5216 if (m_isDragging)
5217 {
ccdee36f
DS
5218 if (m_rowLabelWin->HasCapture())
5219 m_rowLabelWin->ReleaseMouse();
ca65c044 5220 m_isDragging = false;
426b2d87 5221 }
60ff3b99 5222
6d004f67
MB
5223 // ------------ Entering or leaving the window
5224 //
5225 if ( event.Entering() || event.Leaving() )
5226 {
e2b42eeb 5227 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_rowLabelWin);
6d004f67
MB
5228 }
5229
2d66e025 5230 // ------------ Left button pressed
f85afd4e 5231 //
6d004f67 5232 else if ( event.LeftDown() )
f85afd4e 5233 {
2d66e025
MB
5234 // don't send a label click event for a hit on the
5235 // edge of the row label - this is probably the user
5236 // wanting to resize the row
5237 //
5238 if ( YToEdgeOfRow(y) < 0 )
f85afd4e 5239 {
2d66e025 5240 row = YToRow(y);
2f024384 5241 if ( row >= 0 &&
b54ba671 5242 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK, row, -1, event ) )
f85afd4e 5243 {
2b01fd49 5244 if ( !event.ShiftDown() && !event.CmdDown() )
aa5e1f75 5245 ClearSelection();
64e15340 5246 if ( m_selection )
3f3dc2ef
VZ
5247 {
5248 if ( event.ShiftDown() )
5249 {
5250 m_selection->SelectBlock( m_currentCellCoords.GetRow(),
5251 0,
5252 row,
5253 GetNumberCols() - 1,
5254 event.ControlDown(),
5255 event.ShiftDown(),
5256 event.AltDown(),
5257 event.MetaDown() );
5258 }
5259 else
5260 {
5261 m_selection->SelectRow( row,
5262 event.ControlDown(),
5263 event.ShiftDown(),
5264 event.AltDown(),
5265 event.MetaDown() );
5266 }
5267 }
5268
e2b42eeb 5269 ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW, m_rowLabelWin);
f85afd4e 5270 }
2d66e025
MB
5271 }
5272 else
5273 {
5274 // starting to drag-resize a row
6e8524b1
MB
5275 if ( CanDragRowSize() )
5276 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW, m_rowLabelWin);
2d66e025
MB
5277 }
5278 }
f85afd4e 5279
2d66e025
MB
5280 // ------------ Left double click
5281 //
5282 else if (event.LeftDClick() )
5283 {
4e115ed2 5284 row = YToEdgeOfRow(y);
d43851f7 5285 if ( row < 0 )
2d66e025
MB
5286 {
5287 row = YToRow(y);
a967f048 5288 if ( row >=0 &&
ca65c044
WS
5289 !SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK, row, -1, event ) )
5290 {
a967f048 5291 // no default action at the moment
ca65c044 5292 }
f85afd4e 5293 }
d43851f7
JS
5294 else
5295 {
5296 // adjust row height depending on label text
5297 AutoSizeRowLabelSize( row );
5298
5299 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_colLabelWin);
ccdee36f 5300 m_dragLastPos = -1;
d43851f7 5301 }
f85afd4e 5302 }
60ff3b99 5303
2d66e025 5304 // ------------ Left button released
f85afd4e 5305 //
2d66e025 5306 else if ( event.LeftUp() )
f85afd4e 5307 {
2d66e025 5308 if ( m_cursorMode == WXGRID_CURSOR_RESIZE_ROW )
f85afd4e 5309 {
6d004f67 5310 DoEndDragResizeRow();
60ff3b99 5311
6d004f67
MB
5312 // Note: we are ending the event *after* doing
5313 // default processing in this case
5314 //
b54ba671 5315 SendEvent( wxEVT_GRID_ROW_SIZE, m_dragRowOrCol, -1, event );
2d66e025 5316 }
f85afd4e 5317
e2b42eeb
VZ
5318 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_rowLabelWin);
5319 m_dragLastPos = -1;
2d66e025 5320 }
f85afd4e 5321
2d66e025
MB
5322 // ------------ Right button down
5323 //
5324 else if ( event.RightDown() )
5325 {
5326 row = YToRow(y);
ef5df12b 5327 if ( row >=0 &&
ca65c044 5328 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK, row, -1, event ) )
2d66e025
MB
5329 {
5330 // no default action at the moment
f85afd4e
MB
5331 }
5332 }
60ff3b99 5333
2d66e025 5334 // ------------ Right double click
f85afd4e 5335 //
2d66e025
MB
5336 else if ( event.RightDClick() )
5337 {
5338 row = YToRow(y);
a967f048 5339 if ( row >= 0 &&
ca65c044 5340 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK, row, -1, event ) )
2d66e025
MB
5341 {
5342 // no default action at the moment
5343 }
5344 }
60ff3b99 5345
2d66e025 5346 // ------------ No buttons down and mouse moving
f85afd4e 5347 //
2d66e025 5348 else if ( event.Moving() )
f85afd4e 5349 {
2d66e025
MB
5350 m_dragRowOrCol = YToEdgeOfRow( y );
5351 if ( m_dragRowOrCol >= 0 )
8f177c8e 5352 {
2d66e025 5353 if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
8f177c8e 5354 {
e2b42eeb 5355 // don't capture the mouse yet
6e8524b1 5356 if ( CanDragRowSize() )
ca65c044 5357 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW, m_rowLabelWin, false);
8f177c8e 5358 }
2d66e025 5359 }
6d004f67 5360 else if ( m_cursorMode != WXGRID_CURSOR_SELECT_CELL )
2d66e025 5361 {
ca65c044 5362 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_rowLabelWin, false);
8f177c8e 5363 }
f85afd4e 5364 }
2d66e025
MB
5365}
5366
2d66e025
MB
5367void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent& event )
5368{
5369 int x, y, col;
5370 wxPoint pos( event.GetPosition() );
5371 CalcUnscrolledPosition( pos.x, pos.y, &x, &y );
60ff3b99 5372
2d66e025 5373 if ( event.Dragging() )
f85afd4e 5374 {
fe77cf60
JS
5375 if (!m_isDragging)
5376 {
ca65c044 5377 m_isDragging = true;
fe77cf60 5378 m_colLabelWin->CaptureMouse();
d4175745
VZ
5379
5380 if ( m_cursorMode == WXGRID_CURSOR_MOVE_COL )
5381 m_dragRowOrCol = XToCol( x );
fe77cf60 5382 }
8f177c8e 5383
2d66e025 5384 if ( event.LeftIsDown() )
8f177c8e 5385 {
962a48f6 5386 switch ( m_cursorMode )
8f177c8e 5387 {
2d66e025 5388 case WXGRID_CURSOR_RESIZE_COL:
8f177c8e 5389 {
2d66e025
MB
5390 int cw, ch, dummy, top;
5391 m_gridWin->GetClientSize( &cw, &ch );
5392 CalcUnscrolledPosition( 0, 0, &dummy, &top );
60ff3b99 5393
2d66e025
MB
5394 wxClientDC dc( m_gridWin );
5395 PrepareDC( dc );
43947979
VZ
5396
5397 x = wxMax( x, GetColLeft(m_dragRowOrCol) +
5398 GetColMinimalWidth(m_dragRowOrCol));
d2fdd8d2 5399 dc.SetLogicalFunction(wxINVERT);
2d66e025
MB
5400 if ( m_dragLastPos >= 0 )
5401 {
ccdee36f 5402 dc.DrawLine( m_dragLastPos, top, m_dragLastPos, top + ch );
2d66e025 5403 }
ccdee36f 5404 dc.DrawLine( x, top, x, top + ch );
2d66e025 5405 m_dragLastPos = x;
f85afd4e 5406 }
2d66e025 5407 break;
f85afd4e 5408
2d66e025 5409 case WXGRID_CURSOR_SELECT_COL:
902725ee 5410 {
e32352cf 5411 if ( (col = XToCol( x )) >= 0 )
aa5e1f75 5412 {
3f3dc2ef
VZ
5413 if ( m_selection )
5414 {
5415 m_selection->SelectCol( col,
5416 event.ControlDown(),
5417 event.ShiftDown(),
5418 event.AltDown(),
5419 event.MetaDown() );
5420 }
2d66e025 5421 }
902725ee
WS
5422 }
5423 break;
e2b42eeb 5424
d4175745
VZ
5425 case WXGRID_CURSOR_MOVE_COL:
5426 {
5427 if ( x < 0 )
5428 m_moveToCol = GetColAt( 0 );
5429 else
5430 m_moveToCol = XToCol( x );
5431
5432 int markerX;
5433
5434 if ( m_moveToCol < 0 )
5435 markerX = GetColRight( GetColAt( m_numCols - 1 ) );
5436 else
5437 markerX = GetColLeft( m_moveToCol );
5438
5439 if ( markerX != m_dragLastPos )
5440 {
5441 wxClientDC dc( m_colLabelWin );
5442
5443 int cw, ch;
5444 m_colLabelWin->GetClientSize( &cw, &ch );
5445
5446 markerX++;
5447
5448 //Clean up the last indicator
5449 if ( m_dragLastPos >= 0 )
5450 {
5451 wxPen pen( m_colLabelWin->GetBackgroundColour(), 2 );
5452 dc.SetPen(pen);
5453 dc.DrawLine( m_dragLastPos + 1, 0, m_dragLastPos + 1, ch );
5454 dc.SetPen(wxNullPen);
5455
5456 if ( XToCol( m_dragLastPos ) != -1 )
5457 DrawColLabel( dc, XToCol( m_dragLastPos ) );
5458 }
5459
5460 //Moving to the same place? Don't draw a marker
5461 if ( (m_moveToCol == m_dragRowOrCol)
5462 || (GetColPos( m_moveToCol ) == GetColPos( m_dragRowOrCol ) + 1)
5463 || (m_moveToCol < 0 && m_dragRowOrCol == GetColAt( m_numCols - 1 )))
5464 {
5465 m_dragLastPos = -1;
5466 return;
5467 }
5468
5469 //Draw the marker
5470 wxPen pen( *wxBLUE, 2 );
5471 dc.SetPen(pen);
5472
5473 dc.DrawLine( markerX, 0, markerX, ch );
5474
5475 dc.SetPen(wxNullPen);
5476
5477 m_dragLastPos = markerX - 1;
5478 }
5479 }
5480 break;
5481
e2b42eeb
VZ
5482 // default label to suppress warnings about "enumeration value
5483 // 'xxx' not handled in switch
5484 default:
5485 break;
2d66e025 5486 }
f85afd4e 5487 }
2d66e025 5488 return;
f85afd4e 5489 }
2d66e025 5490
fe77cf60
JS
5491 if ( m_isDragging && (event.Entering() || event.Leaving()) )
5492 return;
2d66e025 5493
fe77cf60
JS
5494 if (m_isDragging)
5495 {
ccdee36f
DS
5496 if (m_colLabelWin->HasCapture())
5497 m_colLabelWin->ReleaseMouse();
ca65c044 5498 m_isDragging = false;
fe77cf60 5499 }
60ff3b99 5500
6d004f67
MB
5501 // ------------ Entering or leaving the window
5502 //
5503 if ( event.Entering() || event.Leaving() )
5504 {
e2b42eeb 5505 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_colLabelWin);
6d004f67
MB
5506 }
5507
2d66e025 5508 // ------------ Left button pressed
f85afd4e 5509 //
6d004f67 5510 else if ( event.LeftDown() )
f85afd4e 5511 {
2d66e025
MB
5512 // don't send a label click event for a hit on the
5513 // edge of the col label - this is probably the user
5514 // wanting to resize the col
5515 //
5516 if ( XToEdgeOfCol(x) < 0 )
8f177c8e 5517 {
2d66e025 5518 col = XToCol(x);
2f024384 5519 if ( col >= 0 &&
b54ba671 5520 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK, -1, col, event ) )
8f177c8e 5521 {
d4175745 5522 if ( m_canDragColMove )
3f3dc2ef 5523 {
d4175745
VZ
5524 //Show button as pressed
5525 wxClientDC dc( m_colLabelWin );
5526 int colLeft = GetColLeft( col );
5527 int colRight = GetColRight( col ) - 1;
5528 dc.SetPen( wxPen( m_colLabelWin->GetBackgroundColour(), 1 ) );
5529 dc.DrawLine( colLeft, 1, colLeft, m_colLabelHeight-1 );
5530 dc.DrawLine( colLeft, 1, colRight, 1 );
5531
5532 ChangeCursorMode(WXGRID_CURSOR_MOVE_COL, m_colLabelWin);
5533 }
5534 else
5535 {
5536 if ( !event.ShiftDown() && !event.CmdDown() )
5537 ClearSelection();
5538 if ( m_selection )
3f3dc2ef 5539 {
d4175745
VZ
5540 if ( event.ShiftDown() )
5541 {
5542 m_selection->SelectBlock( 0,
5543 m_currentCellCoords.GetCol(),
5544 GetNumberRows() - 1, col,
5545 event.ControlDown(),
5546 event.ShiftDown(),
5547 event.AltDown(),
5548 event.MetaDown() );
5549 }
5550 else
5551 {
5552 m_selection->SelectCol( col,
5553 event.ControlDown(),
5554 event.ShiftDown(),
5555 event.AltDown(),
5556 event.MetaDown() );
5557 }
3f3dc2ef 5558 }
3f3dc2ef 5559
d4175745
VZ
5560 ChangeCursorMode(WXGRID_CURSOR_SELECT_COL, m_colLabelWin);
5561 }
f85afd4e 5562 }
2d66e025
MB
5563 }
5564 else
5565 {
5566 // starting to drag-resize a col
5567 //
6e8524b1
MB
5568 if ( CanDragColSize() )
5569 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL, m_colLabelWin);
2d66e025
MB
5570 }
5571 }
f85afd4e 5572
2d66e025
MB
5573 // ------------ Left double click
5574 //
5575 if ( event.LeftDClick() )
5576 {
4e115ed2 5577 col = XToEdgeOfCol(x);
d43851f7 5578 if ( col < 0 )
2d66e025
MB
5579 {
5580 col = XToCol(x);
a967f048
RG
5581 if ( col >= 0 &&
5582 ! SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK, -1, col, event ) )
5583 {
ca65c044 5584 // no default action at the moment
a967f048 5585 }
2d66e025 5586 }
d43851f7
JS
5587 else
5588 {
5589 // adjust column width depending on label text
5590 AutoSizeColLabelSize( col );
5591
5592 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_colLabelWin);
ccdee36f 5593 m_dragLastPos = -1;
d43851f7 5594 }
2d66e025 5595 }
60ff3b99 5596
2d66e025
MB
5597 // ------------ Left button released
5598 //
5599 else if ( event.LeftUp() )
5600 {
d4175745 5601 switch ( m_cursorMode )
2d66e025 5602 {
d4175745 5603 case WXGRID_CURSOR_RESIZE_COL:
d4175745 5604 DoEndDragResizeCol();
e2b42eeb 5605
d4175745
VZ
5606 // Note: we are ending the event *after* doing
5607 // default processing in this case
5608 //
5609 SendEvent( wxEVT_GRID_COL_SIZE, -1, m_dragRowOrCol, event );
852b6c3c 5610 break;
d4175745
VZ
5611
5612 case WXGRID_CURSOR_MOVE_COL:
d4175745
VZ
5613 DoEndDragMoveCol();
5614
5615 SendEvent( wxEVT_GRID_COL_MOVE, -1, m_dragRowOrCol, event );
852b6c3c
VZ
5616 break;
5617
5618 case WXGRID_CURSOR_SELECT_COL:
5619 case WXGRID_CURSOR_SELECT_CELL:
5620 case WXGRID_CURSOR_RESIZE_ROW:
5621 case WXGRID_CURSOR_SELECT_ROW:
5622 // nothing to do (?)
5623 break;
2d66e025 5624 }
f85afd4e 5625
e2b42eeb 5626 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_colLabelWin);
ccdee36f 5627 m_dragLastPos = -1;
60ff3b99
VZ
5628 }
5629
2d66e025
MB
5630 // ------------ Right button down
5631 //
5632 else if ( event.RightDown() )
5633 {
5634 col = XToCol(x);
a967f048 5635 if ( col >= 0 &&
ca65c044 5636 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK, -1, col, event ) )
2d66e025
MB
5637 {
5638 // no default action at the moment
f85afd4e
MB
5639 }
5640 }
60ff3b99 5641
2d66e025 5642 // ------------ Right double click
f85afd4e 5643 //
2d66e025
MB
5644 else if ( event.RightDClick() )
5645 {
5646 col = XToCol(x);
a967f048 5647 if ( col >= 0 &&
ca65c044 5648 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK, -1, col, event ) )
2d66e025
MB
5649 {
5650 // no default action at the moment
5651 }
5652 }
60ff3b99 5653
2d66e025 5654 // ------------ No buttons down and mouse moving
f85afd4e 5655 //
2d66e025 5656 else if ( event.Moving() )
f85afd4e 5657 {
2d66e025
MB
5658 m_dragRowOrCol = XToEdgeOfCol( x );
5659 if ( m_dragRowOrCol >= 0 )
f85afd4e 5660 {
2d66e025 5661 if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
f85afd4e 5662 {
e2b42eeb 5663 // don't capture the cursor yet
6e8524b1 5664 if ( CanDragColSize() )
ca65c044 5665 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL, m_colLabelWin, false);
f85afd4e 5666 }
2d66e025 5667 }
6d004f67 5668 else if ( m_cursorMode != WXGRID_CURSOR_SELECT_CELL )
2d66e025 5669 {
ca65c044 5670 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_colLabelWin, false);
8f177c8e 5671 }
f85afd4e
MB
5672 }
5673}
5674
2d66e025 5675void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent& event )
f85afd4e 5676{
2d66e025 5677 if ( event.LeftDown() )
f85afd4e 5678 {
2d66e025
MB
5679 // indicate corner label by having both row and
5680 // col args == -1
f85afd4e 5681 //
b54ba671 5682 if ( !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK, -1, -1, event ) )
2d66e025
MB
5683 {
5684 SelectAll();
5685 }
f85afd4e 5686 }
2d66e025
MB
5687 else if ( event.LeftDClick() )
5688 {
b54ba671 5689 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK, -1, -1, event );
2d66e025 5690 }
2d66e025 5691 else if ( event.RightDown() )
f85afd4e 5692 {
b54ba671 5693 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK, -1, -1, event ) )
f85afd4e 5694 {
2d66e025
MB
5695 // no default action at the moment
5696 }
5697 }
2d66e025
MB
5698 else if ( event.RightDClick() )
5699 {
b54ba671 5700 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK, -1, -1, event ) )
2d66e025
MB
5701 {
5702 // no default action at the moment
5703 }
5704 }
5705}
f85afd4e 5706
e2b42eeb
VZ
5707void wxGrid::ChangeCursorMode(CursorMode mode,
5708 wxWindow *win,
5709 bool captureMouse)
5710{
5711#ifdef __WXDEBUG__
5712 static const wxChar *cursorModes[] =
5713 {
5714 _T("SELECT_CELL"),
5715 _T("RESIZE_ROW"),
5716 _T("RESIZE_COL"),
5717 _T("SELECT_ROW"),
d4175745
VZ
5718 _T("SELECT_COL"),
5719 _T("MOVE_COL"),
e2b42eeb
VZ
5720 };
5721
181bfffd
VZ
5722 wxLogTrace(_T("grid"),
5723 _T("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
e2b42eeb
VZ
5724 win == m_colLabelWin ? _T("colLabelWin")
5725 : win ? _T("rowLabelWin")
5726 : _T("gridWin"),
5727 cursorModes[m_cursorMode], cursorModes[mode]);
2f024384 5728#endif
e2b42eeb 5729
faec5a43
SN
5730 if ( mode == m_cursorMode &&
5731 win == m_winCapture &&
5732 captureMouse == (m_winCapture != NULL))
e2b42eeb
VZ
5733 return;
5734
5735 if ( !win )
5736 {
5737 // by default use the grid itself
5738 win = m_gridWin;
5739 }
5740
5741 if ( m_winCapture )
5742 {
2f024384
DS
5743 if (m_winCapture->HasCapture())
5744 m_winCapture->ReleaseMouse();
e2b42eeb
VZ
5745 m_winCapture = (wxWindow *)NULL;
5746 }
5747
5748 m_cursorMode = mode;
5749
5750 switch ( m_cursorMode )
5751 {
5752 case WXGRID_CURSOR_RESIZE_ROW:
5753 win->SetCursor( m_rowResizeCursor );
5754 break;
5755
5756 case WXGRID_CURSOR_RESIZE_COL:
5757 win->SetCursor( m_colResizeCursor );
5758 break;
5759
d4175745
VZ
5760 case WXGRID_CURSOR_MOVE_COL:
5761 win->SetCursor( wxCursor(wxCURSOR_HAND) );
5762 break;
5763
e2b42eeb
VZ
5764 default:
5765 win->SetCursor( *wxSTANDARD_CURSOR );
2f024384 5766 break;
e2b42eeb
VZ
5767 }
5768
5769 // we need to capture mouse when resizing
5770 bool resize = m_cursorMode == WXGRID_CURSOR_RESIZE_ROW ||
5771 m_cursorMode == WXGRID_CURSOR_RESIZE_COL;
5772
5773 if ( captureMouse && resize )
5774 {
5775 win->CaptureMouse();
5776 m_winCapture = win;
5777 }
5778}
8f177c8e 5779
2d66e025
MB
5780void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent& event )
5781{
5782 int x, y;
5783 wxPoint pos( event.GetPosition() );
5784 CalcUnscrolledPosition( pos.x, pos.y, &x, &y );
60ff3b99 5785
2d66e025
MB
5786 wxGridCellCoords coords;
5787 XYToCell( x, y, coords );
60ff3b99 5788
27f35b66 5789 int cell_rows, cell_cols;
79dbea21 5790 bool isFirstDrag = !m_isDragging;
27f35b66
SN
5791 GetCellSize( coords.GetRow(), coords.GetCol(), &cell_rows, &cell_cols );
5792 if ((cell_rows < 0) || (cell_cols < 0))
5793 {
5794 coords.SetRow(coords.GetRow() + cell_rows);
5795 coords.SetCol(coords.GetCol() + cell_cols);
5796 }
5797
2d66e025
MB
5798 if ( event.Dragging() )
5799 {
07296f0b
RD
5800 //wxLogDebug("pos(%d, %d) coords(%d, %d)", pos.x, pos.y, coords.GetRow(), coords.GetCol());
5801
962a48f6 5802 // Don't start doing anything until the mouse has been dragged at
07296f0b 5803 // least 3 pixels in any direction...
508011ce
VZ
5804 if (! m_isDragging)
5805 {
5806 if (m_startDragPos == wxDefaultPosition)
5807 {
07296f0b
RD
5808 m_startDragPos = pos;
5809 return;
5810 }
5811 if (abs(m_startDragPos.x - pos.x) < 4 && abs(m_startDragPos.y - pos.y) < 4)
5812 return;
5813 }
5814
ca65c044 5815 m_isDragging = true;
2d66e025
MB
5816 if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
5817 {
f0102d2a 5818 // Hide the edit control, so it
4db6714b 5819 // won't interfere with drag-shrinking.
f6bcfd97 5820 if ( IsCellEditControlShown() )
a5777624 5821 {
f0102d2a 5822 HideCellEditControl();
a5777624
RD
5823 SaveEditControlValue();
5824 }
07296f0b
RD
5825
5826 // Have we captured the mouse yet?
508011ce
VZ
5827 if (! m_winCapture)
5828 {
07296f0b
RD
5829 m_winCapture = m_gridWin;
5830 m_winCapture->CaptureMouse();
5831 }
5832
2d66e025
MB
5833 if ( coords != wxGridNoCellCoords )
5834 {
2b01fd49 5835 if ( event.CmdDown() )
aa5e1f75
SN
5836 {
5837 if ( m_selectingKeyboard == wxGridNoCellCoords)
5838 m_selectingKeyboard = coords;
a9339fe2 5839 HighlightBlock( m_selectingKeyboard, coords );
aa5e1f75 5840 }
79dbea21
RD
5841 else if ( CanDragCell() )
5842 {
5843 if ( isFirstDrag )
5844 {
5845 if ( m_selectingKeyboard == wxGridNoCellCoords)
5846 m_selectingKeyboard = coords;
5847
5848 SendEvent( wxEVT_GRID_CELL_BEGIN_DRAG,
5849 coords.GetRow(),
5850 coords.GetCol(),
5851 event );
5852 }
5853 }
aa5e1f75
SN
5854 else
5855 {
5856 if ( !IsSelection() )
5857 {
c9097836 5858 HighlightBlock( coords, coords );
aa5e1f75
SN
5859 }
5860 else
5861 {
c9097836 5862 HighlightBlock( m_currentCellCoords, coords );
aa5e1f75 5863 }
f85afd4e 5864 }
07296f0b 5865
508011ce
VZ
5866 if (! IsVisible(coords))
5867 {
07296f0b
RD
5868 MakeCellVisible(coords);
5869 // TODO: need to introduce a delay or something here. The
e32352cf 5870 // scrolling is way to fast, at least on MSW - also on GTK.
07296f0b 5871 }
2d66e025
MB
5872 }
5873 }
6d004f67
MB
5874 else if ( m_cursorMode == WXGRID_CURSOR_RESIZE_ROW )
5875 {
5876 int cw, ch, left, dummy;
5877 m_gridWin->GetClientSize( &cw, &ch );
5878 CalcUnscrolledPosition( 0, 0, &left, &dummy );
8f177c8e 5879
6d004f67
MB
5880 wxClientDC dc( m_gridWin );
5881 PrepareDC( dc );
a95e38c0
VZ
5882 y = wxMax( y, GetRowTop(m_dragRowOrCol) +
5883 GetRowMinimalHeight(m_dragRowOrCol) );
6d004f67
MB
5884 dc.SetLogicalFunction(wxINVERT);
5885 if ( m_dragLastPos >= 0 )
5886 {
5887 dc.DrawLine( left, m_dragLastPos, left+cw, m_dragLastPos );
5888 }
5889 dc.DrawLine( left, y, left+cw, y );
5890 m_dragLastPos = y;
5891 }
5892 else if ( m_cursorMode == WXGRID_CURSOR_RESIZE_COL )
5893 {
5894 int cw, ch, dummy, top;
5895 m_gridWin->GetClientSize( &cw, &ch );
5896 CalcUnscrolledPosition( 0, 0, &dummy, &top );
e2b42eeb 5897
6d004f67
MB
5898 wxClientDC dc( m_gridWin );
5899 PrepareDC( dc );
43947979
VZ
5900 x = wxMax( x, GetColLeft(m_dragRowOrCol) +
5901 GetColMinimalWidth(m_dragRowOrCol) );
6d004f67
MB
5902 dc.SetLogicalFunction(wxINVERT);
5903 if ( m_dragLastPos >= 0 )
5904 {
a9339fe2 5905 dc.DrawLine( m_dragLastPos, top, m_dragLastPos, top + ch );
6d004f67 5906 }
a9339fe2 5907 dc.DrawLine( x, top, x, top + ch );
6d004f67
MB
5908 m_dragLastPos = x;
5909 }
e2b42eeb 5910
2d66e025
MB
5911 return;
5912 }
66242c80 5913
ca65c044 5914 m_isDragging = false;
07296f0b
RD
5915 m_startDragPos = wxDefaultPosition;
5916
a5777624
RD
5917 // VZ: if we do this, the mode is reset to WXGRID_CURSOR_SELECT_CELL
5918 // immediately after it becomes WXGRID_CURSOR_RESIZE_ROW/COL under
5919 // wxGTK
e2b42eeb 5920#if 0
a5777624
RD
5921 if ( event.Entering() || event.Leaving() )
5922 {
5923 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL);
5924 m_gridWin->SetCursor( *wxSTANDARD_CURSOR );
5925 }
5926 else
e2b42eeb
VZ
5927#endif // 0
5928
a5777624
RD
5929 // ------------ Left button pressed
5930 //
5931 if ( event.LeftDown() && coords != wxGridNoCellCoords )
f6bcfd97
BP
5932 {
5933 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_CLICK,
5934 coords.GetRow(),
5935 coords.GetCol(),
5936 event ) )
a5777624 5937 {
2b01fd49 5938 if ( !event.CmdDown() )
f6bcfd97
BP
5939 ClearSelection();
5940 if ( event.ShiftDown() )
5941 {
3f3dc2ef
VZ
5942 if ( m_selection )
5943 {
5944 m_selection->SelectBlock( m_currentCellCoords.GetRow(),
5945 m_currentCellCoords.GetCol(),
5946 coords.GetRow(),
5947 coords.GetCol(),
5948 event.ControlDown(),
5949 event.ShiftDown(),
5950 event.AltDown(),
5951 event.MetaDown() );
5952 }
f6bcfd97 5953 }
2f024384 5954 else if ( XToEdgeOfCol(x) < 0 &&
f6bcfd97 5955 YToEdgeOfRow(y) < 0 )
58dd5b3b 5956 {
a5777624
RD
5957 DisableCellEditControl();
5958 MakeCellVisible( coords );
5959
2b01fd49 5960 if ( event.CmdDown() )
58dd5b3b 5961 {
73145b0e
JS
5962 if ( m_selection )
5963 {
5964 m_selection->ToggleCellSelection( coords.GetRow(),
5965 coords.GetCol(),
5966 event.ControlDown(),
5967 event.ShiftDown(),
5968 event.AltDown(),
5969 event.MetaDown() );
5970 }
5971 m_selectingTopLeft = wxGridNoCellCoords;
5972 m_selectingBottomRight = wxGridNoCellCoords;
5973 m_selectingKeyboard = coords;
a5777624
RD
5974 }
5975 else
5976 {
73145b0e
JS
5977 m_waitForSlowClick = m_currentCellCoords == coords && coords != wxGridNoCellCoords;
5978 SetCurrentCell( coords );
5979 if ( m_selection )
aa5e1f75 5980 {
73145b0e
JS
5981 if ( m_selection->GetSelectionMode() !=
5982 wxGrid::wxGridSelectCells )
3f3dc2ef 5983 {
73145b0e 5984 HighlightBlock( coords, coords );
3f3dc2ef 5985 }
aa5e1f75 5986 }
58dd5b3b
MB
5987 }
5988 }
f85afd4e 5989 }
a5777624 5990 }
f85afd4e 5991
a5777624
RD
5992 // ------------ Left double click
5993 //
5994 else if ( event.LeftDClick() && coords != wxGridNoCellCoords )
5995 {
5996 DisableCellEditControl();
5997
2f024384 5998 if ( XToEdgeOfCol(x) < 0 && YToEdgeOfRow(y) < 0 )
58dd5b3b 5999 {
1ef49ab5
VS
6000 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_DCLICK,
6001 coords.GetRow(),
6002 coords.GetCol(),
6003 event ) )
6004 {
6005 // we want double click to select a cell and start editing
6006 // (i.e. to behave in same way as sequence of two slow clicks):
6007 m_waitForSlowClick = true;
6008 }
58dd5b3b 6009 }
a5777624 6010 }
f85afd4e 6011
a5777624
RD
6012 // ------------ Left button released
6013 //
6014 else if ( event.LeftUp() )
6015 {
6016 if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
f85afd4e 6017 {
f40f9976
JS
6018 if (m_winCapture)
6019 {
2f024384
DS
6020 if (m_winCapture->HasCapture())
6021 m_winCapture->ReleaseMouse();
f40f9976
JS
6022 m_winCapture = NULL;
6023 }
6024
bee19958 6025 if ( coords == m_currentCellCoords && m_waitForSlowClick && CanEnableCellControl() )
932b55d0
JS
6026 {
6027 ClearSelection();
6028 EnableCellEditControl();
6029
2f024384 6030 wxGridCellAttr *attr = GetCellAttr(coords);
932b55d0
JS
6031 wxGridCellEditor *editor = attr->GetEditor(this, coords.GetRow(), coords.GetCol());
6032 editor->StartingClick();
6033 editor->DecRef();
6034 attr->DecRef();
6035
ca65c044 6036 m_waitForSlowClick = false;
932b55d0
JS
6037 }
6038 else if ( m_selectingTopLeft != wxGridNoCellCoords &&
b5808881 6039 m_selectingBottomRight != wxGridNoCellCoords )
52068ea5 6040 {
3f3dc2ef
VZ
6041 if ( m_selection )
6042 {
6043 m_selection->SelectBlock( m_selectingTopLeft.GetRow(),
6044 m_selectingTopLeft.GetCol(),
6045 m_selectingBottomRight.GetRow(),
6046 m_selectingBottomRight.GetCol(),
6047 event.ControlDown(),
6048 event.ShiftDown(),
6049 event.AltDown(),
6050 event.MetaDown() );
6051 }
6052
5c8fc7c1
SN
6053 m_selectingTopLeft = wxGridNoCellCoords;
6054 m_selectingBottomRight = wxGridNoCellCoords;
2d66e025 6055
73145b0e
JS
6056 // Show the edit control, if it has been hidden for
6057 // drag-shrinking.
6058 ShowCellEditControl();
6059 }
a5777624
RD
6060 }
6061 else if ( m_cursorMode == WXGRID_CURSOR_RESIZE_ROW )
6062 {
6063 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL);
6064 DoEndDragResizeRow();
e2b42eeb 6065
a5777624
RD
6066 // Note: we are ending the event *after* doing
6067 // default processing in this case
6068 //
6069 SendEvent( wxEVT_GRID_ROW_SIZE, m_dragRowOrCol, -1, event );
6070 }
6071 else if ( m_cursorMode == WXGRID_CURSOR_RESIZE_COL )
6072 {
6073 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL);
6074 DoEndDragResizeCol();
e2b42eeb 6075
a5777624
RD
6076 // Note: we are ending the event *after* doing
6077 // default processing in this case
6078 //
6079 SendEvent( wxEVT_GRID_COL_SIZE, -1, m_dragRowOrCol, event );
58dd5b3b 6080 }
f85afd4e 6081
a5777624
RD
6082 m_dragLastPos = -1;
6083 }
6084
a5777624
RD
6085 // ------------ Right button down
6086 //
6087 else if ( event.RightDown() && coords != wxGridNoCellCoords )
6088 {
6089 DisableCellEditControl();
6090 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_CLICK,
6091 coords.GetRow(),
6092 coords.GetCol(),
6093 event ) )
f85afd4e 6094 {
a5777624 6095 // no default action at the moment
60ff3b99 6096 }
a5777624 6097 }
2d66e025 6098
a5777624
RD
6099 // ------------ Right double click
6100 //
6101 else if ( event.RightDClick() && coords != wxGridNoCellCoords )
6102 {
6103 DisableCellEditControl();
6104 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_DCLICK,
6105 coords.GetRow(),
6106 coords.GetCol(),
6107 event ) )
f85afd4e 6108 {
a5777624 6109 // no default action at the moment
60ff3b99 6110 }
a5777624
RD
6111 }
6112
6113 // ------------ Moving and no button action
6114 //
6115 else if ( event.Moving() && !event.IsButton() )
6116 {
4db6714b 6117 if ( coords.GetRow() < 0 || coords.GetCol() < 0 )
d57ad377
SN
6118 {
6119 // out of grid cell area
6120 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL);
6121 return;
6122 }
6123
a5777624
RD
6124 int dragRow = YToEdgeOfRow( y );
6125 int dragCol = XToEdgeOfCol( x );
790cc417 6126
a5777624
RD
6127 // Dragging on the corner of a cell to resize in both
6128 // directions is not implemented yet...
790cc417 6129 //
91894db3 6130 if ( dragRow >= 0 && dragCol >= 0 )
790cc417 6131 {
a5777624
RD
6132 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL);
6133 return;
6134 }
6d004f67 6135
d57ad377 6136 if ( dragRow >= 0 )
a5777624
RD
6137 {
6138 m_dragRowOrCol = dragRow;
6d004f67 6139
a5777624 6140 if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
6d004f67 6141 {
a5777624
RD
6142 if ( CanDragRowSize() && CanDragGridSize() )
6143 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW);
6d004f67 6144 }
a5777624 6145 }
91894db3 6146 else if ( dragCol >= 0 )
a5777624
RD
6147 {
6148 m_dragRowOrCol = dragCol;
e2b42eeb 6149
a5777624 6150 if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
6d004f67 6151 {
a5777624
RD
6152 if ( CanDragColSize() && CanDragGridSize() )
6153 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL);
6d004f67
MB
6154 }
6155 }
91894db3 6156 else // Neither on a row or col edge
a5777624 6157 {
91894db3
VZ
6158 if ( m_cursorMode != WXGRID_CURSOR_SELECT_CELL )
6159 {
6160 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL);
6161 }
a5777624
RD
6162 }
6163 }
6d004f67
MB
6164}
6165
6d004f67
MB
6166void wxGrid::DoEndDragResizeRow()
6167{
6168 if ( m_dragLastPos >= 0 )
6169 {
6170 // erase the last line and resize the row
6171 //
6172 int cw, ch, left, dummy;
6173 m_gridWin->GetClientSize( &cw, &ch );
6174 CalcUnscrolledPosition( 0, 0, &left, &dummy );
6175
6176 wxClientDC dc( m_gridWin );
6177 PrepareDC( dc );
6178 dc.SetLogicalFunction( wxINVERT );
a9339fe2 6179 dc.DrawLine( left, m_dragLastPos, left + cw, m_dragLastPos );
6d004f67 6180 HideCellEditControl();
a5777624 6181 SaveEditControlValue();
6d004f67 6182
7c1cb261 6183 int rowTop = GetRowTop(m_dragRowOrCol);
6d004f67 6184 SetRowSize( m_dragRowOrCol,
b8d24d4e 6185 wxMax( m_dragLastPos - rowTop, m_minAcceptableRowHeight ) );
e2b42eeb 6186
6d004f67
MB
6187 if ( !GetBatchCount() )
6188 {
6189 // Only needed to get the correct rect.y:
6190 wxRect rect ( CellToRect( m_dragRowOrCol, 0 ) );
6191 rect.x = 0;
6192 CalcScrolledPosition(0, rect.y, &dummy, &rect.y);
6193 rect.width = m_rowLabelWidth;
6194 rect.height = ch - rect.y;
ca65c044 6195 m_rowLabelWin->Refresh( true, &rect );
6d004f67 6196 rect.width = cw;
ccdee36f 6197
27f35b66
SN
6198 // if there is a multicell block, paint all of it
6199 if (m_table)
6200 {
6201 int i, cell_rows, cell_cols, subtract_rows = 0;
6202 int leftCol = XToCol(left);
a9339fe2 6203 int rightCol = internalXToCol(left + cw);
27f35b66
SN
6204 if (leftCol >= 0)
6205 {
27f35b66
SN
6206 for (i=leftCol; i<rightCol; i++)
6207 {
6208 GetCellSize(m_dragRowOrCol, i, &cell_rows, &cell_cols);
6209 if (cell_rows < subtract_rows)
6210 subtract_rows = cell_rows;
6211 }
6212 rect.y = GetRowTop(m_dragRowOrCol + subtract_rows);
6213 CalcScrolledPosition(0, rect.y, &dummy, &rect.y);
6214 rect.height = ch - rect.y;
6215 }
6216 }
ca65c044 6217 m_gridWin->Refresh( false, &rect );
6d004f67
MB
6218 }
6219
6220 ShowCellEditControl();
6221 }
6222}
6223
6224
6225void wxGrid::DoEndDragResizeCol()
6226{
6227 if ( m_dragLastPos >= 0 )
6228 {
6229 // erase the last line and resize the col
6230 //
6231 int cw, ch, dummy, top;
6232 m_gridWin->GetClientSize( &cw, &ch );
6233 CalcUnscrolledPosition( 0, 0, &dummy, &top );
6234
6235 wxClientDC dc( m_gridWin );
6236 PrepareDC( dc );
6237 dc.SetLogicalFunction( wxINVERT );
a9339fe2 6238 dc.DrawLine( m_dragLastPos, top, m_dragLastPos, top + ch );
6d004f67 6239 HideCellEditControl();
a5777624 6240 SaveEditControlValue();
6d004f67 6241
7c1cb261 6242 int colLeft = GetColLeft(m_dragRowOrCol);
6d004f67 6243 SetColSize( m_dragRowOrCol,
43947979
VZ
6244 wxMax( m_dragLastPos - colLeft,
6245 GetColMinimalWidth(m_dragRowOrCol) ) );
6d004f67
MB
6246
6247 if ( !GetBatchCount() )
6248 {
6249 // Only needed to get the correct rect.x:
6250 wxRect rect ( CellToRect( 0, m_dragRowOrCol ) );
6251 rect.y = 0;
6252 CalcScrolledPosition(rect.x, 0, &rect.x, &dummy);
6253 rect.width = cw - rect.x;
6254 rect.height = m_colLabelHeight;
ca65c044 6255 m_colLabelWin->Refresh( true, &rect );
6d004f67 6256 rect.height = ch;
2f024384 6257
27f35b66
SN
6258 // if there is a multicell block, paint all of it
6259 if (m_table)
6260 {
6261 int i, cell_rows, cell_cols, subtract_cols = 0;
6262 int topRow = YToRow(top);
a9339fe2 6263 int bottomRow = internalYToRow(top + cw);
27f35b66
SN
6264 if (topRow >= 0)
6265 {
27f35b66
SN
6266 for (i=topRow; i<bottomRow; i++)
6267 {
6268 GetCellSize(i, m_dragRowOrCol, &cell_rows, &cell_cols);
6269 if (cell_cols < subtract_cols)
6270 subtract_cols = cell_cols;
6271 }
a9339fe2 6272
27f35b66
SN
6273 rect.x = GetColLeft(m_dragRowOrCol + subtract_cols);
6274 CalcScrolledPosition(rect.x, 0, &rect.x, &dummy);
6275 rect.width = cw - rect.x;
6276 }
6277 }
2f024384 6278
ca65c044 6279 m_gridWin->Refresh( false, &rect );
790cc417 6280 }
e2b42eeb 6281
6d004f67 6282 ShowCellEditControl();
f85afd4e 6283 }
f85afd4e
MB
6284}
6285
d4175745
VZ
6286void wxGrid::DoEndDragMoveCol()
6287{
6288 //The user clicked on the column but didn't actually drag
6289 if ( m_dragLastPos < 0 )
6290 {
6291 m_colLabelWin->Refresh(); //Do this to "unpress" the column
6292 return;
6293 }
6294
6295 int newPos;
6296 if ( m_moveToCol == -1 )
6297 newPos = m_numCols - 1;
6298 else
6299 {
6300 newPos = GetColPos( m_moveToCol );
6301 if ( newPos > GetColPos( m_dragRowOrCol ) )
6302 newPos--;
6303 }
6304
6305 SetColPos( m_dragRowOrCol, newPos );
6306}
6307
6308void wxGrid::SetColPos( int colID, int newPos )
6309{
6310 if ( m_colAt.IsEmpty() )
6311 {
6312 m_colAt.Alloc( m_numCols );
6313
6314 int i;
6315 for ( i = 0; i < m_numCols; i++ )
6316 {
6317 m_colAt.Add( i );
6318 }
6319 }
6320
6321 int oldPos = GetColPos( colID );
6322
6323 //Reshuffle the m_colAt array
6324 if ( newPos > oldPos )
6325 {
6326 int i;
6327 for ( i = oldPos; i < newPos; i++ )
6328 {
6329 m_colAt[i] = m_colAt[i+1];
6330 }
6331 }
6332 else
6333 {
6334 int i;
6335 for ( i = oldPos; i > newPos; i-- )
6336 {
6337 m_colAt[i] = m_colAt[i-1];
6338 }
6339 }
6340
6341 m_colAt[newPos] = colID;
6342
6343 //Recalculate the column rights
6344 if ( !m_colWidths.IsEmpty() )
6345 {
6346 int colRight = 0;
6347 int colPos;
6348 for ( colPos = 0; colPos < m_numCols; colPos++ )
6349 {
6350 int colID = GetColAt( colPos );
6351
6352 colRight += m_colWidths[colID];
6353 m_colRights[colID] = colRight;
6354 }
6355 }
6356
6357 m_colLabelWin->Refresh();
6358 m_gridWin->Refresh();
6359}
6360
6361
6362
6363void wxGrid::EnableDragColMove( bool enable )
6364{
6365 if ( m_canDragColMove == enable )
6366 return;
6367
6368 m_canDragColMove = enable;
6369
6370 if ( !m_canDragColMove )
6371 {
6372 m_colAt.Clear();
6373
6374 //Recalculate the column rights
6375 if ( !m_colWidths.IsEmpty() )
6376 {
6377 int colRight = 0;
6378 int colPos;
6379 for ( colPos = 0; colPos < m_numCols; colPos++ )
6380 {
6381 colRight += m_colWidths[colPos];
6382 m_colRights[colPos] = colRight;
6383 }
6384 }
6385
6386 m_colLabelWin->Refresh();
6387 m_gridWin->Refresh();
6388 }
6389}
6390
6391
2d66e025
MB
6392//
6393// ------ interaction with data model
6394//
6395bool wxGrid::ProcessTableMessage( wxGridTableMessage& msg )
f85afd4e 6396{
2d66e025 6397 switch ( msg.GetId() )
17732cec 6398 {
2d66e025
MB
6399 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES:
6400 return GetModelValues();
17732cec 6401
2d66e025
MB
6402 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES:
6403 return SetModelValues();
f85afd4e 6404
2d66e025
MB
6405 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED:
6406 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED:
6407 case wxGRIDTABLE_NOTIFY_ROWS_DELETED:
6408 case wxGRIDTABLE_NOTIFY_COLS_INSERTED:
6409 case wxGRIDTABLE_NOTIFY_COLS_APPENDED:
6410 case wxGRIDTABLE_NOTIFY_COLS_DELETED:
6411 return Redimension( msg );
6412
6413 default:
ca65c044 6414 return false;
f85afd4e 6415 }
2d66e025 6416}
f85afd4e 6417
2d66e025 6418// The behaviour of this function depends on the grid table class
5b061713 6419// Clear() function. For the default wxGridStringTable class the
2d66e025
MB
6420// behavious is to replace all cell contents with wxEmptyString but
6421// not to change the number of rows or cols.
6422//
6423void wxGrid::ClearGrid()
6424{
6425 if ( m_table )
f85afd4e 6426 {
4cfa5de6
RD
6427 if (IsCellEditControlEnabled())
6428 DisableCellEditControl();
6429
2d66e025 6430 m_table->Clear();
5b061713 6431 if (!GetBatchCount())
a9339fe2 6432 m_gridWin->Refresh();
f85afd4e
MB
6433 }
6434}
6435
2d66e025 6436bool wxGrid::InsertRows( int pos, int numRows, bool WXUNUSED(updateLabels) )
f85afd4e 6437{
2d66e025 6438 // TODO: something with updateLabels flag
8f177c8e 6439
2d66e025 6440 if ( !m_created )
f85afd4e 6441 {
bd3c6758 6442 wxFAIL_MSG( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") );
ca65c044 6443 return false;
2d66e025 6444 }
8f177c8e 6445
2d66e025
MB
6446 if ( m_table )
6447 {
b7fff980 6448 if (IsCellEditControlEnabled())
b54ba671 6449 DisableCellEditControl();
b7fff980 6450
4ea3479c 6451 bool done = m_table->InsertRows( pos, numRows );
4ea3479c 6452 return done;
f85afd4e 6453
2d66e025
MB
6454 // the table will have sent the results of the insert row
6455 // operation to this view object as a grid table message
f85afd4e 6456 }
a9339fe2 6457
ca65c044 6458 return false;
f85afd4e
MB
6459}
6460
2d66e025 6461bool wxGrid::AppendRows( int numRows, bool WXUNUSED(updateLabels) )
f85afd4e 6462{
2d66e025
MB
6463 // TODO: something with updateLabels flag
6464
6465 if ( !m_created )
f85afd4e 6466 {
bd3c6758 6467 wxFAIL_MSG( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") );
ca65c044 6468 return false;
2d66e025
MB
6469 }
6470
4ea3479c
JS
6471 if ( m_table )
6472 {
6473 bool done = m_table && m_table->AppendRows( numRows );
4ea3479c 6474 return done;
a9339fe2 6475
4ea3479c
JS
6476 // the table will have sent the results of the append row
6477 // operation to this view object as a grid table message
6478 }
a9339fe2 6479
ca65c044 6480 return false;
f85afd4e
MB
6481}
6482
2d66e025 6483bool wxGrid::DeleteRows( int pos, int numRows, bool WXUNUSED(updateLabels) )
f85afd4e 6484{
2d66e025
MB
6485 // TODO: something with updateLabels flag
6486
6487 if ( !m_created )
f85afd4e 6488 {
bd3c6758 6489 wxFAIL_MSG( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") );
ca65c044 6490 return false;
2d66e025
MB
6491 }
6492
b7fff980 6493 if ( m_table )
2d66e025 6494 {
b7fff980 6495 if (IsCellEditControlEnabled())
b54ba671 6496 DisableCellEditControl();
f85afd4e 6497
4ea3479c 6498 bool done = m_table->DeleteRows( pos, numRows );
4ea3479c 6499 return done;
f6bcfd97
BP
6500 // the table will have sent the results of the delete row
6501 // operation to this view object as a grid table message
2d66e025 6502 }
a9339fe2 6503
ca65c044 6504 return false;
2d66e025 6505}
f85afd4e 6506
2d66e025
MB
6507bool wxGrid::InsertCols( int pos, int numCols, bool WXUNUSED(updateLabels) )
6508{
6509 // TODO: something with updateLabels flag
8f177c8e 6510
2d66e025
MB
6511 if ( !m_created )
6512 {
bd3c6758 6513 wxFAIL_MSG( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") );
ca65c044 6514 return false;
2d66e025 6515 }
f85afd4e 6516
2d66e025
MB
6517 if ( m_table )
6518 {
b7fff980 6519 if (IsCellEditControlEnabled())
b54ba671 6520 DisableCellEditControl();
b7fff980 6521
4ea3479c 6522 bool done = m_table->InsertCols( pos, numCols );
4ea3479c 6523 return done;
2d66e025
MB
6524 // the table will have sent the results of the insert col
6525 // operation to this view object as a grid table message
f85afd4e 6526 }
2f024384 6527
ca65c044 6528 return false;
f85afd4e
MB
6529}
6530
2d66e025 6531bool wxGrid::AppendCols( int numCols, bool WXUNUSED(updateLabels) )
f85afd4e 6532{
2d66e025
MB
6533 // TODO: something with updateLabels flag
6534
6535 if ( !m_created )
f85afd4e 6536 {
bd3c6758 6537 wxFAIL_MSG( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") );
ca65c044 6538 return false;
2d66e025 6539 }
f85afd4e 6540
4ea3479c
JS
6541 if ( m_table )
6542 {
6543 bool done = m_table->AppendCols( numCols );
4ea3479c
JS
6544 return done;
6545 // the table will have sent the results of the append col
6546 // operation to this view object as a grid table message
6547 }
2f024384 6548
ca65c044 6549 return false;
f85afd4e
MB
6550}
6551
2d66e025 6552bool wxGrid::DeleteCols( int pos, int numCols, bool WXUNUSED(updateLabels) )
f85afd4e 6553{
2d66e025 6554 // TODO: something with updateLabels flag
8f177c8e 6555
2d66e025 6556 if ( !m_created )
f85afd4e 6557 {
bd3c6758 6558 wxFAIL_MSG( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") );
ca65c044 6559 return false;
f85afd4e
MB
6560 }
6561
b7fff980 6562 if ( m_table )
2d66e025 6563 {
b7fff980 6564 if (IsCellEditControlEnabled())
b54ba671 6565 DisableCellEditControl();
8f177c8e 6566
4ea3479c 6567 bool done = m_table->DeleteCols( pos, numCols );
4ea3479c 6568 return done;
f6bcfd97
BP
6569 // the table will have sent the results of the delete col
6570 // operation to this view object as a grid table message
f85afd4e 6571 }
2f024384 6572
ca65c044 6573 return false;
f85afd4e
MB
6574}
6575
2d66e025
MB
6576//
6577// ----- event handlers
f85afd4e 6578//
8f177c8e 6579
2d66e025
MB
6580// Generate a grid event based on a mouse event and
6581// return the result of ProcessEvent()
6582//
fe7b9ed6 6583int wxGrid::SendEvent( const wxEventType type,
2d66e025
MB
6584 int row, int col,
6585 wxMouseEvent& mouseEv )
6586{
2f024384 6587 bool claimed, vetoed;
fe7b9ed6 6588
97a9929e
VZ
6589 if ( type == wxEVT_GRID_ROW_SIZE || type == wxEVT_GRID_COL_SIZE )
6590 {
6591 int rowOrCol = (row == -1 ? col : row);
6592
6593 wxGridSizeEvent gridEvt( GetId(),
6594 type,
6595 this,
6596 rowOrCol,
6597 mouseEv.GetX() + GetRowLabelSize(),
6598 mouseEv.GetY() + GetColLabelSize(),
6599 mouseEv.ControlDown(),
6600 mouseEv.ShiftDown(),
6601 mouseEv.AltDown(),
6602 mouseEv.MetaDown() );
6603
6604 claimed = GetEventHandler()->ProcessEvent(gridEvt);
6605 vetoed = !gridEvt.IsAllowed();
6606 }
fe7b9ed6 6607 else if ( type == wxEVT_GRID_RANGE_SELECT )
97a9929e
VZ
6608 {
6609 // Right now, it should _never_ end up here!
6610 wxGridRangeSelectEvent gridEvt( GetId(),
6611 type,
6612 this,
6613 m_selectingTopLeft,
6614 m_selectingBottomRight,
ca65c044 6615 true,
97a9929e
VZ
6616 mouseEv.ControlDown(),
6617 mouseEv.ShiftDown(),
6618 mouseEv.AltDown(),
6619 mouseEv.MetaDown() );
6620
6621 claimed = GetEventHandler()->ProcessEvent(gridEvt);
6622 vetoed = !gridEvt.IsAllowed();
6623 }
2b73a34e
RD
6624 else if ( type == wxEVT_GRID_LABEL_LEFT_CLICK ||
6625 type == wxEVT_GRID_LABEL_LEFT_DCLICK ||
6626 type == wxEVT_GRID_LABEL_RIGHT_CLICK ||
6627 type == wxEVT_GRID_LABEL_RIGHT_DCLICK )
6628 {
6629 wxPoint pos = mouseEv.GetPosition();
6630
6631 if ( mouseEv.GetEventObject() == GetGridRowLabelWindow() )
6632 pos.y += GetColLabelSize();
6633 if ( mouseEv.GetEventObject() == GetGridColLabelWindow() )
6634 pos.x += GetRowLabelSize();
6635
6636 wxGridEvent gridEvt( GetId(),
6637 type,
6638 this,
6639 row, col,
6640 pos.x,
6641 pos.y,
6642 false,
6643 mouseEv.ControlDown(),
6644 mouseEv.ShiftDown(),
6645 mouseEv.AltDown(),
6646 mouseEv.MetaDown() );
6647 claimed = GetEventHandler()->ProcessEvent(gridEvt);
6648 vetoed = !gridEvt.IsAllowed();
6649 }
fe7b9ed6 6650 else
97a9929e
VZ
6651 {
6652 wxGridEvent gridEvt( GetId(),
6653 type,
6654 this,
6655 row, col,
6656 mouseEv.GetX() + GetRowLabelSize(),
6657 mouseEv.GetY() + GetColLabelSize(),
ca65c044 6658 false,
97a9929e
VZ
6659 mouseEv.ControlDown(),
6660 mouseEv.ShiftDown(),
6661 mouseEv.AltDown(),
6662 mouseEv.MetaDown() );
6663 claimed = GetEventHandler()->ProcessEvent(gridEvt);
6664 vetoed = !gridEvt.IsAllowed();
6665 }
6666
6667 // A Veto'd event may not be `claimed' so test this first
4db6714b
KH
6668 if (vetoed)
6669 return -1;
6670
97a9929e 6671 return claimed ? 1 : 0;
f85afd4e
MB
6672}
6673
2d66e025
MB
6674// Generate a grid event of specified type and return the result
6675// of ProcessEvent().
f85afd4e 6676//
fe7b9ed6 6677int wxGrid::SendEvent( const wxEventType type,
2d66e025 6678 int row, int col )
f85afd4e 6679{
a9339fe2 6680 bool claimed, vetoed;
fe7b9ed6 6681
b54ba671 6682 if ( type == wxEVT_GRID_ROW_SIZE || type == wxEVT_GRID_COL_SIZE )
f85afd4e 6683 {
2d66e025 6684 int rowOrCol = (row == -1 ? col : row);
f85afd4e 6685
a9339fe2 6686 wxGridSizeEvent gridEvt( GetId(), type, this, rowOrCol );
2d66e025 6687
fe7b9ed6
VZ
6688 claimed = GetEventHandler()->ProcessEvent(gridEvt);
6689 vetoed = !gridEvt.IsAllowed();
2d66e025
MB
6690 }
6691 else
6692 {
a9339fe2 6693 wxGridEvent gridEvt( GetId(), type, this, row, col );
8f177c8e 6694
fe7b9ed6
VZ
6695 claimed = GetEventHandler()->ProcessEvent(gridEvt);
6696 vetoed = !gridEvt.IsAllowed();
6697 }
6698
97a9929e 6699 // A Veto'd event may not be `claimed' so test this first
4db6714b
KH
6700 if (vetoed)
6701 return -1;
6702
97a9929e 6703 return claimed ? 1 : 0;
f85afd4e
MB
6704}
6705
2d66e025 6706void wxGrid::OnPaint( wxPaintEvent& WXUNUSED(event) )
f85afd4e 6707{
3d59537f
DS
6708 // needed to prevent zillions of paint events on MSW
6709 wxPaintDC dc(this);
8f177c8e 6710}
f85afd4e 6711
bca7bfc8 6712void wxGrid::Refresh(bool eraseb, const wxRect* rect)
27f35b66 6713{
ad603bf7
VZ
6714 // Don't do anything if between Begin/EndBatch...
6715 // EndBatch() will do all this on the last nested one anyway.
27f35b66
SN
6716 if (! GetBatchCount())
6717 {
bca7bfc8 6718 // Refresh to get correct scrolled position:
4db6714b 6719 wxScrolledWindow::Refresh(eraseb, rect);
27f35b66 6720
27f35b66
SN
6721 if (rect)
6722 {
bca7bfc8
SN
6723 int rect_x, rect_y, rectWidth, rectHeight;
6724 int width_label, width_cell, height_label, height_cell;
6725 int x, y;
6726
2f024384 6727 // Copy rectangle can get scroll offsets..
bca7bfc8
SN
6728 rect_x = rect->GetX();
6729 rect_y = rect->GetY();
6730 rectWidth = rect->GetWidth();
6731 rectHeight = rect->GetHeight();
27f35b66 6732
bca7bfc8 6733 width_label = m_rowLabelWidth - rect_x;
4db6714b
KH
6734 if (width_label > rectWidth)
6735 width_label = rectWidth;
27f35b66 6736
bca7bfc8 6737 height_label = m_colLabelHeight - rect_y;
a9339fe2
DS
6738 if (height_label > rectHeight)
6739 height_label = rectHeight;
27f35b66 6740
bca7bfc8
SN
6741 if (rect_x > m_rowLabelWidth)
6742 {
6743 x = rect_x - m_rowLabelWidth;
6744 width_cell = rectWidth;
6745 }
6746 else
6747 {
6748 x = 0;
6749 width_cell = rectWidth - (m_rowLabelWidth - rect_x);
6750 }
6751
6752 if (rect_y > m_colLabelHeight)
6753 {
6754 y = rect_y - m_colLabelHeight;
6755 height_cell = rectHeight;
6756 }
6757 else
6758 {
6759 y = 0;
6760 height_cell = rectHeight - (m_colLabelHeight - rect_y);
6761 }
6762
6763 // Paint corner label part intersecting rect.
6764 if ( width_label > 0 && height_label > 0 )
6765 {
6766 wxRect anotherrect(rect_x, rect_y, width_label, height_label);
6767 m_cornerLabelWin->Refresh(eraseb, &anotherrect);
6768 }
6769
6770 // Paint col labels part intersecting rect.
6771 if ( width_cell > 0 && height_label > 0 )
6772 {
6773 wxRect anotherrect(x, rect_y, width_cell, height_label);
6774 m_colLabelWin->Refresh(eraseb, &anotherrect);
6775 }
6776
6777 // Paint row labels part intersecting rect.
6778 if ( width_label > 0 && height_cell > 0 )
6779 {
6780 wxRect anotherrect(rect_x, y, width_label, height_cell);
6781 m_rowLabelWin->Refresh(eraseb, &anotherrect);
6782 }
6783
6784 // Paint cell area part intersecting rect.
6785 if ( width_cell > 0 && height_cell > 0 )
6786 {
6787 wxRect anotherrect(x, y, width_cell, height_cell);
6788 m_gridWin->Refresh(eraseb, &anotherrect);
6789 }
6790 }
6791 else
6792 {
6793 m_cornerLabelWin->Refresh(eraseb, NULL);
2b5f62a0 6794 m_colLabelWin->Refresh(eraseb, NULL);
bca7bfc8
SN
6795 m_rowLabelWin->Refresh(eraseb, NULL);
6796 m_gridWin->Refresh(eraseb, NULL);
6797 }
27f35b66
SN
6798 }
6799}
f85afd4e 6800
97a9929e 6801void wxGrid::OnSize( wxSizeEvent& event )
f85afd4e 6802{
97a9929e 6803 // position the child windows
7807d81c 6804 CalcWindowSizes();
97a9929e
VZ
6805
6806 // don't call CalcDimensions() from here, the base class handles the size
6807 // changes itself
6808 event.Skip();
f85afd4e
MB
6809}
6810
2d66e025 6811void wxGrid::OnKeyDown( wxKeyEvent& event )
f85afd4e 6812{
2d66e025 6813 if ( m_inOnKeyDown )
f85afd4e 6814 {
2d66e025
MB
6815 // shouldn't be here - we are going round in circles...
6816 //
07296f0b 6817 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") );
f85afd4e
MB
6818 }
6819
ca65c044 6820 m_inOnKeyDown = true;
f85afd4e 6821
2d66e025 6822 // propagate the event up and see if it gets processed
2d66e025
MB
6823 wxWindow *parent = GetParent();
6824 wxKeyEvent keyEvt( event );
6825 keyEvt.SetEventObject( parent );
6826
6827 if ( !parent->GetEventHandler()->ProcessEvent( keyEvt ) )
f85afd4e 6828 {
2d66e025 6829 // try local handlers
12a3f227 6830 switch ( event.GetKeyCode() )
2d66e025
MB
6831 {
6832 case WXK_UP:
6833 if ( event.ControlDown() )
5c8fc7c1 6834 MoveCursorUpBlock( event.ShiftDown() );
2d66e025 6835 else
5c8fc7c1 6836 MoveCursorUp( event.ShiftDown() );
2d66e025 6837 break;
f85afd4e 6838
2d66e025
MB
6839 case WXK_DOWN:
6840 if ( event.ControlDown() )
5c8fc7c1 6841 MoveCursorDownBlock( event.ShiftDown() );
2d66e025 6842 else
5c8fc7c1 6843 MoveCursorDown( event.ShiftDown() );
2d66e025 6844 break;
8f177c8e 6845
2d66e025
MB
6846 case WXK_LEFT:
6847 if ( event.ControlDown() )
5c8fc7c1 6848 MoveCursorLeftBlock( event.ShiftDown() );
2d66e025 6849 else
5c8fc7c1 6850 MoveCursorLeft( event.ShiftDown() );
2d66e025
MB
6851 break;
6852
6853 case WXK_RIGHT:
6854 if ( event.ControlDown() )
5c8fc7c1 6855 MoveCursorRightBlock( event.ShiftDown() );
2d66e025 6856 else
5c8fc7c1 6857 MoveCursorRight( event.ShiftDown() );
2d66e025 6858 break;
b99be8fb 6859
2d66e025 6860 case WXK_RETURN:
a4f7bf58 6861 case WXK_NUMPAD_ENTER:
58dd5b3b
MB
6862 if ( event.ControlDown() )
6863 {
6864 event.Skip(); // to let the edit control have the return
6865 }
6866 else
6867 {
f6bcfd97
BP
6868 if ( GetGridCursorRow() < GetNumberRows()-1 )
6869 {
6870 MoveCursorDown( event.ShiftDown() );
6871 }
6872 else
6873 {
6874 // at the bottom of a column
13f6e9e8 6875 DisableCellEditControl();
f6bcfd97 6876 }
58dd5b3b 6877 }
2d66e025
MB
6878 break;
6879
5c8fc7c1 6880 case WXK_ESCAPE:
e32352cf 6881 ClearSelection();
5c8fc7c1
SN
6882 break;
6883
2c9a89e0
RD
6884 case WXK_TAB:
6885 if (event.ShiftDown())
f6bcfd97
BP
6886 {
6887 if ( GetGridCursorCol() > 0 )
6888 {
ca65c044 6889 MoveCursorLeft( false );
f6bcfd97
BP
6890 }
6891 else
6892 {
6893 // at left of grid
13f6e9e8 6894 DisableCellEditControl();
f6bcfd97
BP
6895 }
6896 }
2c9a89e0 6897 else
f6bcfd97 6898 {
ccdee36f 6899 if ( GetGridCursorCol() < GetNumberCols() - 1 )
f6bcfd97 6900 {
ca65c044 6901 MoveCursorRight( false );
f6bcfd97
BP
6902 }
6903 else
6904 {
6905 // at right of grid
13f6e9e8 6906 DisableCellEditControl();
f6bcfd97
BP
6907 }
6908 }
2c9a89e0
RD
6909 break;
6910
2d66e025
MB
6911 case WXK_HOME:
6912 if ( event.ControlDown() )
6913 {
6914 MakeCellVisible( 0, 0 );
6915 SetCurrentCell( 0, 0 );
6916 }
6917 else
6918 {
6919 event.Skip();
6920 }
6921 break;
6922
6923 case WXK_END:
6924 if ( event.ControlDown() )
6925 {
a9339fe2
DS
6926 MakeCellVisible( m_numRows - 1, m_numCols - 1 );
6927 SetCurrentCell( m_numRows - 1, m_numCols - 1 );
2d66e025
MB
6928 }
6929 else
6930 {
6931 event.Skip();
6932 }
6933 break;
6934
faa94f3e 6935 case WXK_PAGEUP:
2d66e025
MB
6936 MovePageUp();
6937 break;
6938
faa94f3e 6939 case WXK_PAGEDOWN:
2d66e025
MB
6940 MovePageDown();
6941 break;
6942
07296f0b 6943 case WXK_SPACE:
aa5e1f75
SN
6944 if ( event.ControlDown() )
6945 {
3f3dc2ef
VZ
6946 if ( m_selection )
6947 {
a9339fe2
DS
6948 m_selection->ToggleCellSelection(
6949 m_currentCellCoords.GetRow(),
6950 m_currentCellCoords.GetCol(),
6951 event.ControlDown(),
6952 event.ShiftDown(),
6953 event.AltDown(),
6954 event.MetaDown() );
3f3dc2ef 6955 }
aa5e1f75
SN
6956 break;
6957 }
ccdee36f 6958
07296f0b 6959 if ( !IsEditable() )
ca65c044 6960 MoveCursorRight( false );
ccdee36f
DS
6961 else
6962 event.Skip();
6963 break;
07296f0b 6964
2d66e025 6965 default:
63e2147c 6966 event.Skip();
025562fe 6967 break;
2d66e025 6968 }
f85afd4e
MB
6969 }
6970
ca65c044 6971 m_inOnKeyDown = false;
f85afd4e
MB
6972}
6973
f6bcfd97
BP
6974void wxGrid::OnKeyUp( wxKeyEvent& event )
6975{
6976 // try local handlers
6977 //
12a3f227 6978 if ( event.GetKeyCode() == WXK_SHIFT )
f6bcfd97
BP
6979 {
6980 if ( m_selectingTopLeft != wxGridNoCellCoords &&
6981 m_selectingBottomRight != wxGridNoCellCoords )
3f3dc2ef
VZ
6982 {
6983 if ( m_selection )
6984 {
a9339fe2
DS
6985 m_selection->SelectBlock(
6986 m_selectingTopLeft.GetRow(),
6987 m_selectingTopLeft.GetCol(),
6988 m_selectingBottomRight.GetRow(),
6989 m_selectingBottomRight.GetCol(),
6990 event.ControlDown(),
6991 true,
6992 event.AltDown(),
6993 event.MetaDown() );
3f3dc2ef
VZ
6994 }
6995 }
6996
f6bcfd97
BP
6997 m_selectingTopLeft = wxGridNoCellCoords;
6998 m_selectingBottomRight = wxGridNoCellCoords;
6999 m_selectingKeyboard = wxGridNoCellCoords;
7000 }
7001}
7002
63e2147c
RD
7003void wxGrid::OnChar( wxKeyEvent& event )
7004{
7005 // is it possible to edit the current cell at all?
7006 if ( !IsCellEditControlEnabled() && CanEnableCellControl() )
7007 {
7008 // yes, now check whether the cells editor accepts the key
7009 int row = m_currentCellCoords.GetRow();
7010 int col = m_currentCellCoords.GetCol();
2f024384 7011 wxGridCellAttr *attr = GetCellAttr(row, col);
63e2147c
RD
7012 wxGridCellEditor *editor = attr->GetEditor(this, row, col);
7013
7014 // <F2> is special and will always start editing, for
7015 // other keys - ask the editor itself
7016 if ( (event.GetKeyCode() == WXK_F2 && !event.HasModifiers())
7017 || editor->IsAcceptedKey(event) )
7018 {
7019 // ensure cell is visble
7020 MakeCellVisible(row, col);
7021 EnableCellEditControl();
7022
7023 // a problem can arise if the cell is not completely
7024 // visible (even after calling MakeCellVisible the
7025 // control is not created and calling StartingKey will
7026 // crash the app
046d682f 7027 if ( event.GetKeyCode() != WXK_F2 && editor->IsCreated() && m_cellEditCtrlEnabled )
63e2147c
RD
7028 editor->StartingKey(event);
7029 }
7030 else
7031 {
7032 event.Skip();
7033 }
7034
7035 editor->DecRef();
7036 attr->DecRef();
7037 }
7038 else
7039 {
7040 event.Skip();
7041 }
7042}
7043
2796cce3 7044void wxGrid::OnEraseBackground(wxEraseEvent&)
508011ce
VZ
7045{
7046}
07296f0b 7047
2d66e025 7048void wxGrid::SetCurrentCell( const wxGridCellCoords& coords )
66242c80 7049{
f6bcfd97
BP
7050 if ( SendEvent( wxEVT_GRID_SELECT_CELL, coords.GetRow(), coords.GetCol() ) )
7051 {
7052 // the event has been intercepted - do nothing
7053 return;
7054 }
7055
bee19958
DS
7056 wxClientDC dc( m_gridWin );
7057 PrepareDC( dc );
f6bcfd97 7058
2a7750d9 7059 if ( m_currentCellCoords != wxGridNoCellCoords )
2d66e025 7060 {
b54ba671 7061 DisableCellEditControl();
07296f0b 7062
ca65c044 7063 if ( IsVisible( m_currentCellCoords, false ) )
f6bcfd97
BP
7064 {
7065 wxRect r;
bee19958 7066 r = BlockToDeviceRect( m_currentCellCoords, m_currentCellCoords );
f6bcfd97
BP
7067 if ( !m_gridLinesEnabled )
7068 {
7069 r.x--;
7070 r.y--;
7071 r.width++;
7072 r.height++;
7073 }
d1c0b4f9 7074
3ed884a0 7075 wxGridCellCoordsArray cells = CalcCellsExposed( r );
84912ef8 7076
f6bcfd97
BP
7077 // Otherwise refresh redraws the highlight!
7078 m_currentCellCoords = coords;
275c4ae4 7079
bee19958 7080 DrawGridCellArea( dc, cells );
f6bcfd97
BP
7081 DrawAllGridLines( dc, r );
7082 }
66242c80 7083 }
8f177c8e 7084
2d66e025
MB
7085 m_currentCellCoords = coords;
7086
bee19958
DS
7087 wxGridCellAttr *attr = GetCellAttr( coords );
7088 DrawCellHighlight( dc, attr );
2a7750d9 7089 attr->DecRef();
66242c80
MB
7090}
7091
c9097836
MB
7092void wxGrid::HighlightBlock( int topRow, int leftCol, int bottomRow, int rightCol )
7093{
7094 int temp;
7095 wxGridCellCoords updateTopLeft, updateBottomRight;
7096
3f3dc2ef 7097 if ( m_selection )
c9097836 7098 {
3f3dc2ef
VZ
7099 if ( m_selection->GetSelectionMode() == wxGrid::wxGridSelectRows )
7100 {
7101 leftCol = 0;
7102 rightCol = GetNumberCols() - 1;
7103 }
7104 else if ( m_selection->GetSelectionMode() == wxGrid::wxGridSelectColumns )
7105 {
7106 topRow = 0;
7107 bottomRow = GetNumberRows() - 1;
7108 }
c9097836 7109 }
3f3dc2ef 7110
c9097836
MB
7111 if ( topRow > bottomRow )
7112 {
7113 temp = topRow;
7114 topRow = bottomRow;
7115 bottomRow = temp;
7116 }
7117
7118 if ( leftCol > rightCol )
7119 {
7120 temp = leftCol;
7121 leftCol = rightCol;
7122 rightCol = temp;
7123 }
7124
7125 updateTopLeft = wxGridCellCoords( topRow, leftCol );
7126 updateBottomRight = wxGridCellCoords( bottomRow, rightCol );
7127
3ed884a0
SN
7128 // First the case that we selected a completely new area
7129 if ( m_selectingTopLeft == wxGridNoCellCoords ||
7130 m_selectingBottomRight == wxGridNoCellCoords )
7131 {
7132 wxRect rect;
7133 rect = BlockToDeviceRect( wxGridCellCoords ( topRow, leftCol ),
7134 wxGridCellCoords ( bottomRow, rightCol ) );
ca65c044 7135 m_gridWin->Refresh( false, &rect );
3ed884a0 7136 }
2f024384 7137
3ed884a0
SN
7138 // Now handle changing an existing selection area.
7139 else if ( m_selectingTopLeft != updateTopLeft ||
7140 m_selectingBottomRight != updateBottomRight )
c9097836
MB
7141 {
7142 // Compute two optimal update rectangles:
7143 // Either one rectangle is a real subset of the
7144 // other, or they are (almost) disjoint!
7145 wxRect rect[4];
7146 bool need_refresh[4];
7147 need_refresh[0] =
7148 need_refresh[1] =
7149 need_refresh[2] =
ca65c044 7150 need_refresh[3] = false;
c9097836
MB
7151 int i;
7152
7153 // Store intermediate values
a9339fe2
DS
7154 wxCoord oldLeft = m_selectingTopLeft.GetCol();
7155 wxCoord oldTop = m_selectingTopLeft.GetRow();
7156 wxCoord oldRight = m_selectingBottomRight.GetCol();
c9097836
MB
7157 wxCoord oldBottom = m_selectingBottomRight.GetRow();
7158
7159 // Determine the outer/inner coordinates.
7160 if (oldLeft > leftCol)
7161 {
7162 temp = oldLeft;
7163 oldLeft = leftCol;
7164 leftCol = temp;
7165 }
7166 if (oldTop > topRow )
7167 {
7168 temp = oldTop;
7169 oldTop = topRow;
7170 topRow = temp;
7171 }
7172 if (oldRight < rightCol )
7173 {
7174 temp = oldRight;
7175 oldRight = rightCol;
7176 rightCol = temp;
7177 }
7178 if (oldBottom < bottomRow)
7179 {
7180 temp = oldBottom;
7181 oldBottom = bottomRow;
7182 bottomRow = temp;
7183 }
7184
7185 // Now, either the stuff marked old is the outer
7186 // rectangle or we don't have a situation where one
7187 // is contained in the other.
7188
7189 if ( oldLeft < leftCol )
7190 {
3ed884a0
SN
7191 // Refresh the newly selected or deselected
7192 // area to the left of the old or new selection.
ca65c044 7193 need_refresh[0] = true;
a9339fe2
DS
7194 rect[0] = BlockToDeviceRect(
7195 wxGridCellCoords( oldTop, oldLeft ),
7196 wxGridCellCoords( oldBottom, leftCol - 1 ) );
c9097836
MB
7197 }
7198
2f024384 7199 if ( oldTop < topRow )
c9097836 7200 {
3ed884a0
SN
7201 // Refresh the newly selected or deselected
7202 // area above the old or new selection.
ca65c044 7203 need_refresh[1] = true;
2f024384
DS
7204 rect[1] = BlockToDeviceRect(
7205 wxGridCellCoords( oldTop, leftCol ),
7206 wxGridCellCoords( topRow - 1, rightCol ) );
c9097836
MB
7207 }
7208
7209 if ( oldRight > rightCol )
7210 {
3ed884a0
SN
7211 // Refresh the newly selected or deselected
7212 // area to the right of the old or new selection.
ca65c044 7213 need_refresh[2] = true;
2f024384 7214 rect[2] = BlockToDeviceRect(
a9339fe2
DS
7215 wxGridCellCoords( oldTop, rightCol + 1 ),
7216 wxGridCellCoords( oldBottom, oldRight ) );
c9097836
MB
7217 }
7218
7219 if ( oldBottom > bottomRow )
7220 {
3ed884a0
SN
7221 // Refresh the newly selected or deselected
7222 // area below the old or new selection.
ca65c044 7223 need_refresh[3] = true;
2f024384 7224 rect[3] = BlockToDeviceRect(
a9339fe2
DS
7225 wxGridCellCoords( bottomRow + 1, leftCol ),
7226 wxGridCellCoords( oldBottom, rightCol ) );
c9097836
MB
7227 }
7228
c9097836
MB
7229 // various Refresh() calls
7230 for (i = 0; i < 4; i++ )
7231 if ( need_refresh[i] && rect[i] != wxGridNoCellRect )
ca65c044 7232 m_gridWin->Refresh( false, &(rect[i]) );
c9097836 7233 }
2f024384
DS
7234
7235 // change selection
3ed884a0
SN
7236 m_selectingTopLeft = updateTopLeft;
7237 m_selectingBottomRight = updateBottomRight;
c9097836
MB
7238}
7239
2d66e025
MB
7240//
7241// ------ functions to get/send data (see also public functions)
7242//
7243
7244bool wxGrid::GetModelValues()
66242c80 7245{
bca7bfc8
SN
7246 // Hide the editor, so it won't hide a changed value.
7247 HideCellEditControl();
c6707d16 7248
2d66e025 7249 if ( m_table )
66242c80 7250 {
2d66e025 7251 // all we need to do is repaint the grid
66242c80 7252 //
2d66e025 7253 m_gridWin->Refresh();
ca65c044 7254 return true;
66242c80 7255 }
8f177c8e 7256
ca65c044 7257 return false;
66242c80
MB
7258}
7259
2d66e025 7260bool wxGrid::SetModelValues()
f85afd4e 7261{
2d66e025 7262 int row, col;
8f177c8e 7263
c6707d16
SN
7264 // Disable the editor, so it won't hide a changed value.
7265 // Do we also want to save the current value of the editor first?
7266 // I think so ...
c6707d16
SN
7267 DisableCellEditControl();
7268
2d66e025
MB
7269 if ( m_table )
7270 {
56b6cf26 7271 for ( row = 0; row < m_numRows; row++ )
f85afd4e 7272 {
56b6cf26 7273 for ( col = 0; col < m_numCols; col++ )
f85afd4e 7274 {
2d66e025 7275 m_table->SetValue( row, col, GetCellValue(row, col) );
f85afd4e
MB
7276 }
7277 }
8f177c8e 7278
ca65c044 7279 return true;
f85afd4e
MB
7280 }
7281
ca65c044 7282 return false;
f85afd4e
MB
7283}
7284
2d66e025
MB
7285// Note - this function only draws cells that are in the list of
7286// exposed cells (usually set from the update region by
7287// CalcExposedCells)
7288//
d10f4bf9 7289void wxGrid::DrawGridCellArea( wxDC& dc, const wxGridCellCoordsArray& cells )
f85afd4e 7290{
4db6714b
KH
7291 if ( !m_numRows || !m_numCols )
7292 return;
60ff3b99 7293
dc1f566f 7294 int i, numCells = cells.GetCount();
27f35b66
SN
7295 int row, col, cell_rows, cell_cols;
7296 wxGridCellCoordsArray redrawCells;
60ff3b99 7297
a9339fe2 7298 for ( i = numCells - 1; i >= 0; i-- )
f85afd4e 7299 {
27f35b66
SN
7300 row = cells[i].GetRow();
7301 col = cells[i].GetCol();
7302 GetCellSize( row, col, &cell_rows, &cell_cols );
7303
7304 // If this cell is part of a multicell block, find owner for repaint
7305 if ( cell_rows <= 0 || cell_cols <= 0 )
7306 {
a9339fe2 7307 wxGridCellCoords cell( row + cell_rows, col + cell_cols );
ca65c044 7308 bool marked = false;
56b6cf26 7309 for ( int j = 0; j < numCells; j++ )
27f35b66
SN
7310 {
7311 if ( cell == cells[j] )
7312 {
ca65c044 7313 marked = true;
3ed884a0 7314 break;
27f35b66
SN
7315 }
7316 }
2f024384 7317
27f35b66
SN
7318 if (!marked)
7319 {
7320 int count = redrawCells.GetCount();
dc1f566f 7321 for (int j = 0; j < count; j++)
27f35b66
SN
7322 {
7323 if ( cell == redrawCells[j] )
7324 {
ca65c044 7325 marked = true;
27f35b66
SN
7326 break;
7327 }
7328 }
2f024384 7329
4db6714b
KH
7330 if (!marked)
7331 redrawCells.Add( cell );
27f35b66 7332 }
2f024384
DS
7333
7334 // don't bother drawing this cell
7335 continue;
27f35b66
SN
7336 }
7337
7338 // If this cell is empty, find cell to left that might want to overflow
7339 if (m_table && m_table->IsEmptyCell(row, col))
7340 {
dc1f566f 7341 for ( int l = 0; l < cell_rows; l++ )
27f35b66 7342 {
56b6cf26 7343 // find a cell in this row to leave already marked for repaint
dc1f566f
SN
7344 int left = col;
7345 for (int k = 0; k < int(redrawCells.GetCount()); k++)
7346 if ((redrawCells[k].GetCol() < left) &&
7347 (redrawCells[k].GetRow() == row))
2f024384 7348 {
4db6714b 7349 left = redrawCells[k].GetCol();
2f024384 7350 }
dc1f566f 7351
4db6714b
KH
7352 if (left == col)
7353 left = 0; // oh well
dc1f566f 7354
2f024384 7355 for (int j = col - 1; j >= left; j--)
27f35b66 7356 {
2f024384 7357 if (!m_table->IsEmptyCell(row + l, j))
27f35b66 7358 {
2f024384 7359 if (GetCellOverflow(row + l, j))
27f35b66 7360 {
2f024384 7361 wxGridCellCoords cell(row + l, j);
ca65c044 7362 bool marked = false;
27f35b66 7363
dc1f566f 7364 for (int k = 0; k < numCells; k++)
27f35b66
SN
7365 {
7366 if ( cell == cells[k] )
7367 {
ca65c044 7368 marked = true;
27f35b66
SN
7369 break;
7370 }
7371 }
4db6714b 7372
27f35b66
SN
7373 if (!marked)
7374 {
7375 int count = redrawCells.GetCount();
dc1f566f 7376 for (int k = 0; k < count; k++)
27f35b66
SN
7377 {
7378 if ( cell == redrawCells[k] )
7379 {
ca65c044 7380 marked = true;
27f35b66
SN
7381 break;
7382 }
7383 }
4db6714b
KH
7384 if (!marked)
7385 redrawCells.Add( cell );
27f35b66
SN
7386 }
7387 }
7388 break;
7389 }
7390 }
7391 }
7392 }
4db6714b 7393
d10f4bf9 7394 DrawCell( dc, cells[i] );
2d66e025 7395 }
27f35b66
SN
7396
7397 numCells = redrawCells.GetCount();
7398
56b6cf26 7399 for ( i = numCells - 1; i >= 0; i-- )
27f35b66
SN
7400 {
7401 DrawCell( dc, redrawCells[i] );
7402 }
2d66e025 7403}
8f177c8e 7404
7c8a8ad5
MB
7405void wxGrid::DrawGridSpace( wxDC& dc )
7406{
f6bcfd97
BP
7407 int cw, ch;
7408 m_gridWin->GetClientSize( &cw, &ch );
7c8a8ad5 7409
f6bcfd97
BP
7410 int right, bottom;
7411 CalcUnscrolledPosition( cw, ch, &right, &bottom );
7c8a8ad5 7412
d4175745 7413 int rightCol = m_numCols > 0 ? GetColRight(GetColAt( m_numCols - 1 )) : 0;
2f024384 7414 int bottomRow = m_numRows > 0 ? GetRowBottom(m_numRows - 1) : 0;
7c8a8ad5 7415
f6bcfd97
BP
7416 if ( right > rightCol || bottom > bottomRow )
7417 {
7418 int left, top;
7419 CalcUnscrolledPosition( 0, 0, &left, &top );
7c8a8ad5 7420
f6bcfd97
BP
7421 dc.SetBrush( wxBrush(GetDefaultCellBackgroundColour(), wxSOLID) );
7422 dc.SetPen( *wxTRANSPARENT_PEN );
7c8a8ad5 7423
f6bcfd97
BP
7424 if ( right > rightCol )
7425 {
a9339fe2 7426 dc.DrawRectangle( rightCol, top, right - rightCol, ch );
f6bcfd97
BP
7427 }
7428
7429 if ( bottom > bottomRow )
7430 {
a9339fe2 7431 dc.DrawRectangle( left, bottomRow, cw, bottom - bottomRow );
f6bcfd97
BP
7432 }
7433 }
7c8a8ad5
MB
7434}
7435
2d66e025
MB
7436void wxGrid::DrawCell( wxDC& dc, const wxGridCellCoords& coords )
7437{
ab79958a
VZ
7438 int row = coords.GetRow();
7439 int col = coords.GetCol();
60ff3b99 7440
7c1cb261 7441 if ( GetColWidth(col) <= 0 || GetRowHeight(row) <= 0 )
ab79958a
VZ
7442 return;
7443
7444 // we draw the cell border ourselves
9496deb5 7445#if !WXGRID_DRAW_LINES
2d66e025
MB
7446 if ( m_gridLinesEnabled )
7447 DrawCellBorder( dc, coords );
796df70a 7448#endif
f85afd4e 7449
189d0213
VZ
7450 wxGridCellAttr* attr = GetCellAttr(row, col);
7451
7452 bool isCurrent = coords == m_currentCellCoords;
508011ce 7453
f6bcfd97 7454 wxRect rect = CellToRect( row, col );
f85afd4e 7455
189d0213 7456 // if the editor is shown, we should use it and not the renderer
f6bcfd97
BP
7457 // Note: However, only if it is really _shown_, i.e. not hidden!
7458 if ( isCurrent && IsCellEditControlShown() )
189d0213 7459 {
a9339fe2 7460 // NB: this "#if..." is temporary and fixes a problem where the
962a48f6
DS
7461 // edit control is erased by this code after being rendered.
7462 // On wxMac (QD build only), the cell editor is a wxTextCntl and is rendered
7463 // implicitly, causing this out-of order render.
7464#if !defined(__WXMAC__) || wxMAC_USE_CORE_GRAPHICS
0b190b0f
VZ
7465 wxGridCellEditor *editor = attr->GetEditor(this, row, col);
7466 editor->PaintBackground(rect, attr);
7467 editor->DecRef();
962a48f6 7468#endif
189d0213
VZ
7469 }
7470 else
7471 {
a9339fe2 7472 // but all the rest is drawn by the cell renderer and hence may be customized
0b190b0f
VZ
7473 wxGridCellRenderer *renderer = attr->GetRenderer(this, row, col);
7474 renderer->Draw(*this, *attr, dc, rect, row, col, IsInSelection(coords));
7475 renderer->DecRef();
189d0213 7476 }
07296f0b 7477
283b7808
VZ
7478 attr->DecRef();
7479}
07296f0b 7480
283b7808 7481void wxGrid::DrawCellHighlight( wxDC& dc, const wxGridCellAttr *attr )
07296f0b
RD
7482{
7483 int row = m_currentCellCoords.GetRow();
7484 int col = m_currentCellCoords.GetCol();
7485
7c1cb261 7486 if ( GetColWidth(col) <= 0 || GetRowHeight(row) <= 0 )
07296f0b
RD
7487 return;
7488
f6bcfd97 7489 wxRect rect = CellToRect(row, col);
07296f0b 7490
b3a7510d
VZ
7491 // hmmm... what could we do here to show that the cell is disabled?
7492 // for now, I just draw a thinner border than for the other ones, but
7493 // it doesn't look really good
b3a7510d 7494
d2520c85
RD
7495 int penWidth = attr->IsReadOnly() ? m_cellHighlightROPenWidth : m_cellHighlightPenWidth;
7496
27f35b66
SN
7497 if (penWidth > 0)
7498 {
56b6cf26
DS
7499 // The center of the drawn line is where the position/width/height of
7500 // the rectangle is actually at (on wxMSW at least), so the
7501 // size of the rectangle is reduced to compensate for the thickness of
7502 // the line. If this is too strange on non-wxMSW platforms then
d2520c85 7503 // please #ifdef this appropriately.
4db6714b
KH
7504 rect.x += penWidth / 2;
7505 rect.y += penWidth / 2;
7506 rect.width -= penWidth - 1;
7507 rect.height -= penWidth - 1;
d2520c85 7508
d2520c85 7509 // Now draw the rectangle
73145b0e
JS
7510 // use the cellHighlightColour if the cell is inside a selection, this
7511 // will ensure the cell is always visible.
4db6714b 7512 dc.SetPen(wxPen(IsInSelection(row,col) ? m_selectionForeground : m_cellHighlightColour, penWidth, wxSOLID));
d2520c85
RD
7513 dc.SetBrush(*wxTRANSPARENT_BRUSH);
7514 dc.DrawRectangle(rect);
7515 }
07296f0b 7516
283b7808 7517#if 0
2f024384 7518 // VZ: my experiments with 3D borders...
283b7808 7519
b3a7510d 7520 // how to properly set colours for arbitrary bg?
283b7808
VZ
7521 wxCoord x1 = rect.x,
7522 y1 = rect.y,
4db6714b
KH
7523 x2 = rect.x + rect.width - 1,
7524 y2 = rect.y + rect.height - 1;
283b7808
VZ
7525
7526 dc.SetPen(*wxWHITE_PEN);
00cf1208
RR
7527 dc.DrawLine(x1, y1, x2, y1);
7528 dc.DrawLine(x1, y1, x1, y2);
283b7808 7529
283b7808 7530 dc.DrawLine(x1 + 1, y2 - 1, x2 - 1, y2 - 1);
2f024384 7531 dc.DrawLine(x2 - 1, y1 + 1, x2 - 1, y2);
283b7808
VZ
7532
7533 dc.SetPen(*wxBLACK_PEN);
7534 dc.DrawLine(x1, y2, x2, y2);
2f024384 7535 dc.DrawLine(x2, y1, x2, y2 + 1);
a9339fe2 7536#endif
2d66e025 7537}
f85afd4e 7538
3d3f3e37
VZ
7539wxPen wxGrid::GetDefaultGridLinePen()
7540{
7541 return wxPen(GetGridLineColour(), 1, wxSOLID);
7542}
7543
7544wxPen wxGrid::GetRowGridLinePen(int WXUNUSED(row))
7545{
7546 return GetDefaultGridLinePen();
7547}
7548
7549wxPen wxGrid::GetColGridLinePen(int WXUNUSED(col))
7550{
7551 return GetDefaultGridLinePen();
7552}
7553
2d66e025
MB
7554void wxGrid::DrawCellBorder( wxDC& dc, const wxGridCellCoords& coords )
7555{
2d66e025
MB
7556 int row = coords.GetRow();
7557 int col = coords.GetCol();
7c1cb261
VZ
7558 if ( GetColWidth(col) <= 0 || GetRowHeight(row) <= 0 )
7559 return;
7560
60ff3b99 7561
27f35b66
SN
7562 wxRect rect = CellToRect( row, col );
7563
2d66e025 7564 // right hand border
3d3f3e37 7565 dc.SetPen( GetColGridLinePen(col) );
27f35b66
SN
7566 dc.DrawLine( rect.x + rect.width, rect.y,
7567 rect.x + rect.width, rect.y + rect.height + 1 );
2d66e025
MB
7568
7569 // bottom border
3d3f3e37 7570 dc.SetPen( GetRowGridLinePen(row) );
2f024384 7571 dc.DrawLine( rect.x, rect.y + rect.height,
27f35b66 7572 rect.x + rect.width, rect.y + rect.height);
f85afd4e
MB
7573}
7574
2f024384 7575void wxGrid::DrawHighlight(wxDC& dc, const wxGridCellCoordsArray& cells)
b3a7510d 7576{
2a7750d9
MB
7577 // This if block was previously in wxGrid::OnPaint but that doesn't
7578 // seem to get called under wxGTK - MB
7579 //
2f024384 7580 if ( m_currentCellCoords == wxGridNoCellCoords &&
2a7750d9
MB
7581 m_numRows && m_numCols )
7582 {
7583 m_currentCellCoords.Set(0, 0);
7584 }
7585
f6bcfd97 7586 if ( IsCellEditControlShown() )
99306db2
VZ
7587 {
7588 // don't show highlight when the edit control is shown
7589 return;
7590 }
7591
b3a7510d
VZ
7592 // if the active cell was repainted, repaint its highlight too because it
7593 // might have been damaged by the grid lines
d10f4bf9 7594 size_t count = cells.GetCount();
b3a7510d
VZ
7595 for ( size_t n = 0; n < count; n++ )
7596 {
d10f4bf9 7597 if ( cells[n] == m_currentCellCoords )
b3a7510d
VZ
7598 {
7599 wxGridCellAttr* attr = GetCellAttr(m_currentCellCoords);
7600 DrawCellHighlight(dc, attr);
7601 attr->DecRef();
7602
7603 break;
7604 }
7605 }
7606}
2d66e025 7607
2d66e025
MB
7608// TODO: remove this ???
7609// This is used to redraw all grid lines e.g. when the grid line colour
7610// has been changed
7611//
33ac7e6f 7612void wxGrid::DrawAllGridLines( wxDC& dc, const wxRegion & WXUNUSED(reg) )
f85afd4e 7613{
27f35b66
SN
7614#if !WXGRID_DRAW_LINES
7615 return;
7616#endif
7617
afd0f084 7618 if ( !m_gridLinesEnabled || !m_numRows || !m_numCols )
4db6714b 7619 return;
f85afd4e 7620
2d66e025 7621 int top, bottom, left, right;
796df70a 7622
f6bcfd97 7623#if 0 //#ifndef __WXGTK__
508011ce
VZ
7624 if (reg.IsEmpty())
7625 {
796df70a
SN
7626 int cw, ch;
7627 m_gridWin->GetClientSize(&cw, &ch);
7628
7629 // virtual coords of visible area
7630 //
7631 CalcUnscrolledPosition( 0, 0, &left, &top );
7632 CalcUnscrolledPosition( cw, ch, &right, &bottom );
7633 }
508011ce
VZ
7634 else
7635 {
796df70a
SN
7636 wxCoord x, y, w, h;
7637 reg.GetBox(x, y, w, h);
7638 CalcUnscrolledPosition( x, y, &left, &top );
7639 CalcUnscrolledPosition( x + w, y + h, &right, &bottom );
7640 }
8e217128
RR
7641#else
7642 int cw, ch;
7643 m_gridWin->GetClientSize(&cw, &ch);
7644 CalcUnscrolledPosition( 0, 0, &left, &top );
7645 CalcUnscrolledPosition( cw, ch, &right, &bottom );
7646#endif
f85afd4e 7647
9496deb5
MB
7648 // avoid drawing grid lines past the last row and col
7649 //
d4175745 7650 right = wxMin( right, GetColRight(GetColAt( m_numCols - 1 )) );
7c1cb261 7651 bottom = wxMin( bottom, GetRowBottom(m_numRows - 1) );
9496deb5 7652
27f35b66 7653 // no gridlines inside multicells, clip them out
d4175745 7654 int leftCol = GetColPos( internalXToCol(left) );
a9339fe2 7655 int topRow = internalYToRow(top);
d4175745 7656 int rightCol = GetColPos( internalXToCol(right) );
a967f048 7657 int bottomRow = internalYToRow(bottom);
27f35b66 7658
c03bf0c7
SC
7659#ifndef __WXMAC__
7660 // CS: I don't know why suddenly unscrolled coordinates are used for clipping
7661 wxRegion clippedcells(0, 0, cw, ch);
27f35b66 7662
a967f048
RG
7663 int i, j, cell_rows, cell_cols;
7664 wxRect rect;
27f35b66 7665
a967f048
RG
7666 for (j=topRow; j<bottomRow; j++)
7667 {
d4175745
VZ
7668 int colPos;
7669 for (colPos=leftCol; colPos<rightCol; colPos++)
27f35b66 7670 {
d4175745
VZ
7671 i = GetColAt( colPos );
7672
a967f048
RG
7673 GetCellSize( j, i, &cell_rows, &cell_cols );
7674 if ((cell_rows > 1) || (cell_cols > 1))
27f35b66 7675 {
a967f048
RG
7676 rect = CellToRect(j,i);
7677 CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
7678 clippedcells.Subtract(rect);
7679 }
7680 else if ((cell_rows < 0) || (cell_cols < 0))
7681 {
a9339fe2 7682 rect = CellToRect(j + cell_rows, i + cell_cols);
a967f048
RG
7683 CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
7684 clippedcells.Subtract(rect);
27f35b66
SN
7685 }
7686 }
7687 }
c03bf0c7 7688#else
c2f5b920 7689 wxRegion clippedcells( left, top, right - left, bottom - top );
c03bf0c7
SC
7690
7691 int i, j, cell_rows, cell_cols;
7692 wxRect rect;
7693
7694 for (j=topRow; j<bottomRow; j++)
7695 {
7696 for (i=leftCol; i<rightCol; i++)
7697 {
7698 GetCellSize( j, i, &cell_rows, &cell_cols );
7699 if ((cell_rows > 1) || (cell_cols > 1))
7700 {
2f024384 7701 rect = CellToRect(j, i);
c03bf0c7
SC
7702 clippedcells.Subtract(rect);
7703 }
7704 else if ((cell_rows < 0) || (cell_cols < 0))
7705 {
2f024384 7706 rect = CellToRect(j + cell_rows, i + cell_cols);
c03bf0c7
SC
7707 clippedcells.Subtract(rect);
7708 }
7709 }
7710 }
7711#endif
a9339fe2 7712
27f35b66
SN
7713 dc.SetClippingRegion( clippedcells );
7714
f85afd4e 7715
2d66e025 7716 // horizontal grid lines
f85afd4e 7717 //
a967f048 7718 // already declared above - int i;
33188aa4 7719 for ( i = internalYToRow(top); i < m_numRows; i++ )
f85afd4e 7720 {
6d55126d 7721 int bot = GetRowBottom(i) - 1;
7c1cb261 7722
6d55126d 7723 if ( bot > bottom )
2d66e025
MB
7724 {
7725 break;
7726 }
7c1cb261 7727
6d55126d 7728 if ( bot >= top )
2d66e025 7729 {
3d3f3e37 7730 dc.SetPen( GetRowGridLinePen(i) );
6d55126d 7731 dc.DrawLine( left, bot, right, bot );
2d66e025 7732 }
f85afd4e
MB
7733 }
7734
2d66e025
MB
7735 // vertical grid lines
7736 //
d4175745
VZ
7737 int colPos;
7738 for ( colPos = leftCol; colPos < m_numCols; colPos++ )
f85afd4e 7739 {
d4175745
VZ
7740 i = GetColAt( colPos );
7741
7c1cb261
VZ
7742 int colRight = GetColRight(i) - 1;
7743 if ( colRight > right )
2d66e025
MB
7744 {
7745 break;
7746 }
7c1cb261
VZ
7747
7748 if ( colRight >= left )
2d66e025 7749 {
3d3f3e37 7750 dc.SetPen( GetColGridLinePen(i) );
7c1cb261 7751 dc.DrawLine( colRight, top, colRight, bottom );
2d66e025
MB
7752 }
7753 }
a9339fe2 7754
27f35b66 7755 dc.DestroyClippingRegion();
2d66e025 7756}
f85afd4e 7757
c2f5b920 7758void wxGrid::DrawRowLabels( wxDC& dc, const wxArrayInt& rows)
2d66e025 7759{
4db6714b
KH
7760 if ( !m_numRows )
7761 return;
60ff3b99 7762
2d66e025 7763 size_t i;
d10f4bf9 7764 size_t numLabels = rows.GetCount();
60ff3b99 7765
2f024384 7766 for ( i = 0; i < numLabels; i++ )
2d66e025 7767 {
d10f4bf9 7768 DrawRowLabel( dc, rows[i] );
60ff3b99 7769 }
f85afd4e
MB
7770}
7771
2d66e025 7772void wxGrid::DrawRowLabel( wxDC& dc, int row )
f85afd4e 7773{
659af826 7774 if ( GetRowHeight(row) <= 0 || m_rowLabelWidth <= 0 )
7c1cb261 7775 return;
60ff3b99 7776
4d1bc39c 7777 wxRect rect;
2f024384 7778
c2651b0a 7779#ifdef __WXGTK20__
4d1bc39c
RR
7780 rect.SetX( 1 );
7781 rect.SetY( GetRowTop(row) + 1 );
7782 rect.SetWidth( m_rowLabelWidth - 2 );
7783 rect.SetHeight( GetRowHeight(row) - 2 );
ec157c8f 7784
4d1bc39c 7785 CalcScrolledPosition( 0, rect.y, NULL, &rect.y );
ec157c8f 7786
4d1bc39c
RR
7787 wxWindowDC *win_dc = (wxWindowDC*) &dc;
7788
7789 wxRendererNative::Get().DrawHeaderButton( win_dc->m_owner, dc, rect, 0 );
7790#else
7c1cb261
VZ
7791 int rowTop = GetRowTop(row),
7792 rowBottom = GetRowBottom(row) - 1;
b99be8fb 7793
d4175745 7794 dc.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW), 1, wxSOLID) );
a9339fe2 7795 dc.DrawLine( m_rowLabelWidth - 1, rowTop, m_rowLabelWidth - 1, rowBottom );
73145b0e 7796 dc.DrawLine( 0, rowTop, 0, rowBottom );
73145b0e 7797 dc.DrawLine( 0, rowBottom, m_rowLabelWidth, rowBottom );
b99be8fb 7798
2d66e025 7799 dc.SetPen( *wxWHITE_PEN );
73145b0e 7800 dc.DrawLine( 1, rowTop, 1, rowBottom );
a9339fe2 7801 dc.DrawLine( 1, rowTop, m_rowLabelWidth - 1, rowTop );
4d1bc39c 7802#endif
2f024384 7803
f85afd4e 7804 dc.SetBackgroundMode( wxTRANSPARENT );
f85afd4e
MB
7805 dc.SetTextForeground( GetLabelTextColour() );
7806 dc.SetFont( GetLabelFont() );
8f177c8e 7807
f85afd4e 7808 int hAlign, vAlign;
2d66e025 7809 GetRowLabelAlignment( &hAlign, &vAlign );
60ff3b99 7810
2d66e025 7811 rect.SetX( 2 );
7c1cb261 7812 rect.SetY( GetRowTop(row) + 2 );
2d66e025 7813 rect.SetWidth( m_rowLabelWidth - 4 );
7c1cb261 7814 rect.SetHeight( GetRowHeight(row) - 4 );
60ff3b99 7815 DrawTextRectangle( dc, GetRowLabelValue( row ), rect, hAlign, vAlign );
f85afd4e
MB
7816}
7817
d10f4bf9 7818void wxGrid::DrawColLabels( wxDC& dc,const wxArrayInt& cols )
f85afd4e 7819{
4db6714b
KH
7820 if ( !m_numCols )
7821 return;
60ff3b99 7822
2d66e025 7823 size_t i;
d10f4bf9 7824 size_t numLabels = cols.GetCount();
60ff3b99 7825
56b6cf26 7826 for ( i = 0; i < numLabels; i++ )
f85afd4e 7827 {
d10f4bf9 7828 DrawColLabel( dc, cols[i] );
60ff3b99 7829 }
f85afd4e
MB
7830}
7831
2d66e025 7832void wxGrid::DrawColLabel( wxDC& dc, int col )
f85afd4e 7833{
659af826 7834 if ( GetColWidth(col) <= 0 || m_colLabelHeight <= 0 )
7c1cb261 7835 return;
60ff3b99 7836
4d1bc39c 7837 int colLeft = GetColLeft(col);
ec157c8f 7838
4d1bc39c 7839 wxRect rect;
2f024384 7840
c2651b0a 7841#ifdef __WXGTK20__
4d1bc39c
RR
7842 rect.SetX( colLeft + 1 );
7843 rect.SetY( 1 );
7844 rect.SetWidth( GetColWidth(col) - 2 );
7845 rect.SetHeight( m_colLabelHeight - 2 );
ec157c8f 7846
4d1bc39c
RR
7847 wxWindowDC *win_dc = (wxWindowDC*) &dc;
7848
7849 wxRendererNative::Get().DrawHeaderButton( win_dc->m_owner, dc, rect, 0 );
7850#else
7851 int colRight = GetColRight(col) - 1;
b99be8fb 7852
d4175745 7853 dc.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW), 1, wxSOLID) );
ccdee36f 7854 dc.DrawLine( colRight, 0, colRight, m_colLabelHeight - 1 );
73145b0e 7855 dc.DrawLine( colLeft, 0, colRight, 0 );
ccdee36f
DS
7856 dc.DrawLine( colLeft, m_colLabelHeight - 1,
7857 colRight + 1, m_colLabelHeight - 1 );
b99be8fb 7858
f85afd4e 7859 dc.SetPen( *wxWHITE_PEN );
ccdee36f 7860 dc.DrawLine( colLeft, 1, colLeft, m_colLabelHeight - 1 );
73145b0e 7861 dc.DrawLine( colLeft, 1, colRight, 1 );
4d1bc39c 7862#endif
2f024384 7863
b0d6ff2f
MB
7864 dc.SetBackgroundMode( wxTRANSPARENT );
7865 dc.SetTextForeground( GetLabelTextColour() );
7866 dc.SetFont( GetLabelFont() );
8f177c8e 7867
d43851f7 7868 int hAlign, vAlign, orient;
2d66e025 7869 GetColLabelAlignment( &hAlign, &vAlign );
d43851f7 7870 orient = GetColLabelTextOrientation();
60ff3b99 7871
7c1cb261 7872 rect.SetX( colLeft + 2 );
2d66e025 7873 rect.SetY( 2 );
7c1cb261 7874 rect.SetWidth( GetColWidth(col) - 4 );
2d66e025 7875 rect.SetHeight( m_colLabelHeight - 4 );
d43851f7 7876 DrawTextRectangle( dc, GetColLabelValue( col ), rect, hAlign, vAlign, orient );
f85afd4e
MB
7877}
7878
2d66e025
MB
7879void wxGrid::DrawTextRectangle( wxDC& dc,
7880 const wxString& value,
7881 const wxRect& rect,
7882 int horizAlign,
d43851f7
JS
7883 int vertAlign,
7884 int textOrientation )
f85afd4e 7885{
2d66e025 7886 wxArrayString lines;
ef5df12b 7887
2d66e025 7888 StringToLines( value, lines );
ef5df12b 7889
2f024384 7890 // Forward to new API.
a9339fe2 7891 DrawTextRectangle( dc,
038a5591
JS
7892 lines,
7893 rect,
7894 horizAlign,
7895 vertAlign,
7896 textOrientation );
d10f4bf9
VZ
7897}
7898
4330c974
VZ
7899// VZ: this should be replaced with wxDC::DrawLabel() to which we just have to
7900// add textOrientation support
7901void wxGrid::DrawTextRectangle(wxDC& dc,
038a5591
JS
7902 const wxArrayString& lines,
7903 const wxRect& rect,
7904 int horizAlign,
7905 int vertAlign,
4330c974 7906 int textOrientation)
d10f4bf9 7907{
4330c974
VZ
7908 if ( lines.empty() )
7909 return;
ef5df12b 7910
4330c974 7911 wxDCClipper clip(dc, rect);
ef5df12b 7912
4330c974
VZ
7913 long textWidth,
7914 textHeight;
ef5df12b 7915
4330c974
VZ
7916 if ( textOrientation == wxHORIZONTAL )
7917 GetTextBoxSize( dc, lines, &textWidth, &textHeight );
7918 else
7919 GetTextBoxSize( dc, lines, &textHeight, &textWidth );
ef5df12b 7920
4330c974
VZ
7921 int x = 0,
7922 y = 0;
7923 switch ( vertAlign )
7924 {
73145b0e 7925 case wxALIGN_BOTTOM:
4db6714b 7926 if ( textOrientation == wxHORIZONTAL )
038a5591 7927 y = rect.y + (rect.height - textHeight - 1);
d43851f7 7928 else
038a5591
JS
7929 x = rect.x + rect.width - textWidth;
7930 break;
ef5df12b 7931
73145b0e 7932 case wxALIGN_CENTRE:
4db6714b 7933 if ( textOrientation == wxHORIZONTAL )
a9339fe2 7934 y = rect.y + ((rect.height - textHeight) / 2);
d43851f7 7935 else
a9339fe2 7936 x = rect.x + ((rect.width - textWidth) / 2);
038a5591 7937 break;
ef5df12b 7938
73145b0e
JS
7939 case wxALIGN_TOP:
7940 default:
4db6714b 7941 if ( textOrientation == wxHORIZONTAL )
038a5591 7942 y = rect.y + 1;
d43851f7 7943 else
038a5591 7944 x = rect.x + 1;
73145b0e 7945 break;
4330c974 7946 }
ef5df12b 7947
4330c974
VZ
7948 // Align each line of a multi-line label
7949 size_t nLines = lines.GetCount();
7950 for ( size_t l = 0; l < nLines; l++ )
7951 {
7952 const wxString& line = lines[l];
ef5df12b 7953
9b7d3c09
VZ
7954 if ( line.empty() )
7955 {
7956 *(textOrientation == wxHORIZONTAL ? &y : &x) += dc.GetCharHeight();
7957 continue;
7958 }
7959
4330c974
VZ
7960 long lineWidth,
7961 lineHeight;
7962 dc.GetTextExtent(line, &lineWidth, &lineHeight);
7963
7964 switch ( horizAlign )
7965 {
038a5591 7966 case wxALIGN_RIGHT:
4db6714b 7967 if ( textOrientation == wxHORIZONTAL )
038a5591
JS
7968 x = rect.x + (rect.width - lineWidth - 1);
7969 else
7970 y = rect.y + lineWidth + 1;
7971 break;
ef5df12b 7972
038a5591 7973 case wxALIGN_CENTRE:
4db6714b 7974 if ( textOrientation == wxHORIZONTAL )
2f024384 7975 x = rect.x + ((rect.width - lineWidth) / 2);
038a5591 7976 else
2f024384 7977 y = rect.y + rect.height - ((rect.height - lineWidth) / 2);
038a5591 7978 break;
ef5df12b 7979
038a5591
JS
7980 case wxALIGN_LEFT:
7981 default:
4db6714b 7982 if ( textOrientation == wxHORIZONTAL )
038a5591
JS
7983 x = rect.x + 1;
7984 else
7985 y = rect.y + rect.height - 1;
7986 break;
4330c974 7987 }
ef5df12b 7988
4330c974
VZ
7989 if ( textOrientation == wxHORIZONTAL )
7990 {
7991 dc.DrawText( line, x, y );
7992 y += lineHeight;
7993 }
7994 else
7995 {
7996 dc.DrawRotatedText( line, x, y, 90.0 );
7997 x += lineHeight;
038a5591 7998 }
f85afd4e 7999 }
f85afd4e
MB
8000}
8001
2f024384
DS
8002// Split multi-line text up into an array of strings.
8003// Any existing contents of the string array are preserved.
f85afd4e
MB
8004//
8005void wxGrid::StringToLines( const wxString& value, wxArrayString& lines )
8006{
f85afd4e
MB
8007 int startPos = 0;
8008 int pos;
6d004f67 8009 wxString eol = wxTextFile::GetEOL( wxTextFileType_Unix );
2433bb2e 8010 wxString tVal = wxTextFile::Translate( value, wxTextFileType_Unix );
e2b42eeb 8011
faa94f3e 8012 while ( startPos < (int)tVal.length() )
f85afd4e 8013 {
2433bb2e 8014 pos = tVal.Mid(startPos).Find( eol );
f85afd4e
MB
8015 if ( pos < 0 )
8016 {
8017 break;
8018 }
8019 else if ( pos == 0 )
8020 {
8021 lines.Add( wxEmptyString );
8022 }
8023 else
8024 {
2433bb2e 8025 lines.Add( value.Mid(startPos, pos) );
f85afd4e 8026 }
a9339fe2 8027
4db6714b 8028 startPos += pos + 1;
f85afd4e 8029 }
4db6714b 8030
faa94f3e 8031 if ( startPos < (int)value.length() )
f85afd4e
MB
8032 {
8033 lines.Add( value.Mid( startPos ) );
8034 }
8035}
8036
fbfb8bcc 8037void wxGrid::GetTextBoxSize( const wxDC& dc,
d10f4bf9 8038 const wxArrayString& lines,
f85afd4e
MB
8039 long *width, long *height )
8040{
8041 long w = 0;
8042 long h = 0;
8d7eaf91 8043 long lineW = 0, lineH = 0;
f85afd4e 8044
b540eb2b 8045 size_t i;
56b6cf26 8046 for ( i = 0; i < lines.GetCount(); i++ )
f85afd4e
MB
8047 {
8048 dc.GetTextExtent( lines[i], &lineW, &lineH );
8049 w = wxMax( w, lineW );
8050 h += lineH;
8051 }
8052
8053 *width = w;
8054 *height = h;
8055}
8056
f6bcfd97
BP
8057//
8058// ------ Batch processing.
8059//
8060void wxGrid::EndBatch()
8061{
8062 if ( m_batchCount > 0 )
8063 {
8064 m_batchCount--;
8065 if ( !m_batchCount )
8066 {
8067 CalcDimensions();
8068 m_rowLabelWin->Refresh();
8069 m_colLabelWin->Refresh();
8070 m_cornerLabelWin->Refresh();
8071 m_gridWin->Refresh();
8072 }
8073 }
8074}
f85afd4e 8075
d8232393
MB
8076// Use this, rather than wxWindow::Refresh(), to force an immediate
8077// repainting of the grid. Has no effect if you are already inside a
8078// BeginBatch / EndBatch block.
8079//
8080void wxGrid::ForceRefresh()
8081{
8082 BeginBatch();
8083 EndBatch();
8084}
8085
bf646121
VZ
8086bool wxGrid::Enable(bool enable)
8087{
8088 if ( !wxScrolledWindow::Enable(enable) )
8089 return false;
8090
8091 // redraw in the new state
8092 m_gridWin->Refresh();
8093
8094 return true;
8095}
d8232393 8096
f85afd4e 8097//
2d66e025 8098// ------ Edit control functions
f85afd4e
MB
8099//
8100
2d66e025 8101void wxGrid::EnableEditing( bool edit )
f85afd4e 8102{
2d66e025
MB
8103 // TODO: improve this ?
8104 //
8105 if ( edit != m_editable )
f85afd4e 8106 {
4db6714b
KH
8107 if (!edit)
8108 EnableCellEditControl(edit);
2d66e025 8109 m_editable = edit;
f85afd4e 8110 }
f85afd4e
MB
8111}
8112
2d66e025 8113void wxGrid::EnableCellEditControl( bool enable )
f85afd4e 8114{
2c9a89e0
RD
8115 if (! m_editable)
8116 return;
8117
2c9a89e0
RD
8118 if ( enable != m_cellEditCtrlEnabled )
8119 {
dcfe4c3d 8120 if ( enable )
f85afd4e 8121 {
97a9929e
VZ
8122 if (SendEvent( wxEVT_GRID_EDITOR_SHOWN) <0)
8123 return;
fe7b9ed6 8124
97a9929e 8125 // this should be checked by the caller!
a9339fe2 8126 wxASSERT_MSG( CanEnableCellControl(), _T("can't enable editing for this cell!") );
b54ba671
VZ
8127
8128 // do it before ShowCellEditControl()
dcfe4c3d 8129 m_cellEditCtrlEnabled = enable;
b54ba671 8130
2d66e025 8131 ShowCellEditControl();
2d66e025
MB
8132 }
8133 else
8134 {
97a9929e 8135 //FIXME:add veto support
a9339fe2 8136 SendEvent( wxEVT_GRID_EDITOR_HIDDEN );
fe7b9ed6 8137
97a9929e 8138 HideCellEditControl();
2d66e025 8139 SaveEditControlValue();
b54ba671
VZ
8140
8141 // do it after HideCellEditControl()
dcfe4c3d 8142 m_cellEditCtrlEnabled = enable;
f85afd4e 8143 }
f85afd4e 8144 }
f85afd4e 8145}
f85afd4e 8146
b54ba671 8147bool wxGrid::IsCurrentCellReadOnly() const
283b7808 8148{
b54ba671
VZ
8149 // const_cast
8150 wxGridCellAttr* attr = ((wxGrid *)this)->GetCellAttr(m_currentCellCoords);
8151 bool readonly = attr->IsReadOnly();
8152 attr->DecRef();
283b7808 8153
b54ba671
VZ
8154 return readonly;
8155}
283b7808 8156
b54ba671
VZ
8157bool wxGrid::CanEnableCellControl() const
8158{
20c84410
SN
8159 return m_editable && (m_currentCellCoords != wxGridNoCellCoords) &&
8160 !IsCurrentCellReadOnly();
b54ba671
VZ
8161}
8162
8163bool wxGrid::IsCellEditControlEnabled() const
8164{
8165 // the cell edit control might be disable for all cells or just for the
8166 // current one if it's read only
ca65c044 8167 return m_cellEditCtrlEnabled ? !IsCurrentCellReadOnly() : false;
283b7808
VZ
8168}
8169
f6bcfd97
BP
8170bool wxGrid::IsCellEditControlShown() const
8171{
ca65c044 8172 bool isShown = false;
f6bcfd97
BP
8173
8174 if ( m_cellEditCtrlEnabled )
8175 {
8176 int row = m_currentCellCoords.GetRow();
8177 int col = m_currentCellCoords.GetCol();
8178 wxGridCellAttr* attr = GetCellAttr(row, col);
8179 wxGridCellEditor* editor = attr->GetEditor((wxGrid*) this, row, col);
8180 attr->DecRef();
8181
8182 if ( editor )
8183 {
8184 if ( editor->IsCreated() )
8185 {
8186 isShown = editor->GetControl()->IsShown();
8187 }
8188
8189 editor->DecRef();
8190 }
8191 }
8192
8193 return isShown;
8194}
8195
2d66e025 8196void wxGrid::ShowCellEditControl()
f85afd4e 8197{
2d66e025 8198 if ( IsCellEditControlEnabled() )
f85afd4e 8199 {
7db713ae 8200 if ( !IsVisible( m_currentCellCoords, false ) )
2d66e025 8201 {
ca65c044 8202 m_cellEditCtrlEnabled = false;
2d66e025
MB
8203 return;
8204 }
8205 else
8206 {
2c9a89e0
RD
8207 wxRect rect = CellToRect( m_currentCellCoords );
8208 int row = m_currentCellCoords.GetRow();
8209 int col = m_currentCellCoords.GetCol();
2d66e025 8210
27f35b66
SN
8211 // if this is part of a multicell, find owner (topleft)
8212 int cell_rows, cell_cols;
8213 GetCellSize( row, col, &cell_rows, &cell_cols );
8214 if ( cell_rows <= 0 || cell_cols <= 0 )
8215 {
8216 row += cell_rows;
8217 col += cell_cols;
8218 m_currentCellCoords.SetRow( row );
8219 m_currentCellCoords.SetCol( col );
8220 }
8221
99306db2
VZ
8222 // erase the highlight and the cell contents because the editor
8223 // might not cover the entire cell
8224 wxClientDC dc( m_gridWin );
8225 PrepareDC( dc );
d4175745 8226 dc.SetBrush(wxBrush(GetCellAttr(row, col)->GetBackgroundColour(), wxSOLID));
99306db2
VZ
8227 dc.SetPen(*wxTRANSPARENT_PEN);
8228 dc.DrawRectangle(rect);
d2fdd8d2 8229
6f706ee0
VZ
8230 // convert to scrolled coords
8231 CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
8232
8233 int nXMove = 0;
8234 if (rect.x < 0)
8235 nXMove = rect.x;
8236
99306db2 8237 // cell is shifted by one pixel
68c5a31c 8238 // However, don't allow x or y to become negative
70e8d961 8239 // since the SetSize() method interprets that as
68c5a31c
SN
8240 // "don't change."
8241 if (rect.x > 0)
8242 rect.x--;
8243 if (rect.y > 0)
8244 rect.y--;
f0102d2a 8245
508011ce 8246 wxGridCellAttr* attr = GetCellAttr(row, col);
28a77bc4 8247 wxGridCellEditor* editor = attr->GetEditor(this, row, col);
3da93aae
VZ
8248 if ( !editor->IsCreated() )
8249 {
ca65c044 8250 editor->Create(m_gridWin, wxID_ANY,
2c9a89e0 8251 new wxGridCellEditorEvtHandler(this, editor));
bf7945ce
RD
8252
8253 wxGridEditorCreatedEvent evt(GetId(),
8254 wxEVT_GRID_EDITOR_CREATED,
8255 this,
8256 row,
8257 col,
8258 editor->GetControl());
8259 GetEventHandler()->ProcessEvent(evt);
2d66e025
MB
8260 }
8261
27f35b66 8262 // resize editor to overflow into righthand cells if allowed
39b80349 8263 int maxWidth = rect.width;
27f35b66
SN
8264 wxString value = GetCellValue(row, col);
8265 if ( (value != wxEmptyString) && (attr->GetOverflow()) )
8266 {
39b80349 8267 int y;
2f024384
DS
8268 GetTextExtent(value, &maxWidth, &y, NULL, NULL, &attr->GetFont());
8269 if (maxWidth < rect.width)
8270 maxWidth = rect.width;
39b80349 8271 }
4db6714b 8272
39b80349 8273 int client_right = m_gridWin->GetClientSize().GetWidth();
2f024384 8274 if (rect.x + maxWidth > client_right)
2b5f62a0 8275 maxWidth = client_right - rect.x;
39b80349 8276
2b5f62a0
VZ
8277 if ((maxWidth > rect.width) && (col < m_numCols) && m_table)
8278 {
39b80349 8279 GetCellSize( row, col, &cell_rows, &cell_cols );
2b5f62a0 8280 // may have changed earlier
2f024384 8281 for (int i = col + cell_cols; i < m_numCols; i++)
2b5f62a0 8282 {
39b80349
SN
8283 int c_rows, c_cols;
8284 GetCellSize( row, i, &c_rows, &c_cols );
2f024384 8285
2b5f62a0 8286 // looks weird going over a multicell
bee19958 8287 if (m_table->IsEmptyCell( row, i ) &&
2b5f62a0 8288 (rect.width < maxWidth) && (c_rows == 1))
2f024384 8289 {
bee19958 8290 rect.width += GetColWidth( i );
2f024384 8291 }
2b5f62a0
VZ
8292 else
8293 break;
8294 }
4db6714b 8295
39b80349 8296 if (rect.GetRight() > client_right)
bee19958 8297 rect.SetRight( client_right - 1 );
27f35b66 8298 }
73145b0e 8299
bee19958 8300 editor->SetCellAttr( attr );
2c9a89e0 8301 editor->SetSize( rect );
e1a66d9a
RD
8302 if (nXMove != 0)
8303 editor->GetControl()->Move(
8304 editor->GetControl()->GetPosition().x + nXMove,
8305 editor->GetControl()->GetPosition().y );
ca65c044 8306 editor->Show( true, attr );
04418332
VZ
8307
8308 // recalc dimensions in case we need to
8309 // expand the scrolled window to account for editor
73145b0e 8310 CalcDimensions();
99306db2 8311
3da93aae 8312 editor->BeginEdit(row, col, this);
1bd71df9 8313 editor->SetCellAttr(NULL);
0b190b0f
VZ
8314
8315 editor->DecRef();
2c9a89e0 8316 attr->DecRef();
2b5f62a0 8317 }
f85afd4e
MB
8318 }
8319}
8320
2d66e025 8321void wxGrid::HideCellEditControl()
f85afd4e 8322{
2d66e025 8323 if ( IsCellEditControlEnabled() )
f85afd4e 8324 {
2c9a89e0
RD
8325 int row = m_currentCellCoords.GetRow();
8326 int col = m_currentCellCoords.GetCol();
8327
bee19958 8328 wxGridCellAttr *attr = GetCellAttr(row, col);
0b190b0f 8329 wxGridCellEditor *editor = attr->GetEditor(this, row, col);
ca65c044 8330 editor->Show( false );
0b190b0f 8331 editor->DecRef();
2c9a89e0 8332 attr->DecRef();
2fb3e528 8333
48962b9f 8334 m_gridWin->SetFocus();
2fb3e528 8335
27f35b66
SN
8336 // refresh whole row to the right
8337 wxRect rect( CellToRect(row, col) );
8338 CalcScrolledPosition(rect.x, rect.y, &rect.x, &rect.y );
8339 rect.width = m_gridWin->GetClientSize().GetWidth() - rect.x;
2f024384 8340
7d75e6c6
RD
8341#ifdef __WXMAC__
8342 // ensure that the pixels under the focus ring get refreshed as well
4db6714b 8343 rect.Inflate(10, 10);
7d75e6c6 8344#endif
2f024384 8345
ca65c044 8346 m_gridWin->Refresh( false, &rect );
f85afd4e 8347 }
2d66e025 8348}
8f177c8e 8349
2d66e025
MB
8350void wxGrid::SaveEditControlValue()
8351{
3da93aae
VZ
8352 if ( IsCellEditControlEnabled() )
8353 {
2c9a89e0
RD
8354 int row = m_currentCellCoords.GetRow();
8355 int col = m_currentCellCoords.GetCol();
8f177c8e 8356
4db6714b 8357 wxString oldval = GetCellValue(row, col);
fe7b9ed6 8358
3da93aae 8359 wxGridCellAttr* attr = GetCellAttr(row, col);
28a77bc4 8360 wxGridCellEditor* editor = attr->GetEditor(this, row, col);
3324d5f5 8361 bool changed = editor->EndEdit(row, col, this);
2d66e025 8362
0b190b0f 8363 editor->DecRef();
2c9a89e0 8364 attr->DecRef();
2d66e025 8365
3da93aae
VZ
8366 if (changed)
8367 {
fe7b9ed6 8368 if ( SendEvent( wxEVT_GRID_CELL_CHANGE,
2d66e025 8369 m_currentCellCoords.GetRow(),
4db6714b
KH
8370 m_currentCellCoords.GetCol() ) < 0 )
8371 {
97a9929e 8372 // Event has been vetoed, set the data back.
4db6714b 8373 SetCellValue(row, col, oldval);
97a9929e 8374 }
2d66e025 8375 }
f85afd4e
MB
8376 }
8377}
8378
2d66e025 8379//
60ff3b99
VZ
8380// ------ Grid location functions
8381// Note that all of these functions work with the logical coordinates of
2d66e025
MB
8382// grid cells and labels so you will need to convert from device
8383// coordinates for mouse events etc.
8384//
8385
8386void wxGrid::XYToCell( int x, int y, wxGridCellCoords& coords )
f85afd4e 8387{
58dd5b3b
MB
8388 int row = YToRow(y);
8389 int col = XToCol(x);
8390
a9339fe2 8391 if ( row == -1 || col == -1 )
58dd5b3b
MB
8392 {
8393 coords = wxGridNoCellCoords;
8394 }
8395 else
8396 {
8397 coords.Set( row, col );
8398 }
2d66e025 8399}
f85afd4e 8400
b1da8107
SN
8401// Internal Helper function for computing row or column from some
8402// (unscrolled) coordinate value, using either
70e8d961 8403// m_defaultRowHeight/m_defaultColWidth or binary search on array
b1da8107
SN
8404// of m_rowBottoms/m_ColRights to speed up the search!
8405
8406static int CoordToRowOrCol(int coord, int defaultDist, int minDist,
64e15340 8407 const wxArrayInt& BorderArray, int nMax,
a967f048 8408 bool clipToMinMax)
2d66e025 8409{
a967f048
RG
8410 if (coord < 0)
8411 return clipToMinMax && (nMax > 0) ? 0 : -1;
8412
b1da8107
SN
8413 if (!defaultDist)
8414 defaultDist = 1;
a967f048 8415
1af546bf
VZ
8416 size_t i_max = coord / defaultDist,
8417 i_min = 0;
d57ad377 8418
b1da8107
SN
8419 if (BorderArray.IsEmpty())
8420 {
4db6714b 8421 if ((int) i_max < nMax)
64e15340 8422 return i_max;
a967f048 8423 return clipToMinMax ? nMax - 1 : -1;
b1da8107 8424 }
8f177c8e 8425
b1da8107 8426 if ( i_max >= BorderArray.GetCount())
2f024384 8427 {
b1da8107 8428 i_max = BorderArray.GetCount() - 1;
2f024384 8429 }
70e8d961 8430 else
f85afd4e 8431 {
b1da8107
SN
8432 if ( coord >= BorderArray[i_max])
8433 {
8434 i_min = i_max;
20c84410
SN
8435 if (minDist)
8436 i_max = coord / minDist;
8437 else
8438 i_max = BorderArray.GetCount() - 1;
b1da8107 8439 }
4db6714b 8440
b1da8107
SN
8441 if ( i_max >= BorderArray.GetCount())
8442 i_max = BorderArray.GetCount() - 1;
f85afd4e 8443 }
4db6714b 8444
33188aa4 8445 if ( coord >= BorderArray[i_max])
a967f048 8446 return clipToMinMax ? (int)i_max : -1;
68c5a31c
SN
8447 if ( coord < BorderArray[0] )
8448 return 0;
2d66e025 8449
68c5a31c 8450 while ( i_max - i_min > 0 )
b1da8107
SN
8451 {
8452 wxCHECK_MSG(BorderArray[i_min] <= coord && coord < BorderArray[i_max],
33188aa4 8453 0, _T("wxGrid: internal error in CoordToRowOrCol"));
b1da8107 8454 if (coord >= BorderArray[ i_max - 1])
b1da8107 8455 return i_max;
b1da8107
SN
8456 else
8457 i_max--;
8458 int median = i_min + (i_max - i_min + 1) / 2;
8459 if (coord < BorderArray[median])
8460 i_max = median;
8461 else
8462 i_min = median;
8463 }
4db6714b 8464
b1da8107 8465 return i_max;
f85afd4e
MB
8466}
8467
b1da8107 8468int wxGrid::YToRow( int y )
f85afd4e 8469{
b1da8107 8470 return CoordToRowOrCol(y, m_defaultRowHeight,
ca65c044 8471 m_minAcceptableRowHeight, m_rowBottoms, m_numRows, false);
b1da8107 8472}
8f177c8e 8473
d4175745 8474int wxGrid::XToCol( int x, bool clipToMinMax )
b1da8107 8475{
d4175745
VZ
8476 if (x < 0)
8477 return clipToMinMax && (m_numCols > 0) ? GetColAt( 0 ) : -1;
8478
8479 if (!m_defaultColWidth)
8480 m_defaultColWidth = 1;
8481
8482 int maxPos = x / m_defaultColWidth;
8483 int minPos = 0;
8484
8485 if (m_colRights.IsEmpty())
8486 {
8487 if(maxPos < m_numCols)
8488 return GetColAt( maxPos );
8489 return clipToMinMax ? GetColAt( m_numCols - 1 ) : -1;
8490 }
8491
8492 if ( maxPos >= m_numCols)
8493 maxPos = m_numCols - 1;
8494 else
8495 {
8496 if ( x >= m_colRights[GetColAt( maxPos )])
8497 {
8498 minPos = maxPos;
8499 if (m_minAcceptableColWidth)
8500 maxPos = x / m_minAcceptableColWidth;
8501 else
8502 maxPos = m_numCols - 1;
8503 }
8504 if ( maxPos >= m_numCols)
8505 maxPos = m_numCols - 1;
8506 }
8507
8508 //X is beyond the last column
8509 if ( x >= m_colRights[GetColAt( maxPos )])
8510 return clipToMinMax ? GetColAt( maxPos ) : -1;
8511
8512 //X is before the first column
8513 if ( x < m_colRights[GetColAt( 0 )] )
8514 return GetColAt( 0 );
8515
8516 //Perform a binary search
8517 while ( maxPos - minPos > 0 )
8518 {
8519 wxCHECK_MSG(m_colRights[GetColAt( minPos )] <= x && x < m_colRights[GetColAt( maxPos )],
8520 0, _T("wxGrid: internal error in XToCol"));
8521
8522 if (x >= m_colRights[GetColAt( maxPos - 1 )])
8523 return GetColAt( maxPos );
8524 else
8525 maxPos--;
8526 int median = minPos + (maxPos - minPos + 1) / 2;
8527 if (x < m_colRights[GetColAt( median )])
8528 maxPos = median;
8529 else
8530 minPos = median;
8531 }
8532 return GetColAt( maxPos );
2d66e025 8533}
8f177c8e 8534
2d66e025
MB
8535// return the row number that that the y coord is near the edge of, or
8536// -1 if not near an edge
8537//
8538int wxGrid::YToEdgeOfRow( int y )
8539{
33188aa4
SN
8540 int i;
8541 i = internalYToRow(y);
8542
8543 if ( GetRowHeight(i) > WXGRID_LABEL_EDGE_ZONE )
2d66e025 8544 {
33188aa4
SN
8545 // We know that we are in row i, test whether we are
8546 // close enough to lower or upper border, respectively.
8547 if ( abs(GetRowBottom(i) - y) < WXGRID_LABEL_EDGE_ZONE )
8548 return i;
4db6714b 8549 else if ( i > 0 && y - GetRowTop(i) < WXGRID_LABEL_EDGE_ZONE )
33188aa4 8550 return i - 1;
f85afd4e 8551 }
2d66e025
MB
8552
8553 return -1;
8554}
8555
2d66e025
MB
8556// return the col number that that the x coord is near the edge of, or
8557// -1 if not near an edge
8558//
8559int wxGrid::XToEdgeOfCol( int x )
8560{
33188aa4
SN
8561 int i;
8562 i = internalXToCol(x);
8563
8564 if ( GetColWidth(i) > WXGRID_LABEL_EDGE_ZONE )
f85afd4e 8565 {
a9339fe2 8566 // We know that we are in column i; test whether we are
33188aa4
SN
8567 // close enough to right or left border, respectively.
8568 if ( abs(GetColRight(i) - x) < WXGRID_LABEL_EDGE_ZONE )
8569 return i;
4db6714b 8570 else if ( i > 0 && x - GetColLeft(i) < WXGRID_LABEL_EDGE_ZONE )
33188aa4 8571 return i - 1;
f85afd4e 8572 }
2d66e025
MB
8573
8574 return -1;
f85afd4e
MB
8575}
8576
2d66e025 8577wxRect wxGrid::CellToRect( int row, int col )
f85afd4e 8578{
2d66e025 8579 wxRect rect( -1, -1, -1, -1 );
f85afd4e 8580
2f024384
DS
8581 if ( row >= 0 && row < m_numRows &&
8582 col >= 0 && col < m_numCols )
f85afd4e 8583 {
27f35b66
SN
8584 int i, cell_rows, cell_cols;
8585 rect.width = rect.height = 0;
8586 GetCellSize( row, col, &cell_rows, &cell_cols );
8587 // if negative then find multicell owner
2f024384
DS
8588 if (cell_rows < 0)
8589 row += cell_rows;
8590 if (cell_cols < 0)
8591 col += cell_cols;
27f35b66
SN
8592 GetCellSize( row, col, &cell_rows, &cell_cols );
8593
7c1cb261
VZ
8594 rect.x = GetColLeft(col);
8595 rect.y = GetRowTop(row);
2f024384
DS
8596 for (i=col; i < col + cell_cols; i++)
8597 rect.width += GetColWidth(i);
8598 for (i=row; i < row + cell_rows; i++)
27f35b66 8599 rect.height += GetRowHeight(i);
f85afd4e
MB
8600 }
8601
f6bcfd97 8602 // if grid lines are enabled, then the area of the cell is a bit smaller
4db6714b
KH
8603 if (m_gridLinesEnabled)
8604 {
f6bcfd97
BP
8605 rect.width -= 1;
8606 rect.height -= 1;
8607 }
4db6714b 8608
2d66e025
MB
8609 return rect;
8610}
8611
2d66e025
MB
8612bool wxGrid::IsVisible( int row, int col, bool wholeCellVisible )
8613{
8614 // get the cell rectangle in logical coords
8615 //
8616 wxRect r( CellToRect( row, col ) );
60ff3b99 8617
2d66e025
MB
8618 // convert to device coords
8619 //
8620 int left, top, right, bottom;
8621 CalcScrolledPosition( r.GetLeft(), r.GetTop(), &left, &top );
8622 CalcScrolledPosition( r.GetRight(), r.GetBottom(), &right, &bottom );
60ff3b99 8623
2d66e025 8624 // check against the client area of the grid window
2d66e025
MB
8625 int cw, ch;
8626 m_gridWin->GetClientSize( &cw, &ch );
60ff3b99 8627
2d66e025 8628 if ( wholeCellVisible )
f85afd4e 8629 {
2d66e025 8630 // is the cell wholly visible ?
2f024384
DS
8631 return ( left >= 0 && right <= cw &&
8632 top >= 0 && bottom <= ch );
2d66e025
MB
8633 }
8634 else
8635 {
8636 // is the cell partly visible ?
8637 //
2f024384
DS
8638 return ( ((left >= 0 && left < cw) || (right > 0 && right <= cw)) &&
8639 ((top >= 0 && top < ch) || (bottom > 0 && bottom <= ch)) );
2d66e025
MB
8640 }
8641}
8642
2d66e025
MB
8643// make the specified cell location visible by doing a minimal amount
8644// of scrolling
8645//
8646void wxGrid::MakeCellVisible( int row, int col )
8647{
8648 int i;
60ff3b99 8649 int xpos = -1, ypos = -1;
2d66e025 8650
2f024384
DS
8651 if ( row >= 0 && row < m_numRows &&
8652 col >= 0 && col < m_numCols )
2d66e025
MB
8653 {
8654 // get the cell rectangle in logical coords
2d66e025 8655 wxRect r( CellToRect( row, col ) );
60ff3b99 8656
2d66e025 8657 // convert to device coords
2d66e025
MB
8658 int left, top, right, bottom;
8659 CalcScrolledPosition( r.GetLeft(), r.GetTop(), &left, &top );
8660 CalcScrolledPosition( r.GetRight(), r.GetBottom(), &right, &bottom );
60ff3b99 8661
2d66e025
MB
8662 int cw, ch;
8663 m_gridWin->GetClientSize( &cw, &ch );
60ff3b99 8664
2d66e025 8665 if ( top < 0 )
3f296516 8666 {
2d66e025 8667 ypos = r.GetTop();
3f296516 8668 }
2d66e025
MB
8669 else if ( bottom > ch )
8670 {
8671 int h = r.GetHeight();
8672 ypos = r.GetTop();
56b6cf26 8673 for ( i = row - 1; i >= 0; i-- )
2d66e025 8674 {
7c1cb261
VZ
8675 int rowHeight = GetRowHeight(i);
8676 if ( h + rowHeight > ch )
8677 break;
2d66e025 8678
7c1cb261
VZ
8679 h += rowHeight;
8680 ypos -= rowHeight;
2d66e025 8681 }
f0102d2a
VZ
8682
8683 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
56b6cf26
DS
8684 // have rounding errors (this is important, because if we do,
8685 // we might not scroll at all and some cells won't be redrawn)
275c4ae4 8686 //
56b6cf26
DS
8687 // Sometimes GRID_SCROLL_LINE / 2 is not enough,
8688 // so just add a full scroll unit...
675a9c0d 8689 ypos += m_scrollLineY;
2d66e025
MB
8690 }
8691
7db713ae 8692 // special handling for wide cells - show always left part of the cell!
faa94f3e 8693 // Otherwise, e.g. when stepping from row to row, it would jump between
7db713ae
JS
8694 // left and right part of the cell on every step!
8695// if ( left < 0 )
ccdee36f 8696 if ( left < 0 || (right - left) >= cw )
2d66e025
MB
8697 {
8698 xpos = r.GetLeft();
8699 }
8700 else if ( right > cw )
8701 {
73145b0e
JS
8702 // position the view so that the cell is on the right
8703 int x0, y0;
8704 CalcUnscrolledPosition(0, 0, &x0, &y0);
8705 xpos = x0 + (right - cw);
f0102d2a
VZ
8706
8707 // see comment for ypos above
675a9c0d 8708 xpos += m_scrollLineX;
2d66e025
MB
8709 }
8710
ccdee36f 8711 if ( xpos != -1 || ypos != -1 )
2d66e025 8712 {
97a9929e 8713 if ( xpos != -1 )
675a9c0d 8714 xpos /= m_scrollLineX;
97a9929e 8715 if ( ypos != -1 )
675a9c0d 8716 ypos /= m_scrollLineY;
2d66e025
MB
8717 Scroll( xpos, ypos );
8718 AdjustScrollbars();
8719 }
8720 }
8721}
8722
2d66e025
MB
8723//
8724// ------ Grid cursor movement functions
8725//
8726
5c8fc7c1 8727bool wxGrid::MoveCursorUp( bool expandSelection )
2d66e025 8728{
2f024384 8729 if ( m_currentCellCoords != wxGridNoCellCoords &&
f6bcfd97 8730 m_currentCellCoords.GetRow() >= 0 )
2d66e025 8731 {
bee19958 8732 if ( expandSelection )
d95b0c2b
SN
8733 {
8734 if ( m_selectingKeyboard == wxGridNoCellCoords )
8735 m_selectingKeyboard = m_currentCellCoords;
f6bcfd97
BP
8736 if ( m_selectingKeyboard.GetRow() > 0 )
8737 {
8738 m_selectingKeyboard.SetRow( m_selectingKeyboard.GetRow() - 1 );
8739 MakeCellVisible( m_selectingKeyboard.GetRow(),
8740 m_selectingKeyboard.GetCol() );
c9097836 8741 HighlightBlock( m_currentCellCoords, m_selectingKeyboard );
f6bcfd97 8742 }
d95b0c2b 8743 }
f6bcfd97 8744 else if ( m_currentCellCoords.GetRow() > 0 )
aa5e1f75 8745 {
962a48f6
DS
8746 int row = m_currentCellCoords.GetRow() - 1;
8747 int col = m_currentCellCoords.GetCol();
aa5e1f75 8748 ClearSelection();
962a48f6
DS
8749 MakeCellVisible( row, col );
8750 SetCurrentCell( row, col );
aa5e1f75 8751 }
f6bcfd97 8752 else
ca65c044 8753 return false;
4db6714b 8754
ca65c044 8755 return true;
f85afd4e 8756 }
2d66e025 8757
ca65c044 8758 return false;
2d66e025
MB
8759}
8760
5c8fc7c1 8761bool wxGrid::MoveCursorDown( bool expandSelection )
2d66e025 8762{
2f024384 8763 if ( m_currentCellCoords != wxGridNoCellCoords &&
f6bcfd97 8764 m_currentCellCoords.GetRow() < m_numRows )
f85afd4e 8765 {
5c8fc7c1 8766 if ( expandSelection )
d95b0c2b
SN
8767 {
8768 if ( m_selectingKeyboard == wxGridNoCellCoords )
8769 m_selectingKeyboard = m_currentCellCoords;
ccdee36f 8770 if ( m_selectingKeyboard.GetRow() < m_numRows - 1 )
f6bcfd97
BP
8771 {
8772 m_selectingKeyboard.SetRow( m_selectingKeyboard.GetRow() + 1 );
8773 MakeCellVisible( m_selectingKeyboard.GetRow(),
8774 m_selectingKeyboard.GetCol() );
c9097836 8775 HighlightBlock( m_currentCellCoords, m_selectingKeyboard );
f6bcfd97 8776 }
d95b0c2b 8777 }
f6bcfd97 8778 else if ( m_currentCellCoords.GetRow() < m_numRows - 1 )
aa5e1f75 8779 {
962a48f6
DS
8780 int row = m_currentCellCoords.GetRow() + 1;
8781 int col = m_currentCellCoords.GetCol();
aa5e1f75 8782 ClearSelection();
962a48f6
DS
8783 MakeCellVisible( row, col );
8784 SetCurrentCell( row, col );
aa5e1f75 8785 }
f6bcfd97 8786 else
ca65c044 8787 return false;
4db6714b 8788
ca65c044 8789 return true;
f85afd4e 8790 }
2d66e025 8791
ca65c044 8792 return false;
f85afd4e
MB
8793}
8794
5c8fc7c1 8795bool wxGrid::MoveCursorLeft( bool expandSelection )
f85afd4e 8796{
2f024384 8797 if ( m_currentCellCoords != wxGridNoCellCoords &&
f6bcfd97 8798 m_currentCellCoords.GetCol() >= 0 )
2d66e025 8799 {
5c8fc7c1 8800 if ( expandSelection )
d95b0c2b
SN
8801 {
8802 if ( m_selectingKeyboard == wxGridNoCellCoords )
8803 m_selectingKeyboard = m_currentCellCoords;
f6bcfd97
BP
8804 if ( m_selectingKeyboard.GetCol() > 0 )
8805 {
8806 m_selectingKeyboard.SetCol( m_selectingKeyboard.GetCol() - 1 );
8807 MakeCellVisible( m_selectingKeyboard.GetRow(),
8808 m_selectingKeyboard.GetCol() );
c9097836 8809 HighlightBlock( m_currentCellCoords, m_selectingKeyboard );
f6bcfd97 8810 }
d95b0c2b 8811 }
d4175745 8812 else if ( GetColPos( m_currentCellCoords.GetCol() ) > 0 )
aa5e1f75 8813 {
962a48f6 8814 int row = m_currentCellCoords.GetRow();
d4175745 8815 int col = GetColAt( GetColPos( m_currentCellCoords.GetCol() ) - 1 );
aa5e1f75 8816 ClearSelection();
d4175745 8817
962a48f6
DS
8818 MakeCellVisible( row, col );
8819 SetCurrentCell( row, col );
aa5e1f75 8820 }
f6bcfd97 8821 else
ca65c044 8822 return false;
4db6714b 8823
ca65c044 8824 return true;
2d66e025
MB
8825 }
8826
ca65c044 8827 return false;
2d66e025
MB
8828}
8829
5c8fc7c1 8830bool wxGrid::MoveCursorRight( bool expandSelection )
2d66e025 8831{
2f024384 8832 if ( m_currentCellCoords != wxGridNoCellCoords &&
f6bcfd97 8833 m_currentCellCoords.GetCol() < m_numCols )
f85afd4e 8834 {
5c8fc7c1 8835 if ( expandSelection )
d95b0c2b
SN
8836 {
8837 if ( m_selectingKeyboard == wxGridNoCellCoords )
8838 m_selectingKeyboard = m_currentCellCoords;
f6bcfd97
BP
8839 if ( m_selectingKeyboard.GetCol() < m_numCols - 1 )
8840 {
8841 m_selectingKeyboard.SetCol( m_selectingKeyboard.GetCol() + 1 );
8842 MakeCellVisible( m_selectingKeyboard.GetRow(),
8843 m_selectingKeyboard.GetCol() );
c9097836 8844 HighlightBlock( m_currentCellCoords, m_selectingKeyboard );
f6bcfd97 8845 }
d95b0c2b 8846 }
d4175745 8847 else if ( GetColPos( m_currentCellCoords.GetCol() ) < m_numCols - 1 )
aa5e1f75 8848 {
962a48f6 8849 int row = m_currentCellCoords.GetRow();
d4175745 8850 int col = GetColAt( GetColPos( m_currentCellCoords.GetCol() ) + 1 );
aa5e1f75 8851 ClearSelection();
d4175745 8852
962a48f6
DS
8853 MakeCellVisible( row, col );
8854 SetCurrentCell( row, col );
aa5e1f75 8855 }
f6bcfd97 8856 else
ca65c044 8857 return false;
4db6714b 8858
ca65c044 8859 return true;
f85afd4e 8860 }
8f177c8e 8861
ca65c044 8862 return false;
2d66e025
MB
8863}
8864
2d66e025
MB
8865bool wxGrid::MovePageUp()
8866{
4db6714b
KH
8867 if ( m_currentCellCoords == wxGridNoCellCoords )
8868 return false;
60ff3b99 8869
2d66e025
MB
8870 int row = m_currentCellCoords.GetRow();
8871 if ( row > 0 )
f85afd4e 8872 {
2d66e025
MB
8873 int cw, ch;
8874 m_gridWin->GetClientSize( &cw, &ch );
60ff3b99 8875
7c1cb261 8876 int y = GetRowTop(row);
a967f048 8877 int newRow = internalYToRow( y - ch + 1 );
ef5df12b 8878
a967f048 8879 if ( newRow == row )
2d66e025 8880 {
c2f5b920 8881 // row > 0, so newRow can never be less than 0 here.
2d66e025
MB
8882 newRow = row - 1;
8883 }
8f177c8e 8884
2d66e025
MB
8885 MakeCellVisible( newRow, m_currentCellCoords.GetCol() );
8886 SetCurrentCell( newRow, m_currentCellCoords.GetCol() );
60ff3b99 8887
ca65c044 8888 return true;
f85afd4e 8889 }
2d66e025 8890
ca65c044 8891 return false;
2d66e025
MB
8892}
8893
8894bool wxGrid::MovePageDown()
8895{
4db6714b
KH
8896 if ( m_currentCellCoords == wxGridNoCellCoords )
8897 return false;
60ff3b99 8898
2d66e025 8899 int row = m_currentCellCoords.GetRow();
ccdee36f 8900 if ( (row + 1) < m_numRows )
f85afd4e 8901 {
2d66e025
MB
8902 int cw, ch;
8903 m_gridWin->GetClientSize( &cw, &ch );
60ff3b99 8904
7c1cb261 8905 int y = GetRowTop(row);
a967f048
RG
8906 int newRow = internalYToRow( y + ch );
8907 if ( newRow == row )
2d66e025 8908 {
c2f5b920 8909 // row < m_numRows, so newRow can't overflow here.
ca65c044 8910 newRow = row + 1;
2d66e025
MB
8911 }
8912
8913 MakeCellVisible( newRow, m_currentCellCoords.GetCol() );
8914 SetCurrentCell( newRow, m_currentCellCoords.GetCol() );
60ff3b99 8915
ca65c044 8916 return true;
f85afd4e 8917 }
2d66e025 8918
ca65c044 8919 return false;
f85afd4e 8920}
8f177c8e 8921
5c8fc7c1 8922bool wxGrid::MoveCursorUpBlock( bool expandSelection )
2d66e025
MB
8923{
8924 if ( m_table &&
2f024384 8925 m_currentCellCoords != wxGridNoCellCoords &&
2d66e025
MB
8926 m_currentCellCoords.GetRow() > 0 )
8927 {
8928 int row = m_currentCellCoords.GetRow();
8929 int col = m_currentCellCoords.GetCol();
8930
8931 if ( m_table->IsEmptyCell(row, col) )
8932 {
8933 // starting in an empty cell: find the next block of
8934 // non-empty cells
8935 //
8936 while ( row > 0 )
8937 {
2f024384 8938 row--;
4db6714b
KH
8939 if ( !(m_table->IsEmptyCell(row, col)) )
8940 break;
2d66e025
MB
8941 }
8942 }
2f024384 8943 else if ( m_table->IsEmptyCell(row - 1, col) )
2d66e025
MB
8944 {
8945 // starting at the top of a block: find the next block
8946 //
8947 row--;
8948 while ( row > 0 )
8949 {
2f024384 8950 row--;
4db6714b
KH
8951 if ( !(m_table->IsEmptyCell(row, col)) )
8952 break;
2d66e025
MB
8953 }
8954 }
8955 else
8956 {
8957 // starting within a block: find the top of the block
8958 //
8959 while ( row > 0 )
8960 {
2f024384 8961 row--;
2d66e025
MB
8962 if ( m_table->IsEmptyCell(row, col) )
8963 {
2f024384 8964 row++;
2d66e025
MB
8965 break;
8966 }
8967 }
8968 }
f85afd4e 8969
2d66e025 8970 MakeCellVisible( row, col );
5c8fc7c1 8971 if ( expandSelection )
d95b0c2b
SN
8972 {
8973 m_selectingKeyboard = wxGridCellCoords( row, col );
c9097836 8974 HighlightBlock( m_currentCellCoords, m_selectingKeyboard );
d95b0c2b
SN
8975 }
8976 else
aa5e1f75
SN
8977 {
8978 ClearSelection();
8979 SetCurrentCell( row, col );
8980 }
4db6714b 8981
ca65c044 8982 return true;
2d66e025 8983 }
f85afd4e 8984
ca65c044 8985 return false;
2d66e025 8986}
f85afd4e 8987
5c8fc7c1 8988bool wxGrid::MoveCursorDownBlock( bool expandSelection )
f85afd4e 8989{
2d66e025 8990 if ( m_table &&
2f024384
DS
8991 m_currentCellCoords != wxGridNoCellCoords &&
8992 m_currentCellCoords.GetRow() < m_numRows - 1 )
f85afd4e 8993 {
2d66e025
MB
8994 int row = m_currentCellCoords.GetRow();
8995 int col = m_currentCellCoords.GetCol();
8996
8997 if ( m_table->IsEmptyCell(row, col) )
8998 {
8999 // starting in an empty cell: find the next block of
9000 // non-empty cells
9001 //
2f024384 9002 while ( row < m_numRows - 1 )
2d66e025 9003 {
2f024384 9004 row++;
4db6714b
KH
9005 if ( !(m_table->IsEmptyCell(row, col)) )
9006 break;
2d66e025
MB
9007 }
9008 }
2f024384 9009 else if ( m_table->IsEmptyCell(row + 1, col) )
2d66e025
MB
9010 {
9011 // starting at the bottom of a block: find the next block
9012 //
9013 row++;
2f024384 9014 while ( row < m_numRows - 1 )
2d66e025 9015 {
2f024384 9016 row++;
4db6714b
KH
9017 if ( !(m_table->IsEmptyCell(row, col)) )
9018 break;
2d66e025
MB
9019 }
9020 }
9021 else
9022 {
9023 // starting within a block: find the bottom of the block
9024 //
2f024384 9025 while ( row < m_numRows - 1 )
2d66e025 9026 {
2f024384 9027 row++;
2d66e025
MB
9028 if ( m_table->IsEmptyCell(row, col) )
9029 {
2f024384 9030 row--;
2d66e025
MB
9031 break;
9032 }
9033 }
9034 }
9035
9036 MakeCellVisible( row, col );
5c8fc7c1 9037 if ( expandSelection )
d95b0c2b
SN
9038 {
9039 m_selectingKeyboard = wxGridCellCoords( row, col );
c9097836 9040 HighlightBlock( m_currentCellCoords, m_selectingKeyboard );
d95b0c2b
SN
9041 }
9042 else
aa5e1f75
SN
9043 {
9044 ClearSelection();
9045 SetCurrentCell( row, col );
9046 }
2d66e025 9047
ca65c044 9048 return true;
f85afd4e 9049 }
f85afd4e 9050
ca65c044 9051 return false;
2d66e025 9052}
f85afd4e 9053
5c8fc7c1 9054bool wxGrid::MoveCursorLeftBlock( bool expandSelection )
f85afd4e 9055{
2d66e025 9056 if ( m_table &&
2f024384 9057 m_currentCellCoords != wxGridNoCellCoords &&
2d66e025 9058 m_currentCellCoords.GetCol() > 0 )
f85afd4e 9059 {
2d66e025
MB
9060 int row = m_currentCellCoords.GetRow();
9061 int col = m_currentCellCoords.GetCol();
9062
9063 if ( m_table->IsEmptyCell(row, col) )
9064 {
9065 // starting in an empty cell: find the next block of
9066 // non-empty cells
9067 //
9068 while ( col > 0 )
9069 {
2f024384 9070 col--;
4db6714b
KH
9071 if ( !(m_table->IsEmptyCell(row, col)) )
9072 break;
2d66e025
MB
9073 }
9074 }
2f024384 9075 else if ( m_table->IsEmptyCell(row, col - 1) )
2d66e025
MB
9076 {
9077 // starting at the left of a block: find the next block
9078 //
9079 col--;
9080 while ( col > 0 )
9081 {
2f024384 9082 col--;
4db6714b
KH
9083 if ( !(m_table->IsEmptyCell(row, col)) )
9084 break;
2d66e025
MB
9085 }
9086 }
9087 else
9088 {
9089 // starting within a block: find the left of the block
9090 //
9091 while ( col > 0 )
9092 {
2f024384 9093 col--;
2d66e025
MB
9094 if ( m_table->IsEmptyCell(row, col) )
9095 {
2f024384 9096 col++;
2d66e025
MB
9097 break;
9098 }
9099 }
9100 }
f85afd4e 9101
2d66e025 9102 MakeCellVisible( row, col );
5c8fc7c1 9103 if ( expandSelection )
d95b0c2b
SN
9104 {
9105 m_selectingKeyboard = wxGridCellCoords( row, col );
c9097836 9106 HighlightBlock( m_currentCellCoords, m_selectingKeyboard );
d95b0c2b
SN
9107 }
9108 else
aa5e1f75
SN
9109 {
9110 ClearSelection();
9111 SetCurrentCell( row, col );
9112 }
8f177c8e 9113
ca65c044 9114 return true;
f85afd4e 9115 }
2d66e025 9116
ca65c044 9117 return false;
f85afd4e
MB
9118}
9119
5c8fc7c1 9120bool wxGrid::MoveCursorRightBlock( bool expandSelection )
f85afd4e 9121{
2d66e025 9122 if ( m_table &&
2f024384
DS
9123 m_currentCellCoords != wxGridNoCellCoords &&
9124 m_currentCellCoords.GetCol() < m_numCols - 1 )
f85afd4e 9125 {
2d66e025
MB
9126 int row = m_currentCellCoords.GetRow();
9127 int col = m_currentCellCoords.GetCol();
8f177c8e 9128
2d66e025
MB
9129 if ( m_table->IsEmptyCell(row, col) )
9130 {
9131 // starting in an empty cell: find the next block of
9132 // non-empty cells
9133 //
2f024384 9134 while ( col < m_numCols - 1 )
2d66e025 9135 {
2f024384 9136 col++;
4db6714b
KH
9137 if ( !(m_table->IsEmptyCell(row, col)) )
9138 break;
2d66e025
MB
9139 }
9140 }
2f024384 9141 else if ( m_table->IsEmptyCell(row, col + 1) )
2d66e025
MB
9142 {
9143 // starting at the right of a block: find the next block
9144 //
9145 col++;
2f024384 9146 while ( col < m_numCols - 1 )
2d66e025 9147 {
2f024384 9148 col++;
4db6714b
KH
9149 if ( !(m_table->IsEmptyCell(row, col)) )
9150 break;
2d66e025
MB
9151 }
9152 }
9153 else
9154 {
9155 // starting within a block: find the right of the block
9156 //
2f024384 9157 while ( col < m_numCols - 1 )
2d66e025 9158 {
2f024384 9159 col++;
2d66e025
MB
9160 if ( m_table->IsEmptyCell(row, col) )
9161 {
2f024384 9162 col--;
2d66e025
MB
9163 break;
9164 }
9165 }
9166 }
8f177c8e 9167
2d66e025 9168 MakeCellVisible( row, col );
5c8fc7c1 9169 if ( expandSelection )
d95b0c2b
SN
9170 {
9171 m_selectingKeyboard = wxGridCellCoords( row, col );
c9097836 9172 HighlightBlock( m_currentCellCoords, m_selectingKeyboard );
d95b0c2b
SN
9173 }
9174 else
aa5e1f75
SN
9175 {
9176 ClearSelection();
9177 SetCurrentCell( row, col );
9178 }
f85afd4e 9179
ca65c044 9180 return true;
f85afd4e 9181 }
2d66e025 9182
ca65c044 9183 return false;
f85afd4e
MB
9184}
9185
f85afd4e 9186//
2d66e025 9187// ------ Label values and formatting
f85afd4e
MB
9188//
9189
9190void wxGrid::GetRowLabelAlignment( int *horiz, int *vert )
9191{
2f024384
DS
9192 if ( horiz )
9193 *horiz = m_rowLabelHorizAlign;
9194 if ( vert )
9195 *vert = m_rowLabelVertAlign;
f85afd4e
MB
9196}
9197
9198void wxGrid::GetColLabelAlignment( int *horiz, int *vert )
9199{
2f024384
DS
9200 if ( horiz )
9201 *horiz = m_colLabelHorizAlign;
9202 if ( vert )
9203 *vert = m_colLabelVertAlign;
f85afd4e
MB
9204}
9205
d43851f7
JS
9206int wxGrid::GetColLabelTextOrientation()
9207{
9208 return m_colLabelTextOrientation;
9209}
9210
f85afd4e
MB
9211wxString wxGrid::GetRowLabelValue( int row )
9212{
9213 if ( m_table )
9214 {
9215 return m_table->GetRowLabelValue( row );
9216 }
9217 else
9218 {
9219 wxString s;
9220 s << row;
9221 return s;
9222 }
9223}
9224
9225wxString wxGrid::GetColLabelValue( int col )
9226{
9227 if ( m_table )
9228 {
9229 return m_table->GetColLabelValue( col );
9230 }
9231 else
9232 {
9233 wxString s;
9234 s << col;
9235 return s;
9236 }
9237}
9238
9239void wxGrid::SetRowLabelSize( int width )
9240{
2e8cd977
MB
9241 width = wxMax( width, 0 );
9242 if ( width != m_rowLabelWidth )
9243 {
2e8cd977
MB
9244 if ( width == 0 )
9245 {
ca65c044
WS
9246 m_rowLabelWin->Show( false );
9247 m_cornerLabelWin->Show( false );
2e8cd977 9248 }
7807d81c 9249 else if ( m_rowLabelWidth == 0 )
2e8cd977 9250 {
ca65c044 9251 m_rowLabelWin->Show( true );
2f024384
DS
9252 if ( m_colLabelHeight > 0 )
9253 m_cornerLabelWin->Show( true );
2e8cd977 9254 }
b99be8fb 9255
2e8cd977 9256 m_rowLabelWidth = width;
7807d81c 9257 CalcWindowSizes();
ca65c044 9258 wxScrolledWindow::Refresh( true );
2e8cd977 9259 }
f85afd4e
MB
9260}
9261
9262void wxGrid::SetColLabelSize( int height )
9263{
7807d81c 9264 height = wxMax( height, 0 );
2e8cd977
MB
9265 if ( height != m_colLabelHeight )
9266 {
2e8cd977
MB
9267 if ( height == 0 )
9268 {
ca65c044
WS
9269 m_colLabelWin->Show( false );
9270 m_cornerLabelWin->Show( false );
2e8cd977 9271 }
7807d81c 9272 else if ( m_colLabelHeight == 0 )
2e8cd977 9273 {
ca65c044 9274 m_colLabelWin->Show( true );
a9339fe2
DS
9275 if ( m_rowLabelWidth > 0 )
9276 m_cornerLabelWin->Show( true );
2e8cd977 9277 }
7807d81c 9278
2e8cd977 9279 m_colLabelHeight = height;
7807d81c 9280 CalcWindowSizes();
ca65c044 9281 wxScrolledWindow::Refresh( true );
2e8cd977 9282 }
f85afd4e
MB
9283}
9284
9285void wxGrid::SetLabelBackgroundColour( const wxColour& colour )
9286{
2d66e025
MB
9287 if ( m_labelBackgroundColour != colour )
9288 {
9289 m_labelBackgroundColour = colour;
9290 m_rowLabelWin->SetBackgroundColour( colour );
9291 m_colLabelWin->SetBackgroundColour( colour );
9292 m_cornerLabelWin->SetBackgroundColour( colour );
9293
9294 if ( !GetBatchCount() )
9295 {
9296 m_rowLabelWin->Refresh();
9297 m_colLabelWin->Refresh();
9298 m_cornerLabelWin->Refresh();
9299 }
9300 }
f85afd4e
MB
9301}
9302
9303void wxGrid::SetLabelTextColour( const wxColour& colour )
9304{
2d66e025
MB
9305 if ( m_labelTextColour != colour )
9306 {
9307 m_labelTextColour = colour;
9308 if ( !GetBatchCount() )
9309 {
9310 m_rowLabelWin->Refresh();
9311 m_colLabelWin->Refresh();
9312 }
9313 }
f85afd4e
MB
9314}
9315
9316void wxGrid::SetLabelFont( const wxFont& font )
9317{
9318 m_labelFont = font;
2d66e025
MB
9319 if ( !GetBatchCount() )
9320 {
9321 m_rowLabelWin->Refresh();
9322 m_colLabelWin->Refresh();
9323 }
f85afd4e
MB
9324}
9325
9326void wxGrid::SetRowLabelAlignment( int horiz, int vert )
9327{
4c7277db
MB
9328 // allow old (incorrect) defs to be used
9329 switch ( horiz )
9330 {
9331 case wxLEFT: horiz = wxALIGN_LEFT; break;
9332 case wxRIGHT: horiz = wxALIGN_RIGHT; break;
9333 case wxCENTRE: horiz = wxALIGN_CENTRE; break;
9334 }
84912ef8 9335
4c7277db
MB
9336 switch ( vert )
9337 {
9338 case wxTOP: vert = wxALIGN_TOP; break;
9339 case wxBOTTOM: vert = wxALIGN_BOTTOM; break;
9340 case wxCENTRE: vert = wxALIGN_CENTRE; break;
9341 }
84912ef8 9342
4c7277db 9343 if ( horiz == wxALIGN_LEFT || horiz == wxALIGN_CENTRE || horiz == wxALIGN_RIGHT )
f85afd4e
MB
9344 {
9345 m_rowLabelHorizAlign = horiz;
9346 }
8f177c8e 9347
4c7277db 9348 if ( vert == wxALIGN_TOP || vert == wxALIGN_CENTRE || vert == wxALIGN_BOTTOM )
f85afd4e
MB
9349 {
9350 m_rowLabelVertAlign = vert;
9351 }
9352
2d66e025
MB
9353 if ( !GetBatchCount() )
9354 {
9355 m_rowLabelWin->Refresh();
60ff3b99 9356 }
f85afd4e
MB
9357}
9358
9359void wxGrid::SetColLabelAlignment( int horiz, int vert )
9360{
4c7277db
MB
9361 // allow old (incorrect) defs to be used
9362 switch ( horiz )
9363 {
9364 case wxLEFT: horiz = wxALIGN_LEFT; break;
9365 case wxRIGHT: horiz = wxALIGN_RIGHT; break;
9366 case wxCENTRE: horiz = wxALIGN_CENTRE; break;
9367 }
84912ef8 9368
4c7277db
MB
9369 switch ( vert )
9370 {
9371 case wxTOP: vert = wxALIGN_TOP; break;
9372 case wxBOTTOM: vert = wxALIGN_BOTTOM; break;
9373 case wxCENTRE: vert = wxALIGN_CENTRE; break;
9374 }
84912ef8 9375
4c7277db 9376 if ( horiz == wxALIGN_LEFT || horiz == wxALIGN_CENTRE || horiz == wxALIGN_RIGHT )
f85afd4e
MB
9377 {
9378 m_colLabelHorizAlign = horiz;
9379 }
8f177c8e 9380
4c7277db 9381 if ( vert == wxALIGN_TOP || vert == wxALIGN_CENTRE || vert == wxALIGN_BOTTOM )
f85afd4e
MB
9382 {
9383 m_colLabelVertAlign = vert;
9384 }
9385
2d66e025
MB
9386 if ( !GetBatchCount() )
9387 {
2d66e025 9388 m_colLabelWin->Refresh();
60ff3b99 9389 }
f85afd4e
MB
9390}
9391
ca65c044
WS
9392// Note: under MSW, the default column label font must be changed because it
9393// does not support vertical printing
d43851f7
JS
9394//
9395// Example: wxFont font(9, wxSWISS, wxNORMAL, wxBOLD);
ca65c044
WS
9396// pGrid->SetLabelFont(font);
9397// pGrid->SetColLabelTextOrientation(wxVERTICAL);
d43851f7
JS
9398//
9399void wxGrid::SetColLabelTextOrientation( int textOrientation )
9400{
4db6714b 9401 if ( textOrientation == wxHORIZONTAL || textOrientation == wxVERTICAL )
d43851f7 9402 m_colLabelTextOrientation = textOrientation;
d43851f7
JS
9403
9404 if ( !GetBatchCount() )
d43851f7 9405 m_colLabelWin->Refresh();
d43851f7
JS
9406}
9407
f85afd4e
MB
9408void wxGrid::SetRowLabelValue( int row, const wxString& s )
9409{
9410 if ( m_table )
9411 {
9412 m_table->SetRowLabelValue( row, s );
2d66e025
MB
9413 if ( !GetBatchCount() )
9414 {
ccdee36f 9415 wxRect rect = CellToRect( row, 0 );
70c7a608
SN
9416 if ( rect.height > 0 )
9417 {
cb309039 9418 CalcScrolledPosition(0, rect.y, &rect.x, &rect.y);
b1944ebc 9419 rect.x = 0;
70c7a608 9420 rect.width = m_rowLabelWidth;
ca65c044 9421 m_rowLabelWin->Refresh( true, &rect );
70c7a608 9422 }
2d66e025 9423 }
f85afd4e
MB
9424 }
9425}
9426
9427void wxGrid::SetColLabelValue( int col, const wxString& s )
9428{
9429 if ( m_table )
9430 {
9431 m_table->SetColLabelValue( col, s );
2d66e025
MB
9432 if ( !GetBatchCount() )
9433 {
70c7a608
SN
9434 wxRect rect = CellToRect( 0, col );
9435 if ( rect.width > 0 )
9436 {
cb309039 9437 CalcScrolledPosition(rect.x, 0, &rect.x, &rect.y);
b1944ebc 9438 rect.y = 0;
70c7a608 9439 rect.height = m_colLabelHeight;
ca65c044 9440 m_colLabelWin->Refresh( true, &rect );
70c7a608 9441 }
2d66e025 9442 }
f85afd4e
MB
9443 }
9444}
9445
9446void wxGrid::SetGridLineColour( const wxColour& colour )
9447{
2d66e025
MB
9448 if ( m_gridLineColour != colour )
9449 {
9450 m_gridLineColour = colour;
60ff3b99 9451
2d66e025
MB
9452 wxClientDC dc( m_gridWin );
9453 PrepareDC( dc );
c6a51dcd 9454 DrawAllGridLines( dc, wxRegion() );
2d66e025 9455 }
f85afd4e
MB
9456}
9457
f6bcfd97
BP
9458void wxGrid::SetCellHighlightColour( const wxColour& colour )
9459{
9460 if ( m_cellHighlightColour != colour )
9461 {
9462 m_cellHighlightColour = colour;
9463
9464 wxClientDC dc( m_gridWin );
9465 PrepareDC( dc );
9466 wxGridCellAttr* attr = GetCellAttr(m_currentCellCoords);
9467 DrawCellHighlight(dc, attr);
9468 attr->DecRef();
9469 }
9470}
9471
d2520c85
RD
9472void wxGrid::SetCellHighlightPenWidth(int width)
9473{
4db6714b
KH
9474 if (m_cellHighlightPenWidth != width)
9475 {
d2520c85
RD
9476 m_cellHighlightPenWidth = width;
9477
9478 // Just redrawing the cell highlight is not enough since that won't
9479 // make any visible change if the the thickness is getting smaller.
9480 int row = m_currentCellCoords.GetRow();
9481 int col = m_currentCellCoords.GetCol();
9482 if ( GetColWidth(col) <= 0 || GetRowHeight(row) <= 0 )
9483 return;
2f024384 9484
d2520c85 9485 wxRect rect = CellToRect(row, col);
ca65c044 9486 m_gridWin->Refresh(true, &rect);
d2520c85
RD
9487 }
9488}
9489
9490void wxGrid::SetCellHighlightROPenWidth(int width)
9491{
4db6714b
KH
9492 if (m_cellHighlightROPenWidth != width)
9493 {
d2520c85
RD
9494 m_cellHighlightROPenWidth = width;
9495
9496 // Just redrawing the cell highlight is not enough since that won't
9497 // make any visible change if the the thickness is getting smaller.
9498 int row = m_currentCellCoords.GetRow();
9499 int col = m_currentCellCoords.GetCol();
9500 if ( GetColWidth(col) <= 0 || GetRowHeight(row) <= 0 )
9501 return;
ccdee36f 9502
d2520c85 9503 wxRect rect = CellToRect(row, col);
ca65c044 9504 m_gridWin->Refresh(true, &rect);
d2520c85
RD
9505 }
9506}
9507
f85afd4e
MB
9508void wxGrid::EnableGridLines( bool enable )
9509{
9510 if ( enable != m_gridLinesEnabled )
9511 {
9512 m_gridLinesEnabled = enable;
2d66e025
MB
9513
9514 if ( !GetBatchCount() )
9515 {
9516 if ( enable )
9517 {
9518 wxClientDC dc( m_gridWin );
9519 PrepareDC( dc );
c6a51dcd 9520 DrawAllGridLines( dc, wxRegion() );
2d66e025
MB
9521 }
9522 else
9523 {
9524 m_gridWin->Refresh();
9525 }
9526 }
f85afd4e
MB
9527 }
9528}
9529
f85afd4e
MB
9530int wxGrid::GetDefaultRowSize()
9531{
9532 return m_defaultRowHeight;
9533}
9534
9535int wxGrid::GetRowSize( int row )
9536{
b99be8fb
VZ
9537 wxCHECK_MSG( row >= 0 && row < m_numRows, 0, _T("invalid row index") );
9538
7c1cb261 9539 return GetRowHeight(row);
f85afd4e
MB
9540}
9541
9542int wxGrid::GetDefaultColSize()
9543{
9544 return m_defaultColWidth;
9545}
9546
9547int wxGrid::GetColSize( int col )
9548{
b99be8fb
VZ
9549 wxCHECK_MSG( col >= 0 && col < m_numCols, 0, _T("invalid column index") );
9550
7c1cb261 9551 return GetColWidth(col);
f85afd4e
MB
9552}
9553
2e9a6788
VZ
9554// ============================================================================
9555// access to the grid attributes: each of them has a default value in the grid
9556// itself and may be overidden on a per-cell basis
9557// ============================================================================
9558
9559// ----------------------------------------------------------------------------
9560// setting default attributes
9561// ----------------------------------------------------------------------------
9562
9563void wxGrid::SetDefaultCellBackgroundColour( const wxColour& col )
9564{
2796cce3 9565 m_defaultCellAttr->SetBackgroundColour(col);
c916e13b
RR
9566#ifdef __WXGTK__
9567 m_gridWin->SetBackgroundColour(col);
9568#endif
2e9a6788
VZ
9569}
9570
9571void wxGrid::SetDefaultCellTextColour( const wxColour& col )
9572{
2796cce3 9573 m_defaultCellAttr->SetTextColour(col);
2e9a6788
VZ
9574}
9575
9576void wxGrid::SetDefaultCellAlignment( int horiz, int vert )
9577{
2796cce3 9578 m_defaultCellAttr->SetAlignment(horiz, vert);
2e9a6788
VZ
9579}
9580
27f35b66
SN
9581void wxGrid::SetDefaultCellOverflow( bool allow )
9582{
9583 m_defaultCellAttr->SetOverflow(allow);
9584}
9585
2e9a6788
VZ
9586void wxGrid::SetDefaultCellFont( const wxFont& font )
9587{
2796cce3
RD
9588 m_defaultCellAttr->SetFont(font);
9589}
9590
ca63e8e9
RD
9591// For editors and renderers the type registry takes precedence over the
9592// default attr, so we need to register the new editor/renderer for the string
9593// data type in order to make setting a default editor/renderer appear to
9594// work correctly.
9595
0ba143c9
RD
9596void wxGrid::SetDefaultRenderer(wxGridCellRenderer *renderer)
9597{
ca63e8e9
RD
9598 RegisterDataType(wxGRID_VALUE_STRING,
9599 renderer,
9600 GetDefaultEditorForType(wxGRID_VALUE_STRING));
0ba143c9 9601}
2e9a6788 9602
0ba143c9
RD
9603void wxGrid::SetDefaultEditor(wxGridCellEditor *editor)
9604{
ca63e8e9
RD
9605 RegisterDataType(wxGRID_VALUE_STRING,
9606 GetDefaultRendererForType(wxGRID_VALUE_STRING),
42841dfc 9607 editor);
0ba143c9 9608}
9b4aede2 9609
2e9a6788
VZ
9610// ----------------------------------------------------------------------------
9611// access to the default attrbiutes
9612// ----------------------------------------------------------------------------
9613
f85afd4e
MB
9614wxColour wxGrid::GetDefaultCellBackgroundColour()
9615{
2796cce3 9616 return m_defaultCellAttr->GetBackgroundColour();
f85afd4e
MB
9617}
9618
2e9a6788
VZ
9619wxColour wxGrid::GetDefaultCellTextColour()
9620{
2796cce3 9621 return m_defaultCellAttr->GetTextColour();
2e9a6788
VZ
9622}
9623
9624wxFont wxGrid::GetDefaultCellFont()
9625{
2796cce3 9626 return m_defaultCellAttr->GetFont();
2e9a6788
VZ
9627}
9628
9629void wxGrid::GetDefaultCellAlignment( int *horiz, int *vert )
9630{
2796cce3 9631 m_defaultCellAttr->GetAlignment(horiz, vert);
2e9a6788
VZ
9632}
9633
27f35b66
SN
9634bool wxGrid::GetDefaultCellOverflow()
9635{
9636 return m_defaultCellAttr->GetOverflow();
9637}
9638
0ba143c9
RD
9639wxGridCellRenderer *wxGrid::GetDefaultRenderer() const
9640{
0b190b0f 9641 return m_defaultCellAttr->GetRenderer(NULL, 0, 0);
0ba143c9 9642}
ab79958a 9643
0ba143c9
RD
9644wxGridCellEditor *wxGrid::GetDefaultEditor() const
9645{
4db6714b 9646 return m_defaultCellAttr->GetEditor(NULL, 0, 0);
0ba143c9 9647}
9b4aede2 9648
2e9a6788
VZ
9649// ----------------------------------------------------------------------------
9650// access to cell attributes
9651// ----------------------------------------------------------------------------
9652
b99be8fb 9653wxColour wxGrid::GetCellBackgroundColour(int row, int col)
f85afd4e 9654{
0a976765 9655 wxGridCellAttr *attr = GetCellAttr(row, col);
2796cce3 9656 wxColour colour = attr->GetBackgroundColour();
39bcce60 9657 attr->DecRef();
2f024384 9658
b99be8fb 9659 return colour;
f85afd4e
MB
9660}
9661
b99be8fb 9662wxColour wxGrid::GetCellTextColour( int row, int col )
f85afd4e 9663{
0a976765 9664 wxGridCellAttr *attr = GetCellAttr(row, col);
2796cce3 9665 wxColour colour = attr->GetTextColour();
39bcce60 9666 attr->DecRef();
2f024384 9667
b99be8fb 9668 return colour;
f85afd4e
MB
9669}
9670
b99be8fb 9671wxFont wxGrid::GetCellFont( int row, int col )
f85afd4e 9672{
0a976765 9673 wxGridCellAttr *attr = GetCellAttr(row, col);
2796cce3 9674 wxFont font = attr->GetFont();
39bcce60 9675 attr->DecRef();
2f024384 9676
b99be8fb 9677 return font;
f85afd4e
MB
9678}
9679
b99be8fb 9680void wxGrid::GetCellAlignment( int row, int col, int *horiz, int *vert )
f85afd4e 9681{
0a976765 9682 wxGridCellAttr *attr = GetCellAttr(row, col);
2796cce3 9683 attr->GetAlignment(horiz, vert);
39bcce60 9684 attr->DecRef();
2e9a6788
VZ
9685}
9686
27f35b66
SN
9687bool wxGrid::GetCellOverflow( int row, int col )
9688{
9689 wxGridCellAttr *attr = GetCellAttr(row, col);
9690 bool allow = attr->GetOverflow();
9691 attr->DecRef();
4db6714b 9692
27f35b66
SN
9693 return allow;
9694}
9695
9696void wxGrid::GetCellSize( int row, int col, int *num_rows, int *num_cols )
9697{
9698 wxGridCellAttr *attr = GetCellAttr(row, col);
9699 attr->GetSize( num_rows, num_cols );
9700 attr->DecRef();
9701}
9702
2796cce3
RD
9703wxGridCellRenderer* wxGrid::GetCellRenderer(int row, int col)
9704{
9705 wxGridCellAttr* attr = GetCellAttr(row, col);
28a77bc4 9706 wxGridCellRenderer* renderer = attr->GetRenderer(this, row, col);
2796cce3 9707 attr->DecRef();
0b190b0f 9708
2796cce3
RD
9709 return renderer;
9710}
9711
9b4aede2
RD
9712wxGridCellEditor* wxGrid::GetCellEditor(int row, int col)
9713{
9714 wxGridCellAttr* attr = GetCellAttr(row, col);
28a77bc4 9715 wxGridCellEditor* editor = attr->GetEditor(this, row, col);
9b4aede2 9716 attr->DecRef();
0b190b0f 9717
9b4aede2
RD
9718 return editor;
9719}
9720
283b7808
VZ
9721bool wxGrid::IsReadOnly(int row, int col) const
9722{
9723 wxGridCellAttr* attr = GetCellAttr(row, col);
9724 bool isReadOnly = attr->IsReadOnly();
9725 attr->DecRef();
4db6714b 9726
283b7808
VZ
9727 return isReadOnly;
9728}
9729
2e9a6788 9730// ----------------------------------------------------------------------------
758cbedf 9731// attribute support: cache, automatic provider creation, ...
2e9a6788
VZ
9732// ----------------------------------------------------------------------------
9733
9734bool wxGrid::CanHaveAttributes()
9735{
9736 if ( !m_table )
9737 {
ca65c044 9738 return false;
2e9a6788
VZ
9739 }
9740
f2d76237 9741 return m_table->CanHaveAttributes();
2e9a6788
VZ
9742}
9743
0a976765
VZ
9744void wxGrid::ClearAttrCache()
9745{
9746 if ( m_attrCache.row != -1 )
9747 {
39bcce60 9748 wxSafeDecRef(m_attrCache.attr);
19d7140e 9749 m_attrCache.attr = NULL;
0a976765
VZ
9750 m_attrCache.row = -1;
9751 }
9752}
9753
9754void wxGrid::CacheAttr(int row, int col, wxGridCellAttr *attr) const
9755{
2b5f62a0
VZ
9756 if ( attr != NULL )
9757 {
9758 wxGrid *self = (wxGrid *)this; // const_cast
0a976765 9759
2b5f62a0
VZ
9760 self->ClearAttrCache();
9761 self->m_attrCache.row = row;
9762 self->m_attrCache.col = col;
9763 self->m_attrCache.attr = attr;
9764 wxSafeIncRef(attr);
9765 }
0a976765
VZ
9766}
9767
9768bool wxGrid::LookupAttr(int row, int col, wxGridCellAttr **attr) const
9769{
9770 if ( row == m_attrCache.row && col == m_attrCache.col )
9771 {
9772 *attr = m_attrCache.attr;
39bcce60 9773 wxSafeIncRef(m_attrCache.attr);
0a976765
VZ
9774
9775#ifdef DEBUG_ATTR_CACHE
9776 gs_nAttrCacheHits++;
9777#endif
9778
ca65c044 9779 return true;
0a976765
VZ
9780 }
9781 else
9782 {
9783#ifdef DEBUG_ATTR_CACHE
9784 gs_nAttrCacheMisses++;
9785#endif
4db6714b 9786
ca65c044 9787 return false;
0a976765
VZ
9788 }
9789}
9790
2e9a6788
VZ
9791wxGridCellAttr *wxGrid::GetCellAttr(int row, int col) const
9792{
a373d23b
SN
9793 wxGridCellAttr *attr = NULL;
9794 // Additional test to avoid looking at the cache e.g. for
9795 // wxNoCellCoords, as this will confuse memory management.
9796 if ( row >= 0 )
9797 {
3ed884a0
SN
9798 if ( !LookupAttr(row, col, &attr) )
9799 {
c2f5b920 9800 attr = m_table ? m_table->GetAttr(row, col, wxGridCellAttr::Any)
3ed884a0
SN
9801 : (wxGridCellAttr *)NULL;
9802 CacheAttr(row, col, attr);
9803 }
0a976765 9804 }
4db6714b 9805
508011ce
VZ
9806 if (attr)
9807 {
2796cce3 9808 attr->SetDefAttr(m_defaultCellAttr);
508011ce
VZ
9809 }
9810 else
9811 {
2796cce3
RD
9812 attr = m_defaultCellAttr;
9813 attr->IncRef();
9814 }
2e9a6788 9815
0a976765
VZ
9816 return attr;
9817}
9818
9819wxGridCellAttr *wxGrid::GetOrCreateCellAttr(int row, int col) const
9820{
19d7140e 9821 wxGridCellAttr *attr = (wxGridCellAttr *)NULL;
71e60f70 9822 bool canHave = ((wxGrid*)this)->CanHaveAttributes();
0a976765 9823
71e60f70
RD
9824 wxCHECK_MSG( canHave, attr, _T("Cell attributes not allowed"));
9825 wxCHECK_MSG( m_table, attr, _T("must have a table") );
1df4050d
VZ
9826
9827 attr = m_table->GetAttr(row, col, wxGridCellAttr::Cell);
9828 if ( !attr )
9829 {
9830 attr = new wxGridCellAttr(m_defaultCellAttr);
9831
9832 // artificially inc the ref count to match DecRef() in caller
9833 attr->IncRef();
9834 m_table->SetAttr(attr, row, col);
9835 }
0a976765 9836
2e9a6788
VZ
9837 return attr;
9838}
9839
0b190b0f
VZ
9840// ----------------------------------------------------------------------------
9841// setting column attributes (wrappers around SetColAttr)
9842// ----------------------------------------------------------------------------
9843
9844void wxGrid::SetColFormatBool(int col)
9845{
9846 SetColFormatCustom(col, wxGRID_VALUE_BOOL);
9847}
9848
9849void wxGrid::SetColFormatNumber(int col)
9850{
9851 SetColFormatCustom(col, wxGRID_VALUE_NUMBER);
9852}
9853
9854void wxGrid::SetColFormatFloat(int col, int width, int precision)
9855{
9856 wxString typeName = wxGRID_VALUE_FLOAT;
9857 if ( (width != -1) || (precision != -1) )
9858 {
9859 typeName << _T(':') << width << _T(',') << precision;
9860 }
9861
9862 SetColFormatCustom(col, typeName);
9863}
9864
9865void wxGrid::SetColFormatCustom(int col, const wxString& typeName)
9866{
999836aa 9867 wxGridCellAttr *attr = m_table->GetAttr(-1, col, wxGridCellAttr::Col );
4db6714b 9868 if (!attr)
19d7140e 9869 attr = new wxGridCellAttr;
0b190b0f
VZ
9870 wxGridCellRenderer *renderer = GetDefaultRendererForType(typeName);
9871 attr->SetRenderer(renderer);
9872
9873 SetColAttr(col, attr);
19d7140e 9874
0b190b0f
VZ
9875}
9876
758cbedf
VZ
9877// ----------------------------------------------------------------------------
9878// setting cell attributes: this is forwarded to the table
9879// ----------------------------------------------------------------------------
9880
27f35b66
SN
9881void wxGrid::SetAttr(int row, int col, wxGridCellAttr *attr)
9882{
9883 if ( CanHaveAttributes() )
9884 {
9885 m_table->SetAttr(attr, row, col);
9886 ClearAttrCache();
9887 }
9888 else
9889 {
9890 wxSafeDecRef(attr);
9891 }
9892}
9893
758cbedf
VZ
9894void wxGrid::SetRowAttr(int row, wxGridCellAttr *attr)
9895{
9896 if ( CanHaveAttributes() )
9897 {
9898 m_table->SetRowAttr(attr, row);
19d7140e 9899 ClearAttrCache();
758cbedf
VZ
9900 }
9901 else
9902 {
39bcce60 9903 wxSafeDecRef(attr);
758cbedf
VZ
9904 }
9905}
9906
9907void wxGrid::SetColAttr(int col, wxGridCellAttr *attr)
9908{
9909 if ( CanHaveAttributes() )
9910 {
9911 m_table->SetColAttr(attr, col);
19d7140e 9912 ClearAttrCache();
758cbedf
VZ
9913 }
9914 else
9915 {
39bcce60 9916 wxSafeDecRef(attr);
758cbedf
VZ
9917 }
9918}
9919
2e9a6788
VZ
9920void wxGrid::SetCellBackgroundColour( int row, int col, const wxColour& colour )
9921{
9922 if ( CanHaveAttributes() )
9923 {
0a976765 9924 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
2e9a6788
VZ
9925 attr->SetBackgroundColour(colour);
9926 attr->DecRef();
9927 }
9928}
9929
9930void wxGrid::SetCellTextColour( int row, int col, const wxColour& colour )
9931{
9932 if ( CanHaveAttributes() )
9933 {
0a976765 9934 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
2e9a6788
VZ
9935 attr->SetTextColour(colour);
9936 attr->DecRef();
9937 }
9938}
9939
9940void wxGrid::SetCellFont( int row, int col, const wxFont& font )
9941{
9942 if ( CanHaveAttributes() )
9943 {
0a976765 9944 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
2e9a6788
VZ
9945 attr->SetFont(font);
9946 attr->DecRef();
9947 }
9948}
9949
9950void wxGrid::SetCellAlignment( int row, int col, int horiz, int vert )
9951{
9952 if ( CanHaveAttributes() )
9953 {
0a976765 9954 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
2e9a6788
VZ
9955 attr->SetAlignment(horiz, vert);
9956 attr->DecRef();
ab79958a
VZ
9957 }
9958}
9959
27f35b66
SN
9960void wxGrid::SetCellOverflow( int row, int col, bool allow )
9961{
9962 if ( CanHaveAttributes() )
9963 {
9964 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
9965 attr->SetOverflow(allow);
9966 attr->DecRef();
9967 }
9968}
9969
9970void wxGrid::SetCellSize( int row, int col, int num_rows, int num_cols )
9971{
9972 if ( CanHaveAttributes() )
9973 {
9974 int cell_rows, cell_cols;
9975
9976 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
9977 attr->GetSize(&cell_rows, &cell_cols);
9978 attr->SetSize(num_rows, num_cols);
9979 attr->DecRef();
9980
9981 // Cannot set the size of a cell to 0 or negative values
9982 // While it is perfectly legal to do that, this function cannot
9983 // handle all the possibilies, do it by hand by getting the CellAttr.
9984 // You can only set the size of a cell to 1,1 or greater with this fn
9985 wxASSERT_MSG( !((cell_rows < 1) || (cell_cols < 1)),
9986 wxT("wxGrid::SetCellSize setting cell size that is already part of another cell"));
9987 wxASSERT_MSG( !((num_rows < 1) || (num_cols < 1)),
9988 wxT("wxGrid::SetCellSize setting cell size to < 1"));
9989
9990 // if this was already a multicell then "turn off" the other cells first
9991 if ((cell_rows > 1) || (cell_rows > 1))
9992 {
9993 int i, j;
2f024384 9994 for (j=row; j < row + cell_rows; j++)
27f35b66 9995 {
2f024384 9996 for (i=col; i < col + cell_cols; i++)
27f35b66
SN
9997 {
9998 if ((i != col) || (j != row))
9999 {
10000 wxGridCellAttr *attr_stub = GetOrCreateCellAttr(j, i);
10001 attr_stub->SetSize( 1, 1 );
10002 attr_stub->DecRef();
10003 }
10004 }
10005 }
10006 }
10007
10008 // mark the cells that will be covered by this cell to
10009 // negative or zero values to point back at this cell
10010 if (((num_rows > 1) || (num_cols > 1)) && (num_rows >= 1) && (num_cols >= 1))
10011 {
10012 int i, j;
2f024384 10013 for (j=row; j < row + num_rows; j++)
27f35b66 10014 {
2f024384 10015 for (i=col; i < col + num_cols; i++)
27f35b66
SN
10016 {
10017 if ((i != col) || (j != row))
10018 {
10019 wxGridCellAttr *attr_stub = GetOrCreateCellAttr(j, i);
2f024384 10020 attr_stub->SetSize( row - j, col - i );
27f35b66
SN
10021 attr_stub->DecRef();
10022 }
10023 }
10024 }
10025 }
10026 }
10027}
10028
ab79958a
VZ
10029void wxGrid::SetCellRenderer(int row, int col, wxGridCellRenderer *renderer)
10030{
10031 if ( CanHaveAttributes() )
10032 {
0a976765 10033 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
ab79958a
VZ
10034 attr->SetRenderer(renderer);
10035 attr->DecRef();
9b4aede2
RD
10036 }
10037}
10038
10039void wxGrid::SetCellEditor(int row, int col, wxGridCellEditor* editor)
10040{
10041 if ( CanHaveAttributes() )
10042 {
10043 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
10044 attr->SetEditor(editor);
10045 attr->DecRef();
283b7808
VZ
10046 }
10047}
10048
10049void wxGrid::SetReadOnly(int row, int col, bool isReadOnly)
10050{
10051 if ( CanHaveAttributes() )
10052 {
10053 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
10054 attr->SetReadOnly(isReadOnly);
10055 attr->DecRef();
2e9a6788 10056 }
f85afd4e
MB
10057}
10058
f2d76237
RD
10059// ----------------------------------------------------------------------------
10060// Data type registration
10061// ----------------------------------------------------------------------------
10062
10063void wxGrid::RegisterDataType(const wxString& typeName,
10064 wxGridCellRenderer* renderer,
10065 wxGridCellEditor* editor)
10066{
10067 m_typeRegistry->RegisterDataType(typeName, renderer, editor);
10068}
10069
10070
a9339fe2 10071wxGridCellEditor * wxGrid::GetDefaultEditorForCell(int row, int col) const
f2d76237
RD
10072{
10073 wxString typeName = m_table->GetTypeName(row, col);
10074 return GetDefaultEditorForType(typeName);
10075}
10076
a9339fe2 10077wxGridCellRenderer * wxGrid::GetDefaultRendererForCell(int row, int col) const
f2d76237
RD
10078{
10079 wxString typeName = m_table->GetTypeName(row, col);
10080 return GetDefaultRendererForType(typeName);
10081}
10082
a9339fe2 10083wxGridCellEditor * wxGrid::GetDefaultEditorForType(const wxString& typeName) const
f2d76237 10084{
c4608a8a 10085 int index = m_typeRegistry->FindOrCloneDataType(typeName);
0b190b0f
VZ
10086 if ( index == wxNOT_FOUND )
10087 {
f87ab3cc 10088 wxString errStr;
4db6714b
KH
10089
10090 errStr.Printf(wxT("Unknown data type name [%s]"), typeName.c_str());
10091 wxFAIL_MSG(errStr.c_str());
0b190b0f 10092
f2d76237
RD
10093 return NULL;
10094 }
0b190b0f 10095
f2d76237
RD
10096 return m_typeRegistry->GetEditor(index);
10097}
10098
a9339fe2 10099wxGridCellRenderer * wxGrid::GetDefaultRendererForType(const wxString& typeName) const
f2d76237 10100{
c4608a8a 10101 int index = m_typeRegistry->FindOrCloneDataType(typeName);
0b190b0f
VZ
10102 if ( index == wxNOT_FOUND )
10103 {
2f024384 10104 wxString errStr;
4db6714b
KH
10105
10106 errStr.Printf(wxT("Unknown data type name [%s]"), typeName.c_str());
10107 wxFAIL_MSG(errStr.c_str());
0b190b0f 10108
c4608a8a 10109 return NULL;
e72b4213 10110 }
0b190b0f 10111
c4608a8a 10112 return m_typeRegistry->GetRenderer(index);
f2d76237
RD
10113}
10114
2e9a6788
VZ
10115// ----------------------------------------------------------------------------
10116// row/col size
10117// ----------------------------------------------------------------------------
10118
6e8524b1
MB
10119void wxGrid::EnableDragRowSize( bool enable )
10120{
10121 m_canDragRowSize = enable;
10122}
10123
6e8524b1
MB
10124void wxGrid::EnableDragColSize( bool enable )
10125{
10126 m_canDragColSize = enable;
10127}
10128
4cfa5de6
RD
10129void wxGrid::EnableDragGridSize( bool enable )
10130{
10131 m_canDragGridSize = enable;
10132}
10133
79dbea21
RD
10134void wxGrid::EnableDragCell( bool enable )
10135{
10136 m_canDragCell = enable;
10137}
6e8524b1 10138
f85afd4e
MB
10139void wxGrid::SetDefaultRowSize( int height, bool resizeExistingRows )
10140{
b8d24d4e 10141 m_defaultRowHeight = wxMax( height, m_minAcceptableRowHeight );
f85afd4e
MB
10142
10143 if ( resizeExistingRows )
10144 {
b1da8107
SN
10145 // since we are resizing all rows to the default row size,
10146 // we can simply clear the row heights and row bottoms
10147 // arrays (which also allows us to take advantage of
10148 // some speed optimisations)
10149 m_rowHeights.Empty();
10150 m_rowBottoms.Empty();
edb89f7e
VZ
10151 if ( !GetBatchCount() )
10152 CalcDimensions();
f85afd4e
MB
10153 }
10154}
10155
10156void wxGrid::SetRowSize( int row, int height )
10157{
b99be8fb 10158 wxCHECK_RET( row >= 0 && row < m_numRows, _T("invalid row index") );
60ff3b99 10159
b4bfd0fa 10160 // See comment in SetColSize
4db6714b
KH
10161 if ( height < GetRowMinimalAcceptableHeight())
10162 return;
b8d24d4e 10163
7c1cb261
VZ
10164 if ( m_rowHeights.IsEmpty() )
10165 {
10166 // need to really create the array
10167 InitRowHeights();
10168 }
60ff3b99 10169
b99be8fb
VZ
10170 int h = wxMax( 0, height );
10171 int diff = h - m_rowHeights[row];
60ff3b99 10172
b99be8fb 10173 m_rowHeights[row] = h;
7c1cb261 10174 int i;
56b6cf26 10175 for ( i = row; i < m_numRows; i++ )
f85afd4e 10176 {
b99be8fb 10177 m_rowBottoms[i] += diff;
f85afd4e 10178 }
2f024384 10179
faec5a43
SN
10180 if ( !GetBatchCount() )
10181 CalcDimensions();
f85afd4e
MB
10182}
10183
10184void wxGrid::SetDefaultColSize( int width, bool resizeExistingCols )
10185{
b8d24d4e 10186 m_defaultColWidth = wxMax( width, m_minAcceptableColWidth );
f85afd4e
MB
10187
10188 if ( resizeExistingCols )
10189 {
b1da8107
SN
10190 // since we are resizing all columns to the default column size,
10191 // we can simply clear the col widths and col rights
10192 // arrays (which also allows us to take advantage of
10193 // some speed optimisations)
10194 m_colWidths.Empty();
10195 m_colRights.Empty();
edb89f7e
VZ
10196 if ( !GetBatchCount() )
10197 CalcDimensions();
f85afd4e
MB
10198 }
10199}
10200
10201void wxGrid::SetColSize( int col, int width )
10202{
b99be8fb 10203 wxCHECK_RET( col >= 0 && col < m_numCols, _T("invalid column index") );
60ff3b99 10204
43947979 10205 // should we check that it's bigger than GetColMinimalWidth(col) here?
b4bfd0fa
RG
10206 // (VZ)
10207 // No, because it is reasonable to assume the library user know's
d4175745 10208 // what he is doing. However we should test against the weaker
ccdee36f 10209 // constraint of minimalAcceptableWidth, as this breaks rendering
3e13956a 10210 //
b4bfd0fa 10211 // This test then fixes sf.net bug #645734
3e13956a 10212
962a48f6 10213 if ( width < GetColMinimalAcceptableWidth() )
4db6714b 10214 return;
3e13956a 10215
7c1cb261
VZ
10216 if ( m_colWidths.IsEmpty() )
10217 {
10218 // need to really create the array
10219 InitColWidths();
10220 }
f85afd4e 10221
56b6cf26 10222 // if < 0 then calculate new width from label
4db6714b 10223 if ( width < 0 )
73145b0e 10224 {
56b6cf26
DS
10225 long w, h;
10226 wxArrayString lines;
10227 wxClientDC dc(m_colLabelWin);
10228 dc.SetFont(GetLabelFont());
10229 StringToLines(GetColLabelValue(col), lines);
10230 GetTextBoxSize(dc, lines, &w, &h);
10231 width = w + 6;
73145b0e 10232 }
962a48f6 10233
b99be8fb
VZ
10234 int w = wxMax( 0, width );
10235 int diff = w - m_colWidths[col];
10236 m_colWidths[col] = w;
60ff3b99 10237
7c1cb261 10238 int i;
d4175745
VZ
10239 int colPos;
10240 for ( colPos = GetColPos( col ); colPos < m_numCols; colPos++ )
f85afd4e 10241 {
d4175745 10242 i = GetColAt( colPos );
b99be8fb 10243 m_colRights[i] += diff;
f85afd4e 10244 }
962a48f6 10245
faec5a43
SN
10246 if ( !GetBatchCount() )
10247 CalcDimensions();
f85afd4e
MB
10248}
10249
43947979
VZ
10250void wxGrid::SetColMinimalWidth( int col, int width )
10251{
4db6714b
KH
10252 if (width > GetColMinimalAcceptableWidth())
10253 {
c6fbe2f0 10254 wxLongToLongHashMap::key_type key = (wxLongToLongHashMap::key_type)col;
8253f2e0 10255 m_colMinWidths[key] = width;
b8d24d4e 10256 }
af547d51
VZ
10257}
10258
10259void wxGrid::SetRowMinimalHeight( int row, int width )
10260{
4db6714b
KH
10261 if (width > GetRowMinimalAcceptableHeight())
10262 {
c6fbe2f0 10263 wxLongToLongHashMap::key_type key = (wxLongToLongHashMap::key_type)row;
8253f2e0 10264 m_rowMinHeights[key] = width;
b8d24d4e 10265 }
43947979
VZ
10266}
10267
10268int wxGrid::GetColMinimalWidth(int col) const
10269{
c6fbe2f0 10270 wxLongToLongHashMap::key_type key = (wxLongToLongHashMap::key_type)col;
8253f2e0 10271 wxLongToLongHashMap::const_iterator it = m_colMinWidths.find(key);
962a48f6 10272
ba8c1601 10273 return it != m_colMinWidths.end() ? (int)it->second : m_minAcceptableColWidth;
af547d51
VZ
10274}
10275
10276int wxGrid::GetRowMinimalHeight(int row) const
10277{
c6fbe2f0 10278 wxLongToLongHashMap::key_type key = (wxLongToLongHashMap::key_type)row;
8253f2e0 10279 wxLongToLongHashMap::const_iterator it = m_rowMinHeights.find(key);
962a48f6 10280
ba8c1601 10281 return it != m_rowMinHeights.end() ? (int)it->second : m_minAcceptableRowHeight;
b8d24d4e
RG
10282}
10283
10284void wxGrid::SetColMinimalAcceptableWidth( int width )
10285{
20c84410 10286 // We do allow a width of 0 since this gives us
962a48f6
DS
10287 // an easy way to temporarily hiding columns.
10288 if ( width >= 0 )
10289 m_minAcceptableColWidth = width;
b8d24d4e
RG
10290}
10291
10292void wxGrid::SetRowMinimalAcceptableHeight( int height )
10293{
20c84410 10294 // We do allow a height of 0 since this gives us
962a48f6
DS
10295 // an easy way to temporarily hiding rows.
10296 if ( height >= 0 )
10297 m_minAcceptableRowHeight = height;
17a1ebd1 10298}
b8d24d4e
RG
10299
10300int wxGrid::GetColMinimalAcceptableWidth() const
10301{
10302 return m_minAcceptableColWidth;
10303}
10304
10305int wxGrid::GetRowMinimalAcceptableHeight() const
10306{
10307 return m_minAcceptableRowHeight;
43947979
VZ
10308}
10309
57c086ef
VZ
10310// ----------------------------------------------------------------------------
10311// auto sizing
10312// ----------------------------------------------------------------------------
10313
af547d51 10314void wxGrid::AutoSizeColOrRow( int colOrRow, bool setAsMin, bool column )
65e4e78e
VZ
10315{
10316 wxClientDC dc(m_gridWin);
10317
962a48f6 10318 // cancel editing of cell
13f6e9e8
RG
10319 HideCellEditControl();
10320 SaveEditControlValue();
10321
962a48f6 10322 // init both of them to avoid compiler warnings, even if we only need one
a95e38c0
VZ
10323 int row = -1,
10324 col = -1;
af547d51
VZ
10325 if ( column )
10326 col = colOrRow;
10327 else
10328 row = colOrRow;
10329
10330 wxCoord extent, extentMax = 0;
10331 int max = column ? m_numRows : m_numCols;
39bcce60 10332 for ( int rowOrCol = 0; rowOrCol < max; rowOrCol++ )
65e4e78e 10333 {
af547d51
VZ
10334 if ( column )
10335 row = rowOrCol;
10336 else
10337 col = rowOrCol;
10338
2f024384
DS
10339 wxGridCellAttr *attr = GetCellAttr(row, col);
10340 wxGridCellRenderer *renderer = attr->GetRenderer(this, row, col);
65e4e78e
VZ
10341 if ( renderer )
10342 {
af547d51
VZ
10343 wxSize size = renderer->GetBestSize(*this, *attr, dc, row, col);
10344 extent = column ? size.x : size.y;
10345 if ( extent > extentMax )
af547d51 10346 extentMax = extent;
0b190b0f
VZ
10347
10348 renderer->DecRef();
65e4e78e
VZ
10349 }
10350
10351 attr->DecRef();
10352 }
10353
af547d51
VZ
10354 // now also compare with the column label extent
10355 wxCoord w, h;
65e4e78e 10356 dc.SetFont( GetLabelFont() );
294d195c
MB
10357
10358 if ( column )
d43851f7 10359 {
294d195c 10360 dc.GetTextExtent( GetColLabelValue(col), &w, &h );
4db6714b 10361 if ( GetColLabelTextOrientation() == wxVERTICAL )
d43851f7
JS
10362 w = h;
10363 }
294d195c 10364 else
ee495e4c 10365 dc.GetTextExtent( GetRowLabelValue(row), &w, &h );
294d195c 10366
af547d51
VZ
10367 extent = column ? w : h;
10368 if ( extent > extentMax )
af547d51 10369 extentMax = extent;
65e4e78e 10370
af547d51 10371 if ( !extentMax )
65e4e78e 10372 {
af547d51 10373 // empty column - give default extent (notice that if extentMax is less
2f024384 10374 // than default extent but != 0, it's OK)
af547d51 10375 extentMax = column ? m_defaultColWidth : m_defaultRowHeight;
65e4e78e
VZ
10376 }
10377 else
10378 {
a95e38c0 10379 if ( column )
a95e38c0
VZ
10380 // leave some space around text
10381 extentMax += 10;
f6bcfd97 10382 else
f6bcfd97 10383 extentMax += 6;
65e4e78e
VZ
10384 }
10385
edb89f7e
VZ
10386 if ( column )
10387 {
962a48f6 10388 SetColSize( col, extentMax );
faec5a43
SN
10389 if ( !GetBatchCount() )
10390 {
edb89f7e
VZ
10391 int cw, ch, dummy;
10392 m_gridWin->GetClientSize( &cw, &ch );
10393 wxRect rect ( CellToRect( 0, col ) );
10394 rect.y = 0;
10395 CalcScrolledPosition(rect.x, 0, &rect.x, &dummy);
10396 rect.width = cw - rect.x;
10397 rect.height = m_colLabelHeight;
ca65c044 10398 m_colLabelWin->Refresh( true, &rect );
edb89f7e
VZ
10399 }
10400 }
10401 else
10402 {
39bcce60 10403 SetRowSize(row, extentMax);
faec5a43
SN
10404 if ( !GetBatchCount() )
10405 {
edb89f7e
VZ
10406 int cw, ch, dummy;
10407 m_gridWin->GetClientSize( &cw, &ch );
ccdee36f 10408 wxRect rect( CellToRect( row, 0 ) );
edb89f7e
VZ
10409 rect.x = 0;
10410 CalcScrolledPosition(0, rect.y, &dummy, &rect.y);
10411 rect.width = m_rowLabelWidth;
faec5a43 10412 rect.height = ch - rect.y;
ca65c044 10413 m_rowLabelWin->Refresh( true, &rect );
edb89f7e 10414 }
faec5a43 10415 }
2f024384 10416
65e4e78e
VZ
10417 if ( setAsMin )
10418 {
af547d51
VZ
10419 if ( column )
10420 SetColMinimalWidth(col, extentMax);
10421 else
39bcce60 10422 SetRowMinimalHeight(row, extentMax);
65e4e78e
VZ
10423 }
10424}
10425
266e8367 10426int wxGrid::SetOrCalcColumnSizes(bool calcOnly, bool setAsMin)
65e4e78e 10427{
57c086ef
VZ
10428 int width = m_rowLabelWidth;
10429
faec5a43
SN
10430 if ( !calcOnly )
10431 BeginBatch();
97a9929e 10432
65e4e78e
VZ
10433 for ( int col = 0; col < m_numCols; col++ )
10434 {
266e8367 10435 if ( !calcOnly )
266e8367 10436 AutoSizeColumn(col, setAsMin);
57c086ef
VZ
10437
10438 width += GetColWidth(col);
65e4e78e 10439 }
97a9929e 10440
faec5a43
SN
10441 if ( !calcOnly )
10442 EndBatch();
97a9929e 10443
266e8367 10444 return width;
65e4e78e
VZ
10445}
10446
266e8367 10447int wxGrid::SetOrCalcRowSizes(bool calcOnly, bool setAsMin)
57c086ef
VZ
10448{
10449 int height = m_colLabelHeight;
10450
faec5a43
SN
10451 if ( !calcOnly )
10452 BeginBatch();
97a9929e 10453
57c086ef
VZ
10454 for ( int row = 0; row < m_numRows; row++ )
10455 {
af547d51 10456 if ( !calcOnly )
af547d51 10457 AutoSizeRow(row, setAsMin);
57c086ef
VZ
10458
10459 height += GetRowHeight(row);
10460 }
97a9929e 10461
faec5a43
SN
10462 if ( !calcOnly )
10463 EndBatch();
97a9929e 10464
266e8367 10465 return height;
57c086ef
VZ
10466}
10467
10468void wxGrid::AutoSize()
10469{
97a9929e
VZ
10470 BeginBatch();
10471
ca65c044 10472 wxSize size(SetOrCalcColumnSizes(false), SetOrCalcRowSizes(false));
97a9929e
VZ
10473
10474 // round up the size to a multiple of scroll step - this ensures that we
10475 // won't get the scrollbars if we're sized exactly to this width
2b5f62a0
VZ
10476 // CalcDimension adds m_extraWidth + 1 etc. to calculate the necessary
10477 // scrollbar steps
2f024384
DS
10478 wxSize sizeFit(
10479 GetScrollX(size.x + m_extraWidth + 1) * m_scrollLineX,
a9339fe2 10480 GetScrollY(size.y + m_extraHeight + 1) * m_scrollLineY );
97a9929e 10481
2b5f62a0 10482 // distribute the extra space between the columns/rows to avoid having
97a9929e 10483 // extra white space
2b5f62a0
VZ
10484
10485 // Remove the extra m_extraWidth + 1 added above
10486 wxCoord diff = sizeFit.x - size.x + (m_extraWidth + 1);
10487 if ( diff && m_numCols )
97a9929e
VZ
10488 {
10489 // try to resize the columns uniformly
10490 wxCoord diffPerCol = diff / m_numCols;
10491 if ( diffPerCol )
10492 {
10493 for ( int col = 0; col < m_numCols; col++ )
10494 {
10495 SetColSize(col, GetColWidth(col) + diffPerCol);
10496 }
10497 }
10498
10499 // add remaining amount to the last columns
10500 diff -= diffPerCol * m_numCols;
10501 if ( diff )
10502 {
10503 for ( int col = m_numCols - 1; col >= m_numCols - diff; col-- )
10504 {
10505 SetColSize(col, GetColWidth(col) + 1);
10506 }
10507 }
10508 }
10509
10510 // same for rows
2b5f62a0
VZ
10511 diff = sizeFit.y - size.y - (m_extraHeight + 1);
10512 if ( diff && m_numRows )
97a9929e
VZ
10513 {
10514 // try to resize the columns uniformly
10515 wxCoord diffPerRow = diff / m_numRows;
10516 if ( diffPerRow )
10517 {
10518 for ( int row = 0; row < m_numRows; row++ )
10519 {
10520 SetRowSize(row, GetRowHeight(row) + diffPerRow);
10521 }
10522 }
10523
10524 // add remaining amount to the last rows
10525 diff -= diffPerRow * m_numRows;
10526 if ( diff )
10527 {
10528 for ( int row = m_numRows - 1; row >= m_numRows - diff; row-- )
10529 {
10530 SetRowSize(row, GetRowHeight(row) + 1);
10531 }
10532 }
10533 }
10534
10535 EndBatch();
10536
10537 SetClientSize(sizeFit);
266e8367
VZ
10538}
10539
d43851f7
JS
10540void wxGrid::AutoSizeRowLabelSize( int row )
10541{
10542 wxArrayString lines;
10543 long w, h;
10544
10545 // Hide the edit control, so it
4db6714b
KH
10546 // won't interfere with drag-shrinking.
10547 if ( IsCellEditControlShown() )
d43851f7
JS
10548 {
10549 HideCellEditControl();
10550 SaveEditControlValue();
10551 }
10552
10553 // autosize row height depending on label text
10554 StringToLines( GetRowLabelValue( row ), lines );
10555 wxClientDC dc( m_rowLabelWin );
ccdee36f 10556 GetTextBoxSize( dc, lines, &w, &h );
4db6714b 10557 if ( h < m_defaultRowHeight )
d43851f7
JS
10558 h = m_defaultRowHeight;
10559 SetRowSize(row, h);
10560 ForceRefresh();
10561}
10562
10563void wxGrid::AutoSizeColLabelSize( int col )
10564{
10565 wxArrayString lines;
10566 long w, h;
10567
10568 // Hide the edit control, so it
c2f5b920 10569 // won't interfere with drag-shrinking.
4db6714b 10570 if ( IsCellEditControlShown() )
d43851f7
JS
10571 {
10572 HideCellEditControl();
10573 SaveEditControlValue();
10574 }
10575
10576 // autosize column width depending on label text
10577 StringToLines( GetColLabelValue( col ), lines );
10578 wxClientDC dc( m_colLabelWin );
4db6714b 10579 if ( GetColLabelTextOrientation() == wxHORIZONTAL )
2f024384 10580 GetTextBoxSize( dc, lines, &w, &h );
d43851f7 10581 else
2f024384 10582 GetTextBoxSize( dc, lines, &h, &w );
4db6714b 10583 if ( w < m_defaultColWidth )
d43851f7
JS
10584 w = m_defaultColWidth;
10585 SetColSize(col, w);
10586 ForceRefresh();
10587}
10588
266e8367
VZ
10589wxSize wxGrid::DoGetBestSize() const
10590{
10591 // don't set sizes, only calculate them
10592 wxGrid *self = (wxGrid *)this; // const_cast
10593
84035150 10594 int width, height;
ca65c044
WS
10595 width = self->SetOrCalcColumnSizes(true);
10596 height = self->SetOrCalcRowSizes(true);
84035150 10597
4db6714b
KH
10598 if (!width)
10599 width = 100;
10600 if (!height)
10601 height = 80;
42841dfc 10602
4db6714b 10603 // Round up to a multiple the scroll rate
c2f5b920 10604 // NOTE: this still doesn't get rid of the scrollbars;
4db6714b 10605 // is there any magic incantation for that?
6d308072
RD
10606 int xpu, ypu;
10607 GetScrollPixelsPerUnit(&xpu, &ypu);
b39a5dc4
RD
10608 if (xpu)
10609 width += 1 + xpu - (width % xpu);
10610 if (ypu)
10611 height += 1 + ypu - (height % ypu);
42841dfc 10612
6d308072 10613 // limit to 1/4 of the screen size
84035150 10614 int maxwidth, maxheight;
4db6714b 10615 wxDisplaySize( &maxwidth, &maxheight );
6d308072 10616 maxwidth /= 2;
42841dfc 10617 maxheight /= 2;
4db6714b
KH
10618 if ( width > maxwidth )
10619 width = maxwidth;
10620 if ( height > maxheight )
10621 height = maxheight;
42841dfc 10622
6d308072 10623 wxSize best(width, height);
962a48f6 10624
6d308072
RD
10625 // NOTE: This size should be cached, but first we need to add calls to
10626 // InvalidateBestSize everywhere that could change the results of this
10627 // calculation.
10628 // CacheBestSize(size);
962a48f6 10629
6d308072 10630 return best;
266e8367
VZ
10631}
10632
10633void wxGrid::Fit()
10634{
10635 AutoSize();
57c086ef
VZ
10636}
10637
6fc0f38f
SN
10638wxPen& wxGrid::GetDividerPen() const
10639{
10640 return wxNullPen;
10641}
10642
57c086ef
VZ
10643// ----------------------------------------------------------------------------
10644// cell value accessor functions
10645// ----------------------------------------------------------------------------
f85afd4e
MB
10646
10647void wxGrid::SetCellValue( int row, int col, const wxString& s )
10648{
10649 if ( m_table )
10650 {
f6bcfd97 10651 m_table->SetValue( row, col, s );
2d66e025
MB
10652 if ( !GetBatchCount() )
10653 {
27f35b66
SN
10654 int dummy;
10655 wxRect rect( CellToRect( row, col ) );
10656 rect.x = 0;
10657 rect.width = m_gridWin->GetClientSize().GetWidth();
10658 CalcScrolledPosition(0, rect.y, &dummy, &rect.y);
ca65c044 10659 m_gridWin->Refresh( false, &rect );
2d66e025 10660 }
60ff3b99 10661
f85afd4e 10662 if ( m_currentCellCoords.GetRow() == row &&
4cfa5de6 10663 m_currentCellCoords.GetCol() == col &&
f6bcfd97
BP
10664 IsCellEditControlShown())
10665 // Note: If we are using IsCellEditControlEnabled,
10666 // this interacts badly with calling SetCellValue from
10667 // an EVT_GRID_CELL_CHANGE handler.
f85afd4e 10668 {
4cfa5de6
RD
10669 HideCellEditControl();
10670 ShowCellEditControl(); // will reread data from table
f85afd4e 10671 }
f85afd4e
MB
10672 }
10673}
10674
962a48f6 10675// ----------------------------------------------------------------------------
2f024384 10676// block, row and column selection
962a48f6 10677// ----------------------------------------------------------------------------
f85afd4e
MB
10678
10679void wxGrid::SelectRow( int row, bool addToSelected )
10680{
b5808881 10681 if ( IsSelection() && !addToSelected )
e32352cf 10682 ClearSelection();
70c7a608 10683
3f3dc2ef 10684 if ( m_selection )
ca65c044 10685 m_selection->SelectRow( row, false, addToSelected );
f85afd4e
MB
10686}
10687
f85afd4e
MB
10688void wxGrid::SelectCol( int col, bool addToSelected )
10689{
b5808881 10690 if ( IsSelection() && !addToSelected )
e32352cf 10691 ClearSelection();
f85afd4e 10692
3f3dc2ef 10693 if ( m_selection )
ca65c044 10694 m_selection->SelectCol( col, false, addToSelected );
f85afd4e
MB
10695}
10696
84912ef8 10697void wxGrid::SelectBlock( int topRow, int leftCol, int bottomRow, int rightCol,
c9097836 10698 bool addToSelected )
f85afd4e 10699{
c9097836
MB
10700 if ( IsSelection() && !addToSelected )
10701 ClearSelection();
f85afd4e 10702
3f3dc2ef
VZ
10703 if ( m_selection )
10704 m_selection->SelectBlock( topRow, leftCol, bottomRow, rightCol,
ca65c044 10705 false, addToSelected );
f85afd4e
MB
10706}
10707
10708void wxGrid::SelectAll()
10709{
f74d0b57 10710 if ( m_numRows > 0 && m_numCols > 0 )
3f3dc2ef
VZ
10711 {
10712 if ( m_selection )
ccdee36f 10713 m_selection->SelectBlock( 0, 0, m_numRows - 1, m_numCols - 1 );
3f3dc2ef 10714 }
b5808881 10715}
f85afd4e 10716
962a48f6
DS
10717// ----------------------------------------------------------------------------
10718// cell, row and col deselection
10719// ----------------------------------------------------------------------------
f7b4b343
VZ
10720
10721void wxGrid::DeselectRow( int row )
10722{
3f3dc2ef
VZ
10723 if ( !m_selection )
10724 return;
10725
f7b4b343
VZ
10726 if ( m_selection->GetSelectionMode() == wxGrid::wxGridSelectRows )
10727 {
10728 if ( m_selection->IsInSelection(row, 0 ) )
2f024384 10729 m_selection->ToggleCellSelection(row, 0);
ffdd3c98 10730 }
f7b4b343
VZ
10731 else
10732 {
10733 int nCols = GetNumberCols();
2f024384 10734 for ( int i = 0; i < nCols; i++ )
f7b4b343
VZ
10735 {
10736 if ( m_selection->IsInSelection(row, i ) )
2f024384 10737 m_selection->ToggleCellSelection(row, i);
f7b4b343
VZ
10738 }
10739 }
10740}
10741
10742void wxGrid::DeselectCol( int col )
10743{
3f3dc2ef
VZ
10744 if ( !m_selection )
10745 return;
10746
f7b4b343
VZ
10747 if ( m_selection->GetSelectionMode() == wxGrid::wxGridSelectColumns )
10748 {
10749 if ( m_selection->IsInSelection(0, col ) )
2f024384 10750 m_selection->ToggleCellSelection(0, col);
f7b4b343
VZ
10751 }
10752 else
10753 {
10754 int nRows = GetNumberRows();
2f024384 10755 for ( int i = 0; i < nRows; i++ )
f7b4b343
VZ
10756 {
10757 if ( m_selection->IsInSelection(i, col ) )
10758 m_selection->ToggleCellSelection(i, col);
10759 }
10760 }
10761}
10762
10763void wxGrid::DeselectCell( int row, int col )
10764{
3f3dc2ef 10765 if ( m_selection && m_selection->IsInSelection(row, col) )
f7b4b343
VZ
10766 m_selection->ToggleCellSelection(row, col);
10767}
10768
b5808881
SN
10769bool wxGrid::IsSelection()
10770{
3f3dc2ef 10771 return ( m_selection && (m_selection->IsSelection() ||
b5808881 10772 ( m_selectingTopLeft != wxGridNoCellCoords &&
3f3dc2ef 10773 m_selectingBottomRight != wxGridNoCellCoords) ) );
f85afd4e
MB
10774}
10775
84035150 10776bool wxGrid::IsInSelection( int row, int col ) const
b5808881 10777{
3f3dc2ef 10778 return ( m_selection && (m_selection->IsInSelection( row, col ) ||
b5808881
SN
10779 ( row >= m_selectingTopLeft.GetRow() &&
10780 col >= m_selectingTopLeft.GetCol() &&
10781 row <= m_selectingBottomRight.GetRow() &&
3f3dc2ef 10782 col <= m_selectingBottomRight.GetCol() )) );
b5808881 10783}
f85afd4e 10784
aa5b8857
SN
10785wxGridCellCoordsArray wxGrid::GetSelectedCells() const
10786{
4db6714b
KH
10787 if (!m_selection)
10788 {
10789 wxGridCellCoordsArray a;
10790 return a;
10791 }
10792
aa5b8857
SN
10793 return m_selection->m_cellSelection;
10794}
4db6714b 10795
aa5b8857
SN
10796wxGridCellCoordsArray wxGrid::GetSelectionBlockTopLeft() const
10797{
4db6714b
KH
10798 if (!m_selection)
10799 {
10800 wxGridCellCoordsArray a;
10801 return a;
10802 }
10803
aa5b8857
SN
10804 return m_selection->m_blockSelectionTopLeft;
10805}
4db6714b 10806
aa5b8857
SN
10807wxGridCellCoordsArray wxGrid::GetSelectionBlockBottomRight() const
10808{
4db6714b
KH
10809 if (!m_selection)
10810 {
10811 wxGridCellCoordsArray a;
10812 return a;
10813 }
10814
a8de8190 10815 return m_selection->m_blockSelectionBottomRight;
aa5b8857 10816}
4db6714b 10817
aa5b8857
SN
10818wxArrayInt wxGrid::GetSelectedRows() const
10819{
4db6714b
KH
10820 if (!m_selection)
10821 {
10822 wxArrayInt a;
10823 return a;
10824 }
10825
aa5b8857
SN
10826 return m_selection->m_rowSelection;
10827}
4db6714b 10828
aa5b8857
SN
10829wxArrayInt wxGrid::GetSelectedCols() const
10830{
4db6714b
KH
10831 if (!m_selection)
10832 {
10833 wxArrayInt a;
10834 return a;
10835 }
10836
aa5b8857
SN
10837 return m_selection->m_colSelection;
10838}
10839
f85afd4e
MB
10840void wxGrid::ClearSelection()
10841{
b524b5c6
VZ
10842 m_selectingTopLeft =
10843 m_selectingBottomRight =
10844 m_selectingKeyboard = wxGridNoCellCoords;
3f3dc2ef
VZ
10845 if ( m_selection )
10846 m_selection->ClearSelection();
8f177c8e 10847}
f85afd4e 10848
da6af900 10849// This function returns the rectangle that encloses the given block
2d66e025
MB
10850// in device coords clipped to the client size of the grid window.
10851//
58dd5b3b
MB
10852wxRect wxGrid::BlockToDeviceRect( const wxGridCellCoords &topLeft,
10853 const wxGridCellCoords &bottomRight )
f85afd4e 10854{
58dd5b3b 10855 wxRect rect( wxGridNoCellRect );
f85afd4e
MB
10856 wxRect cellRect;
10857
58dd5b3b
MB
10858 cellRect = CellToRect( topLeft );
10859 if ( cellRect != wxGridNoCellRect )
f85afd4e 10860 {
58dd5b3b
MB
10861 rect = cellRect;
10862 }
10863 else
10864 {
962a48f6 10865 rect = wxRect(0, 0, 0, 0);
58dd5b3b 10866 }
60ff3b99 10867
58dd5b3b
MB
10868 cellRect = CellToRect( bottomRight );
10869 if ( cellRect != wxGridNoCellRect )
10870 {
10871 rect += cellRect;
2d66e025
MB
10872 }
10873 else
10874 {
10875 return wxGridNoCellRect;
f85afd4e
MB
10876 }
10877
27f35b66
SN
10878 int i, j;
10879 int left = rect.GetLeft();
10880 int top = rect.GetTop();
10881 int right = rect.GetRight();
10882 int bottom = rect.GetBottom();
10883
10884 int leftCol = topLeft.GetCol();
10885 int topRow = topLeft.GetRow();
10886 int rightCol = bottomRight.GetCol();
10887 int bottomRow = bottomRight.GetRow();
10888
3ed884a0
SN
10889 if (left > right)
10890 {
10891 i = left;
10892 left = right;
10893 right = i;
10894 i = leftCol;
4db6714b 10895 leftCol = rightCol;
3ed884a0
SN
10896 rightCol = i;
10897 }
10898
10899 if (top > bottom)
10900 {
10901 i = top;
10902 top = bottom;
10903 bottom = i;
10904 i = topRow;
10905 topRow = bottomRow;
10906 bottomRow = i;
10907 }
10908
27f35b66
SN
10909 for ( j = topRow; j <= bottomRow; j++ )
10910 {
10911 for ( i = leftCol; i <= rightCol; i++ )
10912 {
4db6714b 10913 if ((j == topRow) || (j == bottomRow) || (i == leftCol) || (i == rightCol))
27f35b66
SN
10914 {
10915 cellRect = CellToRect( j, i );
10916
10917 if (cellRect.x < left)
10918 left = cellRect.x;
10919 if (cellRect.y < top)
10920 top = cellRect.y;
10921 if (cellRect.x + cellRect.width > right)
10922 right = cellRect.x + cellRect.width;
10923 if (cellRect.y + cellRect.height > bottom)
10924 bottom = cellRect.y + cellRect.height;
10925 }
4db6714b
KH
10926 else
10927 {
10928 i = rightCol; // jump over inner cells.
10929 }
27f35b66
SN
10930 }
10931 }
10932
58dd5b3b
MB
10933 // convert to scrolled coords
10934 //
27f35b66
SN
10935 CalcScrolledPosition( left, top, &left, &top );
10936 CalcScrolledPosition( right, bottom, &right, &bottom );
58dd5b3b
MB
10937
10938 int cw, ch;
10939 m_gridWin->GetClientSize( &cw, &ch );
10940
f6bcfd97 10941 if (right < 0 || bottom < 0 || left > cw || top > ch)
c47addef 10942 return wxRect(0,0,0,0);
f6bcfd97 10943
58dd5b3b
MB
10944 rect.SetLeft( wxMax(0, left) );
10945 rect.SetTop( wxMax(0, top) );
10946 rect.SetRight( wxMin(cw, right) );
10947 rect.SetBottom( wxMin(ch, bottom) );
10948
f85afd4e
MB
10949 return rect;
10950}
10951
962a48f6
DS
10952// ----------------------------------------------------------------------------
10953// grid event classes
10954// ----------------------------------------------------------------------------
f85afd4e 10955
bf7945ce 10956IMPLEMENT_DYNAMIC_CLASS( wxGridEvent, wxNotifyEvent )
f85afd4e
MB
10957
10958wxGridEvent::wxGridEvent( int id, wxEventType type, wxObject* obj,
5c8fc7c1 10959 int row, int col, int x, int y, bool sel,
f85afd4e
MB
10960 bool control, bool shift, bool alt, bool meta )
10961 : wxNotifyEvent( type, id )
10962{
10963 m_row = row;
10964 m_col = col;
10965 m_x = x;
10966 m_y = y;
5c8fc7c1 10967 m_selecting = sel;
f85afd4e
MB
10968 m_control = control;
10969 m_shift = shift;
10970 m_alt = alt;
10971 m_meta = meta;
8f177c8e 10972
f85afd4e
MB
10973 SetEventObject(obj);
10974}
10975
10976
bf7945ce 10977IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent, wxNotifyEvent )
f85afd4e
MB
10978
10979wxGridSizeEvent::wxGridSizeEvent( int id, wxEventType type, wxObject* obj,
10980 int rowOrCol, int x, int y,
10981 bool control, bool shift, bool alt, bool meta )
10982 : wxNotifyEvent( type, id )
10983{
10984 m_rowOrCol = rowOrCol;
10985 m_x = x;
10986 m_y = y;
10987 m_control = control;
10988 m_shift = shift;
10989 m_alt = alt;
10990 m_meta = meta;
8f177c8e 10991
f85afd4e
MB
10992 SetEventObject(obj);
10993}
10994
10995
bf7945ce 10996IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent, wxNotifyEvent )
f85afd4e
MB
10997
10998wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id, wxEventType type, wxObject* obj,
8f177c8e
VZ
10999 const wxGridCellCoords& topLeft,
11000 const wxGridCellCoords& bottomRight,
5c8fc7c1
SN
11001 bool sel, bool control,
11002 bool shift, bool alt, bool meta )
8f177c8e 11003 : wxNotifyEvent( type, id )
f85afd4e 11004{
2f024384 11005 m_topLeft = topLeft;
f85afd4e 11006 m_bottomRight = bottomRight;
2f024384
DS
11007 m_selecting = sel;
11008 m_control = control;
11009 m_shift = shift;
11010 m_alt = alt;
11011 m_meta = meta;
f85afd4e
MB
11012
11013 SetEventObject(obj);
11014}
11015
11016
bf7945ce
RD
11017IMPLEMENT_DYNAMIC_CLASS(wxGridEditorCreatedEvent, wxCommandEvent)
11018
11019wxGridEditorCreatedEvent::wxGridEditorCreatedEvent(int id, wxEventType type,
11020 wxObject* obj, int row,
11021 int col, wxControl* ctrl)
11022 : wxCommandEvent(type, id)
11023{
11024 SetEventObject(obj);
11025 m_row = row;
11026 m_col = col;
11027 m_ctrl = ctrl;
11028}
11029
27b92ca4 11030#endif // wxUSE_GRID