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