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