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