]> git.saurik.com Git - wxWidgets.git/blob - src/generic/grid.cpp
fixed Printf() format spec warnings
[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) + wxT("\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 == wxT("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 == wxT("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 wxFAIL_MSG( wxString::Format
3078 (
3079 wxT("Called wxGridStringTable::DeleteRows(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu rows"),
3080 (unsigned long)pos,
3081 (unsigned long)numRows,
3082 (unsigned long)curNumRows
3083 ) );
3084
3085 return FALSE;
3086 }
3087
3088 if ( numRows > curNumRows - pos )
3089 {
3090 numRows = curNumRows - pos;
3091 }
3092
3093 if ( numRows >= curNumRows )
3094 {
3095 m_data.Empty(); // don't release memory just yet
3096 }
3097 else
3098 {
3099 m_data.RemoveAt( pos, numRows );
3100 }
3101 if ( GetView() )
3102 {
3103 wxGridTableMessage msg( this,
3104 wxGRIDTABLE_NOTIFY_ROWS_DELETED,
3105 pos,
3106 numRows );
3107
3108 GetView()->ProcessTableMessage( msg );
3109 }
3110
3111 return TRUE;
3112 }
3113
3114 bool wxGridStringTable::InsertCols( size_t pos, size_t numCols )
3115 {
3116 size_t row, col;
3117
3118 size_t curNumRows = m_data.GetCount();
3119 size_t curNumCols = ( curNumRows > 0 ? m_data[0].GetCount() :
3120 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3121
3122 if ( pos >= curNumCols )
3123 {
3124 return AppendCols( numCols );
3125 }
3126
3127 for ( row = 0; row < curNumRows; row++ )
3128 {
3129 for ( col = pos; col < pos + numCols; col++ )
3130 {
3131 m_data[row].Insert( wxEmptyString, col );
3132 }
3133 }
3134 if ( GetView() )
3135 {
3136 wxGridTableMessage msg( this,
3137 wxGRIDTABLE_NOTIFY_COLS_INSERTED,
3138 pos,
3139 numCols );
3140
3141 GetView()->ProcessTableMessage( msg );
3142 }
3143
3144 return TRUE;
3145 }
3146
3147 bool wxGridStringTable::AppendCols( size_t numCols )
3148 {
3149 size_t row;
3150
3151 size_t curNumRows = m_data.GetCount();
3152 #if 0
3153 if ( !curNumRows )
3154 {
3155 // TODO: something better than this ?
3156 //
3157 wxFAIL_MSG( wxT("Unable to append cols to a grid table with no rows.\nCall AppendRows() first") );
3158 return FALSE;
3159 }
3160 #endif
3161
3162 for ( row = 0; row < curNumRows; row++ )
3163 {
3164 m_data[row].Add( wxEmptyString, numCols );
3165 }
3166
3167 if ( GetView() )
3168 {
3169 wxGridTableMessage msg( this,
3170 wxGRIDTABLE_NOTIFY_COLS_APPENDED,
3171 numCols );
3172
3173 GetView()->ProcessTableMessage( msg );
3174 }
3175
3176 return TRUE;
3177 }
3178
3179 bool wxGridStringTable::DeleteCols( size_t pos, size_t numCols )
3180 {
3181 size_t row;
3182
3183 size_t curNumRows = m_data.GetCount();
3184 size_t curNumCols = ( curNumRows > 0 ? m_data[0].GetCount() :
3185 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3186
3187 if ( pos >= curNumCols )
3188 {
3189 wxFAIL_MSG( wxString::Format
3190 (
3191 wxT("Called wxGridStringTable::DeleteCols(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu cols"),
3192 (unsigned long)pos,
3193 (unsigned long)numCols,
3194 (unsigned long)curNumCols
3195 ) );
3196 return FALSE;
3197 }
3198
3199 if ( numCols > curNumCols - pos )
3200 {
3201 numCols = curNumCols - pos;
3202 }
3203
3204 for ( row = 0; row < curNumRows; row++ )
3205 {
3206 if ( numCols >= curNumCols )
3207 {
3208 m_data[row].Clear();
3209 }
3210 else
3211 {
3212 m_data[row].RemoveAt( pos, numCols );
3213 }
3214 }
3215 if ( GetView() )
3216 {
3217 wxGridTableMessage msg( this,
3218 wxGRIDTABLE_NOTIFY_COLS_DELETED,
3219 pos,
3220 numCols );
3221
3222 GetView()->ProcessTableMessage( msg );
3223 }
3224
3225 return TRUE;
3226 }
3227
3228 wxString wxGridStringTable::GetRowLabelValue( int row )
3229 {
3230 if ( row > (int)(m_rowLabels.GetCount()) - 1 )
3231 {
3232 // using default label
3233 //
3234 return wxGridTableBase::GetRowLabelValue( row );
3235 }
3236 else
3237 {
3238 return m_rowLabels[ row ];
3239 }
3240 }
3241
3242 wxString wxGridStringTable::GetColLabelValue( int col )
3243 {
3244 if ( col > (int)(m_colLabels.GetCount()) - 1 )
3245 {
3246 // using default label
3247 //
3248 return wxGridTableBase::GetColLabelValue( col );
3249 }
3250 else
3251 {
3252 return m_colLabels[ col ];
3253 }
3254 }
3255
3256 void wxGridStringTable::SetRowLabelValue( int row, const wxString& value )
3257 {
3258 if ( row > (int)(m_rowLabels.GetCount()) - 1 )
3259 {
3260 int n = m_rowLabels.GetCount();
3261 int i;
3262 for ( i = n; i <= row; i++ )
3263 {
3264 m_rowLabels.Add( wxGridTableBase::GetRowLabelValue(i) );
3265 }
3266 }
3267
3268 m_rowLabels[row] = value;
3269 }
3270
3271 void wxGridStringTable::SetColLabelValue( int col, const wxString& value )
3272 {
3273 if ( col > (int)(m_colLabels.GetCount()) - 1 )
3274 {
3275 int n = m_colLabels.GetCount();
3276 int i;
3277 for ( i = n; i <= col; i++ )
3278 {
3279 m_colLabels.Add( wxGridTableBase::GetColLabelValue(i) );
3280 }
3281 }
3282
3283 m_colLabels[col] = value;
3284 }
3285
3286
3287
3288 //////////////////////////////////////////////////////////////////////
3289 //////////////////////////////////////////////////////////////////////
3290
3291 IMPLEMENT_DYNAMIC_CLASS( wxGridRowLabelWindow, wxWindow )
3292
3293 BEGIN_EVENT_TABLE( wxGridRowLabelWindow, wxWindow )
3294 EVT_PAINT( wxGridRowLabelWindow::OnPaint )
3295 EVT_MOUSEWHEEL( wxGridRowLabelWindow::OnMouseWheel)
3296 EVT_MOUSE_EVENTS( wxGridRowLabelWindow::OnMouseEvent )
3297 EVT_KEY_DOWN( wxGridRowLabelWindow::OnKeyDown )
3298 EVT_KEY_UP( wxGridRowLabelWindow::OnKeyUp )
3299 END_EVENT_TABLE()
3300
3301 wxGridRowLabelWindow::wxGridRowLabelWindow( wxGrid *parent,
3302 wxWindowID id,
3303 const wxPoint &pos, const wxSize &size )
3304 : wxWindow( parent, id, pos, size, wxWANTS_CHARS )
3305 {
3306 m_owner = parent;
3307 }
3308
3309 void wxGridRowLabelWindow::OnPaint( wxPaintEvent& WXUNUSED(event) )
3310 {
3311 wxPaintDC dc(this);
3312
3313 // NO - don't do this because it will set both the x and y origin
3314 // coords to match the parent scrolled window and we just want to
3315 // set the y coord - MB
3316 //
3317 // m_owner->PrepareDC( dc );
3318
3319 int x, y;
3320 m_owner->CalcUnscrolledPosition( 0, 0, &x, &y );
3321 dc.SetDeviceOrigin( 0, -y );
3322
3323 wxArrayInt rows = m_owner->CalcRowLabelsExposed( GetUpdateRegion() );
3324 m_owner->DrawRowLabels( dc , rows );
3325 }
3326
3327
3328 void wxGridRowLabelWindow::OnMouseEvent( wxMouseEvent& event )
3329 {
3330 m_owner->ProcessRowLabelMouseEvent( event );
3331 }
3332
3333
3334 void wxGridRowLabelWindow::OnMouseWheel( wxMouseEvent& event )
3335 {
3336 m_owner->GetEventHandler()->ProcessEvent(event);
3337 }
3338
3339
3340 // This seems to be required for wxMotif otherwise the mouse
3341 // cursor must be in the cell edit control to get key events
3342 //
3343 void wxGridRowLabelWindow::OnKeyDown( wxKeyEvent& event )
3344 {
3345 if ( !m_owner->GetEventHandler()->ProcessEvent( event ) ) event.Skip();
3346 }
3347
3348 void wxGridRowLabelWindow::OnKeyUp( wxKeyEvent& event )
3349 {
3350 if ( !m_owner->GetEventHandler()->ProcessEvent( event ) ) event.Skip();
3351 }
3352
3353
3354
3355 //////////////////////////////////////////////////////////////////////
3356
3357 IMPLEMENT_DYNAMIC_CLASS( wxGridColLabelWindow, wxWindow )
3358
3359 BEGIN_EVENT_TABLE( wxGridColLabelWindow, wxWindow )
3360 EVT_PAINT( wxGridColLabelWindow::OnPaint )
3361 EVT_MOUSEWHEEL( wxGridColLabelWindow::OnMouseWheel)
3362 EVT_MOUSE_EVENTS( wxGridColLabelWindow::OnMouseEvent )
3363 EVT_KEY_DOWN( wxGridColLabelWindow::OnKeyDown )
3364 EVT_KEY_UP( wxGridColLabelWindow::OnKeyUp )
3365 END_EVENT_TABLE()
3366
3367 wxGridColLabelWindow::wxGridColLabelWindow( wxGrid *parent,
3368 wxWindowID id,
3369 const wxPoint &pos, const wxSize &size )
3370 : wxWindow( parent, id, pos, size, wxWANTS_CHARS )
3371 {
3372 m_owner = parent;
3373 }
3374
3375 void wxGridColLabelWindow::OnPaint( wxPaintEvent& WXUNUSED(event) )
3376 {
3377 wxPaintDC dc(this);
3378
3379 // NO - don't do this because it will set both the x and y origin
3380 // coords to match the parent scrolled window and we just want to
3381 // set the x coord - MB
3382 //
3383 // m_owner->PrepareDC( dc );
3384
3385 int x, y;
3386 m_owner->CalcUnscrolledPosition( 0, 0, &x, &y );
3387 dc.SetDeviceOrigin( -x, 0 );
3388
3389 wxArrayInt cols = m_owner->CalcColLabelsExposed( GetUpdateRegion() );
3390 m_owner->DrawColLabels( dc , cols );
3391 }
3392
3393
3394 void wxGridColLabelWindow::OnMouseEvent( wxMouseEvent& event )
3395 {
3396 m_owner->ProcessColLabelMouseEvent( event );
3397 }
3398
3399 void wxGridColLabelWindow::OnMouseWheel( wxMouseEvent& event )
3400 {
3401 m_owner->GetEventHandler()->ProcessEvent(event);
3402 }
3403
3404
3405 // This seems to be required for wxMotif otherwise the mouse
3406 // cursor must be in the cell edit control to get key events
3407 //
3408 void wxGridColLabelWindow::OnKeyDown( wxKeyEvent& event )
3409 {
3410 if ( !m_owner->GetEventHandler()->ProcessEvent( event ) ) event.Skip();
3411 }
3412
3413 void wxGridColLabelWindow::OnKeyUp( wxKeyEvent& event )
3414 {
3415 if ( !m_owner->GetEventHandler()->ProcessEvent( event ) ) event.Skip();
3416 }
3417
3418
3419
3420 //////////////////////////////////////////////////////////////////////
3421
3422 IMPLEMENT_DYNAMIC_CLASS( wxGridCornerLabelWindow, wxWindow )
3423
3424 BEGIN_EVENT_TABLE( wxGridCornerLabelWindow, wxWindow )
3425 EVT_MOUSEWHEEL( wxGridCornerLabelWindow::OnMouseWheel)
3426 EVT_MOUSE_EVENTS( wxGridCornerLabelWindow::OnMouseEvent )
3427 EVT_PAINT( wxGridCornerLabelWindow::OnPaint)
3428 EVT_KEY_DOWN( wxGridCornerLabelWindow::OnKeyDown )
3429 EVT_KEY_UP( wxGridCornerLabelWindow::OnKeyUp )
3430 END_EVENT_TABLE()
3431
3432 wxGridCornerLabelWindow::wxGridCornerLabelWindow( wxGrid *parent,
3433 wxWindowID id,
3434 const wxPoint &pos, const wxSize &size )
3435 : wxWindow( parent, id, pos, size, wxWANTS_CHARS )
3436 {
3437 m_owner = parent;
3438 }
3439
3440 void wxGridCornerLabelWindow::OnPaint( wxPaintEvent& WXUNUSED(event) )
3441 {
3442 wxPaintDC dc(this);
3443
3444 int client_height = 0;
3445 int client_width = 0;
3446 GetClientSize( &client_width, &client_height );
3447
3448 dc.SetPen( *wxBLACK_PEN );
3449 dc.DrawLine( client_width-1, client_height-1, client_width-1, 0 );
3450 dc.DrawLine( client_width-1, client_height-1, 0, client_height-1 );
3451
3452 dc.SetPen( *wxWHITE_PEN );
3453 dc.DrawLine( 0, 0, client_width, 0 );
3454 dc.DrawLine( 0, 0, 0, client_height );
3455 }
3456
3457
3458 void wxGridCornerLabelWindow::OnMouseEvent( wxMouseEvent& event )
3459 {
3460 m_owner->ProcessCornerLabelMouseEvent( event );
3461 }
3462
3463
3464 void wxGridCornerLabelWindow::OnMouseWheel( wxMouseEvent& event )
3465 {
3466 m_owner->GetEventHandler()->ProcessEvent(event);
3467 }
3468
3469 // This seems to be required for wxMotif otherwise the mouse
3470 // cursor must be in the cell edit control to get key events
3471 //
3472 void wxGridCornerLabelWindow::OnKeyDown( wxKeyEvent& event )
3473 {
3474 if ( !m_owner->GetEventHandler()->ProcessEvent( event ) ) event.Skip();
3475 }
3476
3477 void wxGridCornerLabelWindow::OnKeyUp( wxKeyEvent& event )
3478 {
3479 if ( !m_owner->GetEventHandler()->ProcessEvent( event ) ) event.Skip();
3480 }
3481
3482
3483
3484 //////////////////////////////////////////////////////////////////////
3485
3486 IMPLEMENT_DYNAMIC_CLASS( wxGridWindow, wxWindow )
3487
3488 BEGIN_EVENT_TABLE( wxGridWindow, wxWindow )
3489 EVT_PAINT( wxGridWindow::OnPaint )
3490 EVT_MOUSEWHEEL( wxGridWindow::OnMouseWheel)
3491 EVT_MOUSE_EVENTS( wxGridWindow::OnMouseEvent )
3492 EVT_KEY_DOWN( wxGridWindow::OnKeyDown )
3493 EVT_KEY_UP( wxGridWindow::OnKeyUp )
3494 EVT_ERASE_BACKGROUND( wxGridWindow::OnEraseBackground )
3495 END_EVENT_TABLE()
3496
3497 wxGridWindow::wxGridWindow( wxGrid *parent,
3498 wxGridRowLabelWindow *rowLblWin,
3499 wxGridColLabelWindow *colLblWin,
3500 wxWindowID id, const wxPoint &pos, const wxSize &size )
3501 : wxWindow( parent, id, pos, size, wxWANTS_CHARS, wxT("grid window") )
3502 {
3503 m_owner = parent;
3504 m_rowLabelWin = rowLblWin;
3505 m_colLabelWin = colLblWin;
3506 SetBackgroundColour(_T("WHITE"));
3507 }
3508
3509
3510 wxGridWindow::~wxGridWindow()
3511 {
3512 }
3513
3514
3515 void wxGridWindow::OnPaint( wxPaintEvent &WXUNUSED(event) )
3516 {
3517 wxPaintDC dc( this );
3518 m_owner->PrepareDC( dc );
3519 wxRegion reg = GetUpdateRegion();
3520 wxGridCellCoordsArray DirtyCells = m_owner->CalcCellsExposed( reg );
3521 m_owner->DrawGridCellArea( dc , DirtyCells);
3522 #if WXGRID_DRAW_LINES
3523 m_owner->DrawAllGridLines( dc, reg );
3524 #endif
3525 m_owner->DrawGridSpace( dc );
3526 m_owner->DrawHighlight( dc , DirtyCells );
3527 }
3528
3529
3530 void wxGridWindow::ScrollWindow( int dx, int dy, const wxRect *rect )
3531 {
3532 wxWindow::ScrollWindow( dx, dy, rect );
3533 m_rowLabelWin->ScrollWindow( 0, dy, rect );
3534 m_colLabelWin->ScrollWindow( dx, 0, rect );
3535 }
3536
3537
3538 void wxGridWindow::OnMouseEvent( wxMouseEvent& event )
3539 {
3540 m_owner->ProcessGridCellMouseEvent( event );
3541 }
3542
3543 void wxGridWindow::OnMouseWheel( wxMouseEvent& event )
3544 {
3545 m_owner->GetEventHandler()->ProcessEvent(event);
3546 }
3547
3548 // This seems to be required for wxMotif/wxGTK otherwise the mouse
3549 // cursor must be in the cell edit control to get key events
3550 //
3551 void wxGridWindow::OnKeyDown( wxKeyEvent& event )
3552 {
3553 if ( !m_owner->GetEventHandler()->ProcessEvent( event ) ) event.Skip();
3554 }
3555
3556 void wxGridWindow::OnKeyUp( wxKeyEvent& event )
3557 {
3558 if ( !m_owner->GetEventHandler()->ProcessEvent( event ) ) event.Skip();
3559 }
3560
3561 void wxGridWindow::OnEraseBackground( wxEraseEvent& WXUNUSED(event) )
3562 {
3563 }
3564
3565
3566 //////////////////////////////////////////////////////////////////////
3567
3568 // Internal Helper function for computing row or column from some
3569 // (unscrolled) coordinate value, using either
3570 // m_defaultRowHeight/m_defaultColWidth or binary search on array
3571 // of m_rowBottoms/m_ColRights to speed up the search!
3572
3573 // Internal helper macros for simpler use of that function
3574
3575 static int CoordToRowOrCol(int coord, int defaultDist, int minDist,
3576 const wxArrayInt& BorderArray, bool maxOnOverflow);
3577
3578 #define internalXToCol(x) CoordToRowOrCol(x, m_defaultColWidth, \
3579 WXGRID_MIN_COL_WIDTH, \
3580 m_colRights, TRUE)
3581 #define internalYToRow(y) CoordToRowOrCol(y, m_defaultRowHeight, \
3582 WXGRID_MIN_ROW_HEIGHT, \
3583 m_rowBottoms, TRUE)
3584 /////////////////////////////////////////////////////////////////////
3585
3586 IMPLEMENT_DYNAMIC_CLASS( wxGrid, wxScrolledWindow )
3587
3588 BEGIN_EVENT_TABLE( wxGrid, wxScrolledWindow )
3589 EVT_PAINT( wxGrid::OnPaint )
3590 EVT_SIZE( wxGrid::OnSize )
3591 EVT_KEY_DOWN( wxGrid::OnKeyDown )
3592 EVT_KEY_UP( wxGrid::OnKeyUp )
3593 EVT_ERASE_BACKGROUND( wxGrid::OnEraseBackground )
3594 END_EVENT_TABLE()
3595
3596 wxGrid::wxGrid( wxWindow *parent,
3597 wxWindowID id,
3598 const wxPoint& pos,
3599 const wxSize& size,
3600 long style,
3601 const wxString& name )
3602 : wxScrolledWindow( parent, id, pos, size, (style | wxWANTS_CHARS), name ),
3603 m_colMinWidths(GRID_HASH_SIZE),
3604 m_rowMinHeights(GRID_HASH_SIZE)
3605 {
3606 Create();
3607 }
3608
3609
3610 wxGrid::~wxGrid()
3611 {
3612 // Must do this or ~wxScrollHelper will pop the wrong event handler
3613 SetTargetWindow(this);
3614 ClearAttrCache();
3615 wxSafeDecRef(m_defaultCellAttr);
3616
3617 #ifdef DEBUG_ATTR_CACHE
3618 size_t total = gs_nAttrCacheHits + gs_nAttrCacheMisses;
3619 wxPrintf(_T("wxGrid attribute cache statistics: "
3620 "total: %u, hits: %u (%u%%)\n"),
3621 total, gs_nAttrCacheHits,
3622 total ? (gs_nAttrCacheHits*100) / total : 0);
3623 #endif
3624
3625 if (m_ownTable)
3626 delete m_table;
3627
3628 delete m_typeRegistry;
3629 delete m_selection;
3630 }
3631
3632
3633 //
3634 // ----- internal init and update functions
3635 //
3636
3637 void wxGrid::Create()
3638 {
3639 m_created = FALSE; // set to TRUE by CreateGrid
3640
3641 m_table = (wxGridTableBase *) NULL;
3642 m_ownTable = FALSE;
3643
3644 m_cellEditCtrlEnabled = FALSE;
3645
3646 m_defaultCellAttr = new wxGridCellAttr();
3647
3648 // Set default cell attributes
3649 m_defaultCellAttr->SetDefAttr(m_defaultCellAttr);
3650 m_defaultCellAttr->SetKind(wxGridCellAttr::Default);
3651 m_defaultCellAttr->SetFont(GetFont());
3652 m_defaultCellAttr->SetAlignment(wxALIGN_LEFT, wxALIGN_TOP);
3653 m_defaultCellAttr->SetTextColour(
3654 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT));
3655 m_defaultCellAttr->SetBackgroundColour(
3656 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW));
3657 m_defaultCellAttr->SetRenderer(new wxGridCellStringRenderer);
3658 m_defaultCellAttr->SetEditor(new wxGridCellTextEditor);
3659
3660
3661 m_numRows = 0;
3662 m_numCols = 0;
3663 m_currentCellCoords = wxGridNoCellCoords;
3664
3665 m_rowLabelWidth = WXGRID_DEFAULT_ROW_LABEL_WIDTH;
3666 m_colLabelHeight = WXGRID_DEFAULT_COL_LABEL_HEIGHT;
3667
3668 // create the type registry
3669 m_typeRegistry = new wxGridTypeRegistry;
3670 m_selection = NULL;
3671
3672 // subwindow components that make up the wxGrid
3673 m_cornerLabelWin = new wxGridCornerLabelWindow( this,
3674 -1,
3675 wxDefaultPosition,
3676 wxDefaultSize );
3677
3678 m_rowLabelWin = new wxGridRowLabelWindow( this,
3679 -1,
3680 wxDefaultPosition,
3681 wxDefaultSize );
3682
3683 m_colLabelWin = new wxGridColLabelWindow( this,
3684 -1,
3685 wxDefaultPosition,
3686 wxDefaultSize );
3687
3688 m_gridWin = new wxGridWindow( this,
3689 m_rowLabelWin,
3690 m_colLabelWin,
3691 -1,
3692 wxDefaultPosition,
3693 wxDefaultSize );
3694
3695 SetTargetWindow( m_gridWin );
3696
3697 Init();
3698 }
3699
3700
3701 bool wxGrid::CreateGrid( int numRows, int numCols,
3702 wxGrid::wxGridSelectionModes selmode )
3703 {
3704 wxCHECK_MSG( !m_created,
3705 FALSE,
3706 wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
3707
3708 m_numRows = numRows;
3709 m_numCols = numCols;
3710
3711 m_table = new wxGridStringTable( m_numRows, m_numCols );
3712 m_table->SetView( this );
3713 m_ownTable = TRUE;
3714 m_selection = new wxGridSelection( this, selmode );
3715
3716 CalcDimensions();
3717
3718 m_created = TRUE;
3719
3720 return m_created;
3721 }
3722
3723 void wxGrid::SetSelectionMode(wxGrid::wxGridSelectionModes selmode)
3724 {
3725 wxCHECK_RET( m_created,
3726 wxT("Called wxGrid::SetSelectionMode() before calling CreateGrid()") );
3727
3728 m_selection->SetSelectionMode( selmode );
3729 }
3730
3731 wxGrid::wxGridSelectionModes wxGrid::GetSelectionMode() const
3732 {
3733 wxCHECK_MSG( m_created, wxGrid::wxGridSelectCells,
3734 wxT("Called wxGrid::GetSelectionMode() before calling CreateGrid()") );
3735
3736 return m_selection->GetSelectionMode();
3737 }
3738
3739 bool wxGrid::SetTable( wxGridTableBase *table, bool takeOwnership,
3740 wxGrid::wxGridSelectionModes selmode )
3741 {
3742 if ( m_created )
3743 {
3744 // RD: Actually, this should probably be allowed. I think it would be
3745 // nice to be able to switch multiple Tables in and out of a single
3746 // View at runtime. Is there anything in the implementation that
3747 // would prevent this?
3748
3749 // At least, you now have to cope with m_selection
3750 wxFAIL_MSG( wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
3751 return FALSE;
3752 }
3753 else
3754 {
3755 m_numRows = table->GetNumberRows();
3756 m_numCols = table->GetNumberCols();
3757
3758 m_table = table;
3759 m_table->SetView( this );
3760 if (takeOwnership)
3761 m_ownTable = TRUE;
3762 m_selection = new wxGridSelection( this, selmode );
3763
3764 CalcDimensions();
3765
3766 m_created = TRUE;
3767 }
3768
3769 return m_created;
3770 }
3771
3772
3773 void wxGrid::Init()
3774 {
3775 m_rowLabelWidth = WXGRID_DEFAULT_ROW_LABEL_WIDTH;
3776 m_colLabelHeight = WXGRID_DEFAULT_COL_LABEL_HEIGHT;
3777
3778 if ( m_rowLabelWin )
3779 {
3780 m_labelBackgroundColour = m_rowLabelWin->GetBackgroundColour();
3781 }
3782 else
3783 {
3784 m_labelBackgroundColour = wxColour( _T("WHITE") );
3785 }
3786
3787 m_labelTextColour = wxColour( _T("BLACK") );
3788
3789 // init attr cache
3790 m_attrCache.row = -1;
3791
3792 // TODO: something better than this ?
3793 //
3794 m_labelFont = this->GetFont();
3795 m_labelFont.SetWeight( m_labelFont.GetWeight() + 2 );
3796
3797 m_rowLabelHorizAlign = wxALIGN_LEFT;
3798 m_rowLabelVertAlign = wxALIGN_CENTRE;
3799
3800 m_colLabelHorizAlign = wxALIGN_CENTRE;
3801 m_colLabelVertAlign = wxALIGN_TOP;
3802
3803 m_defaultColWidth = WXGRID_DEFAULT_COL_WIDTH;
3804 m_defaultRowHeight = m_gridWin->GetCharHeight();
3805
3806 #if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl()
3807 m_defaultRowHeight += 8;
3808 #else
3809 m_defaultRowHeight += 4;
3810 #endif
3811
3812 m_gridLineColour = wxColour( 128, 128, 255 );
3813 m_gridLinesEnabled = TRUE;
3814 m_cellHighlightColour = m_gridLineColour;
3815 m_cellHighlightPenWidth = 2;
3816 m_cellHighlightROPenWidth = 1;
3817
3818 m_cursorMode = WXGRID_CURSOR_SELECT_CELL;
3819 m_winCapture = (wxWindow *)NULL;
3820 m_canDragRowSize = TRUE;
3821 m_canDragColSize = TRUE;
3822 m_canDragGridSize = TRUE;
3823 m_dragLastPos = -1;
3824 m_dragRowOrCol = -1;
3825 m_isDragging = FALSE;
3826 m_startDragPos = wxDefaultPosition;
3827
3828 m_waitForSlowClick = FALSE;
3829
3830 m_rowResizeCursor = wxCursor( wxCURSOR_SIZENS );
3831 m_colResizeCursor = wxCursor( wxCURSOR_SIZEWE );
3832
3833 m_currentCellCoords = wxGridNoCellCoords;
3834
3835 m_selectingTopLeft = wxGridNoCellCoords;
3836 m_selectingBottomRight = wxGridNoCellCoords;
3837 m_selectionBackground = wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT);
3838 m_selectionForeground = wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT);
3839
3840 m_editable = TRUE; // default for whole grid
3841
3842 m_inOnKeyDown = FALSE;
3843 m_batchCount = 0;
3844
3845 m_extraWidth =
3846 m_extraHeight = 0;
3847 }
3848
3849 // ----------------------------------------------------------------------------
3850 // the idea is to call these functions only when necessary because they create
3851 // quite big arrays which eat memory mostly unnecessary - in particular, if
3852 // default widths/heights are used for all rows/columns, we may not use these
3853 // arrays at all
3854 //
3855 // with some extra code, it should be possible to only store the
3856 // widths/heights different from default ones but this will be done later...
3857 // ----------------------------------------------------------------------------
3858
3859 void wxGrid::InitRowHeights()
3860 {
3861 m_rowHeights.Empty();
3862 m_rowBottoms.Empty();
3863
3864 m_rowHeights.Alloc( m_numRows );
3865 m_rowBottoms.Alloc( m_numRows );
3866
3867 int rowBottom = 0;
3868
3869 m_rowHeights.Add( m_defaultRowHeight, m_numRows );
3870
3871 for ( int i = 0; i < m_numRows; i++ )
3872 {
3873 rowBottom += m_defaultRowHeight;
3874 m_rowBottoms.Add( rowBottom );
3875 }
3876 }
3877
3878 void wxGrid::InitColWidths()
3879 {
3880 m_colWidths.Empty();
3881 m_colRights.Empty();
3882
3883 m_colWidths.Alloc( m_numCols );
3884 m_colRights.Alloc( m_numCols );
3885 int colRight = 0;
3886
3887 m_colWidths.Add( m_defaultColWidth, m_numCols );
3888
3889 for ( int i = 0; i < m_numCols; i++ )
3890 {
3891 colRight += m_defaultColWidth;
3892 m_colRights.Add( colRight );
3893 }
3894 }
3895
3896 int wxGrid::GetColWidth(int col) const
3897 {
3898 return m_colWidths.IsEmpty() ? m_defaultColWidth : m_colWidths[col];
3899 }
3900
3901 int wxGrid::GetColLeft(int col) const
3902 {
3903 return m_colRights.IsEmpty() ? col * m_defaultColWidth
3904 : m_colRights[col] - m_colWidths[col];
3905 }
3906
3907 int wxGrid::GetColRight(int col) const
3908 {
3909 return m_colRights.IsEmpty() ? (col + 1) * m_defaultColWidth
3910 : m_colRights[col];
3911 }
3912
3913 int wxGrid::GetRowHeight(int row) const
3914 {
3915 return m_rowHeights.IsEmpty() ? m_defaultRowHeight : m_rowHeights[row];
3916 }
3917
3918 int wxGrid::GetRowTop(int row) const
3919 {
3920 return m_rowBottoms.IsEmpty() ? row * m_defaultRowHeight
3921 : m_rowBottoms[row] - m_rowHeights[row];
3922 }
3923
3924 int wxGrid::GetRowBottom(int row) const
3925 {
3926 return m_rowBottoms.IsEmpty() ? (row + 1) * m_defaultRowHeight
3927 : m_rowBottoms[row];
3928 }
3929
3930 void wxGrid::CalcDimensions()
3931 {
3932 int cw, ch;
3933 GetClientSize( &cw, &ch );
3934
3935 if ( m_rowLabelWin->IsShown() )
3936 cw -= m_rowLabelWidth;
3937 if ( m_colLabelWin->IsShown() )
3938 ch -= m_colLabelHeight;
3939
3940 // grid total size
3941 int w = m_numCols > 0 ? GetColRight(m_numCols - 1) + m_extraWidth + 1 : 0;
3942 int h = m_numRows > 0 ? GetRowBottom(m_numRows - 1) + m_extraHeight + 1 : 0;
3943
3944 // preserve (more or less) the previous position
3945 int x, y;
3946 GetViewStart( &x, &y );
3947
3948 // maybe we don't need scrollbars at all?
3949 //
3950 // also adjust the position to be valid for the new scroll rangs
3951 if ( w <= cw )
3952 {
3953 w = x = 0;
3954 }
3955 else
3956 {
3957 if ( x >= w )
3958 x = w - 1;
3959 }
3960
3961 if ( h <= ch )
3962 {
3963 h = y = 0;
3964 }
3965 else
3966 {
3967 if ( y >= h )
3968 y = h - 1;
3969 }
3970
3971 // do set scrollbar parameters
3972 SetScrollbars( GRID_SCROLL_LINE_X, GRID_SCROLL_LINE_Y,
3973 GetScrollX(w), GetScrollY(h), x, y,
3974 GetBatchCount() != 0);
3975
3976 // if our OnSize() hadn't been called (it would if we have scrollbars), we
3977 // still must reposition the children
3978 CalcWindowSizes();
3979 }
3980
3981
3982 void wxGrid::CalcWindowSizes()
3983 {
3984 int cw, ch;
3985 GetClientSize( &cw, &ch );
3986
3987 if ( m_cornerLabelWin->IsShown() )
3988 m_cornerLabelWin->SetSize( 0, 0, m_rowLabelWidth, m_colLabelHeight );
3989
3990 if ( m_colLabelWin->IsShown() )
3991 m_colLabelWin->SetSize( m_rowLabelWidth, 0, cw-m_rowLabelWidth, m_colLabelHeight);
3992
3993 if ( m_rowLabelWin->IsShown() )
3994 m_rowLabelWin->SetSize( 0, m_colLabelHeight, m_rowLabelWidth, ch-m_colLabelHeight);
3995
3996 if ( m_gridWin->IsShown() )
3997 m_gridWin->SetSize( m_rowLabelWidth, m_colLabelHeight, cw-m_rowLabelWidth, ch-m_colLabelHeight);
3998 }
3999
4000
4001 // this is called when the grid table sends a message to say that it
4002 // has been redimensioned
4003 //
4004 bool wxGrid::Redimension( wxGridTableMessage& msg )
4005 {
4006 int i;
4007 bool result = FALSE;
4008
4009 // Clear the attribute cache as the attribute might refer to a different
4010 // cell than stored in the cache after adding/removing rows/columns.
4011 ClearAttrCache();
4012 // By the same reasoning, the editor should be dismissed if columns are
4013 // added or removed. And for consistency, it should IMHO always be
4014 // removed, not only if the cell "underneath" it actually changes.
4015 // For now, I intentionally do not save the editor's content as the
4016 // cell it might want to save that stuff to might no longer exist.
4017 HideCellEditControl();
4018 #if 0
4019 // if we were using the default widths/heights so far, we must change them
4020 // now
4021 if ( m_colWidths.IsEmpty() )
4022 {
4023 InitColWidths();
4024 }
4025
4026 if ( m_rowHeights.IsEmpty() )
4027 {
4028 InitRowHeights();
4029 }
4030 #endif
4031
4032 switch ( msg.GetId() )
4033 {
4034 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED:
4035 {
4036 size_t pos = msg.GetCommandInt();
4037 int numRows = msg.GetCommandInt2();
4038
4039 m_numRows += numRows;
4040
4041 if ( !m_rowHeights.IsEmpty() )
4042 {
4043 m_rowHeights.Insert( m_defaultRowHeight, pos, numRows );
4044 m_rowBottoms.Insert( 0, pos, numRows );
4045
4046 int bottom = 0;
4047 if ( pos > 0 ) bottom = m_rowBottoms[pos-1];
4048
4049 for ( i = pos; i < m_numRows; i++ )
4050 {
4051 bottom += m_rowHeights[i];
4052 m_rowBottoms[i] = bottom;
4053 }
4054 }
4055 if ( m_currentCellCoords == wxGridNoCellCoords )
4056 {
4057 // if we have just inserted cols into an empty grid the current
4058 // cell will be undefined...
4059 //
4060 SetCurrentCell( 0, 0 );
4061 }
4062
4063 if ( m_selection )
4064 m_selection->UpdateRows( pos, numRows );
4065 wxGridCellAttrProvider * attrProvider = m_table->GetAttrProvider();
4066 if (attrProvider)
4067 attrProvider->UpdateAttrRows( pos, numRows );
4068
4069 if ( !GetBatchCount() )
4070 {
4071 CalcDimensions();
4072 m_rowLabelWin->Refresh();
4073 }
4074 }
4075 result = TRUE;
4076 break;
4077
4078 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED:
4079 {
4080 int numRows = msg.GetCommandInt();
4081 int oldNumRows = m_numRows;
4082 m_numRows += numRows;
4083
4084 if ( !m_rowHeights.IsEmpty() )
4085 {
4086 m_rowHeights.Add( m_defaultRowHeight, numRows );
4087 m_rowBottoms.Add( 0, numRows );
4088
4089 int bottom = 0;
4090 if ( oldNumRows > 0 ) bottom = m_rowBottoms[oldNumRows-1];
4091
4092 for ( i = oldNumRows; i < m_numRows; i++ )
4093 {
4094 bottom += m_rowHeights[i];
4095 m_rowBottoms[i] = bottom;
4096 }
4097 }
4098 if ( m_currentCellCoords == wxGridNoCellCoords )
4099 {
4100 // if we have just inserted cols into an empty grid the current
4101 // cell will be undefined...
4102 //
4103 SetCurrentCell( 0, 0 );
4104 }
4105 if ( !GetBatchCount() )
4106 {
4107 CalcDimensions();
4108 m_rowLabelWin->Refresh();
4109 }
4110 }
4111 result = TRUE;
4112 break;
4113
4114 case wxGRIDTABLE_NOTIFY_ROWS_DELETED:
4115 {
4116 size_t pos = msg.GetCommandInt();
4117 int numRows = msg.GetCommandInt2();
4118 m_numRows -= numRows;
4119
4120 if ( !m_rowHeights.IsEmpty() )
4121 {
4122 m_rowHeights.RemoveAt( pos, numRows );
4123 m_rowBottoms.RemoveAt( pos, numRows );
4124
4125 int h = 0;
4126 for ( i = 0; i < m_numRows; i++ )
4127 {
4128 h += m_rowHeights[i];
4129 m_rowBottoms[i] = h;
4130 }
4131 }
4132 if ( !m_numRows )
4133 {
4134 m_currentCellCoords = wxGridNoCellCoords;
4135 }
4136 else
4137 {
4138 if ( m_currentCellCoords.GetRow() >= m_numRows )
4139 m_currentCellCoords.Set( 0, 0 );
4140 }
4141
4142 if ( m_selection )
4143 m_selection->UpdateRows( pos, -((int)numRows) );
4144 wxGridCellAttrProvider * attrProvider = m_table->GetAttrProvider();
4145 if (attrProvider) {
4146 attrProvider->UpdateAttrRows( pos, -((int)numRows) );
4147 // ifdef'd out following patch from Paul Gammans
4148 #if 0
4149 // No need to touch column attributes, unless we
4150 // removed _all_ rows, in this case, we remove
4151 // all column attributes.
4152 // I hate to do this here, but the
4153 // needed data is not available inside UpdateAttrRows.
4154 if ( !GetNumberRows() )
4155 attrProvider->UpdateAttrCols( 0, -GetNumberCols() );
4156 #endif
4157 }
4158 if ( !GetBatchCount() )
4159 {
4160 CalcDimensions();
4161 m_rowLabelWin->Refresh();
4162 }
4163 }
4164 result = TRUE;
4165 break;
4166
4167 case wxGRIDTABLE_NOTIFY_COLS_INSERTED:
4168 {
4169 size_t pos = msg.GetCommandInt();
4170 int numCols = msg.GetCommandInt2();
4171 m_numCols += numCols;
4172
4173 if ( !m_colWidths.IsEmpty() )
4174 {
4175 m_colWidths.Insert( m_defaultColWidth, pos, numCols );
4176 m_colRights.Insert( 0, pos, numCols );
4177
4178 int right = 0;
4179 if ( pos > 0 ) right = m_colRights[pos-1];
4180
4181 for ( i = pos; i < m_numCols; i++ )
4182 {
4183 right += m_colWidths[i];
4184 m_colRights[i] = right;
4185 }
4186 }
4187 if ( m_currentCellCoords == wxGridNoCellCoords )
4188 {
4189 // if we have just inserted cols into an empty grid the current
4190 // cell will be undefined...
4191 //
4192 SetCurrentCell( 0, 0 );
4193 }
4194
4195 if ( m_selection )
4196 m_selection->UpdateCols( pos, numCols );
4197 wxGridCellAttrProvider * attrProvider = m_table->GetAttrProvider();
4198 if (attrProvider)
4199 attrProvider->UpdateAttrCols( pos, numCols );
4200 if ( !GetBatchCount() )
4201 {
4202 CalcDimensions();
4203 m_colLabelWin->Refresh();
4204 }
4205
4206 }
4207 result = TRUE;
4208 break;
4209
4210 case wxGRIDTABLE_NOTIFY_COLS_APPENDED:
4211 {
4212 int numCols = msg.GetCommandInt();
4213 int oldNumCols = m_numCols;
4214 m_numCols += numCols;
4215 if ( !m_colWidths.IsEmpty() )
4216 {
4217 m_colWidths.Add( m_defaultColWidth, numCols );
4218 m_colRights.Add( 0, numCols );
4219
4220 int right = 0;
4221 if ( oldNumCols > 0 ) right = m_colRights[oldNumCols-1];
4222
4223 for ( i = oldNumCols; i < m_numCols; i++ )
4224 {
4225 right += m_colWidths[i];
4226 m_colRights[i] = right;
4227 }
4228 }
4229 if ( m_currentCellCoords == wxGridNoCellCoords )
4230 {
4231 // if we have just inserted cols into an empty grid the current
4232 // cell will be undefined...
4233 //
4234 SetCurrentCell( 0, 0 );
4235 }
4236 if ( !GetBatchCount() )
4237 {
4238 CalcDimensions();
4239 m_colLabelWin->Refresh();
4240 }
4241 }
4242 result = TRUE;
4243 break;
4244
4245 case wxGRIDTABLE_NOTIFY_COLS_DELETED:
4246 {
4247 size_t pos = msg.GetCommandInt();
4248 int numCols = msg.GetCommandInt2();
4249 m_numCols -= numCols;
4250
4251 if ( !m_colWidths.IsEmpty() )
4252 {
4253 m_colWidths.RemoveAt( pos, numCols );
4254 m_colRights.RemoveAt( pos, numCols );
4255
4256 int w = 0;
4257 for ( i = 0; i < m_numCols; i++ )
4258 {
4259 w += m_colWidths[i];
4260 m_colRights[i] = w;
4261 }
4262 }
4263 if ( !m_numCols )
4264 {
4265 m_currentCellCoords = wxGridNoCellCoords;
4266 }
4267 else
4268 {
4269 if ( m_currentCellCoords.GetCol() >= m_numCols )
4270 m_currentCellCoords.Set( 0, 0 );
4271 }
4272
4273 if ( m_selection )
4274 m_selection->UpdateCols( pos, -((int)numCols) );
4275 wxGridCellAttrProvider * attrProvider = m_table->GetAttrProvider();
4276 if (attrProvider) {
4277 attrProvider->UpdateAttrCols( pos, -((int)numCols) );
4278 // ifdef'd out following patch from Paul Gammans
4279 #if 0
4280 // No need to touch row attributes, unless we
4281 // removed _all_ columns, in this case, we remove
4282 // all row attributes.
4283 // I hate to do this here, but the
4284 // needed data is not available inside UpdateAttrCols.
4285 if ( !GetNumberCols() )
4286 attrProvider->UpdateAttrRows( 0, -GetNumberRows() );
4287 #endif
4288 }
4289 if ( !GetBatchCount() )
4290 {
4291 CalcDimensions();
4292 m_colLabelWin->Refresh();
4293 }
4294 }
4295 result = TRUE;
4296 break;
4297 }
4298
4299 if (result && !GetBatchCount() )
4300 m_gridWin->Refresh();
4301 return result;
4302 }
4303
4304
4305 wxArrayInt wxGrid::CalcRowLabelsExposed( const wxRegion& reg )
4306 {
4307 wxRegionIterator iter( reg );
4308 wxRect r;
4309
4310 wxArrayInt rowlabels;
4311
4312 int top, bottom;
4313 while ( iter )
4314 {
4315 r = iter.GetRect();
4316
4317 // TODO: remove this when we can...
4318 // There is a bug in wxMotif that gives garbage update
4319 // rectangles if you jump-scroll a long way by clicking the
4320 // scrollbar with middle button. This is a work-around
4321 //
4322 #if defined(__WXMOTIF__)
4323 int cw, ch;
4324 m_gridWin->GetClientSize( &cw, &ch );
4325 if ( r.GetTop() > ch ) r.SetTop( 0 );
4326 r.SetBottom( wxMin( r.GetBottom(), ch ) );
4327 #endif
4328
4329 // logical bounds of update region
4330 //
4331 int dummy;
4332 CalcUnscrolledPosition( 0, r.GetTop(), &dummy, &top );
4333 CalcUnscrolledPosition( 0, r.GetBottom(), &dummy, &bottom );
4334
4335 // find the row labels within these bounds
4336 //
4337 int row;
4338 for ( row = internalYToRow(top); row < m_numRows; row++ )
4339 {
4340 if ( GetRowBottom(row) < top )
4341 continue;
4342
4343 if ( GetRowTop(row) > bottom )
4344 break;
4345
4346 rowlabels.Add( row );
4347 }
4348
4349 iter++ ;
4350 }
4351
4352 return rowlabels;
4353 }
4354
4355
4356 wxArrayInt wxGrid::CalcColLabelsExposed( const wxRegion& reg )
4357 {
4358 wxRegionIterator iter( reg );
4359 wxRect r;
4360
4361 wxArrayInt colLabels;
4362
4363 int left, right;
4364 while ( iter )
4365 {
4366 r = iter.GetRect();
4367
4368 // TODO: remove this when we can...
4369 // There is a bug in wxMotif that gives garbage update
4370 // rectangles if you jump-scroll a long way by clicking the
4371 // scrollbar with middle button. This is a work-around
4372 //
4373 #if defined(__WXMOTIF__)
4374 int cw, ch;
4375 m_gridWin->GetClientSize( &cw, &ch );
4376 if ( r.GetLeft() > cw ) r.SetLeft( 0 );
4377 r.SetRight( wxMin( r.GetRight(), cw ) );
4378 #endif
4379
4380 // logical bounds of update region
4381 //
4382 int dummy;
4383 CalcUnscrolledPosition( r.GetLeft(), 0, &left, &dummy );
4384 CalcUnscrolledPosition( r.GetRight(), 0, &right, &dummy );
4385
4386 // find the cells within these bounds
4387 //
4388 int col;
4389 for ( col = internalXToCol(left); col < m_numCols; col++ )
4390 {
4391 if ( GetColRight(col) < left )
4392 continue;
4393
4394 if ( GetColLeft(col) > right )
4395 break;
4396
4397 colLabels.Add( col );
4398 }
4399
4400 iter++ ;
4401 }
4402 return colLabels;
4403 }
4404
4405
4406 wxGridCellCoordsArray wxGrid::CalcCellsExposed( const wxRegion& reg )
4407 {
4408 wxRegionIterator iter( reg );
4409 wxRect r;
4410
4411 wxGridCellCoordsArray cellsExposed;
4412
4413 int left, top, right, bottom;
4414 while ( iter )
4415 {
4416 r = iter.GetRect();
4417
4418 // TODO: remove this when we can...
4419 // There is a bug in wxMotif that gives garbage update
4420 // rectangles if you jump-scroll a long way by clicking the
4421 // scrollbar with middle button. This is a work-around
4422 //
4423 #if defined(__WXMOTIF__)
4424 int cw, ch;
4425 m_gridWin->GetClientSize( &cw, &ch );
4426 if ( r.GetTop() > ch ) r.SetTop( 0 );
4427 if ( r.GetLeft() > cw ) r.SetLeft( 0 );
4428 r.SetRight( wxMin( r.GetRight(), cw ) );
4429 r.SetBottom( wxMin( r.GetBottom(), ch ) );
4430 #endif
4431
4432 // logical bounds of update region
4433 //
4434 CalcUnscrolledPosition( r.GetLeft(), r.GetTop(), &left, &top );
4435 CalcUnscrolledPosition( r.GetRight(), r.GetBottom(), &right, &bottom );
4436
4437 // find the cells within these bounds
4438 //
4439 int row, col;
4440 for ( row = internalYToRow(top); row < m_numRows; row++ )
4441 {
4442 if ( GetRowBottom(row) <= top )
4443 continue;
4444
4445 if ( GetRowTop(row) > bottom )
4446 break;
4447
4448 for ( col = internalXToCol(left); col < m_numCols; col++ )
4449 {
4450 if ( GetColRight(col) <= left )
4451 continue;
4452
4453 if ( GetColLeft(col) > right )
4454 break;
4455
4456 cellsExposed.Add( wxGridCellCoords( row, col ) );
4457 }
4458 }
4459
4460 iter++;
4461 }
4462
4463 return cellsExposed;
4464 }
4465
4466
4467 void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent& event )
4468 {
4469 int x, y, row;
4470 wxPoint pos( event.GetPosition() );
4471 CalcUnscrolledPosition( pos.x, pos.y, &x, &y );
4472
4473 if ( event.Dragging() )
4474 {
4475 if (!m_isDragging)
4476 {
4477 m_isDragging = TRUE;
4478 m_rowLabelWin->CaptureMouse();
4479 }
4480
4481 if ( event.LeftIsDown() )
4482 {
4483 switch( m_cursorMode )
4484 {
4485 case WXGRID_CURSOR_RESIZE_ROW:
4486 {
4487 int cw, ch, left, dummy;
4488 m_gridWin->GetClientSize( &cw, &ch );
4489 CalcUnscrolledPosition( 0, 0, &left, &dummy );
4490
4491 wxClientDC dc( m_gridWin );
4492 PrepareDC( dc );
4493 y = wxMax( y,
4494 GetRowTop(m_dragRowOrCol) +
4495 GetRowMinimalHeight(m_dragRowOrCol) );
4496 dc.SetLogicalFunction(wxINVERT);
4497 if ( m_dragLastPos >= 0 )
4498 {
4499 dc.DrawLine( left, m_dragLastPos, left+cw, m_dragLastPos );
4500 }
4501 dc.DrawLine( left, y, left+cw, y );
4502 m_dragLastPos = y;
4503 }
4504 break;
4505
4506 case WXGRID_CURSOR_SELECT_ROW:
4507 if ( (row = YToRow( y )) >= 0 )
4508 {
4509 if ( m_selection )
4510 {
4511 m_selection->SelectRow( row,
4512 event.ControlDown(),
4513 event.ShiftDown(),
4514 event.AltDown(),
4515 event.MetaDown() );
4516 }
4517 }
4518
4519 // default label to suppress warnings about "enumeration value
4520 // 'xxx' not handled in switch
4521 default:
4522 break;
4523 }
4524 }
4525 return;
4526 }
4527
4528 if ( m_isDragging && (event.Entering() || event.Leaving()) )
4529 return;
4530
4531 if (m_isDragging)
4532 {
4533 if (m_rowLabelWin->HasCapture()) m_rowLabelWin->ReleaseMouse();
4534 m_isDragging = FALSE;
4535 }
4536
4537 // ------------ Entering or leaving the window
4538 //
4539 if ( event.Entering() || event.Leaving() )
4540 {
4541 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_rowLabelWin);
4542 }
4543
4544
4545 // ------------ Left button pressed
4546 //
4547 else if ( event.LeftDown() )
4548 {
4549 // don't send a label click event for a hit on the
4550 // edge of the row label - this is probably the user
4551 // wanting to resize the row
4552 //
4553 if ( YToEdgeOfRow(y) < 0 )
4554 {
4555 row = YToRow(y);
4556 if ( row >= 0 &&
4557 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK, row, -1, event ) )
4558 {
4559 if ( !event.ShiftDown() && !event.ControlDown() )
4560 ClearSelection();
4561 else if ( m_selection )
4562 {
4563 if ( event.ShiftDown() )
4564 {
4565 m_selection->SelectBlock( m_currentCellCoords.GetRow(),
4566 0,
4567 row,
4568 GetNumberCols() - 1,
4569 event.ControlDown(),
4570 event.ShiftDown(),
4571 event.AltDown(),
4572 event.MetaDown() );
4573 }
4574 else
4575 {
4576 m_selection->SelectRow( row,
4577 event.ControlDown(),
4578 event.ShiftDown(),
4579 event.AltDown(),
4580 event.MetaDown() );
4581 }
4582 }
4583
4584 ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW, m_rowLabelWin);
4585 }
4586 }
4587 else
4588 {
4589 // starting to drag-resize a row
4590 //
4591 if ( CanDragRowSize() )
4592 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW, m_rowLabelWin);
4593 }
4594 }
4595
4596
4597 // ------------ Left double click
4598 //
4599 else if (event.LeftDClick() )
4600 {
4601 if ( YToEdgeOfRow(y) < 0 )
4602 {
4603 row = YToRow(y);
4604 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK, row, -1, event );
4605 }
4606 }
4607
4608
4609 // ------------ Left button released
4610 //
4611 else if ( event.LeftUp() )
4612 {
4613 if ( m_cursorMode == WXGRID_CURSOR_RESIZE_ROW )
4614 {
4615 DoEndDragResizeRow();
4616
4617 // Note: we are ending the event *after* doing
4618 // default processing in this case
4619 //
4620 SendEvent( wxEVT_GRID_ROW_SIZE, m_dragRowOrCol, -1, event );
4621 }
4622
4623 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_rowLabelWin);
4624 m_dragLastPos = -1;
4625 }
4626
4627
4628 // ------------ Right button down
4629 //
4630 else if ( event.RightDown() )
4631 {
4632 row = YToRow(y);
4633 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK, row, -1, event ) )
4634 {
4635 // no default action at the moment
4636 }
4637 }
4638
4639
4640 // ------------ Right double click
4641 //
4642 else if ( event.RightDClick() )
4643 {
4644 row = YToRow(y);
4645 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK, row, -1, event ) )
4646 {
4647 // no default action at the moment
4648 }
4649 }
4650
4651
4652 // ------------ No buttons down and mouse moving
4653 //
4654 else if ( event.Moving() )
4655 {
4656 m_dragRowOrCol = YToEdgeOfRow( y );
4657 if ( m_dragRowOrCol >= 0 )
4658 {
4659 if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
4660 {
4661 // don't capture the mouse yet
4662 if ( CanDragRowSize() )
4663 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW, m_rowLabelWin, FALSE);
4664 }
4665 }
4666 else if ( m_cursorMode != WXGRID_CURSOR_SELECT_CELL )
4667 {
4668 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_rowLabelWin, FALSE);
4669 }
4670 }
4671 }
4672
4673
4674 void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent& event )
4675 {
4676 int x, y, col;
4677 wxPoint pos( event.GetPosition() );
4678 CalcUnscrolledPosition( pos.x, pos.y, &x, &y );
4679
4680 if ( event.Dragging() )
4681 {
4682 if (!m_isDragging)
4683 {
4684 m_isDragging = TRUE;
4685 m_colLabelWin->CaptureMouse();
4686 }
4687
4688 if ( event.LeftIsDown() )
4689 {
4690 switch( m_cursorMode )
4691 {
4692 case WXGRID_CURSOR_RESIZE_COL:
4693 {
4694 int cw, ch, dummy, top;
4695 m_gridWin->GetClientSize( &cw, &ch );
4696 CalcUnscrolledPosition( 0, 0, &dummy, &top );
4697
4698 wxClientDC dc( m_gridWin );
4699 PrepareDC( dc );
4700
4701 x = wxMax( x, GetColLeft(m_dragRowOrCol) +
4702 GetColMinimalWidth(m_dragRowOrCol));
4703 dc.SetLogicalFunction(wxINVERT);
4704 if ( m_dragLastPos >= 0 )
4705 {
4706 dc.DrawLine( m_dragLastPos, top, m_dragLastPos, top+ch );
4707 }
4708 dc.DrawLine( x, top, x, top+ch );
4709 m_dragLastPos = x;
4710 }
4711 break;
4712
4713 case WXGRID_CURSOR_SELECT_COL:
4714 if ( (col = XToCol( x )) >= 0 )
4715 {
4716 if ( m_selection )
4717 {
4718 m_selection->SelectCol( col,
4719 event.ControlDown(),
4720 event.ShiftDown(),
4721 event.AltDown(),
4722 event.MetaDown() );
4723 }
4724 }
4725
4726 // default label to suppress warnings about "enumeration value
4727 // 'xxx' not handled in switch
4728 default:
4729 break;
4730 }
4731 }
4732 return;
4733 }
4734
4735 if ( m_isDragging && (event.Entering() || event.Leaving()) )
4736 return;
4737
4738 if (m_isDragging)
4739 {
4740 if (m_colLabelWin->HasCapture()) m_colLabelWin->ReleaseMouse();
4741 m_isDragging = FALSE;
4742 }
4743
4744 // ------------ Entering or leaving the window
4745 //
4746 if ( event.Entering() || event.Leaving() )
4747 {
4748 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_colLabelWin);
4749 }
4750
4751
4752 // ------------ Left button pressed
4753 //
4754 else if ( event.LeftDown() )
4755 {
4756 // don't send a label click event for a hit on the
4757 // edge of the col label - this is probably the user
4758 // wanting to resize the col
4759 //
4760 if ( XToEdgeOfCol(x) < 0 )
4761 {
4762 col = XToCol(x);
4763 if ( col >= 0 &&
4764 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK, -1, col, event ) )
4765 {
4766 if ( !event.ShiftDown() && !event.ControlDown() )
4767 ClearSelection();
4768 if ( m_selection )
4769 {
4770 if ( event.ShiftDown() )
4771 {
4772 m_selection->SelectBlock( 0,
4773 m_currentCellCoords.GetCol(),
4774 GetNumberRows() - 1, col,
4775 event.ControlDown(),
4776 event.ShiftDown(),
4777 event.AltDown(),
4778 event.MetaDown() );
4779 }
4780 else
4781 {
4782 m_selection->SelectCol( col,
4783 event.ControlDown(),
4784 event.ShiftDown(),
4785 event.AltDown(),
4786 event.MetaDown() );
4787 }
4788 }
4789
4790 ChangeCursorMode(WXGRID_CURSOR_SELECT_COL, m_colLabelWin);
4791 }
4792 }
4793 else
4794 {
4795 // starting to drag-resize a col
4796 //
4797 if ( CanDragColSize() )
4798 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL, m_colLabelWin);
4799 }
4800 }
4801
4802
4803 // ------------ Left double click
4804 //
4805 if ( event.LeftDClick() )
4806 {
4807 if ( XToEdgeOfCol(x) < 0 )
4808 {
4809 col = XToCol(x);
4810 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK, -1, col, event );
4811 }
4812 }
4813
4814
4815 // ------------ Left button released
4816 //
4817 else if ( event.LeftUp() )
4818 {
4819 if ( m_cursorMode == WXGRID_CURSOR_RESIZE_COL )
4820 {
4821 DoEndDragResizeCol();
4822
4823 // Note: we are ending the event *after* doing
4824 // default processing in this case
4825 //
4826 SendEvent( wxEVT_GRID_COL_SIZE, -1, m_dragRowOrCol, event );
4827 }
4828
4829 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_colLabelWin);
4830 m_dragLastPos = -1;
4831 }
4832
4833
4834 // ------------ Right button down
4835 //
4836 else if ( event.RightDown() )
4837 {
4838 col = XToCol(x);
4839 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK, -1, col, event ) )
4840 {
4841 // no default action at the moment
4842 }
4843 }
4844
4845
4846 // ------------ Right double click
4847 //
4848 else if ( event.RightDClick() )
4849 {
4850 col = XToCol(x);
4851 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK, -1, col, event ) )
4852 {
4853 // no default action at the moment
4854 }
4855 }
4856
4857
4858 // ------------ No buttons down and mouse moving
4859 //
4860 else if ( event.Moving() )
4861 {
4862 m_dragRowOrCol = XToEdgeOfCol( x );
4863 if ( m_dragRowOrCol >= 0 )
4864 {
4865 if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
4866 {
4867 // don't capture the cursor yet
4868 if ( CanDragColSize() )
4869 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL, m_colLabelWin, FALSE);
4870 }
4871 }
4872 else if ( m_cursorMode != WXGRID_CURSOR_SELECT_CELL )
4873 {
4874 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_colLabelWin, FALSE);
4875 }
4876 }
4877 }
4878
4879
4880 void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent& event )
4881 {
4882 if ( event.LeftDown() )
4883 {
4884 // indicate corner label by having both row and
4885 // col args == -1
4886 //
4887 if ( !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK, -1, -1, event ) )
4888 {
4889 SelectAll();
4890 }
4891 }
4892
4893 else if ( event.LeftDClick() )
4894 {
4895 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK, -1, -1, event );
4896 }
4897
4898 else if ( event.RightDown() )
4899 {
4900 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK, -1, -1, event ) )
4901 {
4902 // no default action at the moment
4903 }
4904 }
4905
4906 else if ( event.RightDClick() )
4907 {
4908 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK, -1, -1, event ) )
4909 {
4910 // no default action at the moment
4911 }
4912 }
4913 }
4914
4915 void wxGrid::ChangeCursorMode(CursorMode mode,
4916 wxWindow *win,
4917 bool captureMouse)
4918 {
4919 #ifdef __WXDEBUG__
4920 static const wxChar *cursorModes[] =
4921 {
4922 _T("SELECT_CELL"),
4923 _T("RESIZE_ROW"),
4924 _T("RESIZE_COL"),
4925 _T("SELECT_ROW"),
4926 _T("SELECT_COL")
4927 };
4928
4929 wxLogTrace(_T("grid"),
4930 _T("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
4931 win == m_colLabelWin ? _T("colLabelWin")
4932 : win ? _T("rowLabelWin")
4933 : _T("gridWin"),
4934 cursorModes[m_cursorMode], cursorModes[mode]);
4935 #endif // __WXDEBUG__
4936
4937 if ( mode == m_cursorMode &&
4938 win == m_winCapture &&
4939 captureMouse == (m_winCapture != NULL))
4940 return;
4941
4942 if ( !win )
4943 {
4944 // by default use the grid itself
4945 win = m_gridWin;
4946 }
4947
4948 if ( m_winCapture )
4949 {
4950 if (m_winCapture->HasCapture()) m_winCapture->ReleaseMouse();
4951 m_winCapture = (wxWindow *)NULL;
4952 }
4953
4954 m_cursorMode = mode;
4955
4956 switch ( m_cursorMode )
4957 {
4958 case WXGRID_CURSOR_RESIZE_ROW:
4959 win->SetCursor( m_rowResizeCursor );
4960 break;
4961
4962 case WXGRID_CURSOR_RESIZE_COL:
4963 win->SetCursor( m_colResizeCursor );
4964 break;
4965
4966 default:
4967 win->SetCursor( *wxSTANDARD_CURSOR );
4968 }
4969
4970 // we need to capture mouse when resizing
4971 bool resize = m_cursorMode == WXGRID_CURSOR_RESIZE_ROW ||
4972 m_cursorMode == WXGRID_CURSOR_RESIZE_COL;
4973
4974 if ( captureMouse && resize )
4975 {
4976 win->CaptureMouse();
4977 m_winCapture = win;
4978 }
4979 }
4980
4981 void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent& event )
4982 {
4983 int x, y;
4984 wxPoint pos( event.GetPosition() );
4985 CalcUnscrolledPosition( pos.x, pos.y, &x, &y );
4986
4987 wxGridCellCoords coords;
4988 XYToCell( x, y, coords );
4989
4990 int cell_rows, cell_cols;
4991 GetCellSize( coords.GetRow(), coords.GetCol(), &cell_rows, &cell_cols );
4992 if ((cell_rows < 0) || (cell_cols < 0))
4993 {
4994 coords.SetRow(coords.GetRow() + cell_rows);
4995 coords.SetCol(coords.GetCol() + cell_cols);
4996 }
4997
4998 if ( event.Dragging() )
4999 {
5000 //wxLogDebug("pos(%d, %d) coords(%d, %d)", pos.x, pos.y, coords.GetRow(), coords.GetCol());
5001
5002 // Don't start doing anything until the mouse has been drug at
5003 // least 3 pixels in any direction...
5004 if (! m_isDragging)
5005 {
5006 if (m_startDragPos == wxDefaultPosition)
5007 {
5008 m_startDragPos = pos;
5009 return;
5010 }
5011 if (abs(m_startDragPos.x - pos.x) < 4 && abs(m_startDragPos.y - pos.y) < 4)
5012 return;
5013 }
5014
5015 m_isDragging = TRUE;
5016 if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
5017 {
5018 // Hide the edit control, so it
5019 // won't interfer with drag-shrinking.
5020 if ( IsCellEditControlShown() )
5021 {
5022 HideCellEditControl();
5023 SaveEditControlValue();
5024 }
5025
5026 // Have we captured the mouse yet?
5027 if (! m_winCapture)
5028 {
5029 m_winCapture = m_gridWin;
5030 m_winCapture->CaptureMouse();
5031 }
5032
5033 if ( coords != wxGridNoCellCoords )
5034 {
5035 if ( event.ControlDown() )
5036 {
5037 if ( m_selectingKeyboard == wxGridNoCellCoords)
5038 m_selectingKeyboard = coords;
5039 HighlightBlock ( m_selectingKeyboard, coords );
5040 }
5041 else
5042 {
5043 if ( !IsSelection() )
5044 {
5045 HighlightBlock( coords, coords );
5046 }
5047 else
5048 {
5049 HighlightBlock( m_currentCellCoords, coords );
5050 }
5051 }
5052
5053 if (! IsVisible(coords))
5054 {
5055 MakeCellVisible(coords);
5056 // TODO: need to introduce a delay or something here. The
5057 // scrolling is way to fast, at least on MSW - also on GTK.
5058 }
5059 }
5060 }
5061 else if ( m_cursorMode == WXGRID_CURSOR_RESIZE_ROW )
5062 {
5063 int cw, ch, left, dummy;
5064 m_gridWin->GetClientSize( &cw, &ch );
5065 CalcUnscrolledPosition( 0, 0, &left, &dummy );
5066
5067 wxClientDC dc( m_gridWin );
5068 PrepareDC( dc );
5069 y = wxMax( y, GetRowTop(m_dragRowOrCol) +
5070 GetRowMinimalHeight(m_dragRowOrCol) );
5071 dc.SetLogicalFunction(wxINVERT);
5072 if ( m_dragLastPos >= 0 )
5073 {
5074 dc.DrawLine( left, m_dragLastPos, left+cw, m_dragLastPos );
5075 }
5076 dc.DrawLine( left, y, left+cw, y );
5077 m_dragLastPos = y;
5078 }
5079 else if ( m_cursorMode == WXGRID_CURSOR_RESIZE_COL )
5080 {
5081 int cw, ch, dummy, top;
5082 m_gridWin->GetClientSize( &cw, &ch );
5083 CalcUnscrolledPosition( 0, 0, &dummy, &top );
5084
5085 wxClientDC dc( m_gridWin );
5086 PrepareDC( dc );
5087 x = wxMax( x, GetColLeft(m_dragRowOrCol) +
5088 GetColMinimalWidth(m_dragRowOrCol) );
5089 dc.SetLogicalFunction(wxINVERT);
5090 if ( m_dragLastPos >= 0 )
5091 {
5092 dc.DrawLine( m_dragLastPos, top, m_dragLastPos, top+ch );
5093 }
5094 dc.DrawLine( x, top, x, top+ch );
5095 m_dragLastPos = x;
5096 }
5097
5098 return;
5099 }
5100
5101 m_isDragging = FALSE;
5102 m_startDragPos = wxDefaultPosition;
5103
5104 // VZ: if we do this, the mode is reset to WXGRID_CURSOR_SELECT_CELL
5105 // immediately after it becomes WXGRID_CURSOR_RESIZE_ROW/COL under
5106 // wxGTK
5107 #if 0
5108 if ( event.Entering() || event.Leaving() )
5109 {
5110 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL);
5111 m_gridWin->SetCursor( *wxSTANDARD_CURSOR );
5112 }
5113 else
5114 #endif // 0
5115
5116 // ------------ Left button pressed
5117 //
5118 if ( event.LeftDown() && coords != wxGridNoCellCoords )
5119 {
5120 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_CLICK,
5121 coords.GetRow(),
5122 coords.GetCol(),
5123 event ) )
5124 {
5125 if ( !event.ControlDown() )
5126 ClearSelection();
5127 if ( event.ShiftDown() )
5128 {
5129 if ( m_selection )
5130 {
5131 m_selection->SelectBlock( m_currentCellCoords.GetRow(),
5132 m_currentCellCoords.GetCol(),
5133 coords.GetRow(),
5134 coords.GetCol(),
5135 event.ControlDown(),
5136 event.ShiftDown(),
5137 event.AltDown(),
5138 event.MetaDown() );
5139 }
5140 }
5141 else if ( XToEdgeOfCol(x) < 0 &&
5142 YToEdgeOfRow(y) < 0 )
5143 {
5144 DisableCellEditControl();
5145 MakeCellVisible( coords );
5146
5147 // if this is the second click on this cell then start
5148 // the edit control
5149 if ( m_waitForSlowClick &&
5150 (coords == m_currentCellCoords) &&
5151 CanEnableCellControl())
5152 {
5153 EnableCellEditControl();
5154
5155 wxGridCellAttr* attr = GetCellAttr(m_currentCellCoords);
5156 wxGridCellEditor *editor = attr->GetEditor(this,
5157 coords.GetRow(),
5158 coords.GetCol());
5159 editor->StartingClick();
5160 editor->DecRef();
5161 attr->DecRef();
5162
5163 m_waitForSlowClick = FALSE;
5164 }
5165 else
5166 {
5167 if ( event.ControlDown() )
5168 {
5169 if ( m_selection )
5170 {
5171 m_selection->ToggleCellSelection( coords.GetRow(),
5172 coords.GetCol(),
5173 event.ControlDown(),
5174 event.ShiftDown(),
5175 event.AltDown(),
5176 event.MetaDown() );
5177 }
5178 m_selectingTopLeft = wxGridNoCellCoords;
5179 m_selectingBottomRight = wxGridNoCellCoords;
5180 m_selectingKeyboard = coords;
5181 }
5182 else
5183 {
5184 SetCurrentCell( coords );
5185 if ( m_selection )
5186 {
5187 if ( m_selection->GetSelectionMode() !=
5188 wxGrid::wxGridSelectCells )
5189 {
5190 HighlightBlock( coords, coords );
5191 }
5192 }
5193 }
5194 m_waitForSlowClick = TRUE;
5195 }
5196 }
5197 }
5198 }
5199
5200
5201 // ------------ Left double click
5202 //
5203 else if ( event.LeftDClick() && coords != wxGridNoCellCoords )
5204 {
5205 DisableCellEditControl();
5206
5207 if ( XToEdgeOfCol(x) < 0 && YToEdgeOfRow(y) < 0 )
5208 {
5209 SendEvent( wxEVT_GRID_CELL_LEFT_DCLICK,
5210 coords.GetRow(),
5211 coords.GetCol(),
5212 event );
5213 }
5214 }
5215
5216
5217 // ------------ Left button released
5218 //
5219 else if ( event.LeftUp() )
5220 {
5221 if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
5222 {
5223 if ( m_selectingTopLeft != wxGridNoCellCoords &&
5224 m_selectingBottomRight != wxGridNoCellCoords )
5225 {
5226 if (m_winCapture)
5227 {
5228 if (m_winCapture->HasCapture()) m_winCapture->ReleaseMouse();
5229 m_winCapture = NULL;
5230 }
5231
5232 if ( m_selection )
5233 {
5234 m_selection->SelectBlock( m_selectingTopLeft.GetRow(),
5235 m_selectingTopLeft.GetCol(),
5236 m_selectingBottomRight.GetRow(),
5237 m_selectingBottomRight.GetCol(),
5238 event.ControlDown(),
5239 event.ShiftDown(),
5240 event.AltDown(),
5241 event.MetaDown() );
5242 }
5243
5244 m_selectingTopLeft = wxGridNoCellCoords;
5245 m_selectingBottomRight = wxGridNoCellCoords;
5246 }
5247
5248 // Show the edit control, if it has been hidden for
5249 // drag-shrinking.
5250 ShowCellEditControl();
5251 }
5252 else if ( m_cursorMode == WXGRID_CURSOR_RESIZE_ROW )
5253 {
5254 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL);
5255 DoEndDragResizeRow();
5256
5257 // Note: we are ending the event *after* doing
5258 // default processing in this case
5259 //
5260 SendEvent( wxEVT_GRID_ROW_SIZE, m_dragRowOrCol, -1, event );
5261 }
5262 else if ( m_cursorMode == WXGRID_CURSOR_RESIZE_COL )
5263 {
5264 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL);
5265 DoEndDragResizeCol();
5266
5267 // Note: we are ending the event *after* doing
5268 // default processing in this case
5269 //
5270 SendEvent( wxEVT_GRID_COL_SIZE, -1, m_dragRowOrCol, event );
5271 }
5272
5273 m_dragLastPos = -1;
5274 }
5275
5276
5277 // ------------ Right button down
5278 //
5279 else if ( event.RightDown() && coords != wxGridNoCellCoords )
5280 {
5281 DisableCellEditControl();
5282 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_CLICK,
5283 coords.GetRow(),
5284 coords.GetCol(),
5285 event ) )
5286 {
5287 // no default action at the moment
5288 }
5289 }
5290
5291
5292 // ------------ Right double click
5293 //
5294 else if ( event.RightDClick() && coords != wxGridNoCellCoords )
5295 {
5296 DisableCellEditControl();
5297 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_DCLICK,
5298 coords.GetRow(),
5299 coords.GetCol(),
5300 event ) )
5301 {
5302 // no default action at the moment
5303 }
5304 }
5305
5306 // ------------ Moving and no button action
5307 //
5308 else if ( event.Moving() && !event.IsButton() )
5309 {
5310 int dragRow = YToEdgeOfRow( y );
5311 int dragCol = XToEdgeOfCol( x );
5312
5313 // Dragging on the corner of a cell to resize in both
5314 // directions is not implemented yet...
5315 //
5316 if ( dragRow >= 0 && dragCol >= 0 )
5317 {
5318 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL);
5319 return;
5320 }
5321
5322 if ( dragRow >= 0 )
5323 {
5324 m_dragRowOrCol = dragRow;
5325
5326 if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
5327 {
5328 if ( CanDragRowSize() && CanDragGridSize() )
5329 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW);
5330 }
5331
5332 if ( dragCol >= 0 )
5333 {
5334 m_dragRowOrCol = dragCol;
5335 }
5336
5337 return;
5338 }
5339
5340 if ( dragCol >= 0 )
5341 {
5342 m_dragRowOrCol = dragCol;
5343
5344 if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
5345 {
5346 if ( CanDragColSize() && CanDragGridSize() )
5347 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL);
5348 }
5349
5350 return;
5351 }
5352
5353 // Neither on a row or col edge
5354 //
5355 if ( m_cursorMode != WXGRID_CURSOR_SELECT_CELL )
5356 {
5357 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL);
5358 }
5359 }
5360 }
5361
5362
5363 void wxGrid::DoEndDragResizeRow()
5364 {
5365 if ( m_dragLastPos >= 0 )
5366 {
5367 // erase the last line and resize the row
5368 //
5369 int cw, ch, left, dummy;
5370 m_gridWin->GetClientSize( &cw, &ch );
5371 CalcUnscrolledPosition( 0, 0, &left, &dummy );
5372
5373 wxClientDC dc( m_gridWin );
5374 PrepareDC( dc );
5375 dc.SetLogicalFunction( wxINVERT );
5376 dc.DrawLine( left, m_dragLastPos, left+cw, m_dragLastPos );
5377 HideCellEditControl();
5378 SaveEditControlValue();
5379
5380 int rowTop = GetRowTop(m_dragRowOrCol);
5381 SetRowSize( m_dragRowOrCol,
5382 wxMax( m_dragLastPos - rowTop, WXGRID_MIN_ROW_HEIGHT ) );
5383
5384 if ( !GetBatchCount() )
5385 {
5386 // Only needed to get the correct rect.y:
5387 wxRect rect ( CellToRect( m_dragRowOrCol, 0 ) );
5388 rect.x = 0;
5389 CalcScrolledPosition(0, rect.y, &dummy, &rect.y);
5390 rect.width = m_rowLabelWidth;
5391 rect.height = ch - rect.y;
5392 m_rowLabelWin->Refresh( TRUE, &rect );
5393 rect.width = cw;
5394 // if there is a multicell block, paint all of it
5395 if (m_table)
5396 {
5397 int i, cell_rows, cell_cols, subtract_rows = 0;
5398 int leftCol = XToCol(left);
5399 int rightCol = XToCol(left+cw);
5400 if (leftCol >= 0)
5401 {
5402 if (rightCol < 0) rightCol = m_numCols;
5403 for (i=leftCol; i<rightCol; i++)
5404 {
5405 GetCellSize(m_dragRowOrCol, i, &cell_rows, &cell_cols);
5406 if (cell_rows < subtract_rows)
5407 subtract_rows = cell_rows;
5408 }
5409 rect.y = GetRowTop(m_dragRowOrCol + subtract_rows);
5410 CalcScrolledPosition(0, rect.y, &dummy, &rect.y);
5411 rect.height = ch - rect.y;
5412 }
5413 }
5414 m_gridWin->Refresh( FALSE, &rect );
5415 }
5416
5417 ShowCellEditControl();
5418 }
5419 }
5420
5421
5422 void wxGrid::DoEndDragResizeCol()
5423 {
5424 if ( m_dragLastPos >= 0 )
5425 {
5426 // erase the last line and resize the col
5427 //
5428 int cw, ch, dummy, top;
5429 m_gridWin->GetClientSize( &cw, &ch );
5430 CalcUnscrolledPosition( 0, 0, &dummy, &top );
5431
5432 wxClientDC dc( m_gridWin );
5433 PrepareDC( dc );
5434 dc.SetLogicalFunction( wxINVERT );
5435 dc.DrawLine( m_dragLastPos, top, m_dragLastPos, top+ch );
5436 HideCellEditControl();
5437 SaveEditControlValue();
5438
5439 int colLeft = GetColLeft(m_dragRowOrCol);
5440 SetColSize( m_dragRowOrCol,
5441 wxMax( m_dragLastPos - colLeft,
5442 GetColMinimalWidth(m_dragRowOrCol) ) );
5443
5444 if ( !GetBatchCount() )
5445 {
5446 // Only needed to get the correct rect.x:
5447 wxRect rect ( CellToRect( 0, m_dragRowOrCol ) );
5448 rect.y = 0;
5449 CalcScrolledPosition(rect.x, 0, &rect.x, &dummy);
5450 rect.width = cw - rect.x;
5451 rect.height = m_colLabelHeight;
5452 m_colLabelWin->Refresh( TRUE, &rect );
5453 rect.height = ch;
5454 // if there is a multicell block, paint all of it
5455 if (m_table)
5456 {
5457 int i, cell_rows, cell_cols, subtract_cols = 0;
5458 int topRow = YToRow(top);
5459 int bottomRow = YToRow(top+cw);
5460 if (topRow >= 0)
5461 {
5462 if (bottomRow < 0) bottomRow = m_numRows;
5463 for (i=topRow; i<bottomRow; i++)
5464 {
5465 GetCellSize(i, m_dragRowOrCol, &cell_rows, &cell_cols);
5466 if (cell_cols < subtract_cols)
5467 subtract_cols = cell_cols;
5468 }
5469 rect.x = GetColLeft(m_dragRowOrCol + subtract_cols);
5470 CalcScrolledPosition(rect.x, 0, &rect.x, &dummy);
5471 rect.width = cw - rect.x;
5472 }
5473 }
5474 m_gridWin->Refresh( FALSE, &rect );
5475 }
5476
5477 ShowCellEditControl();
5478 }
5479 }
5480
5481
5482
5483 //
5484 // ------ interaction with data model
5485 //
5486 bool wxGrid::ProcessTableMessage( wxGridTableMessage& msg )
5487 {
5488 switch ( msg.GetId() )
5489 {
5490 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES:
5491 return GetModelValues();
5492
5493 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES:
5494 return SetModelValues();
5495
5496 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED:
5497 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED:
5498 case wxGRIDTABLE_NOTIFY_ROWS_DELETED:
5499 case wxGRIDTABLE_NOTIFY_COLS_INSERTED:
5500 case wxGRIDTABLE_NOTIFY_COLS_APPENDED:
5501 case wxGRIDTABLE_NOTIFY_COLS_DELETED:
5502 return Redimension( msg );
5503
5504 default:
5505 return FALSE;
5506 }
5507 }
5508
5509
5510
5511 // The behaviour of this function depends on the grid table class
5512 // Clear() function. For the default wxGridStringTable class the
5513 // behavious is to replace all cell contents with wxEmptyString but
5514 // not to change the number of rows or cols.
5515 //
5516 void wxGrid::ClearGrid()
5517 {
5518 if ( m_table )
5519 {
5520 if (IsCellEditControlEnabled())
5521 DisableCellEditControl();
5522
5523 m_table->Clear();
5524 if ( !GetBatchCount() ) m_gridWin->Refresh();
5525 }
5526 }
5527
5528
5529 bool wxGrid::InsertRows( int pos, int numRows, bool WXUNUSED(updateLabels) )
5530 {
5531 // TODO: something with updateLabels flag
5532
5533 if ( !m_created )
5534 {
5535 wxFAIL_MSG( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") );
5536 return FALSE;
5537 }
5538
5539 if ( m_table )
5540 {
5541 if (IsCellEditControlEnabled())
5542 DisableCellEditControl();
5543
5544 return m_table->InsertRows( pos, numRows );
5545
5546 // the table will have sent the results of the insert row
5547 // operation to this view object as a grid table message
5548 }
5549 return FALSE;
5550 }
5551
5552
5553 bool wxGrid::AppendRows( int numRows, bool WXUNUSED(updateLabels) )
5554 {
5555 // TODO: something with updateLabels flag
5556
5557 if ( !m_created )
5558 {
5559 wxFAIL_MSG( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") );
5560 return FALSE;
5561 }
5562
5563 return ( m_table && m_table->AppendRows( numRows ) );
5564 // the table will have sent the results of the append row
5565 // operation to this view object as a grid table message
5566 }
5567
5568
5569 bool wxGrid::DeleteRows( int pos, int numRows, bool WXUNUSED(updateLabels) )
5570 {
5571 // TODO: something with updateLabels flag
5572
5573 if ( !m_created )
5574 {
5575 wxFAIL_MSG( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") );
5576 return FALSE;
5577 }
5578
5579 if ( m_table )
5580 {
5581 if (IsCellEditControlEnabled())
5582 DisableCellEditControl();
5583
5584 return (m_table->DeleteRows( pos, numRows ));
5585 // the table will have sent the results of the delete row
5586 // operation to this view object as a grid table message
5587 }
5588 return FALSE;
5589 }
5590
5591
5592 bool wxGrid::InsertCols( int pos, int numCols, bool WXUNUSED(updateLabels) )
5593 {
5594 // TODO: something with updateLabels flag
5595
5596 if ( !m_created )
5597 {
5598 wxFAIL_MSG( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") );
5599 return FALSE;
5600 }
5601
5602 if ( m_table )
5603 {
5604 if (IsCellEditControlEnabled())
5605 DisableCellEditControl();
5606
5607 return m_table->InsertCols( pos, numCols );
5608 // the table will have sent the results of the insert col
5609 // operation to this view object as a grid table message
5610 }
5611 return FALSE;
5612 }
5613
5614
5615 bool wxGrid::AppendCols( int numCols, bool WXUNUSED(updateLabels) )
5616 {
5617 // TODO: something with updateLabels flag
5618
5619 if ( !m_created )
5620 {
5621 wxFAIL_MSG( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") );
5622 return FALSE;
5623 }
5624
5625 return ( m_table && m_table->AppendCols( numCols ) );
5626 // the table will have sent the results of the append col
5627 // operation to this view object as a grid table message
5628 }
5629
5630
5631 bool wxGrid::DeleteCols( int pos, int numCols, bool WXUNUSED(updateLabels) )
5632 {
5633 // TODO: something with updateLabels flag
5634
5635 if ( !m_created )
5636 {
5637 wxFAIL_MSG( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") );
5638 return FALSE;
5639 }
5640
5641 if ( m_table )
5642 {
5643 if (IsCellEditControlEnabled())
5644 DisableCellEditControl();
5645
5646 return ( m_table->DeleteCols( pos, numCols ) );
5647 // the table will have sent the results of the delete col
5648 // operation to this view object as a grid table message
5649 }
5650 return FALSE;
5651 }
5652
5653
5654
5655 //
5656 // ----- event handlers
5657 //
5658
5659 // Generate a grid event based on a mouse event and
5660 // return the result of ProcessEvent()
5661 //
5662 int wxGrid::SendEvent( const wxEventType type,
5663 int row, int col,
5664 wxMouseEvent& mouseEv )
5665 {
5666 bool claimed;
5667 bool vetoed= FALSE;
5668
5669 if ( type == wxEVT_GRID_ROW_SIZE || type == wxEVT_GRID_COL_SIZE )
5670 {
5671 int rowOrCol = (row == -1 ? col : row);
5672
5673 wxGridSizeEvent gridEvt( GetId(),
5674 type,
5675 this,
5676 rowOrCol,
5677 mouseEv.GetX() + GetRowLabelSize(),
5678 mouseEv.GetY() + GetColLabelSize(),
5679 mouseEv.ControlDown(),
5680 mouseEv.ShiftDown(),
5681 mouseEv.AltDown(),
5682 mouseEv.MetaDown() );
5683
5684 claimed = GetEventHandler()->ProcessEvent(gridEvt);
5685 vetoed = !gridEvt.IsAllowed();
5686 }
5687 else if ( type == wxEVT_GRID_RANGE_SELECT )
5688 {
5689 // Right now, it should _never_ end up here!
5690 wxGridRangeSelectEvent gridEvt( GetId(),
5691 type,
5692 this,
5693 m_selectingTopLeft,
5694 m_selectingBottomRight,
5695 TRUE,
5696 mouseEv.ControlDown(),
5697 mouseEv.ShiftDown(),
5698 mouseEv.AltDown(),
5699 mouseEv.MetaDown() );
5700
5701 claimed = GetEventHandler()->ProcessEvent(gridEvt);
5702 vetoed = !gridEvt.IsAllowed();
5703 }
5704 else
5705 {
5706 wxGridEvent gridEvt( GetId(),
5707 type,
5708 this,
5709 row, col,
5710 mouseEv.GetX() + GetRowLabelSize(),
5711 mouseEv.GetY() + GetColLabelSize(),
5712 FALSE,
5713 mouseEv.ControlDown(),
5714 mouseEv.ShiftDown(),
5715 mouseEv.AltDown(),
5716 mouseEv.MetaDown() );
5717 claimed = GetEventHandler()->ProcessEvent(gridEvt);
5718 vetoed = !gridEvt.IsAllowed();
5719 }
5720
5721 // A Veto'd event may not be `claimed' so test this first
5722 if (vetoed) return -1;
5723 return claimed ? 1 : 0;
5724 }
5725
5726
5727 // Generate a grid event of specified type and return the result
5728 // of ProcessEvent().
5729 //
5730 int wxGrid::SendEvent( const wxEventType type,
5731 int row, int col )
5732 {
5733 bool claimed;
5734 bool vetoed= FALSE;
5735
5736 if ( type == wxEVT_GRID_ROW_SIZE || type == wxEVT_GRID_COL_SIZE )
5737 {
5738 int rowOrCol = (row == -1 ? col : row);
5739
5740 wxGridSizeEvent gridEvt( GetId(),
5741 type,
5742 this,
5743 rowOrCol );
5744
5745 claimed = GetEventHandler()->ProcessEvent(gridEvt);
5746 vetoed = !gridEvt.IsAllowed();
5747 }
5748 else
5749 {
5750 wxGridEvent gridEvt( GetId(),
5751 type,
5752 this,
5753 row, col );
5754
5755 claimed = GetEventHandler()->ProcessEvent(gridEvt);
5756 vetoed = !gridEvt.IsAllowed();
5757 }
5758
5759 // A Veto'd event may not be `claimed' so test this first
5760 if (vetoed) return -1;
5761 return claimed ? 1 : 0;
5762 }
5763
5764
5765 void wxGrid::OnPaint( wxPaintEvent& WXUNUSED(event) )
5766 {
5767 wxPaintDC dc(this); // needed to prevent zillions of paint events on MSW
5768 }
5769
5770 void wxGrid::Refresh(bool eraseb, const wxRect* rect)
5771 {
5772 // Don't do anything if between Begin/EndBatch...
5773 // EndBatch() will do all this on the last nested one anyway.
5774 if (! GetBatchCount())
5775 {
5776 // Refresh to get correct scrolled position:
5777 wxScrolledWindow::Refresh(eraseb,rect);
5778
5779 if (rect)
5780 {
5781 int rect_x, rect_y, rectWidth, rectHeight;
5782 int width_label, width_cell, height_label, height_cell;
5783 int x, y;
5784
5785 //Copy rectangle can get scroll offsets..
5786 rect_x = rect->GetX();
5787 rect_y = rect->GetY();
5788 rectWidth = rect->GetWidth();
5789 rectHeight = rect->GetHeight();
5790
5791 width_label = m_rowLabelWidth - rect_x;
5792 if (width_label > rectWidth) width_label = rectWidth;
5793
5794 height_label = m_colLabelHeight - rect_y;
5795 if (height_label > rectHeight) height_label = rectHeight;
5796
5797 if (rect_x > m_rowLabelWidth)
5798 {
5799 x = rect_x - m_rowLabelWidth;
5800 width_cell = rectWidth;
5801 }
5802 else
5803 {
5804 x = 0;
5805 width_cell = rectWidth - (m_rowLabelWidth - rect_x);
5806 }
5807
5808 if (rect_y > m_colLabelHeight)
5809 {
5810 y = rect_y - m_colLabelHeight;
5811 height_cell = rectHeight;
5812 }
5813 else
5814 {
5815 y = 0;
5816 height_cell = rectHeight - (m_colLabelHeight - rect_y);
5817 }
5818
5819 // Paint corner label part intersecting rect.
5820 if ( width_label > 0 && height_label > 0 )
5821 {
5822 wxRect anotherrect(rect_x, rect_y, width_label, height_label);
5823 m_cornerLabelWin->Refresh(eraseb, &anotherrect);
5824 }
5825
5826 // Paint col labels part intersecting rect.
5827 if ( width_cell > 0 && height_label > 0 )
5828 {
5829 wxRect anotherrect(x, rect_y, width_cell, height_label);
5830 m_colLabelWin->Refresh(eraseb, &anotherrect);
5831 }
5832
5833 // Paint row labels part intersecting rect.
5834 if ( width_label > 0 && height_cell > 0 )
5835 {
5836 wxRect anotherrect(rect_x, y, width_label, height_cell);
5837 m_rowLabelWin->Refresh(eraseb, &anotherrect);
5838 }
5839
5840 // Paint cell area part intersecting rect.
5841 if ( width_cell > 0 && height_cell > 0 )
5842 {
5843 wxRect anotherrect(x, y, width_cell, height_cell);
5844 m_gridWin->Refresh(eraseb, &anotherrect);
5845 }
5846 }
5847 else
5848 {
5849 m_cornerLabelWin->Refresh(eraseb, NULL);
5850 m_colLabelWin->Refresh(eraseb, NULL);
5851 m_rowLabelWin->Refresh(eraseb, NULL);
5852 m_gridWin->Refresh(eraseb, NULL);
5853 }
5854 }
5855 }
5856
5857 void wxGrid::OnSize( wxSizeEvent& event )
5858 {
5859 // position the child windows
5860 CalcWindowSizes();
5861
5862 // don't call CalcDimensions() from here, the base class handles the size
5863 // changes itself
5864 event.Skip();
5865 }
5866
5867
5868 void wxGrid::OnKeyDown( wxKeyEvent& event )
5869 {
5870 if ( m_inOnKeyDown )
5871 {
5872 // shouldn't be here - we are going round in circles...
5873 //
5874 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") );
5875 }
5876
5877 m_inOnKeyDown = TRUE;
5878
5879 // propagate the event up and see if it gets processed
5880 //
5881 wxWindow *parent = GetParent();
5882 wxKeyEvent keyEvt( event );
5883 keyEvt.SetEventObject( parent );
5884
5885 if ( !parent->GetEventHandler()->ProcessEvent( keyEvt ) )
5886 {
5887
5888 // try local handlers
5889 //
5890 switch ( event.KeyCode() )
5891 {
5892 case WXK_UP:
5893 if ( event.ControlDown() )
5894 {
5895 MoveCursorUpBlock( event.ShiftDown() );
5896 }
5897 else
5898 {
5899 MoveCursorUp( event.ShiftDown() );
5900 }
5901 break;
5902
5903 case WXK_DOWN:
5904 if ( event.ControlDown() )
5905 {
5906 MoveCursorDownBlock( event.ShiftDown() );
5907 }
5908 else
5909 {
5910 MoveCursorDown( event.ShiftDown() );
5911 }
5912 break;
5913
5914 case WXK_LEFT:
5915 if ( event.ControlDown() )
5916 {
5917 MoveCursorLeftBlock( event.ShiftDown() );
5918 }
5919 else
5920 {
5921 MoveCursorLeft( event.ShiftDown() );
5922 }
5923 break;
5924
5925 case WXK_RIGHT:
5926 if ( event.ControlDown() )
5927 {
5928 MoveCursorRightBlock( event.ShiftDown() );
5929 }
5930 else
5931 {
5932 MoveCursorRight( event.ShiftDown() );
5933 }
5934 break;
5935
5936 case WXK_RETURN:
5937 case WXK_NUMPAD_ENTER:
5938 if ( event.ControlDown() )
5939 {
5940 event.Skip(); // to let the edit control have the return
5941 }
5942 else
5943 {
5944 if ( GetGridCursorRow() < GetNumberRows()-1 )
5945 {
5946 MoveCursorDown( event.ShiftDown() );
5947 }
5948 else
5949 {
5950 // at the bottom of a column
5951 HideCellEditControl();
5952 SaveEditControlValue();
5953 }
5954 }
5955 break;
5956
5957 case WXK_ESCAPE:
5958 ClearSelection();
5959 break;
5960
5961 case WXK_TAB:
5962 if (event.ShiftDown())
5963 {
5964 if ( GetGridCursorCol() > 0 )
5965 {
5966 MoveCursorLeft( FALSE );
5967 }
5968 else
5969 {
5970 // at left of grid
5971 HideCellEditControl();
5972 SaveEditControlValue();
5973 }
5974 }
5975 else
5976 {
5977 if ( GetGridCursorCol() < GetNumberCols()-1 )
5978 {
5979 MoveCursorRight( FALSE );
5980 }
5981 else
5982 {
5983 // at right of grid
5984 HideCellEditControl();
5985 SaveEditControlValue();
5986 }
5987 }
5988 break;
5989
5990 case WXK_HOME:
5991 if ( event.ControlDown() )
5992 {
5993 MakeCellVisible( 0, 0 );
5994 SetCurrentCell( 0, 0 );
5995 }
5996 else
5997 {
5998 event.Skip();
5999 }
6000 break;
6001
6002 case WXK_END:
6003 if ( event.ControlDown() )
6004 {
6005 MakeCellVisible( m_numRows-1, m_numCols-1 );
6006 SetCurrentCell( m_numRows-1, m_numCols-1 );
6007 }
6008 else
6009 {
6010 event.Skip();
6011 }
6012 break;
6013
6014 case WXK_PRIOR:
6015 MovePageUp();
6016 break;
6017
6018 case WXK_NEXT:
6019 MovePageDown();
6020 break;
6021
6022 case WXK_SPACE:
6023 if ( event.ControlDown() )
6024 {
6025 if ( m_selection )
6026 {
6027 m_selection->ToggleCellSelection( m_currentCellCoords.GetRow(),
6028 m_currentCellCoords.GetCol(),
6029 event.ControlDown(),
6030 event.ShiftDown(),
6031 event.AltDown(),
6032 event.MetaDown() );
6033 }
6034 break;
6035 }
6036 if ( !IsEditable() )
6037 {
6038 MoveCursorRight( FALSE );
6039 break;
6040 }
6041 // Otherwise fall through to default
6042
6043 default:
6044 // is it possible to edit the current cell at all?
6045 if ( !IsCellEditControlEnabled() && CanEnableCellControl() )
6046 {
6047 // yes, now check whether the cells editor accepts the key
6048 int row = m_currentCellCoords.GetRow();
6049 int col = m_currentCellCoords.GetCol();
6050 wxGridCellAttr* attr = GetCellAttr(row, col);
6051 wxGridCellEditor *editor = attr->GetEditor(this, row, col);
6052
6053 // <F2> is special and will always start editing, for
6054 // other keys - ask the editor itself
6055 if ( (event.KeyCode() == WXK_F2 && !event.HasModifiers())
6056 || editor->IsAcceptedKey(event) )
6057 {
6058 EnableCellEditControl();
6059
6060 // the editor could be not shown for a variety of
6061 // reasons (i.e. blocked by the app or whatever), so
6062 // check if it really was created
6063 if ( m_cellEditCtrlEnabled )
6064 {
6065 editor->StartingKey(event);
6066 }
6067 }
6068 else
6069 {
6070 event.Skip();
6071 }
6072
6073 editor->DecRef();
6074 attr->DecRef();
6075 }
6076 else
6077 {
6078 // let others process char events with modifiers or all
6079 // char events for readonly cells
6080 event.Skip();
6081 }
6082 break;
6083 }
6084 }
6085
6086 m_inOnKeyDown = FALSE;
6087 }
6088
6089 void wxGrid::OnKeyUp( wxKeyEvent& event )
6090 {
6091 // try local handlers
6092 //
6093 if ( event.KeyCode() == WXK_SHIFT )
6094 {
6095 if ( m_selectingTopLeft != wxGridNoCellCoords &&
6096 m_selectingBottomRight != wxGridNoCellCoords )
6097 {
6098 if ( m_selection )
6099 {
6100 m_selection->SelectBlock( m_selectingTopLeft.GetRow(),
6101 m_selectingTopLeft.GetCol(),
6102 m_selectingBottomRight.GetRow(),
6103 m_selectingBottomRight.GetCol(),
6104 event.ControlDown(),
6105 TRUE,
6106 event.AltDown(),
6107 event.MetaDown() );
6108 }
6109 }
6110
6111 m_selectingTopLeft = wxGridNoCellCoords;
6112 m_selectingBottomRight = wxGridNoCellCoords;
6113 m_selectingKeyboard = wxGridNoCellCoords;
6114 }
6115 }
6116
6117 void wxGrid::OnEraseBackground(wxEraseEvent&)
6118 {
6119 }
6120
6121 void wxGrid::SetCurrentCell( const wxGridCellCoords& coords )
6122 {
6123 if ( SendEvent( wxEVT_GRID_SELECT_CELL, coords.GetRow(), coords.GetCol() ) )
6124 {
6125 // the event has been intercepted - do nothing
6126 return;
6127 }
6128
6129 wxClientDC dc(m_gridWin);
6130 PrepareDC(dc);
6131
6132 if ( m_currentCellCoords != wxGridNoCellCoords )
6133 {
6134 HideCellEditControl();
6135 DisableCellEditControl();
6136
6137 if ( IsVisible( m_currentCellCoords, FALSE ) )
6138 {
6139 wxRect r;
6140 r = BlockToDeviceRect(m_currentCellCoords, m_currentCellCoords);
6141 if ( !m_gridLinesEnabled )
6142 {
6143 r.x--;
6144 r.y--;
6145 r.width++;
6146 r.height++;
6147 }
6148
6149 wxGridCellCoordsArray cells = CalcCellsExposed( r );
6150
6151 // Otherwise refresh redraws the highlight!
6152 m_currentCellCoords = coords;
6153
6154 DrawGridCellArea(dc,cells);
6155 DrawAllGridLines( dc, r );
6156 }
6157 }
6158
6159 m_currentCellCoords = coords;
6160
6161 wxGridCellAttr* attr = GetCellAttr(coords);
6162 DrawCellHighlight(dc, attr);
6163 attr->DecRef();
6164 }
6165
6166
6167 void wxGrid::HighlightBlock( int topRow, int leftCol, int bottomRow, int rightCol )
6168 {
6169 int temp;
6170 wxGridCellCoords updateTopLeft, updateBottomRight;
6171
6172 if ( m_selection )
6173 {
6174 if ( m_selection->GetSelectionMode() == wxGrid::wxGridSelectRows )
6175 {
6176 leftCol = 0;
6177 rightCol = GetNumberCols() - 1;
6178 }
6179 else if ( m_selection->GetSelectionMode() == wxGrid::wxGridSelectColumns )
6180 {
6181 topRow = 0;
6182 bottomRow = GetNumberRows() - 1;
6183 }
6184 }
6185
6186 if ( topRow > bottomRow )
6187 {
6188 temp = topRow;
6189 topRow = bottomRow;
6190 bottomRow = temp;
6191 }
6192
6193 if ( leftCol > rightCol )
6194 {
6195 temp = leftCol;
6196 leftCol = rightCol;
6197 rightCol = temp;
6198 }
6199
6200 updateTopLeft = wxGridCellCoords( topRow, leftCol );
6201 updateBottomRight = wxGridCellCoords( bottomRow, rightCol );
6202
6203 // First the case that we selected a completely new area
6204 if ( m_selectingTopLeft == wxGridNoCellCoords ||
6205 m_selectingBottomRight == wxGridNoCellCoords )
6206 {
6207 wxRect rect;
6208 rect = BlockToDeviceRect( wxGridCellCoords ( topRow, leftCol ),
6209 wxGridCellCoords ( bottomRow, rightCol ) );
6210 m_gridWin->Refresh( FALSE, &rect );
6211 }
6212 // Now handle changing an existing selection area.
6213 else if ( m_selectingTopLeft != updateTopLeft ||
6214 m_selectingBottomRight != updateBottomRight )
6215 {
6216 // Compute two optimal update rectangles:
6217 // Either one rectangle is a real subset of the
6218 // other, or they are (almost) disjoint!
6219 wxRect rect[4];
6220 bool need_refresh[4];
6221 need_refresh[0] =
6222 need_refresh[1] =
6223 need_refresh[2] =
6224 need_refresh[3] = FALSE;
6225 int i;
6226
6227 // Store intermediate values
6228 wxCoord oldLeft = m_selectingTopLeft.GetCol();
6229 wxCoord oldTop = m_selectingTopLeft.GetRow();
6230 wxCoord oldRight = m_selectingBottomRight.GetCol();
6231 wxCoord oldBottom = m_selectingBottomRight.GetRow();
6232
6233 // Determine the outer/inner coordinates.
6234 if (oldLeft > leftCol)
6235 {
6236 temp = oldLeft;
6237 oldLeft = leftCol;
6238 leftCol = temp;
6239 }
6240 if (oldTop > topRow )
6241 {
6242 temp = oldTop;
6243 oldTop = topRow;
6244 topRow = temp;
6245 }
6246 if (oldRight < rightCol )
6247 {
6248 temp = oldRight;
6249 oldRight = rightCol;
6250 rightCol = temp;
6251 }
6252 if (oldBottom < bottomRow)
6253 {
6254 temp = oldBottom;
6255 oldBottom = bottomRow;
6256 bottomRow = temp;
6257 }
6258
6259 // Now, either the stuff marked old is the outer
6260 // rectangle or we don't have a situation where one
6261 // is contained in the other.
6262
6263 if ( oldLeft < leftCol )
6264 {
6265 // Refresh the newly selected or deselected
6266 // area to the left of the old or new selection.
6267 need_refresh[0] = TRUE;
6268 rect[0] = BlockToDeviceRect( wxGridCellCoords ( oldTop,
6269 oldLeft ),
6270 wxGridCellCoords ( oldBottom,
6271 leftCol - 1 ) );
6272 }
6273
6274 if ( oldTop < topRow )
6275 {
6276 // Refresh the newly selected or deselected
6277 // area above the old or new selection.
6278 need_refresh[1] = TRUE;
6279 rect[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop,
6280 leftCol ),
6281 wxGridCellCoords ( topRow - 1,
6282 rightCol ) );
6283 }
6284
6285 if ( oldRight > rightCol )
6286 {
6287 // Refresh the newly selected or deselected
6288 // area to the right of the old or new selection.
6289 need_refresh[2] = TRUE;
6290 rect[2] = BlockToDeviceRect( wxGridCellCoords ( oldTop,
6291 rightCol + 1 ),
6292 wxGridCellCoords ( oldBottom,
6293 oldRight ) );
6294 }
6295
6296 if ( oldBottom > bottomRow )
6297 {
6298 // Refresh the newly selected or deselected
6299 // area below the old or new selection.
6300 need_refresh[3] = TRUE;
6301 rect[3] = BlockToDeviceRect( wxGridCellCoords ( bottomRow + 1,
6302 leftCol ),
6303 wxGridCellCoords ( oldBottom,
6304 rightCol ) );
6305 }
6306
6307 // various Refresh() calls
6308 for (i = 0; i < 4; i++ )
6309 if ( need_refresh[i] && rect[i] != wxGridNoCellRect )
6310 m_gridWin->Refresh( FALSE, &(rect[i]) );
6311 }
6312 // Change Selection
6313 m_selectingTopLeft = updateTopLeft;
6314 m_selectingBottomRight = updateBottomRight;
6315 }
6316
6317 //
6318 // ------ functions to get/send data (see also public functions)
6319 //
6320
6321 bool wxGrid::GetModelValues()
6322 {
6323 // Hide the editor, so it won't hide a changed value.
6324 HideCellEditControl();
6325
6326 if ( m_table )
6327 {
6328 // all we need to do is repaint the grid
6329 //
6330 m_gridWin->Refresh();
6331 return TRUE;
6332 }
6333
6334 return FALSE;
6335 }
6336
6337
6338 bool wxGrid::SetModelValues()
6339 {
6340 int row, col;
6341
6342 // Disable the editor, so it won't hide a changed value.
6343 // Do we also want to save the current value of the editor first?
6344 // I think so ...
6345 DisableCellEditControl();
6346
6347 if ( m_table )
6348 {
6349 for ( row = 0; row < m_numRows; row++ )
6350 {
6351 for ( col = 0; col < m_numCols; col++ )
6352 {
6353 m_table->SetValue( row, col, GetCellValue(row, col) );
6354 }
6355 }
6356
6357 return TRUE;
6358 }
6359
6360 return FALSE;
6361 }
6362
6363
6364
6365 // Note - this function only draws cells that are in the list of
6366 // exposed cells (usually set from the update region by
6367 // CalcExposedCells)
6368 //
6369 void wxGrid::DrawGridCellArea( wxDC& dc, const wxGridCellCoordsArray& cells )
6370 {
6371 if ( !m_numRows || !m_numCols ) return;
6372
6373 int i, j, k, l, numCells = cells.GetCount();
6374 int row, col, cell_rows, cell_cols;
6375 wxGridCellCoordsArray redrawCells;
6376
6377 for ( i = numCells-1; i >= 0; i-- )
6378 {
6379 row = cells[i].GetRow();
6380 col = cells[i].GetCol();
6381 GetCellSize( row, col, &cell_rows, &cell_cols );
6382
6383 // If this cell is part of a multicell block, find owner for repaint
6384 if ( cell_rows <= 0 || cell_cols <= 0 )
6385 {
6386 wxGridCellCoords cell(row+cell_rows, col+cell_cols);
6387 bool marked = FALSE;
6388 for ( j = 0; j < numCells; j++ )
6389 {
6390 if ( cell == cells[j] )
6391 {
6392 marked = TRUE;
6393 break;
6394 }
6395 }
6396 if (!marked)
6397 {
6398 int count = redrawCells.GetCount();
6399 for (j = 0; j < count; j++)
6400 {
6401 if ( cell == redrawCells[j] )
6402 {
6403 marked = TRUE;
6404 break;
6405 }
6406 }
6407 if (!marked) redrawCells.Add( cell );
6408 }
6409 continue; // don't bother drawing this cell
6410 }
6411
6412 // If this cell is empty, find cell to left that might want to overflow
6413 if (m_table && m_table->IsEmptyCell(row, col))
6414 {
6415 for ( l = 0; l < cell_rows; l++ )
6416 {
6417 for (j = col-1; j >= 0; j--)
6418 {
6419 if (!m_table->IsEmptyCell(row+l, j))
6420 {
6421 if (GetCellOverflow(row+l, j))
6422 {
6423 wxGridCellCoords cell(row+l, j);
6424 bool marked = FALSE;
6425
6426 for (k = 0; k < numCells; k++)
6427 {
6428 if ( cell == cells[k] )
6429 {
6430 marked = TRUE;
6431 break;
6432 }
6433 }
6434 if (!marked)
6435 {
6436 int count = redrawCells.GetCount();
6437 for (k = 0; k < count; k++)
6438 {
6439 if ( cell == redrawCells[k] )
6440 {
6441 marked = TRUE;
6442 break;
6443 }
6444 }
6445 if (!marked) redrawCells.Add( cell );
6446 }
6447 }
6448 break;
6449 }
6450 }
6451 }
6452 }
6453 DrawCell( dc, cells[i] );
6454 }
6455
6456 numCells = redrawCells.GetCount();
6457
6458 for ( i = numCells - 1; i >= 0; i-- )
6459 {
6460 DrawCell( dc, redrawCells[i] );
6461 }
6462 }
6463
6464
6465 void wxGrid::DrawGridSpace( wxDC& dc )
6466 {
6467 int cw, ch;
6468 m_gridWin->GetClientSize( &cw, &ch );
6469
6470 int right, bottom;
6471 CalcUnscrolledPosition( cw, ch, &right, &bottom );
6472
6473 int rightCol = m_numCols > 0 ? GetColRight(m_numCols - 1) : 0;
6474 int bottomRow = m_numRows > 0 ? GetRowBottom(m_numRows - 1) : 0 ;
6475
6476 if ( right > rightCol || bottom > bottomRow )
6477 {
6478 int left, top;
6479 CalcUnscrolledPosition( 0, 0, &left, &top );
6480
6481 dc.SetBrush( wxBrush(GetDefaultCellBackgroundColour(), wxSOLID) );
6482 dc.SetPen( *wxTRANSPARENT_PEN );
6483
6484 if ( right > rightCol )
6485 {
6486 dc.DrawRectangle( rightCol, top, right - rightCol, ch);
6487 }
6488
6489 if ( bottom > bottomRow )
6490 {
6491 dc.DrawRectangle( left, bottomRow, cw, bottom - bottomRow);
6492 }
6493 }
6494 }
6495
6496
6497 void wxGrid::DrawCell( wxDC& dc, const wxGridCellCoords& coords )
6498 {
6499 int row = coords.GetRow();
6500 int col = coords.GetCol();
6501
6502 if ( GetColWidth(col) <= 0 || GetRowHeight(row) <= 0 )
6503 return;
6504
6505 // we draw the cell border ourselves
6506 #if !WXGRID_DRAW_LINES
6507 if ( m_gridLinesEnabled )
6508 DrawCellBorder( dc, coords );
6509 #endif
6510
6511 wxGridCellAttr* attr = GetCellAttr(row, col);
6512
6513 bool isCurrent = coords == m_currentCellCoords;
6514
6515 wxRect rect = CellToRect( row, col );
6516
6517 // if the editor is shown, we should use it and not the renderer
6518 // Note: However, only if it is really _shown_, i.e. not hidden!
6519 if ( isCurrent && IsCellEditControlShown() )
6520 {
6521 wxGridCellEditor *editor = attr->GetEditor(this, row, col);
6522 editor->PaintBackground(rect, attr);
6523 editor->DecRef();
6524 }
6525 else
6526 {
6527 // but all the rest is drawn by the cell renderer and hence may be
6528 // customized
6529 wxGridCellRenderer *renderer = attr->GetRenderer(this, row, col);
6530 renderer->Draw(*this, *attr, dc, rect, row, col, IsInSelection(coords));
6531 renderer->DecRef();
6532 }
6533
6534 attr->DecRef();
6535 }
6536
6537 void wxGrid::DrawCellHighlight( wxDC& dc, const wxGridCellAttr *attr )
6538 {
6539 int row = m_currentCellCoords.GetRow();
6540 int col = m_currentCellCoords.GetCol();
6541
6542 if ( GetColWidth(col) <= 0 || GetRowHeight(row) <= 0 )
6543 return;
6544
6545 wxRect rect = CellToRect(row, col);
6546
6547 // hmmm... what could we do here to show that the cell is disabled?
6548 // for now, I just draw a thinner border than for the other ones, but
6549 // it doesn't look really good
6550
6551 int penWidth = attr->IsReadOnly() ? m_cellHighlightROPenWidth : m_cellHighlightPenWidth;
6552
6553 if (penWidth > 0)
6554 {
6555 // The center of th drawn line is where the position/width/height of
6556 // the rectangle is actually at, (on wxMSW atr least,) so we will
6557 // reduce the size of the rectangle to compensate for the thickness of
6558 // the line. If this is too strange on non wxMSW platforms then
6559 // please #ifdef this appropriately.
6560 rect.x += penWidth/2;
6561 rect.y += penWidth/2;
6562 rect.width -= penWidth-1;
6563 rect.height -= penWidth-1;
6564
6565
6566 // Now draw the rectangle
6567 dc.SetPen(wxPen(m_cellHighlightColour, penWidth, wxSOLID));
6568 dc.SetBrush(*wxTRANSPARENT_BRUSH);
6569 dc.DrawRectangle(rect);
6570 }
6571
6572 #if 0
6573 // VZ: my experiments with 3d borders...
6574
6575 // how to properly set colours for arbitrary bg?
6576 wxCoord x1 = rect.x,
6577 y1 = rect.y,
6578 x2 = rect.x + rect.width -1,
6579 y2 = rect.y + rect.height -1;
6580
6581 dc.SetPen(*wxWHITE_PEN);
6582 dc.DrawLine(x1, y1, x2, y1);
6583 dc.DrawLine(x1, y1, x1, y2);
6584
6585 dc.DrawLine(x1 + 1, y2 - 1, x2 - 1, y2 - 1);
6586 dc.DrawLine(x2 - 1, y1 + 1, x2 - 1, y2 );
6587
6588 dc.SetPen(*wxBLACK_PEN);
6589 dc.DrawLine(x1, y2, x2, y2);
6590 dc.DrawLine(x2, y1, x2, y2+1);
6591 #endif // 0
6592 }
6593
6594
6595 void wxGrid::DrawCellBorder( wxDC& dc, const wxGridCellCoords& coords )
6596 {
6597 int row = coords.GetRow();
6598 int col = coords.GetCol();
6599 if ( GetColWidth(col) <= 0 || GetRowHeight(row) <= 0 )
6600 return;
6601
6602 dc.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID) );
6603
6604 wxRect rect = CellToRect( row, col );
6605
6606 // right hand border
6607 //
6608 dc.DrawLine( rect.x + rect.width, rect.y,
6609 rect.x + rect.width, rect.y + rect.height + 1 );
6610
6611 // bottom border
6612 //
6613 dc.DrawLine( rect.x, rect.y + rect.height,
6614 rect.x + rect.width, rect.y + rect.height);
6615 }
6616
6617 void wxGrid::DrawHighlight(wxDC& dc,const wxGridCellCoordsArray& cells)
6618 {
6619 // This if block was previously in wxGrid::OnPaint but that doesn't
6620 // seem to get called under wxGTK - MB
6621 //
6622 if ( m_currentCellCoords == wxGridNoCellCoords &&
6623 m_numRows && m_numCols )
6624 {
6625 m_currentCellCoords.Set(0, 0);
6626 }
6627
6628 if ( IsCellEditControlShown() )
6629 {
6630 // don't show highlight when the edit control is shown
6631 return;
6632 }
6633
6634 // if the active cell was repainted, repaint its highlight too because it
6635 // might have been damaged by the grid lines
6636 size_t count = cells.GetCount();
6637 for ( size_t n = 0; n < count; n++ )
6638 {
6639 if ( cells[n] == m_currentCellCoords )
6640 {
6641 wxGridCellAttr* attr = GetCellAttr(m_currentCellCoords);
6642 DrawCellHighlight(dc, attr);
6643 attr->DecRef();
6644
6645 break;
6646 }
6647 }
6648 }
6649
6650 // TODO: remove this ???
6651 // This is used to redraw all grid lines e.g. when the grid line colour
6652 // has been changed
6653 //
6654 void wxGrid::DrawAllGridLines( wxDC& dc, const wxRegion & WXUNUSED(reg) )
6655 {
6656 #if !WXGRID_DRAW_LINES
6657 return;
6658 #endif
6659
6660 if ( !m_gridLinesEnabled ||
6661 !m_numRows ||
6662 !m_numCols ) return;
6663
6664 int top, bottom, left, right;
6665
6666 #if 0 //#ifndef __WXGTK__
6667 if (reg.IsEmpty())
6668 {
6669 int cw, ch;
6670 m_gridWin->GetClientSize(&cw, &ch);
6671
6672 // virtual coords of visible area
6673 //
6674 CalcUnscrolledPosition( 0, 0, &left, &top );
6675 CalcUnscrolledPosition( cw, ch, &right, &bottom );
6676 }
6677 else
6678 {
6679 wxCoord x, y, w, h;
6680 reg.GetBox(x, y, w, h);
6681 CalcUnscrolledPosition( x, y, &left, &top );
6682 CalcUnscrolledPosition( x + w, y + h, &right, &bottom );
6683 }
6684 #else
6685 int cw, ch;
6686 m_gridWin->GetClientSize(&cw, &ch);
6687 CalcUnscrolledPosition( 0, 0, &left, &top );
6688 CalcUnscrolledPosition( cw, ch, &right, &bottom );
6689 #endif
6690
6691 // avoid drawing grid lines past the last row and col
6692 //
6693 right = wxMin( right, GetColRight(m_numCols - 1) );
6694 bottom = wxMin( bottom, GetRowBottom(m_numRows - 1) );
6695
6696 // no gridlines inside multicells, clip them out
6697 int leftCol = XToCol(left);
6698 int topRow = YToRow(top);
6699 int rightCol = XToCol(right);
6700 int bottomRow = YToRow(bottom);
6701 wxRegion clippedcells(0, 0, cw, ch);
6702
6703 if ((leftCol >= 0) && (topRow >= 0))
6704 {
6705 if (rightCol < 0) rightCol = m_numCols;
6706 if (bottomRow < 0) bottomRow = m_numRows;
6707
6708 int i, j, cell_rows, cell_cols;
6709 wxRect rect;
6710
6711 for (j=topRow; j<bottomRow; j++)
6712 {
6713 for (i=leftCol; i<rightCol; i++)
6714 {
6715 GetCellSize( j, i, &cell_rows, &cell_cols );
6716 if ((cell_rows > 1) || (cell_cols > 1))
6717 {
6718 rect = CellToRect(j,i);
6719 CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
6720 clippedcells.Subtract(rect);
6721 }
6722 else if ((cell_rows < 0) || (cell_cols < 0))
6723 {
6724 rect = CellToRect(j+cell_rows, i+cell_cols);
6725 CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
6726 clippedcells.Subtract(rect);
6727 }
6728 }
6729 }
6730 }
6731 dc.SetClippingRegion( clippedcells );
6732
6733 dc.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID) );
6734
6735 // horizontal grid lines
6736 //
6737 int i;
6738 for ( i = internalYToRow(top); i < m_numRows; i++ )
6739 {
6740 int bot = GetRowBottom(i) - 1;
6741
6742 if ( bot > bottom )
6743 {
6744 break;
6745 }
6746
6747 if ( bot >= top )
6748 {
6749 dc.DrawLine( left, bot, right, bot );
6750 }
6751 }
6752
6753
6754 // vertical grid lines
6755 //
6756 for ( i = internalXToCol(left); i < m_numCols; i++ )
6757 {
6758 int colRight = GetColRight(i) - 1;
6759 if ( colRight > right )
6760 {
6761 break;
6762 }
6763
6764 if ( colRight >= left )
6765 {
6766 dc.DrawLine( colRight, top, colRight, bottom );
6767 }
6768 }
6769 dc.DestroyClippingRegion();
6770 }
6771
6772
6773 void wxGrid::DrawRowLabels( wxDC& dc ,const wxArrayInt& rows)
6774 {
6775 if ( !m_numRows ) return;
6776
6777 size_t i;
6778 size_t numLabels = rows.GetCount();
6779
6780 for ( i = 0; i < numLabels; i++ )
6781 {
6782 DrawRowLabel( dc, rows[i] );
6783 }
6784 }
6785
6786
6787 void wxGrid::DrawRowLabel( wxDC& dc, int row )
6788 {
6789 if ( GetRowHeight(row) <= 0 )
6790 return;
6791
6792 int rowTop = GetRowTop(row),
6793 rowBottom = GetRowBottom(row) - 1;
6794
6795 dc.SetPen( *wxBLACK_PEN );
6796 dc.DrawLine( m_rowLabelWidth-1, rowTop,
6797 m_rowLabelWidth-1, rowBottom );
6798
6799 dc.DrawLine( 0, rowBottom, m_rowLabelWidth-1, rowBottom );
6800
6801 dc.SetPen( *wxWHITE_PEN );
6802 dc.DrawLine( 0, rowTop, 0, rowBottom );
6803 dc.DrawLine( 0, rowTop, m_rowLabelWidth-1, rowTop );
6804
6805 dc.SetBackgroundMode( wxTRANSPARENT );
6806 dc.SetTextForeground( GetLabelTextColour() );
6807 dc.SetFont( GetLabelFont() );
6808
6809 int hAlign, vAlign;
6810 GetRowLabelAlignment( &hAlign, &vAlign );
6811
6812 wxRect rect;
6813 rect.SetX( 2 );
6814 rect.SetY( GetRowTop(row) + 2 );
6815 rect.SetWidth( m_rowLabelWidth - 4 );
6816 rect.SetHeight( GetRowHeight(row) - 4 );
6817 DrawTextRectangle( dc, GetRowLabelValue( row ), rect, hAlign, vAlign );
6818 }
6819
6820
6821 void wxGrid::DrawColLabels( wxDC& dc,const wxArrayInt& cols )
6822 {
6823 if ( !m_numCols ) return;
6824
6825 size_t i;
6826 size_t numLabels = cols.GetCount();
6827
6828 for ( i = 0; i < numLabels; i++ )
6829 {
6830 DrawColLabel( dc, cols[i] );
6831 }
6832 }
6833
6834
6835 void wxGrid::DrawColLabel( wxDC& dc, int col )
6836 {
6837 if ( GetColWidth(col) <= 0 )
6838 return;
6839
6840 int colLeft = GetColLeft(col),
6841 colRight = GetColRight(col) - 1;
6842
6843 dc.SetPen( *wxBLACK_PEN );
6844 dc.DrawLine( colRight, 0,
6845 colRight, m_colLabelHeight-1 );
6846
6847 dc.DrawLine( colLeft, m_colLabelHeight-1,
6848 colRight, m_colLabelHeight-1 );
6849
6850 dc.SetPen( *wxWHITE_PEN );
6851 dc.DrawLine( colLeft, 0, colLeft, m_colLabelHeight-1 );
6852 dc.DrawLine( colLeft, 0, colRight, 0 );
6853
6854 dc.SetBackgroundMode( wxTRANSPARENT );
6855 dc.SetTextForeground( GetLabelTextColour() );
6856 dc.SetFont( GetLabelFont() );
6857
6858 dc.SetBackgroundMode( wxTRANSPARENT );
6859 dc.SetTextForeground( GetLabelTextColour() );
6860 dc.SetFont( GetLabelFont() );
6861
6862 int hAlign, vAlign;
6863 GetColLabelAlignment( &hAlign, &vAlign );
6864
6865 wxRect rect;
6866 rect.SetX( colLeft + 2 );
6867 rect.SetY( 2 );
6868 rect.SetWidth( GetColWidth(col) - 4 );
6869 rect.SetHeight( m_colLabelHeight - 4 );
6870 DrawTextRectangle( dc, GetColLabelValue( col ), rect, hAlign, vAlign );
6871 }
6872
6873 void wxGrid::DrawTextRectangle( wxDC& dc,
6874 const wxString& value,
6875 const wxRect& rect,
6876 int horizAlign,
6877 int vertAlign )
6878 {
6879 wxArrayString lines;
6880
6881 StringToLines( value, lines );
6882
6883
6884 //Forward to new API.
6885 DrawTextRectangle( dc,
6886 lines,
6887 rect,
6888 horizAlign,
6889 vertAlign );
6890
6891 }
6892
6893 void wxGrid::DrawTextRectangle( wxDC& dc,
6894 const wxArrayString& lines,
6895 const wxRect& rect,
6896 int horizAlign,
6897 int vertAlign )
6898 {
6899 long textWidth, textHeight;
6900 long lineWidth, lineHeight;
6901
6902 dc.SetClippingRegion( rect );
6903 if ( lines.GetCount() )
6904 {
6905 GetTextBoxSize( dc, lines, &textWidth, &textHeight );
6906 dc.GetTextExtent( lines[0], &lineWidth, &lineHeight );
6907
6908 float x, y;
6909 switch ( horizAlign )
6910 {
6911 case wxALIGN_RIGHT:
6912 x = rect.x + (rect.width - textWidth - 1);
6913 break;
6914
6915 case wxALIGN_CENTRE:
6916 x = rect.x + ((rect.width - textWidth)/2);
6917 break;
6918
6919 case wxALIGN_LEFT:
6920 default:
6921 x = rect.x + 1;
6922 break;
6923 }
6924
6925 switch ( vertAlign )
6926 {
6927 case wxALIGN_BOTTOM:
6928 y = rect.y + (rect.height - textHeight - 1);
6929 break;
6930
6931 case wxALIGN_CENTRE:
6932 y = rect.y + ((rect.height - textHeight)/2);
6933 break;
6934
6935 case wxALIGN_TOP:
6936 default:
6937 y = rect.y + 1;
6938 break;
6939 }
6940
6941 for ( size_t i = 0; i < lines.GetCount(); i++ )
6942 {
6943 dc.DrawText( lines[i], (int)x, (int)y );
6944 y += lineHeight;
6945 }
6946 }
6947
6948 dc.DestroyClippingRegion();
6949 }
6950
6951
6952 // Split multi line text up into an array of strings. Any existing
6953 // contents of the string array are preserved.
6954 //
6955 void wxGrid::StringToLines( const wxString& value, wxArrayString& lines )
6956 {
6957 int startPos = 0;
6958 int pos;
6959 wxString eol = wxTextFile::GetEOL( wxTextFileType_Unix );
6960 wxString tVal = wxTextFile::Translate( value, wxTextFileType_Unix );
6961
6962 while ( startPos < (int)tVal.Length() )
6963 {
6964 pos = tVal.Mid(startPos).Find( eol );
6965 if ( pos < 0 )
6966 {
6967 break;
6968 }
6969 else if ( pos == 0 )
6970 {
6971 lines.Add( wxEmptyString );
6972 }
6973 else
6974 {
6975 lines.Add( value.Mid(startPos, pos) );
6976 }
6977 startPos += pos+1;
6978 }
6979 if ( startPos < (int)value.Length() )
6980 {
6981 lines.Add( value.Mid( startPos ) );
6982 }
6983 }
6984
6985
6986 void wxGrid::GetTextBoxSize( wxDC& dc,
6987 const wxArrayString& lines,
6988 long *width, long *height )
6989 {
6990 long w = 0;
6991 long h = 0;
6992 long lineW, lineH;
6993
6994 size_t i;
6995 for ( i = 0; i < lines.GetCount(); i++ )
6996 {
6997 dc.GetTextExtent( lines[i], &lineW, &lineH );
6998 w = wxMax( w, lineW );
6999 h += lineH;
7000 }
7001
7002 *width = w;
7003 *height = h;
7004 }
7005
7006 //
7007 // ------ Batch processing.
7008 //
7009 void wxGrid::EndBatch()
7010 {
7011 if ( m_batchCount > 0 )
7012 {
7013 m_batchCount--;
7014 if ( !m_batchCount )
7015 {
7016 CalcDimensions();
7017 m_rowLabelWin->Refresh();
7018 m_colLabelWin->Refresh();
7019 m_cornerLabelWin->Refresh();
7020 m_gridWin->Refresh();
7021 }
7022 }
7023 }
7024
7025 // Use this, rather than wxWindow::Refresh(), to force an immediate
7026 // repainting of the grid. Has no effect if you are already inside a
7027 // BeginBatch / EndBatch block.
7028 //
7029 void wxGrid::ForceRefresh()
7030 {
7031 BeginBatch();
7032 EndBatch();
7033 }
7034
7035
7036 //
7037 // ------ Edit control functions
7038 //
7039
7040
7041 void wxGrid::EnableEditing( bool edit )
7042 {
7043 // TODO: improve this ?
7044 //
7045 if ( edit != m_editable )
7046 {
7047 if(!edit) EnableCellEditControl(edit);
7048 m_editable = edit;
7049 }
7050 }
7051
7052
7053 void wxGrid::EnableCellEditControl( bool enable )
7054 {
7055 if (! m_editable)
7056 return;
7057
7058 if ( m_currentCellCoords == wxGridNoCellCoords )
7059 SetCurrentCell( 0, 0 );
7060
7061 if ( enable != m_cellEditCtrlEnabled )
7062 {
7063 if ( enable )
7064 {
7065 if (SendEvent( wxEVT_GRID_EDITOR_SHOWN) <0)
7066 return;
7067
7068 // this should be checked by the caller!
7069 wxASSERT_MSG( CanEnableCellControl(),
7070 _T("can't enable editing for this cell!") );
7071
7072 // do it before ShowCellEditControl()
7073 m_cellEditCtrlEnabled = enable;
7074
7075 ShowCellEditControl();
7076 }
7077 else
7078 {
7079 //FIXME:add veto support
7080 SendEvent( wxEVT_GRID_EDITOR_HIDDEN);
7081
7082 HideCellEditControl();
7083 SaveEditControlValue();
7084
7085 // do it after HideCellEditControl()
7086 m_cellEditCtrlEnabled = enable;
7087 }
7088 }
7089 }
7090
7091 bool wxGrid::IsCurrentCellReadOnly() const
7092 {
7093 // const_cast
7094 wxGridCellAttr* attr = ((wxGrid *)this)->GetCellAttr(m_currentCellCoords);
7095 bool readonly = attr->IsReadOnly();
7096 attr->DecRef();
7097
7098 return readonly;
7099 }
7100
7101 bool wxGrid::CanEnableCellControl() const
7102 {
7103 return m_editable && !IsCurrentCellReadOnly();
7104 }
7105
7106 bool wxGrid::IsCellEditControlEnabled() const
7107 {
7108 // the cell edit control might be disable for all cells or just for the
7109 // current one if it's read only
7110 return m_cellEditCtrlEnabled ? !IsCurrentCellReadOnly() : FALSE;
7111 }
7112
7113 bool wxGrid::IsCellEditControlShown() const
7114 {
7115 bool isShown = FALSE;
7116
7117 if ( m_cellEditCtrlEnabled )
7118 {
7119 int row = m_currentCellCoords.GetRow();
7120 int col = m_currentCellCoords.GetCol();
7121 wxGridCellAttr* attr = GetCellAttr(row, col);
7122 wxGridCellEditor* editor = attr->GetEditor((wxGrid*) this, row, col);
7123 attr->DecRef();
7124
7125 if ( editor )
7126 {
7127 if ( editor->IsCreated() )
7128 {
7129 isShown = editor->GetControl()->IsShown();
7130 }
7131
7132 editor->DecRef();
7133 }
7134 }
7135
7136 return isShown;
7137 }
7138
7139 void wxGrid::ShowCellEditControl()
7140 {
7141 if ( IsCellEditControlEnabled() )
7142 {
7143 if ( !IsVisible( m_currentCellCoords ) )
7144 {
7145 m_cellEditCtrlEnabled = FALSE;
7146 return;
7147 }
7148 else
7149 {
7150 wxRect rect = CellToRect( m_currentCellCoords );
7151 int row = m_currentCellCoords.GetRow();
7152 int col = m_currentCellCoords.GetCol();
7153
7154 // if this is part of a multicell, find owner (topleft)
7155 int cell_rows, cell_cols;
7156 GetCellSize( row, col, &cell_rows, &cell_cols );
7157 if ( cell_rows <= 0 || cell_cols <= 0 )
7158 {
7159 row += cell_rows;
7160 col += cell_cols;
7161 m_currentCellCoords.SetRow( row );
7162 m_currentCellCoords.SetCol( col );
7163 }
7164
7165 // convert to scrolled coords
7166 //
7167 CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
7168
7169 // done in PaintBackground()
7170 #if 0
7171 // erase the highlight and the cell contents because the editor
7172 // might not cover the entire cell
7173 wxClientDC dc( m_gridWin );
7174 PrepareDC( dc );
7175 dc.SetBrush(*wxLIGHT_GREY_BRUSH); //wxBrush(attr->GetBackgroundColour(), wxSOLID));
7176 dc.SetPen(*wxTRANSPARENT_PEN);
7177 dc.DrawRectangle(rect);
7178 #endif // 0
7179
7180 // cell is shifted by one pixel
7181 // However, don't allow x or y to become negative
7182 // since the SetSize() method interprets that as
7183 // "don't change."
7184 if (rect.x > 0)
7185 rect.x--;
7186 if (rect.y > 0)
7187 rect.y--;
7188
7189 wxGridCellAttr* attr = GetCellAttr(row, col);
7190 wxGridCellEditor* editor = attr->GetEditor(this, row, col);
7191 if ( !editor->IsCreated() )
7192 {
7193 editor->Create(m_gridWin, -1,
7194 new wxGridCellEditorEvtHandler(this, editor));
7195
7196 wxGridEditorCreatedEvent evt(GetId(),
7197 wxEVT_GRID_EDITOR_CREATED,
7198 this,
7199 row,
7200 col,
7201 editor->GetControl());
7202 GetEventHandler()->ProcessEvent(evt);
7203 }
7204
7205 editor->Show( TRUE, attr );
7206
7207 // resize editor to overflow into righthand cells if allowed
7208 wxString value = GetCellValue(row, col);
7209 if ( (value != wxEmptyString) && (attr->GetOverflow()) )
7210 {
7211 wxClientDC dc(m_gridWin);
7212 wxCoord y = 0, best_width = 0;
7213 dc.SetFont(attr->GetFont());
7214 dc.GetTextExtent(value, &best_width, &y);
7215
7216 int cell_rows, cell_cols;
7217 attr->GetSize( &cell_rows, &cell_cols );
7218
7219 if ((best_width > rect.width) && (col < m_numCols) && m_table)
7220 {
7221 int i;
7222 for (i = col+cell_cols; i < m_numCols; i++)
7223 {
7224 if (m_table->IsEmptyCell(row,i))
7225 {
7226 rect.width += GetColWidth(i);
7227 if (rect.width >= best_width) break;
7228 }
7229 else
7230 break;
7231 }
7232 }
7233 }
7234 editor->SetSize( rect );
7235
7236 editor->BeginEdit(row, col, this);
7237
7238 editor->DecRef();
7239 attr->DecRef();
7240 }
7241 }
7242 }
7243
7244
7245 void wxGrid::HideCellEditControl()
7246 {
7247 if ( IsCellEditControlEnabled() )
7248 {
7249 int row = m_currentCellCoords.GetRow();
7250 int col = m_currentCellCoords.GetCol();
7251
7252 wxGridCellAttr* attr = GetCellAttr(row, col);
7253 wxGridCellEditor *editor = attr->GetEditor(this, row, col);
7254 editor->Show( FALSE );
7255 editor->DecRef();
7256 attr->DecRef();
7257 m_gridWin->SetFocus();
7258 // refresh whole row to the right
7259 wxRect rect( CellToRect(row, col) );
7260 CalcScrolledPosition(rect.x, rect.y, &rect.x, &rect.y );
7261 rect.width = m_gridWin->GetClientSize().GetWidth() - rect.x;
7262 m_gridWin->Refresh( FALSE, &rect );
7263 }
7264 }
7265
7266
7267 void wxGrid::SaveEditControlValue()
7268 {
7269 if ( IsCellEditControlEnabled() )
7270 {
7271 int row = m_currentCellCoords.GetRow();
7272 int col = m_currentCellCoords.GetCol();
7273
7274 wxString oldval = GetCellValue(row,col);
7275
7276 wxGridCellAttr* attr = GetCellAttr(row, col);
7277 wxGridCellEditor* editor = attr->GetEditor(this, row, col);
7278 bool changed = editor->EndEdit(row, col, this);
7279
7280 editor->DecRef();
7281 attr->DecRef();
7282
7283 if (changed)
7284 {
7285 if ( SendEvent( wxEVT_GRID_CELL_CHANGE,
7286 m_currentCellCoords.GetRow(),
7287 m_currentCellCoords.GetCol() ) < 0 ) {
7288
7289 // Event has been vetoed, set the data back.
7290 SetCellValue(row,col,oldval);
7291 }
7292 }
7293 }
7294 }
7295
7296
7297 //
7298 // ------ Grid location functions
7299 // Note that all of these functions work with the logical coordinates of
7300 // grid cells and labels so you will need to convert from device
7301 // coordinates for mouse events etc.
7302 //
7303
7304 void wxGrid::XYToCell( int x, int y, wxGridCellCoords& coords )
7305 {
7306 int row = YToRow(y);
7307 int col = XToCol(x);
7308
7309 if ( row == -1 || col == -1 )
7310 {
7311 coords = wxGridNoCellCoords;
7312 }
7313 else
7314 {
7315 coords.Set( row, col );
7316 }
7317 }
7318
7319
7320 // Internal Helper function for computing row or column from some
7321 // (unscrolled) coordinate value, using either
7322 // m_defaultRowHeight/m_defaultColWidth or binary search on array
7323 // of m_rowBottoms/m_ColRights to speed up the search!
7324
7325 static int CoordToRowOrCol(int coord, int defaultDist, int minDist,
7326 const wxArrayInt& BorderArray, bool maxOnOverflow)
7327 {
7328 if (!defaultDist)
7329 defaultDist = 1;
7330 size_t i_max = coord / defaultDist,
7331 i_min = 0;
7332 if (BorderArray.IsEmpty())
7333 {
7334 return maxOnOverflow ? (int)i_max : -1;
7335 }
7336
7337 if ( i_max >= BorderArray.GetCount())
7338 i_max = BorderArray.GetCount() - 1;
7339 else
7340 {
7341 if ( coord >= BorderArray[i_max])
7342 {
7343 i_min = i_max;
7344 i_max = coord / minDist;
7345 }
7346 if ( i_max >= BorderArray.GetCount())
7347 i_max = BorderArray.GetCount() - 1;
7348 }
7349 if ( coord >= BorderArray[i_max])
7350 return maxOnOverflow ? (int)i_max : -1;
7351 if ( coord < BorderArray[0] )
7352 return 0;
7353
7354 while ( i_max - i_min > 0 )
7355 {
7356 wxCHECK_MSG(BorderArray[i_min] <= coord && coord < BorderArray[i_max],
7357 0, _T("wxGrid: internal error in CoordToRowOrCol"));
7358 if (coord >= BorderArray[ i_max - 1])
7359 return i_max;
7360 else
7361 i_max--;
7362 int median = i_min + (i_max - i_min + 1) / 2;
7363 if (coord < BorderArray[median])
7364 i_max = median;
7365 else
7366 i_min = median;
7367 }
7368 return i_max;
7369 }
7370
7371 int wxGrid::YToRow( int y )
7372 {
7373 return CoordToRowOrCol(y, m_defaultRowHeight,
7374 WXGRID_MIN_ROW_HEIGHT, m_rowBottoms, FALSE);
7375 }
7376
7377
7378 int wxGrid::XToCol( int x )
7379 {
7380 return CoordToRowOrCol(x, m_defaultColWidth,
7381 WXGRID_MIN_COL_WIDTH, m_colRights, FALSE);
7382 }
7383
7384
7385 // return the row number that that the y coord is near the edge of, or
7386 // -1 if not near an edge
7387 //
7388 int wxGrid::YToEdgeOfRow( int y )
7389 {
7390 int i;
7391 i = internalYToRow(y);
7392
7393 if ( GetRowHeight(i) > WXGRID_LABEL_EDGE_ZONE )
7394 {
7395 // We know that we are in row i, test whether we are
7396 // close enough to lower or upper border, respectively.
7397 if ( abs(GetRowBottom(i) - y) < WXGRID_LABEL_EDGE_ZONE )
7398 return i;
7399 else if( i > 0 && y - GetRowTop(i) < WXGRID_LABEL_EDGE_ZONE )
7400 return i - 1;
7401 }
7402
7403 return -1;
7404 }
7405
7406
7407 // return the col number that that the x coord is near the edge of, or
7408 // -1 if not near an edge
7409 //
7410 int wxGrid::XToEdgeOfCol( int x )
7411 {
7412 int i;
7413 i = internalXToCol(x);
7414
7415 if ( GetColWidth(i) > WXGRID_LABEL_EDGE_ZONE )
7416 {
7417 // We know that we are in column i, test whether we are
7418 // close enough to right or left border, respectively.
7419 if ( abs(GetColRight(i) - x) < WXGRID_LABEL_EDGE_ZONE )
7420 return i;
7421 else if( i > 0 && x - GetColLeft(i) < WXGRID_LABEL_EDGE_ZONE )
7422 return i - 1;
7423 }
7424
7425 return -1;
7426 }
7427
7428
7429 wxRect wxGrid::CellToRect( int row, int col )
7430 {
7431 wxRect rect( -1, -1, -1, -1 );
7432
7433 if ( row >= 0 && row < m_numRows &&
7434 col >= 0 && col < m_numCols )
7435 {
7436 int i, cell_rows, cell_cols;
7437 rect.width = rect.height = 0;
7438 GetCellSize( row, col, &cell_rows, &cell_cols );
7439 // if negative then find multicell owner
7440 if (cell_rows < 0) row += cell_rows;
7441 if (cell_cols < 0) col += cell_cols;
7442 GetCellSize( row, col, &cell_rows, &cell_cols );
7443
7444 rect.x = GetColLeft(col);
7445 rect.y = GetRowTop(row);
7446 for (i=col; i<col+cell_cols; i++)
7447 rect.width += GetColWidth(i);
7448 for (i=row; i<row+cell_rows; i++)
7449 rect.height += GetRowHeight(i);
7450 }
7451
7452 // if grid lines are enabled, then the area of the cell is a bit smaller
7453 if (m_gridLinesEnabled) {
7454 rect.width -= 1;
7455 rect.height -= 1;
7456 }
7457 return rect;
7458 }
7459
7460
7461 bool wxGrid::IsVisible( int row, int col, bool wholeCellVisible )
7462 {
7463 // get the cell rectangle in logical coords
7464 //
7465 wxRect r( CellToRect( row, col ) );
7466
7467 // convert to device coords
7468 //
7469 int left, top, right, bottom;
7470 CalcScrolledPosition( r.GetLeft(), r.GetTop(), &left, &top );
7471 CalcScrolledPosition( r.GetRight(), r.GetBottom(), &right, &bottom );
7472
7473 // check against the client area of the grid window
7474 //
7475 int cw, ch;
7476 m_gridWin->GetClientSize( &cw, &ch );
7477
7478 if ( wholeCellVisible )
7479 {
7480 // is the cell wholly visible ?
7481 //
7482 return ( left >= 0 && right <= cw &&
7483 top >= 0 && bottom <= ch );
7484 }
7485 else
7486 {
7487 // is the cell partly visible ?
7488 //
7489 return ( ((left >=0 && left < cw) || (right > 0 && right <= cw)) &&
7490 ((top >=0 && top < ch) || (bottom > 0 && bottom <= ch)) );
7491 }
7492 }
7493
7494
7495 // make the specified cell location visible by doing a minimal amount
7496 // of scrolling
7497 //
7498 void wxGrid::MakeCellVisible( int row, int col )
7499 {
7500
7501 int i;
7502 int xpos = -1, ypos = -1;
7503
7504 if ( row >= 0 && row < m_numRows &&
7505 col >= 0 && col < m_numCols )
7506 {
7507 // get the cell rectangle in logical coords
7508 //
7509 wxRect r( CellToRect( row, col ) );
7510
7511 // convert to device coords
7512 //
7513 int left, top, right, bottom;
7514 CalcScrolledPosition( r.GetLeft(), r.GetTop(), &left, &top );
7515 CalcScrolledPosition( r.GetRight(), r.GetBottom(), &right, &bottom );
7516
7517 int cw, ch;
7518 m_gridWin->GetClientSize( &cw, &ch );
7519
7520 if ( top < 0 )
7521 {
7522 ypos = r.GetTop();
7523 }
7524 else if ( bottom > ch )
7525 {
7526 int h = r.GetHeight();
7527 ypos = r.GetTop();
7528 for ( i = row-1; i >= 0; i-- )
7529 {
7530 int rowHeight = GetRowHeight(i);
7531 if ( h + rowHeight > ch )
7532 break;
7533
7534 h += rowHeight;
7535 ypos -= rowHeight;
7536 }
7537
7538 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
7539 // have rounding errors (this is important, because if we do, we
7540 // might not scroll at all and some cells won't be redrawn)
7541 //
7542 // Sometimes GRID_SCROLL_LINE/2 is not enough, so just add a full
7543 // scroll unit...
7544 ypos += GRID_SCROLL_LINE_Y;
7545 }
7546
7547 if ( left < 0 )
7548 {
7549 xpos = r.GetLeft();
7550 }
7551 else if ( right > cw )
7552 {
7553 int w = r.GetWidth();
7554 xpos = r.GetLeft();
7555 for ( i = col-1; i >= 0; i-- )
7556 {
7557 int colWidth = GetColWidth(i);
7558 if ( w + colWidth > cw )
7559 break;
7560
7561 w += colWidth;
7562 xpos -= colWidth;
7563 }
7564
7565 // see comment for ypos above
7566 xpos += GRID_SCROLL_LINE_X;
7567 }
7568
7569 if ( xpos != -1 || ypos != -1 )
7570 {
7571 if ( xpos != -1 )
7572 xpos /= GRID_SCROLL_LINE_X;
7573 if ( ypos != -1 )
7574 ypos /= GRID_SCROLL_LINE_Y;
7575 Scroll( xpos, ypos );
7576 AdjustScrollbars();
7577 }
7578 }
7579 }
7580
7581
7582 //
7583 // ------ Grid cursor movement functions
7584 //
7585
7586 bool wxGrid::MoveCursorUp( bool expandSelection )
7587 {
7588 if ( m_currentCellCoords != wxGridNoCellCoords &&
7589 m_currentCellCoords.GetRow() >= 0 )
7590 {
7591 if ( expandSelection)
7592 {
7593 if ( m_selectingKeyboard == wxGridNoCellCoords )
7594 m_selectingKeyboard = m_currentCellCoords;
7595 if ( m_selectingKeyboard.GetRow() > 0 )
7596 {
7597 m_selectingKeyboard.SetRow( m_selectingKeyboard.GetRow() - 1 );
7598 MakeCellVisible( m_selectingKeyboard.GetRow(),
7599 m_selectingKeyboard.GetCol() );
7600 HighlightBlock( m_currentCellCoords, m_selectingKeyboard );
7601 }
7602 }
7603 else if ( m_currentCellCoords.GetRow() > 0 )
7604 {
7605 ClearSelection();
7606 MakeCellVisible( m_currentCellCoords.GetRow() - 1,
7607 m_currentCellCoords.GetCol() );
7608 SetCurrentCell( m_currentCellCoords.GetRow() - 1,
7609 m_currentCellCoords.GetCol() );
7610 }
7611 else
7612 return FALSE;
7613 return TRUE;
7614 }
7615
7616 return FALSE;
7617 }
7618
7619
7620 bool wxGrid::MoveCursorDown( bool expandSelection )
7621 {
7622 if ( m_currentCellCoords != wxGridNoCellCoords &&
7623 m_currentCellCoords.GetRow() < m_numRows )
7624 {
7625 if ( expandSelection )
7626 {
7627 if ( m_selectingKeyboard == wxGridNoCellCoords )
7628 m_selectingKeyboard = m_currentCellCoords;
7629 if ( m_selectingKeyboard.GetRow() < m_numRows-1 )
7630 {
7631 m_selectingKeyboard.SetRow( m_selectingKeyboard.GetRow() + 1 );
7632 MakeCellVisible( m_selectingKeyboard.GetRow(),
7633 m_selectingKeyboard.GetCol() );
7634 HighlightBlock( m_currentCellCoords, m_selectingKeyboard );
7635 }
7636 }
7637 else if ( m_currentCellCoords.GetRow() < m_numRows - 1 )
7638 {
7639 ClearSelection();
7640 MakeCellVisible( m_currentCellCoords.GetRow() + 1,
7641 m_currentCellCoords.GetCol() );
7642 SetCurrentCell( m_currentCellCoords.GetRow() + 1,
7643 m_currentCellCoords.GetCol() );
7644 }
7645 else
7646 return FALSE;
7647 return TRUE;
7648 }
7649
7650 return FALSE;
7651 }
7652
7653
7654 bool wxGrid::MoveCursorLeft( bool expandSelection )
7655 {
7656 if ( m_currentCellCoords != wxGridNoCellCoords &&
7657 m_currentCellCoords.GetCol() >= 0 )
7658 {
7659 if ( expandSelection )
7660 {
7661 if ( m_selectingKeyboard == wxGridNoCellCoords )
7662 m_selectingKeyboard = m_currentCellCoords;
7663 if ( m_selectingKeyboard.GetCol() > 0 )
7664 {
7665 m_selectingKeyboard.SetCol( m_selectingKeyboard.GetCol() - 1 );
7666 MakeCellVisible( m_selectingKeyboard.GetRow(),
7667 m_selectingKeyboard.GetCol() );
7668 HighlightBlock( m_currentCellCoords, m_selectingKeyboard );
7669 }
7670 }
7671 else if ( m_currentCellCoords.GetCol() > 0 )
7672 {
7673 ClearSelection();
7674 MakeCellVisible( m_currentCellCoords.GetRow(),
7675 m_currentCellCoords.GetCol() - 1 );
7676 SetCurrentCell( m_currentCellCoords.GetRow(),
7677 m_currentCellCoords.GetCol() - 1 );
7678 }
7679 else
7680 return FALSE;
7681 return TRUE;
7682 }
7683
7684 return FALSE;
7685 }
7686
7687
7688 bool wxGrid::MoveCursorRight( bool expandSelection )
7689 {
7690 if ( m_currentCellCoords != wxGridNoCellCoords &&
7691 m_currentCellCoords.GetCol() < m_numCols )
7692 {
7693 if ( expandSelection )
7694 {
7695 if ( m_selectingKeyboard == wxGridNoCellCoords )
7696 m_selectingKeyboard = m_currentCellCoords;
7697 if ( m_selectingKeyboard.GetCol() < m_numCols - 1 )
7698 {
7699 m_selectingKeyboard.SetCol( m_selectingKeyboard.GetCol() + 1 );
7700 MakeCellVisible( m_selectingKeyboard.GetRow(),
7701 m_selectingKeyboard.GetCol() );
7702 HighlightBlock( m_currentCellCoords, m_selectingKeyboard );
7703 }
7704 }
7705 else if ( m_currentCellCoords.GetCol() < m_numCols - 1 )
7706 {
7707 ClearSelection();
7708 MakeCellVisible( m_currentCellCoords.GetRow(),
7709 m_currentCellCoords.GetCol() + 1 );
7710 SetCurrentCell( m_currentCellCoords.GetRow(),
7711 m_currentCellCoords.GetCol() + 1 );
7712 }
7713 else
7714 return FALSE;
7715 return TRUE;
7716 }
7717
7718 return FALSE;
7719 }
7720
7721
7722 bool wxGrid::MovePageUp()
7723 {
7724 if ( m_currentCellCoords == wxGridNoCellCoords ) return FALSE;
7725
7726 int row = m_currentCellCoords.GetRow();
7727 if ( row > 0 )
7728 {
7729 int cw, ch;
7730 m_gridWin->GetClientSize( &cw, &ch );
7731
7732 int y = GetRowTop(row);
7733 int newRow = YToRow( y - ch + 1 );
7734 if ( newRow == -1 )
7735 {
7736 newRow = 0;
7737 }
7738 else if ( newRow == row )
7739 {
7740 newRow = row - 1;
7741 }
7742
7743 MakeCellVisible( newRow, m_currentCellCoords.GetCol() );
7744 SetCurrentCell( newRow, m_currentCellCoords.GetCol() );
7745
7746 return TRUE;
7747 }
7748
7749 return FALSE;
7750 }
7751
7752 bool wxGrid::MovePageDown()
7753 {
7754 if ( m_currentCellCoords == wxGridNoCellCoords ) return FALSE;
7755
7756 int row = m_currentCellCoords.GetRow();
7757 if ( row < m_numRows )
7758 {
7759 int cw, ch;
7760 m_gridWin->GetClientSize( &cw, &ch );
7761
7762 int y = GetRowTop(row);
7763 int newRow = YToRow( y + ch );
7764 if ( newRow == -1 )
7765 {
7766 newRow = m_numRows - 1;
7767 }
7768 else if ( newRow == row )
7769 {
7770 newRow = row + 1;
7771 }
7772
7773 MakeCellVisible( newRow, m_currentCellCoords.GetCol() );
7774 SetCurrentCell( newRow, m_currentCellCoords.GetCol() );
7775
7776 return TRUE;
7777 }
7778
7779 return FALSE;
7780 }
7781
7782 bool wxGrid::MoveCursorUpBlock( bool expandSelection )
7783 {
7784 if ( m_table &&
7785 m_currentCellCoords != wxGridNoCellCoords &&
7786 m_currentCellCoords.GetRow() > 0 )
7787 {
7788 int row = m_currentCellCoords.GetRow();
7789 int col = m_currentCellCoords.GetCol();
7790
7791 if ( m_table->IsEmptyCell(row, col) )
7792 {
7793 // starting in an empty cell: find the next block of
7794 // non-empty cells
7795 //
7796 while ( row > 0 )
7797 {
7798 row-- ;
7799 if ( !(m_table->IsEmptyCell(row, col)) ) break;
7800 }
7801 }
7802 else if ( m_table->IsEmptyCell(row-1, col) )
7803 {
7804 // starting at the top of a block: find the next block
7805 //
7806 row--;
7807 while ( row > 0 )
7808 {
7809 row-- ;
7810 if ( !(m_table->IsEmptyCell(row, col)) ) break;
7811 }
7812 }
7813 else
7814 {
7815 // starting within a block: find the top of the block
7816 //
7817 while ( row > 0 )
7818 {
7819 row-- ;
7820 if ( m_table->IsEmptyCell(row, col) )
7821 {
7822 row++ ;
7823 break;
7824 }
7825 }
7826 }
7827
7828 MakeCellVisible( row, col );
7829 if ( expandSelection )
7830 {
7831 m_selectingKeyboard = wxGridCellCoords( row, col );
7832 HighlightBlock( m_currentCellCoords, m_selectingKeyboard );
7833 }
7834 else
7835 {
7836 ClearSelection();
7837 SetCurrentCell( row, col );
7838 }
7839 return TRUE;
7840 }
7841
7842 return FALSE;
7843 }
7844
7845 bool wxGrid::MoveCursorDownBlock( bool expandSelection )
7846 {
7847 if ( m_table &&
7848 m_currentCellCoords != wxGridNoCellCoords &&
7849 m_currentCellCoords.GetRow() < m_numRows-1 )
7850 {
7851 int row = m_currentCellCoords.GetRow();
7852 int col = m_currentCellCoords.GetCol();
7853
7854 if ( m_table->IsEmptyCell(row, col) )
7855 {
7856 // starting in an empty cell: find the next block of
7857 // non-empty cells
7858 //
7859 while ( row < m_numRows-1 )
7860 {
7861 row++ ;
7862 if ( !(m_table->IsEmptyCell(row, col)) ) break;
7863 }
7864 }
7865 else if ( m_table->IsEmptyCell(row+1, col) )
7866 {
7867 // starting at the bottom of a block: find the next block
7868 //
7869 row++;
7870 while ( row < m_numRows-1 )
7871 {
7872 row++ ;
7873 if ( !(m_table->IsEmptyCell(row, col)) ) break;
7874 }
7875 }
7876 else
7877 {
7878 // starting within a block: find the bottom of the block
7879 //
7880 while ( row < m_numRows-1 )
7881 {
7882 row++ ;
7883 if ( m_table->IsEmptyCell(row, col) )
7884 {
7885 row-- ;
7886 break;
7887 }
7888 }
7889 }
7890
7891 MakeCellVisible( row, col );
7892 if ( expandSelection )
7893 {
7894 m_selectingKeyboard = wxGridCellCoords( row, col );
7895 HighlightBlock( m_currentCellCoords, m_selectingKeyboard );
7896 }
7897 else
7898 {
7899 ClearSelection();
7900 SetCurrentCell( row, col );
7901 }
7902
7903 return TRUE;
7904 }
7905
7906 return FALSE;
7907 }
7908
7909 bool wxGrid::MoveCursorLeftBlock( bool expandSelection )
7910 {
7911 if ( m_table &&
7912 m_currentCellCoords != wxGridNoCellCoords &&
7913 m_currentCellCoords.GetCol() > 0 )
7914 {
7915 int row = m_currentCellCoords.GetRow();
7916 int col = m_currentCellCoords.GetCol();
7917
7918 if ( m_table->IsEmptyCell(row, col) )
7919 {
7920 // starting in an empty cell: find the next block of
7921 // non-empty cells
7922 //
7923 while ( col > 0 )
7924 {
7925 col-- ;
7926 if ( !(m_table->IsEmptyCell(row, col)) ) break;
7927 }
7928 }
7929 else if ( m_table->IsEmptyCell(row, col-1) )
7930 {
7931 // starting at the left of a block: find the next block
7932 //
7933 col--;
7934 while ( col > 0 )
7935 {
7936 col-- ;
7937 if ( !(m_table->IsEmptyCell(row, col)) ) break;
7938 }
7939 }
7940 else
7941 {
7942 // starting within a block: find the left of the block
7943 //
7944 while ( col > 0 )
7945 {
7946 col-- ;
7947 if ( m_table->IsEmptyCell(row, col) )
7948 {
7949 col++ ;
7950 break;
7951 }
7952 }
7953 }
7954
7955 MakeCellVisible( row, col );
7956 if ( expandSelection )
7957 {
7958 m_selectingKeyboard = wxGridCellCoords( row, col );
7959 HighlightBlock( m_currentCellCoords, m_selectingKeyboard );
7960 }
7961 else
7962 {
7963 ClearSelection();
7964 SetCurrentCell( row, col );
7965 }
7966
7967 return TRUE;
7968 }
7969
7970 return FALSE;
7971 }
7972
7973 bool wxGrid::MoveCursorRightBlock( bool expandSelection )
7974 {
7975 if ( m_table &&
7976 m_currentCellCoords != wxGridNoCellCoords &&
7977 m_currentCellCoords.GetCol() < m_numCols-1 )
7978 {
7979 int row = m_currentCellCoords.GetRow();
7980 int col = m_currentCellCoords.GetCol();
7981
7982 if ( m_table->IsEmptyCell(row, col) )
7983 {
7984 // starting in an empty cell: find the next block of
7985 // non-empty cells
7986 //
7987 while ( col < m_numCols-1 )
7988 {
7989 col++ ;
7990 if ( !(m_table->IsEmptyCell(row, col)) ) break;
7991 }
7992 }
7993 else if ( m_table->IsEmptyCell(row, col+1) )
7994 {
7995 // starting at the right of a block: find the next block
7996 //
7997 col++;
7998 while ( col < m_numCols-1 )
7999 {
8000 col++ ;
8001 if ( !(m_table->IsEmptyCell(row, col)) ) break;
8002 }
8003 }
8004 else
8005 {
8006 // starting within a block: find the right of the block
8007 //
8008 while ( col < m_numCols-1 )
8009 {
8010 col++ ;
8011 if ( m_table->IsEmptyCell(row, col) )
8012 {
8013 col-- ;
8014 break;
8015 }
8016 }
8017 }
8018
8019 MakeCellVisible( row, col );
8020 if ( expandSelection )
8021 {
8022 m_selectingKeyboard = wxGridCellCoords( row, col );
8023 HighlightBlock( m_currentCellCoords, m_selectingKeyboard );
8024 }
8025 else
8026 {
8027 ClearSelection();
8028 SetCurrentCell( row, col );
8029 }
8030
8031 return TRUE;
8032 }
8033
8034 return FALSE;
8035 }
8036
8037
8038
8039 //
8040 // ------ Label values and formatting
8041 //
8042
8043 void wxGrid::GetRowLabelAlignment( int *horiz, int *vert )
8044 {
8045 *horiz = m_rowLabelHorizAlign;
8046 *vert = m_rowLabelVertAlign;
8047 }
8048
8049 void wxGrid::GetColLabelAlignment( int *horiz, int *vert )
8050 {
8051 *horiz = m_colLabelHorizAlign;
8052 *vert = m_colLabelVertAlign;
8053 }
8054
8055 wxString wxGrid::GetRowLabelValue( int row )
8056 {
8057 if ( m_table )
8058 {
8059 return m_table->GetRowLabelValue( row );
8060 }
8061 else
8062 {
8063 wxString s;
8064 s << row;
8065 return s;
8066 }
8067 }
8068
8069 wxString wxGrid::GetColLabelValue( int col )
8070 {
8071 if ( m_table )
8072 {
8073 return m_table->GetColLabelValue( col );
8074 }
8075 else
8076 {
8077 wxString s;
8078 s << col;
8079 return s;
8080 }
8081 }
8082
8083
8084 void wxGrid::SetRowLabelSize( int width )
8085 {
8086 width = wxMax( width, 0 );
8087 if ( width != m_rowLabelWidth )
8088 {
8089 if ( width == 0 )
8090 {
8091 m_rowLabelWin->Show( FALSE );
8092 m_cornerLabelWin->Show( FALSE );
8093 }
8094 else if ( m_rowLabelWidth == 0 )
8095 {
8096 m_rowLabelWin->Show( TRUE );
8097 if ( m_colLabelHeight > 0 ) m_cornerLabelWin->Show( TRUE );
8098 }
8099
8100 m_rowLabelWidth = width;
8101 CalcWindowSizes();
8102 wxScrolledWindow::Refresh( TRUE );
8103 }
8104 }
8105
8106
8107 void wxGrid::SetColLabelSize( int height )
8108 {
8109 height = wxMax( height, 0 );
8110 if ( height != m_colLabelHeight )
8111 {
8112 if ( height == 0 )
8113 {
8114 m_colLabelWin->Show( FALSE );
8115 m_cornerLabelWin->Show( FALSE );
8116 }
8117 else if ( m_colLabelHeight == 0 )
8118 {
8119 m_colLabelWin->Show( TRUE );
8120 if ( m_rowLabelWidth > 0 ) m_cornerLabelWin->Show( TRUE );
8121 }
8122
8123 m_colLabelHeight = height;
8124 CalcWindowSizes();
8125 wxScrolledWindow::Refresh( TRUE );
8126 }
8127 }
8128
8129
8130 void wxGrid::SetLabelBackgroundColour( const wxColour& colour )
8131 {
8132 if ( m_labelBackgroundColour != colour )
8133 {
8134 m_labelBackgroundColour = colour;
8135 m_rowLabelWin->SetBackgroundColour( colour );
8136 m_colLabelWin->SetBackgroundColour( colour );
8137 m_cornerLabelWin->SetBackgroundColour( colour );
8138
8139 if ( !GetBatchCount() )
8140 {
8141 m_rowLabelWin->Refresh();
8142 m_colLabelWin->Refresh();
8143 m_cornerLabelWin->Refresh();
8144 }
8145 }
8146 }
8147
8148 void wxGrid::SetLabelTextColour( const wxColour& colour )
8149 {
8150 if ( m_labelTextColour != colour )
8151 {
8152 m_labelTextColour = colour;
8153 if ( !GetBatchCount() )
8154 {
8155 m_rowLabelWin->Refresh();
8156 m_colLabelWin->Refresh();
8157 }
8158 }
8159 }
8160
8161 void wxGrid::SetLabelFont( const wxFont& font )
8162 {
8163 m_labelFont = font;
8164 if ( !GetBatchCount() )
8165 {
8166 m_rowLabelWin->Refresh();
8167 m_colLabelWin->Refresh();
8168 }
8169 }
8170
8171 void wxGrid::SetRowLabelAlignment( int horiz, int vert )
8172 {
8173 // allow old (incorrect) defs to be used
8174 switch ( horiz )
8175 {
8176 case wxLEFT: horiz = wxALIGN_LEFT; break;
8177 case wxRIGHT: horiz = wxALIGN_RIGHT; break;
8178 case wxCENTRE: horiz = wxALIGN_CENTRE; break;
8179 }
8180
8181 switch ( vert )
8182 {
8183 case wxTOP: vert = wxALIGN_TOP; break;
8184 case wxBOTTOM: vert = wxALIGN_BOTTOM; break;
8185 case wxCENTRE: vert = wxALIGN_CENTRE; break;
8186 }
8187
8188 if ( horiz == wxALIGN_LEFT || horiz == wxALIGN_CENTRE || horiz == wxALIGN_RIGHT )
8189 {
8190 m_rowLabelHorizAlign = horiz;
8191 }
8192
8193 if ( vert == wxALIGN_TOP || vert == wxALIGN_CENTRE || vert == wxALIGN_BOTTOM )
8194 {
8195 m_rowLabelVertAlign = vert;
8196 }
8197
8198 if ( !GetBatchCount() )
8199 {
8200 m_rowLabelWin->Refresh();
8201 }
8202 }
8203
8204 void wxGrid::SetColLabelAlignment( int horiz, int vert )
8205 {
8206 // allow old (incorrect) defs to be used
8207 switch ( horiz )
8208 {
8209 case wxLEFT: horiz = wxALIGN_LEFT; break;
8210 case wxRIGHT: horiz = wxALIGN_RIGHT; break;
8211 case wxCENTRE: horiz = wxALIGN_CENTRE; break;
8212 }
8213
8214 switch ( vert )
8215 {
8216 case wxTOP: vert = wxALIGN_TOP; break;
8217 case wxBOTTOM: vert = wxALIGN_BOTTOM; break;
8218 case wxCENTRE: vert = wxALIGN_CENTRE; break;
8219 }
8220
8221 if ( horiz == wxALIGN_LEFT || horiz == wxALIGN_CENTRE || horiz == wxALIGN_RIGHT )
8222 {
8223 m_colLabelHorizAlign = horiz;
8224 }
8225
8226 if ( vert == wxALIGN_TOP || vert == wxALIGN_CENTRE || vert == wxALIGN_BOTTOM )
8227 {
8228 m_colLabelVertAlign = vert;
8229 }
8230
8231 if ( !GetBatchCount() )
8232 {
8233 m_colLabelWin->Refresh();
8234 }
8235 }
8236
8237 void wxGrid::SetRowLabelValue( int row, const wxString& s )
8238 {
8239 if ( m_table )
8240 {
8241 m_table->SetRowLabelValue( row, s );
8242 if ( !GetBatchCount() )
8243 {
8244 wxRect rect = CellToRect( row, 0);
8245 if ( rect.height > 0 )
8246 {
8247 CalcScrolledPosition(0, rect.y, &rect.x, &rect.y);
8248 rect.x = 0;
8249 rect.width = m_rowLabelWidth;
8250 m_rowLabelWin->Refresh( TRUE, &rect );
8251 }
8252 }
8253 }
8254 }
8255
8256 void wxGrid::SetColLabelValue( int col, const wxString& s )
8257 {
8258 if ( m_table )
8259 {
8260 m_table->SetColLabelValue( col, s );
8261 if ( !GetBatchCount() )
8262 {
8263 wxRect rect = CellToRect( 0, col );
8264 if ( rect.width > 0 )
8265 {
8266 CalcScrolledPosition(rect.x, 0, &rect.x, &rect.y);
8267 rect.y = 0;
8268 rect.height = m_colLabelHeight;
8269 m_colLabelWin->Refresh( TRUE, &rect );
8270 }
8271 }
8272 }
8273 }
8274
8275 void wxGrid::SetGridLineColour( const wxColour& colour )
8276 {
8277 if ( m_gridLineColour != colour )
8278 {
8279 m_gridLineColour = colour;
8280
8281 wxClientDC dc( m_gridWin );
8282 PrepareDC( dc );
8283 DrawAllGridLines( dc, wxRegion() );
8284 }
8285 }
8286
8287
8288 void wxGrid::SetCellHighlightColour( const wxColour& colour )
8289 {
8290 if ( m_cellHighlightColour != colour )
8291 {
8292 m_cellHighlightColour = colour;
8293
8294 wxClientDC dc( m_gridWin );
8295 PrepareDC( dc );
8296 wxGridCellAttr* attr = GetCellAttr(m_currentCellCoords);
8297 DrawCellHighlight(dc, attr);
8298 attr->DecRef();
8299 }
8300 }
8301
8302 void wxGrid::SetCellHighlightPenWidth(int width)
8303 {
8304 if (m_cellHighlightPenWidth != width) {
8305 m_cellHighlightPenWidth = width;
8306
8307 // Just redrawing the cell highlight is not enough since that won't
8308 // make any visible change if the the thickness is getting smaller.
8309 int row = m_currentCellCoords.GetRow();
8310 int col = m_currentCellCoords.GetCol();
8311 if ( GetColWidth(col) <= 0 || GetRowHeight(row) <= 0 )
8312 return;
8313 wxRect rect = CellToRect(row, col);
8314 m_gridWin->Refresh(TRUE, &rect);
8315 }
8316 }
8317
8318 void wxGrid::SetCellHighlightROPenWidth(int width)
8319 {
8320 if (m_cellHighlightROPenWidth != width) {
8321 m_cellHighlightROPenWidth = width;
8322
8323 // Just redrawing the cell highlight is not enough since that won't
8324 // make any visible change if the the thickness is getting smaller.
8325 int row = m_currentCellCoords.GetRow();
8326 int col = m_currentCellCoords.GetCol();
8327 if ( GetColWidth(col) <= 0 || GetRowHeight(row) <= 0 )
8328 return;
8329 wxRect rect = CellToRect(row, col);
8330 m_gridWin->Refresh(TRUE, &rect);
8331 }
8332 }
8333
8334 void wxGrid::EnableGridLines( bool enable )
8335 {
8336 if ( enable != m_gridLinesEnabled )
8337 {
8338 m_gridLinesEnabled = enable;
8339
8340 if ( !GetBatchCount() )
8341 {
8342 if ( enable )
8343 {
8344 wxClientDC dc( m_gridWin );
8345 PrepareDC( dc );
8346 DrawAllGridLines( dc, wxRegion() );
8347 }
8348 else
8349 {
8350 m_gridWin->Refresh();
8351 }
8352 }
8353 }
8354 }
8355
8356
8357 int wxGrid::GetDefaultRowSize()
8358 {
8359 return m_defaultRowHeight;
8360 }
8361
8362 int wxGrid::GetRowSize( int row )
8363 {
8364 wxCHECK_MSG( row >= 0 && row < m_numRows, 0, _T("invalid row index") );
8365
8366 return GetRowHeight(row);
8367 }
8368
8369 int wxGrid::GetDefaultColSize()
8370 {
8371 return m_defaultColWidth;
8372 }
8373
8374 int wxGrid::GetColSize( int col )
8375 {
8376 wxCHECK_MSG( col >= 0 && col < m_numCols, 0, _T("invalid column index") );
8377
8378 return GetColWidth(col);
8379 }
8380
8381 // ============================================================================
8382 // access to the grid attributes: each of them has a default value in the grid
8383 // itself and may be overidden on a per-cell basis
8384 // ============================================================================
8385
8386 // ----------------------------------------------------------------------------
8387 // setting default attributes
8388 // ----------------------------------------------------------------------------
8389
8390 void wxGrid::SetDefaultCellBackgroundColour( const wxColour& col )
8391 {
8392 m_defaultCellAttr->SetBackgroundColour(col);
8393 #ifdef __WXGTK__
8394 m_gridWin->SetBackgroundColour(col);
8395 #endif
8396 }
8397
8398 void wxGrid::SetDefaultCellTextColour( const wxColour& col )
8399 {
8400 m_defaultCellAttr->SetTextColour(col);
8401 }
8402
8403 void wxGrid::SetDefaultCellAlignment( int horiz, int vert )
8404 {
8405 m_defaultCellAttr->SetAlignment(horiz, vert);
8406 }
8407
8408 void wxGrid::SetDefaultCellOverflow( bool allow )
8409 {
8410 m_defaultCellAttr->SetOverflow(allow);
8411 }
8412
8413 void wxGrid::SetDefaultCellFont( const wxFont& font )
8414 {
8415 m_defaultCellAttr->SetFont(font);
8416 }
8417
8418 void wxGrid::SetDefaultRenderer(wxGridCellRenderer *renderer)
8419 {
8420 m_defaultCellAttr->SetRenderer(renderer);
8421 }
8422
8423 void wxGrid::SetDefaultEditor(wxGridCellEditor *editor)
8424 {
8425 m_defaultCellAttr->SetEditor(editor);
8426 }
8427
8428 // ----------------------------------------------------------------------------
8429 // access to the default attrbiutes
8430 // ----------------------------------------------------------------------------
8431
8432 wxColour wxGrid::GetDefaultCellBackgroundColour()
8433 {
8434 return m_defaultCellAttr->GetBackgroundColour();
8435 }
8436
8437 wxColour wxGrid::GetDefaultCellTextColour()
8438 {
8439 return m_defaultCellAttr->GetTextColour();
8440 }
8441
8442 wxFont wxGrid::GetDefaultCellFont()
8443 {
8444 return m_defaultCellAttr->GetFont();
8445 }
8446
8447 void wxGrid::GetDefaultCellAlignment( int *horiz, int *vert )
8448 {
8449 m_defaultCellAttr->GetAlignment(horiz, vert);
8450 }
8451
8452 bool wxGrid::GetDefaultCellOverflow()
8453 {
8454 return m_defaultCellAttr->GetOverflow();
8455 }
8456
8457 wxGridCellRenderer *wxGrid::GetDefaultRenderer() const
8458 {
8459 return m_defaultCellAttr->GetRenderer(NULL, 0, 0);
8460 }
8461
8462 wxGridCellEditor *wxGrid::GetDefaultEditor() const
8463 {
8464 return m_defaultCellAttr->GetEditor(NULL,0,0);
8465 }
8466
8467 // ----------------------------------------------------------------------------
8468 // access to cell attributes
8469 // ----------------------------------------------------------------------------
8470
8471 wxColour wxGrid::GetCellBackgroundColour(int row, int col)
8472 {
8473 wxGridCellAttr *attr = GetCellAttr(row, col);
8474 wxColour colour = attr->GetBackgroundColour();
8475 attr->DecRef();
8476 return colour;
8477 }
8478
8479 wxColour wxGrid::GetCellTextColour( int row, int col )
8480 {
8481 wxGridCellAttr *attr = GetCellAttr(row, col);
8482 wxColour colour = attr->GetTextColour();
8483 attr->DecRef();
8484 return colour;
8485 }
8486
8487 wxFont wxGrid::GetCellFont( int row, int col )
8488 {
8489 wxGridCellAttr *attr = GetCellAttr(row, col);
8490 wxFont font = attr->GetFont();
8491 attr->DecRef();
8492 return font;
8493 }
8494
8495 void wxGrid::GetCellAlignment( int row, int col, int *horiz, int *vert )
8496 {
8497 wxGridCellAttr *attr = GetCellAttr(row, col);
8498 attr->GetAlignment(horiz, vert);
8499 attr->DecRef();
8500 }
8501
8502 bool wxGrid::GetCellOverflow( int row, int col )
8503 {
8504 wxGridCellAttr *attr = GetCellAttr(row, col);
8505 bool allow = attr->GetOverflow();
8506 attr->DecRef();
8507 return allow;
8508 }
8509
8510 void wxGrid::GetCellSize( int row, int col, int *num_rows, int *num_cols )
8511 {
8512 wxGridCellAttr *attr = GetCellAttr(row, col);
8513 attr->GetSize( num_rows, num_cols );
8514 attr->DecRef();
8515 }
8516
8517 wxGridCellRenderer* wxGrid::GetCellRenderer(int row, int col)
8518 {
8519 wxGridCellAttr* attr = GetCellAttr(row, col);
8520 wxGridCellRenderer* renderer = attr->GetRenderer(this, row, col);
8521 attr->DecRef();
8522
8523 return renderer;
8524 }
8525
8526 wxGridCellEditor* wxGrid::GetCellEditor(int row, int col)
8527 {
8528 wxGridCellAttr* attr = GetCellAttr(row, col);
8529 wxGridCellEditor* editor = attr->GetEditor(this, row, col);
8530 attr->DecRef();
8531
8532 return editor;
8533 }
8534
8535 bool wxGrid::IsReadOnly(int row, int col) const
8536 {
8537 wxGridCellAttr* attr = GetCellAttr(row, col);
8538 bool isReadOnly = attr->IsReadOnly();
8539 attr->DecRef();
8540 return isReadOnly;
8541 }
8542
8543 // ----------------------------------------------------------------------------
8544 // attribute support: cache, automatic provider creation, ...
8545 // ----------------------------------------------------------------------------
8546
8547 bool wxGrid::CanHaveAttributes()
8548 {
8549 if ( !m_table )
8550 {
8551 return FALSE;
8552 }
8553
8554 return m_table->CanHaveAttributes();
8555 }
8556
8557 void wxGrid::ClearAttrCache()
8558 {
8559 if ( m_attrCache.row != -1 )
8560 {
8561 wxSafeDecRef(m_attrCache.attr);
8562 m_attrCache.attr = NULL;
8563 m_attrCache.row = -1;
8564 }
8565 }
8566
8567 void wxGrid::CacheAttr(int row, int col, wxGridCellAttr *attr) const
8568 {
8569 wxGrid *self = (wxGrid *)this; // const_cast
8570
8571 self->ClearAttrCache();
8572 self->m_attrCache.row = row;
8573 self->m_attrCache.col = col;
8574 self->m_attrCache.attr = attr;
8575 wxSafeIncRef(attr);
8576 }
8577
8578 bool wxGrid::LookupAttr(int row, int col, wxGridCellAttr **attr) const
8579 {
8580 if ( row == m_attrCache.row && col == m_attrCache.col )
8581 {
8582 *attr = m_attrCache.attr;
8583 wxSafeIncRef(m_attrCache.attr);
8584
8585 #ifdef DEBUG_ATTR_CACHE
8586 gs_nAttrCacheHits++;
8587 #endif
8588
8589 return TRUE;
8590 }
8591 else
8592 {
8593 #ifdef DEBUG_ATTR_CACHE
8594 gs_nAttrCacheMisses++;
8595 #endif
8596 return FALSE;
8597 }
8598 }
8599
8600 wxGridCellAttr *wxGrid::GetCellAttr(int row, int col) const
8601 {
8602 wxGridCellAttr *attr = NULL;
8603 // Additional test to avoid looking at the cache e.g. for
8604 // wxNoCellCoords, as this will confuse memory management.
8605 if ( row >= 0 )
8606 {
8607 if ( !LookupAttr(row, col, &attr) )
8608 {
8609 attr = m_table ? m_table->GetAttr(row, col , wxGridCellAttr::Any)
8610 : (wxGridCellAttr *)NULL;
8611 CacheAttr(row, col, attr);
8612 }
8613 }
8614 if (attr)
8615 {
8616 attr->SetDefAttr(m_defaultCellAttr);
8617 }
8618 else
8619 {
8620 attr = m_defaultCellAttr;
8621 attr->IncRef();
8622 }
8623
8624 return attr;
8625 }
8626
8627 wxGridCellAttr *wxGrid::GetOrCreateCellAttr(int row, int col) const
8628 {
8629 wxGridCellAttr *attr = (wxGridCellAttr *)NULL;
8630
8631 wxCHECK_MSG( m_table, attr,
8632 _T("we may only be called if CanHaveAttributes() returned TRUE and then m_table should be !NULL") );
8633
8634 attr = m_table->GetAttr(row, col, wxGridCellAttr::Cell);
8635 if ( !attr )
8636 {
8637 attr = new wxGridCellAttr(m_defaultCellAttr);
8638
8639 // artificially inc the ref count to match DecRef() in caller
8640 attr->IncRef();
8641 m_table->SetAttr(attr, row, col);
8642 }
8643
8644 return attr;
8645 }
8646
8647 // ----------------------------------------------------------------------------
8648 // setting column attributes (wrappers around SetColAttr)
8649 // ----------------------------------------------------------------------------
8650
8651 void wxGrid::SetColFormatBool(int col)
8652 {
8653 SetColFormatCustom(col, wxGRID_VALUE_BOOL);
8654 }
8655
8656 void wxGrid::SetColFormatNumber(int col)
8657 {
8658 SetColFormatCustom(col, wxGRID_VALUE_NUMBER);
8659 }
8660
8661 void wxGrid::SetColFormatFloat(int col, int width, int precision)
8662 {
8663 wxString typeName = wxGRID_VALUE_FLOAT;
8664 if ( (width != -1) || (precision != -1) )
8665 {
8666 typeName << _T(':') << width << _T(',') << precision;
8667 }
8668
8669 SetColFormatCustom(col, typeName);
8670 }
8671
8672 void wxGrid::SetColFormatCustom(int col, const wxString& typeName)
8673 {
8674 wxGridCellAttr *attr = (wxGridCellAttr *)NULL;
8675
8676 attr = m_table->GetAttr(-1, col, wxGridCellAttr::Col );
8677 if(!attr)
8678 attr = new wxGridCellAttr;
8679 wxGridCellRenderer *renderer = GetDefaultRendererForType(typeName);
8680 attr->SetRenderer(renderer);
8681
8682 SetColAttr(col, attr);
8683
8684 }
8685
8686 // ----------------------------------------------------------------------------
8687 // setting cell attributes: this is forwarded to the table
8688 // ----------------------------------------------------------------------------
8689
8690 void wxGrid::SetAttr(int row, int col, wxGridCellAttr *attr)
8691 {
8692 if ( CanHaveAttributes() )
8693 {
8694 m_table->SetAttr(attr, row, col);
8695 ClearAttrCache();
8696 }
8697 else
8698 {
8699 wxSafeDecRef(attr);
8700 }
8701 }
8702
8703 void wxGrid::SetRowAttr(int row, wxGridCellAttr *attr)
8704 {
8705 if ( CanHaveAttributes() )
8706 {
8707 m_table->SetRowAttr(attr, row);
8708 ClearAttrCache();
8709 }
8710 else
8711 {
8712 wxSafeDecRef(attr);
8713 }
8714 }
8715
8716 void wxGrid::SetColAttr(int col, wxGridCellAttr *attr)
8717 {
8718 if ( CanHaveAttributes() )
8719 {
8720 m_table->SetColAttr(attr, col);
8721 ClearAttrCache();
8722 }
8723 else
8724 {
8725 wxSafeDecRef(attr);
8726 }
8727 }
8728
8729 void wxGrid::SetCellBackgroundColour( int row, int col, const wxColour& colour )
8730 {
8731 if ( CanHaveAttributes() )
8732 {
8733 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
8734 attr->SetBackgroundColour(colour);
8735 attr->DecRef();
8736 }
8737 }
8738
8739 void wxGrid::SetCellTextColour( int row, int col, const wxColour& colour )
8740 {
8741 if ( CanHaveAttributes() )
8742 {
8743 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
8744 attr->SetTextColour(colour);
8745 attr->DecRef();
8746 }
8747 }
8748
8749 void wxGrid::SetCellFont( int row, int col, const wxFont& font )
8750 {
8751 if ( CanHaveAttributes() )
8752 {
8753 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
8754 attr->SetFont(font);
8755 attr->DecRef();
8756 }
8757 }
8758
8759 void wxGrid::SetCellAlignment( int row, int col, int horiz, int vert )
8760 {
8761 if ( CanHaveAttributes() )
8762 {
8763 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
8764 attr->SetAlignment(horiz, vert);
8765 attr->DecRef();
8766 }
8767 }
8768
8769 void wxGrid::SetCellOverflow( int row, int col, bool allow )
8770 {
8771 if ( CanHaveAttributes() )
8772 {
8773 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
8774 attr->SetOverflow(allow);
8775 attr->DecRef();
8776 }
8777 }
8778
8779 void wxGrid::SetCellSize( int row, int col, int num_rows, int num_cols )
8780 {
8781 if ( CanHaveAttributes() )
8782 {
8783 int cell_rows, cell_cols;
8784
8785 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
8786 attr->GetSize(&cell_rows, &cell_cols);
8787 attr->SetSize(num_rows, num_cols);
8788 attr->DecRef();
8789
8790 // Cannot set the size of a cell to 0 or negative values
8791 // While it is perfectly legal to do that, this function cannot
8792 // handle all the possibilies, do it by hand by getting the CellAttr.
8793 // You can only set the size of a cell to 1,1 or greater with this fn
8794 wxASSERT_MSG( !((cell_rows < 1) || (cell_cols < 1)),
8795 wxT("wxGrid::SetCellSize setting cell size that is already part of another cell"));
8796 wxASSERT_MSG( !((num_rows < 1) || (num_cols < 1)),
8797 wxT("wxGrid::SetCellSize setting cell size to < 1"));
8798
8799 // if this was already a multicell then "turn off" the other cells first
8800 if ((cell_rows > 1) || (cell_rows > 1))
8801 {
8802 int i, j;
8803 for (j=row; j<row+cell_rows; j++)
8804 {
8805 for (i=col; i<col+cell_cols; i++)
8806 {
8807 if ((i != col) || (j != row))
8808 {
8809 wxGridCellAttr *attr_stub = GetOrCreateCellAttr(j, i);
8810 attr_stub->SetSize( 1, 1 );
8811 attr_stub->DecRef();
8812 }
8813 }
8814 }
8815 }
8816
8817 // mark the cells that will be covered by this cell to
8818 // negative or zero values to point back at this cell
8819 if (((num_rows > 1) || (num_cols > 1)) && (num_rows >= 1) && (num_cols >= 1))
8820 {
8821 int i, j;
8822 for (j=row; j<row+num_rows; j++)
8823 {
8824 for (i=col; i<col+num_cols; i++)
8825 {
8826 if ((i != col) || (j != row))
8827 {
8828 wxGridCellAttr *attr_stub = GetOrCreateCellAttr(j, i);
8829 attr_stub->SetSize( row-j, col-i );
8830 attr_stub->DecRef();
8831 }
8832 }
8833 }
8834 }
8835 }
8836 }
8837
8838 void wxGrid::SetCellRenderer(int row, int col, wxGridCellRenderer *renderer)
8839 {
8840 if ( CanHaveAttributes() )
8841 {
8842 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
8843 attr->SetRenderer(renderer);
8844 attr->DecRef();
8845 }
8846 }
8847
8848 void wxGrid::SetCellEditor(int row, int col, wxGridCellEditor* editor)
8849 {
8850 if ( CanHaveAttributes() )
8851 {
8852 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
8853 attr->SetEditor(editor);
8854 attr->DecRef();
8855 }
8856 }
8857
8858 void wxGrid::SetReadOnly(int row, int col, bool isReadOnly)
8859 {
8860 if ( CanHaveAttributes() )
8861 {
8862 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
8863 attr->SetReadOnly(isReadOnly);
8864 attr->DecRef();
8865 }
8866 }
8867
8868 // ----------------------------------------------------------------------------
8869 // Data type registration
8870 // ----------------------------------------------------------------------------
8871
8872 void wxGrid::RegisterDataType(const wxString& typeName,
8873 wxGridCellRenderer* renderer,
8874 wxGridCellEditor* editor)
8875 {
8876 m_typeRegistry->RegisterDataType(typeName, renderer, editor);
8877 }
8878
8879
8880 wxGridCellEditor* wxGrid::GetDefaultEditorForCell(int row, int col) const
8881 {
8882 wxString typeName = m_table->GetTypeName(row, col);
8883 return GetDefaultEditorForType(typeName);
8884 }
8885
8886 wxGridCellRenderer* wxGrid::GetDefaultRendererForCell(int row, int col) const
8887 {
8888 wxString typeName = m_table->GetTypeName(row, col);
8889 return GetDefaultRendererForType(typeName);
8890 }
8891
8892 wxGridCellEditor*
8893 wxGrid::GetDefaultEditorForType(const wxString& typeName) const
8894 {
8895 int index = m_typeRegistry->FindOrCloneDataType(typeName);
8896 if ( index == wxNOT_FOUND )
8897 {
8898 wxFAIL_MSG(wxT("Unknown data type name"));
8899
8900 return NULL;
8901 }
8902
8903 return m_typeRegistry->GetEditor(index);
8904 }
8905
8906 wxGridCellRenderer*
8907 wxGrid::GetDefaultRendererForType(const wxString& typeName) const
8908 {
8909 int index = m_typeRegistry->FindOrCloneDataType(typeName);
8910 if ( index == wxNOT_FOUND )
8911 {
8912 wxFAIL_MSG(wxT("Unknown data type name"));
8913
8914 return NULL;
8915 }
8916
8917 return m_typeRegistry->GetRenderer(index);
8918 }
8919
8920
8921 // ----------------------------------------------------------------------------
8922 // row/col size
8923 // ----------------------------------------------------------------------------
8924
8925 void wxGrid::EnableDragRowSize( bool enable )
8926 {
8927 m_canDragRowSize = enable;
8928 }
8929
8930
8931 void wxGrid::EnableDragColSize( bool enable )
8932 {
8933 m_canDragColSize = enable;
8934 }
8935
8936 void wxGrid::EnableDragGridSize( bool enable )
8937 {
8938 m_canDragGridSize = enable;
8939 }
8940
8941
8942 void wxGrid::SetDefaultRowSize( int height, bool resizeExistingRows )
8943 {
8944 m_defaultRowHeight = wxMax( height, WXGRID_MIN_ROW_HEIGHT );
8945
8946 if ( resizeExistingRows )
8947 {
8948 // since we are resizing all rows to the default row size,
8949 // we can simply clear the row heights and row bottoms
8950 // arrays (which also allows us to take advantage of
8951 // some speed optimisations)
8952 m_rowHeights.Empty();
8953 m_rowBottoms.Empty();
8954 if ( !GetBatchCount() )
8955 CalcDimensions();
8956 }
8957 }
8958
8959 void wxGrid::SetRowSize( int row, int height )
8960 {
8961 wxCHECK_RET( row >= 0 && row < m_numRows, _T("invalid row index") );
8962
8963 if ( m_rowHeights.IsEmpty() )
8964 {
8965 // need to really create the array
8966 InitRowHeights();
8967 }
8968
8969 int h = wxMax( 0, height );
8970 int diff = h - m_rowHeights[row];
8971
8972 m_rowHeights[row] = h;
8973 int i;
8974 for ( i = row; i < m_numRows; i++ )
8975 {
8976 m_rowBottoms[i] += diff;
8977 }
8978 if ( !GetBatchCount() )
8979 CalcDimensions();
8980 }
8981
8982 void wxGrid::SetDefaultColSize( int width, bool resizeExistingCols )
8983 {
8984 m_defaultColWidth = wxMax( width, WXGRID_MIN_COL_WIDTH );
8985
8986 if ( resizeExistingCols )
8987 {
8988 // since we are resizing all columns to the default column size,
8989 // we can simply clear the col widths and col rights
8990 // arrays (which also allows us to take advantage of
8991 // some speed optimisations)
8992 m_colWidths.Empty();
8993 m_colRights.Empty();
8994 if ( !GetBatchCount() )
8995 CalcDimensions();
8996 }
8997 }
8998
8999 void wxGrid::SetColSize( int col, int width )
9000 {
9001 wxCHECK_RET( col >= 0 && col < m_numCols, _T("invalid column index") );
9002
9003 // should we check that it's bigger than GetColMinimalWidth(col) here?
9004
9005 if ( m_colWidths.IsEmpty() )
9006 {
9007 // need to really create the array
9008 InitColWidths();
9009 }
9010
9011 int w = wxMax( 0, width );
9012 int diff = w - m_colWidths[col];
9013 m_colWidths[col] = w;
9014
9015 int i;
9016 for ( i = col; i < m_numCols; i++ )
9017 {
9018 m_colRights[i] += diff;
9019 }
9020 if ( !GetBatchCount() )
9021 CalcDimensions();
9022 }
9023
9024
9025 void wxGrid::SetColMinimalWidth( int col, int width )
9026 {
9027 m_colMinWidths.Put(col, width);
9028 }
9029
9030 void wxGrid::SetRowMinimalHeight( int row, int width )
9031 {
9032 m_rowMinHeights.Put(row, width);
9033 }
9034
9035 int wxGrid::GetColMinimalWidth(int col) const
9036 {
9037 long value = m_colMinWidths.Get(col);
9038 return value != wxNOT_FOUND ? (int)value : WXGRID_MIN_COL_WIDTH;
9039 }
9040
9041 int wxGrid::GetRowMinimalHeight(int row) const
9042 {
9043 long value = m_rowMinHeights.Get(row);
9044 return value != wxNOT_FOUND ? (int)value : WXGRID_MIN_ROW_HEIGHT;
9045 }
9046
9047 // ----------------------------------------------------------------------------
9048 // auto sizing
9049 // ----------------------------------------------------------------------------
9050
9051 void wxGrid::AutoSizeColOrRow( int colOrRow, bool setAsMin, bool column )
9052 {
9053 wxClientDC dc(m_gridWin);
9054
9055 // init both of them to avoid compiler warnings, even if weo nly need one
9056 int row = -1,
9057 col = -1;
9058 if ( column )
9059 col = colOrRow;
9060 else
9061 row = colOrRow;
9062
9063 wxCoord extent, extentMax = 0;
9064 int max = column ? m_numRows : m_numCols;
9065 for ( int rowOrCol = 0; rowOrCol < max; rowOrCol++ )
9066 {
9067 if ( column )
9068 row = rowOrCol;
9069 else
9070 col = rowOrCol;
9071
9072 wxGridCellAttr* attr = GetCellAttr(row, col);
9073 wxGridCellRenderer* renderer = attr->GetRenderer(this, row, col);
9074 if ( renderer )
9075 {
9076 wxSize size = renderer->GetBestSize(*this, *attr, dc, row, col);
9077 extent = column ? size.x : size.y;
9078 if ( extent > extentMax )
9079 {
9080 extentMax = extent;
9081 }
9082
9083 renderer->DecRef();
9084 }
9085
9086 attr->DecRef();
9087 }
9088
9089 // now also compare with the column label extent
9090 wxCoord w, h;
9091 dc.SetFont( GetLabelFont() );
9092
9093 if ( column )
9094 dc.GetTextExtent( GetColLabelValue(col), &w, &h );
9095 else
9096 dc.GetTextExtent( GetRowLabelValue(row), &w, &h );
9097
9098 extent = column ? w : h;
9099 if ( extent > extentMax )
9100 {
9101 extentMax = extent;
9102 }
9103
9104 if ( !extentMax )
9105 {
9106 // empty column - give default extent (notice that if extentMax is less
9107 // than default extent but != 0, it's ok)
9108 extentMax = column ? m_defaultColWidth : m_defaultRowHeight;
9109 }
9110 else
9111 {
9112 if ( column )
9113 {
9114 // leave some space around text
9115 extentMax += 10;
9116 }
9117 else
9118 {
9119 extentMax += 6;
9120 }
9121 }
9122
9123 if ( column )
9124 {
9125 SetColSize(col, extentMax);
9126 if ( !GetBatchCount() )
9127 {
9128 int cw, ch, dummy;
9129 m_gridWin->GetClientSize( &cw, &ch );
9130 wxRect rect ( CellToRect( 0, col ) );
9131 rect.y = 0;
9132 CalcScrolledPosition(rect.x, 0, &rect.x, &dummy);
9133 rect.width = cw - rect.x;
9134 rect.height = m_colLabelHeight;
9135 m_colLabelWin->Refresh( TRUE, &rect );
9136 }
9137 }
9138 else
9139 {
9140 SetRowSize(row, extentMax);
9141 if ( !GetBatchCount() )
9142 {
9143 int cw, ch, dummy;
9144 m_gridWin->GetClientSize( &cw, &ch );
9145 wxRect rect ( CellToRect( row, 0 ) );
9146 rect.x = 0;
9147 CalcScrolledPosition(0, rect.y, &dummy, &rect.y);
9148 rect.width = m_rowLabelWidth;
9149 rect.height = ch - rect.y;
9150 m_rowLabelWin->Refresh( TRUE, &rect );
9151 }
9152 }
9153 if ( setAsMin )
9154 {
9155 if ( column )
9156 SetColMinimalWidth(col, extentMax);
9157 else
9158 SetRowMinimalHeight(row, extentMax);
9159 }
9160 }
9161
9162 int wxGrid::SetOrCalcColumnSizes(bool calcOnly, bool setAsMin)
9163 {
9164 int width = m_rowLabelWidth;
9165
9166 if ( !calcOnly )
9167 BeginBatch();
9168
9169 for ( int col = 0; col < m_numCols; col++ )
9170 {
9171 if ( !calcOnly )
9172 {
9173 AutoSizeColumn(col, setAsMin);
9174 }
9175
9176 width += GetColWidth(col);
9177 }
9178
9179 if ( !calcOnly )
9180 EndBatch();
9181
9182 return width;
9183 }
9184
9185 int wxGrid::SetOrCalcRowSizes(bool calcOnly, bool setAsMin)
9186 {
9187 int height = m_colLabelHeight;
9188
9189 if ( !calcOnly )
9190 BeginBatch();
9191
9192 for ( int row = 0; row < m_numRows; row++ )
9193 {
9194 if ( !calcOnly )
9195 {
9196 AutoSizeRow(row, setAsMin);
9197 }
9198
9199 height += GetRowHeight(row);
9200 }
9201
9202 if ( !calcOnly )
9203 EndBatch();
9204
9205 return height;
9206 }
9207
9208 void wxGrid::AutoSize()
9209 {
9210 BeginBatch();
9211
9212 wxSize size(SetOrCalcColumnSizes(FALSE), SetOrCalcRowSizes(FALSE));
9213
9214 // round up the size to a multiple of scroll step - this ensures that we
9215 // won't get the scrollbars if we're sized exactly to this width
9216 wxSize sizeFit(GetScrollX(size.x) * GRID_SCROLL_LINE_X,
9217 GetScrollY(size.y) * GRID_SCROLL_LINE_Y);
9218
9219 // distribute the extra space between teh columns/rows to avoid having
9220 // extra white space
9221 wxCoord diff = sizeFit.x - size.x;
9222 if ( diff )
9223 {
9224 // try to resize the columns uniformly
9225 wxCoord diffPerCol = diff / m_numCols;
9226 if ( diffPerCol )
9227 {
9228 for ( int col = 0; col < m_numCols; col++ )
9229 {
9230 SetColSize(col, GetColWidth(col) + diffPerCol);
9231 }
9232 }
9233
9234 // add remaining amount to the last columns
9235 diff -= diffPerCol * m_numCols;
9236 if ( diff )
9237 {
9238 for ( int col = m_numCols - 1; col >= m_numCols - diff; col-- )
9239 {
9240 SetColSize(col, GetColWidth(col) + 1);
9241 }
9242 }
9243 }
9244
9245 // same for rows
9246 diff = sizeFit.y - size.y;
9247 if ( diff )
9248 {
9249 // try to resize the columns uniformly
9250 wxCoord diffPerRow = diff / m_numRows;
9251 if ( diffPerRow )
9252 {
9253 for ( int row = 0; row < m_numRows; row++ )
9254 {
9255 SetRowSize(row, GetRowHeight(row) + diffPerRow);
9256 }
9257 }
9258
9259 // add remaining amount to the last rows
9260 diff -= diffPerRow * m_numRows;
9261 if ( diff )
9262 {
9263 for ( int row = m_numRows - 1; row >= m_numRows - diff; row-- )
9264 {
9265 SetRowSize(row, GetRowHeight(row) + 1);
9266 }
9267 }
9268 }
9269
9270 EndBatch();
9271
9272 SetClientSize(sizeFit);
9273 }
9274
9275 wxSize wxGrid::DoGetBestSize() const
9276 {
9277 // don't set sizes, only calculate them
9278 wxGrid *self = (wxGrid *)this; // const_cast
9279
9280 int width, height;
9281 width = self->SetOrCalcColumnSizes(TRUE);
9282 height = self->SetOrCalcRowSizes(TRUE);
9283
9284 int maxwidth, maxheight;
9285 wxDisplaySize( & maxwidth, & maxheight );
9286
9287 if ( width > maxwidth ) width = maxwidth;
9288 if ( height > maxheight ) height = maxheight;
9289
9290 return wxSize( width, height );
9291 }
9292
9293 void wxGrid::Fit()
9294 {
9295 AutoSize();
9296 }
9297
9298
9299 wxPen& wxGrid::GetDividerPen() const
9300 {
9301 return wxNullPen;
9302 }
9303
9304 // ----------------------------------------------------------------------------
9305 // cell value accessor functions
9306 // ----------------------------------------------------------------------------
9307
9308 void wxGrid::SetCellValue( int row, int col, const wxString& s )
9309 {
9310 if ( m_table )
9311 {
9312 m_table->SetValue( row, col, s );
9313 if ( !GetBatchCount() )
9314 {
9315 int dummy;
9316 wxRect rect( CellToRect( row, col ) );
9317 rect.x = 0;
9318 rect.width = m_gridWin->GetClientSize().GetWidth();
9319 CalcScrolledPosition(0, rect.y, &dummy, &rect.y);
9320 m_gridWin->Refresh( FALSE, &rect );
9321 }
9322
9323 if ( m_currentCellCoords.GetRow() == row &&
9324 m_currentCellCoords.GetCol() == col &&
9325 IsCellEditControlShown())
9326 // Note: If we are using IsCellEditControlEnabled,
9327 // this interacts badly with calling SetCellValue from
9328 // an EVT_GRID_CELL_CHANGE handler.
9329 {
9330 HideCellEditControl();
9331 ShowCellEditControl(); // will reread data from table
9332 }
9333 }
9334 }
9335
9336
9337 //
9338 // ------ Block, row and col selection
9339 //
9340
9341 void wxGrid::SelectRow( int row, bool addToSelected )
9342 {
9343 if ( IsSelection() && !addToSelected )
9344 ClearSelection();
9345
9346 if ( m_selection )
9347 m_selection->SelectRow( row, FALSE, addToSelected );
9348 }
9349
9350
9351 void wxGrid::SelectCol( int col, bool addToSelected )
9352 {
9353 if ( IsSelection() && !addToSelected )
9354 ClearSelection();
9355
9356 if ( m_selection )
9357 m_selection->SelectCol( col, FALSE, addToSelected );
9358 }
9359
9360
9361 void wxGrid::SelectBlock( int topRow, int leftCol, int bottomRow, int rightCol,
9362 bool addToSelected )
9363 {
9364 if ( IsSelection() && !addToSelected )
9365 ClearSelection();
9366
9367 if ( m_selection )
9368 m_selection->SelectBlock( topRow, leftCol, bottomRow, rightCol,
9369 FALSE, addToSelected );
9370 }
9371
9372
9373 void wxGrid::SelectAll()
9374 {
9375 if ( m_numRows > 0 && m_numCols > 0 )
9376 {
9377 if ( m_selection )
9378 m_selection->SelectBlock( 0, 0, m_numRows-1, m_numCols-1 );
9379 }
9380 }
9381
9382 //
9383 // ------ Cell, row and col deselection
9384 //
9385
9386 void wxGrid::DeselectRow( int row )
9387 {
9388 if ( !m_selection )
9389 return;
9390
9391 if ( m_selection->GetSelectionMode() == wxGrid::wxGridSelectRows )
9392 {
9393 if ( m_selection->IsInSelection(row, 0 ) )
9394 m_selection->ToggleCellSelection( row, 0);
9395 }
9396 else
9397 {
9398 int nCols = GetNumberCols();
9399 for ( int i = 0; i < nCols ; i++ )
9400 {
9401 if ( m_selection->IsInSelection(row, i ) )
9402 m_selection->ToggleCellSelection( row, i);
9403 }
9404 }
9405 }
9406
9407 void wxGrid::DeselectCol( int col )
9408 {
9409 if ( !m_selection )
9410 return;
9411
9412 if ( m_selection->GetSelectionMode() == wxGrid::wxGridSelectColumns )
9413 {
9414 if ( m_selection->IsInSelection(0, col ) )
9415 m_selection->ToggleCellSelection( 0, col);
9416 }
9417 else
9418 {
9419 int nRows = GetNumberRows();
9420 for ( int i = 0; i < nRows ; i++ )
9421 {
9422 if ( m_selection->IsInSelection(i, col ) )
9423 m_selection->ToggleCellSelection(i, col);
9424 }
9425 }
9426 }
9427
9428 void wxGrid::DeselectCell( int row, int col )
9429 {
9430 if ( m_selection && m_selection->IsInSelection(row, col) )
9431 m_selection->ToggleCellSelection(row, col);
9432 }
9433
9434 bool wxGrid::IsSelection()
9435 {
9436 return ( m_selection && (m_selection->IsSelection() ||
9437 ( m_selectingTopLeft != wxGridNoCellCoords &&
9438 m_selectingBottomRight != wxGridNoCellCoords) ) );
9439 }
9440
9441 bool wxGrid::IsInSelection( int row, int col ) const
9442 {
9443 return ( m_selection && (m_selection->IsInSelection( row, col ) ||
9444 ( row >= m_selectingTopLeft.GetRow() &&
9445 col >= m_selectingTopLeft.GetCol() &&
9446 row <= m_selectingBottomRight.GetRow() &&
9447 col <= m_selectingBottomRight.GetCol() )) );
9448 }
9449
9450 wxGridCellCoordsArray wxGrid::GetSelectedCells() const
9451 {
9452 if (!m_selection) { wxGridCellCoordsArray a; return a; }
9453 return m_selection->m_cellSelection;
9454 }
9455 wxGridCellCoordsArray wxGrid::GetSelectionBlockTopLeft() const
9456 {
9457 if (!m_selection) { wxGridCellCoordsArray a; return a; }
9458 return m_selection->m_blockSelectionTopLeft;
9459 }
9460 wxGridCellCoordsArray wxGrid::GetSelectionBlockBottomRight() const
9461 {
9462 if (!m_selection) { wxGridCellCoordsArray a; return a; }
9463 return m_selection->m_blockSelectionTopLeft;
9464 }
9465 wxArrayInt wxGrid::GetSelectedRows() const
9466 {
9467 if (!m_selection) { wxArrayInt a; return a; }
9468 return m_selection->m_rowSelection;
9469 }
9470 wxArrayInt wxGrid::GetSelectedCols() const
9471 {
9472 if (!m_selection) { wxArrayInt a; return a; }
9473 return m_selection->m_colSelection;
9474 }
9475
9476
9477 void wxGrid::ClearSelection()
9478 {
9479 m_selectingTopLeft = wxGridNoCellCoords;
9480 m_selectingBottomRight = wxGridNoCellCoords;
9481 if ( m_selection )
9482 m_selection->ClearSelection();
9483 }
9484
9485
9486 // This function returns the rectangle that encloses the given block
9487 // in device coords clipped to the client size of the grid window.
9488 //
9489 wxRect wxGrid::BlockToDeviceRect( const wxGridCellCoords &topLeft,
9490 const wxGridCellCoords &bottomRight )
9491 {
9492 wxRect rect( wxGridNoCellRect );
9493 wxRect cellRect;
9494
9495 cellRect = CellToRect( topLeft );
9496 if ( cellRect != wxGridNoCellRect )
9497 {
9498 rect = cellRect;
9499 }
9500 else
9501 {
9502 rect = wxRect( 0, 0, 0, 0 );
9503 }
9504
9505 cellRect = CellToRect( bottomRight );
9506 if ( cellRect != wxGridNoCellRect )
9507 {
9508 rect += cellRect;
9509 }
9510 else
9511 {
9512 return wxGridNoCellRect;
9513 }
9514
9515 int i, j;
9516 int left = rect.GetLeft();
9517 int top = rect.GetTop();
9518 int right = rect.GetRight();
9519 int bottom = rect.GetBottom();
9520
9521 int leftCol = topLeft.GetCol();
9522 int topRow = topLeft.GetRow();
9523 int rightCol = bottomRight.GetCol();
9524 int bottomRow = bottomRight.GetRow();
9525
9526 if (left > right)
9527 {
9528 i = left;
9529 left = right;
9530 right = i;
9531 i = leftCol;
9532 leftCol=rightCol;
9533 rightCol = i;
9534 }
9535
9536 if (top > bottom)
9537 {
9538 i = top;
9539 top = bottom;
9540 bottom = i;
9541 i = topRow;
9542 topRow = bottomRow;
9543 bottomRow = i;
9544 }
9545
9546
9547 for ( j = topRow; j <= bottomRow; j++ )
9548 {
9549 for ( i = leftCol; i <= rightCol; i++ )
9550 {
9551 if ((j==topRow) || (j==bottomRow) || (i==leftCol) || (i==rightCol))
9552 {
9553 cellRect = CellToRect( j, i );
9554
9555 if (cellRect.x < left)
9556 left = cellRect.x;
9557 if (cellRect.y < top)
9558 top = cellRect.y;
9559 if (cellRect.x + cellRect.width > right)
9560 right = cellRect.x + cellRect.width;
9561 if (cellRect.y + cellRect.height > bottom)
9562 bottom = cellRect.y + cellRect.height;
9563 }
9564 else i = rightCol; // jump over inner cells.
9565 }
9566 }
9567
9568 // convert to scrolled coords
9569 //
9570 CalcScrolledPosition( left, top, &left, &top );
9571 CalcScrolledPosition( right, bottom, &right, &bottom );
9572
9573 int cw, ch;
9574 m_gridWin->GetClientSize( &cw, &ch );
9575
9576 if (right < 0 || bottom < 0 || left > cw || top > ch)
9577 return wxRect( 0, 0, 0, 0);
9578
9579 rect.SetLeft( wxMax(0, left) );
9580 rect.SetTop( wxMax(0, top) );
9581 rect.SetRight( wxMin(cw, right) );
9582 rect.SetBottom( wxMin(ch, bottom) );
9583
9584 return rect;
9585 }
9586
9587
9588
9589 //
9590 // ------ Grid event classes
9591 //
9592
9593 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent, wxNotifyEvent )
9594
9595 wxGridEvent::wxGridEvent( int id, wxEventType type, wxObject* obj,
9596 int row, int col, int x, int y, bool sel,
9597 bool control, bool shift, bool alt, bool meta )
9598 : wxNotifyEvent( type, id )
9599 {
9600 m_row = row;
9601 m_col = col;
9602 m_x = x;
9603 m_y = y;
9604 m_selecting = sel;
9605 m_control = control;
9606 m_shift = shift;
9607 m_alt = alt;
9608 m_meta = meta;
9609
9610 SetEventObject(obj);
9611 }
9612
9613
9614 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent, wxNotifyEvent )
9615
9616 wxGridSizeEvent::wxGridSizeEvent( int id, wxEventType type, wxObject* obj,
9617 int rowOrCol, int x, int y,
9618 bool control, bool shift, bool alt, bool meta )
9619 : wxNotifyEvent( type, id )
9620 {
9621 m_rowOrCol = rowOrCol;
9622 m_x = x;
9623 m_y = y;
9624 m_control = control;
9625 m_shift = shift;
9626 m_alt = alt;
9627 m_meta = meta;
9628
9629 SetEventObject(obj);
9630 }
9631
9632
9633 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent, wxNotifyEvent )
9634
9635 wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id, wxEventType type, wxObject* obj,
9636 const wxGridCellCoords& topLeft,
9637 const wxGridCellCoords& bottomRight,
9638 bool sel, bool control,
9639 bool shift, bool alt, bool meta )
9640 : wxNotifyEvent( type, id )
9641 {
9642 m_topLeft = topLeft;
9643 m_bottomRight = bottomRight;
9644 m_selecting = sel;
9645 m_control = control;
9646 m_shift = shift;
9647 m_alt = alt;
9648 m_meta = meta;
9649
9650 SetEventObject(obj);
9651 }
9652
9653
9654 IMPLEMENT_DYNAMIC_CLASS(wxGridEditorCreatedEvent, wxCommandEvent)
9655
9656 wxGridEditorCreatedEvent::wxGridEditorCreatedEvent(int id, wxEventType type,
9657 wxObject* obj, int row,
9658 int col, wxControl* ctrl)
9659 : wxCommandEvent(type, id)
9660 {
9661 SetEventObject(obj);
9662 m_row = row;
9663 m_col = col;
9664 m_ctrl = ctrl;
9665 }
9666
9667
9668 #endif // !wxUSE_NEW_GRID/wxUSE_NEW_GRID
9669
9670 #endif // wxUSE_GRID