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