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