wxGridCellRenderer/Editor made ref counted
[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:
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 precompilation, 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 !defined(wxUSE_NEW_GRID) || !(wxUSE_NEW_GRID)
34 #include "gridg.cpp"
35 #else
36
37 #ifndef WX_PRECOMP
38 #include "wx/utils.h"
39 #include "wx/dcclient.h"
40 #include "wx/settings.h"
41 #include "wx/log.h"
42 #include "wx/textctrl.h"
43 #include "wx/checkbox.h"
44 #include "wx/combobox.h"
45 #include "wx/valtext.h"
46 #endif
47
48 #include "wx/textfile.h"
49 #include "wx/spinctrl.h"
50
51 #include "wx/grid.h"
52
53 // ----------------------------------------------------------------------------
54 // array classes
55 // ----------------------------------------------------------------------------
56
57 WX_DEFINE_ARRAY(wxGridCellAttr *, wxArrayAttrs);
58
59 struct wxGridCellWithAttr
60 {
61 wxGridCellWithAttr(int row, int col, wxGridCellAttr *attr_)
62 : coords(row, col), attr(attr_)
63 {
64 }
65
66 ~wxGridCellWithAttr()
67 {
68 attr->DecRef();
69 }
70
71 wxGridCellCoords coords;
72 wxGridCellAttr *attr;
73 };
74
75 WX_DECLARE_OBJARRAY(wxGridCellWithAttr, wxGridCellWithAttrArray);
76
77 #include "wx/arrimpl.cpp"
78
79 WX_DEFINE_OBJARRAY(wxGridCellCoordsArray)
80 WX_DEFINE_OBJARRAY(wxGridCellWithAttrArray)
81
82 // ----------------------------------------------------------------------------
83 // private classes
84 // ----------------------------------------------------------------------------
85
86 class WXDLLEXPORT wxGridRowLabelWindow : public wxWindow
87 {
88 public:
89 wxGridRowLabelWindow() { m_owner = (wxGrid *)NULL; }
90 wxGridRowLabelWindow( wxGrid *parent, wxWindowID id,
91 const wxPoint &pos, const wxSize &size );
92
93 private:
94 wxGrid *m_owner;
95
96 void OnPaint( wxPaintEvent& event );
97 void OnMouseEvent( wxMouseEvent& event );
98 void OnKeyDown( wxKeyEvent& event );
99
100 DECLARE_DYNAMIC_CLASS(wxGridRowLabelWindow)
101 DECLARE_EVENT_TABLE()
102 };
103
104
105 class WXDLLEXPORT wxGridColLabelWindow : public wxWindow
106 {
107 public:
108 wxGridColLabelWindow() { m_owner = (wxGrid *)NULL; }
109 wxGridColLabelWindow( wxGrid *parent, wxWindowID id,
110 const wxPoint &pos, const wxSize &size );
111
112 private:
113 wxGrid *m_owner;
114
115 void OnPaint( wxPaintEvent &event );
116 void OnMouseEvent( wxMouseEvent& event );
117 void OnKeyDown( wxKeyEvent& event );
118
119 DECLARE_DYNAMIC_CLASS(wxGridColLabelWindow)
120 DECLARE_EVENT_TABLE()
121 };
122
123
124 class WXDLLEXPORT wxGridCornerLabelWindow : public wxWindow
125 {
126 public:
127 wxGridCornerLabelWindow() { m_owner = (wxGrid *)NULL; }
128 wxGridCornerLabelWindow( wxGrid *parent, wxWindowID id,
129 const wxPoint &pos, const wxSize &size );
130
131 private:
132 wxGrid *m_owner;
133
134 void OnMouseEvent( wxMouseEvent& event );
135 void OnKeyDown( wxKeyEvent& event );
136 void OnPaint( wxPaintEvent& event );
137
138 DECLARE_DYNAMIC_CLASS(wxGridCornerLabelWindow)
139 DECLARE_EVENT_TABLE()
140 };
141
142 class WXDLLEXPORT wxGridWindow : public wxPanel
143 {
144 public:
145 wxGridWindow()
146 {
147 m_owner = (wxGrid *)NULL;
148 m_rowLabelWin = (wxGridRowLabelWindow *)NULL;
149 m_colLabelWin = (wxGridColLabelWindow *)NULL;
150 }
151
152 wxGridWindow( wxGrid *parent,
153 wxGridRowLabelWindow *rowLblWin,
154 wxGridColLabelWindow *colLblWin,
155 wxWindowID id, const wxPoint &pos, const wxSize &size );
156 ~wxGridWindow();
157
158 void ScrollWindow( int dx, int dy, const wxRect *rect );
159
160 private:
161 wxGrid *m_owner;
162 wxGridRowLabelWindow *m_rowLabelWin;
163 wxGridColLabelWindow *m_colLabelWin;
164
165 void OnPaint( wxPaintEvent &event );
166 void OnMouseEvent( wxMouseEvent& event );
167 void OnKeyDown( wxKeyEvent& );
168 void OnEraseBackground( wxEraseEvent& );
169
170
171 DECLARE_DYNAMIC_CLASS(wxGridWindow)
172 DECLARE_EVENT_TABLE()
173 };
174
175
176
177 class wxGridCellEditorEvtHandler : public wxEvtHandler
178 {
179 public:
180 wxGridCellEditorEvtHandler()
181 : m_grid(0), m_editor(0)
182 { }
183 wxGridCellEditorEvtHandler(wxGrid* grid, wxGridCellEditor* editor)
184 : m_grid(grid), m_editor(editor)
185 { }
186
187 void OnKeyDown(wxKeyEvent& event);
188 void OnChar(wxKeyEvent& event);
189
190 private:
191 wxGrid* m_grid;
192 wxGridCellEditor* m_editor;
193 DECLARE_DYNAMIC_CLASS(wxGridCellEditorEvtHandler)
194 DECLARE_EVENT_TABLE()
195 };
196
197
198 IMPLEMENT_DYNAMIC_CLASS( wxGridCellEditorEvtHandler, wxEvtHandler )
199 BEGIN_EVENT_TABLE( wxGridCellEditorEvtHandler, wxEvtHandler )
200 EVT_KEY_DOWN( wxGridCellEditorEvtHandler::OnKeyDown )
201 EVT_CHAR( wxGridCellEditorEvtHandler::OnChar )
202 END_EVENT_TABLE()
203
204
205
206 // ----------------------------------------------------------------------------
207 // the internal data representation used by wxGridCellAttrProvider
208 // ----------------------------------------------------------------------------
209
210 // this class stores attributes set for cells
211 class WXDLLEXPORT wxGridCellAttrData
212 {
213 public:
214 void SetAttr(wxGridCellAttr *attr, int row, int col);
215 wxGridCellAttr *GetAttr(int row, int col) const;
216 void UpdateAttrRows( size_t pos, int numRows );
217 void UpdateAttrCols( size_t pos, int numCols );
218
219 private:
220 // searches for the attr for given cell, returns wxNOT_FOUND if not found
221 int FindIndex(int row, int col) const;
222
223 wxGridCellWithAttrArray m_attrs;
224 };
225
226 // this class stores attributes set for rows or columns
227 class WXDLLEXPORT wxGridRowOrColAttrData
228 {
229 public:
230 // empty ctor to suppress warnings
231 wxGridRowOrColAttrData() { }
232 ~wxGridRowOrColAttrData();
233
234 void SetAttr(wxGridCellAttr *attr, int rowOrCol);
235 wxGridCellAttr *GetAttr(int rowOrCol) const;
236 void UpdateAttrRowsOrCols( size_t pos, int numRowsOrCols );
237
238 private:
239 wxArrayInt m_rowsOrCols;
240 wxArrayAttrs m_attrs;
241 };
242
243 // NB: this is just a wrapper around 3 objects: one which stores cell
244 // attributes, and 2 others for row/col ones
245 class WXDLLEXPORT wxGridCellAttrProviderData
246 {
247 public:
248 wxGridCellAttrData m_cellAttrs;
249 wxGridRowOrColAttrData m_rowAttrs,
250 m_colAttrs;
251 };
252
253
254 // ----------------------------------------------------------------------------
255 // data structures used for the data type registry
256 // ----------------------------------------------------------------------------
257
258 struct wxGridDataTypeInfo
259 {
260 wxGridDataTypeInfo(const wxString& typeName,
261 wxGridCellRenderer* renderer,
262 wxGridCellEditor* editor)
263 : m_typeName(typeName), m_renderer(renderer), m_editor(editor)
264 { }
265
266 ~wxGridDataTypeInfo()
267 {
268 wxSafeDecRef(m_renderer);
269 wxSafeDecRef(m_editor);
270 }
271
272 wxString m_typeName;
273 wxGridCellRenderer* m_renderer;
274 wxGridCellEditor* m_editor;
275 };
276
277
278 WX_DEFINE_ARRAY(wxGridDataTypeInfo*, wxGridDataTypeInfoArray);
279
280
281 class WXDLLEXPORT wxGridTypeRegistry
282 {
283 public:
284 ~wxGridTypeRegistry();
285
286 void RegisterDataType(const wxString& typeName,
287 wxGridCellRenderer* renderer,
288 wxGridCellEditor* editor);
289 int FindDataType(const wxString& typeName);
290 wxGridCellRenderer* GetRenderer(int index);
291 wxGridCellEditor* GetEditor(int index);
292
293 private:
294 wxGridDataTypeInfoArray m_typeinfo;
295 };
296
297 // ----------------------------------------------------------------------------
298 // conditional compilation
299 // ----------------------------------------------------------------------------
300
301 #ifndef WXGRID_DRAW_LINES
302 #define WXGRID_DRAW_LINES 1
303 #endif
304
305 // ----------------------------------------------------------------------------
306 // globals
307 // ----------------------------------------------------------------------------
308
309 //#define DEBUG_ATTR_CACHE
310 #ifdef DEBUG_ATTR_CACHE
311 static size_t gs_nAttrCacheHits = 0;
312 static size_t gs_nAttrCacheMisses = 0;
313 #endif // DEBUG_ATTR_CACHE
314
315 // ----------------------------------------------------------------------------
316 // constants
317 // ----------------------------------------------------------------------------
318
319 wxGridCellCoords wxGridNoCellCoords( -1, -1 );
320 wxRect wxGridNoCellRect( -1, -1, -1, -1 );
321
322 // scroll line size
323 // TODO: fixed so far - make configurable later (and also different for x/y)
324 static const size_t GRID_SCROLL_LINE = 10;
325
326 // the size of hash tables used a bit everywhere (the max number of elements
327 // in these hash tables is the number of rows/columns)
328 static const int GRID_HASH_SIZE = 100;
329
330 // ============================================================================
331 // implementation
332 // ============================================================================
333
334 // ----------------------------------------------------------------------------
335 // wxGridCellEditor
336 // ----------------------------------------------------------------------------
337
338 wxGridCellEditor::wxGridCellEditor()
339 {
340 m_control = NULL;
341
342 m_nRef = 1;
343 }
344
345
346 wxGridCellEditor::~wxGridCellEditor()
347 {
348 Destroy();
349 }
350
351 void wxGridCellEditor::Create(wxWindow* WXUNUSED(parent),
352 wxWindowID WXUNUSED(id),
353 wxEvtHandler* evtHandler)
354 {
355 if ( evtHandler )
356 m_control->PushEventHandler(evtHandler);
357 }
358
359 void wxGridCellEditor::PaintBackground(const wxRect& rectCell,
360 wxGridCellAttr *attr)
361 {
362 // erase the background because we might not fill the cell
363 wxClientDC dc(m_control->GetParent());
364 dc.SetPen(*wxTRANSPARENT_PEN);
365 dc.SetBrush(wxBrush(attr->GetBackgroundColour(), wxSOLID));
366 dc.DrawRectangle(rectCell);
367
368 // redraw the control we just painted over
369 m_control->Refresh();
370 }
371
372 void wxGridCellEditor::Destroy()
373 {
374 if (m_control)
375 {
376 m_control->PopEventHandler(TRUE /* delete it*/);
377
378 m_control->Destroy();
379 m_control = NULL;
380 }
381 }
382
383 void wxGridCellEditor::Show(bool show, wxGridCellAttr *attr)
384 {
385 wxASSERT_MSG(m_control,
386 wxT("The wxGridCellEditor must be Created first!"));
387 m_control->Show(show);
388
389 if ( show )
390 {
391 // set the colours/fonts if we have any
392 if ( attr )
393 {
394 m_colFgOld = m_control->GetForegroundColour();
395 m_control->SetForegroundColour(attr->GetTextColour());
396
397 m_colBgOld = m_control->GetBackgroundColour();
398 m_control->SetBackgroundColour(attr->GetBackgroundColour());
399
400 m_fontOld = m_control->GetFont();
401 m_control->SetFont(attr->GetFont());
402
403 // can't do anything more in the base class version, the other
404 // attributes may only be used by the derived classes
405 }
406 }
407 else
408 {
409 // restore the standard colours fonts
410 if ( m_colFgOld.Ok() )
411 {
412 m_control->SetForegroundColour(m_colFgOld);
413 m_colFgOld = wxNullColour;
414 }
415
416 if ( m_colBgOld.Ok() )
417 {
418 m_control->SetBackgroundColour(m_colBgOld);
419 m_colBgOld = wxNullColour;
420 }
421
422 if ( m_fontOld.Ok() )
423 {
424 m_control->SetFont(m_fontOld);
425 m_fontOld = wxNullFont;
426 }
427 }
428 }
429
430 void wxGridCellEditor::SetSize(const wxRect& rect)
431 {
432 wxASSERT_MSG(m_control,
433 wxT("The wxGridCellEditor must be Created first!"));
434 m_control->SetSize(rect, wxSIZE_ALLOW_MINUS_ONE);
435 }
436
437 void wxGridCellEditor::HandleReturn(wxKeyEvent& event)
438 {
439 event.Skip();
440 }
441
442
443 void wxGridCellEditor::StartingKey(wxKeyEvent& event)
444 {
445 event.Skip();
446 }
447
448 void wxGridCellEditor::StartingClick()
449 {
450 }
451
452 // ----------------------------------------------------------------------------
453 // wxGridCellTextEditor
454 // ----------------------------------------------------------------------------
455
456 wxGridCellTextEditor::wxGridCellTextEditor()
457 {
458 }
459
460 void wxGridCellTextEditor::Create(wxWindow* parent,
461 wxWindowID id,
462 wxEvtHandler* evtHandler)
463 {
464 m_control = new wxTextCtrl(parent, id, wxEmptyString,
465 wxDefaultPosition, wxDefaultSize
466 #if defined(__WXMSW__)
467 , wxTE_MULTILINE | wxTE_NO_VSCROLL // necessary ???
468 #endif
469 );
470
471 wxGridCellEditor::Create(parent, id, evtHandler);
472 }
473
474 void wxGridCellTextEditor::PaintBackground(const wxRect& WXUNUSED(rectCell),
475 wxGridCellAttr * WXUNUSED(attr))
476 {
477 // as we fill the entire client area, don't do anything here to minimize
478 // flicker
479 }
480
481 void wxGridCellTextEditor::SetSize(const wxRect& rectOrig)
482 {
483 wxRect rect(rectOrig);
484
485 // Make the edit control large enough to allow for internal
486 // margins
487 //
488 // TODO: remove this if the text ctrl sizing is improved esp. for
489 // unix
490 //
491 #if defined(__WXGTK__)
492 if (rect.x != 0)
493 {
494 rect.x += 1;
495 rect.y += 1;
496 rect.width -= 1;
497 rect.height -= 1;
498 }
499 #else // !GTK
500 int extra_x = ( rect.x > 2 )? 2 : 1;
501 int extra_y = ( rect.y > 2 )? 2 : 1;
502 #if defined(__WXMOTIF__)
503 extra_x *= 2;
504 extra_y *= 2;
505 #endif
506 rect.SetLeft( wxMax(0, rect.x - extra_x) );
507 rect.SetTop( wxMax(0, rect.y - extra_y) );
508 rect.SetRight( rect.GetRight() + 2*extra_x );
509 rect.SetBottom( rect.GetBottom() + 2*extra_y );
510 #endif // GTK/!GTK
511
512 wxGridCellEditor::SetSize(rect);
513 }
514
515 void wxGridCellTextEditor::BeginEdit(int row, int col, wxGrid* grid)
516 {
517 wxASSERT_MSG(m_control,
518 wxT("The wxGridCellEditor must be Created first!"));
519
520 m_startValue = grid->GetTable()->GetValue(row, col);
521
522 DoBeginEdit(m_startValue);
523 }
524
525 void wxGridCellTextEditor::DoBeginEdit(const wxString& startValue)
526 {
527 Text()->SetValue(startValue);
528 Text()->SetInsertionPointEnd();
529 Text()->SetFocus();
530 }
531
532 bool wxGridCellTextEditor::EndEdit(int row, int col,
533 wxGrid* grid)
534 {
535 wxASSERT_MSG(m_control,
536 wxT("The wxGridCellEditor must be Created first!"));
537
538 bool changed = FALSE;
539 wxString value = Text()->GetValue();
540 if (value != m_startValue)
541 changed = TRUE;
542
543 if (changed)
544 grid->GetTable()->SetValue(row, col, value);
545
546 m_startValue = wxEmptyString;
547 Text()->SetValue(m_startValue);
548
549 return changed;
550 }
551
552
553 void wxGridCellTextEditor::Reset()
554 {
555 wxASSERT_MSG(m_control,
556 wxT("The wxGridCellEditor must be Created first!"));
557
558 DoReset(m_startValue);
559 }
560
561 void wxGridCellTextEditor::DoReset(const wxString& startValue)
562 {
563 Text()->SetValue(startValue);
564 Text()->SetInsertionPointEnd();
565 }
566
567 void wxGridCellTextEditor::StartingKey(wxKeyEvent& event)
568 {
569 if ( !event.AltDown() && !event.MetaDown() && !event.ControlDown() )
570 {
571 // insert the key in the control
572 long keycode = event.KeyCode();
573 if ( isprint(keycode) )
574 {
575 // FIXME this is not going to work for non letters...
576 if ( !event.ShiftDown() )
577 {
578 keycode = tolower(keycode);
579 }
580
581 Text()->AppendText((wxChar)keycode);
582
583 return;
584 }
585
586 }
587
588 event.Skip();
589 }
590
591 void wxGridCellTextEditor::HandleReturn(wxKeyEvent& event)
592 {
593 #if defined(__WXMOTIF__) || defined(__WXGTK__)
594 // wxMotif needs a little extra help...
595 int pos = Text()->GetInsertionPoint();
596 wxString s( Text()->GetValue() );
597 s = s.Left(pos) + "\n" + s.Mid(pos);
598 Text()->SetValue(s);
599 Text()->SetInsertionPoint( pos );
600 #else
601 // the other ports can handle a Return key press
602 //
603 event.Skip();
604 #endif
605 }
606
607 // ----------------------------------------------------------------------------
608 // wxGridCellNumberEditor
609 // ----------------------------------------------------------------------------
610
611 wxGridCellNumberEditor::wxGridCellNumberEditor(int min, int max)
612 {
613 m_min = min;
614 m_max = max;
615 }
616
617 void wxGridCellNumberEditor::Create(wxWindow* parent,
618 wxWindowID id,
619 wxEvtHandler* evtHandler)
620 {
621 if ( HasRange() )
622 {
623 // create a spin ctrl
624 m_control = new wxSpinCtrl(parent, -1, wxEmptyString,
625 wxDefaultPosition, wxDefaultSize,
626 wxSP_ARROW_KEYS,
627 m_min, m_max);
628
629 wxGridCellEditor::Create(parent, id, evtHandler);
630 }
631 else
632 {
633 // just a text control
634 wxGridCellTextEditor::Create(parent, id, evtHandler);
635
636 #if wxUSE_VALIDATORS
637 Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC));
638 #endif // wxUSE_VALIDATORS
639 }
640 }
641
642 void wxGridCellNumberEditor::BeginEdit(int row, int col, wxGrid* grid)
643 {
644 // first get the value
645 wxGridTableBase *table = grid->GetTable();
646 if ( table->CanGetValueAs(row, col, wxGRID_VALUE_NUMBER) )
647 {
648 m_valueOld = table->GetValueAsLong(row, col);
649 }
650 else
651 {
652 wxString sValue = table->GetValue(row, col);
653 if (! sValue.ToLong(&m_valueOld))
654 {
655 wxFAIL_MSG( _T("this cell doesn't have numeric value") );
656 return;
657 }
658 }
659
660 if ( HasRange() )
661 {
662 Spin()->SetValue(m_valueOld);
663 }
664 else
665 {
666 DoBeginEdit(GetString());
667 }
668 }
669
670 bool wxGridCellNumberEditor::EndEdit(int row, int col,
671 wxGrid* grid)
672 {
673 bool changed;
674 long value;
675
676 if ( HasRange() )
677 {
678 value = Spin()->GetValue();
679 changed = value != m_valueOld;
680 }
681 else
682 {
683 changed = Text()->GetValue().ToLong(&value) && (value != m_valueOld);
684 }
685
686 if ( changed )
687 {
688 if (grid->GetTable()->CanSetValueAs(row, col, wxGRID_VALUE_NUMBER))
689 grid->GetTable()->SetValueAsLong(row, col, value);
690 else
691 grid->GetTable()->SetValue(row, col, wxString::Format("%ld", value));
692 }
693
694 return changed;
695 }
696
697 void wxGridCellNumberEditor::Reset()
698 {
699 if ( HasRange() )
700 {
701 Spin()->SetValue(m_valueOld);
702 }
703 else
704 {
705 DoReset(GetString());
706 }
707 }
708
709 void wxGridCellNumberEditor::StartingKey(wxKeyEvent& event)
710 {
711 if ( !HasRange() )
712 {
713 long keycode = event.KeyCode();
714 if ( isdigit(keycode) || keycode == '+' || keycode == '-' )
715 {
716 wxGridCellTextEditor::StartingKey(event);
717
718 // skip Skip() below
719 return;
720 }
721 }
722
723 event.Skip();
724 }
725
726 // ----------------------------------------------------------------------------
727 // wxGridCellFloatEditor
728 // ----------------------------------------------------------------------------
729
730 void wxGridCellFloatEditor::Create(wxWindow* parent,
731 wxWindowID id,
732 wxEvtHandler* evtHandler)
733 {
734 wxGridCellTextEditor::Create(parent, id, evtHandler);
735
736 #if wxUSE_VALIDATORS
737 Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC));
738 #endif // wxUSE_VALIDATORS
739 }
740
741 void wxGridCellFloatEditor::BeginEdit(int row, int col, wxGrid* grid)
742 {
743 // first get the value
744 wxGridTableBase *table = grid->GetTable();
745 if ( table->CanGetValueAs(row, col, wxGRID_VALUE_FLOAT) )
746 {
747 m_valueOld = table->GetValueAsDouble(row, col);
748 }
749 else
750 {
751 wxString sValue = table->GetValue(row, col);
752 if (! sValue.ToDouble(&m_valueOld))
753 {
754 wxFAIL_MSG( _T("this cell doesn't have float value") );
755 return;
756 }
757 }
758
759 DoBeginEdit(GetString());
760 }
761
762 bool wxGridCellFloatEditor::EndEdit(int row, int col,
763 wxGrid* grid)
764 {
765 double value;
766 if ( Text()->GetValue().ToDouble(&value) && (value != m_valueOld) )
767 {
768 if (grid->GetTable()->CanSetValueAs(row, col, wxGRID_VALUE_FLOAT))
769 grid->GetTable()->SetValueAsDouble(row, col, value);
770 else
771 grid->GetTable()->SetValue(row, col, wxString::Format("%f", value));
772
773 return TRUE;
774 }
775 else
776 {
777 return FALSE;
778 }
779 }
780
781 void wxGridCellFloatEditor::Reset()
782 {
783 DoReset(GetString());
784 }
785
786 void wxGridCellFloatEditor::StartingKey(wxKeyEvent& event)
787 {
788 long keycode = event.KeyCode();
789 if ( isdigit(keycode) ||
790 keycode == '+' || keycode == '-' || keycode == '.' )
791 {
792 wxGridCellTextEditor::StartingKey(event);
793
794 // skip Skip() below
795 return;
796 }
797
798 event.Skip();
799 }
800
801 // ----------------------------------------------------------------------------
802 // wxGridCellBoolEditor
803 // ----------------------------------------------------------------------------
804
805 void wxGridCellBoolEditor::Create(wxWindow* parent,
806 wxWindowID id,
807 wxEvtHandler* evtHandler)
808 {
809 m_control = new wxCheckBox(parent, id, wxEmptyString,
810 wxDefaultPosition, wxDefaultSize,
811 wxNO_BORDER);
812
813 wxGridCellEditor::Create(parent, id, evtHandler);
814 }
815
816 void wxGridCellBoolEditor::SetSize(const wxRect& r)
817 {
818 bool resize = FALSE;
819 wxSize size = m_control->GetSize();
820 wxCoord minSize = wxMin(r.width, r.height);
821
822 // check if the checkbox is not too big/small for this cell
823 wxSize sizeBest = m_control->GetBestSize();
824 if ( !(size == sizeBest) )
825 {
826 // reset to default size if it had been made smaller
827 size = sizeBest;
828
829 resize = TRUE;
830 }
831
832 if ( size.x >= minSize || size.y >= minSize )
833 {
834 // leave 1 pixel margin
835 size.x = size.y = minSize - 2;
836
837 resize = TRUE;
838 }
839
840 if ( resize )
841 {
842 m_control->SetSize(size);
843 }
844
845 // position it in the centre of the rectangle (TODO: support alignment?)
846
847 #if defined(__WXGTK__) || defined (__WXMOTIF__)
848 // the checkbox without label still has some space to the right in wxGTK,
849 // so shift it to the right
850 size.x -= 8;
851 #elif defined(__WXMSW__)
852 // here too...
853 size.x -= 6;
854 size.y -= 2;
855 #endif
856
857 m_control->Move(r.x + r.width/2 - size.x/2, r.y + r.height/2 - size.y/2);
858 }
859
860 void wxGridCellBoolEditor::Show(bool show, wxGridCellAttr *attr)
861 {
862 m_control->Show(show);
863
864 if ( show )
865 {
866 wxColour colBg = attr ? attr->GetBackgroundColour() : *wxLIGHT_GREY;
867 CBox()->SetBackgroundColour(colBg);
868 }
869 }
870
871 void wxGridCellBoolEditor::BeginEdit(int row, int col, wxGrid* grid)
872 {
873 wxASSERT_MSG(m_control,
874 wxT("The wxGridCellEditor must be Created first!"));
875
876 if (grid->GetTable()->CanGetValueAs(row, col, wxGRID_VALUE_BOOL))
877 m_startValue = grid->GetTable()->GetValueAsBool(row, col);
878 else
879 m_startValue = !!grid->GetTable()->GetValue(row, col);
880 CBox()->SetValue(m_startValue);
881 CBox()->SetFocus();
882 }
883
884 bool wxGridCellBoolEditor::EndEdit(int row, int col,
885 wxGrid* grid)
886 {
887 wxASSERT_MSG(m_control,
888 wxT("The wxGridCellEditor must be Created first!"));
889
890 bool changed = FALSE;
891 bool value = CBox()->GetValue();
892 if ( value != m_startValue )
893 changed = TRUE;
894
895 if ( changed )
896 {
897 if (grid->GetTable()->CanGetValueAs(row, col, wxGRID_VALUE_BOOL))
898 grid->GetTable()->SetValueAsBool(row, col, value);
899 else
900 grid->GetTable()->SetValue(row, col, value ? _T("1") : wxEmptyString);
901 }
902
903 return changed;
904 }
905
906 void wxGridCellBoolEditor::Reset()
907 {
908 wxASSERT_MSG(m_control,
909 wxT("The wxGridCellEditor must be Created first!"));
910
911 CBox()->SetValue(m_startValue);
912 }
913
914 void wxGridCellBoolEditor::StartingClick()
915 {
916 CBox()->SetValue(!CBox()->GetValue());
917 }
918
919 // ----------------------------------------------------------------------------
920 // wxGridCellChoiceEditor
921 // ----------------------------------------------------------------------------
922
923 wxGridCellChoiceEditor::wxGridCellChoiceEditor(size_t count,
924 const wxChar* choices[],
925 bool allowOthers)
926 : m_allowOthers(allowOthers)
927 {
928 m_choices.Alloc(count);
929 for ( size_t n = 0; n < count; n++ )
930 {
931 m_choices.Add(choices[n]);
932 }
933 }
934
935 void wxGridCellChoiceEditor::Create(wxWindow* parent,
936 wxWindowID id,
937 wxEvtHandler* evtHandler)
938 {
939 size_t count = m_choices.GetCount();
940 wxString *choices = new wxString[count];
941 for ( size_t n = 0; n < count; n++ )
942 {
943 choices[n] = m_choices[n];
944 }
945
946 m_control = new wxComboBox(parent, id, wxEmptyString,
947 wxDefaultPosition, wxDefaultSize,
948 count, choices,
949 m_allowOthers ? 0 : wxCB_READONLY);
950
951 delete [] choices;
952
953 wxGridCellEditor::Create(parent, id, evtHandler);
954 }
955
956 void wxGridCellChoiceEditor::PaintBackground(const wxRect& rectCell,
957 wxGridCellAttr * attr)
958 {
959 // as we fill the entire client area, don't do anything here to minimize
960 // flicker
961
962 // TODO: It doesn't actually fill the client area since the height of a
963 // combo always defaults to the standard... Until someone has time to
964 // figure out the right rectangle to paint, just do it the normal way...
965 wxGridCellEditor::PaintBackground(rectCell, attr);
966 }
967
968 void wxGridCellChoiceEditor::BeginEdit(int row, int col, wxGrid* grid)
969 {
970 wxASSERT_MSG(m_control,
971 wxT("The wxGridCellEditor must be Created first!"));
972
973 m_startValue = grid->GetTable()->GetValue(row, col);
974
975 Combo()->SetValue(m_startValue);
976 size_t count = m_choices.GetCount();
977 for (size_t i=0; i<count; i++)
978 {
979 if (m_startValue == m_choices[i])
980 {
981 Combo()->SetSelection(i);
982 break;
983 }
984 }
985 Combo()->SetInsertionPointEnd();
986 Combo()->SetFocus();
987 }
988
989 bool wxGridCellChoiceEditor::EndEdit(int row, int col,
990 wxGrid* grid)
991 {
992 wxString value = Combo()->GetValue();
993 bool changed = value != m_startValue;
994
995 if ( changed )
996 grid->GetTable()->SetValue(row, col, value);
997
998 m_startValue = wxEmptyString;
999 Combo()->SetValue(m_startValue);
1000
1001 return changed;
1002 }
1003
1004 void wxGridCellChoiceEditor::Reset()
1005 {
1006 Combo()->SetValue(m_startValue);
1007 Combo()->SetInsertionPointEnd();
1008 }
1009
1010 // ----------------------------------------------------------------------------
1011 // wxGridCellEditorEvtHandler
1012 // ----------------------------------------------------------------------------
1013
1014 void wxGridCellEditorEvtHandler::OnKeyDown(wxKeyEvent& event)
1015 {
1016 switch ( event.KeyCode() )
1017 {
1018 case WXK_ESCAPE:
1019 m_editor->Reset();
1020 m_grid->DisableCellEditControl();
1021 break;
1022
1023 case WXK_TAB:
1024 event.Skip( m_grid->ProcessEvent( event ) );
1025 break;
1026
1027 case WXK_RETURN:
1028 if (!m_grid->ProcessEvent(event))
1029 m_editor->HandleReturn(event);
1030 break;
1031
1032
1033 default:
1034 event.Skip();
1035 }
1036 }
1037
1038 void wxGridCellEditorEvtHandler::OnChar(wxKeyEvent& event)
1039 {
1040 switch ( event.KeyCode() )
1041 {
1042 case WXK_ESCAPE:
1043 case WXK_TAB:
1044 case WXK_RETURN:
1045 break;
1046
1047 default:
1048 event.Skip();
1049 }
1050 }
1051
1052 // ============================================================================
1053 // renderer classes
1054 // ============================================================================
1055
1056 // ----------------------------------------------------------------------------
1057 // wxGridCellRenderer
1058 // ----------------------------------------------------------------------------
1059
1060 void wxGridCellRenderer::Draw(wxGrid& grid,
1061 wxGridCellAttr& attr,
1062 wxDC& dc,
1063 const wxRect& rect,
1064 int row, int col,
1065 bool isSelected)
1066 {
1067 dc.SetBackgroundMode( wxSOLID );
1068
1069 if ( isSelected )
1070 {
1071 dc.SetBrush( wxBrush(grid.GetSelectionBackground(), wxSOLID) );
1072 }
1073 else
1074 {
1075 dc.SetBrush( wxBrush(attr.GetBackgroundColour(), wxSOLID) );
1076 }
1077
1078 dc.SetPen( *wxTRANSPARENT_PEN );
1079 dc.DrawRectangle(rect);
1080 }
1081
1082 wxGridCellRenderer::~wxGridCellRenderer()
1083 {
1084 }
1085
1086 // ----------------------------------------------------------------------------
1087 // wxGridCellStringRenderer
1088 // ----------------------------------------------------------------------------
1089
1090 void wxGridCellStringRenderer::SetTextColoursAndFont(wxGrid& grid,
1091 wxGridCellAttr& attr,
1092 wxDC& dc,
1093 bool isSelected)
1094 {
1095 dc.SetBackgroundMode( wxTRANSPARENT );
1096
1097 // TODO some special colours for attr.IsReadOnly() case?
1098
1099 if ( isSelected )
1100 {
1101 dc.SetTextBackground( grid.GetSelectionBackground() );
1102 dc.SetTextForeground( grid.GetSelectionForeground() );
1103 }
1104 else
1105 {
1106 dc.SetTextBackground( attr.GetBackgroundColour() );
1107 dc.SetTextForeground( attr.GetTextColour() );
1108 }
1109
1110 dc.SetFont( attr.GetFont() );
1111 }
1112
1113 wxSize wxGridCellStringRenderer::DoGetBestSize(wxGridCellAttr& attr,
1114 wxDC& dc,
1115 const wxString& text)
1116 {
1117 wxCoord x, y;
1118 dc.SetFont(attr.GetFont());
1119 dc.GetTextExtent(text, &x, &y);
1120
1121 return wxSize(x, y);
1122 }
1123
1124 wxSize wxGridCellStringRenderer::GetBestSize(wxGrid& grid,
1125 wxGridCellAttr& attr,
1126 wxDC& dc,
1127 int row, int col)
1128 {
1129 return DoGetBestSize(attr, dc, grid.GetCellValue(row, col));
1130 }
1131
1132 void wxGridCellStringRenderer::Draw(wxGrid& grid,
1133 wxGridCellAttr& attr,
1134 wxDC& dc,
1135 const wxRect& rectCell,
1136 int row, int col,
1137 bool isSelected)
1138 {
1139 wxGridCellRenderer::Draw(grid, attr, dc, rectCell, row, col, isSelected);
1140
1141 // now we only have to draw the text
1142 SetTextColoursAndFont(grid, attr, dc, isSelected);
1143
1144 int hAlign, vAlign;
1145 attr.GetAlignment(&hAlign, &vAlign);
1146
1147 wxRect rect = rectCell;
1148 rect.Inflate(-1);
1149
1150 grid.DrawTextRectangle(dc, grid.GetCellValue(row, col),
1151 rect, hAlign, vAlign);
1152 }
1153
1154 // ----------------------------------------------------------------------------
1155 // wxGridCellNumberRenderer
1156 // ----------------------------------------------------------------------------
1157
1158 wxString wxGridCellNumberRenderer::GetString(wxGrid& grid, int row, int col)
1159 {
1160 wxGridTableBase *table = grid.GetTable();
1161 wxString text;
1162 if ( table->CanGetValueAs(row, col, wxGRID_VALUE_NUMBER) )
1163 {
1164 text.Printf(_T("%ld"), table->GetValueAsLong(row, col));
1165 }
1166 else
1167 {
1168 text = table->GetValue(row, col);
1169 }
1170
1171 return text;
1172 }
1173
1174 void wxGridCellNumberRenderer::Draw(wxGrid& grid,
1175 wxGridCellAttr& attr,
1176 wxDC& dc,
1177 const wxRect& rectCell,
1178 int row, int col,
1179 bool isSelected)
1180 {
1181 wxGridCellRenderer::Draw(grid, attr, dc, rectCell, row, col, isSelected);
1182
1183 SetTextColoursAndFont(grid, attr, dc, isSelected);
1184
1185 // draw the text right aligned by default
1186 int hAlign, vAlign;
1187 attr.GetAlignment(&hAlign, &vAlign);
1188 hAlign = wxRIGHT;
1189
1190 wxRect rect = rectCell;
1191 rect.Inflate(-1);
1192
1193 grid.DrawTextRectangle(dc, GetString(grid, row, col), rect, hAlign, vAlign);
1194 }
1195
1196 wxSize wxGridCellNumberRenderer::GetBestSize(wxGrid& grid,
1197 wxGridCellAttr& attr,
1198 wxDC& dc,
1199 int row, int col)
1200 {
1201 return DoGetBestSize(attr, dc, GetString(grid, row, col));
1202 }
1203
1204 // ----------------------------------------------------------------------------
1205 // wxGridCellFloatRenderer
1206 // ----------------------------------------------------------------------------
1207
1208 wxGridCellFloatRenderer::wxGridCellFloatRenderer(int width, int precision)
1209 {
1210 SetWidth(width);
1211 SetPrecision(precision);
1212 }
1213
1214 wxString wxGridCellFloatRenderer::GetString(wxGrid& grid, int row, int col)
1215 {
1216 wxGridTableBase *table = grid.GetTable();
1217 wxString text;
1218 if ( table->CanGetValueAs(row, col, wxGRID_VALUE_FLOAT) )
1219 {
1220 if ( !m_format )
1221 {
1222 m_format.Printf(_T("%%%d.%d%%f"), m_width, m_precision);
1223 }
1224
1225 text.Printf(m_format, table->GetValueAsDouble(row, col));
1226 }
1227 else
1228 {
1229 text = table->GetValue(row, col);
1230 }
1231
1232 return text;
1233 }
1234
1235 void wxGridCellFloatRenderer::Draw(wxGrid& grid,
1236 wxGridCellAttr& attr,
1237 wxDC& dc,
1238 const wxRect& rectCell,
1239 int row, int col,
1240 bool isSelected)
1241 {
1242 wxGridCellRenderer::Draw(grid, attr, dc, rectCell, row, col, isSelected);
1243
1244 SetTextColoursAndFont(grid, attr, dc, isSelected);
1245
1246 // draw the text right aligned by default
1247 int hAlign, vAlign;
1248 attr.GetAlignment(&hAlign, &vAlign);
1249 hAlign = wxRIGHT;
1250
1251 wxRect rect = rectCell;
1252 rect.Inflate(-1);
1253
1254 grid.DrawTextRectangle(dc, GetString(grid, row, col), rect, hAlign, vAlign);
1255 }
1256
1257 wxSize wxGridCellFloatRenderer::GetBestSize(wxGrid& grid,
1258 wxGridCellAttr& attr,
1259 wxDC& dc,
1260 int row, int col)
1261 {
1262 return DoGetBestSize(attr, dc, GetString(grid, row, col));
1263 }
1264
1265 // ----------------------------------------------------------------------------
1266 // wxGridCellBoolRenderer
1267 // ----------------------------------------------------------------------------
1268
1269 wxSize wxGridCellBoolRenderer::ms_sizeCheckMark;
1270
1271 // FIXME these checkbox size calculations are really ugly...
1272
1273 // between checkmark and box
1274 #ifdef __WXGTK__
1275 static const wxCoord wxGRID_CHECKMARK_MARGIN = 4;
1276 #else
1277 static const wxCoord wxGRID_CHECKMARK_MARGIN = 2;
1278 #endif
1279
1280 wxSize wxGridCellBoolRenderer::GetBestSize(wxGrid& grid,
1281 wxGridCellAttr& WXUNUSED(attr),
1282 wxDC& WXUNUSED(dc),
1283 int WXUNUSED(row),
1284 int WXUNUSED(col))
1285 {
1286 // compute it only once (no locks for MT safeness in GUI thread...)
1287 if ( !ms_sizeCheckMark.x )
1288 {
1289 // get checkbox size
1290 wxCoord checkSize = 0;
1291 wxCheckBox *checkbox = new wxCheckBox(&grid, -1, wxEmptyString);
1292 wxSize size = checkbox->GetBestSize();
1293 checkSize = size.y + wxGRID_CHECKMARK_MARGIN;
1294
1295 // FIXME wxGTK::wxCheckBox::GetBestSize() gives "wrong" result
1296 #if defined(__WXGTK__) || defined(__WXMOTIF__)
1297 checkSize -= size.y / 2;
1298 #endif
1299
1300 delete checkbox;
1301
1302 ms_sizeCheckMark.x = ms_sizeCheckMark.y = checkSize;
1303 }
1304
1305 return ms_sizeCheckMark;
1306 }
1307
1308 void wxGridCellBoolRenderer::Draw(wxGrid& grid,
1309 wxGridCellAttr& attr,
1310 wxDC& dc,
1311 const wxRect& rect,
1312 int row, int col,
1313 bool isSelected)
1314 {
1315 wxGridCellRenderer::Draw(grid, attr, dc, rect, row, col, isSelected);
1316
1317 // draw a check mark in the centre (ignoring alignment - TODO)
1318 wxSize size = GetBestSize(grid, attr, dc, row, col);
1319
1320 // don't draw outside the cell
1321 wxCoord minSize = wxMin(rect.width, rect.height);
1322 if ( size.x >= minSize || size.y >= minSize )
1323 {
1324 // and even leave (at least) 1 pixel margin
1325 size.x = size.y = minSize - 2;
1326 }
1327
1328 // draw a border around checkmark
1329 wxRect rectMark;
1330 rectMark.x = rect.x + rect.width/2 - size.x/2;
1331 rectMark.y = rect.y + rect.height/2 - size.y/2;
1332 rectMark.width = size.x;
1333 rectMark.height = size.y;
1334
1335 dc.SetBrush(*wxTRANSPARENT_BRUSH);
1336 dc.SetPen(wxPen(attr.GetTextColour(), 1, wxSOLID));
1337 dc.DrawRectangle(rectMark);
1338
1339 rectMark.Inflate(-wxGRID_CHECKMARK_MARGIN);
1340
1341 #ifdef __WXMSW__
1342 // looks nicer under MSW
1343 rectMark.x++;
1344 #endif // MSW
1345
1346 bool value;
1347 if ( grid.GetTable()->CanGetValueAs(row, col, wxGRID_VALUE_BOOL) )
1348 value = grid.GetTable()->GetValueAsBool(row, col);
1349 else
1350 value = !!grid.GetTable()->GetValue(row, col);
1351
1352 if ( value )
1353 {
1354 dc.SetTextForeground(attr.GetTextColour());
1355 dc.DrawCheckMark(rectMark);
1356 }
1357 }
1358
1359 // ----------------------------------------------------------------------------
1360 // wxGridCellAttr
1361 // ----------------------------------------------------------------------------
1362
1363 wxGridCellAttr *wxGridCellAttr::Clone() const
1364 {
1365 wxGridCellAttr *attr = new wxGridCellAttr;
1366 if ( HasTextColour() )
1367 attr->SetTextColour(GetTextColour());
1368 if ( HasBackgroundColour() )
1369 attr->SetBackgroundColour(GetBackgroundColour());
1370 if ( HasFont() )
1371 attr->SetFont(GetFont());
1372 if ( HasAlignment() )
1373 attr->SetAlignment(m_hAlign, m_vAlign);
1374
1375 if ( m_renderer )
1376 {
1377 attr->SetRenderer(m_renderer);
1378 m_renderer->IncRef();
1379 }
1380 if ( m_editor )
1381 {
1382 attr->SetEditor(m_editor);
1383 m_editor->IncRef();
1384 }
1385
1386 if ( IsReadOnly() )
1387 attr->SetReadOnly();
1388
1389 attr->SetDefAttr(m_defGridAttr);
1390
1391 return attr;
1392 }
1393
1394 const wxColour& wxGridCellAttr::GetTextColour() const
1395 {
1396 if (HasTextColour())
1397 {
1398 return m_colText;
1399 }
1400 else if (m_defGridAttr != this)
1401 {
1402 return m_defGridAttr->GetTextColour();
1403 }
1404 else
1405 {
1406 wxFAIL_MSG(wxT("Missing default cell attribute"));
1407 return wxNullColour;
1408 }
1409 }
1410
1411
1412 const wxColour& wxGridCellAttr::GetBackgroundColour() const
1413 {
1414 if (HasBackgroundColour())
1415 return m_colBack;
1416 else if (m_defGridAttr != this)
1417 return m_defGridAttr->GetBackgroundColour();
1418 else
1419 {
1420 wxFAIL_MSG(wxT("Missing default cell attribute"));
1421 return wxNullColour;
1422 }
1423 }
1424
1425
1426 const wxFont& wxGridCellAttr::GetFont() const
1427 {
1428 if (HasFont())
1429 return m_font;
1430 else if (m_defGridAttr != this)
1431 return m_defGridAttr->GetFont();
1432 else
1433 {
1434 wxFAIL_MSG(wxT("Missing default cell attribute"));
1435 return wxNullFont;
1436 }
1437 }
1438
1439
1440 void wxGridCellAttr::GetAlignment(int *hAlign, int *vAlign) const
1441 {
1442 if (HasAlignment())
1443 {
1444 if ( hAlign ) *hAlign = m_hAlign;
1445 if ( vAlign ) *vAlign = m_vAlign;
1446 }
1447 else if (m_defGridAttr != this)
1448 m_defGridAttr->GetAlignment(hAlign, vAlign);
1449 else
1450 {
1451 wxFAIL_MSG(wxT("Missing default cell attribute"));
1452 }
1453 }
1454
1455
1456 // GetRenderer and GetEditor use a slightly different decision path about
1457 // which attribute to use. If a non-default attr object has one then it is
1458 // used, otherwise the default editor or renderer is fetched from the grid and
1459 // used. It should be the default for the data type of the cell. If it is
1460 // NULL (because the table has a type that the grid does not have in its
1461 // registry,) then the grid's default editor or renderer is used.
1462
1463 wxGridCellRenderer* wxGridCellAttr::GetRenderer(wxGrid* grid, int row, int col) const
1464 {
1465 if ((m_defGridAttr != this || grid == NULL) && HasRenderer())
1466 return m_renderer; // use local attribute
1467
1468 wxGridCellRenderer* renderer = NULL;
1469 if (grid) // get renderer for the data type
1470 renderer = grid->GetDefaultRendererForCell(row, col);
1471
1472 if (! renderer)
1473 // if we still don't have one then use the grid default
1474 renderer = m_defGridAttr->GetRenderer(NULL,0,0);
1475
1476 if (! renderer)
1477 wxFAIL_MSG(wxT("Missing default cell attribute"));
1478
1479 return renderer;
1480 }
1481
1482 wxGridCellEditor* wxGridCellAttr::GetEditor(wxGrid* grid, int row, int col) const
1483 {
1484 if ((m_defGridAttr != this || grid == NULL) && HasEditor())
1485 return m_editor; // use local attribute
1486
1487 wxGridCellEditor* editor = NULL;
1488 if (grid) // get renderer for the data type
1489 editor = grid->GetDefaultEditorForCell(row, col);
1490
1491 if (! editor)
1492 // if we still don't have one then use the grid default
1493 editor = m_defGridAttr->GetEditor(NULL,0,0);
1494
1495 if (! editor)
1496 wxFAIL_MSG(wxT("Missing default cell attribute"));
1497
1498 return editor;
1499 }
1500
1501 // ----------------------------------------------------------------------------
1502 // wxGridCellAttrData
1503 // ----------------------------------------------------------------------------
1504
1505 void wxGridCellAttrData::SetAttr(wxGridCellAttr *attr, int row, int col)
1506 {
1507 int n = FindIndex(row, col);
1508 if ( n == wxNOT_FOUND )
1509 {
1510 // add the attribute
1511 m_attrs.Add(new wxGridCellWithAttr(row, col, attr));
1512 }
1513 else
1514 {
1515 if ( attr )
1516 {
1517 // change the attribute
1518 m_attrs[(size_t)n].attr = attr;
1519 }
1520 else
1521 {
1522 // remove this attribute
1523 m_attrs.RemoveAt((size_t)n);
1524 }
1525 }
1526 }
1527
1528 wxGridCellAttr *wxGridCellAttrData::GetAttr(int row, int col) const
1529 {
1530 wxGridCellAttr *attr = (wxGridCellAttr *)NULL;
1531
1532 int n = FindIndex(row, col);
1533 if ( n != wxNOT_FOUND )
1534 {
1535 attr = m_attrs[(size_t)n].attr;
1536 attr->IncRef();
1537 }
1538
1539 return attr;
1540 }
1541
1542 void wxGridCellAttrData::UpdateAttrRows( size_t pos, int numRows )
1543 {
1544 size_t count = m_attrs.GetCount();
1545 for ( size_t n = 0; n < count; n++ )
1546 {
1547 wxGridCellCoords& coords = m_attrs[n].coords;
1548 wxCoord row = coords.GetRow();
1549 if ((size_t)row >= pos)
1550 {
1551 if (numRows > 0)
1552 {
1553 // If rows inserted, include row counter where necessary
1554 coords.SetRow(row + numRows);
1555 }
1556 else if (numRows < 0)
1557 {
1558 // If rows deleted ...
1559 if ((size_t)row >= pos - numRows)
1560 {
1561 // ...either decrement row counter (if row still exists)...
1562 coords.SetRow(row + numRows);
1563 }
1564 else
1565 {
1566 // ...or remove the attribute
1567 m_attrs.RemoveAt((size_t)n);
1568 n--; count--;
1569 }
1570 }
1571 }
1572 }
1573 }
1574
1575 void wxGridCellAttrData::UpdateAttrCols( size_t pos, int numCols )
1576 {
1577 size_t count = m_attrs.GetCount();
1578 for ( size_t n = 0; n < count; n++ )
1579 {
1580 wxGridCellCoords& coords = m_attrs[n].coords;
1581 wxCoord col = coords.GetCol();
1582 if ( (size_t)col >= pos )
1583 {
1584 if ( numCols > 0 )
1585 {
1586 // If rows inserted, include row counter where necessary
1587 coords.SetCol(col + numCols);
1588 }
1589 else if (numCols < 0)
1590 {
1591 // If rows deleted ...
1592 if ((size_t)col >= pos - numCols)
1593 {
1594 // ...either decrement row counter (if row still exists)...
1595 coords.SetCol(col + numCols);
1596 }
1597 else
1598 {
1599 // ...or remove the attribute
1600 m_attrs.RemoveAt((size_t)n);
1601 n--; count--;
1602 }
1603 }
1604 }
1605 }
1606 }
1607
1608 int wxGridCellAttrData::FindIndex(int row, int col) const
1609 {
1610 size_t count = m_attrs.GetCount();
1611 for ( size_t n = 0; n < count; n++ )
1612 {
1613 const wxGridCellCoords& coords = m_attrs[n].coords;
1614 if ( (coords.GetRow() == row) && (coords.GetCol() == col) )
1615 {
1616 return n;
1617 }
1618 }
1619
1620 return wxNOT_FOUND;
1621 }
1622
1623 // ----------------------------------------------------------------------------
1624 // wxGridRowOrColAttrData
1625 // ----------------------------------------------------------------------------
1626
1627 wxGridRowOrColAttrData::~wxGridRowOrColAttrData()
1628 {
1629 size_t count = m_attrs.Count();
1630 for ( size_t n = 0; n < count; n++ )
1631 {
1632 m_attrs[n]->DecRef();
1633 }
1634 }
1635
1636 wxGridCellAttr *wxGridRowOrColAttrData::GetAttr(int rowOrCol) const
1637 {
1638 wxGridCellAttr *attr = (wxGridCellAttr *)NULL;
1639
1640 int n = m_rowsOrCols.Index(rowOrCol);
1641 if ( n != wxNOT_FOUND )
1642 {
1643 attr = m_attrs[(size_t)n];
1644 attr->IncRef();
1645 }
1646
1647 return attr;
1648 }
1649
1650 void wxGridRowOrColAttrData::SetAttr(wxGridCellAttr *attr, int rowOrCol)
1651 {
1652 int n = m_rowsOrCols.Index(rowOrCol);
1653 if ( n == wxNOT_FOUND )
1654 {
1655 // add the attribute
1656 m_rowsOrCols.Add(rowOrCol);
1657 m_attrs.Add(attr);
1658 }
1659 else
1660 {
1661 if ( attr )
1662 {
1663 // change the attribute
1664 m_attrs[(size_t)n] = attr;
1665 }
1666 else
1667 {
1668 // remove this attribute
1669 m_attrs[(size_t)n]->DecRef();
1670 m_rowsOrCols.RemoveAt((size_t)n);
1671 m_attrs.RemoveAt((size_t)n);
1672 }
1673 }
1674 }
1675
1676 void wxGridRowOrColAttrData::UpdateAttrRowsOrCols( size_t pos, int numRowsOrCols )
1677 {
1678 size_t count = m_attrs.GetCount();
1679 for ( size_t n = 0; n < count; n++ )
1680 {
1681 int & rowOrCol = m_rowsOrCols[n];
1682 if ( (size_t)rowOrCol >= pos )
1683 {
1684 if ( numRowsOrCols > 0 )
1685 {
1686 // If rows inserted, include row counter where necessary
1687 rowOrCol += numRowsOrCols;
1688 }
1689 else if ( numRowsOrCols < 0)
1690 {
1691 // If rows deleted, either decrement row counter (if row still exists)
1692 if ((size_t)rowOrCol >= pos - numRowsOrCols)
1693 rowOrCol += numRowsOrCols;
1694 else
1695 {
1696 m_rowsOrCols.RemoveAt((size_t)n);
1697 m_attrs.RemoveAt((size_t)n);
1698 n--; count--;
1699 }
1700 }
1701 }
1702 }
1703 }
1704
1705 // ----------------------------------------------------------------------------
1706 // wxGridCellAttrProvider
1707 // ----------------------------------------------------------------------------
1708
1709 wxGridCellAttrProvider::wxGridCellAttrProvider()
1710 {
1711 m_data = (wxGridCellAttrProviderData *)NULL;
1712 }
1713
1714 wxGridCellAttrProvider::~wxGridCellAttrProvider()
1715 {
1716 delete m_data;
1717 }
1718
1719 void wxGridCellAttrProvider::InitData()
1720 {
1721 m_data = new wxGridCellAttrProviderData;
1722 }
1723
1724 wxGridCellAttr *wxGridCellAttrProvider::GetAttr(int row, int col) const
1725 {
1726 wxGridCellAttr *attr = (wxGridCellAttr *)NULL;
1727 if ( m_data )
1728 {
1729 // first look for the attribute of this specific cell
1730 attr = m_data->m_cellAttrs.GetAttr(row, col);
1731
1732 if ( !attr )
1733 {
1734 // then look for the col attr (col attributes are more common than
1735 // the row ones, hence they have priority)
1736 attr = m_data->m_colAttrs.GetAttr(col);
1737 }
1738
1739 if ( !attr )
1740 {
1741 // finally try the row attributes
1742 attr = m_data->m_rowAttrs.GetAttr(row);
1743 }
1744 }
1745
1746 return attr;
1747 }
1748
1749 void wxGridCellAttrProvider::SetAttr(wxGridCellAttr *attr,
1750 int row, int col)
1751 {
1752 if ( !m_data )
1753 InitData();
1754
1755 m_data->m_cellAttrs.SetAttr(attr, row, col);
1756 }
1757
1758 void wxGridCellAttrProvider::SetRowAttr(wxGridCellAttr *attr, int row)
1759 {
1760 if ( !m_data )
1761 InitData();
1762
1763 m_data->m_rowAttrs.SetAttr(attr, row);
1764 }
1765
1766 void wxGridCellAttrProvider::SetColAttr(wxGridCellAttr *attr, int col)
1767 {
1768 if ( !m_data )
1769 InitData();
1770
1771 m_data->m_colAttrs.SetAttr(attr, col);
1772 }
1773
1774 void wxGridCellAttrProvider::UpdateAttrRows( size_t pos, int numRows )
1775 {
1776 if ( m_data )
1777 {
1778 m_data->m_cellAttrs.UpdateAttrRows( pos, numRows );
1779
1780 m_data->m_rowAttrs.UpdateAttrRowsOrCols( pos, numRows );
1781 }
1782 }
1783
1784 void wxGridCellAttrProvider::UpdateAttrCols( size_t pos, int numCols )
1785 {
1786 if ( m_data )
1787 {
1788 m_data->m_cellAttrs.UpdateAttrCols( pos, numCols );
1789
1790 m_data->m_colAttrs.UpdateAttrRowsOrCols( pos, numCols );
1791 }
1792 }
1793
1794 // ----------------------------------------------------------------------------
1795 // wxGridTypeRegistry
1796 // ----------------------------------------------------------------------------
1797
1798 wxGridTypeRegistry::~wxGridTypeRegistry()
1799 {
1800 size_t count = m_typeinfo.Count();
1801 for ( size_t i = 0; i < count; i++ )
1802 delete m_typeinfo[i];
1803 }
1804
1805
1806 void wxGridTypeRegistry::RegisterDataType(const wxString& typeName,
1807 wxGridCellRenderer* renderer,
1808 wxGridCellEditor* editor)
1809 {
1810 wxGridDataTypeInfo* info = new wxGridDataTypeInfo(typeName, renderer, editor);
1811
1812 // is it already registered?
1813 int loc = FindDataType(typeName);
1814 if ( loc != wxNOT_FOUND )
1815 {
1816 delete m_typeinfo[loc];
1817 m_typeinfo[loc] = info;
1818 }
1819 else
1820 {
1821 m_typeinfo.Add(info);
1822 }
1823 }
1824
1825 int wxGridTypeRegistry::FindDataType(const wxString& typeName)
1826 {
1827 int found = -1;
1828
1829 for (size_t i=0; i<m_typeinfo.Count(); i++) {
1830 if (typeName == m_typeinfo[i]->m_typeName) {
1831 found = i;
1832 break;
1833 }
1834 }
1835
1836 return found;
1837 }
1838
1839 wxGridCellRenderer* wxGridTypeRegistry::GetRenderer(int index)
1840 {
1841 wxGridCellRenderer* renderer = m_typeinfo[index]->m_renderer;
1842 return renderer;
1843 }
1844
1845 wxGridCellEditor* wxGridTypeRegistry::GetEditor(int index)
1846 {
1847 wxGridCellEditor* editor = m_typeinfo[index]->m_editor;
1848 return editor;
1849 }
1850
1851 // ----------------------------------------------------------------------------
1852 // wxGridTableBase
1853 // ----------------------------------------------------------------------------
1854
1855 IMPLEMENT_ABSTRACT_CLASS( wxGridTableBase, wxObject )
1856
1857
1858 wxGridTableBase::wxGridTableBase()
1859 {
1860 m_view = (wxGrid *) NULL;
1861 m_attrProvider = (wxGridCellAttrProvider *) NULL;
1862 }
1863
1864 wxGridTableBase::~wxGridTableBase()
1865 {
1866 delete m_attrProvider;
1867 }
1868
1869 void wxGridTableBase::SetAttrProvider(wxGridCellAttrProvider *attrProvider)
1870 {
1871 delete m_attrProvider;
1872 m_attrProvider = attrProvider;
1873 }
1874
1875 bool wxGridTableBase::CanHaveAttributes()
1876 {
1877 if ( ! GetAttrProvider() )
1878 {
1879 // use the default attr provider by default
1880 SetAttrProvider(new wxGridCellAttrProvider);
1881 }
1882 return TRUE;
1883 }
1884
1885 wxGridCellAttr *wxGridTableBase::GetAttr(int row, int col)
1886 {
1887 if ( m_attrProvider )
1888 return m_attrProvider->GetAttr(row, col);
1889 else
1890 return (wxGridCellAttr *)NULL;
1891 }
1892
1893 void wxGridTableBase::SetAttr(wxGridCellAttr* attr, int row, int col)
1894 {
1895 if ( m_attrProvider )
1896 {
1897 m_attrProvider->SetAttr(attr, row, col);
1898 }
1899 else
1900 {
1901 // as we take ownership of the pointer and don't store it, we must
1902 // free it now
1903 wxSafeDecRef(attr);
1904 }
1905 }
1906
1907 void wxGridTableBase::SetRowAttr(wxGridCellAttr *attr, int row)
1908 {
1909 if ( m_attrProvider )
1910 {
1911 m_attrProvider->SetRowAttr(attr, row);
1912 }
1913 else
1914 {
1915 // as we take ownership of the pointer and don't store it, we must
1916 // free it now
1917 wxSafeDecRef(attr);
1918 }
1919 }
1920
1921 void wxGridTableBase::SetColAttr(wxGridCellAttr *attr, int col)
1922 {
1923 if ( m_attrProvider )
1924 {
1925 m_attrProvider->SetColAttr(attr, col);
1926 }
1927 else
1928 {
1929 // as we take ownership of the pointer and don't store it, we must
1930 // free it now
1931 wxSafeDecRef(attr);
1932 }
1933 }
1934
1935 void wxGridTableBase::UpdateAttrRows( size_t pos, int numRows )
1936 {
1937 if ( m_attrProvider )
1938 {
1939 m_attrProvider->UpdateAttrRows( pos, numRows );
1940 }
1941 }
1942
1943 void wxGridTableBase::UpdateAttrCols( size_t pos, int numCols )
1944 {
1945 if ( m_attrProvider )
1946 {
1947 m_attrProvider->UpdateAttrCols( pos, numCols );
1948 }
1949 }
1950
1951 bool wxGridTableBase::InsertRows( size_t pos, size_t numRows )
1952 {
1953 wxFAIL_MSG( wxT("Called grid table class function InsertRows\n"
1954 "but your derived table class does not override this function") );
1955
1956 return FALSE;
1957 }
1958
1959 bool wxGridTableBase::AppendRows( size_t numRows )
1960 {
1961 wxFAIL_MSG( wxT("Called grid table class function AppendRows\n"
1962 "but your derived table class does not override this function"));
1963
1964 return FALSE;
1965 }
1966
1967 bool wxGridTableBase::DeleteRows( size_t pos, size_t numRows )
1968 {
1969 wxFAIL_MSG( wxT("Called grid table class function DeleteRows\n"
1970 "but your derived table class does not override this function"));
1971
1972 return FALSE;
1973 }
1974
1975 bool wxGridTableBase::InsertCols( size_t pos, size_t numCols )
1976 {
1977 wxFAIL_MSG( wxT("Called grid table class function InsertCols\n"
1978 "but your derived table class does not override this function"));
1979
1980 return FALSE;
1981 }
1982
1983 bool wxGridTableBase::AppendCols( size_t numCols )
1984 {
1985 wxFAIL_MSG(wxT("Called grid table class function AppendCols\n"
1986 "but your derived table class does not override this function"));
1987
1988 return FALSE;
1989 }
1990
1991 bool wxGridTableBase::DeleteCols( size_t pos, size_t numCols )
1992 {
1993 wxFAIL_MSG( wxT("Called grid table class function DeleteCols\n"
1994 "but your derived table class does not override this function"));
1995
1996 return FALSE;
1997 }
1998
1999
2000 wxString wxGridTableBase::GetRowLabelValue( int row )
2001 {
2002 wxString s;
2003 s << row + 1; // RD: Starting the rows at zero confuses users, no matter
2004 // how much it makes sense to us geeks.
2005 return s;
2006 }
2007
2008 wxString wxGridTableBase::GetColLabelValue( int col )
2009 {
2010 // default col labels are:
2011 // cols 0 to 25 : A-Z
2012 // cols 26 to 675 : AA-ZZ
2013 // etc.
2014
2015 wxString s;
2016 unsigned int i, n;
2017 for ( n = 1; ; n++ )
2018 {
2019 s += (_T('A') + (wxChar)( col%26 ));
2020 col = col/26 - 1;
2021 if ( col < 0 ) break;
2022 }
2023
2024 // reverse the string...
2025 wxString s2;
2026 for ( i = 0; i < n; i++ )
2027 {
2028 s2 += s[n-i-1];
2029 }
2030
2031 return s2;
2032 }
2033
2034
2035 wxString wxGridTableBase::GetTypeName( int WXUNUSED(row), int WXUNUSED(col) )
2036 {
2037 return wxGRID_VALUE_STRING;
2038 }
2039
2040 bool wxGridTableBase::CanGetValueAs( int WXUNUSED(row), int WXUNUSED(col),
2041 const wxString& typeName )
2042 {
2043 return typeName == wxGRID_VALUE_STRING;
2044 }
2045
2046 bool wxGridTableBase::CanSetValueAs( int row, int col, const wxString& typeName )
2047 {
2048 return CanGetValueAs(row, col, typeName);
2049 }
2050
2051 long wxGridTableBase::GetValueAsLong( int WXUNUSED(row), int WXUNUSED(col) )
2052 {
2053 return 0;
2054 }
2055
2056 double wxGridTableBase::GetValueAsDouble( int WXUNUSED(row), int WXUNUSED(col) )
2057 {
2058 return 0.0;
2059 }
2060
2061 bool wxGridTableBase::GetValueAsBool( int WXUNUSED(row), int WXUNUSED(col) )
2062 {
2063 return FALSE;
2064 }
2065
2066 void wxGridTableBase::SetValueAsLong( int WXUNUSED(row), int WXUNUSED(col),
2067 long WXUNUSED(value) )
2068 {
2069 }
2070
2071 void wxGridTableBase::SetValueAsDouble( int WXUNUSED(row), int WXUNUSED(col),
2072 double WXUNUSED(value) )
2073 {
2074 }
2075
2076 void wxGridTableBase::SetValueAsBool( int WXUNUSED(row), int WXUNUSED(col),
2077 bool WXUNUSED(value) )
2078 {
2079 }
2080
2081
2082 void* wxGridTableBase::GetValueAsCustom( int WXUNUSED(row), int WXUNUSED(col),
2083 const wxString& WXUNUSED(typeName) )
2084 {
2085 return NULL;
2086 }
2087
2088 void wxGridTableBase::SetValueAsCustom( int WXUNUSED(row), int WXUNUSED(col),
2089 const wxString& WXUNUSED(typeName),
2090 void* WXUNUSED(value) )
2091 {
2092 }
2093
2094
2095 //////////////////////////////////////////////////////////////////////
2096 //
2097 // Message class for the grid table to send requests and notifications
2098 // to the grid view
2099 //
2100
2101 wxGridTableMessage::wxGridTableMessage()
2102 {
2103 m_table = (wxGridTableBase *) NULL;
2104 m_id = -1;
2105 m_comInt1 = -1;
2106 m_comInt2 = -1;
2107 }
2108
2109 wxGridTableMessage::wxGridTableMessage( wxGridTableBase *table, int id,
2110 int commandInt1, int commandInt2 )
2111 {
2112 m_table = table;
2113 m_id = id;
2114 m_comInt1 = commandInt1;
2115 m_comInt2 = commandInt2;
2116 }
2117
2118
2119
2120 //////////////////////////////////////////////////////////////////////
2121 //
2122 // A basic grid table for string data. An object of this class will
2123 // created by wxGrid if you don't specify an alternative table class.
2124 //
2125
2126 WX_DEFINE_OBJARRAY(wxGridStringArray)
2127
2128 IMPLEMENT_DYNAMIC_CLASS( wxGridStringTable, wxGridTableBase )
2129
2130 wxGridStringTable::wxGridStringTable()
2131 : wxGridTableBase()
2132 {
2133 }
2134
2135 wxGridStringTable::wxGridStringTable( int numRows, int numCols )
2136 : wxGridTableBase()
2137 {
2138 int row, col;
2139
2140 m_data.Alloc( numRows );
2141
2142 wxArrayString sa;
2143 sa.Alloc( numCols );
2144 for ( col = 0; col < numCols; col++ )
2145 {
2146 sa.Add( wxEmptyString );
2147 }
2148
2149 for ( row = 0; row < numRows; row++ )
2150 {
2151 m_data.Add( sa );
2152 }
2153 }
2154
2155 wxGridStringTable::~wxGridStringTable()
2156 {
2157 }
2158
2159 long wxGridStringTable::GetNumberRows()
2160 {
2161 return m_data.GetCount();
2162 }
2163
2164 long wxGridStringTable::GetNumberCols()
2165 {
2166 if ( m_data.GetCount() > 0 )
2167 return m_data[0].GetCount();
2168 else
2169 return 0;
2170 }
2171
2172 wxString wxGridStringTable::GetValue( int row, int col )
2173 {
2174 wxASSERT_MSG( (row < GetNumberCols()) && (col < GetNumberCols()),
2175 _T("invalid row or column index in wxGridStringTable") );
2176
2177 return m_data[row][col];
2178 }
2179
2180 void wxGridStringTable::SetValue( int row, int col, const wxString& value )
2181 {
2182 wxASSERT_MSG( (row < GetNumberCols()) && (col < GetNumberCols()),
2183 _T("invalid row or column index in wxGridStringTable") );
2184
2185 m_data[row][col] = value;
2186 }
2187
2188 bool wxGridStringTable::IsEmptyCell( int row, int col )
2189 {
2190 wxASSERT_MSG( (row < GetNumberCols()) && (col < GetNumberCols()),
2191 _T("invalid row or column index in wxGridStringTable") );
2192
2193 return (m_data[row][col] == wxEmptyString);
2194 }
2195
2196 void wxGridStringTable::Clear()
2197 {
2198 int row, col;
2199 int numRows, numCols;
2200
2201 numRows = m_data.GetCount();
2202 if ( numRows > 0 )
2203 {
2204 numCols = m_data[0].GetCount();
2205
2206 for ( row = 0; row < numRows; row++ )
2207 {
2208 for ( col = 0; col < numCols; col++ )
2209 {
2210 m_data[row][col] = wxEmptyString;
2211 }
2212 }
2213 }
2214 }
2215
2216
2217 bool wxGridStringTable::InsertRows( size_t pos, size_t numRows )
2218 {
2219 size_t row, col;
2220
2221 size_t curNumRows = m_data.GetCount();
2222 size_t curNumCols = ( curNumRows > 0 ? m_data[0].GetCount() : 0 );
2223
2224 if ( pos >= curNumRows )
2225 {
2226 return AppendRows( numRows );
2227 }
2228
2229 wxArrayString sa;
2230 sa.Alloc( curNumCols );
2231 for ( col = 0; col < curNumCols; col++ )
2232 {
2233 sa.Add( wxEmptyString );
2234 }
2235
2236 for ( row = pos; row < pos + numRows; row++ )
2237 {
2238 m_data.Insert( sa, row );
2239 }
2240 UpdateAttrRows( pos, numRows );
2241 if ( GetView() )
2242 {
2243 wxGridTableMessage msg( this,
2244 wxGRIDTABLE_NOTIFY_ROWS_INSERTED,
2245 pos,
2246 numRows );
2247
2248 GetView()->ProcessTableMessage( msg );
2249 }
2250
2251 return TRUE;
2252 }
2253
2254 bool wxGridStringTable::AppendRows( size_t numRows )
2255 {
2256 size_t row, col;
2257
2258 size_t curNumRows = m_data.GetCount();
2259 size_t curNumCols = ( curNumRows > 0 ? m_data[0].GetCount() : 0 );
2260
2261 wxArrayString sa;
2262 if ( curNumCols > 0 )
2263 {
2264 sa.Alloc( curNumCols );
2265 for ( col = 0; col < curNumCols; col++ )
2266 {
2267 sa.Add( wxEmptyString );
2268 }
2269 }
2270
2271 for ( row = 0; row < numRows; row++ )
2272 {
2273 m_data.Add( sa );
2274 }
2275
2276 if ( GetView() )
2277 {
2278 wxGridTableMessage msg( this,
2279 wxGRIDTABLE_NOTIFY_ROWS_APPENDED,
2280 numRows );
2281
2282 GetView()->ProcessTableMessage( msg );
2283 }
2284
2285 return TRUE;
2286 }
2287
2288 bool wxGridStringTable::DeleteRows( size_t pos, size_t numRows )
2289 {
2290 size_t n;
2291
2292 size_t curNumRows = m_data.GetCount();
2293
2294 if ( pos >= curNumRows )
2295 {
2296 wxString errmsg;
2297 errmsg.Printf("Called wxGridStringTable::DeleteRows(pos=%d, N=%d)\n"
2298 "Pos value is invalid for present table with %d rows",
2299 pos, numRows, curNumRows );
2300 wxFAIL_MSG( wxT(errmsg) );
2301 return FALSE;
2302 }
2303
2304 if ( numRows > curNumRows - pos )
2305 {
2306 numRows = curNumRows - pos;
2307 }
2308
2309 if ( numRows >= curNumRows )
2310 {
2311 m_data.Empty(); // don't release memory just yet
2312 }
2313 else
2314 {
2315 for ( n = 0; n < numRows; n++ )
2316 {
2317 m_data.Remove( pos );
2318 }
2319 }
2320 UpdateAttrRows( pos, -((int)numRows) );
2321 if ( GetView() )
2322 {
2323 wxGridTableMessage msg( this,
2324 wxGRIDTABLE_NOTIFY_ROWS_DELETED,
2325 pos,
2326 numRows );
2327
2328 GetView()->ProcessTableMessage( msg );
2329 }
2330
2331 return TRUE;
2332 }
2333
2334 bool wxGridStringTable::InsertCols( size_t pos, size_t numCols )
2335 {
2336 size_t row, col;
2337
2338 size_t curNumRows = m_data.GetCount();
2339 size_t curNumCols = ( curNumRows > 0 ? m_data[0].GetCount() : 0 );
2340
2341 if ( pos >= curNumCols )
2342 {
2343 return AppendCols( numCols );
2344 }
2345
2346 for ( row = 0; row < curNumRows; row++ )
2347 {
2348 for ( col = pos; col < pos + numCols; col++ )
2349 {
2350 m_data[row].Insert( wxEmptyString, col );
2351 }
2352 }
2353 UpdateAttrCols( pos, numCols );
2354 if ( GetView() )
2355 {
2356 wxGridTableMessage msg( this,
2357 wxGRIDTABLE_NOTIFY_COLS_INSERTED,
2358 pos,
2359 numCols );
2360
2361 GetView()->ProcessTableMessage( msg );
2362 }
2363
2364 return TRUE;
2365 }
2366
2367 bool wxGridStringTable::AppendCols( size_t numCols )
2368 {
2369 size_t row, n;
2370
2371 size_t curNumRows = m_data.GetCount();
2372 if ( !curNumRows )
2373 {
2374 // TODO: something better than this ?
2375 //
2376 wxFAIL_MSG( wxT("Unable to append cols to a grid table with no rows.\n"
2377 "Call AppendRows() first") );
2378 return FALSE;
2379 }
2380
2381 for ( row = 0; row < curNumRows; row++ )
2382 {
2383 for ( n = 0; n < numCols; n++ )
2384 {
2385 m_data[row].Add( wxEmptyString );
2386 }
2387 }
2388
2389 if ( GetView() )
2390 {
2391 wxGridTableMessage msg( this,
2392 wxGRIDTABLE_NOTIFY_COLS_APPENDED,
2393 numCols );
2394
2395 GetView()->ProcessTableMessage( msg );
2396 }
2397
2398 return TRUE;
2399 }
2400
2401 bool wxGridStringTable::DeleteCols( size_t pos, size_t numCols )
2402 {
2403 size_t row, n;
2404
2405 size_t curNumRows = m_data.GetCount();
2406 size_t curNumCols = ( curNumRows > 0 ? m_data[0].GetCount() : 0 );
2407
2408 if ( pos >= curNumCols )
2409 {
2410 wxString errmsg;
2411 errmsg.Printf( "Called wxGridStringTable::DeleteCols(pos=%d, N=%d)...\n"
2412 "Pos value is invalid for present table with %d cols",
2413 pos, numCols, curNumCols );
2414 wxFAIL_MSG( wxT( errmsg ) );
2415 return FALSE;
2416 }
2417
2418 if ( numCols > curNumCols - pos )
2419 {
2420 numCols = curNumCols - pos;
2421 }
2422
2423 for ( row = 0; row < curNumRows; row++ )
2424 {
2425 if ( numCols >= curNumCols )
2426 {
2427 m_data[row].Clear();
2428 }
2429 else
2430 {
2431 for ( n = 0; n < numCols; n++ )
2432 {
2433 m_data[row].Remove( pos );
2434 }
2435 }
2436 }
2437 UpdateAttrCols( pos, -((int)numCols) );
2438 if ( GetView() )
2439 {
2440 wxGridTableMessage msg( this,
2441 wxGRIDTABLE_NOTIFY_COLS_DELETED,
2442 pos,
2443 numCols );
2444
2445 GetView()->ProcessTableMessage( msg );
2446 }
2447
2448 return TRUE;
2449 }
2450
2451 wxString wxGridStringTable::GetRowLabelValue( int row )
2452 {
2453 if ( row > (int)(m_rowLabels.GetCount()) - 1 )
2454 {
2455 // using default label
2456 //
2457 return wxGridTableBase::GetRowLabelValue( row );
2458 }
2459 else
2460 {
2461 return m_rowLabels[ row ];
2462 }
2463 }
2464
2465 wxString wxGridStringTable::GetColLabelValue( int col )
2466 {
2467 if ( col > (int)(m_colLabels.GetCount()) - 1 )
2468 {
2469 // using default label
2470 //
2471 return wxGridTableBase::GetColLabelValue( col );
2472 }
2473 else
2474 {
2475 return m_colLabels[ col ];
2476 }
2477 }
2478
2479 void wxGridStringTable::SetRowLabelValue( int row, const wxString& value )
2480 {
2481 if ( row > (int)(m_rowLabels.GetCount()) - 1 )
2482 {
2483 int n = m_rowLabels.GetCount();
2484 int i;
2485 for ( i = n; i <= row; i++ )
2486 {
2487 m_rowLabels.Add( wxGridTableBase::GetRowLabelValue(i) );
2488 }
2489 }
2490
2491 m_rowLabels[row] = value;
2492 }
2493
2494 void wxGridStringTable::SetColLabelValue( int col, const wxString& value )
2495 {
2496 if ( col > (int)(m_colLabels.GetCount()) - 1 )
2497 {
2498 int n = m_colLabels.GetCount();
2499 int i;
2500 for ( i = n; i <= col; i++ )
2501 {
2502 m_colLabels.Add( wxGridTableBase::GetColLabelValue(i) );
2503 }
2504 }
2505
2506 m_colLabels[col] = value;
2507 }
2508
2509
2510
2511 //////////////////////////////////////////////////////////////////////
2512 //////////////////////////////////////////////////////////////////////
2513
2514 IMPLEMENT_DYNAMIC_CLASS( wxGridRowLabelWindow, wxWindow )
2515
2516 BEGIN_EVENT_TABLE( wxGridRowLabelWindow, wxWindow )
2517 EVT_PAINT( wxGridRowLabelWindow::OnPaint )
2518 EVT_MOUSE_EVENTS( wxGridRowLabelWindow::OnMouseEvent )
2519 EVT_KEY_DOWN( wxGridRowLabelWindow::OnKeyDown )
2520 END_EVENT_TABLE()
2521
2522 wxGridRowLabelWindow::wxGridRowLabelWindow( wxGrid *parent,
2523 wxWindowID id,
2524 const wxPoint &pos, const wxSize &size )
2525 : wxWindow( parent, id, pos, size, wxWANTS_CHARS )
2526 {
2527 m_owner = parent;
2528 }
2529
2530 void wxGridRowLabelWindow::OnPaint( wxPaintEvent &event )
2531 {
2532 wxPaintDC dc(this);
2533
2534 // NO - don't do this because it will set both the x and y origin
2535 // coords to match the parent scrolled window and we just want to
2536 // set the y coord - MB
2537 //
2538 // m_owner->PrepareDC( dc );
2539
2540 int x, y;
2541 m_owner->CalcUnscrolledPosition( 0, 0, &x, &y );
2542 dc.SetDeviceOrigin( 0, -y );
2543
2544 m_owner->CalcRowLabelsExposed( GetUpdateRegion() );
2545 m_owner->DrawRowLabels( dc );
2546 }
2547
2548
2549 void wxGridRowLabelWindow::OnMouseEvent( wxMouseEvent& event )
2550 {
2551 m_owner->ProcessRowLabelMouseEvent( event );
2552 }
2553
2554
2555 // This seems to be required for wxMotif otherwise the mouse
2556 // cursor must be in the cell edit control to get key events
2557 //
2558 void wxGridRowLabelWindow::OnKeyDown( wxKeyEvent& event )
2559 {
2560 if ( !m_owner->ProcessEvent( event ) ) event.Skip();
2561 }
2562
2563
2564
2565 //////////////////////////////////////////////////////////////////////
2566
2567 IMPLEMENT_DYNAMIC_CLASS( wxGridColLabelWindow, wxWindow )
2568
2569 BEGIN_EVENT_TABLE( wxGridColLabelWindow, wxWindow )
2570 EVT_PAINT( wxGridColLabelWindow::OnPaint )
2571 EVT_MOUSE_EVENTS( wxGridColLabelWindow::OnMouseEvent )
2572 EVT_KEY_DOWN( wxGridColLabelWindow::OnKeyDown )
2573 END_EVENT_TABLE()
2574
2575 wxGridColLabelWindow::wxGridColLabelWindow( wxGrid *parent,
2576 wxWindowID id,
2577 const wxPoint &pos, const wxSize &size )
2578 : wxWindow( parent, id, pos, size, wxWANTS_CHARS )
2579 {
2580 m_owner = parent;
2581 }
2582
2583 void wxGridColLabelWindow::OnPaint( wxPaintEvent &event )
2584 {
2585 wxPaintDC dc(this);
2586
2587 // NO - don't do this because it will set both the x and y origin
2588 // coords to match the parent scrolled window and we just want to
2589 // set the x coord - MB
2590 //
2591 // m_owner->PrepareDC( dc );
2592
2593 int x, y;
2594 m_owner->CalcUnscrolledPosition( 0, 0, &x, &y );
2595 dc.SetDeviceOrigin( -x, 0 );
2596
2597 m_owner->CalcColLabelsExposed( GetUpdateRegion() );
2598 m_owner->DrawColLabels( dc );
2599 }
2600
2601
2602 void wxGridColLabelWindow::OnMouseEvent( wxMouseEvent& event )
2603 {
2604 m_owner->ProcessColLabelMouseEvent( event );
2605 }
2606
2607
2608 // This seems to be required for wxMotif otherwise the mouse
2609 // cursor must be in the cell edit control to get key events
2610 //
2611 void wxGridColLabelWindow::OnKeyDown( wxKeyEvent& event )
2612 {
2613 if ( !m_owner->ProcessEvent( event ) ) event.Skip();
2614 }
2615
2616
2617
2618 //////////////////////////////////////////////////////////////////////
2619
2620 IMPLEMENT_DYNAMIC_CLASS( wxGridCornerLabelWindow, wxWindow )
2621
2622 BEGIN_EVENT_TABLE( wxGridCornerLabelWindow, wxWindow )
2623 EVT_MOUSE_EVENTS( wxGridCornerLabelWindow::OnMouseEvent )
2624 EVT_PAINT( wxGridCornerLabelWindow::OnPaint)
2625 EVT_KEY_DOWN( wxGridCornerLabelWindow::OnKeyDown )
2626 END_EVENT_TABLE()
2627
2628 wxGridCornerLabelWindow::wxGridCornerLabelWindow( wxGrid *parent,
2629 wxWindowID id,
2630 const wxPoint &pos, const wxSize &size )
2631 : wxWindow( parent, id, pos, size, wxWANTS_CHARS )
2632 {
2633 m_owner = parent;
2634 }
2635
2636 void wxGridCornerLabelWindow::OnPaint( wxPaintEvent& WXUNUSED(event) )
2637 {
2638 wxPaintDC dc(this);
2639
2640 int client_height = 0;
2641 int client_width = 0;
2642 GetClientSize( &client_width, &client_height );
2643
2644 dc.SetPen( *wxBLACK_PEN );
2645 dc.DrawLine( client_width-1, client_height-1, client_width-1, 0 );
2646 dc.DrawLine( client_width-1, client_height-1, 0, client_height-1 );
2647
2648 dc.SetPen( *wxWHITE_PEN );
2649 dc.DrawLine( 0, 0, client_width, 0 );
2650 dc.DrawLine( 0, 0, 0, client_height );
2651 }
2652
2653
2654 void wxGridCornerLabelWindow::OnMouseEvent( wxMouseEvent& event )
2655 {
2656 m_owner->ProcessCornerLabelMouseEvent( event );
2657 }
2658
2659
2660 // This seems to be required for wxMotif otherwise the mouse
2661 // cursor must be in the cell edit control to get key events
2662 //
2663 void wxGridCornerLabelWindow::OnKeyDown( wxKeyEvent& event )
2664 {
2665 if ( !m_owner->ProcessEvent( event ) ) event.Skip();
2666 }
2667
2668
2669
2670 //////////////////////////////////////////////////////////////////////
2671
2672 IMPLEMENT_DYNAMIC_CLASS( wxGridWindow, wxPanel )
2673
2674 BEGIN_EVENT_TABLE( wxGridWindow, wxPanel )
2675 EVT_PAINT( wxGridWindow::OnPaint )
2676 EVT_MOUSE_EVENTS( wxGridWindow::OnMouseEvent )
2677 EVT_KEY_DOWN( wxGridWindow::OnKeyDown )
2678 EVT_ERASE_BACKGROUND( wxGridWindow::OnEraseBackground )
2679 END_EVENT_TABLE()
2680
2681 wxGridWindow::wxGridWindow( wxGrid *parent,
2682 wxGridRowLabelWindow *rowLblWin,
2683 wxGridColLabelWindow *colLblWin,
2684 wxWindowID id, const wxPoint &pos, const wxSize &size )
2685 : wxPanel( parent, id, pos, size, wxWANTS_CHARS, "grid window" )
2686 {
2687 m_owner = parent;
2688 m_rowLabelWin = rowLblWin;
2689 m_colLabelWin = colLblWin;
2690 SetBackgroundColour( "WHITE" );
2691 }
2692
2693
2694 wxGridWindow::~wxGridWindow()
2695 {
2696 }
2697
2698
2699 void wxGridWindow::OnPaint( wxPaintEvent &WXUNUSED(event) )
2700 {
2701 wxPaintDC dc( this );
2702 m_owner->PrepareDC( dc );
2703 wxRegion reg = GetUpdateRegion();
2704 m_owner->CalcCellsExposed( reg );
2705 m_owner->DrawGridCellArea( dc );
2706 #if WXGRID_DRAW_LINES
2707 m_owner->DrawAllGridLines( dc, reg );
2708 #endif
2709 m_owner->DrawGridSpace( dc );
2710 m_owner->DrawHighlight( dc );
2711 }
2712
2713
2714 void wxGridWindow::ScrollWindow( int dx, int dy, const wxRect *rect )
2715 {
2716 wxPanel::ScrollWindow( dx, dy, rect );
2717 m_rowLabelWin->ScrollWindow( 0, dy, rect );
2718 m_colLabelWin->ScrollWindow( dx, 0, rect );
2719 }
2720
2721
2722 void wxGridWindow::OnMouseEvent( wxMouseEvent& event )
2723 {
2724 m_owner->ProcessGridCellMouseEvent( event );
2725 }
2726
2727
2728 // This seems to be required for wxMotif otherwise the mouse
2729 // cursor must be in the cell edit control to get key events
2730 //
2731 void wxGridWindow::OnKeyDown( wxKeyEvent& event )
2732 {
2733 if ( !m_owner->ProcessEvent( event ) ) event.Skip();
2734 }
2735
2736
2737 void wxGridWindow::OnEraseBackground(wxEraseEvent& event)
2738 {
2739 }
2740
2741
2742 //////////////////////////////////////////////////////////////////////
2743
2744
2745 IMPLEMENT_DYNAMIC_CLASS( wxGrid, wxScrolledWindow )
2746
2747 BEGIN_EVENT_TABLE( wxGrid, wxScrolledWindow )
2748 EVT_PAINT( wxGrid::OnPaint )
2749 EVT_SIZE( wxGrid::OnSize )
2750 EVT_KEY_DOWN( wxGrid::OnKeyDown )
2751 EVT_ERASE_BACKGROUND( wxGrid::OnEraseBackground )
2752 END_EVENT_TABLE()
2753
2754 wxGrid::wxGrid( wxWindow *parent,
2755 wxWindowID id,
2756 const wxPoint& pos,
2757 const wxSize& size,
2758 long style,
2759 const wxString& name )
2760 : wxScrolledWindow( parent, id, pos, size, (style | wxWANTS_CHARS), name ),
2761 m_colMinWidths(GRID_HASH_SIZE),
2762 m_rowMinHeights(GRID_HASH_SIZE)
2763 {
2764 Create();
2765 }
2766
2767
2768 wxGrid::~wxGrid()
2769 {
2770 ClearAttrCache();
2771 wxSafeDecRef(m_defaultCellAttr);
2772
2773 #ifdef DEBUG_ATTR_CACHE
2774 size_t total = gs_nAttrCacheHits + gs_nAttrCacheMisses;
2775 wxPrintf(_T("wxGrid attribute cache statistics: "
2776 "total: %u, hits: %u (%u%%)\n"),
2777 total, gs_nAttrCacheHits,
2778 total ? (gs_nAttrCacheHits*100) / total : 0);
2779 #endif
2780
2781 if (m_ownTable)
2782 delete m_table;
2783
2784 delete m_typeRegistry;
2785 }
2786
2787
2788 //
2789 // ----- internal init and update functions
2790 //
2791
2792 void wxGrid::Create()
2793 {
2794 m_created = FALSE; // set to TRUE by CreateGrid
2795 m_displayed = TRUE; // FALSE; // set to TRUE by OnPaint
2796
2797 m_table = (wxGridTableBase *) NULL;
2798 m_ownTable = FALSE;
2799
2800 m_cellEditCtrlEnabled = FALSE;
2801
2802 m_defaultCellAttr = new wxGridCellAttr;
2803 m_defaultCellAttr->SetDefAttr(m_defaultCellAttr);
2804
2805 // Set default cell attributes
2806 m_defaultCellAttr->SetFont(GetFont());
2807 m_defaultCellAttr->SetAlignment(wxLEFT, wxTOP);
2808 m_defaultCellAttr->SetTextColour(
2809 wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOWTEXT));
2810 m_defaultCellAttr->SetBackgroundColour(
2811 wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW));
2812 m_defaultCellAttr->SetRenderer(new wxGridCellStringRenderer);
2813 m_defaultCellAttr->SetEditor(new wxGridCellTextEditor);
2814
2815
2816 m_numRows = 0;
2817 m_numCols = 0;
2818 m_currentCellCoords = wxGridNoCellCoords;
2819
2820 m_rowLabelWidth = WXGRID_DEFAULT_ROW_LABEL_WIDTH;
2821 m_colLabelHeight = WXGRID_DEFAULT_COL_LABEL_HEIGHT;
2822
2823 // data type registration: register all standard data types
2824 // TODO: may be allow the app to selectively disable some of them?
2825 m_typeRegistry = new wxGridTypeRegistry;
2826 RegisterDataType(wxGRID_VALUE_STRING,
2827 new wxGridCellStringRenderer,
2828 new wxGridCellTextEditor);
2829 RegisterDataType(wxGRID_VALUE_BOOL,
2830 new wxGridCellBoolRenderer,
2831 new wxGridCellBoolEditor);
2832 RegisterDataType(wxGRID_VALUE_NUMBER,
2833 new wxGridCellNumberRenderer,
2834 new wxGridCellNumberEditor);
2835
2836 // subwindow components that make up the wxGrid
2837 m_cornerLabelWin = new wxGridCornerLabelWindow( this,
2838 -1,
2839 wxDefaultPosition,
2840 wxDefaultSize );
2841
2842 m_rowLabelWin = new wxGridRowLabelWindow( this,
2843 -1,
2844 wxDefaultPosition,
2845 wxDefaultSize );
2846
2847 m_colLabelWin = new wxGridColLabelWindow( this,
2848 -1,
2849 wxDefaultPosition,
2850 wxDefaultSize );
2851
2852 m_gridWin = new wxGridWindow( this,
2853 m_rowLabelWin,
2854 m_colLabelWin,
2855 -1,
2856 wxDefaultPosition,
2857 wxDefaultSize );
2858
2859 SetTargetWindow( m_gridWin );
2860 }
2861
2862
2863 bool wxGrid::CreateGrid( int numRows, int numCols )
2864 {
2865 if ( m_created )
2866 {
2867 wxFAIL_MSG( wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
2868 return FALSE;
2869 }
2870 else
2871 {
2872 m_numRows = numRows;
2873 m_numCols = numCols;
2874
2875 m_table = new wxGridStringTable( m_numRows, m_numCols );
2876 m_table->SetView( this );
2877 m_ownTable = TRUE;
2878 Init();
2879 m_created = TRUE;
2880 }
2881
2882 return m_created;
2883 }
2884
2885 bool wxGrid::SetTable( wxGridTableBase *table, bool takeOwnership )
2886 {
2887 if ( m_created )
2888 {
2889 // RD: Actually, this should probably be allowed. I think it would be
2890 // nice to be able to switch multiple Tables in and out of a single
2891 // View at runtime. Is there anything in the implmentation that would
2892 // prevent this?
2893
2894 wxFAIL_MSG( wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
2895 return FALSE;
2896 }
2897 else
2898 {
2899 m_numRows = table->GetNumberRows();
2900 m_numCols = table->GetNumberCols();
2901
2902 m_table = table;
2903 m_table->SetView( this );
2904 if (takeOwnership)
2905 m_ownTable = TRUE;
2906 Init();
2907 m_created = TRUE;
2908 }
2909
2910 return m_created;
2911 }
2912
2913
2914 void wxGrid::Init()
2915 {
2916 if ( m_numRows <= 0 )
2917 m_numRows = WXGRID_DEFAULT_NUMBER_ROWS;
2918
2919 if ( m_numCols <= 0 )
2920 m_numCols = WXGRID_DEFAULT_NUMBER_COLS;
2921
2922 m_rowLabelWidth = WXGRID_DEFAULT_ROW_LABEL_WIDTH;
2923 m_colLabelHeight = WXGRID_DEFAULT_COL_LABEL_HEIGHT;
2924
2925 if ( m_rowLabelWin )
2926 {
2927 m_labelBackgroundColour = m_rowLabelWin->GetBackgroundColour();
2928 }
2929 else
2930 {
2931 m_labelBackgroundColour = wxColour( _T("WHITE") );
2932 }
2933
2934 m_labelTextColour = wxColour( _T("BLACK") );
2935
2936 // init attr cache
2937 m_attrCache.row = -1;
2938
2939 // TODO: something better than this ?
2940 //
2941 m_labelFont = this->GetFont();
2942 m_labelFont.SetWeight( m_labelFont.GetWeight() + 2 );
2943
2944 m_rowLabelHorizAlign = wxLEFT;
2945 m_rowLabelVertAlign = wxCENTRE;
2946
2947 m_colLabelHorizAlign = wxCENTRE;
2948 m_colLabelVertAlign = wxTOP;
2949
2950 m_defaultColWidth = WXGRID_DEFAULT_COL_WIDTH;
2951 m_defaultRowHeight = m_gridWin->GetCharHeight();
2952
2953 #if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl()
2954 m_defaultRowHeight += 8;
2955 #else
2956 m_defaultRowHeight += 4;
2957 #endif
2958
2959 m_gridLineColour = wxColour( 128, 128, 255 );
2960 m_gridLinesEnabled = TRUE;
2961
2962 m_cursorMode = WXGRID_CURSOR_SELECT_CELL;
2963 m_winCapture = (wxWindow *)NULL;
2964 m_canDragRowSize = TRUE;
2965 m_canDragColSize = TRUE;
2966 m_canDragGridSize = TRUE;
2967 m_dragLastPos = -1;
2968 m_dragRowOrCol = -1;
2969 m_isDragging = FALSE;
2970 m_startDragPos = wxDefaultPosition;
2971
2972 m_waitForSlowClick = FALSE;
2973
2974 m_rowResizeCursor = wxCursor( wxCURSOR_SIZENS );
2975 m_colResizeCursor = wxCursor( wxCURSOR_SIZEWE );
2976
2977 m_currentCellCoords = wxGridNoCellCoords;
2978
2979 m_selectedTopLeft = wxGridNoCellCoords;
2980 m_selectedBottomRight = wxGridNoCellCoords;
2981 m_selectionBackground = wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHT);
2982 m_selectionForeground = wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHTTEXT);
2983
2984 m_editable = TRUE; // default for whole grid
2985
2986 m_inOnKeyDown = FALSE;
2987 m_batchCount = 0;
2988
2989 m_extraWidth =
2990 m_extraHeight = 50;
2991
2992 CalcDimensions();
2993 }
2994
2995 // ----------------------------------------------------------------------------
2996 // the idea is to call these functions only when necessary because they create
2997 // quite big arrays which eat memory mostly unnecessary - in particular, if
2998 // default widths/heights are used for all rows/columns, we may not use these
2999 // arrays at all
3000 //
3001 // with some extra code, it should be possible to only store the
3002 // widths/heights different from default ones but this will be done later...
3003 // ----------------------------------------------------------------------------
3004
3005 void wxGrid::InitRowHeights()
3006 {
3007 m_rowHeights.Empty();
3008 m_rowBottoms.Empty();
3009
3010 m_rowHeights.Alloc( m_numRows );
3011 m_rowBottoms.Alloc( m_numRows );
3012
3013 int rowBottom = 0;
3014 for ( int i = 0; i < m_numRows; i++ )
3015 {
3016 m_rowHeights.Add( m_defaultRowHeight );
3017 rowBottom += m_defaultRowHeight;
3018 m_rowBottoms.Add( rowBottom );
3019 }
3020 }
3021
3022 void wxGrid::InitColWidths()
3023 {
3024 m_colWidths.Empty();
3025 m_colRights.Empty();
3026
3027 m_colWidths.Alloc( m_numCols );
3028 m_colRights.Alloc( m_numCols );
3029 int colRight = 0;
3030 for ( int i = 0; i < m_numCols; i++ )
3031 {
3032 m_colWidths.Add( m_defaultColWidth );
3033 colRight += m_defaultColWidth;
3034 m_colRights.Add( colRight );
3035 }
3036 }
3037
3038 int wxGrid::GetColWidth(int col) const
3039 {
3040 return m_colWidths.IsEmpty() ? m_defaultColWidth : m_colWidths[col];
3041 }
3042
3043 int wxGrid::GetColLeft(int col) const
3044 {
3045 return m_colRights.IsEmpty() ? col * m_defaultColWidth
3046 : m_colRights[col] - m_colWidths[col];
3047 }
3048
3049 int wxGrid::GetColRight(int col) const
3050 {
3051 return m_colRights.IsEmpty() ? (col + 1) * m_defaultColWidth
3052 : m_colRights[col];
3053 }
3054
3055 int wxGrid::GetRowHeight(int row) const
3056 {
3057 return m_rowHeights.IsEmpty() ? m_defaultRowHeight : m_rowHeights[row];
3058 }
3059
3060 int wxGrid::GetRowTop(int row) const
3061 {
3062 return m_rowBottoms.IsEmpty() ? row * m_defaultRowHeight
3063 : m_rowBottoms[row] - m_rowHeights[row];
3064 }
3065
3066 int wxGrid::GetRowBottom(int row) const
3067 {
3068 return m_rowBottoms.IsEmpty() ? (row + 1) * m_defaultRowHeight
3069 : m_rowBottoms[row];
3070 }
3071
3072 void wxGrid::CalcDimensions()
3073 {
3074 int cw, ch;
3075 GetClientSize( &cw, &ch );
3076
3077 if ( m_numRows > 0 && m_numCols > 0 )
3078 {
3079 int right = GetColRight( m_numCols-1 ) + m_extraWidth;
3080 int bottom = GetRowBottom( m_numRows-1 ) + m_extraHeight;
3081
3082 // TODO: restore the scroll position that we had before sizing
3083 //
3084 int x, y;
3085 GetViewStart( &x, &y );
3086 SetScrollbars( GRID_SCROLL_LINE, GRID_SCROLL_LINE,
3087 right/GRID_SCROLL_LINE, bottom/GRID_SCROLL_LINE,
3088 x, y );
3089 }
3090 }
3091
3092
3093 void wxGrid::CalcWindowSizes()
3094 {
3095 int cw, ch;
3096 GetClientSize( &cw, &ch );
3097
3098 if ( m_cornerLabelWin->IsShown() )
3099 m_cornerLabelWin->SetSize( 0, 0, m_rowLabelWidth, m_colLabelHeight );
3100
3101 if ( m_colLabelWin->IsShown() )
3102 m_colLabelWin->SetSize( m_rowLabelWidth, 0, cw-m_rowLabelWidth, m_colLabelHeight);
3103
3104 if ( m_rowLabelWin->IsShown() )
3105 m_rowLabelWin->SetSize( 0, m_colLabelHeight, m_rowLabelWidth, ch-m_colLabelHeight);
3106
3107 if ( m_gridWin->IsShown() )
3108 m_gridWin->SetSize( m_rowLabelWidth, m_colLabelHeight, cw-m_rowLabelWidth, ch-m_colLabelHeight);
3109 }
3110
3111
3112 // this is called when the grid table sends a message to say that it
3113 // has been redimensioned
3114 //
3115 bool wxGrid::Redimension( wxGridTableMessage& msg )
3116 {
3117 int i;
3118
3119 // if we were using the default widths/heights so far, we must change them
3120 // now
3121 if ( m_colWidths.IsEmpty() )
3122 {
3123 InitColWidths();
3124 }
3125
3126 if ( m_rowHeights.IsEmpty() )
3127 {
3128 InitRowHeights();
3129 }
3130
3131 switch ( msg.GetId() )
3132 {
3133 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED:
3134 {
3135 size_t pos = msg.GetCommandInt();
3136 int numRows = msg.GetCommandInt2();
3137 for ( i = 0; i < numRows; i++ )
3138 {
3139 m_rowHeights.Insert( m_defaultRowHeight, pos );
3140 m_rowBottoms.Insert( 0, pos );
3141 }
3142 m_numRows += numRows;
3143
3144 int bottom = 0;
3145 if ( pos > 0 ) bottom = m_rowBottoms[pos-1];
3146
3147 for ( i = pos; i < m_numRows; i++ )
3148 {
3149 bottom += m_rowHeights[i];
3150 m_rowBottoms[i] = bottom;
3151 }
3152 CalcDimensions();
3153 }
3154 return TRUE;
3155
3156 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED:
3157 {
3158 int numRows = msg.GetCommandInt();
3159 for ( i = 0; i < numRows; i++ )
3160 {
3161 m_rowHeights.Add( m_defaultRowHeight );
3162 m_rowBottoms.Add( 0 );
3163 }
3164
3165 int oldNumRows = m_numRows;
3166 m_numRows += numRows;
3167
3168 int bottom = 0;
3169 if ( oldNumRows > 0 ) bottom = m_rowBottoms[oldNumRows-1];
3170
3171 for ( i = oldNumRows; i < m_numRows; i++ )
3172 {
3173 bottom += m_rowHeights[i];
3174 m_rowBottoms[i] = bottom;
3175 }
3176 CalcDimensions();
3177 }
3178 return TRUE;
3179
3180 case wxGRIDTABLE_NOTIFY_ROWS_DELETED:
3181 {
3182 size_t pos = msg.GetCommandInt();
3183 int numRows = msg.GetCommandInt2();
3184 for ( i = 0; i < numRows; i++ )
3185 {
3186 m_rowHeights.Remove( pos );
3187 m_rowBottoms.Remove( pos );
3188 }
3189 m_numRows -= numRows;
3190
3191 if ( !m_numRows )
3192 {
3193 m_numCols = 0;
3194 m_colWidths.Clear();
3195 m_colRights.Clear();
3196 m_currentCellCoords = wxGridNoCellCoords;
3197 }
3198 else
3199 {
3200 if ( m_currentCellCoords.GetRow() >= m_numRows )
3201 m_currentCellCoords.Set( 0, 0 );
3202
3203 int h = 0;
3204 for ( i = 0; i < m_numRows; i++ )
3205 {
3206 h += m_rowHeights[i];
3207 m_rowBottoms[i] = h;
3208 }
3209 }
3210
3211 CalcDimensions();
3212 }
3213 return TRUE;
3214
3215 case wxGRIDTABLE_NOTIFY_COLS_INSERTED:
3216 {
3217 size_t pos = msg.GetCommandInt();
3218 int numCols = msg.GetCommandInt2();
3219 for ( i = 0; i < numCols; i++ )
3220 {
3221 m_colWidths.Insert( m_defaultColWidth, pos );
3222 m_colRights.Insert( 0, pos );
3223 }
3224 m_numCols += numCols;
3225
3226 int right = 0;
3227 if ( pos > 0 ) right = m_colRights[pos-1];
3228
3229 for ( i = pos; i < m_numCols; i++ )
3230 {
3231 right += m_colWidths[i];
3232 m_colRights[i] = right;
3233 }
3234 CalcDimensions();
3235 }
3236 return TRUE;
3237
3238 case wxGRIDTABLE_NOTIFY_COLS_APPENDED:
3239 {
3240 int numCols = msg.GetCommandInt();
3241 for ( i = 0; i < numCols; i++ )
3242 {
3243 m_colWidths.Add( m_defaultColWidth );
3244 m_colRights.Add( 0 );
3245 }
3246
3247 int oldNumCols = m_numCols;
3248 m_numCols += numCols;
3249
3250 int right = 0;
3251 if ( oldNumCols > 0 ) right = m_colRights[oldNumCols-1];
3252
3253 for ( i = oldNumCols; i < m_numCols; i++ )
3254 {
3255 right += m_colWidths[i];
3256 m_colRights[i] = right;
3257 }
3258 CalcDimensions();
3259 }
3260 return TRUE;
3261
3262 case wxGRIDTABLE_NOTIFY_COLS_DELETED:
3263 {
3264 size_t pos = msg.GetCommandInt();
3265 int numCols = msg.GetCommandInt2();
3266 for ( i = 0; i < numCols; i++ )
3267 {
3268 m_colWidths.Remove( pos );
3269 m_colRights.Remove( pos );
3270 }
3271 m_numCols -= numCols;
3272
3273 if ( !m_numCols )
3274 {
3275 #if 0 // leave the row alone here so that AppendCols will work subsequently
3276 m_numRows = 0;
3277 m_rowHeights.Clear();
3278 m_rowBottoms.Clear();
3279 #endif
3280 m_currentCellCoords = wxGridNoCellCoords;
3281 }
3282 else
3283 {
3284 if ( m_currentCellCoords.GetCol() >= m_numCols )
3285 m_currentCellCoords.Set( 0, 0 );
3286
3287 int w = 0;
3288 for ( i = 0; i < m_numCols; i++ )
3289 {
3290 w += m_colWidths[i];
3291 m_colRights[i] = w;
3292 }
3293 }
3294 CalcDimensions();
3295 }
3296 return TRUE;
3297 }
3298
3299 return FALSE;
3300 }
3301
3302
3303 void wxGrid::CalcRowLabelsExposed( wxRegion& reg )
3304 {
3305 wxRegionIterator iter( reg );
3306 wxRect r;
3307
3308 m_rowLabelsExposed.Empty();
3309
3310 int top, bottom;
3311 while ( iter )
3312 {
3313 r = iter.GetRect();
3314
3315 // TODO: remove this when we can...
3316 // There is a bug in wxMotif that gives garbage update
3317 // rectangles if you jump-scroll a long way by clicking the
3318 // scrollbar with middle button. This is a work-around
3319 //
3320 #if defined(__WXMOTIF__)
3321 int cw, ch;
3322 m_gridWin->GetClientSize( &cw, &ch );
3323 if ( r.GetTop() > ch ) r.SetTop( 0 );
3324 r.SetBottom( wxMin( r.GetBottom(), ch ) );
3325 #endif
3326
3327 // logical bounds of update region
3328 //
3329 int dummy;
3330 CalcUnscrolledPosition( 0, r.GetTop(), &dummy, &top );
3331 CalcUnscrolledPosition( 0, r.GetBottom(), &dummy, &bottom );
3332
3333 // find the row labels within these bounds
3334 //
3335 int row;
3336 for ( row = 0; row < m_numRows; row++ )
3337 {
3338 if ( GetRowBottom(row) < top )
3339 continue;
3340
3341 if ( GetRowTop(row) > bottom )
3342 break;
3343
3344 m_rowLabelsExposed.Add( row );
3345 }
3346
3347 iter++ ;
3348 }
3349 }
3350
3351
3352 void wxGrid::CalcColLabelsExposed( wxRegion& reg )
3353 {
3354 wxRegionIterator iter( reg );
3355 wxRect r;
3356
3357 m_colLabelsExposed.Empty();
3358
3359 int left, right;
3360 while ( iter )
3361 {
3362 r = iter.GetRect();
3363
3364 // TODO: remove this when we can...
3365 // There is a bug in wxMotif that gives garbage update
3366 // rectangles if you jump-scroll a long way by clicking the
3367 // scrollbar with middle button. This is a work-around
3368 //
3369 #if defined(__WXMOTIF__)
3370 int cw, ch;
3371 m_gridWin->GetClientSize( &cw, &ch );
3372 if ( r.GetLeft() > cw ) r.SetLeft( 0 );
3373 r.SetRight( wxMin( r.GetRight(), cw ) );
3374 #endif
3375
3376 // logical bounds of update region
3377 //
3378 int dummy;
3379 CalcUnscrolledPosition( r.GetLeft(), 0, &left, &dummy );
3380 CalcUnscrolledPosition( r.GetRight(), 0, &right, &dummy );
3381
3382 // find the cells within these bounds
3383 //
3384 int col;
3385 for ( col = 0; col < m_numCols; col++ )
3386 {
3387 if ( GetColRight(col) < left )
3388 continue;
3389
3390 if ( GetColLeft(col) > right )
3391 break;
3392
3393 m_colLabelsExposed.Add( col );
3394 }
3395
3396 iter++ ;
3397 }
3398 }
3399
3400
3401 void wxGrid::CalcCellsExposed( wxRegion& reg )
3402 {
3403 wxRegionIterator iter( reg );
3404 wxRect r;
3405
3406 m_cellsExposed.Empty();
3407 m_rowsExposed.Empty();
3408 m_colsExposed.Empty();
3409
3410 int left, top, right, bottom;
3411 while ( iter )
3412 {
3413 r = iter.GetRect();
3414
3415 // TODO: remove this when we can...
3416 // There is a bug in wxMotif that gives garbage update
3417 // rectangles if you jump-scroll a long way by clicking the
3418 // scrollbar with middle button. This is a work-around
3419 //
3420 #if defined(__WXMOTIF__)
3421 int cw, ch;
3422 m_gridWin->GetClientSize( &cw, &ch );
3423 if ( r.GetTop() > ch ) r.SetTop( 0 );
3424 if ( r.GetLeft() > cw ) r.SetLeft( 0 );
3425 r.SetRight( wxMin( r.GetRight(), cw ) );
3426 r.SetBottom( wxMin( r.GetBottom(), ch ) );
3427 #endif
3428
3429 // logical bounds of update region
3430 //
3431 CalcUnscrolledPosition( r.GetLeft(), r.GetTop(), &left, &top );
3432 CalcUnscrolledPosition( r.GetRight(), r.GetBottom(), &right, &bottom );
3433
3434 // find the cells within these bounds
3435 //
3436 int row, col;
3437 for ( row = 0; row < m_numRows; row++ )
3438 {
3439 if ( GetRowBottom(row) <= top )
3440 continue;
3441
3442 if ( GetRowTop(row) > bottom )
3443 break;
3444
3445 m_rowsExposed.Add( row );
3446
3447 for ( col = 0; col < m_numCols; col++ )
3448 {
3449 if ( GetColRight(col) <= left )
3450 continue;
3451
3452 if ( GetColLeft(col) > right )
3453 break;
3454
3455 if ( m_colsExposed.Index( col ) == wxNOT_FOUND )
3456 m_colsExposed.Add( col );
3457 m_cellsExposed.Add( wxGridCellCoords( row, col ) );
3458 }
3459 }
3460
3461 iter++;
3462 }
3463 }
3464
3465
3466 void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent& event )
3467 {
3468 int x, y, row;
3469 wxPoint pos( event.GetPosition() );
3470 CalcUnscrolledPosition( pos.x, pos.y, &x, &y );
3471
3472 if ( event.Dragging() )
3473 {
3474 m_isDragging = TRUE;
3475
3476 if ( event.LeftIsDown() )
3477 {
3478 switch( m_cursorMode )
3479 {
3480 case WXGRID_CURSOR_RESIZE_ROW:
3481 {
3482 int cw, ch, left, dummy;
3483 m_gridWin->GetClientSize( &cw, &ch );
3484 CalcUnscrolledPosition( 0, 0, &left, &dummy );
3485
3486 wxClientDC dc( m_gridWin );
3487 PrepareDC( dc );
3488 y = wxMax( y,
3489 GetRowTop(m_dragRowOrCol) +
3490 GetRowMinimalHeight(m_dragRowOrCol) );
3491 dc.SetLogicalFunction(wxINVERT);
3492 if ( m_dragLastPos >= 0 )
3493 {
3494 dc.DrawLine( left, m_dragLastPos, left+cw, m_dragLastPos );
3495 }
3496 dc.DrawLine( left, y, left+cw, y );
3497 m_dragLastPos = y;
3498 }
3499 break;
3500
3501 case WXGRID_CURSOR_SELECT_ROW:
3502 if ( (row = YToRow( y )) >= 0 &&
3503 !IsInSelection( row, 0 ) )
3504 {
3505 SelectRow( row, TRUE );
3506 }
3507
3508 // default label to suppress warnings about "enumeration value
3509 // 'xxx' not handled in switch
3510 default:
3511 break;
3512 }
3513 }
3514 return;
3515 }
3516
3517 m_isDragging = FALSE;
3518
3519
3520 // ------------ Entering or leaving the window
3521 //
3522 if ( event.Entering() || event.Leaving() )
3523 {
3524 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_rowLabelWin);
3525 }
3526
3527
3528 // ------------ Left button pressed
3529 //
3530 else if ( event.LeftDown() )
3531 {
3532 // don't send a label click event for a hit on the
3533 // edge of the row label - this is probably the user
3534 // wanting to resize the row
3535 //
3536 if ( YToEdgeOfRow(y) < 0 )
3537 {
3538 row = YToRow(y);
3539 if ( row >= 0 &&
3540 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK, row, -1, event ) )
3541 {
3542 SelectRow( row, event.ShiftDown() );
3543 ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW, m_rowLabelWin);
3544 }
3545 }
3546 else
3547 {
3548 // starting to drag-resize a row
3549 //
3550 if ( CanDragRowSize() )
3551 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW, m_rowLabelWin);
3552 }
3553 }
3554
3555
3556 // ------------ Left double click
3557 //
3558 else if (event.LeftDClick() )
3559 {
3560 if ( YToEdgeOfRow(y) < 0 )
3561 {
3562 row = YToRow(y);
3563 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK, row, -1, event );
3564 }
3565 }
3566
3567
3568 // ------------ Left button released
3569 //
3570 else if ( event.LeftUp() )
3571 {
3572 if ( m_cursorMode == WXGRID_CURSOR_RESIZE_ROW )
3573 {
3574 DoEndDragResizeRow();
3575
3576 // Note: we are ending the event *after* doing
3577 // default processing in this case
3578 //
3579 SendEvent( wxEVT_GRID_ROW_SIZE, m_dragRowOrCol, -1, event );
3580 }
3581
3582 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_rowLabelWin);
3583 m_dragLastPos = -1;
3584 }
3585
3586
3587 // ------------ Right button down
3588 //
3589 else if ( event.RightDown() )
3590 {
3591 row = YToRow(y);
3592 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK, row, -1, event ) )
3593 {
3594 // no default action at the moment
3595 }
3596 }
3597
3598
3599 // ------------ Right double click
3600 //
3601 else if ( event.RightDClick() )
3602 {
3603 row = YToRow(y);
3604 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK, row, -1, event ) )
3605 {
3606 // no default action at the moment
3607 }
3608 }
3609
3610
3611 // ------------ No buttons down and mouse moving
3612 //
3613 else if ( event.Moving() )
3614 {
3615 m_dragRowOrCol = YToEdgeOfRow( y );
3616 if ( m_dragRowOrCol >= 0 )
3617 {
3618 if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
3619 {
3620 // don't capture the mouse yet
3621 if ( CanDragRowSize() )
3622 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW, m_rowLabelWin, FALSE);
3623 }
3624 }
3625 else if ( m_cursorMode != WXGRID_CURSOR_SELECT_CELL )
3626 {
3627 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_rowLabelWin, FALSE);
3628 }
3629 }
3630 }
3631
3632
3633 void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent& event )
3634 {
3635 int x, y, col;
3636 wxPoint pos( event.GetPosition() );
3637 CalcUnscrolledPosition( pos.x, pos.y, &x, &y );
3638
3639 if ( event.Dragging() )
3640 {
3641 m_isDragging = TRUE;
3642
3643 if ( event.LeftIsDown() )
3644 {
3645 switch( m_cursorMode )
3646 {
3647 case WXGRID_CURSOR_RESIZE_COL:
3648 {
3649 int cw, ch, dummy, top;
3650 m_gridWin->GetClientSize( &cw, &ch );
3651 CalcUnscrolledPosition( 0, 0, &dummy, &top );
3652
3653 wxClientDC dc( m_gridWin );
3654 PrepareDC( dc );
3655
3656 x = wxMax( x, GetColLeft(m_dragRowOrCol) +
3657 GetColMinimalWidth(m_dragRowOrCol));
3658 dc.SetLogicalFunction(wxINVERT);
3659 if ( m_dragLastPos >= 0 )
3660 {
3661 dc.DrawLine( m_dragLastPos, top, m_dragLastPos, top+ch );
3662 }
3663 dc.DrawLine( x, top, x, top+ch );
3664 m_dragLastPos = x;
3665 }
3666 break;
3667
3668 case WXGRID_CURSOR_SELECT_COL:
3669 if ( (col = XToCol( x )) >= 0 &&
3670 !IsInSelection( 0, col ) )
3671 {
3672 SelectCol( col, TRUE );
3673 }
3674
3675 // default label to suppress warnings about "enumeration value
3676 // 'xxx' not handled in switch
3677 default:
3678 break;
3679 }
3680 }
3681 return;
3682 }
3683
3684 m_isDragging = FALSE;
3685
3686
3687 // ------------ Entering or leaving the window
3688 //
3689 if ( event.Entering() || event.Leaving() )
3690 {
3691 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_colLabelWin);
3692 }
3693
3694
3695 // ------------ Left button pressed
3696 //
3697 else if ( event.LeftDown() )
3698 {
3699 // don't send a label click event for a hit on the
3700 // edge of the col label - this is probably the user
3701 // wanting to resize the col
3702 //
3703 if ( XToEdgeOfCol(x) < 0 )
3704 {
3705 col = XToCol(x);
3706 if ( col >= 0 &&
3707 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK, -1, col, event ) )
3708 {
3709 SelectCol( col, event.ShiftDown() );
3710 ChangeCursorMode(WXGRID_CURSOR_SELECT_COL, m_colLabelWin);
3711 }
3712 }
3713 else
3714 {
3715 // starting to drag-resize a col
3716 //
3717 if ( CanDragColSize() )
3718 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL, m_colLabelWin);
3719 }
3720 }
3721
3722
3723 // ------------ Left double click
3724 //
3725 if ( event.LeftDClick() )
3726 {
3727 if ( XToEdgeOfCol(x) < 0 )
3728 {
3729 col = XToCol(x);
3730 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK, -1, col, event );
3731 }
3732 }
3733
3734
3735 // ------------ Left button released
3736 //
3737 else if ( event.LeftUp() )
3738 {
3739 if ( m_cursorMode == WXGRID_CURSOR_RESIZE_COL )
3740 {
3741 DoEndDragResizeCol();
3742
3743 // Note: we are ending the event *after* doing
3744 // default processing in this case
3745 //
3746 SendEvent( wxEVT_GRID_COL_SIZE, -1, m_dragRowOrCol, event );
3747 }
3748
3749 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_colLabelWin);
3750 m_dragLastPos = -1;
3751 }
3752
3753
3754 // ------------ Right button down
3755 //
3756 else if ( event.RightDown() )
3757 {
3758 col = XToCol(x);
3759 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK, -1, col, event ) )
3760 {
3761 // no default action at the moment
3762 }
3763 }
3764
3765
3766 // ------------ Right double click
3767 //
3768 else if ( event.RightDClick() )
3769 {
3770 col = XToCol(x);
3771 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK, -1, col, event ) )
3772 {
3773 // no default action at the moment
3774 }
3775 }
3776
3777
3778 // ------------ No buttons down and mouse moving
3779 //
3780 else if ( event.Moving() )
3781 {
3782 m_dragRowOrCol = XToEdgeOfCol( x );
3783 if ( m_dragRowOrCol >= 0 )
3784 {
3785 if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
3786 {
3787 // don't capture the cursor yet
3788 if ( CanDragColSize() )
3789 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL, m_colLabelWin, FALSE);
3790 }
3791 }
3792 else if ( m_cursorMode != WXGRID_CURSOR_SELECT_CELL )
3793 {
3794 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_colLabelWin, FALSE);
3795 }
3796 }
3797 }
3798
3799
3800 void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent& event )
3801 {
3802 if ( event.LeftDown() )
3803 {
3804 // indicate corner label by having both row and
3805 // col args == -1
3806 //
3807 if ( !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK, -1, -1, event ) )
3808 {
3809 SelectAll();
3810 }
3811 }
3812
3813 else if ( event.LeftDClick() )
3814 {
3815 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK, -1, -1, event );
3816 }
3817
3818 else if ( event.RightDown() )
3819 {
3820 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK, -1, -1, event ) )
3821 {
3822 // no default action at the moment
3823 }
3824 }
3825
3826 else if ( event.RightDClick() )
3827 {
3828 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK, -1, -1, event ) )
3829 {
3830 // no default action at the moment
3831 }
3832 }
3833 }
3834
3835 void wxGrid::ChangeCursorMode(CursorMode mode,
3836 wxWindow *win,
3837 bool captureMouse)
3838 {
3839 #ifdef __WXDEBUG__
3840 static const wxChar *cursorModes[] =
3841 {
3842 _T("SELECT_CELL"),
3843 _T("RESIZE_ROW"),
3844 _T("RESIZE_COL"),
3845 _T("SELECT_ROW"),
3846 _T("SELECT_COL")
3847 };
3848
3849 wxLogTrace(_T("grid"),
3850 _T("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
3851 win == m_colLabelWin ? _T("colLabelWin")
3852 : win ? _T("rowLabelWin")
3853 : _T("gridWin"),
3854 cursorModes[m_cursorMode], cursorModes[mode]);
3855 #endif // __WXDEBUG__
3856
3857 if ( mode == m_cursorMode )
3858 return;
3859
3860 if ( !win )
3861 {
3862 // by default use the grid itself
3863 win = m_gridWin;
3864 }
3865
3866 if ( m_winCapture )
3867 {
3868 m_winCapture->ReleaseMouse();
3869 m_winCapture = (wxWindow *)NULL;
3870 }
3871
3872 m_cursorMode = mode;
3873
3874 switch ( m_cursorMode )
3875 {
3876 case WXGRID_CURSOR_RESIZE_ROW:
3877 win->SetCursor( m_rowResizeCursor );
3878 break;
3879
3880 case WXGRID_CURSOR_RESIZE_COL:
3881 win->SetCursor( m_colResizeCursor );
3882 break;
3883
3884 default:
3885 win->SetCursor( *wxSTANDARD_CURSOR );
3886 }
3887
3888 // we need to capture mouse when resizing
3889 bool resize = m_cursorMode == WXGRID_CURSOR_RESIZE_ROW ||
3890 m_cursorMode == WXGRID_CURSOR_RESIZE_COL;
3891
3892 if ( captureMouse && resize )
3893 {
3894 win->CaptureMouse();
3895 m_winCapture = win;
3896 }
3897 }
3898
3899 void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent& event )
3900 {
3901 int x, y;
3902 wxPoint pos( event.GetPosition() );
3903 CalcUnscrolledPosition( pos.x, pos.y, &x, &y );
3904
3905 wxGridCellCoords coords;
3906 XYToCell( x, y, coords );
3907
3908 if ( event.Dragging() )
3909 {
3910 //wxLogDebug("pos(%d, %d) coords(%d, %d)", pos.x, pos.y, coords.GetRow(), coords.GetCol());
3911
3912 // Don't start doing anything until the mouse has been drug at
3913 // least 3 pixels in any direction...
3914 if (! m_isDragging)
3915 {
3916 if (m_startDragPos == wxDefaultPosition)
3917 {
3918 m_startDragPos = pos;
3919 return;
3920 }
3921 if (abs(m_startDragPos.x - pos.x) < 4 && abs(m_startDragPos.y - pos.y) < 4)
3922 return;
3923 }
3924
3925 m_isDragging = TRUE;
3926 if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
3927 {
3928 // Hide the edit control, so it
3929 // won't interfer with drag-shrinking.
3930 if ( IsCellEditControlEnabled() )
3931 {
3932 HideCellEditControl();
3933 SaveEditControlValue();
3934 }
3935
3936 // Have we captured the mouse yet?
3937 if (! m_winCapture)
3938 {
3939 m_winCapture = m_gridWin;
3940 m_winCapture->CaptureMouse();
3941 }
3942
3943 if ( coords != wxGridNoCellCoords )
3944 {
3945 if ( !IsSelection() )
3946 {
3947 SelectBlock( coords, coords );
3948 }
3949 else
3950 {
3951 SelectBlock( m_currentCellCoords, coords );
3952 }
3953
3954 if (! IsVisible(coords))
3955 {
3956 MakeCellVisible(coords);
3957 // TODO: need to introduce a delay or something here. The
3958 // scrolling is way to fast, at least on MSW.
3959 }
3960 }
3961 }
3962 else if ( m_cursorMode == WXGRID_CURSOR_RESIZE_ROW )
3963 {
3964 int cw, ch, left, dummy;
3965 m_gridWin->GetClientSize( &cw, &ch );
3966 CalcUnscrolledPosition( 0, 0, &left, &dummy );
3967
3968 wxClientDC dc( m_gridWin );
3969 PrepareDC( dc );
3970 y = wxMax( y, GetRowTop(m_dragRowOrCol) + WXGRID_MIN_ROW_HEIGHT );
3971 dc.SetLogicalFunction(wxINVERT);
3972 if ( m_dragLastPos >= 0 )
3973 {
3974 dc.DrawLine( left, m_dragLastPos, left+cw, m_dragLastPos );
3975 }
3976 dc.DrawLine( left, y, left+cw, y );
3977 m_dragLastPos = y;
3978 }
3979 else if ( m_cursorMode == WXGRID_CURSOR_RESIZE_COL )
3980 {
3981 int cw, ch, dummy, top;
3982 m_gridWin->GetClientSize( &cw, &ch );
3983 CalcUnscrolledPosition( 0, 0, &dummy, &top );
3984
3985 wxClientDC dc( m_gridWin );
3986 PrepareDC( dc );
3987 x = wxMax( x, GetColLeft(m_dragRowOrCol) +
3988 GetColMinimalWidth(m_dragRowOrCol) );
3989 dc.SetLogicalFunction(wxINVERT);
3990 if ( m_dragLastPos >= 0 )
3991 {
3992 dc.DrawLine( m_dragLastPos, top, m_dragLastPos, top+ch );
3993 }
3994 dc.DrawLine( x, top, x, top+ch );
3995 m_dragLastPos = x;
3996 }
3997
3998 return;
3999 }
4000
4001 m_isDragging = FALSE;
4002 m_startDragPos = wxDefaultPosition;
4003
4004 // VZ: if we do this, the mode is reset to WXGRID_CURSOR_SELECT_CELL
4005 // immediately after it becomes WXGRID_CURSOR_RESIZE_ROW/COL under
4006 // wxGTK
4007 #if 0
4008 if ( event.Entering() || event.Leaving() )
4009 {
4010 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL);
4011 m_gridWin->SetCursor( *wxSTANDARD_CURSOR );
4012 }
4013 else
4014 #endif // 0
4015
4016 // ------------ Left button pressed
4017 //
4018 if ( event.LeftDown() && coords != wxGridNoCellCoords )
4019 {
4020 if ( event.ShiftDown() )
4021 {
4022 SelectBlock( m_currentCellCoords, coords );
4023 }
4024 else if ( XToEdgeOfCol(x) < 0 &&
4025 YToEdgeOfRow(y) < 0 )
4026 {
4027 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_CLICK,
4028 coords.GetRow(),
4029 coords.GetCol(),
4030 event ) )
4031 {
4032 DisableCellEditControl();
4033 MakeCellVisible( coords );
4034
4035 // if this is the second click on this cell then start
4036 // the edit control
4037 if ( m_waitForSlowClick &&
4038 (coords == m_currentCellCoords) &&
4039 CanEnableCellControl())
4040 {
4041 EnableCellEditControl();
4042
4043 wxGridCellAttr* attr = GetCellAttr(m_currentCellCoords);
4044 attr->GetEditor(this, coords.GetRow(), coords.GetCol())->StartingClick();
4045 attr->DecRef();
4046
4047 m_waitForSlowClick = FALSE;
4048 }
4049 else
4050 {
4051 SetCurrentCell( coords );
4052 m_waitForSlowClick = TRUE;
4053 }
4054 }
4055 }
4056 }
4057
4058
4059 // ------------ Left double click
4060 //
4061 else if ( event.LeftDClick() && coords != wxGridNoCellCoords )
4062 {
4063 DisableCellEditControl();
4064
4065 if ( XToEdgeOfCol(x) < 0 && YToEdgeOfRow(y) < 0 )
4066 {
4067 SendEvent( wxEVT_GRID_CELL_LEFT_DCLICK,
4068 coords.GetRow(),
4069 coords.GetCol(),
4070 event );
4071 }
4072 }
4073
4074
4075 // ------------ Left button released
4076 //
4077 else if ( event.LeftUp() )
4078 {
4079 if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
4080 {
4081 if ( IsSelection() )
4082 {
4083 if (m_winCapture)
4084 {
4085 m_winCapture->ReleaseMouse();
4086 m_winCapture = NULL;
4087 }
4088 SendEvent( wxEVT_GRID_RANGE_SELECT, -1, -1, event );
4089 }
4090
4091 // Show the edit control, if it has been hidden for
4092 // drag-shrinking.
4093 ShowCellEditControl();
4094 }
4095 else if ( m_cursorMode == WXGRID_CURSOR_RESIZE_ROW )
4096 {
4097 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL);
4098 DoEndDragResizeRow();
4099
4100 // Note: we are ending the event *after* doing
4101 // default processing in this case
4102 //
4103 SendEvent( wxEVT_GRID_ROW_SIZE, m_dragRowOrCol, -1, event );
4104 }
4105 else if ( m_cursorMode == WXGRID_CURSOR_RESIZE_COL )
4106 {
4107 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL);
4108 DoEndDragResizeCol();
4109
4110 // Note: we are ending the event *after* doing
4111 // default processing in this case
4112 //
4113 SendEvent( wxEVT_GRID_COL_SIZE, -1, m_dragRowOrCol, event );
4114 }
4115
4116 m_dragLastPos = -1;
4117 }
4118
4119
4120 // ------------ Right button down
4121 //
4122 else if ( event.RightDown() && coords != wxGridNoCellCoords )
4123 {
4124 DisableCellEditControl();
4125 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_CLICK,
4126 coords.GetRow(),
4127 coords.GetCol(),
4128 event ) )
4129 {
4130 // no default action at the moment
4131 }
4132 }
4133
4134
4135 // ------------ Right double click
4136 //
4137 else if ( event.RightDClick() && coords != wxGridNoCellCoords )
4138 {
4139 DisableCellEditControl();
4140 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_DCLICK,
4141 coords.GetRow(),
4142 coords.GetCol(),
4143 event ) )
4144 {
4145 // no default action at the moment
4146 }
4147 }
4148
4149 // ------------ Moving and no button action
4150 //
4151 else if ( event.Moving() && !event.IsButton() )
4152 {
4153 int dragRow = YToEdgeOfRow( y );
4154 int dragCol = XToEdgeOfCol( x );
4155
4156 // Dragging on the corner of a cell to resize in both
4157 // directions is not implemented yet...
4158 //
4159 if ( dragRow >= 0 && dragCol >= 0 )
4160 {
4161 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL);
4162 return;
4163 }
4164
4165 if ( dragRow >= 0 )
4166 {
4167 m_dragRowOrCol = dragRow;
4168
4169 if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
4170 {
4171 if ( CanDragRowSize() && CanDragGridSize() )
4172 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW);
4173 }
4174
4175 if ( dragCol >= 0 )
4176 {
4177 m_dragRowOrCol = dragCol;
4178 }
4179
4180 return;
4181 }
4182
4183 if ( dragCol >= 0 )
4184 {
4185 m_dragRowOrCol = dragCol;
4186
4187 if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
4188 {
4189 if ( CanDragColSize() && CanDragGridSize() )
4190 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL);
4191 }
4192
4193 return;
4194 }
4195
4196 // Neither on a row or col edge
4197 //
4198 if ( m_cursorMode != WXGRID_CURSOR_SELECT_CELL )
4199 {
4200 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL);
4201 }
4202 }
4203 }
4204
4205
4206 void wxGrid::DoEndDragResizeRow()
4207 {
4208 if ( m_dragLastPos >= 0 )
4209 {
4210 // erase the last line and resize the row
4211 //
4212 int cw, ch, left, dummy;
4213 m_gridWin->GetClientSize( &cw, &ch );
4214 CalcUnscrolledPosition( 0, 0, &left, &dummy );
4215
4216 wxClientDC dc( m_gridWin );
4217 PrepareDC( dc );
4218 dc.SetLogicalFunction( wxINVERT );
4219 dc.DrawLine( left, m_dragLastPos, left+cw, m_dragLastPos );
4220 HideCellEditControl();
4221 SaveEditControlValue();
4222
4223 int rowTop = GetRowTop(m_dragRowOrCol);
4224 SetRowSize( m_dragRowOrCol,
4225 wxMax( m_dragLastPos - rowTop, WXGRID_MIN_ROW_HEIGHT ) );
4226
4227 if ( !GetBatchCount() )
4228 {
4229 // Only needed to get the correct rect.y:
4230 wxRect rect ( CellToRect( m_dragRowOrCol, 0 ) );
4231 rect.x = 0;
4232 CalcScrolledPosition(0, rect.y, &dummy, &rect.y);
4233 rect.width = m_rowLabelWidth;
4234 rect.height = ch - rect.y;
4235 m_rowLabelWin->Refresh( TRUE, &rect );
4236 rect.width = cw;
4237 m_gridWin->Refresh( FALSE, &rect );
4238 }
4239
4240 ShowCellEditControl();
4241 }
4242 }
4243
4244
4245 void wxGrid::DoEndDragResizeCol()
4246 {
4247 if ( m_dragLastPos >= 0 )
4248 {
4249 // erase the last line and resize the col
4250 //
4251 int cw, ch, dummy, top;
4252 m_gridWin->GetClientSize( &cw, &ch );
4253 CalcUnscrolledPosition( 0, 0, &dummy, &top );
4254
4255 wxClientDC dc( m_gridWin );
4256 PrepareDC( dc );
4257 dc.SetLogicalFunction( wxINVERT );
4258 dc.DrawLine( m_dragLastPos, top, m_dragLastPos, top+ch );
4259 HideCellEditControl();
4260 SaveEditControlValue();
4261
4262 int colLeft = GetColLeft(m_dragRowOrCol);
4263 SetColSize( m_dragRowOrCol,
4264 wxMax( m_dragLastPos - colLeft,
4265 GetColMinimalWidth(m_dragRowOrCol) ) );
4266
4267 if ( !GetBatchCount() )
4268 {
4269 // Only needed to get the correct rect.x:
4270 wxRect rect ( CellToRect( 0, m_dragRowOrCol ) );
4271 rect.y = 0;
4272 CalcScrolledPosition(rect.x, 0, &rect.x, &dummy);
4273 rect.width = cw - rect.x;
4274 rect.height = m_colLabelHeight;
4275 m_colLabelWin->Refresh( TRUE, &rect );
4276 rect.height = ch;
4277 m_gridWin->Refresh( FALSE, &rect );
4278 }
4279
4280 ShowCellEditControl();
4281 }
4282 }
4283
4284
4285
4286 //
4287 // ------ interaction with data model
4288 //
4289 bool wxGrid::ProcessTableMessage( wxGridTableMessage& msg )
4290 {
4291 switch ( msg.GetId() )
4292 {
4293 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES:
4294 return GetModelValues();
4295
4296 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES:
4297 return SetModelValues();
4298
4299 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED:
4300 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED:
4301 case wxGRIDTABLE_NOTIFY_ROWS_DELETED:
4302 case wxGRIDTABLE_NOTIFY_COLS_INSERTED:
4303 case wxGRIDTABLE_NOTIFY_COLS_APPENDED:
4304 case wxGRIDTABLE_NOTIFY_COLS_DELETED:
4305 return Redimension( msg );
4306
4307 default:
4308 return FALSE;
4309 }
4310 }
4311
4312
4313
4314 // The behaviour of this function depends on the grid table class
4315 // Clear() function. For the default wxGridStringTable class the
4316 // behavious is to replace all cell contents with wxEmptyString but
4317 // not to change the number of rows or cols.
4318 //
4319 void wxGrid::ClearGrid()
4320 {
4321 if ( m_table )
4322 {
4323 if (IsCellEditControlEnabled())
4324 DisableCellEditControl();
4325
4326 m_table->Clear();
4327 if ( !GetBatchCount() ) m_gridWin->Refresh();
4328 }
4329 }
4330
4331
4332 bool wxGrid::InsertRows( int pos, int numRows, bool WXUNUSED(updateLabels) )
4333 {
4334 // TODO: something with updateLabels flag
4335
4336 if ( !m_created )
4337 {
4338 wxFAIL_MSG( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") );
4339 return FALSE;
4340 }
4341
4342 if ( m_table )
4343 {
4344 if (IsCellEditControlEnabled())
4345 DisableCellEditControl();
4346
4347 bool ok = m_table->InsertRows( pos, numRows );
4348
4349 // the table will have sent the results of the insert row
4350 // operation to this view object as a grid table message
4351 //
4352 if ( ok )
4353 {
4354 if ( m_numCols == 0 )
4355 {
4356 m_table->AppendCols( WXGRID_DEFAULT_NUMBER_COLS );
4357 //
4358 // TODO: perhaps instead of appending the default number of cols
4359 // we should remember what the last non-zero number of cols was ?
4360 //
4361 }
4362
4363 if ( m_currentCellCoords == wxGridNoCellCoords )
4364 {
4365 // if we have just inserted cols into an empty grid the current
4366 // cell will be undefined...
4367 //
4368 SetCurrentCell( 0, 0 );
4369 }
4370
4371 ClearSelection();
4372 if ( !GetBatchCount() ) Refresh();
4373 }
4374
4375 return ok;
4376 }
4377 else
4378 {
4379 return FALSE;
4380 }
4381 }
4382
4383
4384 bool wxGrid::AppendRows( int numRows, bool WXUNUSED(updateLabels) )
4385 {
4386 // TODO: something with updateLabels flag
4387
4388 if ( !m_created )
4389 {
4390 wxFAIL_MSG( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") );
4391 return FALSE;
4392 }
4393
4394 if ( m_table && m_table->AppendRows( numRows ) )
4395 {
4396 if ( m_currentCellCoords == wxGridNoCellCoords )
4397 {
4398 // if we have just inserted cols into an empty grid the current
4399 // cell will be undefined...
4400 //
4401 SetCurrentCell( 0, 0 );
4402 }
4403
4404 // the table will have sent the results of the append row
4405 // operation to this view object as a grid table message
4406 //
4407 ClearSelection();
4408 if ( !GetBatchCount() ) Refresh();
4409 return TRUE;
4410 }
4411 else
4412 {
4413 return FALSE;
4414 }
4415 }
4416
4417
4418 bool wxGrid::DeleteRows( int pos, int numRows, bool WXUNUSED(updateLabels) )
4419 {
4420 // TODO: something with updateLabels flag
4421
4422 if ( !m_created )
4423 {
4424 wxFAIL_MSG( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") );
4425 return FALSE;
4426 }
4427
4428 if ( m_table )
4429 {
4430 if (IsCellEditControlEnabled())
4431 DisableCellEditControl();
4432
4433 if (m_table->DeleteRows( pos, numRows ))
4434 {
4435
4436 // the table will have sent the results of the delete row
4437 // operation to this view object as a grid table message
4438 //
4439 ClearSelection();
4440 if ( !GetBatchCount() ) Refresh();
4441 return TRUE;
4442 }
4443 }
4444 return FALSE;
4445 }
4446
4447
4448 bool wxGrid::InsertCols( int pos, int numCols, bool WXUNUSED(updateLabels) )
4449 {
4450 // TODO: something with updateLabels flag
4451
4452 if ( !m_created )
4453 {
4454 wxFAIL_MSG( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") );
4455 return FALSE;
4456 }
4457
4458 if ( m_table )
4459 {
4460 if (IsCellEditControlEnabled())
4461 DisableCellEditControl();
4462
4463 bool ok = m_table->InsertCols( pos, numCols );
4464
4465 // the table will have sent the results of the insert col
4466 // operation to this view object as a grid table message
4467 //
4468 if ( ok )
4469 {
4470 if ( m_currentCellCoords == wxGridNoCellCoords )
4471 {
4472 // if we have just inserted cols into an empty grid the current
4473 // cell will be undefined...
4474 //
4475 SetCurrentCell( 0, 0 );
4476 }
4477
4478 ClearSelection();
4479 if ( !GetBatchCount() ) Refresh();
4480 }
4481
4482 return ok;
4483 }
4484 else
4485 {
4486 return FALSE;
4487 }
4488 }
4489
4490
4491 bool wxGrid::AppendCols( int numCols, bool WXUNUSED(updateLabels) )
4492 {
4493 // TODO: something with updateLabels flag
4494
4495 if ( !m_created )
4496 {
4497 wxFAIL_MSG( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") );
4498 return FALSE;
4499 }
4500
4501 if ( m_table && m_table->AppendCols( numCols ) )
4502 {
4503 // the table will have sent the results of the append col
4504 // operation to this view object as a grid table message
4505 //
4506 if ( m_currentCellCoords == wxGridNoCellCoords )
4507 {
4508 // if we have just inserted cols into an empty grid the current
4509 // cell will be undefined...
4510 //
4511 SetCurrentCell( 0, 0 );
4512 }
4513
4514 ClearSelection();
4515 if ( !GetBatchCount() ) Refresh();
4516 return TRUE;
4517 }
4518 else
4519 {
4520 return FALSE;
4521 }
4522 }
4523
4524
4525 bool wxGrid::DeleteCols( int pos, int numCols, bool WXUNUSED(updateLabels) )
4526 {
4527 // TODO: something with updateLabels flag
4528
4529 if ( !m_created )
4530 {
4531 wxFAIL_MSG( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") );
4532 return FALSE;
4533 }
4534
4535 if ( m_table )
4536 {
4537 if (IsCellEditControlEnabled())
4538 DisableCellEditControl();
4539
4540 if ( m_table->DeleteCols( pos, numCols ) )
4541 {
4542 // the table will have sent the results of the delete col
4543 // operation to this view object as a grid table message
4544 //
4545 ClearSelection();
4546 if ( !GetBatchCount() ) Refresh();
4547 return TRUE;
4548 }
4549 }
4550 return FALSE;
4551 }
4552
4553
4554
4555 //
4556 // ----- event handlers
4557 //
4558
4559 // Generate a grid event based on a mouse event and
4560 // return the result of ProcessEvent()
4561 //
4562 bool wxGrid::SendEvent( const wxEventType type,
4563 int row, int col,
4564 wxMouseEvent& mouseEv )
4565 {
4566 if ( type == wxEVT_GRID_ROW_SIZE || type == wxEVT_GRID_COL_SIZE )
4567 {
4568 int rowOrCol = (row == -1 ? col : row);
4569
4570 wxGridSizeEvent gridEvt( GetId(),
4571 type,
4572 this,
4573 rowOrCol,
4574 mouseEv.GetX(), mouseEv.GetY(),
4575 mouseEv.ControlDown(),
4576 mouseEv.ShiftDown(),
4577 mouseEv.AltDown(),
4578 mouseEv.MetaDown() );
4579
4580 return GetEventHandler()->ProcessEvent(gridEvt);
4581 }
4582 else if ( type == wxEVT_GRID_RANGE_SELECT )
4583 {
4584 wxGridRangeSelectEvent gridEvt( GetId(),
4585 type,
4586 this,
4587 m_selectedTopLeft,
4588 m_selectedBottomRight,
4589 mouseEv.ControlDown(),
4590 mouseEv.ShiftDown(),
4591 mouseEv.AltDown(),
4592 mouseEv.MetaDown() );
4593
4594 return GetEventHandler()->ProcessEvent(gridEvt);
4595 }
4596 else
4597 {
4598 wxGridEvent gridEvt( GetId(),
4599 type,
4600 this,
4601 row, col,
4602 mouseEv.GetX(), mouseEv.GetY(),
4603 mouseEv.ControlDown(),
4604 mouseEv.ShiftDown(),
4605 mouseEv.AltDown(),
4606 mouseEv.MetaDown() );
4607
4608 return GetEventHandler()->ProcessEvent(gridEvt);
4609 }
4610 }
4611
4612
4613 // Generate a grid event of specified type and return the result
4614 // of ProcessEvent().
4615 //
4616 bool wxGrid::SendEvent( const wxEventType type,
4617 int row, int col )
4618 {
4619 if ( type == wxEVT_GRID_ROW_SIZE || type == wxEVT_GRID_COL_SIZE )
4620 {
4621 int rowOrCol = (row == -1 ? col : row);
4622
4623 wxGridSizeEvent gridEvt( GetId(),
4624 type,
4625 this,
4626 rowOrCol );
4627
4628 return GetEventHandler()->ProcessEvent(gridEvt);
4629 }
4630 else
4631 {
4632 wxGridEvent gridEvt( GetId(),
4633 type,
4634 this,
4635 row, col );
4636
4637 return GetEventHandler()->ProcessEvent(gridEvt);
4638 }
4639 }
4640
4641
4642 void wxGrid::OnPaint( wxPaintEvent& WXUNUSED(event) )
4643 {
4644 wxPaintDC dc( this );
4645
4646 if ( m_currentCellCoords == wxGridNoCellCoords &&
4647 m_numRows && m_numCols )
4648 {
4649 m_currentCellCoords.Set(0, 0);
4650 ShowCellEditControl();
4651 }
4652
4653 m_displayed = TRUE;
4654 }
4655
4656
4657 // This is just here to make sure that CalcDimensions gets called when
4658 // the grid view is resized... then the size event is skipped to allow
4659 // the box sizers to handle everything
4660 //
4661 void wxGrid::OnSize( wxSizeEvent& event )
4662 {
4663 CalcWindowSizes();
4664 CalcDimensions();
4665 }
4666
4667
4668 void wxGrid::OnKeyDown( wxKeyEvent& event )
4669 {
4670 if ( m_inOnKeyDown )
4671 {
4672 // shouldn't be here - we are going round in circles...
4673 //
4674 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") );
4675 }
4676
4677 m_inOnKeyDown = TRUE;
4678
4679 // propagate the event up and see if it gets processed
4680 //
4681 wxWindow *parent = GetParent();
4682 wxKeyEvent keyEvt( event );
4683 keyEvt.SetEventObject( parent );
4684
4685 if ( !parent->GetEventHandler()->ProcessEvent( keyEvt ) )
4686 {
4687
4688 // TODO: Should also support Shift-cursor keys for
4689 // extending the selection. Maybe add a flag to
4690 // MoveCursorXXX() and MoveCursorXXXBlock() and
4691 // just send event.ShiftDown().
4692
4693 // try local handlers
4694 //
4695 switch ( event.KeyCode() )
4696 {
4697 case WXK_UP:
4698 if ( event.ControlDown() )
4699 {
4700 MoveCursorUpBlock();
4701 }
4702 else
4703 {
4704 MoveCursorUp();
4705 }
4706 break;
4707
4708 case WXK_DOWN:
4709 if ( event.ControlDown() )
4710 {
4711 MoveCursorDownBlock();
4712 }
4713 else
4714 {
4715 MoveCursorDown();
4716 }
4717 break;
4718
4719 case WXK_LEFT:
4720 if ( event.ControlDown() )
4721 {
4722 MoveCursorLeftBlock();
4723 }
4724 else
4725 {
4726 MoveCursorLeft();
4727 }
4728 break;
4729
4730 case WXK_RIGHT:
4731 if ( event.ControlDown() )
4732 {
4733 MoveCursorRightBlock();
4734 }
4735 else
4736 {
4737 MoveCursorRight();
4738 }
4739 break;
4740
4741 case WXK_RETURN:
4742 if ( event.ControlDown() )
4743 {
4744 event.Skip(); // to let the edit control have the return
4745 }
4746 else
4747 {
4748 MoveCursorDown();
4749 }
4750 break;
4751
4752 case WXK_TAB:
4753 if (event.ShiftDown())
4754 MoveCursorLeft();
4755 else
4756 MoveCursorRight();
4757 break;
4758
4759 case WXK_HOME:
4760 if ( event.ControlDown() )
4761 {
4762 MakeCellVisible( 0, 0 );
4763 SetCurrentCell( 0, 0 );
4764 }
4765 else
4766 {
4767 event.Skip();
4768 }
4769 break;
4770
4771 case WXK_END:
4772 if ( event.ControlDown() )
4773 {
4774 MakeCellVisible( m_numRows-1, m_numCols-1 );
4775 SetCurrentCell( m_numRows-1, m_numCols-1 );
4776 }
4777 else
4778 {
4779 event.Skip();
4780 }
4781 break;
4782
4783 case WXK_PRIOR:
4784 MovePageUp();
4785 break;
4786
4787 case WXK_NEXT:
4788 MovePageDown();
4789 break;
4790
4791 case WXK_SPACE:
4792 if ( !IsEditable() )
4793 {
4794 MoveCursorRight();
4795 break;
4796 }
4797 // Otherwise fall through to default
4798
4799 default:
4800 // alphanumeric keys or F2 (special key just for this) enable
4801 // the cell edit control
4802 if ( !(event.AltDown() ||
4803 event.MetaDown() ||
4804 event.ControlDown()) &&
4805 (isalnum(event.KeyCode()) || event.KeyCode() == WXK_F2) &&
4806 !IsCellEditControlEnabled() &&
4807 CanEnableCellControl() )
4808 {
4809 EnableCellEditControl();
4810 int row = m_currentCellCoords.GetRow();
4811 int col = m_currentCellCoords.GetCol();
4812 wxGridCellAttr* attr = GetCellAttr(row, col);
4813 attr->GetEditor(this, row, col)->StartingKey(event);
4814 attr->DecRef();
4815 }
4816 else
4817 {
4818 // let others process char events with modifiers or all
4819 // char events for readonly cells
4820 event.Skip();
4821 }
4822 break;
4823 }
4824 }
4825
4826 m_inOnKeyDown = FALSE;
4827 }
4828
4829
4830 void wxGrid::OnEraseBackground(wxEraseEvent&)
4831 {
4832 }
4833
4834 void wxGrid::SetCurrentCell( const wxGridCellCoords& coords )
4835 {
4836 if ( SendEvent( wxEVT_GRID_SELECT_CELL, coords.GetRow(), coords.GetCol() ) )
4837 {
4838 // the event has been intercepted - do nothing
4839 return;
4840 }
4841
4842 if ( m_displayed &&
4843 m_currentCellCoords != wxGridNoCellCoords )
4844 {
4845 HideCellEditControl();
4846 DisableCellEditControl();
4847
4848 // Clear the old current cell highlight
4849 wxRect r = BlockToDeviceRect(m_currentCellCoords, m_currentCellCoords);
4850
4851 // Otherwise refresh redraws the highlight!
4852 m_currentCellCoords = coords;
4853
4854 m_gridWin->Refresh( FALSE, &r );
4855 }
4856
4857 m_currentCellCoords = coords;
4858
4859 if ( m_displayed )
4860 {
4861 wxClientDC dc(m_gridWin);
4862 PrepareDC(dc);
4863
4864 wxGridCellAttr* attr = GetCellAttr(coords);
4865 DrawCellHighlight(dc, attr);
4866 attr->DecRef();
4867
4868 if ( IsSelection() )
4869 {
4870 wxRect r( SelectionToDeviceRect() );
4871 ClearSelection();
4872 if ( !GetBatchCount() ) m_gridWin->Refresh( FALSE, &r );
4873 }
4874 }
4875 }
4876
4877
4878 //
4879 // ------ functions to get/send data (see also public functions)
4880 //
4881
4882 bool wxGrid::GetModelValues()
4883 {
4884 if ( m_table )
4885 {
4886 // all we need to do is repaint the grid
4887 //
4888 m_gridWin->Refresh();
4889 return TRUE;
4890 }
4891
4892 return FALSE;
4893 }
4894
4895
4896 bool wxGrid::SetModelValues()
4897 {
4898 int row, col;
4899
4900 if ( m_table )
4901 {
4902 for ( row = 0; row < m_numRows; row++ )
4903 {
4904 for ( col = 0; col < m_numCols; col++ )
4905 {
4906 m_table->SetValue( row, col, GetCellValue(row, col) );
4907 }
4908 }
4909
4910 return TRUE;
4911 }
4912
4913 return FALSE;
4914 }
4915
4916
4917
4918 // Note - this function only draws cells that are in the list of
4919 // exposed cells (usually set from the update region by
4920 // CalcExposedCells)
4921 //
4922 void wxGrid::DrawGridCellArea( wxDC& dc )
4923 {
4924 if ( !m_numRows || !m_numCols ) return;
4925
4926 size_t i;
4927 size_t numCells = m_cellsExposed.GetCount();
4928
4929 for ( i = 0; i < numCells; i++ )
4930 {
4931 DrawCell( dc, m_cellsExposed[i] );
4932 }
4933 }
4934
4935
4936 void wxGrid::DrawGridSpace( wxDC& dc )
4937 {
4938 if ( m_numRows && m_numCols )
4939 {
4940 int cw, ch;
4941 m_gridWin->GetClientSize( &cw, &ch );
4942
4943 int right, bottom;
4944 CalcUnscrolledPosition( cw, ch, &right, &bottom );
4945
4946 if ( right > GetColRight(m_numCols-1) ||
4947 bottom > GetRowBottom(m_numRows-1) )
4948 {
4949 int left, top;
4950 CalcUnscrolledPosition( 0, 0, &left, &top );
4951
4952 dc.SetBrush( wxBrush(GetDefaultCellBackgroundColour(), wxSOLID) );
4953 dc.SetPen( *wxTRANSPARENT_PEN );
4954
4955 if ( right > GetColRight(m_numCols-1) )
4956 dc.DrawRectangle( GetColRight(m_numCols-1), top,
4957 right - GetColRight(m_numCols-1), ch );
4958
4959 if ( bottom > GetRowBottom(m_numRows-1) )
4960 dc.DrawRectangle( left, GetRowBottom(m_numRows-1),
4961 cw, bottom - GetRowBottom(m_numRows-1) );
4962 }
4963 }
4964 }
4965
4966
4967 void wxGrid::DrawCell( wxDC& dc, const wxGridCellCoords& coords )
4968 {
4969 int row = coords.GetRow();
4970 int col = coords.GetCol();
4971
4972 if ( GetColWidth(col) <= 0 || GetRowHeight(row) <= 0 )
4973 return;
4974
4975 // we draw the cell border ourselves
4976 #if !WXGRID_DRAW_LINES
4977 if ( m_gridLinesEnabled )
4978 DrawCellBorder( dc, coords );
4979 #endif
4980
4981 wxGridCellAttr* attr = GetCellAttr(row, col);
4982
4983 bool isCurrent = coords == m_currentCellCoords;
4984
4985 wxRect rect;
4986 rect.x = GetColLeft(col);
4987 rect.y = GetRowTop(row);
4988 rect.width = GetColWidth(col) - 1;
4989 rect.height = GetRowHeight(row) - 1;
4990
4991 // if the editor is shown, we should use it and not the renderer
4992 if ( isCurrent && IsCellEditControlEnabled() )
4993 {
4994 attr->GetEditor(this, row, col)->PaintBackground(rect, attr);
4995 }
4996 else
4997 {
4998 // but all the rest is drawn by the cell renderer and hence may be
4999 // customized
5000 attr->GetRenderer(this, row, col)->
5001 Draw(*this, *attr, dc, rect, row, col, IsInSelection(coords));
5002
5003 }
5004
5005 attr->DecRef();
5006 }
5007
5008 void wxGrid::DrawCellHighlight( wxDC& dc, const wxGridCellAttr *attr )
5009 {
5010 int row = m_currentCellCoords.GetRow();
5011 int col = m_currentCellCoords.GetCol();
5012
5013 if ( GetColWidth(col) <= 0 || GetRowHeight(row) <= 0 )
5014 return;
5015
5016 wxRect rect;
5017 rect.x = GetColLeft(col);
5018 rect.y = GetRowTop(row);
5019 rect.width = GetColWidth(col) - 1;
5020 rect.height = GetRowHeight(row) - 1;
5021
5022 // hmmm... what could we do here to show that the cell is disabled?
5023 // for now, I just draw a thinner border than for the other ones, but
5024 // it doesn't look really good
5025 dc.SetPen(wxPen(m_gridLineColour, attr->IsReadOnly() ? 1 : 3, wxSOLID));
5026 dc.SetBrush(*wxTRANSPARENT_BRUSH);
5027
5028 dc.DrawRectangle(rect);
5029
5030 #if 0
5031 // VZ: my experiments with 3d borders...
5032
5033 // how to properly set colours for arbitrary bg?
5034 wxCoord x1 = rect.x,
5035 y1 = rect.y,
5036 x2 = rect.x + rect.width -1,
5037 y2 = rect.y + rect.height -1;
5038
5039 dc.SetPen(*wxWHITE_PEN);
5040 dc.DrawLine(x1, y1, x2, y1);
5041 dc.DrawLine(x1, y1, x1, y2);
5042
5043 dc.DrawLine(x1 + 1, y2 - 1, x2 - 1, y2 - 1);
5044 dc.DrawLine(x2 - 1, y1 + 1, x2 - 1, y2 );
5045
5046 dc.SetPen(*wxBLACK_PEN);
5047 dc.DrawLine(x1, y2, x2, y2);
5048 dc.DrawLine(x2, y1, x2, y2+1);
5049 #endif // 0
5050 }
5051
5052
5053 void wxGrid::DrawCellBorder( wxDC& dc, const wxGridCellCoords& coords )
5054 {
5055 int row = coords.GetRow();
5056 int col = coords.GetCol();
5057 if ( GetColWidth(col) <= 0 || GetRowHeight(row) <= 0 )
5058 return;
5059
5060 dc.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID) );
5061
5062 // right hand border
5063 //
5064 dc.DrawLine( GetColRight(col), GetRowTop(row),
5065 GetColRight(col), GetRowBottom(row) );
5066
5067 // bottom border
5068 //
5069 dc.DrawLine( GetColLeft(col), GetRowBottom(row),
5070 GetColRight(col), GetRowBottom(row) );
5071 }
5072
5073 void wxGrid::DrawHighlight(wxDC& dc)
5074 {
5075 if ( IsCellEditControlEnabled() )
5076 {
5077 // don't show highlight when the edit control is shown
5078 return;
5079 }
5080
5081 // if the active cell was repainted, repaint its highlight too because it
5082 // might have been damaged by the grid lines
5083 size_t count = m_cellsExposed.GetCount();
5084 for ( size_t n = 0; n < count; n++ )
5085 {
5086 if ( m_cellsExposed[n] == m_currentCellCoords )
5087 {
5088 wxGridCellAttr* attr = GetCellAttr(m_currentCellCoords);
5089 DrawCellHighlight(dc, attr);
5090 attr->DecRef();
5091
5092 break;
5093 }
5094 }
5095 }
5096
5097 // TODO: remove this ???
5098 // This is used to redraw all grid lines e.g. when the grid line colour
5099 // has been changed
5100 //
5101 void wxGrid::DrawAllGridLines( wxDC& dc, const wxRegion & reg )
5102 {
5103 if ( !m_gridLinesEnabled ||
5104 !m_numRows ||
5105 !m_numCols ) return;
5106
5107 int top, bottom, left, right;
5108
5109 #ifndef __WXGTK__
5110 if (reg.IsEmpty())
5111 {
5112 int cw, ch;
5113 m_gridWin->GetClientSize(&cw, &ch);
5114
5115 // virtual coords of visible area
5116 //
5117 CalcUnscrolledPosition( 0, 0, &left, &top );
5118 CalcUnscrolledPosition( cw, ch, &right, &bottom );
5119 }
5120 else
5121 {
5122 wxCoord x, y, w, h;
5123 reg.GetBox(x, y, w, h);
5124 CalcUnscrolledPosition( x, y, &left, &top );
5125 CalcUnscrolledPosition( x + w, y + h, &right, &bottom );
5126 }
5127 #else
5128 int cw, ch;
5129 m_gridWin->GetClientSize(&cw, &ch);
5130 CalcUnscrolledPosition( 0, 0, &left, &top );
5131 CalcUnscrolledPosition( cw, ch, &right, &bottom );
5132 #endif
5133
5134 // avoid drawing grid lines past the last row and col
5135 //
5136 right = wxMin( right, GetColRight(m_numCols - 1) );
5137 bottom = wxMin( bottom, GetRowBottom(m_numRows - 1) );
5138
5139 dc.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID) );
5140
5141 // horizontal grid lines
5142 //
5143 int i;
5144 for ( i = 0; i < m_numRows; i++ )
5145 {
5146 int bot = GetRowBottom(i) - 1;
5147
5148 if ( bot > bottom )
5149 {
5150 break;
5151 }
5152
5153 if ( bot >= top )
5154 {
5155 dc.DrawLine( left, bot, right, bot );
5156 }
5157 }
5158
5159
5160 // vertical grid lines
5161 //
5162 for ( i = 0; i < m_numCols; i++ )
5163 {
5164 int colRight = GetColRight(i) - 1;
5165 if ( colRight > right )
5166 {
5167 break;
5168 }
5169
5170 if ( colRight >= left )
5171 {
5172 dc.DrawLine( colRight, top, colRight, bottom );
5173 }
5174 }
5175 }
5176
5177
5178 void wxGrid::DrawRowLabels( wxDC& dc )
5179 {
5180 if ( !m_numRows || !m_numCols ) return;
5181
5182 size_t i;
5183 size_t numLabels = m_rowLabelsExposed.GetCount();
5184
5185 for ( i = 0; i < numLabels; i++ )
5186 {
5187 DrawRowLabel( dc, m_rowLabelsExposed[i] );
5188 }
5189 }
5190
5191
5192 void wxGrid::DrawRowLabel( wxDC& dc, int row )
5193 {
5194 if ( GetRowHeight(row) <= 0 )
5195 return;
5196
5197 int rowTop = GetRowTop(row),
5198 rowBottom = GetRowBottom(row) - 1;
5199
5200 dc.SetPen( *wxBLACK_PEN );
5201 dc.DrawLine( m_rowLabelWidth-1, rowTop,
5202 m_rowLabelWidth-1, rowBottom );
5203
5204 dc.DrawLine( 0, rowBottom, m_rowLabelWidth-1, rowBottom );
5205
5206 dc.SetPen( *wxWHITE_PEN );
5207 dc.DrawLine( 0, rowTop, 0, rowBottom );
5208 dc.DrawLine( 0, rowTop, m_rowLabelWidth-1, rowTop );
5209
5210 dc.SetBackgroundMode( wxTRANSPARENT );
5211 dc.SetTextForeground( GetLabelTextColour() );
5212 dc.SetFont( GetLabelFont() );
5213
5214 int hAlign, vAlign;
5215 GetRowLabelAlignment( &hAlign, &vAlign );
5216
5217 wxRect rect;
5218 rect.SetX( 2 );
5219 rect.SetY( GetRowTop(row) + 2 );
5220 rect.SetWidth( m_rowLabelWidth - 4 );
5221 rect.SetHeight( GetRowHeight(row) - 4 );
5222 DrawTextRectangle( dc, GetRowLabelValue( row ), rect, hAlign, vAlign );
5223 }
5224
5225
5226 void wxGrid::DrawColLabels( wxDC& dc )
5227 {
5228 if ( !m_numRows || !m_numCols ) return;
5229
5230 size_t i;
5231 size_t numLabels = m_colLabelsExposed.GetCount();
5232
5233 for ( i = 0; i < numLabels; i++ )
5234 {
5235 DrawColLabel( dc, m_colLabelsExposed[i] );
5236 }
5237 }
5238
5239
5240 void wxGrid::DrawColLabel( wxDC& dc, int col )
5241 {
5242 if ( GetColWidth(col) <= 0 )
5243 return;
5244
5245 int colLeft = GetColLeft(col),
5246 colRight = GetColRight(col) - 1;
5247
5248 dc.SetPen( *wxBLACK_PEN );
5249 dc.DrawLine( colRight, 0,
5250 colRight, m_colLabelHeight-1 );
5251
5252 dc.DrawLine( colLeft, m_colLabelHeight-1,
5253 colRight, m_colLabelHeight-1 );
5254
5255 dc.SetPen( *wxWHITE_PEN );
5256 dc.DrawLine( colLeft, 0, colLeft, m_colLabelHeight-1 );
5257 dc.DrawLine( colLeft, 0, colRight, 0 );
5258
5259 dc.SetBackgroundMode( wxTRANSPARENT );
5260 dc.SetTextForeground( GetLabelTextColour() );
5261 dc.SetFont( GetLabelFont() );
5262
5263 dc.SetBackgroundMode( wxTRANSPARENT );
5264 dc.SetTextForeground( GetLabelTextColour() );
5265 dc.SetFont( GetLabelFont() );
5266
5267 int hAlign, vAlign;
5268 GetColLabelAlignment( &hAlign, &vAlign );
5269
5270 wxRect rect;
5271 rect.SetX( colLeft + 2 );
5272 rect.SetY( 2 );
5273 rect.SetWidth( GetColWidth(col) - 4 );
5274 rect.SetHeight( m_colLabelHeight - 4 );
5275 DrawTextRectangle( dc, GetColLabelValue( col ), rect, hAlign, vAlign );
5276 }
5277
5278
5279 void wxGrid::DrawTextRectangle( wxDC& dc,
5280 const wxString& value,
5281 const wxRect& rect,
5282 int horizAlign,
5283 int vertAlign )
5284 {
5285 long textWidth, textHeight;
5286 long lineWidth, lineHeight;
5287 wxArrayString lines;
5288
5289 dc.SetClippingRegion( rect );
5290 StringToLines( value, lines );
5291 if ( lines.GetCount() )
5292 {
5293 GetTextBoxSize( dc, lines, &textWidth, &textHeight );
5294 dc.GetTextExtent( lines[0], &lineWidth, &lineHeight );
5295
5296 float x, y;
5297 switch ( horizAlign )
5298 {
5299 case wxRIGHT:
5300 x = rect.x + (rect.width - textWidth - 1);
5301 break;
5302
5303 case wxCENTRE:
5304 x = rect.x + ((rect.width - textWidth)/2);
5305 break;
5306
5307 case wxLEFT:
5308 default:
5309 x = rect.x + 1;
5310 break;
5311 }
5312
5313 switch ( vertAlign )
5314 {
5315 case wxBOTTOM:
5316 y = rect.y + (rect.height - textHeight - 1);
5317 break;
5318
5319 case wxCENTRE:
5320 y = rect.y + ((rect.height - textHeight)/2);
5321 break;
5322
5323 case wxTOP:
5324 default:
5325 y = rect.y + 1;
5326 break;
5327 }
5328
5329 for ( size_t i = 0; i < lines.GetCount(); i++ )
5330 {
5331 dc.DrawText( lines[i], (long)x, (long)y );
5332 y += lineHeight;
5333 }
5334 }
5335
5336 dc.DestroyClippingRegion();
5337 }
5338
5339
5340 // Split multi line text up into an array of strings. Any existing
5341 // contents of the string array are preserved.
5342 //
5343 void wxGrid::StringToLines( const wxString& value, wxArrayString& lines )
5344 {
5345 int startPos = 0;
5346 int pos;
5347 wxString eol = wxTextFile::GetEOL( wxTextFileType_Unix );
5348 wxString tVal = wxTextFile::Translate( value, wxTextFileType_Unix );
5349
5350 while ( startPos < (int)tVal.Length() )
5351 {
5352 pos = tVal.Mid(startPos).Find( eol );
5353 if ( pos < 0 )
5354 {
5355 break;
5356 }
5357 else if ( pos == 0 )
5358 {
5359 lines.Add( wxEmptyString );
5360 }
5361 else
5362 {
5363 lines.Add( value.Mid(startPos, pos) );
5364 }
5365 startPos += pos+1;
5366 }
5367 if ( startPos < (int)value.Length() )
5368 {
5369 lines.Add( value.Mid( startPos ) );
5370 }
5371 }
5372
5373
5374 void wxGrid::GetTextBoxSize( wxDC& dc,
5375 wxArrayString& lines,
5376 long *width, long *height )
5377 {
5378 long w = 0;
5379 long h = 0;
5380 long lineW, lineH;
5381
5382 size_t i;
5383 for ( i = 0; i < lines.GetCount(); i++ )
5384 {
5385 dc.GetTextExtent( lines[i], &lineW, &lineH );
5386 w = wxMax( w, lineW );
5387 h += lineH;
5388 }
5389
5390 *width = w;
5391 *height = h;
5392 }
5393
5394
5395 //
5396 // ------ Edit control functions
5397 //
5398
5399
5400 void wxGrid::EnableEditing( bool edit )
5401 {
5402 // TODO: improve this ?
5403 //
5404 if ( edit != m_editable )
5405 {
5406 m_editable = edit;
5407
5408 // FIXME IMHO this won't disable the edit control if edit == FALSE
5409 // because of the check in the beginning of
5410 // EnableCellEditControl() just below (VZ)
5411 EnableCellEditControl(m_editable);
5412 }
5413 }
5414
5415
5416 void wxGrid::EnableCellEditControl( bool enable )
5417 {
5418 if (! m_editable)
5419 return;
5420
5421 if ( m_currentCellCoords == wxGridNoCellCoords )
5422 SetCurrentCell( 0, 0 );
5423
5424 if ( enable != m_cellEditCtrlEnabled )
5425 {
5426 // TODO allow the app to Veto() this event?
5427 SendEvent(enable ? wxEVT_GRID_EDITOR_SHOWN : wxEVT_GRID_EDITOR_HIDDEN);
5428
5429 if ( enable )
5430 {
5431 // this should be checked by the caller!
5432 wxASSERT_MSG( CanEnableCellControl(),
5433 _T("can't enable editing for this cell!") );
5434
5435 // do it before ShowCellEditControl()
5436 m_cellEditCtrlEnabled = enable;
5437
5438 ShowCellEditControl();
5439 }
5440 else
5441 {
5442 HideCellEditControl();
5443 SaveEditControlValue();
5444
5445 // do it after HideCellEditControl()
5446 m_cellEditCtrlEnabled = enable;
5447 }
5448 }
5449 }
5450
5451 bool wxGrid::IsCurrentCellReadOnly() const
5452 {
5453 // const_cast
5454 wxGridCellAttr* attr = ((wxGrid *)this)->GetCellAttr(m_currentCellCoords);
5455 bool readonly = attr->IsReadOnly();
5456 attr->DecRef();
5457
5458 return readonly;
5459 }
5460
5461 bool wxGrid::CanEnableCellControl() const
5462 {
5463 return m_editable && !IsCurrentCellReadOnly();
5464 }
5465
5466 bool wxGrid::IsCellEditControlEnabled() const
5467 {
5468 // the cell edit control might be disable for all cells or just for the
5469 // current one if it's read only
5470 return m_cellEditCtrlEnabled ? !IsCurrentCellReadOnly() : FALSE;
5471 }
5472
5473 void wxGrid::ShowCellEditControl()
5474 {
5475 if ( IsCellEditControlEnabled() )
5476 {
5477 if ( !IsVisible( m_currentCellCoords ) )
5478 {
5479 return;
5480 }
5481 else
5482 {
5483 wxRect rect = CellToRect( m_currentCellCoords );
5484 int row = m_currentCellCoords.GetRow();
5485 int col = m_currentCellCoords.GetCol();
5486
5487 // convert to scrolled coords
5488 //
5489 CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
5490
5491 // done in PaintBackground()
5492 #if 0
5493 // erase the highlight and the cell contents because the editor
5494 // might not cover the entire cell
5495 wxClientDC dc( m_gridWin );
5496 PrepareDC( dc );
5497 dc.SetBrush(*wxLIGHT_GREY_BRUSH); //wxBrush(attr->GetBackgroundColour(), wxSOLID));
5498 dc.SetPen(*wxTRANSPARENT_PEN);
5499 dc.DrawRectangle(rect);
5500 #endif // 0
5501
5502 // cell is shifted by one pixel
5503 rect.x--;
5504 rect.y--;
5505
5506 wxGridCellAttr* attr = GetCellAttr(row, col);
5507 wxGridCellEditor* editor = attr->GetEditor(this, row, col);
5508 if ( !editor->IsCreated() )
5509 {
5510 editor->Create(m_gridWin, -1,
5511 new wxGridCellEditorEvtHandler(this, editor));
5512 }
5513
5514 editor->Show( TRUE, attr );
5515
5516 editor->SetSize( rect );
5517
5518 editor->BeginEdit(row, col, this);
5519 attr->DecRef();
5520 }
5521 }
5522 }
5523
5524
5525 void wxGrid::HideCellEditControl()
5526 {
5527 if ( IsCellEditControlEnabled() )
5528 {
5529 int row = m_currentCellCoords.GetRow();
5530 int col = m_currentCellCoords.GetCol();
5531
5532 wxGridCellAttr* attr = GetCellAttr(row, col);
5533 attr->GetEditor(this, row, col)->Show( FALSE );
5534 attr->DecRef();
5535 m_gridWin->SetFocus();
5536 }
5537 }
5538
5539
5540 void wxGrid::SaveEditControlValue()
5541 {
5542 if ( IsCellEditControlEnabled() )
5543 {
5544 int row = m_currentCellCoords.GetRow();
5545 int col = m_currentCellCoords.GetCol();
5546
5547 wxGridCellAttr* attr = GetCellAttr(row, col);
5548 wxGridCellEditor* editor = attr->GetEditor(this, row, col);
5549 bool changed = editor->EndEdit(row, col, this);
5550
5551 attr->DecRef();
5552
5553 if (changed)
5554 {
5555 SendEvent( wxEVT_GRID_CELL_CHANGE,
5556 m_currentCellCoords.GetRow(),
5557 m_currentCellCoords.GetCol() );
5558 }
5559 }
5560 }
5561
5562
5563 //
5564 // ------ Grid location functions
5565 // Note that all of these functions work with the logical coordinates of
5566 // grid cells and labels so you will need to convert from device
5567 // coordinates for mouse events etc.
5568 //
5569
5570 void wxGrid::XYToCell( int x, int y, wxGridCellCoords& coords )
5571 {
5572 int row = YToRow(y);
5573 int col = XToCol(x);
5574
5575 if ( row == -1 || col == -1 )
5576 {
5577 coords = wxGridNoCellCoords;
5578 }
5579 else
5580 {
5581 coords.Set( row, col );
5582 }
5583 }
5584
5585
5586 int wxGrid::YToRow( int y )
5587 {
5588 int i;
5589
5590 for ( i = 0; i < m_numRows; i++ )
5591 {
5592 if ( y < GetRowBottom(i) )
5593 return i;
5594 }
5595
5596 return -1;
5597 }
5598
5599
5600 int wxGrid::XToCol( int x )
5601 {
5602 int i;
5603
5604 for ( i = 0; i < m_numCols; i++ )
5605 {
5606 if ( x < GetColRight(i) )
5607 return i;
5608 }
5609
5610 return -1;
5611 }
5612
5613
5614 // return the row number that that the y coord is near the edge of, or
5615 // -1 if not near an edge
5616 //
5617 int wxGrid::YToEdgeOfRow( int y )
5618 {
5619 int i, d;
5620
5621 for ( i = 0; i < m_numRows; i++ )
5622 {
5623 if ( GetRowHeight(i) > WXGRID_LABEL_EDGE_ZONE )
5624 {
5625 d = abs( y - GetRowBottom(i) );
5626 if ( d < WXGRID_LABEL_EDGE_ZONE )
5627 return i;
5628 }
5629 }
5630
5631 return -1;
5632 }
5633
5634
5635 // return the col number that that the x coord is near the edge of, or
5636 // -1 if not near an edge
5637 //
5638 int wxGrid::XToEdgeOfCol( int x )
5639 {
5640 int i, d;
5641
5642 for ( i = 0; i < m_numCols; i++ )
5643 {
5644 if ( GetColWidth(i) > WXGRID_LABEL_EDGE_ZONE )
5645 {
5646 d = abs( x - GetColRight(i) );
5647 if ( d < WXGRID_LABEL_EDGE_ZONE )
5648 return i;
5649 }
5650 }
5651
5652 return -1;
5653 }
5654
5655
5656 wxRect wxGrid::CellToRect( int row, int col )
5657 {
5658 wxRect rect( -1, -1, -1, -1 );
5659
5660 if ( row >= 0 && row < m_numRows &&
5661 col >= 0 && col < m_numCols )
5662 {
5663 rect.x = GetColLeft(col);
5664 rect.y = GetRowTop(row);
5665 rect.width = GetColWidth(col);
5666 rect.height = GetRowHeight(row);
5667 }
5668
5669 return rect;
5670 }
5671
5672
5673 bool wxGrid::IsVisible( int row, int col, bool wholeCellVisible )
5674 {
5675 // get the cell rectangle in logical coords
5676 //
5677 wxRect r( CellToRect( row, col ) );
5678
5679 // convert to device coords
5680 //
5681 int left, top, right, bottom;
5682 CalcScrolledPosition( r.GetLeft(), r.GetTop(), &left, &top );
5683 CalcScrolledPosition( r.GetRight(), r.GetBottom(), &right, &bottom );
5684
5685 // check against the client area of the grid window
5686 //
5687 int cw, ch;
5688 m_gridWin->GetClientSize( &cw, &ch );
5689
5690 if ( wholeCellVisible )
5691 {
5692 // is the cell wholly visible ?
5693 //
5694 return ( left >= 0 && right <= cw &&
5695 top >= 0 && bottom <= ch );
5696 }
5697 else
5698 {
5699 // is the cell partly visible ?
5700 //
5701 return ( ((left >=0 && left < cw) || (right > 0 && right <= cw)) &&
5702 ((top >=0 && top < ch) || (bottom > 0 && bottom <= ch)) );
5703 }
5704 }
5705
5706
5707 // make the specified cell location visible by doing a minimal amount
5708 // of scrolling
5709 //
5710 void wxGrid::MakeCellVisible( int row, int col )
5711 {
5712 int i;
5713 int xpos = -1, ypos = -1;
5714
5715 if ( row >= 0 && row < m_numRows &&
5716 col >= 0 && col < m_numCols )
5717 {
5718 // get the cell rectangle in logical coords
5719 //
5720 wxRect r( CellToRect( row, col ) );
5721
5722 // convert to device coords
5723 //
5724 int left, top, right, bottom;
5725 CalcScrolledPosition( r.GetLeft(), r.GetTop(), &left, &top );
5726 CalcScrolledPosition( r.GetRight(), r.GetBottom(), &right, &bottom );
5727
5728 int cw, ch;
5729 m_gridWin->GetClientSize( &cw, &ch );
5730
5731 if ( top < 0 )
5732 {
5733 ypos = r.GetTop();
5734 }
5735 else if ( bottom > ch )
5736 {
5737 int h = r.GetHeight();
5738 ypos = r.GetTop();
5739 for ( i = row-1; i >= 0; i-- )
5740 {
5741 int rowHeight = GetRowHeight(i);
5742 if ( h + rowHeight > ch )
5743 break;
5744
5745 h += rowHeight;
5746 ypos -= rowHeight;
5747 }
5748
5749 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
5750 // have rounding errors (this is important, because if we do, we
5751 // might not scroll at all and some cells won't be redrawn)
5752 ypos += GRID_SCROLL_LINE / 2;
5753 }
5754
5755 if ( left < 0 )
5756 {
5757 xpos = r.GetLeft();
5758 }
5759 else if ( right > cw )
5760 {
5761 int w = r.GetWidth();
5762 xpos = r.GetLeft();
5763 for ( i = col-1; i >= 0; i-- )
5764 {
5765 int colWidth = GetColWidth(i);
5766 if ( w + colWidth > cw )
5767 break;
5768
5769 w += colWidth;
5770 xpos -= colWidth;
5771 }
5772
5773 // see comment for ypos above
5774 xpos += GRID_SCROLL_LINE / 2;
5775 }
5776
5777 if ( xpos != -1 || ypos != -1 )
5778 {
5779 if ( xpos != -1 ) xpos /= GRID_SCROLL_LINE;
5780 if ( ypos != -1 ) ypos /= GRID_SCROLL_LINE;
5781 Scroll( xpos, ypos );
5782 AdjustScrollbars();
5783 }
5784 }
5785 }
5786
5787
5788 //
5789 // ------ Grid cursor movement functions
5790 //
5791
5792 bool wxGrid::MoveCursorUp()
5793 {
5794 if ( m_currentCellCoords != wxGridNoCellCoords &&
5795 m_currentCellCoords.GetRow() > 0 )
5796 {
5797 MakeCellVisible( m_currentCellCoords.GetRow() - 1,
5798 m_currentCellCoords.GetCol() );
5799
5800 SetCurrentCell( m_currentCellCoords.GetRow() - 1,
5801 m_currentCellCoords.GetCol() );
5802
5803 return TRUE;
5804 }
5805
5806 return FALSE;
5807 }
5808
5809
5810 bool wxGrid::MoveCursorDown()
5811 {
5812 // TODO: allow for scrolling
5813 //
5814 if ( m_currentCellCoords != wxGridNoCellCoords &&
5815 m_currentCellCoords.GetRow() < m_numRows-1 )
5816 {
5817 MakeCellVisible( m_currentCellCoords.GetRow() + 1,
5818 m_currentCellCoords.GetCol() );
5819
5820 SetCurrentCell( m_currentCellCoords.GetRow() + 1,
5821 m_currentCellCoords.GetCol() );
5822
5823 return TRUE;
5824 }
5825
5826 return FALSE;
5827 }
5828
5829
5830 bool wxGrid::MoveCursorLeft()
5831 {
5832 if ( m_currentCellCoords != wxGridNoCellCoords &&
5833 m_currentCellCoords.GetCol() > 0 )
5834 {
5835 MakeCellVisible( m_currentCellCoords.GetRow(),
5836 m_currentCellCoords.GetCol() - 1 );
5837
5838 SetCurrentCell( m_currentCellCoords.GetRow(),
5839 m_currentCellCoords.GetCol() - 1 );
5840
5841 return TRUE;
5842 }
5843
5844 return FALSE;
5845 }
5846
5847
5848 bool wxGrid::MoveCursorRight()
5849 {
5850 if ( m_currentCellCoords != wxGridNoCellCoords &&
5851 m_currentCellCoords.GetCol() < m_numCols - 1 )
5852 {
5853 MakeCellVisible( m_currentCellCoords.GetRow(),
5854 m_currentCellCoords.GetCol() + 1 );
5855
5856 SetCurrentCell( m_currentCellCoords.GetRow(),
5857 m_currentCellCoords.GetCol() + 1 );
5858
5859 return TRUE;
5860 }
5861
5862 return FALSE;
5863 }
5864
5865
5866 bool wxGrid::MovePageUp()
5867 {
5868 if ( m_currentCellCoords == wxGridNoCellCoords ) return FALSE;
5869
5870 int row = m_currentCellCoords.GetRow();
5871 if ( row > 0 )
5872 {
5873 int cw, ch;
5874 m_gridWin->GetClientSize( &cw, &ch );
5875
5876 int y = GetRowTop(row);
5877 int newRow = YToRow( y - ch + 1 );
5878 if ( newRow == -1 )
5879 {
5880 newRow = 0;
5881 }
5882 else if ( newRow == row )
5883 {
5884 newRow = row - 1;
5885 }
5886
5887 MakeCellVisible( newRow, m_currentCellCoords.GetCol() );
5888 SetCurrentCell( newRow, m_currentCellCoords.GetCol() );
5889
5890 return TRUE;
5891 }
5892
5893 return FALSE;
5894 }
5895
5896 bool wxGrid::MovePageDown()
5897 {
5898 if ( m_currentCellCoords == wxGridNoCellCoords ) return FALSE;
5899
5900 int row = m_currentCellCoords.GetRow();
5901 if ( row < m_numRows )
5902 {
5903 int cw, ch;
5904 m_gridWin->GetClientSize( &cw, &ch );
5905
5906 int y = GetRowTop(row);
5907 int newRow = YToRow( y + ch );
5908 if ( newRow == -1 )
5909 {
5910 newRow = m_numRows - 1;
5911 }
5912 else if ( newRow == row )
5913 {
5914 newRow = row + 1;
5915 }
5916
5917 MakeCellVisible( newRow, m_currentCellCoords.GetCol() );
5918 SetCurrentCell( newRow, m_currentCellCoords.GetCol() );
5919
5920 return TRUE;
5921 }
5922
5923 return FALSE;
5924 }
5925
5926 bool wxGrid::MoveCursorUpBlock()
5927 {
5928 if ( m_table &&
5929 m_currentCellCoords != wxGridNoCellCoords &&
5930 m_currentCellCoords.GetRow() > 0 )
5931 {
5932 int row = m_currentCellCoords.GetRow();
5933 int col = m_currentCellCoords.GetCol();
5934
5935 if ( m_table->IsEmptyCell(row, col) )
5936 {
5937 // starting in an empty cell: find the next block of
5938 // non-empty cells
5939 //
5940 while ( row > 0 )
5941 {
5942 row-- ;
5943 if ( !(m_table->IsEmptyCell(row, col)) ) break;
5944 }
5945 }
5946 else if ( m_table->IsEmptyCell(row-1, col) )
5947 {
5948 // starting at the top of a block: find the next block
5949 //
5950 row--;
5951 while ( row > 0 )
5952 {
5953 row-- ;
5954 if ( !(m_table->IsEmptyCell(row, col)) ) break;
5955 }
5956 }
5957 else
5958 {
5959 // starting within a block: find the top of the block
5960 //
5961 while ( row > 0 )
5962 {
5963 row-- ;
5964 if ( m_table->IsEmptyCell(row, col) )
5965 {
5966 row++ ;
5967 break;
5968 }
5969 }
5970 }
5971
5972 MakeCellVisible( row, col );
5973 SetCurrentCell( row, col );
5974
5975 return TRUE;
5976 }
5977
5978 return FALSE;
5979 }
5980
5981 bool wxGrid::MoveCursorDownBlock()
5982 {
5983 if ( m_table &&
5984 m_currentCellCoords != wxGridNoCellCoords &&
5985 m_currentCellCoords.GetRow() < m_numRows-1 )
5986 {
5987 int row = m_currentCellCoords.GetRow();
5988 int col = m_currentCellCoords.GetCol();
5989
5990 if ( m_table->IsEmptyCell(row, col) )
5991 {
5992 // starting in an empty cell: find the next block of
5993 // non-empty cells
5994 //
5995 while ( row < m_numRows-1 )
5996 {
5997 row++ ;
5998 if ( !(m_table->IsEmptyCell(row, col)) ) break;
5999 }
6000 }
6001 else if ( m_table->IsEmptyCell(row+1, col) )
6002 {
6003 // starting at the bottom of a block: find the next block
6004 //
6005 row++;
6006 while ( row < m_numRows-1 )
6007 {
6008 row++ ;
6009 if ( !(m_table->IsEmptyCell(row, col)) ) break;
6010 }
6011 }
6012 else
6013 {
6014 // starting within a block: find the bottom of the block
6015 //
6016 while ( row < m_numRows-1 )
6017 {
6018 row++ ;
6019 if ( m_table->IsEmptyCell(row, col) )
6020 {
6021 row-- ;
6022 break;
6023 }
6024 }
6025 }
6026
6027 MakeCellVisible( row, col );
6028 SetCurrentCell( row, col );
6029
6030 return TRUE;
6031 }
6032
6033 return FALSE;
6034 }
6035
6036 bool wxGrid::MoveCursorLeftBlock()
6037 {
6038 if ( m_table &&
6039 m_currentCellCoords != wxGridNoCellCoords &&
6040 m_currentCellCoords.GetCol() > 0 )
6041 {
6042 int row = m_currentCellCoords.GetRow();
6043 int col = m_currentCellCoords.GetCol();
6044
6045 if ( m_table->IsEmptyCell(row, col) )
6046 {
6047 // starting in an empty cell: find the next block of
6048 // non-empty cells
6049 //
6050 while ( col > 0 )
6051 {
6052 col-- ;
6053 if ( !(m_table->IsEmptyCell(row, col)) ) break;
6054 }
6055 }
6056 else if ( m_table->IsEmptyCell(row, col-1) )
6057 {
6058 // starting at the left of a block: find the next block
6059 //
6060 col--;
6061 while ( col > 0 )
6062 {
6063 col-- ;
6064 if ( !(m_table->IsEmptyCell(row, col)) ) break;
6065 }
6066 }
6067 else
6068 {
6069 // starting within a block: find the left of the block
6070 //
6071 while ( col > 0 )
6072 {
6073 col-- ;
6074 if ( m_table->IsEmptyCell(row, col) )
6075 {
6076 col++ ;
6077 break;
6078 }
6079 }
6080 }
6081
6082 MakeCellVisible( row, col );
6083 SetCurrentCell( row, col );
6084
6085 return TRUE;
6086 }
6087
6088 return FALSE;
6089 }
6090
6091 bool wxGrid::MoveCursorRightBlock()
6092 {
6093 if ( m_table &&
6094 m_currentCellCoords != wxGridNoCellCoords &&
6095 m_currentCellCoords.GetCol() < m_numCols-1 )
6096 {
6097 int row = m_currentCellCoords.GetRow();
6098 int col = m_currentCellCoords.GetCol();
6099
6100 if ( m_table->IsEmptyCell(row, col) )
6101 {
6102 // starting in an empty cell: find the next block of
6103 // non-empty cells
6104 //
6105 while ( col < m_numCols-1 )
6106 {
6107 col++ ;
6108 if ( !(m_table->IsEmptyCell(row, col)) ) break;
6109 }
6110 }
6111 else if ( m_table->IsEmptyCell(row, col+1) )
6112 {
6113 // starting at the right of a block: find the next block
6114 //
6115 col++;
6116 while ( col < m_numCols-1 )
6117 {
6118 col++ ;
6119 if ( !(m_table->IsEmptyCell(row, col)) ) break;
6120 }
6121 }
6122 else
6123 {
6124 // starting within a block: find the right of the block
6125 //
6126 while ( col < m_numCols-1 )
6127 {
6128 col++ ;
6129 if ( m_table->IsEmptyCell(row, col) )
6130 {
6131 col-- ;
6132 break;
6133 }
6134 }
6135 }
6136
6137 MakeCellVisible( row, col );
6138 SetCurrentCell( row, col );
6139
6140 return TRUE;
6141 }
6142
6143 return FALSE;
6144 }
6145
6146
6147
6148 //
6149 // ------ Label values and formatting
6150 //
6151
6152 void wxGrid::GetRowLabelAlignment( int *horiz, int *vert )
6153 {
6154 *horiz = m_rowLabelHorizAlign;
6155 *vert = m_rowLabelVertAlign;
6156 }
6157
6158 void wxGrid::GetColLabelAlignment( int *horiz, int *vert )
6159 {
6160 *horiz = m_colLabelHorizAlign;
6161 *vert = m_colLabelVertAlign;
6162 }
6163
6164 wxString wxGrid::GetRowLabelValue( int row )
6165 {
6166 if ( m_table )
6167 {
6168 return m_table->GetRowLabelValue( row );
6169 }
6170 else
6171 {
6172 wxString s;
6173 s << row;
6174 return s;
6175 }
6176 }
6177
6178 wxString wxGrid::GetColLabelValue( int col )
6179 {
6180 if ( m_table )
6181 {
6182 return m_table->GetColLabelValue( col );
6183 }
6184 else
6185 {
6186 wxString s;
6187 s << col;
6188 return s;
6189 }
6190 }
6191
6192
6193 void wxGrid::SetRowLabelSize( int width )
6194 {
6195 width = wxMax( width, 0 );
6196 if ( width != m_rowLabelWidth )
6197 {
6198 if ( width == 0 )
6199 {
6200 m_rowLabelWin->Show( FALSE );
6201 m_cornerLabelWin->Show( FALSE );
6202 }
6203 else if ( m_rowLabelWidth == 0 )
6204 {
6205 m_rowLabelWin->Show( TRUE );
6206 if ( m_colLabelHeight > 0 ) m_cornerLabelWin->Show( TRUE );
6207 }
6208
6209 m_rowLabelWidth = width;
6210 CalcWindowSizes();
6211 Refresh( TRUE );
6212 }
6213 }
6214
6215
6216 void wxGrid::SetColLabelSize( int height )
6217 {
6218 height = wxMax( height, 0 );
6219 if ( height != m_colLabelHeight )
6220 {
6221 if ( height == 0 )
6222 {
6223 m_colLabelWin->Show( FALSE );
6224 m_cornerLabelWin->Show( FALSE );
6225 }
6226 else if ( m_colLabelHeight == 0 )
6227 {
6228 m_colLabelWin->Show( TRUE );
6229 if ( m_rowLabelWidth > 0 ) m_cornerLabelWin->Show( TRUE );
6230 }
6231
6232 m_colLabelHeight = height;
6233 CalcWindowSizes();
6234 Refresh( TRUE );
6235 }
6236 }
6237
6238
6239 void wxGrid::SetLabelBackgroundColour( const wxColour& colour )
6240 {
6241 if ( m_labelBackgroundColour != colour )
6242 {
6243 m_labelBackgroundColour = colour;
6244 m_rowLabelWin->SetBackgroundColour( colour );
6245 m_colLabelWin->SetBackgroundColour( colour );
6246 m_cornerLabelWin->SetBackgroundColour( colour );
6247
6248 if ( !GetBatchCount() )
6249 {
6250 m_rowLabelWin->Refresh();
6251 m_colLabelWin->Refresh();
6252 m_cornerLabelWin->Refresh();
6253 }
6254 }
6255 }
6256
6257 void wxGrid::SetLabelTextColour( const wxColour& colour )
6258 {
6259 if ( m_labelTextColour != colour )
6260 {
6261 m_labelTextColour = colour;
6262 if ( !GetBatchCount() )
6263 {
6264 m_rowLabelWin->Refresh();
6265 m_colLabelWin->Refresh();
6266 }
6267 }
6268 }
6269
6270 void wxGrid::SetLabelFont( const wxFont& font )
6271 {
6272 m_labelFont = font;
6273 if ( !GetBatchCount() )
6274 {
6275 m_rowLabelWin->Refresh();
6276 m_colLabelWin->Refresh();
6277 }
6278 }
6279
6280 void wxGrid::SetRowLabelAlignment( int horiz, int vert )
6281 {
6282 if ( horiz == wxLEFT || horiz == wxCENTRE || horiz == wxRIGHT )
6283 {
6284 m_rowLabelHorizAlign = horiz;
6285 }
6286
6287 if ( vert == wxTOP || vert == wxCENTRE || vert == wxBOTTOM )
6288 {
6289 m_rowLabelVertAlign = vert;
6290 }
6291
6292 if ( !GetBatchCount() )
6293 {
6294 m_rowLabelWin->Refresh();
6295 }
6296 }
6297
6298 void wxGrid::SetColLabelAlignment( int horiz, int vert )
6299 {
6300 if ( horiz == wxLEFT || horiz == wxCENTRE || horiz == wxRIGHT )
6301 {
6302 m_colLabelHorizAlign = horiz;
6303 }
6304
6305 if ( vert == wxTOP || vert == wxCENTRE || vert == wxBOTTOM )
6306 {
6307 m_colLabelVertAlign = vert;
6308 }
6309
6310 if ( !GetBatchCount() )
6311 {
6312 m_colLabelWin->Refresh();
6313 }
6314 }
6315
6316 void wxGrid::SetRowLabelValue( int row, const wxString& s )
6317 {
6318 if ( m_table )
6319 {
6320 m_table->SetRowLabelValue( row, s );
6321 if ( !GetBatchCount() )
6322 {
6323 wxRect rect = CellToRect( row, 0);
6324 if ( rect.height > 0 )
6325 {
6326 CalcScrolledPosition(0, rect.y, &rect.x, &rect.y);
6327 rect.x = m_left;
6328 rect.width = m_rowLabelWidth;
6329 m_rowLabelWin->Refresh( TRUE, &rect );
6330 }
6331 }
6332 }
6333 }
6334
6335 void wxGrid::SetColLabelValue( int col, const wxString& s )
6336 {
6337 if ( m_table )
6338 {
6339 m_table->SetColLabelValue( col, s );
6340 if ( !GetBatchCount() )
6341 {
6342 wxRect rect = CellToRect( 0, col );
6343 if ( rect.width > 0 )
6344 {
6345 CalcScrolledPosition(rect.x, 0, &rect.x, &rect.y);
6346 rect.y = m_top;
6347 rect.height = m_colLabelHeight;
6348 m_colLabelWin->Refresh( TRUE, &rect );
6349 }
6350 }
6351 }
6352 }
6353
6354 void wxGrid::SetGridLineColour( const wxColour& colour )
6355 {
6356 if ( m_gridLineColour != colour )
6357 {
6358 m_gridLineColour = colour;
6359
6360 wxClientDC dc( m_gridWin );
6361 PrepareDC( dc );
6362 DrawAllGridLines( dc, wxRegion() );
6363 }
6364 }
6365
6366 void wxGrid::EnableGridLines( bool enable )
6367 {
6368 if ( enable != m_gridLinesEnabled )
6369 {
6370 m_gridLinesEnabled = enable;
6371
6372 if ( !GetBatchCount() )
6373 {
6374 if ( enable )
6375 {
6376 wxClientDC dc( m_gridWin );
6377 PrepareDC( dc );
6378 DrawAllGridLines( dc, wxRegion() );
6379 }
6380 else
6381 {
6382 m_gridWin->Refresh();
6383 }
6384 }
6385 }
6386 }
6387
6388
6389 int wxGrid::GetDefaultRowSize()
6390 {
6391 return m_defaultRowHeight;
6392 }
6393
6394 int wxGrid::GetRowSize( int row )
6395 {
6396 wxCHECK_MSG( row >= 0 && row < m_numRows, 0, _T("invalid row index") );
6397
6398 return GetRowHeight(row);
6399 }
6400
6401 int wxGrid::GetDefaultColSize()
6402 {
6403 return m_defaultColWidth;
6404 }
6405
6406 int wxGrid::GetColSize( int col )
6407 {
6408 wxCHECK_MSG( col >= 0 && col < m_numCols, 0, _T("invalid column index") );
6409
6410 return GetColWidth(col);
6411 }
6412
6413 // ============================================================================
6414 // access to the grid attributes: each of them has a default value in the grid
6415 // itself and may be overidden on a per-cell basis
6416 // ============================================================================
6417
6418 // ----------------------------------------------------------------------------
6419 // setting default attributes
6420 // ----------------------------------------------------------------------------
6421
6422 void wxGrid::SetDefaultCellBackgroundColour( const wxColour& col )
6423 {
6424 m_defaultCellAttr->SetBackgroundColour(col);
6425 #ifdef __WXGTK__
6426 m_gridWin->SetBackgroundColour(col);
6427 #endif
6428 }
6429
6430 void wxGrid::SetDefaultCellTextColour( const wxColour& col )
6431 {
6432 m_defaultCellAttr->SetTextColour(col);
6433 }
6434
6435 void wxGrid::SetDefaultCellAlignment( int horiz, int vert )
6436 {
6437 m_defaultCellAttr->SetAlignment(horiz, vert);
6438 }
6439
6440 void wxGrid::SetDefaultCellFont( const wxFont& font )
6441 {
6442 m_defaultCellAttr->SetFont(font);
6443 }
6444
6445 void wxGrid::SetDefaultRenderer(wxGridCellRenderer *renderer)
6446 {
6447 m_defaultCellAttr->SetRenderer(renderer);
6448 }
6449
6450 void wxGrid::SetDefaultEditor(wxGridCellEditor *editor)
6451 {
6452 m_defaultCellAttr->SetEditor(editor);
6453 }
6454
6455 // ----------------------------------------------------------------------------
6456 // access to the default attrbiutes
6457 // ----------------------------------------------------------------------------
6458
6459 wxColour wxGrid::GetDefaultCellBackgroundColour()
6460 {
6461 return m_defaultCellAttr->GetBackgroundColour();
6462 }
6463
6464 wxColour wxGrid::GetDefaultCellTextColour()
6465 {
6466 return m_defaultCellAttr->GetTextColour();
6467 }
6468
6469 wxFont wxGrid::GetDefaultCellFont()
6470 {
6471 return m_defaultCellAttr->GetFont();
6472 }
6473
6474 void wxGrid::GetDefaultCellAlignment( int *horiz, int *vert )
6475 {
6476 m_defaultCellAttr->GetAlignment(horiz, vert);
6477 }
6478
6479 wxGridCellRenderer *wxGrid::GetDefaultRenderer() const
6480 {
6481 return m_defaultCellAttr->GetRenderer(NULL,0,0);
6482 }
6483
6484 wxGridCellEditor *wxGrid::GetDefaultEditor() const
6485 {
6486 return m_defaultCellAttr->GetEditor(NULL,0,0);
6487 }
6488
6489 // ----------------------------------------------------------------------------
6490 // access to cell attributes
6491 // ----------------------------------------------------------------------------
6492
6493 wxColour wxGrid::GetCellBackgroundColour(int row, int col)
6494 {
6495 wxGridCellAttr *attr = GetCellAttr(row, col);
6496 wxColour colour = attr->GetBackgroundColour();
6497 attr->DecRef();
6498 return colour;
6499 }
6500
6501 wxColour wxGrid::GetCellTextColour( int row, int col )
6502 {
6503 wxGridCellAttr *attr = GetCellAttr(row, col);
6504 wxColour colour = attr->GetTextColour();
6505 attr->DecRef();
6506 return colour;
6507 }
6508
6509 wxFont wxGrid::GetCellFont( int row, int col )
6510 {
6511 wxGridCellAttr *attr = GetCellAttr(row, col);
6512 wxFont font = attr->GetFont();
6513 attr->DecRef();
6514 return font;
6515 }
6516
6517 void wxGrid::GetCellAlignment( int row, int col, int *horiz, int *vert )
6518 {
6519 wxGridCellAttr *attr = GetCellAttr(row, col);
6520 attr->GetAlignment(horiz, vert);
6521 attr->DecRef();
6522 }
6523
6524 wxGridCellRenderer* wxGrid::GetCellRenderer(int row, int col)
6525 {
6526 wxGridCellAttr* attr = GetCellAttr(row, col);
6527 wxGridCellRenderer* renderer = attr->GetRenderer(this, row, col);
6528 attr->DecRef();
6529 return renderer;
6530 }
6531
6532 wxGridCellEditor* wxGrid::GetCellEditor(int row, int col)
6533 {
6534 wxGridCellAttr* attr = GetCellAttr(row, col);
6535 wxGridCellEditor* editor = attr->GetEditor(this, row, col);
6536 attr->DecRef();
6537 return editor;
6538 }
6539
6540 bool wxGrid::IsReadOnly(int row, int col) const
6541 {
6542 wxGridCellAttr* attr = GetCellAttr(row, col);
6543 bool isReadOnly = attr->IsReadOnly();
6544 attr->DecRef();
6545 return isReadOnly;
6546 }
6547
6548 // ----------------------------------------------------------------------------
6549 // attribute support: cache, automatic provider creation, ...
6550 // ----------------------------------------------------------------------------
6551
6552 bool wxGrid::CanHaveAttributes()
6553 {
6554 if ( !m_table )
6555 {
6556 return FALSE;
6557 }
6558
6559 return m_table->CanHaveAttributes();
6560 }
6561
6562 void wxGrid::ClearAttrCache()
6563 {
6564 if ( m_attrCache.row != -1 )
6565 {
6566 wxSafeDecRef(m_attrCache.attr);
6567 m_attrCache.row = -1;
6568 }
6569 }
6570
6571 void wxGrid::CacheAttr(int row, int col, wxGridCellAttr *attr) const
6572 {
6573 wxGrid *self = (wxGrid *)this; // const_cast
6574
6575 self->ClearAttrCache();
6576 self->m_attrCache.row = row;
6577 self->m_attrCache.col = col;
6578 self->m_attrCache.attr = attr;
6579 wxSafeIncRef(attr);
6580 }
6581
6582 bool wxGrid::LookupAttr(int row, int col, wxGridCellAttr **attr) const
6583 {
6584 if ( row == m_attrCache.row && col == m_attrCache.col )
6585 {
6586 *attr = m_attrCache.attr;
6587 wxSafeIncRef(m_attrCache.attr);
6588
6589 #ifdef DEBUG_ATTR_CACHE
6590 gs_nAttrCacheHits++;
6591 #endif
6592
6593 return TRUE;
6594 }
6595 else
6596 {
6597 #ifdef DEBUG_ATTR_CACHE
6598 gs_nAttrCacheMisses++;
6599 #endif
6600 return FALSE;
6601 }
6602 }
6603
6604 wxGridCellAttr *wxGrid::GetCellAttr(int row, int col) const
6605 {
6606 wxGridCellAttr *attr;
6607 if ( !LookupAttr(row, col, &attr) )
6608 {
6609 attr = m_table ? m_table->GetAttr(row, col) : (wxGridCellAttr *)NULL;
6610 CacheAttr(row, col, attr);
6611 }
6612 if (attr)
6613 {
6614 attr->SetDefAttr(m_defaultCellAttr);
6615 }
6616 else
6617 {
6618 attr = m_defaultCellAttr;
6619 attr->IncRef();
6620 }
6621
6622 return attr;
6623 }
6624
6625 wxGridCellAttr *wxGrid::GetOrCreateCellAttr(int row, int col) const
6626 {
6627 wxGridCellAttr *attr;
6628 if ( !LookupAttr(row, col, &attr) || !attr )
6629 {
6630 wxASSERT_MSG( m_table,
6631 _T("we may only be called if CanHaveAttributes() "
6632 "returned TRUE and then m_table should be !NULL") );
6633
6634 attr = m_table->GetAttr(row, col);
6635 if ( !attr )
6636 {
6637 attr = new wxGridCellAttr;
6638
6639 // artificially inc the ref count to match DecRef() in caller
6640 attr->IncRef();
6641
6642 m_table->SetAttr(attr, row, col);
6643 }
6644
6645 CacheAttr(row, col, attr);
6646 }
6647 attr->SetDefAttr(m_defaultCellAttr);
6648 return attr;
6649 }
6650
6651 // ----------------------------------------------------------------------------
6652 // setting cell attributes: this is forwarded to the table
6653 // ----------------------------------------------------------------------------
6654
6655 void wxGrid::SetRowAttr(int row, wxGridCellAttr *attr)
6656 {
6657 if ( CanHaveAttributes() )
6658 {
6659 m_table->SetRowAttr(attr, row);
6660 }
6661 else
6662 {
6663 wxSafeDecRef(attr);
6664 }
6665 }
6666
6667 void wxGrid::SetColAttr(int col, wxGridCellAttr *attr)
6668 {
6669 if ( CanHaveAttributes() )
6670 {
6671 m_table->SetColAttr(attr, col);
6672 }
6673 else
6674 {
6675 wxSafeDecRef(attr);
6676 }
6677 }
6678
6679 void wxGrid::SetCellBackgroundColour( int row, int col, const wxColour& colour )
6680 {
6681 if ( CanHaveAttributes() )
6682 {
6683 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
6684 attr->SetBackgroundColour(colour);
6685 attr->DecRef();
6686 }
6687 }
6688
6689 void wxGrid::SetCellTextColour( int row, int col, const wxColour& colour )
6690 {
6691 if ( CanHaveAttributes() )
6692 {
6693 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
6694 attr->SetTextColour(colour);
6695 attr->DecRef();
6696 }
6697 }
6698
6699 void wxGrid::SetCellFont( int row, int col, const wxFont& font )
6700 {
6701 if ( CanHaveAttributes() )
6702 {
6703 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
6704 attr->SetFont(font);
6705 attr->DecRef();
6706 }
6707 }
6708
6709 void wxGrid::SetCellAlignment( int row, int col, int horiz, int vert )
6710 {
6711 if ( CanHaveAttributes() )
6712 {
6713 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
6714 attr->SetAlignment(horiz, vert);
6715 attr->DecRef();
6716 }
6717 }
6718
6719 void wxGrid::SetCellRenderer(int row, int col, wxGridCellRenderer *renderer)
6720 {
6721 if ( CanHaveAttributes() )
6722 {
6723 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
6724 attr->SetRenderer(renderer);
6725 attr->DecRef();
6726 }
6727 }
6728
6729 void wxGrid::SetCellEditor(int row, int col, wxGridCellEditor* editor)
6730 {
6731 if ( CanHaveAttributes() )
6732 {
6733 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
6734 attr->SetEditor(editor);
6735 attr->DecRef();
6736 }
6737 }
6738
6739 void wxGrid::SetReadOnly(int row, int col, bool isReadOnly)
6740 {
6741 if ( CanHaveAttributes() )
6742 {
6743 wxGridCellAttr *attr = GetOrCreateCellAttr(row, col);
6744 attr->SetReadOnly(isReadOnly);
6745 attr->DecRef();
6746 }
6747 }
6748
6749 // ----------------------------------------------------------------------------
6750 // Data type registration
6751 // ----------------------------------------------------------------------------
6752
6753 void wxGrid::RegisterDataType(const wxString& typeName,
6754 wxGridCellRenderer* renderer,
6755 wxGridCellEditor* editor)
6756 {
6757 m_typeRegistry->RegisterDataType(typeName, renderer, editor);
6758 }
6759
6760
6761 wxGridCellEditor* wxGrid::GetDefaultEditorForCell(int row, int col) const
6762 {
6763 wxString typeName = m_table->GetTypeName(row, col);
6764 return GetDefaultEditorForType(typeName);
6765 }
6766
6767 wxGridCellRenderer* wxGrid::GetDefaultRendererForCell(int row, int col) const
6768 {
6769 wxString typeName = m_table->GetTypeName(row, col);
6770 return GetDefaultRendererForType(typeName);
6771 }
6772
6773 wxGridCellEditor*
6774 wxGrid::GetDefaultEditorForType(const wxString& typeName) const
6775 {
6776 int index = m_typeRegistry->FindDataType(typeName);
6777 if (index == -1) {
6778 // Should we force the failure here or let it fallback to string handling???
6779 // wxFAIL_MSG(wxT("Unknown data type name"));
6780 return NULL;
6781 }
6782 return m_typeRegistry->GetEditor(index);
6783 }
6784
6785 wxGridCellRenderer*
6786 wxGrid::GetDefaultRendererForType(const wxString& typeName) const
6787 {
6788 int index = m_typeRegistry->FindDataType(typeName);
6789 if (index == -1) {
6790 // Should we force the failure here or let it fallback to string handling???
6791 // wxFAIL_MSG(wxT("Unknown data type name"));
6792 return NULL;
6793 }
6794 return m_typeRegistry->GetRenderer(index);
6795 }
6796
6797
6798 // ----------------------------------------------------------------------------
6799 // row/col size
6800 // ----------------------------------------------------------------------------
6801
6802 void wxGrid::EnableDragRowSize( bool enable )
6803 {
6804 m_canDragRowSize = enable;
6805 }
6806
6807
6808 void wxGrid::EnableDragColSize( bool enable )
6809 {
6810 m_canDragColSize = enable;
6811 }
6812
6813 void wxGrid::EnableDragGridSize( bool enable )
6814 {
6815 m_canDragGridSize = enable;
6816 }
6817
6818
6819 void wxGrid::SetDefaultRowSize( int height, bool resizeExistingRows )
6820 {
6821 m_defaultRowHeight = wxMax( height, WXGRID_MIN_ROW_HEIGHT );
6822
6823 if ( resizeExistingRows )
6824 {
6825 InitRowHeights();
6826
6827 CalcDimensions();
6828 }
6829 }
6830
6831 void wxGrid::SetRowSize( int row, int height )
6832 {
6833 wxCHECK_RET( row >= 0 && row < m_numRows, _T("invalid row index") );
6834
6835 if ( m_rowHeights.IsEmpty() )
6836 {
6837 // need to really create the array
6838 InitRowHeights();
6839 }
6840
6841 int h = wxMax( 0, height );
6842 int diff = h - m_rowHeights[row];
6843
6844 m_rowHeights[row] = h;
6845 int i;
6846 for ( i = row; i < m_numRows; i++ )
6847 {
6848 m_rowBottoms[i] += diff;
6849 }
6850 CalcDimensions();
6851 }
6852
6853 void wxGrid::SetDefaultColSize( int width, bool resizeExistingCols )
6854 {
6855 m_defaultColWidth = wxMax( width, WXGRID_MIN_COL_WIDTH );
6856
6857 if ( resizeExistingCols )
6858 {
6859 InitColWidths();
6860
6861 CalcDimensions();
6862 }
6863 }
6864
6865 void wxGrid::SetColSize( int col, int width )
6866 {
6867 wxCHECK_RET( col >= 0 && col < m_numCols, _T("invalid column index") );
6868
6869 // should we check that it's bigger than GetColMinimalWidth(col) here?
6870
6871 if ( m_colWidths.IsEmpty() )
6872 {
6873 // need to really create the array
6874 InitColWidths();
6875 }
6876
6877 int w = wxMax( 0, width );
6878 int diff = w - m_colWidths[col];
6879 m_colWidths[col] = w;
6880
6881 int i;
6882 for ( i = col; i < m_numCols; i++ )
6883 {
6884 m_colRights[i] += diff;
6885 }
6886 CalcDimensions();
6887 }
6888
6889
6890 void wxGrid::SetColMinimalWidth( int col, int width )
6891 {
6892 m_colMinWidths.Put(col, width);
6893 }
6894
6895 void wxGrid::SetRowMinimalHeight( int row, int width )
6896 {
6897 m_rowMinHeights.Put(row, width);
6898 }
6899
6900 int wxGrid::GetColMinimalWidth(int col) const
6901 {
6902 long value = m_colMinWidths.Get(col);
6903 return value != wxNOT_FOUND ? (int)value : WXGRID_MIN_COL_WIDTH;
6904 }
6905
6906 int wxGrid::GetRowMinimalHeight(int row) const
6907 {
6908 long value = m_rowMinHeights.Get(row);
6909 return value != wxNOT_FOUND ? (int)value : WXGRID_MIN_ROW_HEIGHT;
6910 }
6911
6912 // ----------------------------------------------------------------------------
6913 // auto sizing
6914 // ----------------------------------------------------------------------------
6915
6916 void wxGrid::AutoSizeColOrRow( int colOrRow, bool setAsMin, bool column )
6917 {
6918 wxClientDC dc(m_gridWin);
6919
6920 int row, col;
6921 if ( column )
6922 col = colOrRow;
6923 else
6924 row = colOrRow;
6925
6926 wxCoord extent, extentMax = 0;
6927 int max = column ? m_numRows : m_numCols;
6928 for ( int rowOrCol = 0; rowOrCol < max; rowOrCol++ )
6929 {
6930 if ( column )
6931 row = rowOrCol;
6932 else
6933 col = rowOrCol;
6934
6935 wxGridCellAttr* attr = GetCellAttr(row, col);
6936 wxGridCellRenderer* renderer = attr->GetRenderer(this, row, col);
6937 if ( renderer )
6938 {
6939 wxSize size = renderer->GetBestSize(*this, *attr, dc, row, col);
6940 extent = column ? size.x : size.y;
6941 if ( extent > extentMax )
6942 {
6943 extentMax = extent;
6944 }
6945 }
6946
6947 attr->DecRef();
6948 }
6949
6950 // now also compare with the column label extent
6951 wxCoord w, h;
6952 dc.SetFont( GetLabelFont() );
6953 dc.GetTextExtent( column ? GetColLabelValue(col)
6954 : GetRowLabelValue(row), &w, &h );
6955 extent = column ? w : h;
6956 if ( extent > extentMax )
6957 {
6958 extentMax = extent;
6959 }
6960
6961 if ( !extentMax )
6962 {
6963 // empty column - give default extent (notice that if extentMax is less
6964 // than default extent but != 0, it's ok)
6965 extentMax = column ? m_defaultColWidth : m_defaultRowHeight;
6966 }
6967 else
6968 {
6969 // leave some space around text
6970 extentMax += 10;
6971 }
6972
6973 if ( column )
6974 SetColSize(col, extentMax);
6975 else
6976 SetRowSize(row, extentMax);
6977
6978 if ( setAsMin )
6979 {
6980 if ( column )
6981 SetColMinimalWidth(col, extentMax);
6982 else
6983 SetRowMinimalHeight(row, extentMax);
6984 }
6985 }
6986
6987 int wxGrid::SetOrCalcColumnSizes(bool calcOnly, bool setAsMin)
6988 {
6989 int width = m_rowLabelWidth;
6990
6991 for ( int col = 0; col < m_numCols; col++ )
6992 {
6993 if ( !calcOnly )
6994 {
6995 AutoSizeColumn(col, setAsMin);
6996 }
6997
6998 width += GetColWidth(col);
6999 }
7000
7001 return width;
7002 }
7003
7004 int wxGrid::SetOrCalcRowSizes(bool calcOnly, bool setAsMin)
7005 {
7006 int height = m_colLabelHeight;
7007
7008 for ( int row = 0; row < m_numRows; row++ )
7009 {
7010 if ( !calcOnly )
7011 {
7012 AutoSizeRow(row, setAsMin);
7013 }
7014
7015 height += GetRowHeight(row);
7016 }
7017
7018 return height;
7019 }
7020
7021 void wxGrid::AutoSize()
7022 {
7023 // set the size too
7024 SetSize(SetOrCalcColumnSizes(FALSE), SetOrCalcRowSizes(FALSE));
7025 }
7026
7027 wxSize wxGrid::DoGetBestSize() const
7028 {
7029 // don't set sizes, only calculate them
7030 wxGrid *self = (wxGrid *)this; // const_cast
7031
7032 return wxSize(self->SetOrCalcColumnSizes(TRUE),
7033 self->SetOrCalcRowSizes(TRUE));
7034 }
7035
7036 void wxGrid::Fit()
7037 {
7038 AutoSize();
7039 }
7040
7041 // ----------------------------------------------------------------------------
7042 // cell value accessor functions
7043 // ----------------------------------------------------------------------------
7044
7045 void wxGrid::SetCellValue( int row, int col, const wxString& s )
7046 {
7047 if ( m_table )
7048 {
7049 m_table->SetValue( row, col, s.c_str() );
7050 if ( !GetBatchCount() )
7051 {
7052 wxClientDC dc( m_gridWin );
7053 PrepareDC( dc );
7054 DrawCell( dc, wxGridCellCoords(row, col) );
7055 }
7056
7057 if ( m_currentCellCoords.GetRow() == row &&
7058 m_currentCellCoords.GetCol() == col &&
7059 IsCellEditControlEnabled())
7060 {
7061 HideCellEditControl();
7062 ShowCellEditControl(); // will reread data from table
7063 }
7064 }
7065 }
7066
7067
7068 //
7069 // ------ Block, row and col selection
7070 //
7071
7072 void wxGrid::SelectRow( int row, bool addToSelected )
7073 {
7074 wxRect r;
7075
7076 if ( IsSelection() && addToSelected )
7077 {
7078 wxRect rect[4];
7079 bool need_refresh[4];
7080 need_refresh[0] =
7081 need_refresh[1] =
7082 need_refresh[2] =
7083 need_refresh[3] = FALSE;
7084
7085 int i;
7086
7087 wxCoord oldLeft = m_selectedTopLeft.GetCol();
7088 wxCoord oldTop = m_selectedTopLeft.GetRow();
7089 wxCoord oldRight = m_selectedBottomRight.GetCol();
7090 wxCoord oldBottom = m_selectedBottomRight.GetRow();
7091
7092 if ( oldTop > row )
7093 {
7094 need_refresh[0] = TRUE;
7095 rect[0] = BlockToDeviceRect( wxGridCellCoords ( row, 0 ),
7096 wxGridCellCoords ( oldTop - 1,
7097 m_numCols - 1 ) );
7098 m_selectedTopLeft.SetRow( row );
7099 }
7100
7101 if ( oldLeft > 0 )
7102 {
7103 need_refresh[1] = TRUE;
7104 rect[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop, 0 ),
7105 wxGridCellCoords ( oldBottom,
7106 oldLeft - 1 ) );
7107
7108 m_selectedTopLeft.SetCol( 0 );
7109 }
7110
7111 if ( oldBottom < row )
7112 {
7113 need_refresh[2] = TRUE;
7114 rect[2] = BlockToDeviceRect( wxGridCellCoords ( oldBottom + 1, 0 ),
7115 wxGridCellCoords ( row,
7116 m_numCols - 1 ) );
7117 m_selectedBottomRight.SetRow( row );
7118 }
7119
7120 if ( oldRight < m_numCols - 1 )
7121 {
7122 need_refresh[3] = TRUE;
7123 rect[3] = BlockToDeviceRect( wxGridCellCoords ( oldTop ,
7124 oldRight + 1 ),
7125 wxGridCellCoords ( oldBottom,
7126 m_numCols - 1 ) );
7127 m_selectedBottomRight.SetCol( m_numCols - 1 );
7128 }
7129
7130 for (i = 0; i < 4; i++ )
7131 if ( need_refresh[i] && rect[i] != wxGridNoCellRect )
7132 m_gridWin->Refresh( FALSE, &(rect[i]) );
7133 }
7134 else
7135 {
7136 r = SelectionToDeviceRect();
7137 ClearSelection();
7138 if ( r != wxGridNoCellRect ) m_gridWin->Refresh( FALSE, &r );
7139
7140 m_selectedTopLeft.Set( row, 0 );
7141 m_selectedBottomRight.Set( row, m_numCols-1 );
7142 r = SelectionToDeviceRect();
7143 m_gridWin->Refresh( FALSE, &r );
7144 }
7145
7146 wxGridRangeSelectEvent gridEvt( GetId(),
7147 wxEVT_GRID_RANGE_SELECT,
7148 this,
7149 m_selectedTopLeft,
7150 m_selectedBottomRight );
7151
7152 GetEventHandler()->ProcessEvent(gridEvt);
7153 }
7154
7155
7156 void wxGrid::SelectCol( int col, bool addToSelected )
7157 {
7158 if ( IsSelection() && addToSelected )
7159 {
7160 wxRect rect[4];
7161 bool need_refresh[4];
7162 need_refresh[0] =
7163 need_refresh[1] =
7164 need_refresh[2] =
7165 need_refresh[3] = FALSE;
7166 int i;
7167
7168 wxCoord oldLeft = m_selectedTopLeft.GetCol();
7169 wxCoord oldTop = m_selectedTopLeft.GetRow();
7170 wxCoord oldRight = m_selectedBottomRight.GetCol();
7171 wxCoord oldBottom = m_selectedBottomRight.GetRow();
7172
7173 if ( oldLeft > col )
7174 {
7175 need_refresh[0] = TRUE;
7176 rect[0] = BlockToDeviceRect( wxGridCellCoords ( 0, col ),
7177 wxGridCellCoords ( m_numRows - 1,
7178 oldLeft - 1 ) );
7179 m_selectedTopLeft.SetCol( col );
7180 }
7181
7182 if ( oldTop > 0 )
7183 {
7184 need_refresh[1] = TRUE;
7185 rect[1] = BlockToDeviceRect( wxGridCellCoords ( 0, oldLeft ),
7186 wxGridCellCoords ( oldTop - 1,
7187 oldRight ) );
7188 m_selectedTopLeft.SetRow( 0 );
7189 }
7190
7191 if ( oldRight < col )
7192 {
7193 need_refresh[2] = TRUE;
7194 rect[2] = BlockToDeviceRect( wxGridCellCoords ( 0, oldRight + 1 ),
7195 wxGridCellCoords ( m_numRows - 1,
7196 col ) );
7197 m_selectedBottomRight.SetCol( col );
7198 }
7199
7200 if ( oldBottom < m_numRows - 1 )
7201 {
7202 need_refresh[3] = TRUE;
7203 rect[3] = BlockToDeviceRect( wxGridCellCoords ( oldBottom + 1,
7204 oldLeft ),
7205 wxGridCellCoords ( m_numRows - 1,
7206 oldRight ) );
7207 m_selectedBottomRight.SetRow( m_numRows - 1 );
7208 }
7209
7210 for (i = 0; i < 4; i++ )
7211 if ( need_refresh[i] && rect[i] != wxGridNoCellRect )
7212 m_gridWin->Refresh( FALSE, &(rect[i]) );
7213 }
7214 else
7215 {
7216 wxRect r;
7217
7218 r = SelectionToDeviceRect();
7219 ClearSelection();
7220 if ( r != wxGridNoCellRect ) m_gridWin->Refresh( FALSE, &r );
7221
7222 m_selectedTopLeft.Set( 0, col );
7223 m_selectedBottomRight.Set( m_numRows-1, col );
7224 r = SelectionToDeviceRect();
7225 m_gridWin->Refresh( FALSE, &r );
7226 }
7227
7228 wxGridRangeSelectEvent gridEvt( GetId(),
7229 wxEVT_GRID_RANGE_SELECT,
7230 this,
7231 m_selectedTopLeft,
7232 m_selectedBottomRight );
7233
7234 GetEventHandler()->ProcessEvent(gridEvt);
7235 }
7236
7237
7238 void wxGrid::SelectBlock( int topRow, int leftCol, int bottomRow, int rightCol )
7239 {
7240 int temp;
7241 wxGridCellCoords updateTopLeft, updateBottomRight;
7242
7243 if ( topRow > bottomRow )
7244 {
7245 temp = topRow;
7246 topRow = bottomRow;
7247 bottomRow = temp;
7248 }
7249
7250 if ( leftCol > rightCol )
7251 {
7252 temp = leftCol;
7253 leftCol = rightCol;
7254 rightCol = temp;
7255 }
7256
7257 updateTopLeft = wxGridCellCoords( topRow, leftCol );
7258 updateBottomRight = wxGridCellCoords( bottomRow, rightCol );
7259
7260 if ( m_selectedTopLeft != updateTopLeft ||
7261 m_selectedBottomRight != updateBottomRight )
7262 {
7263 // Compute two optimal update rectangles:
7264 // Either one rectangle is a real subset of the
7265 // other, or they are (almost) disjoint!
7266 wxRect rect[4];
7267 bool need_refresh[4];
7268 need_refresh[0] =
7269 need_refresh[1] =
7270 need_refresh[2] =
7271 need_refresh[3] = FALSE;
7272 int i;
7273
7274 // Store intermediate values
7275 wxCoord oldLeft = m_selectedTopLeft.GetCol();
7276 wxCoord oldTop = m_selectedTopLeft.GetRow();
7277 wxCoord oldRight = m_selectedBottomRight.GetCol();
7278 wxCoord oldBottom = m_selectedBottomRight.GetRow();
7279
7280 // Determine the outer/inner coordinates.
7281 if (oldLeft > leftCol)
7282 {
7283 temp = oldLeft;
7284 oldLeft = leftCol;
7285 leftCol = temp;
7286 }
7287 if (oldTop > topRow )
7288 {
7289 temp = oldTop;
7290 oldTop = topRow;
7291 topRow = temp;
7292 }
7293 if (oldRight < rightCol )
7294 {
7295 temp = oldRight;
7296 oldRight = rightCol;
7297 rightCol = temp;
7298 }
7299 if (oldBottom < bottomRow)
7300 {
7301 temp = oldBottom;
7302 oldBottom = bottomRow;
7303 bottomRow = temp;
7304 }
7305
7306 // Now, either the stuff marked old is the outer
7307 // rectangle or we don't have a situation where one
7308 // is contained in the other.
7309
7310 if ( oldLeft < leftCol )
7311 {
7312 need_refresh[0] = TRUE;
7313 rect[0] = BlockToDeviceRect( wxGridCellCoords ( oldTop,
7314 oldLeft ),
7315 wxGridCellCoords ( oldBottom,
7316 leftCol - 1 ) );
7317 }
7318
7319 if ( oldTop < topRow )
7320 {
7321 need_refresh[1] = TRUE;
7322 rect[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop,
7323 leftCol ),
7324 wxGridCellCoords ( topRow - 1,
7325 rightCol ) );
7326 }
7327
7328 if ( oldRight > rightCol )
7329 {
7330 need_refresh[2] = TRUE;
7331 rect[2] = BlockToDeviceRect( wxGridCellCoords ( oldTop,
7332 rightCol + 1 ),
7333 wxGridCellCoords ( oldBottom,
7334 oldRight ) );
7335 }
7336
7337 if ( oldBottom > bottomRow )
7338 {
7339 need_refresh[3] = TRUE;
7340 rect[3] = BlockToDeviceRect( wxGridCellCoords ( bottomRow + 1,
7341 leftCol ),
7342 wxGridCellCoords ( oldBottom,
7343 rightCol ) );
7344 }
7345
7346
7347 // Change Selection
7348 m_selectedTopLeft = updateTopLeft;
7349 m_selectedBottomRight = updateBottomRight;
7350
7351 // various Refresh() calls
7352 for (i = 0; i < 4; i++ )
7353 if ( need_refresh[i] && rect[i] != wxGridNoCellRect )
7354 m_gridWin->Refresh( FALSE, &(rect[i]) );
7355 }
7356
7357 // only generate an event if the block is not being selected by
7358 // dragging the mouse (in which case the event will be generated in
7359 // the mouse event handler)
7360 if ( !m_isDragging )
7361 {
7362 wxGridRangeSelectEvent gridEvt( GetId(),
7363 wxEVT_GRID_RANGE_SELECT,
7364 this,
7365 m_selectedTopLeft,
7366 m_selectedBottomRight );
7367
7368 GetEventHandler()->ProcessEvent(gridEvt);
7369 }
7370 }
7371
7372 void wxGrid::SelectAll()
7373 {
7374 m_selectedTopLeft.Set( 0, 0 );
7375 m_selectedBottomRight.Set( m_numRows-1, m_numCols-1 );
7376
7377 m_gridWin->Refresh();
7378 }
7379
7380
7381 void wxGrid::ClearSelection()
7382 {
7383 m_selectedTopLeft = wxGridNoCellCoords;
7384 m_selectedBottomRight = wxGridNoCellCoords;
7385 }
7386
7387
7388 // This function returns the rectangle that encloses the given block
7389 // in device coords clipped to the client size of the grid window.
7390 //
7391 wxRect wxGrid::BlockToDeviceRect( const wxGridCellCoords &topLeft,
7392 const wxGridCellCoords &bottomRight )
7393 {
7394 wxRect rect( wxGridNoCellRect );
7395 wxRect cellRect;
7396
7397 cellRect = CellToRect( topLeft );
7398 if ( cellRect != wxGridNoCellRect )
7399 {
7400 rect = cellRect;
7401 }
7402 else
7403 {
7404 rect = wxRect( 0, 0, 0, 0 );
7405 }
7406
7407 cellRect = CellToRect( bottomRight );
7408 if ( cellRect != wxGridNoCellRect )
7409 {
7410 rect += cellRect;
7411 }
7412 else
7413 {
7414 return wxGridNoCellRect;
7415 }
7416
7417 // convert to scrolled coords
7418 //
7419 int left, top, right, bottom;
7420 CalcScrolledPosition( rect.GetLeft(), rect.GetTop(), &left, &top );
7421 CalcScrolledPosition( rect.GetRight(), rect.GetBottom(), &right, &bottom );
7422
7423 int cw, ch;
7424 m_gridWin->GetClientSize( &cw, &ch );
7425
7426 rect.SetLeft( wxMax(0, left) );
7427 rect.SetTop( wxMax(0, top) );
7428 rect.SetRight( wxMin(cw, right) );
7429 rect.SetBottom( wxMin(ch, bottom) );
7430
7431 return rect;
7432 }
7433
7434
7435
7436 //
7437 // ------ Grid event classes
7438 //
7439
7440 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent, wxEvent )
7441
7442 wxGridEvent::wxGridEvent( int id, wxEventType type, wxObject* obj,
7443 int row, int col, int x, int y,
7444 bool control, bool shift, bool alt, bool meta )
7445 : wxNotifyEvent( type, id )
7446 {
7447 m_row = row;
7448 m_col = col;
7449 m_x = x;
7450 m_y = y;
7451 m_control = control;
7452 m_shift = shift;
7453 m_alt = alt;
7454 m_meta = meta;
7455
7456 SetEventObject(obj);
7457 }
7458
7459
7460 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent, wxEvent )
7461
7462 wxGridSizeEvent::wxGridSizeEvent( int id, wxEventType type, wxObject* obj,
7463 int rowOrCol, int x, int y,
7464 bool control, bool shift, bool alt, bool meta )
7465 : wxNotifyEvent( type, id )
7466 {
7467 m_rowOrCol = rowOrCol;
7468 m_x = x;
7469 m_y = y;
7470 m_control = control;
7471 m_shift = shift;
7472 m_alt = alt;
7473 m_meta = meta;
7474
7475 SetEventObject(obj);
7476 }
7477
7478
7479 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent, wxEvent )
7480
7481 wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id, wxEventType type, wxObject* obj,
7482 const wxGridCellCoords& topLeft,
7483 const wxGridCellCoords& bottomRight,
7484 bool control, bool shift, bool alt, bool meta )
7485 : wxNotifyEvent( type, id )
7486 {
7487 m_topLeft = topLeft;
7488 m_bottomRight = bottomRight;
7489 m_control = control;
7490 m_shift = shift;
7491 m_alt = alt;
7492 m_meta = meta;
7493
7494 SetEventObject(obj);
7495 }
7496
7497
7498 #endif // ifndef wxUSE_NEW_GRID
7499