]> git.saurik.com Git - wxWidgets.git/blame - src/generic/grid.cpp
Added a fix for compilers with problems to compute signed % unsigned
[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)
5// Modified by:
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
f85afd4e
MB
20#ifdef __GNUG__
21 #pragma implementation "grid.h"
22#endif
23
4d85bcd1
JS
24// For compilers that support precompilation, includes "wx/wx.h".
25#include "wx/wxprec.h"
26
27#include "wx/defs.h"
f85afd4e
MB
28
29#ifdef __BORLANDC__
30 #pragma hdrstop
31#endif
32
8f177c8e 33#if !defined(wxUSE_NEW_GRID) || !(wxUSE_NEW_GRID)
4d85bcd1
JS
34#include "gridg.cpp"
35#else
36
f85afd4e
MB
37#ifndef WX_PRECOMP
38 #include "wx/utils.h"
39 #include "wx/dcclient.h"
40 #include "wx/settings.h"
41 #include "wx/log.h"
508011ce
VZ
42 #include "wx/textctrl.h"
43 #include "wx/checkbox.h"
4ee5fc9c 44 #include "wx/combobox.h"
816be743 45 #include "wx/valtext.h"
f85afd4e
MB
46#endif
47
cb5df486 48#include "wx/textfile.h"
816be743 49#include "wx/spinctrl.h"
c4608a8a 50#include "wx/tokenzr.h"
6d004f67 51
07296f0b 52#include "wx/grid.h"
b5808881 53#include "wx/generic/gridsel.h"
07296f0b 54
b99be8fb 55// ----------------------------------------------------------------------------
758cbedf 56// array classes
b99be8fb
VZ
57// ----------------------------------------------------------------------------
58
758cbedf
VZ
59WX_DEFINE_ARRAY(wxGridCellAttr *, wxArrayAttrs);
60
b99be8fb
VZ
61struct wxGridCellWithAttr
62{
2e9a6788
VZ
63 wxGridCellWithAttr(int row, int col, wxGridCellAttr *attr_)
64 : coords(row, col), attr(attr_)
b99be8fb
VZ
65 {
66 }
67
2e9a6788
VZ
68 ~wxGridCellWithAttr()
69 {
70 attr->DecRef();
71 }
72
b99be8fb 73 wxGridCellCoords coords;
2e9a6788 74 wxGridCellAttr *attr;
b99be8fb
VZ
75};
76
77WX_DECLARE_OBJARRAY(wxGridCellWithAttr, wxGridCellWithAttrArray);
78
79#include "wx/arrimpl.cpp"
80
81WX_DEFINE_OBJARRAY(wxGridCellCoordsArray)
82WX_DEFINE_OBJARRAY(wxGridCellWithAttrArray)
83
84// ----------------------------------------------------------------------------
85// private classes
86// ----------------------------------------------------------------------------
87
88class WXDLLEXPORT wxGridRowLabelWindow : public wxWindow
89{
90public:
91 wxGridRowLabelWindow() { m_owner = (wxGrid *)NULL; }
92 wxGridRowLabelWindow( wxGrid *parent, wxWindowID id,
93 const wxPoint &pos, const wxSize &size );
94
95private:
96 wxGrid *m_owner;
97
98 void OnPaint( wxPaintEvent& event );
99 void OnMouseEvent( wxMouseEvent& event );
100 void OnKeyDown( wxKeyEvent& event );
101
102 DECLARE_DYNAMIC_CLASS(wxGridRowLabelWindow)
103 DECLARE_EVENT_TABLE()
104};
105
106
107class WXDLLEXPORT wxGridColLabelWindow : public wxWindow
108{
109public:
110 wxGridColLabelWindow() { m_owner = (wxGrid *)NULL; }
111 wxGridColLabelWindow( wxGrid *parent, wxWindowID id,
112 const wxPoint &pos, const wxSize &size );
113
114private:
115 wxGrid *m_owner;
116
117 void OnPaint( wxPaintEvent &event );
118 void OnMouseEvent( wxMouseEvent& event );
119 void OnKeyDown( wxKeyEvent& event );
120
121 DECLARE_DYNAMIC_CLASS(wxGridColLabelWindow)
122 DECLARE_EVENT_TABLE()
123};
124
125
126class WXDLLEXPORT wxGridCornerLabelWindow : public wxWindow
127{
128public:
129 wxGridCornerLabelWindow() { m_owner = (wxGrid *)NULL; }
130 wxGridCornerLabelWindow( wxGrid *parent, wxWindowID id,
131 const wxPoint &pos, const wxSize &size );
132
133private:
134 wxGrid *m_owner;
135
136 void OnMouseEvent( wxMouseEvent& event );
137 void OnKeyDown( wxKeyEvent& event );
138 void OnPaint( wxPaintEvent& event );
139
140 DECLARE_DYNAMIC_CLASS(wxGridCornerLabelWindow)
141 DECLARE_EVENT_TABLE()
142};
143
144class WXDLLEXPORT wxGridWindow : public wxPanel
145{
146public:
147 wxGridWindow()
148 {
149 m_owner = (wxGrid *)NULL;
150 m_rowLabelWin = (wxGridRowLabelWindow *)NULL;
151 m_colLabelWin = (wxGridColLabelWindow *)NULL;
152 }
153
154 wxGridWindow( wxGrid *parent,
155 wxGridRowLabelWindow *rowLblWin,
156 wxGridColLabelWindow *colLblWin,
157 wxWindowID id, const wxPoint &pos, const wxSize &size );
158 ~wxGridWindow();
159
160 void ScrollWindow( int dx, int dy, const wxRect *rect );
161
162private:
163 wxGrid *m_owner;
164 wxGridRowLabelWindow *m_rowLabelWin;
165 wxGridColLabelWindow *m_colLabelWin;
166
167 void OnPaint( wxPaintEvent &event );
168 void OnMouseEvent( wxMouseEvent& event );
169 void OnKeyDown( wxKeyEvent& );
2796cce3
RD
170 void OnEraseBackground( wxEraseEvent& );
171
b99be8fb
VZ
172
173 DECLARE_DYNAMIC_CLASS(wxGridWindow)
174 DECLARE_EVENT_TABLE()
175};
176
2796cce3
RD
177
178
179class wxGridCellEditorEvtHandler : public wxEvtHandler
180{
181public:
182 wxGridCellEditorEvtHandler()
183 : m_grid(0), m_editor(0)
184 { }
185 wxGridCellEditorEvtHandler(wxGrid* grid, wxGridCellEditor* editor)
186 : m_grid(grid), m_editor(editor)
187 { }
188
189 void OnKeyDown(wxKeyEvent& event);
fb0de762 190 void OnChar(wxKeyEvent& event);
2796cce3
RD
191
192private:
193 wxGrid* m_grid;
194 wxGridCellEditor* m_editor;
195 DECLARE_DYNAMIC_CLASS(wxGridCellEditorEvtHandler)
196 DECLARE_EVENT_TABLE()
197};
198
199
200IMPLEMENT_DYNAMIC_CLASS( wxGridCellEditorEvtHandler, wxEvtHandler )
201BEGIN_EVENT_TABLE( wxGridCellEditorEvtHandler, wxEvtHandler )
202 EVT_KEY_DOWN( wxGridCellEditorEvtHandler::OnKeyDown )
fb0de762 203 EVT_CHAR( wxGridCellEditorEvtHandler::OnChar )
2796cce3
RD
204END_EVENT_TABLE()
205
206
207
758cbedf 208// ----------------------------------------------------------------------------
b99be8fb 209// the internal data representation used by wxGridCellAttrProvider
758cbedf
VZ
210// ----------------------------------------------------------------------------
211
212// this class stores attributes set for cells
213class WXDLLEXPORT wxGridCellAttrData
b99be8fb
VZ
214{
215public:
2e9a6788 216 void SetAttr(wxGridCellAttr *attr, int row, int col);
b99be8fb 217 wxGridCellAttr *GetAttr(int row, int col) const;
4d60017a
SN
218 void UpdateAttrRows( size_t pos, int numRows );
219 void UpdateAttrCols( size_t pos, int numCols );
b99be8fb
VZ
220
221private:
222 // searches for the attr for given cell, returns wxNOT_FOUND if not found
223 int FindIndex(int row, int col) const;
224
225 wxGridCellWithAttrArray m_attrs;
226};
227
758cbedf
VZ
228// this class stores attributes set for rows or columns
229class WXDLLEXPORT wxGridRowOrColAttrData
230{
231public:
ee6694a7
VZ
232 // empty ctor to suppress warnings
233 wxGridRowOrColAttrData() { }
758cbedf
VZ
234 ~wxGridRowOrColAttrData();
235
236 void SetAttr(wxGridCellAttr *attr, int rowOrCol);
237 wxGridCellAttr *GetAttr(int rowOrCol) const;
4d60017a 238 void UpdateAttrRowsOrCols( size_t pos, int numRowsOrCols );
758cbedf
VZ
239
240private:
241 wxArrayInt m_rowsOrCols;
242 wxArrayAttrs m_attrs;
243};
244
245// NB: this is just a wrapper around 3 objects: one which stores cell
246// attributes, and 2 others for row/col ones
247class WXDLLEXPORT wxGridCellAttrProviderData
248{
249public:
250 wxGridCellAttrData m_cellAttrs;
251 wxGridRowOrColAttrData m_rowAttrs,
252 m_colAttrs;
253};
254
f2d76237
RD
255
256// ----------------------------------------------------------------------------
257// data structures used for the data type registry
258// ----------------------------------------------------------------------------
259
b94ae1ea
VZ
260struct wxGridDataTypeInfo
261{
f2d76237
RD
262 wxGridDataTypeInfo(const wxString& typeName,
263 wxGridCellRenderer* renderer,
264 wxGridCellEditor* editor)
265 : m_typeName(typeName), m_renderer(renderer), m_editor(editor)
266 { }
267
39bcce60
VZ
268 ~wxGridDataTypeInfo()
269 {
270 wxSafeDecRef(m_renderer);
271 wxSafeDecRef(m_editor);
272 }
f2d76237
RD
273
274 wxString m_typeName;
275 wxGridCellRenderer* m_renderer;
276 wxGridCellEditor* m_editor;
277};
278
279
280WX_DEFINE_ARRAY(wxGridDataTypeInfo*, wxGridDataTypeInfoArray);
281
282
b94ae1ea
VZ
283class WXDLLEXPORT wxGridTypeRegistry
284{
f2d76237
RD
285public:
286 ~wxGridTypeRegistry();
b94ae1ea 287
f2d76237
RD
288 void RegisterDataType(const wxString& typeName,
289 wxGridCellRenderer* renderer,
290 wxGridCellEditor* editor);
c4608a8a
VZ
291
292 // find one of already registered data types
293 int FindRegisteredDataType(const wxString& typeName);
294
295 // try to FindRegisteredDataType(), if this fails and typeName is one of
296 // standard typenames, register it and return its index
f2d76237 297 int FindDataType(const wxString& typeName);
c4608a8a
VZ
298
299 // try to FindDataType(), if it fails see if it is not one of already
300 // registered data types with some params in which case clone the
301 // registered data type and set params for it
302 int FindOrCloneDataType(const wxString& typeName);
303
f2d76237
RD
304 wxGridCellRenderer* GetRenderer(int index);
305 wxGridCellEditor* GetEditor(int index);
306
307private:
308 wxGridDataTypeInfoArray m_typeinfo;
309};
310
b99be8fb
VZ
311// ----------------------------------------------------------------------------
312// conditional compilation
313// ----------------------------------------------------------------------------
314
9496deb5
MB
315#ifndef WXGRID_DRAW_LINES
316#define WXGRID_DRAW_LINES 1
796df70a
SN
317#endif
318
0a976765
VZ
319// ----------------------------------------------------------------------------
320// globals
321// ----------------------------------------------------------------------------
322
323//#define DEBUG_ATTR_CACHE
324#ifdef DEBUG_ATTR_CACHE
325 static size_t gs_nAttrCacheHits = 0;
326 static size_t gs_nAttrCacheMisses = 0;
327#endif // DEBUG_ATTR_CACHE
f85afd4e 328
43947979
VZ
329// ----------------------------------------------------------------------------
330// constants
331// ----------------------------------------------------------------------------
332
f85afd4e
MB
333wxGridCellCoords wxGridNoCellCoords( -1, -1 );
334wxRect wxGridNoCellRect( -1, -1, -1, -1 );
335
f0102d2a
VZ
336// scroll line size
337// TODO: fixed so far - make configurable later (and also different for x/y)
338static const size_t GRID_SCROLL_LINE = 10;
f85afd4e 339
43947979
VZ
340// the size of hash tables used a bit everywhere (the max number of elements
341// in these hash tables is the number of rows/columns)
342static const int GRID_HASH_SIZE = 100;
343
ab79958a
VZ
344// ============================================================================
345// implementation
346// ============================================================================
347
2796cce3
RD
348// ----------------------------------------------------------------------------
349// wxGridCellEditor
350// ----------------------------------------------------------------------------
351
352wxGridCellEditor::wxGridCellEditor()
353{
354 m_control = NULL;
355}
356
357
358wxGridCellEditor::~wxGridCellEditor()
359{
360 Destroy();
361}
362
508011ce
VZ
363void wxGridCellEditor::Create(wxWindow* WXUNUSED(parent),
364 wxWindowID WXUNUSED(id),
365 wxEvtHandler* evtHandler)
366{
189d0213 367 if ( evtHandler )
508011ce
VZ
368 m_control->PushEventHandler(evtHandler);
369}
2796cce3 370
189d0213
VZ
371void wxGridCellEditor::PaintBackground(const wxRect& rectCell,
372 wxGridCellAttr *attr)
373{
374 // erase the background because we might not fill the cell
375 wxClientDC dc(m_control->GetParent());
376 dc.SetPen(*wxTRANSPARENT_PEN);
377 dc.SetBrush(wxBrush(attr->GetBackgroundColour(), wxSOLID));
378 dc.DrawRectangle(rectCell);
379
380 // redraw the control we just painted over
381 m_control->Refresh();
382}
383
2796cce3
RD
384void wxGridCellEditor::Destroy()
385{
508011ce
VZ
386 if (m_control)
387 {
b94ae1ea
VZ
388 m_control->PopEventHandler(TRUE /* delete it*/);
389
2796cce3
RD
390 m_control->Destroy();
391 m_control = NULL;
392 }
393}
394
3da93aae 395void wxGridCellEditor::Show(bool show, wxGridCellAttr *attr)
2796cce3
RD
396{
397 wxASSERT_MSG(m_control,
398 wxT("The wxGridCellEditor must be Created first!"));
399 m_control->Show(show);
3da93aae
VZ
400
401 if ( show )
402 {
403 // set the colours/fonts if we have any
404 if ( attr )
405 {
f2d76237
RD
406 m_colFgOld = m_control->GetForegroundColour();
407 m_control->SetForegroundColour(attr->GetTextColour());
3da93aae 408
f2d76237
RD
409 m_colBgOld = m_control->GetBackgroundColour();
410 m_control->SetBackgroundColour(attr->GetBackgroundColour());
3da93aae 411
f2d76237
RD
412 m_fontOld = m_control->GetFont();
413 m_control->SetFont(attr->GetFont());
3da93aae
VZ
414
415 // can't do anything more in the base class version, the other
416 // attributes may only be used by the derived classes
417 }
418 }
419 else
420 {
421 // restore the standard colours fonts
422 if ( m_colFgOld.Ok() )
423 {
424 m_control->SetForegroundColour(m_colFgOld);
425 m_colFgOld = wxNullColour;
426 }
427
428 if ( m_colBgOld.Ok() )
429 {
430 m_control->SetBackgroundColour(m_colBgOld);
431 m_colBgOld = wxNullColour;
432 }
433
434 if ( m_fontOld.Ok() )
435 {
436 m_control->SetFont(m_fontOld);
437 m_fontOld = wxNullFont;
438 }
439 }
2796cce3
RD
440}
441
442void wxGridCellEditor::SetSize(const wxRect& rect)
443{
444 wxASSERT_MSG(m_control,
445 wxT("The wxGridCellEditor must be Created first!"));
28a77bc4 446 m_control->SetSize(rect, wxSIZE_ALLOW_MINUS_ONE);
2796cce3
RD
447}
448
449void wxGridCellEditor::HandleReturn(wxKeyEvent& event)
450{
451 event.Skip();
452}
453
454
2c9a89e0
RD
455void wxGridCellEditor::StartingKey(wxKeyEvent& event)
456{
e195a54c
VZ
457 event.Skip();
458}
2c9a89e0 459
e195a54c
VZ
460void wxGridCellEditor::StartingClick()
461{
b54ba671 462}
2c9a89e0 463
b54ba671
VZ
464// ----------------------------------------------------------------------------
465// wxGridCellTextEditor
466// ----------------------------------------------------------------------------
2c9a89e0 467
2796cce3
RD
468wxGridCellTextEditor::wxGridCellTextEditor()
469{
c4608a8a 470 m_maxChars = 0;
2796cce3
RD
471}
472
473void wxGridCellTextEditor::Create(wxWindow* parent,
474 wxWindowID id,
2796cce3
RD
475 wxEvtHandler* evtHandler)
476{
508011ce 477 m_control = new wxTextCtrl(parent, id, wxEmptyString,
2c9a89e0 478 wxDefaultPosition, wxDefaultSize
2796cce3 479#if defined(__WXMSW__)
2c9a89e0 480 , wxTE_MULTILINE | wxTE_NO_VSCROLL // necessary ???
2796cce3 481#endif
508011ce 482 );
2796cce3 483
c4608a8a
VZ
484 // TODO: use m_maxChars
485
508011ce 486 wxGridCellEditor::Create(parent, id, evtHandler);
2796cce3
RD
487}
488
189d0213
VZ
489void wxGridCellTextEditor::PaintBackground(const wxRect& WXUNUSED(rectCell),
490 wxGridCellAttr * WXUNUSED(attr))
491{
492 // as we fill the entire client area, don't do anything here to minimize
493 // flicker
494}
2796cce3 495
99306db2
VZ
496void wxGridCellTextEditor::SetSize(const wxRect& rectOrig)
497{
498 wxRect rect(rectOrig);
499
500 // Make the edit control large enough to allow for internal
501 // margins
502 //
503 // TODO: remove this if the text ctrl sizing is improved esp. for
504 // unix
505 //
506#if defined(__WXGTK__)
b0e282b3
RR
507 if (rect.x != 0)
508 {
509 rect.x += 1;
510 rect.y += 1;
511 rect.width -= 1;
512 rect.height -= 1;
513 }
99306db2 514#else // !GTK
cb105ad4
SN
515 int extra_x = ( rect.x > 2 )? 2 : 1;
516 int extra_y = ( rect.y > 2 )? 2 : 1;
99306db2 517#if defined(__WXMOTIF__)
cb105ad4
SN
518 extra_x *= 2;
519 extra_y *= 2;
99306db2 520#endif
cb105ad4
SN
521 rect.SetLeft( wxMax(0, rect.x - extra_x) );
522 rect.SetTop( wxMax(0, rect.y - extra_y) );
523 rect.SetRight( rect.GetRight() + 2*extra_x );
524 rect.SetBottom( rect.GetBottom() + 2*extra_y );
99306db2
VZ
525#endif // GTK/!GTK
526
527 wxGridCellEditor::SetSize(rect);
528}
529
3da93aae 530void wxGridCellTextEditor::BeginEdit(int row, int col, wxGrid* grid)
2796cce3
RD
531{
532 wxASSERT_MSG(m_control,
533 wxT("The wxGridCellEditor must be Created first!"));
534
535 m_startValue = grid->GetTable()->GetValue(row, col);
816be743
VZ
536
537 DoBeginEdit(m_startValue);
538}
539
540void wxGridCellTextEditor::DoBeginEdit(const wxString& startValue)
541{
542 Text()->SetValue(startValue);
b54ba671
VZ
543 Text()->SetInsertionPointEnd();
544 Text()->SetFocus();
2796cce3
RD
545}
546
3324d5f5 547bool wxGridCellTextEditor::EndEdit(int row, int col,
3da93aae 548 wxGrid* grid)
2796cce3
RD
549{
550 wxASSERT_MSG(m_control,
551 wxT("The wxGridCellEditor must be Created first!"));
552
553 bool changed = FALSE;
b54ba671 554 wxString value = Text()->GetValue();
2796cce3
RD
555 if (value != m_startValue)
556 changed = TRUE;
557
558 if (changed)
559 grid->GetTable()->SetValue(row, col, value);
2c9a89e0 560
3da93aae 561 m_startValue = wxEmptyString;
b54ba671 562 Text()->SetValue(m_startValue);
2796cce3
RD
563
564 return changed;
565}
566
567
568void wxGridCellTextEditor::Reset()
569{
570 wxASSERT_MSG(m_control,
571 wxT("The wxGridCellEditor must be Created first!"));
572
816be743
VZ
573 DoReset(m_startValue);
574}
575
576void wxGridCellTextEditor::DoReset(const wxString& startValue)
577{
578 Text()->SetValue(startValue);
b54ba671 579 Text()->SetInsertionPointEnd();
2796cce3
RD
580}
581
2c9a89e0
RD
582void wxGridCellTextEditor::StartingKey(wxKeyEvent& event)
583{
b54ba671
VZ
584 if ( !event.AltDown() && !event.MetaDown() && !event.ControlDown() )
585 {
586 // insert the key in the control
587 long keycode = event.KeyCode();
588 if ( isprint(keycode) )
589 {
590 // FIXME this is not going to work for non letters...
591 if ( !event.ShiftDown() )
592 {
593 keycode = tolower(keycode);
594 }
595
596 Text()->AppendText((wxChar)keycode);
597
598 return;
599 }
2c9a89e0 600
2c9a89e0 601 }
2c9a89e0 602
b54ba671
VZ
603 event.Skip();
604}
2c9a89e0 605
2796cce3
RD
606void wxGridCellTextEditor::HandleReturn(wxKeyEvent& event)
607{
608#if defined(__WXMOTIF__) || defined(__WXGTK__)
609 // wxMotif needs a little extra help...
b54ba671
VZ
610 int pos = Text()->GetInsertionPoint();
611 wxString s( Text()->GetValue() );
2796cce3 612 s = s.Left(pos) + "\n" + s.Mid(pos);
b54ba671
VZ
613 Text()->SetValue(s);
614 Text()->SetInsertionPoint( pos );
2796cce3
RD
615#else
616 // the other ports can handle a Return key press
617 //
618 event.Skip();
619#endif
620}
621
c4608a8a
VZ
622void wxGridCellTextEditor::SetParameters(const wxString& params)
623{
624 if ( !params )
625 {
626 // reset to default
627 m_maxChars = 0;
628 }
629 else
630 {
631 long tmp;
632 if ( !params.ToLong(&tmp) )
633 {
634 wxLogDebug(_T("Invalid wxGridCellTextEditor parameter string "
635 "'%s' ignored"), params.c_str());
636 }
637 else
638 {
639 m_maxChars = (size_t)tmp;
640 }
641 }
642}
643
816be743
VZ
644// ----------------------------------------------------------------------------
645// wxGridCellNumberEditor
646// ----------------------------------------------------------------------------
647
648wxGridCellNumberEditor::wxGridCellNumberEditor(int min, int max)
649{
650 m_min = min;
651 m_max = max;
652}
653
654void wxGridCellNumberEditor::Create(wxWindow* parent,
655 wxWindowID id,
656 wxEvtHandler* evtHandler)
657{
658 if ( HasRange() )
659 {
660 // create a spin ctrl
661 m_control = new wxSpinCtrl(parent, -1, wxEmptyString,
662 wxDefaultPosition, wxDefaultSize,
663 wxSP_ARROW_KEYS,
664 m_min, m_max);
665
666 wxGridCellEditor::Create(parent, id, evtHandler);
667 }
668 else
669 {
670 // just a text control
671 wxGridCellTextEditor::Create(parent, id, evtHandler);
672
673#if wxUSE_VALIDATORS
85bc0351 674 Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC));
816be743
VZ
675#endif // wxUSE_VALIDATORS
676 }
677}
678
679void wxGridCellNumberEditor::BeginEdit(int row, int col, wxGrid* grid)
680{
681 // first get the value
682 wxGridTableBase *table = grid->GetTable();
683 if ( table->CanGetValueAs(row, col, wxGRID_VALUE_NUMBER) )
684 {
685 m_valueOld = table->GetValueAsLong(row, col);
686 }
687 else
688 {
a5777624
RD
689 wxString sValue = table->GetValue(row, col);
690 if (! sValue.ToLong(&m_valueOld))
691 {
692 wxFAIL_MSG( _T("this cell doesn't have numeric value") );
693 return;
694 }
816be743
VZ
695 }
696
697 if ( HasRange() )
698 {
699 Spin()->SetValue(m_valueOld);
700 }
701 else
702 {
703 DoBeginEdit(GetString());
704 }
705}
706
3324d5f5 707bool wxGridCellNumberEditor::EndEdit(int row, int col,
816be743
VZ
708 wxGrid* grid)
709{
710 bool changed;
711 long value;
712
713 if ( HasRange() )
714 {
715 value = Spin()->GetValue();
716 changed = value != m_valueOld;
717 }
718 else
719 {
720 changed = Text()->GetValue().ToLong(&value) && (value != m_valueOld);
721 }
722
723 if ( changed )
724 {
a5777624
RD
725 if (grid->GetTable()->CanSetValueAs(row, col, wxGRID_VALUE_NUMBER))
726 grid->GetTable()->SetValueAsLong(row, col, value);
727 else
728 grid->GetTable()->SetValue(row, col, wxString::Format("%ld", value));
816be743
VZ
729 }
730
731 return changed;
732}
733
734void wxGridCellNumberEditor::Reset()
735{
736 if ( HasRange() )
737 {
738 Spin()->SetValue(m_valueOld);
739 }
740 else
741 {
742 DoReset(GetString());
743 }
744}
745
746void wxGridCellNumberEditor::StartingKey(wxKeyEvent& event)
747{
748 if ( !HasRange() )
749 {
750 long keycode = event.KeyCode();
751 if ( isdigit(keycode) || keycode == '+' || keycode == '-' )
752 {
753 wxGridCellTextEditor::StartingKey(event);
754
755 // skip Skip() below
756 return;
757 }
758 }
759
760 event.Skip();
761}
9c4ba614 762
c4608a8a
VZ
763void wxGridCellNumberEditor::SetParameters(const wxString& params)
764{
765 if ( !params )
766 {
767 // reset to default
768 m_min =
769 m_max = -1;
770 }
771 else
772 {
773 long tmp;
774 if ( params.BeforeFirst(_T(',')).ToLong(&tmp) )
775 {
776 m_min = (int)tmp;
777
778 if ( params.AfterFirst(_T(',')).ToLong(&tmp) )
779 {
780 m_max = (int)tmp;
781
782 // skip the error message below
783 return;
784 }
785 }
786
787 wxLogDebug(_T("Invalid wxGridCellNumberEditor parameter string "
788 "'%s' ignored"), params.c_str());
789 }
790}
791
816be743
VZ
792// ----------------------------------------------------------------------------
793// wxGridCellFloatEditor
794// ----------------------------------------------------------------------------
795
796void wxGridCellFloatEditor::Create(wxWindow* parent,
797 wxWindowID id,
798 wxEvtHandler* evtHandler)
799{
800 wxGridCellTextEditor::Create(parent, id, evtHandler);
801
802#if wxUSE_VALIDATORS
85bc0351 803 Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC));
816be743
VZ
804#endif // wxUSE_VALIDATORS
805}
806
807void wxGridCellFloatEditor::BeginEdit(int row, int col, wxGrid* grid)
808{
809 // first get the value
810 wxGridTableBase *table = grid->GetTable();
811 if ( table->CanGetValueAs(row, col, wxGRID_VALUE_FLOAT) )
812 {
813 m_valueOld = table->GetValueAsDouble(row, col);
814 }
815 else
816 {
a5777624
RD
817 wxString sValue = table->GetValue(row, col);
818 if (! sValue.ToDouble(&m_valueOld))
819 {
820 wxFAIL_MSG( _T("this cell doesn't have float value") );
821 return;
822 }
816be743
VZ
823 }
824
825 DoBeginEdit(GetString());
826}
827
3324d5f5 828bool wxGridCellFloatEditor::EndEdit(int row, int col,
816be743
VZ
829 wxGrid* grid)
830{
831 double value;
832 if ( Text()->GetValue().ToDouble(&value) && (value != m_valueOld) )
833 {
a5777624
RD
834 if (grid->GetTable()->CanSetValueAs(row, col, wxGRID_VALUE_FLOAT))
835 grid->GetTable()->SetValueAsDouble(row, col, value);
836 else
837 grid->GetTable()->SetValue(row, col, wxString::Format("%f", value));
816be743
VZ
838
839 return TRUE;
840 }
841 else
842 {
843 return FALSE;
844 }
845}
846
847void wxGridCellFloatEditor::Reset()
848{
849 DoReset(GetString());
850}
851
852void wxGridCellFloatEditor::StartingKey(wxKeyEvent& event)
853{
854 long keycode = event.KeyCode();
855 if ( isdigit(keycode) ||
856 keycode == '+' || keycode == '-' || keycode == '.' )
857 {
858 wxGridCellTextEditor::StartingKey(event);
859
860 // skip Skip() below
861 return;
862 }
863
864 event.Skip();
865}
866
508011ce
VZ
867// ----------------------------------------------------------------------------
868// wxGridCellBoolEditor
869// ----------------------------------------------------------------------------
870
871void wxGridCellBoolEditor::Create(wxWindow* parent,
872 wxWindowID id,
873 wxEvtHandler* evtHandler)
874{
875 m_control = new wxCheckBox(parent, id, wxEmptyString,
876 wxDefaultPosition, wxDefaultSize,
877 wxNO_BORDER);
878
879 wxGridCellEditor::Create(parent, id, evtHandler);
880}
881
882void wxGridCellBoolEditor::SetSize(const wxRect& r)
883{
b94ae1ea
VZ
884 bool resize = FALSE;
885 wxSize size = m_control->GetSize();
886 wxCoord minSize = wxMin(r.width, r.height);
887
888 // check if the checkbox is not too big/small for this cell
889 wxSize sizeBest = m_control->GetBestSize();
890 if ( !(size == sizeBest) )
891 {
892 // reset to default size if it had been made smaller
893 size = sizeBest;
894
895 resize = TRUE;
896 }
897
898 if ( size.x >= minSize || size.y >= minSize )
899 {
900 // leave 1 pixel margin
901 size.x = size.y = minSize - 2;
902
903 resize = TRUE;
904 }
905
906 if ( resize )
907 {
908 m_control->SetSize(size);
909 }
910
508011ce 911 // position it in the centre of the rectangle (TODO: support alignment?)
508011ce 912
b94ae1ea 913#if defined(__WXGTK__) || defined (__WXMOTIF__)
508011ce
VZ
914 // the checkbox without label still has some space to the right in wxGTK,
915 // so shift it to the right
b94ae1ea
VZ
916 size.x -= 8;
917#elif defined(__WXMSW__)
a95e38c0
VZ
918 // here too, but in other way
919 size.x += 1;
b94ae1ea
VZ
920 size.y -= 2;
921#endif
508011ce 922
b94ae1ea 923 m_control->Move(r.x + r.width/2 - size.x/2, r.y + r.height/2 - size.y/2);
508011ce
VZ
924}
925
926void wxGridCellBoolEditor::Show(bool show, wxGridCellAttr *attr)
927{
99306db2
VZ
928 m_control->Show(show);
929
189d0213 930 if ( show )
508011ce 931 {
189d0213
VZ
932 wxColour colBg = attr ? attr->GetBackgroundColour() : *wxLIGHT_GREY;
933 CBox()->SetBackgroundColour(colBg);
508011ce 934 }
508011ce
VZ
935}
936
937void wxGridCellBoolEditor::BeginEdit(int row, int col, wxGrid* grid)
938{
939 wxASSERT_MSG(m_control,
940 wxT("The wxGridCellEditor must be Created first!"));
941
28a77bc4 942 if (grid->GetTable()->CanGetValueAs(row, col, wxGRID_VALUE_BOOL))
f2d76237
RD
943 m_startValue = grid->GetTable()->GetValueAsBool(row, col);
944 else
945 m_startValue = !!grid->GetTable()->GetValue(row, col);
508011ce
VZ
946 CBox()->SetValue(m_startValue);
947 CBox()->SetFocus();
948}
949
950bool wxGridCellBoolEditor::EndEdit(int row, int col,
508011ce
VZ
951 wxGrid* grid)
952{
953 wxASSERT_MSG(m_control,
954 wxT("The wxGridCellEditor must be Created first!"));
955
956 bool changed = FALSE;
957 bool value = CBox()->GetValue();
958 if ( value != m_startValue )
959 changed = TRUE;
960
961 if ( changed )
962 {
28a77bc4 963 if (grid->GetTable()->CanGetValueAs(row, col, wxGRID_VALUE_BOOL))
f2d76237
RD
964 grid->GetTable()->SetValueAsBool(row, col, value);
965 else
966 grid->GetTable()->SetValue(row, col, value ? _T("1") : wxEmptyString);
508011ce
VZ
967 }
968
969 return changed;
970}
971
972void wxGridCellBoolEditor::Reset()
973{
974 wxASSERT_MSG(m_control,
975 wxT("The wxGridCellEditor must be Created first!"));
976
977 CBox()->SetValue(m_startValue);
978}
979
e195a54c 980void wxGridCellBoolEditor::StartingClick()
508011ce 981{
e195a54c 982 CBox()->SetValue(!CBox()->GetValue());
508011ce
VZ
983}
984
4ee5fc9c
VZ
985// ----------------------------------------------------------------------------
986// wxGridCellChoiceEditor
987// ----------------------------------------------------------------------------
988
989wxGridCellChoiceEditor::wxGridCellChoiceEditor(size_t count,
990 const wxChar* choices[],
991 bool allowOthers)
992 : m_allowOthers(allowOthers)
993{
c4608a8a 994 if ( count )
4ee5fc9c 995 {
c4608a8a
VZ
996 m_choices.Alloc(count);
997 for ( size_t n = 0; n < count; n++ )
998 {
999 m_choices.Add(choices[n]);
1000 }
4ee5fc9c
VZ
1001 }
1002}
1003
c4608a8a
VZ
1004wxGridCellEditor *wxGridCellChoiceEditor::Clone() const
1005{
1006 wxGridCellChoiceEditor *editor = new wxGridCellChoiceEditor;
1007 editor->m_allowOthers = m_allowOthers;
1008 editor->m_choices = m_choices;
1009
1010 return editor;
1011}
1012
4ee5fc9c
VZ
1013void wxGridCellChoiceEditor::Create(wxWindow* parent,
1014 wxWindowID id,
1015 wxEvtHandler* evtHandler)
1016{
1017 size_t count = m_choices.GetCount();
1018 wxString *choices = new wxString[count];
1019 for ( size_t n = 0; n < count; n++ )
1020 {
1021 choices[n] = m_choices[n];
1022 }
1023
1024 m_control = new wxComboBox(parent, id, wxEmptyString,
1025 wxDefaultPosition, wxDefaultSize,
1026 count, choices,
1027 m_allowOthers ? 0 : wxCB_READONLY);
1028
1029 delete [] choices;
1030
1031 wxGridCellEditor::Create(parent, id, evtHandler);
1032}
1033
a5777624
RD
1034void wxGridCellChoiceEditor::PaintBackground(const wxRect& rectCell,
1035 wxGridCellAttr * attr)
4ee5fc9c
VZ
1036{
1037 // as we fill the entire client area, don't do anything here to minimize
1038 // flicker
a5777624
RD
1039
1040 // TODO: It doesn't actually fill the client area since the height of a
1041 // combo always defaults to the standard... Until someone has time to
1042 // figure out the right rectangle to paint, just do it the normal way...
1043 wxGridCellEditor::PaintBackground(rectCell, attr);
4ee5fc9c
VZ
1044}
1045
1046void wxGridCellChoiceEditor::BeginEdit(int row, int col, wxGrid* grid)
1047{
1048 wxASSERT_MSG(m_control,
1049 wxT("The wxGridCellEditor must be Created first!"));
1050
1051 m_startValue = grid->GetTable()->GetValue(row, col);
1052
1053 Combo()->SetValue(m_startValue);
28a77bc4
RD
1054 size_t count = m_choices.GetCount();
1055 for (size_t i=0; i<count; i++)
1056 {
1057 if (m_startValue == m_choices[i])
1058 {
1059 Combo()->SetSelection(i);
1060 break;
1061 }
1062 }
4ee5fc9c
VZ
1063 Combo()->SetInsertionPointEnd();
1064 Combo()->SetFocus();
1065}
1066
28a77bc4 1067bool wxGridCellChoiceEditor::EndEdit(int row, int col,
4ee5fc9c
VZ
1068 wxGrid* grid)
1069{
1070 wxString value = Combo()->GetValue();
1071 bool changed = value != m_startValue;
1072
1073 if ( changed )
1074 grid->GetTable()->SetValue(row, col, value);
1075
1076 m_startValue = wxEmptyString;
1077 Combo()->SetValue(m_startValue);
1078
1079 return changed;
1080}
1081
1082void wxGridCellChoiceEditor::Reset()
1083{
1084 Combo()->SetValue(m_startValue);
1085 Combo()->SetInsertionPointEnd();
1086}
1087
c4608a8a
VZ
1088void wxGridCellChoiceEditor::SetParameters(const wxString& params)
1089{
1090 if ( !params )
1091 {
1092 // what can we do?
1093 return;
1094 }
1095
1096 m_choices.Empty();
1097
1098 wxStringTokenizer tk(params, _T(','));
1099 while ( tk.HasMoreTokens() )
1100 {
1101 m_choices.Add(tk.GetNextToken());
1102 }
1103}
1104
508011ce
VZ
1105// ----------------------------------------------------------------------------
1106// wxGridCellEditorEvtHandler
1107// ----------------------------------------------------------------------------
2796cce3
RD
1108
1109void wxGridCellEditorEvtHandler::OnKeyDown(wxKeyEvent& event)
1110{
1111 switch ( event.KeyCode() )
1112 {
1113 case WXK_ESCAPE:
1114 m_editor->Reset();
b54ba671 1115 m_grid->DisableCellEditControl();
2796cce3
RD
1116 break;
1117
2c9a89e0 1118 case WXK_TAB:
9b4aede2
RD
1119 event.Skip( m_grid->ProcessEvent( event ) );
1120 break;
1121
2796cce3
RD
1122 case WXK_RETURN:
1123 if (!m_grid->ProcessEvent(event))
1124 m_editor->HandleReturn(event);
1125 break;
1126
2796cce3
RD
1127
1128 default:
1129 event.Skip();
1130 }
1131}
1132
fb0de762
RD
1133void wxGridCellEditorEvtHandler::OnChar(wxKeyEvent& event)
1134{
1135 switch ( event.KeyCode() )
1136 {
1137 case WXK_ESCAPE:
1138 case WXK_TAB:
1139 case WXK_RETURN:
1140 break;
1141
1142 default:
1143 event.Skip();
1144 }
1145}
1146
c4608a8a
VZ
1147// ----------------------------------------------------------------------------
1148// wxGridCellWorker is an (almost) empty common base class for
1149// wxGridCellRenderer and wxGridCellEditor managing ref counting
1150// ----------------------------------------------------------------------------
1151
1152void wxGridCellWorker::SetParameters(const wxString& WXUNUSED(params))
1153{
1154 // nothing to do
1155}
1156
1157wxGridCellWorker::~wxGridCellWorker()
1158{
1159}
1160
508011ce
VZ
1161// ============================================================================
1162// renderer classes
1163// ============================================================================
1164
ab79958a
VZ
1165// ----------------------------------------------------------------------------
1166// wxGridCellRenderer
1167// ----------------------------------------------------------------------------
1168
1169void wxGridCellRenderer::Draw(wxGrid& grid,
2796cce3 1170 wxGridCellAttr& attr,
ab79958a
VZ
1171 wxDC& dc,
1172 const wxRect& rect,
1173 int row, int col,
1174 bool isSelected)
1175{
1176 dc.SetBackgroundMode( wxSOLID );
1177
1178 if ( isSelected )
1179 {
2796cce3 1180 dc.SetBrush( wxBrush(grid.GetSelectionBackground(), wxSOLID) );
ab79958a
VZ
1181 }
1182 else
1183 {
2796cce3 1184 dc.SetBrush( wxBrush(attr.GetBackgroundColour(), wxSOLID) );
ab79958a
VZ
1185 }
1186
1187 dc.SetPen( *wxTRANSPARENT_PEN );
ab79958a
VZ
1188 dc.DrawRectangle(rect);
1189}
1190
508011ce
VZ
1191// ----------------------------------------------------------------------------
1192// wxGridCellStringRenderer
1193// ----------------------------------------------------------------------------
1194
816be743
VZ
1195void wxGridCellStringRenderer::SetTextColoursAndFont(wxGrid& grid,
1196 wxGridCellAttr& attr,
1197 wxDC& dc,
1198 bool isSelected)
ab79958a 1199{
ab79958a
VZ
1200 dc.SetBackgroundMode( wxTRANSPARENT );
1201
283b7808
VZ
1202 // TODO some special colours for attr.IsReadOnly() case?
1203
ab79958a
VZ
1204 if ( isSelected )
1205 {
2796cce3
RD
1206 dc.SetTextBackground( grid.GetSelectionBackground() );
1207 dc.SetTextForeground( grid.GetSelectionForeground() );
ab79958a
VZ
1208 }
1209 else
1210 {
2796cce3
RD
1211 dc.SetTextBackground( attr.GetBackgroundColour() );
1212 dc.SetTextForeground( attr.GetTextColour() );
ab79958a 1213 }
816be743 1214
2796cce3 1215 dc.SetFont( attr.GetFont() );
816be743
VZ
1216}
1217
65e4e78e
VZ
1218wxSize wxGridCellStringRenderer::DoGetBestSize(wxGridCellAttr& attr,
1219 wxDC& dc,
1220 const wxString& text)
1221{
1222 wxCoord x, y;
1223 dc.SetFont(attr.GetFont());
1224 dc.GetTextExtent(text, &x, &y);
1225
1226 return wxSize(x, y);
1227}
1228
1229wxSize wxGridCellStringRenderer::GetBestSize(wxGrid& grid,
1230 wxGridCellAttr& attr,
1231 wxDC& dc,
1232 int row, int col)
1233{
1234 return DoGetBestSize(attr, dc, grid.GetCellValue(row, col));
1235}
1236
816be743
VZ
1237void wxGridCellStringRenderer::Draw(wxGrid& grid,
1238 wxGridCellAttr& attr,
1239 wxDC& dc,
1240 const wxRect& rectCell,
1241 int row, int col,
1242 bool isSelected)
1243{
1244 wxGridCellRenderer::Draw(grid, attr, dc, rectCell, row, col, isSelected);
1245
1246 // now we only have to draw the text
1247 SetTextColoursAndFont(grid, attr, dc, isSelected);
ab79958a
VZ
1248
1249 int hAlign, vAlign;
2796cce3 1250 attr.GetAlignment(&hAlign, &vAlign);
ab79958a
VZ
1251
1252 wxRect rect = rectCell;
816be743 1253 rect.Inflate(-1);
ab79958a
VZ
1254
1255 grid.DrawTextRectangle(dc, grid.GetCellValue(row, col),
1256 rect, hAlign, vAlign);
1257}
1258
65e4e78e
VZ
1259// ----------------------------------------------------------------------------
1260// wxGridCellNumberRenderer
1261// ----------------------------------------------------------------------------
1262
1263wxString wxGridCellNumberRenderer::GetString(wxGrid& grid, int row, int col)
1264{
1265 wxGridTableBase *table = grid.GetTable();
1266 wxString text;
1267 if ( table->CanGetValueAs(row, col, wxGRID_VALUE_NUMBER) )
1268 {
1269 text.Printf(_T("%ld"), table->GetValueAsLong(row, col));
1270 }
9c4ba614
VZ
1271 else
1272 {
1273 text = table->GetValue(row, col);
1274 }
65e4e78e
VZ
1275
1276 return text;
1277}
1278
816be743
VZ
1279void wxGridCellNumberRenderer::Draw(wxGrid& grid,
1280 wxGridCellAttr& attr,
1281 wxDC& dc,
1282 const wxRect& rectCell,
1283 int row, int col,
1284 bool isSelected)
1285{
1286 wxGridCellRenderer::Draw(grid, attr, dc, rectCell, row, col, isSelected);
1287
1288 SetTextColoursAndFont(grid, attr, dc, isSelected);
1289
1290 // draw the text right aligned by default
1291 int hAlign, vAlign;
1292 attr.GetAlignment(&hAlign, &vAlign);
1293 hAlign = wxRIGHT;
1294
1295 wxRect rect = rectCell;
1296 rect.Inflate(-1);
1297
65e4e78e
VZ
1298 grid.DrawTextRectangle(dc, GetString(grid, row, col), rect, hAlign, vAlign);
1299}
816be743 1300
65e4e78e
VZ
1301wxSize wxGridCellNumberRenderer::GetBestSize(wxGrid& grid,
1302 wxGridCellAttr& attr,
1303 wxDC& dc,
1304 int row, int col)
1305{
1306 return DoGetBestSize(attr, dc, GetString(grid, row, col));
816be743
VZ
1307}
1308
1309// ----------------------------------------------------------------------------
1310// wxGridCellFloatRenderer
1311// ----------------------------------------------------------------------------
1312
1313wxGridCellFloatRenderer::wxGridCellFloatRenderer(int width, int precision)
1314{
1315 SetWidth(width);
1316 SetPrecision(precision);
1317}
1318
e72b4213
VZ
1319wxGridCellRenderer *wxGridCellFloatRenderer::Clone() const
1320{
1321 wxGridCellFloatRenderer *renderer = new wxGridCellFloatRenderer;
1322 renderer->m_width = m_width;
1323 renderer->m_precision = m_precision;
1324 renderer->m_format = m_format;
1325
1326 return renderer;
1327}
1328
65e4e78e
VZ
1329wxString wxGridCellFloatRenderer::GetString(wxGrid& grid, int row, int col)
1330{
1331 wxGridTableBase *table = grid.GetTable();
0b190b0f
VZ
1332
1333 bool hasDouble;
1334 double val;
65e4e78e
VZ
1335 wxString text;
1336 if ( table->CanGetValueAs(row, col, wxGRID_VALUE_FLOAT) )
1337 {
0b190b0f
VZ
1338 val = table->GetValueAsDouble(row, col);
1339 hasDouble = TRUE;
65e4e78e 1340 }
9c4ba614
VZ
1341 else
1342 {
1343 text = table->GetValue(row, col);
0b190b0f 1344 hasDouble = text.ToDouble(&val);
9c4ba614 1345 }
65e4e78e 1346
0b190b0f
VZ
1347 if ( hasDouble )
1348 {
1349 if ( !m_format )
1350 {
1351 if ( m_width == -1 )
1352 {
1353 // default width/precision
1354 m_format = _T("%f");
1355 }
1356 else if ( m_precision == -1 )
1357 {
1358 // default precision
1359 m_format.Printf(_T("%%%d.f"), m_width);
1360 }
1361 else
1362 {
1363 m_format.Printf(_T("%%%d.%df"), m_width, m_precision);
1364 }
1365 }
1366
1367 text.Printf(m_format, val);
1368 }
1369 //else: text already contains the string
1370
65e4e78e
VZ
1371 return text;
1372}
1373
816be743
VZ
1374void wxGridCellFloatRenderer::Draw(wxGrid& grid,
1375 wxGridCellAttr& attr,
1376 wxDC& dc,
1377 const wxRect& rectCell,
1378 int row, int col,
1379 bool isSelected)
1380{
1381 wxGridCellRenderer::Draw(grid, attr, dc, rectCell, row, col, isSelected);
1382
1383 SetTextColoursAndFont(grid, attr, dc, isSelected);
1384
1385 // draw the text right aligned by default
1386 int hAlign, vAlign;
1387 attr.GetAlignment(&hAlign, &vAlign);
1388 hAlign = wxRIGHT;
1389
1390 wxRect rect = rectCell;
1391 rect.Inflate(-1);
1392
65e4e78e
VZ
1393 grid.DrawTextRectangle(dc, GetString(grid, row, col), rect, hAlign, vAlign);
1394}
816be743 1395
65e4e78e
VZ
1396wxSize wxGridCellFloatRenderer::GetBestSize(wxGrid& grid,
1397 wxGridCellAttr& attr,
1398 wxDC& dc,
1399 int row, int col)
1400{
1401 return DoGetBestSize(attr, dc, GetString(grid, row, col));
816be743
VZ
1402}
1403
0b190b0f
VZ
1404void wxGridCellFloatRenderer::SetParameters(const wxString& params)
1405{
1406 bool ok = TRUE;
1407
1408 if ( !params )
1409 {
1410 // reset to defaults
1411 SetWidth(-1);
1412 SetPrecision(-1);
1413 }
1414 else
1415 {
1416 wxString tmp = params.BeforeFirst(_T(','));
1417 if ( !!tmp )
1418 {
1419 long width;
1420 if ( !tmp.ToLong(&width) )
1421 {
1422 ok = FALSE;
1423 }
1424 else
1425 {
1426 SetWidth((int)width);
1427
1428 tmp = params.AfterFirst(_T(','));
1429 if ( !!tmp )
1430 {
1431 long precision;
1432 if ( !tmp.ToLong(&precision) )
1433 {
1434 ok = FALSE;
1435 }
1436 else
1437 {
1438 SetPrecision((int)precision);
1439 }
1440 }
1441 }
1442 }
1443
1444 if ( !ok )
1445 {
1446 wxLogDebug(_T("Invalid wxGridCellFloatRenderer parameter string "
1447 "'%s ignored"), params.c_str());
1448 }
1449 }
1450}
1451
508011ce
VZ
1452// ----------------------------------------------------------------------------
1453// wxGridCellBoolRenderer
1454// ----------------------------------------------------------------------------
1455
65e4e78e 1456wxSize wxGridCellBoolRenderer::ms_sizeCheckMark;
508011ce 1457
b94ae1ea
VZ
1458// FIXME these checkbox size calculations are really ugly...
1459
65e4e78e 1460// between checkmark and box
a95e38c0 1461static const wxCoord wxGRID_CHECKMARK_MARGIN = 2;
508011ce 1462
65e4e78e
VZ
1463wxSize wxGridCellBoolRenderer::GetBestSize(wxGrid& grid,
1464 wxGridCellAttr& WXUNUSED(attr),
1465 wxDC& WXUNUSED(dc),
1466 int WXUNUSED(row),
1467 int WXUNUSED(col))
1468{
1469 // compute it only once (no locks for MT safeness in GUI thread...)
1470 if ( !ms_sizeCheckMark.x )
297da4ba 1471 {
65e4e78e
VZ
1472 // get checkbox size
1473 wxCoord checkSize = 0;
297da4ba
VZ
1474 wxCheckBox *checkbox = new wxCheckBox(&grid, -1, wxEmptyString);
1475 wxSize size = checkbox->GetBestSize();
a95e38c0 1476 checkSize = size.y + 2*wxGRID_CHECKMARK_MARGIN;
297da4ba 1477
65e4e78e 1478 // FIXME wxGTK::wxCheckBox::GetBestSize() gives "wrong" result
69d8f612 1479#if defined(__WXGTK__) || defined(__WXMOTIF__)
65e4e78e 1480 checkSize -= size.y / 2;
297da4ba
VZ
1481#endif
1482
1483 delete checkbox;
65e4e78e
VZ
1484
1485 ms_sizeCheckMark.x = ms_sizeCheckMark.y = checkSize;
297da4ba
VZ
1486 }
1487
65e4e78e
VZ
1488 return ms_sizeCheckMark;
1489}
1490
1491void wxGridCellBoolRenderer::Draw(wxGrid& grid,
1492 wxGridCellAttr& attr,
1493 wxDC& dc,
1494 const wxRect& rect,
1495 int row, int col,
1496 bool isSelected)
1497{
1498 wxGridCellRenderer::Draw(grid, attr, dc, rect, row, col, isSelected);
1499
297da4ba 1500 // draw a check mark in the centre (ignoring alignment - TODO)
65e4e78e 1501 wxSize size = GetBestSize(grid, attr, dc, row, col);
b94ae1ea
VZ
1502
1503 // don't draw outside the cell
1504 wxCoord minSize = wxMin(rect.width, rect.height);
1505 if ( size.x >= minSize || size.y >= minSize )
1506 {
1507 // and even leave (at least) 1 pixel margin
1508 size.x = size.y = minSize - 2;
1509 }
1510
1511 // draw a border around checkmark
a95e38c0
VZ
1512 wxRect rectBorder;
1513 rectBorder.x = rect.x + rect.width/2 - size.x/2;
1514 rectBorder.y = rect.y + rect.height/2 - size.y/2;
1515 rectBorder.width = size.x;
1516 rectBorder.height = size.y;
b94ae1ea 1517
f2d76237 1518 bool value;
b94ae1ea 1519 if ( grid.GetTable()->CanGetValueAs(row, col, wxGRID_VALUE_BOOL) )
f2d76237
RD
1520 value = grid.GetTable()->GetValueAsBool(row, col);
1521 else
1522 value = !!grid.GetTable()->GetValue(row, col);
1523
1524 if ( value )
508011ce 1525 {
a95e38c0
VZ
1526 wxRect rectMark = rectBorder;
1527#ifdef __WXMSW__
1528 // MSW DrawCheckMark() is weird (and should probably be changed...)
1529 rectMark.Inflate(-wxGRID_CHECKMARK_MARGIN/2);
1530 rectMark.x++;
1531 rectMark.y++;
1532#else // !MSW
1533 rectMark.Inflate(-wxGRID_CHECKMARK_MARGIN);
1534#endif // MSW/!MSW
1535
508011ce
VZ
1536 dc.SetTextForeground(attr.GetTextColour());
1537 dc.DrawCheckMark(rectMark);
1538 }
a95e38c0
VZ
1539
1540 dc.SetBrush(*wxTRANSPARENT_BRUSH);
1541 dc.SetPen(wxPen(attr.GetTextColour(), 1, wxSOLID));
1542 dc.DrawRectangle(rectBorder);
508011ce
VZ
1543}
1544
2796cce3
RD
1545// ----------------------------------------------------------------------------
1546// wxGridCellAttr
1547// ----------------------------------------------------------------------------
1548
39bcce60 1549wxGridCellAttr *wxGridCellAttr::Clone() const
a68c1246
VZ
1550{
1551 wxGridCellAttr *attr = new wxGridCellAttr;
1552 if ( HasTextColour() )
1553 attr->SetTextColour(GetTextColour());
1554 if ( HasBackgroundColour() )
1555 attr->SetBackgroundColour(GetBackgroundColour());
1556 if ( HasFont() )
1557 attr->SetFont(GetFont());
1558 if ( HasAlignment() )
1559 attr->SetAlignment(m_hAlign, m_vAlign);
1560
1561 if ( m_renderer )
1562 {
1563 attr->SetRenderer(m_renderer);
39bcce60 1564 m_renderer->IncRef();
a68c1246
VZ
1565 }
1566 if ( m_editor )
1567 {
1568 attr->SetEditor(m_editor);
39bcce60 1569 m_editor->IncRef();
a68c1246
VZ
1570 }
1571
1572 if ( IsReadOnly() )
1573 attr->SetReadOnly();
1574
1575 attr->SetDefAttr(m_defGridAttr);
1576
1577 return attr;
1578}
1579
2796cce3
RD
1580const wxColour& wxGridCellAttr::GetTextColour() const
1581{
1582 if (HasTextColour())
508011ce 1583 {
2796cce3 1584 return m_colText;
508011ce 1585 }
2796cce3 1586 else if (m_defGridAttr != this)
508011ce 1587 {
2796cce3 1588 return m_defGridAttr->GetTextColour();
508011ce
VZ
1589 }
1590 else
1591 {
2796cce3
RD
1592 wxFAIL_MSG(wxT("Missing default cell attribute"));
1593 return wxNullColour;
1594 }
1595}
1596
1597
1598const wxColour& wxGridCellAttr::GetBackgroundColour() const
1599{
1600 if (HasBackgroundColour())
1601 return m_colBack;
1602 else if (m_defGridAttr != this)
1603 return m_defGridAttr->GetBackgroundColour();
508011ce
VZ
1604 else
1605 {
2796cce3
RD
1606 wxFAIL_MSG(wxT("Missing default cell attribute"));
1607 return wxNullColour;
1608 }
1609}
1610
1611
1612const wxFont& wxGridCellAttr::GetFont() const
1613{
1614 if (HasFont())
1615 return m_font;
1616 else if (m_defGridAttr != this)
1617 return m_defGridAttr->GetFont();
508011ce
VZ
1618 else
1619 {
2796cce3
RD
1620 wxFAIL_MSG(wxT("Missing default cell attribute"));
1621 return wxNullFont;
1622 }
1623}
1624
1625
1626void wxGridCellAttr::GetAlignment(int *hAlign, int *vAlign) const
1627{
508011ce
VZ
1628 if (HasAlignment())
1629 {
2796cce3
RD
1630 if ( hAlign ) *hAlign = m_hAlign;
1631 if ( vAlign ) *vAlign = m_vAlign;
1632 }
1633 else if (m_defGridAttr != this)
1634 m_defGridAttr->GetAlignment(hAlign, vAlign);
508011ce
VZ
1635 else
1636 {
2796cce3
RD
1637 wxFAIL_MSG(wxT("Missing default cell attribute"));
1638 }
1639}
1640
1641
f2d76237 1642// GetRenderer and GetEditor use a slightly different decision path about
28a77bc4
RD
1643// which attribute to use. If a non-default attr object has one then it is
1644// used, otherwise the default editor or renderer is fetched from the grid and
1645// used. It should be the default for the data type of the cell. If it is
1646// NULL (because the table has a type that the grid does not have in its
1647// registry,) then the grid's default editor or renderer is used.
1648
1649wxGridCellRenderer* wxGridCellAttr::GetRenderer(wxGrid* grid, int row, int col) const
1650{
28a77bc4 1651 wxGridCellRenderer* renderer = NULL;
28a77bc4 1652
0b190b0f
VZ
1653 if ( m_defGridAttr != this || grid == NULL )
1654 {
1655 renderer = m_renderer; // use local attribute
1656 if ( renderer )
1657 renderer->IncRef();
1658 }
1659
1660 if ( !renderer && grid ) // get renderer for the data type
1661 {
1662 // GetDefaultRendererForCell() will do IncRef() for us
1663 renderer = grid->GetDefaultRendererForCell(row, col);
1664 }
1665
1666 if ( !renderer )
1667 {
28a77bc4 1668 // if we still don't have one then use the grid default
0b190b0f 1669 // (no need for IncRef() here neither)
28a77bc4 1670 renderer = m_defGridAttr->GetRenderer(NULL,0,0);
0b190b0f 1671 }
28a77bc4 1672
0b190b0f
VZ
1673 if ( !renderer)
1674 {
2796cce3 1675 wxFAIL_MSG(wxT("Missing default cell attribute"));
0b190b0f 1676 }
28a77bc4
RD
1677
1678 return renderer;
2796cce3
RD
1679}
1680
28a77bc4 1681wxGridCellEditor* wxGridCellAttr::GetEditor(wxGrid* grid, int row, int col) const
07296f0b 1682{
28a77bc4 1683 wxGridCellEditor* editor = NULL;
0b190b0f
VZ
1684
1685 if ( m_defGridAttr != this || grid == NULL )
1686 {
1687 editor = m_editor; // use local attribute
1688 if ( editor )
1689 editor->IncRef();
1690 }
1691
a95e38c0
VZ
1692 if ( !editor && grid ) // get renderer for the data type
1693 editor = grid->GetDefaultEditorForCell(row, col);
28a77bc4 1694
0b190b0f 1695 if ( !editor )
28a77bc4
RD
1696 // if we still don't have one then use the grid default
1697 editor = m_defGridAttr->GetEditor(NULL,0,0);
1698
0b190b0f
VZ
1699 if ( !editor )
1700 {
07296f0b 1701 wxFAIL_MSG(wxT("Missing default cell attribute"));
0b190b0f 1702 }
28a77bc4
RD
1703
1704 return editor;
07296f0b
RD
1705}
1706
b99be8fb 1707// ----------------------------------------------------------------------------
758cbedf 1708// wxGridCellAttrData
b99be8fb
VZ
1709// ----------------------------------------------------------------------------
1710
758cbedf 1711void wxGridCellAttrData::SetAttr(wxGridCellAttr *attr, int row, int col)
b99be8fb
VZ
1712{
1713 int n = FindIndex(row, col);
1714 if ( n == wxNOT_FOUND )
1715 {
1716 // add the attribute
1717 m_attrs.Add(new wxGridCellWithAttr(row, col, attr));
1718 }
1719 else
1720 {
1721 if ( attr )
1722 {
1723 // change the attribute
2e9a6788 1724 m_attrs[(size_t)n].attr = attr;
b99be8fb
VZ
1725 }
1726 else
1727 {
1728 // remove this attribute
1729 m_attrs.RemoveAt((size_t)n);
1730 }
1731 }
b99be8fb
VZ
1732}
1733
758cbedf 1734wxGridCellAttr *wxGridCellAttrData::GetAttr(int row, int col) const
b99be8fb
VZ
1735{
1736 wxGridCellAttr *attr = (wxGridCellAttr *)NULL;
1737
1738 int n = FindIndex(row, col);
1739 if ( n != wxNOT_FOUND )
1740 {
2e9a6788
VZ
1741 attr = m_attrs[(size_t)n].attr;
1742 attr->IncRef();
b99be8fb
VZ
1743 }
1744
1745 return attr;
1746}
1747
4d60017a
SN
1748void wxGridCellAttrData::UpdateAttrRows( size_t pos, int numRows )
1749{
1750 size_t count = m_attrs.GetCount();
1751 for ( size_t n = 0; n < count; n++ )
1752 {
1753 wxGridCellCoords& coords = m_attrs[n].coords;
d1c0b4f9
VZ
1754 wxCoord row = coords.GetRow();
1755 if ((size_t)row >= pos)
1756 {
1757 if (numRows > 0)
1758 {
1759 // If rows inserted, include row counter where necessary
1760 coords.SetRow(row + numRows);
1761 }
1762 else if (numRows < 0)
1763 {
1764 // If rows deleted ...
1765 if ((size_t)row >= pos - numRows)
1766 {
1767 // ...either decrement row counter (if row still exists)...
1768 coords.SetRow(row + numRows);
1769 }
1770 else
1771 {
1772 // ...or remove the attribute
1773 m_attrs.RemoveAt((size_t)n);
1774 n--; count--;
1775 }
1776 }
4d60017a
SN
1777 }
1778 }
1779}
1780
1781void wxGridCellAttrData::UpdateAttrCols( size_t pos, int numCols )
1782{
1783 size_t count = m_attrs.GetCount();
1784 for ( size_t n = 0; n < count; n++ )
1785 {
1786 wxGridCellCoords& coords = m_attrs[n].coords;
d1c0b4f9
VZ
1787 wxCoord col = coords.GetCol();
1788 if ( (size_t)col >= pos )
1789 {
1790 if ( numCols > 0 )
1791 {
1792 // If rows inserted, include row counter where necessary
1793 coords.SetCol(col + numCols);
1794 }
1795 else if (numCols < 0)
1796 {
1797 // If rows deleted ...
1798 if ((size_t)col >= pos - numCols)
1799 {
1800 // ...either decrement row counter (if row still exists)...
1801 coords.SetCol(col + numCols);
1802 }
1803 else
1804 {
1805 // ...or remove the attribute
1806 m_attrs.RemoveAt((size_t)n);
1807 n--; count--;
1808 }
1809 }
4d60017a
SN
1810 }
1811 }
1812}
1813
758cbedf 1814int wxGridCellAttrData::FindIndex(int row, int col) const
b99be8fb
VZ
1815{
1816 size_t count = m_attrs.GetCount();
1817 for ( size_t n = 0; n < count; n++ )
1818 {
1819 const wxGridCellCoords& coords = m_attrs[n].coords;
1820 if ( (coords.GetRow() == row) && (coords.GetCol() == col) )
1821 {
1822 return n;
1823 }
1824 }
1825
1826 return wxNOT_FOUND;
1827}
1828
758cbedf
VZ
1829// ----------------------------------------------------------------------------
1830// wxGridRowOrColAttrData
1831// ----------------------------------------------------------------------------
1832
1833wxGridRowOrColAttrData::~wxGridRowOrColAttrData()
1834{
1835 size_t count = m_attrs.Count();
1836 for ( size_t n = 0; n < count; n++ )
1837 {
1838 m_attrs[n]->DecRef();
1839 }
1840}
1841
1842wxGridCellAttr *wxGridRowOrColAttrData::GetAttr(int rowOrCol) const
1843{
1844 wxGridCellAttr *attr = (wxGridCellAttr *)NULL;
1845
1846 int n = m_rowsOrCols.Index(rowOrCol);
1847 if ( n != wxNOT_FOUND )
1848 {
1849 attr = m_attrs[(size_t)n];
1850 attr->IncRef();
1851 }
1852
1853 return attr;
1854}
1855
1856void wxGridRowOrColAttrData::SetAttr(wxGridCellAttr *attr, int rowOrCol)
1857{
a95e38c0
VZ
1858 int i = m_rowsOrCols.Index(rowOrCol);
1859 if ( i == wxNOT_FOUND )
758cbedf
VZ
1860 {
1861 // add the attribute
1862 m_rowsOrCols.Add(rowOrCol);
1863 m_attrs.Add(attr);
1864 }
1865 else
1866 {
a95e38c0 1867 size_t n = (size_t)i;
758cbedf
VZ
1868 if ( attr )
1869 {
1870 // change the attribute
a95e38c0
VZ
1871 m_attrs[n]->DecRef();
1872 m_attrs[n] = attr;
758cbedf
VZ
1873 }
1874 else
1875 {
1876 // remove this attribute
a95e38c0
VZ
1877 m_attrs[n]->DecRef();
1878 m_rowsOrCols.RemoveAt(n);
1879 m_attrs.RemoveAt(n);
758cbedf
VZ
1880 }
1881 }
1882}
1883
4d60017a
SN
1884void wxGridRowOrColAttrData::UpdateAttrRowsOrCols( size_t pos, int numRowsOrCols )
1885{
1886 size_t count = m_attrs.GetCount();
1887 for ( size_t n = 0; n < count; n++ )
1888 {
1889 int & rowOrCol = m_rowsOrCols[n];
d1c0b4f9
VZ
1890 if ( (size_t)rowOrCol >= pos )
1891 {
1892 if ( numRowsOrCols > 0 )
1893 {
1894 // If rows inserted, include row counter where necessary
1895 rowOrCol += numRowsOrCols;
1896 }
1897 else if ( numRowsOrCols < 0)
1898 {
1899 // If rows deleted, either decrement row counter (if row still exists)
1900 if ((size_t)rowOrCol >= pos - numRowsOrCols)
1901 rowOrCol += numRowsOrCols;
1902 else
1903 {
1904 m_rowsOrCols.RemoveAt((size_t)n);
1905 m_attrs.RemoveAt((size_t)n);
1906 n--; count--;
1907 }
1908 }
4d60017a
SN
1909 }
1910 }
1911}
1912
b99be8fb
VZ
1913// ----------------------------------------------------------------------------
1914// wxGridCellAttrProvider
1915// ----------------------------------------------------------------------------
1916
1917wxGridCellAttrProvider::wxGridCellAttrProvider()
1918{
1919 m_data = (wxGridCellAttrProviderData *)NULL;
1920}
1921
1922wxGridCellAttrProvider::~wxGridCellAttrProvider()
1923{
1924 delete m_data;
1925}
1926
1927void wxGridCellAttrProvider::InitData()
1928{
1929 m_data = new wxGridCellAttrProviderData;
1930}
1931
1932wxGridCellAttr *wxGridCellAttrProvider::GetAttr(int row, int col) const
1933{
758cbedf
VZ
1934 wxGridCellAttr *attr = (wxGridCellAttr *)NULL;
1935 if ( m_data )
1936 {
1937 // first look for the attribute of this specific cell
1938 attr = m_data->m_cellAttrs.GetAttr(row, col);
1939
1940 if ( !attr )
1941 {
1942 // then look for the col attr (col attributes are more common than
1943 // the row ones, hence they have priority)
1944 attr = m_data->m_colAttrs.GetAttr(col);
1945 }
1946
1947 if ( !attr )
1948 {
1949 // finally try the row attributes
1950 attr = m_data->m_rowAttrs.GetAttr(row);
1951 }
1952 }
1953
1954 return attr;
b99be8fb
VZ
1955}
1956
2e9a6788 1957void wxGridCellAttrProvider::SetAttr(wxGridCellAttr *attr,
b99be8fb
VZ
1958 int row, int col)
1959{
1960 if ( !m_data )
1961 InitData();
1962
758cbedf
VZ
1963 m_data->m_cellAttrs.SetAttr(attr, row, col);
1964}
1965
1966void wxGridCellAttrProvider::SetRowAttr(wxGridCellAttr *attr, int row)
1967{
1968 if ( !m_data )
1969 InitData();
1970
1971 m_data->m_rowAttrs.SetAttr(attr, row);
1972}
1973
1974void wxGridCellAttrProvider::SetColAttr(wxGridCellAttr *attr, int col)
1975{
1976 if ( !m_data )
1977 InitData();
1978
1979 m_data->m_colAttrs.SetAttr(attr, col);
b99be8fb
VZ
1980}
1981
4d60017a
SN
1982void wxGridCellAttrProvider::UpdateAttrRows( size_t pos, int numRows )
1983{
1984 if ( m_data )
1985 {
1986 m_data->m_cellAttrs.UpdateAttrRows( pos, numRows );
1987
d1c0b4f9 1988 m_data->m_rowAttrs.UpdateAttrRowsOrCols( pos, numRows );
4d60017a
SN
1989 }
1990}
1991
1992void wxGridCellAttrProvider::UpdateAttrCols( size_t pos, int numCols )
1993{
1994 if ( m_data )
1995 {
1996 m_data->m_cellAttrs.UpdateAttrCols( pos, numCols );
1997
d1c0b4f9 1998 m_data->m_colAttrs.UpdateAttrRowsOrCols( pos, numCols );
4d60017a
SN
1999 }
2000}
2001
f2d76237
RD
2002// ----------------------------------------------------------------------------
2003// wxGridTypeRegistry
2004// ----------------------------------------------------------------------------
2005
2006wxGridTypeRegistry::~wxGridTypeRegistry()
2007{
b94ae1ea
VZ
2008 size_t count = m_typeinfo.Count();
2009 for ( size_t i = 0; i < count; i++ )
f2d76237
RD
2010 delete m_typeinfo[i];
2011}
2012
2013
2014void wxGridTypeRegistry::RegisterDataType(const wxString& typeName,
2015 wxGridCellRenderer* renderer,
2016 wxGridCellEditor* editor)
2017{
f2d76237
RD
2018 wxGridDataTypeInfo* info = new wxGridDataTypeInfo(typeName, renderer, editor);
2019
2020 // is it already registered?
c4608a8a 2021 int loc = FindRegisteredDataType(typeName);
39bcce60
VZ
2022 if ( loc != wxNOT_FOUND )
2023 {
f2d76237
RD
2024 delete m_typeinfo[loc];
2025 m_typeinfo[loc] = info;
2026 }
39bcce60
VZ
2027 else
2028 {
f2d76237
RD
2029 m_typeinfo.Add(info);
2030 }
2031}
2032
c4608a8a
VZ
2033int wxGridTypeRegistry::FindRegisteredDataType(const wxString& typeName)
2034{
2035 size_t count = m_typeinfo.GetCount();
2036 for ( size_t i = 0; i < count; i++ )
2037 {
2038 if ( typeName == m_typeinfo[i]->m_typeName )
2039 {
2040 return i;
2041 }
2042 }
2043
2044 return wxNOT_FOUND;
2045}
2046
f2d76237
RD
2047int wxGridTypeRegistry::FindDataType(const wxString& typeName)
2048{
c4608a8a
VZ
2049 int index = FindRegisteredDataType(typeName);
2050 if ( index == wxNOT_FOUND )
2051 {
2052 // check whether this is one of the standard ones, in which case
2053 // register it "on the fly"
2054 if ( typeName == wxGRID_VALUE_STRING )
2055 {
2056 RegisterDataType(wxGRID_VALUE_STRING,
2057 new wxGridCellStringRenderer,
2058 new wxGridCellTextEditor);
2059 }
2060 else if ( typeName == wxGRID_VALUE_BOOL )
2061 {
2062 RegisterDataType(wxGRID_VALUE_BOOL,
2063 new wxGridCellBoolRenderer,
2064 new wxGridCellBoolEditor);
2065 }
2066 else if ( typeName == wxGRID_VALUE_NUMBER )
2067 {
2068 RegisterDataType(wxGRID_VALUE_NUMBER,
2069 new wxGridCellNumberRenderer,
2070 new wxGridCellNumberEditor);
2071 }
2072 else if ( typeName == wxGRID_VALUE_FLOAT )
2073 {
2074 RegisterDataType(wxGRID_VALUE_FLOAT,
2075 new wxGridCellFloatRenderer,
2076 new wxGridCellFloatEditor);
2077 }
2078 else if ( typeName == wxGRID_VALUE_CHOICE )
2079 {
2080 RegisterDataType(wxGRID_VALUE_CHOICE,
2081 new wxGridCellStringRenderer,
2082 new wxGridCellChoiceEditor);
2083 }
2084 else
2085 {
2086 return wxNOT_FOUND;
2087 }
f2d76237 2088
c4608a8a
VZ
2089 // we get here only if just added the entry for this type, so return
2090 // the last index
2091 index = m_typeinfo.GetCount() - 1;
2092 }
2093
2094 return index;
2095}
2096
2097int wxGridTypeRegistry::FindOrCloneDataType(const wxString& typeName)
2098{
2099 int index = FindDataType(typeName);
2100 if ( index == wxNOT_FOUND )
2101 {
2102 // the first part of the typename is the "real" type, anything after ':'
2103 // are the parameters for the renderer
2104 index = FindDataType(typeName.BeforeFirst(_T(':')));
2105 if ( index == wxNOT_FOUND )
2106 {
2107 return wxNOT_FOUND;
f2d76237 2108 }
c4608a8a
VZ
2109
2110 wxGridCellRenderer *renderer = GetRenderer(index);
2111 wxGridCellRenderer *rendererOld = renderer;
2112 renderer = renderer->Clone();
2113 rendererOld->DecRef();
2114
2115 wxGridCellEditor *editor = GetEditor(index);
2116 wxGridCellEditor *editorOld = editor;
2117 editor = editor->Clone();
2118 editorOld->DecRef();
2119
2120 // do it even if there are no parameters to reset them to defaults
2121 wxString params = typeName.AfterFirst(_T(':'));
2122 renderer->SetParameters(params);
2123 editor->SetParameters(params);
2124
2125 // register the new typename
c4608a8a
VZ
2126 RegisterDataType(typeName, renderer, editor);
2127
2128 // we just registered it, it's the last one
2129 index = m_typeinfo.GetCount() - 1;
f2d76237
RD
2130 }
2131
c4608a8a 2132 return index;
f2d76237
RD
2133}
2134
2135wxGridCellRenderer* wxGridTypeRegistry::GetRenderer(int index)
2136{
2137 wxGridCellRenderer* renderer = m_typeinfo[index]->m_renderer;
0b190b0f 2138 renderer->IncRef();
f2d76237
RD
2139 return renderer;
2140}
2141
0b190b0f 2142wxGridCellEditor* wxGridTypeRegistry::GetEditor(int index)
f2d76237
RD
2143{
2144 wxGridCellEditor* editor = m_typeinfo[index]->m_editor;
0b190b0f 2145 editor->IncRef();
f2d76237
RD
2146 return editor;
2147}
2148
758cbedf
VZ
2149// ----------------------------------------------------------------------------
2150// wxGridTableBase
2151// ----------------------------------------------------------------------------
2152
f85afd4e
MB
2153IMPLEMENT_ABSTRACT_CLASS( wxGridTableBase, wxObject )
2154
2155
2156wxGridTableBase::wxGridTableBase()
f85afd4e
MB
2157{
2158 m_view = (wxGrid *) NULL;
b99be8fb 2159 m_attrProvider = (wxGridCellAttrProvider *) NULL;
f85afd4e
MB
2160}
2161
2162wxGridTableBase::~wxGridTableBase()
2163{
b99be8fb
VZ
2164 delete m_attrProvider;
2165}
2166
2167void wxGridTableBase::SetAttrProvider(wxGridCellAttrProvider *attrProvider)
2168{
2169 delete m_attrProvider;
2170 m_attrProvider = attrProvider;
f85afd4e
MB
2171}
2172
f2d76237
RD
2173bool wxGridTableBase::CanHaveAttributes()
2174{
2175 if ( ! GetAttrProvider() )
2176 {
2177 // use the default attr provider by default
2178 SetAttrProvider(new wxGridCellAttrProvider);
2179 }
2180 return TRUE;
2181}
2182
b99be8fb
VZ
2183wxGridCellAttr *wxGridTableBase::GetAttr(int row, int col)
2184{
2185 if ( m_attrProvider )
2186 return m_attrProvider->GetAttr(row, col);
2187 else
2188 return (wxGridCellAttr *)NULL;
2189}
2190
758cbedf 2191void wxGridTableBase::SetAttr(wxGridCellAttr* attr, int row, int col)
b99be8fb
VZ
2192{
2193 if ( m_attrProvider )
2194 {
2195 m_attrProvider->SetAttr(attr, row, col);
2196 }
2197 else
2198 {
2199 // as we take ownership of the pointer and don't store it, we must
2200 // free it now
39bcce60 2201 wxSafeDecRef(attr);
b99be8fb
VZ
2202 }
2203}
2204
758cbedf
VZ
2205void wxGridTableBase::SetRowAttr(wxGridCellAttr *attr, int row)
2206{
2207 if ( m_attrProvider )
2208 {
2209 m_attrProvider->SetRowAttr(attr, row);
2210 }
2211 else
2212 {
2213 // as we take ownership of the pointer and don't store it, we must
2214 // free it now
39bcce60 2215 wxSafeDecRef(attr);
758cbedf
VZ
2216 }
2217}
2218
2219void wxGridTableBase::SetColAttr(wxGridCellAttr *attr, int col)
2220{
2221 if ( m_attrProvider )
2222 {
2223 m_attrProvider->SetColAttr(attr, col);
2224 }
2225 else
2226 {
2227 // as we take ownership of the pointer and don't store it, we must
2228 // free it now
39bcce60 2229 wxSafeDecRef(attr);
758cbedf
VZ
2230 }
2231}
2232
4d60017a
SN
2233void wxGridTableBase::UpdateAttrRows( size_t pos, int numRows )
2234{
2235 if ( m_attrProvider )
2236 {
2237 m_attrProvider->UpdateAttrRows( pos, numRows );
2238 }
2239}
2240
2241void wxGridTableBase::UpdateAttrCols( size_t pos, int numCols )
2242{
2243 if ( m_attrProvider )
2244 {
2245 m_attrProvider->UpdateAttrCols( pos, numCols );
2246 }
2247}
2248
f85afd4e
MB
2249bool wxGridTableBase::InsertRows( size_t pos, size_t numRows )
2250{
bd3c6758
MB
2251 wxFAIL_MSG( wxT("Called grid table class function InsertRows\n"
2252 "but your derived table class does not override this function") );
8f177c8e 2253
f85afd4e
MB
2254 return FALSE;
2255}
2256
2257bool wxGridTableBase::AppendRows( size_t numRows )
2258{
bd3c6758
MB
2259 wxFAIL_MSG( wxT("Called grid table class function AppendRows\n"
2260 "but your derived table class does not override this function"));
8f177c8e 2261
f85afd4e
MB
2262 return FALSE;
2263}
2264
2265bool wxGridTableBase::DeleteRows( size_t pos, size_t numRows )
2266{
bd3c6758
MB
2267 wxFAIL_MSG( wxT("Called grid table class function DeleteRows\n"
2268 "but your derived table class does not override this function"));
8f177c8e 2269
f85afd4e
MB
2270 return FALSE;
2271}
2272
2273bool wxGridTableBase::InsertCols( size_t pos, size_t numCols )
2274{
bd3c6758
MB
2275 wxFAIL_MSG( wxT("Called grid table class function InsertCols\n"
2276 "but your derived table class does not override this function"));
8f177c8e 2277
f85afd4e
MB
2278 return FALSE;
2279}
2280
2281bool wxGridTableBase::AppendCols( size_t numCols )
2282{
bd3c6758
MB
2283 wxFAIL_MSG(wxT("Called grid table class function AppendCols\n"
2284 "but your derived table class does not override this function"));
8f177c8e 2285
f85afd4e
MB
2286 return FALSE;
2287}
2288
2289bool wxGridTableBase::DeleteCols( size_t pos, size_t numCols )
2290{
bd3c6758
MB
2291 wxFAIL_MSG( wxT("Called grid table class function DeleteCols\n"
2292 "but your derived table class does not override this function"));
8f177c8e 2293
f85afd4e
MB
2294 return FALSE;
2295}
2296
2297
2298wxString wxGridTableBase::GetRowLabelValue( int row )
2299{
2300 wxString s;
f2d76237
RD
2301 s << row + 1; // RD: Starting the rows at zero confuses users, no matter
2302 // how much it makes sense to us geeks.
f85afd4e
MB
2303 return s;
2304}
2305
2306wxString wxGridTableBase::GetColLabelValue( int col )
2307{
2308 // default col labels are:
2309 // cols 0 to 25 : A-Z
2310 // cols 26 to 675 : AA-ZZ
2311 // etc.
2312
2313 wxString s;
2314 unsigned int i, n;
2315 for ( n = 1; ; n++ )
2316 {
f0102d2a 2317 s += (_T('A') + (wxChar)( col%26 ));
f85afd4e
MB
2318 col = col/26 - 1;
2319 if ( col < 0 ) break;
2320 }
2321
2322 // reverse the string...
2323 wxString s2;
2324 for ( i = 0; i < n; i++ )
2325 {
2326 s2 += s[n-i-1];
2327 }
2328
2329 return s2;
2330}
2331
2332
f2d76237
RD
2333wxString wxGridTableBase::GetTypeName( int WXUNUSED(row), int WXUNUSED(col) )
2334{
816be743 2335 return wxGRID_VALUE_STRING;
f2d76237
RD
2336}
2337
2338bool wxGridTableBase::CanGetValueAs( int WXUNUSED(row), int WXUNUSED(col),
2339 const wxString& typeName )
2340{
816be743 2341 return typeName == wxGRID_VALUE_STRING;
f2d76237
RD
2342}
2343
2344bool wxGridTableBase::CanSetValueAs( int row, int col, const wxString& typeName )
2345{
2346 return CanGetValueAs(row, col, typeName);
2347}
2348
2349long wxGridTableBase::GetValueAsLong( int WXUNUSED(row), int WXUNUSED(col) )
2350{
2351 return 0;
2352}
2353
2354double wxGridTableBase::GetValueAsDouble( int WXUNUSED(row), int WXUNUSED(col) )
2355{
2356 return 0.0;
2357}
2358
2359bool wxGridTableBase::GetValueAsBool( int WXUNUSED(row), int WXUNUSED(col) )
2360{
2361 return FALSE;
2362}
2363
2364void wxGridTableBase::SetValueAsLong( int WXUNUSED(row), int WXUNUSED(col),
2365 long WXUNUSED(value) )
2366{
2367}
2368
2369void wxGridTableBase::SetValueAsDouble( int WXUNUSED(row), int WXUNUSED(col),
2370 double WXUNUSED(value) )
2371{
2372}
2373
2374void wxGridTableBase::SetValueAsBool( int WXUNUSED(row), int WXUNUSED(col),
2375 bool WXUNUSED(value) )
2376{
2377}
2378
2379
2380void* wxGridTableBase::GetValueAsCustom( int WXUNUSED(row), int WXUNUSED(col),
2381 const wxString& WXUNUSED(typeName) )
2382{
2383 return NULL;
2384}
2385
2386void wxGridTableBase::SetValueAsCustom( int WXUNUSED(row), int WXUNUSED(col),
2387 const wxString& WXUNUSED(typeName),
2388 void* WXUNUSED(value) )
2389{
2390}
2391
f85afd4e
MB
2392//////////////////////////////////////////////////////////////////////
2393//
2394// Message class for the grid table to send requests and notifications
2395// to the grid view
2396//
2397
2398wxGridTableMessage::wxGridTableMessage()
2399{
2400 m_table = (wxGridTableBase *) NULL;
2401 m_id = -1;
2402 m_comInt1 = -1;
2403 m_comInt2 = -1;
2404}
2405
2406wxGridTableMessage::wxGridTableMessage( wxGridTableBase *table, int id,
2407 int commandInt1, int commandInt2 )
2408{
2409 m_table = table;
2410 m_id = id;
2411 m_comInt1 = commandInt1;
2412 m_comInt2 = commandInt2;
2413}
2414
2415
2416
2417//////////////////////////////////////////////////////////////////////
2418//
2419// A basic grid table for string data. An object of this class will
2420// created by wxGrid if you don't specify an alternative table class.
2421//
2422
223d09f6 2423WX_DEFINE_OBJARRAY(wxGridStringArray)
f85afd4e
MB
2424
2425IMPLEMENT_DYNAMIC_CLASS( wxGridStringTable, wxGridTableBase )
2426
2427wxGridStringTable::wxGridStringTable()
2428 : wxGridTableBase()
2429{
2430}
2431
2432wxGridStringTable::wxGridStringTable( int numRows, int numCols )
2433 : wxGridTableBase()
2434{
2435 int row, col;
8f177c8e 2436
f85afd4e
MB
2437 m_data.Alloc( numRows );
2438
2439 wxArrayString sa;
2440 sa.Alloc( numCols );
2441 for ( col = 0; col < numCols; col++ )
2442 {
2443 sa.Add( wxEmptyString );
2444 }
8f177c8e 2445
f85afd4e
MB
2446 for ( row = 0; row < numRows; row++ )
2447 {
2448 m_data.Add( sa );
2449 }
2450}
2451
2452wxGridStringTable::~wxGridStringTable()
2453{
2454}
2455
2456long wxGridStringTable::GetNumberRows()
2457{
2458 return m_data.GetCount();
2459}
2460
2461long wxGridStringTable::GetNumberCols()
2462{
2463 if ( m_data.GetCount() > 0 )
2464 return m_data[0].GetCount();
2465 else
2466 return 0;
2467}
2468
2469wxString wxGridStringTable::GetValue( int row, int col )
2470{
831243b1 2471 wxASSERT_MSG( (row < GetNumberRows()) && (col < GetNumberCols()),
af547d51
VZ
2472 _T("invalid row or column index in wxGridStringTable") );
2473
f85afd4e
MB
2474 return m_data[row][col];
2475}
2476
f2d76237 2477void wxGridStringTable::SetValue( int row, int col, const wxString& value )
f85afd4e 2478{
831243b1 2479 wxASSERT_MSG( (row < GetNumberRows()) && (col < GetNumberCols()),
af547d51
VZ
2480 _T("invalid row or column index in wxGridStringTable") );
2481
f2d76237 2482 m_data[row][col] = value;
f85afd4e
MB
2483}
2484
2485bool wxGridStringTable::IsEmptyCell( int row, int col )
2486{
831243b1 2487 wxASSERT_MSG( (row < GetNumberRows()) && (col < GetNumberCols()),
af547d51
VZ
2488 _T("invalid row or column index in wxGridStringTable") );
2489
f85afd4e
MB
2490 return (m_data[row][col] == wxEmptyString);
2491}
2492
f85afd4e
MB
2493void wxGridStringTable::Clear()
2494{
2495 int row, col;
2496 int numRows, numCols;
8f177c8e 2497
f85afd4e
MB
2498 numRows = m_data.GetCount();
2499 if ( numRows > 0 )
2500 {
2501 numCols = m_data[0].GetCount();
2502
2503 for ( row = 0; row < numRows; row++ )
2504 {
2505 for ( col = 0; col < numCols; col++ )
2506 {
2507 m_data[row][col] = wxEmptyString;
2508 }
2509 }
2510 }
2511}
2512
2513
2514bool wxGridStringTable::InsertRows( size_t pos, size_t numRows )
2515{
2516 size_t row, col;
2517
2518 size_t curNumRows = m_data.GetCount();
2519 size_t curNumCols = ( curNumRows > 0 ? m_data[0].GetCount() : 0 );
8f177c8e 2520
f85afd4e
MB
2521 if ( pos >= curNumRows )
2522 {
2523 return AppendRows( numRows );
2524 }
8f177c8e 2525
f85afd4e
MB
2526 wxArrayString sa;
2527 sa.Alloc( curNumCols );
2528 for ( col = 0; col < curNumCols; col++ )
2529 {
2530 sa.Add( wxEmptyString );
2531 }
2532
2533 for ( row = pos; row < pos + numRows; row++ )
2534 {
2535 m_data.Insert( sa, row );
2536 }
4d60017a 2537 UpdateAttrRows( pos, numRows );
f85afd4e
MB
2538 if ( GetView() )
2539 {
2540 wxGridTableMessage msg( this,
2541 wxGRIDTABLE_NOTIFY_ROWS_INSERTED,
2542 pos,
2543 numRows );
8f177c8e 2544
f85afd4e
MB
2545 GetView()->ProcessTableMessage( msg );
2546 }
2547
2548 return TRUE;
2549}
2550
2551bool wxGridStringTable::AppendRows( size_t numRows )
2552{
2553 size_t row, col;
2554
2555 size_t curNumRows = m_data.GetCount();
2556 size_t curNumCols = ( curNumRows > 0 ? m_data[0].GetCount() : 0 );
8f177c8e 2557
f85afd4e
MB
2558 wxArrayString sa;
2559 if ( curNumCols > 0 )
2560 {
2561 sa.Alloc( curNumCols );
2562 for ( col = 0; col < curNumCols; col++ )
2563 {
2564 sa.Add( wxEmptyString );
2565 }
2566 }
8f177c8e 2567
f85afd4e
MB
2568 for ( row = 0; row < numRows; row++ )
2569 {
2570 m_data.Add( sa );
2571 }
2572
2573 if ( GetView() )
2574 {
2575 wxGridTableMessage msg( this,
2576 wxGRIDTABLE_NOTIFY_ROWS_APPENDED,
2577 numRows );
8f177c8e 2578
f85afd4e
MB
2579 GetView()->ProcessTableMessage( msg );
2580 }
2581
8f177c8e 2582 return TRUE;
f85afd4e
MB
2583}
2584
2585bool wxGridStringTable::DeleteRows( size_t pos, size_t numRows )
2586{
2587 size_t n;
2588
2589 size_t curNumRows = m_data.GetCount();
8f177c8e 2590
f85afd4e
MB
2591 if ( pos >= curNumRows )
2592 {
bd3c6758
MB
2593 wxString errmsg;
2594 errmsg.Printf("Called wxGridStringTable::DeleteRows(pos=%d, N=%d)\n"
2595 "Pos value is invalid for present table with %d rows",
2596 pos, numRows, curNumRows );
2597 wxFAIL_MSG( wxT(errmsg) );
f85afd4e
MB
2598 return FALSE;
2599 }
2600
2601 if ( numRows > curNumRows - pos )
2602 {
2603 numRows = curNumRows - pos;
2604 }
8f177c8e 2605
f85afd4e
MB
2606 if ( numRows >= curNumRows )
2607 {
2608 m_data.Empty(); // don't release memory just yet
2609 }
2610 else
2611 {
2612 for ( n = 0; n < numRows; n++ )
2613 {
2614 m_data.Remove( pos );
2615 }
2616 }
9b4aede2 2617 UpdateAttrRows( pos, -((int)numRows) );
f85afd4e
MB
2618 if ( GetView() )
2619 {
2620 wxGridTableMessage msg( this,
2621 wxGRIDTABLE_NOTIFY_ROWS_DELETED,
2622 pos,
2623 numRows );
8f177c8e 2624
f85afd4e
MB
2625 GetView()->ProcessTableMessage( msg );
2626 }
2627
8f177c8e 2628 return TRUE;
f85afd4e
MB
2629}
2630
2631bool wxGridStringTable::InsertCols( size_t pos, size_t numCols )
2632{
2633 size_t row, col;
2634
2635 size_t curNumRows = m_data.GetCount();
2636 size_t curNumCols = ( curNumRows > 0 ? m_data[0].GetCount() : 0 );
8f177c8e 2637
f85afd4e
MB
2638 if ( pos >= curNumCols )
2639 {
2640 return AppendCols( numCols );
2641 }
2642
2643 for ( row = 0; row < curNumRows; row++ )
2644 {
2645 for ( col = pos; col < pos + numCols; col++ )
2646 {
2647 m_data[row].Insert( wxEmptyString, col );
2648 }
2649 }
4d60017a 2650 UpdateAttrCols( pos, numCols );
f85afd4e
MB
2651 if ( GetView() )
2652 {
2653 wxGridTableMessage msg( this,
2654 wxGRIDTABLE_NOTIFY_COLS_INSERTED,
2655 pos,
2656 numCols );
8f177c8e 2657
f85afd4e
MB
2658 GetView()->ProcessTableMessage( msg );
2659 }
2660
2661 return TRUE;
2662}
2663
2664bool wxGridStringTable::AppendCols( size_t numCols )
2665{
2666 size_t row, n;
2667
2668 size_t curNumRows = m_data.GetCount();
2669 if ( !curNumRows )
2670 {
2671 // TODO: something better than this ?
2672 //
bd3c6758
MB
2673 wxFAIL_MSG( wxT("Unable to append cols to a grid table with no rows.\n"
2674 "Call AppendRows() first") );
f85afd4e
MB
2675 return FALSE;
2676 }
8f177c8e 2677
f85afd4e
MB
2678 for ( row = 0; row < curNumRows; row++ )
2679 {
2680 for ( n = 0; n < numCols; n++ )
2681 {
2682 m_data[row].Add( wxEmptyString );
2683 }
2684 }
2685
2686 if ( GetView() )
2687 {
2688 wxGridTableMessage msg( this,
2689 wxGRIDTABLE_NOTIFY_COLS_APPENDED,
2690 numCols );
8f177c8e 2691
f85afd4e
MB
2692 GetView()->ProcessTableMessage( msg );
2693 }
2694
2695 return TRUE;
2696}
2697
2698bool wxGridStringTable::DeleteCols( size_t pos, size_t numCols )
2699{
2700 size_t row, n;
2701
2702 size_t curNumRows = m_data.GetCount();
2703 size_t curNumCols = ( curNumRows > 0 ? m_data[0].GetCount() : 0 );
8f177c8e 2704
f85afd4e
MB
2705 if ( pos >= curNumCols )
2706 {
bd3c6758
MB
2707 wxString errmsg;
2708 errmsg.Printf( "Called wxGridStringTable::DeleteCols(pos=%d, N=%d)...\n"
2709 "Pos value is invalid for present table with %d cols",
2710 pos, numCols, curNumCols );
2711 wxFAIL_MSG( wxT( errmsg ) );
8f177c8e 2712 return FALSE;
f85afd4e
MB
2713 }
2714
2715 if ( numCols > curNumCols - pos )
2716 {
8f177c8e 2717 numCols = curNumCols - pos;
f85afd4e
MB
2718 }
2719
2720 for ( row = 0; row < curNumRows; row++ )
2721 {
2722 if ( numCols >= curNumCols )
2723 {
2724 m_data[row].Clear();
2725 }
2726 else
2727 {
2728 for ( n = 0; n < numCols; n++ )
2729 {
2730 m_data[row].Remove( pos );
2731 }
2732 }
2733 }
9b4aede2 2734 UpdateAttrCols( pos, -((int)numCols) );
f85afd4e
MB
2735 if ( GetView() )
2736 {
2737 wxGridTableMessage msg( this,
2738 wxGRIDTABLE_NOTIFY_COLS_DELETED,
2739 pos,
2740 numCols );
8f177c8e 2741
f85afd4e
MB
2742 GetView()->ProcessTableMessage( msg );
2743 }
2744
8f177c8e 2745 return TRUE;
f85afd4e
MB
2746}
2747
2748wxString wxGridStringTable::GetRowLabelValue( int row )
2749{
2750 if ( row > (int)(m_rowLabels.GetCount()) - 1 )
2751 {
2752 // using default label
2753 //
2754 return wxGridTableBase::GetRowLabelValue( row );
2755 }
2756 else
2757 {
2758 return m_rowLabels[ row ];
2759 }
2760}
2761
2762wxString wxGridStringTable::GetColLabelValue( int col )
2763{
2764 if ( col > (int)(m_colLabels.GetCount()) - 1 )
2765 {
2766 // using default label
2767 //
2768 return wxGridTableBase::GetColLabelValue( col );
2769 }
2770 else
2771 {
2772 return m_colLabels[ col ];
2773 }
2774}
2775
2776void wxGridStringTable::SetRowLabelValue( int row, const wxString& value )
2777{
2778 if ( row > (int)(m_rowLabels.GetCount()) - 1 )
2779 {
2780 int n = m_rowLabels.GetCount();
2781 int i;
2782 for ( i = n; i <= row; i++ )
2783 {
2784 m_rowLabels.Add( wxGridTableBase::GetRowLabelValue(i) );
2785 }
2786 }
2787
2788 m_rowLabels[row] = value;
2789}
2790
2791void wxGridStringTable::SetColLabelValue( int col, const wxString& value )
2792{
2793 if ( col > (int)(m_colLabels.GetCount()) - 1 )
2794 {
2795 int n = m_colLabels.GetCount();
2796 int i;
2797 for ( i = n; i <= col; i++ )
2798 {
2799 m_colLabels.Add( wxGridTableBase::GetColLabelValue(i) );
2800 }
2801 }
2802
2803 m_colLabels[col] = value;
2804}
2805
2806
2807
f85afd4e 2808//////////////////////////////////////////////////////////////////////
2d66e025
MB
2809//////////////////////////////////////////////////////////////////////
2810
2811IMPLEMENT_DYNAMIC_CLASS( wxGridRowLabelWindow, wxWindow )
2812
2813BEGIN_EVENT_TABLE( wxGridRowLabelWindow, wxWindow )
2814 EVT_PAINT( wxGridRowLabelWindow::OnPaint )
2815 EVT_MOUSE_EVENTS( wxGridRowLabelWindow::OnMouseEvent )
2816 EVT_KEY_DOWN( wxGridRowLabelWindow::OnKeyDown )
2817END_EVENT_TABLE()
2818
60ff3b99
VZ
2819wxGridRowLabelWindow::wxGridRowLabelWindow( wxGrid *parent,
2820 wxWindowID id,
2d66e025 2821 const wxPoint &pos, const wxSize &size )
ebd773c6 2822 : wxWindow( parent, id, pos, size, wxWANTS_CHARS )
2d66e025
MB
2823{
2824 m_owner = parent;
2825}
2826
2827void wxGridRowLabelWindow::OnPaint( wxPaintEvent &event )
2828{
2829 wxPaintDC dc(this);
2830
2831 // NO - don't do this because it will set both the x and y origin
2832 // coords to match the parent scrolled window and we just want to
2833 // set the y coord - MB
2834 //
2835 // m_owner->PrepareDC( dc );
60ff3b99 2836
790ad94f 2837 int x, y;
2d66e025
MB
2838 m_owner->CalcUnscrolledPosition( 0, 0, &x, &y );
2839 dc.SetDeviceOrigin( 0, -y );
60ff3b99 2840
2d66e025
MB
2841 m_owner->CalcRowLabelsExposed( GetUpdateRegion() );
2842 m_owner->DrawRowLabels( dc );
2843}
2844
2845
2846void wxGridRowLabelWindow::OnMouseEvent( wxMouseEvent& event )
2847{
2848 m_owner->ProcessRowLabelMouseEvent( event );
2849}
2850
2851
2852// This seems to be required for wxMotif otherwise the mouse
2853// cursor must be in the cell edit control to get key events
2854//
2855void wxGridRowLabelWindow::OnKeyDown( wxKeyEvent& event )
2856{
2857 if ( !m_owner->ProcessEvent( event ) ) event.Skip();
2858}
2859
2860
2861
2862//////////////////////////////////////////////////////////////////////
2863
2864IMPLEMENT_DYNAMIC_CLASS( wxGridColLabelWindow, wxWindow )
2865
2866BEGIN_EVENT_TABLE( wxGridColLabelWindow, wxWindow )
2867 EVT_PAINT( wxGridColLabelWindow::OnPaint )
2868 EVT_MOUSE_EVENTS( wxGridColLabelWindow::OnMouseEvent )
2869 EVT_KEY_DOWN( wxGridColLabelWindow::OnKeyDown )
2870END_EVENT_TABLE()
2871
60ff3b99
VZ
2872wxGridColLabelWindow::wxGridColLabelWindow( wxGrid *parent,
2873 wxWindowID id,
2d66e025 2874 const wxPoint &pos, const wxSize &size )
ebd773c6 2875 : wxWindow( parent, id, pos, size, wxWANTS_CHARS )
2d66e025
MB
2876{
2877 m_owner = parent;
2878}
2879
2880void wxGridColLabelWindow::OnPaint( wxPaintEvent &event )
2881{
2882 wxPaintDC dc(this);
2883
2884 // NO - don't do this because it will set both the x and y origin
2885 // coords to match the parent scrolled window and we just want to
2886 // set the x coord - MB
2887 //
2888 // m_owner->PrepareDC( dc );
60ff3b99 2889
790ad94f 2890 int x, y;
2d66e025
MB
2891 m_owner->CalcUnscrolledPosition( 0, 0, &x, &y );
2892 dc.SetDeviceOrigin( -x, 0 );
2893
60ff3b99
VZ
2894 m_owner->CalcColLabelsExposed( GetUpdateRegion() );
2895 m_owner->DrawColLabels( dc );
2d66e025
MB
2896}
2897
2898
2899void wxGridColLabelWindow::OnMouseEvent( wxMouseEvent& event )
2900{
2901 m_owner->ProcessColLabelMouseEvent( event );
2902}
2903
2904
2905// This seems to be required for wxMotif otherwise the mouse
2906// cursor must be in the cell edit control to get key events
2907//
2908void wxGridColLabelWindow::OnKeyDown( wxKeyEvent& event )
2909{
2910 if ( !m_owner->ProcessEvent( event ) ) event.Skip();
2911}
2912
2913
2914
2915//////////////////////////////////////////////////////////////////////
2916
2917IMPLEMENT_DYNAMIC_CLASS( wxGridCornerLabelWindow, wxWindow )
2918
2919BEGIN_EVENT_TABLE( wxGridCornerLabelWindow, wxWindow )
2920 EVT_MOUSE_EVENTS( wxGridCornerLabelWindow::OnMouseEvent )
d2fdd8d2 2921 EVT_PAINT( wxGridCornerLabelWindow::OnPaint)
2d66e025
MB
2922 EVT_KEY_DOWN( wxGridCornerLabelWindow::OnKeyDown )
2923END_EVENT_TABLE()
2924
60ff3b99
VZ
2925wxGridCornerLabelWindow::wxGridCornerLabelWindow( wxGrid *parent,
2926 wxWindowID id,
2d66e025 2927 const wxPoint &pos, const wxSize &size )
ebd773c6 2928 : wxWindow( parent, id, pos, size, wxWANTS_CHARS )
2d66e025
MB
2929{
2930 m_owner = parent;
2931}
2932
d2fdd8d2
RR
2933void wxGridCornerLabelWindow::OnPaint( wxPaintEvent& WXUNUSED(event) )
2934{
2935 wxPaintDC dc(this);
b99be8fb 2936
d2fdd8d2
RR
2937 int client_height = 0;
2938 int client_width = 0;
2939 GetClientSize( &client_width, &client_height );
b99be8fb 2940
d2fdd8d2
RR
2941 dc.SetPen( *wxBLACK_PEN );
2942 dc.DrawLine( client_width-1, client_height-1, client_width-1, 0 );
2943 dc.DrawLine( client_width-1, client_height-1, 0, client_height-1 );
b99be8fb 2944
d2fdd8d2
RR
2945 dc.SetPen( *wxWHITE_PEN );
2946 dc.DrawLine( 0, 0, client_width, 0 );
2947 dc.DrawLine( 0, 0, 0, client_height );
2948}
2949
2d66e025
MB
2950
2951void wxGridCornerLabelWindow::OnMouseEvent( wxMouseEvent& event )
2952{
2953 m_owner->ProcessCornerLabelMouseEvent( event );
2954}
2955
2956
2957// This seems to be required for wxMotif otherwise the mouse
2958// cursor must be in the cell edit control to get key events
2959//
2960void wxGridCornerLabelWindow::OnKeyDown( wxKeyEvent& event )
2961{
2962 if ( !m_owner->ProcessEvent( event ) ) event.Skip();
2963}
2964
2965
2966
f85afd4e
MB
2967//////////////////////////////////////////////////////////////////////
2968
2d66e025
MB
2969IMPLEMENT_DYNAMIC_CLASS( wxGridWindow, wxPanel )
2970
2971BEGIN_EVENT_TABLE( wxGridWindow, wxPanel )
2972 EVT_PAINT( wxGridWindow::OnPaint )
2d66e025
MB
2973 EVT_MOUSE_EVENTS( wxGridWindow::OnMouseEvent )
2974 EVT_KEY_DOWN( wxGridWindow::OnKeyDown )
2796cce3 2975 EVT_ERASE_BACKGROUND( wxGridWindow::OnEraseBackground )
2d66e025
MB
2976END_EVENT_TABLE()
2977
60ff3b99
VZ
2978wxGridWindow::wxGridWindow( wxGrid *parent,
2979 wxGridRowLabelWindow *rowLblWin,
2d66e025
MB
2980 wxGridColLabelWindow *colLblWin,
2981 wxWindowID id, const wxPoint &pos, const wxSize &size )
ebd773c6 2982 : wxPanel( parent, id, pos, size, wxWANTS_CHARS, "grid window" )
2d66e025
MB
2983{
2984 m_owner = parent;
2985 m_rowLabelWin = rowLblWin;
2986 m_colLabelWin = colLblWin;
2d66e025
MB
2987 SetBackgroundColour( "WHITE" );
2988}
2989
2990
2991wxGridWindow::~wxGridWindow()
2992{
2993}
2994
2995
2996void wxGridWindow::OnPaint( wxPaintEvent &WXUNUSED(event) )
2997{
2998 wxPaintDC dc( this );
2999 m_owner->PrepareDC( dc );
796df70a
SN
3000 wxRegion reg = GetUpdateRegion();
3001 m_owner->CalcCellsExposed( reg );
2d66e025 3002 m_owner->DrawGridCellArea( dc );
9496deb5 3003#if WXGRID_DRAW_LINES
796df70a
SN
3004 m_owner->DrawAllGridLines( dc, reg );
3005#endif
a5777624 3006 m_owner->DrawGridSpace( dc );
b3a7510d 3007 m_owner->DrawHighlight( dc );
2d66e025
MB
3008}
3009
3010
3011void wxGridWindow::ScrollWindow( int dx, int dy, const wxRect *rect )
3012{
3013 wxPanel::ScrollWindow( dx, dy, rect );
3014 m_rowLabelWin->ScrollWindow( 0, dy, rect );
3015 m_colLabelWin->ScrollWindow( dx, 0, rect );
3016}
3017
3018
3019void wxGridWindow::OnMouseEvent( wxMouseEvent& event )
3020{
3021 m_owner->ProcessGridCellMouseEvent( event );
3022}
3023
3024
3025// This seems to be required for wxMotif otherwise the mouse
3026// cursor must be in the cell edit control to get key events
3027//
3028void wxGridWindow::OnKeyDown( wxKeyEvent& event )
3029{
3030 if ( !m_owner->ProcessEvent( event ) ) event.Skip();
3031}
f85afd4e 3032
7c8a8ad5 3033
8dd4f536
VZ
3034void wxGridWindow::OnEraseBackground(wxEraseEvent& event)
3035{
8dd4f536 3036}
025562fe 3037
2d66e025
MB
3038
3039//////////////////////////////////////////////////////////////////////
3040
07296f0b 3041
2d66e025
MB
3042IMPLEMENT_DYNAMIC_CLASS( wxGrid, wxScrolledWindow )
3043
3044BEGIN_EVENT_TABLE( wxGrid, wxScrolledWindow )
f85afd4e
MB
3045 EVT_PAINT( wxGrid::OnPaint )
3046 EVT_SIZE( wxGrid::OnSize )
f85afd4e 3047 EVT_KEY_DOWN( wxGrid::OnKeyDown )
2796cce3 3048 EVT_ERASE_BACKGROUND( wxGrid::OnEraseBackground )
f85afd4e 3049END_EVENT_TABLE()
8f177c8e 3050
2d66e025
MB
3051wxGrid::wxGrid( wxWindow *parent,
3052 wxWindowID id,
3053 const wxPoint& pos,
3054 const wxSize& size,
3055 long style,
3056 const wxString& name )
ebd773c6 3057 : wxScrolledWindow( parent, id, pos, size, (style | wxWANTS_CHARS), name ),
af547d51
VZ
3058 m_colMinWidths(GRID_HASH_SIZE),
3059 m_rowMinHeights(GRID_HASH_SIZE)
2d66e025
MB
3060{
3061 Create();
58dd5b3b
MB
3062}
3063
3064
3065wxGrid::~wxGrid()
3066{
0a976765 3067 ClearAttrCache();
39bcce60 3068 wxSafeDecRef(m_defaultCellAttr);
0a976765
VZ
3069
3070#ifdef DEBUG_ATTR_CACHE
3071 size_t total = gs_nAttrCacheHits + gs_nAttrCacheMisses;
3072 wxPrintf(_T("wxGrid attribute cache statistics: "
3073 "total: %u, hits: %u (%u%%)\n"),
3074 total, gs_nAttrCacheHits,
3075 total ? (gs_nAttrCacheHits*100) / total : 0);
3076#endif
3077
2796cce3
RD
3078 if (m_ownTable)
3079 delete m_table;
f2d76237
RD
3080
3081 delete m_typeRegistry;
b5808881 3082 delete m_selection;
58dd5b3b
MB
3083}
3084
2d66e025 3085
58dd5b3b
MB
3086//
3087// ----- internal init and update functions
3088//
3089
3090void wxGrid::Create()
f0102d2a 3091{
4634a5d6 3092 m_created = FALSE; // set to TRUE by CreateGrid
75ecbe45 3093 m_displayed = TRUE; // FALSE; // set to TRUE by OnPaint
4634a5d6
MB
3094
3095 m_table = (wxGridTableBase *) NULL;
2796cce3 3096 m_ownTable = FALSE;
2c9a89e0
RD
3097
3098 m_cellEditCtrlEnabled = FALSE;
4634a5d6 3099
2796cce3
RD
3100 m_defaultCellAttr = new wxGridCellAttr;
3101 m_defaultCellAttr->SetDefAttr(m_defaultCellAttr);
f2d76237
RD
3102
3103 // Set default cell attributes
3104 m_defaultCellAttr->SetFont(GetFont());
3105 m_defaultCellAttr->SetAlignment(wxLEFT, wxTOP);
3106 m_defaultCellAttr->SetTextColour(
3107 wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOWTEXT));
3108 m_defaultCellAttr->SetBackgroundColour(
3109 wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW));
3110 m_defaultCellAttr->SetRenderer(new wxGridCellStringRenderer);
3111 m_defaultCellAttr->SetEditor(new wxGridCellTextEditor);
2796cce3
RD
3112
3113
4634a5d6
MB
3114 m_numRows = 0;
3115 m_numCols = 0;
3116 m_currentCellCoords = wxGridNoCellCoords;
b99be8fb 3117
18f9565d
MB
3118 m_rowLabelWidth = WXGRID_DEFAULT_ROW_LABEL_WIDTH;
3119 m_colLabelHeight = WXGRID_DEFAULT_COL_LABEL_HEIGHT;
2d66e025 3120
c4608a8a 3121 // create the type registry
f2d76237 3122 m_typeRegistry = new wxGridTypeRegistry;
b5808881 3123 m_selection = 0;
f2d76237 3124 // subwindow components that make up the wxGrid
7807d81c
MB
3125 m_cornerLabelWin = new wxGridCornerLabelWindow( this,
3126 -1,
18f9565d
MB
3127 wxDefaultPosition,
3128 wxDefaultSize );
7807d81c 3129
60ff3b99
VZ
3130 m_rowLabelWin = new wxGridRowLabelWindow( this,
3131 -1,
18f9565d
MB
3132 wxDefaultPosition,
3133 wxDefaultSize );
2d66e025
MB
3134
3135 m_colLabelWin = new wxGridColLabelWindow( this,
3136 -1,
18f9565d
MB
3137 wxDefaultPosition,
3138 wxDefaultSize );
60ff3b99 3139
60ff3b99
VZ
3140 m_gridWin = new wxGridWindow( this,
3141 m_rowLabelWin,
3142 m_colLabelWin,
3143 -1,
18f9565d 3144 wxDefaultPosition,
2d66e025
MB
3145 wxDefaultSize );
3146
3147 SetTargetWindow( m_gridWin );
2d66e025 3148}
f85afd4e 3149
2d66e025 3150
b5808881
SN
3151bool wxGrid::CreateGrid( int numRows, int numCols,
3152 wxGrid::wxGridSelectionModes selmode )
2d66e025
MB
3153{
3154 if ( m_created )
3155 {
2796cce3 3156 wxFAIL_MSG( wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
2d66e025
MB
3157 return FALSE;
3158 }
3159 else
3160 {
3161 m_numRows = numRows;
3162 m_numCols = numCols;
3163
3164 m_table = new wxGridStringTable( m_numRows, m_numCols );
3165 m_table->SetView( this );
2796cce3
RD
3166 m_ownTable = TRUE;
3167 Init();
043d16b2 3168 m_selection = new wxGridSelection( this, selmode );
2796cce3
RD
3169 m_created = TRUE;
3170 }
2796cce3
RD
3171 return m_created;
3172}
3173
f1567cdd
SN
3174void wxGrid::SetSelectionMode(wxGrid::wxGridSelectionModes selmode)
3175{
3176 if ( !m_created )
3177 {
3178 wxFAIL_MSG( wxT("Called wxGrid::SetSelectionMode() before calling CreateGrid()") );
3179 }
3180 else
3181 m_selection->SetSelectionMode( selmode );
3182}
3183
043d16b2
SN
3184bool wxGrid::SetTable( wxGridTableBase *table, bool takeOwnership,
3185 wxGrid::wxGridSelectionModes selmode )
2796cce3
RD
3186{
3187 if ( m_created )
3188 {
fb295790
RD
3189 // RD: Actually, this should probably be allowed. I think it would be
3190 // nice to be able to switch multiple Tables in and out of a single
3191 // View at runtime. Is there anything in the implmentation that would
3192 // prevent this?
3193
043d16b2 3194 // At least, you now have to copy with m_selection
2796cce3
RD
3195 wxFAIL_MSG( wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
3196 return FALSE;
3197 }
3198 else
3199 {
3200 m_numRows = table->GetNumberRows();
3201 m_numCols = table->GetNumberCols();
3202
3203 m_table = table;
3204 m_table->SetView( this );
3205 if (takeOwnership)
3206 m_ownTable = TRUE;
2d66e025 3207 Init();
043d16b2 3208 m_selection = new wxGridSelection( this, selmode );
2d66e025
MB
3209 m_created = TRUE;
3210 }
f85afd4e 3211
2d66e025 3212 return m_created;
f85afd4e
MB
3213}
3214
2d66e025 3215
f85afd4e
MB
3216void wxGrid::Init()
3217{
f85afd4e
MB
3218 if ( m_numRows <= 0 )
3219 m_numRows = WXGRID_DEFAULT_NUMBER_ROWS;
3220
3221 if ( m_numCols <= 0 )
3222 m_numCols = WXGRID_DEFAULT_NUMBER_COLS;
3223
3224 m_rowLabelWidth = WXGRID_DEFAULT_ROW_LABEL_WIDTH;
3225 m_colLabelHeight = WXGRID_DEFAULT_COL_LABEL_HEIGHT;
3226
60ff3b99
VZ
3227 if ( m_rowLabelWin )
3228 {
3229 m_labelBackgroundColour = m_rowLabelWin->GetBackgroundColour();
3230 }
3231 else
3232 {
3233 m_labelBackgroundColour = wxColour( _T("WHITE") );
3234 }
3235
3236 m_labelTextColour = wxColour( _T("BLACK") );
f85afd4e 3237
0a976765
VZ
3238 // init attr cache
3239 m_attrCache.row = -1;
3240
f85afd4e
MB
3241 // TODO: something better than this ?
3242 //
3243 m_labelFont = this->GetFont();
3244 m_labelFont.SetWeight( m_labelFont.GetWeight() + 2 );
8f177c8e 3245
f85afd4e
MB
3246 m_rowLabelHorizAlign = wxLEFT;
3247 m_rowLabelVertAlign = wxCENTRE;
3248
3249 m_colLabelHorizAlign = wxCENTRE;
3250 m_colLabelVertAlign = wxTOP;
3251
f85afd4e 3252 m_defaultColWidth = WXGRID_DEFAULT_COL_WIDTH;
1f1ce288
MB
3253 m_defaultRowHeight = m_gridWin->GetCharHeight();
3254
d2fdd8d2 3255#if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl()
1f1ce288
MB
3256 m_defaultRowHeight += 8;
3257#else
3258 m_defaultRowHeight += 4;
3259#endif
3260
2d66e025 3261 m_gridLineColour = wxColour( 128, 128, 255 );
f85afd4e 3262 m_gridLinesEnabled = TRUE;
8f177c8e 3263
58dd5b3b 3264 m_cursorMode = WXGRID_CURSOR_SELECT_CELL;
e2b42eeb 3265 m_winCapture = (wxWindow *)NULL;
6e8524b1
MB
3266 m_canDragRowSize = TRUE;
3267 m_canDragColSize = TRUE;
4cfa5de6 3268 m_canDragGridSize = TRUE;
f85afd4e
MB
3269 m_dragLastPos = -1;
3270 m_dragRowOrCol = -1;
3271 m_isDragging = FALSE;
07296f0b 3272 m_startDragPos = wxDefaultPosition;
f85afd4e 3273
07296f0b 3274 m_waitForSlowClick = FALSE;
025562fe 3275
f85afd4e
MB
3276 m_rowResizeCursor = wxCursor( wxCURSOR_SIZENS );
3277 m_colResizeCursor = wxCursor( wxCURSOR_SIZEWE );
3278
3279 m_currentCellCoords = wxGridNoCellCoords;
f85afd4e 3280
b5808881
SN
3281 m_selectingTopLeft = wxGridNoCellCoords;
3282 m_selectingBottomRight = wxGridNoCellCoords;
2796cce3
RD
3283 m_selectionBackground = wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHT);
3284 m_selectionForeground = wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHTTEXT);
8f177c8e 3285
f85afd4e
MB
3286 m_editable = TRUE; // default for whole grid
3287
2d66e025
MB
3288 m_inOnKeyDown = FALSE;
3289 m_batchCount = 0;
3c79cf49 3290
266e8367
VZ
3291 m_extraWidth =
3292 m_extraHeight = 50;
3293
3c79cf49 3294 CalcDimensions();
7c1cb261
VZ
3295}
3296
3297// ----------------------------------------------------------------------------
3298// the idea is to call these functions only when necessary because they create
3299// quite big arrays which eat memory mostly unnecessary - in particular, if
3300// default widths/heights are used for all rows/columns, we may not use these
3301// arrays at all
3302//
3303// with some extra code, it should be possible to only store the
3304// widths/heights different from default ones but this will be done later...
3305// ----------------------------------------------------------------------------
3306
3307void wxGrid::InitRowHeights()
3308{
3309 m_rowHeights.Empty();
3310 m_rowBottoms.Empty();
3311
3312 m_rowHeights.Alloc( m_numRows );
3313 m_rowBottoms.Alloc( m_numRows );
3314
3315 int rowBottom = 0;
3316 for ( int i = 0; i < m_numRows; i++ )
3317 {
3318 m_rowHeights.Add( m_defaultRowHeight );
3319 rowBottom += m_defaultRowHeight;
3320 m_rowBottoms.Add( rowBottom );
3321 }
3322}
3323
3324void wxGrid::InitColWidths()
3325{
3326 m_colWidths.Empty();
3327 m_colRights.Empty();
3328
3329 m_colWidths.Alloc( m_numCols );
3330 m_colRights.Alloc( m_numCols );
3331 int colRight = 0;
3332 for ( int i = 0; i < m_numCols; i++ )
3333 {
3334 m_colWidths.Add( m_defaultColWidth );
3335 colRight += m_defaultColWidth;
3336 m_colRights.Add( colRight );
3337 }
3338}
3339
3340int wxGrid::GetColWidth(int col) const
3341{
3342 return m_colWidths.IsEmpty() ? m_defaultColWidth : m_colWidths[col];
3343}
3344
3345int wxGrid::GetColLeft(int col) const
3346{
3347 return m_colRights.IsEmpty() ? col * m_defaultColWidth
3348 : m_colRights[col] - m_colWidths[col];
3349}
3350
3351int wxGrid::GetColRight(int col) const
3352{
3353 return m_colRights.IsEmpty() ? (col + 1) * m_defaultColWidth
3354 : m_colRights[col];
3355}
3356
3357int wxGrid::GetRowHeight(int row) const
3358{
3359 return m_rowHeights.IsEmpty() ? m_defaultRowHeight : m_rowHeights[row];
3360}
2d66e025 3361
7c1cb261
VZ
3362int wxGrid::GetRowTop(int row) const
3363{
3364 return m_rowBottoms.IsEmpty() ? row * m_defaultRowHeight
3365 : m_rowBottoms[row] - m_rowHeights[row];
f85afd4e
MB
3366}
3367
7c1cb261
VZ
3368int wxGrid::GetRowBottom(int row) const
3369{
3370 return m_rowBottoms.IsEmpty() ? (row + 1) * m_defaultRowHeight
3371 : m_rowBottoms[row];
3372}
f85afd4e
MB
3373
3374void wxGrid::CalcDimensions()
3375{
f85afd4e 3376 int cw, ch;
2d66e025 3377 GetClientSize( &cw, &ch );
f85afd4e 3378
2d66e025 3379 if ( m_numRows > 0 && m_numCols > 0 )
f85afd4e 3380 {
266e8367
VZ
3381 int right = GetColRight( m_numCols-1 ) + m_extraWidth;
3382 int bottom = GetRowBottom( m_numRows-1 ) + m_extraHeight;
60ff3b99 3383
2d66e025
MB
3384 // TODO: restore the scroll position that we had before sizing
3385 //
3386 int x, y;
3387 GetViewStart( &x, &y );
f0102d2a
VZ
3388 SetScrollbars( GRID_SCROLL_LINE, GRID_SCROLL_LINE,
3389 right/GRID_SCROLL_LINE, bottom/GRID_SCROLL_LINE,
be46e4a6 3390 x, y );
f85afd4e 3391 }
f85afd4e
MB
3392}
3393
3394
7807d81c
MB
3395void wxGrid::CalcWindowSizes()
3396{
3397 int cw, ch;
3398 GetClientSize( &cw, &ch );
b99be8fb 3399
7807d81c
MB
3400 if ( m_cornerLabelWin->IsShown() )
3401 m_cornerLabelWin->SetSize( 0, 0, m_rowLabelWidth, m_colLabelHeight );
3402
3403 if ( m_colLabelWin->IsShown() )
3404 m_colLabelWin->SetSize( m_rowLabelWidth, 0, cw-m_rowLabelWidth, m_colLabelHeight);
3405
3406 if ( m_rowLabelWin->IsShown() )
3407 m_rowLabelWin->SetSize( 0, m_colLabelHeight, m_rowLabelWidth, ch-m_colLabelHeight);
3408
3409 if ( m_gridWin->IsShown() )
3410 m_gridWin->SetSize( m_rowLabelWidth, m_colLabelHeight, cw-m_rowLabelWidth, ch-m_colLabelHeight);
3411}
3412
3413
f85afd4e
MB
3414// this is called when the grid table sends a message to say that it
3415// has been redimensioned
3416//
3417bool wxGrid::Redimension( wxGridTableMessage& msg )
3418{
3419 int i;
8f177c8e 3420
7c1cb261
VZ
3421 // if we were using the default widths/heights so far, we must change them
3422 // now
3423 if ( m_colWidths.IsEmpty() )
3424 {
3425 InitColWidths();
3426 }
3427
3428 if ( m_rowHeights.IsEmpty() )
3429 {
3430 InitRowHeights();
3431 }
3432
f85afd4e
MB
3433 switch ( msg.GetId() )
3434 {
3435 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED:
3436 {
3437 size_t pos = msg.GetCommandInt();
3438 int numRows = msg.GetCommandInt2();
3439 for ( i = 0; i < numRows; i++ )
3440 {
3441 m_rowHeights.Insert( m_defaultRowHeight, pos );
3442 m_rowBottoms.Insert( 0, pos );
3443 }
3444 m_numRows += numRows;
2d66e025
MB
3445
3446 int bottom = 0;
3447 if ( pos > 0 ) bottom = m_rowBottoms[pos-1];
60ff3b99 3448
2d66e025
MB
3449 for ( i = pos; i < m_numRows; i++ )
3450 {
3451 bottom += m_rowHeights[i];
3452 m_rowBottoms[i] = bottom;
3453 }
f85afd4e
MB
3454 CalcDimensions();
3455 }
3456 return TRUE;
3457
3458 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED:
3459 {
3460 int numRows = msg.GetCommandInt();
3461 for ( i = 0; i < numRows; i++ )
3462 {
3463 m_rowHeights.Add( m_defaultRowHeight );
3464 m_rowBottoms.Add( 0 );
3465 }
2d66e025
MB
3466
3467 int oldNumRows = m_numRows;
f85afd4e 3468 m_numRows += numRows;
2d66e025
MB
3469
3470 int bottom = 0;
3471 if ( oldNumRows > 0 ) bottom = m_rowBottoms[oldNumRows-1];
60ff3b99 3472
2d66e025
MB
3473 for ( i = oldNumRows; i < m_numRows; i++ )
3474 {
3475 bottom += m_rowHeights[i];
3476 m_rowBottoms[i] = bottom;
3477 }
f85afd4e
MB
3478 CalcDimensions();
3479 }
3480 return TRUE;
3481
3482 case wxGRIDTABLE_NOTIFY_ROWS_DELETED:
3483 {
3484 size_t pos = msg.GetCommandInt();
3485 int numRows = msg.GetCommandInt2();
3486 for ( i = 0; i < numRows; i++ )
3487 {
3488 m_rowHeights.Remove( pos );
3489 m_rowBottoms.Remove( pos );
3490 }
3491 m_numRows -= numRows;
3492
f85afd4e
MB
3493 if ( !m_numRows )
3494 {
3495 m_numCols = 0;
3496 m_colWidths.Clear();
3497 m_colRights.Clear();
3498 m_currentCellCoords = wxGridNoCellCoords;
3499 }
2d66e025 3500 else
f85afd4e 3501 {
2d66e025
MB
3502 if ( m_currentCellCoords.GetRow() >= m_numRows )
3503 m_currentCellCoords.Set( 0, 0 );
3504
3505 int h = 0;
3506 for ( i = 0; i < m_numRows; i++ )
3507 {
3508 h += m_rowHeights[i];
3509 m_rowBottoms[i] = h;
3510 }
f85afd4e 3511 }
60ff3b99 3512
f85afd4e
MB
3513 CalcDimensions();
3514 }
3515 return TRUE;
3516
3517 case wxGRIDTABLE_NOTIFY_COLS_INSERTED:
3518 {
3519 size_t pos = msg.GetCommandInt();
3520 int numCols = msg.GetCommandInt2();
3521 for ( i = 0; i < numCols; i++ )
3522 {
3523 m_colWidths.Insert( m_defaultColWidth, pos );
3524 m_colRights.Insert( 0, pos );
3525 }
3526 m_numCols += numCols;
2d66e025
MB
3527
3528 int right = 0;
3529 if ( pos > 0 ) right = m_colRights[pos-1];
60ff3b99 3530
2d66e025
MB
3531 for ( i = pos; i < m_numCols; i++ )
3532 {
3533 right += m_colWidths[i];
3534 m_colRights[i] = right;
3535 }
f85afd4e
MB
3536 CalcDimensions();
3537 }
3538 return TRUE;
3539
3540 case wxGRIDTABLE_NOTIFY_COLS_APPENDED:
3541 {
3542 int numCols = msg.GetCommandInt();
3543 for ( i = 0; i < numCols; i++ )
3544 {
3545 m_colWidths.Add( m_defaultColWidth );
3546 m_colRights.Add( 0 );
3547 }
2d66e025
MB
3548
3549 int oldNumCols = m_numCols;
f85afd4e 3550 m_numCols += numCols;
2d66e025
MB
3551
3552 int right = 0;
3553 if ( oldNumCols > 0 ) right = m_colRights[oldNumCols-1];
60ff3b99 3554
2d66e025
MB
3555 for ( i = oldNumCols; i < m_numCols; i++ )
3556 {
3557 right += m_colWidths[i];
3558 m_colRights[i] = right;
3559 }
f85afd4e
MB
3560 CalcDimensions();
3561 }
3562 return TRUE;
3563
3564 case wxGRIDTABLE_NOTIFY_COLS_DELETED:
3565 {
3566 size_t pos = msg.GetCommandInt();
3567 int numCols = msg.GetCommandInt2();
3568 for ( i = 0; i < numCols; i++ )
3569 {
3570 m_colWidths.Remove( pos );
3571 m_colRights.Remove( pos );
3572 }
3573 m_numCols -= numCols;
f85afd4e
MB
3574
3575 if ( !m_numCols )
3576 {
3577#if 0 // leave the row alone here so that AppendCols will work subsequently
3578 m_numRows = 0;
3579 m_rowHeights.Clear();
3580 m_rowBottoms.Clear();
8f177c8e 3581#endif
f85afd4e
MB
3582 m_currentCellCoords = wxGridNoCellCoords;
3583 }
60ff3b99 3584 else
f85afd4e 3585 {
2d66e025
MB
3586 if ( m_currentCellCoords.GetCol() >= m_numCols )
3587 m_currentCellCoords.Set( 0, 0 );
3588
3589 int w = 0;
3590 for ( i = 0; i < m_numCols; i++ )
3591 {
3592 w += m_colWidths[i];
3593 m_colRights[i] = w;
3594 }
f85afd4e
MB
3595 }
3596 CalcDimensions();
3597 }
3598 return TRUE;
3599 }
3600
3601 return FALSE;
3602}
3603
3604
2d66e025 3605void wxGrid::CalcRowLabelsExposed( wxRegion& reg )
f85afd4e 3606{
2d66e025
MB
3607 wxRegionIterator iter( reg );
3608 wxRect r;
f85afd4e 3609
2d66e025 3610 m_rowLabelsExposed.Empty();
f85afd4e 3611
2d66e025
MB
3612 int top, bottom;
3613 while ( iter )
f85afd4e 3614 {
2d66e025 3615 r = iter.GetRect();
f85afd4e 3616
2d66e025
MB
3617 // TODO: remove this when we can...
3618 // There is a bug in wxMotif that gives garbage update
3619 // rectangles if you jump-scroll a long way by clicking the
3620 // scrollbar with middle button. This is a work-around
3621 //
3622#if defined(__WXMOTIF__)
3623 int cw, ch;
3624 m_gridWin->GetClientSize( &cw, &ch );
3625 if ( r.GetTop() > ch ) r.SetTop( 0 );
3626 r.SetBottom( wxMin( r.GetBottom(), ch ) );
3627#endif
f85afd4e 3628
2d66e025
MB
3629 // logical bounds of update region
3630 //
3631 int dummy;
3632 CalcUnscrolledPosition( 0, r.GetTop(), &dummy, &top );
3633 CalcUnscrolledPosition( 0, r.GetBottom(), &dummy, &bottom );
3634
3635 // find the row labels within these bounds
3636 //
3637 int row;
2d66e025
MB
3638 for ( row = 0; row < m_numRows; row++ )
3639 {
7c1cb261
VZ
3640 if ( GetRowBottom(row) < top )
3641 continue;
2d66e025 3642
6d55126d 3643 if ( GetRowTop(row) > bottom )
7c1cb261 3644 break;
60ff3b99 3645
2d66e025
MB
3646 m_rowLabelsExposed.Add( row );
3647 }
60ff3b99 3648
2d66e025 3649 iter++ ;
f85afd4e
MB
3650 }
3651}
3652
3653
2d66e025 3654void wxGrid::CalcColLabelsExposed( wxRegion& reg )
f85afd4e 3655{
2d66e025
MB
3656 wxRegionIterator iter( reg );
3657 wxRect r;
f85afd4e 3658
2d66e025 3659 m_colLabelsExposed.Empty();
f85afd4e 3660
2d66e025
MB
3661 int left, right;
3662 while ( iter )
f85afd4e 3663 {
2d66e025 3664 r = iter.GetRect();
f85afd4e 3665
2d66e025
MB
3666 // TODO: remove this when we can...
3667 // There is a bug in wxMotif that gives garbage update
3668 // rectangles if you jump-scroll a long way by clicking the
3669 // scrollbar with middle button. This is a work-around
3670 //
3671#if defined(__WXMOTIF__)
3672 int cw, ch;
3673 m_gridWin->GetClientSize( &cw, &ch );
3674 if ( r.GetLeft() > cw ) r.SetLeft( 0 );
3675 r.SetRight( wxMin( r.GetRight(), cw ) );
3676#endif
3677
3678 // logical bounds of update region
3679 //
3680 int dummy;
3681 CalcUnscrolledPosition( r.GetLeft(), 0, &left, &dummy );
3682 CalcUnscrolledPosition( r.GetRight(), 0, &right, &dummy );
3683
3684 // find the cells within these bounds
3685 //
3686 int col;
2d66e025
MB
3687 for ( col = 0; col < m_numCols; col++ )
3688 {
7c1cb261
VZ
3689 if ( GetColRight(col) < left )
3690 continue;
60ff3b99 3691
7c1cb261
VZ
3692 if ( GetColLeft(col) > right )
3693 break;
2d66e025
MB
3694
3695 m_colLabelsExposed.Add( col );
3696 }
60ff3b99 3697
2d66e025 3698 iter++ ;
f85afd4e
MB
3699 }
3700}
3701
3702
2d66e025 3703void wxGrid::CalcCellsExposed( wxRegion& reg )
f85afd4e 3704{
2d66e025
MB
3705 wxRegionIterator iter( reg );
3706 wxRect r;
f85afd4e 3707
2d66e025
MB
3708 m_cellsExposed.Empty();
3709 m_rowsExposed.Empty();
3710 m_colsExposed.Empty();
f85afd4e 3711
2d66e025
MB
3712 int left, top, right, bottom;
3713 while ( iter )
3714 {
3715 r = iter.GetRect();
f85afd4e 3716
2d66e025
MB
3717 // TODO: remove this when we can...
3718 // There is a bug in wxMotif that gives garbage update
3719 // rectangles if you jump-scroll a long way by clicking the
3720 // scrollbar with middle button. This is a work-around
3721 //
3722#if defined(__WXMOTIF__)
f85afd4e 3723 int cw, ch;
2d66e025
MB
3724 m_gridWin->GetClientSize( &cw, &ch );
3725 if ( r.GetTop() > ch ) r.SetTop( 0 );
3726 if ( r.GetLeft() > cw ) r.SetLeft( 0 );
3727 r.SetRight( wxMin( r.GetRight(), cw ) );
3728 r.SetBottom( wxMin( r.GetBottom(), ch ) );
3729#endif
8f177c8e 3730
2d66e025
MB
3731 // logical bounds of update region
3732 //
3733 CalcUnscrolledPosition( r.GetLeft(), r.GetTop(), &left, &top );
3734 CalcUnscrolledPosition( r.GetRight(), r.GetBottom(), &right, &bottom );
f85afd4e 3735
2d66e025 3736 // find the cells within these bounds
f85afd4e 3737 //
2d66e025 3738 int row, col;
2d66e025 3739 for ( row = 0; row < m_numRows; row++ )
f85afd4e 3740 {
7c1cb261
VZ
3741 if ( GetRowBottom(row) <= top )
3742 continue;
f85afd4e 3743
7c1cb261
VZ
3744 if ( GetRowTop(row) > bottom )
3745 break;
60ff3b99 3746
2d66e025 3747 m_rowsExposed.Add( row );
f85afd4e 3748
2d66e025
MB
3749 for ( col = 0; col < m_numCols; col++ )
3750 {
7c1cb261
VZ
3751 if ( GetColRight(col) <= left )
3752 continue;
60ff3b99 3753
7c1cb261
VZ
3754 if ( GetColLeft(col) > right )
3755 break;
60ff3b99 3756
7c1cb261
VZ
3757 if ( m_colsExposed.Index( col ) == wxNOT_FOUND )
3758 m_colsExposed.Add( col );
2d66e025
MB
3759 m_cellsExposed.Add( wxGridCellCoords( row, col ) );
3760 }
3761 }
60ff3b99 3762
7c1cb261 3763 iter++;
f85afd4e
MB
3764 }
3765}
3766
3767
2d66e025 3768void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent& event )
f85afd4e 3769{
2d66e025
MB
3770 int x, y, row;
3771 wxPoint pos( event.GetPosition() );
3772 CalcUnscrolledPosition( pos.x, pos.y, &x, &y );
60ff3b99 3773
2d66e025 3774 if ( event.Dragging() )
f85afd4e
MB
3775 {
3776 m_isDragging = TRUE;
8f177c8e 3777
2d66e025 3778 if ( event.LeftIsDown() )
f85afd4e
MB
3779 {
3780 switch( m_cursorMode )
3781 {
f85afd4e
MB
3782 case WXGRID_CURSOR_RESIZE_ROW:
3783 {
2d66e025
MB
3784 int cw, ch, left, dummy;
3785 m_gridWin->GetClientSize( &cw, &ch );
3786 CalcUnscrolledPosition( 0, 0, &left, &dummy );
60ff3b99 3787
2d66e025
MB
3788 wxClientDC dc( m_gridWin );
3789 PrepareDC( dc );
af547d51
VZ
3790 y = wxMax( y,
3791 GetRowTop(m_dragRowOrCol) +
3792 GetRowMinimalHeight(m_dragRowOrCol) );
d2fdd8d2 3793 dc.SetLogicalFunction(wxINVERT);
f85afd4e
MB
3794 if ( m_dragLastPos >= 0 )
3795 {
2d66e025 3796 dc.DrawLine( left, m_dragLastPos, left+cw, m_dragLastPos );
f85afd4e 3797 }
2d66e025
MB
3798 dc.DrawLine( left, y, left+cw, y );
3799 m_dragLastPos = y;
f85afd4e
MB
3800 }
3801 break;
3802
3803 case WXGRID_CURSOR_SELECT_ROW:
f85afd4e
MB
3804 if ( (row = YToRow( y )) >= 0 &&
3805 !IsInSelection( row, 0 ) )
3806 {
3807 SelectRow( row, TRUE );
3808 }
e2b42eeb
VZ
3809
3810 // default label to suppress warnings about "enumeration value
3811 // 'xxx' not handled in switch
3812 default:
3813 break;
f85afd4e
MB
3814 }
3815 }
3816 return;
3817 }
3818
3819 m_isDragging = FALSE;
8f177c8e 3820
60ff3b99 3821
6d004f67
MB
3822 // ------------ Entering or leaving the window
3823 //
3824 if ( event.Entering() || event.Leaving() )
3825 {
e2b42eeb 3826 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_rowLabelWin);
6d004f67
MB
3827 }
3828
e2b42eeb 3829
2d66e025 3830 // ------------ Left button pressed
f85afd4e 3831 //
6d004f67 3832 else if ( event.LeftDown() )
f85afd4e 3833 {
2d66e025
MB
3834 // don't send a label click event for a hit on the
3835 // edge of the row label - this is probably the user
3836 // wanting to resize the row
3837 //
3838 if ( YToEdgeOfRow(y) < 0 )
f85afd4e 3839 {
2d66e025 3840 row = YToRow(y);
58dd5b3b 3841 if ( row >= 0 &&
b54ba671 3842 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK, row, -1, event ) )
f85afd4e 3843 {
2d66e025 3844 SelectRow( row, event.ShiftDown() );
e2b42eeb 3845 ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW, m_rowLabelWin);
f85afd4e 3846 }
2d66e025
MB
3847 }
3848 else
3849 {
3850 // starting to drag-resize a row
3851 //
6e8524b1
MB
3852 if ( CanDragRowSize() )
3853 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW, m_rowLabelWin);
2d66e025
MB
3854 }
3855 }
f85afd4e 3856
f85afd4e 3857
2d66e025
MB
3858 // ------------ Left double click
3859 //
3860 else if (event.LeftDClick() )
3861 {
3862 if ( YToEdgeOfRow(y) < 0 )
3863 {
3864 row = YToRow(y);
b54ba671 3865 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK, row, -1, event );
f85afd4e
MB
3866 }
3867 }
60ff3b99
VZ
3868
3869
2d66e025 3870 // ------------ Left button released
f85afd4e 3871 //
2d66e025 3872 else if ( event.LeftUp() )
f85afd4e 3873 {
2d66e025 3874 if ( m_cursorMode == WXGRID_CURSOR_RESIZE_ROW )
f85afd4e 3875 {
6d004f67 3876 DoEndDragResizeRow();
60ff3b99 3877
6d004f67
MB
3878 // Note: we are ending the event *after* doing
3879 // default processing in this case
3880 //
b54ba671 3881 SendEvent( wxEVT_GRID_ROW_SIZE, m_dragRowOrCol, -1, event );
2d66e025 3882 }
f85afd4e 3883
e2b42eeb
VZ
3884 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_rowLabelWin);
3885 m_dragLastPos = -1;
2d66e025 3886 }
f85afd4e 3887
f85afd4e 3888
2d66e025
MB
3889 // ------------ Right button down
3890 //
3891 else if ( event.RightDown() )
3892 {
3893 row = YToRow(y);
b54ba671 3894 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK, row, -1, event ) )
2d66e025
MB
3895 {
3896 // no default action at the moment
f85afd4e
MB
3897 }
3898 }
60ff3b99
VZ
3899
3900
2d66e025 3901 // ------------ Right double click
f85afd4e 3902 //
2d66e025
MB
3903 else if ( event.RightDClick() )
3904 {
3905 row = YToRow(y);
b54ba671 3906 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK, row, -1, event ) )
2d66e025
MB
3907 {
3908 // no default action at the moment
3909 }
3910 }
60ff3b99
VZ
3911
3912
2d66e025 3913 // ------------ No buttons down and mouse moving
f85afd4e 3914 //
2d66e025 3915 else if ( event.Moving() )
f85afd4e 3916 {
2d66e025
MB
3917 m_dragRowOrCol = YToEdgeOfRow( y );
3918 if ( m_dragRowOrCol >= 0 )
8f177c8e 3919 {
2d66e025 3920 if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
8f177c8e 3921 {
e2b42eeb 3922 // don't capture the mouse yet
6e8524b1
MB
3923 if ( CanDragRowSize() )
3924 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW, m_rowLabelWin, FALSE);
8f177c8e 3925 }
2d66e025 3926 }
6d004f67 3927 else if ( m_cursorMode != WXGRID_CURSOR_SELECT_CELL )
2d66e025 3928 {
e2b42eeb 3929 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_rowLabelWin, FALSE);
8f177c8e 3930 }
f85afd4e 3931 }
2d66e025
MB
3932}
3933
3934
3935void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent& event )
3936{
3937 int x, y, col;
3938 wxPoint pos( event.GetPosition() );
3939 CalcUnscrolledPosition( pos.x, pos.y, &x, &y );
60ff3b99 3940
2d66e025 3941 if ( event.Dragging() )
f85afd4e 3942 {
2d66e025 3943 m_isDragging = TRUE;
8f177c8e 3944
2d66e025 3945 if ( event.LeftIsDown() )
8f177c8e 3946 {
2d66e025 3947 switch( m_cursorMode )
8f177c8e 3948 {
2d66e025 3949 case WXGRID_CURSOR_RESIZE_COL:
8f177c8e 3950 {
2d66e025
MB
3951 int cw, ch, dummy, top;
3952 m_gridWin->GetClientSize( &cw, &ch );
3953 CalcUnscrolledPosition( 0, 0, &dummy, &top );
60ff3b99 3954
2d66e025
MB
3955 wxClientDC dc( m_gridWin );
3956 PrepareDC( dc );
43947979
VZ
3957
3958 x = wxMax( x, GetColLeft(m_dragRowOrCol) +
3959 GetColMinimalWidth(m_dragRowOrCol));
d2fdd8d2 3960 dc.SetLogicalFunction(wxINVERT);
2d66e025
MB
3961 if ( m_dragLastPos >= 0 )
3962 {
3963 dc.DrawLine( m_dragLastPos, top, m_dragLastPos, top+ch );
3964 }
3965 dc.DrawLine( x, top, x, top+ch );
3966 m_dragLastPos = x;
f85afd4e 3967 }
2d66e025 3968 break;
f85afd4e 3969
2d66e025 3970 case WXGRID_CURSOR_SELECT_COL:
2d66e025
MB
3971 if ( (col = XToCol( x )) >= 0 &&
3972 !IsInSelection( 0, col ) )
3973 {
3974 SelectCol( col, TRUE );
3975 }
e2b42eeb
VZ
3976
3977 // default label to suppress warnings about "enumeration value
3978 // 'xxx' not handled in switch
3979 default:
3980 break;
2d66e025 3981 }
f85afd4e 3982 }
2d66e025 3983 return;
f85afd4e 3984 }
2d66e025
MB
3985
3986 m_isDragging = FALSE;
3987
60ff3b99 3988
6d004f67
MB
3989 // ------------ Entering or leaving the window
3990 //
3991 if ( event.Entering() || event.Leaving() )
3992 {
e2b42eeb 3993 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_colLabelWin);
6d004f67
MB
3994 }
3995
e2b42eeb 3996
2d66e025 3997 // ------------ Left button pressed
f85afd4e 3998 //
6d004f67 3999 else if ( event.LeftDown() )
f85afd4e 4000 {
2d66e025
MB
4001 // don't send a label click event for a hit on the
4002 // edge of the col label - this is probably the user
4003 // wanting to resize the col
4004 //
4005 if ( XToEdgeOfCol(x) < 0 )
8f177c8e 4006 {
2d66e025 4007 col = XToCol(x);
58dd5b3b 4008 if ( col >= 0 &&
b54ba671 4009 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK, -1, col, event ) )
8f177c8e 4010 {
2d66e025 4011 SelectCol( col, event.ShiftDown() );
e2b42eeb 4012 ChangeCursorMode(WXGRID_CURSOR_SELECT_COL, m_colLabelWin);
f85afd4e 4013 }
2d66e025
MB
4014 }
4015 else
4016 {
4017 // starting to drag-resize a col
4018 //
6e8524b1
MB
4019 if ( CanDragColSize() )
4020 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL, m_colLabelWin);
2d66e025
MB
4021 }
4022 }
f85afd4e 4023
f85afd4e 4024
2d66e025
MB
4025 // ------------ Left double click
4026 //
4027 if ( event.LeftDClick() )
4028 {
4029 if ( XToEdgeOfCol(x) < 0 )
4030 {
4031 col = XToCol(x);
b54ba671 4032 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK, -1, col, event );
2d66e025
MB
4033 }
4034 }
60ff3b99
VZ
4035
4036
2d66e025
MB
4037 // ------------ Left button released
4038 //
4039 else if ( event.LeftUp() )
4040 {
4041 if ( m_cursorMode == WXGRID_CURSOR_RESIZE_COL )
4042 {
6d004f67 4043 DoEndDragResizeCol();
e2b42eeb 4044
6d004f67
MB
4045 // Note: we are ending the event *after* doing
4046 // default processing in this case
4047 //
b54ba671 4048 SendEvent( wxEVT_GRID_COL_SIZE, -1, m_dragRowOrCol, event );
2d66e025 4049 }
f85afd4e 4050
e2b42eeb 4051 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_colLabelWin);
2d66e025 4052 m_dragLastPos = -1;
60ff3b99
VZ
4053 }
4054
4055
2d66e025
MB
4056 // ------------ Right button down
4057 //
4058 else if ( event.RightDown() )
4059 {
4060 col = XToCol(x);
b54ba671 4061 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK, -1, col, event ) )
2d66e025
MB
4062 {
4063 // no default action at the moment
f85afd4e
MB
4064 }
4065 }
60ff3b99
VZ
4066
4067
2d66e025 4068 // ------------ Right double click
f85afd4e 4069 //
2d66e025
MB
4070 else if ( event.RightDClick() )
4071 {
4072 col = XToCol(x);
b54ba671 4073 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK, -1, col, event ) )
2d66e025
MB
4074 {
4075 // no default action at the moment
4076 }
4077 }
60ff3b99
VZ
4078
4079
2d66e025 4080 // ------------ No buttons down and mouse moving
f85afd4e 4081 //
2d66e025 4082 else if ( event.Moving() )
f85afd4e 4083 {
2d66e025
MB
4084 m_dragRowOrCol = XToEdgeOfCol( x );
4085 if ( m_dragRowOrCol >= 0 )
f85afd4e 4086 {
2d66e025 4087 if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
f85afd4e 4088 {
e2b42eeb 4089 // don't capture the cursor yet
6e8524b1
MB
4090 if ( CanDragColSize() )
4091 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL, m_colLabelWin, FALSE);
f85afd4e 4092 }
2d66e025 4093 }
6d004f67 4094 else if ( m_cursorMode != WXGRID_CURSOR_SELECT_CELL )
2d66e025 4095 {
e2b42eeb 4096 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_colLabelWin, FALSE);
8f177c8e 4097 }
f85afd4e
MB
4098 }
4099}
4100
4101
2d66e025 4102void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent& event )
f85afd4e 4103{
2d66e025 4104 if ( event.LeftDown() )
f85afd4e 4105 {
2d66e025
MB
4106 // indicate corner label by having both row and
4107 // col args == -1
f85afd4e 4108 //
b54ba671 4109 if ( !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK, -1, -1, event ) )
2d66e025
MB
4110 {
4111 SelectAll();
4112 }
f85afd4e
MB
4113 }
4114
2d66e025
MB
4115 else if ( event.LeftDClick() )
4116 {
b54ba671 4117 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK, -1, -1, event );
2d66e025 4118 }
8f177c8e 4119
2d66e025 4120 else if ( event.RightDown() )
f85afd4e 4121 {
b54ba671 4122 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK, -1, -1, event ) )
f85afd4e 4123 {
2d66e025
MB
4124 // no default action at the moment
4125 }
4126 }
f85afd4e 4127
2d66e025
MB
4128 else if ( event.RightDClick() )
4129 {
b54ba671 4130 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK, -1, -1, event ) )
2d66e025
MB
4131 {
4132 // no default action at the moment
4133 }
4134 }
4135}
f85afd4e 4136
e2b42eeb
VZ
4137void wxGrid::ChangeCursorMode(CursorMode mode,
4138 wxWindow *win,
4139 bool captureMouse)
4140{
4141#ifdef __WXDEBUG__
4142 static const wxChar *cursorModes[] =
4143 {
4144 _T("SELECT_CELL"),
4145 _T("RESIZE_ROW"),
4146 _T("RESIZE_COL"),
4147 _T("SELECT_ROW"),
4148 _T("SELECT_COL")
4149 };
4150
181bfffd
VZ
4151 wxLogTrace(_T("grid"),
4152 _T("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
e2b42eeb
VZ
4153 win == m_colLabelWin ? _T("colLabelWin")
4154 : win ? _T("rowLabelWin")
4155 : _T("gridWin"),
4156 cursorModes[m_cursorMode], cursorModes[mode]);
4157#endif // __WXDEBUG__
4158
4159 if ( mode == m_cursorMode )
4160 return;
4161
4162 if ( !win )
4163 {
4164 // by default use the grid itself
4165 win = m_gridWin;
4166 }
4167
4168 if ( m_winCapture )
4169 {
4170 m_winCapture->ReleaseMouse();
4171 m_winCapture = (wxWindow *)NULL;
4172 }
4173
4174 m_cursorMode = mode;
4175
4176 switch ( m_cursorMode )
4177 {
4178 case WXGRID_CURSOR_RESIZE_ROW:
4179 win->SetCursor( m_rowResizeCursor );
4180 break;
4181
4182 case WXGRID_CURSOR_RESIZE_COL:
4183 win->SetCursor( m_colResizeCursor );
4184 break;
4185
4186 default:
4187 win->SetCursor( *wxSTANDARD_CURSOR );
4188 }
4189
4190 // we need to capture mouse when resizing
4191 bool resize = m_cursorMode == WXGRID_CURSOR_RESIZE_ROW ||
4192 m_cursorMode == WXGRID_CURSOR_RESIZE_COL;
4193
4194 if ( captureMouse && resize )
4195 {
4196 win->CaptureMouse();
4197 m_winCapture = win;
4198 }
4199}
8f177c8e 4200
2d66e025
MB
4201void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent& event )
4202{
4203 int x, y;
4204 wxPoint pos( event.GetPosition() );
4205 CalcUnscrolledPosition( pos.x, pos.y, &x, &y );
60ff3b99 4206
2d66e025
MB
4207 wxGridCellCoords coords;
4208 XYToCell( x, y, coords );
60ff3b99 4209
2d66e025
MB
4210 if ( event.Dragging() )
4211 {
07296f0b
RD
4212 //wxLogDebug("pos(%d, %d) coords(%d, %d)", pos.x, pos.y, coords.GetRow(), coords.GetCol());
4213
4214 // Don't start doing anything until the mouse has been drug at
4215 // least 3 pixels in any direction...
508011ce
VZ
4216 if (! m_isDragging)
4217 {
4218 if (m_startDragPos == wxDefaultPosition)
4219 {
07296f0b
RD
4220 m_startDragPos = pos;
4221 return;
4222 }
4223 if (abs(m_startDragPos.x - pos.x) < 4 && abs(m_startDragPos.y - pos.y) < 4)
4224 return;
4225 }
4226
2d66e025 4227 m_isDragging = TRUE;
2d66e025
MB
4228 if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
4229 {
f0102d2a
VZ
4230 // Hide the edit control, so it
4231 // won't interfer with drag-shrinking.
4232 if ( IsCellEditControlEnabled() )
a5777624 4233 {
f0102d2a 4234 HideCellEditControl();
a5777624
RD
4235 SaveEditControlValue();
4236 }
07296f0b
RD
4237
4238 // Have we captured the mouse yet?
508011ce
VZ
4239 if (! m_winCapture)
4240 {
07296f0b
RD
4241 m_winCapture = m_gridWin;
4242 m_winCapture->CaptureMouse();
4243 }
4244
2d66e025
MB
4245 if ( coords != wxGridNoCellCoords )
4246 {
4247 if ( !IsSelection() )
f85afd4e 4248 {
2d66e025 4249 SelectBlock( coords, coords );
f85afd4e
MB
4250 }
4251 else
4252 {
f0102d2a 4253 SelectBlock( m_currentCellCoords, coords );
f85afd4e 4254 }
07296f0b 4255
508011ce
VZ
4256 if (! IsVisible(coords))
4257 {
07296f0b
RD
4258 MakeCellVisible(coords);
4259 // TODO: need to introduce a delay or something here. The
4260 // scrolling is way to fast, at least on MSW.
4261 }
2d66e025
MB
4262 }
4263 }
6d004f67
MB
4264 else if ( m_cursorMode == WXGRID_CURSOR_RESIZE_ROW )
4265 {
4266 int cw, ch, left, dummy;
4267 m_gridWin->GetClientSize( &cw, &ch );
4268 CalcUnscrolledPosition( 0, 0, &left, &dummy );
8f177c8e 4269
6d004f67
MB
4270 wxClientDC dc( m_gridWin );
4271 PrepareDC( dc );
a95e38c0
VZ
4272 y = wxMax( y, GetRowTop(m_dragRowOrCol) +
4273 GetRowMinimalHeight(m_dragRowOrCol) );
6d004f67
MB
4274 dc.SetLogicalFunction(wxINVERT);
4275 if ( m_dragLastPos >= 0 )
4276 {
4277 dc.DrawLine( left, m_dragLastPos, left+cw, m_dragLastPos );
4278 }
4279 dc.DrawLine( left, y, left+cw, y );
4280 m_dragLastPos = y;
4281 }
4282 else if ( m_cursorMode == WXGRID_CURSOR_RESIZE_COL )
4283 {
4284 int cw, ch, dummy, top;
4285 m_gridWin->GetClientSize( &cw, &ch );
4286 CalcUnscrolledPosition( 0, 0, &dummy, &top );
e2b42eeb 4287
6d004f67
MB
4288 wxClientDC dc( m_gridWin );
4289 PrepareDC( dc );
43947979
VZ
4290 x = wxMax( x, GetColLeft(m_dragRowOrCol) +
4291 GetColMinimalWidth(m_dragRowOrCol) );
6d004f67
MB
4292 dc.SetLogicalFunction(wxINVERT);
4293 if ( m_dragLastPos >= 0 )
4294 {
4295 dc.DrawLine( m_dragLastPos, top, m_dragLastPos, top+ch );
4296 }
4297 dc.DrawLine( x, top, x, top+ch );
4298 m_dragLastPos = x;
4299 }
e2b42eeb 4300
2d66e025
MB
4301 return;
4302 }
66242c80 4303
2d66e025 4304 m_isDragging = FALSE;
07296f0b
RD
4305 m_startDragPos = wxDefaultPosition;
4306
a5777624
RD
4307 // VZ: if we do this, the mode is reset to WXGRID_CURSOR_SELECT_CELL
4308 // immediately after it becomes WXGRID_CURSOR_RESIZE_ROW/COL under
4309 // wxGTK
e2b42eeb 4310#if 0
a5777624
RD
4311 if ( event.Entering() || event.Leaving() )
4312 {
4313 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL);
4314 m_gridWin->SetCursor( *wxSTANDARD_CURSOR );
4315 }
4316 else
e2b42eeb
VZ
4317#endif // 0
4318
a5777624
RD
4319 // ------------ Left button pressed
4320 //
4321 if ( event.LeftDown() && coords != wxGridNoCellCoords )
4322 {
b5808881
SN
4323 if ( !event.ShiftDown() && !event.ControlDown() )
4324 ClearSelection();
a5777624 4325 if ( event.ShiftDown() )
2d66e025 4326 {
5c8fc7c1
SN
4327 m_selection->SelectBlock( m_currentCellCoords.GetRow(),
4328 m_currentCellCoords.GetCol(),
4329 coords.GetRow(),
4330 coords.GetCol() );
a5777624
RD
4331 }
4332 else if ( XToEdgeOfCol(x) < 0 &&
4333 YToEdgeOfRow(y) < 0 )
4334 {
4335 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_CLICK,
4336 coords.GetRow(),
4337 coords.GetCol(),
4338 event ) )
58dd5b3b 4339 {
a5777624
RD
4340 DisableCellEditControl();
4341 MakeCellVisible( coords );
4342
4343 // if this is the second click on this cell then start
4344 // the edit control
4345 if ( m_waitForSlowClick &&
4346 (coords == m_currentCellCoords) &&
4347 CanEnableCellControl())
58dd5b3b 4348 {
a5777624 4349 EnableCellEditControl();
e195a54c 4350
a5777624 4351 wxGridCellAttr* attr = GetCellAttr(m_currentCellCoords);
0b190b0f
VZ
4352 wxGridCellEditor *editor = attr->GetEditor(this,
4353 coords.GetRow(),
4354 coords.GetCol());
4355 editor->StartingClick();
4356 editor->DecRef();
a5777624 4357 attr->DecRef();
e195a54c 4358
a5777624
RD
4359 m_waitForSlowClick = FALSE;
4360 }
4361 else
4362 {
b5808881
SN
4363 m_selection->ToggleCellSelection( coords.GetRow(),
4364 coords.GetCol() );
4365 m_selectingTopLeft = wxGridNoCellCoords;
4366 m_selectingBottomRight = wxGridNoCellCoords;
a5777624
RD
4367 SetCurrentCell( coords );
4368 m_waitForSlowClick = TRUE;
58dd5b3b
MB
4369 }
4370 }
f85afd4e 4371 }
a5777624 4372 }
f85afd4e 4373
f85afd4e 4374
a5777624
RD
4375 // ------------ Left double click
4376 //
4377 else if ( event.LeftDClick() && coords != wxGridNoCellCoords )
4378 {
4379 DisableCellEditControl();
4380
4381 if ( XToEdgeOfCol(x) < 0 && YToEdgeOfRow(y) < 0 )
58dd5b3b 4382 {
a5777624
RD
4383 SendEvent( wxEVT_GRID_CELL_LEFT_DCLICK,
4384 coords.GetRow(),
4385 coords.GetCol(),
4386 event );
58dd5b3b 4387 }
a5777624 4388 }
f85afd4e 4389
8f177c8e 4390
a5777624
RD
4391 // ------------ Left button released
4392 //
4393 else if ( event.LeftUp() )
4394 {
4395 if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
f85afd4e 4396 {
b5808881
SN
4397 if ( m_selectingTopLeft != wxGridNoCellCoords &&
4398 m_selectingBottomRight != wxGridNoCellCoords )
52068ea5 4399 {
a5777624 4400 if (m_winCapture)
58dd5b3b 4401 {
a5777624
RD
4402 m_winCapture->ReleaseMouse();
4403 m_winCapture = NULL;
58dd5b3b 4404 }
5c8fc7c1
SN
4405 m_selection->SelectBlock( m_selectingTopLeft.GetRow(),
4406 m_selectingTopLeft.GetCol(),
4407 m_selectingBottomRight.GetRow(),
4408 m_selectingBottomRight.GetCol(),
4409 & event );
4410 m_selectingTopLeft = wxGridNoCellCoords;
4411 m_selectingBottomRight = wxGridNoCellCoords;
6d004f67 4412 }
2d66e025 4413
a5777624
RD
4414 // Show the edit control, if it has been hidden for
4415 // drag-shrinking.
4416 ShowCellEditControl();
4417 }
4418 else if ( m_cursorMode == WXGRID_CURSOR_RESIZE_ROW )
4419 {
4420 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL);
4421 DoEndDragResizeRow();
e2b42eeb 4422
a5777624
RD
4423 // Note: we are ending the event *after* doing
4424 // default processing in this case
4425 //
4426 SendEvent( wxEVT_GRID_ROW_SIZE, m_dragRowOrCol, -1, event );
4427 }
4428 else if ( m_cursorMode == WXGRID_CURSOR_RESIZE_COL )
4429 {
4430 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL);
4431 DoEndDragResizeCol();
e2b42eeb 4432
a5777624
RD
4433 // Note: we are ending the event *after* doing
4434 // default processing in this case
4435 //
4436 SendEvent( wxEVT_GRID_COL_SIZE, -1, m_dragRowOrCol, event );
58dd5b3b 4437 }
f85afd4e 4438
a5777624
RD
4439 m_dragLastPos = -1;
4440 }
4441
f85afd4e 4442
a5777624
RD
4443 // ------------ Right button down
4444 //
4445 else if ( event.RightDown() && coords != wxGridNoCellCoords )
4446 {
4447 DisableCellEditControl();
4448 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_CLICK,
4449 coords.GetRow(),
4450 coords.GetCol(),
4451 event ) )
f85afd4e 4452 {
a5777624 4453 // no default action at the moment
60ff3b99 4454 }
a5777624 4455 }
2d66e025 4456
60ff3b99 4457
a5777624
RD
4458 // ------------ Right double click
4459 //
4460 else if ( event.RightDClick() && coords != wxGridNoCellCoords )
4461 {
4462 DisableCellEditControl();
4463 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_DCLICK,
4464 coords.GetRow(),
4465 coords.GetCol(),
4466 event ) )
f85afd4e 4467 {
a5777624 4468 // no default action at the moment
60ff3b99 4469 }
a5777624
RD
4470 }
4471
4472 // ------------ Moving and no button action
4473 //
4474 else if ( event.Moving() && !event.IsButton() )
4475 {
4476 int dragRow = YToEdgeOfRow( y );
4477 int dragCol = XToEdgeOfCol( x );
790cc417 4478
a5777624
RD
4479 // Dragging on the corner of a cell to resize in both
4480 // directions is not implemented yet...
790cc417 4481 //
a5777624 4482 if ( dragRow >= 0 && dragCol >= 0 )
790cc417 4483 {
a5777624
RD
4484 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL);
4485 return;
4486 }
6d004f67 4487
a5777624
RD
4488 if ( dragRow >= 0 )
4489 {
4490 m_dragRowOrCol = dragRow;
6d004f67 4491
a5777624 4492 if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
6d004f67 4493 {
a5777624
RD
4494 if ( CanDragRowSize() && CanDragGridSize() )
4495 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW);
6d004f67 4496 }
e2b42eeb 4497
b94ae1ea
VZ
4498 if ( dragCol >= 0 )
4499 {
4500 m_dragRowOrCol = dragCol;
122603f1 4501 }
b94ae1ea 4502
a5777624
RD
4503 return;
4504 }
e2b42eeb 4505
a5777624
RD
4506 if ( dragCol >= 0 )
4507 {
4508 m_dragRowOrCol = dragCol;
e2b42eeb 4509
a5777624 4510 if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
6d004f67 4511 {
a5777624
RD
4512 if ( CanDragColSize() && CanDragGridSize() )
4513 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL);
6d004f67 4514 }
a5777624
RD
4515
4516 return;
6d004f67 4517 }
a5777624
RD
4518
4519 // Neither on a row or col edge
4520 //
4521 if ( m_cursorMode != WXGRID_CURSOR_SELECT_CELL )
4522 {
4523 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL);
4524 }
4525 }
6d004f67
MB
4526}
4527
4528
4529void wxGrid::DoEndDragResizeRow()
4530{
4531 if ( m_dragLastPos >= 0 )
4532 {
4533 // erase the last line and resize the row
4534 //
4535 int cw, ch, left, dummy;
4536 m_gridWin->GetClientSize( &cw, &ch );
4537 CalcUnscrolledPosition( 0, 0, &left, &dummy );
4538
4539 wxClientDC dc( m_gridWin );
4540 PrepareDC( dc );
4541 dc.SetLogicalFunction( wxINVERT );
4542 dc.DrawLine( left, m_dragLastPos, left+cw, m_dragLastPos );
4543 HideCellEditControl();
a5777624 4544 SaveEditControlValue();
6d004f67 4545
7c1cb261 4546 int rowTop = GetRowTop(m_dragRowOrCol);
6d004f67
MB
4547 SetRowSize( m_dragRowOrCol,
4548 wxMax( m_dragLastPos - rowTop, WXGRID_MIN_ROW_HEIGHT ) );
e2b42eeb 4549
6d004f67
MB
4550 if ( !GetBatchCount() )
4551 {
4552 // Only needed to get the correct rect.y:
4553 wxRect rect ( CellToRect( m_dragRowOrCol, 0 ) );
4554 rect.x = 0;
4555 CalcScrolledPosition(0, rect.y, &dummy, &rect.y);
4556 rect.width = m_rowLabelWidth;
4557 rect.height = ch - rect.y;
4558 m_rowLabelWin->Refresh( TRUE, &rect );
4559 rect.width = cw;
4560 m_gridWin->Refresh( FALSE, &rect );
4561 }
4562
4563 ShowCellEditControl();
4564 }
4565}
4566
4567
4568void wxGrid::DoEndDragResizeCol()
4569{
4570 if ( m_dragLastPos >= 0 )
4571 {
4572 // erase the last line and resize the col
4573 //
4574 int cw, ch, dummy, top;
4575 m_gridWin->GetClientSize( &cw, &ch );
4576 CalcUnscrolledPosition( 0, 0, &dummy, &top );
4577
4578 wxClientDC dc( m_gridWin );
4579 PrepareDC( dc );
4580 dc.SetLogicalFunction( wxINVERT );
4581 dc.DrawLine( m_dragLastPos, top, m_dragLastPos, top+ch );
4582 HideCellEditControl();
a5777624 4583 SaveEditControlValue();
6d004f67 4584
7c1cb261 4585 int colLeft = GetColLeft(m_dragRowOrCol);
6d004f67 4586 SetColSize( m_dragRowOrCol,
43947979
VZ
4587 wxMax( m_dragLastPos - colLeft,
4588 GetColMinimalWidth(m_dragRowOrCol) ) );
6d004f67
MB
4589
4590 if ( !GetBatchCount() )
4591 {
4592 // Only needed to get the correct rect.x:
4593 wxRect rect ( CellToRect( 0, m_dragRowOrCol ) );
4594 rect.y = 0;
4595 CalcScrolledPosition(rect.x, 0, &rect.x, &dummy);
4596 rect.width = cw - rect.x;
4597 rect.height = m_colLabelHeight;
4598 m_colLabelWin->Refresh( TRUE, &rect );
4599 rect.height = ch;
4600 m_gridWin->Refresh( FALSE, &rect );
790cc417 4601 }
e2b42eeb 4602
6d004f67 4603 ShowCellEditControl();
f85afd4e 4604 }
f85afd4e
MB
4605}
4606
4607
6d004f67 4608
2d66e025
MB
4609//
4610// ------ interaction with data model
4611//
4612bool wxGrid::ProcessTableMessage( wxGridTableMessage& msg )
f85afd4e 4613{
2d66e025 4614 switch ( msg.GetId() )
17732cec 4615 {
2d66e025
MB
4616 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES:
4617 return GetModelValues();
17732cec 4618
2d66e025
MB
4619 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES:
4620 return SetModelValues();
f85afd4e 4621
2d66e025
MB
4622 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED:
4623 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED:
4624 case wxGRIDTABLE_NOTIFY_ROWS_DELETED:
4625 case wxGRIDTABLE_NOTIFY_COLS_INSERTED:
4626 case wxGRIDTABLE_NOTIFY_COLS_APPENDED:
4627 case wxGRIDTABLE_NOTIFY_COLS_DELETED:
4628 return Redimension( msg );
4629
4630 default:
4631 return FALSE;
f85afd4e 4632 }
2d66e025 4633}
f85afd4e 4634
f85afd4e 4635
f85afd4e 4636
2d66e025
MB
4637// The behaviour of this function depends on the grid table class
4638// Clear() function. For the default wxGridStringTable class the
4639// behavious is to replace all cell contents with wxEmptyString but
4640// not to change the number of rows or cols.
4641//
4642void wxGrid::ClearGrid()
4643{
4644 if ( m_table )
f85afd4e 4645 {
4cfa5de6
RD
4646 if (IsCellEditControlEnabled())
4647 DisableCellEditControl();
4648
2d66e025 4649 m_table->Clear();
1f1ce288 4650 if ( !GetBatchCount() ) m_gridWin->Refresh();
f85afd4e
MB
4651 }
4652}
4653
4654
2d66e025 4655bool wxGrid::InsertRows( int pos, int numRows, bool WXUNUSED(updateLabels) )
f85afd4e 4656{
2d66e025 4657 // TODO: something with updateLabels flag
8f177c8e 4658
2d66e025 4659 if ( !m_created )
f85afd4e 4660 {
bd3c6758 4661 wxFAIL_MSG( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") );
2d66e025
MB
4662 return FALSE;
4663 }
8f177c8e 4664
2d66e025
MB
4665 if ( m_table )
4666 {
b7fff980 4667 if (IsCellEditControlEnabled())
b54ba671 4668 DisableCellEditControl();
b7fff980 4669
2d66e025 4670 bool ok = m_table->InsertRows( pos, numRows );
f85afd4e 4671
2d66e025
MB
4672 // the table will have sent the results of the insert row
4673 // operation to this view object as a grid table message
4674 //
4675 if ( ok )
4676 {
4677 if ( m_numCols == 0 )
f85afd4e 4678 {
2d66e025
MB
4679 m_table->AppendCols( WXGRID_DEFAULT_NUMBER_COLS );
4680 //
4681 // TODO: perhaps instead of appending the default number of cols
4682 // we should remember what the last non-zero number of cols was ?
4683 //
4684 }
8f177c8e 4685
2d66e025
MB
4686 if ( m_currentCellCoords == wxGridNoCellCoords )
4687 {
4688 // if we have just inserted cols into an empty grid the current
4689 // cell will be undefined...
4690 //
4691 SetCurrentCell( 0, 0 );
f85afd4e
MB
4692 }
4693
b5808881 4694 m_selection->UpdateRows( pos, numRows );
2d66e025 4695 if ( !GetBatchCount() ) Refresh();
f85afd4e 4696 }
2d66e025 4697
2d66e025
MB
4698 return ok;
4699 }
4700 else
4701 {
4702 return FALSE;
f85afd4e
MB
4703 }
4704}
4705
4706
2d66e025 4707bool wxGrid::AppendRows( int numRows, bool WXUNUSED(updateLabels) )
f85afd4e 4708{
2d66e025
MB
4709 // TODO: something with updateLabels flag
4710
4711 if ( !m_created )
f85afd4e 4712 {
bd3c6758 4713 wxFAIL_MSG( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") );
2d66e025
MB
4714 return FALSE;
4715 }
4716
4717 if ( m_table && m_table->AppendRows( numRows ) )
4718 {
4719 if ( m_currentCellCoords == wxGridNoCellCoords )
4720 {
4721 // if we have just inserted cols into an empty grid the current
4722 // cell will be undefined...
4723 //
4724 SetCurrentCell( 0, 0 );
4725 }
4726
4727 // the table will have sent the results of the append row
4728 // operation to this view object as a grid table message
4729 //
2d66e025
MB
4730 if ( !GetBatchCount() ) Refresh();
4731 return TRUE;
4732 }
4733 else
4734 {
4735 return FALSE;
f85afd4e
MB
4736 }
4737}
4738
2d66e025
MB
4739
4740bool wxGrid::DeleteRows( int pos, int numRows, bool WXUNUSED(updateLabels) )
f85afd4e 4741{
2d66e025
MB
4742 // TODO: something with updateLabels flag
4743
4744 if ( !m_created )
f85afd4e 4745 {
bd3c6758 4746 wxFAIL_MSG( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") );
2d66e025
MB
4747 return FALSE;
4748 }
4749
b7fff980 4750 if ( m_table )
2d66e025 4751 {
b7fff980 4752 if (IsCellEditControlEnabled())
b54ba671 4753 DisableCellEditControl();
f85afd4e 4754
b7fff980
RD
4755 if (m_table->DeleteRows( pos, numRows ))
4756 {
4757
4758 // the table will have sent the results of the delete row
4759 // operation to this view object as a grid table message
4760 //
b5808881 4761 m_selection->UpdateRows( pos, -((int)numRows) );
b7fff980
RD
4762 if ( !GetBatchCount() ) Refresh();
4763 return TRUE;
4764 }
2d66e025 4765 }
b7fff980 4766 return FALSE;
2d66e025 4767}
f85afd4e 4768
f85afd4e 4769
2d66e025
MB
4770bool wxGrid::InsertCols( int pos, int numCols, bool WXUNUSED(updateLabels) )
4771{
4772 // TODO: something with updateLabels flag
8f177c8e 4773
2d66e025
MB
4774 if ( !m_created )
4775 {
bd3c6758 4776 wxFAIL_MSG( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") );
2d66e025
MB
4777 return FALSE;
4778 }
f85afd4e 4779
2d66e025
MB
4780 if ( m_table )
4781 {
b7fff980 4782 if (IsCellEditControlEnabled())
b54ba671 4783 DisableCellEditControl();
b7fff980 4784
2d66e025
MB
4785 bool ok = m_table->InsertCols( pos, numCols );
4786
4787 // the table will have sent the results of the insert col
4788 // operation to this view object as a grid table message
4789 //
4790 if ( ok )
f85afd4e 4791 {
2d66e025 4792 if ( m_currentCellCoords == wxGridNoCellCoords )
f85afd4e 4793 {
2d66e025
MB
4794 // if we have just inserted cols into an empty grid the current
4795 // cell will be undefined...
4796 //
4797 SetCurrentCell( 0, 0 );
f85afd4e 4798 }
2d66e025 4799
b5808881 4800 m_selection->UpdateCols( pos, numCols );
2d66e025 4801 if ( !GetBatchCount() ) Refresh();
f85afd4e 4802 }
2d66e025 4803
2d66e025
MB
4804 return ok;
4805 }
4806 else
4807 {
4808 return FALSE;
f85afd4e
MB
4809 }
4810}
4811
2d66e025
MB
4812
4813bool wxGrid::AppendCols( int numCols, bool WXUNUSED(updateLabels) )
f85afd4e 4814{
2d66e025
MB
4815 // TODO: something with updateLabels flag
4816
4817 if ( !m_created )
f85afd4e 4818 {
bd3c6758 4819 wxFAIL_MSG( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") );
2d66e025
MB
4820 return FALSE;
4821 }
f85afd4e 4822
2d66e025
MB
4823 if ( m_table && m_table->AppendCols( numCols ) )
4824 {
4825 // the table will have sent the results of the append col
4826 // operation to this view object as a grid table message
4827 //
4828 if ( m_currentCellCoords == wxGridNoCellCoords )
f85afd4e 4829 {
2d66e025
MB
4830 // if we have just inserted cols into an empty grid the current
4831 // cell will be undefined...
4832 //
4833 SetCurrentCell( 0, 0 );
f85afd4e 4834 }
8f177c8e 4835
2d66e025
MB
4836 if ( !GetBatchCount() ) Refresh();
4837 return TRUE;
f85afd4e 4838 }
2d66e025 4839 else
f85afd4e 4840 {
2d66e025 4841 return FALSE;
f85afd4e 4842 }
f85afd4e
MB
4843}
4844
4845
2d66e025 4846bool wxGrid::DeleteCols( int pos, int numCols, bool WXUNUSED(updateLabels) )
f85afd4e 4847{
2d66e025 4848 // TODO: something with updateLabels flag
8f177c8e 4849
2d66e025 4850 if ( !m_created )
f85afd4e 4851 {
bd3c6758 4852 wxFAIL_MSG( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") );
2d66e025 4853 return FALSE;
f85afd4e
MB
4854 }
4855
b7fff980 4856 if ( m_table )
2d66e025 4857 {
b7fff980 4858 if (IsCellEditControlEnabled())
b54ba671 4859 DisableCellEditControl();
8f177c8e 4860
b7fff980
RD
4861 if ( m_table->DeleteCols( pos, numCols ) )
4862 {
4863 // the table will have sent the results of the delete col
4864 // operation to this view object as a grid table message
4865 //
b5808881 4866 m_selection->UpdateCols( pos, -((int)numCols) );
b7fff980
RD
4867 if ( !GetBatchCount() ) Refresh();
4868 return TRUE;
4869 }
f85afd4e 4870 }
b7fff980 4871 return FALSE;
f85afd4e
MB
4872}
4873
4874
2d66e025
MB
4875
4876//
4877// ----- event handlers
f85afd4e 4878//
8f177c8e 4879
2d66e025
MB
4880// Generate a grid event based on a mouse event and
4881// return the result of ProcessEvent()
4882//
4883bool wxGrid::SendEvent( const wxEventType type,
4884 int row, int col,
4885 wxMouseEvent& mouseEv )
4886{
b54ba671 4887 if ( type == wxEVT_GRID_ROW_SIZE || type == wxEVT_GRID_COL_SIZE )
f85afd4e 4888 {
2d66e025
MB
4889 int rowOrCol = (row == -1 ? col : row);
4890
4891 wxGridSizeEvent gridEvt( GetId(),
4892 type,
4893 this,
4894 rowOrCol,
4895 mouseEv.GetX(), mouseEv.GetY(),
4896 mouseEv.ControlDown(),
4897 mouseEv.ShiftDown(),
4898 mouseEv.AltDown(),
4899 mouseEv.MetaDown() );
4900
4901 return GetEventHandler()->ProcessEvent(gridEvt);
f85afd4e 4902 }
b54ba671 4903 else if ( type == wxEVT_GRID_RANGE_SELECT )
2d66e025 4904 {
5c8fc7c1 4905 // Right now, it should _never_ end up here!
2d66e025
MB
4906 wxGridRangeSelectEvent gridEvt( GetId(),
4907 type,
4908 this,
b5808881
SN
4909 m_selectingTopLeft,
4910 m_selectingBottomRight,
5c8fc7c1 4911 TRUE,
2d66e025
MB
4912 mouseEv.ControlDown(),
4913 mouseEv.ShiftDown(),
4914 mouseEv.AltDown(),
4915 mouseEv.MetaDown() );
f85afd4e 4916
2d66e025
MB
4917 return GetEventHandler()->ProcessEvent(gridEvt);
4918 }
4919 else
4920 {
4921 wxGridEvent gridEvt( GetId(),
4922 type,
4923 this,
4924 row, col,
5c8fc7c1 4925 false,
2d66e025
MB
4926 mouseEv.GetX(), mouseEv.GetY(),
4927 mouseEv.ControlDown(),
4928 mouseEv.ShiftDown(),
4929 mouseEv.AltDown(),
4930 mouseEv.MetaDown() );
8f177c8e 4931
2d66e025
MB
4932 return GetEventHandler()->ProcessEvent(gridEvt);
4933 }
f85afd4e
MB
4934}
4935
4936
2d66e025
MB
4937// Generate a grid event of specified type and return the result
4938// of ProcessEvent().
f85afd4e 4939//
2d66e025
MB
4940bool wxGrid::SendEvent( const wxEventType type,
4941 int row, int col )
f85afd4e 4942{
b54ba671 4943 if ( type == wxEVT_GRID_ROW_SIZE || type == wxEVT_GRID_COL_SIZE )
f85afd4e 4944 {
2d66e025 4945 int rowOrCol = (row == -1 ? col : row);
f85afd4e 4946
2d66e025
MB
4947 wxGridSizeEvent gridEvt( GetId(),
4948 type,
4949 this,
4950 rowOrCol );
4951
4952 return GetEventHandler()->ProcessEvent(gridEvt);
4953 }
4954 else
4955 {
4956 wxGridEvent gridEvt( GetId(),
4957 type,
4958 this,
4959 row, col );
8f177c8e 4960
2d66e025
MB
4961 return GetEventHandler()->ProcessEvent(gridEvt);
4962 }
f85afd4e
MB
4963}
4964
4965
2d66e025 4966void wxGrid::OnPaint( wxPaintEvent& WXUNUSED(event) )
f85afd4e 4967{
2d66e025 4968 wxPaintDC dc( this );
f85afd4e 4969
2d66e025
MB
4970 if ( m_currentCellCoords == wxGridNoCellCoords &&
4971 m_numRows && m_numCols )
f85afd4e 4972 {
2d66e025 4973 m_currentCellCoords.Set(0, 0);
2d66e025 4974 ShowCellEditControl();
f85afd4e 4975 }
4634a5d6
MB
4976
4977 m_displayed = TRUE;
8f177c8e 4978}
f85afd4e
MB
4979
4980
18f9565d
MB
4981// This is just here to make sure that CalcDimensions gets called when
4982// the grid view is resized... then the size event is skipped to allow
4983// the box sizers to handle everything
4984//
2d66e025 4985void wxGrid::OnSize( wxSizeEvent& event )
f85afd4e 4986{
7807d81c 4987 CalcWindowSizes();
2d66e025 4988 CalcDimensions();
f85afd4e
MB
4989}
4990
f85afd4e 4991
2d66e025 4992void wxGrid::OnKeyDown( wxKeyEvent& event )
f85afd4e 4993{
2d66e025 4994 if ( m_inOnKeyDown )
f85afd4e 4995 {
2d66e025
MB
4996 // shouldn't be here - we are going round in circles...
4997 //
07296f0b 4998 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") );
f85afd4e
MB
4999 }
5000
2d66e025 5001 m_inOnKeyDown = TRUE;
f85afd4e 5002
2d66e025
MB
5003 // propagate the event up and see if it gets processed
5004 //
5005 wxWindow *parent = GetParent();
5006 wxKeyEvent keyEvt( event );
5007 keyEvt.SetEventObject( parent );
5008
5009 if ( !parent->GetEventHandler()->ProcessEvent( keyEvt ) )
f85afd4e 5010 {
9b4aede2 5011
2d66e025
MB
5012 // try local handlers
5013 //
5014 switch ( event.KeyCode() )
5015 {
5016 case WXK_UP:
5017 if ( event.ControlDown() )
5018 {
5c8fc7c1 5019 MoveCursorUpBlock( event.ShiftDown() );
2d66e025
MB
5020 }
5021 else
5022 {
5c8fc7c1 5023 MoveCursorUp( event.ShiftDown() );
2d66e025
MB
5024 }
5025 break;
f85afd4e 5026
2d66e025
MB
5027 case WXK_DOWN:
5028 if ( event.ControlDown() )
5029 {
5c8fc7c1 5030 MoveCursorDownBlock( event.ShiftDown() );
2d66e025
MB
5031 }
5032 else
5033 {
5c8fc7c1 5034 MoveCursorDown( event.ShiftDown() );
2d66e025
MB
5035 }
5036 break;
8f177c8e 5037
2d66e025
MB
5038 case WXK_LEFT:
5039 if ( event.ControlDown() )
5040 {
5c8fc7c1 5041 MoveCursorLeftBlock( event.ShiftDown() );
2d66e025
MB
5042 }
5043 else
5044 {
5c8fc7c1 5045 MoveCursorLeft( event.ShiftDown() );
2d66e025
MB
5046 }
5047 break;
5048
5049 case WXK_RIGHT:
5050 if ( event.ControlDown() )
5051 {
5c8fc7c1 5052 MoveCursorRightBlock( event.ShiftDown() );
2d66e025
MB
5053 }
5054 else
5055 {
5c8fc7c1 5056 MoveCursorRight( event.ShiftDown() );
2d66e025
MB
5057 }
5058 break;
b99be8fb 5059
2d66e025 5060 case WXK_RETURN:
58dd5b3b
MB
5061 if ( event.ControlDown() )
5062 {
5063 event.Skip(); // to let the edit control have the return
5064 }
5065 else
5066 {
5c8fc7c1 5067 MoveCursorDown( event.ShiftDown() );
58dd5b3b 5068 }
2d66e025
MB
5069 break;
5070
5c8fc7c1
SN
5071 case WXK_ESCAPE:
5072 m_selection->ClearSelection();
5073 break;
5074
2c9a89e0
RD
5075 case WXK_TAB:
5076 if (event.ShiftDown())
5c8fc7c1 5077 MoveCursorLeft( FALSE );
2c9a89e0 5078 else
5c8fc7c1 5079 MoveCursorRight( FALSE );
2c9a89e0
RD
5080 break;
5081
2d66e025
MB
5082 case WXK_HOME:
5083 if ( event.ControlDown() )
5084 {
5085 MakeCellVisible( 0, 0 );
5086 SetCurrentCell( 0, 0 );
5087 }
5088 else
5089 {
5090 event.Skip();
5091 }
5092 break;
5093
5094 case WXK_END:
5095 if ( event.ControlDown() )
5096 {
5097 MakeCellVisible( m_numRows-1, m_numCols-1 );
5098 SetCurrentCell( m_numRows-1, m_numCols-1 );
5099 }
5100 else
5101 {
5102 event.Skip();
5103 }
5104 break;
5105
5106 case WXK_PRIOR:
5107 MovePageUp();
5108 break;
5109
5110 case WXK_NEXT:
5111 MovePageDown();
5112 break;
5113
5c8fc7c1 5114#if 0
07296f0b
RD
5115 case WXK_SPACE:
5116 if ( !IsEditable() )
5117 {
5c8fc7c1 5118 MoveCursorRight( FALSE );
07296f0b
RD
5119 break;
5120 }
5121 // Otherwise fall through to default
5c8fc7c1
SN
5122#else
5123 case WXK_SPACE:
5124 m_selection->ToggleCellSelection( m_currentCellCoords.GetRow(),
5125 m_currentCellCoords.GetCol() );
5126 break;
5127#endif
07296f0b 5128
2d66e025 5129 default:
c239a4bb
VZ
5130 // alphanumeric keys or F2 (special key just for this) enable
5131 // the cell edit control
5c8fc7c1
SN
5132 // On just Shift/Control I get values for event.KeyCode()
5133 // that are outside the range where isalnum's behaviour is
5134 // well defined, so do an additional sanity check.
b94ae1ea
VZ
5135 if ( !(event.AltDown() ||
5136 event.MetaDown() ||
5137 event.ControlDown()) &&
5c8fc7c1
SN
5138 ((isalnum(event.KeyCode()) &&
5139 (event.KeyCode() < 256 && event.KeyCode() >= 0)) ||
5140 event.KeyCode() == WXK_F2) &&
b94ae1ea
VZ
5141 !IsCellEditControlEnabled() &&
5142 CanEnableCellControl() )
b54ba671
VZ
5143 {
5144 EnableCellEditControl();
f2d76237
RD
5145 int row = m_currentCellCoords.GetRow();
5146 int col = m_currentCellCoords.GetCol();
5147 wxGridCellAttr* attr = GetCellAttr(row, col);
0b190b0f
VZ
5148 wxGridCellEditor *editor = attr->GetEditor(this, row, col);
5149 editor->StartingKey(event);
5150 editor->DecRef();
2c9a89e0
RD
5151 attr->DecRef();
5152 }
b3a7510d
VZ
5153 else
5154 {
b94ae1ea
VZ
5155 // let others process char events with modifiers or all
5156 // char events for readonly cells
b3a7510d
VZ
5157 event.Skip();
5158 }
025562fe 5159 break;
2d66e025 5160 }
f85afd4e
MB
5161 }
5162
2d66e025 5163 m_inOnKeyDown = FALSE;
f85afd4e
MB
5164}
5165
07296f0b 5166
2796cce3 5167void wxGrid::OnEraseBackground(wxEraseEvent&)
508011ce
VZ
5168{
5169}
07296f0b 5170
2d66e025 5171void wxGrid::SetCurrentCell( const wxGridCellCoords& coords )
66242c80 5172{
4634a5d6
MB
5173 if ( m_displayed &&
5174 m_currentCellCoords != wxGridNoCellCoords )
2d66e025 5175 {
2d66e025 5176 HideCellEditControl();
b54ba671 5177 DisableCellEditControl();
07296f0b
RD
5178
5179 // Clear the old current cell highlight
5180 wxRect r = BlockToDeviceRect(m_currentCellCoords, m_currentCellCoords);
d1c0b4f9
VZ
5181
5182 // Otherwise refresh redraws the highlight!
5183 m_currentCellCoords = coords;
5184
07296f0b 5185 m_gridWin->Refresh( FALSE, &r );
66242c80 5186 }
8f177c8e 5187
2d66e025
MB
5188 m_currentCellCoords = coords;
5189
4634a5d6 5190 if ( m_displayed )
2d66e025 5191 {
07296f0b
RD
5192 wxClientDC dc(m_gridWin);
5193 PrepareDC(dc);
283b7808
VZ
5194
5195 wxGridCellAttr* attr = GetCellAttr(coords);
5196 DrawCellHighlight(dc, attr);
5197 attr->DecRef();
b5808881
SN
5198#if 0
5199 // SN: For my extended selection code, automatic
5200 // deselection is definitely not a good idea.
4634a5d6
MB
5201 if ( IsSelection() )
5202 {
5203 wxRect r( SelectionToDeviceRect() );
5204 ClearSelection();
5205 if ( !GetBatchCount() ) m_gridWin->Refresh( FALSE, &r );
5206 }
b5808881 5207#endif
2d66e025 5208 }
66242c80
MB
5209}
5210
2d66e025
MB
5211
5212//
5213// ------ functions to get/send data (see also public functions)
5214//
5215
5216bool wxGrid::GetModelValues()
66242c80 5217{
2d66e025 5218 if ( m_table )
66242c80 5219 {
2d66e025 5220 // all we need to do is repaint the grid
66242c80 5221 //
2d66e025 5222 m_gridWin->Refresh();
66242c80
MB
5223 return TRUE;
5224 }
8f177c8e 5225
66242c80
MB
5226 return FALSE;
5227}
5228
2d66e025
MB
5229
5230bool wxGrid::SetModelValues()
f85afd4e 5231{
2d66e025 5232 int row, col;
8f177c8e 5233
2d66e025
MB
5234 if ( m_table )
5235 {
5236 for ( row = 0; row < m_numRows; row++ )
f85afd4e 5237 {
2d66e025 5238 for ( col = 0; col < m_numCols; col++ )
f85afd4e 5239 {
2d66e025 5240 m_table->SetValue( row, col, GetCellValue(row, col) );
f85afd4e
MB
5241 }
5242 }
8f177c8e 5243
f85afd4e
MB
5244 return TRUE;
5245 }
5246
5247 return FALSE;
5248}
5249
2d66e025
MB
5250
5251
5252// Note - this function only draws cells that are in the list of
5253// exposed cells (usually set from the update region by
5254// CalcExposedCells)
5255//
5256void wxGrid::DrawGridCellArea( wxDC& dc )
f85afd4e 5257{
2d66e025 5258 if ( !m_numRows || !m_numCols ) return;
60ff3b99 5259
2d66e025
MB
5260 size_t i;
5261 size_t numCells = m_cellsExposed.GetCount();
60ff3b99 5262
2d66e025 5263 for ( i = 0; i < numCells; i++ )
f85afd4e 5264 {
2d66e025
MB
5265 DrawCell( dc, m_cellsExposed[i] );
5266 }
5267}
8f177c8e 5268
8f177c8e 5269
7c8a8ad5
MB
5270void wxGrid::DrawGridSpace( wxDC& dc )
5271{
5272 if ( m_numRows && m_numCols )
5273 {
5274 int cw, ch;
5275 m_gridWin->GetClientSize( &cw, &ch );
5276
5277 int right, bottom;
5278 CalcUnscrolledPosition( cw, ch, &right, &bottom );
5279
5280 if ( right > GetColRight(m_numCols-1) ||
5281 bottom > GetRowBottom(m_numRows-1) )
5282 {
5283 int left, top;
5284 CalcUnscrolledPosition( 0, 0, &left, &top );
5285
5286 dc.SetBrush( wxBrush(GetDefaultCellBackgroundColour(), wxSOLID) );
5287 dc.SetPen( *wxTRANSPARENT_PEN );
5288
5289 if ( right > GetColRight(m_numCols-1) )
a5777624 5290 dc.DrawRectangle( GetColRight(m_numCols-1), top,
7c8a8ad5
MB
5291 right - GetColRight(m_numCols-1), ch );
5292
5293 if ( bottom > GetRowBottom(m_numRows-1) )
a5777624 5294 dc.DrawRectangle( left, GetRowBottom(m_numRows-1),
7c8a8ad5
MB
5295 cw, bottom - GetRowBottom(m_numRows-1) );
5296 }
5297 }
5298}
5299
5300
2d66e025
MB
5301void wxGrid::DrawCell( wxDC& dc, const wxGridCellCoords& coords )
5302{
ab79958a
VZ
5303 int row = coords.GetRow();
5304 int col = coords.GetCol();
60ff3b99 5305
7c1cb261 5306 if ( GetColWidth(col) <= 0 || GetRowHeight(row) <= 0 )
ab79958a
VZ
5307 return;
5308
5309 // we draw the cell border ourselves
9496deb5 5310#if !WXGRID_DRAW_LINES
2d66e025
MB
5311 if ( m_gridLinesEnabled )
5312 DrawCellBorder( dc, coords );
796df70a 5313#endif
f85afd4e 5314
189d0213
VZ
5315 wxGridCellAttr* attr = GetCellAttr(row, col);
5316
5317 bool isCurrent = coords == m_currentCellCoords;
508011ce 5318
ab79958a 5319 wxRect rect;
7c1cb261
VZ
5320 rect.x = GetColLeft(col);
5321 rect.y = GetRowTop(row);
5322 rect.width = GetColWidth(col) - 1;
5323 rect.height = GetRowHeight(row) - 1;
f85afd4e 5324
189d0213
VZ
5325 // if the editor is shown, we should use it and not the renderer
5326 if ( isCurrent && IsCellEditControlEnabled() )
5327 {
0b190b0f
VZ
5328 wxGridCellEditor *editor = attr->GetEditor(this, row, col);
5329 editor->PaintBackground(rect, attr);
5330 editor->DecRef();
189d0213
VZ
5331 }
5332 else
5333 {
5334 // but all the rest is drawn by the cell renderer and hence may be
5335 // customized
0b190b0f
VZ
5336 wxGridCellRenderer *renderer = attr->GetRenderer(this, row, col);
5337 renderer->Draw(*this, *attr, dc, rect, row, col, IsInSelection(coords));
5338 renderer->DecRef();
189d0213 5339 }
07296f0b 5340
283b7808
VZ
5341 attr->DecRef();
5342}
07296f0b 5343
283b7808 5344void wxGrid::DrawCellHighlight( wxDC& dc, const wxGridCellAttr *attr )
07296f0b
RD
5345{
5346 int row = m_currentCellCoords.GetRow();
5347 int col = m_currentCellCoords.GetCol();
5348
7c1cb261 5349 if ( GetColWidth(col) <= 0 || GetRowHeight(row) <= 0 )
07296f0b
RD
5350 return;
5351
5352 wxRect rect;
7c1cb261
VZ
5353 rect.x = GetColLeft(col);
5354 rect.y = GetRowTop(row);
5355 rect.width = GetColWidth(col) - 1;
5356 rect.height = GetRowHeight(row) - 1;
07296f0b 5357
b3a7510d
VZ
5358 // hmmm... what could we do here to show that the cell is disabled?
5359 // for now, I just draw a thinner border than for the other ones, but
5360 // it doesn't look really good
5361 dc.SetPen(wxPen(m_gridLineColour, attr->IsReadOnly() ? 1 : 3, wxSOLID));
5362 dc.SetBrush(*wxTRANSPARENT_BRUSH);
5363
5364 dc.DrawRectangle(rect);
07296f0b 5365
283b7808 5366#if 0
b3a7510d 5367 // VZ: my experiments with 3d borders...
283b7808 5368
b3a7510d 5369 // how to properly set colours for arbitrary bg?
283b7808
VZ
5370 wxCoord x1 = rect.x,
5371 y1 = rect.y,
00cf1208
RR
5372 x2 = rect.x + rect.width -1,
5373 y2 = rect.y + rect.height -1;
283b7808
VZ
5374
5375 dc.SetPen(*wxWHITE_PEN);
00cf1208
RR
5376 dc.DrawLine(x1, y1, x2, y1);
5377 dc.DrawLine(x1, y1, x1, y2);
283b7808 5378
283b7808 5379 dc.DrawLine(x1 + 1, y2 - 1, x2 - 1, y2 - 1);
00cf1208 5380 dc.DrawLine(x2 - 1, y1 + 1, x2 - 1, y2 );
283b7808
VZ
5381
5382 dc.SetPen(*wxBLACK_PEN);
5383 dc.DrawLine(x1, y2, x2, y2);
00cf1208 5384 dc.DrawLine(x2, y1, x2, y2+1);
b3a7510d 5385#endif // 0
2d66e025 5386}
f85afd4e 5387
f2d76237 5388
2d66e025
MB
5389void wxGrid::DrawCellBorder( wxDC& dc, const wxGridCellCoords& coords )
5390{
2d66e025
MB
5391 int row = coords.GetRow();
5392 int col = coords.GetCol();
7c1cb261
VZ
5393 if ( GetColWidth(col) <= 0 || GetRowHeight(row) <= 0 )
5394 return;
5395
5396 dc.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID) );
60ff3b99 5397
2d66e025
MB
5398 // right hand border
5399 //
7c1cb261
VZ
5400 dc.DrawLine( GetColRight(col), GetRowTop(row),
5401 GetColRight(col), GetRowBottom(row) );
2d66e025
MB
5402
5403 // bottom border
5404 //
7c1cb261
VZ
5405 dc.DrawLine( GetColLeft(col), GetRowBottom(row),
5406 GetColRight(col), GetRowBottom(row) );
f85afd4e
MB
5407}
5408
b3a7510d
VZ
5409void wxGrid::DrawHighlight(wxDC& dc)
5410{
99306db2
VZ
5411 if ( IsCellEditControlEnabled() )
5412 {
5413 // don't show highlight when the edit control is shown
5414 return;
5415 }
5416
b3a7510d
VZ
5417 // if the active cell was repainted, repaint its highlight too because it
5418 // might have been damaged by the grid lines
5419 size_t count = m_cellsExposed.GetCount();
5420 for ( size_t n = 0; n < count; n++ )
5421 {
5422 if ( m_cellsExposed[n] == m_currentCellCoords )
5423 {
5424 wxGridCellAttr* attr = GetCellAttr(m_currentCellCoords);
5425 DrawCellHighlight(dc, attr);
5426 attr->DecRef();
5427
5428 break;
5429 }
5430 }
5431}
2d66e025 5432
2d66e025
MB
5433// TODO: remove this ???
5434// This is used to redraw all grid lines e.g. when the grid line colour
5435// has been changed
5436//
796df70a 5437void wxGrid::DrawAllGridLines( wxDC& dc, const wxRegion & reg )
f85afd4e 5438{
60ff3b99
VZ
5439 if ( !m_gridLinesEnabled ||
5440 !m_numRows ||
2d66e025 5441 !m_numCols ) return;
f85afd4e 5442
2d66e025 5443 int top, bottom, left, right;
796df70a 5444
8e217128 5445#ifndef __WXGTK__
508011ce
VZ
5446 if (reg.IsEmpty())
5447 {
796df70a
SN
5448 int cw, ch;
5449 m_gridWin->GetClientSize(&cw, &ch);
5450
5451 // virtual coords of visible area
5452 //
5453 CalcUnscrolledPosition( 0, 0, &left, &top );
5454 CalcUnscrolledPosition( cw, ch, &right, &bottom );
5455 }
508011ce
VZ
5456 else
5457 {
796df70a
SN
5458 wxCoord x, y, w, h;
5459 reg.GetBox(x, y, w, h);
5460 CalcUnscrolledPosition( x, y, &left, &top );
5461 CalcUnscrolledPosition( x + w, y + h, &right, &bottom );
5462 }
8e217128
RR
5463#else
5464 int cw, ch;
5465 m_gridWin->GetClientSize(&cw, &ch);
5466 CalcUnscrolledPosition( 0, 0, &left, &top );
5467 CalcUnscrolledPosition( cw, ch, &right, &bottom );
5468#endif
f85afd4e 5469
9496deb5
MB
5470 // avoid drawing grid lines past the last row and col
5471 //
7c1cb261
VZ
5472 right = wxMin( right, GetColRight(m_numCols - 1) );
5473 bottom = wxMin( bottom, GetRowBottom(m_numRows - 1) );
9496deb5 5474
2d66e025 5475 dc.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID) );
f85afd4e 5476
2d66e025 5477 // horizontal grid lines
f85afd4e 5478 //
60ff3b99 5479 int i;
9496deb5 5480 for ( i = 0; i < m_numRows; i++ )
f85afd4e 5481 {
6d55126d 5482 int bot = GetRowBottom(i) - 1;
7c1cb261 5483
6d55126d 5484 if ( bot > bottom )
2d66e025
MB
5485 {
5486 break;
5487 }
7c1cb261 5488
6d55126d 5489 if ( bot >= top )
2d66e025 5490 {
6d55126d 5491 dc.DrawLine( left, bot, right, bot );
2d66e025 5492 }
f85afd4e
MB
5493 }
5494
f85afd4e 5495
2d66e025
MB
5496 // vertical grid lines
5497 //
9496deb5 5498 for ( i = 0; i < m_numCols; i++ )
f85afd4e 5499 {
7c1cb261
VZ
5500 int colRight = GetColRight(i) - 1;
5501 if ( colRight > right )
2d66e025
MB
5502 {
5503 break;
5504 }
7c1cb261
VZ
5505
5506 if ( colRight >= left )
2d66e025 5507 {
7c1cb261 5508 dc.DrawLine( colRight, top, colRight, bottom );
2d66e025
MB
5509 }
5510 }
5511}
f85afd4e 5512
8f177c8e 5513
2d66e025
MB
5514void wxGrid::DrawRowLabels( wxDC& dc )
5515{
5516 if ( !m_numRows || !m_numCols ) return;
60ff3b99 5517
2d66e025
MB
5518 size_t i;
5519 size_t numLabels = m_rowLabelsExposed.GetCount();
60ff3b99 5520
2d66e025
MB
5521 for ( i = 0; i < numLabels; i++ )
5522 {
5523 DrawRowLabel( dc, m_rowLabelsExposed[i] );
60ff3b99 5524 }
f85afd4e
MB
5525}
5526
5527
2d66e025 5528void wxGrid::DrawRowLabel( wxDC& dc, int row )
f85afd4e 5529{
7c1cb261
VZ
5530 if ( GetRowHeight(row) <= 0 )
5531 return;
60ff3b99 5532
7c1cb261
VZ
5533 int rowTop = GetRowTop(row),
5534 rowBottom = GetRowBottom(row) - 1;
b99be8fb 5535
2d66e025 5536 dc.SetPen( *wxBLACK_PEN );
b0d6ff2f 5537 dc.DrawLine( m_rowLabelWidth-1, rowTop,
7c1cb261 5538 m_rowLabelWidth-1, rowBottom );
b99be8fb 5539
7c1cb261 5540 dc.DrawLine( 0, rowBottom, m_rowLabelWidth-1, rowBottom );
b99be8fb 5541
2d66e025 5542 dc.SetPen( *wxWHITE_PEN );
7c1cb261 5543 dc.DrawLine( 0, rowTop, 0, rowBottom );
b0d6ff2f 5544 dc.DrawLine( 0, rowTop, m_rowLabelWidth-1, rowTop );
60ff3b99 5545
f85afd4e 5546 dc.SetBackgroundMode( wxTRANSPARENT );
f85afd4e
MB
5547 dc.SetTextForeground( GetLabelTextColour() );
5548 dc.SetFont( GetLabelFont() );
8f177c8e 5549
f85afd4e 5550 int hAlign, vAlign;
2d66e025 5551 GetRowLabelAlignment( &hAlign, &vAlign );
60ff3b99 5552
2d66e025
MB
5553 wxRect rect;
5554 rect.SetX( 2 );
7c1cb261 5555 rect.SetY( GetRowTop(row) + 2 );
2d66e025 5556 rect.SetWidth( m_rowLabelWidth - 4 );
7c1cb261 5557 rect.SetHeight( GetRowHeight(row) - 4 );
60ff3b99 5558 DrawTextRectangle( dc, GetRowLabelValue( row ), rect, hAlign, vAlign );
f85afd4e
MB
5559}
5560
5561
2d66e025 5562void wxGrid::DrawColLabels( wxDC& dc )
f85afd4e 5563{
2d66e025 5564 if ( !m_numRows || !m_numCols ) return;
60ff3b99 5565
2d66e025
MB
5566 size_t i;
5567 size_t numLabels = m_colLabelsExposed.GetCount();
60ff3b99 5568
2d66e025 5569 for ( i = 0; i < numLabels; i++ )
f85afd4e 5570 {
2d66e025 5571 DrawColLabel( dc, m_colLabelsExposed[i] );
60ff3b99 5572 }
f85afd4e
MB
5573}
5574
5575
2d66e025 5576void wxGrid::DrawColLabel( wxDC& dc, int col )
f85afd4e 5577{
7c1cb261
VZ
5578 if ( GetColWidth(col) <= 0 )
5579 return;
60ff3b99 5580
7c1cb261
VZ
5581 int colLeft = GetColLeft(col),
5582 colRight = GetColRight(col) - 1;
b99be8fb 5583
2d66e025 5584 dc.SetPen( *wxBLACK_PEN );
7c1cb261
VZ
5585 dc.DrawLine( colRight, 0,
5586 colRight, m_colLabelHeight-1 );
b99be8fb 5587
b0d6ff2f 5588 dc.DrawLine( colLeft, m_colLabelHeight-1,
7c1cb261 5589 colRight, m_colLabelHeight-1 );
b99be8fb 5590
f85afd4e 5591 dc.SetPen( *wxWHITE_PEN );
b0d6ff2f 5592 dc.DrawLine( colLeft, 0, colLeft, m_colLabelHeight-1 );
7c1cb261 5593 dc.DrawLine( colLeft, 0, colRight, 0 );
f85afd4e 5594
b0d6ff2f
MB
5595 dc.SetBackgroundMode( wxTRANSPARENT );
5596 dc.SetTextForeground( GetLabelTextColour() );
5597 dc.SetFont( GetLabelFont() );
8f177c8e 5598
f85afd4e 5599 dc.SetBackgroundMode( wxTRANSPARENT );
f85afd4e
MB
5600 dc.SetTextForeground( GetLabelTextColour() );
5601 dc.SetFont( GetLabelFont() );
8f177c8e 5602
f85afd4e 5603 int hAlign, vAlign;
2d66e025 5604 GetColLabelAlignment( &hAlign, &vAlign );
60ff3b99 5605
2d66e025 5606 wxRect rect;
7c1cb261 5607 rect.SetX( colLeft + 2 );
2d66e025 5608 rect.SetY( 2 );
7c1cb261 5609 rect.SetWidth( GetColWidth(col) - 4 );
2d66e025 5610 rect.SetHeight( m_colLabelHeight - 4 );
60ff3b99 5611 DrawTextRectangle( dc, GetColLabelValue( col ), rect, hAlign, vAlign );
f85afd4e
MB
5612}
5613
5614
2d66e025
MB
5615void wxGrid::DrawTextRectangle( wxDC& dc,
5616 const wxString& value,
5617 const wxRect& rect,
5618 int horizAlign,
5619 int vertAlign )
f85afd4e 5620{
2d66e025
MB
5621 long textWidth, textHeight;
5622 long lineWidth, lineHeight;
5623 wxArrayString lines;
f85afd4e 5624
2d66e025
MB
5625 dc.SetClippingRegion( rect );
5626 StringToLines( value, lines );
5627 if ( lines.GetCount() )
5628 {
5629 GetTextBoxSize( dc, lines, &textWidth, &textHeight );
5630 dc.GetTextExtent( lines[0], &lineWidth, &lineHeight );
f85afd4e 5631
2d66e025
MB
5632 float x, y;
5633 switch ( horizAlign )
5634 {
5635 case wxRIGHT:
5636 x = rect.x + (rect.width - textWidth - 1);
5637 break;
f85afd4e 5638
2d66e025
MB
5639 case wxCENTRE:
5640 x = rect.x + ((rect.width - textWidth)/2);
5641 break;
f85afd4e 5642
2d66e025
MB
5643 case wxLEFT:
5644 default:
5645 x = rect.x + 1;
5646 break;
5647 }
f85afd4e 5648
2d66e025
MB
5649 switch ( vertAlign )
5650 {
5651 case wxBOTTOM:
5652 y = rect.y + (rect.height - textHeight - 1);
5653 break;
f85afd4e
MB
5654
5655 case wxCENTRE:
8f177c8e 5656 y = rect.y + ((rect.height - textHeight)/2);
f85afd4e
MB
5657 break;
5658
5659 case wxTOP:
5660 default:
8f177c8e 5661 y = rect.y + 1;
f85afd4e
MB
5662 break;
5663 }
5664
b540eb2b 5665 for ( size_t i = 0; i < lines.GetCount(); i++ )
f85afd4e
MB
5666 {
5667 dc.DrawText( lines[i], (long)x, (long)y );
5668 y += lineHeight;
5669 }
5670 }
8f177c8e 5671
f85afd4e 5672 dc.DestroyClippingRegion();
f85afd4e
MB
5673}
5674
5675
5676// Split multi line text up into an array of strings. Any existing
5677// contents of the string array are preserved.
5678//
5679void wxGrid::StringToLines( const wxString& value, wxArrayString& lines )
5680{
f85afd4e
MB
5681 int startPos = 0;
5682 int pos;
6d004f67 5683 wxString eol = wxTextFile::GetEOL( wxTextFileType_Unix );
2433bb2e 5684 wxString tVal = wxTextFile::Translate( value, wxTextFileType_Unix );
e2b42eeb 5685
2433bb2e 5686 while ( startPos < (int)tVal.Length() )
f85afd4e 5687 {
2433bb2e 5688 pos = tVal.Mid(startPos).Find( eol );
f85afd4e
MB
5689 if ( pos < 0 )
5690 {
5691 break;
5692 }
5693 else if ( pos == 0 )
5694 {
5695 lines.Add( wxEmptyString );
5696 }
5697 else
5698 {
2433bb2e 5699 lines.Add( value.Mid(startPos, pos) );
f85afd4e
MB
5700 }
5701 startPos += pos+1;
5702 }
b540eb2b 5703 if ( startPos < (int)value.Length() )
f85afd4e
MB
5704 {
5705 lines.Add( value.Mid( startPos ) );
5706 }
5707}
5708
5709
5710void wxGrid::GetTextBoxSize( wxDC& dc,
5711 wxArrayString& lines,
5712 long *width, long *height )
5713{
5714 long w = 0;
5715 long h = 0;
5716 long lineW, lineH;
5717
b540eb2b 5718 size_t i;
f85afd4e
MB
5719 for ( i = 0; i < lines.GetCount(); i++ )
5720 {
5721 dc.GetTextExtent( lines[i], &lineW, &lineH );
5722 w = wxMax( w, lineW );
5723 h += lineH;
5724 }
5725
5726 *width = w;
5727 *height = h;
5728}
5729
5730
5731//
2d66e025 5732// ------ Edit control functions
f85afd4e
MB
5733//
5734
2d66e025
MB
5735
5736void wxGrid::EnableEditing( bool edit )
f85afd4e 5737{
2d66e025
MB
5738 // TODO: improve this ?
5739 //
5740 if ( edit != m_editable )
f85afd4e 5741 {
2d66e025 5742 m_editable = edit;
1f1ce288 5743
b54ba671
VZ
5744 // FIXME IMHO this won't disable the edit control if edit == FALSE
5745 // because of the check in the beginning of
5746 // EnableCellEditControl() just below (VZ)
2c9a89e0 5747 EnableCellEditControl(m_editable);
f85afd4e 5748 }
f85afd4e
MB
5749}
5750
5751
2d66e025 5752void wxGrid::EnableCellEditControl( bool enable )
f85afd4e 5753{
2c9a89e0
RD
5754 if (! m_editable)
5755 return;
5756
dcfe4c3d
SN
5757 if ( m_currentCellCoords == wxGridNoCellCoords )
5758 SetCurrentCell( 0, 0 );
60ff3b99 5759
2c9a89e0
RD
5760 if ( enable != m_cellEditCtrlEnabled )
5761 {
b54ba671
VZ
5762 // TODO allow the app to Veto() this event?
5763 SendEvent(enable ? wxEVT_GRID_EDITOR_SHOWN : wxEVT_GRID_EDITOR_HIDDEN);
5764
dcfe4c3d 5765 if ( enable )
f85afd4e 5766 {
b54ba671
VZ
5767 // this should be checked by the caller!
5768 wxASSERT_MSG( CanEnableCellControl(),
5769 _T("can't enable editing for this cell!") );
5770
5771 // do it before ShowCellEditControl()
dcfe4c3d 5772 m_cellEditCtrlEnabled = enable;
b54ba671 5773
2d66e025 5774 ShowCellEditControl();
2d66e025
MB
5775 }
5776 else
5777 {
2d66e025
MB
5778 HideCellEditControl();
5779 SaveEditControlValue();
b54ba671
VZ
5780
5781 // do it after HideCellEditControl()
dcfe4c3d 5782 m_cellEditCtrlEnabled = enable;
f85afd4e 5783 }
f85afd4e 5784 }
f85afd4e 5785}
f85afd4e 5786
b54ba671 5787bool wxGrid::IsCurrentCellReadOnly() const
283b7808 5788{
b54ba671
VZ
5789 // const_cast
5790 wxGridCellAttr* attr = ((wxGrid *)this)->GetCellAttr(m_currentCellCoords);
5791 bool readonly = attr->IsReadOnly();
5792 attr->DecRef();
283b7808 5793
b54ba671
VZ
5794 return readonly;
5795}
283b7808 5796
b54ba671
VZ
5797bool wxGrid::CanEnableCellControl() const
5798{
5799 return m_editable && !IsCurrentCellReadOnly();
5800}
5801
5802bool wxGrid::IsCellEditControlEnabled() const
5803{
5804 // the cell edit control might be disable for all cells or just for the
5805 // current one if it's read only
5806 return m_cellEditCtrlEnabled ? !IsCurrentCellReadOnly() : FALSE;
283b7808
VZ
5807}
5808
2d66e025 5809void wxGrid::ShowCellEditControl()
f85afd4e 5810{
2d66e025 5811 if ( IsCellEditControlEnabled() )
f85afd4e 5812 {
2d66e025
MB
5813 if ( !IsVisible( m_currentCellCoords ) )
5814 {
5815 return;
5816 }
5817 else
5818 {
2c9a89e0
RD
5819 wxRect rect = CellToRect( m_currentCellCoords );
5820 int row = m_currentCellCoords.GetRow();
5821 int col = m_currentCellCoords.GetCol();
2d66e025
MB
5822
5823 // convert to scrolled coords
5824 //
99306db2 5825 CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
508011ce 5826
99306db2
VZ
5827 // done in PaintBackground()
5828#if 0
5829 // erase the highlight and the cell contents because the editor
5830 // might not cover the entire cell
5831 wxClientDC dc( m_gridWin );
5832 PrepareDC( dc );
5833 dc.SetBrush(*wxLIGHT_GREY_BRUSH); //wxBrush(attr->GetBackgroundColour(), wxSOLID));
5834 dc.SetPen(*wxTRANSPARENT_PEN);
5835 dc.DrawRectangle(rect);
5836#endif // 0
d2fdd8d2 5837
99306db2
VZ
5838 // cell is shifted by one pixel
5839 rect.x--;
5840 rect.y--;
f0102d2a 5841
508011ce 5842 wxGridCellAttr* attr = GetCellAttr(row, col);
28a77bc4 5843 wxGridCellEditor* editor = attr->GetEditor(this, row, col);
3da93aae
VZ
5844 if ( !editor->IsCreated() )
5845 {
2c9a89e0
RD
5846 editor->Create(m_gridWin, -1,
5847 new wxGridCellEditorEvtHandler(this, editor));
2d66e025
MB
5848 }
5849
b0e282b3 5850 editor->Show( TRUE, attr );
0b190b0f 5851
2c9a89e0 5852 editor->SetSize( rect );
99306db2 5853
3da93aae 5854 editor->BeginEdit(row, col, this);
0b190b0f
VZ
5855
5856 editor->DecRef();
2c9a89e0
RD
5857 attr->DecRef();
5858 }
f85afd4e
MB
5859 }
5860}
5861
5862
2d66e025 5863void wxGrid::HideCellEditControl()
f85afd4e 5864{
2d66e025 5865 if ( IsCellEditControlEnabled() )
f85afd4e 5866 {
2c9a89e0
RD
5867 int row = m_currentCellCoords.GetRow();
5868 int col = m_currentCellCoords.GetCol();
5869
3da93aae 5870 wxGridCellAttr* attr = GetCellAttr(row, col);
0b190b0f
VZ
5871 wxGridCellEditor *editor = attr->GetEditor(this, row, col);
5872 editor->Show( FALSE );
5873 editor->DecRef();
2c9a89e0
RD
5874 attr->DecRef();
5875 m_gridWin->SetFocus();
f85afd4e 5876 }
2d66e025 5877}
8f177c8e 5878
2d66e025 5879
2d66e025
MB
5880void wxGrid::SaveEditControlValue()
5881{
3da93aae
VZ
5882 if ( IsCellEditControlEnabled() )
5883 {
2c9a89e0
RD
5884 int row = m_currentCellCoords.GetRow();
5885 int col = m_currentCellCoords.GetCol();
8f177c8e 5886
3da93aae 5887 wxGridCellAttr* attr = GetCellAttr(row, col);
28a77bc4 5888 wxGridCellEditor* editor = attr->GetEditor(this, row, col);
3324d5f5 5889 bool changed = editor->EndEdit(row, col, this);
2d66e025 5890
0b190b0f 5891 editor->DecRef();
2c9a89e0 5892 attr->DecRef();
2d66e025 5893
3da93aae
VZ
5894 if (changed)
5895 {
b54ba671 5896 SendEvent( wxEVT_GRID_CELL_CHANGE,
2d66e025
MB
5897 m_currentCellCoords.GetRow(),
5898 m_currentCellCoords.GetCol() );
5899 }
f85afd4e
MB
5900 }
5901}
5902
2d66e025
MB
5903
5904//
60ff3b99
VZ
5905// ------ Grid location functions
5906// Note that all of these functions work with the logical coordinates of
2d66e025
MB
5907// grid cells and labels so you will need to convert from device
5908// coordinates for mouse events etc.
5909//
5910
5911void wxGrid::XYToCell( int x, int y, wxGridCellCoords& coords )
f85afd4e 5912{
58dd5b3b
MB
5913 int row = YToRow(y);
5914 int col = XToCol(x);
5915
5916 if ( row == -1 || col == -1 )
5917 {
5918 coords = wxGridNoCellCoords;
5919 }
5920 else
5921 {
5922 coords.Set( row, col );
5923 }
2d66e025 5924}
f85afd4e 5925
8f177c8e 5926
2d66e025
MB
5927int wxGrid::YToRow( int y )
5928{
5929 int i;
8f177c8e 5930
2d66e025 5931 for ( i = 0; i < m_numRows; i++ )
f85afd4e 5932 {
7c1cb261
VZ
5933 if ( y < GetRowBottom(i) )
5934 return i;
f85afd4e 5935 }
2d66e025 5936
4ee5fc9c 5937 return -1;
f85afd4e
MB
5938}
5939
2d66e025
MB
5940
5941int wxGrid::XToCol( int x )
f85afd4e 5942{
2d66e025 5943 int i;
8f177c8e 5944
2d66e025 5945 for ( i = 0; i < m_numCols; i++ )
f85afd4e 5946 {
7c1cb261
VZ
5947 if ( x < GetColRight(i) )
5948 return i;
f85afd4e
MB
5949 }
5950
4ee5fc9c 5951 return -1;
2d66e025 5952}
8f177c8e 5953
2d66e025
MB
5954
5955// return the row number that that the y coord is near the edge of, or
5956// -1 if not near an edge
5957//
5958int wxGrid::YToEdgeOfRow( int y )
5959{
5960 int i, d;
5961
5962 for ( i = 0; i < m_numRows; i++ )
5963 {
7c1cb261 5964 if ( GetRowHeight(i) > WXGRID_LABEL_EDGE_ZONE )
f85afd4e 5965 {
7c1cb261
VZ
5966 d = abs( y - GetRowBottom(i) );
5967 if ( d < WXGRID_LABEL_EDGE_ZONE )
5968 return i;
f85afd4e 5969 }
f85afd4e 5970 }
2d66e025
MB
5971
5972 return -1;
5973}
5974
5975
5976// return the col number that that the x coord is near the edge of, or
5977// -1 if not near an edge
5978//
5979int wxGrid::XToEdgeOfCol( int x )
5980{
5981 int i, d;
5982
5983 for ( i = 0; i < m_numCols; i++ )
f85afd4e 5984 {
7c1cb261 5985 if ( GetColWidth(i) > WXGRID_LABEL_EDGE_ZONE )
2d66e025 5986 {
7c1cb261
VZ
5987 d = abs( x - GetColRight(i) );
5988 if ( d < WXGRID_LABEL_EDGE_ZONE )
5989 return i;
2d66e025 5990 }
f85afd4e 5991 }
2d66e025
MB
5992
5993 return -1;
f85afd4e
MB
5994}
5995
2d66e025
MB
5996
5997wxRect wxGrid::CellToRect( int row, int col )
f85afd4e 5998{
2d66e025 5999 wxRect rect( -1, -1, -1, -1 );
f85afd4e 6000
2d66e025
MB
6001 if ( row >= 0 && row < m_numRows &&
6002 col >= 0 && col < m_numCols )
f85afd4e 6003 {
7c1cb261
VZ
6004 rect.x = GetColLeft(col);
6005 rect.y = GetRowTop(row);
6006 rect.width = GetColWidth(col);
6007 rect.height = GetRowHeight(row);
f85afd4e
MB
6008 }
6009
2d66e025
MB
6010 return rect;
6011}
6012
6013
6014bool wxGrid::IsVisible( int row, int col, bool wholeCellVisible )
6015{
6016 // get the cell rectangle in logical coords
6017 //
6018 wxRect r( CellToRect( row, col ) );
60ff3b99 6019
2d66e025
MB
6020 // convert to device coords
6021 //
6022 int left, top, right, bottom;
6023 CalcScrolledPosition( r.GetLeft(), r.GetTop(), &left, &top );
6024 CalcScrolledPosition( r.GetRight(), r.GetBottom(), &right, &bottom );
60ff3b99 6025
2d66e025
MB
6026 // check against the client area of the grid window
6027 //
6028 int cw, ch;
6029 m_gridWin->GetClientSize( &cw, &ch );
60ff3b99 6030
2d66e025 6031 if ( wholeCellVisible )
f85afd4e 6032 {
2d66e025 6033 // is the cell wholly visible ?
8f177c8e 6034 //
2d66e025
MB
6035 return ( left >= 0 && right <= cw &&
6036 top >= 0 && bottom <= ch );
6037 }
6038 else
6039 {
6040 // is the cell partly visible ?
6041 //
6042 return ( ((left >=0 && left < cw) || (right > 0 && right <= cw)) &&
6043 ((top >=0 && top < ch) || (bottom > 0 && bottom <= ch)) );
6044 }
6045}
6046
6047
6048// make the specified cell location visible by doing a minimal amount
6049// of scrolling
6050//
6051void wxGrid::MakeCellVisible( int row, int col )
6052{
6053 int i;
60ff3b99 6054 int xpos = -1, ypos = -1;
2d66e025
MB
6055
6056 if ( row >= 0 && row < m_numRows &&
6057 col >= 0 && col < m_numCols )
6058 {
6059 // get the cell rectangle in logical coords
6060 //
6061 wxRect r( CellToRect( row, col ) );
60ff3b99 6062
2d66e025
MB
6063 // convert to device coords
6064 //
6065 int left, top, right, bottom;
6066 CalcScrolledPosition( r.GetLeft(), r.GetTop(), &left, &top );
6067 CalcScrolledPosition( r.GetRight(), r.GetBottom(), &right, &bottom );
60ff3b99 6068
2d66e025
MB
6069 int cw, ch;
6070 m_gridWin->GetClientSize( &cw, &ch );
60ff3b99 6071
2d66e025 6072 if ( top < 0 )
3f296516 6073 {
2d66e025 6074 ypos = r.GetTop();
3f296516 6075 }
2d66e025
MB
6076 else if ( bottom > ch )
6077 {
6078 int h = r.GetHeight();
6079 ypos = r.GetTop();
6080 for ( i = row-1; i >= 0; i-- )
6081 {
7c1cb261
VZ
6082 int rowHeight = GetRowHeight(i);
6083 if ( h + rowHeight > ch )
6084 break;
2d66e025 6085
7c1cb261
VZ
6086 h += rowHeight;
6087 ypos -= rowHeight;
2d66e025 6088 }
f0102d2a
VZ
6089
6090 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
6091 // have rounding errors (this is important, because if we do, we
6092 // might not scroll at all and some cells won't be redrawn)
6093 ypos += GRID_SCROLL_LINE / 2;
2d66e025
MB
6094 }
6095
6096 if ( left < 0 )
6097 {
6098 xpos = r.GetLeft();
6099 }
6100 else if ( right > cw )
6101 {
6102 int w = r.GetWidth();
6103 xpos = r.GetLeft();
6104 for ( i = col-1; i >= 0; i-- )
6105 {
7c1cb261
VZ
6106 int colWidth = GetColWidth(i);
6107 if ( w + colWidth > cw )
6108 break;
2d66e025 6109
7c1cb261
VZ
6110 w += colWidth;
6111 xpos -= colWidth;
2d66e025 6112 }
f0102d2a
VZ
6113
6114 // see comment for ypos above
6115 xpos += GRID_SCROLL_LINE / 2;
2d66e025
MB
6116 }
6117
6118 if ( xpos != -1 || ypos != -1 )
6119 {
f0102d2a
VZ
6120 if ( xpos != -1 ) xpos /= GRID_SCROLL_LINE;
6121 if ( ypos != -1 ) ypos /= GRID_SCROLL_LINE;
2d66e025
MB
6122 Scroll( xpos, ypos );
6123 AdjustScrollbars();
6124 }
6125 }
6126}
6127
6128
6129//
6130// ------ Grid cursor movement functions
6131//
6132
5c8fc7c1 6133bool wxGrid::MoveCursorUp( bool expandSelection )
2d66e025
MB
6134{
6135 if ( m_currentCellCoords != wxGridNoCellCoords &&
6136 m_currentCellCoords.GetRow() > 0 )
6137 {
6138 MakeCellVisible( m_currentCellCoords.GetRow() - 1,
6139 m_currentCellCoords.GetCol() );
60ff3b99 6140
2d66e025
MB
6141 SetCurrentCell( m_currentCellCoords.GetRow() - 1,
6142 m_currentCellCoords.GetCol() );
6143
5c8fc7c1
SN
6144 if ( expandSelection )
6145 m_selection->SelectCell( m_currentCellCoords.GetRow(),
6146 m_currentCellCoords.GetCol() );
8f177c8e 6147 return TRUE;
f85afd4e 6148 }
2d66e025
MB
6149
6150 return FALSE;
6151}
6152
6153
5c8fc7c1 6154bool wxGrid::MoveCursorDown( bool expandSelection )
2d66e025
MB
6155{
6156 // TODO: allow for scrolling
6157 //
6158 if ( m_currentCellCoords != wxGridNoCellCoords &&
6159 m_currentCellCoords.GetRow() < m_numRows-1 )
f85afd4e 6160 {
2d66e025
MB
6161 MakeCellVisible( m_currentCellCoords.GetRow() + 1,
6162 m_currentCellCoords.GetCol() );
60ff3b99 6163
2d66e025
MB
6164 SetCurrentCell( m_currentCellCoords.GetRow() + 1,
6165 m_currentCellCoords.GetCol() );
6166
5c8fc7c1
SN
6167 if ( expandSelection )
6168 m_selection->SelectCell( m_currentCellCoords.GetRow(),
6169 m_currentCellCoords.GetCol() );
2d66e025 6170 return TRUE;
f85afd4e 6171 }
2d66e025
MB
6172
6173 return FALSE;
f85afd4e
MB
6174}
6175
2d66e025 6176
5c8fc7c1 6177bool wxGrid::MoveCursorLeft( bool expandSelection )
f85afd4e 6178{
2d66e025
MB
6179 if ( m_currentCellCoords != wxGridNoCellCoords &&
6180 m_currentCellCoords.GetCol() > 0 )
6181 {
6182 MakeCellVisible( m_currentCellCoords.GetRow(),
6183 m_currentCellCoords.GetCol() - 1 );
60ff3b99 6184
2d66e025
MB
6185 SetCurrentCell( m_currentCellCoords.GetRow(),
6186 m_currentCellCoords.GetCol() - 1 );
8f177c8e 6187
5c8fc7c1
SN
6188 if ( expandSelection )
6189 m_selection->SelectCell( m_currentCellCoords.GetRow(),
6190 m_currentCellCoords.GetCol() );
2d66e025
MB
6191 return TRUE;
6192 }
6193
6194 return FALSE;
6195}
6196
6197
5c8fc7c1 6198bool wxGrid::MoveCursorRight( bool expandSelection )
2d66e025
MB
6199{
6200 if ( m_currentCellCoords != wxGridNoCellCoords &&
6201 m_currentCellCoords.GetCol() < m_numCols - 1 )
f85afd4e 6202 {
2d66e025
MB
6203 MakeCellVisible( m_currentCellCoords.GetRow(),
6204 m_currentCellCoords.GetCol() + 1 );
60ff3b99 6205
2d66e025
MB
6206 SetCurrentCell( m_currentCellCoords.GetRow(),
6207 m_currentCellCoords.GetCol() + 1 );
6208
5c8fc7c1
SN
6209 if ( expandSelection )
6210 m_selection->SelectCell( m_currentCellCoords.GetRow(),
6211 m_currentCellCoords.GetCol() );
2d66e025 6212 return TRUE;
f85afd4e 6213 }
8f177c8e 6214
2d66e025
MB
6215 return FALSE;
6216}
6217
6218
6219bool wxGrid::MovePageUp()
6220{
6221 if ( m_currentCellCoords == wxGridNoCellCoords ) return FALSE;
60ff3b99 6222
2d66e025
MB
6223 int row = m_currentCellCoords.GetRow();
6224 if ( row > 0 )
f85afd4e 6225 {
2d66e025
MB
6226 int cw, ch;
6227 m_gridWin->GetClientSize( &cw, &ch );
60ff3b99 6228
7c1cb261 6229 int y = GetRowTop(row);
2d66e025
MB
6230 int newRow = YToRow( y - ch + 1 );
6231 if ( newRow == -1 )
6232 {
6233 newRow = 0;
6234 }
60ff3b99 6235 else if ( newRow == row )
2d66e025
MB
6236 {
6237 newRow = row - 1;
6238 }
8f177c8e 6239
2d66e025
MB
6240 MakeCellVisible( newRow, m_currentCellCoords.GetCol() );
6241 SetCurrentCell( newRow, m_currentCellCoords.GetCol() );
60ff3b99 6242
f85afd4e
MB
6243 return TRUE;
6244 }
2d66e025
MB
6245
6246 return FALSE;
6247}
6248
6249bool wxGrid::MovePageDown()
6250{
6251 if ( m_currentCellCoords == wxGridNoCellCoords ) return FALSE;
60ff3b99 6252
2d66e025
MB
6253 int row = m_currentCellCoords.GetRow();
6254 if ( row < m_numRows )
f85afd4e 6255 {
2d66e025
MB
6256 int cw, ch;
6257 m_gridWin->GetClientSize( &cw, &ch );
60ff3b99 6258
7c1cb261 6259 int y = GetRowTop(row);
2d66e025
MB
6260 int newRow = YToRow( y + ch );
6261 if ( newRow == -1 )
6262 {
6263 newRow = m_numRows - 1;
6264 }
60ff3b99 6265 else if ( newRow == row )
2d66e025
MB
6266 {
6267 newRow = row + 1;
6268 }
6269
6270 MakeCellVisible( newRow, m_currentCellCoords.GetCol() );
6271 SetCurrentCell( newRow, m_currentCellCoords.GetCol() );
60ff3b99 6272
2d66e025 6273 return TRUE;
f85afd4e 6274 }
2d66e025
MB
6275
6276 return FALSE;
f85afd4e 6277}
8f177c8e 6278
5c8fc7c1 6279bool wxGrid::MoveCursorUpBlock( bool expandSelection )
2d66e025
MB
6280{
6281 if ( m_table &&
6282 m_currentCellCoords != wxGridNoCellCoords &&
6283 m_currentCellCoords.GetRow() > 0 )
6284 {
6285 int row = m_currentCellCoords.GetRow();
6286 int col = m_currentCellCoords.GetCol();
6287
6288 if ( m_table->IsEmptyCell(row, col) )
6289 {
6290 // starting in an empty cell: find the next block of
6291 // non-empty cells
6292 //
6293 while ( row > 0 )
6294 {
6295 row-- ;
6296 if ( !(m_table->IsEmptyCell(row, col)) ) break;
6297 }
6298 }
6299 else if ( m_table->IsEmptyCell(row-1, col) )
6300 {
6301 // starting at the top of a block: find the next block
6302 //
6303 row--;
6304 while ( row > 0 )
6305 {
6306 row-- ;
6307 if ( !(m_table->IsEmptyCell(row, col)) ) break;
6308 }
6309 }
6310 else
6311 {
6312 // starting within a block: find the top of the block
6313 //
6314 while ( row > 0 )
6315 {
6316 row-- ;
6317 if ( m_table->IsEmptyCell(row, col) )
6318 {
6319 row++ ;
6320 break;
6321 }
6322 }
6323 }
f85afd4e 6324
2d66e025 6325 MakeCellVisible( row, col );
5c8fc7c1
SN
6326 if ( expandSelection )
6327 m_selection->SelectBlock( m_currentCellCoords.GetRow(),
6328 m_currentCellCoords.GetCol(),
6329 row, col );
2d66e025 6330 SetCurrentCell( row, col );
f85afd4e 6331
2d66e025
MB
6332 return TRUE;
6333 }
f85afd4e 6334
2d66e025
MB
6335 return FALSE;
6336}
f85afd4e 6337
5c8fc7c1 6338bool wxGrid::MoveCursorDownBlock( bool expandSelection )
f85afd4e 6339{
2d66e025
MB
6340 if ( m_table &&
6341 m_currentCellCoords != wxGridNoCellCoords &&
6342 m_currentCellCoords.GetRow() < m_numRows-1 )
f85afd4e 6343 {
2d66e025
MB
6344 int row = m_currentCellCoords.GetRow();
6345 int col = m_currentCellCoords.GetCol();
6346
6347 if ( m_table->IsEmptyCell(row, col) )
6348 {
6349 // starting in an empty cell: find the next block of
6350 // non-empty cells
6351 //
6352 while ( row < m_numRows-1 )
6353 {
6354 row++ ;
6355 if ( !(m_table->IsEmptyCell(row, col)) ) break;
6356 }
6357 }
6358 else if ( m_table->IsEmptyCell(row+1, col) )
6359 {
6360 // starting at the bottom of a block: find the next block
6361 //
6362 row++;
6363 while ( row < m_numRows-1 )
6364 {
6365 row++ ;
6366 if ( !(m_table->IsEmptyCell(row, col)) ) break;
6367 }
6368 }
6369 else
6370 {
6371 // starting within a block: find the bottom of the block
6372 //
6373 while ( row < m_numRows-1 )
6374 {
6375 row++ ;
6376 if ( m_table->IsEmptyCell(row, col) )
6377 {
6378 row-- ;
6379 break;
6380 }
6381 }
6382 }
6383
6384 MakeCellVisible( row, col );
5c8fc7c1
SN
6385 if ( expandSelection )
6386 m_selection->SelectBlock( m_currentCellCoords.GetRow(),
6387 m_currentCellCoords.GetCol(),
6388 row, col );
2d66e025
MB
6389 SetCurrentCell( row, col );
6390
6391 return TRUE;
f85afd4e 6392 }
f85afd4e 6393
2d66e025
MB
6394 return FALSE;
6395}
f85afd4e 6396
5c8fc7c1 6397bool wxGrid::MoveCursorLeftBlock( bool expandSelection )
f85afd4e 6398{
2d66e025
MB
6399 if ( m_table &&
6400 m_currentCellCoords != wxGridNoCellCoords &&
6401 m_currentCellCoords.GetCol() > 0 )
f85afd4e 6402 {
2d66e025
MB
6403 int row = m_currentCellCoords.GetRow();
6404 int col = m_currentCellCoords.GetCol();
6405
6406 if ( m_table->IsEmptyCell(row, col) )
6407 {
6408 // starting in an empty cell: find the next block of
6409 // non-empty cells
6410 //
6411 while ( col > 0 )
6412 {
6413 col-- ;
6414 if ( !(m_table->IsEmptyCell(row, col)) ) break;
6415 }
6416 }
6417 else if ( m_table->IsEmptyCell(row, col-1) )
6418 {
6419 // starting at the left of a block: find the next block
6420 //
6421 col--;
6422 while ( col > 0 )
6423 {
6424 col-- ;
6425 if ( !(m_table->IsEmptyCell(row, col)) ) break;
6426 }
6427 }
6428 else
6429 {
6430 // starting within a block: find the left of the block
6431 //
6432 while ( col > 0 )
6433 {
6434 col-- ;
6435 if ( m_table->IsEmptyCell(row, col) )
6436 {
6437 col++ ;
6438 break;
6439 }
6440 }
6441 }
f85afd4e 6442
2d66e025 6443 MakeCellVisible( row, col );
5c8fc7c1
SN
6444 if ( expandSelection )
6445 m_selection->SelectBlock( m_currentCellCoords.GetRow(),
6446 m_currentCellCoords.GetCol(),
6447 row, col );
2d66e025 6448 SetCurrentCell( row, col );
8f177c8e 6449
2d66e025 6450 return TRUE;
f85afd4e 6451 }
2d66e025
MB
6452
6453 return FALSE;
f85afd4e
MB
6454}
6455
5c8fc7c1 6456bool wxGrid::MoveCursorRightBlock( bool expandSelection )
f85afd4e 6457{
2d66e025
MB
6458 if ( m_table &&
6459 m_currentCellCoords != wxGridNoCellCoords &&
6460 m_currentCellCoords.GetCol() < m_numCols-1 )
f85afd4e 6461 {
2d66e025
MB
6462 int row = m_currentCellCoords.GetRow();
6463 int col = m_currentCellCoords.GetCol();
8f177c8e 6464
2d66e025
MB
6465 if ( m_table->IsEmptyCell(row, col) )
6466 {
6467 // starting in an empty cell: find the next block of
6468 // non-empty cells
6469 //
6470 while ( col < m_numCols-1 )
6471 {
6472 col++ ;
6473 if ( !(m_table->IsEmptyCell(row, col)) ) break;
6474 }
6475 }
6476 else if ( m_table->IsEmptyCell(row, col+1) )
6477 {
6478 // starting at the right of a block: find the next block
6479 //
6480 col++;
6481 while ( col < m_numCols-1 )
6482 {
6483 col++ ;
6484 if ( !(m_table->IsEmptyCell(row, col)) ) break;
6485 }
6486 }
6487 else
6488 {
6489 // starting within a block: find the right of the block
6490 //
6491 while ( col < m_numCols-1 )
6492 {
6493 col++ ;
6494 if ( m_table->IsEmptyCell(row, col) )
6495 {
6496 col-- ;
6497 break;
6498 }
6499 }
6500 }
8f177c8e 6501
2d66e025 6502 MakeCellVisible( row, col );
5c8fc7c1
SN
6503 if ( expandSelection )
6504 m_selection->SelectBlock( m_currentCellCoords.GetRow(),
6505 m_currentCellCoords.GetCol(),
6506 row, col );
2d66e025 6507 SetCurrentCell( row, col );
f85afd4e 6508
2d66e025 6509 return TRUE;
f85afd4e 6510 }
2d66e025
MB
6511
6512 return FALSE;
f85afd4e
MB
6513}
6514
6515
2d66e025 6516
f85afd4e 6517//
2d66e025 6518// ------ Label values and formatting
f85afd4e
MB
6519//
6520
6521void wxGrid::GetRowLabelAlignment( int *horiz, int *vert )
6522{
6523 *horiz = m_rowLabelHorizAlign;
6524 *vert = m_rowLabelVertAlign;
6525}
6526
6527void wxGrid::GetColLabelAlignment( int *horiz, int *vert )
6528{
6529 *horiz = m_colLabelHorizAlign;
6530 *vert = m_colLabelVertAlign;
6531}
6532
6533wxString wxGrid::GetRowLabelValue( int row )
6534{
6535 if ( m_table )
6536 {
6537 return m_table->GetRowLabelValue( row );
6538 }
6539 else
6540 {
6541 wxString s;
6542 s << row;
6543 return s;
6544 }
6545}
6546
6547wxString wxGrid::GetColLabelValue( int col )
6548{
6549 if ( m_table )
6550 {
6551 return m_table->GetColLabelValue( col );
6552 }
6553 else
6554 {
6555 wxString s;
6556 s << col;
6557 return s;
6558 }
6559}
6560
2e8cd977 6561
f85afd4e
MB
6562void wxGrid::SetRowLabelSize( int width )
6563{
2e8cd977
MB
6564 width = wxMax( width, 0 );
6565 if ( width != m_rowLabelWidth )
6566 {
2e8cd977
MB
6567 if ( width == 0 )
6568 {
6569 m_rowLabelWin->Show( FALSE );
7807d81c 6570 m_cornerLabelWin->Show( FALSE );
2e8cd977 6571 }
7807d81c 6572 else if ( m_rowLabelWidth == 0 )
2e8cd977 6573 {
7807d81c
MB
6574 m_rowLabelWin->Show( TRUE );
6575 if ( m_colLabelHeight > 0 ) m_cornerLabelWin->Show( TRUE );
2e8cd977 6576 }
b99be8fb 6577
2e8cd977 6578 m_rowLabelWidth = width;
7807d81c
MB
6579 CalcWindowSizes();
6580 Refresh( TRUE );
2e8cd977 6581 }
f85afd4e
MB
6582}
6583
2e8cd977 6584
f85afd4e
MB
6585void wxGrid::SetColLabelSize( int height )
6586{
7807d81c 6587 height = wxMax( height, 0 );
2e8cd977
MB
6588 if ( height != m_colLabelHeight )
6589 {
2e8cd977
MB
6590 if ( height == 0 )
6591 {
2e8cd977 6592 m_colLabelWin->Show( FALSE );
7807d81c 6593 m_cornerLabelWin->Show( FALSE );
2e8cd977 6594 }
7807d81c 6595 else if ( m_colLabelHeight == 0 )
2e8cd977 6596 {
7807d81c
MB
6597 m_colLabelWin->Show( TRUE );
6598 if ( m_rowLabelWidth > 0 ) m_cornerLabelWin->Show( TRUE );
2e8cd977 6599 }
7807d81c 6600
2e8cd977 6601 m_colLabelHeight = height;
7807d81c
MB
6602 CalcWindowSizes();
6603 Refresh( TRUE );
2e8cd977 6604 }
f85afd4e
MB
6605}
6606
2e8cd977 6607
f85afd4e
MB
6608void wxGrid::SetLabelBackgroundColour( const wxColour& colour )
6609{
2d66e025
MB
6610 if ( m_labelBackgroundColour != colour )
6611 {
6612 m_labelBackgroundColour = colour;
6613 m_rowLabelWin->SetBackgroundColour( colour );
6614 m_colLabelWin->SetBackgroundColour( colour );
6615 m_cornerLabelWin->SetBackgroundColour( colour );
6616
6617 if ( !GetBatchCount() )
6618 {
6619 m_rowLabelWin->Refresh();
6620 m_colLabelWin->Refresh();
6621 m_cornerLabelWin->Refresh();
6622 }
6623 }
f85afd4e
MB
6624}
6625
6626void wxGrid::SetLabelTextColour( const wxColour& colour )
6627{
2d66e025
MB
6628 if ( m_labelTextColour != colour )
6629 {
6630 m_labelTextColour = colour;
6631 if ( !GetBatchCount() )
6632 {
6633 m_rowLabelWin->Refresh();
6634 m_colLabelWin->Refresh();
6635 }
6636 }
f85afd4e
MB
6637}
6638
6639void wxGrid::SetLabelFont( const wxFont& font )
6640{
6641 m_labelFont = font;
2d66e025
MB
6642 if ( !GetBatchCount() )
6643 {
6644 m_rowLabelWin->Refresh();
6645 m_colLabelWin->Refresh();
6646 }
f85afd4e
MB
6647}
6648
6649void wxGrid::SetRowLabelAlignment( int horiz, int vert )
6650{
6651 if ( horiz == wxLEFT || horiz == wxCENTRE || horiz == wxRIGHT )
6652 {
6653 m_rowLabelHorizAlign = horiz;
6654 }
8f177c8e 6655
f85afd4e
MB
6656 if ( vert == wxTOP || vert == wxCENTRE || vert == wxBOTTOM )
6657 {
6658 m_rowLabelVertAlign = vert;
6659 }
6660
2d66e025
MB
6661 if ( !GetBatchCount() )
6662 {
6663 m_rowLabelWin->Refresh();
60ff3b99 6664 }
f85afd4e
MB
6665}
6666
6667void wxGrid::SetColLabelAlignment( int horiz, int vert )
6668{
6669 if ( horiz == wxLEFT || horiz == wxCENTRE || horiz == wxRIGHT )
6670 {
6671 m_colLabelHorizAlign = horiz;
6672 }
8f177c8e 6673
f85afd4e
MB
6674 if ( vert == wxTOP || vert == wxCENTRE || vert == wxBOTTOM )
6675 {
6676 m_colLabelVertAlign = vert;
6677 }
6678
2d66e025
MB
6679 if ( !GetBatchCount() )
6680 {
2d66e025 6681 m_colLabelWin->Refresh();
60ff3b99 6682 }
f85afd4e
MB
6683}
6684
6685void wxGrid::SetRowLabelValue( int row, const wxString& s )
6686{
6687 if ( m_table )
6688 {
6689 m_table->SetRowLabelValue( row, s );
2d66e025
MB
6690 if ( !GetBatchCount() )
6691 {
70c7a608
SN
6692 wxRect rect = CellToRect( row, 0);
6693 if ( rect.height > 0 )
6694 {
cb309039 6695 CalcScrolledPosition(0, rect.y, &rect.x, &rect.y);
70c7a608
SN
6696 rect.x = m_left;
6697 rect.width = m_rowLabelWidth;
6698 m_rowLabelWin->Refresh( TRUE, &rect );
6699 }
2d66e025 6700 }
f85afd4e
MB
6701 }
6702}
6703
6704void wxGrid::SetColLabelValue( int col, const wxString& s )
6705{
6706 if ( m_table )
6707 {
6708 m_table->SetColLabelValue( col, s );
2d66e025
MB
6709 if ( !GetBatchCount() )
6710 {
70c7a608
SN
6711 wxRect rect = CellToRect( 0, col );
6712 if ( rect.width > 0 )
6713 {
cb309039 6714 CalcScrolledPosition(rect.x, 0, &rect.x, &rect.y);
70c7a608
SN
6715 rect.y = m_top;
6716 rect.height = m_colLabelHeight;
6717 m_colLabelWin->Refresh( TRUE, &rect );
6718 }
2d66e025 6719 }
f85afd4e
MB
6720 }
6721}
6722
6723void wxGrid::SetGridLineColour( const wxColour& colour )
6724{
2d66e025
MB
6725 if ( m_gridLineColour != colour )
6726 {
6727 m_gridLineColour = colour;
60ff3b99 6728
2d66e025
MB
6729 wxClientDC dc( m_gridWin );
6730 PrepareDC( dc );
c6a51dcd 6731 DrawAllGridLines( dc, wxRegion() );
2d66e025 6732 }
f85afd4e
MB
6733}
6734
6735void wxGrid::EnableGridLines( bool enable )
6736{
6737 if ( enable != m_gridLinesEnabled )
6738 {
6739 m_gridLinesEnabled = enable;
2d66e025
MB
6740
6741 if ( !GetBatchCount() )
6742 {
6743 if ( enable )
6744 {
6745 wxClientDC dc( m_gridWin );
6746 PrepareDC( dc );
c6a51dcd 6747 DrawAllGridLines( dc, wxRegion() );
2d66e025
MB
6748 }
6749 else
6750 {
6751 m_gridWin->Refresh();
6752 }
6753 }
f85afd4e
MB
6754 }
6755}
6756
6757
6758int wxGrid::GetDefaultRowSize()
6759{
6760 return m_defaultRowHeight;
6761}
6762
6763int wxGrid::GetRowSize( int row )
6764{
b99be8fb
VZ
6765 wxCHECK_MSG( row >= 0 && row < m_numRows, 0, _T("invalid row index") );
6766
7c1cb261 6767 return GetRowHeight(row);
f85afd4e
MB
6768}
6769
6770int wxGrid::GetDefaultColSize()
6771{
6772 return m_defaultColWidth;
6773}
6774
6775int wxGrid::GetColSize( int col )
6776{
b99be8fb
VZ
6777 wxCHECK_MSG( col >= 0 && col < m_numCols, 0, _T("invalid column index") );
6778
7c1cb261 6779 return GetColWidth(col);
f85afd4e
MB
6780}
6781
2e9a6788
VZ
6782// ============================================================================
6783// access to the grid attributes: each of them has a default value in the grid
6784// itself and may be overidden on a per-cell basis
6785// ============================================================================
6786
6787// ----------------------------------------------------------------------------
6788// setting default attributes
6789// ----------------------------------------------------------------------------
6790
6791void wxGrid::SetDefaultCellBackgroundColour( const wxColour& col )
6792{
2796cce3 6793 m_defaultCellAttr->SetBackgroundColour(col);
c916e13b
RR
6794#ifdef __WXGTK__
6795 m_gridWin->SetBackgroundColour(col);
6796#endif
2e9a6788
VZ
6797}
6798
6799void wxGrid::SetDefaultCellTextColour( const wxColour& col )
6800{
2796cce3 6801 m_defaultCellAttr->SetTextColour(col);
2e9a6788
VZ
6802}
6803
6804void wxGrid::SetDefaultCellAlignment( int horiz, int vert )
6805{
2796cce3 6806 m_defaultCellAttr->SetAlignment(horiz, vert);
2e9a6788
VZ
6807}
6808
6809void wxGrid::SetDefaultCellFont( const wxFont& font )
6810{
2796cce3
RD
6811 m_defaultCellAttr->SetFont(font);
6812}
6813
0ba143c9
RD
6814void wxGrid::SetDefaultRenderer(wxGridCellRenderer *renderer)
6815{
6816 m_defaultCellAttr->SetRenderer(renderer);
6817}
2e9a6788 6818
0ba143c9
RD
6819void wxGrid::SetDefaultEditor(wxGridCellEditor *editor)
6820{
6821 m_defaultCellAttr->SetEditor(editor);
6822}
9b4aede2 6823
2e9a6788
VZ
6824// ----------------------------------------------------------------------------
6825// access to the default attrbiutes
6826// ----------------------------------------------------------------------------
6827
f85afd4e
MB
6828wxColour wxGrid::GetDefaultCellBackgroundColour()
6829{
2796cce3 6830 return m_defaultCellAttr->GetBackgroundColour();
f85afd4e
MB
6831}
6832
2e9a6788
VZ
6833wxColour wxGrid::GetDefaultCellTextColour()
6834{
2796cce3 6835 return m_defaultCellAttr->GetTextColour();
2e9a6788
VZ
6836}
6837
6838wxFont wxGrid::GetDefaultCellFont()
6839{
2796cce3 6840 return m_defaultCellAttr->GetFont();
2e9a6788
VZ
6841}
6842
6843void wxGrid::GetDefaultCellAlignment( int *horiz, int *vert )
6844{
2796cce3 6845 m_defaultCellAttr->GetAlignment(horiz, vert);
2e9a6788
VZ
6846}
6847
0ba143c9
RD
6848wxGridCellRenderer *wxGrid::GetDefaultRenderer() const
6849{
0b190b0f 6850 return m_defaultCellAttr->GetRenderer(NULL, 0, 0);
0ba143c9 6851}
ab79958a 6852
0ba143c9
RD
6853wxGridCellEditor *wxGrid::GetDefaultEditor() const
6854{
28a77bc4 6855 return m_defaultCellAttr->GetEditor(NULL,0,0);
0ba143c9 6856}
9b4aede2 6857
2e9a6788
VZ
6858// ----------------------------------------------------------------------------
6859// access to cell attributes
6860// ----------------------------------------------------------------------------
6861
b99be8fb 6862wxColour wxGrid::GetCellBackgroundColour(int row, int col)
f85afd4e 6863{
0a976765 6864 wxGridCellAttr *attr = GetCellAttr(row, col);
2796cce3 6865 wxColour colour = attr->GetBackgroundColour();
39bcce60 6866 attr->DecRef();
b99be8fb 6867 return colour;
f85afd4e
MB
6868}
6869
b99be8fb 6870wxColour wxGrid::GetCellTextColour( int row, int col )
f85afd4e 6871{
0a976765 6872 wxGridCellAttr *attr = GetCellAttr(row, col);
2796cce3 6873 wxColour colour = attr->GetTextColour();
39bcce60 6874 attr->DecRef();
b99be8fb 6875 return colour;
f85afd4e
MB
6876}
6877
b99be8fb 6878wxFont wxGrid::GetCellFont( int row, int col )
f85afd4e 6879{
0a976765 6880 wxGridCellAttr *attr = GetCellAttr(row, col);
2796cce3 6881 wxFont font = attr->GetFont();
39bcce60 6882 attr->DecRef();
b99be8fb 6883 return font;
f85afd4e
MB
6884}
6885
b99be8fb 6886void wxGrid::GetCellAlignment( int row, int col, int *horiz, int *vert )
f85afd4e 6887{
0a976765 6888 wxGridCellAttr *attr = GetCellAttr(row, col);
2796cce3 6889 attr->GetAlignment(horiz, vert);
39bcce60 6890 attr->DecRef();
2e9a6788
VZ
6891}
6892
2796cce3
RD
6893wxGridCellRenderer* wxGrid::GetCellRenderer(int row, int col)
6894{
6895 wxGridCellAttr* attr = GetCellAttr(row, col);
28a77bc4 6896 wxGridCellRenderer* renderer = attr->GetRenderer(this, row, col);
2796cce3 6897 attr->DecRef();
0b190b0f 6898
2796cce3
RD
6899 return renderer;
6900}
6901
9b4aede2
RD
6902wxGridCellEditor* wxGrid::GetCellEditor(int row, int col)
6903{
6904 wxGridCellAttr* attr = GetCellAttr(row, col);
28a77bc4 6905 wxGridCellEditor* editor = attr->GetEditor(this, row, col);
9b4aede2 6906 attr->DecRef();
0b190b0f 6907
9b4aede2
RD
6908 return editor;
6909}
6910
283b7808
VZ
6911bool wxGrid::IsReadOnly(int row, int col) const
6912{
6913 wxGridCellAttr* attr = GetCellAttr(row, col);
6914 bool isReadOnly = attr->IsReadOnly();
6915 attr->DecRef();
6916 return isReadOnly;
6917}
6918
2e9a6788 6919// ----------------------------------------------------------------------------
758cbedf 6920// attribute support: cache, automatic provider creation, ...
2e9a6788
VZ
6921// ----------------------------------------------------------------------------
6922
6923bool wxGrid::CanHaveAttributes()
6924{
6925 if ( !m_table )
6926 {
6927 return FALSE;
6928 }
6929
f2d76237 6930 return m_table->CanHaveAttributes();
2e9a6788
VZ
6931}
6932
0a976765
VZ
6933void wxGrid::ClearAttrCache()
6934{
6935 if ( m_attrCache.row != -1 )
6936 {
39bcce60 6937 wxSafeDecRef(m_attrCache.attr);
0a976765
VZ
6938 m_attrCache.row = -1;
6939 }
6940}
6941
6942void wxGrid::CacheAttr(int row, int col, wxGridCellAttr *attr) const
6943{
6944 wxGrid *self = (wxGrid *)this; // const_cast
6945
6946 self->ClearAttrCache();
6947 self->m_attrCache.row = row;
6948 self->m_attrCache.col = col;
6949 self->m_attrCache.attr = attr;
39bcce60 6950 wxSafeIncRef(attr);
0a976765
VZ
6951}
6952
6953bool wxGrid::LookupAttr(int row, int col, wxGridCellAttr **attr) const
6954{
6955 if ( row == m_attrCache.row && col == m_attrCache.col )
6956 {
6957 *attr = m_attrCache.attr;
39bcce60 6958 wxSafeIncRef(m_attrCache.attr);
0a976765
VZ
6959
6960#ifdef DEBUG_ATTR_CACHE
6961 gs_nAttrCacheHits++;
6962#endif
6963
6964 return TRUE;
6965 }
6966 else
6967 {
6968#ifdef DEBUG_ATTR_CACHE
6969 gs_nAttrCacheMisses++;
6970#endif
6971 return FALSE;
6972 }
6973}
6974
2e9a6788
VZ
6975wxGridCellAttr *wxGrid::GetCellAttr(int row, int col) const
6976{
0a976765
VZ
6977 wxGridCellAttr *attr;
6978 if ( !LookupAttr(row, col, &attr) )
2e9a6788 6979 {
0a976765
VZ
6980 attr = m_table ? m_table->GetAttr(row, col) : (wxGridCellAttr *)NULL;
6981 CacheAttr(row, col, attr);
6982 }
508011ce
VZ
6983 if (attr)
6984 {
2796cce3 6985 attr->SetDefAttr(m_defaultCellAttr);
508011ce
VZ
6986 }
6987 else
6988 {
2796cce3
RD
6989 attr = m_defaultCellAttr;
6990 attr->IncRef();
6991 }
2e9a6788 6992
0a976765
VZ
6993 return attr;
6994}
6995
6996wxGridCellAttr *wxGrid::GetOrCreateCellAttr(int row, int col) const
6997{
6998 wxGridCellAttr *attr;
6999 if ( !LookupAttr(row, col, &attr) || !attr )
7000 {
7001 wxASSERT_MSG( m_table,
7002 _T("we may only be called if CanHaveAttributes() "
7003 "returned TRUE and then m_table should be !NULL") );
7004
7005 attr = m_table->GetAttr(row, col);
7006 if ( !attr )
7007 {
7008 attr = new wxGridCellAttr;
7009
7010 // artificially inc the ref count to match DecRef() in caller
7011 attr->IncRef();
7012
7013 m_table->SetAttr(attr, row, col);
7014 }
2e9a6788 7015
0a976765 7016 CacheAttr(row, col, attr);
2e9a6788 7017 }
2796cce3 7018 attr->SetDefAttr(m_defaultCellAttr);
2e9a6788
VZ
7019 return attr;
7020}
7021
0b190b0f
VZ
7022// ----------------------------------------------------------------------------
7023// setting column attributes (wrappers around SetColAttr)
7024// ----------------------------------------------------------------------------
7025
7026void wxGrid::SetColFormatBool(int col)
7027{
7028 SetColFormatCustom(col, wxGRID_VALUE_BOOL);
7029}
7030
7031void wxGrid::SetColFormatNumber(int col)
7032{
7033 SetColFormatCustom(col, wxGRID_VALUE_NUMBER);
7034}
7035
7036void wxGrid::SetColFormatFloat(int col, int width, int precision)
7037{
7038 wxString typeName = wxGRID_VALUE_FLOAT;
7039 if ( (width != -1) || (precision != -1) )
7040 {
7041 typeName << _T(':') << width << _T(',') << precision;
7042 }
7043
7044 SetColFormatCustom(col, typeName);
7045}
7046
7047void wxGrid::SetColFormatCustom(int col, const wxString& typeName)
7048{
7049 wxGridCellAttr *attr = new wxGridCellAttr;
7050 wxGridCellRenderer *renderer = GetDefaultRendererForType(typeName);
7051 attr->SetRenderer(renderer);
7052
7053 SetColAttr(col, attr);
7054}
7055
758cbedf
VZ
7056// ----------------------------------------------------------------------------
7057// setting cell attributes: this is forwarded to the table
7058// ----------------------------------------------------------------------------
7059
7060void wxGrid::SetRowAttr(int row, wxGridCellAttr *attr)
7061{
7062 if ( CanHaveAttributes() )
7063 {
7064 m_table->SetRowAttr(attr, row);
7065 }
7066 else
7067 {
39bcce60 7068 wxSafeDecRef(attr);
758cbedf
VZ
7069 }
7070}
7071
7072void wxGrid::SetColAttr(int col, wxGridCellAttr *attr)
7073{
7074 if ( CanHaveAttributes() )
7075 {
7076 m_table->SetColAttr(attr, col);
7077 }
7078 else
7079 {
39bcce60 7080 wxSafeDecRef(attr);
758cbedf
VZ
7081 }
7082}
7083
2e9a6788
VZ
7084void wxGrid::SetCellBackgroundColour( int row, int col, const wxColour& colour )
7085{
7086 if ( CanHaveAttributes() )
7087 {
0a976765 7088 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
2e9a6788
VZ
7089 attr->SetBackgroundColour(colour);
7090 attr->DecRef();
7091 }
7092}
7093
7094void wxGrid::SetCellTextColour( int row, int col, const wxColour& colour )
7095{
7096 if ( CanHaveAttributes() )
7097 {
0a976765 7098 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
2e9a6788
VZ
7099 attr->SetTextColour(colour);
7100 attr->DecRef();
7101 }
7102}
7103
7104void wxGrid::SetCellFont( int row, int col, const wxFont& font )
7105{
7106 if ( CanHaveAttributes() )
7107 {
0a976765 7108 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
2e9a6788
VZ
7109 attr->SetFont(font);
7110 attr->DecRef();
7111 }
7112}
7113
7114void wxGrid::SetCellAlignment( int row, int col, int horiz, int vert )
7115{
7116 if ( CanHaveAttributes() )
7117 {
0a976765 7118 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
2e9a6788
VZ
7119 attr->SetAlignment(horiz, vert);
7120 attr->DecRef();
ab79958a
VZ
7121 }
7122}
7123
7124void wxGrid::SetCellRenderer(int row, int col, wxGridCellRenderer *renderer)
7125{
7126 if ( CanHaveAttributes() )
7127 {
0a976765 7128 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
ab79958a
VZ
7129 attr->SetRenderer(renderer);
7130 attr->DecRef();
9b4aede2
RD
7131 }
7132}
7133
7134void wxGrid::SetCellEditor(int row, int col, wxGridCellEditor* editor)
7135{
7136 if ( CanHaveAttributes() )
7137 {
7138 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
7139 attr->SetEditor(editor);
7140 attr->DecRef();
283b7808
VZ
7141 }
7142}
7143
7144void wxGrid::SetReadOnly(int row, int col, bool isReadOnly)
7145{
7146 if ( CanHaveAttributes() )
7147 {
7148 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
7149 attr->SetReadOnly(isReadOnly);
7150 attr->DecRef();
2e9a6788 7151 }
f85afd4e
MB
7152}
7153
f2d76237
RD
7154// ----------------------------------------------------------------------------
7155// Data type registration
7156// ----------------------------------------------------------------------------
7157
7158void wxGrid::RegisterDataType(const wxString& typeName,
7159 wxGridCellRenderer* renderer,
7160 wxGridCellEditor* editor)
7161{
7162 m_typeRegistry->RegisterDataType(typeName, renderer, editor);
7163}
7164
7165
99306db2 7166wxGridCellEditor* wxGrid::GetDefaultEditorForCell(int row, int col) const
f2d76237
RD
7167{
7168 wxString typeName = m_table->GetTypeName(row, col);
7169 return GetDefaultEditorForType(typeName);
7170}
7171
99306db2 7172wxGridCellRenderer* wxGrid::GetDefaultRendererForCell(int row, int col) const
f2d76237
RD
7173{
7174 wxString typeName = m_table->GetTypeName(row, col);
7175 return GetDefaultRendererForType(typeName);
7176}
7177
99306db2
VZ
7178wxGridCellEditor*
7179wxGrid::GetDefaultEditorForType(const wxString& typeName) const
f2d76237 7180{
c4608a8a 7181 int index = m_typeRegistry->FindOrCloneDataType(typeName);
0b190b0f
VZ
7182 if ( index == wxNOT_FOUND )
7183 {
7184 wxFAIL_MSG(wxT("Unknown data type name"));
7185
f2d76237
RD
7186 return NULL;
7187 }
0b190b0f 7188
f2d76237
RD
7189 return m_typeRegistry->GetEditor(index);
7190}
7191
99306db2
VZ
7192wxGridCellRenderer*
7193wxGrid::GetDefaultRendererForType(const wxString& typeName) const
f2d76237 7194{
c4608a8a 7195 int index = m_typeRegistry->FindOrCloneDataType(typeName);
0b190b0f
VZ
7196 if ( index == wxNOT_FOUND )
7197 {
c4608a8a 7198 wxFAIL_MSG(wxT("Unknown data type name"));
0b190b0f 7199
c4608a8a 7200 return NULL;
e72b4213 7201 }
0b190b0f 7202
c4608a8a 7203 return m_typeRegistry->GetRenderer(index);
f2d76237
RD
7204}
7205
7206
2e9a6788
VZ
7207// ----------------------------------------------------------------------------
7208// row/col size
7209// ----------------------------------------------------------------------------
7210
6e8524b1
MB
7211void wxGrid::EnableDragRowSize( bool enable )
7212{
7213 m_canDragRowSize = enable;
7214}
7215
7216
7217void wxGrid::EnableDragColSize( bool enable )
7218{
7219 m_canDragColSize = enable;
7220}
7221
4cfa5de6
RD
7222void wxGrid::EnableDragGridSize( bool enable )
7223{
7224 m_canDragGridSize = enable;
7225}
7226
6e8524b1 7227
f85afd4e
MB
7228void wxGrid::SetDefaultRowSize( int height, bool resizeExistingRows )
7229{
7230 m_defaultRowHeight = wxMax( height, WXGRID_MIN_ROW_HEIGHT );
7231
7232 if ( resizeExistingRows )
7233 {
7c1cb261
VZ
7234 InitRowHeights();
7235
f85afd4e 7236 CalcDimensions();
f85afd4e
MB
7237 }
7238}
7239
7240void wxGrid::SetRowSize( int row, int height )
7241{
b99be8fb 7242 wxCHECK_RET( row >= 0 && row < m_numRows, _T("invalid row index") );
60ff3b99 7243
7c1cb261
VZ
7244 if ( m_rowHeights.IsEmpty() )
7245 {
7246 // need to really create the array
7247 InitRowHeights();
7248 }
60ff3b99 7249
b99be8fb
VZ
7250 int h = wxMax( 0, height );
7251 int diff = h - m_rowHeights[row];
60ff3b99 7252
b99be8fb 7253 m_rowHeights[row] = h;
7c1cb261 7254 int i;
b99be8fb 7255 for ( i = row; i < m_numRows; i++ )
f85afd4e 7256 {
b99be8fb 7257 m_rowBottoms[i] += diff;
f85afd4e 7258 }
b99be8fb 7259 CalcDimensions();
f85afd4e
MB
7260}
7261
7262void wxGrid::SetDefaultColSize( int width, bool resizeExistingCols )
7263{
7264 m_defaultColWidth = wxMax( width, WXGRID_MIN_COL_WIDTH );
7265
7266 if ( resizeExistingCols )
7267 {
7c1cb261
VZ
7268 InitColWidths();
7269
f85afd4e 7270 CalcDimensions();
f85afd4e
MB
7271 }
7272}
7273
7274void wxGrid::SetColSize( int col, int width )
7275{
b99be8fb 7276 wxCHECK_RET( col >= 0 && col < m_numCols, _T("invalid column index") );
60ff3b99 7277
43947979
VZ
7278 // should we check that it's bigger than GetColMinimalWidth(col) here?
7279
7c1cb261
VZ
7280 if ( m_colWidths.IsEmpty() )
7281 {
7282 // need to really create the array
7283 InitColWidths();
7284 }
f85afd4e 7285
b99be8fb
VZ
7286 int w = wxMax( 0, width );
7287 int diff = w - m_colWidths[col];
7288 m_colWidths[col] = w;
60ff3b99 7289
7c1cb261 7290 int i;
b99be8fb 7291 for ( i = col; i < m_numCols; i++ )
f85afd4e 7292 {
b99be8fb 7293 m_colRights[i] += diff;
f85afd4e 7294 }
b99be8fb 7295 CalcDimensions();
f85afd4e
MB
7296}
7297
2d66e025 7298
43947979
VZ
7299void wxGrid::SetColMinimalWidth( int col, int width )
7300{
af547d51
VZ
7301 m_colMinWidths.Put(col, width);
7302}
7303
7304void wxGrid::SetRowMinimalHeight( int row, int width )
7305{
7306 m_rowMinHeights.Put(row, width);
43947979
VZ
7307}
7308
7309int wxGrid::GetColMinimalWidth(int col) const
7310{
af547d51
VZ
7311 long value = m_colMinWidths.Get(col);
7312 return value != wxNOT_FOUND ? (int)value : WXGRID_MIN_COL_WIDTH;
7313}
7314
7315int wxGrid::GetRowMinimalHeight(int row) const
7316{
7317 long value = m_rowMinHeights.Get(row);
7318 return value != wxNOT_FOUND ? (int)value : WXGRID_MIN_ROW_HEIGHT;
43947979
VZ
7319}
7320
57c086ef
VZ
7321// ----------------------------------------------------------------------------
7322// auto sizing
7323// ----------------------------------------------------------------------------
7324
af547d51 7325void wxGrid::AutoSizeColOrRow( int colOrRow, bool setAsMin, bool column )
65e4e78e
VZ
7326{
7327 wxClientDC dc(m_gridWin);
7328
a95e38c0
VZ
7329 // init both of them to avoid compiler warnings, even if weo nly need one
7330 int row = -1,
7331 col = -1;
af547d51
VZ
7332 if ( column )
7333 col = colOrRow;
7334 else
7335 row = colOrRow;
7336
7337 wxCoord extent, extentMax = 0;
7338 int max = column ? m_numRows : m_numCols;
39bcce60 7339 for ( int rowOrCol = 0; rowOrCol < max; rowOrCol++ )
65e4e78e 7340 {
af547d51
VZ
7341 if ( column )
7342 row = rowOrCol;
7343 else
7344 col = rowOrCol;
7345
65e4e78e 7346 wxGridCellAttr* attr = GetCellAttr(row, col);
28a77bc4 7347 wxGridCellRenderer* renderer = attr->GetRenderer(this, row, col);
65e4e78e
VZ
7348 if ( renderer )
7349 {
af547d51
VZ
7350 wxSize size = renderer->GetBestSize(*this, *attr, dc, row, col);
7351 extent = column ? size.x : size.y;
7352 if ( extent > extentMax )
65e4e78e 7353 {
af547d51 7354 extentMax = extent;
65e4e78e 7355 }
0b190b0f
VZ
7356
7357 renderer->DecRef();
65e4e78e
VZ
7358 }
7359
7360 attr->DecRef();
7361 }
7362
af547d51
VZ
7363 // now also compare with the column label extent
7364 wxCoord w, h;
65e4e78e 7365 dc.SetFont( GetLabelFont() );
294d195c
MB
7366
7367 if ( column )
7368 dc.GetTextExtent( GetColLabelValue(col), &w, &h );
7369 else
7370 dc.GetTextExtent( GetRowLabelValue(col), &w, &h );
7371
af547d51
VZ
7372 extent = column ? w : h;
7373 if ( extent > extentMax )
65e4e78e 7374 {
af547d51 7375 extentMax = extent;
65e4e78e
VZ
7376 }
7377
af547d51 7378 if ( !extentMax )
65e4e78e 7379 {
af547d51
VZ
7380 // empty column - give default extent (notice that if extentMax is less
7381 // than default extent but != 0, it's ok)
7382 extentMax = column ? m_defaultColWidth : m_defaultRowHeight;
65e4e78e
VZ
7383 }
7384 else
7385 {
a95e38c0
VZ
7386 if ( column )
7387 {
7388 // leave some space around text
7389 extentMax += 10;
7390 }
65e4e78e
VZ
7391 }
7392
af547d51
VZ
7393 if ( column )
7394 SetColSize(col, extentMax);
7395 else
39bcce60 7396 SetRowSize(row, extentMax);
af547d51 7397
65e4e78e
VZ
7398 if ( setAsMin )
7399 {
af547d51
VZ
7400 if ( column )
7401 SetColMinimalWidth(col, extentMax);
7402 else
39bcce60 7403 SetRowMinimalHeight(row, extentMax);
65e4e78e
VZ
7404 }
7405}
7406
266e8367 7407int wxGrid::SetOrCalcColumnSizes(bool calcOnly, bool setAsMin)
65e4e78e 7408{
57c086ef
VZ
7409 int width = m_rowLabelWidth;
7410
65e4e78e
VZ
7411 for ( int col = 0; col < m_numCols; col++ )
7412 {
266e8367
VZ
7413 if ( !calcOnly )
7414 {
7415 AutoSizeColumn(col, setAsMin);
7416 }
57c086ef
VZ
7417
7418 width += GetColWidth(col);
65e4e78e 7419 }
57c086ef 7420
266e8367 7421 return width;
65e4e78e
VZ
7422}
7423
266e8367 7424int wxGrid::SetOrCalcRowSizes(bool calcOnly, bool setAsMin)
57c086ef
VZ
7425{
7426 int height = m_colLabelHeight;
7427
7428 for ( int row = 0; row < m_numRows; row++ )
7429 {
af547d51
VZ
7430 if ( !calcOnly )
7431 {
7432 AutoSizeRow(row, setAsMin);
7433 }
57c086ef
VZ
7434
7435 height += GetRowHeight(row);
7436 }
7437
266e8367 7438 return height;
57c086ef
VZ
7439}
7440
7441void wxGrid::AutoSize()
7442{
266e8367
VZ
7443 // set the size too
7444 SetSize(SetOrCalcColumnSizes(FALSE), SetOrCalcRowSizes(FALSE));
7445}
7446
7447wxSize wxGrid::DoGetBestSize() const
7448{
7449 // don't set sizes, only calculate them
7450 wxGrid *self = (wxGrid *)this; // const_cast
7451
7452 return wxSize(self->SetOrCalcColumnSizes(TRUE),
7453 self->SetOrCalcRowSizes(TRUE));
7454}
7455
7456void wxGrid::Fit()
7457{
7458 AutoSize();
57c086ef
VZ
7459}
7460
7461// ----------------------------------------------------------------------------
7462// cell value accessor functions
7463// ----------------------------------------------------------------------------
f85afd4e
MB
7464
7465void wxGrid::SetCellValue( int row, int col, const wxString& s )
7466{
7467 if ( m_table )
7468 {
7469 m_table->SetValue( row, col, s.c_str() );
2d66e025
MB
7470 if ( !GetBatchCount() )
7471 {
7472 wxClientDC dc( m_gridWin );
7473 PrepareDC( dc );
7474 DrawCell( dc, wxGridCellCoords(row, col) );
7475 }
60ff3b99 7476
f85afd4e 7477 if ( m_currentCellCoords.GetRow() == row &&
4cfa5de6
RD
7478 m_currentCellCoords.GetCol() == col &&
7479 IsCellEditControlEnabled())
f85afd4e 7480 {
4cfa5de6
RD
7481 HideCellEditControl();
7482 ShowCellEditControl(); // will reread data from table
f85afd4e 7483 }
f85afd4e
MB
7484 }
7485}
7486
7487
7488//
2d66e025 7489// ------ Block, row and col selection
f85afd4e
MB
7490//
7491
7492void wxGrid::SelectRow( int row, bool addToSelected )
7493{
b5808881
SN
7494 if ( IsSelection() && !addToSelected )
7495 m_selection->ClearSelection();
70c7a608 7496
b5808881 7497 m_selection->SelectRow( row );
f85afd4e
MB
7498}
7499
7500
7501void wxGrid::SelectCol( int col, bool addToSelected )
7502{
b5808881
SN
7503 if ( IsSelection() && !addToSelected )
7504 m_selection->ClearSelection();
f85afd4e 7505
b5808881 7506 m_selection->SelectCol( col );
f85afd4e
MB
7507}
7508
7509
7510void wxGrid::SelectBlock( int topRow, int leftCol, int bottomRow, int rightCol )
7511{
7512 int temp;
da6af900 7513 wxGridCellCoords updateTopLeft, updateBottomRight;
8f177c8e 7514
f85afd4e
MB
7515 if ( topRow > bottomRow )
7516 {
7517 temp = topRow;
7518 topRow = bottomRow;
7519 bottomRow = temp;
7520 }
7521
7522 if ( leftCol > rightCol )
7523 {
7524 temp = leftCol;
7525 leftCol = rightCol;
7526 rightCol = temp;
7527 }
f0102d2a 7528
70c7a608
SN
7529 updateTopLeft = wxGridCellCoords( topRow, leftCol );
7530 updateBottomRight = wxGridCellCoords( bottomRow, rightCol );
da6af900 7531
b5808881
SN
7532 if ( m_selectingTopLeft != updateTopLeft ||
7533 m_selectingBottomRight != updateBottomRight )
da6af900 7534 {
70c7a608
SN
7535 // Compute two optimal update rectangles:
7536 // Either one rectangle is a real subset of the
7537 // other, or they are (almost) disjoint!
7538 wxRect rect[4];
ee6694a7
VZ
7539 bool need_refresh[4];
7540 need_refresh[0] =
7541 need_refresh[1] =
7542 need_refresh[2] =
7543 need_refresh[3] = FALSE;
70c7a608
SN
7544 int i;
7545
7546 // Store intermediate values
b5808881
SN
7547 wxCoord oldLeft = m_selectingTopLeft.GetCol();
7548 wxCoord oldTop = m_selectingTopLeft.GetRow();
7549 wxCoord oldRight = m_selectingBottomRight.GetCol();
7550 wxCoord oldBottom = m_selectingBottomRight.GetRow();
70c7a608
SN
7551
7552 // Determine the outer/inner coordinates.
7553 if (oldLeft > leftCol)
f0102d2a 7554 {
70c7a608
SN
7555 temp = oldLeft;
7556 oldLeft = leftCol;
7557 leftCol = temp;
cb309039 7558 }
70c7a608 7559 if (oldTop > topRow )
f0102d2a 7560 {
70c7a608
SN
7561 temp = oldTop;
7562 oldTop = topRow;
7563 topRow = temp;
cb309039
SN
7564 }
7565 if (oldRight < rightCol )
70c7a608
SN
7566 {
7567 temp = oldRight;
7568 oldRight = rightCol;
7569 rightCol = temp;
cb309039
SN
7570 }
7571 if (oldBottom < bottomRow)
7572 {
70c7a608
SN
7573 temp = oldBottom;
7574 oldBottom = bottomRow;
7575 bottomRow = temp;
cb309039 7576 }
70c7a608
SN
7577
7578 // Now, either the stuff marked old is the outer
7579 // rectangle or we don't have a situation where one
7580 // is contained in the other.
b99be8fb 7581
cb309039
SN
7582 if ( oldLeft < leftCol )
7583 {
7584 need_refresh[0] = TRUE;
7585 rect[0] = BlockToDeviceRect( wxGridCellCoords ( oldTop,
7586 oldLeft ),
b99be8fb 7587 wxGridCellCoords ( oldBottom,
cb309039
SN
7588 leftCol - 1 ) );
7589 }
7590
7591 if ( oldTop < topRow )
7592 {
7593 need_refresh[1] = TRUE;
7594 rect[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop,
7595 leftCol ),
b99be8fb 7596 wxGridCellCoords ( topRow - 1,
cb309039
SN
7597 rightCol ) );
7598 }
7599
7600 if ( oldRight > rightCol )
7601 {
7602 need_refresh[2] = TRUE;
7603 rect[2] = BlockToDeviceRect( wxGridCellCoords ( oldTop,
7604 rightCol + 1 ),
7605 wxGridCellCoords ( oldBottom,
7606 oldRight ) );
7607 }
7608
7609 if ( oldBottom > bottomRow )
7610 {
7611 need_refresh[3] = TRUE;
7612 rect[3] = BlockToDeviceRect( wxGridCellCoords ( bottomRow + 1,
7613 leftCol ),
7614 wxGridCellCoords ( oldBottom,
7615 rightCol ) );
7616 }
70c7a608
SN
7617
7618
7619 // Change Selection
b5808881
SN
7620 m_selectingTopLeft = updateTopLeft;
7621 m_selectingBottomRight = updateBottomRight;
b99be8fb 7622
70c7a608
SN
7623 // various Refresh() calls
7624 for (i = 0; i < 4; i++ )
7625 if ( need_refresh[i] && rect[i] != wxGridNoCellRect )
c6a51dcd 7626 m_gridWin->Refresh( FALSE, &(rect[i]) );
da6af900 7627 }
f85afd4e 7628
5c8fc7c1
SN
7629 // never generate an event as it will be generated from
7630 // wxGridSelection::SelectBlock!
f85afd4e
MB
7631}
7632
7633void wxGrid::SelectAll()
7634{
b5808881
SN
7635 m_selection->SelectBlock( 0, 0, m_numRows-1, m_numCols-1 );
7636}
f85afd4e 7637
b5808881
SN
7638bool wxGrid::IsSelection()
7639{
7640 return ( m_selection->IsSelection() ||
7641 ( m_selectingTopLeft != wxGridNoCellCoords &&
7642 m_selectingBottomRight != wxGridNoCellCoords ) );
f85afd4e
MB
7643}
7644
b5808881
SN
7645bool wxGrid::IsInSelection( int row, int col )
7646{
7647 return ( m_selection->IsInSelection( row, col ) ||
7648 ( row >= m_selectingTopLeft.GetRow() &&
7649 col >= m_selectingTopLeft.GetCol() &&
7650 row <= m_selectingBottomRight.GetRow() &&
7651 col <= m_selectingBottomRight.GetCol() ) );
7652}
f85afd4e
MB
7653
7654void wxGrid::ClearSelection()
7655{
b5808881
SN
7656 m_selectingTopLeft = wxGridNoCellCoords;
7657 m_selectingBottomRight = wxGridNoCellCoords;
7658 m_selection->ClearSelection();
8f177c8e 7659}
f85afd4e
MB
7660
7661
da6af900 7662// This function returns the rectangle that encloses the given block
2d66e025
MB
7663// in device coords clipped to the client size of the grid window.
7664//
58dd5b3b
MB
7665wxRect wxGrid::BlockToDeviceRect( const wxGridCellCoords &topLeft,
7666 const wxGridCellCoords &bottomRight )
f85afd4e 7667{
58dd5b3b 7668 wxRect rect( wxGridNoCellRect );
f85afd4e
MB
7669 wxRect cellRect;
7670
58dd5b3b
MB
7671 cellRect = CellToRect( topLeft );
7672 if ( cellRect != wxGridNoCellRect )
f85afd4e 7673 {
58dd5b3b
MB
7674 rect = cellRect;
7675 }
7676 else
7677 {
7678 rect = wxRect( 0, 0, 0, 0 );
7679 }
60ff3b99 7680
58dd5b3b
MB
7681 cellRect = CellToRect( bottomRight );
7682 if ( cellRect != wxGridNoCellRect )
7683 {
7684 rect += cellRect;
2d66e025
MB
7685 }
7686 else
7687 {
7688 return wxGridNoCellRect;
f85afd4e
MB
7689 }
7690
58dd5b3b
MB
7691 // convert to scrolled coords
7692 //
7693 int left, top, right, bottom;
7694 CalcScrolledPosition( rect.GetLeft(), rect.GetTop(), &left, &top );
7695 CalcScrolledPosition( rect.GetRight(), rect.GetBottom(), &right, &bottom );
7696
7697 int cw, ch;
7698 m_gridWin->GetClientSize( &cw, &ch );
7699
7700 rect.SetLeft( wxMax(0, left) );
7701 rect.SetTop( wxMax(0, top) );
7702 rect.SetRight( wxMin(cw, right) );
7703 rect.SetBottom( wxMin(ch, bottom) );
7704
f85afd4e
MB
7705 return rect;
7706}
7707
7708
58dd5b3b 7709
f85afd4e
MB
7710//
7711// ------ Grid event classes
7712//
7713
7714IMPLEMENT_DYNAMIC_CLASS( wxGridEvent, wxEvent )
7715
7716wxGridEvent::wxGridEvent( int id, wxEventType type, wxObject* obj,
5c8fc7c1 7717 int row, int col, int x, int y, bool sel,
f85afd4e
MB
7718 bool control, bool shift, bool alt, bool meta )
7719 : wxNotifyEvent( type, id )
7720{
7721 m_row = row;
7722 m_col = col;
7723 m_x = x;
7724 m_y = y;
5c8fc7c1 7725 m_selecting = sel;
f85afd4e
MB
7726 m_control = control;
7727 m_shift = shift;
7728 m_alt = alt;
7729 m_meta = meta;
8f177c8e 7730
f85afd4e
MB
7731 SetEventObject(obj);
7732}
7733
7734
7735IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent, wxEvent )
7736
7737wxGridSizeEvent::wxGridSizeEvent( int id, wxEventType type, wxObject* obj,
7738 int rowOrCol, int x, int y,
7739 bool control, bool shift, bool alt, bool meta )
7740 : wxNotifyEvent( type, id )
7741{
7742 m_rowOrCol = rowOrCol;
7743 m_x = x;
7744 m_y = y;
7745 m_control = control;
7746 m_shift = shift;
7747 m_alt = alt;
7748 m_meta = meta;
8f177c8e 7749
f85afd4e
MB
7750 SetEventObject(obj);
7751}
7752
7753
7754IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent, wxEvent )
7755
7756wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id, wxEventType type, wxObject* obj,
8f177c8e
VZ
7757 const wxGridCellCoords& topLeft,
7758 const wxGridCellCoords& bottomRight,
5c8fc7c1
SN
7759 bool sel, bool control,
7760 bool shift, bool alt, bool meta )
8f177c8e 7761 : wxNotifyEvent( type, id )
f85afd4e
MB
7762{
7763 m_topLeft = topLeft;
7764 m_bottomRight = bottomRight;
5c8fc7c1 7765 m_selecting = sel;
f85afd4e
MB
7766 m_control = control;
7767 m_shift = shift;
7768 m_alt = alt;
7769 m_meta = meta;
7770
7771 SetEventObject(obj);
7772}
7773
7774
7775#endif // ifndef wxUSE_NEW_GRID