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