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