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