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