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