]> git.saurik.com Git - wxWidgets.git/blame - src/generic/grid.cpp
correct i18n problems in accel handling code (replaces patch 1465417; closes bug...
[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 {
191 m_owner = (wxGrid *)NULL;
192 m_rowLabelWin = (wxGridRowLabelWindow *)NULL;
193 m_colLabelWin = (wxGridColLabelWindow *)NULL;
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;
803 if ( !params.ToLong(&tmp) )
804 {
a9339fe2 805 wxLogDebug( _T("Invalid wxGridCellTextEditor parameter string '%s' ignored"), params.c_str() );
c4608a8a
VZ
806 }
807 else
808 {
809 m_maxChars = (size_t)tmp;
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));
816be743
VZ
853#endif // wxUSE_VALIDATORS
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));
816be743
VZ
1046#endif // wxUSE_VALIDATORS
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,
1312 wxT("The wxGridCellEditor must be Created first!"));
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,
1332 wxT("The wxGridCellEditor must be Created first!"));
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,
1353 wxT("The wxGridCellEditor must be Created first!"));
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
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...
1465 wxGridCellEditor::PaintBackground(rectCell, attr);
4ee5fc9c
VZ
1466}
1467
1468void wxGridCellChoiceEditor::BeginEdit(int row, int col, wxGrid* grid)
1469{
1470 wxASSERT_MSG(m_control,
1471 wxT("The wxGridCellEditor must be Created first!"));
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 }
1643 case WXK_END:
1644 {
2f024384 1645 if ( wholeCellVisible )
7db713ae
JS
1646 {
1647 // no special processing needed...
1648 event.Skip();
1649 break;
1650 }
1651
1652 // do special processing for partly visible cell...
1653
1654 int textWidth = 0;
1655 wxString value = m_grid->GetCellValue(row, col);
1656 if ( wxEmptyString != value )
1657 {
1658 // get width of cell CONTENTS (text)
1659 int y;
1660 wxFont font = m_grid->GetCellFont(row, col);
1661 m_grid->GetTextExtent(value, &textWidth, &y, NULL, NULL, &font);
2f024384 1662
7db713ae
JS
1663 // try to RIGHT align the text by scrolling
1664 int client_right = m_grid->GetGridWindow()->GetClientSize().GetWidth();
2f024384 1665
7db713ae
JS
1666 // (m_grid->GetScrollLineX()*2) is a factor for not scrolling to far,
1667 // otherwise the last part of the cell content might be hidden below the scroll bar
1668 // FIXME: maybe there is a more suitable correction?
2f024384 1669 textWidth -= (client_right - (m_grid->GetScrollLineX() * 2));
7db713ae
JS
1670 if ( textWidth < 0 )
1671 {
1672 textWidth = 0;
1673 }
1674 }
1675
1676 // get the widths of all cells previous to this one
1677 int colXPos = 0;
faa94f3e 1678 for ( int i = 0; i < col; i++ )
7db713ae
JS
1679 {
1680 colXPos += m_grid->GetColSize(i);
1681 }
2f024384 1682
7db713ae
JS
1683 // and add the (modified) text width of the cell contents
1684 // as we'd like to see the last part of the cell contents
1685 colXPos += textWidth;
1686
1687 int xUnit = 1, yUnit = 1;
1688 m_grid->GetScrollPixelsPerUnit(&xUnit, &yUnit);
ccdee36f 1689 m_grid->Scroll(colXPos / xUnit - 1, m_grid->GetScrollPos(wxVERTICAL));
7db713ae
JS
1690 event.Skip();
1691 break;
1692 }
1693
fb0de762
RD
1694 default:
1695 event.Skip();
1696 }
1697}
1698
c4608a8a
VZ
1699// ----------------------------------------------------------------------------
1700// wxGridCellWorker is an (almost) empty common base class for
1701// wxGridCellRenderer and wxGridCellEditor managing ref counting
1702// ----------------------------------------------------------------------------
1703
1704void wxGridCellWorker::SetParameters(const wxString& WXUNUSED(params))
1705{
1706 // nothing to do
1707}
1708
1709wxGridCellWorker::~wxGridCellWorker()
1710{
1711}
1712
508011ce
VZ
1713// ============================================================================
1714// renderer classes
1715// ============================================================================
1716
ab79958a
VZ
1717// ----------------------------------------------------------------------------
1718// wxGridCellRenderer
1719// ----------------------------------------------------------------------------
1720
1721void wxGridCellRenderer::Draw(wxGrid& grid,
2796cce3 1722 wxGridCellAttr& attr,
ab79958a
VZ
1723 wxDC& dc,
1724 const wxRect& rect,
c78b3acd 1725 int WXUNUSED(row), int WXUNUSED(col),
ab79958a
VZ
1726 bool isSelected)
1727{
1728 dc.SetBackgroundMode( wxSOLID );
1729
04418332 1730 // grey out fields if the grid is disabled
4db6714b 1731 if ( grid.IsEnabled() )
ab79958a 1732 {
ec157c8f
WS
1733 if ( isSelected )
1734 {
1735 dc.SetBrush( wxBrush(grid.GetSelectionBackground(), wxSOLID) );
1736 }
1737 else
1738 {
1739 dc.SetBrush( wxBrush(attr.GetBackgroundColour(), wxSOLID) );
1740 }
52d6f640 1741 }
ab79958a
VZ
1742 else
1743 {
ec157c8f 1744 dc.SetBrush(wxBrush(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE), wxSOLID));
ab79958a
VZ
1745 }
1746
1747 dc.SetPen( *wxTRANSPARENT_PEN );
ab79958a
VZ
1748 dc.DrawRectangle(rect);
1749}
1750
508011ce
VZ
1751// ----------------------------------------------------------------------------
1752// wxGridCellStringRenderer
1753// ----------------------------------------------------------------------------
1754
fbfb8bcc
VZ
1755void wxGridCellStringRenderer::SetTextColoursAndFont(const wxGrid& grid,
1756 const wxGridCellAttr& attr,
816be743
VZ
1757 wxDC& dc,
1758 bool isSelected)
ab79958a 1759{
ab79958a
VZ
1760 dc.SetBackgroundMode( wxTRANSPARENT );
1761
283b7808
VZ
1762 // TODO some special colours for attr.IsReadOnly() case?
1763
04418332 1764 // different coloured text when the grid is disabled
4db6714b 1765 if ( grid.IsEnabled() )
ab79958a 1766 {
73145b0e
JS
1767 if ( isSelected )
1768 {
1769 dc.SetTextBackground( grid.GetSelectionBackground() );
1770 dc.SetTextForeground( grid.GetSelectionForeground() );
1771 }
1772 else
1773 {
1774 dc.SetTextBackground( attr.GetBackgroundColour() );
1775 dc.SetTextForeground( attr.GetTextColour() );
1776 }
ab79958a
VZ
1777 }
1778 else
1779 {
ec157c8f
WS
1780 dc.SetTextBackground(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE));
1781 dc.SetTextForeground(wxSystemSettings::GetColour(wxSYS_COLOUR_GRAYTEXT));
ab79958a 1782 }
816be743 1783
2796cce3 1784 dc.SetFont( attr.GetFont() );
816be743
VZ
1785}
1786
fbfb8bcc 1787wxSize wxGridCellStringRenderer::DoGetBestSize(const wxGridCellAttr& attr,
65e4e78e
VZ
1788 wxDC& dc,
1789 const wxString& text)
1790{
f6bcfd97 1791 wxCoord x = 0, y = 0, max_x = 0;
65e4e78e 1792 dc.SetFont(attr.GetFont());
f6bcfd97
BP
1793 wxStringTokenizer tk(text, _T('\n'));
1794 while ( tk.HasMoreTokens() )
1795 {
1796 dc.GetTextExtent(tk.GetNextToken(), &x, &y);
1797 max_x = wxMax(max_x, x);
1798 }
1799
1800 y *= 1 + text.Freq(wxT('\n')); // multiply by the number of lines.
65e4e78e 1801
f6bcfd97 1802 return wxSize(max_x, y);
65e4e78e
VZ
1803}
1804
1805wxSize wxGridCellStringRenderer::GetBestSize(wxGrid& grid,
1806 wxGridCellAttr& attr,
1807 wxDC& dc,
1808 int row, int col)
1809{
1810 return DoGetBestSize(attr, dc, grid.GetCellValue(row, col));
1811}
1812
816be743
VZ
1813void wxGridCellStringRenderer::Draw(wxGrid& grid,
1814 wxGridCellAttr& attr,
1815 wxDC& dc,
1816 const wxRect& rectCell,
1817 int row, int col,
1818 bool isSelected)
1819{
27f35b66 1820 wxRect rect = rectCell;
dc1f566f
SN
1821 rect.Inflate(-1);
1822
1823 // erase only this cells background, overflow cells should have been erased
1824 wxGridCellRenderer::Draw(grid, attr, dc, rectCell, row, col, isSelected);
1825
1826 int hAlign, vAlign;
1827 attr.GetAlignment(&hAlign, &vAlign);
27f35b66 1828
39b80349
SN
1829 int overflowCols = 0;
1830
27f35b66
SN
1831 if (attr.GetOverflow())
1832 {
1833 int cols = grid.GetNumberCols();
1834 int best_width = GetBestSize(grid,attr,dc,row,col).GetWidth();
1835 int cell_rows, cell_cols;
2f024384 1836 attr.GetSize( &cell_rows, &cell_cols ); // shouldn't get here if <= 0
27f35b66
SN
1837 if ((best_width > rectCell.width) && (col < cols) && grid.GetTable())
1838 {
1839 int i, c_cols, c_rows;
1840 for (i = col+cell_cols; i < cols; i++)
1841 {
ca65c044 1842 bool is_empty = true;
2f024384 1843 for (int j=row; j < row + cell_rows; j++)
27f35b66 1844 {
39b80349 1845 // check w/ anchor cell for multicell block
ef4d6ce8 1846 grid.GetCellSize(j, i, &c_rows, &c_cols);
2f024384
DS
1847 if (c_rows > 0)
1848 c_rows = 0;
1849 if (!grid.GetTable()->IsEmptyCell(j + c_rows, i))
39b80349 1850 {
ca65c044 1851 is_empty = false;
ef4d6ce8 1852 break;
39b80349 1853 }
27f35b66 1854 }
2f024384 1855
39b80349
SN
1856 if (is_empty)
1857 rect.width += grid.GetColSize(i);
dc1f566f
SN
1858 else
1859 {
1860 i--;
1861 break;
1862 }
2f024384 1863
4db6714b
KH
1864 if (rect.width >= best_width)
1865 break;
2b5f62a0 1866 }
2f024384 1867
39b80349 1868 overflowCols = i - col - cell_cols + 1;
4db6714b
KH
1869 if (overflowCols >= cols)
1870 overflowCols = cols - 1;
39b80349 1871 }
27f35b66 1872
dc1f566f
SN
1873 if (overflowCols > 0) // redraw overflow cells w/ proper hilight
1874 {
39b80349 1875 hAlign = wxALIGN_LEFT; // if oveflowed then it's left aligned
2b5f62a0 1876 wxRect clip = rect;
39b80349 1877 clip.x += rectCell.width;
dc1f566f 1878 // draw each overflow cell individually
2f024384 1879 int col_end = col+cell_cols + overflowCols;
dc1f566f 1880 if (col_end >= grid.GetNumberCols())
2b5f62a0 1881 col_end = grid.GetNumberCols() - 1;
dc1f566f
SN
1882 for (int i = col+cell_cols; i <= col_end; i++)
1883 {
dc1f566f
SN
1884 clip.width = grid.GetColSize(i) - 1;
1885 dc.DestroyClippingRegion();
1886 dc.SetClippingRegion(clip);
39b80349
SN
1887
1888 SetTextColoursAndFont(grid, attr, dc,
2b5f62a0 1889 grid.IsInSelection(row,i));
39b80349 1890
dc1f566f 1891 grid.DrawTextRectangle(dc, grid.GetCellValue(row, col),
2b5f62a0 1892 rect, hAlign, vAlign);
39b80349 1893 clip.x += grid.GetColSize(i) - 1;
dc1f566f 1894 }
ab79958a 1895
39b80349 1896 rect = rectCell;
2b5f62a0 1897 rect.Inflate(-1);
dc1f566f 1898 rect.width++;
39b80349 1899 dc.DestroyClippingRegion();
dc1f566f 1900 }
39b80349 1901 }
ab79958a 1902
dc1f566f
SN
1903 // now we only have to draw the text
1904 SetTextColoursAndFont(grid, attr, dc, isSelected);
39b80349 1905
ab79958a
VZ
1906 grid.DrawTextRectangle(dc, grid.GetCellValue(row, col),
1907 rect, hAlign, vAlign);
1908}
1909
65e4e78e
VZ
1910// ----------------------------------------------------------------------------
1911// wxGridCellNumberRenderer
1912// ----------------------------------------------------------------------------
1913
fbfb8bcc 1914wxString wxGridCellNumberRenderer::GetString(const wxGrid& grid, int row, int col)
65e4e78e
VZ
1915{
1916 wxGridTableBase *table = grid.GetTable();
1917 wxString text;
1918 if ( table->CanGetValueAs(row, col, wxGRID_VALUE_NUMBER) )
1919 {
1920 text.Printf(_T("%ld"), table->GetValueAsLong(row, col));
1921 }
9c4ba614
VZ
1922 else
1923 {
1924 text = table->GetValue(row, col);
1925 }
65e4e78e
VZ
1926
1927 return text;
1928}
1929
816be743
VZ
1930void wxGridCellNumberRenderer::Draw(wxGrid& grid,
1931 wxGridCellAttr& attr,
1932 wxDC& dc,
1933 const wxRect& rectCell,
1934 int row, int col,
1935 bool isSelected)
1936{
1937 wxGridCellRenderer::Draw(grid, attr, dc, rectCell, row, col, isSelected);
1938
1939 SetTextColoursAndFont(grid, attr, dc, isSelected);
1940
1941 // draw the text right aligned by default
1942 int hAlign, vAlign;
1943 attr.GetAlignment(&hAlign, &vAlign);
4c7277db 1944 hAlign = wxALIGN_RIGHT;
816be743
VZ
1945
1946 wxRect rect = rectCell;
1947 rect.Inflate(-1);
1948
65e4e78e
VZ
1949 grid.DrawTextRectangle(dc, GetString(grid, row, col), rect, hAlign, vAlign);
1950}
816be743 1951
65e4e78e
VZ
1952wxSize wxGridCellNumberRenderer::GetBestSize(wxGrid& grid,
1953 wxGridCellAttr& attr,
1954 wxDC& dc,
1955 int row, int col)
1956{
1957 return DoGetBestSize(attr, dc, GetString(grid, row, col));
816be743
VZ
1958}
1959
1960// ----------------------------------------------------------------------------
1961// wxGridCellFloatRenderer
1962// ----------------------------------------------------------------------------
1963
1964wxGridCellFloatRenderer::wxGridCellFloatRenderer(int width, int precision)
1965{
1966 SetWidth(width);
1967 SetPrecision(precision);
1968}
1969
e72b4213
VZ
1970wxGridCellRenderer *wxGridCellFloatRenderer::Clone() const
1971{
1972 wxGridCellFloatRenderer *renderer = new wxGridCellFloatRenderer;
1973 renderer->m_width = m_width;
1974 renderer->m_precision = m_precision;
1975 renderer->m_format = m_format;
1976
1977 return renderer;
1978}
1979
fbfb8bcc 1980wxString wxGridCellFloatRenderer::GetString(const wxGrid& grid, int row, int col)
65e4e78e
VZ
1981{
1982 wxGridTableBase *table = grid.GetTable();
0b190b0f
VZ
1983
1984 bool hasDouble;
1985 double val;
65e4e78e
VZ
1986 wxString text;
1987 if ( table->CanGetValueAs(row, col, wxGRID_VALUE_FLOAT) )
1988 {
0b190b0f 1989 val = table->GetValueAsDouble(row, col);
ca65c044 1990 hasDouble = true;
65e4e78e 1991 }
9c4ba614
VZ
1992 else
1993 {
1994 text = table->GetValue(row, col);
0b190b0f 1995 hasDouble = text.ToDouble(&val);
9c4ba614 1996 }
65e4e78e 1997
0b190b0f
VZ
1998 if ( hasDouble )
1999 {
2000 if ( !m_format )
2001 {
2002 if ( m_width == -1 )
2003 {
19d7140e
VZ
2004 if ( m_precision == -1 )
2005 {
2b5f62a0
VZ
2006 // default width/precision
2007 m_format = _T("%f");
2008 }
19d7140e
VZ
2009 else
2010 {
2011 m_format.Printf(_T("%%.%df"), m_precision);
2012 }
0b190b0f
VZ
2013 }
2014 else if ( m_precision == -1 )
2015 {
2016 // default precision
2017 m_format.Printf(_T("%%%d.f"), m_width);
2018 }
2019 else
2020 {
2021 m_format.Printf(_T("%%%d.%df"), m_width, m_precision);
2022 }
2023 }
2024
2025 text.Printf(m_format, val);
19d7140e 2026
0b190b0f
VZ
2027 }
2028 //else: text already contains the string
2029
65e4e78e
VZ
2030 return text;
2031}
2032
816be743
VZ
2033void wxGridCellFloatRenderer::Draw(wxGrid& grid,
2034 wxGridCellAttr& attr,
2035 wxDC& dc,
2036 const wxRect& rectCell,
2037 int row, int col,
2038 bool isSelected)
2039{
2040 wxGridCellRenderer::Draw(grid, attr, dc, rectCell, row, col, isSelected);
2041
2042 SetTextColoursAndFont(grid, attr, dc, isSelected);
2043
2044 // draw the text right aligned by default
2045 int hAlign, vAlign;
2046 attr.GetAlignment(&hAlign, &vAlign);
4c7277db 2047 hAlign = wxALIGN_RIGHT;
816be743
VZ
2048
2049 wxRect rect = rectCell;
2050 rect.Inflate(-1);
2051
65e4e78e
VZ
2052 grid.DrawTextRectangle(dc, GetString(grid, row, col), rect, hAlign, vAlign);
2053}
816be743 2054
65e4e78e
VZ
2055wxSize wxGridCellFloatRenderer::GetBestSize(wxGrid& grid,
2056 wxGridCellAttr& attr,
2057 wxDC& dc,
2058 int row, int col)
2059{
2060 return DoGetBestSize(attr, dc, GetString(grid, row, col));
816be743
VZ
2061}
2062
0b190b0f
VZ
2063void wxGridCellFloatRenderer::SetParameters(const wxString& params)
2064{
0b190b0f
VZ
2065 if ( !params )
2066 {
2067 // reset to defaults
2068 SetWidth(-1);
2069 SetPrecision(-1);
2070 }
2071 else
2072 {
2073 wxString tmp = params.BeforeFirst(_T(','));
ec157c8f 2074 if ( !tmp.empty() )
0b190b0f
VZ
2075 {
2076 long width;
19d7140e 2077 if ( tmp.ToLong(&width) )
0b190b0f 2078 {
19d7140e 2079 SetWidth((int)width);
0b190b0f
VZ
2080 }
2081 else
2082 {
19d7140e
VZ
2083 wxLogDebug(_T("Invalid wxGridCellFloatRenderer width parameter string '%s ignored"), params.c_str());
2084 }
19d7140e 2085 }
2f024384 2086
7448de8d
WS
2087 tmp = params.AfterFirst(_T(','));
2088 if ( !tmp.empty() )
2089 {
2090 long precision;
19d7140e 2091 if ( tmp.ToLong(&precision) )
7448de8d 2092 {
19d7140e 2093 SetPrecision((int)precision);
7448de8d
WS
2094 }
2095 else
2096 {
19d7140e 2097 wxLogDebug(_T("Invalid wxGridCellFloatRenderer precision parameter string '%s ignored"), params.c_str());
7448de8d 2098 }
0b190b0f
VZ
2099 }
2100 }
2101}
2102
508011ce
VZ
2103// ----------------------------------------------------------------------------
2104// wxGridCellBoolRenderer
2105// ----------------------------------------------------------------------------
2106
65e4e78e 2107wxSize wxGridCellBoolRenderer::ms_sizeCheckMark;
508011ce 2108
b94ae1ea
VZ
2109// FIXME these checkbox size calculations are really ugly...
2110
65e4e78e 2111// between checkmark and box
a95e38c0 2112static const wxCoord wxGRID_CHECKMARK_MARGIN = 2;
508011ce 2113
65e4e78e
VZ
2114wxSize wxGridCellBoolRenderer::GetBestSize(wxGrid& grid,
2115 wxGridCellAttr& WXUNUSED(attr),
2116 wxDC& WXUNUSED(dc),
2117 int WXUNUSED(row),
2118 int WXUNUSED(col))
2119{
2120 // compute it only once (no locks for MT safeness in GUI thread...)
2121 if ( !ms_sizeCheckMark.x )
297da4ba 2122 {
65e4e78e 2123 // get checkbox size
ca65c044 2124 wxCheckBox *checkbox = new wxCheckBox(&grid, wxID_ANY, wxEmptyString);
297da4ba 2125 wxSize size = checkbox->GetBestSize();
2f024384 2126 wxCoord checkSize = size.y + 2 * wxGRID_CHECKMARK_MARGIN;
297da4ba 2127
65e4e78e 2128 // FIXME wxGTK::wxCheckBox::GetBestSize() gives "wrong" result
69d8f612 2129#if defined(__WXGTK__) || defined(__WXMOTIF__)
65e4e78e 2130 checkSize -= size.y / 2;
297da4ba
VZ
2131#endif
2132
2133 delete checkbox;
65e4e78e
VZ
2134
2135 ms_sizeCheckMark.x = ms_sizeCheckMark.y = checkSize;
297da4ba
VZ
2136 }
2137
65e4e78e
VZ
2138 return ms_sizeCheckMark;
2139}
2140
2141void wxGridCellBoolRenderer::Draw(wxGrid& grid,
2142 wxGridCellAttr& attr,
2143 wxDC& dc,
2144 const wxRect& rect,
2145 int row, int col,
2146 bool isSelected)
2147{
2148 wxGridCellRenderer::Draw(grid, attr, dc, rect, row, col, isSelected);
2149
297da4ba 2150 // draw a check mark in the centre (ignoring alignment - TODO)
65e4e78e 2151 wxSize size = GetBestSize(grid, attr, dc, row, col);
b94ae1ea
VZ
2152
2153 // don't draw outside the cell
2154 wxCoord minSize = wxMin(rect.width, rect.height);
2155 if ( size.x >= minSize || size.y >= minSize )
2156 {
2157 // and even leave (at least) 1 pixel margin
2158 size.x = size.y = minSize - 2;
2159 }
2160
2161 // draw a border around checkmark
1bd71df9
JS
2162 int vAlign, hAlign;
2163 attr.GetAlignment(& hAlign, &vAlign);
52d6f640 2164
a95e38c0 2165 wxRect rectBorder;
1bd71df9
JS
2166 if (hAlign == wxALIGN_CENTRE)
2167 {
2f024384
DS
2168 rectBorder.x = rect.x + rect.width / 2 - size.x / 2;
2169 rectBorder.y = rect.y + rect.height / 2 - size.y / 2;
1bd71df9
JS
2170 rectBorder.width = size.x;
2171 rectBorder.height = size.y;
2172 }
2173 else if (hAlign == wxALIGN_LEFT)
2174 {
2175 rectBorder.x = rect.x + 2;
2f024384 2176 rectBorder.y = rect.y + rect.height / 2 - size.y / 2;
1bd71df9 2177 rectBorder.width = size.x;
52d6f640 2178 rectBorder.height = size.y;
1bd71df9
JS
2179 }
2180 else if (hAlign == wxALIGN_RIGHT)
2181 {
2182 rectBorder.x = rect.x + rect.width - size.x - 2;
2f024384 2183 rectBorder.y = rect.y + rect.height / 2 - size.y / 2;
1bd71df9 2184 rectBorder.width = size.x;
52d6f640 2185 rectBorder.height = size.y;
1bd71df9 2186 }
b94ae1ea 2187
f2d76237 2188 bool value;
b94ae1ea 2189 if ( grid.GetTable()->CanGetValueAs(row, col, wxGRID_VALUE_BOOL) )
f2d76237
RD
2190 value = grid.GetTable()->GetValueAsBool(row, col);
2191 else
695a3263
MB
2192 {
2193 wxString cellval( grid.GetTable()->GetValue(row, col) );
8dd8f875 2194 value = !( !cellval || (cellval == wxT("0")) );
695a3263 2195 }
f2d76237
RD
2196
2197 if ( value )
508011ce 2198 {
a95e38c0 2199 wxRect rectMark = rectBorder;
2f024384 2200
a95e38c0
VZ
2201#ifdef __WXMSW__
2202 // MSW DrawCheckMark() is weird (and should probably be changed...)
2f024384 2203 rectMark.Inflate(-wxGRID_CHECKMARK_MARGIN / 2);
a95e38c0
VZ
2204 rectMark.x++;
2205 rectMark.y++;
2f024384 2206#else
a95e38c0 2207 rectMark.Inflate(-wxGRID_CHECKMARK_MARGIN);
2f024384 2208#endif
a95e38c0 2209
508011ce
VZ
2210 dc.SetTextForeground(attr.GetTextColour());
2211 dc.DrawCheckMark(rectMark);
2212 }
a95e38c0
VZ
2213
2214 dc.SetBrush(*wxTRANSPARENT_BRUSH);
2215 dc.SetPen(wxPen(attr.GetTextColour(), 1, wxSOLID));
2216 dc.DrawRectangle(rectBorder);
508011ce
VZ
2217}
2218
2796cce3
RD
2219// ----------------------------------------------------------------------------
2220// wxGridCellAttr
2221// ----------------------------------------------------------------------------
2222
1df4050d
VZ
2223void wxGridCellAttr::Init(wxGridCellAttr *attrDefault)
2224{
2225 m_nRef = 1;
2226
2227 m_isReadOnly = Unset;
2228
2229 m_renderer = NULL;
2230 m_editor = NULL;
2231
2232 m_attrkind = wxGridCellAttr::Cell;
2233
27f35b66 2234 m_sizeRows = m_sizeCols = 1;
b63fce94 2235 m_overflow = UnsetOverflow;
27f35b66 2236
1df4050d
VZ
2237 SetDefAttr(attrDefault);
2238}
2239
39bcce60 2240wxGridCellAttr *wxGridCellAttr::Clone() const
a68c1246 2241{
1df4050d
VZ
2242 wxGridCellAttr *attr = new wxGridCellAttr(m_defGridAttr);
2243
a68c1246
VZ
2244 if ( HasTextColour() )
2245 attr->SetTextColour(GetTextColour());
2246 if ( HasBackgroundColour() )
2247 attr->SetBackgroundColour(GetBackgroundColour());
2248 if ( HasFont() )
2249 attr->SetFont(GetFont());
2250 if ( HasAlignment() )
2251 attr->SetAlignment(m_hAlign, m_vAlign);
2252
27f35b66
SN
2253 attr->SetSize( m_sizeRows, m_sizeCols );
2254
a68c1246
VZ
2255 if ( m_renderer )
2256 {
2257 attr->SetRenderer(m_renderer);
39bcce60 2258 m_renderer->IncRef();
a68c1246
VZ
2259 }
2260 if ( m_editor )
2261 {
2262 attr->SetEditor(m_editor);
39bcce60 2263 m_editor->IncRef();
a68c1246
VZ
2264 }
2265
2266 if ( IsReadOnly() )
2267 attr->SetReadOnly();
2268
19d7140e
VZ
2269 attr->SetKind( m_attrkind );
2270
a68c1246
VZ
2271 return attr;
2272}
2273
19d7140e
VZ
2274void wxGridCellAttr::MergeWith(wxGridCellAttr *mergefrom)
2275{
2276 if ( !HasTextColour() && mergefrom->HasTextColour() )
2277 SetTextColour(mergefrom->GetTextColour());
2278 if ( !HasBackgroundColour() && mergefrom->HasBackgroundColour() )
2279 SetBackgroundColour(mergefrom->GetBackgroundColour());
2280 if ( !HasFont() && mergefrom->HasFont() )
2281 SetFont(mergefrom->GetFont());
4db6714b
KH
2282 if ( !HasAlignment() && mergefrom->HasAlignment() )
2283 {
19d7140e
VZ
2284 int hAlign, vAlign;
2285 mergefrom->GetAlignment( &hAlign, &vAlign);
2286 SetAlignment(hAlign, vAlign);
2287 }
3100c3db
RD
2288 if ( !HasSize() && mergefrom->HasSize() )
2289 mergefrom->GetSize( &m_sizeRows, &m_sizeCols );
27f35b66 2290
19d7140e
VZ
2291 // Directly access member functions as GetRender/Editor don't just return
2292 // m_renderer/m_editor
2293 //
2294 // Maybe add support for merge of Render and Editor?
2295 if (!HasRenderer() && mergefrom->HasRenderer() )
bf7945ce 2296 {
19d7140e
VZ
2297 m_renderer = mergefrom->m_renderer;
2298 m_renderer->IncRef();
2299 }
2300 if ( !HasEditor() && mergefrom->HasEditor() )
2301 {
2302 m_editor = mergefrom->m_editor;
2303 m_editor->IncRef();
2304 }
2f024384 2305 if ( !HasReadWriteMode() && mergefrom->HasReadWriteMode() )
19d7140e
VZ
2306 SetReadOnly(mergefrom->IsReadOnly());
2307
2f024384 2308 if (!HasOverflowMode() && mergefrom->HasOverflowMode() )
ff699386 2309 SetOverflow(mergefrom->GetOverflow());
ef5df12b 2310
19d7140e
VZ
2311 SetDefAttr(mergefrom->m_defGridAttr);
2312}
2313
27f35b66
SN
2314void wxGridCellAttr::SetSize(int num_rows, int num_cols)
2315{
2316 // The size of a cell is normally 1,1
2317
2318 // If this cell is larger (2,2) then this is the top left cell
2319 // the other cells that will be covered (lower right cells) must be
2320 // set to negative or zero values such that
2321 // row + num_rows of the covered cell points to the larger cell (this cell)
2322 // same goes for the col + num_cols.
2323
2324 // Size of 0,0 is NOT valid, neither is <=0 and any positive value
2325
2f024384
DS
2326 wxASSERT_MSG( (!((num_rows > 0) && (num_cols <= 0)) ||
2327 !((num_rows <= 0) && (num_cols > 0)) ||
2328 !((num_rows == 0) && (num_cols == 0))),
27f35b66
SN
2329 wxT("wxGridCellAttr::SetSize only takes two postive values or negative/zero values"));
2330
2331 m_sizeRows = num_rows;
2332 m_sizeCols = num_cols;
2333}
2334
2796cce3
RD
2335const wxColour& wxGridCellAttr::GetTextColour() const
2336{
2337 if (HasTextColour())
508011ce 2338 {
2796cce3 2339 return m_colText;
508011ce 2340 }
0926b2fc 2341 else if (m_defGridAttr && m_defGridAttr != this)
508011ce 2342 {
2796cce3 2343 return m_defGridAttr->GetTextColour();
508011ce
VZ
2344 }
2345 else
2346 {
2796cce3
RD
2347 wxFAIL_MSG(wxT("Missing default cell attribute"));
2348 return wxNullColour;
2349 }
2350}
2351
2796cce3
RD
2352const wxColour& wxGridCellAttr::GetBackgroundColour() const
2353{
2354 if (HasBackgroundColour())
2f024384 2355 {
2796cce3 2356 return m_colBack;
2f024384 2357 }
0926b2fc 2358 else if (m_defGridAttr && m_defGridAttr != this)
2f024384 2359 {
2796cce3 2360 return m_defGridAttr->GetBackgroundColour();
2f024384 2361 }
508011ce
VZ
2362 else
2363 {
2796cce3
RD
2364 wxFAIL_MSG(wxT("Missing default cell attribute"));
2365 return wxNullColour;
2366 }
2367}
2368
2796cce3
RD
2369const wxFont& wxGridCellAttr::GetFont() const
2370{
2371 if (HasFont())
2f024384 2372 {
2796cce3 2373 return m_font;
2f024384 2374 }
0926b2fc 2375 else if (m_defGridAttr && m_defGridAttr != this)
2f024384 2376 {
2796cce3 2377 return m_defGridAttr->GetFont();
2f024384 2378 }
508011ce
VZ
2379 else
2380 {
2796cce3
RD
2381 wxFAIL_MSG(wxT("Missing default cell attribute"));
2382 return wxNullFont;
2383 }
2384}
2385
2796cce3
RD
2386void wxGridCellAttr::GetAlignment(int *hAlign, int *vAlign) const
2387{
508011ce
VZ
2388 if (HasAlignment())
2389 {
4db6714b
KH
2390 if ( hAlign )
2391 *hAlign = m_hAlign;
2392 if ( vAlign )
2393 *vAlign = m_vAlign;
2796cce3 2394 }
0926b2fc 2395 else if (m_defGridAttr && m_defGridAttr != this)
2796cce3 2396 m_defGridAttr->GetAlignment(hAlign, vAlign);
508011ce
VZ
2397 else
2398 {
2796cce3
RD
2399 wxFAIL_MSG(wxT("Missing default cell attribute"));
2400 }
2401}
2402
27f35b66
SN
2403void wxGridCellAttr::GetSize( int *num_rows, int *num_cols ) const
2404{
4db6714b
KH
2405 if ( num_rows )
2406 *num_rows = m_sizeRows;
2407 if ( num_cols )
2408 *num_cols = m_sizeCols;
27f35b66 2409}
2796cce3 2410
f2d76237 2411// GetRenderer and GetEditor use a slightly different decision path about
28a77bc4
RD
2412// which attribute to use. If a non-default attr object has one then it is
2413// used, otherwise the default editor or renderer is fetched from the grid and
2414// used. It should be the default for the data type of the cell. If it is
2415// NULL (because the table has a type that the grid does not have in its
2416// registry,) then the grid's default editor or renderer is used.
2417
2418wxGridCellRenderer* wxGridCellAttr::GetRenderer(wxGrid* grid, int row, int col) const
2419{
3cf883a2 2420 wxGridCellRenderer *renderer;
28a77bc4 2421
3cf883a2 2422 if ( m_renderer && this != m_defGridAttr )
0b190b0f 2423 {
3cf883a2
VZ
2424 // use the cells renderer if it has one
2425 renderer = m_renderer;
2426 renderer->IncRef();
0b190b0f 2427 }
2f024384 2428 else // no non-default cell renderer
0b190b0f 2429 {
3cf883a2
VZ
2430 // get default renderer for the data type
2431 if ( grid )
2432 {
2433 // GetDefaultRendererForCell() will do IncRef() for us
2434 renderer = grid->GetDefaultRendererForCell(row, col);
2435 }
2436 else
2437 {
2438 renderer = NULL;
2439 }
0b190b0f 2440
3cf883a2
VZ
2441 if ( !renderer )
2442 {
0926b2fc 2443 if (m_defGridAttr && this != m_defGridAttr )
3cf883a2
VZ
2444 {
2445 // if we still don't have one then use the grid default
2446 // (no need for IncRef() here neither)
2447 renderer = m_defGridAttr->GetRenderer(NULL, 0, 0);
2448 }
2449 else // default grid attr
2450 {
2451 // use m_renderer which we had decided not to use initially
2452 renderer = m_renderer;
2453 if ( renderer )
2454 renderer->IncRef();
2455 }
2456 }
0b190b0f 2457 }
28a77bc4 2458
3cf883a2
VZ
2459 // we're supposed to always find something
2460 wxASSERT_MSG(renderer, wxT("Missing default cell renderer"));
28a77bc4
RD
2461
2462 return renderer;
2796cce3
RD
2463}
2464
3cf883a2 2465// same as above, except for s/renderer/editor/g
28a77bc4 2466wxGridCellEditor* wxGridCellAttr::GetEditor(wxGrid* grid, int row, int col) const
07296f0b 2467{
3cf883a2 2468 wxGridCellEditor *editor;
0b190b0f 2469
3cf883a2 2470 if ( m_editor && this != m_defGridAttr )
0b190b0f 2471 {
3cf883a2
VZ
2472 // use the cells editor if it has one
2473 editor = m_editor;
2474 editor->IncRef();
0b190b0f 2475 }
3cf883a2 2476 else // no non default cell editor
0b190b0f 2477 {
3cf883a2
VZ
2478 // get default editor for the data type
2479 if ( grid )
2480 {
2481 // GetDefaultEditorForCell() will do IncRef() for us
2482 editor = grid->GetDefaultEditorForCell(row, col);
2483 }
2484 else
2485 {
2486 editor = NULL;
2487 }
2488
2489 if ( !editor )
2490 {
0926b2fc 2491 if ( m_defGridAttr && this != m_defGridAttr )
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;
3209 for ( i = 0; i < n; i++ )
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
3378 for ( row = 0; row < numRows; row++ )
3379 {
3380 for ( col = 0; col < numCols; col++ )
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
3503 for ( row = 0; row < curNumRows; row++ )
3504 {
3505 for ( col = pos; col < pos + numCols; col++ )
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
f85afd4e
MB
3540 for ( row = 0; row < curNumRows; row++ )
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
3582 for ( row = 0; row < curNumRows; row++ )
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
f85afd4e
MB
3642 for ( i = n; i <= row; i++ )
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
f85afd4e
MB
3658 for ( i = n; i <= col; i++ )
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 )
73bb6776 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,
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{
ca65c044 4150 m_created = false; // set to true by CreateGrid
4634a5d6
MB
4151
4152 m_table = (wxGridTableBase *) NULL;
ca65c044 4153 m_ownTable = false;
2c9a89e0 4154
ca65c044 4155 m_cellEditCtrlEnabled = false;
4634a5d6 4156
ccd970b1 4157 m_defaultCellAttr = new wxGridCellAttr();
f2d76237
RD
4158
4159 // Set default cell attributes
ccd970b1 4160 m_defaultCellAttr->SetDefAttr(m_defaultCellAttr);
19d7140e 4161 m_defaultCellAttr->SetKind(wxGridCellAttr::Default);
f2d76237 4162 m_defaultCellAttr->SetFont(GetFont());
4c7277db 4163 m_defaultCellAttr->SetAlignment(wxALIGN_LEFT, wxALIGN_TOP);
9950649c
RD
4164 m_defaultCellAttr->SetRenderer(new wxGridCellStringRenderer);
4165 m_defaultCellAttr->SetEditor(new wxGridCellTextEditor);
4166
4167#if _USE_VISATTR
4168 wxVisualAttributes gva = wxListBox::GetClassDefaultAttributes();
4169 wxVisualAttributes lva = wxPanel::GetClassDefaultAttributes();
4170
4171 m_defaultCellAttr->SetTextColour(gva.colFg);
4172 m_defaultCellAttr->SetBackgroundColour(gva.colBg);
ca65c044 4173
9950649c 4174#else
f2d76237 4175 m_defaultCellAttr->SetTextColour(
a756f210 4176 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT));
f2d76237 4177 m_defaultCellAttr->SetBackgroundColour(
a756f210 4178 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW));
9950649c 4179#endif
2796cce3 4180
4634a5d6
MB
4181 m_numRows = 0;
4182 m_numCols = 0;
4183 m_currentCellCoords = wxGridNoCellCoords;
b99be8fb 4184
18f9565d
MB
4185 m_rowLabelWidth = WXGRID_DEFAULT_ROW_LABEL_WIDTH;
4186 m_colLabelHeight = WXGRID_DEFAULT_COL_LABEL_HEIGHT;
2d66e025 4187
c4608a8a 4188 // create the type registry
f2d76237 4189 m_typeRegistry = new wxGridTypeRegistry;
3f3dc2ef
VZ
4190 m_selection = NULL;
4191
f2d76237 4192 // subwindow components that make up the wxGrid
7807d81c 4193 m_cornerLabelWin = new wxGridCornerLabelWindow( this,
ca65c044 4194 wxID_ANY,
18f9565d
MB
4195 wxDefaultPosition,
4196 wxDefaultSize );
7807d81c 4197
60ff3b99 4198 m_rowLabelWin = new wxGridRowLabelWindow( this,
ca65c044 4199 wxID_ANY,
18f9565d
MB
4200 wxDefaultPosition,
4201 wxDefaultSize );
2d66e025
MB
4202
4203 m_colLabelWin = new wxGridColLabelWindow( this,
ca65c044 4204 wxID_ANY,
18f9565d
MB
4205 wxDefaultPosition,
4206 wxDefaultSize );
60ff3b99 4207
60ff3b99
VZ
4208 m_gridWin = new wxGridWindow( this,
4209 m_rowLabelWin,
4210 m_colLabelWin,
ca65c044 4211 wxID_ANY,
18f9565d 4212 wxDefaultPosition,
2d66e025
MB
4213 wxDefaultSize );
4214
4215 SetTargetWindow( m_gridWin );
6f36917b 4216
9950649c
RD
4217#if _USE_VISATTR
4218 wxColour gfg = gva.colFg;
4219 wxColour gbg = gva.colBg;
4220 wxColour lfg = lva.colFg;
4221 wxColour lbg = lva.colBg;
4222#else
4223 wxColour gfg = wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT );
4224 wxColour gbg = wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOW );
4225 wxColour lfg = wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT );
4226 wxColour lbg = wxSystemSettings::GetColour( wxSYS_COLOUR_BTNFACE );
4227#endif
2f024384 4228
fa47d7a7
VS
4229 m_cornerLabelWin->SetOwnForegroundColour(lfg);
4230 m_cornerLabelWin->SetOwnBackgroundColour(lbg);
4231 m_rowLabelWin->SetOwnForegroundColour(lfg);
4232 m_rowLabelWin->SetOwnBackgroundColour(lbg);
4233 m_colLabelWin->SetOwnForegroundColour(lfg);
4234 m_colLabelWin->SetOwnBackgroundColour(lbg);
4235
4236 m_gridWin->SetOwnForegroundColour(gfg);
4237 m_gridWin->SetOwnBackgroundColour(gbg);
ca65c044 4238
6f36917b 4239 Init();
2d66e025 4240}
f85afd4e 4241
b5808881
SN
4242bool wxGrid::CreateGrid( int numRows, int numCols,
4243 wxGrid::wxGridSelectionModes selmode )
2d66e025 4244{
f6bcfd97 4245 wxCHECK_MSG( !m_created,
ca65c044 4246 false,
f6bcfd97
BP
4247 wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
4248
4249 m_numRows = numRows;
4250 m_numCols = numCols;
4251
4252 m_table = new wxGridStringTable( m_numRows, m_numCols );
4253 m_table->SetView( this );
ca65c044 4254 m_ownTable = true;
f6bcfd97 4255 m_selection = new wxGridSelection( this, selmode );
6f36917b
VZ
4256
4257 CalcDimensions();
4258
ca65c044 4259 m_created = true;
2d66e025 4260
2796cce3
RD
4261 return m_created;
4262}
4263
f1567cdd
SN
4264void wxGrid::SetSelectionMode(wxGrid::wxGridSelectionModes selmode)
4265{
6f36917b
VZ
4266 wxCHECK_RET( m_created,
4267 wxT("Called wxGrid::SetSelectionMode() before calling CreateGrid()") );
4268
4269 m_selection->SetSelectionMode( selmode );
f1567cdd
SN
4270}
4271
aa5b8857
SN
4272wxGrid::wxGridSelectionModes wxGrid::GetSelectionMode() const
4273{
4274 wxCHECK_MSG( m_created, wxGrid::wxGridSelectCells,
4275 wxT("Called wxGrid::GetSelectionMode() before calling CreateGrid()") );
4276
4277 return m_selection->GetSelectionMode();
4278}
4279
043d16b2
SN
4280bool wxGrid::SetTable( wxGridTableBase *table, bool takeOwnership,
4281 wxGrid::wxGridSelectionModes selmode )
2796cce3
RD
4282{
4283 if ( m_created )
4284 {
3e13956a 4285 // stop all processing
ca65c044 4286 m_created = false;
86c7378f
SN
4287
4288 if (m_ownTable)
4289 {
5b061713
DS
4290 wxGridTableBase *t = m_table;
4291 m_table = NULL;
3e13956a 4292 delete t;
86c7378f 4293 }
2f024384 4294
3e13956a
RD
4295 delete m_selection;
4296
5b061713
DS
4297 m_table = NULL;
4298 m_selection = NULL;
2f024384
DS
4299 m_numRows = 0;
4300 m_numCols = 0;
233a54f6 4301 }
2f024384 4302
233a54f6 4303 if (table)
2796cce3
RD
4304 {
4305 m_numRows = table->GetNumberRows();
4306 m_numCols = table->GetNumberCols();
4307
4308 m_table = table;
4309 m_table->SetView( this );
8fc856de 4310 m_ownTable = takeOwnership;
043d16b2 4311 m_selection = new wxGridSelection( this, selmode );
6f36917b
VZ
4312
4313 CalcDimensions();
4314
ca65c044 4315 m_created = true;
2d66e025 4316 }
f85afd4e 4317
2d66e025 4318 return m_created;
f85afd4e
MB
4319}
4320
4321void wxGrid::Init()
4322{
f85afd4e
MB
4323 m_rowLabelWidth = WXGRID_DEFAULT_ROW_LABEL_WIDTH;
4324 m_colLabelHeight = WXGRID_DEFAULT_COL_LABEL_HEIGHT;
4325
60ff3b99
VZ
4326 if ( m_rowLabelWin )
4327 {
4328 m_labelBackgroundColour = m_rowLabelWin->GetBackgroundColour();
4329 }
4330 else
4331 {
ccdee36f 4332 m_labelBackgroundColour = wxColour( wxT("WHITE") );
60ff3b99
VZ
4333 }
4334
ccdee36f 4335 m_labelTextColour = wxColour( wxT("BLACK") );
f85afd4e 4336
0a976765
VZ
4337 // init attr cache
4338 m_attrCache.row = -1;
2b5f62a0
VZ
4339 m_attrCache.col = -1;
4340 m_attrCache.attr = NULL;
0a976765 4341
f85afd4e
MB
4342 // TODO: something better than this ?
4343 //
4344 m_labelFont = this->GetFont();
52d6f640 4345 m_labelFont.SetWeight( wxBOLD );
8f177c8e 4346
73145b0e 4347 m_rowLabelHorizAlign = wxALIGN_CENTRE;
4c7277db 4348 m_rowLabelVertAlign = wxALIGN_CENTRE;
f85afd4e 4349
4c7277db 4350 m_colLabelHorizAlign = wxALIGN_CENTRE;
73145b0e 4351 m_colLabelVertAlign = wxALIGN_CENTRE;
d43851f7 4352 m_colLabelTextOrientation = wxHORIZONTAL;
f85afd4e 4353
f85afd4e 4354 m_defaultColWidth = WXGRID_DEFAULT_COL_WIDTH;
1f1ce288
MB
4355 m_defaultRowHeight = m_gridWin->GetCharHeight();
4356
b8d24d4e
RG
4357 m_minAcceptableColWidth = WXGRID_MIN_COL_WIDTH;
4358 m_minAcceptableRowHeight = WXGRID_MIN_ROW_HEIGHT;
4359
d2fdd8d2 4360#if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl()
1f1ce288
MB
4361 m_defaultRowHeight += 8;
4362#else
4363 m_defaultRowHeight += 4;
4364#endif
4365
73145b0e 4366 m_gridLineColour = wxColour( 192,192,192 );
ca65c044 4367 m_gridLinesEnabled = true;
73145b0e 4368 m_cellHighlightColour = *wxBLACK;
bf7945ce 4369 m_cellHighlightPenWidth = 2;
d2520c85 4370 m_cellHighlightROPenWidth = 1;
8f177c8e 4371
58dd5b3b 4372 m_cursorMode = WXGRID_CURSOR_SELECT_CELL;
e2b42eeb 4373 m_winCapture = (wxWindow *)NULL;
ca65c044
WS
4374 m_canDragRowSize = true;
4375 m_canDragColSize = true;
4376 m_canDragGridSize = true;
79dbea21 4377 m_canDragCell = false;
f85afd4e
MB
4378 m_dragLastPos = -1;
4379 m_dragRowOrCol = -1;
ca65c044 4380 m_isDragging = false;
07296f0b 4381 m_startDragPos = wxDefaultPosition;
f85afd4e 4382
ca65c044 4383 m_waitForSlowClick = false;
025562fe 4384
f85afd4e
MB
4385 m_rowResizeCursor = wxCursor( wxCURSOR_SIZENS );
4386 m_colResizeCursor = wxCursor( wxCURSOR_SIZEWE );
4387
4388 m_currentCellCoords = wxGridNoCellCoords;
f85afd4e 4389
b5808881
SN
4390 m_selectingTopLeft = wxGridNoCellCoords;
4391 m_selectingBottomRight = wxGridNoCellCoords;
d43851f7
JS
4392 m_selectionBackground = wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT);
4393 m_selectionForeground = wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT);
8f177c8e 4394
ca65c044 4395 m_editable = true; // default for whole grid
f85afd4e 4396
ca65c044 4397 m_inOnKeyDown = false;
2d66e025 4398 m_batchCount = 0;
3c79cf49 4399
266e8367 4400 m_extraWidth =
526dbb95 4401 m_extraHeight = 0;
608754c4
JS
4402
4403 m_scrollLineX = GRID_SCROLL_LINE_X;
4404 m_scrollLineY = GRID_SCROLL_LINE_Y;
7c1cb261
VZ
4405}
4406
4407// ----------------------------------------------------------------------------
4408// the idea is to call these functions only when necessary because they create
4409// quite big arrays which eat memory mostly unnecessary - in particular, if
4410// default widths/heights are used for all rows/columns, we may not use these
4411// arrays at all
4412//
4413// with some extra code, it should be possible to only store the
4414// widths/heights different from default ones but this will be done later...
4415// ----------------------------------------------------------------------------
4416
4417void wxGrid::InitRowHeights()
4418{
4419 m_rowHeights.Empty();
4420 m_rowBottoms.Empty();
4421
4422 m_rowHeights.Alloc( m_numRows );
4423 m_rowBottoms.Alloc( m_numRows );
4424
4425 int rowBottom = 0;
2b5f62a0 4426
27f35b66
SN
4427 m_rowHeights.Add( m_defaultRowHeight, m_numRows );
4428
7c1cb261
VZ
4429 for ( int i = 0; i < m_numRows; i++ )
4430 {
7c1cb261
VZ
4431 rowBottom += m_defaultRowHeight;
4432 m_rowBottoms.Add( rowBottom );
4433 }
4434}
4435
4436void wxGrid::InitColWidths()
4437{
4438 m_colWidths.Empty();
4439 m_colRights.Empty();
4440
4441 m_colWidths.Alloc( m_numCols );
4442 m_colRights.Alloc( m_numCols );
4443 int colRight = 0;
27f35b66
SN
4444
4445 m_colWidths.Add( m_defaultColWidth, m_numCols );
4446
7c1cb261
VZ
4447 for ( int i = 0; i < m_numCols; i++ )
4448 {
7c1cb261
VZ
4449 colRight += m_defaultColWidth;
4450 m_colRights.Add( colRight );
4451 }
4452}
4453
4454int wxGrid::GetColWidth(int col) const
4455{
4456 return m_colWidths.IsEmpty() ? m_defaultColWidth : m_colWidths[col];
4457}
4458
4459int wxGrid::GetColLeft(int col) const
4460{
4461 return m_colRights.IsEmpty() ? col * m_defaultColWidth
4462 : m_colRights[col] - m_colWidths[col];
4463}
4464
4465int wxGrid::GetColRight(int col) const
4466{
4467 return m_colRights.IsEmpty() ? (col + 1) * m_defaultColWidth
4468 : m_colRights[col];
4469}
4470
4471int wxGrid::GetRowHeight(int row) const
4472{
4473 return m_rowHeights.IsEmpty() ? m_defaultRowHeight : m_rowHeights[row];
4474}
2d66e025 4475
7c1cb261
VZ
4476int wxGrid::GetRowTop(int row) const
4477{
4478 return m_rowBottoms.IsEmpty() ? row * m_defaultRowHeight
4479 : m_rowBottoms[row] - m_rowHeights[row];
f85afd4e
MB
4480}
4481
7c1cb261
VZ
4482int wxGrid::GetRowBottom(int row) const
4483{
4484 return m_rowBottoms.IsEmpty() ? (row + 1) * m_defaultRowHeight
4485 : m_rowBottoms[row];
4486}
f85afd4e
MB
4487
4488void wxGrid::CalcDimensions()
4489{
f85afd4e 4490 int cw, ch;
2d66e025 4491 GetClientSize( &cw, &ch );
f85afd4e 4492
faec5a43 4493 if ( m_rowLabelWin->IsShown() )
ae1d0c6c
VZ
4494 cw -= m_rowLabelWidth;
4495 if ( m_colLabelWin->IsShown() )
faec5a43 4496 ch -= m_colLabelHeight;
60ff3b99 4497
faec5a43
SN
4498 // grid total size
4499 int w = m_numCols > 0 ? GetColRight(m_numCols - 1) + m_extraWidth + 1 : 0;
4500 int h = m_numRows > 0 ? GetRowBottom(m_numRows - 1) + m_extraHeight + 1 : 0;
4501
73145b0e 4502 // take into account editor if shown
4db6714b 4503 if ( IsCellEditControlShown() )
73145b0e
JS
4504 {
4505 int w2, h2;
4506 int r = m_currentCellCoords.GetRow();
4507 int c = m_currentCellCoords.GetCol();
4508 int x = GetColLeft(c);
4509 int y = GetRowTop(r);
4510
4511 // how big is the editor
4512 wxGridCellAttr* attr = GetCellAttr(r, c);
4513 wxGridCellEditor* editor = attr->GetEditor(this, r, c);
4514 editor->GetControl()->GetSize(&w2, &h2);
4515 w2 += x;
4516 h2 += y;
4db6714b
KH
4517 if ( w2 > w )
4518 w = w2;
4519 if ( h2 > h )
4520 h = h2;
73145b0e
JS
4521 editor->DecRef();
4522 attr->DecRef();
4523 }
4524
faec5a43
SN
4525 // preserve (more or less) the previous position
4526 int x, y;
4527 GetViewStart( &x, &y );
97a9929e 4528
c92ed9f7 4529 // ensure the position is valid for the new scroll ranges
7b519e5e 4530 if ( x >= w )
c92ed9f7 4531 x = wxMax( w - 1, 0 );
7b519e5e 4532 if ( y >= h )
c92ed9f7 4533 y = wxMax( h - 1, 0 );
faec5a43
SN
4534
4535 // do set scrollbar parameters
675a9c0d 4536 SetScrollbars( m_scrollLineX, m_scrollLineY,
97a9929e
VZ
4537 GetScrollX(w), GetScrollY(h), x, y,
4538 GetBatchCount() != 0);
12314291
VZ
4539
4540 // if our OnSize() hadn't been called (it would if we have scrollbars), we
4541 // still must reposition the children
4542 CalcWindowSizes();
f85afd4e
MB
4543}
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
6d308072 4559 if ( m_colLabelWin && m_colLabelWin->IsShown() )
ccdee36f 4560 m_colLabelWin->SetSize( m_rowLabelWidth, 0, cw - m_rowLabelWidth, m_colLabelHeight);
7807d81c 4561
6d308072 4562 if ( m_rowLabelWin && m_rowLabelWin->IsShown() )
ccdee36f 4563 m_rowLabelWin->SetSize( 0, m_colLabelHeight, m_rowLabelWidth, ch - m_colLabelHeight);
7807d81c 4564
6d308072 4565 if ( m_gridWin && m_gridWin->IsShown() )
ccdee36f 4566 m_gridWin->SetSize( m_rowLabelWidth, m_colLabelHeight, cw - m_rowLabelWidth, ch - m_colLabelHeight);
7807d81c
MB
4567}
4568
4569
f85afd4e
MB
4570// this is called when the grid table sends a message to say that it
4571// has been redimensioned
4572//
4573bool wxGrid::Redimension( wxGridTableMessage& msg )
4574{
4575 int i;
ca65c044 4576 bool result = false;
8f177c8e 4577
a6794685
SN
4578 // Clear the attribute cache as the attribute might refer to a different
4579 // cell than stored in the cache after adding/removing rows/columns.
4580 ClearAttrCache();
2f024384 4581
7e48d7d9
SN
4582 // By the same reasoning, the editor should be dismissed if columns are
4583 // added or removed. And for consistency, it should IMHO always be
4584 // removed, not only if the cell "underneath" it actually changes.
4585 // For now, I intentionally do not save the editor's content as the
4586 // cell it might want to save that stuff to might no longer exist.
bca7bfc8 4587 HideCellEditControl();
2f024384 4588
f6bcfd97 4589#if 0
7c1cb261
VZ
4590 // if we were using the default widths/heights so far, we must change them
4591 // now
4592 if ( m_colWidths.IsEmpty() )
4593 {
4594 InitColWidths();
4595 }
4596
4597 if ( m_rowHeights.IsEmpty() )
4598 {
4599 InitRowHeights();
4600 }
f6bcfd97 4601#endif
7c1cb261 4602
f85afd4e
MB
4603 switch ( msg.GetId() )
4604 {
4605 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED:
4606 {
4607 size_t pos = msg.GetCommandInt();
4608 int numRows = msg.GetCommandInt2();
f6bcfd97 4609
f85afd4e 4610 m_numRows += numRows;
2d66e025 4611
f6bcfd97
BP
4612 if ( !m_rowHeights.IsEmpty() )
4613 {
27f35b66
SN
4614 m_rowHeights.Insert( m_defaultRowHeight, pos, numRows );
4615 m_rowBottoms.Insert( 0, pos, numRows );
f6bcfd97
BP
4616
4617 int bottom = 0;
2f024384
DS
4618 if ( pos > 0 )
4619 bottom = m_rowBottoms[pos - 1];
60ff3b99 4620
f6bcfd97
BP
4621 for ( i = pos; i < m_numRows; i++ )
4622 {
4623 bottom += m_rowHeights[i];
4624 m_rowBottoms[i] = bottom;
4625 }
4626 }
2f024384 4627
f6bcfd97
BP
4628 if ( m_currentCellCoords == wxGridNoCellCoords )
4629 {
4630 // if we have just inserted cols into an empty grid the current
4631 // cell will be undefined...
4632 //
4633 SetCurrentCell( 0, 0 );
4634 }
3f3dc2ef
VZ
4635
4636 if ( m_selection )
4637 m_selection->UpdateRows( pos, numRows );
f6bcfd97
BP
4638 wxGridCellAttrProvider * attrProvider = m_table->GetAttrProvider();
4639 if (attrProvider)
4640 attrProvider->UpdateAttrRows( pos, numRows );
4641
4642 if ( !GetBatchCount() )
2d66e025 4643 {
f6bcfd97
BP
4644 CalcDimensions();
4645 m_rowLabelWin->Refresh();
2d66e025 4646 }
f85afd4e 4647 }
ca65c044 4648 result = true;
f6bcfd97 4649 break;
f85afd4e
MB
4650
4651 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED:
4652 {
4653 int numRows = msg.GetCommandInt();
2d66e025 4654 int oldNumRows = m_numRows;
f85afd4e 4655 m_numRows += numRows;
2d66e025 4656
f6bcfd97
BP
4657 if ( !m_rowHeights.IsEmpty() )
4658 {
27f35b66
SN
4659 m_rowHeights.Add( m_defaultRowHeight, numRows );
4660 m_rowBottoms.Add( 0, numRows );
60ff3b99 4661
f6bcfd97 4662 int bottom = 0;
2f024384
DS
4663 if ( oldNumRows > 0 )
4664 bottom = m_rowBottoms[oldNumRows - 1];
f6bcfd97
BP
4665
4666 for ( i = oldNumRows; i < m_numRows; i++ )
4667 {
4668 bottom += m_rowHeights[i];
4669 m_rowBottoms[i] = bottom;
4670 }
4671 }
2f024384 4672
f6bcfd97
BP
4673 if ( m_currentCellCoords == wxGridNoCellCoords )
4674 {
4675 // if we have just inserted cols into an empty grid the current
4676 // cell will be undefined...
4677 //
4678 SetCurrentCell( 0, 0 );
4679 }
2f024384 4680
f6bcfd97 4681 if ( !GetBatchCount() )
2d66e025 4682 {
f6bcfd97
BP
4683 CalcDimensions();
4684 m_rowLabelWin->Refresh();
2d66e025 4685 }
f85afd4e 4686 }
ca65c044 4687 result = true;
f6bcfd97 4688 break;
f85afd4e
MB
4689
4690 case wxGRIDTABLE_NOTIFY_ROWS_DELETED:
4691 {
4692 size_t pos = msg.GetCommandInt();
4693 int numRows = msg.GetCommandInt2();
f85afd4e
MB
4694 m_numRows -= numRows;
4695
f6bcfd97 4696 if ( !m_rowHeights.IsEmpty() )
f85afd4e 4697 {
27f35b66
SN
4698 m_rowHeights.RemoveAt( pos, numRows );
4699 m_rowBottoms.RemoveAt( pos, numRows );
2d66e025
MB
4700
4701 int h = 0;
4702 for ( i = 0; i < m_numRows; i++ )
4703 {
4704 h += m_rowHeights[i];
4705 m_rowBottoms[i] = h;
4706 }
f85afd4e 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) );
84912ef8
RD
4724// ifdef'd out following patch from Paul Gammans
4725#if 0
3ca6a5f0 4726 // No need to touch column attributes, unless we
f6bcfd97
BP
4727 // removed _all_ rows, in this case, we remove
4728 // all column attributes.
4729 // I hate to do this here, but the
4730 // needed data is not available inside UpdateAttrRows.
4731 if ( !GetNumberRows() )
4732 attrProvider->UpdateAttrCols( 0, -GetNumberCols() );
84912ef8 4733#endif
f6bcfd97 4734 }
ccdee36f 4735
f6bcfd97
BP
4736 if ( !GetBatchCount() )
4737 {
4738 CalcDimensions();
4739 m_rowLabelWin->Refresh();
4740 }
f85afd4e 4741 }
ca65c044 4742 result = true;
f6bcfd97 4743 break;
f85afd4e
MB
4744
4745 case wxGRIDTABLE_NOTIFY_COLS_INSERTED:
4746 {
4747 size_t pos = msg.GetCommandInt();
4748 int numCols = msg.GetCommandInt2();
f85afd4e 4749 m_numCols += numCols;
2d66e025 4750
f6bcfd97
BP
4751 if ( !m_colWidths.IsEmpty() )
4752 {
27f35b66
SN
4753 m_colWidths.Insert( m_defaultColWidth, pos, numCols );
4754 m_colRights.Insert( 0, pos, numCols );
f6bcfd97
BP
4755
4756 int right = 0;
2f024384
DS
4757 if ( pos > 0 )
4758 right = m_colRights[pos - 1];
60ff3b99 4759
f6bcfd97
BP
4760 for ( i = pos; i < m_numCols; i++ )
4761 {
4762 right += m_colWidths[i];
4763 m_colRights[i] = right;
4764 }
4765 }
2f024384 4766
f6bcfd97 4767 if ( m_currentCellCoords == wxGridNoCellCoords )
2d66e025 4768 {
f6bcfd97
BP
4769 // if we have just inserted cols into an empty grid the current
4770 // cell will be undefined...
4771 //
4772 SetCurrentCell( 0, 0 );
2d66e025 4773 }
3f3dc2ef
VZ
4774
4775 if ( m_selection )
4776 m_selection->UpdateCols( pos, numCols );
f6bcfd97
BP
4777 wxGridCellAttrProvider * attrProvider = m_table->GetAttrProvider();
4778 if (attrProvider)
4779 attrProvider->UpdateAttrCols( pos, numCols );
4780 if ( !GetBatchCount() )
4781 {
4782 CalcDimensions();
4783 m_colLabelWin->Refresh();
4784 }
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
f6bcfd97
BP
4804 for ( i = oldNumCols; i < m_numCols; i++ )
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;
4839 for ( i = 0; i < m_numCols; i++ )
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 );
4911 if ( r.GetTop() > ch ) r.SetTop( 0 );
4912 r.SetBottom( wxMin( r.GetBottom(), ch ) );
4913#endif
f85afd4e 4914
2d66e025
MB
4915 // logical bounds of update region
4916 //
4917 int dummy;
4918 CalcUnscrolledPosition( 0, r.GetTop(), &dummy, &top );
4919 CalcUnscrolledPosition( 0, r.GetBottom(), &dummy, &bottom );
4920
4921 // find the row labels within these bounds
4922 //
4923 int row;
33188aa4 4924 for ( row = internalYToRow(top); row < m_numRows; row++ )
2d66e025 4925 {
7c1cb261
VZ
4926 if ( GetRowBottom(row) < top )
4927 continue;
2d66e025 4928
6d55126d 4929 if ( GetRowTop(row) > bottom )
7c1cb261 4930 break;
60ff3b99 4931
d10f4bf9 4932 rowlabels.Add( row );
2d66e025 4933 }
60ff3b99 4934
60d8e886 4935 ++iter;
f85afd4e 4936 }
d10f4bf9
VZ
4937
4938 return rowlabels;
f85afd4e
MB
4939}
4940
d10f4bf9 4941wxArrayInt wxGrid::CalcColLabelsExposed( const wxRegion& reg )
f85afd4e 4942{
2d66e025
MB
4943 wxRegionIterator iter( reg );
4944 wxRect r;
f85afd4e 4945
d10f4bf9 4946 wxArrayInt colLabels;
f85afd4e 4947
2d66e025
MB
4948 int left, right;
4949 while ( iter )
f85afd4e 4950 {
2d66e025 4951 r = iter.GetRect();
f85afd4e 4952
2d66e025
MB
4953 // TODO: remove this when we can...
4954 // There is a bug in wxMotif that gives garbage update
4955 // rectangles if you jump-scroll a long way by clicking the
4956 // scrollbar with middle button. This is a work-around
4957 //
4958#if defined(__WXMOTIF__)
4959 int cw, ch;
4960 m_gridWin->GetClientSize( &cw, &ch );
4961 if ( r.GetLeft() > cw ) r.SetLeft( 0 );
4962 r.SetRight( wxMin( r.GetRight(), cw ) );
4963#endif
4964
4965 // logical bounds of update region
4966 //
4967 int dummy;
4968 CalcUnscrolledPosition( r.GetLeft(), 0, &left, &dummy );
4969 CalcUnscrolledPosition( r.GetRight(), 0, &right, &dummy );
4970
4971 // find the cells within these bounds
4972 //
4973 int col;
33188aa4 4974 for ( col = internalXToCol(left); col < m_numCols; col++ )
2d66e025 4975 {
7c1cb261
VZ
4976 if ( GetColRight(col) < left )
4977 continue;
60ff3b99 4978
7c1cb261
VZ
4979 if ( GetColLeft(col) > right )
4980 break;
2d66e025 4981
d10f4bf9 4982 colLabels.Add( col );
2d66e025 4983 }
60ff3b99 4984
60d8e886 4985 ++iter;
f85afd4e 4986 }
2f024384 4987
d10f4bf9 4988 return colLabels;
f85afd4e
MB
4989}
4990
d10f4bf9 4991wxGridCellCoordsArray wxGrid::CalcCellsExposed( const wxRegion& reg )
f85afd4e 4992{
2d66e025
MB
4993 wxRegionIterator iter( reg );
4994 wxRect r;
f85afd4e 4995
d10f4bf9 4996 wxGridCellCoordsArray cellsExposed;
f85afd4e 4997
2d66e025
MB
4998 int left, top, right, bottom;
4999 while ( iter )
5000 {
5001 r = iter.GetRect();
f85afd4e 5002
2d66e025
MB
5003 // TODO: remove this when we can...
5004 // There is a bug in wxMotif that gives garbage update
5005 // rectangles if you jump-scroll a long way by clicking the
5006 // scrollbar with middle button. This is a work-around
5007 //
5008#if defined(__WXMOTIF__)
f85afd4e 5009 int cw, ch;
2d66e025
MB
5010 m_gridWin->GetClientSize( &cw, &ch );
5011 if ( r.GetTop() > ch ) r.SetTop( 0 );
5012 if ( r.GetLeft() > cw ) r.SetLeft( 0 );
5013 r.SetRight( wxMin( r.GetRight(), cw ) );
5014 r.SetBottom( wxMin( r.GetBottom(), ch ) );
5015#endif
8f177c8e 5016
2d66e025
MB
5017 // logical bounds of update region
5018 //
5019 CalcUnscrolledPosition( r.GetLeft(), r.GetTop(), &left, &top );
5020 CalcUnscrolledPosition( r.GetRight(), r.GetBottom(), &right, &bottom );
f85afd4e 5021
2d66e025 5022 // find the cells within these bounds
f85afd4e 5023 //
2d66e025 5024 int row, col;
33188aa4 5025 for ( row = internalYToRow(top); row < m_numRows; row++ )
f85afd4e 5026 {
7c1cb261
VZ
5027 if ( GetRowBottom(row) <= top )
5028 continue;
f85afd4e 5029
7c1cb261
VZ
5030 if ( GetRowTop(row) > bottom )
5031 break;
60ff3b99 5032
33188aa4 5033 for ( col = internalXToCol(left); col < m_numCols; col++ )
2d66e025 5034 {
7c1cb261
VZ
5035 if ( GetColRight(col) <= left )
5036 continue;
60ff3b99 5037
7c1cb261
VZ
5038 if ( GetColLeft(col) > right )
5039 break;
60ff3b99 5040
d10f4bf9 5041 cellsExposed.Add( wxGridCellCoords( row, col ) );
2d66e025
MB
5042 }
5043 }
60ff3b99 5044
60d8e886 5045 ++iter;
f85afd4e 5046 }
d10f4bf9
VZ
5047
5048 return cellsExposed;
f85afd4e
MB
5049}
5050
5051
2d66e025 5052void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent& event )
f85afd4e 5053{
2d66e025
MB
5054 int x, y, row;
5055 wxPoint pos( event.GetPosition() );
5056 CalcUnscrolledPosition( pos.x, pos.y, &x, &y );
60ff3b99 5057
2d66e025 5058 if ( event.Dragging() )
f85afd4e 5059 {
426b2d87
JS
5060 if (!m_isDragging)
5061 {
ca65c044 5062 m_isDragging = true;
426b2d87
JS
5063 m_rowLabelWin->CaptureMouse();
5064 }
8f177c8e 5065
2d66e025 5066 if ( event.LeftIsDown() )
f85afd4e 5067 {
962a48f6 5068 switch ( m_cursorMode )
f85afd4e 5069 {
f85afd4e
MB
5070 case WXGRID_CURSOR_RESIZE_ROW:
5071 {
2d66e025
MB
5072 int cw, ch, left, dummy;
5073 m_gridWin->GetClientSize( &cw, &ch );
5074 CalcUnscrolledPosition( 0, 0, &left, &dummy );
60ff3b99 5075
2d66e025
MB
5076 wxClientDC dc( m_gridWin );
5077 PrepareDC( dc );
af547d51
VZ
5078 y = wxMax( y,
5079 GetRowTop(m_dragRowOrCol) +
5080 GetRowMinimalHeight(m_dragRowOrCol) );
d2fdd8d2 5081 dc.SetLogicalFunction(wxINVERT);
f85afd4e
MB
5082 if ( m_dragLastPos >= 0 )
5083 {
2d66e025 5084 dc.DrawLine( left, m_dragLastPos, left+cw, m_dragLastPos );
f85afd4e 5085 }
2d66e025
MB
5086 dc.DrawLine( left, y, left+cw, y );
5087 m_dragLastPos = y;
f85afd4e
MB
5088 }
5089 break;
5090
5091 case WXGRID_CURSOR_SELECT_ROW:
902725ee 5092 {
e32352cf 5093 if ( (row = YToRow( y )) >= 0 )
aa5e1f75 5094 {
3f3dc2ef
VZ
5095 if ( m_selection )
5096 {
5097 m_selection->SelectRow( row,
5098 event.ControlDown(),
5099 event.ShiftDown(),
5100 event.AltDown(),
5101 event.MetaDown() );
5102 }
f85afd4e 5103 }
902725ee
WS
5104 }
5105 break;
e2b42eeb
VZ
5106
5107 // default label to suppress warnings about "enumeration value
5108 // 'xxx' not handled in switch
5109 default:
5110 break;
f85afd4e
MB
5111 }
5112 }
5113 return;
5114 }
5115
426b2d87
JS
5116 if ( m_isDragging && (event.Entering() || event.Leaving()) )
5117 return;
8f177c8e 5118
426b2d87
JS
5119 if (m_isDragging)
5120 {
ccdee36f
DS
5121 if (m_rowLabelWin->HasCapture())
5122 m_rowLabelWin->ReleaseMouse();
ca65c044 5123 m_isDragging = false;
426b2d87 5124 }
60ff3b99 5125
6d004f67
MB
5126 // ------------ Entering or leaving the window
5127 //
5128 if ( event.Entering() || event.Leaving() )
5129 {
e2b42eeb 5130 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_rowLabelWin);
6d004f67
MB
5131 }
5132
2d66e025 5133 // ------------ Left button pressed
f85afd4e 5134 //
6d004f67 5135 else if ( event.LeftDown() )
f85afd4e 5136 {
2d66e025
MB
5137 // don't send a label click event for a hit on the
5138 // edge of the row label - this is probably the user
5139 // wanting to resize the row
5140 //
5141 if ( YToEdgeOfRow(y) < 0 )
f85afd4e 5142 {
2d66e025 5143 row = YToRow(y);
2f024384 5144 if ( row >= 0 &&
b54ba671 5145 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK, row, -1, event ) )
f85afd4e 5146 {
2b01fd49 5147 if ( !event.ShiftDown() && !event.CmdDown() )
aa5e1f75 5148 ClearSelection();
64e15340 5149 if ( m_selection )
3f3dc2ef
VZ
5150 {
5151 if ( event.ShiftDown() )
5152 {
5153 m_selection->SelectBlock( m_currentCellCoords.GetRow(),
5154 0,
5155 row,
5156 GetNumberCols() - 1,
5157 event.ControlDown(),
5158 event.ShiftDown(),
5159 event.AltDown(),
5160 event.MetaDown() );
5161 }
5162 else
5163 {
5164 m_selection->SelectRow( row,
5165 event.ControlDown(),
5166 event.ShiftDown(),
5167 event.AltDown(),
5168 event.MetaDown() );
5169 }
5170 }
5171
e2b42eeb 5172 ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW, m_rowLabelWin);
f85afd4e 5173 }
2d66e025
MB
5174 }
5175 else
5176 {
5177 // starting to drag-resize a row
6e8524b1
MB
5178 if ( CanDragRowSize() )
5179 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW, m_rowLabelWin);
2d66e025
MB
5180 }
5181 }
f85afd4e 5182
2d66e025
MB
5183 // ------------ Left double click
5184 //
5185 else if (event.LeftDClick() )
5186 {
4e115ed2 5187 row = YToEdgeOfRow(y);
d43851f7 5188 if ( row < 0 )
2d66e025
MB
5189 {
5190 row = YToRow(y);
a967f048 5191 if ( row >=0 &&
ca65c044
WS
5192 !SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK, row, -1, event ) )
5193 {
a967f048 5194 // no default action at the moment
ca65c044 5195 }
f85afd4e 5196 }
d43851f7
JS
5197 else
5198 {
5199 // adjust row height depending on label text
5200 AutoSizeRowLabelSize( row );
5201
5202 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_colLabelWin);
ccdee36f 5203 m_dragLastPos = -1;
d43851f7 5204 }
f85afd4e 5205 }
60ff3b99 5206
2d66e025 5207 // ------------ Left button released
f85afd4e 5208 //
2d66e025 5209 else if ( event.LeftUp() )
f85afd4e 5210 {
2d66e025 5211 if ( m_cursorMode == WXGRID_CURSOR_RESIZE_ROW )
f85afd4e 5212 {
6d004f67 5213 DoEndDragResizeRow();
60ff3b99 5214
6d004f67
MB
5215 // Note: we are ending the event *after* doing
5216 // default processing in this case
5217 //
b54ba671 5218 SendEvent( wxEVT_GRID_ROW_SIZE, m_dragRowOrCol, -1, event );
2d66e025 5219 }
f85afd4e 5220
e2b42eeb
VZ
5221 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_rowLabelWin);
5222 m_dragLastPos = -1;
2d66e025 5223 }
f85afd4e 5224
2d66e025
MB
5225 // ------------ Right button down
5226 //
5227 else if ( event.RightDown() )
5228 {
5229 row = YToRow(y);
ef5df12b 5230 if ( row >=0 &&
ca65c044 5231 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK, row, -1, event ) )
2d66e025
MB
5232 {
5233 // no default action at the moment
f85afd4e
MB
5234 }
5235 }
60ff3b99 5236
2d66e025 5237 // ------------ Right double click
f85afd4e 5238 //
2d66e025
MB
5239 else if ( event.RightDClick() )
5240 {
5241 row = YToRow(y);
a967f048 5242 if ( row >= 0 &&
ca65c044 5243 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK, row, -1, event ) )
2d66e025
MB
5244 {
5245 // no default action at the moment
5246 }
5247 }
60ff3b99 5248
2d66e025 5249 // ------------ No buttons down and mouse moving
f85afd4e 5250 //
2d66e025 5251 else if ( event.Moving() )
f85afd4e 5252 {
2d66e025
MB
5253 m_dragRowOrCol = YToEdgeOfRow( y );
5254 if ( m_dragRowOrCol >= 0 )
8f177c8e 5255 {
2d66e025 5256 if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
8f177c8e 5257 {
e2b42eeb 5258 // don't capture the mouse yet
6e8524b1 5259 if ( CanDragRowSize() )
ca65c044 5260 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW, m_rowLabelWin, false);
8f177c8e 5261 }
2d66e025 5262 }
6d004f67 5263 else if ( m_cursorMode != WXGRID_CURSOR_SELECT_CELL )
2d66e025 5264 {
ca65c044 5265 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_rowLabelWin, false);
8f177c8e 5266 }
f85afd4e 5267 }
2d66e025
MB
5268}
5269
2d66e025
MB
5270void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent& event )
5271{
5272 int x, y, col;
5273 wxPoint pos( event.GetPosition() );
5274 CalcUnscrolledPosition( pos.x, pos.y, &x, &y );
60ff3b99 5275
2d66e025 5276 if ( event.Dragging() )
f85afd4e 5277 {
fe77cf60
JS
5278 if (!m_isDragging)
5279 {
ca65c044 5280 m_isDragging = true;
fe77cf60
JS
5281 m_colLabelWin->CaptureMouse();
5282 }
8f177c8e 5283
2d66e025 5284 if ( event.LeftIsDown() )
8f177c8e 5285 {
962a48f6 5286 switch ( m_cursorMode )
8f177c8e 5287 {
2d66e025 5288 case WXGRID_CURSOR_RESIZE_COL:
8f177c8e 5289 {
2d66e025
MB
5290 int cw, ch, dummy, top;
5291 m_gridWin->GetClientSize( &cw, &ch );
5292 CalcUnscrolledPosition( 0, 0, &dummy, &top );
60ff3b99 5293
2d66e025
MB
5294 wxClientDC dc( m_gridWin );
5295 PrepareDC( dc );
43947979
VZ
5296
5297 x = wxMax( x, GetColLeft(m_dragRowOrCol) +
5298 GetColMinimalWidth(m_dragRowOrCol));
d2fdd8d2 5299 dc.SetLogicalFunction(wxINVERT);
2d66e025
MB
5300 if ( m_dragLastPos >= 0 )
5301 {
ccdee36f 5302 dc.DrawLine( m_dragLastPos, top, m_dragLastPos, top + ch );
2d66e025 5303 }
ccdee36f 5304 dc.DrawLine( x, top, x, top + ch );
2d66e025 5305 m_dragLastPos = x;
f85afd4e 5306 }
2d66e025 5307 break;
f85afd4e 5308
2d66e025 5309 case WXGRID_CURSOR_SELECT_COL:
902725ee 5310 {
e32352cf 5311 if ( (col = XToCol( x )) >= 0 )
aa5e1f75 5312 {
3f3dc2ef
VZ
5313 if ( m_selection )
5314 {
5315 m_selection->SelectCol( col,
5316 event.ControlDown(),
5317 event.ShiftDown(),
5318 event.AltDown(),
5319 event.MetaDown() );
5320 }
2d66e025 5321 }
902725ee
WS
5322 }
5323 break;
e2b42eeb
VZ
5324
5325 // default label to suppress warnings about "enumeration value
5326 // 'xxx' not handled in switch
5327 default:
5328 break;
2d66e025 5329 }
f85afd4e 5330 }
2d66e025 5331 return;
f85afd4e 5332 }
2d66e025 5333
fe77cf60
JS
5334 if ( m_isDragging && (event.Entering() || event.Leaving()) )
5335 return;
2d66e025 5336
fe77cf60
JS
5337 if (m_isDragging)
5338 {
ccdee36f
DS
5339 if (m_colLabelWin->HasCapture())
5340 m_colLabelWin->ReleaseMouse();
ca65c044 5341 m_isDragging = false;
fe77cf60 5342 }
60ff3b99 5343
6d004f67
MB
5344 // ------------ Entering or leaving the window
5345 //
5346 if ( event.Entering() || event.Leaving() )
5347 {
e2b42eeb 5348 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_colLabelWin);
6d004f67
MB
5349 }
5350
2d66e025 5351 // ------------ Left button pressed
f85afd4e 5352 //
6d004f67 5353 else if ( event.LeftDown() )
f85afd4e 5354 {
2d66e025
MB
5355 // don't send a label click event for a hit on the
5356 // edge of the col label - this is probably the user
5357 // wanting to resize the col
5358 //
5359 if ( XToEdgeOfCol(x) < 0 )
8f177c8e 5360 {
2d66e025 5361 col = XToCol(x);
2f024384 5362 if ( col >= 0 &&
b54ba671 5363 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK, -1, col, event ) )
8f177c8e 5364 {
2b01fd49 5365 if ( !event.ShiftDown() && !event.CmdDown() )
aa5e1f75 5366 ClearSelection();
3f3dc2ef
VZ
5367 if ( m_selection )
5368 {
5369 if ( event.ShiftDown() )
5370 {
5371 m_selection->SelectBlock( 0,
5372 m_currentCellCoords.GetCol(),
5373 GetNumberRows() - 1, col,
5374 event.ControlDown(),
5375 event.ShiftDown(),
5376 event.AltDown(),
5377 event.MetaDown() );
5378 }
5379 else
5380 {
5381 m_selection->SelectCol( col,
5382 event.ControlDown(),
5383 event.ShiftDown(),
5384 event.AltDown(),
5385 event.MetaDown() );
5386 }
5387 }
5388
e2b42eeb 5389 ChangeCursorMode(WXGRID_CURSOR_SELECT_COL, m_colLabelWin);
f85afd4e 5390 }
2d66e025
MB
5391 }
5392 else
5393 {
5394 // starting to drag-resize a col
5395 //
6e8524b1
MB
5396 if ( CanDragColSize() )
5397 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL, m_colLabelWin);
2d66e025
MB
5398 }
5399 }
f85afd4e 5400
2d66e025
MB
5401 // ------------ Left double click
5402 //
5403 if ( event.LeftDClick() )
5404 {
4e115ed2 5405 col = XToEdgeOfCol(x);
d43851f7 5406 if ( col < 0 )
2d66e025
MB
5407 {
5408 col = XToCol(x);
a967f048
RG
5409 if ( col >= 0 &&
5410 ! SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK, -1, col, event ) )
5411 {
ca65c044 5412 // no default action at the moment
a967f048 5413 }
2d66e025 5414 }
d43851f7
JS
5415 else
5416 {
5417 // adjust column width depending on label text
5418 AutoSizeColLabelSize( col );
5419
5420 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_colLabelWin);
ccdee36f 5421 m_dragLastPos = -1;
d43851f7 5422 }
2d66e025 5423 }
60ff3b99 5424
2d66e025
MB
5425 // ------------ Left button released
5426 //
5427 else if ( event.LeftUp() )
5428 {
5429 if ( m_cursorMode == WXGRID_CURSOR_RESIZE_COL )
5430 {
6d004f67 5431 DoEndDragResizeCol();
e2b42eeb 5432
6d004f67
MB
5433 // Note: we are ending the event *after* doing
5434 // default processing in this case
5435 //
b54ba671 5436 SendEvent( wxEVT_GRID_COL_SIZE, -1, m_dragRowOrCol, event );
2d66e025 5437 }
f85afd4e 5438
e2b42eeb 5439 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_colLabelWin);
ccdee36f 5440 m_dragLastPos = -1;
60ff3b99
VZ
5441 }
5442
2d66e025
MB
5443 // ------------ Right button down
5444 //
5445 else if ( event.RightDown() )
5446 {
5447 col = XToCol(x);
a967f048 5448 if ( col >= 0 &&
ca65c044 5449 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK, -1, col, event ) )
2d66e025
MB
5450 {
5451 // no default action at the moment
f85afd4e
MB
5452 }
5453 }
60ff3b99 5454
2d66e025 5455 // ------------ Right double click
f85afd4e 5456 //
2d66e025
MB
5457 else if ( event.RightDClick() )
5458 {
5459 col = XToCol(x);
a967f048 5460 if ( col >= 0 &&
ca65c044 5461 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK, -1, col, event ) )
2d66e025
MB
5462 {
5463 // no default action at the moment
5464 }
5465 }
60ff3b99 5466
2d66e025 5467 // ------------ No buttons down and mouse moving
f85afd4e 5468 //
2d66e025 5469 else if ( event.Moving() )
f85afd4e 5470 {
2d66e025
MB
5471 m_dragRowOrCol = XToEdgeOfCol( x );
5472 if ( m_dragRowOrCol >= 0 )
f85afd4e 5473 {
2d66e025 5474 if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
f85afd4e 5475 {
e2b42eeb 5476 // don't capture the cursor yet
6e8524b1 5477 if ( CanDragColSize() )
ca65c044 5478 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL, m_colLabelWin, false);
f85afd4e 5479 }
2d66e025 5480 }
6d004f67 5481 else if ( m_cursorMode != WXGRID_CURSOR_SELECT_CELL )
2d66e025 5482 {
ca65c044 5483 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_colLabelWin, false);
8f177c8e 5484 }
f85afd4e
MB
5485 }
5486}
5487
2d66e025 5488void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent& event )
f85afd4e 5489{
2d66e025 5490 if ( event.LeftDown() )
f85afd4e 5491 {
2d66e025
MB
5492 // indicate corner label by having both row and
5493 // col args == -1
f85afd4e 5494 //
b54ba671 5495 if ( !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK, -1, -1, event ) )
2d66e025
MB
5496 {
5497 SelectAll();
5498 }
f85afd4e 5499 }
2d66e025
MB
5500 else if ( event.LeftDClick() )
5501 {
b54ba671 5502 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK, -1, -1, event );
2d66e025 5503 }
2d66e025 5504 else if ( event.RightDown() )
f85afd4e 5505 {
b54ba671 5506 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK, -1, -1, event ) )
f85afd4e 5507 {
2d66e025
MB
5508 // no default action at the moment
5509 }
5510 }
2d66e025
MB
5511 else if ( event.RightDClick() )
5512 {
b54ba671 5513 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK, -1, -1, event ) )
2d66e025
MB
5514 {
5515 // no default action at the moment
5516 }
5517 }
5518}
f85afd4e 5519
e2b42eeb
VZ
5520void wxGrid::ChangeCursorMode(CursorMode mode,
5521 wxWindow *win,
5522 bool captureMouse)
5523{
5524#ifdef __WXDEBUG__
5525 static const wxChar *cursorModes[] =
5526 {
5527 _T("SELECT_CELL"),
5528 _T("RESIZE_ROW"),
5529 _T("RESIZE_COL"),
5530 _T("SELECT_ROW"),
5531 _T("SELECT_COL")
5532 };
5533
181bfffd
VZ
5534 wxLogTrace(_T("grid"),
5535 _T("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
e2b42eeb
VZ
5536 win == m_colLabelWin ? _T("colLabelWin")
5537 : win ? _T("rowLabelWin")
5538 : _T("gridWin"),
5539 cursorModes[m_cursorMode], cursorModes[mode]);
2f024384 5540#endif
e2b42eeb 5541
faec5a43
SN
5542 if ( mode == m_cursorMode &&
5543 win == m_winCapture &&
5544 captureMouse == (m_winCapture != NULL))
e2b42eeb
VZ
5545 return;
5546
5547 if ( !win )
5548 {
5549 // by default use the grid itself
5550 win = m_gridWin;
5551 }
5552
5553 if ( m_winCapture )
5554 {
2f024384
DS
5555 if (m_winCapture->HasCapture())
5556 m_winCapture->ReleaseMouse();
e2b42eeb
VZ
5557 m_winCapture = (wxWindow *)NULL;
5558 }
5559
5560 m_cursorMode = mode;
5561
5562 switch ( m_cursorMode )
5563 {
5564 case WXGRID_CURSOR_RESIZE_ROW:
5565 win->SetCursor( m_rowResizeCursor );
5566 break;
5567
5568 case WXGRID_CURSOR_RESIZE_COL:
5569 win->SetCursor( m_colResizeCursor );
5570 break;
5571
5572 default:
5573 win->SetCursor( *wxSTANDARD_CURSOR );
2f024384 5574 break;
e2b42eeb
VZ
5575 }
5576
5577 // we need to capture mouse when resizing
5578 bool resize = m_cursorMode == WXGRID_CURSOR_RESIZE_ROW ||
5579 m_cursorMode == WXGRID_CURSOR_RESIZE_COL;
5580
5581 if ( captureMouse && resize )
5582 {
5583 win->CaptureMouse();
5584 m_winCapture = win;
5585 }
5586}
8f177c8e 5587
2d66e025
MB
5588void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent& event )
5589{
5590 int x, y;
5591 wxPoint pos( event.GetPosition() );
5592 CalcUnscrolledPosition( pos.x, pos.y, &x, &y );
60ff3b99 5593
2d66e025
MB
5594 wxGridCellCoords coords;
5595 XYToCell( x, y, coords );
60ff3b99 5596
27f35b66 5597 int cell_rows, cell_cols;
79dbea21 5598 bool isFirstDrag = !m_isDragging;
27f35b66
SN
5599 GetCellSize( coords.GetRow(), coords.GetCol(), &cell_rows, &cell_cols );
5600 if ((cell_rows < 0) || (cell_cols < 0))
5601 {
5602 coords.SetRow(coords.GetRow() + cell_rows);
5603 coords.SetCol(coords.GetCol() + cell_cols);
5604 }
5605
2d66e025
MB
5606 if ( event.Dragging() )
5607 {
07296f0b
RD
5608 //wxLogDebug("pos(%d, %d) coords(%d, %d)", pos.x, pos.y, coords.GetRow(), coords.GetCol());
5609
962a48f6 5610 // Don't start doing anything until the mouse has been dragged at
07296f0b 5611 // least 3 pixels in any direction...
508011ce
VZ
5612 if (! m_isDragging)
5613 {
5614 if (m_startDragPos == wxDefaultPosition)
5615 {
07296f0b
RD
5616 m_startDragPos = pos;
5617 return;
5618 }
5619 if (abs(m_startDragPos.x - pos.x) < 4 && abs(m_startDragPos.y - pos.y) < 4)
5620 return;
5621 }
5622
ca65c044 5623 m_isDragging = true;
2d66e025
MB
5624 if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
5625 {
f0102d2a 5626 // Hide the edit control, so it
4db6714b 5627 // won't interfere with drag-shrinking.
f6bcfd97 5628 if ( IsCellEditControlShown() )
a5777624 5629 {
f0102d2a 5630 HideCellEditControl();
a5777624
RD
5631 SaveEditControlValue();
5632 }
07296f0b
RD
5633
5634 // Have we captured the mouse yet?
508011ce
VZ
5635 if (! m_winCapture)
5636 {
07296f0b
RD
5637 m_winCapture = m_gridWin;
5638 m_winCapture->CaptureMouse();
5639 }
5640
2d66e025
MB
5641 if ( coords != wxGridNoCellCoords )
5642 {
2b01fd49 5643 if ( event.CmdDown() )
aa5e1f75
SN
5644 {
5645 if ( m_selectingKeyboard == wxGridNoCellCoords)
5646 m_selectingKeyboard = coords;
a9339fe2 5647 HighlightBlock( m_selectingKeyboard, coords );
aa5e1f75 5648 }
79dbea21
RD
5649 else if ( CanDragCell() )
5650 {
5651 if ( isFirstDrag )
5652 {
5653 if ( m_selectingKeyboard == wxGridNoCellCoords)
5654 m_selectingKeyboard = coords;
5655
5656 SendEvent( wxEVT_GRID_CELL_BEGIN_DRAG,
5657 coords.GetRow(),
5658 coords.GetCol(),
5659 event );
5660 }
5661 }
aa5e1f75
SN
5662 else
5663 {
5664 if ( !IsSelection() )
5665 {
c9097836 5666 HighlightBlock( coords, coords );
aa5e1f75
SN
5667 }
5668 else
5669 {
c9097836 5670 HighlightBlock( m_currentCellCoords, coords );
aa5e1f75 5671 }
f85afd4e 5672 }
07296f0b 5673
508011ce
VZ
5674 if (! IsVisible(coords))
5675 {
07296f0b
RD
5676 MakeCellVisible(coords);
5677 // TODO: need to introduce a delay or something here. The
e32352cf 5678 // scrolling is way to fast, at least on MSW - also on GTK.
07296f0b 5679 }
2d66e025
MB
5680 }
5681 }
6d004f67
MB
5682 else if ( m_cursorMode == WXGRID_CURSOR_RESIZE_ROW )
5683 {
5684 int cw, ch, left, dummy;
5685 m_gridWin->GetClientSize( &cw, &ch );
5686 CalcUnscrolledPosition( 0, 0, &left, &dummy );
8f177c8e 5687
6d004f67
MB
5688 wxClientDC dc( m_gridWin );
5689 PrepareDC( dc );
a95e38c0
VZ
5690 y = wxMax( y, GetRowTop(m_dragRowOrCol) +
5691 GetRowMinimalHeight(m_dragRowOrCol) );
6d004f67
MB
5692 dc.SetLogicalFunction(wxINVERT);
5693 if ( m_dragLastPos >= 0 )
5694 {
5695 dc.DrawLine( left, m_dragLastPos, left+cw, m_dragLastPos );
5696 }
5697 dc.DrawLine( left, y, left+cw, y );
5698 m_dragLastPos = y;
5699 }
5700 else if ( m_cursorMode == WXGRID_CURSOR_RESIZE_COL )
5701 {
5702 int cw, ch, dummy, top;
5703 m_gridWin->GetClientSize( &cw, &ch );
5704 CalcUnscrolledPosition( 0, 0, &dummy, &top );
e2b42eeb 5705
6d004f67
MB
5706 wxClientDC dc( m_gridWin );
5707 PrepareDC( dc );
43947979
VZ
5708 x = wxMax( x, GetColLeft(m_dragRowOrCol) +
5709 GetColMinimalWidth(m_dragRowOrCol) );
6d004f67
MB
5710 dc.SetLogicalFunction(wxINVERT);
5711 if ( m_dragLastPos >= 0 )
5712 {
a9339fe2 5713 dc.DrawLine( m_dragLastPos, top, m_dragLastPos, top + ch );
6d004f67 5714 }
a9339fe2 5715 dc.DrawLine( x, top, x, top + ch );
6d004f67
MB
5716 m_dragLastPos = x;
5717 }
e2b42eeb 5718
2d66e025
MB
5719 return;
5720 }
66242c80 5721
ca65c044 5722 m_isDragging = false;
07296f0b
RD
5723 m_startDragPos = wxDefaultPosition;
5724
a5777624
RD
5725 // VZ: if we do this, the mode is reset to WXGRID_CURSOR_SELECT_CELL
5726 // immediately after it becomes WXGRID_CURSOR_RESIZE_ROW/COL under
5727 // wxGTK
e2b42eeb 5728#if 0
a5777624
RD
5729 if ( event.Entering() || event.Leaving() )
5730 {
5731 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL);
5732 m_gridWin->SetCursor( *wxSTANDARD_CURSOR );
5733 }
5734 else
e2b42eeb
VZ
5735#endif // 0
5736
a5777624
RD
5737 // ------------ Left button pressed
5738 //
5739 if ( event.LeftDown() && coords != wxGridNoCellCoords )
f6bcfd97
BP
5740 {
5741 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_CLICK,
5742 coords.GetRow(),
5743 coords.GetCol(),
5744 event ) )
a5777624 5745 {
2b01fd49 5746 if ( !event.CmdDown() )
f6bcfd97
BP
5747 ClearSelection();
5748 if ( event.ShiftDown() )
5749 {
3f3dc2ef
VZ
5750 if ( m_selection )
5751 {
5752 m_selection->SelectBlock( m_currentCellCoords.GetRow(),
5753 m_currentCellCoords.GetCol(),
5754 coords.GetRow(),
5755 coords.GetCol(),
5756 event.ControlDown(),
5757 event.ShiftDown(),
5758 event.AltDown(),
5759 event.MetaDown() );
5760 }
f6bcfd97 5761 }
2f024384 5762 else if ( XToEdgeOfCol(x) < 0 &&
f6bcfd97 5763 YToEdgeOfRow(y) < 0 )
58dd5b3b 5764 {
a5777624
RD
5765 DisableCellEditControl();
5766 MakeCellVisible( coords );
5767
2b01fd49 5768 if ( event.CmdDown() )
58dd5b3b 5769 {
73145b0e
JS
5770 if ( m_selection )
5771 {
5772 m_selection->ToggleCellSelection( coords.GetRow(),
5773 coords.GetCol(),
5774 event.ControlDown(),
5775 event.ShiftDown(),
5776 event.AltDown(),
5777 event.MetaDown() );
5778 }
5779 m_selectingTopLeft = wxGridNoCellCoords;
5780 m_selectingBottomRight = wxGridNoCellCoords;
5781 m_selectingKeyboard = coords;
a5777624
RD
5782 }
5783 else
5784 {
73145b0e
JS
5785 m_waitForSlowClick = m_currentCellCoords == coords && coords != wxGridNoCellCoords;
5786 SetCurrentCell( coords );
5787 if ( m_selection )
aa5e1f75 5788 {
73145b0e
JS
5789 if ( m_selection->GetSelectionMode() !=
5790 wxGrid::wxGridSelectCells )
3f3dc2ef 5791 {
73145b0e 5792 HighlightBlock( coords, coords );
3f3dc2ef 5793 }
aa5e1f75 5794 }
58dd5b3b
MB
5795 }
5796 }
f85afd4e 5797 }
a5777624 5798 }
f85afd4e 5799
a5777624
RD
5800 // ------------ Left double click
5801 //
5802 else if ( event.LeftDClick() && coords != wxGridNoCellCoords )
5803 {
5804 DisableCellEditControl();
5805
2f024384 5806 if ( XToEdgeOfCol(x) < 0 && YToEdgeOfRow(y) < 0 )
58dd5b3b 5807 {
1ef49ab5
VS
5808 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_DCLICK,
5809 coords.GetRow(),
5810 coords.GetCol(),
5811 event ) )
5812 {
5813 // we want double click to select a cell and start editing
5814 // (i.e. to behave in same way as sequence of two slow clicks):
5815 m_waitForSlowClick = true;
5816 }
58dd5b3b 5817 }
a5777624 5818 }
f85afd4e 5819
a5777624
RD
5820 // ------------ Left button released
5821 //
5822 else if ( event.LeftUp() )
5823 {
5824 if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
f85afd4e 5825 {
f40f9976
JS
5826 if (m_winCapture)
5827 {
2f024384
DS
5828 if (m_winCapture->HasCapture())
5829 m_winCapture->ReleaseMouse();
f40f9976
JS
5830 m_winCapture = NULL;
5831 }
5832
bee19958 5833 if ( coords == m_currentCellCoords && m_waitForSlowClick && CanEnableCellControl() )
932b55d0
JS
5834 {
5835 ClearSelection();
5836 EnableCellEditControl();
5837
2f024384 5838 wxGridCellAttr *attr = GetCellAttr(coords);
932b55d0
JS
5839 wxGridCellEditor *editor = attr->GetEditor(this, coords.GetRow(), coords.GetCol());
5840 editor->StartingClick();
5841 editor->DecRef();
5842 attr->DecRef();
5843
ca65c044 5844 m_waitForSlowClick = false;
932b55d0
JS
5845 }
5846 else if ( m_selectingTopLeft != wxGridNoCellCoords &&
b5808881 5847 m_selectingBottomRight != wxGridNoCellCoords )
52068ea5 5848 {
3f3dc2ef
VZ
5849 if ( m_selection )
5850 {
5851 m_selection->SelectBlock( m_selectingTopLeft.GetRow(),
5852 m_selectingTopLeft.GetCol(),
5853 m_selectingBottomRight.GetRow(),
5854 m_selectingBottomRight.GetCol(),
5855 event.ControlDown(),
5856 event.ShiftDown(),
5857 event.AltDown(),
5858 event.MetaDown() );
5859 }
5860
5c8fc7c1
SN
5861 m_selectingTopLeft = wxGridNoCellCoords;
5862 m_selectingBottomRight = wxGridNoCellCoords;
2d66e025 5863
73145b0e
JS
5864 // Show the edit control, if it has been hidden for
5865 // drag-shrinking.
5866 ShowCellEditControl();
5867 }
a5777624
RD
5868 }
5869 else if ( m_cursorMode == WXGRID_CURSOR_RESIZE_ROW )
5870 {
5871 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL);
5872 DoEndDragResizeRow();
e2b42eeb 5873
a5777624
RD
5874 // Note: we are ending the event *after* doing
5875 // default processing in this case
5876 //
5877 SendEvent( wxEVT_GRID_ROW_SIZE, m_dragRowOrCol, -1, event );
5878 }
5879 else if ( m_cursorMode == WXGRID_CURSOR_RESIZE_COL )
5880 {
5881 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL);
5882 DoEndDragResizeCol();
e2b42eeb 5883
a5777624
RD
5884 // Note: we are ending the event *after* doing
5885 // default processing in this case
5886 //
5887 SendEvent( wxEVT_GRID_COL_SIZE, -1, m_dragRowOrCol, event );
58dd5b3b 5888 }
f85afd4e 5889
a5777624
RD
5890 m_dragLastPos = -1;
5891 }
5892
a5777624
RD
5893 // ------------ Right button down
5894 //
5895 else if ( event.RightDown() && coords != wxGridNoCellCoords )
5896 {
5897 DisableCellEditControl();
5898 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_CLICK,
5899 coords.GetRow(),
5900 coords.GetCol(),
5901 event ) )
f85afd4e 5902 {
a5777624 5903 // no default action at the moment
60ff3b99 5904 }
a5777624 5905 }
2d66e025 5906
a5777624
RD
5907 // ------------ Right double click
5908 //
5909 else if ( event.RightDClick() && coords != wxGridNoCellCoords )
5910 {
5911 DisableCellEditControl();
5912 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_DCLICK,
5913 coords.GetRow(),
5914 coords.GetCol(),
5915 event ) )
f85afd4e 5916 {
a5777624 5917 // no default action at the moment
60ff3b99 5918 }
a5777624
RD
5919 }
5920
5921 // ------------ Moving and no button action
5922 //
5923 else if ( event.Moving() && !event.IsButton() )
5924 {
4db6714b 5925 if ( coords.GetRow() < 0 || coords.GetCol() < 0 )
d57ad377
SN
5926 {
5927 // out of grid cell area
5928 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL);
5929 return;
5930 }
5931
a5777624
RD
5932 int dragRow = YToEdgeOfRow( y );
5933 int dragCol = XToEdgeOfCol( x );
790cc417 5934
a5777624
RD
5935 // Dragging on the corner of a cell to resize in both
5936 // directions is not implemented yet...
790cc417 5937 //
91894db3 5938 if ( dragRow >= 0 && dragCol >= 0 )
790cc417 5939 {
a5777624
RD
5940 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL);
5941 return;
5942 }
6d004f67 5943
d57ad377 5944 if ( dragRow >= 0 )
a5777624
RD
5945 {
5946 m_dragRowOrCol = dragRow;
6d004f67 5947
a5777624 5948 if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
6d004f67 5949 {
a5777624
RD
5950 if ( CanDragRowSize() && CanDragGridSize() )
5951 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW);
6d004f67 5952 }
a5777624 5953 }
91894db3 5954 else if ( dragCol >= 0 )
a5777624
RD
5955 {
5956 m_dragRowOrCol = dragCol;
e2b42eeb 5957
a5777624 5958 if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
6d004f67 5959 {
a5777624
RD
5960 if ( CanDragColSize() && CanDragGridSize() )
5961 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL);
6d004f67
MB
5962 }
5963 }
91894db3 5964 else // Neither on a row or col edge
a5777624 5965 {
91894db3
VZ
5966 if ( m_cursorMode != WXGRID_CURSOR_SELECT_CELL )
5967 {
5968 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL);
5969 }
a5777624
RD
5970 }
5971 }
6d004f67
MB
5972}
5973
6d004f67
MB
5974void wxGrid::DoEndDragResizeRow()
5975{
5976 if ( m_dragLastPos >= 0 )
5977 {
5978 // erase the last line and resize the row
5979 //
5980 int cw, ch, left, dummy;
5981 m_gridWin->GetClientSize( &cw, &ch );
5982 CalcUnscrolledPosition( 0, 0, &left, &dummy );
5983
5984 wxClientDC dc( m_gridWin );
5985 PrepareDC( dc );
5986 dc.SetLogicalFunction( wxINVERT );
a9339fe2 5987 dc.DrawLine( left, m_dragLastPos, left + cw, m_dragLastPos );
6d004f67 5988 HideCellEditControl();
a5777624 5989 SaveEditControlValue();
6d004f67 5990
7c1cb261 5991 int rowTop = GetRowTop(m_dragRowOrCol);
6d004f67 5992 SetRowSize( m_dragRowOrCol,
b8d24d4e 5993 wxMax( m_dragLastPos - rowTop, m_minAcceptableRowHeight ) );
e2b42eeb 5994
6d004f67
MB
5995 if ( !GetBatchCount() )
5996 {
5997 // Only needed to get the correct rect.y:
5998 wxRect rect ( CellToRect( m_dragRowOrCol, 0 ) );
5999 rect.x = 0;
6000 CalcScrolledPosition(0, rect.y, &dummy, &rect.y);
6001 rect.width = m_rowLabelWidth;
6002 rect.height = ch - rect.y;
ca65c044 6003 m_rowLabelWin->Refresh( true, &rect );
6d004f67 6004 rect.width = cw;
ccdee36f 6005
27f35b66
SN
6006 // if there is a multicell block, paint all of it
6007 if (m_table)
6008 {
6009 int i, cell_rows, cell_cols, subtract_rows = 0;
6010 int leftCol = XToCol(left);
a9339fe2 6011 int rightCol = internalXToCol(left + cw);
27f35b66
SN
6012 if (leftCol >= 0)
6013 {
27f35b66
SN
6014 for (i=leftCol; i<rightCol; i++)
6015 {
6016 GetCellSize(m_dragRowOrCol, i, &cell_rows, &cell_cols);
6017 if (cell_rows < subtract_rows)
6018 subtract_rows = cell_rows;
6019 }
6020 rect.y = GetRowTop(m_dragRowOrCol + subtract_rows);
6021 CalcScrolledPosition(0, rect.y, &dummy, &rect.y);
6022 rect.height = ch - rect.y;
6023 }
6024 }
ca65c044 6025 m_gridWin->Refresh( false, &rect );
6d004f67
MB
6026 }
6027
6028 ShowCellEditControl();
6029 }
6030}
6031
6032
6033void wxGrid::DoEndDragResizeCol()
6034{
6035 if ( m_dragLastPos >= 0 )
6036 {
6037 // erase the last line and resize the col
6038 //
6039 int cw, ch, dummy, top;
6040 m_gridWin->GetClientSize( &cw, &ch );
6041 CalcUnscrolledPosition( 0, 0, &dummy, &top );
6042
6043 wxClientDC dc( m_gridWin );
6044 PrepareDC( dc );
6045 dc.SetLogicalFunction( wxINVERT );
a9339fe2 6046 dc.DrawLine( m_dragLastPos, top, m_dragLastPos, top + ch );
6d004f67 6047 HideCellEditControl();
a5777624 6048 SaveEditControlValue();
6d004f67 6049
7c1cb261 6050 int colLeft = GetColLeft(m_dragRowOrCol);
6d004f67 6051 SetColSize( m_dragRowOrCol,
43947979
VZ
6052 wxMax( m_dragLastPos - colLeft,
6053 GetColMinimalWidth(m_dragRowOrCol) ) );
6d004f67
MB
6054
6055 if ( !GetBatchCount() )
6056 {
6057 // Only needed to get the correct rect.x:
6058 wxRect rect ( CellToRect( 0, m_dragRowOrCol ) );
6059 rect.y = 0;
6060 CalcScrolledPosition(rect.x, 0, &rect.x, &dummy);
6061 rect.width = cw - rect.x;
6062 rect.height = m_colLabelHeight;
ca65c044 6063 m_colLabelWin->Refresh( true, &rect );
6d004f67 6064 rect.height = ch;
2f024384 6065
27f35b66
SN
6066 // if there is a multicell block, paint all of it
6067 if (m_table)
6068 {
6069 int i, cell_rows, cell_cols, subtract_cols = 0;
6070 int topRow = YToRow(top);
a9339fe2 6071 int bottomRow = internalYToRow(top + cw);
27f35b66
SN
6072 if (topRow >= 0)
6073 {
27f35b66
SN
6074 for (i=topRow; i<bottomRow; i++)
6075 {
6076 GetCellSize(i, m_dragRowOrCol, &cell_rows, &cell_cols);
6077 if (cell_cols < subtract_cols)
6078 subtract_cols = cell_cols;
6079 }
a9339fe2 6080
27f35b66
SN
6081 rect.x = GetColLeft(m_dragRowOrCol + subtract_cols);
6082 CalcScrolledPosition(rect.x, 0, &rect.x, &dummy);
6083 rect.width = cw - rect.x;
6084 }
6085 }
2f024384 6086
ca65c044 6087 m_gridWin->Refresh( false, &rect );
790cc417 6088 }
e2b42eeb 6089
6d004f67 6090 ShowCellEditControl();
f85afd4e 6091 }
f85afd4e
MB
6092}
6093
2d66e025
MB
6094//
6095// ------ interaction with data model
6096//
6097bool wxGrid::ProcessTableMessage( wxGridTableMessage& msg )
f85afd4e 6098{
2d66e025 6099 switch ( msg.GetId() )
17732cec 6100 {
2d66e025
MB
6101 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES:
6102 return GetModelValues();
17732cec 6103
2d66e025
MB
6104 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES:
6105 return SetModelValues();
f85afd4e 6106
2d66e025
MB
6107 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED:
6108 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED:
6109 case wxGRIDTABLE_NOTIFY_ROWS_DELETED:
6110 case wxGRIDTABLE_NOTIFY_COLS_INSERTED:
6111 case wxGRIDTABLE_NOTIFY_COLS_APPENDED:
6112 case wxGRIDTABLE_NOTIFY_COLS_DELETED:
6113 return Redimension( msg );
6114
6115 default:
ca65c044 6116 return false;
f85afd4e 6117 }
2d66e025 6118}
f85afd4e 6119
2d66e025 6120// The behaviour of this function depends on the grid table class
5b061713 6121// Clear() function. For the default wxGridStringTable class the
2d66e025
MB
6122// behavious is to replace all cell contents with wxEmptyString but
6123// not to change the number of rows or cols.
6124//
6125void wxGrid::ClearGrid()
6126{
6127 if ( m_table )
f85afd4e 6128 {
4cfa5de6
RD
6129 if (IsCellEditControlEnabled())
6130 DisableCellEditControl();
6131
2d66e025 6132 m_table->Clear();
5b061713 6133 if (!GetBatchCount())
a9339fe2 6134 m_gridWin->Refresh();
f85afd4e
MB
6135 }
6136}
6137
2d66e025 6138bool wxGrid::InsertRows( int pos, int numRows, bool WXUNUSED(updateLabels) )
f85afd4e 6139{
2d66e025 6140 // TODO: something with updateLabels flag
8f177c8e 6141
2d66e025 6142 if ( !m_created )
f85afd4e 6143 {
bd3c6758 6144 wxFAIL_MSG( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") );
ca65c044 6145 return false;
2d66e025 6146 }
8f177c8e 6147
2d66e025
MB
6148 if ( m_table )
6149 {
b7fff980 6150 if (IsCellEditControlEnabled())
b54ba671 6151 DisableCellEditControl();
b7fff980 6152
4ea3479c 6153 bool done = m_table->InsertRows( pos, numRows );
4ea3479c 6154 return done;
f85afd4e 6155
2d66e025
MB
6156 // the table will have sent the results of the insert row
6157 // operation to this view object as a grid table message
f85afd4e 6158 }
a9339fe2 6159
ca65c044 6160 return false;
f85afd4e
MB
6161}
6162
2d66e025 6163bool wxGrid::AppendRows( int numRows, bool WXUNUSED(updateLabels) )
f85afd4e 6164{
2d66e025
MB
6165 // TODO: something with updateLabels flag
6166
6167 if ( !m_created )
f85afd4e 6168 {
bd3c6758 6169 wxFAIL_MSG( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") );
ca65c044 6170 return false;
2d66e025
MB
6171 }
6172
4ea3479c
JS
6173 if ( m_table )
6174 {
6175 bool done = m_table && m_table->AppendRows( numRows );
4ea3479c 6176 return done;
a9339fe2 6177
4ea3479c
JS
6178 // the table will have sent the results of the append row
6179 // operation to this view object as a grid table message
6180 }
a9339fe2 6181
ca65c044 6182 return false;
f85afd4e
MB
6183}
6184
2d66e025 6185bool wxGrid::DeleteRows( int pos, int numRows, bool WXUNUSED(updateLabels) )
f85afd4e 6186{
2d66e025
MB
6187 // TODO: something with updateLabels flag
6188
6189 if ( !m_created )
f85afd4e 6190 {
bd3c6758 6191 wxFAIL_MSG( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") );
ca65c044 6192 return false;
2d66e025
MB
6193 }
6194
b7fff980 6195 if ( m_table )
2d66e025 6196 {
b7fff980 6197 if (IsCellEditControlEnabled())
b54ba671 6198 DisableCellEditControl();
f85afd4e 6199
4ea3479c 6200 bool done = m_table->DeleteRows( pos, numRows );
4ea3479c 6201 return done;
f6bcfd97
BP
6202 // the table will have sent the results of the delete row
6203 // operation to this view object as a grid table message
2d66e025 6204 }
a9339fe2 6205
ca65c044 6206 return false;
2d66e025 6207}
f85afd4e 6208
2d66e025
MB
6209bool wxGrid::InsertCols( int pos, int numCols, bool WXUNUSED(updateLabels) )
6210{
6211 // TODO: something with updateLabels flag
8f177c8e 6212
2d66e025
MB
6213 if ( !m_created )
6214 {
bd3c6758 6215 wxFAIL_MSG( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") );
ca65c044 6216 return false;
2d66e025 6217 }
f85afd4e 6218
2d66e025
MB
6219 if ( m_table )
6220 {
b7fff980 6221 if (IsCellEditControlEnabled())
b54ba671 6222 DisableCellEditControl();
b7fff980 6223
4ea3479c 6224 bool done = m_table->InsertCols( pos, numCols );
4ea3479c 6225 return done;
2d66e025
MB
6226 // the table will have sent the results of the insert col
6227 // operation to this view object as a grid table message
f85afd4e 6228 }
2f024384 6229
ca65c044 6230 return false;
f85afd4e
MB
6231}
6232
2d66e025 6233bool wxGrid::AppendCols( int numCols, bool WXUNUSED(updateLabels) )
f85afd4e 6234{
2d66e025
MB
6235 // TODO: something with updateLabels flag
6236
6237 if ( !m_created )
f85afd4e 6238 {
bd3c6758 6239 wxFAIL_MSG( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") );
ca65c044 6240 return false;
2d66e025 6241 }
f85afd4e 6242
4ea3479c
JS
6243 if ( m_table )
6244 {
6245 bool done = m_table->AppendCols( numCols );
4ea3479c
JS
6246 return done;
6247 // the table will have sent the results of the append col
6248 // operation to this view object as a grid table message
6249 }
2f024384 6250
ca65c044 6251 return false;
f85afd4e
MB
6252}
6253
2d66e025 6254bool wxGrid::DeleteCols( int pos, int numCols, bool WXUNUSED(updateLabels) )
f85afd4e 6255{
2d66e025 6256 // TODO: something with updateLabels flag
8f177c8e 6257
2d66e025 6258 if ( !m_created )
f85afd4e 6259 {
bd3c6758 6260 wxFAIL_MSG( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") );
ca65c044 6261 return false;
f85afd4e
MB
6262 }
6263
b7fff980 6264 if ( m_table )
2d66e025 6265 {
b7fff980 6266 if (IsCellEditControlEnabled())
b54ba671 6267 DisableCellEditControl();
8f177c8e 6268
4ea3479c 6269 bool done = m_table->DeleteCols( pos, numCols );
4ea3479c 6270 return done;
f6bcfd97
BP
6271 // the table will have sent the results of the delete col
6272 // operation to this view object as a grid table message
f85afd4e 6273 }
2f024384 6274
ca65c044 6275 return false;
f85afd4e
MB
6276}
6277
2d66e025
MB
6278//
6279// ----- event handlers
f85afd4e 6280//
8f177c8e 6281
2d66e025
MB
6282// Generate a grid event based on a mouse event and
6283// return the result of ProcessEvent()
6284//
fe7b9ed6 6285int wxGrid::SendEvent( const wxEventType type,
2d66e025
MB
6286 int row, int col,
6287 wxMouseEvent& mouseEv )
6288{
2f024384 6289 bool claimed, vetoed;
fe7b9ed6 6290
97a9929e
VZ
6291 if ( type == wxEVT_GRID_ROW_SIZE || type == wxEVT_GRID_COL_SIZE )
6292 {
6293 int rowOrCol = (row == -1 ? col : row);
6294
6295 wxGridSizeEvent gridEvt( GetId(),
6296 type,
6297 this,
6298 rowOrCol,
6299 mouseEv.GetX() + GetRowLabelSize(),
6300 mouseEv.GetY() + GetColLabelSize(),
6301 mouseEv.ControlDown(),
6302 mouseEv.ShiftDown(),
6303 mouseEv.AltDown(),
6304 mouseEv.MetaDown() );
6305
6306 claimed = GetEventHandler()->ProcessEvent(gridEvt);
6307 vetoed = !gridEvt.IsAllowed();
6308 }
fe7b9ed6 6309 else if ( type == wxEVT_GRID_RANGE_SELECT )
97a9929e
VZ
6310 {
6311 // Right now, it should _never_ end up here!
6312 wxGridRangeSelectEvent gridEvt( GetId(),
6313 type,
6314 this,
6315 m_selectingTopLeft,
6316 m_selectingBottomRight,
ca65c044 6317 true,
97a9929e
VZ
6318 mouseEv.ControlDown(),
6319 mouseEv.ShiftDown(),
6320 mouseEv.AltDown(),
6321 mouseEv.MetaDown() );
6322
6323 claimed = GetEventHandler()->ProcessEvent(gridEvt);
6324 vetoed = !gridEvt.IsAllowed();
6325 }
fe7b9ed6 6326 else
97a9929e
VZ
6327 {
6328 wxGridEvent gridEvt( GetId(),
6329 type,
6330 this,
6331 row, col,
6332 mouseEv.GetX() + GetRowLabelSize(),
6333 mouseEv.GetY() + GetColLabelSize(),
ca65c044 6334 false,
97a9929e
VZ
6335 mouseEv.ControlDown(),
6336 mouseEv.ShiftDown(),
6337 mouseEv.AltDown(),
6338 mouseEv.MetaDown() );
6339 claimed = GetEventHandler()->ProcessEvent(gridEvt);
6340 vetoed = !gridEvt.IsAllowed();
6341 }
6342
6343 // A Veto'd event may not be `claimed' so test this first
4db6714b
KH
6344 if (vetoed)
6345 return -1;
6346
97a9929e 6347 return claimed ? 1 : 0;
f85afd4e
MB
6348}
6349
2d66e025
MB
6350// Generate a grid event of specified type and return the result
6351// of ProcessEvent().
f85afd4e 6352//
fe7b9ed6 6353int wxGrid::SendEvent( const wxEventType type,
2d66e025 6354 int row, int col )
f85afd4e 6355{
a9339fe2 6356 bool claimed, vetoed;
fe7b9ed6 6357
b54ba671 6358 if ( type == wxEVT_GRID_ROW_SIZE || type == wxEVT_GRID_COL_SIZE )
f85afd4e 6359 {
2d66e025 6360 int rowOrCol = (row == -1 ? col : row);
f85afd4e 6361
a9339fe2 6362 wxGridSizeEvent gridEvt( GetId(), type, this, rowOrCol );
2d66e025 6363
fe7b9ed6
VZ
6364 claimed = GetEventHandler()->ProcessEvent(gridEvt);
6365 vetoed = !gridEvt.IsAllowed();
2d66e025
MB
6366 }
6367 else
6368 {
a9339fe2 6369 wxGridEvent gridEvt( GetId(), type, this, row, col );
8f177c8e 6370
fe7b9ed6
VZ
6371 claimed = GetEventHandler()->ProcessEvent(gridEvt);
6372 vetoed = !gridEvt.IsAllowed();
6373 }
6374
97a9929e 6375 // A Veto'd event may not be `claimed' so test this first
4db6714b
KH
6376 if (vetoed)
6377 return -1;
6378
97a9929e 6379 return claimed ? 1 : 0;
f85afd4e
MB
6380}
6381
2d66e025 6382void wxGrid::OnPaint( wxPaintEvent& WXUNUSED(event) )
f85afd4e 6383{
f6bcfd97 6384 wxPaintDC dc(this); // needed to prevent zillions of paint events on MSW
8f177c8e 6385}
f85afd4e 6386
bca7bfc8 6387void wxGrid::Refresh(bool eraseb, const wxRect* rect)
27f35b66 6388{
ad603bf7
VZ
6389 // Don't do anything if between Begin/EndBatch...
6390 // EndBatch() will do all this on the last nested one anyway.
27f35b66
SN
6391 if (! GetBatchCount())
6392 {
bca7bfc8 6393 // Refresh to get correct scrolled position:
4db6714b 6394 wxScrolledWindow::Refresh(eraseb, rect);
27f35b66 6395
27f35b66
SN
6396 if (rect)
6397 {
bca7bfc8
SN
6398 int rect_x, rect_y, rectWidth, rectHeight;
6399 int width_label, width_cell, height_label, height_cell;
6400 int x, y;
6401
2f024384 6402 // Copy rectangle can get scroll offsets..
bca7bfc8
SN
6403 rect_x = rect->GetX();
6404 rect_y = rect->GetY();
6405 rectWidth = rect->GetWidth();
6406 rectHeight = rect->GetHeight();
27f35b66 6407
bca7bfc8 6408 width_label = m_rowLabelWidth - rect_x;
4db6714b
KH
6409 if (width_label > rectWidth)
6410 width_label = rectWidth;
27f35b66 6411
bca7bfc8 6412 height_label = m_colLabelHeight - rect_y;
a9339fe2
DS
6413 if (height_label > rectHeight)
6414 height_label = rectHeight;
27f35b66 6415
bca7bfc8
SN
6416 if (rect_x > m_rowLabelWidth)
6417 {
6418 x = rect_x - m_rowLabelWidth;
6419 width_cell = rectWidth;
6420 }
6421 else
6422 {
6423 x = 0;
6424 width_cell = rectWidth - (m_rowLabelWidth - rect_x);
6425 }
6426
6427 if (rect_y > m_colLabelHeight)
6428 {
6429 y = rect_y - m_colLabelHeight;
6430 height_cell = rectHeight;
6431 }
6432 else
6433 {
6434 y = 0;
6435 height_cell = rectHeight - (m_colLabelHeight - rect_y);
6436 }
6437
6438 // Paint corner label part intersecting rect.
6439 if ( width_label > 0 && height_label > 0 )
6440 {
6441 wxRect anotherrect(rect_x, rect_y, width_label, height_label);
6442 m_cornerLabelWin->Refresh(eraseb, &anotherrect);
6443 }
6444
6445 // Paint col labels part intersecting rect.
6446 if ( width_cell > 0 && height_label > 0 )
6447 {
6448 wxRect anotherrect(x, rect_y, width_cell, height_label);
6449 m_colLabelWin->Refresh(eraseb, &anotherrect);
6450 }
6451
6452 // Paint row labels part intersecting rect.
6453 if ( width_label > 0 && height_cell > 0 )
6454 {
6455 wxRect anotherrect(rect_x, y, width_label, height_cell);
6456 m_rowLabelWin->Refresh(eraseb, &anotherrect);
6457 }
6458
6459 // Paint cell area part intersecting rect.
6460 if ( width_cell > 0 && height_cell > 0 )
6461 {
6462 wxRect anotherrect(x, y, width_cell, height_cell);
6463 m_gridWin->Refresh(eraseb, &anotherrect);
6464 }
6465 }
6466 else
6467 {
6468 m_cornerLabelWin->Refresh(eraseb, NULL);
2b5f62a0 6469 m_colLabelWin->Refresh(eraseb, NULL);
bca7bfc8
SN
6470 m_rowLabelWin->Refresh(eraseb, NULL);
6471 m_gridWin->Refresh(eraseb, NULL);
6472 }
27f35b66
SN
6473 }
6474}
f85afd4e 6475
97a9929e 6476void wxGrid::OnSize( wxSizeEvent& event )
f85afd4e 6477{
97a9929e 6478 // position the child windows
7807d81c 6479 CalcWindowSizes();
97a9929e
VZ
6480
6481 // don't call CalcDimensions() from here, the base class handles the size
6482 // changes itself
6483 event.Skip();
f85afd4e
MB
6484}
6485
2d66e025 6486void wxGrid::OnKeyDown( wxKeyEvent& event )
f85afd4e 6487{
2d66e025 6488 if ( m_inOnKeyDown )
f85afd4e 6489 {
2d66e025
MB
6490 // shouldn't be here - we are going round in circles...
6491 //
07296f0b 6492 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") );
f85afd4e
MB
6493 }
6494
ca65c044 6495 m_inOnKeyDown = true;
f85afd4e 6496
2d66e025 6497 // propagate the event up and see if it gets processed
2d66e025
MB
6498 wxWindow *parent = GetParent();
6499 wxKeyEvent keyEvt( event );
6500 keyEvt.SetEventObject( parent );
6501
6502 if ( !parent->GetEventHandler()->ProcessEvent( keyEvt ) )
f85afd4e 6503 {
2d66e025 6504 // try local handlers
12a3f227 6505 switch ( event.GetKeyCode() )
2d66e025
MB
6506 {
6507 case WXK_UP:
6508 if ( event.ControlDown() )
5c8fc7c1 6509 MoveCursorUpBlock( event.ShiftDown() );
2d66e025 6510 else
5c8fc7c1 6511 MoveCursorUp( event.ShiftDown() );
2d66e025 6512 break;
f85afd4e 6513
2d66e025
MB
6514 case WXK_DOWN:
6515 if ( event.ControlDown() )
5c8fc7c1 6516 MoveCursorDownBlock( event.ShiftDown() );
2d66e025 6517 else
5c8fc7c1 6518 MoveCursorDown( event.ShiftDown() );
2d66e025 6519 break;
8f177c8e 6520
2d66e025
MB
6521 case WXK_LEFT:
6522 if ( event.ControlDown() )
5c8fc7c1 6523 MoveCursorLeftBlock( event.ShiftDown() );
2d66e025 6524 else
5c8fc7c1 6525 MoveCursorLeft( event.ShiftDown() );
2d66e025
MB
6526 break;
6527
6528 case WXK_RIGHT:
6529 if ( event.ControlDown() )
5c8fc7c1 6530 MoveCursorRightBlock( event.ShiftDown() );
2d66e025 6531 else
5c8fc7c1 6532 MoveCursorRight( event.ShiftDown() );
2d66e025 6533 break;
b99be8fb 6534
2d66e025 6535 case WXK_RETURN:
a4f7bf58 6536 case WXK_NUMPAD_ENTER:
58dd5b3b
MB
6537 if ( event.ControlDown() )
6538 {
6539 event.Skip(); // to let the edit control have the return
6540 }
6541 else
6542 {
f6bcfd97
BP
6543 if ( GetGridCursorRow() < GetNumberRows()-1 )
6544 {
6545 MoveCursorDown( event.ShiftDown() );
6546 }
6547 else
6548 {
6549 // at the bottom of a column
13f6e9e8 6550 DisableCellEditControl();
f6bcfd97 6551 }
58dd5b3b 6552 }
2d66e025
MB
6553 break;
6554
5c8fc7c1 6555 case WXK_ESCAPE:
e32352cf 6556 ClearSelection();
5c8fc7c1
SN
6557 break;
6558
2c9a89e0
RD
6559 case WXK_TAB:
6560 if (event.ShiftDown())
f6bcfd97
BP
6561 {
6562 if ( GetGridCursorCol() > 0 )
6563 {
ca65c044 6564 MoveCursorLeft( false );
f6bcfd97
BP
6565 }
6566 else
6567 {
6568 // at left of grid
13f6e9e8 6569 DisableCellEditControl();
f6bcfd97
BP
6570 }
6571 }
2c9a89e0 6572 else
f6bcfd97 6573 {
ccdee36f 6574 if ( GetGridCursorCol() < GetNumberCols() - 1 )
f6bcfd97 6575 {
ca65c044 6576 MoveCursorRight( false );
f6bcfd97
BP
6577 }
6578 else
6579 {
6580 // at right of grid
13f6e9e8 6581 DisableCellEditControl();
f6bcfd97
BP
6582 }
6583 }
2c9a89e0
RD
6584 break;
6585
2d66e025
MB
6586 case WXK_HOME:
6587 if ( event.ControlDown() )
6588 {
6589 MakeCellVisible( 0, 0 );
6590 SetCurrentCell( 0, 0 );
6591 }
6592 else
6593 {
6594 event.Skip();
6595 }
6596 break;
6597
6598 case WXK_END:
6599 if ( event.ControlDown() )
6600 {
a9339fe2
DS
6601 MakeCellVisible( m_numRows - 1, m_numCols - 1 );
6602 SetCurrentCell( m_numRows - 1, m_numCols - 1 );
2d66e025
MB
6603 }
6604 else
6605 {
6606 event.Skip();
6607 }
6608 break;
6609
faa94f3e 6610 case WXK_PAGEUP:
2d66e025
MB
6611 MovePageUp();
6612 break;
6613
faa94f3e 6614 case WXK_PAGEDOWN:
2d66e025
MB
6615 MovePageDown();
6616 break;
6617
07296f0b 6618 case WXK_SPACE:
aa5e1f75
SN
6619 if ( event.ControlDown() )
6620 {
3f3dc2ef
VZ
6621 if ( m_selection )
6622 {
a9339fe2
DS
6623 m_selection->ToggleCellSelection(
6624 m_currentCellCoords.GetRow(),
6625 m_currentCellCoords.GetCol(),
6626 event.ControlDown(),
6627 event.ShiftDown(),
6628 event.AltDown(),
6629 event.MetaDown() );
3f3dc2ef 6630 }
aa5e1f75
SN
6631 break;
6632 }
ccdee36f 6633
07296f0b 6634 if ( !IsEditable() )
ca65c044 6635 MoveCursorRight( false );
ccdee36f
DS
6636 else
6637 event.Skip();
6638 break;
07296f0b 6639
2d66e025 6640 default:
63e2147c 6641 event.Skip();
025562fe 6642 break;
2d66e025 6643 }
f85afd4e
MB
6644 }
6645
ca65c044 6646 m_inOnKeyDown = false;
f85afd4e
MB
6647}
6648
f6bcfd97
BP
6649void wxGrid::OnKeyUp( wxKeyEvent& event )
6650{
6651 // try local handlers
6652 //
12a3f227 6653 if ( event.GetKeyCode() == WXK_SHIFT )
f6bcfd97
BP
6654 {
6655 if ( m_selectingTopLeft != wxGridNoCellCoords &&
6656 m_selectingBottomRight != wxGridNoCellCoords )
3f3dc2ef
VZ
6657 {
6658 if ( m_selection )
6659 {
a9339fe2
DS
6660 m_selection->SelectBlock(
6661 m_selectingTopLeft.GetRow(),
6662 m_selectingTopLeft.GetCol(),
6663 m_selectingBottomRight.GetRow(),
6664 m_selectingBottomRight.GetCol(),
6665 event.ControlDown(),
6666 true,
6667 event.AltDown(),
6668 event.MetaDown() );
3f3dc2ef
VZ
6669 }
6670 }
6671
f6bcfd97
BP
6672 m_selectingTopLeft = wxGridNoCellCoords;
6673 m_selectingBottomRight = wxGridNoCellCoords;
6674 m_selectingKeyboard = wxGridNoCellCoords;
6675 }
6676}
6677
63e2147c
RD
6678void wxGrid::OnChar( wxKeyEvent& event )
6679{
6680 // is it possible to edit the current cell at all?
6681 if ( !IsCellEditControlEnabled() && CanEnableCellControl() )
6682 {
6683 // yes, now check whether the cells editor accepts the key
6684 int row = m_currentCellCoords.GetRow();
6685 int col = m_currentCellCoords.GetCol();
2f024384 6686 wxGridCellAttr *attr = GetCellAttr(row, col);
63e2147c
RD
6687 wxGridCellEditor *editor = attr->GetEditor(this, row, col);
6688
6689 // <F2> is special and will always start editing, for
6690 // other keys - ask the editor itself
6691 if ( (event.GetKeyCode() == WXK_F2 && !event.HasModifiers())
6692 || editor->IsAcceptedKey(event) )
6693 {
6694 // ensure cell is visble
6695 MakeCellVisible(row, col);
6696 EnableCellEditControl();
6697
6698 // a problem can arise if the cell is not completely
6699 // visible (even after calling MakeCellVisible the
6700 // control is not created and calling StartingKey will
6701 // crash the app
046d682f 6702 if ( event.GetKeyCode() != WXK_F2 && editor->IsCreated() && m_cellEditCtrlEnabled )
63e2147c
RD
6703 editor->StartingKey(event);
6704 }
6705 else
6706 {
6707 event.Skip();
6708 }
6709
6710 editor->DecRef();
6711 attr->DecRef();
6712 }
6713 else
6714 {
6715 event.Skip();
6716 }
6717}
6718
2796cce3 6719void wxGrid::OnEraseBackground(wxEraseEvent&)
508011ce
VZ
6720{
6721}
07296f0b 6722
2d66e025 6723void wxGrid::SetCurrentCell( const wxGridCellCoords& coords )
66242c80 6724{
f6bcfd97
BP
6725 if ( SendEvent( wxEVT_GRID_SELECT_CELL, coords.GetRow(), coords.GetCol() ) )
6726 {
6727 // the event has been intercepted - do nothing
6728 return;
6729 }
6730
bee19958
DS
6731 wxClientDC dc( m_gridWin );
6732 PrepareDC( dc );
f6bcfd97 6733
2a7750d9 6734 if ( m_currentCellCoords != wxGridNoCellCoords )
2d66e025 6735 {
b54ba671 6736 DisableCellEditControl();
07296f0b 6737
ca65c044 6738 if ( IsVisible( m_currentCellCoords, false ) )
f6bcfd97
BP
6739 {
6740 wxRect r;
bee19958 6741 r = BlockToDeviceRect( m_currentCellCoords, m_currentCellCoords );
f6bcfd97
BP
6742 if ( !m_gridLinesEnabled )
6743 {
6744 r.x--;
6745 r.y--;
6746 r.width++;
6747 r.height++;
6748 }
d1c0b4f9 6749
3ed884a0 6750 wxGridCellCoordsArray cells = CalcCellsExposed( r );
84912ef8 6751
f6bcfd97
BP
6752 // Otherwise refresh redraws the highlight!
6753 m_currentCellCoords = coords;
275c4ae4 6754
bee19958 6755 DrawGridCellArea( dc, cells );
f6bcfd97
BP
6756 DrawAllGridLines( dc, r );
6757 }
66242c80 6758 }
8f177c8e 6759
2d66e025
MB
6760 m_currentCellCoords = coords;
6761
bee19958
DS
6762 wxGridCellAttr *attr = GetCellAttr( coords );
6763 DrawCellHighlight( dc, attr );
2a7750d9 6764 attr->DecRef();
66242c80
MB
6765}
6766
c9097836
MB
6767void wxGrid::HighlightBlock( int topRow, int leftCol, int bottomRow, int rightCol )
6768{
6769 int temp;
6770 wxGridCellCoords updateTopLeft, updateBottomRight;
6771
3f3dc2ef 6772 if ( m_selection )
c9097836 6773 {
3f3dc2ef
VZ
6774 if ( m_selection->GetSelectionMode() == wxGrid::wxGridSelectRows )
6775 {
6776 leftCol = 0;
6777 rightCol = GetNumberCols() - 1;
6778 }
6779 else if ( m_selection->GetSelectionMode() == wxGrid::wxGridSelectColumns )
6780 {
6781 topRow = 0;
6782 bottomRow = GetNumberRows() - 1;
6783 }
c9097836 6784 }
3f3dc2ef 6785
c9097836
MB
6786 if ( topRow > bottomRow )
6787 {
6788 temp = topRow;
6789 topRow = bottomRow;
6790 bottomRow = temp;
6791 }
6792
6793 if ( leftCol > rightCol )
6794 {
6795 temp = leftCol;
6796 leftCol = rightCol;
6797 rightCol = temp;
6798 }
6799
6800 updateTopLeft = wxGridCellCoords( topRow, leftCol );
6801 updateBottomRight = wxGridCellCoords( bottomRow, rightCol );
6802
3ed884a0
SN
6803 // First the case that we selected a completely new area
6804 if ( m_selectingTopLeft == wxGridNoCellCoords ||
6805 m_selectingBottomRight == wxGridNoCellCoords )
6806 {
6807 wxRect rect;
6808 rect = BlockToDeviceRect( wxGridCellCoords ( topRow, leftCol ),
6809 wxGridCellCoords ( bottomRow, rightCol ) );
ca65c044 6810 m_gridWin->Refresh( false, &rect );
3ed884a0 6811 }
2f024384 6812
3ed884a0
SN
6813 // Now handle changing an existing selection area.
6814 else if ( m_selectingTopLeft != updateTopLeft ||
6815 m_selectingBottomRight != updateBottomRight )
c9097836
MB
6816 {
6817 // Compute two optimal update rectangles:
6818 // Either one rectangle is a real subset of the
6819 // other, or they are (almost) disjoint!
6820 wxRect rect[4];
6821 bool need_refresh[4];
6822 need_refresh[0] =
6823 need_refresh[1] =
6824 need_refresh[2] =
ca65c044 6825 need_refresh[3] = false;
c9097836
MB
6826 int i;
6827
6828 // Store intermediate values
a9339fe2
DS
6829 wxCoord oldLeft = m_selectingTopLeft.GetCol();
6830 wxCoord oldTop = m_selectingTopLeft.GetRow();
6831 wxCoord oldRight = m_selectingBottomRight.GetCol();
c9097836
MB
6832 wxCoord oldBottom = m_selectingBottomRight.GetRow();
6833
6834 // Determine the outer/inner coordinates.
6835 if (oldLeft > leftCol)
6836 {
6837 temp = oldLeft;
6838 oldLeft = leftCol;
6839 leftCol = temp;
6840 }
6841 if (oldTop > topRow )
6842 {
6843 temp = oldTop;
6844 oldTop = topRow;
6845 topRow = temp;
6846 }
6847 if (oldRight < rightCol )
6848 {
6849 temp = oldRight;
6850 oldRight = rightCol;
6851 rightCol = temp;
6852 }
6853 if (oldBottom < bottomRow)
6854 {
6855 temp = oldBottom;
6856 oldBottom = bottomRow;
6857 bottomRow = temp;
6858 }
6859
6860 // Now, either the stuff marked old is the outer
6861 // rectangle or we don't have a situation where one
6862 // is contained in the other.
6863
6864 if ( oldLeft < leftCol )
6865 {
3ed884a0
SN
6866 // Refresh the newly selected or deselected
6867 // area to the left of the old or new selection.
ca65c044 6868 need_refresh[0] = true;
a9339fe2
DS
6869 rect[0] = BlockToDeviceRect(
6870 wxGridCellCoords( oldTop, oldLeft ),
6871 wxGridCellCoords( oldBottom, leftCol - 1 ) );
c9097836
MB
6872 }
6873
2f024384 6874 if ( oldTop < topRow )
c9097836 6875 {
3ed884a0
SN
6876 // Refresh the newly selected or deselected
6877 // area above the old or new selection.
ca65c044 6878 need_refresh[1] = true;
2f024384
DS
6879 rect[1] = BlockToDeviceRect(
6880 wxGridCellCoords( oldTop, leftCol ),
6881 wxGridCellCoords( topRow - 1, rightCol ) );
c9097836
MB
6882 }
6883
6884 if ( oldRight > rightCol )
6885 {
3ed884a0
SN
6886 // Refresh the newly selected or deselected
6887 // area to the right of the old or new selection.
ca65c044 6888 need_refresh[2] = true;
2f024384 6889 rect[2] = BlockToDeviceRect(
a9339fe2
DS
6890 wxGridCellCoords( oldTop, rightCol + 1 ),
6891 wxGridCellCoords( oldBottom, oldRight ) );
c9097836
MB
6892 }
6893
6894 if ( oldBottom > bottomRow )
6895 {
3ed884a0
SN
6896 // Refresh the newly selected or deselected
6897 // area below the old or new selection.
ca65c044 6898 need_refresh[3] = true;
2f024384 6899 rect[3] = BlockToDeviceRect(
a9339fe2
DS
6900 wxGridCellCoords( bottomRow + 1, leftCol ),
6901 wxGridCellCoords( oldBottom, rightCol ) );
c9097836
MB
6902 }
6903
c9097836
MB
6904 // various Refresh() calls
6905 for (i = 0; i < 4; i++ )
6906 if ( need_refresh[i] && rect[i] != wxGridNoCellRect )
ca65c044 6907 m_gridWin->Refresh( false, &(rect[i]) );
c9097836 6908 }
2f024384
DS
6909
6910 // change selection
3ed884a0
SN
6911 m_selectingTopLeft = updateTopLeft;
6912 m_selectingBottomRight = updateBottomRight;
c9097836
MB
6913}
6914
2d66e025
MB
6915//
6916// ------ functions to get/send data (see also public functions)
6917//
6918
6919bool wxGrid::GetModelValues()
66242c80 6920{
bca7bfc8
SN
6921 // Hide the editor, so it won't hide a changed value.
6922 HideCellEditControl();
c6707d16 6923
2d66e025 6924 if ( m_table )
66242c80 6925 {
2d66e025 6926 // all we need to do is repaint the grid
66242c80 6927 //
2d66e025 6928 m_gridWin->Refresh();
ca65c044 6929 return true;
66242c80 6930 }
8f177c8e 6931
ca65c044 6932 return false;
66242c80
MB
6933}
6934
2d66e025 6935bool wxGrid::SetModelValues()
f85afd4e 6936{
2d66e025 6937 int row, col;
8f177c8e 6938
c6707d16
SN
6939 // Disable the editor, so it won't hide a changed value.
6940 // Do we also want to save the current value of the editor first?
6941 // I think so ...
c6707d16
SN
6942 DisableCellEditControl();
6943
2d66e025
MB
6944 if ( m_table )
6945 {
6946 for ( row = 0; row < m_numRows; row++ )
f85afd4e 6947 {
2d66e025 6948 for ( col = 0; col < m_numCols; col++ )
f85afd4e 6949 {
2d66e025 6950 m_table->SetValue( row, col, GetCellValue(row, col) );
f85afd4e
MB
6951 }
6952 }
8f177c8e 6953
ca65c044 6954 return true;
f85afd4e
MB
6955 }
6956
ca65c044 6957 return false;
f85afd4e
MB
6958}
6959
2d66e025
MB
6960// Note - this function only draws cells that are in the list of
6961// exposed cells (usually set from the update region by
6962// CalcExposedCells)
6963//
d10f4bf9 6964void wxGrid::DrawGridCellArea( wxDC& dc, const wxGridCellCoordsArray& cells )
f85afd4e 6965{
4db6714b
KH
6966 if ( !m_numRows || !m_numCols )
6967 return;
60ff3b99 6968
dc1f566f 6969 int i, numCells = cells.GetCount();
27f35b66
SN
6970 int row, col, cell_rows, cell_cols;
6971 wxGridCellCoordsArray redrawCells;
60ff3b99 6972
a9339fe2 6973 for ( i = numCells - 1; i >= 0; i-- )
f85afd4e 6974 {
27f35b66
SN
6975 row = cells[i].GetRow();
6976 col = cells[i].GetCol();
6977 GetCellSize( row, col, &cell_rows, &cell_cols );
6978
6979 // If this cell is part of a multicell block, find owner for repaint
6980 if ( cell_rows <= 0 || cell_cols <= 0 )
6981 {
a9339fe2 6982 wxGridCellCoords cell( row + cell_rows, col + cell_cols );
ca65c044 6983 bool marked = false;
a9339fe2 6984 for ( int j = 0; j < numCells; j++ )
27f35b66
SN
6985 {
6986 if ( cell == cells[j] )
6987 {
ca65c044 6988 marked = true;
3ed884a0 6989 break;
27f35b66
SN
6990 }
6991 }
2f024384 6992
27f35b66
SN
6993 if (!marked)
6994 {
6995 int count = redrawCells.GetCount();
dc1f566f 6996 for (int j = 0; j < count; j++)
27f35b66
SN
6997 {
6998 if ( cell == redrawCells[j] )
6999 {
ca65c044 7000 marked = true;
27f35b66
SN
7001 break;
7002 }
7003 }
2f024384 7004
4db6714b
KH
7005 if (!marked)
7006 redrawCells.Add( cell );
27f35b66 7007 }
2f024384
DS
7008
7009 // don't bother drawing this cell
7010 continue;
27f35b66
SN
7011 }
7012
7013 // If this cell is empty, find cell to left that might want to overflow
7014 if (m_table && m_table->IsEmptyCell(row, col))
7015 {
dc1f566f 7016 for ( int l = 0; l < cell_rows; l++ )
27f35b66 7017 {
dc1f566f
SN
7018 // find a cell in this row to left alreay marked for repaint
7019 int left = col;
7020 for (int k = 0; k < int(redrawCells.GetCount()); k++)
7021 if ((redrawCells[k].GetCol() < left) &&
7022 (redrawCells[k].GetRow() == row))
2f024384 7023 {
4db6714b 7024 left = redrawCells[k].GetCol();
2f024384 7025 }
dc1f566f 7026
4db6714b
KH
7027 if (left == col)
7028 left = 0; // oh well
dc1f566f 7029
2f024384 7030 for (int j = col - 1; j >= left; j--)
27f35b66 7031 {
2f024384 7032 if (!m_table->IsEmptyCell(row + l, j))
27f35b66 7033 {
2f024384 7034 if (GetCellOverflow(row + l, j))
27f35b66 7035 {
2f024384 7036 wxGridCellCoords cell(row + l, j);
ca65c044 7037 bool marked = false;
27f35b66 7038
dc1f566f 7039 for (int k = 0; k < numCells; k++)
27f35b66
SN
7040 {
7041 if ( cell == cells[k] )
7042 {
ca65c044 7043 marked = true;
27f35b66
SN
7044 break;
7045 }
7046 }
4db6714b 7047
27f35b66
SN
7048 if (!marked)
7049 {
7050 int count = redrawCells.GetCount();
dc1f566f 7051 for (int k = 0; k < count; k++)
27f35b66
SN
7052 {
7053 if ( cell == redrawCells[k] )
7054 {
ca65c044 7055 marked = true;
27f35b66
SN
7056 break;
7057 }
7058 }
4db6714b
KH
7059 if (!marked)
7060 redrawCells.Add( cell );
27f35b66
SN
7061 }
7062 }
7063 break;
7064 }
7065 }
7066 }
7067 }
4db6714b 7068
d10f4bf9 7069 DrawCell( dc, cells[i] );
2d66e025 7070 }
27f35b66
SN
7071
7072 numCells = redrawCells.GetCount();
7073
7074 for ( i = numCells - 1; i >= 0; i-- )
7075 {
7076 DrawCell( dc, redrawCells[i] );
7077 }
2d66e025 7078}
8f177c8e 7079
7c8a8ad5
MB
7080void wxGrid::DrawGridSpace( wxDC& dc )
7081{
f6bcfd97
BP
7082 int cw, ch;
7083 m_gridWin->GetClientSize( &cw, &ch );
7c8a8ad5 7084
f6bcfd97
BP
7085 int right, bottom;
7086 CalcUnscrolledPosition( cw, ch, &right, &bottom );
7c8a8ad5 7087
f6bcfd97 7088 int rightCol = m_numCols > 0 ? GetColRight(m_numCols - 1) : 0;
2f024384 7089 int bottomRow = m_numRows > 0 ? GetRowBottom(m_numRows - 1) : 0;
7c8a8ad5 7090
f6bcfd97
BP
7091 if ( right > rightCol || bottom > bottomRow )
7092 {
7093 int left, top;
7094 CalcUnscrolledPosition( 0, 0, &left, &top );
7c8a8ad5 7095
f6bcfd97
BP
7096 dc.SetBrush( wxBrush(GetDefaultCellBackgroundColour(), wxSOLID) );
7097 dc.SetPen( *wxTRANSPARENT_PEN );
7c8a8ad5 7098
f6bcfd97
BP
7099 if ( right > rightCol )
7100 {
a9339fe2 7101 dc.DrawRectangle( rightCol, top, right - rightCol, ch );
f6bcfd97
BP
7102 }
7103
7104 if ( bottom > bottomRow )
7105 {
a9339fe2 7106 dc.DrawRectangle( left, bottomRow, cw, bottom - bottomRow );
f6bcfd97
BP
7107 }
7108 }
7c8a8ad5
MB
7109}
7110
2d66e025
MB
7111void wxGrid::DrawCell( wxDC& dc, const wxGridCellCoords& coords )
7112{
ab79958a
VZ
7113 int row = coords.GetRow();
7114 int col = coords.GetCol();
60ff3b99 7115
7c1cb261 7116 if ( GetColWidth(col) <= 0 || GetRowHeight(row) <= 0 )
ab79958a
VZ
7117 return;
7118
7119 // we draw the cell border ourselves
9496deb5 7120#if !WXGRID_DRAW_LINES
2d66e025
MB
7121 if ( m_gridLinesEnabled )
7122 DrawCellBorder( dc, coords );
796df70a 7123#endif
f85afd4e 7124
189d0213
VZ
7125 wxGridCellAttr* attr = GetCellAttr(row, col);
7126
7127 bool isCurrent = coords == m_currentCellCoords;
508011ce 7128
f6bcfd97 7129 wxRect rect = CellToRect( row, col );
f85afd4e 7130
189d0213 7131 // if the editor is shown, we should use it and not the renderer
f6bcfd97
BP
7132 // Note: However, only if it is really _shown_, i.e. not hidden!
7133 if ( isCurrent && IsCellEditControlShown() )
189d0213 7134 {
a9339fe2 7135 // NB: this "#if..." is temporary and fixes a problem where the
962a48f6
DS
7136 // edit control is erased by this code after being rendered.
7137 // On wxMac (QD build only), the cell editor is a wxTextCntl and is rendered
7138 // implicitly, causing this out-of order render.
7139#if !defined(__WXMAC__) || wxMAC_USE_CORE_GRAPHICS
0b190b0f
VZ
7140 wxGridCellEditor *editor = attr->GetEditor(this, row, col);
7141 editor->PaintBackground(rect, attr);
7142 editor->DecRef();
962a48f6 7143#endif
189d0213
VZ
7144 }
7145 else
7146 {
a9339fe2 7147 // but all the rest is drawn by the cell renderer and hence may be customized
0b190b0f
VZ
7148 wxGridCellRenderer *renderer = attr->GetRenderer(this, row, col);
7149 renderer->Draw(*this, *attr, dc, rect, row, col, IsInSelection(coords));
7150 renderer->DecRef();
189d0213 7151 }
07296f0b 7152
283b7808
VZ
7153 attr->DecRef();
7154}
07296f0b 7155
283b7808 7156void wxGrid::DrawCellHighlight( wxDC& dc, const wxGridCellAttr *attr )
07296f0b
RD
7157{
7158 int row = m_currentCellCoords.GetRow();
7159 int col = m_currentCellCoords.GetCol();
7160
7c1cb261 7161 if ( GetColWidth(col) <= 0 || GetRowHeight(row) <= 0 )
07296f0b
RD
7162 return;
7163
f6bcfd97 7164 wxRect rect = CellToRect(row, col);
07296f0b 7165
b3a7510d
VZ
7166 // hmmm... what could we do here to show that the cell is disabled?
7167 // for now, I just draw a thinner border than for the other ones, but
7168 // it doesn't look really good
b3a7510d 7169
d2520c85
RD
7170 int penWidth = attr->IsReadOnly() ? m_cellHighlightROPenWidth : m_cellHighlightPenWidth;
7171
27f35b66
SN
7172 if (penWidth > 0)
7173 {
d2520c85 7174 // The center of th drawn line is where the position/width/height of
a9339fe2 7175 // the rectangle is actually at, (on wxMSW at least,) so we will
d2520c85 7176 // reduce the size of the rectangle to compensate for the thickness of
a9339fe2 7177 // the line. If this is too strange on non wxMSW platforms then
d2520c85 7178 // please #ifdef this appropriately.
4db6714b
KH
7179 rect.x += penWidth / 2;
7180 rect.y += penWidth / 2;
7181 rect.width -= penWidth - 1;
7182 rect.height -= penWidth - 1;
d2520c85 7183
d2520c85 7184 // Now draw the rectangle
73145b0e
JS
7185 // use the cellHighlightColour if the cell is inside a selection, this
7186 // will ensure the cell is always visible.
4db6714b 7187 dc.SetPen(wxPen(IsInSelection(row,col) ? m_selectionForeground : m_cellHighlightColour, penWidth, wxSOLID));
d2520c85
RD
7188 dc.SetBrush(*wxTRANSPARENT_BRUSH);
7189 dc.DrawRectangle(rect);
7190 }
07296f0b 7191
283b7808 7192#if 0
2f024384 7193 // VZ: my experiments with 3D borders...
283b7808 7194
b3a7510d 7195 // how to properly set colours for arbitrary bg?
283b7808
VZ
7196 wxCoord x1 = rect.x,
7197 y1 = rect.y,
4db6714b
KH
7198 x2 = rect.x + rect.width - 1,
7199 y2 = rect.y + rect.height - 1;
283b7808
VZ
7200
7201 dc.SetPen(*wxWHITE_PEN);
00cf1208
RR
7202 dc.DrawLine(x1, y1, x2, y1);
7203 dc.DrawLine(x1, y1, x1, y2);
283b7808 7204
283b7808 7205 dc.DrawLine(x1 + 1, y2 - 1, x2 - 1, y2 - 1);
2f024384 7206 dc.DrawLine(x2 - 1, y1 + 1, x2 - 1, y2);
283b7808
VZ
7207
7208 dc.SetPen(*wxBLACK_PEN);
7209 dc.DrawLine(x1, y2, x2, y2);
2f024384 7210 dc.DrawLine(x2, y1, x2, y2 + 1);
a9339fe2 7211#endif
2d66e025 7212}
f85afd4e 7213
2d66e025
MB
7214void wxGrid::DrawCellBorder( wxDC& dc, const wxGridCellCoords& coords )
7215{
2d66e025
MB
7216 int row = coords.GetRow();
7217 int col = coords.GetCol();
7c1cb261
VZ
7218 if ( GetColWidth(col) <= 0 || GetRowHeight(row) <= 0 )
7219 return;
7220
7221 dc.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID) );
60ff3b99 7222
27f35b66
SN
7223 wxRect rect = CellToRect( row, col );
7224
2d66e025 7225 // right hand border
27f35b66
SN
7226 dc.DrawLine( rect.x + rect.width, rect.y,
7227 rect.x + rect.width, rect.y + rect.height + 1 );
2d66e025
MB
7228
7229 // bottom border
2f024384 7230 dc.DrawLine( rect.x, rect.y + rect.height,
27f35b66 7231 rect.x + rect.width, rect.y + rect.height);
f85afd4e
MB
7232}
7233
2f024384 7234void wxGrid::DrawHighlight(wxDC& dc, const wxGridCellCoordsArray& cells)
b3a7510d 7235{
2a7750d9
MB
7236 // This if block was previously in wxGrid::OnPaint but that doesn't
7237 // seem to get called under wxGTK - MB
7238 //
2f024384 7239 if ( m_currentCellCoords == wxGridNoCellCoords &&
2a7750d9
MB
7240 m_numRows && m_numCols )
7241 {
7242 m_currentCellCoords.Set(0, 0);
7243 }
7244
f6bcfd97 7245 if ( IsCellEditControlShown() )
99306db2
VZ
7246 {
7247 // don't show highlight when the edit control is shown
7248 return;
7249 }
7250
b3a7510d
VZ
7251 // if the active cell was repainted, repaint its highlight too because it
7252 // might have been damaged by the grid lines
d10f4bf9 7253 size_t count = cells.GetCount();
b3a7510d
VZ
7254 for ( size_t n = 0; n < count; n++ )
7255 {
d10f4bf9 7256 if ( cells[n] == m_currentCellCoords )
b3a7510d
VZ
7257 {
7258 wxGridCellAttr* attr = GetCellAttr(m_currentCellCoords);
7259 DrawCellHighlight(dc, attr);
7260 attr->DecRef();
7261
7262 break;
7263 }
7264 }
7265}
2d66e025 7266
2d66e025
MB
7267// TODO: remove this ???
7268// This is used to redraw all grid lines e.g. when the grid line colour
7269// has been changed
7270//
33ac7e6f 7271void wxGrid::DrawAllGridLines( wxDC& dc, const wxRegion & WXUNUSED(reg) )
f85afd4e 7272{
27f35b66
SN
7273#if !WXGRID_DRAW_LINES
7274 return;
7275#endif
7276
afd0f084 7277 if ( !m_gridLinesEnabled || !m_numRows || !m_numCols )
4db6714b 7278 return;
f85afd4e 7279
2d66e025 7280 int top, bottom, left, right;
796df70a 7281
f6bcfd97 7282#if 0 //#ifndef __WXGTK__
508011ce
VZ
7283 if (reg.IsEmpty())
7284 {
796df70a
SN
7285 int cw, ch;
7286 m_gridWin->GetClientSize(&cw, &ch);
7287
7288 // virtual coords of visible area
7289 //
7290 CalcUnscrolledPosition( 0, 0, &left, &top );
7291 CalcUnscrolledPosition( cw, ch, &right, &bottom );
7292 }
508011ce
VZ
7293 else
7294 {
796df70a
SN
7295 wxCoord x, y, w, h;
7296 reg.GetBox(x, y, w, h);
7297 CalcUnscrolledPosition( x, y, &left, &top );
7298 CalcUnscrolledPosition( x + w, y + h, &right, &bottom );
7299 }
8e217128
RR
7300#else
7301 int cw, ch;
7302 m_gridWin->GetClientSize(&cw, &ch);
7303 CalcUnscrolledPosition( 0, 0, &left, &top );
7304 CalcUnscrolledPosition( cw, ch, &right, &bottom );
7305#endif
f85afd4e 7306
9496deb5
MB
7307 // avoid drawing grid lines past the last row and col
7308 //
7c1cb261
VZ
7309 right = wxMin( right, GetColRight(m_numCols - 1) );
7310 bottom = wxMin( bottom, GetRowBottom(m_numRows - 1) );
9496deb5 7311
27f35b66 7312 // no gridlines inside multicells, clip them out
a9339fe2
DS
7313 int leftCol = internalXToCol(left);
7314 int topRow = internalYToRow(top);
7315 int rightCol = internalXToCol(right);
a967f048 7316 int bottomRow = internalYToRow(bottom);
27f35b66 7317
c03bf0c7
SC
7318#ifndef __WXMAC__
7319 // CS: I don't know why suddenly unscrolled coordinates are used for clipping
7320 wxRegion clippedcells(0, 0, cw, ch);
27f35b66 7321
a967f048
RG
7322 int i, j, cell_rows, cell_cols;
7323 wxRect rect;
27f35b66 7324
a967f048
RG
7325 for (j=topRow; j<bottomRow; j++)
7326 {
7327 for (i=leftCol; i<rightCol; i++)
27f35b66 7328 {
a967f048
RG
7329 GetCellSize( j, i, &cell_rows, &cell_cols );
7330 if ((cell_rows > 1) || (cell_cols > 1))
27f35b66 7331 {
a967f048
RG
7332 rect = CellToRect(j,i);
7333 CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
7334 clippedcells.Subtract(rect);
7335 }
7336 else if ((cell_rows < 0) || (cell_cols < 0))
7337 {
a9339fe2 7338 rect = CellToRect(j + cell_rows, i + cell_cols);
a967f048
RG
7339 CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
7340 clippedcells.Subtract(rect);
27f35b66
SN
7341 }
7342 }
7343 }
c03bf0c7
SC
7344#else
7345 wxRegion clippedcells( left , top, right - left, bottom - top);
7346
7347 int i, j, cell_rows, cell_cols;
7348 wxRect rect;
7349
7350 for (j=topRow; j<bottomRow; j++)
7351 {
7352 for (i=leftCol; i<rightCol; i++)
7353 {
7354 GetCellSize( j, i, &cell_rows, &cell_cols );
7355 if ((cell_rows > 1) || (cell_cols > 1))
7356 {
2f024384 7357 rect = CellToRect(j, i);
c03bf0c7
SC
7358 clippedcells.Subtract(rect);
7359 }
7360 else if ((cell_rows < 0) || (cell_cols < 0))
7361 {
2f024384 7362 rect = CellToRect(j + cell_rows, i + cell_cols);
c03bf0c7
SC
7363 clippedcells.Subtract(rect);
7364 }
7365 }
7366 }
7367#endif
a9339fe2 7368
27f35b66
SN
7369 dc.SetClippingRegion( clippedcells );
7370
2d66e025 7371 dc.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID) );
f85afd4e 7372
2d66e025 7373 // horizontal grid lines
f85afd4e 7374 //
a967f048 7375 // already declared above - int i;
33188aa4 7376 for ( i = internalYToRow(top); i < m_numRows; i++ )
f85afd4e 7377 {
6d55126d 7378 int bot = GetRowBottom(i) - 1;
7c1cb261 7379
6d55126d 7380 if ( bot > bottom )
2d66e025
MB
7381 {
7382 break;
7383 }
7c1cb261 7384
6d55126d 7385 if ( bot >= top )
2d66e025 7386 {
6d55126d 7387 dc.DrawLine( left, bot, right, bot );
2d66e025 7388 }
f85afd4e
MB
7389 }
7390
2d66e025
MB
7391 // vertical grid lines
7392 //
33188aa4 7393 for ( i = internalXToCol(left); i < m_numCols; i++ )
f85afd4e 7394 {
7c1cb261
VZ
7395 int colRight = GetColRight(i) - 1;
7396 if ( colRight > right )
2d66e025
MB
7397 {
7398 break;
7399 }
7c1cb261
VZ
7400
7401 if ( colRight >= left )
2d66e025 7402 {
7c1cb261 7403 dc.DrawLine( colRight, top, colRight, bottom );
2d66e025
MB
7404 }
7405 }
a9339fe2 7406
27f35b66 7407 dc.DestroyClippingRegion();
2d66e025 7408}
f85afd4e 7409
d10f4bf9 7410void wxGrid::DrawRowLabels( wxDC& dc ,const wxArrayInt& rows)
2d66e025 7411{
4db6714b
KH
7412 if ( !m_numRows )
7413 return;
60ff3b99 7414
2d66e025 7415 size_t i;
d10f4bf9 7416 size_t numLabels = rows.GetCount();
60ff3b99 7417
2f024384 7418 for ( i = 0; i < numLabels; i++ )
2d66e025 7419 {
d10f4bf9 7420 DrawRowLabel( dc, rows[i] );
60ff3b99 7421 }
f85afd4e
MB
7422}
7423
2d66e025 7424void wxGrid::DrawRowLabel( wxDC& dc, int row )
f85afd4e 7425{
659af826 7426 if ( GetRowHeight(row) <= 0 || m_rowLabelWidth <= 0 )
7c1cb261 7427 return;
60ff3b99 7428
4d1bc39c 7429 wxRect rect;
2f024384 7430
c2651b0a 7431#ifdef __WXGTK20__
4d1bc39c
RR
7432 rect.SetX( 1 );
7433 rect.SetY( GetRowTop(row) + 1 );
7434 rect.SetWidth( m_rowLabelWidth - 2 );
7435 rect.SetHeight( GetRowHeight(row) - 2 );
ec157c8f 7436
4d1bc39c 7437 CalcScrolledPosition( 0, rect.y, NULL, &rect.y );
ec157c8f 7438
4d1bc39c
RR
7439 wxWindowDC *win_dc = (wxWindowDC*) &dc;
7440
7441 wxRendererNative::Get().DrawHeaderButton( win_dc->m_owner, dc, rect, 0 );
7442#else
7c1cb261
VZ
7443 int rowTop = GetRowTop(row),
7444 rowBottom = GetRowBottom(row) - 1;
b99be8fb 7445
a9339fe2
DS
7446 dc.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DDKSHADOW), 1, wxSOLID) );
7447 dc.DrawLine( m_rowLabelWidth - 1, rowTop, m_rowLabelWidth - 1, rowBottom );
73145b0e 7448 dc.DrawLine( 0, rowTop, 0, rowBottom );
73145b0e 7449 dc.DrawLine( 0, rowBottom, m_rowLabelWidth, rowBottom );
b99be8fb 7450
2d66e025 7451 dc.SetPen( *wxWHITE_PEN );
73145b0e 7452 dc.DrawLine( 1, rowTop, 1, rowBottom );
a9339fe2 7453 dc.DrawLine( 1, rowTop, m_rowLabelWidth - 1, rowTop );
4d1bc39c 7454#endif
2f024384 7455
f85afd4e 7456 dc.SetBackgroundMode( wxTRANSPARENT );
f85afd4e
MB
7457 dc.SetTextForeground( GetLabelTextColour() );
7458 dc.SetFont( GetLabelFont() );
8f177c8e 7459
f85afd4e 7460 int hAlign, vAlign;
2d66e025 7461 GetRowLabelAlignment( &hAlign, &vAlign );
60ff3b99 7462
2d66e025 7463 rect.SetX( 2 );
7c1cb261 7464 rect.SetY( GetRowTop(row) + 2 );
2d66e025 7465 rect.SetWidth( m_rowLabelWidth - 4 );
7c1cb261 7466 rect.SetHeight( GetRowHeight(row) - 4 );
60ff3b99 7467 DrawTextRectangle( dc, GetRowLabelValue( row ), rect, hAlign, vAlign );
f85afd4e
MB
7468}
7469
d10f4bf9 7470void wxGrid::DrawColLabels( wxDC& dc,const wxArrayInt& cols )
f85afd4e 7471{
4db6714b
KH
7472 if ( !m_numCols )
7473 return;
60ff3b99 7474
2d66e025 7475 size_t i;
d10f4bf9 7476 size_t numLabels = cols.GetCount();
60ff3b99 7477
2d66e025 7478 for ( i = 0; i < numLabels; i++ )
f85afd4e 7479 {
d10f4bf9 7480 DrawColLabel( dc, cols[i] );
60ff3b99 7481 }
f85afd4e
MB
7482}
7483
2d66e025 7484void wxGrid::DrawColLabel( wxDC& dc, int col )
f85afd4e 7485{
659af826 7486 if ( GetColWidth(col) <= 0 || m_colLabelHeight <= 0 )
7c1cb261 7487 return;
60ff3b99 7488
4d1bc39c 7489 int colLeft = GetColLeft(col);
ec157c8f 7490
4d1bc39c 7491 wxRect rect;
2f024384 7492
c2651b0a 7493#ifdef __WXGTK20__
4d1bc39c
RR
7494 rect.SetX( colLeft + 1 );
7495 rect.SetY( 1 );
7496 rect.SetWidth( GetColWidth(col) - 2 );
7497 rect.SetHeight( m_colLabelHeight - 2 );
ec157c8f 7498
4d1bc39c
RR
7499 wxWindowDC *win_dc = (wxWindowDC*) &dc;
7500
7501 wxRendererNative::Get().DrawHeaderButton( win_dc->m_owner, dc, rect, 0 );
7502#else
7503 int colRight = GetColRight(col) - 1;
b99be8fb 7504
2f024384 7505 dc.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DDKSHADOW), 1, wxSOLID) );
ccdee36f 7506 dc.DrawLine( colRight, 0, colRight, m_colLabelHeight - 1 );
73145b0e 7507 dc.DrawLine( colLeft, 0, colRight, 0 );
ccdee36f
DS
7508 dc.DrawLine( colLeft, m_colLabelHeight - 1,
7509 colRight + 1, m_colLabelHeight - 1 );
b99be8fb 7510
f85afd4e 7511 dc.SetPen( *wxWHITE_PEN );
ccdee36f 7512 dc.DrawLine( colLeft, 1, colLeft, m_colLabelHeight - 1 );
73145b0e 7513 dc.DrawLine( colLeft, 1, colRight, 1 );
4d1bc39c 7514#endif
2f024384 7515
b0d6ff2f
MB
7516 dc.SetBackgroundMode( wxTRANSPARENT );
7517 dc.SetTextForeground( GetLabelTextColour() );
7518 dc.SetFont( GetLabelFont() );
8f177c8e 7519
d43851f7 7520 int hAlign, vAlign, orient;
2d66e025 7521 GetColLabelAlignment( &hAlign, &vAlign );
d43851f7 7522 orient = GetColLabelTextOrientation();
60ff3b99 7523
7c1cb261 7524 rect.SetX( colLeft + 2 );
2d66e025 7525 rect.SetY( 2 );
7c1cb261 7526 rect.SetWidth( GetColWidth(col) - 4 );
2d66e025 7527 rect.SetHeight( m_colLabelHeight - 4 );
d43851f7 7528 DrawTextRectangle( dc, GetColLabelValue( col ), rect, hAlign, vAlign, orient );
f85afd4e
MB
7529}
7530
2d66e025
MB
7531void wxGrid::DrawTextRectangle( wxDC& dc,
7532 const wxString& value,
7533 const wxRect& rect,
7534 int horizAlign,
d43851f7
JS
7535 int vertAlign,
7536 int textOrientation )
f85afd4e 7537{
2d66e025 7538 wxArrayString lines;
ef5df12b 7539
2d66e025 7540 StringToLines( value, lines );
ef5df12b 7541
2f024384 7542 // Forward to new API.
a9339fe2 7543 DrawTextRectangle( dc,
038a5591
JS
7544 lines,
7545 rect,
7546 horizAlign,
7547 vertAlign,
7548 textOrientation );
d10f4bf9
VZ
7549}
7550
7551void wxGrid::DrawTextRectangle( wxDC& dc,
038a5591
JS
7552 const wxArrayString& lines,
7553 const wxRect& rect,
7554 int horizAlign,
7555 int vertAlign,
7556 int textOrientation )
d10f4bf9 7557{
8d7eaf91
MW
7558 long textWidth = 0, textHeight = 0;
7559 long lineWidth = 0, lineHeight = 0;
73145b0e 7560 int nLines;
ef5df12b 7561
a373d23b 7562 dc.SetClippingRegion( rect );
ef5df12b 7563
73145b0e 7564 nLines = lines.GetCount();
4db6714b 7565 if ( nLines > 0 )
2d66e025 7566 {
038a5591
JS
7567 int l;
7568 float x = 0.0, y = 0.0;
ef5df12b 7569
4db6714b 7570 if ( textOrientation == wxHORIZONTAL )
a9339fe2 7571 GetTextBoxSize( dc, lines, &textWidth, &textHeight );
038a5591
JS
7572 else
7573 GetTextBoxSize( dc, lines, &textHeight, &textWidth );
ef5df12b 7574
962a48f6 7575 switch ( vertAlign )
038a5591 7576 {
73145b0e 7577 case wxALIGN_BOTTOM:
4db6714b 7578 if ( textOrientation == wxHORIZONTAL )
038a5591 7579 y = rect.y + (rect.height - textHeight - 1);
d43851f7 7580 else
038a5591
JS
7581 x = rect.x + rect.width - textWidth;
7582 break;
ef5df12b 7583
73145b0e 7584 case wxALIGN_CENTRE:
4db6714b 7585 if ( textOrientation == wxHORIZONTAL )
a9339fe2 7586 y = rect.y + ((rect.height - textHeight) / 2);
d43851f7 7587 else
a9339fe2 7588 x = rect.x + ((rect.width - textWidth) / 2);
038a5591 7589 break;
ef5df12b 7590
73145b0e
JS
7591 case wxALIGN_TOP:
7592 default:
4db6714b 7593 if ( textOrientation == wxHORIZONTAL )
038a5591 7594 y = rect.y + 1;
d43851f7 7595 else
038a5591 7596 x = rect.x + 1;
73145b0e 7597 break;
f85afd4e 7598 }
ef5df12b 7599
038a5591 7600 // Align each line of a multi-line label
4db6714b 7601 for ( l = 0; l < nLines; l++ )
038a5591
JS
7602 {
7603 dc.GetTextExtent(lines[l], &lineWidth, &lineHeight);
ef5df12b 7604
962a48f6 7605 switch ( horizAlign )
038a5591
JS
7606 {
7607 case wxALIGN_RIGHT:
4db6714b 7608 if ( textOrientation == wxHORIZONTAL )
038a5591
JS
7609 x = rect.x + (rect.width - lineWidth - 1);
7610 else
7611 y = rect.y + lineWidth + 1;
7612 break;
ef5df12b 7613
038a5591 7614 case wxALIGN_CENTRE:
4db6714b 7615 if ( textOrientation == wxHORIZONTAL )
2f024384 7616 x = rect.x + ((rect.width - lineWidth) / 2);
038a5591 7617 else
2f024384 7618 y = rect.y + rect.height - ((rect.height - lineWidth) / 2);
038a5591 7619 break;
ef5df12b 7620
038a5591
JS
7621 case wxALIGN_LEFT:
7622 default:
4db6714b 7623 if ( textOrientation == wxHORIZONTAL )
038a5591
JS
7624 x = rect.x + 1;
7625 else
7626 y = rect.y + rect.height - 1;
7627 break;
7628 }
ef5df12b 7629
4db6714b 7630 if ( textOrientation == wxHORIZONTAL )
038a5591
JS
7631 {
7632 dc.DrawText( lines[l], (int)x, (int)y );
7633 y += lineHeight;
7634 }
7635 else
7636 {
7637 dc.DrawRotatedText( lines[l], (int)x, (int)y, 90.0 );
7638 x += lineHeight;
7639 }
7640 }
f85afd4e 7641 }
4db6714b 7642
f85afd4e 7643 dc.DestroyClippingRegion();
f85afd4e
MB
7644}
7645
2f024384
DS
7646// Split multi-line text up into an array of strings.
7647// Any existing contents of the string array are preserved.
f85afd4e
MB
7648//
7649void wxGrid::StringToLines( const wxString& value, wxArrayString& lines )
7650{
f85afd4e
MB
7651 int startPos = 0;
7652 int pos;
6d004f67 7653 wxString eol = wxTextFile::GetEOL( wxTextFileType_Unix );
2433bb2e 7654 wxString tVal = wxTextFile::Translate( value, wxTextFileType_Unix );
e2b42eeb 7655
faa94f3e 7656 while ( startPos < (int)tVal.length() )
f85afd4e 7657 {
2433bb2e 7658 pos = tVal.Mid(startPos).Find( eol );
f85afd4e
MB
7659 if ( pos < 0 )
7660 {
7661 break;
7662 }
7663 else if ( pos == 0 )
7664 {
7665 lines.Add( wxEmptyString );
7666 }
7667 else
7668 {
2433bb2e 7669 lines.Add( value.Mid(startPos, pos) );
f85afd4e 7670 }
a9339fe2 7671
4db6714b 7672 startPos += pos + 1;
f85afd4e 7673 }
4db6714b 7674
faa94f3e 7675 if ( startPos < (int)value.length() )
f85afd4e
MB
7676 {
7677 lines.Add( value.Mid( startPos ) );
7678 }
7679}
7680
fbfb8bcc 7681void wxGrid::GetTextBoxSize( const wxDC& dc,
d10f4bf9 7682 const wxArrayString& lines,
f85afd4e
MB
7683 long *width, long *height )
7684{
7685 long w = 0;
7686 long h = 0;
8d7eaf91 7687 long lineW = 0, lineH = 0;
f85afd4e 7688
b540eb2b 7689 size_t i;
f85afd4e
MB
7690 for ( i = 0; i < lines.GetCount(); i++ )
7691 {
7692 dc.GetTextExtent( lines[i], &lineW, &lineH );
7693 w = wxMax( w, lineW );
7694 h += lineH;
7695 }
7696
7697 *width = w;
7698 *height = h;
7699}
7700
f6bcfd97
BP
7701//
7702// ------ Batch processing.
7703//
7704void wxGrid::EndBatch()
7705{
7706 if ( m_batchCount > 0 )
7707 {
7708 m_batchCount--;
7709 if ( !m_batchCount )
7710 {
7711 CalcDimensions();
7712 m_rowLabelWin->Refresh();
7713 m_colLabelWin->Refresh();
7714 m_cornerLabelWin->Refresh();
7715 m_gridWin->Refresh();
7716 }
7717 }
7718}
f85afd4e 7719
d8232393
MB
7720// Use this, rather than wxWindow::Refresh(), to force an immediate
7721// repainting of the grid. Has no effect if you are already inside a
7722// BeginBatch / EndBatch block.
7723//
7724void wxGrid::ForceRefresh()
7725{
7726 BeginBatch();
7727 EndBatch();
7728}
7729
bf646121
VZ
7730bool wxGrid::Enable(bool enable)
7731{
7732 if ( !wxScrolledWindow::Enable(enable) )
7733 return false;
7734
7735 // redraw in the new state
7736 m_gridWin->Refresh();
7737
7738 return true;
7739}
d8232393 7740
f85afd4e 7741//
2d66e025 7742// ------ Edit control functions
f85afd4e
MB
7743//
7744
2d66e025 7745void wxGrid::EnableEditing( bool edit )
f85afd4e 7746{
2d66e025
MB
7747 // TODO: improve this ?
7748 //
7749 if ( edit != m_editable )
f85afd4e 7750 {
4db6714b
KH
7751 if (!edit)
7752 EnableCellEditControl(edit);
2d66e025 7753 m_editable = edit;
f85afd4e 7754 }
f85afd4e
MB
7755}
7756
2d66e025 7757void wxGrid::EnableCellEditControl( bool enable )
f85afd4e 7758{
2c9a89e0
RD
7759 if (! m_editable)
7760 return;
7761
2c9a89e0
RD
7762 if ( enable != m_cellEditCtrlEnabled )
7763 {
dcfe4c3d 7764 if ( enable )
f85afd4e 7765 {
97a9929e
VZ
7766 if (SendEvent( wxEVT_GRID_EDITOR_SHOWN) <0)
7767 return;
fe7b9ed6 7768
97a9929e 7769 // this should be checked by the caller!
a9339fe2 7770 wxASSERT_MSG( CanEnableCellControl(), _T("can't enable editing for this cell!") );
b54ba671
VZ
7771
7772 // do it before ShowCellEditControl()
dcfe4c3d 7773 m_cellEditCtrlEnabled = enable;
b54ba671 7774
2d66e025 7775 ShowCellEditControl();
2d66e025
MB
7776 }
7777 else
7778 {
97a9929e 7779 //FIXME:add veto support
a9339fe2 7780 SendEvent( wxEVT_GRID_EDITOR_HIDDEN );
fe7b9ed6 7781
97a9929e 7782 HideCellEditControl();
2d66e025 7783 SaveEditControlValue();
b54ba671
VZ
7784
7785 // do it after HideCellEditControl()
dcfe4c3d 7786 m_cellEditCtrlEnabled = enable;
f85afd4e 7787 }
f85afd4e 7788 }
f85afd4e 7789}
f85afd4e 7790
b54ba671 7791bool wxGrid::IsCurrentCellReadOnly() const
283b7808 7792{
b54ba671
VZ
7793 // const_cast
7794 wxGridCellAttr* attr = ((wxGrid *)this)->GetCellAttr(m_currentCellCoords);
7795 bool readonly = attr->IsReadOnly();
7796 attr->DecRef();
283b7808 7797
b54ba671
VZ
7798 return readonly;
7799}
283b7808 7800
b54ba671
VZ
7801bool wxGrid::CanEnableCellControl() const
7802{
20c84410
SN
7803 return m_editable && (m_currentCellCoords != wxGridNoCellCoords) &&
7804 !IsCurrentCellReadOnly();
b54ba671
VZ
7805}
7806
7807bool wxGrid::IsCellEditControlEnabled() const
7808{
7809 // the cell edit control might be disable for all cells or just for the
7810 // current one if it's read only
ca65c044 7811 return m_cellEditCtrlEnabled ? !IsCurrentCellReadOnly() : false;
283b7808
VZ
7812}
7813
f6bcfd97
BP
7814bool wxGrid::IsCellEditControlShown() const
7815{
ca65c044 7816 bool isShown = false;
f6bcfd97
BP
7817
7818 if ( m_cellEditCtrlEnabled )
7819 {
7820 int row = m_currentCellCoords.GetRow();
7821 int col = m_currentCellCoords.GetCol();
7822 wxGridCellAttr* attr = GetCellAttr(row, col);
7823 wxGridCellEditor* editor = attr->GetEditor((wxGrid*) this, row, col);
7824 attr->DecRef();
7825
7826 if ( editor )
7827 {
7828 if ( editor->IsCreated() )
7829 {
7830 isShown = editor->GetControl()->IsShown();
7831 }
7832
7833 editor->DecRef();
7834 }
7835 }
7836
7837 return isShown;
7838}
7839
2d66e025 7840void wxGrid::ShowCellEditControl()
f85afd4e 7841{
2d66e025 7842 if ( IsCellEditControlEnabled() )
f85afd4e 7843 {
7db713ae 7844 if ( !IsVisible( m_currentCellCoords, false ) )
2d66e025 7845 {
ca65c044 7846 m_cellEditCtrlEnabled = false;
2d66e025
MB
7847 return;
7848 }
7849 else
7850 {
2c9a89e0
RD
7851 wxRect rect = CellToRect( m_currentCellCoords );
7852 int row = m_currentCellCoords.GetRow();
7853 int col = m_currentCellCoords.GetCol();
2d66e025 7854
27f35b66
SN
7855 // if this is part of a multicell, find owner (topleft)
7856 int cell_rows, cell_cols;
7857 GetCellSize( row, col, &cell_rows, &cell_cols );
7858 if ( cell_rows <= 0 || cell_cols <= 0 )
7859 {
7860 row += cell_rows;
7861 col += cell_cols;
7862 m_currentCellCoords.SetRow( row );
7863 m_currentCellCoords.SetCol( col );
7864 }
7865
2d66e025 7866 // convert to scrolled coords
99306db2 7867 CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
508011ce 7868
7db713ae 7869 int nXMove = 0;
bee19958 7870 if (rect.x < 0)
7db713ae
JS
7871 nXMove = rect.x;
7872
a9339fe2 7873 // performed in PaintBackground()
99306db2
VZ
7874#if 0
7875 // erase the highlight and the cell contents because the editor
7876 // might not cover the entire cell
7877 wxClientDC dc( m_gridWin );
7878 PrepareDC( dc );
7879 dc.SetBrush(*wxLIGHT_GREY_BRUSH); //wxBrush(attr->GetBackgroundColour(), wxSOLID));
7880 dc.SetPen(*wxTRANSPARENT_PEN);
7881 dc.DrawRectangle(rect);
a9339fe2 7882#endif
d2fdd8d2 7883
99306db2 7884 // cell is shifted by one pixel
68c5a31c 7885 // However, don't allow x or y to become negative
70e8d961 7886 // since the SetSize() method interprets that as
68c5a31c
SN
7887 // "don't change."
7888 if (rect.x > 0)
7889 rect.x--;
7890 if (rect.y > 0)
7891 rect.y--;
f0102d2a 7892
508011ce 7893 wxGridCellAttr* attr = GetCellAttr(row, col);
28a77bc4 7894 wxGridCellEditor* editor = attr->GetEditor(this, row, col);
3da93aae
VZ
7895 if ( !editor->IsCreated() )
7896 {
ca65c044 7897 editor->Create(m_gridWin, wxID_ANY,
2c9a89e0 7898 new wxGridCellEditorEvtHandler(this, editor));
bf7945ce
RD
7899
7900 wxGridEditorCreatedEvent evt(GetId(),
7901 wxEVT_GRID_EDITOR_CREATED,
7902 this,
7903 row,
7904 col,
7905 editor->GetControl());
7906 GetEventHandler()->ProcessEvent(evt);
2d66e025
MB
7907 }
7908
27f35b66 7909 // resize editor to overflow into righthand cells if allowed
39b80349 7910 int maxWidth = rect.width;
27f35b66
SN
7911 wxString value = GetCellValue(row, col);
7912 if ( (value != wxEmptyString) && (attr->GetOverflow()) )
7913 {
39b80349 7914 int y;
2f024384
DS
7915 GetTextExtent(value, &maxWidth, &y, NULL, NULL, &attr->GetFont());
7916 if (maxWidth < rect.width)
7917 maxWidth = rect.width;
39b80349 7918 }
4db6714b 7919
39b80349 7920 int client_right = m_gridWin->GetClientSize().GetWidth();
2f024384 7921 if (rect.x + maxWidth > client_right)
2b5f62a0 7922 maxWidth = client_right - rect.x;
39b80349 7923
2b5f62a0
VZ
7924 if ((maxWidth > rect.width) && (col < m_numCols) && m_table)
7925 {
39b80349 7926 GetCellSize( row, col, &cell_rows, &cell_cols );
2b5f62a0 7927 // may have changed earlier
2f024384 7928 for (int i = col + cell_cols; i < m_numCols; i++)
2b5f62a0 7929 {
39b80349
SN
7930 int c_rows, c_cols;
7931 GetCellSize( row, i, &c_rows, &c_cols );
2f024384 7932
2b5f62a0 7933 // looks weird going over a multicell
bee19958 7934 if (m_table->IsEmptyCell( row, i ) &&
2b5f62a0 7935 (rect.width < maxWidth) && (c_rows == 1))
2f024384 7936 {
bee19958 7937 rect.width += GetColWidth( i );
2f024384 7938 }
2b5f62a0
VZ
7939 else
7940 break;
7941 }
4db6714b 7942
39b80349 7943 if (rect.GetRight() > client_right)
bee19958 7944 rect.SetRight( client_right - 1 );
27f35b66 7945 }
73145b0e 7946
bee19958 7947 editor->SetCellAttr( attr );
2c9a89e0 7948 editor->SetSize( rect );
2f024384
DS
7949 editor->GetControl()->Move(
7950 editor->GetControl()->GetPosition().x + nXMove,
7951 editor->GetControl()->GetPosition().y );
ca65c044 7952 editor->Show( true, attr );
04418332 7953
7db713ae 7954 int colXPos = 0;
faa94f3e 7955 for (int i = 0; i < m_currentCellCoords.GetCol(); i++)
7db713ae 7956 {
bee19958 7957 colXPos += GetColSize( i );
7db713ae 7958 }
bee19958
DS
7959
7960 int xUnit = 1, yUnit = 1;
7961 GetScrollPixelsPerUnit( &xUnit, &yUnit );
7db713ae 7962 if (m_currentCellCoords.GetCol() != 0)
bee19958 7963 Scroll( colXPos / xUnit - 1, GetScrollPos( wxVERTICAL ) );
7db713ae 7964 else
bee19958 7965 Scroll( colXPos / xUnit, GetScrollPos( wxVERTICAL ) );
7db713ae 7966
04418332
VZ
7967 // recalc dimensions in case we need to
7968 // expand the scrolled window to account for editor
73145b0e 7969 CalcDimensions();
99306db2 7970
3da93aae 7971 editor->BeginEdit(row, col, this);
1bd71df9 7972 editor->SetCellAttr(NULL);
0b190b0f
VZ
7973
7974 editor->DecRef();
2c9a89e0 7975 attr->DecRef();
2b5f62a0 7976 }
f85afd4e
MB
7977 }
7978}
7979
2d66e025 7980void wxGrid::HideCellEditControl()
f85afd4e 7981{
2d66e025 7982 if ( IsCellEditControlEnabled() )
f85afd4e 7983 {
2c9a89e0
RD
7984 int row = m_currentCellCoords.GetRow();
7985 int col = m_currentCellCoords.GetCol();
7986
bee19958 7987 wxGridCellAttr *attr = GetCellAttr(row, col);
0b190b0f 7988 wxGridCellEditor *editor = attr->GetEditor(this, row, col);
ca65c044 7989 editor->Show( false );
0b190b0f 7990 editor->DecRef();
2c9a89e0 7991 attr->DecRef();
2fb3e528 7992
48962b9f 7993 m_gridWin->SetFocus();
2fb3e528 7994
27f35b66
SN
7995 // refresh whole row to the right
7996 wxRect rect( CellToRect(row, col) );
7997 CalcScrolledPosition(rect.x, rect.y, &rect.x, &rect.y );
7998 rect.width = m_gridWin->GetClientSize().GetWidth() - rect.x;
2f024384 7999
7d75e6c6
RD
8000#ifdef __WXMAC__
8001 // ensure that the pixels under the focus ring get refreshed as well
4db6714b 8002 rect.Inflate(10, 10);
7d75e6c6 8003#endif
2f024384 8004
ca65c044 8005 m_gridWin->Refresh( false, &rect );
f85afd4e 8006 }
2d66e025 8007}
8f177c8e 8008
2d66e025
MB
8009void wxGrid::SaveEditControlValue()
8010{
3da93aae
VZ
8011 if ( IsCellEditControlEnabled() )
8012 {
2c9a89e0
RD
8013 int row = m_currentCellCoords.GetRow();
8014 int col = m_currentCellCoords.GetCol();
8f177c8e 8015
4db6714b 8016 wxString oldval = GetCellValue(row, col);
fe7b9ed6 8017
3da93aae 8018 wxGridCellAttr* attr = GetCellAttr(row, col);
28a77bc4 8019 wxGridCellEditor* editor = attr->GetEditor(this, row, col);
3324d5f5 8020 bool changed = editor->EndEdit(row, col, this);
2d66e025 8021
0b190b0f 8022 editor->DecRef();
2c9a89e0 8023 attr->DecRef();
2d66e025 8024
3da93aae
VZ
8025 if (changed)
8026 {
fe7b9ed6 8027 if ( SendEvent( wxEVT_GRID_CELL_CHANGE,
2d66e025 8028 m_currentCellCoords.GetRow(),
4db6714b
KH
8029 m_currentCellCoords.GetCol() ) < 0 )
8030 {
97a9929e 8031 // Event has been vetoed, set the data back.
4db6714b 8032 SetCellValue(row, col, oldval);
97a9929e 8033 }
2d66e025 8034 }
f85afd4e
MB
8035 }
8036}
8037
2d66e025 8038//
60ff3b99
VZ
8039// ------ Grid location functions
8040// Note that all of these functions work with the logical coordinates of
2d66e025
MB
8041// grid cells and labels so you will need to convert from device
8042// coordinates for mouse events etc.
8043//
8044
8045void wxGrid::XYToCell( int x, int y, wxGridCellCoords& coords )
f85afd4e 8046{
58dd5b3b
MB
8047 int row = YToRow(y);
8048 int col = XToCol(x);
8049
a9339fe2 8050 if ( row == -1 || col == -1 )
58dd5b3b
MB
8051 {
8052 coords = wxGridNoCellCoords;
8053 }
8054 else
8055 {
8056 coords.Set( row, col );
8057 }
2d66e025 8058}
f85afd4e 8059
b1da8107
SN
8060// Internal Helper function for computing row or column from some
8061// (unscrolled) coordinate value, using either
70e8d961 8062// m_defaultRowHeight/m_defaultColWidth or binary search on array
b1da8107
SN
8063// of m_rowBottoms/m_ColRights to speed up the search!
8064
8065static int CoordToRowOrCol(int coord, int defaultDist, int minDist,
64e15340 8066 const wxArrayInt& BorderArray, int nMax,
a967f048 8067 bool clipToMinMax)
2d66e025 8068{
a967f048
RG
8069 if (coord < 0)
8070 return clipToMinMax && (nMax > 0) ? 0 : -1;
8071
b1da8107
SN
8072 if (!defaultDist)
8073 defaultDist = 1;
a967f048 8074
1af546bf
VZ
8075 size_t i_max = coord / defaultDist,
8076 i_min = 0;
d57ad377 8077
b1da8107
SN
8078 if (BorderArray.IsEmpty())
8079 {
4db6714b 8080 if ((int) i_max < nMax)
64e15340 8081 return i_max;
a967f048 8082 return clipToMinMax ? nMax - 1 : -1;
b1da8107 8083 }
8f177c8e 8084
b1da8107 8085 if ( i_max >= BorderArray.GetCount())
2f024384 8086 {
b1da8107 8087 i_max = BorderArray.GetCount() - 1;
2f024384 8088 }
70e8d961 8089 else
f85afd4e 8090 {
b1da8107
SN
8091 if ( coord >= BorderArray[i_max])
8092 {
8093 i_min = i_max;
20c84410
SN
8094 if (minDist)
8095 i_max = coord / minDist;
8096 else
8097 i_max = BorderArray.GetCount() - 1;
b1da8107 8098 }
4db6714b 8099
b1da8107
SN
8100 if ( i_max >= BorderArray.GetCount())
8101 i_max = BorderArray.GetCount() - 1;
f85afd4e 8102 }
4db6714b 8103
33188aa4 8104 if ( coord >= BorderArray[i_max])
a967f048 8105 return clipToMinMax ? (int)i_max : -1;
68c5a31c
SN
8106 if ( coord < BorderArray[0] )
8107 return 0;
2d66e025 8108
68c5a31c 8109 while ( i_max - i_min > 0 )
b1da8107
SN
8110 {
8111 wxCHECK_MSG(BorderArray[i_min] <= coord && coord < BorderArray[i_max],
33188aa4 8112 0, _T("wxGrid: internal error in CoordToRowOrCol"));
b1da8107 8113 if (coord >= BorderArray[ i_max - 1])
b1da8107 8114 return i_max;
b1da8107
SN
8115 else
8116 i_max--;
8117 int median = i_min + (i_max - i_min + 1) / 2;
8118 if (coord < BorderArray[median])
8119 i_max = median;
8120 else
8121 i_min = median;
8122 }
4db6714b 8123
b1da8107 8124 return i_max;
f85afd4e
MB
8125}
8126
b1da8107 8127int wxGrid::YToRow( int y )
f85afd4e 8128{
b1da8107 8129 return CoordToRowOrCol(y, m_defaultRowHeight,
ca65c044 8130 m_minAcceptableRowHeight, m_rowBottoms, m_numRows, false);
b1da8107 8131}
8f177c8e 8132
b1da8107
SN
8133int wxGrid::XToCol( int x )
8134{
8135 return CoordToRowOrCol(x, m_defaultColWidth,
ca65c044 8136 m_minAcceptableColWidth, m_colRights, m_numCols, false);
2d66e025 8137}
8f177c8e 8138
2d66e025
MB
8139// return the row number that that the y coord is near the edge of, or
8140// -1 if not near an edge
8141//
8142int wxGrid::YToEdgeOfRow( int y )
8143{
33188aa4
SN
8144 int i;
8145 i = internalYToRow(y);
8146
8147 if ( GetRowHeight(i) > WXGRID_LABEL_EDGE_ZONE )
2d66e025 8148 {
33188aa4
SN
8149 // We know that we are in row i, test whether we are
8150 // close enough to lower or upper border, respectively.
8151 if ( abs(GetRowBottom(i) - y) < WXGRID_LABEL_EDGE_ZONE )
8152 return i;
4db6714b 8153 else if ( i > 0 && y - GetRowTop(i) < WXGRID_LABEL_EDGE_ZONE )
33188aa4 8154 return i - 1;
f85afd4e 8155 }
2d66e025
MB
8156
8157 return -1;
8158}
8159
2d66e025
MB
8160// return the col number that that the x coord is near the edge of, or
8161// -1 if not near an edge
8162//
8163int wxGrid::XToEdgeOfCol( int x )
8164{
33188aa4
SN
8165 int i;
8166 i = internalXToCol(x);
8167
8168 if ( GetColWidth(i) > WXGRID_LABEL_EDGE_ZONE )
f85afd4e 8169 {
a9339fe2 8170 // We know that we are in column i; test whether we are
33188aa4
SN
8171 // close enough to right or left border, respectively.
8172 if ( abs(GetColRight(i) - x) < WXGRID_LABEL_EDGE_ZONE )
8173 return i;
4db6714b 8174 else if ( i > 0 && x - GetColLeft(i) < WXGRID_LABEL_EDGE_ZONE )
33188aa4 8175 return i - 1;
f85afd4e 8176 }
2d66e025
MB
8177
8178 return -1;
f85afd4e
MB
8179}
8180
2d66e025 8181wxRect wxGrid::CellToRect( int row, int col )
f85afd4e 8182{
2d66e025 8183 wxRect rect( -1, -1, -1, -1 );
f85afd4e 8184
2f024384
DS
8185 if ( row >= 0 && row < m_numRows &&
8186 col >= 0 && col < m_numCols )
f85afd4e 8187 {
27f35b66
SN
8188 int i, cell_rows, cell_cols;
8189 rect.width = rect.height = 0;
8190 GetCellSize( row, col, &cell_rows, &cell_cols );
8191 // if negative then find multicell owner
2f024384
DS
8192 if (cell_rows < 0)
8193 row += cell_rows;
8194 if (cell_cols < 0)
8195 col += cell_cols;
27f35b66
SN
8196 GetCellSize( row, col, &cell_rows, &cell_cols );
8197
7c1cb261
VZ
8198 rect.x = GetColLeft(col);
8199 rect.y = GetRowTop(row);
2f024384
DS
8200 for (i=col; i < col + cell_cols; i++)
8201 rect.width += GetColWidth(i);
8202 for (i=row; i < row + cell_rows; i++)
27f35b66 8203 rect.height += GetRowHeight(i);
f85afd4e
MB
8204 }
8205
f6bcfd97 8206 // if grid lines are enabled, then the area of the cell is a bit smaller
4db6714b
KH
8207 if (m_gridLinesEnabled)
8208 {
f6bcfd97
BP
8209 rect.width -= 1;
8210 rect.height -= 1;
8211 }
4db6714b 8212
2d66e025
MB
8213 return rect;
8214}
8215
2d66e025
MB
8216bool wxGrid::IsVisible( int row, int col, bool wholeCellVisible )
8217{
8218 // get the cell rectangle in logical coords
8219 //
8220 wxRect r( CellToRect( row, col ) );
60ff3b99 8221
2d66e025
MB
8222 // convert to device coords
8223 //
8224 int left, top, right, bottom;
8225 CalcScrolledPosition( r.GetLeft(), r.GetTop(), &left, &top );
8226 CalcScrolledPosition( r.GetRight(), r.GetBottom(), &right, &bottom );
60ff3b99 8227
2d66e025 8228 // check against the client area of the grid window
2d66e025
MB
8229 int cw, ch;
8230 m_gridWin->GetClientSize( &cw, &ch );
60ff3b99 8231
2d66e025 8232 if ( wholeCellVisible )
f85afd4e 8233 {
2d66e025 8234 // is the cell wholly visible ?
2f024384
DS
8235 return ( left >= 0 && right <= cw &&
8236 top >= 0 && bottom <= ch );
2d66e025
MB
8237 }
8238 else
8239 {
8240 // is the cell partly visible ?
8241 //
2f024384
DS
8242 return ( ((left >= 0 && left < cw) || (right > 0 && right <= cw)) &&
8243 ((top >= 0 && top < ch) || (bottom > 0 && bottom <= ch)) );
2d66e025
MB
8244 }
8245}
8246
2d66e025
MB
8247// make the specified cell location visible by doing a minimal amount
8248// of scrolling
8249//
8250void wxGrid::MakeCellVisible( int row, int col )
8251{
8252 int i;
60ff3b99 8253 int xpos = -1, ypos = -1;
2d66e025 8254
2f024384
DS
8255 if ( row >= 0 && row < m_numRows &&
8256 col >= 0 && col < m_numCols )
2d66e025
MB
8257 {
8258 // get the cell rectangle in logical coords
2d66e025 8259 wxRect r( CellToRect( row, col ) );
60ff3b99 8260
2d66e025 8261 // convert to device coords
2d66e025
MB
8262 int left, top, right, bottom;
8263 CalcScrolledPosition( r.GetLeft(), r.GetTop(), &left, &top );
8264 CalcScrolledPosition( r.GetRight(), r.GetBottom(), &right, &bottom );
60ff3b99 8265
2d66e025
MB
8266 int cw, ch;
8267 m_gridWin->GetClientSize( &cw, &ch );
60ff3b99 8268
2d66e025 8269 if ( top < 0 )
3f296516 8270 {
2d66e025 8271 ypos = r.GetTop();
3f296516 8272 }
2d66e025
MB
8273 else if ( bottom > ch )
8274 {
8275 int h = r.GetHeight();
8276 ypos = r.GetTop();
a9339fe2 8277 for ( i = row - 1; i >= 0; i-- )
2d66e025 8278 {
7c1cb261
VZ
8279 int rowHeight = GetRowHeight(i);
8280 if ( h + rowHeight > ch )
8281 break;
2d66e025 8282
7c1cb261
VZ
8283 h += rowHeight;
8284 ypos -= rowHeight;
2d66e025 8285 }
f0102d2a
VZ
8286
8287 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
8288 // have rounding errors (this is important, because if we do, we
8289 // might not scroll at all and some cells won't be redrawn)
275c4ae4
RD
8290 //
8291 // Sometimes GRID_SCROLL_LINE/2 is not enough, so just add a full
8292 // scroll unit...
675a9c0d 8293 ypos += m_scrollLineY;
2d66e025
MB
8294 }
8295
7db713ae 8296 // special handling for wide cells - show always left part of the cell!
faa94f3e 8297 // Otherwise, e.g. when stepping from row to row, it would jump between
7db713ae
JS
8298 // left and right part of the cell on every step!
8299// if ( left < 0 )
ccdee36f 8300 if ( left < 0 || (right - left) >= cw )
2d66e025
MB
8301 {
8302 xpos = r.GetLeft();
8303 }
8304 else if ( right > cw )
8305 {
73145b0e
JS
8306 // position the view so that the cell is on the right
8307 int x0, y0;
8308 CalcUnscrolledPosition(0, 0, &x0, &y0);
8309 xpos = x0 + (right - cw);
f0102d2a
VZ
8310
8311 // see comment for ypos above
675a9c0d 8312 xpos += m_scrollLineX;
2d66e025
MB
8313 }
8314
ccdee36f 8315 if ( xpos != -1 || ypos != -1 )
2d66e025 8316 {
97a9929e 8317 if ( xpos != -1 )
675a9c0d 8318 xpos /= m_scrollLineX;
97a9929e 8319 if ( ypos != -1 )
675a9c0d 8320 ypos /= m_scrollLineY;
2d66e025
MB
8321 Scroll( xpos, ypos );
8322 AdjustScrollbars();
8323 }
8324 }
8325}
8326
2d66e025
MB
8327//
8328// ------ Grid cursor movement functions
8329//
8330
5c8fc7c1 8331bool wxGrid::MoveCursorUp( bool expandSelection )
2d66e025 8332{
2f024384 8333 if ( m_currentCellCoords != wxGridNoCellCoords &&
f6bcfd97 8334 m_currentCellCoords.GetRow() >= 0 )
2d66e025 8335 {
bee19958 8336 if ( expandSelection )
d95b0c2b
SN
8337 {
8338 if ( m_selectingKeyboard == wxGridNoCellCoords )
8339 m_selectingKeyboard = m_currentCellCoords;
f6bcfd97
BP
8340 if ( m_selectingKeyboard.GetRow() > 0 )
8341 {
8342 m_selectingKeyboard.SetRow( m_selectingKeyboard.GetRow() - 1 );
8343 MakeCellVisible( m_selectingKeyboard.GetRow(),
8344 m_selectingKeyboard.GetCol() );
c9097836 8345 HighlightBlock( m_currentCellCoords, m_selectingKeyboard );
f6bcfd97 8346 }
d95b0c2b 8347 }
f6bcfd97 8348 else if ( m_currentCellCoords.GetRow() > 0 )
aa5e1f75 8349 {
962a48f6
DS
8350 int row = m_currentCellCoords.GetRow() - 1;
8351 int col = m_currentCellCoords.GetCol();
aa5e1f75 8352 ClearSelection();
962a48f6
DS
8353 MakeCellVisible( row, col );
8354 SetCurrentCell( row, col );
aa5e1f75 8355 }
f6bcfd97 8356 else
ca65c044 8357 return false;
4db6714b 8358
ca65c044 8359 return true;
f85afd4e 8360 }
2d66e025 8361
ca65c044 8362 return false;
2d66e025
MB
8363}
8364
5c8fc7c1 8365bool wxGrid::MoveCursorDown( bool expandSelection )
2d66e025 8366{
2f024384 8367 if ( m_currentCellCoords != wxGridNoCellCoords &&
f6bcfd97 8368 m_currentCellCoords.GetRow() < m_numRows )
f85afd4e 8369 {
5c8fc7c1 8370 if ( expandSelection )
d95b0c2b
SN
8371 {
8372 if ( m_selectingKeyboard == wxGridNoCellCoords )
8373 m_selectingKeyboard = m_currentCellCoords;
ccdee36f 8374 if ( m_selectingKeyboard.GetRow() < m_numRows - 1 )
f6bcfd97
BP
8375 {
8376 m_selectingKeyboard.SetRow( m_selectingKeyboard.GetRow() + 1 );
8377 MakeCellVisible( m_selectingKeyboard.GetRow(),
8378 m_selectingKeyboard.GetCol() );
c9097836 8379 HighlightBlock( m_currentCellCoords, m_selectingKeyboard );
f6bcfd97 8380 }
d95b0c2b 8381 }
f6bcfd97 8382 else if ( m_currentCellCoords.GetRow() < m_numRows - 1 )
aa5e1f75 8383 {
962a48f6
DS
8384 int row = m_currentCellCoords.GetRow() + 1;
8385 int col = m_currentCellCoords.GetCol();
aa5e1f75 8386 ClearSelection();
962a48f6
DS
8387 MakeCellVisible( row, col );
8388 SetCurrentCell( row, col );
aa5e1f75 8389 }
f6bcfd97 8390 else
ca65c044 8391 return false;
4db6714b 8392
ca65c044 8393 return true;
f85afd4e 8394 }
2d66e025 8395
ca65c044 8396 return false;
f85afd4e
MB
8397}
8398
5c8fc7c1 8399bool wxGrid::MoveCursorLeft( bool expandSelection )
f85afd4e 8400{
2f024384 8401 if ( m_currentCellCoords != wxGridNoCellCoords &&
f6bcfd97 8402 m_currentCellCoords.GetCol() >= 0 )
2d66e025 8403 {
5c8fc7c1 8404 if ( expandSelection )
d95b0c2b
SN
8405 {
8406 if ( m_selectingKeyboard == wxGridNoCellCoords )
8407 m_selectingKeyboard = m_currentCellCoords;
f6bcfd97
BP
8408 if ( m_selectingKeyboard.GetCol() > 0 )
8409 {
8410 m_selectingKeyboard.SetCol( m_selectingKeyboard.GetCol() - 1 );
8411 MakeCellVisible( m_selectingKeyboard.GetRow(),
8412 m_selectingKeyboard.GetCol() );
c9097836 8413 HighlightBlock( m_currentCellCoords, m_selectingKeyboard );
f6bcfd97 8414 }
d95b0c2b 8415 }
f6bcfd97 8416 else if ( m_currentCellCoords.GetCol() > 0 )
aa5e1f75 8417 {
962a48f6
DS
8418 int row = m_currentCellCoords.GetRow();
8419 int col = m_currentCellCoords.GetCol() - 1;
aa5e1f75 8420 ClearSelection();
962a48f6
DS
8421 MakeCellVisible( row, col );
8422 SetCurrentCell( row, col );
aa5e1f75 8423 }
f6bcfd97 8424 else
ca65c044 8425 return false;
4db6714b 8426
ca65c044 8427 return true;
2d66e025
MB
8428 }
8429
ca65c044 8430 return false;
2d66e025
MB
8431}
8432
5c8fc7c1 8433bool wxGrid::MoveCursorRight( bool expandSelection )
2d66e025 8434{
2f024384 8435 if ( m_currentCellCoords != wxGridNoCellCoords &&
f6bcfd97 8436 m_currentCellCoords.GetCol() < m_numCols )
f85afd4e 8437 {
5c8fc7c1 8438 if ( expandSelection )
d95b0c2b
SN
8439 {
8440 if ( m_selectingKeyboard == wxGridNoCellCoords )
8441 m_selectingKeyboard = m_currentCellCoords;
f6bcfd97
BP
8442 if ( m_selectingKeyboard.GetCol() < m_numCols - 1 )
8443 {
8444 m_selectingKeyboard.SetCol( m_selectingKeyboard.GetCol() + 1 );
8445 MakeCellVisible( m_selectingKeyboard.GetRow(),
8446 m_selectingKeyboard.GetCol() );
c9097836 8447 HighlightBlock( m_currentCellCoords, m_selectingKeyboard );
f6bcfd97 8448 }
d95b0c2b 8449 }
f6bcfd97 8450 else if ( m_currentCellCoords.GetCol() < m_numCols - 1 )
aa5e1f75 8451 {
962a48f6
DS
8452 int row = m_currentCellCoords.GetRow();
8453 int col = m_currentCellCoords.GetCol() + 1;
aa5e1f75 8454 ClearSelection();
962a48f6
DS
8455 MakeCellVisible( row, col );
8456 SetCurrentCell( row, col );
aa5e1f75 8457 }
f6bcfd97 8458 else
ca65c044 8459 return false;
4db6714b 8460
ca65c044 8461 return true;
f85afd4e 8462 }
8f177c8e 8463
ca65c044 8464 return false;
2d66e025
MB
8465}
8466
2d66e025
MB
8467bool wxGrid::MovePageUp()
8468{
4db6714b
KH
8469 if ( m_currentCellCoords == wxGridNoCellCoords )
8470 return false;
60ff3b99 8471
2d66e025
MB
8472 int row = m_currentCellCoords.GetRow();
8473 if ( row > 0 )
f85afd4e 8474 {
2d66e025
MB
8475 int cw, ch;
8476 m_gridWin->GetClientSize( &cw, &ch );
60ff3b99 8477
7c1cb261 8478 int y = GetRowTop(row);
a967f048 8479 int newRow = internalYToRow( y - ch + 1 );
ef5df12b 8480
a967f048 8481 if ( newRow == row )
2d66e025 8482 {
ca65c044 8483 //row > 0 , so newrow can never be less than 0 here.
2d66e025
MB
8484 newRow = row - 1;
8485 }
8f177c8e 8486
2d66e025
MB
8487 MakeCellVisible( newRow, m_currentCellCoords.GetCol() );
8488 SetCurrentCell( newRow, m_currentCellCoords.GetCol() );
60ff3b99 8489
ca65c044 8490 return true;
f85afd4e 8491 }
2d66e025 8492
ca65c044 8493 return false;
2d66e025
MB
8494}
8495
8496bool wxGrid::MovePageDown()
8497{
4db6714b
KH
8498 if ( m_currentCellCoords == wxGridNoCellCoords )
8499 return false;
60ff3b99 8500
2d66e025 8501 int row = m_currentCellCoords.GetRow();
ccdee36f 8502 if ( (row + 1) < m_numRows )
f85afd4e 8503 {
2d66e025
MB
8504 int cw, ch;
8505 m_gridWin->GetClientSize( &cw, &ch );
60ff3b99 8506
7c1cb261 8507 int y = GetRowTop(row);
a967f048
RG
8508 int newRow = internalYToRow( y + ch );
8509 if ( newRow == row )
2d66e025 8510 {
ef5df12b 8511 // row < m_numRows , so newrow can't overflow here.
ca65c044 8512 newRow = row + 1;
2d66e025
MB
8513 }
8514
8515 MakeCellVisible( newRow, m_currentCellCoords.GetCol() );
8516 SetCurrentCell( newRow, m_currentCellCoords.GetCol() );
60ff3b99 8517
ca65c044 8518 return true;
f85afd4e 8519 }
2d66e025 8520
ca65c044 8521 return false;
f85afd4e 8522}
8f177c8e 8523
5c8fc7c1 8524bool wxGrid::MoveCursorUpBlock( bool expandSelection )
2d66e025
MB
8525{
8526 if ( m_table &&
2f024384 8527 m_currentCellCoords != wxGridNoCellCoords &&
2d66e025
MB
8528 m_currentCellCoords.GetRow() > 0 )
8529 {
8530 int row = m_currentCellCoords.GetRow();
8531 int col = m_currentCellCoords.GetCol();
8532
8533 if ( m_table->IsEmptyCell(row, col) )
8534 {
8535 // starting in an empty cell: find the next block of
8536 // non-empty cells
8537 //
8538 while ( row > 0 )
8539 {
2f024384 8540 row--;
4db6714b
KH
8541 if ( !(m_table->IsEmptyCell(row, col)) )
8542 break;
2d66e025
MB
8543 }
8544 }
2f024384 8545 else if ( m_table->IsEmptyCell(row - 1, col) )
2d66e025
MB
8546 {
8547 // starting at the top of a block: find the next block
8548 //
8549 row--;
8550 while ( row > 0 )
8551 {
2f024384 8552 row--;
4db6714b
KH
8553 if ( !(m_table->IsEmptyCell(row, col)) )
8554 break;
2d66e025
MB
8555 }
8556 }
8557 else
8558 {
8559 // starting within a block: find the top of the block
8560 //
8561 while ( row > 0 )
8562 {
2f024384 8563 row--;
2d66e025
MB
8564 if ( m_table->IsEmptyCell(row, col) )
8565 {
2f024384 8566 row++;
2d66e025
MB
8567 break;
8568 }
8569 }
8570 }
f85afd4e 8571
2d66e025 8572 MakeCellVisible( row, col );
5c8fc7c1 8573 if ( expandSelection )
d95b0c2b
SN
8574 {
8575 m_selectingKeyboard = wxGridCellCoords( row, col );
c9097836 8576 HighlightBlock( m_currentCellCoords, m_selectingKeyboard );
d95b0c2b
SN
8577 }
8578 else
aa5e1f75
SN
8579 {
8580 ClearSelection();
8581 SetCurrentCell( row, col );
8582 }
4db6714b 8583
ca65c044 8584 return true;
2d66e025 8585 }
f85afd4e 8586
ca65c044 8587 return false;
2d66e025 8588}
f85afd4e 8589
5c8fc7c1 8590bool wxGrid::MoveCursorDownBlock( bool expandSelection )
f85afd4e 8591{
2d66e025 8592 if ( m_table &&
2f024384
DS
8593 m_currentCellCoords != wxGridNoCellCoords &&
8594 m_currentCellCoords.GetRow() < m_numRows - 1 )
f85afd4e 8595 {
2d66e025
MB
8596 int row = m_currentCellCoords.GetRow();
8597 int col = m_currentCellCoords.GetCol();
8598
8599 if ( m_table->IsEmptyCell(row, col) )
8600 {
8601 // starting in an empty cell: find the next block of
8602 // non-empty cells
8603 //
2f024384 8604 while ( row < m_numRows - 1 )
2d66e025 8605 {
2f024384 8606 row++;
4db6714b
KH
8607 if ( !(m_table->IsEmptyCell(row, col)) )
8608 break;
2d66e025
MB
8609 }
8610 }
2f024384 8611 else if ( m_table->IsEmptyCell(row + 1, col) )
2d66e025
MB
8612 {
8613 // starting at the bottom of a block: find the next block
8614 //
8615 row++;
2f024384 8616 while ( row < m_numRows - 1 )
2d66e025 8617 {
2f024384 8618 row++;
4db6714b
KH
8619 if ( !(m_table->IsEmptyCell(row, col)) )
8620 break;
2d66e025
MB
8621 }
8622 }
8623 else
8624 {
8625 // starting within a block: find the bottom of the block
8626 //
2f024384 8627 while ( row < m_numRows - 1 )
2d66e025 8628 {
2f024384 8629 row++;
2d66e025
MB
8630 if ( m_table->IsEmptyCell(row, col) )
8631 {
2f024384 8632 row--;
2d66e025
MB
8633 break;
8634 }
8635 }
8636 }
8637
8638 MakeCellVisible( row, col );
5c8fc7c1 8639 if ( expandSelection )
d95b0c2b
SN
8640 {
8641 m_selectingKeyboard = wxGridCellCoords( row, col );
c9097836 8642 HighlightBlock( m_currentCellCoords, m_selectingKeyboard );
d95b0c2b
SN
8643 }
8644 else
aa5e1f75
SN
8645 {
8646 ClearSelection();
8647 SetCurrentCell( row, col );
8648 }
2d66e025 8649
ca65c044 8650 return true;
f85afd4e 8651 }
f85afd4e 8652
ca65c044 8653 return false;
2d66e025 8654}
f85afd4e 8655
5c8fc7c1 8656bool wxGrid::MoveCursorLeftBlock( bool expandSelection )
f85afd4e 8657{
2d66e025 8658 if ( m_table &&
2f024384 8659 m_currentCellCoords != wxGridNoCellCoords &&
2d66e025 8660 m_currentCellCoords.GetCol() > 0 )
f85afd4e 8661 {
2d66e025
MB
8662 int row = m_currentCellCoords.GetRow();
8663 int col = m_currentCellCoords.GetCol();
8664
8665 if ( m_table->IsEmptyCell(row, col) )
8666 {
8667 // starting in an empty cell: find the next block of
8668 // non-empty cells
8669 //
8670 while ( col > 0 )
8671 {
2f024384 8672 col--;
4db6714b
KH
8673 if ( !(m_table->IsEmptyCell(row, col)) )
8674 break;
2d66e025
MB
8675 }
8676 }
2f024384 8677 else if ( m_table->IsEmptyCell(row, col - 1) )
2d66e025
MB
8678 {
8679 // starting at the left of a block: find the next block
8680 //
8681 col--;
8682 while ( col > 0 )
8683 {
2f024384 8684 col--;
4db6714b
KH
8685 if ( !(m_table->IsEmptyCell(row, col)) )
8686 break;
2d66e025
MB
8687 }
8688 }
8689 else
8690 {
8691 // starting within a block: find the left of the block
8692 //
8693 while ( col > 0 )
8694 {
2f024384 8695 col--;
2d66e025
MB
8696 if ( m_table->IsEmptyCell(row, col) )
8697 {
2f024384 8698 col++;
2d66e025
MB
8699 break;
8700 }
8701 }
8702 }
f85afd4e 8703
2d66e025 8704 MakeCellVisible( row, col );
5c8fc7c1 8705 if ( expandSelection )
d95b0c2b
SN
8706 {
8707 m_selectingKeyboard = wxGridCellCoords( row, col );
c9097836 8708 HighlightBlock( m_currentCellCoords, m_selectingKeyboard );
d95b0c2b
SN
8709 }
8710 else
aa5e1f75
SN
8711 {
8712 ClearSelection();
8713 SetCurrentCell( row, col );
8714 }
8f177c8e 8715
ca65c044 8716 return true;
f85afd4e 8717 }
2d66e025 8718
ca65c044 8719 return false;
f85afd4e
MB
8720}
8721
5c8fc7c1 8722bool wxGrid::MoveCursorRightBlock( bool expandSelection )
f85afd4e 8723{
2d66e025 8724 if ( m_table &&
2f024384
DS
8725 m_currentCellCoords != wxGridNoCellCoords &&
8726 m_currentCellCoords.GetCol() < m_numCols - 1 )
f85afd4e 8727 {
2d66e025
MB
8728 int row = m_currentCellCoords.GetRow();
8729 int col = m_currentCellCoords.GetCol();
8f177c8e 8730
2d66e025
MB
8731 if ( m_table->IsEmptyCell(row, col) )
8732 {
8733 // starting in an empty cell: find the next block of
8734 // non-empty cells
8735 //
2f024384 8736 while ( col < m_numCols - 1 )
2d66e025 8737 {
2f024384 8738 col++;
4db6714b
KH
8739 if ( !(m_table->IsEmptyCell(row, col)) )
8740 break;
2d66e025
MB
8741 }
8742 }
2f024384 8743 else if ( m_table->IsEmptyCell(row, col + 1) )
2d66e025
MB
8744 {
8745 // starting at the right of a block: find the next block
8746 //
8747 col++;
2f024384 8748 while ( col < m_numCols - 1 )
2d66e025 8749 {
2f024384 8750 col++;
4db6714b
KH
8751 if ( !(m_table->IsEmptyCell(row, col)) )
8752 break;
2d66e025
MB
8753 }
8754 }
8755 else
8756 {
8757 // starting within a block: find the right of the block
8758 //
2f024384 8759 while ( col < m_numCols - 1 )
2d66e025 8760 {
2f024384 8761 col++;
2d66e025
MB
8762 if ( m_table->IsEmptyCell(row, col) )
8763 {
2f024384 8764 col--;
2d66e025
MB
8765 break;
8766 }
8767 }
8768 }
8f177c8e 8769
2d66e025 8770 MakeCellVisible( row, col );
5c8fc7c1 8771 if ( expandSelection )
d95b0c2b
SN
8772 {
8773 m_selectingKeyboard = wxGridCellCoords( row, col );
c9097836 8774 HighlightBlock( m_currentCellCoords, m_selectingKeyboard );
d95b0c2b
SN
8775 }
8776 else
aa5e1f75
SN
8777 {
8778 ClearSelection();
8779 SetCurrentCell( row, col );
8780 }
f85afd4e 8781
ca65c044 8782 return true;
f85afd4e 8783 }
2d66e025 8784
ca65c044 8785 return false;
f85afd4e
MB
8786}
8787
f85afd4e 8788//
2d66e025 8789// ------ Label values and formatting
f85afd4e
MB
8790//
8791
8792void wxGrid::GetRowLabelAlignment( int *horiz, int *vert )
8793{
2f024384
DS
8794 if ( horiz )
8795 *horiz = m_rowLabelHorizAlign;
8796 if ( vert )
8797 *vert = m_rowLabelVertAlign;
f85afd4e
MB
8798}
8799
8800void wxGrid::GetColLabelAlignment( int *horiz, int *vert )
8801{
2f024384
DS
8802 if ( horiz )
8803 *horiz = m_colLabelHorizAlign;
8804 if ( vert )
8805 *vert = m_colLabelVertAlign;
f85afd4e
MB
8806}
8807
d43851f7
JS
8808int wxGrid::GetColLabelTextOrientation()
8809{
8810 return m_colLabelTextOrientation;
8811}
8812
f85afd4e
MB
8813wxString wxGrid::GetRowLabelValue( int row )
8814{
8815 if ( m_table )
8816 {
8817 return m_table->GetRowLabelValue( row );
8818 }
8819 else
8820 {
8821 wxString s;
8822 s << row;
8823 return s;
8824 }
8825}
8826
8827wxString wxGrid::GetColLabelValue( int col )
8828{
8829 if ( m_table )
8830 {
8831 return m_table->GetColLabelValue( col );
8832 }
8833 else
8834 {
8835 wxString s;
8836 s << col;
8837 return s;
8838 }
8839}
8840
8841void wxGrid::SetRowLabelSize( int width )
8842{
2e8cd977
MB
8843 width = wxMax( width, 0 );
8844 if ( width != m_rowLabelWidth )
8845 {
2e8cd977
MB
8846 if ( width == 0 )
8847 {
ca65c044
WS
8848 m_rowLabelWin->Show( false );
8849 m_cornerLabelWin->Show( false );
2e8cd977 8850 }
7807d81c 8851 else if ( m_rowLabelWidth == 0 )
2e8cd977 8852 {
ca65c044 8853 m_rowLabelWin->Show( true );
2f024384
DS
8854 if ( m_colLabelHeight > 0 )
8855 m_cornerLabelWin->Show( true );
2e8cd977 8856 }
b99be8fb 8857
2e8cd977 8858 m_rowLabelWidth = width;
7807d81c 8859 CalcWindowSizes();
ca65c044 8860 wxScrolledWindow::Refresh( true );
2e8cd977 8861 }
f85afd4e
MB
8862}
8863
8864void wxGrid::SetColLabelSize( int height )
8865{
7807d81c 8866 height = wxMax( height, 0 );
2e8cd977
MB
8867 if ( height != m_colLabelHeight )
8868 {
2e8cd977
MB
8869 if ( height == 0 )
8870 {
ca65c044
WS
8871 m_colLabelWin->Show( false );
8872 m_cornerLabelWin->Show( false );
2e8cd977 8873 }
7807d81c 8874 else if ( m_colLabelHeight == 0 )
2e8cd977 8875 {
ca65c044 8876 m_colLabelWin->Show( true );
a9339fe2
DS
8877 if ( m_rowLabelWidth > 0 )
8878 m_cornerLabelWin->Show( true );
2e8cd977 8879 }
7807d81c 8880
2e8cd977 8881 m_colLabelHeight = height;
7807d81c 8882 CalcWindowSizes();
ca65c044 8883 wxScrolledWindow::Refresh( true );
2e8cd977 8884 }
f85afd4e
MB
8885}
8886
8887void wxGrid::SetLabelBackgroundColour( const wxColour& colour )
8888{
2d66e025
MB
8889 if ( m_labelBackgroundColour != colour )
8890 {
8891 m_labelBackgroundColour = colour;
8892 m_rowLabelWin->SetBackgroundColour( colour );
8893 m_colLabelWin->SetBackgroundColour( colour );
8894 m_cornerLabelWin->SetBackgroundColour( colour );
8895
8896 if ( !GetBatchCount() )
8897 {
8898 m_rowLabelWin->Refresh();
8899 m_colLabelWin->Refresh();
8900 m_cornerLabelWin->Refresh();
8901 }
8902 }
f85afd4e
MB
8903}
8904
8905void wxGrid::SetLabelTextColour( const wxColour& colour )
8906{
2d66e025
MB
8907 if ( m_labelTextColour != colour )
8908 {
8909 m_labelTextColour = colour;
8910 if ( !GetBatchCount() )
8911 {
8912 m_rowLabelWin->Refresh();
8913 m_colLabelWin->Refresh();
8914 }
8915 }
f85afd4e
MB
8916}
8917
8918void wxGrid::SetLabelFont( const wxFont& font )
8919{
8920 m_labelFont = font;
2d66e025
MB
8921 if ( !GetBatchCount() )
8922 {
8923 m_rowLabelWin->Refresh();
8924 m_colLabelWin->Refresh();
8925 }
f85afd4e
MB
8926}
8927
8928void wxGrid::SetRowLabelAlignment( int horiz, int vert )
8929{
4c7277db
MB
8930 // allow old (incorrect) defs to be used
8931 switch ( horiz )
8932 {
8933 case wxLEFT: horiz = wxALIGN_LEFT; break;
8934 case wxRIGHT: horiz = wxALIGN_RIGHT; break;
8935 case wxCENTRE: horiz = wxALIGN_CENTRE; break;
8936 }
84912ef8 8937
4c7277db
MB
8938 switch ( vert )
8939 {
8940 case wxTOP: vert = wxALIGN_TOP; break;
8941 case wxBOTTOM: vert = wxALIGN_BOTTOM; break;
8942 case wxCENTRE: vert = wxALIGN_CENTRE; break;
8943 }
84912ef8 8944
4c7277db 8945 if ( horiz == wxALIGN_LEFT || horiz == wxALIGN_CENTRE || horiz == wxALIGN_RIGHT )
f85afd4e
MB
8946 {
8947 m_rowLabelHorizAlign = horiz;
8948 }
8f177c8e 8949
4c7277db 8950 if ( vert == wxALIGN_TOP || vert == wxALIGN_CENTRE || vert == wxALIGN_BOTTOM )
f85afd4e
MB
8951 {
8952 m_rowLabelVertAlign = vert;
8953 }
8954
2d66e025
MB
8955 if ( !GetBatchCount() )
8956 {
8957 m_rowLabelWin->Refresh();
60ff3b99 8958 }
f85afd4e
MB
8959}
8960
8961void wxGrid::SetColLabelAlignment( int horiz, int vert )
8962{
4c7277db
MB
8963 // allow old (incorrect) defs to be used
8964 switch ( horiz )
8965 {
8966 case wxLEFT: horiz = wxALIGN_LEFT; break;
8967 case wxRIGHT: horiz = wxALIGN_RIGHT; break;
8968 case wxCENTRE: horiz = wxALIGN_CENTRE; break;
8969 }
84912ef8 8970
4c7277db
MB
8971 switch ( vert )
8972 {
8973 case wxTOP: vert = wxALIGN_TOP; break;
8974 case wxBOTTOM: vert = wxALIGN_BOTTOM; break;
8975 case wxCENTRE: vert = wxALIGN_CENTRE; break;
8976 }
84912ef8 8977
4c7277db 8978 if ( horiz == wxALIGN_LEFT || horiz == wxALIGN_CENTRE || horiz == wxALIGN_RIGHT )
f85afd4e
MB
8979 {
8980 m_colLabelHorizAlign = horiz;
8981 }
8f177c8e 8982
4c7277db 8983 if ( vert == wxALIGN_TOP || vert == wxALIGN_CENTRE || vert == wxALIGN_BOTTOM )
f85afd4e
MB
8984 {
8985 m_colLabelVertAlign = vert;
8986 }
8987
2d66e025
MB
8988 if ( !GetBatchCount() )
8989 {
2d66e025 8990 m_colLabelWin->Refresh();
60ff3b99 8991 }
f85afd4e
MB
8992}
8993
ca65c044
WS
8994// Note: under MSW, the default column label font must be changed because it
8995// does not support vertical printing
d43851f7
JS
8996//
8997// Example: wxFont font(9, wxSWISS, wxNORMAL, wxBOLD);
ca65c044
WS
8998// pGrid->SetLabelFont(font);
8999// pGrid->SetColLabelTextOrientation(wxVERTICAL);
d43851f7
JS
9000//
9001void wxGrid::SetColLabelTextOrientation( int textOrientation )
9002{
4db6714b 9003 if ( textOrientation == wxHORIZONTAL || textOrientation == wxVERTICAL )
d43851f7 9004 m_colLabelTextOrientation = textOrientation;
d43851f7
JS
9005
9006 if ( !GetBatchCount() )
d43851f7 9007 m_colLabelWin->Refresh();
d43851f7
JS
9008}
9009
f85afd4e
MB
9010void wxGrid::SetRowLabelValue( int row, const wxString& s )
9011{
9012 if ( m_table )
9013 {
9014 m_table->SetRowLabelValue( row, s );
2d66e025
MB
9015 if ( !GetBatchCount() )
9016 {
ccdee36f 9017 wxRect rect = CellToRect( row, 0 );
70c7a608
SN
9018 if ( rect.height > 0 )
9019 {
cb309039 9020 CalcScrolledPosition(0, rect.y, &rect.x, &rect.y);
b1944ebc 9021 rect.x = 0;
70c7a608 9022 rect.width = m_rowLabelWidth;
ca65c044 9023 m_rowLabelWin->Refresh( true, &rect );
70c7a608 9024 }
2d66e025 9025 }
f85afd4e
MB
9026 }
9027}
9028
9029void wxGrid::SetColLabelValue( int col, const wxString& s )
9030{
9031 if ( m_table )
9032 {
9033 m_table->SetColLabelValue( col, s );
2d66e025
MB
9034 if ( !GetBatchCount() )
9035 {
70c7a608
SN
9036 wxRect rect = CellToRect( 0, col );
9037 if ( rect.width > 0 )
9038 {
cb309039 9039 CalcScrolledPosition(rect.x, 0, &rect.x, &rect.y);
b1944ebc 9040 rect.y = 0;
70c7a608 9041 rect.height = m_colLabelHeight;
ca65c044 9042 m_colLabelWin->Refresh( true, &rect );
70c7a608 9043 }
2d66e025 9044 }
f85afd4e
MB
9045 }
9046}
9047
9048void wxGrid::SetGridLineColour( const wxColour& colour )
9049{
2d66e025
MB
9050 if ( m_gridLineColour != colour )
9051 {
9052 m_gridLineColour = colour;
60ff3b99 9053
2d66e025
MB
9054 wxClientDC dc( m_gridWin );
9055 PrepareDC( dc );
c6a51dcd 9056 DrawAllGridLines( dc, wxRegion() );
2d66e025 9057 }
f85afd4e
MB
9058}
9059
f6bcfd97
BP
9060void wxGrid::SetCellHighlightColour( const wxColour& colour )
9061{
9062 if ( m_cellHighlightColour != colour )
9063 {
9064 m_cellHighlightColour = colour;
9065
9066 wxClientDC dc( m_gridWin );
9067 PrepareDC( dc );
9068 wxGridCellAttr* attr = GetCellAttr(m_currentCellCoords);
9069 DrawCellHighlight(dc, attr);
9070 attr->DecRef();
9071 }
9072}
9073
d2520c85
RD
9074void wxGrid::SetCellHighlightPenWidth(int width)
9075{
4db6714b
KH
9076 if (m_cellHighlightPenWidth != width)
9077 {
d2520c85
RD
9078 m_cellHighlightPenWidth = width;
9079
9080 // Just redrawing the cell highlight is not enough since that won't
9081 // make any visible change if the the thickness is getting smaller.
9082 int row = m_currentCellCoords.GetRow();
9083 int col = m_currentCellCoords.GetCol();
9084 if ( GetColWidth(col) <= 0 || GetRowHeight(row) <= 0 )
9085 return;
2f024384 9086
d2520c85 9087 wxRect rect = CellToRect(row, col);
ca65c044 9088 m_gridWin->Refresh(true, &rect);
d2520c85
RD
9089 }
9090}
9091
9092void wxGrid::SetCellHighlightROPenWidth(int width)
9093{
4db6714b
KH
9094 if (m_cellHighlightROPenWidth != width)
9095 {
d2520c85
RD
9096 m_cellHighlightROPenWidth = width;
9097
9098 // Just redrawing the cell highlight is not enough since that won't
9099 // make any visible change if the the thickness is getting smaller.
9100 int row = m_currentCellCoords.GetRow();
9101 int col = m_currentCellCoords.GetCol();
9102 if ( GetColWidth(col) <= 0 || GetRowHeight(row) <= 0 )
9103 return;
ccdee36f 9104
d2520c85 9105 wxRect rect = CellToRect(row, col);
ca65c044 9106 m_gridWin->Refresh(true, &rect);
d2520c85
RD
9107 }
9108}
9109
f85afd4e
MB
9110void wxGrid::EnableGridLines( bool enable )
9111{
9112 if ( enable != m_gridLinesEnabled )
9113 {
9114 m_gridLinesEnabled = enable;
2d66e025
MB
9115
9116 if ( !GetBatchCount() )
9117 {
9118 if ( enable )
9119 {
9120 wxClientDC dc( m_gridWin );
9121 PrepareDC( dc );
c6a51dcd 9122 DrawAllGridLines( dc, wxRegion() );
2d66e025
MB
9123 }
9124 else
9125 {
9126 m_gridWin->Refresh();
9127 }
9128 }
f85afd4e
MB
9129 }
9130}
9131
f85afd4e
MB
9132int wxGrid::GetDefaultRowSize()
9133{
9134 return m_defaultRowHeight;
9135}
9136
9137int wxGrid::GetRowSize( int row )
9138{
b99be8fb
VZ
9139 wxCHECK_MSG( row >= 0 && row < m_numRows, 0, _T("invalid row index") );
9140
7c1cb261 9141 return GetRowHeight(row);
f85afd4e
MB
9142}
9143
9144int wxGrid::GetDefaultColSize()
9145{
9146 return m_defaultColWidth;
9147}
9148
9149int wxGrid::GetColSize( int col )
9150{
b99be8fb
VZ
9151 wxCHECK_MSG( col >= 0 && col < m_numCols, 0, _T("invalid column index") );
9152
7c1cb261 9153 return GetColWidth(col);
f85afd4e
MB
9154}
9155
2e9a6788
VZ
9156// ============================================================================
9157// access to the grid attributes: each of them has a default value in the grid
9158// itself and may be overidden on a per-cell basis
9159// ============================================================================
9160
9161// ----------------------------------------------------------------------------
9162// setting default attributes
9163// ----------------------------------------------------------------------------
9164
9165void wxGrid::SetDefaultCellBackgroundColour( const wxColour& col )
9166{
2796cce3 9167 m_defaultCellAttr->SetBackgroundColour(col);
c916e13b
RR
9168#ifdef __WXGTK__
9169 m_gridWin->SetBackgroundColour(col);
9170#endif
2e9a6788
VZ
9171}
9172
9173void wxGrid::SetDefaultCellTextColour( const wxColour& col )
9174{
2796cce3 9175 m_defaultCellAttr->SetTextColour(col);
2e9a6788
VZ
9176}
9177
9178void wxGrid::SetDefaultCellAlignment( int horiz, int vert )
9179{
2796cce3 9180 m_defaultCellAttr->SetAlignment(horiz, vert);
2e9a6788
VZ
9181}
9182
27f35b66
SN
9183void wxGrid::SetDefaultCellOverflow( bool allow )
9184{
9185 m_defaultCellAttr->SetOverflow(allow);
9186}
9187
2e9a6788
VZ
9188void wxGrid::SetDefaultCellFont( const wxFont& font )
9189{
2796cce3
RD
9190 m_defaultCellAttr->SetFont(font);
9191}
9192
ca63e8e9
RD
9193// For editors and renderers the type registry takes precedence over the
9194// default attr, so we need to register the new editor/renderer for the string
9195// data type in order to make setting a default editor/renderer appear to
9196// work correctly.
9197
0ba143c9
RD
9198void wxGrid::SetDefaultRenderer(wxGridCellRenderer *renderer)
9199{
ca63e8e9
RD
9200 RegisterDataType(wxGRID_VALUE_STRING,
9201 renderer,
9202 GetDefaultEditorForType(wxGRID_VALUE_STRING));
0ba143c9 9203}
2e9a6788 9204
0ba143c9
RD
9205void wxGrid::SetDefaultEditor(wxGridCellEditor *editor)
9206{
ca63e8e9
RD
9207 RegisterDataType(wxGRID_VALUE_STRING,
9208 GetDefaultRendererForType(wxGRID_VALUE_STRING),
42841dfc 9209 editor);
0ba143c9 9210}
9b4aede2 9211
2e9a6788
VZ
9212// ----------------------------------------------------------------------------
9213// access to the default attrbiutes
9214// ----------------------------------------------------------------------------
9215
f85afd4e
MB
9216wxColour wxGrid::GetDefaultCellBackgroundColour()
9217{
2796cce3 9218 return m_defaultCellAttr->GetBackgroundColour();
f85afd4e
MB
9219}
9220
2e9a6788
VZ
9221wxColour wxGrid::GetDefaultCellTextColour()
9222{
2796cce3 9223 return m_defaultCellAttr->GetTextColour();
2e9a6788
VZ
9224}
9225
9226wxFont wxGrid::GetDefaultCellFont()
9227{
2796cce3 9228 return m_defaultCellAttr->GetFont();
2e9a6788
VZ
9229}
9230
9231void wxGrid::GetDefaultCellAlignment( int *horiz, int *vert )
9232{
2796cce3 9233 m_defaultCellAttr->GetAlignment(horiz, vert);
2e9a6788
VZ
9234}
9235
27f35b66
SN
9236bool wxGrid::GetDefaultCellOverflow()
9237{
9238 return m_defaultCellAttr->GetOverflow();
9239}
9240
0ba143c9
RD
9241wxGridCellRenderer *wxGrid::GetDefaultRenderer() const
9242{
0b190b0f 9243 return m_defaultCellAttr->GetRenderer(NULL, 0, 0);
0ba143c9 9244}
ab79958a 9245
0ba143c9
RD
9246wxGridCellEditor *wxGrid::GetDefaultEditor() const
9247{
4db6714b 9248 return m_defaultCellAttr->GetEditor(NULL, 0, 0);
0ba143c9 9249}
9b4aede2 9250
2e9a6788
VZ
9251// ----------------------------------------------------------------------------
9252// access to cell attributes
9253// ----------------------------------------------------------------------------
9254
b99be8fb 9255wxColour wxGrid::GetCellBackgroundColour(int row, int col)
f85afd4e 9256{
0a976765 9257 wxGridCellAttr *attr = GetCellAttr(row, col);
2796cce3 9258 wxColour colour = attr->GetBackgroundColour();
39bcce60 9259 attr->DecRef();
2f024384 9260
b99be8fb 9261 return colour;
f85afd4e
MB
9262}
9263
b99be8fb 9264wxColour wxGrid::GetCellTextColour( int row, int col )
f85afd4e 9265{
0a976765 9266 wxGridCellAttr *attr = GetCellAttr(row, col);
2796cce3 9267 wxColour colour = attr->GetTextColour();
39bcce60 9268 attr->DecRef();
2f024384 9269
b99be8fb 9270 return colour;
f85afd4e
MB
9271}
9272
b99be8fb 9273wxFont wxGrid::GetCellFont( int row, int col )
f85afd4e 9274{
0a976765 9275 wxGridCellAttr *attr = GetCellAttr(row, col);
2796cce3 9276 wxFont font = attr->GetFont();
39bcce60 9277 attr->DecRef();
2f024384 9278
b99be8fb 9279 return font;
f85afd4e
MB
9280}
9281
b99be8fb 9282void wxGrid::GetCellAlignment( int row, int col, int *horiz, int *vert )
f85afd4e 9283{
0a976765 9284 wxGridCellAttr *attr = GetCellAttr(row, col);
2796cce3 9285 attr->GetAlignment(horiz, vert);
39bcce60 9286 attr->DecRef();
2e9a6788
VZ
9287}
9288
27f35b66
SN
9289bool wxGrid::GetCellOverflow( int row, int col )
9290{
9291 wxGridCellAttr *attr = GetCellAttr(row, col);
9292 bool allow = attr->GetOverflow();
9293 attr->DecRef();
4db6714b 9294
27f35b66
SN
9295 return allow;
9296}
9297
9298void wxGrid::GetCellSize( int row, int col, int *num_rows, int *num_cols )
9299{
9300 wxGridCellAttr *attr = GetCellAttr(row, col);
9301 attr->GetSize( num_rows, num_cols );
9302 attr->DecRef();
9303}
9304
2796cce3
RD
9305wxGridCellRenderer* wxGrid::GetCellRenderer(int row, int col)
9306{
9307 wxGridCellAttr* attr = GetCellAttr(row, col);
28a77bc4 9308 wxGridCellRenderer* renderer = attr->GetRenderer(this, row, col);
2796cce3 9309 attr->DecRef();
0b190b0f 9310
2796cce3
RD
9311 return renderer;
9312}
9313
9b4aede2
RD
9314wxGridCellEditor* wxGrid::GetCellEditor(int row, int col)
9315{
9316 wxGridCellAttr* attr = GetCellAttr(row, col);
28a77bc4 9317 wxGridCellEditor* editor = attr->GetEditor(this, row, col);
9b4aede2 9318 attr->DecRef();
0b190b0f 9319
9b4aede2
RD
9320 return editor;
9321}
9322
283b7808
VZ
9323bool wxGrid::IsReadOnly(int row, int col) const
9324{
9325 wxGridCellAttr* attr = GetCellAttr(row, col);
9326 bool isReadOnly = attr->IsReadOnly();
9327 attr->DecRef();
4db6714b 9328
283b7808
VZ
9329 return isReadOnly;
9330}
9331
2e9a6788 9332// ----------------------------------------------------------------------------
758cbedf 9333// attribute support: cache, automatic provider creation, ...
2e9a6788
VZ
9334// ----------------------------------------------------------------------------
9335
9336bool wxGrid::CanHaveAttributes()
9337{
9338 if ( !m_table )
9339 {
ca65c044 9340 return false;
2e9a6788
VZ
9341 }
9342
f2d76237 9343 return m_table->CanHaveAttributes();
2e9a6788
VZ
9344}
9345
0a976765
VZ
9346void wxGrid::ClearAttrCache()
9347{
9348 if ( m_attrCache.row != -1 )
9349 {
39bcce60 9350 wxSafeDecRef(m_attrCache.attr);
19d7140e 9351 m_attrCache.attr = NULL;
0a976765
VZ
9352 m_attrCache.row = -1;
9353 }
9354}
9355
9356void wxGrid::CacheAttr(int row, int col, wxGridCellAttr *attr) const
9357{
2b5f62a0
VZ
9358 if ( attr != NULL )
9359 {
9360 wxGrid *self = (wxGrid *)this; // const_cast
0a976765 9361
2b5f62a0
VZ
9362 self->ClearAttrCache();
9363 self->m_attrCache.row = row;
9364 self->m_attrCache.col = col;
9365 self->m_attrCache.attr = attr;
9366 wxSafeIncRef(attr);
9367 }
0a976765
VZ
9368}
9369
9370bool wxGrid::LookupAttr(int row, int col, wxGridCellAttr **attr) const
9371{
9372 if ( row == m_attrCache.row && col == m_attrCache.col )
9373 {
9374 *attr = m_attrCache.attr;
39bcce60 9375 wxSafeIncRef(m_attrCache.attr);
0a976765
VZ
9376
9377#ifdef DEBUG_ATTR_CACHE
9378 gs_nAttrCacheHits++;
9379#endif
9380
ca65c044 9381 return true;
0a976765
VZ
9382 }
9383 else
9384 {
9385#ifdef DEBUG_ATTR_CACHE
9386 gs_nAttrCacheMisses++;
9387#endif
4db6714b 9388
ca65c044 9389 return false;
0a976765
VZ
9390 }
9391}
9392
2e9a6788
VZ
9393wxGridCellAttr *wxGrid::GetCellAttr(int row, int col) const
9394{
a373d23b
SN
9395 wxGridCellAttr *attr = NULL;
9396 // Additional test to avoid looking at the cache e.g. for
9397 // wxNoCellCoords, as this will confuse memory management.
9398 if ( row >= 0 )
9399 {
3ed884a0
SN
9400 if ( !LookupAttr(row, col, &attr) )
9401 {
9402 attr = m_table ? m_table->GetAttr(row, col , wxGridCellAttr::Any)
9403 : (wxGridCellAttr *)NULL;
9404 CacheAttr(row, col, attr);
9405 }
0a976765 9406 }
4db6714b 9407
508011ce
VZ
9408 if (attr)
9409 {
2796cce3 9410 attr->SetDefAttr(m_defaultCellAttr);
508011ce
VZ
9411 }
9412 else
9413 {
2796cce3
RD
9414 attr = m_defaultCellAttr;
9415 attr->IncRef();
9416 }
2e9a6788 9417
0a976765
VZ
9418 return attr;
9419}
9420
9421wxGridCellAttr *wxGrid::GetOrCreateCellAttr(int row, int col) const
9422{
19d7140e 9423 wxGridCellAttr *attr = (wxGridCellAttr *)NULL;
71e60f70 9424 bool canHave = ((wxGrid*)this)->CanHaveAttributes();
0a976765 9425
71e60f70
RD
9426 wxCHECK_MSG( canHave, attr, _T("Cell attributes not allowed"));
9427 wxCHECK_MSG( m_table, attr, _T("must have a table") );
1df4050d
VZ
9428
9429 attr = m_table->GetAttr(row, col, wxGridCellAttr::Cell);
9430 if ( !attr )
9431 {
9432 attr = new wxGridCellAttr(m_defaultCellAttr);
9433
9434 // artificially inc the ref count to match DecRef() in caller
9435 attr->IncRef();
9436 m_table->SetAttr(attr, row, col);
9437 }
0a976765 9438
2e9a6788
VZ
9439 return attr;
9440}
9441
0b190b0f
VZ
9442// ----------------------------------------------------------------------------
9443// setting column attributes (wrappers around SetColAttr)
9444// ----------------------------------------------------------------------------
9445
9446void wxGrid::SetColFormatBool(int col)
9447{
9448 SetColFormatCustom(col, wxGRID_VALUE_BOOL);
9449}
9450
9451void wxGrid::SetColFormatNumber(int col)
9452{
9453 SetColFormatCustom(col, wxGRID_VALUE_NUMBER);
9454}
9455
9456void wxGrid::SetColFormatFloat(int col, int width, int precision)
9457{
9458 wxString typeName = wxGRID_VALUE_FLOAT;
9459 if ( (width != -1) || (precision != -1) )
9460 {
9461 typeName << _T(':') << width << _T(',') << precision;
9462 }
9463
9464 SetColFormatCustom(col, typeName);
9465}
9466
9467void wxGrid::SetColFormatCustom(int col, const wxString& typeName)
9468{
999836aa 9469 wxGridCellAttr *attr = m_table->GetAttr(-1, col, wxGridCellAttr::Col );
4db6714b 9470 if (!attr)
19d7140e 9471 attr = new wxGridCellAttr;
0b190b0f
VZ
9472 wxGridCellRenderer *renderer = GetDefaultRendererForType(typeName);
9473 attr->SetRenderer(renderer);
9474
9475 SetColAttr(col, attr);
19d7140e 9476
0b190b0f
VZ
9477}
9478
758cbedf
VZ
9479// ----------------------------------------------------------------------------
9480// setting cell attributes: this is forwarded to the table
9481// ----------------------------------------------------------------------------
9482
27f35b66
SN
9483void wxGrid::SetAttr(int row, int col, wxGridCellAttr *attr)
9484{
9485 if ( CanHaveAttributes() )
9486 {
9487 m_table->SetAttr(attr, row, col);
9488 ClearAttrCache();
9489 }
9490 else
9491 {
9492 wxSafeDecRef(attr);
9493 }
9494}
9495
758cbedf
VZ
9496void wxGrid::SetRowAttr(int row, wxGridCellAttr *attr)
9497{
9498 if ( CanHaveAttributes() )
9499 {
9500 m_table->SetRowAttr(attr, row);
19d7140e 9501 ClearAttrCache();
758cbedf
VZ
9502 }
9503 else
9504 {
39bcce60 9505 wxSafeDecRef(attr);
758cbedf
VZ
9506 }
9507}
9508
9509void wxGrid::SetColAttr(int col, wxGridCellAttr *attr)
9510{
9511 if ( CanHaveAttributes() )
9512 {
9513 m_table->SetColAttr(attr, col);
19d7140e 9514 ClearAttrCache();
758cbedf
VZ
9515 }
9516 else
9517 {
39bcce60 9518 wxSafeDecRef(attr);
758cbedf
VZ
9519 }
9520}
9521
2e9a6788
VZ
9522void wxGrid::SetCellBackgroundColour( int row, int col, const wxColour& colour )
9523{
9524 if ( CanHaveAttributes() )
9525 {
0a976765 9526 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
2e9a6788
VZ
9527 attr->SetBackgroundColour(colour);
9528 attr->DecRef();
9529 }
9530}
9531
9532void wxGrid::SetCellTextColour( int row, int col, const wxColour& colour )
9533{
9534 if ( CanHaveAttributes() )
9535 {
0a976765 9536 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
2e9a6788
VZ
9537 attr->SetTextColour(colour);
9538 attr->DecRef();
9539 }
9540}
9541
9542void wxGrid::SetCellFont( int row, int col, const wxFont& font )
9543{
9544 if ( CanHaveAttributes() )
9545 {
0a976765 9546 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
2e9a6788
VZ
9547 attr->SetFont(font);
9548 attr->DecRef();
9549 }
9550}
9551
9552void wxGrid::SetCellAlignment( int row, int col, int horiz, int vert )
9553{
9554 if ( CanHaveAttributes() )
9555 {
0a976765 9556 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
2e9a6788
VZ
9557 attr->SetAlignment(horiz, vert);
9558 attr->DecRef();
ab79958a
VZ
9559 }
9560}
9561
27f35b66
SN
9562void wxGrid::SetCellOverflow( int row, int col, bool allow )
9563{
9564 if ( CanHaveAttributes() )
9565 {
9566 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
9567 attr->SetOverflow(allow);
9568 attr->DecRef();
9569 }
9570}
9571
9572void wxGrid::SetCellSize( int row, int col, int num_rows, int num_cols )
9573{
9574 if ( CanHaveAttributes() )
9575 {
9576 int cell_rows, cell_cols;
9577
9578 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
9579 attr->GetSize(&cell_rows, &cell_cols);
9580 attr->SetSize(num_rows, num_cols);
9581 attr->DecRef();
9582
9583 // Cannot set the size of a cell to 0 or negative values
9584 // While it is perfectly legal to do that, this function cannot
9585 // handle all the possibilies, do it by hand by getting the CellAttr.
9586 // You can only set the size of a cell to 1,1 or greater with this fn
9587 wxASSERT_MSG( !((cell_rows < 1) || (cell_cols < 1)),
9588 wxT("wxGrid::SetCellSize setting cell size that is already part of another cell"));
9589 wxASSERT_MSG( !((num_rows < 1) || (num_cols < 1)),
9590 wxT("wxGrid::SetCellSize setting cell size to < 1"));
9591
9592 // if this was already a multicell then "turn off" the other cells first
9593 if ((cell_rows > 1) || (cell_rows > 1))
9594 {
9595 int i, j;
2f024384 9596 for (j=row; j < row + cell_rows; j++)
27f35b66 9597 {
2f024384 9598 for (i=col; i < col + cell_cols; i++)
27f35b66
SN
9599 {
9600 if ((i != col) || (j != row))
9601 {
9602 wxGridCellAttr *attr_stub = GetOrCreateCellAttr(j, i);
9603 attr_stub->SetSize( 1, 1 );
9604 attr_stub->DecRef();
9605 }
9606 }
9607 }
9608 }
9609
9610 // mark the cells that will be covered by this cell to
9611 // negative or zero values to point back at this cell
9612 if (((num_rows > 1) || (num_cols > 1)) && (num_rows >= 1) && (num_cols >= 1))
9613 {
9614 int i, j;
2f024384 9615 for (j=row; j < row + num_rows; j++)
27f35b66 9616 {
2f024384 9617 for (i=col; i < col + num_cols; i++)
27f35b66
SN
9618 {
9619 if ((i != col) || (j != row))
9620 {
9621 wxGridCellAttr *attr_stub = GetOrCreateCellAttr(j, i);
2f024384 9622 attr_stub->SetSize( row - j, col - i );
27f35b66
SN
9623 attr_stub->DecRef();
9624 }
9625 }
9626 }
9627 }
9628 }
9629}
9630
ab79958a
VZ
9631void wxGrid::SetCellRenderer(int row, int col, wxGridCellRenderer *renderer)
9632{
9633 if ( CanHaveAttributes() )
9634 {
0a976765 9635 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
ab79958a
VZ
9636 attr->SetRenderer(renderer);
9637 attr->DecRef();
9b4aede2
RD
9638 }
9639}
9640
9641void wxGrid::SetCellEditor(int row, int col, wxGridCellEditor* editor)
9642{
9643 if ( CanHaveAttributes() )
9644 {
9645 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
9646 attr->SetEditor(editor);
9647 attr->DecRef();
283b7808
VZ
9648 }
9649}
9650
9651void wxGrid::SetReadOnly(int row, int col, bool isReadOnly)
9652{
9653 if ( CanHaveAttributes() )
9654 {
9655 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
9656 attr->SetReadOnly(isReadOnly);
9657 attr->DecRef();
2e9a6788 9658 }
f85afd4e
MB
9659}
9660
f2d76237
RD
9661// ----------------------------------------------------------------------------
9662// Data type registration
9663// ----------------------------------------------------------------------------
9664
9665void wxGrid::RegisterDataType(const wxString& typeName,
9666 wxGridCellRenderer* renderer,
9667 wxGridCellEditor* editor)
9668{
9669 m_typeRegistry->RegisterDataType(typeName, renderer, editor);
9670}
9671
9672
a9339fe2 9673wxGridCellEditor * wxGrid::GetDefaultEditorForCell(int row, int col) const
f2d76237
RD
9674{
9675 wxString typeName = m_table->GetTypeName(row, col);
9676 return GetDefaultEditorForType(typeName);
9677}
9678
a9339fe2 9679wxGridCellRenderer * wxGrid::GetDefaultRendererForCell(int row, int col) const
f2d76237
RD
9680{
9681 wxString typeName = m_table->GetTypeName(row, col);
9682 return GetDefaultRendererForType(typeName);
9683}
9684
a9339fe2 9685wxGridCellEditor * wxGrid::GetDefaultEditorForType(const wxString& typeName) const
f2d76237 9686{
c4608a8a 9687 int index = m_typeRegistry->FindOrCloneDataType(typeName);
0b190b0f
VZ
9688 if ( index == wxNOT_FOUND )
9689 {
f87ab3cc 9690 wxString errStr;
4db6714b
KH
9691
9692 errStr.Printf(wxT("Unknown data type name [%s]"), typeName.c_str());
9693 wxFAIL_MSG(errStr.c_str());
0b190b0f 9694
f2d76237
RD
9695 return NULL;
9696 }
0b190b0f 9697
f2d76237
RD
9698 return m_typeRegistry->GetEditor(index);
9699}
9700
a9339fe2 9701wxGridCellRenderer * wxGrid::GetDefaultRendererForType(const wxString& typeName) const
f2d76237 9702{
c4608a8a 9703 int index = m_typeRegistry->FindOrCloneDataType(typeName);
0b190b0f
VZ
9704 if ( index == wxNOT_FOUND )
9705 {
2f024384 9706 wxString errStr;
4db6714b
KH
9707
9708 errStr.Printf(wxT("Unknown data type name [%s]"), typeName.c_str());
9709 wxFAIL_MSG(errStr.c_str());
0b190b0f 9710
c4608a8a 9711 return NULL;
e72b4213 9712 }
0b190b0f 9713
c4608a8a 9714 return m_typeRegistry->GetRenderer(index);
f2d76237
RD
9715}
9716
2e9a6788
VZ
9717// ----------------------------------------------------------------------------
9718// row/col size
9719// ----------------------------------------------------------------------------
9720
6e8524b1
MB
9721void wxGrid::EnableDragRowSize( bool enable )
9722{
9723 m_canDragRowSize = enable;
9724}
9725
6e8524b1
MB
9726void wxGrid::EnableDragColSize( bool enable )
9727{
9728 m_canDragColSize = enable;
9729}
9730
4cfa5de6
RD
9731void wxGrid::EnableDragGridSize( bool enable )
9732{
9733 m_canDragGridSize = enable;
9734}
9735
79dbea21
RD
9736void wxGrid::EnableDragCell( bool enable )
9737{
9738 m_canDragCell = enable;
9739}
6e8524b1 9740
f85afd4e
MB
9741void wxGrid::SetDefaultRowSize( int height, bool resizeExistingRows )
9742{
b8d24d4e 9743 m_defaultRowHeight = wxMax( height, m_minAcceptableRowHeight );
f85afd4e
MB
9744
9745 if ( resizeExistingRows )
9746 {
b1da8107
SN
9747 // since we are resizing all rows to the default row size,
9748 // we can simply clear the row heights and row bottoms
9749 // arrays (which also allows us to take advantage of
9750 // some speed optimisations)
9751 m_rowHeights.Empty();
9752 m_rowBottoms.Empty();
edb89f7e
VZ
9753 if ( !GetBatchCount() )
9754 CalcDimensions();
f85afd4e
MB
9755 }
9756}
9757
9758void wxGrid::SetRowSize( int row, int height )
9759{
b99be8fb 9760 wxCHECK_RET( row >= 0 && row < m_numRows, _T("invalid row index") );
60ff3b99 9761
b4bfd0fa 9762 // See comment in SetColSize
4db6714b
KH
9763 if ( height < GetRowMinimalAcceptableHeight())
9764 return;
b8d24d4e 9765
7c1cb261
VZ
9766 if ( m_rowHeights.IsEmpty() )
9767 {
9768 // need to really create the array
9769 InitRowHeights();
9770 }
60ff3b99 9771
b99be8fb
VZ
9772 int h = wxMax( 0, height );
9773 int diff = h - m_rowHeights[row];
60ff3b99 9774
b99be8fb 9775 m_rowHeights[row] = h;
7c1cb261 9776 int i;
b99be8fb 9777 for ( i = row; i < m_numRows; i++ )
f85afd4e 9778 {
b99be8fb 9779 m_rowBottoms[i] += diff;
f85afd4e 9780 }
2f024384 9781
faec5a43
SN
9782 if ( !GetBatchCount() )
9783 CalcDimensions();
f85afd4e
MB
9784}
9785
9786void wxGrid::SetDefaultColSize( int width, bool resizeExistingCols )
9787{
b8d24d4e 9788 m_defaultColWidth = wxMax( width, m_minAcceptableColWidth );
f85afd4e
MB
9789
9790 if ( resizeExistingCols )
9791 {
b1da8107
SN
9792 // since we are resizing all columns to the default column size,
9793 // we can simply clear the col widths and col rights
9794 // arrays (which also allows us to take advantage of
9795 // some speed optimisations)
9796 m_colWidths.Empty();
9797 m_colRights.Empty();
edb89f7e
VZ
9798 if ( !GetBatchCount() )
9799 CalcDimensions();
f85afd4e
MB
9800 }
9801}
9802
9803void wxGrid::SetColSize( int col, int width )
9804{
b99be8fb 9805 wxCHECK_RET( col >= 0 && col < m_numCols, _T("invalid column index") );
60ff3b99 9806
43947979 9807 // should we check that it's bigger than GetColMinimalWidth(col) here?
b4bfd0fa
RG
9808 // (VZ)
9809 // No, because it is reasonable to assume the library user know's
3e13956a 9810 // what he is doing. However whe should test against the weaker
ccdee36f 9811 // constraint of minimalAcceptableWidth, as this breaks rendering
3e13956a 9812 //
b4bfd0fa 9813 // This test then fixes sf.net bug #645734
3e13956a 9814
962a48f6 9815 if ( width < GetColMinimalAcceptableWidth() )
4db6714b 9816 return;
3e13956a 9817
7c1cb261
VZ
9818 if ( m_colWidths.IsEmpty() )
9819 {
9820 // need to really create the array
9821 InitColWidths();
9822 }
f85afd4e 9823
04418332 9824 // if < 0 calc new width from label
4db6714b 9825 if ( width < 0 )
73145b0e
JS
9826 {
9827 long w, h;
9828 wxArrayString lines;
9829 wxClientDC dc(m_colLabelWin);
9830 dc.SetFont(GetLabelFont());
9831 StringToLines(GetColLabelValue(col), lines);
9832 GetTextBoxSize(dc, lines, &w, &h);
9833 width = w + 6;
9834 }
962a48f6 9835
b99be8fb
VZ
9836 int w = wxMax( 0, width );
9837 int diff = w - m_colWidths[col];
9838 m_colWidths[col] = w;
60ff3b99 9839
7c1cb261 9840 int i;
b99be8fb 9841 for ( i = col; i < m_numCols; i++ )
f85afd4e 9842 {
b99be8fb 9843 m_colRights[i] += diff;
f85afd4e 9844 }
962a48f6 9845
faec5a43
SN
9846 if ( !GetBatchCount() )
9847 CalcDimensions();
f85afd4e
MB
9848}
9849
43947979
VZ
9850void wxGrid::SetColMinimalWidth( int col, int width )
9851{
4db6714b
KH
9852 if (width > GetColMinimalAcceptableWidth())
9853 {
c6fbe2f0 9854 wxLongToLongHashMap::key_type key = (wxLongToLongHashMap::key_type)col;
8253f2e0 9855 m_colMinWidths[key] = width;
b8d24d4e 9856 }
af547d51
VZ
9857}
9858
9859void wxGrid::SetRowMinimalHeight( int row, int width )
9860{
4db6714b
KH
9861 if (width > GetRowMinimalAcceptableHeight())
9862 {
c6fbe2f0 9863 wxLongToLongHashMap::key_type key = (wxLongToLongHashMap::key_type)row;
8253f2e0 9864 m_rowMinHeights[key] = width;
b8d24d4e 9865 }
43947979
VZ
9866}
9867
9868int wxGrid::GetColMinimalWidth(int col) const
9869{
c6fbe2f0 9870 wxLongToLongHashMap::key_type key = (wxLongToLongHashMap::key_type)col;
8253f2e0 9871 wxLongToLongHashMap::const_iterator it = m_colMinWidths.find(key);
962a48f6 9872
ba8c1601 9873 return it != m_colMinWidths.end() ? (int)it->second : m_minAcceptableColWidth;
af547d51
VZ
9874}
9875
9876int wxGrid::GetRowMinimalHeight(int row) const
9877{
c6fbe2f0 9878 wxLongToLongHashMap::key_type key = (wxLongToLongHashMap::key_type)row;
8253f2e0 9879 wxLongToLongHashMap::const_iterator it = m_rowMinHeights.find(key);
962a48f6 9880
ba8c1601 9881 return it != m_rowMinHeights.end() ? (int)it->second : m_minAcceptableRowHeight;
b8d24d4e
RG
9882}
9883
9884void wxGrid::SetColMinimalAcceptableWidth( int width )
9885{
20c84410 9886 // We do allow a width of 0 since this gives us
962a48f6
DS
9887 // an easy way to temporarily hiding columns.
9888 if ( width >= 0 )
9889 m_minAcceptableColWidth = width;
b8d24d4e
RG
9890}
9891
9892void wxGrid::SetRowMinimalAcceptableHeight( int height )
9893{
20c84410 9894 // We do allow a height of 0 since this gives us
962a48f6
DS
9895 // an easy way to temporarily hiding rows.
9896 if ( height >= 0 )
9897 m_minAcceptableRowHeight = height;
17a1ebd1 9898}
b8d24d4e
RG
9899
9900int wxGrid::GetColMinimalAcceptableWidth() const
9901{
9902 return m_minAcceptableColWidth;
9903}
9904
9905int wxGrid::GetRowMinimalAcceptableHeight() const
9906{
9907 return m_minAcceptableRowHeight;
43947979
VZ
9908}
9909
57c086ef
VZ
9910// ----------------------------------------------------------------------------
9911// auto sizing
9912// ----------------------------------------------------------------------------
9913
af547d51 9914void wxGrid::AutoSizeColOrRow( int colOrRow, bool setAsMin, bool column )
65e4e78e
VZ
9915{
9916 wxClientDC dc(m_gridWin);
9917
962a48f6 9918 // cancel editing of cell
13f6e9e8
RG
9919 HideCellEditControl();
9920 SaveEditControlValue();
9921
962a48f6 9922 // init both of them to avoid compiler warnings, even if we only need one
a95e38c0
VZ
9923 int row = -1,
9924 col = -1;
af547d51
VZ
9925 if ( column )
9926 col = colOrRow;
9927 else
9928 row = colOrRow;
9929
9930 wxCoord extent, extentMax = 0;
9931 int max = column ? m_numRows : m_numCols;
39bcce60 9932 for ( int rowOrCol = 0; rowOrCol < max; rowOrCol++ )
65e4e78e 9933 {
af547d51
VZ
9934 if ( column )
9935 row = rowOrCol;
9936 else
9937 col = rowOrCol;
9938
2f024384
DS
9939 wxGridCellAttr *attr = GetCellAttr(row, col);
9940 wxGridCellRenderer *renderer = attr->GetRenderer(this, row, col);
65e4e78e
VZ
9941 if ( renderer )
9942 {
af547d51
VZ
9943 wxSize size = renderer->GetBestSize(*this, *attr, dc, row, col);
9944 extent = column ? size.x : size.y;
9945 if ( extent > extentMax )
65e4e78e 9946 {
af547d51 9947 extentMax = extent;
65e4e78e 9948 }
0b190b0f
VZ
9949
9950 renderer->DecRef();
65e4e78e
VZ
9951 }
9952
9953 attr->DecRef();
9954 }
9955
af547d51
VZ
9956 // now also compare with the column label extent
9957 wxCoord w, h;
65e4e78e 9958 dc.SetFont( GetLabelFont() );
294d195c
MB
9959
9960 if ( column )
d43851f7 9961 {
294d195c 9962 dc.GetTextExtent( GetColLabelValue(col), &w, &h );
4db6714b 9963 if ( GetColLabelTextOrientation() == wxVERTICAL )
d43851f7
JS
9964 w = h;
9965 }
294d195c 9966 else
ee495e4c 9967 dc.GetTextExtent( GetRowLabelValue(row), &w, &h );
294d195c 9968
af547d51
VZ
9969 extent = column ? w : h;
9970 if ( extent > extentMax )
65e4e78e 9971 {
af547d51 9972 extentMax = extent;
65e4e78e
VZ
9973 }
9974
af547d51 9975 if ( !extentMax )
65e4e78e 9976 {
af547d51 9977 // empty column - give default extent (notice that if extentMax is less
2f024384 9978 // than default extent but != 0, it's OK)
af547d51 9979 extentMax = column ? m_defaultColWidth : m_defaultRowHeight;
65e4e78e
VZ
9980 }
9981 else
9982 {
a95e38c0 9983 if ( column )
a95e38c0
VZ
9984 // leave some space around text
9985 extentMax += 10;
f6bcfd97 9986 else
f6bcfd97 9987 extentMax += 6;
65e4e78e
VZ
9988 }
9989
edb89f7e
VZ
9990 if ( column )
9991 {
962a48f6 9992 SetColSize( col, extentMax );
faec5a43
SN
9993 if ( !GetBatchCount() )
9994 {
edb89f7e
VZ
9995 int cw, ch, dummy;
9996 m_gridWin->GetClientSize( &cw, &ch );
9997 wxRect rect ( CellToRect( 0, col ) );
9998 rect.y = 0;
9999 CalcScrolledPosition(rect.x, 0, &rect.x, &dummy);
10000 rect.width = cw - rect.x;
10001 rect.height = m_colLabelHeight;
ca65c044 10002 m_colLabelWin->Refresh( true, &rect );
edb89f7e
VZ
10003 }
10004 }
10005 else
10006 {
39bcce60 10007 SetRowSize(row, extentMax);
faec5a43
SN
10008 if ( !GetBatchCount() )
10009 {
edb89f7e
VZ
10010 int cw, ch, dummy;
10011 m_gridWin->GetClientSize( &cw, &ch );
ccdee36f 10012 wxRect rect( CellToRect( row, 0 ) );
edb89f7e
VZ
10013 rect.x = 0;
10014 CalcScrolledPosition(0, rect.y, &dummy, &rect.y);
10015 rect.width = m_rowLabelWidth;
faec5a43 10016 rect.height = ch - rect.y;
ca65c044 10017 m_rowLabelWin->Refresh( true, &rect );
edb89f7e 10018 }
faec5a43 10019 }
2f024384 10020
65e4e78e
VZ
10021 if ( setAsMin )
10022 {
af547d51
VZ
10023 if ( column )
10024 SetColMinimalWidth(col, extentMax);
10025 else
39bcce60 10026 SetRowMinimalHeight(row, extentMax);
65e4e78e
VZ
10027 }
10028}
10029
266e8367 10030int wxGrid::SetOrCalcColumnSizes(bool calcOnly, bool setAsMin)
65e4e78e 10031{
57c086ef
VZ
10032 int width = m_rowLabelWidth;
10033
faec5a43
SN
10034 if ( !calcOnly )
10035 BeginBatch();
97a9929e 10036
65e4e78e
VZ
10037 for ( int col = 0; col < m_numCols; col++ )
10038 {
266e8367
VZ
10039 if ( !calcOnly )
10040 {
10041 AutoSizeColumn(col, setAsMin);
10042 }
57c086ef
VZ
10043
10044 width += GetColWidth(col);
65e4e78e 10045 }
97a9929e 10046
faec5a43
SN
10047 if ( !calcOnly )
10048 EndBatch();
97a9929e 10049
266e8367 10050 return width;
65e4e78e
VZ
10051}
10052
266e8367 10053int wxGrid::SetOrCalcRowSizes(bool calcOnly, bool setAsMin)
57c086ef
VZ
10054{
10055 int height = m_colLabelHeight;
10056
faec5a43
SN
10057 if ( !calcOnly )
10058 BeginBatch();
97a9929e 10059
57c086ef
VZ
10060 for ( int row = 0; row < m_numRows; row++ )
10061 {
af547d51 10062 if ( !calcOnly )
af547d51 10063 AutoSizeRow(row, setAsMin);
57c086ef
VZ
10064
10065 height += GetRowHeight(row);
10066 }
97a9929e 10067
faec5a43
SN
10068 if ( !calcOnly )
10069 EndBatch();
97a9929e 10070
266e8367 10071 return height;
57c086ef
VZ
10072}
10073
10074void wxGrid::AutoSize()
10075{
97a9929e
VZ
10076 BeginBatch();
10077
ca65c044 10078 wxSize size(SetOrCalcColumnSizes(false), SetOrCalcRowSizes(false));
97a9929e
VZ
10079
10080 // round up the size to a multiple of scroll step - this ensures that we
10081 // won't get the scrollbars if we're sized exactly to this width
2b5f62a0
VZ
10082 // CalcDimension adds m_extraWidth + 1 etc. to calculate the necessary
10083 // scrollbar steps
2f024384
DS
10084 wxSize sizeFit(
10085 GetScrollX(size.x + m_extraWidth + 1) * m_scrollLineX,
a9339fe2 10086 GetScrollY(size.y + m_extraHeight + 1) * m_scrollLineY );
97a9929e 10087
2b5f62a0 10088 // distribute the extra space between the columns/rows to avoid having
97a9929e 10089 // extra white space
2b5f62a0
VZ
10090
10091 // Remove the extra m_extraWidth + 1 added above
10092 wxCoord diff = sizeFit.x - size.x + (m_extraWidth + 1);
10093 if ( diff && m_numCols )
97a9929e
VZ
10094 {
10095 // try to resize the columns uniformly
10096 wxCoord diffPerCol = diff / m_numCols;
10097 if ( diffPerCol )
10098 {
10099 for ( int col = 0; col < m_numCols; col++ )
10100 {
10101 SetColSize(col, GetColWidth(col) + diffPerCol);
10102 }
10103 }
10104
10105 // add remaining amount to the last columns
10106 diff -= diffPerCol * m_numCols;
10107 if ( diff )
10108 {
10109 for ( int col = m_numCols - 1; col >= m_numCols - diff; col-- )
10110 {
10111 SetColSize(col, GetColWidth(col) + 1);
10112 }
10113 }
10114 }
10115
10116 // same for rows
2b5f62a0
VZ
10117 diff = sizeFit.y - size.y - (m_extraHeight + 1);
10118 if ( diff && m_numRows )
97a9929e
VZ
10119 {
10120 // try to resize the columns uniformly
10121 wxCoord diffPerRow = diff / m_numRows;
10122 if ( diffPerRow )
10123 {
10124 for ( int row = 0; row < m_numRows; row++ )
10125 {
10126 SetRowSize(row, GetRowHeight(row) + diffPerRow);
10127 }
10128 }
10129
10130 // add remaining amount to the last rows
10131 diff -= diffPerRow * m_numRows;
10132 if ( diff )
10133 {
10134 for ( int row = m_numRows - 1; row >= m_numRows - diff; row-- )
10135 {
10136 SetRowSize(row, GetRowHeight(row) + 1);
10137 }
10138 }
10139 }
10140
10141 EndBatch();
10142
10143 SetClientSize(sizeFit);
266e8367
VZ
10144}
10145
d43851f7
JS
10146void wxGrid::AutoSizeRowLabelSize( int row )
10147{
10148 wxArrayString lines;
10149 long w, h;
10150
10151 // Hide the edit control, so it
4db6714b
KH
10152 // won't interfere with drag-shrinking.
10153 if ( IsCellEditControlShown() )
d43851f7
JS
10154 {
10155 HideCellEditControl();
10156 SaveEditControlValue();
10157 }
10158
10159 // autosize row height depending on label text
10160 StringToLines( GetRowLabelValue( row ), lines );
10161 wxClientDC dc( m_rowLabelWin );
ccdee36f 10162 GetTextBoxSize( dc, lines, &w, &h );
4db6714b 10163 if ( h < m_defaultRowHeight )
d43851f7
JS
10164 h = m_defaultRowHeight;
10165 SetRowSize(row, h);
10166 ForceRefresh();
10167}
10168
10169void wxGrid::AutoSizeColLabelSize( int col )
10170{
10171 wxArrayString lines;
10172 long w, h;
10173
10174 // Hide the edit control, so it
10175 // won't interfer with drag-shrinking.
4db6714b 10176 if ( IsCellEditControlShown() )
d43851f7
JS
10177 {
10178 HideCellEditControl();
10179 SaveEditControlValue();
10180 }
10181
10182 // autosize column width depending on label text
10183 StringToLines( GetColLabelValue( col ), lines );
10184 wxClientDC dc( m_colLabelWin );
4db6714b 10185 if ( GetColLabelTextOrientation() == wxHORIZONTAL )
2f024384 10186 GetTextBoxSize( dc, lines, &w, &h );
d43851f7 10187 else
2f024384 10188 GetTextBoxSize( dc, lines, &h, &w );
4db6714b 10189 if ( w < m_defaultColWidth )
d43851f7
JS
10190 w = m_defaultColWidth;
10191 SetColSize(col, w);
10192 ForceRefresh();
10193}
10194
266e8367
VZ
10195wxSize wxGrid::DoGetBestSize() const
10196{
10197 // don't set sizes, only calculate them
10198 wxGrid *self = (wxGrid *)this; // const_cast
10199
84035150 10200 int width, height;
ca65c044
WS
10201 width = self->SetOrCalcColumnSizes(true);
10202 height = self->SetOrCalcRowSizes(true);
84035150 10203
4db6714b
KH
10204 if (!width)
10205 width = 100;
10206 if (!height)
10207 height = 80;
42841dfc 10208
4db6714b
KH
10209 // Round up to a multiple the scroll rate
10210 // NOTE: this still doesn't get rid of the scrollbars;
10211 // is there any magic incantation for that?
6d308072
RD
10212 int xpu, ypu;
10213 GetScrollPixelsPerUnit(&xpu, &ypu);
b39a5dc4
RD
10214 if (xpu)
10215 width += 1 + xpu - (width % xpu);
10216 if (ypu)
10217 height += 1 + ypu - (height % ypu);
42841dfc 10218
6d308072 10219 // limit to 1/4 of the screen size
84035150 10220 int maxwidth, maxheight;
4db6714b 10221 wxDisplaySize( &maxwidth, &maxheight );
6d308072 10222 maxwidth /= 2;
42841dfc 10223 maxheight /= 2;
4db6714b
KH
10224 if ( width > maxwidth )
10225 width = maxwidth;
10226 if ( height > maxheight )
10227 height = maxheight;
42841dfc 10228
6d308072 10229 wxSize best(width, height);
962a48f6 10230
6d308072
RD
10231 // NOTE: This size should be cached, but first we need to add calls to
10232 // InvalidateBestSize everywhere that could change the results of this
10233 // calculation.
10234 // CacheBestSize(size);
962a48f6 10235
6d308072 10236 return best;
266e8367
VZ
10237}
10238
10239void wxGrid::Fit()
10240{
10241 AutoSize();
57c086ef
VZ
10242}
10243
6fc0f38f
SN
10244wxPen& wxGrid::GetDividerPen() const
10245{
10246 return wxNullPen;
10247}
10248
57c086ef
VZ
10249// ----------------------------------------------------------------------------
10250// cell value accessor functions
10251// ----------------------------------------------------------------------------
f85afd4e
MB
10252
10253void wxGrid::SetCellValue( int row, int col, const wxString& s )
10254{
10255 if ( m_table )
10256 {
f6bcfd97 10257 m_table->SetValue( row, col, s );
2d66e025
MB
10258 if ( !GetBatchCount() )
10259 {
27f35b66
SN
10260 int dummy;
10261 wxRect rect( CellToRect( row, col ) );
10262 rect.x = 0;
10263 rect.width = m_gridWin->GetClientSize().GetWidth();
10264 CalcScrolledPosition(0, rect.y, &dummy, &rect.y);
ca65c044 10265 m_gridWin->Refresh( false, &rect );
2d66e025 10266 }
60ff3b99 10267
f85afd4e 10268 if ( m_currentCellCoords.GetRow() == row &&
4cfa5de6 10269 m_currentCellCoords.GetCol() == col &&
f6bcfd97
BP
10270 IsCellEditControlShown())
10271 // Note: If we are using IsCellEditControlEnabled,
10272 // this interacts badly with calling SetCellValue from
10273 // an EVT_GRID_CELL_CHANGE handler.
f85afd4e 10274 {
4cfa5de6
RD
10275 HideCellEditControl();
10276 ShowCellEditControl(); // will reread data from table
f85afd4e 10277 }
f85afd4e
MB
10278 }
10279}
10280
962a48f6 10281// ----------------------------------------------------------------------------
2f024384 10282// block, row and column selection
962a48f6 10283// ----------------------------------------------------------------------------
f85afd4e
MB
10284
10285void wxGrid::SelectRow( int row, bool addToSelected )
10286{
b5808881 10287 if ( IsSelection() && !addToSelected )
e32352cf 10288 ClearSelection();
70c7a608 10289
3f3dc2ef 10290 if ( m_selection )
ca65c044 10291 m_selection->SelectRow( row, false, addToSelected );
f85afd4e
MB
10292}
10293
f85afd4e
MB
10294void wxGrid::SelectCol( int col, bool addToSelected )
10295{
b5808881 10296 if ( IsSelection() && !addToSelected )
e32352cf 10297 ClearSelection();
f85afd4e 10298
3f3dc2ef 10299 if ( m_selection )
ca65c044 10300 m_selection->SelectCol( col, false, addToSelected );
f85afd4e
MB
10301}
10302
84912ef8 10303void wxGrid::SelectBlock( int topRow, int leftCol, int bottomRow, int rightCol,
c9097836 10304 bool addToSelected )
f85afd4e 10305{
c9097836
MB
10306 if ( IsSelection() && !addToSelected )
10307 ClearSelection();
f85afd4e 10308
3f3dc2ef
VZ
10309 if ( m_selection )
10310 m_selection->SelectBlock( topRow, leftCol, bottomRow, rightCol,
ca65c044 10311 false, addToSelected );
f85afd4e
MB
10312}
10313
10314void wxGrid::SelectAll()
10315{
f74d0b57 10316 if ( m_numRows > 0 && m_numCols > 0 )
3f3dc2ef
VZ
10317 {
10318 if ( m_selection )
ccdee36f 10319 m_selection->SelectBlock( 0, 0, m_numRows - 1, m_numCols - 1 );
3f3dc2ef 10320 }
b5808881 10321}
f85afd4e 10322
962a48f6
DS
10323// ----------------------------------------------------------------------------
10324// cell, row and col deselection
10325// ----------------------------------------------------------------------------
f7b4b343
VZ
10326
10327void wxGrid::DeselectRow( int row )
10328{
3f3dc2ef
VZ
10329 if ( !m_selection )
10330 return;
10331
f7b4b343
VZ
10332 if ( m_selection->GetSelectionMode() == wxGrid::wxGridSelectRows )
10333 {
10334 if ( m_selection->IsInSelection(row, 0 ) )
2f024384 10335 m_selection->ToggleCellSelection(row, 0);
ffdd3c98 10336 }
f7b4b343
VZ
10337 else
10338 {
10339 int nCols = GetNumberCols();
2f024384 10340 for ( int i = 0; i < nCols; i++ )
f7b4b343
VZ
10341 {
10342 if ( m_selection->IsInSelection(row, i ) )
2f024384 10343 m_selection->ToggleCellSelection(row, i);
f7b4b343
VZ
10344 }
10345 }
10346}
10347
10348void wxGrid::DeselectCol( int col )
10349{
3f3dc2ef
VZ
10350 if ( !m_selection )
10351 return;
10352
f7b4b343
VZ
10353 if ( m_selection->GetSelectionMode() == wxGrid::wxGridSelectColumns )
10354 {
10355 if ( m_selection->IsInSelection(0, col ) )
2f024384 10356 m_selection->ToggleCellSelection(0, col);
f7b4b343
VZ
10357 }
10358 else
10359 {
10360 int nRows = GetNumberRows();
2f024384 10361 for ( int i = 0; i < nRows; i++ )
f7b4b343
VZ
10362 {
10363 if ( m_selection->IsInSelection(i, col ) )
10364 m_selection->ToggleCellSelection(i, col);
10365 }
10366 }
10367}
10368
10369void wxGrid::DeselectCell( int row, int col )
10370{
3f3dc2ef 10371 if ( m_selection && m_selection->IsInSelection(row, col) )
f7b4b343
VZ
10372 m_selection->ToggleCellSelection(row, col);
10373}
10374
b5808881
SN
10375bool wxGrid::IsSelection()
10376{
3f3dc2ef 10377 return ( m_selection && (m_selection->IsSelection() ||
b5808881 10378 ( m_selectingTopLeft != wxGridNoCellCoords &&
3f3dc2ef 10379 m_selectingBottomRight != wxGridNoCellCoords) ) );
f85afd4e
MB
10380}
10381
84035150 10382bool wxGrid::IsInSelection( int row, int col ) const
b5808881 10383{
3f3dc2ef 10384 return ( m_selection && (m_selection->IsInSelection( row, col ) ||
b5808881
SN
10385 ( row >= m_selectingTopLeft.GetRow() &&
10386 col >= m_selectingTopLeft.GetCol() &&
10387 row <= m_selectingBottomRight.GetRow() &&
3f3dc2ef 10388 col <= m_selectingBottomRight.GetCol() )) );
b5808881 10389}
f85afd4e 10390
aa5b8857
SN
10391wxGridCellCoordsArray wxGrid::GetSelectedCells() const
10392{
4db6714b
KH
10393 if (!m_selection)
10394 {
10395 wxGridCellCoordsArray a;
10396 return a;
10397 }
10398
aa5b8857
SN
10399 return m_selection->m_cellSelection;
10400}
4db6714b 10401
aa5b8857
SN
10402wxGridCellCoordsArray wxGrid::GetSelectionBlockTopLeft() const
10403{
4db6714b
KH
10404 if (!m_selection)
10405 {
10406 wxGridCellCoordsArray a;
10407 return a;
10408 }
10409
aa5b8857
SN
10410 return m_selection->m_blockSelectionTopLeft;
10411}
4db6714b 10412
aa5b8857
SN
10413wxGridCellCoordsArray wxGrid::GetSelectionBlockBottomRight() const
10414{
4db6714b
KH
10415 if (!m_selection)
10416 {
10417 wxGridCellCoordsArray a;
10418 return a;
10419 }
10420
a8de8190 10421 return m_selection->m_blockSelectionBottomRight;
aa5b8857 10422}
4db6714b 10423
aa5b8857
SN
10424wxArrayInt wxGrid::GetSelectedRows() const
10425{
4db6714b
KH
10426 if (!m_selection)
10427 {
10428 wxArrayInt a;
10429 return a;
10430 }
10431
aa5b8857
SN
10432 return m_selection->m_rowSelection;
10433}
4db6714b 10434
aa5b8857
SN
10435wxArrayInt wxGrid::GetSelectedCols() const
10436{
4db6714b
KH
10437 if (!m_selection)
10438 {
10439 wxArrayInt a;
10440 return a;
10441 }
10442
aa5b8857
SN
10443 return m_selection->m_colSelection;
10444}
10445
f85afd4e
MB
10446void wxGrid::ClearSelection()
10447{
b5808881
SN
10448 m_selectingTopLeft = wxGridNoCellCoords;
10449 m_selectingBottomRight = wxGridNoCellCoords;
3f3dc2ef
VZ
10450 if ( m_selection )
10451 m_selection->ClearSelection();
8f177c8e 10452}
f85afd4e 10453
da6af900 10454// This function returns the rectangle that encloses the given block
2d66e025
MB
10455// in device coords clipped to the client size of the grid window.
10456//
58dd5b3b
MB
10457wxRect wxGrid::BlockToDeviceRect( const wxGridCellCoords &topLeft,
10458 const wxGridCellCoords &bottomRight )
f85afd4e 10459{
58dd5b3b 10460 wxRect rect( wxGridNoCellRect );
f85afd4e
MB
10461 wxRect cellRect;
10462
58dd5b3b
MB
10463 cellRect = CellToRect( topLeft );
10464 if ( cellRect != wxGridNoCellRect )
f85afd4e 10465 {
58dd5b3b
MB
10466 rect = cellRect;
10467 }
10468 else
10469 {
962a48f6 10470 rect = wxRect(0, 0, 0, 0);
58dd5b3b 10471 }
60ff3b99 10472
58dd5b3b
MB
10473 cellRect = CellToRect( bottomRight );
10474 if ( cellRect != wxGridNoCellRect )
10475 {
10476 rect += cellRect;
2d66e025
MB
10477 }
10478 else
10479 {
10480 return wxGridNoCellRect;
f85afd4e
MB
10481 }
10482
27f35b66
SN
10483 int i, j;
10484 int left = rect.GetLeft();
10485 int top = rect.GetTop();
10486 int right = rect.GetRight();
10487 int bottom = rect.GetBottom();
10488
10489 int leftCol = topLeft.GetCol();
10490 int topRow = topLeft.GetRow();
10491 int rightCol = bottomRight.GetCol();
10492 int bottomRow = bottomRight.GetRow();
10493
3ed884a0
SN
10494 if (left > right)
10495 {
10496 i = left;
10497 left = right;
10498 right = i;
10499 i = leftCol;
4db6714b 10500 leftCol = rightCol;
3ed884a0
SN
10501 rightCol = i;
10502 }
10503
10504 if (top > bottom)
10505 {
10506 i = top;
10507 top = bottom;
10508 bottom = i;
10509 i = topRow;
10510 topRow = bottomRow;
10511 bottomRow = i;
10512 }
10513
27f35b66
SN
10514 for ( j = topRow; j <= bottomRow; j++ )
10515 {
10516 for ( i = leftCol; i <= rightCol; i++ )
10517 {
4db6714b 10518 if ((j == topRow) || (j == bottomRow) || (i == leftCol) || (i == rightCol))
27f35b66
SN
10519 {
10520 cellRect = CellToRect( j, i );
10521
10522 if (cellRect.x < left)
10523 left = cellRect.x;
10524 if (cellRect.y < top)
10525 top = cellRect.y;
10526 if (cellRect.x + cellRect.width > right)
10527 right = cellRect.x + cellRect.width;
10528 if (cellRect.y + cellRect.height > bottom)
10529 bottom = cellRect.y + cellRect.height;
10530 }
4db6714b
KH
10531 else
10532 {
10533 i = rightCol; // jump over inner cells.
10534 }
27f35b66
SN
10535 }
10536 }
10537
58dd5b3b
MB
10538 // convert to scrolled coords
10539 //
27f35b66
SN
10540 CalcScrolledPosition( left, top, &left, &top );
10541 CalcScrolledPosition( right, bottom, &right, &bottom );
58dd5b3b
MB
10542
10543 int cw, ch;
10544 m_gridWin->GetClientSize( &cw, &ch );
10545
f6bcfd97 10546 if (right < 0 || bottom < 0 || left > cw || top > ch)
c47addef 10547 return wxRect(0,0,0,0);
f6bcfd97 10548
58dd5b3b
MB
10549 rect.SetLeft( wxMax(0, left) );
10550 rect.SetTop( wxMax(0, top) );
10551 rect.SetRight( wxMin(cw, right) );
10552 rect.SetBottom( wxMin(ch, bottom) );
10553
f85afd4e
MB
10554 return rect;
10555}
10556
962a48f6
DS
10557// ----------------------------------------------------------------------------
10558// grid event classes
10559// ----------------------------------------------------------------------------
f85afd4e 10560
bf7945ce 10561IMPLEMENT_DYNAMIC_CLASS( wxGridEvent, wxNotifyEvent )
f85afd4e
MB
10562
10563wxGridEvent::wxGridEvent( int id, wxEventType type, wxObject* obj,
5c8fc7c1 10564 int row, int col, int x, int y, bool sel,
f85afd4e
MB
10565 bool control, bool shift, bool alt, bool meta )
10566 : wxNotifyEvent( type, id )
10567{
10568 m_row = row;
10569 m_col = col;
10570 m_x = x;
10571 m_y = y;
5c8fc7c1 10572 m_selecting = sel;
f85afd4e
MB
10573 m_control = control;
10574 m_shift = shift;
10575 m_alt = alt;
10576 m_meta = meta;
8f177c8e 10577
f85afd4e
MB
10578 SetEventObject(obj);
10579}
10580
10581
bf7945ce 10582IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent, wxNotifyEvent )
f85afd4e
MB
10583
10584wxGridSizeEvent::wxGridSizeEvent( int id, wxEventType type, wxObject* obj,
10585 int rowOrCol, int x, int y,
10586 bool control, bool shift, bool alt, bool meta )
10587 : wxNotifyEvent( type, id )
10588{
10589 m_rowOrCol = rowOrCol;
10590 m_x = x;
10591 m_y = y;
10592 m_control = control;
10593 m_shift = shift;
10594 m_alt = alt;
10595 m_meta = meta;
8f177c8e 10596
f85afd4e
MB
10597 SetEventObject(obj);
10598}
10599
10600
bf7945ce 10601IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent, wxNotifyEvent )
f85afd4e
MB
10602
10603wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id, wxEventType type, wxObject* obj,
8f177c8e
VZ
10604 const wxGridCellCoords& topLeft,
10605 const wxGridCellCoords& bottomRight,
5c8fc7c1
SN
10606 bool sel, bool control,
10607 bool shift, bool alt, bool meta )
8f177c8e 10608 : wxNotifyEvent( type, id )
f85afd4e 10609{
2f024384 10610 m_topLeft = topLeft;
f85afd4e 10611 m_bottomRight = bottomRight;
2f024384
DS
10612 m_selecting = sel;
10613 m_control = control;
10614 m_shift = shift;
10615 m_alt = alt;
10616 m_meta = meta;
f85afd4e
MB
10617
10618 SetEventObject(obj);
10619}
10620
10621
bf7945ce
RD
10622IMPLEMENT_DYNAMIC_CLASS(wxGridEditorCreatedEvent, wxCommandEvent)
10623
10624wxGridEditorCreatedEvent::wxGridEditorCreatedEvent(int id, wxEventType type,
10625 wxObject* obj, int row,
10626 int col, wxControl* ctrl)
10627 : wxCommandEvent(type, id)
10628{
10629 SetEventObject(obj);
10630 m_row = row;
10631 m_col = col;
10632 m_ctrl = ctrl;
10633}
10634
27b92ca4 10635#endif // wxUSE_GRID