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