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