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