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