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