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