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