]> git.saurik.com Git - wxWidgets.git/blob - src/generic/grid.cpp
F2 enables edit in place
[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
2948 // ----------------------------------------------------------------------------
2949 // the idea is to call these functions only when necessary because they create
2950 // quite big arrays which eat memory mostly unnecessary - in particular, if
2951 // default widths/heights are used for all rows/columns, we may not use these
2952 // arrays at all
2953 //
2954 // with some extra code, it should be possible to only store the
2955 // widths/heights different from default ones but this will be done later...
2956 // ----------------------------------------------------------------------------
2957
2958 void wxGrid::InitRowHeights()
2959 {
2960 m_rowHeights.Empty();
2961 m_rowBottoms.Empty();
2962
2963 m_rowHeights.Alloc( m_numRows );
2964 m_rowBottoms.Alloc( m_numRows );
2965
2966 int rowBottom = 0;
2967 for ( int i = 0; i < m_numRows; i++ )
2968 {
2969 m_rowHeights.Add( m_defaultRowHeight );
2970 rowBottom += m_defaultRowHeight;
2971 m_rowBottoms.Add( rowBottom );
2972 }
2973 }
2974
2975 void wxGrid::InitColWidths()
2976 {
2977 m_colWidths.Empty();
2978 m_colRights.Empty();
2979
2980 m_colWidths.Alloc( m_numCols );
2981 m_colRights.Alloc( m_numCols );
2982 int colRight = 0;
2983 for ( int i = 0; i < m_numCols; i++ )
2984 {
2985 m_colWidths.Add( m_defaultColWidth );
2986 colRight += m_defaultColWidth;
2987 m_colRights.Add( colRight );
2988 }
2989 }
2990
2991 int wxGrid::GetColWidth(int col) const
2992 {
2993 return m_colWidths.IsEmpty() ? m_defaultColWidth : m_colWidths[col];
2994 }
2995
2996 int wxGrid::GetColLeft(int col) const
2997 {
2998 return m_colRights.IsEmpty() ? col * m_defaultColWidth
2999 : m_colRights[col] - m_colWidths[col];
3000 }
3001
3002 int wxGrid::GetColRight(int col) const
3003 {
3004 return m_colRights.IsEmpty() ? (col + 1) * m_defaultColWidth
3005 : m_colRights[col];
3006 }
3007
3008 int wxGrid::GetRowHeight(int row) const
3009 {
3010 return m_rowHeights.IsEmpty() ? m_defaultRowHeight : m_rowHeights[row];
3011 }
3012
3013 int wxGrid::GetRowTop(int row) const
3014 {
3015 return m_rowBottoms.IsEmpty() ? row * m_defaultRowHeight
3016 : m_rowBottoms[row] - m_rowHeights[row];
3017 }
3018
3019 int wxGrid::GetRowBottom(int row) const
3020 {
3021 return m_rowBottoms.IsEmpty() ? (row + 1) * m_defaultRowHeight
3022 : m_rowBottoms[row];
3023 }
3024
3025 void wxGrid::CalcDimensions()
3026 {
3027 int cw, ch;
3028 GetClientSize( &cw, &ch );
3029
3030 if ( m_numRows > 0 && m_numCols > 0 )
3031 {
3032 int right = GetColRight( m_numCols-1 ) + 50;
3033 int bottom = GetRowBottom( m_numRows-1 ) + 50;
3034
3035 // TODO: restore the scroll position that we had before sizing
3036 //
3037 int x, y;
3038 GetViewStart( &x, &y );
3039 SetScrollbars( GRID_SCROLL_LINE, GRID_SCROLL_LINE,
3040 right/GRID_SCROLL_LINE, bottom/GRID_SCROLL_LINE,
3041 x, y );
3042 }
3043 }
3044
3045
3046 void wxGrid::CalcWindowSizes()
3047 {
3048 int cw, ch;
3049 GetClientSize( &cw, &ch );
3050
3051 if ( m_cornerLabelWin->IsShown() )
3052 m_cornerLabelWin->SetSize( 0, 0, m_rowLabelWidth, m_colLabelHeight );
3053
3054 if ( m_colLabelWin->IsShown() )
3055 m_colLabelWin->SetSize( m_rowLabelWidth, 0, cw-m_rowLabelWidth, m_colLabelHeight);
3056
3057 if ( m_rowLabelWin->IsShown() )
3058 m_rowLabelWin->SetSize( 0, m_colLabelHeight, m_rowLabelWidth, ch-m_colLabelHeight);
3059
3060 if ( m_gridWin->IsShown() )
3061 m_gridWin->SetSize( m_rowLabelWidth, m_colLabelHeight, cw-m_rowLabelWidth, ch-m_colLabelHeight);
3062 }
3063
3064
3065 // this is called when the grid table sends a message to say that it
3066 // has been redimensioned
3067 //
3068 bool wxGrid::Redimension( wxGridTableMessage& msg )
3069 {
3070 int i;
3071
3072 // if we were using the default widths/heights so far, we must change them
3073 // now
3074 if ( m_colWidths.IsEmpty() )
3075 {
3076 InitColWidths();
3077 }
3078
3079 if ( m_rowHeights.IsEmpty() )
3080 {
3081 InitRowHeights();
3082 }
3083
3084 switch ( msg.GetId() )
3085 {
3086 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED:
3087 {
3088 size_t pos = msg.GetCommandInt();
3089 int numRows = msg.GetCommandInt2();
3090 for ( i = 0; i < numRows; i++ )
3091 {
3092 m_rowHeights.Insert( m_defaultRowHeight, pos );
3093 m_rowBottoms.Insert( 0, pos );
3094 }
3095 m_numRows += numRows;
3096
3097 int bottom = 0;
3098 if ( pos > 0 ) bottom = m_rowBottoms[pos-1];
3099
3100 for ( i = pos; i < m_numRows; i++ )
3101 {
3102 bottom += m_rowHeights[i];
3103 m_rowBottoms[i] = bottom;
3104 }
3105 CalcDimensions();
3106 }
3107 return TRUE;
3108
3109 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED:
3110 {
3111 int numRows = msg.GetCommandInt();
3112 for ( i = 0; i < numRows; i++ )
3113 {
3114 m_rowHeights.Add( m_defaultRowHeight );
3115 m_rowBottoms.Add( 0 );
3116 }
3117
3118 int oldNumRows = m_numRows;
3119 m_numRows += numRows;
3120
3121 int bottom = 0;
3122 if ( oldNumRows > 0 ) bottom = m_rowBottoms[oldNumRows-1];
3123
3124 for ( i = oldNumRows; i < m_numRows; i++ )
3125 {
3126 bottom += m_rowHeights[i];
3127 m_rowBottoms[i] = bottom;
3128 }
3129 CalcDimensions();
3130 }
3131 return TRUE;
3132
3133 case wxGRIDTABLE_NOTIFY_ROWS_DELETED:
3134 {
3135 size_t pos = msg.GetCommandInt();
3136 int numRows = msg.GetCommandInt2();
3137 for ( i = 0; i < numRows; i++ )
3138 {
3139 m_rowHeights.Remove( pos );
3140 m_rowBottoms.Remove( pos );
3141 }
3142 m_numRows -= numRows;
3143
3144 if ( !m_numRows )
3145 {
3146 m_numCols = 0;
3147 m_colWidths.Clear();
3148 m_colRights.Clear();
3149 m_currentCellCoords = wxGridNoCellCoords;
3150 }
3151 else
3152 {
3153 if ( m_currentCellCoords.GetRow() >= m_numRows )
3154 m_currentCellCoords.Set( 0, 0 );
3155
3156 int h = 0;
3157 for ( i = 0; i < m_numRows; i++ )
3158 {
3159 h += m_rowHeights[i];
3160 m_rowBottoms[i] = h;
3161 }
3162 }
3163
3164 CalcDimensions();
3165 }
3166 return TRUE;
3167
3168 case wxGRIDTABLE_NOTIFY_COLS_INSERTED:
3169 {
3170 size_t pos = msg.GetCommandInt();
3171 int numCols = msg.GetCommandInt2();
3172 for ( i = 0; i < numCols; i++ )
3173 {
3174 m_colWidths.Insert( m_defaultColWidth, pos );
3175 m_colRights.Insert( 0, pos );
3176 }
3177 m_numCols += numCols;
3178
3179 int right = 0;
3180 if ( pos > 0 ) right = m_colRights[pos-1];
3181
3182 for ( i = pos; i < m_numCols; i++ )
3183 {
3184 right += m_colWidths[i];
3185 m_colRights[i] = right;
3186 }
3187 CalcDimensions();
3188 }
3189 return TRUE;
3190
3191 case wxGRIDTABLE_NOTIFY_COLS_APPENDED:
3192 {
3193 int numCols = msg.GetCommandInt();
3194 for ( i = 0; i < numCols; i++ )
3195 {
3196 m_colWidths.Add( m_defaultColWidth );
3197 m_colRights.Add( 0 );
3198 }
3199
3200 int oldNumCols = m_numCols;
3201 m_numCols += numCols;
3202
3203 int right = 0;
3204 if ( oldNumCols > 0 ) right = m_colRights[oldNumCols-1];
3205
3206 for ( i = oldNumCols; i < m_numCols; i++ )
3207 {
3208 right += m_colWidths[i];
3209 m_colRights[i] = right;
3210 }
3211 CalcDimensions();
3212 }
3213 return TRUE;
3214
3215 case wxGRIDTABLE_NOTIFY_COLS_DELETED:
3216 {
3217 size_t pos = msg.GetCommandInt();
3218 int numCols = msg.GetCommandInt2();
3219 for ( i = 0; i < numCols; i++ )
3220 {
3221 m_colWidths.Remove( pos );
3222 m_colRights.Remove( pos );
3223 }
3224 m_numCols -= numCols;
3225
3226 if ( !m_numCols )
3227 {
3228 #if 0 // leave the row alone here so that AppendCols will work subsequently
3229 m_numRows = 0;
3230 m_rowHeights.Clear();
3231 m_rowBottoms.Clear();
3232 #endif
3233 m_currentCellCoords = wxGridNoCellCoords;
3234 }
3235 else
3236 {
3237 if ( m_currentCellCoords.GetCol() >= m_numCols )
3238 m_currentCellCoords.Set( 0, 0 );
3239
3240 int w = 0;
3241 for ( i = 0; i < m_numCols; i++ )
3242 {
3243 w += m_colWidths[i];
3244 m_colRights[i] = w;
3245 }
3246 }
3247 CalcDimensions();
3248 }
3249 return TRUE;
3250 }
3251
3252 return FALSE;
3253 }
3254
3255
3256 void wxGrid::CalcRowLabelsExposed( wxRegion& reg )
3257 {
3258 wxRegionIterator iter( reg );
3259 wxRect r;
3260
3261 m_rowLabelsExposed.Empty();
3262
3263 int top, bottom;
3264 while ( iter )
3265 {
3266 r = iter.GetRect();
3267
3268 // TODO: remove this when we can...
3269 // There is a bug in wxMotif that gives garbage update
3270 // rectangles if you jump-scroll a long way by clicking the
3271 // scrollbar with middle button. This is a work-around
3272 //
3273 #if defined(__WXMOTIF__)
3274 int cw, ch;
3275 m_gridWin->GetClientSize( &cw, &ch );
3276 if ( r.GetTop() > ch ) r.SetTop( 0 );
3277 r.SetBottom( wxMin( r.GetBottom(), ch ) );
3278 #endif
3279
3280 // logical bounds of update region
3281 //
3282 int dummy;
3283 CalcUnscrolledPosition( 0, r.GetTop(), &dummy, &top );
3284 CalcUnscrolledPosition( 0, r.GetBottom(), &dummy, &bottom );
3285
3286 // find the row labels within these bounds
3287 //
3288 int row;
3289 for ( row = 0; row < m_numRows; row++ )
3290 {
3291 if ( GetRowBottom(row) < top )
3292 continue;
3293
3294 if ( GetRowTop(row) > bottom )
3295 break;
3296
3297 m_rowLabelsExposed.Add( row );
3298 }
3299
3300 iter++ ;
3301 }
3302 }
3303
3304
3305 void wxGrid::CalcColLabelsExposed( wxRegion& reg )
3306 {
3307 wxRegionIterator iter( reg );
3308 wxRect r;
3309
3310 m_colLabelsExposed.Empty();
3311
3312 int left, right;
3313 while ( iter )
3314 {
3315 r = iter.GetRect();
3316
3317 // TODO: remove this when we can...
3318 // There is a bug in wxMotif that gives garbage update
3319 // rectangles if you jump-scroll a long way by clicking the
3320 // scrollbar with middle button. This is a work-around
3321 //
3322 #if defined(__WXMOTIF__)
3323 int cw, ch;
3324 m_gridWin->GetClientSize( &cw, &ch );
3325 if ( r.GetLeft() > cw ) r.SetLeft( 0 );
3326 r.SetRight( wxMin( r.GetRight(), cw ) );
3327 #endif
3328
3329 // logical bounds of update region
3330 //
3331 int dummy;
3332 CalcUnscrolledPosition( r.GetLeft(), 0, &left, &dummy );
3333 CalcUnscrolledPosition( r.GetRight(), 0, &right, &dummy );
3334
3335 // find the cells within these bounds
3336 //
3337 int col;
3338 for ( col = 0; col < m_numCols; col++ )
3339 {
3340 if ( GetColRight(col) < left )
3341 continue;
3342
3343 if ( GetColLeft(col) > right )
3344 break;
3345
3346 m_colLabelsExposed.Add( col );
3347 }
3348
3349 iter++ ;
3350 }
3351 }
3352
3353
3354 void wxGrid::CalcCellsExposed( wxRegion& reg )
3355 {
3356 wxRegionIterator iter( reg );
3357 wxRect r;
3358
3359 m_cellsExposed.Empty();
3360 m_rowsExposed.Empty();
3361 m_colsExposed.Empty();
3362
3363 int left, top, right, bottom;
3364 while ( iter )
3365 {
3366 r = iter.GetRect();
3367
3368 // TODO: remove this when we can...
3369 // There is a bug in wxMotif that gives garbage update
3370 // rectangles if you jump-scroll a long way by clicking the
3371 // scrollbar with middle button. This is a work-around
3372 //
3373 #if defined(__WXMOTIF__)
3374 int cw, ch;
3375 m_gridWin->GetClientSize( &cw, &ch );
3376 if ( r.GetTop() > ch ) r.SetTop( 0 );
3377 if ( r.GetLeft() > cw ) r.SetLeft( 0 );
3378 r.SetRight( wxMin( r.GetRight(), cw ) );
3379 r.SetBottom( wxMin( r.GetBottom(), ch ) );
3380 #endif
3381
3382 // logical bounds of update region
3383 //
3384 CalcUnscrolledPosition( r.GetLeft(), r.GetTop(), &left, &top );
3385 CalcUnscrolledPosition( r.GetRight(), r.GetBottom(), &right, &bottom );
3386
3387 // find the cells within these bounds
3388 //
3389 int row, col;
3390 for ( row = 0; row < m_numRows; row++ )
3391 {
3392 if ( GetRowBottom(row) <= top )
3393 continue;
3394
3395 if ( GetRowTop(row) > bottom )
3396 break;
3397
3398 m_rowsExposed.Add( row );
3399
3400 for ( col = 0; col < m_numCols; col++ )
3401 {
3402 if ( GetColRight(col) <= left )
3403 continue;
3404
3405 if ( GetColLeft(col) > right )
3406 break;
3407
3408 if ( m_colsExposed.Index( col ) == wxNOT_FOUND )
3409 m_colsExposed.Add( col );
3410 m_cellsExposed.Add( wxGridCellCoords( row, col ) );
3411 }
3412 }
3413
3414 iter++;
3415 }
3416 }
3417
3418
3419 void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent& event )
3420 {
3421 int x, y, row;
3422 wxPoint pos( event.GetPosition() );
3423 CalcUnscrolledPosition( pos.x, pos.y, &x, &y );
3424
3425 if ( event.Dragging() )
3426 {
3427 m_isDragging = TRUE;
3428
3429 if ( event.LeftIsDown() )
3430 {
3431 switch( m_cursorMode )
3432 {
3433 case WXGRID_CURSOR_RESIZE_ROW:
3434 {
3435 int cw, ch, left, dummy;
3436 m_gridWin->GetClientSize( &cw, &ch );
3437 CalcUnscrolledPosition( 0, 0, &left, &dummy );
3438
3439 wxClientDC dc( m_gridWin );
3440 PrepareDC( dc );
3441 y = wxMax( y, GetRowTop(m_dragRowOrCol) + WXGRID_MIN_ROW_HEIGHT );
3442 dc.SetLogicalFunction(wxINVERT);
3443 if ( m_dragLastPos >= 0 )
3444 {
3445 dc.DrawLine( left, m_dragLastPos, left+cw, m_dragLastPos );
3446 }
3447 dc.DrawLine( left, y, left+cw, y );
3448 m_dragLastPos = y;
3449 }
3450 break;
3451
3452 case WXGRID_CURSOR_SELECT_ROW:
3453 if ( (row = YToRow( y )) >= 0 &&
3454 !IsInSelection( row, 0 ) )
3455 {
3456 SelectRow( row, TRUE );
3457 }
3458
3459 // default label to suppress warnings about "enumeration value
3460 // 'xxx' not handled in switch
3461 default:
3462 break;
3463 }
3464 }
3465 return;
3466 }
3467
3468 m_isDragging = FALSE;
3469
3470
3471 // ------------ Entering or leaving the window
3472 //
3473 if ( event.Entering() || event.Leaving() )
3474 {
3475 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_rowLabelWin);
3476 }
3477
3478
3479 // ------------ Left button pressed
3480 //
3481 else if ( event.LeftDown() )
3482 {
3483 // don't send a label click event for a hit on the
3484 // edge of the row label - this is probably the user
3485 // wanting to resize the row
3486 //
3487 if ( YToEdgeOfRow(y) < 0 )
3488 {
3489 row = YToRow(y);
3490 if ( row >= 0 &&
3491 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK, row, -1, event ) )
3492 {
3493 SelectRow( row, event.ShiftDown() );
3494 ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW, m_rowLabelWin);
3495 }
3496 }
3497 else
3498 {
3499 // starting to drag-resize a row
3500 //
3501 if ( CanDragRowSize() )
3502 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW, m_rowLabelWin);
3503 }
3504 }
3505
3506
3507 // ------------ Left double click
3508 //
3509 else if (event.LeftDClick() )
3510 {
3511 if ( YToEdgeOfRow(y) < 0 )
3512 {
3513 row = YToRow(y);
3514 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK, row, -1, event );
3515 }
3516 }
3517
3518
3519 // ------------ Left button released
3520 //
3521 else if ( event.LeftUp() )
3522 {
3523 if ( m_cursorMode == WXGRID_CURSOR_RESIZE_ROW )
3524 {
3525 DoEndDragResizeRow();
3526
3527 // Note: we are ending the event *after* doing
3528 // default processing in this case
3529 //
3530 SendEvent( wxEVT_GRID_ROW_SIZE, m_dragRowOrCol, -1, event );
3531 }
3532
3533 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_rowLabelWin);
3534 m_dragLastPos = -1;
3535 }
3536
3537
3538 // ------------ Right button down
3539 //
3540 else if ( event.RightDown() )
3541 {
3542 row = YToRow(y);
3543 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK, row, -1, event ) )
3544 {
3545 // no default action at the moment
3546 }
3547 }
3548
3549
3550 // ------------ Right double click
3551 //
3552 else if ( event.RightDClick() )
3553 {
3554 row = YToRow(y);
3555 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK, row, -1, event ) )
3556 {
3557 // no default action at the moment
3558 }
3559 }
3560
3561
3562 // ------------ No buttons down and mouse moving
3563 //
3564 else if ( event.Moving() )
3565 {
3566 m_dragRowOrCol = YToEdgeOfRow( y );
3567 if ( m_dragRowOrCol >= 0 )
3568 {
3569 if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
3570 {
3571 // don't capture the mouse yet
3572 if ( CanDragRowSize() )
3573 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW, m_rowLabelWin, FALSE);
3574 }
3575 }
3576 else if ( m_cursorMode != WXGRID_CURSOR_SELECT_CELL )
3577 {
3578 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_rowLabelWin, FALSE);
3579 }
3580 }
3581 }
3582
3583
3584 void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent& event )
3585 {
3586 int x, y, col;
3587 wxPoint pos( event.GetPosition() );
3588 CalcUnscrolledPosition( pos.x, pos.y, &x, &y );
3589
3590 if ( event.Dragging() )
3591 {
3592 m_isDragging = TRUE;
3593
3594 if ( event.LeftIsDown() )
3595 {
3596 switch( m_cursorMode )
3597 {
3598 case WXGRID_CURSOR_RESIZE_COL:
3599 {
3600 int cw, ch, dummy, top;
3601 m_gridWin->GetClientSize( &cw, &ch );
3602 CalcUnscrolledPosition( 0, 0, &dummy, &top );
3603
3604 wxClientDC dc( m_gridWin );
3605 PrepareDC( dc );
3606
3607 x = wxMax( x, GetColLeft(m_dragRowOrCol) +
3608 GetColMinimalWidth(m_dragRowOrCol));
3609 dc.SetLogicalFunction(wxINVERT);
3610 if ( m_dragLastPos >= 0 )
3611 {
3612 dc.DrawLine( m_dragLastPos, top, m_dragLastPos, top+ch );
3613 }
3614 dc.DrawLine( x, top, x, top+ch );
3615 m_dragLastPos = x;
3616 }
3617 break;
3618
3619 case WXGRID_CURSOR_SELECT_COL:
3620 if ( (col = XToCol( x )) >= 0 &&
3621 !IsInSelection( 0, col ) )
3622 {
3623 SelectCol( col, TRUE );
3624 }
3625
3626 // default label to suppress warnings about "enumeration value
3627 // 'xxx' not handled in switch
3628 default:
3629 break;
3630 }
3631 }
3632 return;
3633 }
3634
3635 m_isDragging = FALSE;
3636
3637
3638 // ------------ Entering or leaving the window
3639 //
3640 if ( event.Entering() || event.Leaving() )
3641 {
3642 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_colLabelWin);
3643 }
3644
3645
3646 // ------------ Left button pressed
3647 //
3648 else if ( event.LeftDown() )
3649 {
3650 // don't send a label click event for a hit on the
3651 // edge of the col label - this is probably the user
3652 // wanting to resize the col
3653 //
3654 if ( XToEdgeOfCol(x) < 0 )
3655 {
3656 col = XToCol(x);
3657 if ( col >= 0 &&
3658 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK, -1, col, event ) )
3659 {
3660 SelectCol( col, event.ShiftDown() );
3661 ChangeCursorMode(WXGRID_CURSOR_SELECT_COL, m_colLabelWin);
3662 }
3663 }
3664 else
3665 {
3666 // starting to drag-resize a col
3667 //
3668 if ( CanDragColSize() )
3669 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL, m_colLabelWin);
3670 }
3671 }
3672
3673
3674 // ------------ Left double click
3675 //
3676 if ( event.LeftDClick() )
3677 {
3678 if ( XToEdgeOfCol(x) < 0 )
3679 {
3680 col = XToCol(x);
3681 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK, -1, col, event );
3682 }
3683 }
3684
3685
3686 // ------------ Left button released
3687 //
3688 else if ( event.LeftUp() )
3689 {
3690 if ( m_cursorMode == WXGRID_CURSOR_RESIZE_COL )
3691 {
3692 DoEndDragResizeCol();
3693
3694 // Note: we are ending the event *after* doing
3695 // default processing in this case
3696 //
3697 SendEvent( wxEVT_GRID_COL_SIZE, -1, m_dragRowOrCol, event );
3698 }
3699
3700 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_colLabelWin);
3701 m_dragLastPos = -1;
3702 }
3703
3704
3705 // ------------ Right button down
3706 //
3707 else if ( event.RightDown() )
3708 {
3709 col = XToCol(x);
3710 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK, -1, col, event ) )
3711 {
3712 // no default action at the moment
3713 }
3714 }
3715
3716
3717 // ------------ Right double click
3718 //
3719 else if ( event.RightDClick() )
3720 {
3721 col = XToCol(x);
3722 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK, -1, col, event ) )
3723 {
3724 // no default action at the moment
3725 }
3726 }
3727
3728
3729 // ------------ No buttons down and mouse moving
3730 //
3731 else if ( event.Moving() )
3732 {
3733 m_dragRowOrCol = XToEdgeOfCol( x );
3734 if ( m_dragRowOrCol >= 0 )
3735 {
3736 if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
3737 {
3738 // don't capture the cursor yet
3739 if ( CanDragColSize() )
3740 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL, m_colLabelWin, FALSE);
3741 }
3742 }
3743 else if ( m_cursorMode != WXGRID_CURSOR_SELECT_CELL )
3744 {
3745 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_colLabelWin, FALSE);
3746 }
3747 }
3748 }
3749
3750
3751 void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent& event )
3752 {
3753 if ( event.LeftDown() )
3754 {
3755 // indicate corner label by having both row and
3756 // col args == -1
3757 //
3758 if ( !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK, -1, -1, event ) )
3759 {
3760 SelectAll();
3761 }
3762 }
3763
3764 else if ( event.LeftDClick() )
3765 {
3766 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK, -1, -1, event );
3767 }
3768
3769 else if ( event.RightDown() )
3770 {
3771 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK, -1, -1, event ) )
3772 {
3773 // no default action at the moment
3774 }
3775 }
3776
3777 else if ( event.RightDClick() )
3778 {
3779 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK, -1, -1, event ) )
3780 {
3781 // no default action at the moment
3782 }
3783 }
3784 }
3785
3786 void wxGrid::ChangeCursorMode(CursorMode mode,
3787 wxWindow *win,
3788 bool captureMouse)
3789 {
3790 #ifdef __WXDEBUG__
3791 static const wxChar *cursorModes[] =
3792 {
3793 _T("SELECT_CELL"),
3794 _T("RESIZE_ROW"),
3795 _T("RESIZE_COL"),
3796 _T("SELECT_ROW"),
3797 _T("SELECT_COL")
3798 };
3799
3800 wxLogTrace(_T("grid"),
3801 _T("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
3802 win == m_colLabelWin ? _T("colLabelWin")
3803 : win ? _T("rowLabelWin")
3804 : _T("gridWin"),
3805 cursorModes[m_cursorMode], cursorModes[mode]);
3806 #endif // __WXDEBUG__
3807
3808 if ( mode == m_cursorMode )
3809 return;
3810
3811 if ( !win )
3812 {
3813 // by default use the grid itself
3814 win = m_gridWin;
3815 }
3816
3817 if ( m_winCapture )
3818 {
3819 m_winCapture->ReleaseMouse();
3820 m_winCapture = (wxWindow *)NULL;
3821 }
3822
3823 m_cursorMode = mode;
3824
3825 switch ( m_cursorMode )
3826 {
3827 case WXGRID_CURSOR_RESIZE_ROW:
3828 win->SetCursor( m_rowResizeCursor );
3829 break;
3830
3831 case WXGRID_CURSOR_RESIZE_COL:
3832 win->SetCursor( m_colResizeCursor );
3833 break;
3834
3835 default:
3836 win->SetCursor( *wxSTANDARD_CURSOR );
3837 }
3838
3839 // we need to capture mouse when resizing
3840 bool resize = m_cursorMode == WXGRID_CURSOR_RESIZE_ROW ||
3841 m_cursorMode == WXGRID_CURSOR_RESIZE_COL;
3842
3843 if ( captureMouse && resize )
3844 {
3845 win->CaptureMouse();
3846 m_winCapture = win;
3847 }
3848 }
3849
3850 void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent& event )
3851 {
3852 int x, y;
3853 wxPoint pos( event.GetPosition() );
3854 CalcUnscrolledPosition( pos.x, pos.y, &x, &y );
3855
3856 wxGridCellCoords coords;
3857 XYToCell( x, y, coords );
3858
3859 if ( event.Dragging() )
3860 {
3861 //wxLogDebug("pos(%d, %d) coords(%d, %d)", pos.x, pos.y, coords.GetRow(), coords.GetCol());
3862
3863 // Don't start doing anything until the mouse has been drug at
3864 // least 3 pixels in any direction...
3865 if (! m_isDragging)
3866 {
3867 if (m_startDragPos == wxDefaultPosition)
3868 {
3869 m_startDragPos = pos;
3870 return;
3871 }
3872 if (abs(m_startDragPos.x - pos.x) < 4 && abs(m_startDragPos.y - pos.y) < 4)
3873 return;
3874 }
3875
3876 m_isDragging = TRUE;
3877 if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
3878 {
3879 // Hide the edit control, so it
3880 // won't interfer with drag-shrinking.
3881 if ( IsCellEditControlEnabled() )
3882 {
3883 HideCellEditControl();
3884 SaveEditControlValue();
3885 }
3886
3887 // Have we captured the mouse yet?
3888 if (! m_winCapture)
3889 {
3890 m_winCapture = m_gridWin;
3891 m_winCapture->CaptureMouse();
3892 }
3893
3894 if ( coords != wxGridNoCellCoords )
3895 {
3896 if ( !IsSelection() )
3897 {
3898 SelectBlock( coords, coords );
3899 }
3900 else
3901 {
3902 SelectBlock( m_currentCellCoords, coords );
3903 }
3904
3905 if (! IsVisible(coords))
3906 {
3907 MakeCellVisible(coords);
3908 // TODO: need to introduce a delay or something here. The
3909 // scrolling is way to fast, at least on MSW.
3910 }
3911 }
3912 }
3913 else if ( m_cursorMode == WXGRID_CURSOR_RESIZE_ROW )
3914 {
3915 int cw, ch, left, dummy;
3916 m_gridWin->GetClientSize( &cw, &ch );
3917 CalcUnscrolledPosition( 0, 0, &left, &dummy );
3918
3919 wxClientDC dc( m_gridWin );
3920 PrepareDC( dc );
3921 y = wxMax( y, GetRowTop(m_dragRowOrCol) + WXGRID_MIN_ROW_HEIGHT );
3922 dc.SetLogicalFunction(wxINVERT);
3923 if ( m_dragLastPos >= 0 )
3924 {
3925 dc.DrawLine( left, m_dragLastPos, left+cw, m_dragLastPos );
3926 }
3927 dc.DrawLine( left, y, left+cw, y );
3928 m_dragLastPos = y;
3929 }
3930 else if ( m_cursorMode == WXGRID_CURSOR_RESIZE_COL )
3931 {
3932 int cw, ch, dummy, top;
3933 m_gridWin->GetClientSize( &cw, &ch );
3934 CalcUnscrolledPosition( 0, 0, &dummy, &top );
3935
3936 wxClientDC dc( m_gridWin );
3937 PrepareDC( dc );
3938 x = wxMax( x, GetColLeft(m_dragRowOrCol) +
3939 GetColMinimalWidth(m_dragRowOrCol) );
3940 dc.SetLogicalFunction(wxINVERT);
3941 if ( m_dragLastPos >= 0 )
3942 {
3943 dc.DrawLine( m_dragLastPos, top, m_dragLastPos, top+ch );
3944 }
3945 dc.DrawLine( x, top, x, top+ch );
3946 m_dragLastPos = x;
3947 }
3948
3949 return;
3950 }
3951
3952 m_isDragging = FALSE;
3953 m_startDragPos = wxDefaultPosition;
3954
3955 // VZ: if we do this, the mode is reset to WXGRID_CURSOR_SELECT_CELL
3956 // immediately after it becomes WXGRID_CURSOR_RESIZE_ROW/COL under
3957 // wxGTK
3958 #if 0
3959 if ( event.Entering() || event.Leaving() )
3960 {
3961 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL);
3962 m_gridWin->SetCursor( *wxSTANDARD_CURSOR );
3963 }
3964 else
3965 #endif // 0
3966
3967 // ------------ Left button pressed
3968 //
3969 if ( event.LeftDown() && coords != wxGridNoCellCoords )
3970 {
3971 if ( event.ShiftDown() )
3972 {
3973 SelectBlock( m_currentCellCoords, coords );
3974 }
3975 else if ( XToEdgeOfCol(x) < 0 &&
3976 YToEdgeOfRow(y) < 0 )
3977 {
3978 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_CLICK,
3979 coords.GetRow(),
3980 coords.GetCol(),
3981 event ) )
3982 {
3983 DisableCellEditControl();
3984 MakeCellVisible( coords );
3985
3986 // if this is the second click on this cell then start
3987 // the edit control
3988 if ( m_waitForSlowClick &&
3989 (coords == m_currentCellCoords) &&
3990 CanEnableCellControl())
3991 {
3992 EnableCellEditControl();
3993
3994 wxGridCellAttr* attr = GetCellAttr(m_currentCellCoords);
3995 attr->GetEditor(this, coords.GetRow(), coords.GetCol())->StartingClick();
3996 attr->DecRef();
3997
3998 m_waitForSlowClick = FALSE;
3999 }
4000 else
4001 {
4002 SetCurrentCell( coords );
4003 m_waitForSlowClick = TRUE;
4004 }
4005 }
4006 }
4007 }
4008
4009
4010 // ------------ Left double click
4011 //
4012 else if ( event.LeftDClick() && coords != wxGridNoCellCoords )
4013 {
4014 DisableCellEditControl();
4015
4016 if ( XToEdgeOfCol(x) < 0 && YToEdgeOfRow(y) < 0 )
4017 {
4018 SendEvent( wxEVT_GRID_CELL_LEFT_DCLICK,
4019 coords.GetRow(),
4020 coords.GetCol(),
4021 event );
4022 }
4023 }
4024
4025
4026 // ------------ Left button released
4027 //
4028 else if ( event.LeftUp() )
4029 {
4030 if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
4031 {
4032 if ( IsSelection() )
4033 {
4034 if (m_winCapture)
4035 {
4036 m_winCapture->ReleaseMouse();
4037 m_winCapture = NULL;
4038 }
4039 SendEvent( wxEVT_GRID_RANGE_SELECT, -1, -1, event );
4040 }
4041
4042 // Show the edit control, if it has been hidden for
4043 // drag-shrinking.
4044 ShowCellEditControl();
4045 }
4046 else if ( m_cursorMode == WXGRID_CURSOR_RESIZE_ROW )
4047 {
4048 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL);
4049 DoEndDragResizeRow();
4050
4051 // Note: we are ending the event *after* doing
4052 // default processing in this case
4053 //
4054 SendEvent( wxEVT_GRID_ROW_SIZE, m_dragRowOrCol, -1, event );
4055 }
4056 else if ( m_cursorMode == WXGRID_CURSOR_RESIZE_COL )
4057 {
4058 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL);
4059 DoEndDragResizeCol();
4060
4061 // Note: we are ending the event *after* doing
4062 // default processing in this case
4063 //
4064 SendEvent( wxEVT_GRID_COL_SIZE, -1, m_dragRowOrCol, event );
4065 }
4066
4067 m_dragLastPos = -1;
4068 }
4069
4070
4071 // ------------ Right button down
4072 //
4073 else if ( event.RightDown() && coords != wxGridNoCellCoords )
4074 {
4075 DisableCellEditControl();
4076 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_CLICK,
4077 coords.GetRow(),
4078 coords.GetCol(),
4079 event ) )
4080 {
4081 // no default action at the moment
4082 }
4083 }
4084
4085
4086 // ------------ Right double click
4087 //
4088 else if ( event.RightDClick() && coords != wxGridNoCellCoords )
4089 {
4090 DisableCellEditControl();
4091 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_DCLICK,
4092 coords.GetRow(),
4093 coords.GetCol(),
4094 event ) )
4095 {
4096 // no default action at the moment
4097 }
4098 }
4099
4100 // ------------ Moving and no button action
4101 //
4102 else if ( event.Moving() && !event.IsButton() )
4103 {
4104 int dragRow = YToEdgeOfRow( y );
4105 int dragCol = XToEdgeOfCol( x );
4106
4107 // Dragging on the corner of a cell to resize in both
4108 // directions is not implemented yet...
4109 //
4110 if ( dragRow >= 0 && dragCol >= 0 )
4111 {
4112 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL);
4113 return;
4114 }
4115
4116 if ( dragRow >= 0 )
4117 {
4118 m_dragRowOrCol = dragRow;
4119
4120 if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
4121 {
4122 if ( CanDragRowSize() && CanDragGridSize() )
4123 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW);
4124 }
4125
4126 if ( dragCol >= 0 )
4127 {
4128 m_dragRowOrCol = dragCol;
4129 }
4130
4131 return;
4132 }
4133
4134 if ( dragCol >= 0 )
4135 {
4136 m_dragRowOrCol = dragCol;
4137
4138 if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
4139 {
4140 if ( CanDragColSize() && CanDragGridSize() )
4141 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL);
4142 }
4143
4144 return;
4145 }
4146
4147 // Neither on a row or col edge
4148 //
4149 if ( m_cursorMode != WXGRID_CURSOR_SELECT_CELL )
4150 {
4151 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL);
4152 }
4153 }
4154 }
4155
4156
4157 void wxGrid::DoEndDragResizeRow()
4158 {
4159 if ( m_dragLastPos >= 0 )
4160 {
4161 // erase the last line and resize the row
4162 //
4163 int cw, ch, left, dummy;
4164 m_gridWin->GetClientSize( &cw, &ch );
4165 CalcUnscrolledPosition( 0, 0, &left, &dummy );
4166
4167 wxClientDC dc( m_gridWin );
4168 PrepareDC( dc );
4169 dc.SetLogicalFunction( wxINVERT );
4170 dc.DrawLine( left, m_dragLastPos, left+cw, m_dragLastPos );
4171 HideCellEditControl();
4172 SaveEditControlValue();
4173
4174 int rowTop = GetRowTop(m_dragRowOrCol);
4175 SetRowSize( m_dragRowOrCol,
4176 wxMax( m_dragLastPos - rowTop, WXGRID_MIN_ROW_HEIGHT ) );
4177
4178 if ( !GetBatchCount() )
4179 {
4180 // Only needed to get the correct rect.y:
4181 wxRect rect ( CellToRect( m_dragRowOrCol, 0 ) );
4182 rect.x = 0;
4183 CalcScrolledPosition(0, rect.y, &dummy, &rect.y);
4184 rect.width = m_rowLabelWidth;
4185 rect.height = ch - rect.y;
4186 m_rowLabelWin->Refresh( TRUE, &rect );
4187 rect.width = cw;
4188 m_gridWin->Refresh( FALSE, &rect );
4189 }
4190
4191 ShowCellEditControl();
4192 }
4193 }
4194
4195
4196 void wxGrid::DoEndDragResizeCol()
4197 {
4198 if ( m_dragLastPos >= 0 )
4199 {
4200 // erase the last line and resize the col
4201 //
4202 int cw, ch, dummy, top;
4203 m_gridWin->GetClientSize( &cw, &ch );
4204 CalcUnscrolledPosition( 0, 0, &dummy, &top );
4205
4206 wxClientDC dc( m_gridWin );
4207 PrepareDC( dc );
4208 dc.SetLogicalFunction( wxINVERT );
4209 dc.DrawLine( m_dragLastPos, top, m_dragLastPos, top+ch );
4210 HideCellEditControl();
4211 SaveEditControlValue();
4212
4213 int colLeft = GetColLeft(m_dragRowOrCol);
4214 SetColSize( m_dragRowOrCol,
4215 wxMax( m_dragLastPos - colLeft,
4216 GetColMinimalWidth(m_dragRowOrCol) ) );
4217
4218 if ( !GetBatchCount() )
4219 {
4220 // Only needed to get the correct rect.x:
4221 wxRect rect ( CellToRect( 0, m_dragRowOrCol ) );
4222 rect.y = 0;
4223 CalcScrolledPosition(rect.x, 0, &rect.x, &dummy);
4224 rect.width = cw - rect.x;
4225 rect.height = m_colLabelHeight;
4226 m_colLabelWin->Refresh( TRUE, &rect );
4227 rect.height = ch;
4228 m_gridWin->Refresh( FALSE, &rect );
4229 }
4230
4231 ShowCellEditControl();
4232 }
4233 }
4234
4235
4236
4237 //
4238 // ------ interaction with data model
4239 //
4240 bool wxGrid::ProcessTableMessage( wxGridTableMessage& msg )
4241 {
4242 switch ( msg.GetId() )
4243 {
4244 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES:
4245 return GetModelValues();
4246
4247 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES:
4248 return SetModelValues();
4249
4250 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED:
4251 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED:
4252 case wxGRIDTABLE_NOTIFY_ROWS_DELETED:
4253 case wxGRIDTABLE_NOTIFY_COLS_INSERTED:
4254 case wxGRIDTABLE_NOTIFY_COLS_APPENDED:
4255 case wxGRIDTABLE_NOTIFY_COLS_DELETED:
4256 return Redimension( msg );
4257
4258 default:
4259 return FALSE;
4260 }
4261 }
4262
4263
4264
4265 // The behaviour of this function depends on the grid table class
4266 // Clear() function. For the default wxGridStringTable class the
4267 // behavious is to replace all cell contents with wxEmptyString but
4268 // not to change the number of rows or cols.
4269 //
4270 void wxGrid::ClearGrid()
4271 {
4272 if ( m_table )
4273 {
4274 if (IsCellEditControlEnabled())
4275 DisableCellEditControl();
4276
4277 m_table->Clear();
4278 if ( !GetBatchCount() ) m_gridWin->Refresh();
4279 }
4280 }
4281
4282
4283 bool wxGrid::InsertRows( int pos, int numRows, bool WXUNUSED(updateLabels) )
4284 {
4285 // TODO: something with updateLabels flag
4286
4287 if ( !m_created )
4288 {
4289 wxFAIL_MSG( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") );
4290 return FALSE;
4291 }
4292
4293 if ( m_table )
4294 {
4295 if (IsCellEditControlEnabled())
4296 DisableCellEditControl();
4297
4298 bool ok = m_table->InsertRows( pos, numRows );
4299
4300 // the table will have sent the results of the insert row
4301 // operation to this view object as a grid table message
4302 //
4303 if ( ok )
4304 {
4305 if ( m_numCols == 0 )
4306 {
4307 m_table->AppendCols( WXGRID_DEFAULT_NUMBER_COLS );
4308 //
4309 // TODO: perhaps instead of appending the default number of cols
4310 // we should remember what the last non-zero number of cols was ?
4311 //
4312 }
4313
4314 if ( m_currentCellCoords == wxGridNoCellCoords )
4315 {
4316 // if we have just inserted cols into an empty grid the current
4317 // cell will be undefined...
4318 //
4319 SetCurrentCell( 0, 0 );
4320 }
4321
4322 ClearSelection();
4323 if ( !GetBatchCount() ) Refresh();
4324 }
4325
4326 return ok;
4327 }
4328 else
4329 {
4330 return FALSE;
4331 }
4332 }
4333
4334
4335 bool wxGrid::AppendRows( int numRows, bool WXUNUSED(updateLabels) )
4336 {
4337 // TODO: something with updateLabels flag
4338
4339 if ( !m_created )
4340 {
4341 wxFAIL_MSG( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") );
4342 return FALSE;
4343 }
4344
4345 if ( m_table && m_table->AppendRows( numRows ) )
4346 {
4347 if ( m_currentCellCoords == wxGridNoCellCoords )
4348 {
4349 // if we have just inserted cols into an empty grid the current
4350 // cell will be undefined...
4351 //
4352 SetCurrentCell( 0, 0 );
4353 }
4354
4355 // the table will have sent the results of the append row
4356 // operation to this view object as a grid table message
4357 //
4358 ClearSelection();
4359 if ( !GetBatchCount() ) Refresh();
4360 return TRUE;
4361 }
4362 else
4363 {
4364 return FALSE;
4365 }
4366 }
4367
4368
4369 bool wxGrid::DeleteRows( int pos, int numRows, bool WXUNUSED(updateLabels) )
4370 {
4371 // TODO: something with updateLabels flag
4372
4373 if ( !m_created )
4374 {
4375 wxFAIL_MSG( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") );
4376 return FALSE;
4377 }
4378
4379 if ( m_table )
4380 {
4381 if (IsCellEditControlEnabled())
4382 DisableCellEditControl();
4383
4384 if (m_table->DeleteRows( pos, numRows ))
4385 {
4386
4387 // the table will have sent the results of the delete row
4388 // operation to this view object as a grid table message
4389 //
4390 ClearSelection();
4391 if ( !GetBatchCount() ) Refresh();
4392 return TRUE;
4393 }
4394 }
4395 return FALSE;
4396 }
4397
4398
4399 bool wxGrid::InsertCols( int pos, int numCols, bool WXUNUSED(updateLabels) )
4400 {
4401 // TODO: something with updateLabels flag
4402
4403 if ( !m_created )
4404 {
4405 wxFAIL_MSG( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") );
4406 return FALSE;
4407 }
4408
4409 if ( m_table )
4410 {
4411 if (IsCellEditControlEnabled())
4412 DisableCellEditControl();
4413
4414 bool ok = m_table->InsertCols( pos, numCols );
4415
4416 // the table will have sent the results of the insert col
4417 // operation to this view object as a grid table message
4418 //
4419 if ( ok )
4420 {
4421 if ( m_currentCellCoords == wxGridNoCellCoords )
4422 {
4423 // if we have just inserted cols into an empty grid the current
4424 // cell will be undefined...
4425 //
4426 SetCurrentCell( 0, 0 );
4427 }
4428
4429 ClearSelection();
4430 if ( !GetBatchCount() ) Refresh();
4431 }
4432
4433 return ok;
4434 }
4435 else
4436 {
4437 return FALSE;
4438 }
4439 }
4440
4441
4442 bool wxGrid::AppendCols( int numCols, bool WXUNUSED(updateLabels) )
4443 {
4444 // TODO: something with updateLabels flag
4445
4446 if ( !m_created )
4447 {
4448 wxFAIL_MSG( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") );
4449 return FALSE;
4450 }
4451
4452 if ( m_table && m_table->AppendCols( numCols ) )
4453 {
4454 // the table will have sent the results of the append col
4455 // operation to this view object as a grid table message
4456 //
4457 if ( m_currentCellCoords == wxGridNoCellCoords )
4458 {
4459 // if we have just inserted cols into an empty grid the current
4460 // cell will be undefined...
4461 //
4462 SetCurrentCell( 0, 0 );
4463 }
4464
4465 ClearSelection();
4466 if ( !GetBatchCount() ) Refresh();
4467 return TRUE;
4468 }
4469 else
4470 {
4471 return FALSE;
4472 }
4473 }
4474
4475
4476 bool wxGrid::DeleteCols( int pos, int numCols, bool WXUNUSED(updateLabels) )
4477 {
4478 // TODO: something with updateLabels flag
4479
4480 if ( !m_created )
4481 {
4482 wxFAIL_MSG( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") );
4483 return FALSE;
4484 }
4485
4486 if ( m_table )
4487 {
4488 if (IsCellEditControlEnabled())
4489 DisableCellEditControl();
4490
4491 if ( m_table->DeleteCols( pos, numCols ) )
4492 {
4493 // the table will have sent the results of the delete col
4494 // operation to this view object as a grid table message
4495 //
4496 ClearSelection();
4497 if ( !GetBatchCount() ) Refresh();
4498 return TRUE;
4499 }
4500 }
4501 return FALSE;
4502 }
4503
4504
4505
4506 //
4507 // ----- event handlers
4508 //
4509
4510 // Generate a grid event based on a mouse event and
4511 // return the result of ProcessEvent()
4512 //
4513 bool wxGrid::SendEvent( const wxEventType type,
4514 int row, int col,
4515 wxMouseEvent& mouseEv )
4516 {
4517 if ( type == wxEVT_GRID_ROW_SIZE || type == wxEVT_GRID_COL_SIZE )
4518 {
4519 int rowOrCol = (row == -1 ? col : row);
4520
4521 wxGridSizeEvent gridEvt( GetId(),
4522 type,
4523 this,
4524 rowOrCol,
4525 mouseEv.GetX(), mouseEv.GetY(),
4526 mouseEv.ControlDown(),
4527 mouseEv.ShiftDown(),
4528 mouseEv.AltDown(),
4529 mouseEv.MetaDown() );
4530
4531 return GetEventHandler()->ProcessEvent(gridEvt);
4532 }
4533 else if ( type == wxEVT_GRID_RANGE_SELECT )
4534 {
4535 wxGridRangeSelectEvent gridEvt( GetId(),
4536 type,
4537 this,
4538 m_selectedTopLeft,
4539 m_selectedBottomRight,
4540 mouseEv.ControlDown(),
4541 mouseEv.ShiftDown(),
4542 mouseEv.AltDown(),
4543 mouseEv.MetaDown() );
4544
4545 return GetEventHandler()->ProcessEvent(gridEvt);
4546 }
4547 else
4548 {
4549 wxGridEvent gridEvt( GetId(),
4550 type,
4551 this,
4552 row, col,
4553 mouseEv.GetX(), mouseEv.GetY(),
4554 mouseEv.ControlDown(),
4555 mouseEv.ShiftDown(),
4556 mouseEv.AltDown(),
4557 mouseEv.MetaDown() );
4558
4559 return GetEventHandler()->ProcessEvent(gridEvt);
4560 }
4561 }
4562
4563
4564 // Generate a grid event of specified type and return the result
4565 // of ProcessEvent().
4566 //
4567 bool wxGrid::SendEvent( const wxEventType type,
4568 int row, int col )
4569 {
4570 if ( type == wxEVT_GRID_ROW_SIZE || type == wxEVT_GRID_COL_SIZE )
4571 {
4572 int rowOrCol = (row == -1 ? col : row);
4573
4574 wxGridSizeEvent gridEvt( GetId(),
4575 type,
4576 this,
4577 rowOrCol );
4578
4579 return GetEventHandler()->ProcessEvent(gridEvt);
4580 }
4581 else
4582 {
4583 wxGridEvent gridEvt( GetId(),
4584 type,
4585 this,
4586 row, col );
4587
4588 return GetEventHandler()->ProcessEvent(gridEvt);
4589 }
4590 }
4591
4592
4593 void wxGrid::OnPaint( wxPaintEvent& WXUNUSED(event) )
4594 {
4595 wxPaintDC dc( this );
4596
4597 if ( m_currentCellCoords == wxGridNoCellCoords &&
4598 m_numRows && m_numCols )
4599 {
4600 m_currentCellCoords.Set(0, 0);
4601 ShowCellEditControl();
4602 }
4603
4604 m_displayed = TRUE;
4605 }
4606
4607
4608 // This is just here to make sure that CalcDimensions gets called when
4609 // the grid view is resized... then the size event is skipped to allow
4610 // the box sizers to handle everything
4611 //
4612 void wxGrid::OnSize( wxSizeEvent& event )
4613 {
4614 CalcWindowSizes();
4615 CalcDimensions();
4616 }
4617
4618
4619 void wxGrid::OnKeyDown( wxKeyEvent& event )
4620 {
4621 if ( m_inOnKeyDown )
4622 {
4623 // shouldn't be here - we are going round in circles...
4624 //
4625 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") );
4626 }
4627
4628 m_inOnKeyDown = TRUE;
4629
4630 // propagate the event up and see if it gets processed
4631 //
4632 wxWindow *parent = GetParent();
4633 wxKeyEvent keyEvt( event );
4634 keyEvt.SetEventObject( parent );
4635
4636 if ( !parent->GetEventHandler()->ProcessEvent( keyEvt ) )
4637 {
4638
4639 // TODO: Should also support Shift-cursor keys for
4640 // extending the selection. Maybe add a flag to
4641 // MoveCursorXXX() and MoveCursorXXXBlock() and
4642 // just send event.ShiftDown().
4643
4644 // try local handlers
4645 //
4646 switch ( event.KeyCode() )
4647 {
4648 case WXK_UP:
4649 if ( event.ControlDown() )
4650 {
4651 MoveCursorUpBlock();
4652 }
4653 else
4654 {
4655 MoveCursorUp();
4656 }
4657 break;
4658
4659 case WXK_DOWN:
4660 if ( event.ControlDown() )
4661 {
4662 MoveCursorDownBlock();
4663 }
4664 else
4665 {
4666 MoveCursorDown();
4667 }
4668 break;
4669
4670 case WXK_LEFT:
4671 if ( event.ControlDown() )
4672 {
4673 MoveCursorLeftBlock();
4674 }
4675 else
4676 {
4677 MoveCursorLeft();
4678 }
4679 break;
4680
4681 case WXK_RIGHT:
4682 if ( event.ControlDown() )
4683 {
4684 MoveCursorRightBlock();
4685 }
4686 else
4687 {
4688 MoveCursorRight();
4689 }
4690 break;
4691
4692 case WXK_RETURN:
4693 if ( event.ControlDown() )
4694 {
4695 event.Skip(); // to let the edit control have the return
4696 }
4697 else
4698 {
4699 MoveCursorDown();
4700 }
4701 break;
4702
4703 case WXK_TAB:
4704 if (event.ShiftDown())
4705 MoveCursorLeft();
4706 else
4707 MoveCursorRight();
4708 break;
4709
4710 case WXK_HOME:
4711 if ( event.ControlDown() )
4712 {
4713 MakeCellVisible( 0, 0 );
4714 SetCurrentCell( 0, 0 );
4715 }
4716 else
4717 {
4718 event.Skip();
4719 }
4720 break;
4721
4722 case WXK_END:
4723 if ( event.ControlDown() )
4724 {
4725 MakeCellVisible( m_numRows-1, m_numCols-1 );
4726 SetCurrentCell( m_numRows-1, m_numCols-1 );
4727 }
4728 else
4729 {
4730 event.Skip();
4731 }
4732 break;
4733
4734 case WXK_PRIOR:
4735 MovePageUp();
4736 break;
4737
4738 case WXK_NEXT:
4739 MovePageDown();
4740 break;
4741
4742 case WXK_SPACE:
4743 if ( !IsEditable() )
4744 {
4745 MoveCursorRight();
4746 break;
4747 }
4748 // Otherwise fall through to default
4749
4750 default:
4751 // alphanumeric keys or F2 (special key just for this) enable
4752 // the cell edit control
4753 if ( !(event.AltDown() ||
4754 event.MetaDown() ||
4755 event.ControlDown()) &&
4756 (isalnum(event.KeyCode()) || event.KeyCode() == WXK_F2) &&
4757 !IsCellEditControlEnabled() &&
4758 CanEnableCellControl() )
4759 {
4760 EnableCellEditControl();
4761 int row = m_currentCellCoords.GetRow();
4762 int col = m_currentCellCoords.GetCol();
4763 wxGridCellAttr* attr = GetCellAttr(row, col);
4764 attr->GetEditor(this, row, col)->StartingKey(event);
4765 attr->DecRef();
4766 }
4767 else
4768 {
4769 // let others process char events with modifiers or all
4770 // char events for readonly cells
4771 event.Skip();
4772 }
4773 break;
4774 }
4775 }
4776
4777 m_inOnKeyDown = FALSE;
4778 }
4779
4780
4781 void wxGrid::OnEraseBackground(wxEraseEvent&)
4782 {
4783 }
4784
4785 void wxGrid::SetCurrentCell( const wxGridCellCoords& coords )
4786 {
4787 if ( SendEvent( wxEVT_GRID_SELECT_CELL, coords.GetRow(), coords.GetCol() ) )
4788 {
4789 // the event has been intercepted - do nothing
4790 return;
4791 }
4792
4793 if ( m_displayed &&
4794 m_currentCellCoords != wxGridNoCellCoords )
4795 {
4796 HideCellEditControl();
4797 DisableCellEditControl();
4798
4799 // Clear the old current cell highlight
4800 wxRect r = BlockToDeviceRect(m_currentCellCoords, m_currentCellCoords);
4801
4802 // Otherwise refresh redraws the highlight!
4803 m_currentCellCoords = coords;
4804
4805 m_gridWin->Refresh( FALSE, &r );
4806 }
4807
4808 m_currentCellCoords = coords;
4809
4810 if ( m_displayed )
4811 {
4812 wxClientDC dc(m_gridWin);
4813 PrepareDC(dc);
4814
4815 wxGridCellAttr* attr = GetCellAttr(coords);
4816 DrawCellHighlight(dc, attr);
4817 attr->DecRef();
4818
4819 if ( IsSelection() )
4820 {
4821 wxRect r( SelectionToDeviceRect() );
4822 ClearSelection();
4823 if ( !GetBatchCount() ) m_gridWin->Refresh( FALSE, &r );
4824 }
4825 }
4826 }
4827
4828
4829 //
4830 // ------ functions to get/send data (see also public functions)
4831 //
4832
4833 bool wxGrid::GetModelValues()
4834 {
4835 if ( m_table )
4836 {
4837 // all we need to do is repaint the grid
4838 //
4839 m_gridWin->Refresh();
4840 return TRUE;
4841 }
4842
4843 return FALSE;
4844 }
4845
4846
4847 bool wxGrid::SetModelValues()
4848 {
4849 int row, col;
4850
4851 if ( m_table )
4852 {
4853 for ( row = 0; row < m_numRows; row++ )
4854 {
4855 for ( col = 0; col < m_numCols; col++ )
4856 {
4857 m_table->SetValue( row, col, GetCellValue(row, col) );
4858 }
4859 }
4860
4861 return TRUE;
4862 }
4863
4864 return FALSE;
4865 }
4866
4867
4868
4869 // Note - this function only draws cells that are in the list of
4870 // exposed cells (usually set from the update region by
4871 // CalcExposedCells)
4872 //
4873 void wxGrid::DrawGridCellArea( wxDC& dc )
4874 {
4875 if ( !m_numRows || !m_numCols ) return;
4876
4877 size_t i;
4878 size_t numCells = m_cellsExposed.GetCount();
4879
4880 for ( i = 0; i < numCells; i++ )
4881 {
4882 DrawCell( dc, m_cellsExposed[i] );
4883 }
4884 }
4885
4886
4887 void wxGrid::DrawGridSpace( wxDC& dc )
4888 {
4889 if ( m_numRows && m_numCols )
4890 {
4891 int cw, ch;
4892 m_gridWin->GetClientSize( &cw, &ch );
4893
4894 int right, bottom;
4895 CalcUnscrolledPosition( cw, ch, &right, &bottom );
4896
4897 if ( right > GetColRight(m_numCols-1) ||
4898 bottom > GetRowBottom(m_numRows-1) )
4899 {
4900 int left, top;
4901 CalcUnscrolledPosition( 0, 0, &left, &top );
4902
4903 dc.SetBrush( wxBrush(GetDefaultCellBackgroundColour(), wxSOLID) );
4904 dc.SetPen( *wxTRANSPARENT_PEN );
4905
4906 if ( right > GetColRight(m_numCols-1) )
4907 dc.DrawRectangle( GetColRight(m_numCols-1), top,
4908 right - GetColRight(m_numCols-1), ch );
4909
4910 if ( bottom > GetRowBottom(m_numRows-1) )
4911 dc.DrawRectangle( left, GetRowBottom(m_numRows-1),
4912 cw, bottom - GetRowBottom(m_numRows-1) );
4913 }
4914 }
4915 }
4916
4917
4918 void wxGrid::DrawCell( wxDC& dc, const wxGridCellCoords& coords )
4919 {
4920 int row = coords.GetRow();
4921 int col = coords.GetCol();
4922
4923 if ( GetColWidth(col) <= 0 || GetRowHeight(row) <= 0 )
4924 return;
4925
4926 // we draw the cell border ourselves
4927 #if !WXGRID_DRAW_LINES
4928 if ( m_gridLinesEnabled )
4929 DrawCellBorder( dc, coords );
4930 #endif
4931
4932 wxGridCellAttr* attr = GetCellAttr(row, col);
4933
4934 bool isCurrent = coords == m_currentCellCoords;
4935
4936 wxRect rect;
4937 rect.x = GetColLeft(col);
4938 rect.y = GetRowTop(row);
4939 rect.width = GetColWidth(col) - 1;
4940 rect.height = GetRowHeight(row) - 1;
4941
4942 // if the editor is shown, we should use it and not the renderer
4943 if ( isCurrent && IsCellEditControlEnabled() )
4944 {
4945 attr->GetEditor(this, row, col)->PaintBackground(rect, attr);
4946 }
4947 else
4948 {
4949 // but all the rest is drawn by the cell renderer and hence may be
4950 // customized
4951 attr->GetRenderer(this, row, col)->
4952 Draw(*this, *attr, dc, rect, row, col, IsInSelection(coords));
4953
4954 }
4955
4956 attr->DecRef();
4957 }
4958
4959 void wxGrid::DrawCellHighlight( wxDC& dc, const wxGridCellAttr *attr )
4960 {
4961 int row = m_currentCellCoords.GetRow();
4962 int col = m_currentCellCoords.GetCol();
4963
4964 if ( GetColWidth(col) <= 0 || GetRowHeight(row) <= 0 )
4965 return;
4966
4967 wxRect rect;
4968 rect.x = GetColLeft(col);
4969 rect.y = GetRowTop(row);
4970 rect.width = GetColWidth(col) - 1;
4971 rect.height = GetRowHeight(row) - 1;
4972
4973 // hmmm... what could we do here to show that the cell is disabled?
4974 // for now, I just draw a thinner border than for the other ones, but
4975 // it doesn't look really good
4976 dc.SetPen(wxPen(m_gridLineColour, attr->IsReadOnly() ? 1 : 3, wxSOLID));
4977 dc.SetBrush(*wxTRANSPARENT_BRUSH);
4978
4979 dc.DrawRectangle(rect);
4980
4981 #if 0
4982 // VZ: my experiments with 3d borders...
4983
4984 // how to properly set colours for arbitrary bg?
4985 wxCoord x1 = rect.x,
4986 y1 = rect.y,
4987 x2 = rect.x + rect.width -1,
4988 y2 = rect.y + rect.height -1;
4989
4990 dc.SetPen(*wxWHITE_PEN);
4991 dc.DrawLine(x1, y1, x2, y1);
4992 dc.DrawLine(x1, y1, x1, y2);
4993
4994 dc.DrawLine(x1 + 1, y2 - 1, x2 - 1, y2 - 1);
4995 dc.DrawLine(x2 - 1, y1 + 1, x2 - 1, y2 );
4996
4997 dc.SetPen(*wxBLACK_PEN);
4998 dc.DrawLine(x1, y2, x2, y2);
4999 dc.DrawLine(x2, y1, x2, y2+1);
5000 #endif // 0
5001 }
5002
5003
5004 void wxGrid::DrawCellBorder( wxDC& dc, const wxGridCellCoords& coords )
5005 {
5006 int row = coords.GetRow();
5007 int col = coords.GetCol();
5008 if ( GetColWidth(col) <= 0 || GetRowHeight(row) <= 0 )
5009 return;
5010
5011 dc.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID) );
5012
5013 // right hand border
5014 //
5015 dc.DrawLine( GetColRight(col), GetRowTop(row),
5016 GetColRight(col), GetRowBottom(row) );
5017
5018 // bottom border
5019 //
5020 dc.DrawLine( GetColLeft(col), GetRowBottom(row),
5021 GetColRight(col), GetRowBottom(row) );
5022 }
5023
5024 void wxGrid::DrawHighlight(wxDC& dc)
5025 {
5026 if ( IsCellEditControlEnabled() )
5027 {
5028 // don't show highlight when the edit control is shown
5029 return;
5030 }
5031
5032 // if the active cell was repainted, repaint its highlight too because it
5033 // might have been damaged by the grid lines
5034 size_t count = m_cellsExposed.GetCount();
5035 for ( size_t n = 0; n < count; n++ )
5036 {
5037 if ( m_cellsExposed[n] == m_currentCellCoords )
5038 {
5039 wxGridCellAttr* attr = GetCellAttr(m_currentCellCoords);
5040 DrawCellHighlight(dc, attr);
5041 attr->DecRef();
5042
5043 break;
5044 }
5045 }
5046 }
5047
5048 // TODO: remove this ???
5049 // This is used to redraw all grid lines e.g. when the grid line colour
5050 // has been changed
5051 //
5052 void wxGrid::DrawAllGridLines( wxDC& dc, const wxRegion & reg )
5053 {
5054 if ( !m_gridLinesEnabled ||
5055 !m_numRows ||
5056 !m_numCols ) return;
5057
5058 int top, bottom, left, right;
5059
5060 #ifndef __WXGTK__
5061 if (reg.IsEmpty())
5062 {
5063 int cw, ch;
5064 m_gridWin->GetClientSize(&cw, &ch);
5065
5066 // virtual coords of visible area
5067 //
5068 CalcUnscrolledPosition( 0, 0, &left, &top );
5069 CalcUnscrolledPosition( cw, ch, &right, &bottom );
5070 }
5071 else
5072 {
5073 wxCoord x, y, w, h;
5074 reg.GetBox(x, y, w, h);
5075 CalcUnscrolledPosition( x, y, &left, &top );
5076 CalcUnscrolledPosition( x + w, y + h, &right, &bottom );
5077 }
5078 #else
5079 int cw, ch;
5080 m_gridWin->GetClientSize(&cw, &ch);
5081 CalcUnscrolledPosition( 0, 0, &left, &top );
5082 CalcUnscrolledPosition( cw, ch, &right, &bottom );
5083 #endif
5084
5085 // avoid drawing grid lines past the last row and col
5086 //
5087 right = wxMin( right, GetColRight(m_numCols - 1) );
5088 bottom = wxMin( bottom, GetRowBottom(m_numRows - 1) );
5089
5090 dc.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID) );
5091
5092 // horizontal grid lines
5093 //
5094 int i;
5095 for ( i = 0; i < m_numRows; i++ )
5096 {
5097 int bot = GetRowBottom(i) - 1;
5098
5099 if ( bot > bottom )
5100 {
5101 break;
5102 }
5103
5104 if ( bot >= top )
5105 {
5106 dc.DrawLine( left, bot, right, bot );
5107 }
5108 }
5109
5110
5111 // vertical grid lines
5112 //
5113 for ( i = 0; i < m_numCols; i++ )
5114 {
5115 int colRight = GetColRight(i) - 1;
5116 if ( colRight > right )
5117 {
5118 break;
5119 }
5120
5121 if ( colRight >= left )
5122 {
5123 dc.DrawLine( colRight, top, colRight, bottom );
5124 }
5125 }
5126 }
5127
5128
5129 void wxGrid::DrawRowLabels( wxDC& dc )
5130 {
5131 if ( !m_numRows || !m_numCols ) return;
5132
5133 size_t i;
5134 size_t numLabels = m_rowLabelsExposed.GetCount();
5135
5136 for ( i = 0; i < numLabels; i++ )
5137 {
5138 DrawRowLabel( dc, m_rowLabelsExposed[i] );
5139 }
5140 }
5141
5142
5143 void wxGrid::DrawRowLabel( wxDC& dc, int row )
5144 {
5145 if ( GetRowHeight(row) <= 0 )
5146 return;
5147
5148 int rowTop = GetRowTop(row),
5149 rowBottom = GetRowBottom(row) - 1;
5150
5151 dc.SetPen( *wxBLACK_PEN );
5152 dc.DrawLine( m_rowLabelWidth-1, rowTop,
5153 m_rowLabelWidth-1, rowBottom );
5154
5155 dc.DrawLine( 0, rowBottom, m_rowLabelWidth-1, rowBottom );
5156
5157 dc.SetPen( *wxWHITE_PEN );
5158 dc.DrawLine( 0, rowTop, 0, rowBottom );
5159 dc.DrawLine( 0, rowTop, m_rowLabelWidth-1, rowTop );
5160
5161 dc.SetBackgroundMode( wxTRANSPARENT );
5162 dc.SetTextForeground( GetLabelTextColour() );
5163 dc.SetFont( GetLabelFont() );
5164
5165 int hAlign, vAlign;
5166 GetRowLabelAlignment( &hAlign, &vAlign );
5167
5168 wxRect rect;
5169 rect.SetX( 2 );
5170 rect.SetY( GetRowTop(row) + 2 );
5171 rect.SetWidth( m_rowLabelWidth - 4 );
5172 rect.SetHeight( GetRowHeight(row) - 4 );
5173 DrawTextRectangle( dc, GetRowLabelValue( row ), rect, hAlign, vAlign );
5174 }
5175
5176
5177 void wxGrid::DrawColLabels( wxDC& dc )
5178 {
5179 if ( !m_numRows || !m_numCols ) return;
5180
5181 size_t i;
5182 size_t numLabels = m_colLabelsExposed.GetCount();
5183
5184 for ( i = 0; i < numLabels; i++ )
5185 {
5186 DrawColLabel( dc, m_colLabelsExposed[i] );
5187 }
5188 }
5189
5190
5191 void wxGrid::DrawColLabel( wxDC& dc, int col )
5192 {
5193 if ( GetColWidth(col) <= 0 )
5194 return;
5195
5196 int colLeft = GetColLeft(col),
5197 colRight = GetColRight(col) - 1;
5198
5199 dc.SetPen( *wxBLACK_PEN );
5200 dc.DrawLine( colRight, 0,
5201 colRight, m_colLabelHeight-1 );
5202
5203 dc.DrawLine( colLeft, m_colLabelHeight-1,
5204 colRight, m_colLabelHeight-1 );
5205
5206 dc.SetPen( *wxWHITE_PEN );
5207 dc.DrawLine( colLeft, 0, colLeft, m_colLabelHeight-1 );
5208 dc.DrawLine( colLeft, 0, colRight, 0 );
5209
5210 dc.SetBackgroundMode( wxTRANSPARENT );
5211 dc.SetTextForeground( GetLabelTextColour() );
5212 dc.SetFont( GetLabelFont() );
5213
5214 dc.SetBackgroundMode( wxTRANSPARENT );
5215 dc.SetTextForeground( GetLabelTextColour() );
5216 dc.SetFont( GetLabelFont() );
5217
5218 int hAlign, vAlign;
5219 GetColLabelAlignment( &hAlign, &vAlign );
5220
5221 wxRect rect;
5222 rect.SetX( colLeft + 2 );
5223 rect.SetY( 2 );
5224 rect.SetWidth( GetColWidth(col) - 4 );
5225 rect.SetHeight( m_colLabelHeight - 4 );
5226 DrawTextRectangle( dc, GetColLabelValue( col ), rect, hAlign, vAlign );
5227 }
5228
5229
5230 void wxGrid::DrawTextRectangle( wxDC& dc,
5231 const wxString& value,
5232 const wxRect& rect,
5233 int horizAlign,
5234 int vertAlign )
5235 {
5236 long textWidth, textHeight;
5237 long lineWidth, lineHeight;
5238 wxArrayString lines;
5239
5240 dc.SetClippingRegion( rect );
5241 StringToLines( value, lines );
5242 if ( lines.GetCount() )
5243 {
5244 GetTextBoxSize( dc, lines, &textWidth, &textHeight );
5245 dc.GetTextExtent( lines[0], &lineWidth, &lineHeight );
5246
5247 float x, y;
5248 switch ( horizAlign )
5249 {
5250 case wxRIGHT:
5251 x = rect.x + (rect.width - textWidth - 1);
5252 break;
5253
5254 case wxCENTRE:
5255 x = rect.x + ((rect.width - textWidth)/2);
5256 break;
5257
5258 case wxLEFT:
5259 default:
5260 x = rect.x + 1;
5261 break;
5262 }
5263
5264 switch ( vertAlign )
5265 {
5266 case wxBOTTOM:
5267 y = rect.y + (rect.height - textHeight - 1);
5268 break;
5269
5270 case wxCENTRE:
5271 y = rect.y + ((rect.height - textHeight)/2);
5272 break;
5273
5274 case wxTOP:
5275 default:
5276 y = rect.y + 1;
5277 break;
5278 }
5279
5280 for ( size_t i = 0; i < lines.GetCount(); i++ )
5281 {
5282 dc.DrawText( lines[i], (long)x, (long)y );
5283 y += lineHeight;
5284 }
5285 }
5286
5287 dc.DestroyClippingRegion();
5288 }
5289
5290
5291 // Split multi line text up into an array of strings. Any existing
5292 // contents of the string array are preserved.
5293 //
5294 void wxGrid::StringToLines( const wxString& value, wxArrayString& lines )
5295 {
5296 int startPos = 0;
5297 int pos;
5298 wxString eol = wxTextFile::GetEOL( wxTextFileType_Unix );
5299 wxString tVal = wxTextFile::Translate( value, wxTextFileType_Unix );
5300
5301 while ( startPos < (int)tVal.Length() )
5302 {
5303 pos = tVal.Mid(startPos).Find( eol );
5304 if ( pos < 0 )
5305 {
5306 break;
5307 }
5308 else if ( pos == 0 )
5309 {
5310 lines.Add( wxEmptyString );
5311 }
5312 else
5313 {
5314 lines.Add( value.Mid(startPos, pos) );
5315 }
5316 startPos += pos+1;
5317 }
5318 if ( startPos < (int)value.Length() )
5319 {
5320 lines.Add( value.Mid( startPos ) );
5321 }
5322 }
5323
5324
5325 void wxGrid::GetTextBoxSize( wxDC& dc,
5326 wxArrayString& lines,
5327 long *width, long *height )
5328 {
5329 long w = 0;
5330 long h = 0;
5331 long lineW, lineH;
5332
5333 size_t i;
5334 for ( i = 0; i < lines.GetCount(); i++ )
5335 {
5336 dc.GetTextExtent( lines[i], &lineW, &lineH );
5337 w = wxMax( w, lineW );
5338 h += lineH;
5339 }
5340
5341 *width = w;
5342 *height = h;
5343 }
5344
5345
5346 //
5347 // ------ Edit control functions
5348 //
5349
5350
5351 void wxGrid::EnableEditing( bool edit )
5352 {
5353 // TODO: improve this ?
5354 //
5355 if ( edit != m_editable )
5356 {
5357 m_editable = edit;
5358
5359 // FIXME IMHO this won't disable the edit control if edit == FALSE
5360 // because of the check in the beginning of
5361 // EnableCellEditControl() just below (VZ)
5362 EnableCellEditControl(m_editable);
5363 }
5364 }
5365
5366
5367 void wxGrid::EnableCellEditControl( bool enable )
5368 {
5369 if (! m_editable)
5370 return;
5371
5372 if ( m_currentCellCoords == wxGridNoCellCoords )
5373 SetCurrentCell( 0, 0 );
5374
5375 if ( enable != m_cellEditCtrlEnabled )
5376 {
5377 // TODO allow the app to Veto() this event?
5378 SendEvent(enable ? wxEVT_GRID_EDITOR_SHOWN : wxEVT_GRID_EDITOR_HIDDEN);
5379
5380 if ( enable )
5381 {
5382 // this should be checked by the caller!
5383 wxASSERT_MSG( CanEnableCellControl(),
5384 _T("can't enable editing for this cell!") );
5385
5386 // do it before ShowCellEditControl()
5387 m_cellEditCtrlEnabled = enable;
5388
5389 ShowCellEditControl();
5390 }
5391 else
5392 {
5393 HideCellEditControl();
5394 SaveEditControlValue();
5395
5396 // do it after HideCellEditControl()
5397 m_cellEditCtrlEnabled = enable;
5398 }
5399 }
5400 }
5401
5402 bool wxGrid::IsCurrentCellReadOnly() const
5403 {
5404 // const_cast
5405 wxGridCellAttr* attr = ((wxGrid *)this)->GetCellAttr(m_currentCellCoords);
5406 bool readonly = attr->IsReadOnly();
5407 attr->DecRef();
5408
5409 return readonly;
5410 }
5411
5412 bool wxGrid::CanEnableCellControl() const
5413 {
5414 return m_editable && !IsCurrentCellReadOnly();
5415 }
5416
5417 bool wxGrid::IsCellEditControlEnabled() const
5418 {
5419 // the cell edit control might be disable for all cells or just for the
5420 // current one if it's read only
5421 return m_cellEditCtrlEnabled ? !IsCurrentCellReadOnly() : FALSE;
5422 }
5423
5424 void wxGrid::ShowCellEditControl()
5425 {
5426 if ( IsCellEditControlEnabled() )
5427 {
5428 if ( !IsVisible( m_currentCellCoords ) )
5429 {
5430 return;
5431 }
5432 else
5433 {
5434 wxRect rect = CellToRect( m_currentCellCoords );
5435 int row = m_currentCellCoords.GetRow();
5436 int col = m_currentCellCoords.GetCol();
5437
5438 // convert to scrolled coords
5439 //
5440 CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
5441
5442 // done in PaintBackground()
5443 #if 0
5444 // erase the highlight and the cell contents because the editor
5445 // might not cover the entire cell
5446 wxClientDC dc( m_gridWin );
5447 PrepareDC( dc );
5448 dc.SetBrush(*wxLIGHT_GREY_BRUSH); //wxBrush(attr->GetBackgroundColour(), wxSOLID));
5449 dc.SetPen(*wxTRANSPARENT_PEN);
5450 dc.DrawRectangle(rect);
5451 #endif // 0
5452
5453 // cell is shifted by one pixel
5454 rect.x--;
5455 rect.y--;
5456
5457 wxGridCellAttr* attr = GetCellAttr(row, col);
5458 wxGridCellEditor* editor = attr->GetEditor(this, row, col);
5459 if ( !editor->IsCreated() )
5460 {
5461 editor->Create(m_gridWin, -1,
5462 new wxGridCellEditorEvtHandler(this, editor));
5463 }
5464
5465 editor->Show( TRUE, attr );
5466
5467 editor->SetSize( rect );
5468
5469 editor->BeginEdit(row, col, this);
5470 attr->DecRef();
5471 }
5472 }
5473 }
5474
5475
5476 void wxGrid::HideCellEditControl()
5477 {
5478 if ( IsCellEditControlEnabled() )
5479 {
5480 int row = m_currentCellCoords.GetRow();
5481 int col = m_currentCellCoords.GetCol();
5482
5483 wxGridCellAttr* attr = GetCellAttr(row, col);
5484 attr->GetEditor(this, row, col)->Show( FALSE );
5485 attr->DecRef();
5486 m_gridWin->SetFocus();
5487 }
5488 }
5489
5490
5491 void wxGrid::SaveEditControlValue()
5492 {
5493 if ( IsCellEditControlEnabled() )
5494 {
5495 int row = m_currentCellCoords.GetRow();
5496 int col = m_currentCellCoords.GetCol();
5497
5498 wxGridCellAttr* attr = GetCellAttr(row, col);
5499 wxGridCellEditor* editor = attr->GetEditor(this, row, col);
5500 bool changed = editor->EndEdit(row, col, this);
5501
5502 attr->DecRef();
5503
5504 if (changed)
5505 {
5506 SendEvent( wxEVT_GRID_CELL_CHANGE,
5507 m_currentCellCoords.GetRow(),
5508 m_currentCellCoords.GetCol() );
5509 }
5510 }
5511 }
5512
5513
5514 //
5515 // ------ Grid location functions
5516 // Note that all of these functions work with the logical coordinates of
5517 // grid cells and labels so you will need to convert from device
5518 // coordinates for mouse events etc.
5519 //
5520
5521 void wxGrid::XYToCell( int x, int y, wxGridCellCoords& coords )
5522 {
5523 int row = YToRow(y);
5524 int col = XToCol(x);
5525
5526 if ( row == -1 || col == -1 )
5527 {
5528 coords = wxGridNoCellCoords;
5529 }
5530 else
5531 {
5532 coords.Set( row, col );
5533 }
5534 }
5535
5536
5537 int wxGrid::YToRow( int y )
5538 {
5539 int i;
5540
5541 for ( i = 0; i < m_numRows; i++ )
5542 {
5543 if ( y < GetRowBottom(i) )
5544 return i;
5545 }
5546
5547 return -1;
5548 }
5549
5550
5551 int wxGrid::XToCol( int x )
5552 {
5553 int i;
5554
5555 for ( i = 0; i < m_numCols; i++ )
5556 {
5557 if ( x < GetColRight(i) )
5558 return i;
5559 }
5560
5561 return -1;
5562 }
5563
5564
5565 // return the row number that that the y coord is near the edge of, or
5566 // -1 if not near an edge
5567 //
5568 int wxGrid::YToEdgeOfRow( int y )
5569 {
5570 int i, d;
5571
5572 for ( i = 0; i < m_numRows; i++ )
5573 {
5574 if ( GetRowHeight(i) > WXGRID_LABEL_EDGE_ZONE )
5575 {
5576 d = abs( y - GetRowBottom(i) );
5577 if ( d < WXGRID_LABEL_EDGE_ZONE )
5578 return i;
5579 }
5580 }
5581
5582 return -1;
5583 }
5584
5585
5586 // return the col number that that the x coord is near the edge of, or
5587 // -1 if not near an edge
5588 //
5589 int wxGrid::XToEdgeOfCol( int x )
5590 {
5591 int i, d;
5592
5593 for ( i = 0; i < m_numCols; i++ )
5594 {
5595 if ( GetColWidth(i) > WXGRID_LABEL_EDGE_ZONE )
5596 {
5597 d = abs( x - GetColRight(i) );
5598 if ( d < WXGRID_LABEL_EDGE_ZONE )
5599 return i;
5600 }
5601 }
5602
5603 return -1;
5604 }
5605
5606
5607 wxRect wxGrid::CellToRect( int row, int col )
5608 {
5609 wxRect rect( -1, -1, -1, -1 );
5610
5611 if ( row >= 0 && row < m_numRows &&
5612 col >= 0 && col < m_numCols )
5613 {
5614 rect.x = GetColLeft(col);
5615 rect.y = GetRowTop(row);
5616 rect.width = GetColWidth(col);
5617 rect.height = GetRowHeight(row);
5618 }
5619
5620 return rect;
5621 }
5622
5623
5624 bool wxGrid::IsVisible( int row, int col, bool wholeCellVisible )
5625 {
5626 // get the cell rectangle in logical coords
5627 //
5628 wxRect r( CellToRect( row, col ) );
5629
5630 // convert to device coords
5631 //
5632 int left, top, right, bottom;
5633 CalcScrolledPosition( r.GetLeft(), r.GetTop(), &left, &top );
5634 CalcScrolledPosition( r.GetRight(), r.GetBottom(), &right, &bottom );
5635
5636 // check against the client area of the grid window
5637 //
5638 int cw, ch;
5639 m_gridWin->GetClientSize( &cw, &ch );
5640
5641 if ( wholeCellVisible )
5642 {
5643 // is the cell wholly visible ?
5644 //
5645 return ( left >= 0 && right <= cw &&
5646 top >= 0 && bottom <= ch );
5647 }
5648 else
5649 {
5650 // is the cell partly visible ?
5651 //
5652 return ( ((left >=0 && left < cw) || (right > 0 && right <= cw)) &&
5653 ((top >=0 && top < ch) || (bottom > 0 && bottom <= ch)) );
5654 }
5655 }
5656
5657
5658 // make the specified cell location visible by doing a minimal amount
5659 // of scrolling
5660 //
5661 void wxGrid::MakeCellVisible( int row, int col )
5662 {
5663 int i;
5664 int xpos = -1, ypos = -1;
5665
5666 if ( row >= 0 && row < m_numRows &&
5667 col >= 0 && col < m_numCols )
5668 {
5669 // get the cell rectangle in logical coords
5670 //
5671 wxRect r( CellToRect( row, col ) );
5672
5673 // convert to device coords
5674 //
5675 int left, top, right, bottom;
5676 CalcScrolledPosition( r.GetLeft(), r.GetTop(), &left, &top );
5677 CalcScrolledPosition( r.GetRight(), r.GetBottom(), &right, &bottom );
5678
5679 int cw, ch;
5680 m_gridWin->GetClientSize( &cw, &ch );
5681
5682 if ( top < 0 )
5683 {
5684 ypos = r.GetTop();
5685 }
5686 else if ( bottom > ch )
5687 {
5688 int h = r.GetHeight();
5689 ypos = r.GetTop();
5690 for ( i = row-1; i >= 0; i-- )
5691 {
5692 int rowHeight = GetRowHeight(i);
5693 if ( h + rowHeight > ch )
5694 break;
5695
5696 h += rowHeight;
5697 ypos -= rowHeight;
5698 }
5699
5700 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
5701 // have rounding errors (this is important, because if we do, we
5702 // might not scroll at all and some cells won't be redrawn)
5703 ypos += GRID_SCROLL_LINE / 2;
5704 }
5705
5706 if ( left < 0 )
5707 {
5708 xpos = r.GetLeft();
5709 }
5710 else if ( right > cw )
5711 {
5712 int w = r.GetWidth();
5713 xpos = r.GetLeft();
5714 for ( i = col-1; i >= 0; i-- )
5715 {
5716 int colWidth = GetColWidth(i);
5717 if ( w + colWidth > cw )
5718 break;
5719
5720 w += colWidth;
5721 xpos -= colWidth;
5722 }
5723
5724 // see comment for ypos above
5725 xpos += GRID_SCROLL_LINE / 2;
5726 }
5727
5728 if ( xpos != -1 || ypos != -1 )
5729 {
5730 if ( xpos != -1 ) xpos /= GRID_SCROLL_LINE;
5731 if ( ypos != -1 ) ypos /= GRID_SCROLL_LINE;
5732 Scroll( xpos, ypos );
5733 AdjustScrollbars();
5734 }
5735 }
5736 }
5737
5738
5739 //
5740 // ------ Grid cursor movement functions
5741 //
5742
5743 bool wxGrid::MoveCursorUp()
5744 {
5745 if ( m_currentCellCoords != wxGridNoCellCoords &&
5746 m_currentCellCoords.GetRow() > 0 )
5747 {
5748 MakeCellVisible( m_currentCellCoords.GetRow() - 1,
5749 m_currentCellCoords.GetCol() );
5750
5751 SetCurrentCell( m_currentCellCoords.GetRow() - 1,
5752 m_currentCellCoords.GetCol() );
5753
5754 return TRUE;
5755 }
5756
5757 return FALSE;
5758 }
5759
5760
5761 bool wxGrid::MoveCursorDown()
5762 {
5763 // TODO: allow for scrolling
5764 //
5765 if ( m_currentCellCoords != wxGridNoCellCoords &&
5766 m_currentCellCoords.GetRow() < m_numRows-1 )
5767 {
5768 MakeCellVisible( m_currentCellCoords.GetRow() + 1,
5769 m_currentCellCoords.GetCol() );
5770
5771 SetCurrentCell( m_currentCellCoords.GetRow() + 1,
5772 m_currentCellCoords.GetCol() );
5773
5774 return TRUE;
5775 }
5776
5777 return FALSE;
5778 }
5779
5780
5781 bool wxGrid::MoveCursorLeft()
5782 {
5783 if ( m_currentCellCoords != wxGridNoCellCoords &&
5784 m_currentCellCoords.GetCol() > 0 )
5785 {
5786 MakeCellVisible( m_currentCellCoords.GetRow(),
5787 m_currentCellCoords.GetCol() - 1 );
5788
5789 SetCurrentCell( m_currentCellCoords.GetRow(),
5790 m_currentCellCoords.GetCol() - 1 );
5791
5792 return TRUE;
5793 }
5794
5795 return FALSE;
5796 }
5797
5798
5799 bool wxGrid::MoveCursorRight()
5800 {
5801 if ( m_currentCellCoords != wxGridNoCellCoords &&
5802 m_currentCellCoords.GetCol() < m_numCols - 1 )
5803 {
5804 MakeCellVisible( m_currentCellCoords.GetRow(),
5805 m_currentCellCoords.GetCol() + 1 );
5806
5807 SetCurrentCell( m_currentCellCoords.GetRow(),
5808 m_currentCellCoords.GetCol() + 1 );
5809
5810 return TRUE;
5811 }
5812
5813 return FALSE;
5814 }
5815
5816
5817 bool wxGrid::MovePageUp()
5818 {
5819 if ( m_currentCellCoords == wxGridNoCellCoords ) return FALSE;
5820
5821 int row = m_currentCellCoords.GetRow();
5822 if ( row > 0 )
5823 {
5824 int cw, ch;
5825 m_gridWin->GetClientSize( &cw, &ch );
5826
5827 int y = GetRowTop(row);
5828 int newRow = YToRow( y - ch + 1 );
5829 if ( newRow == -1 )
5830 {
5831 newRow = 0;
5832 }
5833 else if ( newRow == row )
5834 {
5835 newRow = row - 1;
5836 }
5837
5838 MakeCellVisible( newRow, m_currentCellCoords.GetCol() );
5839 SetCurrentCell( newRow, m_currentCellCoords.GetCol() );
5840
5841 return TRUE;
5842 }
5843
5844 return FALSE;
5845 }
5846
5847 bool wxGrid::MovePageDown()
5848 {
5849 if ( m_currentCellCoords == wxGridNoCellCoords ) return FALSE;
5850
5851 int row = m_currentCellCoords.GetRow();
5852 if ( row < m_numRows )
5853 {
5854 int cw, ch;
5855 m_gridWin->GetClientSize( &cw, &ch );
5856
5857 int y = GetRowTop(row);
5858 int newRow = YToRow( y + ch );
5859 if ( newRow == -1 )
5860 {
5861 newRow = m_numRows - 1;
5862 }
5863 else if ( newRow == row )
5864 {
5865 newRow = row + 1;
5866 }
5867
5868 MakeCellVisible( newRow, m_currentCellCoords.GetCol() );
5869 SetCurrentCell( newRow, m_currentCellCoords.GetCol() );
5870
5871 return TRUE;
5872 }
5873
5874 return FALSE;
5875 }
5876
5877 bool wxGrid::MoveCursorUpBlock()
5878 {
5879 if ( m_table &&
5880 m_currentCellCoords != wxGridNoCellCoords &&
5881 m_currentCellCoords.GetRow() > 0 )
5882 {
5883 int row = m_currentCellCoords.GetRow();
5884 int col = m_currentCellCoords.GetCol();
5885
5886 if ( m_table->IsEmptyCell(row, col) )
5887 {
5888 // starting in an empty cell: find the next block of
5889 // non-empty cells
5890 //
5891 while ( row > 0 )
5892 {
5893 row-- ;
5894 if ( !(m_table->IsEmptyCell(row, col)) ) break;
5895 }
5896 }
5897 else if ( m_table->IsEmptyCell(row-1, col) )
5898 {
5899 // starting at the top of a block: find the next block
5900 //
5901 row--;
5902 while ( row > 0 )
5903 {
5904 row-- ;
5905 if ( !(m_table->IsEmptyCell(row, col)) ) break;
5906 }
5907 }
5908 else
5909 {
5910 // starting within a block: find the top of the block
5911 //
5912 while ( row > 0 )
5913 {
5914 row-- ;
5915 if ( m_table->IsEmptyCell(row, col) )
5916 {
5917 row++ ;
5918 break;
5919 }
5920 }
5921 }
5922
5923 MakeCellVisible( row, col );
5924 SetCurrentCell( row, col );
5925
5926 return TRUE;
5927 }
5928
5929 return FALSE;
5930 }
5931
5932 bool wxGrid::MoveCursorDownBlock()
5933 {
5934 if ( m_table &&
5935 m_currentCellCoords != wxGridNoCellCoords &&
5936 m_currentCellCoords.GetRow() < m_numRows-1 )
5937 {
5938 int row = m_currentCellCoords.GetRow();
5939 int col = m_currentCellCoords.GetCol();
5940
5941 if ( m_table->IsEmptyCell(row, col) )
5942 {
5943 // starting in an empty cell: find the next block of
5944 // non-empty cells
5945 //
5946 while ( row < m_numRows-1 )
5947 {
5948 row++ ;
5949 if ( !(m_table->IsEmptyCell(row, col)) ) break;
5950 }
5951 }
5952 else if ( m_table->IsEmptyCell(row+1, col) )
5953 {
5954 // starting at the bottom of a block: find the next block
5955 //
5956 row++;
5957 while ( row < m_numRows-1 )
5958 {
5959 row++ ;
5960 if ( !(m_table->IsEmptyCell(row, col)) ) break;
5961 }
5962 }
5963 else
5964 {
5965 // starting within a block: find the bottom of the block
5966 //
5967 while ( row < m_numRows-1 )
5968 {
5969 row++ ;
5970 if ( m_table->IsEmptyCell(row, col) )
5971 {
5972 row-- ;
5973 break;
5974 }
5975 }
5976 }
5977
5978 MakeCellVisible( row, col );
5979 SetCurrentCell( row, col );
5980
5981 return TRUE;
5982 }
5983
5984 return FALSE;
5985 }
5986
5987 bool wxGrid::MoveCursorLeftBlock()
5988 {
5989 if ( m_table &&
5990 m_currentCellCoords != wxGridNoCellCoords &&
5991 m_currentCellCoords.GetCol() > 0 )
5992 {
5993 int row = m_currentCellCoords.GetRow();
5994 int col = m_currentCellCoords.GetCol();
5995
5996 if ( m_table->IsEmptyCell(row, col) )
5997 {
5998 // starting in an empty cell: find the next block of
5999 // non-empty cells
6000 //
6001 while ( col > 0 )
6002 {
6003 col-- ;
6004 if ( !(m_table->IsEmptyCell(row, col)) ) break;
6005 }
6006 }
6007 else if ( m_table->IsEmptyCell(row, col-1) )
6008 {
6009 // starting at the left of a block: find the next block
6010 //
6011 col--;
6012 while ( col > 0 )
6013 {
6014 col-- ;
6015 if ( !(m_table->IsEmptyCell(row, col)) ) break;
6016 }
6017 }
6018 else
6019 {
6020 // starting within a block: find the left of the block
6021 //
6022 while ( col > 0 )
6023 {
6024 col-- ;
6025 if ( m_table->IsEmptyCell(row, col) )
6026 {
6027 col++ ;
6028 break;
6029 }
6030 }
6031 }
6032
6033 MakeCellVisible( row, col );
6034 SetCurrentCell( row, col );
6035
6036 return TRUE;
6037 }
6038
6039 return FALSE;
6040 }
6041
6042 bool wxGrid::MoveCursorRightBlock()
6043 {
6044 if ( m_table &&
6045 m_currentCellCoords != wxGridNoCellCoords &&
6046 m_currentCellCoords.GetCol() < m_numCols-1 )
6047 {
6048 int row = m_currentCellCoords.GetRow();
6049 int col = m_currentCellCoords.GetCol();
6050
6051 if ( m_table->IsEmptyCell(row, col) )
6052 {
6053 // starting in an empty cell: find the next block of
6054 // non-empty cells
6055 //
6056 while ( col < m_numCols-1 )
6057 {
6058 col++ ;
6059 if ( !(m_table->IsEmptyCell(row, col)) ) break;
6060 }
6061 }
6062 else if ( m_table->IsEmptyCell(row, col+1) )
6063 {
6064 // starting at the right of a block: find the next block
6065 //
6066 col++;
6067 while ( col < m_numCols-1 )
6068 {
6069 col++ ;
6070 if ( !(m_table->IsEmptyCell(row, col)) ) break;
6071 }
6072 }
6073 else
6074 {
6075 // starting within a block: find the right of the block
6076 //
6077 while ( col < m_numCols-1 )
6078 {
6079 col++ ;
6080 if ( m_table->IsEmptyCell(row, col) )
6081 {
6082 col-- ;
6083 break;
6084 }
6085 }
6086 }
6087
6088 MakeCellVisible( row, col );
6089 SetCurrentCell( row, col );
6090
6091 return TRUE;
6092 }
6093
6094 return FALSE;
6095 }
6096
6097
6098
6099 //
6100 // ------ Label values and formatting
6101 //
6102
6103 void wxGrid::GetRowLabelAlignment( int *horiz, int *vert )
6104 {
6105 *horiz = m_rowLabelHorizAlign;
6106 *vert = m_rowLabelVertAlign;
6107 }
6108
6109 void wxGrid::GetColLabelAlignment( int *horiz, int *vert )
6110 {
6111 *horiz = m_colLabelHorizAlign;
6112 *vert = m_colLabelVertAlign;
6113 }
6114
6115 wxString wxGrid::GetRowLabelValue( int row )
6116 {
6117 if ( m_table )
6118 {
6119 return m_table->GetRowLabelValue( row );
6120 }
6121 else
6122 {
6123 wxString s;
6124 s << row;
6125 return s;
6126 }
6127 }
6128
6129 wxString wxGrid::GetColLabelValue( int col )
6130 {
6131 if ( m_table )
6132 {
6133 return m_table->GetColLabelValue( col );
6134 }
6135 else
6136 {
6137 wxString s;
6138 s << col;
6139 return s;
6140 }
6141 }
6142
6143
6144 void wxGrid::SetRowLabelSize( int width )
6145 {
6146 width = wxMax( width, 0 );
6147 if ( width != m_rowLabelWidth )
6148 {
6149 if ( width == 0 )
6150 {
6151 m_rowLabelWin->Show( FALSE );
6152 m_cornerLabelWin->Show( FALSE );
6153 }
6154 else if ( m_rowLabelWidth == 0 )
6155 {
6156 m_rowLabelWin->Show( TRUE );
6157 if ( m_colLabelHeight > 0 ) m_cornerLabelWin->Show( TRUE );
6158 }
6159
6160 m_rowLabelWidth = width;
6161 CalcWindowSizes();
6162 Refresh( TRUE );
6163 }
6164 }
6165
6166
6167 void wxGrid::SetColLabelSize( int height )
6168 {
6169 height = wxMax( height, 0 );
6170 if ( height != m_colLabelHeight )
6171 {
6172 if ( height == 0 )
6173 {
6174 m_colLabelWin->Show( FALSE );
6175 m_cornerLabelWin->Show( FALSE );
6176 }
6177 else if ( m_colLabelHeight == 0 )
6178 {
6179 m_colLabelWin->Show( TRUE );
6180 if ( m_rowLabelWidth > 0 ) m_cornerLabelWin->Show( TRUE );
6181 }
6182
6183 m_colLabelHeight = height;
6184 CalcWindowSizes();
6185 Refresh( TRUE );
6186 }
6187 }
6188
6189
6190 void wxGrid::SetLabelBackgroundColour( const wxColour& colour )
6191 {
6192 if ( m_labelBackgroundColour != colour )
6193 {
6194 m_labelBackgroundColour = colour;
6195 m_rowLabelWin->SetBackgroundColour( colour );
6196 m_colLabelWin->SetBackgroundColour( colour );
6197 m_cornerLabelWin->SetBackgroundColour( colour );
6198
6199 if ( !GetBatchCount() )
6200 {
6201 m_rowLabelWin->Refresh();
6202 m_colLabelWin->Refresh();
6203 m_cornerLabelWin->Refresh();
6204 }
6205 }
6206 }
6207
6208 void wxGrid::SetLabelTextColour( const wxColour& colour )
6209 {
6210 if ( m_labelTextColour != colour )
6211 {
6212 m_labelTextColour = colour;
6213 if ( !GetBatchCount() )
6214 {
6215 m_rowLabelWin->Refresh();
6216 m_colLabelWin->Refresh();
6217 }
6218 }
6219 }
6220
6221 void wxGrid::SetLabelFont( const wxFont& font )
6222 {
6223 m_labelFont = font;
6224 if ( !GetBatchCount() )
6225 {
6226 m_rowLabelWin->Refresh();
6227 m_colLabelWin->Refresh();
6228 }
6229 }
6230
6231 void wxGrid::SetRowLabelAlignment( int horiz, int vert )
6232 {
6233 if ( horiz == wxLEFT || horiz == wxCENTRE || horiz == wxRIGHT )
6234 {
6235 m_rowLabelHorizAlign = horiz;
6236 }
6237
6238 if ( vert == wxTOP || vert == wxCENTRE || vert == wxBOTTOM )
6239 {
6240 m_rowLabelVertAlign = vert;
6241 }
6242
6243 if ( !GetBatchCount() )
6244 {
6245 m_rowLabelWin->Refresh();
6246 }
6247 }
6248
6249 void wxGrid::SetColLabelAlignment( int horiz, int vert )
6250 {
6251 if ( horiz == wxLEFT || horiz == wxCENTRE || horiz == wxRIGHT )
6252 {
6253 m_colLabelHorizAlign = horiz;
6254 }
6255
6256 if ( vert == wxTOP || vert == wxCENTRE || vert == wxBOTTOM )
6257 {
6258 m_colLabelVertAlign = vert;
6259 }
6260
6261 if ( !GetBatchCount() )
6262 {
6263 m_colLabelWin->Refresh();
6264 }
6265 }
6266
6267 void wxGrid::SetRowLabelValue( int row, const wxString& s )
6268 {
6269 if ( m_table )
6270 {
6271 m_table->SetRowLabelValue( row, s );
6272 if ( !GetBatchCount() )
6273 {
6274 wxRect rect = CellToRect( row, 0);
6275 if ( rect.height > 0 )
6276 {
6277 CalcScrolledPosition(0, rect.y, &rect.x, &rect.y);
6278 rect.x = m_left;
6279 rect.width = m_rowLabelWidth;
6280 m_rowLabelWin->Refresh( TRUE, &rect );
6281 }
6282 }
6283 }
6284 }
6285
6286 void wxGrid::SetColLabelValue( int col, const wxString& s )
6287 {
6288 if ( m_table )
6289 {
6290 m_table->SetColLabelValue( col, s );
6291 if ( !GetBatchCount() )
6292 {
6293 wxRect rect = CellToRect( 0, col );
6294 if ( rect.width > 0 )
6295 {
6296 CalcScrolledPosition(rect.x, 0, &rect.x, &rect.y);
6297 rect.y = m_top;
6298 rect.height = m_colLabelHeight;
6299 m_colLabelWin->Refresh( TRUE, &rect );
6300 }
6301 }
6302 }
6303 }
6304
6305 void wxGrid::SetGridLineColour( const wxColour& colour )
6306 {
6307 if ( m_gridLineColour != colour )
6308 {
6309 m_gridLineColour = colour;
6310
6311 wxClientDC dc( m_gridWin );
6312 PrepareDC( dc );
6313 DrawAllGridLines( dc, wxRegion() );
6314 }
6315 }
6316
6317 void wxGrid::EnableGridLines( bool enable )
6318 {
6319 if ( enable != m_gridLinesEnabled )
6320 {
6321 m_gridLinesEnabled = enable;
6322
6323 if ( !GetBatchCount() )
6324 {
6325 if ( enable )
6326 {
6327 wxClientDC dc( m_gridWin );
6328 PrepareDC( dc );
6329 DrawAllGridLines( dc, wxRegion() );
6330 }
6331 else
6332 {
6333 m_gridWin->Refresh();
6334 }
6335 }
6336 }
6337 }
6338
6339
6340 int wxGrid::GetDefaultRowSize()
6341 {
6342 return m_defaultRowHeight;
6343 }
6344
6345 int wxGrid::GetRowSize( int row )
6346 {
6347 wxCHECK_MSG( row >= 0 && row < m_numRows, 0, _T("invalid row index") );
6348
6349 return GetRowHeight(row);
6350 }
6351
6352 int wxGrid::GetDefaultColSize()
6353 {
6354 return m_defaultColWidth;
6355 }
6356
6357 int wxGrid::GetColSize( int col )
6358 {
6359 wxCHECK_MSG( col >= 0 && col < m_numCols, 0, _T("invalid column index") );
6360
6361 return GetColWidth(col);
6362 }
6363
6364 // ============================================================================
6365 // access to the grid attributes: each of them has a default value in the grid
6366 // itself and may be overidden on a per-cell basis
6367 // ============================================================================
6368
6369 // ----------------------------------------------------------------------------
6370 // setting default attributes
6371 // ----------------------------------------------------------------------------
6372
6373 void wxGrid::SetDefaultCellBackgroundColour( const wxColour& col )
6374 {
6375 m_defaultCellAttr->SetBackgroundColour(col);
6376 #ifdef __WXGTK__
6377 m_gridWin->SetBackgroundColour(col);
6378 #endif
6379 }
6380
6381 void wxGrid::SetDefaultCellTextColour( const wxColour& col )
6382 {
6383 m_defaultCellAttr->SetTextColour(col);
6384 }
6385
6386 void wxGrid::SetDefaultCellAlignment( int horiz, int vert )
6387 {
6388 m_defaultCellAttr->SetAlignment(horiz, vert);
6389 }
6390
6391 void wxGrid::SetDefaultCellFont( const wxFont& font )
6392 {
6393 m_defaultCellAttr->SetFont(font);
6394 }
6395
6396 void wxGrid::SetDefaultRenderer(wxGridCellRenderer *renderer)
6397 {
6398 m_defaultCellAttr->SetRenderer(renderer);
6399 }
6400
6401 void wxGrid::SetDefaultEditor(wxGridCellEditor *editor)
6402 {
6403 m_defaultCellAttr->SetEditor(editor);
6404 }
6405
6406 // ----------------------------------------------------------------------------
6407 // access to the default attrbiutes
6408 // ----------------------------------------------------------------------------
6409
6410 wxColour wxGrid::GetDefaultCellBackgroundColour()
6411 {
6412 return m_defaultCellAttr->GetBackgroundColour();
6413 }
6414
6415 wxColour wxGrid::GetDefaultCellTextColour()
6416 {
6417 return m_defaultCellAttr->GetTextColour();
6418 }
6419
6420 wxFont wxGrid::GetDefaultCellFont()
6421 {
6422 return m_defaultCellAttr->GetFont();
6423 }
6424
6425 void wxGrid::GetDefaultCellAlignment( int *horiz, int *vert )
6426 {
6427 m_defaultCellAttr->GetAlignment(horiz, vert);
6428 }
6429
6430 wxGridCellRenderer *wxGrid::GetDefaultRenderer() const
6431 {
6432 return m_defaultCellAttr->GetRenderer(NULL,0,0);
6433 }
6434
6435 wxGridCellEditor *wxGrid::GetDefaultEditor() const
6436 {
6437 return m_defaultCellAttr->GetEditor(NULL,0,0);
6438 }
6439
6440 // ----------------------------------------------------------------------------
6441 // access to cell attributes
6442 // ----------------------------------------------------------------------------
6443
6444 wxColour wxGrid::GetCellBackgroundColour(int row, int col)
6445 {
6446 wxGridCellAttr *attr = GetCellAttr(row, col);
6447 wxColour colour = attr->GetBackgroundColour();
6448 attr->SafeDecRef();
6449 return colour;
6450 }
6451
6452 wxColour wxGrid::GetCellTextColour( int row, int col )
6453 {
6454 wxGridCellAttr *attr = GetCellAttr(row, col);
6455 wxColour colour = attr->GetTextColour();
6456 attr->SafeDecRef();
6457 return colour;
6458 }
6459
6460 wxFont wxGrid::GetCellFont( int row, int col )
6461 {
6462 wxGridCellAttr *attr = GetCellAttr(row, col);
6463 wxFont font = attr->GetFont();
6464 attr->SafeDecRef();
6465 return font;
6466 }
6467
6468 void wxGrid::GetCellAlignment( int row, int col, int *horiz, int *vert )
6469 {
6470 wxGridCellAttr *attr = GetCellAttr(row, col);
6471 attr->GetAlignment(horiz, vert);
6472 attr->SafeDecRef();
6473 }
6474
6475 wxGridCellRenderer* wxGrid::GetCellRenderer(int row, int col)
6476 {
6477 wxGridCellAttr* attr = GetCellAttr(row, col);
6478 wxGridCellRenderer* renderer = attr->GetRenderer(this, row, col);
6479 attr->DecRef();
6480 return renderer;
6481 }
6482
6483 wxGridCellEditor* wxGrid::GetCellEditor(int row, int col)
6484 {
6485 wxGridCellAttr* attr = GetCellAttr(row, col);
6486 wxGridCellEditor* editor = attr->GetEditor(this, row, col);
6487 attr->DecRef();
6488 return editor;
6489 }
6490
6491 bool wxGrid::IsReadOnly(int row, int col) const
6492 {
6493 wxGridCellAttr* attr = GetCellAttr(row, col);
6494 bool isReadOnly = attr->IsReadOnly();
6495 attr->DecRef();
6496 return isReadOnly;
6497 }
6498
6499 // ----------------------------------------------------------------------------
6500 // attribute support: cache, automatic provider creation, ...
6501 // ----------------------------------------------------------------------------
6502
6503 bool wxGrid::CanHaveAttributes()
6504 {
6505 if ( !m_table )
6506 {
6507 return FALSE;
6508 }
6509
6510 return m_table->CanHaveAttributes();
6511 }
6512
6513 void wxGrid::ClearAttrCache()
6514 {
6515 if ( m_attrCache.row != -1 )
6516 {
6517 m_attrCache.attr->SafeDecRef();
6518 m_attrCache.row = -1;
6519 }
6520 }
6521
6522 void wxGrid::CacheAttr(int row, int col, wxGridCellAttr *attr) const
6523 {
6524 wxGrid *self = (wxGrid *)this; // const_cast
6525
6526 self->ClearAttrCache();
6527 self->m_attrCache.row = row;
6528 self->m_attrCache.col = col;
6529 self->m_attrCache.attr = attr;
6530 attr->SafeIncRef();
6531 }
6532
6533 bool wxGrid::LookupAttr(int row, int col, wxGridCellAttr **attr) const
6534 {
6535 if ( row == m_attrCache.row && col == m_attrCache.col )
6536 {
6537 *attr = m_attrCache.attr;
6538 (*attr)->SafeIncRef();
6539
6540 #ifdef DEBUG_ATTR_CACHE
6541 gs_nAttrCacheHits++;
6542 #endif
6543
6544 return TRUE;
6545 }
6546 else
6547 {
6548 #ifdef DEBUG_ATTR_CACHE
6549 gs_nAttrCacheMisses++;
6550 #endif
6551 return FALSE;
6552 }
6553 }
6554
6555 wxGridCellAttr *wxGrid::GetCellAttr(int row, int col) const
6556 {
6557 wxGridCellAttr *attr;
6558 if ( !LookupAttr(row, col, &attr) )
6559 {
6560 attr = m_table ? m_table->GetAttr(row, col) : (wxGridCellAttr *)NULL;
6561 CacheAttr(row, col, attr);
6562 }
6563 if (attr)
6564 {
6565 attr->SetDefAttr(m_defaultCellAttr);
6566 }
6567 else
6568 {
6569 attr = m_defaultCellAttr;
6570 attr->IncRef();
6571 }
6572
6573 return attr;
6574 }
6575
6576 wxGridCellAttr *wxGrid::GetOrCreateCellAttr(int row, int col) const
6577 {
6578 wxGridCellAttr *attr;
6579 if ( !LookupAttr(row, col, &attr) || !attr )
6580 {
6581 wxASSERT_MSG( m_table,
6582 _T("we may only be called if CanHaveAttributes() "
6583 "returned TRUE and then m_table should be !NULL") );
6584
6585 attr = m_table->GetAttr(row, col);
6586 if ( !attr )
6587 {
6588 attr = new wxGridCellAttr;
6589
6590 // artificially inc the ref count to match DecRef() in caller
6591 attr->IncRef();
6592
6593 m_table->SetAttr(attr, row, col);
6594 }
6595
6596 CacheAttr(row, col, attr);
6597 }
6598 attr->SetDefAttr(m_defaultCellAttr);
6599 return attr;
6600 }
6601
6602 // ----------------------------------------------------------------------------
6603 // setting cell attributes: this is forwarded to the table
6604 // ----------------------------------------------------------------------------
6605
6606 void wxGrid::SetRowAttr(int row, wxGridCellAttr *attr)
6607 {
6608 if ( CanHaveAttributes() )
6609 {
6610 m_table->SetRowAttr(attr, row);
6611 }
6612 else
6613 {
6614 attr->SafeDecRef();
6615 }
6616 }
6617
6618 void wxGrid::SetColAttr(int col, wxGridCellAttr *attr)
6619 {
6620 if ( CanHaveAttributes() )
6621 {
6622 m_table->SetColAttr(attr, col);
6623 }
6624 else
6625 {
6626 attr->SafeDecRef();
6627 }
6628 }
6629
6630 void wxGrid::SetCellBackgroundColour( int row, int col, const wxColour& colour )
6631 {
6632 if ( CanHaveAttributes() )
6633 {
6634 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
6635 attr->SetBackgroundColour(colour);
6636 attr->DecRef();
6637 }
6638 }
6639
6640 void wxGrid::SetCellTextColour( int row, int col, const wxColour& colour )
6641 {
6642 if ( CanHaveAttributes() )
6643 {
6644 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
6645 attr->SetTextColour(colour);
6646 attr->DecRef();
6647 }
6648 }
6649
6650 void wxGrid::SetCellFont( int row, int col, const wxFont& font )
6651 {
6652 if ( CanHaveAttributes() )
6653 {
6654 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
6655 attr->SetFont(font);
6656 attr->DecRef();
6657 }
6658 }
6659
6660 void wxGrid::SetCellAlignment( int row, int col, int horiz, int vert )
6661 {
6662 if ( CanHaveAttributes() )
6663 {
6664 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
6665 attr->SetAlignment(horiz, vert);
6666 attr->DecRef();
6667 }
6668 }
6669
6670 void wxGrid::SetCellRenderer(int row, int col, wxGridCellRenderer *renderer)
6671 {
6672 if ( CanHaveAttributes() )
6673 {
6674 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
6675 attr->SetRenderer(renderer);
6676 attr->DecRef();
6677 }
6678 }
6679
6680 void wxGrid::SetCellEditor(int row, int col, wxGridCellEditor* editor)
6681 {
6682 if ( CanHaveAttributes() )
6683 {
6684 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
6685 attr->SetEditor(editor);
6686 attr->DecRef();
6687 }
6688 }
6689
6690 void wxGrid::SetReadOnly(int row, int col, bool isReadOnly)
6691 {
6692 if ( CanHaveAttributes() )
6693 {
6694 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
6695 attr->SetReadOnly(isReadOnly);
6696 attr->DecRef();
6697 }
6698 }
6699
6700 // ----------------------------------------------------------------------------
6701 // Data type registration
6702 // ----------------------------------------------------------------------------
6703
6704 void wxGrid::RegisterDataType(const wxString& typeName,
6705 wxGridCellRenderer* renderer,
6706 wxGridCellEditor* editor)
6707 {
6708 m_typeRegistry->RegisterDataType(typeName, renderer, editor);
6709 }
6710
6711
6712 wxGridCellEditor* wxGrid::GetDefaultEditorForCell(int row, int col) const
6713 {
6714 wxString typeName = m_table->GetTypeName(row, col);
6715 return GetDefaultEditorForType(typeName);
6716 }
6717
6718 wxGridCellRenderer* wxGrid::GetDefaultRendererForCell(int row, int col) const
6719 {
6720 wxString typeName = m_table->GetTypeName(row, col);
6721 return GetDefaultRendererForType(typeName);
6722 }
6723
6724 wxGridCellEditor*
6725 wxGrid::GetDefaultEditorForType(const wxString& typeName) const
6726 {
6727 int index = m_typeRegistry->FindDataType(typeName);
6728 if (index == -1) {
6729 // Should we force the failure here or let it fallback to string handling???
6730 // wxFAIL_MSG(wxT("Unknown data type name"));
6731 return NULL;
6732 }
6733 return m_typeRegistry->GetEditor(index);
6734 }
6735
6736 wxGridCellRenderer*
6737 wxGrid::GetDefaultRendererForType(const wxString& typeName) const
6738 {
6739 int index = m_typeRegistry->FindDataType(typeName);
6740 if (index == -1) {
6741 // Should we force the failure here or let it fallback to string handling???
6742 // wxFAIL_MSG(wxT("Unknown data type name"));
6743 return NULL;
6744 }
6745 return m_typeRegistry->GetRenderer(index);
6746 }
6747
6748
6749 // ----------------------------------------------------------------------------
6750 // row/col size
6751 // ----------------------------------------------------------------------------
6752
6753 void wxGrid::EnableDragRowSize( bool enable )
6754 {
6755 m_canDragRowSize = enable;
6756 }
6757
6758
6759 void wxGrid::EnableDragColSize( bool enable )
6760 {
6761 m_canDragColSize = enable;
6762 }
6763
6764 void wxGrid::EnableDragGridSize( bool enable )
6765 {
6766 m_canDragGridSize = enable;
6767 }
6768
6769
6770 void wxGrid::SetDefaultRowSize( int height, bool resizeExistingRows )
6771 {
6772 m_defaultRowHeight = wxMax( height, WXGRID_MIN_ROW_HEIGHT );
6773
6774 if ( resizeExistingRows )
6775 {
6776 InitRowHeights();
6777
6778 CalcDimensions();
6779 }
6780 }
6781
6782 void wxGrid::SetRowSize( int row, int height )
6783 {
6784 wxCHECK_RET( row >= 0 && row < m_numRows, _T("invalid row index") );
6785
6786 if ( m_rowHeights.IsEmpty() )
6787 {
6788 // need to really create the array
6789 InitRowHeights();
6790 }
6791
6792 int h = wxMax( 0, height );
6793 int diff = h - m_rowHeights[row];
6794
6795 m_rowHeights[row] = h;
6796 int i;
6797 for ( i = row; i < m_numRows; i++ )
6798 {
6799 m_rowBottoms[i] += diff;
6800 }
6801 CalcDimensions();
6802 }
6803
6804 void wxGrid::SetDefaultColSize( int width, bool resizeExistingCols )
6805 {
6806 m_defaultColWidth = wxMax( width, WXGRID_MIN_COL_WIDTH );
6807
6808 if ( resizeExistingCols )
6809 {
6810 InitColWidths();
6811
6812 CalcDimensions();
6813 }
6814 }
6815
6816 void wxGrid::SetColSize( int col, int width )
6817 {
6818 wxCHECK_RET( col >= 0 && col < m_numCols, _T("invalid column index") );
6819
6820 // should we check that it's bigger than GetColMinimalWidth(col) here?
6821
6822 if ( m_colWidths.IsEmpty() )
6823 {
6824 // need to really create the array
6825 InitColWidths();
6826 }
6827
6828 int w = wxMax( 0, width );
6829 int diff = w - m_colWidths[col];
6830 m_colWidths[col] = w;
6831
6832 int i;
6833 for ( i = col; i < m_numCols; i++ )
6834 {
6835 m_colRights[i] += diff;
6836 }
6837 CalcDimensions();
6838 }
6839
6840
6841 void wxGrid::SetColMinimalWidth( int col, int width )
6842 {
6843 m_colMinWidths.Put(col, (wxObject *)width);
6844 }
6845
6846 int wxGrid::GetColMinimalWidth(int col) const
6847 {
6848 wxObject *obj = m_colMinWidths.Get(m_dragRowOrCol);
6849 return obj ? (int)obj : WXGRID_MIN_COL_WIDTH;
6850 }
6851
6852 void wxGrid::AutoSizeColumn( int col, bool setAsMin )
6853 {
6854 wxClientDC dc(m_gridWin);
6855
6856 wxCoord width, widthMax = 0;
6857 for ( int row = 0; row < m_numRows; row++ )
6858 {
6859 wxGridCellAttr* attr = GetCellAttr(row, col);
6860 wxGridCellRenderer* renderer = attr->GetRenderer(this, row, col);
6861 if ( renderer )
6862 {
6863 width = renderer->GetBestSize(*this, *attr, dc, row, col).x;
6864 if ( width > widthMax )
6865 {
6866 widthMax = width;
6867 }
6868 }
6869
6870 attr->DecRef();
6871 }
6872
6873 // now also compare with the column label width
6874 dc.SetFont( GetLabelFont() );
6875 dc.GetTextExtent( GetColLabelValue(col), &width, NULL );
6876 if ( width > widthMax )
6877 {
6878 widthMax = width;
6879 }
6880
6881 if ( !widthMax )
6882 {
6883 // empty column - give default width (notice that if widthMax is less
6884 // than default width but != 0, it's ok)
6885 widthMax = m_defaultColWidth;
6886 }
6887 else
6888 {
6889 // leave some space around text
6890 widthMax += 10;
6891 }
6892
6893 SetColSize(col, widthMax);
6894 if ( setAsMin )
6895 {
6896 SetColMinimalWidth(col, widthMax);
6897 }
6898 }
6899
6900 void wxGrid::AutoSizeColumns( bool setAsMin )
6901 {
6902 for ( int col = 0; col < m_numCols; col++ )
6903 {
6904 AutoSizeColumn(col, setAsMin);
6905 }
6906 }
6907
6908 //
6909 // ------ cell value accessor functions
6910 //
6911
6912 void wxGrid::SetCellValue( int row, int col, const wxString& s )
6913 {
6914 if ( m_table )
6915 {
6916 m_table->SetValue( row, col, s.c_str() );
6917 if ( !GetBatchCount() )
6918 {
6919 wxClientDC dc( m_gridWin );
6920 PrepareDC( dc );
6921 DrawCell( dc, wxGridCellCoords(row, col) );
6922 }
6923
6924 if ( m_currentCellCoords.GetRow() == row &&
6925 m_currentCellCoords.GetCol() == col &&
6926 IsCellEditControlEnabled())
6927 {
6928 HideCellEditControl();
6929 ShowCellEditControl(); // will reread data from table
6930 }
6931 }
6932 }
6933
6934
6935 //
6936 // ------ Block, row and col selection
6937 //
6938
6939 void wxGrid::SelectRow( int row, bool addToSelected )
6940 {
6941 wxRect r;
6942
6943 if ( IsSelection() && addToSelected )
6944 {
6945 wxRect rect[4];
6946 bool need_refresh[4];
6947 need_refresh[0] =
6948 need_refresh[1] =
6949 need_refresh[2] =
6950 need_refresh[3] = FALSE;
6951
6952 int i;
6953
6954 wxCoord oldLeft = m_selectedTopLeft.GetCol();
6955 wxCoord oldTop = m_selectedTopLeft.GetRow();
6956 wxCoord oldRight = m_selectedBottomRight.GetCol();
6957 wxCoord oldBottom = m_selectedBottomRight.GetRow();
6958
6959 if ( oldTop > row )
6960 {
6961 need_refresh[0] = TRUE;
6962 rect[0] = BlockToDeviceRect( wxGridCellCoords ( row, 0 ),
6963 wxGridCellCoords ( oldTop - 1,
6964 m_numCols - 1 ) );
6965 m_selectedTopLeft.SetRow( row );
6966 }
6967
6968 if ( oldLeft > 0 )
6969 {
6970 need_refresh[1] = TRUE;
6971 rect[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop, 0 ),
6972 wxGridCellCoords ( oldBottom,
6973 oldLeft - 1 ) );
6974
6975 m_selectedTopLeft.SetCol( 0 );
6976 }
6977
6978 if ( oldBottom < row )
6979 {
6980 need_refresh[2] = TRUE;
6981 rect[2] = BlockToDeviceRect( wxGridCellCoords ( oldBottom + 1, 0 ),
6982 wxGridCellCoords ( row,
6983 m_numCols - 1 ) );
6984 m_selectedBottomRight.SetRow( row );
6985 }
6986
6987 if ( oldRight < m_numCols - 1 )
6988 {
6989 need_refresh[3] = TRUE;
6990 rect[3] = BlockToDeviceRect( wxGridCellCoords ( oldTop ,
6991 oldRight + 1 ),
6992 wxGridCellCoords ( oldBottom,
6993 m_numCols - 1 ) );
6994 m_selectedBottomRight.SetCol( m_numCols - 1 );
6995 }
6996
6997 for (i = 0; i < 4; i++ )
6998 if ( need_refresh[i] && rect[i] != wxGridNoCellRect )
6999 m_gridWin->Refresh( FALSE, &(rect[i]) );
7000 }
7001 else
7002 {
7003 r = SelectionToDeviceRect();
7004 ClearSelection();
7005 if ( r != wxGridNoCellRect ) m_gridWin->Refresh( FALSE, &r );
7006
7007 m_selectedTopLeft.Set( row, 0 );
7008 m_selectedBottomRight.Set( row, m_numCols-1 );
7009 r = SelectionToDeviceRect();
7010 m_gridWin->Refresh( FALSE, &r );
7011 }
7012
7013 wxGridRangeSelectEvent gridEvt( GetId(),
7014 wxEVT_GRID_RANGE_SELECT,
7015 this,
7016 m_selectedTopLeft,
7017 m_selectedBottomRight );
7018
7019 GetEventHandler()->ProcessEvent(gridEvt);
7020 }
7021
7022
7023 void wxGrid::SelectCol( int col, bool addToSelected )
7024 {
7025 if ( IsSelection() && addToSelected )
7026 {
7027 wxRect rect[4];
7028 bool need_refresh[4];
7029 need_refresh[0] =
7030 need_refresh[1] =
7031 need_refresh[2] =
7032 need_refresh[3] = FALSE;
7033 int i;
7034
7035 wxCoord oldLeft = m_selectedTopLeft.GetCol();
7036 wxCoord oldTop = m_selectedTopLeft.GetRow();
7037 wxCoord oldRight = m_selectedBottomRight.GetCol();
7038 wxCoord oldBottom = m_selectedBottomRight.GetRow();
7039
7040 if ( oldLeft > col )
7041 {
7042 need_refresh[0] = TRUE;
7043 rect[0] = BlockToDeviceRect( wxGridCellCoords ( 0, col ),
7044 wxGridCellCoords ( m_numRows - 1,
7045 oldLeft - 1 ) );
7046 m_selectedTopLeft.SetCol( col );
7047 }
7048
7049 if ( oldTop > 0 )
7050 {
7051 need_refresh[1] = TRUE;
7052 rect[1] = BlockToDeviceRect( wxGridCellCoords ( 0, oldLeft ),
7053 wxGridCellCoords ( oldTop - 1,
7054 oldRight ) );
7055 m_selectedTopLeft.SetRow( 0 );
7056 }
7057
7058 if ( oldRight < col )
7059 {
7060 need_refresh[2] = TRUE;
7061 rect[2] = BlockToDeviceRect( wxGridCellCoords ( 0, oldRight + 1 ),
7062 wxGridCellCoords ( m_numRows - 1,
7063 col ) );
7064 m_selectedBottomRight.SetCol( col );
7065 }
7066
7067 if ( oldBottom < m_numRows - 1 )
7068 {
7069 need_refresh[3] = TRUE;
7070 rect[3] = BlockToDeviceRect( wxGridCellCoords ( oldBottom + 1,
7071 oldLeft ),
7072 wxGridCellCoords ( m_numRows - 1,
7073 oldRight ) );
7074 m_selectedBottomRight.SetRow( m_numRows - 1 );
7075 }
7076
7077 for (i = 0; i < 4; i++ )
7078 if ( need_refresh[i] && rect[i] != wxGridNoCellRect )
7079 m_gridWin->Refresh( FALSE, &(rect[i]) );
7080 }
7081 else
7082 {
7083 wxRect r;
7084
7085 r = SelectionToDeviceRect();
7086 ClearSelection();
7087 if ( r != wxGridNoCellRect ) m_gridWin->Refresh( FALSE, &r );
7088
7089 m_selectedTopLeft.Set( 0, col );
7090 m_selectedBottomRight.Set( m_numRows-1, col );
7091 r = SelectionToDeviceRect();
7092 m_gridWin->Refresh( FALSE, &r );
7093 }
7094
7095 wxGridRangeSelectEvent gridEvt( GetId(),
7096 wxEVT_GRID_RANGE_SELECT,
7097 this,
7098 m_selectedTopLeft,
7099 m_selectedBottomRight );
7100
7101 GetEventHandler()->ProcessEvent(gridEvt);
7102 }
7103
7104
7105 void wxGrid::SelectBlock( int topRow, int leftCol, int bottomRow, int rightCol )
7106 {
7107 int temp;
7108 wxGridCellCoords updateTopLeft, updateBottomRight;
7109
7110 if ( topRow > bottomRow )
7111 {
7112 temp = topRow;
7113 topRow = bottomRow;
7114 bottomRow = temp;
7115 }
7116
7117 if ( leftCol > rightCol )
7118 {
7119 temp = leftCol;
7120 leftCol = rightCol;
7121 rightCol = temp;
7122 }
7123
7124 updateTopLeft = wxGridCellCoords( topRow, leftCol );
7125 updateBottomRight = wxGridCellCoords( bottomRow, rightCol );
7126
7127 if ( m_selectedTopLeft != updateTopLeft ||
7128 m_selectedBottomRight != updateBottomRight )
7129 {
7130 // Compute two optimal update rectangles:
7131 // Either one rectangle is a real subset of the
7132 // other, or they are (almost) disjoint!
7133 wxRect rect[4];
7134 bool need_refresh[4];
7135 need_refresh[0] =
7136 need_refresh[1] =
7137 need_refresh[2] =
7138 need_refresh[3] = FALSE;
7139 int i;
7140
7141 // Store intermediate values
7142 wxCoord oldLeft = m_selectedTopLeft.GetCol();
7143 wxCoord oldTop = m_selectedTopLeft.GetRow();
7144 wxCoord oldRight = m_selectedBottomRight.GetCol();
7145 wxCoord oldBottom = m_selectedBottomRight.GetRow();
7146
7147 // Determine the outer/inner coordinates.
7148 if (oldLeft > leftCol)
7149 {
7150 temp = oldLeft;
7151 oldLeft = leftCol;
7152 leftCol = temp;
7153 }
7154 if (oldTop > topRow )
7155 {
7156 temp = oldTop;
7157 oldTop = topRow;
7158 topRow = temp;
7159 }
7160 if (oldRight < rightCol )
7161 {
7162 temp = oldRight;
7163 oldRight = rightCol;
7164 rightCol = temp;
7165 }
7166 if (oldBottom < bottomRow)
7167 {
7168 temp = oldBottom;
7169 oldBottom = bottomRow;
7170 bottomRow = temp;
7171 }
7172
7173 // Now, either the stuff marked old is the outer
7174 // rectangle or we don't have a situation where one
7175 // is contained in the other.
7176
7177 if ( oldLeft < leftCol )
7178 {
7179 need_refresh[0] = TRUE;
7180 rect[0] = BlockToDeviceRect( wxGridCellCoords ( oldTop,
7181 oldLeft ),
7182 wxGridCellCoords ( oldBottom,
7183 leftCol - 1 ) );
7184 }
7185
7186 if ( oldTop < topRow )
7187 {
7188 need_refresh[1] = TRUE;
7189 rect[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop,
7190 leftCol ),
7191 wxGridCellCoords ( topRow - 1,
7192 rightCol ) );
7193 }
7194
7195 if ( oldRight > rightCol )
7196 {
7197 need_refresh[2] = TRUE;
7198 rect[2] = BlockToDeviceRect( wxGridCellCoords ( oldTop,
7199 rightCol + 1 ),
7200 wxGridCellCoords ( oldBottom,
7201 oldRight ) );
7202 }
7203
7204 if ( oldBottom > bottomRow )
7205 {
7206 need_refresh[3] = TRUE;
7207 rect[3] = BlockToDeviceRect( wxGridCellCoords ( bottomRow + 1,
7208 leftCol ),
7209 wxGridCellCoords ( oldBottom,
7210 rightCol ) );
7211 }
7212
7213
7214 // Change Selection
7215 m_selectedTopLeft = updateTopLeft;
7216 m_selectedBottomRight = updateBottomRight;
7217
7218 // various Refresh() calls
7219 for (i = 0; i < 4; i++ )
7220 if ( need_refresh[i] && rect[i] != wxGridNoCellRect )
7221 m_gridWin->Refresh( FALSE, &(rect[i]) );
7222 }
7223
7224 // only generate an event if the block is not being selected by
7225 // dragging the mouse (in which case the event will be generated in
7226 // the mouse event handler)
7227 if ( !m_isDragging )
7228 {
7229 wxGridRangeSelectEvent gridEvt( GetId(),
7230 wxEVT_GRID_RANGE_SELECT,
7231 this,
7232 m_selectedTopLeft,
7233 m_selectedBottomRight );
7234
7235 GetEventHandler()->ProcessEvent(gridEvt);
7236 }
7237 }
7238
7239 void wxGrid::SelectAll()
7240 {
7241 m_selectedTopLeft.Set( 0, 0 );
7242 m_selectedBottomRight.Set( m_numRows-1, m_numCols-1 );
7243
7244 m_gridWin->Refresh();
7245 }
7246
7247
7248 void wxGrid::ClearSelection()
7249 {
7250 m_selectedTopLeft = wxGridNoCellCoords;
7251 m_selectedBottomRight = wxGridNoCellCoords;
7252 }
7253
7254
7255 // This function returns the rectangle that encloses the given block
7256 // in device coords clipped to the client size of the grid window.
7257 //
7258 wxRect wxGrid::BlockToDeviceRect( const wxGridCellCoords &topLeft,
7259 const wxGridCellCoords &bottomRight )
7260 {
7261 wxRect rect( wxGridNoCellRect );
7262 wxRect cellRect;
7263
7264 cellRect = CellToRect( topLeft );
7265 if ( cellRect != wxGridNoCellRect )
7266 {
7267 rect = cellRect;
7268 }
7269 else
7270 {
7271 rect = wxRect( 0, 0, 0, 0 );
7272 }
7273
7274 cellRect = CellToRect( bottomRight );
7275 if ( cellRect != wxGridNoCellRect )
7276 {
7277 rect += cellRect;
7278 }
7279 else
7280 {
7281 return wxGridNoCellRect;
7282 }
7283
7284 // convert to scrolled coords
7285 //
7286 int left, top, right, bottom;
7287 CalcScrolledPosition( rect.GetLeft(), rect.GetTop(), &left, &top );
7288 CalcScrolledPosition( rect.GetRight(), rect.GetBottom(), &right, &bottom );
7289
7290 int cw, ch;
7291 m_gridWin->GetClientSize( &cw, &ch );
7292
7293 rect.SetLeft( wxMax(0, left) );
7294 rect.SetTop( wxMax(0, top) );
7295 rect.SetRight( wxMin(cw, right) );
7296 rect.SetBottom( wxMin(ch, bottom) );
7297
7298 return rect;
7299 }
7300
7301
7302
7303 //
7304 // ------ Grid event classes
7305 //
7306
7307 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent, wxEvent )
7308
7309 wxGridEvent::wxGridEvent( int id, wxEventType type, wxObject* obj,
7310 int row, int col, int x, int y,
7311 bool control, bool shift, bool alt, bool meta )
7312 : wxNotifyEvent( type, id )
7313 {
7314 m_row = row;
7315 m_col = col;
7316 m_x = x;
7317 m_y = y;
7318 m_control = control;
7319 m_shift = shift;
7320 m_alt = alt;
7321 m_meta = meta;
7322
7323 SetEventObject(obj);
7324 }
7325
7326
7327 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent, wxEvent )
7328
7329 wxGridSizeEvent::wxGridSizeEvent( int id, wxEventType type, wxObject* obj,
7330 int rowOrCol, int x, int y,
7331 bool control, bool shift, bool alt, bool meta )
7332 : wxNotifyEvent( type, id )
7333 {
7334 m_rowOrCol = rowOrCol;
7335 m_x = x;
7336 m_y = y;
7337 m_control = control;
7338 m_shift = shift;
7339 m_alt = alt;
7340 m_meta = meta;
7341
7342 SetEventObject(obj);
7343 }
7344
7345
7346 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent, wxEvent )
7347
7348 wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id, wxEventType type, wxObject* obj,
7349 const wxGridCellCoords& topLeft,
7350 const wxGridCellCoords& bottomRight,
7351 bool control, bool shift, bool alt, bool meta )
7352 : wxNotifyEvent( type, id )
7353 {
7354 m_topLeft = topLeft;
7355 m_bottomRight = bottomRight;
7356 m_control = control;
7357 m_shift = shift;
7358 m_alt = alt;
7359 m_meta = meta;
7360
7361 SetEventObject(obj);
7362 }
7363
7364
7365 #endif // ifndef wxUSE_NEW_GRID
7366