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