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