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