]> git.saurik.com Git - wxWidgets.git/blob - src/generic/grid.cpp
implement CreateGrid() in terms of SetTable() instead of duplicating its code and...
[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 wxGridSelectionModes selmode )
4283 {
4284 wxCHECK_MSG( !m_created,
4285 false,
4286 wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
4287
4288 return SetTable(new wxGridStringTable(numRows, numCols), true, selmode);
4289 }
4290
4291 void wxGrid::SetSelectionMode(wxGridSelectionModes selmode)
4292 {
4293 wxCHECK_RET( m_created,
4294 wxT("Called wxGrid::SetSelectionMode() before calling CreateGrid()") );
4295
4296 m_selection->SetSelectionMode( selmode );
4297 }
4298
4299 wxGrid::wxGridSelectionModes wxGrid::GetSelectionMode() const
4300 {
4301 wxCHECK_MSG( m_created, wxGridSelectCells,
4302 wxT("Called wxGrid::GetSelectionMode() before calling CreateGrid()") );
4303
4304 return m_selection->GetSelectionMode();
4305 }
4306
4307 bool
4308 wxGrid::SetTable(wxGridTableBase *table,
4309 bool takeOwnership,
4310 wxGrid::wxGridSelectionModes selmode )
4311 {
4312 bool checkSelection = false;
4313 if ( m_created )
4314 {
4315 // stop all processing
4316 m_created = false;
4317
4318 if (m_table)
4319 {
4320 m_table->SetView(0);
4321 if( m_ownTable )
4322 delete m_table;
4323 m_table = NULL;
4324 }
4325
4326 delete m_selection;
4327 m_selection = NULL;
4328
4329 m_ownTable = false;
4330 m_numRows = 0;
4331 m_numCols = 0;
4332 checkSelection = true;
4333
4334 // kill row and column size arrays
4335 m_colWidths.Empty();
4336 m_colRights.Empty();
4337 m_rowHeights.Empty();
4338 m_rowBottoms.Empty();
4339 }
4340
4341 if (table)
4342 {
4343 m_numRows = table->GetNumberRows();
4344 m_numCols = table->GetNumberCols();
4345
4346 m_table = table;
4347 m_table->SetView( this );
4348 m_ownTable = takeOwnership;
4349 m_selection = new wxGridSelection( this, selmode );
4350 if (checkSelection)
4351 {
4352 // If the newly set table is smaller than the
4353 // original one current cell and selection regions
4354 // might be invalid,
4355 m_selectingKeyboard = wxGridNoCellCoords;
4356 m_currentCellCoords =
4357 wxGridCellCoords(wxMin(m_numRows, m_currentCellCoords.GetRow()),
4358 wxMin(m_numCols, m_currentCellCoords.GetCol()));
4359 if (m_selectingTopLeft.GetRow() >= m_numRows ||
4360 m_selectingTopLeft.GetCol() >= m_numCols)
4361 {
4362 m_selectingTopLeft = wxGridNoCellCoords;
4363 m_selectingBottomRight = wxGridNoCellCoords;
4364 }
4365 else
4366 m_selectingBottomRight =
4367 wxGridCellCoords(wxMin(m_numRows,
4368 m_selectingBottomRight.GetRow()),
4369 wxMin(m_numCols,
4370 m_selectingBottomRight.GetCol()));
4371 }
4372 CalcDimensions();
4373
4374 m_created = true;
4375 }
4376
4377 return m_created;
4378 }
4379
4380 void wxGrid::InitVars()
4381 {
4382 m_created = false;
4383
4384 m_cornerLabelWin = NULL;
4385 m_rowLabelWin = NULL;
4386 m_colLabelWin = NULL;
4387 m_gridWin = NULL;
4388
4389 m_table = NULL;
4390 m_ownTable = false;
4391
4392 m_selection = NULL;
4393 m_defaultCellAttr = NULL;
4394 m_typeRegistry = NULL;
4395 m_winCapture = NULL;
4396 }
4397
4398 void wxGrid::Init()
4399 {
4400 m_rowLabelWidth = WXGRID_DEFAULT_ROW_LABEL_WIDTH;
4401 m_colLabelHeight = WXGRID_DEFAULT_COL_LABEL_HEIGHT;
4402
4403 if ( m_rowLabelWin )
4404 {
4405 m_labelBackgroundColour = m_rowLabelWin->GetBackgroundColour();
4406 }
4407 else
4408 {
4409 m_labelBackgroundColour = *wxWHITE;
4410 }
4411
4412 m_labelTextColour = *wxBLACK;
4413
4414 // init attr cache
4415 m_attrCache.row = -1;
4416 m_attrCache.col = -1;
4417 m_attrCache.attr = NULL;
4418
4419 m_labelFont = GetFont();
4420 m_labelFont.SetWeight( wxBOLD );
4421
4422 m_rowLabelHorizAlign = wxALIGN_CENTRE;
4423 m_rowLabelVertAlign = wxALIGN_CENTRE;
4424
4425 m_colLabelHorizAlign = wxALIGN_CENTRE;
4426 m_colLabelVertAlign = wxALIGN_CENTRE;
4427 m_colLabelTextOrientation = wxHORIZONTAL;
4428
4429 m_defaultColWidth = WXGRID_DEFAULT_COL_WIDTH;
4430 m_defaultRowHeight = m_gridWin->GetCharHeight();
4431
4432 m_minAcceptableColWidth = WXGRID_MIN_COL_WIDTH;
4433 m_minAcceptableRowHeight = WXGRID_MIN_ROW_HEIGHT;
4434
4435 #if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl()
4436 m_defaultRowHeight += 8;
4437 #else
4438 m_defaultRowHeight += 4;
4439 #endif
4440
4441 m_gridLineColour = wxColour( 192,192,192 );
4442 m_gridLinesEnabled = true;
4443 m_cellHighlightColour = *wxBLACK;
4444 m_cellHighlightPenWidth = 2;
4445 m_cellHighlightROPenWidth = 1;
4446
4447 m_canDragColMove = false;
4448
4449 m_cursorMode = WXGRID_CURSOR_SELECT_CELL;
4450 m_winCapture = (wxWindow *)NULL;
4451 m_canDragRowSize = true;
4452 m_canDragColSize = true;
4453 m_canDragGridSize = true;
4454 m_canDragCell = false;
4455 m_dragLastPos = -1;
4456 m_dragRowOrCol = -1;
4457 m_isDragging = false;
4458 m_startDragPos = wxDefaultPosition;
4459 m_nativeColumnLabels = false;
4460
4461 m_waitForSlowClick = false;
4462
4463 m_rowResizeCursor = wxCursor( wxCURSOR_SIZENS );
4464 m_colResizeCursor = wxCursor( wxCURSOR_SIZEWE );
4465
4466 m_currentCellCoords = wxGridNoCellCoords;
4467
4468 ClearSelection();
4469
4470 m_selectionBackground = wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT);
4471 m_selectionForeground = wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT);
4472
4473 m_editable = true; // default for whole grid
4474
4475 m_inOnKeyDown = false;
4476 m_batchCount = 0;
4477
4478 m_extraWidth =
4479 m_extraHeight = 0;
4480
4481 m_scrollLineX = GRID_SCROLL_LINE_X;
4482 m_scrollLineY = GRID_SCROLL_LINE_Y;
4483 }
4484
4485 // ----------------------------------------------------------------------------
4486 // the idea is to call these functions only when necessary because they create
4487 // quite big arrays which eat memory mostly unnecessary - in particular, if
4488 // default widths/heights are used for all rows/columns, we may not use these
4489 // arrays at all
4490 //
4491 // with some extra code, it should be possible to only store the widths/heights
4492 // different from default ones (resulting in space savings for huge grids) but
4493 // this is not done currently
4494 // ----------------------------------------------------------------------------
4495
4496 void wxGrid::InitRowHeights()
4497 {
4498 m_rowHeights.Empty();
4499 m_rowBottoms.Empty();
4500
4501 m_rowHeights.Alloc( m_numRows );
4502 m_rowBottoms.Alloc( m_numRows );
4503
4504 m_rowHeights.Add( m_defaultRowHeight, m_numRows );
4505
4506 int rowBottom = 0;
4507 for ( int i = 0; i < m_numRows; i++ )
4508 {
4509 rowBottom += m_defaultRowHeight;
4510 m_rowBottoms.Add( rowBottom );
4511 }
4512 }
4513
4514 void wxGrid::InitColWidths()
4515 {
4516 m_colWidths.Empty();
4517 m_colRights.Empty();
4518
4519 m_colWidths.Alloc( m_numCols );
4520 m_colRights.Alloc( m_numCols );
4521
4522 m_colWidths.Add( m_defaultColWidth, m_numCols );
4523
4524 int colRight = 0;
4525 for ( int i = 0; i < m_numCols; i++ )
4526 {
4527 colRight = ( GetColPos( i ) + 1 ) * m_defaultColWidth;
4528 m_colRights.Add( colRight );
4529 }
4530 }
4531
4532 int wxGrid::GetColWidth(int col) const
4533 {
4534 return m_colWidths.IsEmpty() ? m_defaultColWidth : m_colWidths[col];
4535 }
4536
4537 int wxGrid::GetColLeft(int col) const
4538 {
4539 return m_colRights.IsEmpty() ? GetColPos( col ) * m_defaultColWidth
4540 : m_colRights[col] - m_colWidths[col];
4541 }
4542
4543 int wxGrid::GetColRight(int col) const
4544 {
4545 return m_colRights.IsEmpty() ? (GetColPos( col ) + 1) * m_defaultColWidth
4546 : m_colRights[col];
4547 }
4548
4549 int wxGrid::GetRowHeight(int row) const
4550 {
4551 return m_rowHeights.IsEmpty() ? m_defaultRowHeight : m_rowHeights[row];
4552 }
4553
4554 int wxGrid::GetRowTop(int row) const
4555 {
4556 return m_rowBottoms.IsEmpty() ? row * m_defaultRowHeight
4557 : m_rowBottoms[row] - m_rowHeights[row];
4558 }
4559
4560 int wxGrid::GetRowBottom(int row) const
4561 {
4562 return m_rowBottoms.IsEmpty() ? (row + 1) * m_defaultRowHeight
4563 : m_rowBottoms[row];
4564 }
4565
4566 void wxGrid::CalcDimensions()
4567 {
4568 // compute the size of the scrollable area
4569 int w = m_numCols > 0 ? GetColRight(GetColAt(m_numCols - 1)) : 0;
4570 int h = m_numRows > 0 ? GetRowBottom(m_numRows - 1) : 0;
4571
4572 w += m_extraWidth;
4573 h += m_extraHeight;
4574
4575 // take into account editor if shown
4576 if ( IsCellEditControlShown() )
4577 {
4578 int w2, h2;
4579 int r = m_currentCellCoords.GetRow();
4580 int c = m_currentCellCoords.GetCol();
4581 int x = GetColLeft(c);
4582 int y = GetRowTop(r);
4583
4584 // how big is the editor
4585 wxGridCellAttr* attr = GetCellAttr(r, c);
4586 wxGridCellEditor* editor = attr->GetEditor(this, r, c);
4587 editor->GetControl()->GetSize(&w2, &h2);
4588 w2 += x;
4589 h2 += y;
4590 if ( w2 > w )
4591 w = w2;
4592 if ( h2 > h )
4593 h = h2;
4594 editor->DecRef();
4595 attr->DecRef();
4596 }
4597
4598 // preserve (more or less) the previous position
4599 int x, y;
4600 GetViewStart( &x, &y );
4601
4602 // ensure the position is valid for the new scroll ranges
4603 if ( x >= w )
4604 x = wxMax( w - 1, 0 );
4605 if ( y >= h )
4606 y = wxMax( h - 1, 0 );
4607
4608 // update the virtual size and refresh the scrollbars to reflect it
4609 m_gridWin->SetVirtualSize(w, h);
4610 Scroll(x, y);
4611 AdjustScrollbars();
4612
4613 // if our OnSize() hadn't been called (it would if we have scrollbars), we
4614 // still must reposition the children
4615 CalcWindowSizes();
4616 }
4617
4618 wxSize wxGrid::GetSizeAvailableForScrollTarget(const wxSize& size)
4619 {
4620 wxSize sizeGridWin(size);
4621 sizeGridWin.x -= m_rowLabelWidth;
4622 sizeGridWin.y -= m_colLabelHeight;
4623
4624 return sizeGridWin;
4625 }
4626
4627 void wxGrid::CalcWindowSizes()
4628 {
4629 // escape if the window is has not been fully created yet
4630
4631 if ( m_cornerLabelWin == NULL )
4632 return;
4633
4634 int cw, ch;
4635 GetClientSize( &cw, &ch );
4636
4637 // the grid may be too small to have enough space for the labels yet, don't
4638 // size the windows to negative sizes in this case
4639 int gw = cw - m_rowLabelWidth;
4640 int gh = ch - m_colLabelHeight;
4641 if (gw < 0)
4642 gw = 0;
4643 if (gh < 0)
4644 gh = 0;
4645
4646 if ( m_cornerLabelWin && m_cornerLabelWin->IsShown() )
4647 m_cornerLabelWin->SetSize( 0, 0, m_rowLabelWidth, m_colLabelHeight );
4648
4649 if ( m_colLabelWin && m_colLabelWin->IsShown() )
4650 m_colLabelWin->SetSize( m_rowLabelWidth, 0, gw, m_colLabelHeight );
4651
4652 if ( m_rowLabelWin && m_rowLabelWin->IsShown() )
4653 m_rowLabelWin->SetSize( 0, m_colLabelHeight, m_rowLabelWidth, gh );
4654
4655 if ( m_gridWin && m_gridWin->IsShown() )
4656 m_gridWin->SetSize( m_rowLabelWidth, m_colLabelHeight, gw, gh );
4657 }
4658
4659 // this is called when the grid table sends a message
4660 // to indicate that it has been redimensioned
4661 //
4662 bool wxGrid::Redimension( wxGridTableMessage& msg )
4663 {
4664 int i;
4665 bool result = false;
4666
4667 // Clear the attribute cache as the attribute might refer to a different
4668 // cell than stored in the cache after adding/removing rows/columns.
4669 ClearAttrCache();
4670
4671 // By the same reasoning, the editor should be dismissed if columns are
4672 // added or removed. And for consistency, it should IMHO always be
4673 // removed, not only if the cell "underneath" it actually changes.
4674 // For now, I intentionally do not save the editor's content as the
4675 // cell it might want to save that stuff to might no longer exist.
4676 HideCellEditControl();
4677
4678 #if 0
4679 // if we were using the default widths/heights so far, we must change them
4680 // now
4681 if ( m_colWidths.IsEmpty() )
4682 {
4683 InitColWidths();
4684 }
4685
4686 if ( m_rowHeights.IsEmpty() )
4687 {
4688 InitRowHeights();
4689 }
4690 #endif
4691
4692 switch ( msg.GetId() )
4693 {
4694 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED:
4695 {
4696 size_t pos = msg.GetCommandInt();
4697 int numRows = msg.GetCommandInt2();
4698
4699 m_numRows += numRows;
4700
4701 if ( !m_rowHeights.IsEmpty() )
4702 {
4703 m_rowHeights.Insert( m_defaultRowHeight, pos, numRows );
4704 m_rowBottoms.Insert( 0, pos, numRows );
4705
4706 int bottom = 0;
4707 if ( pos > 0 )
4708 bottom = m_rowBottoms[pos - 1];
4709
4710 for ( i = pos; i < m_numRows; i++ )
4711 {
4712 bottom += m_rowHeights[i];
4713 m_rowBottoms[i] = bottom;
4714 }
4715 }
4716
4717 if ( m_currentCellCoords == wxGridNoCellCoords )
4718 {
4719 // if we have just inserted cols into an empty grid the current
4720 // cell will be undefined...
4721 //
4722 SetCurrentCell( 0, 0 );
4723 }
4724
4725 if ( m_selection )
4726 m_selection->UpdateRows( pos, numRows );
4727 wxGridCellAttrProvider * attrProvider = m_table->GetAttrProvider();
4728 if (attrProvider)
4729 attrProvider->UpdateAttrRows( pos, numRows );
4730
4731 if ( !GetBatchCount() )
4732 {
4733 CalcDimensions();
4734 m_rowLabelWin->Refresh();
4735 }
4736 }
4737 result = true;
4738 break;
4739
4740 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED:
4741 {
4742 int numRows = msg.GetCommandInt();
4743 int oldNumRows = m_numRows;
4744 m_numRows += numRows;
4745
4746 if ( !m_rowHeights.IsEmpty() )
4747 {
4748 m_rowHeights.Add( m_defaultRowHeight, numRows );
4749 m_rowBottoms.Add( 0, numRows );
4750
4751 int bottom = 0;
4752 if ( oldNumRows > 0 )
4753 bottom = m_rowBottoms[oldNumRows - 1];
4754
4755 for ( i = oldNumRows; i < m_numRows; i++ )
4756 {
4757 bottom += m_rowHeights[i];
4758 m_rowBottoms[i] = bottom;
4759 }
4760 }
4761
4762 if ( m_currentCellCoords == wxGridNoCellCoords )
4763 {
4764 // if we have just inserted cols into an empty grid the current
4765 // cell will be undefined...
4766 //
4767 SetCurrentCell( 0, 0 );
4768 }
4769
4770 if ( !GetBatchCount() )
4771 {
4772 CalcDimensions();
4773 m_rowLabelWin->Refresh();
4774 }
4775 }
4776 result = true;
4777 break;
4778
4779 case wxGRIDTABLE_NOTIFY_ROWS_DELETED:
4780 {
4781 size_t pos = msg.GetCommandInt();
4782 int numRows = msg.GetCommandInt2();
4783 m_numRows -= numRows;
4784
4785 if ( !m_rowHeights.IsEmpty() )
4786 {
4787 m_rowHeights.RemoveAt( pos, numRows );
4788 m_rowBottoms.RemoveAt( pos, numRows );
4789
4790 int h = 0;
4791 for ( i = 0; i < m_numRows; i++ )
4792 {
4793 h += m_rowHeights[i];
4794 m_rowBottoms[i] = h;
4795 }
4796 }
4797
4798 if ( !m_numRows )
4799 {
4800 m_currentCellCoords = wxGridNoCellCoords;
4801 }
4802 else
4803 {
4804 if ( m_currentCellCoords.GetRow() >= m_numRows )
4805 m_currentCellCoords.Set( 0, 0 );
4806 }
4807
4808 if ( m_selection )
4809 m_selection->UpdateRows( pos, -((int)numRows) );
4810 wxGridCellAttrProvider * attrProvider = m_table->GetAttrProvider();
4811 if (attrProvider)
4812 {
4813 attrProvider->UpdateAttrRows( pos, -((int)numRows) );
4814
4815 // ifdef'd out following patch from Paul Gammans
4816 #if 0
4817 // No need to touch column attributes, unless we
4818 // removed _all_ rows, in this case, we remove
4819 // all column attributes.
4820 // I hate to do this here, but the
4821 // needed data is not available inside UpdateAttrRows.
4822 if ( !GetNumberRows() )
4823 attrProvider->UpdateAttrCols( 0, -GetNumberCols() );
4824 #endif
4825 }
4826
4827 if ( !GetBatchCount() )
4828 {
4829 CalcDimensions();
4830 m_rowLabelWin->Refresh();
4831 }
4832 }
4833 result = true;
4834 break;
4835
4836 case wxGRIDTABLE_NOTIFY_COLS_INSERTED:
4837 {
4838 size_t pos = msg.GetCommandInt();
4839 int numCols = msg.GetCommandInt2();
4840 m_numCols += numCols;
4841
4842 if ( !m_colAt.IsEmpty() )
4843 {
4844 //Shift the column IDs
4845 int i;
4846 for ( i = 0; i < m_numCols - numCols; i++ )
4847 {
4848 if ( m_colAt[i] >= (int)pos )
4849 m_colAt[i] += numCols;
4850 }
4851
4852 m_colAt.Insert( pos, pos, numCols );
4853
4854 //Set the new columns' positions
4855 for ( i = pos + 1; i < (int)pos + numCols; i++ )
4856 {
4857 m_colAt[i] = i;
4858 }
4859 }
4860
4861 if ( !m_colWidths.IsEmpty() )
4862 {
4863 m_colWidths.Insert( m_defaultColWidth, pos, numCols );
4864 m_colRights.Insert( 0, pos, numCols );
4865
4866 int right = 0;
4867 if ( pos > 0 )
4868 right = m_colRights[GetColAt( pos - 1 )];
4869
4870 int colPos;
4871 for ( colPos = pos; colPos < m_numCols; colPos++ )
4872 {
4873 i = GetColAt( colPos );
4874
4875 right += m_colWidths[i];
4876 m_colRights[i] = right;
4877 }
4878 }
4879
4880 if ( m_currentCellCoords == wxGridNoCellCoords )
4881 {
4882 // if we have just inserted cols into an empty grid the current
4883 // cell will be undefined...
4884 //
4885 SetCurrentCell( 0, 0 );
4886 }
4887
4888 if ( m_selection )
4889 m_selection->UpdateCols( pos, numCols );
4890 wxGridCellAttrProvider * attrProvider = m_table->GetAttrProvider();
4891 if (attrProvider)
4892 attrProvider->UpdateAttrCols( pos, numCols );
4893 if ( !GetBatchCount() )
4894 {
4895 CalcDimensions();
4896 m_colLabelWin->Refresh();
4897 }
4898 }
4899 result = true;
4900 break;
4901
4902 case wxGRIDTABLE_NOTIFY_COLS_APPENDED:
4903 {
4904 int numCols = msg.GetCommandInt();
4905 int oldNumCols = m_numCols;
4906 m_numCols += numCols;
4907
4908 if ( !m_colAt.IsEmpty() )
4909 {
4910 m_colAt.Add( 0, numCols );
4911
4912 //Set the new columns' positions
4913 int i;
4914 for ( i = oldNumCols; i < m_numCols; i++ )
4915 {
4916 m_colAt[i] = i;
4917 }
4918 }
4919
4920 if ( !m_colWidths.IsEmpty() )
4921 {
4922 m_colWidths.Add( m_defaultColWidth, numCols );
4923 m_colRights.Add( 0, numCols );
4924
4925 int right = 0;
4926 if ( oldNumCols > 0 )
4927 right = m_colRights[GetColAt( oldNumCols - 1 )];
4928
4929 int colPos;
4930 for ( colPos = oldNumCols; colPos < m_numCols; colPos++ )
4931 {
4932 i = GetColAt( colPos );
4933
4934 right += m_colWidths[i];
4935 m_colRights[i] = right;
4936 }
4937 }
4938
4939 if ( m_currentCellCoords == wxGridNoCellCoords )
4940 {
4941 // if we have just inserted cols into an empty grid the current
4942 // cell will be undefined...
4943 //
4944 SetCurrentCell( 0, 0 );
4945 }
4946 if ( !GetBatchCount() )
4947 {
4948 CalcDimensions();
4949 m_colLabelWin->Refresh();
4950 }
4951 }
4952 result = true;
4953 break;
4954
4955 case wxGRIDTABLE_NOTIFY_COLS_DELETED:
4956 {
4957 size_t pos = msg.GetCommandInt();
4958 int numCols = msg.GetCommandInt2();
4959 m_numCols -= numCols;
4960
4961 if ( !m_colAt.IsEmpty() )
4962 {
4963 int colID = GetColAt( pos );
4964
4965 m_colAt.RemoveAt( pos, numCols );
4966
4967 //Shift the column IDs
4968 int colPos;
4969 for ( colPos = 0; colPos < m_numCols; colPos++ )
4970 {
4971 if ( m_colAt[colPos] > colID )
4972 m_colAt[colPos] -= numCols;
4973 }
4974 }
4975
4976 if ( !m_colWidths.IsEmpty() )
4977 {
4978 m_colWidths.RemoveAt( pos, numCols );
4979 m_colRights.RemoveAt( pos, numCols );
4980
4981 int w = 0;
4982 int colPos;
4983 for ( colPos = 0; colPos < m_numCols; colPos++ )
4984 {
4985 i = GetColAt( colPos );
4986
4987 w += m_colWidths[i];
4988 m_colRights[i] = w;
4989 }
4990 }
4991
4992 if ( !m_numCols )
4993 {
4994 m_currentCellCoords = wxGridNoCellCoords;
4995 }
4996 else
4997 {
4998 if ( m_currentCellCoords.GetCol() >= m_numCols )
4999 m_currentCellCoords.Set( 0, 0 );
5000 }
5001
5002 if ( m_selection )
5003 m_selection->UpdateCols( pos, -((int)numCols) );
5004 wxGridCellAttrProvider * attrProvider = m_table->GetAttrProvider();
5005 if (attrProvider)
5006 {
5007 attrProvider->UpdateAttrCols( pos, -((int)numCols) );
5008
5009 // ifdef'd out following patch from Paul Gammans
5010 #if 0
5011 // No need to touch row attributes, unless we
5012 // removed _all_ columns, in this case, we remove
5013 // all row attributes.
5014 // I hate to do this here, but the
5015 // needed data is not available inside UpdateAttrCols.
5016 if ( !GetNumberCols() )
5017 attrProvider->UpdateAttrRows( 0, -GetNumberRows() );
5018 #endif
5019 }
5020
5021 if ( !GetBatchCount() )
5022 {
5023 CalcDimensions();
5024 m_colLabelWin->Refresh();
5025 }
5026 }
5027 result = true;
5028 break;
5029 }
5030
5031 if (result && !GetBatchCount() )
5032 m_gridWin->Refresh();
5033
5034 return result;
5035 }
5036
5037 wxArrayInt wxGrid::CalcRowLabelsExposed( const wxRegion& reg ) const
5038 {
5039 wxRegionIterator iter( reg );
5040 wxRect r;
5041
5042 wxArrayInt rowlabels;
5043
5044 int top, bottom;
5045 while ( iter )
5046 {
5047 r = iter.GetRect();
5048
5049 // TODO: remove this when we can...
5050 // There is a bug in wxMotif that gives garbage update
5051 // rectangles if you jump-scroll a long way by clicking the
5052 // scrollbar with middle button. This is a work-around
5053 //
5054 #if defined(__WXMOTIF__)
5055 int cw, ch;
5056 m_gridWin->GetClientSize( &cw, &ch );
5057 if ( r.GetTop() > ch )
5058 r.SetTop( 0 );
5059 r.SetBottom( wxMin( r.GetBottom(), ch ) );
5060 #endif
5061
5062 // logical bounds of update region
5063 //
5064 int dummy;
5065 CalcUnscrolledPosition( 0, r.GetTop(), &dummy, &top );
5066 CalcUnscrolledPosition( 0, r.GetBottom(), &dummy, &bottom );
5067
5068 // find the row labels within these bounds
5069 //
5070 int row;
5071 for ( row = internalYToRow(top); row < m_numRows; row++ )
5072 {
5073 if ( GetRowBottom(row) < top )
5074 continue;
5075
5076 if ( GetRowTop(row) > bottom )
5077 break;
5078
5079 rowlabels.Add( row );
5080 }
5081
5082 ++iter;
5083 }
5084
5085 return rowlabels;
5086 }
5087
5088 wxArrayInt wxGrid::CalcColLabelsExposed( const wxRegion& reg ) const
5089 {
5090 wxRegionIterator iter( reg );
5091 wxRect r;
5092
5093 wxArrayInt colLabels;
5094
5095 int left, right;
5096 while ( iter )
5097 {
5098 r = iter.GetRect();
5099
5100 // TODO: remove this when we can...
5101 // There is a bug in wxMotif that gives garbage update
5102 // rectangles if you jump-scroll a long way by clicking the
5103 // scrollbar with middle button. This is a work-around
5104 //
5105 #if defined(__WXMOTIF__)
5106 int cw, ch;
5107 m_gridWin->GetClientSize( &cw, &ch );
5108 if ( r.GetLeft() > cw )
5109 r.SetLeft( 0 );
5110 r.SetRight( wxMin( r.GetRight(), cw ) );
5111 #endif
5112
5113 // logical bounds of update region
5114 //
5115 int dummy;
5116 CalcUnscrolledPosition( r.GetLeft(), 0, &left, &dummy );
5117 CalcUnscrolledPosition( r.GetRight(), 0, &right, &dummy );
5118
5119 // find the cells within these bounds
5120 //
5121 int col;
5122 int colPos;
5123 for ( colPos = GetColPos( internalXToCol(left) ); colPos < m_numCols; colPos++ )
5124 {
5125 col = GetColAt( colPos );
5126
5127 if ( GetColRight(col) < left )
5128 continue;
5129
5130 if ( GetColLeft(col) > right )
5131 break;
5132
5133 colLabels.Add( col );
5134 }
5135
5136 ++iter;
5137 }
5138
5139 return colLabels;
5140 }
5141
5142 wxGridCellCoordsArray wxGrid::CalcCellsExposed( const wxRegion& reg ) const
5143 {
5144 wxRegionIterator iter( reg );
5145 wxRect r;
5146
5147 wxGridCellCoordsArray cellsExposed;
5148
5149 int left, top, right, bottom;
5150 while ( iter )
5151 {
5152 r = iter.GetRect();
5153
5154 // TODO: remove this when we can...
5155 // There is a bug in wxMotif that gives garbage update
5156 // rectangles if you jump-scroll a long way by clicking the
5157 // scrollbar with middle button. This is a work-around
5158 //
5159 #if defined(__WXMOTIF__)
5160 int cw, ch;
5161 m_gridWin->GetClientSize( &cw, &ch );
5162 if ( r.GetTop() > ch ) r.SetTop( 0 );
5163 if ( r.GetLeft() > cw ) r.SetLeft( 0 );
5164 r.SetRight( wxMin( r.GetRight(), cw ) );
5165 r.SetBottom( wxMin( r.GetBottom(), ch ) );
5166 #endif
5167
5168 // logical bounds of update region
5169 //
5170 CalcUnscrolledPosition( r.GetLeft(), r.GetTop(), &left, &top );
5171 CalcUnscrolledPosition( r.GetRight(), r.GetBottom(), &right, &bottom );
5172
5173 // find the cells within these bounds
5174 //
5175 int row, col;
5176 for ( row = internalYToRow(top); row < m_numRows; row++ )
5177 {
5178 if ( GetRowBottom(row) <= top )
5179 continue;
5180
5181 if ( GetRowTop(row) > bottom )
5182 break;
5183
5184 int colPos;
5185 for ( colPos = GetColPos( internalXToCol(left) ); colPos < m_numCols; colPos++ )
5186 {
5187 col = GetColAt( colPos );
5188
5189 if ( GetColRight(col) <= left )
5190 continue;
5191
5192 if ( GetColLeft(col) > right )
5193 break;
5194
5195 cellsExposed.Add( wxGridCellCoords( row, col ) );
5196 }
5197 }
5198
5199 ++iter;
5200 }
5201
5202 return cellsExposed;
5203 }
5204
5205
5206 void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent& event )
5207 {
5208 int x, y, row;
5209 wxPoint pos( event.GetPosition() );
5210 CalcUnscrolledPosition( pos.x, pos.y, &x, &y );
5211
5212 if ( event.Dragging() )
5213 {
5214 if (!m_isDragging)
5215 {
5216 m_isDragging = true;
5217 m_rowLabelWin->CaptureMouse();
5218 }
5219
5220 if ( event.LeftIsDown() )
5221 {
5222 switch ( m_cursorMode )
5223 {
5224 case WXGRID_CURSOR_RESIZE_ROW:
5225 {
5226 int cw, ch, left, dummy;
5227 m_gridWin->GetClientSize( &cw, &ch );
5228 CalcUnscrolledPosition( 0, 0, &left, &dummy );
5229
5230 wxClientDC dc( m_gridWin );
5231 PrepareDC( dc );
5232 y = wxMax( y,
5233 GetRowTop(m_dragRowOrCol) +
5234 GetRowMinimalHeight(m_dragRowOrCol) );
5235 dc.SetLogicalFunction(wxINVERT);
5236 if ( m_dragLastPos >= 0 )
5237 {
5238 dc.DrawLine( left, m_dragLastPos, left+cw, m_dragLastPos );
5239 }
5240 dc.DrawLine( left, y, left+cw, y );
5241 m_dragLastPos = y;
5242 }
5243 break;
5244
5245 case WXGRID_CURSOR_SELECT_ROW:
5246 {
5247 if ( (row = YToRow( y )) >= 0 )
5248 {
5249 if ( m_selection )
5250 {
5251 m_selection->SelectRow( row,
5252 event.ControlDown(),
5253 event.ShiftDown(),
5254 event.AltDown(),
5255 event.MetaDown() );
5256 }
5257 }
5258 }
5259 break;
5260
5261 // default label to suppress warnings about "enumeration value
5262 // 'xxx' not handled in switch
5263 default:
5264 break;
5265 }
5266 }
5267 return;
5268 }
5269
5270 if ( m_isDragging && (event.Entering() || event.Leaving()) )
5271 return;
5272
5273 if (m_isDragging)
5274 {
5275 if (m_rowLabelWin->HasCapture())
5276 m_rowLabelWin->ReleaseMouse();
5277 m_isDragging = false;
5278 }
5279
5280 // ------------ Entering or leaving the window
5281 //
5282 if ( event.Entering() || event.Leaving() )
5283 {
5284 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_rowLabelWin);
5285 }
5286
5287 // ------------ Left button pressed
5288 //
5289 else if ( event.LeftDown() )
5290 {
5291 // don't send a label click event for a hit on the
5292 // edge of the row label - this is probably the user
5293 // wanting to resize the row
5294 //
5295 if ( YToEdgeOfRow(y) < 0 )
5296 {
5297 row = YToRow(y);
5298 if ( row >= 0 &&
5299 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK, row, -1, event ) )
5300 {
5301 if ( !event.ShiftDown() && !event.CmdDown() )
5302 ClearSelection();
5303 if ( m_selection )
5304 {
5305 if ( event.ShiftDown() )
5306 {
5307 m_selection->SelectBlock( m_currentCellCoords.GetRow(),
5308 0,
5309 row,
5310 GetNumberCols() - 1,
5311 event.ControlDown(),
5312 event.ShiftDown(),
5313 event.AltDown(),
5314 event.MetaDown() );
5315 }
5316 else
5317 {
5318 m_selection->SelectRow( row,
5319 event.ControlDown(),
5320 event.ShiftDown(),
5321 event.AltDown(),
5322 event.MetaDown() );
5323 }
5324 }
5325
5326 ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW, m_rowLabelWin);
5327 }
5328 }
5329 else
5330 {
5331 // starting to drag-resize a row
5332 if ( CanDragRowSize() )
5333 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW, m_rowLabelWin);
5334 }
5335 }
5336
5337 // ------------ Left double click
5338 //
5339 else if (event.LeftDClick() )
5340 {
5341 row = YToEdgeOfRow(y);
5342 if ( row < 0 )
5343 {
5344 row = YToRow(y);
5345 if ( row >=0 &&
5346 !SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK, row, -1, event ) )
5347 {
5348 // no default action at the moment
5349 }
5350 }
5351 else
5352 {
5353 // adjust row height depending on label text
5354 AutoSizeRowLabelSize( row );
5355
5356 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_colLabelWin);
5357 m_dragLastPos = -1;
5358 }
5359 }
5360
5361 // ------------ Left button released
5362 //
5363 else if ( event.LeftUp() )
5364 {
5365 if ( m_cursorMode == WXGRID_CURSOR_RESIZE_ROW )
5366 {
5367 DoEndDragResizeRow();
5368
5369 // Note: we are ending the event *after* doing
5370 // default processing in this case
5371 //
5372 SendEvent( wxEVT_GRID_ROW_SIZE, m_dragRowOrCol, -1, event );
5373 }
5374
5375 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_rowLabelWin);
5376 m_dragLastPos = -1;
5377 }
5378
5379 // ------------ Right button down
5380 //
5381 else if ( event.RightDown() )
5382 {
5383 row = YToRow(y);
5384 if ( row >=0 &&
5385 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK, row, -1, event ) )
5386 {
5387 // no default action at the moment
5388 }
5389 }
5390
5391 // ------------ Right double click
5392 //
5393 else if ( event.RightDClick() )
5394 {
5395 row = YToRow(y);
5396 if ( row >= 0 &&
5397 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK, row, -1, event ) )
5398 {
5399 // no default action at the moment
5400 }
5401 }
5402
5403 // ------------ No buttons down and mouse moving
5404 //
5405 else if ( event.Moving() )
5406 {
5407 m_dragRowOrCol = YToEdgeOfRow( y );
5408 if ( m_dragRowOrCol >= 0 )
5409 {
5410 if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
5411 {
5412 // don't capture the mouse yet
5413 if ( CanDragRowSize() )
5414 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW, m_rowLabelWin, false);
5415 }
5416 }
5417 else if ( m_cursorMode != WXGRID_CURSOR_SELECT_CELL )
5418 {
5419 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_rowLabelWin, false);
5420 }
5421 }
5422 }
5423
5424 void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent& event )
5425 {
5426 int x, y, col;
5427 wxPoint pos( event.GetPosition() );
5428 CalcUnscrolledPosition( pos.x, pos.y, &x, &y );
5429
5430 if ( event.Dragging() )
5431 {
5432 if (!m_isDragging)
5433 {
5434 m_isDragging = true;
5435 m_colLabelWin->CaptureMouse();
5436
5437 if ( m_cursorMode == WXGRID_CURSOR_MOVE_COL )
5438 m_dragRowOrCol = XToCol( x );
5439 }
5440
5441 if ( event.LeftIsDown() )
5442 {
5443 switch ( m_cursorMode )
5444 {
5445 case WXGRID_CURSOR_RESIZE_COL:
5446 {
5447 int cw, ch, dummy, top;
5448 m_gridWin->GetClientSize( &cw, &ch );
5449 CalcUnscrolledPosition( 0, 0, &dummy, &top );
5450
5451 wxClientDC dc( m_gridWin );
5452 PrepareDC( dc );
5453
5454 x = wxMax( x, GetColLeft(m_dragRowOrCol) +
5455 GetColMinimalWidth(m_dragRowOrCol));
5456 dc.SetLogicalFunction(wxINVERT);
5457 if ( m_dragLastPos >= 0 )
5458 {
5459 dc.DrawLine( m_dragLastPos, top, m_dragLastPos, top + ch );
5460 }
5461 dc.DrawLine( x, top, x, top + ch );
5462 m_dragLastPos = x;
5463 }
5464 break;
5465
5466 case WXGRID_CURSOR_SELECT_COL:
5467 {
5468 if ( (col = XToCol( x )) >= 0 )
5469 {
5470 if ( m_selection )
5471 {
5472 m_selection->SelectCol( col,
5473 event.ControlDown(),
5474 event.ShiftDown(),
5475 event.AltDown(),
5476 event.MetaDown() );
5477 }
5478 }
5479 }
5480 break;
5481
5482 case WXGRID_CURSOR_MOVE_COL:
5483 {
5484 if ( x < 0 )
5485 m_moveToCol = GetColAt( 0 );
5486 else
5487 m_moveToCol = XToCol( x );
5488
5489 int markerX;
5490
5491 if ( m_moveToCol < 0 )
5492 markerX = GetColRight( GetColAt( m_numCols - 1 ) );
5493 else if ( x >= (GetColLeft( m_moveToCol ) + (GetColWidth(m_moveToCol) / 2)) )
5494 {
5495 m_moveToCol = GetColAt( GetColPos( m_moveToCol ) + 1 );
5496 if ( m_moveToCol < 0 )
5497 markerX = GetColRight( GetColAt( m_numCols - 1 ) );
5498 else
5499 markerX = GetColLeft( m_moveToCol );
5500 }
5501 else
5502 markerX = GetColLeft( m_moveToCol );
5503
5504 if ( markerX != m_dragLastPos )
5505 {
5506 wxClientDC dc( m_colLabelWin );
5507 DoPrepareDC(dc);
5508
5509 int cw, ch;
5510 m_colLabelWin->GetClientSize( &cw, &ch );
5511
5512 markerX++;
5513
5514 //Clean up the last indicator
5515 if ( m_dragLastPos >= 0 )
5516 {
5517 wxPen pen( m_colLabelWin->GetBackgroundColour(), 2 );
5518 dc.SetPen(pen);
5519 dc.DrawLine( m_dragLastPos + 1, 0, m_dragLastPos + 1, ch );
5520 dc.SetPen(wxNullPen);
5521
5522 if ( XToCol( m_dragLastPos ) != -1 )
5523 DrawColLabel( dc, XToCol( m_dragLastPos ) );
5524 }
5525
5526 const wxColour *color;
5527 //Moving to the same place? Don't draw a marker
5528 if ( (m_moveToCol == m_dragRowOrCol)
5529 || (GetColPos( m_moveToCol ) == GetColPos( m_dragRowOrCol ) + 1)
5530 || (m_moveToCol < 0 && m_dragRowOrCol == GetColAt( m_numCols - 1 )))
5531 color = wxLIGHT_GREY;
5532 else
5533 color = wxBLUE;
5534
5535 //Draw the marker
5536 wxPen pen( *color, 2 );
5537 dc.SetPen(pen);
5538
5539 dc.DrawLine( markerX, 0, markerX, ch );
5540
5541 dc.SetPen(wxNullPen);
5542
5543 m_dragLastPos = markerX - 1;
5544 }
5545 }
5546 break;
5547
5548 // default label to suppress warnings about "enumeration value
5549 // 'xxx' not handled in switch
5550 default:
5551 break;
5552 }
5553 }
5554 return;
5555 }
5556
5557 if ( m_isDragging && (event.Entering() || event.Leaving()) )
5558 return;
5559
5560 if (m_isDragging)
5561 {
5562 if (m_colLabelWin->HasCapture())
5563 m_colLabelWin->ReleaseMouse();
5564 m_isDragging = false;
5565 }
5566
5567 // ------------ Entering or leaving the window
5568 //
5569 if ( event.Entering() || event.Leaving() )
5570 {
5571 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_colLabelWin);
5572 }
5573
5574 // ------------ Left button pressed
5575 //
5576 else if ( event.LeftDown() )
5577 {
5578 // don't send a label click event for a hit on the
5579 // edge of the col label - this is probably the user
5580 // wanting to resize the col
5581 //
5582 if ( XToEdgeOfCol(x) < 0 )
5583 {
5584 col = XToCol(x);
5585 if ( col >= 0 &&
5586 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK, -1, col, event ) )
5587 {
5588 if ( m_canDragColMove )
5589 {
5590 //Show button as pressed
5591 wxClientDC dc( m_colLabelWin );
5592 int colLeft = GetColLeft( col );
5593 int colRight = GetColRight( col ) - 1;
5594 dc.SetPen( wxPen( m_colLabelWin->GetBackgroundColour(), 1 ) );
5595 dc.DrawLine( colLeft, 1, colLeft, m_colLabelHeight-1 );
5596 dc.DrawLine( colLeft, 1, colRight, 1 );
5597
5598 ChangeCursorMode(WXGRID_CURSOR_MOVE_COL, m_colLabelWin);
5599 }
5600 else
5601 {
5602 if ( !event.ShiftDown() && !event.CmdDown() )
5603 ClearSelection();
5604 if ( m_selection )
5605 {
5606 if ( event.ShiftDown() )
5607 {
5608 m_selection->SelectBlock( 0,
5609 m_currentCellCoords.GetCol(),
5610 GetNumberRows() - 1, col,
5611 event.ControlDown(),
5612 event.ShiftDown(),
5613 event.AltDown(),
5614 event.MetaDown() );
5615 }
5616 else
5617 {
5618 m_selection->SelectCol( col,
5619 event.ControlDown(),
5620 event.ShiftDown(),
5621 event.AltDown(),
5622 event.MetaDown() );
5623 }
5624 }
5625
5626 ChangeCursorMode(WXGRID_CURSOR_SELECT_COL, m_colLabelWin);
5627 }
5628 }
5629 }
5630 else
5631 {
5632 // starting to drag-resize a col
5633 //
5634 if ( CanDragColSize() )
5635 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL, m_colLabelWin);
5636 }
5637 }
5638
5639 // ------------ Left double click
5640 //
5641 if ( event.LeftDClick() )
5642 {
5643 col = XToEdgeOfCol(x);
5644 if ( col < 0 )
5645 {
5646 col = XToCol(x);
5647 if ( col >= 0 &&
5648 ! SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK, -1, col, event ) )
5649 {
5650 // no default action at the moment
5651 }
5652 }
5653 else
5654 {
5655 // adjust column width depending on label text
5656 AutoSizeColLabelSize( col );
5657
5658 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_colLabelWin);
5659 m_dragLastPos = -1;
5660 }
5661 }
5662
5663 // ------------ Left button released
5664 //
5665 else if ( event.LeftUp() )
5666 {
5667 switch ( m_cursorMode )
5668 {
5669 case WXGRID_CURSOR_RESIZE_COL:
5670 DoEndDragResizeCol();
5671
5672 // Note: we are ending the event *after* doing
5673 // default processing in this case
5674 //
5675 SendEvent( wxEVT_GRID_COL_SIZE, -1, m_dragRowOrCol, event );
5676 break;
5677
5678 case WXGRID_CURSOR_MOVE_COL:
5679 DoEndDragMoveCol();
5680
5681 SendEvent( wxEVT_GRID_COL_MOVE, -1, m_dragRowOrCol, event );
5682 break;
5683
5684 case WXGRID_CURSOR_SELECT_COL:
5685 case WXGRID_CURSOR_SELECT_CELL:
5686 case WXGRID_CURSOR_RESIZE_ROW:
5687 case WXGRID_CURSOR_SELECT_ROW:
5688 // nothing to do (?)
5689 break;
5690 }
5691
5692 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_colLabelWin);
5693 m_dragLastPos = -1;
5694 }
5695
5696 // ------------ Right button down
5697 //
5698 else if ( event.RightDown() )
5699 {
5700 col = XToCol(x);
5701 if ( col >= 0 &&
5702 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK, -1, col, event ) )
5703 {
5704 // no default action at the moment
5705 }
5706 }
5707
5708 // ------------ Right double click
5709 //
5710 else if ( event.RightDClick() )
5711 {
5712 col = XToCol(x);
5713 if ( col >= 0 &&
5714 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK, -1, col, event ) )
5715 {
5716 // no default action at the moment
5717 }
5718 }
5719
5720 // ------------ No buttons down and mouse moving
5721 //
5722 else if ( event.Moving() )
5723 {
5724 m_dragRowOrCol = XToEdgeOfCol( x );
5725 if ( m_dragRowOrCol >= 0 )
5726 {
5727 if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
5728 {
5729 // don't capture the cursor yet
5730 if ( CanDragColSize() )
5731 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL, m_colLabelWin, false);
5732 }
5733 }
5734 else if ( m_cursorMode != WXGRID_CURSOR_SELECT_CELL )
5735 {
5736 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_colLabelWin, false);
5737 }
5738 }
5739 }
5740
5741 void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent& event )
5742 {
5743 if ( event.LeftDown() )
5744 {
5745 // indicate corner label by having both row and
5746 // col args == -1
5747 //
5748 if ( !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK, -1, -1, event ) )
5749 {
5750 SelectAll();
5751 }
5752 }
5753 else if ( event.LeftDClick() )
5754 {
5755 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK, -1, -1, event );
5756 }
5757 else if ( event.RightDown() )
5758 {
5759 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK, -1, -1, event ) )
5760 {
5761 // no default action at the moment
5762 }
5763 }
5764 else if ( event.RightDClick() )
5765 {
5766 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK, -1, -1, event ) )
5767 {
5768 // no default action at the moment
5769 }
5770 }
5771 }
5772
5773 void wxGrid::CancelMouseCapture()
5774 {
5775 // cancel operation currently in progress, whatever it is
5776 if ( m_winCapture )
5777 {
5778 m_isDragging = false;
5779 m_cursorMode = WXGRID_CURSOR_SELECT_CELL;
5780 m_winCapture->SetCursor( *wxSTANDARD_CURSOR );
5781 m_winCapture = NULL;
5782
5783 // remove traces of whatever we drew on screen
5784 Refresh();
5785 }
5786 }
5787
5788 void wxGrid::ChangeCursorMode(CursorMode mode,
5789 wxWindow *win,
5790 bool captureMouse)
5791 {
5792 #ifdef __WXDEBUG__
5793 static const wxChar *cursorModes[] =
5794 {
5795 _T("SELECT_CELL"),
5796 _T("RESIZE_ROW"),
5797 _T("RESIZE_COL"),
5798 _T("SELECT_ROW"),
5799 _T("SELECT_COL"),
5800 _T("MOVE_COL"),
5801 };
5802
5803 wxLogTrace(_T("grid"),
5804 _T("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
5805 win == m_colLabelWin ? _T("colLabelWin")
5806 : win ? _T("rowLabelWin")
5807 : _T("gridWin"),
5808 cursorModes[m_cursorMode], cursorModes[mode]);
5809 #endif
5810
5811 if ( mode == m_cursorMode &&
5812 win == m_winCapture &&
5813 captureMouse == (m_winCapture != NULL))
5814 return;
5815
5816 if ( !win )
5817 {
5818 // by default use the grid itself
5819 win = m_gridWin;
5820 }
5821
5822 if ( m_winCapture )
5823 {
5824 if (m_winCapture->HasCapture())
5825 m_winCapture->ReleaseMouse();
5826 m_winCapture = (wxWindow *)NULL;
5827 }
5828
5829 m_cursorMode = mode;
5830
5831 switch ( m_cursorMode )
5832 {
5833 case WXGRID_CURSOR_RESIZE_ROW:
5834 win->SetCursor( m_rowResizeCursor );
5835 break;
5836
5837 case WXGRID_CURSOR_RESIZE_COL:
5838 win->SetCursor( m_colResizeCursor );
5839 break;
5840
5841 case WXGRID_CURSOR_MOVE_COL:
5842 win->SetCursor( wxCursor(wxCURSOR_HAND) );
5843 break;
5844
5845 default:
5846 win->SetCursor( *wxSTANDARD_CURSOR );
5847 break;
5848 }
5849
5850 // we need to capture mouse when resizing
5851 bool resize = m_cursorMode == WXGRID_CURSOR_RESIZE_ROW ||
5852 m_cursorMode == WXGRID_CURSOR_RESIZE_COL;
5853
5854 if ( captureMouse && resize )
5855 {
5856 win->CaptureMouse();
5857 m_winCapture = win;
5858 }
5859 }
5860
5861 void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent& event )
5862 {
5863 int x, y;
5864 wxPoint pos( event.GetPosition() );
5865 CalcUnscrolledPosition( pos.x, pos.y, &x, &y );
5866
5867 wxGridCellCoords coords;
5868 XYToCell( x, y, coords );
5869
5870 int cell_rows, cell_cols;
5871 bool isFirstDrag = !m_isDragging;
5872 GetCellSize( coords.GetRow(), coords.GetCol(), &cell_rows, &cell_cols );
5873 if ((cell_rows < 0) || (cell_cols < 0))
5874 {
5875 coords.SetRow(coords.GetRow() + cell_rows);
5876 coords.SetCol(coords.GetCol() + cell_cols);
5877 }
5878
5879 if ( event.Dragging() )
5880 {
5881 //wxLogDebug("pos(%d, %d) coords(%d, %d)", pos.x, pos.y, coords.GetRow(), coords.GetCol());
5882
5883 // Don't start doing anything until the mouse has been dragged at
5884 // least 3 pixels in any direction...
5885 if (! m_isDragging)
5886 {
5887 if (m_startDragPos == wxDefaultPosition)
5888 {
5889 m_startDragPos = pos;
5890 return;
5891 }
5892 if (abs(m_startDragPos.x - pos.x) < 4 && abs(m_startDragPos.y - pos.y) < 4)
5893 return;
5894 }
5895
5896 m_isDragging = true;
5897 if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
5898 {
5899 // Hide the edit control, so it
5900 // won't interfere with drag-shrinking.
5901 if ( IsCellEditControlShown() )
5902 {
5903 HideCellEditControl();
5904 SaveEditControlValue();
5905 }
5906
5907 if ( coords != wxGridNoCellCoords )
5908 {
5909 if ( event.CmdDown() )
5910 {
5911 if ( m_selectingKeyboard == wxGridNoCellCoords)
5912 m_selectingKeyboard = coords;
5913 HighlightBlock( m_selectingKeyboard, coords );
5914 }
5915 else if ( CanDragCell() )
5916 {
5917 if ( isFirstDrag )
5918 {
5919 if ( m_selectingKeyboard == wxGridNoCellCoords)
5920 m_selectingKeyboard = coords;
5921
5922 SendEvent( wxEVT_GRID_CELL_BEGIN_DRAG,
5923 coords.GetRow(),
5924 coords.GetCol(),
5925 event );
5926 return;
5927 }
5928 }
5929 else
5930 {
5931 if ( !IsSelection() )
5932 {
5933 HighlightBlock( coords, coords );
5934 }
5935 else
5936 {
5937 HighlightBlock( m_currentCellCoords, coords );
5938 }
5939 }
5940
5941 if (! IsVisible(coords))
5942 {
5943 MakeCellVisible(coords);
5944 // TODO: need to introduce a delay or something here. The
5945 // scrolling is way too fast, at least under MSW and GTK.
5946 }
5947 }
5948 // Have we captured the mouse yet?
5949 if (! m_winCapture)
5950 {
5951 m_winCapture = m_gridWin;
5952 m_winCapture->CaptureMouse();
5953 }
5954
5955
5956 }
5957 else if ( event.LeftIsDown() &&
5958 m_cursorMode == WXGRID_CURSOR_RESIZE_ROW )
5959 {
5960 int cw, ch, left, dummy;
5961 m_gridWin->GetClientSize( &cw, &ch );
5962 CalcUnscrolledPosition( 0, 0, &left, &dummy );
5963
5964 wxClientDC dc( m_gridWin );
5965 PrepareDC( dc );
5966 y = wxMax( y, GetRowTop(m_dragRowOrCol) +
5967 GetRowMinimalHeight(m_dragRowOrCol) );
5968 dc.SetLogicalFunction(wxINVERT);
5969 if ( m_dragLastPos >= 0 )
5970 {
5971 dc.DrawLine( left, m_dragLastPos, left+cw, m_dragLastPos );
5972 }
5973 dc.DrawLine( left, y, left+cw, y );
5974 m_dragLastPos = y;
5975 }
5976 else if ( event.LeftIsDown() &&
5977 m_cursorMode == WXGRID_CURSOR_RESIZE_COL )
5978 {
5979 int cw, ch, dummy, top;
5980 m_gridWin->GetClientSize( &cw, &ch );
5981 CalcUnscrolledPosition( 0, 0, &dummy, &top );
5982
5983 wxClientDC dc( m_gridWin );
5984 PrepareDC( dc );
5985 x = wxMax( x, GetColLeft(m_dragRowOrCol) +
5986 GetColMinimalWidth(m_dragRowOrCol) );
5987 dc.SetLogicalFunction(wxINVERT);
5988 if ( m_dragLastPos >= 0 )
5989 {
5990 dc.DrawLine( m_dragLastPos, top, m_dragLastPos, top + ch );
5991 }
5992 dc.DrawLine( x, top, x, top + ch );
5993 m_dragLastPos = x;
5994 }
5995
5996 return;
5997 }
5998
5999 m_isDragging = false;
6000 m_startDragPos = wxDefaultPosition;
6001
6002 // VZ: if we do this, the mode is reset to WXGRID_CURSOR_SELECT_CELL
6003 // immediately after it becomes WXGRID_CURSOR_RESIZE_ROW/COL under
6004 // wxGTK
6005 #if 0
6006 if ( event.Entering() || event.Leaving() )
6007 {
6008 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL);
6009 m_gridWin->SetCursor( *wxSTANDARD_CURSOR );
6010 }
6011 else
6012 #endif // 0
6013
6014 // ------------ Left button pressed
6015 //
6016 if ( event.LeftDown() && coords != wxGridNoCellCoords )
6017 {
6018 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_CLICK,
6019 coords.GetRow(),
6020 coords.GetCol(),
6021 event ) )
6022 {
6023 if ( !event.CmdDown() )
6024 ClearSelection();
6025 if ( event.ShiftDown() )
6026 {
6027 if ( m_selection )
6028 {
6029 m_selection->SelectBlock( m_currentCellCoords.GetRow(),
6030 m_currentCellCoords.GetCol(),
6031 coords.GetRow(),
6032 coords.GetCol(),
6033 event.ControlDown(),
6034 event.ShiftDown(),
6035 event.AltDown(),
6036 event.MetaDown() );
6037 }
6038 }
6039 else if ( XToEdgeOfCol(x) < 0 &&
6040 YToEdgeOfRow(y) < 0 )
6041 {
6042 DisableCellEditControl();
6043 MakeCellVisible( coords );
6044
6045 if ( event.CmdDown() )
6046 {
6047 if ( m_selection )
6048 {
6049 m_selection->ToggleCellSelection( coords.GetRow(),
6050 coords.GetCol(),
6051 event.ControlDown(),
6052 event.ShiftDown(),
6053 event.AltDown(),
6054 event.MetaDown() );
6055 }
6056 m_selectingTopLeft = wxGridNoCellCoords;
6057 m_selectingBottomRight = wxGridNoCellCoords;
6058 m_selectingKeyboard = coords;
6059 }
6060 else
6061 {
6062 m_waitForSlowClick = m_currentCellCoords == coords &&
6063 coords != wxGridNoCellCoords;
6064 SetCurrentCell( coords );
6065 }
6066 }
6067 }
6068 }
6069
6070 // ------------ Left double click
6071 //
6072 else if ( event.LeftDClick() && coords != wxGridNoCellCoords )
6073 {
6074 DisableCellEditControl();
6075
6076 if ( XToEdgeOfCol(x) < 0 && YToEdgeOfRow(y) < 0 )
6077 {
6078 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_DCLICK,
6079 coords.GetRow(),
6080 coords.GetCol(),
6081 event ) )
6082 {
6083 // we want double click to select a cell and start editing
6084 // (i.e. to behave in same way as sequence of two slow clicks):
6085 m_waitForSlowClick = true;
6086 }
6087 }
6088 }
6089
6090 // ------------ Left button released
6091 //
6092 else if ( event.LeftUp() )
6093 {
6094 if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
6095 {
6096 if (m_winCapture)
6097 {
6098 if (m_winCapture->HasCapture())
6099 m_winCapture->ReleaseMouse();
6100 m_winCapture = NULL;
6101 }
6102
6103 if ( coords == m_currentCellCoords && m_waitForSlowClick && CanEnableCellControl() )
6104 {
6105 ClearSelection();
6106 EnableCellEditControl();
6107
6108 wxGridCellAttr *attr = GetCellAttr(coords);
6109 wxGridCellEditor *editor = attr->GetEditor(this, coords.GetRow(), coords.GetCol());
6110 editor->StartingClick();
6111 editor->DecRef();
6112 attr->DecRef();
6113
6114 m_waitForSlowClick = false;
6115 }
6116 else if ( m_selectingTopLeft != wxGridNoCellCoords &&
6117 m_selectingBottomRight != wxGridNoCellCoords )
6118 {
6119 if ( m_selection )
6120 {
6121 m_selection->SelectBlock( m_selectingTopLeft.GetRow(),
6122 m_selectingTopLeft.GetCol(),
6123 m_selectingBottomRight.GetRow(),
6124 m_selectingBottomRight.GetCol(),
6125 event.ControlDown(),
6126 event.ShiftDown(),
6127 event.AltDown(),
6128 event.MetaDown() );
6129 }
6130
6131 m_selectingTopLeft = wxGridNoCellCoords;
6132 m_selectingBottomRight = wxGridNoCellCoords;
6133
6134 // Show the edit control, if it has been hidden for
6135 // drag-shrinking.
6136 ShowCellEditControl();
6137 }
6138 }
6139 else if ( m_cursorMode == WXGRID_CURSOR_RESIZE_ROW )
6140 {
6141 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL);
6142 DoEndDragResizeRow();
6143
6144 // Note: we are ending the event *after* doing
6145 // default processing in this case
6146 //
6147 SendEvent( wxEVT_GRID_ROW_SIZE, m_dragRowOrCol, -1, event );
6148 }
6149 else if ( m_cursorMode == WXGRID_CURSOR_RESIZE_COL )
6150 {
6151 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL);
6152 DoEndDragResizeCol();
6153
6154 // Note: we are ending the event *after* doing
6155 // default processing in this case
6156 //
6157 SendEvent( wxEVT_GRID_COL_SIZE, -1, m_dragRowOrCol, event );
6158 }
6159
6160 m_dragLastPos = -1;
6161 }
6162
6163 // ------------ Right button down
6164 //
6165 else if ( event.RightDown() && coords != wxGridNoCellCoords )
6166 {
6167 DisableCellEditControl();
6168 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_CLICK,
6169 coords.GetRow(),
6170 coords.GetCol(),
6171 event ) )
6172 {
6173 // no default action at the moment
6174 }
6175 }
6176
6177 // ------------ Right double click
6178 //
6179 else if ( event.RightDClick() && coords != wxGridNoCellCoords )
6180 {
6181 DisableCellEditControl();
6182 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_DCLICK,
6183 coords.GetRow(),
6184 coords.GetCol(),
6185 event ) )
6186 {
6187 // no default action at the moment
6188 }
6189 }
6190
6191 // ------------ Moving and no button action
6192 //
6193 else if ( event.Moving() && !event.IsButton() )
6194 {
6195 if ( coords.GetRow() < 0 || coords.GetCol() < 0 )
6196 {
6197 // out of grid cell area
6198 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL);
6199 return;
6200 }
6201
6202 int dragRow = YToEdgeOfRow( y );
6203 int dragCol = XToEdgeOfCol( x );
6204
6205 // Dragging on the corner of a cell to resize in both
6206 // directions is not implemented yet...
6207 //
6208 if ( dragRow >= 0 && dragCol >= 0 )
6209 {
6210 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL);
6211 return;
6212 }
6213
6214 if ( dragRow >= 0 )
6215 {
6216 m_dragRowOrCol = dragRow;
6217
6218 if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
6219 {
6220 if ( CanDragRowSize() && CanDragGridSize() )
6221 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW, NULL, false);
6222 }
6223 }
6224 else if ( dragCol >= 0 )
6225 {
6226 m_dragRowOrCol = dragCol;
6227
6228 if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
6229 {
6230 if ( CanDragColSize() && CanDragGridSize() )
6231 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL, NULL, false);
6232 }
6233 }
6234 else // Neither on a row or col edge
6235 {
6236 if ( m_cursorMode != WXGRID_CURSOR_SELECT_CELL )
6237 {
6238 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL);
6239 }
6240 }
6241 }
6242 }
6243
6244 void wxGrid::DoEndDragResizeRow()
6245 {
6246 if ( m_dragLastPos >= 0 )
6247 {
6248 // erase the last line and resize the row
6249 //
6250 int cw, ch, left, dummy;
6251 m_gridWin->GetClientSize( &cw, &ch );
6252 CalcUnscrolledPosition( 0, 0, &left, &dummy );
6253
6254 wxClientDC dc( m_gridWin );
6255 PrepareDC( dc );
6256 dc.SetLogicalFunction( wxINVERT );
6257 dc.DrawLine( left, m_dragLastPos, left + cw, m_dragLastPos );
6258 HideCellEditControl();
6259 SaveEditControlValue();
6260
6261 int rowTop = GetRowTop(m_dragRowOrCol);
6262 SetRowSize( m_dragRowOrCol,
6263 wxMax( m_dragLastPos - rowTop, m_minAcceptableRowHeight ) );
6264
6265 if ( !GetBatchCount() )
6266 {
6267 // Only needed to get the correct rect.y:
6268 wxRect rect ( CellToRect( m_dragRowOrCol, 0 ) );
6269 rect.x = 0;
6270 CalcScrolledPosition(0, rect.y, &dummy, &rect.y);
6271 rect.width = m_rowLabelWidth;
6272 rect.height = ch - rect.y;
6273 m_rowLabelWin->Refresh( true, &rect );
6274 rect.width = cw;
6275
6276 // if there is a multicell block, paint all of it
6277 if (m_table)
6278 {
6279 int i, cell_rows, cell_cols, subtract_rows = 0;
6280 int leftCol = XToCol(left);
6281 int rightCol = internalXToCol(left + cw);
6282 if (leftCol >= 0)
6283 {
6284 for (i=leftCol; i<rightCol; i++)
6285 {
6286 GetCellSize(m_dragRowOrCol, i, &cell_rows, &cell_cols);
6287 if (cell_rows < subtract_rows)
6288 subtract_rows = cell_rows;
6289 }
6290 rect.y = GetRowTop(m_dragRowOrCol + subtract_rows);
6291 CalcScrolledPosition(0, rect.y, &dummy, &rect.y);
6292 rect.height = ch - rect.y;
6293 }
6294 }
6295 m_gridWin->Refresh( false, &rect );
6296 }
6297
6298 ShowCellEditControl();
6299 }
6300 }
6301
6302
6303 void wxGrid::DoEndDragResizeCol()
6304 {
6305 if ( m_dragLastPos >= 0 )
6306 {
6307 // erase the last line and resize the col
6308 //
6309 int cw, ch, dummy, top;
6310 m_gridWin->GetClientSize( &cw, &ch );
6311 CalcUnscrolledPosition( 0, 0, &dummy, &top );
6312
6313 wxClientDC dc( m_gridWin );
6314 PrepareDC( dc );
6315 dc.SetLogicalFunction( wxINVERT );
6316 dc.DrawLine( m_dragLastPos, top, m_dragLastPos, top + ch );
6317 HideCellEditControl();
6318 SaveEditControlValue();
6319
6320 int colLeft = GetColLeft(m_dragRowOrCol);
6321 SetColSize( m_dragRowOrCol,
6322 wxMax( m_dragLastPos - colLeft,
6323 GetColMinimalWidth(m_dragRowOrCol) ) );
6324
6325 if ( !GetBatchCount() )
6326 {
6327 // Only needed to get the correct rect.x:
6328 wxRect rect ( CellToRect( 0, m_dragRowOrCol ) );
6329 rect.y = 0;
6330 CalcScrolledPosition(rect.x, 0, &rect.x, &dummy);
6331 rect.width = cw - rect.x;
6332 rect.height = m_colLabelHeight;
6333 m_colLabelWin->Refresh( true, &rect );
6334 rect.height = ch;
6335
6336 // if there is a multicell block, paint all of it
6337 if (m_table)
6338 {
6339 int i, cell_rows, cell_cols, subtract_cols = 0;
6340 int topRow = YToRow(top);
6341 int bottomRow = internalYToRow(top + cw);
6342 if (topRow >= 0)
6343 {
6344 for (i=topRow; i<bottomRow; i++)
6345 {
6346 GetCellSize(i, m_dragRowOrCol, &cell_rows, &cell_cols);
6347 if (cell_cols < subtract_cols)
6348 subtract_cols = cell_cols;
6349 }
6350
6351 rect.x = GetColLeft(m_dragRowOrCol + subtract_cols);
6352 CalcScrolledPosition(rect.x, 0, &rect.x, &dummy);
6353 rect.width = cw - rect.x;
6354 }
6355 }
6356
6357 m_gridWin->Refresh( false, &rect );
6358 }
6359
6360 ShowCellEditControl();
6361 }
6362 }
6363
6364 void wxGrid::DoEndDragMoveCol()
6365 {
6366 //The user clicked on the column but didn't actually drag
6367 if ( m_dragLastPos < 0 )
6368 {
6369 m_colLabelWin->Refresh(); //Do this to "unpress" the column
6370 return;
6371 }
6372
6373 int newPos;
6374 if ( m_moveToCol == -1 )
6375 newPos = m_numCols - 1;
6376 else
6377 {
6378 newPos = GetColPos( m_moveToCol );
6379 if ( newPos > GetColPos( m_dragRowOrCol ) )
6380 newPos--;
6381 }
6382
6383 SetColPos( m_dragRowOrCol, newPos );
6384 }
6385
6386 void wxGrid::SetColPos( int colID, int newPos )
6387 {
6388 if ( m_colAt.IsEmpty() )
6389 {
6390 m_colAt.Alloc( m_numCols );
6391
6392 int i;
6393 for ( i = 0; i < m_numCols; i++ )
6394 {
6395 m_colAt.Add( i );
6396 }
6397 }
6398
6399 int oldPos = GetColPos( colID );
6400
6401 //Reshuffle the m_colAt array
6402 if ( newPos > oldPos )
6403 {
6404 int i;
6405 for ( i = oldPos; i < newPos; i++ )
6406 {
6407 m_colAt[i] = m_colAt[i+1];
6408 }
6409 }
6410 else
6411 {
6412 int i;
6413 for ( i = oldPos; i > newPos; i-- )
6414 {
6415 m_colAt[i] = m_colAt[i-1];
6416 }
6417 }
6418
6419 m_colAt[newPos] = colID;
6420
6421 //Recalculate the column rights
6422 if ( !m_colWidths.IsEmpty() )
6423 {
6424 int colRight = 0;
6425 int colPos;
6426 for ( colPos = 0; colPos < m_numCols; colPos++ )
6427 {
6428 int colID = GetColAt( colPos );
6429
6430 colRight += m_colWidths[colID];
6431 m_colRights[colID] = colRight;
6432 }
6433 }
6434
6435 m_colLabelWin->Refresh();
6436 m_gridWin->Refresh();
6437 }
6438
6439
6440
6441 void wxGrid::EnableDragColMove( bool enable )
6442 {
6443 if ( m_canDragColMove == enable )
6444 return;
6445
6446 m_canDragColMove = enable;
6447
6448 if ( !m_canDragColMove )
6449 {
6450 m_colAt.Clear();
6451
6452 //Recalculate the column rights
6453 if ( !m_colWidths.IsEmpty() )
6454 {
6455 int colRight = 0;
6456 int colPos;
6457 for ( colPos = 0; colPos < m_numCols; colPos++ )
6458 {
6459 colRight += m_colWidths[colPos];
6460 m_colRights[colPos] = colRight;
6461 }
6462 }
6463
6464 m_colLabelWin->Refresh();
6465 m_gridWin->Refresh();
6466 }
6467 }
6468
6469
6470 //
6471 // ------ interaction with data model
6472 //
6473 bool wxGrid::ProcessTableMessage( wxGridTableMessage& msg )
6474 {
6475 switch ( msg.GetId() )
6476 {
6477 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES:
6478 return GetModelValues();
6479
6480 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES:
6481 return SetModelValues();
6482
6483 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED:
6484 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED:
6485 case wxGRIDTABLE_NOTIFY_ROWS_DELETED:
6486 case wxGRIDTABLE_NOTIFY_COLS_INSERTED:
6487 case wxGRIDTABLE_NOTIFY_COLS_APPENDED:
6488 case wxGRIDTABLE_NOTIFY_COLS_DELETED:
6489 return Redimension( msg );
6490
6491 default:
6492 return false;
6493 }
6494 }
6495
6496 // The behaviour of this function depends on the grid table class
6497 // Clear() function. For the default wxGridStringTable class the
6498 // behavious is to replace all cell contents with wxEmptyString but
6499 // not to change the number of rows or cols.
6500 //
6501 void wxGrid::ClearGrid()
6502 {
6503 if ( m_table )
6504 {
6505 if (IsCellEditControlEnabled())
6506 DisableCellEditControl();
6507
6508 m_table->Clear();
6509 if (!GetBatchCount())
6510 m_gridWin->Refresh();
6511 }
6512 }
6513
6514 bool wxGrid::InsertRows( int pos, int numRows, bool WXUNUSED(updateLabels) )
6515 {
6516 if ( !m_created )
6517 {
6518 wxFAIL_MSG( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") );
6519 return false;
6520 }
6521
6522 if ( m_table )
6523 {
6524 if (IsCellEditControlEnabled())
6525 DisableCellEditControl();
6526
6527 bool done = m_table->InsertRows( pos, numRows );
6528 return done;
6529
6530 // the table will have sent the results of the insert row
6531 // operation to this view object as a grid table message
6532 }
6533
6534 return false;
6535 }
6536
6537 bool wxGrid::AppendRows( int numRows, bool WXUNUSED(updateLabels) )
6538 {
6539 if ( !m_created )
6540 {
6541 wxFAIL_MSG( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") );
6542 return false;
6543 }
6544
6545 if ( m_table )
6546 {
6547 bool done = m_table && m_table->AppendRows( numRows );
6548 return done;
6549
6550 // the table will have sent the results of the append row
6551 // operation to this view object as a grid table message
6552 }
6553
6554 return false;
6555 }
6556
6557 bool wxGrid::DeleteRows( int pos, int numRows, bool WXUNUSED(updateLabels) )
6558 {
6559 if ( !m_created )
6560 {
6561 wxFAIL_MSG( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") );
6562 return false;
6563 }
6564
6565 if ( m_table )
6566 {
6567 if (IsCellEditControlEnabled())
6568 DisableCellEditControl();
6569
6570 bool done = m_table->DeleteRows( pos, numRows );
6571 return done;
6572 // the table will have sent the results of the delete row
6573 // operation to this view object as a grid table message
6574 }
6575
6576 return false;
6577 }
6578
6579 bool wxGrid::InsertCols( int pos, int numCols, bool WXUNUSED(updateLabels) )
6580 {
6581 if ( !m_created )
6582 {
6583 wxFAIL_MSG( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") );
6584 return false;
6585 }
6586
6587 if ( m_table )
6588 {
6589 if (IsCellEditControlEnabled())
6590 DisableCellEditControl();
6591
6592 bool done = m_table->InsertCols( pos, numCols );
6593 return done;
6594 // the table will have sent the results of the insert col
6595 // operation to this view object as a grid table message
6596 }
6597
6598 return false;
6599 }
6600
6601 bool wxGrid::AppendCols( int numCols, bool WXUNUSED(updateLabels) )
6602 {
6603 if ( !m_created )
6604 {
6605 wxFAIL_MSG( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") );
6606 return false;
6607 }
6608
6609 if ( m_table )
6610 {
6611 bool done = m_table->AppendCols( numCols );
6612 return done;
6613 // the table will have sent the results of the append col
6614 // operation to this view object as a grid table message
6615 }
6616
6617 return false;
6618 }
6619
6620 bool wxGrid::DeleteCols( int pos, int numCols, bool WXUNUSED(updateLabels) )
6621 {
6622 if ( !m_created )
6623 {
6624 wxFAIL_MSG( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") );
6625 return false;
6626 }
6627
6628 if ( m_table )
6629 {
6630 if (IsCellEditControlEnabled())
6631 DisableCellEditControl();
6632
6633 bool done = m_table->DeleteCols( pos, numCols );
6634 return done;
6635 // the table will have sent the results of the delete col
6636 // operation to this view object as a grid table message
6637 }
6638
6639 return false;
6640 }
6641
6642 //
6643 // ----- event handlers
6644 //
6645
6646 // Generate a grid event based on a mouse event and
6647 // return the result of ProcessEvent()
6648 //
6649 int wxGrid::SendEvent( const wxEventType type,
6650 int row, int col,
6651 wxMouseEvent& mouseEv )
6652 {
6653 bool claimed, vetoed;
6654
6655 if ( type == wxEVT_GRID_ROW_SIZE || type == wxEVT_GRID_COL_SIZE )
6656 {
6657 int rowOrCol = (row == -1 ? col : row);
6658
6659 wxGridSizeEvent gridEvt( GetId(),
6660 type,
6661 this,
6662 rowOrCol,
6663 mouseEv.GetX() + GetRowLabelSize(),
6664 mouseEv.GetY() + GetColLabelSize(),
6665 mouseEv.ControlDown(),
6666 mouseEv.ShiftDown(),
6667 mouseEv.AltDown(),
6668 mouseEv.MetaDown() );
6669
6670 claimed = GetEventHandler()->ProcessEvent(gridEvt);
6671 vetoed = !gridEvt.IsAllowed();
6672 }
6673 else if ( type == wxEVT_GRID_RANGE_SELECT )
6674 {
6675 // Right now, it should _never_ end up here!
6676 wxGridRangeSelectEvent gridEvt( GetId(),
6677 type,
6678 this,
6679 m_selectingTopLeft,
6680 m_selectingBottomRight,
6681 true,
6682 mouseEv.ControlDown(),
6683 mouseEv.ShiftDown(),
6684 mouseEv.AltDown(),
6685 mouseEv.MetaDown() );
6686
6687 claimed = GetEventHandler()->ProcessEvent(gridEvt);
6688 vetoed = !gridEvt.IsAllowed();
6689 }
6690 else if ( type == wxEVT_GRID_LABEL_LEFT_CLICK ||
6691 type == wxEVT_GRID_LABEL_LEFT_DCLICK ||
6692 type == wxEVT_GRID_LABEL_RIGHT_CLICK ||
6693 type == wxEVT_GRID_LABEL_RIGHT_DCLICK )
6694 {
6695 wxPoint pos = mouseEv.GetPosition();
6696
6697 if ( mouseEv.GetEventObject() == GetGridRowLabelWindow() )
6698 pos.y += GetColLabelSize();
6699 if ( mouseEv.GetEventObject() == GetGridColLabelWindow() )
6700 pos.x += GetRowLabelSize();
6701
6702 wxGridEvent gridEvt( GetId(),
6703 type,
6704 this,
6705 row, col,
6706 pos.x,
6707 pos.y,
6708 false,
6709 mouseEv.ControlDown(),
6710 mouseEv.ShiftDown(),
6711 mouseEv.AltDown(),
6712 mouseEv.MetaDown() );
6713 claimed = GetEventHandler()->ProcessEvent(gridEvt);
6714 vetoed = !gridEvt.IsAllowed();
6715 }
6716 else
6717 {
6718 wxGridEvent gridEvt( GetId(),
6719 type,
6720 this,
6721 row, col,
6722 mouseEv.GetX() + GetRowLabelSize(),
6723 mouseEv.GetY() + GetColLabelSize(),
6724 false,
6725 mouseEv.ControlDown(),
6726 mouseEv.ShiftDown(),
6727 mouseEv.AltDown(),
6728 mouseEv.MetaDown() );
6729 claimed = GetEventHandler()->ProcessEvent(gridEvt);
6730 vetoed = !gridEvt.IsAllowed();
6731 }
6732
6733 // A Veto'd event may not be `claimed' so test this first
6734 if (vetoed)
6735 return -1;
6736
6737 return claimed ? 1 : 0;
6738 }
6739
6740 // Generate a grid event of specified type and return the result
6741 // of ProcessEvent().
6742 //
6743 int wxGrid::SendEvent( const wxEventType type,
6744 int row, int col )
6745 {
6746 bool claimed, vetoed;
6747
6748 if ( type == wxEVT_GRID_ROW_SIZE || type == wxEVT_GRID_COL_SIZE )
6749 {
6750 int rowOrCol = (row == -1 ? col : row);
6751
6752 wxGridSizeEvent gridEvt( GetId(), type, this, rowOrCol );
6753
6754 claimed = GetEventHandler()->ProcessEvent(gridEvt);
6755 vetoed = !gridEvt.IsAllowed();
6756 }
6757 else
6758 {
6759 wxGridEvent gridEvt( GetId(), type, this, row, col );
6760
6761 claimed = GetEventHandler()->ProcessEvent(gridEvt);
6762 vetoed = !gridEvt.IsAllowed();
6763 }
6764
6765 // A Veto'd event may not be `claimed' so test this first
6766 if (vetoed)
6767 return -1;
6768
6769 return claimed ? 1 : 0;
6770 }
6771
6772 void wxGrid::OnPaint( wxPaintEvent& WXUNUSED(event) )
6773 {
6774 // needed to prevent zillions of paint events on MSW
6775 wxPaintDC dc(this);
6776 }
6777
6778 void wxGrid::Refresh(bool eraseb, const wxRect* rect)
6779 {
6780 // Don't do anything if between Begin/EndBatch...
6781 // EndBatch() will do all this on the last nested one anyway.
6782 if ( m_created && !GetBatchCount() )
6783 {
6784 // Refresh to get correct scrolled position:
6785 wxScrolledWindow::Refresh(eraseb, rect);
6786
6787 if (rect)
6788 {
6789 int rect_x, rect_y, rectWidth, rectHeight;
6790 int width_label, width_cell, height_label, height_cell;
6791 int x, y;
6792
6793 // Copy rectangle can get scroll offsets..
6794 rect_x = rect->GetX();
6795 rect_y = rect->GetY();
6796 rectWidth = rect->GetWidth();
6797 rectHeight = rect->GetHeight();
6798
6799 width_label = m_rowLabelWidth - rect_x;
6800 if (width_label > rectWidth)
6801 width_label = rectWidth;
6802
6803 height_label = m_colLabelHeight - rect_y;
6804 if (height_label > rectHeight)
6805 height_label = rectHeight;
6806
6807 if (rect_x > m_rowLabelWidth)
6808 {
6809 x = rect_x - m_rowLabelWidth;
6810 width_cell = rectWidth;
6811 }
6812 else
6813 {
6814 x = 0;
6815 width_cell = rectWidth - (m_rowLabelWidth - rect_x);
6816 }
6817
6818 if (rect_y > m_colLabelHeight)
6819 {
6820 y = rect_y - m_colLabelHeight;
6821 height_cell = rectHeight;
6822 }
6823 else
6824 {
6825 y = 0;
6826 height_cell = rectHeight - (m_colLabelHeight - rect_y);
6827 }
6828
6829 // Paint corner label part intersecting rect.
6830 if ( width_label > 0 && height_label > 0 )
6831 {
6832 wxRect anotherrect(rect_x, rect_y, width_label, height_label);
6833 m_cornerLabelWin->Refresh(eraseb, &anotherrect);
6834 }
6835
6836 // Paint col labels part intersecting rect.
6837 if ( width_cell > 0 && height_label > 0 )
6838 {
6839 wxRect anotherrect(x, rect_y, width_cell, height_label);
6840 m_colLabelWin->Refresh(eraseb, &anotherrect);
6841 }
6842
6843 // Paint row labels part intersecting rect.
6844 if ( width_label > 0 && height_cell > 0 )
6845 {
6846 wxRect anotherrect(rect_x, y, width_label, height_cell);
6847 m_rowLabelWin->Refresh(eraseb, &anotherrect);
6848 }
6849
6850 // Paint cell area part intersecting rect.
6851 if ( width_cell > 0 && height_cell > 0 )
6852 {
6853 wxRect anotherrect(x, y, width_cell, height_cell);
6854 m_gridWin->Refresh(eraseb, &anotherrect);
6855 }
6856 }
6857 else
6858 {
6859 m_cornerLabelWin->Refresh(eraseb, NULL);
6860 m_colLabelWin->Refresh(eraseb, NULL);
6861 m_rowLabelWin->Refresh(eraseb, NULL);
6862 m_gridWin->Refresh(eraseb, NULL);
6863 }
6864 }
6865 }
6866
6867 void wxGrid::OnSize(wxSizeEvent& WXUNUSED(event))
6868 {
6869 if (m_targetWindow != this) // check whether initialisation has been done
6870 {
6871 // reposition our children windows
6872 CalcWindowSizes();
6873 }
6874 }
6875
6876 void wxGrid::OnKeyDown( wxKeyEvent& event )
6877 {
6878 if ( m_inOnKeyDown )
6879 {
6880 // shouldn't be here - we are going round in circles...
6881 //
6882 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") );
6883 }
6884
6885 m_inOnKeyDown = true;
6886
6887 // propagate the event up and see if it gets processed
6888 wxWindow *parent = GetParent();
6889 wxKeyEvent keyEvt( event );
6890 keyEvt.SetEventObject( parent );
6891
6892 if ( !parent->GetEventHandler()->ProcessEvent( keyEvt ) )
6893 {
6894 if (GetLayoutDirection() == wxLayout_RightToLeft)
6895 {
6896 if (event.GetKeyCode() == WXK_RIGHT)
6897 event.m_keyCode = WXK_LEFT;
6898 else if (event.GetKeyCode() == WXK_LEFT)
6899 event.m_keyCode = WXK_RIGHT;
6900 }
6901
6902 // try local handlers
6903 switch ( event.GetKeyCode() )
6904 {
6905 case WXK_UP:
6906 if ( event.ControlDown() )
6907 MoveCursorUpBlock( event.ShiftDown() );
6908 else
6909 MoveCursorUp( event.ShiftDown() );
6910 break;
6911
6912 case WXK_DOWN:
6913 if ( event.ControlDown() )
6914 MoveCursorDownBlock( event.ShiftDown() );
6915 else
6916 MoveCursorDown( event.ShiftDown() );
6917 break;
6918
6919 case WXK_LEFT:
6920 if ( event.ControlDown() )
6921 MoveCursorLeftBlock( event.ShiftDown() );
6922 else
6923 MoveCursorLeft( event.ShiftDown() );
6924 break;
6925
6926 case WXK_RIGHT:
6927 if ( event.ControlDown() )
6928 MoveCursorRightBlock( event.ShiftDown() );
6929 else
6930 MoveCursorRight( event.ShiftDown() );
6931 break;
6932
6933 case WXK_RETURN:
6934 case WXK_NUMPAD_ENTER:
6935 if ( event.ControlDown() )
6936 {
6937 event.Skip(); // to let the edit control have the return
6938 }
6939 else
6940 {
6941 if ( GetGridCursorRow() < GetNumberRows()-1 )
6942 {
6943 MoveCursorDown( event.ShiftDown() );
6944 }
6945 else
6946 {
6947 // at the bottom of a column
6948 DisableCellEditControl();
6949 }
6950 }
6951 break;
6952
6953 case WXK_ESCAPE:
6954 ClearSelection();
6955 break;
6956
6957 case WXK_TAB:
6958 if (event.ShiftDown())
6959 {
6960 if ( GetGridCursorCol() > 0 )
6961 {
6962 MoveCursorLeft( false );
6963 }
6964 else
6965 {
6966 // at left of grid
6967 DisableCellEditControl();
6968 }
6969 }
6970 else
6971 {
6972 if ( GetGridCursorCol() < GetNumberCols() - 1 )
6973 {
6974 MoveCursorRight( false );
6975 }
6976 else
6977 {
6978 // at right of grid
6979 DisableCellEditControl();
6980 }
6981 }
6982 break;
6983
6984 case WXK_HOME:
6985 if ( event.ControlDown() )
6986 {
6987 MakeCellVisible( 0, 0 );
6988 SetCurrentCell( 0, 0 );
6989 }
6990 else
6991 {
6992 event.Skip();
6993 }
6994 break;
6995
6996 case WXK_END:
6997 if ( event.ControlDown() )
6998 {
6999 MakeCellVisible( m_numRows - 1, m_numCols - 1 );
7000 SetCurrentCell( m_numRows - 1, m_numCols - 1 );
7001 }
7002 else
7003 {
7004 event.Skip();
7005 }
7006 break;
7007
7008 case WXK_PAGEUP:
7009 MovePageUp();
7010 break;
7011
7012 case WXK_PAGEDOWN:
7013 MovePageDown();
7014 break;
7015
7016 case WXK_SPACE:
7017 // Ctrl-Space selects the current column, Shift-Space -- the
7018 // current row and Ctrl-Shift-Space -- everything
7019 switch ( m_selection ? event.GetModifiers() : wxMOD_NONE )
7020 {
7021 case wxMOD_CONTROL:
7022 m_selection->SelectCol(m_currentCellCoords.GetCol());
7023 break;
7024
7025 case wxMOD_SHIFT:
7026 m_selection->SelectRow(m_currentCellCoords.GetRow());
7027 break;
7028
7029 case wxMOD_CONTROL | wxMOD_SHIFT:
7030 m_selection->SelectBlock(0, 0,
7031 m_numRows - 1, m_numCols - 1);
7032 break;
7033
7034 case wxMOD_NONE:
7035 if ( !IsEditable() )
7036 {
7037 MoveCursorRight(false);
7038 break;
7039 }
7040 //else: fall through
7041
7042 default:
7043 event.Skip();
7044 }
7045 break;
7046
7047 default:
7048 event.Skip();
7049 break;
7050 }
7051 }
7052
7053 m_inOnKeyDown = false;
7054 }
7055
7056 void wxGrid::OnKeyUp( wxKeyEvent& event )
7057 {
7058 // try local handlers
7059 //
7060 if ( event.GetKeyCode() == WXK_SHIFT )
7061 {
7062 if ( m_selectingTopLeft != wxGridNoCellCoords &&
7063 m_selectingBottomRight != wxGridNoCellCoords )
7064 {
7065 if ( m_selection )
7066 {
7067 m_selection->SelectBlock(
7068 m_selectingTopLeft.GetRow(),
7069 m_selectingTopLeft.GetCol(),
7070 m_selectingBottomRight.GetRow(),
7071 m_selectingBottomRight.GetCol(),
7072 event.ControlDown(),
7073 true,
7074 event.AltDown(),
7075 event.MetaDown() );
7076 }
7077 }
7078
7079 m_selectingTopLeft = wxGridNoCellCoords;
7080 m_selectingBottomRight = wxGridNoCellCoords;
7081 m_selectingKeyboard = wxGridNoCellCoords;
7082 }
7083 }
7084
7085 void wxGrid::OnChar( wxKeyEvent& event )
7086 {
7087 // is it possible to edit the current cell at all?
7088 if ( !IsCellEditControlEnabled() && CanEnableCellControl() )
7089 {
7090 // yes, now check whether the cells editor accepts the key
7091 int row = m_currentCellCoords.GetRow();
7092 int col = m_currentCellCoords.GetCol();
7093 wxGridCellAttr *attr = GetCellAttr(row, col);
7094 wxGridCellEditor *editor = attr->GetEditor(this, row, col);
7095
7096 // <F2> is special and will always start editing, for
7097 // other keys - ask the editor itself
7098 if ( (event.GetKeyCode() == WXK_F2 && !event.HasModifiers())
7099 || editor->IsAcceptedKey(event) )
7100 {
7101 // ensure cell is visble
7102 MakeCellVisible(row, col);
7103 EnableCellEditControl();
7104
7105 // a problem can arise if the cell is not completely
7106 // visible (even after calling MakeCellVisible the
7107 // control is not created and calling StartingKey will
7108 // crash the app
7109 if ( event.GetKeyCode() != WXK_F2 && editor->IsCreated() && m_cellEditCtrlEnabled )
7110 editor->StartingKey(event);
7111 }
7112 else
7113 {
7114 event.Skip();
7115 }
7116
7117 editor->DecRef();
7118 attr->DecRef();
7119 }
7120 else
7121 {
7122 event.Skip();
7123 }
7124 }
7125
7126 void wxGrid::OnEraseBackground(wxEraseEvent&)
7127 {
7128 }
7129
7130 void wxGrid::SetCurrentCell( const wxGridCellCoords& coords )
7131 {
7132 if ( SendEvent( wxEVT_GRID_SELECT_CELL, coords.GetRow(), coords.GetCol() ) )
7133 {
7134 // the event has been intercepted - do nothing
7135 return;
7136 }
7137
7138 #if !defined(__WXMAC__)
7139 wxClientDC dc( m_gridWin );
7140 PrepareDC( dc );
7141 #endif
7142
7143 if ( m_currentCellCoords != wxGridNoCellCoords )
7144 {
7145 DisableCellEditControl();
7146
7147 if ( IsVisible( m_currentCellCoords, false ) )
7148 {
7149 wxRect r;
7150 r = BlockToDeviceRect( m_currentCellCoords, m_currentCellCoords );
7151 if ( !m_gridLinesEnabled )
7152 {
7153 r.x--;
7154 r.y--;
7155 r.width++;
7156 r.height++;
7157 }
7158
7159 wxGridCellCoordsArray cells = CalcCellsExposed( r );
7160
7161 // Otherwise refresh redraws the highlight!
7162 m_currentCellCoords = coords;
7163
7164 #if defined(__WXMAC__)
7165 m_gridWin->Refresh(true /*, & r */);
7166 #else
7167 DrawGridCellArea( dc, cells );
7168 DrawAllGridLines( dc, r );
7169 #endif
7170 }
7171 }
7172
7173 m_currentCellCoords = coords;
7174
7175 wxGridCellAttr *attr = GetCellAttr( coords );
7176 #if !defined(__WXMAC__)
7177 DrawCellHighlight( dc, attr );
7178 #endif
7179 attr->DecRef();
7180 }
7181
7182 void wxGrid::HighlightBlock( int topRow, int leftCol, int bottomRow, int rightCol )
7183 {
7184 int temp;
7185 wxGridCellCoords updateTopLeft, updateBottomRight;
7186
7187 if ( m_selection )
7188 {
7189 if ( m_selection->GetSelectionMode() == wxGrid::wxGridSelectRows )
7190 {
7191 leftCol = 0;
7192 rightCol = GetNumberCols() - 1;
7193 }
7194 else if ( m_selection->GetSelectionMode() == wxGrid::wxGridSelectColumns )
7195 {
7196 topRow = 0;
7197 bottomRow = GetNumberRows() - 1;
7198 }
7199 }
7200
7201 if ( topRow > bottomRow )
7202 {
7203 temp = topRow;
7204 topRow = bottomRow;
7205 bottomRow = temp;
7206 }
7207
7208 if ( leftCol > rightCol )
7209 {
7210 temp = leftCol;
7211 leftCol = rightCol;
7212 rightCol = temp;
7213 }
7214
7215 updateTopLeft = wxGridCellCoords( topRow, leftCol );
7216 updateBottomRight = wxGridCellCoords( bottomRow, rightCol );
7217
7218 // First the case that we selected a completely new area
7219 if ( m_selectingTopLeft == wxGridNoCellCoords ||
7220 m_selectingBottomRight == wxGridNoCellCoords )
7221 {
7222 wxRect rect;
7223 rect = BlockToDeviceRect( wxGridCellCoords ( topRow, leftCol ),
7224 wxGridCellCoords ( bottomRow, rightCol ) );
7225 m_gridWin->Refresh( false, &rect );
7226 }
7227
7228 // Now handle changing an existing selection area.
7229 else if ( m_selectingTopLeft != updateTopLeft ||
7230 m_selectingBottomRight != updateBottomRight )
7231 {
7232 // Compute two optimal update rectangles:
7233 // Either one rectangle is a real subset of the
7234 // other, or they are (almost) disjoint!
7235 wxRect rect[4];
7236 bool need_refresh[4];
7237 need_refresh[0] =
7238 need_refresh[1] =
7239 need_refresh[2] =
7240 need_refresh[3] = false;
7241 int i;
7242
7243 // Store intermediate values
7244 wxCoord oldLeft = m_selectingTopLeft.GetCol();
7245 wxCoord oldTop = m_selectingTopLeft.GetRow();
7246 wxCoord oldRight = m_selectingBottomRight.GetCol();
7247 wxCoord oldBottom = m_selectingBottomRight.GetRow();
7248
7249 // Determine the outer/inner coordinates.
7250 if (oldLeft > leftCol)
7251 {
7252 temp = oldLeft;
7253 oldLeft = leftCol;
7254 leftCol = temp;
7255 }
7256 if (oldTop > topRow )
7257 {
7258 temp = oldTop;
7259 oldTop = topRow;
7260 topRow = temp;
7261 }
7262 if (oldRight < rightCol )
7263 {
7264 temp = oldRight;
7265 oldRight = rightCol;
7266 rightCol = temp;
7267 }
7268 if (oldBottom < bottomRow)
7269 {
7270 temp = oldBottom;
7271 oldBottom = bottomRow;
7272 bottomRow = temp;
7273 }
7274
7275 // Now, either the stuff marked old is the outer
7276 // rectangle or we don't have a situation where one
7277 // is contained in the other.
7278
7279 if ( oldLeft < leftCol )
7280 {
7281 // Refresh the newly selected or deselected
7282 // area to the left of the old or new selection.
7283 need_refresh[0] = true;
7284 rect[0] = BlockToDeviceRect(
7285 wxGridCellCoords( oldTop, oldLeft ),
7286 wxGridCellCoords( oldBottom, leftCol - 1 ) );
7287 }
7288
7289 if ( oldTop < topRow )
7290 {
7291 // Refresh the newly selected or deselected
7292 // area above the old or new selection.
7293 need_refresh[1] = true;
7294 rect[1] = BlockToDeviceRect(
7295 wxGridCellCoords( oldTop, leftCol ),
7296 wxGridCellCoords( topRow - 1, rightCol ) );
7297 }
7298
7299 if ( oldRight > rightCol )
7300 {
7301 // Refresh the newly selected or deselected
7302 // area to the right of the old or new selection.
7303 need_refresh[2] = true;
7304 rect[2] = BlockToDeviceRect(
7305 wxGridCellCoords( oldTop, rightCol + 1 ),
7306 wxGridCellCoords( oldBottom, oldRight ) );
7307 }
7308
7309 if ( oldBottom > bottomRow )
7310 {
7311 // Refresh the newly selected or deselected
7312 // area below the old or new selection.
7313 need_refresh[3] = true;
7314 rect[3] = BlockToDeviceRect(
7315 wxGridCellCoords( bottomRow + 1, leftCol ),
7316 wxGridCellCoords( oldBottom, rightCol ) );
7317 }
7318
7319 // various Refresh() calls
7320 for (i = 0; i < 4; i++ )
7321 if ( need_refresh[i] && rect[i] != wxGridNoCellRect )
7322 m_gridWin->Refresh( false, &(rect[i]) );
7323 }
7324
7325 // change selection
7326 m_selectingTopLeft = updateTopLeft;
7327 m_selectingBottomRight = updateBottomRight;
7328 }
7329
7330 //
7331 // ------ functions to get/send data (see also public functions)
7332 //
7333
7334 bool wxGrid::GetModelValues()
7335 {
7336 // Hide the editor, so it won't hide a changed value.
7337 HideCellEditControl();
7338
7339 if ( m_table )
7340 {
7341 // all we need to do is repaint the grid
7342 //
7343 m_gridWin->Refresh();
7344 return true;
7345 }
7346
7347 return false;
7348 }
7349
7350 bool wxGrid::SetModelValues()
7351 {
7352 int row, col;
7353
7354 // Disable the editor, so it won't hide a changed value.
7355 // Do we also want to save the current value of the editor first?
7356 // I think so ...
7357 DisableCellEditControl();
7358
7359 if ( m_table )
7360 {
7361 for ( row = 0; row < m_numRows; row++ )
7362 {
7363 for ( col = 0; col < m_numCols; col++ )
7364 {
7365 m_table->SetValue( row, col, GetCellValue(row, col) );
7366 }
7367 }
7368
7369 return true;
7370 }
7371
7372 return false;
7373 }
7374
7375 // Note - this function only draws cells that are in the list of
7376 // exposed cells (usually set from the update region by
7377 // CalcExposedCells)
7378 //
7379 void wxGrid::DrawGridCellArea( wxDC& dc, const wxGridCellCoordsArray& cells )
7380 {
7381 if ( !m_numRows || !m_numCols )
7382 return;
7383
7384 int i, numCells = cells.GetCount();
7385 int row, col, cell_rows, cell_cols;
7386 wxGridCellCoordsArray redrawCells;
7387
7388 for ( i = numCells - 1; i >= 0; i-- )
7389 {
7390 row = cells[i].GetRow();
7391 col = cells[i].GetCol();
7392 GetCellSize( row, col, &cell_rows, &cell_cols );
7393
7394 // If this cell is part of a multicell block, find owner for repaint
7395 if ( cell_rows <= 0 || cell_cols <= 0 )
7396 {
7397 wxGridCellCoords cell( row + cell_rows, col + cell_cols );
7398 bool marked = false;
7399 for ( int j = 0; j < numCells; j++ )
7400 {
7401 if ( cell == cells[j] )
7402 {
7403 marked = true;
7404 break;
7405 }
7406 }
7407
7408 if (!marked)
7409 {
7410 int count = redrawCells.GetCount();
7411 for (int j = 0; j < count; j++)
7412 {
7413 if ( cell == redrawCells[j] )
7414 {
7415 marked = true;
7416 break;
7417 }
7418 }
7419
7420 if (!marked)
7421 redrawCells.Add( cell );
7422 }
7423
7424 // don't bother drawing this cell
7425 continue;
7426 }
7427
7428 // If this cell is empty, find cell to left that might want to overflow
7429 if (m_table && m_table->IsEmptyCell(row, col))
7430 {
7431 for ( int l = 0; l < cell_rows; l++ )
7432 {
7433 // find a cell in this row to leave already marked for repaint
7434 int left = col;
7435 for (int k = 0; k < int(redrawCells.GetCount()); k++)
7436 if ((redrawCells[k].GetCol() < left) &&
7437 (redrawCells[k].GetRow() == row))
7438 {
7439 left = redrawCells[k].GetCol();
7440 }
7441
7442 if (left == col)
7443 left = 0; // oh well
7444
7445 for (int j = col - 1; j >= left; j--)
7446 {
7447 if (!m_table->IsEmptyCell(row + l, j))
7448 {
7449 if (GetCellOverflow(row + l, j))
7450 {
7451 wxGridCellCoords cell(row + l, j);
7452 bool marked = false;
7453
7454 for (int k = 0; k < numCells; k++)
7455 {
7456 if ( cell == cells[k] )
7457 {
7458 marked = true;
7459 break;
7460 }
7461 }
7462
7463 if (!marked)
7464 {
7465 int count = redrawCells.GetCount();
7466 for (int k = 0; k < count; k++)
7467 {
7468 if ( cell == redrawCells[k] )
7469 {
7470 marked = true;
7471 break;
7472 }
7473 }
7474 if (!marked)
7475 redrawCells.Add( cell );
7476 }
7477 }
7478 break;
7479 }
7480 }
7481 }
7482 }
7483
7484 DrawCell( dc, cells[i] );
7485 }
7486
7487 numCells = redrawCells.GetCount();
7488
7489 for ( i = numCells - 1; i >= 0; i-- )
7490 {
7491 DrawCell( dc, redrawCells[i] );
7492 }
7493 }
7494
7495 void wxGrid::DrawGridSpace( wxDC& dc )
7496 {
7497 int cw, ch;
7498 m_gridWin->GetClientSize( &cw, &ch );
7499
7500 int right, bottom;
7501 CalcUnscrolledPosition( cw, ch, &right, &bottom );
7502
7503 int rightCol = m_numCols > 0 ? GetColRight(GetColAt( m_numCols - 1 )) : 0;
7504 int bottomRow = m_numRows > 0 ? GetRowBottom(m_numRows - 1) : 0;
7505
7506 if ( right > rightCol || bottom > bottomRow )
7507 {
7508 int left, top;
7509 CalcUnscrolledPosition( 0, 0, &left, &top );
7510
7511 dc.SetBrush(GetDefaultCellBackgroundColour());
7512 dc.SetPen( *wxTRANSPARENT_PEN );
7513
7514 if ( right > rightCol )
7515 {
7516 dc.DrawRectangle( rightCol, top, right - rightCol, ch );
7517 }
7518
7519 if ( bottom > bottomRow )
7520 {
7521 dc.DrawRectangle( left, bottomRow, cw, bottom - bottomRow );
7522 }
7523 }
7524 }
7525
7526 void wxGrid::DrawCell( wxDC& dc, const wxGridCellCoords& coords )
7527 {
7528 int row = coords.GetRow();
7529 int col = coords.GetCol();
7530
7531 if ( GetColWidth(col) <= 0 || GetRowHeight(row) <= 0 )
7532 return;
7533
7534 // we draw the cell border ourselves
7535 wxGridCellAttr* attr = GetCellAttr(row, col);
7536
7537 bool isCurrent = coords == m_currentCellCoords;
7538
7539 wxRect rect = CellToRect( row, col );
7540
7541 // if the editor is shown, we should use it and not the renderer
7542 // Note: However, only if it is really _shown_, i.e. not hidden!
7543 if ( isCurrent && IsCellEditControlShown() )
7544 {
7545 // NB: this "#if..." is temporary and fixes a problem where the
7546 // edit control is erased by this code after being rendered.
7547 // On wxMac (QD build only), the cell editor is a wxTextCntl and is rendered
7548 // implicitly, causing this out-of order render.
7549 #if !defined(__WXMAC__)
7550 wxGridCellEditor *editor = attr->GetEditor(this, row, col);
7551 editor->PaintBackground(rect, attr);
7552 editor->DecRef();
7553 #endif
7554 }
7555 else
7556 {
7557 // but all the rest is drawn by the cell renderer and hence may be customized
7558 wxGridCellRenderer *renderer = attr->GetRenderer(this, row, col);
7559 renderer->Draw(*this, *attr, dc, rect, row, col, IsInSelection(coords));
7560 renderer->DecRef();
7561 }
7562
7563 attr->DecRef();
7564 }
7565
7566 void wxGrid::DrawCellHighlight( wxDC& dc, const wxGridCellAttr *attr )
7567 {
7568 // don't show highlight when the grid doesn't have focus
7569 if ( !HasFocus() )
7570 return;
7571
7572 int row = m_currentCellCoords.GetRow();
7573 int col = m_currentCellCoords.GetCol();
7574
7575 if ( GetColWidth(col) <= 0 || GetRowHeight(row) <= 0 )
7576 return;
7577
7578 wxRect rect = CellToRect(row, col);
7579
7580 // hmmm... what could we do here to show that the cell is disabled?
7581 // for now, I just draw a thinner border than for the other ones, but
7582 // it doesn't look really good
7583
7584 int penWidth = attr->IsReadOnly() ? m_cellHighlightROPenWidth : m_cellHighlightPenWidth;
7585
7586 if (penWidth > 0)
7587 {
7588 // The center of the drawn line is where the position/width/height of
7589 // the rectangle is actually at (on wxMSW at least), so the
7590 // size of the rectangle is reduced to compensate for the thickness of
7591 // the line. If this is too strange on non-wxMSW platforms then
7592 // please #ifdef this appropriately.
7593 rect.x += penWidth / 2;
7594 rect.y += penWidth / 2;
7595 rect.width -= penWidth - 1;
7596 rect.height -= penWidth - 1;
7597
7598 // Now draw the rectangle
7599 // use the cellHighlightColour if the cell is inside a selection, this
7600 // will ensure the cell is always visible.
7601 dc.SetPen(wxPen(IsInSelection(row,col) ? m_selectionForeground
7602 : m_cellHighlightColour,
7603 penWidth));
7604 dc.SetBrush(*wxTRANSPARENT_BRUSH);
7605 dc.DrawRectangle(rect);
7606 }
7607
7608 #if 0
7609 // VZ: my experiments with 3D borders...
7610
7611 // how to properly set colours for arbitrary bg?
7612 wxCoord x1 = rect.x,
7613 y1 = rect.y,
7614 x2 = rect.x + rect.width - 1,
7615 y2 = rect.y + rect.height - 1;
7616
7617 dc.SetPen(*wxWHITE_PEN);
7618 dc.DrawLine(x1, y1, x2, y1);
7619 dc.DrawLine(x1, y1, x1, y2);
7620
7621 dc.DrawLine(x1 + 1, y2 - 1, x2 - 1, y2 - 1);
7622 dc.DrawLine(x2 - 1, y1 + 1, x2 - 1, y2);
7623
7624 dc.SetPen(*wxBLACK_PEN);
7625 dc.DrawLine(x1, y2, x2, y2);
7626 dc.DrawLine(x2, y1, x2, y2 + 1);
7627 #endif
7628 }
7629
7630 wxPen wxGrid::GetDefaultGridLinePen()
7631 {
7632 return wxPen(GetGridLineColour());
7633 }
7634
7635 wxPen wxGrid::GetRowGridLinePen(int WXUNUSED(row))
7636 {
7637 return GetDefaultGridLinePen();
7638 }
7639
7640 wxPen wxGrid::GetColGridLinePen(int WXUNUSED(col))
7641 {
7642 return GetDefaultGridLinePen();
7643 }
7644
7645 void wxGrid::DrawCellBorder( wxDC& dc, const wxGridCellCoords& coords )
7646 {
7647 int row = coords.GetRow();
7648 int col = coords.GetCol();
7649 if ( GetColWidth(col) <= 0 || GetRowHeight(row) <= 0 )
7650 return;
7651
7652
7653 wxRect rect = CellToRect( row, col );
7654
7655 // right hand border
7656 dc.SetPen( GetColGridLinePen(col) );
7657 dc.DrawLine( rect.x + rect.width, rect.y,
7658 rect.x + rect.width, rect.y + rect.height + 1 );
7659
7660 // bottom border
7661 dc.SetPen( GetRowGridLinePen(row) );
7662 dc.DrawLine( rect.x, rect.y + rect.height,
7663 rect.x + rect.width, rect.y + rect.height);
7664 }
7665
7666 void wxGrid::DrawHighlight(wxDC& dc, const wxGridCellCoordsArray& cells)
7667 {
7668 // This if block was previously in wxGrid::OnPaint but that doesn't
7669 // seem to get called under wxGTK - MB
7670 //
7671 if ( m_currentCellCoords == wxGridNoCellCoords &&
7672 m_numRows && m_numCols )
7673 {
7674 m_currentCellCoords.Set(0, 0);
7675 }
7676
7677 if ( IsCellEditControlShown() )
7678 {
7679 // don't show highlight when the edit control is shown
7680 return;
7681 }
7682
7683 // if the active cell was repainted, repaint its highlight too because it
7684 // might have been damaged by the grid lines
7685 size_t count = cells.GetCount();
7686 for ( size_t n = 0; n < count; n++ )
7687 {
7688 wxGridCellCoords cell = cells[n];
7689
7690 // If we are using attributes, then we may have just exposed another
7691 // cell in a partially-visible merged cluster of cells. If the "anchor"
7692 // (upper left) cell of this merged cluster is the cell indicated by
7693 // m_currentCellCoords, then we need to refresh the cell highlight even
7694 // though the "anchor" itself is not part of our update segment.
7695 if ( CanHaveAttributes() )
7696 {
7697 int rows = 0,
7698 cols = 0;
7699 GetCellSize(cell.GetRow(), cell.GetCol(), &rows, &cols);
7700
7701 if ( rows < 0 )
7702 cell.SetRow(cell.GetRow() + rows);
7703
7704 if ( cols < 0 )
7705 cell.SetCol(cell.GetCol() + cols);
7706 }
7707
7708 if ( cell == m_currentCellCoords )
7709 {
7710 wxGridCellAttr* attr = GetCellAttr(m_currentCellCoords);
7711 DrawCellHighlight(dc, attr);
7712 attr->DecRef();
7713
7714 break;
7715 }
7716 }
7717 }
7718
7719 // This is used to redraw all grid lines e.g. when the grid line colour
7720 // has been changed
7721 //
7722 void wxGrid::DrawAllGridLines( wxDC& dc, const wxRegion & WXUNUSED(reg) )
7723 {
7724 if ( !m_gridLinesEnabled || !m_numRows || !m_numCols )
7725 return;
7726
7727 int top, bottom, left, right;
7728
7729 int cw, ch;
7730 m_gridWin->GetClientSize(&cw, &ch);
7731 CalcUnscrolledPosition( 0, 0, &left, &top );
7732 CalcUnscrolledPosition( cw, ch, &right, &bottom );
7733
7734 // avoid drawing grid lines past the last row and col
7735 //
7736 right = wxMin( right, GetColRight(GetColAt( m_numCols - 1 )) );
7737 bottom = wxMin( bottom, GetRowBottom(m_numRows - 1) );
7738
7739 // no gridlines inside multicells, clip them out
7740 int leftCol = GetColPos( internalXToCol(left) );
7741 int topRow = internalYToRow(top);
7742 int rightCol = GetColPos( internalXToCol(right) );
7743 int bottomRow = internalYToRow(bottom);
7744
7745 wxRegion clippedcells(0, 0, cw, ch);
7746
7747 int cell_rows, cell_cols;
7748 wxRect rect;
7749
7750 for ( int j = topRow; j <= bottomRow; j++ )
7751 {
7752 for ( int colPos = leftCol; colPos <= rightCol; colPos++ )
7753 {
7754 int i = GetColAt( colPos );
7755
7756 GetCellSize( j, i, &cell_rows, &cell_cols );
7757 if ((cell_rows > 1) || (cell_cols > 1))
7758 {
7759 rect = CellToRect(j,i);
7760 CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
7761 clippedcells.Subtract(rect);
7762 }
7763 else if ((cell_rows < 0) || (cell_cols < 0))
7764 {
7765 rect = CellToRect(j + cell_rows, i + cell_cols);
7766 CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
7767 clippedcells.Subtract(rect);
7768 }
7769 }
7770 }
7771
7772 dc.SetDeviceClippingRegion( clippedcells );
7773
7774
7775 // horizontal grid lines
7776 for ( int i = internalYToRow(top); i < m_numRows; i++ )
7777 {
7778 int bot = GetRowBottom(i) - 1;
7779
7780 if ( bot > bottom )
7781 break;
7782
7783 if ( bot >= top )
7784 {
7785 dc.SetPen( GetRowGridLinePen(i) );
7786 dc.DrawLine( left, bot, right, bot );
7787 }
7788 }
7789
7790 // vertical grid lines
7791 for ( int colPos = leftCol; colPos < m_numCols; colPos++ )
7792 {
7793 int i = GetColAt( colPos );
7794
7795 int colRight = GetColRight(i);
7796 #ifdef __WXGTK__
7797 if (GetLayoutDirection() != wxLayout_RightToLeft)
7798 #endif
7799 colRight--;
7800
7801 if ( colRight > right )
7802 break;
7803
7804 if ( colRight >= left )
7805 {
7806 dc.SetPen( GetColGridLinePen(i) );
7807 dc.DrawLine( colRight, top, colRight, bottom );
7808 }
7809 }
7810
7811 dc.DestroyClippingRegion();
7812 }
7813
7814 void wxGrid::DrawRowLabels( wxDC& dc, const wxArrayInt& rows)
7815 {
7816 if ( !m_numRows )
7817 return;
7818
7819 const size_t numLabels = rows.GetCount();
7820 for ( size_t i = 0; i < numLabels; i++ )
7821 {
7822 DrawRowLabel( dc, rows[i] );
7823 }
7824 }
7825
7826 void wxGrid::DrawRowLabel( wxDC& dc, int row )
7827 {
7828 if ( GetRowHeight(row) <= 0 || m_rowLabelWidth <= 0 )
7829 return;
7830
7831 wxRect rect;
7832
7833 int rowTop = GetRowTop(row),
7834 rowBottom = GetRowBottom(row) - 1;
7835
7836 dc.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW)));
7837 dc.DrawLine( m_rowLabelWidth - 1, rowTop, m_rowLabelWidth - 1, rowBottom );
7838 dc.DrawLine( 0, rowTop, 0, rowBottom );
7839 dc.DrawLine( 0, rowBottom, m_rowLabelWidth, rowBottom );
7840
7841 dc.SetPen( *wxWHITE_PEN );
7842 dc.DrawLine( 1, rowTop, 1, rowBottom );
7843 dc.DrawLine( 1, rowTop, m_rowLabelWidth - 1, rowTop );
7844
7845 dc.SetBackgroundMode( wxBRUSHSTYLE_TRANSPARENT );
7846 dc.SetTextForeground( GetLabelTextColour() );
7847 dc.SetFont( GetLabelFont() );
7848
7849 int hAlign, vAlign;
7850 GetRowLabelAlignment( &hAlign, &vAlign );
7851
7852 rect.SetX( 2 );
7853 rect.SetY( GetRowTop(row) + 2 );
7854 rect.SetWidth( m_rowLabelWidth - 4 );
7855 rect.SetHeight( GetRowHeight(row) - 4 );
7856 DrawTextRectangle( dc, GetRowLabelValue( row ), rect, hAlign, vAlign );
7857 }
7858
7859 void wxGrid::SetUseNativeColLabels( bool native )
7860 {
7861 m_nativeColumnLabels = native;
7862 if (native)
7863 {
7864 int height = wxRendererNative::Get().GetHeaderButtonHeight( this );
7865 SetColLabelSize( height );
7866 }
7867
7868 m_colLabelWin->Refresh();
7869 m_cornerLabelWin->Refresh();
7870 }
7871
7872 void wxGrid::DrawColLabels( wxDC& dc,const wxArrayInt& cols )
7873 {
7874 if ( !m_numCols )
7875 return;
7876
7877 const size_t numLabels = cols.GetCount();
7878 for ( size_t i = 0; i < numLabels; i++ )
7879 {
7880 DrawColLabel( dc, cols[i] );
7881 }
7882 }
7883
7884 void wxGrid::DrawCornerLabel(wxDC& dc)
7885 {
7886 if ( m_nativeColumnLabels )
7887 {
7888 wxRect rect(wxSize(m_rowLabelWidth, m_colLabelHeight));
7889 rect.Deflate(1);
7890
7891 wxRendererNative::Get().DrawHeaderButton(m_cornerLabelWin, dc, rect, 0);
7892 }
7893 else
7894 {
7895 dc.SetPen(wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW)));
7896 dc.DrawLine( m_rowLabelWidth - 1, m_colLabelHeight - 1,
7897 m_rowLabelWidth - 1, 0 );
7898 dc.DrawLine( m_rowLabelWidth - 1, m_colLabelHeight - 1,
7899 0, m_colLabelHeight - 1 );
7900 dc.DrawLine( 0, 0, m_rowLabelWidth, 0 );
7901 dc.DrawLine( 0, 0, 0, m_colLabelHeight );
7902
7903 dc.SetPen( *wxWHITE_PEN );
7904 dc.DrawLine( 1, 1, m_rowLabelWidth - 1, 1 );
7905 dc.DrawLine( 1, 1, 1, m_colLabelHeight - 1 );
7906 }
7907 }
7908
7909 void wxGrid::DrawColLabel(wxDC& dc, int col)
7910 {
7911 if ( GetColWidth(col) <= 0 || m_colLabelHeight <= 0 )
7912 return;
7913
7914 int colLeft = GetColLeft(col);
7915
7916 wxRect rect(colLeft, 0, GetColWidth(col), m_colLabelHeight);
7917
7918 if ( m_nativeColumnLabels )
7919 {
7920 wxRendererNative::Get().DrawHeaderButton(m_colLabelWin, dc, rect, 0);
7921 }
7922 else
7923 {
7924 int colRight = GetColRight(col) - 1;
7925
7926 dc.SetPen(wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW)));
7927 dc.DrawLine( colRight, 0,
7928 colRight, m_colLabelHeight - 1 );
7929 dc.DrawLine( colLeft, 0,
7930 colRight, 0 );
7931 dc.DrawLine( colLeft, m_colLabelHeight - 1,
7932 colRight + 1, m_colLabelHeight - 1 );
7933
7934 dc.SetPen( *wxWHITE_PEN );
7935 dc.DrawLine( colLeft, 1, colLeft, m_colLabelHeight - 1 );
7936 dc.DrawLine( colLeft, 1, colRight, 1 );
7937 }
7938
7939 dc.SetBackgroundMode( wxBRUSHSTYLE_TRANSPARENT );
7940 dc.SetTextForeground( GetLabelTextColour() );
7941 dc.SetFont( GetLabelFont() );
7942
7943 int hAlign, vAlign;
7944 GetColLabelAlignment( &hAlign, &vAlign );
7945 const int orient = GetColLabelTextOrientation();
7946
7947 rect.Deflate(2);
7948 DrawTextRectangle(dc, GetColLabelValue(col), rect, hAlign, vAlign, orient);
7949 }
7950
7951 // TODO: these 2 functions should be replaced with wxDC::DrawLabel() to which
7952 // we just have to add textOrientation support
7953 void wxGrid::DrawTextRectangle( wxDC& dc,
7954 const wxString& value,
7955 const wxRect& rect,
7956 int horizAlign,
7957 int vertAlign,
7958 int textOrientation )
7959 {
7960 wxArrayString lines;
7961
7962 StringToLines( value, lines );
7963
7964 DrawTextRectangle(dc, lines, rect, horizAlign, vertAlign, textOrientation);
7965 }
7966
7967 void wxGrid::DrawTextRectangle(wxDC& dc,
7968 const wxArrayString& lines,
7969 const wxRect& rect,
7970 int horizAlign,
7971 int vertAlign,
7972 int textOrientation)
7973 {
7974 if ( lines.empty() )
7975 return;
7976
7977 wxDCClipper clip(dc, rect);
7978
7979 long textWidth,
7980 textHeight;
7981
7982 if ( textOrientation == wxHORIZONTAL )
7983 GetTextBoxSize( dc, lines, &textWidth, &textHeight );
7984 else
7985 GetTextBoxSize( dc, lines, &textHeight, &textWidth );
7986
7987 int x = 0,
7988 y = 0;
7989 switch ( vertAlign )
7990 {
7991 case wxALIGN_BOTTOM:
7992 if ( textOrientation == wxHORIZONTAL )
7993 y = rect.y + (rect.height - textHeight - 1);
7994 else
7995 x = rect.x + rect.width - textWidth;
7996 break;
7997
7998 case wxALIGN_CENTRE:
7999 if ( textOrientation == wxHORIZONTAL )
8000 y = rect.y + ((rect.height - textHeight) / 2);
8001 else
8002 x = rect.x + ((rect.width - textWidth) / 2);
8003 break;
8004
8005 case wxALIGN_TOP:
8006 default:
8007 if ( textOrientation == wxHORIZONTAL )
8008 y = rect.y + 1;
8009 else
8010 x = rect.x + 1;
8011 break;
8012 }
8013
8014 // Align each line of a multi-line label
8015 size_t nLines = lines.GetCount();
8016 for ( size_t l = 0; l < nLines; l++ )
8017 {
8018 const wxString& line = lines[l];
8019
8020 if ( line.empty() )
8021 {
8022 *(textOrientation == wxHORIZONTAL ? &y : &x) += dc.GetCharHeight();
8023 continue;
8024 }
8025
8026 wxCoord lineWidth = 0,
8027 lineHeight = 0;
8028 dc.GetTextExtent(line, &lineWidth, &lineHeight);
8029
8030 switch ( horizAlign )
8031 {
8032 case wxALIGN_RIGHT:
8033 if ( textOrientation == wxHORIZONTAL )
8034 x = rect.x + (rect.width - lineWidth - 1);
8035 else
8036 y = rect.y + lineWidth + 1;
8037 break;
8038
8039 case wxALIGN_CENTRE:
8040 if ( textOrientation == wxHORIZONTAL )
8041 x = rect.x + ((rect.width - lineWidth) / 2);
8042 else
8043 y = rect.y + rect.height - ((rect.height - lineWidth) / 2);
8044 break;
8045
8046 case wxALIGN_LEFT:
8047 default:
8048 if ( textOrientation == wxHORIZONTAL )
8049 x = rect.x + 1;
8050 else
8051 y = rect.y + rect.height - 1;
8052 break;
8053 }
8054
8055 if ( textOrientation == wxHORIZONTAL )
8056 {
8057 dc.DrawText( line, x, y );
8058 y += lineHeight;
8059 }
8060 else
8061 {
8062 dc.DrawRotatedText( line, x, y, 90.0 );
8063 x += lineHeight;
8064 }
8065 }
8066 }
8067
8068 // Split multi-line text up into an array of strings.
8069 // Any existing contents of the string array are preserved.
8070 //
8071 // TODO: refactor wxTextFile::Read() and reuse the same code from here
8072 void wxGrid::StringToLines( const wxString& value, wxArrayString& lines ) const
8073 {
8074 int startPos = 0;
8075 int pos;
8076 wxString eol = wxTextFile::GetEOL( wxTextFileType_Unix );
8077 wxString tVal = wxTextFile::Translate( value, wxTextFileType_Unix );
8078
8079 while ( startPos < (int)tVal.length() )
8080 {
8081 pos = tVal.Mid(startPos).Find( eol );
8082 if ( pos < 0 )
8083 {
8084 break;
8085 }
8086 else if ( pos == 0 )
8087 {
8088 lines.Add( wxEmptyString );
8089 }
8090 else
8091 {
8092 lines.Add( tVal.Mid(startPos, pos) );
8093 }
8094
8095 startPos += pos + 1;
8096 }
8097
8098 if ( startPos < (int)tVal.length() )
8099 {
8100 lines.Add( tVal.Mid( startPos ) );
8101 }
8102 }
8103
8104 void wxGrid::GetTextBoxSize( const wxDC& dc,
8105 const wxArrayString& lines,
8106 long *width, long *height ) const
8107 {
8108 wxCoord w = 0;
8109 wxCoord h = 0;
8110 wxCoord lineW = 0, lineH = 0;
8111
8112 size_t i;
8113 for ( i = 0; i < lines.GetCount(); i++ )
8114 {
8115 dc.GetTextExtent( lines[i], &lineW, &lineH );
8116 w = wxMax( w, lineW );
8117 h += lineH;
8118 }
8119
8120 *width = w;
8121 *height = h;
8122 }
8123
8124 //
8125 // ------ Batch processing.
8126 //
8127 void wxGrid::EndBatch()
8128 {
8129 if ( m_batchCount > 0 )
8130 {
8131 m_batchCount--;
8132 if ( !m_batchCount )
8133 {
8134 CalcDimensions();
8135 m_rowLabelWin->Refresh();
8136 m_colLabelWin->Refresh();
8137 m_cornerLabelWin->Refresh();
8138 m_gridWin->Refresh();
8139 }
8140 }
8141 }
8142
8143 // Use this, rather than wxWindow::Refresh(), to force an immediate
8144 // repainting of the grid. Has no effect if you are already inside a
8145 // BeginBatch / EndBatch block.
8146 //
8147 void wxGrid::ForceRefresh()
8148 {
8149 BeginBatch();
8150 EndBatch();
8151 }
8152
8153 bool wxGrid::Enable(bool enable)
8154 {
8155 if ( !wxScrolledWindow::Enable(enable) )
8156 return false;
8157
8158 // redraw in the new state
8159 m_gridWin->Refresh();
8160
8161 return true;
8162 }
8163
8164 //
8165 // ------ Edit control functions
8166 //
8167
8168 void wxGrid::EnableEditing( bool edit )
8169 {
8170 // TODO: improve this ?
8171 //
8172 if ( edit != m_editable )
8173 {
8174 if (!edit)
8175 EnableCellEditControl(edit);
8176 m_editable = edit;
8177 }
8178 }
8179
8180 void wxGrid::EnableCellEditControl( bool enable )
8181 {
8182 if (! m_editable)
8183 return;
8184
8185 if ( enable != m_cellEditCtrlEnabled )
8186 {
8187 if ( enable )
8188 {
8189 if (SendEvent( wxEVT_GRID_EDITOR_SHOWN) <0)
8190 return;
8191
8192 // this should be checked by the caller!
8193 wxASSERT_MSG( CanEnableCellControl(), _T("can't enable editing for this cell!") );
8194
8195 // do it before ShowCellEditControl()
8196 m_cellEditCtrlEnabled = enable;
8197
8198 ShowCellEditControl();
8199 }
8200 else
8201 {
8202 //FIXME:add veto support
8203 SendEvent( wxEVT_GRID_EDITOR_HIDDEN );
8204
8205 HideCellEditControl();
8206 SaveEditControlValue();
8207
8208 // do it after HideCellEditControl()
8209 m_cellEditCtrlEnabled = enable;
8210 }
8211 }
8212 }
8213
8214 bool wxGrid::IsCurrentCellReadOnly() const
8215 {
8216 // const_cast
8217 wxGridCellAttr* attr = ((wxGrid *)this)->GetCellAttr(m_currentCellCoords);
8218 bool readonly = attr->IsReadOnly();
8219 attr->DecRef();
8220
8221 return readonly;
8222 }
8223
8224 bool wxGrid::CanEnableCellControl() const
8225 {
8226 return m_editable && (m_currentCellCoords != wxGridNoCellCoords) &&
8227 !IsCurrentCellReadOnly();
8228 }
8229
8230 bool wxGrid::IsCellEditControlEnabled() const
8231 {
8232 // the cell edit control might be disable for all cells or just for the
8233 // current one if it's read only
8234 return m_cellEditCtrlEnabled ? !IsCurrentCellReadOnly() : false;
8235 }
8236
8237 bool wxGrid::IsCellEditControlShown() const
8238 {
8239 bool isShown = false;
8240
8241 if ( m_cellEditCtrlEnabled )
8242 {
8243 int row = m_currentCellCoords.GetRow();
8244 int col = m_currentCellCoords.GetCol();
8245 wxGridCellAttr* attr = GetCellAttr(row, col);
8246 wxGridCellEditor* editor = attr->GetEditor((wxGrid*) this, row, col);
8247 attr->DecRef();
8248
8249 if ( editor )
8250 {
8251 if ( editor->IsCreated() )
8252 {
8253 isShown = editor->GetControl()->IsShown();
8254 }
8255
8256 editor->DecRef();
8257 }
8258 }
8259
8260 return isShown;
8261 }
8262
8263 void wxGrid::ShowCellEditControl()
8264 {
8265 if ( IsCellEditControlEnabled() )
8266 {
8267 if ( !IsVisible( m_currentCellCoords, false ) )
8268 {
8269 m_cellEditCtrlEnabled = false;
8270 return;
8271 }
8272 else
8273 {
8274 wxRect rect = CellToRect( m_currentCellCoords );
8275 int row = m_currentCellCoords.GetRow();
8276 int col = m_currentCellCoords.GetCol();
8277
8278 // if this is part of a multicell, find owner (topleft)
8279 int cell_rows, cell_cols;
8280 GetCellSize( row, col, &cell_rows, &cell_cols );
8281 if ( cell_rows <= 0 || cell_cols <= 0 )
8282 {
8283 row += cell_rows;
8284 col += cell_cols;
8285 m_currentCellCoords.SetRow( row );
8286 m_currentCellCoords.SetCol( col );
8287 }
8288
8289 // erase the highlight and the cell contents because the editor
8290 // might not cover the entire cell
8291 wxClientDC dc( m_gridWin );
8292 PrepareDC( dc );
8293 wxGridCellAttr* attr = GetCellAttr(row, col);
8294 dc.SetBrush(wxBrush(attr->GetBackgroundColour()));
8295 dc.SetPen(*wxTRANSPARENT_PEN);
8296 dc.DrawRectangle(rect);
8297
8298 // convert to scrolled coords
8299 CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
8300
8301 int nXMove = 0;
8302 if (rect.x < 0)
8303 nXMove = rect.x;
8304
8305 // cell is shifted by one pixel
8306 // However, don't allow x or y to become negative
8307 // since the SetSize() method interprets that as
8308 // "don't change."
8309 if (rect.x > 0)
8310 rect.x--;
8311 if (rect.y > 0)
8312 rect.y--;
8313
8314 wxGridCellEditor* editor = attr->GetEditor(this, row, col);
8315 if ( !editor->IsCreated() )
8316 {
8317 editor->Create(m_gridWin, wxID_ANY,
8318 new wxGridCellEditorEvtHandler(this, editor));
8319
8320 wxGridEditorCreatedEvent evt(GetId(),
8321 wxEVT_GRID_EDITOR_CREATED,
8322 this,
8323 row,
8324 col,
8325 editor->GetControl());
8326 GetEventHandler()->ProcessEvent(evt);
8327 }
8328
8329 // resize editor to overflow into righthand cells if allowed
8330 int maxWidth = rect.width;
8331 wxString value = GetCellValue(row, col);
8332 if ( (value != wxEmptyString) && (attr->GetOverflow()) )
8333 {
8334 int y;
8335 GetTextExtent(value, &maxWidth, &y, NULL, NULL, &attr->GetFont());
8336 if (maxWidth < rect.width)
8337 maxWidth = rect.width;
8338 }
8339
8340 int client_right = m_gridWin->GetClientSize().GetWidth();
8341 if (rect.x + maxWidth > client_right)
8342 maxWidth = client_right - rect.x;
8343
8344 if ((maxWidth > rect.width) && (col < m_numCols) && m_table)
8345 {
8346 GetCellSize( row, col, &cell_rows, &cell_cols );
8347 // may have changed earlier
8348 for (int i = col + cell_cols; i < m_numCols; i++)
8349 {
8350 int c_rows, c_cols;
8351 GetCellSize( row, i, &c_rows, &c_cols );
8352
8353 // looks weird going over a multicell
8354 if (m_table->IsEmptyCell( row, i ) &&
8355 (rect.width < maxWidth) && (c_rows == 1))
8356 {
8357 rect.width += GetColWidth( i );
8358 }
8359 else
8360 break;
8361 }
8362
8363 if (rect.GetRight() > client_right)
8364 rect.SetRight( client_right - 1 );
8365 }
8366
8367 editor->SetCellAttr( attr );
8368 editor->SetSize( rect );
8369 if (nXMove != 0)
8370 editor->GetControl()->Move(
8371 editor->GetControl()->GetPosition().x + nXMove,
8372 editor->GetControl()->GetPosition().y );
8373 editor->Show( true, attr );
8374
8375 // recalc dimensions in case we need to
8376 // expand the scrolled window to account for editor
8377 CalcDimensions();
8378
8379 editor->BeginEdit(row, col, this);
8380 editor->SetCellAttr(NULL);
8381
8382 editor->DecRef();
8383 attr->DecRef();
8384 }
8385 }
8386 }
8387
8388 void wxGrid::HideCellEditControl()
8389 {
8390 if ( IsCellEditControlEnabled() )
8391 {
8392 int row = m_currentCellCoords.GetRow();
8393 int col = m_currentCellCoords.GetCol();
8394
8395 wxGridCellAttr *attr = GetCellAttr(row, col);
8396 wxGridCellEditor *editor = attr->GetEditor(this, row, col);
8397 const bool editorHadFocus = editor->GetControl()->HasFocus();
8398 editor->Show( false );
8399 editor->DecRef();
8400 attr->DecRef();
8401
8402 // return the focus to the grid itself if the editor had it
8403 //
8404 // note that we must not do this unconditionally to avoid stealing
8405 // focus from the window which just received it if we are hiding the
8406 // editor precisely because we lost focus
8407 if ( editorHadFocus )
8408 m_gridWin->SetFocus();
8409
8410 // refresh whole row to the right
8411 wxRect rect( CellToRect(row, col) );
8412 CalcScrolledPosition(rect.x, rect.y, &rect.x, &rect.y );
8413 rect.width = m_gridWin->GetClientSize().GetWidth() - rect.x;
8414
8415 #ifdef __WXMAC__
8416 // ensure that the pixels under the focus ring get refreshed as well
8417 rect.Inflate(10, 10);
8418 #endif
8419
8420 m_gridWin->Refresh( false, &rect );
8421 }
8422 }
8423
8424 void wxGrid::SaveEditControlValue()
8425 {
8426 if ( IsCellEditControlEnabled() )
8427 {
8428 int row = m_currentCellCoords.GetRow();
8429 int col = m_currentCellCoords.GetCol();
8430
8431 wxString oldval = GetCellValue(row, col);
8432
8433 wxGridCellAttr* attr = GetCellAttr(row, col);
8434 wxGridCellEditor* editor = attr->GetEditor(this, row, col);
8435 bool changed = editor->EndEdit(row, col, this);
8436
8437 editor->DecRef();
8438 attr->DecRef();
8439
8440 if (changed)
8441 {
8442 if ( SendEvent( wxEVT_GRID_CELL_CHANGE,
8443 m_currentCellCoords.GetRow(),
8444 m_currentCellCoords.GetCol() ) < 0 )
8445 {
8446 // Event has been vetoed, set the data back.
8447 SetCellValue(row, col, oldval);
8448 }
8449 }
8450 }
8451 }
8452
8453 //
8454 // ------ Grid location functions
8455 // Note that all of these functions work with the logical coordinates of
8456 // grid cells and labels so you will need to convert from device
8457 // coordinates for mouse events etc.
8458 //
8459
8460 void wxGrid::XYToCell( int x, int y, wxGridCellCoords& coords ) const
8461 {
8462 int row = YToRow(y);
8463 int col = XToCol(x);
8464
8465 if ( row == -1 || col == -1 )
8466 {
8467 coords = wxGridNoCellCoords;
8468 }
8469 else
8470 {
8471 coords.Set( row, col );
8472 }
8473 }
8474
8475 // Internal Helper function for computing row or column from some
8476 // (unscrolled) coordinate value, using either
8477 // m_defaultRowHeight/m_defaultColWidth or binary search on array
8478 // of m_rowBottoms/m_ColRights to speed up the search!
8479
8480 static int CoordToRowOrCol(int coord, int defaultDist, int minDist,
8481 const wxArrayInt& BorderArray, int nMax,
8482 bool clipToMinMax)
8483 {
8484 if (coord < 0)
8485 return clipToMinMax && (nMax > 0) ? 0 : -1;
8486
8487 if (!defaultDist)
8488 defaultDist = 1;
8489
8490 size_t i_max = coord / defaultDist,
8491 i_min = 0;
8492
8493 if (BorderArray.IsEmpty())
8494 {
8495 if ((int) i_max < nMax)
8496 return i_max;
8497 return clipToMinMax ? nMax - 1 : -1;
8498 }
8499
8500 if ( i_max >= BorderArray.GetCount())
8501 {
8502 i_max = BorderArray.GetCount() - 1;
8503 }
8504 else
8505 {
8506 if ( coord >= BorderArray[i_max])
8507 {
8508 i_min = i_max;
8509 if (minDist)
8510 i_max = coord / minDist;
8511 else
8512 i_max = BorderArray.GetCount() - 1;
8513 }
8514
8515 if ( i_max >= BorderArray.GetCount())
8516 i_max = BorderArray.GetCount() - 1;
8517 }
8518
8519 if ( coord >= BorderArray[i_max])
8520 return clipToMinMax ? (int)i_max : -1;
8521 if ( coord < BorderArray[0] )
8522 return 0;
8523
8524 while ( i_max - i_min > 0 )
8525 {
8526 wxCHECK_MSG(BorderArray[i_min] <= coord && coord < BorderArray[i_max],
8527 0, _T("wxGrid: internal error in CoordToRowOrCol"));
8528 if (coord >= BorderArray[ i_max - 1])
8529 return i_max;
8530 else
8531 i_max--;
8532 int median = i_min + (i_max - i_min + 1) / 2;
8533 if (coord < BorderArray[median])
8534 i_max = median;
8535 else
8536 i_min = median;
8537 }
8538
8539 return i_max;
8540 }
8541
8542 int wxGrid::YToRow( int y ) const
8543 {
8544 return CoordToRowOrCol(y, m_defaultRowHeight,
8545 m_minAcceptableRowHeight, m_rowBottoms, m_numRows, false);
8546 }
8547
8548 int wxGrid::XToCol( int x, bool clipToMinMax ) const
8549 {
8550 if (x < 0)
8551 return clipToMinMax && (m_numCols > 0) ? GetColAt( 0 ) : -1;
8552
8553 wxASSERT_MSG(m_defaultColWidth > 0, wxT("Default column width can not be zero"));
8554
8555 int maxPos = x / m_defaultColWidth;
8556 int minPos = 0;
8557
8558 if (m_colRights.IsEmpty())
8559 {
8560 if(maxPos < m_numCols)
8561 return GetColAt( maxPos );
8562 return clipToMinMax ? GetColAt( m_numCols - 1 ) : -1;
8563 }
8564
8565 if ( maxPos >= m_numCols)
8566 maxPos = m_numCols - 1;
8567 else
8568 {
8569 if ( x >= m_colRights[GetColAt( maxPos )])
8570 {
8571 minPos = maxPos;
8572 if (m_minAcceptableColWidth)
8573 maxPos = x / m_minAcceptableColWidth;
8574 else
8575 maxPos = m_numCols - 1;
8576 }
8577 if ( maxPos >= m_numCols)
8578 maxPos = m_numCols - 1;
8579 }
8580
8581 //X is beyond the last column
8582 if ( x >= m_colRights[GetColAt( maxPos )])
8583 return clipToMinMax ? GetColAt( maxPos ) : -1;
8584
8585 //X is before the first column
8586 if ( x < m_colRights[GetColAt( 0 )] )
8587 return GetColAt( 0 );
8588
8589 //Perform a binary search
8590 while ( maxPos - minPos > 0 )
8591 {
8592 wxCHECK_MSG(m_colRights[GetColAt( minPos )] <= x && x < m_colRights[GetColAt( maxPos )],
8593 0, _T("wxGrid: internal error in XToCol"));
8594
8595 if (x >= m_colRights[GetColAt( maxPos - 1 )])
8596 return GetColAt( maxPos );
8597 else
8598 maxPos--;
8599 int median = minPos + (maxPos - minPos + 1) / 2;
8600 if (x < m_colRights[GetColAt( median )])
8601 maxPos = median;
8602 else
8603 minPos = median;
8604 }
8605 return GetColAt( maxPos );
8606 }
8607
8608 // return the row number that that the y coord is near
8609 // the edge of, or -1 if not near an edge.
8610 // coords can only possibly be near an edge if
8611 // (a) the row/column is large enough to still allow for an "inner" area
8612 // that is _not_ nead the edge (i.e., if the height/width is smaller
8613 // than WXGRID_LABEL_EDGE_ZONE, coords are _never_ considered to be
8614 // near the edge).
8615 // and
8616 // (b) resizing rows/columns (the thing for which edge detection is
8617 // relevant at all) is enabled.
8618 //
8619 int wxGrid::YToEdgeOfRow( int y ) const
8620 {
8621 int i;
8622 i = internalYToRow(y);
8623
8624 if ( GetRowHeight(i) > WXGRID_LABEL_EDGE_ZONE && CanDragRowSize() )
8625 {
8626 // We know that we are in row i, test whether we are
8627 // close enough to lower or upper border, respectively.
8628 if ( abs(GetRowBottom(i) - y) < WXGRID_LABEL_EDGE_ZONE )
8629 return i;
8630 else if ( i > 0 && y - GetRowTop(i) < WXGRID_LABEL_EDGE_ZONE )
8631 return i - 1;
8632 }
8633
8634 return -1;
8635 }
8636
8637 // return the col number that that the x coord is near the edge of, or
8638 // -1 if not near an edge
8639 // See comment at YToEdgeOfRow for conditions on edge detection.
8640 //
8641 int wxGrid::XToEdgeOfCol( int x ) const
8642 {
8643 int i;
8644 i = internalXToCol(x);
8645
8646 if ( GetColWidth(i) > WXGRID_LABEL_EDGE_ZONE && CanDragColSize() )
8647 {
8648 // We know that we are in column i; test whether we are
8649 // close enough to right or left border, respectively.
8650 if ( abs(GetColRight(i) - x) < WXGRID_LABEL_EDGE_ZONE )
8651 return i;
8652 else if ( i > 0 && x - GetColLeft(i) < WXGRID_LABEL_EDGE_ZONE )
8653 return i - 1;
8654 }
8655
8656 return -1;
8657 }
8658
8659 wxRect wxGrid::CellToRect( int row, int col ) const
8660 {
8661 wxRect rect( -1, -1, -1, -1 );
8662
8663 if ( row >= 0 && row < m_numRows &&
8664 col >= 0 && col < m_numCols )
8665 {
8666 int i, cell_rows, cell_cols;
8667 rect.width = rect.height = 0;
8668 GetCellSize( row, col, &cell_rows, &cell_cols );
8669 // if negative then find multicell owner
8670 if (cell_rows < 0)
8671 row += cell_rows;
8672 if (cell_cols < 0)
8673 col += cell_cols;
8674 GetCellSize( row, col, &cell_rows, &cell_cols );
8675
8676 rect.x = GetColLeft(col);
8677 rect.y = GetRowTop(row);
8678 for (i=col; i < col + cell_cols; i++)
8679 rect.width += GetColWidth(i);
8680 for (i=row; i < row + cell_rows; i++)
8681 rect.height += GetRowHeight(i);
8682 }
8683
8684 // if grid lines are enabled, then the area of the cell is a bit smaller
8685 if (m_gridLinesEnabled)
8686 {
8687 rect.width -= 1;
8688 rect.height -= 1;
8689 }
8690
8691 return rect;
8692 }
8693
8694 bool wxGrid::IsVisible( int row, int col, bool wholeCellVisible ) const
8695 {
8696 // get the cell rectangle in logical coords
8697 //
8698 wxRect r( CellToRect( row, col ) );
8699
8700 // convert to device coords
8701 //
8702 int left, top, right, bottom;
8703 CalcScrolledPosition( r.GetLeft(), r.GetTop(), &left, &top );
8704 CalcScrolledPosition( r.GetRight(), r.GetBottom(), &right, &bottom );
8705
8706 // check against the client area of the grid window
8707 int cw, ch;
8708 m_gridWin->GetClientSize( &cw, &ch );
8709
8710 if ( wholeCellVisible )
8711 {
8712 // is the cell wholly visible ?
8713 return ( left >= 0 && right <= cw &&
8714 top >= 0 && bottom <= ch );
8715 }
8716 else
8717 {
8718 // is the cell partly visible ?
8719 //
8720 return ( ((left >= 0 && left < cw) || (right > 0 && right <= cw)) &&
8721 ((top >= 0 && top < ch) || (bottom > 0 && bottom <= ch)) );
8722 }
8723 }
8724
8725 // make the specified cell location visible by doing a minimal amount
8726 // of scrolling
8727 //
8728 void wxGrid::MakeCellVisible( int row, int col )
8729 {
8730 int i;
8731 int xpos = -1, ypos = -1;
8732
8733 if ( row >= 0 && row < m_numRows &&
8734 col >= 0 && col < m_numCols )
8735 {
8736 // get the cell rectangle in logical coords
8737 wxRect r( CellToRect( row, col ) );
8738
8739 // convert to device coords
8740 int left, top, right, bottom;
8741 CalcScrolledPosition( r.GetLeft(), r.GetTop(), &left, &top );
8742 CalcScrolledPosition( r.GetRight(), r.GetBottom(), &right, &bottom );
8743
8744 int cw, ch;
8745 m_gridWin->GetClientSize( &cw, &ch );
8746
8747 if ( top < 0 )
8748 {
8749 ypos = r.GetTop();
8750 }
8751 else if ( bottom > ch )
8752 {
8753 int h = r.GetHeight();
8754 ypos = r.GetTop();
8755 for ( i = row - 1; i >= 0; i-- )
8756 {
8757 int rowHeight = GetRowHeight(i);
8758 if ( h + rowHeight > ch )
8759 break;
8760
8761 h += rowHeight;
8762 ypos -= rowHeight;
8763 }
8764
8765 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
8766 // have rounding errors (this is important, because if we do,
8767 // we might not scroll at all and some cells won't be redrawn)
8768 //
8769 // Sometimes GRID_SCROLL_LINE / 2 is not enough,
8770 // so just add a full scroll unit...
8771 ypos += m_scrollLineY;
8772 }
8773
8774 // special handling for wide cells - show always left part of the cell!
8775 // Otherwise, e.g. when stepping from row to row, it would jump between
8776 // left and right part of the cell on every step!
8777 // if ( left < 0 )
8778 if ( left < 0 || (right - left) >= cw )
8779 {
8780 xpos = r.GetLeft();
8781 }
8782 else if ( right > cw )
8783 {
8784 // position the view so that the cell is on the right
8785 int x0, y0;
8786 CalcUnscrolledPosition(0, 0, &x0, &y0);
8787 xpos = x0 + (right - cw);
8788
8789 // see comment for ypos above
8790 xpos += m_scrollLineX;
8791 }
8792
8793 if ( xpos != -1 || ypos != -1 )
8794 {
8795 if ( xpos != -1 )
8796 xpos /= m_scrollLineX;
8797 if ( ypos != -1 )
8798 ypos /= m_scrollLineY;
8799 Scroll( xpos, ypos );
8800 AdjustScrollbars();
8801 }
8802 }
8803 }
8804
8805 //
8806 // ------ Grid cursor movement functions
8807 //
8808
8809 bool wxGrid::MoveCursorUp( bool expandSelection )
8810 {
8811 if ( m_currentCellCoords != wxGridNoCellCoords &&
8812 m_currentCellCoords.GetRow() >= 0 )
8813 {
8814 if ( expandSelection )
8815 {
8816 if ( m_selectingKeyboard == wxGridNoCellCoords )
8817 m_selectingKeyboard = m_currentCellCoords;
8818 if ( m_selectingKeyboard.GetRow() > 0 )
8819 {
8820 m_selectingKeyboard.SetRow( m_selectingKeyboard.GetRow() - 1 );
8821 MakeCellVisible( m_selectingKeyboard.GetRow(),
8822 m_selectingKeyboard.GetCol() );
8823 HighlightBlock( m_currentCellCoords, m_selectingKeyboard );
8824 }
8825 }
8826 else if ( m_currentCellCoords.GetRow() > 0 )
8827 {
8828 int row = m_currentCellCoords.GetRow() - 1;
8829 int col = m_currentCellCoords.GetCol();
8830 ClearSelection();
8831 MakeCellVisible( row, col );
8832 SetCurrentCell( row, col );
8833 }
8834 else
8835 return false;
8836
8837 return true;
8838 }
8839
8840 return false;
8841 }
8842
8843 bool wxGrid::MoveCursorDown( bool expandSelection )
8844 {
8845 if ( m_currentCellCoords != wxGridNoCellCoords &&
8846 m_currentCellCoords.GetRow() < m_numRows )
8847 {
8848 if ( expandSelection )
8849 {
8850 if ( m_selectingKeyboard == wxGridNoCellCoords )
8851 m_selectingKeyboard = m_currentCellCoords;
8852 if ( m_selectingKeyboard.GetRow() < m_numRows - 1 )
8853 {
8854 m_selectingKeyboard.SetRow( m_selectingKeyboard.GetRow() + 1 );
8855 MakeCellVisible( m_selectingKeyboard.GetRow(),
8856 m_selectingKeyboard.GetCol() );
8857 HighlightBlock( m_currentCellCoords, m_selectingKeyboard );
8858 }
8859 }
8860 else if ( m_currentCellCoords.GetRow() < m_numRows - 1 )
8861 {
8862 int row = m_currentCellCoords.GetRow() + 1;
8863 int col = m_currentCellCoords.GetCol();
8864 ClearSelection();
8865 MakeCellVisible( row, col );
8866 SetCurrentCell( row, col );
8867 }
8868 else
8869 return false;
8870
8871 return true;
8872 }
8873
8874 return false;
8875 }
8876
8877 bool wxGrid::MoveCursorLeft( bool expandSelection )
8878 {
8879 if ( m_currentCellCoords != wxGridNoCellCoords &&
8880 m_currentCellCoords.GetCol() >= 0 )
8881 {
8882 if ( expandSelection )
8883 {
8884 if ( m_selectingKeyboard == wxGridNoCellCoords )
8885 m_selectingKeyboard = m_currentCellCoords;
8886 if ( m_selectingKeyboard.GetCol() > 0 )
8887 {
8888 m_selectingKeyboard.SetCol( m_selectingKeyboard.GetCol() - 1 );
8889 MakeCellVisible( m_selectingKeyboard.GetRow(),
8890 m_selectingKeyboard.GetCol() );
8891 HighlightBlock( m_currentCellCoords, m_selectingKeyboard );
8892 }
8893 }
8894 else if ( GetColPos( m_currentCellCoords.GetCol() ) > 0 )
8895 {
8896 int row = m_currentCellCoords.GetRow();
8897 int col = GetColAt( GetColPos( m_currentCellCoords.GetCol() ) - 1 );
8898 ClearSelection();
8899
8900 MakeCellVisible( row, col );
8901 SetCurrentCell( row, col );
8902 }
8903 else
8904 return false;
8905
8906 return true;
8907 }
8908
8909 return false;
8910 }
8911
8912 bool wxGrid::MoveCursorRight( bool expandSelection )
8913 {
8914 if ( m_currentCellCoords != wxGridNoCellCoords &&
8915 m_currentCellCoords.GetCol() < m_numCols )
8916 {
8917 if ( expandSelection )
8918 {
8919 if ( m_selectingKeyboard == wxGridNoCellCoords )
8920 m_selectingKeyboard = m_currentCellCoords;
8921 if ( m_selectingKeyboard.GetCol() < m_numCols - 1 )
8922 {
8923 m_selectingKeyboard.SetCol( m_selectingKeyboard.GetCol() + 1 );
8924 MakeCellVisible( m_selectingKeyboard.GetRow(),
8925 m_selectingKeyboard.GetCol() );
8926 HighlightBlock( m_currentCellCoords, m_selectingKeyboard );
8927 }
8928 }
8929 else if ( GetColPos( m_currentCellCoords.GetCol() ) < m_numCols - 1 )
8930 {
8931 int row = m_currentCellCoords.GetRow();
8932 int col = GetColAt( GetColPos( m_currentCellCoords.GetCol() ) + 1 );
8933 ClearSelection();
8934
8935 MakeCellVisible( row, col );
8936 SetCurrentCell( row, col );
8937 }
8938 else
8939 return false;
8940
8941 return true;
8942 }
8943
8944 return false;
8945 }
8946
8947 bool wxGrid::MovePageUp()
8948 {
8949 if ( m_currentCellCoords == wxGridNoCellCoords )
8950 return false;
8951
8952 int row = m_currentCellCoords.GetRow();
8953 if ( row > 0 )
8954 {
8955 int cw, ch;
8956 m_gridWin->GetClientSize( &cw, &ch );
8957
8958 int y = GetRowTop(row);
8959 int newRow = internalYToRow( y - ch + 1 );
8960
8961 if ( newRow == row )
8962 {
8963 // row > 0, so newRow can never be less than 0 here.
8964 newRow = row - 1;
8965 }
8966
8967 MakeCellVisible( newRow, m_currentCellCoords.GetCol() );
8968 SetCurrentCell( newRow, m_currentCellCoords.GetCol() );
8969
8970 return true;
8971 }
8972
8973 return false;
8974 }
8975
8976 bool wxGrid::MovePageDown()
8977 {
8978 if ( m_currentCellCoords == wxGridNoCellCoords )
8979 return false;
8980
8981 int row = m_currentCellCoords.GetRow();
8982 if ( (row + 1) < m_numRows )
8983 {
8984 int cw, ch;
8985 m_gridWin->GetClientSize( &cw, &ch );
8986
8987 int y = GetRowTop(row);
8988 int newRow = internalYToRow( y + ch );
8989 if ( newRow == row )
8990 {
8991 // row < m_numRows, so newRow can't overflow here.
8992 newRow = row + 1;
8993 }
8994
8995 MakeCellVisible( newRow, m_currentCellCoords.GetCol() );
8996 SetCurrentCell( newRow, m_currentCellCoords.GetCol() );
8997
8998 return true;
8999 }
9000
9001 return false;
9002 }
9003
9004 bool wxGrid::MoveCursorUpBlock( bool expandSelection )
9005 {
9006 if ( m_table &&
9007 m_currentCellCoords != wxGridNoCellCoords &&
9008 m_currentCellCoords.GetRow() > 0 )
9009 {
9010 int row = m_currentCellCoords.GetRow();
9011 int col = m_currentCellCoords.GetCol();
9012
9013 if ( m_table->IsEmptyCell(row, col) )
9014 {
9015 // starting in an empty cell: find the next block of
9016 // non-empty cells
9017 //
9018 while ( row > 0 )
9019 {
9020 row--;
9021 if ( !(m_table->IsEmptyCell(row, col)) )
9022 break;
9023 }
9024 }
9025 else if ( m_table->IsEmptyCell(row - 1, col) )
9026 {
9027 // starting at the top of a block: find the next block
9028 //
9029 row--;
9030 while ( row > 0 )
9031 {
9032 row--;
9033 if ( !(m_table->IsEmptyCell(row, col)) )
9034 break;
9035 }
9036 }
9037 else
9038 {
9039 // starting within a block: find the top of the block
9040 //
9041 while ( row > 0 )
9042 {
9043 row--;
9044 if ( m_table->IsEmptyCell(row, col) )
9045 {
9046 row++;
9047 break;
9048 }
9049 }
9050 }
9051
9052 MakeCellVisible( row, col );
9053 if ( expandSelection )
9054 {
9055 m_selectingKeyboard = wxGridCellCoords( row, col );
9056 HighlightBlock( m_currentCellCoords, m_selectingKeyboard );
9057 }
9058 else
9059 {
9060 ClearSelection();
9061 SetCurrentCell( row, col );
9062 }
9063
9064 return true;
9065 }
9066
9067 return false;
9068 }
9069
9070 bool wxGrid::MoveCursorDownBlock( bool expandSelection )
9071 {
9072 if ( m_table &&
9073 m_currentCellCoords != wxGridNoCellCoords &&
9074 m_currentCellCoords.GetRow() < m_numRows - 1 )
9075 {
9076 int row = m_currentCellCoords.GetRow();
9077 int col = m_currentCellCoords.GetCol();
9078
9079 if ( m_table->IsEmptyCell(row, col) )
9080 {
9081 // starting in an empty cell: find the next block of
9082 // non-empty cells
9083 //
9084 while ( row < m_numRows - 1 )
9085 {
9086 row++;
9087 if ( !(m_table->IsEmptyCell(row, col)) )
9088 break;
9089 }
9090 }
9091 else if ( m_table->IsEmptyCell(row + 1, col) )
9092 {
9093 // starting at the bottom of a block: find the next block
9094 //
9095 row++;
9096 while ( row < m_numRows - 1 )
9097 {
9098 row++;
9099 if ( !(m_table->IsEmptyCell(row, col)) )
9100 break;
9101 }
9102 }
9103 else
9104 {
9105 // starting within a block: find the bottom of the block
9106 //
9107 while ( row < m_numRows - 1 )
9108 {
9109 row++;
9110 if ( m_table->IsEmptyCell(row, col) )
9111 {
9112 row--;
9113 break;
9114 }
9115 }
9116 }
9117
9118 MakeCellVisible( row, col );
9119 if ( expandSelection )
9120 {
9121 m_selectingKeyboard = wxGridCellCoords( row, col );
9122 HighlightBlock( m_currentCellCoords, m_selectingKeyboard );
9123 }
9124 else
9125 {
9126 ClearSelection();
9127 SetCurrentCell( row, col );
9128 }
9129
9130 return true;
9131 }
9132
9133 return false;
9134 }
9135
9136 bool wxGrid::MoveCursorLeftBlock( bool expandSelection )
9137 {
9138 if ( m_table &&
9139 m_currentCellCoords != wxGridNoCellCoords &&
9140 m_currentCellCoords.GetCol() > 0 )
9141 {
9142 int row = m_currentCellCoords.GetRow();
9143 int col = m_currentCellCoords.GetCol();
9144
9145 if ( m_table->IsEmptyCell(row, col) )
9146 {
9147 // starting in an empty cell: find the next block of
9148 // non-empty cells
9149 //
9150 while ( col > 0 )
9151 {
9152 col--;
9153 if ( !(m_table->IsEmptyCell(row, col)) )
9154 break;
9155 }
9156 }
9157 else if ( m_table->IsEmptyCell(row, col - 1) )
9158 {
9159 // starting at the left of a block: find the next block
9160 //
9161 col--;
9162 while ( col > 0 )
9163 {
9164 col--;
9165 if ( !(m_table->IsEmptyCell(row, col)) )
9166 break;
9167 }
9168 }
9169 else
9170 {
9171 // starting within a block: find the left of the block
9172 //
9173 while ( col > 0 )
9174 {
9175 col--;
9176 if ( m_table->IsEmptyCell(row, col) )
9177 {
9178 col++;
9179 break;
9180 }
9181 }
9182 }
9183
9184 MakeCellVisible( row, col );
9185 if ( expandSelection )
9186 {
9187 m_selectingKeyboard = wxGridCellCoords( row, col );
9188 HighlightBlock( m_currentCellCoords, m_selectingKeyboard );
9189 }
9190 else
9191 {
9192 ClearSelection();
9193 SetCurrentCell( row, col );
9194 }
9195
9196 return true;
9197 }
9198
9199 return false;
9200 }
9201
9202 bool wxGrid::MoveCursorRightBlock( bool expandSelection )
9203 {
9204 if ( m_table &&
9205 m_currentCellCoords != wxGridNoCellCoords &&
9206 m_currentCellCoords.GetCol() < m_numCols - 1 )
9207 {
9208 int row = m_currentCellCoords.GetRow();
9209 int col = m_currentCellCoords.GetCol();
9210
9211 if ( m_table->IsEmptyCell(row, col) )
9212 {
9213 // starting in an empty cell: find the next block of
9214 // non-empty cells
9215 //
9216 while ( col < m_numCols - 1 )
9217 {
9218 col++;
9219 if ( !(m_table->IsEmptyCell(row, col)) )
9220 break;
9221 }
9222 }
9223 else if ( m_table->IsEmptyCell(row, col + 1) )
9224 {
9225 // starting at the right of a block: find the next block
9226 //
9227 col++;
9228 while ( col < m_numCols - 1 )
9229 {
9230 col++;
9231 if ( !(m_table->IsEmptyCell(row, col)) )
9232 break;
9233 }
9234 }
9235 else
9236 {
9237 // starting within a block: find the right of the block
9238 //
9239 while ( col < m_numCols - 1 )
9240 {
9241 col++;
9242 if ( m_table->IsEmptyCell(row, col) )
9243 {
9244 col--;
9245 break;
9246 }
9247 }
9248 }
9249
9250 MakeCellVisible( row, col );
9251 if ( expandSelection )
9252 {
9253 m_selectingKeyboard = wxGridCellCoords( row, col );
9254 HighlightBlock( m_currentCellCoords, m_selectingKeyboard );
9255 }
9256 else
9257 {
9258 ClearSelection();
9259 SetCurrentCell( row, col );
9260 }
9261
9262 return true;
9263 }
9264
9265 return false;
9266 }
9267
9268 //
9269 // ------ Label values and formatting
9270 //
9271
9272 void wxGrid::GetRowLabelAlignment( int *horiz, int *vert ) const
9273 {
9274 if ( horiz )
9275 *horiz = m_rowLabelHorizAlign;
9276 if ( vert )
9277 *vert = m_rowLabelVertAlign;
9278 }
9279
9280 void wxGrid::GetColLabelAlignment( int *horiz, int *vert ) const
9281 {
9282 if ( horiz )
9283 *horiz = m_colLabelHorizAlign;
9284 if ( vert )
9285 *vert = m_colLabelVertAlign;
9286 }
9287
9288 int wxGrid::GetColLabelTextOrientation() const
9289 {
9290 return m_colLabelTextOrientation;
9291 }
9292
9293 wxString wxGrid::GetRowLabelValue( int row ) const
9294 {
9295 if ( m_table )
9296 {
9297 return m_table->GetRowLabelValue( row );
9298 }
9299 else
9300 {
9301 wxString s;
9302 s << row;
9303 return s;
9304 }
9305 }
9306
9307 wxString wxGrid::GetColLabelValue( int col ) const
9308 {
9309 if ( m_table )
9310 {
9311 return m_table->GetColLabelValue( col );
9312 }
9313 else
9314 {
9315 wxString s;
9316 s << col;
9317 return s;
9318 }
9319 }
9320
9321 void wxGrid::SetRowLabelSize( int width )
9322 {
9323 wxASSERT( width >= 0 || width == wxGRID_AUTOSIZE );
9324
9325 if ( width == wxGRID_AUTOSIZE )
9326 {
9327 width = CalcColOrRowLabelAreaMinSize(wxGRID_ROW);
9328 }
9329
9330 if ( width != m_rowLabelWidth )
9331 {
9332 if ( width == 0 )
9333 {
9334 m_rowLabelWin->Show( false );
9335 m_cornerLabelWin->Show( false );
9336 }
9337 else if ( m_rowLabelWidth == 0 )
9338 {
9339 m_rowLabelWin->Show( true );
9340 if ( m_colLabelHeight > 0 )
9341 m_cornerLabelWin->Show( true );
9342 }
9343
9344 m_rowLabelWidth = width;
9345 CalcWindowSizes();
9346 wxScrolledWindow::Refresh( true );
9347 }
9348 }
9349
9350 void wxGrid::SetColLabelSize( int height )
9351 {
9352 wxASSERT( height >=0 || height == wxGRID_AUTOSIZE );
9353
9354 if ( height == wxGRID_AUTOSIZE )
9355 {
9356 height = CalcColOrRowLabelAreaMinSize(wxGRID_COLUMN);
9357 }
9358
9359 if ( height != m_colLabelHeight )
9360 {
9361 if ( height == 0 )
9362 {
9363 m_colLabelWin->Show( false );
9364 m_cornerLabelWin->Show( false );
9365 }
9366 else if ( m_colLabelHeight == 0 )
9367 {
9368 m_colLabelWin->Show( true );
9369 if ( m_rowLabelWidth > 0 )
9370 m_cornerLabelWin->Show( true );
9371 }
9372
9373 m_colLabelHeight = height;
9374 CalcWindowSizes();
9375 wxScrolledWindow::Refresh( true );
9376 }
9377 }
9378
9379 void wxGrid::SetLabelBackgroundColour( const wxColour& colour )
9380 {
9381 if ( m_labelBackgroundColour != colour )
9382 {
9383 m_labelBackgroundColour = colour;
9384 m_rowLabelWin->SetBackgroundColour( colour );
9385 m_colLabelWin->SetBackgroundColour( colour );
9386 m_cornerLabelWin->SetBackgroundColour( colour );
9387
9388 if ( !GetBatchCount() )
9389 {
9390 m_rowLabelWin->Refresh();
9391 m_colLabelWin->Refresh();
9392 m_cornerLabelWin->Refresh();
9393 }
9394 }
9395 }
9396
9397 void wxGrid::SetLabelTextColour( const wxColour& colour )
9398 {
9399 if ( m_labelTextColour != colour )
9400 {
9401 m_labelTextColour = colour;
9402 if ( !GetBatchCount() )
9403 {
9404 m_rowLabelWin->Refresh();
9405 m_colLabelWin->Refresh();
9406 }
9407 }
9408 }
9409
9410 void wxGrid::SetLabelFont( const wxFont& font )
9411 {
9412 m_labelFont = font;
9413 if ( !GetBatchCount() )
9414 {
9415 m_rowLabelWin->Refresh();
9416 m_colLabelWin->Refresh();
9417 }
9418 }
9419
9420 void wxGrid::SetRowLabelAlignment( int horiz, int vert )
9421 {
9422 // allow old (incorrect) defs to be used
9423 switch ( horiz )
9424 {
9425 case wxLEFT: horiz = wxALIGN_LEFT; break;
9426 case wxRIGHT: horiz = wxALIGN_RIGHT; break;
9427 case wxCENTRE: horiz = wxALIGN_CENTRE; break;
9428 }
9429
9430 switch ( vert )
9431 {
9432 case wxTOP: vert = wxALIGN_TOP; break;
9433 case wxBOTTOM: vert = wxALIGN_BOTTOM; break;
9434 case wxCENTRE: vert = wxALIGN_CENTRE; break;
9435 }
9436
9437 if ( horiz == wxALIGN_LEFT || horiz == wxALIGN_CENTRE || horiz == wxALIGN_RIGHT )
9438 {
9439 m_rowLabelHorizAlign = horiz;
9440 }
9441
9442 if ( vert == wxALIGN_TOP || vert == wxALIGN_CENTRE || vert == wxALIGN_BOTTOM )
9443 {
9444 m_rowLabelVertAlign = vert;
9445 }
9446
9447 if ( !GetBatchCount() )
9448 {
9449 m_rowLabelWin->Refresh();
9450 }
9451 }
9452
9453 void wxGrid::SetColLabelAlignment( int horiz, int vert )
9454 {
9455 // allow old (incorrect) defs to be used
9456 switch ( horiz )
9457 {
9458 case wxLEFT: horiz = wxALIGN_LEFT; break;
9459 case wxRIGHT: horiz = wxALIGN_RIGHT; break;
9460 case wxCENTRE: horiz = wxALIGN_CENTRE; break;
9461 }
9462
9463 switch ( vert )
9464 {
9465 case wxTOP: vert = wxALIGN_TOP; break;
9466 case wxBOTTOM: vert = wxALIGN_BOTTOM; break;
9467 case wxCENTRE: vert = wxALIGN_CENTRE; break;
9468 }
9469
9470 if ( horiz == wxALIGN_LEFT || horiz == wxALIGN_CENTRE || horiz == wxALIGN_RIGHT )
9471 {
9472 m_colLabelHorizAlign = horiz;
9473 }
9474
9475 if ( vert == wxALIGN_TOP || vert == wxALIGN_CENTRE || vert == wxALIGN_BOTTOM )
9476 {
9477 m_colLabelVertAlign = vert;
9478 }
9479
9480 if ( !GetBatchCount() )
9481 {
9482 m_colLabelWin->Refresh();
9483 }
9484 }
9485
9486 // Note: under MSW, the default column label font must be changed because it
9487 // does not support vertical printing
9488 //
9489 // Example: wxFont font(9, wxSWISS, wxNORMAL, wxBOLD);
9490 // pGrid->SetLabelFont(font);
9491 // pGrid->SetColLabelTextOrientation(wxVERTICAL);
9492 //
9493 void wxGrid::SetColLabelTextOrientation( int textOrientation )
9494 {
9495 if ( textOrientation == wxHORIZONTAL || textOrientation == wxVERTICAL )
9496 m_colLabelTextOrientation = textOrientation;
9497
9498 if ( !GetBatchCount() )
9499 m_colLabelWin->Refresh();
9500 }
9501
9502 void wxGrid::SetRowLabelValue( int row, const wxString& s )
9503 {
9504 if ( m_table )
9505 {
9506 m_table->SetRowLabelValue( row, s );
9507 if ( !GetBatchCount() )
9508 {
9509 wxRect rect = CellToRect( row, 0 );
9510 if ( rect.height > 0 )
9511 {
9512 CalcScrolledPosition(0, rect.y, &rect.x, &rect.y);
9513 rect.x = 0;
9514 rect.width = m_rowLabelWidth;
9515 m_rowLabelWin->Refresh( true, &rect );
9516 }
9517 }
9518 }
9519 }
9520
9521 void wxGrid::SetColLabelValue( int col, const wxString& s )
9522 {
9523 if ( m_table )
9524 {
9525 m_table->SetColLabelValue( col, s );
9526 if ( !GetBatchCount() )
9527 {
9528 wxRect rect = CellToRect( 0, col );
9529 if ( rect.width > 0 )
9530 {
9531 CalcScrolledPosition(rect.x, 0, &rect.x, &rect.y);
9532 rect.y = 0;
9533 rect.height = m_colLabelHeight;
9534 m_colLabelWin->Refresh( true, &rect );
9535 }
9536 }
9537 }
9538 }
9539
9540 void wxGrid::SetGridLineColour( const wxColour& colour )
9541 {
9542 if ( m_gridLineColour != colour )
9543 {
9544 m_gridLineColour = colour;
9545
9546 wxClientDC dc( m_gridWin );
9547 PrepareDC( dc );
9548 DrawAllGridLines( dc, wxRegion() );
9549 }
9550 }
9551
9552 void wxGrid::SetCellHighlightColour( const wxColour& colour )
9553 {
9554 if ( m_cellHighlightColour != colour )
9555 {
9556 m_cellHighlightColour = colour;
9557
9558 wxClientDC dc( m_gridWin );
9559 PrepareDC( dc );
9560 wxGridCellAttr* attr = GetCellAttr(m_currentCellCoords);
9561 DrawCellHighlight(dc, attr);
9562 attr->DecRef();
9563 }
9564 }
9565
9566 void wxGrid::SetCellHighlightPenWidth(int width)
9567 {
9568 if (m_cellHighlightPenWidth != width)
9569 {
9570 m_cellHighlightPenWidth = width;
9571
9572 // Just redrawing the cell highlight is not enough since that won't
9573 // make any visible change if the the thickness is getting smaller.
9574 int row = m_currentCellCoords.GetRow();
9575 int col = m_currentCellCoords.GetCol();
9576 if ( row == -1 || col == -1 || GetColWidth(col) <= 0 || GetRowHeight(row) <= 0 )
9577 return;
9578
9579 wxRect rect = CellToRect(row, col);
9580 m_gridWin->Refresh(true, &rect);
9581 }
9582 }
9583
9584 void wxGrid::SetCellHighlightROPenWidth(int width)
9585 {
9586 if (m_cellHighlightROPenWidth != width)
9587 {
9588 m_cellHighlightROPenWidth = width;
9589
9590 // Just redrawing the cell highlight is not enough since that won't
9591 // make any visible change if the the thickness is getting smaller.
9592 int row = m_currentCellCoords.GetRow();
9593 int col = m_currentCellCoords.GetCol();
9594 if ( row == -1 || col == -1 ||
9595 GetColWidth(col) <= 0 || GetRowHeight(row) <= 0 )
9596 return;
9597
9598 wxRect rect = CellToRect(row, col);
9599 m_gridWin->Refresh(true, &rect);
9600 }
9601 }
9602
9603 void wxGrid::EnableGridLines( bool enable )
9604 {
9605 if ( enable != m_gridLinesEnabled )
9606 {
9607 m_gridLinesEnabled = enable;
9608
9609 if ( !GetBatchCount() )
9610 {
9611 if ( enable )
9612 {
9613 wxClientDC dc( m_gridWin );
9614 PrepareDC( dc );
9615 DrawAllGridLines( dc, wxRegion() );
9616 }
9617 else
9618 {
9619 m_gridWin->Refresh();
9620 }
9621 }
9622 }
9623 }
9624
9625 int wxGrid::GetDefaultRowSize() const
9626 {
9627 return m_defaultRowHeight;
9628 }
9629
9630 int wxGrid::GetRowSize( int row ) const
9631 {
9632 wxCHECK_MSG( row >= 0 && row < m_numRows, 0, _T("invalid row index") );
9633
9634 return GetRowHeight(row);
9635 }
9636
9637 int wxGrid::GetDefaultColSize() const
9638 {
9639 return m_defaultColWidth;
9640 }
9641
9642 int wxGrid::GetColSize( int col ) const
9643 {
9644 wxCHECK_MSG( col >= 0 && col < m_numCols, 0, _T("invalid column index") );
9645
9646 return GetColWidth(col);
9647 }
9648
9649 // ============================================================================
9650 // access to the grid attributes: each of them has a default value in the grid
9651 // itself and may be overidden on a per-cell basis
9652 // ============================================================================
9653
9654 // ----------------------------------------------------------------------------
9655 // setting default attributes
9656 // ----------------------------------------------------------------------------
9657
9658 void wxGrid::SetDefaultCellBackgroundColour( const wxColour& col )
9659 {
9660 m_defaultCellAttr->SetBackgroundColour(col);
9661 #ifdef __WXGTK__
9662 m_gridWin->SetBackgroundColour(col);
9663 #endif
9664 }
9665
9666 void wxGrid::SetDefaultCellTextColour( const wxColour& col )
9667 {
9668 m_defaultCellAttr->SetTextColour(col);
9669 }
9670
9671 void wxGrid::SetDefaultCellAlignment( int horiz, int vert )
9672 {
9673 m_defaultCellAttr->SetAlignment(horiz, vert);
9674 }
9675
9676 void wxGrid::SetDefaultCellOverflow( bool allow )
9677 {
9678 m_defaultCellAttr->SetOverflow(allow);
9679 }
9680
9681 void wxGrid::SetDefaultCellFont( const wxFont& font )
9682 {
9683 m_defaultCellAttr->SetFont(font);
9684 }
9685
9686 // For editors and renderers the type registry takes precedence over the
9687 // default attr, so we need to register the new editor/renderer for the string
9688 // data type in order to make setting a default editor/renderer appear to
9689 // work correctly.
9690
9691 void wxGrid::SetDefaultRenderer(wxGridCellRenderer *renderer)
9692 {
9693 RegisterDataType(wxGRID_VALUE_STRING,
9694 renderer,
9695 GetDefaultEditorForType(wxGRID_VALUE_STRING));
9696 }
9697
9698 void wxGrid::SetDefaultEditor(wxGridCellEditor *editor)
9699 {
9700 RegisterDataType(wxGRID_VALUE_STRING,
9701 GetDefaultRendererForType(wxGRID_VALUE_STRING),
9702 editor);
9703 }
9704
9705 // ----------------------------------------------------------------------------
9706 // access to the default attributes
9707 // ----------------------------------------------------------------------------
9708
9709 wxColour wxGrid::GetDefaultCellBackgroundColour() const
9710 {
9711 return m_defaultCellAttr->GetBackgroundColour();
9712 }
9713
9714 wxColour wxGrid::GetDefaultCellTextColour() const
9715 {
9716 return m_defaultCellAttr->GetTextColour();
9717 }
9718
9719 wxFont wxGrid::GetDefaultCellFont() const
9720 {
9721 return m_defaultCellAttr->GetFont();
9722 }
9723
9724 void wxGrid::GetDefaultCellAlignment( int *horiz, int *vert ) const
9725 {
9726 m_defaultCellAttr->GetAlignment(horiz, vert);
9727 }
9728
9729 bool wxGrid::GetDefaultCellOverflow() const
9730 {
9731 return m_defaultCellAttr->GetOverflow();
9732 }
9733
9734 wxGridCellRenderer *wxGrid::GetDefaultRenderer() const
9735 {
9736 return m_defaultCellAttr->GetRenderer(NULL, 0, 0);
9737 }
9738
9739 wxGridCellEditor *wxGrid::GetDefaultEditor() const
9740 {
9741 return m_defaultCellAttr->GetEditor(NULL, 0, 0);
9742 }
9743
9744 // ----------------------------------------------------------------------------
9745 // access to cell attributes
9746 // ----------------------------------------------------------------------------
9747
9748 wxColour wxGrid::GetCellBackgroundColour(int row, int col) const
9749 {
9750 wxGridCellAttr *attr = GetCellAttr(row, col);
9751 wxColour colour = attr->GetBackgroundColour();
9752 attr->DecRef();
9753
9754 return colour;
9755 }
9756
9757 wxColour wxGrid::GetCellTextColour( int row, int col ) const
9758 {
9759 wxGridCellAttr *attr = GetCellAttr(row, col);
9760 wxColour colour = attr->GetTextColour();
9761 attr->DecRef();
9762
9763 return colour;
9764 }
9765
9766 wxFont wxGrid::GetCellFont( int row, int col ) const
9767 {
9768 wxGridCellAttr *attr = GetCellAttr(row, col);
9769 wxFont font = attr->GetFont();
9770 attr->DecRef();
9771
9772 return font;
9773 }
9774
9775 void wxGrid::GetCellAlignment( int row, int col, int *horiz, int *vert ) const
9776 {
9777 wxGridCellAttr *attr = GetCellAttr(row, col);
9778 attr->GetAlignment(horiz, vert);
9779 attr->DecRef();
9780 }
9781
9782 bool wxGrid::GetCellOverflow( int row, int col ) const
9783 {
9784 wxGridCellAttr *attr = GetCellAttr(row, col);
9785 bool allow = attr->GetOverflow();
9786 attr->DecRef();
9787
9788 return allow;
9789 }
9790
9791 void wxGrid::GetCellSize( int row, int col, int *num_rows, int *num_cols ) const
9792 {
9793 wxGridCellAttr *attr = GetCellAttr(row, col);
9794 attr->GetSize( num_rows, num_cols );
9795 attr->DecRef();
9796 }
9797
9798 wxGridCellRenderer* wxGrid::GetCellRenderer(int row, int col) const
9799 {
9800 wxGridCellAttr* attr = GetCellAttr(row, col);
9801 wxGridCellRenderer* renderer = attr->GetRenderer(this, row, col);
9802 attr->DecRef();
9803
9804 return renderer;
9805 }
9806
9807 wxGridCellEditor* wxGrid::GetCellEditor(int row, int col) const
9808 {
9809 wxGridCellAttr* attr = GetCellAttr(row, col);
9810 wxGridCellEditor* editor = attr->GetEditor(this, row, col);
9811 attr->DecRef();
9812
9813 return editor;
9814 }
9815
9816 bool wxGrid::IsReadOnly(int row, int col) const
9817 {
9818 wxGridCellAttr* attr = GetCellAttr(row, col);
9819 bool isReadOnly = attr->IsReadOnly();
9820 attr->DecRef();
9821
9822 return isReadOnly;
9823 }
9824
9825 // ----------------------------------------------------------------------------
9826 // attribute support: cache, automatic provider creation, ...
9827 // ----------------------------------------------------------------------------
9828
9829 bool wxGrid::CanHaveAttributes() const
9830 {
9831 if ( !m_table )
9832 {
9833 return false;
9834 }
9835
9836 return m_table->CanHaveAttributes();
9837 }
9838
9839 void wxGrid::ClearAttrCache()
9840 {
9841 if ( m_attrCache.row != -1 )
9842 {
9843 wxGridCellAttr *oldAttr = m_attrCache.attr;
9844 m_attrCache.attr = NULL;
9845 m_attrCache.row = -1;
9846 // wxSafeDecRec(...) might cause event processing that accesses
9847 // the cached attribute, if one exists (e.g. by deleting the
9848 // editor stored within the attribute). Therefore it is important
9849 // to invalidate the cache before calling wxSafeDecRef!
9850 wxSafeDecRef(oldAttr);
9851 }
9852 }
9853
9854 void wxGrid::CacheAttr(int row, int col, wxGridCellAttr *attr) const
9855 {
9856 if ( attr != NULL )
9857 {
9858 wxGrid *self = (wxGrid *)this; // const_cast
9859
9860 self->ClearAttrCache();
9861 self->m_attrCache.row = row;
9862 self->m_attrCache.col = col;
9863 self->m_attrCache.attr = attr;
9864 wxSafeIncRef(attr);
9865 }
9866 }
9867
9868 bool wxGrid::LookupAttr(int row, int col, wxGridCellAttr **attr) const
9869 {
9870 if ( row == m_attrCache.row && col == m_attrCache.col )
9871 {
9872 *attr = m_attrCache.attr;
9873 wxSafeIncRef(m_attrCache.attr);
9874
9875 #ifdef DEBUG_ATTR_CACHE
9876 gs_nAttrCacheHits++;
9877 #endif
9878
9879 return true;
9880 }
9881 else
9882 {
9883 #ifdef DEBUG_ATTR_CACHE
9884 gs_nAttrCacheMisses++;
9885 #endif
9886
9887 return false;
9888 }
9889 }
9890
9891 wxGridCellAttr *wxGrid::GetCellAttr(int row, int col) const
9892 {
9893 wxGridCellAttr *attr = NULL;
9894 // Additional test to avoid looking at the cache e.g. for
9895 // wxNoCellCoords, as this will confuse memory management.
9896 if ( row >= 0 )
9897 {
9898 if ( !LookupAttr(row, col, &attr) )
9899 {
9900 attr = m_table ? m_table->GetAttr(row, col, wxGridCellAttr::Any)
9901 : (wxGridCellAttr *)NULL;
9902 CacheAttr(row, col, attr);
9903 }
9904 }
9905
9906 if (attr)
9907 {
9908 attr->SetDefAttr(m_defaultCellAttr);
9909 }
9910 else
9911 {
9912 attr = m_defaultCellAttr;
9913 attr->IncRef();
9914 }
9915
9916 return attr;
9917 }
9918
9919 wxGridCellAttr *wxGrid::GetOrCreateCellAttr(int row, int col) const
9920 {
9921 wxGridCellAttr *attr = (wxGridCellAttr *)NULL;
9922 bool canHave = ((wxGrid*)this)->CanHaveAttributes();
9923
9924 wxCHECK_MSG( canHave, attr, _T("Cell attributes not allowed"));
9925 wxCHECK_MSG( m_table, attr, _T("must have a table") );
9926
9927 attr = m_table->GetAttr(row, col, wxGridCellAttr::Cell);
9928 if ( !attr )
9929 {
9930 attr = new wxGridCellAttr(m_defaultCellAttr);
9931
9932 // artificially inc the ref count to match DecRef() in caller
9933 attr->IncRef();
9934 m_table->SetAttr(attr, row, col);
9935 }
9936
9937 return attr;
9938 }
9939
9940 // ----------------------------------------------------------------------------
9941 // setting column attributes (wrappers around SetColAttr)
9942 // ----------------------------------------------------------------------------
9943
9944 void wxGrid::SetColFormatBool(int col)
9945 {
9946 SetColFormatCustom(col, wxGRID_VALUE_BOOL);
9947 }
9948
9949 void wxGrid::SetColFormatNumber(int col)
9950 {
9951 SetColFormatCustom(col, wxGRID_VALUE_NUMBER);
9952 }
9953
9954 void wxGrid::SetColFormatFloat(int col, int width, int precision)
9955 {
9956 wxString typeName = wxGRID_VALUE_FLOAT;
9957 if ( (width != -1) || (precision != -1) )
9958 {
9959 typeName << _T(':') << width << _T(',') << precision;
9960 }
9961
9962 SetColFormatCustom(col, typeName);
9963 }
9964
9965 void wxGrid::SetColFormatCustom(int col, const wxString& typeName)
9966 {
9967 wxGridCellAttr *attr = m_table->GetAttr(-1, col, wxGridCellAttr::Col );
9968 if (!attr)
9969 attr = new wxGridCellAttr;
9970 wxGridCellRenderer *renderer = GetDefaultRendererForType(typeName);
9971 attr->SetRenderer(renderer);
9972 wxGridCellEditor *editor = GetDefaultEditorForType(typeName);
9973 attr->SetEditor(editor);
9974
9975 SetColAttr(col, attr);
9976
9977 }
9978
9979 // ----------------------------------------------------------------------------
9980 // setting cell attributes: this is forwarded to the table
9981 // ----------------------------------------------------------------------------
9982
9983 void wxGrid::SetAttr(int row, int col, wxGridCellAttr *attr)
9984 {
9985 if ( CanHaveAttributes() )
9986 {
9987 m_table->SetAttr(attr, row, col);
9988 ClearAttrCache();
9989 }
9990 else
9991 {
9992 wxSafeDecRef(attr);
9993 }
9994 }
9995
9996 void wxGrid::SetRowAttr(int row, wxGridCellAttr *attr)
9997 {
9998 if ( CanHaveAttributes() )
9999 {
10000 m_table->SetRowAttr(attr, row);
10001 ClearAttrCache();
10002 }
10003 else
10004 {
10005 wxSafeDecRef(attr);
10006 }
10007 }
10008
10009 void wxGrid::SetColAttr(int col, wxGridCellAttr *attr)
10010 {
10011 if ( CanHaveAttributes() )
10012 {
10013 m_table->SetColAttr(attr, col);
10014 ClearAttrCache();
10015 }
10016 else
10017 {
10018 wxSafeDecRef(attr);
10019 }
10020 }
10021
10022 void wxGrid::SetCellBackgroundColour( int row, int col, const wxColour& colour )
10023 {
10024 if ( CanHaveAttributes() )
10025 {
10026 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
10027 attr->SetBackgroundColour(colour);
10028 attr->DecRef();
10029 }
10030 }
10031
10032 void wxGrid::SetCellTextColour( int row, int col, const wxColour& colour )
10033 {
10034 if ( CanHaveAttributes() )
10035 {
10036 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
10037 attr->SetTextColour(colour);
10038 attr->DecRef();
10039 }
10040 }
10041
10042 void wxGrid::SetCellFont( int row, int col, const wxFont& font )
10043 {
10044 if ( CanHaveAttributes() )
10045 {
10046 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
10047 attr->SetFont(font);
10048 attr->DecRef();
10049 }
10050 }
10051
10052 void wxGrid::SetCellAlignment( int row, int col, int horiz, int vert )
10053 {
10054 if ( CanHaveAttributes() )
10055 {
10056 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
10057 attr->SetAlignment(horiz, vert);
10058 attr->DecRef();
10059 }
10060 }
10061
10062 void wxGrid::SetCellOverflow( int row, int col, bool allow )
10063 {
10064 if ( CanHaveAttributes() )
10065 {
10066 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
10067 attr->SetOverflow(allow);
10068 attr->DecRef();
10069 }
10070 }
10071
10072 void wxGrid::SetCellSize( int row, int col, int num_rows, int num_cols )
10073 {
10074 if ( CanHaveAttributes() )
10075 {
10076 int cell_rows, cell_cols;
10077
10078 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
10079 attr->GetSize(&cell_rows, &cell_cols);
10080 attr->SetSize(num_rows, num_cols);
10081 attr->DecRef();
10082
10083 // Cannot set the size of a cell to 0 or negative values
10084 // While it is perfectly legal to do that, this function cannot
10085 // handle all the possibilies, do it by hand by getting the CellAttr.
10086 // You can only set the size of a cell to 1,1 or greater with this fn
10087 wxASSERT_MSG( !((cell_rows < 1) || (cell_cols < 1)),
10088 wxT("wxGrid::SetCellSize setting cell size that is already part of another cell"));
10089 wxASSERT_MSG( !((num_rows < 1) || (num_cols < 1)),
10090 wxT("wxGrid::SetCellSize setting cell size to < 1"));
10091
10092 // if this was already a multicell then "turn off" the other cells first
10093 if ((cell_rows > 1) || (cell_cols > 1))
10094 {
10095 int i, j;
10096 for (j=row; j < row + cell_rows; j++)
10097 {
10098 for (i=col; i < col + cell_cols; i++)
10099 {
10100 if ((i != col) || (j != row))
10101 {
10102 wxGridCellAttr *attr_stub = GetOrCreateCellAttr(j, i);
10103 attr_stub->SetSize( 1, 1 );
10104 attr_stub->DecRef();
10105 }
10106 }
10107 }
10108 }
10109
10110 // mark the cells that will be covered by this cell to
10111 // negative or zero values to point back at this cell
10112 if (((num_rows > 1) || (num_cols > 1)) && (num_rows >= 1) && (num_cols >= 1))
10113 {
10114 int i, j;
10115 for (j=row; j < row + num_rows; j++)
10116 {
10117 for (i=col; i < col + num_cols; i++)
10118 {
10119 if ((i != col) || (j != row))
10120 {
10121 wxGridCellAttr *attr_stub = GetOrCreateCellAttr(j, i);
10122 attr_stub->SetSize( row - j, col - i );
10123 attr_stub->DecRef();
10124 }
10125 }
10126 }
10127 }
10128 }
10129 }
10130
10131 void wxGrid::SetCellRenderer(int row, int col, wxGridCellRenderer *renderer)
10132 {
10133 if ( CanHaveAttributes() )
10134 {
10135 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
10136 attr->SetRenderer(renderer);
10137 attr->DecRef();
10138 }
10139 }
10140
10141 void wxGrid::SetCellEditor(int row, int col, wxGridCellEditor* editor)
10142 {
10143 if ( CanHaveAttributes() )
10144 {
10145 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
10146 attr->SetEditor(editor);
10147 attr->DecRef();
10148 }
10149 }
10150
10151 void wxGrid::SetReadOnly(int row, int col, bool isReadOnly)
10152 {
10153 if ( CanHaveAttributes() )
10154 {
10155 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
10156 attr->SetReadOnly(isReadOnly);
10157 attr->DecRef();
10158 }
10159 }
10160
10161 // ----------------------------------------------------------------------------
10162 // Data type registration
10163 // ----------------------------------------------------------------------------
10164
10165 void wxGrid::RegisterDataType(const wxString& typeName,
10166 wxGridCellRenderer* renderer,
10167 wxGridCellEditor* editor)
10168 {
10169 m_typeRegistry->RegisterDataType(typeName, renderer, editor);
10170 }
10171
10172
10173 wxGridCellEditor * wxGrid::GetDefaultEditorForCell(int row, int col) const
10174 {
10175 wxString typeName = m_table->GetTypeName(row, col);
10176 return GetDefaultEditorForType(typeName);
10177 }
10178
10179 wxGridCellRenderer * wxGrid::GetDefaultRendererForCell(int row, int col) const
10180 {
10181 wxString typeName = m_table->GetTypeName(row, col);
10182 return GetDefaultRendererForType(typeName);
10183 }
10184
10185 wxGridCellEditor * wxGrid::GetDefaultEditorForType(const wxString& typeName) const
10186 {
10187 int index = m_typeRegistry->FindOrCloneDataType(typeName);
10188 if ( index == wxNOT_FOUND )
10189 {
10190 wxFAIL_MSG(wxString::Format(wxT("Unknown data type name [%s]"), typeName.c_str()));
10191
10192 return NULL;
10193 }
10194
10195 return m_typeRegistry->GetEditor(index);
10196 }
10197
10198 wxGridCellRenderer * wxGrid::GetDefaultRendererForType(const wxString& typeName) const
10199 {
10200 int index = m_typeRegistry->FindOrCloneDataType(typeName);
10201 if ( index == wxNOT_FOUND )
10202 {
10203 wxFAIL_MSG(wxString::Format(wxT("Unknown data type name [%s]"), typeName.c_str()));
10204
10205 return NULL;
10206 }
10207
10208 return m_typeRegistry->GetRenderer(index);
10209 }
10210
10211 // ----------------------------------------------------------------------------
10212 // row/col size
10213 // ----------------------------------------------------------------------------
10214
10215 void wxGrid::EnableDragRowSize( bool enable )
10216 {
10217 m_canDragRowSize = enable;
10218 }
10219
10220 void wxGrid::EnableDragColSize( bool enable )
10221 {
10222 m_canDragColSize = enable;
10223 }
10224
10225 void wxGrid::EnableDragGridSize( bool enable )
10226 {
10227 m_canDragGridSize = enable;
10228 }
10229
10230 void wxGrid::EnableDragCell( bool enable )
10231 {
10232 m_canDragCell = enable;
10233 }
10234
10235 void wxGrid::SetDefaultRowSize( int height, bool resizeExistingRows )
10236 {
10237 m_defaultRowHeight = wxMax( height, m_minAcceptableRowHeight );
10238
10239 if ( resizeExistingRows )
10240 {
10241 // since we are resizing all rows to the default row size,
10242 // we can simply clear the row heights and row bottoms
10243 // arrays (which also allows us to take advantage of
10244 // some speed optimisations)
10245 m_rowHeights.Empty();
10246 m_rowBottoms.Empty();
10247 if ( !GetBatchCount() )
10248 CalcDimensions();
10249 }
10250 }
10251
10252 void wxGrid::SetRowSize( int row, int height )
10253 {
10254 wxCHECK_RET( row >= 0 && row < m_numRows, _T("invalid row index") );
10255
10256 // if < 0 then calculate new height from label
10257 if ( height < 0 )
10258 {
10259 long w, h;
10260 wxArrayString lines;
10261 wxClientDC dc(m_rowLabelWin);
10262 dc.SetFont(GetLabelFont());
10263 StringToLines(GetRowLabelValue( row ), lines);
10264 GetTextBoxSize( dc, lines, &w, &h );
10265 //check that it is not less than the minimal height
10266 height = wxMax(h, GetRowMinimalAcceptableHeight());
10267 }
10268
10269 // See comment in SetColSize
10270 if ( height < GetRowMinimalAcceptableHeight())
10271 return;
10272
10273 if ( m_rowHeights.IsEmpty() )
10274 {
10275 // need to really create the array
10276 InitRowHeights();
10277 }
10278
10279 int h = wxMax( 0, height );
10280 int diff = h - m_rowHeights[row];
10281
10282 m_rowHeights[row] = h;
10283 for ( int i = row; i < m_numRows; i++ )
10284 {
10285 m_rowBottoms[i] += diff;
10286 }
10287
10288 if ( !GetBatchCount() )
10289 CalcDimensions();
10290 }
10291
10292 void wxGrid::SetDefaultColSize( int width, bool resizeExistingCols )
10293 {
10294 // we dont allow zero default column width
10295 m_defaultColWidth = wxMax( wxMax( width, m_minAcceptableColWidth ), 1 );
10296
10297 if ( resizeExistingCols )
10298 {
10299 // since we are resizing all columns to the default column size,
10300 // we can simply clear the col widths and col rights
10301 // arrays (which also allows us to take advantage of
10302 // some speed optimisations)
10303 m_colWidths.Empty();
10304 m_colRights.Empty();
10305 if ( !GetBatchCount() )
10306 CalcDimensions();
10307 }
10308 }
10309
10310 void wxGrid::SetColSize( int col, int width )
10311 {
10312 wxCHECK_RET( col >= 0 && col < m_numCols, _T("invalid column index") );
10313
10314 // if < 0 then calculate new width from label
10315 if ( width < 0 )
10316 {
10317 long w, h;
10318 wxArrayString lines;
10319 wxClientDC dc(m_colLabelWin);
10320 dc.SetFont(GetLabelFont());
10321 StringToLines(GetColLabelValue(col), lines);
10322 if ( GetColLabelTextOrientation() == wxHORIZONTAL )
10323 GetTextBoxSize( dc, lines, &w, &h );
10324 else
10325 GetTextBoxSize( dc, lines, &h, &w );
10326 width = w + 6;
10327 //check that it is not less than the minimal width
10328 width = wxMax(width, GetColMinimalAcceptableWidth());
10329 }
10330
10331 // should we check that it's bigger than GetColMinimalWidth(col) here?
10332 // (VZ)
10333 // No, because it is reasonable to assume the library user know's
10334 // what he is doing. However we should test against the weaker
10335 // constraint of minimalAcceptableWidth, as this breaks rendering
10336 //
10337 // This test then fixes sf.net bug #645734
10338
10339 if ( width < GetColMinimalAcceptableWidth() )
10340 return;
10341
10342 if ( m_colWidths.IsEmpty() )
10343 {
10344 // need to really create the array
10345 InitColWidths();
10346 }
10347
10348 int w = wxMax( 0, width );
10349 int diff = w - m_colWidths[col];
10350 m_colWidths[col] = w;
10351
10352 for ( int colPos = GetColPos(col); colPos < m_numCols; colPos++ )
10353 {
10354 m_colRights[GetColAt(colPos)] += diff;
10355 }
10356
10357 if ( !GetBatchCount() )
10358 CalcDimensions();
10359 }
10360
10361 void wxGrid::SetColMinimalWidth( int col, int width )
10362 {
10363 if (width > GetColMinimalAcceptableWidth())
10364 {
10365 wxLongToLongHashMap::key_type key = (wxLongToLongHashMap::key_type)col;
10366 m_colMinWidths[key] = width;
10367 }
10368 }
10369
10370 void wxGrid::SetRowMinimalHeight( int row, int width )
10371 {
10372 if (width > GetRowMinimalAcceptableHeight())
10373 {
10374 wxLongToLongHashMap::key_type key = (wxLongToLongHashMap::key_type)row;
10375 m_rowMinHeights[key] = width;
10376 }
10377 }
10378
10379 int wxGrid::GetColMinimalWidth(int col) const
10380 {
10381 wxLongToLongHashMap::key_type key = (wxLongToLongHashMap::key_type)col;
10382 wxLongToLongHashMap::const_iterator it = m_colMinWidths.find(key);
10383
10384 return it != m_colMinWidths.end() ? (int)it->second : m_minAcceptableColWidth;
10385 }
10386
10387 int wxGrid::GetRowMinimalHeight(int row) const
10388 {
10389 wxLongToLongHashMap::key_type key = (wxLongToLongHashMap::key_type)row;
10390 wxLongToLongHashMap::const_iterator it = m_rowMinHeights.find(key);
10391
10392 return it != m_rowMinHeights.end() ? (int)it->second : m_minAcceptableRowHeight;
10393 }
10394
10395 void wxGrid::SetColMinimalAcceptableWidth( int width )
10396 {
10397 // We do allow a width of 0 since this gives us
10398 // an easy way to temporarily hiding columns.
10399 if ( width >= 0 )
10400 m_minAcceptableColWidth = width;
10401 }
10402
10403 void wxGrid::SetRowMinimalAcceptableHeight( int height )
10404 {
10405 // We do allow a height of 0 since this gives us
10406 // an easy way to temporarily hiding rows.
10407 if ( height >= 0 )
10408 m_minAcceptableRowHeight = height;
10409 }
10410
10411 int wxGrid::GetColMinimalAcceptableWidth() const
10412 {
10413 return m_minAcceptableColWidth;
10414 }
10415
10416 int wxGrid::GetRowMinimalAcceptableHeight() const
10417 {
10418 return m_minAcceptableRowHeight;
10419 }
10420
10421 // ----------------------------------------------------------------------------
10422 // auto sizing
10423 // ----------------------------------------------------------------------------
10424
10425 void
10426 wxGrid::AutoSizeColOrRow(int colOrRow, bool setAsMin, wxGridDirection direction)
10427 {
10428 const bool column = direction == wxGRID_COLUMN;
10429
10430 wxClientDC dc(m_gridWin);
10431
10432 // cancel editing of cell
10433 HideCellEditControl();
10434 SaveEditControlValue();
10435
10436 // init both of them to avoid compiler warnings, even if we only need one
10437 int row = -1,
10438 col = -1;
10439 if ( column )
10440 col = colOrRow;
10441 else
10442 row = colOrRow;
10443
10444 wxCoord extent, extentMax = 0;
10445 int max = column ? m_numRows : m_numCols;
10446 for ( int rowOrCol = 0; rowOrCol < max; rowOrCol++ )
10447 {
10448 if ( column )
10449 row = rowOrCol;
10450 else
10451 col = rowOrCol;
10452
10453 wxGridCellAttr *attr = GetCellAttr(row, col);
10454 wxGridCellRenderer *renderer = attr->GetRenderer(this, row, col);
10455 if ( renderer )
10456 {
10457 wxSize size = renderer->GetBestSize(*this, *attr, dc, row, col);
10458 extent = column ? size.x : size.y;
10459 if ( extent > extentMax )
10460 extentMax = extent;
10461
10462 renderer->DecRef();
10463 }
10464
10465 attr->DecRef();
10466 }
10467
10468 // now also compare with the column label extent
10469 wxCoord w, h;
10470 dc.SetFont( GetLabelFont() );
10471
10472 if ( column )
10473 {
10474 dc.GetMultiLineTextExtent( GetColLabelValue(col), &w, &h );
10475 if ( GetColLabelTextOrientation() == wxVERTICAL )
10476 w = h;
10477 }
10478 else
10479 dc.GetMultiLineTextExtent( GetRowLabelValue(row), &w, &h );
10480
10481 extent = column ? w : h;
10482 if ( extent > extentMax )
10483 extentMax = extent;
10484
10485 if ( !extentMax )
10486 {
10487 // empty column - give default extent (notice that if extentMax is less
10488 // than default extent but != 0, it's OK)
10489 extentMax = column ? m_defaultColWidth : m_defaultRowHeight;
10490 }
10491 else
10492 {
10493 if ( column )
10494 // leave some space around text
10495 extentMax += 10;
10496 else
10497 extentMax += 6;
10498 }
10499
10500 if ( column )
10501 {
10502 // Ensure automatic width is not less than minimal width. See the
10503 // comment in SetColSize() for explanation of why this isn't done
10504 // in SetColSize().
10505 if ( !setAsMin )
10506 extentMax = wxMax(extentMax, GetColMinimalWidth(col));
10507
10508 SetColSize( col, extentMax );
10509 if ( !GetBatchCount() )
10510 {
10511 int cw, ch, dummy;
10512 m_gridWin->GetClientSize( &cw, &ch );
10513 wxRect rect ( CellToRect( 0, col ) );
10514 rect.y = 0;
10515 CalcScrolledPosition(rect.x, 0, &rect.x, &dummy);
10516 rect.width = cw - rect.x;
10517 rect.height = m_colLabelHeight;
10518 m_colLabelWin->Refresh( true, &rect );
10519 }
10520 }
10521 else
10522 {
10523 // Ensure automatic width is not less than minimal height. See the
10524 // comment in SetColSize() for explanation of why this isn't done
10525 // in SetRowSize().
10526 if ( !setAsMin )
10527 extentMax = wxMax(extentMax, GetRowMinimalHeight(row));
10528
10529 SetRowSize(row, extentMax);
10530 if ( !GetBatchCount() )
10531 {
10532 int cw, ch, dummy;
10533 m_gridWin->GetClientSize( &cw, &ch );
10534 wxRect rect( CellToRect( row, 0 ) );
10535 rect.x = 0;
10536 CalcScrolledPosition(0, rect.y, &dummy, &rect.y);
10537 rect.width = m_rowLabelWidth;
10538 rect.height = ch - rect.y;
10539 m_rowLabelWin->Refresh( true, &rect );
10540 }
10541 }
10542
10543 if ( setAsMin )
10544 {
10545 if ( column )
10546 SetColMinimalWidth(col, extentMax);
10547 else
10548 SetRowMinimalHeight(row, extentMax);
10549 }
10550 }
10551
10552 wxCoord wxGrid::CalcColOrRowLabelAreaMinSize(wxGridDirection direction)
10553 {
10554 // calculate size for the rows or columns?
10555 const bool calcRows = direction == wxGRID_ROW;
10556
10557 wxClientDC dc(calcRows ? GetGridRowLabelWindow()
10558 : GetGridColLabelWindow());
10559 dc.SetFont(GetLabelFont());
10560
10561 // which dimension should we take into account for calculations?
10562 //
10563 // for columns, the text can be only horizontal so it's easy but for rows
10564 // we also have to take into account the text orientation
10565 const bool
10566 useWidth = calcRows || (GetColLabelTextOrientation() == wxVERTICAL);
10567
10568 wxArrayString lines;
10569 wxCoord extentMax = 0;
10570
10571 const int numRowsOrCols = calcRows ? m_numRows : m_numCols;
10572 for ( int rowOrCol = 0; rowOrCol < numRowsOrCols; rowOrCol++ )
10573 {
10574 lines.Clear();
10575
10576 wxString label = calcRows ? GetRowLabelValue(rowOrCol)
10577 : GetColLabelValue(rowOrCol);
10578 StringToLines(label, lines);
10579
10580 long w, h;
10581 GetTextBoxSize(dc, lines, &w, &h);
10582
10583 const wxCoord extent = useWidth ? w : h;
10584 if ( extent > extentMax )
10585 extentMax = extent;
10586 }
10587
10588 if ( !extentMax )
10589 {
10590 // empty column - give default extent (notice that if extentMax is less
10591 // than default extent but != 0, it's OK)
10592 extentMax = calcRows ? GetDefaultRowLabelSize()
10593 : GetDefaultColLabelSize();
10594 }
10595
10596 // leave some space around text (taken from AutoSizeColOrRow)
10597 if ( calcRows )
10598 extentMax += 10;
10599 else
10600 extentMax += 6;
10601
10602 return extentMax;
10603 }
10604
10605 int wxGrid::SetOrCalcColumnSizes(bool calcOnly, bool setAsMin)
10606 {
10607 int width = m_rowLabelWidth;
10608
10609 wxGridUpdateLocker locker;
10610 if(!calcOnly)
10611 locker.Create(this);
10612
10613 for ( int col = 0; col < m_numCols; col++ )
10614 {
10615 if ( !calcOnly )
10616 AutoSizeColumn(col, setAsMin);
10617
10618 width += GetColWidth(col);
10619 }
10620
10621 return width;
10622 }
10623
10624 int wxGrid::SetOrCalcRowSizes(bool calcOnly, bool setAsMin)
10625 {
10626 int height = m_colLabelHeight;
10627
10628 wxGridUpdateLocker locker;
10629 if(!calcOnly)
10630 locker.Create(this);
10631
10632 for ( int row = 0; row < m_numRows; row++ )
10633 {
10634 if ( !calcOnly )
10635 AutoSizeRow(row, setAsMin);
10636
10637 height += GetRowHeight(row);
10638 }
10639
10640 return height;
10641 }
10642
10643 void wxGrid::AutoSize()
10644 {
10645 wxGridUpdateLocker locker(this);
10646
10647 wxSize size(SetOrCalcColumnSizes(false) - m_rowLabelWidth + m_extraWidth,
10648 SetOrCalcRowSizes(false) - m_colLabelHeight + m_extraHeight);
10649
10650 // we know that we're not going to have scrollbars so disable them now to
10651 // avoid trouble in SetClientSize() which can otherwise set the correct
10652 // client size but also leave space for (not needed any more) scrollbars
10653 SetScrollbars(0, 0, 0, 0, 0, 0, true);
10654
10655 // restore the scroll rate parameters overwritten by SetScrollbars()
10656 SetScrollRate(m_scrollLineX, m_scrollLineY);
10657
10658 SetClientSize(size.x + m_rowLabelWidth, size.y + m_colLabelHeight);
10659 }
10660
10661 void wxGrid::AutoSizeRowLabelSize( int row )
10662 {
10663 // Hide the edit control, so it
10664 // won't interfere with drag-shrinking.
10665 if ( IsCellEditControlShown() )
10666 {
10667 HideCellEditControl();
10668 SaveEditControlValue();
10669 }
10670
10671 // autosize row height depending on label text
10672 SetRowSize(row, -1);
10673 ForceRefresh();
10674 }
10675
10676 void wxGrid::AutoSizeColLabelSize( int col )
10677 {
10678 // Hide the edit control, so it
10679 // won't interfere with drag-shrinking.
10680 if ( IsCellEditControlShown() )
10681 {
10682 HideCellEditControl();
10683 SaveEditControlValue();
10684 }
10685
10686 // autosize column width depending on label text
10687 SetColSize(col, -1);
10688 ForceRefresh();
10689 }
10690
10691 wxSize wxGrid::DoGetBestSize() const
10692 {
10693 wxGrid *self = (wxGrid *)this; // const_cast
10694
10695 // we do the same as in AutoSize() here with the exception that we don't
10696 // change the column/row sizes, only calculate them
10697 wxSize size(self->SetOrCalcColumnSizes(true) - m_rowLabelWidth + m_extraWidth,
10698 self->SetOrCalcRowSizes(true) - m_colLabelHeight + m_extraHeight);
10699
10700 // NOTE: This size should be cached, but first we need to add calls to
10701 // InvalidateBestSize everywhere that could change the results of this
10702 // calculation.
10703 // CacheBestSize(size);
10704
10705 return wxSize(size.x + m_rowLabelWidth, size.y + m_colLabelHeight)
10706 + GetWindowBorderSize();
10707 }
10708
10709 void wxGrid::Fit()
10710 {
10711 AutoSize();
10712 }
10713
10714 wxPen& wxGrid::GetDividerPen() const
10715 {
10716 return wxNullPen;
10717 }
10718
10719 // ----------------------------------------------------------------------------
10720 // cell value accessor functions
10721 // ----------------------------------------------------------------------------
10722
10723 void wxGrid::SetCellValue( int row, int col, const wxString& s )
10724 {
10725 if ( m_table )
10726 {
10727 m_table->SetValue( row, col, s );
10728 if ( !GetBatchCount() )
10729 {
10730 int dummy;
10731 wxRect rect( CellToRect( row, col ) );
10732 rect.x = 0;
10733 rect.width = m_gridWin->GetClientSize().GetWidth();
10734 CalcScrolledPosition(0, rect.y, &dummy, &rect.y);
10735 m_gridWin->Refresh( false, &rect );
10736 }
10737
10738 if ( m_currentCellCoords.GetRow() == row &&
10739 m_currentCellCoords.GetCol() == col &&
10740 IsCellEditControlShown())
10741 // Note: If we are using IsCellEditControlEnabled,
10742 // this interacts badly with calling SetCellValue from
10743 // an EVT_GRID_CELL_CHANGE handler.
10744 {
10745 HideCellEditControl();
10746 ShowCellEditControl(); // will reread data from table
10747 }
10748 }
10749 }
10750
10751 // ----------------------------------------------------------------------------
10752 // block, row and column selection
10753 // ----------------------------------------------------------------------------
10754
10755 void wxGrid::SelectRow( int row, bool addToSelected )
10756 {
10757 if ( IsSelection() && !addToSelected )
10758 ClearSelection();
10759
10760 if ( m_selection )
10761 m_selection->SelectRow( row, false, addToSelected );
10762 }
10763
10764 void wxGrid::SelectCol( int col, bool addToSelected )
10765 {
10766 if ( IsSelection() && !addToSelected )
10767 ClearSelection();
10768
10769 if ( m_selection )
10770 m_selection->SelectCol( col, false, addToSelected );
10771 }
10772
10773 void wxGrid::SelectBlock( int topRow, int leftCol, int bottomRow, int rightCol,
10774 bool addToSelected )
10775 {
10776 if ( IsSelection() && !addToSelected )
10777 ClearSelection();
10778
10779 if ( m_selection )
10780 m_selection->SelectBlock( topRow, leftCol, bottomRow, rightCol,
10781 false, addToSelected );
10782 }
10783
10784 void wxGrid::SelectAll()
10785 {
10786 if ( m_numRows > 0 && m_numCols > 0 )
10787 {
10788 if ( m_selection )
10789 m_selection->SelectBlock( 0, 0, m_numRows - 1, m_numCols - 1 );
10790 }
10791 }
10792
10793 // ----------------------------------------------------------------------------
10794 // cell, row and col deselection
10795 // ----------------------------------------------------------------------------
10796
10797 void wxGrid::DeselectRow( int row )
10798 {
10799 if ( !m_selection )
10800 return;
10801
10802 if ( m_selection->GetSelectionMode() == wxGrid::wxGridSelectRows )
10803 {
10804 if ( m_selection->IsInSelection(row, 0 ) )
10805 m_selection->ToggleCellSelection(row, 0);
10806 }
10807 else
10808 {
10809 int nCols = GetNumberCols();
10810 for ( int i = 0; i < nCols; i++ )
10811 {
10812 if ( m_selection->IsInSelection(row, i ) )
10813 m_selection->ToggleCellSelection(row, i);
10814 }
10815 }
10816 }
10817
10818 void wxGrid::DeselectCol( int col )
10819 {
10820 if ( !m_selection )
10821 return;
10822
10823 if ( m_selection->GetSelectionMode() == wxGrid::wxGridSelectColumns )
10824 {
10825 if ( m_selection->IsInSelection(0, col ) )
10826 m_selection->ToggleCellSelection(0, col);
10827 }
10828 else
10829 {
10830 int nRows = GetNumberRows();
10831 for ( int i = 0; i < nRows; i++ )
10832 {
10833 if ( m_selection->IsInSelection(i, col ) )
10834 m_selection->ToggleCellSelection(i, col);
10835 }
10836 }
10837 }
10838
10839 void wxGrid::DeselectCell( int row, int col )
10840 {
10841 if ( m_selection && m_selection->IsInSelection(row, col) )
10842 m_selection->ToggleCellSelection(row, col);
10843 }
10844
10845 bool wxGrid::IsSelection() const
10846 {
10847 return ( m_selection && (m_selection->IsSelection() ||
10848 ( m_selectingTopLeft != wxGridNoCellCoords &&
10849 m_selectingBottomRight != wxGridNoCellCoords) ) );
10850 }
10851
10852 bool wxGrid::IsInSelection( int row, int col ) const
10853 {
10854 return ( m_selection && (m_selection->IsInSelection( row, col ) ||
10855 ( row >= m_selectingTopLeft.GetRow() &&
10856 col >= m_selectingTopLeft.GetCol() &&
10857 row <= m_selectingBottomRight.GetRow() &&
10858 col <= m_selectingBottomRight.GetCol() )) );
10859 }
10860
10861 wxGridCellCoordsArray wxGrid::GetSelectedCells() const
10862 {
10863 if (!m_selection)
10864 {
10865 wxGridCellCoordsArray a;
10866 return a;
10867 }
10868
10869 return m_selection->m_cellSelection;
10870 }
10871
10872 wxGridCellCoordsArray wxGrid::GetSelectionBlockTopLeft() const
10873 {
10874 if (!m_selection)
10875 {
10876 wxGridCellCoordsArray a;
10877 return a;
10878 }
10879
10880 return m_selection->m_blockSelectionTopLeft;
10881 }
10882
10883 wxGridCellCoordsArray wxGrid::GetSelectionBlockBottomRight() const
10884 {
10885 if (!m_selection)
10886 {
10887 wxGridCellCoordsArray a;
10888 return a;
10889 }
10890
10891 return m_selection->m_blockSelectionBottomRight;
10892 }
10893
10894 wxArrayInt wxGrid::GetSelectedRows() const
10895 {
10896 if (!m_selection)
10897 {
10898 wxArrayInt a;
10899 return a;
10900 }
10901
10902 return m_selection->m_rowSelection;
10903 }
10904
10905 wxArrayInt wxGrid::GetSelectedCols() const
10906 {
10907 if (!m_selection)
10908 {
10909 wxArrayInt a;
10910 return a;
10911 }
10912
10913 return m_selection->m_colSelection;
10914 }
10915
10916 void wxGrid::ClearSelection()
10917 {
10918 wxRect r1 = BlockToDeviceRect( m_selectingTopLeft, m_selectingBottomRight);
10919 wxRect r2 = BlockToDeviceRect( m_currentCellCoords, m_selectingKeyboard );
10920 m_selectingTopLeft =
10921 m_selectingBottomRight =
10922 m_selectingKeyboard = wxGridNoCellCoords;
10923 Refresh( false, &r1 );
10924 Refresh( false, &r2 );
10925 if ( m_selection )
10926 m_selection->ClearSelection();
10927 }
10928
10929 // This function returns the rectangle that encloses the given block
10930 // in device coords clipped to the client size of the grid window.
10931 //
10932 wxRect wxGrid::BlockToDeviceRect( const wxGridCellCoords& topLeft,
10933 const wxGridCellCoords& bottomRight ) const
10934 {
10935 wxRect resultRect;
10936 wxRect tempCellRect = CellToRect(topLeft);
10937 if ( tempCellRect != wxGridNoCellRect )
10938 {
10939 resultRect = tempCellRect;
10940 }
10941 else
10942 {
10943 resultRect = wxRect(0, 0, 0, 0);
10944 }
10945
10946 tempCellRect = CellToRect(bottomRight);
10947 if ( tempCellRect != wxGridNoCellRect )
10948 {
10949 resultRect += tempCellRect;
10950 }
10951 else
10952 {
10953 // If both inputs were "wxGridNoCellRect," then there's nothing to do.
10954 return wxGridNoCellRect;
10955 }
10956
10957 // Ensure that left/right and top/bottom pairs are in order.
10958 int left = resultRect.GetLeft();
10959 int top = resultRect.GetTop();
10960 int right = resultRect.GetRight();
10961 int bottom = resultRect.GetBottom();
10962
10963 int leftCol = topLeft.GetCol();
10964 int topRow = topLeft.GetRow();
10965 int rightCol = bottomRight.GetCol();
10966 int bottomRow = bottomRight.GetRow();
10967
10968 if (left > right)
10969 {
10970 int tmp = left;
10971 left = right;
10972 right = tmp;
10973
10974 tmp = leftCol;
10975 leftCol = rightCol;
10976 rightCol = tmp;
10977 }
10978
10979 if (top > bottom)
10980 {
10981 int tmp = top;
10982 top = bottom;
10983 bottom = tmp;
10984
10985 tmp = topRow;
10986 topRow = bottomRow;
10987 bottomRow = tmp;
10988 }
10989
10990 // The following loop is ONLY necessary to detect and handle merged cells.
10991 int cw, ch;
10992 m_gridWin->GetClientSize( &cw, &ch );
10993
10994 // Get the origin coordinates: notice that they will be negative if the
10995 // grid is scrolled downwards/to the right.
10996 int gridOriginX = 0;
10997 int gridOriginY = 0;
10998 CalcScrolledPosition(gridOriginX, gridOriginY, &gridOriginX, &gridOriginY);
10999
11000 int onScreenLeftmostCol = internalXToCol(-gridOriginX);
11001 int onScreenUppermostRow = internalYToRow(-gridOriginY);
11002
11003 int onScreenRightmostCol = internalXToCol(-gridOriginX + cw);
11004 int onScreenBottommostRow = internalYToRow(-gridOriginY + ch);
11005
11006 // Bound our loop so that we only examine the portion of the selected block
11007 // that is shown on screen. Therefore, we compare the Top-Left block values
11008 // to the Top-Left screen values, and the Bottom-Right block values to the
11009 // Bottom-Right screen values, choosing appropriately.
11010 const int visibleTopRow = wxMax(topRow, onScreenUppermostRow);
11011 const int visibleBottomRow = wxMin(bottomRow, onScreenBottommostRow);
11012 const int visibleLeftCol = wxMax(leftCol, onScreenLeftmostCol);
11013 const int visibleRightCol = wxMin(rightCol, onScreenRightmostCol);
11014
11015 for ( int j = visibleTopRow; j <= visibleBottomRow; j++ )
11016 {
11017 for ( int i = visibleLeftCol; i <= visibleRightCol; i++ )
11018 {
11019 if ( (j == visibleTopRow) || (j == visibleBottomRow) ||
11020 (i == visibleLeftCol) || (i == visibleRightCol) )
11021 {
11022 tempCellRect = CellToRect( j, i );
11023
11024 if (tempCellRect.x < left)
11025 left = tempCellRect.x;
11026 if (tempCellRect.y < top)
11027 top = tempCellRect.y;
11028 if (tempCellRect.x + tempCellRect.width > right)
11029 right = tempCellRect.x + tempCellRect.width;
11030 if (tempCellRect.y + tempCellRect.height > bottom)
11031 bottom = tempCellRect.y + tempCellRect.height;
11032 }
11033 else
11034 {
11035 i = visibleRightCol; // jump over inner cells.
11036 }
11037 }
11038 }
11039
11040 // Convert to scrolled coords
11041 CalcScrolledPosition( left, top, &left, &top );
11042 CalcScrolledPosition( right, bottom, &right, &bottom );
11043
11044 if (right < 0 || bottom < 0 || left > cw || top > ch)
11045 return wxRect(0,0,0,0);
11046
11047 resultRect.SetLeft( wxMax(0, left) );
11048 resultRect.SetTop( wxMax(0, top) );
11049 resultRect.SetRight( wxMin(cw, right) );
11050 resultRect.SetBottom( wxMin(ch, bottom) );
11051
11052 return resultRect;
11053 }
11054
11055 // ----------------------------------------------------------------------------
11056 // drop target
11057 // ----------------------------------------------------------------------------
11058
11059 #if wxUSE_DRAG_AND_DROP
11060
11061 // this allow setting drop target directly on wxGrid
11062 void wxGrid::SetDropTarget(wxDropTarget *dropTarget)
11063 {
11064 GetGridWindow()->SetDropTarget(dropTarget);
11065 }
11066
11067 #endif // wxUSE_DRAG_AND_DROP
11068
11069 // ----------------------------------------------------------------------------
11070 // grid event classes
11071 // ----------------------------------------------------------------------------
11072
11073 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent, wxNotifyEvent )
11074
11075 wxGridEvent::wxGridEvent( int id, wxEventType type, wxObject* obj,
11076 int row, int col, int x, int y, bool sel,
11077 bool control, bool shift, bool alt, bool meta )
11078 : wxNotifyEvent( type, id )
11079 {
11080 m_row = row;
11081 m_col = col;
11082 m_x = x;
11083 m_y = y;
11084 m_selecting = sel;
11085 m_control = control;
11086 m_shift = shift;
11087 m_alt = alt;
11088 m_meta = meta;
11089
11090 SetEventObject(obj);
11091 }
11092
11093
11094 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent, wxNotifyEvent )
11095
11096 wxGridSizeEvent::wxGridSizeEvent( int id, wxEventType type, wxObject* obj,
11097 int rowOrCol, int x, int y,
11098 bool control, bool shift, bool alt, bool meta )
11099 : wxNotifyEvent( type, id )
11100 {
11101 m_rowOrCol = rowOrCol;
11102 m_x = x;
11103 m_y = y;
11104 m_control = control;
11105 m_shift = shift;
11106 m_alt = alt;
11107 m_meta = meta;
11108
11109 SetEventObject(obj);
11110 }
11111
11112
11113 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent, wxNotifyEvent )
11114
11115 wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id, wxEventType type, wxObject* obj,
11116 const wxGridCellCoords& topLeft,
11117 const wxGridCellCoords& bottomRight,
11118 bool sel, bool control,
11119 bool shift, bool alt, bool meta )
11120 : wxNotifyEvent( type, id )
11121 {
11122 m_topLeft = topLeft;
11123 m_bottomRight = bottomRight;
11124 m_selecting = sel;
11125 m_control = control;
11126 m_shift = shift;
11127 m_alt = alt;
11128 m_meta = meta;
11129
11130 SetEventObject(obj);
11131 }
11132
11133
11134 IMPLEMENT_DYNAMIC_CLASS(wxGridEditorCreatedEvent, wxCommandEvent)
11135
11136 wxGridEditorCreatedEvent::wxGridEditorCreatedEvent(int id, wxEventType type,
11137 wxObject* obj, int row,
11138 int col, wxControl* ctrl)
11139 : wxCommandEvent(type, id)
11140 {
11141 SetEventObject(obj);
11142 m_row = row;
11143 m_col = col;
11144 m_ctrl = ctrl;
11145 }
11146
11147 #endif // wxUSE_GRID