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