]> git.saurik.com Git - wxWidgets.git/blob - src/generic/grid.cpp
was incorrectly forcing the font to 12 in most cases, fixes #4745
[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 ( m_cursorMode == WXGRID_CURSOR_RESIZE_ROW )
6024 {
6025 int cw, ch, left, dummy;
6026 m_gridWin->GetClientSize( &cw, &ch );
6027 CalcUnscrolledPosition( 0, 0, &left, &dummy );
6028
6029 wxClientDC dc( m_gridWin );
6030 PrepareDC( dc );
6031 y = wxMax( y, GetRowTop(m_dragRowOrCol) +
6032 GetRowMinimalHeight(m_dragRowOrCol) );
6033 dc.SetLogicalFunction(wxINVERT);
6034 if ( m_dragLastPos >= 0 )
6035 {
6036 dc.DrawLine( left, m_dragLastPos, left+cw, m_dragLastPos );
6037 }
6038 dc.DrawLine( left, y, left+cw, y );
6039 m_dragLastPos = y;
6040 }
6041 else if ( m_cursorMode == WXGRID_CURSOR_RESIZE_COL )
6042 {
6043 int cw, ch, dummy, top;
6044 m_gridWin->GetClientSize( &cw, &ch );
6045 CalcUnscrolledPosition( 0, 0, &dummy, &top );
6046
6047 wxClientDC dc( m_gridWin );
6048 PrepareDC( dc );
6049 x = wxMax( x, GetColLeft(m_dragRowOrCol) +
6050 GetColMinimalWidth(m_dragRowOrCol) );
6051 dc.SetLogicalFunction(wxINVERT);
6052 if ( m_dragLastPos >= 0 )
6053 {
6054 dc.DrawLine( m_dragLastPos, top, m_dragLastPos, top + ch );
6055 }
6056 dc.DrawLine( x, top, x, top + ch );
6057 m_dragLastPos = x;
6058 }
6059
6060 return;
6061 }
6062
6063 m_isDragging = false;
6064 m_startDragPos = wxDefaultPosition;
6065
6066 // VZ: if we do this, the mode is reset to WXGRID_CURSOR_SELECT_CELL
6067 // immediately after it becomes WXGRID_CURSOR_RESIZE_ROW/COL under
6068 // wxGTK
6069 #if 0
6070 if ( event.Entering() || event.Leaving() )
6071 {
6072 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL);
6073 m_gridWin->SetCursor( *wxSTANDARD_CURSOR );
6074 }
6075 else
6076 #endif // 0
6077
6078 // ------------ Left button pressed
6079 //
6080 if ( event.LeftDown() && coords != wxGridNoCellCoords )
6081 {
6082 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_CLICK,
6083 coords.GetRow(),
6084 coords.GetCol(),
6085 event ) )
6086 {
6087 if ( !event.CmdDown() )
6088 ClearSelection();
6089 if ( event.ShiftDown() )
6090 {
6091 if ( m_selection )
6092 {
6093 m_selection->SelectBlock( m_currentCellCoords.GetRow(),
6094 m_currentCellCoords.GetCol(),
6095 coords.GetRow(),
6096 coords.GetCol(),
6097 event.ControlDown(),
6098 event.ShiftDown(),
6099 event.AltDown(),
6100 event.MetaDown() );
6101 }
6102 }
6103 else if ( XToEdgeOfCol(x) < 0 &&
6104 YToEdgeOfRow(y) < 0 )
6105 {
6106 DisableCellEditControl();
6107 MakeCellVisible( coords );
6108
6109 if ( event.CmdDown() )
6110 {
6111 if ( m_selection )
6112 {
6113 m_selection->ToggleCellSelection( coords.GetRow(),
6114 coords.GetCol(),
6115 event.ControlDown(),
6116 event.ShiftDown(),
6117 event.AltDown(),
6118 event.MetaDown() );
6119 }
6120 m_selectingTopLeft = wxGridNoCellCoords;
6121 m_selectingBottomRight = wxGridNoCellCoords;
6122 m_selectingKeyboard = coords;
6123 }
6124 else
6125 {
6126 m_waitForSlowClick = m_currentCellCoords == coords && coords != wxGridNoCellCoords;
6127 SetCurrentCell( coords );
6128 if ( m_selection )
6129 {
6130 if ( m_selection->GetSelectionMode() !=
6131 wxGrid::wxGridSelectCells )
6132 {
6133 HighlightBlock( coords, coords );
6134 }
6135 }
6136 }
6137 }
6138 }
6139 }
6140
6141 // ------------ Left double click
6142 //
6143 else if ( event.LeftDClick() && coords != wxGridNoCellCoords )
6144 {
6145 DisableCellEditControl();
6146
6147 if ( XToEdgeOfCol(x) < 0 && YToEdgeOfRow(y) < 0 )
6148 {
6149 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_DCLICK,
6150 coords.GetRow(),
6151 coords.GetCol(),
6152 event ) )
6153 {
6154 // we want double click to select a cell and start editing
6155 // (i.e. to behave in same way as sequence of two slow clicks):
6156 m_waitForSlowClick = true;
6157 }
6158 }
6159 }
6160
6161 // ------------ Left button released
6162 //
6163 else if ( event.LeftUp() )
6164 {
6165 if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
6166 {
6167 if (m_winCapture)
6168 {
6169 if (m_winCapture->HasCapture())
6170 m_winCapture->ReleaseMouse();
6171 m_winCapture = NULL;
6172 }
6173
6174 if ( coords == m_currentCellCoords && m_waitForSlowClick && CanEnableCellControl() )
6175 {
6176 ClearSelection();
6177 EnableCellEditControl();
6178
6179 wxGridCellAttr *attr = GetCellAttr(coords);
6180 wxGridCellEditor *editor = attr->GetEditor(this, coords.GetRow(), coords.GetCol());
6181 editor->StartingClick();
6182 editor->DecRef();
6183 attr->DecRef();
6184
6185 m_waitForSlowClick = false;
6186 }
6187 else if ( m_selectingTopLeft != wxGridNoCellCoords &&
6188 m_selectingBottomRight != wxGridNoCellCoords )
6189 {
6190 if ( m_selection )
6191 {
6192 m_selection->SelectBlock( m_selectingTopLeft.GetRow(),
6193 m_selectingTopLeft.GetCol(),
6194 m_selectingBottomRight.GetRow(),
6195 m_selectingBottomRight.GetCol(),
6196 event.ControlDown(),
6197 event.ShiftDown(),
6198 event.AltDown(),
6199 event.MetaDown() );
6200 }
6201
6202 m_selectingTopLeft = wxGridNoCellCoords;
6203 m_selectingBottomRight = wxGridNoCellCoords;
6204
6205 // Show the edit control, if it has been hidden for
6206 // drag-shrinking.
6207 ShowCellEditControl();
6208 }
6209 }
6210 else if ( m_cursorMode == WXGRID_CURSOR_RESIZE_ROW )
6211 {
6212 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL);
6213 DoEndDragResizeRow();
6214
6215 // Note: we are ending the event *after* doing
6216 // default processing in this case
6217 //
6218 SendEvent( wxEVT_GRID_ROW_SIZE, m_dragRowOrCol, -1, event );
6219 }
6220 else if ( m_cursorMode == WXGRID_CURSOR_RESIZE_COL )
6221 {
6222 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL);
6223 DoEndDragResizeCol();
6224
6225 // Note: we are ending the event *after* doing
6226 // default processing in this case
6227 //
6228 SendEvent( wxEVT_GRID_COL_SIZE, -1, m_dragRowOrCol, event );
6229 }
6230
6231 m_dragLastPos = -1;
6232 }
6233
6234 // ------------ Right button down
6235 //
6236 else if ( event.RightDown() && coords != wxGridNoCellCoords )
6237 {
6238 DisableCellEditControl();
6239 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_CLICK,
6240 coords.GetRow(),
6241 coords.GetCol(),
6242 event ) )
6243 {
6244 // no default action at the moment
6245 }
6246 }
6247
6248 // ------------ Right double click
6249 //
6250 else if ( event.RightDClick() && coords != wxGridNoCellCoords )
6251 {
6252 DisableCellEditControl();
6253 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_DCLICK,
6254 coords.GetRow(),
6255 coords.GetCol(),
6256 event ) )
6257 {
6258 // no default action at the moment
6259 }
6260 }
6261
6262 // ------------ Moving and no button action
6263 //
6264 else if ( event.Moving() && !event.IsButton() )
6265 {
6266 if ( coords.GetRow() < 0 || coords.GetCol() < 0 )
6267 {
6268 // out of grid cell area
6269 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL);
6270 return;
6271 }
6272
6273 int dragRow = YToEdgeOfRow( y );
6274 int dragCol = XToEdgeOfCol( x );
6275
6276 // Dragging on the corner of a cell to resize in both
6277 // directions is not implemented yet...
6278 //
6279 if ( dragRow >= 0 && dragCol >= 0 )
6280 {
6281 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL);
6282 return;
6283 }
6284
6285 if ( dragRow >= 0 )
6286 {
6287 m_dragRowOrCol = dragRow;
6288
6289 if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
6290 {
6291 if ( CanDragRowSize() && CanDragGridSize() )
6292 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW, NULL, false);
6293 }
6294 }
6295 else if ( dragCol >= 0 )
6296 {
6297 m_dragRowOrCol = dragCol;
6298
6299 if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
6300 {
6301 if ( CanDragColSize() && CanDragGridSize() )
6302 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL, NULL, false);
6303 }
6304 }
6305 else // Neither on a row or col edge
6306 {
6307 if ( m_cursorMode != WXGRID_CURSOR_SELECT_CELL )
6308 {
6309 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL);
6310 }
6311 }
6312 }
6313 }
6314
6315 void wxGrid::DoEndDragResizeRow()
6316 {
6317 if ( m_dragLastPos >= 0 )
6318 {
6319 // erase the last line and resize the row
6320 //
6321 int cw, ch, left, dummy;
6322 m_gridWin->GetClientSize( &cw, &ch );
6323 CalcUnscrolledPosition( 0, 0, &left, &dummy );
6324
6325 wxClientDC dc( m_gridWin );
6326 PrepareDC( dc );
6327 dc.SetLogicalFunction( wxINVERT );
6328 dc.DrawLine( left, m_dragLastPos, left + cw, m_dragLastPos );
6329 HideCellEditControl();
6330 SaveEditControlValue();
6331
6332 int rowTop = GetRowTop(m_dragRowOrCol);
6333 SetRowSize( m_dragRowOrCol,
6334 wxMax( m_dragLastPos - rowTop, m_minAcceptableRowHeight ) );
6335
6336 if ( !GetBatchCount() )
6337 {
6338 // Only needed to get the correct rect.y:
6339 wxRect rect ( CellToRect( m_dragRowOrCol, 0 ) );
6340 rect.x = 0;
6341 CalcScrolledPosition(0, rect.y, &dummy, &rect.y);
6342 rect.width = m_rowLabelWidth;
6343 rect.height = ch - rect.y;
6344 m_rowLabelWin->Refresh( true, &rect );
6345 rect.width = cw;
6346
6347 // if there is a multicell block, paint all of it
6348 if (m_table)
6349 {
6350 int i, cell_rows, cell_cols, subtract_rows = 0;
6351 int leftCol = XToCol(left);
6352 int rightCol = internalXToCol(left + cw);
6353 if (leftCol >= 0)
6354 {
6355 for (i=leftCol; i<rightCol; i++)
6356 {
6357 GetCellSize(m_dragRowOrCol, i, &cell_rows, &cell_cols);
6358 if (cell_rows < subtract_rows)
6359 subtract_rows = cell_rows;
6360 }
6361 rect.y = GetRowTop(m_dragRowOrCol + subtract_rows);
6362 CalcScrolledPosition(0, rect.y, &dummy, &rect.y);
6363 rect.height = ch - rect.y;
6364 }
6365 }
6366 m_gridWin->Refresh( false, &rect );
6367 }
6368
6369 ShowCellEditControl();
6370 }
6371 }
6372
6373
6374 void wxGrid::DoEndDragResizeCol()
6375 {
6376 if ( m_dragLastPos >= 0 )
6377 {
6378 // erase the last line and resize the col
6379 //
6380 int cw, ch, dummy, top;
6381 m_gridWin->GetClientSize( &cw, &ch );
6382 CalcUnscrolledPosition( 0, 0, &dummy, &top );
6383
6384 wxClientDC dc( m_gridWin );
6385 PrepareDC( dc );
6386 dc.SetLogicalFunction( wxINVERT );
6387 dc.DrawLine( m_dragLastPos, top, m_dragLastPos, top + ch );
6388 HideCellEditControl();
6389 SaveEditControlValue();
6390
6391 int colLeft = GetColLeft(m_dragRowOrCol);
6392 SetColSize( m_dragRowOrCol,
6393 wxMax( m_dragLastPos - colLeft,
6394 GetColMinimalWidth(m_dragRowOrCol) ) );
6395
6396 if ( !GetBatchCount() )
6397 {
6398 // Only needed to get the correct rect.x:
6399 wxRect rect ( CellToRect( 0, m_dragRowOrCol ) );
6400 rect.y = 0;
6401 CalcScrolledPosition(rect.x, 0, &rect.x, &dummy);
6402 rect.width = cw - rect.x;
6403 rect.height = m_colLabelHeight;
6404 m_colLabelWin->Refresh( true, &rect );
6405 rect.height = ch;
6406
6407 // if there is a multicell block, paint all of it
6408 if (m_table)
6409 {
6410 int i, cell_rows, cell_cols, subtract_cols = 0;
6411 int topRow = YToRow(top);
6412 int bottomRow = internalYToRow(top + cw);
6413 if (topRow >= 0)
6414 {
6415 for (i=topRow; i<bottomRow; i++)
6416 {
6417 GetCellSize(i, m_dragRowOrCol, &cell_rows, &cell_cols);
6418 if (cell_cols < subtract_cols)
6419 subtract_cols = cell_cols;
6420 }
6421
6422 rect.x = GetColLeft(m_dragRowOrCol + subtract_cols);
6423 CalcScrolledPosition(rect.x, 0, &rect.x, &dummy);
6424 rect.width = cw - rect.x;
6425 }
6426 }
6427
6428 m_gridWin->Refresh( false, &rect );
6429 }
6430
6431 ShowCellEditControl();
6432 }
6433 }
6434
6435 void wxGrid::DoEndDragMoveCol()
6436 {
6437 //The user clicked on the column but didn't actually drag
6438 if ( m_dragLastPos < 0 )
6439 {
6440 m_colLabelWin->Refresh(); //Do this to "unpress" the column
6441 return;
6442 }
6443
6444 int newPos;
6445 if ( m_moveToCol == -1 )
6446 newPos = m_numCols - 1;
6447 else
6448 {
6449 newPos = GetColPos( m_moveToCol );
6450 if ( newPos > GetColPos( m_dragRowOrCol ) )
6451 newPos--;
6452 }
6453
6454 SetColPos( m_dragRowOrCol, newPos );
6455 }
6456
6457 void wxGrid::SetColPos( int colID, int newPos )
6458 {
6459 if ( m_colAt.IsEmpty() )
6460 {
6461 m_colAt.Alloc( m_numCols );
6462
6463 int i;
6464 for ( i = 0; i < m_numCols; i++ )
6465 {
6466 m_colAt.Add( i );
6467 }
6468 }
6469
6470 int oldPos = GetColPos( colID );
6471
6472 //Reshuffle the m_colAt array
6473 if ( newPos > oldPos )
6474 {
6475 int i;
6476 for ( i = oldPos; i < newPos; i++ )
6477 {
6478 m_colAt[i] = m_colAt[i+1];
6479 }
6480 }
6481 else
6482 {
6483 int i;
6484 for ( i = oldPos; i > newPos; i-- )
6485 {
6486 m_colAt[i] = m_colAt[i-1];
6487 }
6488 }
6489
6490 m_colAt[newPos] = colID;
6491
6492 //Recalculate the column rights
6493 if ( !m_colWidths.IsEmpty() )
6494 {
6495 int colRight = 0;
6496 int colPos;
6497 for ( colPos = 0; colPos < m_numCols; colPos++ )
6498 {
6499 int colID = GetColAt( colPos );
6500
6501 colRight += m_colWidths[colID];
6502 m_colRights[colID] = colRight;
6503 }
6504 }
6505
6506 m_colLabelWin->Refresh();
6507 m_gridWin->Refresh();
6508 }
6509
6510
6511
6512 void wxGrid::EnableDragColMove( bool enable )
6513 {
6514 if ( m_canDragColMove == enable )
6515 return;
6516
6517 m_canDragColMove = enable;
6518
6519 if ( !m_canDragColMove )
6520 {
6521 m_colAt.Clear();
6522
6523 //Recalculate the column rights
6524 if ( !m_colWidths.IsEmpty() )
6525 {
6526 int colRight = 0;
6527 int colPos;
6528 for ( colPos = 0; colPos < m_numCols; colPos++ )
6529 {
6530 colRight += m_colWidths[colPos];
6531 m_colRights[colPos] = colRight;
6532 }
6533 }
6534
6535 m_colLabelWin->Refresh();
6536 m_gridWin->Refresh();
6537 }
6538 }
6539
6540
6541 //
6542 // ------ interaction with data model
6543 //
6544 bool wxGrid::ProcessTableMessage( wxGridTableMessage& msg )
6545 {
6546 switch ( msg.GetId() )
6547 {
6548 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES:
6549 return GetModelValues();
6550
6551 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES:
6552 return SetModelValues();
6553
6554 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED:
6555 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED:
6556 case wxGRIDTABLE_NOTIFY_ROWS_DELETED:
6557 case wxGRIDTABLE_NOTIFY_COLS_INSERTED:
6558 case wxGRIDTABLE_NOTIFY_COLS_APPENDED:
6559 case wxGRIDTABLE_NOTIFY_COLS_DELETED:
6560 return Redimension( msg );
6561
6562 default:
6563 return false;
6564 }
6565 }
6566
6567 // The behaviour of this function depends on the grid table class
6568 // Clear() function. For the default wxGridStringTable class the
6569 // behavious is to replace all cell contents with wxEmptyString but
6570 // not to change the number of rows or cols.
6571 //
6572 void wxGrid::ClearGrid()
6573 {
6574 if ( m_table )
6575 {
6576 if (IsCellEditControlEnabled())
6577 DisableCellEditControl();
6578
6579 m_table->Clear();
6580 if (!GetBatchCount())
6581 m_gridWin->Refresh();
6582 }
6583 }
6584
6585 bool wxGrid::InsertRows( int pos, int numRows, bool WXUNUSED(updateLabels) )
6586 {
6587 // TODO: something with updateLabels flag
6588
6589 if ( !m_created )
6590 {
6591 wxFAIL_MSG( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") );
6592 return false;
6593 }
6594
6595 if ( m_table )
6596 {
6597 if (IsCellEditControlEnabled())
6598 DisableCellEditControl();
6599
6600 bool done = m_table->InsertRows( pos, numRows );
6601 return done;
6602
6603 // the table will have sent the results of the insert row
6604 // operation to this view object as a grid table message
6605 }
6606
6607 return false;
6608 }
6609
6610 bool wxGrid::AppendRows( int numRows, bool WXUNUSED(updateLabels) )
6611 {
6612 // TODO: something with updateLabels flag
6613
6614 if ( !m_created )
6615 {
6616 wxFAIL_MSG( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") );
6617 return false;
6618 }
6619
6620 if ( m_table )
6621 {
6622 bool done = m_table && m_table->AppendRows( numRows );
6623 return done;
6624
6625 // the table will have sent the results of the append row
6626 // operation to this view object as a grid table message
6627 }
6628
6629 return false;
6630 }
6631
6632 bool wxGrid::DeleteRows( int pos, int numRows, bool WXUNUSED(updateLabels) )
6633 {
6634 // TODO: something with updateLabels flag
6635
6636 if ( !m_created )
6637 {
6638 wxFAIL_MSG( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") );
6639 return false;
6640 }
6641
6642 if ( m_table )
6643 {
6644 if (IsCellEditControlEnabled())
6645 DisableCellEditControl();
6646
6647 bool done = m_table->DeleteRows( pos, numRows );
6648 return done;
6649 // the table will have sent the results of the delete row
6650 // operation to this view object as a grid table message
6651 }
6652
6653 return false;
6654 }
6655
6656 bool wxGrid::InsertCols( int pos, int numCols, bool WXUNUSED(updateLabels) )
6657 {
6658 // TODO: something with updateLabels flag
6659
6660 if ( !m_created )
6661 {
6662 wxFAIL_MSG( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") );
6663 return false;
6664 }
6665
6666 if ( m_table )
6667 {
6668 if (IsCellEditControlEnabled())
6669 DisableCellEditControl();
6670
6671 bool done = m_table->InsertCols( pos, numCols );
6672 return done;
6673 // the table will have sent the results of the insert col
6674 // operation to this view object as a grid table message
6675 }
6676
6677 return false;
6678 }
6679
6680 bool wxGrid::AppendCols( int numCols, bool WXUNUSED(updateLabels) )
6681 {
6682 // TODO: something with updateLabels flag
6683
6684 if ( !m_created )
6685 {
6686 wxFAIL_MSG( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") );
6687 return false;
6688 }
6689
6690 if ( m_table )
6691 {
6692 bool done = m_table->AppendCols( numCols );
6693 return done;
6694 // the table will have sent the results of the append col
6695 // operation to this view object as a grid table message
6696 }
6697
6698 return false;
6699 }
6700
6701 bool wxGrid::DeleteCols( int pos, int numCols, bool WXUNUSED(updateLabels) )
6702 {
6703 // TODO: something with updateLabels flag
6704
6705 if ( !m_created )
6706 {
6707 wxFAIL_MSG( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") );
6708 return false;
6709 }
6710
6711 if ( m_table )
6712 {
6713 if (IsCellEditControlEnabled())
6714 DisableCellEditControl();
6715
6716 bool done = m_table->DeleteCols( pos, numCols );
6717 return done;
6718 // the table will have sent the results of the delete col
6719 // operation to this view object as a grid table message
6720 }
6721
6722 return false;
6723 }
6724
6725 //
6726 // ----- event handlers
6727 //
6728
6729 // Generate a grid event based on a mouse event and
6730 // return the result of ProcessEvent()
6731 //
6732 int wxGrid::SendEvent( const wxEventType type,
6733 int row, int col,
6734 wxMouseEvent& mouseEv )
6735 {
6736 bool claimed, vetoed;
6737
6738 if ( type == wxEVT_GRID_ROW_SIZE || type == wxEVT_GRID_COL_SIZE )
6739 {
6740 int rowOrCol = (row == -1 ? col : row);
6741
6742 wxGridSizeEvent gridEvt( GetId(),
6743 type,
6744 this,
6745 rowOrCol,
6746 mouseEv.GetX() + GetRowLabelSize(),
6747 mouseEv.GetY() + GetColLabelSize(),
6748 mouseEv.ControlDown(),
6749 mouseEv.ShiftDown(),
6750 mouseEv.AltDown(),
6751 mouseEv.MetaDown() );
6752
6753 claimed = GetEventHandler()->ProcessEvent(gridEvt);
6754 vetoed = !gridEvt.IsAllowed();
6755 }
6756 else if ( type == wxEVT_GRID_RANGE_SELECT )
6757 {
6758 // Right now, it should _never_ end up here!
6759 wxGridRangeSelectEvent gridEvt( GetId(),
6760 type,
6761 this,
6762 m_selectingTopLeft,
6763 m_selectingBottomRight,
6764 true,
6765 mouseEv.ControlDown(),
6766 mouseEv.ShiftDown(),
6767 mouseEv.AltDown(),
6768 mouseEv.MetaDown() );
6769
6770 claimed = GetEventHandler()->ProcessEvent(gridEvt);
6771 vetoed = !gridEvt.IsAllowed();
6772 }
6773 else if ( type == wxEVT_GRID_LABEL_LEFT_CLICK ||
6774 type == wxEVT_GRID_LABEL_LEFT_DCLICK ||
6775 type == wxEVT_GRID_LABEL_RIGHT_CLICK ||
6776 type == wxEVT_GRID_LABEL_RIGHT_DCLICK )
6777 {
6778 wxPoint pos = mouseEv.GetPosition();
6779
6780 if ( mouseEv.GetEventObject() == GetGridRowLabelWindow() )
6781 pos.y += GetColLabelSize();
6782 if ( mouseEv.GetEventObject() == GetGridColLabelWindow() )
6783 pos.x += GetRowLabelSize();
6784
6785 wxGridEvent gridEvt( GetId(),
6786 type,
6787 this,
6788 row, col,
6789 pos.x,
6790 pos.y,
6791 false,
6792 mouseEv.ControlDown(),
6793 mouseEv.ShiftDown(),
6794 mouseEv.AltDown(),
6795 mouseEv.MetaDown() );
6796 claimed = GetEventHandler()->ProcessEvent(gridEvt);
6797 vetoed = !gridEvt.IsAllowed();
6798 }
6799 else
6800 {
6801 wxGridEvent gridEvt( GetId(),
6802 type,
6803 this,
6804 row, col,
6805 mouseEv.GetX() + GetRowLabelSize(),
6806 mouseEv.GetY() + GetColLabelSize(),
6807 false,
6808 mouseEv.ControlDown(),
6809 mouseEv.ShiftDown(),
6810 mouseEv.AltDown(),
6811 mouseEv.MetaDown() );
6812 claimed = GetEventHandler()->ProcessEvent(gridEvt);
6813 vetoed = !gridEvt.IsAllowed();
6814 }
6815
6816 // A Veto'd event may not be `claimed' so test this first
6817 if (vetoed)
6818 return -1;
6819
6820 return claimed ? 1 : 0;
6821 }
6822
6823 // Generate a grid event of specified type and return the result
6824 // of ProcessEvent().
6825 //
6826 int wxGrid::SendEvent( const wxEventType type,
6827 int row, int col )
6828 {
6829 bool claimed, vetoed;
6830
6831 if ( type == wxEVT_GRID_ROW_SIZE || type == wxEVT_GRID_COL_SIZE )
6832 {
6833 int rowOrCol = (row == -1 ? col : row);
6834
6835 wxGridSizeEvent gridEvt( GetId(), type, this, rowOrCol );
6836
6837 claimed = GetEventHandler()->ProcessEvent(gridEvt);
6838 vetoed = !gridEvt.IsAllowed();
6839 }
6840 else
6841 {
6842 wxGridEvent gridEvt( GetId(), type, this, row, col );
6843
6844 claimed = GetEventHandler()->ProcessEvent(gridEvt);
6845 vetoed = !gridEvt.IsAllowed();
6846 }
6847
6848 // A Veto'd event may not be `claimed' so test this first
6849 if (vetoed)
6850 return -1;
6851
6852 return claimed ? 1 : 0;
6853 }
6854
6855 void wxGrid::OnPaint( wxPaintEvent& WXUNUSED(event) )
6856 {
6857 // needed to prevent zillions of paint events on MSW
6858 wxPaintDC dc(this);
6859 }
6860
6861 void wxGrid::Refresh(bool eraseb, const wxRect* rect)
6862 {
6863 // Don't do anything if between Begin/EndBatch...
6864 // EndBatch() will do all this on the last nested one anyway.
6865 if ( m_created && !GetBatchCount() )
6866 {
6867 // Refresh to get correct scrolled position:
6868 wxScrolledWindow::Refresh(eraseb, rect);
6869
6870 if (rect)
6871 {
6872 int rect_x, rect_y, rectWidth, rectHeight;
6873 int width_label, width_cell, height_label, height_cell;
6874 int x, y;
6875
6876 // Copy rectangle can get scroll offsets..
6877 rect_x = rect->GetX();
6878 rect_y = rect->GetY();
6879 rectWidth = rect->GetWidth();
6880 rectHeight = rect->GetHeight();
6881
6882 width_label = m_rowLabelWidth - rect_x;
6883 if (width_label > rectWidth)
6884 width_label = rectWidth;
6885
6886 height_label = m_colLabelHeight - rect_y;
6887 if (height_label > rectHeight)
6888 height_label = rectHeight;
6889
6890 if (rect_x > m_rowLabelWidth)
6891 {
6892 x = rect_x - m_rowLabelWidth;
6893 width_cell = rectWidth;
6894 }
6895 else
6896 {
6897 x = 0;
6898 width_cell = rectWidth - (m_rowLabelWidth - rect_x);
6899 }
6900
6901 if (rect_y > m_colLabelHeight)
6902 {
6903 y = rect_y - m_colLabelHeight;
6904 height_cell = rectHeight;
6905 }
6906 else
6907 {
6908 y = 0;
6909 height_cell = rectHeight - (m_colLabelHeight - rect_y);
6910 }
6911
6912 // Paint corner label part intersecting rect.
6913 if ( width_label > 0 && height_label > 0 )
6914 {
6915 wxRect anotherrect(rect_x, rect_y, width_label, height_label);
6916 m_cornerLabelWin->Refresh(eraseb, &anotherrect);
6917 }
6918
6919 // Paint col labels part intersecting rect.
6920 if ( width_cell > 0 && height_label > 0 )
6921 {
6922 wxRect anotherrect(x, rect_y, width_cell, height_label);
6923 m_colLabelWin->Refresh(eraseb, &anotherrect);
6924 }
6925
6926 // Paint row labels part intersecting rect.
6927 if ( width_label > 0 && height_cell > 0 )
6928 {
6929 wxRect anotherrect(rect_x, y, width_label, height_cell);
6930 m_rowLabelWin->Refresh(eraseb, &anotherrect);
6931 }
6932
6933 // Paint cell area part intersecting rect.
6934 if ( width_cell > 0 && height_cell > 0 )
6935 {
6936 wxRect anotherrect(x, y, width_cell, height_cell);
6937 m_gridWin->Refresh(eraseb, &anotherrect);
6938 }
6939 }
6940 else
6941 {
6942 m_cornerLabelWin->Refresh(eraseb, NULL);
6943 m_colLabelWin->Refresh(eraseb, NULL);
6944 m_rowLabelWin->Refresh(eraseb, NULL);
6945 m_gridWin->Refresh(eraseb, NULL);
6946 }
6947 }
6948 }
6949
6950 void wxGrid::OnSize(wxSizeEvent& WXUNUSED(event))
6951 {
6952 if (m_targetWindow != this) // check whether initialisation has been done
6953 {
6954 // update our children window positions and scrollbars
6955 CalcDimensions();
6956 }
6957 }
6958
6959 void wxGrid::OnKeyDown( wxKeyEvent& event )
6960 {
6961 if ( m_inOnKeyDown )
6962 {
6963 // shouldn't be here - we are going round in circles...
6964 //
6965 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") );
6966 }
6967
6968 m_inOnKeyDown = true;
6969
6970 // propagate the event up and see if it gets processed
6971 wxWindow *parent = GetParent();
6972 wxKeyEvent keyEvt( event );
6973 keyEvt.SetEventObject( parent );
6974
6975 if ( !parent->GetEventHandler()->ProcessEvent( keyEvt ) )
6976 {
6977 if (GetLayoutDirection() == wxLayout_RightToLeft)
6978 {
6979 if (event.GetKeyCode() == WXK_RIGHT)
6980 event.m_keyCode = WXK_LEFT;
6981 else if (event.GetKeyCode() == WXK_LEFT)
6982 event.m_keyCode = WXK_RIGHT;
6983 }
6984
6985 // try local handlers
6986 switch ( event.GetKeyCode() )
6987 {
6988 case WXK_UP:
6989 if ( event.ControlDown() )
6990 MoveCursorUpBlock( event.ShiftDown() );
6991 else
6992 MoveCursorUp( event.ShiftDown() );
6993 break;
6994
6995 case WXK_DOWN:
6996 if ( event.ControlDown() )
6997 MoveCursorDownBlock( event.ShiftDown() );
6998 else
6999 MoveCursorDown( event.ShiftDown() );
7000 break;
7001
7002 case WXK_LEFT:
7003 if ( event.ControlDown() )
7004 MoveCursorLeftBlock( event.ShiftDown() );
7005 else
7006 MoveCursorLeft( event.ShiftDown() );
7007 break;
7008
7009 case WXK_RIGHT:
7010 if ( event.ControlDown() )
7011 MoveCursorRightBlock( event.ShiftDown() );
7012 else
7013 MoveCursorRight( event.ShiftDown() );
7014 break;
7015
7016 case WXK_RETURN:
7017 case WXK_NUMPAD_ENTER:
7018 if ( event.ControlDown() )
7019 {
7020 event.Skip(); // to let the edit control have the return
7021 }
7022 else
7023 {
7024 if ( GetGridCursorRow() < GetNumberRows()-1 )
7025 {
7026 MoveCursorDown( event.ShiftDown() );
7027 }
7028 else
7029 {
7030 // at the bottom of a column
7031 DisableCellEditControl();
7032 }
7033 }
7034 break;
7035
7036 case WXK_ESCAPE:
7037 ClearSelection();
7038 break;
7039
7040 case WXK_TAB:
7041 if (event.ShiftDown())
7042 {
7043 if ( GetGridCursorCol() > 0 )
7044 {
7045 MoveCursorLeft( false );
7046 }
7047 else
7048 {
7049 // at left of grid
7050 DisableCellEditControl();
7051 }
7052 }
7053 else
7054 {
7055 if ( GetGridCursorCol() < GetNumberCols() - 1 )
7056 {
7057 MoveCursorRight( false );
7058 }
7059 else
7060 {
7061 // at right of grid
7062 DisableCellEditControl();
7063 }
7064 }
7065 break;
7066
7067 case WXK_HOME:
7068 if ( event.ControlDown() )
7069 {
7070 MakeCellVisible( 0, 0 );
7071 SetCurrentCell( 0, 0 );
7072 }
7073 else
7074 {
7075 event.Skip();
7076 }
7077 break;
7078
7079 case WXK_END:
7080 if ( event.ControlDown() )
7081 {
7082 MakeCellVisible( m_numRows - 1, m_numCols - 1 );
7083 SetCurrentCell( m_numRows - 1, m_numCols - 1 );
7084 }
7085 else
7086 {
7087 event.Skip();
7088 }
7089 break;
7090
7091 case WXK_PAGEUP:
7092 MovePageUp();
7093 break;
7094
7095 case WXK_PAGEDOWN:
7096 MovePageDown();
7097 break;
7098
7099 case WXK_SPACE:
7100 if ( event.ControlDown() )
7101 {
7102 if ( m_selection )
7103 {
7104 m_selection->ToggleCellSelection(
7105 m_currentCellCoords.GetRow(),
7106 m_currentCellCoords.GetCol(),
7107 event.ControlDown(),
7108 event.ShiftDown(),
7109 event.AltDown(),
7110 event.MetaDown() );
7111 }
7112 break;
7113 }
7114
7115 if ( !IsEditable() )
7116 MoveCursorRight( false );
7117 else
7118 event.Skip();
7119 break;
7120
7121 default:
7122 event.Skip();
7123 break;
7124 }
7125 }
7126
7127 m_inOnKeyDown = false;
7128 }
7129
7130 void wxGrid::OnKeyUp( wxKeyEvent& event )
7131 {
7132 // try local handlers
7133 //
7134 if ( event.GetKeyCode() == WXK_SHIFT )
7135 {
7136 if ( m_selectingTopLeft != wxGridNoCellCoords &&
7137 m_selectingBottomRight != wxGridNoCellCoords )
7138 {
7139 if ( m_selection )
7140 {
7141 m_selection->SelectBlock(
7142 m_selectingTopLeft.GetRow(),
7143 m_selectingTopLeft.GetCol(),
7144 m_selectingBottomRight.GetRow(),
7145 m_selectingBottomRight.GetCol(),
7146 event.ControlDown(),
7147 true,
7148 event.AltDown(),
7149 event.MetaDown() );
7150 }
7151 }
7152
7153 m_selectingTopLeft = wxGridNoCellCoords;
7154 m_selectingBottomRight = wxGridNoCellCoords;
7155 m_selectingKeyboard = wxGridNoCellCoords;
7156 }
7157 }
7158
7159 void wxGrid::OnChar( wxKeyEvent& event )
7160 {
7161 // is it possible to edit the current cell at all?
7162 if ( !IsCellEditControlEnabled() && CanEnableCellControl() )
7163 {
7164 // yes, now check whether the cells editor accepts the key
7165 int row = m_currentCellCoords.GetRow();
7166 int col = m_currentCellCoords.GetCol();
7167 wxGridCellAttr *attr = GetCellAttr(row, col);
7168 wxGridCellEditor *editor = attr->GetEditor(this, row, col);
7169
7170 // <F2> is special and will always start editing, for
7171 // other keys - ask the editor itself
7172 if ( (event.GetKeyCode() == WXK_F2 && !event.HasModifiers())
7173 || editor->IsAcceptedKey(event) )
7174 {
7175 // ensure cell is visble
7176 MakeCellVisible(row, col);
7177 EnableCellEditControl();
7178
7179 // a problem can arise if the cell is not completely
7180 // visible (even after calling MakeCellVisible the
7181 // control is not created and calling StartingKey will
7182 // crash the app
7183 if ( event.GetKeyCode() != WXK_F2 && editor->IsCreated() && m_cellEditCtrlEnabled )
7184 editor->StartingKey(event);
7185 }
7186 else
7187 {
7188 event.Skip();
7189 }
7190
7191 editor->DecRef();
7192 attr->DecRef();
7193 }
7194 else
7195 {
7196 event.Skip();
7197 }
7198 }
7199
7200 void wxGrid::OnEraseBackground(wxEraseEvent&)
7201 {
7202 }
7203
7204 void wxGrid::SetCurrentCell( const wxGridCellCoords& coords )
7205 {
7206 if ( SendEvent( wxEVT_GRID_SELECT_CELL, coords.GetRow(), coords.GetCol() ) )
7207 {
7208 // the event has been intercepted - do nothing
7209 return;
7210 }
7211
7212 #if !defined(__WXMAC__)
7213 wxClientDC dc( m_gridWin );
7214 PrepareDC( dc );
7215 #endif
7216
7217 if ( m_currentCellCoords != wxGridNoCellCoords )
7218 {
7219 DisableCellEditControl();
7220
7221 if ( IsVisible( m_currentCellCoords, false ) )
7222 {
7223 wxRect r;
7224 r = BlockToDeviceRect( m_currentCellCoords, m_currentCellCoords );
7225 if ( !m_gridLinesEnabled )
7226 {
7227 r.x--;
7228 r.y--;
7229 r.width++;
7230 r.height++;
7231 }
7232
7233 wxGridCellCoordsArray cells = CalcCellsExposed( r );
7234
7235 // Otherwise refresh redraws the highlight!
7236 m_currentCellCoords = coords;
7237
7238 #if defined(__WXMAC__)
7239 m_gridWin->Refresh(true /*, & r */);
7240 #else
7241 DrawGridCellArea( dc, cells );
7242 DrawAllGridLines( dc, r );
7243 #endif
7244 }
7245 }
7246
7247 m_currentCellCoords = coords;
7248
7249 wxGridCellAttr *attr = GetCellAttr( coords );
7250 #if !defined(__WXMAC__)
7251 DrawCellHighlight( dc, attr );
7252 #endif
7253 attr->DecRef();
7254 }
7255
7256 void wxGrid::HighlightBlock( int topRow, int leftCol, int bottomRow, int rightCol )
7257 {
7258 int temp;
7259 wxGridCellCoords updateTopLeft, updateBottomRight;
7260
7261 if ( m_selection )
7262 {
7263 if ( m_selection->GetSelectionMode() == wxGrid::wxGridSelectRows )
7264 {
7265 leftCol = 0;
7266 rightCol = GetNumberCols() - 1;
7267 }
7268 else if ( m_selection->GetSelectionMode() == wxGrid::wxGridSelectColumns )
7269 {
7270 topRow = 0;
7271 bottomRow = GetNumberRows() - 1;
7272 }
7273 }
7274
7275 if ( topRow > bottomRow )
7276 {
7277 temp = topRow;
7278 topRow = bottomRow;
7279 bottomRow = temp;
7280 }
7281
7282 if ( leftCol > rightCol )
7283 {
7284 temp = leftCol;
7285 leftCol = rightCol;
7286 rightCol = temp;
7287 }
7288
7289 updateTopLeft = wxGridCellCoords( topRow, leftCol );
7290 updateBottomRight = wxGridCellCoords( bottomRow, rightCol );
7291
7292 // First the case that we selected a completely new area
7293 if ( m_selectingTopLeft == wxGridNoCellCoords ||
7294 m_selectingBottomRight == wxGridNoCellCoords )
7295 {
7296 wxRect rect;
7297 rect = BlockToDeviceRect( wxGridCellCoords ( topRow, leftCol ),
7298 wxGridCellCoords ( bottomRow, rightCol ) );
7299 m_gridWin->Refresh( false, &rect );
7300 }
7301
7302 // Now handle changing an existing selection area.
7303 else if ( m_selectingTopLeft != updateTopLeft ||
7304 m_selectingBottomRight != updateBottomRight )
7305 {
7306 // Compute two optimal update rectangles:
7307 // Either one rectangle is a real subset of the
7308 // other, or they are (almost) disjoint!
7309 wxRect rect[4];
7310 bool need_refresh[4];
7311 need_refresh[0] =
7312 need_refresh[1] =
7313 need_refresh[2] =
7314 need_refresh[3] = false;
7315 int i;
7316
7317 // Store intermediate values
7318 wxCoord oldLeft = m_selectingTopLeft.GetCol();
7319 wxCoord oldTop = m_selectingTopLeft.GetRow();
7320 wxCoord oldRight = m_selectingBottomRight.GetCol();
7321 wxCoord oldBottom = m_selectingBottomRight.GetRow();
7322
7323 // Determine the outer/inner coordinates.
7324 if (oldLeft > leftCol)
7325 {
7326 temp = oldLeft;
7327 oldLeft = leftCol;
7328 leftCol = temp;
7329 }
7330 if (oldTop > topRow )
7331 {
7332 temp = oldTop;
7333 oldTop = topRow;
7334 topRow = temp;
7335 }
7336 if (oldRight < rightCol )
7337 {
7338 temp = oldRight;
7339 oldRight = rightCol;
7340 rightCol = temp;
7341 }
7342 if (oldBottom < bottomRow)
7343 {
7344 temp = oldBottom;
7345 oldBottom = bottomRow;
7346 bottomRow = temp;
7347 }
7348
7349 // Now, either the stuff marked old is the outer
7350 // rectangle or we don't have a situation where one
7351 // is contained in the other.
7352
7353 if ( oldLeft < leftCol )
7354 {
7355 // Refresh the newly selected or deselected
7356 // area to the left of the old or new selection.
7357 need_refresh[0] = true;
7358 rect[0] = BlockToDeviceRect(
7359 wxGridCellCoords( oldTop, oldLeft ),
7360 wxGridCellCoords( oldBottom, leftCol - 1 ) );
7361 }
7362
7363 if ( oldTop < topRow )
7364 {
7365 // Refresh the newly selected or deselected
7366 // area above the old or new selection.
7367 need_refresh[1] = true;
7368 rect[1] = BlockToDeviceRect(
7369 wxGridCellCoords( oldTop, leftCol ),
7370 wxGridCellCoords( topRow - 1, rightCol ) );
7371 }
7372
7373 if ( oldRight > rightCol )
7374 {
7375 // Refresh the newly selected or deselected
7376 // area to the right of the old or new selection.
7377 need_refresh[2] = true;
7378 rect[2] = BlockToDeviceRect(
7379 wxGridCellCoords( oldTop, rightCol + 1 ),
7380 wxGridCellCoords( oldBottom, oldRight ) );
7381 }
7382
7383 if ( oldBottom > bottomRow )
7384 {
7385 // Refresh the newly selected or deselected
7386 // area below the old or new selection.
7387 need_refresh[3] = true;
7388 rect[3] = BlockToDeviceRect(
7389 wxGridCellCoords( bottomRow + 1, leftCol ),
7390 wxGridCellCoords( oldBottom, rightCol ) );
7391 }
7392
7393 // various Refresh() calls
7394 for (i = 0; i < 4; i++ )
7395 if ( need_refresh[i] && rect[i] != wxGridNoCellRect )
7396 m_gridWin->Refresh( false, &(rect[i]) );
7397 }
7398
7399 // change selection
7400 m_selectingTopLeft = updateTopLeft;
7401 m_selectingBottomRight = updateBottomRight;
7402 }
7403
7404 //
7405 // ------ functions to get/send data (see also public functions)
7406 //
7407
7408 bool wxGrid::GetModelValues()
7409 {
7410 // Hide the editor, so it won't hide a changed value.
7411 HideCellEditControl();
7412
7413 if ( m_table )
7414 {
7415 // all we need to do is repaint the grid
7416 //
7417 m_gridWin->Refresh();
7418 return true;
7419 }
7420
7421 return false;
7422 }
7423
7424 bool wxGrid::SetModelValues()
7425 {
7426 int row, col;
7427
7428 // Disable the editor, so it won't hide a changed value.
7429 // Do we also want to save the current value of the editor first?
7430 // I think so ...
7431 DisableCellEditControl();
7432
7433 if ( m_table )
7434 {
7435 for ( row = 0; row < m_numRows; row++ )
7436 {
7437 for ( col = 0; col < m_numCols; col++ )
7438 {
7439 m_table->SetValue( row, col, GetCellValue(row, col) );
7440 }
7441 }
7442
7443 return true;
7444 }
7445
7446 return false;
7447 }
7448
7449 // Note - this function only draws cells that are in the list of
7450 // exposed cells (usually set from the update region by
7451 // CalcExposedCells)
7452 //
7453 void wxGrid::DrawGridCellArea( wxDC& dc, const wxGridCellCoordsArray& cells )
7454 {
7455 if ( !m_numRows || !m_numCols )
7456 return;
7457
7458 int i, numCells = cells.GetCount();
7459 int row, col, cell_rows, cell_cols;
7460 wxGridCellCoordsArray redrawCells;
7461
7462 for ( i = numCells - 1; i >= 0; i-- )
7463 {
7464 row = cells[i].GetRow();
7465 col = cells[i].GetCol();
7466 GetCellSize( row, col, &cell_rows, &cell_cols );
7467
7468 // If this cell is part of a multicell block, find owner for repaint
7469 if ( cell_rows <= 0 || cell_cols <= 0 )
7470 {
7471 wxGridCellCoords cell( row + cell_rows, col + cell_cols );
7472 bool marked = false;
7473 for ( int j = 0; j < numCells; j++ )
7474 {
7475 if ( cell == cells[j] )
7476 {
7477 marked = true;
7478 break;
7479 }
7480 }
7481
7482 if (!marked)
7483 {
7484 int count = redrawCells.GetCount();
7485 for (int j = 0; j < count; j++)
7486 {
7487 if ( cell == redrawCells[j] )
7488 {
7489 marked = true;
7490 break;
7491 }
7492 }
7493
7494 if (!marked)
7495 redrawCells.Add( cell );
7496 }
7497
7498 // don't bother drawing this cell
7499 continue;
7500 }
7501
7502 // If this cell is empty, find cell to left that might want to overflow
7503 if (m_table && m_table->IsEmptyCell(row, col))
7504 {
7505 for ( int l = 0; l < cell_rows; l++ )
7506 {
7507 // find a cell in this row to leave already marked for repaint
7508 int left = col;
7509 for (int k = 0; k < int(redrawCells.GetCount()); k++)
7510 if ((redrawCells[k].GetCol() < left) &&
7511 (redrawCells[k].GetRow() == row))
7512 {
7513 left = redrawCells[k].GetCol();
7514 }
7515
7516 if (left == col)
7517 left = 0; // oh well
7518
7519 for (int j = col - 1; j >= left; j--)
7520 {
7521 if (!m_table->IsEmptyCell(row + l, j))
7522 {
7523 if (GetCellOverflow(row + l, j))
7524 {
7525 wxGridCellCoords cell(row + l, j);
7526 bool marked = false;
7527
7528 for (int k = 0; k < numCells; k++)
7529 {
7530 if ( cell == cells[k] )
7531 {
7532 marked = true;
7533 break;
7534 }
7535 }
7536
7537 if (!marked)
7538 {
7539 int count = redrawCells.GetCount();
7540 for (int k = 0; k < count; k++)
7541 {
7542 if ( cell == redrawCells[k] )
7543 {
7544 marked = true;
7545 break;
7546 }
7547 }
7548 if (!marked)
7549 redrawCells.Add( cell );
7550 }
7551 }
7552 break;
7553 }
7554 }
7555 }
7556 }
7557
7558 DrawCell( dc, cells[i] );
7559 }
7560
7561 numCells = redrawCells.GetCount();
7562
7563 for ( i = numCells - 1; i >= 0; i-- )
7564 {
7565 DrawCell( dc, redrawCells[i] );
7566 }
7567 }
7568
7569 void wxGrid::DrawGridSpace( wxDC& dc )
7570 {
7571 int cw, ch;
7572 m_gridWin->GetClientSize( &cw, &ch );
7573
7574 int right, bottom;
7575 CalcUnscrolledPosition( cw, ch, &right, &bottom );
7576
7577 int rightCol = m_numCols > 0 ? GetColRight(GetColAt( m_numCols - 1 )) : 0;
7578 int bottomRow = m_numRows > 0 ? GetRowBottom(m_numRows - 1) : 0;
7579
7580 if ( right > rightCol || bottom > bottomRow )
7581 {
7582 int left, top;
7583 CalcUnscrolledPosition( 0, 0, &left, &top );
7584
7585 dc.SetBrush( wxBrush(GetDefaultCellBackgroundColour(), wxBRUSHSTYLE_SOLID) );
7586 dc.SetPen( *wxTRANSPARENT_PEN );
7587
7588 if ( right > rightCol )
7589 {
7590 dc.DrawRectangle( rightCol, top, right - rightCol, ch );
7591 }
7592
7593 if ( bottom > bottomRow )
7594 {
7595 dc.DrawRectangle( left, bottomRow, cw, bottom - bottomRow );
7596 }
7597 }
7598 }
7599
7600 void wxGrid::DrawCell( wxDC& dc, const wxGridCellCoords& coords )
7601 {
7602 int row = coords.GetRow();
7603 int col = coords.GetCol();
7604
7605 if ( GetColWidth(col) <= 0 || GetRowHeight(row) <= 0 )
7606 return;
7607
7608 // we draw the cell border ourselves
7609 #if !WXGRID_DRAW_LINES
7610 if ( m_gridLinesEnabled )
7611 DrawCellBorder( dc, coords );
7612 #endif
7613
7614 wxGridCellAttr* attr = GetCellAttr(row, col);
7615
7616 bool isCurrent = coords == m_currentCellCoords;
7617
7618 wxRect rect = CellToRect( row, col );
7619
7620 // if the editor is shown, we should use it and not the renderer
7621 // Note: However, only if it is really _shown_, i.e. not hidden!
7622 if ( isCurrent && IsCellEditControlShown() )
7623 {
7624 // NB: this "#if..." is temporary and fixes a problem where the
7625 // edit control is erased by this code after being rendered.
7626 // On wxMac (QD build only), the cell editor is a wxTextCntl and is rendered
7627 // implicitly, causing this out-of order render.
7628 #if !defined(__WXMAC__)
7629 wxGridCellEditor *editor = attr->GetEditor(this, row, col);
7630 editor->PaintBackground(rect, attr);
7631 editor->DecRef();
7632 #endif
7633 }
7634 else
7635 {
7636 // but all the rest is drawn by the cell renderer and hence may be customized
7637 wxGridCellRenderer *renderer = attr->GetRenderer(this, row, col);
7638 renderer->Draw(*this, *attr, dc, rect, row, col, IsInSelection(coords));
7639 renderer->DecRef();
7640 }
7641
7642 attr->DecRef();
7643 }
7644
7645 void wxGrid::DrawCellHighlight( wxDC& dc, const wxGridCellAttr *attr )
7646 {
7647 // don't show highlight when the grid doesn't have focus
7648 if ( !HasFocus() )
7649 return;
7650
7651 int row = m_currentCellCoords.GetRow();
7652 int col = m_currentCellCoords.GetCol();
7653
7654 if ( GetColWidth(col) <= 0 || GetRowHeight(row) <= 0 )
7655 return;
7656
7657 wxRect rect = CellToRect(row, col);
7658
7659 // hmmm... what could we do here to show that the cell is disabled?
7660 // for now, I just draw a thinner border than for the other ones, but
7661 // it doesn't look really good
7662
7663 int penWidth = attr->IsReadOnly() ? m_cellHighlightROPenWidth : m_cellHighlightPenWidth;
7664
7665 if (penWidth > 0)
7666 {
7667 // The center of the drawn line is where the position/width/height of
7668 // the rectangle is actually at (on wxMSW at least), so the
7669 // size of the rectangle is reduced to compensate for the thickness of
7670 // the line. If this is too strange on non-wxMSW platforms then
7671 // please #ifdef this appropriately.
7672 rect.x += penWidth / 2;
7673 rect.y += penWidth / 2;
7674 rect.width -= penWidth - 1;
7675 rect.height -= penWidth - 1;
7676
7677 // Now draw the rectangle
7678 // use the cellHighlightColour if the cell is inside a selection, this
7679 // will ensure the cell is always visible.
7680 dc.SetPen(wxPen(IsInSelection(row,col) ? m_selectionForeground : m_cellHighlightColour, penWidth, wxPENSTYLE_SOLID));
7681 dc.SetBrush(*wxTRANSPARENT_BRUSH);
7682 dc.DrawRectangle(rect);
7683 }
7684
7685 #if 0
7686 // VZ: my experiments with 3D borders...
7687
7688 // how to properly set colours for arbitrary bg?
7689 wxCoord x1 = rect.x,
7690 y1 = rect.y,
7691 x2 = rect.x + rect.width - 1,
7692 y2 = rect.y + rect.height - 1;
7693
7694 dc.SetPen(*wxWHITE_PEN);
7695 dc.DrawLine(x1, y1, x2, y1);
7696 dc.DrawLine(x1, y1, x1, y2);
7697
7698 dc.DrawLine(x1 + 1, y2 - 1, x2 - 1, y2 - 1);
7699 dc.DrawLine(x2 - 1, y1 + 1, x2 - 1, y2);
7700
7701 dc.SetPen(*wxBLACK_PEN);
7702 dc.DrawLine(x1, y2, x2, y2);
7703 dc.DrawLine(x2, y1, x2, y2 + 1);
7704 #endif
7705 }
7706
7707 wxPen wxGrid::GetDefaultGridLinePen()
7708 {
7709 return wxPen(GetGridLineColour(), 1, wxPENSTYLE_SOLID);
7710 }
7711
7712 wxPen wxGrid::GetRowGridLinePen(int WXUNUSED(row))
7713 {
7714 return GetDefaultGridLinePen();
7715 }
7716
7717 wxPen wxGrid::GetColGridLinePen(int WXUNUSED(col))
7718 {
7719 return GetDefaultGridLinePen();
7720 }
7721
7722 void wxGrid::DrawCellBorder( wxDC& dc, const wxGridCellCoords& coords )
7723 {
7724 int row = coords.GetRow();
7725 int col = coords.GetCol();
7726 if ( GetColWidth(col) <= 0 || GetRowHeight(row) <= 0 )
7727 return;
7728
7729
7730 wxRect rect = CellToRect( row, col );
7731
7732 // right hand border
7733 dc.SetPen( GetColGridLinePen(col) );
7734 dc.DrawLine( rect.x + rect.width, rect.y,
7735 rect.x + rect.width, rect.y + rect.height + 1 );
7736
7737 // bottom border
7738 dc.SetPen( GetRowGridLinePen(row) );
7739 dc.DrawLine( rect.x, rect.y + rect.height,
7740 rect.x + rect.width, rect.y + rect.height);
7741 }
7742
7743 void wxGrid::DrawHighlight(wxDC& dc, const wxGridCellCoordsArray& cells)
7744 {
7745 // This if block was previously in wxGrid::OnPaint but that doesn't
7746 // seem to get called under wxGTK - MB
7747 //
7748 if ( m_currentCellCoords == wxGridNoCellCoords &&
7749 m_numRows && m_numCols )
7750 {
7751 m_currentCellCoords.Set(0, 0);
7752 }
7753
7754 if ( IsCellEditControlShown() )
7755 {
7756 // don't show highlight when the edit control is shown
7757 return;
7758 }
7759
7760 // if the active cell was repainted, repaint its highlight too because it
7761 // might have been damaged by the grid lines
7762 size_t count = cells.GetCount();
7763 for ( size_t n = 0; n < count; n++ )
7764 {
7765 if ( cells[n] == m_currentCellCoords )
7766 {
7767 wxGridCellAttr* attr = GetCellAttr(m_currentCellCoords);
7768 DrawCellHighlight(dc, attr);
7769 attr->DecRef();
7770
7771 break;
7772 }
7773 }
7774 }
7775
7776 // TODO: remove this ???
7777 // This is used to redraw all grid lines e.g. when the grid line colour
7778 // has been changed
7779 //
7780 void wxGrid::DrawAllGridLines( wxDC& dc, const wxRegion & WXUNUSED(reg) )
7781 {
7782 #if !WXGRID_DRAW_LINES
7783 return;
7784 #endif
7785
7786 if ( !m_gridLinesEnabled || !m_numRows || !m_numCols )
7787 return;
7788
7789 int top, bottom, left, right;
7790
7791 #if 0 //#ifndef __WXGTK__
7792 if (reg.IsEmpty())
7793 {
7794 int cw, ch;
7795 m_gridWin->GetClientSize(&cw, &ch);
7796
7797 // virtual coords of visible area
7798 //
7799 CalcUnscrolledPosition( 0, 0, &left, &top );
7800 CalcUnscrolledPosition( cw, ch, &right, &bottom );
7801 }
7802 else
7803 {
7804 wxCoord x, y, w, h;
7805 reg.GetBox(x, y, w, h);
7806 CalcUnscrolledPosition( x, y, &left, &top );
7807 CalcUnscrolledPosition( x + w, y + h, &right, &bottom );
7808 }
7809 #else
7810 int cw, ch;
7811 m_gridWin->GetClientSize(&cw, &ch);
7812 CalcUnscrolledPosition( 0, 0, &left, &top );
7813 CalcUnscrolledPosition( cw, ch, &right, &bottom );
7814 #endif
7815
7816 // avoid drawing grid lines past the last row and col
7817 //
7818 right = wxMin( right, GetColRight(GetColAt( m_numCols - 1 )) );
7819 bottom = wxMin( bottom, GetRowBottom(m_numRows - 1) );
7820
7821 // no gridlines inside multicells, clip them out
7822 int leftCol = GetColPos( internalXToCol(left) );
7823 int topRow = internalYToRow(top);
7824 int rightCol = GetColPos( internalXToCol(right) );
7825 int bottomRow = internalYToRow(bottom);
7826
7827 wxRegion clippedcells(0, 0, cw, ch);
7828
7829 int i, j, cell_rows, cell_cols;
7830 wxRect rect;
7831
7832 for (j=topRow; j<=bottomRow; j++)
7833 {
7834 int colPos;
7835 for (colPos=leftCol; colPos<=rightCol; colPos++)
7836 {
7837 i = GetColAt( colPos );
7838
7839 GetCellSize( j, i, &cell_rows, &cell_cols );
7840 if ((cell_rows > 1) || (cell_cols > 1))
7841 {
7842 rect = CellToRect(j,i);
7843 CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
7844 clippedcells.Subtract(rect);
7845 }
7846 else if ((cell_rows < 0) || (cell_cols < 0))
7847 {
7848 rect = CellToRect(j + cell_rows, i + cell_cols);
7849 CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
7850 clippedcells.Subtract(rect);
7851 }
7852 }
7853 }
7854
7855 dc.SetDeviceClippingRegion( clippedcells );
7856
7857
7858 // horizontal grid lines
7859 //
7860 // already declared above - int i;
7861 for ( i = internalYToRow(top); i < m_numRows; i++ )
7862 {
7863 int bot = GetRowBottom(i) - 1;
7864
7865 if ( bot > bottom )
7866 {
7867 break;
7868 }
7869
7870 if ( bot >= top )
7871 {
7872 dc.SetPen( GetRowGridLinePen(i) );
7873 dc.DrawLine( left, bot, right, bot );
7874 }
7875 }
7876
7877 // vertical grid lines
7878 //
7879 int colPos;
7880 for ( colPos = leftCol; colPos < m_numCols; colPos++ )
7881 {
7882 i = GetColAt( colPos );
7883
7884 int colRight = GetColRight(i);
7885 #ifdef __WXGTK__
7886 if (GetLayoutDirection() != wxLayout_RightToLeft)
7887 #endif
7888 colRight--;
7889
7890 if ( colRight > right )
7891 {
7892 break;
7893 }
7894
7895 if ( colRight >= left )
7896 {
7897 dc.SetPen( GetColGridLinePen(i) );
7898 dc.DrawLine( colRight, top, colRight, bottom );
7899 }
7900 }
7901
7902 dc.DestroyClippingRegion();
7903 }
7904
7905 void wxGrid::DrawRowLabels( wxDC& dc, const wxArrayInt& rows)
7906 {
7907 if ( !m_numRows )
7908 return;
7909
7910 size_t i;
7911 size_t numLabels = rows.GetCount();
7912
7913 for ( i = 0; i < numLabels; i++ )
7914 {
7915 DrawRowLabel( dc, rows[i] );
7916 }
7917 }
7918
7919 void wxGrid::DrawRowLabel( wxDC& dc, int row )
7920 {
7921 if ( GetRowHeight(row) <= 0 || m_rowLabelWidth <= 0 )
7922 return;
7923
7924 wxRect rect;
7925
7926 int rowTop = GetRowTop(row),
7927 rowBottom = GetRowBottom(row) - 1;
7928
7929 dc.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW), 1, wxPENSTYLE_SOLID) );
7930 dc.DrawLine( m_rowLabelWidth - 1, rowTop, m_rowLabelWidth - 1, rowBottom );
7931 dc.DrawLine( 0, rowTop, 0, rowBottom );
7932 dc.DrawLine( 0, rowBottom, m_rowLabelWidth, rowBottom );
7933
7934 dc.SetPen( *wxWHITE_PEN );
7935 dc.DrawLine( 1, rowTop, 1, rowBottom );
7936 dc.DrawLine( 1, rowTop, m_rowLabelWidth - 1, rowTop );
7937
7938 dc.SetBackgroundMode( wxBRUSHSTYLE_TRANSPARENT );
7939 dc.SetTextForeground( GetLabelTextColour() );
7940 dc.SetFont( GetLabelFont() );
7941
7942 int hAlign, vAlign;
7943 GetRowLabelAlignment( &hAlign, &vAlign );
7944
7945 rect.SetX( 2 );
7946 rect.SetY( GetRowTop(row) + 2 );
7947 rect.SetWidth( m_rowLabelWidth - 4 );
7948 rect.SetHeight( GetRowHeight(row) - 4 );
7949 DrawTextRectangle( dc, GetRowLabelValue( row ), rect, hAlign, vAlign );
7950 }
7951
7952 void wxGrid::SetUseNativeColLabels( bool native )
7953 {
7954 m_nativeColumnLabels = native;
7955 if (native)
7956 {
7957 int height = wxRendererNative::Get().GetHeaderButtonHeight( this );
7958 SetColLabelSize( height );
7959 }
7960
7961 m_colLabelWin->Refresh();
7962 }
7963
7964 void wxGrid::DrawColLabels( wxDC& dc,const wxArrayInt& cols )
7965 {
7966 if ( !m_numCols )
7967 return;
7968
7969 size_t i;
7970 size_t numLabels = cols.GetCount();
7971
7972 for ( i = 0; i < numLabels; i++ )
7973 {
7974 DrawColLabel( dc, cols[i] );
7975 }
7976 }
7977
7978 void wxGrid::DrawColLabel( wxDC& dc, int col )
7979 {
7980 if ( GetColWidth(col) <= 0 || m_colLabelHeight <= 0 )
7981 return;
7982
7983 int colLeft = GetColLeft(col);
7984
7985 wxRect rect;
7986
7987 if (m_nativeColumnLabels)
7988 {
7989 rect.SetX( colLeft);
7990 rect.SetY( 0 );
7991 rect.SetWidth( GetColWidth(col));
7992 rect.SetHeight( m_colLabelHeight );
7993
7994 wxWindowDC *win_dc = (wxWindowDC*) &dc;
7995 wxRendererNative::Get().DrawHeaderButton( win_dc->GetWindow(), dc, rect, 0 );
7996 }
7997 else
7998 {
7999 int colRight = GetColRight(col) - 1;
8000
8001 dc.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW), 1, wxPENSTYLE_SOLID) );
8002 dc.DrawLine( colRight, 0, colRight, m_colLabelHeight - 1 );
8003 dc.DrawLine( colLeft, 0, colRight, 0 );
8004 dc.DrawLine( colLeft, m_colLabelHeight - 1,
8005 colRight + 1, m_colLabelHeight - 1 );
8006
8007 dc.SetPen( *wxWHITE_PEN );
8008 dc.DrawLine( colLeft, 1, colLeft, m_colLabelHeight - 1 );
8009 dc.DrawLine( colLeft, 1, colRight, 1 );
8010 }
8011
8012 dc.SetBackgroundMode( wxBRUSHSTYLE_TRANSPARENT );
8013 dc.SetTextForeground( GetLabelTextColour() );
8014 dc.SetFont( GetLabelFont() );
8015
8016 int hAlign, vAlign, orient;
8017 GetColLabelAlignment( &hAlign, &vAlign );
8018 orient = GetColLabelTextOrientation();
8019
8020 rect.SetX( colLeft + 2 );
8021 rect.SetY( 2 );
8022 rect.SetWidth( GetColWidth(col) - 4 );
8023 rect.SetHeight( m_colLabelHeight - 4 );
8024 DrawTextRectangle( dc, GetColLabelValue( col ), rect, hAlign, vAlign, orient );
8025 }
8026
8027 void wxGrid::DrawTextRectangle( wxDC& dc,
8028 const wxString& value,
8029 const wxRect& rect,
8030 int horizAlign,
8031 int vertAlign,
8032 int textOrientation )
8033 {
8034 wxArrayString lines;
8035
8036 StringToLines( value, lines );
8037
8038 // Forward to new API.
8039 DrawTextRectangle( dc,
8040 lines,
8041 rect,
8042 horizAlign,
8043 vertAlign,
8044 textOrientation );
8045 }
8046
8047 // VZ: this should be replaced with wxDC::DrawLabel() to which we just have to
8048 // add textOrientation support
8049 void wxGrid::DrawTextRectangle(wxDC& dc,
8050 const wxArrayString& lines,
8051 const wxRect& rect,
8052 int horizAlign,
8053 int vertAlign,
8054 int textOrientation)
8055 {
8056 if ( lines.empty() )
8057 return;
8058
8059 wxDCClipper clip(dc, rect);
8060
8061 long textWidth,
8062 textHeight;
8063
8064 if ( textOrientation == wxHORIZONTAL )
8065 GetTextBoxSize( dc, lines, &textWidth, &textHeight );
8066 else
8067 GetTextBoxSize( dc, lines, &textHeight, &textWidth );
8068
8069 int x = 0,
8070 y = 0;
8071 switch ( vertAlign )
8072 {
8073 case wxALIGN_BOTTOM:
8074 if ( textOrientation == wxHORIZONTAL )
8075 y = rect.y + (rect.height - textHeight - 1);
8076 else
8077 x = rect.x + rect.width - textWidth;
8078 break;
8079
8080 case wxALIGN_CENTRE:
8081 if ( textOrientation == wxHORIZONTAL )
8082 y = rect.y + ((rect.height - textHeight) / 2);
8083 else
8084 x = rect.x + ((rect.width - textWidth) / 2);
8085 break;
8086
8087 case wxALIGN_TOP:
8088 default:
8089 if ( textOrientation == wxHORIZONTAL )
8090 y = rect.y + 1;
8091 else
8092 x = rect.x + 1;
8093 break;
8094 }
8095
8096 // Align each line of a multi-line label
8097 size_t nLines = lines.GetCount();
8098 for ( size_t l = 0; l < nLines; l++ )
8099 {
8100 const wxString& line = lines[l];
8101
8102 if ( line.empty() )
8103 {
8104 *(textOrientation == wxHORIZONTAL ? &y : &x) += dc.GetCharHeight();
8105 continue;
8106 }
8107
8108 wxCoord lineWidth = 0,
8109 lineHeight = 0;
8110 dc.GetTextExtent(line, &lineWidth, &lineHeight);
8111
8112 switch ( horizAlign )
8113 {
8114 case wxALIGN_RIGHT:
8115 if ( textOrientation == wxHORIZONTAL )
8116 x = rect.x + (rect.width - lineWidth - 1);
8117 else
8118 y = rect.y + lineWidth + 1;
8119 break;
8120
8121 case wxALIGN_CENTRE:
8122 if ( textOrientation == wxHORIZONTAL )
8123 x = rect.x + ((rect.width - lineWidth) / 2);
8124 else
8125 y = rect.y + rect.height - ((rect.height - lineWidth) / 2);
8126 break;
8127
8128 case wxALIGN_LEFT:
8129 default:
8130 if ( textOrientation == wxHORIZONTAL )
8131 x = rect.x + 1;
8132 else
8133 y = rect.y + rect.height - 1;
8134 break;
8135 }
8136
8137 if ( textOrientation == wxHORIZONTAL )
8138 {
8139 dc.DrawText( line, x, y );
8140 y += lineHeight;
8141 }
8142 else
8143 {
8144 dc.DrawRotatedText( line, x, y, 90.0 );
8145 x += lineHeight;
8146 }
8147 }
8148 }
8149
8150 // Split multi-line text up into an array of strings.
8151 // Any existing contents of the string array are preserved.
8152 //
8153 // TODO: refactor wxTextFile::Read() and reuse the same code from here
8154 void wxGrid::StringToLines( const wxString& value, wxArrayString& lines ) const
8155 {
8156 int startPos = 0;
8157 int pos;
8158 wxString eol = wxTextFile::GetEOL( wxTextFileType_Unix );
8159 wxString tVal = wxTextFile::Translate( value, wxTextFileType_Unix );
8160
8161 while ( startPos < (int)tVal.length() )
8162 {
8163 pos = tVal.Mid(startPos).Find( eol );
8164 if ( pos < 0 )
8165 {
8166 break;
8167 }
8168 else if ( pos == 0 )
8169 {
8170 lines.Add( wxEmptyString );
8171 }
8172 else
8173 {
8174 lines.Add( tVal.Mid(startPos, pos) );
8175 }
8176
8177 startPos += pos + 1;
8178 }
8179
8180 if ( startPos < (int)tVal.length() )
8181 {
8182 lines.Add( tVal.Mid( startPos ) );
8183 }
8184 }
8185
8186 void wxGrid::GetTextBoxSize( const wxDC& dc,
8187 const wxArrayString& lines,
8188 long *width, long *height ) const
8189 {
8190 wxCoord w = 0;
8191 wxCoord h = 0;
8192 wxCoord lineW = 0, lineH = 0;
8193
8194 size_t i;
8195 for ( i = 0; i < lines.GetCount(); i++ )
8196 {
8197 dc.GetTextExtent( lines[i], &lineW, &lineH );
8198 w = wxMax( w, lineW );
8199 h += lineH;
8200 }
8201
8202 *width = w;
8203 *height = h;
8204 }
8205
8206 //
8207 // ------ Batch processing.
8208 //
8209 void wxGrid::EndBatch()
8210 {
8211 if ( m_batchCount > 0 )
8212 {
8213 m_batchCount--;
8214 if ( !m_batchCount )
8215 {
8216 CalcDimensions();
8217 m_rowLabelWin->Refresh();
8218 m_colLabelWin->Refresh();
8219 m_cornerLabelWin->Refresh();
8220 m_gridWin->Refresh();
8221 }
8222 }
8223 }
8224
8225 // Use this, rather than wxWindow::Refresh(), to force an immediate
8226 // repainting of the grid. Has no effect if you are already inside a
8227 // BeginBatch / EndBatch block.
8228 //
8229 void wxGrid::ForceRefresh()
8230 {
8231 BeginBatch();
8232 EndBatch();
8233 }
8234
8235 bool wxGrid::Enable(bool enable)
8236 {
8237 if ( !wxScrolledWindow::Enable(enable) )
8238 return false;
8239
8240 // redraw in the new state
8241 m_gridWin->Refresh();
8242
8243 return true;
8244 }
8245
8246 //
8247 // ------ Edit control functions
8248 //
8249
8250 void wxGrid::EnableEditing( bool edit )
8251 {
8252 // TODO: improve this ?
8253 //
8254 if ( edit != m_editable )
8255 {
8256 if (!edit)
8257 EnableCellEditControl(edit);
8258 m_editable = edit;
8259 }
8260 }
8261
8262 void wxGrid::EnableCellEditControl( bool enable )
8263 {
8264 if (! m_editable)
8265 return;
8266
8267 if ( enable != m_cellEditCtrlEnabled )
8268 {
8269 if ( enable )
8270 {
8271 if (SendEvent( wxEVT_GRID_EDITOR_SHOWN) <0)
8272 return;
8273
8274 // this should be checked by the caller!
8275 wxASSERT_MSG( CanEnableCellControl(), _T("can't enable editing for this cell!") );
8276
8277 // do it before ShowCellEditControl()
8278 m_cellEditCtrlEnabled = enable;
8279
8280 ShowCellEditControl();
8281 }
8282 else
8283 {
8284 //FIXME:add veto support
8285 SendEvent( wxEVT_GRID_EDITOR_HIDDEN );
8286
8287 HideCellEditControl();
8288 SaveEditControlValue();
8289
8290 // do it after HideCellEditControl()
8291 m_cellEditCtrlEnabled = enable;
8292 }
8293 }
8294 }
8295
8296 bool wxGrid::IsCurrentCellReadOnly() const
8297 {
8298 // const_cast
8299 wxGridCellAttr* attr = ((wxGrid *)this)->GetCellAttr(m_currentCellCoords);
8300 bool readonly = attr->IsReadOnly();
8301 attr->DecRef();
8302
8303 return readonly;
8304 }
8305
8306 bool wxGrid::CanEnableCellControl() const
8307 {
8308 return m_editable && (m_currentCellCoords != wxGridNoCellCoords) &&
8309 !IsCurrentCellReadOnly();
8310 }
8311
8312 bool wxGrid::IsCellEditControlEnabled() const
8313 {
8314 // the cell edit control might be disable for all cells or just for the
8315 // current one if it's read only
8316 return m_cellEditCtrlEnabled ? !IsCurrentCellReadOnly() : false;
8317 }
8318
8319 bool wxGrid::IsCellEditControlShown() const
8320 {
8321 bool isShown = false;
8322
8323 if ( m_cellEditCtrlEnabled )
8324 {
8325 int row = m_currentCellCoords.GetRow();
8326 int col = m_currentCellCoords.GetCol();
8327 wxGridCellAttr* attr = GetCellAttr(row, col);
8328 wxGridCellEditor* editor = attr->GetEditor((wxGrid*) this, row, col);
8329 attr->DecRef();
8330
8331 if ( editor )
8332 {
8333 if ( editor->IsCreated() )
8334 {
8335 isShown = editor->GetControl()->IsShown();
8336 }
8337
8338 editor->DecRef();
8339 }
8340 }
8341
8342 return isShown;
8343 }
8344
8345 void wxGrid::ShowCellEditControl()
8346 {
8347 if ( IsCellEditControlEnabled() )
8348 {
8349 if ( !IsVisible( m_currentCellCoords, false ) )
8350 {
8351 m_cellEditCtrlEnabled = false;
8352 return;
8353 }
8354 else
8355 {
8356 wxRect rect = CellToRect( m_currentCellCoords );
8357 int row = m_currentCellCoords.GetRow();
8358 int col = m_currentCellCoords.GetCol();
8359
8360 // if this is part of a multicell, find owner (topleft)
8361 int cell_rows, cell_cols;
8362 GetCellSize( row, col, &cell_rows, &cell_cols );
8363 if ( cell_rows <= 0 || cell_cols <= 0 )
8364 {
8365 row += cell_rows;
8366 col += cell_cols;
8367 m_currentCellCoords.SetRow( row );
8368 m_currentCellCoords.SetCol( col );
8369 }
8370
8371 // erase the highlight and the cell contents because the editor
8372 // might not cover the entire cell
8373 wxClientDC dc( m_gridWin );
8374 PrepareDC( dc );
8375 wxGridCellAttr* attr = GetCellAttr(row, col);
8376 dc.SetBrush(wxBrush(attr->GetBackgroundColour(), wxBRUSHSTYLE_SOLID));
8377 dc.SetPen(*wxTRANSPARENT_PEN);
8378 dc.DrawRectangle(rect);
8379
8380 // convert to scrolled coords
8381 CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
8382
8383 int nXMove = 0;
8384 if (rect.x < 0)
8385 nXMove = rect.x;
8386
8387 // cell is shifted by one pixel
8388 // However, don't allow x or y to become negative
8389 // since the SetSize() method interprets that as
8390 // "don't change."
8391 if (rect.x > 0)
8392 rect.x--;
8393 if (rect.y > 0)
8394 rect.y--;
8395
8396 wxGridCellEditor* editor = attr->GetEditor(this, row, col);
8397 if ( !editor->IsCreated() )
8398 {
8399 editor->Create(m_gridWin, wxID_ANY,
8400 new wxGridCellEditorEvtHandler(this, editor));
8401
8402 wxGridEditorCreatedEvent evt(GetId(),
8403 wxEVT_GRID_EDITOR_CREATED,
8404 this,
8405 row,
8406 col,
8407 editor->GetControl());
8408 GetEventHandler()->ProcessEvent(evt);
8409 }
8410
8411 // resize editor to overflow into righthand cells if allowed
8412 int maxWidth = rect.width;
8413 wxString value = GetCellValue(row, col);
8414 if ( (value != wxEmptyString) && (attr->GetOverflow()) )
8415 {
8416 int y;
8417 GetTextExtent(value, &maxWidth, &y, NULL, NULL, &attr->GetFont());
8418 if (maxWidth < rect.width)
8419 maxWidth = rect.width;
8420 }
8421
8422 int client_right = m_gridWin->GetClientSize().GetWidth();
8423 if (rect.x + maxWidth > client_right)
8424 maxWidth = client_right - rect.x;
8425
8426 if ((maxWidth > rect.width) && (col < m_numCols) && m_table)
8427 {
8428 GetCellSize( row, col, &cell_rows, &cell_cols );
8429 // may have changed earlier
8430 for (int i = col + cell_cols; i < m_numCols; i++)
8431 {
8432 int c_rows, c_cols;
8433 GetCellSize( row, i, &c_rows, &c_cols );
8434
8435 // looks weird going over a multicell
8436 if (m_table->IsEmptyCell( row, i ) &&
8437 (rect.width < maxWidth) && (c_rows == 1))
8438 {
8439 rect.width += GetColWidth( i );
8440 }
8441 else
8442 break;
8443 }
8444
8445 if (rect.GetRight() > client_right)
8446 rect.SetRight( client_right - 1 );
8447 }
8448
8449 editor->SetCellAttr( attr );
8450 editor->SetSize( rect );
8451 if (nXMove != 0)
8452 editor->GetControl()->Move(
8453 editor->GetControl()->GetPosition().x + nXMove,
8454 editor->GetControl()->GetPosition().y );
8455 editor->Show( true, attr );
8456
8457 // recalc dimensions in case we need to
8458 // expand the scrolled window to account for editor
8459 CalcDimensions();
8460
8461 editor->BeginEdit(row, col, this);
8462 editor->SetCellAttr(NULL);
8463
8464 editor->DecRef();
8465 attr->DecRef();
8466 }
8467 }
8468 }
8469
8470 void wxGrid::HideCellEditControl()
8471 {
8472 if ( IsCellEditControlEnabled() )
8473 {
8474 int row = m_currentCellCoords.GetRow();
8475 int col = m_currentCellCoords.GetCol();
8476
8477 wxGridCellAttr *attr = GetCellAttr(row, col);
8478 wxGridCellEditor *editor = attr->GetEditor(this, row, col);
8479 editor->Show( false );
8480 editor->DecRef();
8481 attr->DecRef();
8482
8483 m_gridWin->SetFocus();
8484
8485 // refresh whole row to the right
8486 wxRect rect( CellToRect(row, col) );
8487 CalcScrolledPosition(rect.x, rect.y, &rect.x, &rect.y );
8488 rect.width = m_gridWin->GetClientSize().GetWidth() - rect.x;
8489
8490 #ifdef __WXMAC__
8491 // ensure that the pixels under the focus ring get refreshed as well
8492 rect.Inflate(10, 10);
8493 #endif
8494
8495 m_gridWin->Refresh( false, &rect );
8496 }
8497 }
8498
8499 void wxGrid::SaveEditControlValue()
8500 {
8501 if ( IsCellEditControlEnabled() )
8502 {
8503 int row = m_currentCellCoords.GetRow();
8504 int col = m_currentCellCoords.GetCol();
8505
8506 wxString oldval = GetCellValue(row, col);
8507
8508 wxGridCellAttr* attr = GetCellAttr(row, col);
8509 wxGridCellEditor* editor = attr->GetEditor(this, row, col);
8510 bool changed = editor->EndEdit(row, col, this);
8511
8512 editor->DecRef();
8513 attr->DecRef();
8514
8515 if (changed)
8516 {
8517 if ( SendEvent( wxEVT_GRID_CELL_CHANGE,
8518 m_currentCellCoords.GetRow(),
8519 m_currentCellCoords.GetCol() ) < 0 )
8520 {
8521 // Event has been vetoed, set the data back.
8522 SetCellValue(row, col, oldval);
8523 }
8524 }
8525 }
8526 }
8527
8528 //
8529 // ------ Grid location functions
8530 // Note that all of these functions work with the logical coordinates of
8531 // grid cells and labels so you will need to convert from device
8532 // coordinates for mouse events etc.
8533 //
8534
8535 void wxGrid::XYToCell( int x, int y, wxGridCellCoords& coords ) const
8536 {
8537 int row = YToRow(y);
8538 int col = XToCol(x);
8539
8540 if ( row == -1 || col == -1 )
8541 {
8542 coords = wxGridNoCellCoords;
8543 }
8544 else
8545 {
8546 coords.Set( row, col );
8547 }
8548 }
8549
8550 // Internal Helper function for computing row or column from some
8551 // (unscrolled) coordinate value, using either
8552 // m_defaultRowHeight/m_defaultColWidth or binary search on array
8553 // of m_rowBottoms/m_ColRights to speed up the search!
8554
8555 static int CoordToRowOrCol(int coord, int defaultDist, int minDist,
8556 const wxArrayInt& BorderArray, int nMax,
8557 bool clipToMinMax)
8558 {
8559 if (coord < 0)
8560 return clipToMinMax && (nMax > 0) ? 0 : -1;
8561
8562 if (!defaultDist)
8563 defaultDist = 1;
8564
8565 size_t i_max = coord / defaultDist,
8566 i_min = 0;
8567
8568 if (BorderArray.IsEmpty())
8569 {
8570 if ((int) i_max < nMax)
8571 return i_max;
8572 return clipToMinMax ? nMax - 1 : -1;
8573 }
8574
8575 if ( i_max >= BorderArray.GetCount())
8576 {
8577 i_max = BorderArray.GetCount() - 1;
8578 }
8579 else
8580 {
8581 if ( coord >= BorderArray[i_max])
8582 {
8583 i_min = i_max;
8584 if (minDist)
8585 i_max = coord / minDist;
8586 else
8587 i_max = BorderArray.GetCount() - 1;
8588 }
8589
8590 if ( i_max >= BorderArray.GetCount())
8591 i_max = BorderArray.GetCount() - 1;
8592 }
8593
8594 if ( coord >= BorderArray[i_max])
8595 return clipToMinMax ? (int)i_max : -1;
8596 if ( coord < BorderArray[0] )
8597 return 0;
8598
8599 while ( i_max - i_min > 0 )
8600 {
8601 wxCHECK_MSG(BorderArray[i_min] <= coord && coord < BorderArray[i_max],
8602 0, _T("wxGrid: internal error in CoordToRowOrCol"));
8603 if (coord >= BorderArray[ i_max - 1])
8604 return i_max;
8605 else
8606 i_max--;
8607 int median = i_min + (i_max - i_min + 1) / 2;
8608 if (coord < BorderArray[median])
8609 i_max = median;
8610 else
8611 i_min = median;
8612 }
8613
8614 return i_max;
8615 }
8616
8617 int wxGrid::YToRow( int y ) const
8618 {
8619 return CoordToRowOrCol(y, m_defaultRowHeight,
8620 m_minAcceptableRowHeight, m_rowBottoms, m_numRows, false);
8621 }
8622
8623 int wxGrid::XToCol( int x, bool clipToMinMax ) const
8624 {
8625 if (x < 0)
8626 return clipToMinMax && (m_numCols > 0) ? GetColAt( 0 ) : -1;
8627
8628 wxASSERT_MSG(m_defaultColWidth > 0, wxT("Default column width can not be zero"));
8629
8630 int maxPos = x / m_defaultColWidth;
8631 int minPos = 0;
8632
8633 if (m_colRights.IsEmpty())
8634 {
8635 if(maxPos < m_numCols)
8636 return GetColAt( maxPos );
8637 return clipToMinMax ? GetColAt( m_numCols - 1 ) : -1;
8638 }
8639
8640 if ( maxPos >= m_numCols)
8641 maxPos = m_numCols - 1;
8642 else
8643 {
8644 if ( x >= m_colRights[GetColAt( maxPos )])
8645 {
8646 minPos = maxPos;
8647 if (m_minAcceptableColWidth)
8648 maxPos = x / m_minAcceptableColWidth;
8649 else
8650 maxPos = m_numCols - 1;
8651 }
8652 if ( maxPos >= m_numCols)
8653 maxPos = m_numCols - 1;
8654 }
8655
8656 //X is beyond the last column
8657 if ( x >= m_colRights[GetColAt( maxPos )])
8658 return clipToMinMax ? GetColAt( maxPos ) : -1;
8659
8660 //X is before the first column
8661 if ( x < m_colRights[GetColAt( 0 )] )
8662 return GetColAt( 0 );
8663
8664 //Perform a binary search
8665 while ( maxPos - minPos > 0 )
8666 {
8667 wxCHECK_MSG(m_colRights[GetColAt( minPos )] <= x && x < m_colRights[GetColAt( maxPos )],
8668 0, _T("wxGrid: internal error in XToCol"));
8669
8670 if (x >= m_colRights[GetColAt( maxPos - 1 )])
8671 return GetColAt( maxPos );
8672 else
8673 maxPos--;
8674 int median = minPos + (maxPos - minPos + 1) / 2;
8675 if (x < m_colRights[GetColAt( median )])
8676 maxPos = median;
8677 else
8678 minPos = median;
8679 }
8680 return GetColAt( maxPos );
8681 }
8682
8683 // return the row number that that the y coord is near
8684 // the edge of, or -1 if not near an edge.
8685 // coords can only possibly be near an edge if
8686 // (a) the row/column is large enough to still allow for an "inner" area
8687 // that is _not_ nead the edge (i.e., if the height/width is smaller
8688 // than WXGRID_LABEL_EDGE_ZONE, coords are _never_ considered to be
8689 // near the edge).
8690 // and
8691 // (b) resizing rows/columns (the thing for which edge detection is
8692 // relevant at all) is enabled.
8693 //
8694 int wxGrid::YToEdgeOfRow( int y ) const
8695 {
8696 int i;
8697 i = internalYToRow(y);
8698
8699 if ( GetRowHeight(i) > WXGRID_LABEL_EDGE_ZONE && CanDragRowSize() )
8700 {
8701 // We know that we are in row i, test whether we are
8702 // close enough to lower or upper border, respectively.
8703 if ( abs(GetRowBottom(i) - y) < WXGRID_LABEL_EDGE_ZONE )
8704 return i;
8705 else if ( i > 0 && y - GetRowTop(i) < WXGRID_LABEL_EDGE_ZONE )
8706 return i - 1;
8707 }
8708
8709 return -1;
8710 }
8711
8712 // return the col number that that the x coord is near the edge of, or
8713 // -1 if not near an edge
8714 // See comment at YToEdgeOfRow for conditions on edge detection.
8715 //
8716 int wxGrid::XToEdgeOfCol( int x ) const
8717 {
8718 int i;
8719 i = internalXToCol(x);
8720
8721 if ( GetColWidth(i) > WXGRID_LABEL_EDGE_ZONE && CanDragColSize() )
8722 {
8723 // We know that we are in column i; test whether we are
8724 // close enough to right or left border, respectively.
8725 if ( abs(GetColRight(i) - x) < WXGRID_LABEL_EDGE_ZONE )
8726 return i;
8727 else if ( i > 0 && x - GetColLeft(i) < WXGRID_LABEL_EDGE_ZONE )
8728 return i - 1;
8729 }
8730
8731 return -1;
8732 }
8733
8734 wxRect wxGrid::CellToRect( int row, int col ) const
8735 {
8736 wxRect rect( -1, -1, -1, -1 );
8737
8738 if ( row >= 0 && row < m_numRows &&
8739 col >= 0 && col < m_numCols )
8740 {
8741 int i, cell_rows, cell_cols;
8742 rect.width = rect.height = 0;
8743 GetCellSize( row, col, &cell_rows, &cell_cols );
8744 // if negative then find multicell owner
8745 if (cell_rows < 0)
8746 row += cell_rows;
8747 if (cell_cols < 0)
8748 col += cell_cols;
8749 GetCellSize( row, col, &cell_rows, &cell_cols );
8750
8751 rect.x = GetColLeft(col);
8752 rect.y = GetRowTop(row);
8753 for (i=col; i < col + cell_cols; i++)
8754 rect.width += GetColWidth(i);
8755 for (i=row; i < row + cell_rows; i++)
8756 rect.height += GetRowHeight(i);
8757 }
8758
8759 // if grid lines are enabled, then the area of the cell is a bit smaller
8760 if (m_gridLinesEnabled)
8761 {
8762 rect.width -= 1;
8763 rect.height -= 1;
8764 }
8765
8766 return rect;
8767 }
8768
8769 bool wxGrid::IsVisible( int row, int col, bool wholeCellVisible ) const
8770 {
8771 // get the cell rectangle in logical coords
8772 //
8773 wxRect r( CellToRect( row, col ) );
8774
8775 // convert to device coords
8776 //
8777 int left, top, right, bottom;
8778 CalcScrolledPosition( r.GetLeft(), r.GetTop(), &left, &top );
8779 CalcScrolledPosition( r.GetRight(), r.GetBottom(), &right, &bottom );
8780
8781 // check against the client area of the grid window
8782 int cw, ch;
8783 m_gridWin->GetClientSize( &cw, &ch );
8784
8785 if ( wholeCellVisible )
8786 {
8787 // is the cell wholly visible ?
8788 return ( left >= 0 && right <= cw &&
8789 top >= 0 && bottom <= ch );
8790 }
8791 else
8792 {
8793 // is the cell partly visible ?
8794 //
8795 return ( ((left >= 0 && left < cw) || (right > 0 && right <= cw)) &&
8796 ((top >= 0 && top < ch) || (bottom > 0 && bottom <= ch)) );
8797 }
8798 }
8799
8800 // make the specified cell location visible by doing a minimal amount
8801 // of scrolling
8802 //
8803 void wxGrid::MakeCellVisible( int row, int col )
8804 {
8805 int i;
8806 int xpos = -1, ypos = -1;
8807
8808 if ( row >= 0 && row < m_numRows &&
8809 col >= 0 && col < m_numCols )
8810 {
8811 // get the cell rectangle in logical coords
8812 wxRect r( CellToRect( row, col ) );
8813
8814 // convert to device coords
8815 int left, top, right, bottom;
8816 CalcScrolledPosition( r.GetLeft(), r.GetTop(), &left, &top );
8817 CalcScrolledPosition( r.GetRight(), r.GetBottom(), &right, &bottom );
8818
8819 int cw, ch;
8820 m_gridWin->GetClientSize( &cw, &ch );
8821
8822 if ( top < 0 )
8823 {
8824 ypos = r.GetTop();
8825 }
8826 else if ( bottom > ch )
8827 {
8828 int h = r.GetHeight();
8829 ypos = r.GetTop();
8830 for ( i = row - 1; i >= 0; i-- )
8831 {
8832 int rowHeight = GetRowHeight(i);
8833 if ( h + rowHeight > ch )
8834 break;
8835
8836 h += rowHeight;
8837 ypos -= rowHeight;
8838 }
8839
8840 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
8841 // have rounding errors (this is important, because if we do,
8842 // we might not scroll at all and some cells won't be redrawn)
8843 //
8844 // Sometimes GRID_SCROLL_LINE / 2 is not enough,
8845 // so just add a full scroll unit...
8846 ypos += m_scrollLineY;
8847 }
8848
8849 // special handling for wide cells - show always left part of the cell!
8850 // Otherwise, e.g. when stepping from row to row, it would jump between
8851 // left and right part of the cell on every step!
8852 // if ( left < 0 )
8853 if ( left < 0 || (right - left) >= cw )
8854 {
8855 xpos = r.GetLeft();
8856 }
8857 else if ( right > cw )
8858 {
8859 // position the view so that the cell is on the right
8860 int x0, y0;
8861 CalcUnscrolledPosition(0, 0, &x0, &y0);
8862 xpos = x0 + (right - cw);
8863
8864 // see comment for ypos above
8865 xpos += m_scrollLineX;
8866 }
8867
8868 if ( xpos != -1 || ypos != -1 )
8869 {
8870 if ( xpos != -1 )
8871 xpos /= m_scrollLineX;
8872 if ( ypos != -1 )
8873 ypos /= m_scrollLineY;
8874 Scroll( xpos, ypos );
8875 AdjustScrollbars();
8876 }
8877 }
8878 }
8879
8880 //
8881 // ------ Grid cursor movement functions
8882 //
8883
8884 bool wxGrid::MoveCursorUp( bool expandSelection )
8885 {
8886 if ( m_currentCellCoords != wxGridNoCellCoords &&
8887 m_currentCellCoords.GetRow() >= 0 )
8888 {
8889 if ( expandSelection )
8890 {
8891 if ( m_selectingKeyboard == wxGridNoCellCoords )
8892 m_selectingKeyboard = m_currentCellCoords;
8893 if ( m_selectingKeyboard.GetRow() > 0 )
8894 {
8895 m_selectingKeyboard.SetRow( m_selectingKeyboard.GetRow() - 1 );
8896 MakeCellVisible( m_selectingKeyboard.GetRow(),
8897 m_selectingKeyboard.GetCol() );
8898 HighlightBlock( m_currentCellCoords, m_selectingKeyboard );
8899 }
8900 }
8901 else if ( m_currentCellCoords.GetRow() > 0 )
8902 {
8903 int row = m_currentCellCoords.GetRow() - 1;
8904 int col = m_currentCellCoords.GetCol();
8905 ClearSelection();
8906 MakeCellVisible( row, col );
8907 SetCurrentCell( row, col );
8908 }
8909 else
8910 return false;
8911
8912 return true;
8913 }
8914
8915 return false;
8916 }
8917
8918 bool wxGrid::MoveCursorDown( bool expandSelection )
8919 {
8920 if ( m_currentCellCoords != wxGridNoCellCoords &&
8921 m_currentCellCoords.GetRow() < m_numRows )
8922 {
8923 if ( expandSelection )
8924 {
8925 if ( m_selectingKeyboard == wxGridNoCellCoords )
8926 m_selectingKeyboard = m_currentCellCoords;
8927 if ( m_selectingKeyboard.GetRow() < m_numRows - 1 )
8928 {
8929 m_selectingKeyboard.SetRow( m_selectingKeyboard.GetRow() + 1 );
8930 MakeCellVisible( m_selectingKeyboard.GetRow(),
8931 m_selectingKeyboard.GetCol() );
8932 HighlightBlock( m_currentCellCoords, m_selectingKeyboard );
8933 }
8934 }
8935 else if ( m_currentCellCoords.GetRow() < m_numRows - 1 )
8936 {
8937 int row = m_currentCellCoords.GetRow() + 1;
8938 int col = m_currentCellCoords.GetCol();
8939 ClearSelection();
8940 MakeCellVisible( row, col );
8941 SetCurrentCell( row, col );
8942 }
8943 else
8944 return false;
8945
8946 return true;
8947 }
8948
8949 return false;
8950 }
8951
8952 bool wxGrid::MoveCursorLeft( bool expandSelection )
8953 {
8954 if ( m_currentCellCoords != wxGridNoCellCoords &&
8955 m_currentCellCoords.GetCol() >= 0 )
8956 {
8957 if ( expandSelection )
8958 {
8959 if ( m_selectingKeyboard == wxGridNoCellCoords )
8960 m_selectingKeyboard = m_currentCellCoords;
8961 if ( m_selectingKeyboard.GetCol() > 0 )
8962 {
8963 m_selectingKeyboard.SetCol( m_selectingKeyboard.GetCol() - 1 );
8964 MakeCellVisible( m_selectingKeyboard.GetRow(),
8965 m_selectingKeyboard.GetCol() );
8966 HighlightBlock( m_currentCellCoords, m_selectingKeyboard );
8967 }
8968 }
8969 else if ( GetColPos( m_currentCellCoords.GetCol() ) > 0 )
8970 {
8971 int row = m_currentCellCoords.GetRow();
8972 int col = GetColAt( GetColPos( m_currentCellCoords.GetCol() ) - 1 );
8973 ClearSelection();
8974
8975 MakeCellVisible( row, col );
8976 SetCurrentCell( row, col );
8977 }
8978 else
8979 return false;
8980
8981 return true;
8982 }
8983
8984 return false;
8985 }
8986
8987 bool wxGrid::MoveCursorRight( bool expandSelection )
8988 {
8989 if ( m_currentCellCoords != wxGridNoCellCoords &&
8990 m_currentCellCoords.GetCol() < m_numCols )
8991 {
8992 if ( expandSelection )
8993 {
8994 if ( m_selectingKeyboard == wxGridNoCellCoords )
8995 m_selectingKeyboard = m_currentCellCoords;
8996 if ( m_selectingKeyboard.GetCol() < m_numCols - 1 )
8997 {
8998 m_selectingKeyboard.SetCol( m_selectingKeyboard.GetCol() + 1 );
8999 MakeCellVisible( m_selectingKeyboard.GetRow(),
9000 m_selectingKeyboard.GetCol() );
9001 HighlightBlock( m_currentCellCoords, m_selectingKeyboard );
9002 }
9003 }
9004 else if ( GetColPos( m_currentCellCoords.GetCol() ) < m_numCols - 1 )
9005 {
9006 int row = m_currentCellCoords.GetRow();
9007 int col = GetColAt( GetColPos( m_currentCellCoords.GetCol() ) + 1 );
9008 ClearSelection();
9009
9010 MakeCellVisible( row, col );
9011 SetCurrentCell( row, col );
9012 }
9013 else
9014 return false;
9015
9016 return true;
9017 }
9018
9019 return false;
9020 }
9021
9022 bool wxGrid::MovePageUp()
9023 {
9024 if ( m_currentCellCoords == wxGridNoCellCoords )
9025 return false;
9026
9027 int row = m_currentCellCoords.GetRow();
9028 if ( row > 0 )
9029 {
9030 int cw, ch;
9031 m_gridWin->GetClientSize( &cw, &ch );
9032
9033 int y = GetRowTop(row);
9034 int newRow = internalYToRow( y - ch + 1 );
9035
9036 if ( newRow == row )
9037 {
9038 // row > 0, so newRow can never be less than 0 here.
9039 newRow = row - 1;
9040 }
9041
9042 MakeCellVisible( newRow, m_currentCellCoords.GetCol() );
9043 SetCurrentCell( newRow, m_currentCellCoords.GetCol() );
9044
9045 return true;
9046 }
9047
9048 return false;
9049 }
9050
9051 bool wxGrid::MovePageDown()
9052 {
9053 if ( m_currentCellCoords == wxGridNoCellCoords )
9054 return false;
9055
9056 int row = m_currentCellCoords.GetRow();
9057 if ( (row + 1) < m_numRows )
9058 {
9059 int cw, ch;
9060 m_gridWin->GetClientSize( &cw, &ch );
9061
9062 int y = GetRowTop(row);
9063 int newRow = internalYToRow( y + ch );
9064 if ( newRow == row )
9065 {
9066 // row < m_numRows, so newRow can't overflow here.
9067 newRow = row + 1;
9068 }
9069
9070 MakeCellVisible( newRow, m_currentCellCoords.GetCol() );
9071 SetCurrentCell( newRow, m_currentCellCoords.GetCol() );
9072
9073 return true;
9074 }
9075
9076 return false;
9077 }
9078
9079 bool wxGrid::MoveCursorUpBlock( bool expandSelection )
9080 {
9081 if ( m_table &&
9082 m_currentCellCoords != wxGridNoCellCoords &&
9083 m_currentCellCoords.GetRow() > 0 )
9084 {
9085 int row = m_currentCellCoords.GetRow();
9086 int col = m_currentCellCoords.GetCol();
9087
9088 if ( m_table->IsEmptyCell(row, col) )
9089 {
9090 // starting in an empty cell: find the next block of
9091 // non-empty cells
9092 //
9093 while ( row > 0 )
9094 {
9095 row--;
9096 if ( !(m_table->IsEmptyCell(row, col)) )
9097 break;
9098 }
9099 }
9100 else if ( m_table->IsEmptyCell(row - 1, col) )
9101 {
9102 // starting at the top of a block: find the next block
9103 //
9104 row--;
9105 while ( row > 0 )
9106 {
9107 row--;
9108 if ( !(m_table->IsEmptyCell(row, col)) )
9109 break;
9110 }
9111 }
9112 else
9113 {
9114 // starting within a block: find the top of the block
9115 //
9116 while ( row > 0 )
9117 {
9118 row--;
9119 if ( m_table->IsEmptyCell(row, col) )
9120 {
9121 row++;
9122 break;
9123 }
9124 }
9125 }
9126
9127 MakeCellVisible( row, col );
9128 if ( expandSelection )
9129 {
9130 m_selectingKeyboard = wxGridCellCoords( row, col );
9131 HighlightBlock( m_currentCellCoords, m_selectingKeyboard );
9132 }
9133 else
9134 {
9135 ClearSelection();
9136 SetCurrentCell( row, col );
9137 }
9138
9139 return true;
9140 }
9141
9142 return false;
9143 }
9144
9145 bool wxGrid::MoveCursorDownBlock( bool expandSelection )
9146 {
9147 if ( m_table &&
9148 m_currentCellCoords != wxGridNoCellCoords &&
9149 m_currentCellCoords.GetRow() < m_numRows - 1 )
9150 {
9151 int row = m_currentCellCoords.GetRow();
9152 int col = m_currentCellCoords.GetCol();
9153
9154 if ( m_table->IsEmptyCell(row, col) )
9155 {
9156 // starting in an empty cell: find the next block of
9157 // non-empty cells
9158 //
9159 while ( row < m_numRows - 1 )
9160 {
9161 row++;
9162 if ( !(m_table->IsEmptyCell(row, col)) )
9163 break;
9164 }
9165 }
9166 else if ( m_table->IsEmptyCell(row + 1, col) )
9167 {
9168 // starting at the bottom of a block: find the next block
9169 //
9170 row++;
9171 while ( row < m_numRows - 1 )
9172 {
9173 row++;
9174 if ( !(m_table->IsEmptyCell(row, col)) )
9175 break;
9176 }
9177 }
9178 else
9179 {
9180 // starting within a block: find the bottom of the block
9181 //
9182 while ( row < m_numRows - 1 )
9183 {
9184 row++;
9185 if ( m_table->IsEmptyCell(row, col) )
9186 {
9187 row--;
9188 break;
9189 }
9190 }
9191 }
9192
9193 MakeCellVisible( row, col );
9194 if ( expandSelection )
9195 {
9196 m_selectingKeyboard = wxGridCellCoords( row, col );
9197 HighlightBlock( m_currentCellCoords, m_selectingKeyboard );
9198 }
9199 else
9200 {
9201 ClearSelection();
9202 SetCurrentCell( row, col );
9203 }
9204
9205 return true;
9206 }
9207
9208 return false;
9209 }
9210
9211 bool wxGrid::MoveCursorLeftBlock( bool expandSelection )
9212 {
9213 if ( m_table &&
9214 m_currentCellCoords != wxGridNoCellCoords &&
9215 m_currentCellCoords.GetCol() > 0 )
9216 {
9217 int row = m_currentCellCoords.GetRow();
9218 int col = m_currentCellCoords.GetCol();
9219
9220 if ( m_table->IsEmptyCell(row, col) )
9221 {
9222 // starting in an empty cell: find the next block of
9223 // non-empty cells
9224 //
9225 while ( col > 0 )
9226 {
9227 col--;
9228 if ( !(m_table->IsEmptyCell(row, col)) )
9229 break;
9230 }
9231 }
9232 else if ( m_table->IsEmptyCell(row, col - 1) )
9233 {
9234 // starting at the left of a block: find the next block
9235 //
9236 col--;
9237 while ( col > 0 )
9238 {
9239 col--;
9240 if ( !(m_table->IsEmptyCell(row, col)) )
9241 break;
9242 }
9243 }
9244 else
9245 {
9246 // starting within a block: find the left of the block
9247 //
9248 while ( col > 0 )
9249 {
9250 col--;
9251 if ( m_table->IsEmptyCell(row, col) )
9252 {
9253 col++;
9254 break;
9255 }
9256 }
9257 }
9258
9259 MakeCellVisible( row, col );
9260 if ( expandSelection )
9261 {
9262 m_selectingKeyboard = wxGridCellCoords( row, col );
9263 HighlightBlock( m_currentCellCoords, m_selectingKeyboard );
9264 }
9265 else
9266 {
9267 ClearSelection();
9268 SetCurrentCell( row, col );
9269 }
9270
9271 return true;
9272 }
9273
9274 return false;
9275 }
9276
9277 bool wxGrid::MoveCursorRightBlock( bool expandSelection )
9278 {
9279 if ( m_table &&
9280 m_currentCellCoords != wxGridNoCellCoords &&
9281 m_currentCellCoords.GetCol() < m_numCols - 1 )
9282 {
9283 int row = m_currentCellCoords.GetRow();
9284 int col = m_currentCellCoords.GetCol();
9285
9286 if ( m_table->IsEmptyCell(row, col) )
9287 {
9288 // starting in an empty cell: find the next block of
9289 // non-empty cells
9290 //
9291 while ( col < m_numCols - 1 )
9292 {
9293 col++;
9294 if ( !(m_table->IsEmptyCell(row, col)) )
9295 break;
9296 }
9297 }
9298 else if ( m_table->IsEmptyCell(row, col + 1) )
9299 {
9300 // starting at the right of a block: find the next block
9301 //
9302 col++;
9303 while ( col < m_numCols - 1 )
9304 {
9305 col++;
9306 if ( !(m_table->IsEmptyCell(row, col)) )
9307 break;
9308 }
9309 }
9310 else
9311 {
9312 // starting within a block: find the right of the block
9313 //
9314 while ( col < m_numCols - 1 )
9315 {
9316 col++;
9317 if ( m_table->IsEmptyCell(row, col) )
9318 {
9319 col--;
9320 break;
9321 }
9322 }
9323 }
9324
9325 MakeCellVisible( row, col );
9326 if ( expandSelection )
9327 {
9328 m_selectingKeyboard = wxGridCellCoords( row, col );
9329 HighlightBlock( m_currentCellCoords, m_selectingKeyboard );
9330 }
9331 else
9332 {
9333 ClearSelection();
9334 SetCurrentCell( row, col );
9335 }
9336
9337 return true;
9338 }
9339
9340 return false;
9341 }
9342
9343 //
9344 // ------ Label values and formatting
9345 //
9346
9347 void wxGrid::GetRowLabelAlignment( int *horiz, int *vert ) const
9348 {
9349 if ( horiz )
9350 *horiz = m_rowLabelHorizAlign;
9351 if ( vert )
9352 *vert = m_rowLabelVertAlign;
9353 }
9354
9355 void wxGrid::GetColLabelAlignment( int *horiz, int *vert ) const
9356 {
9357 if ( horiz )
9358 *horiz = m_colLabelHorizAlign;
9359 if ( vert )
9360 *vert = m_colLabelVertAlign;
9361 }
9362
9363 int wxGrid::GetColLabelTextOrientation() const
9364 {
9365 return m_colLabelTextOrientation;
9366 }
9367
9368 wxString wxGrid::GetRowLabelValue( int row ) const
9369 {
9370 if ( m_table )
9371 {
9372 return m_table->GetRowLabelValue( row );
9373 }
9374 else
9375 {
9376 wxString s;
9377 s << row;
9378 return s;
9379 }
9380 }
9381
9382 wxString wxGrid::GetColLabelValue( int col ) const
9383 {
9384 if ( m_table )
9385 {
9386 return m_table->GetColLabelValue( col );
9387 }
9388 else
9389 {
9390 wxString s;
9391 s << col;
9392 return s;
9393 }
9394 }
9395
9396 void wxGrid::SetRowLabelSize( int width )
9397 {
9398 wxASSERT( width >= 0 || width == wxGRID_AUTOSIZE );
9399
9400 if ( width == wxGRID_AUTOSIZE )
9401 {
9402 width = CalcColOrRowLabelAreaMinSize(wxGRID_ROW);
9403 }
9404
9405 if ( width != m_rowLabelWidth )
9406 {
9407 if ( width == 0 )
9408 {
9409 m_rowLabelWin->Show( false );
9410 m_cornerLabelWin->Show( false );
9411 }
9412 else if ( m_rowLabelWidth == 0 )
9413 {
9414 m_rowLabelWin->Show( true );
9415 if ( m_colLabelHeight > 0 )
9416 m_cornerLabelWin->Show( true );
9417 }
9418
9419 m_rowLabelWidth = width;
9420 CalcWindowSizes();
9421 wxScrolledWindow::Refresh( true );
9422 }
9423 }
9424
9425 void wxGrid::SetColLabelSize( int height )
9426 {
9427 wxASSERT( height >=0 || height == wxGRID_AUTOSIZE );
9428
9429 if ( height == wxGRID_AUTOSIZE )
9430 {
9431 height = CalcColOrRowLabelAreaMinSize(wxGRID_COLUMN);
9432 }
9433
9434 if ( height != m_colLabelHeight )
9435 {
9436 if ( height == 0 )
9437 {
9438 m_colLabelWin->Show( false );
9439 m_cornerLabelWin->Show( false );
9440 }
9441 else if ( m_colLabelHeight == 0 )
9442 {
9443 m_colLabelWin->Show( true );
9444 if ( m_rowLabelWidth > 0 )
9445 m_cornerLabelWin->Show( true );
9446 }
9447
9448 m_colLabelHeight = height;
9449 CalcWindowSizes();
9450 wxScrolledWindow::Refresh( true );
9451 }
9452 }
9453
9454 void wxGrid::SetLabelBackgroundColour( const wxColour& colour )
9455 {
9456 if ( m_labelBackgroundColour != colour )
9457 {
9458 m_labelBackgroundColour = colour;
9459 m_rowLabelWin->SetBackgroundColour( colour );
9460 m_colLabelWin->SetBackgroundColour( colour );
9461 m_cornerLabelWin->SetBackgroundColour( colour );
9462
9463 if ( !GetBatchCount() )
9464 {
9465 m_rowLabelWin->Refresh();
9466 m_colLabelWin->Refresh();
9467 m_cornerLabelWin->Refresh();
9468 }
9469 }
9470 }
9471
9472 void wxGrid::SetLabelTextColour( const wxColour& colour )
9473 {
9474 if ( m_labelTextColour != colour )
9475 {
9476 m_labelTextColour = colour;
9477 if ( !GetBatchCount() )
9478 {
9479 m_rowLabelWin->Refresh();
9480 m_colLabelWin->Refresh();
9481 }
9482 }
9483 }
9484
9485 void wxGrid::SetLabelFont( const wxFont& font )
9486 {
9487 m_labelFont = font;
9488 if ( !GetBatchCount() )
9489 {
9490 m_rowLabelWin->Refresh();
9491 m_colLabelWin->Refresh();
9492 }
9493 }
9494
9495 void wxGrid::SetRowLabelAlignment( int horiz, int vert )
9496 {
9497 // allow old (incorrect) defs to be used
9498 switch ( horiz )
9499 {
9500 case wxLEFT: horiz = wxALIGN_LEFT; break;
9501 case wxRIGHT: horiz = wxALIGN_RIGHT; break;
9502 case wxCENTRE: horiz = wxALIGN_CENTRE; break;
9503 }
9504
9505 switch ( vert )
9506 {
9507 case wxTOP: vert = wxALIGN_TOP; break;
9508 case wxBOTTOM: vert = wxALIGN_BOTTOM; break;
9509 case wxCENTRE: vert = wxALIGN_CENTRE; break;
9510 }
9511
9512 if ( horiz == wxALIGN_LEFT || horiz == wxALIGN_CENTRE || horiz == wxALIGN_RIGHT )
9513 {
9514 m_rowLabelHorizAlign = horiz;
9515 }
9516
9517 if ( vert == wxALIGN_TOP || vert == wxALIGN_CENTRE || vert == wxALIGN_BOTTOM )
9518 {
9519 m_rowLabelVertAlign = vert;
9520 }
9521
9522 if ( !GetBatchCount() )
9523 {
9524 m_rowLabelWin->Refresh();
9525 }
9526 }
9527
9528 void wxGrid::SetColLabelAlignment( int horiz, int vert )
9529 {
9530 // allow old (incorrect) defs to be used
9531 switch ( horiz )
9532 {
9533 case wxLEFT: horiz = wxALIGN_LEFT; break;
9534 case wxRIGHT: horiz = wxALIGN_RIGHT; break;
9535 case wxCENTRE: horiz = wxALIGN_CENTRE; break;
9536 }
9537
9538 switch ( vert )
9539 {
9540 case wxTOP: vert = wxALIGN_TOP; break;
9541 case wxBOTTOM: vert = wxALIGN_BOTTOM; break;
9542 case wxCENTRE: vert = wxALIGN_CENTRE; break;
9543 }
9544
9545 if ( horiz == wxALIGN_LEFT || horiz == wxALIGN_CENTRE || horiz == wxALIGN_RIGHT )
9546 {
9547 m_colLabelHorizAlign = horiz;
9548 }
9549
9550 if ( vert == wxALIGN_TOP || vert == wxALIGN_CENTRE || vert == wxALIGN_BOTTOM )
9551 {
9552 m_colLabelVertAlign = vert;
9553 }
9554
9555 if ( !GetBatchCount() )
9556 {
9557 m_colLabelWin->Refresh();
9558 }
9559 }
9560
9561 // Note: under MSW, the default column label font must be changed because it
9562 // does not support vertical printing
9563 //
9564 // Example: wxFont font(9, wxSWISS, wxNORMAL, wxBOLD);
9565 // pGrid->SetLabelFont(font);
9566 // pGrid->SetColLabelTextOrientation(wxVERTICAL);
9567 //
9568 void wxGrid::SetColLabelTextOrientation( int textOrientation )
9569 {
9570 if ( textOrientation == wxHORIZONTAL || textOrientation == wxVERTICAL )
9571 m_colLabelTextOrientation = textOrientation;
9572
9573 if ( !GetBatchCount() )
9574 m_colLabelWin->Refresh();
9575 }
9576
9577 void wxGrid::SetRowLabelValue( int row, const wxString& s )
9578 {
9579 if ( m_table )
9580 {
9581 m_table->SetRowLabelValue( row, s );
9582 if ( !GetBatchCount() )
9583 {
9584 wxRect rect = CellToRect( row, 0 );
9585 if ( rect.height > 0 )
9586 {
9587 CalcScrolledPosition(0, rect.y, &rect.x, &rect.y);
9588 rect.x = 0;
9589 rect.width = m_rowLabelWidth;
9590 m_rowLabelWin->Refresh( true, &rect );
9591 }
9592 }
9593 }
9594 }
9595
9596 void wxGrid::SetColLabelValue( int col, const wxString& s )
9597 {
9598 if ( m_table )
9599 {
9600 m_table->SetColLabelValue( col, s );
9601 if ( !GetBatchCount() )
9602 {
9603 wxRect rect = CellToRect( 0, col );
9604 if ( rect.width > 0 )
9605 {
9606 CalcScrolledPosition(rect.x, 0, &rect.x, &rect.y);
9607 rect.y = 0;
9608 rect.height = m_colLabelHeight;
9609 m_colLabelWin->Refresh( true, &rect );
9610 }
9611 }
9612 }
9613 }
9614
9615 void wxGrid::SetGridLineColour( const wxColour& colour )
9616 {
9617 if ( m_gridLineColour != colour )
9618 {
9619 m_gridLineColour = colour;
9620
9621 wxClientDC dc( m_gridWin );
9622 PrepareDC( dc );
9623 DrawAllGridLines( dc, wxRegion() );
9624 }
9625 }
9626
9627 void wxGrid::SetCellHighlightColour( const wxColour& colour )
9628 {
9629 if ( m_cellHighlightColour != colour )
9630 {
9631 m_cellHighlightColour = colour;
9632
9633 wxClientDC dc( m_gridWin );
9634 PrepareDC( dc );
9635 wxGridCellAttr* attr = GetCellAttr(m_currentCellCoords);
9636 DrawCellHighlight(dc, attr);
9637 attr->DecRef();
9638 }
9639 }
9640
9641 void wxGrid::SetCellHighlightPenWidth(int width)
9642 {
9643 if (m_cellHighlightPenWidth != width)
9644 {
9645 m_cellHighlightPenWidth = width;
9646
9647 // Just redrawing the cell highlight is not enough since that won't
9648 // make any visible change if the the thickness is getting smaller.
9649 int row = m_currentCellCoords.GetRow();
9650 int col = m_currentCellCoords.GetCol();
9651 if ( row == -1 || col == -1 || GetColWidth(col) <= 0 || GetRowHeight(row) <= 0 )
9652 return;
9653
9654 wxRect rect = CellToRect(row, col);
9655 m_gridWin->Refresh(true, &rect);
9656 }
9657 }
9658
9659 void wxGrid::SetCellHighlightROPenWidth(int width)
9660 {
9661 if (m_cellHighlightROPenWidth != width)
9662 {
9663 m_cellHighlightROPenWidth = width;
9664
9665 // Just redrawing the cell highlight is not enough since that won't
9666 // make any visible change if the the thickness is getting smaller.
9667 int row = m_currentCellCoords.GetRow();
9668 int col = m_currentCellCoords.GetCol();
9669 if ( row == -1 || col == -1 ||
9670 GetColWidth(col) <= 0 || GetRowHeight(row) <= 0 )
9671 return;
9672
9673 wxRect rect = CellToRect(row, col);
9674 m_gridWin->Refresh(true, &rect);
9675 }
9676 }
9677
9678 void wxGrid::EnableGridLines( bool enable )
9679 {
9680 if ( enable != m_gridLinesEnabled )
9681 {
9682 m_gridLinesEnabled = enable;
9683
9684 if ( !GetBatchCount() )
9685 {
9686 if ( enable )
9687 {
9688 wxClientDC dc( m_gridWin );
9689 PrepareDC( dc );
9690 DrawAllGridLines( dc, wxRegion() );
9691 }
9692 else
9693 {
9694 m_gridWin->Refresh();
9695 }
9696 }
9697 }
9698 }
9699
9700 int wxGrid::GetDefaultRowSize() const
9701 {
9702 return m_defaultRowHeight;
9703 }
9704
9705 int wxGrid::GetRowSize( int row ) const
9706 {
9707 wxCHECK_MSG( row >= 0 && row < m_numRows, 0, _T("invalid row index") );
9708
9709 return GetRowHeight(row);
9710 }
9711
9712 int wxGrid::GetDefaultColSize() const
9713 {
9714 return m_defaultColWidth;
9715 }
9716
9717 int wxGrid::GetColSize( int col ) const
9718 {
9719 wxCHECK_MSG( col >= 0 && col < m_numCols, 0, _T("invalid column index") );
9720
9721 return GetColWidth(col);
9722 }
9723
9724 // ============================================================================
9725 // access to the grid attributes: each of them has a default value in the grid
9726 // itself and may be overidden on a per-cell basis
9727 // ============================================================================
9728
9729 // ----------------------------------------------------------------------------
9730 // setting default attributes
9731 // ----------------------------------------------------------------------------
9732
9733 void wxGrid::SetDefaultCellBackgroundColour( const wxColour& col )
9734 {
9735 m_defaultCellAttr->SetBackgroundColour(col);
9736 #ifdef __WXGTK__
9737 m_gridWin->SetBackgroundColour(col);
9738 #endif
9739 }
9740
9741 void wxGrid::SetDefaultCellTextColour( const wxColour& col )
9742 {
9743 m_defaultCellAttr->SetTextColour(col);
9744 }
9745
9746 void wxGrid::SetDefaultCellAlignment( int horiz, int vert )
9747 {
9748 m_defaultCellAttr->SetAlignment(horiz, vert);
9749 }
9750
9751 void wxGrid::SetDefaultCellOverflow( bool allow )
9752 {
9753 m_defaultCellAttr->SetOverflow(allow);
9754 }
9755
9756 void wxGrid::SetDefaultCellFont( const wxFont& font )
9757 {
9758 m_defaultCellAttr->SetFont(font);
9759 }
9760
9761 // For editors and renderers the type registry takes precedence over the
9762 // default attr, so we need to register the new editor/renderer for the string
9763 // data type in order to make setting a default editor/renderer appear to
9764 // work correctly.
9765
9766 void wxGrid::SetDefaultRenderer(wxGridCellRenderer *renderer)
9767 {
9768 RegisterDataType(wxGRID_VALUE_STRING,
9769 renderer,
9770 GetDefaultEditorForType(wxGRID_VALUE_STRING));
9771 }
9772
9773 void wxGrid::SetDefaultEditor(wxGridCellEditor *editor)
9774 {
9775 RegisterDataType(wxGRID_VALUE_STRING,
9776 GetDefaultRendererForType(wxGRID_VALUE_STRING),
9777 editor);
9778 }
9779
9780 // ----------------------------------------------------------------------------
9781 // access to the default attributes
9782 // ----------------------------------------------------------------------------
9783
9784 wxColour wxGrid::GetDefaultCellBackgroundColour() const
9785 {
9786 return m_defaultCellAttr->GetBackgroundColour();
9787 }
9788
9789 wxColour wxGrid::GetDefaultCellTextColour() const
9790 {
9791 return m_defaultCellAttr->GetTextColour();
9792 }
9793
9794 wxFont wxGrid::GetDefaultCellFont() const
9795 {
9796 return m_defaultCellAttr->GetFont();
9797 }
9798
9799 void wxGrid::GetDefaultCellAlignment( int *horiz, int *vert ) const
9800 {
9801 m_defaultCellAttr->GetAlignment(horiz, vert);
9802 }
9803
9804 bool wxGrid::GetDefaultCellOverflow() const
9805 {
9806 return m_defaultCellAttr->GetOverflow();
9807 }
9808
9809 wxGridCellRenderer *wxGrid::GetDefaultRenderer() const
9810 {
9811 return m_defaultCellAttr->GetRenderer(NULL, 0, 0);
9812 }
9813
9814 wxGridCellEditor *wxGrid::GetDefaultEditor() const
9815 {
9816 return m_defaultCellAttr->GetEditor(NULL, 0, 0);
9817 }
9818
9819 // ----------------------------------------------------------------------------
9820 // access to cell attributes
9821 // ----------------------------------------------------------------------------
9822
9823 wxColour wxGrid::GetCellBackgroundColour(int row, int col) const
9824 {
9825 wxGridCellAttr *attr = GetCellAttr(row, col);
9826 wxColour colour = attr->GetBackgroundColour();
9827 attr->DecRef();
9828
9829 return colour;
9830 }
9831
9832 wxColour wxGrid::GetCellTextColour( int row, int col ) const
9833 {
9834 wxGridCellAttr *attr = GetCellAttr(row, col);
9835 wxColour colour = attr->GetTextColour();
9836 attr->DecRef();
9837
9838 return colour;
9839 }
9840
9841 wxFont wxGrid::GetCellFont( int row, int col ) const
9842 {
9843 wxGridCellAttr *attr = GetCellAttr(row, col);
9844 wxFont font = attr->GetFont();
9845 attr->DecRef();
9846
9847 return font;
9848 }
9849
9850 void wxGrid::GetCellAlignment( int row, int col, int *horiz, int *vert ) const
9851 {
9852 wxGridCellAttr *attr = GetCellAttr(row, col);
9853 attr->GetAlignment(horiz, vert);
9854 attr->DecRef();
9855 }
9856
9857 bool wxGrid::GetCellOverflow( int row, int col ) const
9858 {
9859 wxGridCellAttr *attr = GetCellAttr(row, col);
9860 bool allow = attr->GetOverflow();
9861 attr->DecRef();
9862
9863 return allow;
9864 }
9865
9866 void wxGrid::GetCellSize( int row, int col, int *num_rows, int *num_cols ) const
9867 {
9868 wxGridCellAttr *attr = GetCellAttr(row, col);
9869 attr->GetSize( num_rows, num_cols );
9870 attr->DecRef();
9871 }
9872
9873 wxGridCellRenderer* wxGrid::GetCellRenderer(int row, int col) const
9874 {
9875 wxGridCellAttr* attr = GetCellAttr(row, col);
9876 wxGridCellRenderer* renderer = attr->GetRenderer(this, row, col);
9877 attr->DecRef();
9878
9879 return renderer;
9880 }
9881
9882 wxGridCellEditor* wxGrid::GetCellEditor(int row, int col) const
9883 {
9884 wxGridCellAttr* attr = GetCellAttr(row, col);
9885 wxGridCellEditor* editor = attr->GetEditor(this, row, col);
9886 attr->DecRef();
9887
9888 return editor;
9889 }
9890
9891 bool wxGrid::IsReadOnly(int row, int col) const
9892 {
9893 wxGridCellAttr* attr = GetCellAttr(row, col);
9894 bool isReadOnly = attr->IsReadOnly();
9895 attr->DecRef();
9896
9897 return isReadOnly;
9898 }
9899
9900 // ----------------------------------------------------------------------------
9901 // attribute support: cache, automatic provider creation, ...
9902 // ----------------------------------------------------------------------------
9903
9904 bool wxGrid::CanHaveAttributes() const
9905 {
9906 if ( !m_table )
9907 {
9908 return false;
9909 }
9910
9911 return m_table->CanHaveAttributes();
9912 }
9913
9914 void wxGrid::ClearAttrCache()
9915 {
9916 if ( m_attrCache.row != -1 )
9917 {
9918 wxSafeDecRef(m_attrCache.attr);
9919 m_attrCache.attr = NULL;
9920 m_attrCache.row = -1;
9921 }
9922 }
9923
9924 void wxGrid::CacheAttr(int row, int col, wxGridCellAttr *attr) const
9925 {
9926 if ( attr != NULL )
9927 {
9928 wxGrid *self = (wxGrid *)this; // const_cast
9929
9930 self->ClearAttrCache();
9931 self->m_attrCache.row = row;
9932 self->m_attrCache.col = col;
9933 self->m_attrCache.attr = attr;
9934 wxSafeIncRef(attr);
9935 }
9936 }
9937
9938 bool wxGrid::LookupAttr(int row, int col, wxGridCellAttr **attr) const
9939 {
9940 if ( row == m_attrCache.row && col == m_attrCache.col )
9941 {
9942 *attr = m_attrCache.attr;
9943 wxSafeIncRef(m_attrCache.attr);
9944
9945 #ifdef DEBUG_ATTR_CACHE
9946 gs_nAttrCacheHits++;
9947 #endif
9948
9949 return true;
9950 }
9951 else
9952 {
9953 #ifdef DEBUG_ATTR_CACHE
9954 gs_nAttrCacheMisses++;
9955 #endif
9956
9957 return false;
9958 }
9959 }
9960
9961 wxGridCellAttr *wxGrid::GetCellAttr(int row, int col) const
9962 {
9963 wxGridCellAttr *attr = NULL;
9964 // Additional test to avoid looking at the cache e.g. for
9965 // wxNoCellCoords, as this will confuse memory management.
9966 if ( row >= 0 )
9967 {
9968 if ( !LookupAttr(row, col, &attr) )
9969 {
9970 attr = m_table ? m_table->GetAttr(row, col, wxGridCellAttr::Any)
9971 : (wxGridCellAttr *)NULL;
9972 CacheAttr(row, col, attr);
9973 }
9974 }
9975
9976 if (attr)
9977 {
9978 attr->SetDefAttr(m_defaultCellAttr);
9979 }
9980 else
9981 {
9982 attr = m_defaultCellAttr;
9983 attr->IncRef();
9984 }
9985
9986 return attr;
9987 }
9988
9989 wxGridCellAttr *wxGrid::GetOrCreateCellAttr(int row, int col) const
9990 {
9991 wxGridCellAttr *attr = (wxGridCellAttr *)NULL;
9992 bool canHave = ((wxGrid*)this)->CanHaveAttributes();
9993
9994 wxCHECK_MSG( canHave, attr, _T("Cell attributes not allowed"));
9995 wxCHECK_MSG( m_table, attr, _T("must have a table") );
9996
9997 attr = m_table->GetAttr(row, col, wxGridCellAttr::Cell);
9998 if ( !attr )
9999 {
10000 attr = new wxGridCellAttr(m_defaultCellAttr);
10001
10002 // artificially inc the ref count to match DecRef() in caller
10003 attr->IncRef();
10004 m_table->SetAttr(attr, row, col);
10005 }
10006
10007 return attr;
10008 }
10009
10010 // ----------------------------------------------------------------------------
10011 // setting column attributes (wrappers around SetColAttr)
10012 // ----------------------------------------------------------------------------
10013
10014 void wxGrid::SetColFormatBool(int col)
10015 {
10016 SetColFormatCustom(col, wxGRID_VALUE_BOOL);
10017 }
10018
10019 void wxGrid::SetColFormatNumber(int col)
10020 {
10021 SetColFormatCustom(col, wxGRID_VALUE_NUMBER);
10022 }
10023
10024 void wxGrid::SetColFormatFloat(int col, int width, int precision)
10025 {
10026 wxString typeName = wxGRID_VALUE_FLOAT;
10027 if ( (width != -1) || (precision != -1) )
10028 {
10029 typeName << _T(':') << width << _T(',') << precision;
10030 }
10031
10032 SetColFormatCustom(col, typeName);
10033 }
10034
10035 void wxGrid::SetColFormatCustom(int col, const wxString& typeName)
10036 {
10037 wxGridCellAttr *attr = m_table->GetAttr(-1, col, wxGridCellAttr::Col );
10038 if (!attr)
10039 attr = new wxGridCellAttr;
10040 wxGridCellRenderer *renderer = GetDefaultRendererForType(typeName);
10041 attr->SetRenderer(renderer);
10042 wxGridCellEditor *editor = GetDefaultEditorForType(typeName);
10043 attr->SetEditor(editor);
10044
10045 SetColAttr(col, attr);
10046
10047 }
10048
10049 // ----------------------------------------------------------------------------
10050 // setting cell attributes: this is forwarded to the table
10051 // ----------------------------------------------------------------------------
10052
10053 void wxGrid::SetAttr(int row, int col, wxGridCellAttr *attr)
10054 {
10055 if ( CanHaveAttributes() )
10056 {
10057 m_table->SetAttr(attr, row, col);
10058 ClearAttrCache();
10059 }
10060 else
10061 {
10062 wxSafeDecRef(attr);
10063 }
10064 }
10065
10066 void wxGrid::SetRowAttr(int row, wxGridCellAttr *attr)
10067 {
10068 if ( CanHaveAttributes() )
10069 {
10070 m_table->SetRowAttr(attr, row);
10071 ClearAttrCache();
10072 }
10073 else
10074 {
10075 wxSafeDecRef(attr);
10076 }
10077 }
10078
10079 void wxGrid::SetColAttr(int col, wxGridCellAttr *attr)
10080 {
10081 if ( CanHaveAttributes() )
10082 {
10083 m_table->SetColAttr(attr, col);
10084 ClearAttrCache();
10085 }
10086 else
10087 {
10088 wxSafeDecRef(attr);
10089 }
10090 }
10091
10092 void wxGrid::SetCellBackgroundColour( int row, int col, const wxColour& colour )
10093 {
10094 if ( CanHaveAttributes() )
10095 {
10096 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
10097 attr->SetBackgroundColour(colour);
10098 attr->DecRef();
10099 }
10100 }
10101
10102 void wxGrid::SetCellTextColour( int row, int col, const wxColour& colour )
10103 {
10104 if ( CanHaveAttributes() )
10105 {
10106 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
10107 attr->SetTextColour(colour);
10108 attr->DecRef();
10109 }
10110 }
10111
10112 void wxGrid::SetCellFont( int row, int col, const wxFont& font )
10113 {
10114 if ( CanHaveAttributes() )
10115 {
10116 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
10117 attr->SetFont(font);
10118 attr->DecRef();
10119 }
10120 }
10121
10122 void wxGrid::SetCellAlignment( int row, int col, int horiz, int vert )
10123 {
10124 if ( CanHaveAttributes() )
10125 {
10126 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
10127 attr->SetAlignment(horiz, vert);
10128 attr->DecRef();
10129 }
10130 }
10131
10132 void wxGrid::SetCellOverflow( int row, int col, bool allow )
10133 {
10134 if ( CanHaveAttributes() )
10135 {
10136 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
10137 attr->SetOverflow(allow);
10138 attr->DecRef();
10139 }
10140 }
10141
10142 void wxGrid::SetCellSize( int row, int col, int num_rows, int num_cols )
10143 {
10144 if ( CanHaveAttributes() )
10145 {
10146 int cell_rows, cell_cols;
10147
10148 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
10149 attr->GetSize(&cell_rows, &cell_cols);
10150 attr->SetSize(num_rows, num_cols);
10151 attr->DecRef();
10152
10153 // Cannot set the size of a cell to 0 or negative values
10154 // While it is perfectly legal to do that, this function cannot
10155 // handle all the possibilies, do it by hand by getting the CellAttr.
10156 // You can only set the size of a cell to 1,1 or greater with this fn
10157 wxASSERT_MSG( !((cell_rows < 1) || (cell_cols < 1)),
10158 wxT("wxGrid::SetCellSize setting cell size that is already part of another cell"));
10159 wxASSERT_MSG( !((num_rows < 1) || (num_cols < 1)),
10160 wxT("wxGrid::SetCellSize setting cell size to < 1"));
10161
10162 // if this was already a multicell then "turn off" the other cells first
10163 if ((cell_rows > 1) || (cell_rows > 1))
10164 {
10165 int i, j;
10166 for (j=row; j < row + cell_rows; j++)
10167 {
10168 for (i=col; i < col + cell_cols; i++)
10169 {
10170 if ((i != col) || (j != row))
10171 {
10172 wxGridCellAttr *attr_stub = GetOrCreateCellAttr(j, i);
10173 attr_stub->SetSize( 1, 1 );
10174 attr_stub->DecRef();
10175 }
10176 }
10177 }
10178 }
10179
10180 // mark the cells that will be covered by this cell to
10181 // negative or zero values to point back at this cell
10182 if (((num_rows > 1) || (num_cols > 1)) && (num_rows >= 1) && (num_cols >= 1))
10183 {
10184 int i, j;
10185 for (j=row; j < row + num_rows; j++)
10186 {
10187 for (i=col; i < col + num_cols; i++)
10188 {
10189 if ((i != col) || (j != row))
10190 {
10191 wxGridCellAttr *attr_stub = GetOrCreateCellAttr(j, i);
10192 attr_stub->SetSize( row - j, col - i );
10193 attr_stub->DecRef();
10194 }
10195 }
10196 }
10197 }
10198 }
10199 }
10200
10201 void wxGrid::SetCellRenderer(int row, int col, wxGridCellRenderer *renderer)
10202 {
10203 if ( CanHaveAttributes() )
10204 {
10205 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
10206 attr->SetRenderer(renderer);
10207 attr->DecRef();
10208 }
10209 }
10210
10211 void wxGrid::SetCellEditor(int row, int col, wxGridCellEditor* editor)
10212 {
10213 if ( CanHaveAttributes() )
10214 {
10215 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
10216 attr->SetEditor(editor);
10217 attr->DecRef();
10218 }
10219 }
10220
10221 void wxGrid::SetReadOnly(int row, int col, bool isReadOnly)
10222 {
10223 if ( CanHaveAttributes() )
10224 {
10225 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
10226 attr->SetReadOnly(isReadOnly);
10227 attr->DecRef();
10228 }
10229 }
10230
10231 // ----------------------------------------------------------------------------
10232 // Data type registration
10233 // ----------------------------------------------------------------------------
10234
10235 void wxGrid::RegisterDataType(const wxString& typeName,
10236 wxGridCellRenderer* renderer,
10237 wxGridCellEditor* editor)
10238 {
10239 m_typeRegistry->RegisterDataType(typeName, renderer, editor);
10240 }
10241
10242
10243 wxGridCellEditor * wxGrid::GetDefaultEditorForCell(int row, int col) const
10244 {
10245 wxString typeName = m_table->GetTypeName(row, col);
10246 return GetDefaultEditorForType(typeName);
10247 }
10248
10249 wxGridCellRenderer * wxGrid::GetDefaultRendererForCell(int row, int col) const
10250 {
10251 wxString typeName = m_table->GetTypeName(row, col);
10252 return GetDefaultRendererForType(typeName);
10253 }
10254
10255 wxGridCellEditor * wxGrid::GetDefaultEditorForType(const wxString& typeName) const
10256 {
10257 int index = m_typeRegistry->FindOrCloneDataType(typeName);
10258 if ( index == wxNOT_FOUND )
10259 {
10260 wxFAIL_MSG(wxString::Format(wxT("Unknown data type name [%s]"), typeName.c_str()));
10261
10262 return NULL;
10263 }
10264
10265 return m_typeRegistry->GetEditor(index);
10266 }
10267
10268 wxGridCellRenderer * wxGrid::GetDefaultRendererForType(const wxString& typeName) const
10269 {
10270 int index = m_typeRegistry->FindOrCloneDataType(typeName);
10271 if ( index == wxNOT_FOUND )
10272 {
10273 wxFAIL_MSG(wxString::Format(wxT("Unknown data type name [%s]"), typeName.c_str()));
10274
10275 return NULL;
10276 }
10277
10278 return m_typeRegistry->GetRenderer(index);
10279 }
10280
10281 // ----------------------------------------------------------------------------
10282 // row/col size
10283 // ----------------------------------------------------------------------------
10284
10285 void wxGrid::EnableDragRowSize( bool enable )
10286 {
10287 m_canDragRowSize = enable;
10288 }
10289
10290 void wxGrid::EnableDragColSize( bool enable )
10291 {
10292 m_canDragColSize = enable;
10293 }
10294
10295 void wxGrid::EnableDragGridSize( bool enable )
10296 {
10297 m_canDragGridSize = enable;
10298 }
10299
10300 void wxGrid::EnableDragCell( bool enable )
10301 {
10302 m_canDragCell = enable;
10303 }
10304
10305 void wxGrid::SetDefaultRowSize( int height, bool resizeExistingRows )
10306 {
10307 m_defaultRowHeight = wxMax( height, m_minAcceptableRowHeight );
10308
10309 if ( resizeExistingRows )
10310 {
10311 // since we are resizing all rows to the default row size,
10312 // we can simply clear the row heights and row bottoms
10313 // arrays (which also allows us to take advantage of
10314 // some speed optimisations)
10315 m_rowHeights.Empty();
10316 m_rowBottoms.Empty();
10317 if ( !GetBatchCount() )
10318 CalcDimensions();
10319 }
10320 }
10321
10322 void wxGrid::SetRowSize( int row, int height )
10323 {
10324 wxCHECK_RET( row >= 0 && row < m_numRows, _T("invalid row index") );
10325
10326 // See comment in SetColSize
10327 if ( height < GetRowMinimalAcceptableHeight())
10328 return;
10329
10330 if ( m_rowHeights.IsEmpty() )
10331 {
10332 // need to really create the array
10333 InitRowHeights();
10334 }
10335
10336 int h = wxMax( 0, height );
10337 int diff = h - m_rowHeights[row];
10338
10339 m_rowHeights[row] = h;
10340 for ( int i = row; i < m_numRows; i++ )
10341 {
10342 m_rowBottoms[i] += diff;
10343 }
10344
10345 if ( !GetBatchCount() )
10346 CalcDimensions();
10347 }
10348
10349 void wxGrid::SetDefaultColSize( int width, bool resizeExistingCols )
10350 {
10351 // we dont allow zero default column width
10352 m_defaultColWidth = wxMax( wxMax( width, m_minAcceptableColWidth ), 1 );
10353
10354 if ( resizeExistingCols )
10355 {
10356 // since we are resizing all columns to the default column size,
10357 // we can simply clear the col widths and col rights
10358 // arrays (which also allows us to take advantage of
10359 // some speed optimisations)
10360 m_colWidths.Empty();
10361 m_colRights.Empty();
10362 if ( !GetBatchCount() )
10363 CalcDimensions();
10364 }
10365 }
10366
10367 void wxGrid::SetColSize( int col, int width )
10368 {
10369 wxCHECK_RET( col >= 0 && col < m_numCols, _T("invalid column index") );
10370
10371 // should we check that it's bigger than GetColMinimalWidth(col) here?
10372 // (VZ)
10373 // No, because it is reasonable to assume the library user know's
10374 // what he is doing. However we should test against the weaker
10375 // constraint of minimalAcceptableWidth, as this breaks rendering
10376 //
10377 // This test then fixes sf.net bug #645734
10378
10379 if ( width < GetColMinimalAcceptableWidth() )
10380 return;
10381
10382 if ( m_colWidths.IsEmpty() )
10383 {
10384 // need to really create the array
10385 InitColWidths();
10386 }
10387
10388 // if < 0 then calculate new width from label
10389 if ( width < 0 )
10390 {
10391 long w, h;
10392 wxArrayString lines;
10393 wxClientDC dc(m_colLabelWin);
10394 dc.SetFont(GetLabelFont());
10395 StringToLines(GetColLabelValue(col), lines);
10396 GetTextBoxSize(dc, lines, &w, &h);
10397 width = w + 6;
10398 }
10399
10400 int w = wxMax( 0, width );
10401 int diff = w - m_colWidths[col];
10402 m_colWidths[col] = w;
10403
10404 for ( int colPos = GetColPos(col); colPos < m_numCols; colPos++ )
10405 {
10406 m_colRights[GetColAt(colPos)] += diff;
10407 }
10408
10409 if ( !GetBatchCount() )
10410 CalcDimensions();
10411 }
10412
10413 void wxGrid::SetColMinimalWidth( int col, int width )
10414 {
10415 if (width > GetColMinimalAcceptableWidth())
10416 {
10417 wxLongToLongHashMap::key_type key = (wxLongToLongHashMap::key_type)col;
10418 m_colMinWidths[key] = width;
10419 }
10420 }
10421
10422 void wxGrid::SetRowMinimalHeight( int row, int width )
10423 {
10424 if (width > GetRowMinimalAcceptableHeight())
10425 {
10426 wxLongToLongHashMap::key_type key = (wxLongToLongHashMap::key_type)row;
10427 m_rowMinHeights[key] = width;
10428 }
10429 }
10430
10431 int wxGrid::GetColMinimalWidth(int col) const
10432 {
10433 wxLongToLongHashMap::key_type key = (wxLongToLongHashMap::key_type)col;
10434 wxLongToLongHashMap::const_iterator it = m_colMinWidths.find(key);
10435
10436 return it != m_colMinWidths.end() ? (int)it->second : m_minAcceptableColWidth;
10437 }
10438
10439 int wxGrid::GetRowMinimalHeight(int row) const
10440 {
10441 wxLongToLongHashMap::key_type key = (wxLongToLongHashMap::key_type)row;
10442 wxLongToLongHashMap::const_iterator it = m_rowMinHeights.find(key);
10443
10444 return it != m_rowMinHeights.end() ? (int)it->second : m_minAcceptableRowHeight;
10445 }
10446
10447 void wxGrid::SetColMinimalAcceptableWidth( int width )
10448 {
10449 // We do allow a width of 0 since this gives us
10450 // an easy way to temporarily hiding columns.
10451 if ( width >= 0 )
10452 m_minAcceptableColWidth = width;
10453 }
10454
10455 void wxGrid::SetRowMinimalAcceptableHeight( int height )
10456 {
10457 // We do allow a height of 0 since this gives us
10458 // an easy way to temporarily hiding rows.
10459 if ( height >= 0 )
10460 m_minAcceptableRowHeight = height;
10461 }
10462
10463 int wxGrid::GetColMinimalAcceptableWidth() const
10464 {
10465 return m_minAcceptableColWidth;
10466 }
10467
10468 int wxGrid::GetRowMinimalAcceptableHeight() const
10469 {
10470 return m_minAcceptableRowHeight;
10471 }
10472
10473 // ----------------------------------------------------------------------------
10474 // auto sizing
10475 // ----------------------------------------------------------------------------
10476
10477 void
10478 wxGrid::AutoSizeColOrRow(int colOrRow, bool setAsMin, wxGridDirection direction)
10479 {
10480 const bool column = direction == wxGRID_COLUMN;
10481
10482 wxClientDC dc(m_gridWin);
10483
10484 // cancel editing of cell
10485 HideCellEditControl();
10486 SaveEditControlValue();
10487
10488 // init both of them to avoid compiler warnings, even if we only need one
10489 int row = -1,
10490 col = -1;
10491 if ( column )
10492 col = colOrRow;
10493 else
10494 row = colOrRow;
10495
10496 wxCoord extent, extentMax = 0;
10497 int max = column ? m_numRows : m_numCols;
10498 for ( int rowOrCol = 0; rowOrCol < max; rowOrCol++ )
10499 {
10500 if ( column )
10501 row = rowOrCol;
10502 else
10503 col = rowOrCol;
10504
10505 wxGridCellAttr *attr = GetCellAttr(row, col);
10506 wxGridCellRenderer *renderer = attr->GetRenderer(this, row, col);
10507 if ( renderer )
10508 {
10509 wxSize size = renderer->GetBestSize(*this, *attr, dc, row, col);
10510 extent = column ? size.x : size.y;
10511 if ( extent > extentMax )
10512 extentMax = extent;
10513
10514 renderer->DecRef();
10515 }
10516
10517 attr->DecRef();
10518 }
10519
10520 // now also compare with the column label extent
10521 wxCoord w, h;
10522 dc.SetFont( GetLabelFont() );
10523
10524 if ( column )
10525 {
10526 dc.GetMultiLineTextExtent( GetColLabelValue(col), &w, &h );
10527 if ( GetColLabelTextOrientation() == wxVERTICAL )
10528 w = h;
10529 }
10530 else
10531 dc.GetMultiLineTextExtent( GetRowLabelValue(row), &w, &h );
10532
10533 extent = column ? w : h;
10534 if ( extent > extentMax )
10535 extentMax = extent;
10536
10537 if ( !extentMax )
10538 {
10539 // empty column - give default extent (notice that if extentMax is less
10540 // than default extent but != 0, it's OK)
10541 extentMax = column ? m_defaultColWidth : m_defaultRowHeight;
10542 }
10543 else
10544 {
10545 if ( column )
10546 // leave some space around text
10547 extentMax += 10;
10548 else
10549 extentMax += 6;
10550 }
10551
10552 if ( column )
10553 {
10554 // Ensure automatic width is not less than minimal width. See the
10555 // comment in SetColSize() for explanation of why this isn't done
10556 // in SetColSize().
10557 if ( !setAsMin )
10558 extentMax = wxMax(extentMax, GetColMinimalWidth(col));
10559
10560 SetColSize( col, extentMax );
10561 if ( !GetBatchCount() )
10562 {
10563 int cw, ch, dummy;
10564 m_gridWin->GetClientSize( &cw, &ch );
10565 wxRect rect ( CellToRect( 0, col ) );
10566 rect.y = 0;
10567 CalcScrolledPosition(rect.x, 0, &rect.x, &dummy);
10568 rect.width = cw - rect.x;
10569 rect.height = m_colLabelHeight;
10570 m_colLabelWin->Refresh( true, &rect );
10571 }
10572 }
10573 else
10574 {
10575 // Ensure automatic width is not less than minimal height. See the
10576 // comment in SetColSize() for explanation of why this isn't done
10577 // in SetRowSize().
10578 if ( !setAsMin )
10579 extentMax = wxMax(extentMax, GetRowMinimalHeight(row));
10580
10581 SetRowSize(row, extentMax);
10582 if ( !GetBatchCount() )
10583 {
10584 int cw, ch, dummy;
10585 m_gridWin->GetClientSize( &cw, &ch );
10586 wxRect rect( CellToRect( row, 0 ) );
10587 rect.x = 0;
10588 CalcScrolledPosition(0, rect.y, &dummy, &rect.y);
10589 rect.width = m_rowLabelWidth;
10590 rect.height = ch - rect.y;
10591 m_rowLabelWin->Refresh( true, &rect );
10592 }
10593 }
10594
10595 if ( setAsMin )
10596 {
10597 if ( column )
10598 SetColMinimalWidth(col, extentMax);
10599 else
10600 SetRowMinimalHeight(row, extentMax);
10601 }
10602 }
10603
10604 wxCoord wxGrid::CalcColOrRowLabelAreaMinSize(wxGridDirection direction)
10605 {
10606 // calculate size for the rows or columns?
10607 const bool calcRows = direction == wxGRID_ROW;
10608
10609 wxClientDC dc(calcRows ? GetGridRowLabelWindow()
10610 : GetGridColLabelWindow());
10611 dc.SetFont(GetLabelFont());
10612
10613 // which dimension should we take into account for calculations?
10614 //
10615 // for columns, the text can be only horizontal so it's easy but for rows
10616 // we also have to take into account the text orientation
10617 const bool
10618 useWidth = calcRows || (GetColLabelTextOrientation() == wxVERTICAL);
10619
10620 wxArrayString lines;
10621 wxCoord extentMax = 0;
10622
10623 const int numRowsOrCols = calcRows ? m_numRows : m_numCols;
10624 for ( int rowOrCol = 0; rowOrCol < numRowsOrCols; rowOrCol++ )
10625 {
10626 lines.Clear();
10627
10628 wxString label = calcRows ? GetRowLabelValue(rowOrCol)
10629 : GetColLabelValue(rowOrCol);
10630 StringToLines(label, lines);
10631
10632 long w, h;
10633 GetTextBoxSize(dc, lines, &w, &h);
10634
10635 const wxCoord extent = useWidth ? w : h;
10636 if ( extent > extentMax )
10637 extentMax = extent;
10638 }
10639
10640 if ( !extentMax )
10641 {
10642 // empty column - give default extent (notice that if extentMax is less
10643 // than default extent but != 0, it's OK)
10644 extentMax = calcRows ? GetDefaultRowLabelSize()
10645 : GetDefaultColLabelSize();
10646 }
10647
10648 // leave some space around text (taken from AutoSizeColOrRow)
10649 if ( calcRows )
10650 extentMax += 10;
10651 else
10652 extentMax += 6;
10653
10654 return extentMax;
10655 }
10656
10657 int wxGrid::SetOrCalcColumnSizes(bool calcOnly, bool setAsMin)
10658 {
10659 int width = m_rowLabelWidth;
10660
10661 wxGridUpdateLocker locker;
10662 if(!calcOnly)
10663 locker.Create(this);
10664
10665 for ( int col = 0; col < m_numCols; col++ )
10666 {
10667 if ( !calcOnly )
10668 AutoSizeColumn(col, setAsMin);
10669
10670 width += GetColWidth(col);
10671 }
10672
10673 return width;
10674 }
10675
10676 int wxGrid::SetOrCalcRowSizes(bool calcOnly, bool setAsMin)
10677 {
10678 int height = m_colLabelHeight;
10679
10680 wxGridUpdateLocker locker;
10681 if(!calcOnly)
10682 locker.Create(this);
10683
10684 for ( int row = 0; row < m_numRows; row++ )
10685 {
10686 if ( !calcOnly )
10687 AutoSizeRow(row, setAsMin);
10688
10689 height += GetRowHeight(row);
10690 }
10691
10692 return height;
10693 }
10694
10695 void wxGrid::AutoSize()
10696 {
10697 wxGridUpdateLocker locker(this);
10698
10699 // we need to round up the size of the scrollable area to a multiple of
10700 // scroll step to ensure that we don't get the scrollbars when we're sized
10701 // exactly to fit our contents
10702 wxSize size(SetOrCalcColumnSizes(false) - m_rowLabelWidth + m_extraWidth,
10703 SetOrCalcRowSizes(false) - m_colLabelHeight + m_extraHeight);
10704 wxSize sizeFit(GetScrollX(size.x) * GetScrollLineX(),
10705 GetScrollY(size.y) * GetScrollLineY());
10706
10707 // distribute the extra space between the columns/rows to avoid having
10708 // extra white space
10709 wxCoord diff = sizeFit.x - size.x;
10710 if ( diff && m_numCols )
10711 {
10712 // try to resize the columns uniformly
10713 wxCoord diffPerCol = diff / m_numCols;
10714 if ( diffPerCol )
10715 {
10716 for ( int col = 0; col < m_numCols; col++ )
10717 {
10718 SetColSize(col, GetColWidth(col) + diffPerCol);
10719 }
10720 }
10721
10722 // add remaining amount to the last columns
10723 diff -= diffPerCol * m_numCols;
10724 if ( diff )
10725 {
10726 for ( int col = m_numCols - 1; col >= m_numCols - diff; col-- )
10727 {
10728 SetColSize(col, GetColWidth(col) + 1);
10729 }
10730 }
10731 }
10732
10733 // same for rows
10734 diff = sizeFit.y - size.y;
10735 if ( diff && m_numRows )
10736 {
10737 // try to resize the columns uniformly
10738 wxCoord diffPerRow = diff / m_numRows;
10739 if ( diffPerRow )
10740 {
10741 for ( int row = 0; row < m_numRows; row++ )
10742 {
10743 SetRowSize(row, GetRowHeight(row) + diffPerRow);
10744 }
10745 }
10746
10747 // add remaining amount to the last rows
10748 diff -= diffPerRow * m_numRows;
10749 if ( diff )
10750 {
10751 for ( int row = m_numRows - 1; row >= m_numRows - diff; row-- )
10752 {
10753 SetRowSize(row, GetRowHeight(row) + 1);
10754 }
10755 }
10756 }
10757
10758 // we know that we're not going to have scrollbars so disable them now to
10759 // avoid trouble in SetClientSize() which can otherwise set the correct
10760 // client size but also leave space for (not needed any more) scrollbars
10761 SetScrollbars(0, 0, 0, 0, 0, 0, true);
10762 SetClientSize(sizeFit.x + m_rowLabelWidth, sizeFit.y + m_colLabelHeight);
10763 }
10764
10765 void wxGrid::AutoSizeRowLabelSize( int row )
10766 {
10767 wxArrayString lines;
10768 long w, h;
10769
10770 // Hide the edit control, so it
10771 // won't interfere with drag-shrinking.
10772 if ( IsCellEditControlShown() )
10773 {
10774 HideCellEditControl();
10775 SaveEditControlValue();
10776 }
10777
10778 // autosize row height depending on label text
10779 StringToLines( GetRowLabelValue( row ), lines );
10780 wxClientDC dc( m_rowLabelWin );
10781 GetTextBoxSize( dc, lines, &w, &h );
10782 if ( h < m_defaultRowHeight )
10783 h = m_defaultRowHeight;
10784 SetRowSize(row, h);
10785 ForceRefresh();
10786 }
10787
10788 void wxGrid::AutoSizeColLabelSize( int col )
10789 {
10790 wxArrayString lines;
10791 long w, h;
10792
10793 // Hide the edit control, so it
10794 // won't interfere with drag-shrinking.
10795 if ( IsCellEditControlShown() )
10796 {
10797 HideCellEditControl();
10798 SaveEditControlValue();
10799 }
10800
10801 // autosize column width depending on label text
10802 StringToLines( GetColLabelValue( col ), lines );
10803 wxClientDC dc( m_colLabelWin );
10804 if ( GetColLabelTextOrientation() == wxHORIZONTAL )
10805 GetTextBoxSize( dc, lines, &w, &h );
10806 else
10807 GetTextBoxSize( dc, lines, &h, &w );
10808 if ( w < m_defaultColWidth )
10809 w = m_defaultColWidth;
10810 SetColSize(col, w);
10811 ForceRefresh();
10812 }
10813
10814 wxSize wxGrid::DoGetBestSize() const
10815 {
10816 wxGrid *self = (wxGrid *)this; // const_cast
10817
10818 // we do the same as in AutoSize() here with the exception that we don't
10819 // change the column/row sizes, only calculate them
10820 wxSize size(self->SetOrCalcColumnSizes(true) - m_rowLabelWidth + m_extraWidth,
10821 self->SetOrCalcRowSizes(true) - m_colLabelHeight + m_extraHeight);
10822 wxSize sizeFit(GetScrollX(size.x) * GetScrollLineX(),
10823 GetScrollY(size.y) * GetScrollLineY());
10824
10825 // NOTE: This size should be cached, but first we need to add calls to
10826 // InvalidateBestSize everywhere that could change the results of this
10827 // calculation.
10828 // CacheBestSize(size);
10829
10830 return wxSize(sizeFit.x + m_rowLabelWidth, sizeFit.y + m_colLabelHeight)
10831 + GetWindowBorderSize();
10832 }
10833
10834 void wxGrid::Fit()
10835 {
10836 AutoSize();
10837 }
10838
10839 wxPen& wxGrid::GetDividerPen() const
10840 {
10841 return wxNullPen;
10842 }
10843
10844 // ----------------------------------------------------------------------------
10845 // cell value accessor functions
10846 // ----------------------------------------------------------------------------
10847
10848 void wxGrid::SetCellValue( int row, int col, const wxString& s )
10849 {
10850 if ( m_table )
10851 {
10852 m_table->SetValue( row, col, s );
10853 if ( !GetBatchCount() )
10854 {
10855 int dummy;
10856 wxRect rect( CellToRect( row, col ) );
10857 rect.x = 0;
10858 rect.width = m_gridWin->GetClientSize().GetWidth();
10859 CalcScrolledPosition(0, rect.y, &dummy, &rect.y);
10860 m_gridWin->Refresh( false, &rect );
10861 }
10862
10863 if ( m_currentCellCoords.GetRow() == row &&
10864 m_currentCellCoords.GetCol() == col &&
10865 IsCellEditControlShown())
10866 // Note: If we are using IsCellEditControlEnabled,
10867 // this interacts badly with calling SetCellValue from
10868 // an EVT_GRID_CELL_CHANGE handler.
10869 {
10870 HideCellEditControl();
10871 ShowCellEditControl(); // will reread data from table
10872 }
10873 }
10874 }
10875
10876 // ----------------------------------------------------------------------------
10877 // block, row and column selection
10878 // ----------------------------------------------------------------------------
10879
10880 void wxGrid::SelectRow( int row, bool addToSelected )
10881 {
10882 if ( IsSelection() && !addToSelected )
10883 ClearSelection();
10884
10885 if ( m_selection )
10886 m_selection->SelectRow( row, false, addToSelected );
10887 }
10888
10889 void wxGrid::SelectCol( int col, bool addToSelected )
10890 {
10891 if ( IsSelection() && !addToSelected )
10892 ClearSelection();
10893
10894 if ( m_selection )
10895 m_selection->SelectCol( col, false, addToSelected );
10896 }
10897
10898 void wxGrid::SelectBlock( int topRow, int leftCol, int bottomRow, int rightCol,
10899 bool addToSelected )
10900 {
10901 if ( IsSelection() && !addToSelected )
10902 ClearSelection();
10903
10904 if ( m_selection )
10905 m_selection->SelectBlock( topRow, leftCol, bottomRow, rightCol,
10906 false, addToSelected );
10907 }
10908
10909 void wxGrid::SelectAll()
10910 {
10911 if ( m_numRows > 0 && m_numCols > 0 )
10912 {
10913 if ( m_selection )
10914 m_selection->SelectBlock( 0, 0, m_numRows - 1, m_numCols - 1 );
10915 }
10916 }
10917
10918 // ----------------------------------------------------------------------------
10919 // cell, row and col deselection
10920 // ----------------------------------------------------------------------------
10921
10922 void wxGrid::DeselectRow( int row )
10923 {
10924 if ( !m_selection )
10925 return;
10926
10927 if ( m_selection->GetSelectionMode() == wxGrid::wxGridSelectRows )
10928 {
10929 if ( m_selection->IsInSelection(row, 0 ) )
10930 m_selection->ToggleCellSelection(row, 0);
10931 }
10932 else
10933 {
10934 int nCols = GetNumberCols();
10935 for ( int i = 0; i < nCols; i++ )
10936 {
10937 if ( m_selection->IsInSelection(row, i ) )
10938 m_selection->ToggleCellSelection(row, i);
10939 }
10940 }
10941 }
10942
10943 void wxGrid::DeselectCol( int col )
10944 {
10945 if ( !m_selection )
10946 return;
10947
10948 if ( m_selection->GetSelectionMode() == wxGrid::wxGridSelectColumns )
10949 {
10950 if ( m_selection->IsInSelection(0, col ) )
10951 m_selection->ToggleCellSelection(0, col);
10952 }
10953 else
10954 {
10955 int nRows = GetNumberRows();
10956 for ( int i = 0; i < nRows; i++ )
10957 {
10958 if ( m_selection->IsInSelection(i, col ) )
10959 m_selection->ToggleCellSelection(i, col);
10960 }
10961 }
10962 }
10963
10964 void wxGrid::DeselectCell( int row, int col )
10965 {
10966 if ( m_selection && m_selection->IsInSelection(row, col) )
10967 m_selection->ToggleCellSelection(row, col);
10968 }
10969
10970 bool wxGrid::IsSelection() const
10971 {
10972 return ( m_selection && (m_selection->IsSelection() ||
10973 ( m_selectingTopLeft != wxGridNoCellCoords &&
10974 m_selectingBottomRight != wxGridNoCellCoords) ) );
10975 }
10976
10977 bool wxGrid::IsInSelection( int row, int col ) const
10978 {
10979 return ( m_selection && (m_selection->IsInSelection( row, col ) ||
10980 ( row >= m_selectingTopLeft.GetRow() &&
10981 col >= m_selectingTopLeft.GetCol() &&
10982 row <= m_selectingBottomRight.GetRow() &&
10983 col <= m_selectingBottomRight.GetCol() )) );
10984 }
10985
10986 wxGridCellCoordsArray wxGrid::GetSelectedCells() const
10987 {
10988 if (!m_selection)
10989 {
10990 wxGridCellCoordsArray a;
10991 return a;
10992 }
10993
10994 return m_selection->m_cellSelection;
10995 }
10996
10997 wxGridCellCoordsArray wxGrid::GetSelectionBlockTopLeft() const
10998 {
10999 if (!m_selection)
11000 {
11001 wxGridCellCoordsArray a;
11002 return a;
11003 }
11004
11005 return m_selection->m_blockSelectionTopLeft;
11006 }
11007
11008 wxGridCellCoordsArray wxGrid::GetSelectionBlockBottomRight() const
11009 {
11010 if (!m_selection)
11011 {
11012 wxGridCellCoordsArray a;
11013 return a;
11014 }
11015
11016 return m_selection->m_blockSelectionBottomRight;
11017 }
11018
11019 wxArrayInt wxGrid::GetSelectedRows() const
11020 {
11021 if (!m_selection)
11022 {
11023 wxArrayInt a;
11024 return a;
11025 }
11026
11027 return m_selection->m_rowSelection;
11028 }
11029
11030 wxArrayInt wxGrid::GetSelectedCols() const
11031 {
11032 if (!m_selection)
11033 {
11034 wxArrayInt a;
11035 return a;
11036 }
11037
11038 return m_selection->m_colSelection;
11039 }
11040
11041 void wxGrid::ClearSelection()
11042 {
11043 wxRect r1 = BlockToDeviceRect( m_selectingTopLeft, m_selectingBottomRight);
11044 wxRect r2 = BlockToDeviceRect( m_currentCellCoords, m_selectingKeyboard );
11045 m_selectingTopLeft =
11046 m_selectingBottomRight =
11047 m_selectingKeyboard = wxGridNoCellCoords;
11048 Refresh( false, &r1 );
11049 Refresh( false, &r2 );
11050 if ( m_selection )
11051 m_selection->ClearSelection();
11052 }
11053
11054 // This function returns the rectangle that encloses the given block
11055 // in device coords clipped to the client size of the grid window.
11056 //
11057 wxRect wxGrid::BlockToDeviceRect( const wxGridCellCoords& topLeft,
11058 const wxGridCellCoords& bottomRight ) const
11059 {
11060 wxRect resultRect;
11061 wxRect tempCellRect = CellToRect(topLeft);
11062 if ( tempCellRect != wxGridNoCellRect )
11063 {
11064 resultRect = tempCellRect;
11065 }
11066 else
11067 {
11068 resultRect = wxRect(0, 0, 0, 0);
11069 }
11070
11071 tempCellRect = CellToRect(bottomRight);
11072 if ( tempCellRect != wxGridNoCellRect )
11073 {
11074 resultRect += tempCellRect;
11075 }
11076 else
11077 {
11078 // If both inputs were "wxGridNoCellRect," then there's nothing to do.
11079 return wxGridNoCellRect;
11080 }
11081
11082 // Ensure that left/right and top/bottom pairs are in order.
11083 int left = resultRect.GetLeft();
11084 int top = resultRect.GetTop();
11085 int right = resultRect.GetRight();
11086 int bottom = resultRect.GetBottom();
11087
11088 int leftCol = topLeft.GetCol();
11089 int topRow = topLeft.GetRow();
11090 int rightCol = bottomRight.GetCol();
11091 int bottomRow = bottomRight.GetRow();
11092
11093 if (left > right)
11094 {
11095 int tmp = left;
11096 left = right;
11097 right = tmp;
11098
11099 tmp = leftCol;
11100 leftCol = rightCol;
11101 rightCol = tmp;
11102 }
11103
11104 if (top > bottom)
11105 {
11106 int tmp = top;
11107 top = bottom;
11108 bottom = tmp;
11109
11110 tmp = topRow;
11111 topRow = bottomRow;
11112 bottomRow = tmp;
11113 }
11114
11115 // The following loop is ONLY necessary to detect and handle merged cells.
11116 int cw, ch;
11117 m_gridWin->GetClientSize( &cw, &ch );
11118
11119 // Get the origin coordinates: notice that they will be negative if the
11120 // grid is scrolled downwards/to the right.
11121 int gridOriginX = 0;
11122 int gridOriginY = 0;
11123 CalcScrolledPosition(gridOriginX, gridOriginY, &gridOriginX, &gridOriginY);
11124
11125 int onScreenLeftmostCol = internalXToCol(-gridOriginX);
11126 int onScreenUppermostRow = internalYToRow(-gridOriginY);
11127
11128 int onScreenRightmostCol = internalXToCol(-gridOriginX + cw);
11129 int onScreenBottommostRow = internalYToRow(-gridOriginY + ch);
11130
11131 // Bound our loop so that we only examine the portion of the selected block
11132 // that is shown on screen. Therefore, we compare the Top-Left block values
11133 // to the Top-Left screen values, and the Bottom-Right block values to the
11134 // Bottom-Right screen values, choosing appropriately.
11135 const int visibleTopRow = wxMax(topRow, onScreenUppermostRow);
11136 const int visibleBottomRow = wxMin(bottomRow, onScreenBottommostRow);
11137 const int visibleLeftCol = wxMax(leftCol, onScreenLeftmostCol);
11138 const int visibleRightCol = wxMin(rightCol, onScreenRightmostCol);
11139
11140 for ( int j = visibleTopRow; j <= visibleBottomRow; j++ )
11141 {
11142 for ( int i = visibleLeftCol; i <= visibleRightCol; i++ )
11143 {
11144 if ( (j == visibleTopRow) || (j == visibleBottomRow) ||
11145 (i == visibleLeftCol) || (i == visibleRightCol) )
11146 {
11147 tempCellRect = CellToRect( j, i );
11148
11149 if (tempCellRect.x < left)
11150 left = tempCellRect.x;
11151 if (tempCellRect.y < top)
11152 top = tempCellRect.y;
11153 if (tempCellRect.x + tempCellRect.width > right)
11154 right = tempCellRect.x + tempCellRect.width;
11155 if (tempCellRect.y + tempCellRect.height > bottom)
11156 bottom = tempCellRect.y + tempCellRect.height;
11157 }
11158 else
11159 {
11160 i = visibleRightCol; // jump over inner cells.
11161 }
11162 }
11163 }
11164
11165 // Convert to scrolled coords
11166 CalcScrolledPosition( left, top, &left, &top );
11167 CalcScrolledPosition( right, bottom, &right, &bottom );
11168
11169 if (right < 0 || bottom < 0 || left > cw || top > ch)
11170 return wxRect(0,0,0,0);
11171
11172 resultRect.SetLeft( wxMax(0, left) );
11173 resultRect.SetTop( wxMax(0, top) );
11174 resultRect.SetRight( wxMin(cw, right) );
11175 resultRect.SetBottom( wxMin(ch, bottom) );
11176
11177 return resultRect;
11178 }
11179
11180 // ----------------------------------------------------------------------------
11181 // drop target
11182 // ----------------------------------------------------------------------------
11183
11184 #if wxUSE_DRAG_AND_DROP
11185
11186 // this allow setting drop target directly on wxGrid
11187 void wxGrid::SetDropTarget(wxDropTarget *dropTarget)
11188 {
11189 GetGridWindow()->SetDropTarget(dropTarget);
11190 }
11191
11192 #endif // wxUSE_DRAG_AND_DROP
11193
11194 // ----------------------------------------------------------------------------
11195 // grid event classes
11196 // ----------------------------------------------------------------------------
11197
11198 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent, wxNotifyEvent )
11199
11200 wxGridEvent::wxGridEvent( int id, wxEventType type, wxObject* obj,
11201 int row, int col, int x, int y, bool sel,
11202 bool control, bool shift, bool alt, bool meta )
11203 : wxNotifyEvent( type, id )
11204 {
11205 m_row = row;
11206 m_col = col;
11207 m_x = x;
11208 m_y = y;
11209 m_selecting = sel;
11210 m_control = control;
11211 m_shift = shift;
11212 m_alt = alt;
11213 m_meta = meta;
11214
11215 SetEventObject(obj);
11216 }
11217
11218
11219 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent, wxNotifyEvent )
11220
11221 wxGridSizeEvent::wxGridSizeEvent( int id, wxEventType type, wxObject* obj,
11222 int rowOrCol, int x, int y,
11223 bool control, bool shift, bool alt, bool meta )
11224 : wxNotifyEvent( type, id )
11225 {
11226 m_rowOrCol = rowOrCol;
11227 m_x = x;
11228 m_y = y;
11229 m_control = control;
11230 m_shift = shift;
11231 m_alt = alt;
11232 m_meta = meta;
11233
11234 SetEventObject(obj);
11235 }
11236
11237
11238 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent, wxNotifyEvent )
11239
11240 wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id, wxEventType type, wxObject* obj,
11241 const wxGridCellCoords& topLeft,
11242 const wxGridCellCoords& bottomRight,
11243 bool sel, bool control,
11244 bool shift, bool alt, bool meta )
11245 : wxNotifyEvent( type, id )
11246 {
11247 m_topLeft = topLeft;
11248 m_bottomRight = bottomRight;
11249 m_selecting = sel;
11250 m_control = control;
11251 m_shift = shift;
11252 m_alt = alt;
11253 m_meta = meta;
11254
11255 SetEventObject(obj);
11256 }
11257
11258
11259 IMPLEMENT_DYNAMIC_CLASS(wxGridEditorCreatedEvent, wxCommandEvent)
11260
11261 wxGridEditorCreatedEvent::wxGridEditorCreatedEvent(int id, wxEventType type,
11262 wxObject* obj, int row,
11263 int col, wxControl* ctrl)
11264 : wxCommandEvent(type, id)
11265 {
11266 SetEventObject(obj);
11267 m_row = row;
11268 m_col = col;
11269 m_ctrl = ctrl;
11270 }
11271
11272 #endif // wxUSE_GRID