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