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