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