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