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