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