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