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