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