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