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