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