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