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