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