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