]> git.saurik.com Git - wxWidgets.git/blame - src/generic/grid.cpp
implemented wxNotificationMessage for wxMSW using wxTaskBarIcon and fallback to gener...
[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;
f85afd4e 4518
ca65c044 4519 m_waitForSlowClick = false;
025562fe 4520
f85afd4e
MB
4521 m_rowResizeCursor = wxCursor( wxCURSOR_SIZENS );
4522 m_colResizeCursor = wxCursor( wxCURSOR_SIZEWE );
4523
4524 m_currentCellCoords = wxGridNoCellCoords;
f85afd4e 4525
b524b5c6
VZ
4526 ClearSelection();
4527
d43851f7
JS
4528 m_selectionBackground = wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT);
4529 m_selectionForeground = wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT);
8f177c8e 4530
ca65c044 4531 m_editable = true; // default for whole grid
f85afd4e 4532
ca65c044 4533 m_inOnKeyDown = false;
2d66e025 4534 m_batchCount = 0;
3c79cf49 4535
266e8367 4536 m_extraWidth =
526dbb95 4537 m_extraHeight = 0;
608754c4
JS
4538
4539 m_scrollLineX = GRID_SCROLL_LINE_X;
4540 m_scrollLineY = GRID_SCROLL_LINE_Y;
7c1cb261
VZ
4541}
4542
4543// ----------------------------------------------------------------------------
4544// the idea is to call these functions only when necessary because they create
4545// quite big arrays which eat memory mostly unnecessary - in particular, if
4546// default widths/heights are used for all rows/columns, we may not use these
4547// arrays at all
4548//
0ed3b812
VZ
4549// with some extra code, it should be possible to only store the widths/heights
4550// different from default ones (resulting in space savings for huge grids) but
4551// this is not done currently
7c1cb261
VZ
4552// ----------------------------------------------------------------------------
4553
4554void wxGrid::InitRowHeights()
4555{
4556 m_rowHeights.Empty();
4557 m_rowBottoms.Empty();
4558
4559 m_rowHeights.Alloc( m_numRows );
4560 m_rowBottoms.Alloc( m_numRows );
4561
27f35b66
SN
4562 m_rowHeights.Add( m_defaultRowHeight, m_numRows );
4563
0ed3b812 4564 int rowBottom = 0;
3d59537f 4565 for ( int i = 0; i < m_numRows; i++ )
7c1cb261 4566 {
7c1cb261
VZ
4567 rowBottom += m_defaultRowHeight;
4568 m_rowBottoms.Add( rowBottom );
4569 }
4570}
4571
4572void wxGrid::InitColWidths()
4573{
4574 m_colWidths.Empty();
4575 m_colRights.Empty();
4576
4577 m_colWidths.Alloc( m_numCols );
4578 m_colRights.Alloc( m_numCols );
27f35b66
SN
4579
4580 m_colWidths.Add( m_defaultColWidth, m_numCols );
4581
0ed3b812 4582 int colRight = 0;
3d59537f 4583 for ( int i = 0; i < m_numCols; i++ )
7c1cb261 4584 {
d4175745 4585 colRight = ( GetColPos( i ) + 1 ) * m_defaultColWidth;
7c1cb261
VZ
4586 m_colRights.Add( colRight );
4587 }
4588}
4589
4590int wxGrid::GetColWidth(int col) const
4591{
4592 return m_colWidths.IsEmpty() ? m_defaultColWidth : m_colWidths[col];
4593}
4594
4595int wxGrid::GetColLeft(int col) const
4596{
d4175745 4597 return m_colRights.IsEmpty() ? GetColPos( col ) * m_defaultColWidth
7c1cb261
VZ
4598 : m_colRights[col] - m_colWidths[col];
4599}
4600
4601int wxGrid::GetColRight(int col) const
4602{
d4175745 4603 return m_colRights.IsEmpty() ? (GetColPos( col ) + 1) * m_defaultColWidth
7c1cb261
VZ
4604 : m_colRights[col];
4605}
4606
4607int wxGrid::GetRowHeight(int row) const
4608{
4609 return m_rowHeights.IsEmpty() ? m_defaultRowHeight : m_rowHeights[row];
4610}
2d66e025 4611
7c1cb261
VZ
4612int wxGrid::GetRowTop(int row) const
4613{
4614 return m_rowBottoms.IsEmpty() ? row * m_defaultRowHeight
4615 : m_rowBottoms[row] - m_rowHeights[row];
f85afd4e
MB
4616}
4617
7c1cb261
VZ
4618int wxGrid::GetRowBottom(int row) const
4619{
4620 return m_rowBottoms.IsEmpty() ? (row + 1) * m_defaultRowHeight
4621 : m_rowBottoms[row];
4622}
f85afd4e
MB
4623
4624void wxGrid::CalcDimensions()
4625{
0ed3b812
VZ
4626 // compute the size of the scrollable area
4627 int w = m_numCols > 0 ? GetColRight(GetColAt(m_numCols - 1)) : 0;
4628 int h = m_numRows > 0 ? GetRowBottom(m_numRows - 1) : 0;
60ff3b99 4629
0ed3b812
VZ
4630 w += m_extraWidth;
4631 h += m_extraHeight;
faec5a43 4632
73145b0e 4633 // take into account editor if shown
4db6714b 4634 if ( IsCellEditControlShown() )
73145b0e 4635 {
3d59537f
DS
4636 int w2, h2;
4637 int r = m_currentCellCoords.GetRow();
4638 int c = m_currentCellCoords.GetCol();
4639 int x = GetColLeft(c);
4640 int y = GetRowTop(r);
4641
4642 // how big is the editor
4643 wxGridCellAttr* attr = GetCellAttr(r, c);
4644 wxGridCellEditor* editor = attr->GetEditor(this, r, c);
4645 editor->GetControl()->GetSize(&w2, &h2);
4646 w2 += x;
4647 h2 += y;
4648 if ( w2 > w )
4649 w = w2;
4650 if ( h2 > h )
4651 h = h2;
4652 editor->DecRef();
4653 attr->DecRef();
73145b0e
JS
4654 }
4655
faec5a43
SN
4656 // preserve (more or less) the previous position
4657 int x, y;
4658 GetViewStart( &x, &y );
97a9929e 4659
c92ed9f7 4660 // ensure the position is valid for the new scroll ranges
7b519e5e 4661 if ( x >= w )
c92ed9f7 4662 x = wxMax( w - 1, 0 );
7b519e5e 4663 if ( y >= h )
c92ed9f7 4664 y = wxMax( h - 1, 0 );
faec5a43
SN
4665
4666 // do set scrollbar parameters
675a9c0d 4667 SetScrollbars( m_scrollLineX, m_scrollLineY,
0ed3b812
VZ
4668 GetScrollX(w), GetScrollY(h),
4669 x, y,
97a9929e 4670 GetBatchCount() != 0);
12314291
VZ
4671
4672 // if our OnSize() hadn't been called (it would if we have scrollbars), we
4673 // still must reposition the children
4674 CalcWindowSizes();
f85afd4e
MB
4675}
4676
7807d81c
MB
4677void wxGrid::CalcWindowSizes()
4678{
b0a877ec
SC
4679 // escape if the window is has not been fully created yet
4680
4681 if ( m_cornerLabelWin == NULL )
2f024384 4682 return;
b0a877ec 4683
7807d81c
MB
4684 int cw, ch;
4685 GetClientSize( &cw, &ch );
b99be8fb 4686
388703a5
VZ
4687 // this block of code tries to work around the following problem: the grid
4688 // could have been just resized to have enough space to show the full grid
4689 // window contents without the scrollbars, but its client size could be
4690 // not big enough because the grid has the scrollbars right now and so the
4691 // scrollbars would remain even though we don't need them any more
4692 //
4693 // to prevent this from happening, check if we have enough space for
4694 // everything without the scrollbars and explicitly disable them then
4695 wxSize size = GetSize() - GetWindowBorderSize();
4696 if ( size != wxSize(cw, ch) )
4697 {
4698 // check if we have enough space for grid window after accounting for
4699 // the fixed size elements
4700 size.x -= m_rowLabelWidth;
4701 size.y -= m_colLabelHeight;
4702
4703 const wxSize vsize = m_gridWin->GetVirtualSize();
4704
4705 if ( size.x >= vsize.x && size.y >= vsize.y )
4706 {
4707 // yes, we do, so remove the scrollbars and use the new client size
4708 // (which should be the same as full window size - borders now)
4709 SetScrollbars(0, 0, 0, 0);
4710 GetClientSize(&cw, &ch);
4711 }
4712 }
4713
6d308072 4714 if ( m_cornerLabelWin && m_cornerLabelWin->IsShown() )
7807d81c
MB
4715 m_cornerLabelWin->SetSize( 0, 0, m_rowLabelWidth, m_colLabelHeight );
4716
c2f5b920
DS
4717 if ( m_colLabelWin && m_colLabelWin->IsShown() )
4718 m_colLabelWin->SetSize( m_rowLabelWidth, 0, cw - m_rowLabelWidth, m_colLabelHeight );
7807d81c 4719
6d308072 4720 if ( m_rowLabelWin && m_rowLabelWin->IsShown() )
c2f5b920 4721 m_rowLabelWin->SetSize( 0, m_colLabelHeight, m_rowLabelWidth, ch - m_colLabelHeight );
7807d81c 4722
6d308072 4723 if ( m_gridWin && m_gridWin->IsShown() )
c2f5b920 4724 m_gridWin->SetSize( m_rowLabelWidth, m_colLabelHeight, cw - m_rowLabelWidth, ch - m_colLabelHeight );
7807d81c
MB
4725}
4726
3d59537f
DS
4727// this is called when the grid table sends a message
4728// to indicate that it has been redimensioned
f85afd4e
MB
4729//
4730bool wxGrid::Redimension( wxGridTableMessage& msg )
4731{
4732 int i;
ca65c044 4733 bool result = false;
8f177c8e 4734
a6794685
SN
4735 // Clear the attribute cache as the attribute might refer to a different
4736 // cell than stored in the cache after adding/removing rows/columns.
4737 ClearAttrCache();
2f024384 4738
7e48d7d9
SN
4739 // By the same reasoning, the editor should be dismissed if columns are
4740 // added or removed. And for consistency, it should IMHO always be
4741 // removed, not only if the cell "underneath" it actually changes.
4742 // For now, I intentionally do not save the editor's content as the
4743 // cell it might want to save that stuff to might no longer exist.
bca7bfc8 4744 HideCellEditControl();
2f024384 4745
f6bcfd97 4746#if 0
7c1cb261
VZ
4747 // if we were using the default widths/heights so far, we must change them
4748 // now
4749 if ( m_colWidths.IsEmpty() )
4750 {
4751 InitColWidths();
4752 }
4753
4754 if ( m_rowHeights.IsEmpty() )
4755 {
4756 InitRowHeights();
4757 }
f6bcfd97 4758#endif
7c1cb261 4759
f85afd4e
MB
4760 switch ( msg.GetId() )
4761 {
4762 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED:
4763 {
4764 size_t pos = msg.GetCommandInt();
4765 int numRows = msg.GetCommandInt2();
f6bcfd97 4766
f85afd4e 4767 m_numRows += numRows;
2d66e025 4768
f6bcfd97
BP
4769 if ( !m_rowHeights.IsEmpty() )
4770 {
27f35b66
SN
4771 m_rowHeights.Insert( m_defaultRowHeight, pos, numRows );
4772 m_rowBottoms.Insert( 0, pos, numRows );
f6bcfd97
BP
4773
4774 int bottom = 0;
2f024384
DS
4775 if ( pos > 0 )
4776 bottom = m_rowBottoms[pos - 1];
60ff3b99 4777
3d59537f 4778 for ( i = pos; i < m_numRows; i++ )
f6bcfd97
BP
4779 {
4780 bottom += m_rowHeights[i];
4781 m_rowBottoms[i] = bottom;
4782 }
4783 }
2f024384 4784
f6bcfd97
BP
4785 if ( m_currentCellCoords == wxGridNoCellCoords )
4786 {
4787 // if we have just inserted cols into an empty grid the current
4788 // cell will be undefined...
4789 //
4790 SetCurrentCell( 0, 0 );
4791 }
3f3dc2ef
VZ
4792
4793 if ( m_selection )
4794 m_selection->UpdateRows( pos, numRows );
f6bcfd97
BP
4795 wxGridCellAttrProvider * attrProvider = m_table->GetAttrProvider();
4796 if (attrProvider)
4797 attrProvider->UpdateAttrRows( pos, numRows );
4798
4799 if ( !GetBatchCount() )
2d66e025 4800 {
f6bcfd97
BP
4801 CalcDimensions();
4802 m_rowLabelWin->Refresh();
2d66e025 4803 }
f85afd4e 4804 }
ca65c044 4805 result = true;
f6bcfd97 4806 break;
f85afd4e
MB
4807
4808 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED:
4809 {
4810 int numRows = msg.GetCommandInt();
2d66e025 4811 int oldNumRows = m_numRows;
f85afd4e 4812 m_numRows += numRows;
2d66e025 4813
f6bcfd97
BP
4814 if ( !m_rowHeights.IsEmpty() )
4815 {
27f35b66
SN
4816 m_rowHeights.Add( m_defaultRowHeight, numRows );
4817 m_rowBottoms.Add( 0, numRows );
60ff3b99 4818
f6bcfd97 4819 int bottom = 0;
2f024384
DS
4820 if ( oldNumRows > 0 )
4821 bottom = m_rowBottoms[oldNumRows - 1];
f6bcfd97 4822
3d59537f 4823 for ( i = oldNumRows; i < m_numRows; i++ )
f6bcfd97
BP
4824 {
4825 bottom += m_rowHeights[i];
4826 m_rowBottoms[i] = bottom;
4827 }
4828 }
2f024384 4829
f6bcfd97
BP
4830 if ( m_currentCellCoords == wxGridNoCellCoords )
4831 {
4832 // if we have just inserted cols into an empty grid the current
4833 // cell will be undefined...
4834 //
4835 SetCurrentCell( 0, 0 );
4836 }
2f024384 4837
f6bcfd97 4838 if ( !GetBatchCount() )
2d66e025 4839 {
f6bcfd97
BP
4840 CalcDimensions();
4841 m_rowLabelWin->Refresh();
2d66e025 4842 }
f85afd4e 4843 }
ca65c044 4844 result = true;
f6bcfd97 4845 break;
f85afd4e
MB
4846
4847 case wxGRIDTABLE_NOTIFY_ROWS_DELETED:
4848 {
4849 size_t pos = msg.GetCommandInt();
4850 int numRows = msg.GetCommandInt2();
f85afd4e
MB
4851 m_numRows -= numRows;
4852
f6bcfd97 4853 if ( !m_rowHeights.IsEmpty() )
f85afd4e 4854 {
27f35b66
SN
4855 m_rowHeights.RemoveAt( pos, numRows );
4856 m_rowBottoms.RemoveAt( pos, numRows );
2d66e025
MB
4857
4858 int h = 0;
3d59537f 4859 for ( i = 0; i < m_numRows; i++ )
2d66e025
MB
4860 {
4861 h += m_rowHeights[i];
4862 m_rowBottoms[i] = h;
4863 }
f85afd4e 4864 }
3d59537f 4865
f6bcfd97
BP
4866 if ( !m_numRows )
4867 {
4868 m_currentCellCoords = wxGridNoCellCoords;
4869 }
4870 else
4871 {
4872 if ( m_currentCellCoords.GetRow() >= m_numRows )
4873 m_currentCellCoords.Set( 0, 0 );
4874 }
3f3dc2ef
VZ
4875
4876 if ( m_selection )
4877 m_selection->UpdateRows( pos, -((int)numRows) );
f6bcfd97 4878 wxGridCellAttrProvider * attrProvider = m_table->GetAttrProvider();
4db6714b
KH
4879 if (attrProvider)
4880 {
f6bcfd97 4881 attrProvider->UpdateAttrRows( pos, -((int)numRows) );
3d59537f 4882
84912ef8
RD
4883// ifdef'd out following patch from Paul Gammans
4884#if 0
3ca6a5f0 4885 // No need to touch column attributes, unless we
f6bcfd97
BP
4886 // removed _all_ rows, in this case, we remove
4887 // all column attributes.
4888 // I hate to do this here, but the
4889 // needed data is not available inside UpdateAttrRows.
4890 if ( !GetNumberRows() )
4891 attrProvider->UpdateAttrCols( 0, -GetNumberCols() );
84912ef8 4892#endif
f6bcfd97 4893 }
ccdee36f 4894
f6bcfd97
BP
4895 if ( !GetBatchCount() )
4896 {
4897 CalcDimensions();
4898 m_rowLabelWin->Refresh();
4899 }
f85afd4e 4900 }
ca65c044 4901 result = true;
f6bcfd97 4902 break;
f85afd4e
MB
4903
4904 case wxGRIDTABLE_NOTIFY_COLS_INSERTED:
4905 {
4906 size_t pos = msg.GetCommandInt();
4907 int numCols = msg.GetCommandInt2();
f85afd4e 4908 m_numCols += numCols;
2d66e025 4909
d4175745
VZ
4910 if ( !m_colAt.IsEmpty() )
4911 {
4912 //Shift the column IDs
4913 int i;
4914 for ( i = 0; i < m_numCols - numCols; i++ )
4915 {
4916 if ( m_colAt[i] >= (int)pos )
4917 m_colAt[i] += numCols;
4918 }
4919
4920 m_colAt.Insert( pos, pos, numCols );
4921
4922 //Set the new columns' positions
4923 for ( i = pos + 1; i < (int)pos + numCols; i++ )
4924 {
4925 m_colAt[i] = i;
4926 }
4927 }
4928
f6bcfd97
BP
4929 if ( !m_colWidths.IsEmpty() )
4930 {
27f35b66
SN
4931 m_colWidths.Insert( m_defaultColWidth, pos, numCols );
4932 m_colRights.Insert( 0, pos, numCols );
f6bcfd97
BP
4933
4934 int right = 0;
2f024384 4935 if ( pos > 0 )
d4175745 4936 right = m_colRights[GetColAt( pos - 1 )];
60ff3b99 4937
d4175745
VZ
4938 int colPos;
4939 for ( colPos = pos; colPos < m_numCols; colPos++ )
f6bcfd97 4940 {
d4175745
VZ
4941 i = GetColAt( colPos );
4942
f6bcfd97
BP
4943 right += m_colWidths[i];
4944 m_colRights[i] = right;
4945 }
4946 }
2f024384 4947
f6bcfd97 4948 if ( m_currentCellCoords == wxGridNoCellCoords )
2d66e025 4949 {
f6bcfd97
BP
4950 // if we have just inserted cols into an empty grid the current
4951 // cell will be undefined...
4952 //
4953 SetCurrentCell( 0, 0 );
2d66e025 4954 }
3f3dc2ef
VZ
4955
4956 if ( m_selection )
4957 m_selection->UpdateCols( pos, numCols );
f6bcfd97
BP
4958 wxGridCellAttrProvider * attrProvider = m_table->GetAttrProvider();
4959 if (attrProvider)
4960 attrProvider->UpdateAttrCols( pos, numCols );
4961 if ( !GetBatchCount() )
4962 {
4963 CalcDimensions();
4964 m_colLabelWin->Refresh();
4965 }
f85afd4e 4966 }
ca65c044 4967 result = true;
f6bcfd97 4968 break;
f85afd4e
MB
4969
4970 case wxGRIDTABLE_NOTIFY_COLS_APPENDED:
4971 {
4972 int numCols = msg.GetCommandInt();
2d66e025 4973 int oldNumCols = m_numCols;
f85afd4e 4974 m_numCols += numCols;
d4175745
VZ
4975
4976 if ( !m_colAt.IsEmpty() )
4977 {
4978 m_colAt.Add( 0, numCols );
4979
4980 //Set the new columns' positions
4981 int i;
4982 for ( i = oldNumCols; i < m_numCols; i++ )
4983 {
4984 m_colAt[i] = i;
4985 }
4986 }
4987
f6bcfd97
BP
4988 if ( !m_colWidths.IsEmpty() )
4989 {
27f35b66
SN
4990 m_colWidths.Add( m_defaultColWidth, numCols );
4991 m_colRights.Add( 0, numCols );
2d66e025 4992
f6bcfd97 4993 int right = 0;
2f024384 4994 if ( oldNumCols > 0 )
d4175745 4995 right = m_colRights[GetColAt( oldNumCols - 1 )];
60ff3b99 4996
d4175745
VZ
4997 int colPos;
4998 for ( colPos = oldNumCols; colPos < m_numCols; colPos++ )
f6bcfd97 4999 {
d4175745
VZ
5000 i = GetColAt( colPos );
5001
f6bcfd97
BP
5002 right += m_colWidths[i];
5003 m_colRights[i] = right;
5004 }
5005 }
2f024384 5006
f6bcfd97 5007 if ( m_currentCellCoords == wxGridNoCellCoords )
2d66e025 5008 {
f6bcfd97
BP
5009 // if we have just inserted cols into an empty grid the current
5010 // cell will be undefined...
5011 //
5012 SetCurrentCell( 0, 0 );
5013 }
5014 if ( !GetBatchCount() )
5015 {
5016 CalcDimensions();
5017 m_colLabelWin->Refresh();
2d66e025 5018 }
f85afd4e 5019 }
ca65c044 5020 result = true;
f6bcfd97 5021 break;
f85afd4e
MB
5022
5023 case wxGRIDTABLE_NOTIFY_COLS_DELETED:
5024 {
5025 size_t pos = msg.GetCommandInt();
5026 int numCols = msg.GetCommandInt2();
f85afd4e 5027 m_numCols -= numCols;
f85afd4e 5028
d4175745
VZ
5029 if ( !m_colAt.IsEmpty() )
5030 {
5031 int colID = GetColAt( pos );
5032
5033 m_colAt.RemoveAt( pos, numCols );
5034
5035 //Shift the column IDs
5036 int colPos;
5037 for ( colPos = 0; colPos < m_numCols; colPos++ )
5038 {
5039 if ( m_colAt[colPos] > colID )
5040 m_colAt[colPos] -= numCols;
5041 }
5042 }
5043
f6bcfd97 5044 if ( !m_colWidths.IsEmpty() )
f85afd4e 5045 {
27f35b66
SN
5046 m_colWidths.RemoveAt( pos, numCols );
5047 m_colRights.RemoveAt( pos, numCols );
2d66e025
MB
5048
5049 int w = 0;
d4175745
VZ
5050 int colPos;
5051 for ( colPos = 0; colPos < m_numCols; colPos++ )
2d66e025 5052 {
d4175745
VZ
5053 i = GetColAt( colPos );
5054
2d66e025
MB
5055 w += m_colWidths[i];
5056 m_colRights[i] = w;
5057 }
f85afd4e 5058 }
2f024384 5059
f6bcfd97
BP
5060 if ( !m_numCols )
5061 {
5062 m_currentCellCoords = wxGridNoCellCoords;
5063 }
5064 else
5065 {
5066 if ( m_currentCellCoords.GetCol() >= m_numCols )
5067 m_currentCellCoords.Set( 0, 0 );
5068 }
3f3dc2ef
VZ
5069
5070 if ( m_selection )
5071 m_selection->UpdateCols( pos, -((int)numCols) );
f6bcfd97 5072 wxGridCellAttrProvider * attrProvider = m_table->GetAttrProvider();
4db6714b
KH
5073 if (attrProvider)
5074 {
f6bcfd97 5075 attrProvider->UpdateAttrCols( pos, -((int)numCols) );
ccdee36f 5076
84912ef8
RD
5077// ifdef'd out following patch from Paul Gammans
5078#if 0
f6bcfd97
BP
5079 // No need to touch row attributes, unless we
5080 // removed _all_ columns, in this case, we remove
5081 // all row attributes.
5082 // I hate to do this here, but the
5083 // needed data is not available inside UpdateAttrCols.
5084 if ( !GetNumberCols() )
5085 attrProvider->UpdateAttrRows( 0, -GetNumberRows() );
84912ef8 5086#endif
f6bcfd97 5087 }
ccdee36f 5088
f6bcfd97
BP
5089 if ( !GetBatchCount() )
5090 {
5091 CalcDimensions();
5092 m_colLabelWin->Refresh();
5093 }
f85afd4e 5094 }
ca65c044 5095 result = true;
f6bcfd97 5096 break;
f85afd4e
MB
5097 }
5098
f6bcfd97
BP
5099 if (result && !GetBatchCount() )
5100 m_gridWin->Refresh();
2f024384 5101
f6bcfd97 5102 return result;
f85afd4e
MB
5103}
5104
ef316e23 5105wxArrayInt wxGrid::CalcRowLabelsExposed( const wxRegion& reg ) const
f85afd4e 5106{
2d66e025
MB
5107 wxRegionIterator iter( reg );
5108 wxRect r;
f85afd4e 5109
275c4ae4
RD
5110 wxArrayInt rowlabels;
5111
2d66e025
MB
5112 int top, bottom;
5113 while ( iter )
f85afd4e 5114 {
2d66e025 5115 r = iter.GetRect();
f85afd4e 5116
2d66e025
MB
5117 // TODO: remove this when we can...
5118 // There is a bug in wxMotif that gives garbage update
5119 // rectangles if you jump-scroll a long way by clicking the
5120 // scrollbar with middle button. This is a work-around
5121 //
5122#if defined(__WXMOTIF__)
5123 int cw, ch;
5124 m_gridWin->GetClientSize( &cw, &ch );
56b6cf26
DS
5125 if ( r.GetTop() > ch )
5126 r.SetTop( 0 );
2d66e025
MB
5127 r.SetBottom( wxMin( r.GetBottom(), ch ) );
5128#endif
f85afd4e 5129
2d66e025
MB
5130 // logical bounds of update region
5131 //
5132 int dummy;
5133 CalcUnscrolledPosition( 0, r.GetTop(), &dummy, &top );
5134 CalcUnscrolledPosition( 0, r.GetBottom(), &dummy, &bottom );
5135
5136 // find the row labels within these bounds
5137 //
5138 int row;
3d59537f 5139 for ( row = internalYToRow(top); row < m_numRows; row++ )
2d66e025 5140 {
7c1cb261
VZ
5141 if ( GetRowBottom(row) < top )
5142 continue;
2d66e025 5143
6d55126d 5144 if ( GetRowTop(row) > bottom )
7c1cb261 5145 break;
60ff3b99 5146
d10f4bf9 5147 rowlabels.Add( row );
2d66e025 5148 }
60ff3b99 5149
60d8e886 5150 ++iter;
f85afd4e 5151 }
d10f4bf9
VZ
5152
5153 return rowlabels;
f85afd4e
MB
5154}
5155
ef316e23 5156wxArrayInt wxGrid::CalcColLabelsExposed( const wxRegion& reg ) const
f85afd4e 5157{
2d66e025
MB
5158 wxRegionIterator iter( reg );
5159 wxRect r;
f85afd4e 5160
d10f4bf9 5161 wxArrayInt colLabels;
f85afd4e 5162
2d66e025
MB
5163 int left, right;
5164 while ( iter )
f85afd4e 5165 {
2d66e025 5166 r = iter.GetRect();
f85afd4e 5167
2d66e025
MB
5168 // TODO: remove this when we can...
5169 // There is a bug in wxMotif that gives garbage update
5170 // rectangles if you jump-scroll a long way by clicking the
5171 // scrollbar with middle button. This is a work-around
5172 //
5173#if defined(__WXMOTIF__)
5174 int cw, ch;
5175 m_gridWin->GetClientSize( &cw, &ch );
56b6cf26
DS
5176 if ( r.GetLeft() > cw )
5177 r.SetLeft( 0 );
2d66e025
MB
5178 r.SetRight( wxMin( r.GetRight(), cw ) );
5179#endif
5180
5181 // logical bounds of update region
5182 //
5183 int dummy;
5184 CalcUnscrolledPosition( r.GetLeft(), 0, &left, &dummy );
5185 CalcUnscrolledPosition( r.GetRight(), 0, &right, &dummy );
5186
5187 // find the cells within these bounds
5188 //
5189 int col;
d4175745
VZ
5190 int colPos;
5191 for ( colPos = GetColPos( internalXToCol(left) ); colPos < m_numCols; colPos++ )
2d66e025 5192 {
d4175745
VZ
5193 col = GetColAt( colPos );
5194
7c1cb261
VZ
5195 if ( GetColRight(col) < left )
5196 continue;
60ff3b99 5197
7c1cb261
VZ
5198 if ( GetColLeft(col) > right )
5199 break;
2d66e025 5200
d10f4bf9 5201 colLabels.Add( col );
2d66e025 5202 }
60ff3b99 5203
60d8e886 5204 ++iter;
f85afd4e 5205 }
2f024384 5206
d10f4bf9 5207 return colLabels;
f85afd4e
MB
5208}
5209
ef316e23 5210wxGridCellCoordsArray wxGrid::CalcCellsExposed( const wxRegion& reg ) const
f85afd4e 5211{
2d66e025
MB
5212 wxRegionIterator iter( reg );
5213 wxRect r;
f85afd4e 5214
d10f4bf9 5215 wxGridCellCoordsArray cellsExposed;
f85afd4e 5216
2d66e025
MB
5217 int left, top, right, bottom;
5218 while ( iter )
5219 {
5220 r = iter.GetRect();
f85afd4e 5221
2d66e025
MB
5222 // TODO: remove this when we can...
5223 // There is a bug in wxMotif that gives garbage update
5224 // rectangles if you jump-scroll a long way by clicking the
5225 // scrollbar with middle button. This is a work-around
5226 //
5227#if defined(__WXMOTIF__)
f85afd4e 5228 int cw, ch;
2d66e025
MB
5229 m_gridWin->GetClientSize( &cw, &ch );
5230 if ( r.GetTop() > ch ) r.SetTop( 0 );
5231 if ( r.GetLeft() > cw ) r.SetLeft( 0 );
5232 r.SetRight( wxMin( r.GetRight(), cw ) );
5233 r.SetBottom( wxMin( r.GetBottom(), ch ) );
5234#endif
8f177c8e 5235
2d66e025
MB
5236 // logical bounds of update region
5237 //
5238 CalcUnscrolledPosition( r.GetLeft(), r.GetTop(), &left, &top );
5239 CalcUnscrolledPosition( r.GetRight(), r.GetBottom(), &right, &bottom );
f85afd4e 5240
2d66e025 5241 // find the cells within these bounds
f85afd4e 5242 //
2d66e025 5243 int row, col;
3d59537f 5244 for ( row = internalYToRow(top); row < m_numRows; row++ )
f85afd4e 5245 {
7c1cb261
VZ
5246 if ( GetRowBottom(row) <= top )
5247 continue;
f85afd4e 5248
7c1cb261
VZ
5249 if ( GetRowTop(row) > bottom )
5250 break;
60ff3b99 5251
d4175745
VZ
5252 int colPos;
5253 for ( colPos = GetColPos( internalXToCol(left) ); colPos < m_numCols; colPos++ )
2d66e025 5254 {
d4175745
VZ
5255 col = GetColAt( colPos );
5256
7c1cb261
VZ
5257 if ( GetColRight(col) <= left )
5258 continue;
60ff3b99 5259
7c1cb261
VZ
5260 if ( GetColLeft(col) > right )
5261 break;
60ff3b99 5262
d10f4bf9 5263 cellsExposed.Add( wxGridCellCoords( row, col ) );
2d66e025
MB
5264 }
5265 }
60ff3b99 5266
60d8e886 5267 ++iter;
f85afd4e 5268 }
d10f4bf9
VZ
5269
5270 return cellsExposed;
f85afd4e
MB
5271}
5272
5273
2d66e025 5274void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent& event )
f85afd4e 5275{
2d66e025
MB
5276 int x, y, row;
5277 wxPoint pos( event.GetPosition() );
5278 CalcUnscrolledPosition( pos.x, pos.y, &x, &y );
60ff3b99 5279
2d66e025 5280 if ( event.Dragging() )
f85afd4e 5281 {
426b2d87
JS
5282 if (!m_isDragging)
5283 {
ca65c044 5284 m_isDragging = true;
426b2d87
JS
5285 m_rowLabelWin->CaptureMouse();
5286 }
8f177c8e 5287
2d66e025 5288 if ( event.LeftIsDown() )
f85afd4e 5289 {
962a48f6 5290 switch ( m_cursorMode )
f85afd4e 5291 {
f85afd4e
MB
5292 case WXGRID_CURSOR_RESIZE_ROW:
5293 {
2d66e025
MB
5294 int cw, ch, left, dummy;
5295 m_gridWin->GetClientSize( &cw, &ch );
5296 CalcUnscrolledPosition( 0, 0, &left, &dummy );
60ff3b99 5297
2d66e025
MB
5298 wxClientDC dc( m_gridWin );
5299 PrepareDC( dc );
af547d51
VZ
5300 y = wxMax( y,
5301 GetRowTop(m_dragRowOrCol) +
5302 GetRowMinimalHeight(m_dragRowOrCol) );
d2fdd8d2 5303 dc.SetLogicalFunction(wxINVERT);
f85afd4e
MB
5304 if ( m_dragLastPos >= 0 )
5305 {
2d66e025 5306 dc.DrawLine( left, m_dragLastPos, left+cw, m_dragLastPos );
f85afd4e 5307 }
2d66e025
MB
5308 dc.DrawLine( left, y, left+cw, y );
5309 m_dragLastPos = y;
f85afd4e
MB
5310 }
5311 break;
5312
5313 case WXGRID_CURSOR_SELECT_ROW:
902725ee 5314 {
e32352cf 5315 if ( (row = YToRow( y )) >= 0 )
aa5e1f75 5316 {
3f3dc2ef
VZ
5317 if ( m_selection )
5318 {
5319 m_selection->SelectRow( row,
5320 event.ControlDown(),
5321 event.ShiftDown(),
5322 event.AltDown(),
5323 event.MetaDown() );
5324 }
f85afd4e 5325 }
902725ee
WS
5326 }
5327 break;
e2b42eeb
VZ
5328
5329 // default label to suppress warnings about "enumeration value
5330 // 'xxx' not handled in switch
5331 default:
5332 break;
f85afd4e
MB
5333 }
5334 }
5335 return;
5336 }
5337
426b2d87
JS
5338 if ( m_isDragging && (event.Entering() || event.Leaving()) )
5339 return;
8f177c8e 5340
426b2d87
JS
5341 if (m_isDragging)
5342 {
ccdee36f
DS
5343 if (m_rowLabelWin->HasCapture())
5344 m_rowLabelWin->ReleaseMouse();
ca65c044 5345 m_isDragging = false;
426b2d87 5346 }
60ff3b99 5347
6d004f67
MB
5348 // ------------ Entering or leaving the window
5349 //
5350 if ( event.Entering() || event.Leaving() )
5351 {
e2b42eeb 5352 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_rowLabelWin);
6d004f67
MB
5353 }
5354
2d66e025 5355 // ------------ Left button pressed
f85afd4e 5356 //
6d004f67 5357 else if ( event.LeftDown() )
f85afd4e 5358 {
2d66e025
MB
5359 // don't send a label click event for a hit on the
5360 // edge of the row label - this is probably the user
5361 // wanting to resize the row
5362 //
5363 if ( YToEdgeOfRow(y) < 0 )
f85afd4e 5364 {
2d66e025 5365 row = YToRow(y);
2f024384 5366 if ( row >= 0 &&
b54ba671 5367 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK, row, -1, event ) )
f85afd4e 5368 {
2b01fd49 5369 if ( !event.ShiftDown() && !event.CmdDown() )
aa5e1f75 5370 ClearSelection();
64e15340 5371 if ( m_selection )
3f3dc2ef
VZ
5372 {
5373 if ( event.ShiftDown() )
5374 {
5375 m_selection->SelectBlock( m_currentCellCoords.GetRow(),
5376 0,
5377 row,
5378 GetNumberCols() - 1,
5379 event.ControlDown(),
5380 event.ShiftDown(),
5381 event.AltDown(),
5382 event.MetaDown() );
5383 }
5384 else
5385 {
5386 m_selection->SelectRow( row,
5387 event.ControlDown(),
5388 event.ShiftDown(),
5389 event.AltDown(),
5390 event.MetaDown() );
5391 }
5392 }
5393
e2b42eeb 5394 ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW, m_rowLabelWin);
f85afd4e 5395 }
2d66e025
MB
5396 }
5397 else
5398 {
5399 // starting to drag-resize a row
6e8524b1
MB
5400 if ( CanDragRowSize() )
5401 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW, m_rowLabelWin);
2d66e025
MB
5402 }
5403 }
f85afd4e 5404
2d66e025
MB
5405 // ------------ Left double click
5406 //
5407 else if (event.LeftDClick() )
5408 {
4e115ed2 5409 row = YToEdgeOfRow(y);
d43851f7 5410 if ( row < 0 )
2d66e025
MB
5411 {
5412 row = YToRow(y);
a967f048 5413 if ( row >=0 &&
ca65c044
WS
5414 !SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK, row, -1, event ) )
5415 {
a967f048 5416 // no default action at the moment
ca65c044 5417 }
f85afd4e 5418 }
d43851f7
JS
5419 else
5420 {
5421 // adjust row height depending on label text
5422 AutoSizeRowLabelSize( row );
5423
5424 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_colLabelWin);
ccdee36f 5425 m_dragLastPos = -1;
d43851f7 5426 }
f85afd4e 5427 }
60ff3b99 5428
2d66e025 5429 // ------------ Left button released
f85afd4e 5430 //
2d66e025 5431 else if ( event.LeftUp() )
f85afd4e 5432 {
2d66e025 5433 if ( m_cursorMode == WXGRID_CURSOR_RESIZE_ROW )
f85afd4e 5434 {
6d004f67 5435 DoEndDragResizeRow();
60ff3b99 5436
6d004f67
MB
5437 // Note: we are ending the event *after* doing
5438 // default processing in this case
5439 //
b54ba671 5440 SendEvent( wxEVT_GRID_ROW_SIZE, m_dragRowOrCol, -1, event );
2d66e025 5441 }
f85afd4e 5442
e2b42eeb
VZ
5443 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_rowLabelWin);
5444 m_dragLastPos = -1;
2d66e025 5445 }
f85afd4e 5446
2d66e025
MB
5447 // ------------ Right button down
5448 //
5449 else if ( event.RightDown() )
5450 {
5451 row = YToRow(y);
ef5df12b 5452 if ( row >=0 &&
ca65c044 5453 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK, row, -1, event ) )
2d66e025
MB
5454 {
5455 // no default action at the moment
f85afd4e
MB
5456 }
5457 }
60ff3b99 5458
2d66e025 5459 // ------------ Right double click
f85afd4e 5460 //
2d66e025
MB
5461 else if ( event.RightDClick() )
5462 {
5463 row = YToRow(y);
a967f048 5464 if ( row >= 0 &&
ca65c044 5465 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK, row, -1, event ) )
2d66e025
MB
5466 {
5467 // no default action at the moment
5468 }
5469 }
60ff3b99 5470
2d66e025 5471 // ------------ No buttons down and mouse moving
f85afd4e 5472 //
2d66e025 5473 else if ( event.Moving() )
f85afd4e 5474 {
2d66e025
MB
5475 m_dragRowOrCol = YToEdgeOfRow( y );
5476 if ( m_dragRowOrCol >= 0 )
8f177c8e 5477 {
2d66e025 5478 if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
8f177c8e 5479 {
e2b42eeb 5480 // don't capture the mouse yet
6e8524b1 5481 if ( CanDragRowSize() )
ca65c044 5482 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW, m_rowLabelWin, false);
8f177c8e 5483 }
2d66e025 5484 }
6d004f67 5485 else if ( m_cursorMode != WXGRID_CURSOR_SELECT_CELL )
2d66e025 5486 {
ca65c044 5487 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_rowLabelWin, false);
8f177c8e 5488 }
f85afd4e 5489 }
2d66e025
MB
5490}
5491
2d66e025
MB
5492void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent& event )
5493{
5494 int x, y, col;
5495 wxPoint pos( event.GetPosition() );
5496 CalcUnscrolledPosition( pos.x, pos.y, &x, &y );
60ff3b99 5497
2d66e025 5498 if ( event.Dragging() )
f85afd4e 5499 {
fe77cf60
JS
5500 if (!m_isDragging)
5501 {
ca65c044 5502 m_isDragging = true;
fe77cf60 5503 m_colLabelWin->CaptureMouse();
d4175745
VZ
5504
5505 if ( m_cursorMode == WXGRID_CURSOR_MOVE_COL )
5506 m_dragRowOrCol = XToCol( x );
fe77cf60 5507 }
8f177c8e 5508
2d66e025 5509 if ( event.LeftIsDown() )
8f177c8e 5510 {
962a48f6 5511 switch ( m_cursorMode )
8f177c8e 5512 {
2d66e025 5513 case WXGRID_CURSOR_RESIZE_COL:
8f177c8e 5514 {
2d66e025
MB
5515 int cw, ch, dummy, top;
5516 m_gridWin->GetClientSize( &cw, &ch );
5517 CalcUnscrolledPosition( 0, 0, &dummy, &top );
60ff3b99 5518
2d66e025
MB
5519 wxClientDC dc( m_gridWin );
5520 PrepareDC( dc );
43947979
VZ
5521
5522 x = wxMax( x, GetColLeft(m_dragRowOrCol) +
5523 GetColMinimalWidth(m_dragRowOrCol));
d2fdd8d2 5524 dc.SetLogicalFunction(wxINVERT);
2d66e025
MB
5525 if ( m_dragLastPos >= 0 )
5526 {
ccdee36f 5527 dc.DrawLine( m_dragLastPos, top, m_dragLastPos, top + ch );
2d66e025 5528 }
ccdee36f 5529 dc.DrawLine( x, top, x, top + ch );
2d66e025 5530 m_dragLastPos = x;
f85afd4e 5531 }
2d66e025 5532 break;
f85afd4e 5533
2d66e025 5534 case WXGRID_CURSOR_SELECT_COL:
902725ee 5535 {
e32352cf 5536 if ( (col = XToCol( x )) >= 0 )
aa5e1f75 5537 {
3f3dc2ef
VZ
5538 if ( m_selection )
5539 {
5540 m_selection->SelectCol( col,
5541 event.ControlDown(),
5542 event.ShiftDown(),
5543 event.AltDown(),
5544 event.MetaDown() );
5545 }
2d66e025 5546 }
902725ee
WS
5547 }
5548 break;
e2b42eeb 5549
d4175745
VZ
5550 case WXGRID_CURSOR_MOVE_COL:
5551 {
5552 if ( x < 0 )
5553 m_moveToCol = GetColAt( 0 );
5554 else
5555 m_moveToCol = XToCol( x );
5556
5557 int markerX;
5558
5559 if ( m_moveToCol < 0 )
5560 markerX = GetColRight( GetColAt( m_numCols - 1 ) );
5561 else
5562 markerX = GetColLeft( m_moveToCol );
5563
5564 if ( markerX != m_dragLastPos )
5565 {
5566 wxClientDC dc( m_colLabelWin );
5567
5568 int cw, ch;
5569 m_colLabelWin->GetClientSize( &cw, &ch );
5570
5571 markerX++;
5572
5573 //Clean up the last indicator
5574 if ( m_dragLastPos >= 0 )
5575 {
5576 wxPen pen( m_colLabelWin->GetBackgroundColour(), 2 );
5577 dc.SetPen(pen);
5578 dc.DrawLine( m_dragLastPos + 1, 0, m_dragLastPos + 1, ch );
5579 dc.SetPen(wxNullPen);
5580
5581 if ( XToCol( m_dragLastPos ) != -1 )
5582 DrawColLabel( dc, XToCol( m_dragLastPos ) );
5583 }
5584
5585 //Moving to the same place? Don't draw a marker
5586 if ( (m_moveToCol == m_dragRowOrCol)
5587 || (GetColPos( m_moveToCol ) == GetColPos( m_dragRowOrCol ) + 1)
5588 || (m_moveToCol < 0 && m_dragRowOrCol == GetColAt( m_numCols - 1 )))
5589 {
5590 m_dragLastPos = -1;
5591 return;
5592 }
5593
5594 //Draw the marker
5595 wxPen pen( *wxBLUE, 2 );
5596 dc.SetPen(pen);
5597
5598 dc.DrawLine( markerX, 0, markerX, ch );
5599
5600 dc.SetPen(wxNullPen);
5601
5602 m_dragLastPos = markerX - 1;
5603 }
5604 }
5605 break;
5606
e2b42eeb
VZ
5607 // default label to suppress warnings about "enumeration value
5608 // 'xxx' not handled in switch
5609 default:
5610 break;
2d66e025 5611 }
f85afd4e 5612 }
2d66e025 5613 return;
f85afd4e 5614 }
2d66e025 5615
fe77cf60
JS
5616 if ( m_isDragging && (event.Entering() || event.Leaving()) )
5617 return;
2d66e025 5618
fe77cf60
JS
5619 if (m_isDragging)
5620 {
ccdee36f
DS
5621 if (m_colLabelWin->HasCapture())
5622 m_colLabelWin->ReleaseMouse();
ca65c044 5623 m_isDragging = false;
fe77cf60 5624 }
60ff3b99 5625
6d004f67
MB
5626 // ------------ Entering or leaving the window
5627 //
5628 if ( event.Entering() || event.Leaving() )
5629 {
e2b42eeb 5630 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_colLabelWin);
6d004f67
MB
5631 }
5632
2d66e025 5633 // ------------ Left button pressed
f85afd4e 5634 //
6d004f67 5635 else if ( event.LeftDown() )
f85afd4e 5636 {
2d66e025
MB
5637 // don't send a label click event for a hit on the
5638 // edge of the col label - this is probably the user
5639 // wanting to resize the col
5640 //
5641 if ( XToEdgeOfCol(x) < 0 )
8f177c8e 5642 {
2d66e025 5643 col = XToCol(x);
2f024384 5644 if ( col >= 0 &&
b54ba671 5645 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK, -1, col, event ) )
8f177c8e 5646 {
d4175745 5647 if ( m_canDragColMove )
3f3dc2ef 5648 {
d4175745
VZ
5649 //Show button as pressed
5650 wxClientDC dc( m_colLabelWin );
5651 int colLeft = GetColLeft( col );
5652 int colRight = GetColRight( col ) - 1;
5653 dc.SetPen( wxPen( m_colLabelWin->GetBackgroundColour(), 1 ) );
5654 dc.DrawLine( colLeft, 1, colLeft, m_colLabelHeight-1 );
5655 dc.DrawLine( colLeft, 1, colRight, 1 );
5656
5657 ChangeCursorMode(WXGRID_CURSOR_MOVE_COL, m_colLabelWin);
5658 }
5659 else
5660 {
5661 if ( !event.ShiftDown() && !event.CmdDown() )
5662 ClearSelection();
5663 if ( m_selection )
3f3dc2ef 5664 {
d4175745
VZ
5665 if ( event.ShiftDown() )
5666 {
5667 m_selection->SelectBlock( 0,
5668 m_currentCellCoords.GetCol(),
5669 GetNumberRows() - 1, col,
5670 event.ControlDown(),
5671 event.ShiftDown(),
5672 event.AltDown(),
5673 event.MetaDown() );
5674 }
5675 else
5676 {
5677 m_selection->SelectCol( col,
5678 event.ControlDown(),
5679 event.ShiftDown(),
5680 event.AltDown(),
5681 event.MetaDown() );
5682 }
3f3dc2ef 5683 }
3f3dc2ef 5684
d4175745
VZ
5685 ChangeCursorMode(WXGRID_CURSOR_SELECT_COL, m_colLabelWin);
5686 }
f85afd4e 5687 }
2d66e025
MB
5688 }
5689 else
5690 {
5691 // starting to drag-resize a col
5692 //
6e8524b1
MB
5693 if ( CanDragColSize() )
5694 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL, m_colLabelWin);
2d66e025
MB
5695 }
5696 }
f85afd4e 5697
2d66e025
MB
5698 // ------------ Left double click
5699 //
5700 if ( event.LeftDClick() )
5701 {
4e115ed2 5702 col = XToEdgeOfCol(x);
d43851f7 5703 if ( col < 0 )
2d66e025
MB
5704 {
5705 col = XToCol(x);
a967f048
RG
5706 if ( col >= 0 &&
5707 ! SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK, -1, col, event ) )
5708 {
ca65c044 5709 // no default action at the moment
a967f048 5710 }
2d66e025 5711 }
d43851f7
JS
5712 else
5713 {
5714 // adjust column width depending on label text
5715 AutoSizeColLabelSize( col );
5716
5717 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_colLabelWin);
ccdee36f 5718 m_dragLastPos = -1;
d43851f7 5719 }
2d66e025 5720 }
60ff3b99 5721
2d66e025
MB
5722 // ------------ Left button released
5723 //
5724 else if ( event.LeftUp() )
5725 {
d4175745 5726 switch ( m_cursorMode )
2d66e025 5727 {
d4175745 5728 case WXGRID_CURSOR_RESIZE_COL:
d4175745 5729 DoEndDragResizeCol();
e2b42eeb 5730
d4175745
VZ
5731 // Note: we are ending the event *after* doing
5732 // default processing in this case
5733 //
5734 SendEvent( wxEVT_GRID_COL_SIZE, -1, m_dragRowOrCol, event );
852b6c3c 5735 break;
d4175745
VZ
5736
5737 case WXGRID_CURSOR_MOVE_COL:
d4175745
VZ
5738 DoEndDragMoveCol();
5739
5740 SendEvent( wxEVT_GRID_COL_MOVE, -1, m_dragRowOrCol, event );
852b6c3c
VZ
5741 break;
5742
5743 case WXGRID_CURSOR_SELECT_COL:
5744 case WXGRID_CURSOR_SELECT_CELL:
5745 case WXGRID_CURSOR_RESIZE_ROW:
5746 case WXGRID_CURSOR_SELECT_ROW:
5747 // nothing to do (?)
5748 break;
2d66e025 5749 }
f85afd4e 5750
e2b42eeb 5751 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_colLabelWin);
ccdee36f 5752 m_dragLastPos = -1;
60ff3b99
VZ
5753 }
5754
2d66e025
MB
5755 // ------------ Right button down
5756 //
5757 else if ( event.RightDown() )
5758 {
5759 col = XToCol(x);
a967f048 5760 if ( col >= 0 &&
ca65c044 5761 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK, -1, col, event ) )
2d66e025
MB
5762 {
5763 // no default action at the moment
f85afd4e
MB
5764 }
5765 }
60ff3b99 5766
2d66e025 5767 // ------------ Right double click
f85afd4e 5768 //
2d66e025
MB
5769 else if ( event.RightDClick() )
5770 {
5771 col = XToCol(x);
a967f048 5772 if ( col >= 0 &&
ca65c044 5773 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK, -1, col, event ) )
2d66e025
MB
5774 {
5775 // no default action at the moment
5776 }
5777 }
60ff3b99 5778
2d66e025 5779 // ------------ No buttons down and mouse moving
f85afd4e 5780 //
2d66e025 5781 else if ( event.Moving() )
f85afd4e 5782 {
2d66e025
MB
5783 m_dragRowOrCol = XToEdgeOfCol( x );
5784 if ( m_dragRowOrCol >= 0 )
f85afd4e 5785 {
2d66e025 5786 if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
f85afd4e 5787 {
e2b42eeb 5788 // don't capture the cursor yet
6e8524b1 5789 if ( CanDragColSize() )
ca65c044 5790 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL, m_colLabelWin, false);
f85afd4e 5791 }
2d66e025 5792 }
6d004f67 5793 else if ( m_cursorMode != WXGRID_CURSOR_SELECT_CELL )
2d66e025 5794 {
ca65c044 5795 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_colLabelWin, false);
8f177c8e 5796 }
f85afd4e
MB
5797 }
5798}
5799
2d66e025 5800void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent& event )
f85afd4e 5801{
2d66e025 5802 if ( event.LeftDown() )
f85afd4e 5803 {
2d66e025
MB
5804 // indicate corner label by having both row and
5805 // col args == -1
f85afd4e 5806 //
b54ba671 5807 if ( !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK, -1, -1, event ) )
2d66e025
MB
5808 {
5809 SelectAll();
5810 }
f85afd4e 5811 }
2d66e025
MB
5812 else if ( event.LeftDClick() )
5813 {
b54ba671 5814 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK, -1, -1, event );
2d66e025 5815 }
2d66e025 5816 else if ( event.RightDown() )
f85afd4e 5817 {
b54ba671 5818 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK, -1, -1, event ) )
f85afd4e 5819 {
2d66e025
MB
5820 // no default action at the moment
5821 }
5822 }
2d66e025
MB
5823 else if ( event.RightDClick() )
5824 {
b54ba671 5825 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK, -1, -1, event ) )
2d66e025
MB
5826 {
5827 // no default action at the moment
5828 }
5829 }
5830}
f85afd4e 5831
86033c4b
VZ
5832void wxGrid::CancelMouseCapture()
5833{
5834 // cancel operation currently in progress, whatever it is
5835 if ( m_winCapture )
5836 {
5837 m_isDragging = false;
5838 m_cursorMode = WXGRID_CURSOR_SELECT_CELL;
5839 m_winCapture->SetCursor( *wxSTANDARD_CURSOR );
5840 m_winCapture = NULL;
5841
5842 // remove traces of whatever we drew on screen
5843 Refresh();
5844 }
5845}
5846
e2b42eeb
VZ
5847void wxGrid::ChangeCursorMode(CursorMode mode,
5848 wxWindow *win,
5849 bool captureMouse)
5850{
5851#ifdef __WXDEBUG__
5852 static const wxChar *cursorModes[] =
5853 {
5854 _T("SELECT_CELL"),
5855 _T("RESIZE_ROW"),
5856 _T("RESIZE_COL"),
5857 _T("SELECT_ROW"),
d4175745
VZ
5858 _T("SELECT_COL"),
5859 _T("MOVE_COL"),
e2b42eeb
VZ
5860 };
5861
181bfffd
VZ
5862 wxLogTrace(_T("grid"),
5863 _T("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
e2b42eeb
VZ
5864 win == m_colLabelWin ? _T("colLabelWin")
5865 : win ? _T("rowLabelWin")
5866 : _T("gridWin"),
5867 cursorModes[m_cursorMode], cursorModes[mode]);
2f024384 5868#endif
e2b42eeb 5869
faec5a43
SN
5870 if ( mode == m_cursorMode &&
5871 win == m_winCapture &&
5872 captureMouse == (m_winCapture != NULL))
e2b42eeb
VZ
5873 return;
5874
5875 if ( !win )
5876 {
5877 // by default use the grid itself
5878 win = m_gridWin;
5879 }
5880
5881 if ( m_winCapture )
5882 {
2f024384
DS
5883 if (m_winCapture->HasCapture())
5884 m_winCapture->ReleaseMouse();
e2b42eeb
VZ
5885 m_winCapture = (wxWindow *)NULL;
5886 }
5887
5888 m_cursorMode = mode;
5889
5890 switch ( m_cursorMode )
5891 {
5892 case WXGRID_CURSOR_RESIZE_ROW:
5893 win->SetCursor( m_rowResizeCursor );
5894 break;
5895
5896 case WXGRID_CURSOR_RESIZE_COL:
5897 win->SetCursor( m_colResizeCursor );
5898 break;
5899
d4175745
VZ
5900 case WXGRID_CURSOR_MOVE_COL:
5901 win->SetCursor( wxCursor(wxCURSOR_HAND) );
5902 break;
5903
e2b42eeb
VZ
5904 default:
5905 win->SetCursor( *wxSTANDARD_CURSOR );
2f024384 5906 break;
e2b42eeb
VZ
5907 }
5908
5909 // we need to capture mouse when resizing
5910 bool resize = m_cursorMode == WXGRID_CURSOR_RESIZE_ROW ||
5911 m_cursorMode == WXGRID_CURSOR_RESIZE_COL;
5912
5913 if ( captureMouse && resize )
5914 {
5915 win->CaptureMouse();
5916 m_winCapture = win;
5917 }
5918}
8f177c8e 5919
2d66e025
MB
5920void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent& event )
5921{
5922 int x, y;
5923 wxPoint pos( event.GetPosition() );
5924 CalcUnscrolledPosition( pos.x, pos.y, &x, &y );
60ff3b99 5925
2d66e025
MB
5926 wxGridCellCoords coords;
5927 XYToCell( x, y, coords );
60ff3b99 5928
27f35b66 5929 int cell_rows, cell_cols;
79dbea21 5930 bool isFirstDrag = !m_isDragging;
27f35b66
SN
5931 GetCellSize( coords.GetRow(), coords.GetCol(), &cell_rows, &cell_cols );
5932 if ((cell_rows < 0) || (cell_cols < 0))
5933 {
5934 coords.SetRow(coords.GetRow() + cell_rows);
5935 coords.SetCol(coords.GetCol() + cell_cols);
5936 }
5937
2d66e025
MB
5938 if ( event.Dragging() )
5939 {
07296f0b
RD
5940 //wxLogDebug("pos(%d, %d) coords(%d, %d)", pos.x, pos.y, coords.GetRow(), coords.GetCol());
5941
962a48f6 5942 // Don't start doing anything until the mouse has been dragged at
07296f0b 5943 // least 3 pixels in any direction...
508011ce
VZ
5944 if (! m_isDragging)
5945 {
5946 if (m_startDragPos == wxDefaultPosition)
5947 {
07296f0b
RD
5948 m_startDragPos = pos;
5949 return;
5950 }
5951 if (abs(m_startDragPos.x - pos.x) < 4 && abs(m_startDragPos.y - pos.y) < 4)
5952 return;
5953 }
5954
ca65c044 5955 m_isDragging = true;
2d66e025
MB
5956 if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
5957 {
f0102d2a 5958 // Hide the edit control, so it
4db6714b 5959 // won't interfere with drag-shrinking.
f6bcfd97 5960 if ( IsCellEditControlShown() )
a5777624 5961 {
f0102d2a 5962 HideCellEditControl();
a5777624
RD
5963 SaveEditControlValue();
5964 }
07296f0b 5965
2d66e025
MB
5966 if ( coords != wxGridNoCellCoords )
5967 {
2b01fd49 5968 if ( event.CmdDown() )
aa5e1f75
SN
5969 {
5970 if ( m_selectingKeyboard == wxGridNoCellCoords)
5971 m_selectingKeyboard = coords;
a9339fe2 5972 HighlightBlock( m_selectingKeyboard, coords );
aa5e1f75 5973 }
79dbea21
RD
5974 else if ( CanDragCell() )
5975 {
5976 if ( isFirstDrag )
5977 {
5978 if ( m_selectingKeyboard == wxGridNoCellCoords)
5979 m_selectingKeyboard = coords;
5980
5981 SendEvent( wxEVT_GRID_CELL_BEGIN_DRAG,
5982 coords.GetRow(),
5983 coords.GetCol(),
5984 event );
25107357 5985 return;
79dbea21
RD
5986 }
5987 }
aa5e1f75
SN
5988 else
5989 {
5990 if ( !IsSelection() )
5991 {
c9097836 5992 HighlightBlock( coords, coords );
aa5e1f75
SN
5993 }
5994 else
5995 {
c9097836 5996 HighlightBlock( m_currentCellCoords, coords );
aa5e1f75 5997 }
f85afd4e 5998 }
07296f0b 5999
508011ce
VZ
6000 if (! IsVisible(coords))
6001 {
07296f0b
RD
6002 MakeCellVisible(coords);
6003 // TODO: need to introduce a delay or something here. The
e32352cf 6004 // scrolling is way to fast, at least on MSW - also on GTK.
07296f0b 6005 }
2d66e025 6006 }
25107357
RD
6007 // Have we captured the mouse yet?
6008 if (! m_winCapture)
6009 {
6010 m_winCapture = m_gridWin;
6011 m_winCapture->CaptureMouse();
6012 }
6013
c71b2126 6014
2d66e025 6015 }
6d004f67
MB
6016 else if ( m_cursorMode == WXGRID_CURSOR_RESIZE_ROW )
6017 {
6018 int cw, ch, left, dummy;
6019 m_gridWin->GetClientSize( &cw, &ch );
6020 CalcUnscrolledPosition( 0, 0, &left, &dummy );
8f177c8e 6021
6d004f67
MB
6022 wxClientDC dc( m_gridWin );
6023 PrepareDC( dc );
a95e38c0
VZ
6024 y = wxMax( y, GetRowTop(m_dragRowOrCol) +
6025 GetRowMinimalHeight(m_dragRowOrCol) );
6d004f67
MB
6026 dc.SetLogicalFunction(wxINVERT);
6027 if ( m_dragLastPos >= 0 )
6028 {
6029 dc.DrawLine( left, m_dragLastPos, left+cw, m_dragLastPos );
6030 }
6031 dc.DrawLine( left, y, left+cw, y );
6032 m_dragLastPos = y;
6033 }
6034 else if ( m_cursorMode == WXGRID_CURSOR_RESIZE_COL )
6035 {
6036 int cw, ch, dummy, top;
6037 m_gridWin->GetClientSize( &cw, &ch );
6038 CalcUnscrolledPosition( 0, 0, &dummy, &top );
e2b42eeb 6039
6d004f67
MB
6040 wxClientDC dc( m_gridWin );
6041 PrepareDC( dc );
43947979
VZ
6042 x = wxMax( x, GetColLeft(m_dragRowOrCol) +
6043 GetColMinimalWidth(m_dragRowOrCol) );
6d004f67
MB
6044 dc.SetLogicalFunction(wxINVERT);
6045 if ( m_dragLastPos >= 0 )
6046 {
a9339fe2 6047 dc.DrawLine( m_dragLastPos, top, m_dragLastPos, top + ch );
6d004f67 6048 }
a9339fe2 6049 dc.DrawLine( x, top, x, top + ch );
6d004f67
MB
6050 m_dragLastPos = x;
6051 }
e2b42eeb 6052
2d66e025
MB
6053 return;
6054 }
66242c80 6055
ca65c044 6056 m_isDragging = false;
07296f0b
RD
6057 m_startDragPos = wxDefaultPosition;
6058
a5777624
RD
6059 // VZ: if we do this, the mode is reset to WXGRID_CURSOR_SELECT_CELL
6060 // immediately after it becomes WXGRID_CURSOR_RESIZE_ROW/COL under
6061 // wxGTK
e2b42eeb 6062#if 0
a5777624
RD
6063 if ( event.Entering() || event.Leaving() )
6064 {
6065 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL);
6066 m_gridWin->SetCursor( *wxSTANDARD_CURSOR );
6067 }
6068 else
e2b42eeb
VZ
6069#endif // 0
6070
a5777624
RD
6071 // ------------ Left button pressed
6072 //
6073 if ( event.LeftDown() && coords != wxGridNoCellCoords )
f6bcfd97
BP
6074 {
6075 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_CLICK,
6076 coords.GetRow(),
6077 coords.GetCol(),
6078 event ) )
a5777624 6079 {
2b01fd49 6080 if ( !event.CmdDown() )
f6bcfd97
BP
6081 ClearSelection();
6082 if ( event.ShiftDown() )
6083 {
3f3dc2ef
VZ
6084 if ( m_selection )
6085 {
6086 m_selection->SelectBlock( m_currentCellCoords.GetRow(),
6087 m_currentCellCoords.GetCol(),
6088 coords.GetRow(),
6089 coords.GetCol(),
6090 event.ControlDown(),
6091 event.ShiftDown(),
6092 event.AltDown(),
6093 event.MetaDown() );
6094 }
f6bcfd97 6095 }
2f024384 6096 else if ( XToEdgeOfCol(x) < 0 &&
f6bcfd97 6097 YToEdgeOfRow(y) < 0 )
58dd5b3b 6098 {
a5777624
RD
6099 DisableCellEditControl();
6100 MakeCellVisible( coords );
6101
2b01fd49 6102 if ( event.CmdDown() )
58dd5b3b 6103 {
73145b0e
JS
6104 if ( m_selection )
6105 {
6106 m_selection->ToggleCellSelection( coords.GetRow(),
6107 coords.GetCol(),
6108 event.ControlDown(),
6109 event.ShiftDown(),
6110 event.AltDown(),
6111 event.MetaDown() );
6112 }
6113 m_selectingTopLeft = wxGridNoCellCoords;
6114 m_selectingBottomRight = wxGridNoCellCoords;
6115 m_selectingKeyboard = coords;
a5777624
RD
6116 }
6117 else
6118 {
73145b0e
JS
6119 m_waitForSlowClick = m_currentCellCoords == coords && coords != wxGridNoCellCoords;
6120 SetCurrentCell( coords );
6121 if ( m_selection )
aa5e1f75 6122 {
73145b0e
JS
6123 if ( m_selection->GetSelectionMode() !=
6124 wxGrid::wxGridSelectCells )
3f3dc2ef 6125 {
73145b0e 6126 HighlightBlock( coords, coords );
3f3dc2ef 6127 }
aa5e1f75 6128 }
58dd5b3b
MB
6129 }
6130 }
f85afd4e 6131 }
a5777624 6132 }
f85afd4e 6133
a5777624
RD
6134 // ------------ Left double click
6135 //
6136 else if ( event.LeftDClick() && coords != wxGridNoCellCoords )
6137 {
6138 DisableCellEditControl();
6139
2f024384 6140 if ( XToEdgeOfCol(x) < 0 && YToEdgeOfRow(y) < 0 )
58dd5b3b 6141 {
1ef49ab5
VS
6142 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_DCLICK,
6143 coords.GetRow(),
6144 coords.GetCol(),
6145 event ) )
6146 {
6147 // we want double click to select a cell and start editing
6148 // (i.e. to behave in same way as sequence of two slow clicks):
6149 m_waitForSlowClick = true;
6150 }
58dd5b3b 6151 }
a5777624 6152 }
f85afd4e 6153
a5777624
RD
6154 // ------------ Left button released
6155 //
6156 else if ( event.LeftUp() )
6157 {
6158 if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
f85afd4e 6159 {
f40f9976
JS
6160 if (m_winCapture)
6161 {
2f024384
DS
6162 if (m_winCapture->HasCapture())
6163 m_winCapture->ReleaseMouse();
f40f9976
JS
6164 m_winCapture = NULL;
6165 }
6166
bee19958 6167 if ( coords == m_currentCellCoords && m_waitForSlowClick && CanEnableCellControl() )
932b55d0
JS
6168 {
6169 ClearSelection();
6170 EnableCellEditControl();
6171
2f024384 6172 wxGridCellAttr *attr = GetCellAttr(coords);
932b55d0
JS
6173 wxGridCellEditor *editor = attr->GetEditor(this, coords.GetRow(), coords.GetCol());
6174 editor->StartingClick();
6175 editor->DecRef();
6176 attr->DecRef();
6177
ca65c044 6178 m_waitForSlowClick = false;
932b55d0
JS
6179 }
6180 else if ( m_selectingTopLeft != wxGridNoCellCoords &&
b5808881 6181 m_selectingBottomRight != wxGridNoCellCoords )
52068ea5 6182 {
3f3dc2ef
VZ
6183 if ( m_selection )
6184 {
6185 m_selection->SelectBlock( m_selectingTopLeft.GetRow(),
6186 m_selectingTopLeft.GetCol(),
6187 m_selectingBottomRight.GetRow(),
6188 m_selectingBottomRight.GetCol(),
6189 event.ControlDown(),
6190 event.ShiftDown(),
6191 event.AltDown(),
6192 event.MetaDown() );
6193 }
6194
5c8fc7c1
SN
6195 m_selectingTopLeft = wxGridNoCellCoords;
6196 m_selectingBottomRight = wxGridNoCellCoords;
2d66e025 6197
73145b0e
JS
6198 // Show the edit control, if it has been hidden for
6199 // drag-shrinking.
6200 ShowCellEditControl();
6201 }
a5777624
RD
6202 }
6203 else if ( m_cursorMode == WXGRID_CURSOR_RESIZE_ROW )
6204 {
6205 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL);
6206 DoEndDragResizeRow();
e2b42eeb 6207
a5777624
RD
6208 // Note: we are ending the event *after* doing
6209 // default processing in this case
6210 //
6211 SendEvent( wxEVT_GRID_ROW_SIZE, m_dragRowOrCol, -1, event );
6212 }
6213 else if ( m_cursorMode == WXGRID_CURSOR_RESIZE_COL )
6214 {
6215 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL);
6216 DoEndDragResizeCol();
e2b42eeb 6217
a5777624
RD
6218 // Note: we are ending the event *after* doing
6219 // default processing in this case
6220 //
6221 SendEvent( wxEVT_GRID_COL_SIZE, -1, m_dragRowOrCol, event );
58dd5b3b 6222 }
f85afd4e 6223
a5777624
RD
6224 m_dragLastPos = -1;
6225 }
6226
a5777624
RD
6227 // ------------ Right button down
6228 //
6229 else if ( event.RightDown() && coords != wxGridNoCellCoords )
6230 {
6231 DisableCellEditControl();
6232 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_CLICK,
6233 coords.GetRow(),
6234 coords.GetCol(),
6235 event ) )
f85afd4e 6236 {
a5777624 6237 // no default action at the moment
60ff3b99 6238 }
a5777624 6239 }
2d66e025 6240
a5777624
RD
6241 // ------------ Right double click
6242 //
6243 else if ( event.RightDClick() && coords != wxGridNoCellCoords )
6244 {
6245 DisableCellEditControl();
6246 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_DCLICK,
6247 coords.GetRow(),
6248 coords.GetCol(),
6249 event ) )
f85afd4e 6250 {
a5777624 6251 // no default action at the moment
60ff3b99 6252 }
a5777624
RD
6253 }
6254
6255 // ------------ Moving and no button action
6256 //
6257 else if ( event.Moving() && !event.IsButton() )
6258 {
4db6714b 6259 if ( coords.GetRow() < 0 || coords.GetCol() < 0 )
d57ad377
SN
6260 {
6261 // out of grid cell area
6262 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL);
6263 return;
6264 }
6265
a5777624
RD
6266 int dragRow = YToEdgeOfRow( y );
6267 int dragCol = XToEdgeOfCol( x );
790cc417 6268
a5777624
RD
6269 // Dragging on the corner of a cell to resize in both
6270 // directions is not implemented yet...
790cc417 6271 //
91894db3 6272 if ( dragRow >= 0 && dragCol >= 0 )
790cc417 6273 {
a5777624
RD
6274 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL);
6275 return;
6276 }
6d004f67 6277
d57ad377 6278 if ( dragRow >= 0 )
a5777624
RD
6279 {
6280 m_dragRowOrCol = dragRow;
6d004f67 6281
a5777624 6282 if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
6d004f67 6283 {
a5777624 6284 if ( CanDragRowSize() && CanDragGridSize() )
521ff07e 6285 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW, NULL, false);
6d004f67 6286 }
a5777624 6287 }
91894db3 6288 else if ( dragCol >= 0 )
a5777624
RD
6289 {
6290 m_dragRowOrCol = dragCol;
e2b42eeb 6291
a5777624 6292 if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
6d004f67 6293 {
a5777624 6294 if ( CanDragColSize() && CanDragGridSize() )
521ff07e 6295 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL, NULL, false);
6d004f67
MB
6296 }
6297 }
91894db3 6298 else // Neither on a row or col edge
a5777624 6299 {
91894db3
VZ
6300 if ( m_cursorMode != WXGRID_CURSOR_SELECT_CELL )
6301 {
6302 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL);
6303 }
a5777624
RD
6304 }
6305 }
6d004f67
MB
6306}
6307
6d004f67
MB
6308void wxGrid::DoEndDragResizeRow()
6309{
6310 if ( m_dragLastPos >= 0 )
6311 {
6312 // erase the last line and resize the row
6313 //
6314 int cw, ch, left, dummy;
6315 m_gridWin->GetClientSize( &cw, &ch );
6316 CalcUnscrolledPosition( 0, 0, &left, &dummy );
6317
6318 wxClientDC dc( m_gridWin );
6319 PrepareDC( dc );
6320 dc.SetLogicalFunction( wxINVERT );
a9339fe2 6321 dc.DrawLine( left, m_dragLastPos, left + cw, m_dragLastPos );
6d004f67 6322 HideCellEditControl();
a5777624 6323 SaveEditControlValue();
6d004f67 6324
7c1cb261 6325 int rowTop = GetRowTop(m_dragRowOrCol);
6d004f67 6326 SetRowSize( m_dragRowOrCol,
b8d24d4e 6327 wxMax( m_dragLastPos - rowTop, m_minAcceptableRowHeight ) );
e2b42eeb 6328
6d004f67
MB
6329 if ( !GetBatchCount() )
6330 {
6331 // Only needed to get the correct rect.y:
6332 wxRect rect ( CellToRect( m_dragRowOrCol, 0 ) );
6333 rect.x = 0;
6334 CalcScrolledPosition(0, rect.y, &dummy, &rect.y);
6335 rect.width = m_rowLabelWidth;
6336 rect.height = ch - rect.y;
ca65c044 6337 m_rowLabelWin->Refresh( true, &rect );
6d004f67 6338 rect.width = cw;
ccdee36f 6339
27f35b66
SN
6340 // if there is a multicell block, paint all of it
6341 if (m_table)
6342 {
6343 int i, cell_rows, cell_cols, subtract_rows = 0;
6344 int leftCol = XToCol(left);
a9339fe2 6345 int rightCol = internalXToCol(left + cw);
27f35b66
SN
6346 if (leftCol >= 0)
6347 {
27f35b66
SN
6348 for (i=leftCol; i<rightCol; i++)
6349 {
6350 GetCellSize(m_dragRowOrCol, i, &cell_rows, &cell_cols);
6351 if (cell_rows < subtract_rows)
6352 subtract_rows = cell_rows;
6353 }
6354 rect.y = GetRowTop(m_dragRowOrCol + subtract_rows);
6355 CalcScrolledPosition(0, rect.y, &dummy, &rect.y);
6356 rect.height = ch - rect.y;
6357 }
6358 }
ca65c044 6359 m_gridWin->Refresh( false, &rect );
6d004f67
MB
6360 }
6361
6362 ShowCellEditControl();
6363 }
6364}
6365
6366
6367void wxGrid::DoEndDragResizeCol()
6368{
6369 if ( m_dragLastPos >= 0 )
6370 {
6371 // erase the last line and resize the col
6372 //
6373 int cw, ch, dummy, top;
6374 m_gridWin->GetClientSize( &cw, &ch );
6375 CalcUnscrolledPosition( 0, 0, &dummy, &top );
6376
6377 wxClientDC dc( m_gridWin );
6378 PrepareDC( dc );
6379 dc.SetLogicalFunction( wxINVERT );
a9339fe2 6380 dc.DrawLine( m_dragLastPos, top, m_dragLastPos, top + ch );
6d004f67 6381 HideCellEditControl();
a5777624 6382 SaveEditControlValue();
6d004f67 6383
7c1cb261 6384 int colLeft = GetColLeft(m_dragRowOrCol);
6d004f67 6385 SetColSize( m_dragRowOrCol,
43947979
VZ
6386 wxMax( m_dragLastPos - colLeft,
6387 GetColMinimalWidth(m_dragRowOrCol) ) );
6d004f67
MB
6388
6389 if ( !GetBatchCount() )
6390 {
6391 // Only needed to get the correct rect.x:
6392 wxRect rect ( CellToRect( 0, m_dragRowOrCol ) );
6393 rect.y = 0;
6394 CalcScrolledPosition(rect.x, 0, &rect.x, &dummy);
6395 rect.width = cw - rect.x;
6396 rect.height = m_colLabelHeight;
ca65c044 6397 m_colLabelWin->Refresh( true, &rect );
6d004f67 6398 rect.height = ch;
2f024384 6399
27f35b66
SN
6400 // if there is a multicell block, paint all of it
6401 if (m_table)
6402 {
6403 int i, cell_rows, cell_cols, subtract_cols = 0;
6404 int topRow = YToRow(top);
a9339fe2 6405 int bottomRow = internalYToRow(top + cw);
27f35b66
SN
6406 if (topRow >= 0)
6407 {
27f35b66
SN
6408 for (i=topRow; i<bottomRow; i++)
6409 {
6410 GetCellSize(i, m_dragRowOrCol, &cell_rows, &cell_cols);
6411 if (cell_cols < subtract_cols)
6412 subtract_cols = cell_cols;
6413 }
a9339fe2 6414
27f35b66
SN
6415 rect.x = GetColLeft(m_dragRowOrCol + subtract_cols);
6416 CalcScrolledPosition(rect.x, 0, &rect.x, &dummy);
6417 rect.width = cw - rect.x;
6418 }
6419 }
2f024384 6420
ca65c044 6421 m_gridWin->Refresh( false, &rect );
790cc417 6422 }
e2b42eeb 6423
6d004f67 6424 ShowCellEditControl();
f85afd4e 6425 }
f85afd4e
MB
6426}
6427
d4175745
VZ
6428void wxGrid::DoEndDragMoveCol()
6429{
6430 //The user clicked on the column but didn't actually drag
6431 if ( m_dragLastPos < 0 )
6432 {
6433 m_colLabelWin->Refresh(); //Do this to "unpress" the column
6434 return;
6435 }
6436
6437 int newPos;
6438 if ( m_moveToCol == -1 )
6439 newPos = m_numCols - 1;
6440 else
6441 {
6442 newPos = GetColPos( m_moveToCol );
6443 if ( newPos > GetColPos( m_dragRowOrCol ) )
6444 newPos--;
6445 }
6446
6447 SetColPos( m_dragRowOrCol, newPos );
6448}
6449
6450void wxGrid::SetColPos( int colID, int newPos )
6451{
6452 if ( m_colAt.IsEmpty() )
6453 {
6454 m_colAt.Alloc( m_numCols );
6455
6456 int i;
6457 for ( i = 0; i < m_numCols; i++ )
6458 {
6459 m_colAt.Add( i );
6460 }
6461 }
6462
6463 int oldPos = GetColPos( colID );
6464
6465 //Reshuffle the m_colAt array
6466 if ( newPos > oldPos )
6467 {
6468 int i;
6469 for ( i = oldPos; i < newPos; i++ )
6470 {
6471 m_colAt[i] = m_colAt[i+1];
6472 }
6473 }
6474 else
6475 {
6476 int i;
6477 for ( i = oldPos; i > newPos; i-- )
6478 {
6479 m_colAt[i] = m_colAt[i-1];
6480 }
6481 }
6482
6483 m_colAt[newPos] = colID;
6484
6485 //Recalculate the column rights
6486 if ( !m_colWidths.IsEmpty() )
6487 {
6488 int colRight = 0;
6489 int colPos;
6490 for ( colPos = 0; colPos < m_numCols; colPos++ )
6491 {
6492 int colID = GetColAt( colPos );
6493
6494 colRight += m_colWidths[colID];
6495 m_colRights[colID] = colRight;
6496 }
6497 }
6498
6499 m_colLabelWin->Refresh();
6500 m_gridWin->Refresh();
6501}
6502
6503
6504
6505void wxGrid::EnableDragColMove( bool enable )
6506{
6507 if ( m_canDragColMove == enable )
6508 return;
6509
6510 m_canDragColMove = enable;
6511
6512 if ( !m_canDragColMove )
6513 {
6514 m_colAt.Clear();
6515
6516 //Recalculate the column rights
6517 if ( !m_colWidths.IsEmpty() )
6518 {
6519 int colRight = 0;
6520 int colPos;
6521 for ( colPos = 0; colPos < m_numCols; colPos++ )
6522 {
6523 colRight += m_colWidths[colPos];
6524 m_colRights[colPos] = colRight;
6525 }
6526 }
6527
6528 m_colLabelWin->Refresh();
6529 m_gridWin->Refresh();
6530 }
6531}
6532
6533
2d66e025
MB
6534//
6535// ------ interaction with data model
6536//
6537bool wxGrid::ProcessTableMessage( wxGridTableMessage& msg )
f85afd4e 6538{
2d66e025 6539 switch ( msg.GetId() )
17732cec 6540 {
2d66e025
MB
6541 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES:
6542 return GetModelValues();
17732cec 6543
2d66e025
MB
6544 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES:
6545 return SetModelValues();
f85afd4e 6546
2d66e025
MB
6547 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED:
6548 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED:
6549 case wxGRIDTABLE_NOTIFY_ROWS_DELETED:
6550 case wxGRIDTABLE_NOTIFY_COLS_INSERTED:
6551 case wxGRIDTABLE_NOTIFY_COLS_APPENDED:
6552 case wxGRIDTABLE_NOTIFY_COLS_DELETED:
6553 return Redimension( msg );
6554
6555 default:
ca65c044 6556 return false;
f85afd4e 6557 }
2d66e025 6558}
f85afd4e 6559
2d66e025 6560// The behaviour of this function depends on the grid table class
5b061713 6561// Clear() function. For the default wxGridStringTable class the
2d66e025
MB
6562// behavious is to replace all cell contents with wxEmptyString but
6563// not to change the number of rows or cols.
6564//
6565void wxGrid::ClearGrid()
6566{
6567 if ( m_table )
f85afd4e 6568 {
4cfa5de6
RD
6569 if (IsCellEditControlEnabled())
6570 DisableCellEditControl();
6571
2d66e025 6572 m_table->Clear();
5b061713 6573 if (!GetBatchCount())
a9339fe2 6574 m_gridWin->Refresh();
f85afd4e
MB
6575 }
6576}
6577
2d66e025 6578bool wxGrid::InsertRows( int pos, int numRows, bool WXUNUSED(updateLabels) )
f85afd4e 6579{
2d66e025 6580 // TODO: something with updateLabels flag
8f177c8e 6581
2d66e025 6582 if ( !m_created )
f85afd4e 6583 {
bd3c6758 6584 wxFAIL_MSG( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") );
ca65c044 6585 return false;
2d66e025 6586 }
8f177c8e 6587
2d66e025
MB
6588 if ( m_table )
6589 {
b7fff980 6590 if (IsCellEditControlEnabled())
b54ba671 6591 DisableCellEditControl();
b7fff980 6592
4ea3479c 6593 bool done = m_table->InsertRows( pos, numRows );
4ea3479c 6594 return done;
f85afd4e 6595
2d66e025
MB
6596 // the table will have sent the results of the insert row
6597 // operation to this view object as a grid table message
f85afd4e 6598 }
a9339fe2 6599
ca65c044 6600 return false;
f85afd4e
MB
6601}
6602
2d66e025 6603bool wxGrid::AppendRows( int numRows, bool WXUNUSED(updateLabels) )
f85afd4e 6604{
2d66e025
MB
6605 // TODO: something with updateLabels flag
6606
6607 if ( !m_created )
f85afd4e 6608 {
bd3c6758 6609 wxFAIL_MSG( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") );
ca65c044 6610 return false;
2d66e025
MB
6611 }
6612
4ea3479c
JS
6613 if ( m_table )
6614 {
6615 bool done = m_table && m_table->AppendRows( numRows );
4ea3479c 6616 return done;
a9339fe2 6617
4ea3479c
JS
6618 // the table will have sent the results of the append row
6619 // operation to this view object as a grid table message
6620 }
a9339fe2 6621
ca65c044 6622 return false;
f85afd4e
MB
6623}
6624
2d66e025 6625bool wxGrid::DeleteRows( int pos, int numRows, bool WXUNUSED(updateLabels) )
f85afd4e 6626{
2d66e025
MB
6627 // TODO: something with updateLabels flag
6628
6629 if ( !m_created )
f85afd4e 6630 {
bd3c6758 6631 wxFAIL_MSG( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") );
ca65c044 6632 return false;
2d66e025
MB
6633 }
6634
b7fff980 6635 if ( m_table )
2d66e025 6636 {
b7fff980 6637 if (IsCellEditControlEnabled())
b54ba671 6638 DisableCellEditControl();
f85afd4e 6639
4ea3479c 6640 bool done = m_table->DeleteRows( pos, numRows );
4ea3479c 6641 return done;
f6bcfd97
BP
6642 // the table will have sent the results of the delete row
6643 // operation to this view object as a grid table message
2d66e025 6644 }
a9339fe2 6645
ca65c044 6646 return false;
2d66e025 6647}
f85afd4e 6648
2d66e025
MB
6649bool wxGrid::InsertCols( int pos, int numCols, bool WXUNUSED(updateLabels) )
6650{
6651 // TODO: something with updateLabels flag
8f177c8e 6652
2d66e025
MB
6653 if ( !m_created )
6654 {
bd3c6758 6655 wxFAIL_MSG( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") );
ca65c044 6656 return false;
2d66e025 6657 }
f85afd4e 6658
2d66e025
MB
6659 if ( m_table )
6660 {
b7fff980 6661 if (IsCellEditControlEnabled())
b54ba671 6662 DisableCellEditControl();
b7fff980 6663
4ea3479c 6664 bool done = m_table->InsertCols( pos, numCols );
4ea3479c 6665 return done;
2d66e025
MB
6666 // the table will have sent the results of the insert col
6667 // operation to this view object as a grid table message
f85afd4e 6668 }
2f024384 6669
ca65c044 6670 return false;
f85afd4e
MB
6671}
6672
2d66e025 6673bool wxGrid::AppendCols( int numCols, bool WXUNUSED(updateLabels) )
f85afd4e 6674{
2d66e025
MB
6675 // TODO: something with updateLabels flag
6676
6677 if ( !m_created )
f85afd4e 6678 {
bd3c6758 6679 wxFAIL_MSG( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") );
ca65c044 6680 return false;
2d66e025 6681 }
f85afd4e 6682
4ea3479c
JS
6683 if ( m_table )
6684 {
6685 bool done = m_table->AppendCols( numCols );
4ea3479c
JS
6686 return done;
6687 // the table will have sent the results of the append col
6688 // operation to this view object as a grid table message
6689 }
2f024384 6690
ca65c044 6691 return false;
f85afd4e
MB
6692}
6693
2d66e025 6694bool wxGrid::DeleteCols( int pos, int numCols, bool WXUNUSED(updateLabels) )
f85afd4e 6695{
2d66e025 6696 // TODO: something with updateLabels flag
8f177c8e 6697
2d66e025 6698 if ( !m_created )
f85afd4e 6699 {
bd3c6758 6700 wxFAIL_MSG( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") );
ca65c044 6701 return false;
f85afd4e
MB
6702 }
6703
b7fff980 6704 if ( m_table )
2d66e025 6705 {
b7fff980 6706 if (IsCellEditControlEnabled())
b54ba671 6707 DisableCellEditControl();
8f177c8e 6708
4ea3479c 6709 bool done = m_table->DeleteCols( pos, numCols );
4ea3479c 6710 return done;
f6bcfd97
BP
6711 // the table will have sent the results of the delete col
6712 // operation to this view object as a grid table message
f85afd4e 6713 }
2f024384 6714
ca65c044 6715 return false;
f85afd4e
MB
6716}
6717
2d66e025
MB
6718//
6719// ----- event handlers
f85afd4e 6720//
8f177c8e 6721
2d66e025
MB
6722// Generate a grid event based on a mouse event and
6723// return the result of ProcessEvent()
6724//
fe7b9ed6 6725int wxGrid::SendEvent( const wxEventType type,
2d66e025
MB
6726 int row, int col,
6727 wxMouseEvent& mouseEv )
6728{
2f024384 6729 bool claimed, vetoed;
fe7b9ed6 6730
97a9929e
VZ
6731 if ( type == wxEVT_GRID_ROW_SIZE || type == wxEVT_GRID_COL_SIZE )
6732 {
6733 int rowOrCol = (row == -1 ? col : row);
6734
6735 wxGridSizeEvent gridEvt( GetId(),
6736 type,
6737 this,
6738 rowOrCol,
6739 mouseEv.GetX() + GetRowLabelSize(),
6740 mouseEv.GetY() + GetColLabelSize(),
6741 mouseEv.ControlDown(),
6742 mouseEv.ShiftDown(),
6743 mouseEv.AltDown(),
6744 mouseEv.MetaDown() );
6745
6746 claimed = GetEventHandler()->ProcessEvent(gridEvt);
6747 vetoed = !gridEvt.IsAllowed();
6748 }
fe7b9ed6 6749 else if ( type == wxEVT_GRID_RANGE_SELECT )
97a9929e
VZ
6750 {
6751 // Right now, it should _never_ end up here!
6752 wxGridRangeSelectEvent gridEvt( GetId(),
6753 type,
6754 this,
6755 m_selectingTopLeft,
6756 m_selectingBottomRight,
ca65c044 6757 true,
97a9929e
VZ
6758 mouseEv.ControlDown(),
6759 mouseEv.ShiftDown(),
6760 mouseEv.AltDown(),
6761 mouseEv.MetaDown() );
6762
6763 claimed = GetEventHandler()->ProcessEvent(gridEvt);
6764 vetoed = !gridEvt.IsAllowed();
6765 }
2b73a34e
RD
6766 else if ( type == wxEVT_GRID_LABEL_LEFT_CLICK ||
6767 type == wxEVT_GRID_LABEL_LEFT_DCLICK ||
6768 type == wxEVT_GRID_LABEL_RIGHT_CLICK ||
6769 type == wxEVT_GRID_LABEL_RIGHT_DCLICK )
6770 {
6771 wxPoint pos = mouseEv.GetPosition();
6772
6773 if ( mouseEv.GetEventObject() == GetGridRowLabelWindow() )
6774 pos.y += GetColLabelSize();
6775 if ( mouseEv.GetEventObject() == GetGridColLabelWindow() )
6776 pos.x += GetRowLabelSize();
c71b2126 6777
2b73a34e
RD
6778 wxGridEvent gridEvt( GetId(),
6779 type,
6780 this,
6781 row, col,
6782 pos.x,
6783 pos.y,
6784 false,
6785 mouseEv.ControlDown(),
6786 mouseEv.ShiftDown(),
6787 mouseEv.AltDown(),
6788 mouseEv.MetaDown() );
6789 claimed = GetEventHandler()->ProcessEvent(gridEvt);
6790 vetoed = !gridEvt.IsAllowed();
6791 }
fe7b9ed6 6792 else
97a9929e
VZ
6793 {
6794 wxGridEvent gridEvt( GetId(),
6795 type,
6796 this,
6797 row, col,
6798 mouseEv.GetX() + GetRowLabelSize(),
6799 mouseEv.GetY() + GetColLabelSize(),
ca65c044 6800 false,
97a9929e
VZ
6801 mouseEv.ControlDown(),
6802 mouseEv.ShiftDown(),
6803 mouseEv.AltDown(),
6804 mouseEv.MetaDown() );
6805 claimed = GetEventHandler()->ProcessEvent(gridEvt);
6806 vetoed = !gridEvt.IsAllowed();
6807 }
6808
6809 // A Veto'd event may not be `claimed' so test this first
4db6714b
KH
6810 if (vetoed)
6811 return -1;
6812
97a9929e 6813 return claimed ? 1 : 0;
f85afd4e
MB
6814}
6815
2d66e025
MB
6816// Generate a grid event of specified type and return the result
6817// of ProcessEvent().
f85afd4e 6818//
fe7b9ed6 6819int wxGrid::SendEvent( const wxEventType type,
2d66e025 6820 int row, int col )
f85afd4e 6821{
a9339fe2 6822 bool claimed, vetoed;
fe7b9ed6 6823
b54ba671 6824 if ( type == wxEVT_GRID_ROW_SIZE || type == wxEVT_GRID_COL_SIZE )
f85afd4e 6825 {
2d66e025 6826 int rowOrCol = (row == -1 ? col : row);
f85afd4e 6827
a9339fe2 6828 wxGridSizeEvent gridEvt( GetId(), type, this, rowOrCol );
2d66e025 6829
fe7b9ed6
VZ
6830 claimed = GetEventHandler()->ProcessEvent(gridEvt);
6831 vetoed = !gridEvt.IsAllowed();
2d66e025
MB
6832 }
6833 else
6834 {
a9339fe2 6835 wxGridEvent gridEvt( GetId(), type, this, row, col );
8f177c8e 6836
fe7b9ed6
VZ
6837 claimed = GetEventHandler()->ProcessEvent(gridEvt);
6838 vetoed = !gridEvt.IsAllowed();
6839 }
6840
97a9929e 6841 // A Veto'd event may not be `claimed' so test this first
4db6714b
KH
6842 if (vetoed)
6843 return -1;
6844
97a9929e 6845 return claimed ? 1 : 0;
f85afd4e
MB
6846}
6847
2d66e025 6848void wxGrid::OnPaint( wxPaintEvent& WXUNUSED(event) )
f85afd4e 6849{
3d59537f
DS
6850 // needed to prevent zillions of paint events on MSW
6851 wxPaintDC dc(this);
8f177c8e 6852}
f85afd4e 6853
bca7bfc8 6854void wxGrid::Refresh(bool eraseb, const wxRect* rect)
27f35b66 6855{
ad603bf7
VZ
6856 // Don't do anything if between Begin/EndBatch...
6857 // EndBatch() will do all this on the last nested one anyway.
27f35b66
SN
6858 if (! GetBatchCount())
6859 {
bca7bfc8 6860 // Refresh to get correct scrolled position:
4db6714b 6861 wxScrolledWindow::Refresh(eraseb, rect);
27f35b66 6862
27f35b66
SN
6863 if (rect)
6864 {
bca7bfc8
SN
6865 int rect_x, rect_y, rectWidth, rectHeight;
6866 int width_label, width_cell, height_label, height_cell;
6867 int x, y;
6868
2f024384 6869 // Copy rectangle can get scroll offsets..
bca7bfc8
SN
6870 rect_x = rect->GetX();
6871 rect_y = rect->GetY();
6872 rectWidth = rect->GetWidth();
6873 rectHeight = rect->GetHeight();
27f35b66 6874
bca7bfc8 6875 width_label = m_rowLabelWidth - rect_x;
4db6714b
KH
6876 if (width_label > rectWidth)
6877 width_label = rectWidth;
27f35b66 6878
bca7bfc8 6879 height_label = m_colLabelHeight - rect_y;
a9339fe2
DS
6880 if (height_label > rectHeight)
6881 height_label = rectHeight;
27f35b66 6882
bca7bfc8
SN
6883 if (rect_x > m_rowLabelWidth)
6884 {
6885 x = rect_x - m_rowLabelWidth;
6886 width_cell = rectWidth;
6887 }
6888 else
6889 {
6890 x = 0;
6891 width_cell = rectWidth - (m_rowLabelWidth - rect_x);
6892 }
6893
6894 if (rect_y > m_colLabelHeight)
6895 {
6896 y = rect_y - m_colLabelHeight;
6897 height_cell = rectHeight;
6898 }
6899 else
6900 {
6901 y = 0;
6902 height_cell = rectHeight - (m_colLabelHeight - rect_y);
6903 }
6904
6905 // Paint corner label part intersecting rect.
6906 if ( width_label > 0 && height_label > 0 )
6907 {
6908 wxRect anotherrect(rect_x, rect_y, width_label, height_label);
6909 m_cornerLabelWin->Refresh(eraseb, &anotherrect);
6910 }
6911
6912 // Paint col labels part intersecting rect.
6913 if ( width_cell > 0 && height_label > 0 )
6914 {
6915 wxRect anotherrect(x, rect_y, width_cell, height_label);
6916 m_colLabelWin->Refresh(eraseb, &anotherrect);
6917 }
6918
6919 // Paint row labels part intersecting rect.
6920 if ( width_label > 0 && height_cell > 0 )
6921 {
6922 wxRect anotherrect(rect_x, y, width_label, height_cell);
6923 m_rowLabelWin->Refresh(eraseb, &anotherrect);
6924 }
6925
6926 // Paint cell area part intersecting rect.
6927 if ( width_cell > 0 && height_cell > 0 )
6928 {
6929 wxRect anotherrect(x, y, width_cell, height_cell);
6930 m_gridWin->Refresh(eraseb, &anotherrect);
6931 }
6932 }
6933 else
6934 {
6935 m_cornerLabelWin->Refresh(eraseb, NULL);
2b5f62a0 6936 m_colLabelWin->Refresh(eraseb, NULL);
bca7bfc8
SN
6937 m_rowLabelWin->Refresh(eraseb, NULL);
6938 m_gridWin->Refresh(eraseb, NULL);
6939 }
27f35b66
SN
6940 }
6941}
f85afd4e 6942
c71b2126 6943void wxGrid::OnSize(wxSizeEvent& WXUNUSED(event))
f85afd4e 6944{
b93aafab
JS
6945 if (m_targetWindow != this) // check whether initialisation has been done
6946 {
6947 // update our children window positions and scrollbars
6948 CalcDimensions();
6949 }
f85afd4e
MB
6950}
6951
2d66e025 6952void wxGrid::OnKeyDown( wxKeyEvent& event )
f85afd4e 6953{
2d66e025 6954 if ( m_inOnKeyDown )
f85afd4e 6955 {
2d66e025
MB
6956 // shouldn't be here - we are going round in circles...
6957 //
07296f0b 6958 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") );
f85afd4e
MB
6959 }
6960
ca65c044 6961 m_inOnKeyDown = true;
f85afd4e 6962
2d66e025 6963 // propagate the event up and see if it gets processed
2d66e025
MB
6964 wxWindow *parent = GetParent();
6965 wxKeyEvent keyEvt( event );
6966 keyEvt.SetEventObject( parent );
6967
6968 if ( !parent->GetEventHandler()->ProcessEvent( keyEvt ) )
f85afd4e 6969 {
bcb614b3
RR
6970 if (GetLayoutDirection() == wxLayout_RightToLeft)
6971 {
6972 if (event.GetKeyCode() == WXK_RIGHT)
6973 event.m_keyCode = WXK_LEFT;
6974 else if (event.GetKeyCode() == WXK_LEFT)
6975 event.m_keyCode = WXK_RIGHT;
6976 }
c71b2126 6977
2d66e025 6978 // try local handlers
12a3f227 6979 switch ( event.GetKeyCode() )
2d66e025
MB
6980 {
6981 case WXK_UP:
6982 if ( event.ControlDown() )
5c8fc7c1 6983 MoveCursorUpBlock( event.ShiftDown() );
2d66e025 6984 else
5c8fc7c1 6985 MoveCursorUp( event.ShiftDown() );
2d66e025 6986 break;
f85afd4e 6987
2d66e025
MB
6988 case WXK_DOWN:
6989 if ( event.ControlDown() )
5c8fc7c1 6990 MoveCursorDownBlock( event.ShiftDown() );
2d66e025 6991 else
5c8fc7c1 6992 MoveCursorDown( event.ShiftDown() );
2d66e025 6993 break;
8f177c8e 6994
2d66e025
MB
6995 case WXK_LEFT:
6996 if ( event.ControlDown() )
5c8fc7c1 6997 MoveCursorLeftBlock( event.ShiftDown() );
2d66e025 6998 else
5c8fc7c1 6999 MoveCursorLeft( event.ShiftDown() );
2d66e025
MB
7000 break;
7001
7002 case WXK_RIGHT:
7003 if ( event.ControlDown() )
5c8fc7c1 7004 MoveCursorRightBlock( event.ShiftDown() );
2d66e025 7005 else
5c8fc7c1 7006 MoveCursorRight( event.ShiftDown() );
2d66e025 7007 break;
b99be8fb 7008
2d66e025 7009 case WXK_RETURN:
a4f7bf58 7010 case WXK_NUMPAD_ENTER:
58dd5b3b
MB
7011 if ( event.ControlDown() )
7012 {
7013 event.Skip(); // to let the edit control have the return
7014 }
7015 else
7016 {
f6bcfd97
BP
7017 if ( GetGridCursorRow() < GetNumberRows()-1 )
7018 {
7019 MoveCursorDown( event.ShiftDown() );
7020 }
7021 else
7022 {
7023 // at the bottom of a column
13f6e9e8 7024 DisableCellEditControl();
f6bcfd97 7025 }
58dd5b3b 7026 }
2d66e025
MB
7027 break;
7028
5c8fc7c1 7029 case WXK_ESCAPE:
e32352cf 7030 ClearSelection();
5c8fc7c1
SN
7031 break;
7032
2c9a89e0
RD
7033 case WXK_TAB:
7034 if (event.ShiftDown())
f6bcfd97
BP
7035 {
7036 if ( GetGridCursorCol() > 0 )
7037 {
ca65c044 7038 MoveCursorLeft( false );
f6bcfd97
BP
7039 }
7040 else
7041 {
7042 // at left of grid
13f6e9e8 7043 DisableCellEditControl();
f6bcfd97
BP
7044 }
7045 }
2c9a89e0 7046 else
f6bcfd97 7047 {
ccdee36f 7048 if ( GetGridCursorCol() < GetNumberCols() - 1 )
f6bcfd97 7049 {
ca65c044 7050 MoveCursorRight( false );
f6bcfd97
BP
7051 }
7052 else
7053 {
7054 // at right of grid
13f6e9e8 7055 DisableCellEditControl();
f6bcfd97
BP
7056 }
7057 }
2c9a89e0
RD
7058 break;
7059
2d66e025
MB
7060 case WXK_HOME:
7061 if ( event.ControlDown() )
7062 {
7063 MakeCellVisible( 0, 0 );
7064 SetCurrentCell( 0, 0 );
7065 }
7066 else
7067 {
7068 event.Skip();
7069 }
7070 break;
7071
7072 case WXK_END:
7073 if ( event.ControlDown() )
7074 {
a9339fe2
DS
7075 MakeCellVisible( m_numRows - 1, m_numCols - 1 );
7076 SetCurrentCell( m_numRows - 1, m_numCols - 1 );
2d66e025
MB
7077 }
7078 else
7079 {
7080 event.Skip();
7081 }
7082 break;
7083
faa94f3e 7084 case WXK_PAGEUP:
2d66e025
MB
7085 MovePageUp();
7086 break;
7087
faa94f3e 7088 case WXK_PAGEDOWN:
2d66e025
MB
7089 MovePageDown();
7090 break;
7091
07296f0b 7092 case WXK_SPACE:
aa5e1f75
SN
7093 if ( event.ControlDown() )
7094 {
3f3dc2ef
VZ
7095 if ( m_selection )
7096 {
a9339fe2
DS
7097 m_selection->ToggleCellSelection(
7098 m_currentCellCoords.GetRow(),
7099 m_currentCellCoords.GetCol(),
7100 event.ControlDown(),
7101 event.ShiftDown(),
7102 event.AltDown(),
7103 event.MetaDown() );
3f3dc2ef 7104 }
aa5e1f75
SN
7105 break;
7106 }
ccdee36f 7107
07296f0b 7108 if ( !IsEditable() )
ca65c044 7109 MoveCursorRight( false );
ccdee36f
DS
7110 else
7111 event.Skip();
7112 break;
07296f0b 7113
2d66e025 7114 default:
63e2147c 7115 event.Skip();
025562fe 7116 break;
2d66e025 7117 }
f85afd4e
MB
7118 }
7119
ca65c044 7120 m_inOnKeyDown = false;
f85afd4e
MB
7121}
7122
f6bcfd97
BP
7123void wxGrid::OnKeyUp( wxKeyEvent& event )
7124{
7125 // try local handlers
7126 //
12a3f227 7127 if ( event.GetKeyCode() == WXK_SHIFT )
f6bcfd97
BP
7128 {
7129 if ( m_selectingTopLeft != wxGridNoCellCoords &&
7130 m_selectingBottomRight != wxGridNoCellCoords )
3f3dc2ef
VZ
7131 {
7132 if ( m_selection )
7133 {
a9339fe2
DS
7134 m_selection->SelectBlock(
7135 m_selectingTopLeft.GetRow(),
7136 m_selectingTopLeft.GetCol(),
7137 m_selectingBottomRight.GetRow(),
7138 m_selectingBottomRight.GetCol(),
7139 event.ControlDown(),
7140 true,
7141 event.AltDown(),
7142 event.MetaDown() );
3f3dc2ef
VZ
7143 }
7144 }
7145
f6bcfd97
BP
7146 m_selectingTopLeft = wxGridNoCellCoords;
7147 m_selectingBottomRight = wxGridNoCellCoords;
7148 m_selectingKeyboard = wxGridNoCellCoords;
7149 }
7150}
7151
63e2147c
RD
7152void wxGrid::OnChar( wxKeyEvent& event )
7153{
7154 // is it possible to edit the current cell at all?
7155 if ( !IsCellEditControlEnabled() && CanEnableCellControl() )
7156 {
7157 // yes, now check whether the cells editor accepts the key
7158 int row = m_currentCellCoords.GetRow();
7159 int col = m_currentCellCoords.GetCol();
2f024384 7160 wxGridCellAttr *attr = GetCellAttr(row, col);
63e2147c
RD
7161 wxGridCellEditor *editor = attr->GetEditor(this, row, col);
7162
7163 // <F2> is special and will always start editing, for
7164 // other keys - ask the editor itself
7165 if ( (event.GetKeyCode() == WXK_F2 && !event.HasModifiers())
7166 || editor->IsAcceptedKey(event) )
7167 {
7168 // ensure cell is visble
7169 MakeCellVisible(row, col);
7170 EnableCellEditControl();
7171
7172 // a problem can arise if the cell is not completely
7173 // visible (even after calling MakeCellVisible the
7174 // control is not created and calling StartingKey will
7175 // crash the app
046d682f 7176 if ( event.GetKeyCode() != WXK_F2 && editor->IsCreated() && m_cellEditCtrlEnabled )
63e2147c
RD
7177 editor->StartingKey(event);
7178 }
7179 else
7180 {
7181 event.Skip();
7182 }
7183
7184 editor->DecRef();
7185 attr->DecRef();
7186 }
7187 else
7188 {
7189 event.Skip();
7190 }
7191}
7192
2796cce3 7193void wxGrid::OnEraseBackground(wxEraseEvent&)
508011ce
VZ
7194{
7195}
07296f0b 7196
2d66e025 7197void wxGrid::SetCurrentCell( const wxGridCellCoords& coords )
66242c80 7198{
f6bcfd97
BP
7199 if ( SendEvent( wxEVT_GRID_SELECT_CELL, coords.GetRow(), coords.GetCol() ) )
7200 {
7201 // the event has been intercepted - do nothing
7202 return;
7203 }
7204
9553702e 7205#if !defined(__WXMAC__)
bee19958
DS
7206 wxClientDC dc( m_gridWin );
7207 PrepareDC( dc );
5d38a5f3 7208#endif
f6bcfd97 7209
2a7750d9 7210 if ( m_currentCellCoords != wxGridNoCellCoords )
2d66e025 7211 {
b54ba671 7212 DisableCellEditControl();
07296f0b 7213
ca65c044 7214 if ( IsVisible( m_currentCellCoords, false ) )
f6bcfd97
BP
7215 {
7216 wxRect r;
bee19958 7217 r = BlockToDeviceRect( m_currentCellCoords, m_currentCellCoords );
f6bcfd97
BP
7218 if ( !m_gridLinesEnabled )
7219 {
7220 r.x--;
7221 r.y--;
7222 r.width++;
7223 r.height++;
7224 }
d1c0b4f9 7225
3ed884a0 7226 wxGridCellCoordsArray cells = CalcCellsExposed( r );
84912ef8 7227
f6bcfd97
BP
7228 // Otherwise refresh redraws the highlight!
7229 m_currentCellCoords = coords;
275c4ae4 7230
9553702e 7231#if defined(__WXMAC__)
5d38a5f3
JS
7232 m_gridWin->Refresh(true /*, & r */);
7233#else
bee19958 7234 DrawGridCellArea( dc, cells );
f6bcfd97 7235 DrawAllGridLines( dc, r );
5d38a5f3 7236#endif
f6bcfd97 7237 }
66242c80 7238 }
8f177c8e 7239
2d66e025
MB
7240 m_currentCellCoords = coords;
7241
bee19958 7242 wxGridCellAttr *attr = GetCellAttr( coords );
9553702e 7243#if !defined(__WXMAC__)
bee19958 7244 DrawCellHighlight( dc, attr );
5d38a5f3 7245#endif
2a7750d9 7246 attr->DecRef();
66242c80
MB
7247}
7248
c9097836
MB
7249void wxGrid::HighlightBlock( int topRow, int leftCol, int bottomRow, int rightCol )
7250{
7251 int temp;
7252 wxGridCellCoords updateTopLeft, updateBottomRight;
7253
3f3dc2ef 7254 if ( m_selection )
c9097836 7255 {
3f3dc2ef
VZ
7256 if ( m_selection->GetSelectionMode() == wxGrid::wxGridSelectRows )
7257 {
7258 leftCol = 0;
7259 rightCol = GetNumberCols() - 1;
7260 }
7261 else if ( m_selection->GetSelectionMode() == wxGrid::wxGridSelectColumns )
7262 {
7263 topRow = 0;
7264 bottomRow = GetNumberRows() - 1;
7265 }
c9097836 7266 }
3f3dc2ef 7267
c9097836
MB
7268 if ( topRow > bottomRow )
7269 {
7270 temp = topRow;
7271 topRow = bottomRow;
7272 bottomRow = temp;
7273 }
7274
7275 if ( leftCol > rightCol )
7276 {
7277 temp = leftCol;
7278 leftCol = rightCol;
7279 rightCol = temp;
7280 }
7281
7282 updateTopLeft = wxGridCellCoords( topRow, leftCol );
7283 updateBottomRight = wxGridCellCoords( bottomRow, rightCol );
7284
3ed884a0
SN
7285 // First the case that we selected a completely new area
7286 if ( m_selectingTopLeft == wxGridNoCellCoords ||
7287 m_selectingBottomRight == wxGridNoCellCoords )
7288 {
7289 wxRect rect;
7290 rect = BlockToDeviceRect( wxGridCellCoords ( topRow, leftCol ),
7291 wxGridCellCoords ( bottomRow, rightCol ) );
ca65c044 7292 m_gridWin->Refresh( false, &rect );
3ed884a0 7293 }
2f024384 7294
3ed884a0
SN
7295 // Now handle changing an existing selection area.
7296 else if ( m_selectingTopLeft != updateTopLeft ||
7297 m_selectingBottomRight != updateBottomRight )
c9097836
MB
7298 {
7299 // Compute two optimal update rectangles:
7300 // Either one rectangle is a real subset of the
7301 // other, or they are (almost) disjoint!
7302 wxRect rect[4];
7303 bool need_refresh[4];
7304 need_refresh[0] =
7305 need_refresh[1] =
7306 need_refresh[2] =
ca65c044 7307 need_refresh[3] = false;
c9097836
MB
7308 int i;
7309
7310 // Store intermediate values
a9339fe2
DS
7311 wxCoord oldLeft = m_selectingTopLeft.GetCol();
7312 wxCoord oldTop = m_selectingTopLeft.GetRow();
7313 wxCoord oldRight = m_selectingBottomRight.GetCol();
c9097836
MB
7314 wxCoord oldBottom = m_selectingBottomRight.GetRow();
7315
7316 // Determine the outer/inner coordinates.
7317 if (oldLeft > leftCol)
7318 {
7319 temp = oldLeft;
7320 oldLeft = leftCol;
7321 leftCol = temp;
7322 }
7323 if (oldTop > topRow )
7324 {
7325 temp = oldTop;
7326 oldTop = topRow;
7327 topRow = temp;
7328 }
7329 if (oldRight < rightCol )
7330 {
7331 temp = oldRight;
7332 oldRight = rightCol;
7333 rightCol = temp;
7334 }
7335 if (oldBottom < bottomRow)
7336 {
7337 temp = oldBottom;
7338 oldBottom = bottomRow;
7339 bottomRow = temp;
7340 }
7341
7342 // Now, either the stuff marked old is the outer
7343 // rectangle or we don't have a situation where one
7344 // is contained in the other.
7345
7346 if ( oldLeft < leftCol )
7347 {
3ed884a0
SN
7348 // Refresh the newly selected or deselected
7349 // area to the left of the old or new selection.
ca65c044 7350 need_refresh[0] = true;
a9339fe2
DS
7351 rect[0] = BlockToDeviceRect(
7352 wxGridCellCoords( oldTop, oldLeft ),
7353 wxGridCellCoords( oldBottom, leftCol - 1 ) );
c9097836
MB
7354 }
7355
2f024384 7356 if ( oldTop < topRow )
c9097836 7357 {
3ed884a0
SN
7358 // Refresh the newly selected or deselected
7359 // area above the old or new selection.
ca65c044 7360 need_refresh[1] = true;
2f024384
DS
7361 rect[1] = BlockToDeviceRect(
7362 wxGridCellCoords( oldTop, leftCol ),
7363 wxGridCellCoords( topRow - 1, rightCol ) );
c9097836
MB
7364 }
7365
7366 if ( oldRight > rightCol )
7367 {
3ed884a0
SN
7368 // Refresh the newly selected or deselected
7369 // area to the right of the old or new selection.
ca65c044 7370 need_refresh[2] = true;
2f024384 7371 rect[2] = BlockToDeviceRect(
a9339fe2
DS
7372 wxGridCellCoords( oldTop, rightCol + 1 ),
7373 wxGridCellCoords( oldBottom, oldRight ) );
c9097836
MB
7374 }
7375
7376 if ( oldBottom > bottomRow )
7377 {
3ed884a0
SN
7378 // Refresh the newly selected or deselected
7379 // area below the old or new selection.
ca65c044 7380 need_refresh[3] = true;
2f024384 7381 rect[3] = BlockToDeviceRect(
a9339fe2
DS
7382 wxGridCellCoords( bottomRow + 1, leftCol ),
7383 wxGridCellCoords( oldBottom, rightCol ) );
c9097836
MB
7384 }
7385
c9097836
MB
7386 // various Refresh() calls
7387 for (i = 0; i < 4; i++ )
7388 if ( need_refresh[i] && rect[i] != wxGridNoCellRect )
ca65c044 7389 m_gridWin->Refresh( false, &(rect[i]) );
c9097836 7390 }
2f024384
DS
7391
7392 // change selection
3ed884a0
SN
7393 m_selectingTopLeft = updateTopLeft;
7394 m_selectingBottomRight = updateBottomRight;
c9097836
MB
7395}
7396
2d66e025
MB
7397//
7398// ------ functions to get/send data (see also public functions)
7399//
7400
7401bool wxGrid::GetModelValues()
66242c80 7402{
bca7bfc8
SN
7403 // Hide the editor, so it won't hide a changed value.
7404 HideCellEditControl();
c6707d16 7405
2d66e025 7406 if ( m_table )
66242c80 7407 {
2d66e025 7408 // all we need to do is repaint the grid
66242c80 7409 //
2d66e025 7410 m_gridWin->Refresh();
ca65c044 7411 return true;
66242c80 7412 }
8f177c8e 7413
ca65c044 7414 return false;
66242c80
MB
7415}
7416
2d66e025 7417bool wxGrid::SetModelValues()
f85afd4e 7418{
2d66e025 7419 int row, col;
8f177c8e 7420
c6707d16
SN
7421 // Disable the editor, so it won't hide a changed value.
7422 // Do we also want to save the current value of the editor first?
7423 // I think so ...
c6707d16
SN
7424 DisableCellEditControl();
7425
2d66e025
MB
7426 if ( m_table )
7427 {
56b6cf26 7428 for ( row = 0; row < m_numRows; row++ )
f85afd4e 7429 {
56b6cf26 7430 for ( col = 0; col < m_numCols; col++ )
f85afd4e 7431 {
2d66e025 7432 m_table->SetValue( row, col, GetCellValue(row, col) );
f85afd4e
MB
7433 }
7434 }
8f177c8e 7435
ca65c044 7436 return true;
f85afd4e
MB
7437 }
7438
ca65c044 7439 return false;
f85afd4e
MB
7440}
7441
2d66e025
MB
7442// Note - this function only draws cells that are in the list of
7443// exposed cells (usually set from the update region by
7444// CalcExposedCells)
7445//
d10f4bf9 7446void wxGrid::DrawGridCellArea( wxDC& dc, const wxGridCellCoordsArray& cells )
f85afd4e 7447{
4db6714b
KH
7448 if ( !m_numRows || !m_numCols )
7449 return;
60ff3b99 7450
dc1f566f 7451 int i, numCells = cells.GetCount();
27f35b66
SN
7452 int row, col, cell_rows, cell_cols;
7453 wxGridCellCoordsArray redrawCells;
60ff3b99 7454
a9339fe2 7455 for ( i = numCells - 1; i >= 0; i-- )
f85afd4e 7456 {
27f35b66
SN
7457 row = cells[i].GetRow();
7458 col = cells[i].GetCol();
7459 GetCellSize( row, col, &cell_rows, &cell_cols );
7460
7461 // If this cell is part of a multicell block, find owner for repaint
7462 if ( cell_rows <= 0 || cell_cols <= 0 )
7463 {
a9339fe2 7464 wxGridCellCoords cell( row + cell_rows, col + cell_cols );
ca65c044 7465 bool marked = false;
56b6cf26 7466 for ( int j = 0; j < numCells; j++ )
27f35b66
SN
7467 {
7468 if ( cell == cells[j] )
7469 {
ca65c044 7470 marked = true;
3ed884a0 7471 break;
27f35b66
SN
7472 }
7473 }
2f024384 7474
27f35b66
SN
7475 if (!marked)
7476 {
7477 int count = redrawCells.GetCount();
dc1f566f 7478 for (int j = 0; j < count; j++)
27f35b66
SN
7479 {
7480 if ( cell == redrawCells[j] )
7481 {
ca65c044 7482 marked = true;
27f35b66
SN
7483 break;
7484 }
7485 }
2f024384 7486
4db6714b
KH
7487 if (!marked)
7488 redrawCells.Add( cell );
27f35b66 7489 }
2f024384
DS
7490
7491 // don't bother drawing this cell
7492 continue;
27f35b66
SN
7493 }
7494
7495 // If this cell is empty, find cell to left that might want to overflow
7496 if (m_table && m_table->IsEmptyCell(row, col))
7497 {
dc1f566f 7498 for ( int l = 0; l < cell_rows; l++ )
27f35b66 7499 {
56b6cf26 7500 // find a cell in this row to leave already marked for repaint
dc1f566f
SN
7501 int left = col;
7502 for (int k = 0; k < int(redrawCells.GetCount()); k++)
7503 if ((redrawCells[k].GetCol() < left) &&
7504 (redrawCells[k].GetRow() == row))
2f024384 7505 {
4db6714b 7506 left = redrawCells[k].GetCol();
2f024384 7507 }
dc1f566f 7508
4db6714b
KH
7509 if (left == col)
7510 left = 0; // oh well
dc1f566f 7511
2f024384 7512 for (int j = col - 1; j >= left; j--)
27f35b66 7513 {
2f024384 7514 if (!m_table->IsEmptyCell(row + l, j))
27f35b66 7515 {
2f024384 7516 if (GetCellOverflow(row + l, j))
27f35b66 7517 {
2f024384 7518 wxGridCellCoords cell(row + l, j);
ca65c044 7519 bool marked = false;
27f35b66 7520
dc1f566f 7521 for (int k = 0; k < numCells; k++)
27f35b66
SN
7522 {
7523 if ( cell == cells[k] )
7524 {
ca65c044 7525 marked = true;
27f35b66
SN
7526 break;
7527 }
7528 }
4db6714b 7529
27f35b66
SN
7530 if (!marked)
7531 {
7532 int count = redrawCells.GetCount();
dc1f566f 7533 for (int k = 0; k < count; k++)
27f35b66
SN
7534 {
7535 if ( cell == redrawCells[k] )
7536 {
ca65c044 7537 marked = true;
27f35b66
SN
7538 break;
7539 }
7540 }
4db6714b
KH
7541 if (!marked)
7542 redrawCells.Add( cell );
27f35b66
SN
7543 }
7544 }
7545 break;
7546 }
7547 }
7548 }
7549 }
4db6714b 7550
d10f4bf9 7551 DrawCell( dc, cells[i] );
2d66e025 7552 }
27f35b66
SN
7553
7554 numCells = redrawCells.GetCount();
7555
56b6cf26 7556 for ( i = numCells - 1; i >= 0; i-- )
27f35b66
SN
7557 {
7558 DrawCell( dc, redrawCells[i] );
7559 }
2d66e025 7560}
8f177c8e 7561
7c8a8ad5
MB
7562void wxGrid::DrawGridSpace( wxDC& dc )
7563{
f6bcfd97
BP
7564 int cw, ch;
7565 m_gridWin->GetClientSize( &cw, &ch );
7c8a8ad5 7566
f6bcfd97
BP
7567 int right, bottom;
7568 CalcUnscrolledPosition( cw, ch, &right, &bottom );
7c8a8ad5 7569
d4175745 7570 int rightCol = m_numCols > 0 ? GetColRight(GetColAt( m_numCols - 1 )) : 0;
2f024384 7571 int bottomRow = m_numRows > 0 ? GetRowBottom(m_numRows - 1) : 0;
7c8a8ad5 7572
f6bcfd97
BP
7573 if ( right > rightCol || bottom > bottomRow )
7574 {
7575 int left, top;
7576 CalcUnscrolledPosition( 0, 0, &left, &top );
7c8a8ad5 7577
f6bcfd97
BP
7578 dc.SetBrush( wxBrush(GetDefaultCellBackgroundColour(), wxSOLID) );
7579 dc.SetPen( *wxTRANSPARENT_PEN );
7c8a8ad5 7580
f6bcfd97
BP
7581 if ( right > rightCol )
7582 {
a9339fe2 7583 dc.DrawRectangle( rightCol, top, right - rightCol, ch );
f6bcfd97
BP
7584 }
7585
7586 if ( bottom > bottomRow )
7587 {
a9339fe2 7588 dc.DrawRectangle( left, bottomRow, cw, bottom - bottomRow );
f6bcfd97
BP
7589 }
7590 }
7c8a8ad5
MB
7591}
7592
2d66e025
MB
7593void wxGrid::DrawCell( wxDC& dc, const wxGridCellCoords& coords )
7594{
ab79958a
VZ
7595 int row = coords.GetRow();
7596 int col = coords.GetCol();
60ff3b99 7597
7c1cb261 7598 if ( GetColWidth(col) <= 0 || GetRowHeight(row) <= 0 )
ab79958a
VZ
7599 return;
7600
7601 // we draw the cell border ourselves
9496deb5 7602#if !WXGRID_DRAW_LINES
2d66e025
MB
7603 if ( m_gridLinesEnabled )
7604 DrawCellBorder( dc, coords );
796df70a 7605#endif
f85afd4e 7606
189d0213
VZ
7607 wxGridCellAttr* attr = GetCellAttr(row, col);
7608
7609 bool isCurrent = coords == m_currentCellCoords;
508011ce 7610
f6bcfd97 7611 wxRect rect = CellToRect( row, col );
f85afd4e 7612
189d0213 7613 // if the editor is shown, we should use it and not the renderer
f6bcfd97
BP
7614 // Note: However, only if it is really _shown_, i.e. not hidden!
7615 if ( isCurrent && IsCellEditControlShown() )
189d0213 7616 {
a9339fe2 7617 // NB: this "#if..." is temporary and fixes a problem where the
962a48f6
DS
7618 // edit control is erased by this code after being rendered.
7619 // On wxMac (QD build only), the cell editor is a wxTextCntl and is rendered
7620 // implicitly, causing this out-of order render.
5d38a5f3 7621#if !defined(__WXMAC__)
0b190b0f
VZ
7622 wxGridCellEditor *editor = attr->GetEditor(this, row, col);
7623 editor->PaintBackground(rect, attr);
7624 editor->DecRef();
962a48f6 7625#endif
189d0213
VZ
7626 }
7627 else
7628 {
a9339fe2 7629 // but all the rest is drawn by the cell renderer and hence may be customized
0b190b0f
VZ
7630 wxGridCellRenderer *renderer = attr->GetRenderer(this, row, col);
7631 renderer->Draw(*this, *attr, dc, rect, row, col, IsInSelection(coords));
7632 renderer->DecRef();
189d0213 7633 }
07296f0b 7634
283b7808
VZ
7635 attr->DecRef();
7636}
07296f0b 7637
283b7808 7638void wxGrid::DrawCellHighlight( wxDC& dc, const wxGridCellAttr *attr )
07296f0b
RD
7639{
7640 int row = m_currentCellCoords.GetRow();
7641 int col = m_currentCellCoords.GetCol();
7642
7c1cb261 7643 if ( GetColWidth(col) <= 0 || GetRowHeight(row) <= 0 )
07296f0b
RD
7644 return;
7645
f6bcfd97 7646 wxRect rect = CellToRect(row, col);
07296f0b 7647
b3a7510d
VZ
7648 // hmmm... what could we do here to show that the cell is disabled?
7649 // for now, I just draw a thinner border than for the other ones, but
7650 // it doesn't look really good
b3a7510d 7651
d2520c85
RD
7652 int penWidth = attr->IsReadOnly() ? m_cellHighlightROPenWidth : m_cellHighlightPenWidth;
7653
27f35b66
SN
7654 if (penWidth > 0)
7655 {
56b6cf26
DS
7656 // The center of the drawn line is where the position/width/height of
7657 // the rectangle is actually at (on wxMSW at least), so the
7658 // size of the rectangle is reduced to compensate for the thickness of
7659 // the line. If this is too strange on non-wxMSW platforms then
d2520c85 7660 // please #ifdef this appropriately.
4db6714b
KH
7661 rect.x += penWidth / 2;
7662 rect.y += penWidth / 2;
7663 rect.width -= penWidth - 1;
7664 rect.height -= penWidth - 1;
d2520c85 7665
d2520c85 7666 // Now draw the rectangle
73145b0e
JS
7667 // use the cellHighlightColour if the cell is inside a selection, this
7668 // will ensure the cell is always visible.
4db6714b 7669 dc.SetPen(wxPen(IsInSelection(row,col) ? m_selectionForeground : m_cellHighlightColour, penWidth, wxSOLID));
d2520c85
RD
7670 dc.SetBrush(*wxTRANSPARENT_BRUSH);
7671 dc.DrawRectangle(rect);
7672 }
07296f0b 7673
283b7808 7674#if 0
2f024384 7675 // VZ: my experiments with 3D borders...
283b7808 7676
b3a7510d 7677 // how to properly set colours for arbitrary bg?
283b7808
VZ
7678 wxCoord x1 = rect.x,
7679 y1 = rect.y,
4db6714b
KH
7680 x2 = rect.x + rect.width - 1,
7681 y2 = rect.y + rect.height - 1;
283b7808
VZ
7682
7683 dc.SetPen(*wxWHITE_PEN);
00cf1208
RR
7684 dc.DrawLine(x1, y1, x2, y1);
7685 dc.DrawLine(x1, y1, x1, y2);
283b7808 7686
283b7808 7687 dc.DrawLine(x1 + 1, y2 - 1, x2 - 1, y2 - 1);
2f024384 7688 dc.DrawLine(x2 - 1, y1 + 1, x2 - 1, y2);
283b7808
VZ
7689
7690 dc.SetPen(*wxBLACK_PEN);
7691 dc.DrawLine(x1, y2, x2, y2);
2f024384 7692 dc.DrawLine(x2, y1, x2, y2 + 1);
a9339fe2 7693#endif
2d66e025 7694}
f85afd4e 7695
3d3f3e37
VZ
7696wxPen wxGrid::GetDefaultGridLinePen()
7697{
7698 return wxPen(GetGridLineColour(), 1, wxSOLID);
7699}
7700
7701wxPen wxGrid::GetRowGridLinePen(int WXUNUSED(row))
7702{
7703 return GetDefaultGridLinePen();
7704}
7705
7706wxPen wxGrid::GetColGridLinePen(int WXUNUSED(col))
7707{
7708 return GetDefaultGridLinePen();
7709}
7710
2d66e025
MB
7711void wxGrid::DrawCellBorder( wxDC& dc, const wxGridCellCoords& coords )
7712{
2d66e025
MB
7713 int row = coords.GetRow();
7714 int col = coords.GetCol();
7c1cb261
VZ
7715 if ( GetColWidth(col) <= 0 || GetRowHeight(row) <= 0 )
7716 return;
7717
60ff3b99 7718
27f35b66
SN
7719 wxRect rect = CellToRect( row, col );
7720
2d66e025 7721 // right hand border
3d3f3e37 7722 dc.SetPen( GetColGridLinePen(col) );
27f35b66
SN
7723 dc.DrawLine( rect.x + rect.width, rect.y,
7724 rect.x + rect.width, rect.y + rect.height + 1 );
2d66e025
MB
7725
7726 // bottom border
3d3f3e37 7727 dc.SetPen( GetRowGridLinePen(row) );
2f024384 7728 dc.DrawLine( rect.x, rect.y + rect.height,
27f35b66 7729 rect.x + rect.width, rect.y + rect.height);
f85afd4e
MB
7730}
7731
2f024384 7732void wxGrid::DrawHighlight(wxDC& dc, const wxGridCellCoordsArray& cells)
b3a7510d 7733{
2a7750d9
MB
7734 // This if block was previously in wxGrid::OnPaint but that doesn't
7735 // seem to get called under wxGTK - MB
7736 //
2f024384 7737 if ( m_currentCellCoords == wxGridNoCellCoords &&
2a7750d9
MB
7738 m_numRows && m_numCols )
7739 {
7740 m_currentCellCoords.Set(0, 0);
7741 }
7742
f6bcfd97 7743 if ( IsCellEditControlShown() )
99306db2
VZ
7744 {
7745 // don't show highlight when the edit control is shown
7746 return;
7747 }
7748
b3a7510d
VZ
7749 // if the active cell was repainted, repaint its highlight too because it
7750 // might have been damaged by the grid lines
d10f4bf9 7751 size_t count = cells.GetCount();
b3a7510d
VZ
7752 for ( size_t n = 0; n < count; n++ )
7753 {
d10f4bf9 7754 if ( cells[n] == m_currentCellCoords )
b3a7510d
VZ
7755 {
7756 wxGridCellAttr* attr = GetCellAttr(m_currentCellCoords);
7757 DrawCellHighlight(dc, attr);
7758 attr->DecRef();
7759
7760 break;
7761 }
7762 }
7763}
2d66e025 7764
2d66e025
MB
7765// TODO: remove this ???
7766// This is used to redraw all grid lines e.g. when the grid line colour
7767// has been changed
7768//
33ac7e6f 7769void wxGrid::DrawAllGridLines( wxDC& dc, const wxRegion & WXUNUSED(reg) )
f85afd4e 7770{
27f35b66
SN
7771#if !WXGRID_DRAW_LINES
7772 return;
7773#endif
7774
afd0f084 7775 if ( !m_gridLinesEnabled || !m_numRows || !m_numCols )
4db6714b 7776 return;
f85afd4e 7777
2d66e025 7778 int top, bottom, left, right;
796df70a 7779
f6bcfd97 7780#if 0 //#ifndef __WXGTK__
508011ce
VZ
7781 if (reg.IsEmpty())
7782 {
796df70a
SN
7783 int cw, ch;
7784 m_gridWin->GetClientSize(&cw, &ch);
7785
7786 // virtual coords of visible area
7787 //
7788 CalcUnscrolledPosition( 0, 0, &left, &top );
7789 CalcUnscrolledPosition( cw, ch, &right, &bottom );
7790 }
508011ce
VZ
7791 else
7792 {
796df70a
SN
7793 wxCoord x, y, w, h;
7794 reg.GetBox(x, y, w, h);
7795 CalcUnscrolledPosition( x, y, &left, &top );
7796 CalcUnscrolledPosition( x + w, y + h, &right, &bottom );
7797 }
8e217128
RR
7798#else
7799 int cw, ch;
7800 m_gridWin->GetClientSize(&cw, &ch);
7801 CalcUnscrolledPosition( 0, 0, &left, &top );
7802 CalcUnscrolledPosition( cw, ch, &right, &bottom );
7803#endif
f85afd4e 7804
9496deb5
MB
7805 // avoid drawing grid lines past the last row and col
7806 //
d4175745 7807 right = wxMin( right, GetColRight(GetColAt( m_numCols - 1 )) );
7c1cb261 7808 bottom = wxMin( bottom, GetRowBottom(m_numRows - 1) );
9496deb5 7809
27f35b66 7810 // no gridlines inside multicells, clip them out
d4175745 7811 int leftCol = GetColPos( internalXToCol(left) );
a9339fe2 7812 int topRow = internalYToRow(top);
d4175745 7813 int rightCol = GetColPos( internalXToCol(right) );
a967f048 7814 int bottomRow = internalYToRow(bottom);
27f35b66 7815
c03bf0c7 7816 wxRegion clippedcells(0, 0, cw, ch);
27f35b66 7817
a967f048
RG
7818 int i, j, cell_rows, cell_cols;
7819 wxRect rect;
27f35b66 7820
7c3f33a9 7821 for (j=topRow; j<=bottomRow; j++)
a967f048 7822 {
d4175745 7823 int colPos;
7c3f33a9 7824 for (colPos=leftCol; colPos<=rightCol; colPos++)
27f35b66 7825 {
d4175745
VZ
7826 i = GetColAt( colPos );
7827
a967f048
RG
7828 GetCellSize( j, i, &cell_rows, &cell_cols );
7829 if ((cell_rows > 1) || (cell_cols > 1))
27f35b66 7830 {
a967f048
RG
7831 rect = CellToRect(j,i);
7832 CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
7833 clippedcells.Subtract(rect);
7834 }
7835 else if ((cell_rows < 0) || (cell_cols < 0))
7836 {
a9339fe2 7837 rect = CellToRect(j + cell_rows, i + cell_cols);
a967f048
RG
7838 CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
7839 clippedcells.Subtract(rect);
27f35b66
SN
7840 }
7841 }
7842 }
a9339fe2 7843
27f35b66
SN
7844 dc.SetClippingRegion( clippedcells );
7845
f85afd4e 7846
2d66e025 7847 // horizontal grid lines
f85afd4e 7848 //
a967f048 7849 // already declared above - int i;
33188aa4 7850 for ( i = internalYToRow(top); i < m_numRows; i++ )
f85afd4e 7851 {
6d55126d 7852 int bot = GetRowBottom(i) - 1;
7c1cb261 7853
6d55126d 7854 if ( bot > bottom )
2d66e025
MB
7855 {
7856 break;
7857 }
7c1cb261 7858
6d55126d 7859 if ( bot >= top )
2d66e025 7860 {
3d3f3e37 7861 dc.SetPen( GetRowGridLinePen(i) );
6d55126d 7862 dc.DrawLine( left, bot, right, bot );
2d66e025 7863 }
f85afd4e
MB
7864 }
7865
2d66e025
MB
7866 // vertical grid lines
7867 //
d4175745
VZ
7868 int colPos;
7869 for ( colPos = leftCol; colPos < m_numCols; colPos++ )
f85afd4e 7870 {
d4175745
VZ
7871 i = GetColAt( colPos );
7872
2121eb69
RR
7873 int colRight = GetColRight(i);
7874#ifdef __WXGTK__
7875 if (GetLayoutDirection() != wxLayout_RightToLeft)
7876#endif
7877 colRight--;
7878
7c1cb261 7879 if ( colRight > right )
2d66e025
MB
7880 {
7881 break;
7882 }
7c1cb261
VZ
7883
7884 if ( colRight >= left )
2d66e025 7885 {
3d3f3e37 7886 dc.SetPen( GetColGridLinePen(i) );
7c1cb261 7887 dc.DrawLine( colRight, top, colRight, bottom );
2d66e025
MB
7888 }
7889 }
a9339fe2 7890
27f35b66 7891 dc.DestroyClippingRegion();
2d66e025 7892}
f85afd4e 7893
c2f5b920 7894void wxGrid::DrawRowLabels( wxDC& dc, const wxArrayInt& rows)
2d66e025 7895{
4db6714b
KH
7896 if ( !m_numRows )
7897 return;
60ff3b99 7898
2d66e025 7899 size_t i;
d10f4bf9 7900 size_t numLabels = rows.GetCount();
60ff3b99 7901
2f024384 7902 for ( i = 0; i < numLabels; i++ )
2d66e025 7903 {
d10f4bf9 7904 DrawRowLabel( dc, rows[i] );
60ff3b99 7905 }
f85afd4e
MB
7906}
7907
2d66e025 7908void wxGrid::DrawRowLabel( wxDC& dc, int row )
f85afd4e 7909{
659af826 7910 if ( GetRowHeight(row) <= 0 || m_rowLabelWidth <= 0 )
7c1cb261 7911 return;
60ff3b99 7912
4d1bc39c 7913 wxRect rect;
2f024384 7914
e6002250
RR
7915#if 0
7916def __WXGTK20__
4d1bc39c
RR
7917 rect.SetX( 1 );
7918 rect.SetY( GetRowTop(row) + 1 );
7919 rect.SetWidth( m_rowLabelWidth - 2 );
7920 rect.SetHeight( GetRowHeight(row) - 2 );
ec157c8f 7921
4d1bc39c 7922 CalcScrolledPosition( 0, rect.y, NULL, &rect.y );
ec157c8f 7923
4d1bc39c
RR
7924 wxWindowDC *win_dc = (wxWindowDC*) &dc;
7925
7926 wxRendererNative::Get().DrawHeaderButton( win_dc->m_owner, dc, rect, 0 );
7927#else
7c1cb261
VZ
7928 int rowTop = GetRowTop(row),
7929 rowBottom = GetRowBottom(row) - 1;
b99be8fb 7930
d4175745 7931 dc.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW), 1, wxSOLID) );
a9339fe2 7932 dc.DrawLine( m_rowLabelWidth - 1, rowTop, m_rowLabelWidth - 1, rowBottom );
73145b0e 7933 dc.DrawLine( 0, rowTop, 0, rowBottom );
73145b0e 7934 dc.DrawLine( 0, rowBottom, m_rowLabelWidth, rowBottom );
b99be8fb 7935
2d66e025 7936 dc.SetPen( *wxWHITE_PEN );
73145b0e 7937 dc.DrawLine( 1, rowTop, 1, rowBottom );
a9339fe2 7938 dc.DrawLine( 1, rowTop, m_rowLabelWidth - 1, rowTop );
4d1bc39c 7939#endif
2f024384 7940
f85afd4e 7941 dc.SetBackgroundMode( wxTRANSPARENT );
f85afd4e
MB
7942 dc.SetTextForeground( GetLabelTextColour() );
7943 dc.SetFont( GetLabelFont() );
8f177c8e 7944
f85afd4e 7945 int hAlign, vAlign;
2d66e025 7946 GetRowLabelAlignment( &hAlign, &vAlign );
60ff3b99 7947
2d66e025 7948 rect.SetX( 2 );
7c1cb261 7949 rect.SetY( GetRowTop(row) + 2 );
2d66e025 7950 rect.SetWidth( m_rowLabelWidth - 4 );
7c1cb261 7951 rect.SetHeight( GetRowHeight(row) - 4 );
60ff3b99 7952 DrawTextRectangle( dc, GetRowLabelValue( row ), rect, hAlign, vAlign );
f85afd4e
MB
7953}
7954
d10f4bf9 7955void wxGrid::DrawColLabels( wxDC& dc,const wxArrayInt& cols )
f85afd4e 7956{
4db6714b
KH
7957 if ( !m_numCols )
7958 return;
60ff3b99 7959
2d66e025 7960 size_t i;
d10f4bf9 7961 size_t numLabels = cols.GetCount();
60ff3b99 7962
56b6cf26 7963 for ( i = 0; i < numLabels; i++ )
f85afd4e 7964 {
d10f4bf9 7965 DrawColLabel( dc, cols[i] );
60ff3b99 7966 }
f85afd4e
MB
7967}
7968
2d66e025 7969void wxGrid::DrawColLabel( wxDC& dc, int col )
f85afd4e 7970{
659af826 7971 if ( GetColWidth(col) <= 0 || m_colLabelHeight <= 0 )
7c1cb261 7972 return;
60ff3b99 7973
4d1bc39c 7974 int colLeft = GetColLeft(col);
ec157c8f 7975
4d1bc39c 7976 wxRect rect;
2f024384 7977
e6002250
RR
7978#if 0
7979def __WXGTK20__
4d1bc39c
RR
7980 rect.SetX( colLeft + 1 );
7981 rect.SetY( 1 );
7982 rect.SetWidth( GetColWidth(col) - 2 );
7983 rect.SetHeight( m_colLabelHeight - 2 );
ec157c8f 7984
4d1bc39c
RR
7985 wxWindowDC *win_dc = (wxWindowDC*) &dc;
7986
7987 wxRendererNative::Get().DrawHeaderButton( win_dc->m_owner, dc, rect, 0 );
7988#else
7989 int colRight = GetColRight(col) - 1;
b99be8fb 7990
d4175745 7991 dc.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW), 1, wxSOLID) );
ccdee36f 7992 dc.DrawLine( colRight, 0, colRight, m_colLabelHeight - 1 );
73145b0e 7993 dc.DrawLine( colLeft, 0, colRight, 0 );
ccdee36f
DS
7994 dc.DrawLine( colLeft, m_colLabelHeight - 1,
7995 colRight + 1, m_colLabelHeight - 1 );
b99be8fb 7996
f85afd4e 7997 dc.SetPen( *wxWHITE_PEN );
ccdee36f 7998 dc.DrawLine( colLeft, 1, colLeft, m_colLabelHeight - 1 );
73145b0e 7999 dc.DrawLine( colLeft, 1, colRight, 1 );
4d1bc39c 8000#endif
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