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