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