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