1. wxGCBoolRenderer looks better under MSW
[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 return;
4125 }
4126
4127 if ( dragCol >= 0 )
4128 {
4129 m_dragRowOrCol = dragCol;
4130
4131 if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
4132 {
4133 if ( CanDragColSize() && CanDragGridSize() )
4134 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL);
4135 }
4136
4137 return;
4138 }
4139
4140 // Neither on a row or col edge
4141 //
4142 if ( m_cursorMode != WXGRID_CURSOR_SELECT_CELL )
4143 {
4144 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL);
4145 }
4146 }
4147 }
4148
4149
4150 void wxGrid::DoEndDragResizeRow()
4151 {
4152 if ( m_dragLastPos >= 0 )
4153 {
4154 // erase the last line and resize the row
4155 //
4156 int cw, ch, left, dummy;
4157 m_gridWin->GetClientSize( &cw, &ch );
4158 CalcUnscrolledPosition( 0, 0, &left, &dummy );
4159
4160 wxClientDC dc( m_gridWin );
4161 PrepareDC( dc );
4162 dc.SetLogicalFunction( wxINVERT );
4163 dc.DrawLine( left, m_dragLastPos, left+cw, m_dragLastPos );
4164 HideCellEditControl();
4165 SaveEditControlValue();
4166
4167 int rowTop = GetRowTop(m_dragRowOrCol);
4168 SetRowSize( m_dragRowOrCol,
4169 wxMax( m_dragLastPos - rowTop, WXGRID_MIN_ROW_HEIGHT ) );
4170
4171 if ( !GetBatchCount() )
4172 {
4173 // Only needed to get the correct rect.y:
4174 wxRect rect ( CellToRect( m_dragRowOrCol, 0 ) );
4175 rect.x = 0;
4176 CalcScrolledPosition(0, rect.y, &dummy, &rect.y);
4177 rect.width = m_rowLabelWidth;
4178 rect.height = ch - rect.y;
4179 m_rowLabelWin->Refresh( TRUE, &rect );
4180 rect.width = cw;
4181 m_gridWin->Refresh( FALSE, &rect );
4182 }
4183
4184 ShowCellEditControl();
4185 }
4186 }
4187
4188
4189 void wxGrid::DoEndDragResizeCol()
4190 {
4191 if ( m_dragLastPos >= 0 )
4192 {
4193 // erase the last line and resize the col
4194 //
4195 int cw, ch, dummy, top;
4196 m_gridWin->GetClientSize( &cw, &ch );
4197 CalcUnscrolledPosition( 0, 0, &dummy, &top );
4198
4199 wxClientDC dc( m_gridWin );
4200 PrepareDC( dc );
4201 dc.SetLogicalFunction( wxINVERT );
4202 dc.DrawLine( m_dragLastPos, top, m_dragLastPos, top+ch );
4203 HideCellEditControl();
4204 SaveEditControlValue();
4205
4206 int colLeft = GetColLeft(m_dragRowOrCol);
4207 SetColSize( m_dragRowOrCol,
4208 wxMax( m_dragLastPos - colLeft,
4209 GetColMinimalWidth(m_dragRowOrCol) ) );
4210
4211 if ( !GetBatchCount() )
4212 {
4213 // Only needed to get the correct rect.x:
4214 wxRect rect ( CellToRect( 0, m_dragRowOrCol ) );
4215 rect.y = 0;
4216 CalcScrolledPosition(rect.x, 0, &rect.x, &dummy);
4217 rect.width = cw - rect.x;
4218 rect.height = m_colLabelHeight;
4219 m_colLabelWin->Refresh( TRUE, &rect );
4220 rect.height = ch;
4221 m_gridWin->Refresh( FALSE, &rect );
4222 }
4223
4224 ShowCellEditControl();
4225 }
4226 }
4227
4228
4229
4230 //
4231 // ------ interaction with data model
4232 //
4233 bool wxGrid::ProcessTableMessage( wxGridTableMessage& msg )
4234 {
4235 switch ( msg.GetId() )
4236 {
4237 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES:
4238 return GetModelValues();
4239
4240 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES:
4241 return SetModelValues();
4242
4243 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED:
4244 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED:
4245 case wxGRIDTABLE_NOTIFY_ROWS_DELETED:
4246 case wxGRIDTABLE_NOTIFY_COLS_INSERTED:
4247 case wxGRIDTABLE_NOTIFY_COLS_APPENDED:
4248 case wxGRIDTABLE_NOTIFY_COLS_DELETED:
4249 return Redimension( msg );
4250
4251 default:
4252 return FALSE;
4253 }
4254 }
4255
4256
4257
4258 // The behaviour of this function depends on the grid table class
4259 // Clear() function. For the default wxGridStringTable class the
4260 // behavious is to replace all cell contents with wxEmptyString but
4261 // not to change the number of rows or cols.
4262 //
4263 void wxGrid::ClearGrid()
4264 {
4265 if ( m_table )
4266 {
4267 if (IsCellEditControlEnabled())
4268 DisableCellEditControl();
4269
4270 m_table->Clear();
4271 if ( !GetBatchCount() ) m_gridWin->Refresh();
4272 }
4273 }
4274
4275
4276 bool wxGrid::InsertRows( int pos, int numRows, bool WXUNUSED(updateLabels) )
4277 {
4278 // TODO: something with updateLabels flag
4279
4280 if ( !m_created )
4281 {
4282 wxFAIL_MSG( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") );
4283 return FALSE;
4284 }
4285
4286 if ( m_table )
4287 {
4288 if (IsCellEditControlEnabled())
4289 DisableCellEditControl();
4290
4291 bool ok = m_table->InsertRows( pos, numRows );
4292
4293 // the table will have sent the results of the insert row
4294 // operation to this view object as a grid table message
4295 //
4296 if ( ok )
4297 {
4298 if ( m_numCols == 0 )
4299 {
4300 m_table->AppendCols( WXGRID_DEFAULT_NUMBER_COLS );
4301 //
4302 // TODO: perhaps instead of appending the default number of cols
4303 // we should remember what the last non-zero number of cols was ?
4304 //
4305 }
4306
4307 if ( m_currentCellCoords == wxGridNoCellCoords )
4308 {
4309 // if we have just inserted cols into an empty grid the current
4310 // cell will be undefined...
4311 //
4312 SetCurrentCell( 0, 0 );
4313 }
4314
4315 ClearSelection();
4316 if ( !GetBatchCount() ) Refresh();
4317 }
4318
4319 return ok;
4320 }
4321 else
4322 {
4323 return FALSE;
4324 }
4325 }
4326
4327
4328 bool wxGrid::AppendRows( int numRows, bool WXUNUSED(updateLabels) )
4329 {
4330 // TODO: something with updateLabels flag
4331
4332 if ( !m_created )
4333 {
4334 wxFAIL_MSG( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") );
4335 return FALSE;
4336 }
4337
4338 if ( m_table && m_table->AppendRows( numRows ) )
4339 {
4340 if ( m_currentCellCoords == wxGridNoCellCoords )
4341 {
4342 // if we have just inserted cols into an empty grid the current
4343 // cell will be undefined...
4344 //
4345 SetCurrentCell( 0, 0 );
4346 }
4347
4348 // the table will have sent the results of the append row
4349 // operation to this view object as a grid table message
4350 //
4351 ClearSelection();
4352 if ( !GetBatchCount() ) Refresh();
4353 return TRUE;
4354 }
4355 else
4356 {
4357 return FALSE;
4358 }
4359 }
4360
4361
4362 bool wxGrid::DeleteRows( int pos, int numRows, bool WXUNUSED(updateLabels) )
4363 {
4364 // TODO: something with updateLabels flag
4365
4366 if ( !m_created )
4367 {
4368 wxFAIL_MSG( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") );
4369 return FALSE;
4370 }
4371
4372 if ( m_table )
4373 {
4374 if (IsCellEditControlEnabled())
4375 DisableCellEditControl();
4376
4377 if (m_table->DeleteRows( pos, numRows ))
4378 {
4379
4380 // the table will have sent the results of the delete row
4381 // operation to this view object as a grid table message
4382 //
4383 ClearSelection();
4384 if ( !GetBatchCount() ) Refresh();
4385 return TRUE;
4386 }
4387 }
4388 return FALSE;
4389 }
4390
4391
4392 bool wxGrid::InsertCols( int pos, int numCols, bool WXUNUSED(updateLabels) )
4393 {
4394 // TODO: something with updateLabels flag
4395
4396 if ( !m_created )
4397 {
4398 wxFAIL_MSG( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") );
4399 return FALSE;
4400 }
4401
4402 if ( m_table )
4403 {
4404 if (IsCellEditControlEnabled())
4405 DisableCellEditControl();
4406
4407 bool ok = m_table->InsertCols( pos, numCols );
4408
4409 // the table will have sent the results of the insert col
4410 // operation to this view object as a grid table message
4411 //
4412 if ( ok )
4413 {
4414 if ( m_currentCellCoords == wxGridNoCellCoords )
4415 {
4416 // if we have just inserted cols into an empty grid the current
4417 // cell will be undefined...
4418 //
4419 SetCurrentCell( 0, 0 );
4420 }
4421
4422 ClearSelection();
4423 if ( !GetBatchCount() ) Refresh();
4424 }
4425
4426 return ok;
4427 }
4428 else
4429 {
4430 return FALSE;
4431 }
4432 }
4433
4434
4435 bool wxGrid::AppendCols( int numCols, bool WXUNUSED(updateLabels) )
4436 {
4437 // TODO: something with updateLabels flag
4438
4439 if ( !m_created )
4440 {
4441 wxFAIL_MSG( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") );
4442 return FALSE;
4443 }
4444
4445 if ( m_table && m_table->AppendCols( numCols ) )
4446 {
4447 // the table will have sent the results of the append col
4448 // operation to this view object as a grid table message
4449 //
4450 if ( m_currentCellCoords == wxGridNoCellCoords )
4451 {
4452 // if we have just inserted cols into an empty grid the current
4453 // cell will be undefined...
4454 //
4455 SetCurrentCell( 0, 0 );
4456 }
4457
4458 ClearSelection();
4459 if ( !GetBatchCount() ) Refresh();
4460 return TRUE;
4461 }
4462 else
4463 {
4464 return FALSE;
4465 }
4466 }
4467
4468
4469 bool wxGrid::DeleteCols( int pos, int numCols, bool WXUNUSED(updateLabels) )
4470 {
4471 // TODO: something with updateLabels flag
4472
4473 if ( !m_created )
4474 {
4475 wxFAIL_MSG( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") );
4476 return FALSE;
4477 }
4478
4479 if ( m_table )
4480 {
4481 if (IsCellEditControlEnabled())
4482 DisableCellEditControl();
4483
4484 if ( m_table->DeleteCols( pos, numCols ) )
4485 {
4486 // the table will have sent the results of the delete col
4487 // operation to this view object as a grid table message
4488 //
4489 ClearSelection();
4490 if ( !GetBatchCount() ) Refresh();
4491 return TRUE;
4492 }
4493 }
4494 return FALSE;
4495 }
4496
4497
4498
4499 //
4500 // ----- event handlers
4501 //
4502
4503 // Generate a grid event based on a mouse event and
4504 // return the result of ProcessEvent()
4505 //
4506 bool wxGrid::SendEvent( const wxEventType type,
4507 int row, int col,
4508 wxMouseEvent& mouseEv )
4509 {
4510 if ( type == wxEVT_GRID_ROW_SIZE || type == wxEVT_GRID_COL_SIZE )
4511 {
4512 int rowOrCol = (row == -1 ? col : row);
4513
4514 wxGridSizeEvent gridEvt( GetId(),
4515 type,
4516 this,
4517 rowOrCol,
4518 mouseEv.GetX(), mouseEv.GetY(),
4519 mouseEv.ControlDown(),
4520 mouseEv.ShiftDown(),
4521 mouseEv.AltDown(),
4522 mouseEv.MetaDown() );
4523
4524 return GetEventHandler()->ProcessEvent(gridEvt);
4525 }
4526 else if ( type == wxEVT_GRID_RANGE_SELECT )
4527 {
4528 wxGridRangeSelectEvent gridEvt( GetId(),
4529 type,
4530 this,
4531 m_selectedTopLeft,
4532 m_selectedBottomRight,
4533 mouseEv.ControlDown(),
4534 mouseEv.ShiftDown(),
4535 mouseEv.AltDown(),
4536 mouseEv.MetaDown() );
4537
4538 return GetEventHandler()->ProcessEvent(gridEvt);
4539 }
4540 else
4541 {
4542 wxGridEvent gridEvt( GetId(),
4543 type,
4544 this,
4545 row, col,
4546 mouseEv.GetX(), mouseEv.GetY(),
4547 mouseEv.ControlDown(),
4548 mouseEv.ShiftDown(),
4549 mouseEv.AltDown(),
4550 mouseEv.MetaDown() );
4551
4552 return GetEventHandler()->ProcessEvent(gridEvt);
4553 }
4554 }
4555
4556
4557 // Generate a grid event of specified type and return the result
4558 // of ProcessEvent().
4559 //
4560 bool wxGrid::SendEvent( const wxEventType type,
4561 int row, int col )
4562 {
4563 if ( type == wxEVT_GRID_ROW_SIZE || type == wxEVT_GRID_COL_SIZE )
4564 {
4565 int rowOrCol = (row == -1 ? col : row);
4566
4567 wxGridSizeEvent gridEvt( GetId(),
4568 type,
4569 this,
4570 rowOrCol );
4571
4572 return GetEventHandler()->ProcessEvent(gridEvt);
4573 }
4574 else
4575 {
4576 wxGridEvent gridEvt( GetId(),
4577 type,
4578 this,
4579 row, col );
4580
4581 return GetEventHandler()->ProcessEvent(gridEvt);
4582 }
4583 }
4584
4585
4586 void wxGrid::OnPaint( wxPaintEvent& WXUNUSED(event) )
4587 {
4588 wxPaintDC dc( this );
4589
4590 if ( m_currentCellCoords == wxGridNoCellCoords &&
4591 m_numRows && m_numCols )
4592 {
4593 m_currentCellCoords.Set(0, 0);
4594 ShowCellEditControl();
4595 }
4596
4597 m_displayed = TRUE;
4598 }
4599
4600
4601 // This is just here to make sure that CalcDimensions gets called when
4602 // the grid view is resized... then the size event is skipped to allow
4603 // the box sizers to handle everything
4604 //
4605 void wxGrid::OnSize( wxSizeEvent& event )
4606 {
4607 CalcWindowSizes();
4608 CalcDimensions();
4609 }
4610
4611
4612 void wxGrid::OnKeyDown( wxKeyEvent& event )
4613 {
4614 if ( m_inOnKeyDown )
4615 {
4616 // shouldn't be here - we are going round in circles...
4617 //
4618 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") );
4619 }
4620
4621 m_inOnKeyDown = TRUE;
4622
4623 // propagate the event up and see if it gets processed
4624 //
4625 wxWindow *parent = GetParent();
4626 wxKeyEvent keyEvt( event );
4627 keyEvt.SetEventObject( parent );
4628
4629 if ( !parent->GetEventHandler()->ProcessEvent( keyEvt ) )
4630 {
4631
4632 // TODO: Should also support Shift-cursor keys for
4633 // extending the selection. Maybe add a flag to
4634 // MoveCursorXXX() and MoveCursorXXXBlock() and
4635 // just send event.ShiftDown().
4636
4637 // try local handlers
4638 //
4639 switch ( event.KeyCode() )
4640 {
4641 case WXK_UP:
4642 if ( event.ControlDown() )
4643 {
4644 MoveCursorUpBlock();
4645 }
4646 else
4647 {
4648 MoveCursorUp();
4649 }
4650 break;
4651
4652 case WXK_DOWN:
4653 if ( event.ControlDown() )
4654 {
4655 MoveCursorDownBlock();
4656 }
4657 else
4658 {
4659 MoveCursorDown();
4660 }
4661 break;
4662
4663 case WXK_LEFT:
4664 if ( event.ControlDown() )
4665 {
4666 MoveCursorLeftBlock();
4667 }
4668 else
4669 {
4670 MoveCursorLeft();
4671 }
4672 break;
4673
4674 case WXK_RIGHT:
4675 if ( event.ControlDown() )
4676 {
4677 MoveCursorRightBlock();
4678 }
4679 else
4680 {
4681 MoveCursorRight();
4682 }
4683 break;
4684
4685 case WXK_RETURN:
4686 if ( event.ControlDown() )
4687 {
4688 event.Skip(); // to let the edit control have the return
4689 }
4690 else
4691 {
4692 MoveCursorDown();
4693 }
4694 break;
4695
4696 case WXK_TAB:
4697 if (event.ShiftDown())
4698 MoveCursorLeft();
4699 else
4700 MoveCursorRight();
4701 break;
4702
4703 case WXK_HOME:
4704 if ( event.ControlDown() )
4705 {
4706 MakeCellVisible( 0, 0 );
4707 SetCurrentCell( 0, 0 );
4708 }
4709 else
4710 {
4711 event.Skip();
4712 }
4713 break;
4714
4715 case WXK_END:
4716 if ( event.ControlDown() )
4717 {
4718 MakeCellVisible( m_numRows-1, m_numCols-1 );
4719 SetCurrentCell( m_numRows-1, m_numCols-1 );
4720 }
4721 else
4722 {
4723 event.Skip();
4724 }
4725 break;
4726
4727 case WXK_PRIOR:
4728 MovePageUp();
4729 break;
4730
4731 case WXK_NEXT:
4732 MovePageDown();
4733 break;
4734
4735 case WXK_SPACE:
4736 if ( !IsEditable() )
4737 {
4738 MoveCursorRight();
4739 break;
4740 }
4741 // Otherwise fall through to default
4742
4743 default:
4744 // alphanumeric keys enable the cell edit control
4745 if ( !(event.AltDown() ||
4746 event.MetaDown() ||
4747 event.ControlDown()) &&
4748 isalnum(event.KeyCode()) &&
4749 !IsCellEditControlEnabled() &&
4750 CanEnableCellControl() )
4751 {
4752 EnableCellEditControl();
4753 int row = m_currentCellCoords.GetRow();
4754 int col = m_currentCellCoords.GetCol();
4755 wxGridCellAttr* attr = GetCellAttr(row, col);
4756 attr->GetEditor(this, row, col)->StartingKey(event);
4757 attr->DecRef();
4758 }
4759 else
4760 {
4761 // let others process char events with modifiers or all
4762 // char events for readonly cells
4763 event.Skip();
4764 }
4765 break;
4766 }
4767 }
4768
4769 m_inOnKeyDown = FALSE;
4770 }
4771
4772
4773 void wxGrid::OnEraseBackground(wxEraseEvent&)
4774 {
4775 }
4776
4777 void wxGrid::SetCurrentCell( const wxGridCellCoords& coords )
4778 {
4779 if ( SendEvent( wxEVT_GRID_SELECT_CELL, coords.GetRow(), coords.GetCol() ) )
4780 {
4781 // the event has been intercepted - do nothing
4782 return;
4783 }
4784
4785 if ( m_displayed &&
4786 m_currentCellCoords != wxGridNoCellCoords )
4787 {
4788 HideCellEditControl();
4789 DisableCellEditControl();
4790
4791 // Clear the old current cell highlight
4792 wxRect r = BlockToDeviceRect(m_currentCellCoords, m_currentCellCoords);
4793
4794 // Otherwise refresh redraws the highlight!
4795 m_currentCellCoords = coords;
4796
4797 m_gridWin->Refresh( FALSE, &r );
4798 }
4799
4800 m_currentCellCoords = coords;
4801
4802 if ( m_displayed )
4803 {
4804 wxClientDC dc(m_gridWin);
4805 PrepareDC(dc);
4806
4807 wxGridCellAttr* attr = GetCellAttr(coords);
4808 DrawCellHighlight(dc, attr);
4809 attr->DecRef();
4810
4811 if ( IsSelection() )
4812 {
4813 wxRect r( SelectionToDeviceRect() );
4814 ClearSelection();
4815 if ( !GetBatchCount() ) m_gridWin->Refresh( FALSE, &r );
4816 }
4817 }
4818 }
4819
4820
4821 //
4822 // ------ functions to get/send data (see also public functions)
4823 //
4824
4825 bool wxGrid::GetModelValues()
4826 {
4827 if ( m_table )
4828 {
4829 // all we need to do is repaint the grid
4830 //
4831 m_gridWin->Refresh();
4832 return TRUE;
4833 }
4834
4835 return FALSE;
4836 }
4837
4838
4839 bool wxGrid::SetModelValues()
4840 {
4841 int row, col;
4842
4843 if ( m_table )
4844 {
4845 for ( row = 0; row < m_numRows; row++ )
4846 {
4847 for ( col = 0; col < m_numCols; col++ )
4848 {
4849 m_table->SetValue( row, col, GetCellValue(row, col) );
4850 }
4851 }
4852
4853 return TRUE;
4854 }
4855
4856 return FALSE;
4857 }
4858
4859
4860
4861 // Note - this function only draws cells that are in the list of
4862 // exposed cells (usually set from the update region by
4863 // CalcExposedCells)
4864 //
4865 void wxGrid::DrawGridCellArea( wxDC& dc )
4866 {
4867 if ( !m_numRows || !m_numCols ) return;
4868
4869 size_t i;
4870 size_t numCells = m_cellsExposed.GetCount();
4871
4872 for ( i = 0; i < numCells; i++ )
4873 {
4874 DrawCell( dc, m_cellsExposed[i] );
4875 }
4876 }
4877
4878
4879 void wxGrid::DrawGridSpace( wxDC& dc )
4880 {
4881 if ( m_numRows && m_numCols )
4882 {
4883 int cw, ch;
4884 m_gridWin->GetClientSize( &cw, &ch );
4885
4886 int right, bottom;
4887 CalcUnscrolledPosition( cw, ch, &right, &bottom );
4888
4889 if ( right > GetColRight(m_numCols-1) ||
4890 bottom > GetRowBottom(m_numRows-1) )
4891 {
4892 int left, top;
4893 CalcUnscrolledPosition( 0, 0, &left, &top );
4894
4895 dc.SetBrush( wxBrush(GetDefaultCellBackgroundColour(), wxSOLID) );
4896 dc.SetPen( *wxTRANSPARENT_PEN );
4897
4898 if ( right > GetColRight(m_numCols-1) )
4899 dc.DrawRectangle( GetColRight(m_numCols-1), top,
4900 right - GetColRight(m_numCols-1), ch );
4901
4902 if ( bottom > GetRowBottom(m_numRows-1) )
4903 dc.DrawRectangle( left, GetRowBottom(m_numRows-1),
4904 cw, bottom - GetRowBottom(m_numRows-1) );
4905 }
4906 }
4907 }
4908
4909
4910 void wxGrid::DrawCell( wxDC& dc, const wxGridCellCoords& coords )
4911 {
4912 int row = coords.GetRow();
4913 int col = coords.GetCol();
4914
4915 if ( GetColWidth(col) <= 0 || GetRowHeight(row) <= 0 )
4916 return;
4917
4918 // we draw the cell border ourselves
4919 #if !WXGRID_DRAW_LINES
4920 if ( m_gridLinesEnabled )
4921 DrawCellBorder( dc, coords );
4922 #endif
4923
4924 wxGridCellAttr* attr = GetCellAttr(row, col);
4925
4926 bool isCurrent = coords == m_currentCellCoords;
4927
4928 wxRect rect;
4929 rect.x = GetColLeft(col);
4930 rect.y = GetRowTop(row);
4931 rect.width = GetColWidth(col) - 1;
4932 rect.height = GetRowHeight(row) - 1;
4933
4934 // if the editor is shown, we should use it and not the renderer
4935 if ( isCurrent && IsCellEditControlEnabled() )
4936 {
4937 attr->GetEditor(this, row, col)->PaintBackground(rect, attr);
4938 }
4939 else
4940 {
4941 // but all the rest is drawn by the cell renderer and hence may be
4942 // customized
4943 attr->GetRenderer(this, row, col)->
4944 Draw(*this, *attr, dc, rect, row, col, IsInSelection(coords));
4945
4946 }
4947
4948 attr->DecRef();
4949 }
4950
4951 void wxGrid::DrawCellHighlight( wxDC& dc, const wxGridCellAttr *attr )
4952 {
4953 int row = m_currentCellCoords.GetRow();
4954 int col = m_currentCellCoords.GetCol();
4955
4956 if ( GetColWidth(col) <= 0 || GetRowHeight(row) <= 0 )
4957 return;
4958
4959 wxRect rect;
4960 rect.x = GetColLeft(col);
4961 rect.y = GetRowTop(row);
4962 rect.width = GetColWidth(col) - 1;
4963 rect.height = GetRowHeight(row) - 1;
4964
4965 // hmmm... what could we do here to show that the cell is disabled?
4966 // for now, I just draw a thinner border than for the other ones, but
4967 // it doesn't look really good
4968 dc.SetPen(wxPen(m_gridLineColour, attr->IsReadOnly() ? 1 : 3, wxSOLID));
4969 dc.SetBrush(*wxTRANSPARENT_BRUSH);
4970
4971 dc.DrawRectangle(rect);
4972
4973 #if 0
4974 // VZ: my experiments with 3d borders...
4975
4976 // how to properly set colours for arbitrary bg?
4977 wxCoord x1 = rect.x,
4978 y1 = rect.y,
4979 x2 = rect.x + rect.width -1,
4980 y2 = rect.y + rect.height -1;
4981
4982 dc.SetPen(*wxWHITE_PEN);
4983 dc.DrawLine(x1, y1, x2, y1);
4984 dc.DrawLine(x1, y1, x1, y2);
4985
4986 dc.DrawLine(x1 + 1, y2 - 1, x2 - 1, y2 - 1);
4987 dc.DrawLine(x2 - 1, y1 + 1, x2 - 1, y2 );
4988
4989 dc.SetPen(*wxBLACK_PEN);
4990 dc.DrawLine(x1, y2, x2, y2);
4991 dc.DrawLine(x2, y1, x2, y2+1);
4992 #endif // 0
4993 }
4994
4995
4996 void wxGrid::DrawCellBorder( wxDC& dc, const wxGridCellCoords& coords )
4997 {
4998 int row = coords.GetRow();
4999 int col = coords.GetCol();
5000 if ( GetColWidth(col) <= 0 || GetRowHeight(row) <= 0 )
5001 return;
5002
5003 dc.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID) );
5004
5005 // right hand border
5006 //
5007 dc.DrawLine( GetColRight(col), GetRowTop(row),
5008 GetColRight(col), GetRowBottom(row) );
5009
5010 // bottom border
5011 //
5012 dc.DrawLine( GetColLeft(col), GetRowBottom(row),
5013 GetColRight(col), GetRowBottom(row) );
5014 }
5015
5016 void wxGrid::DrawHighlight(wxDC& dc)
5017 {
5018 if ( IsCellEditControlEnabled() )
5019 {
5020 // don't show highlight when the edit control is shown
5021 return;
5022 }
5023
5024 // if the active cell was repainted, repaint its highlight too because it
5025 // might have been damaged by the grid lines
5026 size_t count = m_cellsExposed.GetCount();
5027 for ( size_t n = 0; n < count; n++ )
5028 {
5029 if ( m_cellsExposed[n] == m_currentCellCoords )
5030 {
5031 wxGridCellAttr* attr = GetCellAttr(m_currentCellCoords);
5032 DrawCellHighlight(dc, attr);
5033 attr->DecRef();
5034
5035 break;
5036 }
5037 }
5038 }
5039
5040 // TODO: remove this ???
5041 // This is used to redraw all grid lines e.g. when the grid line colour
5042 // has been changed
5043 //
5044 void wxGrid::DrawAllGridLines( wxDC& dc, const wxRegion & reg )
5045 {
5046 if ( !m_gridLinesEnabled ||
5047 !m_numRows ||
5048 !m_numCols ) return;
5049
5050 int top, bottom, left, right;
5051
5052 #ifndef __WXGTK__
5053 if (reg.IsEmpty())
5054 {
5055 int cw, ch;
5056 m_gridWin->GetClientSize(&cw, &ch);
5057
5058 // virtual coords of visible area
5059 //
5060 CalcUnscrolledPosition( 0, 0, &left, &top );
5061 CalcUnscrolledPosition( cw, ch, &right, &bottom );
5062 }
5063 else
5064 {
5065 wxCoord x, y, w, h;
5066 reg.GetBox(x, y, w, h);
5067 CalcUnscrolledPosition( x, y, &left, &top );
5068 CalcUnscrolledPosition( x + w, y + h, &right, &bottom );
5069 }
5070 #else
5071 int cw, ch;
5072 m_gridWin->GetClientSize(&cw, &ch);
5073 CalcUnscrolledPosition( 0, 0, &left, &top );
5074 CalcUnscrolledPosition( cw, ch, &right, &bottom );
5075 #endif
5076
5077 // avoid drawing grid lines past the last row and col
5078 //
5079 right = wxMin( right, GetColRight(m_numCols - 1) );
5080 bottom = wxMin( bottom, GetRowBottom(m_numRows - 1) );
5081
5082 dc.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID) );
5083
5084 // horizontal grid lines
5085 //
5086 int i;
5087 for ( i = 0; i < m_numRows; i++ )
5088 {
5089 int bot = GetRowBottom(i) - 1;
5090
5091 if ( bot > bottom )
5092 {
5093 break;
5094 }
5095
5096 if ( bot >= top )
5097 {
5098 dc.DrawLine( left, bot, right, bot );
5099 }
5100 }
5101
5102
5103 // vertical grid lines
5104 //
5105 for ( i = 0; i < m_numCols; i++ )
5106 {
5107 int colRight = GetColRight(i) - 1;
5108 if ( colRight > right )
5109 {
5110 break;
5111 }
5112
5113 if ( colRight >= left )
5114 {
5115 dc.DrawLine( colRight, top, colRight, bottom );
5116 }
5117 }
5118 }
5119
5120
5121 void wxGrid::DrawRowLabels( wxDC& dc )
5122 {
5123 if ( !m_numRows || !m_numCols ) return;
5124
5125 size_t i;
5126 size_t numLabels = m_rowLabelsExposed.GetCount();
5127
5128 for ( i = 0; i < numLabels; i++ )
5129 {
5130 DrawRowLabel( dc, m_rowLabelsExposed[i] );
5131 }
5132 }
5133
5134
5135 void wxGrid::DrawRowLabel( wxDC& dc, int row )
5136 {
5137 if ( GetRowHeight(row) <= 0 )
5138 return;
5139
5140 int rowTop = GetRowTop(row),
5141 rowBottom = GetRowBottom(row) - 1;
5142
5143 dc.SetPen( *wxBLACK_PEN );
5144 dc.DrawLine( m_rowLabelWidth-1, rowTop,
5145 m_rowLabelWidth-1, rowBottom );
5146
5147 dc.DrawLine( 0, rowBottom, m_rowLabelWidth-1, rowBottom );
5148
5149 dc.SetPen( *wxWHITE_PEN );
5150 dc.DrawLine( 0, rowTop, 0, rowBottom );
5151 dc.DrawLine( 0, rowTop, m_rowLabelWidth-1, rowTop );
5152
5153 dc.SetBackgroundMode( wxTRANSPARENT );
5154 dc.SetTextForeground( GetLabelTextColour() );
5155 dc.SetFont( GetLabelFont() );
5156
5157 int hAlign, vAlign;
5158 GetRowLabelAlignment( &hAlign, &vAlign );
5159
5160 wxRect rect;
5161 rect.SetX( 2 );
5162 rect.SetY( GetRowTop(row) + 2 );
5163 rect.SetWidth( m_rowLabelWidth - 4 );
5164 rect.SetHeight( GetRowHeight(row) - 4 );
5165 DrawTextRectangle( dc, GetRowLabelValue( row ), rect, hAlign, vAlign );
5166 }
5167
5168
5169 void wxGrid::DrawColLabels( wxDC& dc )
5170 {
5171 if ( !m_numRows || !m_numCols ) return;
5172
5173 size_t i;
5174 size_t numLabels = m_colLabelsExposed.GetCount();
5175
5176 for ( i = 0; i < numLabels; i++ )
5177 {
5178 DrawColLabel( dc, m_colLabelsExposed[i] );
5179 }
5180 }
5181
5182
5183 void wxGrid::DrawColLabel( wxDC& dc, int col )
5184 {
5185 if ( GetColWidth(col) <= 0 )
5186 return;
5187
5188 int colLeft = GetColLeft(col),
5189 colRight = GetColRight(col) - 1;
5190
5191 dc.SetPen( *wxBLACK_PEN );
5192 dc.DrawLine( colRight, 0,
5193 colRight, m_colLabelHeight-1 );
5194
5195 dc.DrawLine( colLeft, m_colLabelHeight-1,
5196 colRight, m_colLabelHeight-1 );
5197
5198 dc.SetPen( *wxWHITE_PEN );
5199 dc.DrawLine( colLeft, 0, colLeft, m_colLabelHeight-1 );
5200 dc.DrawLine( colLeft, 0, colRight, 0 );
5201
5202 dc.SetBackgroundMode( wxTRANSPARENT );
5203 dc.SetTextForeground( GetLabelTextColour() );
5204 dc.SetFont( GetLabelFont() );
5205
5206 dc.SetBackgroundMode( wxTRANSPARENT );
5207 dc.SetTextForeground( GetLabelTextColour() );
5208 dc.SetFont( GetLabelFont() );
5209
5210 int hAlign, vAlign;
5211 GetColLabelAlignment( &hAlign, &vAlign );
5212
5213 wxRect rect;
5214 rect.SetX( colLeft + 2 );
5215 rect.SetY( 2 );
5216 rect.SetWidth( GetColWidth(col) - 4 );
5217 rect.SetHeight( m_colLabelHeight - 4 );
5218 DrawTextRectangle( dc, GetColLabelValue( col ), rect, hAlign, vAlign );
5219 }
5220
5221
5222 void wxGrid::DrawTextRectangle( wxDC& dc,
5223 const wxString& value,
5224 const wxRect& rect,
5225 int horizAlign,
5226 int vertAlign )
5227 {
5228 long textWidth, textHeight;
5229 long lineWidth, lineHeight;
5230 wxArrayString lines;
5231
5232 dc.SetClippingRegion( rect );
5233 StringToLines( value, lines );
5234 if ( lines.GetCount() )
5235 {
5236 GetTextBoxSize( dc, lines, &textWidth, &textHeight );
5237 dc.GetTextExtent( lines[0], &lineWidth, &lineHeight );
5238
5239 float x, y;
5240 switch ( horizAlign )
5241 {
5242 case wxRIGHT:
5243 x = rect.x + (rect.width - textWidth - 1);
5244 break;
5245
5246 case wxCENTRE:
5247 x = rect.x + ((rect.width - textWidth)/2);
5248 break;
5249
5250 case wxLEFT:
5251 default:
5252 x = rect.x + 1;
5253 break;
5254 }
5255
5256 switch ( vertAlign )
5257 {
5258 case wxBOTTOM:
5259 y = rect.y + (rect.height - textHeight - 1);
5260 break;
5261
5262 case wxCENTRE:
5263 y = rect.y + ((rect.height - textHeight)/2);
5264 break;
5265
5266 case wxTOP:
5267 default:
5268 y = rect.y + 1;
5269 break;
5270 }
5271
5272 for ( size_t i = 0; i < lines.GetCount(); i++ )
5273 {
5274 dc.DrawText( lines[i], (long)x, (long)y );
5275 y += lineHeight;
5276 }
5277 }
5278
5279 dc.DestroyClippingRegion();
5280 }
5281
5282
5283 // Split multi line text up into an array of strings. Any existing
5284 // contents of the string array are preserved.
5285 //
5286 void wxGrid::StringToLines( const wxString& value, wxArrayString& lines )
5287 {
5288 int startPos = 0;
5289 int pos;
5290 wxString eol = wxTextFile::GetEOL( wxTextFileType_Unix );
5291 wxString tVal = wxTextFile::Translate( value, wxTextFileType_Unix );
5292
5293 while ( startPos < (int)tVal.Length() )
5294 {
5295 pos = tVal.Mid(startPos).Find( eol );
5296 if ( pos < 0 )
5297 {
5298 break;
5299 }
5300 else if ( pos == 0 )
5301 {
5302 lines.Add( wxEmptyString );
5303 }
5304 else
5305 {
5306 lines.Add( value.Mid(startPos, pos) );
5307 }
5308 startPos += pos+1;
5309 }
5310 if ( startPos < (int)value.Length() )
5311 {
5312 lines.Add( value.Mid( startPos ) );
5313 }
5314 }
5315
5316
5317 void wxGrid::GetTextBoxSize( wxDC& dc,
5318 wxArrayString& lines,
5319 long *width, long *height )
5320 {
5321 long w = 0;
5322 long h = 0;
5323 long lineW, lineH;
5324
5325 size_t i;
5326 for ( i = 0; i < lines.GetCount(); i++ )
5327 {
5328 dc.GetTextExtent( lines[i], &lineW, &lineH );
5329 w = wxMax( w, lineW );
5330 h += lineH;
5331 }
5332
5333 *width = w;
5334 *height = h;
5335 }
5336
5337
5338 //
5339 // ------ Edit control functions
5340 //
5341
5342
5343 void wxGrid::EnableEditing( bool edit )
5344 {
5345 // TODO: improve this ?
5346 //
5347 if ( edit != m_editable )
5348 {
5349 m_editable = edit;
5350
5351 // FIXME IMHO this won't disable the edit control if edit == FALSE
5352 // because of the check in the beginning of
5353 // EnableCellEditControl() just below (VZ)
5354 EnableCellEditControl(m_editable);
5355 }
5356 }
5357
5358
5359 void wxGrid::EnableCellEditControl( bool enable )
5360 {
5361 if (! m_editable)
5362 return;
5363
5364 if ( m_currentCellCoords == wxGridNoCellCoords )
5365 SetCurrentCell( 0, 0 );
5366
5367 if ( enable != m_cellEditCtrlEnabled )
5368 {
5369 // TODO allow the app to Veto() this event?
5370 SendEvent(enable ? wxEVT_GRID_EDITOR_SHOWN : wxEVT_GRID_EDITOR_HIDDEN);
5371
5372 if ( enable )
5373 {
5374 // this should be checked by the caller!
5375 wxASSERT_MSG( CanEnableCellControl(),
5376 _T("can't enable editing for this cell!") );
5377
5378 // do it before ShowCellEditControl()
5379 m_cellEditCtrlEnabled = enable;
5380
5381 ShowCellEditControl();
5382 }
5383 else
5384 {
5385 HideCellEditControl();
5386 SaveEditControlValue();
5387
5388 // do it after HideCellEditControl()
5389 m_cellEditCtrlEnabled = enable;
5390 }
5391 }
5392 }
5393
5394 bool wxGrid::IsCurrentCellReadOnly() const
5395 {
5396 // const_cast
5397 wxGridCellAttr* attr = ((wxGrid *)this)->GetCellAttr(m_currentCellCoords);
5398 bool readonly = attr->IsReadOnly();
5399 attr->DecRef();
5400
5401 return readonly;
5402 }
5403
5404 bool wxGrid::CanEnableCellControl() const
5405 {
5406 return m_editable && !IsCurrentCellReadOnly();
5407 }
5408
5409 bool wxGrid::IsCellEditControlEnabled() const
5410 {
5411 // the cell edit control might be disable for all cells or just for the
5412 // current one if it's read only
5413 return m_cellEditCtrlEnabled ? !IsCurrentCellReadOnly() : FALSE;
5414 }
5415
5416 void wxGrid::ShowCellEditControl()
5417 {
5418 if ( IsCellEditControlEnabled() )
5419 {
5420 if ( !IsVisible( m_currentCellCoords ) )
5421 {
5422 return;
5423 }
5424 else
5425 {
5426 wxRect rect = CellToRect( m_currentCellCoords );
5427 int row = m_currentCellCoords.GetRow();
5428 int col = m_currentCellCoords.GetCol();
5429
5430 // convert to scrolled coords
5431 //
5432 CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
5433
5434 // done in PaintBackground()
5435 #if 0
5436 // erase the highlight and the cell contents because the editor
5437 // might not cover the entire cell
5438 wxClientDC dc( m_gridWin );
5439 PrepareDC( dc );
5440 dc.SetBrush(*wxLIGHT_GREY_BRUSH); //wxBrush(attr->GetBackgroundColour(), wxSOLID));
5441 dc.SetPen(*wxTRANSPARENT_PEN);
5442 dc.DrawRectangle(rect);
5443 #endif // 0
5444
5445 // cell is shifted by one pixel
5446 rect.x--;
5447 rect.y--;
5448
5449 wxGridCellAttr* attr = GetCellAttr(row, col);
5450 wxGridCellEditor* editor = attr->GetEditor(this, row, col);
5451 if ( !editor->IsCreated() )
5452 {
5453 editor->Create(m_gridWin, -1,
5454 new wxGridCellEditorEvtHandler(this, editor));
5455 }
5456
5457 editor->SetSize( rect );
5458
5459 editor->Show( TRUE, attr );
5460 editor->BeginEdit(row, col, this);
5461 attr->DecRef();
5462 }
5463 }
5464 }
5465
5466
5467 void wxGrid::HideCellEditControl()
5468 {
5469 if ( IsCellEditControlEnabled() )
5470 {
5471 int row = m_currentCellCoords.GetRow();
5472 int col = m_currentCellCoords.GetCol();
5473
5474 wxGridCellAttr* attr = GetCellAttr(row, col);
5475 attr->GetEditor(this, row, col)->Show( FALSE );
5476 attr->DecRef();
5477 m_gridWin->SetFocus();
5478 }
5479 }
5480
5481
5482 void wxGrid::SaveEditControlValue()
5483 {
5484 if ( IsCellEditControlEnabled() )
5485 {
5486 int row = m_currentCellCoords.GetRow();
5487 int col = m_currentCellCoords.GetCol();
5488
5489 wxGridCellAttr* attr = GetCellAttr(row, col);
5490 wxGridCellEditor* editor = attr->GetEditor(this, row, col);
5491 bool changed = editor->EndEdit(row, col, this);
5492
5493 attr->DecRef();
5494
5495 if (changed)
5496 {
5497 SendEvent( wxEVT_GRID_CELL_CHANGE,
5498 m_currentCellCoords.GetRow(),
5499 m_currentCellCoords.GetCol() );
5500 }
5501 }
5502 }
5503
5504
5505 //
5506 // ------ Grid location functions
5507 // Note that all of these functions work with the logical coordinates of
5508 // grid cells and labels so you will need to convert from device
5509 // coordinates for mouse events etc.
5510 //
5511
5512 void wxGrid::XYToCell( int x, int y, wxGridCellCoords& coords )
5513 {
5514 int row = YToRow(y);
5515 int col = XToCol(x);
5516
5517 if ( row == -1 || col == -1 )
5518 {
5519 coords = wxGridNoCellCoords;
5520 }
5521 else
5522 {
5523 coords.Set( row, col );
5524 }
5525 }
5526
5527
5528 int wxGrid::YToRow( int y )
5529 {
5530 int i;
5531
5532 for ( i = 0; i < m_numRows; i++ )
5533 {
5534 if ( y < GetRowBottom(i) )
5535 return i;
5536 }
5537
5538 return -1;
5539 }
5540
5541
5542 int wxGrid::XToCol( int x )
5543 {
5544 int i;
5545
5546 for ( i = 0; i < m_numCols; i++ )
5547 {
5548 if ( x < GetColRight(i) )
5549 return i;
5550 }
5551
5552 return -1;
5553 }
5554
5555
5556 // return the row number that that the y coord is near the edge of, or
5557 // -1 if not near an edge
5558 //
5559 int wxGrid::YToEdgeOfRow( int y )
5560 {
5561 int i, d;
5562
5563 for ( i = 0; i < m_numRows; i++ )
5564 {
5565 if ( GetRowHeight(i) > WXGRID_LABEL_EDGE_ZONE )
5566 {
5567 d = abs( y - GetRowBottom(i) );
5568 if ( d < WXGRID_LABEL_EDGE_ZONE )
5569 return i;
5570 }
5571 }
5572
5573 return -1;
5574 }
5575
5576
5577 // return the col number that that the x coord is near the edge of, or
5578 // -1 if not near an edge
5579 //
5580 int wxGrid::XToEdgeOfCol( int x )
5581 {
5582 int i, d;
5583
5584 for ( i = 0; i < m_numCols; i++ )
5585 {
5586 if ( GetColWidth(i) > WXGRID_LABEL_EDGE_ZONE )
5587 {
5588 d = abs( x - GetColRight(i) );
5589 if ( d < WXGRID_LABEL_EDGE_ZONE )
5590 return i;
5591 }
5592 }
5593
5594 return -1;
5595 }
5596
5597
5598 wxRect wxGrid::CellToRect( int row, int col )
5599 {
5600 wxRect rect( -1, -1, -1, -1 );
5601
5602 if ( row >= 0 && row < m_numRows &&
5603 col >= 0 && col < m_numCols )
5604 {
5605 rect.x = GetColLeft(col);
5606 rect.y = GetRowTop(row);
5607 rect.width = GetColWidth(col);
5608 rect.height = GetRowHeight(row);
5609 }
5610
5611 return rect;
5612 }
5613
5614
5615 bool wxGrid::IsVisible( int row, int col, bool wholeCellVisible )
5616 {
5617 // get the cell rectangle in logical coords
5618 //
5619 wxRect r( CellToRect( row, col ) );
5620
5621 // convert to device coords
5622 //
5623 int left, top, right, bottom;
5624 CalcScrolledPosition( r.GetLeft(), r.GetTop(), &left, &top );
5625 CalcScrolledPosition( r.GetRight(), r.GetBottom(), &right, &bottom );
5626
5627 // check against the client area of the grid window
5628 //
5629 int cw, ch;
5630 m_gridWin->GetClientSize( &cw, &ch );
5631
5632 if ( wholeCellVisible )
5633 {
5634 // is the cell wholly visible ?
5635 //
5636 return ( left >= 0 && right <= cw &&
5637 top >= 0 && bottom <= ch );
5638 }
5639 else
5640 {
5641 // is the cell partly visible ?
5642 //
5643 return ( ((left >=0 && left < cw) || (right > 0 && right <= cw)) &&
5644 ((top >=0 && top < ch) || (bottom > 0 && bottom <= ch)) );
5645 }
5646 }
5647
5648
5649 // make the specified cell location visible by doing a minimal amount
5650 // of scrolling
5651 //
5652 void wxGrid::MakeCellVisible( int row, int col )
5653 {
5654 int i;
5655 int xpos = -1, ypos = -1;
5656
5657 if ( row >= 0 && row < m_numRows &&
5658 col >= 0 && col < m_numCols )
5659 {
5660 // get the cell rectangle in logical coords
5661 //
5662 wxRect r( CellToRect( row, col ) );
5663
5664 // convert to device coords
5665 //
5666 int left, top, right, bottom;
5667 CalcScrolledPosition( r.GetLeft(), r.GetTop(), &left, &top );
5668 CalcScrolledPosition( r.GetRight(), r.GetBottom(), &right, &bottom );
5669
5670 int cw, ch;
5671 m_gridWin->GetClientSize( &cw, &ch );
5672
5673 if ( top < 0 )
5674 {
5675 ypos = r.GetTop();
5676 }
5677 else if ( bottom > ch )
5678 {
5679 int h = r.GetHeight();
5680 ypos = r.GetTop();
5681 for ( i = row-1; i >= 0; i-- )
5682 {
5683 int rowHeight = GetRowHeight(i);
5684 if ( h + rowHeight > ch )
5685 break;
5686
5687 h += rowHeight;
5688 ypos -= rowHeight;
5689 }
5690
5691 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
5692 // have rounding errors (this is important, because if we do, we
5693 // might not scroll at all and some cells won't be redrawn)
5694 ypos += GRID_SCROLL_LINE / 2;
5695 }
5696
5697 if ( left < 0 )
5698 {
5699 xpos = r.GetLeft();
5700 }
5701 else if ( right > cw )
5702 {
5703 int w = r.GetWidth();
5704 xpos = r.GetLeft();
5705 for ( i = col-1; i >= 0; i-- )
5706 {
5707 int colWidth = GetColWidth(i);
5708 if ( w + colWidth > cw )
5709 break;
5710
5711 w += colWidth;
5712 xpos -= colWidth;
5713 }
5714
5715 // see comment for ypos above
5716 xpos += GRID_SCROLL_LINE / 2;
5717 }
5718
5719 if ( xpos != -1 || ypos != -1 )
5720 {
5721 if ( xpos != -1 ) xpos /= GRID_SCROLL_LINE;
5722 if ( ypos != -1 ) ypos /= GRID_SCROLL_LINE;
5723 Scroll( xpos, ypos );
5724 AdjustScrollbars();
5725 }
5726 }
5727 }
5728
5729
5730 //
5731 // ------ Grid cursor movement functions
5732 //
5733
5734 bool wxGrid::MoveCursorUp()
5735 {
5736 if ( m_currentCellCoords != wxGridNoCellCoords &&
5737 m_currentCellCoords.GetRow() > 0 )
5738 {
5739 MakeCellVisible( m_currentCellCoords.GetRow() - 1,
5740 m_currentCellCoords.GetCol() );
5741
5742 SetCurrentCell( m_currentCellCoords.GetRow() - 1,
5743 m_currentCellCoords.GetCol() );
5744
5745 return TRUE;
5746 }
5747
5748 return FALSE;
5749 }
5750
5751
5752 bool wxGrid::MoveCursorDown()
5753 {
5754 // TODO: allow for scrolling
5755 //
5756 if ( m_currentCellCoords != wxGridNoCellCoords &&
5757 m_currentCellCoords.GetRow() < m_numRows-1 )
5758 {
5759 MakeCellVisible( m_currentCellCoords.GetRow() + 1,
5760 m_currentCellCoords.GetCol() );
5761
5762 SetCurrentCell( m_currentCellCoords.GetRow() + 1,
5763 m_currentCellCoords.GetCol() );
5764
5765 return TRUE;
5766 }
5767
5768 return FALSE;
5769 }
5770
5771
5772 bool wxGrid::MoveCursorLeft()
5773 {
5774 if ( m_currentCellCoords != wxGridNoCellCoords &&
5775 m_currentCellCoords.GetCol() > 0 )
5776 {
5777 MakeCellVisible( m_currentCellCoords.GetRow(),
5778 m_currentCellCoords.GetCol() - 1 );
5779
5780 SetCurrentCell( m_currentCellCoords.GetRow(),
5781 m_currentCellCoords.GetCol() - 1 );
5782
5783 return TRUE;
5784 }
5785
5786 return FALSE;
5787 }
5788
5789
5790 bool wxGrid::MoveCursorRight()
5791 {
5792 if ( m_currentCellCoords != wxGridNoCellCoords &&
5793 m_currentCellCoords.GetCol() < m_numCols - 1 )
5794 {
5795 MakeCellVisible( m_currentCellCoords.GetRow(),
5796 m_currentCellCoords.GetCol() + 1 );
5797
5798 SetCurrentCell( m_currentCellCoords.GetRow(),
5799 m_currentCellCoords.GetCol() + 1 );
5800
5801 return TRUE;
5802 }
5803
5804 return FALSE;
5805 }
5806
5807
5808 bool wxGrid::MovePageUp()
5809 {
5810 if ( m_currentCellCoords == wxGridNoCellCoords ) return FALSE;
5811
5812 int row = m_currentCellCoords.GetRow();
5813 if ( row > 0 )
5814 {
5815 int cw, ch;
5816 m_gridWin->GetClientSize( &cw, &ch );
5817
5818 int y = GetRowTop(row);
5819 int newRow = YToRow( y - ch + 1 );
5820 if ( newRow == -1 )
5821 {
5822 newRow = 0;
5823 }
5824 else if ( newRow == row )
5825 {
5826 newRow = row - 1;
5827 }
5828
5829 MakeCellVisible( newRow, m_currentCellCoords.GetCol() );
5830 SetCurrentCell( newRow, m_currentCellCoords.GetCol() );
5831
5832 return TRUE;
5833 }
5834
5835 return FALSE;
5836 }
5837
5838 bool wxGrid::MovePageDown()
5839 {
5840 if ( m_currentCellCoords == wxGridNoCellCoords ) return FALSE;
5841
5842 int row = m_currentCellCoords.GetRow();
5843 if ( row < m_numRows )
5844 {
5845 int cw, ch;
5846 m_gridWin->GetClientSize( &cw, &ch );
5847
5848 int y = GetRowTop(row);
5849 int newRow = YToRow( y + ch );
5850 if ( newRow == -1 )
5851 {
5852 newRow = m_numRows - 1;
5853 }
5854 else if ( newRow == row )
5855 {
5856 newRow = row + 1;
5857 }
5858
5859 MakeCellVisible( newRow, m_currentCellCoords.GetCol() );
5860 SetCurrentCell( newRow, m_currentCellCoords.GetCol() );
5861
5862 return TRUE;
5863 }
5864
5865 return FALSE;
5866 }
5867
5868 bool wxGrid::MoveCursorUpBlock()
5869 {
5870 if ( m_table &&
5871 m_currentCellCoords != wxGridNoCellCoords &&
5872 m_currentCellCoords.GetRow() > 0 )
5873 {
5874 int row = m_currentCellCoords.GetRow();
5875 int col = m_currentCellCoords.GetCol();
5876
5877 if ( m_table->IsEmptyCell(row, col) )
5878 {
5879 // starting in an empty cell: find the next block of
5880 // non-empty cells
5881 //
5882 while ( row > 0 )
5883 {
5884 row-- ;
5885 if ( !(m_table->IsEmptyCell(row, col)) ) break;
5886 }
5887 }
5888 else if ( m_table->IsEmptyCell(row-1, col) )
5889 {
5890 // starting at the top of a block: find the next block
5891 //
5892 row--;
5893 while ( row > 0 )
5894 {
5895 row-- ;
5896 if ( !(m_table->IsEmptyCell(row, col)) ) break;
5897 }
5898 }
5899 else
5900 {
5901 // starting within a block: find the top of the block
5902 //
5903 while ( row > 0 )
5904 {
5905 row-- ;
5906 if ( m_table->IsEmptyCell(row, col) )
5907 {
5908 row++ ;
5909 break;
5910 }
5911 }
5912 }
5913
5914 MakeCellVisible( row, col );
5915 SetCurrentCell( row, col );
5916
5917 return TRUE;
5918 }
5919
5920 return FALSE;
5921 }
5922
5923 bool wxGrid::MoveCursorDownBlock()
5924 {
5925 if ( m_table &&
5926 m_currentCellCoords != wxGridNoCellCoords &&
5927 m_currentCellCoords.GetRow() < m_numRows-1 )
5928 {
5929 int row = m_currentCellCoords.GetRow();
5930 int col = m_currentCellCoords.GetCol();
5931
5932 if ( m_table->IsEmptyCell(row, col) )
5933 {
5934 // starting in an empty cell: find the next block of
5935 // non-empty cells
5936 //
5937 while ( row < m_numRows-1 )
5938 {
5939 row++ ;
5940 if ( !(m_table->IsEmptyCell(row, col)) ) break;
5941 }
5942 }
5943 else if ( m_table->IsEmptyCell(row+1, col) )
5944 {
5945 // starting at the bottom of a block: find the next block
5946 //
5947 row++;
5948 while ( row < m_numRows-1 )
5949 {
5950 row++ ;
5951 if ( !(m_table->IsEmptyCell(row, col)) ) break;
5952 }
5953 }
5954 else
5955 {
5956 // starting within a block: find the bottom of the block
5957 //
5958 while ( row < m_numRows-1 )
5959 {
5960 row++ ;
5961 if ( m_table->IsEmptyCell(row, col) )
5962 {
5963 row-- ;
5964 break;
5965 }
5966 }
5967 }
5968
5969 MakeCellVisible( row, col );
5970 SetCurrentCell( row, col );
5971
5972 return TRUE;
5973 }
5974
5975 return FALSE;
5976 }
5977
5978 bool wxGrid::MoveCursorLeftBlock()
5979 {
5980 if ( m_table &&
5981 m_currentCellCoords != wxGridNoCellCoords &&
5982 m_currentCellCoords.GetCol() > 0 )
5983 {
5984 int row = m_currentCellCoords.GetRow();
5985 int col = m_currentCellCoords.GetCol();
5986
5987 if ( m_table->IsEmptyCell(row, col) )
5988 {
5989 // starting in an empty cell: find the next block of
5990 // non-empty cells
5991 //
5992 while ( col > 0 )
5993 {
5994 col-- ;
5995 if ( !(m_table->IsEmptyCell(row, col)) ) break;
5996 }
5997 }
5998 else if ( m_table->IsEmptyCell(row, col-1) )
5999 {
6000 // starting at the left of a block: find the next block
6001 //
6002 col--;
6003 while ( col > 0 )
6004 {
6005 col-- ;
6006 if ( !(m_table->IsEmptyCell(row, col)) ) break;
6007 }
6008 }
6009 else
6010 {
6011 // starting within a block: find the left of the block
6012 //
6013 while ( col > 0 )
6014 {
6015 col-- ;
6016 if ( m_table->IsEmptyCell(row, col) )
6017 {
6018 col++ ;
6019 break;
6020 }
6021 }
6022 }
6023
6024 MakeCellVisible( row, col );
6025 SetCurrentCell( row, col );
6026
6027 return TRUE;
6028 }
6029
6030 return FALSE;
6031 }
6032
6033 bool wxGrid::MoveCursorRightBlock()
6034 {
6035 if ( m_table &&
6036 m_currentCellCoords != wxGridNoCellCoords &&
6037 m_currentCellCoords.GetCol() < m_numCols-1 )
6038 {
6039 int row = m_currentCellCoords.GetRow();
6040 int col = m_currentCellCoords.GetCol();
6041
6042 if ( m_table->IsEmptyCell(row, col) )
6043 {
6044 // starting in an empty cell: find the next block of
6045 // non-empty cells
6046 //
6047 while ( col < m_numCols-1 )
6048 {
6049 col++ ;
6050 if ( !(m_table->IsEmptyCell(row, col)) ) break;
6051 }
6052 }
6053 else if ( m_table->IsEmptyCell(row, col+1) )
6054 {
6055 // starting at the right of a block: find the next block
6056 //
6057 col++;
6058 while ( col < m_numCols-1 )
6059 {
6060 col++ ;
6061 if ( !(m_table->IsEmptyCell(row, col)) ) break;
6062 }
6063 }
6064 else
6065 {
6066 // starting within a block: find the right of the block
6067 //
6068 while ( col < m_numCols-1 )
6069 {
6070 col++ ;
6071 if ( m_table->IsEmptyCell(row, col) )
6072 {
6073 col-- ;
6074 break;
6075 }
6076 }
6077 }
6078
6079 MakeCellVisible( row, col );
6080 SetCurrentCell( row, col );
6081
6082 return TRUE;
6083 }
6084
6085 return FALSE;
6086 }
6087
6088
6089
6090 //
6091 // ------ Label values and formatting
6092 //
6093
6094 void wxGrid::GetRowLabelAlignment( int *horiz, int *vert )
6095 {
6096 *horiz = m_rowLabelHorizAlign;
6097 *vert = m_rowLabelVertAlign;
6098 }
6099
6100 void wxGrid::GetColLabelAlignment( int *horiz, int *vert )
6101 {
6102 *horiz = m_colLabelHorizAlign;
6103 *vert = m_colLabelVertAlign;
6104 }
6105
6106 wxString wxGrid::GetRowLabelValue( int row )
6107 {
6108 if ( m_table )
6109 {
6110 return m_table->GetRowLabelValue( row );
6111 }
6112 else
6113 {
6114 wxString s;
6115 s << row;
6116 return s;
6117 }
6118 }
6119
6120 wxString wxGrid::GetColLabelValue( int col )
6121 {
6122 if ( m_table )
6123 {
6124 return m_table->GetColLabelValue( col );
6125 }
6126 else
6127 {
6128 wxString s;
6129 s << col;
6130 return s;
6131 }
6132 }
6133
6134
6135 void wxGrid::SetRowLabelSize( int width )
6136 {
6137 width = wxMax( width, 0 );
6138 if ( width != m_rowLabelWidth )
6139 {
6140 if ( width == 0 )
6141 {
6142 m_rowLabelWin->Show( FALSE );
6143 m_cornerLabelWin->Show( FALSE );
6144 }
6145 else if ( m_rowLabelWidth == 0 )
6146 {
6147 m_rowLabelWin->Show( TRUE );
6148 if ( m_colLabelHeight > 0 ) m_cornerLabelWin->Show( TRUE );
6149 }
6150
6151 m_rowLabelWidth = width;
6152 CalcWindowSizes();
6153 Refresh( TRUE );
6154 }
6155 }
6156
6157
6158 void wxGrid::SetColLabelSize( int height )
6159 {
6160 height = wxMax( height, 0 );
6161 if ( height != m_colLabelHeight )
6162 {
6163 if ( height == 0 )
6164 {
6165 m_colLabelWin->Show( FALSE );
6166 m_cornerLabelWin->Show( FALSE );
6167 }
6168 else if ( m_colLabelHeight == 0 )
6169 {
6170 m_colLabelWin->Show( TRUE );
6171 if ( m_rowLabelWidth > 0 ) m_cornerLabelWin->Show( TRUE );
6172 }
6173
6174 m_colLabelHeight = height;
6175 CalcWindowSizes();
6176 Refresh( TRUE );
6177 }
6178 }
6179
6180
6181 void wxGrid::SetLabelBackgroundColour( const wxColour& colour )
6182 {
6183 if ( m_labelBackgroundColour != colour )
6184 {
6185 m_labelBackgroundColour = colour;
6186 m_rowLabelWin->SetBackgroundColour( colour );
6187 m_colLabelWin->SetBackgroundColour( colour );
6188 m_cornerLabelWin->SetBackgroundColour( colour );
6189
6190 if ( !GetBatchCount() )
6191 {
6192 m_rowLabelWin->Refresh();
6193 m_colLabelWin->Refresh();
6194 m_cornerLabelWin->Refresh();
6195 }
6196 }
6197 }
6198
6199 void wxGrid::SetLabelTextColour( const wxColour& colour )
6200 {
6201 if ( m_labelTextColour != colour )
6202 {
6203 m_labelTextColour = colour;
6204 if ( !GetBatchCount() )
6205 {
6206 m_rowLabelWin->Refresh();
6207 m_colLabelWin->Refresh();
6208 }
6209 }
6210 }
6211
6212 void wxGrid::SetLabelFont( const wxFont& font )
6213 {
6214 m_labelFont = font;
6215 if ( !GetBatchCount() )
6216 {
6217 m_rowLabelWin->Refresh();
6218 m_colLabelWin->Refresh();
6219 }
6220 }
6221
6222 void wxGrid::SetRowLabelAlignment( int horiz, int vert )
6223 {
6224 if ( horiz == wxLEFT || horiz == wxCENTRE || horiz == wxRIGHT )
6225 {
6226 m_rowLabelHorizAlign = horiz;
6227 }
6228
6229 if ( vert == wxTOP || vert == wxCENTRE || vert == wxBOTTOM )
6230 {
6231 m_rowLabelVertAlign = vert;
6232 }
6233
6234 if ( !GetBatchCount() )
6235 {
6236 m_rowLabelWin->Refresh();
6237 }
6238 }
6239
6240 void wxGrid::SetColLabelAlignment( int horiz, int vert )
6241 {
6242 if ( horiz == wxLEFT || horiz == wxCENTRE || horiz == wxRIGHT )
6243 {
6244 m_colLabelHorizAlign = horiz;
6245 }
6246
6247 if ( vert == wxTOP || vert == wxCENTRE || vert == wxBOTTOM )
6248 {
6249 m_colLabelVertAlign = vert;
6250 }
6251
6252 if ( !GetBatchCount() )
6253 {
6254 m_colLabelWin->Refresh();
6255 }
6256 }
6257
6258 void wxGrid::SetRowLabelValue( int row, const wxString& s )
6259 {
6260 if ( m_table )
6261 {
6262 m_table->SetRowLabelValue( row, s );
6263 if ( !GetBatchCount() )
6264 {
6265 wxRect rect = CellToRect( row, 0);
6266 if ( rect.height > 0 )
6267 {
6268 CalcScrolledPosition(0, rect.y, &rect.x, &rect.y);
6269 rect.x = m_left;
6270 rect.width = m_rowLabelWidth;
6271 m_rowLabelWin->Refresh( TRUE, &rect );
6272 }
6273 }
6274 }
6275 }
6276
6277 void wxGrid::SetColLabelValue( int col, const wxString& s )
6278 {
6279 if ( m_table )
6280 {
6281 m_table->SetColLabelValue( col, s );
6282 if ( !GetBatchCount() )
6283 {
6284 wxRect rect = CellToRect( 0, col );
6285 if ( rect.width > 0 )
6286 {
6287 CalcScrolledPosition(rect.x, 0, &rect.x, &rect.y);
6288 rect.y = m_top;
6289 rect.height = m_colLabelHeight;
6290 m_colLabelWin->Refresh( TRUE, &rect );
6291 }
6292 }
6293 }
6294 }
6295
6296 void wxGrid::SetGridLineColour( const wxColour& colour )
6297 {
6298 if ( m_gridLineColour != colour )
6299 {
6300 m_gridLineColour = colour;
6301
6302 wxClientDC dc( m_gridWin );
6303 PrepareDC( dc );
6304 DrawAllGridLines( dc, wxRegion() );
6305 }
6306 }
6307
6308 void wxGrid::EnableGridLines( bool enable )
6309 {
6310 if ( enable != m_gridLinesEnabled )
6311 {
6312 m_gridLinesEnabled = enable;
6313
6314 if ( !GetBatchCount() )
6315 {
6316 if ( enable )
6317 {
6318 wxClientDC dc( m_gridWin );
6319 PrepareDC( dc );
6320 DrawAllGridLines( dc, wxRegion() );
6321 }
6322 else
6323 {
6324 m_gridWin->Refresh();
6325 }
6326 }
6327 }
6328 }
6329
6330
6331 int wxGrid::GetDefaultRowSize()
6332 {
6333 return m_defaultRowHeight;
6334 }
6335
6336 int wxGrid::GetRowSize( int row )
6337 {
6338 wxCHECK_MSG( row >= 0 && row < m_numRows, 0, _T("invalid row index") );
6339
6340 return GetRowHeight(row);
6341 }
6342
6343 int wxGrid::GetDefaultColSize()
6344 {
6345 return m_defaultColWidth;
6346 }
6347
6348 int wxGrid::GetColSize( int col )
6349 {
6350 wxCHECK_MSG( col >= 0 && col < m_numCols, 0, _T("invalid column index") );
6351
6352 return GetColWidth(col);
6353 }
6354
6355 // ============================================================================
6356 // access to the grid attributes: each of them has a default value in the grid
6357 // itself and may be overidden on a per-cell basis
6358 // ============================================================================
6359
6360 // ----------------------------------------------------------------------------
6361 // setting default attributes
6362 // ----------------------------------------------------------------------------
6363
6364 void wxGrid::SetDefaultCellBackgroundColour( const wxColour& col )
6365 {
6366 m_defaultCellAttr->SetBackgroundColour(col);
6367 #ifdef __WXGTK__
6368 m_gridWin->SetBackgroundColour(col);
6369 #endif
6370 }
6371
6372 void wxGrid::SetDefaultCellTextColour( const wxColour& col )
6373 {
6374 m_defaultCellAttr->SetTextColour(col);
6375 }
6376
6377 void wxGrid::SetDefaultCellAlignment( int horiz, int vert )
6378 {
6379 m_defaultCellAttr->SetAlignment(horiz, vert);
6380 }
6381
6382 void wxGrid::SetDefaultCellFont( const wxFont& font )
6383 {
6384 m_defaultCellAttr->SetFont(font);
6385 }
6386
6387 void wxGrid::SetDefaultRenderer(wxGridCellRenderer *renderer)
6388 {
6389 m_defaultCellAttr->SetRenderer(renderer);
6390 }
6391
6392 void wxGrid::SetDefaultEditor(wxGridCellEditor *editor)
6393 {
6394 m_defaultCellAttr->SetEditor(editor);
6395 }
6396
6397 // ----------------------------------------------------------------------------
6398 // access to the default attrbiutes
6399 // ----------------------------------------------------------------------------
6400
6401 wxColour wxGrid::GetDefaultCellBackgroundColour()
6402 {
6403 return m_defaultCellAttr->GetBackgroundColour();
6404 }
6405
6406 wxColour wxGrid::GetDefaultCellTextColour()
6407 {
6408 return m_defaultCellAttr->GetTextColour();
6409 }
6410
6411 wxFont wxGrid::GetDefaultCellFont()
6412 {
6413 return m_defaultCellAttr->GetFont();
6414 }
6415
6416 void wxGrid::GetDefaultCellAlignment( int *horiz, int *vert )
6417 {
6418 m_defaultCellAttr->GetAlignment(horiz, vert);
6419 }
6420
6421 wxGridCellRenderer *wxGrid::GetDefaultRenderer() const
6422 {
6423 return m_defaultCellAttr->GetRenderer(NULL,0,0);
6424 }
6425
6426 wxGridCellEditor *wxGrid::GetDefaultEditor() const
6427 {
6428 return m_defaultCellAttr->GetEditor(NULL,0,0);
6429 }
6430
6431 // ----------------------------------------------------------------------------
6432 // access to cell attributes
6433 // ----------------------------------------------------------------------------
6434
6435 wxColour wxGrid::GetCellBackgroundColour(int row, int col)
6436 {
6437 wxGridCellAttr *attr = GetCellAttr(row, col);
6438 wxColour colour = attr->GetBackgroundColour();
6439 attr->SafeDecRef();
6440 return colour;
6441 }
6442
6443 wxColour wxGrid::GetCellTextColour( int row, int col )
6444 {
6445 wxGridCellAttr *attr = GetCellAttr(row, col);
6446 wxColour colour = attr->GetTextColour();
6447 attr->SafeDecRef();
6448 return colour;
6449 }
6450
6451 wxFont wxGrid::GetCellFont( int row, int col )
6452 {
6453 wxGridCellAttr *attr = GetCellAttr(row, col);
6454 wxFont font = attr->GetFont();
6455 attr->SafeDecRef();
6456 return font;
6457 }
6458
6459 void wxGrid::GetCellAlignment( int row, int col, int *horiz, int *vert )
6460 {
6461 wxGridCellAttr *attr = GetCellAttr(row, col);
6462 attr->GetAlignment(horiz, vert);
6463 attr->SafeDecRef();
6464 }
6465
6466 wxGridCellRenderer* wxGrid::GetCellRenderer(int row, int col)
6467 {
6468 wxGridCellAttr* attr = GetCellAttr(row, col);
6469 wxGridCellRenderer* renderer = attr->GetRenderer(this, row, col);
6470 attr->DecRef();
6471 return renderer;
6472 }
6473
6474 wxGridCellEditor* wxGrid::GetCellEditor(int row, int col)
6475 {
6476 wxGridCellAttr* attr = GetCellAttr(row, col);
6477 wxGridCellEditor* editor = attr->GetEditor(this, row, col);
6478 attr->DecRef();
6479 return editor;
6480 }
6481
6482 bool wxGrid::IsReadOnly(int row, int col) const
6483 {
6484 wxGridCellAttr* attr = GetCellAttr(row, col);
6485 bool isReadOnly = attr->IsReadOnly();
6486 attr->DecRef();
6487 return isReadOnly;
6488 }
6489
6490 // ----------------------------------------------------------------------------
6491 // attribute support: cache, automatic provider creation, ...
6492 // ----------------------------------------------------------------------------
6493
6494 bool wxGrid::CanHaveAttributes()
6495 {
6496 if ( !m_table )
6497 {
6498 return FALSE;
6499 }
6500
6501 return m_table->CanHaveAttributes();
6502 }
6503
6504 void wxGrid::ClearAttrCache()
6505 {
6506 if ( m_attrCache.row != -1 )
6507 {
6508 m_attrCache.attr->SafeDecRef();
6509 m_attrCache.row = -1;
6510 }
6511 }
6512
6513 void wxGrid::CacheAttr(int row, int col, wxGridCellAttr *attr) const
6514 {
6515 wxGrid *self = (wxGrid *)this; // const_cast
6516
6517 self->ClearAttrCache();
6518 self->m_attrCache.row = row;
6519 self->m_attrCache.col = col;
6520 self->m_attrCache.attr = attr;
6521 attr->SafeIncRef();
6522 }
6523
6524 bool wxGrid::LookupAttr(int row, int col, wxGridCellAttr **attr) const
6525 {
6526 if ( row == m_attrCache.row && col == m_attrCache.col )
6527 {
6528 *attr = m_attrCache.attr;
6529 (*attr)->SafeIncRef();
6530
6531 #ifdef DEBUG_ATTR_CACHE
6532 gs_nAttrCacheHits++;
6533 #endif
6534
6535 return TRUE;
6536 }
6537 else
6538 {
6539 #ifdef DEBUG_ATTR_CACHE
6540 gs_nAttrCacheMisses++;
6541 #endif
6542 return FALSE;
6543 }
6544 }
6545
6546 wxGridCellAttr *wxGrid::GetCellAttr(int row, int col) const
6547 {
6548 wxGridCellAttr *attr;
6549 if ( !LookupAttr(row, col, &attr) )
6550 {
6551 attr = m_table ? m_table->GetAttr(row, col) : (wxGridCellAttr *)NULL;
6552 CacheAttr(row, col, attr);
6553 }
6554 if (attr)
6555 {
6556 attr->SetDefAttr(m_defaultCellAttr);
6557 }
6558 else
6559 {
6560 attr = m_defaultCellAttr;
6561 attr->IncRef();
6562 }
6563
6564 return attr;
6565 }
6566
6567 wxGridCellAttr *wxGrid::GetOrCreateCellAttr(int row, int col) const
6568 {
6569 wxGridCellAttr *attr;
6570 if ( !LookupAttr(row, col, &attr) || !attr )
6571 {
6572 wxASSERT_MSG( m_table,
6573 _T("we may only be called if CanHaveAttributes() "
6574 "returned TRUE and then m_table should be !NULL") );
6575
6576 attr = m_table->GetAttr(row, col);
6577 if ( !attr )
6578 {
6579 attr = new wxGridCellAttr;
6580
6581 // artificially inc the ref count to match DecRef() in caller
6582 attr->IncRef();
6583
6584 m_table->SetAttr(attr, row, col);
6585 }
6586
6587 CacheAttr(row, col, attr);
6588 }
6589 attr->SetDefAttr(m_defaultCellAttr);
6590 return attr;
6591 }
6592
6593 // ----------------------------------------------------------------------------
6594 // setting cell attributes: this is forwarded to the table
6595 // ----------------------------------------------------------------------------
6596
6597 void wxGrid::SetRowAttr(int row, wxGridCellAttr *attr)
6598 {
6599 if ( CanHaveAttributes() )
6600 {
6601 m_table->SetRowAttr(attr, row);
6602 }
6603 else
6604 {
6605 attr->SafeDecRef();
6606 }
6607 }
6608
6609 void wxGrid::SetColAttr(int col, wxGridCellAttr *attr)
6610 {
6611 if ( CanHaveAttributes() )
6612 {
6613 m_table->SetColAttr(attr, col);
6614 }
6615 else
6616 {
6617 attr->SafeDecRef();
6618 }
6619 }
6620
6621 void wxGrid::SetCellBackgroundColour( int row, int col, const wxColour& colour )
6622 {
6623 if ( CanHaveAttributes() )
6624 {
6625 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
6626 attr->SetBackgroundColour(colour);
6627 attr->DecRef();
6628 }
6629 }
6630
6631 void wxGrid::SetCellTextColour( int row, int col, const wxColour& colour )
6632 {
6633 if ( CanHaveAttributes() )
6634 {
6635 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
6636 attr->SetTextColour(colour);
6637 attr->DecRef();
6638 }
6639 }
6640
6641 void wxGrid::SetCellFont( int row, int col, const wxFont& font )
6642 {
6643 if ( CanHaveAttributes() )
6644 {
6645 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
6646 attr->SetFont(font);
6647 attr->DecRef();
6648 }
6649 }
6650
6651 void wxGrid::SetCellAlignment( int row, int col, int horiz, int vert )
6652 {
6653 if ( CanHaveAttributes() )
6654 {
6655 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
6656 attr->SetAlignment(horiz, vert);
6657 attr->DecRef();
6658 }
6659 }
6660
6661 void wxGrid::SetCellRenderer(int row, int col, wxGridCellRenderer *renderer)
6662 {
6663 if ( CanHaveAttributes() )
6664 {
6665 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
6666 attr->SetRenderer(renderer);
6667 attr->DecRef();
6668 }
6669 }
6670
6671 void wxGrid::SetCellEditor(int row, int col, wxGridCellEditor* editor)
6672 {
6673 if ( CanHaveAttributes() )
6674 {
6675 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
6676 attr->SetEditor(editor);
6677 attr->DecRef();
6678 }
6679 }
6680
6681 void wxGrid::SetReadOnly(int row, int col, bool isReadOnly)
6682 {
6683 if ( CanHaveAttributes() )
6684 {
6685 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
6686 attr->SetReadOnly(isReadOnly);
6687 attr->DecRef();
6688 }
6689 }
6690
6691 // ----------------------------------------------------------------------------
6692 // Data type registration
6693 // ----------------------------------------------------------------------------
6694
6695 void wxGrid::RegisterDataType(const wxString& typeName,
6696 wxGridCellRenderer* renderer,
6697 wxGridCellEditor* editor)
6698 {
6699 m_typeRegistry->RegisterDataType(typeName, renderer, editor);
6700 }
6701
6702
6703 wxGridCellEditor* wxGrid::GetDefaultEditorForCell(int row, int col) const
6704 {
6705 wxString typeName = m_table->GetTypeName(row, col);
6706 return GetDefaultEditorForType(typeName);
6707 }
6708
6709 wxGridCellRenderer* wxGrid::GetDefaultRendererForCell(int row, int col) const
6710 {
6711 wxString typeName = m_table->GetTypeName(row, col);
6712 return GetDefaultRendererForType(typeName);
6713 }
6714
6715 wxGridCellEditor*
6716 wxGrid::GetDefaultEditorForType(const wxString& typeName) const
6717 {
6718 int index = m_typeRegistry->FindDataType(typeName);
6719 if (index == -1) {
6720 // Should we force the failure here or let it fallback to string handling???
6721 // wxFAIL_MSG(wxT("Unknown data type name"));
6722 return NULL;
6723 }
6724 return m_typeRegistry->GetEditor(index);
6725 }
6726
6727 wxGridCellRenderer*
6728 wxGrid::GetDefaultRendererForType(const wxString& typeName) const
6729 {
6730 int index = m_typeRegistry->FindDataType(typeName);
6731 if (index == -1) {
6732 // Should we force the failure here or let it fallback to string handling???
6733 // wxFAIL_MSG(wxT("Unknown data type name"));
6734 return NULL;
6735 }
6736 return m_typeRegistry->GetRenderer(index);
6737 }
6738
6739
6740 // ----------------------------------------------------------------------------
6741 // row/col size
6742 // ----------------------------------------------------------------------------
6743
6744 void wxGrid::EnableDragRowSize( bool enable )
6745 {
6746 m_canDragRowSize = enable;
6747 }
6748
6749
6750 void wxGrid::EnableDragColSize( bool enable )
6751 {
6752 m_canDragColSize = enable;
6753 }
6754
6755 void wxGrid::EnableDragGridSize( bool enable )
6756 {
6757 m_canDragGridSize = enable;
6758 }
6759
6760
6761 void wxGrid::SetDefaultRowSize( int height, bool resizeExistingRows )
6762 {
6763 m_defaultRowHeight = wxMax( height, WXGRID_MIN_ROW_HEIGHT );
6764
6765 if ( resizeExistingRows )
6766 {
6767 InitRowHeights();
6768
6769 CalcDimensions();
6770 }
6771 }
6772
6773 void wxGrid::SetRowSize( int row, int height )
6774 {
6775 wxCHECK_RET( row >= 0 && row < m_numRows, _T("invalid row index") );
6776
6777 if ( m_rowHeights.IsEmpty() )
6778 {
6779 // need to really create the array
6780 InitRowHeights();
6781 }
6782
6783 int h = wxMax( 0, height );
6784 int diff = h - m_rowHeights[row];
6785
6786 m_rowHeights[row] = h;
6787 int i;
6788 for ( i = row; i < m_numRows; i++ )
6789 {
6790 m_rowBottoms[i] += diff;
6791 }
6792 CalcDimensions();
6793 }
6794
6795 void wxGrid::SetDefaultColSize( int width, bool resizeExistingCols )
6796 {
6797 m_defaultColWidth = wxMax( width, WXGRID_MIN_COL_WIDTH );
6798
6799 if ( resizeExistingCols )
6800 {
6801 InitColWidths();
6802
6803 CalcDimensions();
6804 }
6805 }
6806
6807 void wxGrid::SetColSize( int col, int width )
6808 {
6809 wxCHECK_RET( col >= 0 && col < m_numCols, _T("invalid column index") );
6810
6811 // should we check that it's bigger than GetColMinimalWidth(col) here?
6812
6813 if ( m_colWidths.IsEmpty() )
6814 {
6815 // need to really create the array
6816 InitColWidths();
6817 }
6818
6819 int w = wxMax( 0, width );
6820 int diff = w - m_colWidths[col];
6821 m_colWidths[col] = w;
6822
6823 int i;
6824 for ( i = col; i < m_numCols; i++ )
6825 {
6826 m_colRights[i] += diff;
6827 }
6828 CalcDimensions();
6829 }
6830
6831
6832 void wxGrid::SetColMinimalWidth( int col, int width )
6833 {
6834 m_colMinWidths.Put(col, (wxObject *)width);
6835 }
6836
6837 int wxGrid::GetColMinimalWidth(int col) const
6838 {
6839 wxObject *obj = m_colMinWidths.Get(m_dragRowOrCol);
6840 return obj ? (int)obj : WXGRID_MIN_COL_WIDTH;
6841 }
6842
6843 void wxGrid::AutoSizeColumn( int col, bool setAsMin )
6844 {
6845 wxClientDC dc(m_gridWin);
6846
6847 wxCoord width, widthMax = 0;
6848 for ( int row = 0; row < m_numRows; row++ )
6849 {
6850 wxGridCellAttr* attr = GetCellAttr(row, col);
6851 wxGridCellRenderer* renderer = attr->GetRenderer(this, row, col);
6852 if ( renderer )
6853 {
6854 width = renderer->GetBestSize(*this, *attr, dc, row, col).x;
6855 if ( width > widthMax )
6856 {
6857 widthMax = width;
6858 }
6859 }
6860
6861 attr->DecRef();
6862 }
6863
6864 // now also compare with the column label width
6865 dc.SetFont( GetLabelFont() );
6866 dc.GetTextExtent( GetColLabelValue(col), &width, NULL );
6867 if ( width > widthMax )
6868 {
6869 widthMax = width;
6870 }
6871
6872 if ( !widthMax )
6873 {
6874 // empty column - give default width (notice that if widthMax is less
6875 // than default width but != 0, it's ok)
6876 widthMax = m_defaultColWidth;
6877 }
6878 else
6879 {
6880 // leave some space around text
6881 widthMax += 10;
6882 }
6883
6884 SetColSize(col, widthMax);
6885 if ( setAsMin )
6886 {
6887 SetColMinimalWidth(col, widthMax);
6888 }
6889 }
6890
6891 void wxGrid::AutoSizeColumns( bool setAsMin )
6892 {
6893 for ( int col = 0; col < m_numCols; col++ )
6894 {
6895 AutoSizeColumn(col, setAsMin);
6896 }
6897 }
6898
6899 //
6900 // ------ cell value accessor functions
6901 //
6902
6903 void wxGrid::SetCellValue( int row, int col, const wxString& s )
6904 {
6905 if ( m_table )
6906 {
6907 m_table->SetValue( row, col, s.c_str() );
6908 if ( !GetBatchCount() )
6909 {
6910 wxClientDC dc( m_gridWin );
6911 PrepareDC( dc );
6912 DrawCell( dc, wxGridCellCoords(row, col) );
6913 }
6914
6915 if ( m_currentCellCoords.GetRow() == row &&
6916 m_currentCellCoords.GetCol() == col &&
6917 IsCellEditControlEnabled())
6918 {
6919 HideCellEditControl();
6920 ShowCellEditControl(); // will reread data from table
6921 }
6922 }
6923 }
6924
6925
6926 //
6927 // ------ Block, row and col selection
6928 //
6929
6930 void wxGrid::SelectRow( int row, bool addToSelected )
6931 {
6932 wxRect r;
6933
6934 if ( IsSelection() && addToSelected )
6935 {
6936 wxRect rect[4];
6937 bool need_refresh[4];
6938 need_refresh[0] =
6939 need_refresh[1] =
6940 need_refresh[2] =
6941 need_refresh[3] = FALSE;
6942
6943 int i;
6944
6945 wxCoord oldLeft = m_selectedTopLeft.GetCol();
6946 wxCoord oldTop = m_selectedTopLeft.GetRow();
6947 wxCoord oldRight = m_selectedBottomRight.GetCol();
6948 wxCoord oldBottom = m_selectedBottomRight.GetRow();
6949
6950 if ( oldTop > row )
6951 {
6952 need_refresh[0] = TRUE;
6953 rect[0] = BlockToDeviceRect( wxGridCellCoords ( row, 0 ),
6954 wxGridCellCoords ( oldTop - 1,
6955 m_numCols - 1 ) );
6956 m_selectedTopLeft.SetRow( row );
6957 }
6958
6959 if ( oldLeft > 0 )
6960 {
6961 need_refresh[1] = TRUE;
6962 rect[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop, 0 ),
6963 wxGridCellCoords ( oldBottom,
6964 oldLeft - 1 ) );
6965
6966 m_selectedTopLeft.SetCol( 0 );
6967 }
6968
6969 if ( oldBottom < row )
6970 {
6971 need_refresh[2] = TRUE;
6972 rect[2] = BlockToDeviceRect( wxGridCellCoords ( oldBottom + 1, 0 ),
6973 wxGridCellCoords ( row,
6974 m_numCols - 1 ) );
6975 m_selectedBottomRight.SetRow( row );
6976 }
6977
6978 if ( oldRight < m_numCols - 1 )
6979 {
6980 need_refresh[3] = TRUE;
6981 rect[3] = BlockToDeviceRect( wxGridCellCoords ( oldTop ,
6982 oldRight + 1 ),
6983 wxGridCellCoords ( oldBottom,
6984 m_numCols - 1 ) );
6985 m_selectedBottomRight.SetCol( m_numCols - 1 );
6986 }
6987
6988 for (i = 0; i < 4; i++ )
6989 if ( need_refresh[i] && rect[i] != wxGridNoCellRect )
6990 m_gridWin->Refresh( FALSE, &(rect[i]) );
6991 }
6992 else
6993 {
6994 r = SelectionToDeviceRect();
6995 ClearSelection();
6996 if ( r != wxGridNoCellRect ) m_gridWin->Refresh( FALSE, &r );
6997
6998 m_selectedTopLeft.Set( row, 0 );
6999 m_selectedBottomRight.Set( row, m_numCols-1 );
7000 r = SelectionToDeviceRect();
7001 m_gridWin->Refresh( FALSE, &r );
7002 }
7003
7004 wxGridRangeSelectEvent gridEvt( GetId(),
7005 wxEVT_GRID_RANGE_SELECT,
7006 this,
7007 m_selectedTopLeft,
7008 m_selectedBottomRight );
7009
7010 GetEventHandler()->ProcessEvent(gridEvt);
7011 }
7012
7013
7014 void wxGrid::SelectCol( int col, bool addToSelected )
7015 {
7016 if ( IsSelection() && addToSelected )
7017 {
7018 wxRect rect[4];
7019 bool need_refresh[4];
7020 need_refresh[0] =
7021 need_refresh[1] =
7022 need_refresh[2] =
7023 need_refresh[3] = FALSE;
7024 int i;
7025
7026 wxCoord oldLeft = m_selectedTopLeft.GetCol();
7027 wxCoord oldTop = m_selectedTopLeft.GetRow();
7028 wxCoord oldRight = m_selectedBottomRight.GetCol();
7029 wxCoord oldBottom = m_selectedBottomRight.GetRow();
7030
7031 if ( oldLeft > col )
7032 {
7033 need_refresh[0] = TRUE;
7034 rect[0] = BlockToDeviceRect( wxGridCellCoords ( 0, col ),
7035 wxGridCellCoords ( m_numRows - 1,
7036 oldLeft - 1 ) );
7037 m_selectedTopLeft.SetCol( col );
7038 }
7039
7040 if ( oldTop > 0 )
7041 {
7042 need_refresh[1] = TRUE;
7043 rect[1] = BlockToDeviceRect( wxGridCellCoords ( 0, oldLeft ),
7044 wxGridCellCoords ( oldTop - 1,
7045 oldRight ) );
7046 m_selectedTopLeft.SetRow( 0 );
7047 }
7048
7049 if ( oldRight < col )
7050 {
7051 need_refresh[2] = TRUE;
7052 rect[2] = BlockToDeviceRect( wxGridCellCoords ( 0, oldRight + 1 ),
7053 wxGridCellCoords ( m_numRows - 1,
7054 col ) );
7055 m_selectedBottomRight.SetCol( col );
7056 }
7057
7058 if ( oldBottom < m_numRows - 1 )
7059 {
7060 need_refresh[3] = TRUE;
7061 rect[3] = BlockToDeviceRect( wxGridCellCoords ( oldBottom + 1,
7062 oldLeft ),
7063 wxGridCellCoords ( m_numRows - 1,
7064 oldRight ) );
7065 m_selectedBottomRight.SetRow( m_numRows - 1 );
7066 }
7067
7068 for (i = 0; i < 4; i++ )
7069 if ( need_refresh[i] && rect[i] != wxGridNoCellRect )
7070 m_gridWin->Refresh( FALSE, &(rect[i]) );
7071 }
7072 else
7073 {
7074 wxRect r;
7075
7076 r = SelectionToDeviceRect();
7077 ClearSelection();
7078 if ( r != wxGridNoCellRect ) m_gridWin->Refresh( FALSE, &r );
7079
7080 m_selectedTopLeft.Set( 0, col );
7081 m_selectedBottomRight.Set( m_numRows-1, col );
7082 r = SelectionToDeviceRect();
7083 m_gridWin->Refresh( FALSE, &r );
7084 }
7085
7086 wxGridRangeSelectEvent gridEvt( GetId(),
7087 wxEVT_GRID_RANGE_SELECT,
7088 this,
7089 m_selectedTopLeft,
7090 m_selectedBottomRight );
7091
7092 GetEventHandler()->ProcessEvent(gridEvt);
7093 }
7094
7095
7096 void wxGrid::SelectBlock( int topRow, int leftCol, int bottomRow, int rightCol )
7097 {
7098 int temp;
7099 wxGridCellCoords updateTopLeft, updateBottomRight;
7100
7101 if ( topRow > bottomRow )
7102 {
7103 temp = topRow;
7104 topRow = bottomRow;
7105 bottomRow = temp;
7106 }
7107
7108 if ( leftCol > rightCol )
7109 {
7110 temp = leftCol;
7111 leftCol = rightCol;
7112 rightCol = temp;
7113 }
7114
7115 updateTopLeft = wxGridCellCoords( topRow, leftCol );
7116 updateBottomRight = wxGridCellCoords( bottomRow, rightCol );
7117
7118 if ( m_selectedTopLeft != updateTopLeft ||
7119 m_selectedBottomRight != updateBottomRight )
7120 {
7121 // Compute two optimal update rectangles:
7122 // Either one rectangle is a real subset of the
7123 // other, or they are (almost) disjoint!
7124 wxRect rect[4];
7125 bool need_refresh[4];
7126 need_refresh[0] =
7127 need_refresh[1] =
7128 need_refresh[2] =
7129 need_refresh[3] = FALSE;
7130 int i;
7131
7132 // Store intermediate values
7133 wxCoord oldLeft = m_selectedTopLeft.GetCol();
7134 wxCoord oldTop = m_selectedTopLeft.GetRow();
7135 wxCoord oldRight = m_selectedBottomRight.GetCol();
7136 wxCoord oldBottom = m_selectedBottomRight.GetRow();
7137
7138 // Determine the outer/inner coordinates.
7139 if (oldLeft > leftCol)
7140 {
7141 temp = oldLeft;
7142 oldLeft = leftCol;
7143 leftCol = temp;
7144 }
7145 if (oldTop > topRow )
7146 {
7147 temp = oldTop;
7148 oldTop = topRow;
7149 topRow = temp;
7150 }
7151 if (oldRight < rightCol )
7152 {
7153 temp = oldRight;
7154 oldRight = rightCol;
7155 rightCol = temp;
7156 }
7157 if (oldBottom < bottomRow)
7158 {
7159 temp = oldBottom;
7160 oldBottom = bottomRow;
7161 bottomRow = temp;
7162 }
7163
7164 // Now, either the stuff marked old is the outer
7165 // rectangle or we don't have a situation where one
7166 // is contained in the other.
7167
7168 if ( oldLeft < leftCol )
7169 {
7170 need_refresh[0] = TRUE;
7171 rect[0] = BlockToDeviceRect( wxGridCellCoords ( oldTop,
7172 oldLeft ),
7173 wxGridCellCoords ( oldBottom,
7174 leftCol - 1 ) );
7175 }
7176
7177 if ( oldTop < topRow )
7178 {
7179 need_refresh[1] = TRUE;
7180 rect[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop,
7181 leftCol ),
7182 wxGridCellCoords ( topRow - 1,
7183 rightCol ) );
7184 }
7185
7186 if ( oldRight > rightCol )
7187 {
7188 need_refresh[2] = TRUE;
7189 rect[2] = BlockToDeviceRect( wxGridCellCoords ( oldTop,
7190 rightCol + 1 ),
7191 wxGridCellCoords ( oldBottom,
7192 oldRight ) );
7193 }
7194
7195 if ( oldBottom > bottomRow )
7196 {
7197 need_refresh[3] = TRUE;
7198 rect[3] = BlockToDeviceRect( wxGridCellCoords ( bottomRow + 1,
7199 leftCol ),
7200 wxGridCellCoords ( oldBottom,
7201 rightCol ) );
7202 }
7203
7204
7205 // Change Selection
7206 m_selectedTopLeft = updateTopLeft;
7207 m_selectedBottomRight = updateBottomRight;
7208
7209 // various Refresh() calls
7210 for (i = 0; i < 4; i++ )
7211 if ( need_refresh[i] && rect[i] != wxGridNoCellRect )
7212 m_gridWin->Refresh( FALSE, &(rect[i]) );
7213 }
7214
7215 // only generate an event if the block is not being selected by
7216 // dragging the mouse (in which case the event will be generated in
7217 // the mouse event handler)
7218 if ( !m_isDragging )
7219 {
7220 wxGridRangeSelectEvent gridEvt( GetId(),
7221 wxEVT_GRID_RANGE_SELECT,
7222 this,
7223 m_selectedTopLeft,
7224 m_selectedBottomRight );
7225
7226 GetEventHandler()->ProcessEvent(gridEvt);
7227 }
7228 }
7229
7230 void wxGrid::SelectAll()
7231 {
7232 m_selectedTopLeft.Set( 0, 0 );
7233 m_selectedBottomRight.Set( m_numRows-1, m_numCols-1 );
7234
7235 m_gridWin->Refresh();
7236 }
7237
7238
7239 void wxGrid::ClearSelection()
7240 {
7241 m_selectedTopLeft = wxGridNoCellCoords;
7242 m_selectedBottomRight = wxGridNoCellCoords;
7243 }
7244
7245
7246 // This function returns the rectangle that encloses the given block
7247 // in device coords clipped to the client size of the grid window.
7248 //
7249 wxRect wxGrid::BlockToDeviceRect( const wxGridCellCoords &topLeft,
7250 const wxGridCellCoords &bottomRight )
7251 {
7252 wxRect rect( wxGridNoCellRect );
7253 wxRect cellRect;
7254
7255 cellRect = CellToRect( topLeft );
7256 if ( cellRect != wxGridNoCellRect )
7257 {
7258 rect = cellRect;
7259 }
7260 else
7261 {
7262 rect = wxRect( 0, 0, 0, 0 );
7263 }
7264
7265 cellRect = CellToRect( bottomRight );
7266 if ( cellRect != wxGridNoCellRect )
7267 {
7268 rect += cellRect;
7269 }
7270 else
7271 {
7272 return wxGridNoCellRect;
7273 }
7274
7275 // convert to scrolled coords
7276 //
7277 int left, top, right, bottom;
7278 CalcScrolledPosition( rect.GetLeft(), rect.GetTop(), &left, &top );
7279 CalcScrolledPosition( rect.GetRight(), rect.GetBottom(), &right, &bottom );
7280
7281 int cw, ch;
7282 m_gridWin->GetClientSize( &cw, &ch );
7283
7284 rect.SetLeft( wxMax(0, left) );
7285 rect.SetTop( wxMax(0, top) );
7286 rect.SetRight( wxMin(cw, right) );
7287 rect.SetBottom( wxMin(ch, bottom) );
7288
7289 return rect;
7290 }
7291
7292
7293
7294 //
7295 // ------ Grid event classes
7296 //
7297
7298 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent, wxEvent )
7299
7300 wxGridEvent::wxGridEvent( int id, wxEventType type, wxObject* obj,
7301 int row, int col, int x, int y,
7302 bool control, bool shift, bool alt, bool meta )
7303 : wxNotifyEvent( type, id )
7304 {
7305 m_row = row;
7306 m_col = col;
7307 m_x = x;
7308 m_y = y;
7309 m_control = control;
7310 m_shift = shift;
7311 m_alt = alt;
7312 m_meta = meta;
7313
7314 SetEventObject(obj);
7315 }
7316
7317
7318 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent, wxEvent )
7319
7320 wxGridSizeEvent::wxGridSizeEvent( int id, wxEventType type, wxObject* obj,
7321 int rowOrCol, int x, int y,
7322 bool control, bool shift, bool alt, bool meta )
7323 : wxNotifyEvent( type, id )
7324 {
7325 m_rowOrCol = rowOrCol;
7326 m_x = x;
7327 m_y = y;
7328 m_control = control;
7329 m_shift = shift;
7330 m_alt = alt;
7331 m_meta = meta;
7332
7333 SetEventObject(obj);
7334 }
7335
7336
7337 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent, wxEvent )
7338
7339 wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id, wxEventType type, wxObject* obj,
7340 const wxGridCellCoords& topLeft,
7341 const wxGridCellCoords& bottomRight,
7342 bool control, bool shift, bool alt, bool meta )
7343 : wxNotifyEvent( type, id )
7344 {
7345 m_topLeft = topLeft;
7346 m_bottomRight = bottomRight;
7347 m_control = control;
7348 m_shift = shift;
7349 m_alt = alt;
7350 m_meta = meta;
7351
7352 SetEventObject(obj);
7353 }
7354
7355
7356 #endif // ifndef wxUSE_NEW_GRID
7357