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