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