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