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