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