Some mouse events need to be handled even when outside the grid.
[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 #if WXGRID_DRAW_LINES
2583 m_owner->DrawAllGridLines( dc, reg );
2584 #endif
2585 m_owner->DrawHighlight( dc );
2586 }
2587
2588
2589 void wxGridWindow::ScrollWindow( int dx, int dy, const wxRect *rect )
2590 {
2591 wxPanel::ScrollWindow( dx, dy, rect );
2592 m_rowLabelWin->ScrollWindow( 0, dy, rect );
2593 m_colLabelWin->ScrollWindow( dx, 0, rect );
2594 }
2595
2596
2597 void wxGridWindow::OnMouseEvent( wxMouseEvent& event )
2598 {
2599 m_owner->ProcessGridCellMouseEvent( event );
2600 }
2601
2602
2603 // This seems to be required for wxMotif otherwise the mouse
2604 // cursor must be in the cell edit control to get key events
2605 //
2606 void wxGridWindow::OnKeyDown( wxKeyEvent& event )
2607 {
2608 if ( !m_owner->ProcessEvent( event ) ) event.Skip();
2609 }
2610
2611 // We are trapping erase background events to reduce flicker under MSW
2612 // and GTK but this can leave junk in the space beyond the last row and
2613 // col. So here we paint these spaces if they are visible.
2614 //
2615 void wxGridWindow::OnEraseBackground(wxEraseEvent& event)
2616 {
2617 int cw, ch;
2618 GetClientSize( &cw, &ch );
2619
2620 int right, bottom;
2621 m_owner->CalcUnscrolledPosition( cw, ch, &right, &bottom );
2622
2623 wxRect rightRect;
2624 rightRect = m_owner->CellToRect( 0, m_owner->GetNumberCols()-1 );
2625
2626 wxRect bottomRect;
2627 bottomRect = m_owner->CellToRect( m_owner->GetNumberRows()-1, 0 );
2628
2629 if ( right > rightRect.GetRight() || bottom > bottomRect.GetBottom() )
2630 {
2631 int left, top;
2632 m_owner->CalcUnscrolledPosition( 0, 0, &left, &top );
2633
2634 wxClientDC dc( this );
2635 m_owner->PrepareDC( dc );
2636 dc.SetBrush( wxBrush(m_owner->GetDefaultCellBackgroundColour(), wxSOLID) );
2637 dc.SetPen( *wxTRANSPARENT_PEN );
2638
2639 if ( right > rightRect.GetRight() )
2640 dc.DrawRectangle( rightRect.GetRight()+1, top, right - rightRect.GetRight(), ch );
2641
2642 if ( bottom > bottomRect.GetBottom() )
2643 dc.DrawRectangle( left, bottomRect.GetBottom()+1, cw, bottom - bottomRect.GetBottom() );
2644 }
2645 }
2646
2647
2648 //////////////////////////////////////////////////////////////////////
2649
2650
2651 IMPLEMENT_DYNAMIC_CLASS( wxGrid, wxScrolledWindow )
2652
2653 BEGIN_EVENT_TABLE( wxGrid, wxScrolledWindow )
2654 EVT_PAINT( wxGrid::OnPaint )
2655 EVT_SIZE( wxGrid::OnSize )
2656 EVT_KEY_DOWN( wxGrid::OnKeyDown )
2657 EVT_ERASE_BACKGROUND( wxGrid::OnEraseBackground )
2658 END_EVENT_TABLE()
2659
2660 wxGrid::wxGrid( wxWindow *parent,
2661 wxWindowID id,
2662 const wxPoint& pos,
2663 const wxSize& size,
2664 long style,
2665 const wxString& name )
2666 : wxScrolledWindow( parent, id, pos, size, style, name ),
2667 m_colMinWidths(wxKEY_INTEGER, GRID_HASH_SIZE)
2668 {
2669 Create();
2670 }
2671
2672
2673 wxGrid::~wxGrid()
2674 {
2675 ClearAttrCache();
2676 m_defaultCellAttr->SafeDecRef();
2677
2678 #ifdef DEBUG_ATTR_CACHE
2679 size_t total = gs_nAttrCacheHits + gs_nAttrCacheMisses;
2680 wxPrintf(_T("wxGrid attribute cache statistics: "
2681 "total: %u, hits: %u (%u%%)\n"),
2682 total, gs_nAttrCacheHits,
2683 total ? (gs_nAttrCacheHits*100) / total : 0);
2684 #endif
2685
2686 if (m_ownTable)
2687 delete m_table;
2688
2689 delete m_typeRegistry;
2690 }
2691
2692
2693 //
2694 // ----- internal init and update functions
2695 //
2696
2697 void wxGrid::Create()
2698 {
2699 m_created = FALSE; // set to TRUE by CreateGrid
2700 m_displayed = TRUE; // FALSE; // set to TRUE by OnPaint
2701
2702 m_table = (wxGridTableBase *) NULL;
2703 m_ownTable = FALSE;
2704
2705 m_cellEditCtrlEnabled = FALSE;
2706
2707 m_defaultCellAttr = new wxGridCellAttr;
2708 m_defaultCellAttr->SetDefAttr(m_defaultCellAttr);
2709
2710 // Set default cell attributes
2711 m_defaultCellAttr->SetFont(GetFont());
2712 m_defaultCellAttr->SetAlignment(wxLEFT, wxTOP);
2713 m_defaultCellAttr->SetTextColour(
2714 wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOWTEXT));
2715 m_defaultCellAttr->SetBackgroundColour(
2716 wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW));
2717 m_defaultCellAttr->SetRenderer(new wxGridCellStringRenderer);
2718 m_defaultCellAttr->SetEditor(new wxGridCellTextEditor);
2719
2720
2721 m_numRows = 0;
2722 m_numCols = 0;
2723 m_currentCellCoords = wxGridNoCellCoords;
2724
2725 m_rowLabelWidth = WXGRID_DEFAULT_ROW_LABEL_WIDTH;
2726 m_colLabelHeight = WXGRID_DEFAULT_COL_LABEL_HEIGHT;
2727
2728 // data type registration: register all standard data types
2729 // TODO: may be allow the app to selectively disable some of them?
2730 m_typeRegistry = new wxGridTypeRegistry;
2731 RegisterDataType(wxGRID_VALUE_STRING,
2732 new wxGridCellStringRenderer,
2733 new wxGridCellTextEditor);
2734 RegisterDataType(wxGRID_VALUE_BOOL,
2735 new wxGridCellBoolRenderer,
2736 new wxGridCellBoolEditor);
2737 RegisterDataType(wxGRID_VALUE_NUMBER,
2738 new wxGridCellNumberRenderer,
2739 new wxGridCellNumberEditor);
2740
2741 // subwindow components that make up the wxGrid
2742 m_cornerLabelWin = new wxGridCornerLabelWindow( this,
2743 -1,
2744 wxDefaultPosition,
2745 wxDefaultSize );
2746
2747 m_rowLabelWin = new wxGridRowLabelWindow( this,
2748 -1,
2749 wxDefaultPosition,
2750 wxDefaultSize );
2751
2752 m_colLabelWin = new wxGridColLabelWindow( this,
2753 -1,
2754 wxDefaultPosition,
2755 wxDefaultSize );
2756
2757 m_gridWin = new wxGridWindow( this,
2758 m_rowLabelWin,
2759 m_colLabelWin,
2760 -1,
2761 wxDefaultPosition,
2762 wxDefaultSize );
2763
2764 SetTargetWindow( m_gridWin );
2765 }
2766
2767
2768 bool wxGrid::CreateGrid( int numRows, int numCols )
2769 {
2770 if ( m_created )
2771 {
2772 wxFAIL_MSG( wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
2773 return FALSE;
2774 }
2775 else
2776 {
2777 m_numRows = numRows;
2778 m_numCols = numCols;
2779
2780 m_table = new wxGridStringTable( m_numRows, m_numCols );
2781 m_table->SetView( this );
2782 m_ownTable = TRUE;
2783 Init();
2784 m_created = TRUE;
2785 }
2786
2787 return m_created;
2788 }
2789
2790 bool wxGrid::SetTable( wxGridTableBase *table, bool takeOwnership )
2791 {
2792 if ( m_created )
2793 {
2794 // RD: Actually, this should probably be allowed. I think it would be
2795 // nice to be able to switch multiple Tables in and out of a single
2796 // View at runtime. Is there anything in the implmentation that would
2797 // prevent this?
2798
2799 wxFAIL_MSG( wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
2800 return FALSE;
2801 }
2802 else
2803 {
2804 m_numRows = table->GetNumberRows();
2805 m_numCols = table->GetNumberCols();
2806
2807 m_table = table;
2808 m_table->SetView( this );
2809 if (takeOwnership)
2810 m_ownTable = TRUE;
2811 Init();
2812 m_created = TRUE;
2813 }
2814
2815 return m_created;
2816 }
2817
2818
2819 void wxGrid::Init()
2820 {
2821 if ( m_numRows <= 0 )
2822 m_numRows = WXGRID_DEFAULT_NUMBER_ROWS;
2823
2824 if ( m_numCols <= 0 )
2825 m_numCols = WXGRID_DEFAULT_NUMBER_COLS;
2826
2827 m_rowLabelWidth = WXGRID_DEFAULT_ROW_LABEL_WIDTH;
2828 m_colLabelHeight = WXGRID_DEFAULT_COL_LABEL_HEIGHT;
2829
2830 if ( m_rowLabelWin )
2831 {
2832 m_labelBackgroundColour = m_rowLabelWin->GetBackgroundColour();
2833 }
2834 else
2835 {
2836 m_labelBackgroundColour = wxColour( _T("WHITE") );
2837 }
2838
2839 m_labelTextColour = wxColour( _T("BLACK") );
2840
2841 // init attr cache
2842 m_attrCache.row = -1;
2843
2844 // TODO: something better than this ?
2845 //
2846 m_labelFont = this->GetFont();
2847 m_labelFont.SetWeight( m_labelFont.GetWeight() + 2 );
2848
2849 m_rowLabelHorizAlign = wxLEFT;
2850 m_rowLabelVertAlign = wxCENTRE;
2851
2852 m_colLabelHorizAlign = wxCENTRE;
2853 m_colLabelVertAlign = wxTOP;
2854
2855 m_defaultColWidth = WXGRID_DEFAULT_COL_WIDTH;
2856 m_defaultRowHeight = m_gridWin->GetCharHeight();
2857
2858 #if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl()
2859 m_defaultRowHeight += 8;
2860 #else
2861 m_defaultRowHeight += 4;
2862 #endif
2863
2864 m_gridLineColour = wxColour( 128, 128, 255 );
2865 m_gridLinesEnabled = TRUE;
2866
2867 m_cursorMode = WXGRID_CURSOR_SELECT_CELL;
2868 m_winCapture = (wxWindow *)NULL;
2869 m_canDragRowSize = TRUE;
2870 m_canDragColSize = TRUE;
2871 m_canDragGridSize = TRUE;
2872 m_dragLastPos = -1;
2873 m_dragRowOrCol = -1;
2874 m_isDragging = FALSE;
2875 m_startDragPos = wxDefaultPosition;
2876
2877 m_waitForSlowClick = FALSE;
2878
2879 m_rowResizeCursor = wxCursor( wxCURSOR_SIZENS );
2880 m_colResizeCursor = wxCursor( wxCURSOR_SIZEWE );
2881
2882 m_currentCellCoords = wxGridNoCellCoords;
2883
2884 m_selectedTopLeft = wxGridNoCellCoords;
2885 m_selectedBottomRight = wxGridNoCellCoords;
2886 m_selectionBackground = wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHT);
2887 m_selectionForeground = wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHTTEXT);
2888
2889 m_editable = TRUE; // default for whole grid
2890
2891 m_inOnKeyDown = FALSE;
2892 m_batchCount = 0;
2893 }
2894
2895 // ----------------------------------------------------------------------------
2896 // the idea is to call these functions only when necessary because they create
2897 // quite big arrays which eat memory mostly unnecessary - in particular, if
2898 // default widths/heights are used for all rows/columns, we may not use these
2899 // arrays at all
2900 //
2901 // with some extra code, it should be possible to only store the
2902 // widths/heights different from default ones but this will be done later...
2903 // ----------------------------------------------------------------------------
2904
2905 void wxGrid::InitRowHeights()
2906 {
2907 m_rowHeights.Empty();
2908 m_rowBottoms.Empty();
2909
2910 m_rowHeights.Alloc( m_numRows );
2911 m_rowBottoms.Alloc( m_numRows );
2912
2913 int rowBottom = 0;
2914 for ( int i = 0; i < m_numRows; i++ )
2915 {
2916 m_rowHeights.Add( m_defaultRowHeight );
2917 rowBottom += m_defaultRowHeight;
2918 m_rowBottoms.Add( rowBottom );
2919 }
2920 }
2921
2922 void wxGrid::InitColWidths()
2923 {
2924 m_colWidths.Empty();
2925 m_colRights.Empty();
2926
2927 m_colWidths.Alloc( m_numCols );
2928 m_colRights.Alloc( m_numCols );
2929 int colRight = 0;
2930 for ( int i = 0; i < m_numCols; i++ )
2931 {
2932 m_colWidths.Add( m_defaultColWidth );
2933 colRight += m_defaultColWidth;
2934 m_colRights.Add( colRight );
2935 }
2936 }
2937
2938 int wxGrid::GetColWidth(int col) const
2939 {
2940 return m_colWidths.IsEmpty() ? m_defaultColWidth : m_colWidths[col];
2941 }
2942
2943 int wxGrid::GetColLeft(int col) const
2944 {
2945 return m_colRights.IsEmpty() ? col * m_defaultColWidth
2946 : m_colRights[col] - m_colWidths[col];
2947 }
2948
2949 int wxGrid::GetColRight(int col) const
2950 {
2951 return m_colRights.IsEmpty() ? (col + 1) * m_defaultColWidth
2952 : m_colRights[col];
2953 }
2954
2955 int wxGrid::GetRowHeight(int row) const
2956 {
2957 return m_rowHeights.IsEmpty() ? m_defaultRowHeight : m_rowHeights[row];
2958 }
2959
2960 int wxGrid::GetRowTop(int row) const
2961 {
2962 return m_rowBottoms.IsEmpty() ? row * m_defaultRowHeight
2963 : m_rowBottoms[row] - m_rowHeights[row];
2964 }
2965
2966 int wxGrid::GetRowBottom(int row) const
2967 {
2968 return m_rowBottoms.IsEmpty() ? (row + 1) * m_defaultRowHeight
2969 : m_rowBottoms[row];
2970 }
2971
2972 void wxGrid::CalcDimensions()
2973 {
2974 int cw, ch;
2975 GetClientSize( &cw, &ch );
2976
2977 if ( m_numRows > 0 && m_numCols > 0 )
2978 {
2979 int right = GetColRight( m_numCols-1 ) + 50;
2980 int bottom = GetRowBottom( m_numRows-1 ) + 50;
2981
2982 // TODO: restore the scroll position that we had before sizing
2983 //
2984 int x, y;
2985 GetViewStart( &x, &y );
2986 SetScrollbars( GRID_SCROLL_LINE, GRID_SCROLL_LINE,
2987 right/GRID_SCROLL_LINE, bottom/GRID_SCROLL_LINE,
2988 x, y );
2989 }
2990 }
2991
2992
2993 void wxGrid::CalcWindowSizes()
2994 {
2995 int cw, ch;
2996 GetClientSize( &cw, &ch );
2997
2998 if ( m_cornerLabelWin->IsShown() )
2999 m_cornerLabelWin->SetSize( 0, 0, m_rowLabelWidth, m_colLabelHeight );
3000
3001 if ( m_colLabelWin->IsShown() )
3002 m_colLabelWin->SetSize( m_rowLabelWidth, 0, cw-m_rowLabelWidth, m_colLabelHeight);
3003
3004 if ( m_rowLabelWin->IsShown() )
3005 m_rowLabelWin->SetSize( 0, m_colLabelHeight, m_rowLabelWidth, ch-m_colLabelHeight);
3006
3007 if ( m_gridWin->IsShown() )
3008 m_gridWin->SetSize( m_rowLabelWidth, m_colLabelHeight, cw-m_rowLabelWidth, ch-m_colLabelHeight);
3009 }
3010
3011
3012 // this is called when the grid table sends a message to say that it
3013 // has been redimensioned
3014 //
3015 bool wxGrid::Redimension( wxGridTableMessage& msg )
3016 {
3017 int i;
3018
3019 // if we were using the default widths/heights so far, we must change them
3020 // now
3021 if ( m_colWidths.IsEmpty() )
3022 {
3023 InitColWidths();
3024 }
3025
3026 if ( m_rowHeights.IsEmpty() )
3027 {
3028 InitRowHeights();
3029 }
3030
3031 switch ( msg.GetId() )
3032 {
3033 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED:
3034 {
3035 size_t pos = msg.GetCommandInt();
3036 int numRows = msg.GetCommandInt2();
3037 for ( i = 0; i < numRows; i++ )
3038 {
3039 m_rowHeights.Insert( m_defaultRowHeight, pos );
3040 m_rowBottoms.Insert( 0, pos );
3041 }
3042 m_numRows += numRows;
3043
3044 int bottom = 0;
3045 if ( pos > 0 ) bottom = m_rowBottoms[pos-1];
3046
3047 for ( i = pos; i < m_numRows; i++ )
3048 {
3049 bottom += m_rowHeights[i];
3050 m_rowBottoms[i] = bottom;
3051 }
3052 CalcDimensions();
3053 }
3054 return TRUE;
3055
3056 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED:
3057 {
3058 int numRows = msg.GetCommandInt();
3059 for ( i = 0; i < numRows; i++ )
3060 {
3061 m_rowHeights.Add( m_defaultRowHeight );
3062 m_rowBottoms.Add( 0 );
3063 }
3064
3065 int oldNumRows = m_numRows;
3066 m_numRows += numRows;
3067
3068 int bottom = 0;
3069 if ( oldNumRows > 0 ) bottom = m_rowBottoms[oldNumRows-1];
3070
3071 for ( i = oldNumRows; i < m_numRows; i++ )
3072 {
3073 bottom += m_rowHeights[i];
3074 m_rowBottoms[i] = bottom;
3075 }
3076 CalcDimensions();
3077 }
3078 return TRUE;
3079
3080 case wxGRIDTABLE_NOTIFY_ROWS_DELETED:
3081 {
3082 size_t pos = msg.GetCommandInt();
3083 int numRows = msg.GetCommandInt2();
3084 for ( i = 0; i < numRows; i++ )
3085 {
3086 m_rowHeights.Remove( pos );
3087 m_rowBottoms.Remove( pos );
3088 }
3089 m_numRows -= numRows;
3090
3091 if ( !m_numRows )
3092 {
3093 m_numCols = 0;
3094 m_colWidths.Clear();
3095 m_colRights.Clear();
3096 m_currentCellCoords = wxGridNoCellCoords;
3097 }
3098 else
3099 {
3100 if ( m_currentCellCoords.GetRow() >= m_numRows )
3101 m_currentCellCoords.Set( 0, 0 );
3102
3103 int h = 0;
3104 for ( i = 0; i < m_numRows; i++ )
3105 {
3106 h += m_rowHeights[i];
3107 m_rowBottoms[i] = h;
3108 }
3109 }
3110
3111 CalcDimensions();
3112 }
3113 return TRUE;
3114
3115 case wxGRIDTABLE_NOTIFY_COLS_INSERTED:
3116 {
3117 size_t pos = msg.GetCommandInt();
3118 int numCols = msg.GetCommandInt2();
3119 for ( i = 0; i < numCols; i++ )
3120 {
3121 m_colWidths.Insert( m_defaultColWidth, pos );
3122 m_colRights.Insert( 0, pos );
3123 }
3124 m_numCols += numCols;
3125
3126 int right = 0;
3127 if ( pos > 0 ) right = m_colRights[pos-1];
3128
3129 for ( i = pos; i < m_numCols; i++ )
3130 {
3131 right += m_colWidths[i];
3132 m_colRights[i] = right;
3133 }
3134 CalcDimensions();
3135 }
3136 return TRUE;
3137
3138 case wxGRIDTABLE_NOTIFY_COLS_APPENDED:
3139 {
3140 int numCols = msg.GetCommandInt();
3141 for ( i = 0; i < numCols; i++ )
3142 {
3143 m_colWidths.Add( m_defaultColWidth );
3144 m_colRights.Add( 0 );
3145 }
3146
3147 int oldNumCols = m_numCols;
3148 m_numCols += numCols;
3149
3150 int right = 0;
3151 if ( oldNumCols > 0 ) right = m_colRights[oldNumCols-1];
3152
3153 for ( i = oldNumCols; i < m_numCols; i++ )
3154 {
3155 right += m_colWidths[i];
3156 m_colRights[i] = right;
3157 }
3158 CalcDimensions();
3159 }
3160 return TRUE;
3161
3162 case wxGRIDTABLE_NOTIFY_COLS_DELETED:
3163 {
3164 size_t pos = msg.GetCommandInt();
3165 int numCols = msg.GetCommandInt2();
3166 for ( i = 0; i < numCols; i++ )
3167 {
3168 m_colWidths.Remove( pos );
3169 m_colRights.Remove( pos );
3170 }
3171 m_numCols -= numCols;
3172
3173 if ( !m_numCols )
3174 {
3175 #if 0 // leave the row alone here so that AppendCols will work subsequently
3176 m_numRows = 0;
3177 m_rowHeights.Clear();
3178 m_rowBottoms.Clear();
3179 #endif
3180 m_currentCellCoords = wxGridNoCellCoords;
3181 }
3182 else
3183 {
3184 if ( m_currentCellCoords.GetCol() >= m_numCols )
3185 m_currentCellCoords.Set( 0, 0 );
3186
3187 int w = 0;
3188 for ( i = 0; i < m_numCols; i++ )
3189 {
3190 w += m_colWidths[i];
3191 m_colRights[i] = w;
3192 }
3193 }
3194 CalcDimensions();
3195 }
3196 return TRUE;
3197 }
3198
3199 return FALSE;
3200 }
3201
3202
3203 void wxGrid::CalcRowLabelsExposed( wxRegion& reg )
3204 {
3205 wxRegionIterator iter( reg );
3206 wxRect r;
3207
3208 m_rowLabelsExposed.Empty();
3209
3210 int top, bottom;
3211 while ( iter )
3212 {
3213 r = iter.GetRect();
3214
3215 // TODO: remove this when we can...
3216 // There is a bug in wxMotif that gives garbage update
3217 // rectangles if you jump-scroll a long way by clicking the
3218 // scrollbar with middle button. This is a work-around
3219 //
3220 #if defined(__WXMOTIF__)
3221 int cw, ch;
3222 m_gridWin->GetClientSize( &cw, &ch );
3223 if ( r.GetTop() > ch ) r.SetTop( 0 );
3224 r.SetBottom( wxMin( r.GetBottom(), ch ) );
3225 #endif
3226
3227 // logical bounds of update region
3228 //
3229 int dummy;
3230 CalcUnscrolledPosition( 0, r.GetTop(), &dummy, &top );
3231 CalcUnscrolledPosition( 0, r.GetBottom(), &dummy, &bottom );
3232
3233 // find the row labels within these bounds
3234 //
3235 int row;
3236 for ( row = 0; row < m_numRows; row++ )
3237 {
3238 if ( GetRowBottom(row) < top )
3239 continue;
3240
3241 if ( GetRowTop(row) > bottom )
3242 break;
3243
3244 m_rowLabelsExposed.Add( row );
3245 }
3246
3247 iter++ ;
3248 }
3249 }
3250
3251
3252 void wxGrid::CalcColLabelsExposed( wxRegion& reg )
3253 {
3254 wxRegionIterator iter( reg );
3255 wxRect r;
3256
3257 m_colLabelsExposed.Empty();
3258
3259 int left, right;
3260 while ( iter )
3261 {
3262 r = iter.GetRect();
3263
3264 // TODO: remove this when we can...
3265 // There is a bug in wxMotif that gives garbage update
3266 // rectangles if you jump-scroll a long way by clicking the
3267 // scrollbar with middle button. This is a work-around
3268 //
3269 #if defined(__WXMOTIF__)
3270 int cw, ch;
3271 m_gridWin->GetClientSize( &cw, &ch );
3272 if ( r.GetLeft() > cw ) r.SetLeft( 0 );
3273 r.SetRight( wxMin( r.GetRight(), cw ) );
3274 #endif
3275
3276 // logical bounds of update region
3277 //
3278 int dummy;
3279 CalcUnscrolledPosition( r.GetLeft(), 0, &left, &dummy );
3280 CalcUnscrolledPosition( r.GetRight(), 0, &right, &dummy );
3281
3282 // find the cells within these bounds
3283 //
3284 int col;
3285 for ( col = 0; col < m_numCols; col++ )
3286 {
3287 if ( GetColRight(col) < left )
3288 continue;
3289
3290 if ( GetColLeft(col) > right )
3291 break;
3292
3293 m_colLabelsExposed.Add( col );
3294 }
3295
3296 iter++ ;
3297 }
3298 }
3299
3300
3301 void wxGrid::CalcCellsExposed( wxRegion& reg )
3302 {
3303 wxRegionIterator iter( reg );
3304 wxRect r;
3305
3306 m_cellsExposed.Empty();
3307 m_rowsExposed.Empty();
3308 m_colsExposed.Empty();
3309
3310 int left, top, right, bottom;
3311 while ( iter )
3312 {
3313 r = iter.GetRect();
3314
3315 // TODO: remove this when we can...
3316 // There is a bug in wxMotif that gives garbage update
3317 // rectangles if you jump-scroll a long way by clicking the
3318 // scrollbar with middle button. This is a work-around
3319 //
3320 #if defined(__WXMOTIF__)
3321 int cw, ch;
3322 m_gridWin->GetClientSize( &cw, &ch );
3323 if ( r.GetTop() > ch ) r.SetTop( 0 );
3324 if ( r.GetLeft() > cw ) r.SetLeft( 0 );
3325 r.SetRight( wxMin( r.GetRight(), cw ) );
3326 r.SetBottom( wxMin( r.GetBottom(), ch ) );
3327 #endif
3328
3329 // logical bounds of update region
3330 //
3331 CalcUnscrolledPosition( r.GetLeft(), r.GetTop(), &left, &top );
3332 CalcUnscrolledPosition( r.GetRight(), r.GetBottom(), &right, &bottom );
3333
3334 // find the cells within these bounds
3335 //
3336 int row, col;
3337 for ( row = 0; row < m_numRows; row++ )
3338 {
3339 if ( GetRowBottom(row) <= top )
3340 continue;
3341
3342 if ( GetRowTop(row) > bottom )
3343 break;
3344
3345 m_rowsExposed.Add( row );
3346
3347 for ( col = 0; col < m_numCols; col++ )
3348 {
3349 if ( GetColRight(col) <= left )
3350 continue;
3351
3352 if ( GetColLeft(col) > right )
3353 break;
3354
3355 if ( m_colsExposed.Index( col ) == wxNOT_FOUND )
3356 m_colsExposed.Add( col );
3357 m_cellsExposed.Add( wxGridCellCoords( row, col ) );
3358 }
3359 }
3360
3361 iter++;
3362 }
3363 }
3364
3365
3366 void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent& event )
3367 {
3368 int x, y, row;
3369 wxPoint pos( event.GetPosition() );
3370 CalcUnscrolledPosition( pos.x, pos.y, &x, &y );
3371
3372 if ( event.Dragging() )
3373 {
3374 m_isDragging = TRUE;
3375
3376 if ( event.LeftIsDown() )
3377 {
3378 switch( m_cursorMode )
3379 {
3380 case WXGRID_CURSOR_RESIZE_ROW:
3381 {
3382 int cw, ch, left, dummy;
3383 m_gridWin->GetClientSize( &cw, &ch );
3384 CalcUnscrolledPosition( 0, 0, &left, &dummy );
3385
3386 wxClientDC dc( m_gridWin );
3387 PrepareDC( dc );
3388 y = wxMax( y, GetRowTop(m_dragRowOrCol) + WXGRID_MIN_ROW_HEIGHT );
3389 dc.SetLogicalFunction(wxINVERT);
3390 if ( m_dragLastPos >= 0 )
3391 {
3392 dc.DrawLine( left, m_dragLastPos, left+cw, m_dragLastPos );
3393 }
3394 dc.DrawLine( left, y, left+cw, y );
3395 m_dragLastPos = y;
3396 }
3397 break;
3398
3399 case WXGRID_CURSOR_SELECT_ROW:
3400 if ( (row = YToRow( y )) >= 0 &&
3401 !IsInSelection( row, 0 ) )
3402 {
3403 SelectRow( row, TRUE );
3404 }
3405
3406 // default label to suppress warnings about "enumeration value
3407 // 'xxx' not handled in switch
3408 default:
3409 break;
3410 }
3411 }
3412 return;
3413 }
3414
3415 m_isDragging = FALSE;
3416
3417
3418 // ------------ Entering or leaving the window
3419 //
3420 if ( event.Entering() || event.Leaving() )
3421 {
3422 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_rowLabelWin);
3423 }
3424
3425
3426 // ------------ Left button pressed
3427 //
3428 else if ( event.LeftDown() )
3429 {
3430 // don't send a label click event for a hit on the
3431 // edge of the row label - this is probably the user
3432 // wanting to resize the row
3433 //
3434 if ( YToEdgeOfRow(y) < 0 )
3435 {
3436 row = YToRow(y);
3437 if ( row >= 0 &&
3438 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK, row, -1, event ) )
3439 {
3440 SelectRow( row, event.ShiftDown() );
3441 ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW, m_rowLabelWin);
3442 }
3443 }
3444 else
3445 {
3446 // starting to drag-resize a row
3447 //
3448 if ( CanDragRowSize() )
3449 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW, m_rowLabelWin);
3450 }
3451 }
3452
3453
3454 // ------------ Left double click
3455 //
3456 else if (event.LeftDClick() )
3457 {
3458 if ( YToEdgeOfRow(y) < 0 )
3459 {
3460 row = YToRow(y);
3461 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK, row, -1, event );
3462 }
3463 }
3464
3465
3466 // ------------ Left button released
3467 //
3468 else if ( event.LeftUp() )
3469 {
3470 if ( m_cursorMode == WXGRID_CURSOR_RESIZE_ROW )
3471 {
3472 DoEndDragResizeRow();
3473
3474 // Note: we are ending the event *after* doing
3475 // default processing in this case
3476 //
3477 SendEvent( wxEVT_GRID_ROW_SIZE, m_dragRowOrCol, -1, event );
3478 }
3479
3480 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_rowLabelWin);
3481 m_dragLastPos = -1;
3482 }
3483
3484
3485 // ------------ Right button down
3486 //
3487 else if ( event.RightDown() )
3488 {
3489 row = YToRow(y);
3490 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK, row, -1, event ) )
3491 {
3492 // no default action at the moment
3493 }
3494 }
3495
3496
3497 // ------------ Right double click
3498 //
3499 else if ( event.RightDClick() )
3500 {
3501 row = YToRow(y);
3502 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK, row, -1, event ) )
3503 {
3504 // no default action at the moment
3505 }
3506 }
3507
3508
3509 // ------------ No buttons down and mouse moving
3510 //
3511 else if ( event.Moving() )
3512 {
3513 m_dragRowOrCol = YToEdgeOfRow( y );
3514 if ( m_dragRowOrCol >= 0 )
3515 {
3516 if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
3517 {
3518 // don't capture the mouse yet
3519 if ( CanDragRowSize() )
3520 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW, m_rowLabelWin, FALSE);
3521 }
3522 }
3523 else if ( m_cursorMode != WXGRID_CURSOR_SELECT_CELL )
3524 {
3525 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_rowLabelWin, FALSE);
3526 }
3527 }
3528 }
3529
3530
3531 void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent& event )
3532 {
3533 int x, y, col;
3534 wxPoint pos( event.GetPosition() );
3535 CalcUnscrolledPosition( pos.x, pos.y, &x, &y );
3536
3537 if ( event.Dragging() )
3538 {
3539 m_isDragging = TRUE;
3540
3541 if ( event.LeftIsDown() )
3542 {
3543 switch( m_cursorMode )
3544 {
3545 case WXGRID_CURSOR_RESIZE_COL:
3546 {
3547 int cw, ch, dummy, top;
3548 m_gridWin->GetClientSize( &cw, &ch );
3549 CalcUnscrolledPosition( 0, 0, &dummy, &top );
3550
3551 wxClientDC dc( m_gridWin );
3552 PrepareDC( dc );
3553
3554 x = wxMax( x, GetColLeft(m_dragRowOrCol) +
3555 GetColMinimalWidth(m_dragRowOrCol));
3556 dc.SetLogicalFunction(wxINVERT);
3557 if ( m_dragLastPos >= 0 )
3558 {
3559 dc.DrawLine( m_dragLastPos, top, m_dragLastPos, top+ch );
3560 }
3561 dc.DrawLine( x, top, x, top+ch );
3562 m_dragLastPos = x;
3563 }
3564 break;
3565
3566 case WXGRID_CURSOR_SELECT_COL:
3567 if ( (col = XToCol( x )) >= 0 &&
3568 !IsInSelection( 0, col ) )
3569 {
3570 SelectCol( col, TRUE );
3571 }
3572
3573 // default label to suppress warnings about "enumeration value
3574 // 'xxx' not handled in switch
3575 default:
3576 break;
3577 }
3578 }
3579 return;
3580 }
3581
3582 m_isDragging = FALSE;
3583
3584
3585 // ------------ Entering or leaving the window
3586 //
3587 if ( event.Entering() || event.Leaving() )
3588 {
3589 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_colLabelWin);
3590 }
3591
3592
3593 // ------------ Left button pressed
3594 //
3595 else if ( event.LeftDown() )
3596 {
3597 // don't send a label click event for a hit on the
3598 // edge of the col label - this is probably the user
3599 // wanting to resize the col
3600 //
3601 if ( XToEdgeOfCol(x) < 0 )
3602 {
3603 col = XToCol(x);
3604 if ( col >= 0 &&
3605 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK, -1, col, event ) )
3606 {
3607 SelectCol( col, event.ShiftDown() );
3608 ChangeCursorMode(WXGRID_CURSOR_SELECT_COL, m_colLabelWin);
3609 }
3610 }
3611 else
3612 {
3613 // starting to drag-resize a col
3614 //
3615 if ( CanDragColSize() )
3616 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL, m_colLabelWin);
3617 }
3618 }
3619
3620
3621 // ------------ Left double click
3622 //
3623 if ( event.LeftDClick() )
3624 {
3625 if ( XToEdgeOfCol(x) < 0 )
3626 {
3627 col = XToCol(x);
3628 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK, -1, col, event );
3629 }
3630 }
3631
3632
3633 // ------------ Left button released
3634 //
3635 else if ( event.LeftUp() )
3636 {
3637 if ( m_cursorMode == WXGRID_CURSOR_RESIZE_COL )
3638 {
3639 DoEndDragResizeCol();
3640
3641 // Note: we are ending the event *after* doing
3642 // default processing in this case
3643 //
3644 SendEvent( wxEVT_GRID_COL_SIZE, -1, m_dragRowOrCol, event );
3645 }
3646
3647 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_colLabelWin);
3648 m_dragLastPos = -1;
3649 }
3650
3651
3652 // ------------ Right button down
3653 //
3654 else if ( event.RightDown() )
3655 {
3656 col = XToCol(x);
3657 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK, -1, col, event ) )
3658 {
3659 // no default action at the moment
3660 }
3661 }
3662
3663
3664 // ------------ Right double click
3665 //
3666 else if ( event.RightDClick() )
3667 {
3668 col = XToCol(x);
3669 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK, -1, col, event ) )
3670 {
3671 // no default action at the moment
3672 }
3673 }
3674
3675
3676 // ------------ No buttons down and mouse moving
3677 //
3678 else if ( event.Moving() )
3679 {
3680 m_dragRowOrCol = XToEdgeOfCol( x );
3681 if ( m_dragRowOrCol >= 0 )
3682 {
3683 if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
3684 {
3685 // don't capture the cursor yet
3686 if ( CanDragColSize() )
3687 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL, m_colLabelWin, FALSE);
3688 }
3689 }
3690 else if ( m_cursorMode != WXGRID_CURSOR_SELECT_CELL )
3691 {
3692 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_colLabelWin, FALSE);
3693 }
3694 }
3695 }
3696
3697
3698 void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent& event )
3699 {
3700 if ( event.LeftDown() )
3701 {
3702 // indicate corner label by having both row and
3703 // col args == -1
3704 //
3705 if ( !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK, -1, -1, event ) )
3706 {
3707 SelectAll();
3708 }
3709 }
3710
3711 else if ( event.LeftDClick() )
3712 {
3713 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK, -1, -1, event );
3714 }
3715
3716 else if ( event.RightDown() )
3717 {
3718 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK, -1, -1, event ) )
3719 {
3720 // no default action at the moment
3721 }
3722 }
3723
3724 else if ( event.RightDClick() )
3725 {
3726 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK, -1, -1, event ) )
3727 {
3728 // no default action at the moment
3729 }
3730 }
3731 }
3732
3733 void wxGrid::ChangeCursorMode(CursorMode mode,
3734 wxWindow *win,
3735 bool captureMouse)
3736 {
3737 #ifdef __WXDEBUG__
3738 static const wxChar *cursorModes[] =
3739 {
3740 _T("SELECT_CELL"),
3741 _T("RESIZE_ROW"),
3742 _T("RESIZE_COL"),
3743 _T("SELECT_ROW"),
3744 _T("SELECT_COL")
3745 };
3746
3747 wxLogTrace(_T("grid"),
3748 _T("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
3749 win == m_colLabelWin ? _T("colLabelWin")
3750 : win ? _T("rowLabelWin")
3751 : _T("gridWin"),
3752 cursorModes[m_cursorMode], cursorModes[mode]);
3753 #endif // __WXDEBUG__
3754
3755 if ( mode == m_cursorMode )
3756 return;
3757
3758 if ( !win )
3759 {
3760 // by default use the grid itself
3761 win = m_gridWin;
3762 }
3763
3764 if ( m_winCapture )
3765 {
3766 m_winCapture->ReleaseMouse();
3767 m_winCapture = (wxWindow *)NULL;
3768 }
3769
3770 m_cursorMode = mode;
3771
3772 switch ( m_cursorMode )
3773 {
3774 case WXGRID_CURSOR_RESIZE_ROW:
3775 win->SetCursor( m_rowResizeCursor );
3776 break;
3777
3778 case WXGRID_CURSOR_RESIZE_COL:
3779 win->SetCursor( m_colResizeCursor );
3780 break;
3781
3782 default:
3783 win->SetCursor( *wxSTANDARD_CURSOR );
3784 }
3785
3786 // we need to capture mouse when resizing
3787 bool resize = m_cursorMode == WXGRID_CURSOR_RESIZE_ROW ||
3788 m_cursorMode == WXGRID_CURSOR_RESIZE_COL;
3789
3790 if ( captureMouse && resize )
3791 {
3792 win->CaptureMouse();
3793 m_winCapture = win;
3794 }
3795 }
3796
3797 void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent& event )
3798 {
3799 int x, y;
3800 wxPoint pos( event.GetPosition() );
3801 CalcUnscrolledPosition( pos.x, pos.y, &x, &y );
3802
3803 wxGridCellCoords coords;
3804 XYToCell( x, y, coords );
3805
3806 if ( event.Dragging() )
3807 {
3808 //wxLogDebug("pos(%d, %d) coords(%d, %d)", pos.x, pos.y, coords.GetRow(), coords.GetCol());
3809
3810 // Don't start doing anything until the mouse has been drug at
3811 // least 3 pixels in any direction...
3812 if (! m_isDragging)
3813 {
3814 if (m_startDragPos == wxDefaultPosition)
3815 {
3816 m_startDragPos = pos;
3817 return;
3818 }
3819 if (abs(m_startDragPos.x - pos.x) < 4 && abs(m_startDragPos.y - pos.y) < 4)
3820 return;
3821 }
3822
3823 m_isDragging = TRUE;
3824 if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
3825 {
3826 // Hide the edit control, so it
3827 // won't interfer with drag-shrinking.
3828 if ( IsCellEditControlEnabled() )
3829 HideCellEditControl();
3830
3831 // Have we captured the mouse yet?
3832 if (! m_winCapture)
3833 {
3834 m_winCapture = m_gridWin;
3835 m_winCapture->CaptureMouse();
3836 }
3837
3838 if ( coords != wxGridNoCellCoords )
3839 {
3840 if ( !IsSelection() )
3841 {
3842 SelectBlock( coords, coords );
3843 }
3844 else
3845 {
3846 SelectBlock( m_currentCellCoords, coords );
3847 }
3848
3849 if (! IsVisible(coords))
3850 {
3851 MakeCellVisible(coords);
3852 // TODO: need to introduce a delay or something here. The
3853 // scrolling is way to fast, at least on MSW.
3854 }
3855 }
3856 }
3857 else if ( m_cursorMode == WXGRID_CURSOR_RESIZE_ROW )
3858 {
3859 int cw, ch, left, dummy;
3860 m_gridWin->GetClientSize( &cw, &ch );
3861 CalcUnscrolledPosition( 0, 0, &left, &dummy );
3862
3863 wxClientDC dc( m_gridWin );
3864 PrepareDC( dc );
3865 y = wxMax( y, GetRowTop(m_dragRowOrCol) + WXGRID_MIN_ROW_HEIGHT );
3866 dc.SetLogicalFunction(wxINVERT);
3867 if ( m_dragLastPos >= 0 )
3868 {
3869 dc.DrawLine( left, m_dragLastPos, left+cw, m_dragLastPos );
3870 }
3871 dc.DrawLine( left, y, left+cw, y );
3872 m_dragLastPos = y;
3873 }
3874 else if ( m_cursorMode == WXGRID_CURSOR_RESIZE_COL )
3875 {
3876 int cw, ch, dummy, top;
3877 m_gridWin->GetClientSize( &cw, &ch );
3878 CalcUnscrolledPosition( 0, 0, &dummy, &top );
3879
3880 wxClientDC dc( m_gridWin );
3881 PrepareDC( dc );
3882 x = wxMax( x, GetColLeft(m_dragRowOrCol) +
3883 GetColMinimalWidth(m_dragRowOrCol) );
3884 dc.SetLogicalFunction(wxINVERT);
3885 if ( m_dragLastPos >= 0 )
3886 {
3887 dc.DrawLine( m_dragLastPos, top, m_dragLastPos, top+ch );
3888 }
3889 dc.DrawLine( x, top, x, top+ch );
3890 m_dragLastPos = x;
3891 }
3892
3893 return;
3894 }
3895
3896 m_isDragging = FALSE;
3897 m_startDragPos = wxDefaultPosition;
3898
3899 // if ( coords == wxGridNoCellCoords && m_cursorMode != WXGRID_CURSOR_SELECT_CELL )
3900 // {
3901 // ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL);
3902 // }
3903
3904 // if ( coords != wxGridNoCellCoords )
3905 // {
3906 // VZ: if we do this, the mode is reset to WXGRID_CURSOR_SELECT_CELL
3907 // immediately after it becomes WXGRID_CURSOR_RESIZE_ROW/COL under
3908 // wxGTK
3909 #if 0
3910 if ( event.Entering() || event.Leaving() )
3911 {
3912 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL);
3913 m_gridWin->SetCursor( *wxSTANDARD_CURSOR );
3914 }
3915 else
3916 #endif // 0
3917
3918 // ------------ Left button pressed
3919 //
3920 if ( event.LeftDown() && coords != wxGridNoCellCoords )
3921 {
3922 DisableCellEditControl();
3923 if ( event.ShiftDown() )
3924 {
3925 SelectBlock( m_currentCellCoords, coords );
3926 }
3927 else if ( XToEdgeOfCol(x) < 0 &&
3928 YToEdgeOfRow(y) < 0 )
3929 {
3930 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_CLICK,
3931 coords.GetRow(),
3932 coords.GetCol(),
3933 event ) )
3934 {
3935 MakeCellVisible( coords );
3936
3937 // if this is the second click on this cell then start
3938 // the edit control
3939 if ( m_waitForSlowClick &&
3940 (coords == m_currentCellCoords) &&
3941 CanEnableCellControl())
3942 {
3943 EnableCellEditControl();
3944
3945 wxGridCellAttr* attr = GetCellAttr(m_currentCellCoords);
3946 attr->GetEditor(this, coords.GetRow(), coords.GetCol())->StartingClick();
3947 attr->DecRef();
3948
3949 m_waitForSlowClick = FALSE;
3950 }
3951 else
3952 {
3953 SetCurrentCell( coords );
3954 m_waitForSlowClick = TRUE;
3955 }
3956 }
3957 }
3958 }
3959
3960
3961 // ------------ Left double click
3962 //
3963 else if ( event.LeftDClick() && coords != wxGridNoCellCoords )
3964 {
3965 DisableCellEditControl();
3966 if ( XToEdgeOfCol(x) < 0 && YToEdgeOfRow(y) < 0 )
3967 {
3968 SendEvent( wxEVT_GRID_CELL_LEFT_DCLICK,
3969 coords.GetRow(),
3970 coords.GetCol(),
3971 event );
3972 }
3973 }
3974
3975
3976 // ------------ Left button released
3977 //
3978 else if ( event.LeftUp() )
3979 {
3980 if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
3981 {
3982 if ( IsSelection() )
3983 {
3984 if (m_winCapture)
3985 {
3986 m_winCapture->ReleaseMouse();
3987 m_winCapture = NULL;
3988 }
3989 SendEvent( wxEVT_GRID_RANGE_SELECT, -1, -1, event );
3990 }
3991
3992 // Show the edit control, if it has been hidden for
3993 // drag-shrinking.
3994 ShowCellEditControl();
3995 }
3996 else if ( m_cursorMode == WXGRID_CURSOR_RESIZE_ROW )
3997 {
3998 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL);
3999 DoEndDragResizeRow();
4000
4001 // Note: we are ending the event *after* doing
4002 // default processing in this case
4003 //
4004 SendEvent( wxEVT_GRID_ROW_SIZE, m_dragRowOrCol, -1, event );
4005 }
4006 else if ( m_cursorMode == WXGRID_CURSOR_RESIZE_COL )
4007 {
4008 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL);
4009 DoEndDragResizeCol();
4010
4011 // Note: we are ending the event *after* doing
4012 // default processing in this case
4013 //
4014 SendEvent( wxEVT_GRID_COL_SIZE, -1, m_dragRowOrCol, event );
4015 }
4016
4017 m_dragLastPos = -1;
4018 }
4019
4020
4021 // ------------ Right button down
4022 //
4023 else if ( event.RightDown() && coords != wxGridNoCellCoords )
4024 {
4025 DisableCellEditControl();
4026 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_CLICK,
4027 coords.GetRow(),
4028 coords.GetCol(),
4029 event ) )
4030 {
4031 // no default action at the moment
4032 }
4033 }
4034
4035
4036 // ------------ Right double click
4037 //
4038 else if ( event.RightDClick() && coords != wxGridNoCellCoords )
4039 {
4040 DisableCellEditControl();
4041 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_DCLICK,
4042 coords.GetRow(),
4043 coords.GetCol(),
4044 event ) )
4045 {
4046 // no default action at the moment
4047 }
4048 }
4049
4050 // ------------ Moving and no button action
4051 //
4052 else if ( event.Moving() && !event.IsButton() )
4053 {
4054 int dragRow = YToEdgeOfRow( y );
4055 int dragCol = XToEdgeOfCol( x );
4056
4057 // Dragging on the corner of a cell to resize in both
4058 // directions is not implemented yet...
4059 //
4060 if ( dragRow >= 0 && dragCol >= 0 )
4061 {
4062 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL);
4063 return;
4064 }
4065
4066 if ( dragRow >= 0 )
4067 {
4068 m_dragRowOrCol = dragRow;
4069
4070 if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
4071 {
4072 if ( CanDragRowSize() && CanDragGridSize() )
4073 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW);
4074 }
4075
4076 return;
4077 }
4078
4079 if ( dragCol >= 0 )
4080 {
4081 m_dragRowOrCol = dragCol;
4082
4083 if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
4084 {
4085 if ( CanDragColSize() && CanDragGridSize() )
4086 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL);
4087 }
4088
4089 return;
4090 }
4091
4092 // Neither on a row or col edge
4093 //
4094 if ( m_cursorMode != WXGRID_CURSOR_SELECT_CELL )
4095 {
4096 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL);
4097 }
4098 }
4099 }
4100
4101
4102 void wxGrid::DoEndDragResizeRow()
4103 {
4104 if ( m_dragLastPos >= 0 )
4105 {
4106 // erase the last line and resize the row
4107 //
4108 int cw, ch, left, dummy;
4109 m_gridWin->GetClientSize( &cw, &ch );
4110 CalcUnscrolledPosition( 0, 0, &left, &dummy );
4111
4112 wxClientDC dc( m_gridWin );
4113 PrepareDC( dc );
4114 dc.SetLogicalFunction( wxINVERT );
4115 dc.DrawLine( left, m_dragLastPos, left+cw, m_dragLastPos );
4116 HideCellEditControl();
4117
4118 int rowTop = GetRowTop(m_dragRowOrCol);
4119 SetRowSize( m_dragRowOrCol,
4120 wxMax( m_dragLastPos - rowTop, WXGRID_MIN_ROW_HEIGHT ) );
4121
4122 if ( !GetBatchCount() )
4123 {
4124 // Only needed to get the correct rect.y:
4125 wxRect rect ( CellToRect( m_dragRowOrCol, 0 ) );
4126 rect.x = 0;
4127 CalcScrolledPosition(0, rect.y, &dummy, &rect.y);
4128 rect.width = m_rowLabelWidth;
4129 rect.height = ch - rect.y;
4130 m_rowLabelWin->Refresh( TRUE, &rect );
4131 rect.width = cw;
4132 m_gridWin->Refresh( FALSE, &rect );
4133 }
4134
4135 ShowCellEditControl();
4136 }
4137 }
4138
4139
4140 void wxGrid::DoEndDragResizeCol()
4141 {
4142 if ( m_dragLastPos >= 0 )
4143 {
4144 // erase the last line and resize the col
4145 //
4146 int cw, ch, dummy, top;
4147 m_gridWin->GetClientSize( &cw, &ch );
4148 CalcUnscrolledPosition( 0, 0, &dummy, &top );
4149
4150 wxClientDC dc( m_gridWin );
4151 PrepareDC( dc );
4152 dc.SetLogicalFunction( wxINVERT );
4153 dc.DrawLine( m_dragLastPos, top, m_dragLastPos, top+ch );
4154 HideCellEditControl();
4155
4156 int colLeft = GetColLeft(m_dragRowOrCol);
4157 SetColSize( m_dragRowOrCol,
4158 wxMax( m_dragLastPos - colLeft,
4159 GetColMinimalWidth(m_dragRowOrCol) ) );
4160
4161 if ( !GetBatchCount() )
4162 {
4163 // Only needed to get the correct rect.x:
4164 wxRect rect ( CellToRect( 0, m_dragRowOrCol ) );
4165 rect.y = 0;
4166 CalcScrolledPosition(rect.x, 0, &rect.x, &dummy);
4167 rect.width = cw - rect.x;
4168 rect.height = m_colLabelHeight;
4169 m_colLabelWin->Refresh( TRUE, &rect );
4170 rect.height = ch;
4171 m_gridWin->Refresh( FALSE, &rect );
4172 }
4173
4174 ShowCellEditControl();
4175 }
4176 }
4177
4178
4179
4180 //
4181 // ------ interaction with data model
4182 //
4183 bool wxGrid::ProcessTableMessage( wxGridTableMessage& msg )
4184 {
4185 switch ( msg.GetId() )
4186 {
4187 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES:
4188 return GetModelValues();
4189
4190 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES:
4191 return SetModelValues();
4192
4193 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED:
4194 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED:
4195 case wxGRIDTABLE_NOTIFY_ROWS_DELETED:
4196 case wxGRIDTABLE_NOTIFY_COLS_INSERTED:
4197 case wxGRIDTABLE_NOTIFY_COLS_APPENDED:
4198 case wxGRIDTABLE_NOTIFY_COLS_DELETED:
4199 return Redimension( msg );
4200
4201 default:
4202 return FALSE;
4203 }
4204 }
4205
4206
4207
4208 // The behaviour of this function depends on the grid table class
4209 // Clear() function. For the default wxGridStringTable class the
4210 // behavious is to replace all cell contents with wxEmptyString but
4211 // not to change the number of rows or cols.
4212 //
4213 void wxGrid::ClearGrid()
4214 {
4215 if ( m_table )
4216 {
4217 if (IsCellEditControlEnabled())
4218 DisableCellEditControl();
4219
4220 m_table->Clear();
4221 if ( !GetBatchCount() ) m_gridWin->Refresh();
4222 }
4223 }
4224
4225
4226 bool wxGrid::InsertRows( int pos, int numRows, bool WXUNUSED(updateLabels) )
4227 {
4228 // TODO: something with updateLabels flag
4229
4230 if ( !m_created )
4231 {
4232 wxFAIL_MSG( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") );
4233 return FALSE;
4234 }
4235
4236 if ( m_table )
4237 {
4238 if (IsCellEditControlEnabled())
4239 DisableCellEditControl();
4240
4241 bool ok = m_table->InsertRows( pos, numRows );
4242
4243 // the table will have sent the results of the insert row
4244 // operation to this view object as a grid table message
4245 //
4246 if ( ok )
4247 {
4248 if ( m_numCols == 0 )
4249 {
4250 m_table->AppendCols( WXGRID_DEFAULT_NUMBER_COLS );
4251 //
4252 // TODO: perhaps instead of appending the default number of cols
4253 // we should remember what the last non-zero number of cols was ?
4254 //
4255 }
4256
4257 if ( m_currentCellCoords == wxGridNoCellCoords )
4258 {
4259 // if we have just inserted cols into an empty grid the current
4260 // cell will be undefined...
4261 //
4262 SetCurrentCell( 0, 0 );
4263 }
4264
4265 ClearSelection();
4266 if ( !GetBatchCount() ) Refresh();
4267 }
4268
4269 return ok;
4270 }
4271 else
4272 {
4273 return FALSE;
4274 }
4275 }
4276
4277
4278 bool wxGrid::AppendRows( int numRows, bool WXUNUSED(updateLabels) )
4279 {
4280 // TODO: something with updateLabels flag
4281
4282 if ( !m_created )
4283 {
4284 wxFAIL_MSG( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") );
4285 return FALSE;
4286 }
4287
4288 if ( m_table && m_table->AppendRows( numRows ) )
4289 {
4290 if ( m_currentCellCoords == wxGridNoCellCoords )
4291 {
4292 // if we have just inserted cols into an empty grid the current
4293 // cell will be undefined...
4294 //
4295 SetCurrentCell( 0, 0 );
4296 }
4297
4298 // the table will have sent the results of the append row
4299 // operation to this view object as a grid table message
4300 //
4301 ClearSelection();
4302 if ( !GetBatchCount() ) Refresh();
4303 return TRUE;
4304 }
4305 else
4306 {
4307 return FALSE;
4308 }
4309 }
4310
4311
4312 bool wxGrid::DeleteRows( int pos, int numRows, bool WXUNUSED(updateLabels) )
4313 {
4314 // TODO: something with updateLabels flag
4315
4316 if ( !m_created )
4317 {
4318 wxFAIL_MSG( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") );
4319 return FALSE;
4320 }
4321
4322 if ( m_table )
4323 {
4324 if (IsCellEditControlEnabled())
4325 DisableCellEditControl();
4326
4327 if (m_table->DeleteRows( pos, numRows ))
4328 {
4329
4330 // the table will have sent the results of the delete row
4331 // operation to this view object as a grid table message
4332 //
4333 ClearSelection();
4334 if ( !GetBatchCount() ) Refresh();
4335 return TRUE;
4336 }
4337 }
4338 return FALSE;
4339 }
4340
4341
4342 bool wxGrid::InsertCols( int pos, int numCols, bool WXUNUSED(updateLabels) )
4343 {
4344 // TODO: something with updateLabels flag
4345
4346 if ( !m_created )
4347 {
4348 wxFAIL_MSG( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") );
4349 return FALSE;
4350 }
4351
4352 if ( m_table )
4353 {
4354 if (IsCellEditControlEnabled())
4355 DisableCellEditControl();
4356
4357 bool ok = m_table->InsertCols( pos, numCols );
4358
4359 // the table will have sent the results of the insert col
4360 // operation to this view object as a grid table message
4361 //
4362 if ( ok )
4363 {
4364 if ( m_currentCellCoords == wxGridNoCellCoords )
4365 {
4366 // if we have just inserted cols into an empty grid the current
4367 // cell will be undefined...
4368 //
4369 SetCurrentCell( 0, 0 );
4370 }
4371
4372 ClearSelection();
4373 if ( !GetBatchCount() ) Refresh();
4374 }
4375
4376 return ok;
4377 }
4378 else
4379 {
4380 return FALSE;
4381 }
4382 }
4383
4384
4385 bool wxGrid::AppendCols( int numCols, bool WXUNUSED(updateLabels) )
4386 {
4387 // TODO: something with updateLabels flag
4388
4389 if ( !m_created )
4390 {
4391 wxFAIL_MSG( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") );
4392 return FALSE;
4393 }
4394
4395 if ( m_table && m_table->AppendCols( numCols ) )
4396 {
4397 // the table will have sent the results of the append col
4398 // operation to this view object as a grid table message
4399 //
4400 if ( m_currentCellCoords == wxGridNoCellCoords )
4401 {
4402 // if we have just inserted cols into an empty grid the current
4403 // cell will be undefined...
4404 //
4405 SetCurrentCell( 0, 0 );
4406 }
4407
4408 ClearSelection();
4409 if ( !GetBatchCount() ) Refresh();
4410 return TRUE;
4411 }
4412 else
4413 {
4414 return FALSE;
4415 }
4416 }
4417
4418
4419 bool wxGrid::DeleteCols( int pos, int numCols, bool WXUNUSED(updateLabels) )
4420 {
4421 // TODO: something with updateLabels flag
4422
4423 if ( !m_created )
4424 {
4425 wxFAIL_MSG( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") );
4426 return FALSE;
4427 }
4428
4429 if ( m_table )
4430 {
4431 if (IsCellEditControlEnabled())
4432 DisableCellEditControl();
4433
4434 if ( m_table->DeleteCols( pos, numCols ) )
4435 {
4436 // the table will have sent the results of the delete col
4437 // operation to this view object as a grid table message
4438 //
4439 ClearSelection();
4440 if ( !GetBatchCount() ) Refresh();
4441 return TRUE;
4442 }
4443 }
4444 return FALSE;
4445 }
4446
4447
4448
4449 //
4450 // ----- event handlers
4451 //
4452
4453 // Generate a grid event based on a mouse event and
4454 // return the result of ProcessEvent()
4455 //
4456 bool wxGrid::SendEvent( const wxEventType type,
4457 int row, int col,
4458 wxMouseEvent& mouseEv )
4459 {
4460 if ( type == wxEVT_GRID_ROW_SIZE || type == wxEVT_GRID_COL_SIZE )
4461 {
4462 int rowOrCol = (row == -1 ? col : row);
4463
4464 wxGridSizeEvent gridEvt( GetId(),
4465 type,
4466 this,
4467 rowOrCol,
4468 mouseEv.GetX(), mouseEv.GetY(),
4469 mouseEv.ControlDown(),
4470 mouseEv.ShiftDown(),
4471 mouseEv.AltDown(),
4472 mouseEv.MetaDown() );
4473
4474 return GetEventHandler()->ProcessEvent(gridEvt);
4475 }
4476 else if ( type == wxEVT_GRID_RANGE_SELECT )
4477 {
4478 wxGridRangeSelectEvent gridEvt( GetId(),
4479 type,
4480 this,
4481 m_selectedTopLeft,
4482 m_selectedBottomRight,
4483 mouseEv.ControlDown(),
4484 mouseEv.ShiftDown(),
4485 mouseEv.AltDown(),
4486 mouseEv.MetaDown() );
4487
4488 return GetEventHandler()->ProcessEvent(gridEvt);
4489 }
4490 else
4491 {
4492 wxGridEvent gridEvt( GetId(),
4493 type,
4494 this,
4495 row, col,
4496 mouseEv.GetX(), mouseEv.GetY(),
4497 mouseEv.ControlDown(),
4498 mouseEv.ShiftDown(),
4499 mouseEv.AltDown(),
4500 mouseEv.MetaDown() );
4501
4502 return GetEventHandler()->ProcessEvent(gridEvt);
4503 }
4504 }
4505
4506
4507 // Generate a grid event of specified type and return the result
4508 // of ProcessEvent().
4509 //
4510 bool wxGrid::SendEvent( const wxEventType type,
4511 int row, int col )
4512 {
4513 if ( type == wxEVT_GRID_ROW_SIZE || type == wxEVT_GRID_COL_SIZE )
4514 {
4515 int rowOrCol = (row == -1 ? col : row);
4516
4517 wxGridSizeEvent gridEvt( GetId(),
4518 type,
4519 this,
4520 rowOrCol );
4521
4522 return GetEventHandler()->ProcessEvent(gridEvt);
4523 }
4524 else
4525 {
4526 wxGridEvent gridEvt( GetId(),
4527 type,
4528 this,
4529 row, col );
4530
4531 return GetEventHandler()->ProcessEvent(gridEvt);
4532 }
4533 }
4534
4535
4536 void wxGrid::OnPaint( wxPaintEvent& WXUNUSED(event) )
4537 {
4538 wxPaintDC dc( this );
4539
4540 if ( m_currentCellCoords == wxGridNoCellCoords &&
4541 m_numRows && m_numCols )
4542 {
4543 m_currentCellCoords.Set(0, 0);
4544 ShowCellEditControl();
4545 }
4546
4547 m_displayed = TRUE;
4548 }
4549
4550
4551 // This is just here to make sure that CalcDimensions gets called when
4552 // the grid view is resized... then the size event is skipped to allow
4553 // the box sizers to handle everything
4554 //
4555 void wxGrid::OnSize( wxSizeEvent& event )
4556 {
4557 CalcWindowSizes();
4558 CalcDimensions();
4559 }
4560
4561
4562 void wxGrid::OnKeyDown( wxKeyEvent& event )
4563 {
4564 if ( m_inOnKeyDown )
4565 {
4566 // shouldn't be here - we are going round in circles...
4567 //
4568 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") );
4569 }
4570
4571 m_inOnKeyDown = TRUE;
4572
4573 // propagate the event up and see if it gets processed
4574 //
4575 wxWindow *parent = GetParent();
4576 wxKeyEvent keyEvt( event );
4577 keyEvt.SetEventObject( parent );
4578
4579 if ( !parent->GetEventHandler()->ProcessEvent( keyEvt ) )
4580 {
4581
4582 // TODO: Should also support Shift-cursor keys for
4583 // extending the selection. Maybe add a flag to
4584 // MoveCursorXXX() and MoveCursorXXXBlock() and
4585 // just send event.ShiftDown().
4586
4587 // try local handlers
4588 //
4589 switch ( event.KeyCode() )
4590 {
4591 case WXK_UP:
4592 if ( event.ControlDown() )
4593 {
4594 MoveCursorUpBlock();
4595 }
4596 else
4597 {
4598 MoveCursorUp();
4599 }
4600 break;
4601
4602 case WXK_DOWN:
4603 if ( event.ControlDown() )
4604 {
4605 MoveCursorDownBlock();
4606 }
4607 else
4608 {
4609 MoveCursorDown();
4610 }
4611 break;
4612
4613 case WXK_LEFT:
4614 if ( event.ControlDown() )
4615 {
4616 MoveCursorLeftBlock();
4617 }
4618 else
4619 {
4620 MoveCursorLeft();
4621 }
4622 break;
4623
4624 case WXK_RIGHT:
4625 if ( event.ControlDown() )
4626 {
4627 MoveCursorRightBlock();
4628 }
4629 else
4630 {
4631 MoveCursorRight();
4632 }
4633 break;
4634
4635 case WXK_RETURN:
4636 if ( event.ControlDown() )
4637 {
4638 event.Skip(); // to let the edit control have the return
4639 }
4640 else
4641 {
4642 MoveCursorDown();
4643 }
4644 break;
4645
4646 case WXK_TAB:
4647 if (event.ShiftDown())
4648 MoveCursorLeft();
4649 else
4650 MoveCursorRight();
4651 break;
4652
4653 case WXK_HOME:
4654 if ( event.ControlDown() )
4655 {
4656 MakeCellVisible( 0, 0 );
4657 SetCurrentCell( 0, 0 );
4658 }
4659 else
4660 {
4661 event.Skip();
4662 }
4663 break;
4664
4665 case WXK_END:
4666 if ( event.ControlDown() )
4667 {
4668 MakeCellVisible( m_numRows-1, m_numCols-1 );
4669 SetCurrentCell( m_numRows-1, m_numCols-1 );
4670 }
4671 else
4672 {
4673 event.Skip();
4674 }
4675 break;
4676
4677 case WXK_PRIOR:
4678 MovePageUp();
4679 break;
4680
4681 case WXK_NEXT:
4682 MovePageDown();
4683 break;
4684
4685 // We don't want these keys to trigger the edit control, any others?
4686 case WXK_SHIFT:
4687 case WXK_ALT:
4688 case WXK_CONTROL:
4689 case WXK_CAPITAL:
4690 event.Skip();
4691 break;
4692
4693 case WXK_SPACE:
4694 if ( !IsEditable() )
4695 {
4696 MoveCursorRight();
4697 break;
4698 }
4699 // Otherwise fall through to default
4700
4701 default:
4702 // now try the cell edit control
4703 //
4704 if ( !IsCellEditControlEnabled() && CanEnableCellControl() )
4705 {
4706 EnableCellEditControl();
4707 int row = m_currentCellCoords.GetRow();
4708 int col = m_currentCellCoords.GetCol();
4709 wxGridCellAttr* attr = GetCellAttr(row, col);
4710 attr->GetEditor(this, row, col)->StartingKey(event);
4711 attr->DecRef();
4712 }
4713 else
4714 {
4715 // let others process char events for readonly cells
4716 event.Skip();
4717 }
4718 break;
4719 }
4720 }
4721
4722 m_inOnKeyDown = FALSE;
4723 }
4724
4725
4726 void wxGrid::OnEraseBackground(wxEraseEvent&)
4727 {
4728 }
4729
4730 void wxGrid::SetCurrentCell( const wxGridCellCoords& coords )
4731 {
4732 if ( SendEvent( wxEVT_GRID_SELECT_CELL, coords.GetRow(), coords.GetCol() ) )
4733 {
4734 // the event has been intercepted - do nothing
4735 return;
4736 }
4737
4738 if ( m_displayed &&
4739 m_currentCellCoords != wxGridNoCellCoords )
4740 {
4741 HideCellEditControl();
4742 DisableCellEditControl();
4743
4744 // Clear the old current cell highlight
4745 wxRect r = BlockToDeviceRect(m_currentCellCoords, m_currentCellCoords);
4746
4747 // Otherwise refresh redraws the highlight!
4748 m_currentCellCoords = coords;
4749
4750 m_gridWin->Refresh( FALSE, &r );
4751 }
4752
4753 m_currentCellCoords = coords;
4754
4755 if ( m_displayed )
4756 {
4757 wxClientDC dc(m_gridWin);
4758 PrepareDC(dc);
4759
4760 wxGridCellAttr* attr = GetCellAttr(coords);
4761 DrawCellHighlight(dc, attr);
4762 attr->DecRef();
4763
4764 if ( IsSelection() )
4765 {
4766 wxRect r( SelectionToDeviceRect() );
4767 ClearSelection();
4768 if ( !GetBatchCount() ) m_gridWin->Refresh( FALSE, &r );
4769 }
4770 }
4771 }
4772
4773
4774 //
4775 // ------ functions to get/send data (see also public functions)
4776 //
4777
4778 bool wxGrid::GetModelValues()
4779 {
4780 if ( m_table )
4781 {
4782 // all we need to do is repaint the grid
4783 //
4784 m_gridWin->Refresh();
4785 return TRUE;
4786 }
4787
4788 return FALSE;
4789 }
4790
4791
4792 bool wxGrid::SetModelValues()
4793 {
4794 int row, col;
4795
4796 if ( m_table )
4797 {
4798 for ( row = 0; row < m_numRows; row++ )
4799 {
4800 for ( col = 0; col < m_numCols; col++ )
4801 {
4802 m_table->SetValue( row, col, GetCellValue(row, col) );
4803 }
4804 }
4805
4806 return TRUE;
4807 }
4808
4809 return FALSE;
4810 }
4811
4812
4813
4814 // Note - this function only draws cells that are in the list of
4815 // exposed cells (usually set from the update region by
4816 // CalcExposedCells)
4817 //
4818 void wxGrid::DrawGridCellArea( wxDC& dc )
4819 {
4820 if ( !m_numRows || !m_numCols ) return;
4821
4822 size_t i;
4823 size_t numCells = m_cellsExposed.GetCount();
4824
4825 for ( i = 0; i < numCells; i++ )
4826 {
4827 DrawCell( dc, m_cellsExposed[i] );
4828 }
4829 }
4830
4831
4832 void wxGrid::DrawCell( wxDC& dc, const wxGridCellCoords& coords )
4833 {
4834 int row = coords.GetRow();
4835 int col = coords.GetCol();
4836
4837 if ( GetColWidth(col) <= 0 || GetRowHeight(row) <= 0 )
4838 return;
4839
4840 // we draw the cell border ourselves
4841 #if !WXGRID_DRAW_LINES
4842 if ( m_gridLinesEnabled )
4843 DrawCellBorder( dc, coords );
4844 #endif
4845
4846 wxGridCellAttr* attr = GetCellAttr(row, col);
4847
4848 bool isCurrent = coords == m_currentCellCoords;
4849
4850 wxRect rect;
4851 rect.x = GetColLeft(col);
4852 rect.y = GetRowTop(row);
4853 rect.width = GetColWidth(col) - 1;
4854 rect.height = GetRowHeight(row) - 1;
4855
4856 // if the editor is shown, we should use it and not the renderer
4857 if ( isCurrent && IsCellEditControlEnabled() )
4858 {
4859 attr->GetEditor(this, row, col)->PaintBackground(rect, attr);
4860 }
4861 else
4862 {
4863 // but all the rest is drawn by the cell renderer and hence may be
4864 // customized
4865 attr->GetRenderer(this, row, col)->
4866 Draw(*this, *attr, dc, rect, row, col, IsInSelection(coords));
4867
4868 }
4869
4870 attr->DecRef();
4871 }
4872
4873 void wxGrid::DrawCellHighlight( wxDC& dc, const wxGridCellAttr *attr )
4874 {
4875 int row = m_currentCellCoords.GetRow();
4876 int col = m_currentCellCoords.GetCol();
4877
4878 if ( GetColWidth(col) <= 0 || GetRowHeight(row) <= 0 )
4879 return;
4880
4881 wxRect rect;
4882 rect.x = GetColLeft(col);
4883 rect.y = GetRowTop(row);
4884 rect.width = GetColWidth(col) - 1;
4885 rect.height = GetRowHeight(row) - 1;
4886
4887 // hmmm... what could we do here to show that the cell is disabled?
4888 // for now, I just draw a thinner border than for the other ones, but
4889 // it doesn't look really good
4890 dc.SetPen(wxPen(m_gridLineColour, attr->IsReadOnly() ? 1 : 3, wxSOLID));
4891 dc.SetBrush(*wxTRANSPARENT_BRUSH);
4892
4893 dc.DrawRectangle(rect);
4894
4895 #if 0
4896 // VZ: my experiments with 3d borders...
4897
4898 // how to properly set colours for arbitrary bg?
4899 wxCoord x1 = rect.x,
4900 y1 = rect.y,
4901 x2 = rect.x + rect.width -1,
4902 y2 = rect.y + rect.height -1;
4903
4904 dc.SetPen(*wxWHITE_PEN);
4905 dc.DrawLine(x1, y1, x2, y1);
4906 dc.DrawLine(x1, y1, x1, y2);
4907
4908 dc.DrawLine(x1 + 1, y2 - 1, x2 - 1, y2 - 1);
4909 dc.DrawLine(x2 - 1, y1 + 1, x2 - 1, y2 );
4910
4911 dc.SetPen(*wxBLACK_PEN);
4912 dc.DrawLine(x1, y2, x2, y2);
4913 dc.DrawLine(x2, y1, x2, y2+1);
4914 #endif // 0
4915 }
4916
4917
4918 void wxGrid::DrawCellBorder( wxDC& dc, const wxGridCellCoords& coords )
4919 {
4920 int row = coords.GetRow();
4921 int col = coords.GetCol();
4922 if ( GetColWidth(col) <= 0 || GetRowHeight(row) <= 0 )
4923 return;
4924
4925 dc.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID) );
4926
4927 // right hand border
4928 //
4929 dc.DrawLine( GetColRight(col), GetRowTop(row),
4930 GetColRight(col), GetRowBottom(row) );
4931
4932 // bottom border
4933 //
4934 dc.DrawLine( GetColLeft(col), GetRowBottom(row),
4935 GetColRight(col), GetRowBottom(row) );
4936 }
4937
4938 void wxGrid::DrawHighlight(wxDC& dc)
4939 {
4940 if ( IsCellEditControlEnabled() )
4941 {
4942 // don't show highlight when the edit control is shown
4943 return;
4944 }
4945
4946 // if the active cell was repainted, repaint its highlight too because it
4947 // might have been damaged by the grid lines
4948 size_t count = m_cellsExposed.GetCount();
4949 for ( size_t n = 0; n < count; n++ )
4950 {
4951 if ( m_cellsExposed[n] == m_currentCellCoords )
4952 {
4953 wxGridCellAttr* attr = GetCellAttr(m_currentCellCoords);
4954 DrawCellHighlight(dc, attr);
4955 attr->DecRef();
4956
4957 break;
4958 }
4959 }
4960 }
4961
4962 // TODO: remove this ???
4963 // This is used to redraw all grid lines e.g. when the grid line colour
4964 // has been changed
4965 //
4966 void wxGrid::DrawAllGridLines( wxDC& dc, const wxRegion & reg )
4967 {
4968 if ( !m_gridLinesEnabled ||
4969 !m_numRows ||
4970 !m_numCols ) return;
4971
4972 int top, bottom, left, right;
4973
4974 #ifndef __WXGTK__
4975 if (reg.IsEmpty())
4976 {
4977 int cw, ch;
4978 m_gridWin->GetClientSize(&cw, &ch);
4979
4980 // virtual coords of visible area
4981 //
4982 CalcUnscrolledPosition( 0, 0, &left, &top );
4983 CalcUnscrolledPosition( cw, ch, &right, &bottom );
4984 }
4985 else
4986 {
4987 wxCoord x, y, w, h;
4988 reg.GetBox(x, y, w, h);
4989 CalcUnscrolledPosition( x, y, &left, &top );
4990 CalcUnscrolledPosition( x + w, y + h, &right, &bottom );
4991 }
4992 #else
4993 int cw, ch;
4994 m_gridWin->GetClientSize(&cw, &ch);
4995 CalcUnscrolledPosition( 0, 0, &left, &top );
4996 CalcUnscrolledPosition( cw, ch, &right, &bottom );
4997 #endif
4998
4999 // avoid drawing grid lines past the last row and col
5000 //
5001 right = wxMin( right, GetColRight(m_numCols - 1) );
5002 bottom = wxMin( bottom, GetRowBottom(m_numRows - 1) );
5003
5004 dc.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID) );
5005
5006 // horizontal grid lines
5007 //
5008 int i;
5009 for ( i = 0; i < m_numRows; i++ )
5010 {
5011 int bot = GetRowBottom(i) - 1;
5012
5013 if ( bot > bottom )
5014 {
5015 break;
5016 }
5017
5018 if ( bot >= top )
5019 {
5020 dc.DrawLine( left, bot, right, bot );
5021 }
5022 }
5023
5024
5025 // vertical grid lines
5026 //
5027 for ( i = 0; i < m_numCols; i++ )
5028 {
5029 int colRight = GetColRight(i) - 1;
5030 if ( colRight > right )
5031 {
5032 break;
5033 }
5034
5035 if ( colRight >= left )
5036 {
5037 dc.DrawLine( colRight, top, colRight, bottom );
5038 }
5039 }
5040 }
5041
5042
5043 void wxGrid::DrawRowLabels( wxDC& dc )
5044 {
5045 if ( !m_numRows || !m_numCols ) return;
5046
5047 size_t i;
5048 size_t numLabels = m_rowLabelsExposed.GetCount();
5049
5050 for ( i = 0; i < numLabels; i++ )
5051 {
5052 DrawRowLabel( dc, m_rowLabelsExposed[i] );
5053 }
5054 }
5055
5056
5057 void wxGrid::DrawRowLabel( wxDC& dc, int row )
5058 {
5059 if ( GetRowHeight(row) <= 0 )
5060 return;
5061
5062 int rowTop = GetRowTop(row),
5063 rowBottom = GetRowBottom(row) - 1;
5064
5065 dc.SetPen( *wxBLACK_PEN );
5066 dc.DrawLine( m_rowLabelWidth-1, rowTop,
5067 m_rowLabelWidth-1, rowBottom );
5068
5069 dc.DrawLine( 0, rowBottom, m_rowLabelWidth-1, rowBottom );
5070
5071 dc.SetPen( *wxWHITE_PEN );
5072 dc.DrawLine( 0, rowTop, 0, rowBottom );
5073 dc.DrawLine( 0, rowTop, m_rowLabelWidth-1, rowTop );
5074
5075 dc.SetBackgroundMode( wxTRANSPARENT );
5076 dc.SetTextForeground( GetLabelTextColour() );
5077 dc.SetFont( GetLabelFont() );
5078
5079 int hAlign, vAlign;
5080 GetRowLabelAlignment( &hAlign, &vAlign );
5081
5082 wxRect rect;
5083 rect.SetX( 2 );
5084 rect.SetY( GetRowTop(row) + 2 );
5085 rect.SetWidth( m_rowLabelWidth - 4 );
5086 rect.SetHeight( GetRowHeight(row) - 4 );
5087 DrawTextRectangle( dc, GetRowLabelValue( row ), rect, hAlign, vAlign );
5088 }
5089
5090
5091 void wxGrid::DrawColLabels( wxDC& dc )
5092 {
5093 if ( !m_numRows || !m_numCols ) return;
5094
5095 size_t i;
5096 size_t numLabels = m_colLabelsExposed.GetCount();
5097
5098 for ( i = 0; i < numLabels; i++ )
5099 {
5100 DrawColLabel( dc, m_colLabelsExposed[i] );
5101 }
5102 }
5103
5104
5105 void wxGrid::DrawColLabel( wxDC& dc, int col )
5106 {
5107 if ( GetColWidth(col) <= 0 )
5108 return;
5109
5110 int colLeft = GetColLeft(col),
5111 colRight = GetColRight(col) - 1;
5112
5113 dc.SetPen( *wxBLACK_PEN );
5114 dc.DrawLine( colRight, 0,
5115 colRight, m_colLabelHeight-1 );
5116
5117 dc.DrawLine( colLeft, m_colLabelHeight-1,
5118 colRight, m_colLabelHeight-1 );
5119
5120 dc.SetPen( *wxWHITE_PEN );
5121 dc.DrawLine( colLeft, 0, colLeft, m_colLabelHeight-1 );
5122 dc.DrawLine( colLeft, 0, colRight, 0 );
5123
5124 dc.SetBackgroundMode( wxTRANSPARENT );
5125 dc.SetTextForeground( GetLabelTextColour() );
5126 dc.SetFont( GetLabelFont() );
5127
5128 dc.SetBackgroundMode( wxTRANSPARENT );
5129 dc.SetTextForeground( GetLabelTextColour() );
5130 dc.SetFont( GetLabelFont() );
5131
5132 int hAlign, vAlign;
5133 GetColLabelAlignment( &hAlign, &vAlign );
5134
5135 wxRect rect;
5136 rect.SetX( colLeft + 2 );
5137 rect.SetY( 2 );
5138 rect.SetWidth( GetColWidth(col) - 4 );
5139 rect.SetHeight( m_colLabelHeight - 4 );
5140 DrawTextRectangle( dc, GetColLabelValue( col ), rect, hAlign, vAlign );
5141 }
5142
5143
5144 void wxGrid::DrawTextRectangle( wxDC& dc,
5145 const wxString& value,
5146 const wxRect& rect,
5147 int horizAlign,
5148 int vertAlign )
5149 {
5150 long textWidth, textHeight;
5151 long lineWidth, lineHeight;
5152 wxArrayString lines;
5153
5154 dc.SetClippingRegion( rect );
5155 StringToLines( value, lines );
5156 if ( lines.GetCount() )
5157 {
5158 GetTextBoxSize( dc, lines, &textWidth, &textHeight );
5159 dc.GetTextExtent( lines[0], &lineWidth, &lineHeight );
5160
5161 float x, y;
5162 switch ( horizAlign )
5163 {
5164 case wxRIGHT:
5165 x = rect.x + (rect.width - textWidth - 1);
5166 break;
5167
5168 case wxCENTRE:
5169 x = rect.x + ((rect.width - textWidth)/2);
5170 break;
5171
5172 case wxLEFT:
5173 default:
5174 x = rect.x + 1;
5175 break;
5176 }
5177
5178 switch ( vertAlign )
5179 {
5180 case wxBOTTOM:
5181 y = rect.y + (rect.height - textHeight - 1);
5182 break;
5183
5184 case wxCENTRE:
5185 y = rect.y + ((rect.height - textHeight)/2);
5186 break;
5187
5188 case wxTOP:
5189 default:
5190 y = rect.y + 1;
5191 break;
5192 }
5193
5194 for ( size_t i = 0; i < lines.GetCount(); i++ )
5195 {
5196 dc.DrawText( lines[i], (long)x, (long)y );
5197 y += lineHeight;
5198 }
5199 }
5200
5201 dc.DestroyClippingRegion();
5202 }
5203
5204
5205 // Split multi line text up into an array of strings. Any existing
5206 // contents of the string array are preserved.
5207 //
5208 void wxGrid::StringToLines( const wxString& value, wxArrayString& lines )
5209 {
5210 int startPos = 0;
5211 int pos;
5212 wxString eol = wxTextFile::GetEOL( wxTextFileType_Unix );
5213 wxString tVal = wxTextFile::Translate( value, wxTextFileType_Unix );
5214
5215 while ( startPos < (int)tVal.Length() )
5216 {
5217 pos = tVal.Mid(startPos).Find( eol );
5218 if ( pos < 0 )
5219 {
5220 break;
5221 }
5222 else if ( pos == 0 )
5223 {
5224 lines.Add( wxEmptyString );
5225 }
5226 else
5227 {
5228 lines.Add( value.Mid(startPos, pos) );
5229 }
5230 startPos += pos+1;
5231 }
5232 if ( startPos < (int)value.Length() )
5233 {
5234 lines.Add( value.Mid( startPos ) );
5235 }
5236 }
5237
5238
5239 void wxGrid::GetTextBoxSize( wxDC& dc,
5240 wxArrayString& lines,
5241 long *width, long *height )
5242 {
5243 long w = 0;
5244 long h = 0;
5245 long lineW, lineH;
5246
5247 size_t i;
5248 for ( i = 0; i < lines.GetCount(); i++ )
5249 {
5250 dc.GetTextExtent( lines[i], &lineW, &lineH );
5251 w = wxMax( w, lineW );
5252 h += lineH;
5253 }
5254
5255 *width = w;
5256 *height = h;
5257 }
5258
5259
5260 //
5261 // ------ Edit control functions
5262 //
5263
5264
5265 void wxGrid::EnableEditing( bool edit )
5266 {
5267 // TODO: improve this ?
5268 //
5269 if ( edit != m_editable )
5270 {
5271 m_editable = edit;
5272
5273 // FIXME IMHO this won't disable the edit control if edit == FALSE
5274 // because of the check in the beginning of
5275 // EnableCellEditControl() just below (VZ)
5276 EnableCellEditControl(m_editable);
5277 }
5278 }
5279
5280
5281 void wxGrid::EnableCellEditControl( bool enable )
5282 {
5283 if (! m_editable)
5284 return;
5285
5286 if ( m_currentCellCoords == wxGridNoCellCoords )
5287 SetCurrentCell( 0, 0 );
5288
5289 if ( enable != m_cellEditCtrlEnabled )
5290 {
5291 // TODO allow the app to Veto() this event?
5292 SendEvent(enable ? wxEVT_GRID_EDITOR_SHOWN : wxEVT_GRID_EDITOR_HIDDEN);
5293
5294 if ( enable )
5295 {
5296 // this should be checked by the caller!
5297 wxASSERT_MSG( CanEnableCellControl(),
5298 _T("can't enable editing for this cell!") );
5299
5300 // do it before ShowCellEditControl()
5301 m_cellEditCtrlEnabled = enable;
5302
5303 ShowCellEditControl();
5304 }
5305 else
5306 {
5307 HideCellEditControl();
5308 SaveEditControlValue();
5309
5310 // do it after HideCellEditControl()
5311 m_cellEditCtrlEnabled = enable;
5312 }
5313 }
5314 }
5315
5316 bool wxGrid::IsCurrentCellReadOnly() const
5317 {
5318 // const_cast
5319 wxGridCellAttr* attr = ((wxGrid *)this)->GetCellAttr(m_currentCellCoords);
5320 bool readonly = attr->IsReadOnly();
5321 attr->DecRef();
5322
5323 return readonly;
5324 }
5325
5326 bool wxGrid::CanEnableCellControl() const
5327 {
5328 return m_editable && !IsCurrentCellReadOnly();
5329 }
5330
5331 bool wxGrid::IsCellEditControlEnabled() const
5332 {
5333 // the cell edit control might be disable for all cells or just for the
5334 // current one if it's read only
5335 return m_cellEditCtrlEnabled ? !IsCurrentCellReadOnly() : FALSE;
5336 }
5337
5338 void wxGrid::ShowCellEditControl()
5339 {
5340 if ( IsCellEditControlEnabled() )
5341 {
5342 if ( !IsVisible( m_currentCellCoords ) )
5343 {
5344 return;
5345 }
5346 else
5347 {
5348 wxRect rect = CellToRect( m_currentCellCoords );
5349 int row = m_currentCellCoords.GetRow();
5350 int col = m_currentCellCoords.GetCol();
5351
5352 // convert to scrolled coords
5353 //
5354 CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
5355
5356 // done in PaintBackground()
5357 #if 0
5358 // erase the highlight and the cell contents because the editor
5359 // might not cover the entire cell
5360 wxClientDC dc( m_gridWin );
5361 PrepareDC( dc );
5362 dc.SetBrush(*wxLIGHT_GREY_BRUSH); //wxBrush(attr->GetBackgroundColour(), wxSOLID));
5363 dc.SetPen(*wxTRANSPARENT_PEN);
5364 dc.DrawRectangle(rect);
5365 #endif // 0
5366
5367 // cell is shifted by one pixel
5368 rect.x--;
5369 rect.y--;
5370
5371 wxGridCellAttr* attr = GetCellAttr(row, col);
5372 wxGridCellEditor* editor = attr->GetEditor(this, row, col);
5373 if ( !editor->IsCreated() )
5374 {
5375 editor->Create(m_gridWin, -1,
5376 new wxGridCellEditorEvtHandler(this, editor));
5377 }
5378
5379 editor->SetSize( rect );
5380
5381 editor->Show( TRUE, attr );
5382 editor->BeginEdit(row, col, this);
5383 attr->DecRef();
5384 }
5385 }
5386 }
5387
5388
5389 void wxGrid::HideCellEditControl()
5390 {
5391 if ( IsCellEditControlEnabled() )
5392 {
5393 int row = m_currentCellCoords.GetRow();
5394 int col = m_currentCellCoords.GetCol();
5395
5396 wxGridCellAttr* attr = GetCellAttr(row, col);
5397 attr->GetEditor(this, row, col)->Show( FALSE );
5398 attr->DecRef();
5399 m_gridWin->SetFocus();
5400 }
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