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