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