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