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