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