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