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