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