Minor optical things.
[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 enable the cell edit control
4752 if ( !(event.AltDown() ||
4753 event.MetaDown() ||
4754 event.ControlDown()) &&
4755 isalnum(event.KeyCode()) &&
4756 !IsCellEditControlEnabled() &&
4757 CanEnableCellControl() )
4758 {
4759 EnableCellEditControl();
4760 int row = m_currentCellCoords.GetRow();
4761 int col = m_currentCellCoords.GetCol();
4762 wxGridCellAttr* attr = GetCellAttr(row, col);
4763 attr->GetEditor(this, row, col)->StartingKey(event);
4764 attr->DecRef();
4765 }
4766 else
4767 {
4768 // let others process char events with modifiers or all
4769 // char events for readonly cells
4770 event.Skip();
4771 }
4772 break;
4773 }
4774 }
4775
4776 m_inOnKeyDown = FALSE;
4777 }
4778
4779
4780 void wxGrid::OnEraseBackground(wxEraseEvent&)
4781 {
4782 }
4783
4784 void wxGrid::SetCurrentCell( const wxGridCellCoords& coords )
4785 {
4786 if ( SendEvent( wxEVT_GRID_SELECT_CELL, coords.GetRow(), coords.GetCol() ) )
4787 {
4788 // the event has been intercepted - do nothing
4789 return;
4790 }
4791
4792 if ( m_displayed &&
4793 m_currentCellCoords != wxGridNoCellCoords )
4794 {
4795 HideCellEditControl();
4796 DisableCellEditControl();
4797
4798 // Clear the old current cell highlight
4799 wxRect r = BlockToDeviceRect(m_currentCellCoords, m_currentCellCoords);
4800
4801 // Otherwise refresh redraws the highlight!
4802 m_currentCellCoords = coords;
4803
4804 m_gridWin->Refresh( FALSE, &r );
4805 }
4806
4807 m_currentCellCoords = coords;
4808
4809 if ( m_displayed )
4810 {
4811 wxClientDC dc(m_gridWin);
4812 PrepareDC(dc);
4813
4814 wxGridCellAttr* attr = GetCellAttr(coords);
4815 DrawCellHighlight(dc, attr);
4816 attr->DecRef();
4817
4818 if ( IsSelection() )
4819 {
4820 wxRect r( SelectionToDeviceRect() );
4821 ClearSelection();
4822 if ( !GetBatchCount() ) m_gridWin->Refresh( FALSE, &r );
4823 }
4824 }
4825 }
4826
4827
4828 //
4829 // ------ functions to get/send data (see also public functions)
4830 //
4831
4832 bool wxGrid::GetModelValues()
4833 {
4834 if ( m_table )
4835 {
4836 // all we need to do is repaint the grid
4837 //
4838 m_gridWin->Refresh();
4839 return TRUE;
4840 }
4841
4842 return FALSE;
4843 }
4844
4845
4846 bool wxGrid::SetModelValues()
4847 {
4848 int row, col;
4849
4850 if ( m_table )
4851 {
4852 for ( row = 0; row < m_numRows; row++ )
4853 {
4854 for ( col = 0; col < m_numCols; col++ )
4855 {
4856 m_table->SetValue( row, col, GetCellValue(row, col) );
4857 }
4858 }
4859
4860 return TRUE;
4861 }
4862
4863 return FALSE;
4864 }
4865
4866
4867
4868 // Note - this function only draws cells that are in the list of
4869 // exposed cells (usually set from the update region by
4870 // CalcExposedCells)
4871 //
4872 void wxGrid::DrawGridCellArea( wxDC& dc )
4873 {
4874 if ( !m_numRows || !m_numCols ) return;
4875
4876 size_t i;
4877 size_t numCells = m_cellsExposed.GetCount();
4878
4879 for ( i = 0; i < numCells; i++ )
4880 {
4881 DrawCell( dc, m_cellsExposed[i] );
4882 }
4883 }
4884
4885
4886 void wxGrid::DrawGridSpace( wxDC& dc )
4887 {
4888 if ( m_numRows && m_numCols )
4889 {
4890 int cw, ch;
4891 m_gridWin->GetClientSize( &cw, &ch );
4892
4893 int right, bottom;
4894 CalcUnscrolledPosition( cw, ch, &right, &bottom );
4895
4896 if ( right > GetColRight(m_numCols-1) ||
4897 bottom > GetRowBottom(m_numRows-1) )
4898 {
4899 int left, top;
4900 CalcUnscrolledPosition( 0, 0, &left, &top );
4901
4902 dc.SetBrush( wxBrush(GetDefaultCellBackgroundColour(), wxSOLID) );
4903 dc.SetPen( *wxTRANSPARENT_PEN );
4904
4905 if ( right > GetColRight(m_numCols-1) )
4906 dc.DrawRectangle( GetColRight(m_numCols-1), top,
4907 right - GetColRight(m_numCols-1), ch );
4908
4909 if ( bottom > GetRowBottom(m_numRows-1) )
4910 dc.DrawRectangle( left, GetRowBottom(m_numRows-1),
4911 cw, bottom - GetRowBottom(m_numRows-1) );
4912 }
4913 }
4914 }
4915
4916
4917 void wxGrid::DrawCell( wxDC& dc, const wxGridCellCoords& coords )
4918 {
4919 int row = coords.GetRow();
4920 int col = coords.GetCol();
4921
4922 if ( GetColWidth(col) <= 0 || GetRowHeight(row) <= 0 )
4923 return;
4924
4925 // we draw the cell border ourselves
4926 #if !WXGRID_DRAW_LINES
4927 if ( m_gridLinesEnabled )
4928 DrawCellBorder( dc, coords );
4929 #endif
4930
4931 wxGridCellAttr* attr = GetCellAttr(row, col);
4932
4933 bool isCurrent = coords == m_currentCellCoords;
4934
4935 wxRect rect;
4936 rect.x = GetColLeft(col);
4937 rect.y = GetRowTop(row);
4938 rect.width = GetColWidth(col) - 1;
4939 rect.height = GetRowHeight(row) - 1;
4940
4941 // if the editor is shown, we should use it and not the renderer
4942 if ( isCurrent && IsCellEditControlEnabled() )
4943 {
4944 attr->GetEditor(this, row, col)->PaintBackground(rect, attr);
4945 }
4946 else
4947 {
4948 // but all the rest is drawn by the cell renderer and hence may be
4949 // customized
4950 attr->GetRenderer(this, row, col)->
4951 Draw(*this, *attr, dc, rect, row, col, IsInSelection(coords));
4952
4953 }
4954
4955 attr->DecRef();
4956 }
4957
4958 void wxGrid::DrawCellHighlight( wxDC& dc, const wxGridCellAttr *attr )
4959 {
4960 int row = m_currentCellCoords.GetRow();
4961 int col = m_currentCellCoords.GetCol();
4962
4963 if ( GetColWidth(col) <= 0 || GetRowHeight(row) <= 0 )
4964 return;
4965
4966 wxRect rect;
4967 rect.x = GetColLeft(col);
4968 rect.y = GetRowTop(row);
4969 rect.width = GetColWidth(col) - 1;
4970 rect.height = GetRowHeight(row) - 1;
4971
4972 // hmmm... what could we do here to show that the cell is disabled?
4973 // for now, I just draw a thinner border than for the other ones, but
4974 // it doesn't look really good
4975 dc.SetPen(wxPen(m_gridLineColour, attr->IsReadOnly() ? 1 : 3, wxSOLID));
4976 dc.SetBrush(*wxTRANSPARENT_BRUSH);
4977
4978 dc.DrawRectangle(rect);
4979
4980 #if 0
4981 // VZ: my experiments with 3d borders...
4982
4983 // how to properly set colours for arbitrary bg?
4984 wxCoord x1 = rect.x,
4985 y1 = rect.y,
4986 x2 = rect.x + rect.width -1,
4987 y2 = rect.y + rect.height -1;
4988
4989 dc.SetPen(*wxWHITE_PEN);
4990 dc.DrawLine(x1, y1, x2, y1);
4991 dc.DrawLine(x1, y1, x1, y2);
4992
4993 dc.DrawLine(x1 + 1, y2 - 1, x2 - 1, y2 - 1);
4994 dc.DrawLine(x2 - 1, y1 + 1, x2 - 1, y2 );
4995
4996 dc.SetPen(*wxBLACK_PEN);
4997 dc.DrawLine(x1, y2, x2, y2);
4998 dc.DrawLine(x2, y1, x2, y2+1);
4999 #endif // 0
5000 }
5001
5002
5003 void wxGrid::DrawCellBorder( wxDC& dc, const wxGridCellCoords& coords )
5004 {
5005 int row = coords.GetRow();
5006 int col = coords.GetCol();
5007 if ( GetColWidth(col) <= 0 || GetRowHeight(row) <= 0 )
5008 return;
5009
5010 dc.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID) );
5011
5012 // right hand border
5013 //
5014 dc.DrawLine( GetColRight(col), GetRowTop(row),
5015 GetColRight(col), GetRowBottom(row) );
5016
5017 // bottom border
5018 //
5019 dc.DrawLine( GetColLeft(col), GetRowBottom(row),
5020 GetColRight(col), GetRowBottom(row) );
5021 }
5022
5023 void wxGrid::DrawHighlight(wxDC& dc)
5024 {
5025 if ( IsCellEditControlEnabled() )
5026 {
5027 // don't show highlight when the edit control is shown
5028 return;
5029 }
5030
5031 // if the active cell was repainted, repaint its highlight too because it
5032 // might have been damaged by the grid lines
5033 size_t count = m_cellsExposed.GetCount();
5034 for ( size_t n = 0; n < count; n++ )
5035 {
5036 if ( m_cellsExposed[n] == m_currentCellCoords )
5037 {
5038 wxGridCellAttr* attr = GetCellAttr(m_currentCellCoords);
5039 DrawCellHighlight(dc, attr);
5040 attr->DecRef();
5041
5042 break;
5043 }
5044 }
5045 }
5046
5047 // TODO: remove this ???
5048 // This is used to redraw all grid lines e.g. when the grid line colour
5049 // has been changed
5050 //
5051 void wxGrid::DrawAllGridLines( wxDC& dc, const wxRegion & reg )
5052 {
5053 if ( !m_gridLinesEnabled ||
5054 !m_numRows ||
5055 !m_numCols ) return;
5056
5057 int top, bottom, left, right;
5058
5059 #ifndef __WXGTK__
5060 if (reg.IsEmpty())
5061 {
5062 int cw, ch;
5063 m_gridWin->GetClientSize(&cw, &ch);
5064
5065 // virtual coords of visible area
5066 //
5067 CalcUnscrolledPosition( 0, 0, &left, &top );
5068 CalcUnscrolledPosition( cw, ch, &right, &bottom );
5069 }
5070 else
5071 {
5072 wxCoord x, y, w, h;
5073 reg.GetBox(x, y, w, h);
5074 CalcUnscrolledPosition( x, y, &left, &top );
5075 CalcUnscrolledPosition( x + w, y + h, &right, &bottom );
5076 }
5077 #else
5078 int cw, ch;
5079 m_gridWin->GetClientSize(&cw, &ch);
5080 CalcUnscrolledPosition( 0, 0, &left, &top );
5081 CalcUnscrolledPosition( cw, ch, &right, &bottom );
5082 #endif
5083
5084 // avoid drawing grid lines past the last row and col
5085 //
5086 right = wxMin( right, GetColRight(m_numCols - 1) );
5087 bottom = wxMin( bottom, GetRowBottom(m_numRows - 1) );
5088
5089 dc.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID) );
5090
5091 // horizontal grid lines
5092 //
5093 int i;
5094 for ( i = 0; i < m_numRows; i++ )
5095 {
5096 int bot = GetRowBottom(i) - 1;
5097
5098 if ( bot > bottom )
5099 {
5100 break;
5101 }
5102
5103 if ( bot >= top )
5104 {
5105 dc.DrawLine( left, bot, right, bot );
5106 }
5107 }
5108
5109
5110 // vertical grid lines
5111 //
5112 for ( i = 0; i < m_numCols; i++ )
5113 {
5114 int colRight = GetColRight(i) - 1;
5115 if ( colRight > right )
5116 {
5117 break;
5118 }
5119
5120 if ( colRight >= left )
5121 {
5122 dc.DrawLine( colRight, top, colRight, bottom );
5123 }
5124 }
5125 }
5126
5127
5128 void wxGrid::DrawRowLabels( wxDC& dc )
5129 {
5130 if ( !m_numRows || !m_numCols ) return;
5131
5132 size_t i;
5133 size_t numLabels = m_rowLabelsExposed.GetCount();
5134
5135 for ( i = 0; i < numLabels; i++ )
5136 {
5137 DrawRowLabel( dc, m_rowLabelsExposed[i] );
5138 }
5139 }
5140
5141
5142 void wxGrid::DrawRowLabel( wxDC& dc, int row )
5143 {
5144 if ( GetRowHeight(row) <= 0 )
5145 return;
5146
5147 int rowTop = GetRowTop(row),
5148 rowBottom = GetRowBottom(row) - 1;
5149
5150 dc.SetPen( *wxBLACK_PEN );
5151 dc.DrawLine( m_rowLabelWidth-1, rowTop,
5152 m_rowLabelWidth-1, rowBottom );
5153
5154 dc.DrawLine( 0, rowBottom, m_rowLabelWidth-1, rowBottom );
5155
5156 dc.SetPen( *wxWHITE_PEN );
5157 dc.DrawLine( 0, rowTop, 0, rowBottom );
5158 dc.DrawLine( 0, rowTop, m_rowLabelWidth-1, rowTop );
5159
5160 dc.SetBackgroundMode( wxTRANSPARENT );
5161 dc.SetTextForeground( GetLabelTextColour() );
5162 dc.SetFont( GetLabelFont() );
5163
5164 int hAlign, vAlign;
5165 GetRowLabelAlignment( &hAlign, &vAlign );
5166
5167 wxRect rect;
5168 rect.SetX( 2 );
5169 rect.SetY( GetRowTop(row) + 2 );
5170 rect.SetWidth( m_rowLabelWidth - 4 );
5171 rect.SetHeight( GetRowHeight(row) - 4 );
5172 DrawTextRectangle( dc, GetRowLabelValue( row ), rect, hAlign, vAlign );
5173 }
5174
5175
5176 void wxGrid::DrawColLabels( wxDC& dc )
5177 {
5178 if ( !m_numRows || !m_numCols ) return;
5179
5180 size_t i;
5181 size_t numLabels = m_colLabelsExposed.GetCount();
5182
5183 for ( i = 0; i < numLabels; i++ )
5184 {
5185 DrawColLabel( dc, m_colLabelsExposed[i] );
5186 }
5187 }
5188
5189
5190 void wxGrid::DrawColLabel( wxDC& dc, int col )
5191 {
5192 if ( GetColWidth(col) <= 0 )
5193 return;
5194
5195 int colLeft = GetColLeft(col),
5196 colRight = GetColRight(col) - 1;
5197
5198 dc.SetPen( *wxBLACK_PEN );
5199 dc.DrawLine( colRight, 0,
5200 colRight, m_colLabelHeight-1 );
5201
5202 dc.DrawLine( colLeft, m_colLabelHeight-1,
5203 colRight, m_colLabelHeight-1 );
5204
5205 dc.SetPen( *wxWHITE_PEN );
5206 dc.DrawLine( colLeft, 0, colLeft, m_colLabelHeight-1 );
5207 dc.DrawLine( colLeft, 0, colRight, 0 );
5208
5209 dc.SetBackgroundMode( wxTRANSPARENT );
5210 dc.SetTextForeground( GetLabelTextColour() );
5211 dc.SetFont( GetLabelFont() );
5212
5213 dc.SetBackgroundMode( wxTRANSPARENT );
5214 dc.SetTextForeground( GetLabelTextColour() );
5215 dc.SetFont( GetLabelFont() );
5216
5217 int hAlign, vAlign;
5218 GetColLabelAlignment( &hAlign, &vAlign );
5219
5220 wxRect rect;
5221 rect.SetX( colLeft + 2 );
5222 rect.SetY( 2 );
5223 rect.SetWidth( GetColWidth(col) - 4 );
5224 rect.SetHeight( m_colLabelHeight - 4 );
5225 DrawTextRectangle( dc, GetColLabelValue( col ), rect, hAlign, vAlign );
5226 }
5227
5228
5229 void wxGrid::DrawTextRectangle( wxDC& dc,
5230 const wxString& value,
5231 const wxRect& rect,
5232 int horizAlign,
5233 int vertAlign )
5234 {
5235 long textWidth, textHeight;
5236 long lineWidth, lineHeight;
5237 wxArrayString lines;
5238
5239 dc.SetClippingRegion( rect );
5240 StringToLines( value, lines );
5241 if ( lines.GetCount() )
5242 {
5243 GetTextBoxSize( dc, lines, &textWidth, &textHeight );
5244 dc.GetTextExtent( lines[0], &lineWidth, &lineHeight );
5245
5246 float x, y;
5247 switch ( horizAlign )
5248 {
5249 case wxRIGHT:
5250 x = rect.x + (rect.width - textWidth - 1);
5251 break;
5252
5253 case wxCENTRE:
5254 x = rect.x + ((rect.width - textWidth)/2);
5255 break;
5256
5257 case wxLEFT:
5258 default:
5259 x = rect.x + 1;
5260 break;
5261 }
5262
5263 switch ( vertAlign )
5264 {
5265 case wxBOTTOM:
5266 y = rect.y + (rect.height - textHeight - 1);
5267 break;
5268
5269 case wxCENTRE:
5270 y = rect.y + ((rect.height - textHeight)/2);
5271 break;
5272
5273 case wxTOP:
5274 default:
5275 y = rect.y + 1;
5276 break;
5277 }
5278
5279 for ( size_t i = 0; i < lines.GetCount(); i++ )
5280 {
5281 dc.DrawText( lines[i], (long)x, (long)y );
5282 y += lineHeight;
5283 }
5284 }
5285
5286 dc.DestroyClippingRegion();
5287 }
5288
5289
5290 // Split multi line text up into an array of strings. Any existing
5291 // contents of the string array are preserved.
5292 //
5293 void wxGrid::StringToLines( const wxString& value, wxArrayString& lines )
5294 {
5295 int startPos = 0;
5296 int pos;
5297 wxString eol = wxTextFile::GetEOL( wxTextFileType_Unix );
5298 wxString tVal = wxTextFile::Translate( value, wxTextFileType_Unix );
5299
5300 while ( startPos < (int)tVal.Length() )
5301 {
5302 pos = tVal.Mid(startPos).Find( eol );
5303 if ( pos < 0 )
5304 {
5305 break;
5306 }
5307 else if ( pos == 0 )
5308 {
5309 lines.Add( wxEmptyString );
5310 }
5311 else
5312 {
5313 lines.Add( value.Mid(startPos, pos) );
5314 }
5315 startPos += pos+1;
5316 }
5317 if ( startPos < (int)value.Length() )
5318 {
5319 lines.Add( value.Mid( startPos ) );
5320 }
5321 }
5322
5323
5324 void wxGrid::GetTextBoxSize( wxDC& dc,
5325 wxArrayString& lines,
5326 long *width, long *height )
5327 {
5328 long w = 0;
5329 long h = 0;
5330 long lineW, lineH;
5331
5332 size_t i;
5333 for ( i = 0; i < lines.GetCount(); i++ )
5334 {
5335 dc.GetTextExtent( lines[i], &lineW, &lineH );
5336 w = wxMax( w, lineW );
5337 h += lineH;
5338 }
5339
5340 *width = w;
5341 *height = h;
5342 }
5343
5344
5345 //
5346 // ------ Edit control functions
5347 //
5348
5349
5350 void wxGrid::EnableEditing( bool edit )
5351 {
5352 // TODO: improve this ?
5353 //
5354 if ( edit != m_editable )
5355 {
5356 m_editable = edit;
5357
5358 // FIXME IMHO this won't disable the edit control if edit == FALSE
5359 // because of the check in the beginning of
5360 // EnableCellEditControl() just below (VZ)
5361 EnableCellEditControl(m_editable);
5362 }
5363 }
5364
5365
5366 void wxGrid::EnableCellEditControl( bool enable )
5367 {
5368 if (! m_editable)
5369 return;
5370
5371 if ( m_currentCellCoords == wxGridNoCellCoords )
5372 SetCurrentCell( 0, 0 );
5373
5374 if ( enable != m_cellEditCtrlEnabled )
5375 {
5376 // TODO allow the app to Veto() this event?
5377 SendEvent(enable ? wxEVT_GRID_EDITOR_SHOWN : wxEVT_GRID_EDITOR_HIDDEN);
5378
5379 if ( enable )
5380 {
5381 // this should be checked by the caller!
5382 wxASSERT_MSG( CanEnableCellControl(),
5383 _T("can't enable editing for this cell!") );
5384
5385 // do it before ShowCellEditControl()
5386 m_cellEditCtrlEnabled = enable;
5387
5388 ShowCellEditControl();
5389 }
5390 else
5391 {
5392 HideCellEditControl();
5393 SaveEditControlValue();
5394
5395 // do it after HideCellEditControl()
5396 m_cellEditCtrlEnabled = enable;
5397 }
5398 }
5399 }
5400
5401 bool wxGrid::IsCurrentCellReadOnly() const
5402 {
5403 // const_cast
5404 wxGridCellAttr* attr = ((wxGrid *)this)->GetCellAttr(m_currentCellCoords);
5405 bool readonly = attr->IsReadOnly();
5406 attr->DecRef();
5407
5408 return readonly;
5409 }
5410
5411 bool wxGrid::CanEnableCellControl() const
5412 {
5413 return m_editable && !IsCurrentCellReadOnly();
5414 }
5415
5416 bool wxGrid::IsCellEditControlEnabled() const
5417 {
5418 // the cell edit control might be disable for all cells or just for the
5419 // current one if it's read only
5420 return m_cellEditCtrlEnabled ? !IsCurrentCellReadOnly() : FALSE;
5421 }
5422
5423 void wxGrid::ShowCellEditControl()
5424 {
5425 if ( IsCellEditControlEnabled() )
5426 {
5427 if ( !IsVisible( m_currentCellCoords ) )
5428 {
5429 return;
5430 }
5431 else
5432 {
5433 wxRect rect = CellToRect( m_currentCellCoords );
5434 int row = m_currentCellCoords.GetRow();
5435 int col = m_currentCellCoords.GetCol();
5436
5437 // convert to scrolled coords
5438 //
5439 CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
5440
5441 // done in PaintBackground()
5442 #if 0
5443 // erase the highlight and the cell contents because the editor
5444 // might not cover the entire cell
5445 wxClientDC dc( m_gridWin );
5446 PrepareDC( dc );
5447 dc.SetBrush(*wxLIGHT_GREY_BRUSH); //wxBrush(attr->GetBackgroundColour(), wxSOLID));
5448 dc.SetPen(*wxTRANSPARENT_PEN);
5449 dc.DrawRectangle(rect);
5450 #endif // 0
5451
5452 // cell is shifted by one pixel
5453 rect.x--;
5454 rect.y--;
5455
5456 wxGridCellAttr* attr = GetCellAttr(row, col);
5457 wxGridCellEditor* editor = attr->GetEditor(this, row, col);
5458 if ( !editor->IsCreated() )
5459 {
5460 editor->Create(m_gridWin, -1,
5461 new wxGridCellEditorEvtHandler(this, editor));
5462 }
5463
5464 editor->Show( TRUE, attr );
5465
5466 editor->SetSize( rect );
5467
5468 editor->BeginEdit(row, col, this);
5469 attr->DecRef();
5470 }
5471 }
5472 }
5473
5474
5475 void wxGrid::HideCellEditControl()
5476 {
5477 if ( IsCellEditControlEnabled() )
5478 {
5479 int row = m_currentCellCoords.GetRow();
5480 int col = m_currentCellCoords.GetCol();
5481
5482 wxGridCellAttr* attr = GetCellAttr(row, col);
5483 attr->GetEditor(this, row, col)->Show( FALSE );
5484 attr->DecRef();
5485 m_gridWin->SetFocus();
5486 }
5487 }
5488
5489
5490 void wxGrid::SaveEditControlValue()
5491 {
5492 if ( IsCellEditControlEnabled() )
5493 {
5494 int row = m_currentCellCoords.GetRow();
5495 int col = m_currentCellCoords.GetCol();
5496
5497 wxGridCellAttr* attr = GetCellAttr(row, col);
5498 wxGridCellEditor* editor = attr->GetEditor(this, row, col);
5499 bool changed = editor->EndEdit(row, col, this);
5500
5501 attr->DecRef();
5502
5503 if (changed)
5504 {
5505 SendEvent( wxEVT_GRID_CELL_CHANGE,
5506 m_currentCellCoords.GetRow(),
5507 m_currentCellCoords.GetCol() );
5508 }
5509 }
5510 }
5511
5512
5513 //
5514 // ------ Grid location functions
5515 // Note that all of these functions work with the logical coordinates of
5516 // grid cells and labels so you will need to convert from device
5517 // coordinates for mouse events etc.
5518 //
5519
5520 void wxGrid::XYToCell( int x, int y, wxGridCellCoords& coords )
5521 {
5522 int row = YToRow(y);
5523 int col = XToCol(x);
5524
5525 if ( row == -1 || col == -1 )
5526 {
5527 coords = wxGridNoCellCoords;
5528 }
5529 else
5530 {
5531 coords.Set( row, col );
5532 }
5533 }
5534
5535
5536 int wxGrid::YToRow( int y )
5537 {
5538 int i;
5539
5540 for ( i = 0; i < m_numRows; i++ )
5541 {
5542 if ( y < GetRowBottom(i) )
5543 return i;
5544 }
5545
5546 return -1;
5547 }
5548
5549
5550 int wxGrid::XToCol( int x )
5551 {
5552 int i;
5553
5554 for ( i = 0; i < m_numCols; i++ )
5555 {
5556 if ( x < GetColRight(i) )
5557 return i;
5558 }
5559
5560 return -1;
5561 }
5562
5563
5564 // return the row number that that the y coord is near the edge of, or
5565 // -1 if not near an edge
5566 //
5567 int wxGrid::YToEdgeOfRow( int y )
5568 {
5569 int i, d;
5570
5571 for ( i = 0; i < m_numRows; i++ )
5572 {
5573 if ( GetRowHeight(i) > WXGRID_LABEL_EDGE_ZONE )
5574 {
5575 d = abs( y - GetRowBottom(i) );
5576 if ( d < WXGRID_LABEL_EDGE_ZONE )
5577 return i;
5578 }
5579 }
5580
5581 return -1;
5582 }
5583
5584
5585 // return the col number that that the x coord is near the edge of, or
5586 // -1 if not near an edge
5587 //
5588 int wxGrid::XToEdgeOfCol( int x )
5589 {
5590 int i, d;
5591
5592 for ( i = 0; i < m_numCols; i++ )
5593 {
5594 if ( GetColWidth(i) > WXGRID_LABEL_EDGE_ZONE )
5595 {
5596 d = abs( x - GetColRight(i) );
5597 if ( d < WXGRID_LABEL_EDGE_ZONE )
5598 return i;
5599 }
5600 }
5601
5602 return -1;
5603 }
5604
5605
5606 wxRect wxGrid::CellToRect( int row, int col )
5607 {
5608 wxRect rect( -1, -1, -1, -1 );
5609
5610 if ( row >= 0 && row < m_numRows &&
5611 col >= 0 && col < m_numCols )
5612 {
5613 rect.x = GetColLeft(col);
5614 rect.y = GetRowTop(row);
5615 rect.width = GetColWidth(col);
5616 rect.height = GetRowHeight(row);
5617 }
5618
5619 return rect;
5620 }
5621
5622
5623 bool wxGrid::IsVisible( int row, int col, bool wholeCellVisible )
5624 {
5625 // get the cell rectangle in logical coords
5626 //
5627 wxRect r( CellToRect( row, col ) );
5628
5629 // convert to device coords
5630 //
5631 int left, top, right, bottom;
5632 CalcScrolledPosition( r.GetLeft(), r.GetTop(), &left, &top );
5633 CalcScrolledPosition( r.GetRight(), r.GetBottom(), &right, &bottom );
5634
5635 // check against the client area of the grid window
5636 //
5637 int cw, ch;
5638 m_gridWin->GetClientSize( &cw, &ch );
5639
5640 if ( wholeCellVisible )
5641 {
5642 // is the cell wholly visible ?
5643 //
5644 return ( left >= 0 && right <= cw &&
5645 top >= 0 && bottom <= ch );
5646 }
5647 else
5648 {
5649 // is the cell partly visible ?
5650 //
5651 return ( ((left >=0 && left < cw) || (right > 0 && right <= cw)) &&
5652 ((top >=0 && top < ch) || (bottom > 0 && bottom <= ch)) );
5653 }
5654 }
5655
5656
5657 // make the specified cell location visible by doing a minimal amount
5658 // of scrolling
5659 //
5660 void wxGrid::MakeCellVisible( int row, int col )
5661 {
5662 int i;
5663 int xpos = -1, ypos = -1;
5664
5665 if ( row >= 0 && row < m_numRows &&
5666 col >= 0 && col < m_numCols )
5667 {
5668 // get the cell rectangle in logical coords
5669 //
5670 wxRect r( CellToRect( row, col ) );
5671
5672 // convert to device coords
5673 //
5674 int left, top, right, bottom;
5675 CalcScrolledPosition( r.GetLeft(), r.GetTop(), &left, &top );
5676 CalcScrolledPosition( r.GetRight(), r.GetBottom(), &right, &bottom );
5677
5678 int cw, ch;
5679 m_gridWin->GetClientSize( &cw, &ch );
5680
5681 if ( top < 0 )
5682 {
5683 ypos = r.GetTop();
5684 }
5685 else if ( bottom > ch )
5686 {
5687 int h = r.GetHeight();
5688 ypos = r.GetTop();
5689 for ( i = row-1; i >= 0; i-- )
5690 {
5691 int rowHeight = GetRowHeight(i);
5692 if ( h + rowHeight > ch )
5693 break;
5694
5695 h += rowHeight;
5696 ypos -= rowHeight;
5697 }
5698
5699 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
5700 // have rounding errors (this is important, because if we do, we
5701 // might not scroll at all and some cells won't be redrawn)
5702 ypos += GRID_SCROLL_LINE / 2;
5703 }
5704
5705 if ( left < 0 )
5706 {
5707 xpos = r.GetLeft();
5708 }
5709 else if ( right > cw )
5710 {
5711 int w = r.GetWidth();
5712 xpos = r.GetLeft();
5713 for ( i = col-1; i >= 0; i-- )
5714 {
5715 int colWidth = GetColWidth(i);
5716 if ( w + colWidth > cw )
5717 break;
5718
5719 w += colWidth;
5720 xpos -= colWidth;
5721 }
5722
5723 // see comment for ypos above
5724 xpos += GRID_SCROLL_LINE / 2;
5725 }
5726
5727 if ( xpos != -1 || ypos != -1 )
5728 {
5729 if ( xpos != -1 ) xpos /= GRID_SCROLL_LINE;
5730 if ( ypos != -1 ) ypos /= GRID_SCROLL_LINE;
5731 Scroll( xpos, ypos );
5732 AdjustScrollbars();
5733 }
5734 }
5735 }
5736
5737
5738 //
5739 // ------ Grid cursor movement functions
5740 //
5741
5742 bool wxGrid::MoveCursorUp()
5743 {
5744 if ( m_currentCellCoords != wxGridNoCellCoords &&
5745 m_currentCellCoords.GetRow() > 0 )
5746 {
5747 MakeCellVisible( m_currentCellCoords.GetRow() - 1,
5748 m_currentCellCoords.GetCol() );
5749
5750 SetCurrentCell( m_currentCellCoords.GetRow() - 1,
5751 m_currentCellCoords.GetCol() );
5752
5753 return TRUE;
5754 }
5755
5756 return FALSE;
5757 }
5758
5759
5760 bool wxGrid::MoveCursorDown()
5761 {
5762 // TODO: allow for scrolling
5763 //
5764 if ( m_currentCellCoords != wxGridNoCellCoords &&
5765 m_currentCellCoords.GetRow() < m_numRows-1 )
5766 {
5767 MakeCellVisible( m_currentCellCoords.GetRow() + 1,
5768 m_currentCellCoords.GetCol() );
5769
5770 SetCurrentCell( m_currentCellCoords.GetRow() + 1,
5771 m_currentCellCoords.GetCol() );
5772
5773 return TRUE;
5774 }
5775
5776 return FALSE;
5777 }
5778
5779
5780 bool wxGrid::MoveCursorLeft()
5781 {
5782 if ( m_currentCellCoords != wxGridNoCellCoords &&
5783 m_currentCellCoords.GetCol() > 0 )
5784 {
5785 MakeCellVisible( m_currentCellCoords.GetRow(),
5786 m_currentCellCoords.GetCol() - 1 );
5787
5788 SetCurrentCell( m_currentCellCoords.GetRow(),
5789 m_currentCellCoords.GetCol() - 1 );
5790
5791 return TRUE;
5792 }
5793
5794 return FALSE;
5795 }
5796
5797
5798 bool wxGrid::MoveCursorRight()
5799 {
5800 if ( m_currentCellCoords != wxGridNoCellCoords &&
5801 m_currentCellCoords.GetCol() < m_numCols - 1 )
5802 {
5803 MakeCellVisible( m_currentCellCoords.GetRow(),
5804 m_currentCellCoords.GetCol() + 1 );
5805
5806 SetCurrentCell( m_currentCellCoords.GetRow(),
5807 m_currentCellCoords.GetCol() + 1 );
5808
5809 return TRUE;
5810 }
5811
5812 return FALSE;
5813 }
5814
5815
5816 bool wxGrid::MovePageUp()
5817 {
5818 if ( m_currentCellCoords == wxGridNoCellCoords ) return FALSE;
5819
5820 int row = m_currentCellCoords.GetRow();
5821 if ( row > 0 )
5822 {
5823 int cw, ch;
5824 m_gridWin->GetClientSize( &cw, &ch );
5825
5826 int y = GetRowTop(row);
5827 int newRow = YToRow( y - ch + 1 );
5828 if ( newRow == -1 )
5829 {
5830 newRow = 0;
5831 }
5832 else if ( newRow == row )
5833 {
5834 newRow = row - 1;
5835 }
5836
5837 MakeCellVisible( newRow, m_currentCellCoords.GetCol() );
5838 SetCurrentCell( newRow, m_currentCellCoords.GetCol() );
5839
5840 return TRUE;
5841 }
5842
5843 return FALSE;
5844 }
5845
5846 bool wxGrid::MovePageDown()
5847 {
5848 if ( m_currentCellCoords == wxGridNoCellCoords ) return FALSE;
5849
5850 int row = m_currentCellCoords.GetRow();
5851 if ( row < m_numRows )
5852 {
5853 int cw, ch;
5854 m_gridWin->GetClientSize( &cw, &ch );
5855
5856 int y = GetRowTop(row);
5857 int newRow = YToRow( y + ch );
5858 if ( newRow == -1 )
5859 {
5860 newRow = m_numRows - 1;
5861 }
5862 else if ( newRow == row )
5863 {
5864 newRow = row + 1;
5865 }
5866
5867 MakeCellVisible( newRow, m_currentCellCoords.GetCol() );
5868 SetCurrentCell( newRow, m_currentCellCoords.GetCol() );
5869
5870 return TRUE;
5871 }
5872
5873 return FALSE;
5874 }
5875
5876 bool wxGrid::MoveCursorUpBlock()
5877 {
5878 if ( m_table &&
5879 m_currentCellCoords != wxGridNoCellCoords &&
5880 m_currentCellCoords.GetRow() > 0 )
5881 {
5882 int row = m_currentCellCoords.GetRow();
5883 int col = m_currentCellCoords.GetCol();
5884
5885 if ( m_table->IsEmptyCell(row, col) )
5886 {
5887 // starting in an empty cell: find the next block of
5888 // non-empty cells
5889 //
5890 while ( row > 0 )
5891 {
5892 row-- ;
5893 if ( !(m_table->IsEmptyCell(row, col)) ) break;
5894 }
5895 }
5896 else if ( m_table->IsEmptyCell(row-1, col) )
5897 {
5898 // starting at the top of a block: find the next block
5899 //
5900 row--;
5901 while ( row > 0 )
5902 {
5903 row-- ;
5904 if ( !(m_table->IsEmptyCell(row, col)) ) break;
5905 }
5906 }
5907 else
5908 {
5909 // starting within a block: find the top of the block
5910 //
5911 while ( row > 0 )
5912 {
5913 row-- ;
5914 if ( m_table->IsEmptyCell(row, col) )
5915 {
5916 row++ ;
5917 break;
5918 }
5919 }
5920 }
5921
5922 MakeCellVisible( row, col );
5923 SetCurrentCell( row, col );
5924
5925 return TRUE;
5926 }
5927
5928 return FALSE;
5929 }
5930
5931 bool wxGrid::MoveCursorDownBlock()
5932 {
5933 if ( m_table &&
5934 m_currentCellCoords != wxGridNoCellCoords &&
5935 m_currentCellCoords.GetRow() < m_numRows-1 )
5936 {
5937 int row = m_currentCellCoords.GetRow();
5938 int col = m_currentCellCoords.GetCol();
5939
5940 if ( m_table->IsEmptyCell(row, col) )
5941 {
5942 // starting in an empty cell: find the next block of
5943 // non-empty cells
5944 //
5945 while ( row < m_numRows-1 )
5946 {
5947 row++ ;
5948 if ( !(m_table->IsEmptyCell(row, col)) ) break;
5949 }
5950 }
5951 else if ( m_table->IsEmptyCell(row+1, col) )
5952 {
5953 // starting at the bottom of a block: find the next block
5954 //
5955 row++;
5956 while ( row < m_numRows-1 )
5957 {
5958 row++ ;
5959 if ( !(m_table->IsEmptyCell(row, col)) ) break;
5960 }
5961 }
5962 else
5963 {
5964 // starting within a block: find the bottom of the block
5965 //
5966 while ( row < m_numRows-1 )
5967 {
5968 row++ ;
5969 if ( m_table->IsEmptyCell(row, col) )
5970 {
5971 row-- ;
5972 break;
5973 }
5974 }
5975 }
5976
5977 MakeCellVisible( row, col );
5978 SetCurrentCell( row, col );
5979
5980 return TRUE;
5981 }
5982
5983 return FALSE;
5984 }
5985
5986 bool wxGrid::MoveCursorLeftBlock()
5987 {
5988 if ( m_table &&
5989 m_currentCellCoords != wxGridNoCellCoords &&
5990 m_currentCellCoords.GetCol() > 0 )
5991 {
5992 int row = m_currentCellCoords.GetRow();
5993 int col = m_currentCellCoords.GetCol();
5994
5995 if ( m_table->IsEmptyCell(row, col) )
5996 {
5997 // starting in an empty cell: find the next block of
5998 // non-empty cells
5999 //
6000 while ( col > 0 )
6001 {
6002 col-- ;
6003 if ( !(m_table->IsEmptyCell(row, col)) ) break;
6004 }
6005 }
6006 else if ( m_table->IsEmptyCell(row, col-1) )
6007 {
6008 // starting at the left of a block: find the next block
6009 //
6010 col--;
6011 while ( col > 0 )
6012 {
6013 col-- ;
6014 if ( !(m_table->IsEmptyCell(row, col)) ) break;
6015 }
6016 }
6017 else
6018 {
6019 // starting within a block: find the left of the block
6020 //
6021 while ( col > 0 )
6022 {
6023 col-- ;
6024 if ( m_table->IsEmptyCell(row, col) )
6025 {
6026 col++ ;
6027 break;
6028 }
6029 }
6030 }
6031
6032 MakeCellVisible( row, col );
6033 SetCurrentCell( row, col );
6034
6035 return TRUE;
6036 }
6037
6038 return FALSE;
6039 }
6040
6041 bool wxGrid::MoveCursorRightBlock()
6042 {
6043 if ( m_table &&
6044 m_currentCellCoords != wxGridNoCellCoords &&
6045 m_currentCellCoords.GetCol() < m_numCols-1 )
6046 {
6047 int row = m_currentCellCoords.GetRow();
6048 int col = m_currentCellCoords.GetCol();
6049
6050 if ( m_table->IsEmptyCell(row, col) )
6051 {
6052 // starting in an empty cell: find the next block of
6053 // non-empty cells
6054 //
6055 while ( col < m_numCols-1 )
6056 {
6057 col++ ;
6058 if ( !(m_table->IsEmptyCell(row, col)) ) break;
6059 }
6060 }
6061 else if ( m_table->IsEmptyCell(row, col+1) )
6062 {
6063 // starting at the right of a block: find the next block
6064 //
6065 col++;
6066 while ( col < m_numCols-1 )
6067 {
6068 col++ ;
6069 if ( !(m_table->IsEmptyCell(row, col)) ) break;
6070 }
6071 }
6072 else
6073 {
6074 // starting within a block: find the right of the block
6075 //
6076 while ( col < m_numCols-1 )
6077 {
6078 col++ ;
6079 if ( m_table->IsEmptyCell(row, col) )
6080 {
6081 col-- ;
6082 break;
6083 }
6084 }
6085 }
6086
6087 MakeCellVisible( row, col );
6088 SetCurrentCell( row, col );
6089
6090 return TRUE;
6091 }
6092
6093 return FALSE;
6094 }
6095
6096
6097
6098 //
6099 // ------ Label values and formatting
6100 //
6101
6102 void wxGrid::GetRowLabelAlignment( int *horiz, int *vert )
6103 {
6104 *horiz = m_rowLabelHorizAlign;
6105 *vert = m_rowLabelVertAlign;
6106 }
6107
6108 void wxGrid::GetColLabelAlignment( int *horiz, int *vert )
6109 {
6110 *horiz = m_colLabelHorizAlign;
6111 *vert = m_colLabelVertAlign;
6112 }
6113
6114 wxString wxGrid::GetRowLabelValue( int row )
6115 {
6116 if ( m_table )
6117 {
6118 return m_table->GetRowLabelValue( row );
6119 }
6120 else
6121 {
6122 wxString s;
6123 s << row;
6124 return s;
6125 }
6126 }
6127
6128 wxString wxGrid::GetColLabelValue( int col )
6129 {
6130 if ( m_table )
6131 {
6132 return m_table->GetColLabelValue( col );
6133 }
6134 else
6135 {
6136 wxString s;
6137 s << col;
6138 return s;
6139 }
6140 }
6141
6142
6143 void wxGrid::SetRowLabelSize( int width )
6144 {
6145 width = wxMax( width, 0 );
6146 if ( width != m_rowLabelWidth )
6147 {
6148 if ( width == 0 )
6149 {
6150 m_rowLabelWin->Show( FALSE );
6151 m_cornerLabelWin->Show( FALSE );
6152 }
6153 else if ( m_rowLabelWidth == 0 )
6154 {
6155 m_rowLabelWin->Show( TRUE );
6156 if ( m_colLabelHeight > 0 ) m_cornerLabelWin->Show( TRUE );
6157 }
6158
6159 m_rowLabelWidth = width;
6160 CalcWindowSizes();
6161 Refresh( TRUE );
6162 }
6163 }
6164
6165
6166 void wxGrid::SetColLabelSize( int height )
6167 {
6168 height = wxMax( height, 0 );
6169 if ( height != m_colLabelHeight )
6170 {
6171 if ( height == 0 )
6172 {
6173 m_colLabelWin->Show( FALSE );
6174 m_cornerLabelWin->Show( FALSE );
6175 }
6176 else if ( m_colLabelHeight == 0 )
6177 {
6178 m_colLabelWin->Show( TRUE );
6179 if ( m_rowLabelWidth > 0 ) m_cornerLabelWin->Show( TRUE );
6180 }
6181
6182 m_colLabelHeight = height;
6183 CalcWindowSizes();
6184 Refresh( TRUE );
6185 }
6186 }
6187
6188
6189 void wxGrid::SetLabelBackgroundColour( const wxColour& colour )
6190 {
6191 if ( m_labelBackgroundColour != colour )
6192 {
6193 m_labelBackgroundColour = colour;
6194 m_rowLabelWin->SetBackgroundColour( colour );
6195 m_colLabelWin->SetBackgroundColour( colour );
6196 m_cornerLabelWin->SetBackgroundColour( colour );
6197
6198 if ( !GetBatchCount() )
6199 {
6200 m_rowLabelWin->Refresh();
6201 m_colLabelWin->Refresh();
6202 m_cornerLabelWin->Refresh();
6203 }
6204 }
6205 }
6206
6207 void wxGrid::SetLabelTextColour( const wxColour& colour )
6208 {
6209 if ( m_labelTextColour != colour )
6210 {
6211 m_labelTextColour = colour;
6212 if ( !GetBatchCount() )
6213 {
6214 m_rowLabelWin->Refresh();
6215 m_colLabelWin->Refresh();
6216 }
6217 }
6218 }
6219
6220 void wxGrid::SetLabelFont( const wxFont& font )
6221 {
6222 m_labelFont = font;
6223 if ( !GetBatchCount() )
6224 {
6225 m_rowLabelWin->Refresh();
6226 m_colLabelWin->Refresh();
6227 }
6228 }
6229
6230 void wxGrid::SetRowLabelAlignment( int horiz, int vert )
6231 {
6232 if ( horiz == wxLEFT || horiz == wxCENTRE || horiz == wxRIGHT )
6233 {
6234 m_rowLabelHorizAlign = horiz;
6235 }
6236
6237 if ( vert == wxTOP || vert == wxCENTRE || vert == wxBOTTOM )
6238 {
6239 m_rowLabelVertAlign = vert;
6240 }
6241
6242 if ( !GetBatchCount() )
6243 {
6244 m_rowLabelWin->Refresh();
6245 }
6246 }
6247
6248 void wxGrid::SetColLabelAlignment( int horiz, int vert )
6249 {
6250 if ( horiz == wxLEFT || horiz == wxCENTRE || horiz == wxRIGHT )
6251 {
6252 m_colLabelHorizAlign = horiz;
6253 }
6254
6255 if ( vert == wxTOP || vert == wxCENTRE || vert == wxBOTTOM )
6256 {
6257 m_colLabelVertAlign = vert;
6258 }
6259
6260 if ( !GetBatchCount() )
6261 {
6262 m_colLabelWin->Refresh();
6263 }
6264 }
6265
6266 void wxGrid::SetRowLabelValue( int row, const wxString& s )
6267 {
6268 if ( m_table )
6269 {
6270 m_table->SetRowLabelValue( row, s );
6271 if ( !GetBatchCount() )
6272 {
6273 wxRect rect = CellToRect( row, 0);
6274 if ( rect.height > 0 )
6275 {
6276 CalcScrolledPosition(0, rect.y, &rect.x, &rect.y);
6277 rect.x = m_left;
6278 rect.width = m_rowLabelWidth;
6279 m_rowLabelWin->Refresh( TRUE, &rect );
6280 }
6281 }
6282 }
6283 }
6284
6285 void wxGrid::SetColLabelValue( int col, const wxString& s )
6286 {
6287 if ( m_table )
6288 {
6289 m_table->SetColLabelValue( col, s );
6290 if ( !GetBatchCount() )
6291 {
6292 wxRect rect = CellToRect( 0, col );
6293 if ( rect.width > 0 )
6294 {
6295 CalcScrolledPosition(rect.x, 0, &rect.x, &rect.y);
6296 rect.y = m_top;
6297 rect.height = m_colLabelHeight;
6298 m_colLabelWin->Refresh( TRUE, &rect );
6299 }
6300 }
6301 }
6302 }
6303
6304 void wxGrid::SetGridLineColour( const wxColour& colour )
6305 {
6306 if ( m_gridLineColour != colour )
6307 {
6308 m_gridLineColour = colour;
6309
6310 wxClientDC dc( m_gridWin );
6311 PrepareDC( dc );
6312 DrawAllGridLines( dc, wxRegion() );
6313 }
6314 }
6315
6316 void wxGrid::EnableGridLines( bool enable )
6317 {
6318 if ( enable != m_gridLinesEnabled )
6319 {
6320 m_gridLinesEnabled = enable;
6321
6322 if ( !GetBatchCount() )
6323 {
6324 if ( enable )
6325 {
6326 wxClientDC dc( m_gridWin );
6327 PrepareDC( dc );
6328 DrawAllGridLines( dc, wxRegion() );
6329 }
6330 else
6331 {
6332 m_gridWin->Refresh();
6333 }
6334 }
6335 }
6336 }
6337
6338
6339 int wxGrid::GetDefaultRowSize()
6340 {
6341 return m_defaultRowHeight;
6342 }
6343
6344 int wxGrid::GetRowSize( int row )
6345 {
6346 wxCHECK_MSG( row >= 0 && row < m_numRows, 0, _T("invalid row index") );
6347
6348 return GetRowHeight(row);
6349 }
6350
6351 int wxGrid::GetDefaultColSize()
6352 {
6353 return m_defaultColWidth;
6354 }
6355
6356 int wxGrid::GetColSize( int col )
6357 {
6358 wxCHECK_MSG( col >= 0 && col < m_numCols, 0, _T("invalid column index") );
6359
6360 return GetColWidth(col);
6361 }
6362
6363 // ============================================================================
6364 // access to the grid attributes: each of them has a default value in the grid
6365 // itself and may be overidden on a per-cell basis
6366 // ============================================================================
6367
6368 // ----------------------------------------------------------------------------
6369 // setting default attributes
6370 // ----------------------------------------------------------------------------
6371
6372 void wxGrid::SetDefaultCellBackgroundColour( const wxColour& col )
6373 {
6374 m_defaultCellAttr->SetBackgroundColour(col);
6375 #ifdef __WXGTK__
6376 m_gridWin->SetBackgroundColour(col);
6377 #endif
6378 }
6379
6380 void wxGrid::SetDefaultCellTextColour( const wxColour& col )
6381 {
6382 m_defaultCellAttr->SetTextColour(col);
6383 }
6384
6385 void wxGrid::SetDefaultCellAlignment( int horiz, int vert )
6386 {
6387 m_defaultCellAttr->SetAlignment(horiz, vert);
6388 }
6389
6390 void wxGrid::SetDefaultCellFont( const wxFont& font )
6391 {
6392 m_defaultCellAttr->SetFont(font);
6393 }
6394
6395 void wxGrid::SetDefaultRenderer(wxGridCellRenderer *renderer)
6396 {
6397 m_defaultCellAttr->SetRenderer(renderer);
6398 }
6399
6400 void wxGrid::SetDefaultEditor(wxGridCellEditor *editor)
6401 {
6402 m_defaultCellAttr->SetEditor(editor);
6403 }
6404
6405 // ----------------------------------------------------------------------------
6406 // access to the default attrbiutes
6407 // ----------------------------------------------------------------------------
6408
6409 wxColour wxGrid::GetDefaultCellBackgroundColour()
6410 {
6411 return m_defaultCellAttr->GetBackgroundColour();
6412 }
6413
6414 wxColour wxGrid::GetDefaultCellTextColour()
6415 {
6416 return m_defaultCellAttr->GetTextColour();
6417 }
6418
6419 wxFont wxGrid::GetDefaultCellFont()
6420 {
6421 return m_defaultCellAttr->GetFont();
6422 }
6423
6424 void wxGrid::GetDefaultCellAlignment( int *horiz, int *vert )
6425 {
6426 m_defaultCellAttr->GetAlignment(horiz, vert);
6427 }
6428
6429 wxGridCellRenderer *wxGrid::GetDefaultRenderer() const
6430 {
6431 return m_defaultCellAttr->GetRenderer(NULL,0,0);
6432 }
6433
6434 wxGridCellEditor *wxGrid::GetDefaultEditor() const
6435 {
6436 return m_defaultCellAttr->GetEditor(NULL,0,0);
6437 }
6438
6439 // ----------------------------------------------------------------------------
6440 // access to cell attributes
6441 // ----------------------------------------------------------------------------
6442
6443 wxColour wxGrid::GetCellBackgroundColour(int row, int col)
6444 {
6445 wxGridCellAttr *attr = GetCellAttr(row, col);
6446 wxColour colour = attr->GetBackgroundColour();
6447 attr->SafeDecRef();
6448 return colour;
6449 }
6450
6451 wxColour wxGrid::GetCellTextColour( int row, int col )
6452 {
6453 wxGridCellAttr *attr = GetCellAttr(row, col);
6454 wxColour colour = attr->GetTextColour();
6455 attr->SafeDecRef();
6456 return colour;
6457 }
6458
6459 wxFont wxGrid::GetCellFont( int row, int col )
6460 {
6461 wxGridCellAttr *attr = GetCellAttr(row, col);
6462 wxFont font = attr->GetFont();
6463 attr->SafeDecRef();
6464 return font;
6465 }
6466
6467 void wxGrid::GetCellAlignment( int row, int col, int *horiz, int *vert )
6468 {
6469 wxGridCellAttr *attr = GetCellAttr(row, col);
6470 attr->GetAlignment(horiz, vert);
6471 attr->SafeDecRef();
6472 }
6473
6474 wxGridCellRenderer* wxGrid::GetCellRenderer(int row, int col)
6475 {
6476 wxGridCellAttr* attr = GetCellAttr(row, col);
6477 wxGridCellRenderer* renderer = attr->GetRenderer(this, row, col);
6478 attr->DecRef();
6479 return renderer;
6480 }
6481
6482 wxGridCellEditor* wxGrid::GetCellEditor(int row, int col)
6483 {
6484 wxGridCellAttr* attr = GetCellAttr(row, col);
6485 wxGridCellEditor* editor = attr->GetEditor(this, row, col);
6486 attr->DecRef();
6487 return editor;
6488 }
6489
6490 bool wxGrid::IsReadOnly(int row, int col) const
6491 {
6492 wxGridCellAttr* attr = GetCellAttr(row, col);
6493 bool isReadOnly = attr->IsReadOnly();
6494 attr->DecRef();
6495 return isReadOnly;
6496 }
6497
6498 // ----------------------------------------------------------------------------
6499 // attribute support: cache, automatic provider creation, ...
6500 // ----------------------------------------------------------------------------
6501
6502 bool wxGrid::CanHaveAttributes()
6503 {
6504 if ( !m_table )
6505 {
6506 return FALSE;
6507 }
6508
6509 return m_table->CanHaveAttributes();
6510 }
6511
6512 void wxGrid::ClearAttrCache()
6513 {
6514 if ( m_attrCache.row != -1 )
6515 {
6516 m_attrCache.attr->SafeDecRef();
6517 m_attrCache.row = -1;
6518 }
6519 }
6520
6521 void wxGrid::CacheAttr(int row, int col, wxGridCellAttr *attr) const
6522 {
6523 wxGrid *self = (wxGrid *)this; // const_cast
6524
6525 self->ClearAttrCache();
6526 self->m_attrCache.row = row;
6527 self->m_attrCache.col = col;
6528 self->m_attrCache.attr = attr;
6529 attr->SafeIncRef();
6530 }
6531
6532 bool wxGrid::LookupAttr(int row, int col, wxGridCellAttr **attr) const
6533 {
6534 if ( row == m_attrCache.row && col == m_attrCache.col )
6535 {
6536 *attr = m_attrCache.attr;
6537 (*attr)->SafeIncRef();
6538
6539 #ifdef DEBUG_ATTR_CACHE
6540 gs_nAttrCacheHits++;
6541 #endif
6542
6543 return TRUE;
6544 }
6545 else
6546 {
6547 #ifdef DEBUG_ATTR_CACHE
6548 gs_nAttrCacheMisses++;
6549 #endif
6550 return FALSE;
6551 }
6552 }
6553
6554 wxGridCellAttr *wxGrid::GetCellAttr(int row, int col) const
6555 {
6556 wxGridCellAttr *attr;
6557 if ( !LookupAttr(row, col, &attr) )
6558 {
6559 attr = m_table ? m_table->GetAttr(row, col) : (wxGridCellAttr *)NULL;
6560 CacheAttr(row, col, attr);
6561 }
6562 if (attr)
6563 {
6564 attr->SetDefAttr(m_defaultCellAttr);
6565 }
6566 else
6567 {
6568 attr = m_defaultCellAttr;
6569 attr->IncRef();
6570 }
6571
6572 return attr;
6573 }
6574
6575 wxGridCellAttr *wxGrid::GetOrCreateCellAttr(int row, int col) const
6576 {
6577 wxGridCellAttr *attr;
6578 if ( !LookupAttr(row, col, &attr) || !attr )
6579 {
6580 wxASSERT_MSG( m_table,
6581 _T("we may only be called if CanHaveAttributes() "
6582 "returned TRUE and then m_table should be !NULL") );
6583
6584 attr = m_table->GetAttr(row, col);
6585 if ( !attr )
6586 {
6587 attr = new wxGridCellAttr;
6588
6589 // artificially inc the ref count to match DecRef() in caller
6590 attr->IncRef();
6591
6592 m_table->SetAttr(attr, row, col);
6593 }
6594
6595 CacheAttr(row, col, attr);
6596 }
6597 attr->SetDefAttr(m_defaultCellAttr);
6598 return attr;
6599 }
6600
6601 // ----------------------------------------------------------------------------
6602 // setting cell attributes: this is forwarded to the table
6603 // ----------------------------------------------------------------------------
6604
6605 void wxGrid::SetRowAttr(int row, wxGridCellAttr *attr)
6606 {
6607 if ( CanHaveAttributes() )
6608 {
6609 m_table->SetRowAttr(attr, row);
6610 }
6611 else
6612 {
6613 attr->SafeDecRef();
6614 }
6615 }
6616
6617 void wxGrid::SetColAttr(int col, wxGridCellAttr *attr)
6618 {
6619 if ( CanHaveAttributes() )
6620 {
6621 m_table->SetColAttr(attr, col);
6622 }
6623 else
6624 {
6625 attr->SafeDecRef();
6626 }
6627 }
6628
6629 void wxGrid::SetCellBackgroundColour( int row, int col, const wxColour& colour )
6630 {
6631 if ( CanHaveAttributes() )
6632 {
6633 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
6634 attr->SetBackgroundColour(colour);
6635 attr->DecRef();
6636 }
6637 }
6638
6639 void wxGrid::SetCellTextColour( int row, int col, const wxColour& colour )
6640 {
6641 if ( CanHaveAttributes() )
6642 {
6643 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
6644 attr->SetTextColour(colour);
6645 attr->DecRef();
6646 }
6647 }
6648
6649 void wxGrid::SetCellFont( int row, int col, const wxFont& font )
6650 {
6651 if ( CanHaveAttributes() )
6652 {
6653 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
6654 attr->SetFont(font);
6655 attr->DecRef();
6656 }
6657 }
6658
6659 void wxGrid::SetCellAlignment( int row, int col, int horiz, int vert )
6660 {
6661 if ( CanHaveAttributes() )
6662 {
6663 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
6664 attr->SetAlignment(horiz, vert);
6665 attr->DecRef();
6666 }
6667 }
6668
6669 void wxGrid::SetCellRenderer(int row, int col, wxGridCellRenderer *renderer)
6670 {
6671 if ( CanHaveAttributes() )
6672 {
6673 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
6674 attr->SetRenderer(renderer);
6675 attr->DecRef();
6676 }
6677 }
6678
6679 void wxGrid::SetCellEditor(int row, int col, wxGridCellEditor* editor)
6680 {
6681 if ( CanHaveAttributes() )
6682 {
6683 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
6684 attr->SetEditor(editor);
6685 attr->DecRef();
6686 }
6687 }
6688
6689 void wxGrid::SetReadOnly(int row, int col, bool isReadOnly)
6690 {
6691 if ( CanHaveAttributes() )
6692 {
6693 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
6694 attr->SetReadOnly(isReadOnly);
6695 attr->DecRef();
6696 }
6697 }
6698
6699 // ----------------------------------------------------------------------------
6700 // Data type registration
6701 // ----------------------------------------------------------------------------
6702
6703 void wxGrid::RegisterDataType(const wxString& typeName,
6704 wxGridCellRenderer* renderer,
6705 wxGridCellEditor* editor)
6706 {
6707 m_typeRegistry->RegisterDataType(typeName, renderer, editor);
6708 }
6709
6710
6711 wxGridCellEditor* wxGrid::GetDefaultEditorForCell(int row, int col) const
6712 {
6713 wxString typeName = m_table->GetTypeName(row, col);
6714 return GetDefaultEditorForType(typeName);
6715 }
6716
6717 wxGridCellRenderer* wxGrid::GetDefaultRendererForCell(int row, int col) const
6718 {
6719 wxString typeName = m_table->GetTypeName(row, col);
6720 return GetDefaultRendererForType(typeName);
6721 }
6722
6723 wxGridCellEditor*
6724 wxGrid::GetDefaultEditorForType(const wxString& typeName) const
6725 {
6726 int index = m_typeRegistry->FindDataType(typeName);
6727 if (index == -1) {
6728 // Should we force the failure here or let it fallback to string handling???
6729 // wxFAIL_MSG(wxT("Unknown data type name"));
6730 return NULL;
6731 }
6732 return m_typeRegistry->GetEditor(index);
6733 }
6734
6735 wxGridCellRenderer*
6736 wxGrid::GetDefaultRendererForType(const wxString& typeName) const
6737 {
6738 int index = m_typeRegistry->FindDataType(typeName);
6739 if (index == -1) {
6740 // Should we force the failure here or let it fallback to string handling???
6741 // wxFAIL_MSG(wxT("Unknown data type name"));
6742 return NULL;
6743 }
6744 return m_typeRegistry->GetRenderer(index);
6745 }
6746
6747
6748 // ----------------------------------------------------------------------------
6749 // row/col size
6750 // ----------------------------------------------------------------------------
6751
6752 void wxGrid::EnableDragRowSize( bool enable )
6753 {
6754 m_canDragRowSize = enable;
6755 }
6756
6757
6758 void wxGrid::EnableDragColSize( bool enable )
6759 {
6760 m_canDragColSize = enable;
6761 }
6762
6763 void wxGrid::EnableDragGridSize( bool enable )
6764 {
6765 m_canDragGridSize = enable;
6766 }
6767
6768
6769 void wxGrid::SetDefaultRowSize( int height, bool resizeExistingRows )
6770 {
6771 m_defaultRowHeight = wxMax( height, WXGRID_MIN_ROW_HEIGHT );
6772
6773 if ( resizeExistingRows )
6774 {
6775 InitRowHeights();
6776
6777 CalcDimensions();
6778 }
6779 }
6780
6781 void wxGrid::SetRowSize( int row, int height )
6782 {
6783 wxCHECK_RET( row >= 0 && row < m_numRows, _T("invalid row index") );
6784
6785 if ( m_rowHeights.IsEmpty() )
6786 {
6787 // need to really create the array
6788 InitRowHeights();
6789 }
6790
6791 int h = wxMax( 0, height );
6792 int diff = h - m_rowHeights[row];
6793
6794 m_rowHeights[row] = h;
6795 int i;
6796 for ( i = row; i < m_numRows; i++ )
6797 {
6798 m_rowBottoms[i] += diff;
6799 }
6800 CalcDimensions();
6801 }
6802
6803 void wxGrid::SetDefaultColSize( int width, bool resizeExistingCols )
6804 {
6805 m_defaultColWidth = wxMax( width, WXGRID_MIN_COL_WIDTH );
6806
6807 if ( resizeExistingCols )
6808 {
6809 InitColWidths();
6810
6811 CalcDimensions();
6812 }
6813 }
6814
6815 void wxGrid::SetColSize( int col, int width )
6816 {
6817 wxCHECK_RET( col >= 0 && col < m_numCols, _T("invalid column index") );
6818
6819 // should we check that it's bigger than GetColMinimalWidth(col) here?
6820
6821 if ( m_colWidths.IsEmpty() )
6822 {
6823 // need to really create the array
6824 InitColWidths();
6825 }
6826
6827 int w = wxMax( 0, width );
6828 int diff = w - m_colWidths[col];
6829 m_colWidths[col] = w;
6830
6831 int i;
6832 for ( i = col; i < m_numCols; i++ )
6833 {
6834 m_colRights[i] += diff;
6835 }
6836 CalcDimensions();
6837 }
6838
6839
6840 void wxGrid::SetColMinimalWidth( int col, int width )
6841 {
6842 m_colMinWidths.Put(col, (wxObject *)width);
6843 }
6844
6845 int wxGrid::GetColMinimalWidth(int col) const
6846 {
6847 wxObject *obj = m_colMinWidths.Get(m_dragRowOrCol);
6848 return obj ? (int)obj : WXGRID_MIN_COL_WIDTH;
6849 }
6850
6851 void wxGrid::AutoSizeColumn( int col, bool setAsMin )
6852 {
6853 wxClientDC dc(m_gridWin);
6854
6855 wxCoord width, widthMax = 0;
6856 for ( int row = 0; row < m_numRows; row++ )
6857 {
6858 wxGridCellAttr* attr = GetCellAttr(row, col);
6859 wxGridCellRenderer* renderer = attr->GetRenderer(this, row, col);
6860 if ( renderer )
6861 {
6862 width = renderer->GetBestSize(*this, *attr, dc, row, col).x;
6863 if ( width > widthMax )
6864 {
6865 widthMax = width;
6866 }
6867 }
6868
6869 attr->DecRef();
6870 }
6871
6872 // now also compare with the column label width
6873 dc.SetFont( GetLabelFont() );
6874 dc.GetTextExtent( GetColLabelValue(col), &width, NULL );
6875 if ( width > widthMax )
6876 {
6877 widthMax = width;
6878 }
6879
6880 if ( !widthMax )
6881 {
6882 // empty column - give default width (notice that if widthMax is less
6883 // than default width but != 0, it's ok)
6884 widthMax = m_defaultColWidth;
6885 }
6886 else
6887 {
6888 // leave some space around text
6889 widthMax += 10;
6890 }
6891
6892 SetColSize(col, widthMax);
6893 if ( setAsMin )
6894 {
6895 SetColMinimalWidth(col, widthMax);
6896 }
6897 }
6898
6899 void wxGrid::AutoSizeColumns( bool setAsMin )
6900 {
6901 for ( int col = 0; col < m_numCols; col++ )
6902 {
6903 AutoSizeColumn(col, setAsMin);
6904 }
6905 }
6906
6907 //
6908 // ------ cell value accessor functions
6909 //
6910
6911 void wxGrid::SetCellValue( int row, int col, const wxString& s )
6912 {
6913 if ( m_table )
6914 {
6915 m_table->SetValue( row, col, s.c_str() );
6916 if ( !GetBatchCount() )
6917 {
6918 wxClientDC dc( m_gridWin );
6919 PrepareDC( dc );
6920 DrawCell( dc, wxGridCellCoords(row, col) );
6921 }
6922
6923 if ( m_currentCellCoords.GetRow() == row &&
6924 m_currentCellCoords.GetCol() == col &&
6925 IsCellEditControlEnabled())
6926 {
6927 HideCellEditControl();
6928 ShowCellEditControl(); // will reread data from table
6929 }
6930 }
6931 }
6932
6933
6934 //
6935 // ------ Block, row and col selection
6936 //
6937
6938 void wxGrid::SelectRow( int row, bool addToSelected )
6939 {
6940 wxRect r;
6941
6942 if ( IsSelection() && addToSelected )
6943 {
6944 wxRect rect[4];
6945 bool need_refresh[4];
6946 need_refresh[0] =
6947 need_refresh[1] =
6948 need_refresh[2] =
6949 need_refresh[3] = FALSE;
6950
6951 int i;
6952
6953 wxCoord oldLeft = m_selectedTopLeft.GetCol();
6954 wxCoord oldTop = m_selectedTopLeft.GetRow();
6955 wxCoord oldRight = m_selectedBottomRight.GetCol();
6956 wxCoord oldBottom = m_selectedBottomRight.GetRow();
6957
6958 if ( oldTop > row )
6959 {
6960 need_refresh[0] = TRUE;
6961 rect[0] = BlockToDeviceRect( wxGridCellCoords ( row, 0 ),
6962 wxGridCellCoords ( oldTop - 1,
6963 m_numCols - 1 ) );
6964 m_selectedTopLeft.SetRow( row );
6965 }
6966
6967 if ( oldLeft > 0 )
6968 {
6969 need_refresh[1] = TRUE;
6970 rect[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop, 0 ),
6971 wxGridCellCoords ( oldBottom,
6972 oldLeft - 1 ) );
6973
6974 m_selectedTopLeft.SetCol( 0 );
6975 }
6976
6977 if ( oldBottom < row )
6978 {
6979 need_refresh[2] = TRUE;
6980 rect[2] = BlockToDeviceRect( wxGridCellCoords ( oldBottom + 1, 0 ),
6981 wxGridCellCoords ( row,
6982 m_numCols - 1 ) );
6983 m_selectedBottomRight.SetRow( row );
6984 }
6985
6986 if ( oldRight < m_numCols - 1 )
6987 {
6988 need_refresh[3] = TRUE;
6989 rect[3] = BlockToDeviceRect( wxGridCellCoords ( oldTop ,
6990 oldRight + 1 ),
6991 wxGridCellCoords ( oldBottom,
6992 m_numCols - 1 ) );
6993 m_selectedBottomRight.SetCol( m_numCols - 1 );
6994 }
6995
6996 for (i = 0; i < 4; i++ )
6997 if ( need_refresh[i] && rect[i] != wxGridNoCellRect )
6998 m_gridWin->Refresh( FALSE, &(rect[i]) );
6999 }
7000 else
7001 {
7002 r = SelectionToDeviceRect();
7003 ClearSelection();
7004 if ( r != wxGridNoCellRect ) m_gridWin->Refresh( FALSE, &r );
7005
7006 m_selectedTopLeft.Set( row, 0 );
7007 m_selectedBottomRight.Set( row, m_numCols-1 );
7008 r = SelectionToDeviceRect();
7009 m_gridWin->Refresh( FALSE, &r );
7010 }
7011
7012 wxGridRangeSelectEvent gridEvt( GetId(),
7013 wxEVT_GRID_RANGE_SELECT,
7014 this,
7015 m_selectedTopLeft,
7016 m_selectedBottomRight );
7017
7018 GetEventHandler()->ProcessEvent(gridEvt);
7019 }
7020
7021
7022 void wxGrid::SelectCol( int col, bool addToSelected )
7023 {
7024 if ( IsSelection() && addToSelected )
7025 {
7026 wxRect rect[4];
7027 bool need_refresh[4];
7028 need_refresh[0] =
7029 need_refresh[1] =
7030 need_refresh[2] =
7031 need_refresh[3] = FALSE;
7032 int i;
7033
7034 wxCoord oldLeft = m_selectedTopLeft.GetCol();
7035 wxCoord oldTop = m_selectedTopLeft.GetRow();
7036 wxCoord oldRight = m_selectedBottomRight.GetCol();
7037 wxCoord oldBottom = m_selectedBottomRight.GetRow();
7038
7039 if ( oldLeft > col )
7040 {
7041 need_refresh[0] = TRUE;
7042 rect[0] = BlockToDeviceRect( wxGridCellCoords ( 0, col ),
7043 wxGridCellCoords ( m_numRows - 1,
7044 oldLeft - 1 ) );
7045 m_selectedTopLeft.SetCol( col );
7046 }
7047
7048 if ( oldTop > 0 )
7049 {
7050 need_refresh[1] = TRUE;
7051 rect[1] = BlockToDeviceRect( wxGridCellCoords ( 0, oldLeft ),
7052 wxGridCellCoords ( oldTop - 1,
7053 oldRight ) );
7054 m_selectedTopLeft.SetRow( 0 );
7055 }
7056
7057 if ( oldRight < col )
7058 {
7059 need_refresh[2] = TRUE;
7060 rect[2] = BlockToDeviceRect( wxGridCellCoords ( 0, oldRight + 1 ),
7061 wxGridCellCoords ( m_numRows - 1,
7062 col ) );
7063 m_selectedBottomRight.SetCol( col );
7064 }
7065
7066 if ( oldBottom < m_numRows - 1 )
7067 {
7068 need_refresh[3] = TRUE;
7069 rect[3] = BlockToDeviceRect( wxGridCellCoords ( oldBottom + 1,
7070 oldLeft ),
7071 wxGridCellCoords ( m_numRows - 1,
7072 oldRight ) );
7073 m_selectedBottomRight.SetRow( m_numRows - 1 );
7074 }
7075
7076 for (i = 0; i < 4; i++ )
7077 if ( need_refresh[i] && rect[i] != wxGridNoCellRect )
7078 m_gridWin->Refresh( FALSE, &(rect[i]) );
7079 }
7080 else
7081 {
7082 wxRect r;
7083
7084 r = SelectionToDeviceRect();
7085 ClearSelection();
7086 if ( r != wxGridNoCellRect ) m_gridWin->Refresh( FALSE, &r );
7087
7088 m_selectedTopLeft.Set( 0, col );
7089 m_selectedBottomRight.Set( m_numRows-1, col );
7090 r = SelectionToDeviceRect();
7091 m_gridWin->Refresh( FALSE, &r );
7092 }
7093
7094 wxGridRangeSelectEvent gridEvt( GetId(),
7095 wxEVT_GRID_RANGE_SELECT,
7096 this,
7097 m_selectedTopLeft,
7098 m_selectedBottomRight );
7099
7100 GetEventHandler()->ProcessEvent(gridEvt);
7101 }
7102
7103
7104 void wxGrid::SelectBlock( int topRow, int leftCol, int bottomRow, int rightCol )
7105 {
7106 int temp;
7107 wxGridCellCoords updateTopLeft, updateBottomRight;
7108
7109 if ( topRow > bottomRow )
7110 {
7111 temp = topRow;
7112 topRow = bottomRow;
7113 bottomRow = temp;
7114 }
7115
7116 if ( leftCol > rightCol )
7117 {
7118 temp = leftCol;
7119 leftCol = rightCol;
7120 rightCol = temp;
7121 }
7122
7123 updateTopLeft = wxGridCellCoords( topRow, leftCol );
7124 updateBottomRight = wxGridCellCoords( bottomRow, rightCol );
7125
7126 if ( m_selectedTopLeft != updateTopLeft ||
7127 m_selectedBottomRight != updateBottomRight )
7128 {
7129 // Compute two optimal update rectangles:
7130 // Either one rectangle is a real subset of the
7131 // other, or they are (almost) disjoint!
7132 wxRect rect[4];
7133 bool need_refresh[4];
7134 need_refresh[0] =
7135 need_refresh[1] =
7136 need_refresh[2] =
7137 need_refresh[3] = FALSE;
7138 int i;
7139
7140 // Store intermediate values
7141 wxCoord oldLeft = m_selectedTopLeft.GetCol();
7142 wxCoord oldTop = m_selectedTopLeft.GetRow();
7143 wxCoord oldRight = m_selectedBottomRight.GetCol();
7144 wxCoord oldBottom = m_selectedBottomRight.GetRow();
7145
7146 // Determine the outer/inner coordinates.
7147 if (oldLeft > leftCol)
7148 {
7149 temp = oldLeft;
7150 oldLeft = leftCol;
7151 leftCol = temp;
7152 }
7153 if (oldTop > topRow )
7154 {
7155 temp = oldTop;
7156 oldTop = topRow;
7157 topRow = temp;
7158 }
7159 if (oldRight < rightCol )
7160 {
7161 temp = oldRight;
7162 oldRight = rightCol;
7163 rightCol = temp;
7164 }
7165 if (oldBottom < bottomRow)
7166 {
7167 temp = oldBottom;
7168 oldBottom = bottomRow;
7169 bottomRow = temp;
7170 }
7171
7172 // Now, either the stuff marked old is the outer
7173 // rectangle or we don't have a situation where one
7174 // is contained in the other.
7175
7176 if ( oldLeft < leftCol )
7177 {
7178 need_refresh[0] = TRUE;
7179 rect[0] = BlockToDeviceRect( wxGridCellCoords ( oldTop,
7180 oldLeft ),
7181 wxGridCellCoords ( oldBottom,
7182 leftCol - 1 ) );
7183 }
7184
7185 if ( oldTop < topRow )
7186 {
7187 need_refresh[1] = TRUE;
7188 rect[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop,
7189 leftCol ),
7190 wxGridCellCoords ( topRow - 1,
7191 rightCol ) );
7192 }
7193
7194 if ( oldRight > rightCol )
7195 {
7196 need_refresh[2] = TRUE;
7197 rect[2] = BlockToDeviceRect( wxGridCellCoords ( oldTop,
7198 rightCol + 1 ),
7199 wxGridCellCoords ( oldBottom,
7200 oldRight ) );
7201 }
7202
7203 if ( oldBottom > bottomRow )
7204 {
7205 need_refresh[3] = TRUE;
7206 rect[3] = BlockToDeviceRect( wxGridCellCoords ( bottomRow + 1,
7207 leftCol ),
7208 wxGridCellCoords ( oldBottom,
7209 rightCol ) );
7210 }
7211
7212
7213 // Change Selection
7214 m_selectedTopLeft = updateTopLeft;
7215 m_selectedBottomRight = updateBottomRight;
7216
7217 // various Refresh() calls
7218 for (i = 0; i < 4; i++ )
7219 if ( need_refresh[i] && rect[i] != wxGridNoCellRect )
7220 m_gridWin->Refresh( FALSE, &(rect[i]) );
7221 }
7222
7223 // only generate an event if the block is not being selected by
7224 // dragging the mouse (in which case the event will be generated in
7225 // the mouse event handler)
7226 if ( !m_isDragging )
7227 {
7228 wxGridRangeSelectEvent gridEvt( GetId(),
7229 wxEVT_GRID_RANGE_SELECT,
7230 this,
7231 m_selectedTopLeft,
7232 m_selectedBottomRight );
7233
7234 GetEventHandler()->ProcessEvent(gridEvt);
7235 }
7236 }
7237
7238 void wxGrid::SelectAll()
7239 {
7240 m_selectedTopLeft.Set( 0, 0 );
7241 m_selectedBottomRight.Set( m_numRows-1, m_numCols-1 );
7242
7243 m_gridWin->Refresh();
7244 }
7245
7246
7247 void wxGrid::ClearSelection()
7248 {
7249 m_selectedTopLeft = wxGridNoCellCoords;
7250 m_selectedBottomRight = wxGridNoCellCoords;
7251 }
7252
7253
7254 // This function returns the rectangle that encloses the given block
7255 // in device coords clipped to the client size of the grid window.
7256 //
7257 wxRect wxGrid::BlockToDeviceRect( const wxGridCellCoords &topLeft,
7258 const wxGridCellCoords &bottomRight )
7259 {
7260 wxRect rect( wxGridNoCellRect );
7261 wxRect cellRect;
7262
7263 cellRect = CellToRect( topLeft );
7264 if ( cellRect != wxGridNoCellRect )
7265 {
7266 rect = cellRect;
7267 }
7268 else
7269 {
7270 rect = wxRect( 0, 0, 0, 0 );
7271 }
7272
7273 cellRect = CellToRect( bottomRight );
7274 if ( cellRect != wxGridNoCellRect )
7275 {
7276 rect += cellRect;
7277 }
7278 else
7279 {
7280 return wxGridNoCellRect;
7281 }
7282
7283 // convert to scrolled coords
7284 //
7285 int left, top, right, bottom;
7286 CalcScrolledPosition( rect.GetLeft(), rect.GetTop(), &left, &top );
7287 CalcScrolledPosition( rect.GetRight(), rect.GetBottom(), &right, &bottom );
7288
7289 int cw, ch;
7290 m_gridWin->GetClientSize( &cw, &ch );
7291
7292 rect.SetLeft( wxMax(0, left) );
7293 rect.SetTop( wxMax(0, top) );
7294 rect.SetRight( wxMin(cw, right) );
7295 rect.SetBottom( wxMin(ch, bottom) );
7296
7297 return rect;
7298 }
7299
7300
7301
7302 //
7303 // ------ Grid event classes
7304 //
7305
7306 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent, wxEvent )
7307
7308 wxGridEvent::wxGridEvent( int id, wxEventType type, wxObject* obj,
7309 int row, int col, int x, int y,
7310 bool control, bool shift, bool alt, bool meta )
7311 : wxNotifyEvent( type, id )
7312 {
7313 m_row = row;
7314 m_col = col;
7315 m_x = x;
7316 m_y = y;
7317 m_control = control;
7318 m_shift = shift;
7319 m_alt = alt;
7320 m_meta = meta;
7321
7322 SetEventObject(obj);
7323 }
7324
7325
7326 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent, wxEvent )
7327
7328 wxGridSizeEvent::wxGridSizeEvent( int id, wxEventType type, wxObject* obj,
7329 int rowOrCol, int x, int y,
7330 bool control, bool shift, bool alt, bool meta )
7331 : wxNotifyEvent( type, id )
7332 {
7333 m_rowOrCol = rowOrCol;
7334 m_x = x;
7335 m_y = y;
7336 m_control = control;
7337 m_shift = shift;
7338 m_alt = alt;
7339 m_meta = meta;
7340
7341 SetEventObject(obj);
7342 }
7343
7344
7345 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent, wxEvent )
7346
7347 wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id, wxEventType type, wxObject* obj,
7348 const wxGridCellCoords& topLeft,
7349 const wxGridCellCoords& bottomRight,
7350 bool control, bool shift, bool alt, bool meta )
7351 : wxNotifyEvent( type, id )
7352 {
7353 m_topLeft = topLeft;
7354 m_bottomRight = bottomRight;
7355 m_control = control;
7356 m_shift = shift;
7357 m_alt = alt;
7358 m_meta = meta;
7359
7360 SetEventObject(obj);
7361 }
7362
7363
7364 #endif // ifndef wxUSE_NEW_GRID
7365