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