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