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