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