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