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