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