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