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