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