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