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