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