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