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