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