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