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