Unnecessary const_ prefix removed (blindly).
[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 wxString strbuf(tmpbuf, *wxConvCurrent);
1187 bool is_decimal_point =
1188 ( strbuf == 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 void wxGridWindow::OnPaint( wxPaintEvent &WXUNUSED(event) )
3704 {
3705 wxPaintDC dc( this );
3706 m_owner->PrepareDC( dc );
3707 wxRegion reg = GetUpdateRegion();
3708 wxGridCellCoordsArray DirtyCells = m_owner->CalcCellsExposed( reg );
3709 m_owner->DrawGridCellArea( dc , DirtyCells);
3710 #if WXGRID_DRAW_LINES
3711 m_owner->DrawAllGridLines( dc, reg );
3712 #endif
3713 m_owner->DrawGridSpace( dc );
3714 m_owner->DrawHighlight( dc , DirtyCells );
3715 }
3716
3717
3718 void wxGridWindow::ScrollWindow( int dx, int dy, const wxRect *rect )
3719 {
3720 wxWindow::ScrollWindow( dx, dy, rect );
3721 m_rowLabelWin->ScrollWindow( 0, dy, rect );
3722 m_colLabelWin->ScrollWindow( dx, 0, rect );
3723 }
3724
3725
3726 void wxGridWindow::OnMouseEvent( wxMouseEvent& event )
3727 {
3728 m_owner->ProcessGridCellMouseEvent( event );
3729 }
3730
3731 void wxGridWindow::OnMouseWheel( wxMouseEvent& event )
3732 {
3733 m_owner->GetEventHandler()->ProcessEvent(event);
3734 }
3735
3736 // This seems to be required for wxMotif/wxGTK otherwise the mouse
3737 // cursor must be in the cell edit control to get key events
3738 //
3739 void wxGridWindow::OnKeyDown( wxKeyEvent& event )
3740 {
3741 if ( !m_owner->GetEventHandler()->ProcessEvent( event ) ) event.Skip();
3742 }
3743
3744 void wxGridWindow::OnKeyUp( wxKeyEvent& event )
3745 {
3746 if ( !m_owner->GetEventHandler()->ProcessEvent( event ) ) event.Skip();
3747 }
3748
3749 void wxGridWindow::OnEraseBackground( wxEraseEvent& WXUNUSED(event) )
3750 {
3751 }
3752
3753 void wxGridWindow::OnFocus(wxFocusEvent& event)
3754 {
3755 if ( !m_owner->GetEventHandler()->ProcessEvent( event ) )
3756 event.Skip();
3757 }
3758
3759 //////////////////////////////////////////////////////////////////////
3760
3761 // Internal Helper function for computing row or column from some
3762 // (unscrolled) coordinate value, using either
3763 // m_defaultRowHeight/m_defaultColWidth or binary search on array
3764 // of m_rowBottoms/m_ColRights to speed up the search!
3765
3766 // Internal helper macros for simpler use of that function
3767
3768 static int CoordToRowOrCol(int coord, int defaultDist, int minDist,
3769 const wxArrayInt& BorderArray, int nMax,
3770 bool clipToMinMax);
3771
3772 #define internalXToCol(x) CoordToRowOrCol(x, m_defaultColWidth, \
3773 m_minAcceptableColWidth, \
3774 m_colRights, m_numCols, true)
3775 #define internalYToRow(y) CoordToRowOrCol(y, m_defaultRowHeight, \
3776 m_minAcceptableRowHeight, \
3777 m_rowBottoms, m_numRows, true)
3778 /////////////////////////////////////////////////////////////////////
3779
3780 #if wxUSE_EXTENDED_RTTI
3781 WX_DEFINE_FLAGS( wxGridStyle )
3782
3783 wxBEGIN_FLAGS( wxGridStyle )
3784 // new style border flags, we put them first to
3785 // use them for streaming out
3786 wxFLAGS_MEMBER(wxBORDER_SIMPLE)
3787 wxFLAGS_MEMBER(wxBORDER_SUNKEN)
3788 wxFLAGS_MEMBER(wxBORDER_DOUBLE)
3789 wxFLAGS_MEMBER(wxBORDER_RAISED)
3790 wxFLAGS_MEMBER(wxBORDER_STATIC)
3791 wxFLAGS_MEMBER(wxBORDER_NONE)
3792
3793 // old style border flags
3794 wxFLAGS_MEMBER(wxSIMPLE_BORDER)
3795 wxFLAGS_MEMBER(wxSUNKEN_BORDER)
3796 wxFLAGS_MEMBER(wxDOUBLE_BORDER)
3797 wxFLAGS_MEMBER(wxRAISED_BORDER)
3798 wxFLAGS_MEMBER(wxSTATIC_BORDER)
3799 wxFLAGS_MEMBER(wxBORDER)
3800
3801 // standard window styles
3802 wxFLAGS_MEMBER(wxTAB_TRAVERSAL)
3803 wxFLAGS_MEMBER(wxCLIP_CHILDREN)
3804 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW)
3805 wxFLAGS_MEMBER(wxWANTS_CHARS)
3806 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE)
3807 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB )
3808 wxFLAGS_MEMBER(wxVSCROLL)
3809 wxFLAGS_MEMBER(wxHSCROLL)
3810
3811 wxEND_FLAGS( wxGridStyle )
3812
3813 IMPLEMENT_DYNAMIC_CLASS_XTI(wxGrid, wxScrolledWindow,"wx/grid.h")
3814
3815 wxBEGIN_PROPERTIES_TABLE(wxGrid)
3816 wxHIDE_PROPERTY( Children )
3817 wxPROPERTY_FLAGS( WindowStyle , wxGridStyle , long , SetWindowStyleFlag , GetWindowStyleFlag , EMPTY_MACROVALUE, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
3818 wxEND_PROPERTIES_TABLE()
3819
3820 wxBEGIN_HANDLERS_TABLE(wxGrid)
3821 wxEND_HANDLERS_TABLE()
3822
3823 wxCONSTRUCTOR_5( wxGrid , wxWindow* , Parent , wxWindowID , Id , wxPoint , Position , wxSize , Size , long , WindowStyle )
3824
3825 /*
3826 TODO : Expose more information of a list's layout etc. via appropriate objects (\81à la NotebookPageInfo)
3827 */
3828 #else
3829 IMPLEMENT_DYNAMIC_CLASS( wxGrid, wxScrolledWindow )
3830 #endif
3831
3832 BEGIN_EVENT_TABLE( wxGrid, wxScrolledWindow )
3833 EVT_PAINT( wxGrid::OnPaint )
3834 EVT_SIZE( wxGrid::OnSize )
3835 EVT_KEY_DOWN( wxGrid::OnKeyDown )
3836 EVT_KEY_UP( wxGrid::OnKeyUp )
3837 EVT_ERASE_BACKGROUND( wxGrid::OnEraseBackground )
3838 END_EVENT_TABLE()
3839
3840 wxGrid::wxGrid()
3841 {
3842 // in order to make sure that a size event is not
3843 // trigerred in a unfinished state
3844 m_cornerLabelWin = NULL ;
3845 m_rowLabelWin = NULL ;
3846 m_colLabelWin = NULL ;
3847 m_gridWin = NULL ;
3848 }
3849
3850 wxGrid::wxGrid( wxWindow *parent,
3851 wxWindowID id,
3852 const wxPoint& pos,
3853 const wxSize& size,
3854 long style,
3855 const wxString& name )
3856 : wxScrolledWindow( parent, id, pos, size, (style | wxWANTS_CHARS), name ),
3857 m_colMinWidths(GRID_HASH_SIZE),
3858 m_rowMinHeights(GRID_HASH_SIZE)
3859 {
3860 Create();
3861 SetBestFittingSize(size);
3862 }
3863
3864 bool wxGrid::Create(wxWindow *parent, wxWindowID id,
3865 const wxPoint& pos, const wxSize& size,
3866 long style, const wxString& name)
3867 {
3868 if (!wxScrolledWindow::Create(parent, id, pos, size,
3869 style | wxWANTS_CHARS , name))
3870 return false;
3871
3872 m_colMinWidths = wxLongToLongHashMap(GRID_HASH_SIZE) ;
3873 m_rowMinHeights = wxLongToLongHashMap(GRID_HASH_SIZE) ;
3874
3875 Create() ;
3876 SetBestFittingSize(size);
3877
3878 return true;
3879 }
3880
3881
3882 wxGrid::~wxGrid()
3883 {
3884 // Must do this or ~wxScrollHelper will pop the wrong event handler
3885 SetTargetWindow(this);
3886 ClearAttrCache();
3887 wxSafeDecRef(m_defaultCellAttr);
3888
3889 #ifdef DEBUG_ATTR_CACHE
3890 size_t total = gs_nAttrCacheHits + gs_nAttrCacheMisses;
3891 wxPrintf(_T("wxGrid attribute cache statistics: "
3892 "total: %u, hits: %u (%u%%)\n"),
3893 total, gs_nAttrCacheHits,
3894 total ? (gs_nAttrCacheHits*100) / total : 0);
3895 #endif
3896
3897 if (m_ownTable)
3898 delete m_table;
3899
3900 delete m_typeRegistry;
3901 delete m_selection;
3902 }
3903
3904
3905 //
3906 // ----- internal init and update functions
3907 //
3908
3909 // NOTE: If using the default visual attributes works everywhere then this can
3910 // be removed as well as the #else cases below.
3911 #define _USE_VISATTR 0
3912
3913 #if _USE_VISATTR
3914 #include "wx/listbox.h"
3915 #endif
3916
3917 void wxGrid::Create()
3918 {
3919 m_created = false; // set to true by CreateGrid
3920
3921 m_table = (wxGridTableBase *) NULL;
3922 m_ownTable = false;
3923
3924 m_cellEditCtrlEnabled = false;
3925
3926 m_defaultCellAttr = new wxGridCellAttr();
3927
3928 // Set default cell attributes
3929 m_defaultCellAttr->SetDefAttr(m_defaultCellAttr);
3930 m_defaultCellAttr->SetKind(wxGridCellAttr::Default);
3931 m_defaultCellAttr->SetFont(GetFont());
3932 m_defaultCellAttr->SetAlignment(wxALIGN_LEFT, wxALIGN_TOP);
3933 m_defaultCellAttr->SetRenderer(new wxGridCellStringRenderer);
3934 m_defaultCellAttr->SetEditor(new wxGridCellTextEditor);
3935
3936 #if _USE_VISATTR
3937 wxVisualAttributes gva = wxListBox::GetClassDefaultAttributes();
3938 wxVisualAttributes lva = wxPanel::GetClassDefaultAttributes();
3939
3940 m_defaultCellAttr->SetTextColour(gva.colFg);
3941 m_defaultCellAttr->SetBackgroundColour(gva.colBg);
3942
3943 #else
3944 m_defaultCellAttr->SetTextColour(
3945 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT));
3946 m_defaultCellAttr->SetBackgroundColour(
3947 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW));
3948 #endif
3949
3950 m_numRows = 0;
3951 m_numCols = 0;
3952 m_currentCellCoords = wxGridNoCellCoords;
3953
3954 m_rowLabelWidth = WXGRID_DEFAULT_ROW_LABEL_WIDTH;
3955 m_colLabelHeight = WXGRID_DEFAULT_COL_LABEL_HEIGHT;
3956
3957 // create the type registry
3958 m_typeRegistry = new wxGridTypeRegistry;
3959 m_selection = NULL;
3960
3961 // subwindow components that make up the wxGrid
3962 m_cornerLabelWin = new wxGridCornerLabelWindow( this,
3963 wxID_ANY,
3964 wxDefaultPosition,
3965 wxDefaultSize );
3966
3967 m_rowLabelWin = new wxGridRowLabelWindow( this,
3968 wxID_ANY,
3969 wxDefaultPosition,
3970 wxDefaultSize );
3971
3972 m_colLabelWin = new wxGridColLabelWindow( this,
3973 wxID_ANY,
3974 wxDefaultPosition,
3975 wxDefaultSize );
3976
3977 m_gridWin = new wxGridWindow( this,
3978 m_rowLabelWin,
3979 m_colLabelWin,
3980 wxID_ANY,
3981 wxDefaultPosition,
3982 wxDefaultSize );
3983
3984 SetTargetWindow( m_gridWin );
3985
3986 #if _USE_VISATTR
3987 wxColour gfg = gva.colFg;
3988 wxColour gbg = gva.colBg;
3989 wxColour lfg = lva.colFg;
3990 wxColour lbg = lva.colBg;
3991 #else
3992 wxColour gfg = wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT );
3993 wxColour gbg = wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOW );
3994 wxColour lfg = wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT );
3995 wxColour lbg = wxSystemSettings::GetColour( wxSYS_COLOUR_BTNFACE );
3996 #endif
3997 m_cornerLabelWin->SetOwnForegroundColour(lfg);
3998 m_cornerLabelWin->SetOwnBackgroundColour(lbg);
3999 m_rowLabelWin->SetOwnForegroundColour(lfg);
4000 m_rowLabelWin->SetOwnBackgroundColour(lbg);
4001 m_colLabelWin->SetOwnForegroundColour(lfg);
4002 m_colLabelWin->SetOwnBackgroundColour(lbg);
4003
4004 m_gridWin->SetOwnForegroundColour(gfg);
4005 m_gridWin->SetOwnBackgroundColour(gbg);
4006
4007 Init();
4008 }
4009
4010
4011 bool wxGrid::CreateGrid( int numRows, int numCols,
4012 wxGrid::wxGridSelectionModes selmode )
4013 {
4014 wxCHECK_MSG( !m_created,
4015 false,
4016 wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
4017
4018 m_numRows = numRows;
4019 m_numCols = numCols;
4020
4021 m_table = new wxGridStringTable( m_numRows, m_numCols );
4022 m_table->SetView( this );
4023 m_ownTable = true;
4024 m_selection = new wxGridSelection( this, selmode );
4025
4026 CalcDimensions();
4027
4028 m_created = true;
4029
4030 return m_created;
4031 }
4032
4033 void wxGrid::SetSelectionMode(wxGrid::wxGridSelectionModes selmode)
4034 {
4035 wxCHECK_RET( m_created,
4036 wxT("Called wxGrid::SetSelectionMode() before calling CreateGrid()") );
4037
4038 m_selection->SetSelectionMode( selmode );
4039 }
4040
4041 wxGrid::wxGridSelectionModes wxGrid::GetSelectionMode() const
4042 {
4043 wxCHECK_MSG( m_created, wxGrid::wxGridSelectCells,
4044 wxT("Called wxGrid::GetSelectionMode() before calling CreateGrid()") );
4045
4046 return m_selection->GetSelectionMode();
4047 }
4048
4049 bool wxGrid::SetTable( wxGridTableBase *table, bool takeOwnership,
4050 wxGrid::wxGridSelectionModes selmode )
4051 {
4052 if ( m_created )
4053 {
4054 // stop all processing
4055 m_created = false;
4056
4057 if (m_ownTable)
4058 {
4059 wxGridTableBase *t=m_table;
4060 m_table=0;
4061 delete t;
4062 }
4063 delete m_selection;
4064
4065 m_table=0;
4066 m_selection=0;
4067 m_numRows=0;
4068 m_numCols=0;
4069 }
4070 if (table)
4071 {
4072 m_numRows = table->GetNumberRows();
4073 m_numCols = table->GetNumberCols();
4074
4075 m_table = table;
4076 m_table->SetView( this );
4077 if (takeOwnership)
4078 m_ownTable = true;
4079 m_selection = new wxGridSelection( this, selmode );
4080
4081 CalcDimensions();
4082
4083 m_created = true;
4084 }
4085
4086 return m_created;
4087 }
4088
4089
4090 void wxGrid::Init()
4091 {
4092 m_rowLabelWidth = WXGRID_DEFAULT_ROW_LABEL_WIDTH;
4093 m_colLabelHeight = WXGRID_DEFAULT_COL_LABEL_HEIGHT;
4094
4095 if ( m_rowLabelWin )
4096 {
4097 m_labelBackgroundColour = m_rowLabelWin->GetBackgroundColour();
4098 }
4099 else
4100 {
4101 m_labelBackgroundColour = wxColour( _T("WHITE") );
4102 }
4103
4104 m_labelTextColour = wxColour( _T("BLACK") );
4105
4106 // init attr cache
4107 m_attrCache.row = -1;
4108 m_attrCache.col = -1;
4109 m_attrCache.attr = NULL;
4110
4111 // TODO: something better than this ?
4112 //
4113 m_labelFont = this->GetFont();
4114 m_labelFont.SetWeight( wxBOLD );
4115
4116 m_rowLabelHorizAlign = wxALIGN_CENTRE;
4117 m_rowLabelVertAlign = wxALIGN_CENTRE;
4118
4119 m_colLabelHorizAlign = wxALIGN_CENTRE;
4120 m_colLabelVertAlign = wxALIGN_CENTRE;
4121 m_colLabelTextOrientation = wxHORIZONTAL;
4122
4123 m_defaultColWidth = WXGRID_DEFAULT_COL_WIDTH;
4124 m_defaultRowHeight = m_gridWin->GetCharHeight();
4125
4126 m_minAcceptableColWidth = WXGRID_MIN_COL_WIDTH;
4127 m_minAcceptableRowHeight = WXGRID_MIN_ROW_HEIGHT;
4128
4129 #if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl()
4130 m_defaultRowHeight += 8;
4131 #else
4132 m_defaultRowHeight += 4;
4133 #endif
4134
4135 m_gridLineColour = wxColour( 192,192,192 );
4136 m_gridLinesEnabled = true;
4137 m_cellHighlightColour = *wxBLACK;
4138 m_cellHighlightPenWidth = 2;
4139 m_cellHighlightROPenWidth = 1;
4140
4141 m_cursorMode = WXGRID_CURSOR_SELECT_CELL;
4142 m_winCapture = (wxWindow *)NULL;
4143 m_canDragRowSize = true;
4144 m_canDragColSize = true;
4145 m_canDragGridSize = true;
4146 m_canDragCell = false;
4147 m_dragLastPos = -1;
4148 m_dragRowOrCol = -1;
4149 m_isDragging = false;
4150 m_startDragPos = wxDefaultPosition;
4151
4152 m_waitForSlowClick = false;
4153
4154 m_rowResizeCursor = wxCursor( wxCURSOR_SIZENS );
4155 m_colResizeCursor = wxCursor( wxCURSOR_SIZEWE );
4156
4157 m_currentCellCoords = wxGridNoCellCoords;
4158
4159 m_selectingTopLeft = wxGridNoCellCoords;
4160 m_selectingBottomRight = wxGridNoCellCoords;
4161 m_selectionBackground = wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT);
4162 m_selectionForeground = wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT);
4163
4164 m_editable = true; // default for whole grid
4165
4166 m_inOnKeyDown = false;
4167 m_batchCount = 0;
4168
4169 m_extraWidth =
4170 m_extraHeight = 0;
4171
4172 m_scrollLineX = GRID_SCROLL_LINE_X;
4173 m_scrollLineY = GRID_SCROLL_LINE_Y;
4174 }
4175
4176 // ----------------------------------------------------------------------------
4177 // the idea is to call these functions only when necessary because they create
4178 // quite big arrays which eat memory mostly unnecessary - in particular, if
4179 // default widths/heights are used for all rows/columns, we may not use these
4180 // arrays at all
4181 //
4182 // with some extra code, it should be possible to only store the
4183 // widths/heights different from default ones but this will be done later...
4184 // ----------------------------------------------------------------------------
4185
4186 void wxGrid::InitRowHeights()
4187 {
4188 m_rowHeights.Empty();
4189 m_rowBottoms.Empty();
4190
4191 m_rowHeights.Alloc( m_numRows );
4192 m_rowBottoms.Alloc( m_numRows );
4193
4194 int rowBottom = 0;
4195
4196 m_rowHeights.Add( m_defaultRowHeight, m_numRows );
4197
4198 for ( int i = 0; i < m_numRows; i++ )
4199 {
4200 rowBottom += m_defaultRowHeight;
4201 m_rowBottoms.Add( rowBottom );
4202 }
4203 }
4204
4205 void wxGrid::InitColWidths()
4206 {
4207 m_colWidths.Empty();
4208 m_colRights.Empty();
4209
4210 m_colWidths.Alloc( m_numCols );
4211 m_colRights.Alloc( m_numCols );
4212 int colRight = 0;
4213
4214 m_colWidths.Add( m_defaultColWidth, m_numCols );
4215
4216 for ( int i = 0; i < m_numCols; i++ )
4217 {
4218 colRight += m_defaultColWidth;
4219 m_colRights.Add( colRight );
4220 }
4221 }
4222
4223 int wxGrid::GetColWidth(int col) const
4224 {
4225 return m_colWidths.IsEmpty() ? m_defaultColWidth : m_colWidths[col];
4226 }
4227
4228 int wxGrid::GetColLeft(int col) const
4229 {
4230 return m_colRights.IsEmpty() ? col * m_defaultColWidth
4231 : m_colRights[col] - m_colWidths[col];
4232 }
4233
4234 int wxGrid::GetColRight(int col) const
4235 {
4236 return m_colRights.IsEmpty() ? (col + 1) * m_defaultColWidth
4237 : m_colRights[col];
4238 }
4239
4240 int wxGrid::GetRowHeight(int row) const
4241 {
4242 return m_rowHeights.IsEmpty() ? m_defaultRowHeight : m_rowHeights[row];
4243 }
4244
4245 int wxGrid::GetRowTop(int row) const
4246 {
4247 return m_rowBottoms.IsEmpty() ? row * m_defaultRowHeight
4248 : m_rowBottoms[row] - m_rowHeights[row];
4249 }
4250
4251 int wxGrid::GetRowBottom(int row) const
4252 {
4253 return m_rowBottoms.IsEmpty() ? (row + 1) * m_defaultRowHeight
4254 : m_rowBottoms[row];
4255 }
4256
4257 void wxGrid::CalcDimensions()
4258 {
4259 int cw, ch;
4260 GetClientSize( &cw, &ch );
4261
4262 if ( m_rowLabelWin->IsShown() )
4263 cw -= m_rowLabelWidth;
4264 if ( m_colLabelWin->IsShown() )
4265 ch -= m_colLabelHeight;
4266
4267 // grid total size
4268 int w = m_numCols > 0 ? GetColRight(m_numCols - 1) + m_extraWidth + 1 : 0;
4269 int h = m_numRows > 0 ? GetRowBottom(m_numRows - 1) + m_extraHeight + 1 : 0;
4270
4271 // take into account editor if shown
4272 if( IsCellEditControlShown() )
4273 {
4274 int w2, h2;
4275 int r = m_currentCellCoords.GetRow();
4276 int c = m_currentCellCoords.GetCol();
4277 int x = GetColLeft(c);
4278 int y = GetRowTop(r);
4279
4280 // how big is the editor
4281 wxGridCellAttr* attr = GetCellAttr(r, c);
4282 wxGridCellEditor* editor = attr->GetEditor(this, r, c);
4283 editor->GetControl()->GetSize(&w2, &h2);
4284 w2 += x;
4285 h2 += y;
4286 if( w2 > w ) w = w2;
4287 if( h2 > h ) h = h2;
4288 editor->DecRef();
4289 attr->DecRef();
4290 }
4291
4292 // preserve (more or less) the previous position
4293 int x, y;
4294 GetViewStart( &x, &y );
4295
4296 // ensure the position is valid for the new scroll ranges
4297 if ( x >= w )
4298 x = wxMax( w - 1, 0 );
4299 if ( y >= h )
4300 y = wxMax( h - 1, 0 );
4301
4302 // do set scrollbar parameters
4303 SetScrollbars( GRID_SCROLL_LINE_X, GRID_SCROLL_LINE_Y,
4304 GetScrollX(w), GetScrollY(h), x, y,
4305 GetBatchCount() != 0);
4306
4307 // if our OnSize() hadn't been called (it would if we have scrollbars), we
4308 // still must reposition the children
4309 CalcWindowSizes();
4310 }
4311
4312
4313 void wxGrid::CalcWindowSizes()
4314 {
4315 // escape if the window is has not been fully created yet
4316
4317 if ( m_cornerLabelWin == NULL )
4318 return ;
4319
4320 int cw, ch;
4321 GetClientSize( &cw, &ch );
4322
4323 if ( m_cornerLabelWin && m_cornerLabelWin->IsShown() )
4324 m_cornerLabelWin->SetSize( 0, 0, m_rowLabelWidth, m_colLabelHeight );
4325
4326 if ( m_colLabelWin && m_colLabelWin->IsShown() )
4327 m_colLabelWin->SetSize( m_rowLabelWidth, 0, cw-m_rowLabelWidth, m_colLabelHeight);
4328
4329 if ( m_rowLabelWin && m_rowLabelWin->IsShown() )
4330 m_rowLabelWin->SetSize( 0, m_colLabelHeight, m_rowLabelWidth, ch-m_colLabelHeight);
4331
4332 if ( m_gridWin && m_gridWin->IsShown() )
4333 m_gridWin->SetSize( m_rowLabelWidth, m_colLabelHeight, cw-m_rowLabelWidth, ch-m_colLabelHeight);
4334 }
4335
4336
4337 // this is called when the grid table sends a message to say that it
4338 // has been redimensioned
4339 //
4340 bool wxGrid::Redimension( wxGridTableMessage& msg )
4341 {
4342 int i;
4343 bool result = false;
4344
4345 // Clear the attribute cache as the attribute might refer to a different
4346 // cell than stored in the cache after adding/removing rows/columns.
4347 ClearAttrCache();
4348 // By the same reasoning, the editor should be dismissed if columns are
4349 // added or removed. And for consistency, it should IMHO always be
4350 // removed, not only if the cell "underneath" it actually changes.
4351 // For now, I intentionally do not save the editor's content as the
4352 // cell it might want to save that stuff to might no longer exist.
4353 HideCellEditControl();
4354 #if 0
4355 // if we were using the default widths/heights so far, we must change them
4356 // now
4357 if ( m_colWidths.IsEmpty() )
4358 {
4359 InitColWidths();
4360 }
4361
4362 if ( m_rowHeights.IsEmpty() )
4363 {
4364 InitRowHeights();
4365 }
4366 #endif
4367
4368 switch ( msg.GetId() )
4369 {
4370 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED:
4371 {
4372 size_t pos = msg.GetCommandInt();
4373 int numRows = msg.GetCommandInt2();
4374
4375 m_numRows += numRows;
4376
4377 if ( !m_rowHeights.IsEmpty() )
4378 {
4379 m_rowHeights.Insert( m_defaultRowHeight, pos, numRows );
4380 m_rowBottoms.Insert( 0, pos, numRows );
4381
4382 int bottom = 0;
4383 if ( pos > 0 ) bottom = m_rowBottoms[pos-1];
4384
4385 for ( i = pos; i < m_numRows; i++ )
4386 {
4387 bottom += m_rowHeights[i];
4388 m_rowBottoms[i] = bottom;
4389 }
4390 }
4391 if ( m_currentCellCoords == wxGridNoCellCoords )
4392 {
4393 // if we have just inserted cols into an empty grid the current
4394 // cell will be undefined...
4395 //
4396 SetCurrentCell( 0, 0 );
4397 }
4398
4399 if ( m_selection )
4400 m_selection->UpdateRows( pos, numRows );
4401 wxGridCellAttrProvider * attrProvider = m_table->GetAttrProvider();
4402 if (attrProvider)
4403 attrProvider->UpdateAttrRows( pos, numRows );
4404
4405 if ( !GetBatchCount() )
4406 {
4407 CalcDimensions();
4408 m_rowLabelWin->Refresh();
4409 }
4410 }
4411 result = true;
4412 break;
4413
4414 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED:
4415 {
4416 int numRows = msg.GetCommandInt();
4417 int oldNumRows = m_numRows;
4418 m_numRows += numRows;
4419
4420 if ( !m_rowHeights.IsEmpty() )
4421 {
4422 m_rowHeights.Add( m_defaultRowHeight, numRows );
4423 m_rowBottoms.Add( 0, numRows );
4424
4425 int bottom = 0;
4426 if ( oldNumRows > 0 ) bottom = m_rowBottoms[oldNumRows-1];
4427
4428 for ( i = oldNumRows; i < m_numRows; i++ )
4429 {
4430 bottom += m_rowHeights[i];
4431 m_rowBottoms[i] = bottom;
4432 }
4433 }
4434 if ( m_currentCellCoords == wxGridNoCellCoords )
4435 {
4436 // if we have just inserted cols into an empty grid the current
4437 // cell will be undefined...
4438 //
4439 SetCurrentCell( 0, 0 );
4440 }
4441 if ( !GetBatchCount() )
4442 {
4443 CalcDimensions();
4444 m_rowLabelWin->Refresh();
4445 }
4446 }
4447 result = true;
4448 break;
4449
4450 case wxGRIDTABLE_NOTIFY_ROWS_DELETED:
4451 {
4452 size_t pos = msg.GetCommandInt();
4453 int numRows = msg.GetCommandInt2();
4454 m_numRows -= numRows;
4455
4456 if ( !m_rowHeights.IsEmpty() )
4457 {
4458 m_rowHeights.RemoveAt( pos, numRows );
4459 m_rowBottoms.RemoveAt( pos, numRows );
4460
4461 int h = 0;
4462 for ( i = 0; i < m_numRows; i++ )
4463 {
4464 h += m_rowHeights[i];
4465 m_rowBottoms[i] = h;
4466 }
4467 }
4468 if ( !m_numRows )
4469 {
4470 m_currentCellCoords = wxGridNoCellCoords;
4471 }
4472 else
4473 {
4474 if ( m_currentCellCoords.GetRow() >= m_numRows )
4475 m_currentCellCoords.Set( 0, 0 );
4476 }
4477
4478 if ( m_selection )
4479 m_selection->UpdateRows( pos, -((int)numRows) );
4480 wxGridCellAttrProvider * attrProvider = m_table->GetAttrProvider();
4481 if (attrProvider) {
4482 attrProvider->UpdateAttrRows( pos, -((int)numRows) );
4483 // ifdef'd out following patch from Paul Gammans
4484 #if 0
4485 // No need to touch column attributes, unless we
4486 // removed _all_ rows, in this case, we remove
4487 // all column attributes.
4488 // I hate to do this here, but the
4489 // needed data is not available inside UpdateAttrRows.
4490 if ( !GetNumberRows() )
4491 attrProvider->UpdateAttrCols( 0, -GetNumberCols() );
4492 #endif
4493 }
4494 if ( !GetBatchCount() )
4495 {
4496 CalcDimensions();
4497 m_rowLabelWin->Refresh();
4498 }
4499 }
4500 result = true;
4501 break;
4502
4503 case wxGRIDTABLE_NOTIFY_COLS_INSERTED:
4504 {
4505 size_t pos = msg.GetCommandInt();
4506 int numCols = msg.GetCommandInt2();
4507 m_numCols += numCols;
4508
4509 if ( !m_colWidths.IsEmpty() )
4510 {
4511 m_colWidths.Insert( m_defaultColWidth, pos, numCols );
4512 m_colRights.Insert( 0, pos, numCols );
4513
4514 int right = 0;
4515 if ( pos > 0 ) right = m_colRights[pos-1];
4516
4517 for ( i = pos; i < m_numCols; i++ )
4518 {
4519 right += m_colWidths[i];
4520 m_colRights[i] = right;
4521 }
4522 }
4523 if ( m_currentCellCoords == wxGridNoCellCoords )
4524 {
4525 // if we have just inserted cols into an empty grid the current
4526 // cell will be undefined...
4527 //
4528 SetCurrentCell( 0, 0 );
4529 }
4530
4531 if ( m_selection )
4532 m_selection->UpdateCols( pos, numCols );
4533 wxGridCellAttrProvider * attrProvider = m_table->GetAttrProvider();
4534 if (attrProvider)
4535 attrProvider->UpdateAttrCols( pos, numCols );
4536 if ( !GetBatchCount() )
4537 {
4538 CalcDimensions();
4539 m_colLabelWin->Refresh();
4540 }
4541
4542 }
4543 result = true;
4544 break;
4545
4546 case wxGRIDTABLE_NOTIFY_COLS_APPENDED:
4547 {
4548 int numCols = msg.GetCommandInt();
4549 int oldNumCols = m_numCols;
4550 m_numCols += numCols;
4551 if ( !m_colWidths.IsEmpty() )
4552 {
4553 m_colWidths.Add( m_defaultColWidth, numCols );
4554 m_colRights.Add( 0, numCols );
4555
4556 int right = 0;
4557 if ( oldNumCols > 0 ) right = m_colRights[oldNumCols-1];
4558
4559 for ( i = oldNumCols; i < m_numCols; i++ )
4560 {
4561 right += m_colWidths[i];
4562 m_colRights[i] = right;
4563 }
4564 }
4565 if ( m_currentCellCoords == wxGridNoCellCoords )
4566 {
4567 // if we have just inserted cols into an empty grid the current
4568 // cell will be undefined...
4569 //
4570 SetCurrentCell( 0, 0 );
4571 }
4572 if ( !GetBatchCount() )
4573 {
4574 CalcDimensions();
4575 m_colLabelWin->Refresh();
4576 }
4577 }
4578 result = true;
4579 break;
4580
4581 case wxGRIDTABLE_NOTIFY_COLS_DELETED:
4582 {
4583 size_t pos = msg.GetCommandInt();
4584 int numCols = msg.GetCommandInt2();
4585 m_numCols -= numCols;
4586
4587 if ( !m_colWidths.IsEmpty() )
4588 {
4589 m_colWidths.RemoveAt( pos, numCols );
4590 m_colRights.RemoveAt( pos, numCols );
4591
4592 int w = 0;
4593 for ( i = 0; i < m_numCols; i++ )
4594 {
4595 w += m_colWidths[i];
4596 m_colRights[i] = w;
4597 }
4598 }
4599 if ( !m_numCols )
4600 {
4601 m_currentCellCoords = wxGridNoCellCoords;
4602 }
4603 else
4604 {
4605 if ( m_currentCellCoords.GetCol() >= m_numCols )
4606 m_currentCellCoords.Set( 0, 0 );
4607 }
4608
4609 if ( m_selection )
4610 m_selection->UpdateCols( pos, -((int)numCols) );
4611 wxGridCellAttrProvider * attrProvider = m_table->GetAttrProvider();
4612 if (attrProvider) {
4613 attrProvider->UpdateAttrCols( pos, -((int)numCols) );
4614 // ifdef'd out following patch from Paul Gammans
4615 #if 0
4616 // No need to touch row attributes, unless we
4617 // removed _all_ columns, in this case, we remove
4618 // all row attributes.
4619 // I hate to do this here, but the
4620 // needed data is not available inside UpdateAttrCols.
4621 if ( !GetNumberCols() )
4622 attrProvider->UpdateAttrRows( 0, -GetNumberRows() );
4623 #endif
4624 }
4625 if ( !GetBatchCount() )
4626 {
4627 CalcDimensions();
4628 m_colLabelWin->Refresh();
4629 }
4630 }
4631 result = true;
4632 break;
4633 }
4634
4635 if (result && !GetBatchCount() )
4636 m_gridWin->Refresh();
4637 return result;
4638 }
4639
4640
4641 wxArrayInt wxGrid::CalcRowLabelsExposed( const wxRegion& reg )
4642 {
4643 wxRegionIterator iter( reg );
4644 wxRect r;
4645
4646 wxArrayInt rowlabels;
4647
4648 int top, bottom;
4649 while ( iter )
4650 {
4651 r = iter.GetRect();
4652
4653 // TODO: remove this when we can...
4654 // There is a bug in wxMotif that gives garbage update
4655 // rectangles if you jump-scroll a long way by clicking the
4656 // scrollbar with middle button. This is a work-around
4657 //
4658 #if defined(__WXMOTIF__)
4659 int cw, ch;
4660 m_gridWin->GetClientSize( &cw, &ch );
4661 if ( r.GetTop() > ch ) r.SetTop( 0 );
4662 r.SetBottom( wxMin( r.GetBottom(), ch ) );
4663 #endif
4664
4665 // logical bounds of update region
4666 //
4667 int dummy;
4668 CalcUnscrolledPosition( 0, r.GetTop(), &dummy, &top );
4669 CalcUnscrolledPosition( 0, r.GetBottom(), &dummy, &bottom );
4670
4671 // find the row labels within these bounds
4672 //
4673 int row;
4674 for ( row = internalYToRow(top); row < m_numRows; row++ )
4675 {
4676 if ( GetRowBottom(row) < top )
4677 continue;
4678
4679 if ( GetRowTop(row) > bottom )
4680 break;
4681
4682 rowlabels.Add( row );
4683 }
4684
4685 iter++ ;
4686 }
4687
4688 return rowlabels;
4689 }
4690
4691
4692 wxArrayInt wxGrid::CalcColLabelsExposed( const wxRegion& reg )
4693 {
4694 wxRegionIterator iter( reg );
4695 wxRect r;
4696
4697 wxArrayInt colLabels;
4698
4699 int left, right;
4700 while ( iter )
4701 {
4702 r = iter.GetRect();
4703
4704 // TODO: remove this when we can...
4705 // There is a bug in wxMotif that gives garbage update
4706 // rectangles if you jump-scroll a long way by clicking the
4707 // scrollbar with middle button. This is a work-around
4708 //
4709 #if defined(__WXMOTIF__)
4710 int cw, ch;
4711 m_gridWin->GetClientSize( &cw, &ch );
4712 if ( r.GetLeft() > cw ) r.SetLeft( 0 );
4713 r.SetRight( wxMin( r.GetRight(), cw ) );
4714 #endif
4715
4716 // logical bounds of update region
4717 //
4718 int dummy;
4719 CalcUnscrolledPosition( r.GetLeft(), 0, &left, &dummy );
4720 CalcUnscrolledPosition( r.GetRight(), 0, &right, &dummy );
4721
4722 // find the cells within these bounds
4723 //
4724 int col;
4725 for ( col = internalXToCol(left); col < m_numCols; col++ )
4726 {
4727 if ( GetColRight(col) < left )
4728 continue;
4729
4730 if ( GetColLeft(col) > right )
4731 break;
4732
4733 colLabels.Add( col );
4734 }
4735
4736 iter++ ;
4737 }
4738 return colLabels;
4739 }
4740
4741
4742 wxGridCellCoordsArray wxGrid::CalcCellsExposed( const wxRegion& reg )
4743 {
4744 wxRegionIterator iter( reg );
4745 wxRect r;
4746
4747 wxGridCellCoordsArray cellsExposed;
4748
4749 int left, top, right, bottom;
4750 while ( iter )
4751 {
4752 r = iter.GetRect();
4753
4754 // TODO: remove this when we can...
4755 // There is a bug in wxMotif that gives garbage update
4756 // rectangles if you jump-scroll a long way by clicking the
4757 // scrollbar with middle button. This is a work-around
4758 //
4759 #if defined(__WXMOTIF__)
4760 int cw, ch;
4761 m_gridWin->GetClientSize( &cw, &ch );
4762 if ( r.GetTop() > ch ) r.SetTop( 0 );
4763 if ( r.GetLeft() > cw ) r.SetLeft( 0 );
4764 r.SetRight( wxMin( r.GetRight(), cw ) );
4765 r.SetBottom( wxMin( r.GetBottom(), ch ) );
4766 #endif
4767
4768 // logical bounds of update region
4769 //
4770 CalcUnscrolledPosition( r.GetLeft(), r.GetTop(), &left, &top );
4771 CalcUnscrolledPosition( r.GetRight(), r.GetBottom(), &right, &bottom );
4772
4773 // find the cells within these bounds
4774 //
4775 int row, col;
4776 for ( row = internalYToRow(top); row < m_numRows; row++ )
4777 {
4778 if ( GetRowBottom(row) <= top )
4779 continue;
4780
4781 if ( GetRowTop(row) > bottom )
4782 break;
4783
4784 for ( col = internalXToCol(left); col < m_numCols; col++ )
4785 {
4786 if ( GetColRight(col) <= left )
4787 continue;
4788
4789 if ( GetColLeft(col) > right )
4790 break;
4791
4792 cellsExposed.Add( wxGridCellCoords( row, col ) );
4793 }
4794 }
4795
4796 iter++;
4797 }
4798
4799 return cellsExposed;
4800 }
4801
4802
4803 void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent& event )
4804 {
4805 int x, y, row;
4806 wxPoint pos( event.GetPosition() );
4807 CalcUnscrolledPosition( pos.x, pos.y, &x, &y );
4808
4809 if ( event.Dragging() )
4810 {
4811 if (!m_isDragging)
4812 {
4813 m_isDragging = true;
4814 m_rowLabelWin->CaptureMouse();
4815 }
4816
4817 if ( event.LeftIsDown() )
4818 {
4819 switch( m_cursorMode )
4820 {
4821 case WXGRID_CURSOR_RESIZE_ROW:
4822 {
4823 int cw, ch, left, dummy;
4824 m_gridWin->GetClientSize( &cw, &ch );
4825 CalcUnscrolledPosition( 0, 0, &left, &dummy );
4826
4827 wxClientDC dc( m_gridWin );
4828 PrepareDC( dc );
4829 y = wxMax( y,
4830 GetRowTop(m_dragRowOrCol) +
4831 GetRowMinimalHeight(m_dragRowOrCol) );
4832 dc.SetLogicalFunction(wxINVERT);
4833 if ( m_dragLastPos >= 0 )
4834 {
4835 dc.DrawLine( left, m_dragLastPos, left+cw, m_dragLastPos );
4836 }
4837 dc.DrawLine( left, y, left+cw, y );
4838 m_dragLastPos = y;
4839 }
4840 break;
4841
4842 case WXGRID_CURSOR_SELECT_ROW:
4843 if ( (row = YToRow( y )) >= 0 )
4844 {
4845 if ( m_selection )
4846 {
4847 m_selection->SelectRow( row,
4848 event.ControlDown(),
4849 event.ShiftDown(),
4850 event.AltDown(),
4851 event.MetaDown() );
4852 }
4853 }
4854
4855 // default label to suppress warnings about "enumeration value
4856 // 'xxx' not handled in switch
4857 default:
4858 break;
4859 }
4860 }
4861 return;
4862 }
4863
4864 if ( m_isDragging && (event.Entering() || event.Leaving()) )
4865 return;
4866
4867 if (m_isDragging)
4868 {
4869 if (m_rowLabelWin->HasCapture()) m_rowLabelWin->ReleaseMouse();
4870 m_isDragging = false;
4871 }
4872
4873 // ------------ Entering or leaving the window
4874 //
4875 if ( event.Entering() || event.Leaving() )
4876 {
4877 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_rowLabelWin);
4878 }
4879
4880
4881 // ------------ Left button pressed
4882 //
4883 else if ( event.LeftDown() )
4884 {
4885 // don't send a label click event for a hit on the
4886 // edge of the row label - this is probably the user
4887 // wanting to resize the row
4888 //
4889 if ( YToEdgeOfRow(y) < 0 )
4890 {
4891 row = YToRow(y);
4892 if ( row >= 0 &&
4893 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK, row, -1, event ) )
4894 {
4895 if ( !event.ShiftDown() && !event.ControlDown() )
4896 ClearSelection();
4897 if ( m_selection )
4898 {
4899 if ( event.ShiftDown() )
4900 {
4901 m_selection->SelectBlock( m_currentCellCoords.GetRow(),
4902 0,
4903 row,
4904 GetNumberCols() - 1,
4905 event.ControlDown(),
4906 event.ShiftDown(),
4907 event.AltDown(),
4908 event.MetaDown() );
4909 }
4910 else
4911 {
4912 m_selection->SelectRow( row,
4913 event.ControlDown(),
4914 event.ShiftDown(),
4915 event.AltDown(),
4916 event.MetaDown() );
4917 }
4918 }
4919
4920 ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW, m_rowLabelWin);
4921 }
4922 }
4923 else
4924 {
4925 // starting to drag-resize a row
4926 //
4927 if ( CanDragRowSize() )
4928 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW, m_rowLabelWin);
4929 }
4930 }
4931
4932
4933 // ------------ Left double click
4934 //
4935 else if (event.LeftDClick() )
4936 {
4937 int row = YToEdgeOfRow(y);
4938 if ( row < 0 )
4939 {
4940 row = YToRow(y);
4941 if ( row >=0 &&
4942 !SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK, row, -1, event ) )
4943 {
4944 // no default action at the moment
4945 }
4946 }
4947 else
4948 {
4949 // adjust row height depending on label text
4950 AutoSizeRowLabelSize( row );
4951
4952 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_colLabelWin);
4953 m_dragLastPos = -1;
4954 }
4955 }
4956
4957
4958 // ------------ Left button released
4959 //
4960 else if ( event.LeftUp() )
4961 {
4962 if ( m_cursorMode == WXGRID_CURSOR_RESIZE_ROW )
4963 {
4964 DoEndDragResizeRow();
4965
4966 // Note: we are ending the event *after* doing
4967 // default processing in this case
4968 //
4969 SendEvent( wxEVT_GRID_ROW_SIZE, m_dragRowOrCol, -1, event );
4970 }
4971
4972 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_rowLabelWin);
4973 m_dragLastPos = -1;
4974 }
4975
4976
4977 // ------------ Right button down
4978 //
4979 else if ( event.RightDown() )
4980 {
4981 row = YToRow(y);
4982 if ( row >=0 &&
4983 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK, row, -1, event ) )
4984 {
4985 // no default action at the moment
4986 }
4987 }
4988
4989
4990 // ------------ Right double click
4991 //
4992 else if ( event.RightDClick() )
4993 {
4994 row = YToRow(y);
4995 if ( row >= 0 &&
4996 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK, row, -1, event ) )
4997 {
4998 // no default action at the moment
4999 }
5000 }
5001
5002
5003 // ------------ No buttons down and mouse moving
5004 //
5005 else if ( event.Moving() )
5006 {
5007 m_dragRowOrCol = YToEdgeOfRow( y );
5008 if ( m_dragRowOrCol >= 0 )
5009 {
5010 if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
5011 {
5012 // don't capture the mouse yet
5013 if ( CanDragRowSize() )
5014 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW, m_rowLabelWin, false);
5015 }
5016 }
5017 else if ( m_cursorMode != WXGRID_CURSOR_SELECT_CELL )
5018 {
5019 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_rowLabelWin, false);
5020 }
5021 }
5022 }
5023
5024
5025 void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent& event )
5026 {
5027 int x, y, col;
5028 wxPoint pos( event.GetPosition() );
5029 CalcUnscrolledPosition( pos.x, pos.y, &x, &y );
5030
5031 if ( event.Dragging() )
5032 {
5033 if (!m_isDragging)
5034 {
5035 m_isDragging = true;
5036 m_colLabelWin->CaptureMouse();
5037 }
5038
5039 if ( event.LeftIsDown() )
5040 {
5041 switch( m_cursorMode )
5042 {
5043 case WXGRID_CURSOR_RESIZE_COL:
5044 {
5045 int cw, ch, dummy, top;
5046 m_gridWin->GetClientSize( &cw, &ch );
5047 CalcUnscrolledPosition( 0, 0, &dummy, &top );
5048
5049 wxClientDC dc( m_gridWin );
5050 PrepareDC( dc );
5051
5052 x = wxMax( x, GetColLeft(m_dragRowOrCol) +
5053 GetColMinimalWidth(m_dragRowOrCol));
5054 dc.SetLogicalFunction(wxINVERT);
5055 if ( m_dragLastPos >= 0 )
5056 {
5057 dc.DrawLine( m_dragLastPos, top, m_dragLastPos, top+ch );
5058 }
5059 dc.DrawLine( x, top, x, top+ch );
5060 m_dragLastPos = x;
5061 }
5062 break;
5063
5064 case WXGRID_CURSOR_SELECT_COL:
5065 if ( (col = XToCol( x )) >= 0 )
5066 {
5067 if ( m_selection )
5068 {
5069 m_selection->SelectCol( col,
5070 event.ControlDown(),
5071 event.ShiftDown(),
5072 event.AltDown(),
5073 event.MetaDown() );
5074 }
5075 }
5076
5077 // default label to suppress warnings about "enumeration value
5078 // 'xxx' not handled in switch
5079 default:
5080 break;
5081 }
5082 }
5083 return;
5084 }
5085
5086 if ( m_isDragging && (event.Entering() || event.Leaving()) )
5087 return;
5088
5089 if (m_isDragging)
5090 {
5091 if (m_colLabelWin->HasCapture()) m_colLabelWin->ReleaseMouse();
5092 m_isDragging = false;
5093 }
5094
5095 // ------------ Entering or leaving the window
5096 //
5097 if ( event.Entering() || event.Leaving() )
5098 {
5099 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_colLabelWin);
5100 }
5101
5102
5103 // ------------ Left button pressed
5104 //
5105 else if ( event.LeftDown() )
5106 {
5107 // don't send a label click event for a hit on the
5108 // edge of the col label - this is probably the user
5109 // wanting to resize the col
5110 //
5111 if ( XToEdgeOfCol(x) < 0 )
5112 {
5113 col = XToCol(x);
5114 if ( col >= 0 &&
5115 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK, -1, col, event ) )
5116 {
5117 if ( !event.ShiftDown() && !event.ControlDown() )
5118 ClearSelection();
5119 if ( m_selection )
5120 {
5121 if ( event.ShiftDown() )
5122 {
5123 m_selection->SelectBlock( 0,
5124 m_currentCellCoords.GetCol(),
5125 GetNumberRows() - 1, col,
5126 event.ControlDown(),
5127 event.ShiftDown(),
5128 event.AltDown(),
5129 event.MetaDown() );
5130 }
5131 else
5132 {
5133 m_selection->SelectCol( col,
5134 event.ControlDown(),
5135 event.ShiftDown(),
5136 event.AltDown(),
5137 event.MetaDown() );
5138 }
5139 }
5140
5141 ChangeCursorMode(WXGRID_CURSOR_SELECT_COL, m_colLabelWin);
5142 }
5143 }
5144 else
5145 {
5146 // starting to drag-resize a col
5147 //
5148 if ( CanDragColSize() )
5149 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL, m_colLabelWin);
5150 }
5151 }
5152
5153
5154 // ------------ Left double click
5155 //
5156 if ( event.LeftDClick() )
5157 {
5158 int col = XToEdgeOfCol(x);
5159 if ( col < 0 )
5160 {
5161 col = XToCol(x);
5162 if ( col >= 0 &&
5163 ! SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK, -1, col, event ) )
5164 {
5165 // no default action at the moment
5166 }
5167 }
5168 else
5169 {
5170 // adjust column width depending on label text
5171 AutoSizeColLabelSize( col );
5172
5173 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_colLabelWin);
5174 m_dragLastPos = -1;
5175 }
5176 }
5177
5178
5179 // ------------ Left button released
5180 //
5181 else if ( event.LeftUp() )
5182 {
5183 if ( m_cursorMode == WXGRID_CURSOR_RESIZE_COL )
5184 {
5185 DoEndDragResizeCol();
5186
5187 // Note: we are ending the event *after* doing
5188 // default processing in this case
5189 //
5190 SendEvent( wxEVT_GRID_COL_SIZE, -1, m_dragRowOrCol, event );
5191 }
5192
5193 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_colLabelWin);
5194 m_dragLastPos = -1;
5195 }
5196
5197
5198 // ------------ Right button down
5199 //
5200 else if ( event.RightDown() )
5201 {
5202 col = XToCol(x);
5203 if ( col >= 0 &&
5204 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK, -1, col, event ) )
5205 {
5206 // no default action at the moment
5207 }
5208 }
5209
5210
5211 // ------------ Right double click
5212 //
5213 else if ( event.RightDClick() )
5214 {
5215 col = XToCol(x);
5216 if ( col >= 0 &&
5217 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK, -1, col, event ) )
5218 {
5219 // no default action at the moment
5220 }
5221 }
5222
5223
5224 // ------------ No buttons down and mouse moving
5225 //
5226 else if ( event.Moving() )
5227 {
5228 m_dragRowOrCol = XToEdgeOfCol( x );
5229 if ( m_dragRowOrCol >= 0 )
5230 {
5231 if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
5232 {
5233 // don't capture the cursor yet
5234 if ( CanDragColSize() )
5235 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL, m_colLabelWin, false);
5236 }
5237 }
5238 else if ( m_cursorMode != WXGRID_CURSOR_SELECT_CELL )
5239 {
5240 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_colLabelWin, false);
5241 }
5242 }
5243 }
5244
5245
5246 void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent& event )
5247 {
5248 if ( event.LeftDown() )
5249 {
5250 // indicate corner label by having both row and
5251 // col args == -1
5252 //
5253 if ( !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK, -1, -1, event ) )
5254 {
5255 SelectAll();
5256 }
5257 }
5258
5259 else if ( event.LeftDClick() )
5260 {
5261 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK, -1, -1, event );
5262 }
5263
5264 else if ( event.RightDown() )
5265 {
5266 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK, -1, -1, event ) )
5267 {
5268 // no default action at the moment
5269 }
5270 }
5271
5272 else if ( event.RightDClick() )
5273 {
5274 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK, -1, -1, event ) )
5275 {
5276 // no default action at the moment
5277 }
5278 }
5279 }
5280
5281 void wxGrid::ChangeCursorMode(CursorMode mode,
5282 wxWindow *win,
5283 bool captureMouse)
5284 {
5285 #ifdef __WXDEBUG__
5286 static const wxChar *cursorModes[] =
5287 {
5288 _T("SELECT_CELL"),
5289 _T("RESIZE_ROW"),
5290 _T("RESIZE_COL"),
5291 _T("SELECT_ROW"),
5292 _T("SELECT_COL")
5293 };
5294
5295 wxLogTrace(_T("grid"),
5296 _T("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
5297 win == m_colLabelWin ? _T("colLabelWin")
5298 : win ? _T("rowLabelWin")
5299 : _T("gridWin"),
5300 cursorModes[m_cursorMode], cursorModes[mode]);
5301 #endif // __WXDEBUG__
5302
5303 if ( mode == m_cursorMode &&
5304 win == m_winCapture &&
5305 captureMouse == (m_winCapture != NULL))
5306 return;
5307
5308 if ( !win )
5309 {
5310 // by default use the grid itself
5311 win = m_gridWin;
5312 }
5313
5314 if ( m_winCapture )
5315 {
5316 if (m_winCapture->HasCapture()) m_winCapture->ReleaseMouse();
5317 m_winCapture = (wxWindow *)NULL;
5318 }
5319
5320 m_cursorMode = mode;
5321
5322 switch ( m_cursorMode )
5323 {
5324 case WXGRID_CURSOR_RESIZE_ROW:
5325 win->SetCursor( m_rowResizeCursor );
5326 break;
5327
5328 case WXGRID_CURSOR_RESIZE_COL:
5329 win->SetCursor( m_colResizeCursor );
5330 break;
5331
5332 default:
5333 win->SetCursor( *wxSTANDARD_CURSOR );
5334 }
5335
5336 // we need to capture mouse when resizing
5337 bool resize = m_cursorMode == WXGRID_CURSOR_RESIZE_ROW ||
5338 m_cursorMode == WXGRID_CURSOR_RESIZE_COL;
5339
5340 if ( captureMouse && resize )
5341 {
5342 win->CaptureMouse();
5343 m_winCapture = win;
5344 }
5345 }
5346
5347 void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent& event )
5348 {
5349 int x, y;
5350 wxPoint pos( event.GetPosition() );
5351 CalcUnscrolledPosition( pos.x, pos.y, &x, &y );
5352
5353 wxGridCellCoords coords;
5354 XYToCell( x, y, coords );
5355
5356 int cell_rows, cell_cols;
5357 bool isFirstDrag = !m_isDragging;
5358 GetCellSize( coords.GetRow(), coords.GetCol(), &cell_rows, &cell_cols );
5359 if ((cell_rows < 0) || (cell_cols < 0))
5360 {
5361 coords.SetRow(coords.GetRow() + cell_rows);
5362 coords.SetCol(coords.GetCol() + cell_cols);
5363 }
5364
5365 if ( event.Dragging() )
5366 {
5367 //wxLogDebug("pos(%d, %d) coords(%d, %d)", pos.x, pos.y, coords.GetRow(), coords.GetCol());
5368
5369 // Don't start doing anything until the mouse has been drug at
5370 // least 3 pixels in any direction...
5371 if (! m_isDragging)
5372 {
5373 if (m_startDragPos == wxDefaultPosition)
5374 {
5375 m_startDragPos = pos;
5376 return;
5377 }
5378 if (abs(m_startDragPos.x - pos.x) < 4 && abs(m_startDragPos.y - pos.y) < 4)
5379 return;
5380 }
5381
5382 m_isDragging = true;
5383 if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
5384 {
5385 // Hide the edit control, so it
5386 // won't interfer with drag-shrinking.
5387 if ( IsCellEditControlShown() )
5388 {
5389 HideCellEditControl();
5390 SaveEditControlValue();
5391 }
5392
5393 // Have we captured the mouse yet?
5394 if (! m_winCapture)
5395 {
5396 m_winCapture = m_gridWin;
5397 m_winCapture->CaptureMouse();
5398 }
5399
5400 if ( coords != wxGridNoCellCoords )
5401 {
5402 if ( event.ControlDown() )
5403 {
5404 if ( m_selectingKeyboard == wxGridNoCellCoords)
5405 m_selectingKeyboard = coords;
5406 HighlightBlock ( m_selectingKeyboard, coords );
5407 }
5408 else if ( CanDragCell() )
5409 {
5410 if ( isFirstDrag )
5411 {
5412 if ( m_selectingKeyboard == wxGridNoCellCoords)
5413 m_selectingKeyboard = coords;
5414
5415 SendEvent( wxEVT_GRID_CELL_BEGIN_DRAG,
5416 coords.GetRow(),
5417 coords.GetCol(),
5418 event );
5419 }
5420 }
5421 else
5422 {
5423 if ( !IsSelection() )
5424 {
5425 HighlightBlock( coords, coords );
5426 }
5427 else
5428 {
5429 HighlightBlock( m_currentCellCoords, coords );
5430 }
5431 }
5432
5433 if (! IsVisible(coords))
5434 {
5435 MakeCellVisible(coords);
5436 // TODO: need to introduce a delay or something here. The
5437 // scrolling is way to fast, at least on MSW - also on GTK.
5438 }
5439 }
5440 }
5441 else if ( m_cursorMode == WXGRID_CURSOR_RESIZE_ROW )
5442 {
5443 int cw, ch, left, dummy;
5444 m_gridWin->GetClientSize( &cw, &ch );
5445 CalcUnscrolledPosition( 0, 0, &left, &dummy );
5446
5447 wxClientDC dc( m_gridWin );
5448 PrepareDC( dc );
5449 y = wxMax( y, GetRowTop(m_dragRowOrCol) +
5450 GetRowMinimalHeight(m_dragRowOrCol) );
5451 dc.SetLogicalFunction(wxINVERT);
5452 if ( m_dragLastPos >= 0 )
5453 {
5454 dc.DrawLine( left, m_dragLastPos, left+cw, m_dragLastPos );
5455 }
5456 dc.DrawLine( left, y, left+cw, y );
5457 m_dragLastPos = y;
5458 }
5459 else if ( m_cursorMode == WXGRID_CURSOR_RESIZE_COL )
5460 {
5461 int cw, ch, dummy, top;
5462 m_gridWin->GetClientSize( &cw, &ch );
5463 CalcUnscrolledPosition( 0, 0, &dummy, &top );
5464
5465 wxClientDC dc( m_gridWin );
5466 PrepareDC( dc );
5467 x = wxMax( x, GetColLeft(m_dragRowOrCol) +
5468 GetColMinimalWidth(m_dragRowOrCol) );
5469 dc.SetLogicalFunction(wxINVERT);
5470 if ( m_dragLastPos >= 0 )
5471 {
5472 dc.DrawLine( m_dragLastPos, top, m_dragLastPos, top+ch );
5473 }
5474 dc.DrawLine( x, top, x, top+ch );
5475 m_dragLastPos = x;
5476 }
5477
5478 return;
5479 }
5480
5481 m_isDragging = false;
5482 m_startDragPos = wxDefaultPosition;
5483
5484 // VZ: if we do this, the mode is reset to WXGRID_CURSOR_SELECT_CELL
5485 // immediately after it becomes WXGRID_CURSOR_RESIZE_ROW/COL under
5486 // wxGTK
5487 #if 0
5488 if ( event.Entering() || event.Leaving() )
5489 {
5490 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL);
5491 m_gridWin->SetCursor( *wxSTANDARD_CURSOR );
5492 }
5493 else
5494 #endif // 0
5495
5496 // ------------ Left button pressed
5497 //
5498 if ( event.LeftDown() && coords != wxGridNoCellCoords )
5499 {
5500 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_CLICK,
5501 coords.GetRow(),
5502 coords.GetCol(),
5503 event ) )
5504 {
5505 if ( !event.ControlDown() )
5506 ClearSelection();
5507 if ( event.ShiftDown() )
5508 {
5509 if ( m_selection )
5510 {
5511 m_selection->SelectBlock( m_currentCellCoords.GetRow(),
5512 m_currentCellCoords.GetCol(),
5513 coords.GetRow(),
5514 coords.GetCol(),
5515 event.ControlDown(),
5516 event.ShiftDown(),
5517 event.AltDown(),
5518 event.MetaDown() );
5519 }
5520 }
5521 else if ( XToEdgeOfCol(x) < 0 &&
5522 YToEdgeOfRow(y) < 0 )
5523 {
5524 DisableCellEditControl();
5525 MakeCellVisible( coords );
5526
5527 if ( event.ControlDown() )
5528 {
5529 if ( m_selection )
5530 {
5531 m_selection->ToggleCellSelection( coords.GetRow(),
5532 coords.GetCol(),
5533 event.ControlDown(),
5534 event.ShiftDown(),
5535 event.AltDown(),
5536 event.MetaDown() );
5537 }
5538 m_selectingTopLeft = wxGridNoCellCoords;
5539 m_selectingBottomRight = wxGridNoCellCoords;
5540 m_selectingKeyboard = coords;
5541 }
5542 else
5543 {
5544 m_waitForSlowClick = m_currentCellCoords == coords && coords != wxGridNoCellCoords;
5545 SetCurrentCell( coords );
5546 if ( m_selection )
5547 {
5548 if ( m_selection->GetSelectionMode() !=
5549 wxGrid::wxGridSelectCells )
5550 {
5551 HighlightBlock( coords, coords );
5552 }
5553 }
5554 }
5555 }
5556 }
5557 }
5558
5559
5560 // ------------ Left double click
5561 //
5562 else if ( event.LeftDClick() && coords != wxGridNoCellCoords )
5563 {
5564 DisableCellEditControl();
5565
5566 if ( XToEdgeOfCol(x) < 0 && YToEdgeOfRow(y) < 0 )
5567 {
5568 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_DCLICK,
5569 coords.GetRow(),
5570 coords.GetCol(),
5571 event ) )
5572 {
5573 // we want double click to select a cell and start editing
5574 // (i.e. to behave in same way as sequence of two slow clicks):
5575 m_waitForSlowClick = true;
5576 }
5577 }
5578
5579 }
5580
5581
5582 // ------------ Left button released
5583 //
5584 else if ( event.LeftUp() )
5585 {
5586 if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
5587 {
5588 if (m_winCapture)
5589 {
5590 if (m_winCapture->HasCapture()) m_winCapture->ReleaseMouse();
5591 m_winCapture = NULL;
5592 }
5593
5594 if ( coords == m_currentCellCoords && m_waitForSlowClick && CanEnableCellControl())
5595 {
5596 ClearSelection();
5597 EnableCellEditControl();
5598
5599 wxGridCellAttr* attr = GetCellAttr(coords);
5600 wxGridCellEditor *editor = attr->GetEditor(this, coords.GetRow(), coords.GetCol());
5601 editor->StartingClick();
5602 editor->DecRef();
5603 attr->DecRef();
5604
5605 m_waitForSlowClick = false;
5606 }
5607 else if ( m_selectingTopLeft != wxGridNoCellCoords &&
5608 m_selectingBottomRight != wxGridNoCellCoords )
5609 {
5610 if ( m_selection )
5611 {
5612 m_selection->SelectBlock( m_selectingTopLeft.GetRow(),
5613 m_selectingTopLeft.GetCol(),
5614 m_selectingBottomRight.GetRow(),
5615 m_selectingBottomRight.GetCol(),
5616 event.ControlDown(),
5617 event.ShiftDown(),
5618 event.AltDown(),
5619 event.MetaDown() );
5620 }
5621
5622 m_selectingTopLeft = wxGridNoCellCoords;
5623 m_selectingBottomRight = wxGridNoCellCoords;
5624
5625 // Show the edit control, if it has been hidden for
5626 // drag-shrinking.
5627 ShowCellEditControl();
5628 }
5629 }
5630 else if ( m_cursorMode == WXGRID_CURSOR_RESIZE_ROW )
5631 {
5632 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL);
5633 DoEndDragResizeRow();
5634
5635 // Note: we are ending the event *after* doing
5636 // default processing in this case
5637 //
5638 SendEvent( wxEVT_GRID_ROW_SIZE, m_dragRowOrCol, -1, event );
5639 }
5640 else if ( m_cursorMode == WXGRID_CURSOR_RESIZE_COL )
5641 {
5642 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL);
5643 DoEndDragResizeCol();
5644
5645 // Note: we are ending the event *after* doing
5646 // default processing in this case
5647 //
5648 SendEvent( wxEVT_GRID_COL_SIZE, -1, m_dragRowOrCol, event );
5649 }
5650
5651 m_dragLastPos = -1;
5652 }
5653
5654
5655 // ------------ Right button down
5656 //
5657 else if ( event.RightDown() && coords != wxGridNoCellCoords )
5658 {
5659 DisableCellEditControl();
5660 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_CLICK,
5661 coords.GetRow(),
5662 coords.GetCol(),
5663 event ) )
5664 {
5665 // no default action at the moment
5666 }
5667 }
5668
5669
5670 // ------------ Right double click
5671 //
5672 else if ( event.RightDClick() && coords != wxGridNoCellCoords )
5673 {
5674 DisableCellEditControl();
5675 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_DCLICK,
5676 coords.GetRow(),
5677 coords.GetCol(),
5678 event ) )
5679 {
5680 // no default action at the moment
5681 }
5682 }
5683
5684 // ------------ Moving and no button action
5685 //
5686 else if ( event.Moving() && !event.IsButton() )
5687 {
5688 if( coords.GetRow() < 0 || coords.GetCol() < 0 )
5689 {
5690 // out of grid cell area
5691 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL);
5692 return;
5693 }
5694
5695 int dragRow = YToEdgeOfRow( y );
5696 int dragCol = XToEdgeOfCol( x );
5697
5698 // Dragging on the corner of a cell to resize in both
5699 // directions is not implemented yet...
5700 //
5701 if ( dragRow >= 0 && dragCol >= 0 )
5702 {
5703 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL);
5704 return;
5705 }
5706
5707 if ( dragRow >= 0 )
5708 {
5709 m_dragRowOrCol = dragRow;
5710
5711 if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
5712 {
5713 if ( CanDragRowSize() && CanDragGridSize() )
5714 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW);
5715 }
5716
5717 if ( dragCol >= 0 )
5718 {
5719 m_dragRowOrCol = dragCol;
5720 }
5721
5722 return;
5723 }
5724
5725 if ( dragCol >= 0 )
5726 {
5727 m_dragRowOrCol = dragCol;
5728
5729 if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
5730 {
5731 if ( CanDragColSize() && CanDragGridSize() )
5732 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL);
5733 }
5734
5735 return;
5736 }
5737
5738 // Neither on a row or col edge
5739 //
5740 if ( m_cursorMode != WXGRID_CURSOR_SELECT_CELL )
5741 {
5742 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL);
5743 }
5744 }
5745 }
5746
5747
5748 void wxGrid::DoEndDragResizeRow()
5749 {
5750 if ( m_dragLastPos >= 0 )
5751 {
5752 // erase the last line and resize the row
5753 //
5754 int cw, ch, left, dummy;
5755 m_gridWin->GetClientSize( &cw, &ch );
5756 CalcUnscrolledPosition( 0, 0, &left, &dummy );
5757
5758 wxClientDC dc( m_gridWin );
5759 PrepareDC( dc );
5760 dc.SetLogicalFunction( wxINVERT );
5761 dc.DrawLine( left, m_dragLastPos, left+cw, m_dragLastPos );
5762 HideCellEditControl();
5763 SaveEditControlValue();
5764
5765 int rowTop = GetRowTop(m_dragRowOrCol);
5766 SetRowSize( m_dragRowOrCol,
5767 wxMax( m_dragLastPos - rowTop, m_minAcceptableRowHeight ) );
5768
5769 if ( !GetBatchCount() )
5770 {
5771 // Only needed to get the correct rect.y:
5772 wxRect rect ( CellToRect( m_dragRowOrCol, 0 ) );
5773 rect.x = 0;
5774 CalcScrolledPosition(0, rect.y, &dummy, &rect.y);
5775 rect.width = m_rowLabelWidth;
5776 rect.height = ch - rect.y;
5777 m_rowLabelWin->Refresh( true, &rect );
5778 rect.width = cw;
5779 // if there is a multicell block, paint all of it
5780 if (m_table)
5781 {
5782 int i, cell_rows, cell_cols, subtract_rows = 0;
5783 int leftCol = XToCol(left);
5784 int rightCol = internalXToCol(left+cw);
5785 if (leftCol >= 0)
5786 {
5787 for (i=leftCol; i<rightCol; i++)
5788 {
5789 GetCellSize(m_dragRowOrCol, i, &cell_rows, &cell_cols);
5790 if (cell_rows < subtract_rows)
5791 subtract_rows = cell_rows;
5792 }
5793 rect.y = GetRowTop(m_dragRowOrCol + subtract_rows);
5794 CalcScrolledPosition(0, rect.y, &dummy, &rect.y);
5795 rect.height = ch - rect.y;
5796 }
5797 }
5798 m_gridWin->Refresh( false, &rect );
5799 }
5800
5801 ShowCellEditControl();
5802 }
5803 }
5804
5805
5806 void wxGrid::DoEndDragResizeCol()
5807 {
5808 if ( m_dragLastPos >= 0 )
5809 {
5810 // erase the last line and resize the col
5811 //
5812 int cw, ch, dummy, top;
5813 m_gridWin->GetClientSize( &cw, &ch );
5814 CalcUnscrolledPosition( 0, 0, &dummy, &top );
5815
5816 wxClientDC dc( m_gridWin );
5817 PrepareDC( dc );
5818 dc.SetLogicalFunction( wxINVERT );
5819 dc.DrawLine( m_dragLastPos, top, m_dragLastPos, top+ch );
5820 HideCellEditControl();
5821 SaveEditControlValue();
5822
5823 int colLeft = GetColLeft(m_dragRowOrCol);
5824 SetColSize( m_dragRowOrCol,
5825 wxMax( m_dragLastPos - colLeft,
5826 GetColMinimalWidth(m_dragRowOrCol) ) );
5827
5828 if ( !GetBatchCount() )
5829 {
5830 // Only needed to get the correct rect.x:
5831 wxRect rect ( CellToRect( 0, m_dragRowOrCol ) );
5832 rect.y = 0;
5833 CalcScrolledPosition(rect.x, 0, &rect.x, &dummy);
5834 rect.width = cw - rect.x;
5835 rect.height = m_colLabelHeight;
5836 m_colLabelWin->Refresh( true, &rect );
5837 rect.height = ch;
5838 // if there is a multicell block, paint all of it
5839 if (m_table)
5840 {
5841 int i, cell_rows, cell_cols, subtract_cols = 0;
5842 int topRow = YToRow(top);
5843 int bottomRow = internalYToRow(top+cw);
5844 if (topRow >= 0)
5845 {
5846 for (i=topRow; i<bottomRow; i++)
5847 {
5848 GetCellSize(i, m_dragRowOrCol, &cell_rows, &cell_cols);
5849 if (cell_cols < subtract_cols)
5850 subtract_cols = cell_cols;
5851 }
5852 rect.x = GetColLeft(m_dragRowOrCol + subtract_cols);
5853 CalcScrolledPosition(rect.x, 0, &rect.x, &dummy);
5854 rect.width = cw - rect.x;
5855 }
5856 }
5857 m_gridWin->Refresh( false, &rect );
5858 }
5859
5860 ShowCellEditControl();
5861 }
5862 }
5863
5864
5865
5866 //
5867 // ------ interaction with data model
5868 //
5869 bool wxGrid::ProcessTableMessage( wxGridTableMessage& msg )
5870 {
5871 switch ( msg.GetId() )
5872 {
5873 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES:
5874 return GetModelValues();
5875
5876 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES:
5877 return SetModelValues();
5878
5879 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED:
5880 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED:
5881 case wxGRIDTABLE_NOTIFY_ROWS_DELETED:
5882 case wxGRIDTABLE_NOTIFY_COLS_INSERTED:
5883 case wxGRIDTABLE_NOTIFY_COLS_APPENDED:
5884 case wxGRIDTABLE_NOTIFY_COLS_DELETED:
5885 return Redimension( msg );
5886
5887 default:
5888 return false;
5889 }
5890 }
5891
5892
5893
5894 // The behaviour of this function depends on the grid table class
5895 // Clear() function. For the default wxGridStringTable class the
5896 // behavious is to replace all cell contents with wxEmptyString but
5897 // not to change the number of rows or cols.
5898 //
5899 void wxGrid::ClearGrid()
5900 {
5901 if ( m_table )
5902 {
5903 if (IsCellEditControlEnabled())
5904 DisableCellEditControl();
5905
5906 m_table->Clear();
5907 if ( !GetBatchCount() ) m_gridWin->Refresh();
5908 }
5909 }
5910
5911
5912 bool wxGrid::InsertRows( int pos, int numRows, bool WXUNUSED(updateLabels) )
5913 {
5914 // TODO: something with updateLabels flag
5915
5916 if ( !m_created )
5917 {
5918 wxFAIL_MSG( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") );
5919 return false;
5920 }
5921
5922 if ( m_table )
5923 {
5924 if (IsCellEditControlEnabled())
5925 DisableCellEditControl();
5926
5927 bool done = m_table->InsertRows( pos, numRows );
5928 return done;
5929
5930 // the table will have sent the results of the insert row
5931 // operation to this view object as a grid table message
5932 }
5933 return false;
5934 }
5935
5936
5937 bool wxGrid::AppendRows( int numRows, bool WXUNUSED(updateLabels) )
5938 {
5939 // TODO: something with updateLabels flag
5940
5941 if ( !m_created )
5942 {
5943 wxFAIL_MSG( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") );
5944 return false;
5945 }
5946
5947 if ( m_table )
5948 {
5949 bool done = m_table && m_table->AppendRows( numRows );
5950 return done;
5951 // the table will have sent the results of the append row
5952 // operation to this view object as a grid table message
5953 }
5954 return false;
5955 }
5956
5957
5958 bool wxGrid::DeleteRows( int pos, int numRows, bool WXUNUSED(updateLabels) )
5959 {
5960 // TODO: something with updateLabels flag
5961
5962 if ( !m_created )
5963 {
5964 wxFAIL_MSG( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") );
5965 return false;
5966 }
5967
5968 if ( m_table )
5969 {
5970 if (IsCellEditControlEnabled())
5971 DisableCellEditControl();
5972
5973 bool done = m_table->DeleteRows( pos, numRows );
5974 return done;
5975 // the table will have sent the results of the delete row
5976 // operation to this view object as a grid table message
5977 }
5978 return false;
5979 }
5980
5981
5982 bool wxGrid::InsertCols( int pos, int numCols, bool WXUNUSED(updateLabels) )
5983 {
5984 // TODO: something with updateLabels flag
5985
5986 if ( !m_created )
5987 {
5988 wxFAIL_MSG( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") );
5989 return false;
5990 }
5991
5992 if ( m_table )
5993 {
5994 if (IsCellEditControlEnabled())
5995 DisableCellEditControl();
5996
5997 bool done = m_table->InsertCols( pos, numCols );
5998 return done;
5999 // the table will have sent the results of the insert col
6000 // operation to this view object as a grid table message
6001 }
6002 return false;
6003 }
6004
6005
6006 bool wxGrid::AppendCols( int numCols, bool WXUNUSED(updateLabels) )
6007 {
6008 // TODO: something with updateLabels flag
6009
6010 if ( !m_created )
6011 {
6012 wxFAIL_MSG( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") );
6013 return false;
6014 }
6015
6016 if ( m_table )
6017 {
6018 bool done = m_table->AppendCols( numCols );
6019 return done;
6020 // the table will have sent the results of the append col
6021 // operation to this view object as a grid table message
6022 }
6023 return false;
6024 }
6025
6026
6027 bool wxGrid::DeleteCols( int pos, int numCols, bool WXUNUSED(updateLabels) )
6028 {
6029 // TODO: something with updateLabels flag
6030
6031 if ( !m_created )
6032 {
6033 wxFAIL_MSG( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") );
6034 return false;
6035 }
6036
6037 if ( m_table )
6038 {
6039 if (IsCellEditControlEnabled())
6040 DisableCellEditControl();
6041
6042 bool done = m_table->DeleteCols( pos, numCols );
6043 return done;
6044 // the table will have sent the results of the delete col
6045 // operation to this view object as a grid table message
6046 }
6047 return false;
6048 }
6049
6050
6051
6052 //
6053 // ----- event handlers
6054 //
6055
6056 // Generate a grid event based on a mouse event and
6057 // return the result of ProcessEvent()
6058 //
6059 int wxGrid::SendEvent( const wxEventType type,
6060 int row, int col,
6061 wxMouseEvent& mouseEv )
6062 {
6063 bool claimed;
6064 bool vetoed;
6065
6066 if ( type == wxEVT_GRID_ROW_SIZE || type == wxEVT_GRID_COL_SIZE )
6067 {
6068 int rowOrCol = (row == -1 ? col : row);
6069
6070 wxGridSizeEvent gridEvt( GetId(),
6071 type,
6072 this,
6073 rowOrCol,
6074 mouseEv.GetX() + GetRowLabelSize(),
6075 mouseEv.GetY() + GetColLabelSize(),
6076 mouseEv.ControlDown(),
6077 mouseEv.ShiftDown(),
6078 mouseEv.AltDown(),
6079 mouseEv.MetaDown() );
6080
6081 claimed = GetEventHandler()->ProcessEvent(gridEvt);
6082 vetoed = !gridEvt.IsAllowed();
6083 }
6084 else if ( type == wxEVT_GRID_RANGE_SELECT )
6085 {
6086 // Right now, it should _never_ end up here!
6087 wxGridRangeSelectEvent gridEvt( GetId(),
6088 type,
6089 this,
6090 m_selectingTopLeft,
6091 m_selectingBottomRight,
6092 true,
6093 mouseEv.ControlDown(),
6094 mouseEv.ShiftDown(),
6095 mouseEv.AltDown(),
6096 mouseEv.MetaDown() );
6097
6098 claimed = GetEventHandler()->ProcessEvent(gridEvt);
6099 vetoed = !gridEvt.IsAllowed();
6100 }
6101 else
6102 {
6103 wxGridEvent gridEvt( GetId(),
6104 type,
6105 this,
6106 row, col,
6107 mouseEv.GetX() + GetRowLabelSize(),
6108 mouseEv.GetY() + GetColLabelSize(),
6109 false,
6110 mouseEv.ControlDown(),
6111 mouseEv.ShiftDown(),
6112 mouseEv.AltDown(),
6113 mouseEv.MetaDown() );
6114 claimed = GetEventHandler()->ProcessEvent(gridEvt);
6115 vetoed = !gridEvt.IsAllowed();
6116 }
6117
6118 // A Veto'd event may not be `claimed' so test this first
6119 if (vetoed) return -1;
6120 return claimed ? 1 : 0;
6121 }
6122
6123
6124 // Generate a grid event of specified type and return the result
6125 // of ProcessEvent().
6126 //
6127 int wxGrid::SendEvent( const wxEventType type,
6128 int row, int col )
6129 {
6130 bool claimed;
6131 bool vetoed;
6132
6133 if ( type == wxEVT_GRID_ROW_SIZE || type == wxEVT_GRID_COL_SIZE )
6134 {
6135 int rowOrCol = (row == -1 ? col : row);
6136
6137 wxGridSizeEvent gridEvt( GetId(),
6138 type,
6139 this,
6140 rowOrCol );
6141
6142 claimed = GetEventHandler()->ProcessEvent(gridEvt);
6143 vetoed = !gridEvt.IsAllowed();
6144 }
6145 else
6146 {
6147 wxGridEvent gridEvt( GetId(),
6148 type,
6149 this,
6150 row, col );
6151
6152 claimed = GetEventHandler()->ProcessEvent(gridEvt);
6153 vetoed = !gridEvt.IsAllowed();
6154 }
6155
6156 // A Veto'd event may not be `claimed' so test this first
6157 if (vetoed) return -1;
6158 return claimed ? 1 : 0;
6159 }
6160
6161
6162 void wxGrid::OnPaint( wxPaintEvent& WXUNUSED(event) )
6163 {
6164 wxPaintDC dc(this); // needed to prevent zillions of paint events on MSW
6165 }
6166
6167 void wxGrid::Refresh(bool eraseb, const wxRect* rect)
6168 {
6169 // Don't do anything if between Begin/EndBatch...
6170 // EndBatch() will do all this on the last nested one anyway.
6171 if (! GetBatchCount())
6172 {
6173 // Refresh to get correct scrolled position:
6174 wxScrolledWindow::Refresh(eraseb,rect);
6175
6176 if (rect)
6177 {
6178 int rect_x, rect_y, rectWidth, rectHeight;
6179 int width_label, width_cell, height_label, height_cell;
6180 int x, y;
6181
6182 //Copy rectangle can get scroll offsets..
6183 rect_x = rect->GetX();
6184 rect_y = rect->GetY();
6185 rectWidth = rect->GetWidth();
6186 rectHeight = rect->GetHeight();
6187
6188 width_label = m_rowLabelWidth - rect_x;
6189 if (width_label > rectWidth) width_label = rectWidth;
6190
6191 height_label = m_colLabelHeight - rect_y;
6192 if (height_label > rectHeight) height_label = rectHeight;
6193
6194 if (rect_x > m_rowLabelWidth)
6195 {
6196 x = rect_x - m_rowLabelWidth;
6197 width_cell = rectWidth;
6198 }
6199 else
6200 {
6201 x = 0;
6202 width_cell = rectWidth - (m_rowLabelWidth - rect_x);
6203 }
6204
6205 if (rect_y > m_colLabelHeight)
6206 {
6207 y = rect_y - m_colLabelHeight;
6208 height_cell = rectHeight;
6209 }
6210 else
6211 {
6212 y = 0;
6213 height_cell = rectHeight - (m_colLabelHeight - rect_y);
6214 }
6215
6216 // Paint corner label part intersecting rect.
6217 if ( width_label > 0 && height_label > 0 )
6218 {
6219 wxRect anotherrect(rect_x, rect_y, width_label, height_label);
6220 m_cornerLabelWin->Refresh(eraseb, &anotherrect);
6221 }
6222
6223 // Paint col labels part intersecting rect.
6224 if ( width_cell > 0 && height_label > 0 )
6225 {
6226 wxRect anotherrect(x, rect_y, width_cell, height_label);
6227 m_colLabelWin->Refresh(eraseb, &anotherrect);
6228 }
6229
6230 // Paint row labels part intersecting rect.
6231 if ( width_label > 0 && height_cell > 0 )
6232 {
6233 wxRect anotherrect(rect_x, y, width_label, height_cell);
6234 m_rowLabelWin->Refresh(eraseb, &anotherrect);
6235 }
6236
6237 // Paint cell area part intersecting rect.
6238 if ( width_cell > 0 && height_cell > 0 )
6239 {
6240 wxRect anotherrect(x, y, width_cell, height_cell);
6241 m_gridWin->Refresh(eraseb, &anotherrect);
6242 }
6243 }
6244 else
6245 {
6246 m_cornerLabelWin->Refresh(eraseb, NULL);
6247 m_colLabelWin->Refresh(eraseb, NULL);
6248 m_rowLabelWin->Refresh(eraseb, NULL);
6249 m_gridWin->Refresh(eraseb, NULL);
6250 }
6251 }
6252 }
6253
6254 void wxGrid::OnSize( wxSizeEvent& event )
6255 {
6256 // position the child windows
6257 CalcWindowSizes();
6258
6259 // don't call CalcDimensions() from here, the base class handles the size
6260 // changes itself
6261 event.Skip();
6262 }
6263
6264
6265 void wxGrid::OnKeyDown( wxKeyEvent& event )
6266 {
6267 if ( m_inOnKeyDown )
6268 {
6269 // shouldn't be here - we are going round in circles...
6270 //
6271 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") );
6272 }
6273
6274 m_inOnKeyDown = true;
6275
6276 // propagate the event up and see if it gets processed
6277 //
6278 wxWindow *parent = GetParent();
6279 wxKeyEvent keyEvt( event );
6280 keyEvt.SetEventObject( parent );
6281
6282 if ( !parent->GetEventHandler()->ProcessEvent( keyEvt ) )
6283 {
6284
6285 // try local handlers
6286 //
6287 switch ( event.GetKeyCode() )
6288 {
6289 case WXK_UP:
6290 if ( event.ControlDown() )
6291 {
6292 MoveCursorUpBlock( event.ShiftDown() );
6293 }
6294 else
6295 {
6296 MoveCursorUp( event.ShiftDown() );
6297 }
6298 break;
6299
6300 case WXK_DOWN:
6301 if ( event.ControlDown() )
6302 {
6303 MoveCursorDownBlock( event.ShiftDown() );
6304 }
6305 else
6306 {
6307 MoveCursorDown( event.ShiftDown() );
6308 }
6309 break;
6310
6311 case WXK_LEFT:
6312 if ( event.ControlDown() )
6313 {
6314 MoveCursorLeftBlock( event.ShiftDown() );
6315 }
6316 else
6317 {
6318 MoveCursorLeft( event.ShiftDown() );
6319 }
6320 break;
6321
6322 case WXK_RIGHT:
6323 if ( event.ControlDown() )
6324 {
6325 MoveCursorRightBlock( event.ShiftDown() );
6326 }
6327 else
6328 {
6329 MoveCursorRight( event.ShiftDown() );
6330 }
6331 break;
6332
6333 case WXK_RETURN:
6334 case WXK_NUMPAD_ENTER:
6335 if ( event.ControlDown() )
6336 {
6337 event.Skip(); // to let the edit control have the return
6338 }
6339 else
6340 {
6341 if ( GetGridCursorRow() < GetNumberRows()-1 )
6342 {
6343 MoveCursorDown( event.ShiftDown() );
6344 }
6345 else
6346 {
6347 // at the bottom of a column
6348 DisableCellEditControl();
6349 }
6350 }
6351 break;
6352
6353 case WXK_ESCAPE:
6354 ClearSelection();
6355 break;
6356
6357 case WXK_TAB:
6358 if (event.ShiftDown())
6359 {
6360 if ( GetGridCursorCol() > 0 )
6361 {
6362 MoveCursorLeft( false );
6363 }
6364 else
6365 {
6366 // at left of grid
6367 DisableCellEditControl();
6368 }
6369 }
6370 else
6371 {
6372 if ( GetGridCursorCol() < GetNumberCols()-1 )
6373 {
6374 MoveCursorRight( false );
6375 }
6376 else
6377 {
6378 // at right of grid
6379 DisableCellEditControl();
6380 }
6381 }
6382 break;
6383
6384 case WXK_HOME:
6385 if ( event.ControlDown() )
6386 {
6387 MakeCellVisible( 0, 0 );
6388 SetCurrentCell( 0, 0 );
6389 }
6390 else
6391 {
6392 event.Skip();
6393 }
6394 break;
6395
6396 case WXK_END:
6397 if ( event.ControlDown() )
6398 {
6399 MakeCellVisible( m_numRows-1, m_numCols-1 );
6400 SetCurrentCell( m_numRows-1, m_numCols-1 );
6401 }
6402 else
6403 {
6404 event.Skip();
6405 }
6406 break;
6407
6408 case WXK_PRIOR:
6409 MovePageUp();
6410 break;
6411
6412 case WXK_NEXT:
6413 MovePageDown();
6414 break;
6415
6416 case WXK_SPACE:
6417 if ( event.ControlDown() )
6418 {
6419 if ( m_selection )
6420 {
6421 m_selection->ToggleCellSelection( m_currentCellCoords.GetRow(),
6422 m_currentCellCoords.GetCol(),
6423 event.ControlDown(),
6424 event.ShiftDown(),
6425 event.AltDown(),
6426 event.MetaDown() );
6427 }
6428 break;
6429 }
6430 if ( !IsEditable() )
6431 {
6432 MoveCursorRight( false );
6433 break;
6434 }
6435 // Otherwise fall through to default
6436
6437 default:
6438 // is it possible to edit the current cell at all?
6439 if ( !IsCellEditControlEnabled() && CanEnableCellControl() )
6440 {
6441 // yes, now check whether the cells editor accepts the key
6442 int row = m_currentCellCoords.GetRow();
6443 int col = m_currentCellCoords.GetCol();
6444 wxGridCellAttr* attr = GetCellAttr(row, col);
6445 wxGridCellEditor *editor = attr->GetEditor(this, row, col);
6446
6447 // <F2> is special and will always start editing, for
6448 // other keys - ask the editor itself
6449 if ( (event.GetKeyCode() == WXK_F2 && !event.HasModifiers())
6450 || editor->IsAcceptedKey(event) )
6451 {
6452 // ensure cell is visble
6453 MakeCellVisible(row, col);
6454 EnableCellEditControl();
6455
6456 // a problem can arise if the cell is not completely
6457 // visible (even after calling MakeCellVisible the
6458 // control is not created and calling StartingKey will
6459 // crash the app
6460 if( editor->IsCreated() && m_cellEditCtrlEnabled ) editor->StartingKey(event);
6461 }
6462 else
6463 {
6464 event.Skip();
6465 }
6466
6467 editor->DecRef();
6468 attr->DecRef();
6469 }
6470 else
6471 {
6472 // let others process char events with modifiers or all
6473 // char events for readonly cells
6474 event.Skip();
6475 }
6476 break;
6477 }
6478 }
6479
6480 m_inOnKeyDown = false;
6481 }
6482
6483 void wxGrid::OnKeyUp( wxKeyEvent& event )
6484 {
6485 // try local handlers
6486 //
6487 if ( event.GetKeyCode() == WXK_SHIFT )
6488 {
6489 if ( m_selectingTopLeft != wxGridNoCellCoords &&
6490 m_selectingBottomRight != wxGridNoCellCoords )
6491 {
6492 if ( m_selection )
6493 {
6494 m_selection->SelectBlock( m_selectingTopLeft.GetRow(),
6495 m_selectingTopLeft.GetCol(),
6496 m_selectingBottomRight.GetRow(),
6497 m_selectingBottomRight.GetCol(),
6498 event.ControlDown(),
6499 true,
6500 event.AltDown(),
6501 event.MetaDown() );
6502 }
6503 }
6504
6505 m_selectingTopLeft = wxGridNoCellCoords;
6506 m_selectingBottomRight = wxGridNoCellCoords;
6507 m_selectingKeyboard = wxGridNoCellCoords;
6508 }
6509 }
6510
6511 void wxGrid::OnEraseBackground(wxEraseEvent&)
6512 {
6513 }
6514
6515 void wxGrid::SetCurrentCell( const wxGridCellCoords& coords )
6516 {
6517 if ( SendEvent( wxEVT_GRID_SELECT_CELL, coords.GetRow(), coords.GetCol() ) )
6518 {
6519 // the event has been intercepted - do nothing
6520 return;
6521 }
6522
6523 wxClientDC dc(m_gridWin);
6524 PrepareDC(dc);
6525
6526 if ( m_currentCellCoords != wxGridNoCellCoords )
6527 {
6528 DisableCellEditControl();
6529
6530 if ( IsVisible( m_currentCellCoords, false ) )
6531 {
6532 wxRect r;
6533 r = BlockToDeviceRect(m_currentCellCoords, m_currentCellCoords);
6534 if ( !m_gridLinesEnabled )
6535 {
6536 r.x--;
6537 r.y--;
6538 r.width++;
6539 r.height++;
6540 }
6541
6542 wxGridCellCoordsArray cells = CalcCellsExposed( r );
6543
6544 // Otherwise refresh redraws the highlight!
6545 m_currentCellCoords = coords;
6546
6547 DrawGridCellArea(dc,cells);
6548 DrawAllGridLines( dc, r );
6549 }
6550 }
6551
6552 m_currentCellCoords = coords;
6553
6554 wxGridCellAttr* attr = GetCellAttr(coords);
6555 DrawCellHighlight(dc, attr);
6556 attr->DecRef();
6557 }
6558
6559
6560 void wxGrid::HighlightBlock( int topRow, int leftCol, int bottomRow, int rightCol )
6561 {
6562 int temp;
6563 wxGridCellCoords updateTopLeft, updateBottomRight;
6564
6565 if ( m_selection )
6566 {
6567 if ( m_selection->GetSelectionMode() == wxGrid::wxGridSelectRows )
6568 {
6569 leftCol = 0;
6570 rightCol = GetNumberCols() - 1;
6571 }
6572 else if ( m_selection->GetSelectionMode() == wxGrid::wxGridSelectColumns )
6573 {
6574 topRow = 0;
6575 bottomRow = GetNumberRows() - 1;
6576 }
6577 }
6578
6579 if ( topRow > bottomRow )
6580 {
6581 temp = topRow;
6582 topRow = bottomRow;
6583 bottomRow = temp;
6584 }
6585
6586 if ( leftCol > rightCol )
6587 {
6588 temp = leftCol;
6589 leftCol = rightCol;
6590 rightCol = temp;
6591 }
6592
6593 updateTopLeft = wxGridCellCoords( topRow, leftCol );
6594 updateBottomRight = wxGridCellCoords( bottomRow, rightCol );
6595
6596 // First the case that we selected a completely new area
6597 if ( m_selectingTopLeft == wxGridNoCellCoords ||
6598 m_selectingBottomRight == wxGridNoCellCoords )
6599 {
6600 wxRect rect;
6601 rect = BlockToDeviceRect( wxGridCellCoords ( topRow, leftCol ),
6602 wxGridCellCoords ( bottomRow, rightCol ) );
6603 m_gridWin->Refresh( false, &rect );
6604 }
6605 // Now handle changing an existing selection area.
6606 else if ( m_selectingTopLeft != updateTopLeft ||
6607 m_selectingBottomRight != updateBottomRight )
6608 {
6609 // Compute two optimal update rectangles:
6610 // Either one rectangle is a real subset of the
6611 // other, or they are (almost) disjoint!
6612 wxRect rect[4];
6613 bool need_refresh[4];
6614 need_refresh[0] =
6615 need_refresh[1] =
6616 need_refresh[2] =
6617 need_refresh[3] = false;
6618 int i;
6619
6620 // Store intermediate values
6621 wxCoord oldLeft = m_selectingTopLeft.GetCol();
6622 wxCoord oldTop = m_selectingTopLeft.GetRow();
6623 wxCoord oldRight = m_selectingBottomRight.GetCol();
6624 wxCoord oldBottom = m_selectingBottomRight.GetRow();
6625
6626 // Determine the outer/inner coordinates.
6627 if (oldLeft > leftCol)
6628 {
6629 temp = oldLeft;
6630 oldLeft = leftCol;
6631 leftCol = temp;
6632 }
6633 if (oldTop > topRow )
6634 {
6635 temp = oldTop;
6636 oldTop = topRow;
6637 topRow = temp;
6638 }
6639 if (oldRight < rightCol )
6640 {
6641 temp = oldRight;
6642 oldRight = rightCol;
6643 rightCol = temp;
6644 }
6645 if (oldBottom < bottomRow)
6646 {
6647 temp = oldBottom;
6648 oldBottom = bottomRow;
6649 bottomRow = temp;
6650 }
6651
6652 // Now, either the stuff marked old is the outer
6653 // rectangle or we don't have a situation where one
6654 // is contained in the other.
6655
6656 if ( oldLeft < leftCol )
6657 {
6658 // Refresh the newly selected or deselected
6659 // area to the left of the old or new selection.
6660 need_refresh[0] = true;
6661 rect[0] = BlockToDeviceRect( wxGridCellCoords ( oldTop,
6662 oldLeft ),
6663 wxGridCellCoords ( oldBottom,
6664 leftCol - 1 ) );
6665 }
6666
6667 if ( oldTop < topRow )
6668 {
6669 // Refresh the newly selected or deselected
6670 // area above the old or new selection.
6671 need_refresh[1] = true;
6672 rect[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop,
6673 leftCol ),
6674 wxGridCellCoords ( topRow - 1,
6675 rightCol ) );
6676 }
6677
6678 if ( oldRight > rightCol )
6679 {
6680 // Refresh the newly selected or deselected
6681 // area to the right of the old or new selection.
6682 need_refresh[2] = true;
6683 rect[2] = BlockToDeviceRect( wxGridCellCoords ( oldTop,
6684 rightCol + 1 ),
6685 wxGridCellCoords ( oldBottom,
6686 oldRight ) );
6687 }
6688
6689 if ( oldBottom > bottomRow )
6690 {
6691 // Refresh the newly selected or deselected
6692 // area below the old or new selection.
6693 need_refresh[3] = true;
6694 rect[3] = BlockToDeviceRect( wxGridCellCoords ( bottomRow + 1,
6695 leftCol ),
6696 wxGridCellCoords ( oldBottom,
6697 rightCol ) );
6698 }
6699
6700 // various Refresh() calls
6701 for (i = 0; i < 4; i++ )
6702 if ( need_refresh[i] && rect[i] != wxGridNoCellRect )
6703 m_gridWin->Refresh( false, &(rect[i]) );
6704 }
6705 // Change Selection
6706 m_selectingTopLeft = updateTopLeft;
6707 m_selectingBottomRight = updateBottomRight;
6708 }
6709
6710 //
6711 // ------ functions to get/send data (see also public functions)
6712 //
6713
6714 bool wxGrid::GetModelValues()
6715 {
6716 // Hide the editor, so it won't hide a changed value.
6717 HideCellEditControl();
6718
6719 if ( m_table )
6720 {
6721 // all we need to do is repaint the grid
6722 //
6723 m_gridWin->Refresh();
6724 return true;
6725 }
6726
6727 return false;
6728 }
6729
6730
6731 bool wxGrid::SetModelValues()
6732 {
6733 int row, col;
6734
6735 // Disable the editor, so it won't hide a changed value.
6736 // Do we also want to save the current value of the editor first?
6737 // I think so ...
6738 DisableCellEditControl();
6739
6740 if ( m_table )
6741 {
6742 for ( row = 0; row < m_numRows; row++ )
6743 {
6744 for ( col = 0; col < m_numCols; col++ )
6745 {
6746 m_table->SetValue( row, col, GetCellValue(row, col) );
6747 }
6748 }
6749
6750 return true;
6751 }
6752
6753 return false;
6754 }
6755
6756
6757
6758 // Note - this function only draws cells that are in the list of
6759 // exposed cells (usually set from the update region by
6760 // CalcExposedCells)
6761 //
6762 void wxGrid::DrawGridCellArea( wxDC& dc, const wxGridCellCoordsArray& cells )
6763 {
6764 if ( !m_numRows || !m_numCols ) return;
6765
6766 int i, numCells = cells.GetCount();
6767 int row, col, cell_rows, cell_cols;
6768 wxGridCellCoordsArray redrawCells;
6769
6770 for ( i = numCells-1; i >= 0; i-- )
6771 {
6772 row = cells[i].GetRow();
6773 col = cells[i].GetCol();
6774 GetCellSize( row, col, &cell_rows, &cell_cols );
6775
6776 // If this cell is part of a multicell block, find owner for repaint
6777 if ( cell_rows <= 0 || cell_cols <= 0 )
6778 {
6779 wxGridCellCoords cell(row+cell_rows, col+cell_cols);
6780 bool marked = false;
6781 for ( int j = 0; j < numCells; j++ )
6782 {
6783 if ( cell == cells[j] )
6784 {
6785 marked = true;
6786 break;
6787 }
6788 }
6789 if (!marked)
6790 {
6791 int count = redrawCells.GetCount();
6792 for (int j = 0; j < count; j++)
6793 {
6794 if ( cell == redrawCells[j] )
6795 {
6796 marked = true;
6797 break;
6798 }
6799 }
6800 if (!marked) redrawCells.Add( cell );
6801 }
6802 continue; // don't bother drawing this cell
6803 }
6804
6805 // If this cell is empty, find cell to left that might want to overflow
6806 if (m_table && m_table->IsEmptyCell(row, col))
6807 {
6808 for ( int l = 0; l < cell_rows; l++ )
6809 {
6810 // find a cell in this row to left alreay marked for repaint
6811 int left = col;
6812 for (int k = 0; k < int(redrawCells.GetCount()); k++)
6813 if ((redrawCells[k].GetCol() < left) &&
6814 (redrawCells[k].GetRow() == row))
6815 left=redrawCells[k].GetCol();
6816
6817 if (left == col) left = 0; // oh well
6818
6819 for (int j = col-1; j >= left; j--)
6820 {
6821 if (!m_table->IsEmptyCell(row+l, j))
6822 {
6823 if (GetCellOverflow(row+l, j))
6824 {
6825 wxGridCellCoords cell(row+l, j);
6826 bool marked = false;
6827
6828 for (int k = 0; k < numCells; k++)
6829 {
6830 if ( cell == cells[k] )
6831 {
6832 marked = true;
6833 break;
6834 }
6835 }
6836 if (!marked)
6837 {
6838 int count = redrawCells.GetCount();
6839 for (int k = 0; k < count; k++)
6840 {
6841 if ( cell == redrawCells[k] )
6842 {
6843 marked = true;
6844 break;
6845 }
6846 }
6847 if (!marked) redrawCells.Add( cell );
6848 }
6849 }
6850 break;
6851 }
6852 }
6853 }
6854 }
6855 DrawCell( dc, cells[i] );
6856 }
6857
6858 numCells = redrawCells.GetCount();
6859
6860 for ( i = numCells - 1; i >= 0; i-- )
6861 {
6862 DrawCell( dc, redrawCells[i] );
6863 }
6864 }
6865
6866
6867 void wxGrid::DrawGridSpace( wxDC& dc )
6868 {
6869 int cw, ch;
6870 m_gridWin->GetClientSize( &cw, &ch );
6871
6872 int right, bottom;
6873 CalcUnscrolledPosition( cw, ch, &right, &bottom );
6874
6875 int rightCol = m_numCols > 0 ? GetColRight(m_numCols - 1) : 0;
6876 int bottomRow = m_numRows > 0 ? GetRowBottom(m_numRows - 1) : 0 ;
6877
6878 if ( right > rightCol || bottom > bottomRow )
6879 {
6880 int left, top;
6881 CalcUnscrolledPosition( 0, 0, &left, &top );
6882
6883 dc.SetBrush( wxBrush(GetDefaultCellBackgroundColour(), wxSOLID) );
6884 dc.SetPen( *wxTRANSPARENT_PEN );
6885
6886 if ( right > rightCol )
6887 {
6888 dc.DrawRectangle( rightCol, top, right - rightCol, ch);
6889 }
6890
6891 if ( bottom > bottomRow )
6892 {
6893 dc.DrawRectangle( left, bottomRow, cw, bottom - bottomRow);
6894 }
6895 }
6896 }
6897
6898
6899 void wxGrid::DrawCell( wxDC& dc, const wxGridCellCoords& coords )
6900 {
6901 int row = coords.GetRow();
6902 int col = coords.GetCol();
6903
6904 if ( GetColWidth(col) <= 0 || GetRowHeight(row) <= 0 )
6905 return;
6906
6907 // we draw the cell border ourselves
6908 #if !WXGRID_DRAW_LINES
6909 if ( m_gridLinesEnabled )
6910 DrawCellBorder( dc, coords );
6911 #endif
6912
6913 wxGridCellAttr* attr = GetCellAttr(row, col);
6914
6915 bool isCurrent = coords == m_currentCellCoords;
6916
6917 wxRect rect = CellToRect( row, col );
6918
6919 // if the editor is shown, we should use it and not the renderer
6920 // Note: However, only if it is really _shown_, i.e. not hidden!
6921 if ( isCurrent && IsCellEditControlShown() )
6922 {
6923 wxGridCellEditor *editor = attr->GetEditor(this, row, col);
6924 editor->PaintBackground(rect, attr);
6925 editor->DecRef();
6926 }
6927 else
6928 {
6929 // but all the rest is drawn by the cell renderer and hence may be
6930 // customized
6931 wxGridCellRenderer *renderer = attr->GetRenderer(this, row, col);
6932 renderer->Draw(*this, *attr, dc, rect, row, col, IsInSelection(coords));
6933 renderer->DecRef();
6934 }
6935
6936 attr->DecRef();
6937 }
6938
6939 void wxGrid::DrawCellHighlight( wxDC& dc, const wxGridCellAttr *attr )
6940 {
6941 int row = m_currentCellCoords.GetRow();
6942 int col = m_currentCellCoords.GetCol();
6943
6944 if ( GetColWidth(col) <= 0 || GetRowHeight(row) <= 0 )
6945 return;
6946
6947 wxRect rect = CellToRect(row, col);
6948
6949 // hmmm... what could we do here to show that the cell is disabled?
6950 // for now, I just draw a thinner border than for the other ones, but
6951 // it doesn't look really good
6952
6953 int penWidth = attr->IsReadOnly() ? m_cellHighlightROPenWidth : m_cellHighlightPenWidth;
6954
6955 if (penWidth > 0)
6956 {
6957 // The center of th drawn line is where the position/width/height of
6958 // the rectangle is actually at, (on wxMSW atr least,) so we will
6959 // reduce the size of the rectangle to compensate for the thickness of
6960 // the line. If this is too strange on non wxMSW platforms then
6961 // please #ifdef this appropriately.
6962 rect.x += penWidth/2;
6963 rect.y += penWidth/2;
6964 rect.width -= penWidth-1;
6965 rect.height -= penWidth-1;
6966
6967
6968 // Now draw the rectangle
6969 // use the cellHighlightColour if the cell is inside a selection, this
6970 // will ensure the cell is always visible.
6971 dc.SetPen(wxPen(IsInSelection(row,col)?m_selectionForeground:m_cellHighlightColour, penWidth, wxSOLID));
6972 dc.SetBrush(*wxTRANSPARENT_BRUSH);
6973 dc.DrawRectangle(rect);
6974 }
6975
6976 #if 0
6977 // VZ: my experiments with 3d borders...
6978
6979 // how to properly set colours for arbitrary bg?
6980 wxCoord x1 = rect.x,
6981 y1 = rect.y,
6982 x2 = rect.x + rect.width -1,
6983 y2 = rect.y + rect.height -1;
6984
6985 dc.SetPen(*wxWHITE_PEN);
6986 dc.DrawLine(x1, y1, x2, y1);
6987 dc.DrawLine(x1, y1, x1, y2);
6988
6989 dc.DrawLine(x1 + 1, y2 - 1, x2 - 1, y2 - 1);
6990 dc.DrawLine(x2 - 1, y1 + 1, x2 - 1, y2 );
6991
6992 dc.SetPen(*wxBLACK_PEN);
6993 dc.DrawLine(x1, y2, x2, y2);
6994 dc.DrawLine(x2, y1, x2, y2+1);
6995 #endif // 0
6996 }
6997
6998
6999 void wxGrid::DrawCellBorder( wxDC& dc, const wxGridCellCoords& coords )
7000 {
7001 int row = coords.GetRow();
7002 int col = coords.GetCol();
7003 if ( GetColWidth(col) <= 0 || GetRowHeight(row) <= 0 )
7004 return;
7005
7006 dc.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID) );
7007
7008 wxRect rect = CellToRect( row, col );
7009
7010 // right hand border
7011 //
7012 dc.DrawLine( rect.x + rect.width, rect.y,
7013 rect.x + rect.width, rect.y + rect.height + 1 );
7014
7015 // bottom border
7016 //
7017 dc.DrawLine( rect.x, rect.y + rect.height,
7018 rect.x + rect.width, rect.y + rect.height);
7019 }
7020
7021 void wxGrid::DrawHighlight(wxDC& dc,const wxGridCellCoordsArray& cells)
7022 {
7023 // This if block was previously in wxGrid::OnPaint but that doesn't
7024 // seem to get called under wxGTK - MB
7025 //
7026 if ( m_currentCellCoords == wxGridNoCellCoords &&
7027 m_numRows && m_numCols )
7028 {
7029 m_currentCellCoords.Set(0, 0);
7030 }
7031
7032 if ( IsCellEditControlShown() )
7033 {
7034 // don't show highlight when the edit control is shown
7035 return;
7036 }
7037
7038 // if the active cell was repainted, repaint its highlight too because it
7039 // might have been damaged by the grid lines
7040 size_t count = cells.GetCount();
7041 for ( size_t n = 0; n < count; n++ )
7042 {
7043 if ( cells[n] == m_currentCellCoords )
7044 {
7045 wxGridCellAttr* attr = GetCellAttr(m_currentCellCoords);
7046 DrawCellHighlight(dc, attr);
7047 attr->DecRef();
7048
7049 break;
7050 }
7051 }
7052 }
7053
7054 // TODO: remove this ???
7055 // This is used to redraw all grid lines e.g. when the grid line colour
7056 // has been changed
7057 //
7058 void wxGrid::DrawAllGridLines( wxDC& dc, const wxRegion & WXUNUSED(reg) )
7059 {
7060 #if !WXGRID_DRAW_LINES
7061 return;
7062 #endif
7063
7064 if ( !m_gridLinesEnabled ||
7065 !m_numRows ||
7066 !m_numCols ) return;
7067
7068 int top, bottom, left, right;
7069
7070 #if 0 //#ifndef __WXGTK__
7071 if (reg.IsEmpty())
7072 {
7073 int cw, ch;
7074 m_gridWin->GetClientSize(&cw, &ch);
7075
7076 // virtual coords of visible area
7077 //
7078 CalcUnscrolledPosition( 0, 0, &left, &top );
7079 CalcUnscrolledPosition( cw, ch, &right, &bottom );
7080 }
7081 else
7082 {
7083 wxCoord x, y, w, h;
7084 reg.GetBox(x, y, w, h);
7085 CalcUnscrolledPosition( x, y, &left, &top );
7086 CalcUnscrolledPosition( x + w, y + h, &right, &bottom );
7087 }
7088 #else
7089 int cw, ch;
7090 m_gridWin->GetClientSize(&cw, &ch);
7091 CalcUnscrolledPosition( 0, 0, &left, &top );
7092 CalcUnscrolledPosition( cw, ch, &right, &bottom );
7093 #endif
7094
7095 // avoid drawing grid lines past the last row and col
7096 //
7097 right = wxMin( right, GetColRight(m_numCols - 1) );
7098 bottom = wxMin( bottom, GetRowBottom(m_numRows - 1) );
7099
7100 // no gridlines inside multicells, clip them out
7101 int leftCol = internalXToCol(left);
7102 int topRow = internalYToRow(top);
7103 int rightCol = internalXToCol(right);
7104 int bottomRow = internalYToRow(bottom);
7105 wxRegion clippedcells(0, 0, cw, ch);
7106
7107
7108 int i, j, cell_rows, cell_cols;
7109 wxRect rect;
7110
7111 for (j=topRow; j<bottomRow; j++)
7112 {
7113 for (i=leftCol; i<rightCol; i++)
7114 {
7115 GetCellSize( j, i, &cell_rows, &cell_cols );
7116 if ((cell_rows > 1) || (cell_cols > 1))
7117 {
7118 rect = CellToRect(j,i);
7119 CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
7120 clippedcells.Subtract(rect);
7121 }
7122 else if ((cell_rows < 0) || (cell_cols < 0))
7123 {
7124 rect = CellToRect(j+cell_rows, i+cell_cols);
7125 CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
7126 clippedcells.Subtract(rect);
7127 }
7128 }
7129 }
7130 dc.SetClippingRegion( clippedcells );
7131
7132 dc.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID) );
7133
7134 // horizontal grid lines
7135 //
7136 // already declared above - int i;
7137 for ( i = internalYToRow(top); i < m_numRows; i++ )
7138 {
7139 int bot = GetRowBottom(i) - 1;
7140
7141 if ( bot > bottom )
7142 {
7143 break;
7144 }
7145
7146 if ( bot >= top )
7147 {
7148 dc.DrawLine( left, bot, right, bot );
7149 }
7150 }
7151
7152
7153 // vertical grid lines
7154 //
7155 for ( i = internalXToCol(left); i < m_numCols; i++ )
7156 {
7157 int colRight = GetColRight(i) - 1;
7158 if ( colRight > right )
7159 {
7160 break;
7161 }
7162
7163 if ( colRight >= left )
7164 {
7165 dc.DrawLine( colRight, top, colRight, bottom );
7166 }
7167 }
7168 dc.DestroyClippingRegion();
7169 }
7170
7171
7172 void wxGrid::DrawRowLabels( wxDC& dc ,const wxArrayInt& rows)
7173 {
7174 if ( !m_numRows ) return;
7175
7176 size_t i;
7177 size_t numLabels = rows.GetCount();
7178
7179 for ( i = 0; i < numLabels; i++ )
7180 {
7181 DrawRowLabel( dc, rows[i] );
7182 }
7183 }
7184
7185
7186 void wxGrid::DrawRowLabel( wxDC& dc, int row )
7187 {
7188 if ( GetRowHeight(row) <= 0 )
7189 return;
7190
7191 int rowTop = GetRowTop(row),
7192 rowBottom = GetRowBottom(row) - 1;
7193
7194 dc.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DDKSHADOW),1, wxSOLID) );
7195 dc.DrawLine( m_rowLabelWidth-1, rowTop,
7196 m_rowLabelWidth-1, rowBottom );
7197
7198 dc.DrawLine( 0, rowTop, 0, rowBottom );
7199
7200 dc.DrawLine( 0, rowBottom, m_rowLabelWidth, rowBottom );
7201
7202 dc.SetPen( *wxWHITE_PEN );
7203 dc.DrawLine( 1, rowTop, 1, rowBottom );
7204 dc.DrawLine( 1, rowTop, m_rowLabelWidth-1, rowTop );
7205
7206 dc.SetBackgroundMode( wxTRANSPARENT );
7207 dc.SetTextForeground( GetLabelTextColour() );
7208 dc.SetFont( GetLabelFont() );
7209
7210 int hAlign, vAlign;
7211 GetRowLabelAlignment( &hAlign, &vAlign );
7212
7213 wxRect rect;
7214 rect.SetX( 2 );
7215 rect.SetY( GetRowTop(row) + 2 );
7216 rect.SetWidth( m_rowLabelWidth - 4 );
7217 rect.SetHeight( GetRowHeight(row) - 4 );
7218 DrawTextRectangle( dc, GetRowLabelValue( row ), rect, hAlign, vAlign );
7219 }
7220
7221
7222 void wxGrid::DrawColLabels( wxDC& dc,const wxArrayInt& cols )
7223 {
7224 if ( !m_numCols ) return;
7225
7226 size_t i;
7227 size_t numLabels = cols.GetCount();
7228
7229 for ( i = 0; i < numLabels; i++ )
7230 {
7231 DrawColLabel( dc, cols[i] );
7232 }
7233 }
7234
7235
7236 void wxGrid::DrawColLabel( wxDC& dc, int col )
7237 {
7238 if ( GetColWidth(col) <= 0 )
7239 return;
7240
7241 int colLeft = GetColLeft(col),
7242 colRight = GetColRight(col) - 1;
7243
7244 dc.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DDKSHADOW),1, wxSOLID) );
7245 dc.DrawLine( colRight, 0,
7246 colRight, m_colLabelHeight-1 );
7247
7248 dc.DrawLine( colLeft, 0, colRight, 0 );
7249
7250 dc.DrawLine( colLeft, m_colLabelHeight-1,
7251 colRight+1, m_colLabelHeight-1 );
7252
7253 dc.SetPen( *wxWHITE_PEN );
7254 dc.DrawLine( colLeft, 1, colLeft, m_colLabelHeight-1 );
7255 dc.DrawLine( colLeft, 1, colRight, 1 );
7256
7257 dc.SetBackgroundMode( wxTRANSPARENT );
7258 dc.SetTextForeground( GetLabelTextColour() );
7259 dc.SetFont( GetLabelFont() );
7260
7261 int hAlign, vAlign, orient;
7262 GetColLabelAlignment( &hAlign, &vAlign );
7263 orient = GetColLabelTextOrientation();
7264
7265 wxRect rect;
7266 rect.SetX( colLeft + 2 );
7267 rect.SetY( 2 );
7268 rect.SetWidth( GetColWidth(col) - 4 );
7269 rect.SetHeight( m_colLabelHeight - 4 );
7270 DrawTextRectangle( dc, GetColLabelValue( col ), rect, hAlign, vAlign, orient );
7271 }
7272
7273 void wxGrid::DrawTextRectangle( wxDC& dc,
7274 const wxString& value,
7275 const wxRect& rect,
7276 int horizAlign,
7277 int vertAlign,
7278 int textOrientation )
7279 {
7280 wxArrayString lines;
7281
7282 StringToLines( value, lines );
7283
7284
7285 //Forward to new API.
7286 DrawTextRectangle( dc,
7287 lines,
7288 rect,
7289 horizAlign,
7290 vertAlign,
7291 textOrientation );
7292
7293 }
7294
7295 void wxGrid::DrawTextRectangle( wxDC& dc,
7296 const wxArrayString& lines,
7297 const wxRect& rect,
7298 int horizAlign,
7299 int vertAlign,
7300 int textOrientation )
7301 {
7302 long textWidth, textHeight;
7303 long lineWidth, lineHeight;
7304 int nLines;
7305
7306 dc.SetClippingRegion( rect );
7307
7308 nLines = lines.GetCount();
7309 if( nLines > 0 )
7310 {
7311 int l;
7312 float x = 0.0, y = 0.0;
7313
7314 if( textOrientation == wxHORIZONTAL )
7315 GetTextBoxSize(dc, lines, &textWidth, &textHeight);
7316 else
7317 GetTextBoxSize( dc, lines, &textHeight, &textWidth );
7318
7319 switch( vertAlign )
7320 {
7321 case wxALIGN_BOTTOM:
7322 if( textOrientation == wxHORIZONTAL )
7323 y = rect.y + (rect.height - textHeight - 1);
7324 else
7325 x = rect.x + rect.width - textWidth;
7326 break;
7327
7328 case wxALIGN_CENTRE:
7329 if( textOrientation == wxHORIZONTAL )
7330 y = rect.y + ((rect.height - textHeight)/2);
7331 else
7332 x = rect.x + ((rect.width - textWidth)/2);
7333 break;
7334
7335 case wxALIGN_TOP:
7336 default:
7337 if( textOrientation == wxHORIZONTAL )
7338 y = rect.y + 1;
7339 else
7340 x = rect.x + 1;
7341 break;
7342 }
7343
7344 // Align each line of a multi-line label
7345 for( l = 0; l < nLines; l++ )
7346 {
7347 dc.GetTextExtent(lines[l], &lineWidth, &lineHeight);
7348
7349 switch( horizAlign )
7350 {
7351 case wxALIGN_RIGHT:
7352 if( textOrientation == wxHORIZONTAL )
7353 x = rect.x + (rect.width - lineWidth - 1);
7354 else
7355 y = rect.y + lineWidth + 1;
7356 break;
7357
7358 case wxALIGN_CENTRE:
7359 if( textOrientation == wxHORIZONTAL )
7360 x = rect.x + ((rect.width - lineWidth)/2);
7361 else
7362 y = rect.y + rect.height - ((rect.height - lineWidth)/2);
7363 break;
7364
7365 case wxALIGN_LEFT:
7366 default:
7367 if( textOrientation == wxHORIZONTAL )
7368 x = rect.x + 1;
7369 else
7370 y = rect.y + rect.height - 1;
7371 break;
7372 }
7373
7374 if( textOrientation == wxHORIZONTAL )
7375 {
7376 dc.DrawText( lines[l], (int)x, (int)y );
7377 y += lineHeight;
7378 }
7379 else
7380 {
7381 dc.DrawRotatedText( lines[l], (int)x, (int)y, 90.0 );
7382 x += lineHeight;
7383 }
7384 }
7385 }
7386 dc.DestroyClippingRegion();
7387 }
7388
7389
7390 // Split multi line text up into an array of strings. Any existing
7391 // contents of the string array are preserved.
7392 //
7393 void wxGrid::StringToLines( const wxString& value, wxArrayString& lines )
7394 {
7395 int startPos = 0;
7396 int pos;
7397 wxString eol = wxTextFile::GetEOL( wxTextFileType_Unix );
7398 wxString tVal = wxTextFile::Translate( value, wxTextFileType_Unix );
7399
7400 while ( startPos < (int)tVal.Length() )
7401 {
7402 pos = tVal.Mid(startPos).Find( eol );
7403 if ( pos < 0 )
7404 {
7405 break;
7406 }
7407 else if ( pos == 0 )
7408 {
7409 lines.Add( wxEmptyString );
7410 }
7411 else
7412 {
7413 lines.Add( value.Mid(startPos, pos) );
7414 }
7415 startPos += pos+1;
7416 }
7417 if ( startPos < (int)value.Length() )
7418 {
7419 lines.Add( value.Mid( startPos ) );
7420 }
7421 }
7422
7423
7424 void wxGrid::GetTextBoxSize( wxDC& dc,
7425 const wxArrayString& lines,
7426 long *width, long *height )
7427 {
7428 long w = 0;
7429 long h = 0;
7430 long lineW, lineH;
7431
7432 size_t i;
7433 for ( i = 0; i < lines.GetCount(); i++ )
7434 {
7435 dc.GetTextExtent( lines[i], &lineW, &lineH );
7436 w = wxMax( w, lineW );
7437 h += lineH;
7438 }
7439
7440 *width = w;
7441 *height = h;
7442 }
7443
7444 //
7445 // ------ Batch processing.
7446 //
7447 void wxGrid::EndBatch()
7448 {
7449 if ( m_batchCount > 0 )
7450 {
7451 m_batchCount--;
7452 if ( !m_batchCount )
7453 {
7454 CalcDimensions();
7455 m_rowLabelWin->Refresh();
7456 m_colLabelWin->Refresh();
7457 m_cornerLabelWin->Refresh();
7458 m_gridWin->Refresh();
7459 }
7460 }
7461 }
7462
7463 // Use this, rather than wxWindow::Refresh(), to force an immediate
7464 // repainting of the grid. Has no effect if you are already inside a
7465 // BeginBatch / EndBatch block.
7466 //
7467 void wxGrid::ForceRefresh()
7468 {
7469 BeginBatch();
7470 EndBatch();
7471 }
7472
7473
7474 //
7475 // ------ Edit control functions
7476 //
7477
7478
7479 void wxGrid::EnableEditing( bool edit )
7480 {
7481 // TODO: improve this ?
7482 //
7483 if ( edit != m_editable )
7484 {
7485 if(!edit) EnableCellEditControl(edit);
7486 m_editable = edit;
7487 }
7488 }
7489
7490
7491 void wxGrid::EnableCellEditControl( bool enable )
7492 {
7493 if (! m_editable)
7494 return;
7495
7496 if ( m_currentCellCoords == wxGridNoCellCoords )
7497 SetCurrentCell( 0, 0 );
7498
7499 if ( enable != m_cellEditCtrlEnabled )
7500 {
7501 if ( enable )
7502 {
7503 if (SendEvent( wxEVT_GRID_EDITOR_SHOWN) <0)
7504 return;
7505
7506 // this should be checked by the caller!
7507 wxASSERT_MSG( CanEnableCellControl(),
7508 _T("can't enable editing for this cell!") );
7509
7510 // do it before ShowCellEditControl()
7511 m_cellEditCtrlEnabled = enable;
7512
7513 ShowCellEditControl();
7514 }
7515 else
7516 {
7517 //FIXME:add veto support
7518 SendEvent( wxEVT_GRID_EDITOR_HIDDEN);
7519
7520 HideCellEditControl();
7521 SaveEditControlValue();
7522
7523 // do it after HideCellEditControl()
7524 m_cellEditCtrlEnabled = enable;
7525 }
7526 }
7527 }
7528
7529 bool wxGrid::IsCurrentCellReadOnly() const
7530 {
7531 // const_cast
7532 wxGridCellAttr* attr = ((wxGrid *)this)->GetCellAttr(m_currentCellCoords);
7533 bool readonly = attr->IsReadOnly();
7534 attr->DecRef();
7535
7536 return readonly;
7537 }
7538
7539 bool wxGrid::CanEnableCellControl() const
7540 {
7541 return m_editable && (m_currentCellCoords != wxGridNoCellCoords) &&
7542 !IsCurrentCellReadOnly();
7543
7544 }
7545
7546 bool wxGrid::IsCellEditControlEnabled() const
7547 {
7548 // the cell edit control might be disable for all cells or just for the
7549 // current one if it's read only
7550 return m_cellEditCtrlEnabled ? !IsCurrentCellReadOnly() : false;
7551 }
7552
7553 bool wxGrid::IsCellEditControlShown() const
7554 {
7555 bool isShown = false;
7556
7557 if ( m_cellEditCtrlEnabled )
7558 {
7559 int row = m_currentCellCoords.GetRow();
7560 int col = m_currentCellCoords.GetCol();
7561 wxGridCellAttr* attr = GetCellAttr(row, col);
7562 wxGridCellEditor* editor = attr->GetEditor((wxGrid*) this, row, col);
7563 attr->DecRef();
7564
7565 if ( editor )
7566 {
7567 if ( editor->IsCreated() )
7568 {
7569 isShown = editor->GetControl()->IsShown();
7570 }
7571
7572 editor->DecRef();
7573 }
7574 }
7575
7576 return isShown;
7577 }
7578
7579 void wxGrid::ShowCellEditControl()
7580 {
7581 if ( IsCellEditControlEnabled() )
7582 {
7583 if ( !IsVisible( m_currentCellCoords ) )
7584 {
7585 m_cellEditCtrlEnabled = false;
7586 return;
7587 }
7588 else
7589 {
7590 wxRect rect = CellToRect( m_currentCellCoords );
7591 int row = m_currentCellCoords.GetRow();
7592 int col = m_currentCellCoords.GetCol();
7593
7594 // if this is part of a multicell, find owner (topleft)
7595 int cell_rows, cell_cols;
7596 GetCellSize( row, col, &cell_rows, &cell_cols );
7597 if ( cell_rows <= 0 || cell_cols <= 0 )
7598 {
7599 row += cell_rows;
7600 col += cell_cols;
7601 m_currentCellCoords.SetRow( row );
7602 m_currentCellCoords.SetCol( col );
7603 }
7604
7605 // convert to scrolled coords
7606 //
7607 CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
7608
7609 // done in PaintBackground()
7610 #if 0
7611 // erase the highlight and the cell contents because the editor
7612 // might not cover the entire cell
7613 wxClientDC dc( m_gridWin );
7614 PrepareDC( dc );
7615 dc.SetBrush(*wxLIGHT_GREY_BRUSH); //wxBrush(attr->GetBackgroundColour(), wxSOLID));
7616 dc.SetPen(*wxTRANSPARENT_PEN);
7617 dc.DrawRectangle(rect);
7618 #endif // 0
7619
7620 // cell is shifted by one pixel
7621 // However, don't allow x or y to become negative
7622 // since the SetSize() method interprets that as
7623 // "don't change."
7624 if (rect.x > 0)
7625 rect.x--;
7626 if (rect.y > 0)
7627 rect.y--;
7628
7629 wxGridCellAttr* attr = GetCellAttr(row, col);
7630 wxGridCellEditor* editor = attr->GetEditor(this, row, col);
7631 if ( !editor->IsCreated() )
7632 {
7633 editor->Create(m_gridWin, wxID_ANY,
7634 new wxGridCellEditorEvtHandler(this, editor));
7635
7636 wxGridEditorCreatedEvent evt(GetId(),
7637 wxEVT_GRID_EDITOR_CREATED,
7638 this,
7639 row,
7640 col,
7641 editor->GetControl());
7642 GetEventHandler()->ProcessEvent(evt);
7643 }
7644
7645
7646 // resize editor to overflow into righthand cells if allowed
7647 int maxWidth = rect.width;
7648 wxString value = GetCellValue(row, col);
7649 if ( (value != wxEmptyString) && (attr->GetOverflow()) )
7650 {
7651 int y;
7652 GetTextExtent(value, &maxWidth, &y,
7653 NULL, NULL, &attr->GetFont());
7654 if (maxWidth < rect.width) maxWidth = rect.width;
7655 }
7656 int client_right = m_gridWin->GetClientSize().GetWidth();
7657 if (rect.x+maxWidth > client_right)
7658 maxWidth = client_right - rect.x;
7659
7660 if ((maxWidth > rect.width) && (col < m_numCols) && m_table)
7661 {
7662 GetCellSize( row, col, &cell_rows, &cell_cols );
7663 // may have changed earlier
7664 for (int i = col+cell_cols; i < m_numCols; i++)
7665 {
7666 int c_rows, c_cols;
7667 GetCellSize( row, i, &c_rows, &c_cols );
7668 // looks weird going over a multicell
7669 if (m_table->IsEmptyCell(row,i) &&
7670 (rect.width < maxWidth) && (c_rows == 1))
7671 rect.width += GetColWidth(i);
7672 else
7673 break;
7674 }
7675 if (rect.GetRight() > client_right)
7676 rect.SetRight(client_right-1);
7677 }
7678
7679 editor->SetCellAttr(attr);
7680 editor->SetSize( rect );
7681 editor->Show( true, attr );
7682
7683 // recalc dimensions in case we need to
7684 // expand the scrolled window to account for editor
7685 CalcDimensions();
7686
7687 editor->BeginEdit(row, col, this);
7688 editor->SetCellAttr(NULL);
7689
7690 editor->DecRef();
7691 attr->DecRef();
7692 }
7693 }
7694 }
7695
7696
7697 void wxGrid::HideCellEditControl()
7698 {
7699 if ( IsCellEditControlEnabled() )
7700 {
7701 int row = m_currentCellCoords.GetRow();
7702 int col = m_currentCellCoords.GetCol();
7703
7704 wxGridCellAttr* attr = GetCellAttr(row, col);
7705 wxGridCellEditor *editor = attr->GetEditor(this, row, col);
7706 editor->Show( false );
7707 editor->DecRef();
7708 attr->DecRef();
7709 m_gridWin->SetFocus();
7710 // refresh whole row to the right
7711 wxRect rect( CellToRect(row, col) );
7712 CalcScrolledPosition(rect.x, rect.y, &rect.x, &rect.y );
7713 rect.width = m_gridWin->GetClientSize().GetWidth() - rect.x;
7714 m_gridWin->Refresh( false, &rect );
7715 }
7716 }
7717
7718
7719 void wxGrid::SaveEditControlValue()
7720 {
7721 if ( IsCellEditControlEnabled() )
7722 {
7723 int row = m_currentCellCoords.GetRow();
7724 int col = m_currentCellCoords.GetCol();
7725
7726 wxString oldval = GetCellValue(row,col);
7727
7728 wxGridCellAttr* attr = GetCellAttr(row, col);
7729 wxGridCellEditor* editor = attr->GetEditor(this, row, col);
7730 bool changed = editor->EndEdit(row, col, this);
7731
7732 editor->DecRef();
7733 attr->DecRef();
7734
7735 if (changed)
7736 {
7737 if ( SendEvent( wxEVT_GRID_CELL_CHANGE,
7738 m_currentCellCoords.GetRow(),
7739 m_currentCellCoords.GetCol() ) < 0 ) {
7740
7741 // Event has been vetoed, set the data back.
7742 SetCellValue(row,col,oldval);
7743 }
7744 }
7745 }
7746 }
7747
7748
7749 //
7750 // ------ Grid location functions
7751 // Note that all of these functions work with the logical coordinates of
7752 // grid cells and labels so you will need to convert from device
7753 // coordinates for mouse events etc.
7754 //
7755
7756 void wxGrid::XYToCell( int x, int y, wxGridCellCoords& coords )
7757 {
7758 int row = YToRow(y);
7759 int col = XToCol(x);
7760
7761 if ( row == -1 || col == -1 )
7762 {
7763 coords = wxGridNoCellCoords;
7764 }
7765 else
7766 {
7767 coords.Set( row, col );
7768 }
7769 }
7770
7771
7772 // Internal Helper function for computing row or column from some
7773 // (unscrolled) coordinate value, using either
7774 // m_defaultRowHeight/m_defaultColWidth or binary search on array
7775 // of m_rowBottoms/m_ColRights to speed up the search!
7776
7777 static int CoordToRowOrCol(int coord, int defaultDist, int minDist,
7778 const wxArrayInt& BorderArray, int nMax,
7779 bool clipToMinMax)
7780 {
7781
7782 if (coord < 0)
7783 return clipToMinMax && (nMax > 0) ? 0 : -1;
7784
7785
7786 if (!defaultDist)
7787 defaultDist = 1;
7788
7789 size_t i_max = coord / defaultDist,
7790 i_min = 0;
7791
7792 if (BorderArray.IsEmpty())
7793 {
7794 if((int) i_max < nMax)
7795 return i_max;
7796 return clipToMinMax ? nMax - 1 : -1;
7797 }
7798
7799 if ( i_max >= BorderArray.GetCount())
7800 i_max = BorderArray.GetCount() - 1;
7801 else
7802 {
7803 if ( coord >= BorderArray[i_max])
7804 {
7805 i_min = i_max;
7806 if (minDist)
7807 i_max = coord / minDist;
7808 else
7809 i_max = BorderArray.GetCount() - 1;
7810 }
7811 if ( i_max >= BorderArray.GetCount())
7812 i_max = BorderArray.GetCount() - 1;
7813 }
7814 if ( coord >= BorderArray[i_max])
7815 return clipToMinMax ? (int)i_max : -1;
7816 if ( coord < BorderArray[0] )
7817 return 0;
7818
7819 while ( i_max - i_min > 0 )
7820 {
7821 wxCHECK_MSG(BorderArray[i_min] <= coord && coord < BorderArray[i_max],
7822 0, _T("wxGrid: internal error in CoordToRowOrCol"));
7823 if (coord >= BorderArray[ i_max - 1])
7824 return i_max;
7825 else
7826 i_max--;
7827 int median = i_min + (i_max - i_min + 1) / 2;
7828 if (coord < BorderArray[median])
7829 i_max = median;
7830 else
7831 i_min = median;
7832 }
7833 return i_max;
7834 }
7835
7836 int wxGrid::YToRow( int y )
7837 {
7838 return CoordToRowOrCol(y, m_defaultRowHeight,
7839 m_minAcceptableRowHeight, m_rowBottoms, m_numRows, false);
7840 }
7841
7842
7843 int wxGrid::XToCol( int x )
7844 {
7845 return CoordToRowOrCol(x, m_defaultColWidth,
7846 m_minAcceptableColWidth, m_colRights, m_numCols, false);
7847 }
7848
7849
7850 // return the row number that that the y coord is near the edge of, or
7851 // -1 if not near an edge
7852 //
7853 int wxGrid::YToEdgeOfRow( int y )
7854 {
7855 int i;
7856 i = internalYToRow(y);
7857
7858 if ( GetRowHeight(i) > WXGRID_LABEL_EDGE_ZONE )
7859 {
7860 // We know that we are in row i, test whether we are
7861 // close enough to lower or upper border, respectively.
7862 if ( abs(GetRowBottom(i) - y) < WXGRID_LABEL_EDGE_ZONE )
7863 return i;
7864 else if( i > 0 && y - GetRowTop(i) < WXGRID_LABEL_EDGE_ZONE )
7865 return i - 1;
7866 }
7867
7868 return -1;
7869 }
7870
7871
7872 // return the col number that that the x coord is near the edge of, or
7873 // -1 if not near an edge
7874 //
7875 int wxGrid::XToEdgeOfCol( int x )
7876 {
7877 int i;
7878 i = internalXToCol(x);
7879
7880 if ( GetColWidth(i) > WXGRID_LABEL_EDGE_ZONE )
7881 {
7882 // We know that we are in column i, test whether we are
7883 // close enough to right or left border, respectively.
7884 if ( abs(GetColRight(i) - x) < WXGRID_LABEL_EDGE_ZONE )
7885 return i;
7886 else if( i > 0 && x - GetColLeft(i) < WXGRID_LABEL_EDGE_ZONE )
7887 return i - 1;
7888 }
7889
7890 return -1;
7891 }
7892
7893
7894 wxRect wxGrid::CellToRect( int row, int col )
7895 {
7896 wxRect rect( -1, -1, -1, -1 );
7897
7898 if ( row >= 0 && row < m_numRows &&
7899 col >= 0 && col < m_numCols )
7900 {
7901 int i, cell_rows, cell_cols;
7902 rect.width = rect.height = 0;
7903 GetCellSize( row, col, &cell_rows, &cell_cols );
7904 // if negative then find multicell owner
7905 if (cell_rows < 0) row += cell_rows;
7906 if (cell_cols < 0) col += cell_cols;
7907 GetCellSize( row, col, &cell_rows, &cell_cols );
7908
7909 rect.x = GetColLeft(col);
7910 rect.y = GetRowTop(row);
7911 for (i=col; i<col+cell_cols; i++)
7912 rect.width += GetColWidth(i);
7913 for (i=row; i<row+cell_rows; i++)
7914 rect.height += GetRowHeight(i);
7915 }
7916
7917 // if grid lines are enabled, then the area of the cell is a bit smaller
7918 if (m_gridLinesEnabled) {
7919 rect.width -= 1;
7920 rect.height -= 1;
7921 }
7922 return rect;
7923 }
7924
7925
7926 bool wxGrid::IsVisible( int row, int col, bool wholeCellVisible )
7927 {
7928 // get the cell rectangle in logical coords
7929 //
7930 wxRect r( CellToRect( row, col ) );
7931
7932 // convert to device coords
7933 //
7934 int left, top, right, bottom;
7935 CalcScrolledPosition( r.GetLeft(), r.GetTop(), &left, &top );
7936 CalcScrolledPosition( r.GetRight(), r.GetBottom(), &right, &bottom );
7937
7938 // check against the client area of the grid window
7939 //
7940 int cw, ch;
7941 m_gridWin->GetClientSize( &cw, &ch );
7942
7943 if ( wholeCellVisible )
7944 {
7945 // is the cell wholly visible ?
7946 //
7947 return ( left >= 0 && right <= cw &&
7948 top >= 0 && bottom <= ch );
7949 }
7950 else
7951 {
7952 // is the cell partly visible ?
7953 //
7954 return ( ((left >=0 && left < cw) || (right > 0 && right <= cw)) &&
7955 ((top >=0 && top < ch) || (bottom > 0 && bottom <= ch)) );
7956 }
7957 }
7958
7959
7960 // make the specified cell location visible by doing a minimal amount
7961 // of scrolling
7962 //
7963 void wxGrid::MakeCellVisible( int row, int col )
7964 {
7965
7966 int i;
7967 int xpos = -1, ypos = -1;
7968
7969 if ( row >= 0 && row < m_numRows &&
7970 col >= 0 && col < m_numCols )
7971 {
7972 // get the cell rectangle in logical coords
7973 //
7974 wxRect r( CellToRect( row, col ) );
7975
7976 // convert to device coords
7977 //
7978 int left, top, right, bottom;
7979 CalcScrolledPosition( r.GetLeft(), r.GetTop(), &left, &top );
7980 CalcScrolledPosition( r.GetRight(), r.GetBottom(), &right, &bottom );
7981
7982 int cw, ch;
7983 m_gridWin->GetClientSize( &cw, &ch );
7984
7985 if ( top < 0 )
7986 {
7987 ypos = r.GetTop();
7988 }
7989 else if ( bottom > ch )
7990 {
7991 int h = r.GetHeight();
7992 ypos = r.GetTop();
7993 for ( i = row-1; i >= 0; i-- )
7994 {
7995 int rowHeight = GetRowHeight(i);
7996 if ( h + rowHeight > ch )
7997 break;
7998
7999 h += rowHeight;
8000 ypos -= rowHeight;
8001 }
8002
8003 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
8004 // have rounding errors (this is important, because if we do, we
8005 // might not scroll at all and some cells won't be redrawn)
8006 //
8007 // Sometimes GRID_SCROLL_LINE/2 is not enough, so just add a full
8008 // scroll unit...
8009 ypos += GRID_SCROLL_LINE_Y;
8010 }
8011
8012 if ( left < 0 )
8013 {
8014 xpos = r.GetLeft();
8015 }
8016 else if ( right > cw )
8017 {
8018 // position the view so that the cell is on the right
8019 int x0, y0;
8020 CalcUnscrolledPosition(0, 0, &x0, &y0);
8021 xpos = x0 + (right - cw);
8022
8023 // see comment for ypos above
8024 xpos += GRID_SCROLL_LINE_X;
8025 }
8026
8027 if ( xpos != -1 || ypos != -1 )
8028 {
8029 if ( xpos != -1 )
8030 xpos /= GRID_SCROLL_LINE_X;
8031 if ( ypos != -1 )
8032 ypos /= GRID_SCROLL_LINE_Y;
8033 Scroll( xpos, ypos );
8034 AdjustScrollbars();
8035 }
8036 }
8037 }
8038
8039
8040 //
8041 // ------ Grid cursor movement functions
8042 //
8043
8044 bool wxGrid::MoveCursorUp( bool expandSelection )
8045 {
8046 if ( m_currentCellCoords != wxGridNoCellCoords &&
8047 m_currentCellCoords.GetRow() >= 0 )
8048 {
8049 if ( expandSelection)
8050 {
8051 if ( m_selectingKeyboard == wxGridNoCellCoords )
8052 m_selectingKeyboard = m_currentCellCoords;
8053 if ( m_selectingKeyboard.GetRow() > 0 )
8054 {
8055 m_selectingKeyboard.SetRow( m_selectingKeyboard.GetRow() - 1 );
8056 MakeCellVisible( m_selectingKeyboard.GetRow(),
8057 m_selectingKeyboard.GetCol() );
8058 HighlightBlock( m_currentCellCoords, m_selectingKeyboard );
8059 }
8060 }
8061 else if ( m_currentCellCoords.GetRow() > 0 )
8062 {
8063 ClearSelection();
8064 MakeCellVisible( m_currentCellCoords.GetRow() - 1,
8065 m_currentCellCoords.GetCol() );
8066 SetCurrentCell( m_currentCellCoords.GetRow() - 1,
8067 m_currentCellCoords.GetCol() );
8068 }
8069 else
8070 return false;
8071 return true;
8072 }
8073
8074 return false;
8075 }
8076
8077
8078 bool wxGrid::MoveCursorDown( bool expandSelection )
8079 {
8080 if ( m_currentCellCoords != wxGridNoCellCoords &&
8081 m_currentCellCoords.GetRow() < m_numRows )
8082 {
8083 if ( expandSelection )
8084 {
8085 if ( m_selectingKeyboard == wxGridNoCellCoords )
8086 m_selectingKeyboard = m_currentCellCoords;
8087 if ( m_selectingKeyboard.GetRow() < m_numRows-1 )
8088 {
8089 m_selectingKeyboard.SetRow( m_selectingKeyboard.GetRow() + 1 );
8090 MakeCellVisible( m_selectingKeyboard.GetRow(),
8091 m_selectingKeyboard.GetCol() );
8092 HighlightBlock( m_currentCellCoords, m_selectingKeyboard );
8093 }
8094 }
8095 else if ( m_currentCellCoords.GetRow() < m_numRows - 1 )
8096 {
8097 ClearSelection();
8098 MakeCellVisible( m_currentCellCoords.GetRow() + 1,
8099 m_currentCellCoords.GetCol() );
8100 SetCurrentCell( m_currentCellCoords.GetRow() + 1,
8101 m_currentCellCoords.GetCol() );
8102 }
8103 else
8104 return false;
8105 return true;
8106 }
8107
8108 return false;
8109 }
8110
8111
8112 bool wxGrid::MoveCursorLeft( bool expandSelection )
8113 {
8114 if ( m_currentCellCoords != wxGridNoCellCoords &&
8115 m_currentCellCoords.GetCol() >= 0 )
8116 {
8117 if ( expandSelection )
8118 {
8119 if ( m_selectingKeyboard == wxGridNoCellCoords )
8120 m_selectingKeyboard = m_currentCellCoords;
8121 if ( m_selectingKeyboard.GetCol() > 0 )
8122 {
8123 m_selectingKeyboard.SetCol( m_selectingKeyboard.GetCol() - 1 );
8124 MakeCellVisible( m_selectingKeyboard.GetRow(),
8125 m_selectingKeyboard.GetCol() );
8126 HighlightBlock( m_currentCellCoords, m_selectingKeyboard );
8127 }
8128 }
8129 else if ( m_currentCellCoords.GetCol() > 0 )
8130 {
8131 ClearSelection();
8132 MakeCellVisible( m_currentCellCoords.GetRow(),
8133 m_currentCellCoords.GetCol() - 1 );
8134 SetCurrentCell( m_currentCellCoords.GetRow(),
8135 m_currentCellCoords.GetCol() - 1 );
8136 }
8137 else
8138 return false;
8139 return true;
8140 }
8141
8142 return false;
8143 }
8144
8145
8146 bool wxGrid::MoveCursorRight( bool expandSelection )
8147 {
8148 if ( m_currentCellCoords != wxGridNoCellCoords &&
8149 m_currentCellCoords.GetCol() < m_numCols )
8150 {
8151 if ( expandSelection )
8152 {
8153 if ( m_selectingKeyboard == wxGridNoCellCoords )
8154 m_selectingKeyboard = m_currentCellCoords;
8155 if ( m_selectingKeyboard.GetCol() < m_numCols - 1 )
8156 {
8157 m_selectingKeyboard.SetCol( m_selectingKeyboard.GetCol() + 1 );
8158 MakeCellVisible( m_selectingKeyboard.GetRow(),
8159 m_selectingKeyboard.GetCol() );
8160 HighlightBlock( m_currentCellCoords, m_selectingKeyboard );
8161 }
8162 }
8163 else if ( m_currentCellCoords.GetCol() < m_numCols - 1 )
8164 {
8165 ClearSelection();
8166 MakeCellVisible( m_currentCellCoords.GetRow(),
8167 m_currentCellCoords.GetCol() + 1 );
8168 SetCurrentCell( m_currentCellCoords.GetRow(),
8169 m_currentCellCoords.GetCol() + 1 );
8170 }
8171 else
8172 return false;
8173 return true;
8174 }
8175
8176 return false;
8177 }
8178
8179
8180 bool wxGrid::MovePageUp()
8181 {
8182 if ( m_currentCellCoords == wxGridNoCellCoords ) return false;
8183
8184 int row = m_currentCellCoords.GetRow();
8185 if ( row > 0 )
8186 {
8187 int cw, ch;
8188 m_gridWin->GetClientSize( &cw, &ch );
8189
8190 int y = GetRowTop(row);
8191 int newRow = internalYToRow( y - ch + 1 );
8192
8193 if ( newRow == row )
8194 {
8195 //row > 0 , so newrow can never be less than 0 here.
8196 newRow = row - 1;
8197 }
8198
8199 MakeCellVisible( newRow, m_currentCellCoords.GetCol() );
8200 SetCurrentCell( newRow, m_currentCellCoords.GetCol() );
8201
8202 return true;
8203 }
8204
8205 return false;
8206 }
8207
8208 bool wxGrid::MovePageDown()
8209 {
8210 if ( m_currentCellCoords == wxGridNoCellCoords ) return false;
8211
8212 int row = m_currentCellCoords.GetRow();
8213 if ( (row+1) < m_numRows )
8214 {
8215 int cw, ch;
8216 m_gridWin->GetClientSize( &cw, &ch );
8217
8218 int y = GetRowTop(row);
8219 int newRow = internalYToRow( y + ch );
8220 if ( newRow == row )
8221 {
8222 // row < m_numRows , so newrow can't overflow here.
8223 newRow = row + 1;
8224 }
8225
8226 MakeCellVisible( newRow, m_currentCellCoords.GetCol() );
8227 SetCurrentCell( newRow, m_currentCellCoords.GetCol() );
8228
8229 return true;
8230 }
8231
8232 return false;
8233 }
8234
8235 bool wxGrid::MoveCursorUpBlock( bool expandSelection )
8236 {
8237 if ( m_table &&
8238 m_currentCellCoords != wxGridNoCellCoords &&
8239 m_currentCellCoords.GetRow() > 0 )
8240 {
8241 int row = m_currentCellCoords.GetRow();
8242 int col = m_currentCellCoords.GetCol();
8243
8244 if ( m_table->IsEmptyCell(row, col) )
8245 {
8246 // starting in an empty cell: find the next block of
8247 // non-empty cells
8248 //
8249 while ( row > 0 )
8250 {
8251 row-- ;
8252 if ( !(m_table->IsEmptyCell(row, col)) ) break;
8253 }
8254 }
8255 else if ( m_table->IsEmptyCell(row-1, col) )
8256 {
8257 // starting at the top of a block: find the next block
8258 //
8259 row--;
8260 while ( row > 0 )
8261 {
8262 row-- ;
8263 if ( !(m_table->IsEmptyCell(row, col)) ) break;
8264 }
8265 }
8266 else
8267 {
8268 // starting within a block: find the top of the block
8269 //
8270 while ( row > 0 )
8271 {
8272 row-- ;
8273 if ( m_table->IsEmptyCell(row, col) )
8274 {
8275 row++ ;
8276 break;
8277 }
8278 }
8279 }
8280
8281 MakeCellVisible( row, col );
8282 if ( expandSelection )
8283 {
8284 m_selectingKeyboard = wxGridCellCoords( row, col );
8285 HighlightBlock( m_currentCellCoords, m_selectingKeyboard );
8286 }
8287 else
8288 {
8289 ClearSelection();
8290 SetCurrentCell( row, col );
8291 }
8292 return true;
8293 }
8294
8295 return false;
8296 }
8297
8298 bool wxGrid::MoveCursorDownBlock( bool expandSelection )
8299 {
8300 if ( m_table &&
8301 m_currentCellCoords != wxGridNoCellCoords &&
8302 m_currentCellCoords.GetRow() < m_numRows-1 )
8303 {
8304 int row = m_currentCellCoords.GetRow();
8305 int col = m_currentCellCoords.GetCol();
8306
8307 if ( m_table->IsEmptyCell(row, col) )
8308 {
8309 // starting in an empty cell: find the next block of
8310 // non-empty cells
8311 //
8312 while ( row < m_numRows-1 )
8313 {
8314 row++ ;
8315 if ( !(m_table->IsEmptyCell(row, col)) ) break;
8316 }
8317 }
8318 else if ( m_table->IsEmptyCell(row+1, col) )
8319 {
8320 // starting at the bottom of a block: find the next block
8321 //
8322 row++;
8323 while ( row < m_numRows-1 )
8324 {
8325 row++ ;
8326 if ( !(m_table->IsEmptyCell(row, col)) ) break;
8327 }
8328 }
8329 else
8330 {
8331 // starting within a block: find the bottom of the block
8332 //
8333 while ( row < m_numRows-1 )
8334 {
8335 row++ ;
8336 if ( m_table->IsEmptyCell(row, col) )
8337 {
8338 row-- ;
8339 break;
8340 }
8341 }
8342 }
8343
8344 MakeCellVisible( row, col );
8345 if ( expandSelection )
8346 {
8347 m_selectingKeyboard = wxGridCellCoords( row, col );
8348 HighlightBlock( m_currentCellCoords, m_selectingKeyboard );
8349 }
8350 else
8351 {
8352 ClearSelection();
8353 SetCurrentCell( row, col );
8354 }
8355
8356 return true;
8357 }
8358
8359 return false;
8360 }
8361
8362 bool wxGrid::MoveCursorLeftBlock( bool expandSelection )
8363 {
8364 if ( m_table &&
8365 m_currentCellCoords != wxGridNoCellCoords &&
8366 m_currentCellCoords.GetCol() > 0 )
8367 {
8368 int row = m_currentCellCoords.GetRow();
8369 int col = m_currentCellCoords.GetCol();
8370
8371 if ( m_table->IsEmptyCell(row, col) )
8372 {
8373 // starting in an empty cell: find the next block of
8374 // non-empty cells
8375 //
8376 while ( col > 0 )
8377 {
8378 col-- ;
8379 if ( !(m_table->IsEmptyCell(row, col)) ) break;
8380 }
8381 }
8382 else if ( m_table->IsEmptyCell(row, col-1) )
8383 {
8384 // starting at the left of a block: find the next block
8385 //
8386 col--;
8387 while ( col > 0 )
8388 {
8389 col-- ;
8390 if ( !(m_table->IsEmptyCell(row, col)) ) break;
8391 }
8392 }
8393 else
8394 {
8395 // starting within a block: find the left of the block
8396 //
8397 while ( col > 0 )
8398 {
8399 col-- ;
8400 if ( m_table->IsEmptyCell(row, col) )
8401 {
8402 col++ ;
8403 break;
8404 }
8405 }
8406 }
8407
8408 MakeCellVisible( row, col );
8409 if ( expandSelection )
8410 {
8411 m_selectingKeyboard = wxGridCellCoords( row, col );
8412 HighlightBlock( m_currentCellCoords, m_selectingKeyboard );
8413 }
8414 else
8415 {
8416 ClearSelection();
8417 SetCurrentCell( row, col );
8418 }
8419
8420 return true;
8421 }
8422
8423 return false;
8424 }
8425
8426 bool wxGrid::MoveCursorRightBlock( bool expandSelection )
8427 {
8428 if ( m_table &&
8429 m_currentCellCoords != wxGridNoCellCoords &&
8430 m_currentCellCoords.GetCol() < m_numCols-1 )
8431 {
8432 int row = m_currentCellCoords.GetRow();
8433 int col = m_currentCellCoords.GetCol();
8434
8435 if ( m_table->IsEmptyCell(row, col) )
8436 {
8437 // starting in an empty cell: find the next block of
8438 // non-empty cells
8439 //
8440 while ( col < m_numCols-1 )
8441 {
8442 col++ ;
8443 if ( !(m_table->IsEmptyCell(row, col)) ) break;
8444 }
8445 }
8446 else if ( m_table->IsEmptyCell(row, col+1) )
8447 {
8448 // starting at the right of a block: find the next block
8449 //
8450 col++;
8451 while ( col < m_numCols-1 )
8452 {
8453 col++ ;
8454 if ( !(m_table->IsEmptyCell(row, col)) ) break;
8455 }
8456 }
8457 else
8458 {
8459 // starting within a block: find the right of the block
8460 //
8461 while ( col < m_numCols-1 )
8462 {
8463 col++ ;
8464 if ( m_table->IsEmptyCell(row, col) )
8465 {
8466 col-- ;
8467 break;
8468 }
8469 }
8470 }
8471
8472 MakeCellVisible( row, col );
8473 if ( expandSelection )
8474 {
8475 m_selectingKeyboard = wxGridCellCoords( row, col );
8476 HighlightBlock( m_currentCellCoords, m_selectingKeyboard );
8477 }
8478 else
8479 {
8480 ClearSelection();
8481 SetCurrentCell( row, col );
8482 }
8483
8484 return true;
8485 }
8486
8487 return false;
8488 }
8489
8490
8491
8492 //
8493 // ------ Label values and formatting
8494 //
8495
8496 void wxGrid::GetRowLabelAlignment( int *horiz, int *vert )
8497 {
8498 *horiz = m_rowLabelHorizAlign;
8499 *vert = m_rowLabelVertAlign;
8500 }
8501
8502 void wxGrid::GetColLabelAlignment( int *horiz, int *vert )
8503 {
8504 *horiz = m_colLabelHorizAlign;
8505 *vert = m_colLabelVertAlign;
8506 }
8507
8508 int wxGrid::GetColLabelTextOrientation()
8509 {
8510 return m_colLabelTextOrientation;
8511 }
8512
8513 wxString wxGrid::GetRowLabelValue( int row )
8514 {
8515 if ( m_table )
8516 {
8517 return m_table->GetRowLabelValue( row );
8518 }
8519 else
8520 {
8521 wxString s;
8522 s << row;
8523 return s;
8524 }
8525 }
8526
8527 wxString wxGrid::GetColLabelValue( int col )
8528 {
8529 if ( m_table )
8530 {
8531 return m_table->GetColLabelValue( col );
8532 }
8533 else
8534 {
8535 wxString s;
8536 s << col;
8537 return s;
8538 }
8539 }
8540
8541
8542 void wxGrid::SetRowLabelSize( int width )
8543 {
8544 width = wxMax( width, 0 );
8545 if ( width != m_rowLabelWidth )
8546 {
8547 if ( width == 0 )
8548 {
8549 m_rowLabelWin->Show( false );
8550 m_cornerLabelWin->Show( false );
8551 }
8552 else if ( m_rowLabelWidth == 0 )
8553 {
8554 m_rowLabelWin->Show( true );
8555 if ( m_colLabelHeight > 0 ) m_cornerLabelWin->Show( true );
8556 }
8557
8558 m_rowLabelWidth = width;
8559 CalcWindowSizes();
8560 wxScrolledWindow::Refresh( true );
8561 }
8562 }
8563
8564
8565 void wxGrid::SetColLabelSize( int height )
8566 {
8567 height = wxMax( height, 0 );
8568 if ( height != m_colLabelHeight )
8569 {
8570 if ( height == 0 )
8571 {
8572 m_colLabelWin->Show( false );
8573 m_cornerLabelWin->Show( false );
8574 }
8575 else if ( m_colLabelHeight == 0 )
8576 {
8577 m_colLabelWin->Show( true );
8578 if ( m_rowLabelWidth > 0 ) m_cornerLabelWin->Show( true );
8579 }
8580
8581 m_colLabelHeight = height;
8582 CalcWindowSizes();
8583 wxScrolledWindow::Refresh( true );
8584 }
8585 }
8586
8587
8588 void wxGrid::SetLabelBackgroundColour( const wxColour& colour )
8589 {
8590 if ( m_labelBackgroundColour != colour )
8591 {
8592 m_labelBackgroundColour = colour;
8593 m_rowLabelWin->SetBackgroundColour( colour );
8594 m_colLabelWin->SetBackgroundColour( colour );
8595 m_cornerLabelWin->SetBackgroundColour( colour );
8596
8597 if ( !GetBatchCount() )
8598 {
8599 m_rowLabelWin->Refresh();
8600 m_colLabelWin->Refresh();
8601 m_cornerLabelWin->Refresh();
8602 }
8603 }
8604 }
8605
8606 void wxGrid::SetLabelTextColour( const wxColour& colour )
8607 {
8608 if ( m_labelTextColour != colour )
8609 {
8610 m_labelTextColour = colour;
8611 if ( !GetBatchCount() )
8612 {
8613 m_rowLabelWin->Refresh();
8614 m_colLabelWin->Refresh();
8615 }
8616 }
8617 }
8618
8619 void wxGrid::SetLabelFont( const wxFont& font )
8620 {
8621 m_labelFont = font;
8622 if ( !GetBatchCount() )
8623 {
8624 m_rowLabelWin->Refresh();
8625 m_colLabelWin->Refresh();
8626 }
8627 }
8628
8629 void wxGrid::SetRowLabelAlignment( int horiz, int vert )
8630 {
8631 // allow old (incorrect) defs to be used
8632 switch ( horiz )
8633 {
8634 case wxLEFT: horiz = wxALIGN_LEFT; break;
8635 case wxRIGHT: horiz = wxALIGN_RIGHT; break;
8636 case wxCENTRE: horiz = wxALIGN_CENTRE; break;
8637 }
8638
8639 switch ( vert )
8640 {
8641 case wxTOP: vert = wxALIGN_TOP; break;
8642 case wxBOTTOM: vert = wxALIGN_BOTTOM; break;
8643 case wxCENTRE: vert = wxALIGN_CENTRE; break;
8644 }
8645
8646 if ( horiz == wxALIGN_LEFT || horiz == wxALIGN_CENTRE || horiz == wxALIGN_RIGHT )
8647 {
8648 m_rowLabelHorizAlign = horiz;
8649 }
8650
8651 if ( vert == wxALIGN_TOP || vert == wxALIGN_CENTRE || vert == wxALIGN_BOTTOM )
8652 {
8653 m_rowLabelVertAlign = vert;
8654 }
8655
8656 if ( !GetBatchCount() )
8657 {
8658 m_rowLabelWin->Refresh();
8659 }
8660 }
8661
8662 void wxGrid::SetColLabelAlignment( int horiz, int vert )
8663 {
8664 // allow old (incorrect) defs to be used
8665 switch ( horiz )
8666 {
8667 case wxLEFT: horiz = wxALIGN_LEFT; break;
8668 case wxRIGHT: horiz = wxALIGN_RIGHT; break;
8669 case wxCENTRE: horiz = wxALIGN_CENTRE; break;
8670 }
8671
8672 switch ( vert )
8673 {
8674 case wxTOP: vert = wxALIGN_TOP; break;
8675 case wxBOTTOM: vert = wxALIGN_BOTTOM; break;
8676 case wxCENTRE: vert = wxALIGN_CENTRE; break;
8677 }
8678
8679 if ( horiz == wxALIGN_LEFT || horiz == wxALIGN_CENTRE || horiz == wxALIGN_RIGHT )
8680 {
8681 m_colLabelHorizAlign = horiz;
8682 }
8683
8684 if ( vert == wxALIGN_TOP || vert == wxALIGN_CENTRE || vert == wxALIGN_BOTTOM )
8685 {
8686 m_colLabelVertAlign = vert;
8687 }
8688
8689 if ( !GetBatchCount() )
8690 {
8691 m_colLabelWin->Refresh();
8692 }
8693 }
8694
8695 // Note: under MSW, the default column label font must be changed because it
8696 // does not support vertical printing
8697 //
8698 // Example: wxFont font(9, wxSWISS, wxNORMAL, wxBOLD);
8699 // pGrid->SetLabelFont(font);
8700 // pGrid->SetColLabelTextOrientation(wxVERTICAL);
8701 //
8702 void wxGrid::SetColLabelTextOrientation( int textOrientation )
8703 {
8704 if( textOrientation == wxHORIZONTAL || textOrientation == wxVERTICAL )
8705 {
8706 m_colLabelTextOrientation = textOrientation;
8707 }
8708
8709 if ( !GetBatchCount() )
8710 {
8711 m_colLabelWin->Refresh();
8712 }
8713 }
8714
8715 void wxGrid::SetRowLabelValue( int row, const wxString& s )
8716 {
8717 if ( m_table )
8718 {
8719 m_table->SetRowLabelValue( row, s );
8720 if ( !GetBatchCount() )
8721 {
8722 wxRect rect = CellToRect( row, 0);
8723 if ( rect.height > 0 )
8724 {
8725 CalcScrolledPosition(0, rect.y, &rect.x, &rect.y);
8726 rect.x = 0;
8727 rect.width = m_rowLabelWidth;
8728 m_rowLabelWin->Refresh( true, &rect );
8729 }
8730 }
8731 }
8732 }
8733
8734 void wxGrid::SetColLabelValue( int col, const wxString& s )
8735 {
8736 if ( m_table )
8737 {
8738 m_table->SetColLabelValue( col, s );
8739 if ( !GetBatchCount() )
8740 {
8741 wxRect rect = CellToRect( 0, col );
8742 if ( rect.width > 0 )
8743 {
8744 CalcScrolledPosition(rect.x, 0, &rect.x, &rect.y);
8745 rect.y = 0;
8746 rect.height = m_colLabelHeight;
8747 m_colLabelWin->Refresh( true, &rect );
8748 }
8749 }
8750 }
8751 }
8752
8753 void wxGrid::SetGridLineColour( const wxColour& colour )
8754 {
8755 if ( m_gridLineColour != colour )
8756 {
8757 m_gridLineColour = colour;
8758
8759 wxClientDC dc( m_gridWin );
8760 PrepareDC( dc );
8761 DrawAllGridLines( dc, wxRegion() );
8762 }
8763 }
8764
8765
8766 void wxGrid::SetCellHighlightColour( const wxColour& colour )
8767 {
8768 if ( m_cellHighlightColour != colour )
8769 {
8770 m_cellHighlightColour = colour;
8771
8772 wxClientDC dc( m_gridWin );
8773 PrepareDC( dc );
8774 wxGridCellAttr* attr = GetCellAttr(m_currentCellCoords);
8775 DrawCellHighlight(dc, attr);
8776 attr->DecRef();
8777 }
8778 }
8779
8780 void wxGrid::SetCellHighlightPenWidth(int width)
8781 {
8782 if (m_cellHighlightPenWidth != width) {
8783 m_cellHighlightPenWidth = width;
8784
8785 // Just redrawing the cell highlight is not enough since that won't
8786 // make any visible change if the the thickness is getting smaller.
8787 int row = m_currentCellCoords.GetRow();
8788 int col = m_currentCellCoords.GetCol();
8789 if ( GetColWidth(col) <= 0 || GetRowHeight(row) <= 0 )
8790 return;
8791 wxRect rect = CellToRect(row, col);
8792 m_gridWin->Refresh(true, &rect);
8793 }
8794 }
8795
8796 void wxGrid::SetCellHighlightROPenWidth(int width)
8797 {
8798 if (m_cellHighlightROPenWidth != width) {
8799 m_cellHighlightROPenWidth = width;
8800
8801 // Just redrawing the cell highlight is not enough since that won't
8802 // make any visible change if the the thickness is getting smaller.
8803 int row = m_currentCellCoords.GetRow();
8804 int col = m_currentCellCoords.GetCol();
8805 if ( GetColWidth(col) <= 0 || GetRowHeight(row) <= 0 )
8806 return;
8807 wxRect rect = CellToRect(row, col);
8808 m_gridWin->Refresh(true, &rect);
8809 }
8810 }
8811
8812 void wxGrid::EnableGridLines( bool enable )
8813 {
8814 if ( enable != m_gridLinesEnabled )
8815 {
8816 m_gridLinesEnabled = enable;
8817
8818 if ( !GetBatchCount() )
8819 {
8820 if ( enable )
8821 {
8822 wxClientDC dc( m_gridWin );
8823 PrepareDC( dc );
8824 DrawAllGridLines( dc, wxRegion() );
8825 }
8826 else
8827 {
8828 m_gridWin->Refresh();
8829 }
8830 }
8831 }
8832 }
8833
8834
8835 int wxGrid::GetDefaultRowSize()
8836 {
8837 return m_defaultRowHeight;
8838 }
8839
8840 int wxGrid::GetRowSize( int row )
8841 {
8842 wxCHECK_MSG( row >= 0 && row < m_numRows, 0, _T("invalid row index") );
8843
8844 return GetRowHeight(row);
8845 }
8846
8847 int wxGrid::GetDefaultColSize()
8848 {
8849 return m_defaultColWidth;
8850 }
8851
8852 int wxGrid::GetColSize( int col )
8853 {
8854 wxCHECK_MSG( col >= 0 && col < m_numCols, 0, _T("invalid column index") );
8855
8856 return GetColWidth(col);
8857 }
8858
8859 // ============================================================================
8860 // access to the grid attributes: each of them has a default value in the grid
8861 // itself and may be overidden on a per-cell basis
8862 // ============================================================================
8863
8864 // ----------------------------------------------------------------------------
8865 // setting default attributes
8866 // ----------------------------------------------------------------------------
8867
8868 void wxGrid::SetDefaultCellBackgroundColour( const wxColour& col )
8869 {
8870 m_defaultCellAttr->SetBackgroundColour(col);
8871 #ifdef __WXGTK__
8872 m_gridWin->SetBackgroundColour(col);
8873 #endif
8874 }
8875
8876 void wxGrid::SetDefaultCellTextColour( const wxColour& col )
8877 {
8878 m_defaultCellAttr->SetTextColour(col);
8879 }
8880
8881 void wxGrid::SetDefaultCellAlignment( int horiz, int vert )
8882 {
8883 m_defaultCellAttr->SetAlignment(horiz, vert);
8884 }
8885
8886 void wxGrid::SetDefaultCellOverflow( bool allow )
8887 {
8888 m_defaultCellAttr->SetOverflow(allow);
8889 }
8890
8891 void wxGrid::SetDefaultCellFont( const wxFont& font )
8892 {
8893 m_defaultCellAttr->SetFont(font);
8894 }
8895
8896
8897 // For editors and renderers the type registry takes precedence over the
8898 // default attr, so we need to register the new editor/renderer for the string
8899 // data type in order to make setting a default editor/renderer appear to
8900 // work correctly.
8901
8902 void wxGrid::SetDefaultRenderer(wxGridCellRenderer *renderer)
8903 {
8904 RegisterDataType(wxGRID_VALUE_STRING,
8905 renderer,
8906 GetDefaultEditorForType(wxGRID_VALUE_STRING));
8907 }
8908
8909 void wxGrid::SetDefaultEditor(wxGridCellEditor *editor)
8910 {
8911 RegisterDataType(wxGRID_VALUE_STRING,
8912 GetDefaultRendererForType(wxGRID_VALUE_STRING),
8913 editor);
8914 }
8915
8916 // ----------------------------------------------------------------------------
8917 // access to the default attrbiutes
8918 // ----------------------------------------------------------------------------
8919
8920 wxColour wxGrid::GetDefaultCellBackgroundColour()
8921 {
8922 return m_defaultCellAttr->GetBackgroundColour();
8923 }
8924
8925 wxColour wxGrid::GetDefaultCellTextColour()
8926 {
8927 return m_defaultCellAttr->GetTextColour();
8928 }
8929
8930 wxFont wxGrid::GetDefaultCellFont()
8931 {
8932 return m_defaultCellAttr->GetFont();
8933 }
8934
8935 void wxGrid::GetDefaultCellAlignment( int *horiz, int *vert )
8936 {
8937 m_defaultCellAttr->GetAlignment(horiz, vert);
8938 }
8939
8940 bool wxGrid::GetDefaultCellOverflow()
8941 {
8942 return m_defaultCellAttr->GetOverflow();
8943 }
8944
8945 wxGridCellRenderer *wxGrid::GetDefaultRenderer() const
8946 {
8947 return m_defaultCellAttr->GetRenderer(NULL, 0, 0);
8948 }
8949
8950 wxGridCellEditor *wxGrid::GetDefaultEditor() const
8951 {
8952 return m_defaultCellAttr->GetEditor(NULL,0,0);
8953 }
8954
8955 // ----------------------------------------------------------------------------
8956 // access to cell attributes
8957 // ----------------------------------------------------------------------------
8958
8959 wxColour wxGrid::GetCellBackgroundColour(int row, int col)
8960 {
8961 wxGridCellAttr *attr = GetCellAttr(row, col);
8962 wxColour colour = attr->GetBackgroundColour();
8963 attr->DecRef();
8964 return colour;
8965 }
8966
8967 wxColour wxGrid::GetCellTextColour( int row, int col )
8968 {
8969 wxGridCellAttr *attr = GetCellAttr(row, col);
8970 wxColour colour = attr->GetTextColour();
8971 attr->DecRef();
8972 return colour;
8973 }
8974
8975 wxFont wxGrid::GetCellFont( int row, int col )
8976 {
8977 wxGridCellAttr *attr = GetCellAttr(row, col);
8978 wxFont font = attr->GetFont();
8979 attr->DecRef();
8980 return font;
8981 }
8982
8983 void wxGrid::GetCellAlignment( int row, int col, int *horiz, int *vert )
8984 {
8985 wxGridCellAttr *attr = GetCellAttr(row, col);
8986 attr->GetAlignment(horiz, vert);
8987 attr->DecRef();
8988 }
8989
8990 bool wxGrid::GetCellOverflow( int row, int col )
8991 {
8992 wxGridCellAttr *attr = GetCellAttr(row, col);
8993 bool allow = attr->GetOverflow();
8994 attr->DecRef();
8995 return allow;
8996 }
8997
8998 void wxGrid::GetCellSize( int row, int col, int *num_rows, int *num_cols )
8999 {
9000 wxGridCellAttr *attr = GetCellAttr(row, col);
9001 attr->GetSize( num_rows, num_cols );
9002 attr->DecRef();
9003 }
9004
9005 wxGridCellRenderer* wxGrid::GetCellRenderer(int row, int col)
9006 {
9007 wxGridCellAttr* attr = GetCellAttr(row, col);
9008 wxGridCellRenderer* renderer = attr->GetRenderer(this, row, col);
9009 attr->DecRef();
9010
9011 return renderer;
9012 }
9013
9014 wxGridCellEditor* wxGrid::GetCellEditor(int row, int col)
9015 {
9016 wxGridCellAttr* attr = GetCellAttr(row, col);
9017 wxGridCellEditor* editor = attr->GetEditor(this, row, col);
9018 attr->DecRef();
9019
9020 return editor;
9021 }
9022
9023 bool wxGrid::IsReadOnly(int row, int col) const
9024 {
9025 wxGridCellAttr* attr = GetCellAttr(row, col);
9026 bool isReadOnly = attr->IsReadOnly();
9027 attr->DecRef();
9028 return isReadOnly;
9029 }
9030
9031 // ----------------------------------------------------------------------------
9032 // attribute support: cache, automatic provider creation, ...
9033 // ----------------------------------------------------------------------------
9034
9035 bool wxGrid::CanHaveAttributes()
9036 {
9037 if ( !m_table )
9038 {
9039 return false;
9040 }
9041
9042 return m_table->CanHaveAttributes();
9043 }
9044
9045 void wxGrid::ClearAttrCache()
9046 {
9047 if ( m_attrCache.row != -1 )
9048 {
9049 wxSafeDecRef(m_attrCache.attr);
9050 m_attrCache.attr = NULL;
9051 m_attrCache.row = -1;
9052 }
9053 }
9054
9055 void wxGrid::CacheAttr(int row, int col, wxGridCellAttr *attr) const
9056 {
9057 if ( attr != NULL )
9058 {
9059 wxGrid *self = (wxGrid *)this; // const_cast
9060
9061 self->ClearAttrCache();
9062 self->m_attrCache.row = row;
9063 self->m_attrCache.col = col;
9064 self->m_attrCache.attr = attr;
9065 wxSafeIncRef(attr);
9066 }
9067 }
9068
9069 bool wxGrid::LookupAttr(int row, int col, wxGridCellAttr **attr) const
9070 {
9071 if ( row == m_attrCache.row && col == m_attrCache.col )
9072 {
9073 *attr = m_attrCache.attr;
9074 wxSafeIncRef(m_attrCache.attr);
9075
9076 #ifdef DEBUG_ATTR_CACHE
9077 gs_nAttrCacheHits++;
9078 #endif
9079
9080 return true;
9081 }
9082 else
9083 {
9084 #ifdef DEBUG_ATTR_CACHE
9085 gs_nAttrCacheMisses++;
9086 #endif
9087 return false;
9088 }
9089 }
9090
9091 wxGridCellAttr *wxGrid::GetCellAttr(int row, int col) const
9092 {
9093 wxGridCellAttr *attr = NULL;
9094 // Additional test to avoid looking at the cache e.g. for
9095 // wxNoCellCoords, as this will confuse memory management.
9096 if ( row >= 0 )
9097 {
9098 if ( !LookupAttr(row, col, &attr) )
9099 {
9100 attr = m_table ? m_table->GetAttr(row, col , wxGridCellAttr::Any)
9101 : (wxGridCellAttr *)NULL;
9102 CacheAttr(row, col, attr);
9103 }
9104 }
9105 if (attr)
9106 {
9107 attr->SetDefAttr(m_defaultCellAttr);
9108 }
9109 else
9110 {
9111 attr = m_defaultCellAttr;
9112 attr->IncRef();
9113 }
9114
9115 return attr;
9116 }
9117
9118 wxGridCellAttr *wxGrid::GetOrCreateCellAttr(int row, int col) const
9119 {
9120 wxGridCellAttr *attr = (wxGridCellAttr *)NULL;
9121 bool canHave = ((wxGrid*)this)->CanHaveAttributes();
9122
9123 wxCHECK_MSG( canHave, attr, _T("Cell attributes not allowed"));
9124 wxCHECK_MSG( m_table, attr, _T("must have a table") );
9125
9126 attr = m_table->GetAttr(row, col, wxGridCellAttr::Cell);
9127 if ( !attr )
9128 {
9129 attr = new wxGridCellAttr(m_defaultCellAttr);
9130
9131 // artificially inc the ref count to match DecRef() in caller
9132 attr->IncRef();
9133 m_table->SetAttr(attr, row, col);
9134 }
9135
9136 return attr;
9137 }
9138
9139 // ----------------------------------------------------------------------------
9140 // setting column attributes (wrappers around SetColAttr)
9141 // ----------------------------------------------------------------------------
9142
9143 void wxGrid::SetColFormatBool(int col)
9144 {
9145 SetColFormatCustom(col, wxGRID_VALUE_BOOL);
9146 }
9147
9148 void wxGrid::SetColFormatNumber(int col)
9149 {
9150 SetColFormatCustom(col, wxGRID_VALUE_NUMBER);
9151 }
9152
9153 void wxGrid::SetColFormatFloat(int col, int width, int precision)
9154 {
9155 wxString typeName = wxGRID_VALUE_FLOAT;
9156 if ( (width != -1) || (precision != -1) )
9157 {
9158 typeName << _T(':') << width << _T(',') << precision;
9159 }
9160
9161 SetColFormatCustom(col, typeName);
9162 }
9163
9164 void wxGrid::SetColFormatCustom(int col, const wxString& typeName)
9165 {
9166 wxGridCellAttr *attr = m_table->GetAttr(-1, col, wxGridCellAttr::Col );
9167 if(!attr)
9168 attr = new wxGridCellAttr;
9169 wxGridCellRenderer *renderer = GetDefaultRendererForType(typeName);
9170 attr->SetRenderer(renderer);
9171
9172 SetColAttr(col, attr);
9173
9174 }
9175
9176 // ----------------------------------------------------------------------------
9177 // setting cell attributes: this is forwarded to the table
9178 // ----------------------------------------------------------------------------
9179
9180 void wxGrid::SetAttr(int row, int col, wxGridCellAttr *attr)
9181 {
9182 if ( CanHaveAttributes() )
9183 {
9184 m_table->SetAttr(attr, row, col);
9185 ClearAttrCache();
9186 }
9187 else
9188 {
9189 wxSafeDecRef(attr);
9190 }
9191 }
9192
9193 void wxGrid::SetRowAttr(int row, wxGridCellAttr *attr)
9194 {
9195 if ( CanHaveAttributes() )
9196 {
9197 m_table->SetRowAttr(attr, row);
9198 ClearAttrCache();
9199 }
9200 else
9201 {
9202 wxSafeDecRef(attr);
9203 }
9204 }
9205
9206 void wxGrid::SetColAttr(int col, wxGridCellAttr *attr)
9207 {
9208 if ( CanHaveAttributes() )
9209 {
9210 m_table->SetColAttr(attr, col);
9211 ClearAttrCache();
9212 }
9213 else
9214 {
9215 wxSafeDecRef(attr);
9216 }
9217 }
9218
9219 void wxGrid::SetCellBackgroundColour( int row, int col, const wxColour& colour )
9220 {
9221 if ( CanHaveAttributes() )
9222 {
9223 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
9224 attr->SetBackgroundColour(colour);
9225 attr->DecRef();
9226 }
9227 }
9228
9229 void wxGrid::SetCellTextColour( int row, int col, const wxColour& colour )
9230 {
9231 if ( CanHaveAttributes() )
9232 {
9233 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
9234 attr->SetTextColour(colour);
9235 attr->DecRef();
9236 }
9237 }
9238
9239 void wxGrid::SetCellFont( int row, int col, const wxFont& font )
9240 {
9241 if ( CanHaveAttributes() )
9242 {
9243 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
9244 attr->SetFont(font);
9245 attr->DecRef();
9246 }
9247 }
9248
9249 void wxGrid::SetCellAlignment( int row, int col, int horiz, int vert )
9250 {
9251 if ( CanHaveAttributes() )
9252 {
9253 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
9254 attr->SetAlignment(horiz, vert);
9255 attr->DecRef();
9256 }
9257 }
9258
9259 void wxGrid::SetCellOverflow( int row, int col, bool allow )
9260 {
9261 if ( CanHaveAttributes() )
9262 {
9263 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
9264 attr->SetOverflow(allow);
9265 attr->DecRef();
9266 }
9267 }
9268
9269 void wxGrid::SetCellSize( int row, int col, int num_rows, int num_cols )
9270 {
9271 if ( CanHaveAttributes() )
9272 {
9273 int cell_rows, cell_cols;
9274
9275 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
9276 attr->GetSize(&cell_rows, &cell_cols);
9277 attr->SetSize(num_rows, num_cols);
9278 attr->DecRef();
9279
9280 // Cannot set the size of a cell to 0 or negative values
9281 // While it is perfectly legal to do that, this function cannot
9282 // handle all the possibilies, do it by hand by getting the CellAttr.
9283 // You can only set the size of a cell to 1,1 or greater with this fn
9284 wxASSERT_MSG( !((cell_rows < 1) || (cell_cols < 1)),
9285 wxT("wxGrid::SetCellSize setting cell size that is already part of another cell"));
9286 wxASSERT_MSG( !((num_rows < 1) || (num_cols < 1)),
9287 wxT("wxGrid::SetCellSize setting cell size to < 1"));
9288
9289 // if this was already a multicell then "turn off" the other cells first
9290 if ((cell_rows > 1) || (cell_rows > 1))
9291 {
9292 int i, j;
9293 for (j=row; j<row+cell_rows; j++)
9294 {
9295 for (i=col; i<col+cell_cols; i++)
9296 {
9297 if ((i != col) || (j != row))
9298 {
9299 wxGridCellAttr *attr_stub = GetOrCreateCellAttr(j, i);
9300 attr_stub->SetSize( 1, 1 );
9301 attr_stub->DecRef();
9302 }
9303 }
9304 }
9305 }
9306
9307 // mark the cells that will be covered by this cell to
9308 // negative or zero values to point back at this cell
9309 if (((num_rows > 1) || (num_cols > 1)) && (num_rows >= 1) && (num_cols >= 1))
9310 {
9311 int i, j;
9312 for (j=row; j<row+num_rows; j++)
9313 {
9314 for (i=col; i<col+num_cols; i++)
9315 {
9316 if ((i != col) || (j != row))
9317 {
9318 wxGridCellAttr *attr_stub = GetOrCreateCellAttr(j, i);
9319 attr_stub->SetSize( row-j, col-i );
9320 attr_stub->DecRef();
9321 }
9322 }
9323 }
9324 }
9325 }
9326 }
9327
9328 void wxGrid::SetCellRenderer(int row, int col, wxGridCellRenderer *renderer)
9329 {
9330 if ( CanHaveAttributes() )
9331 {
9332 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
9333 attr->SetRenderer(renderer);
9334 attr->DecRef();
9335 }
9336 }
9337
9338 void wxGrid::SetCellEditor(int row, int col, wxGridCellEditor* editor)
9339 {
9340 if ( CanHaveAttributes() )
9341 {
9342 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
9343 attr->SetEditor(editor);
9344 attr->DecRef();
9345 }
9346 }
9347
9348 void wxGrid::SetReadOnly(int row, int col, bool isReadOnly)
9349 {
9350 if ( CanHaveAttributes() )
9351 {
9352 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
9353 attr->SetReadOnly(isReadOnly);
9354 attr->DecRef();
9355 }
9356 }
9357
9358 // ----------------------------------------------------------------------------
9359 // Data type registration
9360 // ----------------------------------------------------------------------------
9361
9362 void wxGrid::RegisterDataType(const wxString& typeName,
9363 wxGridCellRenderer* renderer,
9364 wxGridCellEditor* editor)
9365 {
9366 m_typeRegistry->RegisterDataType(typeName, renderer, editor);
9367 }
9368
9369
9370 wxGridCellEditor* wxGrid::GetDefaultEditorForCell(int row, int col) const
9371 {
9372 wxString typeName = m_table->GetTypeName(row, col);
9373 return GetDefaultEditorForType(typeName);
9374 }
9375
9376 wxGridCellRenderer* wxGrid::GetDefaultRendererForCell(int row, int col) const
9377 {
9378 wxString typeName = m_table->GetTypeName(row, col);
9379 return GetDefaultRendererForType(typeName);
9380 }
9381
9382 wxGridCellEditor*
9383 wxGrid::GetDefaultEditorForType(const wxString& typeName) const
9384 {
9385 int index = m_typeRegistry->FindOrCloneDataType(typeName);
9386 if ( index == wxNOT_FOUND )
9387 {
9388 wxFAIL_MSG(wxT("Unknown data type name"));
9389
9390 return NULL;
9391 }
9392
9393 return m_typeRegistry->GetEditor(index);
9394 }
9395
9396 wxGridCellRenderer*
9397 wxGrid::GetDefaultRendererForType(const wxString& typeName) const
9398 {
9399 int index = m_typeRegistry->FindOrCloneDataType(typeName);
9400 if ( index == wxNOT_FOUND )
9401 {
9402 wxFAIL_MSG(wxT("Unknown data type name"));
9403
9404 return NULL;
9405 }
9406
9407 return m_typeRegistry->GetRenderer(index);
9408 }
9409
9410
9411 // ----------------------------------------------------------------------------
9412 // row/col size
9413 // ----------------------------------------------------------------------------
9414
9415 void wxGrid::EnableDragRowSize( bool enable )
9416 {
9417 m_canDragRowSize = enable;
9418 }
9419
9420
9421 void wxGrid::EnableDragColSize( bool enable )
9422 {
9423 m_canDragColSize = enable;
9424 }
9425
9426 void wxGrid::EnableDragGridSize( bool enable )
9427 {
9428 m_canDragGridSize = enable;
9429 }
9430
9431 void wxGrid::EnableDragCell( bool enable )
9432 {
9433 m_canDragCell = enable;
9434 }
9435
9436 void wxGrid::SetDefaultRowSize( int height, bool resizeExistingRows )
9437 {
9438 m_defaultRowHeight = wxMax( height, m_minAcceptableRowHeight );
9439
9440 if ( resizeExistingRows )
9441 {
9442 // since we are resizing all rows to the default row size,
9443 // we can simply clear the row heights and row bottoms
9444 // arrays (which also allows us to take advantage of
9445 // some speed optimisations)
9446 m_rowHeights.Empty();
9447 m_rowBottoms.Empty();
9448 if ( !GetBatchCount() )
9449 CalcDimensions();
9450 }
9451 }
9452
9453 void wxGrid::SetRowSize( int row, int height )
9454 {
9455 wxCHECK_RET( row >= 0 && row < m_numRows, _T("invalid row index") );
9456
9457 // See comment in SetColSize
9458 if ( height < GetRowMinimalAcceptableHeight()) { return; }
9459
9460 if ( m_rowHeights.IsEmpty() )
9461 {
9462 // need to really create the array
9463 InitRowHeights();
9464 }
9465
9466 int h = wxMax( 0, height );
9467 int diff = h - m_rowHeights[row];
9468
9469 m_rowHeights[row] = h;
9470 int i;
9471 for ( i = row; i < m_numRows; i++ )
9472 {
9473 m_rowBottoms[i] += diff;
9474 }
9475 if ( !GetBatchCount() )
9476 CalcDimensions();
9477 }
9478
9479 void wxGrid::SetDefaultColSize( int width, bool resizeExistingCols )
9480 {
9481 m_defaultColWidth = wxMax( width, m_minAcceptableColWidth );
9482
9483 if ( resizeExistingCols )
9484 {
9485 // since we are resizing all columns to the default column size,
9486 // we can simply clear the col widths and col rights
9487 // arrays (which also allows us to take advantage of
9488 // some speed optimisations)
9489 m_colWidths.Empty();
9490 m_colRights.Empty();
9491 if ( !GetBatchCount() )
9492 CalcDimensions();
9493 }
9494 }
9495
9496 void wxGrid::SetColSize( int col, int width )
9497 {
9498 wxCHECK_RET( col >= 0 && col < m_numCols, _T("invalid column index") );
9499
9500 // should we check that it's bigger than GetColMinimalWidth(col) here?
9501 // (VZ)
9502 // No, because it is reasonable to assume the library user know's
9503 // what he is doing. However whe should test against the weaker
9504 // constariant of minimalAcceptableWidth, as this breaks rendering
9505 //
9506 // This test then fixes sf.net bug #645734
9507
9508 if ( width < GetColMinimalAcceptableWidth()) { return; }
9509
9510 if ( m_colWidths.IsEmpty() )
9511 {
9512 // need to really create the array
9513 InitColWidths();
9514 }
9515
9516 // if < 0 calc new width from label
9517 if( width < 0 )
9518 {
9519 long w, h;
9520 wxArrayString lines;
9521 wxClientDC dc(m_colLabelWin);
9522 dc.SetFont(GetLabelFont());
9523 StringToLines(GetColLabelValue(col), lines);
9524 GetTextBoxSize(dc, lines, &w, &h);
9525 width = w + 6;
9526 }
9527 int w = wxMax( 0, width );
9528 int diff = w - m_colWidths[col];
9529 m_colWidths[col] = w;
9530
9531 int i;
9532 for ( i = col; i < m_numCols; i++ )
9533 {
9534 m_colRights[i] += diff;
9535 }
9536 if ( !GetBatchCount() )
9537 CalcDimensions();
9538 }
9539
9540
9541 void wxGrid::SetColMinimalWidth( int col, int width )
9542 {
9543 if (width > GetColMinimalAcceptableWidth()) {
9544 wxLongToLongHashMap::key_type key = (wxLongToLongHashMap::key_type)col;
9545 m_colMinWidths[key] = width;
9546 }
9547 }
9548
9549 void wxGrid::SetRowMinimalHeight( int row, int width )
9550 {
9551 if (width > GetRowMinimalAcceptableHeight()) {
9552 wxLongToLongHashMap::key_type key = (wxLongToLongHashMap::key_type)row;
9553 m_rowMinHeights[key] = width;
9554 }
9555 }
9556
9557 int wxGrid::GetColMinimalWidth(int col) const
9558 {
9559 wxLongToLongHashMap::key_type key = (wxLongToLongHashMap::key_type)col;
9560 wxLongToLongHashMap::const_iterator it = m_colMinWidths.find(key);
9561 return it != m_colMinWidths.end() ? (int)it->second : m_minAcceptableColWidth;
9562 }
9563
9564 int wxGrid::GetRowMinimalHeight(int row) const
9565 {
9566 wxLongToLongHashMap::key_type key = (wxLongToLongHashMap::key_type)row;
9567 wxLongToLongHashMap::const_iterator it = m_rowMinHeights.find(key);
9568 return it != m_rowMinHeights.end() ? (int)it->second : m_minAcceptableRowHeight;
9569 }
9570
9571 void wxGrid::SetColMinimalAcceptableWidth( int width )
9572 {
9573 // We do allow a width of 0 since this gives us
9574 // an easy way to temporarily hidding columns.
9575 if ( width<0 )
9576 return;
9577 m_minAcceptableColWidth = width;
9578 }
9579
9580 void wxGrid::SetRowMinimalAcceptableHeight( int height )
9581 {
9582 // We do allow a height of 0 since this gives us
9583 // an easy way to temporarily hidding rows.
9584 if ( height<0 )
9585 return;
9586 m_minAcceptableRowHeight = height;
9587 };
9588
9589 int wxGrid::GetColMinimalAcceptableWidth() const
9590 {
9591 return m_minAcceptableColWidth;
9592 }
9593
9594 int wxGrid::GetRowMinimalAcceptableHeight() const
9595 {
9596 return m_minAcceptableRowHeight;
9597 }
9598
9599 // ----------------------------------------------------------------------------
9600 // auto sizing
9601 // ----------------------------------------------------------------------------
9602
9603 void wxGrid::AutoSizeColOrRow( int colOrRow, bool setAsMin, bool column )
9604 {
9605 wxClientDC dc(m_gridWin);
9606
9607 //Cancel editting of cell
9608 HideCellEditControl();
9609 SaveEditControlValue();
9610
9611 // init both of them to avoid compiler warnings, even if weo nly need one
9612 int row = -1,
9613 col = -1;
9614 if ( column )
9615 col = colOrRow;
9616 else
9617 row = colOrRow;
9618
9619 wxCoord extent, extentMax = 0;
9620 int max = column ? m_numRows : m_numCols;
9621 for ( int rowOrCol = 0; rowOrCol < max; rowOrCol++ )
9622 {
9623 if ( column )
9624 row = rowOrCol;
9625 else
9626 col = rowOrCol;
9627
9628 wxGridCellAttr* attr = GetCellAttr(row, col);
9629 wxGridCellRenderer* renderer = attr->GetRenderer(this, row, col);
9630 if ( renderer )
9631 {
9632 wxSize size = renderer->GetBestSize(*this, *attr, dc, row, col);
9633 extent = column ? size.x : size.y;
9634 if ( extent > extentMax )
9635 {
9636 extentMax = extent;
9637 }
9638
9639 renderer->DecRef();
9640 }
9641
9642 attr->DecRef();
9643 }
9644
9645 // now also compare with the column label extent
9646 wxCoord w, h;
9647 dc.SetFont( GetLabelFont() );
9648
9649 if ( column )
9650 {
9651 dc.GetTextExtent( GetColLabelValue(col), &w, &h );
9652 if( GetColLabelTextOrientation() == wxVERTICAL )
9653 w = h;
9654 }
9655 else
9656 dc.GetTextExtent( GetRowLabelValue(row), &w, &h );
9657
9658 extent = column ? w : h;
9659 if ( extent > extentMax )
9660 {
9661 extentMax = extent;
9662 }
9663
9664 if ( !extentMax )
9665 {
9666 // empty column - give default extent (notice that if extentMax is less
9667 // than default extent but != 0, it's ok)
9668 extentMax = column ? m_defaultColWidth : m_defaultRowHeight;
9669 }
9670 else
9671 {
9672 if ( column )
9673 {
9674 // leave some space around text
9675 extentMax += 10;
9676 }
9677 else
9678 {
9679 extentMax += 6;
9680 }
9681 }
9682
9683 if ( column )
9684 {
9685 SetColSize(col, extentMax);
9686 if ( !GetBatchCount() )
9687 {
9688 int cw, ch, dummy;
9689 m_gridWin->GetClientSize( &cw, &ch );
9690 wxRect rect ( CellToRect( 0, col ) );
9691 rect.y = 0;
9692 CalcScrolledPosition(rect.x, 0, &rect.x, &dummy);
9693 rect.width = cw - rect.x;
9694 rect.height = m_colLabelHeight;
9695 m_colLabelWin->Refresh( true, &rect );
9696 }
9697 }
9698 else
9699 {
9700 SetRowSize(row, extentMax);
9701 if ( !GetBatchCount() )
9702 {
9703 int cw, ch, dummy;
9704 m_gridWin->GetClientSize( &cw, &ch );
9705 wxRect rect ( CellToRect( row, 0 ) );
9706 rect.x = 0;
9707 CalcScrolledPosition(0, rect.y, &dummy, &rect.y);
9708 rect.width = m_rowLabelWidth;
9709 rect.height = ch - rect.y;
9710 m_rowLabelWin->Refresh( true, &rect );
9711 }
9712 }
9713 if ( setAsMin )
9714 {
9715 if ( column )
9716 SetColMinimalWidth(col, extentMax);
9717 else
9718 SetRowMinimalHeight(row, extentMax);
9719 }
9720 }
9721
9722 int wxGrid::SetOrCalcColumnSizes(bool calcOnly, bool setAsMin)
9723 {
9724 int width = m_rowLabelWidth;
9725
9726 if ( !calcOnly )
9727 BeginBatch();
9728
9729 for ( int col = 0; col < m_numCols; col++ )
9730 {
9731 if ( !calcOnly )
9732 {
9733 AutoSizeColumn(col, setAsMin);
9734 }
9735
9736 width += GetColWidth(col);
9737 }
9738
9739 if ( !calcOnly )
9740 EndBatch();
9741
9742 return width;
9743 }
9744
9745 int wxGrid::SetOrCalcRowSizes(bool calcOnly, bool setAsMin)
9746 {
9747 int height = m_colLabelHeight;
9748
9749 if ( !calcOnly )
9750 BeginBatch();
9751
9752 for ( int row = 0; row < m_numRows; row++ )
9753 {
9754 if ( !calcOnly )
9755 {
9756 AutoSizeRow(row, setAsMin);
9757 }
9758
9759 height += GetRowHeight(row);
9760 }
9761
9762 if ( !calcOnly )
9763 EndBatch();
9764
9765 return height;
9766 }
9767
9768 void wxGrid::AutoSize()
9769 {
9770 BeginBatch();
9771
9772 wxSize size(SetOrCalcColumnSizes(false), SetOrCalcRowSizes(false));
9773
9774 // round up the size to a multiple of scroll step - this ensures that we
9775 // won't get the scrollbars if we're sized exactly to this width
9776 // CalcDimension adds m_extraWidth + 1 etc. to calculate the necessary
9777 // scrollbar steps
9778 wxSize sizeFit(GetScrollX(size.x + m_extraWidth + 1) * GRID_SCROLL_LINE_X,
9779 GetScrollY(size.y + m_extraHeight + 1) * GRID_SCROLL_LINE_Y);
9780
9781 // distribute the extra space between the columns/rows to avoid having
9782 // extra white space
9783
9784 // Remove the extra m_extraWidth + 1 added above
9785 wxCoord diff = sizeFit.x - size.x + (m_extraWidth + 1);
9786 if ( diff && m_numCols )
9787 {
9788 // try to resize the columns uniformly
9789 wxCoord diffPerCol = diff / m_numCols;
9790 if ( diffPerCol )
9791 {
9792 for ( int col = 0; col < m_numCols; col++ )
9793 {
9794 SetColSize(col, GetColWidth(col) + diffPerCol);
9795 }
9796 }
9797
9798 // add remaining amount to the last columns
9799 diff -= diffPerCol * m_numCols;
9800 if ( diff )
9801 {
9802 for ( int col = m_numCols - 1; col >= m_numCols - diff; col-- )
9803 {
9804 SetColSize(col, GetColWidth(col) + 1);
9805 }
9806 }
9807 }
9808
9809 // same for rows
9810 diff = sizeFit.y - size.y - (m_extraHeight + 1);
9811 if ( diff && m_numRows )
9812 {
9813 // try to resize the columns uniformly
9814 wxCoord diffPerRow = diff / m_numRows;
9815 if ( diffPerRow )
9816 {
9817 for ( int row = 0; row < m_numRows; row++ )
9818 {
9819 SetRowSize(row, GetRowHeight(row) + diffPerRow);
9820 }
9821 }
9822
9823 // add remaining amount to the last rows
9824 diff -= diffPerRow * m_numRows;
9825 if ( diff )
9826 {
9827 for ( int row = m_numRows - 1; row >= m_numRows - diff; row-- )
9828 {
9829 SetRowSize(row, GetRowHeight(row) + 1);
9830 }
9831 }
9832 }
9833
9834 EndBatch();
9835
9836 SetClientSize(sizeFit);
9837 }
9838
9839 void wxGrid::AutoSizeRowLabelSize( int row )
9840 {
9841 wxArrayString lines;
9842 long w, h;
9843
9844 // Hide the edit control, so it
9845 // won't interfer with drag-shrinking.
9846 if( IsCellEditControlShown() )
9847 {
9848 HideCellEditControl();
9849 SaveEditControlValue();
9850 }
9851
9852 // autosize row height depending on label text
9853 StringToLines( GetRowLabelValue( row ), lines );
9854 wxClientDC dc( m_rowLabelWin );
9855 GetTextBoxSize( dc, lines, &w, &h);
9856 if( h < m_defaultRowHeight )
9857 h = m_defaultRowHeight;
9858 SetRowSize(row, h);
9859 ForceRefresh();
9860 }
9861
9862 void wxGrid::AutoSizeColLabelSize( int col )
9863 {
9864 wxArrayString lines;
9865 long w, h;
9866
9867 // Hide the edit control, so it
9868 // won't interfer with drag-shrinking.
9869 if( IsCellEditControlShown() )
9870 {
9871 HideCellEditControl();
9872 SaveEditControlValue();
9873 }
9874
9875 // autosize column width depending on label text
9876 StringToLines( GetColLabelValue( col ), lines );
9877 wxClientDC dc( m_colLabelWin );
9878 if( GetColLabelTextOrientation() == wxHORIZONTAL )
9879 GetTextBoxSize( dc, lines, &w, &h);
9880 else
9881 GetTextBoxSize( dc, lines, &h, &w);
9882 if( w < m_defaultColWidth )
9883 w = m_defaultColWidth;
9884 SetColSize(col, w);
9885 ForceRefresh();
9886 }
9887
9888 wxSize wxGrid::DoGetBestSize() const
9889 {
9890 // don't set sizes, only calculate them
9891 wxGrid *self = (wxGrid *)this; // const_cast
9892
9893 int width, height;
9894 width = self->SetOrCalcColumnSizes(true);
9895 height = self->SetOrCalcRowSizes(true);
9896
9897 if (!width) width=100;
9898 if (!height) height=80;
9899
9900 // Round up to a multiple the scroll rate NOTE: this still doesn't get rid
9901 // of the scrollbars, is there any magic incantaion for that?
9902 int xpu, ypu;
9903 GetScrollPixelsPerUnit(&xpu, &ypu);
9904 if (xpu)
9905 width += 1 + xpu - (width % xpu);
9906 if (ypu)
9907 height += 1 + ypu - (height % ypu);
9908
9909 // limit to 1/4 of the screen size
9910 int maxwidth, maxheight;
9911 wxDisplaySize( & maxwidth, & maxheight );
9912 maxwidth /= 2;
9913 maxheight /= 2;
9914 if ( width > maxwidth ) width = maxwidth;
9915 if ( height > maxheight ) height = maxheight;
9916
9917
9918 wxSize best(width, height);
9919 // NOTE: This size should be cached, but first we need to add calls to
9920 // InvalidateBestSize everywhere that could change the results of this
9921 // calculation.
9922 // CacheBestSize(size);
9923 return best;
9924 }
9925
9926 void wxGrid::Fit()
9927 {
9928 AutoSize();
9929 }
9930
9931
9932 wxPen& wxGrid::GetDividerPen() const
9933 {
9934 return wxNullPen;
9935 }
9936
9937 // ----------------------------------------------------------------------------
9938 // cell value accessor functions
9939 // ----------------------------------------------------------------------------
9940
9941 void wxGrid::SetCellValue( int row, int col, const wxString& s )
9942 {
9943 if ( m_table )
9944 {
9945 m_table->SetValue( row, col, s );
9946 if ( !GetBatchCount() )
9947 {
9948 int dummy;
9949 wxRect rect( CellToRect( row, col ) );
9950 rect.x = 0;
9951 rect.width = m_gridWin->GetClientSize().GetWidth();
9952 CalcScrolledPosition(0, rect.y, &dummy, &rect.y);
9953 m_gridWin->Refresh( false, &rect );
9954 }
9955
9956 if ( m_currentCellCoords.GetRow() == row &&
9957 m_currentCellCoords.GetCol() == col &&
9958 IsCellEditControlShown())
9959 // Note: If we are using IsCellEditControlEnabled,
9960 // this interacts badly with calling SetCellValue from
9961 // an EVT_GRID_CELL_CHANGE handler.
9962 {
9963 HideCellEditControl();
9964 ShowCellEditControl(); // will reread data from table
9965 }
9966 }
9967 }
9968
9969
9970 //
9971 // ------ Block, row and col selection
9972 //
9973
9974 void wxGrid::SelectRow( int row, bool addToSelected )
9975 {
9976 if ( IsSelection() && !addToSelected )
9977 ClearSelection();
9978
9979 if ( m_selection )
9980 m_selection->SelectRow( row, false, addToSelected );
9981 }
9982
9983
9984 void wxGrid::SelectCol( int col, bool addToSelected )
9985 {
9986 if ( IsSelection() && !addToSelected )
9987 ClearSelection();
9988
9989 if ( m_selection )
9990 m_selection->SelectCol( col, false, addToSelected );
9991 }
9992
9993
9994 void wxGrid::SelectBlock( int topRow, int leftCol, int bottomRow, int rightCol,
9995 bool addToSelected )
9996 {
9997 if ( IsSelection() && !addToSelected )
9998 ClearSelection();
9999
10000 if ( m_selection )
10001 m_selection->SelectBlock( topRow, leftCol, bottomRow, rightCol,
10002 false, addToSelected );
10003 }
10004
10005
10006 void wxGrid::SelectAll()
10007 {
10008 if ( m_numRows > 0 && m_numCols > 0 )
10009 {
10010 if ( m_selection )
10011 m_selection->SelectBlock( 0, 0, m_numRows-1, m_numCols-1 );
10012 }
10013 }
10014
10015 //
10016 // ------ Cell, row and col deselection
10017 //
10018
10019 void wxGrid::DeselectRow( int row )
10020 {
10021 if ( !m_selection )
10022 return;
10023
10024 if ( m_selection->GetSelectionMode() == wxGrid::wxGridSelectRows )
10025 {
10026 if ( m_selection->IsInSelection(row, 0 ) )
10027 m_selection->ToggleCellSelection( row, 0);
10028 }
10029 else
10030 {
10031 int nCols = GetNumberCols();
10032 for ( int i = 0; i < nCols ; i++ )
10033 {
10034 if ( m_selection->IsInSelection(row, i ) )
10035 m_selection->ToggleCellSelection( row, i);
10036 }
10037 }
10038 }
10039
10040 void wxGrid::DeselectCol( int col )
10041 {
10042 if ( !m_selection )
10043 return;
10044
10045 if ( m_selection->GetSelectionMode() == wxGrid::wxGridSelectColumns )
10046 {
10047 if ( m_selection->IsInSelection(0, col ) )
10048 m_selection->ToggleCellSelection( 0, col);
10049 }
10050 else
10051 {
10052 int nRows = GetNumberRows();
10053 for ( int i = 0; i < nRows ; i++ )
10054 {
10055 if ( m_selection->IsInSelection(i, col ) )
10056 m_selection->ToggleCellSelection(i, col);
10057 }
10058 }
10059 }
10060
10061 void wxGrid::DeselectCell( int row, int col )
10062 {
10063 if ( m_selection && m_selection->IsInSelection(row, col) )
10064 m_selection->ToggleCellSelection(row, col);
10065 }
10066
10067 bool wxGrid::IsSelection()
10068 {
10069 return ( m_selection && (m_selection->IsSelection() ||
10070 ( m_selectingTopLeft != wxGridNoCellCoords &&
10071 m_selectingBottomRight != wxGridNoCellCoords) ) );
10072 }
10073
10074 bool wxGrid::IsInSelection( int row, int col ) const
10075 {
10076 return ( m_selection && (m_selection->IsInSelection( row, col ) ||
10077 ( row >= m_selectingTopLeft.GetRow() &&
10078 col >= m_selectingTopLeft.GetCol() &&
10079 row <= m_selectingBottomRight.GetRow() &&
10080 col <= m_selectingBottomRight.GetCol() )) );
10081 }
10082
10083 wxGridCellCoordsArray wxGrid::GetSelectedCells() const
10084 {
10085 if (!m_selection) { wxGridCellCoordsArray a; return a; }
10086 return m_selection->m_cellSelection;
10087 }
10088 wxGridCellCoordsArray wxGrid::GetSelectionBlockTopLeft() const
10089 {
10090 if (!m_selection) { wxGridCellCoordsArray a; return a; }
10091 return m_selection->m_blockSelectionTopLeft;
10092 }
10093 wxGridCellCoordsArray wxGrid::GetSelectionBlockBottomRight() const
10094 {
10095 if (!m_selection) { wxGridCellCoordsArray a; return a; }
10096 return m_selection->m_blockSelectionBottomRight;
10097 }
10098 wxArrayInt wxGrid::GetSelectedRows() const
10099 {
10100 if (!m_selection) { wxArrayInt a; return a; }
10101 return m_selection->m_rowSelection;
10102 }
10103 wxArrayInt wxGrid::GetSelectedCols() const
10104 {
10105 if (!m_selection) { wxArrayInt a; return a; }
10106 return m_selection->m_colSelection;
10107 }
10108
10109
10110 void wxGrid::ClearSelection()
10111 {
10112 m_selectingTopLeft = wxGridNoCellCoords;
10113 m_selectingBottomRight = wxGridNoCellCoords;
10114 if ( m_selection )
10115 m_selection->ClearSelection();
10116 }
10117
10118
10119 // This function returns the rectangle that encloses the given block
10120 // in device coords clipped to the client size of the grid window.
10121 //
10122 wxRect wxGrid::BlockToDeviceRect( const wxGridCellCoords &topLeft,
10123 const wxGridCellCoords &bottomRight )
10124 {
10125 wxRect rect( wxGridNoCellRect );
10126 wxRect cellRect;
10127
10128 cellRect = CellToRect( topLeft );
10129 if ( cellRect != wxGridNoCellRect )
10130 {
10131 rect = cellRect;
10132 }
10133 else
10134 {
10135 rect = wxRect( 0, 0, 0, 0 );
10136 }
10137
10138 cellRect = CellToRect( bottomRight );
10139 if ( cellRect != wxGridNoCellRect )
10140 {
10141 rect += cellRect;
10142 }
10143 else
10144 {
10145 return wxGridNoCellRect;
10146 }
10147
10148 int i, j;
10149 int left = rect.GetLeft();
10150 int top = rect.GetTop();
10151 int right = rect.GetRight();
10152 int bottom = rect.GetBottom();
10153
10154 int leftCol = topLeft.GetCol();
10155 int topRow = topLeft.GetRow();
10156 int rightCol = bottomRight.GetCol();
10157 int bottomRow = bottomRight.GetRow();
10158
10159 if (left > right)
10160 {
10161 i = left;
10162 left = right;
10163 right = i;
10164 i = leftCol;
10165 leftCol=rightCol;
10166 rightCol = i;
10167 }
10168
10169 if (top > bottom)
10170 {
10171 i = top;
10172 top = bottom;
10173 bottom = i;
10174 i = topRow;
10175 topRow = bottomRow;
10176 bottomRow = i;
10177 }
10178
10179
10180 for ( j = topRow; j <= bottomRow; j++ )
10181 {
10182 for ( i = leftCol; i <= rightCol; i++ )
10183 {
10184 if ((j==topRow) || (j==bottomRow) || (i==leftCol) || (i==rightCol))
10185 {
10186 cellRect = CellToRect( j, i );
10187
10188 if (cellRect.x < left)
10189 left = cellRect.x;
10190 if (cellRect.y < top)
10191 top = cellRect.y;
10192 if (cellRect.x + cellRect.width > right)
10193 right = cellRect.x + cellRect.width;
10194 if (cellRect.y + cellRect.height > bottom)
10195 bottom = cellRect.y + cellRect.height;
10196 }
10197 else i = rightCol; // jump over inner cells.
10198 }
10199 }
10200
10201 // convert to scrolled coords
10202 //
10203 CalcScrolledPosition( left, top, &left, &top );
10204 CalcScrolledPosition( right, bottom, &right, &bottom );
10205
10206 int cw, ch;
10207 m_gridWin->GetClientSize( &cw, &ch );
10208
10209 if (right < 0 || bottom < 0 || left > cw || top > ch)
10210 return wxRect( 0, 0, 0, 0);
10211
10212 rect.SetLeft( wxMax(0, left) );
10213 rect.SetTop( wxMax(0, top) );
10214 rect.SetRight( wxMin(cw, right) );
10215 rect.SetBottom( wxMin(ch, bottom) );
10216
10217 return rect;
10218 }
10219
10220 //
10221 // ------ Grid event classes
10222 //
10223
10224 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent, wxNotifyEvent )
10225
10226 wxGridEvent::wxGridEvent( int id, wxEventType type, wxObject* obj,
10227 int row, int col, int x, int y, bool sel,
10228 bool control, bool shift, bool alt, bool meta )
10229 : wxNotifyEvent( type, id )
10230 {
10231 m_row = row;
10232 m_col = col;
10233 m_x = x;
10234 m_y = y;
10235 m_selecting = sel;
10236 m_control = control;
10237 m_shift = shift;
10238 m_alt = alt;
10239 m_meta = meta;
10240
10241 SetEventObject(obj);
10242 }
10243
10244
10245 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent, wxNotifyEvent )
10246
10247 wxGridSizeEvent::wxGridSizeEvent( int id, wxEventType type, wxObject* obj,
10248 int rowOrCol, int x, int y,
10249 bool control, bool shift, bool alt, bool meta )
10250 : wxNotifyEvent( type, id )
10251 {
10252 m_rowOrCol = rowOrCol;
10253 m_x = x;
10254 m_y = y;
10255 m_control = control;
10256 m_shift = shift;
10257 m_alt = alt;
10258 m_meta = meta;
10259
10260 SetEventObject(obj);
10261 }
10262
10263
10264 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent, wxNotifyEvent )
10265
10266 wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id, wxEventType type, wxObject* obj,
10267 const wxGridCellCoords& topLeft,
10268 const wxGridCellCoords& bottomRight,
10269 bool sel, bool control,
10270 bool shift, bool alt, bool meta )
10271 : wxNotifyEvent( type, id )
10272 {
10273 m_topLeft = topLeft;
10274 m_bottomRight = bottomRight;
10275 m_selecting = sel;
10276 m_control = control;
10277 m_shift = shift;
10278 m_alt = alt;
10279 m_meta = meta;
10280
10281 SetEventObject(obj);
10282 }
10283
10284
10285 IMPLEMENT_DYNAMIC_CLASS(wxGridEditorCreatedEvent, wxCommandEvent)
10286
10287 wxGridEditorCreatedEvent::wxGridEditorCreatedEvent(int id, wxEventType type,
10288 wxObject* obj, int row,
10289 int col, wxControl* ctrl)
10290 : wxCommandEvent(type, id)
10291 {
10292 SetEventObject(obj);
10293 m_row = row;
10294 m_col = col;
10295 m_ctrl = ctrl;
10296 }
10297
10298 #endif // wxUSE_GRID
10299