]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/generic/grid.cpp
added missing wxListItem copy ctor to wxMSW, moved wxListItem to the header
[wxWidgets.git] / src / generic / grid.cpp
... / ...
CommitLineData
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
76WX_DEFINE_EXPORTED_ARRAY(wxGridCellAttr *, wxArrayAttrs);
77
78struct 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
94WX_DECLARE_EXPORTED_OBJARRAY(wxGridCellWithAttr, wxGridCellWithAttrArray);
95
96#include "wx/arrimpl.cpp"
97
98WX_DEFINE_OBJARRAY(wxGridCellCoordsArray)
99WX_DEFINE_OBJARRAY(wxGridCellWithAttrArray)
100
101// ----------------------------------------------------------------------------
102// events
103// ----------------------------------------------------------------------------
104
105DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_LEFT_CLICK)
106DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_RIGHT_CLICK)
107DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_LEFT_DCLICK)
108DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_RIGHT_DCLICK)
109DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_LEFT_CLICK)
110DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_RIGHT_CLICK)
111DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_LEFT_DCLICK)
112DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_RIGHT_DCLICK)
113DEFINE_EVENT_TYPE(wxEVT_GRID_ROW_SIZE)
114DEFINE_EVENT_TYPE(wxEVT_GRID_COL_SIZE)
115DEFINE_EVENT_TYPE(wxEVT_GRID_RANGE_SELECT)
116DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_CHANGE)
117DEFINE_EVENT_TYPE(wxEVT_GRID_SELECT_CELL)
118DEFINE_EVENT_TYPE(wxEVT_GRID_EDITOR_SHOWN)
119DEFINE_EVENT_TYPE(wxEVT_GRID_EDITOR_HIDDEN)
120DEFINE_EVENT_TYPE(wxEVT_GRID_EDITOR_CREATED)
121
122// ----------------------------------------------------------------------------
123// private classes
124// ----------------------------------------------------------------------------
125
126class WXDLLEXPORT wxGridRowLabelWindow : public wxWindow
127{
128public:
129 wxGridRowLabelWindow() { m_owner = (wxGrid *)NULL; }
130 wxGridRowLabelWindow( wxGrid *parent, wxWindowID id,
131 const wxPoint &pos, const wxSize &size );
132
133private:
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
147class WXDLLEXPORT wxGridColLabelWindow : public wxWindow
148{
149public:
150 wxGridColLabelWindow() { m_owner = (wxGrid *)NULL; }
151 wxGridColLabelWindow( wxGrid *parent, wxWindowID id,
152 const wxPoint &pos, const wxSize &size );
153
154private:
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
168class WXDLLEXPORT wxGridCornerLabelWindow : public wxWindow
169{
170public:
171 wxGridCornerLabelWindow() { m_owner = (wxGrid *)NULL; }
172 wxGridCornerLabelWindow( wxGrid *parent, wxWindowID id,
173 const wxPoint &pos, const wxSize &size );
174
175private:
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
188class WXDLLEXPORT wxGridWindow : public wxWindow
189{
190public:
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
206private:
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
225class wxGridCellEditorEvtHandler : public wxEvtHandler
226{
227public:
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
238private:
239 wxGrid* m_grid;
240 wxGridCellEditor* m_editor;
241 DECLARE_DYNAMIC_CLASS(wxGridCellEditorEvtHandler)
242 DECLARE_EVENT_TABLE()
243};
244
245
246IMPLEMENT_DYNAMIC_CLASS( wxGridCellEditorEvtHandler, wxEvtHandler )
247BEGIN_EVENT_TABLE( wxGridCellEditorEvtHandler, wxEvtHandler )
248 EVT_KEY_DOWN( wxGridCellEditorEvtHandler::OnKeyDown )
249 EVT_CHAR( wxGridCellEditorEvtHandler::OnChar )
250END_EVENT_TABLE()
251
252
253
254// ----------------------------------------------------------------------------
255// the internal data representation used by wxGridCellAttrProvider
256// ----------------------------------------------------------------------------
257
258// this class stores attributes set for cells
259class WXDLLEXPORT wxGridCellAttrData
260{
261public:
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
267private:
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
275class WXDLLEXPORT wxGridRowOrColAttrData
276{
277public:
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
286private:
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
293class WXDLLEXPORT wxGridCellAttrProviderData
294{
295public:
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
306struct 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
326WX_DEFINE_EXPORTED_ARRAY(wxGridDataTypeInfo*, wxGridDataTypeInfoArray);
327
328
329class WXDLLEXPORT wxGridTypeRegistry
330{
331public:
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
354private:
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
380wxGridCellCoords wxGridNoCellCoords( -1, -1 );
381wxRect 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
395static const size_t GRID_SCROLL_LINE_X = 15; // 1;
396static 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)
400static const int GRID_HASH_SIZE = 100;
401
402// ----------------------------------------------------------------------------
403// private functions
404// ----------------------------------------------------------------------------
405
406static inline int GetScrollX(int x)
407{
408 return (x + GRID_SCROLL_LINE_X - 1) / GRID_SCROLL_LINE_X;
409}
410
411static 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
424wxGridCellEditor::wxGridCellEditor()
425{
426 m_control = NULL;
427}
428
429
430wxGridCellEditor::~wxGridCellEditor()
431{
432 Destroy();
433}
434
435void wxGridCellEditor::Create(wxWindow* WXUNUSED(parent),
436 wxWindowID WXUNUSED(id),
437 wxEvtHandler* evtHandler)
438{
439 if ( evtHandler )
440 m_control->PushEventHandler(evtHandler);
441}
442
443void 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
456void 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
467void 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
514void 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
521void wxGridCellEditor::HandleReturn(wxKeyEvent& event)
522{
523 event.Skip();
524}
525
526bool 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
532void wxGridCellEditor::StartingKey(wxKeyEvent& event)
533{
534 event.Skip();
535}
536
537void wxGridCellEditor::StartingClick()
538{
539}
540
541#if wxUSE_TEXTCTRL
542
543// ----------------------------------------------------------------------------
544// wxGridCellTextEditor
545// ----------------------------------------------------------------------------
546
547wxGridCellTextEditor::wxGridCellTextEditor()
548{
549 m_maxChars = 0;
550}
551
552void 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
569void 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
576void 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
618void 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
628void wxGridCellTextEditor::DoBeginEdit(const wxString& startValue)
629{
630 Text()->SetValue(startValue);
631 Text()->SetInsertionPointEnd();
632 Text()->SetFocus();
633}
634
635bool 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
656void wxGridCellTextEditor::Reset()
657{
658 wxASSERT_MSG(m_control,
659 wxT("The wxGridCellEditor must be Created first!"));
660
661 DoReset(m_startValue);
662}
663
664void wxGridCellTextEditor::DoReset(const wxString& startValue)
665{
666 Text()->SetValue(startValue);
667 Text()->SetInsertionPointEnd();
668}
669
670bool 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
709void wxGridCellTextEditor::StartingKey(wxKeyEvent& event)
710{
711 if ( !Text()->EmulateKeyPress(event) )
712 {
713 event.Skip();
714 }
715}
716
717void 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
734void 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
759wxGridCellNumberEditor::wxGridCellNumberEditor(int min, int max)
760{
761 m_min = min;
762 m_max = max;
763}
764
765void 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
790void 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
819bool 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
846void wxGridCellNumberEditor::Reset()
847{
848 if ( HasRange() )
849 {
850 Spin()->SetValue((int)m_valueOld);
851 }
852 else
853 {
854 DoReset(GetString());
855 }
856}
857
858bool 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
892void 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
909void 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
941wxGridCellFloatEditor::wxGridCellFloatEditor(int width, int precision)
942{
943 m_width = width;
944 m_precision = precision;
945}
946
947void 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
958void 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
979bool 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
998void wxGridCellFloatEditor::Reset()
999{
1000 DoReset(GetString());
1001}
1002
1003void 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
1018void 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
1046wxString 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
1067bool 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
1111void 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
1122void 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
1166void 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
1177void 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
1193bool 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
1215void wxGridCellBoolEditor::Reset()
1216{
1217 wxASSERT_MSG(m_control,
1218 wxT("The wxGridCellEditor must be Created first!"));
1219
1220 CBox()->SetValue(m_startValue);
1221}
1222
1223void wxGridCellBoolEditor::StartingClick()
1224{
1225 CBox()->SetValue(!CBox()->GetValue());
1226}
1227
1228bool 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
1259wxGridCellChoiceEditor::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
1274wxGridCellEditor *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
1283void 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
1304void 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
1316void 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
1337bool 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
1352void wxGridCellChoiceEditor::Reset()
1353{
1354 Combo()->SetValue(m_startValue);
1355 Combo()->SetInsertionPointEnd();
1356}
1357
1358void 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
1381void 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
1406void 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
1426void wxGridCellWorker::SetParameters(const wxString& WXUNUSED(params))
1427{
1428 // nothing to do
1429}
1430
1431wxGridCellWorker::~wxGridCellWorker()
1432{
1433}
1434
1435// ============================================================================
1436// renderer classes
1437// ============================================================================
1438
1439// ----------------------------------------------------------------------------
1440// wxGridCellRenderer
1441// ----------------------------------------------------------------------------
1442
1443void 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
1469void 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
1492wxSize 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
1510wxSize 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
1518void 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
1544wxString 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
1560void 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
1582wxSize 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
1594wxGridCellFloatRenderer::wxGridCellFloatRenderer(int width, int precision)
1595{
1596 SetWidth(width);
1597 SetPrecision(precision);
1598}
1599
1600wxGridCellRenderer *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
1610wxString 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
1663void 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
1685wxSize 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
1693void 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
1739wxSize wxGridCellBoolRenderer::ms_sizeCheckMark;
1740
1741// FIXME these checkbox size calculations are really ugly...
1742
1743// between checkmark and box
1744static const wxCoord wxGRID_CHECKMARK_MARGIN = 2;
1745
1746wxSize 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
1774void 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
1835void 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
1849wxGridCellAttr *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
1881void 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
1915const 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
1933const 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
1947const 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
1961void 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
1984wxGridCellRenderer* 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
2032wxGridCellEditor* 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
2083void 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
2109wxGridCellAttr *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
2123void 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
2156void 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
2189int 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
2208wxGridRowOrColAttrData::~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
2217wxGridCellAttr *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
2231void 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
2259void 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
2292wxGridCellAttrProvider::wxGridCellAttrProvider()
2293{
2294 m_data = (wxGridCellAttrProviderData *)NULL;
2295}
2296
2297wxGridCellAttrProvider::~wxGridCellAttrProvider()
2298{
2299 delete m_data;
2300}
2301
2302void wxGridCellAttrProvider::InitData()
2303{
2304 m_data = new wxGridCellAttrProviderData;
2305}
2306
2307wxGridCellAttr *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
2381void 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
2390void wxGridCellAttrProvider::SetRowAttr(wxGridCellAttr *attr, int row)
2391{
2392 if ( !m_data )
2393 InitData();
2394
2395 m_data->m_rowAttrs.SetAttr(attr, row);
2396}
2397
2398void wxGridCellAttrProvider::SetColAttr(wxGridCellAttr *attr, int col)
2399{
2400 if ( !m_data )
2401 InitData();
2402
2403 m_data->m_colAttrs.SetAttr(attr, col);
2404}
2405
2406void 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
2416void 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
2430wxGridTypeRegistry::~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
2438void 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
2457int 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
2471int 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
2528int 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
2566wxGridCellRenderer* wxGridTypeRegistry::GetRenderer(int index)
2567{
2568 wxGridCellRenderer* renderer = m_typeinfo[index]->m_renderer;
2569 if (renderer)
2570 renderer->IncRef();
2571 return renderer;
2572}
2573
2574wxGridCellEditor* 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
2586IMPLEMENT_ABSTRACT_CLASS( wxGridTableBase, wxObject )
2587
2588
2589wxGridTableBase::wxGridTableBase()
2590{
2591 m_view = (wxGrid *) NULL;
2592 m_attrProvider = (wxGridCellAttrProvider *) NULL;
2593}
2594
2595wxGridTableBase::~wxGridTableBase()
2596{
2597 delete m_attrProvider;
2598}
2599
2600void wxGridTableBase::SetAttrProvider(wxGridCellAttrProvider *attrProvider)
2601{
2602 delete m_attrProvider;
2603 m_attrProvider = attrProvider;
2604}
2605
2606bool 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
2616wxGridCellAttr *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
2624void 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
2639void 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
2654void 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
2669bool 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
2677bool 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
2684bool 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
2692bool 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
2700bool 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
2707bool 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
2716wxString 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
2724wxString 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
2751wxString wxGridTableBase::GetTypeName( int WXUNUSED(row), int WXUNUSED(col) )
2752{
2753 return wxGRID_VALUE_STRING;
2754}
2755
2756bool wxGridTableBase::CanGetValueAs( int WXUNUSED(row), int WXUNUSED(col),
2757 const wxString& typeName )
2758{
2759 return typeName == wxGRID_VALUE_STRING;
2760}
2761
2762bool wxGridTableBase::CanSetValueAs( int row, int col, const wxString& typeName )
2763{
2764 return CanGetValueAs(row, col, typeName);
2765}
2766
2767long wxGridTableBase::GetValueAsLong( int WXUNUSED(row), int WXUNUSED(col) )
2768{
2769 return 0;
2770}
2771
2772double wxGridTableBase::GetValueAsDouble( int WXUNUSED(row), int WXUNUSED(col) )
2773{
2774 return 0.0;
2775}
2776
2777bool wxGridTableBase::GetValueAsBool( int WXUNUSED(row), int WXUNUSED(col) )
2778{
2779 return FALSE;
2780}
2781
2782void wxGridTableBase::SetValueAsLong( int WXUNUSED(row), int WXUNUSED(col),
2783 long WXUNUSED(value) )
2784{
2785}
2786
2787void wxGridTableBase::SetValueAsDouble( int WXUNUSED(row), int WXUNUSED(col),
2788 double WXUNUSED(value) )
2789{
2790}
2791
2792void wxGridTableBase::SetValueAsBool( int WXUNUSED(row), int WXUNUSED(col),
2793 bool WXUNUSED(value) )
2794{
2795}
2796
2797
2798void* wxGridTableBase::GetValueAsCustom( int WXUNUSED(row), int WXUNUSED(col),
2799 const wxString& WXUNUSED(typeName) )
2800{
2801 return NULL;
2802}
2803
2804void 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
2816wxGridTableMessage::wxGridTableMessage()
2817{
2818 m_table = (wxGridTableBase *) NULL;
2819 m_id = -1;
2820 m_comInt1 = -1;
2821 m_comInt2 = -1;
2822}
2823
2824wxGridTableMessage::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
2841WX_DEFINE_OBJARRAY(wxGridStringArray)
2842
2843IMPLEMENT_DYNAMIC_CLASS( wxGridStringTable, wxGridTableBase )
2844
2845wxGridStringTable::wxGridStringTable()
2846 : wxGridTableBase()
2847{
2848}
2849
2850wxGridStringTable::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
2870wxGridStringTable::~wxGridStringTable()
2871{
2872}
2873
2874int wxGridStringTable::GetNumberRows()
2875{
2876 return m_data.GetCount();
2877}
2878
2879int wxGridStringTable::GetNumberCols()
2880{
2881 if ( m_data.GetCount() > 0 )
2882 return m_data[0].GetCount();
2883 else
2884 return 0;
2885}
2886
2887wxString 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
2895void 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
2903bool 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
2911void 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
2932bool 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
2969bool 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
3004bool 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
3048bool 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
3081bool 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
3116bool 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
3165wxString 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
3179wxString 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
3193void 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
3208void 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
3228IMPLEMENT_DYNAMIC_CLASS( wxGridRowLabelWindow, wxWindow )
3229
3230BEGIN_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 )
3236END_EVENT_TABLE()
3237
3238wxGridRowLabelWindow::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
3246void 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
3265void wxGridRowLabelWindow::OnMouseEvent( wxMouseEvent& event )
3266{
3267 m_owner->ProcessRowLabelMouseEvent( event );
3268}
3269
3270
3271void 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//
3280void wxGridRowLabelWindow::OnKeyDown( wxKeyEvent& event )
3281{
3282 if ( !m_owner->GetEventHandler()->ProcessEvent( event ) ) event.Skip();
3283}
3284
3285void wxGridRowLabelWindow::OnKeyUp( wxKeyEvent& event )
3286{
3287 if ( !m_owner->GetEventHandler()->ProcessEvent( event ) ) event.Skip();
3288}
3289
3290
3291
3292//////////////////////////////////////////////////////////////////////
3293
3294IMPLEMENT_DYNAMIC_CLASS( wxGridColLabelWindow, wxWindow )
3295
3296BEGIN_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 )
3302END_EVENT_TABLE()
3303
3304wxGridColLabelWindow::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
3312void 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
3331void wxGridColLabelWindow::OnMouseEvent( wxMouseEvent& event )
3332{
3333 m_owner->ProcessColLabelMouseEvent( event );
3334}
3335
3336void 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//
3345void wxGridColLabelWindow::OnKeyDown( wxKeyEvent& event )
3346{
3347 if ( !m_owner->GetEventHandler()->ProcessEvent( event ) ) event.Skip();
3348}
3349
3350void wxGridColLabelWindow::OnKeyUp( wxKeyEvent& event )
3351{
3352 if ( !m_owner->GetEventHandler()->ProcessEvent( event ) ) event.Skip();
3353}
3354
3355
3356
3357//////////////////////////////////////////////////////////////////////
3358
3359IMPLEMENT_DYNAMIC_CLASS( wxGridCornerLabelWindow, wxWindow )
3360
3361BEGIN_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 )
3367END_EVENT_TABLE()
3368
3369wxGridCornerLabelWindow::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
3377void 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
3395void wxGridCornerLabelWindow::OnMouseEvent( wxMouseEvent& event )
3396{
3397 m_owner->ProcessCornerLabelMouseEvent( event );
3398}
3399
3400
3401void 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//
3409void wxGridCornerLabelWindow::OnKeyDown( wxKeyEvent& event )
3410{
3411 if ( !m_owner->GetEventHandler()->ProcessEvent( event ) ) event.Skip();
3412}
3413
3414void wxGridCornerLabelWindow::OnKeyUp( wxKeyEvent& event )
3415{
3416 if ( !m_owner->GetEventHandler()->ProcessEvent( event ) ) event.Skip();
3417}
3418
3419
3420
3421//////////////////////////////////////////////////////////////////////
3422
3423IMPLEMENT_DYNAMIC_CLASS( wxGridWindow, wxWindow )
3424
3425BEGIN_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 )
3432END_EVENT_TABLE()
3433
3434wxGridWindow::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
3447wxGridWindow::~wxGridWindow()
3448{
3449}
3450
3451
3452void 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
3467void 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
3475void wxGridWindow::OnMouseEvent( wxMouseEvent& event )
3476{
3477 m_owner->ProcessGridCellMouseEvent( event );
3478}
3479
3480void 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//
3488void wxGridWindow::OnKeyDown( wxKeyEvent& event )
3489{
3490 if ( !m_owner->GetEventHandler()->ProcessEvent( event ) ) event.Skip();
3491}
3492
3493void wxGridWindow::OnKeyUp( wxKeyEvent& event )
3494{
3495 if ( !m_owner->GetEventHandler()->ProcessEvent( event ) ) event.Skip();
3496}
3497
3498void wxGridWindow::OnEraseBackground( wxEraseEvent& WXUNUSED(event) )
3499{
3500}
3501
3502
3503//////////////////////////////////////////////////////////////////////
3504
3505// Internal Helper function for computing row or column from some
3506// (unscrolled) coordinate value, using either
3507// m_defaultRowHeight/m_defaultColWidth or binary search on array
3508// of m_rowBottoms/m_ColRights to speed up the search!
3509
3510// Internal helper macros for simpler use of that function
3511
3512static int CoordToRowOrCol(int coord, int defaultDist, int minDist,
3513 const wxArrayInt& BorderArray, bool maxOnOverflow);
3514
3515#define internalXToCol(x) CoordToRowOrCol(x, m_defaultColWidth, \
3516 WXGRID_MIN_COL_WIDTH, \
3517 m_colRights, TRUE)
3518#define internalYToRow(y) CoordToRowOrCol(y, m_defaultRowHeight, \
3519 WXGRID_MIN_ROW_HEIGHT, \
3520 m_rowBottoms, TRUE)
3521/////////////////////////////////////////////////////////////////////
3522
3523IMPLEMENT_DYNAMIC_CLASS( wxGrid, wxScrolledWindow )
3524
3525BEGIN_EVENT_TABLE( wxGrid, wxScrolledWindow )
3526 EVT_PAINT( wxGrid::OnPaint )
3527 EVT_SIZE( wxGrid::OnSize )
3528 EVT_KEY_DOWN( wxGrid::OnKeyDown )
3529 EVT_KEY_UP( wxGrid::OnKeyUp )
3530 EVT_ERASE_BACKGROUND( wxGrid::OnEraseBackground )
3531END_EVENT_TABLE()
3532
3533wxGrid::wxGrid( wxWindow *parent,
3534 wxWindowID id,
3535 const wxPoint& pos,
3536 const wxSize& size,
3537 long style,
3538 const wxString& name )
3539 : wxScrolledWindow( parent, id, pos, size, (style | wxWANTS_CHARS), name ),
3540 m_colMinWidths(GRID_HASH_SIZE),
3541 m_rowMinHeights(GRID_HASH_SIZE)
3542{
3543 Create();
3544}
3545
3546
3547wxGrid::~wxGrid()
3548{
3549 // Must do this or ~wxScrollHelper will pop the wrong event handler
3550 SetTargetWindow(this);
3551 ClearAttrCache();
3552 wxSafeDecRef(m_defaultCellAttr);
3553
3554#ifdef DEBUG_ATTR_CACHE
3555 size_t total = gs_nAttrCacheHits + gs_nAttrCacheMisses;
3556 wxPrintf(_T("wxGrid attribute cache statistics: "
3557 "total: %u, hits: %u (%u%%)\n"),
3558 total, gs_nAttrCacheHits,
3559 total ? (gs_nAttrCacheHits*100) / total : 0);
3560#endif
3561
3562 if (m_ownTable)
3563 delete m_table;
3564
3565 delete m_typeRegistry;
3566 delete m_selection;
3567}
3568
3569
3570//
3571// ----- internal init and update functions
3572//
3573
3574void wxGrid::Create()
3575{
3576 m_created = FALSE; // set to TRUE by CreateGrid
3577
3578 m_table = (wxGridTableBase *) NULL;
3579 m_ownTable = FALSE;
3580
3581 m_cellEditCtrlEnabled = FALSE;
3582
3583 m_defaultCellAttr = new wxGridCellAttr();
3584
3585 // Set default cell attributes
3586 m_defaultCellAttr->SetDefAttr(m_defaultCellAttr);
3587 m_defaultCellAttr->SetKind(wxGridCellAttr::Default);
3588 m_defaultCellAttr->SetFont(GetFont());
3589 m_defaultCellAttr->SetAlignment(wxALIGN_LEFT, wxALIGN_TOP);
3590 m_defaultCellAttr->SetTextColour(
3591 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT));
3592 m_defaultCellAttr->SetBackgroundColour(
3593 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW));
3594 m_defaultCellAttr->SetRenderer(new wxGridCellStringRenderer);
3595 m_defaultCellAttr->SetEditor(new wxGridCellTextEditor);
3596
3597
3598 m_numRows = 0;
3599 m_numCols = 0;
3600 m_currentCellCoords = wxGridNoCellCoords;
3601
3602 m_rowLabelWidth = WXGRID_DEFAULT_ROW_LABEL_WIDTH;
3603 m_colLabelHeight = WXGRID_DEFAULT_COL_LABEL_HEIGHT;
3604
3605 // create the type registry
3606 m_typeRegistry = new wxGridTypeRegistry;
3607 m_selection = 0;
3608 // subwindow components that make up the wxGrid
3609 m_cornerLabelWin = new wxGridCornerLabelWindow( this,
3610 -1,
3611 wxDefaultPosition,
3612 wxDefaultSize );
3613
3614 m_rowLabelWin = new wxGridRowLabelWindow( this,
3615 -1,
3616 wxDefaultPosition,
3617 wxDefaultSize );
3618
3619 m_colLabelWin = new wxGridColLabelWindow( this,
3620 -1,
3621 wxDefaultPosition,
3622 wxDefaultSize );
3623
3624 m_gridWin = new wxGridWindow( this,
3625 m_rowLabelWin,
3626 m_colLabelWin,
3627 -1,
3628 wxDefaultPosition,
3629 wxDefaultSize );
3630
3631 SetTargetWindow( m_gridWin );
3632
3633 Init();
3634}
3635
3636
3637bool wxGrid::CreateGrid( int numRows, int numCols,
3638 wxGrid::wxGridSelectionModes selmode )
3639{
3640 wxCHECK_MSG( !m_created,
3641 FALSE,
3642 wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
3643
3644 m_numRows = numRows;
3645 m_numCols = numCols;
3646
3647 m_table = new wxGridStringTable( m_numRows, m_numCols );
3648 m_table->SetView( this );
3649 m_ownTable = TRUE;
3650 m_selection = new wxGridSelection( this, selmode );
3651
3652 CalcDimensions();
3653
3654 m_created = TRUE;
3655
3656 return m_created;
3657}
3658
3659void wxGrid::SetSelectionMode(wxGrid::wxGridSelectionModes selmode)
3660{
3661 wxCHECK_RET( m_created,
3662 wxT("Called wxGrid::SetSelectionMode() before calling CreateGrid()") );
3663
3664 m_selection->SetSelectionMode( selmode );
3665}
3666
3667bool wxGrid::SetTable( wxGridTableBase *table, bool takeOwnership,
3668 wxGrid::wxGridSelectionModes selmode )
3669{
3670 if ( m_created )
3671 {
3672 // RD: Actually, this should probably be allowed. I think it would be
3673 // nice to be able to switch multiple Tables in and out of a single
3674 // View at runtime. Is there anything in the implmentation that would
3675 // prevent this?
3676
3677 // At least, you now have to cope with m_selection
3678 wxFAIL_MSG( wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
3679 return FALSE;
3680 }
3681 else
3682 {
3683 m_numRows = table->GetNumberRows();
3684 m_numCols = table->GetNumberCols();
3685
3686 m_table = table;
3687 m_table->SetView( this );
3688 if (takeOwnership)
3689 m_ownTable = TRUE;
3690 m_selection = new wxGridSelection( this, selmode );
3691
3692 CalcDimensions();
3693
3694 m_created = TRUE;
3695 }
3696
3697 return m_created;
3698}
3699
3700
3701void wxGrid::Init()
3702{
3703 m_rowLabelWidth = WXGRID_DEFAULT_ROW_LABEL_WIDTH;
3704 m_colLabelHeight = WXGRID_DEFAULT_COL_LABEL_HEIGHT;
3705
3706 if ( m_rowLabelWin )
3707 {
3708 m_labelBackgroundColour = m_rowLabelWin->GetBackgroundColour();
3709 }
3710 else
3711 {
3712 m_labelBackgroundColour = wxColour( _T("WHITE") );
3713 }
3714
3715 m_labelTextColour = wxColour( _T("BLACK") );
3716
3717 // init attr cache
3718 m_attrCache.row = -1;
3719
3720 // TODO: something better than this ?
3721 //
3722 m_labelFont = this->GetFont();
3723 m_labelFont.SetWeight( m_labelFont.GetWeight() + 2 );
3724
3725 m_rowLabelHorizAlign = wxALIGN_LEFT;
3726 m_rowLabelVertAlign = wxALIGN_CENTRE;
3727
3728 m_colLabelHorizAlign = wxALIGN_CENTRE;
3729 m_colLabelVertAlign = wxALIGN_TOP;
3730
3731 m_defaultColWidth = WXGRID_DEFAULT_COL_WIDTH;
3732 m_defaultRowHeight = m_gridWin->GetCharHeight();
3733
3734#if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl()
3735 m_defaultRowHeight += 8;
3736#else
3737 m_defaultRowHeight += 4;
3738#endif
3739
3740 m_gridLineColour = wxColour( 128, 128, 255 );
3741 m_gridLinesEnabled = TRUE;
3742 m_cellHighlightColour = m_gridLineColour;
3743 m_cellHighlightPenWidth = 2;
3744 m_cellHighlightROPenWidth = 1;
3745
3746 m_cursorMode = WXGRID_CURSOR_SELECT_CELL;
3747 m_winCapture = (wxWindow *)NULL;
3748 m_canDragRowSize = TRUE;
3749 m_canDragColSize = TRUE;
3750 m_canDragGridSize = TRUE;
3751 m_dragLastPos = -1;
3752 m_dragRowOrCol = -1;
3753 m_isDragging = FALSE;
3754 m_startDragPos = wxDefaultPosition;
3755
3756 m_waitForSlowClick = FALSE;
3757
3758 m_rowResizeCursor = wxCursor( wxCURSOR_SIZENS );
3759 m_colResizeCursor = wxCursor( wxCURSOR_SIZEWE );
3760
3761 m_currentCellCoords = wxGridNoCellCoords;
3762
3763 m_selectingTopLeft = wxGridNoCellCoords;
3764 m_selectingBottomRight = wxGridNoCellCoords;
3765 m_selectionBackground = wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT);
3766 m_selectionForeground = wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT);
3767
3768 m_editable = TRUE; // default for whole grid
3769
3770 m_inOnKeyDown = FALSE;
3771 m_batchCount = 0;
3772
3773 m_extraWidth =
3774 m_extraHeight = 0;
3775}
3776
3777// ----------------------------------------------------------------------------
3778// the idea is to call these functions only when necessary because they create
3779// quite big arrays which eat memory mostly unnecessary - in particular, if
3780// default widths/heights are used for all rows/columns, we may not use these
3781// arrays at all
3782//
3783// with some extra code, it should be possible to only store the
3784// widths/heights different from default ones but this will be done later...
3785// ----------------------------------------------------------------------------
3786
3787void wxGrid::InitRowHeights()
3788{
3789 m_rowHeights.Empty();
3790 m_rowBottoms.Empty();
3791
3792 m_rowHeights.Alloc( m_numRows );
3793 m_rowBottoms.Alloc( m_numRows );
3794
3795 int rowBottom = 0;
3796 for ( int i = 0; i < m_numRows; i++ )
3797 {
3798 m_rowHeights.Add( m_defaultRowHeight );
3799 rowBottom += m_defaultRowHeight;
3800 m_rowBottoms.Add( rowBottom );
3801 }
3802}
3803
3804void wxGrid::InitColWidths()
3805{
3806 m_colWidths.Empty();
3807 m_colRights.Empty();
3808
3809 m_colWidths.Alloc( m_numCols );
3810 m_colRights.Alloc( m_numCols );
3811 int colRight = 0;
3812 for ( int i = 0; i < m_numCols; i++ )
3813 {
3814 m_colWidths.Add( m_defaultColWidth );
3815 colRight += m_defaultColWidth;
3816 m_colRights.Add( colRight );
3817 }
3818}
3819
3820int wxGrid::GetColWidth(int col) const
3821{
3822 return m_colWidths.IsEmpty() ? m_defaultColWidth : m_colWidths[col];
3823}
3824
3825int wxGrid::GetColLeft(int col) const
3826{
3827 return m_colRights.IsEmpty() ? col * m_defaultColWidth
3828 : m_colRights[col] - m_colWidths[col];
3829}
3830
3831int wxGrid::GetColRight(int col) const
3832{
3833 return m_colRights.IsEmpty() ? (col + 1) * m_defaultColWidth
3834 : m_colRights[col];
3835}
3836
3837int wxGrid::GetRowHeight(int row) const
3838{
3839 return m_rowHeights.IsEmpty() ? m_defaultRowHeight : m_rowHeights[row];
3840}
3841
3842int wxGrid::GetRowTop(int row) const
3843{
3844 return m_rowBottoms.IsEmpty() ? row * m_defaultRowHeight
3845 : m_rowBottoms[row] - m_rowHeights[row];
3846}
3847
3848int wxGrid::GetRowBottom(int row) const
3849{
3850 return m_rowBottoms.IsEmpty() ? (row + 1) * m_defaultRowHeight
3851 : m_rowBottoms[row];
3852}
3853
3854void wxGrid::CalcDimensions()
3855{
3856 int cw, ch;
3857 GetClientSize( &cw, &ch );
3858
3859 if ( m_rowLabelWin->IsShown() )
3860 cw -= m_rowLabelWidth;
3861 if ( m_colLabelWin->IsShown() )
3862 ch -= m_colLabelHeight;
3863
3864 // grid total size
3865 int w = m_numCols > 0 ? GetColRight(m_numCols - 1) + m_extraWidth + 1 : 0;
3866 int h = m_numRows > 0 ? GetRowBottom(m_numRows - 1) + m_extraHeight + 1 : 0;
3867
3868 // preserve (more or less) the previous position
3869 int x, y;
3870 GetViewStart( &x, &y );
3871
3872 // maybe we don't need scrollbars at all?
3873 //
3874 // also adjust the position to be valid for the new scroll rangs
3875 if ( w <= cw )
3876 {
3877 w = x = 0;
3878 }
3879 else
3880 {
3881 if ( x >= w )
3882 x = w - 1;
3883 }
3884
3885 if ( h <= ch )
3886 {
3887 h = y = 0;
3888 }
3889 else
3890 {
3891 if ( y >= h )
3892 y = h - 1;
3893 }
3894
3895 // do set scrollbar parameters
3896 SetScrollbars( GRID_SCROLL_LINE_X, GRID_SCROLL_LINE_Y,
3897 GetScrollX(w), GetScrollY(h), x, y,
3898 GetBatchCount() != 0);
3899
3900 // if our OnSize() hadn't been called (it would if we have scrollbars), we
3901 // still must reposition the children
3902 CalcWindowSizes();
3903}
3904
3905
3906void wxGrid::CalcWindowSizes()
3907{
3908 int cw, ch;
3909 GetClientSize( &cw, &ch );
3910
3911 if ( m_cornerLabelWin->IsShown() )
3912 m_cornerLabelWin->SetSize( 0, 0, m_rowLabelWidth, m_colLabelHeight );
3913
3914 if ( m_colLabelWin->IsShown() )
3915 m_colLabelWin->SetSize( m_rowLabelWidth, 0, cw-m_rowLabelWidth, m_colLabelHeight);
3916
3917 if ( m_rowLabelWin->IsShown() )
3918 m_rowLabelWin->SetSize( 0, m_colLabelHeight, m_rowLabelWidth, ch-m_colLabelHeight);
3919
3920 if ( m_gridWin->IsShown() )
3921 m_gridWin->SetSize( m_rowLabelWidth, m_colLabelHeight, cw-m_rowLabelWidth, ch-m_colLabelHeight);
3922}
3923
3924
3925// this is called when the grid table sends a message to say that it
3926// has been redimensioned
3927//
3928bool wxGrid::Redimension( wxGridTableMessage& msg )
3929{
3930 int i;
3931 bool result = FALSE;
3932
3933#if 0
3934 // if we were using the default widths/heights so far, we must change them
3935 // now
3936 if ( m_colWidths.IsEmpty() )
3937 {
3938 InitColWidths();
3939 }
3940
3941 if ( m_rowHeights.IsEmpty() )
3942 {
3943 InitRowHeights();
3944 }
3945#endif
3946
3947 switch ( msg.GetId() )
3948 {
3949 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED:
3950 {
3951 size_t pos = msg.GetCommandInt();
3952 int numRows = msg.GetCommandInt2();
3953
3954 m_numRows += numRows;
3955
3956 if ( !m_rowHeights.IsEmpty() )
3957 {
3958 for ( i = 0; i < numRows; i++ )
3959 {
3960 m_rowHeights.Insert( m_defaultRowHeight, pos );
3961 m_rowBottoms.Insert( 0, pos );
3962 }
3963
3964 int bottom = 0;
3965 if ( pos > 0 ) bottom = m_rowBottoms[pos-1];
3966
3967 for ( i = pos; i < m_numRows; i++ )
3968 {
3969 bottom += m_rowHeights[i];
3970 m_rowBottoms[i] = bottom;
3971 }
3972 }
3973 if ( m_currentCellCoords == wxGridNoCellCoords )
3974 {
3975 // if we have just inserted cols into an empty grid the current
3976 // cell will be undefined...
3977 //
3978 SetCurrentCell( 0, 0 );
3979 }
3980 m_selection->UpdateRows( pos, numRows );
3981 wxGridCellAttrProvider * attrProvider = m_table->GetAttrProvider();
3982 if (attrProvider)
3983 attrProvider->UpdateAttrRows( pos, numRows );
3984
3985 if ( !GetBatchCount() )
3986 {
3987 CalcDimensions();
3988 m_rowLabelWin->Refresh();
3989 }
3990 }
3991 result = TRUE;
3992 break;
3993
3994 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED:
3995 {
3996 int numRows = msg.GetCommandInt();
3997 int oldNumRows = m_numRows;
3998 m_numRows += numRows;
3999
4000 if ( !m_rowHeights.IsEmpty() )
4001 {
4002 for ( i = 0; i < numRows; i++ )
4003 {
4004 m_rowHeights.Add( m_defaultRowHeight );
4005 m_rowBottoms.Add( 0 );
4006 }
4007
4008 int bottom = 0;
4009 if ( oldNumRows > 0 ) bottom = m_rowBottoms[oldNumRows-1];
4010
4011 for ( i = oldNumRows; i < m_numRows; i++ )
4012 {
4013 bottom += m_rowHeights[i];
4014 m_rowBottoms[i] = bottom;
4015 }
4016 }
4017 if ( m_currentCellCoords == wxGridNoCellCoords )
4018 {
4019 // if we have just inserted cols into an empty grid the current
4020 // cell will be undefined...
4021 //
4022 SetCurrentCell( 0, 0 );
4023 }
4024 if ( !GetBatchCount() )
4025 {
4026 CalcDimensions();
4027 m_rowLabelWin->Refresh();
4028 }
4029 }
4030 result = TRUE;
4031 break;
4032
4033 case wxGRIDTABLE_NOTIFY_ROWS_DELETED:
4034 {
4035 size_t pos = msg.GetCommandInt();
4036 int numRows = msg.GetCommandInt2();
4037 m_numRows -= numRows;
4038
4039 if ( !m_rowHeights.IsEmpty() )
4040 {
4041 for ( i = 0; i < numRows; i++ )
4042 {
4043 m_rowHeights.RemoveAt( pos );
4044 m_rowBottoms.RemoveAt( pos );
4045 }
4046
4047 int h = 0;
4048 for ( i = 0; i < m_numRows; i++ )
4049 {
4050 h += m_rowHeights[i];
4051 m_rowBottoms[i] = h;
4052 }
4053 }
4054 if ( !m_numRows )
4055 {
4056 m_currentCellCoords = wxGridNoCellCoords;
4057 }
4058 else
4059 {
4060 if ( m_currentCellCoords.GetRow() >= m_numRows )
4061 m_currentCellCoords.Set( 0, 0 );
4062 }
4063 m_selection->UpdateRows( pos, -((int)numRows) );
4064 wxGridCellAttrProvider * attrProvider = m_table->GetAttrProvider();
4065 if (attrProvider) {
4066 attrProvider->UpdateAttrRows( pos, -((int)numRows) );
4067// ifdef'd out following patch from Paul Gammans
4068#if 0
4069 // No need to touch column attributes, unless we
4070 // removed _all_ rows, in this case, we remove
4071 // all column attributes.
4072 // I hate to do this here, but the
4073 // needed data is not available inside UpdateAttrRows.
4074 if ( !GetNumberRows() )
4075 attrProvider->UpdateAttrCols( 0, -GetNumberCols() );
4076#endif
4077 }
4078 if ( !GetBatchCount() )
4079 {
4080 CalcDimensions();
4081 m_rowLabelWin->Refresh();
4082 }
4083 }
4084 result = TRUE;
4085 break;
4086
4087 case wxGRIDTABLE_NOTIFY_COLS_INSERTED:
4088 {
4089 size_t pos = msg.GetCommandInt();
4090 int numCols = msg.GetCommandInt2();
4091 m_numCols += numCols;
4092
4093 if ( !m_colWidths.IsEmpty() )
4094 {
4095 for ( i = 0; i < numCols; i++ )
4096 {
4097 m_colWidths.Insert( m_defaultColWidth, pos );
4098 m_colRights.Insert( 0, pos );
4099 }
4100
4101 int right = 0;
4102 if ( pos > 0 ) right = m_colRights[pos-1];
4103
4104 for ( i = pos; i < m_numCols; i++ )
4105 {
4106 right += m_colWidths[i];
4107 m_colRights[i] = right;
4108 }
4109 }
4110 if ( m_currentCellCoords == wxGridNoCellCoords )
4111 {
4112 // if we have just inserted cols into an empty grid the current
4113 // cell will be undefined...
4114 //
4115 SetCurrentCell( 0, 0 );
4116 }
4117 m_selection->UpdateCols( pos, numCols );
4118 wxGridCellAttrProvider * attrProvider = m_table->GetAttrProvider();
4119 if (attrProvider)
4120 attrProvider->UpdateAttrCols( pos, numCols );
4121 if ( !GetBatchCount() )
4122 {
4123 CalcDimensions();
4124 m_colLabelWin->Refresh();
4125 }
4126
4127 }
4128 result = TRUE;
4129 break;
4130
4131 case wxGRIDTABLE_NOTIFY_COLS_APPENDED:
4132 {
4133 int numCols = msg.GetCommandInt();
4134 int oldNumCols = m_numCols;
4135 m_numCols += numCols;
4136 if ( !m_colWidths.IsEmpty() )
4137 {
4138 for ( i = 0; i < numCols; i++ )
4139 {
4140 m_colWidths.Add( m_defaultColWidth );
4141 m_colRights.Add( 0 );
4142 }
4143
4144 int right = 0;
4145 if ( oldNumCols > 0 ) right = m_colRights[oldNumCols-1];
4146
4147 for ( i = oldNumCols; i < m_numCols; i++ )
4148 {
4149 right += m_colWidths[i];
4150 m_colRights[i] = right;
4151 }
4152 }
4153 if ( m_currentCellCoords == wxGridNoCellCoords )
4154 {
4155 // if we have just inserted cols into an empty grid the current
4156 // cell will be undefined...
4157 //
4158 SetCurrentCell( 0, 0 );
4159 }
4160 if ( !GetBatchCount() )
4161 {
4162 CalcDimensions();
4163 m_colLabelWin->Refresh();
4164 }
4165 }
4166 result = TRUE;
4167 break;
4168
4169 case wxGRIDTABLE_NOTIFY_COLS_DELETED:
4170 {
4171 size_t pos = msg.GetCommandInt();
4172 int numCols = msg.GetCommandInt2();
4173 m_numCols -= numCols;
4174
4175 if ( !m_colWidths.IsEmpty() )
4176 {
4177 for ( i = 0; i < numCols; i++ )
4178 {
4179 m_colWidths.RemoveAt( pos );
4180 m_colRights.RemoveAt( pos );
4181 }
4182
4183 int w = 0;
4184 for ( i = 0; i < m_numCols; i++ )
4185 {
4186 w += m_colWidths[i];
4187 m_colRights[i] = w;
4188 }
4189 }
4190 if ( !m_numCols )
4191 {
4192 m_currentCellCoords = wxGridNoCellCoords;
4193 }
4194 else
4195 {
4196 if ( m_currentCellCoords.GetCol() >= m_numCols )
4197 m_currentCellCoords.Set( 0, 0 );
4198 }
4199 m_selection->UpdateCols( pos, -((int)numCols) );
4200 wxGridCellAttrProvider * attrProvider = m_table->GetAttrProvider();
4201 if (attrProvider) {
4202 attrProvider->UpdateAttrCols( pos, -((int)numCols) );
4203// ifdef'd out following patch from Paul Gammans
4204#if 0
4205 // No need to touch row attributes, unless we
4206 // removed _all_ columns, in this case, we remove
4207 // all row attributes.
4208 // I hate to do this here, but the
4209 // needed data is not available inside UpdateAttrCols.
4210 if ( !GetNumberCols() )
4211 attrProvider->UpdateAttrRows( 0, -GetNumberRows() );
4212#endif
4213 }
4214 if ( !GetBatchCount() )
4215 {
4216 CalcDimensions();
4217 m_colLabelWin->Refresh();
4218 }
4219 }
4220 result = TRUE;
4221 break;
4222 }
4223
4224 if (result && !GetBatchCount() )
4225 m_gridWin->Refresh();
4226 return result;
4227}
4228
4229
4230wxArrayInt wxGrid::CalcRowLabelsExposed( const wxRegion& reg )
4231{
4232 wxRegionIterator iter( reg );
4233 wxRect r;
4234
4235 wxArrayInt rowlabels;
4236
4237 int top, bottom;
4238 while ( iter )
4239 {
4240 r = iter.GetRect();
4241
4242 // TODO: remove this when we can...
4243 // There is a bug in wxMotif that gives garbage update
4244 // rectangles if you jump-scroll a long way by clicking the
4245 // scrollbar with middle button. This is a work-around
4246 //
4247#if defined(__WXMOTIF__)
4248 int cw, ch;
4249 m_gridWin->GetClientSize( &cw, &ch );
4250 if ( r.GetTop() > ch ) r.SetTop( 0 );
4251 r.SetBottom( wxMin( r.GetBottom(), ch ) );
4252#endif
4253
4254 // logical bounds of update region
4255 //
4256 int dummy;
4257 CalcUnscrolledPosition( 0, r.GetTop(), &dummy, &top );
4258 CalcUnscrolledPosition( 0, r.GetBottom(), &dummy, &bottom );
4259
4260 // find the row labels within these bounds
4261 //
4262 int row;
4263 for ( row = internalYToRow(top); row < m_numRows; row++ )
4264 {
4265 if ( GetRowBottom(row) < top )
4266 continue;
4267
4268 if ( GetRowTop(row) > bottom )
4269 break;
4270
4271 rowlabels.Add( row );
4272 }
4273
4274 iter++ ;
4275 }
4276
4277 return rowlabels;
4278}
4279
4280
4281wxArrayInt wxGrid::CalcColLabelsExposed( const wxRegion& reg )
4282{
4283 wxRegionIterator iter( reg );
4284 wxRect r;
4285
4286 wxArrayInt colLabels;
4287
4288 int left, right;
4289 while ( iter )
4290 {
4291 r = iter.GetRect();
4292
4293 // TODO: remove this when we can...
4294 // There is a bug in wxMotif that gives garbage update
4295 // rectangles if you jump-scroll a long way by clicking the
4296 // scrollbar with middle button. This is a work-around
4297 //
4298#if defined(__WXMOTIF__)
4299 int cw, ch;
4300 m_gridWin->GetClientSize( &cw, &ch );
4301 if ( r.GetLeft() > cw ) r.SetLeft( 0 );
4302 r.SetRight( wxMin( r.GetRight(), cw ) );
4303#endif
4304
4305 // logical bounds of update region
4306 //
4307 int dummy;
4308 CalcUnscrolledPosition( r.GetLeft(), 0, &left, &dummy );
4309 CalcUnscrolledPosition( r.GetRight(), 0, &right, &dummy );
4310
4311 // find the cells within these bounds
4312 //
4313 int col;
4314 for ( col = internalXToCol(left); col < m_numCols; col++ )
4315 {
4316 if ( GetColRight(col) < left )
4317 continue;
4318
4319 if ( GetColLeft(col) > right )
4320 break;
4321
4322 colLabels.Add( col );
4323 }
4324
4325 iter++ ;
4326 }
4327 return colLabels;
4328}
4329
4330
4331wxGridCellCoordsArray wxGrid::CalcCellsExposed( const wxRegion& reg )
4332{
4333 wxRegionIterator iter( reg );
4334 wxRect r;
4335
4336 wxGridCellCoordsArray cellsExposed;
4337
4338 int left, top, right, bottom;
4339 while ( iter )
4340 {
4341 r = iter.GetRect();
4342
4343 // TODO: remove this when we can...
4344 // There is a bug in wxMotif that gives garbage update
4345 // rectangles if you jump-scroll a long way by clicking the
4346 // scrollbar with middle button. This is a work-around
4347 //
4348#if defined(__WXMOTIF__)
4349 int cw, ch;
4350 m_gridWin->GetClientSize( &cw, &ch );
4351 if ( r.GetTop() > ch ) r.SetTop( 0 );
4352 if ( r.GetLeft() > cw ) r.SetLeft( 0 );
4353 r.SetRight( wxMin( r.GetRight(), cw ) );
4354 r.SetBottom( wxMin( r.GetBottom(), ch ) );
4355#endif
4356
4357 // logical bounds of update region
4358 //
4359 CalcUnscrolledPosition( r.GetLeft(), r.GetTop(), &left, &top );
4360 CalcUnscrolledPosition( r.GetRight(), r.GetBottom(), &right, &bottom );
4361
4362 // find the cells within these bounds
4363 //
4364 int row, col;
4365 for ( row = internalYToRow(top); row < m_numRows; row++ )
4366 {
4367 if ( GetRowBottom(row) <= top )
4368 continue;
4369
4370 if ( GetRowTop(row) > bottom )
4371 break;
4372
4373 for ( col = internalXToCol(left); col < m_numCols; col++ )
4374 {
4375 if ( GetColRight(col) <= left )
4376 continue;
4377
4378 if ( GetColLeft(col) > right )
4379 break;
4380
4381 cellsExposed.Add( wxGridCellCoords( row, col ) );
4382 }
4383 }
4384
4385 iter++;
4386 }
4387
4388 return cellsExposed;
4389}
4390
4391
4392void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent& event )
4393{
4394 int x, y, row;
4395 wxPoint pos( event.GetPosition() );
4396 CalcUnscrolledPosition( pos.x, pos.y, &x, &y );
4397
4398 if ( event.Dragging() )
4399 {
4400 m_isDragging = TRUE;
4401
4402 if ( event.LeftIsDown() )
4403 {
4404 switch( m_cursorMode )
4405 {
4406 case WXGRID_CURSOR_RESIZE_ROW:
4407 {
4408 int cw, ch, left, dummy;
4409 m_gridWin->GetClientSize( &cw, &ch );
4410 CalcUnscrolledPosition( 0, 0, &left, &dummy );
4411
4412 wxClientDC dc( m_gridWin );
4413 PrepareDC( dc );
4414 y = wxMax( y,
4415 GetRowTop(m_dragRowOrCol) +
4416 GetRowMinimalHeight(m_dragRowOrCol) );
4417 dc.SetLogicalFunction(wxINVERT);
4418 if ( m_dragLastPos >= 0 )
4419 {
4420 dc.DrawLine( left, m_dragLastPos, left+cw, m_dragLastPos );
4421 }
4422 dc.DrawLine( left, y, left+cw, y );
4423 m_dragLastPos = y;
4424 }
4425 break;
4426
4427 case WXGRID_CURSOR_SELECT_ROW:
4428 if ( (row = YToRow( y )) >= 0 )
4429 {
4430 m_selection->SelectRow( row,
4431 event.ControlDown(),
4432 event.ShiftDown(),
4433 event.AltDown(),
4434 event.MetaDown() );
4435 }
4436
4437 // default label to suppress warnings about "enumeration value
4438 // 'xxx' not handled in switch
4439 default:
4440 break;
4441 }
4442 }
4443 return;
4444 }
4445
4446 m_isDragging = FALSE;
4447
4448
4449 // ------------ Entering or leaving the window
4450 //
4451 if ( event.Entering() || event.Leaving() )
4452 {
4453 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_rowLabelWin);
4454 }
4455
4456
4457 // ------------ Left button pressed
4458 //
4459 else if ( event.LeftDown() )
4460 {
4461 // don't send a label click event for a hit on the
4462 // edge of the row label - this is probably the user
4463 // wanting to resize the row
4464 //
4465 if ( YToEdgeOfRow(y) < 0 )
4466 {
4467 row = YToRow(y);
4468 if ( row >= 0 &&
4469 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK, row, -1, event ) )
4470 {
4471 if ( !event.ShiftDown() && !event.ControlDown() )
4472 ClearSelection();
4473 if ( event.ShiftDown() )
4474 m_selection->SelectBlock( m_currentCellCoords.GetRow(),
4475 0,
4476 row,
4477 GetNumberCols() - 1,
4478 event.ControlDown(),
4479 event.ShiftDown(),
4480 event.AltDown(),
4481 event.MetaDown() );
4482 else
4483 m_selection->SelectRow( row,
4484 event.ControlDown(),
4485 event.ShiftDown(),
4486 event.AltDown(),
4487 event.MetaDown() );
4488 ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW, m_rowLabelWin);
4489 }
4490 }
4491 else
4492 {
4493 // starting to drag-resize a row
4494 //
4495 if ( CanDragRowSize() )
4496 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW, m_rowLabelWin);
4497 }
4498 }
4499
4500
4501 // ------------ Left double click
4502 //
4503 else if (event.LeftDClick() )
4504 {
4505 if ( YToEdgeOfRow(y) < 0 )
4506 {
4507 row = YToRow(y);
4508 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK, row, -1, event );
4509 }
4510 }
4511
4512
4513 // ------------ Left button released
4514 //
4515 else if ( event.LeftUp() )
4516 {
4517 if ( m_cursorMode == WXGRID_CURSOR_RESIZE_ROW )
4518 {
4519 DoEndDragResizeRow();
4520
4521 // Note: we are ending the event *after* doing
4522 // default processing in this case
4523 //
4524 SendEvent( wxEVT_GRID_ROW_SIZE, m_dragRowOrCol, -1, event );
4525 }
4526
4527 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_rowLabelWin);
4528 m_dragLastPos = -1;
4529 }
4530
4531
4532 // ------------ Right button down
4533 //
4534 else if ( event.RightDown() )
4535 {
4536 row = YToRow(y);
4537 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK, row, -1, event ) )
4538 {
4539 // no default action at the moment
4540 }
4541 }
4542
4543
4544 // ------------ Right double click
4545 //
4546 else if ( event.RightDClick() )
4547 {
4548 row = YToRow(y);
4549 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK, row, -1, event ) )
4550 {
4551 // no default action at the moment
4552 }
4553 }
4554
4555
4556 // ------------ No buttons down and mouse moving
4557 //
4558 else if ( event.Moving() )
4559 {
4560 m_dragRowOrCol = YToEdgeOfRow( y );
4561 if ( m_dragRowOrCol >= 0 )
4562 {
4563 if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
4564 {
4565 // don't capture the mouse yet
4566 if ( CanDragRowSize() )
4567 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW, m_rowLabelWin, FALSE);
4568 }
4569 }
4570 else if ( m_cursorMode != WXGRID_CURSOR_SELECT_CELL )
4571 {
4572 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_rowLabelWin, FALSE);
4573 }
4574 }
4575}
4576
4577
4578void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent& event )
4579{
4580 int x, y, col;
4581 wxPoint pos( event.GetPosition() );
4582 CalcUnscrolledPosition( pos.x, pos.y, &x, &y );
4583
4584 if ( event.Dragging() )
4585 {
4586 m_isDragging = TRUE;
4587
4588 if ( event.LeftIsDown() )
4589 {
4590 switch( m_cursorMode )
4591 {
4592 case WXGRID_CURSOR_RESIZE_COL:
4593 {
4594 int cw, ch, dummy, top;
4595 m_gridWin->GetClientSize( &cw, &ch );
4596 CalcUnscrolledPosition( 0, 0, &dummy, &top );
4597
4598 wxClientDC dc( m_gridWin );
4599 PrepareDC( dc );
4600
4601 x = wxMax( x, GetColLeft(m_dragRowOrCol) +
4602 GetColMinimalWidth(m_dragRowOrCol));
4603 dc.SetLogicalFunction(wxINVERT);
4604 if ( m_dragLastPos >= 0 )
4605 {
4606 dc.DrawLine( m_dragLastPos, top, m_dragLastPos, top+ch );
4607 }
4608 dc.DrawLine( x, top, x, top+ch );
4609 m_dragLastPos = x;
4610 }
4611 break;
4612
4613 case WXGRID_CURSOR_SELECT_COL:
4614 if ( (col = XToCol( x )) >= 0 )
4615 {
4616 m_selection->SelectCol( col,
4617 event.ControlDown(),
4618 event.ShiftDown(),
4619 event.AltDown(),
4620 event.MetaDown() );
4621 }
4622
4623 // default label to suppress warnings about "enumeration value
4624 // 'xxx' not handled in switch
4625 default:
4626 break;
4627 }
4628 }
4629 return;
4630 }
4631
4632 m_isDragging = FALSE;
4633
4634
4635 // ------------ Entering or leaving the window
4636 //
4637 if ( event.Entering() || event.Leaving() )
4638 {
4639 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_colLabelWin);
4640 }
4641
4642
4643 // ------------ Left button pressed
4644 //
4645 else if ( event.LeftDown() )
4646 {
4647 // don't send a label click event for a hit on the
4648 // edge of the col label - this is probably the user
4649 // wanting to resize the col
4650 //
4651 if ( XToEdgeOfCol(x) < 0 )
4652 {
4653 col = XToCol(x);
4654 if ( col >= 0 &&
4655 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK, -1, col, event ) )
4656 {
4657 if ( !event.ShiftDown() && !event.ControlDown() )
4658 ClearSelection();
4659 if ( event.ShiftDown() )
4660 m_selection->SelectBlock( 0,
4661 m_currentCellCoords.GetCol(),
4662 GetNumberRows() - 1, col,
4663 event.ControlDown(),
4664 event.ShiftDown(),
4665 event.AltDown(),
4666 event.MetaDown() );
4667 else
4668 m_selection->SelectCol( col,
4669 event.ControlDown(),
4670 event.ShiftDown(),
4671 event.AltDown(),
4672 event.MetaDown() );
4673 ChangeCursorMode(WXGRID_CURSOR_SELECT_COL, m_colLabelWin);
4674 }
4675 }
4676 else
4677 {
4678 // starting to drag-resize a col
4679 //
4680 if ( CanDragColSize() )
4681 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL, m_colLabelWin);
4682 }
4683 }
4684
4685
4686 // ------------ Left double click
4687 //
4688 if ( event.LeftDClick() )
4689 {
4690 if ( XToEdgeOfCol(x) < 0 )
4691 {
4692 col = XToCol(x);
4693 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK, -1, col, event );
4694 }
4695 }
4696
4697
4698 // ------------ Left button released
4699 //
4700 else if ( event.LeftUp() )
4701 {
4702 if ( m_cursorMode == WXGRID_CURSOR_RESIZE_COL )
4703 {
4704 DoEndDragResizeCol();
4705
4706 // Note: we are ending the event *after* doing
4707 // default processing in this case
4708 //
4709 SendEvent( wxEVT_GRID_COL_SIZE, -1, m_dragRowOrCol, event );
4710 }
4711
4712 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_colLabelWin);
4713 m_dragLastPos = -1;
4714 }
4715
4716
4717 // ------------ Right button down
4718 //
4719 else if ( event.RightDown() )
4720 {
4721 col = XToCol(x);
4722 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK, -1, col, event ) )
4723 {
4724 // no default action at the moment
4725 }
4726 }
4727
4728
4729 // ------------ Right double click
4730 //
4731 else if ( event.RightDClick() )
4732 {
4733 col = XToCol(x);
4734 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK, -1, col, event ) )
4735 {
4736 // no default action at the moment
4737 }
4738 }
4739
4740
4741 // ------------ No buttons down and mouse moving
4742 //
4743 else if ( event.Moving() )
4744 {
4745 m_dragRowOrCol = XToEdgeOfCol( x );
4746 if ( m_dragRowOrCol >= 0 )
4747 {
4748 if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
4749 {
4750 // don't capture the cursor yet
4751 if ( CanDragColSize() )
4752 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL, m_colLabelWin, FALSE);
4753 }
4754 }
4755 else if ( m_cursorMode != WXGRID_CURSOR_SELECT_CELL )
4756 {
4757 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_colLabelWin, FALSE);
4758 }
4759 }
4760}
4761
4762
4763void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent& event )
4764{
4765 if ( event.LeftDown() )
4766 {
4767 // indicate corner label by having both row and
4768 // col args == -1
4769 //
4770 if ( !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK, -1, -1, event ) )
4771 {
4772 SelectAll();
4773 }
4774 }
4775
4776 else if ( event.LeftDClick() )
4777 {
4778 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK, -1, -1, event );
4779 }
4780
4781 else if ( event.RightDown() )
4782 {
4783 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK, -1, -1, event ) )
4784 {
4785 // no default action at the moment
4786 }
4787 }
4788
4789 else if ( event.RightDClick() )
4790 {
4791 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK, -1, -1, event ) )
4792 {
4793 // no default action at the moment
4794 }
4795 }
4796}
4797
4798void wxGrid::ChangeCursorMode(CursorMode mode,
4799 wxWindow *win,
4800 bool captureMouse)
4801{
4802#ifdef __WXDEBUG__
4803 static const wxChar *cursorModes[] =
4804 {
4805 _T("SELECT_CELL"),
4806 _T("RESIZE_ROW"),
4807 _T("RESIZE_COL"),
4808 _T("SELECT_ROW"),
4809 _T("SELECT_COL")
4810 };
4811
4812 wxLogTrace(_T("grid"),
4813 _T("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
4814 win == m_colLabelWin ? _T("colLabelWin")
4815 : win ? _T("rowLabelWin")
4816 : _T("gridWin"),
4817 cursorModes[m_cursorMode], cursorModes[mode]);
4818#endif // __WXDEBUG__
4819
4820 if ( mode == m_cursorMode &&
4821 win == m_winCapture &&
4822 captureMouse == (m_winCapture != NULL))
4823 return;
4824
4825 if ( !win )
4826 {
4827 // by default use the grid itself
4828 win = m_gridWin;
4829 }
4830
4831 if ( m_winCapture )
4832 {
4833 m_winCapture->ReleaseMouse();
4834 m_winCapture = (wxWindow *)NULL;
4835 }
4836
4837 m_cursorMode = mode;
4838
4839 switch ( m_cursorMode )
4840 {
4841 case WXGRID_CURSOR_RESIZE_ROW:
4842 win->SetCursor( m_rowResizeCursor );
4843 break;
4844
4845 case WXGRID_CURSOR_RESIZE_COL:
4846 win->SetCursor( m_colResizeCursor );
4847 break;
4848
4849 default:
4850 win->SetCursor( *wxSTANDARD_CURSOR );
4851 }
4852
4853 // we need to capture mouse when resizing
4854 bool resize = m_cursorMode == WXGRID_CURSOR_RESIZE_ROW ||
4855 m_cursorMode == WXGRID_CURSOR_RESIZE_COL;
4856
4857 if ( captureMouse && resize )
4858 {
4859 win->CaptureMouse();
4860 m_winCapture = win;
4861 }
4862}
4863
4864void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent& event )
4865{
4866 int x, y;
4867 wxPoint pos( event.GetPosition() );
4868 CalcUnscrolledPosition( pos.x, pos.y, &x, &y );
4869
4870 wxGridCellCoords coords;
4871 XYToCell( x, y, coords );
4872
4873 if ( event.Dragging() )
4874 {
4875 //wxLogDebug("pos(%d, %d) coords(%d, %d)", pos.x, pos.y, coords.GetRow(), coords.GetCol());
4876
4877 // Don't start doing anything until the mouse has been drug at
4878 // least 3 pixels in any direction...
4879 if (! m_isDragging)
4880 {
4881 if (m_startDragPos == wxDefaultPosition)
4882 {
4883 m_startDragPos = pos;
4884 return;
4885 }
4886 if (abs(m_startDragPos.x - pos.x) < 4 && abs(m_startDragPos.y - pos.y) < 4)
4887 return;
4888 }
4889
4890 m_isDragging = TRUE;
4891 if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
4892 {
4893 // Hide the edit control, so it
4894 // won't interfer with drag-shrinking.
4895 if ( IsCellEditControlShown() )
4896 {
4897 HideCellEditControl();
4898 SaveEditControlValue();
4899 }
4900
4901 // Have we captured the mouse yet?
4902 if (! m_winCapture)
4903 {
4904 m_winCapture = m_gridWin;
4905 m_winCapture->CaptureMouse();
4906 }
4907
4908 if ( coords != wxGridNoCellCoords )
4909 {
4910 if ( event.ControlDown() )
4911 {
4912 if ( m_selectingKeyboard == wxGridNoCellCoords)
4913 m_selectingKeyboard = coords;
4914 HighlightBlock ( m_selectingKeyboard, coords );
4915 }
4916 else
4917 {
4918 if ( !IsSelection() )
4919 {
4920 HighlightBlock( coords, coords );
4921 }
4922 else
4923 {
4924 HighlightBlock( m_currentCellCoords, coords );
4925 }
4926 }
4927
4928 if (! IsVisible(coords))
4929 {
4930 MakeCellVisible(coords);
4931 // TODO: need to introduce a delay or something here. The
4932 // scrolling is way to fast, at least on MSW - also on GTK.
4933 }
4934 }
4935 }
4936 else if ( m_cursorMode == WXGRID_CURSOR_RESIZE_ROW )
4937 {
4938 int cw, ch, left, dummy;
4939 m_gridWin->GetClientSize( &cw, &ch );
4940 CalcUnscrolledPosition( 0, 0, &left, &dummy );
4941
4942 wxClientDC dc( m_gridWin );
4943 PrepareDC( dc );
4944 y = wxMax( y, GetRowTop(m_dragRowOrCol) +
4945 GetRowMinimalHeight(m_dragRowOrCol) );
4946 dc.SetLogicalFunction(wxINVERT);
4947 if ( m_dragLastPos >= 0 )
4948 {
4949 dc.DrawLine( left, m_dragLastPos, left+cw, m_dragLastPos );
4950 }
4951 dc.DrawLine( left, y, left+cw, y );
4952 m_dragLastPos = y;
4953 }
4954 else if ( m_cursorMode == WXGRID_CURSOR_RESIZE_COL )
4955 {
4956 int cw, ch, dummy, top;
4957 m_gridWin->GetClientSize( &cw, &ch );
4958 CalcUnscrolledPosition( 0, 0, &dummy, &top );
4959
4960 wxClientDC dc( m_gridWin );
4961 PrepareDC( dc );
4962 x = wxMax( x, GetColLeft(m_dragRowOrCol) +
4963 GetColMinimalWidth(m_dragRowOrCol) );
4964 dc.SetLogicalFunction(wxINVERT);
4965 if ( m_dragLastPos >= 0 )
4966 {
4967 dc.DrawLine( m_dragLastPos, top, m_dragLastPos, top+ch );
4968 }
4969 dc.DrawLine( x, top, x, top+ch );
4970 m_dragLastPos = x;
4971 }
4972
4973 return;
4974 }
4975
4976 m_isDragging = FALSE;
4977 m_startDragPos = wxDefaultPosition;
4978
4979 // VZ: if we do this, the mode is reset to WXGRID_CURSOR_SELECT_CELL
4980 // immediately after it becomes WXGRID_CURSOR_RESIZE_ROW/COL under
4981 // wxGTK
4982#if 0
4983 if ( event.Entering() || event.Leaving() )
4984 {
4985 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL);
4986 m_gridWin->SetCursor( *wxSTANDARD_CURSOR );
4987 }
4988 else
4989#endif // 0
4990
4991 // ------------ Left button pressed
4992 //
4993 if ( event.LeftDown() && coords != wxGridNoCellCoords )
4994 {
4995 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_CLICK,
4996 coords.GetRow(),
4997 coords.GetCol(),
4998 event ) )
4999 {
5000 if ( !event.ControlDown() )
5001 ClearSelection();
5002 if ( event.ShiftDown() )
5003 {
5004 m_selection->SelectBlock( m_currentCellCoords.GetRow(),
5005 m_currentCellCoords.GetCol(),
5006 coords.GetRow(),
5007 coords.GetCol(),
5008 event.ControlDown(),
5009 event.ShiftDown(),
5010 event.AltDown(),
5011 event.MetaDown() );
5012 }
5013 else if ( XToEdgeOfCol(x) < 0 &&
5014 YToEdgeOfRow(y) < 0 )
5015 {
5016 DisableCellEditControl();
5017 MakeCellVisible( coords );
5018
5019 // if this is the second click on this cell then start
5020 // the edit control
5021 if ( m_waitForSlowClick &&
5022 (coords == m_currentCellCoords) &&
5023 CanEnableCellControl())
5024 {
5025 EnableCellEditControl();
5026
5027 wxGridCellAttr* attr = GetCellAttr(m_currentCellCoords);
5028 wxGridCellEditor *editor = attr->GetEditor(this,
5029 coords.GetRow(),
5030 coords.GetCol());
5031 editor->StartingClick();
5032 editor->DecRef();
5033 attr->DecRef();
5034
5035 m_waitForSlowClick = FALSE;
5036 }
5037 else
5038 {
5039 if ( event.ControlDown() )
5040 {
5041 m_selection->ToggleCellSelection( coords.GetRow(),
5042 coords.GetCol(),
5043 event.ControlDown(),
5044 event.ShiftDown(),
5045 event.AltDown(),
5046 event.MetaDown() );
5047 m_selectingTopLeft = wxGridNoCellCoords;
5048 m_selectingBottomRight = wxGridNoCellCoords;
5049 m_selectingKeyboard = coords;
5050 }
5051 else
5052 {
5053 SetCurrentCell( coords );
5054 if ( m_selection->GetSelectionMode()
5055 != wxGrid::wxGridSelectCells)
5056 HighlightBlock( coords, coords );
5057 }
5058 m_waitForSlowClick = TRUE;
5059 }
5060 }
5061 }
5062 }
5063
5064
5065 // ------------ Left double click
5066 //
5067 else if ( event.LeftDClick() && coords != wxGridNoCellCoords )
5068 {
5069 DisableCellEditControl();
5070
5071 if ( XToEdgeOfCol(x) < 0 && YToEdgeOfRow(y) < 0 )
5072 {
5073 SendEvent( wxEVT_GRID_CELL_LEFT_DCLICK,
5074 coords.GetRow(),
5075 coords.GetCol(),
5076 event );
5077 }
5078 }
5079
5080
5081 // ------------ Left button released
5082 //
5083 else if ( event.LeftUp() )
5084 {
5085 if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
5086 {
5087 if ( m_selectingTopLeft != wxGridNoCellCoords &&
5088 m_selectingBottomRight != wxGridNoCellCoords )
5089 {
5090 if (m_winCapture)
5091 {
5092 m_winCapture->ReleaseMouse();
5093 m_winCapture = NULL;
5094 }
5095 m_selection->SelectBlock( m_selectingTopLeft.GetRow(),
5096 m_selectingTopLeft.GetCol(),
5097 m_selectingBottomRight.GetRow(),
5098 m_selectingBottomRight.GetCol(),
5099 event.ControlDown(),
5100 event.ShiftDown(),
5101 event.AltDown(),
5102 event.MetaDown() );
5103 m_selectingTopLeft = wxGridNoCellCoords;
5104 m_selectingBottomRight = wxGridNoCellCoords;
5105 }
5106
5107 // Show the edit control, if it has been hidden for
5108 // drag-shrinking.
5109 ShowCellEditControl();
5110 }
5111 else if ( m_cursorMode == WXGRID_CURSOR_RESIZE_ROW )
5112 {
5113 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL);
5114 DoEndDragResizeRow();
5115
5116 // Note: we are ending the event *after* doing
5117 // default processing in this case
5118 //
5119 SendEvent( wxEVT_GRID_ROW_SIZE, m_dragRowOrCol, -1, event );
5120 }
5121 else if ( m_cursorMode == WXGRID_CURSOR_RESIZE_COL )
5122 {
5123 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL);
5124 DoEndDragResizeCol();
5125
5126 // Note: we are ending the event *after* doing
5127 // default processing in this case
5128 //
5129 SendEvent( wxEVT_GRID_COL_SIZE, -1, m_dragRowOrCol, event );
5130 }
5131
5132 m_dragLastPos = -1;
5133 }
5134
5135
5136 // ------------ Right button down
5137 //
5138 else if ( event.RightDown() && coords != wxGridNoCellCoords )
5139 {
5140 DisableCellEditControl();
5141 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_CLICK,
5142 coords.GetRow(),
5143 coords.GetCol(),
5144 event ) )
5145 {
5146 // no default action at the moment
5147 }
5148 }
5149
5150
5151 // ------------ Right double click
5152 //
5153 else if ( event.RightDClick() && coords != wxGridNoCellCoords )
5154 {
5155 DisableCellEditControl();
5156 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_DCLICK,
5157 coords.GetRow(),
5158 coords.GetCol(),
5159 event ) )
5160 {
5161 // no default action at the moment
5162 }
5163 }
5164
5165 // ------------ Moving and no button action
5166 //
5167 else if ( event.Moving() && !event.IsButton() )
5168 {
5169 int dragRow = YToEdgeOfRow( y );
5170 int dragCol = XToEdgeOfCol( x );
5171
5172 // Dragging on the corner of a cell to resize in both
5173 // directions is not implemented yet...
5174 //
5175 if ( dragRow >= 0 && dragCol >= 0 )
5176 {
5177 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL);
5178 return;
5179 }
5180
5181 if ( dragRow >= 0 )
5182 {
5183 m_dragRowOrCol = dragRow;
5184
5185 if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
5186 {
5187 if ( CanDragRowSize() && CanDragGridSize() )
5188 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW);
5189 }
5190
5191 if ( dragCol >= 0 )
5192 {
5193 m_dragRowOrCol = dragCol;
5194 }
5195
5196 return;
5197 }
5198
5199 if ( dragCol >= 0 )
5200 {
5201 m_dragRowOrCol = dragCol;
5202
5203 if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
5204 {
5205 if ( CanDragColSize() && CanDragGridSize() )
5206 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL);
5207 }
5208
5209 return;
5210 }
5211
5212 // Neither on a row or col edge
5213 //
5214 if ( m_cursorMode != WXGRID_CURSOR_SELECT_CELL )
5215 {
5216 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL);
5217 }
5218 }
5219}
5220
5221
5222void wxGrid::DoEndDragResizeRow()
5223{
5224 if ( m_dragLastPos >= 0 )
5225 {
5226 // erase the last line and resize the row
5227 //
5228 int cw, ch, left, dummy;
5229 m_gridWin->GetClientSize( &cw, &ch );
5230 CalcUnscrolledPosition( 0, 0, &left, &dummy );
5231
5232 wxClientDC dc( m_gridWin );
5233 PrepareDC( dc );
5234 dc.SetLogicalFunction( wxINVERT );
5235 dc.DrawLine( left, m_dragLastPos, left+cw, m_dragLastPos );
5236 HideCellEditControl();
5237 SaveEditControlValue();
5238
5239 int rowTop = GetRowTop(m_dragRowOrCol);
5240 SetRowSize( m_dragRowOrCol,
5241 wxMax( m_dragLastPos - rowTop, WXGRID_MIN_ROW_HEIGHT ) );
5242
5243 if ( !GetBatchCount() )
5244 {
5245 // Only needed to get the correct rect.y:
5246 wxRect rect ( CellToRect( m_dragRowOrCol, 0 ) );
5247 rect.x = 0;
5248 CalcScrolledPosition(0, rect.y, &dummy, &rect.y);
5249 rect.width = m_rowLabelWidth;
5250 rect.height = ch - rect.y;
5251 m_rowLabelWin->Refresh( TRUE, &rect );
5252 rect.width = cw;
5253 m_gridWin->Refresh( FALSE, &rect );
5254 }
5255
5256 ShowCellEditControl();
5257 }
5258}
5259
5260
5261void wxGrid::DoEndDragResizeCol()
5262{
5263 if ( m_dragLastPos >= 0 )
5264 {
5265 // erase the last line and resize the col
5266 //
5267 int cw, ch, dummy, top;
5268 m_gridWin->GetClientSize( &cw, &ch );
5269 CalcUnscrolledPosition( 0, 0, &dummy, &top );
5270
5271 wxClientDC dc( m_gridWin );
5272 PrepareDC( dc );
5273 dc.SetLogicalFunction( wxINVERT );
5274 dc.DrawLine( m_dragLastPos, top, m_dragLastPos, top+ch );
5275 HideCellEditControl();
5276 SaveEditControlValue();
5277
5278 int colLeft = GetColLeft(m_dragRowOrCol);
5279 SetColSize( m_dragRowOrCol,
5280 wxMax( m_dragLastPos - colLeft,
5281 GetColMinimalWidth(m_dragRowOrCol) ) );
5282
5283 if ( !GetBatchCount() )
5284 {
5285 // Only needed to get the correct rect.x:
5286 wxRect rect ( CellToRect( 0, m_dragRowOrCol ) );
5287 rect.y = 0;
5288 CalcScrolledPosition(rect.x, 0, &rect.x, &dummy);
5289 rect.width = cw - rect.x;
5290 rect.height = m_colLabelHeight;
5291 m_colLabelWin->Refresh( TRUE, &rect );
5292 rect.height = ch;
5293 m_gridWin->Refresh( FALSE, &rect );
5294 }
5295
5296 ShowCellEditControl();
5297 }
5298}
5299
5300
5301
5302//
5303// ------ interaction with data model
5304//
5305bool wxGrid::ProcessTableMessage( wxGridTableMessage& msg )
5306{
5307 switch ( msg.GetId() )
5308 {
5309 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES:
5310 return GetModelValues();
5311
5312 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES:
5313 return SetModelValues();
5314
5315 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED:
5316 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED:
5317 case wxGRIDTABLE_NOTIFY_ROWS_DELETED:
5318 case wxGRIDTABLE_NOTIFY_COLS_INSERTED:
5319 case wxGRIDTABLE_NOTIFY_COLS_APPENDED:
5320 case wxGRIDTABLE_NOTIFY_COLS_DELETED:
5321 return Redimension( msg );
5322
5323 default:
5324 return FALSE;
5325 }
5326}
5327
5328
5329
5330// The behaviour of this function depends on the grid table class
5331// Clear() function. For the default wxGridStringTable class the
5332// behavious is to replace all cell contents with wxEmptyString but
5333// not to change the number of rows or cols.
5334//
5335void wxGrid::ClearGrid()
5336{
5337 if ( m_table )
5338 {
5339 if (IsCellEditControlEnabled())
5340 DisableCellEditControl();
5341
5342 m_table->Clear();
5343 if ( !GetBatchCount() ) m_gridWin->Refresh();
5344 }
5345}
5346
5347
5348bool wxGrid::InsertRows( int pos, int numRows, bool WXUNUSED(updateLabels) )
5349{
5350 // TODO: something with updateLabels flag
5351
5352 if ( !m_created )
5353 {
5354 wxFAIL_MSG( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") );
5355 return FALSE;
5356 }
5357
5358 if ( m_table )
5359 {
5360 if (IsCellEditControlEnabled())
5361 DisableCellEditControl();
5362
5363 return m_table->InsertRows( pos, numRows );
5364
5365 // the table will have sent the results of the insert row
5366 // operation to this view object as a grid table message
5367 }
5368 return FALSE;
5369}
5370
5371
5372bool wxGrid::AppendRows( int numRows, bool WXUNUSED(updateLabels) )
5373{
5374 // TODO: something with updateLabels flag
5375
5376 if ( !m_created )
5377 {
5378 wxFAIL_MSG( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") );
5379 return FALSE;
5380 }
5381
5382 return ( m_table && m_table->AppendRows( numRows ) );
5383 // the table will have sent the results of the append row
5384 // operation to this view object as a grid table message
5385}
5386
5387
5388bool wxGrid::DeleteRows( int pos, int numRows, bool WXUNUSED(updateLabels) )
5389{
5390 // TODO: something with updateLabels flag
5391
5392 if ( !m_created )
5393 {
5394 wxFAIL_MSG( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") );
5395 return FALSE;
5396 }
5397
5398 if ( m_table )
5399 {
5400 if (IsCellEditControlEnabled())
5401 DisableCellEditControl();
5402
5403 return (m_table->DeleteRows( pos, numRows ));
5404 // the table will have sent the results of the delete row
5405 // operation to this view object as a grid table message
5406 }
5407 return FALSE;
5408}
5409
5410
5411bool wxGrid::InsertCols( int pos, int numCols, bool WXUNUSED(updateLabels) )
5412{
5413 // TODO: something with updateLabels flag
5414
5415 if ( !m_created )
5416 {
5417 wxFAIL_MSG( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") );
5418 return FALSE;
5419 }
5420
5421 if ( m_table )
5422 {
5423 if (IsCellEditControlEnabled())
5424 DisableCellEditControl();
5425
5426 return m_table->InsertCols( pos, numCols );
5427 // the table will have sent the results of the insert col
5428 // operation to this view object as a grid table message
5429 }
5430 return FALSE;
5431}
5432
5433
5434bool wxGrid::AppendCols( int numCols, bool WXUNUSED(updateLabels) )
5435{
5436 // TODO: something with updateLabels flag
5437
5438 if ( !m_created )
5439 {
5440 wxFAIL_MSG( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") );
5441 return FALSE;
5442 }
5443
5444 return ( m_table && m_table->AppendCols( numCols ) );
5445 // the table will have sent the results of the append col
5446 // operation to this view object as a grid table message
5447}
5448
5449
5450bool wxGrid::DeleteCols( int pos, int numCols, bool WXUNUSED(updateLabels) )
5451{
5452 // TODO: something with updateLabels flag
5453
5454 if ( !m_created )
5455 {
5456 wxFAIL_MSG( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") );
5457 return FALSE;
5458 }
5459
5460 if ( m_table )
5461 {
5462 if (IsCellEditControlEnabled())
5463 DisableCellEditControl();
5464
5465 return ( m_table->DeleteCols( pos, numCols ) );
5466 // the table will have sent the results of the delete col
5467 // operation to this view object as a grid table message
5468 }
5469 return FALSE;
5470}
5471
5472
5473
5474//
5475// ----- event handlers
5476//
5477
5478// Generate a grid event based on a mouse event and
5479// return the result of ProcessEvent()
5480//
5481int wxGrid::SendEvent( const wxEventType type,
5482 int row, int col,
5483 wxMouseEvent& mouseEv )
5484{
5485 bool claimed;
5486 bool vetoed= FALSE;
5487
5488 if ( type == wxEVT_GRID_ROW_SIZE || type == wxEVT_GRID_COL_SIZE )
5489 {
5490 int rowOrCol = (row == -1 ? col : row);
5491
5492 wxGridSizeEvent gridEvt( GetId(),
5493 type,
5494 this,
5495 rowOrCol,
5496 mouseEv.GetX() + GetRowLabelSize(),
5497 mouseEv.GetY() + GetColLabelSize(),
5498 mouseEv.ControlDown(),
5499 mouseEv.ShiftDown(),
5500 mouseEv.AltDown(),
5501 mouseEv.MetaDown() );
5502
5503 claimed = GetEventHandler()->ProcessEvent(gridEvt);
5504 vetoed = !gridEvt.IsAllowed();
5505 }
5506 else if ( type == wxEVT_GRID_RANGE_SELECT )
5507 {
5508 // Right now, it should _never_ end up here!
5509 wxGridRangeSelectEvent gridEvt( GetId(),
5510 type,
5511 this,
5512 m_selectingTopLeft,
5513 m_selectingBottomRight,
5514 TRUE,
5515 mouseEv.ControlDown(),
5516 mouseEv.ShiftDown(),
5517 mouseEv.AltDown(),
5518 mouseEv.MetaDown() );
5519
5520 claimed = GetEventHandler()->ProcessEvent(gridEvt);
5521 vetoed = !gridEvt.IsAllowed();
5522 }
5523 else
5524 {
5525 wxGridEvent gridEvt( GetId(),
5526 type,
5527 this,
5528 row, col,
5529 mouseEv.GetX() + GetRowLabelSize(),
5530 mouseEv.GetY() + GetColLabelSize(),
5531 FALSE,
5532 mouseEv.ControlDown(),
5533 mouseEv.ShiftDown(),
5534 mouseEv.AltDown(),
5535 mouseEv.MetaDown() );
5536 claimed = GetEventHandler()->ProcessEvent(gridEvt);
5537 vetoed = !gridEvt.IsAllowed();
5538 }
5539
5540 // A Veto'd event may not be `claimed' so test this first
5541 if (vetoed) return -1;
5542 return claimed ? 1 : 0;
5543}
5544
5545
5546// Generate a grid event of specified type and return the result
5547// of ProcessEvent().
5548//
5549int wxGrid::SendEvent( const wxEventType type,
5550 int row, int col )
5551{
5552 bool claimed;
5553 bool vetoed= FALSE;
5554
5555 if ( type == wxEVT_GRID_ROW_SIZE || type == wxEVT_GRID_COL_SIZE )
5556 {
5557 int rowOrCol = (row == -1 ? col : row);
5558
5559 wxGridSizeEvent gridEvt( GetId(),
5560 type,
5561 this,
5562 rowOrCol );
5563
5564 claimed = GetEventHandler()->ProcessEvent(gridEvt);
5565 vetoed = !gridEvt.IsAllowed();
5566 }
5567 else
5568 {
5569 wxGridEvent gridEvt( GetId(),
5570 type,
5571 this,
5572 row, col );
5573
5574 claimed = GetEventHandler()->ProcessEvent(gridEvt);
5575 vetoed = !gridEvt.IsAllowed();
5576 }
5577
5578 // A Veto'd event may not be `claimed' so test this first
5579 if (vetoed) return -1;
5580 return claimed ? 1 : 0;
5581}
5582
5583
5584void wxGrid::OnPaint( wxPaintEvent& WXUNUSED(event) )
5585{
5586 wxPaintDC dc(this); // needed to prevent zillions of paint events on MSW
5587}
5588
5589
5590void wxGrid::OnSize( wxSizeEvent& event )
5591{
5592 // position the child windows
5593 CalcWindowSizes();
5594
5595 // don't call CalcDimensions() from here, the base class handles the size
5596 // changes itself
5597 event.Skip();
5598}
5599
5600
5601void wxGrid::OnKeyDown( wxKeyEvent& event )
5602{
5603 if ( m_inOnKeyDown )
5604 {
5605 // shouldn't be here - we are going round in circles...
5606 //
5607 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") );
5608 }
5609
5610 m_inOnKeyDown = TRUE;
5611
5612 // propagate the event up and see if it gets processed
5613 //
5614 wxWindow *parent = GetParent();
5615 wxKeyEvent keyEvt( event );
5616 keyEvt.SetEventObject( parent );
5617
5618 if ( !parent->GetEventHandler()->ProcessEvent( keyEvt ) )
5619 {
5620
5621 // try local handlers
5622 //
5623 switch ( event.KeyCode() )
5624 {
5625 case WXK_UP:
5626 if ( event.ControlDown() )
5627 {
5628 MoveCursorUpBlock( event.ShiftDown() );
5629 }
5630 else
5631 {
5632 MoveCursorUp( event.ShiftDown() );
5633 }
5634 break;
5635
5636 case WXK_DOWN:
5637 if ( event.ControlDown() )
5638 {
5639 MoveCursorDownBlock( event.ShiftDown() );
5640 }
5641 else
5642 {
5643 MoveCursorDown( event.ShiftDown() );
5644 }
5645 break;
5646
5647 case WXK_LEFT:
5648 if ( event.ControlDown() )
5649 {
5650 MoveCursorLeftBlock( event.ShiftDown() );
5651 }
5652 else
5653 {
5654 MoveCursorLeft( event.ShiftDown() );
5655 }
5656 break;
5657
5658 case WXK_RIGHT:
5659 if ( event.ControlDown() )
5660 {
5661 MoveCursorRightBlock( event.ShiftDown() );
5662 }
5663 else
5664 {
5665 MoveCursorRight( event.ShiftDown() );
5666 }
5667 break;
5668
5669 case WXK_RETURN:
5670 case WXK_NUMPAD_ENTER:
5671 if ( event.ControlDown() )
5672 {
5673 event.Skip(); // to let the edit control have the return
5674 }
5675 else
5676 {
5677 if ( GetGridCursorRow() < GetNumberRows()-1 )
5678 {
5679 MoveCursorDown( event.ShiftDown() );
5680 }
5681 else
5682 {
5683 // at the bottom of a column
5684 HideCellEditControl();
5685 SaveEditControlValue();
5686 }
5687 }
5688 break;
5689
5690 case WXK_ESCAPE:
5691 ClearSelection();
5692 break;
5693
5694 case WXK_TAB:
5695 if (event.ShiftDown())
5696 {
5697 if ( GetGridCursorCol() > 0 )
5698 {
5699 MoveCursorLeft( FALSE );
5700 }
5701 else
5702 {
5703 // at left of grid
5704 HideCellEditControl();
5705 SaveEditControlValue();
5706 }
5707 }
5708 else
5709 {
5710 if ( GetGridCursorCol() < GetNumberCols()-1 )
5711 {
5712 MoveCursorRight( FALSE );
5713 }
5714 else
5715 {
5716 // at right of grid
5717 HideCellEditControl();
5718 SaveEditControlValue();
5719 }
5720 }
5721 break;
5722
5723 case WXK_HOME:
5724 if ( event.ControlDown() )
5725 {
5726 MakeCellVisible( 0, 0 );
5727 SetCurrentCell( 0, 0 );
5728 }
5729 else
5730 {
5731 event.Skip();
5732 }
5733 break;
5734
5735 case WXK_END:
5736 if ( event.ControlDown() )
5737 {
5738 MakeCellVisible( m_numRows-1, m_numCols-1 );
5739 SetCurrentCell( m_numRows-1, m_numCols-1 );
5740 }
5741 else
5742 {
5743 event.Skip();
5744 }
5745 break;
5746
5747 case WXK_PRIOR:
5748 MovePageUp();
5749 break;
5750
5751 case WXK_NEXT:
5752 MovePageDown();
5753 break;
5754
5755 case WXK_SPACE:
5756 if ( event.ControlDown() )
5757 {
5758 m_selection->ToggleCellSelection( m_currentCellCoords.GetRow(),
5759 m_currentCellCoords.GetCol(),
5760 event.ControlDown(),
5761 event.ShiftDown(),
5762 event.AltDown(),
5763 event.MetaDown() );
5764 break;
5765 }
5766 if ( !IsEditable() )
5767 {
5768 MoveCursorRight( FALSE );
5769 break;
5770 }
5771 // Otherwise fall through to default
5772
5773 default:
5774 // is it possible to edit the current cell at all?
5775 if ( !IsCellEditControlEnabled() && CanEnableCellControl() )
5776 {
5777 // yes, now check whether the cells editor accepts the key
5778 int row = m_currentCellCoords.GetRow();
5779 int col = m_currentCellCoords.GetCol();
5780 wxGridCellAttr* attr = GetCellAttr(row, col);
5781 wxGridCellEditor *editor = attr->GetEditor(this, row, col);
5782
5783 // <F2> is special and will always start editing, for
5784 // other keys - ask the editor itself
5785 if ( (event.KeyCode() == WXK_F2 && !event.HasModifiers())
5786 || editor->IsAcceptedKey(event) )
5787 {
5788 EnableCellEditControl();
5789
5790 // the editor could be not shown for a variety of
5791 // reasons (i.e. blocked by the app or whatever), so
5792 // check if it really was created
5793 if ( m_cellEditCtrlEnabled )
5794 {
5795 editor->StartingKey(event);
5796 }
5797 }
5798 else
5799 {
5800 event.Skip();
5801 }
5802
5803 editor->DecRef();
5804 attr->DecRef();
5805 }
5806 else
5807 {
5808 // let others process char events with modifiers or all
5809 // char events for readonly cells
5810 event.Skip();
5811 }
5812 break;
5813 }
5814 }
5815
5816 m_inOnKeyDown = FALSE;
5817}
5818
5819void wxGrid::OnKeyUp( wxKeyEvent& event )
5820{
5821 // try local handlers
5822 //
5823 if ( event.KeyCode() == WXK_SHIFT )
5824 {
5825 if ( m_selectingTopLeft != wxGridNoCellCoords &&
5826 m_selectingBottomRight != wxGridNoCellCoords )
5827 m_selection->SelectBlock( m_selectingTopLeft.GetRow(),
5828 m_selectingTopLeft.GetCol(),
5829 m_selectingBottomRight.GetRow(),
5830 m_selectingBottomRight.GetCol(),
5831 event.ControlDown(),
5832 TRUE,
5833 event.AltDown(),
5834 event.MetaDown() );
5835 m_selectingTopLeft = wxGridNoCellCoords;
5836 m_selectingBottomRight = wxGridNoCellCoords;
5837 m_selectingKeyboard = wxGridNoCellCoords;
5838 }
5839}
5840
5841void wxGrid::OnEraseBackground(wxEraseEvent&)
5842{
5843}
5844
5845void wxGrid::SetCurrentCell( const wxGridCellCoords& coords )
5846{
5847 if ( SendEvent( wxEVT_GRID_SELECT_CELL, coords.GetRow(), coords.GetCol() ) )
5848 {
5849 // the event has been intercepted - do nothing
5850 return;
5851 }
5852
5853 wxClientDC dc(m_gridWin);
5854 PrepareDC(dc);
5855
5856 if ( m_currentCellCoords != wxGridNoCellCoords )
5857 {
5858 HideCellEditControl();
5859 DisableCellEditControl();
5860
5861 if ( IsVisible( m_currentCellCoords, FALSE ) )
5862 {
5863 wxRect r;
5864 r = BlockToDeviceRect(m_currentCellCoords, coords);
5865 if ( !m_gridLinesEnabled )
5866 {
5867 r.x--;
5868 r.y--;
5869 r.width++;
5870 r.height++;
5871 }
5872
5873 wxGridCellCoordsArray cells = CalcCellsExposed( r );
5874
5875 // Otherwise refresh redraws the highlight!
5876 m_currentCellCoords = coords;
5877
5878 DrawGridCellArea(dc,cells);
5879 DrawAllGridLines( dc, r );
5880 }
5881 }
5882
5883 m_currentCellCoords = coords;
5884
5885 wxGridCellAttr* attr = GetCellAttr(coords);
5886 DrawCellHighlight(dc, attr);
5887 attr->DecRef();
5888}
5889
5890
5891void wxGrid::HighlightBlock( int topRow, int leftCol, int bottomRow, int rightCol )
5892{
5893 int temp;
5894 wxGridCellCoords updateTopLeft, updateBottomRight;
5895
5896 if ( m_selection->GetSelectionMode() == wxGrid::wxGridSelectRows )
5897 {
5898 leftCol = 0;
5899 rightCol = GetNumberCols() - 1;
5900 }
5901 else if ( m_selection->GetSelectionMode() == wxGrid::wxGridSelectColumns )
5902 {
5903 topRow = 0;
5904 bottomRow = GetNumberRows() - 1;
5905 }
5906 if ( topRow > bottomRow )
5907 {
5908 temp = topRow;
5909 topRow = bottomRow;
5910 bottomRow = temp;
5911 }
5912
5913 if ( leftCol > rightCol )
5914 {
5915 temp = leftCol;
5916 leftCol = rightCol;
5917 rightCol = temp;
5918 }
5919
5920 updateTopLeft = wxGridCellCoords( topRow, leftCol );
5921 updateBottomRight = wxGridCellCoords( bottomRow, rightCol );
5922
5923 if ( m_selectingTopLeft != updateTopLeft ||
5924 m_selectingBottomRight != updateBottomRight )
5925 {
5926 // Compute two optimal update rectangles:
5927 // Either one rectangle is a real subset of the
5928 // other, or they are (almost) disjoint!
5929 wxRect rect[4];
5930 bool need_refresh[4];
5931 need_refresh[0] =
5932 need_refresh[1] =
5933 need_refresh[2] =
5934 need_refresh[3] = FALSE;
5935 int i;
5936
5937 // Store intermediate values
5938 wxCoord oldLeft = m_selectingTopLeft.GetCol();
5939 wxCoord oldTop = m_selectingTopLeft.GetRow();
5940 wxCoord oldRight = m_selectingBottomRight.GetCol();
5941 wxCoord oldBottom = m_selectingBottomRight.GetRow();
5942
5943 // Determine the outer/inner coordinates.
5944 if (oldLeft > leftCol)
5945 {
5946 temp = oldLeft;
5947 oldLeft = leftCol;
5948 leftCol = temp;
5949 }
5950 if (oldTop > topRow )
5951 {
5952 temp = oldTop;
5953 oldTop = topRow;
5954 topRow = temp;
5955 }
5956 if (oldRight < rightCol )
5957 {
5958 temp = oldRight;
5959 oldRight = rightCol;
5960 rightCol = temp;
5961 }
5962 if (oldBottom < bottomRow)
5963 {
5964 temp = oldBottom;
5965 oldBottom = bottomRow;
5966 bottomRow = temp;
5967 }
5968
5969 // Now, either the stuff marked old is the outer
5970 // rectangle or we don't have a situation where one
5971 // is contained in the other.
5972
5973 if ( oldLeft < leftCol )
5974 {
5975 need_refresh[0] = TRUE;
5976 rect[0] = BlockToDeviceRect( wxGridCellCoords ( oldTop,
5977 oldLeft ),
5978 wxGridCellCoords ( oldBottom,
5979 leftCol - 1 ) );
5980 }
5981
5982 if ( oldTop < topRow )
5983 {
5984 need_refresh[1] = TRUE;
5985 rect[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop,
5986 leftCol ),
5987 wxGridCellCoords ( topRow - 1,
5988 rightCol ) );
5989 }
5990
5991 if ( oldRight > rightCol )
5992 {
5993 need_refresh[2] = TRUE;
5994 rect[2] = BlockToDeviceRect( wxGridCellCoords ( oldTop,
5995 rightCol + 1 ),
5996 wxGridCellCoords ( oldBottom,
5997 oldRight ) );
5998 }
5999
6000 if ( oldBottom > bottomRow )
6001 {
6002 need_refresh[3] = TRUE;
6003 rect[3] = BlockToDeviceRect( wxGridCellCoords ( bottomRow + 1,
6004 leftCol ),
6005 wxGridCellCoords ( oldBottom,
6006 rightCol ) );
6007 }
6008
6009
6010 // Change Selection
6011 m_selectingTopLeft = updateTopLeft;
6012 m_selectingBottomRight = updateBottomRight;
6013
6014 // various Refresh() calls
6015 for (i = 0; i < 4; i++ )
6016 if ( need_refresh[i] && rect[i] != wxGridNoCellRect )
6017 m_gridWin->Refresh( FALSE, &(rect[i]) );
6018 }
6019
6020 // never generate an event as it will be generated from
6021 // wxGridSelection::SelectBlock!
6022 // (old comment from when this was the body of SelectBlock)
6023}
6024
6025//
6026// ------ functions to get/send data (see also public functions)
6027//
6028
6029bool wxGrid::GetModelValues()
6030{
6031 if ( m_table )
6032 {
6033 // all we need to do is repaint the grid
6034 //
6035 m_gridWin->Refresh();
6036 return TRUE;
6037 }
6038
6039 return FALSE;
6040}
6041
6042
6043bool wxGrid::SetModelValues()
6044{
6045 int row, col;
6046
6047 if ( m_table )
6048 {
6049 for ( row = 0; row < m_numRows; row++ )
6050 {
6051 for ( col = 0; col < m_numCols; col++ )
6052 {
6053 m_table->SetValue( row, col, GetCellValue(row, col) );
6054 }
6055 }
6056
6057 return TRUE;
6058 }
6059
6060 return FALSE;
6061}
6062
6063
6064
6065// Note - this function only draws cells that are in the list of
6066// exposed cells (usually set from the update region by
6067// CalcExposedCells)
6068//
6069void wxGrid::DrawGridCellArea( wxDC& dc, const wxGridCellCoordsArray& cells )
6070{
6071 if ( !m_numRows || !m_numCols ) return;
6072
6073 size_t i;
6074 size_t numCells = cells.GetCount();
6075
6076 for ( i = 0; i < numCells; i++ )
6077 {
6078 DrawCell( dc, cells[i] );
6079 }
6080}
6081
6082
6083void wxGrid::DrawGridSpace( wxDC& dc )
6084{
6085 int cw, ch;
6086 m_gridWin->GetClientSize( &cw, &ch );
6087
6088 int right, bottom;
6089 CalcUnscrolledPosition( cw, ch, &right, &bottom );
6090
6091 int rightCol = m_numCols > 0 ? GetColRight(m_numCols - 1) : 0;
6092 int bottomRow = m_numRows > 0 ? GetRowBottom(m_numRows - 1) : 0 ;
6093
6094 if ( right > rightCol || bottom > bottomRow )
6095 {
6096 int left, top;
6097 CalcUnscrolledPosition( 0, 0, &left, &top );
6098
6099 dc.SetBrush( wxBrush(GetDefaultCellBackgroundColour(), wxSOLID) );
6100 dc.SetPen( *wxTRANSPARENT_PEN );
6101
6102 if ( right > rightCol )
6103 {
6104 dc.DrawRectangle( rightCol, top, right - rightCol, ch);
6105 }
6106
6107 if ( bottom > bottomRow )
6108 {
6109 dc.DrawRectangle( left, bottomRow, cw, bottom - bottomRow);
6110 }
6111 }
6112}
6113
6114
6115void wxGrid::DrawCell( wxDC& dc, const wxGridCellCoords& coords )
6116{
6117 int row = coords.GetRow();
6118 int col = coords.GetCol();
6119
6120 if ( GetColWidth(col) <= 0 || GetRowHeight(row) <= 0 )
6121 return;
6122
6123 // we draw the cell border ourselves
6124#if !WXGRID_DRAW_LINES
6125 if ( m_gridLinesEnabled )
6126 DrawCellBorder( dc, coords );
6127#endif
6128
6129 wxGridCellAttr* attr = GetCellAttr(row, col);
6130
6131 bool isCurrent = coords == m_currentCellCoords;
6132
6133 wxRect rect = CellToRect( row, col );
6134
6135 // if the editor is shown, we should use it and not the renderer
6136 // Note: However, only if it is really _shown_, i.e. not hidden!
6137 if ( isCurrent && IsCellEditControlShown() )
6138 {
6139 wxGridCellEditor *editor = attr->GetEditor(this, row, col);
6140 editor->PaintBackground(rect, attr);
6141 editor->DecRef();
6142 }
6143 else
6144 {
6145 // but all the rest is drawn by the cell renderer and hence may be
6146 // customized
6147 wxGridCellRenderer *renderer = attr->GetRenderer(this, row, col);
6148 renderer->Draw(*this, *attr, dc, rect, row, col, IsInSelection(coords));
6149 renderer->DecRef();
6150 }
6151
6152 attr->DecRef();
6153}
6154
6155void wxGrid::DrawCellHighlight( wxDC& dc, const wxGridCellAttr *attr )
6156{
6157 int row = m_currentCellCoords.GetRow();
6158 int col = m_currentCellCoords.GetCol();
6159
6160 if ( GetColWidth(col) <= 0 || GetRowHeight(row) <= 0 )
6161 return;
6162
6163 wxRect rect = CellToRect(row, col);
6164
6165 // hmmm... what could we do here to show that the cell is disabled?
6166 // for now, I just draw a thinner border than for the other ones, but
6167 // it doesn't look really good
6168
6169 int penWidth = attr->IsReadOnly() ? m_cellHighlightROPenWidth : m_cellHighlightPenWidth;
6170
6171 if (penWidth > 0) {
6172
6173 // The center of th drawn line is where the position/width/height of
6174 // the rectangle is actually at, (on wxMSW atr least,) so we will
6175 // reduce the size of the rectangle to compensate for the thickness of
6176 // the line. If this is too strange on non wxMSW platforms then
6177 // please #ifdef this appropriately.
6178 rect.x += penWidth/2;
6179 rect.y += penWidth/2;
6180 rect.width -= penWidth-1;
6181 rect.height -= penWidth-1;
6182
6183
6184 // Now draw the rectangle
6185 dc.SetPen(wxPen(m_cellHighlightColour, penWidth, wxSOLID));
6186 dc.SetBrush(*wxTRANSPARENT_BRUSH);
6187 dc.DrawRectangle(rect);
6188 }
6189
6190#if 0
6191 // VZ: my experiments with 3d borders...
6192
6193 // how to properly set colours for arbitrary bg?
6194 wxCoord x1 = rect.x,
6195 y1 = rect.y,
6196 x2 = rect.x + rect.width -1,
6197 y2 = rect.y + rect.height -1;
6198
6199 dc.SetPen(*wxWHITE_PEN);
6200 dc.DrawLine(x1, y1, x2, y1);
6201 dc.DrawLine(x1, y1, x1, y2);
6202
6203 dc.DrawLine(x1 + 1, y2 - 1, x2 - 1, y2 - 1);
6204 dc.DrawLine(x2 - 1, y1 + 1, x2 - 1, y2 );
6205
6206 dc.SetPen(*wxBLACK_PEN);
6207 dc.DrawLine(x1, y2, x2, y2);
6208 dc.DrawLine(x2, y1, x2, y2+1);
6209#endif // 0
6210}
6211
6212
6213void wxGrid::DrawCellBorder( wxDC& dc, const wxGridCellCoords& coords )
6214{
6215 int row = coords.GetRow();
6216 int col = coords.GetCol();
6217 if ( GetColWidth(col) <= 0 || GetRowHeight(row) <= 0 )
6218 return;
6219
6220 dc.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID) );
6221
6222 // right hand border
6223 //
6224 dc.DrawLine( GetColRight(col), GetRowTop(row),
6225 GetColRight(col), GetRowBottom(row) );
6226
6227 // bottom border
6228 //
6229 dc.DrawLine( GetColLeft(col), GetRowBottom(row),
6230 GetColRight(col), GetRowBottom(row) );
6231}
6232
6233void wxGrid::DrawHighlight(wxDC& dc,const wxGridCellCoordsArray& cells)
6234{
6235 // This if block was previously in wxGrid::OnPaint but that doesn't
6236 // seem to get called under wxGTK - MB
6237 //
6238 if ( m_currentCellCoords == wxGridNoCellCoords &&
6239 m_numRows && m_numCols )
6240 {
6241 m_currentCellCoords.Set(0, 0);
6242 }
6243
6244 if ( IsCellEditControlShown() )
6245 {
6246 // don't show highlight when the edit control is shown
6247 return;
6248 }
6249
6250 // if the active cell was repainted, repaint its highlight too because it
6251 // might have been damaged by the grid lines
6252 size_t count = cells.GetCount();
6253 for ( size_t n = 0; n < count; n++ )
6254 {
6255 if ( cells[n] == m_currentCellCoords )
6256 {
6257 wxGridCellAttr* attr = GetCellAttr(m_currentCellCoords);
6258 DrawCellHighlight(dc, attr);
6259 attr->DecRef();
6260
6261 break;
6262 }
6263 }
6264}
6265
6266// TODO: remove this ???
6267// This is used to redraw all grid lines e.g. when the grid line colour
6268// has been changed
6269//
6270void wxGrid::DrawAllGridLines( wxDC& dc, const wxRegion & WXUNUSED(reg) )
6271{
6272 if ( !m_gridLinesEnabled ||
6273 !m_numRows ||
6274 !m_numCols ) return;
6275
6276 int top, bottom, left, right;
6277
6278#if 0 //#ifndef __WXGTK__
6279 if (reg.IsEmpty())
6280 {
6281 int cw, ch;
6282 m_gridWin->GetClientSize(&cw, &ch);
6283
6284 // virtual coords of visible area
6285 //
6286 CalcUnscrolledPosition( 0, 0, &left, &top );
6287 CalcUnscrolledPosition( cw, ch, &right, &bottom );
6288 }
6289 else
6290 {
6291 wxCoord x, y, w, h;
6292 reg.GetBox(x, y, w, h);
6293 CalcUnscrolledPosition( x, y, &left, &top );
6294 CalcUnscrolledPosition( x + w, y + h, &right, &bottom );
6295 }
6296#else
6297 int cw, ch;
6298 m_gridWin->GetClientSize(&cw, &ch);
6299 CalcUnscrolledPosition( 0, 0, &left, &top );
6300 CalcUnscrolledPosition( cw, ch, &right, &bottom );
6301#endif
6302
6303 // avoid drawing grid lines past the last row and col
6304 //
6305 right = wxMin( right, GetColRight(m_numCols - 1) );
6306 bottom = wxMin( bottom, GetRowBottom(m_numRows - 1) );
6307
6308 dc.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID) );
6309
6310 // horizontal grid lines
6311 //
6312 int i;
6313 for ( i = internalYToRow(top); i < m_numRows; i++ )
6314 {
6315 int bot = GetRowBottom(i) - 1;
6316
6317 if ( bot > bottom )
6318 {
6319 break;
6320 }
6321
6322 if ( bot >= top )
6323 {
6324 dc.DrawLine( left, bot, right, bot );
6325 }
6326 }
6327
6328
6329 // vertical grid lines
6330 //
6331 for ( i = internalXToCol(left); i < m_numCols; i++ )
6332 {
6333 int colRight = GetColRight(i) - 1;
6334 if ( colRight > right )
6335 {
6336 break;
6337 }
6338
6339 if ( colRight >= left )
6340 {
6341 dc.DrawLine( colRight, top, colRight, bottom );
6342 }
6343 }
6344}
6345
6346
6347void wxGrid::DrawRowLabels( wxDC& dc ,const wxArrayInt& rows)
6348{
6349 if ( !m_numRows ) return;
6350
6351 size_t i;
6352 size_t numLabels = rows.GetCount();
6353
6354 for ( i = 0; i < numLabels; i++ )
6355 {
6356 DrawRowLabel( dc, rows[i] );
6357 }
6358}
6359
6360
6361void wxGrid::DrawRowLabel( wxDC& dc, int row )
6362{
6363 if ( GetRowHeight(row) <= 0 )
6364 return;
6365
6366 int rowTop = GetRowTop(row),
6367 rowBottom = GetRowBottom(row) - 1;
6368
6369 dc.SetPen( *wxBLACK_PEN );
6370 dc.DrawLine( m_rowLabelWidth-1, rowTop,
6371 m_rowLabelWidth-1, rowBottom );
6372
6373 dc.DrawLine( 0, rowBottom, m_rowLabelWidth-1, rowBottom );
6374
6375 dc.SetPen( *wxWHITE_PEN );
6376 dc.DrawLine( 0, rowTop, 0, rowBottom );
6377 dc.DrawLine( 0, rowTop, m_rowLabelWidth-1, rowTop );
6378
6379 dc.SetBackgroundMode( wxTRANSPARENT );
6380 dc.SetTextForeground( GetLabelTextColour() );
6381 dc.SetFont( GetLabelFont() );
6382
6383 int hAlign, vAlign;
6384 GetRowLabelAlignment( &hAlign, &vAlign );
6385
6386 wxRect rect;
6387 rect.SetX( 2 );
6388 rect.SetY( GetRowTop(row) + 2 );
6389 rect.SetWidth( m_rowLabelWidth - 4 );
6390 rect.SetHeight( GetRowHeight(row) - 4 );
6391 DrawTextRectangle( dc, GetRowLabelValue( row ), rect, hAlign, vAlign );
6392}
6393
6394
6395void wxGrid::DrawColLabels( wxDC& dc,const wxArrayInt& cols )
6396{
6397 if ( !m_numCols ) return;
6398
6399 size_t i;
6400 size_t numLabels = cols.GetCount();
6401
6402 for ( i = 0; i < numLabels; i++ )
6403 {
6404 DrawColLabel( dc, cols[i] );
6405 }
6406}
6407
6408
6409void wxGrid::DrawColLabel( wxDC& dc, int col )
6410{
6411 if ( GetColWidth(col) <= 0 )
6412 return;
6413
6414 int colLeft = GetColLeft(col),
6415 colRight = GetColRight(col) - 1;
6416
6417 dc.SetPen( *wxBLACK_PEN );
6418 dc.DrawLine( colRight, 0,
6419 colRight, m_colLabelHeight-1 );
6420
6421 dc.DrawLine( colLeft, m_colLabelHeight-1,
6422 colRight, m_colLabelHeight-1 );
6423
6424 dc.SetPen( *wxWHITE_PEN );
6425 dc.DrawLine( colLeft, 0, colLeft, m_colLabelHeight-1 );
6426 dc.DrawLine( colLeft, 0, colRight, 0 );
6427
6428 dc.SetBackgroundMode( wxTRANSPARENT );
6429 dc.SetTextForeground( GetLabelTextColour() );
6430 dc.SetFont( GetLabelFont() );
6431
6432 dc.SetBackgroundMode( wxTRANSPARENT );
6433 dc.SetTextForeground( GetLabelTextColour() );
6434 dc.SetFont( GetLabelFont() );
6435
6436 int hAlign, vAlign;
6437 GetColLabelAlignment( &hAlign, &vAlign );
6438
6439 wxRect rect;
6440 rect.SetX( colLeft + 2 );
6441 rect.SetY( 2 );
6442 rect.SetWidth( GetColWidth(col) - 4 );
6443 rect.SetHeight( m_colLabelHeight - 4 );
6444 DrawTextRectangle( dc, GetColLabelValue( col ), rect, hAlign, vAlign );
6445}
6446
6447void wxGrid::DrawTextRectangle( wxDC& dc,
6448 const wxString& value,
6449 const wxRect& rect,
6450 int horizAlign,
6451 int vertAlign )
6452{
6453 wxArrayString lines;
6454
6455 dc.SetClippingRegion( rect );
6456 StringToLines( value, lines );
6457
6458
6459 //Forward to new API.
6460 DrawTextRectangle( dc,
6461 lines,
6462 rect,
6463 horizAlign,
6464 vertAlign );
6465
6466}
6467
6468void wxGrid::DrawTextRectangle( wxDC& dc,
6469 const wxArrayString& lines,
6470 const wxRect& rect,
6471 int horizAlign,
6472 int vertAlign )
6473{
6474 long textWidth, textHeight;
6475 long lineWidth, lineHeight;
6476
6477 if ( lines.GetCount() )
6478 {
6479 GetTextBoxSize( dc, lines, &textWidth, &textHeight );
6480 dc.GetTextExtent( lines[0], &lineWidth, &lineHeight );
6481
6482 float x, y;
6483 switch ( horizAlign )
6484 {
6485 case wxALIGN_RIGHT:
6486 x = rect.x + (rect.width - textWidth - 1);
6487 break;
6488
6489 case wxALIGN_CENTRE:
6490 x = rect.x + ((rect.width - textWidth)/2);
6491 break;
6492
6493 case wxALIGN_LEFT:
6494 default:
6495 x = rect.x + 1;
6496 break;
6497 }
6498
6499 switch ( vertAlign )
6500 {
6501 case wxALIGN_BOTTOM:
6502 y = rect.y + (rect.height - textHeight - 1);
6503 break;
6504
6505 case wxALIGN_CENTRE:
6506 y = rect.y + ((rect.height - textHeight)/2);
6507 break;
6508
6509 case wxALIGN_TOP:
6510 default:
6511 y = rect.y + 1;
6512 break;
6513 }
6514
6515 for ( size_t i = 0; i < lines.GetCount(); i++ )
6516 {
6517 dc.DrawText( lines[i], (int)x, (int)y );
6518 y += lineHeight;
6519 }
6520 }
6521
6522 dc.DestroyClippingRegion();
6523}
6524
6525
6526// Split multi line text up into an array of strings. Any existing
6527// contents of the string array are preserved.
6528//
6529void wxGrid::StringToLines( const wxString& value, wxArrayString& lines )
6530{
6531 int startPos = 0;
6532 int pos;
6533 wxString eol = wxTextFile::GetEOL( wxTextFileType_Unix );
6534 wxString tVal = wxTextFile::Translate( value, wxTextFileType_Unix );
6535
6536 while ( startPos < (int)tVal.Length() )
6537 {
6538 pos = tVal.Mid(startPos).Find( eol );
6539 if ( pos < 0 )
6540 {
6541 break;
6542 }
6543 else if ( pos == 0 )
6544 {
6545 lines.Add( wxEmptyString );
6546 }
6547 else
6548 {
6549 lines.Add( value.Mid(startPos, pos) );
6550 }
6551 startPos += pos+1;
6552 }
6553 if ( startPos < (int)value.Length() )
6554 {
6555 lines.Add( value.Mid( startPos ) );
6556 }
6557}
6558
6559
6560void wxGrid::GetTextBoxSize( wxDC& dc,
6561 const wxArrayString& lines,
6562 long *width, long *height )
6563{
6564 long w = 0;
6565 long h = 0;
6566 long lineW, lineH;
6567
6568 size_t i;
6569 for ( i = 0; i < lines.GetCount(); i++ )
6570 {
6571 dc.GetTextExtent( lines[i], &lineW, &lineH );
6572 w = wxMax( w, lineW );
6573 h += lineH;
6574 }
6575
6576 *width = w;
6577 *height = h;
6578}
6579
6580//
6581// ------ Batch processing.
6582//
6583void wxGrid::EndBatch()
6584{
6585 if ( m_batchCount > 0 )
6586 {
6587 m_batchCount--;
6588 if ( !m_batchCount )
6589 {
6590 CalcDimensions();
6591 m_rowLabelWin->Refresh();
6592 m_colLabelWin->Refresh();
6593 m_cornerLabelWin->Refresh();
6594 m_gridWin->Refresh();
6595 }
6596 }
6597}
6598
6599// Use this, rather than wxWindow::Refresh(), to force an immediate
6600// repainting of the grid. Has no effect if you are already inside a
6601// BeginBatch / EndBatch block.
6602//
6603void wxGrid::ForceRefresh()
6604{
6605 BeginBatch();
6606 EndBatch();
6607}
6608
6609
6610//
6611// ------ Edit control functions
6612//
6613
6614
6615void wxGrid::EnableEditing( bool edit )
6616{
6617 // TODO: improve this ?
6618 //
6619 if ( edit != m_editable )
6620 {
6621 if(!edit) EnableCellEditControl(edit);
6622 m_editable = edit;
6623 }
6624}
6625
6626
6627void wxGrid::EnableCellEditControl( bool enable )
6628{
6629 if (! m_editable)
6630 return;
6631
6632 if ( m_currentCellCoords == wxGridNoCellCoords )
6633 SetCurrentCell( 0, 0 );
6634
6635 if ( enable != m_cellEditCtrlEnabled )
6636 {
6637 if ( enable )
6638 {
6639 if (SendEvent( wxEVT_GRID_EDITOR_SHOWN) <0)
6640 return;
6641
6642 // this should be checked by the caller!
6643 wxASSERT_MSG( CanEnableCellControl(),
6644 _T("can't enable editing for this cell!") );
6645
6646 // do it before ShowCellEditControl()
6647 m_cellEditCtrlEnabled = enable;
6648
6649 ShowCellEditControl();
6650 }
6651 else
6652 {
6653 //FIXME:add veto support
6654 SendEvent( wxEVT_GRID_EDITOR_HIDDEN);
6655
6656 HideCellEditControl();
6657 SaveEditControlValue();
6658
6659 // do it after HideCellEditControl()
6660 m_cellEditCtrlEnabled = enable;
6661 }
6662 }
6663}
6664
6665bool wxGrid::IsCurrentCellReadOnly() const
6666{
6667 // const_cast
6668 wxGridCellAttr* attr = ((wxGrid *)this)->GetCellAttr(m_currentCellCoords);
6669 bool readonly = attr->IsReadOnly();
6670 attr->DecRef();
6671
6672 return readonly;
6673}
6674
6675bool wxGrid::CanEnableCellControl() const
6676{
6677 return m_editable && !IsCurrentCellReadOnly();
6678}
6679
6680bool wxGrid::IsCellEditControlEnabled() const
6681{
6682 // the cell edit control might be disable for all cells or just for the
6683 // current one if it's read only
6684 return m_cellEditCtrlEnabled ? !IsCurrentCellReadOnly() : FALSE;
6685}
6686
6687bool wxGrid::IsCellEditControlShown() const
6688{
6689 bool isShown = FALSE;
6690
6691 if ( m_cellEditCtrlEnabled )
6692 {
6693 int row = m_currentCellCoords.GetRow();
6694 int col = m_currentCellCoords.GetCol();
6695 wxGridCellAttr* attr = GetCellAttr(row, col);
6696 wxGridCellEditor* editor = attr->GetEditor((wxGrid*) this, row, col);
6697 attr->DecRef();
6698
6699 if ( editor )
6700 {
6701 if ( editor->IsCreated() )
6702 {
6703 isShown = editor->GetControl()->IsShown();
6704 }
6705
6706 editor->DecRef();
6707 }
6708 }
6709
6710 return isShown;
6711}
6712
6713void wxGrid::ShowCellEditControl()
6714{
6715 if ( IsCellEditControlEnabled() )
6716 {
6717 if ( !IsVisible( m_currentCellCoords ) )
6718 {
6719 m_cellEditCtrlEnabled = FALSE;
6720 return;
6721 }
6722 else
6723 {
6724 wxRect rect = CellToRect( m_currentCellCoords );
6725 int row = m_currentCellCoords.GetRow();
6726 int col = m_currentCellCoords.GetCol();
6727
6728 // convert to scrolled coords
6729 //
6730 CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
6731
6732 // done in PaintBackground()
6733#if 0
6734 // erase the highlight and the cell contents because the editor
6735 // might not cover the entire cell
6736 wxClientDC dc( m_gridWin );
6737 PrepareDC( dc );
6738 dc.SetBrush(*wxLIGHT_GREY_BRUSH); //wxBrush(attr->GetBackgroundColour(), wxSOLID));
6739 dc.SetPen(*wxTRANSPARENT_PEN);
6740 dc.DrawRectangle(rect);
6741#endif // 0
6742
6743 // cell is shifted by one pixel
6744 // However, don't allow x or y to become negative
6745 // since the SetSize() method interprets that as
6746 // "don't change."
6747 if (rect.x > 0)
6748 rect.x--;
6749 if (rect.y > 0)
6750 rect.y--;
6751
6752 wxGridCellAttr* attr = GetCellAttr(row, col);
6753 wxGridCellEditor* editor = attr->GetEditor(this, row, col);
6754 if ( !editor->IsCreated() )
6755 {
6756 editor->Create(m_gridWin, -1,
6757 new wxGridCellEditorEvtHandler(this, editor));
6758
6759 wxGridEditorCreatedEvent evt(GetId(),
6760 wxEVT_GRID_EDITOR_CREATED,
6761 this,
6762 row,
6763 col,
6764 editor->GetControl());
6765 GetEventHandler()->ProcessEvent(evt);
6766 }
6767
6768 editor->Show( TRUE, attr );
6769
6770 editor->SetSize( rect );
6771
6772 editor->BeginEdit(row, col, this);
6773
6774 editor->DecRef();
6775 attr->DecRef();
6776 }
6777 }
6778}
6779
6780
6781void wxGrid::HideCellEditControl()
6782{
6783 if ( IsCellEditControlEnabled() )
6784 {
6785 int row = m_currentCellCoords.GetRow();
6786 int col = m_currentCellCoords.GetCol();
6787
6788 wxGridCellAttr* attr = GetCellAttr(row, col);
6789 wxGridCellEditor *editor = attr->GetEditor(this, row, col);
6790 editor->Show( FALSE );
6791 editor->DecRef();
6792 attr->DecRef();
6793 m_gridWin->SetFocus();
6794 wxRect rect( CellToRect( row, col ) );
6795 m_gridWin->Refresh( FALSE, &rect );
6796 }
6797}
6798
6799
6800void wxGrid::SaveEditControlValue()
6801{
6802 if ( IsCellEditControlEnabled() )
6803 {
6804 int row = m_currentCellCoords.GetRow();
6805 int col = m_currentCellCoords.GetCol();
6806
6807 wxString oldval = GetCellValue(row,col);
6808
6809 wxGridCellAttr* attr = GetCellAttr(row, col);
6810 wxGridCellEditor* editor = attr->GetEditor(this, row, col);
6811 bool changed = editor->EndEdit(row, col, this);
6812
6813 editor->DecRef();
6814 attr->DecRef();
6815
6816 if (changed)
6817 {
6818 if ( SendEvent( wxEVT_GRID_CELL_CHANGE,
6819 m_currentCellCoords.GetRow(),
6820 m_currentCellCoords.GetCol() ) < 0 ) {
6821
6822 // Event has been vetoed, set the data back.
6823 SetCellValue(row,col,oldval);
6824 }
6825 }
6826 }
6827}
6828
6829
6830//
6831// ------ Grid location functions
6832// Note that all of these functions work with the logical coordinates of
6833// grid cells and labels so you will need to convert from device
6834// coordinates for mouse events etc.
6835//
6836
6837void wxGrid::XYToCell( int x, int y, wxGridCellCoords& coords )
6838{
6839 int row = YToRow(y);
6840 int col = XToCol(x);
6841
6842 if ( row == -1 || col == -1 )
6843 {
6844 coords = wxGridNoCellCoords;
6845 }
6846 else
6847 {
6848 coords.Set( row, col );
6849 }
6850}
6851
6852
6853// Internal Helper function for computing row or column from some
6854// (unscrolled) coordinate value, using either
6855// m_defaultRowHeight/m_defaultColWidth or binary search on array
6856// of m_rowBottoms/m_ColRights to speed up the search!
6857
6858static int CoordToRowOrCol(int coord, int defaultDist, int minDist,
6859 const wxArrayInt& BorderArray, bool maxOnOverflow)
6860{
6861 if (!defaultDist)
6862 defaultDist = 1;
6863 size_t i_max = coord / defaultDist,
6864 i_min = 0;
6865 if (BorderArray.IsEmpty())
6866 {
6867 return i_max;
6868 }
6869
6870 if ( i_max >= BorderArray.GetCount())
6871 i_max = BorderArray.GetCount() - 1;
6872 else
6873 {
6874 if ( coord >= BorderArray[i_max])
6875 {
6876 i_min = i_max;
6877 i_max = coord / minDist;
6878 }
6879 if ( i_max >= BorderArray.GetCount())
6880 i_max = BorderArray.GetCount() - 1;
6881 }
6882 if ( coord >= BorderArray[i_max])
6883 return maxOnOverflow ? (int)i_max : -1;
6884 if ( coord < BorderArray[0] )
6885 return 0;
6886
6887 while ( i_max - i_min > 0 )
6888 {
6889 wxCHECK_MSG(BorderArray[i_min] <= coord && coord < BorderArray[i_max],
6890 0, _T("wxGrid: internal error in CoordToRowOrCol"));
6891 if (coord >= BorderArray[ i_max - 1])
6892 return i_max;
6893 else
6894 i_max--;
6895 int median = i_min + (i_max - i_min + 1) / 2;
6896 if (coord < BorderArray[median])
6897 i_max = median;
6898 else
6899 i_min = median;
6900 }
6901 return i_max;
6902}
6903
6904int wxGrid::YToRow( int y )
6905{
6906 return CoordToRowOrCol(y, m_defaultRowHeight,
6907 WXGRID_MIN_ROW_HEIGHT, m_rowBottoms, FALSE);
6908}
6909
6910
6911int wxGrid::XToCol( int x )
6912{
6913 return CoordToRowOrCol(x, m_defaultColWidth,
6914 WXGRID_MIN_COL_WIDTH, m_colRights, FALSE);
6915}
6916
6917
6918// return the row number that that the y coord is near the edge of, or
6919// -1 if not near an edge
6920//
6921int wxGrid::YToEdgeOfRow( int y )
6922{
6923 int i;
6924 i = internalYToRow(y);
6925
6926 if ( GetRowHeight(i) > WXGRID_LABEL_EDGE_ZONE )
6927 {
6928 // We know that we are in row i, test whether we are
6929 // close enough to lower or upper border, respectively.
6930 if ( abs(GetRowBottom(i) - y) < WXGRID_LABEL_EDGE_ZONE )
6931 return i;
6932 else if( i > 0 && y - GetRowTop(i) < WXGRID_LABEL_EDGE_ZONE )
6933 return i - 1;
6934 }
6935
6936 return -1;
6937}
6938
6939
6940// return the col number that that the x coord is near the edge of, or
6941// -1 if not near an edge
6942//
6943int wxGrid::XToEdgeOfCol( int x )
6944{
6945 int i;
6946 i = internalXToCol(x);
6947
6948 if ( GetColWidth(i) > WXGRID_LABEL_EDGE_ZONE )
6949 {
6950 // We know that we are in column i, test whether we are
6951 // close enough to right or left border, respectively.
6952 if ( abs(GetColRight(i) - x) < WXGRID_LABEL_EDGE_ZONE )
6953 return i;
6954 else if( i > 0 && x - GetColLeft(i) < WXGRID_LABEL_EDGE_ZONE )
6955 return i - 1;
6956 }
6957
6958 return -1;
6959}
6960
6961
6962wxRect wxGrid::CellToRect( int row, int col )
6963{
6964 wxRect rect( -1, -1, -1, -1 );
6965
6966 if ( row >= 0 && row < m_numRows &&
6967 col >= 0 && col < m_numCols )
6968 {
6969 rect.x = GetColLeft(col);
6970 rect.y = GetRowTop(row);
6971 rect.width = GetColWidth(col);
6972 rect.height = GetRowHeight(row);
6973 }
6974
6975 // if grid lines are enabled, then the area of the cell is a bit smaller
6976 if (m_gridLinesEnabled) {
6977 rect.width -= 1;
6978 rect.height -= 1;
6979 }
6980 return rect;
6981}
6982
6983
6984bool wxGrid::IsVisible( int row, int col, bool wholeCellVisible )
6985{
6986 // get the cell rectangle in logical coords
6987 //
6988 wxRect r( CellToRect( row, col ) );
6989
6990 // convert to device coords
6991 //
6992 int left, top, right, bottom;
6993 CalcScrolledPosition( r.GetLeft(), r.GetTop(), &left, &top );
6994 CalcScrolledPosition( r.GetRight(), r.GetBottom(), &right, &bottom );
6995
6996 // check against the client area of the grid window
6997 //
6998 int cw, ch;
6999 m_gridWin->GetClientSize( &cw, &ch );
7000
7001 if ( wholeCellVisible )
7002 {
7003 // is the cell wholly visible ?
7004 //
7005 return ( left >= 0 && right <= cw &&
7006 top >= 0 && bottom <= ch );
7007 }
7008 else
7009 {
7010 // is the cell partly visible ?
7011 //
7012 return ( ((left >=0 && left < cw) || (right > 0 && right <= cw)) &&
7013 ((top >=0 && top < ch) || (bottom > 0 && bottom <= ch)) );
7014 }
7015}
7016
7017
7018// make the specified cell location visible by doing a minimal amount
7019// of scrolling
7020//
7021void wxGrid::MakeCellVisible( int row, int col )
7022{
7023
7024 int i;
7025 int xpos = -1, ypos = -1;
7026
7027 if ( row >= 0 && row < m_numRows &&
7028 col >= 0 && col < m_numCols )
7029 {
7030 // get the cell rectangle in logical coords
7031 //
7032 wxRect r( CellToRect( row, col ) );
7033
7034 // convert to device coords
7035 //
7036 int left, top, right, bottom;
7037 CalcScrolledPosition( r.GetLeft(), r.GetTop(), &left, &top );
7038 CalcScrolledPosition( r.GetRight(), r.GetBottom(), &right, &bottom );
7039
7040 int cw, ch;
7041 m_gridWin->GetClientSize( &cw, &ch );
7042
7043 if ( top < 0 )
7044 {
7045 ypos = r.GetTop();
7046 }
7047 else if ( bottom > ch )
7048 {
7049 int h = r.GetHeight();
7050 ypos = r.GetTop();
7051 for ( i = row-1; i >= 0; i-- )
7052 {
7053 int rowHeight = GetRowHeight(i);
7054 if ( h + rowHeight > ch )
7055 break;
7056
7057 h += rowHeight;
7058 ypos -= rowHeight;
7059 }
7060
7061 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
7062 // have rounding errors (this is important, because if we do, we
7063 // might not scroll at all and some cells won't be redrawn)
7064 //
7065 // Sometimes GRID_SCROLL_LINE/2 is not enough, so just add a full
7066 // scroll unit...
7067 ypos += GRID_SCROLL_LINE_Y;
7068 }
7069
7070 if ( left < 0 )
7071 {
7072 xpos = r.GetLeft();
7073 }
7074 else if ( right > cw )
7075 {
7076 int w = r.GetWidth();
7077 xpos = r.GetLeft();
7078 for ( i = col-1; i >= 0; i-- )
7079 {
7080 int colWidth = GetColWidth(i);
7081 if ( w + colWidth > cw )
7082 break;
7083
7084 w += colWidth;
7085 xpos -= colWidth;
7086 }
7087
7088 // see comment for ypos above
7089 xpos += GRID_SCROLL_LINE_X;
7090 }
7091
7092 if ( xpos != -1 || ypos != -1 )
7093 {
7094 if ( xpos != -1 )
7095 xpos /= GRID_SCROLL_LINE_X;
7096 if ( ypos != -1 )
7097 ypos /= GRID_SCROLL_LINE_Y;
7098 Scroll( xpos, ypos );
7099 AdjustScrollbars();
7100 }
7101 }
7102}
7103
7104
7105//
7106// ------ Grid cursor movement functions
7107//
7108
7109bool wxGrid::MoveCursorUp( bool expandSelection )
7110{
7111 if ( m_currentCellCoords != wxGridNoCellCoords &&
7112 m_currentCellCoords.GetRow() >= 0 )
7113 {
7114 if ( expandSelection)
7115 {
7116 if ( m_selectingKeyboard == wxGridNoCellCoords )
7117 m_selectingKeyboard = m_currentCellCoords;
7118 if ( m_selectingKeyboard.GetRow() > 0 )
7119 {
7120 m_selectingKeyboard.SetRow( m_selectingKeyboard.GetRow() - 1 );
7121 MakeCellVisible( m_selectingKeyboard.GetRow(),
7122 m_selectingKeyboard.GetCol() );
7123 HighlightBlock( m_currentCellCoords, m_selectingKeyboard );
7124 }
7125 }
7126 else if ( m_currentCellCoords.GetRow() > 0 )
7127 {
7128 ClearSelection();
7129 MakeCellVisible( m_currentCellCoords.GetRow() - 1,
7130 m_currentCellCoords.GetCol() );
7131 SetCurrentCell( m_currentCellCoords.GetRow() - 1,
7132 m_currentCellCoords.GetCol() );
7133 }
7134 else
7135 return FALSE;
7136 return TRUE;
7137 }
7138
7139 return FALSE;
7140}
7141
7142
7143bool wxGrid::MoveCursorDown( bool expandSelection )
7144{
7145 if ( m_currentCellCoords != wxGridNoCellCoords &&
7146 m_currentCellCoords.GetRow() < m_numRows )
7147 {
7148 if ( expandSelection )
7149 {
7150 if ( m_selectingKeyboard == wxGridNoCellCoords )
7151 m_selectingKeyboard = m_currentCellCoords;
7152 if ( m_selectingKeyboard.GetRow() < m_numRows-1 )
7153 {
7154 m_selectingKeyboard.SetRow( m_selectingKeyboard.GetRow() + 1 );
7155 MakeCellVisible( m_selectingKeyboard.GetRow(),
7156 m_selectingKeyboard.GetCol() );
7157 HighlightBlock( m_currentCellCoords, m_selectingKeyboard );
7158 }
7159 }
7160 else if ( m_currentCellCoords.GetRow() < m_numRows - 1 )
7161 {
7162 ClearSelection();
7163 MakeCellVisible( m_currentCellCoords.GetRow() + 1,
7164 m_currentCellCoords.GetCol() );
7165 SetCurrentCell( m_currentCellCoords.GetRow() + 1,
7166 m_currentCellCoords.GetCol() );
7167 }
7168 else
7169 return FALSE;
7170 return TRUE;
7171 }
7172
7173 return FALSE;
7174}
7175
7176
7177bool wxGrid::MoveCursorLeft( bool expandSelection )
7178{
7179 if ( m_currentCellCoords != wxGridNoCellCoords &&
7180 m_currentCellCoords.GetCol() >= 0 )
7181 {
7182 if ( expandSelection )
7183 {
7184 if ( m_selectingKeyboard == wxGridNoCellCoords )
7185 m_selectingKeyboard = m_currentCellCoords;
7186 if ( m_selectingKeyboard.GetCol() > 0 )
7187 {
7188 m_selectingKeyboard.SetCol( m_selectingKeyboard.GetCol() - 1 );
7189 MakeCellVisible( m_selectingKeyboard.GetRow(),
7190 m_selectingKeyboard.GetCol() );
7191 HighlightBlock( m_currentCellCoords, m_selectingKeyboard );
7192 }
7193 }
7194 else if ( m_currentCellCoords.GetCol() > 0 )
7195 {
7196 ClearSelection();
7197 MakeCellVisible( m_currentCellCoords.GetRow(),
7198 m_currentCellCoords.GetCol() - 1 );
7199 SetCurrentCell( m_currentCellCoords.GetRow(),
7200 m_currentCellCoords.GetCol() - 1 );
7201 }
7202 else
7203 return FALSE;
7204 return TRUE;
7205 }
7206
7207 return FALSE;
7208}
7209
7210
7211bool wxGrid::MoveCursorRight( bool expandSelection )
7212{
7213 if ( m_currentCellCoords != wxGridNoCellCoords &&
7214 m_currentCellCoords.GetCol() < m_numCols )
7215 {
7216 if ( expandSelection )
7217 {
7218 if ( m_selectingKeyboard == wxGridNoCellCoords )
7219 m_selectingKeyboard = m_currentCellCoords;
7220 if ( m_selectingKeyboard.GetCol() < m_numCols - 1 )
7221 {
7222 m_selectingKeyboard.SetCol( m_selectingKeyboard.GetCol() + 1 );
7223 MakeCellVisible( m_selectingKeyboard.GetRow(),
7224 m_selectingKeyboard.GetCol() );
7225 HighlightBlock( m_currentCellCoords, m_selectingKeyboard );
7226 }
7227 }
7228 else if ( m_currentCellCoords.GetCol() < m_numCols - 1 )
7229 {
7230 ClearSelection();
7231 MakeCellVisible( m_currentCellCoords.GetRow(),
7232 m_currentCellCoords.GetCol() + 1 );
7233 SetCurrentCell( m_currentCellCoords.GetRow(),
7234 m_currentCellCoords.GetCol() + 1 );
7235 }
7236 else
7237 return FALSE;
7238 return TRUE;
7239 }
7240
7241 return FALSE;
7242}
7243
7244
7245bool wxGrid::MovePageUp()
7246{
7247 if ( m_currentCellCoords == wxGridNoCellCoords ) return FALSE;
7248
7249 int row = m_currentCellCoords.GetRow();
7250 if ( row > 0 )
7251 {
7252 int cw, ch;
7253 m_gridWin->GetClientSize( &cw, &ch );
7254
7255 int y = GetRowTop(row);
7256 int newRow = YToRow( y - ch + 1 );
7257 if ( newRow == -1 )
7258 {
7259 newRow = 0;
7260 }
7261 else if ( newRow == row )
7262 {
7263 newRow = row - 1;
7264 }
7265
7266 MakeCellVisible( newRow, m_currentCellCoords.GetCol() );
7267 SetCurrentCell( newRow, m_currentCellCoords.GetCol() );
7268
7269 return TRUE;
7270 }
7271
7272 return FALSE;
7273}
7274
7275bool wxGrid::MovePageDown()
7276{
7277 if ( m_currentCellCoords == wxGridNoCellCoords ) return FALSE;
7278
7279 int row = m_currentCellCoords.GetRow();
7280 if ( row < m_numRows )
7281 {
7282 int cw, ch;
7283 m_gridWin->GetClientSize( &cw, &ch );
7284
7285 int y = GetRowTop(row);
7286 int newRow = YToRow( y + ch );
7287 if ( newRow == -1 )
7288 {
7289 newRow = m_numRows - 1;
7290 }
7291 else if ( newRow == row )
7292 {
7293 newRow = row + 1;
7294 }
7295
7296 MakeCellVisible( newRow, m_currentCellCoords.GetCol() );
7297 SetCurrentCell( newRow, m_currentCellCoords.GetCol() );
7298
7299 return TRUE;
7300 }
7301
7302 return FALSE;
7303}
7304
7305bool wxGrid::MoveCursorUpBlock( bool expandSelection )
7306{
7307 if ( m_table &&
7308 m_currentCellCoords != wxGridNoCellCoords &&
7309 m_currentCellCoords.GetRow() > 0 )
7310 {
7311 int row = m_currentCellCoords.GetRow();
7312 int col = m_currentCellCoords.GetCol();
7313
7314 if ( m_table->IsEmptyCell(row, col) )
7315 {
7316 // starting in an empty cell: find the next block of
7317 // non-empty cells
7318 //
7319 while ( row > 0 )
7320 {
7321 row-- ;
7322 if ( !(m_table->IsEmptyCell(row, col)) ) break;
7323 }
7324 }
7325 else if ( m_table->IsEmptyCell(row-1, col) )
7326 {
7327 // starting at the top of a block: find the next block
7328 //
7329 row--;
7330 while ( row > 0 )
7331 {
7332 row-- ;
7333 if ( !(m_table->IsEmptyCell(row, col)) ) break;
7334 }
7335 }
7336 else
7337 {
7338 // starting within a block: find the top of the block
7339 //
7340 while ( row > 0 )
7341 {
7342 row-- ;
7343 if ( m_table->IsEmptyCell(row, col) )
7344 {
7345 row++ ;
7346 break;
7347 }
7348 }
7349 }
7350
7351 MakeCellVisible( row, col );
7352 if ( expandSelection )
7353 {
7354 m_selectingKeyboard = wxGridCellCoords( row, col );
7355 HighlightBlock( m_currentCellCoords, m_selectingKeyboard );
7356 }
7357 else
7358 {
7359 ClearSelection();
7360 SetCurrentCell( row, col );
7361 }
7362 return TRUE;
7363 }
7364
7365 return FALSE;
7366}
7367
7368bool wxGrid::MoveCursorDownBlock( bool expandSelection )
7369{
7370 if ( m_table &&
7371 m_currentCellCoords != wxGridNoCellCoords &&
7372 m_currentCellCoords.GetRow() < m_numRows-1 )
7373 {
7374 int row = m_currentCellCoords.GetRow();
7375 int col = m_currentCellCoords.GetCol();
7376
7377 if ( m_table->IsEmptyCell(row, col) )
7378 {
7379 // starting in an empty cell: find the next block of
7380 // non-empty cells
7381 //
7382 while ( row < m_numRows-1 )
7383 {
7384 row++ ;
7385 if ( !(m_table->IsEmptyCell(row, col)) ) break;
7386 }
7387 }
7388 else if ( m_table->IsEmptyCell(row+1, col) )
7389 {
7390 // starting at the bottom of a block: find the next block
7391 //
7392 row++;
7393 while ( row < m_numRows-1 )
7394 {
7395 row++ ;
7396 if ( !(m_table->IsEmptyCell(row, col)) ) break;
7397 }
7398 }
7399 else
7400 {
7401 // starting within a block: find the bottom of the block
7402 //
7403 while ( row < m_numRows-1 )
7404 {
7405 row++ ;
7406 if ( m_table->IsEmptyCell(row, col) )
7407 {
7408 row-- ;
7409 break;
7410 }
7411 }
7412 }
7413
7414 MakeCellVisible( row, col );
7415 if ( expandSelection )
7416 {
7417 m_selectingKeyboard = wxGridCellCoords( row, col );
7418 HighlightBlock( m_currentCellCoords, m_selectingKeyboard );
7419 }
7420 else
7421 {
7422 ClearSelection();
7423 SetCurrentCell( row, col );
7424 }
7425
7426 return TRUE;
7427 }
7428
7429 return FALSE;
7430}
7431
7432bool wxGrid::MoveCursorLeftBlock( bool expandSelection )
7433{
7434 if ( m_table &&
7435 m_currentCellCoords != wxGridNoCellCoords &&
7436 m_currentCellCoords.GetCol() > 0 )
7437 {
7438 int row = m_currentCellCoords.GetRow();
7439 int col = m_currentCellCoords.GetCol();
7440
7441 if ( m_table->IsEmptyCell(row, col) )
7442 {
7443 // starting in an empty cell: find the next block of
7444 // non-empty cells
7445 //
7446 while ( col > 0 )
7447 {
7448 col-- ;
7449 if ( !(m_table->IsEmptyCell(row, col)) ) break;
7450 }
7451 }
7452 else if ( m_table->IsEmptyCell(row, col-1) )
7453 {
7454 // starting at the left of a block: find the next block
7455 //
7456 col--;
7457 while ( col > 0 )
7458 {
7459 col-- ;
7460 if ( !(m_table->IsEmptyCell(row, col)) ) break;
7461 }
7462 }
7463 else
7464 {
7465 // starting within a block: find the left of the block
7466 //
7467 while ( col > 0 )
7468 {
7469 col-- ;
7470 if ( m_table->IsEmptyCell(row, col) )
7471 {
7472 col++ ;
7473 break;
7474 }
7475 }
7476 }
7477
7478 MakeCellVisible( row, col );
7479 if ( expandSelection )
7480 {
7481 m_selectingKeyboard = wxGridCellCoords( row, col );
7482 HighlightBlock( m_currentCellCoords, m_selectingKeyboard );
7483 }
7484 else
7485 {
7486 ClearSelection();
7487 SetCurrentCell( row, col );
7488 }
7489
7490 return TRUE;
7491 }
7492
7493 return FALSE;
7494}
7495
7496bool wxGrid::MoveCursorRightBlock( bool expandSelection )
7497{
7498 if ( m_table &&
7499 m_currentCellCoords != wxGridNoCellCoords &&
7500 m_currentCellCoords.GetCol() < m_numCols-1 )
7501 {
7502 int row = m_currentCellCoords.GetRow();
7503 int col = m_currentCellCoords.GetCol();
7504
7505 if ( m_table->IsEmptyCell(row, col) )
7506 {
7507 // starting in an empty cell: find the next block of
7508 // non-empty cells
7509 //
7510 while ( col < m_numCols-1 )
7511 {
7512 col++ ;
7513 if ( !(m_table->IsEmptyCell(row, col)) ) break;
7514 }
7515 }
7516 else if ( m_table->IsEmptyCell(row, col+1) )
7517 {
7518 // starting at the right of a block: find the next block
7519 //
7520 col++;
7521 while ( col < m_numCols-1 )
7522 {
7523 col++ ;
7524 if ( !(m_table->IsEmptyCell(row, col)) ) break;
7525 }
7526 }
7527 else
7528 {
7529 // starting within a block: find the right of the block
7530 //
7531 while ( col < m_numCols-1 )
7532 {
7533 col++ ;
7534 if ( m_table->IsEmptyCell(row, col) )
7535 {
7536 col-- ;
7537 break;
7538 }
7539 }
7540 }
7541
7542 MakeCellVisible( row, col );
7543 if ( expandSelection )
7544 {
7545 m_selectingKeyboard = wxGridCellCoords( row, col );
7546 HighlightBlock( m_currentCellCoords, m_selectingKeyboard );
7547 }
7548 else
7549 {
7550 ClearSelection();
7551 SetCurrentCell( row, col );
7552 }
7553
7554 return TRUE;
7555 }
7556
7557 return FALSE;
7558}
7559
7560
7561
7562//
7563// ------ Label values and formatting
7564//
7565
7566void wxGrid::GetRowLabelAlignment( int *horiz, int *vert )
7567{
7568 *horiz = m_rowLabelHorizAlign;
7569 *vert = m_rowLabelVertAlign;
7570}
7571
7572void wxGrid::GetColLabelAlignment( int *horiz, int *vert )
7573{
7574 *horiz = m_colLabelHorizAlign;
7575 *vert = m_colLabelVertAlign;
7576}
7577
7578wxString wxGrid::GetRowLabelValue( int row )
7579{
7580 if ( m_table )
7581 {
7582 return m_table->GetRowLabelValue( row );
7583 }
7584 else
7585 {
7586 wxString s;
7587 s << row;
7588 return s;
7589 }
7590}
7591
7592wxString wxGrid::GetColLabelValue( int col )
7593{
7594 if ( m_table )
7595 {
7596 return m_table->GetColLabelValue( col );
7597 }
7598 else
7599 {
7600 wxString s;
7601 s << col;
7602 return s;
7603 }
7604}
7605
7606
7607void wxGrid::SetRowLabelSize( int width )
7608{
7609 width = wxMax( width, 0 );
7610 if ( width != m_rowLabelWidth )
7611 {
7612 if ( width == 0 )
7613 {
7614 m_rowLabelWin->Show( FALSE );
7615 m_cornerLabelWin->Show( FALSE );
7616 }
7617 else if ( m_rowLabelWidth == 0 )
7618 {
7619 m_rowLabelWin->Show( TRUE );
7620 if ( m_colLabelHeight > 0 ) m_cornerLabelWin->Show( TRUE );
7621 }
7622
7623 m_rowLabelWidth = width;
7624 CalcWindowSizes();
7625 Refresh( TRUE );
7626 }
7627}
7628
7629
7630void wxGrid::SetColLabelSize( int height )
7631{
7632 height = wxMax( height, 0 );
7633 if ( height != m_colLabelHeight )
7634 {
7635 if ( height == 0 )
7636 {
7637 m_colLabelWin->Show( FALSE );
7638 m_cornerLabelWin->Show( FALSE );
7639 }
7640 else if ( m_colLabelHeight == 0 )
7641 {
7642 m_colLabelWin->Show( TRUE );
7643 if ( m_rowLabelWidth > 0 ) m_cornerLabelWin->Show( TRUE );
7644 }
7645
7646 m_colLabelHeight = height;
7647 CalcWindowSizes();
7648 Refresh( TRUE );
7649 }
7650}
7651
7652
7653void wxGrid::SetLabelBackgroundColour( const wxColour& colour )
7654{
7655 if ( m_labelBackgroundColour != colour )
7656 {
7657 m_labelBackgroundColour = colour;
7658 m_rowLabelWin->SetBackgroundColour( colour );
7659 m_colLabelWin->SetBackgroundColour( colour );
7660 m_cornerLabelWin->SetBackgroundColour( colour );
7661
7662 if ( !GetBatchCount() )
7663 {
7664 m_rowLabelWin->Refresh();
7665 m_colLabelWin->Refresh();
7666 m_cornerLabelWin->Refresh();
7667 }
7668 }
7669}
7670
7671void wxGrid::SetLabelTextColour( const wxColour& colour )
7672{
7673 if ( m_labelTextColour != colour )
7674 {
7675 m_labelTextColour = colour;
7676 if ( !GetBatchCount() )
7677 {
7678 m_rowLabelWin->Refresh();
7679 m_colLabelWin->Refresh();
7680 }
7681 }
7682}
7683
7684void wxGrid::SetLabelFont( const wxFont& font )
7685{
7686 m_labelFont = font;
7687 if ( !GetBatchCount() )
7688 {
7689 m_rowLabelWin->Refresh();
7690 m_colLabelWin->Refresh();
7691 }
7692}
7693
7694void wxGrid::SetRowLabelAlignment( int horiz, int vert )
7695{
7696 // allow old (incorrect) defs to be used
7697 switch ( horiz )
7698 {
7699 case wxLEFT: horiz = wxALIGN_LEFT; break;
7700 case wxRIGHT: horiz = wxALIGN_RIGHT; break;
7701 case wxCENTRE: horiz = wxALIGN_CENTRE; break;
7702 }
7703
7704 switch ( vert )
7705 {
7706 case wxTOP: vert = wxALIGN_TOP; break;
7707 case wxBOTTOM: vert = wxALIGN_BOTTOM; break;
7708 case wxCENTRE: vert = wxALIGN_CENTRE; break;
7709 }
7710
7711 if ( horiz == wxALIGN_LEFT || horiz == wxALIGN_CENTRE || horiz == wxALIGN_RIGHT )
7712 {
7713 m_rowLabelHorizAlign = horiz;
7714 }
7715
7716 if ( vert == wxALIGN_TOP || vert == wxALIGN_CENTRE || vert == wxALIGN_BOTTOM )
7717 {
7718 m_rowLabelVertAlign = vert;
7719 }
7720
7721 if ( !GetBatchCount() )
7722 {
7723 m_rowLabelWin->Refresh();
7724 }
7725}
7726
7727void wxGrid::SetColLabelAlignment( int horiz, int vert )
7728{
7729 // allow old (incorrect) defs to be used
7730 switch ( horiz )
7731 {
7732 case wxLEFT: horiz = wxALIGN_LEFT; break;
7733 case wxRIGHT: horiz = wxALIGN_RIGHT; break;
7734 case wxCENTRE: horiz = wxALIGN_CENTRE; break;
7735 }
7736
7737 switch ( vert )
7738 {
7739 case wxTOP: vert = wxALIGN_TOP; break;
7740 case wxBOTTOM: vert = wxALIGN_BOTTOM; break;
7741 case wxCENTRE: vert = wxALIGN_CENTRE; break;
7742 }
7743
7744 if ( horiz == wxALIGN_LEFT || horiz == wxALIGN_CENTRE || horiz == wxALIGN_RIGHT )
7745 {
7746 m_colLabelHorizAlign = horiz;
7747 }
7748
7749 if ( vert == wxALIGN_TOP || vert == wxALIGN_CENTRE || vert == wxALIGN_BOTTOM )
7750 {
7751 m_colLabelVertAlign = vert;
7752 }
7753
7754 if ( !GetBatchCount() )
7755 {
7756 m_colLabelWin->Refresh();
7757 }
7758}
7759
7760void wxGrid::SetRowLabelValue( int row, const wxString& s )
7761{
7762 if ( m_table )
7763 {
7764 m_table->SetRowLabelValue( row, s );
7765 if ( !GetBatchCount() )
7766 {
7767 wxRect rect = CellToRect( row, 0);
7768 if ( rect.height > 0 )
7769 {
7770 CalcScrolledPosition(0, rect.y, &rect.x, &rect.y);
7771 rect.x = 0;
7772 rect.width = m_rowLabelWidth;
7773 m_rowLabelWin->Refresh( TRUE, &rect );
7774 }
7775 }
7776 }
7777}
7778
7779void wxGrid::SetColLabelValue( int col, const wxString& s )
7780{
7781 if ( m_table )
7782 {
7783 m_table->SetColLabelValue( col, s );
7784 if ( !GetBatchCount() )
7785 {
7786 wxRect rect = CellToRect( 0, col );
7787 if ( rect.width > 0 )
7788 {
7789 CalcScrolledPosition(rect.x, 0, &rect.x, &rect.y);
7790 rect.y = 0;
7791 rect.height = m_colLabelHeight;
7792 m_colLabelWin->Refresh( TRUE, &rect );
7793 }
7794 }
7795 }
7796}
7797
7798void wxGrid::SetGridLineColour( const wxColour& colour )
7799{
7800 if ( m_gridLineColour != colour )
7801 {
7802 m_gridLineColour = colour;
7803
7804 wxClientDC dc( m_gridWin );
7805 PrepareDC( dc );
7806 DrawAllGridLines( dc, wxRegion() );
7807 }
7808}
7809
7810
7811void wxGrid::SetCellHighlightColour( const wxColour& colour )
7812{
7813 if ( m_cellHighlightColour != colour )
7814 {
7815 m_cellHighlightColour = colour;
7816
7817 wxClientDC dc( m_gridWin );
7818 PrepareDC( dc );
7819 wxGridCellAttr* attr = GetCellAttr(m_currentCellCoords);
7820 DrawCellHighlight(dc, attr);
7821 attr->DecRef();
7822 }
7823}
7824
7825void wxGrid::SetCellHighlightPenWidth(int width)
7826{
7827 if (m_cellHighlightPenWidth != width) {
7828 m_cellHighlightPenWidth = width;
7829
7830 // Just redrawing the cell highlight is not enough since that won't
7831 // make any visible change if the the thickness is getting smaller.
7832 int row = m_currentCellCoords.GetRow();
7833 int col = m_currentCellCoords.GetCol();
7834 if ( GetColWidth(col) <= 0 || GetRowHeight(row) <= 0 )
7835 return;
7836 wxRect rect = CellToRect(row, col);
7837 m_gridWin->Refresh(TRUE, &rect);
7838 }
7839}
7840
7841void wxGrid::SetCellHighlightROPenWidth(int width)
7842{
7843 if (m_cellHighlightROPenWidth != width) {
7844 m_cellHighlightROPenWidth = width;
7845
7846 // Just redrawing the cell highlight is not enough since that won't
7847 // make any visible change if the the thickness is getting smaller.
7848 int row = m_currentCellCoords.GetRow();
7849 int col = m_currentCellCoords.GetCol();
7850 if ( GetColWidth(col) <= 0 || GetRowHeight(row) <= 0 )
7851 return;
7852 wxRect rect = CellToRect(row, col);
7853 m_gridWin->Refresh(TRUE, &rect);
7854 }
7855}
7856
7857void wxGrid::EnableGridLines( bool enable )
7858{
7859 if ( enable != m_gridLinesEnabled )
7860 {
7861 m_gridLinesEnabled = enable;
7862
7863 if ( !GetBatchCount() )
7864 {
7865 if ( enable )
7866 {
7867 wxClientDC dc( m_gridWin );
7868 PrepareDC( dc );
7869 DrawAllGridLines( dc, wxRegion() );
7870 }
7871 else
7872 {
7873 m_gridWin->Refresh();
7874 }
7875 }
7876 }
7877}
7878
7879
7880int wxGrid::GetDefaultRowSize()
7881{
7882 return m_defaultRowHeight;
7883}
7884
7885int wxGrid::GetRowSize( int row )
7886{
7887 wxCHECK_MSG( row >= 0 && row < m_numRows, 0, _T("invalid row index") );
7888
7889 return GetRowHeight(row);
7890}
7891
7892int wxGrid::GetDefaultColSize()
7893{
7894 return m_defaultColWidth;
7895}
7896
7897int wxGrid::GetColSize( int col )
7898{
7899 wxCHECK_MSG( col >= 0 && col < m_numCols, 0, _T("invalid column index") );
7900
7901 return GetColWidth(col);
7902}
7903
7904// ============================================================================
7905// access to the grid attributes: each of them has a default value in the grid
7906// itself and may be overidden on a per-cell basis
7907// ============================================================================
7908
7909// ----------------------------------------------------------------------------
7910// setting default attributes
7911// ----------------------------------------------------------------------------
7912
7913void wxGrid::SetDefaultCellBackgroundColour( const wxColour& col )
7914{
7915 m_defaultCellAttr->SetBackgroundColour(col);
7916#ifdef __WXGTK__
7917 m_gridWin->SetBackgroundColour(col);
7918#endif
7919}
7920
7921void wxGrid::SetDefaultCellTextColour( const wxColour& col )
7922{
7923 m_defaultCellAttr->SetTextColour(col);
7924}
7925
7926void wxGrid::SetDefaultCellAlignment( int horiz, int vert )
7927{
7928 m_defaultCellAttr->SetAlignment(horiz, vert);
7929}
7930
7931void wxGrid::SetDefaultCellFont( const wxFont& font )
7932{
7933 m_defaultCellAttr->SetFont(font);
7934}
7935
7936void wxGrid::SetDefaultRenderer(wxGridCellRenderer *renderer)
7937{
7938 m_defaultCellAttr->SetRenderer(renderer);
7939}
7940
7941void wxGrid::SetDefaultEditor(wxGridCellEditor *editor)
7942{
7943 m_defaultCellAttr->SetEditor(editor);
7944}
7945
7946// ----------------------------------------------------------------------------
7947// access to the default attrbiutes
7948// ----------------------------------------------------------------------------
7949
7950wxColour wxGrid::GetDefaultCellBackgroundColour()
7951{
7952 return m_defaultCellAttr->GetBackgroundColour();
7953}
7954
7955wxColour wxGrid::GetDefaultCellTextColour()
7956{
7957 return m_defaultCellAttr->GetTextColour();
7958}
7959
7960wxFont wxGrid::GetDefaultCellFont()
7961{
7962 return m_defaultCellAttr->GetFont();
7963}
7964
7965void wxGrid::GetDefaultCellAlignment( int *horiz, int *vert )
7966{
7967 m_defaultCellAttr->GetAlignment(horiz, vert);
7968}
7969
7970wxGridCellRenderer *wxGrid::GetDefaultRenderer() const
7971{
7972 return m_defaultCellAttr->GetRenderer(NULL, 0, 0);
7973}
7974
7975wxGridCellEditor *wxGrid::GetDefaultEditor() const
7976{
7977 return m_defaultCellAttr->GetEditor(NULL,0,0);
7978}
7979
7980// ----------------------------------------------------------------------------
7981// access to cell attributes
7982// ----------------------------------------------------------------------------
7983
7984wxColour wxGrid::GetCellBackgroundColour(int row, int col)
7985{
7986 wxGridCellAttr *attr = GetCellAttr(row, col);
7987 wxColour colour = attr->GetBackgroundColour();
7988 attr->DecRef();
7989 return colour;
7990}
7991
7992wxColour wxGrid::GetCellTextColour( int row, int col )
7993{
7994 wxGridCellAttr *attr = GetCellAttr(row, col);
7995 wxColour colour = attr->GetTextColour();
7996 attr->DecRef();
7997 return colour;
7998}
7999
8000wxFont wxGrid::GetCellFont( int row, int col )
8001{
8002 wxGridCellAttr *attr = GetCellAttr(row, col);
8003 wxFont font = attr->GetFont();
8004 attr->DecRef();
8005 return font;
8006}
8007
8008void wxGrid::GetCellAlignment( int row, int col, int *horiz, int *vert )
8009{
8010 wxGridCellAttr *attr = GetCellAttr(row, col);
8011 attr->GetAlignment(horiz, vert);
8012 attr->DecRef();
8013}
8014
8015wxGridCellRenderer* wxGrid::GetCellRenderer(int row, int col)
8016{
8017 wxGridCellAttr* attr = GetCellAttr(row, col);
8018 wxGridCellRenderer* renderer = attr->GetRenderer(this, row, col);
8019 attr->DecRef();
8020
8021 return renderer;
8022}
8023
8024wxGridCellEditor* wxGrid::GetCellEditor(int row, int col)
8025{
8026 wxGridCellAttr* attr = GetCellAttr(row, col);
8027 wxGridCellEditor* editor = attr->GetEditor(this, row, col);
8028 attr->DecRef();
8029
8030 return editor;
8031}
8032
8033bool wxGrid::IsReadOnly(int row, int col) const
8034{
8035 wxGridCellAttr* attr = GetCellAttr(row, col);
8036 bool isReadOnly = attr->IsReadOnly();
8037 attr->DecRef();
8038 return isReadOnly;
8039}
8040
8041// ----------------------------------------------------------------------------
8042// attribute support: cache, automatic provider creation, ...
8043// ----------------------------------------------------------------------------
8044
8045bool wxGrid::CanHaveAttributes()
8046{
8047 if ( !m_table )
8048 {
8049 return FALSE;
8050 }
8051
8052 return m_table->CanHaveAttributes();
8053}
8054
8055void wxGrid::ClearAttrCache()
8056{
8057 if ( m_attrCache.row != -1 )
8058 {
8059 wxSafeDecRef(m_attrCache.attr);
8060 m_attrCache.attr = NULL;
8061 m_attrCache.row = -1;
8062 }
8063}
8064
8065void wxGrid::CacheAttr(int row, int col, wxGridCellAttr *attr) const
8066{
8067 wxGrid *self = (wxGrid *)this; // const_cast
8068
8069 self->ClearAttrCache();
8070 self->m_attrCache.row = row;
8071 self->m_attrCache.col = col;
8072 self->m_attrCache.attr = attr;
8073 wxSafeIncRef(attr);
8074}
8075
8076bool wxGrid::LookupAttr(int row, int col, wxGridCellAttr **attr) const
8077{
8078 if ( row == m_attrCache.row && col == m_attrCache.col )
8079 {
8080 *attr = m_attrCache.attr;
8081 wxSafeIncRef(m_attrCache.attr);
8082
8083#ifdef DEBUG_ATTR_CACHE
8084 gs_nAttrCacheHits++;
8085#endif
8086
8087 return TRUE;
8088 }
8089 else
8090 {
8091#ifdef DEBUG_ATTR_CACHE
8092 gs_nAttrCacheMisses++;
8093#endif
8094 return FALSE;
8095 }
8096}
8097
8098wxGridCellAttr *wxGrid::GetCellAttr(int row, int col) const
8099{
8100 wxGridCellAttr *attr;
8101 if ( !LookupAttr(row, col, &attr) )
8102 {
8103 attr = m_table ? m_table->GetAttr(row, col , wxGridCellAttr::Any) : (wxGridCellAttr *)NULL;
8104 CacheAttr(row, col, attr);
8105 }
8106 if (attr)
8107 {
8108 attr->SetDefAttr(m_defaultCellAttr);
8109 }
8110 else
8111 {
8112 attr = m_defaultCellAttr;
8113 attr->IncRef();
8114 }
8115
8116 return attr;
8117}
8118
8119wxGridCellAttr *wxGrid::GetOrCreateCellAttr(int row, int col) const
8120{
8121 wxGridCellAttr *attr = (wxGridCellAttr *)NULL;
8122
8123 wxCHECK_MSG( m_table, attr,
8124 _T("we may only be called if CanHaveAttributes() returned TRUE and then m_table should be !NULL") );
8125
8126 attr = m_table->GetAttr(row, col, wxGridCellAttr::Cell);
8127 if ( !attr )
8128 {
8129 attr = new wxGridCellAttr(m_defaultCellAttr);
8130
8131 // artificially inc the ref count to match DecRef() in caller
8132 attr->IncRef();
8133 m_table->SetAttr(attr, row, col);
8134 }
8135
8136 return attr;
8137}
8138
8139// ----------------------------------------------------------------------------
8140// setting column attributes (wrappers around SetColAttr)
8141// ----------------------------------------------------------------------------
8142
8143void wxGrid::SetColFormatBool(int col)
8144{
8145 SetColFormatCustom(col, wxGRID_VALUE_BOOL);
8146}
8147
8148void wxGrid::SetColFormatNumber(int col)
8149{
8150 SetColFormatCustom(col, wxGRID_VALUE_NUMBER);
8151}
8152
8153void wxGrid::SetColFormatFloat(int col, int width, int precision)
8154{
8155 wxString typeName = wxGRID_VALUE_FLOAT;
8156 if ( (width != -1) || (precision != -1) )
8157 {
8158 typeName << _T(':') << width << _T(',') << precision;
8159 }
8160
8161 SetColFormatCustom(col, typeName);
8162}
8163
8164void wxGrid::SetColFormatCustom(int col, const wxString& typeName)
8165{
8166 wxGridCellAttr *attr = (wxGridCellAttr *)NULL;
8167
8168 attr = m_table->GetAttr(-1, col, wxGridCellAttr::Col );
8169 if(!attr)
8170 attr = new wxGridCellAttr;
8171 wxGridCellRenderer *renderer = GetDefaultRendererForType(typeName);
8172 attr->SetRenderer(renderer);
8173
8174 SetColAttr(col, attr);
8175
8176}
8177
8178// ----------------------------------------------------------------------------
8179// setting cell attributes: this is forwarded to the table
8180// ----------------------------------------------------------------------------
8181
8182void wxGrid::SetRowAttr(int row, wxGridCellAttr *attr)
8183{
8184 if ( CanHaveAttributes() )
8185 {
8186 m_table->SetRowAttr(attr, row);
8187 ClearAttrCache();
8188 }
8189 else
8190 {
8191 wxSafeDecRef(attr);
8192 }
8193}
8194
8195void wxGrid::SetColAttr(int col, wxGridCellAttr *attr)
8196{
8197 if ( CanHaveAttributes() )
8198 {
8199 m_table->SetColAttr(attr, col);
8200 ClearAttrCache();
8201 }
8202 else
8203 {
8204 wxSafeDecRef(attr);
8205 }
8206}
8207
8208void wxGrid::SetCellBackgroundColour( int row, int col, const wxColour& colour )
8209{
8210 if ( CanHaveAttributes() )
8211 {
8212 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
8213 attr->SetBackgroundColour(colour);
8214 attr->DecRef();
8215 }
8216}
8217
8218void wxGrid::SetCellTextColour( int row, int col, const wxColour& colour )
8219{
8220 if ( CanHaveAttributes() )
8221 {
8222 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
8223 attr->SetTextColour(colour);
8224 attr->DecRef();
8225 }
8226}
8227
8228void wxGrid::SetCellFont( int row, int col, const wxFont& font )
8229{
8230 if ( CanHaveAttributes() )
8231 {
8232 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
8233 attr->SetFont(font);
8234 attr->DecRef();
8235 }
8236}
8237
8238void wxGrid::SetCellAlignment( int row, int col, int horiz, int vert )
8239{
8240 if ( CanHaveAttributes() )
8241 {
8242 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
8243 attr->SetAlignment(horiz, vert);
8244 attr->DecRef();
8245 }
8246}
8247
8248void wxGrid::SetCellRenderer(int row, int col, wxGridCellRenderer *renderer)
8249{
8250 if ( CanHaveAttributes() )
8251 {
8252 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
8253 attr->SetRenderer(renderer);
8254 attr->DecRef();
8255 }
8256}
8257
8258void wxGrid::SetCellEditor(int row, int col, wxGridCellEditor* editor)
8259{
8260 if ( CanHaveAttributes() )
8261 {
8262 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
8263 attr->SetEditor(editor);
8264 attr->DecRef();
8265 }
8266}
8267
8268void wxGrid::SetReadOnly(int row, int col, bool isReadOnly)
8269{
8270 if ( CanHaveAttributes() )
8271 {
8272 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
8273 attr->SetReadOnly(isReadOnly);
8274 attr->DecRef();
8275 }
8276}
8277
8278// ----------------------------------------------------------------------------
8279// Data type registration
8280// ----------------------------------------------------------------------------
8281
8282void wxGrid::RegisterDataType(const wxString& typeName,
8283 wxGridCellRenderer* renderer,
8284 wxGridCellEditor* editor)
8285{
8286 m_typeRegistry->RegisterDataType(typeName, renderer, editor);
8287}
8288
8289
8290wxGridCellEditor* wxGrid::GetDefaultEditorForCell(int row, int col) const
8291{
8292 wxString typeName = m_table->GetTypeName(row, col);
8293 return GetDefaultEditorForType(typeName);
8294}
8295
8296wxGridCellRenderer* wxGrid::GetDefaultRendererForCell(int row, int col) const
8297{
8298 wxString typeName = m_table->GetTypeName(row, col);
8299 return GetDefaultRendererForType(typeName);
8300}
8301
8302wxGridCellEditor*
8303wxGrid::GetDefaultEditorForType(const wxString& typeName) const
8304{
8305 int index = m_typeRegistry->FindOrCloneDataType(typeName);
8306 if ( index == wxNOT_FOUND )
8307 {
8308 wxFAIL_MSG(wxT("Unknown data type name"));
8309
8310 return NULL;
8311 }
8312
8313 return m_typeRegistry->GetEditor(index);
8314}
8315
8316wxGridCellRenderer*
8317wxGrid::GetDefaultRendererForType(const wxString& typeName) const
8318{
8319 int index = m_typeRegistry->FindOrCloneDataType(typeName);
8320 if ( index == wxNOT_FOUND )
8321 {
8322 wxFAIL_MSG(wxT("Unknown data type name"));
8323
8324 return NULL;
8325 }
8326
8327 return m_typeRegistry->GetRenderer(index);
8328}
8329
8330
8331// ----------------------------------------------------------------------------
8332// row/col size
8333// ----------------------------------------------------------------------------
8334
8335void wxGrid::EnableDragRowSize( bool enable )
8336{
8337 m_canDragRowSize = enable;
8338}
8339
8340
8341void wxGrid::EnableDragColSize( bool enable )
8342{
8343 m_canDragColSize = enable;
8344}
8345
8346void wxGrid::EnableDragGridSize( bool enable )
8347{
8348 m_canDragGridSize = enable;
8349}
8350
8351
8352void wxGrid::SetDefaultRowSize( int height, bool resizeExistingRows )
8353{
8354 m_defaultRowHeight = wxMax( height, WXGRID_MIN_ROW_HEIGHT );
8355
8356 if ( resizeExistingRows )
8357 {
8358 // since we are resizing all rows to the default row size,
8359 // we can simply clear the row heights and row bottoms
8360 // arrays (which also allows us to take advantage of
8361 // some speed optimisations)
8362 m_rowHeights.Empty();
8363 m_rowBottoms.Empty();
8364 if ( !GetBatchCount() )
8365 CalcDimensions();
8366 }
8367}
8368
8369void wxGrid::SetRowSize( int row, int height )
8370{
8371 wxCHECK_RET( row >= 0 && row < m_numRows, _T("invalid row index") );
8372
8373 if ( m_rowHeights.IsEmpty() )
8374 {
8375 // need to really create the array
8376 InitRowHeights();
8377 }
8378
8379 int h = wxMax( 0, height );
8380 int diff = h - m_rowHeights[row];
8381
8382 m_rowHeights[row] = h;
8383 int i;
8384 for ( i = row; i < m_numRows; i++ )
8385 {
8386 m_rowBottoms[i] += diff;
8387 }
8388 if ( !GetBatchCount() )
8389 CalcDimensions();
8390}
8391
8392void wxGrid::SetDefaultColSize( int width, bool resizeExistingCols )
8393{
8394 m_defaultColWidth = wxMax( width, WXGRID_MIN_COL_WIDTH );
8395
8396 if ( resizeExistingCols )
8397 {
8398 // since we are resizing all columns to the default column size,
8399 // we can simply clear the col widths and col rights
8400 // arrays (which also allows us to take advantage of
8401 // some speed optimisations)
8402 m_colWidths.Empty();
8403 m_colRights.Empty();
8404 if ( !GetBatchCount() )
8405 CalcDimensions();
8406 }
8407}
8408
8409void wxGrid::SetColSize( int col, int width )
8410{
8411 wxCHECK_RET( col >= 0 && col < m_numCols, _T("invalid column index") );
8412
8413 // should we check that it's bigger than GetColMinimalWidth(col) here?
8414
8415 if ( m_colWidths.IsEmpty() )
8416 {
8417 // need to really create the array
8418 InitColWidths();
8419 }
8420
8421 int w = wxMax( 0, width );
8422 int diff = w - m_colWidths[col];
8423 m_colWidths[col] = w;
8424
8425 int i;
8426 for ( i = col; i < m_numCols; i++ )
8427 {
8428 m_colRights[i] += diff;
8429 }
8430 if ( !GetBatchCount() )
8431 CalcDimensions();
8432}
8433
8434
8435void wxGrid::SetColMinimalWidth( int col, int width )
8436{
8437 m_colMinWidths.Put(col, width);
8438}
8439
8440void wxGrid::SetRowMinimalHeight( int row, int width )
8441{
8442 m_rowMinHeights.Put(row, width);
8443}
8444
8445int wxGrid::GetColMinimalWidth(int col) const
8446{
8447 long value = m_colMinWidths.Get(col);
8448 return value != wxNOT_FOUND ? (int)value : WXGRID_MIN_COL_WIDTH;
8449}
8450
8451int wxGrid::GetRowMinimalHeight(int row) const
8452{
8453 long value = m_rowMinHeights.Get(row);
8454 return value != wxNOT_FOUND ? (int)value : WXGRID_MIN_ROW_HEIGHT;
8455}
8456
8457// ----------------------------------------------------------------------------
8458// auto sizing
8459// ----------------------------------------------------------------------------
8460
8461void wxGrid::AutoSizeColOrRow( int colOrRow, bool setAsMin, bool column )
8462{
8463 wxClientDC dc(m_gridWin);
8464
8465 // init both of them to avoid compiler warnings, even if weo nly need one
8466 int row = -1,
8467 col = -1;
8468 if ( column )
8469 col = colOrRow;
8470 else
8471 row = colOrRow;
8472
8473 wxCoord extent, extentMax = 0;
8474 int max = column ? m_numRows : m_numCols;
8475 for ( int rowOrCol = 0; rowOrCol < max; rowOrCol++ )
8476 {
8477 if ( column )
8478 row = rowOrCol;
8479 else
8480 col = rowOrCol;
8481
8482 wxGridCellAttr* attr = GetCellAttr(row, col);
8483 wxGridCellRenderer* renderer = attr->GetRenderer(this, row, col);
8484 if ( renderer )
8485 {
8486 wxSize size = renderer->GetBestSize(*this, *attr, dc, row, col);
8487 extent = column ? size.x : size.y;
8488 if ( extent > extentMax )
8489 {
8490 extentMax = extent;
8491 }
8492
8493 renderer->DecRef();
8494 }
8495
8496 attr->DecRef();
8497 }
8498
8499 // now also compare with the column label extent
8500 wxCoord w, h;
8501 dc.SetFont( GetLabelFont() );
8502
8503 if ( column )
8504 dc.GetTextExtent( GetColLabelValue(col), &w, &h );
8505 else
8506 dc.GetTextExtent( GetRowLabelValue(row), &w, &h );
8507
8508 extent = column ? w : h;
8509 if ( extent > extentMax )
8510 {
8511 extentMax = extent;
8512 }
8513
8514 if ( !extentMax )
8515 {
8516 // empty column - give default extent (notice that if extentMax is less
8517 // than default extent but != 0, it's ok)
8518 extentMax = column ? m_defaultColWidth : m_defaultRowHeight;
8519 }
8520 else
8521 {
8522 if ( column )
8523 {
8524 // leave some space around text
8525 extentMax += 10;
8526 }
8527 else
8528 {
8529 extentMax += 6;
8530 }
8531 }
8532
8533 if ( column )
8534 {
8535 SetColSize(col, extentMax);
8536 if ( !GetBatchCount() )
8537 {
8538 int cw, ch, dummy;
8539 m_gridWin->GetClientSize( &cw, &ch );
8540 wxRect rect ( CellToRect( 0, col ) );
8541 rect.y = 0;
8542 CalcScrolledPosition(rect.x, 0, &rect.x, &dummy);
8543 rect.width = cw - rect.x;
8544 rect.height = m_colLabelHeight;
8545 m_colLabelWin->Refresh( TRUE, &rect );
8546 }
8547 }
8548 else
8549 {
8550 SetRowSize(row, extentMax);
8551 if ( !GetBatchCount() )
8552 {
8553 int cw, ch, dummy;
8554 m_gridWin->GetClientSize( &cw, &ch );
8555 wxRect rect ( CellToRect( row, 0 ) );
8556 rect.x = 0;
8557 CalcScrolledPosition(0, rect.y, &dummy, &rect.y);
8558 rect.width = m_rowLabelWidth;
8559 rect.height = ch - rect.y;
8560 m_rowLabelWin->Refresh( TRUE, &rect );
8561 }
8562 }
8563 if ( setAsMin )
8564 {
8565 if ( column )
8566 SetColMinimalWidth(col, extentMax);
8567 else
8568 SetRowMinimalHeight(row, extentMax);
8569 }
8570}
8571
8572int wxGrid::SetOrCalcColumnSizes(bool calcOnly, bool setAsMin)
8573{
8574 int width = m_rowLabelWidth;
8575
8576 if ( !calcOnly )
8577 BeginBatch();
8578
8579 for ( int col = 0; col < m_numCols; col++ )
8580 {
8581 if ( !calcOnly )
8582 {
8583 AutoSizeColumn(col, setAsMin);
8584 }
8585
8586 width += GetColWidth(col);
8587 }
8588
8589 if ( !calcOnly )
8590 EndBatch();
8591
8592 return width;
8593}
8594
8595int wxGrid::SetOrCalcRowSizes(bool calcOnly, bool setAsMin)
8596{
8597 int height = m_colLabelHeight;
8598
8599 if ( !calcOnly )
8600 BeginBatch();
8601
8602 for ( int row = 0; row < m_numRows; row++ )
8603 {
8604 if ( !calcOnly )
8605 {
8606 AutoSizeRow(row, setAsMin);
8607 }
8608
8609 height += GetRowHeight(row);
8610 }
8611
8612 if ( !calcOnly )
8613 EndBatch();
8614
8615 return height;
8616}
8617
8618void wxGrid::AutoSize()
8619{
8620 BeginBatch();
8621
8622 wxSize size(SetOrCalcColumnSizes(FALSE), SetOrCalcRowSizes(FALSE));
8623
8624 // round up the size to a multiple of scroll step - this ensures that we
8625 // won't get the scrollbars if we're sized exactly to this width
8626 wxSize sizeFit(GetScrollX(size.x) * GRID_SCROLL_LINE_X,
8627 GetScrollY(size.y) * GRID_SCROLL_LINE_Y);
8628
8629 // distribute the extra space between teh columns/rows to avoid having
8630 // extra white space
8631 wxCoord diff = sizeFit.x - size.x;
8632 if ( diff )
8633 {
8634 // try to resize the columns uniformly
8635 wxCoord diffPerCol = diff / m_numCols;
8636 if ( diffPerCol )
8637 {
8638 for ( int col = 0; col < m_numCols; col++ )
8639 {
8640 SetColSize(col, GetColWidth(col) + diffPerCol);
8641 }
8642 }
8643
8644 // add remaining amount to the last columns
8645 diff -= diffPerCol * m_numCols;
8646 if ( diff )
8647 {
8648 for ( int col = m_numCols - 1; col >= m_numCols - diff; col-- )
8649 {
8650 SetColSize(col, GetColWidth(col) + 1);
8651 }
8652 }
8653 }
8654
8655 // same for rows
8656 diff = sizeFit.y - size.y;
8657 if ( diff )
8658 {
8659 // try to resize the columns uniformly
8660 wxCoord diffPerRow = diff / m_numRows;
8661 if ( diffPerRow )
8662 {
8663 for ( int row = 0; row < m_numRows; row++ )
8664 {
8665 SetRowSize(row, GetRowHeight(row) + diffPerRow);
8666 }
8667 }
8668
8669 // add remaining amount to the last rows
8670 diff -= diffPerRow * m_numRows;
8671 if ( diff )
8672 {
8673 for ( int row = m_numRows - 1; row >= m_numRows - diff; row-- )
8674 {
8675 SetRowSize(row, GetRowHeight(row) + 1);
8676 }
8677 }
8678 }
8679
8680 EndBatch();
8681
8682 SetClientSize(sizeFit);
8683}
8684
8685wxSize wxGrid::DoGetBestSize() const
8686{
8687 // don't set sizes, only calculate them
8688 wxGrid *self = (wxGrid *)this; // const_cast
8689
8690 return wxSize(self->SetOrCalcColumnSizes(TRUE),
8691 self->SetOrCalcRowSizes(TRUE));
8692}
8693
8694void wxGrid::Fit()
8695{
8696 AutoSize();
8697}
8698
8699
8700wxPen& wxGrid::GetDividerPen() const
8701{
8702 return wxNullPen;
8703}
8704
8705// ----------------------------------------------------------------------------
8706// cell value accessor functions
8707// ----------------------------------------------------------------------------
8708
8709void wxGrid::SetCellValue( int row, int col, const wxString& s )
8710{
8711 if ( m_table )
8712 {
8713 m_table->SetValue( row, col, s );
8714 if ( !GetBatchCount() )
8715 {
8716 wxClientDC dc( m_gridWin );
8717 PrepareDC( dc );
8718 DrawCell( dc, wxGridCellCoords(row, col) );
8719 }
8720
8721 if ( m_currentCellCoords.GetRow() == row &&
8722 m_currentCellCoords.GetCol() == col &&
8723 IsCellEditControlShown())
8724 // Note: If we are using IsCellEditControlEnabled,
8725 // this interacts badly with calling SetCellValue from
8726 // an EVT_GRID_CELL_CHANGE handler.
8727 {
8728 HideCellEditControl();
8729 ShowCellEditControl(); // will reread data from table
8730 }
8731 }
8732}
8733
8734
8735//
8736// ------ Block, row and col selection
8737//
8738
8739void wxGrid::SelectRow( int row, bool addToSelected )
8740{
8741 if ( IsSelection() && !addToSelected )
8742 ClearSelection();
8743
8744 m_selection->SelectRow( row, FALSE, addToSelected );
8745}
8746
8747
8748void wxGrid::SelectCol( int col, bool addToSelected )
8749{
8750 if ( IsSelection() && !addToSelected )
8751 ClearSelection();
8752
8753 m_selection->SelectCol( col, FALSE, addToSelected );
8754}
8755
8756
8757void wxGrid::SelectBlock( int topRow, int leftCol, int bottomRow, int rightCol,
8758 bool addToSelected )
8759{
8760 if ( IsSelection() && !addToSelected )
8761 ClearSelection();
8762
8763 m_selection->SelectBlock( topRow, leftCol, bottomRow, rightCol,
8764 FALSE, addToSelected );
8765}
8766
8767
8768void wxGrid::SelectAll()
8769{
8770 if ( m_numRows > 0 && m_numCols > 0 )
8771 m_selection->SelectBlock( 0, 0, m_numRows-1, m_numCols-1 );
8772}
8773
8774//
8775// ------ Cell, row and col deselection
8776//
8777
8778void wxGrid::DeselectRow( int row )
8779{
8780 if ( m_selection->GetSelectionMode() == wxGrid::wxGridSelectRows )
8781 {
8782 if ( m_selection->IsInSelection(row, 0 ) )
8783 m_selection->ToggleCellSelection( row, 0);
8784 }
8785 else
8786 {
8787 int nCols = GetNumberCols();
8788 for ( int i = 0; i < nCols ; i++ )
8789 {
8790 if ( m_selection->IsInSelection(row, i ) )
8791 m_selection->ToggleCellSelection( row, i);
8792 }
8793 }
8794}
8795
8796void wxGrid::DeselectCol( int col )
8797{
8798 if ( m_selection->GetSelectionMode() == wxGrid::wxGridSelectColumns )
8799 {
8800 if ( m_selection->IsInSelection(0, col ) )
8801 m_selection->ToggleCellSelection( 0, col);
8802 }
8803 else
8804 {
8805 int nRows = GetNumberRows();
8806 for ( int i = 0; i < nRows ; i++ )
8807 {
8808 if ( m_selection->IsInSelection(i, col ) )
8809 m_selection->ToggleCellSelection(i, col);
8810 }
8811 }
8812}
8813
8814void wxGrid::DeselectCell( int row, int col )
8815{
8816 if ( m_selection->IsInSelection(row, col) )
8817 m_selection->ToggleCellSelection(row, col);
8818}
8819
8820bool wxGrid::IsSelection()
8821{
8822 return ( m_selection->IsSelection() ||
8823 ( m_selectingTopLeft != wxGridNoCellCoords &&
8824 m_selectingBottomRight != wxGridNoCellCoords ) );
8825}
8826
8827bool wxGrid::IsInSelection( int row, int col )
8828{
8829 return ( m_selection->IsInSelection( row, col ) ||
8830 ( row >= m_selectingTopLeft.GetRow() &&
8831 col >= m_selectingTopLeft.GetCol() &&
8832 row <= m_selectingBottomRight.GetRow() &&
8833 col <= m_selectingBottomRight.GetCol() ) );
8834}
8835
8836void wxGrid::ClearSelection()
8837{
8838 m_selectingTopLeft = wxGridNoCellCoords;
8839 m_selectingBottomRight = wxGridNoCellCoords;
8840 m_selection->ClearSelection();
8841}
8842
8843
8844// This function returns the rectangle that encloses the given block
8845// in device coords clipped to the client size of the grid window.
8846//
8847wxRect wxGrid::BlockToDeviceRect( const wxGridCellCoords &topLeft,
8848 const wxGridCellCoords &bottomRight )
8849{
8850 wxRect rect( wxGridNoCellRect );
8851 wxRect cellRect;
8852
8853 cellRect = CellToRect( topLeft );
8854 if ( cellRect != wxGridNoCellRect )
8855 {
8856 rect = cellRect;
8857 }
8858 else
8859 {
8860 rect = wxRect( 0, 0, 0, 0 );
8861 }
8862
8863 cellRect = CellToRect( bottomRight );
8864 if ( cellRect != wxGridNoCellRect )
8865 {
8866 rect += cellRect;
8867 }
8868 else
8869 {
8870 return wxGridNoCellRect;
8871 }
8872
8873 // convert to scrolled coords
8874 //
8875 int left, top, right, bottom;
8876 CalcScrolledPosition( rect.GetLeft(), rect.GetTop(), &left, &top );
8877 CalcScrolledPosition( rect.GetRight(), rect.GetBottom(), &right, &bottom );
8878
8879 int cw, ch;
8880 m_gridWin->GetClientSize( &cw, &ch );
8881
8882 if (right < 0 || bottom < 0 || left > cw || top > ch)
8883 return wxRect( 0, 0, 0, 0);
8884
8885 rect.SetLeft( wxMax(0, left) );
8886 rect.SetTop( wxMax(0, top) );
8887 rect.SetRight( wxMin(cw, right) );
8888 rect.SetBottom( wxMin(ch, bottom) );
8889
8890 return rect;
8891}
8892
8893
8894
8895//
8896// ------ Grid event classes
8897//
8898
8899IMPLEMENT_DYNAMIC_CLASS( wxGridEvent, wxNotifyEvent )
8900
8901wxGridEvent::wxGridEvent( int id, wxEventType type, wxObject* obj,
8902 int row, int col, int x, int y, bool sel,
8903 bool control, bool shift, bool alt, bool meta )
8904 : wxNotifyEvent( type, id )
8905{
8906 m_row = row;
8907 m_col = col;
8908 m_x = x;
8909 m_y = y;
8910 m_selecting = sel;
8911 m_control = control;
8912 m_shift = shift;
8913 m_alt = alt;
8914 m_meta = meta;
8915
8916 SetEventObject(obj);
8917}
8918
8919
8920IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent, wxNotifyEvent )
8921
8922wxGridSizeEvent::wxGridSizeEvent( int id, wxEventType type, wxObject* obj,
8923 int rowOrCol, int x, int y,
8924 bool control, bool shift, bool alt, bool meta )
8925 : wxNotifyEvent( type, id )
8926{
8927 m_rowOrCol = rowOrCol;
8928 m_x = x;
8929 m_y = y;
8930 m_control = control;
8931 m_shift = shift;
8932 m_alt = alt;
8933 m_meta = meta;
8934
8935 SetEventObject(obj);
8936}
8937
8938
8939IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent, wxNotifyEvent )
8940
8941wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id, wxEventType type, wxObject* obj,
8942 const wxGridCellCoords& topLeft,
8943 const wxGridCellCoords& bottomRight,
8944 bool sel, bool control,
8945 bool shift, bool alt, bool meta )
8946 : wxNotifyEvent( type, id )
8947{
8948 m_topLeft = topLeft;
8949 m_bottomRight = bottomRight;
8950 m_selecting = sel;
8951 m_control = control;
8952 m_shift = shift;
8953 m_alt = alt;
8954 m_meta = meta;
8955
8956 SetEventObject(obj);
8957}
8958
8959
8960IMPLEMENT_DYNAMIC_CLASS(wxGridEditorCreatedEvent, wxCommandEvent)
8961
8962wxGridEditorCreatedEvent::wxGridEditorCreatedEvent(int id, wxEventType type,
8963 wxObject* obj, int row,
8964 int col, wxControl* ctrl)
8965 : wxCommandEvent(type, id)
8966{
8967 SetEventObject(obj);
8968 m_row = row;
8969 m_col = col;
8970 m_ctrl = ctrl;
8971}
8972
8973
8974#endif // !wxUSE_NEW_GRID/wxUSE_NEW_GRID
8975
8976#endif // wxUSE_GRID
8977