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