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