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