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