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