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