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