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