]> git.saurik.com Git - wxWidgets.git/blob - src/generic/grid.cpp
Fixed player dialog listbox size and app icon on wxGTK
[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
4519 m_waitForSlowClick = false;
4520
4521 m_rowResizeCursor = wxCursor( wxCURSOR_SIZENS );
4522 m_colResizeCursor = wxCursor( wxCURSOR_SIZEWE );
4523
4524 m_currentCellCoords = wxGridNoCellCoords;
4525
4526 ClearSelection();
4527
4528 m_selectionBackground = wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT);
4529 m_selectionForeground = wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT);
4530
4531 m_editable = true; // default for whole grid
4532
4533 m_inOnKeyDown = false;
4534 m_batchCount = 0;
4535
4536 m_extraWidth =
4537 m_extraHeight = 0;
4538
4539 m_scrollLineX = GRID_SCROLL_LINE_X;
4540 m_scrollLineY = GRID_SCROLL_LINE_Y;
4541 }
4542
4543 // ----------------------------------------------------------------------------
4544 // the idea is to call these functions only when necessary because they create
4545 // quite big arrays which eat memory mostly unnecessary - in particular, if
4546 // default widths/heights are used for all rows/columns, we may not use these
4547 // arrays at all
4548 //
4549 // with some extra code, it should be possible to only store the widths/heights
4550 // different from default ones (resulting in space savings for huge grids) but
4551 // this is not done currently
4552 // ----------------------------------------------------------------------------
4553
4554 void wxGrid::InitRowHeights()
4555 {
4556 m_rowHeights.Empty();
4557 m_rowBottoms.Empty();
4558
4559 m_rowHeights.Alloc( m_numRows );
4560 m_rowBottoms.Alloc( m_numRows );
4561
4562 m_rowHeights.Add( m_defaultRowHeight, m_numRows );
4563
4564 int rowBottom = 0;
4565 for ( int i = 0; i < m_numRows; i++ )
4566 {
4567 rowBottom += m_defaultRowHeight;
4568 m_rowBottoms.Add( rowBottom );
4569 }
4570 }
4571
4572 void wxGrid::InitColWidths()
4573 {
4574 m_colWidths.Empty();
4575 m_colRights.Empty();
4576
4577 m_colWidths.Alloc( m_numCols );
4578 m_colRights.Alloc( m_numCols );
4579
4580 m_colWidths.Add( m_defaultColWidth, m_numCols );
4581
4582 int colRight = 0;
4583 for ( int i = 0; i < m_numCols; i++ )
4584 {
4585 colRight = ( GetColPos( i ) + 1 ) * m_defaultColWidth;
4586 m_colRights.Add( colRight );
4587 }
4588 }
4589
4590 int wxGrid::GetColWidth(int col) const
4591 {
4592 return m_colWidths.IsEmpty() ? m_defaultColWidth : m_colWidths[col];
4593 }
4594
4595 int wxGrid::GetColLeft(int col) const
4596 {
4597 return m_colRights.IsEmpty() ? GetColPos( col ) * m_defaultColWidth
4598 : m_colRights[col] - m_colWidths[col];
4599 }
4600
4601 int wxGrid::GetColRight(int col) const
4602 {
4603 return m_colRights.IsEmpty() ? (GetColPos( col ) + 1) * m_defaultColWidth
4604 : m_colRights[col];
4605 }
4606
4607 int wxGrid::GetRowHeight(int row) const
4608 {
4609 return m_rowHeights.IsEmpty() ? m_defaultRowHeight : m_rowHeights[row];
4610 }
4611
4612 int wxGrid::GetRowTop(int row) const
4613 {
4614 return m_rowBottoms.IsEmpty() ? row * m_defaultRowHeight
4615 : m_rowBottoms[row] - m_rowHeights[row];
4616 }
4617
4618 int wxGrid::GetRowBottom(int row) const
4619 {
4620 return m_rowBottoms.IsEmpty() ? (row + 1) * m_defaultRowHeight
4621 : m_rowBottoms[row];
4622 }
4623
4624 void wxGrid::CalcDimensions()
4625 {
4626 // compute the size of the scrollable area
4627 int w = m_numCols > 0 ? GetColRight(GetColAt(m_numCols - 1)) : 0;
4628 int h = m_numRows > 0 ? GetRowBottom(m_numRows - 1) : 0;
4629
4630 w += m_extraWidth;
4631 h += m_extraHeight;
4632
4633 // take into account editor if shown
4634 if ( IsCellEditControlShown() )
4635 {
4636 int w2, h2;
4637 int r = m_currentCellCoords.GetRow();
4638 int c = m_currentCellCoords.GetCol();
4639 int x = GetColLeft(c);
4640 int y = GetRowTop(r);
4641
4642 // how big is the editor
4643 wxGridCellAttr* attr = GetCellAttr(r, c);
4644 wxGridCellEditor* editor = attr->GetEditor(this, r, c);
4645 editor->GetControl()->GetSize(&w2, &h2);
4646 w2 += x;
4647 h2 += y;
4648 if ( w2 > w )
4649 w = w2;
4650 if ( h2 > h )
4651 h = h2;
4652 editor->DecRef();
4653 attr->DecRef();
4654 }
4655
4656 // preserve (more or less) the previous position
4657 int x, y;
4658 GetViewStart( &x, &y );
4659
4660 // ensure the position is valid for the new scroll ranges
4661 if ( x >= w )
4662 x = wxMax( w - 1, 0 );
4663 if ( y >= h )
4664 y = wxMax( h - 1, 0 );
4665
4666 // do set scrollbar parameters
4667 SetScrollbars( m_scrollLineX, m_scrollLineY,
4668 GetScrollX(w), GetScrollY(h),
4669 x, y,
4670 GetBatchCount() != 0);
4671
4672 // if our OnSize() hadn't been called (it would if we have scrollbars), we
4673 // still must reposition the children
4674 CalcWindowSizes();
4675 }
4676
4677 void wxGrid::CalcWindowSizes()
4678 {
4679 // escape if the window is has not been fully created yet
4680
4681 if ( m_cornerLabelWin == NULL )
4682 return;
4683
4684 int cw, ch;
4685 GetClientSize( &cw, &ch );
4686
4687 // this block of code tries to work around the following problem: the grid
4688 // could have been just resized to have enough space to show the full grid
4689 // window contents without the scrollbars, but its client size could be
4690 // not big enough because the grid has the scrollbars right now and so the
4691 // scrollbars would remain even though we don't need them any more
4692 //
4693 // to prevent this from happening, check if we have enough space for
4694 // everything without the scrollbars and explicitly disable them then
4695 wxSize size = GetSize() - GetWindowBorderSize();
4696 if ( size != wxSize(cw, ch) )
4697 {
4698 // check if we have enough space for grid window after accounting for
4699 // the fixed size elements
4700 size.x -= m_rowLabelWidth;
4701 size.y -= m_colLabelHeight;
4702
4703 const wxSize vsize = m_gridWin->GetVirtualSize();
4704
4705 if ( size.x >= vsize.x && size.y >= vsize.y )
4706 {
4707 // yes, we do, so remove the scrollbars and use the new client size
4708 // (which should be the same as full window size - borders now)
4709 SetScrollbars(0, 0, 0, 0);
4710 GetClientSize(&cw, &ch);
4711 }
4712 }
4713
4714 if ( m_cornerLabelWin && m_cornerLabelWin->IsShown() )
4715 m_cornerLabelWin->SetSize( 0, 0, m_rowLabelWidth, m_colLabelHeight );
4716
4717 if ( m_colLabelWin && m_colLabelWin->IsShown() )
4718 m_colLabelWin->SetSize( m_rowLabelWidth, 0, cw - m_rowLabelWidth, m_colLabelHeight );
4719
4720 if ( m_rowLabelWin && m_rowLabelWin->IsShown() )
4721 m_rowLabelWin->SetSize( 0, m_colLabelHeight, m_rowLabelWidth, ch - m_colLabelHeight );
4722
4723 if ( m_gridWin && m_gridWin->IsShown() )
4724 m_gridWin->SetSize( m_rowLabelWidth, m_colLabelHeight, cw - m_rowLabelWidth, ch - m_colLabelHeight );
4725 }
4726
4727 // this is called when the grid table sends a message
4728 // to indicate that it has been redimensioned
4729 //
4730 bool wxGrid::Redimension( wxGridTableMessage& msg )
4731 {
4732 int i;
4733 bool result = false;
4734
4735 // Clear the attribute cache as the attribute might refer to a different
4736 // cell than stored in the cache after adding/removing rows/columns.
4737 ClearAttrCache();
4738
4739 // By the same reasoning, the editor should be dismissed if columns are
4740 // added or removed. And for consistency, it should IMHO always be
4741 // removed, not only if the cell "underneath" it actually changes.
4742 // For now, I intentionally do not save the editor's content as the
4743 // cell it might want to save that stuff to might no longer exist.
4744 HideCellEditControl();
4745
4746 #if 0
4747 // if we were using the default widths/heights so far, we must change them
4748 // now
4749 if ( m_colWidths.IsEmpty() )
4750 {
4751 InitColWidths();
4752 }
4753
4754 if ( m_rowHeights.IsEmpty() )
4755 {
4756 InitRowHeights();
4757 }
4758 #endif
4759
4760 switch ( msg.GetId() )
4761 {
4762 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED:
4763 {
4764 size_t pos = msg.GetCommandInt();
4765 int numRows = msg.GetCommandInt2();
4766
4767 m_numRows += numRows;
4768
4769 if ( !m_rowHeights.IsEmpty() )
4770 {
4771 m_rowHeights.Insert( m_defaultRowHeight, pos, numRows );
4772 m_rowBottoms.Insert( 0, pos, numRows );
4773
4774 int bottom = 0;
4775 if ( pos > 0 )
4776 bottom = m_rowBottoms[pos - 1];
4777
4778 for ( i = pos; i < m_numRows; i++ )
4779 {
4780 bottom += m_rowHeights[i];
4781 m_rowBottoms[i] = bottom;
4782 }
4783 }
4784
4785 if ( m_currentCellCoords == wxGridNoCellCoords )
4786 {
4787 // if we have just inserted cols into an empty grid the current
4788 // cell will be undefined...
4789 //
4790 SetCurrentCell( 0, 0 );
4791 }
4792
4793 if ( m_selection )
4794 m_selection->UpdateRows( pos, numRows );
4795 wxGridCellAttrProvider * attrProvider = m_table->GetAttrProvider();
4796 if (attrProvider)
4797 attrProvider->UpdateAttrRows( pos, numRows );
4798
4799 if ( !GetBatchCount() )
4800 {
4801 CalcDimensions();
4802 m_rowLabelWin->Refresh();
4803 }
4804 }
4805 result = true;
4806 break;
4807
4808 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED:
4809 {
4810 int numRows = msg.GetCommandInt();
4811 int oldNumRows = m_numRows;
4812 m_numRows += numRows;
4813
4814 if ( !m_rowHeights.IsEmpty() )
4815 {
4816 m_rowHeights.Add( m_defaultRowHeight, numRows );
4817 m_rowBottoms.Add( 0, numRows );
4818
4819 int bottom = 0;
4820 if ( oldNumRows > 0 )
4821 bottom = m_rowBottoms[oldNumRows - 1];
4822
4823 for ( i = oldNumRows; i < m_numRows; i++ )
4824 {
4825 bottom += m_rowHeights[i];
4826 m_rowBottoms[i] = bottom;
4827 }
4828 }
4829
4830 if ( m_currentCellCoords == wxGridNoCellCoords )
4831 {
4832 // if we have just inserted cols into an empty grid the current
4833 // cell will be undefined...
4834 //
4835 SetCurrentCell( 0, 0 );
4836 }
4837
4838 if ( !GetBatchCount() )
4839 {
4840 CalcDimensions();
4841 m_rowLabelWin->Refresh();
4842 }
4843 }
4844 result = true;
4845 break;
4846
4847 case wxGRIDTABLE_NOTIFY_ROWS_DELETED:
4848 {
4849 size_t pos = msg.GetCommandInt();
4850 int numRows = msg.GetCommandInt2();
4851 m_numRows -= numRows;
4852
4853 if ( !m_rowHeights.IsEmpty() )
4854 {
4855 m_rowHeights.RemoveAt( pos, numRows );
4856 m_rowBottoms.RemoveAt( pos, numRows );
4857
4858 int h = 0;
4859 for ( i = 0; i < m_numRows; i++ )
4860 {
4861 h += m_rowHeights[i];
4862 m_rowBottoms[i] = h;
4863 }
4864 }
4865
4866 if ( !m_numRows )
4867 {
4868 m_currentCellCoords = wxGridNoCellCoords;
4869 }
4870 else
4871 {
4872 if ( m_currentCellCoords.GetRow() >= m_numRows )
4873 m_currentCellCoords.Set( 0, 0 );
4874 }
4875
4876 if ( m_selection )
4877 m_selection->UpdateRows( pos, -((int)numRows) );
4878 wxGridCellAttrProvider * attrProvider = m_table->GetAttrProvider();
4879 if (attrProvider)
4880 {
4881 attrProvider->UpdateAttrRows( pos, -((int)numRows) );
4882
4883 // ifdef'd out following patch from Paul Gammans
4884 #if 0
4885 // No need to touch column attributes, unless we
4886 // removed _all_ rows, in this case, we remove
4887 // all column attributes.
4888 // I hate to do this here, but the
4889 // needed data is not available inside UpdateAttrRows.
4890 if ( !GetNumberRows() )
4891 attrProvider->UpdateAttrCols( 0, -GetNumberCols() );
4892 #endif
4893 }
4894
4895 if ( !GetBatchCount() )
4896 {
4897 CalcDimensions();
4898 m_rowLabelWin->Refresh();
4899 }
4900 }
4901 result = true;
4902 break;
4903
4904 case wxGRIDTABLE_NOTIFY_COLS_INSERTED:
4905 {
4906 size_t pos = msg.GetCommandInt();
4907 int numCols = msg.GetCommandInt2();
4908 m_numCols += numCols;
4909
4910 if ( !m_colAt.IsEmpty() )
4911 {
4912 //Shift the column IDs
4913 int i;
4914 for ( i = 0; i < m_numCols - numCols; i++ )
4915 {
4916 if ( m_colAt[i] >= (int)pos )
4917 m_colAt[i] += numCols;
4918 }
4919
4920 m_colAt.Insert( pos, pos, numCols );
4921
4922 //Set the new columns' positions
4923 for ( i = pos + 1; i < (int)pos + numCols; i++ )
4924 {
4925 m_colAt[i] = i;
4926 }
4927 }
4928
4929 if ( !m_colWidths.IsEmpty() )
4930 {
4931 m_colWidths.Insert( m_defaultColWidth, pos, numCols );
4932 m_colRights.Insert( 0, pos, numCols );
4933
4934 int right = 0;
4935 if ( pos > 0 )
4936 right = m_colRights[GetColAt( pos - 1 )];
4937
4938 int colPos;
4939 for ( colPos = pos; colPos < m_numCols; colPos++ )
4940 {
4941 i = GetColAt( colPos );
4942
4943 right += m_colWidths[i];
4944 m_colRights[i] = right;
4945 }
4946 }
4947
4948 if ( m_currentCellCoords == wxGridNoCellCoords )
4949 {
4950 // if we have just inserted cols into an empty grid the current
4951 // cell will be undefined...
4952 //
4953 SetCurrentCell( 0, 0 );
4954 }
4955
4956 if ( m_selection )
4957 m_selection->UpdateCols( pos, numCols );
4958 wxGridCellAttrProvider * attrProvider = m_table->GetAttrProvider();
4959 if (attrProvider)
4960 attrProvider->UpdateAttrCols( pos, numCols );
4961 if ( !GetBatchCount() )
4962 {
4963 CalcDimensions();
4964 m_colLabelWin->Refresh();
4965 }
4966 }
4967 result = true;
4968 break;
4969
4970 case wxGRIDTABLE_NOTIFY_COLS_APPENDED:
4971 {
4972 int numCols = msg.GetCommandInt();
4973 int oldNumCols = m_numCols;
4974 m_numCols += numCols;
4975
4976 if ( !m_colAt.IsEmpty() )
4977 {
4978 m_colAt.Add( 0, numCols );
4979
4980 //Set the new columns' positions
4981 int i;
4982 for ( i = oldNumCols; i < m_numCols; i++ )
4983 {
4984 m_colAt[i] = i;
4985 }
4986 }
4987
4988 if ( !m_colWidths.IsEmpty() )
4989 {
4990 m_colWidths.Add( m_defaultColWidth, numCols );
4991 m_colRights.Add( 0, numCols );
4992
4993 int right = 0;
4994 if ( oldNumCols > 0 )
4995 right = m_colRights[GetColAt( oldNumCols - 1 )];
4996
4997 int colPos;
4998 for ( colPos = oldNumCols; colPos < m_numCols; colPos++ )
4999 {
5000 i = GetColAt( colPos );
5001
5002 right += m_colWidths[i];
5003 m_colRights[i] = right;
5004 }
5005 }
5006
5007 if ( m_currentCellCoords == wxGridNoCellCoords )
5008 {
5009 // if we have just inserted cols into an empty grid the current
5010 // cell will be undefined...
5011 //
5012 SetCurrentCell( 0, 0 );
5013 }
5014 if ( !GetBatchCount() )
5015 {
5016 CalcDimensions();
5017 m_colLabelWin->Refresh();
5018 }
5019 }
5020 result = true;
5021 break;
5022
5023 case wxGRIDTABLE_NOTIFY_COLS_DELETED:
5024 {
5025 size_t pos = msg.GetCommandInt();
5026 int numCols = msg.GetCommandInt2();
5027 m_numCols -= numCols;
5028
5029 if ( !m_colAt.IsEmpty() )
5030 {
5031 int colID = GetColAt( pos );
5032
5033 m_colAt.RemoveAt( pos, numCols );
5034
5035 //Shift the column IDs
5036 int colPos;
5037 for ( colPos = 0; colPos < m_numCols; colPos++ )
5038 {
5039 if ( m_colAt[colPos] > colID )
5040 m_colAt[colPos] -= numCols;
5041 }
5042 }
5043
5044 if ( !m_colWidths.IsEmpty() )
5045 {
5046 m_colWidths.RemoveAt( pos, numCols );
5047 m_colRights.RemoveAt( pos, numCols );
5048
5049 int w = 0;
5050 int colPos;
5051 for ( colPos = 0; colPos < m_numCols; colPos++ )
5052 {
5053 i = GetColAt( colPos );
5054
5055 w += m_colWidths[i];
5056 m_colRights[i] = w;
5057 }
5058 }
5059
5060 if ( !m_numCols )
5061 {
5062 m_currentCellCoords = wxGridNoCellCoords;
5063 }
5064 else
5065 {
5066 if ( m_currentCellCoords.GetCol() >= m_numCols )
5067 m_currentCellCoords.Set( 0, 0 );
5068 }
5069
5070 if ( m_selection )
5071 m_selection->UpdateCols( pos, -((int)numCols) );
5072 wxGridCellAttrProvider * attrProvider = m_table->GetAttrProvider();
5073 if (attrProvider)
5074 {
5075 attrProvider->UpdateAttrCols( pos, -((int)numCols) );
5076
5077 // ifdef'd out following patch from Paul Gammans
5078 #if 0
5079 // No need to touch row attributes, unless we
5080 // removed _all_ columns, in this case, we remove
5081 // all row attributes.
5082 // I hate to do this here, but the
5083 // needed data is not available inside UpdateAttrCols.
5084 if ( !GetNumberCols() )
5085 attrProvider->UpdateAttrRows( 0, -GetNumberRows() );
5086 #endif
5087 }
5088
5089 if ( !GetBatchCount() )
5090 {
5091 CalcDimensions();
5092 m_colLabelWin->Refresh();
5093 }
5094 }
5095 result = true;
5096 break;
5097 }
5098
5099 if (result && !GetBatchCount() )
5100 m_gridWin->Refresh();
5101
5102 return result;
5103 }
5104
5105 wxArrayInt wxGrid::CalcRowLabelsExposed( const wxRegion& reg ) const
5106 {
5107 wxRegionIterator iter( reg );
5108 wxRect r;
5109
5110 wxArrayInt rowlabels;
5111
5112 int top, bottom;
5113 while ( iter )
5114 {
5115 r = iter.GetRect();
5116
5117 // TODO: remove this when we can...
5118 // There is a bug in wxMotif that gives garbage update
5119 // rectangles if you jump-scroll a long way by clicking the
5120 // scrollbar with middle button. This is a work-around
5121 //
5122 #if defined(__WXMOTIF__)
5123 int cw, ch;
5124 m_gridWin->GetClientSize( &cw, &ch );
5125 if ( r.GetTop() > ch )
5126 r.SetTop( 0 );
5127 r.SetBottom( wxMin( r.GetBottom(), ch ) );
5128 #endif
5129
5130 // logical bounds of update region
5131 //
5132 int dummy;
5133 CalcUnscrolledPosition( 0, r.GetTop(), &dummy, &top );
5134 CalcUnscrolledPosition( 0, r.GetBottom(), &dummy, &bottom );
5135
5136 // find the row labels within these bounds
5137 //
5138 int row;
5139 for ( row = internalYToRow(top); row < m_numRows; row++ )
5140 {
5141 if ( GetRowBottom(row) < top )
5142 continue;
5143
5144 if ( GetRowTop(row) > bottom )
5145 break;
5146
5147 rowlabels.Add( row );
5148 }
5149
5150 ++iter;
5151 }
5152
5153 return rowlabels;
5154 }
5155
5156 wxArrayInt wxGrid::CalcColLabelsExposed( const wxRegion& reg ) const
5157 {
5158 wxRegionIterator iter( reg );
5159 wxRect r;
5160
5161 wxArrayInt colLabels;
5162
5163 int left, right;
5164 while ( iter )
5165 {
5166 r = iter.GetRect();
5167
5168 // TODO: remove this when we can...
5169 // There is a bug in wxMotif that gives garbage update
5170 // rectangles if you jump-scroll a long way by clicking the
5171 // scrollbar with middle button. This is a work-around
5172 //
5173 #if defined(__WXMOTIF__)
5174 int cw, ch;
5175 m_gridWin->GetClientSize( &cw, &ch );
5176 if ( r.GetLeft() > cw )
5177 r.SetLeft( 0 );
5178 r.SetRight( wxMin( r.GetRight(), cw ) );
5179 #endif
5180
5181 // logical bounds of update region
5182 //
5183 int dummy;
5184 CalcUnscrolledPosition( r.GetLeft(), 0, &left, &dummy );
5185 CalcUnscrolledPosition( r.GetRight(), 0, &right, &dummy );
5186
5187 // find the cells within these bounds
5188 //
5189 int col;
5190 int colPos;
5191 for ( colPos = GetColPos( internalXToCol(left) ); colPos < m_numCols; colPos++ )
5192 {
5193 col = GetColAt( colPos );
5194
5195 if ( GetColRight(col) < left )
5196 continue;
5197
5198 if ( GetColLeft(col) > right )
5199 break;
5200
5201 colLabels.Add( col );
5202 }
5203
5204 ++iter;
5205 }
5206
5207 return colLabels;
5208 }
5209
5210 wxGridCellCoordsArray wxGrid::CalcCellsExposed( const wxRegion& reg ) const
5211 {
5212 wxRegionIterator iter( reg );
5213 wxRect r;
5214
5215 wxGridCellCoordsArray cellsExposed;
5216
5217 int left, top, right, bottom;
5218 while ( iter )
5219 {
5220 r = iter.GetRect();
5221
5222 // TODO: remove this when we can...
5223 // There is a bug in wxMotif that gives garbage update
5224 // rectangles if you jump-scroll a long way by clicking the
5225 // scrollbar with middle button. This is a work-around
5226 //
5227 #if defined(__WXMOTIF__)
5228 int cw, ch;
5229 m_gridWin->GetClientSize( &cw, &ch );
5230 if ( r.GetTop() > ch ) r.SetTop( 0 );
5231 if ( r.GetLeft() > cw ) r.SetLeft( 0 );
5232 r.SetRight( wxMin( r.GetRight(), cw ) );
5233 r.SetBottom( wxMin( r.GetBottom(), ch ) );
5234 #endif
5235
5236 // logical bounds of update region
5237 //
5238 CalcUnscrolledPosition( r.GetLeft(), r.GetTop(), &left, &top );
5239 CalcUnscrolledPosition( r.GetRight(), r.GetBottom(), &right, &bottom );
5240
5241 // find the cells within these bounds
5242 //
5243 int row, col;
5244 for ( row = internalYToRow(top); row < m_numRows; row++ )
5245 {
5246 if ( GetRowBottom(row) <= top )
5247 continue;
5248
5249 if ( GetRowTop(row) > bottom )
5250 break;
5251
5252 int colPos;
5253 for ( colPos = GetColPos( internalXToCol(left) ); colPos < m_numCols; colPos++ )
5254 {
5255 col = GetColAt( colPos );
5256
5257 if ( GetColRight(col) <= left )
5258 continue;
5259
5260 if ( GetColLeft(col) > right )
5261 break;
5262
5263 cellsExposed.Add( wxGridCellCoords( row, col ) );
5264 }
5265 }
5266
5267 ++iter;
5268 }
5269
5270 return cellsExposed;
5271 }
5272
5273
5274 void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent& event )
5275 {
5276 int x, y, row;
5277 wxPoint pos( event.GetPosition() );
5278 CalcUnscrolledPosition( pos.x, pos.y, &x, &y );
5279
5280 if ( event.Dragging() )
5281 {
5282 if (!m_isDragging)
5283 {
5284 m_isDragging = true;
5285 m_rowLabelWin->CaptureMouse();
5286 }
5287
5288 if ( event.LeftIsDown() )
5289 {
5290 switch ( m_cursorMode )
5291 {
5292 case WXGRID_CURSOR_RESIZE_ROW:
5293 {
5294 int cw, ch, left, dummy;
5295 m_gridWin->GetClientSize( &cw, &ch );
5296 CalcUnscrolledPosition( 0, 0, &left, &dummy );
5297
5298 wxClientDC dc( m_gridWin );
5299 PrepareDC( dc );
5300 y = wxMax( y,
5301 GetRowTop(m_dragRowOrCol) +
5302 GetRowMinimalHeight(m_dragRowOrCol) );
5303 dc.SetLogicalFunction(wxINVERT);
5304 if ( m_dragLastPos >= 0 )
5305 {
5306 dc.DrawLine( left, m_dragLastPos, left+cw, m_dragLastPos );
5307 }
5308 dc.DrawLine( left, y, left+cw, y );
5309 m_dragLastPos = y;
5310 }
5311 break;
5312
5313 case WXGRID_CURSOR_SELECT_ROW:
5314 {
5315 if ( (row = YToRow( y )) >= 0 )
5316 {
5317 if ( m_selection )
5318 {
5319 m_selection->SelectRow( row,
5320 event.ControlDown(),
5321 event.ShiftDown(),
5322 event.AltDown(),
5323 event.MetaDown() );
5324 }
5325 }
5326 }
5327 break;
5328
5329 // default label to suppress warnings about "enumeration value
5330 // 'xxx' not handled in switch
5331 default:
5332 break;
5333 }
5334 }
5335 return;
5336 }
5337
5338 if ( m_isDragging && (event.Entering() || event.Leaving()) )
5339 return;
5340
5341 if (m_isDragging)
5342 {
5343 if (m_rowLabelWin->HasCapture())
5344 m_rowLabelWin->ReleaseMouse();
5345 m_isDragging = false;
5346 }
5347
5348 // ------------ Entering or leaving the window
5349 //
5350 if ( event.Entering() || event.Leaving() )
5351 {
5352 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_rowLabelWin);
5353 }
5354
5355 // ------------ Left button pressed
5356 //
5357 else if ( event.LeftDown() )
5358 {
5359 // don't send a label click event for a hit on the
5360 // edge of the row label - this is probably the user
5361 // wanting to resize the row
5362 //
5363 if ( YToEdgeOfRow(y) < 0 )
5364 {
5365 row = YToRow(y);
5366 if ( row >= 0 &&
5367 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK, row, -1, event ) )
5368 {
5369 if ( !event.ShiftDown() && !event.CmdDown() )
5370 ClearSelection();
5371 if ( m_selection )
5372 {
5373 if ( event.ShiftDown() )
5374 {
5375 m_selection->SelectBlock( m_currentCellCoords.GetRow(),
5376 0,
5377 row,
5378 GetNumberCols() - 1,
5379 event.ControlDown(),
5380 event.ShiftDown(),
5381 event.AltDown(),
5382 event.MetaDown() );
5383 }
5384 else
5385 {
5386 m_selection->SelectRow( row,
5387 event.ControlDown(),
5388 event.ShiftDown(),
5389 event.AltDown(),
5390 event.MetaDown() );
5391 }
5392 }
5393
5394 ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW, m_rowLabelWin);
5395 }
5396 }
5397 else
5398 {
5399 // starting to drag-resize a row
5400 if ( CanDragRowSize() )
5401 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW, m_rowLabelWin);
5402 }
5403 }
5404
5405 // ------------ Left double click
5406 //
5407 else if (event.LeftDClick() )
5408 {
5409 row = YToEdgeOfRow(y);
5410 if ( row < 0 )
5411 {
5412 row = YToRow(y);
5413 if ( row >=0 &&
5414 !SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK, row, -1, event ) )
5415 {
5416 // no default action at the moment
5417 }
5418 }
5419 else
5420 {
5421 // adjust row height depending on label text
5422 AutoSizeRowLabelSize( row );
5423
5424 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_colLabelWin);
5425 m_dragLastPos = -1;
5426 }
5427 }
5428
5429 // ------------ Left button released
5430 //
5431 else if ( event.LeftUp() )
5432 {
5433 if ( m_cursorMode == WXGRID_CURSOR_RESIZE_ROW )
5434 {
5435 DoEndDragResizeRow();
5436
5437 // Note: we are ending the event *after* doing
5438 // default processing in this case
5439 //
5440 SendEvent( wxEVT_GRID_ROW_SIZE, m_dragRowOrCol, -1, event );
5441 }
5442
5443 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_rowLabelWin);
5444 m_dragLastPos = -1;
5445 }
5446
5447 // ------------ Right button down
5448 //
5449 else if ( event.RightDown() )
5450 {
5451 row = YToRow(y);
5452 if ( row >=0 &&
5453 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK, row, -1, event ) )
5454 {
5455 // no default action at the moment
5456 }
5457 }
5458
5459 // ------------ Right double click
5460 //
5461 else if ( event.RightDClick() )
5462 {
5463 row = YToRow(y);
5464 if ( row >= 0 &&
5465 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK, row, -1, event ) )
5466 {
5467 // no default action at the moment
5468 }
5469 }
5470
5471 // ------------ No buttons down and mouse moving
5472 //
5473 else if ( event.Moving() )
5474 {
5475 m_dragRowOrCol = YToEdgeOfRow( y );
5476 if ( m_dragRowOrCol >= 0 )
5477 {
5478 if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
5479 {
5480 // don't capture the mouse yet
5481 if ( CanDragRowSize() )
5482 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW, m_rowLabelWin, false);
5483 }
5484 }
5485 else if ( m_cursorMode != WXGRID_CURSOR_SELECT_CELL )
5486 {
5487 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_rowLabelWin, false);
5488 }
5489 }
5490 }
5491
5492 void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent& event )
5493 {
5494 int x, y, col;
5495 wxPoint pos( event.GetPosition() );
5496 CalcUnscrolledPosition( pos.x, pos.y, &x, &y );
5497
5498 if ( event.Dragging() )
5499 {
5500 if (!m_isDragging)
5501 {
5502 m_isDragging = true;
5503 m_colLabelWin->CaptureMouse();
5504
5505 if ( m_cursorMode == WXGRID_CURSOR_MOVE_COL )
5506 m_dragRowOrCol = XToCol( x );
5507 }
5508
5509 if ( event.LeftIsDown() )
5510 {
5511 switch ( m_cursorMode )
5512 {
5513 case WXGRID_CURSOR_RESIZE_COL:
5514 {
5515 int cw, ch, dummy, top;
5516 m_gridWin->GetClientSize( &cw, &ch );
5517 CalcUnscrolledPosition( 0, 0, &dummy, &top );
5518
5519 wxClientDC dc( m_gridWin );
5520 PrepareDC( dc );
5521
5522 x = wxMax( x, GetColLeft(m_dragRowOrCol) +
5523 GetColMinimalWidth(m_dragRowOrCol));
5524 dc.SetLogicalFunction(wxINVERT);
5525 if ( m_dragLastPos >= 0 )
5526 {
5527 dc.DrawLine( m_dragLastPos, top, m_dragLastPos, top + ch );
5528 }
5529 dc.DrawLine( x, top, x, top + ch );
5530 m_dragLastPos = x;
5531 }
5532 break;
5533
5534 case WXGRID_CURSOR_SELECT_COL:
5535 {
5536 if ( (col = XToCol( x )) >= 0 )
5537 {
5538 if ( m_selection )
5539 {
5540 m_selection->SelectCol( col,
5541 event.ControlDown(),
5542 event.ShiftDown(),
5543 event.AltDown(),
5544 event.MetaDown() );
5545 }
5546 }
5547 }
5548 break;
5549
5550 case WXGRID_CURSOR_MOVE_COL:
5551 {
5552 if ( x < 0 )
5553 m_moveToCol = GetColAt( 0 );
5554 else
5555 m_moveToCol = XToCol( x );
5556
5557 int markerX;
5558
5559 if ( m_moveToCol < 0 )
5560 markerX = GetColRight( GetColAt( m_numCols - 1 ) );
5561 else
5562 markerX = GetColLeft( m_moveToCol );
5563
5564 if ( markerX != m_dragLastPos )
5565 {
5566 wxClientDC dc( m_colLabelWin );
5567
5568 int cw, ch;
5569 m_colLabelWin->GetClientSize( &cw, &ch );
5570
5571 markerX++;
5572
5573 //Clean up the last indicator
5574 if ( m_dragLastPos >= 0 )
5575 {
5576 wxPen pen( m_colLabelWin->GetBackgroundColour(), 2 );
5577 dc.SetPen(pen);
5578 dc.DrawLine( m_dragLastPos + 1, 0, m_dragLastPos + 1, ch );
5579 dc.SetPen(wxNullPen);
5580
5581 if ( XToCol( m_dragLastPos ) != -1 )
5582 DrawColLabel( dc, XToCol( m_dragLastPos ) );
5583 }
5584
5585 //Moving to the same place? Don't draw a marker
5586 if ( (m_moveToCol == m_dragRowOrCol)
5587 || (GetColPos( m_moveToCol ) == GetColPos( m_dragRowOrCol ) + 1)
5588 || (m_moveToCol < 0 && m_dragRowOrCol == GetColAt( m_numCols - 1 )))
5589 {
5590 m_dragLastPos = -1;
5591 return;
5592 }
5593
5594 //Draw the marker
5595 wxPen pen( *wxBLUE, 2 );
5596 dc.SetPen(pen);
5597
5598 dc.DrawLine( markerX, 0, markerX, ch );
5599
5600 dc.SetPen(wxNullPen);
5601
5602 m_dragLastPos = markerX - 1;
5603 }
5604 }
5605 break;
5606
5607 // default label to suppress warnings about "enumeration value
5608 // 'xxx' not handled in switch
5609 default:
5610 break;
5611 }
5612 }
5613 return;
5614 }
5615
5616 if ( m_isDragging && (event.Entering() || event.Leaving()) )
5617 return;
5618
5619 if (m_isDragging)
5620 {
5621 if (m_colLabelWin->HasCapture())
5622 m_colLabelWin->ReleaseMouse();
5623 m_isDragging = false;
5624 }
5625
5626 // ------------ Entering or leaving the window
5627 //
5628 if ( event.Entering() || event.Leaving() )
5629 {
5630 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_colLabelWin);
5631 }
5632
5633 // ------------ Left button pressed
5634 //
5635 else if ( event.LeftDown() )
5636 {
5637 // don't send a label click event for a hit on the
5638 // edge of the col label - this is probably the user
5639 // wanting to resize the col
5640 //
5641 if ( XToEdgeOfCol(x) < 0 )
5642 {
5643 col = XToCol(x);
5644 if ( col >= 0 &&
5645 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK, -1, col, event ) )
5646 {
5647 if ( m_canDragColMove )
5648 {
5649 //Show button as pressed
5650 wxClientDC dc( m_colLabelWin );
5651 int colLeft = GetColLeft( col );
5652 int colRight = GetColRight( col ) - 1;
5653 dc.SetPen( wxPen( m_colLabelWin->GetBackgroundColour(), 1 ) );
5654 dc.DrawLine( colLeft, 1, colLeft, m_colLabelHeight-1 );
5655 dc.DrawLine( colLeft, 1, colRight, 1 );
5656
5657 ChangeCursorMode(WXGRID_CURSOR_MOVE_COL, m_colLabelWin);
5658 }
5659 else
5660 {
5661 if ( !event.ShiftDown() && !event.CmdDown() )
5662 ClearSelection();
5663 if ( m_selection )
5664 {
5665 if ( event.ShiftDown() )
5666 {
5667 m_selection->SelectBlock( 0,
5668 m_currentCellCoords.GetCol(),
5669 GetNumberRows() - 1, col,
5670 event.ControlDown(),
5671 event.ShiftDown(),
5672 event.AltDown(),
5673 event.MetaDown() );
5674 }
5675 else
5676 {
5677 m_selection->SelectCol( col,
5678 event.ControlDown(),
5679 event.ShiftDown(),
5680 event.AltDown(),
5681 event.MetaDown() );
5682 }
5683 }
5684
5685 ChangeCursorMode(WXGRID_CURSOR_SELECT_COL, m_colLabelWin);
5686 }
5687 }
5688 }
5689 else
5690 {
5691 // starting to drag-resize a col
5692 //
5693 if ( CanDragColSize() )
5694 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL, m_colLabelWin);
5695 }
5696 }
5697
5698 // ------------ Left double click
5699 //
5700 if ( event.LeftDClick() )
5701 {
5702 col = XToEdgeOfCol(x);
5703 if ( col < 0 )
5704 {
5705 col = XToCol(x);
5706 if ( col >= 0 &&
5707 ! SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK, -1, col, event ) )
5708 {
5709 // no default action at the moment
5710 }
5711 }
5712 else
5713 {
5714 // adjust column width depending on label text
5715 AutoSizeColLabelSize( col );
5716
5717 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_colLabelWin);
5718 m_dragLastPos = -1;
5719 }
5720 }
5721
5722 // ------------ Left button released
5723 //
5724 else if ( event.LeftUp() )
5725 {
5726 switch ( m_cursorMode )
5727 {
5728 case WXGRID_CURSOR_RESIZE_COL:
5729 DoEndDragResizeCol();
5730
5731 // Note: we are ending the event *after* doing
5732 // default processing in this case
5733 //
5734 SendEvent( wxEVT_GRID_COL_SIZE, -1, m_dragRowOrCol, event );
5735 break;
5736
5737 case WXGRID_CURSOR_MOVE_COL:
5738 DoEndDragMoveCol();
5739
5740 SendEvent( wxEVT_GRID_COL_MOVE, -1, m_dragRowOrCol, event );
5741 break;
5742
5743 case WXGRID_CURSOR_SELECT_COL:
5744 case WXGRID_CURSOR_SELECT_CELL:
5745 case WXGRID_CURSOR_RESIZE_ROW:
5746 case WXGRID_CURSOR_SELECT_ROW:
5747 // nothing to do (?)
5748 break;
5749 }
5750
5751 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_colLabelWin);
5752 m_dragLastPos = -1;
5753 }
5754
5755 // ------------ Right button down
5756 //
5757 else if ( event.RightDown() )
5758 {
5759 col = XToCol(x);
5760 if ( col >= 0 &&
5761 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK, -1, col, event ) )
5762 {
5763 // no default action at the moment
5764 }
5765 }
5766
5767 // ------------ Right double click
5768 //
5769 else if ( event.RightDClick() )
5770 {
5771 col = XToCol(x);
5772 if ( col >= 0 &&
5773 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK, -1, col, event ) )
5774 {
5775 // no default action at the moment
5776 }
5777 }
5778
5779 // ------------ No buttons down and mouse moving
5780 //
5781 else if ( event.Moving() )
5782 {
5783 m_dragRowOrCol = XToEdgeOfCol( x );
5784 if ( m_dragRowOrCol >= 0 )
5785 {
5786 if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
5787 {
5788 // don't capture the cursor yet
5789 if ( CanDragColSize() )
5790 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL, m_colLabelWin, false);
5791 }
5792 }
5793 else if ( m_cursorMode != WXGRID_CURSOR_SELECT_CELL )
5794 {
5795 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_colLabelWin, false);
5796 }
5797 }
5798 }
5799
5800 void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent& event )
5801 {
5802 if ( event.LeftDown() )
5803 {
5804 // indicate corner label by having both row and
5805 // col args == -1
5806 //
5807 if ( !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK, -1, -1, event ) )
5808 {
5809 SelectAll();
5810 }
5811 }
5812 else if ( event.LeftDClick() )
5813 {
5814 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK, -1, -1, event );
5815 }
5816 else if ( event.RightDown() )
5817 {
5818 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK, -1, -1, event ) )
5819 {
5820 // no default action at the moment
5821 }
5822 }
5823 else if ( event.RightDClick() )
5824 {
5825 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK, -1, -1, event ) )
5826 {
5827 // no default action at the moment
5828 }
5829 }
5830 }
5831
5832 void wxGrid::CancelMouseCapture()
5833 {
5834 // cancel operation currently in progress, whatever it is
5835 if ( m_winCapture )
5836 {
5837 m_isDragging = false;
5838 m_cursorMode = WXGRID_CURSOR_SELECT_CELL;
5839 m_winCapture->SetCursor( *wxSTANDARD_CURSOR );
5840 m_winCapture = NULL;
5841
5842 // remove traces of whatever we drew on screen
5843 Refresh();
5844 }
5845 }
5846
5847 void wxGrid::ChangeCursorMode(CursorMode mode,
5848 wxWindow *win,
5849 bool captureMouse)
5850 {
5851 #ifdef __WXDEBUG__
5852 static const wxChar *cursorModes[] =
5853 {
5854 _T("SELECT_CELL"),
5855 _T("RESIZE_ROW"),
5856 _T("RESIZE_COL"),
5857 _T("SELECT_ROW"),
5858 _T("SELECT_COL"),
5859 _T("MOVE_COL"),
5860 };
5861
5862 wxLogTrace(_T("grid"),
5863 _T("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
5864 win == m_colLabelWin ? _T("colLabelWin")
5865 : win ? _T("rowLabelWin")
5866 : _T("gridWin"),
5867 cursorModes[m_cursorMode], cursorModes[mode]);
5868 #endif
5869
5870 if ( mode == m_cursorMode &&
5871 win == m_winCapture &&
5872 captureMouse == (m_winCapture != NULL))
5873 return;
5874
5875 if ( !win )
5876 {
5877 // by default use the grid itself
5878 win = m_gridWin;
5879 }
5880
5881 if ( m_winCapture )
5882 {
5883 if (m_winCapture->HasCapture())
5884 m_winCapture->ReleaseMouse();
5885 m_winCapture = (wxWindow *)NULL;
5886 }
5887
5888 m_cursorMode = mode;
5889
5890 switch ( m_cursorMode )
5891 {
5892 case WXGRID_CURSOR_RESIZE_ROW:
5893 win->SetCursor( m_rowResizeCursor );
5894 break;
5895
5896 case WXGRID_CURSOR_RESIZE_COL:
5897 win->SetCursor( m_colResizeCursor );
5898 break;
5899
5900 case WXGRID_CURSOR_MOVE_COL:
5901 win->SetCursor( wxCursor(wxCURSOR_HAND) );
5902 break;
5903
5904 default:
5905 win->SetCursor( *wxSTANDARD_CURSOR );
5906 break;
5907 }
5908
5909 // we need to capture mouse when resizing
5910 bool resize = m_cursorMode == WXGRID_CURSOR_RESIZE_ROW ||
5911 m_cursorMode == WXGRID_CURSOR_RESIZE_COL;
5912
5913 if ( captureMouse && resize )
5914 {
5915 win->CaptureMouse();
5916 m_winCapture = win;
5917 }
5918 }
5919
5920 void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent& event )
5921 {
5922 int x, y;
5923 wxPoint pos( event.GetPosition() );
5924 CalcUnscrolledPosition( pos.x, pos.y, &x, &y );
5925
5926 wxGridCellCoords coords;
5927 XYToCell( x, y, coords );
5928
5929 int cell_rows, cell_cols;
5930 bool isFirstDrag = !m_isDragging;
5931 GetCellSize( coords.GetRow(), coords.GetCol(), &cell_rows, &cell_cols );
5932 if ((cell_rows < 0) || (cell_cols < 0))
5933 {
5934 coords.SetRow(coords.GetRow() + cell_rows);
5935 coords.SetCol(coords.GetCol() + cell_cols);
5936 }
5937
5938 if ( event.Dragging() )
5939 {
5940 //wxLogDebug("pos(%d, %d) coords(%d, %d)", pos.x, pos.y, coords.GetRow(), coords.GetCol());
5941
5942 // Don't start doing anything until the mouse has been dragged at
5943 // least 3 pixels in any direction...
5944 if (! m_isDragging)
5945 {
5946 if (m_startDragPos == wxDefaultPosition)
5947 {
5948 m_startDragPos = pos;
5949 return;
5950 }
5951 if (abs(m_startDragPos.x - pos.x) < 4 && abs(m_startDragPos.y - pos.y) < 4)
5952 return;
5953 }
5954
5955 m_isDragging = true;
5956 if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
5957 {
5958 // Hide the edit control, so it
5959 // won't interfere with drag-shrinking.
5960 if ( IsCellEditControlShown() )
5961 {
5962 HideCellEditControl();
5963 SaveEditControlValue();
5964 }
5965
5966 if ( coords != wxGridNoCellCoords )
5967 {
5968 if ( event.CmdDown() )
5969 {
5970 if ( m_selectingKeyboard == wxGridNoCellCoords)
5971 m_selectingKeyboard = coords;
5972 HighlightBlock( m_selectingKeyboard, coords );
5973 }
5974 else if ( CanDragCell() )
5975 {
5976 if ( isFirstDrag )
5977 {
5978 if ( m_selectingKeyboard == wxGridNoCellCoords)
5979 m_selectingKeyboard = coords;
5980
5981 SendEvent( wxEVT_GRID_CELL_BEGIN_DRAG,
5982 coords.GetRow(),
5983 coords.GetCol(),
5984 event );
5985 return;
5986 }
5987 }
5988 else
5989 {
5990 if ( !IsSelection() )
5991 {
5992 HighlightBlock( coords, coords );
5993 }
5994 else
5995 {
5996 HighlightBlock( m_currentCellCoords, coords );
5997 }
5998 }
5999
6000 if (! IsVisible(coords))
6001 {
6002 MakeCellVisible(coords);
6003 // TODO: need to introduce a delay or something here. The
6004 // scrolling is way to fast, at least on MSW - also on GTK.
6005 }
6006 }
6007 // Have we captured the mouse yet?
6008 if (! m_winCapture)
6009 {
6010 m_winCapture = m_gridWin;
6011 m_winCapture->CaptureMouse();
6012 }
6013
6014
6015 }
6016 else if ( m_cursorMode == WXGRID_CURSOR_RESIZE_ROW )
6017 {
6018 int cw, ch, left, dummy;
6019 m_gridWin->GetClientSize( &cw, &ch );
6020 CalcUnscrolledPosition( 0, 0, &left, &dummy );
6021
6022 wxClientDC dc( m_gridWin );
6023 PrepareDC( dc );
6024 y = wxMax( y, GetRowTop(m_dragRowOrCol) +
6025 GetRowMinimalHeight(m_dragRowOrCol) );
6026 dc.SetLogicalFunction(wxINVERT);
6027 if ( m_dragLastPos >= 0 )
6028 {
6029 dc.DrawLine( left, m_dragLastPos, left+cw, m_dragLastPos );
6030 }
6031 dc.DrawLine( left, y, left+cw, y );
6032 m_dragLastPos = y;
6033 }
6034 else if ( m_cursorMode == WXGRID_CURSOR_RESIZE_COL )
6035 {
6036 int cw, ch, dummy, top;
6037 m_gridWin->GetClientSize( &cw, &ch );
6038 CalcUnscrolledPosition( 0, 0, &dummy, &top );
6039
6040 wxClientDC dc( m_gridWin );
6041 PrepareDC( dc );
6042 x = wxMax( x, GetColLeft(m_dragRowOrCol) +
6043 GetColMinimalWidth(m_dragRowOrCol) );
6044 dc.SetLogicalFunction(wxINVERT);
6045 if ( m_dragLastPos >= 0 )
6046 {
6047 dc.DrawLine( m_dragLastPos, top, m_dragLastPos, top + ch );
6048 }
6049 dc.DrawLine( x, top, x, top + ch );
6050 m_dragLastPos = x;
6051 }
6052
6053 return;
6054 }
6055
6056 m_isDragging = false;
6057 m_startDragPos = wxDefaultPosition;
6058
6059 // VZ: if we do this, the mode is reset to WXGRID_CURSOR_SELECT_CELL
6060 // immediately after it becomes WXGRID_CURSOR_RESIZE_ROW/COL under
6061 // wxGTK
6062 #if 0
6063 if ( event.Entering() || event.Leaving() )
6064 {
6065 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL);
6066 m_gridWin->SetCursor( *wxSTANDARD_CURSOR );
6067 }
6068 else
6069 #endif // 0
6070
6071 // ------------ Left button pressed
6072 //
6073 if ( event.LeftDown() && coords != wxGridNoCellCoords )
6074 {
6075 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_CLICK,
6076 coords.GetRow(),
6077 coords.GetCol(),
6078 event ) )
6079 {
6080 if ( !event.CmdDown() )
6081 ClearSelection();
6082 if ( event.ShiftDown() )
6083 {
6084 if ( m_selection )
6085 {
6086 m_selection->SelectBlock( m_currentCellCoords.GetRow(),
6087 m_currentCellCoords.GetCol(),
6088 coords.GetRow(),
6089 coords.GetCol(),
6090 event.ControlDown(),
6091 event.ShiftDown(),
6092 event.AltDown(),
6093 event.MetaDown() );
6094 }
6095 }
6096 else if ( XToEdgeOfCol(x) < 0 &&
6097 YToEdgeOfRow(y) < 0 )
6098 {
6099 DisableCellEditControl();
6100 MakeCellVisible( coords );
6101
6102 if ( event.CmdDown() )
6103 {
6104 if ( m_selection )
6105 {
6106 m_selection->ToggleCellSelection( coords.GetRow(),
6107 coords.GetCol(),
6108 event.ControlDown(),
6109 event.ShiftDown(),
6110 event.AltDown(),
6111 event.MetaDown() );
6112 }
6113 m_selectingTopLeft = wxGridNoCellCoords;
6114 m_selectingBottomRight = wxGridNoCellCoords;
6115 m_selectingKeyboard = coords;
6116 }
6117 else
6118 {
6119 m_waitForSlowClick = m_currentCellCoords == coords && coords != wxGridNoCellCoords;
6120 SetCurrentCell( coords );
6121 if ( m_selection )
6122 {
6123 if ( m_selection->GetSelectionMode() !=
6124 wxGrid::wxGridSelectCells )
6125 {
6126 HighlightBlock( coords, coords );
6127 }
6128 }
6129 }
6130 }
6131 }
6132 }
6133
6134 // ------------ Left double click
6135 //
6136 else if ( event.LeftDClick() && coords != wxGridNoCellCoords )
6137 {
6138 DisableCellEditControl();
6139
6140 if ( XToEdgeOfCol(x) < 0 && YToEdgeOfRow(y) < 0 )
6141 {
6142 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_DCLICK,
6143 coords.GetRow(),
6144 coords.GetCol(),
6145 event ) )
6146 {
6147 // we want double click to select a cell and start editing
6148 // (i.e. to behave in same way as sequence of two slow clicks):
6149 m_waitForSlowClick = true;
6150 }
6151 }
6152 }
6153
6154 // ------------ Left button released
6155 //
6156 else if ( event.LeftUp() )
6157 {
6158 if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
6159 {
6160 if (m_winCapture)
6161 {
6162 if (m_winCapture->HasCapture())
6163 m_winCapture->ReleaseMouse();
6164 m_winCapture = NULL;
6165 }
6166
6167 if ( coords == m_currentCellCoords && m_waitForSlowClick && CanEnableCellControl() )
6168 {
6169 ClearSelection();
6170 EnableCellEditControl();
6171
6172 wxGridCellAttr *attr = GetCellAttr(coords);
6173 wxGridCellEditor *editor = attr->GetEditor(this, coords.GetRow(), coords.GetCol());
6174 editor->StartingClick();
6175 editor->DecRef();
6176 attr->DecRef();
6177
6178 m_waitForSlowClick = false;
6179 }
6180 else if ( m_selectingTopLeft != wxGridNoCellCoords &&
6181 m_selectingBottomRight != wxGridNoCellCoords )
6182 {
6183 if ( m_selection )
6184 {
6185 m_selection->SelectBlock( m_selectingTopLeft.GetRow(),
6186 m_selectingTopLeft.GetCol(),
6187 m_selectingBottomRight.GetRow(),
6188 m_selectingBottomRight.GetCol(),
6189 event.ControlDown(),
6190 event.ShiftDown(),
6191 event.AltDown(),
6192 event.MetaDown() );
6193 }
6194
6195 m_selectingTopLeft = wxGridNoCellCoords;
6196 m_selectingBottomRight = wxGridNoCellCoords;
6197
6198 // Show the edit control, if it has been hidden for
6199 // drag-shrinking.
6200 ShowCellEditControl();
6201 }
6202 }
6203 else if ( m_cursorMode == WXGRID_CURSOR_RESIZE_ROW )
6204 {
6205 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL);
6206 DoEndDragResizeRow();
6207
6208 // Note: we are ending the event *after* doing
6209 // default processing in this case
6210 //
6211 SendEvent( wxEVT_GRID_ROW_SIZE, m_dragRowOrCol, -1, event );
6212 }
6213 else if ( m_cursorMode == WXGRID_CURSOR_RESIZE_COL )
6214 {
6215 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL);
6216 DoEndDragResizeCol();
6217
6218 // Note: we are ending the event *after* doing
6219 // default processing in this case
6220 //
6221 SendEvent( wxEVT_GRID_COL_SIZE, -1, m_dragRowOrCol, event );
6222 }
6223
6224 m_dragLastPos = -1;
6225 }
6226
6227 // ------------ Right button down
6228 //
6229 else if ( event.RightDown() && coords != wxGridNoCellCoords )
6230 {
6231 DisableCellEditControl();
6232 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_CLICK,
6233 coords.GetRow(),
6234 coords.GetCol(),
6235 event ) )
6236 {
6237 // no default action at the moment
6238 }
6239 }
6240
6241 // ------------ Right double click
6242 //
6243 else if ( event.RightDClick() && coords != wxGridNoCellCoords )
6244 {
6245 DisableCellEditControl();
6246 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_DCLICK,
6247 coords.GetRow(),
6248 coords.GetCol(),
6249 event ) )
6250 {
6251 // no default action at the moment
6252 }
6253 }
6254
6255 // ------------ Moving and no button action
6256 //
6257 else if ( event.Moving() && !event.IsButton() )
6258 {
6259 if ( coords.GetRow() < 0 || coords.GetCol() < 0 )
6260 {
6261 // out of grid cell area
6262 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL);
6263 return;
6264 }
6265
6266 int dragRow = YToEdgeOfRow( y );
6267 int dragCol = XToEdgeOfCol( x );
6268
6269 // Dragging on the corner of a cell to resize in both
6270 // directions is not implemented yet...
6271 //
6272 if ( dragRow >= 0 && dragCol >= 0 )
6273 {
6274 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL);
6275 return;
6276 }
6277
6278 if ( dragRow >= 0 )
6279 {
6280 m_dragRowOrCol = dragRow;
6281
6282 if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
6283 {
6284 if ( CanDragRowSize() && CanDragGridSize() )
6285 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW, NULL, false);
6286 }
6287 }
6288 else if ( dragCol >= 0 )
6289 {
6290 m_dragRowOrCol = dragCol;
6291
6292 if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
6293 {
6294 if ( CanDragColSize() && CanDragGridSize() )
6295 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL, NULL, false);
6296 }
6297 }
6298 else // Neither on a row or col edge
6299 {
6300 if ( m_cursorMode != WXGRID_CURSOR_SELECT_CELL )
6301 {
6302 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL);
6303 }
6304 }
6305 }
6306 }
6307
6308 void wxGrid::DoEndDragResizeRow()
6309 {
6310 if ( m_dragLastPos >= 0 )
6311 {
6312 // erase the last line and resize the row
6313 //
6314 int cw, ch, left, dummy;
6315 m_gridWin->GetClientSize( &cw, &ch );
6316 CalcUnscrolledPosition( 0, 0, &left, &dummy );
6317
6318 wxClientDC dc( m_gridWin );
6319 PrepareDC( dc );
6320 dc.SetLogicalFunction( wxINVERT );
6321 dc.DrawLine( left, m_dragLastPos, left + cw, m_dragLastPos );
6322 HideCellEditControl();
6323 SaveEditControlValue();
6324
6325 int rowTop = GetRowTop(m_dragRowOrCol);
6326 SetRowSize( m_dragRowOrCol,
6327 wxMax( m_dragLastPos - rowTop, m_minAcceptableRowHeight ) );
6328
6329 if ( !GetBatchCount() )
6330 {
6331 // Only needed to get the correct rect.y:
6332 wxRect rect ( CellToRect( m_dragRowOrCol, 0 ) );
6333 rect.x = 0;
6334 CalcScrolledPosition(0, rect.y, &dummy, &rect.y);
6335 rect.width = m_rowLabelWidth;
6336 rect.height = ch - rect.y;
6337 m_rowLabelWin->Refresh( true, &rect );
6338 rect.width = cw;
6339
6340 // if there is a multicell block, paint all of it
6341 if (m_table)
6342 {
6343 int i, cell_rows, cell_cols, subtract_rows = 0;
6344 int leftCol = XToCol(left);
6345 int rightCol = internalXToCol(left + cw);
6346 if (leftCol >= 0)
6347 {
6348 for (i=leftCol; i<rightCol; i++)
6349 {
6350 GetCellSize(m_dragRowOrCol, i, &cell_rows, &cell_cols);
6351 if (cell_rows < subtract_rows)
6352 subtract_rows = cell_rows;
6353 }
6354 rect.y = GetRowTop(m_dragRowOrCol + subtract_rows);
6355 CalcScrolledPosition(0, rect.y, &dummy, &rect.y);
6356 rect.height = ch - rect.y;
6357 }
6358 }
6359 m_gridWin->Refresh( false, &rect );
6360 }
6361
6362 ShowCellEditControl();
6363 }
6364 }
6365
6366
6367 void wxGrid::DoEndDragResizeCol()
6368 {
6369 if ( m_dragLastPos >= 0 )
6370 {
6371 // erase the last line and resize the col
6372 //
6373 int cw, ch, dummy, top;
6374 m_gridWin->GetClientSize( &cw, &ch );
6375 CalcUnscrolledPosition( 0, 0, &dummy, &top );
6376
6377 wxClientDC dc( m_gridWin );
6378 PrepareDC( dc );
6379 dc.SetLogicalFunction( wxINVERT );
6380 dc.DrawLine( m_dragLastPos, top, m_dragLastPos, top + ch );
6381 HideCellEditControl();
6382 SaveEditControlValue();
6383
6384 int colLeft = GetColLeft(m_dragRowOrCol);
6385 SetColSize( m_dragRowOrCol,
6386 wxMax( m_dragLastPos - colLeft,
6387 GetColMinimalWidth(m_dragRowOrCol) ) );
6388
6389 if ( !GetBatchCount() )
6390 {
6391 // Only needed to get the correct rect.x:
6392 wxRect rect ( CellToRect( 0, m_dragRowOrCol ) );
6393 rect.y = 0;
6394 CalcScrolledPosition(rect.x, 0, &rect.x, &dummy);
6395 rect.width = cw - rect.x;
6396 rect.height = m_colLabelHeight;
6397 m_colLabelWin->Refresh( true, &rect );
6398 rect.height = ch;
6399
6400 // if there is a multicell block, paint all of it
6401 if (m_table)
6402 {
6403 int i, cell_rows, cell_cols, subtract_cols = 0;
6404 int topRow = YToRow(top);
6405 int bottomRow = internalYToRow(top + cw);
6406 if (topRow >= 0)
6407 {
6408 for (i=topRow; i<bottomRow; i++)
6409 {
6410 GetCellSize(i, m_dragRowOrCol, &cell_rows, &cell_cols);
6411 if (cell_cols < subtract_cols)
6412 subtract_cols = cell_cols;
6413 }
6414
6415 rect.x = GetColLeft(m_dragRowOrCol + subtract_cols);
6416 CalcScrolledPosition(rect.x, 0, &rect.x, &dummy);
6417 rect.width = cw - rect.x;
6418 }
6419 }
6420
6421 m_gridWin->Refresh( false, &rect );
6422 }
6423
6424 ShowCellEditControl();
6425 }
6426 }
6427
6428 void wxGrid::DoEndDragMoveCol()
6429 {
6430 //The user clicked on the column but didn't actually drag
6431 if ( m_dragLastPos < 0 )
6432 {
6433 m_colLabelWin->Refresh(); //Do this to "unpress" the column
6434 return;
6435 }
6436
6437 int newPos;
6438 if ( m_moveToCol == -1 )
6439 newPos = m_numCols - 1;
6440 else
6441 {
6442 newPos = GetColPos( m_moveToCol );
6443 if ( newPos > GetColPos( m_dragRowOrCol ) )
6444 newPos--;
6445 }
6446
6447 SetColPos( m_dragRowOrCol, newPos );
6448 }
6449
6450 void wxGrid::SetColPos( int colID, int newPos )
6451 {
6452 if ( m_colAt.IsEmpty() )
6453 {
6454 m_colAt.Alloc( m_numCols );
6455
6456 int i;
6457 for ( i = 0; i < m_numCols; i++ )
6458 {
6459 m_colAt.Add( i );
6460 }
6461 }
6462
6463 int oldPos = GetColPos( colID );
6464
6465 //Reshuffle the m_colAt array
6466 if ( newPos > oldPos )
6467 {
6468 int i;
6469 for ( i = oldPos; i < newPos; i++ )
6470 {
6471 m_colAt[i] = m_colAt[i+1];
6472 }
6473 }
6474 else
6475 {
6476 int i;
6477 for ( i = oldPos; i > newPos; i-- )
6478 {
6479 m_colAt[i] = m_colAt[i-1];
6480 }
6481 }
6482
6483 m_colAt[newPos] = colID;
6484
6485 //Recalculate the column rights
6486 if ( !m_colWidths.IsEmpty() )
6487 {
6488 int colRight = 0;
6489 int colPos;
6490 for ( colPos = 0; colPos < m_numCols; colPos++ )
6491 {
6492 int colID = GetColAt( colPos );
6493
6494 colRight += m_colWidths[colID];
6495 m_colRights[colID] = colRight;
6496 }
6497 }
6498
6499 m_colLabelWin->Refresh();
6500 m_gridWin->Refresh();
6501 }
6502
6503
6504
6505 void wxGrid::EnableDragColMove( bool enable )
6506 {
6507 if ( m_canDragColMove == enable )
6508 return;
6509
6510 m_canDragColMove = enable;
6511
6512 if ( !m_canDragColMove )
6513 {
6514 m_colAt.Clear();
6515
6516 //Recalculate the column rights
6517 if ( !m_colWidths.IsEmpty() )
6518 {
6519 int colRight = 0;
6520 int colPos;
6521 for ( colPos = 0; colPos < m_numCols; colPos++ )
6522 {
6523 colRight += m_colWidths[colPos];
6524 m_colRights[colPos] = colRight;
6525 }
6526 }
6527
6528 m_colLabelWin->Refresh();
6529 m_gridWin->Refresh();
6530 }
6531 }
6532
6533
6534 //
6535 // ------ interaction with data model
6536 //
6537 bool wxGrid::ProcessTableMessage( wxGridTableMessage& msg )
6538 {
6539 switch ( msg.GetId() )
6540 {
6541 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES:
6542 return GetModelValues();
6543
6544 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES:
6545 return SetModelValues();
6546
6547 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED:
6548 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED:
6549 case wxGRIDTABLE_NOTIFY_ROWS_DELETED:
6550 case wxGRIDTABLE_NOTIFY_COLS_INSERTED:
6551 case wxGRIDTABLE_NOTIFY_COLS_APPENDED:
6552 case wxGRIDTABLE_NOTIFY_COLS_DELETED:
6553 return Redimension( msg );
6554
6555 default:
6556 return false;
6557 }
6558 }
6559
6560 // The behaviour of this function depends on the grid table class
6561 // Clear() function. For the default wxGridStringTable class the
6562 // behavious is to replace all cell contents with wxEmptyString but
6563 // not to change the number of rows or cols.
6564 //
6565 void wxGrid::ClearGrid()
6566 {
6567 if ( m_table )
6568 {
6569 if (IsCellEditControlEnabled())
6570 DisableCellEditControl();
6571
6572 m_table->Clear();
6573 if (!GetBatchCount())
6574 m_gridWin->Refresh();
6575 }
6576 }
6577
6578 bool wxGrid::InsertRows( int pos, int numRows, bool WXUNUSED(updateLabels) )
6579 {
6580 // TODO: something with updateLabels flag
6581
6582 if ( !m_created )
6583 {
6584 wxFAIL_MSG( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") );
6585 return false;
6586 }
6587
6588 if ( m_table )
6589 {
6590 if (IsCellEditControlEnabled())
6591 DisableCellEditControl();
6592
6593 bool done = m_table->InsertRows( pos, numRows );
6594 return done;
6595
6596 // the table will have sent the results of the insert row
6597 // operation to this view object as a grid table message
6598 }
6599
6600 return false;
6601 }
6602
6603 bool wxGrid::AppendRows( int numRows, bool WXUNUSED(updateLabels) )
6604 {
6605 // TODO: something with updateLabels flag
6606
6607 if ( !m_created )
6608 {
6609 wxFAIL_MSG( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") );
6610 return false;
6611 }
6612
6613 if ( m_table )
6614 {
6615 bool done = m_table && m_table->AppendRows( numRows );
6616 return done;
6617
6618 // the table will have sent the results of the append row
6619 // operation to this view object as a grid table message
6620 }
6621
6622 return false;
6623 }
6624
6625 bool wxGrid::DeleteRows( int pos, int numRows, bool WXUNUSED(updateLabels) )
6626 {
6627 // TODO: something with updateLabels flag
6628
6629 if ( !m_created )
6630 {
6631 wxFAIL_MSG( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") );
6632 return false;
6633 }
6634
6635 if ( m_table )
6636 {
6637 if (IsCellEditControlEnabled())
6638 DisableCellEditControl();
6639
6640 bool done = m_table->DeleteRows( pos, numRows );
6641 return done;
6642 // the table will have sent the results of the delete row
6643 // operation to this view object as a grid table message
6644 }
6645
6646 return false;
6647 }
6648
6649 bool wxGrid::InsertCols( int pos, int numCols, bool WXUNUSED(updateLabels) )
6650 {
6651 // TODO: something with updateLabels flag
6652
6653 if ( !m_created )
6654 {
6655 wxFAIL_MSG( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") );
6656 return false;
6657 }
6658
6659 if ( m_table )
6660 {
6661 if (IsCellEditControlEnabled())
6662 DisableCellEditControl();
6663
6664 bool done = m_table->InsertCols( pos, numCols );
6665 return done;
6666 // the table will have sent the results of the insert col
6667 // operation to this view object as a grid table message
6668 }
6669
6670 return false;
6671 }
6672
6673 bool wxGrid::AppendCols( int numCols, bool WXUNUSED(updateLabels) )
6674 {
6675 // TODO: something with updateLabels flag
6676
6677 if ( !m_created )
6678 {
6679 wxFAIL_MSG( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") );
6680 return false;
6681 }
6682
6683 if ( m_table )
6684 {
6685 bool done = m_table->AppendCols( numCols );
6686 return done;
6687 // the table will have sent the results of the append col
6688 // operation to this view object as a grid table message
6689 }
6690
6691 return false;
6692 }
6693
6694 bool wxGrid::DeleteCols( int pos, int numCols, bool WXUNUSED(updateLabels) )
6695 {
6696 // TODO: something with updateLabels flag
6697
6698 if ( !m_created )
6699 {
6700 wxFAIL_MSG( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") );
6701 return false;
6702 }
6703
6704 if ( m_table )
6705 {
6706 if (IsCellEditControlEnabled())
6707 DisableCellEditControl();
6708
6709 bool done = m_table->DeleteCols( pos, numCols );
6710 return done;
6711 // the table will have sent the results of the delete col
6712 // operation to this view object as a grid table message
6713 }
6714
6715 return false;
6716 }
6717
6718 //
6719 // ----- event handlers
6720 //
6721
6722 // Generate a grid event based on a mouse event and
6723 // return the result of ProcessEvent()
6724 //
6725 int wxGrid::SendEvent( const wxEventType type,
6726 int row, int col,
6727 wxMouseEvent& mouseEv )
6728 {
6729 bool claimed, vetoed;
6730
6731 if ( type == wxEVT_GRID_ROW_SIZE || type == wxEVT_GRID_COL_SIZE )
6732 {
6733 int rowOrCol = (row == -1 ? col : row);
6734
6735 wxGridSizeEvent gridEvt( GetId(),
6736 type,
6737 this,
6738 rowOrCol,
6739 mouseEv.GetX() + GetRowLabelSize(),
6740 mouseEv.GetY() + GetColLabelSize(),
6741 mouseEv.ControlDown(),
6742 mouseEv.ShiftDown(),
6743 mouseEv.AltDown(),
6744 mouseEv.MetaDown() );
6745
6746 claimed = GetEventHandler()->ProcessEvent(gridEvt);
6747 vetoed = !gridEvt.IsAllowed();
6748 }
6749 else if ( type == wxEVT_GRID_RANGE_SELECT )
6750 {
6751 // Right now, it should _never_ end up here!
6752 wxGridRangeSelectEvent gridEvt( GetId(),
6753 type,
6754 this,
6755 m_selectingTopLeft,
6756 m_selectingBottomRight,
6757 true,
6758 mouseEv.ControlDown(),
6759 mouseEv.ShiftDown(),
6760 mouseEv.AltDown(),
6761 mouseEv.MetaDown() );
6762
6763 claimed = GetEventHandler()->ProcessEvent(gridEvt);
6764 vetoed = !gridEvt.IsAllowed();
6765 }
6766 else if ( type == wxEVT_GRID_LABEL_LEFT_CLICK ||
6767 type == wxEVT_GRID_LABEL_LEFT_DCLICK ||
6768 type == wxEVT_GRID_LABEL_RIGHT_CLICK ||
6769 type == wxEVT_GRID_LABEL_RIGHT_DCLICK )
6770 {
6771 wxPoint pos = mouseEv.GetPosition();
6772
6773 if ( mouseEv.GetEventObject() == GetGridRowLabelWindow() )
6774 pos.y += GetColLabelSize();
6775 if ( mouseEv.GetEventObject() == GetGridColLabelWindow() )
6776 pos.x += GetRowLabelSize();
6777
6778 wxGridEvent gridEvt( GetId(),
6779 type,
6780 this,
6781 row, col,
6782 pos.x,
6783 pos.y,
6784 false,
6785 mouseEv.ControlDown(),
6786 mouseEv.ShiftDown(),
6787 mouseEv.AltDown(),
6788 mouseEv.MetaDown() );
6789 claimed = GetEventHandler()->ProcessEvent(gridEvt);
6790 vetoed = !gridEvt.IsAllowed();
6791 }
6792 else
6793 {
6794 wxGridEvent gridEvt( GetId(),
6795 type,
6796 this,
6797 row, col,
6798 mouseEv.GetX() + GetRowLabelSize(),
6799 mouseEv.GetY() + GetColLabelSize(),
6800 false,
6801 mouseEv.ControlDown(),
6802 mouseEv.ShiftDown(),
6803 mouseEv.AltDown(),
6804 mouseEv.MetaDown() );
6805 claimed = GetEventHandler()->ProcessEvent(gridEvt);
6806 vetoed = !gridEvt.IsAllowed();
6807 }
6808
6809 // A Veto'd event may not be `claimed' so test this first
6810 if (vetoed)
6811 return -1;
6812
6813 return claimed ? 1 : 0;
6814 }
6815
6816 // Generate a grid event of specified type and return the result
6817 // of ProcessEvent().
6818 //
6819 int wxGrid::SendEvent( const wxEventType type,
6820 int row, int col )
6821 {
6822 bool claimed, vetoed;
6823
6824 if ( type == wxEVT_GRID_ROW_SIZE || type == wxEVT_GRID_COL_SIZE )
6825 {
6826 int rowOrCol = (row == -1 ? col : row);
6827
6828 wxGridSizeEvent gridEvt( GetId(), type, this, rowOrCol );
6829
6830 claimed = GetEventHandler()->ProcessEvent(gridEvt);
6831 vetoed = !gridEvt.IsAllowed();
6832 }
6833 else
6834 {
6835 wxGridEvent gridEvt( GetId(), type, this, row, col );
6836
6837 claimed = GetEventHandler()->ProcessEvent(gridEvt);
6838 vetoed = !gridEvt.IsAllowed();
6839 }
6840
6841 // A Veto'd event may not be `claimed' so test this first
6842 if (vetoed)
6843 return -1;
6844
6845 return claimed ? 1 : 0;
6846 }
6847
6848 void wxGrid::OnPaint( wxPaintEvent& WXUNUSED(event) )
6849 {
6850 // needed to prevent zillions of paint events on MSW
6851 wxPaintDC dc(this);
6852 }
6853
6854 void wxGrid::Refresh(bool eraseb, const wxRect* rect)
6855 {
6856 // Don't do anything if between Begin/EndBatch...
6857 // EndBatch() will do all this on the last nested one anyway.
6858 if (! GetBatchCount())
6859 {
6860 // Refresh to get correct scrolled position:
6861 wxScrolledWindow::Refresh(eraseb, rect);
6862
6863 if (rect)
6864 {
6865 int rect_x, rect_y, rectWidth, rectHeight;
6866 int width_label, width_cell, height_label, height_cell;
6867 int x, y;
6868
6869 // Copy rectangle can get scroll offsets..
6870 rect_x = rect->GetX();
6871 rect_y = rect->GetY();
6872 rectWidth = rect->GetWidth();
6873 rectHeight = rect->GetHeight();
6874
6875 width_label = m_rowLabelWidth - rect_x;
6876 if (width_label > rectWidth)
6877 width_label = rectWidth;
6878
6879 height_label = m_colLabelHeight - rect_y;
6880 if (height_label > rectHeight)
6881 height_label = rectHeight;
6882
6883 if (rect_x > m_rowLabelWidth)
6884 {
6885 x = rect_x - m_rowLabelWidth;
6886 width_cell = rectWidth;
6887 }
6888 else
6889 {
6890 x = 0;
6891 width_cell = rectWidth - (m_rowLabelWidth - rect_x);
6892 }
6893
6894 if (rect_y > m_colLabelHeight)
6895 {
6896 y = rect_y - m_colLabelHeight;
6897 height_cell = rectHeight;
6898 }
6899 else
6900 {
6901 y = 0;
6902 height_cell = rectHeight - (m_colLabelHeight - rect_y);
6903 }
6904
6905 // Paint corner label part intersecting rect.
6906 if ( width_label > 0 && height_label > 0 )
6907 {
6908 wxRect anotherrect(rect_x, rect_y, width_label, height_label);
6909 m_cornerLabelWin->Refresh(eraseb, &anotherrect);
6910 }
6911
6912 // Paint col labels part intersecting rect.
6913 if ( width_cell > 0 && height_label > 0 )
6914 {
6915 wxRect anotherrect(x, rect_y, width_cell, height_label);
6916 m_colLabelWin->Refresh(eraseb, &anotherrect);
6917 }
6918
6919 // Paint row labels part intersecting rect.
6920 if ( width_label > 0 && height_cell > 0 )
6921 {
6922 wxRect anotherrect(rect_x, y, width_label, height_cell);
6923 m_rowLabelWin->Refresh(eraseb, &anotherrect);
6924 }
6925
6926 // Paint cell area part intersecting rect.
6927 if ( width_cell > 0 && height_cell > 0 )
6928 {
6929 wxRect anotherrect(x, y, width_cell, height_cell);
6930 m_gridWin->Refresh(eraseb, &anotherrect);
6931 }
6932 }
6933 else
6934 {
6935 m_cornerLabelWin->Refresh(eraseb, NULL);
6936 m_colLabelWin->Refresh(eraseb, NULL);
6937 m_rowLabelWin->Refresh(eraseb, NULL);
6938 m_gridWin->Refresh(eraseb, NULL);
6939 }
6940 }
6941 }
6942
6943 void wxGrid::OnSize(wxSizeEvent& WXUNUSED(event))
6944 {
6945 if (m_targetWindow != this) // check whether initialisation has been done
6946 {
6947 // update our children window positions and scrollbars
6948 CalcDimensions();
6949 }
6950 }
6951
6952 void wxGrid::OnKeyDown( wxKeyEvent& event )
6953 {
6954 if ( m_inOnKeyDown )
6955 {
6956 // shouldn't be here - we are going round in circles...
6957 //
6958 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") );
6959 }
6960
6961 m_inOnKeyDown = true;
6962
6963 // propagate the event up and see if it gets processed
6964 wxWindow *parent = GetParent();
6965 wxKeyEvent keyEvt( event );
6966 keyEvt.SetEventObject( parent );
6967
6968 if ( !parent->GetEventHandler()->ProcessEvent( keyEvt ) )
6969 {
6970 if (GetLayoutDirection() == wxLayout_RightToLeft)
6971 {
6972 if (event.GetKeyCode() == WXK_RIGHT)
6973 event.m_keyCode = WXK_LEFT;
6974 else if (event.GetKeyCode() == WXK_LEFT)
6975 event.m_keyCode = WXK_RIGHT;
6976 }
6977
6978 // try local handlers
6979 switch ( event.GetKeyCode() )
6980 {
6981 case WXK_UP:
6982 if ( event.ControlDown() )
6983 MoveCursorUpBlock( event.ShiftDown() );
6984 else
6985 MoveCursorUp( event.ShiftDown() );
6986 break;
6987
6988 case WXK_DOWN:
6989 if ( event.ControlDown() )
6990 MoveCursorDownBlock( event.ShiftDown() );
6991 else
6992 MoveCursorDown( event.ShiftDown() );
6993 break;
6994
6995 case WXK_LEFT:
6996 if ( event.ControlDown() )
6997 MoveCursorLeftBlock( event.ShiftDown() );
6998 else
6999 MoveCursorLeft( event.ShiftDown() );
7000 break;
7001
7002 case WXK_RIGHT:
7003 if ( event.ControlDown() )
7004 MoveCursorRightBlock( event.ShiftDown() );
7005 else
7006 MoveCursorRight( event.ShiftDown() );
7007 break;
7008
7009 case WXK_RETURN:
7010 case WXK_NUMPAD_ENTER:
7011 if ( event.ControlDown() )
7012 {
7013 event.Skip(); // to let the edit control have the return
7014 }
7015 else
7016 {
7017 if ( GetGridCursorRow() < GetNumberRows()-1 )
7018 {
7019 MoveCursorDown( event.ShiftDown() );
7020 }
7021 else
7022 {
7023 // at the bottom of a column
7024 DisableCellEditControl();
7025 }
7026 }
7027 break;
7028
7029 case WXK_ESCAPE:
7030 ClearSelection();
7031 break;
7032
7033 case WXK_TAB:
7034 if (event.ShiftDown())
7035 {
7036 if ( GetGridCursorCol() > 0 )
7037 {
7038 MoveCursorLeft( false );
7039 }
7040 else
7041 {
7042 // at left of grid
7043 DisableCellEditControl();
7044 }
7045 }
7046 else
7047 {
7048 if ( GetGridCursorCol() < GetNumberCols() - 1 )
7049 {
7050 MoveCursorRight( false );
7051 }
7052 else
7053 {
7054 // at right of grid
7055 DisableCellEditControl();
7056 }
7057 }
7058 break;
7059
7060 case WXK_HOME:
7061 if ( event.ControlDown() )
7062 {
7063 MakeCellVisible( 0, 0 );
7064 SetCurrentCell( 0, 0 );
7065 }
7066 else
7067 {
7068 event.Skip();
7069 }
7070 break;
7071
7072 case WXK_END:
7073 if ( event.ControlDown() )
7074 {
7075 MakeCellVisible( m_numRows - 1, m_numCols - 1 );
7076 SetCurrentCell( m_numRows - 1, m_numCols - 1 );
7077 }
7078 else
7079 {
7080 event.Skip();
7081 }
7082 break;
7083
7084 case WXK_PAGEUP:
7085 MovePageUp();
7086 break;
7087
7088 case WXK_PAGEDOWN:
7089 MovePageDown();
7090 break;
7091
7092 case WXK_SPACE:
7093 if ( event.ControlDown() )
7094 {
7095 if ( m_selection )
7096 {
7097 m_selection->ToggleCellSelection(
7098 m_currentCellCoords.GetRow(),
7099 m_currentCellCoords.GetCol(),
7100 event.ControlDown(),
7101 event.ShiftDown(),
7102 event.AltDown(),
7103 event.MetaDown() );
7104 }
7105 break;
7106 }
7107
7108 if ( !IsEditable() )
7109 MoveCursorRight( false );
7110 else
7111 event.Skip();
7112 break;
7113
7114 default:
7115 event.Skip();
7116 break;
7117 }
7118 }
7119
7120 m_inOnKeyDown = false;
7121 }
7122
7123 void wxGrid::OnKeyUp( wxKeyEvent& event )
7124 {
7125 // try local handlers
7126 //
7127 if ( event.GetKeyCode() == WXK_SHIFT )
7128 {
7129 if ( m_selectingTopLeft != wxGridNoCellCoords &&
7130 m_selectingBottomRight != wxGridNoCellCoords )
7131 {
7132 if ( m_selection )
7133 {
7134 m_selection->SelectBlock(
7135 m_selectingTopLeft.GetRow(),
7136 m_selectingTopLeft.GetCol(),
7137 m_selectingBottomRight.GetRow(),
7138 m_selectingBottomRight.GetCol(),
7139 event.ControlDown(),
7140 true,
7141 event.AltDown(),
7142 event.MetaDown() );
7143 }
7144 }
7145
7146 m_selectingTopLeft = wxGridNoCellCoords;
7147 m_selectingBottomRight = wxGridNoCellCoords;
7148 m_selectingKeyboard = wxGridNoCellCoords;
7149 }
7150 }
7151
7152 void wxGrid::OnChar( wxKeyEvent& event )
7153 {
7154 // is it possible to edit the current cell at all?
7155 if ( !IsCellEditControlEnabled() && CanEnableCellControl() )
7156 {
7157 // yes, now check whether the cells editor accepts the key
7158 int row = m_currentCellCoords.GetRow();
7159 int col = m_currentCellCoords.GetCol();
7160 wxGridCellAttr *attr = GetCellAttr(row, col);
7161 wxGridCellEditor *editor = attr->GetEditor(this, row, col);
7162
7163 // <F2> is special and will always start editing, for
7164 // other keys - ask the editor itself
7165 if ( (event.GetKeyCode() == WXK_F2 && !event.HasModifiers())
7166 || editor->IsAcceptedKey(event) )
7167 {
7168 // ensure cell is visble
7169 MakeCellVisible(row, col);
7170 EnableCellEditControl();
7171
7172 // a problem can arise if the cell is not completely
7173 // visible (even after calling MakeCellVisible the
7174 // control is not created and calling StartingKey will
7175 // crash the app
7176 if ( event.GetKeyCode() != WXK_F2 && editor->IsCreated() && m_cellEditCtrlEnabled )
7177 editor->StartingKey(event);
7178 }
7179 else
7180 {
7181 event.Skip();
7182 }
7183
7184 editor->DecRef();
7185 attr->DecRef();
7186 }
7187 else
7188 {
7189 event.Skip();
7190 }
7191 }
7192
7193 void wxGrid::OnEraseBackground(wxEraseEvent&)
7194 {
7195 }
7196
7197 void wxGrid::SetCurrentCell( const wxGridCellCoords& coords )
7198 {
7199 if ( SendEvent( wxEVT_GRID_SELECT_CELL, coords.GetRow(), coords.GetCol() ) )
7200 {
7201 // the event has been intercepted - do nothing
7202 return;
7203 }
7204
7205 #if !defined(__WXMAC__)
7206 wxClientDC dc( m_gridWin );
7207 PrepareDC( dc );
7208 #endif
7209
7210 if ( m_currentCellCoords != wxGridNoCellCoords )
7211 {
7212 DisableCellEditControl();
7213
7214 if ( IsVisible( m_currentCellCoords, false ) )
7215 {
7216 wxRect r;
7217 r = BlockToDeviceRect( m_currentCellCoords, m_currentCellCoords );
7218 if ( !m_gridLinesEnabled )
7219 {
7220 r.x--;
7221 r.y--;
7222 r.width++;
7223 r.height++;
7224 }
7225
7226 wxGridCellCoordsArray cells = CalcCellsExposed( r );
7227
7228 // Otherwise refresh redraws the highlight!
7229 m_currentCellCoords = coords;
7230
7231 #if defined(__WXMAC__)
7232 m_gridWin->Refresh(true /*, & r */);
7233 #else
7234 DrawGridCellArea( dc, cells );
7235 DrawAllGridLines( dc, r );
7236 #endif
7237 }
7238 }
7239
7240 m_currentCellCoords = coords;
7241
7242 wxGridCellAttr *attr = GetCellAttr( coords );
7243 #if !defined(__WXMAC__)
7244 DrawCellHighlight( dc, attr );
7245 #endif
7246 attr->DecRef();
7247 }
7248
7249 void wxGrid::HighlightBlock( int topRow, int leftCol, int bottomRow, int rightCol )
7250 {
7251 int temp;
7252 wxGridCellCoords updateTopLeft, updateBottomRight;
7253
7254 if ( m_selection )
7255 {
7256 if ( m_selection->GetSelectionMode() == wxGrid::wxGridSelectRows )
7257 {
7258 leftCol = 0;
7259 rightCol = GetNumberCols() - 1;
7260 }
7261 else if ( m_selection->GetSelectionMode() == wxGrid::wxGridSelectColumns )
7262 {
7263 topRow = 0;
7264 bottomRow = GetNumberRows() - 1;
7265 }
7266 }
7267
7268 if ( topRow > bottomRow )
7269 {
7270 temp = topRow;
7271 topRow = bottomRow;
7272 bottomRow = temp;
7273 }
7274
7275 if ( leftCol > rightCol )
7276 {
7277 temp = leftCol;
7278 leftCol = rightCol;
7279 rightCol = temp;
7280 }
7281
7282 updateTopLeft = wxGridCellCoords( topRow, leftCol );
7283 updateBottomRight = wxGridCellCoords( bottomRow, rightCol );
7284
7285 // First the case that we selected a completely new area
7286 if ( m_selectingTopLeft == wxGridNoCellCoords ||
7287 m_selectingBottomRight == wxGridNoCellCoords )
7288 {
7289 wxRect rect;
7290 rect = BlockToDeviceRect( wxGridCellCoords ( topRow, leftCol ),
7291 wxGridCellCoords ( bottomRow, rightCol ) );
7292 m_gridWin->Refresh( false, &rect );
7293 }
7294
7295 // Now handle changing an existing selection area.
7296 else if ( m_selectingTopLeft != updateTopLeft ||
7297 m_selectingBottomRight != updateBottomRight )
7298 {
7299 // Compute two optimal update rectangles:
7300 // Either one rectangle is a real subset of the
7301 // other, or they are (almost) disjoint!
7302 wxRect rect[4];
7303 bool need_refresh[4];
7304 need_refresh[0] =
7305 need_refresh[1] =
7306 need_refresh[2] =
7307 need_refresh[3] = false;
7308 int i;
7309
7310 // Store intermediate values
7311 wxCoord oldLeft = m_selectingTopLeft.GetCol();
7312 wxCoord oldTop = m_selectingTopLeft.GetRow();
7313 wxCoord oldRight = m_selectingBottomRight.GetCol();
7314 wxCoord oldBottom = m_selectingBottomRight.GetRow();
7315
7316 // Determine the outer/inner coordinates.
7317 if (oldLeft > leftCol)
7318 {
7319 temp = oldLeft;
7320 oldLeft = leftCol;
7321 leftCol = temp;
7322 }
7323 if (oldTop > topRow )
7324 {
7325 temp = oldTop;
7326 oldTop = topRow;
7327 topRow = temp;
7328 }
7329 if (oldRight < rightCol )
7330 {
7331 temp = oldRight;
7332 oldRight = rightCol;
7333 rightCol = temp;
7334 }
7335 if (oldBottom < bottomRow)
7336 {
7337 temp = oldBottom;
7338 oldBottom = bottomRow;
7339 bottomRow = temp;
7340 }
7341
7342 // Now, either the stuff marked old is the outer
7343 // rectangle or we don't have a situation where one
7344 // is contained in the other.
7345
7346 if ( oldLeft < leftCol )
7347 {
7348 // Refresh the newly selected or deselected
7349 // area to the left of the old or new selection.
7350 need_refresh[0] = true;
7351 rect[0] = BlockToDeviceRect(
7352 wxGridCellCoords( oldTop, oldLeft ),
7353 wxGridCellCoords( oldBottom, leftCol - 1 ) );
7354 }
7355
7356 if ( oldTop < topRow )
7357 {
7358 // Refresh the newly selected or deselected
7359 // area above the old or new selection.
7360 need_refresh[1] = true;
7361 rect[1] = BlockToDeviceRect(
7362 wxGridCellCoords( oldTop, leftCol ),
7363 wxGridCellCoords( topRow - 1, rightCol ) );
7364 }
7365
7366 if ( oldRight > rightCol )
7367 {
7368 // Refresh the newly selected or deselected
7369 // area to the right of the old or new selection.
7370 need_refresh[2] = true;
7371 rect[2] = BlockToDeviceRect(
7372 wxGridCellCoords( oldTop, rightCol + 1 ),
7373 wxGridCellCoords( oldBottom, oldRight ) );
7374 }
7375
7376 if ( oldBottom > bottomRow )
7377 {
7378 // Refresh the newly selected or deselected
7379 // area below the old or new selection.
7380 need_refresh[3] = true;
7381 rect[3] = BlockToDeviceRect(
7382 wxGridCellCoords( bottomRow + 1, leftCol ),
7383 wxGridCellCoords( oldBottom, rightCol ) );
7384 }
7385
7386 // various Refresh() calls
7387 for (i = 0; i < 4; i++ )
7388 if ( need_refresh[i] && rect[i] != wxGridNoCellRect )
7389 m_gridWin->Refresh( false, &(rect[i]) );
7390 }
7391
7392 // change selection
7393 m_selectingTopLeft = updateTopLeft;
7394 m_selectingBottomRight = updateBottomRight;
7395 }
7396
7397 //
7398 // ------ functions to get/send data (see also public functions)
7399 //
7400
7401 bool wxGrid::GetModelValues()
7402 {
7403 // Hide the editor, so it won't hide a changed value.
7404 HideCellEditControl();
7405
7406 if ( m_table )
7407 {
7408 // all we need to do is repaint the grid
7409 //
7410 m_gridWin->Refresh();
7411 return true;
7412 }
7413
7414 return false;
7415 }
7416
7417 bool wxGrid::SetModelValues()
7418 {
7419 int row, col;
7420
7421 // Disable the editor, so it won't hide a changed value.
7422 // Do we also want to save the current value of the editor first?
7423 // I think so ...
7424 DisableCellEditControl();
7425
7426 if ( m_table )
7427 {
7428 for ( row = 0; row < m_numRows; row++ )
7429 {
7430 for ( col = 0; col < m_numCols; col++ )
7431 {
7432 m_table->SetValue( row, col, GetCellValue(row, col) );
7433 }
7434 }
7435
7436 return true;
7437 }
7438
7439 return false;
7440 }
7441
7442 // Note - this function only draws cells that are in the list of
7443 // exposed cells (usually set from the update region by
7444 // CalcExposedCells)
7445 //
7446 void wxGrid::DrawGridCellArea( wxDC& dc, const wxGridCellCoordsArray& cells )
7447 {
7448 if ( !m_numRows || !m_numCols )
7449 return;
7450
7451 int i, numCells = cells.GetCount();
7452 int row, col, cell_rows, cell_cols;
7453 wxGridCellCoordsArray redrawCells;
7454
7455 for ( i = numCells - 1; i >= 0; i-- )
7456 {
7457 row = cells[i].GetRow();
7458 col = cells[i].GetCol();
7459 GetCellSize( row, col, &cell_rows, &cell_cols );
7460
7461 // If this cell is part of a multicell block, find owner for repaint
7462 if ( cell_rows <= 0 || cell_cols <= 0 )
7463 {
7464 wxGridCellCoords cell( row + cell_rows, col + cell_cols );
7465 bool marked = false;
7466 for ( int j = 0; j < numCells; j++ )
7467 {
7468 if ( cell == cells[j] )
7469 {
7470 marked = true;
7471 break;
7472 }
7473 }
7474
7475 if (!marked)
7476 {
7477 int count = redrawCells.GetCount();
7478 for (int j = 0; j < count; j++)
7479 {
7480 if ( cell == redrawCells[j] )
7481 {
7482 marked = true;
7483 break;
7484 }
7485 }
7486
7487 if (!marked)
7488 redrawCells.Add( cell );
7489 }
7490
7491 // don't bother drawing this cell
7492 continue;
7493 }
7494
7495 // If this cell is empty, find cell to left that might want to overflow
7496 if (m_table && m_table->IsEmptyCell(row, col))
7497 {
7498 for ( int l = 0; l < cell_rows; l++ )
7499 {
7500 // find a cell in this row to leave already marked for repaint
7501 int left = col;
7502 for (int k = 0; k < int(redrawCells.GetCount()); k++)
7503 if ((redrawCells[k].GetCol() < left) &&
7504 (redrawCells[k].GetRow() == row))
7505 {
7506 left = redrawCells[k].GetCol();
7507 }
7508
7509 if (left == col)
7510 left = 0; // oh well
7511
7512 for (int j = col - 1; j >= left; j--)
7513 {
7514 if (!m_table->IsEmptyCell(row + l, j))
7515 {
7516 if (GetCellOverflow(row + l, j))
7517 {
7518 wxGridCellCoords cell(row + l, j);
7519 bool marked = false;
7520
7521 for (int k = 0; k < numCells; k++)
7522 {
7523 if ( cell == cells[k] )
7524 {
7525 marked = true;
7526 break;
7527 }
7528 }
7529
7530 if (!marked)
7531 {
7532 int count = redrawCells.GetCount();
7533 for (int k = 0; k < count; k++)
7534 {
7535 if ( cell == redrawCells[k] )
7536 {
7537 marked = true;
7538 break;
7539 }
7540 }
7541 if (!marked)
7542 redrawCells.Add( cell );
7543 }
7544 }
7545 break;
7546 }
7547 }
7548 }
7549 }
7550
7551 DrawCell( dc, cells[i] );
7552 }
7553
7554 numCells = redrawCells.GetCount();
7555
7556 for ( i = numCells - 1; i >= 0; i-- )
7557 {
7558 DrawCell( dc, redrawCells[i] );
7559 }
7560 }
7561
7562 void wxGrid::DrawGridSpace( wxDC& dc )
7563 {
7564 int cw, ch;
7565 m_gridWin->GetClientSize( &cw, &ch );
7566
7567 int right, bottom;
7568 CalcUnscrolledPosition( cw, ch, &right, &bottom );
7569
7570 int rightCol = m_numCols > 0 ? GetColRight(GetColAt( m_numCols - 1 )) : 0;
7571 int bottomRow = m_numRows > 0 ? GetRowBottom(m_numRows - 1) : 0;
7572
7573 if ( right > rightCol || bottom > bottomRow )
7574 {
7575 int left, top;
7576 CalcUnscrolledPosition( 0, 0, &left, &top );
7577
7578 dc.SetBrush( wxBrush(GetDefaultCellBackgroundColour(), wxSOLID) );
7579 dc.SetPen( *wxTRANSPARENT_PEN );
7580
7581 if ( right > rightCol )
7582 {
7583 dc.DrawRectangle( rightCol, top, right - rightCol, ch );
7584 }
7585
7586 if ( bottom > bottomRow )
7587 {
7588 dc.DrawRectangle( left, bottomRow, cw, bottom - bottomRow );
7589 }
7590 }
7591 }
7592
7593 void wxGrid::DrawCell( wxDC& dc, const wxGridCellCoords& coords )
7594 {
7595 int row = coords.GetRow();
7596 int col = coords.GetCol();
7597
7598 if ( GetColWidth(col) <= 0 || GetRowHeight(row) <= 0 )
7599 return;
7600
7601 // we draw the cell border ourselves
7602 #if !WXGRID_DRAW_LINES
7603 if ( m_gridLinesEnabled )
7604 DrawCellBorder( dc, coords );
7605 #endif
7606
7607 wxGridCellAttr* attr = GetCellAttr(row, col);
7608
7609 bool isCurrent = coords == m_currentCellCoords;
7610
7611 wxRect rect = CellToRect( row, col );
7612
7613 // if the editor is shown, we should use it and not the renderer
7614 // Note: However, only if it is really _shown_, i.e. not hidden!
7615 if ( isCurrent && IsCellEditControlShown() )
7616 {
7617 // NB: this "#if..." is temporary and fixes a problem where the
7618 // edit control is erased by this code after being rendered.
7619 // On wxMac (QD build only), the cell editor is a wxTextCntl and is rendered
7620 // implicitly, causing this out-of order render.
7621 #if !defined(__WXMAC__)
7622 wxGridCellEditor *editor = attr->GetEditor(this, row, col);
7623 editor->PaintBackground(rect, attr);
7624 editor->DecRef();
7625 #endif
7626 }
7627 else
7628 {
7629 // but all the rest is drawn by the cell renderer and hence may be customized
7630 wxGridCellRenderer *renderer = attr->GetRenderer(this, row, col);
7631 renderer->Draw(*this, *attr, dc, rect, row, col, IsInSelection(coords));
7632 renderer->DecRef();
7633 }
7634
7635 attr->DecRef();
7636 }
7637
7638 void wxGrid::DrawCellHighlight( wxDC& dc, const wxGridCellAttr *attr )
7639 {
7640 int row = m_currentCellCoords.GetRow();
7641 int col = m_currentCellCoords.GetCol();
7642
7643 if ( GetColWidth(col) <= 0 || GetRowHeight(row) <= 0 )
7644 return;
7645
7646 wxRect rect = CellToRect(row, col);
7647
7648 // hmmm... what could we do here to show that the cell is disabled?
7649 // for now, I just draw a thinner border than for the other ones, but
7650 // it doesn't look really good
7651
7652 int penWidth = attr->IsReadOnly() ? m_cellHighlightROPenWidth : m_cellHighlightPenWidth;
7653
7654 if (penWidth > 0)
7655 {
7656 // The center of the drawn line is where the position/width/height of
7657 // the rectangle is actually at (on wxMSW at least), so the
7658 // size of the rectangle is reduced to compensate for the thickness of
7659 // the line. If this is too strange on non-wxMSW platforms then
7660 // please #ifdef this appropriately.
7661 rect.x += penWidth / 2;
7662 rect.y += penWidth / 2;
7663 rect.width -= penWidth - 1;
7664 rect.height -= penWidth - 1;
7665
7666 // Now draw the rectangle
7667 // use the cellHighlightColour if the cell is inside a selection, this
7668 // will ensure the cell is always visible.
7669 dc.SetPen(wxPen(IsInSelection(row,col) ? m_selectionForeground : m_cellHighlightColour, penWidth, wxSOLID));
7670 dc.SetBrush(*wxTRANSPARENT_BRUSH);
7671 dc.DrawRectangle(rect);
7672 }
7673
7674 #if 0
7675 // VZ: my experiments with 3D borders...
7676
7677 // how to properly set colours for arbitrary bg?
7678 wxCoord x1 = rect.x,
7679 y1 = rect.y,
7680 x2 = rect.x + rect.width - 1,
7681 y2 = rect.y + rect.height - 1;
7682
7683 dc.SetPen(*wxWHITE_PEN);
7684 dc.DrawLine(x1, y1, x2, y1);
7685 dc.DrawLine(x1, y1, x1, y2);
7686
7687 dc.DrawLine(x1 + 1, y2 - 1, x2 - 1, y2 - 1);
7688 dc.DrawLine(x2 - 1, y1 + 1, x2 - 1, y2);
7689
7690 dc.SetPen(*wxBLACK_PEN);
7691 dc.DrawLine(x1, y2, x2, y2);
7692 dc.DrawLine(x2, y1, x2, y2 + 1);
7693 #endif
7694 }
7695
7696 wxPen wxGrid::GetDefaultGridLinePen()
7697 {
7698 return wxPen(GetGridLineColour(), 1, wxSOLID);
7699 }
7700
7701 wxPen wxGrid::GetRowGridLinePen(int WXUNUSED(row))
7702 {
7703 return GetDefaultGridLinePen();
7704 }
7705
7706 wxPen wxGrid::GetColGridLinePen(int WXUNUSED(col))
7707 {
7708 return GetDefaultGridLinePen();
7709 }
7710
7711 void wxGrid::DrawCellBorder( wxDC& dc, const wxGridCellCoords& coords )
7712 {
7713 int row = coords.GetRow();
7714 int col = coords.GetCol();
7715 if ( GetColWidth(col) <= 0 || GetRowHeight(row) <= 0 )
7716 return;
7717
7718
7719 wxRect rect = CellToRect( row, col );
7720
7721 // right hand border
7722 dc.SetPen( GetColGridLinePen(col) );
7723 dc.DrawLine( rect.x + rect.width, rect.y,
7724 rect.x + rect.width, rect.y + rect.height + 1 );
7725
7726 // bottom border
7727 dc.SetPen( GetRowGridLinePen(row) );
7728 dc.DrawLine( rect.x, rect.y + rect.height,
7729 rect.x + rect.width, rect.y + rect.height);
7730 }
7731
7732 void wxGrid::DrawHighlight(wxDC& dc, const wxGridCellCoordsArray& cells)
7733 {
7734 // This if block was previously in wxGrid::OnPaint but that doesn't
7735 // seem to get called under wxGTK - MB
7736 //
7737 if ( m_currentCellCoords == wxGridNoCellCoords &&
7738 m_numRows && m_numCols )
7739 {
7740 m_currentCellCoords.Set(0, 0);
7741 }
7742
7743 if ( IsCellEditControlShown() )
7744 {
7745 // don't show highlight when the edit control is shown
7746 return;
7747 }
7748
7749 // if the active cell was repainted, repaint its highlight too because it
7750 // might have been damaged by the grid lines
7751 size_t count = cells.GetCount();
7752 for ( size_t n = 0; n < count; n++ )
7753 {
7754 if ( cells[n] == m_currentCellCoords )
7755 {
7756 wxGridCellAttr* attr = GetCellAttr(m_currentCellCoords);
7757 DrawCellHighlight(dc, attr);
7758 attr->DecRef();
7759
7760 break;
7761 }
7762 }
7763 }
7764
7765 // TODO: remove this ???
7766 // This is used to redraw all grid lines e.g. when the grid line colour
7767 // has been changed
7768 //
7769 void wxGrid::DrawAllGridLines( wxDC& dc, const wxRegion & WXUNUSED(reg) )
7770 {
7771 #if !WXGRID_DRAW_LINES
7772 return;
7773 #endif
7774
7775 if ( !m_gridLinesEnabled || !m_numRows || !m_numCols )
7776 return;
7777
7778 int top, bottom, left, right;
7779
7780 #if 0 //#ifndef __WXGTK__
7781 if (reg.IsEmpty())
7782 {
7783 int cw, ch;
7784 m_gridWin->GetClientSize(&cw, &ch);
7785
7786 // virtual coords of visible area
7787 //
7788 CalcUnscrolledPosition( 0, 0, &left, &top );
7789 CalcUnscrolledPosition( cw, ch, &right, &bottom );
7790 }
7791 else
7792 {
7793 wxCoord x, y, w, h;
7794 reg.GetBox(x, y, w, h);
7795 CalcUnscrolledPosition( x, y, &left, &top );
7796 CalcUnscrolledPosition( x + w, y + h, &right, &bottom );
7797 }
7798 #else
7799 int cw, ch;
7800 m_gridWin->GetClientSize(&cw, &ch);
7801 CalcUnscrolledPosition( 0, 0, &left, &top );
7802 CalcUnscrolledPosition( cw, ch, &right, &bottom );
7803 #endif
7804
7805 // avoid drawing grid lines past the last row and col
7806 //
7807 right = wxMin( right, GetColRight(GetColAt( m_numCols - 1 )) );
7808 bottom = wxMin( bottom, GetRowBottom(m_numRows - 1) );
7809
7810 // no gridlines inside multicells, clip them out
7811 int leftCol = GetColPos( internalXToCol(left) );
7812 int topRow = internalYToRow(top);
7813 int rightCol = GetColPos( internalXToCol(right) );
7814 int bottomRow = internalYToRow(bottom);
7815
7816 wxRegion clippedcells(0, 0, cw, ch);
7817
7818 int i, j, cell_rows, cell_cols;
7819 wxRect rect;
7820
7821 for (j=topRow; j<=bottomRow; j++)
7822 {
7823 int colPos;
7824 for (colPos=leftCol; colPos<=rightCol; colPos++)
7825 {
7826 i = GetColAt( colPos );
7827
7828 GetCellSize( j, i, &cell_rows, &cell_cols );
7829 if ((cell_rows > 1) || (cell_cols > 1))
7830 {
7831 rect = CellToRect(j,i);
7832 CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
7833 clippedcells.Subtract(rect);
7834 }
7835 else if ((cell_rows < 0) || (cell_cols < 0))
7836 {
7837 rect = CellToRect(j + cell_rows, i + cell_cols);
7838 CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
7839 clippedcells.Subtract(rect);
7840 }
7841 }
7842 }
7843
7844 dc.SetClippingRegion( clippedcells );
7845
7846
7847 // horizontal grid lines
7848 //
7849 // already declared above - int i;
7850 for ( i = internalYToRow(top); i < m_numRows; i++ )
7851 {
7852 int bot = GetRowBottom(i) - 1;
7853
7854 if ( bot > bottom )
7855 {
7856 break;
7857 }
7858
7859 if ( bot >= top )
7860 {
7861 dc.SetPen( GetRowGridLinePen(i) );
7862 dc.DrawLine( left, bot, right, bot );
7863 }
7864 }
7865
7866 // vertical grid lines
7867 //
7868 int colPos;
7869 for ( colPos = leftCol; colPos < m_numCols; colPos++ )
7870 {
7871 i = GetColAt( colPos );
7872
7873 int colRight = GetColRight(i);
7874 #ifdef __WXGTK__
7875 if (GetLayoutDirection() != wxLayout_RightToLeft)
7876 #endif
7877 colRight--;
7878
7879 if ( colRight > right )
7880 {
7881 break;
7882 }
7883
7884 if ( colRight >= left )
7885 {
7886 dc.SetPen( GetColGridLinePen(i) );
7887 dc.DrawLine( colRight, top, colRight, bottom );
7888 }
7889 }
7890
7891 dc.DestroyClippingRegion();
7892 }
7893
7894 void wxGrid::DrawRowLabels( wxDC& dc, const wxArrayInt& rows)
7895 {
7896 if ( !m_numRows )
7897 return;
7898
7899 size_t i;
7900 size_t numLabels = rows.GetCount();
7901
7902 for ( i = 0; i < numLabels; i++ )
7903 {
7904 DrawRowLabel( dc, rows[i] );
7905 }
7906 }
7907
7908 void wxGrid::DrawRowLabel( wxDC& dc, int row )
7909 {
7910 if ( GetRowHeight(row) <= 0 || m_rowLabelWidth <= 0 )
7911 return;
7912
7913 wxRect rect;
7914
7915 #if 0
7916 def __WXGTK20__
7917 rect.SetX( 1 );
7918 rect.SetY( GetRowTop(row) + 1 );
7919 rect.SetWidth( m_rowLabelWidth - 2 );
7920 rect.SetHeight( GetRowHeight(row) - 2 );
7921
7922 CalcScrolledPosition( 0, rect.y, NULL, &rect.y );
7923
7924 wxWindowDC *win_dc = (wxWindowDC*) &dc;
7925
7926 wxRendererNative::Get().DrawHeaderButton( win_dc->m_owner, dc, rect, 0 );
7927 #else
7928 int rowTop = GetRowTop(row),
7929 rowBottom = GetRowBottom(row) - 1;
7930
7931 dc.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW), 1, wxSOLID) );
7932 dc.DrawLine( m_rowLabelWidth - 1, rowTop, m_rowLabelWidth - 1, rowBottom );
7933 dc.DrawLine( 0, rowTop, 0, rowBottom );
7934 dc.DrawLine( 0, rowBottom, m_rowLabelWidth, rowBottom );
7935
7936 dc.SetPen( *wxWHITE_PEN );
7937 dc.DrawLine( 1, rowTop, 1, rowBottom );
7938 dc.DrawLine( 1, rowTop, m_rowLabelWidth - 1, rowTop );
7939 #endif
7940
7941 dc.SetBackgroundMode( wxTRANSPARENT );
7942 dc.SetTextForeground( GetLabelTextColour() );
7943 dc.SetFont( GetLabelFont() );
7944
7945 int hAlign, vAlign;
7946 GetRowLabelAlignment( &hAlign, &vAlign );
7947
7948 rect.SetX( 2 );
7949 rect.SetY( GetRowTop(row) + 2 );
7950 rect.SetWidth( m_rowLabelWidth - 4 );
7951 rect.SetHeight( GetRowHeight(row) - 4 );
7952 DrawTextRectangle( dc, GetRowLabelValue( row ), rect, hAlign, vAlign );
7953 }
7954
7955 void wxGrid::DrawColLabels( wxDC& dc,const wxArrayInt& cols )
7956 {
7957 if ( !m_numCols )
7958 return;
7959
7960 size_t i;
7961 size_t numLabels = cols.GetCount();
7962
7963 for ( i = 0; i < numLabels; i++ )
7964 {
7965 DrawColLabel( dc, cols[i] );
7966 }
7967 }
7968
7969 void wxGrid::DrawColLabel( wxDC& dc, int col )
7970 {
7971 if ( GetColWidth(col) <= 0 || m_colLabelHeight <= 0 )
7972 return;
7973
7974 int colLeft = GetColLeft(col);
7975
7976 wxRect rect;
7977
7978 #if 0
7979 def __WXGTK20__
7980 rect.SetX( colLeft + 1 );
7981 rect.SetY( 1 );
7982 rect.SetWidth( GetColWidth(col) - 2 );
7983 rect.SetHeight( m_colLabelHeight - 2 );
7984
7985 wxWindowDC *win_dc = (wxWindowDC*) &dc;
7986
7987 wxRendererNative::Get().DrawHeaderButton( win_dc->m_owner, dc, rect, 0 );
7988 #else
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 #endif
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