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