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