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