]> git.saurik.com Git - wxWidgets.git/blob - src/generic/grid.cpp
1d30f7baf5d80880fb64630b7bdfff550275aa77
[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 SetScrollRate(m_scrollLineX, m_scrollLineY);
4226 CalcDimensions();
4227
4228 return true;
4229 }
4230
4231 wxGrid::~wxGrid()
4232 {
4233 // Must do this or ~wxScrollHelper will pop the wrong event handler
4234 SetTargetWindow(this);
4235 ClearAttrCache();
4236 wxSafeDecRef(m_defaultCellAttr);
4237
4238 #ifdef DEBUG_ATTR_CACHE
4239 size_t total = gs_nAttrCacheHits + gs_nAttrCacheMisses;
4240 wxPrintf(_T("wxGrid attribute cache statistics: "
4241 "total: %u, hits: %u (%u%%)\n"),
4242 total, gs_nAttrCacheHits,
4243 total ? (gs_nAttrCacheHits*100) / total : 0);
4244 #endif
4245
4246 // if we own the table, just delete it, otherwise at least don't leave it
4247 // with dangling view pointer
4248 if ( m_ownTable )
4249 delete m_table;
4250 else if ( m_table && m_table->GetView() == this )
4251 m_table->SetView(NULL);
4252
4253 delete m_typeRegistry;
4254 delete m_selection;
4255 }
4256
4257 //
4258 // ----- internal init and update functions
4259 //
4260
4261 // NOTE: If using the default visual attributes works everywhere then this can
4262 // be removed as well as the #else cases below.
4263 #define _USE_VISATTR 0
4264
4265 void wxGrid::Create()
4266 {
4267 // create the type registry
4268 m_typeRegistry = new wxGridTypeRegistry;
4269
4270 m_cellEditCtrlEnabled = false;
4271
4272 m_defaultCellAttr = new wxGridCellAttr();
4273
4274 // Set default cell attributes
4275 m_defaultCellAttr->SetDefAttr(m_defaultCellAttr);
4276 m_defaultCellAttr->SetKind(wxGridCellAttr::Default);
4277 m_defaultCellAttr->SetFont(GetFont());
4278 m_defaultCellAttr->SetAlignment(wxALIGN_LEFT, wxALIGN_TOP);
4279 m_defaultCellAttr->SetRenderer(new wxGridCellStringRenderer);
4280 m_defaultCellAttr->SetEditor(new wxGridCellTextEditor);
4281
4282 #if _USE_VISATTR
4283 wxVisualAttributes gva = wxListBox::GetClassDefaultAttributes();
4284 wxVisualAttributes lva = wxPanel::GetClassDefaultAttributes();
4285
4286 m_defaultCellAttr->SetTextColour(gva.colFg);
4287 m_defaultCellAttr->SetBackgroundColour(gva.colBg);
4288
4289 #else
4290 m_defaultCellAttr->SetTextColour(
4291 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT));
4292 m_defaultCellAttr->SetBackgroundColour(
4293 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW));
4294 #endif
4295
4296 m_numRows = 0;
4297 m_numCols = 0;
4298 m_currentCellCoords = wxGridNoCellCoords;
4299
4300 m_rowLabelWidth = WXGRID_DEFAULT_ROW_LABEL_WIDTH;
4301 m_colLabelHeight = WXGRID_DEFAULT_COL_LABEL_HEIGHT;
4302
4303 // subwindow components that make up the wxGrid
4304 m_rowLabelWin = new wxGridRowLabelWindow( this,
4305 wxID_ANY,
4306 wxDefaultPosition,
4307 wxDefaultSize );
4308
4309 m_colLabelWin = new wxGridColLabelWindow( this,
4310 wxID_ANY,
4311 wxDefaultPosition,
4312 wxDefaultSize );
4313
4314 m_cornerLabelWin = new wxGridCornerLabelWindow( this,
4315 wxID_ANY,
4316 wxDefaultPosition,
4317 wxDefaultSize );
4318
4319 m_gridWin = new wxGridWindow( this,
4320 m_rowLabelWin,
4321 m_colLabelWin,
4322 wxID_ANY,
4323 wxDefaultPosition,
4324 wxDefaultSize );
4325
4326 SetTargetWindow( m_gridWin );
4327
4328 #if _USE_VISATTR
4329 wxColour gfg = gva.colFg;
4330 wxColour gbg = gva.colBg;
4331 wxColour lfg = lva.colFg;
4332 wxColour lbg = lva.colBg;
4333 #else
4334 wxColour gfg = wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT );
4335 wxColour gbg = wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOW );
4336 wxColour lfg = wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT );
4337 wxColour lbg = wxSystemSettings::GetColour( wxSYS_COLOUR_BTNFACE );
4338 #endif
4339
4340 m_cornerLabelWin->SetOwnForegroundColour(lfg);
4341 m_cornerLabelWin->SetOwnBackgroundColour(lbg);
4342 m_rowLabelWin->SetOwnForegroundColour(lfg);
4343 m_rowLabelWin->SetOwnBackgroundColour(lbg);
4344 m_colLabelWin->SetOwnForegroundColour(lfg);
4345 m_colLabelWin->SetOwnBackgroundColour(lbg);
4346
4347 m_gridWin->SetOwnForegroundColour(gfg);
4348 m_gridWin->SetOwnBackgroundColour(gbg);
4349
4350 Init();
4351 }
4352
4353 bool wxGrid::CreateGrid( int numRows, int numCols,
4354 wxGrid::wxGridSelectionModes selmode )
4355 {
4356 wxCHECK_MSG( !m_created,
4357 false,
4358 wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
4359
4360 m_numRows = numRows;
4361 m_numCols = numCols;
4362
4363 m_table = new wxGridStringTable( m_numRows, m_numCols );
4364 m_table->SetView( this );
4365 m_ownTable = true;
4366 m_selection = new wxGridSelection( this, selmode );
4367
4368 CalcDimensions();
4369
4370 m_created = true;
4371
4372 return m_created;
4373 }
4374
4375 void wxGrid::SetSelectionMode(wxGrid::wxGridSelectionModes selmode)
4376 {
4377 wxCHECK_RET( m_created,
4378 wxT("Called wxGrid::SetSelectionMode() before calling CreateGrid()") );
4379
4380 m_selection->SetSelectionMode( selmode );
4381 }
4382
4383 wxGrid::wxGridSelectionModes wxGrid::GetSelectionMode() const
4384 {
4385 wxCHECK_MSG( m_created, wxGrid::wxGridSelectCells,
4386 wxT("Called wxGrid::GetSelectionMode() before calling CreateGrid()") );
4387
4388 return m_selection->GetSelectionMode();
4389 }
4390
4391 bool wxGrid::SetTable( wxGridTableBase *table, bool takeOwnership,
4392 wxGrid::wxGridSelectionModes selmode )
4393 {
4394 bool checkSelection = false;
4395 if ( m_created )
4396 {
4397 // stop all processing
4398 m_created = false;
4399
4400 if (m_table)
4401 {
4402 m_table->SetView(0);
4403 if( m_ownTable )
4404 delete m_table;
4405 m_table = NULL;
4406 }
4407
4408 delete m_selection;
4409 m_selection = NULL;
4410
4411 m_ownTable = false;
4412 m_numRows = 0;
4413 m_numCols = 0;
4414 checkSelection = true;
4415
4416 // kill row and column size arrays
4417 m_colWidths.Empty();
4418 m_colRights.Empty();
4419 m_rowHeights.Empty();
4420 m_rowBottoms.Empty();
4421 }
4422
4423 if (table)
4424 {
4425 m_numRows = table->GetNumberRows();
4426 m_numCols = table->GetNumberCols();
4427
4428 m_table = table;
4429 m_table->SetView( this );
4430 m_ownTable = takeOwnership;
4431 m_selection = new wxGridSelection( this, selmode );
4432 if (checkSelection)
4433 {
4434 // If the newly set table is smaller than the
4435 // original one current cell and selection regions
4436 // might be invalid,
4437 m_selectingKeyboard = wxGridNoCellCoords;
4438 m_currentCellCoords =
4439 wxGridCellCoords(wxMin(m_numRows, m_currentCellCoords.GetRow()),
4440 wxMin(m_numCols, m_currentCellCoords.GetCol()));
4441 if (m_selectingTopLeft.GetRow() >= m_numRows ||
4442 m_selectingTopLeft.GetCol() >= m_numCols)
4443 {
4444 m_selectingTopLeft = wxGridNoCellCoords;
4445 m_selectingBottomRight = wxGridNoCellCoords;
4446 }
4447 else
4448 m_selectingBottomRight =
4449 wxGridCellCoords(wxMin(m_numRows,
4450 m_selectingBottomRight.GetRow()),
4451 wxMin(m_numCols,
4452 m_selectingBottomRight.GetCol()));
4453 }
4454 CalcDimensions();
4455
4456 m_created = true;
4457 }
4458
4459 return m_created;
4460 }
4461
4462 void wxGrid::InitVars()
4463 {
4464 m_created = false;
4465
4466 m_cornerLabelWin = NULL;
4467 m_rowLabelWin = NULL;
4468 m_colLabelWin = NULL;
4469 m_gridWin = NULL;
4470
4471 m_table = NULL;
4472 m_ownTable = false;
4473
4474 m_selection = NULL;
4475 m_defaultCellAttr = NULL;
4476 m_typeRegistry = NULL;
4477 m_winCapture = NULL;
4478 }
4479
4480 void wxGrid::Init()
4481 {
4482 m_rowLabelWidth = WXGRID_DEFAULT_ROW_LABEL_WIDTH;
4483 m_colLabelHeight = WXGRID_DEFAULT_COL_LABEL_HEIGHT;
4484
4485 if ( m_rowLabelWin )
4486 {
4487 m_labelBackgroundColour = m_rowLabelWin->GetBackgroundColour();
4488 }
4489 else
4490 {
4491 m_labelBackgroundColour = *wxWHITE;
4492 }
4493
4494 m_labelTextColour = *wxBLACK;
4495
4496 // init attr cache
4497 m_attrCache.row = -1;
4498 m_attrCache.col = -1;
4499 m_attrCache.attr = NULL;
4500
4501 // TODO: something better than this ?
4502 //
4503 m_labelFont = this->GetFont();
4504 m_labelFont.SetWeight( wxBOLD );
4505
4506 m_rowLabelHorizAlign = wxALIGN_CENTRE;
4507 m_rowLabelVertAlign = wxALIGN_CENTRE;
4508
4509 m_colLabelHorizAlign = wxALIGN_CENTRE;
4510 m_colLabelVertAlign = wxALIGN_CENTRE;
4511 m_colLabelTextOrientation = wxHORIZONTAL;
4512
4513 m_defaultColWidth = WXGRID_DEFAULT_COL_WIDTH;
4514 m_defaultRowHeight = m_gridWin->GetCharHeight();
4515
4516 m_minAcceptableColWidth = WXGRID_MIN_COL_WIDTH;
4517 m_minAcceptableRowHeight = WXGRID_MIN_ROW_HEIGHT;
4518
4519 #if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl()
4520 m_defaultRowHeight += 8;
4521 #else
4522 m_defaultRowHeight += 4;
4523 #endif
4524
4525 m_gridLineColour = wxColour( 192,192,192 );
4526 m_gridLinesEnabled = true;
4527 m_cellHighlightColour = *wxBLACK;
4528 m_cellHighlightPenWidth = 2;
4529 m_cellHighlightROPenWidth = 1;
4530
4531 m_canDragColMove = false;
4532
4533 m_cursorMode = WXGRID_CURSOR_SELECT_CELL;
4534 m_winCapture = (wxWindow *)NULL;
4535 m_canDragRowSize = true;
4536 m_canDragColSize = true;
4537 m_canDragGridSize = true;
4538 m_canDragCell = false;
4539 m_dragLastPos = -1;
4540 m_dragRowOrCol = -1;
4541 m_isDragging = false;
4542 m_startDragPos = wxDefaultPosition;
4543 m_nativeColumnLabels = false;
4544
4545 m_waitForSlowClick = false;
4546
4547 m_rowResizeCursor = wxCursor( wxCURSOR_SIZENS );
4548 m_colResizeCursor = wxCursor( wxCURSOR_SIZEWE );
4549
4550 m_currentCellCoords = wxGridNoCellCoords;
4551
4552 ClearSelection();
4553
4554 m_selectionBackground = wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT);
4555 m_selectionForeground = wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT);
4556
4557 m_editable = true; // default for whole grid
4558
4559 m_inOnKeyDown = false;
4560 m_batchCount = 0;
4561
4562 m_extraWidth =
4563 m_extraHeight = 0;
4564
4565 m_scrollLineX = GRID_SCROLL_LINE_X;
4566 m_scrollLineY = GRID_SCROLL_LINE_Y;
4567 }
4568
4569 // ----------------------------------------------------------------------------
4570 // the idea is to call these functions only when necessary because they create
4571 // quite big arrays which eat memory mostly unnecessary - in particular, if
4572 // default widths/heights are used for all rows/columns, we may not use these
4573 // arrays at all
4574 //
4575 // with some extra code, it should be possible to only store the widths/heights
4576 // different from default ones (resulting in space savings for huge grids) but
4577 // this is not done currently
4578 // ----------------------------------------------------------------------------
4579
4580 void wxGrid::InitRowHeights()
4581 {
4582 m_rowHeights.Empty();
4583 m_rowBottoms.Empty();
4584
4585 m_rowHeights.Alloc( m_numRows );
4586 m_rowBottoms.Alloc( m_numRows );
4587
4588 m_rowHeights.Add( m_defaultRowHeight, m_numRows );
4589
4590 int rowBottom = 0;
4591 for ( int i = 0; i < m_numRows; i++ )
4592 {
4593 rowBottom += m_defaultRowHeight;
4594 m_rowBottoms.Add( rowBottom );
4595 }
4596 }
4597
4598 void wxGrid::InitColWidths()
4599 {
4600 m_colWidths.Empty();
4601 m_colRights.Empty();
4602
4603 m_colWidths.Alloc( m_numCols );
4604 m_colRights.Alloc( m_numCols );
4605
4606 m_colWidths.Add( m_defaultColWidth, m_numCols );
4607
4608 int colRight = 0;
4609 for ( int i = 0; i < m_numCols; i++ )
4610 {
4611 colRight = ( GetColPos( i ) + 1 ) * m_defaultColWidth;
4612 m_colRights.Add( colRight );
4613 }
4614 }
4615
4616 int wxGrid::GetColWidth(int col) const
4617 {
4618 return m_colWidths.IsEmpty() ? m_defaultColWidth : m_colWidths[col];
4619 }
4620
4621 int wxGrid::GetColLeft(int col) const
4622 {
4623 return m_colRights.IsEmpty() ? GetColPos( col ) * m_defaultColWidth
4624 : m_colRights[col] - m_colWidths[col];
4625 }
4626
4627 int wxGrid::GetColRight(int col) const
4628 {
4629 return m_colRights.IsEmpty() ? (GetColPos( col ) + 1) * m_defaultColWidth
4630 : m_colRights[col];
4631 }
4632
4633 int wxGrid::GetRowHeight(int row) const
4634 {
4635 return m_rowHeights.IsEmpty() ? m_defaultRowHeight : m_rowHeights[row];
4636 }
4637
4638 int wxGrid::GetRowTop(int row) const
4639 {
4640 return m_rowBottoms.IsEmpty() ? row * m_defaultRowHeight
4641 : m_rowBottoms[row] - m_rowHeights[row];
4642 }
4643
4644 int wxGrid::GetRowBottom(int row) const
4645 {
4646 return m_rowBottoms.IsEmpty() ? (row + 1) * m_defaultRowHeight
4647 : m_rowBottoms[row];
4648 }
4649
4650 void wxGrid::CalcDimensions()
4651 {
4652 // compute the size of the scrollable area
4653 int w = m_numCols > 0 ? GetColRight(GetColAt(m_numCols - 1)) : 0;
4654 int h = m_numRows > 0 ? GetRowBottom(m_numRows - 1) : 0;
4655
4656 w += m_extraWidth;
4657 h += m_extraHeight;
4658
4659 // take into account editor if shown
4660 if ( IsCellEditControlShown() )
4661 {
4662 int w2, h2;
4663 int r = m_currentCellCoords.GetRow();
4664 int c = m_currentCellCoords.GetCol();
4665 int x = GetColLeft(c);
4666 int y = GetRowTop(r);
4667
4668 // how big is the editor
4669 wxGridCellAttr* attr = GetCellAttr(r, c);
4670 wxGridCellEditor* editor = attr->GetEditor(this, r, c);
4671 editor->GetControl()->GetSize(&w2, &h2);
4672 w2 += x;
4673 h2 += y;
4674 if ( w2 > w )
4675 w = w2;
4676 if ( h2 > h )
4677 h = h2;
4678 editor->DecRef();
4679 attr->DecRef();
4680 }
4681
4682 // preserve (more or less) the previous position
4683 int x, y;
4684 GetViewStart( &x, &y );
4685
4686 // ensure the position is valid for the new scroll ranges
4687 if ( x >= w )
4688 x = wxMax( w - 1, 0 );
4689 if ( y >= h )
4690 y = wxMax( h - 1, 0 );
4691
4692 // update the virtual size and refresh the scrollbars to reflect it
4693 m_gridWin->SetVirtualSize(w, h);
4694 Scroll(x, y);
4695 AdjustScrollbars();
4696
4697 // if our OnSize() hadn't been called (it would if we have scrollbars), we
4698 // still must reposition the children
4699 CalcWindowSizes();
4700 }
4701
4702 wxSize wxGrid::GetSizeAvailableForScrollTarget(const wxSize& size)
4703 {
4704 wxSize sizeGridWin(size);
4705 sizeGridWin.x -= m_rowLabelWidth;
4706 sizeGridWin.y -= m_colLabelHeight;
4707
4708 return sizeGridWin;
4709 }
4710
4711 void wxGrid::CalcWindowSizes()
4712 {
4713 // escape if the window is has not been fully created yet
4714
4715 if ( m_cornerLabelWin == NULL )
4716 return;
4717
4718 int cw, ch;
4719 GetClientSize( &cw, &ch );
4720
4721 // the grid may be too small to have enough space for the labels yet, don't
4722 // size the windows to negative sizes in this case
4723 int gw = cw - m_rowLabelWidth;
4724 int gh = ch - m_colLabelHeight;
4725 if (gw < 0)
4726 gw = 0;
4727 if (gh < 0)
4728 gh = 0;
4729
4730 if ( m_cornerLabelWin && m_cornerLabelWin->IsShown() )
4731 m_cornerLabelWin->SetSize( 0, 0, m_rowLabelWidth, m_colLabelHeight );
4732
4733 if ( m_colLabelWin && m_colLabelWin->IsShown() )
4734 m_colLabelWin->SetSize( m_rowLabelWidth, 0, gw, m_colLabelHeight );
4735
4736 if ( m_rowLabelWin && m_rowLabelWin->IsShown() )
4737 m_rowLabelWin->SetSize( 0, m_colLabelHeight, m_rowLabelWidth, gh );
4738
4739 if ( m_gridWin && m_gridWin->IsShown() )
4740 m_gridWin->SetSize( m_rowLabelWidth, m_colLabelHeight, gw, gh );
4741 }
4742
4743 // this is called when the grid table sends a message
4744 // to indicate that it has been redimensioned
4745 //
4746 bool wxGrid::Redimension( wxGridTableMessage& msg )
4747 {
4748 int i;
4749 bool result = false;
4750
4751 // Clear the attribute cache as the attribute might refer to a different
4752 // cell than stored in the cache after adding/removing rows/columns.
4753 ClearAttrCache();
4754
4755 // By the same reasoning, the editor should be dismissed if columns are
4756 // added or removed. And for consistency, it should IMHO always be
4757 // removed, not only if the cell "underneath" it actually changes.
4758 // For now, I intentionally do not save the editor's content as the
4759 // cell it might want to save that stuff to might no longer exist.
4760 HideCellEditControl();
4761
4762 #if 0
4763 // if we were using the default widths/heights so far, we must change them
4764 // now
4765 if ( m_colWidths.IsEmpty() )
4766 {
4767 InitColWidths();
4768 }
4769
4770 if ( m_rowHeights.IsEmpty() )
4771 {
4772 InitRowHeights();
4773 }
4774 #endif
4775
4776 switch ( msg.GetId() )
4777 {
4778 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED:
4779 {
4780 size_t pos = msg.GetCommandInt();
4781 int numRows = msg.GetCommandInt2();
4782
4783 m_numRows += numRows;
4784
4785 if ( !m_rowHeights.IsEmpty() )
4786 {
4787 m_rowHeights.Insert( m_defaultRowHeight, pos, numRows );
4788 m_rowBottoms.Insert( 0, pos, numRows );
4789
4790 int bottom = 0;
4791 if ( pos > 0 )
4792 bottom = m_rowBottoms[pos - 1];
4793
4794 for ( i = pos; i < m_numRows; i++ )
4795 {
4796 bottom += m_rowHeights[i];
4797 m_rowBottoms[i] = bottom;
4798 }
4799 }
4800
4801 if ( m_currentCellCoords == wxGridNoCellCoords )
4802 {
4803 // if we have just inserted cols into an empty grid the current
4804 // cell will be undefined...
4805 //
4806 SetCurrentCell( 0, 0 );
4807 }
4808
4809 if ( m_selection )
4810 m_selection->UpdateRows( pos, numRows );
4811 wxGridCellAttrProvider * attrProvider = m_table->GetAttrProvider();
4812 if (attrProvider)
4813 attrProvider->UpdateAttrRows( pos, numRows );
4814
4815 if ( !GetBatchCount() )
4816 {
4817 CalcDimensions();
4818 m_rowLabelWin->Refresh();
4819 }
4820 }
4821 result = true;
4822 break;
4823
4824 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED:
4825 {
4826 int numRows = msg.GetCommandInt();
4827 int oldNumRows = m_numRows;
4828 m_numRows += numRows;
4829
4830 if ( !m_rowHeights.IsEmpty() )
4831 {
4832 m_rowHeights.Add( m_defaultRowHeight, numRows );
4833 m_rowBottoms.Add( 0, numRows );
4834
4835 int bottom = 0;
4836 if ( oldNumRows > 0 )
4837 bottom = m_rowBottoms[oldNumRows - 1];
4838
4839 for ( i = oldNumRows; i < m_numRows; i++ )
4840 {
4841 bottom += m_rowHeights[i];
4842 m_rowBottoms[i] = bottom;
4843 }
4844 }
4845
4846 if ( m_currentCellCoords == wxGridNoCellCoords )
4847 {
4848 // if we have just inserted cols into an empty grid the current
4849 // cell will be undefined...
4850 //
4851 SetCurrentCell( 0, 0 );
4852 }
4853
4854 if ( !GetBatchCount() )
4855 {
4856 CalcDimensions();
4857 m_rowLabelWin->Refresh();
4858 }
4859 }
4860 result = true;
4861 break;
4862
4863 case wxGRIDTABLE_NOTIFY_ROWS_DELETED:
4864 {
4865 size_t pos = msg.GetCommandInt();
4866 int numRows = msg.GetCommandInt2();
4867 m_numRows -= numRows;
4868
4869 if ( !m_rowHeights.IsEmpty() )
4870 {
4871 m_rowHeights.RemoveAt( pos, numRows );
4872 m_rowBottoms.RemoveAt( pos, numRows );
4873
4874 int h = 0;
4875 for ( i = 0; i < m_numRows; i++ )
4876 {
4877 h += m_rowHeights[i];
4878 m_rowBottoms[i] = h;
4879 }
4880 }
4881
4882 if ( !m_numRows )
4883 {
4884 m_currentCellCoords = wxGridNoCellCoords;
4885 }
4886 else
4887 {
4888 if ( m_currentCellCoords.GetRow() >= m_numRows )
4889 m_currentCellCoords.Set( 0, 0 );
4890 }
4891
4892 if ( m_selection )
4893 m_selection->UpdateRows( pos, -((int)numRows) );
4894 wxGridCellAttrProvider * attrProvider = m_table->GetAttrProvider();
4895 if (attrProvider)
4896 {
4897 attrProvider->UpdateAttrRows( pos, -((int)numRows) );
4898
4899 // ifdef'd out following patch from Paul Gammans
4900 #if 0
4901 // No need to touch column attributes, unless we
4902 // removed _all_ rows, in this case, we remove
4903 // all column attributes.
4904 // I hate to do this here, but the
4905 // needed data is not available inside UpdateAttrRows.
4906 if ( !GetNumberRows() )
4907 attrProvider->UpdateAttrCols( 0, -GetNumberCols() );
4908 #endif
4909 }
4910
4911 if ( !GetBatchCount() )
4912 {
4913 CalcDimensions();
4914 m_rowLabelWin->Refresh();
4915 }
4916 }
4917 result = true;
4918 break;
4919
4920 case wxGRIDTABLE_NOTIFY_COLS_INSERTED:
4921 {
4922 size_t pos = msg.GetCommandInt();
4923 int numCols = msg.GetCommandInt2();
4924 m_numCols += numCols;
4925
4926 if ( !m_colAt.IsEmpty() )
4927 {
4928 //Shift the column IDs
4929 int i;
4930 for ( i = 0; i < m_numCols - numCols; i++ )
4931 {
4932 if ( m_colAt[i] >= (int)pos )
4933 m_colAt[i] += numCols;
4934 }
4935
4936 m_colAt.Insert( pos, pos, numCols );
4937
4938 //Set the new columns' positions
4939 for ( i = pos + 1; i < (int)pos + numCols; i++ )
4940 {
4941 m_colAt[i] = i;
4942 }
4943 }
4944
4945 if ( !m_colWidths.IsEmpty() )
4946 {
4947 m_colWidths.Insert( m_defaultColWidth, pos, numCols );
4948 m_colRights.Insert( 0, pos, numCols );
4949
4950 int right = 0;
4951 if ( pos > 0 )
4952 right = m_colRights[GetColAt( pos - 1 )];
4953
4954 int colPos;
4955 for ( colPos = pos; colPos < m_numCols; colPos++ )
4956 {
4957 i = GetColAt( colPos );
4958
4959 right += m_colWidths[i];
4960 m_colRights[i] = right;
4961 }
4962 }
4963
4964 if ( m_currentCellCoords == wxGridNoCellCoords )
4965 {
4966 // if we have just inserted cols into an empty grid the current
4967 // cell will be undefined...
4968 //
4969 SetCurrentCell( 0, 0 );
4970 }
4971
4972 if ( m_selection )
4973 m_selection->UpdateCols( pos, numCols );
4974 wxGridCellAttrProvider * attrProvider = m_table->GetAttrProvider();
4975 if (attrProvider)
4976 attrProvider->UpdateAttrCols( pos, numCols );
4977 if ( !GetBatchCount() )
4978 {
4979 CalcDimensions();
4980 m_colLabelWin->Refresh();
4981 }
4982 }
4983 result = true;
4984 break;
4985
4986 case wxGRIDTABLE_NOTIFY_COLS_APPENDED:
4987 {
4988 int numCols = msg.GetCommandInt();
4989 int oldNumCols = m_numCols;
4990 m_numCols += numCols;
4991
4992 if ( !m_colAt.IsEmpty() )
4993 {
4994 m_colAt.Add( 0, numCols );
4995
4996 //Set the new columns' positions
4997 int i;
4998 for ( i = oldNumCols; i < m_numCols; i++ )
4999 {
5000 m_colAt[i] = i;
5001 }
5002 }
5003
5004 if ( !m_colWidths.IsEmpty() )
5005 {
5006 m_colWidths.Add( m_defaultColWidth, numCols );
5007 m_colRights.Add( 0, numCols );
5008
5009 int right = 0;
5010 if ( oldNumCols > 0 )
5011 right = m_colRights[GetColAt( oldNumCols - 1 )];
5012
5013 int colPos;
5014 for ( colPos = oldNumCols; colPos < m_numCols; colPos++ )
5015 {
5016 i = GetColAt( colPos );
5017
5018 right += m_colWidths[i];
5019 m_colRights[i] = right;
5020 }
5021 }
5022
5023 if ( m_currentCellCoords == wxGridNoCellCoords )
5024 {
5025 // if we have just inserted cols into an empty grid the current
5026 // cell will be undefined...
5027 //
5028 SetCurrentCell( 0, 0 );
5029 }
5030 if ( !GetBatchCount() )
5031 {
5032 CalcDimensions();
5033 m_colLabelWin->Refresh();
5034 }
5035 }
5036 result = true;
5037 break;
5038
5039 case wxGRIDTABLE_NOTIFY_COLS_DELETED:
5040 {
5041 size_t pos = msg.GetCommandInt();
5042 int numCols = msg.GetCommandInt2();
5043 m_numCols -= numCols;
5044
5045 if ( !m_colAt.IsEmpty() )
5046 {
5047 int colID = GetColAt( pos );
5048
5049 m_colAt.RemoveAt( pos, numCols );
5050
5051 //Shift the column IDs
5052 int colPos;
5053 for ( colPos = 0; colPos < m_numCols; colPos++ )
5054 {
5055 if ( m_colAt[colPos] > colID )
5056 m_colAt[colPos] -= numCols;
5057 }
5058 }
5059
5060 if ( !m_colWidths.IsEmpty() )
5061 {
5062 m_colWidths.RemoveAt( pos, numCols );
5063 m_colRights.RemoveAt( pos, numCols );
5064
5065 int w = 0;
5066 int colPos;
5067 for ( colPos = 0; colPos < m_numCols; colPos++ )
5068 {
5069 i = GetColAt( colPos );
5070
5071 w += m_colWidths[i];
5072 m_colRights[i] = w;
5073 }
5074 }
5075
5076 if ( !m_numCols )
5077 {
5078 m_currentCellCoords = wxGridNoCellCoords;
5079 }
5080 else
5081 {
5082 if ( m_currentCellCoords.GetCol() >= m_numCols )
5083 m_currentCellCoords.Set( 0, 0 );
5084 }
5085
5086 if ( m_selection )
5087 m_selection->UpdateCols( pos, -((int)numCols) );
5088 wxGridCellAttrProvider * attrProvider = m_table->GetAttrProvider();
5089 if (attrProvider)
5090 {
5091 attrProvider->UpdateAttrCols( pos, -((int)numCols) );
5092
5093 // ifdef'd out following patch from Paul Gammans
5094 #if 0
5095 // No need to touch row attributes, unless we
5096 // removed _all_ columns, in this case, we remove
5097 // all row attributes.
5098 // I hate to do this here, but the
5099 // needed data is not available inside UpdateAttrCols.
5100 if ( !GetNumberCols() )
5101 attrProvider->UpdateAttrRows( 0, -GetNumberRows() );
5102 #endif
5103 }
5104
5105 if ( !GetBatchCount() )
5106 {
5107 CalcDimensions();
5108 m_colLabelWin->Refresh();
5109 }
5110 }
5111 result = true;
5112 break;
5113 }
5114
5115 if (result && !GetBatchCount() )
5116 m_gridWin->Refresh();
5117
5118 return result;
5119 }
5120
5121 wxArrayInt wxGrid::CalcRowLabelsExposed( const wxRegion& reg ) const
5122 {
5123 wxRegionIterator iter( reg );
5124 wxRect r;
5125
5126 wxArrayInt rowlabels;
5127
5128 int top, bottom;
5129 while ( iter )
5130 {
5131 r = iter.GetRect();
5132
5133 // TODO: remove this when we can...
5134 // There is a bug in wxMotif that gives garbage update
5135 // rectangles if you jump-scroll a long way by clicking the
5136 // scrollbar with middle button. This is a work-around
5137 //
5138 #if defined(__WXMOTIF__)
5139 int cw, ch;
5140 m_gridWin->GetClientSize( &cw, &ch );
5141 if ( r.GetTop() > ch )
5142 r.SetTop( 0 );
5143 r.SetBottom( wxMin( r.GetBottom(), ch ) );
5144 #endif
5145
5146 // logical bounds of update region
5147 //
5148 int dummy;
5149 CalcUnscrolledPosition( 0, r.GetTop(), &dummy, &top );
5150 CalcUnscrolledPosition( 0, r.GetBottom(), &dummy, &bottom );
5151
5152 // find the row labels within these bounds
5153 //
5154 int row;
5155 for ( row = internalYToRow(top); row < m_numRows; row++ )
5156 {
5157 if ( GetRowBottom(row) < top )
5158 continue;
5159
5160 if ( GetRowTop(row) > bottom )
5161 break;
5162
5163 rowlabels.Add( row );
5164 }
5165
5166 ++iter;
5167 }
5168
5169 return rowlabels;
5170 }
5171
5172 wxArrayInt wxGrid::CalcColLabelsExposed( const wxRegion& reg ) const
5173 {
5174 wxRegionIterator iter( reg );
5175 wxRect r;
5176
5177 wxArrayInt colLabels;
5178
5179 int left, right;
5180 while ( iter )
5181 {
5182 r = iter.GetRect();
5183
5184 // TODO: remove this when we can...
5185 // There is a bug in wxMotif that gives garbage update
5186 // rectangles if you jump-scroll a long way by clicking the
5187 // scrollbar with middle button. This is a work-around
5188 //
5189 #if defined(__WXMOTIF__)
5190 int cw, ch;
5191 m_gridWin->GetClientSize( &cw, &ch );
5192 if ( r.GetLeft() > cw )
5193 r.SetLeft( 0 );
5194 r.SetRight( wxMin( r.GetRight(), cw ) );
5195 #endif
5196
5197 // logical bounds of update region
5198 //
5199 int dummy;
5200 CalcUnscrolledPosition( r.GetLeft(), 0, &left, &dummy );
5201 CalcUnscrolledPosition( r.GetRight(), 0, &right, &dummy );
5202
5203 // find the cells within these bounds
5204 //
5205 int col;
5206 int colPos;
5207 for ( colPos = GetColPos( internalXToCol(left) ); colPos < m_numCols; colPos++ )
5208 {
5209 col = GetColAt( colPos );
5210
5211 if ( GetColRight(col) < left )
5212 continue;
5213
5214 if ( GetColLeft(col) > right )
5215 break;
5216
5217 colLabels.Add( col );
5218 }
5219
5220 ++iter;
5221 }
5222
5223 return colLabels;
5224 }
5225
5226 wxGridCellCoordsArray wxGrid::CalcCellsExposed( const wxRegion& reg ) const
5227 {
5228 wxRegionIterator iter( reg );
5229 wxRect r;
5230
5231 wxGridCellCoordsArray cellsExposed;
5232
5233 int left, top, right, bottom;
5234 while ( iter )
5235 {
5236 r = iter.GetRect();
5237
5238 // TODO: remove this when we can...
5239 // There is a bug in wxMotif that gives garbage update
5240 // rectangles if you jump-scroll a long way by clicking the
5241 // scrollbar with middle button. This is a work-around
5242 //
5243 #if defined(__WXMOTIF__)
5244 int cw, ch;
5245 m_gridWin->GetClientSize( &cw, &ch );
5246 if ( r.GetTop() > ch ) r.SetTop( 0 );
5247 if ( r.GetLeft() > cw ) r.SetLeft( 0 );
5248 r.SetRight( wxMin( r.GetRight(), cw ) );
5249 r.SetBottom( wxMin( r.GetBottom(), ch ) );
5250 #endif
5251
5252 // logical bounds of update region
5253 //
5254 CalcUnscrolledPosition( r.GetLeft(), r.GetTop(), &left, &top );
5255 CalcUnscrolledPosition( r.GetRight(), r.GetBottom(), &right, &bottom );
5256
5257 // find the cells within these bounds
5258 //
5259 int row, col;
5260 for ( row = internalYToRow(top); row < m_numRows; row++ )
5261 {
5262 if ( GetRowBottom(row) <= top )
5263 continue;
5264
5265 if ( GetRowTop(row) > bottom )
5266 break;
5267
5268 int colPos;
5269 for ( colPos = GetColPos( internalXToCol(left) ); colPos < m_numCols; colPos++ )
5270 {
5271 col = GetColAt( colPos );
5272
5273 if ( GetColRight(col) <= left )
5274 continue;
5275
5276 if ( GetColLeft(col) > right )
5277 break;
5278
5279 cellsExposed.Add( wxGridCellCoords( row, col ) );
5280 }
5281 }
5282
5283 ++iter;
5284 }
5285
5286 return cellsExposed;
5287 }
5288
5289
5290 void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent& event )
5291 {
5292 int x, y, row;
5293 wxPoint pos( event.GetPosition() );
5294 CalcUnscrolledPosition( pos.x, pos.y, &x, &y );
5295
5296 if ( event.Dragging() )
5297 {
5298 if (!m_isDragging)
5299 {
5300 m_isDragging = true;
5301 m_rowLabelWin->CaptureMouse();
5302 }
5303
5304 if ( event.LeftIsDown() )
5305 {
5306 switch ( m_cursorMode )
5307 {
5308 case WXGRID_CURSOR_RESIZE_ROW:
5309 {
5310 int cw, ch, left, dummy;
5311 m_gridWin->GetClientSize( &cw, &ch );
5312 CalcUnscrolledPosition( 0, 0, &left, &dummy );
5313
5314 wxClientDC dc( m_gridWin );
5315 PrepareDC( dc );
5316 y = wxMax( y,
5317 GetRowTop(m_dragRowOrCol) +
5318 GetRowMinimalHeight(m_dragRowOrCol) );
5319 dc.SetLogicalFunction(wxINVERT);
5320 if ( m_dragLastPos >= 0 )
5321 {
5322 dc.DrawLine( left, m_dragLastPos, left+cw, m_dragLastPos );
5323 }
5324 dc.DrawLine( left, y, left+cw, y );
5325 m_dragLastPos = y;
5326 }
5327 break;
5328
5329 case WXGRID_CURSOR_SELECT_ROW:
5330 {
5331 if ( (row = YToRow( y )) >= 0 )
5332 {
5333 if ( m_selection )
5334 {
5335 m_selection->SelectRow( row,
5336 event.ControlDown(),
5337 event.ShiftDown(),
5338 event.AltDown(),
5339 event.MetaDown() );
5340 }
5341 }
5342 }
5343 break;
5344
5345 // default label to suppress warnings about "enumeration value
5346 // 'xxx' not handled in switch
5347 default:
5348 break;
5349 }
5350 }
5351 return;
5352 }
5353
5354 if ( m_isDragging && (event.Entering() || event.Leaving()) )
5355 return;
5356
5357 if (m_isDragging)
5358 {
5359 if (m_rowLabelWin->HasCapture())
5360 m_rowLabelWin->ReleaseMouse();
5361 m_isDragging = false;
5362 }
5363
5364 // ------------ Entering or leaving the window
5365 //
5366 if ( event.Entering() || event.Leaving() )
5367 {
5368 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_rowLabelWin);
5369 }
5370
5371 // ------------ Left button pressed
5372 //
5373 else if ( event.LeftDown() )
5374 {
5375 // don't send a label click event for a hit on the
5376 // edge of the row label - this is probably the user
5377 // wanting to resize the row
5378 //
5379 if ( YToEdgeOfRow(y) < 0 )
5380 {
5381 row = YToRow(y);
5382 if ( row >= 0 &&
5383 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK, row, -1, event ) )
5384 {
5385 if ( !event.ShiftDown() && !event.CmdDown() )
5386 ClearSelection();
5387 if ( m_selection )
5388 {
5389 if ( event.ShiftDown() )
5390 {
5391 m_selection->SelectBlock( m_currentCellCoords.GetRow(),
5392 0,
5393 row,
5394 GetNumberCols() - 1,
5395 event.ControlDown(),
5396 event.ShiftDown(),
5397 event.AltDown(),
5398 event.MetaDown() );
5399 }
5400 else
5401 {
5402 m_selection->SelectRow( row,
5403 event.ControlDown(),
5404 event.ShiftDown(),
5405 event.AltDown(),
5406 event.MetaDown() );
5407 }
5408 }
5409
5410 ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW, m_rowLabelWin);
5411 }
5412 }
5413 else
5414 {
5415 // starting to drag-resize a row
5416 if ( CanDragRowSize() )
5417 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW, m_rowLabelWin);
5418 }
5419 }
5420
5421 // ------------ Left double click
5422 //
5423 else if (event.LeftDClick() )
5424 {
5425 row = YToEdgeOfRow(y);
5426 if ( row < 0 )
5427 {
5428 row = YToRow(y);
5429 if ( row >=0 &&
5430 !SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK, row, -1, event ) )
5431 {
5432 // no default action at the moment
5433 }
5434 }
5435 else
5436 {
5437 // adjust row height depending on label text
5438 AutoSizeRowLabelSize( row );
5439
5440 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_colLabelWin);
5441 m_dragLastPos = -1;
5442 }
5443 }
5444
5445 // ------------ Left button released
5446 //
5447 else if ( event.LeftUp() )
5448 {
5449 if ( m_cursorMode == WXGRID_CURSOR_RESIZE_ROW )
5450 {
5451 DoEndDragResizeRow();
5452
5453 // Note: we are ending the event *after* doing
5454 // default processing in this case
5455 //
5456 SendEvent( wxEVT_GRID_ROW_SIZE, m_dragRowOrCol, -1, event );
5457 }
5458
5459 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_rowLabelWin);
5460 m_dragLastPos = -1;
5461 }
5462
5463 // ------------ Right button down
5464 //
5465 else if ( event.RightDown() )
5466 {
5467 row = YToRow(y);
5468 if ( row >=0 &&
5469 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK, row, -1, event ) )
5470 {
5471 // no default action at the moment
5472 }
5473 }
5474
5475 // ------------ Right double click
5476 //
5477 else if ( event.RightDClick() )
5478 {
5479 row = YToRow(y);
5480 if ( row >= 0 &&
5481 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK, row, -1, event ) )
5482 {
5483 // no default action at the moment
5484 }
5485 }
5486
5487 // ------------ No buttons down and mouse moving
5488 //
5489 else if ( event.Moving() )
5490 {
5491 m_dragRowOrCol = YToEdgeOfRow( y );
5492 if ( m_dragRowOrCol >= 0 )
5493 {
5494 if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
5495 {
5496 // don't capture the mouse yet
5497 if ( CanDragRowSize() )
5498 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW, m_rowLabelWin, false);
5499 }
5500 }
5501 else if ( m_cursorMode != WXGRID_CURSOR_SELECT_CELL )
5502 {
5503 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_rowLabelWin, false);
5504 }
5505 }
5506 }
5507
5508 void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent& event )
5509 {
5510 int x, y, col;
5511 wxPoint pos( event.GetPosition() );
5512 CalcUnscrolledPosition( pos.x, pos.y, &x, &y );
5513
5514 if ( event.Dragging() )
5515 {
5516 if (!m_isDragging)
5517 {
5518 m_isDragging = true;
5519 m_colLabelWin->CaptureMouse();
5520
5521 if ( m_cursorMode == WXGRID_CURSOR_MOVE_COL )
5522 m_dragRowOrCol = XToCol( x );
5523 }
5524
5525 if ( event.LeftIsDown() )
5526 {
5527 switch ( m_cursorMode )
5528 {
5529 case WXGRID_CURSOR_RESIZE_COL:
5530 {
5531 int cw, ch, dummy, top;
5532 m_gridWin->GetClientSize( &cw, &ch );
5533 CalcUnscrolledPosition( 0, 0, &dummy, &top );
5534
5535 wxClientDC dc( m_gridWin );
5536 PrepareDC( dc );
5537
5538 x = wxMax( x, GetColLeft(m_dragRowOrCol) +
5539 GetColMinimalWidth(m_dragRowOrCol));
5540 dc.SetLogicalFunction(wxINVERT);
5541 if ( m_dragLastPos >= 0 )
5542 {
5543 dc.DrawLine( m_dragLastPos, top, m_dragLastPos, top + ch );
5544 }
5545 dc.DrawLine( x, top, x, top + ch );
5546 m_dragLastPos = x;
5547 }
5548 break;
5549
5550 case WXGRID_CURSOR_SELECT_COL:
5551 {
5552 if ( (col = XToCol( x )) >= 0 )
5553 {
5554 if ( m_selection )
5555 {
5556 m_selection->SelectCol( col,
5557 event.ControlDown(),
5558 event.ShiftDown(),
5559 event.AltDown(),
5560 event.MetaDown() );
5561 }
5562 }
5563 }
5564 break;
5565
5566 case WXGRID_CURSOR_MOVE_COL:
5567 {
5568 if ( x < 0 )
5569 m_moveToCol = GetColAt( 0 );
5570 else
5571 m_moveToCol = XToCol( x );
5572
5573 int markerX;
5574
5575 if ( m_moveToCol < 0 )
5576 markerX = GetColRight( GetColAt( m_numCols - 1 ) );
5577 else if ( x >= (GetColLeft( m_moveToCol ) + (GetColWidth(m_moveToCol) / 2)) )
5578 {
5579 m_moveToCol = GetColAt( GetColPos( m_moveToCol ) + 1 );
5580 if ( m_moveToCol < 0 )
5581 markerX = GetColRight( GetColAt( m_numCols - 1 ) );
5582 else
5583 markerX = GetColLeft( m_moveToCol );
5584 }
5585 else
5586 markerX = GetColLeft( m_moveToCol );
5587
5588 if ( markerX != m_dragLastPos )
5589 {
5590 wxClientDC dc( m_colLabelWin );
5591 DoPrepareDC(dc);
5592
5593 int cw, ch;
5594 m_colLabelWin->GetClientSize( &cw, &ch );
5595
5596 markerX++;
5597
5598 //Clean up the last indicator
5599 if ( m_dragLastPos >= 0 )
5600 {
5601 wxPen pen( m_colLabelWin->GetBackgroundColour(), 2 );
5602 dc.SetPen(pen);
5603 dc.DrawLine( m_dragLastPos + 1, 0, m_dragLastPos + 1, ch );
5604 dc.SetPen(wxNullPen);
5605
5606 if ( XToCol( m_dragLastPos ) != -1 )
5607 DrawColLabel( dc, XToCol( m_dragLastPos ) );
5608 }
5609
5610 const wxColour *color;
5611 //Moving to the same place? Don't draw a marker
5612 if ( (m_moveToCol == m_dragRowOrCol)
5613 || (GetColPos( m_moveToCol ) == GetColPos( m_dragRowOrCol ) + 1)
5614 || (m_moveToCol < 0 && m_dragRowOrCol == GetColAt( m_numCols - 1 )))
5615 color = wxLIGHT_GREY;
5616 else
5617 color = wxBLUE;
5618
5619 //Draw the marker
5620 wxPen pen( *color, 2 );
5621 dc.SetPen(pen);
5622
5623 dc.DrawLine( markerX, 0, markerX, ch );
5624
5625 dc.SetPen(wxNullPen);
5626
5627 m_dragLastPos = markerX - 1;
5628 }
5629 }
5630 break;
5631
5632 // default label to suppress warnings about "enumeration value
5633 // 'xxx' not handled in switch
5634 default:
5635 break;
5636 }
5637 }
5638 return;
5639 }
5640
5641 if ( m_isDragging && (event.Entering() || event.Leaving()) )
5642 return;
5643
5644 if (m_isDragging)
5645 {
5646 if (m_colLabelWin->HasCapture())
5647 m_colLabelWin->ReleaseMouse();
5648 m_isDragging = false;
5649 }
5650
5651 // ------------ Entering or leaving the window
5652 //
5653 if ( event.Entering() || event.Leaving() )
5654 {
5655 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_colLabelWin);
5656 }
5657
5658 // ------------ Left button pressed
5659 //
5660 else if ( event.LeftDown() )
5661 {
5662 // don't send a label click event for a hit on the
5663 // edge of the col label - this is probably the user
5664 // wanting to resize the col
5665 //
5666 if ( XToEdgeOfCol(x) < 0 )
5667 {
5668 col = XToCol(x);
5669 if ( col >= 0 &&
5670 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK, -1, col, event ) )
5671 {
5672 if ( m_canDragColMove )
5673 {
5674 //Show button as pressed
5675 wxClientDC dc( m_colLabelWin );
5676 int colLeft = GetColLeft( col );
5677 int colRight = GetColRight( col ) - 1;
5678 dc.SetPen( wxPen( m_colLabelWin->GetBackgroundColour(), 1 ) );
5679 dc.DrawLine( colLeft, 1, colLeft, m_colLabelHeight-1 );
5680 dc.DrawLine( colLeft, 1, colRight, 1 );
5681
5682 ChangeCursorMode(WXGRID_CURSOR_MOVE_COL, m_colLabelWin);
5683 }
5684 else
5685 {
5686 if ( !event.ShiftDown() && !event.CmdDown() )
5687 ClearSelection();
5688 if ( m_selection )
5689 {
5690 if ( event.ShiftDown() )
5691 {
5692 m_selection->SelectBlock( 0,
5693 m_currentCellCoords.GetCol(),
5694 GetNumberRows() - 1, col,
5695 event.ControlDown(),
5696 event.ShiftDown(),
5697 event.AltDown(),
5698 event.MetaDown() );
5699 }
5700 else
5701 {
5702 m_selection->SelectCol( col,
5703 event.ControlDown(),
5704 event.ShiftDown(),
5705 event.AltDown(),
5706 event.MetaDown() );
5707 }
5708 }
5709
5710 ChangeCursorMode(WXGRID_CURSOR_SELECT_COL, m_colLabelWin);
5711 }
5712 }
5713 }
5714 else
5715 {
5716 // starting to drag-resize a col
5717 //
5718 if ( CanDragColSize() )
5719 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL, m_colLabelWin);
5720 }
5721 }
5722
5723 // ------------ Left double click
5724 //
5725 if ( event.LeftDClick() )
5726 {
5727 col = XToEdgeOfCol(x);
5728 if ( col < 0 )
5729 {
5730 col = XToCol(x);
5731 if ( col >= 0 &&
5732 ! SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK, -1, col, event ) )
5733 {
5734 // no default action at the moment
5735 }
5736 }
5737 else
5738 {
5739 // adjust column width depending on label text
5740 AutoSizeColLabelSize( col );
5741
5742 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_colLabelWin);
5743 m_dragLastPos = -1;
5744 }
5745 }
5746
5747 // ------------ Left button released
5748 //
5749 else if ( event.LeftUp() )
5750 {
5751 switch ( m_cursorMode )
5752 {
5753 case WXGRID_CURSOR_RESIZE_COL:
5754 DoEndDragResizeCol();
5755
5756 // Note: we are ending the event *after* doing
5757 // default processing in this case
5758 //
5759 SendEvent( wxEVT_GRID_COL_SIZE, -1, m_dragRowOrCol, event );
5760 break;
5761
5762 case WXGRID_CURSOR_MOVE_COL:
5763 DoEndDragMoveCol();
5764
5765 SendEvent( wxEVT_GRID_COL_MOVE, -1, m_dragRowOrCol, event );
5766 break;
5767
5768 case WXGRID_CURSOR_SELECT_COL:
5769 case WXGRID_CURSOR_SELECT_CELL:
5770 case WXGRID_CURSOR_RESIZE_ROW:
5771 case WXGRID_CURSOR_SELECT_ROW:
5772 // nothing to do (?)
5773 break;
5774 }
5775
5776 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_colLabelWin);
5777 m_dragLastPos = -1;
5778 }
5779
5780 // ------------ Right button down
5781 //
5782 else if ( event.RightDown() )
5783 {
5784 col = XToCol(x);
5785 if ( col >= 0 &&
5786 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK, -1, col, event ) )
5787 {
5788 // no default action at the moment
5789 }
5790 }
5791
5792 // ------------ Right double click
5793 //
5794 else if ( event.RightDClick() )
5795 {
5796 col = XToCol(x);
5797 if ( col >= 0 &&
5798 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK, -1, col, event ) )
5799 {
5800 // no default action at the moment
5801 }
5802 }
5803
5804 // ------------ No buttons down and mouse moving
5805 //
5806 else if ( event.Moving() )
5807 {
5808 m_dragRowOrCol = XToEdgeOfCol( x );
5809 if ( m_dragRowOrCol >= 0 )
5810 {
5811 if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
5812 {
5813 // don't capture the cursor yet
5814 if ( CanDragColSize() )
5815 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL, m_colLabelWin, false);
5816 }
5817 }
5818 else if ( m_cursorMode != WXGRID_CURSOR_SELECT_CELL )
5819 {
5820 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_colLabelWin, false);
5821 }
5822 }
5823 }
5824
5825 void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent& event )
5826 {
5827 if ( event.LeftDown() )
5828 {
5829 // indicate corner label by having both row and
5830 // col args == -1
5831 //
5832 if ( !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK, -1, -1, event ) )
5833 {
5834 SelectAll();
5835 }
5836 }
5837 else if ( event.LeftDClick() )
5838 {
5839 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK, -1, -1, event );
5840 }
5841 else if ( event.RightDown() )
5842 {
5843 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK, -1, -1, event ) )
5844 {
5845 // no default action at the moment
5846 }
5847 }
5848 else if ( event.RightDClick() )
5849 {
5850 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK, -1, -1, event ) )
5851 {
5852 // no default action at the moment
5853 }
5854 }
5855 }
5856
5857 void wxGrid::CancelMouseCapture()
5858 {
5859 // cancel operation currently in progress, whatever it is
5860 if ( m_winCapture )
5861 {
5862 m_isDragging = false;
5863 m_cursorMode = WXGRID_CURSOR_SELECT_CELL;
5864 m_winCapture->SetCursor( *wxSTANDARD_CURSOR );
5865 m_winCapture = NULL;
5866
5867 // remove traces of whatever we drew on screen
5868 Refresh();
5869 }
5870 }
5871
5872 void wxGrid::ChangeCursorMode(CursorMode mode,
5873 wxWindow *win,
5874 bool captureMouse)
5875 {
5876 #ifdef __WXDEBUG__
5877 static const wxChar *cursorModes[] =
5878 {
5879 _T("SELECT_CELL"),
5880 _T("RESIZE_ROW"),
5881 _T("RESIZE_COL"),
5882 _T("SELECT_ROW"),
5883 _T("SELECT_COL"),
5884 _T("MOVE_COL"),
5885 };
5886
5887 wxLogTrace(_T("grid"),
5888 _T("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
5889 win == m_colLabelWin ? _T("colLabelWin")
5890 : win ? _T("rowLabelWin")
5891 : _T("gridWin"),
5892 cursorModes[m_cursorMode], cursorModes[mode]);
5893 #endif
5894
5895 if ( mode == m_cursorMode &&
5896 win == m_winCapture &&
5897 captureMouse == (m_winCapture != NULL))
5898 return;
5899
5900 if ( !win )
5901 {
5902 // by default use the grid itself
5903 win = m_gridWin;
5904 }
5905
5906 if ( m_winCapture )
5907 {
5908 if (m_winCapture->HasCapture())
5909 m_winCapture->ReleaseMouse();
5910 m_winCapture = (wxWindow *)NULL;
5911 }
5912
5913 m_cursorMode = mode;
5914
5915 switch ( m_cursorMode )
5916 {
5917 case WXGRID_CURSOR_RESIZE_ROW:
5918 win->SetCursor( m_rowResizeCursor );
5919 break;
5920
5921 case WXGRID_CURSOR_RESIZE_COL:
5922 win->SetCursor( m_colResizeCursor );
5923 break;
5924
5925 case WXGRID_CURSOR_MOVE_COL:
5926 win->SetCursor( wxCursor(wxCURSOR_HAND) );
5927 break;
5928
5929 default:
5930 win->SetCursor( *wxSTANDARD_CURSOR );
5931 break;
5932 }
5933
5934 // we need to capture mouse when resizing
5935 bool resize = m_cursorMode == WXGRID_CURSOR_RESIZE_ROW ||
5936 m_cursorMode == WXGRID_CURSOR_RESIZE_COL;
5937
5938 if ( captureMouse && resize )
5939 {
5940 win->CaptureMouse();
5941 m_winCapture = win;
5942 }
5943 }
5944
5945 void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent& event )
5946 {
5947 int x, y;
5948 wxPoint pos( event.GetPosition() );
5949 CalcUnscrolledPosition( pos.x, pos.y, &x, &y );
5950
5951 wxGridCellCoords coords;
5952 XYToCell( x, y, coords );
5953
5954 int cell_rows, cell_cols;
5955 bool isFirstDrag = !m_isDragging;
5956 GetCellSize( coords.GetRow(), coords.GetCol(), &cell_rows, &cell_cols );
5957 if ((cell_rows < 0) || (cell_cols < 0))
5958 {
5959 coords.SetRow(coords.GetRow() + cell_rows);
5960 coords.SetCol(coords.GetCol() + cell_cols);
5961 }
5962
5963 if ( event.Dragging() )
5964 {
5965 //wxLogDebug("pos(%d, %d) coords(%d, %d)", pos.x, pos.y, coords.GetRow(), coords.GetCol());
5966
5967 // Don't start doing anything until the mouse has been dragged at
5968 // least 3 pixels in any direction...
5969 if (! m_isDragging)
5970 {
5971 if (m_startDragPos == wxDefaultPosition)
5972 {
5973 m_startDragPos = pos;
5974 return;
5975 }
5976 if (abs(m_startDragPos.x - pos.x) < 4 && abs(m_startDragPos.y - pos.y) < 4)
5977 return;
5978 }
5979
5980 m_isDragging = true;
5981 if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
5982 {
5983 // Hide the edit control, so it
5984 // won't interfere with drag-shrinking.
5985 if ( IsCellEditControlShown() )
5986 {
5987 HideCellEditControl();
5988 SaveEditControlValue();
5989 }
5990
5991 if ( coords != wxGridNoCellCoords )
5992 {
5993 if ( event.CmdDown() )
5994 {
5995 if ( m_selectingKeyboard == wxGridNoCellCoords)
5996 m_selectingKeyboard = coords;
5997 HighlightBlock( m_selectingKeyboard, coords );
5998 }
5999 else if ( CanDragCell() )
6000 {
6001 if ( isFirstDrag )
6002 {
6003 if ( m_selectingKeyboard == wxGridNoCellCoords)
6004 m_selectingKeyboard = coords;
6005
6006 SendEvent( wxEVT_GRID_CELL_BEGIN_DRAG,
6007 coords.GetRow(),
6008 coords.GetCol(),
6009 event );
6010 return;
6011 }
6012 }
6013 else
6014 {
6015 if ( !IsSelection() )
6016 {
6017 HighlightBlock( coords, coords );
6018 }
6019 else
6020 {
6021 HighlightBlock( m_currentCellCoords, coords );
6022 }
6023 }
6024
6025 if (! IsVisible(coords))
6026 {
6027 MakeCellVisible(coords);
6028 // TODO: need to introduce a delay or something here. The
6029 // scrolling is way to fast, at least on MSW - also on GTK.
6030 }
6031 }
6032 // Have we captured the mouse yet?
6033 if (! m_winCapture)
6034 {
6035 m_winCapture = m_gridWin;
6036 m_winCapture->CaptureMouse();
6037 }
6038
6039
6040 }
6041 else if ( event.LeftIsDown() &&
6042 m_cursorMode == WXGRID_CURSOR_RESIZE_ROW )
6043 {
6044 int cw, ch, left, dummy;
6045 m_gridWin->GetClientSize( &cw, &ch );
6046 CalcUnscrolledPosition( 0, 0, &left, &dummy );
6047
6048 wxClientDC dc( m_gridWin );
6049 PrepareDC( dc );
6050 y = wxMax( y, GetRowTop(m_dragRowOrCol) +
6051 GetRowMinimalHeight(m_dragRowOrCol) );
6052 dc.SetLogicalFunction(wxINVERT);
6053 if ( m_dragLastPos >= 0 )
6054 {
6055 dc.DrawLine( left, m_dragLastPos, left+cw, m_dragLastPos );
6056 }
6057 dc.DrawLine( left, y, left+cw, y );
6058 m_dragLastPos = y;
6059 }
6060 else if ( event.LeftIsDown() &&
6061 m_cursorMode == WXGRID_CURSOR_RESIZE_COL )
6062 {
6063 int cw, ch, dummy, top;
6064 m_gridWin->GetClientSize( &cw, &ch );
6065 CalcUnscrolledPosition( 0, 0, &dummy, &top );
6066
6067 wxClientDC dc( m_gridWin );
6068 PrepareDC( dc );
6069 x = wxMax( x, GetColLeft(m_dragRowOrCol) +
6070 GetColMinimalWidth(m_dragRowOrCol) );
6071 dc.SetLogicalFunction(wxINVERT);
6072 if ( m_dragLastPos >= 0 )
6073 {
6074 dc.DrawLine( m_dragLastPos, top, m_dragLastPos, top + ch );
6075 }
6076 dc.DrawLine( x, top, x, top + ch );
6077 m_dragLastPos = x;
6078 }
6079
6080 return;
6081 }
6082
6083 m_isDragging = false;
6084 m_startDragPos = wxDefaultPosition;
6085
6086 // VZ: if we do this, the mode is reset to WXGRID_CURSOR_SELECT_CELL
6087 // immediately after it becomes WXGRID_CURSOR_RESIZE_ROW/COL under
6088 // wxGTK
6089 #if 0
6090 if ( event.Entering() || event.Leaving() )
6091 {
6092 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL);
6093 m_gridWin->SetCursor( *wxSTANDARD_CURSOR );
6094 }
6095 else
6096 #endif // 0
6097
6098 // ------------ Left button pressed
6099 //
6100 if ( event.LeftDown() && coords != wxGridNoCellCoords )
6101 {
6102 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_CLICK,
6103 coords.GetRow(),
6104 coords.GetCol(),
6105 event ) )
6106 {
6107 if ( !event.CmdDown() )
6108 ClearSelection();
6109 if ( event.ShiftDown() )
6110 {
6111 if ( m_selection )
6112 {
6113 m_selection->SelectBlock( m_currentCellCoords.GetRow(),
6114 m_currentCellCoords.GetCol(),
6115 coords.GetRow(),
6116 coords.GetCol(),
6117 event.ControlDown(),
6118 event.ShiftDown(),
6119 event.AltDown(),
6120 event.MetaDown() );
6121 }
6122 }
6123 else if ( XToEdgeOfCol(x) < 0 &&
6124 YToEdgeOfRow(y) < 0 )
6125 {
6126 DisableCellEditControl();
6127 MakeCellVisible( coords );
6128
6129 if ( event.CmdDown() )
6130 {
6131 if ( m_selection )
6132 {
6133 m_selection->ToggleCellSelection( coords.GetRow(),
6134 coords.GetCol(),
6135 event.ControlDown(),
6136 event.ShiftDown(),
6137 event.AltDown(),
6138 event.MetaDown() );
6139 }
6140 m_selectingTopLeft = wxGridNoCellCoords;
6141 m_selectingBottomRight = wxGridNoCellCoords;
6142 m_selectingKeyboard = coords;
6143 }
6144 else
6145 {
6146 m_waitForSlowClick = m_currentCellCoords == coords && coords != wxGridNoCellCoords;
6147 SetCurrentCell( coords );
6148 if ( m_selection )
6149 {
6150 if ( m_selection->GetSelectionMode() !=
6151 wxGrid::wxGridSelectCells )
6152 {
6153 HighlightBlock( coords, coords );
6154 }
6155 }
6156 }
6157 }
6158 }
6159 }
6160
6161 // ------------ Left double click
6162 //
6163 else if ( event.LeftDClick() && coords != wxGridNoCellCoords )
6164 {
6165 DisableCellEditControl();
6166
6167 if ( XToEdgeOfCol(x) < 0 && YToEdgeOfRow(y) < 0 )
6168 {
6169 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_DCLICK,
6170 coords.GetRow(),
6171 coords.GetCol(),
6172 event ) )
6173 {
6174 // we want double click to select a cell and start editing
6175 // (i.e. to behave in same way as sequence of two slow clicks):
6176 m_waitForSlowClick = true;
6177 }
6178 }
6179 }
6180
6181 // ------------ Left button released
6182 //
6183 else if ( event.LeftUp() )
6184 {
6185 if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
6186 {
6187 if (m_winCapture)
6188 {
6189 if (m_winCapture->HasCapture())
6190 m_winCapture->ReleaseMouse();
6191 m_winCapture = NULL;
6192 }
6193
6194 if ( coords == m_currentCellCoords && m_waitForSlowClick && CanEnableCellControl() )
6195 {
6196 ClearSelection();
6197 EnableCellEditControl();
6198
6199 wxGridCellAttr *attr = GetCellAttr(coords);
6200 wxGridCellEditor *editor = attr->GetEditor(this, coords.GetRow(), coords.GetCol());
6201 editor->StartingClick();
6202 editor->DecRef();
6203 attr->DecRef();
6204
6205 m_waitForSlowClick = false;
6206 }
6207 else if ( m_selectingTopLeft != wxGridNoCellCoords &&
6208 m_selectingBottomRight != wxGridNoCellCoords )
6209 {
6210 if ( m_selection )
6211 {
6212 m_selection->SelectBlock( m_selectingTopLeft.GetRow(),
6213 m_selectingTopLeft.GetCol(),
6214 m_selectingBottomRight.GetRow(),
6215 m_selectingBottomRight.GetCol(),
6216 event.ControlDown(),
6217 event.ShiftDown(),
6218 event.AltDown(),
6219 event.MetaDown() );
6220 }
6221
6222 m_selectingTopLeft = wxGridNoCellCoords;
6223 m_selectingBottomRight = wxGridNoCellCoords;
6224
6225 // Show the edit control, if it has been hidden for
6226 // drag-shrinking.
6227 ShowCellEditControl();
6228 }
6229 }
6230 else if ( m_cursorMode == WXGRID_CURSOR_RESIZE_ROW )
6231 {
6232 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL);
6233 DoEndDragResizeRow();
6234
6235 // Note: we are ending the event *after* doing
6236 // default processing in this case
6237 //
6238 SendEvent( wxEVT_GRID_ROW_SIZE, m_dragRowOrCol, -1, event );
6239 }
6240 else if ( m_cursorMode == WXGRID_CURSOR_RESIZE_COL )
6241 {
6242 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL);
6243 DoEndDragResizeCol();
6244
6245 // Note: we are ending the event *after* doing
6246 // default processing in this case
6247 //
6248 SendEvent( wxEVT_GRID_COL_SIZE, -1, m_dragRowOrCol, event );
6249 }
6250
6251 m_dragLastPos = -1;
6252 }
6253
6254 // ------------ Right button down
6255 //
6256 else if ( event.RightDown() && coords != wxGridNoCellCoords )
6257 {
6258 DisableCellEditControl();
6259 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_CLICK,
6260 coords.GetRow(),
6261 coords.GetCol(),
6262 event ) )
6263 {
6264 // no default action at the moment
6265 }
6266 }
6267
6268 // ------------ Right double click
6269 //
6270 else if ( event.RightDClick() && coords != wxGridNoCellCoords )
6271 {
6272 DisableCellEditControl();
6273 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_DCLICK,
6274 coords.GetRow(),
6275 coords.GetCol(),
6276 event ) )
6277 {
6278 // no default action at the moment
6279 }
6280 }
6281
6282 // ------------ Moving and no button action
6283 //
6284 else if ( event.Moving() && !event.IsButton() )
6285 {
6286 if ( coords.GetRow() < 0 || coords.GetCol() < 0 )
6287 {
6288 // out of grid cell area
6289 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL);
6290 return;
6291 }
6292
6293 int dragRow = YToEdgeOfRow( y );
6294 int dragCol = XToEdgeOfCol( x );
6295
6296 // Dragging on the corner of a cell to resize in both
6297 // directions is not implemented yet...
6298 //
6299 if ( dragRow >= 0 && dragCol >= 0 )
6300 {
6301 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL);
6302 return;
6303 }
6304
6305 if ( dragRow >= 0 )
6306 {
6307 m_dragRowOrCol = dragRow;
6308
6309 if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
6310 {
6311 if ( CanDragRowSize() && CanDragGridSize() )
6312 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW, NULL, false);
6313 }
6314 }
6315 else if ( dragCol >= 0 )
6316 {
6317 m_dragRowOrCol = dragCol;
6318
6319 if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
6320 {
6321 if ( CanDragColSize() && CanDragGridSize() )
6322 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL, NULL, false);
6323 }
6324 }
6325 else // Neither on a row or col edge
6326 {
6327 if ( m_cursorMode != WXGRID_CURSOR_SELECT_CELL )
6328 {
6329 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL);
6330 }
6331 }
6332 }
6333 }
6334
6335 void wxGrid::DoEndDragResizeRow()
6336 {
6337 if ( m_dragLastPos >= 0 )
6338 {
6339 // erase the last line and resize the row
6340 //
6341 int cw, ch, left, dummy;
6342 m_gridWin->GetClientSize( &cw, &ch );
6343 CalcUnscrolledPosition( 0, 0, &left, &dummy );
6344
6345 wxClientDC dc( m_gridWin );
6346 PrepareDC( dc );
6347 dc.SetLogicalFunction( wxINVERT );
6348 dc.DrawLine( left, m_dragLastPos, left + cw, m_dragLastPos );
6349 HideCellEditControl();
6350 SaveEditControlValue();
6351
6352 int rowTop = GetRowTop(m_dragRowOrCol);
6353 SetRowSize( m_dragRowOrCol,
6354 wxMax( m_dragLastPos - rowTop, m_minAcceptableRowHeight ) );
6355
6356 if ( !GetBatchCount() )
6357 {
6358 // Only needed to get the correct rect.y:
6359 wxRect rect ( CellToRect( m_dragRowOrCol, 0 ) );
6360 rect.x = 0;
6361 CalcScrolledPosition(0, rect.y, &dummy, &rect.y);
6362 rect.width = m_rowLabelWidth;
6363 rect.height = ch - rect.y;
6364 m_rowLabelWin->Refresh( true, &rect );
6365 rect.width = cw;
6366
6367 // if there is a multicell block, paint all of it
6368 if (m_table)
6369 {
6370 int i, cell_rows, cell_cols, subtract_rows = 0;
6371 int leftCol = XToCol(left);
6372 int rightCol = internalXToCol(left + cw);
6373 if (leftCol >= 0)
6374 {
6375 for (i=leftCol; i<rightCol; i++)
6376 {
6377 GetCellSize(m_dragRowOrCol, i, &cell_rows, &cell_cols);
6378 if (cell_rows < subtract_rows)
6379 subtract_rows = cell_rows;
6380 }
6381 rect.y = GetRowTop(m_dragRowOrCol + subtract_rows);
6382 CalcScrolledPosition(0, rect.y, &dummy, &rect.y);
6383 rect.height = ch - rect.y;
6384 }
6385 }
6386 m_gridWin->Refresh( false, &rect );
6387 }
6388
6389 ShowCellEditControl();
6390 }
6391 }
6392
6393
6394 void wxGrid::DoEndDragResizeCol()
6395 {
6396 if ( m_dragLastPos >= 0 )
6397 {
6398 // erase the last line and resize the col
6399 //
6400 int cw, ch, dummy, top;
6401 m_gridWin->GetClientSize( &cw, &ch );
6402 CalcUnscrolledPosition( 0, 0, &dummy, &top );
6403
6404 wxClientDC dc( m_gridWin );
6405 PrepareDC( dc );
6406 dc.SetLogicalFunction( wxINVERT );
6407 dc.DrawLine( m_dragLastPos, top, m_dragLastPos, top + ch );
6408 HideCellEditControl();
6409 SaveEditControlValue();
6410
6411 int colLeft = GetColLeft(m_dragRowOrCol);
6412 SetColSize( m_dragRowOrCol,
6413 wxMax( m_dragLastPos - colLeft,
6414 GetColMinimalWidth(m_dragRowOrCol) ) );
6415
6416 if ( !GetBatchCount() )
6417 {
6418 // Only needed to get the correct rect.x:
6419 wxRect rect ( CellToRect( 0, m_dragRowOrCol ) );
6420 rect.y = 0;
6421 CalcScrolledPosition(rect.x, 0, &rect.x, &dummy);
6422 rect.width = cw - rect.x;
6423 rect.height = m_colLabelHeight;
6424 m_colLabelWin->Refresh( true, &rect );
6425 rect.height = ch;
6426
6427 // if there is a multicell block, paint all of it
6428 if (m_table)
6429 {
6430 int i, cell_rows, cell_cols, subtract_cols = 0;
6431 int topRow = YToRow(top);
6432 int bottomRow = internalYToRow(top + cw);
6433 if (topRow >= 0)
6434 {
6435 for (i=topRow; i<bottomRow; i++)
6436 {
6437 GetCellSize(i, m_dragRowOrCol, &cell_rows, &cell_cols);
6438 if (cell_cols < subtract_cols)
6439 subtract_cols = cell_cols;
6440 }
6441
6442 rect.x = GetColLeft(m_dragRowOrCol + subtract_cols);
6443 CalcScrolledPosition(rect.x, 0, &rect.x, &dummy);
6444 rect.width = cw - rect.x;
6445 }
6446 }
6447
6448 m_gridWin->Refresh( false, &rect );
6449 }
6450
6451 ShowCellEditControl();
6452 }
6453 }
6454
6455 void wxGrid::DoEndDragMoveCol()
6456 {
6457 //The user clicked on the column but didn't actually drag
6458 if ( m_dragLastPos < 0 )
6459 {
6460 m_colLabelWin->Refresh(); //Do this to "unpress" the column
6461 return;
6462 }
6463
6464 int newPos;
6465 if ( m_moveToCol == -1 )
6466 newPos = m_numCols - 1;
6467 else
6468 {
6469 newPos = GetColPos( m_moveToCol );
6470 if ( newPos > GetColPos( m_dragRowOrCol ) )
6471 newPos--;
6472 }
6473
6474 SetColPos( m_dragRowOrCol, newPos );
6475 }
6476
6477 void wxGrid::SetColPos( int colID, int newPos )
6478 {
6479 if ( m_colAt.IsEmpty() )
6480 {
6481 m_colAt.Alloc( m_numCols );
6482
6483 int i;
6484 for ( i = 0; i < m_numCols; i++ )
6485 {
6486 m_colAt.Add( i );
6487 }
6488 }
6489
6490 int oldPos = GetColPos( colID );
6491
6492 //Reshuffle the m_colAt array
6493 if ( newPos > oldPos )
6494 {
6495 int i;
6496 for ( i = oldPos; i < newPos; i++ )
6497 {
6498 m_colAt[i] = m_colAt[i+1];
6499 }
6500 }
6501 else
6502 {
6503 int i;
6504 for ( i = oldPos; i > newPos; i-- )
6505 {
6506 m_colAt[i] = m_colAt[i-1];
6507 }
6508 }
6509
6510 m_colAt[newPos] = colID;
6511
6512 //Recalculate the column rights
6513 if ( !m_colWidths.IsEmpty() )
6514 {
6515 int colRight = 0;
6516 int colPos;
6517 for ( colPos = 0; colPos < m_numCols; colPos++ )
6518 {
6519 int colID = GetColAt( colPos );
6520
6521 colRight += m_colWidths[colID];
6522 m_colRights[colID] = colRight;
6523 }
6524 }
6525
6526 m_colLabelWin->Refresh();
6527 m_gridWin->Refresh();
6528 }
6529
6530
6531
6532 void wxGrid::EnableDragColMove( bool enable )
6533 {
6534 if ( m_canDragColMove == enable )
6535 return;
6536
6537 m_canDragColMove = enable;
6538
6539 if ( !m_canDragColMove )
6540 {
6541 m_colAt.Clear();
6542
6543 //Recalculate the column rights
6544 if ( !m_colWidths.IsEmpty() )
6545 {
6546 int colRight = 0;
6547 int colPos;
6548 for ( colPos = 0; colPos < m_numCols; colPos++ )
6549 {
6550 colRight += m_colWidths[colPos];
6551 m_colRights[colPos] = colRight;
6552 }
6553 }
6554
6555 m_colLabelWin->Refresh();
6556 m_gridWin->Refresh();
6557 }
6558 }
6559
6560
6561 //
6562 // ------ interaction with data model
6563 //
6564 bool wxGrid::ProcessTableMessage( wxGridTableMessage& msg )
6565 {
6566 switch ( msg.GetId() )
6567 {
6568 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES:
6569 return GetModelValues();
6570
6571 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES:
6572 return SetModelValues();
6573
6574 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED:
6575 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED:
6576 case wxGRIDTABLE_NOTIFY_ROWS_DELETED:
6577 case wxGRIDTABLE_NOTIFY_COLS_INSERTED:
6578 case wxGRIDTABLE_NOTIFY_COLS_APPENDED:
6579 case wxGRIDTABLE_NOTIFY_COLS_DELETED:
6580 return Redimension( msg );
6581
6582 default:
6583 return false;
6584 }
6585 }
6586
6587 // The behaviour of this function depends on the grid table class
6588 // Clear() function. For the default wxGridStringTable class the
6589 // behavious is to replace all cell contents with wxEmptyString but
6590 // not to change the number of rows or cols.
6591 //
6592 void wxGrid::ClearGrid()
6593 {
6594 if ( m_table )
6595 {
6596 if (IsCellEditControlEnabled())
6597 DisableCellEditControl();
6598
6599 m_table->Clear();
6600 if (!GetBatchCount())
6601 m_gridWin->Refresh();
6602 }
6603 }
6604
6605 bool wxGrid::InsertRows( int pos, int numRows, bool WXUNUSED(updateLabels) )
6606 {
6607 // TODO: something with updateLabels flag
6608
6609 if ( !m_created )
6610 {
6611 wxFAIL_MSG( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") );
6612 return false;
6613 }
6614
6615 if ( m_table )
6616 {
6617 if (IsCellEditControlEnabled())
6618 DisableCellEditControl();
6619
6620 bool done = m_table->InsertRows( pos, numRows );
6621 return done;
6622
6623 // the table will have sent the results of the insert row
6624 // operation to this view object as a grid table message
6625 }
6626
6627 return false;
6628 }
6629
6630 bool wxGrid::AppendRows( int numRows, bool WXUNUSED(updateLabels) )
6631 {
6632 // TODO: something with updateLabels flag
6633
6634 if ( !m_created )
6635 {
6636 wxFAIL_MSG( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") );
6637 return false;
6638 }
6639
6640 if ( m_table )
6641 {
6642 bool done = m_table && m_table->AppendRows( numRows );
6643 return done;
6644
6645 // the table will have sent the results of the append row
6646 // operation to this view object as a grid table message
6647 }
6648
6649 return false;
6650 }
6651
6652 bool wxGrid::DeleteRows( int pos, int numRows, bool WXUNUSED(updateLabels) )
6653 {
6654 // TODO: something with updateLabels flag
6655
6656 if ( !m_created )
6657 {
6658 wxFAIL_MSG( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") );
6659 return false;
6660 }
6661
6662 if ( m_table )
6663 {
6664 if (IsCellEditControlEnabled())
6665 DisableCellEditControl();
6666
6667 bool done = m_table->DeleteRows( pos, numRows );
6668 return done;
6669 // the table will have sent the results of the delete row
6670 // operation to this view object as a grid table message
6671 }
6672
6673 return false;
6674 }
6675
6676 bool wxGrid::InsertCols( int pos, int numCols, bool WXUNUSED(updateLabels) )
6677 {
6678 // TODO: something with updateLabels flag
6679
6680 if ( !m_created )
6681 {
6682 wxFAIL_MSG( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") );
6683 return false;
6684 }
6685
6686 if ( m_table )
6687 {
6688 if (IsCellEditControlEnabled())
6689 DisableCellEditControl();
6690
6691 bool done = m_table->InsertCols( pos, numCols );
6692 return done;
6693 // the table will have sent the results of the insert col
6694 // operation to this view object as a grid table message
6695 }
6696
6697 return false;
6698 }
6699
6700 bool wxGrid::AppendCols( int numCols, bool WXUNUSED(updateLabels) )
6701 {
6702 // TODO: something with updateLabels flag
6703
6704 if ( !m_created )
6705 {
6706 wxFAIL_MSG( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") );
6707 return false;
6708 }
6709
6710 if ( m_table )
6711 {
6712 bool done = m_table->AppendCols( numCols );
6713 return done;
6714 // the table will have sent the results of the append col
6715 // operation to this view object as a grid table message
6716 }
6717
6718 return false;
6719 }
6720
6721 bool wxGrid::DeleteCols( int pos, int numCols, bool WXUNUSED(updateLabels) )
6722 {
6723 // TODO: something with updateLabels flag
6724
6725 if ( !m_created )
6726 {
6727 wxFAIL_MSG( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") );
6728 return false;
6729 }
6730
6731 if ( m_table )
6732 {
6733 if (IsCellEditControlEnabled())
6734 DisableCellEditControl();
6735
6736 bool done = m_table->DeleteCols( pos, numCols );
6737 return done;
6738 // the table will have sent the results of the delete col
6739 // operation to this view object as a grid table message
6740 }
6741
6742 return false;
6743 }
6744
6745 //
6746 // ----- event handlers
6747 //
6748
6749 // Generate a grid event based on a mouse event and
6750 // return the result of ProcessEvent()
6751 //
6752 int wxGrid::SendEvent( const wxEventType type,
6753 int row, int col,
6754 wxMouseEvent& mouseEv )
6755 {
6756 bool claimed, vetoed;
6757
6758 if ( type == wxEVT_GRID_ROW_SIZE || type == wxEVT_GRID_COL_SIZE )
6759 {
6760 int rowOrCol = (row == -1 ? col : row);
6761
6762 wxGridSizeEvent gridEvt( GetId(),
6763 type,
6764 this,
6765 rowOrCol,
6766 mouseEv.GetX() + GetRowLabelSize(),
6767 mouseEv.GetY() + GetColLabelSize(),
6768 mouseEv.ControlDown(),
6769 mouseEv.ShiftDown(),
6770 mouseEv.AltDown(),
6771 mouseEv.MetaDown() );
6772
6773 claimed = GetEventHandler()->ProcessEvent(gridEvt);
6774 vetoed = !gridEvt.IsAllowed();
6775 }
6776 else if ( type == wxEVT_GRID_RANGE_SELECT )
6777 {
6778 // Right now, it should _never_ end up here!
6779 wxGridRangeSelectEvent gridEvt( GetId(),
6780 type,
6781 this,
6782 m_selectingTopLeft,
6783 m_selectingBottomRight,
6784 true,
6785 mouseEv.ControlDown(),
6786 mouseEv.ShiftDown(),
6787 mouseEv.AltDown(),
6788 mouseEv.MetaDown() );
6789
6790 claimed = GetEventHandler()->ProcessEvent(gridEvt);
6791 vetoed = !gridEvt.IsAllowed();
6792 }
6793 else if ( type == wxEVT_GRID_LABEL_LEFT_CLICK ||
6794 type == wxEVT_GRID_LABEL_LEFT_DCLICK ||
6795 type == wxEVT_GRID_LABEL_RIGHT_CLICK ||
6796 type == wxEVT_GRID_LABEL_RIGHT_DCLICK )
6797 {
6798 wxPoint pos = mouseEv.GetPosition();
6799
6800 if ( mouseEv.GetEventObject() == GetGridRowLabelWindow() )
6801 pos.y += GetColLabelSize();
6802 if ( mouseEv.GetEventObject() == GetGridColLabelWindow() )
6803 pos.x += GetRowLabelSize();
6804
6805 wxGridEvent gridEvt( GetId(),
6806 type,
6807 this,
6808 row, col,
6809 pos.x,
6810 pos.y,
6811 false,
6812 mouseEv.ControlDown(),
6813 mouseEv.ShiftDown(),
6814 mouseEv.AltDown(),
6815 mouseEv.MetaDown() );
6816 claimed = GetEventHandler()->ProcessEvent(gridEvt);
6817 vetoed = !gridEvt.IsAllowed();
6818 }
6819 else
6820 {
6821 wxGridEvent gridEvt( GetId(),
6822 type,
6823 this,
6824 row, col,
6825 mouseEv.GetX() + GetRowLabelSize(),
6826 mouseEv.GetY() + GetColLabelSize(),
6827 false,
6828 mouseEv.ControlDown(),
6829 mouseEv.ShiftDown(),
6830 mouseEv.AltDown(),
6831 mouseEv.MetaDown() );
6832 claimed = GetEventHandler()->ProcessEvent(gridEvt);
6833 vetoed = !gridEvt.IsAllowed();
6834 }
6835
6836 // A Veto'd event may not be `claimed' so test this first
6837 if (vetoed)
6838 return -1;
6839
6840 return claimed ? 1 : 0;
6841 }
6842
6843 // Generate a grid event of specified type and return the result
6844 // of ProcessEvent().
6845 //
6846 int wxGrid::SendEvent( const wxEventType type,
6847 int row, int col )
6848 {
6849 bool claimed, vetoed;
6850
6851 if ( type == wxEVT_GRID_ROW_SIZE || type == wxEVT_GRID_COL_SIZE )
6852 {
6853 int rowOrCol = (row == -1 ? col : row);
6854
6855 wxGridSizeEvent gridEvt( GetId(), type, this, rowOrCol );
6856
6857 claimed = GetEventHandler()->ProcessEvent(gridEvt);
6858 vetoed = !gridEvt.IsAllowed();
6859 }
6860 else
6861 {
6862 wxGridEvent gridEvt( GetId(), type, this, row, col );
6863
6864 claimed = GetEventHandler()->ProcessEvent(gridEvt);
6865 vetoed = !gridEvt.IsAllowed();
6866 }
6867
6868 // A Veto'd event may not be `claimed' so test this first
6869 if (vetoed)
6870 return -1;
6871
6872 return claimed ? 1 : 0;
6873 }
6874
6875 void wxGrid::OnPaint( wxPaintEvent& WXUNUSED(event) )
6876 {
6877 // needed to prevent zillions of paint events on MSW
6878 wxPaintDC dc(this);
6879 }
6880
6881 void wxGrid::Refresh(bool eraseb, const wxRect* rect)
6882 {
6883 // Don't do anything if between Begin/EndBatch...
6884 // EndBatch() will do all this on the last nested one anyway.
6885 if ( m_created && !GetBatchCount() )
6886 {
6887 // Refresh to get correct scrolled position:
6888 wxScrolledWindow::Refresh(eraseb, rect);
6889
6890 if (rect)
6891 {
6892 int rect_x, rect_y, rectWidth, rectHeight;
6893 int width_label, width_cell, height_label, height_cell;
6894 int x, y;
6895
6896 // Copy rectangle can get scroll offsets..
6897 rect_x = rect->GetX();
6898 rect_y = rect->GetY();
6899 rectWidth = rect->GetWidth();
6900 rectHeight = rect->GetHeight();
6901
6902 width_label = m_rowLabelWidth - rect_x;
6903 if (width_label > rectWidth)
6904 width_label = rectWidth;
6905
6906 height_label = m_colLabelHeight - rect_y;
6907 if (height_label > rectHeight)
6908 height_label = rectHeight;
6909
6910 if (rect_x > m_rowLabelWidth)
6911 {
6912 x = rect_x - m_rowLabelWidth;
6913 width_cell = rectWidth;
6914 }
6915 else
6916 {
6917 x = 0;
6918 width_cell = rectWidth - (m_rowLabelWidth - rect_x);
6919 }
6920
6921 if (rect_y > m_colLabelHeight)
6922 {
6923 y = rect_y - m_colLabelHeight;
6924 height_cell = rectHeight;
6925 }
6926 else
6927 {
6928 y = 0;
6929 height_cell = rectHeight - (m_colLabelHeight - rect_y);
6930 }
6931
6932 // Paint corner label part intersecting rect.
6933 if ( width_label > 0 && height_label > 0 )
6934 {
6935 wxRect anotherrect(rect_x, rect_y, width_label, height_label);
6936 m_cornerLabelWin->Refresh(eraseb, &anotherrect);
6937 }
6938
6939 // Paint col labels part intersecting rect.
6940 if ( width_cell > 0 && height_label > 0 )
6941 {
6942 wxRect anotherrect(x, rect_y, width_cell, height_label);
6943 m_colLabelWin->Refresh(eraseb, &anotherrect);
6944 }
6945
6946 // Paint row labels part intersecting rect.
6947 if ( width_label > 0 && height_cell > 0 )
6948 {
6949 wxRect anotherrect(rect_x, y, width_label, height_cell);
6950 m_rowLabelWin->Refresh(eraseb, &anotherrect);
6951 }
6952
6953 // Paint cell area part intersecting rect.
6954 if ( width_cell > 0 && height_cell > 0 )
6955 {
6956 wxRect anotherrect(x, y, width_cell, height_cell);
6957 m_gridWin->Refresh(eraseb, &anotherrect);
6958 }
6959 }
6960 else
6961 {
6962 m_cornerLabelWin->Refresh(eraseb, NULL);
6963 m_colLabelWin->Refresh(eraseb, NULL);
6964 m_rowLabelWin->Refresh(eraseb, NULL);
6965 m_gridWin->Refresh(eraseb, NULL);
6966 }
6967 }
6968 }
6969
6970 void wxGrid::OnSize(wxSizeEvent& WXUNUSED(event))
6971 {
6972 if (m_targetWindow != this) // check whether initialisation has been done
6973 {
6974 // reposition our children windows
6975 CalcWindowSizes();
6976 }
6977 }
6978
6979 void wxGrid::OnKeyDown( wxKeyEvent& event )
6980 {
6981 if ( m_inOnKeyDown )
6982 {
6983 // shouldn't be here - we are going round in circles...
6984 //
6985 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") );
6986 }
6987
6988 m_inOnKeyDown = true;
6989
6990 // propagate the event up and see if it gets processed
6991 wxWindow *parent = GetParent();
6992 wxKeyEvent keyEvt( event );
6993 keyEvt.SetEventObject( parent );
6994
6995 if ( !parent->GetEventHandler()->ProcessEvent( keyEvt ) )
6996 {
6997 if (GetLayoutDirection() == wxLayout_RightToLeft)
6998 {
6999 if (event.GetKeyCode() == WXK_RIGHT)
7000 event.m_keyCode = WXK_LEFT;
7001 else if (event.GetKeyCode() == WXK_LEFT)
7002 event.m_keyCode = WXK_RIGHT;
7003 }
7004
7005 // try local handlers
7006 switch ( event.GetKeyCode() )
7007 {
7008 case WXK_UP:
7009 if ( event.ControlDown() )
7010 MoveCursorUpBlock( event.ShiftDown() );
7011 else
7012 MoveCursorUp( event.ShiftDown() );
7013 break;
7014
7015 case WXK_DOWN:
7016 if ( event.ControlDown() )
7017 MoveCursorDownBlock( event.ShiftDown() );
7018 else
7019 MoveCursorDown( event.ShiftDown() );
7020 break;
7021
7022 case WXK_LEFT:
7023 if ( event.ControlDown() )
7024 MoveCursorLeftBlock( event.ShiftDown() );
7025 else
7026 MoveCursorLeft( event.ShiftDown() );
7027 break;
7028
7029 case WXK_RIGHT:
7030 if ( event.ControlDown() )
7031 MoveCursorRightBlock( event.ShiftDown() );
7032 else
7033 MoveCursorRight( event.ShiftDown() );
7034 break;
7035
7036 case WXK_RETURN:
7037 case WXK_NUMPAD_ENTER:
7038 if ( event.ControlDown() )
7039 {
7040 event.Skip(); // to let the edit control have the return
7041 }
7042 else
7043 {
7044 if ( GetGridCursorRow() < GetNumberRows()-1 )
7045 {
7046 MoveCursorDown( event.ShiftDown() );
7047 }
7048 else
7049 {
7050 // at the bottom of a column
7051 DisableCellEditControl();
7052 }
7053 }
7054 break;
7055
7056 case WXK_ESCAPE:
7057 ClearSelection();
7058 break;
7059
7060 case WXK_TAB:
7061 if (event.ShiftDown())
7062 {
7063 if ( GetGridCursorCol() > 0 )
7064 {
7065 MoveCursorLeft( false );
7066 }
7067 else
7068 {
7069 // at left of grid
7070 DisableCellEditControl();
7071 }
7072 }
7073 else
7074 {
7075 if ( GetGridCursorCol() < GetNumberCols() - 1 )
7076 {
7077 MoveCursorRight( false );
7078 }
7079 else
7080 {
7081 // at right of grid
7082 DisableCellEditControl();
7083 }
7084 }
7085 break;
7086
7087 case WXK_HOME:
7088 if ( event.ControlDown() )
7089 {
7090 MakeCellVisible( 0, 0 );
7091 SetCurrentCell( 0, 0 );
7092 }
7093 else
7094 {
7095 event.Skip();
7096 }
7097 break;
7098
7099 case WXK_END:
7100 if ( event.ControlDown() )
7101 {
7102 MakeCellVisible( m_numRows - 1, m_numCols - 1 );
7103 SetCurrentCell( m_numRows - 1, m_numCols - 1 );
7104 }
7105 else
7106 {
7107 event.Skip();
7108 }
7109 break;
7110
7111 case WXK_PAGEUP:
7112 MovePageUp();
7113 break;
7114
7115 case WXK_PAGEDOWN:
7116 MovePageDown();
7117 break;
7118
7119 case WXK_SPACE:
7120 if ( event.ControlDown() )
7121 {
7122 if ( m_selection )
7123 {
7124 m_selection->ToggleCellSelection(
7125 m_currentCellCoords.GetRow(),
7126 m_currentCellCoords.GetCol(),
7127 event.ControlDown(),
7128 event.ShiftDown(),
7129 event.AltDown(),
7130 event.MetaDown() );
7131 }
7132 break;
7133 }
7134
7135 if ( !IsEditable() )
7136 MoveCursorRight( false );
7137 else
7138 event.Skip();
7139 break;
7140
7141 default:
7142 event.Skip();
7143 break;
7144 }
7145 }
7146
7147 m_inOnKeyDown = false;
7148 }
7149
7150 void wxGrid::OnKeyUp( wxKeyEvent& event )
7151 {
7152 // try local handlers
7153 //
7154 if ( event.GetKeyCode() == WXK_SHIFT )
7155 {
7156 if ( m_selectingTopLeft != wxGridNoCellCoords &&
7157 m_selectingBottomRight != wxGridNoCellCoords )
7158 {
7159 if ( m_selection )
7160 {
7161 m_selection->SelectBlock(
7162 m_selectingTopLeft.GetRow(),
7163 m_selectingTopLeft.GetCol(),
7164 m_selectingBottomRight.GetRow(),
7165 m_selectingBottomRight.GetCol(),
7166 event.ControlDown(),
7167 true,
7168 event.AltDown(),
7169 event.MetaDown() );
7170 }
7171 }
7172
7173 m_selectingTopLeft = wxGridNoCellCoords;
7174 m_selectingBottomRight = wxGridNoCellCoords;
7175 m_selectingKeyboard = wxGridNoCellCoords;
7176 }
7177 }
7178
7179 void wxGrid::OnChar( wxKeyEvent& event )
7180 {
7181 // is it possible to edit the current cell at all?
7182 if ( !IsCellEditControlEnabled() && CanEnableCellControl() )
7183 {
7184 // yes, now check whether the cells editor accepts the key
7185 int row = m_currentCellCoords.GetRow();
7186 int col = m_currentCellCoords.GetCol();
7187 wxGridCellAttr *attr = GetCellAttr(row, col);
7188 wxGridCellEditor *editor = attr->GetEditor(this, row, col);
7189
7190 // <F2> is special and will always start editing, for
7191 // other keys - ask the editor itself
7192 if ( (event.GetKeyCode() == WXK_F2 && !event.HasModifiers())
7193 || editor->IsAcceptedKey(event) )
7194 {
7195 // ensure cell is visble
7196 MakeCellVisible(row, col);
7197 EnableCellEditControl();
7198
7199 // a problem can arise if the cell is not completely
7200 // visible (even after calling MakeCellVisible the
7201 // control is not created and calling StartingKey will
7202 // crash the app
7203 if ( event.GetKeyCode() != WXK_F2 && editor->IsCreated() && m_cellEditCtrlEnabled )
7204 editor->StartingKey(event);
7205 }
7206 else
7207 {
7208 event.Skip();
7209 }
7210
7211 editor->DecRef();
7212 attr->DecRef();
7213 }
7214 else
7215 {
7216 event.Skip();
7217 }
7218 }
7219
7220 void wxGrid::OnEraseBackground(wxEraseEvent&)
7221 {
7222 }
7223
7224 void wxGrid::SetCurrentCell( const wxGridCellCoords& coords )
7225 {
7226 if ( SendEvent( wxEVT_GRID_SELECT_CELL, coords.GetRow(), coords.GetCol() ) )
7227 {
7228 // the event has been intercepted - do nothing
7229 return;
7230 }
7231
7232 #if !defined(__WXMAC__)
7233 wxClientDC dc( m_gridWin );
7234 PrepareDC( dc );
7235 #endif
7236
7237 if ( m_currentCellCoords != wxGridNoCellCoords )
7238 {
7239 DisableCellEditControl();
7240
7241 if ( IsVisible( m_currentCellCoords, false ) )
7242 {
7243 wxRect r;
7244 r = BlockToDeviceRect( m_currentCellCoords, m_currentCellCoords );
7245 if ( !m_gridLinesEnabled )
7246 {
7247 r.x--;
7248 r.y--;
7249 r.width++;
7250 r.height++;
7251 }
7252
7253 wxGridCellCoordsArray cells = CalcCellsExposed( r );
7254
7255 // Otherwise refresh redraws the highlight!
7256 m_currentCellCoords = coords;
7257
7258 #if defined(__WXMAC__)
7259 m_gridWin->Refresh(true /*, & r */);
7260 #else
7261 DrawGridCellArea( dc, cells );
7262 DrawAllGridLines( dc, r );
7263 #endif
7264 }
7265 }
7266
7267 m_currentCellCoords = coords;
7268
7269 wxGridCellAttr *attr = GetCellAttr( coords );
7270 #if !defined(__WXMAC__)
7271 DrawCellHighlight( dc, attr );
7272 #endif
7273 attr->DecRef();
7274 }
7275
7276 void wxGrid::HighlightBlock( int topRow, int leftCol, int bottomRow, int rightCol )
7277 {
7278 int temp;
7279 wxGridCellCoords updateTopLeft, updateBottomRight;
7280
7281 if ( m_selection )
7282 {
7283 if ( m_selection->GetSelectionMode() == wxGrid::wxGridSelectRows )
7284 {
7285 leftCol = 0;
7286 rightCol = GetNumberCols() - 1;
7287 }
7288 else if ( m_selection->GetSelectionMode() == wxGrid::wxGridSelectColumns )
7289 {
7290 topRow = 0;
7291 bottomRow = GetNumberRows() - 1;
7292 }
7293 }
7294
7295 if ( topRow > bottomRow )
7296 {
7297 temp = topRow;
7298 topRow = bottomRow;
7299 bottomRow = temp;
7300 }
7301
7302 if ( leftCol > rightCol )
7303 {
7304 temp = leftCol;
7305 leftCol = rightCol;
7306 rightCol = temp;
7307 }
7308
7309 updateTopLeft = wxGridCellCoords( topRow, leftCol );
7310 updateBottomRight = wxGridCellCoords( bottomRow, rightCol );
7311
7312 // First the case that we selected a completely new area
7313 if ( m_selectingTopLeft == wxGridNoCellCoords ||
7314 m_selectingBottomRight == wxGridNoCellCoords )
7315 {
7316 wxRect rect;
7317 rect = BlockToDeviceRect( wxGridCellCoords ( topRow, leftCol ),
7318 wxGridCellCoords ( bottomRow, rightCol ) );
7319 m_gridWin->Refresh( false, &rect );
7320 }
7321
7322 // Now handle changing an existing selection area.
7323 else if ( m_selectingTopLeft != updateTopLeft ||
7324 m_selectingBottomRight != updateBottomRight )
7325 {
7326 // Compute two optimal update rectangles:
7327 // Either one rectangle is a real subset of the
7328 // other, or they are (almost) disjoint!
7329 wxRect rect[4];
7330 bool need_refresh[4];
7331 need_refresh[0] =
7332 need_refresh[1] =
7333 need_refresh[2] =
7334 need_refresh[3] = false;
7335 int i;
7336
7337 // Store intermediate values
7338 wxCoord oldLeft = m_selectingTopLeft.GetCol();
7339 wxCoord oldTop = m_selectingTopLeft.GetRow();
7340 wxCoord oldRight = m_selectingBottomRight.GetCol();
7341 wxCoord oldBottom = m_selectingBottomRight.GetRow();
7342
7343 // Determine the outer/inner coordinates.
7344 if (oldLeft > leftCol)
7345 {
7346 temp = oldLeft;
7347 oldLeft = leftCol;
7348 leftCol = temp;
7349 }
7350 if (oldTop > topRow )
7351 {
7352 temp = oldTop;
7353 oldTop = topRow;
7354 topRow = temp;
7355 }
7356 if (oldRight < rightCol )
7357 {
7358 temp = oldRight;
7359 oldRight = rightCol;
7360 rightCol = temp;
7361 }
7362 if (oldBottom < bottomRow)
7363 {
7364 temp = oldBottom;
7365 oldBottom = bottomRow;
7366 bottomRow = temp;
7367 }
7368
7369 // Now, either the stuff marked old is the outer
7370 // rectangle or we don't have a situation where one
7371 // is contained in the other.
7372
7373 if ( oldLeft < leftCol )
7374 {
7375 // Refresh the newly selected or deselected
7376 // area to the left of the old or new selection.
7377 need_refresh[0] = true;
7378 rect[0] = BlockToDeviceRect(
7379 wxGridCellCoords( oldTop, oldLeft ),
7380 wxGridCellCoords( oldBottom, leftCol - 1 ) );
7381 }
7382
7383 if ( oldTop < topRow )
7384 {
7385 // Refresh the newly selected or deselected
7386 // area above the old or new selection.
7387 need_refresh[1] = true;
7388 rect[1] = BlockToDeviceRect(
7389 wxGridCellCoords( oldTop, leftCol ),
7390 wxGridCellCoords( topRow - 1, rightCol ) );
7391 }
7392
7393 if ( oldRight > rightCol )
7394 {
7395 // Refresh the newly selected or deselected
7396 // area to the right of the old or new selection.
7397 need_refresh[2] = true;
7398 rect[2] = BlockToDeviceRect(
7399 wxGridCellCoords( oldTop, rightCol + 1 ),
7400 wxGridCellCoords( oldBottom, oldRight ) );
7401 }
7402
7403 if ( oldBottom > bottomRow )
7404 {
7405 // Refresh the newly selected or deselected
7406 // area below the old or new selection.
7407 need_refresh[3] = true;
7408 rect[3] = BlockToDeviceRect(
7409 wxGridCellCoords( bottomRow + 1, leftCol ),
7410 wxGridCellCoords( oldBottom, rightCol ) );
7411 }
7412
7413 // various Refresh() calls
7414 for (i = 0; i < 4; i++ )
7415 if ( need_refresh[i] && rect[i] != wxGridNoCellRect )
7416 m_gridWin->Refresh( false, &(rect[i]) );
7417 }
7418
7419 // change selection
7420 m_selectingTopLeft = updateTopLeft;
7421 m_selectingBottomRight = updateBottomRight;
7422 }
7423
7424 //
7425 // ------ functions to get/send data (see also public functions)
7426 //
7427
7428 bool wxGrid::GetModelValues()
7429 {
7430 // Hide the editor, so it won't hide a changed value.
7431 HideCellEditControl();
7432
7433 if ( m_table )
7434 {
7435 // all we need to do is repaint the grid
7436 //
7437 m_gridWin->Refresh();
7438 return true;
7439 }
7440
7441 return false;
7442 }
7443
7444 bool wxGrid::SetModelValues()
7445 {
7446 int row, col;
7447
7448 // Disable the editor, so it won't hide a changed value.
7449 // Do we also want to save the current value of the editor first?
7450 // I think so ...
7451 DisableCellEditControl();
7452
7453 if ( m_table )
7454 {
7455 for ( row = 0; row < m_numRows; row++ )
7456 {
7457 for ( col = 0; col < m_numCols; col++ )
7458 {
7459 m_table->SetValue( row, col, GetCellValue(row, col) );
7460 }
7461 }
7462
7463 return true;
7464 }
7465
7466 return false;
7467 }
7468
7469 // Note - this function only draws cells that are in the list of
7470 // exposed cells (usually set from the update region by
7471 // CalcExposedCells)
7472 //
7473 void wxGrid::DrawGridCellArea( wxDC& dc, const wxGridCellCoordsArray& cells )
7474 {
7475 if ( !m_numRows || !m_numCols )
7476 return;
7477
7478 int i, numCells = cells.GetCount();
7479 int row, col, cell_rows, cell_cols;
7480 wxGridCellCoordsArray redrawCells;
7481
7482 for ( i = numCells - 1; i >= 0; i-- )
7483 {
7484 row = cells[i].GetRow();
7485 col = cells[i].GetCol();
7486 GetCellSize( row, col, &cell_rows, &cell_cols );
7487
7488 // If this cell is part of a multicell block, find owner for repaint
7489 if ( cell_rows <= 0 || cell_cols <= 0 )
7490 {
7491 wxGridCellCoords cell( row + cell_rows, col + cell_cols );
7492 bool marked = false;
7493 for ( int j = 0; j < numCells; j++ )
7494 {
7495 if ( cell == cells[j] )
7496 {
7497 marked = true;
7498 break;
7499 }
7500 }
7501
7502 if (!marked)
7503 {
7504 int count = redrawCells.GetCount();
7505 for (int j = 0; j < count; j++)
7506 {
7507 if ( cell == redrawCells[j] )
7508 {
7509 marked = true;
7510 break;
7511 }
7512 }
7513
7514 if (!marked)
7515 redrawCells.Add( cell );
7516 }
7517
7518 // don't bother drawing this cell
7519 continue;
7520 }
7521
7522 // If this cell is empty, find cell to left that might want to overflow
7523 if (m_table && m_table->IsEmptyCell(row, col))
7524 {
7525 for ( int l = 0; l < cell_rows; l++ )
7526 {
7527 // find a cell in this row to leave already marked for repaint
7528 int left = col;
7529 for (int k = 0; k < int(redrawCells.GetCount()); k++)
7530 if ((redrawCells[k].GetCol() < left) &&
7531 (redrawCells[k].GetRow() == row))
7532 {
7533 left = redrawCells[k].GetCol();
7534 }
7535
7536 if (left == col)
7537 left = 0; // oh well
7538
7539 for (int j = col - 1; j >= left; j--)
7540 {
7541 if (!m_table->IsEmptyCell(row + l, j))
7542 {
7543 if (GetCellOverflow(row + l, j))
7544 {
7545 wxGridCellCoords cell(row + l, j);
7546 bool marked = false;
7547
7548 for (int k = 0; k < numCells; k++)
7549 {
7550 if ( cell == cells[k] )
7551 {
7552 marked = true;
7553 break;
7554 }
7555 }
7556
7557 if (!marked)
7558 {
7559 int count = redrawCells.GetCount();
7560 for (int k = 0; k < count; k++)
7561 {
7562 if ( cell == redrawCells[k] )
7563 {
7564 marked = true;
7565 break;
7566 }
7567 }
7568 if (!marked)
7569 redrawCells.Add( cell );
7570 }
7571 }
7572 break;
7573 }
7574 }
7575 }
7576 }
7577
7578 DrawCell( dc, cells[i] );
7579 }
7580
7581 numCells = redrawCells.GetCount();
7582
7583 for ( i = numCells - 1; i >= 0; i-- )
7584 {
7585 DrawCell( dc, redrawCells[i] );
7586 }
7587 }
7588
7589 void wxGrid::DrawGridSpace( wxDC& dc )
7590 {
7591 int cw, ch;
7592 m_gridWin->GetClientSize( &cw, &ch );
7593
7594 int right, bottom;
7595 CalcUnscrolledPosition( cw, ch, &right, &bottom );
7596
7597 int rightCol = m_numCols > 0 ? GetColRight(GetColAt( m_numCols - 1 )) : 0;
7598 int bottomRow = m_numRows > 0 ? GetRowBottom(m_numRows - 1) : 0;
7599
7600 if ( right > rightCol || bottom > bottomRow )
7601 {
7602 int left, top;
7603 CalcUnscrolledPosition( 0, 0, &left, &top );
7604
7605 dc.SetBrush( wxBrush(GetDefaultCellBackgroundColour(), wxBRUSHSTYLE_SOLID) );
7606 dc.SetPen( *wxTRANSPARENT_PEN );
7607
7608 if ( right > rightCol )
7609 {
7610 dc.DrawRectangle( rightCol, top, right - rightCol, ch );
7611 }
7612
7613 if ( bottom > bottomRow )
7614 {
7615 dc.DrawRectangle( left, bottomRow, cw, bottom - bottomRow );
7616 }
7617 }
7618 }
7619
7620 void wxGrid::DrawCell( wxDC& dc, const wxGridCellCoords& coords )
7621 {
7622 int row = coords.GetRow();
7623 int col = coords.GetCol();
7624
7625 if ( GetColWidth(col) <= 0 || GetRowHeight(row) <= 0 )
7626 return;
7627
7628 // we draw the cell border ourselves
7629 #if !WXGRID_DRAW_LINES
7630 if ( m_gridLinesEnabled )
7631 DrawCellBorder( dc, coords );
7632 #endif
7633
7634 wxGridCellAttr* attr = GetCellAttr(row, col);
7635
7636 bool isCurrent = coords == m_currentCellCoords;
7637
7638 wxRect rect = CellToRect( row, col );
7639
7640 // if the editor is shown, we should use it and not the renderer
7641 // Note: However, only if it is really _shown_, i.e. not hidden!
7642 if ( isCurrent && IsCellEditControlShown() )
7643 {
7644 // NB: this "#if..." is temporary and fixes a problem where the
7645 // edit control is erased by this code after being rendered.
7646 // On wxMac (QD build only), the cell editor is a wxTextCntl and is rendered
7647 // implicitly, causing this out-of order render.
7648 #if !defined(__WXMAC__)
7649 wxGridCellEditor *editor = attr->GetEditor(this, row, col);
7650 editor->PaintBackground(rect, attr);
7651 editor->DecRef();
7652 #endif
7653 }
7654 else
7655 {
7656 // but all the rest is drawn by the cell renderer and hence may be customized
7657 wxGridCellRenderer *renderer = attr->GetRenderer(this, row, col);
7658 renderer->Draw(*this, *attr, dc, rect, row, col, IsInSelection(coords));
7659 renderer->DecRef();
7660 }
7661
7662 attr->DecRef();
7663 }
7664
7665 void wxGrid::DrawCellHighlight( wxDC& dc, const wxGridCellAttr *attr )
7666 {
7667 // don't show highlight when the grid doesn't have focus
7668 if ( !HasFocus() )
7669 return;
7670
7671 int row = m_currentCellCoords.GetRow();
7672 int col = m_currentCellCoords.GetCol();
7673
7674 if ( GetColWidth(col) <= 0 || GetRowHeight(row) <= 0 )
7675 return;
7676
7677 wxRect rect = CellToRect(row, col);
7678
7679 // hmmm... what could we do here to show that the cell is disabled?
7680 // for now, I just draw a thinner border than for the other ones, but
7681 // it doesn't look really good
7682
7683 int penWidth = attr->IsReadOnly() ? m_cellHighlightROPenWidth : m_cellHighlightPenWidth;
7684
7685 if (penWidth > 0)
7686 {
7687 // The center of the drawn line is where the position/width/height of
7688 // the rectangle is actually at (on wxMSW at least), so the
7689 // size of the rectangle is reduced to compensate for the thickness of
7690 // the line. If this is too strange on non-wxMSW platforms then
7691 // please #ifdef this appropriately.
7692 rect.x += penWidth / 2;
7693 rect.y += penWidth / 2;
7694 rect.width -= penWidth - 1;
7695 rect.height -= penWidth - 1;
7696
7697 // Now draw the rectangle
7698 // use the cellHighlightColour if the cell is inside a selection, this
7699 // will ensure the cell is always visible.
7700 dc.SetPen(wxPen(IsInSelection(row,col) ? m_selectionForeground : m_cellHighlightColour, penWidth, wxPENSTYLE_SOLID));
7701 dc.SetBrush(*wxTRANSPARENT_BRUSH);
7702 dc.DrawRectangle(rect);
7703 }
7704
7705 #if 0
7706 // VZ: my experiments with 3D borders...
7707
7708 // how to properly set colours for arbitrary bg?
7709 wxCoord x1 = rect.x,
7710 y1 = rect.y,
7711 x2 = rect.x + rect.width - 1,
7712 y2 = rect.y + rect.height - 1;
7713
7714 dc.SetPen(*wxWHITE_PEN);
7715 dc.DrawLine(x1, y1, x2, y1);
7716 dc.DrawLine(x1, y1, x1, y2);
7717
7718 dc.DrawLine(x1 + 1, y2 - 1, x2 - 1, y2 - 1);
7719 dc.DrawLine(x2 - 1, y1 + 1, x2 - 1, y2);
7720
7721 dc.SetPen(*wxBLACK_PEN);
7722 dc.DrawLine(x1, y2, x2, y2);
7723 dc.DrawLine(x2, y1, x2, y2 + 1);
7724 #endif
7725 }
7726
7727 wxPen wxGrid::GetDefaultGridLinePen()
7728 {
7729 return wxPen(GetGridLineColour(), 1, wxPENSTYLE_SOLID);
7730 }
7731
7732 wxPen wxGrid::GetRowGridLinePen(int WXUNUSED(row))
7733 {
7734 return GetDefaultGridLinePen();
7735 }
7736
7737 wxPen wxGrid::GetColGridLinePen(int WXUNUSED(col))
7738 {
7739 return GetDefaultGridLinePen();
7740 }
7741
7742 void wxGrid::DrawCellBorder( wxDC& dc, const wxGridCellCoords& coords )
7743 {
7744 int row = coords.GetRow();
7745 int col = coords.GetCol();
7746 if ( GetColWidth(col) <= 0 || GetRowHeight(row) <= 0 )
7747 return;
7748
7749
7750 wxRect rect = CellToRect( row, col );
7751
7752 // right hand border
7753 dc.SetPen( GetColGridLinePen(col) );
7754 dc.DrawLine( rect.x + rect.width, rect.y,
7755 rect.x + rect.width, rect.y + rect.height + 1 );
7756
7757 // bottom border
7758 dc.SetPen( GetRowGridLinePen(row) );
7759 dc.DrawLine( rect.x, rect.y + rect.height,
7760 rect.x + rect.width, rect.y + rect.height);
7761 }
7762
7763 void wxGrid::DrawHighlight(wxDC& dc, const wxGridCellCoordsArray& cells)
7764 {
7765 // This if block was previously in wxGrid::OnPaint but that doesn't
7766 // seem to get called under wxGTK - MB
7767 //
7768 if ( m_currentCellCoords == wxGridNoCellCoords &&
7769 m_numRows && m_numCols )
7770 {
7771 m_currentCellCoords.Set(0, 0);
7772 }
7773
7774 if ( IsCellEditControlShown() )
7775 {
7776 // don't show highlight when the edit control is shown
7777 return;
7778 }
7779
7780 // if the active cell was repainted, repaint its highlight too because it
7781 // might have been damaged by the grid lines
7782 size_t count = cells.GetCount();
7783 for ( size_t n = 0; n < count; n++ )
7784 {
7785 wxGridCellCoords cell = cells[n];
7786
7787 // If we are using attributes, then we may have just exposed another
7788 // cell in a partially-visible merged cluster of cells. If the "anchor"
7789 // (upper left) cell of this merged cluster is the cell indicated by
7790 // m_currentCellCoords, then we need to refresh the cell highlight even
7791 // though the "anchor" itself is not part of our update segment.
7792 if ( CanHaveAttributes() )
7793 {
7794 int rows = 0,
7795 cols = 0;
7796 GetCellSize(cell.GetRow(), cell.GetCol(), &rows, &cols);
7797
7798 if ( rows < 0 )
7799 cell.SetRow(cell.GetRow() + rows);
7800
7801 if ( cols < 0 )
7802 cell.SetCol(cell.GetCol() + cols);
7803 }
7804
7805 if ( cell == m_currentCellCoords )
7806 {
7807 wxGridCellAttr* attr = GetCellAttr(m_currentCellCoords);
7808 DrawCellHighlight(dc, attr);
7809 attr->DecRef();
7810
7811 break;
7812 }
7813 }
7814 }
7815
7816 // TODO: remove this ???
7817 // This is used to redraw all grid lines e.g. when the grid line colour
7818 // has been changed
7819 //
7820 void wxGrid::DrawAllGridLines( wxDC& dc, const wxRegion & WXUNUSED(reg) )
7821 {
7822 #if !WXGRID_DRAW_LINES
7823 return;
7824 #endif
7825
7826 if ( !m_gridLinesEnabled || !m_numRows || !m_numCols )
7827 return;
7828
7829 int top, bottom, left, right;
7830
7831 #if 0 //#ifndef __WXGTK__
7832 if (reg.IsEmpty())
7833 {
7834 int cw, ch;
7835 m_gridWin->GetClientSize(&cw, &ch);
7836
7837 // virtual coords of visible area
7838 //
7839 CalcUnscrolledPosition( 0, 0, &left, &top );
7840 CalcUnscrolledPosition( cw, ch, &right, &bottom );
7841 }
7842 else
7843 {
7844 wxCoord x, y, w, h;
7845 reg.GetBox(x, y, w, h);
7846 CalcUnscrolledPosition( x, y, &left, &top );
7847 CalcUnscrolledPosition( x + w, y + h, &right, &bottom );
7848 }
7849 #else
7850 int cw, ch;
7851 m_gridWin->GetClientSize(&cw, &ch);
7852 CalcUnscrolledPosition( 0, 0, &left, &top );
7853 CalcUnscrolledPosition( cw, ch, &right, &bottom );
7854 #endif
7855
7856 // avoid drawing grid lines past the last row and col
7857 //
7858 right = wxMin( right, GetColRight(GetColAt( m_numCols - 1 )) );
7859 bottom = wxMin( bottom, GetRowBottom(m_numRows - 1) );
7860
7861 // no gridlines inside multicells, clip them out
7862 int leftCol = GetColPos( internalXToCol(left) );
7863 int topRow = internalYToRow(top);
7864 int rightCol = GetColPos( internalXToCol(right) );
7865 int bottomRow = internalYToRow(bottom);
7866
7867 wxRegion clippedcells(0, 0, cw, ch);
7868
7869 int i, j, cell_rows, cell_cols;
7870 wxRect rect;
7871
7872 for (j=topRow; j<=bottomRow; j++)
7873 {
7874 int colPos;
7875 for (colPos=leftCol; colPos<=rightCol; colPos++)
7876 {
7877 i = GetColAt( colPos );
7878
7879 GetCellSize( j, i, &cell_rows, &cell_cols );
7880 if ((cell_rows > 1) || (cell_cols > 1))
7881 {
7882 rect = CellToRect(j,i);
7883 CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
7884 clippedcells.Subtract(rect);
7885 }
7886 else if ((cell_rows < 0) || (cell_cols < 0))
7887 {
7888 rect = CellToRect(j + cell_rows, i + cell_cols);
7889 CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
7890 clippedcells.Subtract(rect);
7891 }
7892 }
7893 }
7894
7895 dc.SetDeviceClippingRegion( clippedcells );
7896
7897
7898 // horizontal grid lines
7899 //
7900 // already declared above - int i;
7901 for ( i = internalYToRow(top); i < m_numRows; i++ )
7902 {
7903 int bot = GetRowBottom(i) - 1;
7904
7905 if ( bot > bottom )
7906 {
7907 break;
7908 }
7909
7910 if ( bot >= top )
7911 {
7912 dc.SetPen( GetRowGridLinePen(i) );
7913 dc.DrawLine( left, bot, right, bot );
7914 }
7915 }
7916
7917 // vertical grid lines
7918 //
7919 int colPos;
7920 for ( colPos = leftCol; colPos < m_numCols; colPos++ )
7921 {
7922 i = GetColAt( colPos );
7923
7924 int colRight = GetColRight(i);
7925 #ifdef __WXGTK__
7926 if (GetLayoutDirection() != wxLayout_RightToLeft)
7927 #endif
7928 colRight--;
7929
7930 if ( colRight > right )
7931 {
7932 break;
7933 }
7934
7935 if ( colRight >= left )
7936 {
7937 dc.SetPen( GetColGridLinePen(i) );
7938 dc.DrawLine( colRight, top, colRight, bottom );
7939 }
7940 }
7941
7942 dc.DestroyClippingRegion();
7943 }
7944
7945 void wxGrid::DrawRowLabels( wxDC& dc, const wxArrayInt& rows)
7946 {
7947 if ( !m_numRows )
7948 return;
7949
7950 size_t i;
7951 size_t numLabels = rows.GetCount();
7952
7953 for ( i = 0; i < numLabels; i++ )
7954 {
7955 DrawRowLabel( dc, rows[i] );
7956 }
7957 }
7958
7959 void wxGrid::DrawRowLabel( wxDC& dc, int row )
7960 {
7961 if ( GetRowHeight(row) <= 0 || m_rowLabelWidth <= 0 )
7962 return;
7963
7964 wxRect rect;
7965
7966 int rowTop = GetRowTop(row),
7967 rowBottom = GetRowBottom(row) - 1;
7968
7969 dc.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW), 1, wxPENSTYLE_SOLID) );
7970 dc.DrawLine( m_rowLabelWidth - 1, rowTop, m_rowLabelWidth - 1, rowBottom );
7971 dc.DrawLine( 0, rowTop, 0, rowBottom );
7972 dc.DrawLine( 0, rowBottom, m_rowLabelWidth, rowBottom );
7973
7974 dc.SetPen( *wxWHITE_PEN );
7975 dc.DrawLine( 1, rowTop, 1, rowBottom );
7976 dc.DrawLine( 1, rowTop, m_rowLabelWidth - 1, rowTop );
7977
7978 dc.SetBackgroundMode( wxBRUSHSTYLE_TRANSPARENT );
7979 dc.SetTextForeground( GetLabelTextColour() );
7980 dc.SetFont( GetLabelFont() );
7981
7982 int hAlign, vAlign;
7983 GetRowLabelAlignment( &hAlign, &vAlign );
7984
7985 rect.SetX( 2 );
7986 rect.SetY( GetRowTop(row) + 2 );
7987 rect.SetWidth( m_rowLabelWidth - 4 );
7988 rect.SetHeight( GetRowHeight(row) - 4 );
7989 DrawTextRectangle( dc, GetRowLabelValue( row ), rect, hAlign, vAlign );
7990 }
7991
7992 void wxGrid::SetUseNativeColLabels( bool native )
7993 {
7994 m_nativeColumnLabels = native;
7995 if (native)
7996 {
7997 int height = wxRendererNative::Get().GetHeaderButtonHeight( this );
7998 SetColLabelSize( height );
7999 }
8000
8001 m_colLabelWin->Refresh();
8002 }
8003
8004 void wxGrid::DrawColLabels( wxDC& dc,const wxArrayInt& cols )
8005 {
8006 if ( !m_numCols )
8007 return;
8008
8009 size_t i;
8010 size_t numLabels = cols.GetCount();
8011
8012 for ( i = 0; i < numLabels; i++ )
8013 {
8014 DrawColLabel( dc, cols[i] );
8015 }
8016 }
8017
8018 void wxGrid::DrawColLabel( wxDC& dc, int col )
8019 {
8020 if ( GetColWidth(col) <= 0 || m_colLabelHeight <= 0 )
8021 return;
8022
8023 int colLeft = GetColLeft(col);
8024
8025 wxRect rect;
8026
8027 if (m_nativeColumnLabels)
8028 {
8029 rect.SetX( colLeft);
8030 rect.SetY( 0 );
8031 rect.SetWidth( GetColWidth(col));
8032 rect.SetHeight( m_colLabelHeight );
8033
8034 wxWindowDC *win_dc = (wxWindowDC*) &dc;
8035 wxRendererNative::Get().DrawHeaderButton( win_dc->GetWindow(), dc, rect, 0 );
8036 }
8037 else
8038 {
8039 int colRight = GetColRight(col) - 1;
8040
8041 dc.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW), 1, wxPENSTYLE_SOLID) );
8042 dc.DrawLine( colRight, 0, colRight, m_colLabelHeight - 1 );
8043 dc.DrawLine( colLeft, 0, colRight, 0 );
8044 dc.DrawLine( colLeft, m_colLabelHeight - 1,
8045 colRight + 1, m_colLabelHeight - 1 );
8046
8047 dc.SetPen( *wxWHITE_PEN );
8048 dc.DrawLine( colLeft, 1, colLeft, m_colLabelHeight - 1 );
8049 dc.DrawLine( colLeft, 1, colRight, 1 );
8050 }
8051
8052 dc.SetBackgroundMode( wxBRUSHSTYLE_TRANSPARENT );
8053 dc.SetTextForeground( GetLabelTextColour() );
8054 dc.SetFont( GetLabelFont() );
8055
8056 int hAlign, vAlign, orient;
8057 GetColLabelAlignment( &hAlign, &vAlign );
8058 orient = GetColLabelTextOrientation();
8059
8060 rect.SetX( colLeft + 2 );
8061 rect.SetY( 2 );
8062 rect.SetWidth( GetColWidth(col) - 4 );
8063 rect.SetHeight( m_colLabelHeight - 4 );
8064 DrawTextRectangle( dc, GetColLabelValue( col ), rect, hAlign, vAlign, orient );
8065 }
8066
8067 void wxGrid::DrawTextRectangle( wxDC& dc,
8068 const wxString& value,
8069 const wxRect& rect,
8070 int horizAlign,
8071 int vertAlign,
8072 int textOrientation )
8073 {
8074 wxArrayString lines;
8075
8076 StringToLines( value, lines );
8077
8078 // Forward to new API.
8079 DrawTextRectangle( dc,
8080 lines,
8081 rect,
8082 horizAlign,
8083 vertAlign,
8084 textOrientation );
8085 }
8086
8087 // VZ: this should be replaced with wxDC::DrawLabel() to which we just have to
8088 // add textOrientation support
8089 void wxGrid::DrawTextRectangle(wxDC& dc,
8090 const wxArrayString& lines,
8091 const wxRect& rect,
8092 int horizAlign,
8093 int vertAlign,
8094 int textOrientation)
8095 {
8096 if ( lines.empty() )
8097 return;
8098
8099 wxDCClipper clip(dc, rect);
8100
8101 long textWidth,
8102 textHeight;
8103
8104 if ( textOrientation == wxHORIZONTAL )
8105 GetTextBoxSize( dc, lines, &textWidth, &textHeight );
8106 else
8107 GetTextBoxSize( dc, lines, &textHeight, &textWidth );
8108
8109 int x = 0,
8110 y = 0;
8111 switch ( vertAlign )
8112 {
8113 case wxALIGN_BOTTOM:
8114 if ( textOrientation == wxHORIZONTAL )
8115 y = rect.y + (rect.height - textHeight - 1);
8116 else
8117 x = rect.x + rect.width - textWidth;
8118 break;
8119
8120 case wxALIGN_CENTRE:
8121 if ( textOrientation == wxHORIZONTAL )
8122 y = rect.y + ((rect.height - textHeight) / 2);
8123 else
8124 x = rect.x + ((rect.width - textWidth) / 2);
8125 break;
8126
8127 case wxALIGN_TOP:
8128 default:
8129 if ( textOrientation == wxHORIZONTAL )
8130 y = rect.y + 1;
8131 else
8132 x = rect.x + 1;
8133 break;
8134 }
8135
8136 // Align each line of a multi-line label
8137 size_t nLines = lines.GetCount();
8138 for ( size_t l = 0; l < nLines; l++ )
8139 {
8140 const wxString& line = lines[l];
8141
8142 if ( line.empty() )
8143 {
8144 *(textOrientation == wxHORIZONTAL ? &y : &x) += dc.GetCharHeight();
8145 continue;
8146 }
8147
8148 wxCoord lineWidth = 0,
8149 lineHeight = 0;
8150 dc.GetTextExtent(line, &lineWidth, &lineHeight);
8151
8152 switch ( horizAlign )
8153 {
8154 case wxALIGN_RIGHT:
8155 if ( textOrientation == wxHORIZONTAL )
8156 x = rect.x + (rect.width - lineWidth - 1);
8157 else
8158 y = rect.y + lineWidth + 1;
8159 break;
8160
8161 case wxALIGN_CENTRE:
8162 if ( textOrientation == wxHORIZONTAL )
8163 x = rect.x + ((rect.width - lineWidth) / 2);
8164 else
8165 y = rect.y + rect.height - ((rect.height - lineWidth) / 2);
8166 break;
8167
8168 case wxALIGN_LEFT:
8169 default:
8170 if ( textOrientation == wxHORIZONTAL )
8171 x = rect.x + 1;
8172 else
8173 y = rect.y + rect.height - 1;
8174 break;
8175 }
8176
8177 if ( textOrientation == wxHORIZONTAL )
8178 {
8179 dc.DrawText( line, x, y );
8180 y += lineHeight;
8181 }
8182 else
8183 {
8184 dc.DrawRotatedText( line, x, y, 90.0 );
8185 x += lineHeight;
8186 }
8187 }
8188 }
8189
8190 // Split multi-line text up into an array of strings.
8191 // Any existing contents of the string array are preserved.
8192 //
8193 // TODO: refactor wxTextFile::Read() and reuse the same code from here
8194 void wxGrid::StringToLines( const wxString& value, wxArrayString& lines ) const
8195 {
8196 int startPos = 0;
8197 int pos;
8198 wxString eol = wxTextFile::GetEOL( wxTextFileType_Unix );
8199 wxString tVal = wxTextFile::Translate( value, wxTextFileType_Unix );
8200
8201 while ( startPos < (int)tVal.length() )
8202 {
8203 pos = tVal.Mid(startPos).Find( eol );
8204 if ( pos < 0 )
8205 {
8206 break;
8207 }
8208 else if ( pos == 0 )
8209 {
8210 lines.Add( wxEmptyString );
8211 }
8212 else
8213 {
8214 lines.Add( tVal.Mid(startPos, pos) );
8215 }
8216
8217 startPos += pos + 1;
8218 }
8219
8220 if ( startPos < (int)tVal.length() )
8221 {
8222 lines.Add( tVal.Mid( startPos ) );
8223 }
8224 }
8225
8226 void wxGrid::GetTextBoxSize( const wxDC& dc,
8227 const wxArrayString& lines,
8228 long *width, long *height ) const
8229 {
8230 wxCoord w = 0;
8231 wxCoord h = 0;
8232 wxCoord lineW = 0, lineH = 0;
8233
8234 size_t i;
8235 for ( i = 0; i < lines.GetCount(); i++ )
8236 {
8237 dc.GetTextExtent( lines[i], &lineW, &lineH );
8238 w = wxMax( w, lineW );
8239 h += lineH;
8240 }
8241
8242 *width = w;
8243 *height = h;
8244 }
8245
8246 //
8247 // ------ Batch processing.
8248 //
8249 void wxGrid::EndBatch()
8250 {
8251 if ( m_batchCount > 0 )
8252 {
8253 m_batchCount--;
8254 if ( !m_batchCount )
8255 {
8256 CalcDimensions();
8257 m_rowLabelWin->Refresh();
8258 m_colLabelWin->Refresh();
8259 m_cornerLabelWin->Refresh();
8260 m_gridWin->Refresh();
8261 }
8262 }
8263 }
8264
8265 // Use this, rather than wxWindow::Refresh(), to force an immediate
8266 // repainting of the grid. Has no effect if you are already inside a
8267 // BeginBatch / EndBatch block.
8268 //
8269 void wxGrid::ForceRefresh()
8270 {
8271 BeginBatch();
8272 EndBatch();
8273 }
8274
8275 bool wxGrid::Enable(bool enable)
8276 {
8277 if ( !wxScrolledWindow::Enable(enable) )
8278 return false;
8279
8280 // redraw in the new state
8281 m_gridWin->Refresh();
8282
8283 return true;
8284 }
8285
8286 //
8287 // ------ Edit control functions
8288 //
8289
8290 void wxGrid::EnableEditing( bool edit )
8291 {
8292 // TODO: improve this ?
8293 //
8294 if ( edit != m_editable )
8295 {
8296 if (!edit)
8297 EnableCellEditControl(edit);
8298 m_editable = edit;
8299 }
8300 }
8301
8302 void wxGrid::EnableCellEditControl( bool enable )
8303 {
8304 if (! m_editable)
8305 return;
8306
8307 if ( enable != m_cellEditCtrlEnabled )
8308 {
8309 if ( enable )
8310 {
8311 if (SendEvent( wxEVT_GRID_EDITOR_SHOWN) <0)
8312 return;
8313
8314 // this should be checked by the caller!
8315 wxASSERT_MSG( CanEnableCellControl(), _T("can't enable editing for this cell!") );
8316
8317 // do it before ShowCellEditControl()
8318 m_cellEditCtrlEnabled = enable;
8319
8320 ShowCellEditControl();
8321 }
8322 else
8323 {
8324 //FIXME:add veto support
8325 SendEvent( wxEVT_GRID_EDITOR_HIDDEN );
8326
8327 HideCellEditControl();
8328 SaveEditControlValue();
8329
8330 // do it after HideCellEditControl()
8331 m_cellEditCtrlEnabled = enable;
8332 }
8333 }
8334 }
8335
8336 bool wxGrid::IsCurrentCellReadOnly() const
8337 {
8338 // const_cast
8339 wxGridCellAttr* attr = ((wxGrid *)this)->GetCellAttr(m_currentCellCoords);
8340 bool readonly = attr->IsReadOnly();
8341 attr->DecRef();
8342
8343 return readonly;
8344 }
8345
8346 bool wxGrid::CanEnableCellControl() const
8347 {
8348 return m_editable && (m_currentCellCoords != wxGridNoCellCoords) &&
8349 !IsCurrentCellReadOnly();
8350 }
8351
8352 bool wxGrid::IsCellEditControlEnabled() const
8353 {
8354 // the cell edit control might be disable for all cells or just for the
8355 // current one if it's read only
8356 return m_cellEditCtrlEnabled ? !IsCurrentCellReadOnly() : false;
8357 }
8358
8359 bool wxGrid::IsCellEditControlShown() const
8360 {
8361 bool isShown = false;
8362
8363 if ( m_cellEditCtrlEnabled )
8364 {
8365 int row = m_currentCellCoords.GetRow();
8366 int col = m_currentCellCoords.GetCol();
8367 wxGridCellAttr* attr = GetCellAttr(row, col);
8368 wxGridCellEditor* editor = attr->GetEditor((wxGrid*) this, row, col);
8369 attr->DecRef();
8370
8371 if ( editor )
8372 {
8373 if ( editor->IsCreated() )
8374 {
8375 isShown = editor->GetControl()->IsShown();
8376 }
8377
8378 editor->DecRef();
8379 }
8380 }
8381
8382 return isShown;
8383 }
8384
8385 void wxGrid::ShowCellEditControl()
8386 {
8387 if ( IsCellEditControlEnabled() )
8388 {
8389 if ( !IsVisible( m_currentCellCoords, false ) )
8390 {
8391 m_cellEditCtrlEnabled = false;
8392 return;
8393 }
8394 else
8395 {
8396 wxRect rect = CellToRect( m_currentCellCoords );
8397 int row = m_currentCellCoords.GetRow();
8398 int col = m_currentCellCoords.GetCol();
8399
8400 // if this is part of a multicell, find owner (topleft)
8401 int cell_rows, cell_cols;
8402 GetCellSize( row, col, &cell_rows, &cell_cols );
8403 if ( cell_rows <= 0 || cell_cols <= 0 )
8404 {
8405 row += cell_rows;
8406 col += cell_cols;
8407 m_currentCellCoords.SetRow( row );
8408 m_currentCellCoords.SetCol( col );
8409 }
8410
8411 // erase the highlight and the cell contents because the editor
8412 // might not cover the entire cell
8413 wxClientDC dc( m_gridWin );
8414 PrepareDC( dc );
8415 wxGridCellAttr* attr = GetCellAttr(row, col);
8416 dc.SetBrush(wxBrush(attr->GetBackgroundColour(), wxBRUSHSTYLE_SOLID));
8417 dc.SetPen(*wxTRANSPARENT_PEN);
8418 dc.DrawRectangle(rect);
8419
8420 // convert to scrolled coords
8421 CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
8422
8423 int nXMove = 0;
8424 if (rect.x < 0)
8425 nXMove = rect.x;
8426
8427 // cell is shifted by one pixel
8428 // However, don't allow x or y to become negative
8429 // since the SetSize() method interprets that as
8430 // "don't change."
8431 if (rect.x > 0)
8432 rect.x--;
8433 if (rect.y > 0)
8434 rect.y--;
8435
8436 wxGridCellEditor* editor = attr->GetEditor(this, row, col);
8437 if ( !editor->IsCreated() )
8438 {
8439 editor->Create(m_gridWin, wxID_ANY,
8440 new wxGridCellEditorEvtHandler(this, editor));
8441
8442 wxGridEditorCreatedEvent evt(GetId(),
8443 wxEVT_GRID_EDITOR_CREATED,
8444 this,
8445 row,
8446 col,
8447 editor->GetControl());
8448 GetEventHandler()->ProcessEvent(evt);
8449 }
8450
8451 // resize editor to overflow into righthand cells if allowed
8452 int maxWidth = rect.width;
8453 wxString value = GetCellValue(row, col);
8454 if ( (value != wxEmptyString) && (attr->GetOverflow()) )
8455 {
8456 int y;
8457 GetTextExtent(value, &maxWidth, &y, NULL, NULL, &attr->GetFont());
8458 if (maxWidth < rect.width)
8459 maxWidth = rect.width;
8460 }
8461
8462 int client_right = m_gridWin->GetClientSize().GetWidth();
8463 if (rect.x + maxWidth > client_right)
8464 maxWidth = client_right - rect.x;
8465
8466 if ((maxWidth > rect.width) && (col < m_numCols) && m_table)
8467 {
8468 GetCellSize( row, col, &cell_rows, &cell_cols );
8469 // may have changed earlier
8470 for (int i = col + cell_cols; i < m_numCols; i++)
8471 {
8472 int c_rows, c_cols;
8473 GetCellSize( row, i, &c_rows, &c_cols );
8474
8475 // looks weird going over a multicell
8476 if (m_table->IsEmptyCell( row, i ) &&
8477 (rect.width < maxWidth) && (c_rows == 1))
8478 {
8479 rect.width += GetColWidth( i );
8480 }
8481 else
8482 break;
8483 }
8484
8485 if (rect.GetRight() > client_right)
8486 rect.SetRight( client_right - 1 );
8487 }
8488
8489 editor->SetCellAttr( attr );
8490 editor->SetSize( rect );
8491 if (nXMove != 0)
8492 editor->GetControl()->Move(
8493 editor->GetControl()->GetPosition().x + nXMove,
8494 editor->GetControl()->GetPosition().y );
8495 editor->Show( true, attr );
8496
8497 // recalc dimensions in case we need to
8498 // expand the scrolled window to account for editor
8499 CalcDimensions();
8500
8501 editor->BeginEdit(row, col, this);
8502 editor->SetCellAttr(NULL);
8503
8504 editor->DecRef();
8505 attr->DecRef();
8506 }
8507 }
8508 }
8509
8510 void wxGrid::HideCellEditControl()
8511 {
8512 if ( IsCellEditControlEnabled() )
8513 {
8514 int row = m_currentCellCoords.GetRow();
8515 int col = m_currentCellCoords.GetCol();
8516
8517 wxGridCellAttr *attr = GetCellAttr(row, col);
8518 wxGridCellEditor *editor = attr->GetEditor(this, row, col);
8519 const bool editorHadFocus = editor->GetControl()->HasFocus();
8520 editor->Show( false );
8521 editor->DecRef();
8522 attr->DecRef();
8523
8524 // return the focus to the grid itself if the editor had it
8525 //
8526 // note that we must not do this unconditionally to avoid stealing
8527 // focus from the window which just received it if we are hiding the
8528 // editor precisely because we lost focus
8529 if ( editorHadFocus )
8530 m_gridWin->SetFocus();
8531
8532 // refresh whole row to the right
8533 wxRect rect( CellToRect(row, col) );
8534 CalcScrolledPosition(rect.x, rect.y, &rect.x, &rect.y );
8535 rect.width = m_gridWin->GetClientSize().GetWidth() - rect.x;
8536
8537 #ifdef __WXMAC__
8538 // ensure that the pixels under the focus ring get refreshed as well
8539 rect.Inflate(10, 10);
8540 #endif
8541
8542 m_gridWin->Refresh( false, &rect );
8543 }
8544 }
8545
8546 void wxGrid::SaveEditControlValue()
8547 {
8548 if ( IsCellEditControlEnabled() )
8549 {
8550 int row = m_currentCellCoords.GetRow();
8551 int col = m_currentCellCoords.GetCol();
8552
8553 wxString oldval = GetCellValue(row, col);
8554
8555 wxGridCellAttr* attr = GetCellAttr(row, col);
8556 wxGridCellEditor* editor = attr->GetEditor(this, row, col);
8557 bool changed = editor->EndEdit(row, col, this);
8558
8559 editor->DecRef();
8560 attr->DecRef();
8561
8562 if (changed)
8563 {
8564 if ( SendEvent( wxEVT_GRID_CELL_CHANGE,
8565 m_currentCellCoords.GetRow(),
8566 m_currentCellCoords.GetCol() ) < 0 )
8567 {
8568 // Event has been vetoed, set the data back.
8569 SetCellValue(row, col, oldval);
8570 }
8571 }
8572 }
8573 }
8574
8575 //
8576 // ------ Grid location functions
8577 // Note that all of these functions work with the logical coordinates of
8578 // grid cells and labels so you will need to convert from device
8579 // coordinates for mouse events etc.
8580 //
8581
8582 void wxGrid::XYToCell( int x, int y, wxGridCellCoords& coords ) const
8583 {
8584 int row = YToRow(y);
8585 int col = XToCol(x);
8586
8587 if ( row == -1 || col == -1 )
8588 {
8589 coords = wxGridNoCellCoords;
8590 }
8591 else
8592 {
8593 coords.Set( row, col );
8594 }
8595 }
8596
8597 // Internal Helper function for computing row or column from some
8598 // (unscrolled) coordinate value, using either
8599 // m_defaultRowHeight/m_defaultColWidth or binary search on array
8600 // of m_rowBottoms/m_ColRights to speed up the search!
8601
8602 static int CoordToRowOrCol(int coord, int defaultDist, int minDist,
8603 const wxArrayInt& BorderArray, int nMax,
8604 bool clipToMinMax)
8605 {
8606 if (coord < 0)
8607 return clipToMinMax && (nMax > 0) ? 0 : -1;
8608
8609 if (!defaultDist)
8610 defaultDist = 1;
8611
8612 size_t i_max = coord / defaultDist,
8613 i_min = 0;
8614
8615 if (BorderArray.IsEmpty())
8616 {
8617 if ((int) i_max < nMax)
8618 return i_max;
8619 return clipToMinMax ? nMax - 1 : -1;
8620 }
8621
8622 if ( i_max >= BorderArray.GetCount())
8623 {
8624 i_max = BorderArray.GetCount() - 1;
8625 }
8626 else
8627 {
8628 if ( coord >= BorderArray[i_max])
8629 {
8630 i_min = i_max;
8631 if (minDist)
8632 i_max = coord / minDist;
8633 else
8634 i_max = BorderArray.GetCount() - 1;
8635 }
8636
8637 if ( i_max >= BorderArray.GetCount())
8638 i_max = BorderArray.GetCount() - 1;
8639 }
8640
8641 if ( coord >= BorderArray[i_max])
8642 return clipToMinMax ? (int)i_max : -1;
8643 if ( coord < BorderArray[0] )
8644 return 0;
8645
8646 while ( i_max - i_min > 0 )
8647 {
8648 wxCHECK_MSG(BorderArray[i_min] <= coord && coord < BorderArray[i_max],
8649 0, _T("wxGrid: internal error in CoordToRowOrCol"));
8650 if (coord >= BorderArray[ i_max - 1])
8651 return i_max;
8652 else
8653 i_max--;
8654 int median = i_min + (i_max - i_min + 1) / 2;
8655 if (coord < BorderArray[median])
8656 i_max = median;
8657 else
8658 i_min = median;
8659 }
8660
8661 return i_max;
8662 }
8663
8664 int wxGrid::YToRow( int y ) const
8665 {
8666 return CoordToRowOrCol(y, m_defaultRowHeight,
8667 m_minAcceptableRowHeight, m_rowBottoms, m_numRows, false);
8668 }
8669
8670 int wxGrid::XToCol( int x, bool clipToMinMax ) const
8671 {
8672 if (x < 0)
8673 return clipToMinMax && (m_numCols > 0) ? GetColAt( 0 ) : -1;
8674
8675 wxASSERT_MSG(m_defaultColWidth > 0, wxT("Default column width can not be zero"));
8676
8677 int maxPos = x / m_defaultColWidth;
8678 int minPos = 0;
8679
8680 if (m_colRights.IsEmpty())
8681 {
8682 if(maxPos < m_numCols)
8683 return GetColAt( maxPos );
8684 return clipToMinMax ? GetColAt( m_numCols - 1 ) : -1;
8685 }
8686
8687 if ( maxPos >= m_numCols)
8688 maxPos = m_numCols - 1;
8689 else
8690 {
8691 if ( x >= m_colRights[GetColAt( maxPos )])
8692 {
8693 minPos = maxPos;
8694 if (m_minAcceptableColWidth)
8695 maxPos = x / m_minAcceptableColWidth;
8696 else
8697 maxPos = m_numCols - 1;
8698 }
8699 if ( maxPos >= m_numCols)
8700 maxPos = m_numCols - 1;
8701 }
8702
8703 //X is beyond the last column
8704 if ( x >= m_colRights[GetColAt( maxPos )])
8705 return clipToMinMax ? GetColAt( maxPos ) : -1;
8706
8707 //X is before the first column
8708 if ( x < m_colRights[GetColAt( 0 )] )
8709 return GetColAt( 0 );
8710
8711 //Perform a binary search
8712 while ( maxPos - minPos > 0 )
8713 {
8714 wxCHECK_MSG(m_colRights[GetColAt( minPos )] <= x && x < m_colRights[GetColAt( maxPos )],
8715 0, _T("wxGrid: internal error in XToCol"));
8716
8717 if (x >= m_colRights[GetColAt( maxPos - 1 )])
8718 return GetColAt( maxPos );
8719 else
8720 maxPos--;
8721 int median = minPos + (maxPos - minPos + 1) / 2;
8722 if (x < m_colRights[GetColAt( median )])
8723 maxPos = median;
8724 else
8725 minPos = median;
8726 }
8727 return GetColAt( maxPos );
8728 }
8729
8730 // return the row number that that the y coord is near
8731 // the edge of, or -1 if not near an edge.
8732 // coords can only possibly be near an edge if
8733 // (a) the row/column is large enough to still allow for an "inner" area
8734 // that is _not_ nead the edge (i.e., if the height/width is smaller
8735 // than WXGRID_LABEL_EDGE_ZONE, coords are _never_ considered to be
8736 // near the edge).
8737 // and
8738 // (b) resizing rows/columns (the thing for which edge detection is
8739 // relevant at all) is enabled.
8740 //
8741 int wxGrid::YToEdgeOfRow( int y ) const
8742 {
8743 int i;
8744 i = internalYToRow(y);
8745
8746 if ( GetRowHeight(i) > WXGRID_LABEL_EDGE_ZONE && CanDragRowSize() )
8747 {
8748 // We know that we are in row i, test whether we are
8749 // close enough to lower or upper border, respectively.
8750 if ( abs(GetRowBottom(i) - y) < WXGRID_LABEL_EDGE_ZONE )
8751 return i;
8752 else if ( i > 0 && y - GetRowTop(i) < WXGRID_LABEL_EDGE_ZONE )
8753 return i - 1;
8754 }
8755
8756 return -1;
8757 }
8758
8759 // return the col number that that the x coord is near the edge of, or
8760 // -1 if not near an edge
8761 // See comment at YToEdgeOfRow for conditions on edge detection.
8762 //
8763 int wxGrid::XToEdgeOfCol( int x ) const
8764 {
8765 int i;
8766 i = internalXToCol(x);
8767
8768 if ( GetColWidth(i) > WXGRID_LABEL_EDGE_ZONE && CanDragColSize() )
8769 {
8770 // We know that we are in column i; test whether we are
8771 // close enough to right or left border, respectively.
8772 if ( abs(GetColRight(i) - x) < WXGRID_LABEL_EDGE_ZONE )
8773 return i;
8774 else if ( i > 0 && x - GetColLeft(i) < WXGRID_LABEL_EDGE_ZONE )
8775 return i - 1;
8776 }
8777
8778 return -1;
8779 }
8780
8781 wxRect wxGrid::CellToRect( int row, int col ) const
8782 {
8783 wxRect rect( -1, -1, -1, -1 );
8784
8785 if ( row >= 0 && row < m_numRows &&
8786 col >= 0 && col < m_numCols )
8787 {
8788 int i, cell_rows, cell_cols;
8789 rect.width = rect.height = 0;
8790 GetCellSize( row, col, &cell_rows, &cell_cols );
8791 // if negative then find multicell owner
8792 if (cell_rows < 0)
8793 row += cell_rows;
8794 if (cell_cols < 0)
8795 col += cell_cols;
8796 GetCellSize( row, col, &cell_rows, &cell_cols );
8797
8798 rect.x = GetColLeft(col);
8799 rect.y = GetRowTop(row);
8800 for (i=col; i < col + cell_cols; i++)
8801 rect.width += GetColWidth(i);
8802 for (i=row; i < row + cell_rows; i++)
8803 rect.height += GetRowHeight(i);
8804 }
8805
8806 // if grid lines are enabled, then the area of the cell is a bit smaller
8807 if (m_gridLinesEnabled)
8808 {
8809 rect.width -= 1;
8810 rect.height -= 1;
8811 }
8812
8813 return rect;
8814 }
8815
8816 bool wxGrid::IsVisible( int row, int col, bool wholeCellVisible ) const
8817 {
8818 // get the cell rectangle in logical coords
8819 //
8820 wxRect r( CellToRect( row, col ) );
8821
8822 // convert to device coords
8823 //
8824 int left, top, right, bottom;
8825 CalcScrolledPosition( r.GetLeft(), r.GetTop(), &left, &top );
8826 CalcScrolledPosition( r.GetRight(), r.GetBottom(), &right, &bottom );
8827
8828 // check against the client area of the grid window
8829 int cw, ch;
8830 m_gridWin->GetClientSize( &cw, &ch );
8831
8832 if ( wholeCellVisible )
8833 {
8834 // is the cell wholly visible ?
8835 return ( left >= 0 && right <= cw &&
8836 top >= 0 && bottom <= ch );
8837 }
8838 else
8839 {
8840 // is the cell partly visible ?
8841 //
8842 return ( ((left >= 0 && left < cw) || (right > 0 && right <= cw)) &&
8843 ((top >= 0 && top < ch) || (bottom > 0 && bottom <= ch)) );
8844 }
8845 }
8846
8847 // make the specified cell location visible by doing a minimal amount
8848 // of scrolling
8849 //
8850 void wxGrid::MakeCellVisible( int row, int col )
8851 {
8852 int i;
8853 int xpos = -1, ypos = -1;
8854
8855 if ( row >= 0 && row < m_numRows &&
8856 col >= 0 && col < m_numCols )
8857 {
8858 // get the cell rectangle in logical coords
8859 wxRect r( CellToRect( row, col ) );
8860
8861 // convert to device coords
8862 int left, top, right, bottom;
8863 CalcScrolledPosition( r.GetLeft(), r.GetTop(), &left, &top );
8864 CalcScrolledPosition( r.GetRight(), r.GetBottom(), &right, &bottom );
8865
8866 int cw, ch;
8867 m_gridWin->GetClientSize( &cw, &ch );
8868
8869 if ( top < 0 )
8870 {
8871 ypos = r.GetTop();
8872 }
8873 else if ( bottom > ch )
8874 {
8875 int h = r.GetHeight();
8876 ypos = r.GetTop();
8877 for ( i = row - 1; i >= 0; i-- )
8878 {
8879 int rowHeight = GetRowHeight(i);
8880 if ( h + rowHeight > ch )
8881 break;
8882
8883 h += rowHeight;
8884 ypos -= rowHeight;
8885 }
8886
8887 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
8888 // have rounding errors (this is important, because if we do,
8889 // we might not scroll at all and some cells won't be redrawn)
8890 //
8891 // Sometimes GRID_SCROLL_LINE / 2 is not enough,
8892 // so just add a full scroll unit...
8893 ypos += m_scrollLineY;
8894 }
8895
8896 // special handling for wide cells - show always left part of the cell!
8897 // Otherwise, e.g. when stepping from row to row, it would jump between
8898 // left and right part of the cell on every step!
8899 // if ( left < 0 )
8900 if ( left < 0 || (right - left) >= cw )
8901 {
8902 xpos = r.GetLeft();
8903 }
8904 else if ( right > cw )
8905 {
8906 // position the view so that the cell is on the right
8907 int x0, y0;
8908 CalcUnscrolledPosition(0, 0, &x0, &y0);
8909 xpos = x0 + (right - cw);
8910
8911 // see comment for ypos above
8912 xpos += m_scrollLineX;
8913 }
8914
8915 if ( xpos != -1 || ypos != -1 )
8916 {
8917 if ( xpos != -1 )
8918 xpos /= m_scrollLineX;
8919 if ( ypos != -1 )
8920 ypos /= m_scrollLineY;
8921 Scroll( xpos, ypos );
8922 AdjustScrollbars();
8923 }
8924 }
8925 }
8926
8927 //
8928 // ------ Grid cursor movement functions
8929 //
8930
8931 bool wxGrid::MoveCursorUp( bool expandSelection )
8932 {
8933 if ( m_currentCellCoords != wxGridNoCellCoords &&
8934 m_currentCellCoords.GetRow() >= 0 )
8935 {
8936 if ( expandSelection )
8937 {
8938 if ( m_selectingKeyboard == wxGridNoCellCoords )
8939 m_selectingKeyboard = m_currentCellCoords;
8940 if ( m_selectingKeyboard.GetRow() > 0 )
8941 {
8942 m_selectingKeyboard.SetRow( m_selectingKeyboard.GetRow() - 1 );
8943 MakeCellVisible( m_selectingKeyboard.GetRow(),
8944 m_selectingKeyboard.GetCol() );
8945 HighlightBlock( m_currentCellCoords, m_selectingKeyboard );
8946 }
8947 }
8948 else if ( m_currentCellCoords.GetRow() > 0 )
8949 {
8950 int row = m_currentCellCoords.GetRow() - 1;
8951 int col = m_currentCellCoords.GetCol();
8952 ClearSelection();
8953 MakeCellVisible( row, col );
8954 SetCurrentCell( row, col );
8955 }
8956 else
8957 return false;
8958
8959 return true;
8960 }
8961
8962 return false;
8963 }
8964
8965 bool wxGrid::MoveCursorDown( bool expandSelection )
8966 {
8967 if ( m_currentCellCoords != wxGridNoCellCoords &&
8968 m_currentCellCoords.GetRow() < m_numRows )
8969 {
8970 if ( expandSelection )
8971 {
8972 if ( m_selectingKeyboard == wxGridNoCellCoords )
8973 m_selectingKeyboard = m_currentCellCoords;
8974 if ( m_selectingKeyboard.GetRow() < m_numRows - 1 )
8975 {
8976 m_selectingKeyboard.SetRow( m_selectingKeyboard.GetRow() + 1 );
8977 MakeCellVisible( m_selectingKeyboard.GetRow(),
8978 m_selectingKeyboard.GetCol() );
8979 HighlightBlock( m_currentCellCoords, m_selectingKeyboard );
8980 }
8981 }
8982 else if ( m_currentCellCoords.GetRow() < m_numRows - 1 )
8983 {
8984 int row = m_currentCellCoords.GetRow() + 1;
8985 int col = m_currentCellCoords.GetCol();
8986 ClearSelection();
8987 MakeCellVisible( row, col );
8988 SetCurrentCell( row, col );
8989 }
8990 else
8991 return false;
8992
8993 return true;
8994 }
8995
8996 return false;
8997 }
8998
8999 bool wxGrid::MoveCursorLeft( bool expandSelection )
9000 {
9001 if ( m_currentCellCoords != wxGridNoCellCoords &&
9002 m_currentCellCoords.GetCol() >= 0 )
9003 {
9004 if ( expandSelection )
9005 {
9006 if ( m_selectingKeyboard == wxGridNoCellCoords )
9007 m_selectingKeyboard = m_currentCellCoords;
9008 if ( m_selectingKeyboard.GetCol() > 0 )
9009 {
9010 m_selectingKeyboard.SetCol( m_selectingKeyboard.GetCol() - 1 );
9011 MakeCellVisible( m_selectingKeyboard.GetRow(),
9012 m_selectingKeyboard.GetCol() );
9013 HighlightBlock( m_currentCellCoords, m_selectingKeyboard );
9014 }
9015 }
9016 else if ( GetColPos( m_currentCellCoords.GetCol() ) > 0 )
9017 {
9018 int row = m_currentCellCoords.GetRow();
9019 int col = GetColAt( GetColPos( m_currentCellCoords.GetCol() ) - 1 );
9020 ClearSelection();
9021
9022 MakeCellVisible( row, col );
9023 SetCurrentCell( row, col );
9024 }
9025 else
9026 return false;
9027
9028 return true;
9029 }
9030
9031 return false;
9032 }
9033
9034 bool wxGrid::MoveCursorRight( bool expandSelection )
9035 {
9036 if ( m_currentCellCoords != wxGridNoCellCoords &&
9037 m_currentCellCoords.GetCol() < m_numCols )
9038 {
9039 if ( expandSelection )
9040 {
9041 if ( m_selectingKeyboard == wxGridNoCellCoords )
9042 m_selectingKeyboard = m_currentCellCoords;
9043 if ( m_selectingKeyboard.GetCol() < m_numCols - 1 )
9044 {
9045 m_selectingKeyboard.SetCol( m_selectingKeyboard.GetCol() + 1 );
9046 MakeCellVisible( m_selectingKeyboard.GetRow(),
9047 m_selectingKeyboard.GetCol() );
9048 HighlightBlock( m_currentCellCoords, m_selectingKeyboard );
9049 }
9050 }
9051 else if ( GetColPos( m_currentCellCoords.GetCol() ) < m_numCols - 1 )
9052 {
9053 int row = m_currentCellCoords.GetRow();
9054 int col = GetColAt( GetColPos( m_currentCellCoords.GetCol() ) + 1 );
9055 ClearSelection();
9056
9057 MakeCellVisible( row, col );
9058 SetCurrentCell( row, col );
9059 }
9060 else
9061 return false;
9062
9063 return true;
9064 }
9065
9066 return false;
9067 }
9068
9069 bool wxGrid::MovePageUp()
9070 {
9071 if ( m_currentCellCoords == wxGridNoCellCoords )
9072 return false;
9073
9074 int row = m_currentCellCoords.GetRow();
9075 if ( row > 0 )
9076 {
9077 int cw, ch;
9078 m_gridWin->GetClientSize( &cw, &ch );
9079
9080 int y = GetRowTop(row);
9081 int newRow = internalYToRow( y - ch + 1 );
9082
9083 if ( newRow == row )
9084 {
9085 // row > 0, so newRow can never be less than 0 here.
9086 newRow = row - 1;
9087 }
9088
9089 MakeCellVisible( newRow, m_currentCellCoords.GetCol() );
9090 SetCurrentCell( newRow, m_currentCellCoords.GetCol() );
9091
9092 return true;
9093 }
9094
9095 return false;
9096 }
9097
9098 bool wxGrid::MovePageDown()
9099 {
9100 if ( m_currentCellCoords == wxGridNoCellCoords )
9101 return false;
9102
9103 int row = m_currentCellCoords.GetRow();
9104 if ( (row + 1) < m_numRows )
9105 {
9106 int cw, ch;
9107 m_gridWin->GetClientSize( &cw, &ch );
9108
9109 int y = GetRowTop(row);
9110 int newRow = internalYToRow( y + ch );
9111 if ( newRow == row )
9112 {
9113 // row < m_numRows, so newRow can't overflow here.
9114 newRow = row + 1;
9115 }
9116
9117 MakeCellVisible( newRow, m_currentCellCoords.GetCol() );
9118 SetCurrentCell( newRow, m_currentCellCoords.GetCol() );
9119
9120 return true;
9121 }
9122
9123 return false;
9124 }
9125
9126 bool wxGrid::MoveCursorUpBlock( bool expandSelection )
9127 {
9128 if ( m_table &&
9129 m_currentCellCoords != wxGridNoCellCoords &&
9130 m_currentCellCoords.GetRow() > 0 )
9131 {
9132 int row = m_currentCellCoords.GetRow();
9133 int col = m_currentCellCoords.GetCol();
9134
9135 if ( m_table->IsEmptyCell(row, col) )
9136 {
9137 // starting in an empty cell: find the next block of
9138 // non-empty cells
9139 //
9140 while ( row > 0 )
9141 {
9142 row--;
9143 if ( !(m_table->IsEmptyCell(row, col)) )
9144 break;
9145 }
9146 }
9147 else if ( m_table->IsEmptyCell(row - 1, col) )
9148 {
9149 // starting at the top of a block: find the next block
9150 //
9151 row--;
9152 while ( row > 0 )
9153 {
9154 row--;
9155 if ( !(m_table->IsEmptyCell(row, col)) )
9156 break;
9157 }
9158 }
9159 else
9160 {
9161 // starting within a block: find the top of the block
9162 //
9163 while ( row > 0 )
9164 {
9165 row--;
9166 if ( m_table->IsEmptyCell(row, col) )
9167 {
9168 row++;
9169 break;
9170 }
9171 }
9172 }
9173
9174 MakeCellVisible( row, col );
9175 if ( expandSelection )
9176 {
9177 m_selectingKeyboard = wxGridCellCoords( row, col );
9178 HighlightBlock( m_currentCellCoords, m_selectingKeyboard );
9179 }
9180 else
9181 {
9182 ClearSelection();
9183 SetCurrentCell( row, col );
9184 }
9185
9186 return true;
9187 }
9188
9189 return false;
9190 }
9191
9192 bool wxGrid::MoveCursorDownBlock( bool expandSelection )
9193 {
9194 if ( m_table &&
9195 m_currentCellCoords != wxGridNoCellCoords &&
9196 m_currentCellCoords.GetRow() < m_numRows - 1 )
9197 {
9198 int row = m_currentCellCoords.GetRow();
9199 int col = m_currentCellCoords.GetCol();
9200
9201 if ( m_table->IsEmptyCell(row, col) )
9202 {
9203 // starting in an empty cell: find the next block of
9204 // non-empty cells
9205 //
9206 while ( row < m_numRows - 1 )
9207 {
9208 row++;
9209 if ( !(m_table->IsEmptyCell(row, col)) )
9210 break;
9211 }
9212 }
9213 else if ( m_table->IsEmptyCell(row + 1, col) )
9214 {
9215 // starting at the bottom of a block: find the next block
9216 //
9217 row++;
9218 while ( row < m_numRows - 1 )
9219 {
9220 row++;
9221 if ( !(m_table->IsEmptyCell(row, col)) )
9222 break;
9223 }
9224 }
9225 else
9226 {
9227 // starting within a block: find the bottom of the block
9228 //
9229 while ( row < m_numRows - 1 )
9230 {
9231 row++;
9232 if ( m_table->IsEmptyCell(row, col) )
9233 {
9234 row--;
9235 break;
9236 }
9237 }
9238 }
9239
9240 MakeCellVisible( row, col );
9241 if ( expandSelection )
9242 {
9243 m_selectingKeyboard = wxGridCellCoords( row, col );
9244 HighlightBlock( m_currentCellCoords, m_selectingKeyboard );
9245 }
9246 else
9247 {
9248 ClearSelection();
9249 SetCurrentCell( row, col );
9250 }
9251
9252 return true;
9253 }
9254
9255 return false;
9256 }
9257
9258 bool wxGrid::MoveCursorLeftBlock( bool expandSelection )
9259 {
9260 if ( m_table &&
9261 m_currentCellCoords != wxGridNoCellCoords &&
9262 m_currentCellCoords.GetCol() > 0 )
9263 {
9264 int row = m_currentCellCoords.GetRow();
9265 int col = m_currentCellCoords.GetCol();
9266
9267 if ( m_table->IsEmptyCell(row, col) )
9268 {
9269 // starting in an empty cell: find the next block of
9270 // non-empty cells
9271 //
9272 while ( col > 0 )
9273 {
9274 col--;
9275 if ( !(m_table->IsEmptyCell(row, col)) )
9276 break;
9277 }
9278 }
9279 else if ( m_table->IsEmptyCell(row, col - 1) )
9280 {
9281 // starting at the left of a block: find the next block
9282 //
9283 col--;
9284 while ( col > 0 )
9285 {
9286 col--;
9287 if ( !(m_table->IsEmptyCell(row, col)) )
9288 break;
9289 }
9290 }
9291 else
9292 {
9293 // starting within a block: find the left of the block
9294 //
9295 while ( col > 0 )
9296 {
9297 col--;
9298 if ( m_table->IsEmptyCell(row, col) )
9299 {
9300 col++;
9301 break;
9302 }
9303 }
9304 }
9305
9306 MakeCellVisible( row, col );
9307 if ( expandSelection )
9308 {
9309 m_selectingKeyboard = wxGridCellCoords( row, col );
9310 HighlightBlock( m_currentCellCoords, m_selectingKeyboard );
9311 }
9312 else
9313 {
9314 ClearSelection();
9315 SetCurrentCell( row, col );
9316 }
9317
9318 return true;
9319 }
9320
9321 return false;
9322 }
9323
9324 bool wxGrid::MoveCursorRightBlock( bool expandSelection )
9325 {
9326 if ( m_table &&
9327 m_currentCellCoords != wxGridNoCellCoords &&
9328 m_currentCellCoords.GetCol() < m_numCols - 1 )
9329 {
9330 int row = m_currentCellCoords.GetRow();
9331 int col = m_currentCellCoords.GetCol();
9332
9333 if ( m_table->IsEmptyCell(row, col) )
9334 {
9335 // starting in an empty cell: find the next block of
9336 // non-empty cells
9337 //
9338 while ( col < m_numCols - 1 )
9339 {
9340 col++;
9341 if ( !(m_table->IsEmptyCell(row, col)) )
9342 break;
9343 }
9344 }
9345 else if ( m_table->IsEmptyCell(row, col + 1) )
9346 {
9347 // starting at the right of a block: find the next block
9348 //
9349 col++;
9350 while ( col < m_numCols - 1 )
9351 {
9352 col++;
9353 if ( !(m_table->IsEmptyCell(row, col)) )
9354 break;
9355 }
9356 }
9357 else
9358 {
9359 // starting within a block: find the right of the block
9360 //
9361 while ( col < m_numCols - 1 )
9362 {
9363 col++;
9364 if ( m_table->IsEmptyCell(row, col) )
9365 {
9366 col--;
9367 break;
9368 }
9369 }
9370 }
9371
9372 MakeCellVisible( row, col );
9373 if ( expandSelection )
9374 {
9375 m_selectingKeyboard = wxGridCellCoords( row, col );
9376 HighlightBlock( m_currentCellCoords, m_selectingKeyboard );
9377 }
9378 else
9379 {
9380 ClearSelection();
9381 SetCurrentCell( row, col );
9382 }
9383
9384 return true;
9385 }
9386
9387 return false;
9388 }
9389
9390 //
9391 // ------ Label values and formatting
9392 //
9393
9394 void wxGrid::GetRowLabelAlignment( int *horiz, int *vert ) const
9395 {
9396 if ( horiz )
9397 *horiz = m_rowLabelHorizAlign;
9398 if ( vert )
9399 *vert = m_rowLabelVertAlign;
9400 }
9401
9402 void wxGrid::GetColLabelAlignment( int *horiz, int *vert ) const
9403 {
9404 if ( horiz )
9405 *horiz = m_colLabelHorizAlign;
9406 if ( vert )
9407 *vert = m_colLabelVertAlign;
9408 }
9409
9410 int wxGrid::GetColLabelTextOrientation() const
9411 {
9412 return m_colLabelTextOrientation;
9413 }
9414
9415 wxString wxGrid::GetRowLabelValue( int row ) const
9416 {
9417 if ( m_table )
9418 {
9419 return m_table->GetRowLabelValue( row );
9420 }
9421 else
9422 {
9423 wxString s;
9424 s << row;
9425 return s;
9426 }
9427 }
9428
9429 wxString wxGrid::GetColLabelValue( int col ) const
9430 {
9431 if ( m_table )
9432 {
9433 return m_table->GetColLabelValue( col );
9434 }
9435 else
9436 {
9437 wxString s;
9438 s << col;
9439 return s;
9440 }
9441 }
9442
9443 void wxGrid::SetRowLabelSize( int width )
9444 {
9445 wxASSERT( width >= 0 || width == wxGRID_AUTOSIZE );
9446
9447 if ( width == wxGRID_AUTOSIZE )
9448 {
9449 width = CalcColOrRowLabelAreaMinSize(wxGRID_ROW);
9450 }
9451
9452 if ( width != m_rowLabelWidth )
9453 {
9454 if ( width == 0 )
9455 {
9456 m_rowLabelWin->Show( false );
9457 m_cornerLabelWin->Show( false );
9458 }
9459 else if ( m_rowLabelWidth == 0 )
9460 {
9461 m_rowLabelWin->Show( true );
9462 if ( m_colLabelHeight > 0 )
9463 m_cornerLabelWin->Show( true );
9464 }
9465
9466 m_rowLabelWidth = width;
9467 CalcWindowSizes();
9468 wxScrolledWindow::Refresh( true );
9469 }
9470 }
9471
9472 void wxGrid::SetColLabelSize( int height )
9473 {
9474 wxASSERT( height >=0 || height == wxGRID_AUTOSIZE );
9475
9476 if ( height == wxGRID_AUTOSIZE )
9477 {
9478 height = CalcColOrRowLabelAreaMinSize(wxGRID_COLUMN);
9479 }
9480
9481 if ( height != m_colLabelHeight )
9482 {
9483 if ( height == 0 )
9484 {
9485 m_colLabelWin->Show( false );
9486 m_cornerLabelWin->Show( false );
9487 }
9488 else if ( m_colLabelHeight == 0 )
9489 {
9490 m_colLabelWin->Show( true );
9491 if ( m_rowLabelWidth > 0 )
9492 m_cornerLabelWin->Show( true );
9493 }
9494
9495 m_colLabelHeight = height;
9496 CalcWindowSizes();
9497 wxScrolledWindow::Refresh( true );
9498 }
9499 }
9500
9501 void wxGrid::SetLabelBackgroundColour( const wxColour& colour )
9502 {
9503 if ( m_labelBackgroundColour != colour )
9504 {
9505 m_labelBackgroundColour = colour;
9506 m_rowLabelWin->SetBackgroundColour( colour );
9507 m_colLabelWin->SetBackgroundColour( colour );
9508 m_cornerLabelWin->SetBackgroundColour( colour );
9509
9510 if ( !GetBatchCount() )
9511 {
9512 m_rowLabelWin->Refresh();
9513 m_colLabelWin->Refresh();
9514 m_cornerLabelWin->Refresh();
9515 }
9516 }
9517 }
9518
9519 void wxGrid::SetLabelTextColour( const wxColour& colour )
9520 {
9521 if ( m_labelTextColour != colour )
9522 {
9523 m_labelTextColour = colour;
9524 if ( !GetBatchCount() )
9525 {
9526 m_rowLabelWin->Refresh();
9527 m_colLabelWin->Refresh();
9528 }
9529 }
9530 }
9531
9532 void wxGrid::SetLabelFont( const wxFont& font )
9533 {
9534 m_labelFont = font;
9535 if ( !GetBatchCount() )
9536 {
9537 m_rowLabelWin->Refresh();
9538 m_colLabelWin->Refresh();
9539 }
9540 }
9541
9542 void wxGrid::SetRowLabelAlignment( int horiz, int vert )
9543 {
9544 // allow old (incorrect) defs to be used
9545 switch ( horiz )
9546 {
9547 case wxLEFT: horiz = wxALIGN_LEFT; break;
9548 case wxRIGHT: horiz = wxALIGN_RIGHT; break;
9549 case wxCENTRE: horiz = wxALIGN_CENTRE; break;
9550 }
9551
9552 switch ( vert )
9553 {
9554 case wxTOP: vert = wxALIGN_TOP; break;
9555 case wxBOTTOM: vert = wxALIGN_BOTTOM; break;
9556 case wxCENTRE: vert = wxALIGN_CENTRE; break;
9557 }
9558
9559 if ( horiz == wxALIGN_LEFT || horiz == wxALIGN_CENTRE || horiz == wxALIGN_RIGHT )
9560 {
9561 m_rowLabelHorizAlign = horiz;
9562 }
9563
9564 if ( vert == wxALIGN_TOP || vert == wxALIGN_CENTRE || vert == wxALIGN_BOTTOM )
9565 {
9566 m_rowLabelVertAlign = vert;
9567 }
9568
9569 if ( !GetBatchCount() )
9570 {
9571 m_rowLabelWin->Refresh();
9572 }
9573 }
9574
9575 void wxGrid::SetColLabelAlignment( int horiz, int vert )
9576 {
9577 // allow old (incorrect) defs to be used
9578 switch ( horiz )
9579 {
9580 case wxLEFT: horiz = wxALIGN_LEFT; break;
9581 case wxRIGHT: horiz = wxALIGN_RIGHT; break;
9582 case wxCENTRE: horiz = wxALIGN_CENTRE; break;
9583 }
9584
9585 switch ( vert )
9586 {
9587 case wxTOP: vert = wxALIGN_TOP; break;
9588 case wxBOTTOM: vert = wxALIGN_BOTTOM; break;
9589 case wxCENTRE: vert = wxALIGN_CENTRE; break;
9590 }
9591
9592 if ( horiz == wxALIGN_LEFT || horiz == wxALIGN_CENTRE || horiz == wxALIGN_RIGHT )
9593 {
9594 m_colLabelHorizAlign = horiz;
9595 }
9596
9597 if ( vert == wxALIGN_TOP || vert == wxALIGN_CENTRE || vert == wxALIGN_BOTTOM )
9598 {
9599 m_colLabelVertAlign = vert;
9600 }
9601
9602 if ( !GetBatchCount() )
9603 {
9604 m_colLabelWin->Refresh();
9605 }
9606 }
9607
9608 // Note: under MSW, the default column label font must be changed because it
9609 // does not support vertical printing
9610 //
9611 // Example: wxFont font(9, wxSWISS, wxNORMAL, wxBOLD);
9612 // pGrid->SetLabelFont(font);
9613 // pGrid->SetColLabelTextOrientation(wxVERTICAL);
9614 //
9615 void wxGrid::SetColLabelTextOrientation( int textOrientation )
9616 {
9617 if ( textOrientation == wxHORIZONTAL || textOrientation == wxVERTICAL )
9618 m_colLabelTextOrientation = textOrientation;
9619
9620 if ( !GetBatchCount() )
9621 m_colLabelWin->Refresh();
9622 }
9623
9624 void wxGrid::SetRowLabelValue( int row, const wxString& s )
9625 {
9626 if ( m_table )
9627 {
9628 m_table->SetRowLabelValue( row, s );
9629 if ( !GetBatchCount() )
9630 {
9631 wxRect rect = CellToRect( row, 0 );
9632 if ( rect.height > 0 )
9633 {
9634 CalcScrolledPosition(0, rect.y, &rect.x, &rect.y);
9635 rect.x = 0;
9636 rect.width = m_rowLabelWidth;
9637 m_rowLabelWin->Refresh( true, &rect );
9638 }
9639 }
9640 }
9641 }
9642
9643 void wxGrid::SetColLabelValue( int col, const wxString& s )
9644 {
9645 if ( m_table )
9646 {
9647 m_table->SetColLabelValue( col, s );
9648 if ( !GetBatchCount() )
9649 {
9650 wxRect rect = CellToRect( 0, col );
9651 if ( rect.width > 0 )
9652 {
9653 CalcScrolledPosition(rect.x, 0, &rect.x, &rect.y);
9654 rect.y = 0;
9655 rect.height = m_colLabelHeight;
9656 m_colLabelWin->Refresh( true, &rect );
9657 }
9658 }
9659 }
9660 }
9661
9662 void wxGrid::SetGridLineColour( const wxColour& colour )
9663 {
9664 if ( m_gridLineColour != colour )
9665 {
9666 m_gridLineColour = colour;
9667
9668 wxClientDC dc( m_gridWin );
9669 PrepareDC( dc );
9670 DrawAllGridLines( dc, wxRegion() );
9671 }
9672 }
9673
9674 void wxGrid::SetCellHighlightColour( const wxColour& colour )
9675 {
9676 if ( m_cellHighlightColour != colour )
9677 {
9678 m_cellHighlightColour = colour;
9679
9680 wxClientDC dc( m_gridWin );
9681 PrepareDC( dc );
9682 wxGridCellAttr* attr = GetCellAttr(m_currentCellCoords);
9683 DrawCellHighlight(dc, attr);
9684 attr->DecRef();
9685 }
9686 }
9687
9688 void wxGrid::SetCellHighlightPenWidth(int width)
9689 {
9690 if (m_cellHighlightPenWidth != width)
9691 {
9692 m_cellHighlightPenWidth = width;
9693
9694 // Just redrawing the cell highlight is not enough since that won't
9695 // make any visible change if the the thickness is getting smaller.
9696 int row = m_currentCellCoords.GetRow();
9697 int col = m_currentCellCoords.GetCol();
9698 if ( row == -1 || col == -1 || 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::SetCellHighlightROPenWidth(int width)
9707 {
9708 if (m_cellHighlightROPenWidth != width)
9709 {
9710 m_cellHighlightROPenWidth = width;
9711
9712 // Just redrawing the cell highlight is not enough since that won't
9713 // make any visible change if the the thickness is getting smaller.
9714 int row = m_currentCellCoords.GetRow();
9715 int col = m_currentCellCoords.GetCol();
9716 if ( row == -1 || col == -1 ||
9717 GetColWidth(col) <= 0 || GetRowHeight(row) <= 0 )
9718 return;
9719
9720 wxRect rect = CellToRect(row, col);
9721 m_gridWin->Refresh(true, &rect);
9722 }
9723 }
9724
9725 void wxGrid::EnableGridLines( bool enable )
9726 {
9727 if ( enable != m_gridLinesEnabled )
9728 {
9729 m_gridLinesEnabled = enable;
9730
9731 if ( !GetBatchCount() )
9732 {
9733 if ( enable )
9734 {
9735 wxClientDC dc( m_gridWin );
9736 PrepareDC( dc );
9737 DrawAllGridLines( dc, wxRegion() );
9738 }
9739 else
9740 {
9741 m_gridWin->Refresh();
9742 }
9743 }
9744 }
9745 }
9746
9747 int wxGrid::GetDefaultRowSize() const
9748 {
9749 return m_defaultRowHeight;
9750 }
9751
9752 int wxGrid::GetRowSize( int row ) const
9753 {
9754 wxCHECK_MSG( row >= 0 && row < m_numRows, 0, _T("invalid row index") );
9755
9756 return GetRowHeight(row);
9757 }
9758
9759 int wxGrid::GetDefaultColSize() const
9760 {
9761 return m_defaultColWidth;
9762 }
9763
9764 int wxGrid::GetColSize( int col ) const
9765 {
9766 wxCHECK_MSG( col >= 0 && col < m_numCols, 0, _T("invalid column index") );
9767
9768 return GetColWidth(col);
9769 }
9770
9771 // ============================================================================
9772 // access to the grid attributes: each of them has a default value in the grid
9773 // itself and may be overidden on a per-cell basis
9774 // ============================================================================
9775
9776 // ----------------------------------------------------------------------------
9777 // setting default attributes
9778 // ----------------------------------------------------------------------------
9779
9780 void wxGrid::SetDefaultCellBackgroundColour( const wxColour& col )
9781 {
9782 m_defaultCellAttr->SetBackgroundColour(col);
9783 #ifdef __WXGTK__
9784 m_gridWin->SetBackgroundColour(col);
9785 #endif
9786 }
9787
9788 void wxGrid::SetDefaultCellTextColour( const wxColour& col )
9789 {
9790 m_defaultCellAttr->SetTextColour(col);
9791 }
9792
9793 void wxGrid::SetDefaultCellAlignment( int horiz, int vert )
9794 {
9795 m_defaultCellAttr->SetAlignment(horiz, vert);
9796 }
9797
9798 void wxGrid::SetDefaultCellOverflow( bool allow )
9799 {
9800 m_defaultCellAttr->SetOverflow(allow);
9801 }
9802
9803 void wxGrid::SetDefaultCellFont( const wxFont& font )
9804 {
9805 m_defaultCellAttr->SetFont(font);
9806 }
9807
9808 // For editors and renderers the type registry takes precedence over the
9809 // default attr, so we need to register the new editor/renderer for the string
9810 // data type in order to make setting a default editor/renderer appear to
9811 // work correctly.
9812
9813 void wxGrid::SetDefaultRenderer(wxGridCellRenderer *renderer)
9814 {
9815 RegisterDataType(wxGRID_VALUE_STRING,
9816 renderer,
9817 GetDefaultEditorForType(wxGRID_VALUE_STRING));
9818 }
9819
9820 void wxGrid::SetDefaultEditor(wxGridCellEditor *editor)
9821 {
9822 RegisterDataType(wxGRID_VALUE_STRING,
9823 GetDefaultRendererForType(wxGRID_VALUE_STRING),
9824 editor);
9825 }
9826
9827 // ----------------------------------------------------------------------------
9828 // access to the default attributes
9829 // ----------------------------------------------------------------------------
9830
9831 wxColour wxGrid::GetDefaultCellBackgroundColour() const
9832 {
9833 return m_defaultCellAttr->GetBackgroundColour();
9834 }
9835
9836 wxColour wxGrid::GetDefaultCellTextColour() const
9837 {
9838 return m_defaultCellAttr->GetTextColour();
9839 }
9840
9841 wxFont wxGrid::GetDefaultCellFont() const
9842 {
9843 return m_defaultCellAttr->GetFont();
9844 }
9845
9846 void wxGrid::GetDefaultCellAlignment( int *horiz, int *vert ) const
9847 {
9848 m_defaultCellAttr->GetAlignment(horiz, vert);
9849 }
9850
9851 bool wxGrid::GetDefaultCellOverflow() const
9852 {
9853 return m_defaultCellAttr->GetOverflow();
9854 }
9855
9856 wxGridCellRenderer *wxGrid::GetDefaultRenderer() const
9857 {
9858 return m_defaultCellAttr->GetRenderer(NULL, 0, 0);
9859 }
9860
9861 wxGridCellEditor *wxGrid::GetDefaultEditor() const
9862 {
9863 return m_defaultCellAttr->GetEditor(NULL, 0, 0);
9864 }
9865
9866 // ----------------------------------------------------------------------------
9867 // access to cell attributes
9868 // ----------------------------------------------------------------------------
9869
9870 wxColour wxGrid::GetCellBackgroundColour(int row, int col) const
9871 {
9872 wxGridCellAttr *attr = GetCellAttr(row, col);
9873 wxColour colour = attr->GetBackgroundColour();
9874 attr->DecRef();
9875
9876 return colour;
9877 }
9878
9879 wxColour wxGrid::GetCellTextColour( int row, int col ) const
9880 {
9881 wxGridCellAttr *attr = GetCellAttr(row, col);
9882 wxColour colour = attr->GetTextColour();
9883 attr->DecRef();
9884
9885 return colour;
9886 }
9887
9888 wxFont wxGrid::GetCellFont( int row, int col ) const
9889 {
9890 wxGridCellAttr *attr = GetCellAttr(row, col);
9891 wxFont font = attr->GetFont();
9892 attr->DecRef();
9893
9894 return font;
9895 }
9896
9897 void wxGrid::GetCellAlignment( int row, int col, int *horiz, int *vert ) const
9898 {
9899 wxGridCellAttr *attr = GetCellAttr(row, col);
9900 attr->GetAlignment(horiz, vert);
9901 attr->DecRef();
9902 }
9903
9904 bool wxGrid::GetCellOverflow( int row, int col ) const
9905 {
9906 wxGridCellAttr *attr = GetCellAttr(row, col);
9907 bool allow = attr->GetOverflow();
9908 attr->DecRef();
9909
9910 return allow;
9911 }
9912
9913 void wxGrid::GetCellSize( int row, int col, int *num_rows, int *num_cols ) const
9914 {
9915 wxGridCellAttr *attr = GetCellAttr(row, col);
9916 attr->GetSize( num_rows, num_cols );
9917 attr->DecRef();
9918 }
9919
9920 wxGridCellRenderer* wxGrid::GetCellRenderer(int row, int col) const
9921 {
9922 wxGridCellAttr* attr = GetCellAttr(row, col);
9923 wxGridCellRenderer* renderer = attr->GetRenderer(this, row, col);
9924 attr->DecRef();
9925
9926 return renderer;
9927 }
9928
9929 wxGridCellEditor* wxGrid::GetCellEditor(int row, int col) const
9930 {
9931 wxGridCellAttr* attr = GetCellAttr(row, col);
9932 wxGridCellEditor* editor = attr->GetEditor(this, row, col);
9933 attr->DecRef();
9934
9935 return editor;
9936 }
9937
9938 bool wxGrid::IsReadOnly(int row, int col) const
9939 {
9940 wxGridCellAttr* attr = GetCellAttr(row, col);
9941 bool isReadOnly = attr->IsReadOnly();
9942 attr->DecRef();
9943
9944 return isReadOnly;
9945 }
9946
9947 // ----------------------------------------------------------------------------
9948 // attribute support: cache, automatic provider creation, ...
9949 // ----------------------------------------------------------------------------
9950
9951 bool wxGrid::CanHaveAttributes() const
9952 {
9953 if ( !m_table )
9954 {
9955 return false;
9956 }
9957
9958 return m_table->CanHaveAttributes();
9959 }
9960
9961 void wxGrid::ClearAttrCache()
9962 {
9963 if ( m_attrCache.row != -1 )
9964 {
9965 wxGridCellAttr *oldAttr = m_attrCache.attr;
9966 m_attrCache.attr = NULL;
9967 m_attrCache.row = -1;
9968 // wxSafeDecRec(...) might cause event processing that accesses
9969 // the cached attribute, if one exists (e.g. by deleting the
9970 // editor stored within the attribute). Therefore it is important
9971 // to invalidate the cache before calling wxSafeDecRef!
9972 wxSafeDecRef(oldAttr);
9973 }
9974 }
9975
9976 void wxGrid::CacheAttr(int row, int col, wxGridCellAttr *attr) const
9977 {
9978 if ( attr != NULL )
9979 {
9980 wxGrid *self = (wxGrid *)this; // const_cast
9981
9982 self->ClearAttrCache();
9983 self->m_attrCache.row = row;
9984 self->m_attrCache.col = col;
9985 self->m_attrCache.attr = attr;
9986 wxSafeIncRef(attr);
9987 }
9988 }
9989
9990 bool wxGrid::LookupAttr(int row, int col, wxGridCellAttr **attr) const
9991 {
9992 if ( row == m_attrCache.row && col == m_attrCache.col )
9993 {
9994 *attr = m_attrCache.attr;
9995 wxSafeIncRef(m_attrCache.attr);
9996
9997 #ifdef DEBUG_ATTR_CACHE
9998 gs_nAttrCacheHits++;
9999 #endif
10000
10001 return true;
10002 }
10003 else
10004 {
10005 #ifdef DEBUG_ATTR_CACHE
10006 gs_nAttrCacheMisses++;
10007 #endif
10008
10009 return false;
10010 }
10011 }
10012
10013 wxGridCellAttr *wxGrid::GetCellAttr(int row, int col) const
10014 {
10015 wxGridCellAttr *attr = NULL;
10016 // Additional test to avoid looking at the cache e.g. for
10017 // wxNoCellCoords, as this will confuse memory management.
10018 if ( row >= 0 )
10019 {
10020 if ( !LookupAttr(row, col, &attr) )
10021 {
10022 attr = m_table ? m_table->GetAttr(row, col, wxGridCellAttr::Any)
10023 : (wxGridCellAttr *)NULL;
10024 CacheAttr(row, col, attr);
10025 }
10026 }
10027
10028 if (attr)
10029 {
10030 attr->SetDefAttr(m_defaultCellAttr);
10031 }
10032 else
10033 {
10034 attr = m_defaultCellAttr;
10035 attr->IncRef();
10036 }
10037
10038 return attr;
10039 }
10040
10041 wxGridCellAttr *wxGrid::GetOrCreateCellAttr(int row, int col) const
10042 {
10043 wxGridCellAttr *attr = (wxGridCellAttr *)NULL;
10044 bool canHave = ((wxGrid*)this)->CanHaveAttributes();
10045
10046 wxCHECK_MSG( canHave, attr, _T("Cell attributes not allowed"));
10047 wxCHECK_MSG( m_table, attr, _T("must have a table") );
10048
10049 attr = m_table->GetAttr(row, col, wxGridCellAttr::Cell);
10050 if ( !attr )
10051 {
10052 attr = new wxGridCellAttr(m_defaultCellAttr);
10053
10054 // artificially inc the ref count to match DecRef() in caller
10055 attr->IncRef();
10056 m_table->SetAttr(attr, row, col);
10057 }
10058
10059 return attr;
10060 }
10061
10062 // ----------------------------------------------------------------------------
10063 // setting column attributes (wrappers around SetColAttr)
10064 // ----------------------------------------------------------------------------
10065
10066 void wxGrid::SetColFormatBool(int col)
10067 {
10068 SetColFormatCustom(col, wxGRID_VALUE_BOOL);
10069 }
10070
10071 void wxGrid::SetColFormatNumber(int col)
10072 {
10073 SetColFormatCustom(col, wxGRID_VALUE_NUMBER);
10074 }
10075
10076 void wxGrid::SetColFormatFloat(int col, int width, int precision)
10077 {
10078 wxString typeName = wxGRID_VALUE_FLOAT;
10079 if ( (width != -1) || (precision != -1) )
10080 {
10081 typeName << _T(':') << width << _T(',') << precision;
10082 }
10083
10084 SetColFormatCustom(col, typeName);
10085 }
10086
10087 void wxGrid::SetColFormatCustom(int col, const wxString& typeName)
10088 {
10089 wxGridCellAttr *attr = m_table->GetAttr(-1, col, wxGridCellAttr::Col );
10090 if (!attr)
10091 attr = new wxGridCellAttr;
10092 wxGridCellRenderer *renderer = GetDefaultRendererForType(typeName);
10093 attr->SetRenderer(renderer);
10094 wxGridCellEditor *editor = GetDefaultEditorForType(typeName);
10095 attr->SetEditor(editor);
10096
10097 SetColAttr(col, attr);
10098
10099 }
10100
10101 // ----------------------------------------------------------------------------
10102 // setting cell attributes: this is forwarded to the table
10103 // ----------------------------------------------------------------------------
10104
10105 void wxGrid::SetAttr(int row, int col, wxGridCellAttr *attr)
10106 {
10107 if ( CanHaveAttributes() )
10108 {
10109 m_table->SetAttr(attr, row, col);
10110 ClearAttrCache();
10111 }
10112 else
10113 {
10114 wxSafeDecRef(attr);
10115 }
10116 }
10117
10118 void wxGrid::SetRowAttr(int row, wxGridCellAttr *attr)
10119 {
10120 if ( CanHaveAttributes() )
10121 {
10122 m_table->SetRowAttr(attr, row);
10123 ClearAttrCache();
10124 }
10125 else
10126 {
10127 wxSafeDecRef(attr);
10128 }
10129 }
10130
10131 void wxGrid::SetColAttr(int col, wxGridCellAttr *attr)
10132 {
10133 if ( CanHaveAttributes() )
10134 {
10135 m_table->SetColAttr(attr, col);
10136 ClearAttrCache();
10137 }
10138 else
10139 {
10140 wxSafeDecRef(attr);
10141 }
10142 }
10143
10144 void wxGrid::SetCellBackgroundColour( int row, int col, const wxColour& colour )
10145 {
10146 if ( CanHaveAttributes() )
10147 {
10148 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
10149 attr->SetBackgroundColour(colour);
10150 attr->DecRef();
10151 }
10152 }
10153
10154 void wxGrid::SetCellTextColour( int row, int col, const wxColour& colour )
10155 {
10156 if ( CanHaveAttributes() )
10157 {
10158 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
10159 attr->SetTextColour(colour);
10160 attr->DecRef();
10161 }
10162 }
10163
10164 void wxGrid::SetCellFont( int row, int col, const wxFont& font )
10165 {
10166 if ( CanHaveAttributes() )
10167 {
10168 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
10169 attr->SetFont(font);
10170 attr->DecRef();
10171 }
10172 }
10173
10174 void wxGrid::SetCellAlignment( int row, int col, int horiz, int vert )
10175 {
10176 if ( CanHaveAttributes() )
10177 {
10178 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
10179 attr->SetAlignment(horiz, vert);
10180 attr->DecRef();
10181 }
10182 }
10183
10184 void wxGrid::SetCellOverflow( int row, int col, bool allow )
10185 {
10186 if ( CanHaveAttributes() )
10187 {
10188 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
10189 attr->SetOverflow(allow);
10190 attr->DecRef();
10191 }
10192 }
10193
10194 void wxGrid::SetCellSize( int row, int col, int num_rows, int num_cols )
10195 {
10196 if ( CanHaveAttributes() )
10197 {
10198 int cell_rows, cell_cols;
10199
10200 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
10201 attr->GetSize(&cell_rows, &cell_cols);
10202 attr->SetSize(num_rows, num_cols);
10203 attr->DecRef();
10204
10205 // Cannot set the size of a cell to 0 or negative values
10206 // While it is perfectly legal to do that, this function cannot
10207 // handle all the possibilies, do it by hand by getting the CellAttr.
10208 // You can only set the size of a cell to 1,1 or greater with this fn
10209 wxASSERT_MSG( !((cell_rows < 1) || (cell_cols < 1)),
10210 wxT("wxGrid::SetCellSize setting cell size that is already part of another cell"));
10211 wxASSERT_MSG( !((num_rows < 1) || (num_cols < 1)),
10212 wxT("wxGrid::SetCellSize setting cell size to < 1"));
10213
10214 // if this was already a multicell then "turn off" the other cells first
10215 if ((cell_rows > 1) || (cell_cols > 1))
10216 {
10217 int i, j;
10218 for (j=row; j < row + cell_rows; j++)
10219 {
10220 for (i=col; i < col + cell_cols; i++)
10221 {
10222 if ((i != col) || (j != row))
10223 {
10224 wxGridCellAttr *attr_stub = GetOrCreateCellAttr(j, i);
10225 attr_stub->SetSize( 1, 1 );
10226 attr_stub->DecRef();
10227 }
10228 }
10229 }
10230 }
10231
10232 // mark the cells that will be covered by this cell to
10233 // negative or zero values to point back at this cell
10234 if (((num_rows > 1) || (num_cols > 1)) && (num_rows >= 1) && (num_cols >= 1))
10235 {
10236 int i, j;
10237 for (j=row; j < row + num_rows; j++)
10238 {
10239 for (i=col; i < col + num_cols; i++)
10240 {
10241 if ((i != col) || (j != row))
10242 {
10243 wxGridCellAttr *attr_stub = GetOrCreateCellAttr(j, i);
10244 attr_stub->SetSize( row - j, col - i );
10245 attr_stub->DecRef();
10246 }
10247 }
10248 }
10249 }
10250 }
10251 }
10252
10253 void wxGrid::SetCellRenderer(int row, int col, wxGridCellRenderer *renderer)
10254 {
10255 if ( CanHaveAttributes() )
10256 {
10257 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
10258 attr->SetRenderer(renderer);
10259 attr->DecRef();
10260 }
10261 }
10262
10263 void wxGrid::SetCellEditor(int row, int col, wxGridCellEditor* editor)
10264 {
10265 if ( CanHaveAttributes() )
10266 {
10267 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
10268 attr->SetEditor(editor);
10269 attr->DecRef();
10270 }
10271 }
10272
10273 void wxGrid::SetReadOnly(int row, int col, bool isReadOnly)
10274 {
10275 if ( CanHaveAttributes() )
10276 {
10277 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
10278 attr->SetReadOnly(isReadOnly);
10279 attr->DecRef();
10280 }
10281 }
10282
10283 // ----------------------------------------------------------------------------
10284 // Data type registration
10285 // ----------------------------------------------------------------------------
10286
10287 void wxGrid::RegisterDataType(const wxString& typeName,
10288 wxGridCellRenderer* renderer,
10289 wxGridCellEditor* editor)
10290 {
10291 m_typeRegistry->RegisterDataType(typeName, renderer, editor);
10292 }
10293
10294
10295 wxGridCellEditor * wxGrid::GetDefaultEditorForCell(int row, int col) const
10296 {
10297 wxString typeName = m_table->GetTypeName(row, col);
10298 return GetDefaultEditorForType(typeName);
10299 }
10300
10301 wxGridCellRenderer * wxGrid::GetDefaultRendererForCell(int row, int col) const
10302 {
10303 wxString typeName = m_table->GetTypeName(row, col);
10304 return GetDefaultRendererForType(typeName);
10305 }
10306
10307 wxGridCellEditor * wxGrid::GetDefaultEditorForType(const wxString& typeName) const
10308 {
10309 int index = m_typeRegistry->FindOrCloneDataType(typeName);
10310 if ( index == wxNOT_FOUND )
10311 {
10312 wxFAIL_MSG(wxString::Format(wxT("Unknown data type name [%s]"), typeName.c_str()));
10313
10314 return NULL;
10315 }
10316
10317 return m_typeRegistry->GetEditor(index);
10318 }
10319
10320 wxGridCellRenderer * wxGrid::GetDefaultRendererForType(const wxString& typeName) const
10321 {
10322 int index = m_typeRegistry->FindOrCloneDataType(typeName);
10323 if ( index == wxNOT_FOUND )
10324 {
10325 wxFAIL_MSG(wxString::Format(wxT("Unknown data type name [%s]"), typeName.c_str()));
10326
10327 return NULL;
10328 }
10329
10330 return m_typeRegistry->GetRenderer(index);
10331 }
10332
10333 // ----------------------------------------------------------------------------
10334 // row/col size
10335 // ----------------------------------------------------------------------------
10336
10337 void wxGrid::EnableDragRowSize( bool enable )
10338 {
10339 m_canDragRowSize = enable;
10340 }
10341
10342 void wxGrid::EnableDragColSize( bool enable )
10343 {
10344 m_canDragColSize = enable;
10345 }
10346
10347 void wxGrid::EnableDragGridSize( bool enable )
10348 {
10349 m_canDragGridSize = enable;
10350 }
10351
10352 void wxGrid::EnableDragCell( bool enable )
10353 {
10354 m_canDragCell = enable;
10355 }
10356
10357 void wxGrid::SetDefaultRowSize( int height, bool resizeExistingRows )
10358 {
10359 m_defaultRowHeight = wxMax( height, m_minAcceptableRowHeight );
10360
10361 if ( resizeExistingRows )
10362 {
10363 // since we are resizing all rows to the default row size,
10364 // we can simply clear the row heights and row bottoms
10365 // arrays (which also allows us to take advantage of
10366 // some speed optimisations)
10367 m_rowHeights.Empty();
10368 m_rowBottoms.Empty();
10369 if ( !GetBatchCount() )
10370 CalcDimensions();
10371 }
10372 }
10373
10374 void wxGrid::SetRowSize( int row, int height )
10375 {
10376 wxCHECK_RET( row >= 0 && row < m_numRows, _T("invalid row index") );
10377
10378 // if < 0 then calculate new height from label
10379 if ( height < 0 )
10380 {
10381 long w, h;
10382 wxArrayString lines;
10383 wxClientDC dc(m_rowLabelWin);
10384 dc.SetFont(GetLabelFont());
10385 StringToLines(GetRowLabelValue( row ), lines);
10386 GetTextBoxSize( dc, lines, &w, &h );
10387 //check that it is not less than the minimal height
10388 height = wxMax(h, GetRowMinimalAcceptableHeight());
10389 }
10390
10391 // See comment in SetColSize
10392 if ( height < GetRowMinimalAcceptableHeight())
10393 return;
10394
10395 if ( m_rowHeights.IsEmpty() )
10396 {
10397 // need to really create the array
10398 InitRowHeights();
10399 }
10400
10401 int h = wxMax( 0, height );
10402 int diff = h - m_rowHeights[row];
10403
10404 m_rowHeights[row] = h;
10405 for ( int i = row; i < m_numRows; i++ )
10406 {
10407 m_rowBottoms[i] += diff;
10408 }
10409
10410 if ( !GetBatchCount() )
10411 CalcDimensions();
10412 }
10413
10414 void wxGrid::SetDefaultColSize( int width, bool resizeExistingCols )
10415 {
10416 // we dont allow zero default column width
10417 m_defaultColWidth = wxMax( wxMax( width, m_minAcceptableColWidth ), 1 );
10418
10419 if ( resizeExistingCols )
10420 {
10421 // since we are resizing all columns to the default column size,
10422 // we can simply clear the col widths and col rights
10423 // arrays (which also allows us to take advantage of
10424 // some speed optimisations)
10425 m_colWidths.Empty();
10426 m_colRights.Empty();
10427 if ( !GetBatchCount() )
10428 CalcDimensions();
10429 }
10430 }
10431
10432 void wxGrid::SetColSize( int col, int width )
10433 {
10434 wxCHECK_RET( col >= 0 && col < m_numCols, _T("invalid column index") );
10435
10436 // if < 0 then calculate new width from label
10437 if ( width < 0 )
10438 {
10439 long w, h;
10440 wxArrayString lines;
10441 wxClientDC dc(m_colLabelWin);
10442 dc.SetFont(GetLabelFont());
10443 StringToLines(GetColLabelValue(col), lines);
10444 if ( GetColLabelTextOrientation() == wxHORIZONTAL )
10445 GetTextBoxSize( dc, lines, &w, &h );
10446 else
10447 GetTextBoxSize( dc, lines, &h, &w );
10448 width = w + 6;
10449 //check that it is not less than the minimal width
10450 width = wxMax(width, GetColMinimalAcceptableWidth());
10451 }
10452
10453 // should we check that it's bigger than GetColMinimalWidth(col) here?
10454 // (VZ)
10455 // No, because it is reasonable to assume the library user know's
10456 // what he is doing. However we should test against the weaker
10457 // constraint of minimalAcceptableWidth, as this breaks rendering
10458 //
10459 // This test then fixes sf.net bug #645734
10460
10461 if ( width < GetColMinimalAcceptableWidth() )
10462 return;
10463
10464 if ( m_colWidths.IsEmpty() )
10465 {
10466 // need to really create the array
10467 InitColWidths();
10468 }
10469
10470 int w = wxMax( 0, width );
10471 int diff = w - m_colWidths[col];
10472 m_colWidths[col] = w;
10473
10474 for ( int colPos = GetColPos(col); colPos < m_numCols; colPos++ )
10475 {
10476 m_colRights[GetColAt(colPos)] += diff;
10477 }
10478
10479 if ( !GetBatchCount() )
10480 CalcDimensions();
10481 }
10482
10483 void wxGrid::SetColMinimalWidth( int col, int width )
10484 {
10485 if (width > GetColMinimalAcceptableWidth())
10486 {
10487 wxLongToLongHashMap::key_type key = (wxLongToLongHashMap::key_type)col;
10488 m_colMinWidths[key] = width;
10489 }
10490 }
10491
10492 void wxGrid::SetRowMinimalHeight( int row, int width )
10493 {
10494 if (width > GetRowMinimalAcceptableHeight())
10495 {
10496 wxLongToLongHashMap::key_type key = (wxLongToLongHashMap::key_type)row;
10497 m_rowMinHeights[key] = width;
10498 }
10499 }
10500
10501 int wxGrid::GetColMinimalWidth(int col) const
10502 {
10503 wxLongToLongHashMap::key_type key = (wxLongToLongHashMap::key_type)col;
10504 wxLongToLongHashMap::const_iterator it = m_colMinWidths.find(key);
10505
10506 return it != m_colMinWidths.end() ? (int)it->second : m_minAcceptableColWidth;
10507 }
10508
10509 int wxGrid::GetRowMinimalHeight(int row) const
10510 {
10511 wxLongToLongHashMap::key_type key = (wxLongToLongHashMap::key_type)row;
10512 wxLongToLongHashMap::const_iterator it = m_rowMinHeights.find(key);
10513
10514 return it != m_rowMinHeights.end() ? (int)it->second : m_minAcceptableRowHeight;
10515 }
10516
10517 void wxGrid::SetColMinimalAcceptableWidth( int width )
10518 {
10519 // We do allow a width of 0 since this gives us
10520 // an easy way to temporarily hiding columns.
10521 if ( width >= 0 )
10522 m_minAcceptableColWidth = width;
10523 }
10524
10525 void wxGrid::SetRowMinimalAcceptableHeight( int height )
10526 {
10527 // We do allow a height of 0 since this gives us
10528 // an easy way to temporarily hiding rows.
10529 if ( height >= 0 )
10530 m_minAcceptableRowHeight = height;
10531 }
10532
10533 int wxGrid::GetColMinimalAcceptableWidth() const
10534 {
10535 return m_minAcceptableColWidth;
10536 }
10537
10538 int wxGrid::GetRowMinimalAcceptableHeight() const
10539 {
10540 return m_minAcceptableRowHeight;
10541 }
10542
10543 // ----------------------------------------------------------------------------
10544 // auto sizing
10545 // ----------------------------------------------------------------------------
10546
10547 void
10548 wxGrid::AutoSizeColOrRow(int colOrRow, bool setAsMin, wxGridDirection direction)
10549 {
10550 const bool column = direction == wxGRID_COLUMN;
10551
10552 wxClientDC dc(m_gridWin);
10553
10554 // cancel editing of cell
10555 HideCellEditControl();
10556 SaveEditControlValue();
10557
10558 // init both of them to avoid compiler warnings, even if we only need one
10559 int row = -1,
10560 col = -1;
10561 if ( column )
10562 col = colOrRow;
10563 else
10564 row = colOrRow;
10565
10566 wxCoord extent, extentMax = 0;
10567 int max = column ? m_numRows : m_numCols;
10568 for ( int rowOrCol = 0; rowOrCol < max; rowOrCol++ )
10569 {
10570 if ( column )
10571 row = rowOrCol;
10572 else
10573 col = rowOrCol;
10574
10575 wxGridCellAttr *attr = GetCellAttr(row, col);
10576 wxGridCellRenderer *renderer = attr->GetRenderer(this, row, col);
10577 if ( renderer )
10578 {
10579 wxSize size = renderer->GetBestSize(*this, *attr, dc, row, col);
10580 extent = column ? size.x : size.y;
10581 if ( extent > extentMax )
10582 extentMax = extent;
10583
10584 renderer->DecRef();
10585 }
10586
10587 attr->DecRef();
10588 }
10589
10590 // now also compare with the column label extent
10591 wxCoord w, h;
10592 dc.SetFont( GetLabelFont() );
10593
10594 if ( column )
10595 {
10596 dc.GetMultiLineTextExtent( GetColLabelValue(col), &w, &h );
10597 if ( GetColLabelTextOrientation() == wxVERTICAL )
10598 w = h;
10599 }
10600 else
10601 dc.GetMultiLineTextExtent( GetRowLabelValue(row), &w, &h );
10602
10603 extent = column ? w : h;
10604 if ( extent > extentMax )
10605 extentMax = extent;
10606
10607 if ( !extentMax )
10608 {
10609 // empty column - give default extent (notice that if extentMax is less
10610 // than default extent but != 0, it's OK)
10611 extentMax = column ? m_defaultColWidth : m_defaultRowHeight;
10612 }
10613 else
10614 {
10615 if ( column )
10616 // leave some space around text
10617 extentMax += 10;
10618 else
10619 extentMax += 6;
10620 }
10621
10622 if ( column )
10623 {
10624 // Ensure automatic width is not less than minimal width. See the
10625 // comment in SetColSize() for explanation of why this isn't done
10626 // in SetColSize().
10627 if ( !setAsMin )
10628 extentMax = wxMax(extentMax, GetColMinimalWidth(col));
10629
10630 SetColSize( col, extentMax );
10631 if ( !GetBatchCount() )
10632 {
10633 int cw, ch, dummy;
10634 m_gridWin->GetClientSize( &cw, &ch );
10635 wxRect rect ( CellToRect( 0, col ) );
10636 rect.y = 0;
10637 CalcScrolledPosition(rect.x, 0, &rect.x, &dummy);
10638 rect.width = cw - rect.x;
10639 rect.height = m_colLabelHeight;
10640 m_colLabelWin->Refresh( true, &rect );
10641 }
10642 }
10643 else
10644 {
10645 // Ensure automatic width is not less than minimal height. See the
10646 // comment in SetColSize() for explanation of why this isn't done
10647 // in SetRowSize().
10648 if ( !setAsMin )
10649 extentMax = wxMax(extentMax, GetRowMinimalHeight(row));
10650
10651 SetRowSize(row, extentMax);
10652 if ( !GetBatchCount() )
10653 {
10654 int cw, ch, dummy;
10655 m_gridWin->GetClientSize( &cw, &ch );
10656 wxRect rect( CellToRect( row, 0 ) );
10657 rect.x = 0;
10658 CalcScrolledPosition(0, rect.y, &dummy, &rect.y);
10659 rect.width = m_rowLabelWidth;
10660 rect.height = ch - rect.y;
10661 m_rowLabelWin->Refresh( true, &rect );
10662 }
10663 }
10664
10665 if ( setAsMin )
10666 {
10667 if ( column )
10668 SetColMinimalWidth(col, extentMax);
10669 else
10670 SetRowMinimalHeight(row, extentMax);
10671 }
10672 }
10673
10674 wxCoord wxGrid::CalcColOrRowLabelAreaMinSize(wxGridDirection direction)
10675 {
10676 // calculate size for the rows or columns?
10677 const bool calcRows = direction == wxGRID_ROW;
10678
10679 wxClientDC dc(calcRows ? GetGridRowLabelWindow()
10680 : GetGridColLabelWindow());
10681 dc.SetFont(GetLabelFont());
10682
10683 // which dimension should we take into account for calculations?
10684 //
10685 // for columns, the text can be only horizontal so it's easy but for rows
10686 // we also have to take into account the text orientation
10687 const bool
10688 useWidth = calcRows || (GetColLabelTextOrientation() == wxVERTICAL);
10689
10690 wxArrayString lines;
10691 wxCoord extentMax = 0;
10692
10693 const int numRowsOrCols = calcRows ? m_numRows : m_numCols;
10694 for ( int rowOrCol = 0; rowOrCol < numRowsOrCols; rowOrCol++ )
10695 {
10696 lines.Clear();
10697
10698 wxString label = calcRows ? GetRowLabelValue(rowOrCol)
10699 : GetColLabelValue(rowOrCol);
10700 StringToLines(label, lines);
10701
10702 long w, h;
10703 GetTextBoxSize(dc, lines, &w, &h);
10704
10705 const wxCoord extent = useWidth ? w : h;
10706 if ( extent > extentMax )
10707 extentMax = extent;
10708 }
10709
10710 if ( !extentMax )
10711 {
10712 // empty column - give default extent (notice that if extentMax is less
10713 // than default extent but != 0, it's OK)
10714 extentMax = calcRows ? GetDefaultRowLabelSize()
10715 : GetDefaultColLabelSize();
10716 }
10717
10718 // leave some space around text (taken from AutoSizeColOrRow)
10719 if ( calcRows )
10720 extentMax += 10;
10721 else
10722 extentMax += 6;
10723
10724 return extentMax;
10725 }
10726
10727 int wxGrid::SetOrCalcColumnSizes(bool calcOnly, bool setAsMin)
10728 {
10729 int width = m_rowLabelWidth;
10730
10731 wxGridUpdateLocker locker;
10732 if(!calcOnly)
10733 locker.Create(this);
10734
10735 for ( int col = 0; col < m_numCols; col++ )
10736 {
10737 if ( !calcOnly )
10738 AutoSizeColumn(col, setAsMin);
10739
10740 width += GetColWidth(col);
10741 }
10742
10743 return width;
10744 }
10745
10746 int wxGrid::SetOrCalcRowSizes(bool calcOnly, bool setAsMin)
10747 {
10748 int height = m_colLabelHeight;
10749
10750 wxGridUpdateLocker locker;
10751 if(!calcOnly)
10752 locker.Create(this);
10753
10754 for ( int row = 0; row < m_numRows; row++ )
10755 {
10756 if ( !calcOnly )
10757 AutoSizeRow(row, setAsMin);
10758
10759 height += GetRowHeight(row);
10760 }
10761
10762 return height;
10763 }
10764
10765 void wxGrid::AutoSize()
10766 {
10767 wxGridUpdateLocker locker(this);
10768
10769 wxSize size(SetOrCalcColumnSizes(false) - m_rowLabelWidth + m_extraWidth,
10770 SetOrCalcRowSizes(false) - m_colLabelHeight + m_extraHeight);
10771
10772 // we know that we're not going to have scrollbars so disable them now to
10773 // avoid trouble in SetClientSize() which can otherwise set the correct
10774 // client size but also leave space for (not needed any more) scrollbars
10775 SetScrollbars(0, 0, 0, 0, 0, 0, true);
10776
10777 // restore the scroll rate parameters overwritten by SetScrollbars()
10778 SetScrollRate(m_scrollLineX, m_scrollLineY);
10779
10780 SetClientSize(size.x + m_rowLabelWidth, size.y + m_colLabelHeight);
10781 }
10782
10783 void wxGrid::AutoSizeRowLabelSize( int row )
10784 {
10785 // Hide the edit control, so it
10786 // won't interfere with drag-shrinking.
10787 if ( IsCellEditControlShown() )
10788 {
10789 HideCellEditControl();
10790 SaveEditControlValue();
10791 }
10792
10793 // autosize row height depending on label text
10794 SetRowSize(row, -1);
10795 ForceRefresh();
10796 }
10797
10798 void wxGrid::AutoSizeColLabelSize( int col )
10799 {
10800 // Hide the edit control, so it
10801 // won't interfere with drag-shrinking.
10802 if ( IsCellEditControlShown() )
10803 {
10804 HideCellEditControl();
10805 SaveEditControlValue();
10806 }
10807
10808 // autosize column width depending on label text
10809 SetColSize(col, -1);
10810 ForceRefresh();
10811 }
10812
10813 wxSize wxGrid::DoGetBestSize() const
10814 {
10815 wxGrid *self = (wxGrid *)this; // const_cast
10816
10817 // we do the same as in AutoSize() here with the exception that we don't
10818 // change the column/row sizes, only calculate them
10819 wxSize size(self->SetOrCalcColumnSizes(true) - m_rowLabelWidth + m_extraWidth,
10820 self->SetOrCalcRowSizes(true) - m_colLabelHeight + m_extraHeight);
10821
10822 // NOTE: This size should be cached, but first we need to add calls to
10823 // InvalidateBestSize everywhere that could change the results of this
10824 // calculation.
10825 // CacheBestSize(size);
10826
10827 return wxSize(size.x + m_rowLabelWidth, size.y + m_colLabelHeight)
10828 + GetWindowBorderSize();
10829 }
10830
10831 void wxGrid::Fit()
10832 {
10833 AutoSize();
10834 }
10835
10836 wxPen& wxGrid::GetDividerPen() const
10837 {
10838 return wxNullPen;
10839 }
10840
10841 // ----------------------------------------------------------------------------
10842 // cell value accessor functions
10843 // ----------------------------------------------------------------------------
10844
10845 void wxGrid::SetCellValue( int row, int col, const wxString& s )
10846 {
10847 if ( m_table )
10848 {
10849 m_table->SetValue( row, col, s );
10850 if ( !GetBatchCount() )
10851 {
10852 int dummy;
10853 wxRect rect( CellToRect( row, col ) );
10854 rect.x = 0;
10855 rect.width = m_gridWin->GetClientSize().GetWidth();
10856 CalcScrolledPosition(0, rect.y, &dummy, &rect.y);
10857 m_gridWin->Refresh( false, &rect );
10858 }
10859
10860 if ( m_currentCellCoords.GetRow() == row &&
10861 m_currentCellCoords.GetCol() == col &&
10862 IsCellEditControlShown())
10863 // Note: If we are using IsCellEditControlEnabled,
10864 // this interacts badly with calling SetCellValue from
10865 // an EVT_GRID_CELL_CHANGE handler.
10866 {
10867 HideCellEditControl();
10868 ShowCellEditControl(); // will reread data from table
10869 }
10870 }
10871 }
10872
10873 // ----------------------------------------------------------------------------
10874 // block, row and column selection
10875 // ----------------------------------------------------------------------------
10876
10877 void wxGrid::SelectRow( int row, bool addToSelected )
10878 {
10879 if ( IsSelection() && !addToSelected )
10880 ClearSelection();
10881
10882 if ( m_selection )
10883 m_selection->SelectRow( row, false, addToSelected );
10884 }
10885
10886 void wxGrid::SelectCol( int col, bool addToSelected )
10887 {
10888 if ( IsSelection() && !addToSelected )
10889 ClearSelection();
10890
10891 if ( m_selection )
10892 m_selection->SelectCol( col, false, addToSelected );
10893 }
10894
10895 void wxGrid::SelectBlock( int topRow, int leftCol, int bottomRow, int rightCol,
10896 bool addToSelected )
10897 {
10898 if ( IsSelection() && !addToSelected )
10899 ClearSelection();
10900
10901 if ( m_selection )
10902 m_selection->SelectBlock( topRow, leftCol, bottomRow, rightCol,
10903 false, addToSelected );
10904 }
10905
10906 void wxGrid::SelectAll()
10907 {
10908 if ( m_numRows > 0 && m_numCols > 0 )
10909 {
10910 if ( m_selection )
10911 m_selection->SelectBlock( 0, 0, m_numRows - 1, m_numCols - 1 );
10912 }
10913 }
10914
10915 // ----------------------------------------------------------------------------
10916 // cell, row and col deselection
10917 // ----------------------------------------------------------------------------
10918
10919 void wxGrid::DeselectRow( int row )
10920 {
10921 if ( !m_selection )
10922 return;
10923
10924 if ( m_selection->GetSelectionMode() == wxGrid::wxGridSelectRows )
10925 {
10926 if ( m_selection->IsInSelection(row, 0 ) )
10927 m_selection->ToggleCellSelection(row, 0);
10928 }
10929 else
10930 {
10931 int nCols = GetNumberCols();
10932 for ( int i = 0; i < nCols; i++ )
10933 {
10934 if ( m_selection->IsInSelection(row, i ) )
10935 m_selection->ToggleCellSelection(row, i);
10936 }
10937 }
10938 }
10939
10940 void wxGrid::DeselectCol( int col )
10941 {
10942 if ( !m_selection )
10943 return;
10944
10945 if ( m_selection->GetSelectionMode() == wxGrid::wxGridSelectColumns )
10946 {
10947 if ( m_selection->IsInSelection(0, col ) )
10948 m_selection->ToggleCellSelection(0, col);
10949 }
10950 else
10951 {
10952 int nRows = GetNumberRows();
10953 for ( int i = 0; i < nRows; i++ )
10954 {
10955 if ( m_selection->IsInSelection(i, col ) )
10956 m_selection->ToggleCellSelection(i, col);
10957 }
10958 }
10959 }
10960
10961 void wxGrid::DeselectCell( int row, int col )
10962 {
10963 if ( m_selection && m_selection->IsInSelection(row, col) )
10964 m_selection->ToggleCellSelection(row, col);
10965 }
10966
10967 bool wxGrid::IsSelection() const
10968 {
10969 return ( m_selection && (m_selection->IsSelection() ||
10970 ( m_selectingTopLeft != wxGridNoCellCoords &&
10971 m_selectingBottomRight != wxGridNoCellCoords) ) );
10972 }
10973
10974 bool wxGrid::IsInSelection( int row, int col ) const
10975 {
10976 return ( m_selection && (m_selection->IsInSelection( row, col ) ||
10977 ( row >= m_selectingTopLeft.GetRow() &&
10978 col >= m_selectingTopLeft.GetCol() &&
10979 row <= m_selectingBottomRight.GetRow() &&
10980 col <= m_selectingBottomRight.GetCol() )) );
10981 }
10982
10983 wxGridCellCoordsArray wxGrid::GetSelectedCells() const
10984 {
10985 if (!m_selection)
10986 {
10987 wxGridCellCoordsArray a;
10988 return a;
10989 }
10990
10991 return m_selection->m_cellSelection;
10992 }
10993
10994 wxGridCellCoordsArray wxGrid::GetSelectionBlockTopLeft() const
10995 {
10996 if (!m_selection)
10997 {
10998 wxGridCellCoordsArray a;
10999 return a;
11000 }
11001
11002 return m_selection->m_blockSelectionTopLeft;
11003 }
11004
11005 wxGridCellCoordsArray wxGrid::GetSelectionBlockBottomRight() const
11006 {
11007 if (!m_selection)
11008 {
11009 wxGridCellCoordsArray a;
11010 return a;
11011 }
11012
11013 return m_selection->m_blockSelectionBottomRight;
11014 }
11015
11016 wxArrayInt wxGrid::GetSelectedRows() const
11017 {
11018 if (!m_selection)
11019 {
11020 wxArrayInt a;
11021 return a;
11022 }
11023
11024 return m_selection->m_rowSelection;
11025 }
11026
11027 wxArrayInt wxGrid::GetSelectedCols() const
11028 {
11029 if (!m_selection)
11030 {
11031 wxArrayInt a;
11032 return a;
11033 }
11034
11035 return m_selection->m_colSelection;
11036 }
11037
11038 void wxGrid::ClearSelection()
11039 {
11040 wxRect r1 = BlockToDeviceRect( m_selectingTopLeft, m_selectingBottomRight);
11041 wxRect r2 = BlockToDeviceRect( m_currentCellCoords, m_selectingKeyboard );
11042 m_selectingTopLeft =
11043 m_selectingBottomRight =
11044 m_selectingKeyboard = wxGridNoCellCoords;
11045 Refresh( false, &r1 );
11046 Refresh( false, &r2 );
11047 if ( m_selection )
11048 m_selection->ClearSelection();
11049 }
11050
11051 // This function returns the rectangle that encloses the given block
11052 // in device coords clipped to the client size of the grid window.
11053 //
11054 wxRect wxGrid::BlockToDeviceRect( const wxGridCellCoords& topLeft,
11055 const wxGridCellCoords& bottomRight ) const
11056 {
11057 wxRect resultRect;
11058 wxRect tempCellRect = CellToRect(topLeft);
11059 if ( tempCellRect != wxGridNoCellRect )
11060 {
11061 resultRect = tempCellRect;
11062 }
11063 else
11064 {
11065 resultRect = wxRect(0, 0, 0, 0);
11066 }
11067
11068 tempCellRect = CellToRect(bottomRight);
11069 if ( tempCellRect != wxGridNoCellRect )
11070 {
11071 resultRect += tempCellRect;
11072 }
11073 else
11074 {
11075 // If both inputs were "wxGridNoCellRect," then there's nothing to do.
11076 return wxGridNoCellRect;
11077 }
11078
11079 // Ensure that left/right and top/bottom pairs are in order.
11080 int left = resultRect.GetLeft();
11081 int top = resultRect.GetTop();
11082 int right = resultRect.GetRight();
11083 int bottom = resultRect.GetBottom();
11084
11085 int leftCol = topLeft.GetCol();
11086 int topRow = topLeft.GetRow();
11087 int rightCol = bottomRight.GetCol();
11088 int bottomRow = bottomRight.GetRow();
11089
11090 if (left > right)
11091 {
11092 int tmp = left;
11093 left = right;
11094 right = tmp;
11095
11096 tmp = leftCol;
11097 leftCol = rightCol;
11098 rightCol = tmp;
11099 }
11100
11101 if (top > bottom)
11102 {
11103 int tmp = top;
11104 top = bottom;
11105 bottom = tmp;
11106
11107 tmp = topRow;
11108 topRow = bottomRow;
11109 bottomRow = tmp;
11110 }
11111
11112 // The following loop is ONLY necessary to detect and handle merged cells.
11113 int cw, ch;
11114 m_gridWin->GetClientSize( &cw, &ch );
11115
11116 // Get the origin coordinates: notice that they will be negative if the
11117 // grid is scrolled downwards/to the right.
11118 int gridOriginX = 0;
11119 int gridOriginY = 0;
11120 CalcScrolledPosition(gridOriginX, gridOriginY, &gridOriginX, &gridOriginY);
11121
11122 int onScreenLeftmostCol = internalXToCol(-gridOriginX);
11123 int onScreenUppermostRow = internalYToRow(-gridOriginY);
11124
11125 int onScreenRightmostCol = internalXToCol(-gridOriginX + cw);
11126 int onScreenBottommostRow = internalYToRow(-gridOriginY + ch);
11127
11128 // Bound our loop so that we only examine the portion of the selected block
11129 // that is shown on screen. Therefore, we compare the Top-Left block values
11130 // to the Top-Left screen values, and the Bottom-Right block values to the
11131 // Bottom-Right screen values, choosing appropriately.
11132 const int visibleTopRow = wxMax(topRow, onScreenUppermostRow);
11133 const int visibleBottomRow = wxMin(bottomRow, onScreenBottommostRow);
11134 const int visibleLeftCol = wxMax(leftCol, onScreenLeftmostCol);
11135 const int visibleRightCol = wxMin(rightCol, onScreenRightmostCol);
11136
11137 for ( int j = visibleTopRow; j <= visibleBottomRow; j++ )
11138 {
11139 for ( int i = visibleLeftCol; i <= visibleRightCol; i++ )
11140 {
11141 if ( (j == visibleTopRow) || (j == visibleBottomRow) ||
11142 (i == visibleLeftCol) || (i == visibleRightCol) )
11143 {
11144 tempCellRect = CellToRect( j, i );
11145
11146 if (tempCellRect.x < left)
11147 left = tempCellRect.x;
11148 if (tempCellRect.y < top)
11149 top = tempCellRect.y;
11150 if (tempCellRect.x + tempCellRect.width > right)
11151 right = tempCellRect.x + tempCellRect.width;
11152 if (tempCellRect.y + tempCellRect.height > bottom)
11153 bottom = tempCellRect.y + tempCellRect.height;
11154 }
11155 else
11156 {
11157 i = visibleRightCol; // jump over inner cells.
11158 }
11159 }
11160 }
11161
11162 // Convert to scrolled coords
11163 CalcScrolledPosition( left, top, &left, &top );
11164 CalcScrolledPosition( right, bottom, &right, &bottom );
11165
11166 if (right < 0 || bottom < 0 || left > cw || top > ch)
11167 return wxRect(0,0,0,0);
11168
11169 resultRect.SetLeft( wxMax(0, left) );
11170 resultRect.SetTop( wxMax(0, top) );
11171 resultRect.SetRight( wxMin(cw, right) );
11172 resultRect.SetBottom( wxMin(ch, bottom) );
11173
11174 return resultRect;
11175 }
11176
11177 // ----------------------------------------------------------------------------
11178 // drop target
11179 // ----------------------------------------------------------------------------
11180
11181 #if wxUSE_DRAG_AND_DROP
11182
11183 // this allow setting drop target directly on wxGrid
11184 void wxGrid::SetDropTarget(wxDropTarget *dropTarget)
11185 {
11186 GetGridWindow()->SetDropTarget(dropTarget);
11187 }
11188
11189 #endif // wxUSE_DRAG_AND_DROP
11190
11191 // ----------------------------------------------------------------------------
11192 // grid event classes
11193 // ----------------------------------------------------------------------------
11194
11195 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent, wxNotifyEvent )
11196
11197 wxGridEvent::wxGridEvent( int id, wxEventType type, wxObject* obj,
11198 int row, int col, int x, int y, bool sel,
11199 bool control, bool shift, bool alt, bool meta )
11200 : wxNotifyEvent( type, id )
11201 {
11202 m_row = row;
11203 m_col = col;
11204 m_x = x;
11205 m_y = y;
11206 m_selecting = sel;
11207 m_control = control;
11208 m_shift = shift;
11209 m_alt = alt;
11210 m_meta = meta;
11211
11212 SetEventObject(obj);
11213 }
11214
11215
11216 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent, wxNotifyEvent )
11217
11218 wxGridSizeEvent::wxGridSizeEvent( int id, wxEventType type, wxObject* obj,
11219 int rowOrCol, int x, int y,
11220 bool control, bool shift, bool alt, bool meta )
11221 : wxNotifyEvent( type, id )
11222 {
11223 m_rowOrCol = rowOrCol;
11224 m_x = x;
11225 m_y = y;
11226 m_control = control;
11227 m_shift = shift;
11228 m_alt = alt;
11229 m_meta = meta;
11230
11231 SetEventObject(obj);
11232 }
11233
11234
11235 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent, wxNotifyEvent )
11236
11237 wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id, wxEventType type, wxObject* obj,
11238 const wxGridCellCoords& topLeft,
11239 const wxGridCellCoords& bottomRight,
11240 bool sel, bool control,
11241 bool shift, bool alt, bool meta )
11242 : wxNotifyEvent( type, id )
11243 {
11244 m_topLeft = topLeft;
11245 m_bottomRight = bottomRight;
11246 m_selecting = sel;
11247 m_control = control;
11248 m_shift = shift;
11249 m_alt = alt;
11250 m_meta = meta;
11251
11252 SetEventObject(obj);
11253 }
11254
11255
11256 IMPLEMENT_DYNAMIC_CLASS(wxGridEditorCreatedEvent, wxCommandEvent)
11257
11258 wxGridEditorCreatedEvent::wxGridEditorCreatedEvent(int id, wxEventType type,
11259 wxObject* obj, int row,
11260 int col, wxControl* ctrl)
11261 : wxCommandEvent(type, id)
11262 {
11263 SetEventObject(obj);
11264 m_row = row;
11265 m_col = col;
11266 m_ctrl = ctrl;
11267 }
11268
11269 #endif // wxUSE_GRID