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