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