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