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