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