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